Hex Artifact Content
Not logged in

Artifact 6af451108a874c070e07e2da9320d53b5c9ef44e:

File src/sqlite3.c part of check-in [fac950a173] - Update to the latest version of SQLite. Make use of the new sqlite3_strnicmp() interface. by drh on 2009-09-09 16:14:08.

0000: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
0010: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0020: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0030: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0040: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
0050: 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20  ** This file is 
0060: 61 6e 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20  an amalgamation 
0070: 6f 66 20 6d 61 6e 79 20 73 65 70 61 72 61 74 65  of many separate
0080: 20 43 20 73 6f 75 72 63 65 20 66 69 6c 65 73 20   C source files 
0090: 66 72 6f 6d 20 53 51 4c 69 74 65 0a 2a 2a 20 76  from SQLite.** v
00a0: 65 72 73 69 6f 6e 20 33 2e 36 2e 31 38 2e 20 20  ersion 3.6.18.  
00b0: 42 79 20 63 6f 6d 62 69 6e 69 6e 67 20 61 6c 6c  By combining all
00c0: 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20   the individual 
00d0: 43 20 63 6f 64 65 20 66 69 6c 65 73 20 69 6e 74  C code files int
00e0: 6f 20 74 68 69 73 20 0a 2a 2a 20 73 69 6e 67 6c  o this .** singl
00f0: 65 20 6c 61 72 67 65 20 66 69 6c 65 2c 20 74 68  e large file, th
0100: 65 20 65 6e 74 69 72 65 20 63 6f 64 65 20 63 61  e entire code ca
0110: 6e 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 61 73  n be compiled as
0120: 20 61 20 6f 6e 65 20 74 72 61 6e 73 6c 61 74 69   a one translati
0130: 6f 6e 0a 2a 2a 20 75 6e 69 74 2e 20 20 54 68 69  on.** unit.  Thi
0140: 73 20 61 6c 6c 6f 77 73 20 6d 61 6e 79 20 63 6f  s allows many co
0150: 6d 70 69 6c 65 72 73 20 74 6f 20 64 6f 20 6f 70  mpilers to do op
0160: 74 69 6d 69 7a 61 74 69 6f 6e 73 20 74 68 61 74  timizations that
0170: 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a   would not be.**
0180: 20 70 6f 73 73 69 62 6c 65 20 69 66 20 74 68 65   possible if the
0190: 20 66 69 6c 65 73 20 77 65 72 65 20 63 6f 6d 70   files were comp
01a0: 69 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e  iled separately.
01b0: 20 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d    Performance im
01c0: 70 72 6f 76 65 6d 65 6e 74 73 0a 2a 2a 20 6f 66  provements.** of
01d0: 20 35 25 20 61 72 65 20 6d 6f 72 65 20 61 72 65   5% are more are
01e0: 20 63 6f 6d 6d 6f 6e 6c 79 20 73 65 65 6e 20 77   commonly seen w
01f0: 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 6f  hen SQLite is co
0200: 6d 70 69 6c 65 64 20 61 73 20 61 20 73 69 6e 67  mpiled as a sing
0210: 6c 65 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6f  le.** translatio
0220: 6e 20 75 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  n unit..**.** Th
0230: 69 73 20 66 69 6c 65 20 69 73 20 61 6c 6c 20 79  is file is all y
0240: 6f 75 20 6e 65 65 64 20 74 6f 20 63 6f 6d 70 69  ou need to compi
0250: 6c 65 20 53 51 4c 69 74 65 2e 20 20 54 6f 20 75  le SQLite.  To u
0260: 73 65 20 53 51 4c 69 74 65 20 69 6e 20 6f 74 68  se SQLite in oth
0270: 65 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 2c 20  er.** programs, 
0280: 79 6f 75 20 6e 65 65 64 20 74 68 69 73 20 66 69  you need this fi
0290: 6c 65 20 61 6e 64 20 74 68 65 20 22 73 71 6c 69  le and the "sqli
02a0: 74 65 33 2e 68 22 20 68 65 61 64 65 72 20 66 69  te3.h" header fi
02b0: 6c 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 0a  le that defines.
02c0: 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 6d 69  ** the programmi
02d0: 6e 67 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20  ng interface to 
02e0: 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
02f0: 72 79 2e 20 20 28 49 66 20 79 6f 75 20 64 6f 20  ry.  (If you do 
0300: 6e 6f 74 20 68 61 76 65 20 0a 2a 2a 20 74 68 65  not have .** the
0310: 20 22 73 71 6c 69 74 65 33 2e 68 22 20 68 65 61   "sqlite3.h" hea
0320: 64 65 72 20 66 69 6c 65 20 61 74 20 68 61 6e 64  der file at hand
0330: 2c 20 79 6f 75 20 77 69 6c 6c 20 66 69 6e 64 20  , you will find 
0340: 61 20 63 6f 70 79 20 65 6d 62 65 64 64 65 64 20  a copy embedded 
0350: 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 74 65  within.** the te
0360: 78 74 20 6f 66 20 74 68 69 73 20 66 69 6c 65 2e  xt of this file.
0370: 20 20 53 65 61 72 63 68 20 66 6f 72 20 22 42 65    Search for "Be
0380: 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 33  gin file sqlite3
0390: 2e 68 22 20 74 6f 20 66 69 6e 64 20 74 68 65 20  .h" to find the 
03a0: 73 74 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20  start.** of the 
03b0: 65 6d 62 65 64 64 65 64 20 73 71 6c 69 74 65 33  embedded sqlite3
03c0: 2e 68 20 68 65 61 64 65 72 20 66 69 6c 65 2e 29  .h header file.)
03d0: 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 64 65   Additional code
03e0: 20 66 69 6c 65 73 20 6d 61 79 20 62 65 20 6e 65   files may be ne
03f0: 65 64 65 64 0a 2a 2a 20 69 66 20 79 6f 75 20 77  eded.** if you w
0400: 61 6e 74 20 61 20 77 72 61 70 70 65 72 20 74 6f  ant a wrapper to
0410: 20 69 6e 74 65 72 66 61 63 65 20 53 51 4c 69 74   interface SQLit
0420: 65 20 77 69 74 68 20 79 6f 75 72 20 63 68 6f 69  e with your choi
0430: 63 65 20 6f 66 20 70 72 6f 67 72 61 6d 6d 69 6e  ce of programmin
0440: 67 0a 2a 2a 20 6c 61 6e 67 75 61 67 65 2e 20 54  g.** language. T
0450: 68 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20  he code for the 
0460: 22 73 71 6c 69 74 65 33 22 20 63 6f 6d 6d 61 6e  "sqlite3" comman
0470: 64 2d 6c 69 6e 65 20 73 68 65 6c 6c 20 69 73 20  d-line shell is 
0480: 61 6c 73 6f 20 69 6e 20 61 0a 2a 2a 20 73 65 70  also in a.** sep
0490: 61 72 61 74 65 20 66 69 6c 65 2e 20 54 68 69 73  arate file. This
04a0: 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6f   file contains o
04b0: 6e 6c 79 20 63 6f 64 65 20 66 6f 72 20 74 68 65  nly code for the
04c0: 20 63 6f 72 65 20 53 51 4c 69 74 65 20 6c 69 62   core SQLite lib
04d0: 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rary..**.** This
04e0: 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20 77 61   amalgamation wa
04f0: 73 20 67 65 6e 65 72 61 74 65 64 20 6f 6e 20 32  s generated on 2
0500: 30 30 39 2d 30 39 2d 30 39 20 31 36 3a 31 31 3a  009-09-09 16:11:
0510: 30 36 20 55 54 43 2e 0a 2a 2f 0a 23 64 65 66 69  06 UTC..*/.#defi
0520: 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 31  ne SQLITE_CORE 1
0530: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
0540: 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 31 0a 23  AMALGAMATION 1.#
0550: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 50 52  ifndef SQLITE_PR
0560: 49 56 41 54 45 0a 23 20 64 65 66 69 6e 65 20 53  IVATE.# define S
0570: 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 74  QLITE_PRIVATE st
0580: 61 74 69 63 0a 23 65 6e 64 69 66 0a 23 69 66 6e  atic.#endif.#ifn
0590: 64 65 66 20 53 51 4c 49 54 45 5f 41 50 49 0a 23  def SQLITE_API.#
05a0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41   define SQLITE_A
05b0: 50 49 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a 2a 2a  PI.#endif./*****
05c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
05d0: 66 69 6c 65 20 73 71 6c 69 74 65 49 6e 74 2e 68  file sqliteInt.h
05e0: 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
05f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0600: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
0610: 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
0620: 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
0630: 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
0640: 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
0650: 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
0660: 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
0670: 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
0680: 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
0690: 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
06a0: 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
06b0: 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
06c0: 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
06d0: 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
06e0: 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
06f0: 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
0700: 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
0710: 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
0720: 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
0730: 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
0740: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0750: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0760: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0770: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0780: 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 69 6e 74  .** Internal int
0790: 65 72 66 61 63 65 20 64 65 66 69 6e 69 74 69 6f  erface definitio
07a0: 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 2e 0a 2a  ns for SQLite..*
07b0: 2a 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51  *.*/.#ifndef _SQ
07c0: 4c 49 54 45 49 4e 54 5f 48 5f 0a 23 64 65 66 69  LITEINT_H_.#defi
07d0: 6e 65 20 5f 53 51 4c 49 54 45 49 4e 54 5f 48 5f  ne _SQLITEINT_H_
07e0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64  ../*.** These #d
07f0: 65 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e  efines should en
0800: 61 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73  able >2GB file s
0810: 75 70 70 6f 72 74 20 6f 6e 20 50 4f 53 49 58 20  upport on POSIX 
0820: 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c  if the.** underl
0830: 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73  ying operating s
0840: 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69  ystem supports i
0850: 74 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61  t.  If the OS la
0860: 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c  cks.** large fil
0870: 65 20 73 75 70 70 6f 72 74 2c 20 6f 72 20 69 66  e support, or if
0880: 20 74 68 65 20 4f 53 20 69 73 20 77 69 6e 64 6f   the OS is windo
0890: 77 73 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64  ws, these should
08a0: 20 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a   be no-ops..**.*
08b0: 2a 20 54 69 63 6b 65 74 20 23 32 37 33 39 3a 20  * Ticket #2739: 
08c0: 20 54 68 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f   The _LARGEFILE_
08d0: 53 4f 55 52 43 45 20 6d 61 63 72 6f 20 6d 75 73  SOURCE macro mus
08e0: 74 20 61 70 70 65 61 72 20 62 65 66 6f 72 65 20  t appear before 
08f0: 61 6e 79 0a 2a 2a 20 73 79 73 74 65 6d 20 23 69  any.** system #i
0900: 6e 63 6c 75 64 65 73 2e 20 20 48 65 6e 63 65 2c  ncludes.  Hence,
0910: 20 74 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63   this block of c
0920: 6f 64 65 20 6d 75 73 74 20 62 65 20 74 68 65 20  ode must be the 
0930: 76 65 72 79 20 66 69 72 73 74 0a 2a 2a 20 63 6f  very first.** co
0940: 64 65 20 69 6e 20 61 6c 6c 20 73 6f 75 72 63 65  de in all source
0950: 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61   files..**.** La
0960: 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74  rge file support
0970: 20 63 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64   can be disabled
0980: 20 75 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c   using the -DSQL
0990: 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20  ITE_DISABLE_LFS 
09a0: 73 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65  switch.** on the
09b0: 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e   compiler comman
09c0: 64 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73  d line.  This is
09d0: 20 6e 65 63 65 73 73 61 72 79 20 69 66 20 79 6f   necessary if yo
09e0: 75 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a  u are compiling.
09f0: 2a 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d  ** on a recent m
0a00: 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 20  achine (ex: Red 
0a10: 48 61 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75  Hat 7.2) but you
0a20: 20 77 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20   want your code 
0a30: 74 6f 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e  to work.** on an
0a40: 20 6f 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28   older machine (
0a50: 65 78 3a 20 52 65 64 20 48 61 74 20 36 2e 30 29  ex: Red Hat 6.0)
0a60: 2e 20 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c  .  If you compil
0a70: 65 20 6f 6e 20 52 65 64 20 48 61 74 20 37 2e 32  e on Red Hat 7.2
0a80: 0a 2a 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73  .** without this
0a90: 20 6f 70 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20   option, LFS is 
0aa0: 65 6e 61 62 6c 65 2e 20 20 42 75 74 20 4c 46 53  enable.  But LFS
0ab0: 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20   does not exist 
0ac0: 69 6e 20 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a  in the kernel.**
0ad0: 20 69 6e 20 52 65 64 20 48 61 74 20 36 2e 30 2c   in Red Hat 6.0,
0ae0: 20 73 6f 20 74 68 65 20 63 6f 64 65 20 77 6f 6e   so the code won
0af0: 27 74 20 77 6f 72 6b 2e 20 20 48 65 6e 63 65 2c  't work.  Hence,
0b00: 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 62 69 6e   for maximum bin
0b10: 61 72 79 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69  ary.** portabili
0b20: 74 79 20 79 6f 75 20 73 68 6f 75 6c 64 20 6f 6d  ty you should om
0b30: 69 74 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 53 69  it LFS..**.** Si
0b40: 6d 69 6c 61 72 20 69 73 20 74 72 75 65 20 66 6f  milar is true fo
0b50: 72 20 4d 61 63 20 4f 53 20 58 2e 20 20 4c 46 53  r Mac OS X.  LFS
0b60: 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74   is only support
0b70: 65 64 20 6f 6e 20 4d 61 63 20 4f 53 20 58 20 39  ed on Mac OS X 9
0b80: 20 61 6e 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 23   and later..*/.#
0b90: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49  ifndef SQLITE_DI
0ba0: 53 41 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 69  SABLE_LFS.# defi
0bb0: 6e 65 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 20  ne _LARGE_FILE  
0bc0: 20 20 20 20 20 31 0a 23 20 69 66 6e 64 65 66 20       1.# ifndef 
0bd0: 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54  _FILE_OFFSET_BIT
0be0: 53 0a 23 20 20 20 64 65 66 69 6e 65 20 5f 46 49  S.#   define _FI
0bf0: 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 36  LE_OFFSET_BITS 6
0c00: 34 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69  4.# endif.# defi
0c10: 6e 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f  ne _LARGEFILE_SO
0c20: 55 52 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f  URCE 1.#endif../
0c30: 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 74 68 65  *.** Include the
0c40: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 68   configuration h
0c50: 65 61 64 65 72 20 6f 75 74 70 75 74 20 62 79 20  eader output by 
0c60: 27 63 6f 6e 66 69 67 75 72 65 27 20 69 66 20 77  'configure' if w
0c70: 65 27 72 65 20 75 73 69 6e 67 20 74 68 65 0a 2a  e're using the.*
0c80: 2a 20 61 75 74 6f 63 6f 6e 66 2d 62 61 73 65 64  * autoconf-based
0c90: 20 62 75 69 6c 64 0a 2a 2f 0a 23 69 66 64 65 66   build.*/.#ifdef
0ca0: 20 5f 48 41 56 45 5f 53 51 4c 49 54 45 5f 43 4f   _HAVE_SQLITE_CO
0cb0: 4e 46 49 47 5f 48 0a 23 69 6e 63 6c 75 64 65 20  NFIG_H.#include 
0cc0: 22 63 6f 6e 66 69 67 2e 68 22 0a 23 65 6e 64 69  "config.h".#endi
0cd0: 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
0ce0: 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74  ** Include sqlit
0cf0: 65 4c 69 6d 69 74 2e 68 20 69 6e 20 74 68 65 20  eLimit.h in the 
0d00: 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65  middle of sqlite
0d10: 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
0d20: 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
0d30: 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71  ** Begin file sq
0d40: 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a  liteLimit.h ****
0d50: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0d60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0d70: 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61  */./*.** 2007 Ma
0d80: 79 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75  y 7.**.** The au
0d90: 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
0da0: 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
0db0: 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
0dc0: 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
0dd0: 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
0de0: 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
0df0: 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
0e00: 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
0e10: 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
0e20: 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
0e30: 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
0e40: 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
0e50: 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
0e60: 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
0e70: 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
0e80: 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
0e90: 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
0ea0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0eb0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0ec0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0ed0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0ee0: 2a 2a 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 66  **.** .** This f
0ef0: 69 6c 65 20 64 65 66 69 6e 65 73 20 76 61 72 69  ile defines vari
0f00: 6f 75 73 20 6c 69 6d 69 74 73 20 6f 66 20 77 68  ous limits of wh
0f10: 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20 70 72  at SQLite can pr
0f20: 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23  ocess..**.** @(#
0f30: 29 20 24 49 64 3a 20 73 71 6c 69 74 65 4c 69 6d  ) $Id: sqliteLim
0f40: 69 74 2e 68 2c 76 20 31 2e 31 30 20 32 30 30 39  it.h,v 1.10 2009
0f50: 2f 30 31 2f 31 30 20 31 36 3a 31 35 3a 30 39 20  /01/10 16:15:09 
0f60: 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
0f70: 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  $.*/../*.** The 
0f80: 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f  maximum length o
0f90: 66 20 61 20 54 45 58 54 20 6f 72 20 42 4c 4f 42  f a TEXT or BLOB
0fa0: 20 69 6e 20 62 79 74 65 73 2e 20 20 20 54 68 69   in bytes.   Thi
0fb0: 73 20 61 6c 73 6f 0a 2a 2a 20 6c 69 6d 69 74 73  s also.** limits
0fc0: 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 72   the size of a r
0fd0: 6f 77 20 69 6e 20 61 20 74 61 62 6c 65 20 6f 72  ow in a table or
0fe0: 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68   index..**.** Th
0ff0: 65 20 68 61 72 64 20 6c 69 6d 69 74 20 69 73 20  e hard limit is 
1000: 74 68 65 20 61 62 69 6c 69 74 79 20 6f 66 20 61  the ability of a
1010: 20 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69   32-bit signed i
1020: 6e 74 65 67 65 72 0a 2a 2a 20 74 6f 20 63 6f 75  nteger.** to cou
1030: 6e 74 20 74 68 65 20 73 69 7a 65 3a 20 32 5e 33  nt the size: 2^3
1040: 31 2d 31 20 6f 72 20 32 31 34 37 34 38 33 36 34  1-1 or 214748364
1050: 37 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  7..*/.#ifndef SQ
1060: 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a  LITE_MAX_LENGTH.
1070: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
1080: 4d 41 58 5f 4c 45 4e 47 54 48 20 31 30 30 30 30  MAX_LENGTH 10000
1090: 30 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a  00000.#endif../*
10a0: 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
10b0: 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  maximum number o
10c0: 66 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c  f.**.**    * Col
10d0: 75 6d 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 0a  umns in a table.
10e0: 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73 20  **    * Columns 
10f0: 69 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 20  in an index.**  
1100: 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61    * Columns in a
1110: 20 76 69 65 77 0a 2a 2a 20 20 20 20 2a 20 54 65   view.**    * Te
1120: 72 6d 73 20 69 6e 20 74 68 65 20 53 45 54 20 63  rms in the SET c
1130: 6c 61 75 73 65 20 6f 66 20 61 6e 20 55 50 44 41  lause of an UPDA
1140: 54 45 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20  TE statement.** 
1150: 20 20 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68     * Terms in th
1160: 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20  e result set of 
1170: 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
1180: 6e 74 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73  nt.**    * Terms
1190: 20 69 6e 20 74 68 65 20 47 52 4f 55 50 20 42 59   in the GROUP BY
11a0: 20 6f 72 20 4f 52 44 45 52 20 42 59 20 63 6c 61   or ORDER BY cla
11b0: 75 73 65 73 20 6f 66 20 61 20 53 45 4c 45 43 54  uses of a SELECT
11c0: 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 20   statement..**  
11d0: 20 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 65    * Terms in the
11e0: 20 56 41 4c 55 45 53 20 63 6c 61 75 73 65 20 6f   VALUES clause o
11f0: 66 20 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74  f an INSERT stat
1200: 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ement.**.** The 
1210: 68 61 72 64 20 75 70 70 65 72 20 6c 69 6d 69 74  hard upper limit
1220: 20 68 65 72 65 20 69 73 20 33 32 36 37 36 2e 20   here is 32676. 
1230: 20 4d 6f 73 74 20 64 61 74 61 62 61 73 65 20 70   Most database p
1240: 65 6f 70 6c 65 20 77 69 6c 6c 0a 2a 2a 20 74 65  eople will.** te
1250: 6c 6c 20 79 6f 75 20 74 68 61 74 20 69 6e 20 61  ll you that in a
1260: 20 77 65 6c 6c 2d 6e 6f 72 6d 61 6c 69 7a 65 64   well-normalized
1270: 20 64 61 74 61 62 61 73 65 2c 20 79 6f 75 20 75   database, you u
1280: 73 75 61 6c 6c 79 20 73 68 6f 75 6c 64 0a 2a 2a  sually should.**
1290: 20 6e 6f 74 20 68 61 76 65 20 6d 6f 72 65 20 74   not have more t
12a0: 68 61 6e 20 61 20 64 6f 7a 65 6e 20 6f 72 20 73  han a dozen or s
12b0: 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 79  o columns in any
12c0: 20 74 61 62 6c 65 2e 20 20 41 6e 64 20 69 66 0a   table.  And if.
12d0: 2a 2a 20 74 68 61 74 20 69 73 20 74 68 65 20 63  ** that is the c
12e0: 61 73 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f  ase, there is no
12f0: 20 70 6f 69 6e 74 20 69 6e 20 68 61 76 69 6e 67   point in having
1300: 20 6d 6f 72 65 20 74 68 61 6e 20 61 20 66 65 77   more than a few
1310: 0a 2a 2a 20 64 6f 7a 65 6e 20 76 61 6c 75 65 73  .** dozen values
1320: 20 69 6e 20 61 6e 79 20 6f 66 20 74 68 65 20 6f   in any of the o
1330: 74 68 65 72 20 73 69 74 75 61 74 69 6f 6e 73 20  ther situations 
1340: 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2e  described above.
1350: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
1360: 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 23 20  TE_MAX_COLUMN.# 
1370: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
1380: 58 5f 43 4f 4c 55 4d 4e 20 32 30 30 30 0a 23 65  X_COLUMN 2000.#e
1390: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
13a0: 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f  maximum length o
13b0: 66 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73  f a single SQL s
13c0: 74 61 74 65 6d 65 6e 74 20 69 6e 20 62 79 74 65  tatement in byte
13d0: 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 75 73 65 64  s..**.** It used
13e0: 20 74 6f 20 62 65 20 74 68 65 20 63 61 73 65 20   to be the case 
13f0: 74 68 61 74 20 73 65 74 74 69 6e 67 20 74 68 69  that setting thi
1400: 73 20 76 61 6c 75 65 20 74 6f 20 7a 65 72 6f 20  s value to zero 
1410: 77 6f 75 6c 64 0a 2a 2a 20 74 75 72 6e 20 74 68  would.** turn th
1420: 65 20 6c 69 6d 69 74 20 6f 66 66 2e 20 20 54 68  e limit off.  Th
1430: 61 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20  at is no longer 
1440: 74 72 75 65 2e 20 20 49 74 20 69 73 20 6e 6f 74  true.  It is not
1450: 20 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20   possible.** to 
1460: 74 75 72 6e 20 74 68 69 73 20 6c 69 6d 69 74 20  turn this limit 
1470: 6f 66 66 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  off..*/.#ifndef 
1480: 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c  SQLITE_MAX_SQL_L
1490: 45 4e 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53  ENGTH.# define S
14a0: 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45  QLITE_MAX_SQL_LE
14b0: 4e 47 54 48 20 31 30 30 30 30 30 30 30 30 30 0a  NGTH 1000000000.
14c0: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
14d0: 65 20 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20  e maximum depth 
14e0: 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  of an expression
14f0: 20 74 72 65 65 2e 20 54 68 69 73 20 69 73 20 6c   tree. This is l
1500: 69 6d 69 74 65 64 20 74 6f 20 0a 2a 2a 20 73 6f  imited to .** so
1510: 6d 65 20 65 78 74 65 6e 74 20 62 79 20 53 51 4c  me extent by SQL
1520: 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47  ITE_MAX_SQL_LENG
1530: 54 48 2e 20 42 75 74 20 73 6f 6d 65 74 69 6d 65  TH. But sometime
1540: 20 79 6f 75 20 6d 69 67 68 74 20 0a 2a 2a 20 77   you might .** w
1550: 61 6e 74 20 74 6f 20 70 6c 61 63 65 20 6d 6f 72  ant to place mor
1560: 65 20 73 65 76 65 72 65 20 6c 69 6d 69 74 73 20  e severe limits 
1570: 6f 6e 20 74 68 65 20 63 6f 6d 70 6c 65 78 69 74  on the complexit
1580: 79 20 6f 66 20 61 6e 20 0a 2a 2a 20 65 78 70 72  y of an .** expr
1590: 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20  ession..**.** A 
15a0: 76 61 6c 75 65 20 6f 66 20 30 20 75 73 65 64 20  value of 0 used 
15b0: 74 6f 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65  to mean that the
15c0: 20 6c 69 6d 69 74 20 77 61 73 20 6e 6f 74 20 65   limit was not e
15d0: 6e 66 6f 72 63 65 64 2e 0a 2a 2a 20 42 75 74 20  nforced..** But 
15e0: 74 68 61 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65  that is no longe
15f0: 72 20 74 72 75 65 2e 20 20 54 68 65 20 6c 69 6d  r true.  The lim
1600: 69 74 20 69 73 20 6e 6f 77 20 73 74 72 69 63 74  it is now strict
1610: 6c 79 20 65 6e 66 6f 72 63 65 64 0a 2a 2a 20 61  ly enforced.** a
1620: 74 20 61 6c 6c 20 74 69 6d 65 73 2e 0a 2a 2f 0a  t all times..*/.
1630: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  #ifndef SQLITE_M
1640: 41 58 5f 45 58 50 52 5f 44 45 50 54 48 0a 23 20  AX_EXPR_DEPTH.# 
1650: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
1660: 58 5f 45 58 50 52 5f 44 45 50 54 48 20 31 30 30  X_EXPR_DEPTH 100
1670: 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
1680: 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62  The maximum numb
1690: 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 61  er of terms in a
16a0: 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54   compound SELECT
16b0: 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 54   statement..** T
16c0: 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f  he code generato
16d0: 72 20 66 6f 72 20 63 6f 6d 70 6f 75 6e 64 20 53  r for compound S
16e0: 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73  ELECT statements
16f0: 20 64 6f 65 73 20 6f 6e 65 0a 2a 2a 20 6c 65 76   does one.** lev
1700: 65 6c 20 6f 66 20 72 65 63 75 72 73 69 6f 6e 20  el of recursion 
1710: 66 6f 72 20 65 61 63 68 20 74 65 72 6d 2e 20 20  for each term.  
1720: 41 20 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77  A stack overflow
1730: 20 63 61 6e 20 72 65 73 75 6c 74 0a 2a 2a 20 69   can result.** i
1740: 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  f the number of 
1750: 74 65 72 6d 73 20 69 73 20 74 6f 6f 20 6c 61 72  terms is too lar
1760: 67 65 2e 20 20 49 6e 20 70 72 61 63 74 69 63 65  ge.  In practice
1770: 2c 20 6d 6f 73 74 20 53 51 4c 0a 2a 2a 20 6e 65  , most SQL.** ne
1780: 76 65 72 20 68 61 73 20 6d 6f 72 65 20 74 68 61  ver has more tha
1790: 6e 20 33 20 6f 72 20 34 20 74 65 72 6d 73 2e 20  n 3 or 4 terms. 
17a0: 20 55 73 65 20 61 20 76 61 6c 75 65 20 6f 66 20   Use a value of 
17b0: 30 20 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20  0 to disable.** 
17c0: 61 6e 79 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65  any limit on the
17d0: 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73   number of terms
17e0: 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 74 20 53   in a compount S
17f0: 45 4c 45 43 54 2e 0a 2a 2f 0a 23 69 66 6e 64 65  ELECT..*/.#ifnde
1800: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d  f SQLITE_MAX_COM
1810: 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 23 20 64  POUND_SELECT.# d
1820: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58  efine SQLITE_MAX
1830: 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
1840: 20 35 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a   500.#endif../*.
1850: 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e  ** The maximum n
1860: 75 6d 62 65 72 20 6f 66 20 6f 70 63 6f 64 65 73  umber of opcodes
1870: 20 69 6e 20 61 20 56 44 42 45 20 70 72 6f 67 72   in a VDBE progr
1880: 61 6d 2e 0a 2a 2a 20 4e 6f 74 20 63 75 72 72 65  am..** Not curre
1890: 6e 74 6c 79 20 65 6e 66 6f 72 63 65 64 2e 0a 2a  ntly enforced..*
18a0: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
18b0: 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 0a 23 20 64  _MAX_VDBE_OP.# d
18c0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58  efine SQLITE_MAX
18d0: 5f 56 44 42 45 5f 4f 50 20 32 35 30 30 30 0a 23  _VDBE_OP 25000.#
18e0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
18f0: 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
1900: 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  of arguments to 
1910: 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e  an SQL function.
1920: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
1930: 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f  TE_MAX_FUNCTION_
1940: 41 52 47 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  ARG.# define SQL
1950: 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e  ITE_MAX_FUNCTION
1960: 5f 41 52 47 20 31 32 37 0a 23 65 6e 64 69 66 0a  _ARG 127.#endif.
1970: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d  ./*.** The maxim
1980: 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 2d  um number of in-
1990: 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 74 6f 20  memory pages to 
19a0: 75 73 65 20 66 6f 72 20 74 68 65 20 6d 61 69 6e  use for the main
19b0: 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62   database.** tab
19c0: 6c 65 20 61 6e 64 20 66 6f 72 20 74 65 6d 70 6f  le and for tempo
19d0: 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20 54 68  rary tables.  Th
19e0: 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  e SQLITE_DEFAULT
19f0: 5f 43 41 43 48 45 5f 53 49 5a 45 0a 2a 2f 0a 23  _CACHE_SIZE.*/.#
1a00: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45  ifndef SQLITE_DE
1a10: 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45  FAULT_CACHE_SIZE
1a20: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
1a30: 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53  _DEFAULT_CACHE_S
1a40: 49 5a 45 20 20 32 30 30 30 0a 23 65 6e 64 69 66  IZE  2000.#endif
1a50: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1a60: 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43  DEFAULT_TEMP_CAC
1a70: 48 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65  HE_SIZE.# define
1a80: 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
1a90: 54 45 4d 50 5f 43 41 43 48 45 5f 53 49 5a 45 20  TEMP_CACHE_SIZE 
1aa0: 20 35 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a   500.#endif../*.
1ab0: 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e  ** The maximum n
1ac0: 75 6d 62 65 72 20 6f 66 20 61 74 74 61 63 68 65  umber of attache
1ad0: 64 20 64 61 74 61 62 61 73 65 73 2e 20 20 54 68  d databases.  Th
1ae0: 69 73 20 6d 75 73 74 20 62 65 20 62 65 74 77 65  is must be betwe
1af0: 65 6e 20 30 0a 2a 2a 20 61 6e 64 20 33 30 2e 20  en 0.** and 30. 
1b00: 20 54 68 65 20 75 70 70 65 72 20 62 6f 75 6e 64   The upper bound
1b10: 20 6f 6e 20 33 30 20 69 73 20 62 65 63 61 75 73   on 30 is becaus
1b20: 65 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67  e a 32-bit integ
1b30: 65 72 20 62 69 74 6d 61 70 0a 2a 2a 20 69 73 20  er bitmap.** is 
1b40: 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20  used internally 
1b50: 74 6f 20 74 72 61 63 6b 20 61 74 74 61 63 68 65  to track attache
1b60: 64 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2f 0a  d databases..*/.
1b70: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  #ifndef SQLITE_M
1b80: 41 58 5f 41 54 54 41 43 48 45 44 0a 23 20 64 65  AX_ATTACHED.# de
1b90: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  fine SQLITE_MAX_
1ba0: 41 54 54 41 43 48 45 44 20 31 30 0a 23 65 6e 64  ATTACHED 10.#end
1bb0: 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d  if.../*.** The m
1bc0: 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20  aximum value of 
1bd0: 61 20 3f 6e 6e 6e 20 77 69 6c 64 63 61 72 64 20  a ?nnn wildcard 
1be0: 74 68 61 74 20 74 68 65 20 70 61 72 73 65 72 20  that the parser 
1bf0: 77 69 6c 6c 20 61 63 63 65 70 74 2e 0a 2a 2f 0a  will accept..*/.
1c00: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  #ifndef SQLITE_M
1c10: 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42  AX_VARIABLE_NUMB
1c20: 45 52 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  ER.# define SQLI
1c30: 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f  TE_MAX_VARIABLE_
1c40: 4e 55 4d 42 45 52 20 39 39 39 0a 23 65 6e 64 69  NUMBER 999.#endi
1c50: 66 0a 0a 2f 2a 20 4d 61 78 69 6d 75 6d 20 70 61  f../* Maximum pa
1c60: 67 65 20 73 69 7a 65 2e 20 20 54 68 65 20 75 70  ge size.  The up
1c70: 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 69  per bound on thi
1c80: 73 20 76 61 6c 75 65 20 69 73 20 33 32 37 36 38  s value is 32768
1c90: 2e 20 20 54 68 69 73 20 61 20 6c 69 6d 69 74 0a  .  This a limit.
1ca0: 2a 2a 20 69 6d 70 6f 73 65 64 20 62 79 20 74 68  ** imposed by th
1cb0: 65 20 6e 65 63 65 73 73 69 74 79 20 6f 66 20 73  e necessity of s
1cc0: 74 6f 72 69 6e 67 20 74 68 65 20 76 61 6c 75 65  toring the value
1cd0: 20 69 6e 20 61 20 32 2d 62 79 74 65 20 75 6e 73   in a 2-byte uns
1ce0: 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a  igned integer.**
1cf0: 20 61 6e 64 20 74 68 65 20 66 61 63 74 20 74 68   and the fact th
1d00: 61 74 20 74 68 65 20 70 61 67 65 20 73 69 7a 65  at the page size
1d10: 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72   must be a power
1d20: 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   of 2..**.** If 
1d30: 74 68 69 73 20 6c 69 6d 69 74 20 69 73 20 63 68  this limit is ch
1d40: 61 6e 67 65 64 2c 20 74 68 65 6e 20 74 68 65 20  anged, then the 
1d50: 63 6f 6d 70 69 6c 65 64 20 6c 69 62 72 61 72 79  compiled library
1d60: 20 69 73 20 74 65 63 68 6e 69 63 61 6c 6c 79 0a   is technically.
1d70: 2a 2a 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20  ** incompatible 
1d80: 77 69 74 68 20 61 6e 20 53 51 4c 69 74 65 20 6c  with an SQLite l
1d90: 69 62 72 61 72 79 20 63 6f 6d 70 69 6c 65 64 20  ibrary compiled 
1da0: 77 69 74 68 20 61 20 64 69 66 66 65 72 65 6e 74  with a different
1db0: 20 6c 69 6d 69 74 2e 20 49 66 0a 2a 2a 20 61 20   limit. If.** a 
1dc0: 70 72 6f 63 65 73 73 20 6f 70 65 72 61 74 69 6e  process operatin
1dd0: 67 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20  g on a database 
1de0: 77 69 74 68 20 61 20 70 61 67 65 2d 73 69 7a 65  with a page-size
1df0: 20 6f 66 20 36 35 35 33 36 20 62 79 74 65 73 20   of 65536 bytes 
1e00: 0a 2a 2a 20 63 72 61 73 68 65 73 2c 20 74 68 65  .** crashes, the
1e10: 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  n an instance of
1e20: 20 53 51 4c 69 74 65 20 63 6f 6d 70 69 6c 65 64   SQLite compiled
1e30: 20 77 69 74 68 20 74 68 65 20 64 65 66 61 75 6c   with the defaul
1e40: 74 20 70 61 67 65 2d 73 69 7a 65 20 0a 2a 2a 20  t page-size .** 
1e50: 6c 69 6d 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62  limit will not b
1e60: 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61  e able to rollba
1e70: 63 6b 20 74 68 65 20 61 62 6f 72 74 65 64 20 74  ck the aborted t
1e80: 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73  ransaction. This
1e90: 20 63 6f 75 6c 64 0a 2a 2a 20 6c 65 61 64 20 74   could.** lead t
1ea0: 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75  o database corru
1eb0: 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65  ption..*/.#ifnde
1ec0: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47  f SQLITE_MAX_PAG
1ed0: 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20  E_SIZE.# define 
1ee0: 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f  SQLITE_MAX_PAGE_
1ef0: 53 49 5a 45 20 33 32 37 36 38 0a 23 65 6e 64 69  SIZE 32768.#endi
1f00: 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 65  f.../*.** The de
1f10: 66 61 75 6c 74 20 73 69 7a 65 20 6f 66 20 61 20  fault size of a 
1f20: 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 0a 2a  database page..*
1f30: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1f40: 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49  _DEFAULT_PAGE_SI
1f50: 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  ZE.# define SQLI
1f60: 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  TE_DEFAULT_PAGE_
1f70: 53 49 5a 45 20 31 30 32 34 0a 23 65 6e 64 69 66  SIZE 1024.#endif
1f80: 0a 23 69 66 20 53 51 4c 49 54 45 5f 44 45 46 41  .#if SQLITE_DEFA
1f90: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3e 53 51  ULT_PAGE_SIZE>SQ
1fa0: 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49  LITE_MAX_PAGE_SI
1fb0: 5a 45 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54  ZE.# undef SQLIT
1fc0: 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53  E_DEFAULT_PAGE_S
1fd0: 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  IZE.# define SQL
1fe0: 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45  ITE_DEFAULT_PAGE
1ff0: 5f 53 49 5a 45 20 53 51 4c 49 54 45 5f 4d 41 58  _SIZE SQLITE_MAX
2000: 5f 50 41 47 45 5f 53 49 5a 45 0a 23 65 6e 64 69  _PAGE_SIZE.#endi
2010: 66 0a 0a 2f 2a 0a 2a 2a 20 4f 72 64 69 6e 61 72  f../*.** Ordinar
2020: 69 6c 79 2c 20 69 66 20 6e 6f 20 76 61 6c 75 65  ily, if no value
2030: 20 69 73 20 65 78 70 6c 69 63 69 74 6c 79 20 70   is explicitly p
2040: 72 6f 76 69 64 65 64 2c 20 53 51 4c 69 74 65 20  rovided, SQLite 
2050: 63 72 65 61 74 65 73 20 64 61 74 61 62 61 73 65  creates database
2060: 73 0a 2a 2a 20 77 69 74 68 20 70 61 67 65 20 73  s.** with page s
2070: 69 7a 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55  ize SQLITE_DEFAU
2080: 4c 54 5f 50 41 47 45 5f 53 49 5a 45 2e 20 48 6f  LT_PAGE_SIZE. Ho
2090: 77 65 76 65 72 2c 20 62 61 73 65 64 20 6f 6e 20  wever, based on 
20a0: 63 65 72 74 61 69 6e 0a 2a 2a 20 64 65 76 69 63  certain.** devic
20b0: 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63  e characteristic
20c0: 73 20 28 73 65 63 74 6f 72 2d 73 69 7a 65 20 61  s (sector-size a
20d0: 6e 64 20 61 74 6f 6d 69 63 20 77 72 69 74 65 28  nd atomic write(
20e0: 29 20 73 75 70 70 6f 72 74 29 2c 0a 2a 2a 20 53  ) support),.** S
20f0: 51 4c 69 74 65 20 6d 61 79 20 63 68 6f 6f 73 65  QLite may choose
2100: 20 61 20 6c 61 72 67 65 72 20 76 61 6c 75 65 2e   a larger value.
2110: 20 54 68 69 73 20 63 6f 6e 73 74 61 6e 74 20 69   This constant i
2120: 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61  s the maximum va
2130: 6c 75 65 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69  lue.** SQLite wi
2140: 6c 6c 20 63 68 6f 6f 73 65 20 6f 6e 20 69 74 73  ll choose on its
2150: 20 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66   own..*/.#ifndef
2160: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41   SQLITE_MAX_DEFA
2170: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20  ULT_PAGE_SIZE.# 
2180: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
2190: 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53  X_DEFAULT_PAGE_S
21a0: 49 5a 45 20 38 31 39 32 0a 23 65 6e 64 69 66 0a  IZE 8192.#endif.
21b0: 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44  #if SQLITE_MAX_D
21c0: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
21d0: 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45  >SQLITE_MAX_PAGE
21e0: 5f 53 49 5a 45 0a 23 20 75 6e 64 65 66 20 53 51  _SIZE.# undef SQ
21f0: 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54  LITE_MAX_DEFAULT
2200: 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 66  _PAGE_SIZE.# def
2210: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44  ine SQLITE_MAX_D
2220: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
2230: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45   SQLITE_MAX_PAGE
2240: 5f 53 49 5a 45 0a 23 65 6e 64 69 66 0a 0a 0a 2f  _SIZE.#endif.../
2250: 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d  *.** Maximum num
2260: 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
2270: 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  one database fil
2280: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73  e..**.** This is
2290: 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 74 68 65   really just the
22a0: 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66   default value f
22b0: 6f 72 20 74 68 65 20 6d 61 78 5f 70 61 67 65 5f  or the max_page_
22c0: 63 6f 75 6e 74 20 70 72 61 67 6d 61 2e 0a 2a 2a  count pragma..**
22d0: 20 54 68 69 73 20 76 61 6c 75 65 20 63 61 6e 20   This value can 
22e0: 62 65 20 6c 6f 77 65 72 65 64 20 28 6f 72 20 72  be lowered (or r
22f0: 61 69 73 65 64 29 20 61 74 20 72 75 6e 2d 74 69  aised) at run-ti
2300: 6d 65 20 75 73 69 6e 67 20 74 68 61 74 20 74 68  me using that th
2310: 65 0a 2a 2a 20 6d 61 78 5f 70 61 67 65 5f 63 6f  e.** max_page_co
2320: 75 6e 74 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69  unt macro..*/.#i
2330: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
2340: 5f 50 41 47 45 5f 43 4f 55 4e 54 0a 23 20 64 65  _PAGE_COUNT.# de
2350: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  fine SQLITE_MAX_
2360: 50 41 47 45 5f 43 4f 55 4e 54 20 31 30 37 33 37  PAGE_COUNT 10737
2370: 34 31 38 32 33 0a 23 65 6e 64 69 66 0a 0a 2f 2a  41823.#endif../*
2380: 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67  .** Maximum leng
2390: 74 68 20 28 69 6e 20 62 79 74 65 73 29 20 6f 66  th (in bytes) of
23a0: 20 74 68 65 20 70 61 74 74 65 72 6e 20 69 6e 20   the pattern in 
23b0: 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 0a 2a  a LIKE or GLOB.*
23c0: 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 23  * operator..*/.#
23d0: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41  ifndef SQLITE_MA
23e0: 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c  X_LIKE_PATTERN_L
23f0: 45 4e 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53  ENGTH.# define S
2400: 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50  QLITE_MAX_LIKE_P
2410: 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20 35 30  ATTERN_LENGTH 50
2420: 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  000.#endif../*.*
2430: 2a 20 4d 61 78 69 6d 75 6d 20 64 65 70 74 68 20  * Maximum depth 
2440: 6f 66 20 72 65 63 75 72 73 69 6f 6e 20 66 6f 72  of recursion for
2450: 20 74 72 69 67 67 65 72 73 2e 0a 2a 2f 0a 23 69   triggers..*/.#i
2460: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
2470: 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 0a 23  _TRIGGER_DEPTH.#
2480: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d   define SQLITE_M
2490: 41 58 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48  AX_TRIGGER_DEPTH
24a0: 20 31 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a   1000.#endif../*
24b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
24c0: 64 20 6f 66 20 73 71 6c 69 74 65 4c 69 6d 69 74  d of sqliteLimit
24d0: 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
24e0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2500: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f  ************* Co
2510: 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77  ntinuing where w
2520: 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71  e left off in sq
2530: 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  liteInt.h ******
2540: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f  ************/../
2550: 2a 20 44 69 73 61 62 6c 65 20 6e 75 69 73 61 6e  * Disable nuisan
2560: 63 65 20 77 61 72 6e 69 6e 67 73 20 6f 6e 20 42  ce warnings on B
2570: 6f 72 6c 61 6e 64 20 63 6f 6d 70 69 6c 65 72 73  orland compilers
2580: 20 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28   */.#if defined(
2590: 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 23 70  __BORLANDC__).#p
25a0: 72 61 67 6d 61 20 77 61 72 6e 20 2d 72 63 68 20  ragma warn -rch 
25b0: 2f 2a 20 75 6e 72 65 61 63 68 61 62 6c 65 20 63  /* unreachable c
25c0: 6f 64 65 20 2a 2f 0a 23 70 72 61 67 6d 61 20 77  ode */.#pragma w
25d0: 61 72 6e 20 2d 63 63 63 20 2f 2a 20 43 6f 6e 64  arn -ccc /* Cond
25e0: 69 74 69 6f 6e 20 69 73 20 61 6c 77 61 79 73 20  ition is always 
25f0: 74 72 75 65 20 6f 72 20 66 61 6c 73 65 20 2a 2f  true or false */
2600: 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 61  .#pragma warn -a
2610: 75 73 20 2f 2a 20 41 73 73 69 67 6e 65 64 20 76  us /* Assigned v
2620: 61 6c 75 65 20 69 73 20 6e 65 76 65 72 20 75 73  alue is never us
2630: 65 64 20 2a 2f 0a 23 70 72 61 67 6d 61 20 77 61  ed */.#pragma wa
2640: 72 6e 20 2d 63 73 75 20 2f 2a 20 43 6f 6d 70 61  rn -csu /* Compa
2650: 72 69 6e 67 20 73 69 67 6e 65 64 20 61 6e 64 20  ring signed and 
2660: 75 6e 73 69 67 6e 65 64 20 2a 2f 0a 23 70 72 61  unsigned */.#pra
2670: 67 6d 61 20 77 61 72 6e 20 2d 73 70 61 20 2f 2a  gma warn -spa /*
2680: 20 53 75 73 70 69 63 69 6f 75 73 20 70 6f 69 6e   Suspicious poin
2690: 74 65 72 20 61 72 69 74 68 6d 65 74 69 63 20 2a  ter arithmetic *
26a0: 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4e 65 65  /.#endif../* Nee
26b0: 64 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73 20  ded for various 
26c0: 64 65 66 69 6e 69 74 69 6f 6e 73 2e 2e 2e 20 2a  definitions... *
26d0: 2f 0a 23 69 66 6e 64 65 66 20 5f 47 4e 55 5f 53  /.#ifndef _GNU_S
26e0: 4f 55 52 43 45 0a 23 20 64 65 66 69 6e 65 20 5f  OURCE.# define _
26f0: 47 4e 55 5f 53 4f 55 52 43 45 0a 23 65 6e 64 69  GNU_SOURCE.#endi
2700: 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65  f../*.** Include
2710: 20 73 74 61 6e 64 61 72 64 20 68 65 61 64 65 72   standard header
2720: 20 66 69 6c 65 73 20 61 73 20 6e 65 63 65 73 73   files as necess
2730: 61 72 79 0a 2a 2f 0a 23 69 66 64 65 66 20 48 41  ary.*/.#ifdef HA
2740: 56 45 5f 53 54 44 49 4e 54 5f 48 0a 23 69 6e 63  VE_STDINT_H.#inc
2750: 6c 75 64 65 20 3c 73 74 64 69 6e 74 2e 68 3e 0a  lude <stdint.h>.
2760: 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 48 41  #endif.#ifdef HA
2770: 56 45 5f 49 4e 54 54 59 50 45 53 5f 48 0a 23 69  VE_INTTYPES_H.#i
2780: 6e 63 6c 75 64 65 20 3c 69 6e 74 74 79 70 65 73  nclude <inttypes
2790: 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66  .h>.#endif..#def
27a0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  ine SQLITE_INDEX
27b0: 5f 53 41 4d 50 4c 45 53 20 31 30 0a 0a 2f 2a 0a  _SAMPLES 10../*.
27c0: 2a 2a 20 54 68 69 73 20 6d 61 63 72 6f 20 69 73  ** This macro is
27d0: 20 75 73 65 64 20 74 6f 20 22 68 69 64 65 22 20   used to "hide" 
27e0: 73 6f 6d 65 20 75 67 6c 69 6e 65 73 73 20 69 6e  some ugliness in
27f0: 20 63 61 73 74 69 6e 67 20 61 6e 20 69 6e 74 0a   casting an int.
2800: 2a 2a 20 76 61 6c 75 65 20 74 6f 20 61 20 70 74  ** value to a pt
2810: 72 20 76 61 6c 75 65 20 75 6e 64 65 72 20 74 68  r value under th
2820: 65 20 4d 53 56 43 20 36 34 2d 62 69 74 20 63 6f  e MSVC 64-bit co
2830: 6d 70 69 6c 65 72 2e 20 20 20 43 61 73 74 69 6e  mpiler.   Castin
2840: 67 0a 2a 2a 20 6e 6f 6e 20 36 34 2d 62 69 74 20  g.** non 64-bit 
2850: 76 61 6c 75 65 73 20 74 6f 20 70 74 72 20 74 79  values to ptr ty
2860: 70 65 73 20 72 65 73 75 6c 74 73 20 69 6e 20 61  pes results in a
2870: 20 22 68 61 72 64 22 20 65 72 72 6f 72 20 77 69   "hard" error wi
2880: 74 68 20 0a 2a 2a 20 74 68 65 20 4d 53 56 43 20  th .** the MSVC 
2890: 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c 65 72 20  64-bit compiler 
28a0: 77 68 69 63 68 20 74 68 69 73 20 61 74 74 65 6d  which this attem
28b0: 70 74 73 20 74 6f 20 61 76 6f 69 64 2e 20 20 0a  pts to avoid.  .
28c0: 2a 2a 0a 2a 2a 20 41 20 73 69 6d 70 6c 65 20 63  **.** A simple c
28d0: 6f 6d 70 69 6c 65 72 20 70 72 61 67 6d 61 20 6f  ompiler pragma o
28e0: 72 20 63 61 73 74 69 6e 67 20 73 65 71 75 65 6e  r casting sequen
28f0: 63 65 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ce could not be 
2900: 66 6f 75 6e 64 0a 2a 2a 20 74 6f 20 63 6f 72 72  found.** to corr
2910: 65 63 74 20 74 68 69 73 20 69 6e 20 61 6c 6c 20  ect this in all 
2920: 73 69 74 75 61 74 69 6f 6e 73 2c 20 73 6f 20 74  situations, so t
2930: 68 69 73 20 6d 61 63 72 6f 20 77 61 73 20 69 6e  his macro was in
2940: 74 72 6f 64 75 63 65 64 2e 0a 2a 2a 0a 2a 2a 20  troduced..**.** 
2950: 49 74 20 63 6f 75 6c 64 20 62 65 20 61 72 67 75  It could be argu
2960: 65 64 20 74 68 61 74 20 74 68 65 20 69 6e 74 70  ed that the intp
2970: 74 72 5f 74 20 74 79 70 65 20 63 6f 75 6c 64 20  tr_t type could 
2980: 62 65 20 75 73 65 64 20 69 6e 20 74 68 69 73 0a  be used in this.
2990: 2a 2a 20 63 61 73 65 2c 20 62 75 74 20 74 68 61  ** case, but tha
29a0: 74 20 74 79 70 65 20 69 73 20 6e 6f 74 20 61 76  t type is not av
29b0: 61 69 6c 61 62 6c 65 20 6f 6e 20 61 6c 6c 20 63  ailable on all c
29c0: 6f 6d 70 69 6c 65 72 73 2c 20 6f 72 20 0a 2a 2a  ompilers, or .**
29d0: 20 72 65 71 75 69 72 65 73 20 74 68 65 20 23 69   requires the #i
29e0: 6e 63 6c 75 64 65 20 6f 66 20 73 70 65 63 69 66  nclude of specif
29f0: 69 63 20 68 65 61 64 65 72 73 20 77 68 69 63 68  ic headers which
2a00: 20 64 69 66 66 65 72 73 20 62 65 74 77 65 65 6e   differs between
2a10: 0a 2a 2a 20 70 6c 61 74 66 6f 72 6d 73 2e 0a 2a  .** platforms..*
2a20: 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 33 38 36  *.** Ticket #386
2a30: 30 3a 20 20 54 68 65 20 6c 6c 76 6d 2d 67 63 63  0:  The llvm-gcc
2a40: 2d 34 2e 32 20 63 6f 6d 70 69 6c 65 72 20 66 72  -4.2 compiler fr
2a50: 6f 6d 20 41 70 70 6c 65 20 63 68 6f 6b 65 73 20  om Apple chokes 
2a60: 6f 6e 0a 2a 2a 20 74 68 65 20 28 28 76 6f 69 64  on.** the ((void
2a70: 2a 29 26 28 28 63 68 61 72 2a 29 30 29 5b 58 5d  *)&((char*)0)[X]
2a80: 29 20 63 6f 6e 73 74 72 75 63 74 2e 20 20 42 75  ) construct.  Bu
2a90: 74 20 4d 53 56 43 20 63 68 6f 6b 65 73 20 6f 6e  t MSVC chokes on
2aa0: 20 28 28 76 6f 69 64 2a 29 28 58 29 29 2e 0a 2a   ((void*)(X))..*
2ab0: 2a 20 53 6f 20 77 65 20 68 61 76 65 20 74 6f 20  * So we have to 
2ac0: 64 65 66 69 6e 65 20 74 68 65 20 6d 61 63 72 6f  define the macro
2ad0: 73 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 77  s in different w
2ae0: 61 79 73 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  ays depending on
2af0: 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72   the.** compiler
2b00: 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64  ..*/.#if defined
2b10: 28 5f 5f 47 4e 55 43 5f 5f 29 0a 23 20 69 66 20  (__GNUC__).# if 
2b20: 64 65 66 69 6e 65 64 28 48 41 56 45 5f 53 54 44  defined(HAVE_STD
2b30: 49 4e 54 5f 48 29 0a 23 20 20 20 64 65 66 69 6e  INT_H).#   defin
2b40: 65 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f  e SQLITE_INT_TO_
2b50: 50 54 52 28 58 29 20 20 28 28 76 6f 69 64 2a 29  PTR(X)  ((void*)
2b60: 28 69 6e 74 70 74 72 5f 74 29 28 58 29 29 0a 23  (intptr_t)(X)).#
2b70: 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45     define SQLITE
2b80: 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 58 29 20 20  _PTR_TO_INT(X)  
2b90: 28 28 69 6e 74 29 28 69 6e 74 70 74 72 5f 74 29  ((int)(intptr_t)
2ba0: 28 58 29 29 0a 23 20 65 6c 73 65 0a 23 20 20 20  (X)).# else.#   
2bb0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e  define SQLITE_IN
2bc0: 54 5f 54 4f 5f 50 54 52 28 58 29 20 20 28 28 76  T_TO_PTR(X)  ((v
2bd0: 6f 69 64 2a 29 28 58 29 29 0a 23 20 20 20 64 65  oid*)(X)).#   de
2be0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 54 52 5f  fine SQLITE_PTR_
2bf0: 54 4f 5f 49 4e 54 28 58 29 20 20 28 28 69 6e 74  TO_INT(X)  ((int
2c00: 29 28 58 29 29 0a 23 20 65 6e 64 69 66 0a 23 65  )(X)).# endif.#e
2c10: 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  lse.# define SQL
2c20: 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 58  ITE_INT_TO_PTR(X
2c30: 29 20 20 20 28 28 76 6f 69 64 2a 29 26 28 28 63  )   ((void*)&((c
2c40: 68 61 72 2a 29 30 29 5b 58 5d 29 0a 23 20 64 65  har*)0)[X]).# de
2c50: 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 54 52 5f  fine SQLITE_PTR_
2c60: 54 4f 5f 49 4e 54 28 58 29 20 20 20 28 28 69 6e  TO_INT(X)   ((in
2c70: 74 29 28 28 28 63 68 61 72 2a 29 58 29 2d 28 63  t)(((char*)X)-(c
2c80: 68 61 72 2a 29 30 29 29 0a 23 65 6e 64 69 66 0a  har*)0)).#endif.
2c90: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  ../*.** The SQLI
2ca0: 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 6d 61  TE_THREADSAFE ma
2cb0: 63 72 6f 20 6d 75 73 74 20 62 65 20 64 65 66 69  cro must be defi
2cc0: 6e 65 64 20 61 73 20 65 69 74 68 65 72 20 30 20  ned as either 0 
2cd0: 6f 72 20 31 2e 0a 2a 2a 20 4f 6c 64 65 72 20 76  or 1..** Older v
2ce0: 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74  ersions of SQLit
2cf0: 65 20 75 73 65 64 20 61 6e 20 6f 70 74 69 6f 6e  e used an option
2d00: 61 6c 20 54 48 52 45 41 44 53 41 46 45 20 6d 61  al THREADSAFE ma
2d10: 63 72 6f 2e 0a 2a 2a 20 57 65 20 73 75 70 70 6f  cro..** We suppo
2d20: 72 74 20 74 68 61 74 20 66 6f 72 20 6c 65 67 61  rt that for lega
2d30: 63 79 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  cy.*/.#if !defin
2d40: 65 64 28 53 51 4c 49 54 45 5f 54 48 52 45 41 44  ed(SQLITE_THREAD
2d50: 53 41 46 45 29 0a 23 69 66 20 64 65 66 69 6e 65  SAFE).#if define
2d60: 64 28 54 48 52 45 41 44 53 41 46 45 29 0a 23 20  d(THREADSAFE).# 
2d70: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 48  define SQLITE_TH
2d80: 52 45 41 44 53 41 46 45 20 54 48 52 45 41 44 53  READSAFE THREADS
2d90: 41 46 45 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  AFE.#else.# defi
2da0: 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  ne SQLITE_THREAD
2db0: 53 41 46 45 20 31 0a 23 65 6e 64 69 66 0a 23 65  SAFE 1.#endif.#e
2dc0: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
2dd0: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d  SQLITE_DEFAULT_M
2de0: 45 4d 53 54 41 54 55 53 20 6d 61 63 72 6f 20 6d  EMSTATUS macro m
2df0: 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 61  ust be defined a
2e00: 73 20 65 69 74 68 65 72 20 30 20 6f 72 20 31 2e  s either 0 or 1.
2e10: 0a 2a 2a 20 49 74 20 64 65 74 65 72 6d 69 6e 65  .** It determine
2e20: 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  s whether or not
2e30: 20 74 68 65 20 66 65 61 74 75 72 65 73 20 72 65   the features re
2e40: 6c 61 74 65 64 20 74 6f 20 0a 2a 2a 20 53 51 4c  lated to .** SQL
2e50: 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54  ITE_CONFIG_MEMST
2e60: 41 54 55 53 20 61 72 65 20 61 76 61 69 6c 61 62  ATUS are availab
2e70: 6c 65 20 62 79 20 64 65 66 61 75 6c 74 20 6f 72  le by default or
2e80: 20 6e 6f 74 2e 20 54 68 69 73 20 76 61 6c 75 65   not. This value
2e90: 20 63 61 6e 0a 2a 2a 20 62 65 20 6f 76 65 72 72   can.** be overr
2ea0: 69 64 64 65 6e 20 61 74 20 72 75 6e 74 69 6d 65  idden at runtime
2eb0: 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74   using the sqlit
2ec0: 65 33 5f 63 6f 6e 66 69 67 28 29 20 41 50 49 2e  e3_config() API.
2ed0: 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .*/.#if !defined
2ee0: 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f  (SQLITE_DEFAULT_
2ef0: 4d 45 4d 53 54 41 54 55 53 29 0a 23 20 64 65 66  MEMSTATUS).# def
2f00: 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55  ine SQLITE_DEFAU
2f10: 4c 54 5f 4d 45 4d 53 54 41 54 55 53 20 31 0a 23  LT_MEMSTATUS 1.#
2f20: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 61  endif../*.** Exa
2f30: 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68 65 20  ctly one of the 
2f40: 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73  following macros
2f50: 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 64   must be defined
2f60: 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20   in order to.** 
2f70: 73 70 65 63 69 66 79 20 77 68 69 63 68 20 6d 65  specify which me
2f80: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
2f90: 73 75 62 73 79 73 74 65 6d 20 74 6f 20 75 73 65  subsystem to use
2fa0: 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c 49  ..**.**     SQLI
2fb0: 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43  TE_SYSTEM_MALLOC
2fc0: 20 20 20 20 20 20 20 20 20 20 2f 2f 20 55 73 65            // Use
2fd0: 20 6e 6f 72 6d 61 6c 20 73 79 73 74 65 6d 20 6d   normal system m
2fe0: 61 6c 6c 6f 63 28 29 0a 2a 2a 20 20 20 20 20 53  alloc().**     S
2ff0: 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 20 20  QLITE_MEMDEBUG  
3000: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2f 20               // 
3010: 44 65 62 75 67 67 69 6e 67 20 76 65 72 73 69 6f  Debugging versio
3020: 6e 20 6f 66 20 73 79 73 74 65 6d 20 6d 61 6c 6c  n of system mall
3030: 6f 63 28 29 0a 2a 2a 20 20 20 20 20 53 51 4c 49  oc().**     SQLI
3040: 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20 20  TE_MEMORY_SIZE  
3050: 20 20 20 20 20 20 20 20 20 20 2f 2f 20 69 6e 74            // int
3060: 65 72 6e 61 6c 20 61 6c 6c 6f 63 61 74 6f 72 20  ernal allocator 
3070: 23 31 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  #1.**     SQLITE
3080: 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 20  _MMAP_HEAP_SIZE 
3090: 20 20 20 20 20 20 20 20 2f 2f 20 69 6e 74 65 72          // inter
30a0: 6e 61 6c 20 6d 6d 61 70 28 29 20 61 6c 6c 6f 63  nal mmap() alloc
30b0: 61 74 6f 72 0a 2a 2a 20 20 20 20 20 53 51 4c 49  ator.**     SQLI
30c0: 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53  TE_POW2_MEMORY_S
30d0: 49 5a 45 20 20 20 20 20 20 20 2f 2f 20 69 6e 74  IZE       // int
30e0: 65 72 6e 61 6c 20 70 6f 77 65 72 2d 6f 66 2d 74  ernal power-of-t
30f0: 77 6f 20 61 6c 6c 6f 63 61 74 6f 72 0a 2a 2a 0a  wo allocator.**.
3100: 2a 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68  ** If none of th
3110: 65 20 61 62 6f 76 65 20 61 72 65 20 64 65 66 69  e above are defi
3120: 6e 65 64 2c 20 74 68 65 6e 20 73 65 74 20 53 51  ned, then set SQ
3130: 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c  LITE_SYSTEM_MALL
3140: 4f 43 20 61 73 0a 2a 2a 20 74 68 65 20 64 65 66  OC as.** the def
3150: 61 75 6c 74 2e 0a 2a 2f 0a 23 69 66 20 64 65 66  ault..*/.#if def
3160: 69 6e 65 64 28 53 51 4c 49 54 45 5f 53 59 53 54  ined(SQLITE_SYST
3170: 45 4d 5f 4d 41 4c 4c 4f 43 29 2b 64 65 66 69 6e  EM_MALLOC)+defin
3180: 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42  ed(SQLITE_MEMDEB
3190: 55 47 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65  UG)+\.    define
31a0: 64 28 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f  d(SQLITE_MEMORY_
31b0: 53 49 5a 45 29 2b 64 65 66 69 6e 65 64 28 53 51  SIZE)+defined(SQ
31c0: 4c 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53  LITE_MMAP_HEAP_S
31d0: 49 5a 45 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e  IZE)+\.    defin
31e0: 65 64 28 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d  ed(SQLITE_POW2_M
31f0: 45 4d 4f 52 59 5f 53 49 5a 45 29 3e 31 0a 23 20  EMORY_SIZE)>1.# 
3200: 65 72 72 6f 72 20 22 41 74 20 6d 6f 73 74 20 6f  error "At most o
3210: 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
3220: 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  ing compile-time
3230: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f   configuration o
3240: 70 74 69 6f 6e 73 5c 0a 20 69 73 20 61 6c 6c 6f  ptions\. is allo
3250: 77 73 3a 20 53 51 4c 49 54 45 5f 53 59 53 54 45  ws: SQLITE_SYSTE
3260: 4d 5f 4d 41 4c 4c 4f 43 2c 20 53 51 4c 49 54 45  M_MALLOC, SQLITE
3270: 5f 4d 45 4d 44 45 42 55 47 2c 20 53 51 4c 49 54  _MEMDEBUG, SQLIT
3280: 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 2c 5c 0a  E_MEMORY_SIZE,\.
3290: 20 53 51 4c 49 54 45 5f 4d 4d 41 50 5f 48 45 41   SQLITE_MMAP_HEA
32a0: 50 5f 53 49 5a 45 2c 20 53 51 4c 49 54 45 5f 50  P_SIZE, SQLITE_P
32b0: 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 22  OW2_MEMORY_SIZE"
32c0: 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69  .#endif.#if defi
32d0: 6e 65 64 28 53 51 4c 49 54 45 5f 53 59 53 54 45  ned(SQLITE_SYSTE
32e0: 4d 5f 4d 41 4c 4c 4f 43 29 2b 64 65 66 69 6e 65  M_MALLOC)+define
32f0: 64 28 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55  d(SQLITE_MEMDEBU
3300: 47 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65 64  G)+\.    defined
3310: 28 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53  (SQLITE_MEMORY_S
3320: 49 5a 45 29 2b 64 65 66 69 6e 65 64 28 53 51 4c  IZE)+defined(SQL
3330: 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49  ITE_MMAP_HEAP_SI
3340: 5a 45 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65  ZE)+\.    define
3350: 64 28 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d 45  d(SQLITE_POW2_ME
3360: 4d 4f 52 59 5f 53 49 5a 45 29 3d 3d 30 0a 23 20  MORY_SIZE)==0.# 
3370: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59  define SQLITE_SY
3380: 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 31 0a 23 65  STEM_MALLOC 1.#e
3390: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53  ndif../*.** If S
33a0: 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46  QLITE_MALLOC_SOF
33b0: 54 5f 4c 49 4d 49 54 20 69 73 20 6e 6f 74 20 7a  T_LIMIT is not z
33c0: 65 72 6f 2c 20 74 68 65 6e 20 74 72 79 20 74 6f  ero, then try to
33d0: 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 69 7a   keep the.** siz
33e0: 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c  es of memory all
33f0: 6f 63 61 74 69 6f 6e 73 20 62 65 6c 6f 77 20 74  ocations below t
3400: 68 69 73 20 76 61 6c 75 65 20 77 68 65 72 65 20  his value where 
3410: 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a 23 69 66  possible..*/.#if
3420: 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
3430: 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d  _MALLOC_SOFT_LIM
3440: 49 54 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  IT).# define SQL
3450: 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f  ITE_MALLOC_SOFT_
3460: 4c 49 4d 49 54 20 31 30 32 34 0a 23 65 6e 64 69  LIMIT 1024.#endi
3470: 66 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 6e 65 65 64  f../*.** We need
3480: 20 74 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50 45   to define _XOPE
3490: 4e 5f 53 4f 55 52 43 45 20 61 73 20 66 6f 6c 6c  N_SOURCE as foll
34a0: 6f 77 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  ows in order to 
34b0: 65 6e 61 62 6c 65 0a 2a 2a 20 72 65 63 75 72 73  enable.** recurs
34c0: 69 76 65 20 6d 75 74 65 78 65 73 20 6f 6e 20 6d  ive mutexes on m
34d0: 6f 73 74 20 55 6e 69 78 20 73 79 73 74 65 6d 73  ost Unix systems
34e0: 2e 20 20 42 75 74 20 4d 61 63 20 4f 53 20 58 20  .  But Mac OS X 
34f0: 69 73 20 64 69 66 66 65 72 65 6e 74 2e 0a 2a 2a  is different..**
3500: 20 54 68 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52   The _XOPEN_SOUR
3510: 43 45 20 64 65 66 69 6e 65 20 63 61 75 73 65 73  CE define causes
3520: 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 4d 61   problems for Ma
3530: 63 20 4f 53 20 58 20 77 65 20 61 72 65 20 74 6f  c OS X we are to
3540: 6c 64 2c 0a 2a 2a 20 73 6f 20 69 74 20 69 73 20  ld,.** so it is 
3550: 6f 6d 69 74 74 65 64 20 74 68 65 72 65 2e 20 20  omitted there.  
3560: 53 65 65 20 74 69 63 6b 65 74 20 23 32 36 37 33  See ticket #2673
3570: 2e 0a 2a 2a 0a 2a 2a 20 4c 61 74 65 72 20 77 65  ..**.** Later we
3580: 20 6c 65 61 72 6e 20 74 68 61 74 20 5f 58 4f 50   learn that _XOP
3590: 45 4e 5f 53 4f 55 52 43 45 20 69 73 20 70 6f 6f  EN_SOURCE is poo
35a0: 72 6c 79 20 6f 72 20 69 6e 63 6f 72 72 65 63 74  rly or incorrect
35b0: 6c 79 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65  ly.** implemente
35c0: 64 20 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d  d on some system
35d0: 73 2e 20 20 53 6f 20 77 65 20 61 76 6f 69 64 20  s.  So we avoid 
35e0: 64 65 66 69 6e 69 6e 67 20 69 74 20 61 74 20 61  defining it at a
35f0: 6c 6c 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 61  ll.** if it is a
3600: 6c 72 65 61 64 79 20 64 65 66 69 6e 65 64 20 6f  lready defined o
3610: 72 20 69 66 20 69 74 20 69 73 20 75 6e 6e 65 65  r if it is unnee
3620: 64 65 64 20 62 65 63 61 75 73 65 20 77 65 20 61  ded because we a
3630: 72 65 0a 2a 2a 20 6e 6f 74 20 64 6f 69 6e 67 20  re.** not doing 
3640: 61 20 74 68 72 65 61 64 73 61 66 65 20 62 75 69  a threadsafe bui
3650: 6c 64 2e 20 20 54 69 63 6b 65 74 20 23 32 36 38  ld.  Ticket #268
3660: 31 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  1..**.** See als
3670: 6f 20 74 69 63 6b 65 74 20 23 32 37 34 31 2e 0a  o ticket #2741..
3680: 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  */.#if !defined(
3690: 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 29 20 26  _XOPEN_SOURCE) &
36a0: 26 20 21 64 65 66 69 6e 65 64 28 5f 5f 44 41 52  & !defined(__DAR
36b0: 57 49 4e 5f 5f 29 20 26 26 20 21 64 65 66 69 6e  WIN__) && !defin
36c0: 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26  ed(__APPLE__) &&
36d0: 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
36e0: 46 45 0a 23 20 20 64 65 66 69 6e 65 20 5f 58 4f  FE.#  define _XO
36f0: 50 45 4e 5f 53 4f 55 52 43 45 20 35 30 30 20 20  PEN_SOURCE 500  
3700: 2f 2a 20 4e 65 65 64 65 64 20 74 6f 20 65 6e 61  /* Needed to ena
3710: 62 6c 65 20 70 74 68 72 65 61 64 20 72 65 63 75  ble pthread recu
3720: 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 2a 2f  rsive mutexes */
3730: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
3740: 68 65 20 54 43 4c 20 68 65 61 64 65 72 73 20 61  he TCL headers a
3750: 72 65 20 6f 6e 6c 79 20 6e 65 65 64 65 64 20 77  re only needed w
3760: 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68  hen compiling th
3770: 65 20 54 43 4c 20 62 69 6e 64 69 6e 67 73 2e 0a  e TCL bindings..
3780: 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  */.#if defined(S
3790: 51 4c 49 54 45 5f 54 43 4c 29 20 7c 7c 20 64 65  QLITE_TCL) || de
37a0: 66 69 6e 65 64 28 54 43 4c 53 48 29 0a 23 20 69  fined(TCLSH).# i
37b0: 6e 63 6c 75 64 65 20 3c 74 63 6c 2e 68 3e 0a 23  nclude <tcl.h>.#
37c0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6e  endif../*.** Man
37d0: 79 20 70 65 6f 70 6c 65 20 61 72 65 20 66 61 69  y people are fai
37e0: 6c 69 6e 67 20 74 6f 20 73 65 74 20 2d 44 4e 44  ling to set -DND
37f0: 45 42 55 47 3d 31 20 77 68 65 6e 20 63 6f 6d 70  EBUG=1 when comp
3800: 69 6c 69 6e 67 20 53 51 4c 69 74 65 2e 0a 2a 2a  iling SQLite..**
3810: 20 53 65 74 74 69 6e 67 20 4e 44 45 42 55 47 20   Setting NDEBUG 
3820: 6d 61 6b 65 73 20 74 68 65 20 63 6f 64 65 20 73  makes the code s
3830: 6d 61 6c 6c 65 72 20 61 6e 64 20 72 75 6e 20 66  maller and run f
3840: 61 73 74 65 72 2e 20 20 53 6f 20 74 68 65 20 66  aster.  So the f
3850: 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 6c 69 6e 65  ollowing.** line
3860: 73 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 61  s are added to a
3870: 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 73 65 74  utomatically set
3880: 20 4e 44 45 42 55 47 20 75 6e 6c 65 73 73 20 74   NDEBUG unless t
3890: 68 65 20 2d 44 53 51 4c 49 54 45 5f 44 45 42 55  he -DSQLITE_DEBU
38a0: 47 3d 31 0a 2a 2a 20 6f 70 74 69 6f 6e 20 69 73  G=1.** option is
38b0: 20 73 65 74 2e 20 20 54 68 75 73 20 4e 44 45 42   set.  Thus NDEB
38c0: 55 47 20 62 65 63 6f 6d 65 73 20 61 6e 20 6f 70  UG becomes an op
38d0: 74 2d 69 6e 20 72 61 74 68 65 72 20 74 68 61 6e  t-in rather than
38e0: 20 61 6e 20 6f 70 74 2d 6f 75 74 0a 2a 2a 20 66   an opt-out.** f
38f0: 65 61 74 75 72 65 2e 0a 2a 2f 0a 23 69 66 20 21  eature..*/.#if !
3900: 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20  defined(NDEBUG) 
3910: 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
3920: 54 45 5f 44 45 42 55 47 29 20 0a 23 20 64 65 66  TE_DEBUG) .# def
3930: 69 6e 65 20 4e 44 45 42 55 47 20 31 0a 23 65 6e  ine NDEBUG 1.#en
3940: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74  dif../*.** The t
3950: 65 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20  estcase() macro 
3960: 69 73 20 75 73 65 64 20 74 6f 20 61 69 64 20 69  is used to aid i
3970: 6e 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 69  n coverage testi
3980: 6e 67 2e 20 20 57 68 65 6e 20 0a 2a 2a 20 64 6f  ng.  When .** do
3990: 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 65 73  ing coverage tes
39a0: 74 69 6e 67 2c 20 74 68 65 20 63 6f 6e 64 69 74  ting, the condit
39b0: 69 6f 6e 20 69 6e 73 69 64 65 20 74 68 65 20 61  ion inside the a
39c0: 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 65  rgument to.** te
39d0: 73 74 63 61 73 65 28 29 20 6d 75 73 74 20 62 65  stcase() must be
39e0: 20 65 76 61 6c 75 61 74 65 64 20 62 6f 74 68 20   evaluated both 
39f0: 74 72 75 65 20 61 6e 64 20 66 61 6c 73 65 20 69  true and false i
3a00: 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 67 65  n order to.** ge
3a10: 74 20 66 75 6c 6c 20 62 72 61 6e 63 68 20 63 6f  t full branch co
3a20: 76 65 72 61 67 65 2e 20 20 54 68 65 20 74 65 73  verage.  The tes
3a30: 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 69 73  tcase() macro is
3a40: 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 74 6f 20   inserted.** to 
3a50: 68 65 6c 70 20 65 6e 73 75 72 65 20 61 64 65 71  help ensure adeq
3a60: 75 61 74 65 20 74 65 73 74 20 63 6f 76 65 72 61  uate test covera
3a70: 67 65 20 69 6e 20 70 6c 61 63 65 73 20 77 68 65  ge in places whe
3a80: 72 65 20 73 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6e  re simple.** con
3a90: 64 69 74 69 6f 6e 2f 64 65 63 69 73 69 6f 6e 20  dition/decision 
3aa0: 63 6f 76 65 72 61 67 65 20 69 73 20 69 6e 61 64  coverage is inad
3ab0: 65 71 75 61 74 65 2e 20 20 46 6f 72 20 65 78 61  equate.  For exa
3ac0: 6d 70 6c 65 2c 20 74 65 73 74 63 61 73 65 28 29  mple, testcase()
3ad0: 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20  .** can be used 
3ae0: 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 62 6f 75  to make sure bou
3af0: 6e 64 61 72 79 20 76 61 6c 75 65 73 20 61 72 65  ndary values are
3b00: 20 74 65 73 74 65 64 2e 20 20 46 6f 72 0a 2a 2a   tested.  For.**
3b10: 20 62 69 74 6d 61 73 6b 20 74 65 73 74 73 2c 20   bitmask tests, 
3b20: 74 65 73 74 63 61 73 65 28 29 20 63 61 6e 20 62  testcase() can b
3b30: 65 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73  e used to make s
3b40: 75 72 65 20 65 61 63 68 20 62 69 74 0a 2a 2a 20  ure each bit.** 
3b50: 69 73 20 73 69 67 6e 69 66 69 63 61 6e 74 20 61  is significant a
3b60: 6e 64 20 75 73 65 64 20 61 74 20 6c 65 61 73 74  nd used at least
3b70: 20 6f 6e 63 65 2e 20 20 4f 6e 20 73 77 69 74 63   once.  On switc
3b80: 68 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20  h statements.** 
3b90: 77 68 65 72 65 20 6d 75 6c 74 69 70 6c 65 20 63  where multiple c
3ba0: 61 73 65 73 20 67 6f 20 74 6f 20 74 68 65 20 73  ases go to the s
3bb0: 61 6d 65 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64  ame block of cod
3bc0: 65 2c 20 74 65 73 74 63 61 73 65 28 29 0a 2a 2a  e, testcase().**
3bd0: 20 63 61 6e 20 69 6e 73 75 72 65 20 74 68 61 74   can insure that
3be0: 20 61 6c 6c 20 63 61 73 65 73 20 61 72 65 20 65   all cases are e
3bf0: 76 61 6c 75 61 74 65 64 2e 0a 2a 2a 0a 2a 2f 0a  valuated..**.*/.
3c00: 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 4f  #ifdef SQLITE_CO
3c10: 56 45 52 41 47 45 5f 54 45 53 54 0a 53 51 4c 49  VERAGE_TEST.SQLI
3c20: 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
3c30: 64 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61 67  d sqlite3Coverag
3c40: 65 28 69 6e 74 29 3b 0a 23 20 64 65 66 69 6e 65  e(int);.# define
3c50: 20 74 65 73 74 63 61 73 65 28 58 29 20 20 69 66   testcase(X)  if
3c60: 28 20 58 20 29 7b 20 73 71 6c 69 74 65 33 43 6f  ( X ){ sqlite3Co
3c70: 76 65 72 61 67 65 28 5f 5f 4c 49 4e 45 5f 5f 29  verage(__LINE__)
3c80: 3b 20 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  ; }.#else.# defi
3c90: 6e 65 20 74 65 73 74 63 61 73 65 28 58 29 0a 23  ne testcase(X).#
3ca0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
3cb0: 20 54 45 53 54 4f 4e 4c 59 20 6d 61 63 72 6f 20   TESTONLY macro 
3cc0: 69 73 20 75 73 65 64 20 74 6f 20 65 6e 63 6c 6f  is used to enclo
3cd0: 73 65 20 76 61 72 69 61 62 6c 65 20 64 65 63 6c  se variable decl
3ce0: 61 72 61 74 69 6f 6e 73 20 6f 72 0a 2a 2a 20 6f  arations or.** o
3cf0: 74 68 65 72 20 62 69 74 73 20 6f 66 20 63 6f 64  ther bits of cod
3d00: 65 20 74 68 61 74 20 61 72 65 20 6e 65 65 64 65  e that are neede
3d10: 64 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68 65  d to support the
3d20: 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 77 69   arguments.** wi
3d30: 74 68 69 6e 20 74 65 73 74 63 61 73 65 28 29 20  thin testcase() 
3d40: 61 6e 64 20 61 73 73 65 72 74 28 29 20 6d 61 63  and assert() mac
3d50: 72 6f 73 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66  ros..*/.#if !def
3d60: 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20  ined(NDEBUG) || 
3d70: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43  defined(SQLITE_C
3d80: 4f 56 45 52 41 47 45 5f 54 45 53 54 29 0a 23 20  OVERAGE_TEST).# 
3d90: 64 65 66 69 6e 65 20 54 45 53 54 4f 4e 4c 59 28  define TESTONLY(
3da0: 58 29 20 20 58 0a 23 65 6c 73 65 0a 23 20 64 65  X)  X.#else.# de
3db0: 66 69 6e 65 20 54 45 53 54 4f 4e 4c 59 28 58 29  fine TESTONLY(X)
3dc0: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53  .#endif../*.** S
3dd0: 6f 6d 65 74 69 6d 65 73 20 77 65 20 6e 65 65 64  ometimes we need
3de0: 20 61 20 73 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20   a small amount 
3df0: 6f 66 20 63 6f 64 65 20 73 75 63 68 20 61 73 20  of code such as 
3e00: 61 20 76 61 72 69 61 62 6c 65 20 69 6e 69 74 69  a variable initi
3e10: 61 6c 69 7a 61 74 69 6f 6e 0a 2a 2a 20 74 6f 20  alization.** to 
3e20: 73 65 74 75 70 20 66 6f 72 20 61 20 6c 61 74 65  setup for a late
3e30: 72 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65  r assert() state
3e40: 6d 65 6e 74 2e 20 20 57 65 20 64 6f 20 6e 6f 74  ment.  We do not
3e50: 20 77 61 6e 74 20 74 68 69 73 20 63 6f 64 65 20   want this code 
3e60: 74 6f 0a 2a 2a 20 61 70 70 65 61 72 20 77 68 65  to.** appear whe
3e70: 6e 20 61 73 73 65 72 74 28 29 20 69 73 20 64 69  n assert() is di
3e80: 73 61 62 6c 65 64 2e 20 20 54 68 65 20 66 6f 6c  sabled.  The fol
3e90: 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 69 73 20  lowing macro is 
3ea0: 74 68 65 72 65 66 6f 72 65 0a 2a 2a 20 75 73 65  therefore.** use
3eb0: 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 61  d to contain tha
3ec0: 74 20 73 65 74 75 70 20 63 6f 64 65 2e 20 20 54  t setup code.  T
3ed0: 68 65 20 22 56 56 41 22 20 61 63 72 6f 6e 79 6d  he "VVA" acronym
3ee0: 20 73 74 61 6e 64 73 20 66 6f 72 0a 2a 2a 20 22   stands for.** "
3ef0: 56 65 72 69 66 69 63 61 74 69 6f 6e 2c 20 56 61  Verification, Va
3f00: 6c 69 64 61 74 69 6f 6e 2c 20 61 6e 64 20 41 63  lidation, and Ac
3f10: 63 72 65 64 69 74 61 74 69 6f 6e 22 2e 20 20 49  creditation".  I
3f20: 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74  n other words, t
3f30: 68 65 0a 2a 2a 20 63 6f 64 65 20 77 69 74 68 69  he.** code withi
3f40: 6e 20 56 56 41 5f 4f 4e 4c 59 28 29 20 77 69 6c  n VVA_ONLY() wil
3f50: 6c 20 6f 6e 6c 79 20 72 75 6e 20 64 75 72 69 6e  l only run durin
3f60: 67 20 76 65 72 69 66 69 63 61 74 69 6f 6e 20 70  g verification p
3f70: 72 6f 63 65 73 73 65 73 2e 0a 2a 2f 0a 23 69 66  rocesses..*/.#if
3f80: 6e 64 65 66 20 4e 44 45 42 55 47 0a 23 20 64 65  ndef NDEBUG.# de
3f90: 66 69 6e 65 20 56 56 41 5f 4f 4e 4c 59 28 58 29  fine VVA_ONLY(X)
3fa0: 20 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69    X.#else.# defi
3fb0: 6e 65 20 56 56 41 5f 4f 4e 4c 59 28 58 29 0a 23  ne VVA_ONLY(X).#
3fc0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
3fd0: 20 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45   ALWAYS and NEVE
3fe0: 52 20 6d 61 63 72 6f 73 20 73 75 72 72 6f 75 6e  R macros surroun
3ff0: 64 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73  d boolean expres
4000: 73 69 6f 6e 73 20 77 68 69 63 68 20 0a 2a 2a 20  sions which .** 
4010: 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f 20  are intended to 
4020: 61 6c 77 61 79 73 20 62 65 20 74 72 75 65 20 6f  always be true o
4030: 72 20 66 61 6c 73 65 2c 20 72 65 73 70 65 63 74  r false, respect
4040: 69 76 65 6c 79 2e 20 20 53 75 63 68 0a 2a 2a 20  ively.  Such.** 
4050: 65 78 70 72 65 73 73 69 6f 6e 73 20 63 6f 75 6c  expressions coul
4060: 64 20 62 65 20 6f 6d 69 74 74 65 64 20 66 72 6f  d be omitted fro
4070: 6d 20 74 68 65 20 63 6f 64 65 20 63 6f 6d 70 6c  m the code compl
4080: 65 74 65 6c 79 2e 20 20 42 75 74 20 74 68 65 79  etely.  But they
4090: 0a 2a 2a 20 61 72 65 20 69 6e 63 6c 75 64 65 64  .** are included
40a0: 20 69 6e 20 61 20 66 65 77 20 63 61 73 65 73 20   in a few cases 
40b0: 69 6e 20 6f 72 64 65 72 20 74 6f 20 65 6e 68 61  in order to enha
40c0: 6e 63 65 20 74 68 65 20 72 65 73 69 6c 69 65 6e  nce the resilien
40d0: 63 65 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20  ce.** of SQLite 
40e0: 74 6f 20 75 6e 65 78 70 65 63 74 65 64 20 62 65  to unexpected be
40f0: 68 61 76 69 6f 72 20 2d 20 74 6f 20 6d 61 6b 65  havior - to make
4100: 20 74 68 65 20 63 6f 64 65 20 22 73 65 6c 66 2d   the code "self-
4110: 68 65 61 6c 69 6e 67 22 0a 2a 2a 20 6f 72 20 22  healing".** or "
4120: 64 75 63 74 69 6c 65 22 20 72 61 74 68 65 72 20  ductile" rather 
4130: 74 68 61 6e 20 62 65 69 6e 67 20 22 62 72 69 74  than being "brit
4140: 74 6c 65 22 20 61 6e 64 20 63 72 61 73 68 69 6e  tle" and crashin
4150: 67 20 61 74 20 74 68 65 20 66 69 72 73 74 0a 2a  g at the first.*
4160: 2a 20 68 69 6e 74 20 6f 66 20 75 6e 70 6c 61 6e  * hint of unplan
4170: 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a  ned behavior..**
4180: 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72  .** In other wor
4190: 64 73 2c 20 41 4c 57 41 59 53 20 61 6e 64 20 4e  ds, ALWAYS and N
41a0: 45 56 45 52 20 61 72 65 20 61 64 64 65 64 20 66  EVER are added f
41b0: 6f 72 20 64 65 66 65 6e 73 69 76 65 20 63 6f 64  or defensive cod
41c0: 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 64 6f  e..**.** When do
41d0: 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 65 73  ing coverage tes
41e0: 74 69 6e 67 20 41 4c 57 41 59 53 20 61 6e 64 20  ting ALWAYS and 
41f0: 4e 45 56 45 52 20 61 72 65 20 68 61 72 64 2d 63  NEVER are hard-c
4200: 6f 64 65 64 20 74 6f 0a 2a 2a 20 62 65 20 74 72  oded to.** be tr
4210: 75 65 20 61 6e 64 20 66 61 6c 73 65 20 73 6f 20  ue and false so 
4220: 74 68 61 74 20 74 68 65 20 75 6e 72 65 61 63 68  that the unreach
4230: 61 62 6c 65 20 63 6f 64 65 20 74 68 65 6e 20 73  able code then s
4240: 70 65 63 69 66 79 20 77 69 6c 6c 0a 2a 2a 20 6e  pecify will.** n
4250: 6f 74 20 62 65 20 63 6f 75 6e 74 65 64 20 61 73  ot be counted as
4260: 20 75 6e 74 65 73 74 65 64 20 63 6f 64 65 2e 0a   untested code..
4270: 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  */.#if defined(S
4280: 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 5f 54  QLITE_COVERAGE_T
4290: 45 53 54 29 0a 23 20 64 65 66 69 6e 65 20 41 4c  EST).# define AL
42a0: 57 41 59 53 28 58 29 20 20 20 20 20 20 28 31 29  WAYS(X)      (1)
42b0: 0a 23 20 64 65 66 69 6e 65 20 4e 45 56 45 52 28  .# define NEVER(
42c0: 58 29 20 20 20 20 20 20 20 28 30 29 0a 23 65 6c  X)       (0).#el
42d0: 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42  if !defined(NDEB
42e0: 55 47 29 0a 23 20 64 65 66 69 6e 65 20 41 4c 57  UG).# define ALW
42f0: 41 59 53 28 58 29 20 20 20 20 20 20 28 28 58 29  AYS(X)      ((X)
4300: 3f 31 3a 28 61 73 73 65 72 74 28 30 29 2c 30 29  ?1:(assert(0),0)
4310: 29 0a 23 20 64 65 66 69 6e 65 20 4e 45 56 45 52  ).# define NEVER
4320: 28 58 29 20 20 20 20 20 20 20 28 28 58 29 3f 28  (X)       ((X)?(
4330: 61 73 73 65 72 74 28 30 29 2c 31 29 3a 30 29 0a  assert(0),1):0).
4340: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 41  #else.# define A
4350: 4c 57 41 59 53 28 58 29 20 20 20 20 20 20 28 58  LWAYS(X)      (X
4360: 29 0a 23 20 64 65 66 69 6e 65 20 4e 45 56 45 52  ).# define NEVER
4370: 28 58 29 20 20 20 20 20 20 20 28 58 29 0a 23 65  (X)       (X).#e
4380: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
4390: 6d 61 63 72 6f 20 75 6e 6c 69 6b 65 6c 79 28 29  macro unlikely()
43a0: 20 69 73 20 61 20 68 69 6e 74 20 74 68 61 74 20   is a hint that 
43b0: 73 75 72 72 6f 75 6e 64 73 20 61 20 62 6f 6f 6c  surrounds a bool
43c0: 65 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  ean.** expressio
43d0: 6e 20 74 68 61 74 20 69 73 20 75 73 75 61 6c 6c  n that is usuall
43e0: 79 20 66 61 6c 73 65 2e 20 20 4d 61 63 72 6f 20  y false.  Macro 
43f0: 6c 69 6b 65 6c 79 28 29 20 73 75 72 72 6f 75 6e  likely() surroun
4400: 64 73 0a 2a 2a 20 61 20 62 6f 6f 6c 65 61 6e 20  ds.** a boolean 
4410: 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20  expression that 
4420: 69 73 20 75 73 75 61 6c 6c 79 20 74 72 75 65 2e  is usually true.
4430: 20 20 47 43 43 20 69 73 20 61 62 6c 65 20 74 6f    GCC is able to
4440: 0a 2a 2a 20 75 73 65 20 74 68 65 73 65 20 68 69  .** use these hi
4450: 6e 74 73 20 74 6f 20 67 65 6e 65 72 61 74 65 20  nts to generate 
4460: 62 65 74 74 65 72 20 63 6f 64 65 2c 20 73 6f 6d  better code, som
4470: 65 74 69 6d 65 73 2e 0a 2a 2f 0a 23 69 66 20 64  etimes..*/.#if d
4480: 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29  efined(__GNUC__)
4490: 20 26 26 20 30 0a 23 20 64 65 66 69 6e 65 20 6c   && 0.# define l
44a0: 69 6b 65 6c 79 28 58 29 20 20 20 20 5f 5f 62 75  ikely(X)    __bu
44b0: 69 6c 74 69 6e 5f 65 78 70 65 63 74 28 28 58 29  iltin_expect((X)
44c0: 2c 31 29 0a 23 20 64 65 66 69 6e 65 20 75 6e 6c  ,1).# define unl
44d0: 69 6b 65 6c 79 28 58 29 20 20 5f 5f 62 75 69 6c  ikely(X)  __buil
44e0: 74 69 6e 5f 65 78 70 65 63 74 28 28 58 29 2c 30  tin_expect((X),0
44f0: 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ).#else.# define
4500: 20 6c 69 6b 65 6c 79 28 58 29 20 20 20 20 21 21   likely(X)    !!
4510: 28 58 29 0a 23 20 64 65 66 69 6e 65 20 75 6e 6c  (X).# define unl
4520: 69 6b 65 6c 79 28 58 29 20 20 21 21 28 58 29 0a  ikely(X)  !!(X).
4530: 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  #endif../*******
4540: 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
4550: 73 71 6c 69 74 65 33 2e 68 20 69 6e 20 74 68 65  sqlite3.h in the
4560: 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74   middle of sqlit
4570: 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
4580: 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
4590: 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
45a0: 6c 65 20 73 71 6c 69 74 65 33 2e 68 20 2a 2a 2a  le sqlite3.h ***
45b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45d0: 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
45e0: 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
45f0: 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
4600: 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
4610: 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
4620: 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
4630: 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
4640: 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
4650: 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
4660: 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
4670: 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
4680: 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
4690: 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
46a0: 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
46b0: 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
46c0: 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
46d0: 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
46e0: 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
46f0: 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
4700: 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
4710: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4720: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4730: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4740: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
4750: 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69  * This header fi
4760: 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 69  le defines the i
4770: 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74 68  nterface that th
4780: 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79  e SQLite library
4790: 0a 2a 2a 20 70 72 65 73 65 6e 74 73 20 74 6f 20  .** presents to 
47a0: 63 6c 69 65 6e 74 20 70 72 6f 67 72 61 6d 73 2e  client programs.
47b0: 20 20 49 66 20 61 20 43 2d 66 75 6e 63 74 69 6f    If a C-functio
47c0: 6e 2c 20 73 74 72 75 63 74 75 72 65 2c 20 64 61  n, structure, da
47d0: 74 61 74 79 70 65 2c 0a 2a 2a 20 6f 72 20 63 6f  tatype,.** or co
47e0: 6e 73 74 61 6e 74 20 64 65 66 69 6e 69 74 69 6f  nstant definitio
47f0: 6e 20 64 6f 65 73 20 6e 6f 74 20 61 70 70 65 61  n does not appea
4800: 72 20 69 6e 20 74 68 69 73 20 66 69 6c 65 2c 20  r in this file, 
4810: 74 68 65 6e 20 69 74 20 69 73 0a 2a 2a 20 6e 6f  then it is.** no
4820: 74 20 61 20 70 75 62 6c 69 73 68 65 64 20 41 50  t a published AP
4830: 49 20 6f 66 20 53 51 4c 69 74 65 2c 20 69 73 20  I of SQLite, is 
4840: 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67  subject to chang
4850: 65 20 77 69 74 68 6f 75 74 0a 2a 2a 20 6e 6f 74  e without.** not
4860: 69 63 65 2c 20 61 6e 64 20 73 68 6f 75 6c 64 20  ice, and should 
4870: 6e 6f 74 20 62 65 20 72 65 66 65 72 65 6e 63 65  not be reference
4880: 64 20 62 79 20 70 72 6f 67 72 61 6d 73 20 74 68  d by programs th
4890: 61 74 20 75 73 65 20 53 51 4c 69 74 65 2e 0a 2a  at use SQLite..*
48a0: 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65  *.** Some of the
48b0: 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 74 68 61   definitions tha
48c0: 74 20 61 72 65 20 69 6e 20 74 68 69 73 20 66 69  t are in this fi
48d0: 6c 65 20 61 72 65 20 6d 61 72 6b 65 64 20 61 73  le are marked as
48e0: 0a 2a 2a 20 22 65 78 70 65 72 69 6d 65 6e 74 61  .** "experimenta
48f0: 6c 22 2e 20 20 45 78 70 65 72 69 6d 65 6e 74 61  l".  Experimenta
4900: 6c 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65  l interfaces are
4910: 20 6e 6f 72 6d 61 6c 6c 79 20 6e 65 77 0a 2a 2a   normally new.**
4920: 20 66 65 61 74 75 72 65 73 20 72 65 63 65 6e 74   features recent
4930: 6c 79 20 61 64 64 65 64 20 74 6f 20 53 51 4c 69  ly added to SQLi
4940: 74 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 61  te.  We do not a
4950: 6e 74 69 63 69 70 61 74 65 20 63 68 61 6e 67 65  nticipate change
4960: 73 0a 2a 2a 20 74 6f 20 65 78 70 65 72 69 6d 65  s.** to experime
4970: 6e 74 61 6c 20 69 6e 74 65 72 66 61 63 65 73 20  ntal interfaces 
4980: 62 75 74 20 72 65 73 65 72 76 65 20 74 68 65 20  but reserve the 
4990: 72 69 67 68 74 20 74 6f 20 6d 61 6b 65 20 6d 69  right to make mi
49a0: 6e 6f 72 20 63 68 61 6e 67 65 73 0a 2a 2a 20 69  nor changes.** i
49b0: 66 20 65 78 70 65 72 69 65 6e 63 65 20 66 72 6f  f experience fro
49c0: 6d 20 75 73 65 20 22 69 6e 20 74 68 65 20 77 69  m use "in the wi
49d0: 6c 64 22 20 73 75 67 67 65 73 74 20 73 75 63 68  ld" suggest such
49e0: 20 63 68 61 6e 67 65 73 20 61 72 65 20 70 72 75   changes are pru
49f0: 64 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  dent..**.** The 
4a00: 6f 66 66 69 63 69 61 6c 20 43 2d 6c 61 6e 67 75  official C-langu
4a10: 61 67 65 20 41 50 49 20 64 6f 63 75 6d 65 6e 74  age API document
4a20: 61 74 69 6f 6e 20 66 6f 72 20 53 51 4c 69 74 65  ation for SQLite
4a30: 20 69 73 20 64 65 72 69 76 65 64 0a 2a 2a 20 66   is derived.** f
4a40: 72 6f 6d 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20  rom comments in 
4a50: 74 68 69 73 20 66 69 6c 65 2e 20 20 54 68 69 73  this file.  This
4a60: 20 66 69 6c 65 20 69 73 20 74 68 65 20 61 75 74   file is the aut
4a70: 68 6f 72 69 74 61 74 69 76 65 20 73 6f 75 72 63  horitative sourc
4a80: 65 0a 2a 2a 20 6f 6e 20 68 6f 77 20 53 51 4c 69  e.** on how SQLi
4a90: 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 61 72  te interfaces ar
4aa0: 65 20 73 75 70 70 6f 73 65 20 74 6f 20 6f 70 65  e suppose to ope
4ab0: 72 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rate..**.** The 
4ac0: 6e 61 6d 65 20 6f 66 20 74 68 69 73 20 66 69 6c  name of this fil
4ad0: 65 20 75 6e 64 65 72 20 63 6f 6e 66 69 67 75 72  e under configur
4ae0: 61 74 69 6f 6e 20 6d 61 6e 61 67 65 6d 65 6e 74  ation management
4af0: 20 69 73 20 22 73 71 6c 69 74 65 2e 68 2e 69 6e   is "sqlite.h.in
4b00: 22 2e 0a 2a 2a 20 54 68 65 20 6d 61 6b 65 66 69  "..** The makefi
4b10: 6c 65 20 6d 61 6b 65 73 20 73 6f 6d 65 20 6d 69  le makes some mi
4b20: 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f 20 74  nor changes to t
4b30: 68 69 73 20 66 69 6c 65 20 28 73 75 63 68 20 61  his file (such a
4b40: 73 20 69 6e 73 65 72 74 69 6e 67 0a 2a 2a 20 74  s inserting.** t
4b50: 68 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65  he version numbe
4b60: 72 29 20 61 6e 64 20 63 68 61 6e 67 65 73 20 69  r) and changes i
4b70: 74 73 20 6e 61 6d 65 20 74 6f 20 22 73 71 6c 69  ts name to "sqli
4b80: 74 65 33 2e 68 22 20 61 73 0a 2a 2a 20 70 61 72  te3.h" as.** par
4b90: 74 20 6f 66 20 74 68 65 20 62 75 69 6c 64 20 70  t of the build p
4ba0: 72 6f 63 65 73 73 2e 0a 2a 2f 0a 23 69 66 6e 64  rocess..*/.#ifnd
4bb0: 65 66 20 5f 53 51 4c 49 54 45 33 5f 48 5f 0a 23  ef _SQLITE3_H_.#
4bc0: 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 33 5f  define _SQLITE3_
4bd0: 48 5f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64  H_.#include <std
4be0: 61 72 67 2e 68 3e 20 20 20 20 20 2f 2a 20 4e 65  arg.h>     /* Ne
4bf0: 65 64 65 64 20 66 6f 72 20 74 68 65 20 64 65 66  eded for the def
4c00: 69 6e 69 74 69 6f 6e 20 6f 66 20 76 61 5f 6c 69  inition of va_li
4c10: 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  st */../*.** Mak
4c20: 65 20 73 75 72 65 20 77 65 20 63 61 6e 20 63 61  e sure we can ca
4c30: 6c 6c 20 74 68 69 73 20 73 74 75 66 66 20 66 72  ll this stuff fr
4c40: 6f 6d 20 43 2b 2b 2e 0a 2a 2f 0a 23 69 66 20 30  om C++..*/.#if 0
4c50: 0a 65 78 74 65 72 6e 20 22 43 22 20 7b 0a 23 65  .extern "C" {.#e
4c60: 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  ndif.../*.** Add
4c70: 20 74 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20   the ability to 
4c80: 6f 76 65 72 72 69 64 65 20 27 65 78 74 65 72 6e  override 'extern
4c90: 27 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  '.*/.#ifndef SQL
4ca0: 49 54 45 5f 45 58 54 45 52 4e 0a 23 20 64 65 66  ITE_EXTERN.# def
4cb0: 69 6e 65 20 53 51 4c 49 54 45 5f 45 58 54 45 52  ine SQLITE_EXTER
4cc0: 4e 20 65 78 74 65 72 6e 0a 23 65 6e 64 69 66 0a  N extern.#endif.
4cd0: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
4ce0: 41 50 49 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  API.# define SQL
4cf0: 49 54 45 5f 41 50 49 0a 23 65 6e 64 69 66 0a 0a  ITE_API.#endif..
4d00: 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6e 6f 2d  ./*.** These no-
4d10: 6f 70 20 6d 61 63 72 6f 73 20 61 72 65 20 75 73  op macros are us
4d20: 65 64 20 69 6e 20 66 72 6f 6e 74 20 6f 66 20 69  ed in front of i
4d30: 6e 74 65 72 66 61 63 65 73 20 74 6f 20 6d 61 72  nterfaces to mar
4d40: 6b 20 74 68 6f 73 65 0a 2a 2a 20 69 6e 74 65 72  k those.** inter
4d50: 66 61 63 65 73 20 61 73 20 65 69 74 68 65 72 20  faces as either 
4d60: 64 65 70 72 65 63 61 74 65 64 20 6f 72 20 65 78  deprecated or ex
4d70: 70 65 72 69 6d 65 6e 74 61 6c 2e 20 20 4e 65 77  perimental.  New
4d80: 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a   applications.**
4d90: 20 73 68 6f 75 6c 64 20 6e 6f 74 20 75 73 65 20   should not use 
4da0: 64 65 70 72 65 63 61 74 65 64 20 69 6e 74 65 72  deprecated inter
4db0: 66 61 63 65 73 20 2d 20 74 68 65 79 20 61 72 65  faces - they are
4dc0: 20 73 75 70 70 6f 72 74 20 66 6f 72 20 62 61 63   support for bac
4dd0: 6b 77 61 72 64 73 0a 2a 2a 20 63 6f 6d 70 61 74  kwards.** compat
4de0: 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e 20 20 41  ibility only.  A
4df0: 70 70 6c 69 63 61 74 69 6f 6e 20 77 72 69 74 65  pplication write
4e00: 72 73 20 73 68 6f 75 6c 64 20 62 65 20 61 77 61  rs should be awa
4e10: 72 65 20 74 68 61 74 0a 2a 2a 20 65 78 70 65 72  re that.** exper
4e20: 69 6d 65 6e 74 61 6c 20 69 6e 74 65 72 66 61 63  imental interfac
4e30: 65 73 20 61 72 65 20 73 75 62 6a 65 63 74 20 74  es are subject t
4e40: 6f 20 63 68 61 6e 67 65 20 69 6e 20 70 6f 69 6e  o change in poin
4e50: 74 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a  t releases..**.*
4e60: 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 75  * These macros u
4e70: 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 65 20 74  sed to resolve t
4e80: 6f 20 76 61 72 69 6f 75 73 20 6b 69 6e 64 73 20  o various kinds 
4e90: 6f 66 20 63 6f 6d 70 69 6c 65 72 20 6d 61 67 69  of compiler magi
4ea0: 63 20 74 68 61 74 0a 2a 2a 20 77 6f 75 6c 64 20  c that.** would 
4eb0: 67 65 6e 65 72 61 74 65 20 77 61 72 6e 69 6e 67  generate warning
4ec0: 20 6d 65 73 73 61 67 65 73 20 77 68 65 6e 20 74   messages when t
4ed0: 68 65 79 20 77 65 72 65 20 75 73 65 64 2e 20 20  hey were used.  
4ee0: 42 75 74 20 74 68 61 74 0a 2a 2a 20 63 6f 6d 70  But that.** comp
4ef0: 69 6c 65 72 20 6d 61 67 69 63 20 65 6e 64 65 64  iler magic ended
4f00: 20 75 70 20 67 65 6e 65 72 61 74 69 6e 67 20 73   up generating s
4f10: 75 63 68 20 61 20 66 6c 75 72 72 79 20 6f 66 20  uch a flurry of 
4f20: 62 75 67 20 72 65 70 6f 72 74 73 0a 2a 2a 20 74  bug reports.** t
4f30: 68 61 74 20 77 65 20 68 61 76 65 20 74 61 6b 65  hat we have take
4f40: 6e 20 69 74 20 61 6c 6c 20 6f 75 74 20 61 6e 64  n it all out and
4f50: 20 67 6f 6e 65 20 62 61 63 6b 20 74 6f 20 75 73   gone back to us
4f60: 69 6e 67 20 73 69 6d 70 6c 65 0a 2a 2a 20 6e 6f  ing simple.** no
4f70: 6f 70 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 64  op macros..*/.#d
4f80: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 50  efine SQLITE_DEP
4f90: 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e 65 20  RECATED.#define 
4fa0: 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e  SQLITE_EXPERIMEN
4fb0: 54 41 4c 0a 0a 2f 2a 0a 2a 2a 20 45 6e 73 75 72  TAL../*.** Ensur
4fc0: 65 20 74 68 65 73 65 20 73 79 6d 62 6f 6c 73 20  e these symbols 
4fd0: 77 65 72 65 20 6e 6f 74 20 64 65 66 69 6e 65 64  were not defined
4fe0: 20 62 79 20 73 6f 6d 65 20 70 72 65 76 69 6f 75   by some previou
4ff0: 73 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a  s header file..*
5000: 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
5010: 56 45 52 53 49 4f 4e 0a 23 20 75 6e 64 65 66 20  VERSION.# undef 
5020: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 0a 23  SQLITE_VERSION.#
5030: 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
5040: 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42  ITE_VERSION_NUMB
5050: 45 52 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54  ER.# undef SQLIT
5060: 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52  E_VERSION_NUMBER
5070: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43  .#endif../*.** C
5080: 41 50 49 33 52 45 46 3a 20 43 6f 6d 70 69 6c 65  API3REF: Compile
5090: 2d 54 69 6d 65 20 4c 69 62 72 61 72 79 20 56 65  -Time Library Ve
50a0: 72 73 69 6f 6e 20 4e 75 6d 62 65 72 73 20 7b 48  rsion Numbers {H
50b0: 31 30 30 31 30 7d 20 3c 53 36 30 31 30 30 3e 0a  10010} <S60100>.
50c0: 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45  **.** The SQLITE
50d0: 5f 56 45 52 53 49 4f 4e 20 61 6e 64 20 53 51 4c  _VERSION and SQL
50e0: 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42  ITE_VERSION_NUMB
50f0: 45 52 20 23 64 65 66 69 6e 65 73 20 69 6e 0a 2a  ER #defines in.*
5100: 2a 20 74 68 65 20 73 71 6c 69 74 65 33 2e 68 20  * the sqlite3.h 
5110: 66 69 6c 65 20 73 70 65 63 69 66 79 20 74 68 65  file specify the
5120: 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69   version of SQLi
5130: 74 65 20 77 69 74 68 20 77 68 69 63 68 0a 2a 2a  te with which.**
5140: 20 74 68 61 74 20 68 65 61 64 65 72 20 66 69 6c   that header fil
5150: 65 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 2e  e is associated.
5160: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 76 65 72 73  .**.** The "vers
5170: 69 6f 6e 22 20 6f 66 20 53 51 4c 69 74 65 20 69  ion" of SQLite i
5180: 73 20 61 20 73 74 72 69 6e 67 20 6f 66 20 74 68  s a string of th
5190: 65 20 66 6f 72 6d 20 22 57 2e 58 2e 59 22 20 6f  e form "W.X.Y" o
51a0: 72 20 22 57 2e 58 2e 59 2e 5a 22 2e 0a 2a 2a 20  r "W.X.Y.Z"..** 
51b0: 54 68 65 20 57 20 76 61 6c 75 65 20 69 73 20 6d  The W value is m
51c0: 61 6a 6f 72 20 76 65 72 73 69 6f 6e 20 6e 75 6d  ajor version num
51d0: 62 65 72 20 61 6e 64 20 69 73 20 61 6c 77 61 79  ber and is alway
51e0: 73 20 33 20 69 6e 20 53 51 4c 69 74 65 33 2e 0a  s 3 in SQLite3..
51f0: 2a 2a 20 54 68 65 20 57 20 76 61 6c 75 65 20 6f  ** The W value o
5200: 6e 6c 79 20 63 68 61 6e 67 65 73 20 77 68 65 6e  nly changes when
5210: 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61   backwards compa
5220: 74 69 62 69 6c 69 74 79 20 69 73 0a 2a 2a 20 62  tibility is.** b
5230: 72 6f 6b 65 6e 20 61 6e 64 20 77 65 20 69 6e 74  roken and we int
5240: 65 6e 64 20 74 6f 20 6e 65 76 65 72 20 62 72 65  end to never bre
5250: 61 6b 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d  ak backwards com
5260: 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a 20 54  patibility..** T
5270: 68 65 20 58 20 76 61 6c 75 65 20 69 73 20 74 68  he X value is th
5280: 65 20 6d 69 6e 6f 72 20 76 65 72 73 69 6f 6e 20  e minor version 
5290: 6e 75 6d 62 65 72 20 61 6e 64 20 6f 6e 6c 79 20  number and only 
52a0: 63 68 61 6e 67 65 73 20 77 68 65 6e 0a 2a 2a 20  changes when.** 
52b0: 74 68 65 72 65 20 61 72 65 20 6d 61 6a 6f 72 20  there are major 
52c0: 66 65 61 74 75 72 65 20 65 6e 68 61 6e 63 65 6d  feature enhancem
52d0: 65 6e 74 73 20 74 68 61 74 20 61 72 65 20 66 6f  ents that are fo
52e0: 72 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 6c  rwards compatibl
52f0: 65 0a 2a 2a 20 62 75 74 20 6e 6f 74 20 62 61 63  e.** but not bac
5300: 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 6c  kwards compatibl
5310: 65 2e 0a 2a 2a 20 54 68 65 20 59 20 76 61 6c 75  e..** The Y valu
5320: 65 20 69 73 20 74 68 65 20 72 65 6c 65 61 73 65  e is the release
5330: 20 6e 75 6d 62 65 72 20 61 6e 64 20 69 73 20 69   number and is i
5340: 6e 63 72 65 6d 65 6e 74 65 64 20 77 69 74 68 0a  ncremented with.
5350: 2a 2a 20 65 61 63 68 20 72 65 6c 65 61 73 65 20  ** each release 
5360: 62 75 74 20 72 65 73 65 74 73 20 62 61 63 6b 20  but resets back 
5370: 74 6f 20 30 20 77 68 65 6e 65 76 65 72 20 58 20  to 0 whenever X 
5380: 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 0a  is incremented..
5390: 2a 2a 20 54 68 65 20 5a 20 76 61 6c 75 65 20 6f  ** The Z value o
53a0: 6e 6c 79 20 61 70 70 65 61 72 73 20 6f 6e 20 62  nly appears on b
53b0: 72 61 6e 63 68 20 72 65 6c 65 61 73 65 73 2e 0a  ranch releases..
53c0: 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45  **.** The SQLITE
53d0: 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20  _VERSION_NUMBER 
53e0: 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68  is an integer th
53f0: 61 74 20 69 73 20 63 6f 6d 70 75 74 65 64 20 61  at is computed a
5400: 73 0a 2a 2a 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  s.** follows:.**
5410: 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e  .** <blockquote>
5420: 3c 70 72 65 3e 0a 2a 2a 20 53 51 4c 49 54 45 5f  <pre>.** SQLITE_
5430: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 3d  VERSION_NUMBER =
5440: 20 57 2a 31 30 30 30 30 30 30 20 2b 20 58 2a 31   W*1000000 + X*1
5450: 30 30 30 20 2b 20 59 0a 2a 2a 20 3c 2f 70 72 65  000 + Y.** </pre
5460: 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a  ></blockquote>.*
5470: 2a 0a 2a 2a 20 53 69 6e 63 65 20 76 65 72 73 69  *.** Since versi
5480: 6f 6e 20 33 2e 36 2e 31 38 2c 20 53 51 4c 69 74  on 3.6.18, SQLit
5490: 65 20 73 6f 75 72 63 65 20 63 6f 64 65 20 68 61  e source code ha
54a0: 73 20 62 65 65 6e 20 73 74 6f 72 65 64 20 69 6e  s been stored in
54b0: 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65 66 3d   the.** <a href=
54c0: 22 68 74 74 70 3a 2f 2f 77 77 77 2e 66 6f 73 73  "http://www.foss
54d0: 69 6c 2d 73 63 6d 2e 6f 72 67 2f 22 3e 66 6f 73  il-scm.org/">fos
54e0: 73 69 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  sil configuratio
54f0: 6e 20 6d 61 6e 61 67 65 6d 65 6e 74 0a 2a 2a 20  n management.** 
5500: 73 79 73 74 65 6d 3c 2f 61 3e 2e 20 20 54 68 65  system</a>.  The
5510: 20 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49   SQLITE_SOURCE_I
5520: 44 0a 2a 2a 20 6d 61 63 72 6f 20 69 73 20 61 20  D.** macro is a 
5530: 73 74 72 69 6e 67 20 77 68 69 63 68 20 69 64 65  string which ide
5540: 6e 74 69 66 69 65 73 20 61 20 70 61 72 74 69 63  ntifies a partic
5550: 75 6c 61 72 20 63 68 65 63 6b 2d 69 6e 20 6f 66  ular check-in of
5560: 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 74 68 69   SQLite.** withi
5570: 6e 20 69 74 73 20 63 6f 6e 66 69 67 75 72 61 74  n its configurat
5580: 69 6f 6e 20 6d 61 6e 61 67 65 6d 65 6e 74 20 73  ion management s
5590: 79 73 74 65 6d 2e 20 20 54 68 65 20 73 74 72 69  ystem.  The stri
55a0: 6e 67 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a  ng contains the.
55b0: 2a 2a 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65  ** date and time
55c0: 20 6f 66 20 74 68 65 20 63 68 65 63 6b 2d 69 6e   of the check-in
55d0: 20 28 55 54 43 29 20 61 6e 64 20 61 6e 20 53 48   (UTC) and an SH
55e0: 41 31 20 68 61 73 68 20 6f 66 20 74 68 65 20 65  A1 hash of the e
55f0: 6e 74 69 72 65 0a 2a 2a 20 73 6f 75 72 63 65 20  ntire.** source 
5600: 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  tree..**.** See 
5610: 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 6c  also: [sqlite3_l
5620: 69 62 76 65 72 73 69 6f 6e 28 29 5d 2c 0a 2a 2a  ibversion()],.**
5630: 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72   [sqlite3_libver
5640: 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 5d 2c 20  sion_number()], 
5650: 5b 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69  [sqlite3_sourcei
5660: 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65  d()],.** [sqlite
5670: 5f 76 65 72 73 69 6f 6e 28 29 5d 20 61 6e 64 20  _version()] and 
5680: 5b 73 71 6c 69 74 65 5f 73 6f 75 72 63 65 5f 69  [sqlite_source_i
5690: 64 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  d()]..**.** Requ
56a0: 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 30 31  irements: [H1001
56b0: 31 5d 20 5b 48 31 30 30 31 34 5d 0a 2a 2f 0a 23  1] [H10014].*/.#
56c0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 45  define SQLITE_VE
56d0: 52 53 49 4f 4e 20 20 20 20 20 20 20 20 22 33 2e  RSION        "3.
56e0: 36 2e 31 38 22 0a 23 64 65 66 69 6e 65 20 53 51  6.18".#define SQ
56f0: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d  LITE_VERSION_NUM
5700: 42 45 52 20 33 30 30 36 30 31 38 0a 23 64 65 66  BER 3006018.#def
5710: 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f 55 52 43  ine SQLITE_SOURC
5720: 45 5f 49 44 20 20 20 20 20 20 22 32 30 30 39 2d  E_ID      "2009-
5730: 30 39 2d 30 39 20 31 36 3a 31 30 3a 35 31 20 66  09-09 16:10:51 f
5740: 30 63 37 32 61 35 33 63 35 64 35 37 64 37 34 38  0c72a53c5d57d748
5750: 37 62 34 38 61 30 36 61 34 30 38 31 36 31 35 33  7b48a06a40816153
5760: 66 34 37 61 61 61 63 22 0a 0a 2f 2a 0a 2a 2a 20  f47aaac"../*.** 
5770: 43 41 50 49 33 52 45 46 3a 20 52 75 6e 2d 54 69  CAPI3REF: Run-Ti
5780: 6d 65 20 4c 69 62 72 61 72 79 20 56 65 72 73 69  me Library Versi
5790: 6f 6e 20 4e 75 6d 62 65 72 73 20 7b 48 31 30 30  on Numbers {H100
57a0: 32 30 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 20  20} <S60100>.** 
57b0: 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65  KEYWORDS: sqlite
57c0: 33 5f 76 65 72 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20  3_version.**.** 
57d0: 54 68 65 73 65 20 69 6e 74 65 72 66 61 63 65 73  These interfaces
57e0: 20 70 72 6f 76 69 64 65 20 74 68 65 20 73 61 6d   provide the sam
57f0: 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73  e information as
5800: 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 56 45 52   the [SQLITE_VER
5810: 53 49 4f 4e 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54  SION],.** [SQLIT
5820: 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52  E_VERSION_NUMBER
5830: 5d 2c 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 53  ], and [SQLITE_S
5840: 4f 55 52 43 45 5f 49 44 5d 20 23 64 65 66 69 6e  OURCE_ID] #defin
5850: 65 73 20 69 6e 20 74 68 65 20 68 65 61 64 65 72  es in the header
5860: 2c 0a 2a 2a 20 62 75 74 20 61 72 65 20 61 73 73  ,.** but are ass
5870: 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
5880: 20 6c 69 62 72 61 72 79 20 69 6e 73 74 65 61 64   library instead
5890: 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 20 66   of the header f
58a0: 69 6c 65 2e 20 20 43 61 75 74 69 6f 75 73 0a 2a  ile.  Cautious.*
58b0: 2a 20 70 72 6f 67 72 61 6d 6d 65 72 73 20 6d 69  * programmers mi
58c0: 67 68 74 20 69 6e 63 6c 75 64 65 20 61 73 73 65  ght include asse
58d0: 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20  rt() statements 
58e0: 69 6e 20 74 68 65 69 72 20 61 70 70 6c 69 63 61  in their applica
58f0: 74 69 6f 6e 20 74 6f 0a 2a 2a 20 76 65 72 69 66  tion to.** verif
5900: 79 20 74 68 61 74 20 76 61 6c 75 65 73 20 72 65  y that values re
5910: 74 75 72 6e 65 64 20 62 79 20 74 68 65 73 65 20  turned by these 
5920: 69 6e 74 65 72 66 61 63 65 73 20 6d 61 74 63 68  interfaces match
5930: 20 74 68 65 20 6d 61 63 72 6f 73 20 69 6e 0a 2a   the macros in.*
5940: 2a 20 74 68 65 20 68 65 61 64 65 72 2c 20 61 6e  * the header, an
5950: 64 20 74 68 75 73 20 69 6e 73 75 72 65 20 74 68  d thus insure th
5960: 61 74 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  at the applicati
5970: 6f 6e 20 69 73 0a 2a 2a 20 63 6f 6d 70 69 6c 65  on is.** compile
5980: 64 20 77 69 74 68 20 6d 61 74 63 68 69 6e 67 20  d with matching 
5990: 6c 69 62 72 61 72 79 20 61 6e 64 20 68 65 61 64  library and head
59a0: 65 72 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  er files..**.** 
59b0: 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65  <blockquote><pre
59c0: 3e 0a 2a 2a 20 61 73 73 65 72 74 28 20 73 71 6c  >.** assert( sql
59d0: 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f  ite3_libversion_
59e0: 6e 75 6d 62 65 72 28 29 3d 3d 53 51 4c 49 54 45  number()==SQLITE
59f0: 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20  _VERSION_NUMBER 
5a00: 29 3b 0a 2a 2a 20 61 73 73 65 72 74 28 20 73 74  );.** assert( st
5a10: 72 63 6d 70 28 73 71 6c 69 74 65 33 5f 73 6f 75  rcmp(sqlite3_sou
5a20: 72 63 65 69 64 28 29 2c 53 51 4c 49 54 45 5f 53  rceid(),SQLITE_S
5a30: 4f 55 52 43 45 5f 49 44 29 3d 3d 30 20 29 3b 0a  OURCE_ID)==0 );.
5a40: 2a 2a 20 61 73 73 65 72 74 28 20 73 74 72 63 6d  ** assert( strcm
5a50: 70 28 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72  p(sqlite3_libver
5a60: 73 69 6f 6e 2c 53 51 4c 49 54 45 5f 56 45 52 53  sion,SQLITE_VERS
5a70: 49 4f 4e 29 3d 3d 30 20 29 3b 0a 2a 2a 20 3c 2f  ION)==0 );.** </
5a80: 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65  pre></blockquote
5a90: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  >.**.** The sqli
5aa0: 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 29  te3_libversion()
5ab0: 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
5ac0: 73 20 74 68 65 20 73 61 6d 65 20 69 6e 66 6f 72  s the same infor
5ad0: 6d 61 74 69 6f 6e 20 61 73 20 69 73 0a 2a 2a 20  mation as is.** 
5ae0: 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  in the sqlite3_v
5af0: 65 72 73 69 6f 6e 5b 5d 20 73 74 72 69 6e 67 20  ersion[] string 
5b00: 63 6f 6e 73 74 61 6e 74 2e 20 20 54 68 65 20 66  constant.  The f
5b10: 75 6e 63 74 69 6f 6e 20 69 73 20 70 72 6f 76 69  unction is provi
5b20: 64 65 64 0a 2a 2a 20 66 6f 72 20 75 73 65 20 69  ded.** for use i
5b30: 6e 20 44 4c 4c 73 20 73 69 6e 63 65 20 44 4c 4c  n DLLs since DLL
5b40: 20 75 73 65 72 73 20 75 73 75 61 6c 6c 79 20 64   users usually d
5b50: 6f 20 6e 6f 74 20 68 61 76 65 20 64 69 72 65 63  o not have direc
5b60: 74 20 61 63 63 65 73 73 20 74 6f 20 73 74 72 69  t access to stri
5b70: 6e 67 0a 2a 2a 20 63 6f 6e 73 74 61 6e 74 73 20  ng.** constants 
5b80: 77 69 74 68 69 6e 20 74 68 65 20 44 4c 4c 2e 20  within the DLL. 
5b90: 20 53 69 6d 69 6c 61 72 6c 79 2c 20 74 68 65 20   Similarly, the 
5ba0: 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64  sqlite3_sourceid
5bb0: 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 72  () function.** r
5bc0: 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20  eturns the same 
5bd0: 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 20 69  information as i
5be0: 73 20 69 6e 20 74 68 65 20 5b 53 51 4c 49 54 45  s in the [SQLITE
5bf0: 5f 53 4f 55 52 43 45 5f 49 44 5d 20 23 64 65 66  _SOURCE_ID] #def
5c00: 69 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 68 65  ine of.** the he
5c10: 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  ader file..**.**
5c20: 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69   See also: [sqli
5c30: 74 65 5f 76 65 72 73 69 6f 6e 28 29 5d 20 61 6e  te_version()] an
5c40: 64 20 5b 73 71 6c 69 74 65 5f 73 6f 75 72 63 65  d [sqlite_source
5c50: 5f 69 64 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65  _id()]..**.** Re
5c60: 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30  quirements: [H10
5c70: 30 32 31 5d 20 5b 48 31 30 30 32 32 5d 20 5b 48  021] [H10022] [H
5c80: 31 30 30 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45  10023].*/.SQLITE
5c90: 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
5ca0: 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b  sqlite3_version[
5cb0: 5d 20 3d 20 53 51 4c 49 54 45 5f 56 45 52 53 49  ] = SQLITE_VERSI
5cc0: 4f 4e 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  ON;.SQLITE_API c
5cd0: 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
5ce0: 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 76 6f  e3_libversion(vo
5cf0: 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  id);.SQLITE_API 
5d00: 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
5d10: 74 65 33 5f 73 6f 75 72 63 65 69 64 28 76 6f 69  te3_sourceid(voi
5d20: 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  d);.SQLITE_API i
5d30: 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65  nt sqlite3_libve
5d40: 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 76 6f 69  rsion_number(voi
5d50: 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  d);../*.** CAPI3
5d60: 52 45 46 3a 20 54 65 73 74 20 54 6f 20 53 65 65  REF: Test To See
5d70: 20 49 66 20 54 68 65 20 4c 69 62 72 61 72 79 20   If The Library 
5d80: 49 73 20 54 68 72 65 61 64 73 61 66 65 20 7b 48  Is Threadsafe {H
5d90: 31 30 31 30 30 7d 20 3c 53 36 30 31 30 30 3e 0a  10100} <S60100>.
5da0: 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 61 6e  **.** SQLite can
5db0: 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74   be compiled wit
5dc0: 68 20 6f 72 20 77 69 74 68 6f 75 74 20 6d 75 74  h or without mut
5dd0: 65 78 65 73 2e 20 20 57 68 65 6e 0a 2a 2a 20 74  exes.  When.** t
5de0: 68 65 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41  he [SQLITE_THREA
5df0: 44 53 41 46 45 5d 20 43 20 70 72 65 70 72 6f 63  DSAFE] C preproc
5e00: 65 73 73 6f 72 20 6d 61 63 72 6f 20 69 73 20 31  essor macro is 1
5e10: 20 6f 72 20 32 2c 20 6d 75 74 65 78 65 73 0a 2a   or 2, mutexes.*
5e20: 2a 20 61 72 65 20 65 6e 61 62 6c 65 64 20 61 6e  * are enabled an
5e30: 64 20 53 51 4c 69 74 65 20 69 73 20 74 68 72 65  d SQLite is thre
5e40: 61 64 73 61 66 65 2e 20 20 57 68 65 6e 20 74 68  adsafe.  When th
5e50: 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 54 48 52  e.** [SQLITE_THR
5e60: 45 41 44 53 41 46 45 5d 20 6d 61 63 72 6f 20 69  EADSAFE] macro i
5e70: 73 20 30 2c 20 0a 2a 2a 20 74 68 65 20 6d 75 74  s 0, .** the mut
5e80: 65 78 65 73 20 61 72 65 20 6f 6d 69 74 74 65 64  exes are omitted
5e90: 2e 20 20 57 69 74 68 6f 75 74 20 74 68 65 20 6d  .  Without the m
5ea0: 75 74 65 78 65 73 2c 20 69 74 20 69 73 20 6e 6f  utexes, it is no
5eb0: 74 20 73 61 66 65 0a 2a 2a 20 74 6f 20 75 73 65  t safe.** to use
5ec0: 20 53 51 4c 69 74 65 20 63 6f 6e 63 75 72 72 65   SQLite concurre
5ed0: 6e 74 6c 79 20 66 72 6f 6d 20 6d 6f 72 65 20 74  ntly from more t
5ee0: 68 61 6e 20 6f 6e 65 20 74 68 72 65 61 64 2e 0a  han one thread..
5ef0: 2a 2a 0a 2a 2a 20 45 6e 61 62 6c 69 6e 67 20 6d  **.** Enabling m
5f00: 75 74 65 78 65 73 20 69 6e 63 75 72 73 20 61 20  utexes incurs a 
5f10: 6d 65 61 73 75 72 61 62 6c 65 20 70 65 72 66 6f  measurable perfo
5f20: 72 6d 61 6e 63 65 20 70 65 6e 61 6c 74 79 2e 0a  rmance penalty..
5f30: 2a 2a 20 53 6f 20 69 66 20 73 70 65 65 64 20 69  ** So if speed i
5f40: 73 20 6f 66 20 75 74 6d 6f 73 74 20 69 6d 70 6f  s of utmost impo
5f50: 72 74 61 6e 63 65 2c 20 69 74 20 6d 61 6b 65 73  rtance, it makes
5f60: 20 73 65 6e 73 65 20 74 6f 20 64 69 73 61 62 6c   sense to disabl
5f70: 65 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 65 73  e.** the mutexes
5f80: 2e 20 20 42 75 74 20 66 6f 72 20 6d 61 78 69 6d  .  But for maxim
5f90: 75 6d 20 73 61 66 65 74 79 2c 20 6d 75 74 65 78  um safety, mutex
5fa0: 65 73 20 73 68 6f 75 6c 64 20 62 65 20 65 6e 61  es should be ena
5fb0: 62 6c 65 64 2e 0a 2a 2a 20 54 68 65 20 64 65 66  bled..** The def
5fc0: 61 75 6c 74 20 62 65 68 61 76 69 6f 72 20 69 73  ault behavior is
5fd0: 20 66 6f 72 20 6d 75 74 65 78 65 73 20 74 6f 20   for mutexes to 
5fe0: 62 65 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a  be enabled..**.*
5ff0: 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65  * This interface
6000: 20 63 61 6e 20 62 65 20 75 73 65 64 20 62 79 20   can be used by 
6010: 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74  an application t
6020: 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74  o make sure that
6030: 20 74 68 65 0a 2a 2a 20 76 65 72 73 69 6f 6e 20   the.** version 
6040: 6f 66 20 53 51 4c 69 74 65 20 74 68 61 74 20 69  of SQLite that i
6050: 74 20 69 73 20 6c 69 6e 6b 69 6e 67 20 61 67 61  t is linking aga
6060: 69 6e 73 74 20 77 61 73 20 63 6f 6d 70 69 6c 65  inst was compile
6070: 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 64 65  d with.** the de
6080: 73 69 72 65 64 20 73 65 74 74 69 6e 67 20 6f 66  sired setting of
6090: 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 48 52   the [SQLITE_THR
60a0: 45 41 44 53 41 46 45 5d 20 6d 61 63 72 6f 2e 0a  EADSAFE] macro..
60b0: 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72  **.** This inter
60c0: 66 61 63 65 20 6f 6e 6c 79 20 72 65 70 6f 72 74  face only report
60d0: 73 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65  s on the compile
60e0: 2d 74 69 6d 65 20 6d 75 74 65 78 20 73 65 74 74  -time mutex sett
60f0: 69 6e 67 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 53  ing.** of the [S
6100: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
6110: 5d 20 66 6c 61 67 2e 20 20 49 66 20 53 51 4c 69  ] flag.  If SQLi
6120: 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77  te is compiled w
6130: 69 74 68 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 48  ith.** SQLITE_TH
6140: 52 45 41 44 53 41 46 45 3d 31 20 74 68 65 6e 20  READSAFE=1 then 
6150: 6d 75 74 65 78 65 73 20 61 72 65 20 65 6e 61 62  mutexes are enab
6160: 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 20 62  led by default b
6170: 75 74 0a 2a 2a 20 63 61 6e 20 62 65 20 66 75 6c  ut.** can be ful
6180: 6c 79 20 6f 72 20 70 61 72 74 69 61 6c 6c 79 20  ly or partially 
6190: 64 69 73 61 62 6c 65 64 20 75 73 69 6e 67 20 61  disabled using a
61a0: 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
61b0: 33 5f 63 6f 6e 66 69 67 28 29 5d 0a 2a 2a 20 77  3_config()].** w
61c0: 69 74 68 20 74 68 65 20 76 65 72 62 73 20 5b 53  ith the verbs [S
61d0: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e  QLITE_CONFIG_SIN
61e0: 47 4c 45 54 48 52 45 41 44 5d 2c 20 5b 53 51 4c  GLETHREAD], [SQL
61f0: 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49  ITE_CONFIG_MULTI
6200: 54 48 52 45 41 44 5d 2c 0a 2a 2a 20 6f 72 20 5b  THREAD],.** or [
6210: 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55  SQLITE_CONFIG_MU
6220: 54 45 58 5d 2e 20 20 54 68 65 20 72 65 74 75 72  TEX].  The retur
6230: 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20  n value of this 
6240: 66 75 6e 63 74 69 6f 6e 20 73 68 6f 77 73 0a 2a  function shows.*
6250: 2a 20 6f 6e 6c 79 20 74 68 65 20 64 65 66 61 75  * only the defau
6260: 6c 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20  lt compile-time 
6270: 73 65 74 74 69 6e 67 2c 20 6e 6f 74 20 61 6e 79  setting, not any
6280: 20 72 75 6e 2d 74 69 6d 65 20 63 68 61 6e 67 65   run-time change
6290: 73 0a 2a 2a 20 74 6f 20 74 68 61 74 20 73 65 74  s.** to that set
62a0: 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  ting..**.** See 
62b0: 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d  the [threading m
62c0: 6f 64 65 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69  ode] documentati
62d0: 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  on for additiona
62e0: 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  l information..*
62f0: 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
6300: 73 3a 20 5b 48 31 30 31 30 31 5d 20 5b 48 31 30  s: [H10101] [H10
6310: 31 30 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  102].*/.SQLITE_A
6320: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74  PI int sqlite3_t
6330: 68 72 65 61 64 73 61 66 65 28 76 6f 69 64 29 3b  hreadsafe(void);
6340: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
6350: 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65  : Database Conne
6360: 63 74 69 6f 6e 20 48 61 6e 64 6c 65 20 7b 48 31  ction Handle {H1
6370: 32 30 30 30 7d 20 3c 53 34 30 32 30 30 3e 0a 2a  2000} <S40200>.*
6380: 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 64 61 74  * KEYWORDS: {dat
6390: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
63a0: 7d 20 7b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  } {database conn
63b0: 65 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 45  ections}.**.** E
63c0: 61 63 68 20 6f 70 65 6e 20 53 51 4c 69 74 65 20  ach open SQLite 
63d0: 64 61 74 61 62 61 73 65 20 69 73 20 72 65 70 72  database is repr
63e0: 65 73 65 6e 74 65 64 20 62 79 20 61 20 70 6f 69  esented by a poi
63f0: 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61  nter to an insta
6400: 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6f 70  nce of.** the op
6410: 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20 6e  aque structure n
6420: 61 6d 65 64 20 22 73 71 6c 69 74 65 33 22 2e 20  amed "sqlite3". 
6430: 20 49 74 20 69 73 20 75 73 65 66 75 6c 20 74 6f   It is useful to
6440: 20 74 68 69 6e 6b 20 6f 66 20 61 6e 20 73 71 6c   think of an sql
6450: 69 74 65 33 0a 2a 2a 20 70 6f 69 6e 74 65 72 20  ite3.** pointer 
6460: 61 73 20 61 6e 20 6f 62 6a 65 63 74 2e 20 20 54  as an object.  T
6470: 68 65 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  he [sqlite3_open
6480: 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70  ()], [sqlite3_op
6490: 65 6e 31 36 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20  en16()], and.** 
64a0: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32  [sqlite3_open_v2
64b0: 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 61  ()] interfaces a
64c0: 72 65 20 69 74 73 20 63 6f 6e 73 74 72 75 63 74  re its construct
64d0: 6f 72 73 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65  ors, and [sqlite
64e0: 33 5f 63 6c 6f 73 65 28 29 5d 0a 2a 2a 20 69 73  3_close()].** is
64f0: 20 69 74 73 20 64 65 73 74 72 75 63 74 6f 72 2e   its destructor.
6500: 20 20 54 68 65 72 65 20 61 72 65 20 6d 61 6e 79    There are many
6510: 20 6f 74 68 65 72 20 69 6e 74 65 72 66 61 63 65   other interface
6520: 73 20 28 73 75 63 68 20 61 73 0a 2a 2a 20 5b 73  s (such as.** [s
6530: 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
6540: 32 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63  2()], [sqlite3_c
6550: 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29  reate_function()
6560: 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74  ], and.** [sqlit
6570: 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28  e3_busy_timeout(
6580: 29 5d 20 74 6f 20 6e 61 6d 65 20 62 75 74 20 74  )] to name but t
6590: 68 72 65 65 29 20 74 68 61 74 20 61 72 65 20 6d  hree) that are m
65a0: 65 74 68 6f 64 73 20 6f 6e 20 61 6e 0a 2a 2a 20  ethods on an.** 
65b0: 73 71 6c 69 74 65 33 20 6f 62 6a 65 63 74 2e 0a  sqlite3 object..
65c0: 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
65d0: 74 20 73 71 6c 69 74 65 33 20 73 71 6c 69 74 65  t sqlite3 sqlite
65e0: 33 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  3;../*.** CAPI3R
65f0: 45 46 3a 20 36 34 2d 42 69 74 20 49 6e 74 65 67  EF: 64-Bit Integ
6600: 65 72 20 54 79 70 65 73 20 7b 48 31 30 32 30 30  er Types {H10200
6610: 7d 20 3c 53 31 30 31 31 30 3e 0a 2a 2a 20 4b 45  } <S10110>.** KE
6620: 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 5f 69  YWORDS: sqlite_i
6630: 6e 74 36 34 20 73 71 6c 69 74 65 5f 75 69 6e 74  nt64 sqlite_uint
6640: 36 34 0a 2a 2a 0a 2a 2a 20 42 65 63 61 75 73 65  64.**.** Because
6650: 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 72 6f   there is no cro
6660: 73 73 2d 70 6c 61 74 66 6f 72 6d 20 77 61 79 20  ss-platform way 
6670: 74 6f 20 73 70 65 63 69 66 79 20 36 34 2d 62 69  to specify 64-bi
6680: 74 20 69 6e 74 65 67 65 72 20 74 79 70 65 73 0a  t integer types.
6690: 2a 2a 20 53 51 4c 69 74 65 20 69 6e 63 6c 75 64  ** SQLite includ
66a0: 65 73 20 74 79 70 65 64 65 66 73 20 66 6f 72 20  es typedefs for 
66b0: 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 61 6e  64-bit signed an
66c0: 64 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67  d unsigned integ
66d0: 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ers..**.** The s
66e0: 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 61 6e 64  qlite3_int64 and
66f0: 20 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 20   sqlite3_uint64 
6700: 61 72 65 20 74 68 65 20 70 72 65 66 65 72 72 65  are the preferre
6710: 64 20 74 79 70 65 20 64 65 66 69 6e 69 74 69 6f  d type definitio
6720: 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ns..** The sqlit
6730: 65 5f 69 6e 74 36 34 20 61 6e 64 20 73 71 6c 69  e_int64 and sqli
6740: 74 65 5f 75 69 6e 74 36 34 20 74 79 70 65 73 20  te_uint64 types 
6750: 61 72 65 20 73 75 70 70 6f 72 74 65 64 20 66 6f  are supported fo
6760: 72 20 62 61 63 6b 77 61 72 64 73 0a 2a 2a 20 63  r backwards.** c
6770: 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 6f 6e 6c  ompatibility onl
6780: 79 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  y..**.** Require
6790: 6d 65 6e 74 73 3a 20 5b 48 31 30 32 30 31 5d 20  ments: [H10201] 
67a0: 5b 48 31 30 32 30 32 5d 0a 2a 2f 0a 23 69 66 64  [H10202].*/.#ifd
67b0: 65 66 20 53 51 4c 49 54 45 5f 49 4e 54 36 34 5f  ef SQLITE_INT64_
67c0: 54 59 50 45 0a 20 20 74 79 70 65 64 65 66 20 53  TYPE.  typedef S
67d0: 51 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45  QLITE_INT64_TYPE
67e0: 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20   sqlite_int64;. 
67f0: 20 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65   typedef unsigne
6800: 64 20 53 51 4c 49 54 45 5f 49 4e 54 36 34 5f 54  d SQLITE_INT64_T
6810: 59 50 45 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  YPE sqlite_uint6
6820: 34 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64  4;.#elif defined
6830: 28 5f 4d 53 43 5f 56 45 52 29 20 7c 7c 20 64 65  (_MSC_VER) || de
6840: 66 69 6e 65 64 28 5f 5f 42 4f 52 4c 41 4e 44 43  fined(__BORLANDC
6850: 5f 5f 29 0a 20 20 74 79 70 65 64 65 66 20 5f 5f  __).  typedef __
6860: 69 6e 74 36 34 20 73 71 6c 69 74 65 5f 69 6e 74  int64 sqlite_int
6870: 36 34 3b 0a 20 20 74 79 70 65 64 65 66 20 75 6e  64;.  typedef un
6880: 73 69 67 6e 65 64 20 5f 5f 69 6e 74 36 34 20 73  signed __int64 s
6890: 71 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65  qlite_uint64;.#e
68a0: 6c 73 65 0a 20 20 74 79 70 65 64 65 66 20 6c 6f  lse.  typedef lo
68b0: 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 73 71 6c 69  ng long int sqli
68c0: 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70 65  te_int64;.  type
68d0: 64 65 66 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  def unsigned lon
68e0: 67 20 6c 6f 6e 67 20 69 6e 74 20 73 71 6c 69 74  g long int sqlit
68f0: 65 5f 75 69 6e 74 36 34 3b 0a 23 65 6e 64 69 66  e_uint64;.#endif
6900: 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 5f  .typedef sqlite_
6910: 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 69 6e  int64 sqlite3_in
6920: 74 36 34 3b 0a 74 79 70 65 64 65 66 20 73 71 6c  t64;.typedef sql
6930: 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74  ite_uint64 sqlit
6940: 65 33 5f 75 69 6e 74 36 34 3b 0a 0a 2f 2a 0a 2a  e3_uint64;../*.*
6950: 2a 20 49 66 20 63 6f 6d 70 69 6c 69 6e 67 20 66  * If compiling f
6960: 6f 72 20 61 20 70 72 6f 63 65 73 73 6f 72 20 74  or a processor t
6970: 68 61 74 20 6c 61 63 6b 73 20 66 6c 6f 61 74 69  hat lacks floati
6980: 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 6f 72 74  ng point support
6990: 2c 0a 2a 2a 20 73 75 62 73 74 69 74 75 74 65 20  ,.** substitute 
69a0: 69 6e 74 65 67 65 72 20 66 6f 72 20 66 6c 6f 61  integer for floa
69b0: 74 69 6e 67 2d 70 6f 69 6e 74 2e 0a 2a 2f 0a 23  ting-point..*/.#
69c0: 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
69d0: 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
69e0: 0a 23 20 64 65 66 69 6e 65 20 64 6f 75 62 6c 65  .# define double
69f0: 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 0a 23   sqlite3_int64.#
6a00: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  endif../*.** CAP
6a10: 49 33 52 45 46 3a 20 43 6c 6f 73 69 6e 67 20 41  I3REF: Closing A
6a20: 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63   Database Connec
6a30: 74 69 6f 6e 20 7b 48 31 32 30 31 30 7d 20 3c 53  tion {H12010} <S
6a40: 33 30 31 30 30 3e 3c 53 34 30 32 30 30 3e 0a 2a  30100><S40200>.*
6a50: 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
6a60: 65 20 69 73 20 74 68 65 20 64 65 73 74 72 75 63  e is the destruc
6a70: 74 6f 72 20 66 6f 72 20 74 68 65 20 5b 73 71 6c  tor for the [sql
6a80: 69 74 65 33 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a  ite3] object..**
6a90: 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  .** Applications
6aa0: 20 73 68 6f 75 6c 64 20 5b 73 71 6c 69 74 65 33   should [sqlite3
6ab0: 5f 66 69 6e 61 6c 69 7a 65 20 7c 20 66 69 6e 61  _finalize | fina
6ac0: 6c 69 7a 65 5d 20 61 6c 6c 20 5b 70 72 65 70 61  lize] all [prepa
6ad0: 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 0a  red statements].
6ae0: 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  ** and [sqlite3_
6af0: 62 6c 6f 62 5f 63 6c 6f 73 65 20 7c 20 63 6c 6f  blob_close | clo
6b00: 73 65 5d 20 61 6c 6c 20 5b 42 4c 4f 42 20 68 61  se] all [BLOB ha
6b10: 6e 64 6c 65 73 5d 20 61 73 73 6f 63 69 61 74 65  ndles] associate
6b20: 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 5b 73  d with.** the [s
6b30: 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74 20 70  qlite3] object p
6b40: 72 69 6f 72 20 74 6f 20 61 74 74 65 6d 70 74 69  rior to attempti
6b50: 6e 67 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20  ng to close the 
6b60: 6f 62 6a 65 63 74 2e 0a 2a 2a 20 54 68 65 20 5b  object..** The [
6b70: 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d  sqlite3_next_stm
6b80: 74 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63  t()] interface c
6b90: 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6c 6f  an be used to lo
6ba0: 63 61 74 65 20 61 6c 6c 0a 2a 2a 20 5b 70 72 65  cate all.** [pre
6bb0: 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73  pared statements
6bc0: 5d 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  ] associated wit
6bd0: 68 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f  h a [database co
6be0: 6e 6e 65 63 74 69 6f 6e 5d 20 69 66 20 64 65 73  nnection] if des
6bf0: 69 72 65 64 2e 0a 2a 2a 20 54 79 70 69 63 61 6c  ired..** Typical
6c00: 20 63 6f 64 65 20 6d 69 67 68 74 20 6c 6f 6f 6b   code might look
6c10: 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   like this:.**.*
6c20: 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70  * <blockquote><p
6c30: 72 65 3e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73  re>.** sqlite3_s
6c40: 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 2a 2a 20 77  tmt *pStmt;.** w
6c50: 68 69 6c 65 28 20 28 70 53 74 6d 74 20 3d 20 73  hile( (pStmt = s
6c60: 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74  qlite3_next_stmt
6c70: 28 64 62 2c 20 30 29 29 21 3d 30 20 29 7b 0a 2a  (db, 0))!=0 ){.*
6c80: 2a 20 26 6e 62 73 70 3b 20 20 20 73 71 6c 69 74  * &nbsp;   sqlit
6c90: 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d  e3_finalize(pStm
6ca0: 74 29 3b 0a 2a 2a 20 7d 0a 2a 2a 20 3c 2f 70 72  t);.** }.** </pr
6cb0: 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a  e></blockquote>.
6cc0: 2a 2a 0a 2a 2a 20 49 66 20 5b 73 71 6c 69 74 65  **.** If [sqlite
6cd0: 33 5f 63 6c 6f 73 65 28 29 5d 20 69 73 20 69 6e  3_close()] is in
6ce0: 76 6f 6b 65 64 20 77 68 69 6c 65 20 61 20 74 72  voked while a tr
6cf0: 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65  ansaction is ope
6d00: 6e 2c 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73 61  n,.** the transa
6d10: 63 74 69 6f 6e 20 69 73 20 61 75 74 6f 6d 61 74  ction is automat
6d20: 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61  ically rolled ba
6d30: 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 20  ck..**.** The C 
6d40: 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71  parameter to [sq
6d50: 6c 69 74 65 33 5f 63 6c 6f 73 65 28 43 29 5d 20  lite3_close(C)] 
6d60: 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 61  must be either a
6d70: 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72   NULL.** pointer
6d80: 20 6f 72 20 61 6e 20 5b 73 71 6c 69 74 65 33 5d   or an [sqlite3]
6d90: 20 6f 62 6a 65 63 74 20 70 6f 69 6e 74 65 72 20   object pointer 
6da0: 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d  obtained.** from
6db0: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29   [sqlite3_open()
6dc0: 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  ], [sqlite3_open
6dd0: 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71  16()], or.** [sq
6de0: 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d  lite3_open_v2()]
6df0: 2c 20 61 6e 64 20 6e 6f 74 20 70 72 65 76 69 6f  , and not previo
6e00: 75 73 6c 79 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a  usly closed..**.
6e10: 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
6e20: 0a 2a 2a 20 5b 48 31 32 30 31 31 5d 20 5b 48 31  .** [H12011] [H1
6e30: 32 30 31 32 5d 20 5b 48 31 32 30 31 33 5d 20 5b  2012] [H12013] [
6e40: 48 31 32 30 31 34 5d 20 5b 48 31 32 30 31 35 5d  H12014] [H12015]
6e50: 20 5b 48 31 32 30 31 39 5d 0a 2a 2f 0a 53 51 4c   [H12019].*/.SQL
6e60: 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
6e70: 74 65 33 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65  te3_close(sqlite
6e80: 33 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  3 *);../*.** The
6e90: 20 74 79 70 65 20 66 6f 72 20 61 20 63 61 6c 6c   type for a call
6ea0: 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  back function..*
6eb0: 2a 20 54 68 69 73 20 69 73 20 6c 65 67 61 63 79  * This is legacy
6ec0: 20 61 6e 64 20 64 65 70 72 65 63 61 74 65 64 2e   and deprecated.
6ed0: 20 20 49 74 20 69 73 20 69 6e 63 6c 75 64 65 64    It is included
6ee0: 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 0a   for historical.
6ef0: 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79  ** compatibility
6f00: 20 61 6e 64 20 69 73 20 6e 6f 74 20 64 6f 63 75   and is not docu
6f10: 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 74 79 70 65 64  mented..*/.typed
6f20: 65 66 20 69 6e 74 20 28 2a 73 71 6c 69 74 65 33  ef int (*sqlite3
6f30: 5f 63 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a  _callback)(void*
6f40: 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 20 63 68 61  ,int,char**, cha
6f50: 72 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  r**);../*.** CAP
6f60: 49 33 52 45 46 3a 20 4f 6e 65 2d 53 74 65 70 20  I3REF: One-Step 
6f70: 51 75 65 72 79 20 45 78 65 63 75 74 69 6f 6e 20  Query Execution 
6f80: 49 6e 74 65 72 66 61 63 65 20 7b 48 31 32 31 30  Interface {H1210
6f90: 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a  0} <S10000>.**.*
6fa0: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78  * The sqlite3_ex
6fb0: 65 63 28 29 20 69 6e 74 65 72 66 61 63 65 20 69  ec() interface i
6fc0: 73 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 77  s a convenient w
6fd0: 61 79 20 6f 66 20 72 75 6e 6e 69 6e 67 20 6f 6e  ay of running on
6fe0: 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 53 51 4c  e or more.** SQL
6ff0: 20 73 74 61 74 65 6d 65 6e 74 73 20 77 69 74 68   statements with
7000: 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 77 72  out having to wr
7010: 69 74 65 20 61 20 6c 6f 74 20 6f 66 20 43 20 63  ite a lot of C c
7020: 6f 64 65 2e 20 20 54 68 65 20 55 54 46 2d 38 20  ode.  The UTF-8 
7030: 65 6e 63 6f 64 65 64 0a 2a 2a 20 53 51 4c 20 73  encoded.** SQL s
7040: 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 70 61  tatements are pa
7050: 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 20 73  ssed in as the s
7060: 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
7070: 74 6f 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  to sqlite3_exec(
7080: 29 2e 0a 2a 2a 20 54 68 65 20 73 74 61 74 65 6d  )..** The statem
7090: 65 6e 74 73 20 61 72 65 20 65 76 61 6c 75 61 74  ents are evaluat
70a0: 65 64 20 6f 6e 65 20 62 79 20 6f 6e 65 20 75 6e  ed one by one un
70b0: 74 69 6c 20 65 69 74 68 65 72 20 61 6e 20 65 72  til either an er
70c0: 72 6f 72 20 6f 72 0a 2a 2a 20 61 6e 20 69 6e 74  ror or.** an int
70d0: 65 72 72 75 70 74 20 69 73 20 65 6e 63 6f 75 6e  errupt is encoun
70e0: 74 65 72 65 64 2c 20 6f 72 20 75 6e 74 69 6c 20  tered, or until 
70f0: 74 68 65 79 20 61 72 65 20 61 6c 6c 20 64 6f 6e  they are all don
7100: 65 2e 20 20 54 68 65 20 33 72 64 20 70 61 72 61  e.  The 3rd para
7110: 6d 65 74 65 72 0a 2a 2a 20 69 73 20 61 6e 20 6f  meter.** is an o
7120: 70 74 69 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b  ptional callback
7130: 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64   that is invoked
7140: 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 72   once for each r
7150: 6f 77 20 6f 66 20 61 6e 79 20 71 75 65 72 79 0a  ow of any query.
7160: 2a 2a 20 72 65 73 75 6c 74 73 20 70 72 6f 64 75  ** results produ
7170: 63 65 64 20 62 79 20 74 68 65 20 53 51 4c 20 73  ced by the SQL s
7180: 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 65 20  tatements.  The 
7190: 35 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 65  5th parameter te
71a0: 6c 6c 73 20 77 68 65 72 65 0a 2a 2a 20 74 6f 20  lls where.** to 
71b0: 77 72 69 74 65 20 61 6e 79 20 65 72 72 6f 72 20  write any error 
71c0: 6d 65 73 73 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20  messages..**.** 
71d0: 54 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  The error messag
71e0: 65 20 70 61 73 73 65 64 20 62 61 63 6b 20 74 68  e passed back th
71f0: 72 6f 75 67 68 20 74 68 65 20 35 74 68 20 70 61  rough the 5th pa
7200: 72 61 6d 65 74 65 72 20 69 73 20 68 65 6c 64 0a  rameter is held.
7210: 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74  ** in memory obt
7220: 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69  ained from [sqli
7230: 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20  te3_malloc()].  
7240: 54 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d 6f 72  To avoid a memor
7250: 79 20 6c 65 61 6b 2c 0a 2a 2a 20 74 68 65 20 63  y leak,.** the c
7260: 61 6c 6c 69 6e 67 20 61 70 70 6c 69 63 61 74 69  alling applicati
7270: 6f 6e 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 5b  on should call [
7280: 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20  sqlite3_free()] 
7290: 6f 6e 20 61 6e 79 20 65 72 72 6f 72 0a 2a 2a 20  on any error.** 
72a0: 6d 65 73 73 61 67 65 20 72 65 74 75 72 6e 65 64  message returned
72b0: 20 74 68 72 6f 75 67 68 20 74 68 65 20 35 74 68   through the 5th
72c0: 20 70 61 72 61 6d 65 74 65 72 20 77 68 65 6e 20   parameter when 
72d0: 69 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 20  it has finished 
72e0: 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 65 72 72  using.** the err
72f0: 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a  or message..**.*
7300: 2a 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61  * If the SQL sta
7310: 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 32 6e  tement in the 2n
7320: 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 4e  d parameter is N
7330: 55 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 74 79 20  ULL or an empty 
7340: 73 74 72 69 6e 67 0a 2a 2a 20 6f 72 20 61 20 73  string.** or a s
7350: 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67  tring containing
7360: 20 6f 6e 6c 79 20 77 68 69 74 65 73 70 61 63 65   only whitespace
7370: 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 2c 20 74   and comments, t
7380: 68 65 6e 20 6e 6f 20 53 51 4c 0a 2a 2a 20 73 74  hen no SQL.** st
7390: 61 74 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61  atements are eva
73a0: 6c 75 61 74 65 64 20 61 6e 64 20 74 68 65 20 64  luated and the d
73b0: 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 63  atabase is not c
73c0: 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  hanged..**.** Th
73d0: 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29  e sqlite3_exec()
73e0: 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 69 6d   interface is im
73f0: 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74 65 72  plemented in ter
7400: 6d 73 20 6f 66 0a 2a 2a 20 5b 73 71 6c 69 74 65  ms of.** [sqlite
7410: 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c  3_prepare_v2()],
7420: 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
7430: 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  ], and [sqlite3_
7440: 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20  finalize()]..** 
7450: 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63  The sqlite3_exec
7460: 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20  () routine does 
7470: 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 65 20 64  nothing to the d
7480: 61 74 61 62 61 73 65 20 74 68 61 74 20 63 61 6e  atabase that can
7490: 6e 6f 74 20 62 65 20 64 6f 6e 65 0a 2a 2a 20 62  not be done.** b
74a0: 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  y [sqlite3_prepa
74b0: 72 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c 69 74  re_v2()], [sqlit
74c0: 65 33 5f 73 74 65 70 28 29 5d 2c 20 61 6e 64 20  e3_step()], and 
74d0: 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a  [sqlite3_finaliz
74e0: 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  e()]..**.** The 
74f0: 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20  first parameter 
7500: 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63  to [sqlite3_exec
7510: 28 29 5d 20 6d 75 73 74 20 62 65 20 61 6e 20 76  ()] must be an v
7520: 61 6c 69 64 20 61 6e 64 20 6f 70 65 6e 0a 2a 2a  alid and open.**
7530: 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
7540: 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  ction]..**.** Th
7550: 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
7560: 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 62  ction must not b
7570: 65 20 63 6c 6f 73 65 64 20 77 68 69 6c 65 0a 2a  e closed while.*
7580: 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28  * [sqlite3_exec(
7590: 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a  )] is running..*
75a0: 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67  *.** The calling
75b0: 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64   function should
75c0: 20 75 73 65 20 5b 73 71 6c 69 74 65 33 5f 66 72   use [sqlite3_fr
75d0: 65 65 28 29 5d 20 74 6f 20 66 72 65 65 0a 2a 2a  ee()] to free.**
75e0: 20 74 68 65 20 6d 65 6d 6f 72 79 20 74 68 61 74   the memory that
75f0: 20 2a 65 72 72 6d 73 67 20 69 73 20 6c 65 66 74   *errmsg is left
7600: 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 6f 6e 63   pointing at onc
7610: 65 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 6d  e the error.** m
7620: 65 73 73 61 67 65 20 69 73 20 6e 6f 20 6c 6f 6e  essage is no lon
7630: 67 65 72 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a  ger needed..**.*
7640: 2a 20 54 68 65 20 53 51 4c 20 73 74 61 74 65 6d  * The SQL statem
7650: 65 6e 74 20 74 65 78 74 20 69 6e 20 74 68 65 20  ent text in the 
7660: 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f  2nd parameter to
7670: 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29   [sqlite3_exec()
7680: 5d 0a 2a 2a 20 6d 75 73 74 20 72 65 6d 61 69 6e  ].** must remain
7690: 20 75 6e 63 68 61 6e 67 65 64 20 77 68 69 6c 65   unchanged while
76a0: 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29   [sqlite3_exec()
76b0: 5d 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a  ] is running..**
76c0: 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
76d0: 3a 0a 2a 2a 20 5b 48 31 32 31 30 31 5d 20 5b 48  :.** [H12101] [H
76e0: 31 32 31 30 32 5d 20 5b 48 31 32 31 30 34 5d 20  12102] [H12104] 
76f0: 5b 48 31 32 31 30 35 5d 20 5b 48 31 32 31 30 37  [H12105] [H12107
7700: 5d 20 5b 48 31 32 31 31 30 5d 20 5b 48 31 32 31  ] [H12110] [H121
7710: 31 33 5d 20 5b 48 31 32 31 31 36 5d 0a 2a 2a 20  13] [H12116].** 
7720: 5b 48 31 32 31 31 39 5d 20 5b 48 31 32 31 32 32  [H12119] [H12122
7730: 5d 20 5b 48 31 32 31 32 35 5d 20 5b 48 31 32 31  ] [H12125] [H121
7740: 33 31 5d 20 5b 48 31 32 31 33 34 5d 20 5b 48 31  31] [H12134] [H1
7750: 32 31 33 37 5d 20 5b 48 31 32 31 33 38 5d 0a 2a  2137] [H12138].*
7760: 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
7770: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 0a 20   sqlite3_exec(. 
7780: 20 73 71 6c 69 74 65 33 2a 2c 20 20 20 20 20 20   sqlite3*,      
7790: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77a0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
77b0: 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20  n open database 
77c0: 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
77d0: 2a 73 71 6c 2c 20 20 20 20 20 20 20 20 20 20 20  *sql,           
77e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77f0: 2f 2a 20 53 51 4c 20 74 6f 20 62 65 20 65 76 61  /* SQL to be eva
7800: 6c 75 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20  luated */.  int 
7810: 28 2a 63 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64  (*callback)(void
7820: 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 63 68 61  *,int,char**,cha
7830: 72 2a 2a 29 2c 20 20 2f 2a 20 43 61 6c 6c 62 61  r**),  /* Callba
7840: 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  ck function */. 
7850: 20 76 6f 69 64 20 2a 2c 20 20 20 20 20 20 20 20   void *,        
7860: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7870: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31              /* 1
7880: 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 63  st argument to c
7890: 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 63 68 61  allback */.  cha
78a0: 72 20 2a 2a 65 72 72 6d 73 67 20 20 20 20 20 20  r **errmsg      
78b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78c0: 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72          /* Error
78d0: 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68 65 72   msg written her
78e0: 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  e */.);../*.** C
78f0: 41 50 49 33 52 45 46 3a 20 52 65 73 75 6c 74 20  API3REF: Result 
7900: 43 6f 64 65 73 20 7b 48 31 30 32 31 30 7d 20 3c  Codes {H10210} <
7910: 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f  S10700>.** KEYWO
7920: 52 44 53 3a 20 53 51 4c 49 54 45 5f 4f 4b 20 7b  RDS: SQLITE_OK {
7930: 65 72 72 6f 72 20 63 6f 64 65 7d 20 7b 65 72 72  error code} {err
7940: 6f 72 20 63 6f 64 65 73 7d 0a 2a 2a 20 4b 45 59  or codes}.** KEY
7950: 57 4f 52 44 53 3a 20 7b 72 65 73 75 6c 74 20 63  WORDS: {result c
7960: 6f 64 65 7d 20 7b 72 65 73 75 6c 74 20 63 6f 64  ode} {result cod
7970: 65 73 7d 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 20 53  es}.**.** Many S
7980: 51 4c 69 74 65 20 66 75 6e 63 74 69 6f 6e 73 20  QLite functions 
7990: 72 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65  return an intege
79a0: 72 20 72 65 73 75 6c 74 20 63 6f 64 65 20 66 72  r result code fr
79b0: 6f 6d 20 74 68 65 20 73 65 74 20 73 68 6f 77 6e  om the set shown
79c0: 0a 2a 2a 20 68 65 72 65 20 69 6e 20 6f 72 64 65  .** here in orde
79d0: 72 20 74 6f 20 69 6e 64 69 63 61 74 65 73 20 73  r to indicates s
79e0: 75 63 63 65 73 73 20 6f 72 20 66 61 69 6c 75 72  uccess or failur
79f0: 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 65 72 72  e..**.** New err
7a00: 6f 72 20 63 6f 64 65 73 20 6d 61 79 20 62 65 20  or codes may be 
7a10: 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 20  added in future 
7a20: 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69  versions of SQLi
7a30: 74 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  te..**.** See al
7a40: 73 6f 3a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52  so: [SQLITE_IOER
7a50: 52 5f 52 45 41 44 20 7c 20 65 78 74 65 6e 64 65  R_READ | extende
7a60: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 0a  d result codes].
7a70: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
7a80: 45 5f 4f 4b 20 20 20 20 20 20 20 20 20 20 20 30  E_OK           0
7a90: 20 20 20 2f 2a 20 53 75 63 63 65 73 73 66 75 6c     /* Successful
7aa0: 20 72 65 73 75 6c 74 20 2a 2f 0a 2f 2a 20 62 65   result */./* be
7ab0: 67 69 6e 6e 69 6e 67 2d 6f 66 2d 65 72 72 6f 72  ginning-of-error
7ac0: 2d 63 6f 64 65 73 20 2a 2f 0a 23 64 65 66 69 6e  -codes */.#defin
7ad0: 65 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 20  e SQLITE_ERROR  
7ae0: 20 20 20 20 20 20 31 20 20 20 2f 2a 20 53 51 4c        1   /* SQL
7af0: 20 65 72 72 6f 72 20 6f 72 20 6d 69 73 73 69 6e   error or missin
7b00: 67 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 23 64  g database */.#d
7b10: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54  efine SQLITE_INT
7b20: 45 52 4e 41 4c 20 20 20 20 20 32 20 20 20 2f 2a  ERNAL     2   /*
7b30: 20 49 6e 74 65 72 6e 61 6c 20 6c 6f 67 69 63 20   Internal logic 
7b40: 65 72 72 6f 72 20 69 6e 20 53 51 4c 69 74 65 20  error in SQLite 
7b50: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
7b60: 45 5f 50 45 52 4d 20 20 20 20 20 20 20 20 20 33  E_PERM         3
7b70: 20 20 20 2f 2a 20 41 63 63 65 73 73 20 70 65 72     /* Access per
7b80: 6d 69 73 73 69 6f 6e 20 64 65 6e 69 65 64 20 2a  mission denied *
7b90: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
7ba0: 5f 41 42 4f 52 54 20 20 20 20 20 20 20 20 34 20  _ABORT        4 
7bb0: 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 72 6f    /* Callback ro
7bc0: 75 74 69 6e 65 20 72 65 71 75 65 73 74 65 64 20  utine requested 
7bd0: 61 6e 20 61 62 6f 72 74 20 2a 2f 0a 23 64 65 66  an abort */.#def
7be0: 69 6e 65 20 53 51 4c 49 54 45 5f 42 55 53 59 20  ine SQLITE_BUSY 
7bf0: 20 20 20 20 20 20 20 20 35 20 20 20 2f 2a 20 54          5   /* T
7c00: 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
7c10: 20 69 73 20 6c 6f 63 6b 65 64 20 2a 2f 0a 23 64   is locked */.#d
7c20: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43  efine SQLITE_LOC
7c30: 4b 45 44 20 20 20 20 20 20 20 36 20 20 20 2f 2a  KED       6   /*
7c40: 20 41 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20   A table in the 
7c50: 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b  database is lock
7c60: 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ed */.#define SQ
7c70: 4c 49 54 45 5f 4e 4f 4d 45 4d 20 20 20 20 20 20  LITE_NOMEM      
7c80: 20 20 37 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f    7   /* A mallo
7c90: 63 28 29 20 66 61 69 6c 65 64 20 2a 2f 0a 23 64  c() failed */.#d
7ca0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 41  efine SQLITE_REA
7cb0: 44 4f 4e 4c 59 20 20 20 20 20 38 20 20 20 2f 2a  DONLY     8   /*
7cc0: 20 41 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74   Attempt to writ
7cd0: 65 20 61 20 72 65 61 64 6f 6e 6c 79 20 64 61 74  e a readonly dat
7ce0: 61 62 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65  abase */.#define
7cf0: 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50   SQLITE_INTERRUP
7d00: 54 20 20 20 20 39 20 20 20 2f 2a 20 4f 70 65 72  T    9   /* Oper
7d10: 61 74 69 6f 6e 20 74 65 72 6d 69 6e 61 74 65 64  ation terminated
7d20: 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 74 65   by sqlite3_inte
7d30: 72 72 75 70 74 28 29 2a 2f 0a 23 64 65 66 69 6e  rrupt()*/.#defin
7d40: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 20  e SQLITE_IOERR  
7d50: 20 20 20 20 20 31 30 20 20 20 2f 2a 20 53 6f 6d       10   /* Som
7d60: 65 20 6b 69 6e 64 20 6f 66 20 64 69 73 6b 20 49  e kind of disk I
7d70: 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65  /O error occurre
7d80: 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  d */.#define SQL
7d90: 49 54 45 5f 43 4f 52 52 55 50 54 20 20 20 20 20  ITE_CORRUPT     
7da0: 31 31 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61  11   /* The data
7db0: 62 61 73 65 20 64 69 73 6b 20 69 6d 61 67 65 20  base disk image 
7dc0: 69 73 20 6d 61 6c 66 6f 72 6d 65 64 20 2a 2f 0a  is malformed */.
7dd0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e  #define SQLITE_N
7de0: 4f 54 46 4f 55 4e 44 20 20 20 20 31 32 20 20 20  OTFOUND    12   
7df0: 2f 2a 20 4e 4f 54 20 55 53 45 44 2e 20 54 61 62  /* NOT USED. Tab
7e00: 6c 65 20 6f 72 20 72 65 63 6f 72 64 20 6e 6f 74  le or record not
7e10: 20 66 6f 75 6e 64 20 2a 2f 0a 23 64 65 66 69 6e   found */.#defin
7e20: 65 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 20 20  e SQLITE_FULL   
7e30: 20 20 20 20 20 31 33 20 20 20 2f 2a 20 49 6e 73       13   /* Ins
7e40: 65 72 74 69 6f 6e 20 66 61 69 6c 65 64 20 62 65  ertion failed be
7e50: 63 61 75 73 65 20 64 61 74 61 62 61 73 65 20 69  cause database i
7e60: 73 20 66 75 6c 6c 20 2a 2f 0a 23 64 65 66 69 6e  s full */.#defin
7e70: 65 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45  e SQLITE_CANTOPE
7e80: 4e 20 20 20 20 31 34 20 20 20 2f 2a 20 55 6e 61  N    14   /* Una
7e90: 62 6c 65 20 74 6f 20 6f 70 65 6e 20 74 68 65 20  ble to open the 
7ea0: 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
7eb0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
7ec0: 50 52 4f 54 4f 43 4f 4c 20 20 20 20 31 35 20 20  PROTOCOL    15  
7ed0: 20 2f 2a 20 4e 4f 54 20 55 53 45 44 2e 20 44 61   /* NOT USED. Da
7ee0: 74 61 62 61 73 65 20 6c 6f 63 6b 20 70 72 6f 74  tabase lock prot
7ef0: 6f 63 6f 6c 20 65 72 72 6f 72 20 2a 2f 0a 23 64  ocol error */.#d
7f00: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4d 50  efine SQLITE_EMP
7f10: 54 59 20 20 20 20 20 20 20 31 36 20 20 20 2f 2a  TY       16   /*
7f20: 20 44 61 74 61 62 61 73 65 20 69 73 20 65 6d 70   Database is emp
7f30: 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ty */.#define SQ
7f40: 4c 49 54 45 5f 53 43 48 45 4d 41 20 20 20 20 20  LITE_SCHEMA     
7f50: 20 31 37 20 20 20 2f 2a 20 54 68 65 20 64 61 74   17   /* The dat
7f60: 61 62 61 73 65 20 73 63 68 65 6d 61 20 63 68 61  abase schema cha
7f70: 6e 67 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  nged */.#define 
7f80: 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 20 20  SQLITE_TOOBIG   
7f90: 20 20 20 31 38 20 20 20 2f 2a 20 53 74 72 69 6e     18   /* Strin
7fa0: 67 20 6f 72 20 42 4c 4f 42 20 65 78 63 65 65 64  g or BLOB exceed
7fb0: 73 20 73 69 7a 65 20 6c 69 6d 69 74 20 2a 2f 0a  s size limit */.
7fc0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
7fd0: 4f 4e 53 54 52 41 49 4e 54 20 20 31 39 20 20 20  ONSTRAINT  19   
7fe0: 2f 2a 20 41 62 6f 72 74 20 64 75 65 20 74 6f 20  /* Abort due to 
7ff0: 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61  constraint viola
8000: 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  tion */.#define 
8010: 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 20  SQLITE_MISMATCH 
8020: 20 20 20 32 30 20 20 20 2f 2a 20 44 61 74 61 20     20   /* Data 
8030: 74 79 70 65 20 6d 69 73 6d 61 74 63 68 20 2a 2f  type mismatch */
8040: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
8050: 4d 49 53 55 53 45 20 20 20 20 20 20 32 31 20 20  MISUSE      21  
8060: 20 2f 2a 20 4c 69 62 72 61 72 79 20 75 73 65 64   /* Library used
8070: 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 2a 2f 0a   incorrectly */.
8080: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e  #define SQLITE_N
8090: 4f 4c 46 53 20 20 20 20 20 20 20 32 32 20 20 20  OLFS       22   
80a0: 2f 2a 20 55 73 65 73 20 4f 53 20 66 65 61 74 75  /* Uses OS featu
80b0: 72 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 65  res not supporte
80c0: 64 20 6f 6e 20 68 6f 73 74 20 2a 2f 0a 23 64 65  d on host */.#de
80d0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 55 54 48  fine SQLITE_AUTH
80e0: 20 20 20 20 20 20 20 20 32 33 20 20 20 2f 2a 20          23   /* 
80f0: 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 64 65  Authorization de
8100: 6e 69 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  nied */.#define 
8110: 53 51 4c 49 54 45 5f 46 4f 52 4d 41 54 20 20 20  SQLITE_FORMAT   
8120: 20 20 20 32 34 20 20 20 2f 2a 20 41 75 78 69 6c     24   /* Auxil
8130: 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66 6f  iary database fo
8140: 72 6d 61 74 20 65 72 72 6f 72 20 2a 2f 0a 23 64  rmat error */.#d
8150: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 41 4e  efine SQLITE_RAN
8160: 47 45 20 20 20 20 20 20 20 32 35 20 20 20 2f 2a  GE       25   /*
8170: 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74   2nd parameter t
8180: 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 20 6f  o sqlite3_bind o
8190: 75 74 20 6f 66 20 72 61 6e 67 65 20 2a 2f 0a 23  ut of range */.#
81a0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f  define SQLITE_NO
81b0: 54 41 44 42 20 20 20 20 20 20 32 36 20 20 20 2f  TADB      26   /
81c0: 2a 20 46 69 6c 65 20 6f 70 65 6e 65 64 20 74 68  * File opened th
81d0: 61 74 20 69 73 20 6e 6f 74 20 61 20 64 61 74 61  at is not a data
81e0: 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 23 64 65  base file */.#de
81f0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 4f 57 20  fine SQLITE_ROW 
8200: 20 20 20 20 20 20 20 20 31 30 30 20 20 2f 2a 20          100  /* 
8210: 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 68  sqlite3_step() h
8220: 61 73 20 61 6e 6f 74 68 65 72 20 72 6f 77 20 72  as another row r
8230: 65 61 64 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  eady */.#define 
8240: 53 51 4c 49 54 45 5f 44 4f 4e 45 20 20 20 20 20  SQLITE_DONE     
8250: 20 20 20 31 30 31 20 20 2f 2a 20 73 71 6c 69 74     101  /* sqlit
8260: 65 33 5f 73 74 65 70 28 29 20 68 61 73 20 66 69  e3_step() has fi
8270: 6e 69 73 68 65 64 20 65 78 65 63 75 74 69 6e 67  nished executing
8280: 20 2a 2f 0a 2f 2a 20 65 6e 64 2d 6f 66 2d 65 72   */./* end-of-er
8290: 72 6f 72 2d 63 6f 64 65 73 20 2a 2f 0a 0a 2f 2a  ror-codes */../*
82a0: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 78  .** CAPI3REF: Ex
82b0: 74 65 6e 64 65 64 20 52 65 73 75 6c 74 20 43 6f  tended Result Co
82c0: 64 65 73 20 7b 48 31 30 32 32 30 7d 20 3c 53 31  des {H10220} <S1
82d0: 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44  0700>.** KEYWORD
82e0: 53 3a 20 7b 65 78 74 65 6e 64 65 64 20 65 72 72  S: {extended err
82f0: 6f 72 20 63 6f 64 65 7d 20 7b 65 78 74 65 6e 64  or code} {extend
8300: 65 64 20 65 72 72 6f 72 20 63 6f 64 65 73 7d 0a  ed error codes}.
8310: 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 65 78  ** KEYWORDS: {ex
8320: 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f  tended result co
8330: 64 65 7d 20 7b 65 78 74 65 6e 64 65 64 20 72 65  de} {extended re
8340: 73 75 6c 74 20 63 6f 64 65 73 7d 0a 2a 2a 0a 2a  sult codes}.**.*
8350: 2a 20 49 6e 20 69 74 73 20 64 65 66 61 75 6c 74  * In its default
8360: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c 20   configuration, 
8370: 53 51 4c 69 74 65 20 41 50 49 20 72 6f 75 74 69  SQLite API routi
8380: 6e 65 73 20 72 65 74 75 72 6e 20 6f 6e 65 20 6f  nes return one o
8390: 66 20 32 36 20 69 6e 74 65 67 65 72 0a 2a 2a 20  f 26 integer.** 
83a0: 5b 53 51 4c 49 54 45 5f 4f 4b 20 7c 20 72 65 73  [SQLITE_OK | res
83b0: 75 6c 74 20 63 6f 64 65 73 5d 2e 20 20 48 6f 77  ult codes].  How
83c0: 65 76 65 72 2c 20 65 78 70 65 72 69 65 6e 63 65  ever, experience
83d0: 20 68 61 73 20 73 68 6f 77 6e 20 74 68 61 74 20   has shown that 
83e0: 6d 61 6e 79 20 6f 66 0a 2a 2a 20 74 68 65 73 65  many of.** these
83f0: 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61 72   result codes ar
8400: 65 20 74 6f 6f 20 63 6f 61 72 73 65 2d 67 72 61  e too coarse-gra
8410: 69 6e 65 64 2e 20 20 54 68 65 79 20 64 6f 20 6e  ined.  They do n
8420: 6f 74 20 70 72 6f 76 69 64 65 20 61 73 0a 2a 2a  ot provide as.**
8430: 20 6d 75 63 68 20 69 6e 66 6f 72 6d 61 74 69 6f   much informatio
8440: 6e 20 61 62 6f 75 74 20 70 72 6f 62 6c 65 6d 73  n about problems
8450: 20 61 73 20 70 72 6f 67 72 61 6d 6d 65 72 73 20   as programmers 
8460: 6d 69 67 68 74 20 6c 69 6b 65 2e 20 20 49 6e 20  might like.  In 
8470: 61 6e 20 65 66 66 6f 72 74 20 74 6f 0a 2a 2a 20  an effort to.** 
8480: 61 64 64 72 65 73 73 20 74 68 69 73 2c 20 6e 65  address this, ne
8490: 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  wer versions of 
84a0: 53 51 4c 69 74 65 20 28 76 65 72 73 69 6f 6e 20  SQLite (version 
84b0: 33 2e 33 2e 38 20 61 6e 64 20 6c 61 74 65 72 29  3.3.8 and later)
84c0: 20 69 6e 63 6c 75 64 65 0a 2a 2a 20 73 75 70 70   include.** supp
84d0: 6f 72 74 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  ort for addition
84e0: 61 6c 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20  al result codes 
84f0: 74 68 61 74 20 70 72 6f 76 69 64 65 20 6d 6f 72  that provide mor
8500: 65 20 64 65 74 61 69 6c 65 64 20 69 6e 66 6f 72  e detailed infor
8510: 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20  mation.** about 
8520: 65 72 72 6f 72 73 2e 20 54 68 65 20 65 78 74 65  errors. The exte
8530: 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65  nded result code
8540: 73 20 61 72 65 20 65 6e 61 62 6c 65 64 20 6f 72  s are enabled or
8550: 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 6f 6e 20   disabled.** on 
8560: 61 20 70 65 72 20 64 61 74 61 62 61 73 65 20 63  a per database c
8570: 6f 6e 6e 65 63 74 69 6f 6e 20 62 61 73 69 73 20  onnection basis 
8580: 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71  using the.** [sq
8590: 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72  lite3_extended_r
85a0: 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 5d 20 41  esult_codes()] A
85b0: 50 49 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f  PI..**.** Some o
85c0: 66 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20  f the available 
85d0: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
85e0: 63 6f 64 65 73 20 61 72 65 20 6c 69 73 74 65 64  codes are listed
85f0: 20 68 65 72 65 2e 0a 2a 2a 20 4f 6e 65 20 6d 61   here..** One ma
8600: 79 20 65 78 70 65 63 74 20 74 68 65 20 6e 75 6d  y expect the num
8610: 62 65 72 20 6f 66 20 65 78 74 65 6e 64 65 64 20  ber of extended 
8620: 72 65 73 75 6c 74 20 63 6f 64 65 73 20 77 69 6c  result codes wil
8630: 6c 20 62 65 20 65 78 70 61 6e 64 0a 2a 2a 20 6f  l be expand.** o
8640: 76 65 72 20 74 69 6d 65 2e 20 20 53 6f 66 74 77  ver time.  Softw
8650: 61 72 65 20 74 68 61 74 20 75 73 65 73 20 65 78  are that uses ex
8660: 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f  tended result co
8670: 64 65 73 20 73 68 6f 75 6c 64 20 65 78 70 65 63  des should expec
8680: 74 0a 2a 2a 20 74 6f 20 73 65 65 20 6e 65 77 20  t.** to see new 
8690: 72 65 73 75 6c 74 20 63 6f 64 65 73 20 69 6e 20  result codes in 
86a0: 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20  future releases 
86b0: 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a  of SQLite..**.**
86c0: 20 54 68 65 20 53 51 4c 49 54 45 5f 4f 4b 20 72   The SQLITE_OK r
86d0: 65 73 75 6c 74 20 63 6f 64 65 20 77 69 6c 6c 20  esult code will 
86e0: 6e 65 76 65 72 20 62 65 20 65 78 74 65 6e 64 65  never be extende
86f0: 64 2e 20 20 49 74 20 77 69 6c 6c 20 61 6c 77 61  d.  It will alwa
8700: 79 73 0a 2a 2a 20 62 65 20 65 78 61 63 74 6c 79  ys.** be exactly
8710: 20 7a 65 72 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e   zero..*/.#defin
8720: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52  e SQLITE_IOERR_R
8730: 45 41 44 20 20 20 20 20 20 20 20 20 20 20 20 20  EAD             
8740: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c   (SQLITE_IOERR |
8750: 20 28 31 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65   (1<<8)).#define
8760: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48   SQLITE_IOERR_SH
8770: 4f 52 54 5f 52 45 41 44 20 20 20 20 20 20 20 20  ORT_READ        
8780: 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20  (SQLITE_IOERR | 
8790: 28 32 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  (2<<8)).#define 
87a0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49  SQLITE_IOERR_WRI
87b0: 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 28  TE             (
87c0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
87d0: 33 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53  3<<8)).#define S
87e0: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e  QLITE_IOERR_FSYN
87f0: 43 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53  C             (S
8800: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 34  QLITE_IOERR | (4
8810: 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51  <<8)).#define SQ
8820: 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46  LITE_IOERR_DIR_F
8830: 53 59 4e 43 20 20 20 20 20 20 20 20 20 28 53 51  SYNC         (SQ
8840: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 35 3c  LITE_IOERR | (5<
8850: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  <8)).#define SQL
8860: 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41  ITE_IOERR_TRUNCA
8870: 54 45 20 20 20 20 20 20 20 20 20 20 28 53 51 4c  TE          (SQL
8880: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 36 3c 3c  ITE_IOERR | (6<<
8890: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  8)).#define SQLI
88a0: 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 20 20  TE_IOERR_FSTAT  
88b0: 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49             (SQLI
88c0: 54 45 5f 49 4f 45 52 52 20 7c 20 28 37 3c 3c 38  TE_IOERR | (7<<8
88d0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
88e0: 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 20 20  E_IOERR_UNLOCK  
88f0: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
8900: 45 5f 49 4f 45 52 52 20 7c 20 28 38 3c 3c 38 29  E_IOERR | (8<<8)
8910: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ).#define SQLITE
8920: 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 20 20 20  _IOERR_RDLOCK   
8930: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45           (SQLITE
8940: 5f 49 4f 45 52 52 20 7c 20 28 39 3c 3c 38 29 29  _IOERR | (9<<8))
8950: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
8960: 49 4f 45 52 52 5f 44 45 4c 45 54 45 20 20 20 20  IOERR_DELETE    
8970: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f          (SQLITE_
8980: 49 4f 45 52 52 20 7c 20 28 31 30 3c 3c 38 29 29  IOERR | (10<<8))
8990: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
89a0: 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 20 20  IOERR_BLOCKED   
89b0: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f          (SQLITE_
89c0: 49 4f 45 52 52 20 7c 20 28 31 31 3c 3c 38 29 29  IOERR | (11<<8))
89d0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
89e0: 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 20 20 20 20  IOERR_NOMEM     
89f0: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f          (SQLITE_
8a00: 49 4f 45 52 52 20 7c 20 28 31 32 3c 3c 38 29 29  IOERR | (12<<8))
8a10: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
8a20: 49 4f 45 52 52 5f 41 43 43 45 53 53 20 20 20 20  IOERR_ACCESS    
8a30: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f          (SQLITE_
8a40: 49 4f 45 52 52 20 7c 20 28 31 33 3c 3c 38 29 29  IOERR | (13<<8))
8a50: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
8a60: 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52  IOERR_CHECKRESER
8a70: 56 45 44 4c 4f 43 4b 20 28 53 51 4c 49 54 45 5f  VEDLOCK (SQLITE_
8a80: 49 4f 45 52 52 20 7c 20 28 31 34 3c 3c 38 29 29  IOERR | (14<<8))
8a90: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
8aa0: 49 4f 45 52 52 5f 4c 4f 43 4b 20 20 20 20 20 20  IOERR_LOCK      
8ab0: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f          (SQLITE_
8ac0: 49 4f 45 52 52 20 7c 20 28 31 35 3c 3c 38 29 29  IOERR | (15<<8))
8ad0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
8ae0: 49 4f 45 52 52 5f 43 4c 4f 53 45 20 20 20 20 20  IOERR_CLOSE     
8af0: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f          (SQLITE_
8b00: 49 4f 45 52 52 20 7c 20 28 31 36 3c 3c 38 29 29  IOERR | (16<<8))
8b10: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
8b20: 49 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 20  IOERR_DIR_CLOSE 
8b30: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f          (SQLITE_
8b40: 49 4f 45 52 52 20 7c 20 28 31 37 3c 3c 38 29 29  IOERR | (17<<8))
8b50: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
8b60: 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43  LOCKED_SHAREDCAC
8b70: 48 45 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f  HE      (SQLITE_
8b80: 4c 4f 43 4b 45 44 20 7c 20 28 31 3c 3c 38 29 20  LOCKED | (1<<8) 
8b90: 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  )../*.** CAPI3RE
8ba0: 46 3a 20 46 6c 61 67 73 20 46 6f 72 20 46 69 6c  F: Flags For Fil
8bb0: 65 20 4f 70 65 6e 20 4f 70 65 72 61 74 69 6f 6e  e Open Operation
8bc0: 73 20 7b 48 31 30 32 33 30 7d 20 3c 48 31 31 31  s {H10230} <H111
8bd0: 32 30 3e 20 3c 48 31 32 37 30 30 3e 0a 2a 2a 0a  20> <H12700>.**.
8be0: 2a 2a 20 54 68 65 73 65 20 62 69 74 20 76 61 6c  ** These bit val
8bf0: 75 65 73 20 61 72 65 20 69 6e 74 65 6e 64 65 64  ues are intended
8c00: 20 66 6f 72 20 75 73 65 20 69 6e 20 74 68 65 0a   for use in the.
8c10: 2a 2a 20 33 72 64 20 70 61 72 61 6d 65 74 65 72  ** 3rd parameter
8c20: 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33   to the [sqlite3
8c30: 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69 6e 74 65  _open_v2()] inte
8c40: 72 66 61 63 65 20 61 6e 64 0a 2a 2a 20 69 6e 20  rface and.** in 
8c50: 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65  the 4th paramete
8c60: 72 20 74 6f 20 74 68 65 20 78 4f 70 65 6e 20 6d  r to the xOpen m
8c70: 65 74 68 6f 64 20 6f 66 20 74 68 65 0a 2a 2a 20  ethod of the.** 
8c80: 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62  [sqlite3_vfs] ob
8c90: 6a 65 63 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ject..*/.#define
8ca0: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
8cb0: 44 4f 4e 4c 59 20 20 20 20 20 20 20 20 20 30 78  DONLY         0x
8cc0: 30 30 30 30 30 30 30 31 20 20 2f 2a 20 4f 6b 20  00000001  /* Ok 
8cd0: 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  for sqlite3_open
8ce0: 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65  _v2() */.#define
8cf0: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
8d00: 44 57 52 49 54 45 20 20 20 20 20 20 20 20 30 78  DWRITE        0x
8d10: 30 30 30 30 30 30 30 32 20 20 2f 2a 20 4f 6b 20  00000002  /* Ok 
8d20: 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  for sqlite3_open
8d30: 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65  _v2() */.#define
8d40: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45   SQLITE_OPEN_CRE
8d50: 41 54 45 20 20 20 20 20 20 20 20 20 20 20 30 78  ATE           0x
8d60: 30 30 30 30 30 30 30 34 20 20 2f 2a 20 4f 6b 20  00000004  /* Ok 
8d70: 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  for sqlite3_open
8d80: 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65  _v2() */.#define
8d90: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c   SQLITE_OPEN_DEL
8da0: 45 54 45 4f 4e 43 4c 4f 53 45 20 20 20 20 30 78  ETEONCLOSE    0x
8db0: 30 30 30 30 30 30 30 38 20 20 2f 2a 20 56 46 53  00000008  /* VFS
8dc0: 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65   only */.#define
8dd0: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43   SQLITE_OPEN_EXC
8de0: 4c 55 53 49 56 45 20 20 20 20 20 20 20 20 30 78  LUSIVE        0x
8df0: 30 30 30 30 30 30 31 30 20 20 2f 2a 20 56 46 53  00000010  /* VFS
8e00: 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65   only */.#define
8e10: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49   SQLITE_OPEN_MAI
8e20: 4e 5f 44 42 20 20 20 20 20 20 20 20 20 20 30 78  N_DB          0x
8e30: 30 30 30 30 30 31 30 30 20 20 2f 2a 20 56 46 53  00000100  /* VFS
8e40: 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65   only */.#define
8e50: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d   SQLITE_OPEN_TEM
8e60: 50 5f 44 42 20 20 20 20 20 20 20 20 20 20 30 78  P_DB          0x
8e70: 30 30 30 30 30 32 30 30 20 20 2f 2a 20 56 46 53  00000200  /* VFS
8e80: 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65   only */.#define
8e90: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41   SQLITE_OPEN_TRA
8ea0: 4e 53 49 45 4e 54 5f 44 42 20 20 20 20 20 30 78  NSIENT_DB     0x
8eb0: 30 30 30 30 30 34 30 30 20 20 2f 2a 20 56 46 53  00000400  /* VFS
8ec0: 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65   only */.#define
8ed0: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49   SQLITE_OPEN_MAI
8ee0: 4e 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20 30 78  N_JOURNAL     0x
8ef0: 30 30 30 30 30 38 30 30 20 20 2f 2a 20 56 46 53  00000800  /* VFS
8f00: 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65   only */.#define
8f10: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d   SQLITE_OPEN_TEM
8f20: 50 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20 30 78  P_JOURNAL     0x
8f30: 30 30 30 30 31 30 30 30 20 20 2f 2a 20 56 46 53  00001000  /* VFS
8f40: 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65   only */.#define
8f50: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42   SQLITE_OPEN_SUB
8f60: 4a 4f 55 52 4e 41 4c 20 20 20 20 20 20 20 30 78  JOURNAL       0x
8f70: 30 30 30 30 32 30 30 30 20 20 2f 2a 20 56 46 53  00002000  /* VFS
8f80: 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65   only */.#define
8f90: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53   SQLITE_OPEN_MAS
8fa0: 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 20 20 30 78  TER_JOURNAL   0x
8fb0: 30 30 30 30 34 30 30 30 20 20 2f 2a 20 56 46 53  00004000  /* VFS
8fc0: 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65   only */.#define
8fd0: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d   SQLITE_OPEN_NOM
8fe0: 55 54 45 58 20 20 20 20 20 20 20 20 20 20 30 78  UTEX          0x
8ff0: 30 30 30 30 38 30 30 30 20 20 2f 2a 20 4f 6b 20  00008000  /* Ok 
9000: 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  for sqlite3_open
9010: 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65  _v2() */.#define
9020: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c   SQLITE_OPEN_FUL
9030: 4c 4d 55 54 45 58 20 20 20 20 20 20 20 20 30 78  LMUTEX        0x
9040: 30 30 30 31 30 30 30 30 20 20 2f 2a 20 4f 6b 20  00010000  /* Ok 
9050: 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  for sqlite3_open
9060: 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65  _v2() */.#define
9070: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41   SQLITE_OPEN_SHA
9080: 52 45 44 43 41 43 48 45 20 20 20 20 20 20 30 78  REDCACHE      0x
9090: 30 30 30 32 30 30 30 30 20 20 2f 2a 20 4f 6b 20  00020000  /* Ok 
90a0: 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  for sqlite3_open
90b0: 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65  _v2() */.#define
90c0: 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49   SQLITE_OPEN_PRI
90d0: 56 41 54 45 43 41 43 48 45 20 20 20 20 20 30 78  VATECACHE     0x
90e0: 30 30 30 34 30 30 30 30 20 20 2f 2a 20 4f 6b 20  00040000  /* Ok 
90f0: 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  for sqlite3_open
9100: 5f 76 32 28 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  _v2() */../*.** 
9110: 43 41 50 49 33 52 45 46 3a 20 44 65 76 69 63 65  CAPI3REF: Device
9120: 20 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73   Characteristics
9130: 20 7b 48 31 30 32 34 30 7d 20 3c 48 31 31 31 32   {H10240} <H1112
9140: 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 44 65  0>.**.** The xDe
9150: 76 69 63 65 43 61 70 61 62 69 6c 69 74 69 65 73  viceCapabilities
9160: 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 5b   method of the [
9170: 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
9180: 64 73 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 72 65  ds].** object re
9190: 74 75 72 6e 73 20 61 6e 20 69 6e 74 65 67 65 72  turns an integer
91a0: 20 77 68 69 63 68 20 69 73 20 61 20 76 65 63 74   which is a vect
91b0: 6f 72 20 6f 66 20 74 68 65 20 74 68 65 73 65 0a  or of the these.
91c0: 2a 2a 20 62 69 74 20 76 61 6c 75 65 73 20 65 78  ** bit values ex
91d0: 70 72 65 73 73 69 6e 67 20 49 2f 4f 20 63 68 61  pressing I/O cha
91e0: 72 61 63 74 65 72 69 73 74 69 63 73 20 6f 66 20  racteristics of 
91f0: 74 68 65 20 6d 61 73 73 20 73 74 6f 72 61 67 65  the mass storage
9200: 0a 2a 2a 20 64 65 76 69 63 65 20 74 68 61 74 20  .** device that 
9210: 68 6f 6c 64 73 20 74 68 65 20 66 69 6c 65 20 74  holds the file t
9220: 68 61 74 20 74 68 65 20 5b 73 71 6c 69 74 65 33  hat the [sqlite3
9230: 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20  _io_methods].** 
9240: 72 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a  refers to..**.**
9250: 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41   The SQLITE_IOCA
9260: 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70 65 72 74  P_ATOMIC propert
9270: 79 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6c 6c  y means that all
9280: 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a 20 61 6e   writes of.** an
9290: 79 20 73 69 7a 65 20 61 72 65 20 61 74 6f 6d 69  y size are atomi
92a0: 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49  c.  The SQLITE_I
92b0: 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e 20 76  OCAP_ATOMICnnn v
92c0: 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e 20 74 68  alues.** mean th
92d0: 61 74 20 77 72 69 74 65 73 20 6f 66 20 62 6c 6f  at writes of blo
92e0: 63 6b 73 20 74 68 61 74 20 61 72 65 20 6e 6e 6e  cks that are nnn
92f0: 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 61   bytes in size a
9300: 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69 67 6e 65  nd.** are aligne
9310: 64 20 74 6f 20 61 6e 20 61 64 64 72 65 73 73 20  d to an address 
9320: 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e 74 65  which is an inte
9330: 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 0a  ger multiple of.
9340: 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f 6d 69  ** nnn are atomi
9350: 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49  c.  The SQLITE_I
9360: 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44  OCAP_SAFE_APPEND
9370: 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a 2a 2a 20   value means.** 
9380: 74 68 61 74 20 77 68 65 6e 20 64 61 74 61 20 69  that when data i
9390: 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 61 20  s appended to a 
93a0: 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61 20 69  file, the data i
93b0: 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 66 69  s appended.** fi
93c0: 72 73 74 20 74 68 65 6e 20 74 68 65 20 73 69 7a  rst then the siz
93d0: 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73  e of the file is
93e0: 20 65 78 74 65 6e 64 65 64 2c 20 6e 65 76 65 72   extended, never
93f0: 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 77 61   the other.** wa
9400: 79 20 61 72 6f 75 6e 64 2e 20 20 54 68 65 20 53  y around.  The S
9410: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55  QLITE_IOCAP_SEQU
9420: 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 79 20  ENTIAL property 
9430: 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e  means that.** in
9440: 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 77 72 69  formation is wri
9450: 74 74 65 6e 20 74 6f 20 64 69 73 6b 20 69 6e 20  tten to disk in 
9460: 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72 20 61  the same order a
9470: 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 78 57  s calls.** to xW
9480: 72 69 74 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69  rite()..*/.#defi
9490: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  ne SQLITE_IOCAP_
94a0: 41 54 4f 4d 49 43 20 20 20 20 20 20 20 20 20 20  ATOMIC          
94b0: 30 78 30 30 30 30 30 30 30 31 0a 23 64 65 66 69  0x00000001.#defi
94c0: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  ne SQLITE_IOCAP_
94d0: 41 54 4f 4d 49 43 35 31 32 20 20 20 20 20 20 20  ATOMIC512       
94e0: 30 78 30 30 30 30 30 30 30 32 0a 23 64 65 66 69  0x00000002.#defi
94f0: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  ne SQLITE_IOCAP_
9500: 41 54 4f 4d 49 43 31 4b 20 20 20 20 20 20 20 20  ATOMIC1K        
9510: 30 78 30 30 30 30 30 30 30 34 0a 23 64 65 66 69  0x00000004.#defi
9520: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  ne SQLITE_IOCAP_
9530: 41 54 4f 4d 49 43 32 4b 20 20 20 20 20 20 20 20  ATOMIC2K        
9540: 30 78 30 30 30 30 30 30 30 38 0a 23 64 65 66 69  0x00000008.#defi
9550: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  ne SQLITE_IOCAP_
9560: 41 54 4f 4d 49 43 34 4b 20 20 20 20 20 20 20 20  ATOMIC4K        
9570: 30 78 30 30 30 30 30 30 31 30 0a 23 64 65 66 69  0x00000010.#defi
9580: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  ne SQLITE_IOCAP_
9590: 41 54 4f 4d 49 43 38 4b 20 20 20 20 20 20 20 20  ATOMIC8K        
95a0: 30 78 30 30 30 30 30 30 32 30 0a 23 64 65 66 69  0x00000020.#defi
95b0: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  ne SQLITE_IOCAP_
95c0: 41 54 4f 4d 49 43 31 36 4b 20 20 20 20 20 20 20  ATOMIC16K       
95d0: 30 78 30 30 30 30 30 30 34 30 0a 23 64 65 66 69  0x00000040.#defi
95e0: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  ne SQLITE_IOCAP_
95f0: 41 54 4f 4d 49 43 33 32 4b 20 20 20 20 20 20 20  ATOMIC32K       
9600: 30 78 30 30 30 30 30 30 38 30 0a 23 64 65 66 69  0x00000080.#defi
9610: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  ne SQLITE_IOCAP_
9620: 41 54 4f 4d 49 43 36 34 4b 20 20 20 20 20 20 20  ATOMIC64K       
9630: 30 78 30 30 30 30 30 31 30 30 0a 23 64 65 66 69  0x00000100.#defi
9640: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  ne SQLITE_IOCAP_
9650: 53 41 46 45 5f 41 50 50 45 4e 44 20 20 20 20 20  SAFE_APPEND     
9660: 30 78 30 30 30 30 30 32 30 30 0a 23 64 65 66 69  0x00000200.#defi
9670: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  ne SQLITE_IOCAP_
9680: 53 45 51 55 45 4e 54 49 41 4c 20 20 20 20 20 20  SEQUENTIAL      
9690: 30 78 30 30 30 30 30 34 30 30 0a 0a 2f 2a 0a 2a  0x00000400../*.*
96a0: 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69 6c 65  * CAPI3REF: File
96b0: 20 4c 6f 63 6b 69 6e 67 20 4c 65 76 65 6c 73 20   Locking Levels 
96c0: 7b 48 31 30 32 35 30 7d 20 3c 48 31 31 31 32 30  {H10250} <H11120
96d0: 3e 20 3c 48 31 31 33 31 30 3e 0a 2a 2a 0a 2a 2a  > <H11310>.**.**
96e0: 20 53 51 4c 69 74 65 20 75 73 65 73 20 6f 6e 65   SQLite uses one
96f0: 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65   of these intege
9700: 72 20 76 61 6c 75 65 73 20 61 73 20 74 68 65 20  r values as the 
9710: 73 65 63 6f 6e 64 0a 2a 2a 20 61 72 67 75 6d 65  second.** argume
9720: 6e 74 20 74 6f 20 63 61 6c 6c 73 20 69 74 20 6d  nt to calls it m
9730: 61 6b 65 73 20 74 6f 20 74 68 65 20 78 4c 6f 63  akes to the xLoc
9740: 6b 28 29 20 61 6e 64 20 78 55 6e 6c 6f 63 6b 28  k() and xUnlock(
9750: 29 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20  ) methods.** of 
9760: 61 6e 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  an [sqlite3_io_m
9770: 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 0a  ethods] object..
9780: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
9790: 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 20 20 20 20 20  E_LOCK_NONE     
97a0: 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53       0.#define S
97b0: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45  QLITE_LOCK_SHARE
97c0: 44 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69  D        1.#defi
97d0: 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52  ne SQLITE_LOCK_R
97e0: 45 53 45 52 56 45 44 20 20 20 20 20 20 32 0a 23  ESERVED      2.#
97f0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f  define SQLITE_LO
9800: 43 4b 5f 50 45 4e 44 49 4e 47 20 20 20 20 20 20  CK_PENDING      
9810: 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   3.#define SQLIT
9820: 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45  E_LOCK_EXCLUSIVE
9830: 20 20 20 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 43 41       4../*.** CA
9840: 50 49 33 52 45 46 3a 20 53 79 6e 63 68 72 6f 6e  PI3REF: Synchron
9850: 69 7a 61 74 69 6f 6e 20 54 79 70 65 20 46 6c 61  ization Type Fla
9860: 67 73 20 7b 48 31 30 32 36 30 7d 20 3c 48 31 31  gs {H10260} <H11
9870: 31 32 30 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  120>.**.** When 
9880: 53 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 74  SQLite invokes t
9890: 68 65 20 78 53 79 6e 63 28 29 20 6d 65 74 68 6f  he xSync() metho
98a0: 64 20 6f 66 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69  d of an.** [sqli
98b0: 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20  te3_io_methods] 
98c0: 6f 62 6a 65 63 74 20 69 74 20 75 73 65 73 20 61  object it uses a
98d0: 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 0a   combination of.
98e0: 2a 2a 20 74 68 65 73 65 20 69 6e 74 65 67 65 72  ** these integer
98f0: 20 76 61 6c 75 65 73 20 61 73 20 74 68 65 20 73   values as the s
9900: 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a  econd argument..
9910: 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 53  **.** When the S
9920: 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f  QLITE_SYNC_DATAO
9930: 4e 4c 59 20 66 6c 61 67 20 69 73 20 75 73 65 64  NLY flag is used
9940: 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20  , it means that 
9950: 74 68 65 0a 2a 2a 20 73 79 6e 63 20 6f 70 65 72  the.** sync oper
9960: 61 74 69 6f 6e 20 6f 6e 6c 79 20 6e 65 65 64 73  ation only needs
9970: 20 74 6f 20 66 6c 75 73 68 20 64 61 74 61 20 74   to flush data t
9980: 6f 20 6d 61 73 73 20 73 74 6f 72 61 67 65 2e 20  o mass storage. 
9990: 20 49 6e 6f 64 65 0a 2a 2a 20 69 6e 66 6f 72 6d   Inode.** inform
99a0: 61 74 69 6f 6e 20 6e 65 65 64 20 6e 6f 74 20 62  ation need not b
99b0: 65 20 66 6c 75 73 68 65 64 2e 20 49 66 20 74 68  e flushed. If th
99c0: 65 20 6c 6f 77 65 72 20 66 6f 75 72 20 62 69 74  e lower four bit
99d0: 73 20 6f 66 20 74 68 65 20 66 6c 61 67 0a 2a 2a  s of the flag.**
99e0: 20 65 71 75 61 6c 20 53 51 4c 49 54 45 5f 53 59   equal SQLITE_SY
99f0: 4e 43 5f 4e 4f 52 4d 41 4c 2c 20 74 68 61 74 20  NC_NORMAL, that 
9a00: 6d 65 61 6e 73 20 74 6f 20 75 73 65 20 6e 6f 72  means to use nor
9a10: 6d 61 6c 20 66 73 79 6e 63 28 29 20 73 65 6d 61  mal fsync() sema
9a20: 6e 74 69 63 73 2e 0a 2a 2a 20 49 66 20 74 68 65  ntics..** If the
9a30: 20 6c 6f 77 65 72 20 66 6f 75 72 20 62 69 74 73   lower four bits
9a40: 20 65 71 75 61 6c 20 53 51 4c 49 54 45 5f 53 59   equal SQLITE_SY
9a50: 4e 43 5f 46 55 4c 4c 2c 20 74 68 61 74 20 6d 65  NC_FULL, that me
9a60: 61 6e 73 0a 2a 2a 20 74 6f 20 75 73 65 20 4d 61  ans.** to use Ma
9a70: 63 20 4f 53 20 58 20 73 74 79 6c 65 20 66 75 6c  c OS X style ful
9a80: 6c 73 79 6e 63 20 69 6e 73 74 65 61 64 20 6f 66  lsync instead of
9a90: 20 66 73 79 6e 63 28 29 2e 0a 2a 2f 0a 23 64 65   fsync()..*/.#de
9aa0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 4e 43  fine SQLITE_SYNC
9ab0: 5f 4e 4f 52 4d 41 4c 20 20 20 20 20 20 20 20 30  _NORMAL        0
9ac0: 78 30 30 30 30 32 0a 23 64 65 66 69 6e 65 20 53  x00002.#define S
9ad0: 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 20  QLITE_SYNC_FULL 
9ae0: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 33           0x00003
9af0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
9b00: 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20 20 20  SYNC_DATAONLY   
9b10: 20 20 20 30 78 30 30 30 31 30 0a 0a 2f 2a 0a 2a     0x00010../*.*
9b20: 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49  * CAPI3REF: OS I
9b30: 6e 74 65 72 66 61 63 65 20 4f 70 65 6e 20 46 69  nterface Open Fi
9b40: 6c 65 20 48 61 6e 64 6c 65 20 7b 48 31 31 31 31  le Handle {H1111
9b50: 30 7d 20 3c 53 32 30 31 31 30 3e 0a 2a 2a 0a 2a  0} <S20110>.**.*
9b60: 2a 20 41 6e 20 5b 73 71 6c 69 74 65 33 5f 66 69  * An [sqlite3_fi
9b70: 6c 65 5d 20 6f 62 6a 65 63 74 20 72 65 70 72 65  le] object repre
9b80: 73 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20 66 69  sents an open fi
9b90: 6c 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 5b 73  le in the .** [s
9ba0: 71 6c 69 74 65 33 5f 76 66 73 20 7c 20 4f 53 20  qlite3_vfs | OS 
9bb0: 69 6e 74 65 72 66 61 63 65 20 6c 61 79 65 72 5d  interface layer]
9bc0: 2e 20 20 49 6e 64 69 76 69 64 75 61 6c 20 4f 53  .  Individual OS
9bd0: 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 69 6d   interface.** im
9be0: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69  plementations wi
9bf0: 6c 6c 0a 2a 2a 20 77 61 6e 74 20 74 6f 20 73 75  ll.** want to su
9c00: 62 63 6c 61 73 73 20 74 68 69 73 20 6f 62 6a 65  bclass this obje
9c10: 63 74 20 62 79 20 61 70 70 65 6e 64 69 6e 67 20  ct by appending 
9c20: 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64  additional field
9c30: 73 0a 2a 2a 20 66 6f 72 20 74 68 65 69 72 20 6f  s.** for their o
9c40: 77 6e 20 75 73 65 2e 20 20 54 68 65 20 70 4d 65  wn use.  The pMe
9c50: 74 68 6f 64 73 20 65 6e 74 72 79 20 69 73 20 61  thods entry is a
9c60: 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a   pointer to an.*
9c70: 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  * [sqlite3_io_me
9c80: 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 74 68  thods] object th
9c90: 61 74 20 64 65 66 69 6e 65 73 20 6d 65 74 68 6f  at defines metho
9ca0: 64 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 69 6e  ds for performin
9cb0: 67 0a 2a 2a 20 49 2f 4f 20 6f 70 65 72 61 74 69  g.** I/O operati
9cc0: 6f 6e 73 20 6f 6e 20 74 68 65 20 6f 70 65 6e 20  ons on the open 
9cd0: 66 69 6c 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  file..*/.typedef
9ce0: 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
9cf0: 66 69 6c 65 20 73 71 6c 69 74 65 33 5f 66 69 6c  file sqlite3_fil
9d00: 65 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  e;.struct sqlite
9d10: 33 5f 66 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74  3_file {.  const
9d20: 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
9d30: 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74  io_methods *pMet
9d40: 68 6f 64 73 3b 20 20 2f 2a 20 4d 65 74 68 6f 64  hods;  /* Method
9d50: 73 20 66 6f 72 20 61 6e 20 6f 70 65 6e 20 66 69  s for an open fi
9d60: 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  le */.};../*.** 
9d70: 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49 6e 74  CAPI3REF: OS Int
9d80: 65 72 66 61 63 65 20 46 69 6c 65 20 56 69 72 74  erface File Virt
9d90: 75 61 6c 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65  ual Methods Obje
9da0: 63 74 20 7b 48 31 31 31 32 30 7d 20 3c 53 32 30  ct {H11120} <S20
9db0: 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79  110>.**.** Every
9dc0: 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 62 79 20   file opened by 
9dd0: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73  the [sqlite3_vfs
9de0: 5d 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 70  ] xOpen method p
9df0: 6f 70 75 6c 61 74 65 73 20 61 6e 0a 2a 2a 20 5b  opulates an.** [
9e00: 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62  sqlite3_file] ob
9e10: 6a 65 63 74 20 28 6f 72 2c 20 6d 6f 72 65 20 63  ject (or, more c
9e20: 6f 6d 6d 6f 6e 6c 79 2c 20 61 20 73 75 62 63 6c  ommonly, a subcl
9e30: 61 73 73 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73  ass of the.** [s
9e40: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a  qlite3_file] obj
9e50: 65 63 74 29 20 77 69 74 68 20 61 20 70 6f 69 6e  ect) with a poin
9e60: 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e  ter to an instan
9e70: 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63  ce of this objec
9e80: 74 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63  t..** This objec
9e90: 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 6d 65  t defines the me
9ea0: 74 68 6f 64 73 20 75 73 65 64 20 74 6f 20 70 65  thods used to pe
9eb0: 72 66 6f 72 6d 20 76 61 72 69 6f 75 73 20 6f 70  rform various op
9ec0: 65 72 61 74 69 6f 6e 73 0a 2a 2a 20 61 67 61 69  erations.** agai
9ed0: 6e 73 74 20 74 68 65 20 6f 70 65 6e 20 66 69 6c  nst the open fil
9ee0: 65 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 79  e represented by
9ef0: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69   the [sqlite3_fi
9f00: 6c 65 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a  le] object..**.*
9f10: 2a 20 49 66 20 74 68 65 20 78 4f 70 65 6e 20 6d  * If the xOpen m
9f20: 65 74 68 6f 64 20 73 65 74 73 20 74 68 65 20 73  ethod sets the s
9f30: 71 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d 65 74  qlite3_file.pMet
9f40: 68 6f 64 73 20 65 6c 65 6d 65 6e 74 20 0a 2a 2a  hods element .**
9f50: 20 74 6f 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70   to a non-NULL p
9f60: 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 74 68 65  ointer, then the
9f70: 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
9f80: 6f 64 73 2e 78 43 6c 6f 73 65 20 6d 65 74 68 6f  ods.xClose metho
9f90: 64 0a 2a 2a 20 6d 61 79 20 62 65 20 69 6e 76 6f  d.** may be invo
9fa0: 6b 65 64 20 65 76 65 6e 20 69 66 20 74 68 65 20  ked even if the 
9fb0: 78 4f 70 65 6e 20 72 65 70 6f 72 74 65 64 20 74  xOpen reported t
9fc0: 68 61 74 20 69 74 20 66 61 69 6c 65 64 2e 20 20  hat it failed.  
9fd0: 54 68 65 0a 2a 2a 20 6f 6e 6c 79 20 77 61 79 20  The.** only way 
9fe0: 74 6f 20 70 72 65 76 65 6e 74 20 61 20 63 61 6c  to prevent a cal
9ff0: 6c 20 74 6f 20 78 43 6c 6f 73 65 20 66 6f 6c 6c  l to xClose foll
a000: 6f 77 69 6e 67 20 61 20 66 61 69 6c 65 64 20 78  owing a failed x
a010: 4f 70 65 6e 0a 2a 2a 20 69 73 20 66 6f 72 20 74  Open.** is for t
a020: 68 65 20 78 4f 70 65 6e 20 74 6f 20 73 65 74 20  he xOpen to set 
a030: 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  the sqlite3_file
a040: 2e 70 4d 65 74 68 6f 64 73 20 65 6c 65 6d 65 6e  .pMethods elemen
a050: 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  t to NULL..**.**
a060: 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d   The flags argum
a070: 65 6e 74 20 74 6f 20 78 53 79 6e 63 20 6d 61 79  ent to xSync may
a080: 20 62 65 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49   be one of [SQLI
a090: 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 5d 20  TE_SYNC_NORMAL] 
a0a0: 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 53 59  or.** [SQLITE_SY
a0b0: 4e 43 5f 46 55 4c 4c 5d 2e 20 20 54 68 65 20 66  NC_FULL].  The f
a0c0: 69 72 73 74 20 63 68 6f 69 63 65 20 69 73 20 74  irst choice is t
a0d0: 68 65 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 28  he normal fsync(
a0e0: 29 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64  )..** The second
a0f0: 20 63 68 6f 69 63 65 20 69 73 20 61 20 4d 61 63   choice is a Mac
a100: 20 4f 53 20 58 20 73 74 79 6c 65 20 66 75 6c 6c   OS X style full
a110: 73 79 6e 63 2e 20 20 54 68 65 20 5b 53 51 4c 49  sync.  The [SQLI
a120: 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59  TE_SYNC_DATAONLY
a130: 5d 0a 2a 2a 20 66 6c 61 67 20 6d 61 79 20 62 65  ].** flag may be
a140: 20 4f 52 65 64 20 69 6e 20 74 6f 20 69 6e 64 69   ORed in to indi
a150: 63 61 74 65 20 74 68 61 74 20 6f 6e 6c 79 20 74  cate that only t
a160: 68 65 20 64 61 74 61 20 6f 66 20 74 68 65 20 66  he data of the f
a170: 69 6c 65 0a 2a 2a 20 61 6e 64 20 6e 6f 74 20 69  ile.** and not i
a180: 74 73 20 69 6e 6f 64 65 20 6e 65 65 64 73 20 74  ts inode needs t
a190: 6f 20 62 65 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a  o be synced..**.
a1a0: 2a 2a 20 54 68 65 20 69 6e 74 65 67 65 72 20 76  ** The integer v
a1b0: 61 6c 75 65 73 20 74 6f 20 78 4c 6f 63 6b 28 29  alues to xLock()
a1c0: 20 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 20 61   and xUnlock() a
a1d0: 72 65 20 6f 6e 65 20 6f 66 0a 2a 2a 20 3c 75 6c  re one of.** <ul
a1e0: 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54  >.** <li> [SQLIT
a1f0: 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 0a 2a 2a  E_LOCK_NONE],.**
a200: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f   <li> [SQLITE_LO
a210: 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a 20 3c  CK_SHARED],.** <
a220: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b  li> [SQLITE_LOCK
a230: 5f 52 45 53 45 52 56 45 44 5d 2c 0a 2a 2a 20 3c  _RESERVED],.** <
a240: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b  li> [SQLITE_LOCK
a250: 5f 50 45 4e 44 49 4e 47 5d 2c 20 6f 72 0a 2a 2a  _PENDING], or.**
a260: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f   <li> [SQLITE_LO
a270: 43 4b 5f 45 58 43 4c 55 53 49 56 45 5d 2e 0a 2a  CK_EXCLUSIVE]..*
a280: 2a 20 3c 2f 75 6c 3e 0a 2a 2a 20 78 4c 6f 63 6b  * </ul>.** xLock
a290: 28 29 20 69 6e 63 72 65 61 73 65 73 20 74 68 65  () increases the
a2a0: 20 6c 6f 63 6b 2e 20 78 55 6e 6c 6f 63 6b 28 29   lock. xUnlock()
a2b0: 20 64 65 63 72 65 61 73 65 73 20 74 68 65 20 6c   decreases the l
a2c0: 6f 63 6b 2e 0a 2a 2a 20 54 68 65 20 78 43 68 65  ock..** The xChe
a2d0: 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 29  ckReservedLock()
a2e0: 20 6d 65 74 68 6f 64 20 63 68 65 63 6b 73 20 77   method checks w
a2f0: 68 65 74 68 65 72 20 61 6e 79 20 64 61 74 61 62  hether any datab
a300: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 0a  ase connection,.
a310: 2a 2a 20 65 69 74 68 65 72 20 69 6e 20 74 68 69  ** either in thi
a320: 73 20 70 72 6f 63 65 73 73 20 6f 72 20 69 6e 20  s process or in 
a330: 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65  some other proce
a340: 73 73 2c 20 69 73 20 68 6f 6c 64 69 6e 67 20 61  ss, is holding a
a350: 20 52 45 53 45 52 56 45 44 2c 0a 2a 2a 20 50 45   RESERVED,.** PE
a360: 4e 44 49 4e 47 2c 20 6f 72 20 45 58 43 4c 55 53  NDING, or EXCLUS
a370: 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  IVE lock on the 
a380: 66 69 6c 65 2e 20 20 49 74 20 72 65 74 75 72 6e  file.  It return
a390: 73 20 74 72 75 65 0a 2a 2a 20 69 66 20 73 75 63  s true.** if suc
a3a0: 68 20 61 20 6c 6f 63 6b 20 65 78 69 73 74 73 20  h a lock exists 
a3b0: 61 6e 64 20 66 61 6c 73 65 20 6f 74 68 65 72 77  and false otherw
a3c0: 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  ise..**.** The x
a3d0: 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 29 20 6d 65  FileControl() me
a3e0: 74 68 6f 64 20 69 73 20 61 20 67 65 6e 65 72 69  thod is a generi
a3f0: 63 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74  c interface that
a400: 20 61 6c 6c 6f 77 73 20 63 75 73 74 6f 6d 0a 2a   allows custom.*
a410: 2a 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61  * VFS implementa
a420: 74 69 6f 6e 73 20 74 6f 20 64 69 72 65 63 74 6c  tions to directl
a430: 79 20 63 6f 6e 74 72 6f 6c 20 61 6e 20 6f 70 65  y control an ope
a440: 6e 20 66 69 6c 65 20 75 73 69 6e 67 20 74 68 65  n file using the
a450: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c  .** [sqlite3_fil
a460: 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e 74  e_control()] int
a470: 65 72 66 61 63 65 2e 20 20 54 68 65 20 73 65 63  erface.  The sec
a480: 6f 6e 64 20 22 6f 70 22 20 61 72 67 75 6d 65 6e  ond "op" argumen
a490: 74 20 69 73 20 61 6e 0a 2a 2a 20 69 6e 74 65 67  t is an.** integ
a4a0: 65 72 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 20  er opcode.  The 
a4b0: 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69  third argument i
a4c0: 73 20 61 20 67 65 6e 65 72 69 63 20 70 6f 69 6e  s a generic poin
a4d0: 74 65 72 20 69 6e 74 65 6e 64 65 64 20 74 6f 0a  ter intended to.
a4e0: 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74  ** point to a st
a4f0: 72 75 63 74 75 72 65 20 74 68 61 74 20 6d 61 79  ructure that may
a500: 20 63 6f 6e 74 61 69 6e 20 61 72 67 75 6d 65 6e   contain argumen
a510: 74 73 20 6f 72 20 73 70 61 63 65 20 69 6e 20 77  ts or space in w
a520: 68 69 63 68 20 74 6f 0a 2a 2a 20 77 72 69 74 65  hich to.** write
a530: 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73 2e 20   return values. 
a540: 20 50 6f 74 65 6e 74 69 61 6c 20 75 73 65 73 20   Potential uses 
a550: 66 6f 72 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c  for xFileControl
a560: 28 29 20 6d 69 67 68 74 20 62 65 0a 2a 2a 20 66  () might be.** f
a570: 75 6e 63 74 69 6f 6e 73 20 74 6f 20 65 6e 61 62  unctions to enab
a580: 6c 65 20 62 6c 6f 63 6b 69 6e 67 20 6c 6f 63 6b  le blocking lock
a590: 73 20 77 69 74 68 20 74 69 6d 65 6f 75 74 73 2c  s with timeouts,
a5a0: 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 0a 2a   to change the.*
a5b0: 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65  * locking strate
a5c0: 67 79 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20  gy (for example 
a5d0: 74 6f 20 75 73 65 20 64 6f 74 2d 66 69 6c 65 20  to use dot-file 
a5e0: 6c 6f 63 6b 73 29 2c 20 74 6f 20 69 6e 71 75 69  locks), to inqui
a5f0: 72 65 0a 2a 2a 20 61 62 6f 75 74 20 74 68 65 20  re.** about the 
a600: 73 74 61 74 75 73 20 6f 66 20 61 20 6c 6f 63 6b  status of a lock
a610: 2c 20 6f 72 20 74 6f 20 62 72 65 61 6b 20 73 74  , or to break st
a620: 61 6c 65 20 6c 6f 63 6b 73 2e 20 20 54 68 65 20  ale locks.  The 
a630: 53 51 4c 69 74 65 0a 2a 2a 20 63 6f 72 65 20 72  SQLite.** core r
a640: 65 73 65 72 76 65 73 20 61 6c 6c 20 6f 70 63 6f  eserves all opco
a650: 64 65 73 20 6c 65 73 73 20 74 68 61 6e 20 31 30  des less than 10
a660: 30 20 66 6f 72 20 69 74 73 20 6f 77 6e 20 75 73  0 for its own us
a670: 65 2e 0a 2a 2a 20 41 20 5b 53 51 4c 49 54 45 5f  e..** A [SQLITE_
a680: 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20  FCNTL_LOCKSTATE 
a690: 7c 20 6c 69 73 74 20 6f 66 20 6f 70 63 6f 64 65  | list of opcode
a6a0: 73 5d 20 6c 65 73 73 20 74 68 61 6e 20 31 30 30  s] less than 100
a6b0: 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a   is available..*
a6c0: 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74  * Applications t
a6d0: 68 61 74 20 64 65 66 69 6e 65 20 61 20 63 75 73  hat define a cus
a6e0: 74 6f 6d 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c  tom xFileControl
a6f0: 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 20 75   method should u
a700: 73 65 20 6f 70 63 6f 64 65 73 0a 2a 2a 20 67 72  se opcodes.** gr
a710: 65 61 74 65 72 20 74 68 61 6e 20 31 30 30 20 74  eater than 100 t
a720: 6f 20 61 76 6f 69 64 20 63 6f 6e 66 6c 69 63 74  o avoid conflict
a730: 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 53 65  s..**.** The xSe
a740: 63 74 6f 72 53 69 7a 65 28 29 20 6d 65 74 68 6f  ctorSize() metho
a750: 64 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 65  d returns the se
a760: 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 74 68 65  ctor size of the
a770: 0a 2a 2a 20 64 65 76 69 63 65 20 74 68 61 74 20  .** device that 
a780: 75 6e 64 65 72 6c 69 65 73 20 74 68 65 20 66 69  underlies the fi
a790: 6c 65 2e 20 20 54 68 65 20 73 65 63 74 6f 72 20  le.  The sector 
a7a0: 73 69 7a 65 20 69 73 20 74 68 65 0a 2a 2a 20 6d  size is the.** m
a7b0: 69 6e 69 6d 75 6d 20 77 72 69 74 65 20 74 68 61  inimum write tha
a7c0: 74 20 63 61 6e 20 62 65 20 70 65 72 66 6f 72 6d  t can be perform
a7d0: 65 64 20 77 69 74 68 6f 75 74 20 64 69 73 74 75  ed without distu
a7e0: 72 62 69 6e 67 0a 2a 2a 20 6f 74 68 65 72 20 62  rbing.** other b
a7f0: 79 74 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65  ytes in the file
a800: 2e 20 20 54 68 65 20 78 44 65 76 69 63 65 43 68  .  The xDeviceCh
a810: 61 72 61 63 74 65 72 69 73 74 69 63 73 28 29 0a  aracteristics().
a820: 2a 2a 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e  ** method return
a830: 73 20 61 20 62 69 74 20 76 65 63 74 6f 72 20 64  s a bit vector d
a840: 65 73 63 72 69 62 69 6e 67 20 62 65 68 61 76 69  escribing behavi
a850: 6f 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 75 6e  ors of the.** un
a860: 64 65 72 6c 79 69 6e 67 20 64 65 76 69 63 65 3a  derlying device:
a870: 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c  .**.** <ul>.** <
a880: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41  li> [SQLITE_IOCA
a890: 50 5f 41 54 4f 4d 49 43 5d 0a 2a 2a 20 3c 6c 69  P_ATOMIC].** <li
a8a0: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  > [SQLITE_IOCAP_
a8b0: 41 54 4f 4d 49 43 35 31 32 5d 0a 2a 2a 20 3c 6c  ATOMIC512].** <l
a8c0: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50  i> [SQLITE_IOCAP
a8d0: 5f 41 54 4f 4d 49 43 31 4b 5d 0a 2a 2a 20 3c 6c  _ATOMIC1K].** <l
a8e0: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50  i> [SQLITE_IOCAP
a8f0: 5f 41 54 4f 4d 49 43 32 4b 5d 0a 2a 2a 20 3c 6c  _ATOMIC2K].** <l
a900: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50  i> [SQLITE_IOCAP
a910: 5f 41 54 4f 4d 49 43 34 4b 5d 0a 2a 2a 20 3c 6c  _ATOMIC4K].** <l
a920: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50  i> [SQLITE_IOCAP
a930: 5f 41 54 4f 4d 49 43 38 4b 5d 0a 2a 2a 20 3c 6c  _ATOMIC8K].** <l
a940: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50  i> [SQLITE_IOCAP
a950: 5f 41 54 4f 4d 49 43 31 36 4b 5d 0a 2a 2a 20 3c  _ATOMIC16K].** <
a960: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41  li> [SQLITE_IOCA
a970: 50 5f 41 54 4f 4d 49 43 33 32 4b 5d 0a 2a 2a 20  P_ATOMIC32K].** 
a980: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43  <li> [SQLITE_IOC
a990: 41 50 5f 41 54 4f 4d 49 43 36 34 4b 5d 0a 2a 2a  AP_ATOMIC64K].**
a9a0: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f   <li> [SQLITE_IO
a9b0: 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 5d  CAP_SAFE_APPEND]
a9c0: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
a9d0: 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41  _IOCAP_SEQUENTIA
a9e0: 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a  L].** </ul>.**.*
a9f0: 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43  * The SQLITE_IOC
aa00: 41 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70 65 72  AP_ATOMIC proper
aa10: 74 79 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6c  ty means that al
aa20: 6c 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a 20 61  l writes of.** a
aa30: 6e 79 20 73 69 7a 65 20 61 72 65 20 61 74 6f 6d  ny size are atom
aa40: 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f  ic.  The SQLITE_
aa50: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e 20  IOCAP_ATOMICnnn 
aa60: 76 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e 20 74  values.** mean t
aa70: 68 61 74 20 77 72 69 74 65 73 20 6f 66 20 62 6c  hat writes of bl
aa80: 6f 63 6b 73 20 74 68 61 74 20 61 72 65 20 6e 6e  ocks that are nn
aa90: 6e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20  n bytes in size 
aaa0: 61 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69 67 6e  and.** are align
aab0: 65 64 20 74 6f 20 61 6e 20 61 64 64 72 65 73 73  ed to an address
aac0: 20 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e 74   which is an int
aad0: 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66  eger multiple of
aae0: 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f 6d  .** nnn are atom
aaf0: 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f  ic.  The SQLITE_
ab00: 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e  IOCAP_SAFE_APPEN
ab10: 44 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a 2a 2a  D value means.**
ab20: 20 74 68 61 74 20 77 68 65 6e 20 64 61 74 61 20   that when data 
ab30: 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 61  is appended to a
ab40: 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61 20   file, the data 
ab50: 69 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 66  is appended.** f
ab60: 69 72 73 74 20 74 68 65 6e 20 74 68 65 20 73 69  irst then the si
ab70: 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69  ze of the file i
ab80: 73 20 65 78 74 65 6e 64 65 64 2c 20 6e 65 76 65  s extended, neve
ab90: 72 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 77  r the other.** w
aba0: 61 79 20 61 72 6f 75 6e 64 2e 20 20 54 68 65 20  ay around.  The 
abb0: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51  SQLITE_IOCAP_SEQ
abc0: 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 79  UENTIAL property
abd0: 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 69   means that.** i
abe0: 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 77 72  nformation is wr
abf0: 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 20 69 6e  itten to disk in
ac00: 20 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72 20   the same order 
ac10: 61 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 78  as calls.** to x
ac20: 57 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49  Write()..**.** I
ac30: 66 20 78 52 65 61 64 28 29 20 72 65 74 75 72 6e  f xRead() return
ac40: 73 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53  s SQLITE_IOERR_S
ac50: 48 4f 52 54 5f 52 45 41 44 20 69 74 20 6d 75 73  HORT_READ it mus
ac60: 74 20 61 6c 73 6f 20 66 69 6c 6c 0a 2a 2a 20 69  t also fill.** i
ac70: 6e 20 74 68 65 20 75 6e 72 65 61 64 20 70 6f 72  n the unread por
ac80: 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 62 75 66  tions of the buf
ac90: 66 65 72 20 77 69 74 68 20 7a 65 72 6f 73 2e 20  fer with zeros. 
aca0: 20 41 20 56 46 53 20 74 68 61 74 0a 2a 2a 20 66   A VFS that.** f
acb0: 61 69 6c 73 20 74 6f 20 7a 65 72 6f 2d 66 69 6c  ails to zero-fil
acc0: 6c 20 73 68 6f 72 74 20 72 65 61 64 73 20 6d 69  l short reads mi
acd0: 67 68 74 20 73 65 65 6d 20 74 6f 20 77 6f 72 6b  ght seem to work
ace0: 2e 20 20 48 6f 77 65 76 65 72 2c 0a 2a 2a 20 66  .  However,.** f
acf0: 61 69 6c 75 72 65 20 74 6f 20 7a 65 72 6f 2d 66  ailure to zero-f
ad00: 69 6c 6c 20 73 68 6f 72 74 20 72 65 61 64 73 20  ill short reads 
ad10: 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 20  will eventually 
ad20: 6c 65 61 64 20 74 6f 0a 2a 2a 20 64 61 74 61 62  lead to.** datab
ad30: 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a  ase corruption..
ad40: 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
ad50: 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
ad60: 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 69 6f 5f  hods sqlite3_io_
ad70: 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20  methods;.struct 
ad80: 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
ad90: 64 73 20 7b 0a 20 20 69 6e 74 20 69 56 65 72 73  ds {.  int iVers
ada0: 69 6f 6e 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c  ion;.  int (*xCl
adb0: 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  ose)(sqlite3_fil
adc0: 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 65  e*);.  int (*xRe
add0: 61 64 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ad)(sqlite3_file
ade0: 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 69 41  *, void*, int iA
adf0: 6d 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  mt, sqlite3_int6
ae00: 34 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e 74 20  4 iOfst);.  int 
ae10: 28 2a 78 57 72 69 74 65 29 28 73 71 6c 69 74 65  (*xWrite)(sqlite
ae20: 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 74 20 76  3_file*, const v
ae30: 6f 69 64 2a 2c 20 69 6e 74 20 69 41 6d 74 2c 20  oid*, int iAmt, 
ae40: 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f  sqlite3_int64 iO
ae50: 66 73 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 54  fst);.  int (*xT
ae60: 72 75 6e 63 61 74 65 29 28 73 71 6c 69 74 65 33  runcate)(sqlite3
ae70: 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65 33 5f  _file*, sqlite3_
ae80: 69 6e 74 36 34 20 73 69 7a 65 29 3b 0a 20 20 69  int64 size);.  i
ae90: 6e 74 20 28 2a 78 53 79 6e 63 29 28 73 71 6c 69  nt (*xSync)(sqli
aea0: 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 66  te3_file*, int f
aeb0: 6c 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78  lags);.  int (*x
aec0: 46 69 6c 65 53 69 7a 65 29 28 73 71 6c 69 74 65  FileSize)(sqlite
aed0: 33 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65 33  3_file*, sqlite3
aee0: 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 3b 0a  _int64 *pSize);.
aef0: 20 20 69 6e 74 20 28 2a 78 4c 6f 63 6b 29 28 73    int (*xLock)(s
af00: 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e  qlite3_file*, in
af10: 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 55 6e 6c  t);.  int (*xUnl
af20: 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  ock)(sqlite3_fil
af30: 65 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20  e*, int);.  int 
af40: 28 2a 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  (*xCheckReserved
af50: 4c 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69  Lock)(sqlite3_fi
af60: 6c 65 2a 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75  le*, int *pResOu
af70: 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 69 6c  t);.  int (*xFil
af80: 65 43 6f 6e 74 72 6f 6c 29 28 73 71 6c 69 74 65  eControl)(sqlite
af90: 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 6f 70 2c  3_file*, int op,
afa0: 20 76 6f 69 64 20 2a 70 41 72 67 29 3b 0a 20 20   void *pArg);.  
afb0: 69 6e 74 20 28 2a 78 53 65 63 74 6f 72 53 69 7a  int (*xSectorSiz
afc0: 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  e)(sqlite3_file*
afd0: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 65 76 69  );.  int (*xDevi
afe0: 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
aff0: 73 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  s)(sqlite3_file*
b000: 29 3b 0a 20 20 2f 2a 20 41 64 64 69 74 69 6f 6e  );.  /* Addition
b010: 61 6c 20 6d 65 74 68 6f 64 73 20 6d 61 79 20 62  al methods may b
b020: 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72  e added in futur
b030: 65 20 72 65 6c 65 61 73 65 73 20 2a 2f 0a 7d 3b  e releases */.};
b040: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
b050: 3a 20 53 74 61 6e 64 61 72 64 20 46 69 6c 65 20  : Standard File 
b060: 43 6f 6e 74 72 6f 6c 20 4f 70 63 6f 64 65 73 20  Control Opcodes 
b070: 7b 48 31 31 33 31 30 7d 20 3c 53 33 30 38 30 30  {H11310} <S30800
b080: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 6e  >.**.** These in
b090: 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 20  teger constants 
b0a0: 61 72 65 20 6f 70 63 6f 64 65 73 20 66 6f 72 20  are opcodes for 
b0b0: 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c  the xFileControl
b0c0: 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68   method.** of th
b0d0: 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  e [sqlite3_io_me
b0e0: 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 61 6e  thods] object an
b0f0: 64 20 66 6f 72 20 74 68 65 20 5b 73 71 6c 69 74  d for the [sqlit
b100: 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28  e3_file_control(
b110: 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e  )].** interface.
b120: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49  .**.** The [SQLI
b130: 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41  TE_FCNTL_LOCKSTA
b140: 54 45 5d 20 6f 70 63 6f 64 65 20 69 73 20 75 73  TE] opcode is us
b150: 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67  ed for debugging
b160: 2e 20 20 54 68 69 73 0a 2a 2a 20 6f 70 63 6f 64  .  This.** opcod
b170: 65 20 63 61 75 73 65 73 20 74 68 65 20 78 46 69  e causes the xFi
b180: 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64  leControl method
b190: 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 75   to write the cu
b1a0: 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 0a 2a  rrent state of.*
b1b0: 2a 20 74 68 65 20 6c 6f 63 6b 20 28 6f 6e 65 20  * the lock (one 
b1c0: 6f 66 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f  of [SQLITE_LOCK_
b1d0: 4e 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c  NONE], [SQLITE_L
b1e0: 4f 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a 20  OCK_SHARED],.** 
b1f0: 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 53  [SQLITE_LOCK_RES
b200: 45 52 56 45 44 5d 2c 20 5b 53 51 4c 49 54 45 5f  ERVED], [SQLITE_
b210: 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c 20 6f  LOCK_PENDING], o
b220: 72 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 45  r [SQLITE_LOCK_E
b230: 58 43 4c 55 53 49 56 45 5d 29 0a 2a 2a 20 69 6e  XCLUSIVE]).** in
b240: 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68  to an integer th
b250: 61 74 20 74 68 65 20 70 41 72 67 20 61 72 67 75  at the pArg argu
b260: 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f 2e 20  ment points to. 
b270: 54 68 69 73 20 63 61 70 61 62 69 6c 69 74 79 0a  This capability.
b280: 2a 2a 20 69 73 20 75 73 65 64 20 64 75 72 69 6e  ** is used durin
b290: 67 20 74 65 73 74 69 6e 67 20 61 6e 64 20 6f 6e  g testing and on
b2a0: 6c 79 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73  ly needs to be s
b2b0: 75 70 70 6f 72 74 65 64 20 77 68 65 6e 20 53 51  upported when SQ
b2c0: 4c 49 54 45 5f 54 45 53 54 0a 2a 2a 20 69 73 20  LITE_TEST.** is 
b2d0: 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 64 65 66  defined..*/.#def
b2e0: 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c  ine SQLITE_FCNTL
b2f0: 5f 4c 4f 43 4b 53 54 41 54 45 20 20 20 20 20 20  _LOCKSTATE      
b300: 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    1.#define SQLI
b310: 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59  TE_GET_LOCKPROXY
b320: 46 49 4c 45 20 20 20 20 20 20 32 0a 23 64 65 66  FILE      2.#def
b330: 69 6e 65 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c  ine SQLITE_SET_L
b340: 4f 43 4b 50 52 4f 58 59 46 49 4c 45 20 20 20 20  OCKPROXYFILE    
b350: 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    3.#define SQLI
b360: 54 45 5f 4c 41 53 54 5f 45 52 52 4e 4f 20 20 20  TE_LAST_ERRNO   
b370: 20 20 20 20 20 20 20 20 20 20 34 0a 0a 2f 2a 0a            4../*.
b380: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74  ** CAPI3REF: Mut
b390: 65 78 20 48 61 6e 64 6c 65 20 7b 48 31 37 31 31  ex Handle {H1711
b3a0: 30 7d 20 3c 53 32 30 31 33 30 3e 0a 2a 2a 0a 2a  0} <S20130>.**.*
b3b0: 2a 20 54 68 65 20 6d 75 74 65 78 20 6d 6f 64 75  * The mutex modu
b3c0: 6c 65 20 77 69 74 68 69 6e 20 53 51 4c 69 74 65  le within SQLite
b3d0: 20 64 65 66 69 6e 65 73 20 5b 73 71 6c 69 74 65   defines [sqlite
b3e0: 33 5f 6d 75 74 65 78 5d 20 74 6f 20 62 65 20 61  3_mutex] to be a
b3f0: 6e 0a 2a 2a 20 61 62 73 74 72 61 63 74 20 74 79  n.** abstract ty
b400: 70 65 20 66 6f 72 20 61 20 6d 75 74 65 78 20 6f  pe for a mutex o
b410: 62 6a 65 63 74 2e 20 20 54 68 65 20 53 51 4c 69  bject.  The SQLi
b420: 74 65 20 63 6f 72 65 20 6e 65 76 65 72 20 6c 6f  te core never lo
b430: 6f 6b 73 0a 2a 2a 20 61 74 20 74 68 65 20 69 6e  oks.** at the in
b440: 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74  ternal represent
b450: 61 74 69 6f 6e 20 6f 66 20 61 6e 20 5b 73 71 6c  ation of an [sql
b460: 69 74 65 33 5f 6d 75 74 65 78 5d 2e 20 20 49 74  ite3_mutex].  It
b470: 20 6f 6e 6c 79 0a 2a 2a 20 64 65 61 6c 73 20 77   only.** deals w
b480: 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  ith pointers to 
b490: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74  the [sqlite3_mut
b4a0: 65 78 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a  ex] object..**.*
b4b0: 2a 20 4d 75 74 65 78 65 73 20 61 72 65 20 63 72  * Mutexes are cr
b4c0: 65 61 74 65 64 20 75 73 69 6e 67 20 5b 73 71 6c  eated using [sql
b4d0: 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
b4e0: 28 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ()]..*/.typedef 
b4f0: 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d  struct sqlite3_m
b500: 75 74 65 78 20 73 71 6c 69 74 65 33 5f 6d 75 74  utex sqlite3_mut
b510: 65 78 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  ex;../*.** CAPI3
b520: 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 63  REF: OS Interfac
b530: 65 20 4f 62 6a 65 63 74 20 7b 48 31 31 31 34 30  e Object {H11140
b540: 7d 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a  } <S20100>.**.**
b550: 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
b560: 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20  the sqlite3_vfs 
b570: 6f 62 6a 65 63 74 20 64 65 66 69 6e 65 73 20 74  object defines t
b580: 68 65 20 69 6e 74 65 72 66 61 63 65 20 62 65 74  he interface bet
b590: 77 65 65 6e 0a 2a 2a 20 74 68 65 20 53 51 4c 69  ween.** the SQLi
b5a0: 74 65 20 63 6f 72 65 20 61 6e 64 20 74 68 65 20  te core and the 
b5b0: 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61  underlying opera
b5c0: 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 54 68  ting system.  Th
b5d0: 65 20 22 76 66 73 22 0a 2a 2a 20 69 6e 20 74 68  e "vfs".** in th
b5e0: 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6f 62  e name of the ob
b5f0: 6a 65 63 74 20 73 74 61 6e 64 73 20 66 6f 72 20  ject stands for 
b600: 22 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79  "virtual file sy
b610: 73 74 65 6d 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  stem"..**.** The
b620: 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 56   value of the iV
b630: 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 69 73 20  ersion field is 
b640: 69 6e 69 74 69 61 6c 6c 79 20 31 20 62 75 74 20  initially 1 but 
b650: 6d 61 79 20 62 65 20 6c 61 72 67 65 72 20 69 6e  may be larger in
b660: 0a 2a 2a 20 66 75 74 75 72 65 20 76 65 72 73 69  .** future versi
b670: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20  ons of SQLite.  
b680: 41 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64  Additional field
b690: 73 20 6d 61 79 20 62 65 20 61 70 70 65 6e 64 65  s may be appende
b6a0: 64 20 74 6f 20 74 68 69 73 0a 2a 2a 20 6f 62 6a  d to this.** obj
b6b0: 65 63 74 20 77 68 65 6e 20 74 68 65 20 69 56 65  ect when the iVe
b6c0: 72 73 69 6f 6e 20 76 61 6c 75 65 20 69 73 20 69  rsion value is i
b6d0: 6e 63 72 65 61 73 65 64 2e 20 20 4e 6f 74 65 20  ncreased.  Note 
b6e0: 74 68 61 74 20 74 68 65 20 73 74 72 75 63 74 75  that the structu
b6f0: 72 65 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c  re.** of the sql
b700: 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 20  ite3_vfs object 
b710: 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 74  changes in the t
b720: 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 74 77 65  ransaction betwe
b730: 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 76 65 72  en.** SQLite ver
b740: 73 69 6f 6e 20 33 2e 35 2e 39 20 61 6e 64 20 33  sion 3.5.9 and 3
b750: 2e 36 2e 30 20 61 6e 64 20 79 65 74 20 74 68 65  .6.0 and yet the
b760: 20 69 56 65 72 73 69 6f 6e 20 66 69 65 6c 64 20   iVersion field 
b770: 77 61 73 20 6e 6f 74 0a 2a 2a 20 6d 6f 64 69 66  was not.** modif
b780: 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ied..**.** The s
b790: 7a 4f 73 46 69 6c 65 20 66 69 65 6c 64 20 69 73  zOsFile field is
b7a0: 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
b7b0: 20 73 75 62 63 6c 61 73 73 65 64 20 5b 73 71 6c   subclassed [sql
b7c0: 69 74 65 33 5f 66 69 6c 65 5d 0a 2a 2a 20 73 74  ite3_file].** st
b7d0: 72 75 63 74 75 72 65 20 75 73 65 64 20 62 79 20  ructure used by 
b7e0: 74 68 69 73 20 56 46 53 2e 20 20 6d 78 50 61 74  this VFS.  mxPat
b7f0: 68 6e 61 6d 65 20 69 73 20 74 68 65 20 6d 61 78  hname is the max
b800: 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 0a 2a  imum length of.*
b810: 2a 20 61 20 70 61 74 68 6e 61 6d 65 20 69 6e 20  * a pathname in 
b820: 74 68 69 73 20 56 46 53 2e 0a 2a 2a 0a 2a 2a 20  this VFS..**.** 
b830: 52 65 67 69 73 74 65 72 65 64 20 73 71 6c 69 74  Registered sqlit
b840: 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 73 20 61  e3_vfs objects a
b850: 72 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c 69 6e  re kept on a lin
b860: 6b 65 64 20 6c 69 73 74 20 66 6f 72 6d 65 64 20  ked list formed 
b870: 62 79 0a 2a 2a 20 74 68 65 20 70 4e 65 78 74 20  by.** the pNext 
b880: 70 6f 69 6e 74 65 72 2e 20 20 54 68 65 20 5b 73  pointer.  The [s
b890: 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73  qlite3_vfs_regis
b8a0: 74 65 72 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73  ter()].** and [s
b8b0: 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67  qlite3_vfs_unreg
b8c0: 69 73 74 65 72 28 29 5d 20 69 6e 74 65 72 66 61  ister()] interfa
b8d0: 63 65 73 20 6d 61 6e 61 67 65 20 74 68 69 73 20  ces manage this 
b8e0: 6c 69 73 74 0a 2a 2a 20 69 6e 20 61 20 74 68 72  list.** in a thr
b8f0: 65 61 64 2d 73 61 66 65 20 77 61 79 2e 20 20 54  ead-safe way.  T
b900: 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f  he [sqlite3_vfs_
b910: 66 69 6e 64 28 29 5d 20 69 6e 74 65 72 66 61 63  find()] interfac
b920: 65 0a 2a 2a 20 73 65 61 72 63 68 65 73 20 74 68  e.** searches th
b930: 65 20 6c 69 73 74 2e 20 20 4e 65 69 74 68 65 72  e list.  Neither
b940: 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
b950: 20 63 6f 64 65 20 6e 6f 72 20 74 68 65 20 56 46   code nor the VF
b960: 53 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  S.** implementat
b970: 69 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65 20 74  ion should use t
b980: 68 65 20 70 4e 65 78 74 20 70 6f 69 6e 74 65 72  he pNext pointer
b990: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4e 65 78  ..**.** The pNex
b9a0: 74 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 6f  t field is the o
b9b0: 6e 6c 79 20 66 69 65 6c 64 20 69 6e 20 74 68 65  nly field in the
b9c0: 20 73 71 6c 69 74 65 33 5f 76 66 73 0a 2a 2a 20   sqlite3_vfs.** 
b9d0: 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 53  structure that S
b9e0: 51 4c 69 74 65 20 77 69 6c 6c 20 65 76 65 72 20  QLite will ever 
b9f0: 6d 6f 64 69 66 79 2e 20 20 53 51 4c 69 74 65 20  modify.  SQLite 
ba00: 77 69 6c 6c 20 6f 6e 6c 79 20 61 63 63 65 73 73  will only access
ba10: 0a 2a 2a 20 6f 72 20 6d 6f 64 69 66 79 20 74 68  .** or modify th
ba20: 69 73 20 66 69 65 6c 64 20 77 68 69 6c 65 20 68  is field while h
ba30: 6f 6c 64 69 6e 67 20 61 20 70 61 72 74 69 63 75  olding a particu
ba40: 6c 61 72 20 73 74 61 74 69 63 20 6d 75 74 65 78  lar static mutex
ba50: 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61  ..** The applica
ba60: 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65  tion should neve
ba70: 72 20 6d 6f 64 69 66 79 20 61 6e 79 74 68 69 6e  r modify anythin
ba80: 67 20 77 69 74 68 69 6e 20 74 68 65 20 73 71 6c  g within the sql
ba90: 69 74 65 33 5f 76 66 73 0a 2a 2a 20 6f 62 6a 65  ite3_vfs.** obje
baa0: 63 74 20 6f 6e 63 65 20 74 68 65 20 6f 62 6a 65  ct once the obje
bab0: 63 74 20 68 61 73 20 62 65 65 6e 20 72 65 67 69  ct has been regi
bac0: 73 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  stered..**.** Th
bad0: 65 20 7a 4e 61 6d 65 20 66 69 65 6c 64 20 68 6f  e zName field ho
bae0: 6c 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  lds the name of 
baf0: 74 68 65 20 56 46 53 20 6d 6f 64 75 6c 65 2e 20  the VFS module. 
bb00: 20 54 68 65 20 6e 61 6d 65 20 6d 75 73 74 0a 2a   The name must.*
bb10: 2a 20 62 65 20 75 6e 69 71 75 65 20 61 63 72 6f  * be unique acro
bb20: 73 73 20 61 6c 6c 20 56 46 53 20 6d 6f 64 75 6c  ss all VFS modul
bb30: 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  es..**.** SQLite
bb40: 20 77 69 6c 6c 20 67 75 61 72 61 6e 74 65 65 20   will guarantee 
bb50: 74 68 61 74 20 74 68 65 20 7a 46 69 6c 65 6e 61  that the zFilena
bb60: 6d 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  me parameter to 
bb70: 78 4f 70 65 6e 0a 2a 2a 20 69 73 20 65 69 74 68  xOpen.** is eith
bb80: 65 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  er a NULL pointe
bb90: 72 20 6f 72 20 73 74 72 69 6e 67 20 6f 62 74 61  r or string obta
bba0: 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 78 46 75  ined.** from xFu
bbb0: 6c 6c 50 61 74 68 6e 61 6d 65 28 29 2e 20 20 53  llPathname().  S
bbc0: 51 4c 69 74 65 20 66 75 72 74 68 65 72 20 67 75  QLite further gu
bbd0: 61 72 61 6e 74 65 65 73 20 74 68 61 74 0a 2a 2a  arantees that.**
bbe0: 20 74 68 65 20 73 74 72 69 6e 67 20 77 69 6c 6c   the string will
bbf0: 20 62 65 20 76 61 6c 69 64 20 61 6e 64 20 75 6e   be valid and un
bc00: 63 68 61 6e 67 65 64 20 75 6e 74 69 6c 20 78 43  changed until xC
bc10: 6c 6f 73 65 28 29 20 69 73 0a 2a 2a 20 63 61 6c  lose() is.** cal
bc20: 6c 65 64 2e 20 42 65 63 61 75 73 65 20 6f 66 20  led. Because of 
bc30: 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e  the previous sen
bc40: 74 65 6e 63 65 2c 0a 2a 2a 20 74 68 65 20 5b 73  tence,.** the [s
bc50: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 63 61 6e  qlite3_file] can
bc60: 20 73 61 66 65 6c 79 20 73 74 6f 72 65 20 61 20   safely store a 
bc70: 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a  pointer to the.*
bc80: 2a 20 66 69 6c 65 6e 61 6d 65 20 69 66 20 69 74  * filename if it
bc90: 20 6e 65 65 64 73 20 74 6f 20 72 65 6d 65 6d 62   needs to rememb
bca0: 65 72 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20  er the filename 
bcb0: 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e 2e  for some reason.
bcc0: 0a 2a 2a 20 49 66 20 74 68 65 20 7a 46 69 6c 65  .** If the zFile
bcd0: 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 69  name parameter i
bce0: 73 20 78 4f 70 65 6e 20 69 73 20 61 20 4e 55 4c  s xOpen is a NUL
bcf0: 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 78  L pointer then x
bd00: 4f 70 65 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e 76  Open.** must inv
bd10: 65 6e 74 20 69 74 73 20 6f 77 6e 20 74 65 6d 70  ent its own temp
bd20: 6f 72 61 72 79 20 6e 61 6d 65 20 66 6f 72 20 74  orary name for t
bd30: 68 65 20 66 69 6c 65 2e 20 20 57 68 65 6e 65 76  he file.  Whenev
bd40: 65 72 20 74 68 65 20 0a 2a 2a 20 78 46 69 6c 65  er the .** xFile
bd50: 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 69  name parameter i
bd60: 73 20 4e 55 4c 4c 20 69 74 20 77 69 6c 6c 20 61  s NULL it will a
bd70: 6c 73 6f 20 62 65 20 74 68 65 20 63 61 73 65 20  lso be the case 
bd80: 74 68 61 74 20 74 68 65 0a 2a 2a 20 66 6c 61 67  that the.** flag
bd90: 73 20 70 61 72 61 6d 65 74 65 72 20 77 69 6c 6c  s parameter will
bda0: 20 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54 45   include [SQLITE
bdb0: 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c  _OPEN_DELETEONCL
bdc0: 4f 53 45 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  OSE]..**.** The 
bdd0: 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74  flags argument t
bde0: 6f 20 78 4f 70 65 6e 28 29 20 69 6e 63 6c 75 64  o xOpen() includ
bdf0: 65 73 20 61 6c 6c 20 62 69 74 73 20 73 65 74 20  es all bits set 
be00: 69 6e 0a 2a 2a 20 74 68 65 20 66 6c 61 67 73 20  in.** the flags 
be10: 61 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c  argument to [sql
be20: 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2e  ite3_open_v2()].
be30: 20 20 4f 72 20 69 66 20 5b 73 71 6c 69 74 65 33    Or if [sqlite3
be40: 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 6f 72 20 5b  _open()].** or [
be50: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29  sqlite3_open16()
be60: 5d 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20  ] is used, then 
be70: 66 6c 61 67 73 20 69 6e 63 6c 75 64 65 73 20 61  flags includes a
be80: 74 20 6c 65 61 73 74 0a 2a 2a 20 5b 53 51 4c 49  t least.** [SQLI
be90: 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
bea0: 45 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f 50 45  E] | [SQLITE_OPE
beb0: 4e 5f 43 52 45 41 54 45 5d 2e 20 0a 2a 2a 20 49  N_CREATE]. .** I
bec0: 66 20 78 4f 70 65 6e 28 29 20 6f 70 65 6e 73 20  f xOpen() opens 
bed0: 61 20 66 69 6c 65 20 72 65 61 64 2d 6f 6e 6c 79  a file read-only
bee0: 20 74 68 65 6e 20 69 74 20 73 65 74 73 20 2a 70   then it sets *p
bef0: 4f 75 74 46 6c 61 67 73 20 74 6f 0a 2a 2a 20 69  OutFlags to.** i
bf00: 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54 45 5f 4f  nclude [SQLITE_O
bf10: 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 2e 20 20  PEN_READONLY].  
bf20: 4f 74 68 65 72 20 62 69 74 73 20 69 6e 20 2a 70  Other bits in *p
bf30: 4f 75 74 46 6c 61 67 73 20 6d 61 79 20 62 65 20  OutFlags may be 
bf40: 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  set..**.** SQLit
bf50: 65 20 77 69 6c 6c 20 61 6c 73 6f 20 61 64 64 20  e will also add 
bf60: 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  one of the follo
bf70: 77 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 74 68  wing flags to th
bf80: 65 20 78 4f 70 65 6e 28 29 0a 2a 2a 20 63 61 6c  e xOpen().** cal
bf90: 6c 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20  l, depending on 
bfa0: 74 68 65 20 6f 62 6a 65 63 74 20 62 65 69 6e 67  the object being
bfb0: 20 6f 70 65 6e 65 64 3a 0a 2a 2a 0a 2a 2a 20 3c   opened:.**.** <
bfc0: 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51  ul>.** <li>  [SQ
bfd0: 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44  LITE_OPEN_MAIN_D
bfe0: 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c  B].** <li>  [SQL
bff0: 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f  ITE_OPEN_MAIN_JO
c000: 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20  URNAL].** <li>  
c010: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d  [SQLITE_OPEN_TEM
c020: 50 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b  P_DB].** <li>  [
c030: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50  SQLITE_OPEN_TEMP
c040: 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69  _JOURNAL].** <li
c050: 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  >  [SQLITE_OPEN_
c060: 54 52 41 4e 53 49 45 4e 54 5f 44 42 5d 0a 2a 2a  TRANSIENT_DB].**
c070: 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f   <li>  [SQLITE_O
c080: 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 5d 0a  PEN_SUBJOURNAL].
c090: 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45  ** <li>  [SQLITE
c0a0: 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55  _OPEN_MASTER_JOU
c0b0: 52 4e 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a  RNAL].** </ul>.*
c0c0: 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 49 2f  *.** The file I/
c0d0: 4f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  O implementation
c0e0: 20 63 61 6e 20 75 73 65 20 74 68 65 20 6f 62 6a   can use the obj
c0f0: 65 63 74 20 74 79 70 65 20 66 6c 61 67 73 20 74  ect type flags t
c100: 6f 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68 65 20  o.** change the 
c110: 77 61 79 20 69 74 20 64 65 61 6c 73 20 77 69 74  way it deals wit
c120: 68 20 66 69 6c 65 73 2e 20 20 46 6f 72 20 65 78  h files.  For ex
c130: 61 6d 70 6c 65 2c 20 61 6e 20 61 70 70 6c 69 63  ample, an applic
c140: 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 64 6f  ation.** that do
c150: 65 73 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75  es not care abou
c160: 74 20 63 72 61 73 68 20 72 65 63 6f 76 65 72 79  t crash recovery
c170: 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6d 69 67   or rollback mig
c180: 68 74 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6f  ht make.** the o
c190: 70 65 6e 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c  pen of a journal
c1a0: 20 66 69 6c 65 20 61 20 6e 6f 2d 6f 70 2e 20 20   file a no-op.  
c1b0: 57 72 69 74 65 73 20 74 6f 20 74 68 69 73 20 6a  Writes to this j
c1c0: 6f 75 72 6e 61 6c 20 77 6f 75 6c 64 0a 2a 2a 20  ournal would.** 
c1d0: 61 6c 73 6f 20 62 65 20 6e 6f 2d 6f 70 73 2c 20  also be no-ops, 
c1e0: 61 6e 64 20 61 6e 79 20 61 74 74 65 6d 70 74 20  and any attempt 
c1f0: 74 6f 20 72 65 61 64 20 74 68 65 20 6a 6f 75 72  to read the jour
c200: 6e 61 6c 20 77 6f 75 6c 64 20 72 65 74 75 72 6e  nal would return
c210: 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  .** SQLITE_IOERR
c220: 2e 20 20 4f 72 20 74 68 65 20 69 6d 70 6c 65 6d  .  Or the implem
c230: 65 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74 20 72  entation might r
c240: 65 63 6f 67 6e 69 7a 65 20 74 68 61 74 20 61 20  ecognize that a 
c250: 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65  database.** file
c260: 20 77 69 6c 6c 20 62 65 20 64 6f 69 6e 67 20 70   will be doing p
c270: 61 67 65 2d 61 6c 69 67 6e 65 64 20 73 65 63 74  age-aligned sect
c280: 6f 72 20 72 65 61 64 73 20 61 6e 64 20 77 72 69  or reads and wri
c290: 74 65 73 20 69 6e 20 61 20 72 61 6e 64 6f 6d 0a  tes in a random.
c2a0: 2a 2a 20 6f 72 64 65 72 20 61 6e 64 20 73 65 74  ** order and set
c2b0: 20 75 70 20 69 74 73 20 49 2f 4f 20 73 75 62 73   up its I/O subs
c2c0: 79 73 74 65 6d 20 61 63 63 6f 72 64 69 6e 67 6c  ystem accordingl
c2d0: 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  y..**.** SQLite 
c2e0: 6d 69 67 68 74 20 61 6c 73 6f 20 61 64 64 20 6f  might also add o
c2f0: 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
c300: 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 74 68 65  ing flags to the
c310: 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 3a 0a 2a   xOpen method:.*
c320: 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69  *.** <ul>.** <li
c330: 3e 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44  > [SQLITE_OPEN_D
c340: 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a  ELETEONCLOSE].**
c350: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4f 50   <li> [SQLITE_OP
c360: 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d 0a 2a 2a  EN_EXCLUSIVE].**
c370: 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65   </ul>.**.** The
c380: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45   [SQLITE_OPEN_DE
c390: 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 20 66 6c 61  LETEONCLOSE] fla
c3a0: 67 20 6d 65 61 6e 73 20 74 68 65 20 66 69 6c 65  g means the file
c3b0: 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 64 65   should be.** de
c3c0: 6c 65 74 65 64 20 77 68 65 6e 20 69 74 20 69 73  leted when it is
c3d0: 20 63 6c 6f 73 65 64 2e 20 20 54 68 65 20 5b 53   closed.  The [S
c3e0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54  QLITE_OPEN_DELET
c3f0: 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 77 69 6c  EONCLOSE].** wil
c400: 6c 20 62 65 20 73 65 74 20 66 6f 72 20 54 45 4d  l be set for TEM
c410: 50 20 20 64 61 74 61 62 61 73 65 73 2c 20 6a 6f  P  databases, jo
c420: 75 72 6e 61 6c 73 20 61 6e 64 20 66 6f 72 20 73  urnals and for s
c430: 75 62 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2a 0a 2a  ubjournals..**.*
c440: 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50  * The [SQLITE_OP
c450: 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d 20 66 6c  EN_EXCLUSIVE] fl
c460: 61 67 20 69 73 20 61 6c 77 61 79 73 20 75 73 65  ag is always use
c470: 64 20 69 6e 20 63 6f 6e 6a 75 6e 63 74 69 6f 6e  d in conjunction
c480: 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b 53 51  .** with the [SQ
c490: 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45  LITE_OPEN_CREATE
c4a0: 5d 20 66 6c 61 67 2c 20 77 68 69 63 68 20 61 72  ] flag, which ar
c4b0: 65 20 62 6f 74 68 20 64 69 72 65 63 74 6c 79 0a  e both directly.
c4c0: 2a 2a 20 61 6e 61 6c 6f 67 6f 75 73 20 74 6f 20  ** analogous to 
c4d0: 74 68 65 20 4f 5f 45 58 43 4c 20 61 6e 64 20 4f  the O_EXCL and O
c4e0: 5f 43 52 45 41 54 20 66 6c 61 67 73 20 6f 66 20  _CREAT flags of 
c4f0: 74 68 65 20 50 4f 53 49 58 20 6f 70 65 6e 28 29  the POSIX open()
c500: 0a 2a 2a 20 41 50 49 2e 20 20 54 68 65 20 53 51  .** API.  The SQ
c510: 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53  LITE_OPEN_EXCLUS
c520: 49 56 45 20 66 6c 61 67 2c 20 77 68 65 6e 20 70  IVE flag, when p
c530: 61 69 72 65 64 20 77 69 74 68 20 74 68 65 20 0a  aired with the .
c540: 2a 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43  ** SQLITE_OPEN_C
c550: 52 45 41 54 45 2c 20 69 73 20 75 73 65 64 20 74  REATE, is used t
c560: 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20  o indicate that 
c570: 66 69 6c 65 20 73 68 6f 75 6c 64 20 61 6c 77 61  file should alwa
c580: 79 73 0a 2a 2a 20 62 65 20 63 72 65 61 74 65 64  ys.** be created
c590: 2c 20 61 6e 64 20 74 68 61 74 20 69 74 20 69 73  , and that it is
c5a0: 20 61 6e 20 65 72 72 6f 72 20 69 66 20 69 74 20   an error if it 
c5b0: 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e 0a  already exists..
c5c0: 2a 2a 20 49 74 20 69 73 20 3c 69 3e 6e 6f 74 3c  ** It is <i>not<
c5d0: 2f 69 3e 20 75 73 65 64 20 74 6f 20 69 6e 64 69  /i> used to indi
c5e0: 63 61 74 65 20 74 68 65 20 66 69 6c 65 20 73 68  cate the file sh
c5f0: 6f 75 6c 64 20 62 65 20 6f 70 65 6e 65 64 20 0a  ould be opened .
c600: 2a 2a 20 66 6f 72 20 65 78 63 6c 75 73 69 76 65  ** for exclusive
c610: 20 61 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 41   access..**.** A
c620: 74 20 6c 65 61 73 74 20 73 7a 4f 73 46 69 6c 65  t least szOsFile
c630: 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79   bytes of memory
c640: 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62   are allocated b
c650: 79 20 53 51 4c 69 74 65 0a 2a 2a 20 74 6f 20 68  y SQLite.** to h
c660: 6f 6c 64 20 74 68 65 20 20 5b 73 71 6c 69 74 65  old the  [sqlite
c670: 33 5f 66 69 6c 65 5d 20 73 74 72 75 63 74 75 72  3_file] structur
c680: 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
c690: 74 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e  third.** argumen
c6a0: 74 20 74 6f 20 78 4f 70 65 6e 2e 20 20 54 68 65  t to xOpen.  The
c6b0: 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 64 6f   xOpen method do
c6c0: 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 0a 2a  es not have to.*
c6d0: 2a 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 73  * allocate the s
c6e0: 74 72 75 63 74 75 72 65 3b 20 69 74 20 73 68 6f  tructure; it sho
c6f0: 75 6c 64 20 6a 75 73 74 20 66 69 6c 6c 20 69 74  uld just fill it
c700: 20 69 6e 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a   in.  Note that.
c710: 2a 2a 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74  ** the xOpen met
c720: 68 6f 64 20 6d 75 73 74 20 73 65 74 20 74 68 65  hod must set the
c730: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d   sqlite3_file.pM
c740: 65 74 68 6f 64 73 20 74 6f 20 65 69 74 68 65 72  ethods to either
c750: 0a 2a 2a 20 61 20 76 61 6c 69 64 20 5b 73 71 6c  .** a valid [sql
c760: 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d  ite3_io_methods]
c770: 20 6f 62 6a 65 63 74 20 6f 72 20 74 6f 20 4e 55   object or to NU
c780: 4c 4c 2e 20 20 78 4f 70 65 6e 20 6d 75 73 74 20  LL.  xOpen must 
c790: 64 6f 0a 2a 2a 20 74 68 69 73 20 65 76 65 6e 20  do.** this even 
c7a0: 69 66 20 74 68 65 20 6f 70 65 6e 20 66 61 69 6c  if the open fail
c7b0: 73 2e 20 20 53 51 4c 69 74 65 20 65 78 70 65 63  s.  SQLite expec
c7c0: 74 73 20 74 68 61 74 20 74 68 65 20 73 71 6c 69  ts that the sqli
c7d0: 74 65 33 5f 66 69 6c 65 2e 70 4d 65 74 68 6f 64  te3_file.pMethod
c7e0: 73 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 77 69 6c  s.** element wil
c7f0: 6c 20 62 65 20 76 61 6c 69 64 20 61 66 74 65 72  l be valid after
c800: 20 78 4f 70 65 6e 20 72 65 74 75 72 6e 73 20 72   xOpen returns r
c810: 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65  egardless of the
c820: 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 66   success.** or f
c830: 61 69 6c 75 72 65 20 6f 66 20 74 68 65 20 78 4f  ailure of the xO
c840: 70 65 6e 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20  pen call..**.** 
c850: 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65  The flags argume
c860: 6e 74 20 74 6f 20 78 41 63 63 65 73 73 28 29 20  nt to xAccess() 
c870: 6d 61 79 20 62 65 20 5b 53 51 4c 49 54 45 5f 41  may be [SQLITE_A
c880: 43 43 45 53 53 5f 45 58 49 53 54 53 5d 0a 2a 2a  CCESS_EXISTS].**
c890: 20 74 6f 20 74 65 73 74 20 66 6f 72 20 74 68 65   to test for the
c8a0: 20 65 78 69 73 74 65 6e 63 65 20 6f 66 20 61 20   existence of a 
c8b0: 66 69 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 54 45  file, or [SQLITE
c8c0: 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54  _ACCESS_READWRIT
c8d0: 45 5d 20 74 6f 0a 2a 2a 20 74 65 73 74 20 77 68  E] to.** test wh
c8e0: 65 74 68 65 72 20 61 20 66 69 6c 65 20 69 73 20  ether a file is 
c8f0: 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72 69  readable and wri
c900: 74 61 62 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 54  table, or [SQLIT
c910: 45 5f 41 43 43 45 53 53 5f 52 45 41 44 5d 0a 2a  E_ACCESS_READ].*
c920: 2a 20 74 6f 20 74 65 73 74 20 77 68 65 74 68 65  * to test whethe
c930: 72 20 61 20 66 69 6c 65 20 69 73 20 61 74 20 6c  r a file is at l
c940: 65 61 73 74 20 72 65 61 64 61 62 6c 65 2e 20 20  east readable.  
c950: 20 54 68 65 20 66 69 6c 65 20 63 61 6e 20 62 65   The file can be
c960: 20 61 0a 2a 2a 20 64 69 72 65 63 74 6f 72 79 2e   a.** directory.
c970: 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69  .**.** SQLite wi
c980: 6c 6c 20 61 6c 77 61 79 73 20 61 6c 6c 6f 63 61  ll always alloca
c990: 74 65 20 61 74 20 6c 65 61 73 74 20 6d 78 50 61  te at least mxPa
c9a0: 74 68 6e 61 6d 65 2b 31 20 62 79 74 65 73 20 66  thname+1 bytes f
c9b0: 6f 72 20 74 68 65 0a 2a 2a 20 6f 75 74 70 75 74  or the.** output
c9c0: 20 62 75 66 66 65 72 20 78 46 75 6c 6c 50 61 74   buffer xFullPat
c9d0: 68 6e 61 6d 65 2e 20 20 54 68 65 20 65 78 61 63  hname.  The exac
c9e0: 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6f 75  t size of the ou
c9f0: 74 70 75 74 20 62 75 66 66 65 72 0a 2a 2a 20 69  tput buffer.** i
ca00: 73 20 61 6c 73 6f 20 70 61 73 73 65 64 20 61 73  s also passed as
ca10: 20 61 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20   a parameter to 
ca20: 62 6f 74 68 20 20 6d 65 74 68 6f 64 73 2e 20 49  both  methods. I
ca30: 66 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66  f the output buf
ca40: 66 65 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c 61  fer.** is not la
ca50: 72 67 65 20 65 6e 6f 75 67 68 2c 20 5b 53 51 4c  rge enough, [SQL
ca60: 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 73 68  ITE_CANTOPEN] sh
ca70: 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64  ould be returned
ca80: 2e 20 53 69 6e 63 65 20 74 68 69 73 20 69 73 0a  . Since this is.
ca90: 2a 2a 20 68 61 6e 64 6c 65 64 20 61 73 20 61 20  ** handled as a 
caa0: 66 61 74 61 6c 20 65 72 72 6f 72 20 62 79 20 53  fatal error by S
cab0: 51 4c 69 74 65 2c 20 76 66 73 20 69 6d 70 6c 65  QLite, vfs imple
cac0: 6d 65 6e 74 61 74 69 6f 6e 73 20 73 68 6f 75 6c  mentations shoul
cad0: 64 20 65 6e 64 65 61 76 6f 72 0a 2a 2a 20 74 6f  d endeavor.** to
cae0: 20 70 72 65 76 65 6e 74 20 74 68 69 73 20 62 79   prevent this by
caf0: 20 73 65 74 74 69 6e 67 20 6d 78 50 61 74 68 6e   setting mxPathn
cb00: 61 6d 65 20 74 6f 20 61 20 73 75 66 66 69 63 69  ame to a suffici
cb10: 65 6e 74 6c 79 20 6c 61 72 67 65 20 76 61 6c 75  ently large valu
cb20: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 61  e..**.** The xRa
cb30: 6e 64 6f 6d 6e 65 73 73 28 29 2c 20 78 53 6c 65  ndomness(), xSle
cb40: 65 70 28 29 2c 20 61 6e 64 20 78 43 75 72 72 65  ep(), and xCurre
cb50: 6e 74 54 69 6d 65 28 29 20 69 6e 74 65 72 66 61  ntTime() interfa
cb60: 63 65 73 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 73  ces.** are not s
cb70: 74 72 69 63 74 6c 79 20 61 20 70 61 72 74 20 6f  trictly a part o
cb80: 66 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d  f the filesystem
cb90: 2c 20 62 75 74 20 74 68 65 79 20 61 72 65 0a 2a  , but they are.*
cba0: 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68  * included in th
cbb0: 65 20 56 46 53 20 73 74 72 75 63 74 75 72 65 20  e VFS structure 
cbc0: 66 6f 72 20 63 6f 6d 70 6c 65 74 65 6e 65 73 73  for completeness
cbd0: 2e 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64 6f 6d  ..** The xRandom
cbe0: 6e 65 73 73 28 29 20 66 75 6e 63 74 69 6f 6e 20  ness() function 
cbf0: 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 74 75  attempts to retu
cc00: 72 6e 20 6e 42 79 74 65 73 20 62 79 74 65 73 0a  rn nBytes bytes.
cc10: 2a 2a 20 6f 66 20 67 6f 6f 64 2d 71 75 61 6c 69  ** of good-quali
cc20: 74 79 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e  ty randomness in
cc30: 74 6f 20 7a 4f 75 74 2e 20 20 54 68 65 20 72 65  to zOut.  The re
cc40: 74 75 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a  turn value is.**
cc50: 20 74 68 65 20 61 63 74 75 61 6c 20 6e 75 6d 62   the actual numb
cc60: 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 72  er of bytes of r
cc70: 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61 69 6e  andomness obtain
cc80: 65 64 2e 0a 2a 2a 20 54 68 65 20 78 53 6c 65 65  ed..** The xSlee
cc90: 70 28 29 20 6d 65 74 68 6f 64 20 63 61 75 73 65  p() method cause
cca0: 73 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 74 68  s the calling th
ccb0: 72 65 61 64 20 74 6f 20 73 6c 65 65 70 20 66 6f  read to sleep fo
ccc0: 72 20 61 74 0a 2a 2a 20 6c 65 61 73 74 20 74 68  r at.** least th
ccd0: 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72  e number of micr
cce0: 6f 73 65 63 6f 6e 64 73 20 67 69 76 65 6e 2e 20  oseconds given. 
ccf0: 20 54 68 65 20 78 43 75 72 72 65 6e 74 54 69 6d   The xCurrentTim
cd00: 65 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 72 65  e().** method re
cd10: 74 75 72 6e 73 20 61 20 4a 75 6c 69 61 6e 20 44  turns a Julian D
cd20: 61 79 20 4e 75 6d 62 65 72 20 66 6f 72 20 74 68  ay Number for th
cd30: 65 20 63 75 72 72 65 6e 74 20 64 61 74 65 20 61  e current date a
cd40: 6e 64 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2f 0a 74  nd time..**.*/.t
cd50: 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
cd60: 6c 69 74 65 33 5f 76 66 73 20 73 71 6c 69 74 65  lite3_vfs sqlite
cd70: 33 5f 76 66 73 3b 0a 73 74 72 75 63 74 20 73 71  3_vfs;.struct sq
cd80: 6c 69 74 65 33 5f 76 66 73 20 7b 0a 20 20 69 6e  lite3_vfs {.  in
cd90: 74 20 69 56 65 72 73 69 6f 6e 3b 20 20 20 20 20  t iVersion;     
cda0: 20 20 20 20 20 20 20 2f 2a 20 53 74 72 75 63 74         /* Struct
cdb0: 75 72 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62  ure version numb
cdc0: 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4f 73  er */.  int szOs
cdd0: 46 69 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20  File;           
cde0: 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 75 62 63   /* Size of subc
cdf0: 6c 61 73 73 65 64 20 73 71 6c 69 74 65 33 5f 66  lassed sqlite3_f
ce00: 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50  ile */.  int mxP
ce10: 61 74 68 6e 61 6d 65 3b 20 20 20 20 20 20 20 20  athname;        
ce20: 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 66 69 6c    /* Maximum fil
ce30: 65 20 70 61 74 68 6e 61 6d 65 20 6c 65 6e 67 74  e pathname lengt
ce40: 68 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76  h */.  sqlite3_v
ce50: 66 73 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20  fs *pNext;      
ce60: 2f 2a 20 4e 65 78 74 20 72 65 67 69 73 74 65 72  /* Next register
ce70: 65 64 20 56 46 53 20 2a 2f 0a 20 20 63 6f 6e 73  ed VFS */.  cons
ce80: 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20  t char *zName;  
ce90: 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
cea0: 74 68 69 73 20 76 69 72 74 75 61 6c 20 66 69 6c  this virtual fil
ceb0: 65 20 73 79 73 74 65 6d 20 2a 2f 0a 20 20 76 6f  e system */.  vo
cec0: 69 64 20 2a 70 41 70 70 44 61 74 61 3b 20 20 20  id *pAppData;   
ced0: 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
cee0: 72 20 74 6f 20 61 70 70 6c 69 63 61 74 69 6f 6e  r to application
cef0: 2d 73 70 65 63 69 66 69 63 20 64 61 74 61 20 2a  -specific data *
cf00: 2f 0a 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29  /.  int (*xOpen)
cf10: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63  (sqlite3_vfs*, c
cf20: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
cf30: 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  , sqlite3_file*,
cf40: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
cf50: 69 6e 74 20 66 6c 61 67 73 2c 20 69 6e 74 20 2a  int flags, int *
cf60: 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 69 6e  pOutFlags);.  in
cf70: 74 20 28 2a 78 44 65 6c 65 74 65 29 28 73 71 6c  t (*xDelete)(sql
cf80: 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74  ite3_vfs*, const
cf90: 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e   char *zName, in
cfa0: 74 20 73 79 6e 63 44 69 72 29 3b 0a 20 20 69 6e  t syncDir);.  in
cfb0: 74 20 28 2a 78 41 63 63 65 73 73 29 28 73 71 6c  t (*xAccess)(sql
cfc0: 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74  ite3_vfs*, const
cfd0: 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e   char *zName, in
cfe0: 74 20 66 6c 61 67 73 2c 20 69 6e 74 20 2a 70 52  t flags, int *pR
cff0: 65 73 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a  esOut);.  int (*
d000: 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 28 73  xFullPathname)(s
d010: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e  qlite3_vfs*, con
d020: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
d030: 69 6e 74 20 6e 4f 75 74 2c 20 63 68 61 72 20 2a  int nOut, char *
d040: 7a 4f 75 74 29 3b 0a 20 20 76 6f 69 64 20 2a 28  zOut);.  void *(
d050: 2a 78 44 6c 4f 70 65 6e 29 28 73 71 6c 69 74 65  *xDlOpen)(sqlite
d060: 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68  3_vfs*, const ch
d070: 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  ar *zFilename);.
d080: 20 20 76 6f 69 64 20 28 2a 78 44 6c 45 72 72 6f    void (*xDlErro
d090: 72 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c  r)(sqlite3_vfs*,
d0a0: 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72   int nByte, char
d0b0: 20 2a 7a 45 72 72 4d 73 67 29 3b 0a 20 20 76 6f   *zErrMsg);.  vo
d0c0: 69 64 20 28 2a 28 2a 78 44 6c 53 79 6d 29 28 73  id (*(*xDlSym)(s
d0d0: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 76 6f 69 64  qlite3_vfs*,void
d0e0: 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  *, const char *z
d0f0: 53 79 6d 62 6f 6c 29 29 28 76 6f 69 64 29 3b 0a  Symbol))(void);.
d100: 20 20 76 6f 69 64 20 28 2a 78 44 6c 43 6c 6f 73    void (*xDlClos
d110: 65 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c  e)(sqlite3_vfs*,
d120: 20 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 28   void*);.  int (
d130: 2a 78 52 61 6e 64 6f 6d 6e 65 73 73 29 28 73 71  *xRandomness)(sq
d140: 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20  lite3_vfs*, int 
d150: 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 4f 75  nByte, char *zOu
d160: 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 6c 65  t);.  int (*xSle
d170: 65 70 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a  ep)(sqlite3_vfs*
d180: 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 6f 6e  , int microsecon
d190: 64 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 75  ds);.  int (*xCu
d1a0: 72 72 65 6e 74 54 69 6d 65 29 28 73 71 6c 69 74  rrentTime)(sqlit
d1b0: 65 33 5f 76 66 73 2a 2c 20 64 6f 75 62 6c 65 2a  e3_vfs*, double*
d1c0: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 47 65 74 4c  );.  int (*xGetL
d1d0: 61 73 74 45 72 72 6f 72 29 28 73 71 6c 69 74 65  astError)(sqlite
d1e0: 33 5f 76 66 73 2a 2c 20 69 6e 74 2c 20 63 68 61  3_vfs*, int, cha
d1f0: 72 20 2a 29 3b 0a 20 20 2f 2a 20 4e 65 77 20 66  r *);.  /* New f
d200: 69 65 6c 64 73 20 6d 61 79 20 62 65 20 61 70 70  ields may be app
d210: 65 6e 64 65 64 20 69 6e 20 66 69 67 75 72 65 20  ended in figure 
d220: 76 65 72 73 69 6f 6e 73 2e 20 20 54 68 65 20 69  versions.  The i
d230: 56 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 76 61 6c  Version.  ** val
d240: 75 65 20 77 69 6c 6c 20 69 6e 63 72 65 6d 65 6e  ue will incremen
d250: 74 20 77 68 65 6e 65 76 65 72 20 74 68 69 73 20  t whenever this 
d260: 68 61 70 70 65 6e 73 2e 20 2a 2f 0a 7d 3b 0a 0a  happens. */.};..
d270: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
d280: 46 6c 61 67 73 20 66 6f 72 20 74 68 65 20 78 41  Flags for the xA
d290: 63 63 65 73 73 20 56 46 53 20 6d 65 74 68 6f 64  ccess VFS method
d2a0: 20 7b 48 31 31 31 39 30 7d 20 3c 48 31 31 31 34   {H11190} <H1114
d2b0: 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69  0>.**.** These i
d2c0: 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73  nteger constants
d2d0: 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 20   can be used as 
d2e0: 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65  the third parame
d2f0: 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 78 41  ter to.** the xA
d300: 63 63 65 73 73 20 6d 65 74 68 6f 64 20 6f 66 20  ccess method of 
d310: 61 6e 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d  an [sqlite3_vfs]
d320: 20 6f 62 6a 65 63 74 2e 20 7b 45 4e 44 7d 20 20   object. {END}  
d330: 54 68 65 79 20 64 65 74 65 72 6d 69 6e 65 0a 2a  They determine.*
d340: 2a 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 70  * what kind of p
d350: 65 72 6d 69 73 73 69 6f 6e 73 20 74 68 65 20 78  ermissions the x
d360: 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 69 73  Access method is
d370: 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 2e 0a 2a 2a   looking for..**
d380: 20 57 69 74 68 20 53 51 4c 49 54 45 5f 41 43 43   With SQLITE_ACC
d390: 45 53 53 5f 45 58 49 53 54 53 2c 20 74 68 65 20  ESS_EXISTS, the 
d3a0: 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a  xAccess method.*
d3b0: 2a 20 73 69 6d 70 6c 79 20 63 68 65 63 6b 73 20  * simply checks 
d3c0: 77 68 65 74 68 65 72 20 74 68 65 20 66 69 6c 65  whether the file
d3d0: 20 65 78 69 73 74 73 2e 0a 2a 2a 20 57 69 74 68   exists..** With
d3e0: 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52   SQLITE_ACCESS_R
d3f0: 45 41 44 57 52 49 54 45 2c 20 74 68 65 20 78 41  EADWRITE, the xA
d400: 63 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a 2a 20  ccess method.** 
d410: 63 68 65 63 6b 73 20 77 68 65 74 68 65 72 20 74  checks whether t
d420: 68 65 20 66 69 6c 65 20 69 73 20 62 6f 74 68 20  he file is both 
d430: 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72 69  readable and wri
d440: 74 61 62 6c 65 2e 0a 2a 2a 20 57 69 74 68 20 53  table..** With S
d450: 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41  QLITE_ACCESS_REA
d460: 44 2c 20 74 68 65 20 78 41 63 63 65 73 73 20 6d  D, the xAccess m
d470: 65 74 68 6f 64 0a 2a 2a 20 63 68 65 63 6b 73 20  ethod.** checks 
d480: 77 68 65 74 68 65 72 20 74 68 65 20 66 69 6c 65  whether the file
d490: 20 69 73 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2f   is readable..*/
d4a0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
d4b0: 41 43 43 45 53 53 5f 45 58 49 53 54 53 20 20 20  ACCESS_EXISTS   
d4c0: 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   0.#define SQLIT
d4d0: 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49  E_ACCESS_READWRI
d4e0: 54 45 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c  TE 1.#define SQL
d4f0: 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 20  ITE_ACCESS_READ 
d500: 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 43 41       2../*.** CA
d510: 50 49 33 52 45 46 3a 20 49 6e 69 74 69 61 6c 69  PI3REF: Initiali
d520: 7a 65 20 54 68 65 20 53 51 4c 69 74 65 20 4c 69  ze The SQLite Li
d530: 62 72 61 72 79 20 7b 48 31 30 31 33 30 7d 20 3c  brary {H10130} <
d540: 53 32 30 30 30 30 3e 3c 53 33 30 31 30 30 3e 0a  S20000><S30100>.
d550: 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
d560: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 72  3_initialize() r
d570: 6f 75 74 69 6e 65 20 69 6e 69 74 69 61 6c 69 7a  outine initializ
d580: 65 73 20 74 68 65 0a 2a 2a 20 53 51 4c 69 74 65  es the.** SQLite
d590: 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 73   library.  The s
d5a0: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
d5b0: 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 64 65 61  ) routine.** dea
d5c0: 6c 6c 6f 63 61 74 65 73 20 61 6e 79 20 72 65 73  llocates any res
d5d0: 6f 75 72 63 65 73 20 74 68 61 74 20 77 65 72 65  ources that were
d5e0: 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71   allocated by sq
d5f0: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
d600: 28 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c  ()..**.** A call
d610: 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e 69 74   to sqlite3_init
d620: 69 61 6c 69 7a 65 28 29 20 69 73 20 61 6e 20 22  ialize() is an "
d630: 65 66 66 65 63 74 69 76 65 22 20 63 61 6c 6c 20  effective" call 
d640: 69 66 20 69 74 20 69 73 0a 2a 2a 20 74 68 65 20  if it is.** the 
d650: 66 69 72 73 74 20 74 69 6d 65 20 73 71 6c 69 74  first time sqlit
d660: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
d670: 69 73 20 69 6e 76 6f 6b 65 64 20 64 75 72 69 6e  is invoked durin
d680: 67 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f  g the lifetime o
d690: 66 0a 2a 2a 20 74 68 65 20 70 72 6f 63 65 73 73  f.** the process
d6a0: 2c 20 6f 72 20 69 66 20 69 74 20 69 73 20 74 68  , or if it is th
d6b0: 65 20 66 69 72 73 74 20 74 69 6d 65 20 73 71 6c  e first time sql
d6c0: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
d6d0: 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20  ) is invoked.** 
d6e0: 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 63 61 6c 6c  following a call
d6f0: 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75 74   to sqlite3_shut
d700: 64 6f 77 6e 28 29 2e 20 20 4f 6e 6c 79 20 61 6e  down().  Only an
d710: 20 65 66 66 65 63 74 69 76 65 20 63 61 6c 6c 0a   effective call.
d720: 2a 2a 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6e  ** of sqlite3_in
d730: 69 74 69 61 6c 69 7a 65 28 29 20 64 6f 65 73 20  itialize() does 
d740: 61 6e 79 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  any initializati
d750: 6f 6e 2e 20 20 41 6c 6c 20 6f 74 68 65 72 20 63  on.  All other c
d760: 61 6c 6c 73 0a 2a 2a 20 61 72 65 20 68 61 72 6d  alls.** are harm
d770: 6c 65 73 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a  less no-ops..**.
d780: 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c  ** A call to sql
d790: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20  ite3_shutdown() 
d7a0: 69 73 20 61 6e 20 22 65 66 66 65 63 74 69 76 65  is an "effective
d7b0: 22 20 63 61 6c 6c 20 69 66 20 69 74 20 69 73 20  " call if it is 
d7c0: 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 63 61 6c  the first.** cal
d7d0: 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75  l to sqlite3_shu
d7e0: 74 64 6f 77 6e 28 29 20 73 69 6e 63 65 20 74 68  tdown() since th
d7f0: 65 20 6c 61 73 74 20 73 71 6c 69 74 65 33 5f 69  e last sqlite3_i
d800: 6e 69 74 69 61 6c 69 7a 65 28 29 2e 20 20 4f 6e  nitialize().  On
d810: 6c 79 0a 2a 2a 20 61 6e 20 65 66 66 65 63 74 69  ly.** an effecti
d820: 76 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  ve call to sqlit
d830: 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 64 6f  e3_shutdown() do
d840: 65 73 20 61 6e 79 20 64 65 69 6e 69 74 69 61 6c  es any deinitial
d850: 69 7a 61 74 69 6f 6e 2e 0a 2a 2a 20 41 6c 6c 20  ization..** All 
d860: 6f 74 68 65 72 20 63 61 6c 6c 73 20 74 6f 20 73  other calls to s
d870: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
d880: 29 20 61 72 65 20 68 61 72 6d 6c 65 73 73 20 6e  ) are harmless n
d890: 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 41 6d 6f  o-ops..**.** Amo
d8a0: 6e 67 20 6f 74 68 65 72 20 74 68 69 6e 67 73 2c  ng other things,
d8b0: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
d8c0: 69 7a 65 28 29 20 73 68 61 6c 6c 20 69 6e 76 6f  ize() shall invo
d8d0: 6b 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73  ke.** sqlite3_os
d8e0: 5f 69 6e 69 74 28 29 2e 20 20 53 69 6d 69 6c 61  _init().  Simila
d8f0: 72 6c 79 2c 20 73 71 6c 69 74 65 33 5f 73 68 75  rly, sqlite3_shu
d900: 74 64 6f 77 6e 28 29 0a 2a 2a 20 73 68 61 6c 6c  tdown().** shall
d910: 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f   invoke sqlite3_
d920: 6f 73 5f 65 6e 64 28 29 2e 0a 2a 2a 0a 2a 2a 20  os_end()..**.** 
d930: 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  The sqlite3_init
d940: 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e 65  ialize() routine
d950: 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
d960: 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 2e  _OK] on success.
d970: 0a 2a 2a 20 49 66 20 66 6f 72 20 73 6f 6d 65 20  .** If for some 
d980: 72 65 61 73 6f 6e 2c 20 73 71 6c 69 74 65 33 5f  reason, sqlite3_
d990: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20  initialize() is 
d9a0: 75 6e 61 62 6c 65 20 74 6f 20 69 6e 69 74 69 61  unable to initia
d9b0: 6c 69 7a 65 0a 2a 2a 20 74 68 65 20 6c 69 62 72  lize.** the libr
d9c0: 61 72 79 20 28 70 65 72 68 61 70 73 20 69 74 20  ary (perhaps it 
d9d0: 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c  is unable to all
d9e0: 6f 63 61 74 65 20 61 20 6e 65 65 64 65 64 20 72  ocate a needed r
d9f0: 65 73 6f 75 72 63 65 20 73 75 63 68 0a 2a 2a 20  esource such.** 
da00: 61 73 20 61 20 6d 75 74 65 78 29 20 69 74 20 72  as a mutex) it r
da10: 65 74 75 72 6e 73 20 61 6e 20 5b 65 72 72 6f 72  eturns an [error
da20: 20 63 6f 64 65 5d 20 6f 74 68 65 72 20 74 68 61   code] other tha
da30: 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a  n [SQLITE_OK]..*
da40: 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
da50: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 72 6f  _initialize() ro
da60: 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
da70: 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20 6d 61  internally by ma
da80: 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 53 51 4c 69  ny other.** SQLi
da90: 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 73 6f  te interfaces so
daa0: 20 74 68 61 74 20 61 6e 20 61 70 70 6c 69 63 61   that an applica
dab0: 74 69 6f 6e 20 75 73 75 61 6c 6c 79 20 64 6f 65  tion usually doe
dac0: 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 0a 2a 2a  s not need to.**
dad0: 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f   invoke sqlite3_
dae0: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 64 69 72  initialize() dir
daf0: 65 63 74 6c 79 2e 20 20 46 6f 72 20 65 78 61 6d  ectly.  For exam
db00: 70 6c 65 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70  ple, [sqlite3_op
db10: 65 6e 28 29 5d 0a 2a 2a 20 63 61 6c 6c 73 20 73  en()].** calls s
db20: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
db30: 65 28 29 20 73 6f 20 74 68 65 20 53 51 4c 69 74  e() so the SQLit
db40: 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 62  e library will b
db50: 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a  e automatically.
db60: 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 77  ** initialized w
db70: 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  hen [sqlite3_ope
db80: 6e 28 29 5d 20 69 73 20 63 61 6c 6c 65 64 20 69  n()] is called i
db90: 66 20 69 74 20 68 61 73 20 6e 6f 74 20 62 65 20  f it has not be 
dba0: 69 6e 69 74 69 61 6c 69 7a 65 64 0a 2a 2a 20 61  initialized.** a
dbb0: 6c 72 65 61 64 79 2e 20 20 48 6f 77 65 76 65 72  lready.  However
dbc0: 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 63  , if SQLite is c
dbd0: 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65  ompiled with the
dbe0: 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55   [SQLITE_OMIT_AU
dbf0: 54 4f 49 4e 49 54 5d 0a 2a 2a 20 63 6f 6d 70 69  TOINIT].** compi
dc00: 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 2c 20  le-time option, 
dc10: 74 68 65 6e 20 74 68 65 20 61 75 74 6f 6d 61 74  then the automat
dc20: 69 63 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  ic calls to sqli
dc30: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
dc40: 0a 2a 2a 20 61 72 65 20 6f 6d 69 74 74 65 64 20  .** are omitted 
dc50: 61 6e 64 20 74 68 65 20 61 70 70 6c 69 63 61 74  and the applicat
dc60: 69 6f 6e 20 6d 75 73 74 20 63 61 6c 6c 20 73 71  ion must call sq
dc70: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
dc80: 28 29 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 70  () directly.** p
dc90: 72 69 6f 72 20 74 6f 20 75 73 69 6e 67 20 61 6e  rior to using an
dca0: 79 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 69  y other SQLite i
dcb0: 6e 74 65 72 66 61 63 65 2e 20 20 46 6f 72 20 6d  nterface.  For m
dcc0: 61 78 69 6d 75 6d 20 70 6f 72 74 61 62 69 6c 69  aximum portabili
dcd0: 74 79 2c 0a 2a 2a 20 69 74 20 69 73 20 72 65 63  ty,.** it is rec
dce0: 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 61 70  ommended that ap
dcf0: 70 6c 69 63 61 74 69 6f 6e 73 20 61 6c 77 61 79  plications alway
dd00: 73 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33  s invoke sqlite3
dd10: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 0a 2a 2a  _initialize().**
dd20: 20 64 69 72 65 63 74 6c 79 20 70 72 69 6f 72 20   directly prior 
dd30: 74 6f 20 75 73 69 6e 67 20 61 6e 79 20 6f 74 68  to using any oth
dd40: 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66  er SQLite interf
dd50: 61 63 65 2e 20 20 46 75 74 75 72 65 20 72 65 6c  ace.  Future rel
dd60: 65 61 73 65 73 0a 2a 2a 20 6f 66 20 53 51 4c 69  eases.** of SQLi
dd70: 74 65 20 6d 61 79 20 72 65 71 75 69 72 65 20 74  te may require t
dd80: 68 69 73 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  his.  In other w
dd90: 6f 72 64 73 2c 20 74 68 65 20 62 65 68 61 76 69  ords, the behavi
dda0: 6f 72 20 65 78 68 69 62 69 74 65 64 0a 2a 2a 20  or exhibited.** 
ddb0: 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63  when SQLite is c
ddc0: 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 5b 53 51  ompiled with [SQ
ddd0: 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
dde0: 49 54 5d 20 6d 69 67 68 74 20 62 65 63 6f 6d 65  IT] might become
ddf0: 20 74 68 65 0a 2a 2a 20 64 65 66 61 75 6c 74 20   the.** default 
de00: 62 65 68 61 76 69 6f 72 20 69 6e 20 73 6f 6d 65  behavior in some
de10: 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 20   future release 
de20: 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a  of SQLite..**.**
de30: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f   The sqlite3_os_
de40: 69 6e 69 74 28 29 20 72 6f 75 74 69 6e 65 20 64  init() routine d
de50: 6f 65 73 20 6f 70 65 72 61 74 69 6e 67 2d 73 79  oes operating-sy
de60: 73 74 65 6d 20 73 70 65 63 69 66 69 63 0a 2a 2a  stem specific.**
de70: 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
de80: 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69  of the SQLite li
de90: 62 72 61 72 79 2e 20 20 54 68 65 20 73 71 6c 69  brary.  The sqli
dea0: 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20  te3_os_end().** 
deb0: 72 6f 75 74 69 6e 65 20 75 6e 64 6f 65 73 20 74  routine undoes t
dec0: 68 65 20 65 66 66 65 63 74 20 6f 66 20 73 71 6c  he effect of sql
ded0: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 2e 20  ite3_os_init(). 
dee0: 20 54 79 70 69 63 61 6c 20 74 61 73 6b 73 0a 2a   Typical tasks.*
def0: 2a 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74  * performed by t
df00: 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 69 6e  hese routines in
df10: 63 6c 75 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  clude allocation
df20: 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6f 6e   or deallocation
df30: 0a 2a 2a 20 6f 66 20 73 74 61 74 69 63 20 72 65  .** of static re
df40: 73 6f 75 72 63 65 73 2c 20 69 6e 69 74 69 61 6c  sources, initial
df50: 69 7a 61 74 69 6f 6e 20 6f 66 20 67 6c 6f 62 61  ization of globa
df60: 6c 20 76 61 72 69 61 62 6c 65 73 2c 0a 2a 2a 20  l variables,.** 
df70: 73 65 74 74 69 6e 67 20 75 70 20 61 20 64 65 66  setting up a def
df80: 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66  ault [sqlite3_vf
df90: 73 5d 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 73 65  s] module, or se
dfa0: 74 74 69 6e 67 20 75 70 0a 2a 2a 20 61 20 64 65  tting up.** a de
dfb0: 66 61 75 6c 74 20 63 6f 6e 66 69 67 75 72 61 74  fault configurat
dfc0: 69 6f 6e 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  ion using [sqlit
dfd0: 65 33 5f 63 6f 6e 66 69 67 28 29 5d 2e 0a 2a 2a  e3_config()]..**
dfe0: 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74  .** The applicat
dff0: 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65 72  ion should never
e000: 20 69 6e 76 6f 6b 65 20 65 69 74 68 65 72 20 73   invoke either s
e010: 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29  qlite3_os_init()
e020: 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f  .** or sqlite3_o
e030: 73 5f 65 6e 64 28 29 20 64 69 72 65 63 74 6c 79  s_end() directly
e040: 2e 20 20 54 68 65 20 61 70 70 6c 69 63 61 74 69  .  The applicati
e050: 6f 6e 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 69  on should only i
e060: 6e 76 6f 6b 65 0a 2a 2a 20 73 71 6c 69 74 65 33  nvoke.** sqlite3
e070: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 61 6e  _initialize() an
e080: 64 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f  d sqlite3_shutdo
e090: 77 6e 28 29 2e 20 20 54 68 65 20 73 71 6c 69 74  wn().  The sqlit
e0a0: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 0a 2a 2a 20  e3_os_init().** 
e0b0: 69 6e 74 65 72 66 61 63 65 20 69 73 20 63 61 6c  interface is cal
e0c0: 6c 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  led automaticall
e0d0: 79 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 69  y by sqlite3_ini
e0e0: 74 69 61 6c 69 7a 65 28 29 20 61 6e 64 0a 2a 2a  tialize() and.**
e0f0: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28   sqlite3_os_end(
e100: 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 73  ) is called by s
e110: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
e120: 29 2e 20 20 41 70 70 72 6f 70 72 69 61 74 65 0a  ).  Appropriate.
e130: 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
e140: 6e 73 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f  ns for sqlite3_o
e150: 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 71 6c  s_init() and sql
e160: 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a 2a  ite3_os_end().**
e170: 20 61 72 65 20 62 75 69 6c 74 20 69 6e 74 6f 20   are built into 
e180: 53 51 4c 69 74 65 20 77 68 65 6e 20 69 74 20 69  SQLite when it i
e190: 73 20 63 6f 6d 70 69 6c 65 64 20 66 6f 72 20 55  s compiled for U
e1a0: 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c 20 6f 72  nix, Windows, or
e1b0: 20 4f 53 2f 32 2e 0a 2a 2a 20 57 68 65 6e 20 5b   OS/2..** When [
e1c0: 63 75 73 74 6f 6d 20 62 75 69 6c 64 73 20 7c 20  custom builds | 
e1d0: 62 75 69 6c 74 20 66 6f 72 20 6f 74 68 65 72 20  built for other 
e1e0: 70 6c 61 74 66 6f 72 6d 73 5d 0a 2a 2a 20 28 75  platforms].** (u
e1f0: 73 69 6e 67 20 74 68 65 20 5b 53 51 4c 49 54 45  sing the [SQLITE
e200: 5f 4f 53 5f 4f 54 48 45 52 3d 31 5d 20 63 6f 6d  _OS_OTHER=1] com
e210: 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6f 70 74  pile-time.** opt
e220: 69 6f 6e 29 20 74 68 65 20 61 70 70 6c 69 63 61  ion) the applica
e230: 74 69 6f 6e 20 6d 75 73 74 20 73 75 70 70 6c 79  tion must supply
e240: 20 61 20 73 75 69 74 61 62 6c 65 20 69 6d 70 6c   a suitable impl
e250: 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 0a 2a  ementation for.*
e260: 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69  * sqlite3_os_ini
e270: 74 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  t() and sqlite3_
e280: 6f 73 5f 65 6e 64 28 29 2e 20 20 41 6e 20 61 70  os_end().  An ap
e290: 70 6c 69 63 61 74 69 6f 6e 2d 73 75 70 70 6c 69  plication-suppli
e2a0: 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  ed.** implementa
e2b0: 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 5f  tion of sqlite3_
e2c0: 6f 73 5f 69 6e 69 74 28 29 20 6f 72 20 73 71 6c  os_init() or sql
e2d0: 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a 2a  ite3_os_end().**
e2e0: 20 6d 75 73 74 20 72 65 74 75 72 6e 20 5b 53 51   must return [SQ
e2f0: 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63  LITE_OK] on succ
e300: 65 73 73 20 61 6e 64 20 73 6f 6d 65 20 6f 74 68  ess and some oth
e310: 65 72 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20  er [error code] 
e320: 75 70 6f 6e 0a 2a 2a 20 66 61 69 6c 75 72 65 2e  upon.** failure.
e330: 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
e340: 6e 74 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  nt sqlite3_initi
e350: 61 6c 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51 4c  alize(void);.SQL
e360: 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
e370: 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 76 6f 69  te3_shutdown(voi
e380: 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  d);.SQLITE_API i
e390: 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  nt sqlite3_os_in
e3a0: 69 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45  it(void);.SQLITE
e3b0: 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
e3c0: 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 29 3b 0a 0a  _os_end(void);..
e3d0: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
e3e0: 43 6f 6e 66 69 67 75 72 69 6e 67 20 54 68 65 20  Configuring The 
e3f0: 53 51 4c 69 74 65 20 4c 69 62 72 61 72 79 20 7b  SQLite Library {
e400: 48 31 34 31 30 30 7d 20 3c 53 32 30 30 30 30 3e  H14100} <S20000>
e410: 3c 53 33 30 32 30 30 3e 0a 2a 2a 20 45 58 50 45  <S30200>.** EXPE
e420: 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54  RIMENTAL.**.** T
e430: 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  he sqlite3_confi
e440: 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73  g() interface is
e450: 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 67 6c   used to make gl
e460: 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61 74 69  obal configurati
e470: 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f  on.** changes to
e480: 20 53 51 4c 69 74 65 20 69 6e 20 6f 72 64 65 72   SQLite in order
e490: 20 74 6f 20 74 75 6e 65 20 53 51 4c 69 74 65 20   to tune SQLite 
e4a0: 74 6f 20 74 68 65 20 73 70 65 63 69 66 69 63 20  to the specific 
e4b0: 6e 65 65 64 73 20 6f 66 0a 2a 2a 20 74 68 65 20  needs of.** the 
e4c0: 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 54 68  application.  Th
e4d0: 65 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67  e default config
e4e0: 75 72 61 74 69 6f 6e 20 69 73 20 72 65 63 6f 6d  uration is recom
e4f0: 6d 65 6e 64 65 64 20 66 6f 72 20 6d 6f 73 74 0a  mended for most.
e500: 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  ** applications 
e510: 61 6e 64 20 73 6f 20 74 68 69 73 20 72 6f 75 74  and so this rout
e520: 69 6e 65 20 69 73 20 75 73 75 61 6c 6c 79 20 6e  ine is usually n
e530: 6f 74 20 6e 65 63 65 73 73 61 72 79 2e 20 20 49  ot necessary.  I
e540: 74 20 69 73 0a 2a 2a 20 70 72 6f 76 69 64 65 64  t is.** provided
e550: 20 74 6f 20 73 75 70 70 6f 72 74 20 72 61 72 65   to support rare
e560: 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 77 69   applications wi
e570: 74 68 20 75 6e 75 73 75 61 6c 20 6e 65 65 64 73  th unusual needs
e580: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
e590: 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74  te3_config() int
e5a0: 65 72 66 61 63 65 20 69 73 20 6e 6f 74 20 74 68  erface is not th
e5b0: 72 65 61 64 73 61 66 65 2e 20 20 54 68 65 20 61  readsafe.  The a
e5c0: 70 70 6c 69 63 61 74 69 6f 6e 0a 2a 2a 20 6d 75  pplication.** mu
e5d0: 73 74 20 69 6e 73 75 72 65 20 74 68 61 74 20 6e  st insure that n
e5e0: 6f 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 69  o other SQLite i
e5f0: 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 69 6e  nterfaces are in
e600: 76 6f 6b 65 64 20 62 79 20 6f 74 68 65 72 0a 2a  voked by other.*
e610: 2a 20 74 68 72 65 61 64 73 20 77 68 69 6c 65 20  * threads while 
e620: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
e630: 20 69 73 20 72 75 6e 6e 69 6e 67 2e 20 20 46 75   is running.  Fu
e640: 72 74 68 65 72 6d 6f 72 65 2c 20 73 71 6c 69 74  rthermore, sqlit
e650: 65 33 5f 63 6f 6e 66 69 67 28 29 0a 2a 2a 20 6d  e3_config().** m
e660: 61 79 20 6f 6e 6c 79 20 62 65 20 69 6e 76 6f 6b  ay only be invok
e670: 65 64 20 70 72 69 6f 72 20 74 6f 20 6c 69 62 72  ed prior to libr
e680: 61 72 79 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  ary initializati
e690: 6f 6e 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c  on using.** [sql
e6a0: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
e6b0: 29 5d 20 6f 72 20 61 66 74 65 72 20 73 68 75 74  )] or after shut
e6c0: 64 6f 77 6e 20 62 79 20 5b 73 71 6c 69 74 65 33  down by [sqlite3
e6d0: 5f 73 68 75 74 64 6f 77 6e 28 29 5d 2e 0a 2a 2a  _shutdown()]..**
e6e0: 20 4e 6f 74 65 2c 20 68 6f 77 65 76 65 72 2c 20   Note, however, 
e6f0: 74 68 61 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e  that sqlite3_con
e700: 66 69 67 28 29 20 63 61 6e 20 62 65 20 63 61 6c  fig() can be cal
e710: 6c 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74  led as part of t
e720: 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  he.** implementa
e730: 74 69 6f 6e 20 6f 66 20 61 6e 20 61 70 70 6c 69  tion of an appli
e740: 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 5b  cation-defined [
e750: 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28  sqlite3_os_init(
e760: 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  )]..**.** The fi
e770: 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
e780: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
e790: 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a   is an integer.*
e7a0: 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  * [SQLITE_CONFIG
e7b0: 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 20 7c 20  _SINGLETHREAD | 
e7c0: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70  configuration op
e7d0: 74 69 6f 6e 5d 20 74 68 61 74 20 64 65 74 65 72  tion] that deter
e7e0: 6d 69 6e 65 73 0a 2a 2a 20 77 68 61 74 20 70 72  mines.** what pr
e7f0: 6f 70 65 72 74 79 20 6f 66 20 53 51 4c 69 74 65  operty of SQLite
e800: 20 69 73 20 74 6f 20 62 65 20 63 6f 6e 66 69 67   is to be config
e810: 75 72 65 64 2e 20 20 53 75 62 73 65 71 75 65 6e  ured.  Subsequen
e820: 74 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 76  t arguments.** v
e830: 61 72 79 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  ary depending on
e840: 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e   the [SQLITE_CON
e850: 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44  FIG_SINGLETHREAD
e860: 20 7c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e   | configuration
e870: 20 6f 70 74 69 6f 6e 5d 0a 2a 2a 20 69 6e 20 74   option].** in t
e880: 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
e890: 74 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20  t..**.** When a 
e8a0: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70  configuration op
e8b0: 74 69 6f 6e 20 69 73 20 73 65 74 2c 20 73 71 6c  tion is set, sql
e8c0: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 72 65  ite3_config() re
e8d0: 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b  turns [SQLITE_OK
e8e0: 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 74  ]..** If the opt
e8f0: 69 6f 6e 20 69 73 20 75 6e 6b 6e 6f 77 6e 20 6f  ion is unknown o
e900: 72 20 53 51 4c 69 74 65 20 69 73 20 75 6e 61 62  r SQLite is unab
e910: 6c 65 20 74 6f 20 73 65 74 20 74 68 65 20 6f 70  le to set the op
e920: 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 74 68 69  tion.** then thi
e930: 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
e940: 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 5b 65 72  s a non-zero [er
e950: 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a  ror code]..**.**
e960: 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
e970: 2a 20 5b 48 31 34 31 30 33 5d 20 5b 48 31 34 31  * [H14103] [H141
e980: 30 36 5d 20 5b 48 31 34 31 32 30 5d 20 5b 48 31  06] [H14120] [H1
e990: 34 31 32 33 5d 20 5b 48 31 34 31 32 36 5d 20 5b  4123] [H14126] [
e9a0: 48 31 34 31 32 39 5d 20 5b 48 31 34 31 33 32 5d  H14129] [H14132]
e9b0: 20 5b 48 31 34 31 33 35 5d 0a 2a 2a 20 5b 48 31   [H14135].** [H1
e9c0: 34 31 33 38 5d 20 5b 48 31 34 31 34 31 5d 20 5b  4138] [H14141] [
e9d0: 48 31 34 31 34 34 5d 20 5b 48 31 34 31 34 37 5d  H14144] [H14147]
e9e0: 20 5b 48 31 34 31 35 30 5d 20 5b 48 31 34 31 35   [H14150] [H1415
e9f0: 33 5d 20 5b 48 31 34 31 35 36 5d 20 5b 48 31 34  3] [H14156] [H14
ea00: 31 35 39 5d 0a 2a 2a 20 5b 48 31 34 31 36 32 5d  159].** [H14162]
ea10: 20 5b 48 31 34 31 36 35 5d 20 5b 48 31 34 31 36   [H14165] [H1416
ea20: 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  8].*/.SQLITE_API
ea30: 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45   SQLITE_EXPERIME
ea40: 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33  NTAL int sqlite3
ea50: 5f 63 6f 6e 66 69 67 28 69 6e 74 2c 20 2e 2e 2e  _config(int, ...
ea60: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
ea70: 45 46 3a 20 43 6f 6e 66 69 67 75 72 65 20 64 61  EF: Configure da
ea80: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
ea90: 6e 73 20 20 7b 48 31 34 32 30 30 7d 20 3c 53 32  ns  {H14200} <S2
eaa0: 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d  0000>.** EXPERIM
eab0: 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ENTAL.**.** The 
eac0: 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69  sqlite3_db_confi
ead0: 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73  g() interface is
eae0: 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 63 6f   used to make co
eaf0: 6e 66 69 67 75 72 61 74 69 6f 6e 0a 2a 2a 20 63  nfiguration.** c
eb00: 68 61 6e 67 65 73 20 74 6f 20 61 20 5b 64 61 74  hanges to a [dat
eb10: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
eb20: 5d 2e 20 20 54 68 65 20 69 6e 74 65 72 66 61 63  ].  The interfac
eb30: 65 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 0a  e is similar to.
eb40: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66  ** [sqlite3_conf
eb50: 69 67 28 29 5d 20 65 78 63 65 70 74 20 74 68 61  ig()] except tha
eb60: 74 20 74 68 65 20 63 68 61 6e 67 65 73 20 61 70  t the changes ap
eb70: 70 6c 79 20 74 6f 20 61 20 73 69 6e 67 6c 65 0a  ply to a single.
eb80: 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ** [database con
eb90: 6e 65 63 74 69 6f 6e 5d 20 28 73 70 65 63 69 66  nection] (specif
eba0: 69 65 64 20 69 6e 20 74 68 65 20 66 69 72 73 74  ied in the first
ebb0: 20 61 72 67 75 6d 65 6e 74 29 2e 20 20 54 68 65   argument).  The
ebc0: 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 64 62 5f 63  .** sqlite3_db_c
ebd0: 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 63  onfig() interfac
ebe0: 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73  e can only be us
ebf0: 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61  ed immediately a
ec00: 66 74 65 72 0a 2a 2a 20 74 68 65 20 64 61 74 61  fter.** the data
ec10: 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
ec20: 69 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  is created using
ec30: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29   [sqlite3_open()
ec40: 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6f  ],.** [sqlite3_o
ec50: 70 65 6e 31 36 28 29 5d 2c 20 6f 72 20 5b 73 71  pen16()], or [sq
ec60: 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d  lite3_open_v2()]
ec70: 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65  .  .**.** The se
ec80: 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f  cond argument to
ec90: 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66   sqlite3_db_conf
eca0: 69 67 28 44 2c 56 2c 2e 2e 2e 29 20 20 69 73 20  ig(D,V,...)  is 
ecb0: 74 68 65 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 61  the.** configura
ecc0: 74 69 6f 6e 20 76 65 72 62 20 2d 20 61 6e 20 69  tion verb - an i
ecd0: 6e 74 65 67 65 72 20 63 6f 64 65 20 74 68 61 74  nteger code that
ece0: 20 69 6e 64 69 63 61 74 65 73 20 77 68 61 74 0a   indicates what.
ecf0: 2a 2a 20 61 73 70 65 63 74 20 6f 66 20 74 68 65  ** aspect of the
ed00: 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
ed10: 63 74 69 6f 6e 5d 20 69 73 20 62 65 69 6e 67 20  ction] is being 
ed20: 63 6f 6e 66 69 67 75 72 65 64 2e 0a 2a 2a 20 54  configured..** T
ed30: 68 65 20 6f 6e 6c 79 20 63 68 6f 69 63 65 20 66  he only choice f
ed40: 6f 72 20 74 68 69 73 20 76 61 6c 75 65 20 69 73  or this value is
ed50: 20 5b 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49   [SQLITE_DBCONFI
ed60: 47 5f 4c 4f 4f 4b 41 53 49 44 45 5d 2e 0a 2a 2a  G_LOOKASIDE]..**
ed70: 20 4e 65 77 20 76 65 72 62 73 20 61 72 65 20 6c   New verbs are l
ed80: 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 64 64 65  ikely to be adde
ed90: 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65  d in future rele
eda0: 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a  ases of SQLite..
edb0: 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 61 72  ** Additional ar
edc0: 67 75 6d 65 6e 74 73 20 64 65 70 65 6e 64 20 6f  guments depend o
edd0: 6e 20 74 68 65 20 76 65 72 62 2e 0a 2a 2a 0a 2a  n the verb..**.*
ede0: 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
edf0: 2a 2a 20 5b 48 31 34 32 30 33 5d 20 5b 48 31 34  ** [H14203] [H14
ee00: 32 30 36 5d 20 5b 48 31 34 32 30 39 5d 20 5b 48  206] [H14209] [H
ee10: 31 34 32 31 32 5d 20 5b 48 31 34 32 31 35 5d 0a  14212] [H14215].
ee20: 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  */.SQLITE_API SQ
ee30: 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41  LITE_EXPERIMENTA
ee40: 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 62  L int sqlite3_db
ee50: 5f 63 6f 6e 66 69 67 28 73 71 6c 69 74 65 33 2a  _config(sqlite3*
ee60: 2c 20 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 3b 0a  , int op, ...);.
ee70: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
ee80: 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69   Memory Allocati
ee90: 6f 6e 20 52 6f 75 74 69 6e 65 73 20 7b 48 31 30  on Routines {H10
eea0: 31 35 35 7d 20 3c 53 32 30 31 32 30 3e 0a 2a 2a  155} <S20120>.**
eeb0: 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
eec0: 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
eed0: 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 64  of this object d
eee0: 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72  efines the inter
eef0: 66 61 63 65 20 62 65 74 77 65 65 6e 20 53 51 4c  face between SQL
ef00: 69 74 65 0a 2a 2a 20 61 6e 64 20 6c 6f 77 2d 6c  ite.** and low-l
ef10: 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  evel memory allo
ef20: 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 2e  cation routines.
ef30: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65  .**.** This obje
ef40: 63 74 20 69 73 20 75 73 65 64 20 69 6e 20 6f 6e  ct is used in on
ef50: 6c 79 20 6f 6e 65 20 70 6c 61 63 65 20 69 6e 20  ly one place in 
ef60: 74 68 65 20 53 51 4c 69 74 65 20 69 6e 74 65 72  the SQLite inter
ef70: 66 61 63 65 2e 0a 2a 2a 20 41 20 70 6f 69 6e 74  face..** A point
ef80: 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63  er to an instanc
ef90: 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74  e of this object
efa0: 20 69 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74   is the argument
efb0: 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   to.** [sqlite3_
efc0: 63 6f 6e 66 69 67 28 29 5d 20 77 68 65 6e 20 74  config()] when t
efd0: 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  he configuration
efe0: 20 6f 70 74 69 6f 6e 20 69 73 0a 2a 2a 20 5b 53   option is.** [S
eff0: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c  QLITE_CONFIG_MAL
f000: 4c 4f 43 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f  LOC] or [SQLITE_
f010: 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c 4f 43  CONFIG_GETMALLOC
f020: 5d 2e 20 20 0a 2a 2a 20 42 79 20 63 72 65 61 74  ].  .** By creat
f030: 69 6e 67 20 61 6e 20 69 6e 73 74 61 6e 63 65 20  ing an instance 
f040: 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 0a 2a  of this object.*
f050: 2a 20 61 6e 64 20 70 61 73 73 69 6e 67 20 69 74  * and passing it
f060: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e   to [sqlite3_con
f070: 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e  fig]([SQLITE_CON
f080: 46 49 47 5f 4d 41 4c 4c 4f 43 5d 29 0a 2a 2a 20  FIG_MALLOC]).** 
f090: 64 75 72 69 6e 67 20 63 6f 6e 66 69 67 75 72 61  during configura
f0a0: 74 69 6f 6e 2c 20 61 6e 20 61 70 70 6c 69 63 61  tion, an applica
f0b0: 74 69 6f 6e 20 63 61 6e 20 73 70 65 63 69 66 79  tion can specify
f0c0: 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 0a   an alternative.
f0d0: 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ** memory alloca
f0e0: 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 66  tion subsystem f
f0f0: 6f 72 20 53 51 4c 69 74 65 20 74 6f 20 75 73 65  or SQLite to use
f100: 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 0a   for all of its.
f110: 2a 2a 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72  ** dynamic memor
f120: 79 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 4e  y needs..**.** N
f130: 6f 74 65 20 74 68 61 74 20 53 51 4c 69 74 65 20  ote that SQLite 
f140: 63 6f 6d 65 73 20 77 69 74 68 20 73 65 76 65 72  comes with sever
f150: 61 6c 20 5b 62 75 69 6c 74 2d 69 6e 20 6d 65 6d  al [built-in mem
f160: 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 73 5d 0a  ory allocators].
f170: 2a 2a 20 74 68 61 74 20 61 72 65 20 70 65 72 66  ** that are perf
f180: 65 63 74 6c 79 20 61 64 65 71 75 61 74 65 20 66  ectly adequate f
f190: 6f 72 20 74 68 65 20 6f 76 65 72 77 68 65 6c 6d  or the overwhelm
f1a0: 69 6e 67 20 6d 61 6a 6f 72 69 74 79 20 6f 66 20  ing majority of 
f1b0: 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20  applications.** 
f1c0: 61 6e 64 20 74 68 61 74 20 74 68 69 73 20 6f 62  and that this ob
f1d0: 6a 65 63 74 20 69 73 20 6f 6e 6c 79 20 75 73 65  ject is only use
f1e0: 66 75 6c 20 74 6f 20 61 20 74 69 6e 79 20 6d 69  ful to a tiny mi
f1f0: 6e 6f 72 69 74 79 20 6f 66 20 61 70 70 6c 69 63  nority of applic
f200: 61 74 69 6f 6e 73 0a 2a 2a 20 77 69 74 68 20 73  ations.** with s
f210: 70 65 63 69 61 6c 69 7a 65 64 20 6d 65 6d 6f 72  pecialized memor
f220: 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71  y allocation req
f230: 75 69 72 65 6d 65 6e 74 73 2e 20 20 54 68 69 73  uirements.  This
f240: 20 6f 62 6a 65 63 74 20 69 73 0a 2a 2a 20 61 6c   object is.** al
f250: 73 6f 20 75 73 65 64 20 64 75 72 69 6e 67 20 74  so used during t
f260: 65 73 74 69 6e 67 20 6f 66 20 53 51 4c 69 74 65  esting of SQLite
f270: 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 70 65   in order to spe
f280: 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74  cify an alternat
f290: 69 76 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c  ive.** memory al
f2a0: 6c 6f 63 61 74 6f 72 20 74 68 61 74 20 73 69 6d  locator that sim
f2b0: 75 6c 61 74 65 73 20 6d 65 6d 6f 72 79 20 6f 75  ulates memory ou
f2c0: 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e 64  t-of-memory cond
f2d0: 69 74 69 6f 6e 73 20 69 6e 0a 2a 2a 20 6f 72 64  itions in.** ord
f2e0: 65 72 20 74 6f 20 76 65 72 69 66 79 20 74 68 61  er to verify tha
f2f0: 74 20 53 51 4c 69 74 65 20 72 65 63 6f 76 65 72  t SQLite recover
f300: 73 20 67 72 61 63 65 66 75 6c 6c 79 20 66 72 6f  s gracefully fro
f310: 6d 20 73 75 63 68 0a 2a 2a 20 63 6f 6e 64 69 74  m such.** condit
f320: 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ions..**.** The 
f330: 78 4d 61 6c 6c 6f 63 20 61 6e 64 20 78 46 72 65  xMalloc and xFre
f340: 65 20 6d 65 74 68 6f 64 73 20 6d 75 73 74 20 77  e methods must w
f350: 6f 72 6b 20 6c 69 6b 65 20 74 68 65 0a 2a 2a 20  ork like the.** 
f360: 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72 65  malloc() and fre
f370: 65 28 29 20 66 75 6e 63 74 69 6f 6e 73 20 66 72  e() functions fr
f380: 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 72 64 20  om the standard 
f390: 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 20 54 68  C library..** Th
f3a0: 65 20 78 52 65 61 6c 6c 6f 63 20 6d 65 74 68 6f  e xRealloc metho
f3b0: 64 20 6d 75 73 74 20 77 6f 72 6b 20 6c 69 6b 65  d must work like
f3c0: 20 72 65 61 6c 6c 6f 63 28 29 20 66 72 6f 6d 20   realloc() from 
f3d0: 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c  the standard C l
f3e0: 69 62 72 61 72 79 0a 2a 2a 20 77 69 74 68 20 74  ibrary.** with t
f3f0: 68 65 20 65 78 63 65 70 74 69 6f 6e 20 74 68 61  he exception tha
f400: 74 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20  t if the second 
f410: 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 52 65 61  argument to xRea
f420: 6c 6c 6f 63 20 69 73 20 7a 65 72 6f 2c 0a 2a 2a  lloc is zero,.**
f430: 20 78 52 65 61 6c 6c 6f 63 20 6d 75 73 74 20 62   xRealloc must b
f440: 65 20 61 20 6e 6f 2d 6f 70 20 2d 20 69 74 20 6d  e a no-op - it m
f450: 75 73 74 20 6e 6f 74 20 70 65 72 66 6f 72 6d 20  ust not perform 
f460: 61 6e 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f  any allocation o
f470: 72 0a 2a 2a 20 64 65 61 6c 6c 6f 63 61 74 69 6f  r.** deallocatio
f480: 6e 2e 20 20 53 51 4c 69 74 65 20 67 75 61 72 61  n.  SQLite guara
f490: 6e 74 65 65 64 73 20 74 68 61 74 20 74 68 65 20  nteeds that the 
f4a0: 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
f4b0: 74 6f 0a 2a 2a 20 78 52 65 61 6c 6c 6f 63 20 69  to.** xRealloc i
f4c0: 73 20 61 6c 77 61 79 73 20 61 20 76 61 6c 75 65  s always a value
f4d0: 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 20 70   returned by a p
f4e0: 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 78 52 6f  rior call to xRo
f4f0: 75 6e 64 75 70 2e 0a 2a 2a 20 41 6e 64 20 73 6f  undup..** And so
f500: 20 69 6e 20 63 61 73 65 73 20 77 68 65 72 65 20   in cases where 
f510: 78 52 6f 75 6e 64 75 70 20 61 6c 77 61 79 73 20  xRoundup always 
f520: 72 65 74 75 72 6e 73 20 61 20 70 6f 73 69 74 69  returns a positi
f530: 76 65 20 6e 75 6d 62 65 72 2c 0a 2a 2a 20 78 52  ve number,.** xR
f540: 65 61 6c 6c 6f 63 20 63 61 6e 20 70 65 72 66 6f  ealloc can perfo
f550: 72 6d 20 65 78 61 63 74 6c 79 20 61 73 20 74 68  rm exactly as th
f560: 65 20 73 74 61 6e 64 61 72 64 20 6c 69 62 72 61  e standard libra
f570: 72 79 20 72 65 61 6c 6c 6f 63 28 29 20 61 6e 64  ry realloc() and
f580: 0a 2a 2a 20 73 74 69 6c 6c 20 62 65 20 69 6e 20  .** still be in 
f590: 63 6f 6d 70 6c 69 61 6e 63 65 20 77 69 74 68 20  compliance with 
f5a0: 74 68 69 73 20 73 70 65 63 69 66 69 63 61 74 69  this specificati
f5b0: 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 78 53 69 7a 65 20  on..**.** xSize 
f5c0: 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68  should return th
f5d0: 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65  e allocated size
f5e0: 20 6f 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c   of a memory all
f5f0: 6f 63 61 74 69 6f 6e 0a 2a 2a 20 70 72 65 76 69  ocation.** previ
f600: 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66  ously obtained f
f610: 72 6f 6d 20 78 4d 61 6c 6c 6f 63 20 6f 72 20 78  rom xMalloc or x
f620: 52 65 61 6c 6c 6f 63 2e 20 20 54 68 65 20 61 6c  Realloc.  The al
f630: 6c 6f 63 61 74 65 64 20 73 69 7a 65 0a 2a 2a 20  located size.** 
f640: 69 73 20 61 6c 77 61 79 73 20 61 74 20 6c 65 61  is always at lea
f650: 73 74 20 61 73 20 62 69 67 20 61 73 20 74 68 65  st as big as the
f660: 20 72 65 71 75 65 73 74 65 64 20 73 69 7a 65 20   requested size 
f670: 62 75 74 20 6d 61 79 20 62 65 20 6c 61 72 67 65  but may be large
f680: 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 6f  r..**.** The xRo
f690: 75 6e 64 75 70 20 6d 65 74 68 6f 64 20 72 65 74  undup method ret
f6a0: 75 72 6e 73 20 77 68 61 74 20 77 6f 75 6c 64 20  urns what would 
f6b0: 62 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64  be the allocated
f6c0: 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 61 20 6d 65   size of.** a me
f6d0: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
f6e0: 67 69 76 65 6e 20 61 20 70 61 72 74 69 63 75 6c  given a particul
f6f0: 61 72 20 72 65 71 75 65 73 74 65 64 20 73 69 7a  ar requested siz
f700: 65 2e 20 20 4d 6f 73 74 20 6d 65 6d 6f 72 79 0a  e.  Most memory.
f710: 2a 2a 20 61 6c 6c 6f 63 61 74 6f 72 73 20 72 6f  ** allocators ro
f720: 75 6e 64 20 75 70 20 6d 65 6d 6f 72 79 20 61 6c  und up memory al
f730: 6c 6f 63 61 74 69 6f 6e 73 20 61 74 20 6c 65 61  locations at lea
f740: 73 74 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6d  st to the next m
f750: 75 6c 74 69 70 6c 65 0a 2a 2a 20 6f 66 20 38 2e  ultiple.** of 8.
f760: 20 20 53 6f 6d 65 20 61 6c 6c 6f 63 61 74 6f 72    Some allocator
f770: 73 20 72 6f 75 6e 64 20 75 70 20 74 6f 20 61 20  s round up to a 
f780: 6c 61 72 67 65 72 20 6d 75 6c 74 69 70 6c 65 20  larger multiple 
f790: 6f 72 20 74 6f 20 61 20 70 6f 77 65 72 20 6f 66  or to a power of
f7a0: 20 32 2e 0a 2a 2a 20 45 76 65 72 79 20 6d 65 6d   2..** Every mem
f7b0: 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72  ory allocation r
f7c0: 65 71 75 65 73 74 20 63 6f 6d 69 6e 67 20 69 6e  equest coming in
f7d0: 20 74 68 72 6f 75 67 68 20 5b 73 71 6c 69 74 65   through [sqlite
f7e0: 33 5f 6d 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 6f  3_malloc()].** o
f7f0: 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  r [sqlite3_reall
f800: 6f 63 28 29 5d 20 66 69 72 73 74 20 63 61 6c 6c  oc()] first call
f810: 73 20 78 52 6f 75 6e 64 75 70 2e 20 20 49 66 20  s xRoundup.  If 
f820: 78 52 6f 75 6e 64 75 70 20 72 65 74 75 72 6e 73  xRoundup returns
f830: 20 30 2c 20 0a 2a 2a 20 74 68 61 74 20 63 61 75   0, .** that cau
f840: 73 65 73 20 74 68 65 20 63 6f 72 72 65 73 70 6f  ses the correspo
f850: 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c  nding memory all
f860: 6f 63 61 74 69 6f 6e 20 74 6f 20 66 61 69 6c 2e  ocation to fail.
f870: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e 69 74  .**.** The xInit
f880: 20 6d 65 74 68 6f 64 20 69 6e 69 74 69 61 6c 69   method initiali
f890: 7a 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 61  zes the memory a
f8a0: 6c 6c 6f 63 61 74 6f 72 2e 20 20 28 46 6f 72 20  llocator.  (For 
f8b0: 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 69 74 20 6d  example,.** it m
f8c0: 69 67 68 74 20 61 6c 6c 6f 63 61 74 65 20 61 6e  ight allocate an
f8d0: 79 20 72 65 71 75 69 72 65 20 6d 75 74 65 78 65  y require mutexe
f8e0: 73 20 6f 72 20 69 6e 69 74 69 61 6c 69 7a 65 20  s or initialize 
f8f0: 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 0a 2a 2a  internal data.**
f900: 20 73 74 72 75 63 74 75 72 65 73 2e 20 20 54 68   structures.  Th
f910: 65 20 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68  e xShutdown meth
f920: 6f 64 20 69 73 20 69 6e 76 6f 6b 65 64 20 28 69  od is invoked (i
f930: 6e 64 69 72 65 63 74 6c 79 29 20 62 79 0a 2a 2a  ndirectly) by.**
f940: 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f   [sqlite3_shutdo
f950: 77 6e 28 29 5d 20 61 6e 64 20 73 68 6f 75 6c 64  wn()] and should
f960: 20 64 65 61 6c 6c 6f 63 61 74 65 20 61 6e 79 20   deallocate any 
f970: 72 65 73 6f 75 72 63 65 73 20 61 63 71 75 69 72  resources acquir
f980: 65 64 0a 2a 2a 20 62 79 20 78 49 6e 69 74 2e 20  ed.** by xInit. 
f990: 20 54 68 65 20 70 41 70 70 44 61 74 61 20 70 6f   The pAppData po
f9a0: 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 73  inter is used as
f9b0: 20 74 68 65 20 6f 6e 6c 79 20 70 61 72 61 6d 65   the only parame
f9c0: 74 65 72 20 74 6f 0a 2a 2a 20 78 49 6e 69 74 20  ter to.** xInit 
f9d0: 61 6e 64 20 78 53 68 75 74 64 6f 77 6e 2e 0a 2a  and xShutdown..*
f9e0: 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 68 6f 6c 64  *.** SQLite hold
f9f0: 73 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4d 55  s the [SQLITE_MU
fa00: 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
fa10: 52 5d 20 6d 75 74 65 78 20 77 68 65 6e 20 69 74  R] mutex when it
fa20: 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 74 68 65 20   invokes.** the 
fa30: 78 49 6e 69 74 20 6d 65 74 68 6f 64 2c 20 73 6f  xInit method, so
fa40: 20 74 68 65 20 78 49 6e 69 74 20 6d 65 74 68 6f   the xInit metho
fa50: 64 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 74 68  d need not be th
fa60: 72 65 61 64 73 61 66 65 2e 20 20 54 68 65 0a 2a  readsafe.  The.*
fa70: 2a 20 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68  * xShutdown meth
fa80: 6f 64 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65  od is only calle
fa90: 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f  d from [sqlite3_
faa0: 73 68 75 74 64 6f 77 6e 28 29 5d 20 73 6f 20 69  shutdown()] so i
fab0: 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 6e 65  t does.** not ne
fac0: 65 64 20 74 6f 20 62 65 20 74 68 72 65 61 64 73  ed to be threads
fad0: 61 66 65 20 65 69 74 68 65 72 2e 20 20 46 6f 72  afe either.  For
fae0: 20 61 6c 6c 20 6f 74 68 65 72 20 6d 65 74 68 6f   all other metho
faf0: 64 73 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 68 6f  ds, SQLite.** ho
fb00: 6c 64 73 20 74 68 65 20 5b 53 51 4c 49 54 45 5f  lds the [SQLITE_
fb10: 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d  MUTEX_STATIC_MEM
fb20: 5d 20 6d 75 74 65 78 20 61 73 20 6c 6f 6e 67 20  ] mutex as long 
fb30: 61 73 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54  as the.** [SQLIT
fb40: 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54  E_CONFIG_MEMSTAT
fb50: 55 53 5d 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  US] configuratio
fb60: 6e 20 6f 70 74 69 6f 6e 20 69 73 20 74 75 72 6e  n option is turn
fb70: 65 64 20 6f 6e 20 28 77 68 69 63 68 0a 2a 2a 20  ed on (which.** 
fb80: 69 74 20 69 73 20 62 79 20 64 65 66 61 75 6c 74  it is by default
fb90: 29 20 61 6e 64 20 73 6f 20 74 68 65 20 6d 65 74  ) and so the met
fba0: 68 6f 64 73 20 61 72 65 20 61 75 74 6f 6d 61 74  hods are automat
fbb0: 69 63 61 6c 6c 79 20 73 65 72 69 61 6c 69 7a 65  ically serialize
fbc0: 64 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 69  d..** However, i
fbd0: 66 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  f [SQLITE_CONFIG
fbe0: 5f 4d 45 4d 53 54 41 54 55 53 5d 20 69 73 20 64  _MEMSTATUS] is d
fbf0: 69 73 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 68  isabled, then th
fc00: 65 20 6f 74 68 65 72 0a 2a 2a 20 6d 65 74 68 6f  e other.** metho
fc10: 64 73 20 6d 75 73 74 20 62 65 20 74 68 72 65 61  ds must be threa
fc20: 64 73 61 66 65 20 6f 72 20 65 6c 73 65 20 6d 61  dsafe or else ma
fc30: 6b 65 20 74 68 65 69 72 20 6f 77 6e 20 61 72 72  ke their own arr
fc40: 61 6e 67 65 6d 65 6e 74 73 20 66 6f 72 0a 2a 2a  angements for.**
fc50: 20 73 65 72 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a   serialization..
fc60: 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c  **.** SQLite wil
fc70: 6c 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 20 78  l never invoke x
fc80: 49 6e 69 74 28 29 20 6d 6f 72 65 20 74 68 61 6e  Init() more than
fc90: 20 6f 6e 63 65 20 77 69 74 68 6f 75 74 20 61 6e   once without an
fca0: 20 69 6e 74 65 72 76 65 6e 69 6e 67 0a 2a 2a 20   intervening.** 
fcb0: 63 61 6c 6c 20 74 6f 20 78 53 68 75 74 64 6f 77  call to xShutdow
fcc0: 6e 28 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  n()..*/.typedef 
fcd0: 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d  struct sqlite3_m
fce0: 65 6d 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74  em_methods sqlit
fcf0: 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 3b 0a  e3_mem_methods;.
fd00: 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d  struct sqlite3_m
fd10: 65 6d 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 20 76  em_methods {.  v
fd20: 6f 69 64 20 2a 28 2a 78 4d 61 6c 6c 6f 63 29 28  oid *(*xMalloc)(
fd30: 69 6e 74 29 3b 20 20 20 20 20 20 20 20 20 2f 2a  int);         /*
fd40: 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   Memory allocati
fd50: 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  on function */. 
fd60: 20 76 6f 69 64 20 28 2a 78 46 72 65 65 29 28 76   void (*xFree)(v
fd70: 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 20 20 20  oid*);          
fd80: 2f 2a 20 46 72 65 65 20 61 20 70 72 69 6f 72 20  /* Free a prior 
fd90: 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20  allocation */.  
fda0: 76 6f 69 64 20 2a 28 2a 78 52 65 61 6c 6c 6f 63  void *(*xRealloc
fdb0: 29 28 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 2f  )(void*,int);  /
fdc0: 2a 20 52 65 73 69 7a 65 20 61 6e 20 61 6c 6c 6f  * Resize an allo
fdd0: 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  cation */.  int 
fde0: 28 2a 78 53 69 7a 65 29 28 76 6f 69 64 2a 29 3b  (*xSize)(void*);
fdf0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
fe00: 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66  turn the size of
fe10: 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a   an allocation *
fe20: 2f 0a 20 20 69 6e 74 20 28 2a 78 52 6f 75 6e 64  /.  int (*xRound
fe30: 75 70 29 28 69 6e 74 29 3b 20 20 20 20 20 20 20  up)(int);       
fe40: 20 20 20 2f 2a 20 52 6f 75 6e 64 20 75 70 20 72     /* Round up r
fe50: 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 61  equest size to a
fe60: 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 2a  llocation size *
fe70: 2f 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69 74 29  /.  int (*xInit)
fe80: 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 20  (void*);        
fe90: 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65     /* Initialize
fea0: 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
feb0: 63 61 74 6f 72 20 2a 2f 0a 20 20 76 6f 69 64 20  cator */.  void 
fec0: 28 2a 78 53 68 75 74 64 6f 77 6e 29 28 76 6f 69  (*xShutdown)(voi
fed0: 64 2a 29 3b 20 20 20 20 20 20 2f 2a 20 44 65 69  d*);      /* Dei
fee0: 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65  nitialize the me
fef0: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 2a  mory allocator *
ff00: 2f 0a 20 20 76 6f 69 64 20 2a 70 41 70 70 44 61  /.  void *pAppDa
ff10: 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ta;             
ff20: 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74     /* Argument t
ff30: 6f 20 78 49 6e 69 74 28 29 20 61 6e 64 20 78 53  o xInit() and xS
ff40: 68 75 74 64 6f 77 6e 28 29 20 2a 2f 0a 7d 3b 0a  hutdown() */.};.
ff50: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
ff60: 20 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 4f   Configuration O
ff70: 70 74 69 6f 6e 73 20 7b 48 31 30 31 36 30 7d 20  ptions {H10160} 
ff80: 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45  <S20000>.** EXPE
ff90: 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54  RIMENTAL.**.** T
ffa0: 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61  hese constants a
ffb0: 72 65 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65  re the available
ffc0: 20 69 6e 74 65 67 65 72 20 63 6f 6e 66 69 67 75   integer configu
ffd0: 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 74  ration options t
ffe0: 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 70 61  hat.** can be pa
fff0: 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73  ssed as the firs
10000 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  t argument to th
10010 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  e [sqlite3_confi
10020 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a  g()] interface..
10030 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f 6e 66 69 67  **.** New config
10040 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20  uration options 
10050 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e 20  may be added in 
10060 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20  future releases 
10070 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 45 78  of SQLite..** Ex
10080 69 73 74 69 6e 67 20 63 6f 6e 66 69 67 75 72 61  isting configura
10090 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 6d 69 67  tion options mig
100a0 68 74 20 62 65 20 64 69 73 63 6f 6e 74 69 6e 75  ht be discontinu
100b0 65 64 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e  ed.  Application
100c0 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 68 65 63  s.** should chec
100d0 6b 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64  k the return cod
100e0 65 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f  e from [sqlite3_
100f0 63 6f 6e 66 69 67 28 29 5d 20 74 6f 20 6d 61 6b  config()] to mak
10100 65 20 73 75 72 65 20 74 68 61 74 0a 2a 2a 20 74  e sure that.** t
10110 68 65 20 63 61 6c 6c 20 77 6f 72 6b 65 64 2e 20  he call worked. 
10120 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f   The [sqlite3_co
10130 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63  nfig()] interfac
10140 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 61 0a  e will return a.
10150 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 5b 65 72 72  ** non-zero [err
10160 6f 72 20 63 6f 64 65 5d 20 69 66 20 61 20 64 69  or code] if a di
10170 73 63 6f 6e 74 69 6e 75 65 64 20 6f 72 20 75 6e  scontinued or un
10180 73 75 70 70 6f 72 74 65 64 20 63 6f 6e 66 69 67  supported config
10190 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 0a 2a  uration option.*
101a0 2a 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a  * is invoked..**
101b0 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e  .** <dl>.** <dt>
101c0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49  SQLITE_CONFIG_SI
101d0 4e 47 4c 45 54 48 52 45 41 44 3c 2f 64 74 3e 0a  NGLETHREAD</dt>.
101e0 2a 2a 20 3c 64 64 3e 54 68 65 72 65 20 61 72 65  ** <dd>There are
101f0 20 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 6f   no arguments to
10200 20 74 68 69 73 20 6f 70 74 69 6f 6e 2e 20 20 54   this option.  T
10210 68 69 73 20 6f 70 74 69 6f 6e 20 64 69 73 61 62  his option disab
10220 6c 65 73 0a 2a 2a 20 61 6c 6c 20 6d 75 74 65 78  les.** all mutex
10230 69 6e 67 20 61 6e 64 20 70 75 74 73 20 53 51 4c  ing and puts SQL
10240 69 74 65 20 69 6e 74 6f 20 61 20 6d 6f 64 65 20  ite into a mode 
10250 77 68 65 72 65 20 69 74 20 63 61 6e 20 6f 6e 6c  where it can onl
10260 79 20 62 65 20 75 73 65 64 0a 2a 2a 20 62 79 20  y be used.** by 
10270 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 2e  a single thread.
10280 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
10290 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55  SQLITE_CONFIG_MU
102a0 4c 54 49 54 48 52 45 41 44 3c 2f 64 74 3e 0a 2a  LTITHREAD</dt>.*
102b0 2a 20 3c 64 64 3e 54 68 65 72 65 20 61 72 65 20  * <dd>There are 
102c0 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  no arguments to 
102d0 74 68 69 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68  this option.  Th
102e0 69 73 20 6f 70 74 69 6f 6e 20 64 69 73 61 62 6c  is option disabl
102f0 65 73 0a 2a 2a 20 6d 75 74 65 78 69 6e 67 20 6f  es.** mutexing o
10300 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  n [database conn
10310 65 63 74 69 6f 6e 5d 20 61 6e 64 20 5b 70 72 65  ection] and [pre
10320 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
10330 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 54 68 65   objects..** The
10340 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 73 20   application is 
10350 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
10360 73 65 72 69 61 6c 69 7a 69 6e 67 20 61 63 63 65  serializing acce
10370 73 73 20 74 6f 0a 2a 2a 20 5b 64 61 74 61 62 61  ss to.** [databa
10380 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20  se connections] 
10390 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20 73 74  and [prepared st
103a0 61 74 65 6d 65 6e 74 73 5d 2e 20 20 42 75 74 20  atements].  But 
103b0 6f 74 68 65 72 20 6d 75 74 65 78 65 73 0a 2a 2a  other mutexes.**
103c0 20 61 72 65 20 65 6e 61 62 6c 65 64 20 73 6f 20   are enabled so 
103d0 74 68 61 74 20 53 51 4c 69 74 65 20 77 69 6c 6c  that SQLite will
103e0 20 62 65 20 73 61 66 65 20 74 6f 20 75 73 65 20   be safe to use 
103f0 69 6e 20 61 20 6d 75 6c 74 69 2d 74 68 72 65 61  in a multi-threa
10400 64 65 64 0a 2a 2a 20 65 6e 76 69 72 6f 6e 6d 65  ded.** environme
10410 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 6e 6f  nt as long as no
10420 20 74 77 6f 20 74 68 72 65 61 64 73 20 61 74 74   two threads att
10430 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65 20  empt to use the 
10440 73 61 6d 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73  same.** [databas
10450 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 74  e connection] at
10460 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 20   the same time. 
10470 20 53 65 65 20 74 68 65 20 5b 74 68 72 65 61 64   See the [thread
10480 69 6e 67 20 6d 6f 64 65 5d 0a 2a 2a 20 64 6f 63  ing mode].** doc
10490 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61  umentation for a
104a0 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
104b0 61 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  ation.</dd>.**.*
104c0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e  * <dt>SQLITE_CON
104d0 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44 3c 2f  FIG_SERIALIZED</
104e0 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 72 65  dt>.** <dd>There
104f0 20 61 72 65 20 6e 6f 20 61 72 67 75 6d 65 6e 74   are no argument
10500 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e  s to this option
10510 2e 20 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 65  .  This option e
10520 6e 61 62 6c 65 73 0a 2a 2a 20 61 6c 6c 20 6d 75  nables.** all mu
10530 74 65 78 65 73 20 69 6e 63 6c 75 64 69 6e 67 20  texes including 
10540 74 68 65 20 72 65 63 75 72 73 69 76 65 0a 2a 2a  the recursive.**
10550 20 6d 75 74 65 78 65 73 20 6f 6e 20 5b 64 61 74   mutexes on [dat
10560 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
10570 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20  ] and [prepared 
10580 73 74 61 74 65 6d 65 6e 74 5d 20 6f 62 6a 65 63  statement] objec
10590 74 73 2e 0a 2a 2a 20 49 6e 20 74 68 69 73 20 6d  ts..** In this m
105a0 6f 64 65 20 28 77 68 69 63 68 20 69 73 20 74 68  ode (which is th
105b0 65 20 64 65 66 61 75 6c 74 20 77 68 65 6e 20 53  e default when S
105c0 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65  QLite is compile
105d0 64 20 77 69 74 68 0a 2a 2a 20 5b 53 51 4c 49 54  d with.** [SQLIT
105e0 45 5f 54 48 52 45 41 44 53 41 46 45 3d 31 5d 29  E_THREADSAFE=1])
105f0 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
10600 61 72 79 20 77 69 6c 6c 20 69 74 73 65 6c 66 20  ary will itself 
10610 73 65 72 69 61 6c 69 7a 65 20 61 63 63 65 73 73  serialize access
10620 0a 2a 2a 20 74 6f 20 5b 64 61 74 61 62 61 73 65  .** to [database
10630 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20 61 6e   connections] an
10640 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  d [prepared stat
10650 65 6d 65 6e 74 73 5d 20 73 6f 20 74 68 61 74 20  ements] so that 
10660 74 68 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69  the.** applicati
10670 6f 6e 20 69 73 20 66 72 65 65 20 74 6f 20 75 73  on is free to us
10680 65 20 74 68 65 20 73 61 6d 65 20 5b 64 61 74 61  e the same [data
10690 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
106a0 20 6f 72 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20   or the.** same 
106b0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
106c0 65 6e 74 5d 20 69 6e 20 64 69 66 66 65 72 65 6e  ent] in differen
106d0 74 20 74 68 72 65 61 64 73 20 61 74 20 74 68 65  t threads at the
106e0 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a 20 53   same time..** S
106f0 65 65 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e  ee the [threadin
10700 67 20 6d 6f 64 65 5d 20 64 6f 63 75 6d 65 6e 74  g mode] document
10710 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69  ation for additi
10720 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
10730 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
10740 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  >SQLITE_CONFIG_M
10750 41 4c 4c 4f 43 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  ALLOC</dt>.** <d
10760 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61  d>This option ta
10770 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67  kes a single arg
10780 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 61  ument which is a
10790 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a   pointer to an.*
107a0 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  * instance of th
107b0 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  e [sqlite3_mem_m
107c0 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74 75 72  ethods] structur
107d0 65 2e 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74  e.  The argument
107e0 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 61 6c   specifies.** al
107f0 74 65 72 6e 61 74 69 76 65 20 6c 6f 77 2d 6c 65  ternative low-le
10800 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  vel memory alloc
10810 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 74  ation routines t
10820 6f 20 62 65 20 75 73 65 64 20 69 6e 20 70 6c 61  o be used in pla
10830 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6d 65 6d  ce of.** the mem
10840 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72  ory allocation r
10850 6f 75 74 69 6e 65 73 20 62 75 69 6c 74 20 69 6e  outines built in
10860 74 6f 20 53 51 4c 69 74 65 2e 3c 2f 64 64 3e 0a  to SQLite.</dd>.
10870 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
10880 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c 4f  _CONFIG_GETMALLO
10890 43 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  C</dt>.** <dd>Th
108a0 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20  is option takes 
108b0 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e  a single argumen
108c0 74 20 77 68 69 63 68 20 69 73 20 61 20 70 6f 69  t which is a poi
108d0 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e  nter to an.** in
108e0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73  stance of the [s
108f0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
10900 64 73 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20  ds] structure.  
10910 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d  The [sqlite3_mem
10920 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 73 74 72  _methods].** str
10930 75 63 74 75 72 65 20 69 73 20 66 69 6c 6c 65 64  ucture is filled
10940 20 77 69 74 68 20 74 68 65 20 63 75 72 72 65 6e   with the curren
10950 74 6c 79 20 64 65 66 69 6e 65 64 20 6d 65 6d 6f  tly defined memo
10960 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f  ry allocation ro
10970 75 74 69 6e 65 73 2e 0a 2a 2a 20 54 68 69 73 20  utines..** This 
10980 6f 70 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73  option can be us
10990 65 64 20 74 6f 20 6f 76 65 72 6c 6f 61 64 20 74  ed to overload t
109a0 68 65 20 64 65 66 61 75 6c 74 20 6d 65 6d 6f 72  he default memor
109b0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20  y allocation.** 
109c0 72 6f 75 74 69 6e 65 73 20 77 69 74 68 20 61 20  routines with a 
109d0 77 72 61 70 70 65 72 20 74 68 61 74 20 73 69 6d  wrapper that sim
109e0 75 6c 61 74 69 6f 6e 73 20 6d 65 6d 6f 72 79 20  ulations memory 
109f0 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 75  allocation failu
10a00 72 65 20 6f 72 0a 2a 2a 20 74 72 61 63 6b 73 20  re or.** tracks 
10a10 6d 65 6d 6f 72 79 20 75 73 61 67 65 2c 20 66 6f  memory usage, fo
10a20 72 20 65 78 61 6d 70 6c 65 2e 3c 2f 64 64 3e 0a  r example.</dd>.
10a30 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
10a40 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55  _CONFIG_MEMSTATU
10a50 53 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  S</dt>.** <dd>Th
10a60 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20  is option takes 
10a70 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20  single argument 
10a80 6f 66 20 74 79 70 65 20 69 6e 74 2c 20 69 6e 74  of type int, int
10a90 65 72 70 72 65 74 65 64 20 61 73 20 61 20 0a 2a  erpreted as a .*
10aa0 2a 20 62 6f 6f 6c 65 61 6e 2c 20 77 68 69 63 68  * boolean, which
10ab0 20 65 6e 61 62 6c 65 73 20 6f 72 20 64 69 73 61   enables or disa
10ac0 62 6c 65 73 20 74 68 65 20 63 6f 6c 6c 65 63 74  bles the collect
10ad0 69 6f 6e 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c  ion of memory al
10ae0 6c 6f 63 61 74 69 6f 6e 20 0a 2a 2a 20 73 74 61  location .** sta
10af0 74 69 73 74 69 63 73 2e 20 57 68 65 6e 20 64 69  tistics. When di
10b00 73 61 62 6c 65 64 2c 20 74 68 65 20 66 6f 6c 6c  sabled, the foll
10b10 6f 77 69 6e 67 20 53 51 4c 69 74 65 20 69 6e 74  owing SQLite int
10b20 65 72 66 61 63 65 73 20 62 65 63 6f 6d 65 20 0a  erfaces become .
10b30 2a 2a 20 6e 6f 6e 2d 6f 70 65 72 61 74 69 6f 6e  ** non-operation
10b40 61 6c 3a 0a 2a 2a 20 20 20 3c 75 6c 3e 0a 2a 2a  al:.**   <ul>.**
10b50 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 33     <li> [sqlite3
10b60 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 5d 0a  _memory_used()].
10b70 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74  **   <li> [sqlit
10b80 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61  e3_memory_highwa
10b90 74 65 72 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e  ter()].**   <li>
10ba0 20 5b 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68   [sqlite3_soft_h
10bb0 65 61 70 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20  eap_limit()].** 
10bc0 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 33 5f    <li> [sqlite3_
10bd0 73 74 61 74 75 73 28 29 5d 0a 2a 2a 20 20 20 3c  status()].**   <
10be0 2f 75 6c 3e 0a 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a  /ul>.** </dd>.**
10bf0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43  .** <dt>SQLITE_C
10c00 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 3c 2f 64  ONFIG_SCRATCH</d
10c10 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f  t>.** <dd>This o
10c20 70 74 69 6f 6e 20 73 70 65 63 69 66 69 65 73 20  ption specifies 
10c30 61 20 73 74 61 74 69 63 20 6d 65 6d 6f 72 79 20  a static memory 
10c40 62 75 66 66 65 72 20 74 68 61 74 20 53 51 4c 69  buffer that SQLi
10c50 74 65 20 63 61 6e 20 75 73 65 20 66 6f 72 0a 2a  te can use for.*
10c60 2a 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79  * scratch memory
10c70 2e 20 20 54 68 65 72 65 20 61 72 65 20 74 68 72  .  There are thr
10c80 65 65 20 61 72 67 75 6d 65 6e 74 73 3a 20 20 41  ee arguments:  A
10c90 20 70 6f 69 6e 74 65 72 20 61 6e 20 38 2d 62 79   pointer an 8-by
10ca0 74 65 0a 2a 2a 20 61 6c 69 67 6e 65 64 20 6d 65  te.** aligned me
10cb0 6d 6f 72 79 20 62 75 66 66 65 72 20 66 72 6f 6d  mory buffer from
10cc0 20 77 68 69 63 68 20 74 68 65 20 73 63 72 61 63   which the scrac
10cd0 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 77 69  h allocations wi
10ce0 6c 6c 20 62 65 0a 2a 2a 20 64 72 61 77 6e 2c 20  ll be.** drawn, 
10cf0 74 68 65 20 73 69 7a 65 20 6f 66 20 65 61 63 68  the size of each
10d00 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 74   scratch allocat
10d10 69 6f 6e 20 28 73 7a 29 2c 0a 2a 2a 20 61 6e 64  ion (sz),.** and
10d20 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   the maximum num
10d30 62 65 72 20 6f 66 20 73 63 72 61 74 63 68 20 61  ber of scratch a
10d40 6c 6c 6f 63 61 74 69 6f 6e 73 20 28 4e 29 2e 20  llocations (N). 
10d50 20 54 68 65 20 73 7a 0a 2a 2a 20 61 72 67 75 6d   The sz.** argum
10d60 65 6e 74 20 6d 75 73 74 20 62 65 20 61 20 6d 75  ent must be a mu
10d70 6c 74 69 70 6c 65 20 6f 66 20 31 36 2e 20 54 68  ltiple of 16. Th
10d80 65 20 73 7a 20 70 61 72 61 6d 65 74 65 72 20 73  e sz parameter s
10d90 68 6f 75 6c 64 20 62 65 20 61 20 66 65 77 20 62  hould be a few b
10da0 79 74 65 73 0a 2a 2a 20 6c 61 72 67 65 72 20 74  ytes.** larger t
10db0 68 61 6e 20 74 68 65 20 61 63 74 75 61 6c 20 73  han the actual s
10dc0 63 72 61 74 63 68 20 73 70 61 63 65 20 72 65 71  cratch space req
10dd0 75 69 72 65 64 20 64 75 65 20 74 6f 20 69 6e 74  uired due to int
10de0 65 72 6e 61 6c 20 6f 76 65 72 68 65 61 64 2e 0a  ernal overhead..
10df0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67  ** The first arg
10e00 75 6d 65 6e 74 20 73 68 6f 75 6c 64 20 70 6f 69  ument should poi
10e10 6e 74 65 72 20 74 6f 20 61 6e 20 38 2d 62 79 74  nter to an 8-byt
10e20 65 20 61 6c 69 67 6e 65 64 20 62 75 66 66 65 72  e aligned buffer
10e30 0a 2a 2a 20 6f 66 20 61 74 20 6c 65 61 73 74 20  .** of at least 
10e40 73 7a 2a 4e 20 62 79 74 65 73 20 6f 66 20 6d 65  sz*N bytes of me
10e50 6d 6f 72 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 20  mory..** SQLite 
10e60 77 69 6c 6c 20 75 73 65 20 6e 6f 20 6d 6f 72 65  will use no more
10e70 20 74 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63   than one scratc
10e80 68 20 62 75 66 66 65 72 20 61 74 20 6f 6e 63 65  h buffer at once
10e90 20 70 65 72 20 74 68 72 65 61 64 2c 20 73 6f 0a   per thread, so.
10ea0 2a 2a 20 4e 20 73 68 6f 75 6c 64 20 62 65 20 73  ** N should be s
10eb0 65 74 20 74 6f 20 74 68 65 20 65 78 70 65 63 74  et to the expect
10ec0 65 64 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  ed maximum numbe
10ed0 72 20 6f 66 20 74 68 72 65 61 64 73 2e 20 20 54  r of threads.  T
10ee0 68 65 20 73 7a 0a 2a 2a 20 70 61 72 61 6d 65 74  he sz.** paramet
10ef0 65 72 20 73 68 6f 75 6c 64 20 62 65 20 36 20 74  er should be 6 t
10f00 69 6d 65 73 20 74 68 65 20 73 69 7a 65 20 6f 66  imes the size of
10f10 20 74 68 65 20 6c 61 72 67 65 73 74 20 64 61 74   the largest dat
10f20 61 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 2e  abase page size.
10f30 0a 2a 2a 20 53 63 72 61 74 63 68 20 62 75 66 66  .** Scratch buff
10f40 65 72 73 20 61 72 65 20 75 73 65 64 20 61 73 20  ers are used as 
10f50 70 61 72 74 20 6f 66 20 74 68 65 20 62 74 72 65  part of the btre
10f60 65 20 62 61 6c 61 6e 63 65 20 6f 70 65 72 61 74  e balance operat
10f70 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 54 68 65 20  ion.  If.** The 
10f80 62 74 72 65 65 20 62 61 6c 61 6e 63 65 72 20 6e  btree balancer n
10f90 65 65 64 73 20 61 64 64 69 74 69 6f 6e 61 6c 20  eeds additional 
10fa0 6d 65 6d 6f 72 79 20 62 65 79 6f 6e 64 20 77 68  memory beyond wh
10fb0 61 74 20 69 73 20 70 72 6f 76 69 64 65 64 20 62  at is provided b
10fc0 79 0a 2a 2a 20 73 63 72 61 74 63 68 20 62 75 66  y.** scratch buf
10fd0 66 65 72 73 20 6f 72 20 69 66 20 6e 6f 20 73 63  fers or if no sc
10fe0 72 61 74 63 68 20 62 75 66 66 65 72 20 73 70 61  ratch buffer spa
10ff0 63 65 20 69 73 20 73 70 65 63 69 66 69 65 64 2c  ce is specified,
11000 20 74 68 65 6e 20 53 51 4c 69 74 65 0a 2a 2a 20   then SQLite.** 
11010 67 6f 65 73 20 74 6f 20 5b 73 71 6c 69 74 65 33  goes to [sqlite3
11020 5f 6d 61 6c 6c 6f 63 28 29 5d 20 74 6f 20 6f 62  _malloc()] to ob
11030 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20  tain the memory 
11040 69 74 20 6e 65 65 64 73 2e 3c 2f 64 64 3e 0a 2a  it needs.</dd>.*
11050 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
11060 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
11070 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
11080 73 20 6f 70 74 69 6f 6e 20 73 70 65 63 69 66 69  s option specifi
11090 65 73 20 61 20 73 74 61 74 69 63 20 6d 65 6d 6f  es a static memo
110a0 72 79 20 62 75 66 66 65 72 20 74 68 61 74 20 53  ry buffer that S
110b0 51 4c 69 74 65 20 63 61 6e 20 75 73 65 20 66 6f  QLite can use fo
110c0 72 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73  r.** the databas
110d0 65 20 70 61 67 65 20 63 61 63 68 65 20 77 69 74  e page cache wit
110e0 68 20 74 68 65 20 64 65 66 61 75 6c 74 20 70 61  h the default pa
110f0 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65  ge cache impleme
11100 6e 61 74 69 6f 6e 2e 20 20 0a 2a 2a 20 54 68 69  nation.  .** Thi
11110 73 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  s configuration 
11120 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 75 73  should not be us
11130 65 64 20 69 66 20 61 6e 20 61 70 70 6c 69 63 61  ed if an applica
11140 74 69 6f 6e 2d 64 65 66 69 6e 65 20 70 61 67 65  tion-define page
11150 0a 2a 2a 20 63 61 63 68 65 20 69 6d 70 6c 65 6d  .** cache implem
11160 65 6e 74 61 74 69 6f 6e 20 69 73 20 6c 6f 61 64  entation is load
11170 65 64 20 75 73 69 6e 67 20 74 68 65 20 53 51 4c  ed using the SQL
11180 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48  ITE_CONFIG_PCACH
11190 45 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65  E option..** The
111a0 72 65 20 61 72 65 20 74 68 72 65 65 20 61 72 67  re are three arg
111b0 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 6f  uments to this o
111c0 70 74 69 6f 6e 3a 20 41 20 70 6f 69 6e 74 65 72  ption: A pointer
111d0 20 74 6f 20 38 2d 62 79 74 65 20 61 6c 69 67 6e   to 8-byte align
111e0 65 64 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 74 68  ed.** memory, th
111f0 65 20 73 69 7a 65 20 6f 66 20 65 61 63 68 20 70  e size of each p
11200 61 67 65 20 62 75 66 66 65 72 20 28 73 7a 29 2c  age buffer (sz),
11210 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20   and the number 
11220 6f 66 20 70 61 67 65 73 20 28 4e 29 2e 0a 2a 2a  of pages (N)..**
11230 20 54 68 65 20 73 7a 20 61 72 67 75 6d 65 6e 74   The sz argument
11240 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 20 73   should be the s
11250 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65  ize of the large
11260 73 74 20 64 61 74 61 62 61 73 65 20 70 61 67 65  st database page
11270 0a 2a 2a 20 28 61 20 70 6f 77 65 72 20 6f 66 20  .** (a power of 
11280 74 77 6f 20 62 65 74 77 65 65 6e 20 35 31 32 20  two between 512 
11290 61 6e 64 20 33 32 37 36 38 29 20 70 6c 75 73 20  and 32768) plus 
112a0 61 20 6c 69 74 74 6c 65 20 65 78 74 72 61 20 66  a little extra f
112b0 6f 72 20 65 61 63 68 0a 2a 2a 20 70 61 67 65 20  or each.** page 
112c0 68 65 61 64 65 72 2e 20 20 54 68 65 20 70 61 67  header.  The pag
112d0 65 20 68 65 61 64 65 72 20 73 69 7a 65 20 69 73  e header size is
112e0 20 32 30 20 74 6f 20 34 30 20 62 79 74 65 73 20   20 to 40 bytes 
112f0 64 65 70 65 6e 64 69 6e 67 20 6f 6e 0a 2a 2a 20  depending on.** 
11300 74 68 65 20 68 6f 73 74 20 61 72 63 68 69 74 65  the host archite
11310 63 74 75 72 65 2e 20 20 49 74 20 69 73 20 68 61  cture.  It is ha
11320 72 6d 6c 65 73 73 2c 20 61 70 61 72 74 20 66 72  rmless, apart fr
11330 6f 6d 20 74 68 65 20 77 61 73 74 65 64 20 6d 65  om the wasted me
11340 6d 6f 72 79 2c 0a 2a 2a 20 74 6f 20 6d 61 6b 65  mory,.** to make
11350 20 73 7a 20 61 20 6c 69 74 74 6c 65 20 74 6f 6f   sz a little too
11360 20 6c 61 72 67 65 2e 20 20 54 68 65 20 66 69 72   large.  The fir
11370 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 73  st.** argument s
11380 68 6f 75 6c 64 20 70 6f 69 6e 74 20 74 6f 20 61  hould point to a
11390 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20  n allocation of 
113a0 61 74 20 6c 65 61 73 74 20 73 7a 2a 4e 20 62 79  at least sz*N by
113b0 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a  tes of memory..*
113c0 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73  * SQLite will us
113d0 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 70 72 6f  e the memory pro
113e0 76 69 64 65 64 20 62 79 20 74 68 65 20 66 69 72  vided by the fir
113f0 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73  st argument to s
11400 61 74 69 73 66 79 20 69 74 73 0a 2a 2a 20 6d 65  atisfy its.** me
11410 6d 6f 72 79 20 6e 65 65 64 73 20 66 6f 72 20 74  mory needs for t
11420 68 65 20 66 69 72 73 74 20 4e 20 70 61 67 65 73  he first N pages
11430 20 74 68 61 74 20 69 74 20 61 64 64 73 20 74 6f   that it adds to
11440 20 63 61 63 68 65 2e 20 20 49 66 20 61 64 64 69   cache.  If addi
11450 74 69 6f 6e 61 6c 0a 2a 2a 20 70 61 67 65 20 63  tional.** page c
11460 61 63 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 6e  ache memory is n
11470 65 65 64 65 64 20 62 65 79 6f 6e 64 20 77 68 61  eeded beyond wha
11480 74 20 69 73 20 70 72 6f 76 69 64 65 64 20 62 79  t is provided by
11490 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 74 68   this option, th
114a0 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 67 6f 65  en.** SQLite goe
114b0 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61  s to [sqlite3_ma
114c0 6c 6c 6f 63 28 29 5d 20 66 6f 72 20 74 68 65 20  lloc()] for the 
114d0 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 6f 72 61  additional stora
114e0 67 65 20 73 70 61 63 65 2e 0a 2a 2a 20 54 68 65  ge space..** The
114f0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
11500 6d 69 67 68 74 20 75 73 65 20 6f 6e 65 20 6f 72  might use one or
11510 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 4e 20 62   more of the N b
11520 75 66 66 65 72 73 20 74 6f 20 68 6f 6c 64 20 0a  uffers to hold .
11530 2a 2a 20 6d 65 6d 6f 72 79 20 61 63 63 6f 75 6e  ** memory accoun
11540 74 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ting information
11550 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69 6e  . The pointer in
11560 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
11570 65 6e 74 20 6d 75 73 74 0a 2a 2a 20 62 65 20 61  ent must.** be a
11580 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62  ligned to an 8-b
11590 79 74 65 20 62 6f 75 6e 64 61 72 79 20 6f 72 20  yte boundary or 
115a0 73 75 62 73 65 71 75 65 6e 74 20 62 65 68 61 76  subsequent behav
115b0 69 6f 72 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a  ior of SQLite.**
115c0 20 77 69 6c 6c 20 62 65 20 75 6e 64 65 66 69 6e   will be undefin
115d0 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  ed.</dd>.**.** <
115e0 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  dt>SQLITE_CONFIG
115f0 5f 48 45 41 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  _HEAP</dt>.** <d
11600 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 73 70  d>This option sp
11610 65 63 69 66 69 65 73 20 61 20 73 74 61 74 69 63  ecifies a static
11620 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74   memory buffer t
11630 68 61 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  hat SQLite will 
11640 75 73 65 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20 6f  use.** for all o
11650 66 20 69 74 73 20 64 79 6e 61 6d 69 63 20 6d 65  f its dynamic me
11660 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
11670 6e 65 65 64 73 20 62 65 79 6f 6e 64 20 74 68 6f  needs beyond tho
11680 73 65 20 70 72 6f 76 69 64 65 64 0a 2a 2a 20 66  se provided.** f
11690 6f 72 20 62 79 20 5b 53 51 4c 49 54 45 5f 43 4f  or by [SQLITE_CO
116a0 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 20 61 6e  NFIG_SCRATCH] an
116b0 64 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  d [SQLITE_CONFIG
116c0 5f 50 41 47 45 43 41 43 48 45 5d 2e 0a 2a 2a 20  _PAGECACHE]..** 
116d0 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65 20  There are three 
116e0 61 72 67 75 6d 65 6e 74 73 3a 20 41 6e 20 38 2d  arguments: An 8-
116f0 62 79 74 65 20 61 6c 69 67 6e 65 64 20 70 6f 69  byte aligned poi
11700 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65 6d 6f  nter to the memo
11710 72 79 2c 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65  ry,.** the numbe
11720 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
11730 65 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 2c  e memory buffer,
11740 20 61 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d   and the minimum
11750 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65   allocation size
11760 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 72 73  ..** If the firs
11770 74 20 70 6f 69 6e 74 65 72 20 28 74 68 65 20 6d  t pointer (the m
11780 65 6d 6f 72 79 20 70 6f 69 6e 74 65 72 29 20 69  emory pointer) i
11790 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 53 51 4c  s NULL, then SQL
117a0 69 74 65 20 72 65 76 65 72 74 73 0a 2a 2a 20 74  ite reverts.** t
117b0 6f 20 75 73 69 6e 67 20 69 74 73 20 64 65 66 61  o using its defa
117c0 75 6c 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ult memory alloc
117d0 61 74 6f 72 20 28 74 68 65 20 73 79 73 74 65 6d  ator (the system
117e0 20 6d 61 6c 6c 6f 63 28 29 20 69 6d 70 6c 65 6d   malloc() implem
117f0 65 6e 74 61 74 69 6f 6e 29 2c 0a 2a 2a 20 75 6e  entation),.** un
11800 64 6f 69 6e 67 20 61 6e 79 20 70 72 69 6f 72 20  doing any prior 
11810 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 53  invocation of [S
11820 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c  QLITE_CONFIG_MAL
11830 4c 4f 43 5d 2e 20 20 49 66 20 74 68 65 0a 2a 2a  LOC].  If the.**
11840 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 72 20   memory pointer 
11850 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e 64 20  is not NULL and 
11860 65 69 74 68 65 72 20 5b 53 51 4c 49 54 45 5f 45  either [SQLITE_E
11870 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 5d 20 6f  NABLE_MEMSYS3] o
11880 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41  r.** [SQLITE_ENA
11890 42 4c 45 5f 4d 45 4d 53 59 53 35 5d 20 61 72 65  BLE_MEMSYS5] are
118a0 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74   defined, then t
118b0 68 65 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6d  he alternative m
118c0 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74  emory.** allocat
118d0 6f 72 20 69 73 20 65 6e 67 61 67 65 64 20 74 6f  or is engaged to
118e0 20 68 61 6e 64 6c 65 20 61 6c 6c 20 6f 66 20 53   handle all of S
118f0 51 4c 69 74 65 73 20 6d 65 6d 6f 72 79 20 61 6c  QLites memory al
11900 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e 0a  location needs..
11910 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 6f 69  ** The first poi
11920 6e 74 65 72 20 28 74 68 65 20 6d 65 6d 6f 72 79  nter (the memory
11930 20 70 6f 69 6e 74 65 72 29 20 6d 75 73 74 20 62   pointer) must b
11940 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20  e aligned to an 
11950 38 2d 62 79 74 65 0a 2a 2a 20 62 6f 75 6e 64 61  8-byte.** bounda
11960 72 79 20 6f 72 20 73 75 62 73 65 71 75 65 6e 74  ry or subsequent
11970 20 62 65 68 61 76 69 6f 72 20 6f 66 20 53 51 4c   behavior of SQL
11980 69 74 65 20 77 69 6c 6c 20 62 65 20 75 6e 64 65  ite will be unde
11990 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  fined.</dd>.**.*
119a0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e  * <dt>SQLITE_CON
119b0 46 49 47 5f 4d 55 54 45 58 3c 2f 64 74 3e 0a 2a  FIG_MUTEX</dt>.*
119c0 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f  * <dd>This optio
119d0 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65  n takes a single
119e0 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20   argument which 
119f0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
11a00 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f  an.** instance o
11a10 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d  f the [sqlite3_m
11a20 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20 73 74  utex_methods] st
11a30 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 61 72  ructure.  The ar
11a40 67 75 6d 65 6e 74 20 73 70 65 63 69 66 69 65 73  gument specifies
11a50 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69 76 65 20  .** alternative 
11a60 6c 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20  low-level mutex 
11a70 72 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 75  routines to be u
11a80 73 65 64 20 69 6e 20 70 6c 61 63 65 0a 2a 2a 20  sed in place.** 
11a90 74 68 65 20 6d 75 74 65 78 20 72 6f 75 74 69 6e  the mutex routin
11aa0 65 73 20 62 75 69 6c 74 20 69 6e 74 6f 20 53 51  es built into SQ
11ab0 4c 69 74 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  Lite.</dd>.**.**
11ac0 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46   <dt>SQLITE_CONF
11ad0 49 47 5f 47 45 54 4d 55 54 45 58 3c 2f 64 74 3e  IG_GETMUTEX</dt>
11ae0 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74  .** <dd>This opt
11af0 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67  ion takes a sing
11b00 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63  le argument whic
11b10 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  h is a pointer t
11b20 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65  o an.** instance
11b30 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   of the [sqlite3
11b40 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20  _mutex_methods] 
11b50 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 0a  structure.  The.
11b60 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  ** [sqlite3_mute
11b70 78 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 73 74  x_methods].** st
11b80 72 75 63 74 75 72 65 20 69 73 20 66 69 6c 6c 65  ructure is fille
11b90 64 20 77 69 74 68 20 74 68 65 20 63 75 72 72 65  d with the curre
11ba0 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 6d 75 74  ntly defined mut
11bb0 65 78 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20  ex routines..** 
11bc0 54 68 69 73 20 6f 70 74 69 6f 6e 20 63 61 6e 20  This option can 
11bd0 62 65 20 75 73 65 64 20 74 6f 20 6f 76 65 72 6c  be used to overl
11be0 6f 61 64 20 74 68 65 20 64 65 66 61 75 6c 74 20  oad the default 
11bf0 6d 75 74 65 78 20 61 6c 6c 6f 63 61 74 69 6f 6e  mutex allocation
11c00 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77 69 74  .** routines wit
11c10 68 20 61 20 77 72 61 70 70 65 72 20 75 73 65 64  h a wrapper used
11c20 20 74 6f 20 74 72 61 63 6b 20 6d 75 74 65 78 20   to track mutex 
11c30 75 73 61 67 65 20 66 6f 72 20 70 65 72 66 6f 72  usage for perfor
11c40 6d 61 6e 63 65 0a 2a 2a 20 70 72 6f 66 69 6c 69  mance.** profili
11c50 6e 67 20 6f 72 20 74 65 73 74 69 6e 67 2c 20 66  ng or testing, f
11c60 6f 72 20 65 78 61 6d 70 6c 65 2e 3c 2f 64 64 3e  or example.</dd>
11c70 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  .**.** <dt>SQLIT
11c80 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49  E_CONFIG_LOOKASI
11c90 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  DE</dt>.** <dd>T
11ca0 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73  his option takes
11cb0 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 20 74   two arguments t
11cc0 68 61 74 20 64 65 74 65 72 6d 69 6e 65 20 74 68  hat determine th
11cd0 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 6d 65 6d  e default.** mem
11ce0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c  ory allocation l
11cf0 6f 6f 6b 61 73 69 64 65 20 6f 70 74 69 6d 69 7a  ookaside optimiz
11d00 61 74 69 6f 6e 2e 20 20 54 68 65 20 66 69 72 73  ation.  The firs
11d10 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  t argument is th
11d20 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65 61 63  e.** size of eac
11d30 68 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66  h lookaside buff
11d40 65 72 20 73 6c 6f 74 20 61 6e 64 20 74 68 65 20  er slot and the 
11d50 73 65 63 6f 6e 64 20 69 73 20 74 68 65 20 6e 75  second is the nu
11d60 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f 74 73  mber of.** slots
11d70 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 65 61   allocated to ea
11d80 63 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ch database conn
11d90 65 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70  ection.  This op
11da0 74 69 6f 6e 20 73 65 74 73 20 74 68 65 0a 2a 2a  tion sets the.**
11db0 20 3c 69 3e 64 65 66 61 75 6c 74 3c 2f 69 3e 20   <i>default</i> 
11dc0 6c 6f 6f 6b 61 73 69 64 65 20 73 69 7a 65 2e 20  lookaside size. 
11dd0 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 44 42 43   The [SQLITE_DBC
11de0 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 5d  ONFIG_LOOKASIDE]
11df0 0a 2a 2a 20 76 65 72 62 20 74 6f 20 5b 73 71 6c  .** verb to [sql
11e00 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29  ite3_db_config()
11e10 5d 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  ] can be used to
11e20 20 63 68 61 6e 67 65 20 74 68 65 20 6c 6f 6f 6b   change the look
11e30 61 73 69 64 65 0a 2a 2a 20 63 6f 6e 66 69 67 75  aside.** configu
11e40 72 61 74 69 6f 6e 20 6f 6e 20 69 6e 64 69 76 69  ration on indivi
11e50 64 75 61 6c 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  dual connections
11e60 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
11e70 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50  >SQLITE_CONFIG_P
11e80 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  CACHE</dt>.** <d
11e90 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61  d>This option ta
11ea0 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67  kes a single arg
11eb0 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 61  ument which is a
11ec0 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61   pointer to.** a
11ed0 6e 20 5b 73 71 6c 69 74 65 33 5f 70 63 61 63 68  n [sqlite3_pcach
11ee0 65 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63  e_methods] objec
11ef0 74 2e 20 20 54 68 69 73 20 6f 62 6a 65 63 74 20  t.  This object 
11f00 73 70 65 63 69 66 69 65 73 20 74 68 65 20 69 6e  specifies the in
11f10 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 61 20  terface.** to a 
11f20 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61 63 68  custom page cach
11f30 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
11f40 2e 20 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20  .  SQLite makes 
11f50 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a  a copy of the.**
11f60 20 6f 62 6a 65 63 74 20 61 6e 64 20 75 73 65 73   object and uses
11f70 20 69 74 20 66 6f 72 20 70 61 67 65 20 63 61 63   it for page cac
11f80 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
11f90 74 69 6f 6e 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  tions.</dd>.**.*
11fa0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e  * <dt>SQLITE_CON
11fb0 46 49 47 5f 47 45 54 50 43 41 43 48 45 3c 2f 64  FIG_GETPCACHE</d
11fc0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f  t>.** <dd>This o
11fd0 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69  ption takes a si
11fe0 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68  ngle argument wh
11ff0 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ich is a pointer
12000 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74   to an.** [sqlit
12010 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64  e3_pcache_method
12020 73 5d 20 6f 62 6a 65 63 74 2e 20 20 53 51 4c 69  s] object.  SQLi
12030 74 65 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65  te copies of the
12040 20 63 75 72 72 65 6e 74 0a 2a 2a 20 70 61 67 65   current.** page
12050 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74   cache implement
12060 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 61 74 20  ation into that 
12070 6f 62 6a 65 63 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a  object.</dd>.**.
12080 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66  ** </dl>.*/.#def
12090 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ine SQLITE_CONFI
120a0 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 20 20  G_SINGLETHREAD  
120b0 31 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 64 65  1  /* nil */.#de
120c0 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  fine SQLITE_CONF
120d0 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 20 20  IG_MULTITHREAD  
120e0 20 32 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 64   2  /* nil */.#d
120f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e  efine SQLITE_CON
12100 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44 20 20  FIG_SERIALIZED  
12110 20 20 33 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23    3  /* nil */.#
12120 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
12130 4e 46 49 47 5f 4d 41 4c 4c 4f 43 20 20 20 20 20  NFIG_MALLOC     
12140 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 74 65 33     4  /* sqlite3
12150 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f  _mem_methods* */
12160 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
12170 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c 4f 43  CONFIG_GETMALLOC
12180 20 20 20 20 20 35 20 20 2f 2a 20 73 71 6c 69 74       5  /* sqlit
12190 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 20  e3_mem_methods* 
121a0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
121b0 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48  E_CONFIG_SCRATCH
121c0 20 20 20 20 20 20 20 36 20 20 2f 2a 20 76 6f 69         6  /* voi
121d0 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20  d*, int sz, int 
121e0 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  N */.#define SQL
121f0 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43  ITE_CONFIG_PAGEC
12200 41 43 48 45 20 20 20 20 20 37 20 20 2f 2a 20 76  ACHE     7  /* v
12210 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e  oid*, int sz, in
12220 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  t N */.#define S
12230 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48 45 41  QLITE_CONFIG_HEA
12240 50 20 20 20 20 20 20 20 20 20 20 38 20 20 2f 2a  P          8  /*
12250 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e 42 79 74   void*, int nByt
12260 65 2c 20 69 6e 74 20 6d 69 6e 20 2a 2f 0a 23 64  e, int min */.#d
12270 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e  efine SQLITE_CON
12280 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 20 20 20  FIG_MEMSTATUS   
12290 20 20 39 20 20 2f 2a 20 62 6f 6f 6c 65 61 6e 20    9  /* boolean 
122a0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
122b0 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 20 20  E_CONFIG_MUTEX  
122c0 20 20 20 20 20 20 31 30 20 20 2f 2a 20 73 71 6c        10  /* sql
122d0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
122e0 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ds* */.#define S
122f0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54  QLITE_CONFIG_GET
12300 4d 55 54 45 58 20 20 20 20 20 31 31 20 20 2f 2a  MUTEX     11  /*
12310 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
12320 65 74 68 6f 64 73 2a 20 2a 2f 0a 2f 2a 20 70 72  ethods* */./* pr
12330 65 76 69 6f 75 73 6c 79 20 53 51 4c 49 54 45 5f  eviously SQLITE_
12340 43 4f 4e 46 49 47 5f 43 48 55 4e 4b 41 4c 4c 4f  CONFIG_CHUNKALLO
12350 43 20 31 32 20 77 68 69 63 68 20 69 73 20 6e 6f  C 12 which is no
12360 77 20 75 6e 75 73 65 64 2e 20 2a 2f 20 0a 23 64  w unused. */ .#d
12370 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e  efine SQLITE_CON
12380 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 20 20 20  FIG_LOOKASIDE   
12390 20 31 33 20 20 2f 2a 20 69 6e 74 20 69 6e 74 20   13  /* int int 
123a0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
123b0 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 20  E_CONFIG_PCACHE 
123c0 20 20 20 20 20 20 31 34 20 20 2f 2a 20 73 71 6c        14  /* sql
123d0 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68  ite3_pcache_meth
123e0 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ods* */.#define 
123f0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45  SQLITE_CONFIG_GE
12400 54 50 43 41 43 48 45 20 20 20 20 31 35 20 20 2f  TPCACHE    15  /
12410 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  * sqlite3_pcache
12420 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 0a 2f 2a  _methods* */../*
12430 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f  .** CAPI3REF: Co
12440 6e 66 69 67 75 72 61 74 69 6f 6e 20 4f 70 74 69  nfiguration Opti
12450 6f 6e 73 20 7b 48 31 30 31 37 30 7d 20 3c 53 32  ons {H10170} <S2
12460 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d  0000>.** EXPERIM
12470 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73  ENTAL.**.** Thes
12480 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20  e constants are 
12490 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e  the available in
124a0 74 65 67 65 72 20 63 6f 6e 66 69 67 75 72 61 74  teger configurat
124b0 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 74 68 61 74  ion options that
124c0 0a 2a 2a 20 63 61 6e 20 62 65 20 70 61 73 73 65  .** can be passe
124d0 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
124e0 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
124f0 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66  [sqlite3_db_conf
12500 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e  ig()] interface.
12510 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f 6e 66 69  .**.** New confi
12520 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73  guration options
12530 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e   may be added in
12540 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73   future releases
12550 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 45   of SQLite..** E
12560 78 69 73 74 69 6e 67 20 63 6f 6e 66 69 67 75 72  xisting configur
12570 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 6d 69  ation options mi
12580 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74 69 6e  ght be discontin
12590 75 65 64 2e 20 20 41 70 70 6c 69 63 61 74 69 6f  ued.  Applicatio
125a0 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 68 65  ns.** should che
125b0 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f  ck the return co
125c0 64 65 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33  de from [sqlite3
125d0 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 74 6f  _db_config()] to
125e0 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a   make sure that.
125f0 2a 2a 20 74 68 65 20 63 61 6c 6c 20 77 6f 72 6b  ** the call work
12600 65 64 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65  ed.  The [sqlite
12610 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 69  3_db_config()] i
12620 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20 72 65  nterface will re
12630 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65  turn a.** non-ze
12640 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20  ro [error code] 
12650 69 66 20 61 20 64 69 73 63 6f 6e 74 69 6e 75 65  if a discontinue
12660 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64  d or unsupported
12670 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f   configuration o
12680 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f  ption.** is invo
12690 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a  ked..**.** <dl>.
126a0 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 44 42  ** <dt>SQLITE_DB
126b0 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45  CONFIG_LOOKASIDE
126c0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
126d0 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 74  s option takes t
126e0 68 72 65 65 20 61 64 64 69 74 69 6f 6e 61 6c 20  hree additional 
126f0 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 20 64  arguments that d
12700 65 74 65 72 6d 69 6e 65 20 74 68 65 20 0a 2a 2a  etermine the .**
12710 20 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f   [lookaside memo
12720 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 63 6f  ry allocator] co
12730 6e 66 69 67 75 72 61 74 69 6f 6e 20 66 6f 72 20  nfiguration for 
12740 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  the [database co
12750 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 20 54 68  nnection]..** Th
12760 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
12770 20 28 74 68 65 20 74 68 69 72 64 20 70 61 72 61   (the third para
12780 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65  meter to [sqlite
12790 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 69  3_db_config()] i
127a0 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74  s a.** pointer t
127b0 6f 20 61 6e 20 6d 65 6d 6f 72 79 20 62 75 66 66  o an memory buff
127c0 65 72 20 74 6f 20 75 73 65 20 66 6f 72 20 6c 6f  er to use for lo
127d0 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 2e 0a  okaside memory..
127e0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67  ** The first arg
127f0 75 6d 65 6e 74 20 6d 61 79 20 62 65 20 4e 55 4c  ument may be NUL
12800 4c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20  L in which case 
12810 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 6c 6f  SQLite will allo
12820 63 61 74 65 20 74 68 65 0a 2a 2a 20 6c 6f 6f 6b  cate the.** look
12830 61 73 69 64 65 20 62 75 66 66 65 72 20 69 74 73  aside buffer its
12840 65 6c 66 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  elf using [sqlit
12850 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 54  e3_malloc()].  T
12860 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
12870 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 73 69 7a  nt is the.** siz
12880 65 20 6f 66 20 65 61 63 68 20 6c 6f 6f 6b 61 73  e of each lookas
12890 69 64 65 20 62 75 66 66 65 72 20 73 6c 6f 74 20  ide buffer slot 
128a0 61 6e 64 20 74 68 65 20 74 68 69 72 64 20 61 72  and the third ar
128b0 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 75  gument is the nu
128c0 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f 74 73  mber of.** slots
128d0 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74  .  The size of t
128e0 68 65 20 62 75 66 66 65 72 20 69 6e 20 74 68 65  he buffer in the
128f0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
12900 6d 75 73 74 20 62 65 20 67 72 65 61 74 65 72 20  must be greater 
12910 74 68 61 6e 0a 2a 2a 20 6f 72 20 65 71 75 61 6c  than.** or equal
12920 20 74 6f 20 74 68 65 20 70 72 6f 64 75 63 74 20   to the product 
12930 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 6e  of the second an
12940 64 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  d third argument
12950 73 2e 20 20 54 68 65 20 62 75 66 66 65 72 0a 2a  s.  The buffer.*
12960 2a 20 6d 75 73 74 20 62 65 20 61 6c 69 67 6e 65  * must be aligne
12970 64 20 74 6f 20 61 6e 20 38 2d 62 79 74 65 20 62  d to an 8-byte b
12980 6f 75 6e 64 61 72 79 2e 20 20 49 66 20 74 68 65  oundary.  If the
12990 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
129a0 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 6d 75 6c   is not.** a mul
129b0 74 69 70 6c 65 20 6f 66 20 38 2c 20 69 74 20 69  tiple of 8, it i
129c0 73 20 69 6e 74 65 72 6e 61 6c 6c 79 20 72 6f 75  s internally rou
129d0 6e 64 65 64 20 64 6f 77 6e 20 74 6f 20 74 68 65  nded down to the
129e0 20 6e 65 78 74 20 73 6d 61 6c 6c 65 72 0a 2a 2a   next smaller.**
129f0 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 2e 20   multiple of 8. 
12a00 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51 4c 49   See also: [SQLI
12a10 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53  TE_CONFIG_LOOKAS
12a20 49 44 45 5d 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  IDE]</dd>.**.** 
12a30 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65  </dl>.*/.#define
12a40 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47   SQLITE_DBCONFIG
12a50 5f 4c 4f 4f 4b 41 53 49 44 45 20 20 20 20 31 30  _LOOKASIDE    10
12a60 30 31 20 20 2f 2a 20 76 6f 69 64 2a 20 69 6e 74  01  /* void* int
12a70 20 69 6e 74 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20   int */.../*.** 
12a80 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62 6c 65  CAPI3REF: Enable
12a90 20 4f 72 20 44 69 73 61 62 6c 65 20 45 78 74 65   Or Disable Exte
12aa0 6e 64 65 64 20 52 65 73 75 6c 74 20 43 6f 64 65  nded Result Code
12ab0 73 20 7b 48 31 32 32 30 30 7d 20 3c 53 31 30 37  s {H12200} <S107
12ac0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  00>.**.** The sq
12ad0 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72  lite3_extended_r
12ae0 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 20 72 6f  esult_codes() ro
12af0 75 74 69 6e 65 20 65 6e 61 62 6c 65 73 20 6f 72  utine enables or
12b00 20 64 69 73 61 62 6c 65 73 20 74 68 65 0a 2a 2a   disables the.**
12b10 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c   [extended resul
12b20 74 20 63 6f 64 65 73 5d 20 66 65 61 74 75 72 65  t codes] feature
12b30 20 6f 66 20 53 51 4c 69 74 65 2e 20 54 68 65 20   of SQLite. The 
12b40 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 0a  extended result.
12b50 2a 2a 20 63 6f 64 65 73 20 61 72 65 20 64 69 73  ** codes are dis
12b60 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74  abled by default
12b70 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 20   for historical 
12b80 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 63 6f  compatibility co
12b90 6e 73 69 64 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a  nsiderations..**
12ba0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
12bb0 3a 0a 2a 2a 20 5b 48 31 32 32 30 31 5d 20 5b 48  :.** [H12201] [H
12bc0 31 32 32 30 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45  12202].*/.SQLITE
12bd0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
12be0 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74  _extended_result
12bf0 5f 63 6f 64 65 73 28 73 71 6c 69 74 65 33 2a 2c  _codes(sqlite3*,
12c00 20 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a   int onoff);../*
12c10 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4c 61  .** CAPI3REF: La
12c20 73 74 20 49 6e 73 65 72 74 20 52 6f 77 69 64 20  st Insert Rowid 
12c30 7b 48 31 32 32 32 30 7d 20 3c 53 31 30 37 30 30  {H12220} <S10700
12c40 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 65 6e 74  >.**.** Each ent
12c50 72 79 20 69 6e 20 61 6e 20 53 51 4c 69 74 65 20  ry in an SQLite 
12c60 74 61 62 6c 65 20 68 61 73 20 61 20 75 6e 69 71  table has a uniq
12c70 75 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64  ue 64-bit signed
12c80 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 79 20  .** integer key 
12c90 63 61 6c 6c 65 64 20 74 68 65 20 5b 52 4f 57 49  called the [ROWI
12ca0 44 20 7c 20 22 72 6f 77 69 64 22 5d 2e 20 54 68  D | "rowid"]. Th
12cb0 65 20 72 6f 77 69 64 20 69 73 20 61 6c 77 61 79  e rowid is alway
12cc0 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 61  s available.** a
12cd0 73 20 61 6e 20 75 6e 64 65 63 6c 61 72 65 64 20  s an undeclared 
12ce0 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 52 4f 57  column named ROW
12cf0 49 44 2c 20 4f 49 44 2c 20 6f 72 20 5f 52 4f 57  ID, OID, or _ROW
12d00 49 44 5f 20 61 73 20 6c 6f 6e 67 20 61 73 20 74  ID_ as long as t
12d10 68 6f 73 65 0a 2a 2a 20 6e 61 6d 65 73 20 61 72  hose.** names ar
12d20 65 20 6e 6f 74 20 61 6c 73 6f 20 75 73 65 64 20  e not also used 
12d30 62 79 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65  by explicitly de
12d40 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 73 2e 20  clared columns. 
12d50 49 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20  If.** the table 
12d60 68 61 73 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20  has a column of 
12d70 74 79 70 65 20 5b 49 4e 54 45 47 45 52 20 50 52  type [INTEGER PR
12d80 49 4d 41 52 59 20 4b 45 59 5d 20 74 68 65 6e 20  IMARY KEY] then 
12d90 74 68 61 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69  that column.** i
12da0 73 20 61 6e 6f 74 68 65 72 20 61 6c 69 61 73 20  s another alias 
12db0 66 6f 72 20 74 68 65 20 72 6f 77 69 64 2e 0a 2a  for the rowid..*
12dc0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
12dd0 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 5b 72  e returns the [r
12de0 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 6d 6f 73  owid] of the mos
12df0 74 20 72 65 63 65 6e 74 0a 2a 2a 20 73 75 63 63  t recent.** succ
12e00 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54 5d 20  essful [INSERT] 
12e10 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  into the databas
12e20 65 20 66 72 6f 6d 20 74 68 65 20 5b 64 61 74 61  e from the [data
12e30 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
12e40 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 72 73 74  .** in the first
12e50 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 6e   argument.  If n
12e60 6f 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49 4e  o successful [IN
12e70 53 45 52 54 5d 73 0a 2a 2a 20 68 61 76 65 20 65  SERT]s.** have e
12e80 76 65 72 20 6f 63 63 75 72 72 65 64 20 6f 6e 20  ver occurred on 
12e90 74 68 61 74 20 64 61 74 61 62 61 73 65 20 63 6f  that database co
12ea0 6e 6e 65 63 74 69 6f 6e 2c 20 7a 65 72 6f 20 69  nnection, zero i
12eb0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
12ec0 2a 20 49 66 20 61 6e 20 5b 49 4e 53 45 52 54 5d  * If an [INSERT]
12ed0 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 61   occurs within a
12ee0 20 74 72 69 67 67 65 72 2c 20 74 68 65 6e 20 74   trigger, then t
12ef0 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20 74 68  he [rowid] of th
12f00 65 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 72 6f  e inserted.** ro
12f10 77 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 79  w is returned by
12f20 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 73   this routine as
12f30 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 74 72 69   long as the tri
12f40 67 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 2e  gger is running.
12f50 0a 2a 2a 20 42 75 74 20 6f 6e 63 65 20 74 68 65  .** But once the
12f60 20 74 72 69 67 67 65 72 20 74 65 72 6d 69 6e 61   trigger termina
12f70 74 65 73 2c 20 74 68 65 20 76 61 6c 75 65 20 72  tes, the value r
12f80 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 20  eturned by this 
12f90 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 76 65 72  routine.** rever
12fa0 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74 20 76  ts to the last v
12fb0 61 6c 75 65 20 69 6e 73 65 72 74 65 64 20 62 65  alue inserted be
12fc0 66 6f 72 65 20 74 68 65 20 74 72 69 67 67 65 72  fore the trigger
12fd0 20 66 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e   fired..**.** An
12fe0 20 5b 49 4e 53 45 52 54 5d 20 74 68 61 74 20 66   [INSERT] that f
12ff0 61 69 6c 73 20 64 75 65 20 74 6f 20 61 20 63 6f  ails due to a co
13000 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69  nstraint violati
13010 6f 6e 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 73  on is not a.** s
13020 75 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52  uccessful [INSER
13030 54 5d 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20  T] and does not 
13040 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65  change the value
13050 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69   returned by thi
13060 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20 20 54  s.** routine.  T
13070 68 75 73 20 49 4e 53 45 52 54 20 4f 52 20 46 41  hus INSERT OR FA
13080 49 4c 2c 20 49 4e 53 45 52 54 20 4f 52 20 49 47  IL, INSERT OR IG
13090 4e 4f 52 45 2c 20 49 4e 53 45 52 54 20 4f 52 20  NORE, INSERT OR 
130a0 52 4f 4c 4c 42 41 43 4b 2c 0a 2a 2a 20 61 6e 64  ROLLBACK,.** and
130b0 20 49 4e 53 45 52 54 20 4f 52 20 41 42 4f 52 54   INSERT OR ABORT
130c0 20 6d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73   make no changes
130d0 20 74 6f 20 74 68 65 20 72 65 74 75 72 6e 20 76   to the return v
130e0 61 6c 75 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20  alue of this.** 
130f0 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 74 68 65  routine when the
13100 69 72 20 69 6e 73 65 72 74 69 6f 6e 20 66 61 69  ir insertion fai
13110 6c 73 2e 20 20 57 68 65 6e 20 49 4e 53 45 52 54  ls.  When INSERT
13120 20 4f 52 20 52 45 50 4c 41 43 45 0a 2a 2a 20 65   OR REPLACE.** e
13130 6e 63 6f 75 6e 74 65 72 73 20 61 20 63 6f 6e 73  ncounters a cons
13140 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e  traint violation
13150 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 66 61  , it does not fa
13160 69 6c 2e 20 20 54 68 65 0a 2a 2a 20 49 4e 53 45  il.  The.** INSE
13170 52 54 20 63 6f 6e 74 69 6e 75 65 73 20 74 6f 20  RT continues to 
13180 63 6f 6d 70 6c 65 74 69 6f 6e 20 61 66 74 65 72  completion after
13190 20 64 65 6c 65 74 69 6e 67 20 72 6f 77 73 20 74   deleting rows t
131a0 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 74 68  hat caused.** th
131b0 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 72 6f  e constraint pro
131c0 62 6c 65 6d 20 73 6f 20 49 4e 53 45 52 54 20 4f  blem so INSERT O
131d0 52 20 52 45 50 4c 41 43 45 20 77 69 6c 6c 20 61  R REPLACE will a
131e0 6c 77 61 79 73 20 63 68 61 6e 67 65 0a 2a 2a 20  lways change.** 
131f0 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
13200 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66 61   of this interfa
13210 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68  ce..**.** For th
13220 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68  e purposes of th
13230 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 6e 20 5b  is routine, an [
13240 49 4e 53 45 52 54 5d 20 69 73 20 63 6f 6e 73 69  INSERT] is consi
13250 64 65 72 65 64 20 74 6f 0a 2a 2a 20 62 65 20 73  dered to.** be s
13260 75 63 63 65 73 73 66 75 6c 20 65 76 65 6e 20 69  uccessful even i
13270 66 20 69 74 20 69 73 20 73 75 62 73 65 71 75 65  f it is subseque
13280 6e 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b  ntly rolled back
13290 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
132a0 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 32 31  ents:.** [H12221
132b0 5d 20 5b 48 31 32 32 32 33 5d 0a 2a 2a 0a 2a 2a  ] [H12223].**.**
132c0 20 49 66 20 61 20 73 65 70 61 72 61 74 65 20 74   If a separate t
132d0 68 72 65 61 64 20 70 65 72 66 6f 72 6d 73 20 61  hread performs a
132e0 20 6e 65 77 20 5b 49 4e 53 45 52 54 5d 20 6f 6e   new [INSERT] on
132f0 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 64 61 74   the same.** dat
13300 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
13310 20 77 68 69 6c 65 20 74 68 65 20 5b 73 71 6c 69   while the [sqli
13320 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f  te3_last_insert_
13330 72 6f 77 69 64 28 29 5d 0a 2a 2a 20 66 75 6e 63  rowid()].** func
13340 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 20  tion is running 
13350 61 6e 64 20 74 68 75 73 20 63 68 61 6e 67 65 73  and thus changes
13360 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72 74   the last insert
13370 20 5b 72 6f 77 69 64 5d 2c 0a 2a 2a 20 74 68 65   [rowid],.** the
13380 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75  n the value retu
13390 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33  rned by [sqlite3
133a0 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77  _last_insert_row
133b0 69 64 28 29 5d 20 69 73 0a 2a 2a 20 75 6e 70 72  id()] is.** unpr
133c0 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 6d 69  edictable and mi
133d0 67 68 74 20 6e 6f 74 20 65 71 75 61 6c 20 65 69  ght not equal ei
133e0 74 68 65 72 20 74 68 65 20 6f 6c 64 20 6f 72 20  ther the old or 
133f0 74 68 65 20 6e 65 77 0a 2a 2a 20 6c 61 73 74 20  the new.** last 
13400 69 6e 73 65 72 74 20 5b 72 6f 77 69 64 5d 2e 0a  insert [rowid]..
13410 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  */.SQLITE_API sq
13420 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69  lite3_int64 sqli
13430 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f  te3_last_insert_
13440 72 6f 77 69 64 28 73 71 6c 69 74 65 33 2a 29 3b  rowid(sqlite3*);
13450 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
13460 3a 20 43 6f 75 6e 74 20 54 68 65 20 4e 75 6d 62  : Count The Numb
13470 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f 64 69 66  er Of Rows Modif
13480 69 65 64 20 7b 48 31 32 32 34 30 7d 20 3c 53 31  ied {H12240} <S1
13490 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  0600>.**.** This
134a0 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
134b0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
134c0 64 61 74 61 62 61 73 65 20 72 6f 77 73 20 74 68  database rows th
134d0 61 74 20 77 65 72 65 20 63 68 61 6e 67 65 64 0a  at were changed.
134e0 2a 2a 20 6f 72 20 69 6e 73 65 72 74 65 64 20 6f  ** or inserted o
134f0 72 20 64 65 6c 65 74 65 64 20 62 79 20 74 68 65  r deleted by the
13500 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63   most recently c
13510 6f 6d 70 6c 65 74 65 64 20 53 51 4c 20 73 74 61  ompleted SQL sta
13520 74 65 6d 65 6e 74 0a 2a 2a 20 6f 6e 20 74 68 65  tement.** on the
13530 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
13540 63 74 69 6f 6e 5d 20 73 70 65 63 69 66 69 65 64  ction] specified
13550 20 62 79 20 74 68 65 20 66 69 72 73 74 20 70 61   by the first pa
13560 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 4f 6e 6c 79  rameter..** Only
13570 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 61 72   changes that ar
13580 65 20 64 69 72 65 63 74 6c 79 20 73 70 65 63 69  e directly speci
13590 66 69 65 64 20 62 79 20 74 68 65 20 5b 49 4e 53  fied by the [INS
135a0 45 52 54 5d 2c 20 5b 55 50 44 41 54 45 5d 2c 0a  ERT], [UPDATE],.
135b0 2a 2a 20 6f 72 20 5b 44 45 4c 45 54 45 5d 20 73  ** or [DELETE] s
135c0 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 6f 75  tatement are cou
135d0 6e 74 65 64 2e 20 20 41 75 78 69 6c 69 61 72 79  nted.  Auxiliary
135e0 20 63 68 61 6e 67 65 73 20 63 61 75 73 65 64 20   changes caused 
135f0 62 79 0a 2a 2a 20 74 72 69 67 67 65 72 73 20 61  by.** triggers a
13600 72 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64 2e 20  re not counted. 
13610 55 73 65 20 74 68 65 20 5b 73 71 6c 69 74 65 33  Use the [sqlite3
13620 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29  _total_changes()
13630 5d 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 6f  ] function.** to
13640 20 66 69 6e 64 20 74 68 65 20 74 6f 74 61 6c 20   find the total 
13650 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 65  number of change
13660 73 20 69 6e 63 6c 75 64 69 6e 67 20 63 68 61 6e  s including chan
13670 67 65 73 20 63 61 75 73 65 64 20 62 79 20 74 72  ges caused by tr
13680 69 67 67 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 43 68  iggers..**.** Ch
13690 61 6e 67 65 73 20 74 6f 20 61 20 76 69 65 77 20  anges to a view 
136a0 74 68 61 74 20 61 72 65 20 73 69 6d 75 6c 61 74  that are simulat
136b0 65 64 20 62 79 20 61 6e 20 5b 49 4e 53 54 45 41  ed by an [INSTEA
136c0 44 20 4f 46 20 74 72 69 67 67 65 72 5d 0a 2a 2a  D OF trigger].**
136d0 20 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64   are not counted
136e0 2e 20 20 4f 6e 6c 79 20 72 65 61 6c 20 74 61 62  .  Only real tab
136f0 6c 65 20 63 68 61 6e 67 65 73 20 61 72 65 20 63  le changes are c
13700 6f 75 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20  ounted..**.** A 
13710 22 72 6f 77 20 63 68 61 6e 67 65 22 20 69 73 20  "row change" is 
13720 61 20 63 68 61 6e 67 65 20 74 6f 20 61 20 73 69  a change to a si
13730 6e 67 6c 65 20 72 6f 77 20 6f 66 20 61 20 73 69  ngle row of a si
13740 6e 67 6c 65 20 74 61 62 6c 65 0a 2a 2a 20 63 61  ngle table.** ca
13750 75 73 65 64 20 62 79 20 61 6e 20 49 4e 53 45 52  used by an INSER
13760 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50  T, DELETE, or UP
13770 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20  DATE statement. 
13780 20 52 6f 77 73 20 74 68 61 74 0a 2a 2a 20 61 72   Rows that.** ar
13790 65 20 63 68 61 6e 67 65 64 20 61 73 20 73 69 64  e changed as sid
137a0 65 20 65 66 66 65 63 74 73 20 6f 66 20 5b 52 45  e effects of [RE
137b0 50 4c 41 43 45 5d 20 63 6f 6e 73 74 72 61 69 6e  PLACE] constrain
137c0 74 20 72 65 73 6f 6c 75 74 69 6f 6e 2c 0a 2a 2a  t resolution,.**
137d0 20 72 6f 6c 6c 62 61 63 6b 2c 20 41 42 4f 52 54   rollback, ABORT
137e0 20 70 72 6f 63 65 73 73 69 6e 67 2c 20 5b 44 52   processing, [DR
137f0 4f 50 20 54 41 42 4c 45 5d 2c 20 6f 72 20 62 79  OP TABLE], or by
13800 20 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 6d 65   any other.** me
13810 63 68 61 6e 69 73 6d 73 20 64 6f 20 6e 6f 74 20  chanisms do not 
13820 63 6f 75 6e 74 20 61 73 20 64 69 72 65 63 74 20  count as direct 
13830 72 6f 77 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 0a  row changes..**.
13840 2a 2a 20 41 20 22 74 72 69 67 67 65 72 20 63 6f  ** A "trigger co
13850 6e 74 65 78 74 22 20 69 73 20 61 20 73 63 6f 70  ntext" is a scop
13860 65 20 6f 66 20 65 78 65 63 75 74 69 6f 6e 20 74  e of execution t
13870 68 61 74 20 62 65 67 69 6e 73 20 61 6e 64 0a 2a  hat begins and.*
13880 2a 20 65 6e 64 73 20 77 69 74 68 20 74 68 65 20  * ends with the 
13890 73 63 72 69 70 74 20 6f 66 20 61 20 5b 43 52 45  script of a [CRE
138a0 41 54 45 20 54 52 49 47 47 45 52 20 7c 20 74 72  ATE TRIGGER | tr
138b0 69 67 67 65 72 5d 2e 20 0a 2a 2a 20 4d 6f 73 74  igger]. .** Most
138c0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20   SQL statements 
138d0 61 72 65 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64  are.** evaluated
138e0 20 6f 75 74 73 69 64 65 20 6f 66 20 61 6e 79 20   outside of any 
138f0 74 72 69 67 67 65 72 2e 20 20 54 68 69 73 20 69  trigger.  This i
13900 73 20 74 68 65 20 22 74 6f 70 20 6c 65 76 65 6c  s the "top level
13910 22 0a 2a 2a 20 74 72 69 67 67 65 72 20 63 6f 6e  ".** trigger con
13920 74 65 78 74 2e 20 20 49 66 20 61 20 74 72 69 67  text.  If a trig
13930 67 65 72 20 66 69 72 65 73 20 66 72 6f 6d 20 74  ger fires from t
13940 68 65 20 74 6f 70 20 6c 65 76 65 6c 2c 20 61 0a  he top level, a.
13950 2a 2a 20 6e 65 77 20 74 72 69 67 67 65 72 20 63  ** new trigger c
13960 6f 6e 74 65 78 74 20 69 73 20 65 6e 74 65 72 65  ontext is entere
13970 64 20 66 6f 72 20 74 68 65 20 64 75 72 61 74 69  d for the durati
13980 6f 6e 20 6f 66 20 74 68 61 74 20 6f 6e 65 0a 2a  on of that one.*
13990 2a 20 74 72 69 67 67 65 72 2e 20 20 53 75 62 74  * trigger.  Subt
139a0 72 69 67 67 65 72 73 20 63 72 65 61 74 65 20 73  riggers create s
139b0 75 62 63 6f 6e 74 65 78 74 73 20 66 6f 72 20 74  ubcontexts for t
139c0 68 65 69 72 20 64 75 72 61 74 69 6f 6e 2e 0a 2a  heir duration..*
139d0 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 5b 73 71  *.** Calling [sq
139e0 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 6f 72  lite3_exec()] or
139f0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
13a00 5d 20 72 65 63 75 72 73 69 76 65 6c 79 20 64 6f  ] recursively do
13a10 65 73 0a 2a 2a 20 6e 6f 74 20 63 72 65 61 74 65  es.** not create
13a20 20 61 20 6e 65 77 20 74 72 69 67 67 65 72 20 63   a new trigger c
13a30 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  ontext..**.** Th
13a40 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
13a50 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
13a60 66 20 64 69 72 65 63 74 20 72 6f 77 20 63 68 61  f direct row cha
13a70 6e 67 65 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6d  nges in the.** m
13a80 6f 73 74 20 72 65 63 65 6e 74 20 49 4e 53 45 52  ost recent INSER
13a90 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45  T, UPDATE, or DE
13aa0 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20 77  LETE statement w
13ab0 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 0a 2a  ithin the same.*
13ac0 2a 20 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78  * trigger contex
13ad0 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 75 73 2c 20 77  t..**.** Thus, w
13ae0 68 65 6e 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20  hen called from 
13af0 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2c 20 74  the top level, t
13b00 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
13b10 75 72 6e 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62  urns the.** numb
13b20 65 72 20 6f 66 20 63 68 61 6e 67 65 73 20 69 6e  er of changes in
13b30 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
13b40 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c   INSERT, UPDATE,
13b50 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 74 68   or DELETE.** th
13b60 61 74 20 61 6c 73 6f 20 6f 63 63 75 72 72 65 64  at also occurred
13b70 20 61 74 20 74 68 65 20 74 6f 70 20 6c 65 76 65   at the top leve
13b80 6c 2e 20 20 57 69 74 68 69 6e 20 74 68 65 20 62  l.  Within the b
13b90 6f 64 79 20 6f 66 20 61 20 74 72 69 67 67 65 72  ody of a trigger
13ba0 2c 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33  ,.** the sqlite3
13bb0 5f 63 68 61 6e 67 65 73 28 29 20 69 6e 74 65 72  _changes() inter
13bc0 66 61 63 65 20 63 61 6e 20 62 65 20 63 61 6c 6c  face can be call
13bd0 65 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e  ed to find the n
13be0 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 6e  umber of.** chan
13bf0 67 65 73 20 69 6e 20 74 68 65 20 6d 6f 73 74 20  ges in the most 
13c00 72 65 63 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74  recently complet
13c10 65 64 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54  ed INSERT, UPDAT
13c20 45 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20  E, or DELETE.** 
13c30 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68 69 6e  statement within
13c40 20 74 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65   the body of the
13c50 20 73 61 6d 65 20 74 72 69 67 67 65 72 2e 0a 2a   same trigger..*
13c60 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20 6e  * However, the n
13c70 75 6d 62 65 72 20 72 65 74 75 72 6e 65 64 20 64  umber returned d
13c80 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20  oes not include 
13c90 63 68 61 6e 67 65 73 0a 2a 2a 20 63 61 75 73 65  changes.** cause
13ca0 64 20 62 79 20 73 75 62 74 72 69 67 67 65 72 73  d by subtriggers
13cb0 20 73 69 6e 63 65 20 74 68 6f 73 65 20 68 61 76   since those hav
13cc0 65 20 74 68 65 69 72 20 6f 77 6e 20 63 6f 6e 74  e their own cont
13cd0 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  ext..**.** See a
13ce0 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33  lso the [sqlite3
13cf0 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29  _total_changes()
13d00 5d 20 69 6e 74 65 72 66 61 63 65 20 61 6e 64 20  ] interface and 
13d10 74 68 65 0a 2a 2a 20 5b 63 6f 75 6e 74 5f 63 68  the.** [count_ch
13d20 61 6e 67 65 73 20 70 72 61 67 6d 61 5d 2e 0a 2a  anges pragma]..*
13d30 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
13d40 73 3a 0a 2a 2a 20 5b 48 31 32 32 34 31 5d 20 5b  s:.** [H12241] [
13d50 48 31 32 32 34 33 5d 0a 2a 2a 0a 2a 2a 20 49 66  H12243].**.** If
13d60 20 61 20 73 65 70 61 72 61 74 65 20 74 68 72 65   a separate thre
13d70 61 64 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73  ad makes changes
13d80 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74   on the same dat
13d90 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
13da0 0a 2a 2a 20 77 68 69 6c 65 20 5b 73 71 6c 69 74  .** while [sqlit
13db0 65 33 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 73  e3_changes()] is
13dc0 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20 74 68   running then th
13dd0 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
13de0 0a 2a 2a 20 69 73 20 75 6e 70 72 65 64 69 63 74  .** is unpredict
13df0 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20 6d 65 61  able and not mea
13e00 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51 4c 49  ningful..*/.SQLI
13e10 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
13e20 65 33 5f 63 68 61 6e 67 65 73 28 73 71 6c 69 74  e3_changes(sqlit
13e30 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  e3*);../*.** CAP
13e40 49 33 52 45 46 3a 20 54 6f 74 61 6c 20 4e 75 6d  I3REF: Total Num
13e50 62 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f 64 69  ber Of Rows Modi
13e60 66 69 65 64 20 7b 48 31 32 32 36 30 7d 20 3c 53  fied {H12260} <S
13e70 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  10600>.**.** Thi
13e80 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
13e90 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
13ea0 20 72 6f 77 20 63 68 61 6e 67 65 73 20 63 61 75   row changes cau
13eb0 73 65 64 20 62 79 20 5b 49 4e 53 45 52 54 5d 2c  sed by [INSERT],
13ec0 0a 2a 2a 20 5b 55 50 44 41 54 45 5d 20 6f 72 20  .** [UPDATE] or 
13ed0 5b 44 45 4c 45 54 45 5d 20 73 74 61 74 65 6d 65  [DELETE] stateme
13ee0 6e 74 73 20 73 69 6e 63 65 20 74 68 65 20 5b 64  nts since the [d
13ef0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
13f00 6f 6e 5d 20 77 61 73 20 6f 70 65 6e 65 64 2e 0a  on] was opened..
13f10 2a 2a 20 54 68 65 20 63 6f 75 6e 74 20 69 6e 63  ** The count inc
13f20 6c 75 64 65 73 20 61 6c 6c 20 63 68 61 6e 67 65  ludes all change
13f30 73 20 66 72 6f 6d 20 61 6c 6c 20 0a 2a 2a 20 5b  s from all .** [
13f40 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 7c  CREATE TRIGGER |
13f50 20 74 72 69 67 67 65 72 5d 20 63 6f 6e 74 65 78   trigger] contex
13f60 74 73 2e 20 20 48 6f 77 65 76 65 72 2c 0a 2a 2a  ts.  However,.**
13f70 20 74 68 65 20 63 6f 75 6e 74 20 64 6f 65 73 20   the count does 
13f80 6e 6f 74 20 69 6e 63 6c 75 64 65 20 63 68 61 6e  not include chan
13f90 67 65 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c  ges used to impl
13fa0 65 6d 65 6e 74 20 5b 52 45 50 4c 41 43 45 5d 20  ement [REPLACE] 
13fb0 63 6f 6e 73 74 72 61 69 6e 74 73 2c 0a 2a 2a 20  constraints,.** 
13fc0 64 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 6f 72 20  do rollbacks or 
13fd0 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e 67  ABORT processing
13fe0 2c 20 6f 72 20 5b 44 52 4f 50 20 54 41 42 4c 45  , or [DROP TABLE
13ff0 5d 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 20 54  ] processing.  T
14000 68 65 0a 2a 2a 20 63 6f 75 6e 74 20 64 6f 65 73  he.** count does
14010 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 72 6f 77   not include row
14020 73 20 6f 66 20 76 69 65 77 73 20 74 68 61 74 20  s of views that 
14030 66 69 72 65 20 61 6e 20 5b 49 4e 53 54 45 41 44  fire an [INSTEAD
14040 20 4f 46 20 74 72 69 67 67 65 72 5d 2c 0a 2a 2a   OF trigger],.**
14050 20 74 68 6f 75 67 68 20 69 66 20 74 68 65 20 49   though if the I
14060 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 65  NSTEAD OF trigge
14070 72 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 20  r makes changes 
14080 6f 66 20 69 74 73 20 6f 77 6e 2c 20 74 68 6f 73  of its own, thos
14090 65 20 63 68 61 6e 67 65 73 20 0a 2a 2a 20 61 72  e changes .** ar
140a0 65 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a 20 54 68  e counted..** Th
140b0 65 20 63 68 61 6e 67 65 73 20 61 72 65 20 63 6f  e changes are co
140c0 75 6e 74 65 64 20 61 73 20 73 6f 6f 6e 20 61 73  unted as soon as
140d0 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74   the statement t
140e0 68 61 74 20 6d 61 6b 65 73 20 74 68 65 6d 20 69  hat makes them i
140f0 73 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 64 20 28  s.** completed (
14100 77 68 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65  when the stateme
14110 6e 74 20 68 61 6e 64 6c 65 20 69 73 20 70 61 73  nt handle is pas
14120 73 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  sed to [sqlite3_
14130 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a 2a 20 5b  reset()] or.** [
14140 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
14150 28 29 5d 29 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  ()])..**.** See 
14160 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 65  also the [sqlite
14170 33 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 6e 74  3_changes()] int
14180 65 72 66 61 63 65 20 61 6e 64 20 74 68 65 0a 2a  erface and the.*
14190 2a 20 5b 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73  * [count_changes
141a0 20 70 72 61 67 6d 61 5d 2e 0a 2a 2a 0a 2a 2a 20   pragma]..**.** 
141b0 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
141c0 20 5b 48 31 32 32 36 31 5d 20 5b 48 31 32 32 36   [H12261] [H1226
141d0 33 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 65  3].**.** If a se
141e0 70 61 72 61 74 65 20 74 68 72 65 61 64 20 6d 61  parate thread ma
141f0 6b 65 73 20 63 68 61 6e 67 65 73 20 6f 6e 20 74  kes changes on t
14200 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
14210 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 77   connection.** w
14220 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 74 6f  hile [sqlite3_to
14230 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20 69  tal_changes()] i
14240 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20 74  s running then t
14250 68 65 20 76 61 6c 75 65 0a 2a 2a 20 72 65 74 75  he value.** retu
14260 72 6e 65 64 20 69 73 20 75 6e 70 72 65 64 69 63  rned is unpredic
14270 74 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20 6d 65  table and not me
14280 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51 4c  aningful..*/.SQL
14290 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
142a0 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65  te3_total_change
142b0 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a  s(sqlite3*);../*
142c0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6e  .** CAPI3REF: In
142d0 74 65 72 72 75 70 74 20 41 20 4c 6f 6e 67 2d 52  terrupt A Long-R
142e0 75 6e 6e 69 6e 67 20 51 75 65 72 79 20 7b 48 31  unning Query {H1
142f0 32 32 37 30 7d 20 3c 53 33 30 35 30 30 3e 0a 2a  2270} <S30500>.*
14300 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
14310 6f 6e 20 63 61 75 73 65 73 20 61 6e 79 20 70 65  on causes any pe
14320 6e 64 69 6e 67 20 64 61 74 61 62 61 73 65 20 6f  nding database o
14330 70 65 72 61 74 69 6f 6e 20 74 6f 20 61 62 6f 72  peration to abor
14340 74 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20  t and.** return 
14350 61 74 20 69 74 73 20 65 61 72 6c 69 65 73 74 20  at its earliest 
14360 6f 70 70 6f 72 74 75 6e 69 74 79 2e 20 54 68 69  opportunity. Thi
14370 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 79 70  s routine is typ
14380 69 63 61 6c 6c 79 0a 2a 2a 20 63 61 6c 6c 65 64  ically.** called
14390 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20   in response to 
143a0 61 20 75 73 65 72 20 61 63 74 69 6f 6e 20 73 75  a user action su
143b0 63 68 20 61 73 20 70 72 65 73 73 69 6e 67 20 22  ch as pressing "
143c0 43 61 6e 63 65 6c 22 0a 2a 2a 20 6f 72 20 43 74  Cancel".** or Ct
143d0 72 6c 2d 43 20 77 68 65 72 65 20 74 68 65 20 75  rl-C where the u
143e0 73 65 72 20 77 61 6e 74 73 20 61 20 6c 6f 6e 67  ser wants a long
143f0 20 71 75 65 72 79 20 6f 70 65 72 61 74 69 6f 6e   query operation
14400 20 74 6f 20 68 61 6c 74 0a 2a 2a 20 69 6d 6d 65   to halt.** imme
14410 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49  diately..**.** I
14420 74 20 69 73 20 73 61 66 65 20 74 6f 20 63 61 6c  t is safe to cal
14430 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66  l this routine f
14440 72 6f 6d 20 61 20 74 68 72 65 61 64 20 64 69 66  rom a thread dif
14450 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 0a  ferent from the.
14460 2a 2a 20 74 68 72 65 61 64 20 74 68 61 74 20 69  ** thread that i
14470 73 20 63 75 72 72 65 6e 74 6c 79 20 72 75 6e 6e  s currently runn
14480 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65  ing the database
14490 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 75 74   operation.  But
144a0 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 20 73 61   it.** is not sa
144b0 66 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20  fe to call this 
144c0 72 6f 75 74 69 6e 65 20 77 69 74 68 20 61 20 5b  routine with a [
144d0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
144e0 69 6f 6e 5d 20 74 68 61 74 0a 2a 2a 20 69 73 20  ion] that.** is 
144f0 63 6c 6f 73 65 64 20 6f 72 20 6d 69 67 68 74 20  closed or might 
14500 63 6c 6f 73 65 20 62 65 66 6f 72 65 20 73 71 6c  close before sql
14510 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29  ite3_interrupt()
14520 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20   returns..**.** 
14530 49 66 20 61 6e 20 53 51 4c 20 6f 70 65 72 61 74  If an SQL operat
14540 69 6f 6e 20 69 73 20 76 65 72 79 20 6e 65 61 72  ion is very near
14550 6c 79 20 66 69 6e 69 73 68 65 64 20 61 74 20 74  ly finished at t
14560 68 65 20 74 69 6d 65 20 77 68 65 6e 0a 2a 2a 20  he time when.** 
14570 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
14580 74 28 29 20 69 73 20 63 61 6c 6c 65 64 2c 20 74  t() is called, t
14590 68 65 6e 20 69 74 20 6d 69 67 68 74 20 6e 6f 74  hen it might not
145a0 20 68 61 76 65 20 61 6e 20 6f 70 70 6f 72 74 75   have an opportu
145b0 6e 69 74 79 0a 2a 2a 20 74 6f 20 62 65 20 69 6e  nity.** to be in
145c0 74 65 72 72 75 70 74 65 64 20 61 6e 64 20 6d 69  terrupted and mi
145d0 67 68 74 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20  ght continue to 
145e0 63 6f 6d 70 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a  completion..**.*
145f0 2a 20 41 6e 20 53 51 4c 20 6f 70 65 72 61 74 69  * An SQL operati
14600 6f 6e 20 74 68 61 74 20 69 73 20 69 6e 74 65 72  on that is inter
14610 72 75 70 74 65 64 20 77 69 6c 6c 20 72 65 74 75  rupted will retu
14620 72 6e 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52  rn [SQLITE_INTER
14630 52 55 50 54 5d 2e 0a 2a 2a 20 49 66 20 74 68 65  RUPT]..** If the
14640 20 69 6e 74 65 72 72 75 70 74 65 64 20 53 51 4c   interrupted SQL
14650 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6e   operation is an
14660 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c   INSERT, UPDATE,
14670 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 74 68   or DELETE.** th
14680 61 74 20 69 73 20 69 6e 73 69 64 65 20 61 6e 20  at is inside an 
14690 65 78 70 6c 69 63 69 74 20 74 72 61 6e 73 61 63  explicit transac
146a0 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 65  tion, then the e
146b0 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f  ntire transactio
146c0 6e 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 72 6f 6c  n.** will be rol
146d0 6c 65 64 20 62 61 63 6b 20 61 75 74 6f 6d 61 74  led back automat
146e0 69 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  ically..**.** Th
146f0 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72  e sqlite3_interr
14700 75 70 74 28 44 29 20 63 61 6c 6c 20 69 73 20 69  upt(D) call is i
14710 6e 20 65 66 66 65 63 74 20 75 6e 74 69 6c 20 61  n effect until a
14720 6c 6c 20 63 75 72 72 65 6e 74 6c 79 20 72 75 6e  ll currently run
14730 6e 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74  ning.** SQL stat
14740 65 6d 65 6e 74 73 20 6f 6e 20 5b 64 61 74 61 62  ements on [datab
14750 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
14760 44 20 63 6f 6d 70 6c 65 74 65 2e 20 20 41 6e 79  D complete.  Any
14770 20 6e 65 77 20 53 51 4c 20 73 74 61 74 65 6d 65   new SQL stateme
14780 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20  nts.** that are 
14790 73 74 61 72 74 65 64 20 61 66 74 65 72 20 74 68  started after th
147a0 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72  e sqlite3_interr
147b0 75 70 74 28 29 20 63 61 6c 6c 20 61 6e 64 20 62  upt() call and b
147c0 65 66 6f 72 65 20 74 68 65 20 0a 2a 2a 20 72 75  efore the .** ru
147d0 6e 6e 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 73  nning statements
147e0 20 72 65 61 63 68 65 73 20 7a 65 72 6f 20 61 72   reaches zero ar
147f0 65 20 69 6e 74 65 72 72 75 70 74 65 64 20 61 73  e interrupted as
14800 20 69 66 20 74 68 65 79 20 68 61 64 20 62 65 65   if they had bee
14810 6e 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 70 72 69  n.** running pri
14820 6f 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65  or to the sqlite
14830 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 63 61  3_interrupt() ca
14840 6c 6c 2e 20 20 4e 65 77 20 53 51 4c 20 73 74 61  ll.  New SQL sta
14850 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20  tements.** that 
14860 61 72 65 20 73 74 61 72 74 65 64 20 61 66 74 65  are started afte
14870 72 20 74 68 65 20 72 75 6e 6e 69 6e 67 20 73 74  r the running st
14880 61 74 65 6d 65 6e 74 20 63 6f 75 6e 74 20 72 65  atement count re
14890 61 63 68 65 73 20 7a 65 72 6f 20 61 72 65 0a 2a  aches zero are.*
148a0 2a 20 6e 6f 74 20 65 66 66 65 63 74 65 64 20 62  * not effected b
148b0 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e  y the sqlite3_in
148c0 74 65 72 72 75 70 74 28 29 2e 0a 2a 2a 20 41 20  terrupt()..** A 
148d0 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
148e0 69 6e 74 65 72 72 75 70 74 28 44 29 20 74 68 61  interrupt(D) tha
148f0 74 20 6f 63 63 75 72 73 20 77 68 65 6e 20 74 68  t occurs when th
14900 65 72 65 20 61 72 65 20 6e 6f 20 72 75 6e 6e 69  ere are no runni
14910 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d  ng.** SQL statem
14920 65 6e 74 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20  ents is a no-op 
14930 61 6e 64 20 68 61 73 20 6e 6f 20 65 66 66 65 63  and has no effec
14940 74 20 6f 6e 20 53 51 4c 20 73 74 61 74 65 6d 65  t on SQL stateme
14950 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20  nts.** that are 
14960 73 74 61 72 74 65 64 20 61 66 74 65 72 20 74 68  started after th
14970 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72  e sqlite3_interr
14980 75 70 74 28 29 20 63 61 6c 6c 20 72 65 74 75 72  upt() call retur
14990 6e 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ns..**.** Requir
149a0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32  ements:.** [H122
149b0 37 31 5d 20 5b 48 31 32 32 37 32 5d 0a 2a 2a 0a  71] [H12272].**.
149c0 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61  ** If the databa
149d0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 6c  se connection cl
149e0 6f 73 65 73 20 77 68 69 6c 65 20 5b 73 71 6c 69  oses while [sqli
149f0 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 5d  te3_interrupt()]
14a00 0a 2a 2a 20 69 73 20 72 75 6e 6e 69 6e 67 20 74  .** is running t
14a10 68 65 6e 20 62 61 64 20 74 68 69 6e 67 73 20 77  hen bad things w
14a20 69 6c 6c 20 6c 69 6b 65 6c 79 20 68 61 70 70 65  ill likely happe
14a30 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  n..*/.SQLITE_API
14a40 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 69 6e   void sqlite3_in
14a50 74 65 72 72 75 70 74 28 73 71 6c 69 74 65 33 2a  terrupt(sqlite3*
14a60 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
14a70 45 46 3a 20 44 65 74 65 72 6d 69 6e 65 20 49 66  EF: Determine If
14a80 20 41 6e 20 53 51 4c 20 53 74 61 74 65 6d 65 6e   An SQL Statemen
14a90 74 20 49 73 20 43 6f 6d 70 6c 65 74 65 20 7b 48  t Is Complete {H
14aa0 31 30 35 31 30 7d 20 3c 53 37 30 32 30 30 3e 0a  10510} <S70200>.
14ab0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  **.** These rout
14ac0 69 6e 65 73 20 61 72 65 20 75 73 65 66 75 6c 20  ines are useful 
14ad0 64 75 72 69 6e 67 20 63 6f 6d 6d 61 6e 64 2d 6c  during command-l
14ae0 69 6e 65 20 69 6e 70 75 74 20 74 6f 20 64 65 74  ine input to det
14af0 65 72 6d 69 6e 65 20 69 66 20 74 68 65 0a 2a 2a  ermine if the.**
14b00 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72   currently enter
14b10 65 64 20 74 65 78 74 20 73 65 65 6d 73 20 74 6f  ed text seems to
14b20 20 66 6f 72 6d 20 61 20 63 6f 6d 70 6c 65 74 65   form a complete
14b30 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 6f   SQL statement o
14b40 72 0a 2a 2a 20 69 66 20 61 64 64 69 74 69 6f 6e  r.** if addition
14b50 61 6c 20 69 6e 70 75 74 20 69 73 20 6e 65 65 64  al input is need
14b60 65 64 20 62 65 66 6f 72 65 20 73 65 6e 64 69 6e  ed before sendin
14b70 67 20 74 68 65 20 74 65 78 74 20 69 6e 74 6f 0a  g the text into.
14b80 2a 2a 20 53 51 4c 69 74 65 20 66 6f 72 20 70 61  ** SQLite for pa
14b90 72 73 69 6e 67 2e 20 20 54 68 65 73 65 20 72 6f  rsing.  These ro
14ba0 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 31 20  utines return 1 
14bb0 69 66 20 74 68 65 20 69 6e 70 75 74 20 73 74 72  if the input str
14bc0 69 6e 67 0a 2a 2a 20 61 70 70 65 61 72 73 20 74  ing.** appears t
14bd0 6f 20 62 65 20 61 20 63 6f 6d 70 6c 65 74 65 20  o be a complete 
14be0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  SQL statement.  
14bf0 41 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6a  A statement is j
14c00 75 64 67 65 64 20 74 6f 20 62 65 0a 2a 2a 20 63  udged to be.** c
14c10 6f 6d 70 6c 65 74 65 20 69 66 20 69 74 20 65 6e  omplete if it en
14c20 64 73 20 77 69 74 68 20 61 20 73 65 6d 69 63 6f  ds with a semico
14c30 6c 6f 6e 20 74 6f 6b 65 6e 20 61 6e 64 20 69 73  lon token and is
14c40 20 6e 6f 74 20 61 20 70 72 65 66 69 78 20 6f 66   not a prefix of
14c50 20 61 0a 2a 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65   a.** well-forme
14c60 64 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52  d CREATE TRIGGER
14c70 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 53 65 6d   statement.  Sem
14c80 69 63 6f 6c 6f 6e 73 20 74 68 61 74 20 61 72 65  icolons that are
14c90 20 65 6d 62 65 64 64 65 64 20 77 69 74 68 69 6e   embedded within
14ca0 0a 2a 2a 20 73 74 72 69 6e 67 20 6c 69 74 65 72  .** string liter
14cb0 61 6c 73 20 6f 72 20 71 75 6f 74 65 64 20 69 64  als or quoted id
14cc0 65 6e 74 69 66 69 65 72 20 6e 61 6d 65 73 20 6f  entifier names o
14cd0 72 20 63 6f 6d 6d 65 6e 74 73 20 61 72 65 20 6e  r comments are n
14ce0 6f 74 0a 2a 2a 20 69 6e 64 65 70 65 6e 64 65 6e  ot.** independen
14cf0 74 20 74 6f 6b 65 6e 73 20 28 74 68 65 79 20 61  t tokens (they a
14d00 72 65 20 70 61 72 74 20 6f 66 20 74 68 65 20 74  re part of the t
14d10 6f 6b 65 6e 20 69 6e 20 77 68 69 63 68 20 74 68  oken in which th
14d20 65 79 20 61 72 65 0a 2a 2a 20 65 6d 62 65 64 64  ey are.** embedd
14d30 65 64 29 20 61 6e 64 20 74 68 75 73 20 64 6f 20  ed) and thus do 
14d40 6e 6f 74 20 63 6f 75 6e 74 20 61 73 20 61 20 73  not count as a s
14d50 74 61 74 65 6d 65 6e 74 20 74 65 72 6d 69 6e 61  tatement termina
14d60 74 6f 72 2e 20 20 57 68 69 74 65 73 70 61 63 65  tor.  Whitespace
14d70 0a 2a 2a 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73  .** and comments
14d80 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 74 68 65   that follow the
14d90 20 66 69 6e 61 6c 20 73 65 6d 69 63 6f 6c 6f 6e   final semicolon
14da0 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a   are ignored..**
14db0 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
14dc0 65 73 20 72 65 74 75 72 6e 20 30 20 69 66 20 74  es return 0 if t
14dd0 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  he statement is 
14de0 69 6e 63 6f 6d 70 6c 65 74 65 2e 20 20 49 66 20  incomplete.  If 
14df0 61 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  a.** memory allo
14e00 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68  cation fails, th
14e10 65 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  en SQLITE_NOMEM 
14e20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
14e30 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
14e40 73 20 64 6f 20 6e 6f 74 20 70 61 72 73 65 20 74  s do not parse t
14e50 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
14e60 73 20 74 68 75 73 0a 2a 2a 20 77 69 6c 6c 20 6e  s thus.** will n
14e70 6f 74 20 64 65 74 65 63 74 20 73 79 6e 74 61 63  ot detect syntac
14e80 74 69 63 61 6c 6c 79 20 69 6e 63 6f 72 72 65 63  tically incorrec
14e90 74 20 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  t SQL..**.** If 
14ea0 53 51 4c 69 74 65 20 68 61 73 20 6e 6f 74 20 62  SQLite has not b
14eb0 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  een initialized 
14ec0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 69  using [sqlite3_i
14ed0 6e 69 74 69 61 6c 69 7a 65 28 29 5d 20 70 72 69  nitialize()] pri
14ee0 6f 72 20 0a 2a 2a 20 74 6f 20 69 6e 76 6f 6b 69  or .** to invoki
14ef0 6e 67 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  ng sqlite3_compl
14f00 65 74 65 31 36 28 29 20 74 68 65 6e 20 73 71 6c  ete16() then sql
14f10 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
14f20 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20  ) is invoked.** 
14f30 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79  automatically by
14f40 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
14f50 65 31 36 28 29 2e 20 20 49 66 20 74 68 61 74 20  e16().  If that 
14f60 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 66  initialization f
14f70 61 69 6c 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68  ails,.** then th
14f80 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66  e return value f
14f90 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  rom sqlite3_comp
14fa0 6c 65 74 65 31 36 28 29 20 77 69 6c 6c 20 62 65  lete16() will be
14fb0 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 72 65 67   non-zero.** reg
14fc0 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68  ardless of wheth
14fd0 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e  er or not the in
14fe0 70 75 74 20 53 51 4c 20 69 73 20 63 6f 6d 70 6c  put SQL is compl
14ff0 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ete..**.** Requi
15000 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 35 31 31  rements: [H10511
15010 5d 20 5b 48 31 30 35 31 32 5d 0a 2a 2a 0a 2a 2a  ] [H10512].**.**
15020 20 54 68 65 20 69 6e 70 75 74 20 74 6f 20 5b 73   The input to [s
15030 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28  qlite3_complete(
15040 29 5d 20 6d 75 73 74 20 62 65 20 61 20 7a 65 72  )] must be a zer
15050 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20  o-terminated.** 
15060 55 54 46 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a  UTF-8 string..**
15070 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 74 6f  .** The input to
15080 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65   [sqlite3_comple
15090 74 65 31 36 28 29 5d 20 6d 75 73 74 20 62 65 20  te16()] must be 
150a0 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  a zero-terminate
150b0 64 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 69  d.** UTF-16 stri
150c0 6e 67 20 69 6e 20 6e 61 74 69 76 65 20 62 79 74  ng in native byt
150d0 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a 53 51 4c 49  e order..*/.SQLI
150e0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
150f0 65 33 5f 63 6f 6d 70 6c 65 74 65 28 63 6f 6e 73  e3_complete(cons
15100 74 20 63 68 61 72 20 2a 73 71 6c 29 3b 0a 53 51  t char *sql);.SQ
15110 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
15120 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28  ite3_complete16(
15130 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 29  const void *sql)
15140 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
15150 46 3a 20 52 65 67 69 73 74 65 72 20 41 20 43 61  F: Register A Ca
15160 6c 6c 62 61 63 6b 20 54 6f 20 48 61 6e 64 6c 65  llback To Handle
15170 20 53 51 4c 49 54 45 5f 42 55 53 59 20 45 72 72   SQLITE_BUSY Err
15180 6f 72 73 20 7b 48 31 32 33 31 30 7d 20 3c 53 34  ors {H12310} <S4
15190 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  0400>.**.** This
151a0 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 61 20   routine sets a 
151b0 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f  callback functio
151c0 6e 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 20  n that might be 
151d0 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72  invoked whenever
151e0 0a 2a 2a 20 61 6e 20 61 74 74 65 6d 70 74 20 69  .** an attempt i
151f0 73 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20 61  s made to open a
15200 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20   database table 
15210 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68 72  that another thr
15220 65 61 64 0a 2a 2a 20 6f 72 20 70 72 6f 63 65 73  ead.** or proces
15230 73 20 68 61 73 20 6c 6f 63 6b 65 64 2e 0a 2a 2a  s has locked..**
15240 0a 2a 2a 20 49 66 20 74 68 65 20 62 75 73 79 20  .** If the busy 
15250 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55 4c 4c  callback is NULL
15260 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 42  , then [SQLITE_B
15270 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f  USY] or [SQLITE_
15280 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a 2a  IOERR_BLOCKED].*
15290 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6d  * is returned im
152a0 6d 65 64 69 61 74 65 6c 79 20 75 70 6f 6e 20 65  mediately upon e
152b0 6e 63 6f 75 6e 74 65 72 69 6e 67 20 74 68 65 20  ncountering the 
152c0 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 62 75 73  lock. If the bus
152d0 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73  y callback.** is
152e0 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   not NULL, then 
152f0 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 77 69 6c  the callback wil
15300 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 69 74  l be invoked wit
15310 68 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e  h two arguments.
15320 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
15330 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
15340 20 68 61 6e 64 6c 65 72 20 69 73 20 61 20 63 6f   handler is a co
15350 70 79 20 6f 66 20 74 68 65 20 76 6f 69 64 2a 20  py of the void* 
15360 70 6f 69 6e 74 65 72 20 77 68 69 63 68 0a 2a 2a  pointer which.**
15370 20 69 73 20 74 68 65 20 74 68 69 72 64 20 61 72   is the third ar
15380 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65  gument to sqlite
15390 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 29  3_busy_handler()
153a0 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72  .  The second ar
153b0 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65  gument to.** the
153c0 20 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63   handler callbac
153d0 6b 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  k is the number 
153e0 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 74 68  of times that th
153f0 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 68  e busy handler h
15400 61 73 0a 2a 2a 20 62 65 65 6e 20 69 6e 76 6f 6b  as.** been invok
15410 65 64 20 66 6f 72 20 74 68 69 73 20 6c 6f 63 6b  ed for this lock
15420 69 6e 67 20 65 76 65 6e 74 2e 20 20 49 66 20 74  ing event.  If t
15430 68 65 0a 2a 2a 20 62 75 73 79 20 63 61 6c 6c 62  he.** busy callb
15440 61 63 6b 20 72 65 74 75 72 6e 73 20 30 2c 20 74  ack returns 0, t
15450 68 65 6e 20 6e 6f 20 61 64 64 69 74 69 6f 6e 61  hen no additiona
15460 6c 20 61 74 74 65 6d 70 74 73 20 61 72 65 20 6d  l attempts are m
15470 61 64 65 20 74 6f 0a 2a 2a 20 61 63 63 65 73 73  ade to.** access
15480 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e   the database an
15490 64 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20  d [SQLITE_BUSY] 
154a0 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52  or [SQLITE_IOERR
154b0 5f 42 4c 4f 43 4b 45 44 5d 20 69 73 20 72 65 74  _BLOCKED] is ret
154c0 75 72 6e 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65  urned..** If the
154d0 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e   callback return
154e0 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
154f0 20 61 6e 6f 74 68 65 72 20 61 74 74 65 6d 70 74   another attempt
15500 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 6f  .** is made to o
15510 70 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  pen the database
15520 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64   for reading and
15530 20 74 68 65 20 63 79 63 6c 65 20 72 65 70 65 61   the cycle repea
15540 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72  ts..**.** The pr
15550 65 73 65 6e 63 65 20 6f 66 20 61 20 62 75 73 79  esence of a busy
15560 20 68 61 6e 64 6c 65 72 20 64 6f 65 73 20 6e 6f   handler does no
15570 74 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74  t guarantee that
15580 20 69 74 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f   it will be invo
15590 6b 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 72  ked.** when ther
155a0 65 20 69 73 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e  e is lock conten
155b0 74 69 6f 6e 2e 20 49 66 20 53 51 4c 69 74 65 20  tion. If SQLite 
155c0 64 65 74 65 72 6d 69 6e 65 73 20 74 68 61 74 20  determines that 
155d0 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73  invoking the bus
155e0 79 0a 2a 2a 20 68 61 6e 64 6c 65 72 20 63 6f 75  y.** handler cou
155f0 6c 64 20 72 65 73 75 6c 74 20 69 6e 20 61 20 64  ld result in a d
15600 65 61 64 6c 6f 63 6b 2c 20 69 74 20 77 69 6c 6c  eadlock, it will
15610 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 72 65   go ahead and re
15620 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53  turn [SQLITE_BUS
15630 59 5d 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 45  Y].** or [SQLITE
15640 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 20  _IOERR_BLOCKED] 
15650 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 6f 6b  instead of invok
15660 69 6e 67 20 74 68 65 20 62 75 73 79 20 68 61 6e  ing the busy han
15670 64 6c 65 72 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65  dler..** Conside
15680 72 20 61 20 73 63 65 6e 61 72 69 6f 20 77 68 65  r a scenario whe
15690 72 65 20 6f 6e 65 20 70 72 6f 63 65 73 73 20 69  re one process i
156a0 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 61 64  s holding a read
156b0 20 6c 6f 63 6b 20 74 68 61 74 0a 2a 2a 20 69 74   lock that.** it
156c0 20 69 73 20 74 72 79 69 6e 67 20 74 6f 20 70 72   is trying to pr
156d0 6f 6d 6f 74 65 20 74 6f 20 61 20 72 65 73 65 72  omote to a reser
156e0 76 65 64 20 6c 6f 63 6b 20 61 6e 64 0a 2a 2a 20  ved lock and.** 
156f0 61 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 73  a second process
15700 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 65   is holding a re
15710 73 65 72 76 65 64 20 6c 6f 63 6b 20 74 68 61 74  served lock that
15720 20 69 74 20 69 73 20 74 72 79 69 6e 67 0a 2a 2a   it is trying.**
15730 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 61   to promote to a
15740 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
15750 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 72 6f  .  The first pro
15760 63 65 73 73 20 63 61 6e 6e 6f 74 20 70 72 6f 63  cess cannot proc
15770 65 65 64 0a 2a 2a 20 62 65 63 61 75 73 65 20 69  eed.** because i
15780 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20  t is blocked by 
15790 74 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 74  the second and t
157a0 68 65 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73  he second proces
157b0 73 20 63 61 6e 6e 6f 74 0a 2a 2a 20 70 72 6f 63  s cannot.** proc
157c0 65 65 64 20 62 65 63 61 75 73 65 20 69 74 20 69  eed because it i
157d0 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 74 68 65  s blocked by the
157e0 20 66 69 72 73 74 2e 20 20 49 66 20 62 6f 74 68   first.  If both
157f0 20 70 72 6f 63 65 73 73 65 73 0a 2a 2a 20 69 6e   processes.** in
15800 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 61  voke the busy ha
15810 6e 64 6c 65 72 73 2c 20 6e 65 69 74 68 65 72 20  ndlers, neither 
15820 77 69 6c 6c 20 6d 61 6b 65 20 61 6e 79 20 70 72  will make any pr
15830 6f 67 72 65 73 73 2e 20 20 54 68 65 72 65 66 6f  ogress.  Therefo
15840 72 65 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 72 65  re,.** SQLite re
15850 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 42 55  turns [SQLITE_BU
15860 53 59 5d 20 66 6f 72 20 74 68 65 20 66 69 72 73  SY] for the firs
15870 74 20 70 72 6f 63 65 73 73 2c 20 68 6f 70 69 6e  t process, hopin
15880 67 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 77  g that this.** w
15890 69 6c 6c 20 69 6e 64 75 63 65 20 74 68 65 20 66  ill induce the f
158a0 69 72 73 74 20 70 72 6f 63 65 73 73 20 74 6f 20  irst process to 
158b0 72 65 6c 65 61 73 65 20 69 74 73 20 72 65 61 64  release its read
158c0 20 6c 6f 63 6b 20 61 6e 64 20 61 6c 6c 6f 77 0a   lock and allow.
158d0 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72  ** the second pr
158e0 6f 63 65 73 73 20 74 6f 20 70 72 6f 63 65 65 64  ocess to proceed
158f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61  ..**.** The defa
15900 75 6c 74 20 62 75 73 79 20 63 61 6c 6c 62 61 63  ult busy callbac
15910 6b 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  k is NULL..**.**
15920 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 42 55 53   The [SQLITE_BUS
15930 59 5d 20 65 72 72 6f 72 20 69 73 20 63 6f 6e 76  Y] error is conv
15940 65 72 74 65 64 20 74 6f 20 5b 53 51 4c 49 54 45  erted to [SQLITE
15950 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a  _IOERR_BLOCKED].
15960 2a 2a 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69  ** when SQLite i
15970 73 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  s in the middle 
15980 6f 66 20 61 20 6c 61 72 67 65 20 74 72 61 6e 73  of a large trans
15990 61 63 74 69 6f 6e 20 77 68 65 72 65 20 61 6c 6c  action where all
159a0 20 74 68 65 0a 2a 2a 20 63 68 61 6e 67 65 73 20   the.** changes 
159b0 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 69 6e 74  will not fit int
159c0 6f 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20  o the in-memory 
159d0 63 61 63 68 65 2e 20 20 53 51 4c 69 74 65 20 77  cache.  SQLite w
159e0 69 6c 6c 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68  ill.** already h
159f0 6f 6c 64 20 61 20 52 45 53 45 52 56 45 44 20 6c  old a RESERVED l
15a00 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
15a10 61 73 65 20 66 69 6c 65 2c 20 62 75 74 20 69 74  ase file, but it
15a20 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 70 72 6f   needs.** to pro
15a30 6d 6f 74 65 20 74 68 69 73 20 6c 6f 63 6b 20 74  mote this lock t
15a40 6f 20 45 58 43 4c 55 53 49 56 45 20 73 6f 20 74  o EXCLUSIVE so t
15a50 68 61 74 20 69 74 20 63 61 6e 20 73 70 69 6c 6c  hat it can spill
15a60 20 63 61 63 68 65 0a 2a 2a 20 70 61 67 65 73 20   cache.** pages 
15a70 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  into the databas
15a80 65 20 66 69 6c 65 20 77 69 74 68 6f 75 74 20 68  e file without h
15a90 61 72 6d 20 74 6f 20 63 6f 6e 63 75 72 72 65 6e  arm to concurren
15aa0 74 0a 2a 2a 20 72 65 61 64 65 72 73 2e 20 20 49  t.** readers.  I
15ab0 66 20 69 74 20 69 73 20 75 6e 61 62 6c 65 20 74  f it is unable t
15ac0 6f 20 70 72 6f 6d 6f 74 65 20 74 68 65 20 6c 6f  o promote the lo
15ad0 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 69 6e 2d  ck, then the in-
15ae0 6d 65 6d 6f 72 79 0a 2a 2a 20 63 61 63 68 65 20  memory.** cache 
15af0 77 69 6c 6c 20 62 65 20 6c 65 66 74 20 69 6e 20  will be left in 
15b00 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20  an inconsistent 
15b10 73 74 61 74 65 20 61 6e 64 20 73 6f 20 74 68 65  state and so the
15b20 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69   error.** code i
15b30 73 20 70 72 6f 6d 6f 74 65 64 20 66 72 6f 6d 20  s promoted from 
15b40 74 68 65 20 72 65 6c 61 74 69 76 65 6c 79 20 62  the relatively b
15b50 65 6e 69 67 6e 20 5b 53 51 4c 49 54 45 5f 42 55  enign [SQLITE_BU
15b60 53 59 5d 20 74 6f 0a 2a 2a 20 74 68 65 20 6d 6f  SY] to.** the mo
15b70 72 65 20 73 65 76 65 72 65 20 5b 53 51 4c 49 54  re severe [SQLIT
15b80 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d  E_IOERR_BLOCKED]
15b90 2e 20 20 54 68 69 73 20 65 72 72 6f 72 20 63 6f  .  This error co
15ba0 64 65 20 70 72 6f 6d 6f 74 69 6f 6e 0a 2a 2a 20  de promotion.** 
15bb0 66 6f 72 63 65 73 20 61 6e 20 61 75 74 6f 6d 61  forces an automa
15bc0 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20  tic rollback of 
15bd0 74 68 65 20 63 68 61 6e 67 65 73 2e 20 20 53 65  the changes.  Se
15be0 65 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65 66  e the.** <a href
15bf0 3d 22 2f 63 76 73 74 72 61 63 2f 77 69 6b 69 3f  ="/cvstrac/wiki?
15c00 70 3d 43 6f 72 72 75 70 74 69 6f 6e 46 6f 6c 6c  p=CorruptionFoll
15c10 6f 77 69 6e 67 42 75 73 79 45 72 72 6f 72 22 3e  owingBusyError">
15c20 0a 2a 2a 20 43 6f 72 72 75 70 74 69 6f 6e 46 6f  .** CorruptionFo
15c30 6c 6c 6f 77 69 6e 67 42 75 73 79 45 72 72 6f 72  llowingBusyError
15c40 3c 2f 61 3e 20 77 69 6b 69 20 70 61 67 65 20 66  </a> wiki page f
15c50 6f 72 20 61 20 64 69 73 63 75 73 73 69 6f 6e 20  or a discussion 
15c60 6f 66 20 77 68 79 0a 2a 2a 20 74 68 69 73 20 69  of why.** this i
15c70 73 20 69 6d 70 6f 72 74 61 6e 74 2e 0a 2a 2a 0a  s important..**.
15c80 2a 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c  ** There can onl
15c90 79 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62 75  y be a single bu
15ca0 73 79 20 68 61 6e 64 6c 65 72 20 64 65 66 69 6e  sy handler defin
15cb0 65 64 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 5b  ed for each.** [
15cc0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
15cd0 69 6f 6e 5d 2e 20 20 53 65 74 74 69 6e 67 20 61  ion].  Setting a
15ce0 20 6e 65 77 20 62 75 73 79 20 68 61 6e 64 6c 65   new busy handle
15cf0 72 20 63 6c 65 61 72 73 20 61 6e 79 0a 2a 2a 20  r clears any.** 
15d00 70 72 65 76 69 6f 75 73 6c 79 20 73 65 74 20 68  previously set h
15d10 61 6e 64 6c 65 72 2e 20 20 4e 6f 74 65 20 74 68  andler.  Note th
15d20 61 74 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69  at calling [sqli
15d30 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74  te3_busy_timeout
15d40 28 29 5d 0a 2a 2a 20 77 69 6c 6c 20 61 6c 73 6f  ()].** will also
15d50 20 73 65 74 20 6f 72 20 63 6c 65 61 72 20 74 68   set or clear th
15d60 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a  e busy handler..
15d70 2a 2a 0a 2a 2a 20 54 68 65 20 62 75 73 79 20 63  **.** The busy c
15d80 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 20 6e  allback should n
15d90 6f 74 20 74 61 6b 65 20 61 6e 79 20 61 63 74 69  ot take any acti
15da0 6f 6e 73 20 77 68 69 63 68 20 6d 6f 64 69 66 79  ons which modify
15db0 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
15dc0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74   connection that
15dd0 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75 73   invoked the bus
15de0 79 20 68 61 6e 64 6c 65 72 2e 20 20 41 6e 79 20  y handler.  Any 
15df0 73 75 63 68 20 61 63 74 69 6f 6e 73 0a 2a 2a 20  such actions.** 
15e00 72 65 73 75 6c 74 20 69 6e 20 75 6e 64 65 66 69  result in undefi
15e10 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a  ned behavior..**
15e20 20 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74   .** Requirement
15e30 73 3a 0a 2a 2a 20 5b 48 31 32 33 31 31 5d 20 5b  s:.** [H12311] [
15e40 48 31 32 33 31 32 5d 20 5b 48 31 32 33 31 34 5d  H12312] [H12314]
15e50 20 5b 48 31 32 33 31 36 5d 20 5b 48 31 32 33 31   [H12316] [H1231
15e60 38 5d 0a 2a 2a 0a 2a 2a 20 41 20 62 75 73 79 20  8].**.** A busy 
15e70 68 61 6e 64 6c 65 72 20 6d 75 73 74 20 6e 6f 74  handler must not
15e80 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74 61 62   close the datab
15e90 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a  ase connection.*
15ea0 2a 20 6f 72 20 5b 70 72 65 70 61 72 65 64 20 73  * or [prepared s
15eb0 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 69  tatement] that i
15ec0 6e 76 6f 6b 65 64 20 74 68 65 20 62 75 73 79 20  nvoked the busy 
15ed0 68 61 6e 64 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49  handler..*/.SQLI
15ee0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
15ef0 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28  e3_busy_handler(
15f00 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a 29  sqlite3*, int(*)
15f10 28 76 6f 69 64 2a 2c 69 6e 74 29 2c 20 76 6f 69  (void*,int), voi
15f20 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  d*);../*.** CAPI
15f30 33 52 45 46 3a 20 53 65 74 20 41 20 42 75 73 79  3REF: Set A Busy
15f40 20 54 69 6d 65 6f 75 74 20 7b 48 31 32 33 34 30   Timeout {H12340
15f50 7d 20 3c 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a  } <S40410>.**.**
15f60 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65   This routine se
15f70 74 73 20 61 20 5b 73 71 6c 69 74 65 33 5f 62 75  ts a [sqlite3_bu
15f80 73 79 5f 68 61 6e 64 6c 65 72 20 7c 20 62 75 73  sy_handler | bus
15f90 79 20 68 61 6e 64 6c 65 72 5d 20 74 68 61 74 20  y handler] that 
15fa0 73 6c 65 65 70 73 0a 2a 2a 20 66 6f 72 20 61 20  sleeps.** for a 
15fb0 73 70 65 63 69 66 69 65 64 20 61 6d 6f 75 6e 74  specified amount
15fc0 20 6f 66 20 74 69 6d 65 20 77 68 65 6e 20 61 20   of time when a 
15fd0 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 2e  table is locked.
15fe0 20 20 54 68 65 20 68 61 6e 64 6c 65 72 0a 2a 2a    The handler.**
15ff0 20 77 69 6c 6c 20 73 6c 65 65 70 20 6d 75 6c 74   will sleep mult
16000 69 70 6c 65 20 74 69 6d 65 73 20 75 6e 74 69 6c  iple times until
16010 20 61 74 20 6c 65 61 73 74 20 22 6d 73 22 20 6d   at least "ms" m
16020 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73  illiseconds of s
16030 6c 65 65 70 69 6e 67 0a 2a 2a 20 68 61 76 65 20  leeping.** have 
16040 61 63 63 75 6d 75 6c 61 74 65 64 2e 20 7b 48 31  accumulated. {H1
16050 32 33 34 33 7d 20 41 66 74 65 72 20 22 6d 73 22  2343} After "ms"
16060 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66   milliseconds of
16070 20 73 6c 65 65 70 69 6e 67 2c 0a 2a 2a 20 74 68   sleeping,.** th
16080 65 20 68 61 6e 64 6c 65 72 20 72 65 74 75 72 6e  e handler return
16090 73 20 30 20 77 68 69 63 68 20 63 61 75 73 65 73  s 0 which causes
160a0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
160b0 5d 20 74 6f 20 72 65 74 75 72 6e 0a 2a 2a 20 5b  ] to return.** [
160c0 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72 20  SQLITE_BUSY] or 
160d0 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c  [SQLITE_IOERR_BL
160e0 4f 43 4b 45 44 5d 2e 0a 2a 2a 0a 2a 2a 20 43 61  OCKED]..**.** Ca
160f0 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69  lling this routi
16100 6e 65 20 77 69 74 68 20 61 6e 20 61 72 67 75 6d  ne with an argum
16110 65 6e 74 20 6c 65 73 73 20 74 68 61 6e 20 6f 72  ent less than or
16120 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 0a 2a   equal to zero.*
16130 2a 20 74 75 72 6e 73 20 6f 66 66 20 61 6c 6c 20  * turns off all 
16140 62 75 73 79 20 68 61 6e 64 6c 65 72 73 2e 0a 2a  busy handlers..*
16150 2a 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20 6f  *.** There can o
16160 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65 20  nly be a single 
16170 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66 6f 72  busy handler for
16180 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a   a particular.**
16190 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
161a0 63 74 69 6f 6e 5d 20 61 6e 79 20 61 6e 79 20 67  ction] any any g
161b0 69 76 65 6e 20 6d 6f 6d 65 6e 74 2e 20 20 49 66  iven moment.  If
161c0 20 61 6e 6f 74 68 65 72 20 62 75 73 79 20 68 61   another busy ha
161d0 6e 64 6c 65 72 0a 2a 2a 20 77 61 73 20 64 65 66  ndler.** was def
161e0 69 6e 65 64 20 20 28 75 73 69 6e 67 20 5b 73 71  ined  (using [sq
161f0 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c  lite3_busy_handl
16200 65 72 28 29 5d 29 20 70 72 69 6f 72 20 74 6f 20  er()]) prior to 
16210 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20  calling.** this 
16220 72 6f 75 74 69 6e 65 2c 20 74 68 61 74 20 6f 74  routine, that ot
16230 68 65 72 20 62 75 73 79 20 68 61 6e 64 6c 65 72  her busy handler
16240 20 69 73 20 63 6c 65 61 72 65 64 2e 0a 2a 2a 0a   is cleared..**.
16250 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
16260 0a 2a 2a 20 5b 48 31 32 33 34 31 5d 20 5b 48 31  .** [H12341] [H1
16270 32 33 34 33 5d 20 5b 48 31 32 33 34 34 5d 0a 2a  2343] [H12344].*
16280 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
16290 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69   sqlite3_busy_ti
162a0 6d 65 6f 75 74 28 73 71 6c 69 74 65 33 2a 2c 20  meout(sqlite3*, 
162b0 69 6e 74 20 6d 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20  int ms);../*.** 
162c0 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 76 65 6e  CAPI3REF: Conven
162d0 69 65 6e 63 65 20 52 6f 75 74 69 6e 65 73 20 46  ience Routines F
162e0 6f 72 20 52 75 6e 6e 69 6e 67 20 51 75 65 72 69  or Running Queri
162f0 65 73 20 7b 48 31 32 33 37 30 7d 20 3c 53 31 30  es {H12370} <S10
16300 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 44 65 66 69 6e  000>.**.** Defin
16310 69 74 69 6f 6e 3a 20 41 20 3c 62 3e 72 65 73 75  ition: A <b>resu
16320 6c 74 20 74 61 62 6c 65 3c 2f 62 3e 20 69 73 20  lt table</b> is 
16330 6d 65 6d 6f 72 79 20 64 61 74 61 20 73 74 72 75  memory data stru
16340 63 74 75 72 65 20 63 72 65 61 74 65 64 20 62 79  cture created by
16350 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   the.** [sqlite3
16360 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 20 69 6e  _get_table()] in
16370 74 65 72 66 61 63 65 2e 20 20 41 20 72 65 73 75  terface.  A resu
16380 6c 74 20 74 61 62 6c 65 20 72 65 63 6f 72 64 73  lt table records
16390 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65   the.** complete
163a0 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 20 66   query results f
163b0 72 6f 6d 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  rom one or more 
163c0 71 75 65 72 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 54  queries..**.** T
163d0 68 65 20 74 61 62 6c 65 20 63 6f 6e 63 65 70 74  he table concept
163e0 75 61 6c 6c 79 20 68 61 73 20 61 20 6e 75 6d 62  ually has a numb
163f0 65 72 20 6f 66 20 72 6f 77 73 20 61 6e 64 20 63  er of rows and c
16400 6f 6c 75 6d 6e 73 2e 20 20 42 75 74 0a 2a 2a 20  olumns.  But.** 
16410 74 68 65 73 65 20 6e 75 6d 62 65 72 73 20 61 72  these numbers ar
16420 65 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68  e not part of th
16430 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 69  e result table i
16440 74 73 65 6c 66 2e 20 20 54 68 65 73 65 0a 2a 2a  tself.  These.**
16450 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6f 62 74   numbers are obt
16460 61 69 6e 65 64 20 73 65 70 61 72 61 74 65 6c 79  ained separately
16470 2e 20 20 4c 65 74 20 4e 20 62 65 20 74 68 65 20  .  Let N be the 
16480 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 0a 2a  number of rows.*
16490 2a 20 61 6e 64 20 4d 20 62 65 20 74 68 65 20 6e  * and M be the n
164a0 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
164b0 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 75 6c 74  ..**.** A result
164c0 20 74 61 62 6c 65 20 69 73 20 61 6e 20 61 72 72   table is an arr
164d0 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74  ay of pointers t
164e0 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  o zero-terminate
164f0 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73 2e  d UTF-8 strings.
16500 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 28 4e  .** There are (N
16510 2b 31 29 2a 4d 20 65 6c 65 6d 65 6e 74 73 20 69  +1)*M elements i
16520 6e 20 74 68 65 20 61 72 72 61 79 2e 20 20 54 68  n the array.  Th
16530 65 20 66 69 72 73 74 20 4d 20 70 6f 69 6e 74 65  e first M pointe
16540 72 73 20 70 6f 69 6e 74 0a 2a 2a 20 74 6f 20 7a  rs point.** to z
16550 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 73  ero-terminated s
16560 74 72 69 6e 67 73 20 74 68 61 74 20 20 63 6f 6e  trings that  con
16570 74 61 69 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f  tain the names o
16580 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 2e 0a 2a  f the columns..*
16590 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e 67 20  * The remaining 
165a0 65 6e 74 72 69 65 73 20 61 6c 6c 20 70 6f 69 6e  entries all poin
165b0 74 20 74 6f 20 71 75 65 72 79 20 72 65 73 75 6c  t to query resul
165c0 74 73 2e 20 20 4e 55 4c 4c 20 76 61 6c 75 65 73  ts.  NULL values
165d0 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 4e 55   result.** in NU
165e0 4c 4c 20 70 6f 69 6e 74 65 72 73 2e 20 20 41 6c  LL pointers.  Al
165f0 6c 20 6f 74 68 65 72 20 76 61 6c 75 65 73 20 61  l other values a
16600 72 65 20 69 6e 20 74 68 65 69 72 20 55 54 46 2d  re in their UTF-
16610 38 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  8 zero-terminate
16620 64 0a 2a 2a 20 73 74 72 69 6e 67 20 72 65 70 72  d.** string repr
16630 65 73 65 6e 74 61 74 69 6f 6e 20 61 73 20 72 65  esentation as re
16640 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74  turned by [sqlit
16650 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29  e3_column_text()
16660 5d 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 75 6c  ]..**.** A resul
16670 74 20 74 61 62 6c 65 20 6d 69 67 68 74 20 63 6f  t table might co
16680 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20 6f 72 20  nsist of one or 
16690 6d 6f 72 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  more memory allo
166a0 63 61 74 69 6f 6e 73 2e 0a 2a 2a 20 49 74 20 69  cations..** It i
166b0 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 70 61  s not safe to pa
166c0 73 73 20 61 20 72 65 73 75 6c 74 20 74 61 62 6c  ss a result tabl
166d0 65 20 64 69 72 65 63 74 6c 79 20 74 6f 20 5b 73  e directly to [s
166e0 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a  qlite3_free()]..
166f0 2a 2a 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c  ** A result tabl
16700 65 20 73 68 6f 75 6c 64 20 62 65 20 64 65 61 6c  e should be deal
16710 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 5b 73  located using [s
16720 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c  qlite3_free_tabl
16730 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 61  e()]..**.** As a
16740 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 74 68 65  n example of the
16750 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 66 6f   result table fo
16760 72 6d 61 74 2c 20 73 75 70 70 6f 73 65 20 61 20  rmat, suppose a 
16770 71 75 65 72 79 20 72 65 73 75 6c 74 0a 2a 2a 20  query result.** 
16780 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  is as follows:.*
16790 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65  *.** <blockquote
167a0 3e 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 20 20  ><pre>.**       
167b0 20 4e 61 6d 65 20 20 20 20 20 20 20 20 7c 20 41   Name        | A
167c0 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 2d 2d 2d  ge.**        ---
167d0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
167e0 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 20 20 41  ----.**        A
167f0 6c 69 63 65 20 20 20 20 20 20 20 7c 20 34 33 0a  lice       | 43.
16800 2a 2a 20 20 20 20 20 20 20 20 42 6f 62 20 20 20  **        Bob   
16810 20 20 20 20 20 20 7c 20 32 38 0a 2a 2a 20 20 20        | 28.**   
16820 20 20 20 20 20 43 69 6e 64 79 20 20 20 20 20 20       Cindy      
16830 20 7c 20 32 31 0a 2a 2a 20 3c 2f 70 72 65 3e 3c   | 21.** </pre><
16840 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a  /blockquote>.**.
16850 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 77 6f  ** There are two
16860 20 63 6f 6c 75 6d 6e 20 28 4d 3d 3d 32 29 20 61   column (M==2) a
16870 6e 64 20 74 68 72 65 65 20 72 6f 77 73 20 28 4e  nd three rows (N
16880 3d 3d 33 29 2e 20 20 54 68 75 73 20 74 68 65 0a  ==3).  Thus the.
16890 2a 2a 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20  ** result table 
168a0 68 61 73 20 38 20 65 6e 74 72 69 65 73 2e 20 20  has 8 entries.  
168b0 53 75 70 70 6f 73 65 20 74 68 65 20 72 65 73 75  Suppose the resu
168c0 6c 74 20 74 61 62 6c 65 20 69 73 20 73 74 6f 72  lt table is stor
168d0 65 64 0a 2a 2a 20 69 6e 20 61 6e 20 61 72 72 61  ed.** in an arra
168e0 79 20 6e 61 6d 65 73 20 61 7a 52 65 73 75 6c 74  y names azResult
168f0 2e 20 20 54 68 65 6e 20 61 7a 52 65 73 75 6c 74  .  Then azResult
16900 20 68 6f 6c 64 73 20 74 68 69 73 20 63 6f 6e 74   holds this cont
16910 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63  ent:.**.** <bloc
16920 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20  kquote><pre>.** 
16930 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26         azResult&
16940 23 39 31 3b 30 5d 20 3d 20 22 4e 61 6d 65 22 3b  #91;0] = "Name";
16950 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73  .**        azRes
16960 75 6c 74 26 23 39 31 3b 31 5d 20 3d 20 22 41 67  ult&#91;1] = "Ag
16970 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a  e";.**        az
16980 52 65 73 75 6c 74 26 23 39 31 3b 32 5d 20 3d 20  Result&#91;2] = 
16990 22 41 6c 69 63 65 22 3b 0a 2a 2a 20 20 20 20 20  "Alice";.**     
169a0 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b     azResult&#91;
169b0 33 5d 20 3d 20 22 34 33 22 3b 0a 2a 2a 20 20 20  3] = "43";.**   
169c0 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39       azResult&#9
169d0 31 3b 34 5d 20 3d 20 22 42 6f 62 22 3b 0a 2a 2a  1;4] = "Bob";.**
169e0 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74          azResult
169f0 26 23 39 31 3b 35 5d 20 3d 20 22 32 38 22 3b 0a  &#91;5] = "28";.
16a00 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75  **        azResu
16a10 6c 74 26 23 39 31 3b 36 5d 20 3d 20 22 43 69 6e  lt&#91;6] = "Cin
16a20 64 79 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61  dy";.**        a
16a30 7a 52 65 73 75 6c 74 26 23 39 31 3b 37 5d 20 3d  zResult&#91;7] =
16a40 20 22 32 31 22 3b 0a 2a 2a 20 3c 2f 70 72 65 3e   "21";.** </pre>
16a50 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a  </blockquote>.**
16a60 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
16a70 67 65 74 5f 74 61 62 6c 65 28 29 20 66 75 6e 63  get_table() func
16a80 74 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 20 6f  tion evaluates o
16a90 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 73 65  ne or more.** se
16aa0 6d 69 63 6f 6c 6f 6e 2d 73 65 70 61 72 61 74 65  micolon-separate
16ab0 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  d SQL statements
16ac0 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d 74 65 72   in the zero-ter
16ad0 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 0a 2a 2a  minated UTF-8.**
16ae0 20 73 74 72 69 6e 67 20 6f 66 20 69 74 73 20 32   string of its 2
16af0 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 49  nd parameter.  I
16b00 74 20 72 65 74 75 72 6e 73 20 61 20 72 65 73 75  t returns a resu
16b10 6c 74 20 74 61 62 6c 65 20 74 6f 20 74 68 65 0a  lt table to the.
16b20 2a 2a 20 70 6f 69 6e 74 65 72 20 67 69 76 65 6e  ** pointer given
16b30 20 69 6e 20 69 74 73 20 33 72 64 20 70 61 72 61   in its 3rd para
16b40 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 66 74  meter..**.** Aft
16b50 65 72 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66  er the calling f
16b60 75 6e 63 74 69 6f 6e 20 68 61 73 20 66 69 6e 69  unction has fini
16b70 73 68 65 64 20 75 73 69 6e 67 20 74 68 65 20 72  shed using the r
16b80 65 73 75 6c 74 2c 20 69 74 20 73 68 6f 75 6c 64  esult, it should
16b90 0a 2a 2a 20 70 61 73 73 20 74 68 65 20 70 6f 69  .** pass the poi
16ba0 6e 74 65 72 20 74 6f 20 74 68 65 20 72 65 73 75  nter to the resu
16bb0 6c 74 20 74 61 62 6c 65 20 74 6f 20 73 71 6c 69  lt table to sqli
16bc0 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29  te3_free_table()
16bd0 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20   in order to.** 
16be0 72 65 6c 65 61 73 65 20 74 68 65 20 6d 65 6d 6f  release the memo
16bf0 72 79 20 74 68 61 74 20 77 61 73 20 6d 61 6c 6c  ry that was mall
16c00 6f 63 65 64 2e 20 20 42 65 63 61 75 73 65 20 6f  oced.  Because o
16c10 66 20 74 68 65 20 77 61 79 20 74 68 65 0a 2a 2a  f the way the.**
16c20 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
16c30 28 29 5d 20 68 61 70 70 65 6e 73 20 77 69 74 68  ()] happens with
16c40 69 6e 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  in sqlite3_get_t
16c50 61 62 6c 65 28 29 2c 20 74 68 65 20 63 61 6c 6c  able(), the call
16c60 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  ing.** function 
16c70 6d 75 73 74 20 6e 6f 74 20 74 72 79 20 74 6f 20  must not try to 
16c80 63 61 6c 6c 20 5b 73 71 6c 69 74 65 33 5f 66 72  call [sqlite3_fr
16c90 65 65 28 29 5d 20 64 69 72 65 63 74 6c 79 2e 20  ee()] directly. 
16ca0 20 4f 6e 6c 79 0a 2a 2a 20 5b 73 71 6c 69 74 65   Only.** [sqlite
16cb0 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 5d 20  3_free_table()] 
16cc0 69 73 20 61 62 6c 65 20 74 6f 20 72 65 6c 65 61  is able to relea
16cd0 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 70 72  se the memory pr
16ce0 6f 70 65 72 6c 79 20 61 6e 64 20 73 61 66 65 6c  operly and safel
16cf0 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  y..**.** The sql
16d00 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29  ite3_get_table()
16d10 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 69 6d   interface is im
16d20 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20 77  plemented as a w
16d30 72 61 70 70 65 72 20 61 72 6f 75 6e 64 0a 2a 2a  rapper around.**
16d40 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29   [sqlite3_exec()
16d50 5d 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f  ].  The sqlite3_
16d60 67 65 74 5f 74 61 62 6c 65 28 29 20 72 6f 75 74  get_table() rout
16d70 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 68 61 76  ine does not hav
16d80 65 20 61 63 63 65 73 73 0a 2a 2a 20 74 6f 20 61  e access.** to a
16d90 6e 79 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 61  ny internal data
16da0 20 73 74 72 75 63 74 75 72 65 73 20 6f 66 20 53   structures of S
16db0 51 4c 69 74 65 2e 20 20 49 74 20 75 73 65 73 20  QLite.  It uses 
16dc0 6f 6e 6c 79 20 74 68 65 20 70 75 62 6c 69 63 0a  only the public.
16dd0 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 64 65 66  ** interface def
16de0 69 6e 65 64 20 68 65 72 65 2e 20 20 41 73 20 61  ined here.  As a
16df0 20 63 6f 6e 73 65 71 75 65 6e 63 65 2c 20 65 72   consequence, er
16e00 72 6f 72 73 20 74 68 61 74 20 6f 63 63 75 72 20  rors that occur 
16e10 69 6e 20 74 68 65 0a 2a 2a 20 77 72 61 70 70 65  in the.** wrappe
16e20 72 20 6c 61 79 65 72 20 6f 75 74 73 69 64 65 20  r layer outside 
16e30 6f 66 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  of the internal 
16e40 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d  [sqlite3_exec()]
16e50 20 63 61 6c 6c 20 61 72 65 20 6e 6f 74 0a 2a 2a   call are not.**
16e60 20 72 65 66 6c 65 63 74 65 64 20 69 6e 20 73 75   reflected in su
16e70 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74  bsequent calls t
16e80 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  o [sqlite3_errco
16e90 64 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65  de()] or [sqlite
16ea0 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a 0a  3_errmsg()]..**.
16eb0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
16ec0 0a 2a 2a 20 5b 48 31 32 33 37 31 5d 20 5b 48 31  .** [H12371] [H1
16ed0 32 33 37 33 5d 20 5b 48 31 32 33 37 34 5d 20 5b  2373] [H12374] [
16ee0 48 31 32 33 37 36 5d 20 5b 48 31 32 33 37 39 5d  H12376] [H12379]
16ef0 20 5b 48 31 32 33 38 32 5d 0a 2a 2f 0a 53 51 4c   [H12382].*/.SQL
16f00 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
16f10 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 0a 20  te3_get_table(. 
16f20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
16f30 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65         /* An ope
16f40 6e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  n database */.  
16f50 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
16f60 2c 20 20 20 20 20 2f 2a 20 53 51 4c 20 74 6f 20  ,     /* SQL to 
16f70 62 65 20 65 76 61 6c 75 61 74 65 64 20 2a 2f 0a  be evaluated */.
16f80 20 20 63 68 61 72 20 2a 2a 2a 70 61 7a 52 65 73    char ***pazRes
16f90 75 6c 74 2c 20 20 20 20 2f 2a 20 52 65 73 75 6c  ult,    /* Resul
16fa0 74 73 20 6f 66 20 74 68 65 20 71 75 65 72 79 20  ts of the query 
16fb0 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52 6f 77 2c  */.  int *pnRow,
16fc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
16fd0 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 72  mber of result r
16fe0 6f 77 73 20 77 72 69 74 74 65 6e 20 68 65 72 65  ows written here
16ff0 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6c   */.  int *pnCol
17000 75 6d 6e 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e  umn,        /* N
17010 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20  umber of result 
17020 63 6f 6c 75 6d 6e 73 20 77 72 69 74 74 65 6e 20  columns written 
17030 68 65 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  here */.  char *
17040 2a 70 7a 45 72 72 6d 73 67 20 20 20 20 20 20 20  *pzErrmsg       
17050 2f 2a 20 45 72 72 6f 72 20 6d 73 67 20 77 72 69  /* Error msg wri
17060 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 3b 0a  tten here */.);.
17070 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
17080 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62  sqlite3_free_tab
17090 6c 65 28 63 68 61 72 20 2a 2a 72 65 73 75 6c 74  le(char **result
170a0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
170b0 45 46 3a 20 46 6f 72 6d 61 74 74 65 64 20 53 74  EF: Formatted St
170c0 72 69 6e 67 20 50 72 69 6e 74 69 6e 67 20 46 75  ring Printing Fu
170d0 6e 63 74 69 6f 6e 73 20 7b 48 31 37 34 30 30 7d  nctions {H17400}
170e0 20 3c 53 37 30 30 30 30 3e 3c 53 32 30 30 30 30   <S70000><S20000
170f0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  >.**.** These ro
17100 75 74 69 6e 65 73 20 61 72 65 20 77 6f 72 6b 2d  utines are work-
17110 61 6c 69 6b 65 73 20 6f 66 20 74 68 65 20 22 70  alikes of the "p
17120 72 69 6e 74 66 28 29 22 20 66 61 6d 69 6c 79 20  rintf()" family 
17130 6f 66 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20  of functions.** 
17140 66 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 72  from the standar
17150 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a  d C library..**.
17160 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  ** The sqlite3_m
17170 70 72 69 6e 74 66 28 29 20 61 6e 64 20 73 71 6c  printf() and sql
17180 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 29 20  ite3_vmprintf() 
17190 72 6f 75 74 69 6e 65 73 20 77 72 69 74 65 20 74  routines write t
171a0 68 65 69 72 0a 2a 2a 20 72 65 73 75 6c 74 73 20  heir.** results 
171b0 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61  into memory obta
171c0 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74  ined from [sqlit
171d0 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2a  e3_malloc()]..**
171e0 20 54 68 65 20 73 74 72 69 6e 67 73 20 72 65 74   The strings ret
171f0 75 72 6e 65 64 20 62 79 20 74 68 65 73 65 20 74  urned by these t
17200 77 6f 20 72 6f 75 74 69 6e 65 73 20 73 68 6f 75  wo routines shou
17210 6c 64 20 62 65 0a 2a 2a 20 72 65 6c 65 61 73 65  ld be.** release
17220 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 66 72  d by [sqlite3_fr
17230 65 65 28 29 5d 2e 20 20 42 6f 74 68 20 72 6f 75  ee()].  Both rou
17240 74 69 6e 65 73 20 72 65 74 75 72 6e 20 61 0a 2a  tines return a.*
17250 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69  * NULL pointer i
17260 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  f [sqlite3_mallo
17270 63 28 29 5d 20 69 73 20 75 6e 61 62 6c 65 20 74  c()] is unable t
17280 6f 20 61 6c 6c 6f 63 61 74 65 20 65 6e 6f 75 67  o allocate enoug
17290 68 0a 2a 2a 20 6d 65 6d 6f 72 79 20 74 6f 20 68  h.** memory to h
172a0 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 69 6e  old the resultin
172b0 67 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  g string..**.** 
172c0 49 6e 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  In sqlite3_snpri
172d0 6e 74 66 28 29 20 72 6f 75 74 69 6e 65 20 69 73  ntf() routine is
172e0 20 73 69 6d 69 6c 61 72 20 74 6f 20 22 73 6e 70   similar to "snp
172f0 72 69 6e 74 66 28 29 22 20 66 72 6f 6d 0a 2a 2a  rintf()" from.**
17300 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20   the standard C 
17310 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 72 65  library.  The re
17320 73 75 6c 74 20 69 73 20 77 72 69 74 74 65 6e 20  sult is written 
17330 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 62 75 66 66  into the.** buff
17340 65 72 20 73 75 70 70 6c 69 65 64 20 61 73 20 74  er supplied as t
17350 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  he second parame
17360 74 65 72 20 77 68 6f 73 65 20 73 69 7a 65 20 69  ter whose size i
17370 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a 20 74 68  s given by.** th
17380 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
17390 72 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  r. Note that the
173a0 20 6f 72 64 65 72 20 6f 66 20 74 68 65 0a 2a 2a   order of the.**
173b0 20 66 69 72 73 74 20 74 77 6f 20 70 61 72 61 6d   first two param
173c0 65 74 65 72 73 20 69 73 20 72 65 76 65 72 73 65  eters is reverse
173d0 64 20 66 72 6f 6d 20 73 6e 70 72 69 6e 74 66 28  d from snprintf(
173e0 29 2e 20 20 54 68 69 73 20 69 73 20 61 6e 0a 2a  ).  This is an.*
173f0 2a 20 68 69 73 74 6f 72 69 63 61 6c 20 61 63 63  * historical acc
17400 69 64 65 6e 74 20 74 68 61 74 20 63 61 6e 6e 6f  ident that canno
17410 74 20 62 65 20 66 69 78 65 64 20 77 69 74 68 6f  t be fixed witho
17420 75 74 20 62 72 65 61 6b 69 6e 67 0a 2a 2a 20 62  ut breaking.** b
17430 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69  ackwards compati
17440 62 69 6c 69 74 79 2e 20 20 4e 6f 74 65 20 61 6c  bility.  Note al
17450 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f  so that sqlite3_
17460 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 72 65  snprintf().** re
17470 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
17480 74 6f 20 69 74 73 20 62 75 66 66 65 72 20 69 6e  to its buffer in
17490 73 74 65 61 64 20 6f 66 20 74 68 65 20 6e 75 6d  stead of the num
174a0 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 72 61 63  ber of.** charac
174b0 74 65 72 73 20 61 63 74 75 61 6c 6c 79 20 77 72  ters actually wr
174c0 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 62  itten into the b
174d0 75 66 66 65 72 2e 20 20 57 65 20 61 64 6d 69 74  uffer.  We admit
174e0 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6e 75 6d   that.** the num
174f0 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72  ber of character
17500 73 20 77 72 69 74 74 65 6e 20 77 6f 75 6c 64 20  s written would 
17510 62 65 20 61 20 6d 6f 72 65 20 75 73 65 66 75 6c  be a more useful
17520 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65   return.** value
17530 20 62 75 74 20 77 65 20 63 61 6e 6e 6f 74 20 63   but we cannot c
17540 68 61 6e 67 65 20 74 68 65 20 69 6d 70 6c 65 6d  hange the implem
17550 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69  entation of sqli
17560 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a  te3_snprintf().*
17570 2a 20 6e 6f 77 20 77 69 74 68 6f 75 74 20 62 72  * now without br
17580 65 61 6b 69 6e 67 20 63 6f 6d 70 61 74 69 62 69  eaking compatibi
17590 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 6c  lity..**.** As l
175a0 6f 6e 67 20 61 73 20 74 68 65 20 62 75 66 66 65  ong as the buffe
175b0 72 20 73 69 7a 65 20 69 73 20 67 72 65 61 74 65  r size is greate
175c0 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 73 71 6c  r than zero, sql
175d0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 0a  ite3_snprintf().
175e0 2a 2a 20 67 75 61 72 61 6e 74 65 65 73 20 74 68  ** guarantees th
175f0 61 74 20 74 68 65 20 62 75 66 66 65 72 20 69 73  at the buffer is
17600 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d 74 65 72   always zero-ter
17610 6d 69 6e 61 74 65 64 2e 20 20 54 68 65 20 66 69  minated.  The fi
17620 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  rst.** parameter
17630 20 22 6e 22 20 69 73 20 74 68 65 20 74 6f 74 61   "n" is the tota
17640 6c 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75  l size of the bu
17650 66 66 65 72 2c 20 69 6e 63 6c 75 64 69 6e 67 20  ffer, including 
17660 73 70 61 63 65 20 66 6f 72 0a 2a 2a 20 74 68 65  space for.** the
17670 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72   zero terminator
17680 2e 20 20 53 6f 20 74 68 65 20 6c 6f 6e 67 65 73  .  So the longes
17690 74 20 73 74 72 69 6e 67 20 74 68 61 74 20 63 61  t string that ca
176a0 6e 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 0a  n be completely.
176b0 2a 2a 20 77 72 69 74 74 65 6e 20 77 69 6c 6c 20  ** written will 
176c0 62 65 20 6e 2d 31 20 63 68 61 72 61 63 74 65 72  be n-1 character
176d0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  s..**.** These r
176e0 6f 75 74 69 6e 65 73 20 61 6c 6c 20 69 6d 70 6c  outines all impl
176f0 65 6d 65 6e 74 20 73 6f 6d 65 20 61 64 64 69 74  ement some addit
17700 69 6f 6e 61 6c 20 66 6f 72 6d 61 74 74 69 6e 67  ional formatting
17710 0a 2a 2a 20 6f 70 74 69 6f 6e 73 20 74 68 61 74  .** options that
17720 20 61 72 65 20 75 73 65 66 75 6c 20 66 6f 72 20   are useful for 
17730 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 53 51 4c  constructing SQL
17740 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20   statements..** 
17750 41 6c 6c 20 6f 66 20 74 68 65 20 75 73 75 61 6c  All of the usual
17760 20 70 72 69 6e 74 66 28 29 20 66 6f 72 6d 61 74   printf() format
17770 74 69 6e 67 20 6f 70 74 69 6f 6e 73 20 61 70 70  ting options app
17780 6c 79 2e 20 20 49 6e 20 61 64 64 69 74 69 6f 6e  ly.  In addition
17790 2c 20 74 68 65 72 65 0a 2a 2a 20 69 73 20 61 72  , there.** is ar
177a0 65 20 22 25 71 22 2c 20 22 25 51 22 2c 20 61 6e  e "%q", "%Q", an
177b0 64 20 22 25 7a 22 20 6f 70 74 69 6f 6e 73 2e 0a  d "%z" options..
177c0 2a 2a 0a 2a 2a 20 54 68 65 20 25 71 20 6f 70 74  **.** The %q opt
177d0 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25  ion works like %
177e0 73 20 69 6e 20 74 68 61 74 20 69 74 20 73 75 62  s in that it sub
177f0 73 74 69 74 75 74 65 73 20 61 20 6e 75 6c 6c 2d  stitutes a null-
17800 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 73 74  terminated.** st
17810 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 61 72  ring from the ar
17820 67 75 6d 65 6e 74 20 6c 69 73 74 2e 20 20 42 75  gument list.  Bu
17830 74 20 25 71 20 61 6c 73 6f 20 64 6f 75 62 6c 65  t %q also double
17840 73 20 65 76 65 72 79 20 27 5c 27 27 20 63 68 61  s every '\'' cha
17850 72 61 63 74 65 72 2e 0a 2a 2a 20 25 71 20 69 73  racter..** %q is
17860 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75 73   designed for us
17870 65 20 69 6e 73 69 64 65 20 61 20 73 74 72 69 6e  e inside a strin
17880 67 20 6c 69 74 65 72 61 6c 2e 20 20 42 79 20 64  g literal.  By d
17890 6f 75 62 6c 69 6e 67 20 65 61 63 68 20 27 5c 27  oubling each '\'
178a0 27 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 20 69  '.** character i
178b0 74 20 65 73 63 61 70 65 73 20 74 68 61 74 20 63  t escapes that c
178c0 68 61 72 61 63 74 65 72 20 61 6e 64 20 61 6c 6c  haracter and all
178d0 6f 77 73 20 69 74 20 74 6f 20 62 65 20 69 6e 73  ows it to be ins
178e0 65 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68  erted into.** th
178f0 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  e string..**.** 
17900 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 73 73  For example, ass
17910 75 6d 65 20 74 68 65 20 73 74 72 69 6e 67 20 76  ume the string v
17920 61 72 69 61 62 6c 65 20 7a 54 65 78 74 20 63 6f  ariable zText co
17930 6e 74 61 69 6e 73 20 74 65 78 74 20 61 73 20 66  ntains text as f
17940 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62  ollows:.**.** <b
17950 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a  lockquote><pre>.
17960 2a 2a 20 20 63 68 61 72 20 2a 7a 54 65 78 74 20  **  char *zText 
17970 3d 20 22 49 74 27 73 20 61 20 68 61 70 70 79 20  = "It's a happy 
17980 64 61 79 21 22 3b 0a 2a 2a 20 3c 2f 70 72 65 3e  day!";.** </pre>
17990 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a  </blockquote>.**
179a0 0a 2a 2a 20 4f 6e 65 20 63 61 6e 20 75 73 65 20  .** One can use 
179b0 74 68 69 73 20 74 65 78 74 20 69 6e 20 61 6e 20  this text in an 
179c0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 73  SQL statement as
179d0 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
179e0 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65  <blockquote><pre
179f0 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 53 51 4c  >.**  char *zSQL
17a00 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
17a10 74 66 28 22 49 4e 53 45 52 54 20 49 4e 54 4f 20  tf("INSERT INTO 
17a20 74 61 62 6c 65 20 56 41 4c 55 45 53 28 27 25 71  table VALUES('%q
17a30 27 29 22 2c 20 7a 54 65 78 74 29 3b 0a 2a 2a 20  ')", zText);.** 
17a40 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62   sqlite3_exec(db
17a50 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 2c 20 30 29  , zSQL, 0, 0, 0)
17a60 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.**  sqlite3_fr
17a70 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 3c 2f 70  ee(zSQL);.** </p
17a80 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e  re></blockquote>
17a90 0a 2a 2a 0a 2a 2a 20 42 65 63 61 75 73 65 20 74  .**.** Because t
17aa0 68 65 20 25 71 20 66 6f 72 6d 61 74 20 73 74 72  he %q format str
17ab0 69 6e 67 20 69 73 20 75 73 65 64 2c 20 74 68 65  ing is used, the
17ac0 20 27 5c 27 27 20 63 68 61 72 61 63 74 65 72 20   '\'' character 
17ad0 69 6e 20 7a 54 65 78 74 0a 2a 2a 20 69 73 20 65  in zText.** is e
17ae0 73 63 61 70 65 64 20 61 6e 64 20 74 68 65 20 53  scaped and the S
17af0 51 4c 20 67 65 6e 65 72 61 74 65 64 20 69 73 20  QL generated is 
17b00 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
17b10 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70  * <blockquote><p
17b20 72 65 3e 0a 2a 2a 20 20 49 4e 53 45 52 54 20 49  re>.**  INSERT I
17b30 4e 54 4f 20 74 61 62 6c 65 31 20 56 41 4c 55 45  NTO table1 VALUE
17b40 53 28 27 49 74 27 27 73 20 61 20 68 61 70 70 79  S('It''s a happy
17b50 20 64 61 79 21 27 29 0a 2a 2a 20 3c 2f 70 72 65   day!').** </pre
17b60 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a  ></blockquote>.*
17b70 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 63 6f 72  *.** This is cor
17b80 72 65 63 74 2e 20 20 48 61 64 20 77 65 20 75 73  rect.  Had we us
17b90 65 64 20 25 73 20 69 6e 73 74 65 61 64 20 6f 66  ed %s instead of
17ba0 20 25 71 2c 20 74 68 65 20 67 65 6e 65 72 61 74   %q, the generat
17bb0 65 64 20 53 51 4c 0a 2a 2a 20 77 6f 75 6c 64 20  ed SQL.** would 
17bc0 68 61 76 65 20 6c 6f 6f 6b 65 64 20 6c 69 6b 65  have looked like
17bd0 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c   this:.**.** <bl
17be0 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a  ockquote><pre>.*
17bf0 2a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74  *  INSERT INTO t
17c00 61 62 6c 65 31 20 56 41 4c 55 45 53 28 27 49 74  able1 VALUES('It
17c10 27 73 20 61 20 68 61 70 70 79 20 64 61 79 21 27  's a happy day!'
17c20 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c  );.** </pre></bl
17c30 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20  ockquote>.**.** 
17c40 54 68 69 73 20 73 65 63 6f 6e 64 20 65 78 61 6d  This second exam
17c50 70 6c 65 20 69 73 20 61 6e 20 53 51 4c 20 73 79  ple is an SQL sy
17c60 6e 74 61 78 20 65 72 72 6f 72 2e 20 20 41 73 20  ntax error.  As 
17c70 61 20 67 65 6e 65 72 61 6c 20 72 75 6c 65 20 79  a general rule y
17c80 6f 75 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 6c 77  ou should.** alw
17c90 61 79 73 20 75 73 65 20 25 71 20 69 6e 73 74 65  ays use %q inste
17ca0 61 64 20 6f 66 20 25 73 20 77 68 65 6e 20 69 6e  ad of %s when in
17cb0 73 65 72 74 69 6e 67 20 74 65 78 74 20 69 6e 74  serting text int
17cc0 6f 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72  o a string liter
17cd0 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 25 51  al..**.** The %Q
17ce0 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 20 6c 69   option works li
17cf0 6b 65 20 25 71 20 65 78 63 65 70 74 20 69 74 20  ke %q except it 
17d00 61 6c 73 6f 20 61 64 64 73 20 73 69 6e 67 6c 65  also adds single
17d10 20 71 75 6f 74 65 73 20 61 72 6f 75 6e 64 0a 2a   quotes around.*
17d20 2a 20 74 68 65 20 6f 75 74 73 69 64 65 20 6f 66  * the outside of
17d30 20 74 68 65 20 74 6f 74 61 6c 20 73 74 72 69 6e   the total strin
17d40 67 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79  g.  Additionally
17d50 2c 20 69 66 20 74 68 65 20 70 61 72 61 6d 65 74  , if the paramet
17d60 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 72 67  er in the.** arg
17d70 75 6d 65 6e 74 20 6c 69 73 74 20 69 73 20 61 20  ument list is a 
17d80 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 25 51  NULL pointer, %Q
17d90 20 73 75 62 73 74 69 74 75 74 65 73 20 74 68 65   substitutes the
17da0 20 74 65 78 74 20 22 4e 55 4c 4c 22 20 28 77 69   text "NULL" (wi
17db0 74 68 6f 75 74 0a 2a 2a 20 73 69 6e 67 6c 65 20  thout.** single 
17dc0 71 75 6f 74 65 73 29 20 69 6e 20 70 6c 61 63 65  quotes) in place
17dd0 20 6f 66 20 74 68 65 20 25 51 20 6f 70 74 69 6f   of the %Q optio
17de0 6e 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78 61 6d  n.  So, for exam
17df0 70 6c 65 2c 20 6f 6e 65 20 63 6f 75 6c 64 20 73  ple, one could s
17e00 61 79 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b  ay:.**.** <block
17e10 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20  quote><pre>.**  
17e20 63 68 61 72 20 2a 7a 53 51 4c 20 3d 20 73 71 6c  char *zSQL = sql
17e30 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 49 4e  ite3_mprintf("IN
17e40 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 20  SERT INTO table 
17e50 56 41 4c 55 45 53 28 25 51 29 22 2c 20 7a 54 65  VALUES(%Q)", zTe
17e60 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33  xt);.**  sqlite3
17e70 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 4c 2c 20  _exec(db, zSQL, 
17e80 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20 73 71  0, 0, 0);.**  sq
17e90 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 51 4c 29  lite3_free(zSQL)
17ea0 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f  ;.** </pre></blo
17eb0 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54  ckquote>.**.** T
17ec0 68 65 20 63 6f 64 65 20 61 62 6f 76 65 20 77 69  he code above wi
17ed0 6c 6c 20 72 65 6e 64 65 72 20 61 20 63 6f 72 72  ll render a corr
17ee0 65 63 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ect SQL statemen
17ef0 74 20 69 6e 20 74 68 65 20 7a 53 51 4c 0a 2a 2a  t in the zSQL.**
17f00 20 76 61 72 69 61 62 6c 65 20 65 76 65 6e 20 69   variable even i
17f10 66 20 74 68 65 20 7a 54 65 78 74 20 76 61 72 69  f the zText vari
17f20 61 62 6c 65 20 69 73 20 61 20 4e 55 4c 4c 20 70  able is a NULL p
17f30 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  ointer..**.** Th
17f40 65 20 22 25 7a 22 20 66 6f 72 6d 61 74 74 69 6e  e "%z" formattin
17f50 67 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 20 65  g option works e
17f60 78 61 63 74 6c 79 20 6c 69 6b 65 20 22 25 73 22  xactly like "%s"
17f70 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 61 64 64   with the.** add
17f80 69 74 69 6f 6e 20 74 68 61 74 20 61 66 74 65 72  ition that after
17f90 20 74 68 65 20 73 74 72 69 6e 67 20 68 61 73 20   the string has 
17fa0 62 65 65 6e 20 72 65 61 64 20 61 6e 64 20 63 6f  been read and co
17fb0 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65  pied into.** the
17fc0 20 72 65 73 75 6c 74 2c 20 5b 73 71 6c 69 74 65   result, [sqlite
17fd0 33 5f 66 72 65 65 28 29 5d 20 69 73 20 63 61 6c  3_free()] is cal
17fe0 6c 65 64 20 6f 6e 20 74 68 65 20 69 6e 70 75 74  led on the input
17ff0 20 73 74 72 69 6e 67 2e 20 7b 45 4e 44 7d 0a 2a   string. {END}.*
18000 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
18010 73 3a 0a 2a 2a 20 5b 48 31 37 34 30 33 5d 20 5b  s:.** [H17403] [
18020 48 31 37 34 30 36 5d 20 5b 48 31 37 34 30 37 5d  H17406] [H17407]
18030 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
18040 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6d 70 72  har *sqlite3_mpr
18050 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 2a  intf(const char*
18060 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 41 50  ,...);.SQLITE_AP
18070 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  I char *sqlite3_
18080 76 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 63  vmprintf(const c
18090 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b 0a  har*, va_list);.
180a0 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20  SQLITE_API char 
180b0 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74  *sqlite3_snprint
180c0 66 28 69 6e 74 2c 63 68 61 72 2a 2c 63 6f 6e 73  f(int,char*,cons
180d0 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 0a  t char*, ...);..
180e0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
180f0 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69 6f  Memory Allocatio
18100 6e 20 53 75 62 73 79 73 74 65 6d 20 7b 48 31 37  n Subsystem {H17
18110 33 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a  300} <S20000>.**
18120 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 63  .** The SQLite c
18130 6f 72 65 20 20 75 73 65 73 20 74 68 65 73 65 20  ore  uses these 
18140 74 68 72 65 65 20 72 6f 75 74 69 6e 65 73 20 66  three routines f
18150 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 6f 77  or all of its ow
18160 6e 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 20 6d 65  n.** internal me
18170 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
18180 6e 65 65 64 73 2e 20 22 43 6f 72 65 22 20 69 6e  needs. "Core" in
18190 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65   the previous se
181a0 6e 74 65 6e 63 65 0a 2a 2a 20 64 6f 65 73 20 6e  ntence.** does n
181b0 6f 74 20 69 6e 63 6c 75 64 65 20 6f 70 65 72 61  ot include opera
181c0 74 69 6e 67 2d 73 79 73 74 65 6d 20 73 70 65 63  ting-system spec
181d0 69 66 69 63 20 56 46 53 20 69 6d 70 6c 65 6d 65  ific VFS impleme
181e0 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a  ntation.  The.**
181f0 20 57 69 6e 64 6f 77 73 20 56 46 53 20 75 73 65   Windows VFS use
18200 73 20 6e 61 74 69 76 65 20 6d 61 6c 6c 6f 63 28  s native malloc(
18210 29 20 61 6e 64 20 66 72 65 65 28 29 20 66 6f 72  ) and free() for
18220 20 73 6f 6d 65 20 6f 70 65 72 61 74 69 6f 6e 73   some operations
18230 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
18240 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 6f 75  te3_malloc() rou
18250 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 70  tine returns a p
18260 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c 6f 63  ointer to a bloc
18270 6b 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79 20 61  k.** of memory a
18280 74 20 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20  t least N bytes 
18290 69 6e 20 6c 65 6e 67 74 68 2c 20 77 68 65 72 65  in length, where
182a0 20 4e 20 69 73 20 74 68 65 20 70 61 72 61 6d 65   N is the parame
182b0 74 65 72 2e 0a 2a 2a 20 49 66 20 73 71 6c 69 74  ter..** If sqlit
182c0 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 73 20 75  e3_malloc() is u
182d0 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61 69 6e 20  nable to obtain 
182e0 73 75 66 66 69 63 69 65 6e 74 20 66 72 65 65 0a  sufficient free.
182f0 2a 2a 20 6d 65 6d 6f 72 79 2c 20 69 74 20 72 65  ** memory, it re
18300 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69  turns a NULL poi
18310 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 70 61  nter.  If the pa
18320 72 61 6d 65 74 65 72 20 4e 20 74 6f 0a 2a 2a 20  rameter N to.** 
18330 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
18340 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61   is zero or nega
18350 74 69 76 65 20 74 68 65 6e 20 73 71 6c 69 74 65  tive then sqlite
18360 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 65 74 75 72  3_malloc() retur
18370 6e 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69  ns.** a NULL poi
18380 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c  nter..**.** Call
18390 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ing sqlite3_free
183a0 28 29 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65  () with a pointe
183b0 72 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 74  r previously ret
183c0 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69  urned.** by sqli
183d0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 6f 72 20  te3_malloc() or 
183e0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
183f0 29 20 72 65 6c 65 61 73 65 73 20 74 68 61 74 20  ) releases that 
18400 6d 65 6d 6f 72 79 20 73 6f 0a 2a 2a 20 74 68 61  memory so.** tha
18410 74 20 69 74 20 6d 69 67 68 74 20 62 65 20 72 65  t it might be re
18420 75 73 65 64 2e 20 20 54 68 65 20 73 71 6c 69 74  used.  The sqlit
18430 65 33 5f 66 72 65 65 28 29 20 72 6f 75 74 69 6e  e3_free() routin
18440 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 20  e is.** a no-op 
18450 69 66 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74  if is called wit
18460 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  h a NULL pointer
18470 2e 20 20 50 61 73 73 69 6e 67 20 61 20 4e 55 4c  .  Passing a NUL
18480 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20  L pointer.** to 
18490 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69  sqlite3_free() i
184a0 73 20 68 61 72 6d 6c 65 73 73 2e 20 20 41 66 74  s harmless.  Aft
184b0 65 72 20 62 65 69 6e 67 20 66 72 65 65 64 2c 20  er being freed, 
184c0 6d 65 6d 6f 72 79 0a 2a 2a 20 73 68 6f 75 6c 64  memory.** should
184d0 20 6e 65 69 74 68 65 72 20 62 65 20 72 65 61 64   neither be read
184e0 20 6e 6f 72 20 77 72 69 74 74 65 6e 2e 20 20 45   nor written.  E
184f0 76 65 6e 20 72 65 61 64 69 6e 67 20 70 72 65 76  ven reading prev
18500 69 6f 75 73 6c 79 20 66 72 65 65 64 0a 2a 2a 20  iously freed.** 
18510 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20 72 65 73  memory might res
18520 75 6c 74 20 69 6e 20 61 20 73 65 67 6d 65 6e 74  ult in a segment
18530 61 74 69 6f 6e 20 66 61 75 6c 74 20 6f 72 20 6f  ation fault or o
18540 74 68 65 72 20 73 65 76 65 72 65 20 65 72 72 6f  ther severe erro
18550 72 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 63 6f 72  r..** Memory cor
18560 72 75 70 74 69 6f 6e 2c 20 61 20 73 65 67 6d 65  ruption, a segme
18570 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 2c 20 6f  ntation fault, o
18580 72 20 6f 74 68 65 72 20 73 65 76 65 72 65 20 65  r other severe e
18590 72 72 6f 72 0a 2a 2a 20 6d 69 67 68 74 20 72 65  rror.** might re
185a0 73 75 6c 74 20 69 66 20 73 71 6c 69 74 65 33 5f  sult if sqlite3_
185b0 66 72 65 65 28 29 20 69 73 20 63 61 6c 6c 65 64  free() is called
185c0 20 77 69 74 68 20 61 20 6e 6f 6e 2d 4e 55 4c 4c   with a non-NULL
185d0 20 70 6f 69 6e 74 65 72 20 74 68 61 74 0a 2a 2a   pointer that.**
185e0 20 77 61 73 20 6e 6f 74 20 6f 62 74 61 69 6e 65   was not obtaine
185f0 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d  d from sqlite3_m
18600 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69 74  alloc() or sqlit
18610 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a  e3_realloc()..**
18620 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
18630 72 65 61 6c 6c 6f 63 28 29 20 69 6e 74 65 72 66  realloc() interf
18640 61 63 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ace attempts to 
18650 72 65 73 69 7a 65 20 61 0a 2a 2a 20 70 72 69 6f  resize a.** prio
18660 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  r memory allocat
18670 69 6f 6e 20 74 6f 20 62 65 20 61 74 20 6c 65 61  ion to be at lea
18680 73 74 20 4e 20 62 79 74 65 73 2c 20 77 68 65 72  st N bytes, wher
18690 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a 20 73 65  e N is the.** se
186a0 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20  cond parameter. 
186b0 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   The memory allo
186c0 63 61 74 69 6f 6e 20 74 6f 20 62 65 20 72 65 73  cation to be res
186d0 69 7a 65 64 20 69 73 20 74 68 65 20 66 69 72 73  ized is the firs
186e0 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 2e 20  t.** parameter. 
186f0 20 49 66 20 74 68 65 20 66 69 72 73 74 20 70 61   If the first pa
18700 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
18710 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a 2a 2a 20  e3_realloc().** 
18720 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  is a NULL pointe
18730 72 20 74 68 65 6e 20 69 74 73 20 62 65 68 61 76  r then its behav
18740 69 6f 72 20 69 73 20 69 64 65 6e 74 69 63 61 6c  ior is identical
18750 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73   to calling.** s
18760 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 4e 29  qlite3_malloc(N)
18770 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20   where N is the 
18780 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
18790 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 6c   to sqlite3_real
187a0 6c 6f 63 28 29 2e 0a 2a 2a 20 49 66 20 74 68 65  loc()..** If the
187b0 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
187c0 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61  r to sqlite3_rea
187d0 6c 6c 6f 63 28 29 20 69 73 20 7a 65 72 6f 20 6f  lloc() is zero o
187e0 72 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 74 68  r.** negative th
187f0 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 20  en the behavior 
18800 69 73 20 65 78 61 63 74 6c 79 20 74 68 65 20 73  is exactly the s
18810 61 6d 65 20 61 73 20 63 61 6c 6c 69 6e 67 0a 2a  ame as calling.*
18820 2a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 50  * sqlite3_free(P
18830 29 20 77 68 65 72 65 20 50 20 69 73 20 74 68 65  ) where P is the
18840 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72   first parameter
18850 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 6c   to sqlite3_real
18860 6c 6f 63 28 29 2e 0a 2a 2a 20 73 71 6c 69 74 65  loc()..** sqlite
18870 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65 74 75  3_realloc() retu
18880 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  rns a pointer to
18890 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
188a0 74 69 6f 6e 0a 2a 2a 20 6f 66 20 61 74 20 6c 65  tion.** of at le
188b0 61 73 74 20 4e 20 62 79 74 65 73 20 69 6e 20 73  ast N bytes in s
188c0 69 7a 65 20 6f 72 20 4e 55 4c 4c 20 69 66 20 73  ize or NULL if s
188d0 75 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f 72 79  ufficient memory
188e0 20 69 73 20 75 6e 61 76 61 69 6c 61 62 6c 65 2e   is unavailable.
188f0 0a 2a 2a 20 49 66 20 4d 20 69 73 20 74 68 65 20  .** If M is the 
18900 73 69 7a 65 20 6f 66 20 74 68 65 20 70 72 69 6f  size of the prio
18910 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 74 68  r allocation, th
18920 65 6e 20 6d 69 6e 28 4e 2c 4d 29 20 62 79 74 65  en min(N,M) byte
18930 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70 72 69 6f  s.** of the prio
18940 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 72 65  r allocation are
18950 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65   copied into the
18960 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 62 75   beginning of bu
18970 66 66 65 72 20 72 65 74 75 72 6e 65 64 0a 2a 2a  ffer returned.**
18980 20 62 79 20 73 71 6c 69 74 65 33 5f 72 65 61 6c   by sqlite3_real
18990 6c 6f 63 28 29 20 61 6e 64 20 74 68 65 20 70 72  loc() and the pr
189a0 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  ior allocation i
189b0 73 20 66 72 65 65 64 2e 0a 2a 2a 20 49 66 20 73  s freed..** If s
189c0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29  qlite3_realloc()
189d0 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2c 20 74   returns NULL, t
189e0 68 65 6e 20 74 68 65 20 70 72 69 6f 72 20 61 6c  hen the prior al
189f0 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 6e  location.** is n
18a00 6f 74 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20  ot freed..**.** 
18a10 54 68 65 20 6d 65 6d 6f 72 79 20 72 65 74 75 72  The memory retur
18a20 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6d  ned by sqlite3_m
18a30 61 6c 6c 6f 63 28 29 20 61 6e 64 20 73 71 6c 69  alloc() and sqli
18a40 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a 2a 2a  te3_realloc().**
18a50 20 69 73 20 61 6c 77 61 79 73 20 61 6c 69 67 6e   is always align
18a60 65 64 20 74 6f 20 61 74 20 6c 65 61 73 74 20 61  ed to at least a
18a70 6e 20 38 20 62 79 74 65 20 62 6f 75 6e 64 61 72  n 8 byte boundar
18a80 79 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54  y. {END}.**.** T
18a90 68 65 20 64 65 66 61 75 6c 74 20 69 6d 70 6c 65  he default imple
18aa0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
18ab0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
18ac0 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 75 73 65  on subsystem use
18ad0 73 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f 63 28  s.** the malloc(
18ae0 29 2c 20 72 65 61 6c 6c 6f 63 28 29 20 61 6e 64  ), realloc() and
18af0 20 66 72 65 65 28 29 20 70 72 6f 76 69 64 65 64   free() provided
18b00 20 62 79 20 74 68 65 20 73 74 61 6e 64 61 72 64   by the standard
18b10 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 20 7b   C library..** {
18b20 48 31 37 33 38 32 7d 20 48 6f 77 65 76 65 72 2c  H17382} However,
18b30 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f   if SQLite is co
18b40 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a  mpiled with the.
18b50 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59  ** SQLITE_MEMORY
18b60 5f 53 49 5a 45 3d 3c 69 3e 4e 4e 4e 3c 2f 69 3e  _SIZE=<i>NNN</i>
18b70 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f 72 20   C preprocessor 
18b80 6d 61 63 72 6f 20 28 77 68 65 72 65 20 3c 69 3e  macro (where <i>
18b90 4e 4e 4e 3c 2f 69 3e 0a 2a 2a 20 69 73 20 61 6e  NNN</i>.** is an
18ba0 20 69 6e 74 65 67 65 72 29 2c 20 74 68 65 6e 20   integer), then 
18bb0 53 51 4c 69 74 65 20 63 72 65 61 74 65 20 61 20  SQLite create a 
18bc0 73 74 61 74 69 63 20 61 72 72 61 79 20 6f 66 20  static array of 
18bd0 61 74 20 6c 65 61 73 74 0a 2a 2a 20 3c 69 3e 4e  at least.** <i>N
18be0 4e 4e 3c 2f 69 3e 20 62 79 74 65 73 20 69 6e 20  NN</i> bytes in 
18bf0 73 69 7a 65 20 61 6e 64 20 75 73 65 73 20 74 68  size and uses th
18c00 61 74 20 61 72 72 61 79 20 66 6f 72 20 61 6c 6c  at array for all
18c10 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69 63 0a   of its dynamic.
18c20 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ** memory alloca
18c30 74 69 6f 6e 20 6e 65 65 64 73 2e 20 7b 45 4e 44  tion needs. {END
18c40 7d 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 6d 65  }  Additional me
18c50 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 6f  mory allocator o
18c60 70 74 69 6f 6e 73 0a 2a 2a 20 6d 61 79 20 62 65  ptions.** may be
18c70 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65   added in future
18c80 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a   releases..**.**
18c90 20 49 6e 20 53 51 4c 69 74 65 20 76 65 72 73 69   In SQLite versi
18ca0 6f 6e 20 33 2e 35 2e 30 20 61 6e 64 20 33 2e 35  on 3.5.0 and 3.5
18cb0 2e 31 2c 20 69 74 20 77 61 73 20 70 6f 73 73 69  .1, it was possi
18cc0 62 6c 65 20 74 6f 20 64 65 66 69 6e 65 0a 2a 2a  ble to define.**
18cd0 20 74 68 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54   the SQLITE_OMIT
18ce0 5f 4d 45 4d 4f 52 59 5f 41 4c 4c 4f 43 41 54 49  _MEMORY_ALLOCATI
18cf0 4f 4e 20 77 68 69 63 68 20 77 6f 75 6c 64 20 63  ON which would c
18d00 61 75 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69  ause the built-i
18d10 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  n.** implementat
18d20 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 72 6f 75  ion of these rou
18d30 74 69 6e 65 73 20 74 6f 20 62 65 20 6f 6d 69 74  tines to be omit
18d40 74 65 64 2e 20 20 54 68 61 74 20 63 61 70 61 62  ted.  That capab
18d50 69 6c 69 74 79 0a 2a 2a 20 69 73 20 6e 6f 20 6c  ility.** is no l
18d60 6f 6e 67 65 72 20 70 72 6f 76 69 64 65 64 2e 20  onger provided. 
18d70 20 4f 6e 6c 79 20 62 75 69 6c 74 2d 69 6e 20 6d   Only built-in m
18d80 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 73  emory allocators
18d90 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a 2a 2a   can be used..**
18da0 0a 2a 2a 20 54 68 65 20 57 69 6e 64 6f 77 73 20  .** The Windows 
18db0 4f 53 20 69 6e 74 65 72 66 61 63 65 20 6c 61 79  OS interface lay
18dc0 65 72 20 63 61 6c 6c 73 0a 2a 2a 20 74 68 65 20  er calls.** the 
18dd0 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 20  system malloc() 
18de0 61 6e 64 20 66 72 65 65 28 29 20 64 69 72 65 63  and free() direc
18df0 74 6c 79 20 77 68 65 6e 20 63 6f 6e 76 65 72 74  tly when convert
18e00 69 6e 67 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 73  ing.** filenames
18e10 20 62 65 74 77 65 65 6e 20 74 68 65 20 55 54 46   between the UTF
18e20 2d 38 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64  -8 encoding used
18e30 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a 20 61 6e   by SQLite.** an
18e40 64 20 77 68 61 74 65 76 65 72 20 66 69 6c 65 6e  d whatever filen
18e50 61 6d 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20  ame encoding is 
18e60 75 73 65 64 20 62 79 20 74 68 65 20 70 61 72 74  used by the part
18e70 69 63 75 6c 61 72 20 57 69 6e 64 6f 77 73 0a 2a  icular Windows.*
18e80 2a 20 69 6e 73 74 61 6c 6c 61 74 69 6f 6e 2e 20  * installation. 
18e90 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   Memory allocati
18ea0 6f 6e 20 65 72 72 6f 72 73 20 61 72 65 20 64 65  on errors are de
18eb0 74 65 63 74 65 64 2c 20 62 75 74 0a 2a 2a 20 74  tected, but.** t
18ec0 68 65 79 20 61 72 65 20 72 65 70 6f 72 74 65 64  hey are reported
18ed0 20 62 61 63 6b 20 61 73 20 5b 53 51 4c 49 54 45   back as [SQLITE
18ee0 5f 43 41 4e 54 4f 50 45 4e 5d 20 6f 72 0a 2a 2a  _CANTOPEN] or.**
18ef0 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5d 20   [SQLITE_IOERR] 
18f00 72 61 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c  rather than [SQL
18f10 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a 2a 0a 2a  ITE_NOMEM]..**.*
18f20 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
18f30 2a 2a 20 5b 48 31 37 33 30 33 5d 20 5b 48 31 37  ** [H17303] [H17
18f40 33 30 34 5d 20 5b 48 31 37 33 30 35 5d 20 5b 48  304] [H17305] [H
18f50 31 37 33 30 36 5d 20 5b 48 31 37 33 31 30 5d 20  17306] [H17310] 
18f60 5b 48 31 37 33 31 32 5d 20 5b 48 31 37 33 31 35  [H17312] [H17315
18f70 5d 20 5b 48 31 37 33 31 38 5d 0a 2a 2a 20 5b 48  ] [H17318].** [H
18f80 31 37 33 32 31 5d 20 5b 48 31 37 33 32 32 5d 20  17321] [H17322] 
18f90 5b 48 31 37 33 32 33 5d 0a 2a 2a 0a 2a 2a 20 54  [H17323].**.** T
18fa0 68 65 20 70 6f 69 6e 74 65 72 20 61 72 67 75 6d  he pointer argum
18fb0 65 6e 74 73 20 74 6f 20 5b 73 71 6c 69 74 65 33  ents to [sqlite3
18fc0 5f 66 72 65 65 28 29 5d 20 61 6e 64 20 5b 73 71  _free()] and [sq
18fd0 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d  lite3_realloc()]
18fe0 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68  .** must be eith
18ff0 65 72 20 4e 55 4c 4c 20 6f 72 20 65 6c 73 65 20  er NULL or else 
19000 70 6f 69 6e 74 65 72 73 20 6f 62 74 61 69 6e 65  pointers obtaine
19010 64 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 0a 2a  d from a prior.*
19020 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20  * invocation of 
19030 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
19040 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72  )] or [sqlite3_r
19050 65 61 6c 6c 6f 63 28 29 5d 20 74 68 61 74 20 68  ealloc()] that h
19060 61 76 65 0a 2a 2a 20 6e 6f 74 20 79 65 74 20 62  ave.** not yet b
19070 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a  een released..**
19080 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74  .** The applicat
19090 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 72 65 61  ion must not rea
190a0 64 20 6f 72 20 77 72 69 74 65 20 61 6e 79 20 70  d or write any p
190b0 61 72 74 20 6f 66 0a 2a 2a 20 61 20 62 6c 6f 63  art of.** a bloc
190c0 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 61 66 74 65  k of memory afte
190d0 72 20 69 74 20 68 61 73 20 62 65 65 6e 20 72 65  r it has been re
190e0 6c 65 61 73 65 64 20 75 73 69 6e 67 0a 2a 2a 20  leased using.** 
190f0 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d  [sqlite3_free()]
19100 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 61   or [sqlite3_rea
19110 6c 6c 6f 63 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49  lloc()]..*/.SQLI
19120 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
19130 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74 29  ite3_malloc(int)
19140 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ;.SQLITE_API voi
19150 64 20 2a 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  d *sqlite3_reall
19160 6f 63 28 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a  oc(void*, int);.
19170 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
19180 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76 6f 69  sqlite3_free(voi
19190 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  d*);../*.** CAPI
191a0 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c  3REF: Memory All
191b0 6f 63 61 74 6f 72 20 53 74 61 74 69 73 74 69 63  ocator Statistic
191c0 73 20 7b 48 31 37 33 37 30 7d 20 3c 53 33 30 32  s {H17370} <S302
191d0 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  10>.**.** SQLite
191e0 20 70 72 6f 76 69 64 65 73 20 74 68 65 73 65 20   provides these 
191f0 74 77 6f 20 69 6e 74 65 72 66 61 63 65 73 20 66  two interfaces f
19200 6f 72 20 72 65 70 6f 72 74 69 6e 67 20 6f 6e 20  or reporting on 
19210 74 68 65 20 73 74 61 74 75 73 0a 2a 2a 20 6f 66  the status.** of
19220 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 61   the [sqlite3_ma
19230 6c 6c 6f 63 28 29 5d 2c 20 5b 73 71 6c 69 74 65  lloc()], [sqlite
19240 33 5f 66 72 65 65 28 29 5d 2c 20 61 6e 64 20 5b  3_free()], and [
19250 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
19260 29 5d 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 2c 20  )].** routines, 
19270 77 68 69 63 68 20 66 6f 72 6d 20 74 68 65 20 62  which form the b
19280 75 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 20 61  uilt-in memory a
19290 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
192a0 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  tem..**.** Requi
192b0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37  rements:.** [H17
192c0 33 37 31 5d 20 5b 48 31 37 33 37 33 5d 20 5b 48  371] [H17373] [H
192d0 31 37 33 37 34 5d 20 5b 48 31 37 33 37 35 5d 0a  17374] [H17375].
192e0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  */.SQLITE_API sq
192f0 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69  lite3_int64 sqli
19300 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28  te3_memory_used(
19310 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50  void);.SQLITE_AP
19320 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  I sqlite3_int64 
19330 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68  sqlite3_memory_h
19340 69 67 68 77 61 74 65 72 28 69 6e 74 20 72 65 73  ighwater(int res
19350 65 74 46 6c 61 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20  etFlag);../*.** 
19360 43 41 50 49 33 52 45 46 3a 20 50 73 65 75 64 6f  CAPI3REF: Pseudo
19370 2d 52 61 6e 64 6f 6d 20 4e 75 6d 62 65 72 20 47  -Random Number G
19380 65 6e 65 72 61 74 6f 72 20 7b 48 31 37 33 39 30  enerator {H17390
19390 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a  } <S20000>.**.**
193a0 20 53 51 4c 69 74 65 20 63 6f 6e 74 61 69 6e 73   SQLite contains
193b0 20 61 20 68 69 67 68 2d 71 75 61 6c 69 74 79 20   a high-quality 
193c0 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75  pseudo-random nu
193d0 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 28  mber generator (
193e0 50 52 4e 47 29 20 75 73 65 64 20 74 6f 0a 2a 2a  PRNG) used to.**
193f0 20 73 65 6c 65 63 74 20 72 61 6e 64 6f 6d 20 5b   select random [
19400 52 4f 57 49 44 20 7c 20 52 4f 57 49 44 73 5d 20  ROWID | ROWIDs] 
19410 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 6e  when inserting n
19420 65 77 20 72 65 63 6f 72 64 73 20 69 6e 74 6f 20  ew records into 
19430 61 20 74 61 62 6c 65 20 74 68 61 74 0a 2a 2a 20  a table that.** 
19440 61 6c 72 65 61 64 79 20 75 73 65 73 20 74 68 65  already uses the
19450 20 6c 61 72 67 65 73 74 20 70 6f 73 73 69 62 6c   largest possibl
19460 65 20 5b 52 4f 57 49 44 5d 2e 20 20 54 68 65 20  e [ROWID].  The 
19470 50 52 4e 47 20 69 73 20 61 6c 73 6f 20 75 73 65  PRNG is also use
19480 64 20 66 6f 72 0a 2a 2a 20 74 68 65 20 62 75 69  d for.** the bui
19490 6c 64 2d 69 6e 20 72 61 6e 64 6f 6d 28 29 20 61  ld-in random() a
194a0 6e 64 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 29 20  nd randomblob() 
194b0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20  SQL functions.  
194c0 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 61  This interface a
194d0 6c 6c 6f 77 73 0a 2a 2a 20 61 70 70 6c 69 63 61  llows.** applica
194e0 74 69 6f 6e 73 20 74 6f 20 61 63 63 65 73 73 20  tions to access 
194f0 74 68 65 20 73 61 6d 65 20 50 52 4e 47 20 66 6f  the same PRNG fo
19500 72 20 6f 74 68 65 72 20 70 75 72 70 6f 73 65 73  r other purposes
19510 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74  ..**.** A call t
19520 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73  o this routine s
19530 74 6f 72 65 73 20 4e 20 62 79 74 65 73 20 6f 66  tores N bytes of
19540 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f   randomness into
19550 20 62 75 66 66 65 72 20 50 2e 0a 2a 2a 0a 2a 2a   buffer P..**.**
19560 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20   The first time 
19570 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
19580 69 6e 76 6f 6b 65 64 20 28 65 69 74 68 65 72 20  invoked (either 
19590 69 6e 74 65 72 6e 61 6c 6c 79 20 6f 72 20 62 79  internally or by
195a0 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74  .** the applicat
195b0 69 6f 6e 29 20 74 68 65 20 50 52 4e 47 20 69 73  ion) the PRNG is
195c0 20 73 65 65 64 65 64 20 75 73 69 6e 67 20 72 61   seeded using ra
195d0 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61 69 6e 65  ndomness obtaine
195e0 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 78 52  d.** from the xR
195f0 61 6e 64 6f 6d 6e 65 73 73 20 6d 65 74 68 6f 64  andomness method
19600 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   of the default 
19610 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62  [sqlite3_vfs] ob
19620 6a 65 63 74 2e 0a 2a 2a 20 4f 6e 20 61 6c 6c 20  ject..** On all 
19630 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f 63  subsequent invoc
19640 61 74 69 6f 6e 73 2c 20 74 68 65 20 70 73 65 75  ations, the pseu
19650 64 6f 2d 72 61 6e 64 6f 6d 6e 65 73 73 20 69 73  do-randomness is
19660 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 69 6e   generated.** in
19670 74 65 72 6e 61 6c 6c 79 20 61 6e 64 20 77 69 74  ternally and wit
19680 68 6f 75 74 20 72 65 63 6f 75 72 73 65 20 74 6f  hout recourse to
19690 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66   the [sqlite3_vf
196a0 73 5d 20 78 52 61 6e 64 6f 6d 6e 65 73 73 0a 2a  s] xRandomness.*
196b0 2a 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20  * method..**.** 
196c0 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
196d0 20 5b 48 31 37 33 39 32 5d 0a 2a 2f 0a 53 51 4c   [H17392].*/.SQL
196e0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
196f0 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28  ite3_randomness(
19700 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a 50 29 3b  int N, void *P);
19710 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
19720 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65 20 41  : Compile-Time A
19730 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 43 61 6c  uthorization Cal
19740 6c 62 61 63 6b 73 20 7b 48 31 32 35 30 30 7d 20  lbacks {H12500} 
19750 3c 53 37 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S70100>.**.** T
19760 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 67 69  his routine regi
19770 73 74 65 72 73 20 61 20 61 75 74 68 6f 72 69 7a  sters a authoriz
19780 65 72 20 63 61 6c 6c 62 61 63 6b 20 77 69 74 68  er callback with
19790 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a   a particular.**
197a0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
197b0 63 74 69 6f 6e 5d 2c 20 73 75 70 70 6c 69 65 64  ction], supplied
197c0 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72   in the first ar
197d0 67 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 61  gument..** The a
197e0 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61  uthorizer callba
197f0 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 73  ck is invoked as
19800 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20   SQL statements 
19810 61 72 65 20 62 65 69 6e 67 20 63 6f 6d 70 69 6c  are being compil
19820 65 64 0a 2a 2a 20 62 79 20 5b 73 71 6c 69 74 65  ed.** by [sqlite
19830 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f 72 20  3_prepare()] or 
19840 69 74 73 20 76 61 72 69 61 6e 74 73 20 5b 73 71  its variants [sq
19850 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
19860 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  ()],.** [sqlite3
19870 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20 61 6e  _prepare16()] an
19880 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  d [sqlite3_prepa
19890 72 65 31 36 5f 76 32 28 29 5d 2e 20 20 41 74 20  re16_v2()].  At 
198a0 76 61 72 69 6f 75 73 0a 2a 2a 20 70 6f 69 6e 74  various.** point
198b0 73 20 64 75 72 69 6e 67 20 74 68 65 20 63 6f 6d  s during the com
198c0 70 69 6c 61 74 69 6f 6e 20 70 72 6f 63 65 73 73  pilation process
198d0 2c 20 61 73 20 6c 6f 67 69 63 20 69 73 20 62 65  , as logic is be
198e0 69 6e 67 20 63 72 65 61 74 65 64 0a 2a 2a 20 74  ing created.** t
198f0 6f 20 70 65 72 66 6f 72 6d 20 76 61 72 69 6f 75  o perform variou
19900 73 20 61 63 74 69 6f 6e 73 2c 20 74 68 65 20 61  s actions, the a
19910 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61  uthorizer callba
19920 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f  ck is invoked to
19930 0a 2a 2a 20 73 65 65 20 69 66 20 74 68 6f 73 65  .** see if those
19940 20 61 63 74 69 6f 6e 73 20 61 72 65 20 61 6c 6c   actions are all
19950 6f 77 65 64 2e 20 20 54 68 65 20 61 75 74 68 6f  owed.  The autho
19960 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 73  rizer callback s
19970 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 72 6e 20  hould.** return 
19980 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 74 6f 20 61  [SQLITE_OK] to a
19990 6c 6c 6f 77 20 74 68 65 20 61 63 74 69 6f 6e 2c  llow the action,
199a0 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d   [SQLITE_IGNORE]
199b0 20 74 6f 20 64 69 73 61 6c 6c 6f 77 20 74 68 65   to disallow the
199c0 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 61 63 74  .** specific act
199d0 69 6f 6e 20 62 75 74 20 61 6c 6c 6f 77 20 74 68  ion but allow th
199e0 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
199f0 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 62  to continue to b
19a00 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 2c 20 6f  e.** compiled, o
19a10 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d 20  r [SQLITE_DENY] 
19a20 74 6f 20 63 61 75 73 65 20 74 68 65 20 65 6e 74  to cause the ent
19a30 69 72 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ire SQL statemen
19a40 74 20 74 6f 20 62 65 0a 2a 2a 20 72 65 6a 65 63  t to be.** rejec
19a50 74 65 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f  ted with an erro
19a60 72 2e 20 20 49 66 20 74 68 65 20 61 75 74 68 6f  r.  If the autho
19a70 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 72  rizer callback r
19a80 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 79 20 76 61  eturns.** any va
19a90 6c 75 65 20 6f 74 68 65 72 20 74 68 61 6e 20 5b  lue other than [
19aa0 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 2c 20  SQLITE_IGNORE], 
19ab0 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 6f 72 20  [SQLITE_OK], or 
19ac0 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d 0a 2a 2a  [SQLITE_DENY].**
19ad0 20 74 68 65 6e 20 74 68 65 20 5b 73 71 6c 69 74   then the [sqlit
19ae0 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
19af0 20 6f 72 20 65 71 75 69 76 61 6c 65 6e 74 20 63   or equivalent c
19b00 61 6c 6c 20 74 68 61 74 20 74 72 69 67 67 65 72  all that trigger
19b10 65 64 0a 2a 2a 20 74 68 65 20 61 75 74 68 6f 72  ed.** the author
19b20 69 7a 65 72 20 77 69 6c 6c 20 66 61 69 6c 20 77  izer will fail w
19b30 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ith an error mes
19b40 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  sage..**.** When
19b50 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65   the callback re
19b60 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b  turns [SQLITE_OK
19b70 5d 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  ], that means th
19b80 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 72  e operation.** r
19b90 65 71 75 65 73 74 65 64 20 69 73 20 6f 6b 2e 20  equested is ok. 
19ba0 20 57 68 65 6e 20 74 68 65 20 63 61 6c 6c 62 61   When the callba
19bb0 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49  ck returns [SQLI
19bc0 54 45 5f 44 45 4e 59 5d 2c 20 74 68 65 0a 2a 2a  TE_DENY], the.**
19bd0 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
19be0 65 5f 76 32 28 29 5d 20 6f 72 20 65 71 75 69 76  e_v2()] or equiv
19bf0 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68 61 74 20  alent call that 
19c00 74 72 69 67 67 65 72 65 64 20 74 68 65 0a 2a 2a  triggered the.**
19c10 20 61 75 74 68 6f 72 69 7a 65 72 20 77 69 6c 6c   authorizer will
19c20 20 66 61 69 6c 20 77 69 74 68 20 61 6e 20 65 72   fail with an er
19c30 72 6f 72 20 6d 65 73 73 61 67 65 20 65 78 70 6c  ror message expl
19c40 61 69 6e 69 6e 67 20 74 68 61 74 0a 2a 2a 20 61  aining that.** a
19c50 63 63 65 73 73 20 69 73 20 64 65 6e 69 65 64 2e  ccess is denied.
19c60 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73   .**.** The firs
19c70 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  t parameter to t
19c80 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61  he authorizer ca
19c90 6c 6c 62 61 63 6b 20 69 73 20 61 20 63 6f 70 79  llback is a copy
19ca0 20 6f 66 20 74 68 65 20 74 68 69 72 64 0a 2a 2a   of the third.**
19cb0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68   parameter to th
19cc0 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75  e sqlite3_set_au
19cd0 74 68 6f 72 69 7a 65 72 28 29 20 69 6e 74 65 72  thorizer() inter
19ce0 66 61 63 65 2e 20 54 68 65 20 73 65 63 6f 6e 64  face. The second
19cf0 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 6f   parameter.** to
19d00 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73   the callback is
19d10 20 61 6e 20 69 6e 74 65 67 65 72 20 5b 53 51 4c   an integer [SQL
19d20 49 54 45 5f 43 4f 50 59 20 7c 20 61 63 74 69 6f  ITE_COPY | actio
19d30 6e 20 63 6f 64 65 5d 20 74 68 61 74 20 73 70 65  n code] that spe
19d40 63 69 66 69 65 73 0a 2a 2a 20 74 68 65 20 70 61  cifies.** the pa
19d50 72 74 69 63 75 6c 61 72 20 61 63 74 69 6f 6e 20  rticular action 
19d60 74 6f 20 62 65 20 61 75 74 68 6f 72 69 7a 65 64  to be authorized
19d70 2e 20 54 68 65 20 74 68 69 72 64 20 74 68 72 6f  . The third thro
19d80 75 67 68 20 73 69 78 74 68 20 70 61 72 61 6d 65  ugh sixth parame
19d90 74 65 72 73 0a 2a 2a 20 74 6f 20 74 68 65 20 63  ters.** to the c
19da0 61 6c 6c 62 61 63 6b 20 61 72 65 20 7a 65 72 6f  allback are zero
19db0 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69  -terminated stri
19dc0 6e 67 73 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ngs that contain
19dd0 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 64   additional.** d
19de0 65 74 61 69 6c 73 20 61 62 6f 75 74 20 74 68 65  etails about the
19df0 20 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 75   action to be au
19e00 74 68 6f 72 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20  thorized..**.** 
19e10 49 66 20 74 68 65 20 61 63 74 69 6f 6e 20 63 6f  If the action co
19e20 64 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 52 45  de is [SQLITE_RE
19e30 41 44 5d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63  AD].** and the c
19e40 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20  allback returns 
19e50 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20  [SQLITE_IGNORE] 
19e60 74 68 65 6e 20 74 68 65 0a 2a 2a 20 5b 70 72 65  then the.** [pre
19e70 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
19e80 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 63 6f   statement is co
19e90 6e 73 74 72 75 63 74 65 64 20 74 6f 20 73 75 62  nstructed to sub
19ea0 73 74 69 74 75 74 65 0a 2a 2a 20 61 20 4e 55 4c  stitute.** a NUL
19eb0 4c 20 76 61 6c 75 65 20 69 6e 20 70 6c 61 63 65  L value in place
19ec0 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f   of the table co
19ed0 6c 75 6d 6e 20 74 68 61 74 20 77 6f 75 6c 64 20  lumn that would 
19ee0 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20 72 65 61  have.** been rea
19ef0 64 20 69 66 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d  d if [SQLITE_OK]
19f00 20 68 61 64 20 62 65 65 6e 20 72 65 74 75 72 6e   had been return
19f10 65 64 2e 20 20 54 68 65 20 5b 53 51 4c 49 54 45  ed.  The [SQLITE
19f20 5f 49 47 4e 4f 52 45 5d 0a 2a 2a 20 72 65 74 75  _IGNORE].** retu
19f30 72 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  rn can be used t
19f40 6f 20 64 65 6e 79 20 61 6e 20 75 6e 74 72 75 73  o deny an untrus
19f50 74 65 64 20 75 73 65 72 20 61 63 63 65 73 73 20  ted user access 
19f60 74 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a  to individual.**
19f70 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 61 20 74 61   columns of a ta
19f80 62 6c 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20 61  ble..** If the a
19f90 63 74 69 6f 6e 20 63 6f 64 65 20 69 73 20 5b 53  ction code is [S
19fa0 51 4c 49 54 45 5f 44 45 4c 45 54 45 5d 20 61 6e  QLITE_DELETE] an
19fb0 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72  d the callback r
19fc0 65 74 75 72 6e 73 0a 2a 2a 20 5b 53 51 4c 49 54  eturns.** [SQLIT
19fd0 45 5f 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20 74  E_IGNORE] then t
19fe0 68 65 20 5b 44 45 4c 45 54 45 5d 20 6f 70 65 72  he [DELETE] oper
19ff0 61 74 69 6f 6e 20 70 72 6f 63 65 65 64 73 20 62  ation proceeds b
1a000 75 74 20 74 68 65 0a 2a 2a 20 5b 74 72 75 6e 63  ut the.** [trunc
1a010 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ate optimization
1a020 5d 20 69 73 20 64 69 73 61 62 6c 65 64 20 61 6e  ] is disabled an
1a030 64 20 61 6c 6c 20 72 6f 77 73 20 61 72 65 20 64  d all rows are d
1a040 65 6c 65 74 65 64 20 69 6e 64 69 76 69 64 75 61  eleted individua
1a050 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 75  lly..**.** An au
1a060 74 68 6f 72 69 7a 65 72 20 69 73 20 75 73 65 64  thorizer is used
1a070 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70   when [sqlite3_p
1a080 72 65 70 61 72 65 20 7c 20 70 72 65 70 61 72 69  repare | prepari
1a090 6e 67 5d 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65  ng].** SQL state
1a0a0 6d 65 6e 74 73 20 66 72 6f 6d 20 61 6e 20 75 6e  ments from an un
1a0b0 74 72 75 73 74 65 64 20 73 6f 75 72 63 65 2c 20  trusted source, 
1a0c0 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74  to ensure that t
1a0d0 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
1a0e0 73 0a 2a 2a 20 64 6f 20 6e 6f 74 20 74 72 79 20  s.** do not try 
1a0f0 74 6f 20 61 63 63 65 73 73 20 64 61 74 61 20 74  to access data t
1a100 68 65 79 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f  hey are not allo
1a110 77 65 64 20 74 6f 20 73 65 65 2c 20 6f 72 20 74  wed to see, or t
1a120 68 61 74 20 74 68 65 79 20 64 6f 20 6e 6f 74 0a  hat they do not.
1a130 2a 2a 20 74 72 79 20 74 6f 20 65 78 65 63 75 74  ** try to execut
1a140 65 20 6d 61 6c 69 63 69 6f 75 73 20 73 74 61 74  e malicious stat
1a150 65 6d 65 6e 74 73 20 74 68 61 74 20 64 61 6d 61  ements that dama
1a160 67 65 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  ge the database.
1a170 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65    For.** example
1a180 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e  , an application
1a190 20 6d 61 79 20 61 6c 6c 6f 77 20 61 20 75 73 65   may allow a use
1a1a0 72 20 74 6f 20 65 6e 74 65 72 20 61 72 62 69 74  r to enter arbit
1a1b0 72 61 72 79 0a 2a 2a 20 53 51 4c 20 71 75 65 72  rary.** SQL quer
1a1c0 69 65 73 20 66 6f 72 20 65 76 61 6c 75 61 74 69  ies for evaluati
1a1d0 6f 6e 20 62 79 20 61 20 64 61 74 61 62 61 73 65  on by a database
1a1e0 2e 20 20 42 75 74 20 74 68 65 20 61 70 70 6c 69  .  But the appli
1a1f0 63 61 74 69 6f 6e 20 64 6f 65 73 0a 2a 2a 20 6e  cation does.** n
1a200 6f 74 20 77 61 6e 74 20 74 68 65 20 75 73 65 72  ot want the user
1a210 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 6d   to be able to m
1a220 61 6b 65 20 61 72 62 69 74 72 61 72 79 20 63 68  ake arbitrary ch
1a230 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20  anges to the.** 
1a240 64 61 74 61 62 61 73 65 2e 20 20 41 6e 20 61 75  database.  An au
1a250 74 68 6f 72 69 7a 65 72 20 63 6f 75 6c 64 20 74  thorizer could t
1a260 68 65 6e 20 62 65 20 70 75 74 20 69 6e 20 70 6c  hen be put in pl
1a270 61 63 65 20 77 68 69 6c 65 20 74 68 65 0a 2a 2a  ace while the.**
1a280 20 75 73 65 72 2d 65 6e 74 65 72 65 64 20 53 51   user-entered SQ
1a290 4c 20 69 73 20 62 65 69 6e 67 20 5b 73 71 6c 69  L is being [sqli
1a2a0 74 65 33 5f 70 72 65 70 61 72 65 20 7c 20 70 72  te3_prepare | pr
1a2b0 65 70 61 72 65 64 5d 20 74 68 61 74 0a 2a 2a 20  epared] that.** 
1a2c0 64 69 73 61 6c 6c 6f 77 73 20 65 76 65 72 79 74  disallows everyt
1a2d0 68 69 6e 67 20 65 78 63 65 70 74 20 5b 53 45 4c  hing except [SEL
1a2e0 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 73 2e  ECT] statements.
1a2f0 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69  .**.** Applicati
1a300 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f  ons that need to
1a310 20 70 72 6f 63 65 73 73 20 53 51 4c 20 66 72 6f   process SQL fro
1a320 6d 20 75 6e 74 72 75 73 74 65 64 20 73 6f 75 72  m untrusted sour
1a330 63 65 73 0a 2a 2a 20 6d 69 67 68 74 20 61 6c 73  ces.** might als
1a340 6f 20 63 6f 6e 73 69 64 65 72 20 6c 6f 77 65 72  o consider lower
1a350 69 6e 67 20 72 65 73 6f 75 72 63 65 20 6c 69 6d  ing resource lim
1a360 69 74 73 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  its using [sqlit
1a370 65 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 61  e3_limit()].** a
1a380 6e 64 20 6c 69 6d 69 74 69 6e 67 20 64 61 74 61  nd limiting data
1a390 62 61 73 65 20 73 69 7a 65 20 75 73 69 6e 67 20  base size using 
1a3a0 74 68 65 20 5b 6d 61 78 5f 70 61 67 65 5f 63 6f  the [max_page_co
1a3b0 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 0a 2a 2a  unt] [PRAGMA].**
1a3c0 20 69 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f 20   in addition to 
1a3d0 75 73 69 6e 67 20 61 6e 20 61 75 74 68 6f 72 69  using an authori
1a3e0 7a 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20  zer..**.** Only 
1a3f0 61 20 73 69 6e 67 6c 65 20 61 75 74 68 6f 72 69  a single authori
1a400 7a 65 72 20 63 61 6e 20 62 65 20 69 6e 20 70 6c  zer can be in pl
1a410 61 63 65 20 6f 6e 20 61 20 64 61 74 61 62 61 73  ace on a databas
1a420 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20  e connection.** 
1a430 61 74 20 61 20 74 69 6d 65 2e 20 20 45 61 63 68  at a time.  Each
1a440 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
1a450 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20  _set_authorizer 
1a460 6f 76 65 72 72 69 64 65 73 20 74 68 65 0a 2a 2a  overrides the.**
1a470 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 2e 20   previous call. 
1a480 20 44 69 73 61 62 6c 65 20 74 68 65 20 61 75 74   Disable the aut
1a490 68 6f 72 69 7a 65 72 20 62 79 20 69 6e 73 74 61  horizer by insta
1a4a0 6c 6c 69 6e 67 20 61 20 4e 55 4c 4c 20 63 61 6c  lling a NULL cal
1a4b0 6c 62 61 63 6b 2e 0a 2a 2a 20 54 68 65 20 61 75  lback..** The au
1a4c0 74 68 6f 72 69 7a 65 72 20 69 73 20 64 69 73 61  thorizer is disa
1a4d0 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e  bled by default.
1a4e0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
1a4f0 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 6d  rizer callback m
1a500 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68  ust not do anyth
1a510 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f  ing that will mo
1a520 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61  dify.** the data
1a530 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
1a540 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65  that invoked the
1a550 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c   authorizer call
1a560 62 61 63 6b 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68  back..** Note th
1a570 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  at [sqlite3_prep
1a580 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73  are_v2()] and [s
1a590 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 62  qlite3_step()] b
1a5a0 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69 72  oth modify their
1a5b0 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e  .** database con
1a5c0 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 65  nections for the
1a5d0 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f 64   meaning of "mod
1a5e0 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 61 72  ify" in this par
1a5f0 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 57 68  agraph..**.** Wh
1a600 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  en [sqlite3_prep
1a610 61 72 65 5f 76 32 28 29 5d 20 69 73 20 75 73 65  are_v2()] is use
1a620 64 20 74 6f 20 70 72 65 70 61 72 65 20 61 20 73  d to prepare a s
1a630 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 0a 2a 2a  tatement, the.**
1a640 20 73 74 61 74 65 6d 65 6e 74 20 6d 69 67 68 74   statement might
1a650 20 62 65 20 72 65 2d 70 72 65 70 61 72 65 64 20   be re-prepared 
1a660 64 75 72 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f  during [sqlite3_
1a670 73 74 65 70 28 29 5d 20 64 75 65 20 74 6f 20 61  step()] due to a
1a680 20 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e   .** schema chan
1a690 67 65 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20  ge.  Hence, the 
1a6a0 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75  application shou
1a6b0 6c 64 20 65 6e 73 75 72 65 20 74 68 61 74 20 74  ld ensure that t
1a6c0 68 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 75  he.** correct au
1a6d0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
1a6e0 6b 20 72 65 6d 61 69 6e 73 20 69 6e 20 70 6c 61  k remains in pla
1a6f0 63 65 20 64 75 72 69 6e 67 20 74 68 65 20 5b 73  ce during the [s
1a700 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 0a  qlite3_step()]..
1a710 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
1a720 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63  the authorizer c
1a730 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b  allback is invok
1a740 65 64 20 6f 6e 6c 79 20 64 75 72 69 6e 67 0a 2a  ed only during.*
1a750 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  * [sqlite3_prepa
1a760 72 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72  re()] or its var
1a770 69 61 6e 74 73 2e 20 20 41 75 74 68 6f 72 69 7a  iants.  Authoriz
1a780 61 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20  ation is not.** 
1a790 70 65 72 66 6f 72 6d 65 64 20 64 75 72 69 6e 67  performed during
1a7a0 20 73 74 61 74 65 6d 65 6e 74 20 65 76 61 6c 75   statement evalu
1a7b0 61 74 69 6f 6e 20 69 6e 20 5b 73 71 6c 69 74 65  ation in [sqlite
1a7c0 33 5f 73 74 65 70 28 29 5d 2c 20 75 6e 6c 65 73  3_step()], unles
1a7d0 73 0a 2a 2a 20 61 73 20 73 74 61 74 65 64 20 69  s.** as stated i
1a7e0 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 70  n the previous p
1a7f0 61 72 61 67 72 61 70 68 2c 20 73 71 6c 69 74 65  aragraph, sqlite
1a800 33 5f 73 74 65 70 28 29 20 69 6e 76 6f 6b 65 73  3_step() invokes
1a810 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 72 65 70  .** sqlite3_prep
1a820 61 72 65 5f 76 32 28 29 20 74 6f 20 72 65 70 72  are_v2() to repr
1a830 65 70 61 72 65 20 61 20 73 74 61 74 65 6d 65 6e  epare a statemen
1a840 74 20 61 66 74 65 72 20 61 20 73 63 68 65 6d 61  t after a schema
1a850 20 63 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 52   change..**.** R
1a860 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
1a870 5b 48 31 32 35 30 31 5d 20 5b 48 31 32 35 30 32  [H12501] [H12502
1a880 5d 20 5b 48 31 32 35 30 33 5d 20 5b 48 31 32 35  ] [H12503] [H125
1a890 30 34 5d 20 5b 48 31 32 35 30 35 5d 20 5b 48 31  04] [H12505] [H1
1a8a0 32 35 30 36 5d 20 5b 48 31 32 35 30 37 5d 20 5b  2506] [H12507] [
1a8b0 48 31 32 35 31 30 5d 0a 2a 2a 20 5b 48 31 32 35  H12510].** [H125
1a8c0 31 31 5d 20 5b 48 31 32 35 31 32 5d 20 5b 48 31  11] [H12512] [H1
1a8d0 32 35 32 30 5d 20 5b 48 31 32 35 32 31 5d 20 5b  2520] [H12521] [
1a8e0 48 31 32 35 32 32 5d 0a 2a 2f 0a 53 51 4c 49 54  H12522].*/.SQLIT
1a8f0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1a900 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72  3_set_authorizer
1a910 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 0a 20 20  (.  sqlite3*,.  
1a920 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69  int (*xAuth)(voi
1a930 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61  d*,int,const cha
1a940 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  r*,const char*,c
1a950 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
1a960 20 63 68 61 72 2a 29 2c 0a 20 20 76 6f 69 64 20   char*),.  void 
1a970 2a 70 55 73 65 72 44 61 74 61 0a 29 3b 0a 0a 2f  *pUserData.);../
1a980 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41  *.** CAPI3REF: A
1a990 75 74 68 6f 72 69 7a 65 72 20 52 65 74 75 72 6e  uthorizer Return
1a9a0 20 43 6f 64 65 73 20 7b 48 31 32 35 39 30 7d 20   Codes {H12590} 
1a9b0 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <H12500>.**.** T
1a9c0 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f  he [sqlite3_set_
1a9d0 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 75 74  authorizer | aut
1a9e0 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
1a9f0 20 66 75 6e 63 74 69 6f 6e 5d 20 6d 75 73 74 0a   function] must.
1aa00 2a 2a 20 72 65 74 75 72 6e 20 65 69 74 68 65 72  ** return either
1aa10 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 72 20   [SQLITE_OK] or 
1aa20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 74 77 6f  one of these two
1aa30 20 63 6f 6e 73 74 61 6e 74 73 20 69 6e 20 6f 72   constants in or
1aa40 64 65 72 0a 2a 2a 20 74 6f 20 73 69 67 6e 61 6c  der.** to signal
1aa50 20 53 51 4c 69 74 65 20 77 68 65 74 68 65 72 20   SQLite whether 
1aa60 6f 72 20 6e 6f 74 20 74 68 65 20 61 63 74 69 6f  or not the actio
1aa70 6e 20 69 73 20 70 65 72 6d 69 74 74 65 64 2e 20  n is permitted. 
1aa80 20 53 65 65 20 74 68 65 0a 2a 2a 20 5b 73 71 6c   See the.** [sql
1aa90 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69  ite3_set_authori
1aaa0 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65 72  zer | authorizer
1aab0 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 5d 20   documentation] 
1aac0 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a  for additional.*
1aad0 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  * information..*
1aae0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1aaf0 5f 44 45 4e 59 20 20 20 31 20 20 20 2f 2a 20 41  _DENY   1   /* A
1ab00 62 6f 72 74 20 74 68 65 20 53 51 4c 20 73 74 61  bort the SQL sta
1ab10 74 65 6d 65 6e 74 20 77 69 74 68 20 61 6e 20 65  tement with an e
1ab20 72 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20  rror */.#define 
1ab30 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 32 20  SQLITE_IGNORE 2 
1ab40 20 20 2f 2a 20 44 6f 6e 27 74 20 61 6c 6c 6f 77    /* Don't allow
1ab50 20 61 63 63 65 73 73 2c 20 62 75 74 20 64 6f 6e   access, but don
1ab60 27 74 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65  't generate an e
1ab70 72 72 6f 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43  rror */../*.** C
1ab80 41 50 49 33 52 45 46 3a 20 41 75 74 68 6f 72 69  API3REF: Authori
1ab90 7a 65 72 20 41 63 74 69 6f 6e 20 43 6f 64 65 73  zer Action Codes
1aba0 20 7b 48 31 32 35 35 30 7d 20 3c 48 31 32 35 30   {H12550} <H1250
1abb0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71  0>.**.** The [sq
1abc0 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72  lite3_set_author
1abd0 69 7a 65 72 28 29 5d 20 69 6e 74 65 72 66 61 63  izer()] interfac
1abe0 65 20 72 65 67 69 73 74 65 72 73 20 61 20 63 61  e registers a ca
1abf0 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 0a  llback function.
1ac00 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b  ** that is invok
1ac10 65 64 20 74 6f 20 61 75 74 68 6f 72 69 7a 65 20  ed to authorize 
1ac20 63 65 72 74 61 69 6e 20 53 51 4c 20 73 74 61 74  certain SQL stat
1ac30 65 6d 65 6e 74 20 61 63 74 69 6f 6e 73 2e 20 20  ement actions.  
1ac40 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61  The.** second pa
1ac50 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 63  rameter to the c
1ac60 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 69 6e  allback is an in
1ac70 74 65 67 65 72 20 63 6f 64 65 20 74 68 61 74 20  teger code that 
1ac80 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 77 68 61  specifies.** wha
1ac90 74 20 61 63 74 69 6f 6e 20 69 73 20 62 65 69 6e  t action is bein
1aca0 67 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 20 54  g authorized.  T
1acb0 68 65 73 65 20 61 72 65 20 74 68 65 20 69 6e 74  hese are the int
1acc0 65 67 65 72 20 61 63 74 69 6f 6e 20 63 6f 64 65  eger action code
1acd0 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 61 75  s that.** the au
1ace0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
1acf0 6b 20 6d 61 79 20 62 65 20 70 61 73 73 65 64 2e  k may be passed.
1ad00 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61 63 74  .**.** These act
1ad10 69 6f 6e 20 63 6f 64 65 20 76 61 6c 75 65 73 20  ion code values 
1ad20 73 69 67 6e 69 66 79 20 77 68 61 74 20 6b 69 6e  signify what kin
1ad30 64 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 20 69  d of operation i
1ad40 73 20 74 6f 20 62 65 0a 2a 2a 20 61 75 74 68 6f  s to be.** autho
1ad50 72 69 7a 65 64 2e 20 20 54 68 65 20 33 72 64 20  rized.  The 3rd 
1ad60 61 6e 64 20 34 74 68 20 70 61 72 61 6d 65 74 65  and 4th paramete
1ad70 72 73 20 74 6f 20 74 68 65 20 61 75 74 68 6f 72  rs to the author
1ad80 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6c 6c 62  ization.** callb
1ad90 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c  ack function wil
1ada0 6c 20 62 65 20 70 61 72 61 6d 65 74 65 72 73 20  l be parameters 
1adb0 6f 72 20 4e 55 4c 4c 20 64 65 70 65 6e 64 69 6e  or NULL dependin
1adc0 67 20 6f 6e 20 77 68 69 63 68 20 6f 66 20 74 68  g on which of th
1add0 65 73 65 0a 2a 2a 20 63 6f 64 65 73 20 69 73 20  ese.** codes is 
1ade0 75 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f  used as the seco
1adf0 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54  nd parameter.  T
1ae00 68 65 20 35 74 68 20 70 61 72 61 6d 65 74 65 72  he 5th parameter
1ae10 20 74 6f 20 74 68 65 0a 2a 2a 20 61 75 74 68 6f   to the.** autho
1ae20 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69  rizer callback i
1ae30 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
1ae40 65 20 64 61 74 61 62 61 73 65 20 28 22 6d 61 69  e database ("mai
1ae50 6e 22 2c 20 22 74 65 6d 70 22 2c 0a 2a 2a 20 65  n", "temp",.** e
1ae60 74 63 2e 29 20 69 66 20 61 70 70 6c 69 63 61 62  tc.) if applicab
1ae70 6c 65 2e 20 20 54 68 65 20 36 74 68 20 70 61 72  le.  The 6th par
1ae80 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 75  ameter to the au
1ae90 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
1aea0 6b 0a 2a 2a 20 69 73 20 74 68 65 20 6e 61 6d 65  k.** is the name
1aeb0 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f   of the inner-mo
1aec0 73 74 20 74 72 69 67 67 65 72 20 6f 72 20 76 69  st trigger or vi
1aed0 65 77 20 74 68 61 74 20 69 73 20 72 65 73 70 6f  ew that is respo
1aee0 6e 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 74 68  nsible for.** th
1aef0 65 20 61 63 63 65 73 73 20 61 74 74 65 6d 70 74  e access attempt
1af00 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 69 73   or NULL if this
1af10 20 61 63 63 65 73 73 20 61 74 74 65 6d 70 74 20   access attempt 
1af20 69 73 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d  is directly from
1af30 0a 2a 2a 20 74 6f 70 2d 6c 65 76 65 6c 20 53 51  .** top-level SQ
1af40 4c 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 52 65  L code..**.** Re
1af50 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
1af60 48 31 32 35 35 31 5d 20 5b 48 31 32 35 35 32 5d  H12551] [H12552]
1af70 20 5b 48 31 32 35 35 33 5d 20 5b 48 31 32 35 35   [H12553] [H1255
1af80 34 5d 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  4].*/./*********
1af90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1afa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1afb0 2a 2a 20 33 72 64 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ** 3rd *********
1afc0 2a 2a 2a 20 34 74 68 20 2a 2a 2a 2a 2a 2a 2a 2a  *** 4th ********
1afd0 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ***/.#define SQL
1afe0 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45 58  ITE_CREATE_INDEX
1aff0 20 20 20 20 20 20 20 20 20 20 31 20 20 20 2f 2a            1   /*
1b000 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20   Index Name     
1b010 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
1b020 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1b030 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c 45 20  TE_CREATE_TABLE 
1b040 20 20 20 20 20 20 20 20 20 32 20 20 20 2f 2a 20           2   /* 
1b050 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
1b060 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  NULL            
1b070 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1b080 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49 4e  E_CREATE_TEMP_IN
1b090 44 45 58 20 20 20 20 20 33 20 20 20 2f 2a 20 49  DEX     3   /* I
1b0a0 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 54  ndex Name      T
1b0b0 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a  able Name      *
1b0c0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1b0d0 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41 42  _CREATE_TEMP_TAB
1b0e0 4c 45 20 20 20 20 20 34 20 20 20 2f 2a 20 54 61  LE     4   /* Ta
1b0f0 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55  ble Name      NU
1b100 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  LL            */
1b110 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1b120 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 49 47  CREATE_TEMP_TRIG
1b130 47 45 52 20 20 20 35 20 20 20 2f 2a 20 54 72 69  GER   5   /* Tri
1b140 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62  gger Name    Tab
1b150 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a  le Name      */.
1b160 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
1b170 52 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 57 20  REATE_TEMP_VIEW 
1b180 20 20 20 20 20 36 20 20 20 2f 2a 20 56 69 65 77       6   /* View
1b190 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c   Name       NULL
1b1a0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23              */.#
1b1b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52  define SQLITE_CR
1b1c0 45 41 54 45 5f 54 52 49 47 47 45 52 20 20 20 20  EATE_TRIGGER    
1b1d0 20 20 20 20 37 20 20 20 2f 2a 20 54 72 69 67 67      7   /* Trigg
1b1e0 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c 65  er Name    Table
1b1f0 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64   Name      */.#d
1b200 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45  efine SQLITE_CRE
1b210 41 54 45 5f 56 49 45 57 20 20 20 20 20 20 20 20  ATE_VIEW        
1b220 20 20 20 38 20 20 20 2f 2a 20 56 69 65 77 20 4e     8   /* View N
1b230 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 20  ame       NULL  
1b240 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65            */.#de
1b250 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 4c 45  fine SQLITE_DELE
1b260 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TE              
1b270 20 20 39 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e    9   /* Table N
1b280 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20  ame      NULL   
1b290 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66           */.#def
1b2a0 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f  ine SQLITE_DROP_
1b2b0 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20  INDEX           
1b2c0 31 30 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61  10   /* Index Na
1b2d0 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61  me      Table Na
1b2e0 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  me      */.#defi
1b2f0 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54  ne SQLITE_DROP_T
1b300 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 31  ABLE           1
1b310 31 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d  1   /* Table Nam
1b320 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20  e      NULL     
1b330 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
1b340 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45  e SQLITE_DROP_TE
1b350 4d 50 5f 49 4e 44 45 58 20 20 20 20 20 20 31 32  MP_INDEX      12
1b360 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65     /* Index Name
1b370 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65        Table Name
1b380 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
1b390 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d   SQLITE_DROP_TEM
1b3a0 50 5f 54 41 42 4c 45 20 20 20 20 20 20 31 33 20  P_TABLE      13 
1b3b0 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20    /* Table Name 
1b3c0 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20       NULL       
1b3d0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
1b3e0 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50  SQLITE_DROP_TEMP
1b3f0 5f 54 52 49 47 47 45 52 20 20 20 20 31 34 20 20  _TRIGGER    14  
1b400 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65   /* Trigger Name
1b410 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20      Table Name  
1b420 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
1b430 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f  QLITE_DROP_TEMP_
1b440 56 49 45 57 20 20 20 20 20 20 20 31 35 20 20 20  VIEW       15   
1b450 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20  /* View Name    
1b460 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20     NULL         
1b470 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
1b480 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47 45  LITE_DROP_TRIGGE
1b490 52 20 20 20 20 20 20 20 20 20 31 36 20 20 20 2f  R         16   /
1b4a0 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20 20  * Trigger Name  
1b4b0 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20    Table Name    
1b4c0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1b4d0 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 20 20 20  ITE_DROP_VIEW   
1b4e0 20 20 20 20 20 20 20 20 20 31 37 20 20 20 2f 2a           17   /*
1b4f0 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20 20   View Name      
1b500 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20   NULL           
1b510 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1b520 54 45 5f 49 4e 53 45 52 54 20 20 20 20 20 20 20  TE_INSERT       
1b530 20 20 20 20 20 20 20 20 31 38 20 20 20 2f 2a 20          18   /* 
1b540 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
1b550 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  NULL            
1b560 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1b570 45 5f 50 52 41 47 4d 41 20 20 20 20 20 20 20 20  E_PRAGMA        
1b580 20 20 20 20 20 20 20 31 39 20 20 20 2f 2a 20 50         19   /* P
1b590 72 61 67 6d 61 20 4e 61 6d 65 20 20 20 20 20 31  ragma Name     1
1b5a0 73 74 20 61 72 67 20 6f 72 20 4e 55 4c 4c 20 2a  st arg or NULL *
1b5b0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1b5c0 5f 52 45 41 44 20 20 20 20 20 20 20 20 20 20 20  _READ           
1b5d0 20 20 20 20 20 20 32 30 20 20 20 2f 2a 20 54 61        20   /* Ta
1b5e0 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 43 6f  ble Name      Co
1b5f0 6c 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f  lumn Name     */
1b600 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1b610 53 45 4c 45 43 54 20 20 20 20 20 20 20 20 20 20  SELECT          
1b620 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4e 55 4c       21   /* NUL
1b630 4c 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c  L            NUL
1b640 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  L            */.
1b650 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54  #define SQLITE_T
1b660 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 20 20  RANSACTION      
1b670 20 20 20 20 32 32 20 20 20 2f 2a 20 4f 70 65 72      22   /* Oper
1b680 61 74 69 6f 6e 20 20 20 20 20 20 20 4e 55 4c 4c  ation       NULL
1b690 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23              */.#
1b6a0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 50  define SQLITE_UP
1b6b0 44 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20  DATE            
1b6c0 20 20 20 32 33 20 20 20 2f 2a 20 54 61 62 6c 65     23   /* Table
1b6d0 20 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c 75 6d   Name      Colum
1b6e0 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23 64  n Name     */.#d
1b6f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 54 54  efine SQLITE_ATT
1b700 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20  ACH             
1b710 20 20 32 34 20 20 20 2f 2a 20 46 69 6c 65 6e 61    24   /* Filena
1b720 6d 65 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 20  me        NULL  
1b730 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65            */.#de
1b740 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 54 41  fine SQLITE_DETA
1b750 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20  CH              
1b760 20 32 35 20 20 20 2f 2a 20 44 61 74 61 62 61 73   25   /* Databas
1b770 65 20 4e 61 6d 65 20 20 20 4e 55 4c 4c 20 20 20  e Name   NULL   
1b780 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66           */.#def
1b790 69 6e 65 20 53 51 4c 49 54 45 5f 41 4c 54 45 52  ine SQLITE_ALTER
1b7a0 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20  _TABLE          
1b7b0 32 36 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65  26   /* Database
1b7c0 20 4e 61 6d 65 20 20 20 54 61 62 6c 65 20 4e 61   Name   Table Na
1b7d0 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  me      */.#defi
1b7e0 6e 65 20 53 51 4c 49 54 45 5f 52 45 49 4e 44 45  ne SQLITE_REINDE
1b7f0 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32  X              2
1b800 37 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d  7   /* Index Nam
1b810 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20  e      NULL     
1b820 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
1b830 65 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59 5a 45  e SQLITE_ANALYZE
1b840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 38                28
1b850 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65     /* Table Name
1b860 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20        NULL      
1b870 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
1b880 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56   SQLITE_CREATE_V
1b890 54 41 42 4c 45 20 20 20 20 20 20 20 20 32 39 20  TABLE        29 
1b8a0 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20    /* Table Name 
1b8b0 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d 65       Module Name
1b8c0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
1b8d0 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54 41 42  SQLITE_DROP_VTAB
1b8e0 4c 45 20 20 20 20 20 20 20 20 20 20 33 30 20 20  LE          30  
1b8f0 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20   /* Table Name  
1b900 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d 65 20      Module Name 
1b910 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
1b920 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e 20 20  QLITE_FUNCTION  
1b930 20 20 20 20 20 20 20 20 20 20 20 33 31 20 20 20             31   
1b940 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20  /* NULL         
1b950 20 20 20 46 75 6e 63 74 69 6f 6e 20 4e 61 6d 65     Function Name
1b960 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
1b970 4c 49 54 45 5f 53 41 56 45 50 4f 49 4e 54 20 20  LITE_SAVEPOINT  
1b980 20 20 20 20 20 20 20 20 20 20 33 32 20 20 20 2f            32   /
1b990 2a 20 4f 70 65 72 61 74 69 6f 6e 20 20 20 20 20  * Operation     
1b9a0 20 20 53 61 76 65 70 6f 69 6e 74 20 4e 61 6d 65    Savepoint Name
1b9b0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1b9c0 49 54 45 5f 43 4f 50 59 20 20 20 20 20 20 20 20  ITE_COPY        
1b9d0 20 20 20 20 20 20 20 20 20 20 30 20 20 20 2f 2a            0   /*
1b9e0 20 4e 6f 20 6c 6f 6e 67 65 72 20 75 73 65 64 20   No longer used 
1b9f0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  */../*.** CAPI3R
1ba00 45 46 3a 20 54 72 61 63 69 6e 67 20 41 6e 64 20  EF: Tracing And 
1ba10 50 72 6f 66 69 6c 69 6e 67 20 46 75 6e 63 74 69  Profiling Functi
1ba20 6f 6e 73 20 7b 48 31 32 32 38 30 7d 20 3c 53 36  ons {H12280} <S6
1ba30 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d  0400>.** EXPERIM
1ba40 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73  ENTAL.**.** Thes
1ba50 65 20 72 6f 75 74 69 6e 65 73 20 72 65 67 69 73  e routines regis
1ba60 74 65 72 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  ter callback fun
1ba70 63 74 69 6f 6e 73 20 74 68 61 74 20 63 61 6e 20  ctions that can 
1ba80 62 65 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74  be used for.** t
1ba90 72 61 63 69 6e 67 20 61 6e 64 20 70 72 6f 66 69  racing and profi
1baa0 6c 69 6e 67 20 74 68 65 20 65 78 65 63 75 74 69  ling the executi
1bab0 6f 6e 20 6f 66 20 53 51 4c 20 73 74 61 74 65 6d  on of SQL statem
1bac0 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ents..**.** The 
1bad0 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f  callback functio
1bae0 6e 20 72 65 67 69 73 74 65 72 65 64 20 62 79 20  n registered by 
1baf0 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 29 20  sqlite3_trace() 
1bb00 69 73 20 69 6e 76 6f 6b 65 64 20 61 74 0a 2a 2a  is invoked at.**
1bb10 20 76 61 72 69 6f 75 73 20 74 69 6d 65 73 20 77   various times w
1bb20 68 65 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 65  hen an SQL state
1bb30 6d 65 6e 74 20 69 73 20 62 65 69 6e 67 20 72 75  ment is being ru
1bb40 6e 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 74  n by [sqlite3_st
1bb50 65 70 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 63 61  ep()]..** The ca
1bb60 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 61  llback returns a
1bb70 20 55 54 46 2d 38 20 72 65 6e 64 65 72 69 6e 67   UTF-8 rendering
1bb80 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61 74   of the SQL stat
1bb90 65 6d 65 6e 74 20 74 65 78 74 0a 2a 2a 20 61 73  ement text.** as
1bba0 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 66   the statement f
1bbb0 69 72 73 74 20 62 65 67 69 6e 73 20 65 78 65 63  irst begins exec
1bbc0 75 74 69 6e 67 2e 20 20 41 64 64 69 74 69 6f 6e  uting.  Addition
1bbd0 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 6f 63 63  al callbacks occ
1bbe0 75 72 0a 2a 2a 20 61 73 20 65 61 63 68 20 74 72  ur.** as each tr
1bbf0 69 67 67 65 72 65 64 20 73 75 62 70 72 6f 67 72  iggered subprogr
1bc00 61 6d 20 69 73 20 65 6e 74 65 72 65 64 2e 20 20  am is entered.  
1bc10 54 68 65 20 63 61 6c 6c 62 61 63 6b 73 20 66 6f  The callbacks fo
1bc20 72 20 74 72 69 67 67 65 72 73 0a 2a 2a 20 63 6f  r triggers.** co
1bc30 6e 74 61 69 6e 20 61 20 55 54 46 2d 38 20 53 51  ntain a UTF-8 SQ
1bc40 4c 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74 20 69  L comment that i
1bc50 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 74 72  dentifies the tr
1bc60 69 67 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  igger..**.** The
1bc70 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
1bc80 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 62 79  on registered by
1bc90 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65   sqlite3_profile
1bca0 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a  () is invoked.**
1bcb0 20 61 73 20 65 61 63 68 20 53 51 4c 20 73 74 61   as each SQL sta
1bcc0 74 65 6d 65 6e 74 20 66 69 6e 69 73 68 65 73 2e  tement finishes.
1bcd0 20 20 54 68 65 20 70 72 6f 66 69 6c 65 20 63 61    The profile ca
1bce0 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 73 0a  llback contains.
1bcf0 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  ** the original 
1bd00 73 74 61 74 65 6d 65 6e 74 20 74 65 78 74 20 61  statement text a
1bd10 6e 64 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f  nd an estimate o
1bd20 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 74 69 6d  f wall-clock tim
1bd30 65 0a 2a 2a 20 6f 66 20 68 6f 77 20 6c 6f 6e 67  e.** of how long
1bd40 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 20   that statement 
1bd50 74 6f 6f 6b 20 74 6f 20 72 75 6e 2e 0a 2a 2a 0a  took to run..**.
1bd60 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
1bd70 0a 2a 2a 20 5b 48 31 32 32 38 31 5d 20 5b 48 31  .** [H12281] [H1
1bd80 32 32 38 32 5d 20 5b 48 31 32 32 38 33 5d 20 5b  2282] [H12283] [
1bd90 48 31 32 32 38 34 5d 20 5b 48 31 32 32 38 35 5d  H12284] [H12285]
1bda0 20 5b 48 31 32 32 38 37 5d 20 5b 48 31 32 32 38   [H12287] [H1228
1bdb0 38 5d 20 5b 48 31 32 32 38 39 5d 0a 2a 2a 20 5b  8] [H12289].** [
1bdc0 48 31 32 32 39 30 5d 0a 2a 2f 0a 53 51 4c 49 54  H12290].*/.SQLIT
1bdd0 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50  E_API SQLITE_EXP
1bde0 45 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64 20 2a  ERIMENTAL void *
1bdf0 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 73 71  sqlite3_trace(sq
1be00 6c 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a 78 54  lite3*, void(*xT
1be10 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73  race)(void*,cons
1be20 74 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 2a 29  t char*), void*)
1be30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c  ;.SQLITE_API SQL
1be40 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c  ITE_EXPERIMENTAL
1be50 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 70   void *sqlite3_p
1be60 72 6f 66 69 6c 65 28 73 71 6c 69 74 65 33 2a 2c  rofile(sqlite3*,
1be70 0a 20 20 20 76 6f 69 64 28 2a 78 50 72 6f 66 69  .   void(*xProfi
1be80 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20  le)(void*,const 
1be90 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 75 69  char*,sqlite3_ui
1bea0 6e 74 36 34 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a  nt64), void*);..
1beb0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
1bec0 51 75 65 72 79 20 50 72 6f 67 72 65 73 73 20 43  Query Progress C
1bed0 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 39 31 30  allbacks {H12910
1bee0 7d 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a  } <S60400>.**.**
1bef0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f   This routine co
1bf00 6e 66 69 67 75 72 65 73 20 61 20 63 61 6c 6c 62  nfigures a callb
1bf10 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2d 20 74  ack function - t
1bf20 68 65 0a 2a 2a 20 70 72 6f 67 72 65 73 73 20 63  he.** progress c
1bf30 61 6c 6c 62 61 63 6b 20 2d 20 74 68 61 74 20 69  allback - that i
1bf40 73 20 69 6e 76 6f 6b 65 64 20 70 65 72 69 6f 64  s invoked period
1bf50 69 63 61 6c 6c 79 20 64 75 72 69 6e 67 20 6c 6f  ically during lo
1bf60 6e 67 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 63 61  ng.** running ca
1bf70 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  lls to [sqlite3_
1bf80 65 78 65 63 28 29 5d 2c 20 5b 73 71 6c 69 74 65  exec()], [sqlite
1bf90 33 5f 73 74 65 70 28 29 5d 20 61 6e 64 0a 2a 2a  3_step()] and.**
1bfa0 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61   [sqlite3_get_ta
1bfb0 62 6c 65 28 29 5d 2e 20 20 41 6e 20 65 78 61 6d  ble()].  An exam
1bfc0 70 6c 65 20 75 73 65 20 66 6f 72 20 74 68 69 73  ple use for this
1bfd0 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73  .** interface is
1bfe0 20 74 6f 20 6b 65 65 70 20 61 20 47 55 49 20 75   to keep a GUI u
1bff0 70 64 61 74 65 64 20 64 75 72 69 6e 67 20 61 20  pdated during a 
1c000 6c 61 72 67 65 20 71 75 65 72 79 2e 0a 2a 2a 0a  large query..**.
1c010 2a 2a 20 49 66 20 74 68 65 20 70 72 6f 67 72 65  ** If the progre
1c020 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75  ss callback retu
1c030 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68  rns non-zero, th
1c040 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a 2a  e operation is.*
1c050 2a 20 69 6e 74 65 72 72 75 70 74 65 64 2e 20 20  * interrupted.  
1c060 54 68 69 73 20 66 65 61 74 75 72 65 20 63 61 6e  This feature can
1c070 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c   be used to impl
1c080 65 6d 65 6e 74 20 61 0a 2a 2a 20 22 43 61 6e 63  ement a.** "Canc
1c090 65 6c 22 20 62 75 74 74 6f 6e 20 6f 6e 20 61 20  el" button on a 
1c0a0 47 55 49 20 70 72 6f 67 72 65 73 73 20 64 69 61  GUI progress dia
1c0b0 6c 6f 67 20 62 6f 78 2e 0a 2a 2a 0a 2a 2a 20 54  log box..**.** T
1c0c0 68 65 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64  he progress hand
1c0d0 6c 65 72 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20  ler must not do 
1c0e0 61 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 69  anything that wi
1c0f0 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65  ll modify.** the
1c100 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1c110 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65  tion that invoke
1c120 64 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 68  d the progress h
1c130 61 6e 64 6c 65 72 2e 0a 2a 2a 20 4e 6f 74 65 20  andler..** Note 
1c140 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72  that [sqlite3_pr
1c150 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20  epare_v2()] and 
1c160 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
1c170 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65   both modify the
1c180 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  ir.** database c
1c190 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74  onnections for t
1c1a0 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d  he meaning of "m
1c1b0 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70  odify" in this p
1c1c0 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20  aragraph..**.** 
1c1d0 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
1c1e0 20 5b 48 31 32 39 31 31 5d 20 5b 48 31 32 39 31   [H12911] [H1291
1c1f0 32 5d 20 5b 48 31 32 39 31 33 5d 20 5b 48 31 32  2] [H12913] [H12
1c200 39 31 34 5d 20 5b 48 31 32 39 31 35 5d 20 5b 48  914] [H12915] [H
1c210 31 32 39 31 36 5d 20 5b 48 31 32 39 31 37 5d 20  12916] [H12917] 
1c220 5b 48 31 32 39 31 38 5d 0a 2a 2a 0a 2a 2f 0a 53  [H12918].**.*/.S
1c230 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
1c240 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f  qlite3_progress_
1c250 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65 33 2a  handler(sqlite3*
1c260 2c 20 69 6e 74 2c 20 69 6e 74 28 2a 29 28 76 6f  , int, int(*)(vo
1c270 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f  id*), void*);../
1c280 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f  *.** CAPI3REF: O
1c290 70 65 6e 69 6e 67 20 41 20 4e 65 77 20 44 61 74  pening A New Dat
1c2a0 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e  abase Connection
1c2b0 20 7b 48 31 32 37 30 30 7d 20 3c 53 34 30 32 30   {H12700} <S4020
1c2c0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  0>.**.** These r
1c2d0 6f 75 74 69 6e 65 73 20 6f 70 65 6e 20 61 6e 20  outines open an 
1c2e0 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20  SQLite database 
1c2f0 66 69 6c 65 20 77 68 6f 73 65 20 6e 61 6d 65 20  file whose name 
1c300 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 65 0a  is given by the.
1c310 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 75  ** filename argu
1c320 6d 65 6e 74 2e 20 54 68 65 20 66 69 6c 65 6e 61  ment. The filena
1c330 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 69  me argument is i
1c340 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 55 54  nterpreted as UT
1c350 46 2d 38 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 74  F-8 for.** sqlit
1c360 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 71  e3_open() and sq
1c370 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20  lite3_open_v2() 
1c380 61 6e 64 20 61 73 20 55 54 46 2d 31 36 20 69 6e  and as UTF-16 in
1c390 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 65   the native byte
1c3a0 0a 2a 2a 20 6f 72 64 65 72 20 66 6f 72 20 73 71  .** order for sq
1c3b0 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e 20  lite3_open16(). 
1c3c0 41 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  A [database conn
1c3d0 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 20 69  ection] handle i
1c3e0 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 72 65 74  s usually.** ret
1c3f0 75 72 6e 65 64 20 69 6e 20 2a 70 70 44 62 2c 20  urned in *ppDb, 
1c400 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f 72  even if an error
1c410 20 6f 63 63 75 72 73 2e 20 20 54 68 65 20 6f 6e   occurs.  The on
1c420 6c 79 20 65 78 63 65 70 74 69 6f 6e 20 69 73 20  ly exception is 
1c430 74 68 61 74 0a 2a 2a 20 69 66 20 53 51 4c 69 74  that.** if SQLit
1c440 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 61  e is unable to a
1c450 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 74  llocate memory t
1c460 6f 20 68 6f 6c 64 20 74 68 65 20 5b 73 71 6c 69  o hold the [sqli
1c470 74 65 33 5d 20 6f 62 6a 65 63 74 2c 0a 2a 2a 20  te3] object,.** 
1c480 61 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 20 77  a NULL will be w
1c490 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 70 44  ritten into *ppD
1c4a0 62 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 70  b instead of a p
1c4b0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b 73  ointer to the [s
1c4c0 71 6c 69 74 65 33 5d 0a 2a 2a 20 6f 62 6a 65 63  qlite3].** objec
1c4d0 74 2e 20 49 66 20 74 68 65 20 64 61 74 61 62 61  t. If the databa
1c4e0 73 65 20 69 73 20 6f 70 65 6e 65 64 20 28 61 6e  se is opened (an
1c4f0 64 2f 6f 72 20 63 72 65 61 74 65 64 29 20 73 75  d/or created) su
1c500 63 63 65 73 73 66 75 6c 6c 79 2c 20 74 68 65 6e  ccessfully, then
1c510 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20  .** [SQLITE_OK] 
1c520 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f 74  is returned.  Ot
1c530 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 6f  herwise an [erro
1c540 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72  r code] is retur
1c550 6e 65 64 2e 20 20 54 68 65 0a 2a 2a 20 5b 73 71  ned.  The.** [sq
1c560 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 20  lite3_errmsg()] 
1c570 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d  or [sqlite3_errm
1c580 73 67 31 36 28 29 5d 20 72 6f 75 74 69 6e 65 73  sg16()] routines
1c590 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
1c5a0 6f 62 74 61 69 6e 0a 2a 2a 20 61 6e 20 45 6e 67  obtain.** an Eng
1c5b0 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 20 64 65  lish language de
1c5c0 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65  scription of the
1c5d0 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68   error..**.** Th
1c5e0 65 20 64 65 66 61 75 6c 74 20 65 6e 63 6f 64 69  e default encodi
1c5f0 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 61 62  ng for the datab
1c600 61 73 65 20 77 69 6c 6c 20 62 65 20 55 54 46 2d  ase will be UTF-
1c610 38 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  8 if.** sqlite3_
1c620 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74 65  open() or sqlite
1c630 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 63  3_open_v2() is c
1c640 61 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 55 54 46  alled and.** UTF
1c650 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74 69 76  -16 in the nativ
1c660 65 20 62 79 74 65 20 6f 72 64 65 72 20 69 66 20  e byte order if 
1c670 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29  sqlite3_open16()
1c680 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20   is used..**.** 
1c690 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61  Whether or not a
1c6a0 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77  n error occurs w
1c6b0 68 65 6e 20 69 74 20 69 73 20 6f 70 65 6e 65 64  hen it is opened
1c6c0 2c 20 72 65 73 6f 75 72 63 65 73 0a 2a 2a 20 61  , resources.** a
1c6d0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
1c6e0 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
1c6f0 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 20  nection] handle 
1c700 73 68 6f 75 6c 64 20 62 65 20 72 65 6c 65 61 73  should be releas
1c710 65 64 20 62 79 0a 2a 2a 20 70 61 73 73 69 6e 67  ed by.** passing
1c720 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f   it to [sqlite3_
1c730 63 6c 6f 73 65 28 29 5d 20 77 68 65 6e 20 69 74  close()] when it
1c740 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65   is no longer re
1c750 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  quired..**.** Th
1c760 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76  e sqlite3_open_v
1c770 32 28 29 20 69 6e 74 65 72 66 61 63 65 20 77 6f  2() interface wo
1c780 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 74 65 33  rks like sqlite3
1c790 5f 6f 70 65 6e 28 29 0a 2a 2a 20 65 78 63 65 70  _open().** excep
1c7a0 74 20 74 68 61 74 20 69 74 20 61 63 63 65 70 74  t that it accept
1c7b0 73 20 74 77 6f 20 61 64 64 69 74 69 6f 6e 61 6c  s two additional
1c7c0 20 70 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20   parameters for 
1c7d0 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6e 74 72  additional contr
1c7e0 6f 6c 0a 2a 2a 20 6f 76 65 72 20 74 68 65 20 6e  ol.** over the n
1c7f0 65 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ew database conn
1c800 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 66 6c 61  ection.  The fla
1c810 67 73 20 70 61 72 61 6d 65 74 65 72 20 63 61 6e  gs parameter can
1c820 20 74 61 6b 65 20 6f 6e 65 20 6f 66 0a 2a 2a 20   take one of.** 
1c830 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  the following th
1c840 72 65 65 20 76 61 6c 75 65 73 2c 20 6f 70 74 69  ree values, opti
1c850 6f 6e 61 6c 6c 79 20 63 6f 6d 62 69 6e 65 64 20  onally combined 
1c860 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 5b 53 51  with the .** [SQ
1c870 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45  LITE_OPEN_NOMUTE
1c880 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  X], [SQLITE_OPEN
1c890 5f 46 55 4c 4c 4d 55 54 45 58 5d 2c 20 5b 53 51  _FULLMUTEX], [SQ
1c8a0 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44  LITE_OPEN_SHARED
1c8b0 43 41 43 48 45 5d 2c 0a 2a 2a 20 61 6e 64 2f 6f  CACHE],.** and/o
1c8c0 72 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50  r [SQLITE_OPEN_P
1c8d0 52 49 56 41 54 45 43 41 43 48 45 5d 20 66 6c 61  RIVATECACHE] fla
1c8e0 67 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a  gs:.**.** <dl>.*
1c8f0 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50  * <dt>[SQLITE_OP
1c900 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 3c 2f 64 74  EN_READONLY]</dt
1c910 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 74  >.** <dd>The dat
1c920 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20  abase is opened 
1c930 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64  in read-only mod
1c940 65 2e 20 20 49 66 20 74 68 65 20 64 61 74 61 62  e.  If the datab
1c950 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20  ase does not.** 
1c960 61 6c 72 65 61 64 79 20 65 78 69 73 74 2c 20 61  already exist, a
1c970 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72  n error is retur
1c980 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ned.</dd>.**.** 
1c990 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  <dt>[SQLITE_OPEN
1c9a0 5f 52 45 41 44 57 52 49 54 45 5d 3c 2f 64 74 3e  _READWRITE]</dt>
1c9b0 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 74 61  .** <dd>The data
1c9c0 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 66  base is opened f
1c9d0 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77  or reading and w
1c9e0 72 69 74 69 6e 67 20 69 66 20 70 6f 73 73 69 62  riting if possib
1c9f0 6c 65 2c 20 6f 72 20 72 65 61 64 69 6e 67 0a 2a  le, or reading.*
1ca00 2a 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 66 69  * only if the fi
1ca10 6c 65 20 69 73 20 77 72 69 74 65 20 70 72 6f 74  le is write prot
1ca20 65 63 74 65 64 20 62 79 20 74 68 65 20 6f 70 65  ected by the ope
1ca30 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20  rating system.  
1ca40 49 6e 20 65 69 74 68 65 72 0a 2a 2a 20 63 61 73  In either.** cas
1ca50 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d  e the database m
1ca60 75 73 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  ust already exis
1ca70 74 2c 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20  t, otherwise an 
1ca80 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65  error is returne
1ca90 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  d.</dd>.**.** <d
1caa0 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  t>[SQLITE_OPEN_R
1cab0 45 41 44 57 52 49 54 45 5d 20 7c 20 5b 53 51 4c  EADWRITE] | [SQL
1cac0 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d  ITE_OPEN_CREATE]
1cad0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65  </dt>.** <dd>The
1cae0 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65   database is ope
1caf0 6e 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67 20  ned for reading 
1cb00 61 6e 64 20 77 72 69 74 69 6e 67 2c 20 61 6e 64  and writing, and
1cb10 20 69 73 20 63 72 65 61 74 65 73 20 69 74 20 69   is creates it i
1cb20 66 0a 2a 2a 20 69 74 20 64 6f 65 73 20 6e 6f 74  f.** it does not
1cb30 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2e 20   already exist. 
1cb40 54 68 69 73 20 69 73 20 74 68 65 20 62 65 68 61  This is the beha
1cb50 76 69 6f 72 20 74 68 61 74 20 69 73 20 61 6c 77  vior that is alw
1cb60 61 79 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20  ays used for.** 
1cb70 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 61  sqlite3_open() a
1cb80 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31  nd sqlite3_open1
1cb90 36 28 29 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64  6().</dd>.** </d
1cba0 6c 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  l>.**.** If the 
1cbb0 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f  3rd parameter to
1cbc0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32   sqlite3_open_v2
1cbd0 28 29 20 69 73 20 6e 6f 74 20 6f 6e 65 20 6f 66  () is not one of
1cbe0 20 74 68 65 0a 2a 2a 20 63 6f 6d 62 69 6e 61 74   the.** combinat
1cbf0 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f 76 65  ions shown above
1cc00 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 63   or one of the c
1cc10 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73 68 6f 77  ombinations show
1cc20 6e 20 61 62 6f 76 65 20 63 6f 6d 62 69 6e 65 64  n above combined
1cc30 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b 53 51  .** with the [SQ
1cc40 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45  LITE_OPEN_NOMUTE
1cc50 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  X], [SQLITE_OPEN
1cc60 5f 46 55 4c 4c 4d 55 54 45 58 5d 2c 0a 2a 2a 20  _FULLMUTEX],.** 
1cc70 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41  [SQLITE_OPEN_SHA
1cc80 52 45 44 43 41 43 48 45 5d 20 61 6e 64 2f 6f 72  REDCACHE] and/or
1cc90 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48   [SQLITE_OPEN_SH
1cca0 41 52 45 44 43 41 43 48 45 5d 20 66 6c 61 67 73  AREDCACHE] flags
1ccb0 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 62 65  ,.** then the be
1ccc0 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69  havior is undefi
1ccd0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ned..**.** If th
1cce0 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e  e [SQLITE_OPEN_N
1ccf0 4f 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 73 20  OMUTEX] flag is 
1cd00 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 64 61  set, then the da
1cd10 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1cd20 6e 0a 2a 2a 20 6f 70 65 6e 73 20 69 6e 20 74 68  n.** opens in th
1cd30 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 20 5b  e multi-thread [
1cd40 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20  threading mode] 
1cd50 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 73  as long as the s
1cd60 69 6e 67 6c 65 2d 74 68 72 65 61 64 0a 2a 2a 20  ingle-thread.** 
1cd70 6d 6f 64 65 20 68 61 73 20 6e 6f 74 20 62 65 65  mode has not bee
1cd80 6e 20 73 65 74 20 61 74 20 63 6f 6d 70 69 6c 65  n set at compile
1cd90 2d 74 69 6d 65 20 6f 72 20 73 74 61 72 74 2d 74  -time or start-t
1cda0 69 6d 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20  ime.  If the.** 
1cdb0 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c  [SQLITE_OPEN_FUL
1cdc0 4c 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 73 20  LMUTEX] flag is 
1cdd0 73 65 74 20 74 68 65 6e 20 74 68 65 20 64 61 74  set then the dat
1cde0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1cdf0 20 6f 70 65 6e 73 0a 2a 2a 20 69 6e 20 74 68 65   opens.** in the
1ce00 20 73 65 72 69 61 6c 69 7a 65 64 20 5b 74 68 72   serialized [thr
1ce10 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 75 6e 6c  eading mode] unl
1ce20 65 73 73 20 73 69 6e 67 6c 65 2d 74 68 72 65 61  ess single-threa
1ce30 64 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75  d was.** previou
1ce40 73 6c 79 20 73 65 6c 65 63 74 65 64 20 61 74 20  sly selected at 
1ce50 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72 20  compile-time or 
1ce60 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 20 54  start-time..** T
1ce70 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  he [SQLITE_OPEN_
1ce80 53 48 41 52 45 44 43 41 43 48 45 5d 20 66 6c 61  SHAREDCACHE] fla
1ce90 67 20 63 61 75 73 65 73 20 74 68 65 20 64 61 74  g causes the dat
1cea0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1ceb0 20 74 6f 20 62 65 0a 2a 2a 20 65 6c 69 67 69 62   to be.** eligib
1cec0 6c 65 20 74 6f 20 75 73 65 20 5b 73 68 61 72 65  le to use [share
1ced0 64 20 63 61 63 68 65 20 6d 6f 64 65 5d 2c 20 72  d cache mode], r
1cee0 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65  egardless of whe
1cef0 74 68 65 72 20 6f 72 20 6e 6f 74 20 73 68 61 72  ther or not shar
1cf00 65 64 0a 2a 2a 20 63 61 63 68 65 20 69 73 20 65  ed.** cache is e
1cf10 6e 61 62 6c 65 64 20 75 73 69 6e 67 20 5b 73 71  nabled using [sq
1cf20 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61  lite3_enable_sha
1cf30 72 65 64 5f 63 61 63 68 65 28 29 5d 2e 20 20 54  red_cache()].  T
1cf40 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50  he.** [SQLITE_OP
1cf50 45 4e 5f 50 52 49 56 41 54 45 43 41 43 48 45 5d  EN_PRIVATECACHE]
1cf60 20 66 6c 61 67 20 63 61 75 73 65 73 20 74 68 65   flag causes the
1cf70 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1cf80 74 69 6f 6e 20 74 6f 20 6e 6f 74 0a 2a 2a 20 70  tion to not.** p
1cf90 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 5b 73  articipate in [s
1cfa0 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65  hared cache mode
1cfb0 5d 20 65 76 65 6e 20 69 66 20 69 74 20 69 73 20  ] even if it is 
1cfc0 65 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  enabled..**.** I
1cfd0 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 69  f the filename i
1cfe0 73 20 22 3a 6d 65 6d 6f 72 79 3a 22 2c 20 74 68  s ":memory:", th
1cff0 65 6e 20 61 20 70 72 69 76 61 74 65 2c 20 74 65  en a private, te
1d000 6d 70 6f 72 61 72 79 20 69 6e 2d 6d 65 6d 6f 72  mporary in-memor
1d010 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 73  y database.** is
1d020 20 63 72 65 61 74 65 64 20 66 6f 72 20 74 68 65   created for the
1d030 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68   connection.  Th
1d040 69 73 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  is in-memory dat
1d050 61 62 61 73 65 20 77 69 6c 6c 20 76 61 6e 69 73  abase will vanis
1d060 68 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 64 61  h when.** the da
1d070 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1d080 6e 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 46 75  n is closed.  Fu
1d090 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66  ture versions of
1d0a0 20 53 51 4c 69 74 65 20 6d 69 67 68 74 0a 2a 2a   SQLite might.**
1d0b0 20 6d 61 6b 65 20 75 73 65 20 6f 66 20 61 64 64   make use of add
1d0c0 69 74 69 6f 6e 61 6c 20 73 70 65 63 69 61 6c 20  itional special 
1d0d0 66 69 6c 65 6e 61 6d 65 73 20 74 68 61 74 20 62  filenames that b
1d0e0 65 67 69 6e 20 77 69 74 68 20 74 68 65 20 22 3a  egin with the ":
1d0f0 22 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20  " character..** 
1d100 49 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65  It is recommende
1d110 64 20 74 68 61 74 20 77 68 65 6e 20 61 20 64 61  d that when a da
1d120 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20  tabase filename 
1d130 61 63 74 75 61 6c 6c 79 20 64 6f 65 73 20 62 65  actually does be
1d140 67 69 6e 20 77 69 74 68 0a 2a 2a 20 61 20 22 3a  gin with.** a ":
1d150 22 20 63 68 61 72 61 63 74 65 72 20 79 6f 75 20  " character you 
1d160 73 68 6f 75 6c 64 20 70 72 65 66 69 78 20 74 68  should prefix th
1d170 65 20 66 69 6c 65 6e 61 6d 65 20 77 69 74 68 20  e filename with 
1d180 61 20 70 61 74 68 6e 61 6d 65 20 73 75 63 68 20  a pathname such 
1d190 61 73 0a 2a 2a 20 22 2e 2f 22 20 74 6f 20 61 76  as.** "./" to av
1d1a0 6f 69 64 20 61 6d 62 69 67 75 69 74 79 2e 0a 2a  oid ambiguity..*
1d1b0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6c 65  *.** If the file
1d1c0 6e 61 6d 65 20 69 73 20 61 6e 20 65 6d 70 74 79  name is an empty
1d1d0 20 73 74 72 69 6e 67 2c 20 74 68 65 6e 20 61 20   string, then a 
1d1e0 70 72 69 76 61 74 65 2c 20 74 65 6d 70 6f 72 61  private, tempora
1d1f0 72 79 0a 2a 2a 20 6f 6e 2d 64 69 73 6b 20 64 61  ry.** on-disk da
1d200 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 63  tabase will be c
1d210 72 65 61 74 65 64 2e 20 20 54 68 69 73 20 70 72  reated.  This pr
1d220 69 76 61 74 65 20 64 61 74 61 62 61 73 65 20 77  ivate database w
1d230 69 6c 6c 20 62 65 0a 2a 2a 20 61 75 74 6f 6d 61  ill be.** automa
1d240 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 20  tically deleted 
1d250 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 64  as soon as the d
1d260 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1d270 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a  on is closed..**
1d280 0a 2a 2a 20 54 68 65 20 66 6f 75 72 74 68 20 70  .** The fourth p
1d290 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69  arameter to sqli
1d2a0 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73  te3_open_v2() is
1d2b0 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
1d2c0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 66 73  .** [sqlite3_vfs
1d2d0 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 20 64 65  ] object that de
1d2e0 66 69 6e 65 73 20 74 68 65 20 6f 70 65 72 61 74  fines the operat
1d2f0 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72  ing system inter
1d300 66 61 63 65 20 74 68 61 74 0a 2a 2a 20 74 68 65  face that.** the
1d310 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f   new database co
1d320 6e 6e 65 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20  nnection should 
1d330 75 73 65 2e 20 20 49 66 20 74 68 65 20 66 6f 75  use.  If the fou
1d340 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 73  rth parameter is
1d350 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  .** a NULL point
1d360 65 72 20 74 68 65 6e 20 74 68 65 20 64 65 66 61  er then the defa
1d370 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73  ult [sqlite3_vfs
1d380 5d 20 6f 62 6a 65 63 74 20 69 73 20 75 73 65 64  ] object is used
1d390 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 4e 6f 74 65 20  ..**.** <b>Note 
1d3a0 74 6f 20 57 69 6e 64 6f 77 73 20 75 73 65 72 73  to Windows users
1d3b0 3a 3c 2f 62 3e 20 20 54 68 65 20 65 6e 63 6f 64  :</b>  The encod
1d3c0 69 6e 67 20 75 73 65 64 20 66 6f 72 20 74 68 65  ing used for the
1d3d0 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65   filename argume
1d3e0 6e 74 0a 2a 2a 20 6f 66 20 73 71 6c 69 74 65 33  nt.** of sqlite3
1d3f0 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c 69  _open() and sqli
1d400 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 6d 75  te3_open_v2() mu
1d410 73 74 20 62 65 20 55 54 46 2d 38 2c 20 6e 6f 74  st be UTF-8, not
1d420 20 77 68 61 74 65 76 65 72 0a 2a 2a 20 63 6f 64   whatever.** cod
1d430 65 70 61 67 65 20 69 73 20 63 75 72 72 65 6e 74  epage is current
1d440 6c 79 20 64 65 66 69 6e 65 64 2e 20 20 46 69 6c  ly defined.  Fil
1d450 65 6e 61 6d 65 73 20 63 6f 6e 74 61 69 6e 69 6e  enames containin
1d460 67 20 69 6e 74 65 72 6e 61 74 69 6f 6e 61 6c 0a  g international.
1d470 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20 6d 75  ** characters mu
1d480 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20  st be converted 
1d490 74 6f 20 55 54 46 2d 38 20 70 72 69 6f 72 20 74  to UTF-8 prior t
1d4a0 6f 20 70 61 73 73 69 6e 67 20 74 68 65 6d 20 69  o passing them i
1d4b0 6e 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f  nto.** sqlite3_o
1d4c0 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 33  pen() or sqlite3
1d4d0 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2a 0a 2a  _open_v2()..**.*
1d4e0 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
1d4f0 2a 2a 20 5b 48 31 32 37 30 31 5d 20 5b 48 31 32  ** [H12701] [H12
1d500 37 30 32 5d 20 5b 48 31 32 37 30 33 5d 20 5b 48  702] [H12703] [H
1d510 31 32 37 30 34 5d 20 5b 48 31 32 37 30 36 5d 20  12704] [H12706] 
1d520 5b 48 31 32 37 30 37 5d 20 5b 48 31 32 37 30 39  [H12707] [H12709
1d530 5d 20 5b 48 31 32 37 31 31 5d 0a 2a 2a 20 5b 48  ] [H12711].** [H
1d540 31 32 37 31 32 5d 20 5b 48 31 32 37 31 33 5d 20  12712] [H12713] 
1d550 5b 48 31 32 37 31 34 5d 20 5b 48 31 32 37 31 37  [H12714] [H12717
1d560 5d 20 5b 48 31 32 37 31 39 5d 20 5b 48 31 32 37  ] [H12719] [H127
1d570 32 31 5d 20 5b 48 31 32 37 32 33 5d 0a 2a 2f 0a  21] [H12723].*/.
1d580 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1d590 71 6c 69 74 65 33 5f 6f 70 65 6e 28 0a 20 20 63  qlite3_open(.  c
1d5a0 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e  onst char *filen
1d5b0 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61  ame,   /* Databa
1d5c0 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46  se filename (UTF
1d5d0 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  -8) */.  sqlite3
1d5e0 20 2a 2a 70 70 44 62 20 20 20 20 20 20 20 20 20   **ppDb         
1d5f0 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20   /* OUT: SQLite 
1d600 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 29 3b 0a  db handle */.);.
1d610 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1d620 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 0a 20  qlite3_open16(. 
1d630 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 66 69 6c   const void *fil
1d640 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61  ename,   /* Data
1d650 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55  base filename (U
1d660 54 46 2d 31 36 29 20 2a 2f 0a 20 20 73 71 6c 69  TF-16) */.  sqli
1d670 74 65 33 20 2a 2a 70 70 44 62 20 20 20 20 20 20  te3 **ppDb      
1d680 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69      /* OUT: SQLi
1d690 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a  te db handle */.
1d6a0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
1d6b0 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76  t sqlite3_open_v
1d6c0 32 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  2(.  const char 
1d6d0 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20  *filename,   /* 
1d6e0 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d  Database filenam
1d6f0 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73  e (UTF-8) */.  s
1d700 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 2c 20 20  qlite3 **ppDb,  
1d710 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53         /* OUT: S
1d720 51 4c 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20  QLite db handle 
1d730 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20  */.  int flags, 
1d740 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1d750 46 6c 61 67 73 20 2a 2f 0a 20 20 63 6f 6e 73 74  Flags */.  const
1d760 20 63 68 61 72 20 2a 7a 56 66 73 20 20 20 20 20   char *zVfs     
1d770 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 56 46     /* Name of VF
1d780 53 20 6d 6f 64 75 6c 65 20 74 6f 20 75 73 65 20  S module to use 
1d790 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  */.);../*.** CAP
1d7a0 49 33 52 45 46 3a 20 45 72 72 6f 72 20 43 6f 64  I3REF: Error Cod
1d7b0 65 73 20 41 6e 64 20 4d 65 73 73 61 67 65 73 20  es And Messages 
1d7c0 7b 48 31 32 38 30 30 7d 20 3c 53 36 30 32 30 30  {H12800} <S60200
1d7d0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  >.**.** The sqli
1d7e0 74 65 33 5f 65 72 72 63 6f 64 65 28 29 20 69 6e  te3_errcode() in
1d7f0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
1d800 74 68 65 20 6e 75 6d 65 72 69 63 20 5b 72 65 73  the numeric [res
1d810 75 6c 74 20 63 6f 64 65 5d 20 6f 72 0a 2a 2a 20  ult code] or.** 
1d820 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74  [extended result
1d830 20 63 6f 64 65 5d 20 66 6f 72 20 74 68 65 20 6d   code] for the m
1d840 6f 73 74 20 72 65 63 65 6e 74 20 66 61 69 6c 65  ost recent faile
1d850 64 20 73 71 6c 69 74 65 33 5f 2a 20 41 50 49 20  d sqlite3_* API 
1d860 63 61 6c 6c 0a 2a 2a 20 61 73 73 6f 63 69 61 74  call.** associat
1d870 65 64 20 77 69 74 68 20 61 20 5b 64 61 74 61 62  ed with a [datab
1d880 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e  ase connection].
1d890 20 49 66 20 61 20 70 72 69 6f 72 20 41 50 49 20   If a prior API 
1d8a0 63 61 6c 6c 20 66 61 69 6c 65 64 0a 2a 2a 20 62  call failed.** b
1d8b0 75 74 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  ut the most rece
1d8c0 6e 74 20 41 50 49 20 63 61 6c 6c 20 73 75 63 63  nt API call succ
1d8d0 65 65 64 65 64 2c 20 74 68 65 20 72 65 74 75 72  eeded, the retur
1d8e0 6e 20 76 61 6c 75 65 20 66 72 6f 6d 0a 2a 2a 20  n value from.** 
1d8f0 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28  sqlite3_errcode(
1d900 29 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20  ) is undefined. 
1d910 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 74   The sqlite3_ext
1d920 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 29 0a  ended_errcode().
1d930 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  ** interface is 
1d940 74 68 65 20 73 61 6d 65 20 65 78 63 65 70 74 20  the same except 
1d950 74 68 61 74 20 69 74 20 61 6c 77 61 79 73 20 72  that it always r
1d960 65 74 75 72 6e 73 20 74 68 65 20 0a 2a 2a 20 5b  eturns the .** [
1d970 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
1d980 63 6f 64 65 5d 20 65 76 65 6e 20 77 68 65 6e 20  code] even when 
1d990 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
1d9a0 63 6f 64 65 73 20 61 72 65 0a 2a 2a 20 64 69 73  codes are.** dis
1d9b0 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  abled..**.** The
1d9c0 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28   sqlite3_errmsg(
1d9d0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72  ) and sqlite3_er
1d9e0 72 6d 73 67 31 36 28 29 20 72 65 74 75 72 6e 20  rmsg16() return 
1d9f0 45 6e 67 6c 69 73 68 2d 6c 61 6e 67 75 61 67 65  English-language
1da00 0a 2a 2a 20 74 65 78 74 20 74 68 61 74 20 64 65  .** text that de
1da10 73 63 72 69 62 65 73 20 74 68 65 20 65 72 72 6f  scribes the erro
1da20 72 2c 20 61 73 20 65 69 74 68 65 72 20 55 54 46  r, as either UTF
1da30 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 72 65 73  -8 or UTF-16 res
1da40 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 4d 65  pectively..** Me
1da50 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65  mory to hold the
1da60 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73   error message s
1da70 74 72 69 6e 67 20 69 73 20 6d 61 6e 61 67 65 64  tring is managed
1da80 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 2a 2a 20   internally..** 
1da90 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  The application 
1daa0 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f  does not need to
1dab0 20 77 6f 72 72 79 20 61 62 6f 75 74 20 66 72 65   worry about fre
1dac0 65 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 2e  eing the result.
1dad0 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65  .** However, the
1dae0 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20 6d 69   error string mi
1daf0 67 68 74 20 62 65 20 6f 76 65 72 77 72 69 74 74  ght be overwritt
1db00 65 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65  en or deallocate
1db10 64 20 62 79 0a 2a 2a 20 73 75 62 73 65 71 75 65  d by.** subseque
1db20 6e 74 20 63 61 6c 6c 73 20 74 6f 20 6f 74 68 65  nt calls to othe
1db30 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61  r SQLite interfa
1db40 63 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a  ce functions..**
1db50 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 73 65 72  .** When the ser
1db60 69 61 6c 69 7a 65 64 20 5b 74 68 72 65 61 64 69  ialized [threadi
1db70 6e 67 20 6d 6f 64 65 5d 20 69 73 20 69 6e 20 75  ng mode] is in u
1db80 73 65 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20  se, it might be 
1db90 74 68 65 0a 2a 2a 20 63 61 73 65 20 74 68 61 74  the.** case that
1dba0 20 61 20 73 65 63 6f 6e 64 20 65 72 72 6f 72 20   a second error 
1dbb0 6f 63 63 75 72 73 20 6f 6e 20 61 20 73 65 70 61  occurs on a sepa
1dbc0 72 61 74 65 20 74 68 72 65 61 64 20 69 6e 20 62  rate thread in b
1dbd0 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20 74 69  etween.** the ti
1dbe0 6d 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20  me of the first 
1dbf0 65 72 72 6f 72 20 61 6e 64 20 74 68 65 20 63 61  error and the ca
1dc00 6c 6c 20 74 6f 20 74 68 65 73 65 20 69 6e 74 65  ll to these inte
1dc10 72 66 61 63 65 73 2e 0a 2a 2a 20 57 68 65 6e 20  rfaces..** When 
1dc20 74 68 61 74 20 68 61 70 70 65 6e 73 2c 20 74 68  that happens, th
1dc30 65 20 73 65 63 6f 6e 64 20 65 72 72 6f 72 20 77  e second error w
1dc40 69 6c 6c 20 62 65 20 72 65 70 6f 72 74 65 64 20  ill be reported 
1dc50 73 69 6e 63 65 20 74 68 65 73 65 0a 2a 2a 20 69  since these.** i
1dc60 6e 74 65 72 66 61 63 65 73 20 61 6c 77 61 79 73  nterfaces always
1dc70 20 72 65 70 6f 72 74 20 74 68 65 20 6d 6f 73 74   report the most
1dc80 20 72 65 63 65 6e 74 20 72 65 73 75 6c 74 2e 20   recent result. 
1dc90 20 54 6f 20 61 76 6f 69 64 0a 2a 2a 20 74 68 69   To avoid.** thi
1dca0 73 2c 20 65 61 63 68 20 74 68 72 65 61 64 20 63  s, each thread c
1dcb0 61 6e 20 6f 62 74 61 69 6e 20 65 78 63 6c 75 73  an obtain exclus
1dcc0 69 76 65 20 75 73 65 20 6f 66 20 74 68 65 20 5b  ive use of the [
1dcd0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1dce0 69 6f 6e 5d 20 44 0a 2a 2a 20 62 79 20 69 6e 76  ion] D.** by inv
1dcf0 6f 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d  oking [sqlite3_m
1dd00 75 74 65 78 5f 65 6e 74 65 72 5d 28 5b 73 71 6c  utex_enter]([sql
1dd10 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d 28 44  ite3_db_mutex](D
1dd20 29 29 20 62 65 66 6f 72 65 20 62 65 67 69 6e 6e  )) before beginn
1dd30 69 6e 67 0a 2a 2a 20 74 6f 20 75 73 65 20 44 20  ing.** to use D 
1dd40 61 6e 64 20 69 6e 76 6f 6b 69 6e 67 20 5b 73 71  and invoking [sq
1dd50 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1dd60 65 5d 28 5b 73 71 6c 69 74 65 33 5f 64 62 5f 6d  e]([sqlite3_db_m
1dd70 75 74 65 78 5d 28 44 29 29 20 61 66 74 65 72 0a  utex](D)) after.
1dd80 2a 2a 20 61 6c 6c 20 63 61 6c 6c 73 20 74 6f 20  ** all calls to 
1dd90 74 68 65 20 69 6e 74 65 72 66 61 63 65 73 20 6c  the interfaces l
1dda0 69 73 74 65 64 20 68 65 72 65 20 61 72 65 20 63  isted here are c
1ddb0 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  ompleted..**.** 
1ddc0 49 66 20 61 6e 20 69 6e 74 65 72 66 61 63 65 20  If an interface 
1ddd0 66 61 69 6c 73 20 77 69 74 68 20 53 51 4c 49 54  fails with SQLIT
1dde0 45 5f 4d 49 53 55 53 45 2c 20 74 68 61 74 20 6d  E_MISUSE, that m
1ddf0 65 61 6e 73 20 74 68 65 20 69 6e 74 65 72 66 61  eans the interfa
1de00 63 65 0a 2a 2a 20 77 61 73 20 69 6e 76 6f 6b 65  ce.** was invoke
1de10 64 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 62 79  d incorrectly by
1de20 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
1de30 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c  .  In that case,
1de40 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 63 6f   the.** error co
1de50 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65 20 6d  de and message m
1de60 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65  ay or may not be
1de70 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75   set..**.** Requ
1de80 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
1de90 32 38 30 31 5d 20 5b 48 31 32 38 30 32 5d 20 5b  2801] [H12802] [
1dea0 48 31 32 38 30 33 5d 20 5b 48 31 32 38 30 37 5d  H12803] [H12807]
1deb0 20 5b 48 31 32 38 30 38 5d 20 5b 48 31 32 38 30   [H12808] [H1280
1dec0 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  9].*/.SQLITE_API
1ded0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 72 72   int sqlite3_err
1dee0 63 6f 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62  code(sqlite3 *db
1def0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
1df00 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64  t sqlite3_extend
1df10 65 64 5f 65 72 72 63 6f 64 65 28 73 71 6c 69 74  ed_errcode(sqlit
1df20 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f  e3 *db);.SQLITE_
1df30 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  API const char *
1df40 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 73  sqlite3_errmsg(s
1df50 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45  qlite3*);.SQLITE
1df60 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
1df70 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31  *sqlite3_errmsg1
1df80 36 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a  6(sqlite3*);../*
1df90 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51  .** CAPI3REF: SQ
1dfa0 4c 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a 65  L Statement Obje
1dfb0 63 74 20 7b 48 31 33 30 30 30 7d 20 3c 48 31 33  ct {H13000} <H13
1dfc0 30 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  010>.** KEYWORDS
1dfd0 3a 20 7b 70 72 65 70 61 72 65 64 20 73 74 61 74  : {prepared stat
1dfe0 65 6d 65 6e 74 7d 20 7b 70 72 65 70 61 72 65 64  ement} {prepared
1dff0 20 73 74 61 74 65 6d 65 6e 74 73 7d 0a 2a 2a 0a   statements}.**.
1e000 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
1e010 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65  f this object re
1e020 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c  presents a singl
1e030 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  e SQL statement.
1e040 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 20  .** This object 
1e050 69 73 20 76 61 72 69 6f 75 73 6c 79 20 6b 6e 6f  is variously kno
1e060 77 6e 20 61 73 20 61 20 22 70 72 65 70 61 72 65  wn as a "prepare
1e070 64 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20  d statement" or 
1e080 61 0a 2a 2a 20 22 63 6f 6d 70 69 6c 65 64 20 53  a.** "compiled S
1e090 51 4c 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72  QL statement" or
1e0a0 20 73 69 6d 70 6c 79 20 61 73 20 61 20 22 73 74   simply as a "st
1e0b0 61 74 65 6d 65 6e 74 22 2e 0a 2a 2a 0a 2a 2a 20  atement"..**.** 
1e0c0 54 68 65 20 6c 69 66 65 20 6f 66 20 61 20 73 74  The life of a st
1e0d0 61 74 65 6d 65 6e 74 20 6f 62 6a 65 63 74 20 67  atement object g
1e0e0 6f 65 73 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69  oes something li
1e0f0 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c  ke this:.**.** <
1e100 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 43 72 65 61  ol>.** <li> Crea
1e110 74 65 20 74 68 65 20 6f 62 6a 65 63 74 20 75 73  te the object us
1e120 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  ing [sqlite3_pre
1e130 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 61 20  pare_v2()] or a 
1e140 72 65 6c 61 74 65 64 0a 2a 2a 20 20 20 20 20 20  related.**      
1e150 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 3c 6c 69  function..** <li
1e160 3e 20 42 69 6e 64 20 76 61 6c 75 65 73 20 74 6f  > Bind values to
1e170 20 5b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72   [host parameter
1e180 73 5d 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c  s] using the sql
1e190 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 0a 2a 2a  ite3_bind_*().**
1e1a0 20 20 20 20 20 20 69 6e 74 65 72 66 61 63 65 73        interfaces
1e1b0 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 75 6e 20 74 68  ..** <li> Run th
1e1c0 65 20 53 51 4c 20 62 79 20 63 61 6c 6c 69 6e 67  e SQL by calling
1e1d0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
1e1e0 5d 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69  ] one or more ti
1e1f0 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 65 73  mes..** <li> Res
1e200 65 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  et the statement
1e210 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
1e220 72 65 73 65 74 28 29 5d 20 74 68 65 6e 20 67 6f  reset()] then go
1e230 20 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 74 6f   back.**      to
1e240 20 73 74 65 70 20 32 2e 20 20 44 6f 20 74 68 69   step 2.  Do thi
1e250 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 74  s zero or more t
1e260 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 44 65  imes..** <li> De
1e270 73 74 72 6f 79 20 74 68 65 20 6f 62 6a 65 63 74  stroy the object
1e280 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
1e290 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20  finalize()]..** 
1e2a0 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 66 65  </ol>.**.** Refe
1e2b0 72 20 74 6f 20 64 6f 63 75 6d 65 6e 74 61 74 69  r to documentati
1e2c0 6f 6e 20 6f 6e 20 69 6e 64 69 76 69 64 75 61 6c  on on individual
1e2d0 20 6d 65 74 68 6f 64 73 20 61 62 6f 76 65 20 66   methods above f
1e2e0 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a  or additional.**
1e2f0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f   information..*/
1e300 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
1e310 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 73 71 6c  sqlite3_stmt sql
1e320 69 74 65 33 5f 73 74 6d 74 3b 0a 0a 2f 2a 0a 2a  ite3_stmt;../*.*
1e330 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 6e 2d  * CAPI3REF: Run-
1e340 74 69 6d 65 20 4c 69 6d 69 74 73 20 7b 48 31 32  time Limits {H12
1e350 37 36 30 7d 20 3c 53 32 30 36 30 30 3e 0a 2a 2a  760} <S20600>.**
1e360 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61  .** This interfa
1e370 63 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 73 69  ce allows the si
1e380 7a 65 20 6f 66 20 76 61 72 69 6f 75 73 20 63 6f  ze of various co
1e390 6e 73 74 72 75 63 74 73 20 74 6f 20 62 65 20 6c  nstructs to be l
1e3a0 69 6d 69 74 65 64 0a 2a 2a 20 6f 6e 20 61 20 63  imited.** on a c
1e3b0 6f 6e 6e 65 63 74 69 6f 6e 20 62 79 20 63 6f 6e  onnection by con
1e3c0 6e 65 63 74 69 6f 6e 20 62 61 73 69 73 2e 20 20  nection basis.  
1e3d0 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  The first parame
1e3e0 74 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 5b 64  ter is the.** [d
1e3f0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1e400 6f 6e 5d 20 77 68 6f 73 65 20 6c 69 6d 69 74 20  on] whose limit 
1e410 69 73 20 74 6f 20 62 65 20 73 65 74 20 6f 72 20  is to be set or 
1e420 71 75 65 72 69 65 64 2e 20 20 54 68 65 0a 2a 2a  queried.  The.**
1e430 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
1e440 72 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20  r is one of the 
1e450 5b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65  [limit categorie
1e460 73 5d 20 74 68 61 74 20 64 65 66 69 6e 65 20 61  s] that define a
1e470 0a 2a 2a 20 63 6c 61 73 73 20 6f 66 20 63 6f 6e  .** class of con
1e480 73 74 72 75 63 74 73 20 74 6f 20 62 65 20 73 69  structs to be si
1e490 7a 65 20 6c 69 6d 69 74 65 64 2e 20 20 54 68 65  ze limited.  The
1e4a0 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72   third parameter
1e4b0 20 69 73 20 74 68 65 0a 2a 2a 20 6e 65 77 20 6c   is the.** new l
1e4c0 69 6d 69 74 20 66 6f 72 20 74 68 61 74 20 63 6f  imit for that co
1e4d0 6e 73 74 72 75 63 74 2e 20 20 54 68 65 20 66 75  nstruct.  The fu
1e4e0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
1e4f0 68 65 20 6f 6c 64 20 6c 69 6d 69 74 2e 0a 2a 2a  he old limit..**
1e500 0a 2a 2a 20 49 66 20 74 68 65 20 6e 65 77 20 6c  .** If the new l
1e510 69 6d 69 74 20 69 73 20 61 20 6e 65 67 61 74 69  imit is a negati
1e520 76 65 20 6e 75 6d 62 65 72 2c 20 74 68 65 20 6c  ve number, the l
1e530 69 6d 69 74 20 69 73 20 75 6e 63 68 61 6e 67 65  imit is unchange
1e540 64 2e 0a 2a 2a 20 46 6f 72 20 74 68 65 20 6c 69  d..** For the li
1e550 6d 69 74 20 63 61 74 65 67 6f 72 79 20 6f 66 20  mit category of 
1e560 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 58 59 5a  SQLITE_LIMIT_XYZ
1e570 20 74 68 65 72 65 20 69 73 20 61 20 0a 2a 2a 20   there is a .** 
1e580 5b 6c 69 6d 69 74 73 20 7c 20 68 61 72 64 20 75  [limits | hard u
1e590 70 70 65 72 20 62 6f 75 6e 64 5d 0a 2a 2a 20 73  pper bound].** s
1e5a0 65 74 20 62 79 20 61 20 63 6f 6d 70 69 6c 65 2d  et by a compile-
1e5b0 74 69 6d 65 20 43 20 70 72 65 70 72 6f 63 65 73  time C preproces
1e5c0 73 6f 72 20 6d 61 63 72 6f 20 6e 61 6d 65 64 20  sor macro named 
1e5d0 0a 2a 2a 20 5b 6c 69 6d 69 74 73 20 7c 20 53 51  .** [limits | SQ
1e5e0 4c 49 54 45 5f 4d 41 58 5f 58 59 5a 5d 2e 0a 2a  LITE_MAX_XYZ]..*
1e5f0 2a 20 28 54 68 65 20 22 5f 4c 49 4d 49 54 5f 22  * (The "_LIMIT_"
1e600 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 69 73 20   in the name is 
1e610 63 68 61 6e 67 65 64 20 74 6f 20 22 5f 4d 41 58  changed to "_MAX
1e620 5f 22 2e 29 0a 2a 2a 20 41 74 74 65 6d 70 74 73  _".).** Attempts
1e630 20 74 6f 20 69 6e 63 72 65 61 73 65 20 61 20 6c   to increase a l
1e640 69 6d 69 74 20 61 62 6f 76 65 20 69 74 73 20 68  imit above its h
1e650 61 72 64 20 75 70 70 65 72 20 62 6f 75 6e 64 20  ard upper bound 
1e660 61 72 65 0a 2a 2a 20 73 69 6c 65 6e 74 6c 79 20  are.** silently 
1e670 74 72 75 6e 63 61 74 65 64 20 74 6f 20 74 68 65  truncated to the
1e680 20 68 61 72 64 20 75 70 70 65 72 20 6c 69 6d 69   hard upper limi
1e690 74 2e 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 74 69 6d  t..**.** Run tim
1e6a0 65 20 6c 69 6d 69 74 73 20 61 72 65 20 69 6e 74  e limits are int
1e6b0 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e  ended for use in
1e6c0 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68   applications th
1e6d0 61 74 20 6d 61 6e 61 67 65 0a 2a 2a 20 62 6f 74  at manage.** bot
1e6e0 68 20 74 68 65 69 72 20 6f 77 6e 20 69 6e 74 65  h their own inte
1e6f0 72 6e 61 6c 20 64 61 74 61 62 61 73 65 20 61 6e  rnal database an
1e700 64 20 61 6c 73 6f 20 64 61 74 61 62 61 73 65 73  d also databases
1e710 20 74 68 61 74 20 61 72 65 20 63 6f 6e 74 72 6f   that are contro
1e720 6c 6c 65 64 0a 2a 2a 20 62 79 20 75 6e 74 72 75  lled.** by untru
1e730 73 74 65 64 20 65 78 74 65 72 6e 61 6c 20 73 6f  sted external so
1e740 75 72 63 65 73 2e 20 20 41 6e 20 65 78 61 6d 70  urces.  An examp
1e750 6c 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d  le application m
1e760 69 67 68 74 20 62 65 20 61 0a 2a 2a 20 77 65 62  ight be a.** web
1e770 20 62 72 6f 77 73 65 72 20 74 68 61 74 20 68 61   browser that ha
1e780 73 20 69 74 73 20 6f 77 6e 20 64 61 74 61 62 61  s its own databa
1e790 73 65 73 20 66 6f 72 20 73 74 6f 72 69 6e 67 20  ses for storing 
1e7a0 68 69 73 74 6f 72 79 20 61 6e 64 0a 2a 2a 20 73  history and.** s
1e7b0 65 70 61 72 61 74 65 20 64 61 74 61 62 61 73 65  eparate database
1e7c0 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20  s controlled by 
1e7d0 4a 61 76 61 53 63 72 69 70 74 20 61 70 70 6c 69  JavaScript appli
1e7e0 63 61 74 69 6f 6e 73 20 64 6f 77 6e 6c 6f 61 64  cations download
1e7f0 65 64 0a 2a 2a 20 6f 66 66 20 74 68 65 20 49 6e  ed.** off the In
1e800 74 65 72 6e 65 74 2e 20 20 54 68 65 20 69 6e 74  ternet.  The int
1e810 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 73 20  ernal databases 
1e820 63 61 6e 20 62 65 20 67 69 76 65 6e 20 74 68 65  can be given the
1e830 0a 2a 2a 20 6c 61 72 67 65 2c 20 64 65 66 61 75  .** large, defau
1e840 6c 74 20 6c 69 6d 69 74 73 2e 20 20 44 61 74 61  lt limits.  Data
1e850 62 61 73 65 73 20 6d 61 6e 61 67 65 64 20 62 79  bases managed by
1e860 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 65   external source
1e870 73 20 63 61 6e 0a 2a 2a 20 62 65 20 67 69 76 65  s can.** be give
1e880 6e 20 6d 75 63 68 20 73 6d 61 6c 6c 65 72 20 6c  n much smaller l
1e890 69 6d 69 74 73 20 64 65 73 69 67 6e 65 64 20 74  imits designed t
1e8a0 6f 20 70 72 65 76 65 6e 74 20 61 20 64 65 6e 69  o prevent a deni
1e8b0 61 6c 20 6f 66 20 73 65 72 76 69 63 65 0a 2a 2a  al of service.**
1e8c0 20 61 74 74 61 63 6b 2e 20 20 44 65 76 65 6c 6f   attack.  Develo
1e8d0 70 65 72 73 20 6d 69 67 68 74 20 61 6c 73 6f 20  pers might also 
1e8e0 77 61 6e 74 20 74 6f 20 75 73 65 20 74 68 65 20  want to use the 
1e8f0 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74  [sqlite3_set_aut
1e900 68 6f 72 69 7a 65 72 28 29 5d 0a 2a 2a 20 69 6e  horizer()].** in
1e910 74 65 72 66 61 63 65 20 74 6f 20 66 75 72 74 68  terface to furth
1e920 65 72 20 63 6f 6e 74 72 6f 6c 20 75 6e 74 72 75  er control untru
1e930 73 74 65 64 20 53 51 4c 2e 20 20 54 68 65 20 73  sted SQL.  The s
1e940 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ize of the datab
1e950 61 73 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 62  ase.** created b
1e960 79 20 61 6e 20 75 6e 74 72 75 73 74 65 64 20 73  y an untrusted s
1e970 63 72 69 70 74 20 63 61 6e 20 62 65 20 63 6f 6e  cript can be con
1e980 74 61 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65  tained using the
1e990 0a 2a 2a 20 5b 6d 61 78 5f 70 61 67 65 5f 63 6f  .** [max_page_co
1e9a0 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 2e 0a 2a  unt] [PRAGMA]..*
1e9b0 2a 0a 2a 2a 20 4e 65 77 20 72 75 6e 2d 74 69 6d  *.** New run-tim
1e9c0 65 20 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69  e limit categori
1e9d0 65 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20  es may be added 
1e9e0 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73  in future releas
1e9f0 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  es..**.** Requir
1ea00 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 37  ements:.** [H127
1ea10 36 32 5d 20 5b 48 31 32 37 36 36 5d 20 5b 48 31  62] [H12766] [H1
1ea20 32 37 36 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  2769].*/.SQLITE_
1ea30 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1ea40 6c 69 6d 69 74 28 73 71 6c 69 74 65 33 2a 2c 20  limit(sqlite3*, 
1ea50 69 6e 74 20 69 64 2c 20 69 6e 74 20 6e 65 77 56  int id, int newV
1ea60 61 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  al);../*.** CAPI
1ea70 33 52 45 46 3a 20 52 75 6e 2d 54 69 6d 65 20 4c  3REF: Run-Time L
1ea80 69 6d 69 74 20 43 61 74 65 67 6f 72 69 65 73 20  imit Categories 
1ea90 7b 48 31 32 37 39 30 7d 20 3c 48 31 32 37 36 30  {H12790} <H12760
1eaa0 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b  >.** KEYWORDS: {
1eab0 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 79 7d 20  limit category} 
1eac0 7b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65  {limit categorie
1ead0 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63  s}.**.** These c
1eae0 6f 6e 73 74 61 6e 74 73 20 64 65 66 69 6e 65 20  onstants define 
1eaf0 76 61 72 69 6f 75 73 20 70 65 72 66 6f 72 6d 61  various performa
1eb00 6e 63 65 20 6c 69 6d 69 74 73 0a 2a 2a 20 74 68  nce limits.** th
1eb10 61 74 20 63 61 6e 20 62 65 20 6c 6f 77 65 72 65  at can be lowere
1eb20 64 20 61 74 20 72 75 6e 2d 74 69 6d 65 20 75 73  d at run-time us
1eb30 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d  ing [sqlite3_lim
1eb40 69 74 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 79  it()]..** The sy
1eb50 6e 6f 70 73 69 73 20 6f 66 20 74 68 65 20 6d 65  nopsis of the me
1eb60 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 76 61  anings of the va
1eb70 72 69 6f 75 73 20 6c 69 6d 69 74 73 20 69 73 20  rious limits is 
1eb80 73 68 6f 77 6e 20 62 65 6c 6f 77 2e 0a 2a 2a 20  shown below..** 
1eb90 41 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  Additional infor
1eba0 6d 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61  mation is availa
1ebb0 62 6c 65 20 61 74 20 5b 6c 69 6d 69 74 73 20 7c  ble at [limits |
1ebc0 20 4c 69 6d 69 74 73 20 69 6e 20 53 51 4c 69 74   Limits in SQLit
1ebd0 65 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a  e]..**.** <dl>.*
1ebe0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d  * <dt>SQLITE_LIM
1ebf0 49 54 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a 2a  IT_LENGTH</dt>.*
1ec00 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75  * <dd>The maximu
1ec10 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 73 74  m size of any st
1ec20 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 6f 72 20  ring or BLOB or 
1ec30 74 61 62 6c 65 20 72 6f 77 2e 3c 64 64 3e 0a 2a  table row.<dd>.*
1ec40 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
1ec50 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54 48  LIMIT_SQL_LENGTH
1ec60 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65  </dt>.** <dd>The
1ec70 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20   maximum length 
1ec80 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d  of an SQL statem
1ec90 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ent.</dd>.**.** 
1eca0 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54  <dt>SQLITE_LIMIT
1ecb0 5f 43 4f 4c 55 4d 4e 3c 2f 64 74 3e 0a 2a 2a 20  _COLUMN</dt>.** 
1ecc0 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20  <dd>The maximum 
1ecd0 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
1ece0 73 20 69 6e 20 61 20 74 61 62 6c 65 20 64 65 66  s in a table def
1ecf0 69 6e 69 74 69 6f 6e 20 6f 72 20 69 6e 20 74 68  inition or in th
1ed00 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65 74 20  e.** result set 
1ed10 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 6f 72  of a [SELECT] or
1ed20 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   the maximum num
1ed30 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
1ed40 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 6f 72  n an index.** or
1ed50 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42 59 20   in an ORDER BY 
1ed60 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  or GROUP BY clau
1ed70 73 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  se.</dd>.**.** <
1ed80 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  dt>SQLITE_LIMIT_
1ed90 45 58 50 52 5f 44 45 50 54 48 3c 2f 64 74 3e 0a  EXPR_DEPTH</dt>.
1eda0 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d  ** <dd>The maxim
1edb0 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 65 20  um depth of the 
1edc0 70 61 72 73 65 20 74 72 65 65 20 6f 6e 20 61 6e  parse tree on an
1edd0 79 20 65 78 70 72 65 73 73 69 6f 6e 2e 3c 2f 64  y expression.</d
1ede0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
1edf0 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55  ITE_LIMIT_COMPOU
1ee00 4e 44 5f 53 45 4c 45 43 54 3c 2f 64 74 3e 0a 2a  ND_SELECT</dt>.*
1ee10 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75  * <dd>The maximu
1ee20 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d  m number of term
1ee30 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20  s in a compound 
1ee40 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
1ee50 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
1ee60 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 44  >SQLITE_LIMIT_VD
1ee70 42 45 5f 4f 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  BE_OP</dt>.** <d
1ee80 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  d>The maximum nu
1ee90 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75 63 74  mber of instruct
1eea0 69 6f 6e 73 20 69 6e 20 61 20 76 69 72 74 75 61  ions in a virtua
1eeb0 6c 20 6d 61 63 68 69 6e 65 20 70 72 6f 67 72 61  l machine progra
1eec0 6d 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6d 70  m.** used to imp
1eed0 6c 65 6d 65 6e 74 20 61 6e 20 53 51 4c 20 73 74  lement an SQL st
1eee0 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a  atement.</dd>.**
1eef0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c  .** <dt>SQLITE_L
1ef00 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52  IMIT_FUNCTION_AR
1ef10 47 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  G</dt>.** <dd>Th
1ef20 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
1ef30 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 6f 6e   of arguments on
1ef40 20 61 20 66 75 6e 63 74 69 6f 6e 2e 3c 2f 64 64   a function.</dd
1ef50 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
1ef60 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45  TE_LIMIT_ATTACHE
1ef70 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  D</dt>.** <dd>Th
1ef80 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
1ef90 20 6f 66 20 5b 41 54 54 41 43 48 20 7c 20 61 74   of [ATTACH | at
1efa0 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73  tached databases
1efb0 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  ].</dd>.**.** <d
1efc0 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t>SQLITE_LIMIT_L
1efd0 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47  IKE_PATTERN_LENG
1efe0 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  TH</dt>.** <dd>T
1eff0 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  he maximum lengt
1f000 68 20 6f 66 20 74 68 65 20 70 61 74 74 65 72 6e  h of the pattern
1f010 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
1f020 20 5b 4c 49 4b 45 5d 20 6f 72 0a 2a 2a 20 5b 47   [LIKE] or.** [G
1f030 4c 4f 42 5d 20 6f 70 65 72 61 74 6f 72 73 2e 3c  LOB] operators.<
1f040 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53  /dd>.**.** <dt>S
1f050 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49  QLITE_LIMIT_VARI
1f060 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 2f 64 74 3e  ABLE_NUMBER</dt>
1f070 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69  .** <dd>The maxi
1f080 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 76 61  mum number of va
1f090 72 69 61 62 6c 65 73 20 69 6e 20 61 6e 20 53 51  riables in an SQ
1f0a0 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  L statement that
1f0b0 20 63 61 6e 0a 2a 2a 20 62 65 20 62 6f 75 6e 64   can.** be bound
1f0c0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
1f0d0 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 52  >SQLITE_LIMIT_TR
1f0e0 49 47 47 45 52 5f 44 45 50 54 48 3c 2f 64 74 3e  IGGER_DEPTH</dt>
1f0f0 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69  .** <dd>The maxi
1f100 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 72 65 63  mum depth of rec
1f110 75 72 73 69 6f 6e 20 66 6f 72 20 74 72 69 67 67  ursion for trigg
1f120 65 72 73 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64  ers.</dd>.** </d
1f130 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  l>.*/.#define SQ
1f140 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
1f150 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  H               
1f160 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53       0.#define S
1f170 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f  QLITE_LIMIT_SQL_
1f180 4c 45 4e 47 54 48 20 20 20 20 20 20 20 20 20 20  LENGTH          
1f190 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20        1.#define 
1f1a0 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c  SQLITE_LIMIT_COL
1f1b0 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20 20  UMN             
1f1c0 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65         2.#define
1f1d0 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58   SQLITE_LIMIT_EX
1f1e0 50 52 5f 44 45 50 54 48 20 20 20 20 20 20 20 20  PR_DEPTH        
1f1f0 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e          3.#defin
1f200 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43  e SQLITE_LIMIT_C
1f210 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 20  OMPOUND_SELECT  
1f220 20 20 20 20 20 20 20 20 20 34 0a 23 64 65 66 69           4.#defi
1f230 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  ne SQLITE_LIMIT_
1f240 56 44 42 45 5f 4f 50 20 20 20 20 20 20 20 20 20  VDBE_OP         
1f250 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 66            5.#def
1f260 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54  ine SQLITE_LIMIT
1f270 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 20 20  _FUNCTION_ARG   
1f280 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 64 65             6.#de
1f290 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49  fine SQLITE_LIMI
1f2a0 54 5f 41 54 54 41 43 48 45 44 20 20 20 20 20 20  T_ATTACHED      
1f2b0 20 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 64              7.#d
1f2c0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d  efine SQLITE_LIM
1f2d0 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f  IT_LIKE_PATTERN_
1f2e0 4c 45 4e 47 54 48 20 20 20 20 20 20 20 38 0a 23  LENGTH       8.#
1f2f0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49  define SQLITE_LI
1f300 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d  MIT_VARIABLE_NUM
1f310 42 45 52 20 20 20 20 20 20 20 20 20 20 20 39 0a  BER           9.
1f320 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
1f330 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45 50  IMIT_TRIGGER_DEP
1f340 54 48 20 20 20 20 20 20 20 20 20 20 20 20 31 30  TH            10
1f350 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
1f360 3a 20 43 6f 6d 70 69 6c 69 6e 67 20 41 6e 20 53  : Compiling An S
1f370 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31  QL Statement {H1
1f380 33 30 31 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a  3010} <S10000>.*
1f390 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c  * KEYWORDS: {SQL
1f3a0 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69   statement compi
1f3b0 6c 65 72 7d 0a 2a 2a 0a 2a 2a 20 54 6f 20 65 78  ler}.**.** To ex
1f3c0 65 63 75 74 65 20 61 6e 20 53 51 4c 20 71 75 65  ecute an SQL que
1f3d0 72 79 2c 20 69 74 20 6d 75 73 74 20 66 69 72 73  ry, it must firs
1f3e0 74 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 69 6e  t be compiled in
1f3f0 74 6f 20 61 20 62 79 74 65 2d 63 6f 64 65 0a 2a  to a byte-code.*
1f400 2a 20 70 72 6f 67 72 61 6d 20 75 73 69 6e 67 20  * program using 
1f410 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 72 6f 75  one of these rou
1f420 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tines..**.** The
1f430 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2c   first argument,
1f440 20 22 64 62 22 2c 20 69 73 20 61 20 5b 64 61 74   "db", is a [dat
1f450 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1f460 5d 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  ] obtained from 
1f470 61 0a 2a 2a 20 70 72 69 6f 72 20 73 75 63 63 65  a.** prior succe
1f480 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73  ssful call to [s
1f490 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20  qlite3_open()], 
1f4a0 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32  [sqlite3_open_v2
1f4b0 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74  ()] or.** [sqlit
1f4c0 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 20 20 54  e3_open16()].  T
1f4d0 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
1f4e0 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20  ection must not 
1f4f0 68 61 76 65 20 62 65 65 6e 20 63 6c 6f 73 65 64  have been closed
1f500 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f  ..**.** The seco
1f510 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 22 7a 53  nd argument, "zS
1f520 71 6c 22 2c 20 69 73 20 74 68 65 20 73 74 61 74  ql", is the stat
1f530 65 6d 65 6e 74 20 74 6f 20 62 65 20 63 6f 6d 70  ement to be comp
1f540 69 6c 65 64 2c 20 65 6e 63 6f 64 65 64 0a 2a 2a  iled, encoded.**
1f550 20 61 73 20 65 69 74 68 65 72 20 55 54 46 2d 38   as either UTF-8
1f560 20 6f 72 20 55 54 46 2d 31 36 2e 20 20 54 68 65   or UTF-16.  The
1f570 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
1f580 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70  () and sqlite3_p
1f590 72 65 70 61 72 65 5f 76 32 28 29 0a 2a 2a 20 69  repare_v2().** i
1f5a0 6e 74 65 72 66 61 63 65 73 20 75 73 65 20 55 54  nterfaces use UT
1f5b0 46 2d 38 2c 20 61 6e 64 20 73 71 6c 69 74 65 33  F-8, and sqlite3
1f5c0 5f 70 72 65 70 61 72 65 31 36 28 29 20 61 6e 64  _prepare16() and
1f5d0 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
1f5e0 31 36 5f 76 32 28 29 0a 2a 2a 20 75 73 65 20 55  16_v2().** use U
1f5f0 54 46 2d 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  TF-16..**.** If 
1f600 74 68 65 20 6e 42 79 74 65 20 61 72 67 75 6d 65  the nByte argume
1f610 6e 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20  nt is less than 
1f620 7a 65 72 6f 2c 20 74 68 65 6e 20 7a 53 71 6c 20  zero, then zSql 
1f630 69 73 20 72 65 61 64 20 75 70 20 74 6f 20 74 68  is read up to th
1f640 65 0a 2a 2a 20 66 69 72 73 74 20 7a 65 72 6f 20  e.** first zero 
1f650 74 65 72 6d 69 6e 61 74 6f 72 2e 20 49 66 20 6e  terminator. If n
1f660 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e 65 67 61  Byte is non-nega
1f670 74 69 76 65 2c 20 74 68 65 6e 20 69 74 20 69 73  tive, then it is
1f680 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20   the maximum.** 
1f690 6e 75 6d 62 65 72 20 6f 66 20 20 62 79 74 65 73  number of  bytes
1f6a0 20 72 65 61 64 20 66 72 6f 6d 20 7a 53 71 6c 2e   read from zSql.
1f6b0 20 20 57 68 65 6e 20 6e 42 79 74 65 20 69 73 20    When nByte is 
1f6c0 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68  non-negative, th
1f6d0 65 0a 2a 2a 20 7a 53 71 6c 20 73 74 72 69 6e 67  e.** zSql string
1f6e0 20 65 6e 64 73 20 61 74 20 65 69 74 68 65 72 20   ends at either 
1f6f0 74 68 65 20 66 69 72 73 74 20 27 5c 30 30 30 27  the first '\000'
1f700 20 6f 72 20 27 5c 75 30 30 30 30 27 20 63 68 61   or '\u0000' cha
1f710 72 61 63 74 65 72 20 6f 72 0a 2a 2a 20 74 68 65  racter or.** the
1f720 20 6e 42 79 74 65 2d 74 68 20 62 79 74 65 2c 20   nByte-th byte, 
1f730 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20  whichever comes 
1f740 66 69 72 73 74 2e 20 49 66 20 74 68 65 20 63 61  first. If the ca
1f750 6c 6c 65 72 20 6b 6e 6f 77 73 0a 2a 2a 20 74 68  ller knows.** th
1f760 61 74 20 74 68 65 20 73 75 70 70 6c 69 65 64 20  at the supplied 
1f770 73 74 72 69 6e 67 20 69 73 20 6e 75 6c 2d 74 65  string is nul-te
1f780 72 6d 69 6e 61 74 65 64 2c 20 74 68 65 6e 20 74  rminated, then t
1f790 68 65 72 65 20 69 73 20 61 20 73 6d 61 6c 6c 0a  here is a small.
1f7a0 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 61  ** performance a
1f7b0 64 76 61 6e 74 61 67 65 20 74 6f 20 62 65 20 67  dvantage to be g
1f7c0 61 69 6e 65 64 20 62 79 20 70 61 73 73 69 6e 67  ained by passing
1f7d0 20 61 6e 20 6e 42 79 74 65 20 70 61 72 61 6d 65   an nByte parame
1f7e0 74 65 72 20 74 68 61 74 0a 2a 2a 20 69 73 20 65  ter that.** is e
1f7f0 71 75 61 6c 20 74 6f 20 74 68 65 20 6e 75 6d 62  qual to the numb
1f800 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
1f810 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20  he input string 
1f820 3c 69 3e 69 6e 63 6c 75 64 69 6e 67 3c 2f 69 3e  <i>including</i>
1f830 0a 2a 2a 20 74 68 65 20 6e 75 6c 2d 74 65 72 6d  .** the nul-term
1f840 69 6e 61 74 6f 72 20 62 79 74 65 73 2e 0a 2a 2a  inator bytes..**
1f850 0a 2a 2a 20 49 66 20 70 7a 54 61 69 6c 20 69 73  .** If pzTail is
1f860 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 2a   not NULL then *
1f870 70 7a 54 61 69 6c 20 69 73 20 6d 61 64 65 20 74  pzTail is made t
1f880 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66  o point to the f
1f890 69 72 73 74 20 62 79 74 65 0a 2a 2a 20 70 61 73  irst byte.** pas
1f8a0 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
1f8b0 20 66 69 72 73 74 20 53 51 4c 20 73 74 61 74 65   first SQL state
1f8c0 6d 65 6e 74 20 69 6e 20 7a 53 71 6c 2e 20 20 54  ment in zSql.  T
1f8d0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f 6e  hese routines on
1f8e0 6c 79 0a 2a 2a 20 63 6f 6d 70 69 6c 65 20 74 68  ly.** compile th
1f8f0 65 20 66 69 72 73 74 20 73 74 61 74 65 6d 65 6e  e first statemen
1f900 74 20 69 6e 20 7a 53 71 6c 2c 20 73 6f 20 2a 70  t in zSql, so *p
1f910 7a 54 61 69 6c 20 69 73 20 6c 65 66 74 20 70 6f  zTail is left po
1f920 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a 20 77 68 61  inting to.** wha
1f930 74 20 72 65 6d 61 69 6e 73 20 75 6e 63 6f 6d 70  t remains uncomp
1f940 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70 53  iled..**.** *ppS
1f950 74 6d 74 20 69 73 20 6c 65 66 74 20 70 6f 69 6e  tmt is left poin
1f960 74 69 6e 67 20 74 6f 20 61 20 63 6f 6d 70 69 6c  ting to a compil
1f970 65 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  ed [prepared sta
1f980 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 63 61 6e  tement] that can
1f990 20 62 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20   be.** executed 
1f9a0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73  using [sqlite3_s
1f9b0 74 65 70 28 29 5d 2e 20 20 49 66 20 74 68 65 72  tep()].  If ther
1f9c0 65 20 69 73 20 61 6e 20 65 72 72 6f 72 2c 20 2a  e is an error, *
1f9d0 70 70 53 74 6d 74 20 69 73 20 73 65 74 0a 2a 2a  ppStmt is set.**
1f9e0 20 74 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 74 68   to NULL.  If th
1f9f0 65 20 69 6e 70 75 74 20 74 65 78 74 20 63 6f 6e  e input text con
1fa00 74 61 69 6e 73 20 6e 6f 20 53 51 4c 20 28 69 66  tains no SQL (if
1fa10 20 74 68 65 20 69 6e 70 75 74 20 69 73 20 61 6e   the input is an
1fa20 20 65 6d 70 74 79 0a 2a 2a 20 73 74 72 69 6e 67   empty.** string
1fa30 20 6f 72 20 61 20 63 6f 6d 6d 65 6e 74 29 20 74   or a comment) t
1fa40 68 65 6e 20 2a 70 70 53 74 6d 74 20 69 73 20 73  hen *ppStmt is s
1fa50 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 20 54  et to NULL..** T
1fa60 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65  he calling proce
1fa70 64 75 72 65 20 69 73 20 72 65 73 70 6f 6e 73 69  dure is responsi
1fa80 62 6c 65 20 66 6f 72 20 64 65 6c 65 74 69 6e 67  ble for deleting
1fa90 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a   the compiled.**
1faa0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 75   SQL statement u
1fab0 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69  sing [sqlite3_fi
1fac0 6e 61 6c 69 7a 65 28 29 5d 20 61 66 74 65 72 20  nalize()] after 
1fad0 69 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 20  it has finished 
1fae0 77 69 74 68 20 69 74 2e 0a 2a 2a 20 70 70 53 74  with it..** ppSt
1faf0 6d 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 4e 55  mt may not be NU
1fb00 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63  LL..**.** On suc
1fb10 63 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b  cess, [SQLITE_OK
1fb20 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f  ] is returned, o
1fb30 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72  therwise an [err
1fb40 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75  or code] is retu
1fb50 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rned..**.** The 
1fb60 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
1fb70 76 32 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  v2() and sqlite3
1fb80 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 20  _prepare16_v2() 
1fb90 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 0a 2a  interfaces are.*
1fba0 2a 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f  * recommended fo
1fbb0 72 20 61 6c 6c 20 6e 65 77 20 70 72 6f 67 72 61  r all new progra
1fbc0 6d 73 2e 20 54 68 65 20 74 77 6f 20 6f 6c 64 65  ms. The two olde
1fbd0 72 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65  r interfaces are
1fbe0 20 72 65 74 61 69 6e 65 64 0a 2a 2a 20 66 6f 72   retained.** for
1fbf0 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61   backwards compa
1fc00 74 69 62 69 6c 69 74 79 2c 20 62 75 74 20 74 68  tibility, but th
1fc10 65 69 72 20 75 73 65 20 69 73 20 64 69 73 63 6f  eir use is disco
1fc20 75 72 61 67 65 64 2e 0a 2a 2a 20 49 6e 20 74 68  uraged..** In th
1fc30 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65  e "v2" interface
1fc40 73 2c 20 74 68 65 20 70 72 65 70 61 72 65 64 20  s, the prepared 
1fc50 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 68 61  statement.** tha
1fc60 74 20 69 73 20 72 65 74 75 72 6e 65 64 20 28 74  t is returned (t
1fc70 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74  he [sqlite3_stmt
1fc80 5d 20 6f 62 6a 65 63 74 29 20 63 6f 6e 74 61 69  ] object) contai
1fc90 6e 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  ns a copy of the
1fca0 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c  .** original SQL
1fcb0 20 74 65 78 74 2e 20 54 68 69 73 20 63 61 75 73   text. This caus
1fcc0 65 73 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  es the [sqlite3_
1fcd0 73 74 65 70 28 29 5d 20 69 6e 74 65 72 66 61 63  step()] interfac
1fce0 65 20 74 6f 0a 2a 2a 20 62 65 68 61 76 65 20 61  e to.** behave a
1fcf0 20 64 69 66 66 65 72 65 6e 74 6c 79 20 69 6e 20   differently in 
1fd00 74 77 6f 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20  two ways:.**.** 
1fd10 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20  <ol>.** <li>.** 
1fd20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
1fd30 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 2c 20  schema changes, 
1fd40 69 6e 73 74 65 61 64 20 6f 66 20 72 65 74 75 72  instead of retur
1fd50 6e 69 6e 67 20 5b 53 51 4c 49 54 45 5f 53 43 48  ning [SQLITE_SCH
1fd60 45 4d 41 5d 20 61 73 20 69 74 0a 2a 2a 20 61 6c  EMA] as it.** al
1fd70 77 61 79 73 20 75 73 65 64 20 74 6f 20 64 6f 2c  ways used to do,
1fd80 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
1fd90 5d 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63  ] will automatic
1fda0 61 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65 20 74  ally recompile t
1fdb0 68 65 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d  he SQL.** statem
1fdc0 65 6e 74 20 61 6e 64 20 74 72 79 20 74 6f 20 72  ent and try to r
1fdd0 75 6e 20 69 74 20 61 67 61 69 6e 2e 20 20 49 66  un it again.  If
1fde0 20 74 68 65 20 73 63 68 65 6d 61 20 68 61 73 20   the schema has 
1fdf0 63 68 61 6e 67 65 64 20 69 6e 0a 2a 2a 20 61 20  changed in.** a 
1fe00 77 61 79 20 74 68 61 74 20 6d 61 6b 65 73 20 74  way that makes t
1fe10 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6e 6f 20  he statement no 
1fe20 6c 6f 6e 67 65 72 20 76 61 6c 69 64 2c 20 5b 73  longer valid, [s
1fe30 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 77  qlite3_step()] w
1fe40 69 6c 6c 20 73 74 69 6c 6c 0a 2a 2a 20 72 65 74  ill still.** ret
1fe50 75 72 6e 20 5b 53 51 4c 49 54 45 5f 53 43 48 45  urn [SQLITE_SCHE
1fe60 4d 41 5d 2e 20 20 42 75 74 20 75 6e 6c 69 6b 65  MA].  But unlike
1fe70 20 74 68 65 20 6c 65 67 61 63 79 20 62 65 68 61   the legacy beha
1fe80 76 69 6f 72 2c 20 5b 53 51 4c 49 54 45 5f 53 43  vior, [SQLITE_SC
1fe90 48 45 4d 41 5d 20 69 73 0a 2a 2a 20 6e 6f 77 20  HEMA] is.** now 
1fea0 61 20 66 61 74 61 6c 20 65 72 72 6f 72 2e 20 20  a fatal error.  
1feb0 43 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33  Calling [sqlite3
1fec0 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61  _prepare_v2()] a
1fed0 67 61 69 6e 20 77 69 6c 6c 20 6e 6f 74 20 6d 61  gain will not ma
1fee0 6b 65 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20  ke the.** error 
1fef0 67 6f 20 61 77 61 79 2e 20 20 4e 6f 74 65 3a 20  go away.  Note: 
1ff00 75 73 65 20 5b 73 71 6c 69 74 65 33 5f 65 72 72  use [sqlite3_err
1ff10 6d 73 67 28 29 5d 20 74 6f 20 66 69 6e 64 20 74  msg()] to find t
1ff20 68 65 20 74 65 78 74 0a 2a 2a 20 6f 66 20 74 68  he text.** of th
1ff30 65 20 70 61 72 73 69 6e 67 20 65 72 72 6f 72 20  e parsing error 
1ff40 74 68 61 74 20 72 65 73 75 6c 74 73 20 69 6e 20  that results in 
1ff50 61 6e 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d  an [SQLITE_SCHEM
1ff60 41 5d 20 72 65 74 75 72 6e 2e 0a 2a 2a 20 3c 2f  A] return..** </
1ff70 6c 69 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e 0a 2a  li>.**.** <li>.*
1ff80 2a 20 57 68 65 6e 20 61 6e 20 65 72 72 6f 72 20  * When an error 
1ff90 6f 63 63 75 72 73 2c 20 5b 73 71 6c 69 74 65 33  occurs, [sqlite3
1ffa0 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c 20 72 65  _step()] will re
1ffb0 74 75 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20  turn one of the 
1ffc0 64 65 74 61 69 6c 65 64 0a 2a 2a 20 5b 65 72 72  detailed.** [err
1ffd0 6f 72 20 63 6f 64 65 73 5d 20 6f 72 20 5b 65 78  or codes] or [ex
1ffe0 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64  tended error cod
1fff0 65 73 5d 2e 20 20 54 68 65 20 6c 65 67 61 63 79  es].  The legacy
20000 20 62 65 68 61 76 69 6f 72 20 77 61 73 20 74 68   behavior was th
20010 61 74 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73  at.** [sqlite3_s
20020 74 65 70 28 29 5d 20 77 6f 75 6c 64 20 6f 6e 6c  tep()] would onl
20030 79 20 72 65 74 75 72 6e 20 61 20 67 65 6e 65 72  y return a gener
20040 69 63 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52  ic [SQLITE_ERROR
20050 5d 20 72 65 73 75 6c 74 20 63 6f 64 65 0a 2a 2a  ] result code.**
20060 20 61 6e 64 20 79 6f 75 20 77 6f 75 6c 64 20 68   and you would h
20070 61 76 65 20 74 6f 20 6d 61 6b 65 20 61 20 73 65  ave to make a se
20080 63 6f 6e 64 20 63 61 6c 6c 20 74 6f 20 5b 73 71  cond call to [sq
20090 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 69  lite3_reset()] i
200a0 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 66 69  n order.** to fi
200b0 6e 64 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  nd the underlyin
200c0 67 20 63 61 75 73 65 20 6f 66 20 74 68 65 20 70  g cause of the p
200d0 72 6f 62 6c 65 6d 2e 20 57 69 74 68 20 74 68 65  roblem. With the
200e0 20 22 76 32 22 20 70 72 65 70 61 72 65 0a 2a 2a   "v2" prepare.**
200f0 20 69 6e 74 65 72 66 61 63 65 73 2c 20 74 68 65   interfaces, the
20100 20 75 6e 64 65 72 6c 79 69 6e 67 20 72 65 61 73   underlying reas
20110 6f 6e 20 66 6f 72 20 74 68 65 20 65 72 72 6f 72  on for the error
20120 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d   is returned imm
20130 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 20 3c 2f 6c  ediately..** </l
20140 69 3e 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a  i>.** </ol>.**.*
20150 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
20160 2a 2a 20 5b 48 31 33 30 31 31 5d 20 5b 48 31 33  ** [H13011] [H13
20170 30 31 32 5d 20 5b 48 31 33 30 31 33 5d 20 5b 48  012] [H13013] [H
20180 31 33 30 31 34 5d 20 5b 48 31 33 30 31 35 5d 20  13014] [H13015] 
20190 5b 48 31 33 30 31 36 5d 20 5b 48 31 33 30 31 39  [H13016] [H13019
201a0 5d 20 5b 48 31 33 30 32 31 5d 0a 2a 2a 0a 2a 2f  ] [H13021].**.*/
201b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
201c0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
201d0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
201e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
201f0 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f  tabase handle */
20200 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
20210 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51  Sql,       /* SQ
20220 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46  L statement, UTF
20230 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20  -8 encoded */.  
20240 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20  int nByte,      
20250 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d          /* Maxim
20260 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71  um length of zSq
20270 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20  l in bytes. */. 
20280 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
20290 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a  ppStmt,  /* OUT:
202a0 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c   Statement handl
202b0 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
202c0 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f  r **pzTail     /
202d0 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74  * OUT: Pointer t
202e0 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e  o unused portion
202f0 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53   of zSql */.);.S
20300 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
20310 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
20320 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
20330 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
20340 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a  atabase handle *
20350 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
20360 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53  zSql,       /* S
20370 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54  QL statement, UT
20380 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20  F-8 encoded */. 
20390 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20   int nByte,     
203a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69           /* Maxi
203b0 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53  mum length of zS
203c0 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a  ql in bytes. */.
203d0 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
203e0 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54  *ppStmt,  /* OUT
203f0 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64  : Statement hand
20400 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  le */.  const ch
20410 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20  ar **pzTail     
20420 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20  /* OUT: Pointer 
20430 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f  to unused portio
20440 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a  n of zSql */.);.
20450 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
20460 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
20470 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
20480 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
20490 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a  atabase handle *
204a0 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
204b0 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53  zSql,       /* S
204c0 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54  QL statement, UT
204d0 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 2a 2f 0a  F-16 encoded */.
204e0 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20    int nByte,    
204f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
20500 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a  imum length of z
20510 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f  Sql in bytes. */
20520 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
20530 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55  **ppStmt,  /* OU
20540 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e  T: Statement han
20550 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  dle */.  const v
20560 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20  oid **pzTail    
20570 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72   /* OUT: Pointer
20580 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69   to unused porti
20590 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b  on of zSql */.);
205a0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
205b0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
205c0 36 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20  6_v2(.  sqlite3 
205d0 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
205e0 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64  /* Database hand
205f0 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  le */.  const vo
20600 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20  id *zSql,       
20610 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  /* SQL statement
20620 2c 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64  , UTF-16 encoded
20630 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c   */.  int nByte,
20640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
20650 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20   Maximum length 
20660 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73  of zSql in bytes
20670 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  . */.  sqlite3_s
20680 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f  tmt **ppStmt,  /
20690 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74  * OUT: Statement
206a0 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e   handle */.  con
206b0 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c  st void **pzTail
206c0 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69       /* OUT: Poi
206d0 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70  nter to unused p
206e0 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a  ortion of zSql *
206f0 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  /.);../*.** CAPI
20700 33 52 45 46 3a 20 52 65 74 72 69 65 76 69 6e 67  3REF: Retrieving
20710 20 53 74 61 74 65 6d 65 6e 74 20 53 51 4c 20 7b   Statement SQL {
20720 48 31 33 31 30 30 7d 20 3c 48 31 33 30 30 30 3e  H13100} <H13000>
20730 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65  .**.** This inte
20740 72 66 61 63 65 20 63 61 6e 20 62 65 20 75 73 65  rface can be use
20750 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 61 20  d to retrieve a 
20760 73 61 76 65 64 20 63 6f 70 79 20 6f 66 20 74 68  saved copy of th
20770 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 53 51  e original.** SQ
20780 4c 20 74 65 78 74 20 75 73 65 64 20 74 6f 20 63  L text used to c
20790 72 65 61 74 65 20 61 20 5b 70 72 65 70 61 72 65  reate a [prepare
207a0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 66 20  d statement] if 
207b0 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 20 77  that statement w
207c0 61 73 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 75  as.** compiled u
207d0 73 69 6e 67 20 65 69 74 68 65 72 20 5b 73 71 6c  sing either [sql
207e0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
207f0 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70  )] or [sqlite3_p
20800 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a  repare16_v2()]..
20810 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
20820 74 73 3a 0a 2a 2a 20 5b 48 31 33 31 30 31 5d 20  ts:.** [H13101] 
20830 5b 48 31 33 31 30 32 5d 20 5b 48 31 33 31 30 33  [H13102] [H13103
20840 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
20850 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
20860 74 65 33 5f 73 71 6c 28 73 71 6c 69 74 65 33 5f  te3_sql(sqlite3_
20870 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f  stmt *pStmt);../
20880 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44  *.** CAPI3REF: D
20890 79 6e 61 6d 69 63 61 6c 6c 79 20 54 79 70 65 64  ynamically Typed
208a0 20 56 61 6c 75 65 20 4f 62 6a 65 63 74 20 7b 48   Value Object {H
208b0 31 35 30 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a  15000} <S20200>.
208c0 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 70 72  ** KEYWORDS: {pr
208d0 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
208e0 76 61 6c 75 65 7d 20 7b 75 6e 70 72 6f 74 65 63  value} {unprotec
208f0 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ted sqlite3_valu
20900 65 7d 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  e}.**.** SQLite 
20910 75 73 65 73 20 74 68 65 20 73 71 6c 69 74 65 33  uses the sqlite3
20920 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 74 6f  _value object to
20930 20 72 65 70 72 65 73 65 6e 74 20 61 6c 6c 20 76   represent all v
20940 61 6c 75 65 73 0a 2a 2a 20 74 68 61 74 20 63 61  alues.** that ca
20950 6e 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 61  n be stored in a
20960 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e   database table.
20970 20 53 51 4c 69 74 65 20 75 73 65 73 20 64 79 6e   SQLite uses dyn
20980 61 6d 69 63 20 74 79 70 69 6e 67 0a 2a 2a 20 66  amic typing.** f
20990 6f 72 20 74 68 65 20 76 61 6c 75 65 73 20 69 74  or the values it
209a0 20 73 74 6f 72 65 73 2e 20 56 61 6c 75 65 73 20   stores. Values 
209b0 73 74 6f 72 65 64 20 69 6e 20 73 71 6c 69 74 65  stored in sqlite
209c0 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 0a  3_value objects.
209d0 2a 2a 20 63 61 6e 20 62 65 20 69 6e 74 65 67 65  ** can be intege
209e0 72 73 2c 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  rs, floating poi
209f0 6e 74 20 76 61 6c 75 65 73 2c 20 73 74 72 69 6e  nt values, strin
20a00 67 73 2c 20 42 4c 4f 42 73 2c 20 6f 72 20 4e 55  gs, BLOBs, or NU
20a10 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 73 71 6c  LL..**.** An sql
20a20 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63  ite3_value objec
20a30 74 20 6d 61 79 20 62 65 20 65 69 74 68 65 72 20  t may be either 
20a40 22 70 72 6f 74 65 63 74 65 64 22 20 6f 72 20 22  "protected" or "
20a50 75 6e 70 72 6f 74 65 63 74 65 64 22 2e 0a 2a 2a  unprotected"..**
20a60 20 53 6f 6d 65 20 69 6e 74 65 72 66 61 63 65 73   Some interfaces
20a70 20 72 65 71 75 69 72 65 20 61 20 70 72 6f 74 65   require a prote
20a80 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c  cted sqlite3_val
20a90 75 65 2e 20 20 4f 74 68 65 72 20 69 6e 74 65 72  ue.  Other inter
20aa0 66 61 63 65 73 0a 2a 2a 20 77 69 6c 6c 20 61 63  faces.** will ac
20ab0 63 65 70 74 20 65 69 74 68 65 72 20 61 20 70 72  cept either a pr
20ac0 6f 74 65 63 74 65 64 20 6f 72 20 61 6e 20 75 6e  otected or an un
20ad0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
20ae0 33 5f 76 61 6c 75 65 2e 0a 2a 2a 20 45 76 65 72  3_value..** Ever
20af0 79 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74  y interface that
20b00 20 61 63 63 65 70 74 73 20 73 71 6c 69 74 65 33   accepts sqlite3
20b10 5f 76 61 6c 75 65 20 61 72 67 75 6d 65 6e 74 73  _value arguments
20b20 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 77 68   specifies.** wh
20b30 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 69 74 20  ether or not it 
20b40 72 65 71 75 69 72 65 73 20 61 20 70 72 6f 74 65  requires a prote
20b50 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c  cted sqlite3_val
20b60 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 65  ue..**.** The te
20b70 72 6d 73 20 22 70 72 6f 74 65 63 74 65 64 22 20  rms "protected" 
20b80 61 6e 64 20 22 75 6e 70 72 6f 74 65 63 74 65 64  and "unprotected
20b90 22 20 72 65 66 65 72 20 74 6f 20 77 68 65 74 68  " refer to wheth
20ba0 65 72 20 6f 72 20 6e 6f 74 0a 2a 2a 20 61 20 6d  er or not.** a m
20bb0 75 74 65 78 20 69 73 20 68 65 6c 64 2e 20 20 41  utex is held.  A
20bc0 20 69 6e 74 65 72 6e 61 6c 20 6d 75 74 65 78 20   internal mutex 
20bd0 69 73 20 68 65 6c 64 20 66 6f 72 20 61 20 70 72  is held for a pr
20be0 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74  otected.** sqlit
20bf0 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20  e3_value object 
20c00 62 75 74 20 6e 6f 20 6d 75 74 65 78 20 69 73 20  but no mutex is 
20c10 68 65 6c 64 20 66 6f 72 20 61 6e 20 75 6e 70 72  held for an unpr
20c20 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74  otected.** sqlit
20c30 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2e  e3_value object.
20c40 20 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 63    If SQLite is c
20c50 6f 6d 70 69 6c 65 64 20 74 6f 20 62 65 20 73 69  ompiled to be si
20c60 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 0a 2a 2a  ngle-threaded.**
20c70 20 28 77 69 74 68 20 5b 53 51 4c 49 54 45 5f 54   (with [SQLITE_T
20c80 48 52 45 41 44 53 41 46 45 3d 30 5d 20 61 6e 64  HREADSAFE=0] and
20c90 20 77 69 74 68 20 5b 73 71 6c 69 74 65 33 5f 74   with [sqlite3_t
20ca0 68 72 65 61 64 73 61 66 65 28 29 5d 20 72 65 74  hreadsafe()] ret
20cb0 75 72 6e 69 6e 67 20 30 29 0a 2a 2a 20 6f 72 20  urning 0).** or 
20cc0 69 66 20 53 51 4c 69 74 65 20 69 73 20 72 75 6e  if SQLite is run
20cd0 20 69 6e 20 6f 6e 65 20 6f 66 20 72 65 64 75 63   in one of reduc
20ce0 65 64 20 6d 75 74 65 78 20 6d 6f 64 65 73 20 0a  ed mutex modes .
20cf0 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ** [SQLITE_CONFI
20d00 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 5d 20  G_SINGLETHREAD] 
20d10 6f 72 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  or [SQLITE_CONFI
20d20 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 5d 0a 2a  G_MULTITHREAD].*
20d30 2a 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20  * then there is 
20d40 6e 6f 20 64 69 73 74 69 6e 63 74 69 6f 6e 20 62  no distinction b
20d50 65 74 77 65 65 6e 20 70 72 6f 74 65 63 74 65 64  etween protected
20d60 20 61 6e 64 20 75 6e 70 72 6f 74 65 63 74 65 64   and unprotected
20d70 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  .** sqlite3_valu
20d80 65 20 6f 62 6a 65 63 74 73 20 61 6e 64 20 74 68  e objects and th
20d90 65 79 20 63 61 6e 20 62 65 20 75 73 65 64 20 69  ey can be used i
20da0 6e 74 65 72 63 68 61 6e 67 65 61 62 6c 79 2e 20  nterchangeably. 
20db0 20 48 6f 77 65 76 65 72 2c 0a 2a 2a 20 66 6f 72   However,.** for
20dc0 20 6d 61 78 69 6d 75 6d 20 63 6f 64 65 20 70 6f   maximum code po
20dd0 72 74 61 62 69 6c 69 74 79 20 69 74 20 69 73 20  rtability it is 
20de0 72 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74  recommended that
20df0 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a   applications.**
20e00 20 73 74 69 6c 6c 20 6d 61 6b 65 20 74 68 65 20   still make the 
20e10 64 69 73 74 69 6e 63 74 69 6f 6e 20 62 65 74 77  distinction betw
20e20 65 65 6e 20 62 65 74 77 65 65 6e 20 70 72 6f 74  een between prot
20e30 65 63 74 65 64 20 61 6e 64 20 75 6e 70 72 6f 74  ected and unprot
20e40 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33  ected.** sqlite3
20e50 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 65  _value objects e
20e60 76 65 6e 20 77 68 65 6e 20 6e 6f 74 20 73 74 72  ven when not str
20e70 69 63 74 6c 79 20 72 65 71 75 69 72 65 64 2e 0a  ictly required..
20e80 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
20e90 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20  3_value objects 
20ea0 74 68 61 74 20 61 72 65 20 70 61 73 73 65 64 20  that are passed 
20eb0 61 73 20 70 61 72 61 6d 65 74 65 72 73 20 69 6e  as parameters in
20ec0 74 6f 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d  to the.** implem
20ed0 65 6e 74 61 74 69 6f 6e 20 6f 66 20 5b 61 70 70  entation of [app
20ee0 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
20ef0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 5d 20   SQL functions] 
20f00 61 72 65 20 70 72 6f 74 65 63 74 65 64 2e 0a 2a  are protected..*
20f10 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61  * The sqlite3_va
20f20 6c 75 65 20 6f 62 6a 65 63 74 20 72 65 74 75 72  lue object retur
20f30 6e 65 64 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74  ned by.** [sqlit
20f40 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28  e3_column_value(
20f50 29 5d 20 69 73 20 75 6e 70 72 6f 74 65 63 74 65  )] is unprotecte
20f60 64 2e 0a 2a 2a 20 55 6e 70 72 6f 74 65 63 74 65  d..** Unprotecte
20f70 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  d sqlite3_value 
20f80 6f 62 6a 65 63 74 73 20 6d 61 79 20 6f 6e 6c 79  objects may only
20f90 20 62 65 20 75 73 65 64 20 77 69 74 68 0a 2a 2a   be used with.**
20fa0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74   [sqlite3_result
20fb0 5f 76 61 6c 75 65 28 29 5d 20 61 6e 64 20 5b 73  _value()] and [s
20fc0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75  qlite3_bind_valu
20fd0 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b 73 71  e()]..** The [sq
20fe0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
20ff0 20 7c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   | sqlite3_value
21000 5f 74 79 70 65 28 29 5d 20 66 61 6d 69 6c 79 20  _type()] family 
21010 6f 66 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73  of.** interfaces
21020 20 72 65 71 75 69 72 65 20 70 72 6f 74 65 63 74   require protect
21030 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
21040 20 6f 62 6a 65 63 74 73 2e 0a 2a 2f 0a 74 79 70   objects..*/.typ
21050 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d 20  edef struct Mem 
21060 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 3b 0a 0a  sqlite3_value;..
21070 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
21080 53 51 4c 20 46 75 6e 63 74 69 6f 6e 20 43 6f 6e  SQL Function Con
21090 74 65 78 74 20 4f 62 6a 65 63 74 20 7b 48 31 36  text Object {H16
210a0 30 30 31 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a  001} <S20200>.**
210b0 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 78 74 20  .** The context 
210c0 69 6e 20 77 68 69 63 68 20 61 6e 20 53 51 4c 20  in which an SQL 
210d0 66 75 6e 63 74 69 6f 6e 20 65 78 65 63 75 74 65  function execute
210e0 73 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61  s is stored in a
210f0 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e  n.** sqlite3_con
21100 74 65 78 74 20 6f 62 6a 65 63 74 2e 20 20 41 20  text object.  A 
21110 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71  pointer to an sq
21120 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 6f 62  lite3_context ob
21130 6a 65 63 74 0a 2a 2a 20 69 73 20 61 6c 77 61 79  ject.** is alway
21140 73 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  s first paramete
21150 72 20 74 6f 20 5b 61 70 70 6c 69 63 61 74 69 6f  r to [applicatio
21160 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75  n-defined SQL fu
21170 6e 63 74 69 6f 6e 73 5d 2e 0a 2a 2a 20 54 68 65  nctions]..** The
21180 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66   application-def
21190 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f  ined SQL functio
211a0 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
211b0 20 77 69 6c 6c 20 70 61 73 73 20 74 68 69 73 0a   will pass this.
211c0 2a 2a 20 70 6f 69 6e 74 65 72 20 74 68 72 6f 75  ** pointer throu
211d0 67 68 20 69 6e 74 6f 20 63 61 6c 6c 73 20 74 6f  gh into calls to
211e0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74   [sqlite3_result
211f0 5f 69 6e 74 20 7c 20 73 71 6c 69 74 65 33 5f 72  _int | sqlite3_r
21200 65 73 75 6c 74 28 29 5d 2c 0a 2a 2a 20 5b 73 71  esult()],.** [sq
21210 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f  lite3_aggregate_
21220 63 6f 6e 74 65 78 74 28 29 5d 2c 20 5b 73 71 6c  context()], [sql
21230 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29  ite3_user_data()
21240 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63  ],.** [sqlite3_c
21250 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
21260 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 67 65  ()], [sqlite3_ge
21270 74 5f 61 75 78 64 61 74 61 28 29 5d 2c 0a 2a 2a  t_auxdata()],.**
21280 20 61 6e 64 2f 6f 72 20 5b 73 71 6c 69 74 65 33   and/or [sqlite3
21290 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 5d 2e  _set_auxdata()].
212a0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
212b0 63 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ct sqlite3_conte
212c0 78 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  xt sqlite3_conte
212d0 78 74 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  xt;../*.** CAPI3
212e0 52 45 46 3a 20 42 69 6e 64 69 6e 67 20 56 61 6c  REF: Binding Val
212f0 75 65 73 20 54 6f 20 50 72 65 70 61 72 65 64 20  ues To Prepared 
21300 53 74 61 74 65 6d 65 6e 74 73 20 7b 48 31 33 35  Statements {H135
21310 30 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 20  00} <S70300>.** 
21320 4b 45 59 57 4f 52 44 53 3a 20 7b 68 6f 73 74 20  KEYWORDS: {host 
21330 70 61 72 61 6d 65 74 65 72 7d 20 7b 68 6f 73 74  parameter} {host
21340 20 70 61 72 61 6d 65 74 65 72 73 7d 20 7b 68 6f   parameters} {ho
21350 73 74 20 70 61 72 61 6d 65 74 65 72 20 6e 61 6d  st parameter nam
21360 65 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  e}.** KEYWORDS: 
21370 7b 53 51 4c 20 70 61 72 61 6d 65 74 65 72 7d 20  {SQL parameter} 
21380 7b 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 7d  {SQL parameters}
21390 20 7b 70 61 72 61 6d 65 74 65 72 20 62 69 6e 64   {parameter bind
213a0 69 6e 67 7d 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68  ing}.**.** In th
213b0 65 20 53 51 4c 20 73 74 72 69 6e 67 73 20 69 6e  e SQL strings in
213c0 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  put to [sqlite3_
213d0 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e  prepare_v2()] an
213e0 64 20 69 74 73 20 76 61 72 69 61 6e 74 73 2c 0a  d its variants,.
213f0 2a 2a 20 6c 69 74 65 72 61 6c 73 20 6d 61 79 20  ** literals may 
21400 62 65 20 72 65 70 6c 61 63 65 64 20 62 79 20 61  be replaced by a
21410 20 5b 70 61 72 61 6d 65 74 65 72 5d 20 74 68 61   [parameter] tha
21420 74 20 6d 61 74 63 68 65 73 20 6f 6e 65 20 6f 66  t matches one of
21430 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 74 65   following.** te
21440 6d 70 6c 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c  mplates:.**.** <
21450 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 0a 2a  ul>.** <li>  ?.*
21460 2a 20 3c 6c 69 3e 20 20 3f 4e 4e 4e 0a 2a 2a 20  * <li>  ?NNN.** 
21470 3c 6c 69 3e 20 20 3a 56 56 56 0a 2a 2a 20 3c 6c  <li>  :VVV.** <l
21480 69 3e 20 20 40 56 56 56 0a 2a 2a 20 3c 6c 69 3e  i>  @VVV.** <li>
21490 20 20 24 56 56 56 0a 2a 2a 20 3c 2f 75 6c 3e 0a    $VVV.** </ul>.
214a0 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 74 65 6d  **.** In the tem
214b0 70 6c 61 74 65 73 20 61 62 6f 76 65 2c 20 4e 4e  plates above, NN
214c0 4e 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20  N represents an 
214d0 69 6e 74 65 67 65 72 20 6c 69 74 65 72 61 6c 2c  integer literal,
214e0 0a 2a 2a 20 61 6e 64 20 56 56 56 20 72 65 70 72  .** and VVV repr
214f0 65 73 65 6e 74 73 20 61 6e 20 61 6c 70 68 61 6e  esents an alphan
21500 75 6d 65 72 69 63 20 69 64 65 6e 74 69 66 65 72  umeric identifer
21510 2e 20 20 54 68 65 20 76 61 6c 75 65 73 20 6f 66  .  The values of
21520 20 74 68 65 73 65 0a 2a 2a 20 70 61 72 61 6d 65   these.** parame
21530 74 65 72 73 20 28 61 6c 73 6f 20 63 61 6c 6c 65  ters (also calle
21540 64 20 22 68 6f 73 74 20 70 61 72 61 6d 65 74 65  d "host paramete
21550 72 20 6e 61 6d 65 73 22 20 6f 72 20 22 53 51 4c  r names" or "SQL
21560 20 70 61 72 61 6d 65 74 65 72 73 22 29 0a 2a 2a   parameters").**
21570 20 63 61 6e 20 62 65 20 73 65 74 20 75 73 69 6e   can be set usin
21580 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69  g the sqlite3_bi
21590 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20  nd_*() routines 
215a0 64 65 66 69 6e 65 64 20 68 65 72 65 2e 0a 2a 2a  defined here..**
215b0 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72  .** The first ar
215c0 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 73 71  gument to the sq
215d0 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72  lite3_bind_*() r
215e0 6f 75 74 69 6e 65 73 20 69 73 20 61 6c 77 61 79  outines is alway
215f0 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74  s.** a pointer t
21600 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73  o the [sqlite3_s
21610 74 6d 74 5d 20 6f 62 6a 65 63 74 20 72 65 74 75  tmt] object retu
21620 72 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 5b 73 71  rned from.** [sq
21630 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
21640 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72 69 61  ()] or its varia
21650 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  nts..**.** The s
21660 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69  econd argument i
21670 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  s the index of t
21680 68 65 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72  he SQL parameter
21690 20 74 6f 20 62 65 20 73 65 74 2e 0a 2a 2a 20 54   to be set..** T
216a0 68 65 20 6c 65 66 74 6d 6f 73 74 20 53 51 4c 20  he leftmost SQL 
216b0 70 61 72 61 6d 65 74 65 72 20 68 61 73 20 61 6e  parameter has an
216c0 20 69 6e 64 65 78 20 6f 66 20 31 2e 20 20 57 68   index of 1.  Wh
216d0 65 6e 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65  en the same name
216e0 64 0a 2a 2a 20 53 51 4c 20 70 61 72 61 6d 65 74  d.** SQL paramet
216f0 65 72 20 69 73 20 75 73 65 64 20 6d 6f 72 65 20  er is used more 
21700 74 68 61 6e 20 6f 6e 63 65 2c 20 73 65 63 6f 6e  than once, secon
21710 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74  d and subsequent
21720 0a 2a 2a 20 6f 63 63 75 72 72 65 6e 63 65 73 20  .** occurrences 
21730 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 69 6e  have the same in
21740 64 65 78 20 61 73 20 74 68 65 20 66 69 72 73 74  dex as the first
21750 20 6f 63 63 75 72 72 65 6e 63 65 2e 0a 2a 2a 20   occurrence..** 
21760 54 68 65 20 69 6e 64 65 78 20 66 6f 72 20 6e 61  The index for na
21770 6d 65 64 20 70 61 72 61 6d 65 74 65 72 73 20 63  med parameters c
21780 61 6e 20 62 65 20 6c 6f 6f 6b 65 64 20 75 70 20  an be looked up 
21790 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71  using the.** [sq
217a0 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
217b0 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 20 41 50  eter_index()] AP
217c0 49 20 69 66 20 64 65 73 69 72 65 64 2e 20 20 54  I if desired.  T
217d0 68 65 20 69 6e 64 65 78 0a 2a 2a 20 66 6f 72 20  he index.** for 
217e0 22 3f 4e 4e 4e 22 20 70 61 72 61 6d 65 74 65 72  "?NNN" parameter
217f0 73 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f  s is the value o
21800 66 20 4e 4e 4e 2e 0a 2a 2a 20 54 68 65 20 4e 4e  f NNN..** The NN
21810 4e 20 76 61 6c 75 65 20 6d 75 73 74 20 62 65 20  N value must be 
21820 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 74 68  between 1 and th
21830 65 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74  e [sqlite3_limit
21840 28 29 5d 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  ()].** parameter
21850 20 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56   [SQLITE_LIMIT_V
21860 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20  ARIABLE_NUMBER] 
21870 28 64 65 66 61 75 6c 74 20 76 61 6c 75 65 3a 20  (default value: 
21880 39 39 39 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  999)..**.** The 
21890 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69  third argument i
218a0 73 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 62  s the value to b
218b0 69 6e 64 20 74 6f 20 74 68 65 20 70 61 72 61 6d  ind to the param
218c0 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74  eter..**.** In t
218d0 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20 74 68  hose routines th
218e0 61 74 20 68 61 76 65 20 61 20 66 6f 75 72 74 68  at have a fourth
218f0 20 61 72 67 75 6d 65 6e 74 2c 20 69 74 73 20 76   argument, its v
21900 61 6c 75 65 20 69 73 20 74 68 65 0a 2a 2a 20 6e  alue is the.** n
21910 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
21920 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 2e  n the parameter.
21930 20 20 54 6f 20 62 65 20 63 6c 65 61 72 3a 20 74    To be clear: t
21940 68 65 20 76 61 6c 75 65 20 69 73 20 74 68 65 0a  he value is the.
21950 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 3c 75 3e  ** number of <u>
21960 62 79 74 65 73 3c 2f 75 3e 20 69 6e 20 74 68 65  bytes</u> in the
21970 20 76 61 6c 75 65 2c 20 6e 6f 74 20 74 68 65 20   value, not the 
21980 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63  number of charac
21990 74 65 72 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20  ters..** If the 
219a0 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72  fourth parameter
219b0 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68   is negative, th
219c0 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
219d0 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 74 68 65  string is.** the
219e0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
219f0 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73 74   up to the first
21a00 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72   zero terminator
21a10 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 66 74  ..**.** The fift
21a20 68 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71  h argument to sq
21a30 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28  lite3_bind_blob(
21a40 29 2c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ), sqlite3_bind_
21a50 74 65 78 74 28 29 2c 20 61 6e 64 0a 2a 2a 20 73  text(), and.** s
21a60 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
21a70 31 36 28 29 20 69 73 20 61 20 64 65 73 74 72 75  16() is a destru
21a80 63 74 6f 72 20 75 73 65 64 20 74 6f 20 64 69 73  ctor used to dis
21a90 70 6f 73 65 20 6f 66 20 74 68 65 20 42 4c 4f 42  pose of the BLOB
21aa0 20 6f 72 0a 2a 2a 20 73 74 72 69 6e 67 20 61 66   or.** string af
21ab0 74 65 72 20 53 51 4c 69 74 65 20 68 61 73 20 66  ter SQLite has f
21ac0 69 6e 69 73 68 65 64 20 77 69 74 68 20 69 74 2e  inished with it.
21ad0 20 49 66 20 74 68 65 20 66 69 66 74 68 20 61 72   If the fifth ar
21ae0 67 75 6d 65 6e 74 20 69 73 0a 2a 2a 20 74 68 65  gument is.** the
21af0 20 73 70 65 63 69 61 6c 20 76 61 6c 75 65 20 5b   special value [
21b00 53 51 4c 49 54 45 5f 53 54 41 54 49 43 5d 2c 20  SQLITE_STATIC], 
21b10 74 68 65 6e 20 53 51 4c 69 74 65 20 61 73 73 75  then SQLite assu
21b20 6d 65 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20  mes that the.** 
21b30 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 69  information is i
21b40 6e 20 73 74 61 74 69 63 2c 20 75 6e 6d 61 6e 61  n static, unmana
21b50 67 65 64 20 73 70 61 63 65 20 61 6e 64 20 64 6f  ged space and do
21b60 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62  es not need to b
21b70 65 20 66 72 65 65 64 2e 0a 2a 2a 20 49 66 20 74  e freed..** If t
21b80 68 65 20 66 69 66 74 68 20 61 72 67 75 6d 65 6e  he fifth argumen
21b90 74 20 68 61 73 20 74 68 65 20 76 61 6c 75 65 20  t has the value 
21ba0 5b 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e  [SQLITE_TRANSIEN
21bb0 54 5d 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 69  T], then.** SQLi
21bc0 74 65 20 6d 61 6b 65 73 20 69 74 73 20 6f 77 6e  te makes its own
21bd0 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66   private copy of
21be0 20 74 68 65 20 64 61 74 61 20 69 6d 6d 65 64 69   the data immedi
21bf0 61 74 65 6c 79 2c 20 62 65 66 6f 72 65 0a 2a 2a  ately, before.**
21c00 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e   the sqlite3_bin
21c10 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 20 72 65  d_*() routine re
21c20 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  turns..**.** The
21c30 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65   sqlite3_bind_ze
21c40 72 6f 62 6c 6f 62 28 29 20 72 6f 75 74 69 6e 65  roblob() routine
21c50 20 62 69 6e 64 73 20 61 20 42 4c 4f 42 20 6f 66   binds a BLOB of
21c60 20 6c 65 6e 67 74 68 20 4e 20 74 68 61 74 0a 2a   length N that.*
21c70 2a 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68  * is filled with
21c80 20 7a 65 72 6f 65 73 2e 20 20 41 20 7a 65 72 6f   zeroes.  A zero
21c90 62 6c 6f 62 20 75 73 65 73 20 61 20 66 69 78 65  blob uses a fixe
21ca0 64 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f  d amount of memo
21cb0 72 79 0a 2a 2a 20 28 6a 75 73 74 20 61 6e 20 69  ry.** (just an i
21cc0 6e 74 65 67 65 72 20 74 6f 20 68 6f 6c 64 20 69  nteger to hold i
21cd0 74 73 20 73 69 7a 65 29 20 77 68 69 6c 65 20 69  ts size) while i
21ce0 74 20 69 73 20 62 65 69 6e 67 20 70 72 6f 63 65  t is being proce
21cf0 73 73 65 64 2e 0a 2a 2a 20 5a 65 72 6f 62 6c 6f  ssed..** Zeroblo
21d00 62 73 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20  bs are intended 
21d10 74 6f 20 73 65 72 76 65 20 61 73 20 70 6c 61 63  to serve as plac
21d20 65 68 6f 6c 64 65 72 73 20 66 6f 72 20 42 4c 4f  eholders for BLO
21d30 42 73 20 77 68 6f 73 65 0a 2a 2a 20 63 6f 6e 74  Bs whose.** cont
21d40 65 6e 74 20 69 73 20 6c 61 74 65 72 20 77 72 69  ent is later wri
21d50 74 74 65 6e 20 75 73 69 6e 67 0a 2a 2a 20 5b 73  tten using.** [s
21d60 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e  qlite3_blob_open
21d70 20 7c 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42   | incremental B
21d80 4c 4f 42 20 49 2f 4f 5d 20 72 6f 75 74 69 6e 65  LOB I/O] routine
21d90 73 2e 0a 2a 2a 20 41 20 6e 65 67 61 74 69 76 65  s..** A negative
21da0 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 7a   value for the z
21db0 65 72 6f 62 6c 6f 62 20 72 65 73 75 6c 74 73 20  eroblob results 
21dc0 69 6e 20 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68  in a zero-length
21dd0 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   BLOB..**.** The
21de0 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28   sqlite3_bind_*(
21df0 29 20 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20  ) routines must 
21e00 62 65 20 63 61 6c 6c 65 64 20 61 66 74 65 72 0a  be called after.
21e10 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  ** [sqlite3_prep
21e20 61 72 65 5f 76 32 28 29 5d 20 28 61 6e 64 20 69  are_v2()] (and i
21e30 74 73 20 76 61 72 69 61 6e 74 73 29 20 6f 72 20  ts variants) or 
21e40 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29  [sqlite3_reset()
21e50 5d 20 61 6e 64 0a 2a 2a 20 62 65 66 6f 72 65 20  ] and.** before 
21e60 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
21e70 2e 0a 2a 2a 20 42 69 6e 64 69 6e 67 73 20 61 72  ..** Bindings ar
21e80 65 20 6e 6f 74 20 63 6c 65 61 72 65 64 20 62 79  e not cleared by
21e90 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65   the [sqlite3_re
21ea0 73 65 74 28 29 5d 20 72 6f 75 74 69 6e 65 2e 0a  set()] routine..
21eb0 2a 2a 20 55 6e 62 6f 75 6e 64 20 70 61 72 61 6d  ** Unbound param
21ec0 65 74 65 72 73 20 61 72 65 20 69 6e 74 65 72 70  eters are interp
21ed0 72 65 74 65 64 20 61 73 20 4e 55 4c 4c 2e 0a 2a  reted as NULL..*
21ee0 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
21ef0 6e 65 73 20 72 65 74 75 72 6e 20 5b 53 51 4c 49  nes return [SQLI
21f00 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73  TE_OK] on succes
21f10 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f  s or an error co
21f20 64 65 20 69 66 0a 2a 2a 20 61 6e 79 74 68 69 6e  de if.** anythin
21f30 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 20 20 5b  g goes wrong.  [
21f40 53 51 4c 49 54 45 5f 52 41 4e 47 45 5d 20 69 73  SQLITE_RANGE] is
21f50 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65   returned if the
21f60 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 69 6e   parameter.** in
21f70 64 65 78 20 69 73 20 6f 75 74 20 6f 66 20 72 61  dex is out of ra
21f80 6e 67 65 2e 20 20 5b 53 51 4c 49 54 45 5f 4e 4f  nge.  [SQLITE_NO
21f90 4d 45 4d 5d 20 69 73 20 72 65 74 75 72 6e 65 64  MEM] is returned
21fa0 20 69 66 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69   if malloc() fai
21fb0 6c 73 2e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4d  ls..** [SQLITE_M
21fc0 49 53 55 53 45 5d 20 6d 69 67 68 74 20 62 65 20  ISUSE] might be 
21fd0 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 73  returned if thes
21fe0 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63  e routines are c
21ff0 61 6c 6c 65 64 20 6f 6e 20 61 0a 2a 2a 20 76 69  alled on a.** vi
22000 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 74 68  rtual machine th
22010 61 74 20 69 73 20 74 68 65 20 77 72 6f 6e 67 20  at is the wrong 
22020 73 74 61 74 65 20 6f 72 20 77 68 69 63 68 20 68  state or which h
22030 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
22040 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20 44 65  finalized..** De
22050 74 65 63 74 69 6f 6e 20 6f 66 20 6d 69 73 75 73  tection of misus
22060 65 20 69 73 20 75 6e 72 65 6c 69 61 62 6c 65 2e  e is unreliable.
22070 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 73    Applications s
22080 68 6f 75 6c 64 20 6e 6f 74 20 64 65 70 65 6e 64  hould not depend
22090 0a 2a 2a 20 6f 6e 20 53 51 4c 49 54 45 5f 4d 49  .** on SQLITE_MI
220a0 53 55 53 45 20 72 65 74 75 72 6e 73 2e 20 20 53  SUSE returns.  S
220b0 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 69 73 20  QLITE_MISUSE is 
220c0 69 6e 74 65 6e 64 65 64 20 74 6f 20 69 6e 64 69  intended to indi
220d0 63 61 74 65 20 61 0a 2a 2a 20 61 20 6c 6f 67 69  cate a.** a logi
220e0 63 20 65 72 72 6f 72 20 69 6e 20 74 68 65 20 61  c error in the a
220f0 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 46 75 74  pplication.  Fut
22100 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  ure versions of 
22110 53 51 4c 69 74 65 20 6d 69 67 68 74 0a 2a 2a 20  SQLite might.** 
22120 70 61 6e 69 63 20 72 61 74 68 65 72 20 74 68 61  panic rather tha
22130 6e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  n return SQLITE_
22140 4d 49 53 55 53 45 2e 0a 2a 2a 0a 2a 2a 20 53 65  MISUSE..**.** Se
22150 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33  e also: [sqlite3
22160 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
22170 63 6f 75 6e 74 28 29 5d 2c 0a 2a 2a 20 5b 73 71  count()],.** [sq
22180 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
22190 65 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e  eter_name()], an
221a0 64 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  d [sqlite3_bind_
221b0 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28  parameter_index(
221c0 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  )]..**.** Requir
221d0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 35  ements:.** [H135
221e0 30 36 5d 20 5b 48 31 33 35 30 39 5d 20 5b 48 31  06] [H13509] [H1
221f0 33 35 31 32 5d 20 5b 48 31 33 35 31 35 5d 20 5b  3512] [H13515] [
22200 48 31 33 35 31 38 5d 20 5b 48 31 33 35 32 31 5d  H13518] [H13521]
22210 20 5b 48 31 33 35 32 34 5d 20 5b 48 31 33 35 32   [H13524] [H1352
22220 37 5d 0a 2a 2a 20 5b 48 31 33 35 33 30 5d 20 5b  7].** [H13530] [
22230 48 31 33 35 33 33 5d 20 5b 48 31 33 35 33 36 5d  H13533] [H13536]
22240 20 5b 48 31 33 35 33 39 5d 20 5b 48 31 33 35 34   [H13539] [H1354
22250 32 5d 20 5b 48 31 33 35 34 35 5d 20 5b 48 31 33  2] [H13545] [H13
22260 35 34 38 5d 20 5b 48 31 33 35 35 31 5d 0a 2a 2a  548] [H13551].**
22270 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
22280 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
22290 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d  blob(sqlite3_stm
222a0 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76  t*, int, const v
222b0 6f 69 64 2a 2c 20 69 6e 74 20 6e 2c 20 76 6f 69  oid*, int n, voi
222c0 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51  d(*)(void*));.SQ
222d0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
222e0 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65  ite3_bind_double
222f0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20  (sqlite3_stmt*, 
22300 69 6e 74 2c 20 64 6f 75 62 6c 65 29 3b 0a 53 51  int, double);.SQ
22310 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
22320 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 71  ite3_bind_int(sq
22330 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
22340 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41  , int);.SQLITE_A
22350 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
22360 69 6e 64 5f 69 6e 74 36 34 28 73 71 6c 69 74 65  ind_int64(sqlite
22370 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 73 71  3_stmt*, int, sq
22380 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51  lite3_int64);.SQ
22390 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
223a0 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 73  ite3_bind_null(s
223b0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
223c0 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  t);.SQLITE_API i
223d0 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
223e0 74 65 78 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  text(sqlite3_stm
223f0 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63  t*, int, const c
22400 68 61 72 2a 2c 20 69 6e 74 20 6e 2c 20 76 6f 69  har*, int n, voi
22410 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51  d(*)(void*));.SQ
22420 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
22430 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36  ite3_bind_text16
22440 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20  (sqlite3_stmt*, 
22450 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a  int, const void*
22460 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76  , int, void(*)(v
22470 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41  oid*));.SQLITE_A
22480 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
22490 69 6e 64 5f 76 61 6c 75 65 28 73 71 6c 69 74 65  ind_value(sqlite
224a0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f  3_stmt*, int, co
224b0 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  nst sqlite3_valu
224c0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  e*);.SQLITE_API 
224d0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
224e0 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65  _zeroblob(sqlite
224f0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 69 6e  3_stmt*, int, in
22500 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  t n);../*.** CAP
22510 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 4f 66  I3REF: Number Of
22520 20 53 51 4c 20 50 61 72 61 6d 65 74 65 72 73 20   SQL Parameters 
22530 7b 48 31 33 36 30 30 7d 20 3c 53 37 30 33 30 30  {H13600} <S70300
22540 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  >.**.** This rou
22550 74 69 6e 65 20 63 61 6e 20 62 65 20 75 73 65 64  tine can be used
22560 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 75 6d   to find the num
22570 62 65 72 20 6f 66 20 5b 53 51 4c 20 70 61 72 61  ber of [SQL para
22580 6d 65 74 65 72 73 5d 0a 2a 2a 20 69 6e 20 61 20  meters].** in a 
22590 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
225a0 65 6e 74 5d 2e 20 20 53 51 4c 20 70 61 72 61 6d  ent].  SQL param
225b0 65 74 65 72 73 20 61 72 65 20 74 6f 6b 65 6e 73  eters are tokens
225c0 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 72 6d 20   of the.** form 
225d0 22 3f 22 2c 20 22 3f 4e 4e 4e 22 2c 20 22 3a 41  "?", "?NNN", ":A
225e0 41 41 22 2c 20 22 24 41 41 41 22 2c 20 6f 72 20  AA", "$AAA", or 
225f0 22 40 41 41 41 22 20 74 68 61 74 20 73 65 72 76  "@AAA" that serv
22600 65 20 61 73 0a 2a 2a 20 70 6c 61 63 65 68 6f 6c  e as.** placehol
22610 64 65 72 73 20 66 6f 72 20 76 61 6c 75 65 73 20  ders for values 
22620 74 68 61 74 20 61 72 65 20 5b 73 71 6c 69 74 65  that are [sqlite
22630 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 62 6f  3_bind_blob | bo
22640 75 6e 64 5d 0a 2a 2a 20 74 6f 20 74 68 65 20 70  und].** to the p
22650 61 72 61 6d 65 74 65 72 73 20 61 74 20 61 20 6c  arameters at a l
22660 61 74 65 72 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a  ater time..**.**
22670 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63   This routine ac
22680 74 75 61 6c 6c 79 20 72 65 74 75 72 6e 73 20 74  tually returns t
22690 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
226a0 6c 61 72 67 65 73 74 20 28 72 69 67 68 74 6d 6f  largest (rightmo
226b0 73 74 29 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  st).** parameter
226c0 2e 20 46 6f 72 20 61 6c 6c 20 66 6f 72 6d 73 20  . For all forms 
226d0 65 78 63 65 70 74 20 3f 4e 4e 4e 2c 20 74 68 69  except ?NNN, thi
226e0 73 20 77 69 6c 6c 20 63 6f 72 72 65 73 70 6f 6e  s will correspon
226f0 64 20 74 6f 20 74 68 65 0a 2a 2a 20 6e 75 6d 62  d to the.** numb
22700 65 72 20 6f 66 20 75 6e 69 71 75 65 20 70 61 72  er of unique par
22710 61 6d 65 74 65 72 73 2e 20 20 49 66 20 70 61 72  ameters.  If par
22720 61 6d 65 74 65 72 73 20 6f 66 20 74 68 65 20 3f  ameters of the ?
22730 4e 4e 4e 20 61 72 65 20 75 73 65 64 2c 0a 2a 2a  NNN are used,.**
22740 20 74 68 65 72 65 20 6d 61 79 20 62 65 20 67 61   there may be ga
22750 70 73 20 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a  ps in the list..
22760 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
22770 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c  [sqlite3_bind_bl
22780 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28  ob|sqlite3_bind(
22790 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  )],.** [sqlite3_
227a0 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e  bind_parameter_n
227b0 61 6d 65 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b  ame()], and.** [
227c0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
227d0 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e  ameter_index()].
227e0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
227f0 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 30 31 5d  nts:.** [H13601]
22800 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
22810 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
22820 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28  parameter_count(
22830 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a  sqlite3_stmt*);.
22840 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
22850 20 4e 61 6d 65 20 4f 66 20 41 20 48 6f 73 74 20   Name Of A Host 
22860 50 61 72 61 6d 65 74 65 72 20 7b 48 31 33 36 32  Parameter {H1362
22870 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a  0} <S70300>.**.*
22880 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
22890 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
228a0 20 74 6f 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   to the name of 
228b0 74 68 65 20 6e 2d 74 68 0a 2a 2a 20 5b 53 51 4c  the n-th.** [SQL
228c0 20 70 61 72 61 6d 65 74 65 72 5d 20 69 6e 20 61   parameter] in a
228d0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
228e0 6d 65 6e 74 5d 2e 0a 2a 2a 20 53 51 4c 20 70 61  ment]..** SQL pa
228f0 72 61 6d 65 74 65 72 73 20 6f 66 20 74 68 65 20  rameters of the 
22900 66 6f 72 6d 20 22 3f 4e 4e 4e 22 20 6f 72 20 22  form "?NNN" or "
22910 3a 41 41 41 22 20 6f 72 20 22 40 41 41 41 22 20  :AAA" or "@AAA" 
22920 6f 72 20 22 24 41 41 41 22 0a 2a 2a 20 68 61 76  or "$AAA".** hav
22930 65 20 61 20 6e 61 6d 65 20 77 68 69 63 68 20 69  e a name which i
22940 73 20 74 68 65 20 73 74 72 69 6e 67 20 22 3f 4e  s the string "?N
22950 4e 4e 22 20 6f 72 20 22 3a 41 41 41 22 20 6f 72  NN" or ":AAA" or
22960 20 22 40 41 41 41 22 20 6f 72 20 22 24 41 41 41   "@AAA" or "$AAA
22970 22 0a 2a 2a 20 72 65 73 70 65 63 74 69 76 65 6c  ".** respectivel
22980 79 2e 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77  y..** In other w
22990 6f 72 64 73 2c 20 74 68 65 20 69 6e 69 74 69 61  ords, the initia
229a0 6c 20 22 3a 22 20 6f 72 20 22 24 22 20 6f 72 20  l ":" or "$" or 
229b0 22 40 22 20 6f 72 20 22 3f 22 0a 2a 2a 20 69 73  "@" or "?".** is
229c0 20 69 6e 63 6c 75 64 65 64 20 61 73 20 70 61 72   included as par
229d0 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 2e 0a 2a  t of the name..*
229e0 2a 20 50 61 72 61 6d 65 74 65 72 73 20 6f 66 20  * Parameters of 
229f0 74 68 65 20 66 6f 72 6d 20 22 3f 22 20 77 69 74  the form "?" wit
22a00 68 6f 75 74 20 61 20 66 6f 6c 6c 6f 77 69 6e 67  hout a following
22a10 20 69 6e 74 65 67 65 72 20 68 61 76 65 20 6e 6f   integer have no
22a20 20 6e 61 6d 65 0a 2a 2a 20 61 6e 64 20 61 72 65   name.** and are
22a30 20 61 6c 73 6f 20 72 65 66 65 72 72 65 64 20 74   also referred t
22a40 6f 20 61 73 20 22 61 6e 6f 6e 79 6d 6f 75 73 20  o as "anonymous 
22a50 70 61 72 61 6d 65 74 65 72 73 22 2e 0a 2a 2a 0a  parameters"..**.
22a60 2a 2a 20 54 68 65 20 66 69 72 73 74 20 68 6f 73  ** The first hos
22a70 74 20 70 61 72 61 6d 65 74 65 72 20 68 61 73 20  t parameter has 
22a80 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 2c 20 6e  an index of 1, n
22a90 6f 74 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ot 0..**.** If t
22aa0 68 65 20 76 61 6c 75 65 20 6e 20 69 73 20 6f 75  he value n is ou
22ab0 74 20 6f 66 20 72 61 6e 67 65 20 6f 72 20 69 66  t of range or if
22ac0 20 74 68 65 20 6e 2d 74 68 20 70 61 72 61 6d 65   the n-th parame
22ad0 74 65 72 20 69 73 0a 2a 2a 20 6e 61 6d 65 6c 65  ter is.** namele
22ae0 73 73 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73  ss, then NULL is
22af0 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65 20   returned.  The 
22b00 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20  returned string 
22b10 69 73 0a 2a 2a 20 61 6c 77 61 79 73 20 69 6e 20  is.** always in 
22b20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 65  UTF-8 encoding e
22b30 76 65 6e 20 69 66 20 74 68 65 20 6e 61 6d 65 64  ven if the named
22b40 20 70 61 72 61 6d 65 74 65 72 20 77 61 73 0a 2a   parameter was.*
22b50 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 20 73 70 65  * originally spe
22b60 63 69 66 69 65 64 20 61 73 20 55 54 46 2d 31 36  cified as UTF-16
22b70 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65   in [sqlite3_pre
22b80 70 61 72 65 31 36 28 29 5d 20 6f 72 0a 2a 2a 20  pare16()] or.** 
22b90 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
22ba0 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  16_v2()]..**.** 
22bb0 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74  See also: [sqlit
22bc0 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c  e3_bind_blob|sql
22bd0 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a  ite3_bind()],.**
22be0 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70   [sqlite3_bind_p
22bf0 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29  arameter_count()
22c00 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74  ], and.** [sqlit
22c10 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
22c20 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a  r_index()]..**.*
22c30 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
22c40 2a 2a 20 5b 48 31 33 36 32 31 5d 0a 2a 2f 0a 53  ** [H13621].*/.S
22c50 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
22c60 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 62 69  char *sqlite3_bi
22c70 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d  nd_parameter_nam
22c80 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  e(sqlite3_stmt*,
22c90 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   int);../*.** CA
22ca0 50 49 33 52 45 46 3a 20 49 6e 64 65 78 20 4f 66  PI3REF: Index Of
22cb0 20 41 20 50 61 72 61 6d 65 74 65 72 20 57 69 74   A Parameter Wit
22cc0 68 20 41 20 47 69 76 65 6e 20 4e 61 6d 65 20 7b  h A Given Name {
22cd0 48 31 33 36 34 30 7d 20 3c 53 37 30 33 30 30 3e  H13640} <S70300>
22ce0 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  .**.** Return th
22cf0 65 20 69 6e 64 65 78 20 6f 66 20 61 6e 20 53 51  e index of an SQ
22d00 4c 20 70 61 72 61 6d 65 74 65 72 20 67 69 76 65  L parameter give
22d10 6e 20 69 74 73 20 6e 61 6d 65 2e 20 20 54 68 65  n its name.  The
22d20 0a 2a 2a 20 69 6e 64 65 78 20 76 61 6c 75 65 20  .** index value 
22d30 72 65 74 75 72 6e 65 64 20 69 73 20 73 75 69 74  returned is suit
22d40 61 62 6c 65 20 66 6f 72 20 75 73 65 20 61 73 20  able for use as 
22d50 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 70 61  the second.** pa
22d60 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69  rameter to [sqli
22d70 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71  te3_bind_blob|sq
22d80 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2e 20 20  lite3_bind()].  
22d90 41 20 7a 65 72 6f 0a 2a 2a 20 69 73 20 72 65 74  A zero.** is ret
22da0 75 72 6e 65 64 20 69 66 20 6e 6f 20 6d 61 74 63  urned if no matc
22db0 68 69 6e 67 20 70 61 72 61 6d 65 74 65 72 20 69  hing parameter i
22dc0 73 20 66 6f 75 6e 64 2e 20 20 54 68 65 20 70 61  s found.  The pa
22dd0 72 61 6d 65 74 65 72 0a 2a 2a 20 6e 61 6d 65 20  rameter.** name 
22de0 6d 75 73 74 20 62 65 20 67 69 76 65 6e 20 69 6e  must be given in
22df0 20 55 54 46 2d 38 20 65 76 65 6e 20 69 66 20 74   UTF-8 even if t
22e00 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74  he original stat
22e10 65 6d 65 6e 74 0a 2a 2a 20 77 61 73 20 70 72 65  ement.** was pre
22e20 70 61 72 65 64 20 66 72 6f 6d 20 55 54 46 2d 31  pared from UTF-1
22e30 36 20 74 65 78 74 20 75 73 69 6e 67 20 5b 73 71  6 text using [sq
22e40 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f  lite3_prepare16_
22e50 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  v2()]..**.** See
22e60 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f   also: [sqlite3_
22e70 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65  bind_blob|sqlite
22e80 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73  3_bind()],.** [s
22e90 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61  qlite3_bind_para
22ea0 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 20  meter_count()], 
22eb0 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  and.** [sqlite3_
22ec0 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69  bind_parameter_i
22ed0 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52  ndex()]..**.** R
22ee0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
22ef0 5b 48 31 33 36 34 31 5d 0a 2a 2f 0a 53 51 4c 49  [H13641].*/.SQLI
22f00 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
22f10 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
22f20 72 5f 69 6e 64 65 78 28 73 71 6c 69 74 65 33 5f  r_index(sqlite3_
22f30 73 74 6d 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61  stmt*, const cha
22f40 72 20 2a 7a 4e 61 6d 65 29 3b 0a 0a 2f 2a 0a 2a  r *zName);../*.*
22f50 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 73 65  * CAPI3REF: Rese
22f60 74 20 41 6c 6c 20 42 69 6e 64 69 6e 67 73 20 4f  t All Bindings O
22f70 6e 20 41 20 50 72 65 70 61 72 65 64 20 53 74 61  n A Prepared Sta
22f80 74 65 6d 65 6e 74 20 7b 48 31 33 36 36 30 7d 20  tement {H13660} 
22f90 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 43  <S70300>.**.** C
22fa0 6f 6e 74 72 61 72 79 20 74 6f 20 74 68 65 20 69  ontrary to the i
22fb0 6e 74 75 69 74 69 6f 6e 20 6f 66 20 6d 61 6e 79  ntuition of many
22fc0 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74  , [sqlite3_reset
22fd0 28 29 5d 20 64 6f 65 73 20 6e 6f 74 20 72 65 73  ()] does not res
22fe0 65 74 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74  et.** the [sqlit
22ff0 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 62  e3_bind_blob | b
23000 69 6e 64 69 6e 67 73 5d 20 6f 6e 20 61 20 5b 70  indings] on a [p
23010 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
23020 74 5d 2e 0a 2a 2a 20 55 73 65 20 74 68 69 73 20  t]..** Use this 
23030 72 6f 75 74 69 6e 65 20 74 6f 20 72 65 73 65 74  routine to reset
23040 20 61 6c 6c 20 68 6f 73 74 20 70 61 72 61 6d 65   all host parame
23050 74 65 72 73 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a  ters to NULL..**
23060 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
23070 3a 0a 2a 2a 20 5b 48 31 33 36 36 31 5d 0a 2a 2f  :.** [H13661].*/
23080 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
23090 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69  sqlite3_clear_bi
230a0 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73  ndings(sqlite3_s
230b0 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  tmt*);../*.** CA
230c0 50 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 4f  PI3REF: Number O
230d0 66 20 43 6f 6c 75 6d 6e 73 20 49 6e 20 41 20 52  f Columns In A R
230e0 65 73 75 6c 74 20 53 65 74 20 7b 48 31 33 37 31  esult Set {H1371
230f0 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a  0} <S10700>.**.*
23100 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
23110 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
23120 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
23130 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65   returned by the
23140 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74  .** [prepared st
23150 61 74 65 6d 65 6e 74 5d 2e 20 54 68 69 73 20 72  atement]. This r
23160 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 30  outine returns 0
23170 20 69 66 20 70 53 74 6d 74 20 69 73 20 61 6e 20   if pStmt is an 
23180 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  SQL.** statement
23190 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72   that does not r
231a0 65 74 75 72 6e 20 64 61 74 61 20 28 66 6f 72 20  eturn data (for 
231b0 65 78 61 6d 70 6c 65 20 61 6e 20 5b 55 50 44 41  example an [UPDA
231c0 54 45 5d 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  TE])..**.** Requ
231d0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
231e0 33 37 31 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  3711].*/.SQLITE_
231f0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
23200 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 73 71 6c  column_count(sql
23210 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
23220 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
23230 45 46 3a 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65 73  EF: Column Names
23240 20 49 6e 20 41 20 52 65 73 75 6c 74 20 53 65 74   In A Result Set
23250 20 7b 48 31 33 37 32 30 7d 20 3c 53 31 30 37 30   {H13720} <S1070
23260 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  0>.**.** These r
23270 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74  outines return t
23280 68 65 20 6e 61 6d 65 20 61 73 73 69 67 6e 65 64  he name assigned
23290 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72   to a particular
232a0 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e 20 74 68   column.** in th
232b0 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20  e result set of 
232c0 61 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65  a [SELECT] state
232d0 6d 65 6e 74 2e 20 20 54 68 65 20 73 71 6c 69 74  ment.  The sqlit
232e0 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29  e3_column_name()
232f0 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 72 65  .** interface re
23300 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
23310 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e  to a zero-termin
23320 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e  ated UTF-8 strin
23330 67 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33  g.** and sqlite3
23340 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 29  _column_name16()
23350 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
23360 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72  er to a zero-ter
23370 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 54 46 2d 31  minated.** UTF-1
23380 36 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20 66  6 string.  The f
23390 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69  irst parameter i
233a0 73 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20  s the [prepared 
233b0 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 74 68  statement].** th
233c0 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  at implements th
233d0 65 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65  e [SELECT] state
233e0 6d 65 6e 74 2e 20 54 68 65 20 73 65 63 6f 6e 64  ment. The second
233f0 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68   parameter is th
23400 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62  e.** column numb
23410 65 72 2e 20 20 54 68 65 20 6c 65 66 74 6d 6f 73  er.  The leftmos
23420 74 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 75 6d 62  t column is numb
23430 65 72 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 0..**.** The 
23440 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20  returned string 
23450 70 6f 69 6e 74 65 72 20 69 73 20 76 61 6c 69 64  pointer is valid
23460 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 74 68   until either th
23470 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  e [prepared stat
23480 65 6d 65 6e 74 5d 0a 2a 2a 20 69 73 20 64 65 73  ement].** is des
23490 74 72 6f 79 65 64 20 62 79 20 5b 73 71 6c 69 74  troyed by [sqlit
234a0 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f  e3_finalize()] o
234b0 72 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74  r until the next
234c0 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69   call to.** sqli
234d0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28  te3_column_name(
234e0 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ) or sqlite3_col
234f0 75 6d 6e 5f 6e 61 6d 65 31 36 28 29 20 6f 6e 20  umn_name16() on 
23500 74 68 65 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 2e  the same column.
23510 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65  .**.** If sqlite
23520 33 5f 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73  3_malloc() fails
23530 20 64 75 72 69 6e 67 20 74 68 65 20 70 72 6f 63   during the proc
23540 65 73 73 69 6e 67 20 6f 66 20 65 69 74 68 65 72  essing of either
23550 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 28 66 6f 72   routine.** (for
23560 20 65 78 61 6d 70 6c 65 20 64 75 72 69 6e 67 20   example during 
23570 61 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 72 6f  a conversion fro
23580 6d 20 55 54 46 2d 38 20 74 6f 20 55 54 46 2d 31  m UTF-8 to UTF-1
23590 36 29 20 74 68 65 6e 20 61 0a 2a 2a 20 4e 55 4c  6) then a.** NUL
235a0 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74  L pointer is ret
235b0 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  urned..**.** The
235c0 20 6e 61 6d 65 20 6f 66 20 61 20 72 65 73 75 6c   name of a resul
235d0 74 20 63 6f 6c 75 6d 6e 20 69 73 20 74 68 65 20  t column is the 
235e0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 22 41 53  value of the "AS
235f0 22 20 63 6c 61 75 73 65 20 66 6f 72 0a 2a 2a 20  " clause for.** 
23600 74 68 61 74 20 63 6f 6c 75 6d 6e 2c 20 69 66 20  that column, if 
23610 74 68 65 72 65 20 69 73 20 61 6e 20 41 53 20 63  there is an AS c
23620 6c 61 75 73 65 2e 20 20 49 66 20 74 68 65 72 65  lause.  If there
23630 20 69 73 20 6e 6f 20 41 53 20 63 6c 61 75 73 65   is no AS clause
23640 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 6e 61 6d  .** then the nam
23650 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20  e of the column 
23660 69 73 20 75 6e 73 70 65 63 69 66 69 65 64 20 61  is unspecified a
23670 6e 64 20 6d 61 79 20 63 68 61 6e 67 65 20 66 72  nd may change fr
23680 6f 6d 0a 2a 2a 20 6f 6e 65 20 72 65 6c 65 61 73  om.** one releas
23690 65 20 6f 66 20 53 51 4c 69 74 65 20 74 6f 20 74  e of SQLite to t
236a0 68 65 20 6e 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 52  he next..**.** R
236b0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
236c0 5b 48 31 33 37 32 31 5d 20 5b 48 31 33 37 32 33  [H13721] [H13723
236d0 5d 20 5b 48 31 33 37 32 34 5d 20 5b 48 31 33 37  ] [H13724] [H137
236e0 32 35 5d 20 5b 48 31 33 37 32 36 5d 20 5b 48 31  25] [H13726] [H1
236f0 33 37 32 37 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  3727].*/.SQLITE_
23700 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  API const char *
23710 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
23720 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ame(sqlite3_stmt
23730 2a 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54  *, int N);.SQLIT
23740 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
23750 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
23760 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f  _name16(sqlite3_
23770 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a 0a  stmt*, int N);..
23780 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
23790 53 6f 75 72 63 65 20 4f 66 20 44 61 74 61 20 49  Source Of Data I
237a0 6e 20 41 20 51 75 65 72 79 20 52 65 73 75 6c 74  n A Query Result
237b0 20 7b 48 31 33 37 34 30 7d 20 3c 53 31 30 37 30   {H13740} <S1070
237c0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  0>.**.** These r
237d0 6f 75 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20  outines provide 
237e0 61 20 6d 65 61 6e 73 20 74 6f 20 64 65 74 65 72  a means to deter
237f0 6d 69 6e 65 20 77 68 61 74 20 63 6f 6c 75 6d 6e  mine what column
23800 20 6f 66 20 77 68 61 74 0a 2a 2a 20 74 61 62 6c   of what.** tabl
23810 65 20 69 6e 20 77 68 69 63 68 20 64 61 74 61 62  e in which datab
23820 61 73 65 20 61 20 72 65 73 75 6c 74 20 6f 66 20  ase a result of 
23830 61 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65  a [SELECT] state
23840 6d 65 6e 74 20 63 6f 6d 65 73 20 66 72 6f 6d 2e  ment comes from.
23850 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20  .** The name of 
23860 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20  the database or 
23870 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 20  table or column 
23880 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65 64 20  can be returned 
23890 61 73 0a 2a 2a 20 65 69 74 68 65 72 20 61 20 55  as.** either a U
238a0 54 46 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 73  TF-8 or UTF-16 s
238b0 74 72 69 6e 67 2e 20 20 54 68 65 20 5f 64 61 74  tring.  The _dat
238c0 61 62 61 73 65 5f 20 72 6f 75 74 69 6e 65 73 20  abase_ routines 
238d0 72 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 64 61  return.** the da
238e0 74 61 62 61 73 65 20 6e 61 6d 65 2c 20 74 68 65  tabase name, the
238f0 20 5f 74 61 62 6c 65 5f 20 72 6f 75 74 69 6e 65   _table_ routine
23900 73 20 72 65 74 75 72 6e 20 74 68 65 20 74 61 62  s return the tab
23910 6c 65 20 6e 61 6d 65 2c 20 61 6e 64 0a 2a 2a 20  le name, and.** 
23920 74 68 65 20 6f 72 69 67 69 6e 5f 20 72 6f 75 74  the origin_ rout
23930 69 6e 65 73 20 72 65 74 75 72 6e 20 74 68 65 20  ines return the 
23940 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a 2a 20  column name..** 
23950 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72  The returned str
23960 69 6e 67 20 69 73 20 76 61 6c 69 64 20 75 6e 74  ing is valid unt
23970 69 6c 20 74 68 65 20 5b 70 72 65 70 61 72 65 64  il the [prepared
23980 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 73 20 64   statement] is d
23990 65 73 74 72 6f 79 65 64 0a 2a 2a 20 75 73 69 6e  estroyed.** usin
239a0 67 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  g [sqlite3_final
239b0 69 7a 65 28 29 5d 20 6f 72 20 75 6e 74 69 6c 20  ize()] or until 
239c0 74 68 65 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61  the same informa
239d0 74 69 6f 6e 20 69 73 20 72 65 71 75 65 73 74 65  tion is requeste
239e0 64 0a 2a 2a 20 61 67 61 69 6e 20 69 6e 20 61 20  d.** again in a 
239f0 64 69 66 66 65 72 65 6e 74 20 65 6e 63 6f 64 69  different encodi
23a00 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61  ng..**.** The na
23a10 6d 65 73 20 72 65 74 75 72 6e 65 64 20 61 72 65  mes returned are
23a20 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 75 6e   the original un
23a30 2d 61 6c 69 61 73 65 64 20 6e 61 6d 65 73 20 6f  -aliased names o
23a40 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  f the.** databas
23a50 65 2c 20 74 61 62 6c 65 2c 20 61 6e 64 20 63 6f  e, table, and co
23a60 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  lumn..**.** The 
23a70 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  first argument t
23a80 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  o the following 
23a90 63 61 6c 6c 73 20 69 73 20 61 20 5b 70 72 65 70  calls is a [prep
23aa0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e  ared statement].
23ab0 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69  .** These functi
23ac0 6f 6e 73 20 72 65 74 75 72 6e 20 69 6e 66 6f 72  ons return infor
23ad0 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mation about the
23ae0 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 72 65 74 75   Nth column retu
23af0 72 6e 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 73  rned by.** the s
23b00 74 61 74 65 6d 65 6e 74 2c 20 77 68 65 72 65 20  tatement, where 
23b10 4e 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20  N is the second 
23b20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e  function argumen
23b30 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  t..**.** If the 
23b40 4e 74 68 20 63 6f 6c 75 6d 6e 20 72 65 74 75 72  Nth column retur
23b50 6e 65 64 20 62 79 20 74 68 65 20 73 74 61 74 65  ned by the state
23b60 6d 65 6e 74 20 69 73 20 61 6e 20 65 78 70 72 65  ment is an expre
23b70 73 73 69 6f 6e 20 6f 72 0a 2a 2a 20 73 75 62 71  ssion or.** subq
23b80 75 65 72 79 20 61 6e 64 20 69 73 20 6e 6f 74 20  uery and is not 
23b90 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20  a column value, 
23ba0 74 68 65 6e 20 61 6c 6c 20 6f 66 20 74 68 65 73  then all of thes
23bb0 65 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75  e functions retu
23bc0 72 6e 0a 2a 2a 20 4e 55 4c 4c 2e 20 20 54 68 65  rn.** NULL.  The
23bd0 73 65 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74  se routine might
23be0 20 61 6c 73 6f 20 72 65 74 75 72 6e 20 4e 55 4c   also return NUL
23bf0 4c 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c  L if a memory al
23c00 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 0a 2a  location error.*
23c10 2a 20 6f 63 63 75 72 73 2e 20 20 4f 74 68 65 72  * occurs.  Other
23c20 77 69 73 65 2c 20 74 68 65 79 20 72 65 74 75 72  wise, they retur
23c30 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  n the name of th
23c40 65 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  e attached datab
23c50 61 73 65 2c 20 74 61 62 6c 65 0a 2a 2a 20 61 6e  ase, table.** an
23c60 64 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 71 75  d column that qu
23c70 65 72 79 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d  ery result colum
23c80 6e 20 77 61 73 20 65 78 74 72 61 63 74 65 64 20  n was extracted 
23c90 66 72 6f 6d 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 77  from..**.** As w
23ca0 69 74 68 20 61 6c 6c 20 6f 74 68 65 72 20 53 51  ith all other SQ
23cb0 4c 69 74 65 20 41 50 49 73 2c 20 74 68 6f 73 65  Lite APIs, those
23cc0 20 70 6f 73 74 66 69 78 65 64 20 77 69 74 68 20   postfixed with 
23cd0 22 31 36 22 20 72 65 74 75 72 6e 0a 2a 2a 20 55  "16" return.** U
23ce0 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 73 74  TF-16 encoded st
23cf0 72 69 6e 67 73 2c 20 74 68 65 20 6f 74 68 65 72  rings, the other
23d00 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72   functions retur
23d10 6e 20 55 54 46 2d 38 2e 20 7b 45 4e 44 7d 0a 2a  n UTF-8. {END}.*
23d20 2a 0a 2a 2a 20 54 68 65 73 65 20 41 50 49 73 20  *.** These APIs 
23d30 61 72 65 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62  are only availab
23d40 6c 65 20 69 66 20 74 68 65 20 6c 69 62 72 61 72  le if the librar
23d50 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77  y was compiled w
23d60 69 74 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49  ith the.** [SQLI
23d70 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e  TE_ENABLE_COLUMN
23d80 5f 4d 45 54 41 44 41 54 41 5d 20 43 2d 70 72 65  _METADATA] C-pre
23d90 70 72 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c  processor symbol
23da0 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20   defined..**.** 
23db0 7b 41 31 33 37 35 31 7d 0a 2a 2a 20 49 66 20 74  {A13751}.** If t
23dc0 77 6f 20 6f 72 20 6d 6f 72 65 20 74 68 72 65 61  wo or more threa
23dd0 64 73 20 63 61 6c 6c 20 6f 6e 65 20 6f 72 20 6d  ds call one or m
23de0 6f 72 65 20 6f 66 20 74 68 65 73 65 20 72 6f 75  ore of these rou
23df0 74 69 6e 65 73 20 61 67 61 69 6e 73 74 20 74 68  tines against th
23e00 65 20 73 61 6d 65 0a 2a 2a 20 70 72 65 70 61 72  e same.** prepar
23e10 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64  ed statement and
23e20 20 63 6f 6c 75 6d 6e 20 61 74 20 74 68 65 20 73   column at the s
23e30 61 6d 65 20 74 69 6d 65 20 74 68 65 6e 20 74 68  ame time then th
23e40 65 20 72 65 73 75 6c 74 73 20 61 72 65 0a 2a 2a  e results are.**
23e50 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a   undefined..**.*
23e60 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
23e70 2a 2a 20 5b 48 31 33 37 34 31 5d 20 5b 48 31 33  ** [H13741] [H13
23e80 37 34 32 5d 20 5b 48 31 33 37 34 33 5d 20 5b 48  742] [H13743] [H
23e90 31 33 37 34 34 5d 20 5b 48 31 33 37 34 35 5d 20  13744] [H13745] 
23ea0 5b 48 31 33 37 34 36 5d 20 5b 48 31 33 37 34 38  [H13746] [H13748
23eb0 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 77 6f 20 6f  ].**.** If two o
23ec0 72 20 6d 6f 72 65 20 74 68 72 65 61 64 73 20 63  r more threads c
23ed0 61 6c 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a  all one or more.
23ee0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ** [sqlite3_colu
23ef0 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
23f00 20 7c 20 63 6f 6c 75 6d 6e 20 6d 65 74 61 64 61   | column metada
23f10 74 61 20 69 6e 74 65 72 66 61 63 65 73 5d 0a 2a  ta interfaces].*
23f20 2a 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 5b  * for the same [
23f30 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
23f40 6e 74 5d 20 61 6e 64 20 72 65 73 75 6c 74 20 63  nt] and result c
23f50 6f 6c 75 6d 6e 0a 2a 2a 20 61 74 20 74 68 65 20  olumn.** at the 
23f60 73 61 6d 65 20 74 69 6d 65 20 74 68 65 6e 20 74  same time then t
23f70 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 75  he results are u
23f80 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c  ndefined..*/.SQL
23f90 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
23fa0 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ar *sqlite3_colu
23fb0 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
23fc0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
23fd0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  nt);.SQLITE_API 
23fe0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
23ff0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
24000 61 73 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74  ase_name16(sqlit
24010 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53  e3_stmt*,int);.S
24020 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
24030 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f  char *sqlite3_co
24040 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 28  lumn_table_name(
24050 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
24060 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  t);.SQLITE_API c
24070 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
24080 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  e3_column_table_
24090 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73  name16(sqlite3_s
240a0 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  tmt*,int);.SQLIT
240b0 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72  E_API const char
240c0 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
240d0 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 28 73 71 6c  _origin_name(sql
240e0 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
240f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
24100 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
24110 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61  column_origin_na
24120 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d  me16(sqlite3_stm
24130 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20  t*,int);../*.** 
24140 43 41 50 49 33 52 45 46 3a 20 44 65 63 6c 61 72  CAPI3REF: Declar
24150 65 64 20 44 61 74 61 74 79 70 65 20 4f 66 20 41  ed Datatype Of A
24160 20 51 75 65 72 79 20 52 65 73 75 6c 74 20 7b 48   Query Result {H
24170 31 33 37 36 30 7d 20 3c 53 31 30 37 30 30 3e 0a  13760} <S10700>.
24180 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  **.** The first 
24190 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 5b  parameter is a [
241a0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
241b0 6e 74 5d 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20  nt]..** If this 
241c0 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 20 5b  statement is a [
241d0 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e  SELECT] statemen
241e0 74 20 61 6e 64 20 74 68 65 20 4e 74 68 20 63 6f  t and the Nth co
241f0 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 72  lumn of the.** r
24200 65 74 75 72 6e 65 64 20 72 65 73 75 6c 74 20 73  eturned result s
24210 65 74 20 6f 66 20 74 68 61 74 20 5b 53 45 4c 45  et of that [SELE
24220 43 54 5d 20 69 73 20 61 20 74 61 62 6c 65 20 63  CT] is a table c
24230 6f 6c 75 6d 6e 20 28 6e 6f 74 20 61 6e 0a 2a 2a  olumn (not an.**
24240 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 73   expression or s
24250 75 62 71 75 65 72 79 29 20 74 68 65 6e 20 74 68  ubquery) then th
24260 65 20 64 65 63 6c 61 72 65 64 20 74 79 70 65 20  e declared type 
24270 6f 66 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20  of the table.** 
24280 63 6f 6c 75 6d 6e 20 69 73 20 72 65 74 75 72 6e  column is return
24290 65 64 2e 20 20 49 66 20 74 68 65 20 4e 74 68 20  ed.  If the Nth 
242a0 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65  column of the re
242b0 73 75 6c 74 20 73 65 74 20 69 73 20 61 6e 0a 2a  sult set is an.*
242c0 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20  * expression or 
242d0 73 75 62 71 75 65 72 79 2c 20 74 68 65 6e 20 61  subquery, then a
242e0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73   NULL pointer is
242f0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54 68   returned..** Th
24300 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e  e returned strin
24310 67 20 69 73 20 61 6c 77 61 79 73 20 55 54 46 2d  g is always UTF-
24320 38 20 65 6e 63 6f 64 65 64 2e 20 7b 45 4e 44 7d  8 encoded. {END}
24330 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70  .**.** For examp
24340 6c 65 2c 20 67 69 76 65 6e 20 74 68 65 20 64 61  le, given the da
24350 74 61 62 61 73 65 20 73 63 68 65 6d 61 3a 0a 2a  tabase schema:.*
24360 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c  *.** CREATE TABL
24370 45 20 74 31 28 63 31 20 56 41 52 49 41 4e 54 29  E t1(c1 VARIANT)
24380 3b 0a 2a 2a 0a 2a 2a 20 61 6e 64 20 74 68 65 20  ;.**.** and the 
24390 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d  following statem
243a0 65 6e 74 20 74 6f 20 62 65 20 63 6f 6d 70 69 6c  ent to be compil
243b0 65 64 3a 0a 2a 2a 0a 2a 2a 20 53 45 4c 45 43 54  ed:.**.** SELECT
243c0 20 63 31 20 2b 20 31 2c 20 63 31 20 46 52 4f 4d   c1 + 1, c1 FROM
243d0 20 74 31 3b 0a 2a 2a 0a 2a 2a 20 74 68 69 73 20   t1;.**.** this 
243e0 72 6f 75 74 69 6e 65 20 77 6f 75 6c 64 20 72 65  routine would re
243f0 74 75 72 6e 20 74 68 65 20 73 74 72 69 6e 67 20  turn the string 
24400 22 56 41 52 49 41 4e 54 22 20 66 6f 72 20 74 68  "VARIANT" for th
24410 65 20 73 65 63 6f 6e 64 20 72 65 73 75 6c 74 0a  e second result.
24420 2a 2a 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d 31 29  ** column (i==1)
24430 2c 20 61 6e 64 20 61 20 4e 55 4c 4c 20 70 6f 69  , and a NULL poi
24440 6e 74 65 72 20 66 6f 72 20 74 68 65 20 66 69 72  nter for the fir
24450 73 74 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  st result column
24460 20 28 69 3d 3d 30 29 2e 0a 2a 2a 0a 2a 2a 20 53   (i==0)..**.** S
24470 51 4c 69 74 65 20 75 73 65 73 20 64 79 6e 61 6d  QLite uses dynam
24480 69 63 20 72 75 6e 2d 74 69 6d 65 20 74 79 70 69  ic run-time typi
24490 6e 67 2e 20 20 53 6f 20 6a 75 73 74 20 62 65 63  ng.  So just bec
244a0 61 75 73 65 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a  ause a column.**
244b0 20 69 73 20 64 65 63 6c 61 72 65 64 20 74 6f 20   is declared to 
244c0 63 6f 6e 74 61 69 6e 20 61 20 70 61 72 74 69 63  contain a partic
244d0 75 6c 61 72 20 74 79 70 65 20 64 6f 65 73 20 6e  ular type does n
244e0 6f 74 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65  ot mean that the
244f0 0a 2a 2a 20 64 61 74 61 20 73 74 6f 72 65 64 20  .** data stored 
24500 69 6e 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 69  in that column i
24510 73 20 6f 66 20 74 68 65 20 64 65 63 6c 61 72 65  s of the declare
24520 64 20 74 79 70 65 2e 20 20 53 51 4c 69 74 65 20  d type.  SQLite 
24530 69 73 0a 2a 2a 20 73 74 72 6f 6e 67 6c 79 20 74  is.** strongly t
24540 79 70 65 64 2c 20 62 75 74 20 74 68 65 20 74 79  yped, but the ty
24550 70 69 6e 67 20 69 73 20 64 79 6e 61 6d 69 63 20  ping is dynamic 
24560 6e 6f 74 20 73 74 61 74 69 63 2e 20 20 54 79 70  not static.  Typ
24570 65 0a 2a 2a 20 69 73 20 61 73 73 6f 63 69 61 74  e.** is associat
24580 65 64 20 77 69 74 68 20 69 6e 64 69 76 69 64 75  ed with individu
24590 61 6c 20 76 61 6c 75 65 73 2c 20 6e 6f 74 20 77  al values, not w
245a0 69 74 68 20 74 68 65 20 63 6f 6e 74 61 69 6e 65  ith the containe
245b0 72 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 68 6f  rs.** used to ho
245c0 6c 64 20 74 68 6f 73 65 20 76 61 6c 75 65 73 2e  ld those values.
245d0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
245e0 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 36 31 5d  nts:.** [H13761]
245f0 20 5b 48 31 33 37 36 32 5d 20 5b 48 31 33 37 36   [H13762] [H1376
24600 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  3].*/.SQLITE_API
24610 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
24620 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c  ite3_column_decl
24630 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  type(sqlite3_stm
24640 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t*,int);.SQLITE_
24650 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
24660 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
24670 65 63 6c 74 79 70 65 31 36 28 73 71 6c 69 74 65  ecltype16(sqlite
24680 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f  3_stmt*,int);../
24690 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45  *.** CAPI3REF: E
246a0 76 61 6c 75 61 74 65 20 41 6e 20 53 51 4c 20 53  valuate An SQL S
246b0 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 32 30 30  tatement {H13200
246c0 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a  } <S10000>.**.**
246d0 20 41 66 74 65 72 20 61 20 5b 70 72 65 70 61 72   After a [prepar
246e0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 68 61  ed statement] ha
246f0 73 20 62 65 65 6e 20 70 72 65 70 61 72 65 64 20  s been prepared 
24700 75 73 69 6e 67 20 65 69 74 68 65 72 0a 2a 2a 20  using either.** 
24710 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
24720 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74  _v2()] or [sqlit
24730 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28  e3_prepare16_v2(
24740 29 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65  )] or one of the
24750 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72   legacy.** inter
24760 66 61 63 65 73 20 5b 73 71 6c 69 74 65 33 5f 70  faces [sqlite3_p
24770 72 65 70 61 72 65 28 29 5d 20 6f 72 20 5b 73 71  repare()] or [sq
24780 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28  lite3_prepare16(
24790 29 5d 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  )], this functio
247a0 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 63 61 6c  n.** must be cal
247b0 6c 65 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  led one or more 
247c0 74 69 6d 65 73 20 74 6f 20 65 76 61 6c 75 61 74  times to evaluat
247d0 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2e  e the statement.
247e0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 74 61 69  .**.** The detai
247f0 6c 73 20 6f 66 20 74 68 65 20 62 65 68 61 76 69  ls of the behavi
24800 6f 72 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  or of the sqlite
24810 33 5f 73 74 65 70 28 29 20 69 6e 74 65 72 66 61  3_step() interfa
24820 63 65 20 64 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20  ce depend.** on 
24830 77 68 65 74 68 65 72 20 74 68 65 20 73 74 61 74  whether the stat
24840 65 6d 65 6e 74 20 77 61 73 20 70 72 65 70 61 72  ement was prepar
24850 65 64 20 75 73 69 6e 67 20 74 68 65 20 6e 65 77  ed using the new
24860 65 72 20 22 76 32 22 20 69 6e 74 65 72 66 61 63  er "v2" interfac
24870 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72  e.** [sqlite3_pr
24880 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20  epare_v2()] and 
24890 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
248a0 31 36 5f 76 32 28 29 5d 20 6f 72 20 74 68 65 20  16_v2()] or the 
248b0 6f 6c 64 65 72 20 6c 65 67 61 63 79 0a 2a 2a 20  older legacy.** 
248c0 69 6e 74 65 72 66 61 63 65 20 5b 73 71 6c 69 74  interface [sqlit
248d0 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 61 6e  e3_prepare()] an
248e0 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  d [sqlite3_prepa
248f0 72 65 31 36 28 29 5d 2e 20 20 54 68 65 20 75 73  re16()].  The us
24900 65 20 6f 66 20 74 68 65 0a 2a 2a 20 6e 65 77 20  e of the.** new 
24910 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 20 69  "v2" interface i
24920 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f  s recommended fo
24930 72 20 6e 65 77 20 61 70 70 6c 69 63 61 74 69 6f  r new applicatio
24940 6e 73 20 62 75 74 20 74 68 65 20 6c 65 67 61 63  ns but the legac
24950 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 77  y.** interface w
24960 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20  ill continue to 
24970 62 65 20 73 75 70 70 6f 72 74 65 64 2e 0a 2a 2a  be supported..**
24980 0a 2a 2a 20 49 6e 20 74 68 65 20 6c 65 67 61 63  .** In the legac
24990 79 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65  y interface, the
249a0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 77 69   return value wi
249b0 6c 6c 20 62 65 20 65 69 74 68 65 72 20 5b 53 51  ll be either [SQ
249c0 4c 49 54 45 5f 42 55 53 59 5d 2c 0a 2a 2a 20 5b  LITE_BUSY],.** [
249d0 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 5b 53  SQLITE_DONE], [S
249e0 51 4c 49 54 45 5f 52 4f 57 5d 2c 20 5b 53 51 4c  QLITE_ROW], [SQL
249f0 49 54 45 5f 45 52 52 4f 52 5d 2c 20 6f 72 20 5b  ITE_ERROR], or [
24a00 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 2e 0a  SQLITE_MISUSE]..
24a10 2a 2a 20 57 69 74 68 20 74 68 65 20 22 76 32 22  ** With the "v2"
24a20 20 69 6e 74 65 72 66 61 63 65 2c 20 61 6e 79 20   interface, any 
24a30 6f 66 20 74 68 65 20 6f 74 68 65 72 20 5b 72 65  of the other [re
24a40 73 75 6c 74 20 63 6f 64 65 73 5d 20 6f 72 0a 2a  sult codes] or.*
24a50 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75  * [extended resu
24a60 6c 74 20 63 6f 64 65 73 5d 20 6d 69 67 68 74 20  lt codes] might 
24a70 62 65 20 72 65 74 75 72 6e 65 64 20 61 73 20 77  be returned as w
24a80 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49  ell..**.** [SQLI
24a90 54 45 5f 42 55 53 59 5d 20 6d 65 61 6e 73 20 74  TE_BUSY] means t
24aa0 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65  hat the database
24ab0 20 65 6e 67 69 6e 65 20 77 61 73 20 75 6e 61 62   engine was unab
24ac0 6c 65 20 74 6f 20 61 63 71 75 69 72 65 20 74 68  le to acquire th
24ad0 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 6c 6f  e.** database lo
24ae0 63 6b 73 20 69 74 20 6e 65 65 64 73 20 74 6f 20  cks it needs to 
24af0 64 6f 20 69 74 73 20 6a 6f 62 2e 20 20 49 66 20  do its job.  If 
24b00 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73  the statement is
24b10 20 61 20 5b 43 4f 4d 4d 49 54 5d 0a 2a 2a 20 6f   a [COMMIT].** o
24b20 72 20 6f 63 63 75 72 73 20 6f 75 74 73 69 64 65  r occurs outside
24b30 20 6f 66 20 61 6e 20 65 78 70 6c 69 63 69 74 20   of an explicit 
24b40 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65  transaction, the
24b50 6e 20 79 6f 75 20 63 61 6e 20 72 65 74 72 79 20  n you can retry 
24b60 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  the.** statement
24b70 2e 20 20 49 66 20 74 68 65 20 73 74 61 74 65 6d  .  If the statem
24b80 65 6e 74 20 69 73 20 6e 6f 74 20 61 20 5b 43 4f  ent is not a [CO
24b90 4d 4d 49 54 5d 20 61 6e 64 20 6f 63 63 75 72 73  MMIT] and occurs
24ba0 20 77 69 74 68 69 6e 20 61 0a 2a 2a 20 65 78 70   within a.** exp
24bb0 6c 69 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f  licit transactio
24bc0 6e 20 74 68 65 6e 20 79 6f 75 20 73 68 6f 75 6c  n then you shoul
24bd0 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74  d rollback the t
24be0 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 66 6f 72  ransaction befor
24bf0 65 0a 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e 67 2e  e.** continuing.
24c00 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44  .**.** [SQLITE_D
24c10 4f 4e 45 5d 20 6d 65 61 6e 73 20 74 68 61 74 20  ONE] means that 
24c20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61  the statement ha
24c30 73 20 66 69 6e 69 73 68 65 64 20 65 78 65 63 75  s finished execu
24c40 74 69 6e 67 0a 2a 2a 20 73 75 63 63 65 73 73 66  ting.** successf
24c50 75 6c 6c 79 2e 20 20 73 71 6c 69 74 65 33 5f 73  ully.  sqlite3_s
24c60 74 65 70 28 29 20 73 68 6f 75 6c 64 20 6e 6f 74  tep() should not
24c70 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61 69 6e   be called again
24c80 20 6f 6e 20 74 68 69 73 20 76 69 72 74 75 61 6c   on this virtual
24c90 0a 2a 2a 20 6d 61 63 68 69 6e 65 20 77 69 74 68  .** machine with
24ca0 6f 75 74 20 66 69 72 73 74 20 63 61 6c 6c 69 6e  out first callin
24cb0 67 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74  g [sqlite3_reset
24cc0 28 29 5d 20 74 6f 20 72 65 73 65 74 20 74 68 65  ()] to reset the
24cd0 20 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68   virtual.** mach
24ce0 69 6e 65 20 62 61 63 6b 20 74 6f 20 69 74 73 20  ine back to its 
24cf0 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2e 0a 2a  initial state..*
24d00 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 20  *.** If the SQL 
24d10 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20  statement being 
24d20 65 78 65 63 75 74 65 64 20 72 65 74 75 72 6e 73  executed returns
24d30 20 61 6e 79 20 64 61 74 61 2c 20 74 68 65 6e 20   any data, then 
24d40 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 0a 2a 2a 20  [SQLITE_ROW].** 
24d50 69 73 20 72 65 74 75 72 6e 65 64 20 65 61 63 68  is returned each
24d60 20 74 69 6d 65 20 61 20 6e 65 77 20 72 6f 77 20   time a new row 
24d70 6f 66 20 64 61 74 61 20 69 73 20 72 65 61 64 79  of data is ready
24d80 20 66 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 20   for processing 
24d90 62 79 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72  by the.** caller
24da0 2e 20 54 68 65 20 76 61 6c 75 65 73 20 6d 61 79  . The values may
24db0 20 62 65 20 61 63 63 65 73 73 65 64 20 75 73 69   be accessed usi
24dc0 6e 67 20 74 68 65 20 5b 63 6f 6c 75 6d 6e 20 61  ng the [column a
24dd0 63 63 65 73 73 20 66 75 6e 63 74 69 6f 6e 73 5d  ccess functions]
24de0 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 74 65  ..** sqlite3_ste
24df0 70 28 29 20 69 73 20 63 61 6c 6c 65 64 20 61 67  p() is called ag
24e00 61 69 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20  ain to retrieve 
24e10 74 68 65 20 6e 65 78 74 20 72 6f 77 20 6f 66 20  the next row of 
24e20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c  data..**.** [SQL
24e30 49 54 45 5f 45 52 52 4f 52 5d 20 6d 65 61 6e 73  ITE_ERROR] means
24e40 20 74 68 61 74 20 61 20 72 75 6e 2d 74 69 6d 65   that a run-time
24e50 20 65 72 72 6f 72 20 28 73 75 63 68 20 61 73 20   error (such as 
24e60 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20  a constraint.** 
24e70 76 69 6f 6c 61 74 69 6f 6e 29 20 68 61 73 20 6f  violation) has o
24e80 63 63 75 72 72 65 64 2e 20 20 73 71 6c 69 74 65  ccurred.  sqlite
24e90 33 5f 73 74 65 70 28 29 20 73 68 6f 75 6c 64 20  3_step() should 
24ea0 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 61 67  not be called ag
24eb0 61 69 6e 20 6f 6e 0a 2a 2a 20 74 68 65 20 56 4d  ain on.** the VM
24ec0 2e 20 4d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69  . More informati
24ed0 6f 6e 20 6d 61 79 20 62 65 20 66 6f 75 6e 64 20  on may be found 
24ee0 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69  by calling [sqli
24ef0 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a  te3_errmsg()]..*
24f00 2a 20 57 69 74 68 20 74 68 65 20 6c 65 67 61 63  * With the legac
24f10 79 20 69 6e 74 65 72 66 61 63 65 2c 20 61 20 6d  y interface, a m
24f20 6f 72 65 20 73 70 65 63 69 66 69 63 20 65 72 72  ore specific err
24f30 6f 72 20 63 6f 64 65 20 28 66 6f 72 20 65 78 61  or code (for exa
24f40 6d 70 6c 65 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45  mple,.** [SQLITE
24f50 5f 49 4e 54 45 52 52 55 50 54 5d 2c 20 5b 53 51  _INTERRUPT], [SQ
24f60 4c 49 54 45 5f 53 43 48 45 4d 41 5d 2c 20 5b 53  LITE_SCHEMA], [S
24f70 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5d 2c 20  QLITE_CORRUPT], 
24f80 61 6e 64 20 73 6f 20 66 6f 72 74 68 29 0a 2a 2a  and so forth).**
24f90 20 63 61 6e 20 62 65 20 6f 62 74 61 69 6e 65 64   can be obtained
24fa0 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c   by calling [sql
24fb0 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 6e  ite3_reset()] on
24fc0 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65   the.** [prepare
24fd0 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49  d statement].  I
24fe0 6e 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72  n the "v2" inter
24ff0 66 61 63 65 2c 0a 2a 2a 20 74 68 65 20 6d 6f 72  face,.** the mor
25000 65 20 73 70 65 63 69 66 69 63 20 65 72 72 6f 72  e specific error
25010 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
25020 64 20 64 69 72 65 63 74 6c 79 20 62 79 20 73 71  d directly by sq
25030 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 0a 2a 2a  lite3_step()..**
25040 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55  .** [SQLITE_MISU
25050 53 45 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74  SE] means that t
25060 68 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  he this routine 
25070 77 61 73 20 63 61 6c 6c 65 64 20 69 6e 61 70 70  was called inapp
25080 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2a 20 50  ropriately..** P
25090 65 72 68 61 70 73 20 69 74 20 77 61 73 20 63 61  erhaps it was ca
250a0 6c 6c 65 64 20 6f 6e 20 61 20 5b 70 72 65 70 61  lled on a [prepa
250b0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74  red statement] t
250c0 68 61 74 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61  hat has.** alrea
250d0 64 79 20 62 65 65 6e 20 5b 73 71 6c 69 74 65 33  dy been [sqlite3
250e0 5f 66 69 6e 61 6c 69 7a 65 20 7c 20 66 69 6e 61  _finalize | fina
250f0 6c 69 7a 65 64 5d 20 6f 72 20 6f 6e 20 6f 6e 65  lized] or on one
25100 20 74 68 61 74 20 68 61 64 0a 2a 2a 20 70 72 65   that had.** pre
25110 76 69 6f 75 73 6c 79 20 72 65 74 75 72 6e 65 64  viously returned
25120 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20   [SQLITE_ERROR] 
25130 6f 72 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d  or [SQLITE_DONE]
25140 2e 20 20 4f 72 20 69 74 20 63 6f 75 6c 64 0a 2a  .  Or it could.*
25150 2a 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68  * be the case th
25160 61 74 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  at the same data
25170 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
25180 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 62 79  is being used by
25190 20 74 77 6f 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20   two or.** more 
251a0 74 68 72 65 61 64 73 20 61 74 20 74 68 65 20 73  threads at the s
251b0 61 6d 65 20 6d 6f 6d 65 6e 74 20 69 6e 20 74 69  ame moment in ti
251c0 6d 65 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 47 6f 6f  me..**.** <b>Goo
251d0 66 79 20 49 6e 74 65 72 66 61 63 65 20 41 6c 65  fy Interface Ale
251e0 72 74 3a 3c 2f 62 3e 20 49 6e 20 74 68 65 20 6c  rt:</b> In the l
251f0 65 67 61 63 79 20 69 6e 74 65 72 66 61 63 65 2c  egacy interface,
25200 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 65   the sqlite3_ste
25210 70 28 29 0a 2a 2a 20 41 50 49 20 61 6c 77 61 79  p().** API alway
25220 73 20 72 65 74 75 72 6e 73 20 61 20 67 65 6e 65  s returns a gene
25230 72 69 63 20 65 72 72 6f 72 20 63 6f 64 65 2c 20  ric error code, 
25240 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2c 20  [SQLITE_ERROR], 
25250 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e 79 0a 2a 2a  following any.**
25260 20 65 72 72 6f 72 20 6f 74 68 65 72 20 74 68 61   error other tha
25270 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20  n [SQLITE_BUSY] 
25280 61 6e 64 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55  and [SQLITE_MISU
25290 53 45 5d 2e 20 20 59 6f 75 20 6d 75 73 74 20 63  SE].  You must c
252a0 61 6c 6c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  all.** [sqlite3_
252b0 72 65 73 65 74 28 29 5d 20 6f 72 20 5b 73 71 6c  reset()] or [sql
252c0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d  ite3_finalize()]
252d0 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 66 69 6e   in order to fin
252e0 64 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a 2a 20  d one of the.** 
252f0 73 70 65 63 69 66 69 63 20 5b 65 72 72 6f 72 20  specific [error 
25300 63 6f 64 65 73 5d 20 74 68 61 74 20 62 65 74 74  codes] that bett
25310 65 72 20 64 65 73 63 72 69 62 65 73 20 74 68 65  er describes the
25320 20 65 72 72 6f 72 2e 0a 2a 2a 20 57 65 20 61 64   error..** We ad
25330 6d 69 74 20 74 68 61 74 20 74 68 69 73 20 69 73  mit that this is
25340 20 61 20 67 6f 6f 66 79 20 64 65 73 69 67 6e 2e   a goofy design.
25350 20 20 54 68 65 20 70 72 6f 62 6c 65 6d 20 68 61    The problem ha
25360 73 20 62 65 65 6e 20 66 69 78 65 64 0a 2a 2a 20  s been fixed.** 
25370 77 69 74 68 20 74 68 65 20 22 76 32 22 20 69 6e  with the "v2" in
25380 74 65 72 66 61 63 65 2e 20 20 49 66 20 79 6f 75  terface.  If you
25390 20 70 72 65 70 61 72 65 20 61 6c 6c 20 6f 66 20   prepare all of 
253a0 79 6f 75 72 20 53 51 4c 20 73 74 61 74 65 6d 65  your SQL stateme
253b0 6e 74 73 0a 2a 2a 20 75 73 69 6e 67 20 65 69 74  nts.** using eit
253c0 68 65 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  her [sqlite3_pre
253d0 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73  pare_v2()] or [s
253e0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
253f0 5f 76 32 28 29 5d 20 69 6e 73 74 65 61 64 0a 2a  _v2()] instead.*
25400 2a 20 6f 66 20 74 68 65 20 6c 65 67 61 63 79 20  * of the legacy 
25410 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
25420 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
25430 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20 69 6e  _prepare16()] in
25440 74 65 72 66 61 63 65 73 2c 0a 2a 2a 20 74 68 65  terfaces,.** the
25450 6e 20 74 68 65 20 6d 6f 72 65 20 73 70 65 63 69  n the more speci
25460 66 69 63 20 5b 65 72 72 6f 72 20 63 6f 64 65 73  fic [error codes
25470 5d 20 61 72 65 20 72 65 74 75 72 6e 65 64 20 64  ] are returned d
25480 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 73 71  irectly.** by sq
25490 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 20 54  lite3_step().  T
254a0 68 65 20 75 73 65 20 6f 66 20 74 68 65 20 22 76  he use of the "v
254b0 32 22 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  2" interface is 
254c0 72 65 63 6f 6d 6d 65 6e 64 65 64 2e 0a 2a 2a 0a  recommended..**.
254d0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
254e0 0a 2a 2a 20 5b 48 31 33 32 30 32 5d 20 5b 48 31  .** [H13202] [H1
254f0 35 33 30 34 5d 20 5b 48 31 35 33 30 36 5d 20 5b  5304] [H15306] [
25500 48 31 35 33 30 38 5d 20 5b 48 31 35 33 31 30 5d  H15308] [H15310]
25510 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
25520 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  nt sqlite3_step(
25530 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a  sqlite3_stmt*);.
25540 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
25550 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   Number of colum
25560 6e 73 20 69 6e 20 61 20 72 65 73 75 6c 74 20 73  ns in a result s
25570 65 74 20 7b 48 31 33 37 37 30 7d 20 3c 53 31 30  et {H13770} <S10
25580 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  700>.**.** Retur
25590 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
255a0 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 63   values in the c
255b0 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68  urrent row of th
255c0 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2a  e result set..**
255d0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
255e0 3a 0a 2a 2a 20 5b 48 31 33 37 37 31 5d 20 5b 48  :.** [H13771] [H
255f0 31 33 37 37 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45  13772].*/.SQLITE
25600 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
25610 5f 64 61 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69  _data_count(sqli
25620 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29  te3_stmt *pStmt)
25630 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
25640 46 3a 20 46 75 6e 64 61 6d 65 6e 74 61 6c 20 44  F: Fundamental D
25650 61 74 61 74 79 70 65 73 20 7b 48 31 30 32 36 35  atatypes {H10265
25660 7d 20 3c 53 31 30 31 31 30 3e 3c 53 31 30 31 32  } <S10110><S1012
25670 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  0>.** KEYWORDS: 
25680 53 51 4c 49 54 45 5f 54 45 58 54 0a 2a 2a 0a 2a  SQLITE_TEXT.**.*
25690 2a 20 7b 48 31 30 32 36 36 7d 20 45 76 65 72 79  * {H10266} Every
256a0 20 76 61 6c 75 65 20 69 6e 20 53 51 4c 69 74 65   value in SQLite
256b0 20 68 61 73 20 6f 6e 65 20 6f 66 20 66 69 76 65   has one of five
256c0 20 66 75 6e 64 61 6d 65 6e 74 61 6c 20 64 61 74   fundamental dat
256d0 61 74 79 70 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75  atypes:.**.** <u
256e0 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69  l>.** <li> 64-bi
256f0 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
25700 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20  .** <li> 64-bit 
25710 49 45 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f  IEEE floating po
25720 69 6e 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 3c 6c  int number.** <l
25730 69 3e 20 73 74 72 69 6e 67 0a 2a 2a 20 3c 6c 69  i> string.** <li
25740 3e 20 42 4c 4f 42 0a 2a 2a 20 3c 6c 69 3e 20 4e  > BLOB.** <li> N
25750 55 4c 4c 0a 2a 2a 20 3c 2f 75 6c 3e 20 7b 45 4e  ULL.** </ul> {EN
25760 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63  D}.**.** These c
25770 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 63 6f 64  onstants are cod
25780 65 73 20 66 6f 72 20 65 61 63 68 20 6f 66 20 74  es for each of t
25790 68 6f 73 65 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a  hose types..**.*
257a0 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20  * Note that the 
257b0 53 51 4c 49 54 45 5f 54 45 58 54 20 63 6f 6e 73  SQLITE_TEXT cons
257c0 74 61 6e 74 20 77 61 73 20 61 6c 73 6f 20 75 73  tant was also us
257d0 65 64 20 69 6e 20 53 51 4c 69 74 65 20 76 65 72  ed in SQLite ver
257e0 73 69 6f 6e 20 32 0a 2a 2a 20 66 6f 72 20 61 20  sion 2.** for a 
257f0 63 6f 6d 70 6c 65 74 65 6c 79 20 64 69 66 66 65  completely diffe
25800 72 65 6e 74 20 6d 65 61 6e 69 6e 67 2e 20 20 53  rent meaning.  S
25810 6f 66 74 77 61 72 65 20 74 68 61 74 20 6c 69 6e  oftware that lin
25820 6b 73 20 61 67 61 69 6e 73 74 20 62 6f 74 68 0a  ks against both.
25830 2a 2a 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f  ** SQLite versio
25840 6e 20 32 20 61 6e 64 20 53 51 4c 69 74 65 20 76  n 2 and SQLite v
25850 65 72 73 69 6f 6e 20 33 20 73 68 6f 75 6c 64 20  ersion 3 should 
25860 75 73 65 20 53 51 4c 49 54 45 33 5f 54 45 58 54  use SQLITE3_TEXT
25870 2c 20 6e 6f 74 0a 2a 2a 20 53 51 4c 49 54 45 5f  , not.** SQLITE_
25880 54 45 58 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  TEXT..*/.#define
25890 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20   SQLITE_INTEGER 
258a0 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   1.#define SQLIT
258b0 45 5f 46 4c 4f 41 54 20 20 20 20 32 0a 23 64 65  E_FLOAT    2.#de
258c0 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42  fine SQLITE_BLOB
258d0 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53       4.#define S
258e0 51 4c 49 54 45 5f 4e 55 4c 4c 20 20 20 20 20 35  QLITE_NULL     5
258f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
25900 45 58 54 0a 23 20 75 6e 64 65 66 20 53 51 4c 49  EXT.# undef SQLI
25910 54 45 5f 54 45 58 54 0a 23 65 6c 73 65 0a 23 20  TE_TEXT.#else.# 
25920 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
25930 58 54 20 20 20 20 20 33 0a 23 65 6e 64 69 66 0a  XT     3.#endif.
25940 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f  #define SQLITE3_
25950 54 45 58 54 20 20 20 20 20 33 0a 0a 2f 2a 0a 2a  TEXT     3../*.*
25960 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 73 75  * CAPI3REF: Resu
25970 6c 74 20 56 61 6c 75 65 73 20 46 72 6f 6d 20 41  lt Values From A
25980 20 51 75 65 72 79 20 7b 48 31 33 38 30 30 7d 20   Query {H13800} 
25990 3c 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57  <S10700>.** KEYW
259a0 4f 52 44 53 3a 20 7b 63 6f 6c 75 6d 6e 20 61 63  ORDS: {column ac
259b0 63 65 73 73 20 66 75 6e 63 74 69 6f 6e 73 7d 0a  cess functions}.
259c0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  **.** These rout
259d0 69 6e 65 73 20 66 6f 72 6d 20 74 68 65 20 22 72  ines form the "r
259e0 65 73 75 6c 74 20 73 65 74 20 71 75 65 72 79 22  esult set query"
259f0 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a   interface..**.*
25a00 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
25a10 20 72 65 74 75 72 6e 20 69 6e 66 6f 72 6d 61 74   return informat
25a20 69 6f 6e 20 61 62 6f 75 74 20 61 20 73 69 6e 67  ion about a sing
25a30 6c 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  le column of the
25a40 20 63 75 72 72 65 6e 74 0a 2a 2a 20 72 65 73 75   current.** resu
25a50 6c 74 20 72 6f 77 20 6f 66 20 61 20 71 75 65 72  lt row of a quer
25a60 79 2e 20 20 49 6e 20 65 76 65 72 79 20 63 61 73  y.  In every cas
25a70 65 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  e the first argu
25a80 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65  ment is a pointe
25a90 72 0a 2a 2a 20 74 6f 20 74 68 65 20 5b 70 72 65  r.** to the [pre
25aa0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
25ab0 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 20 65   that is being e
25ac0 76 61 6c 75 61 74 65 64 20 28 74 68 65 20 5b 73  valuated (the [s
25ad0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 5d 0a 2a 2a  qlite3_stmt*].**
25ae0 20 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e   that was return
25af0 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33  ed from [sqlite3
25b00 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f  _prepare_v2()] o
25b10 72 20 6f 6e 65 20 6f 66 20 69 74 73 20 76 61 72  r one of its var
25b20 69 61 6e 74 73 29 0a 2a 2a 20 61 6e 64 20 74 68  iants).** and th
25b30 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
25b40 74 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f  t is the index o
25b50 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 66 6f 72  f the column for
25b60 20 77 68 69 63 68 20 69 6e 66 6f 72 6d 61 74 69   which informati
25b70 6f 6e 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20  on.** should be 
25b80 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65 20 6c  returned.  The l
25b90 65 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f  eftmost column o
25ba0 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  f the result set
25bb0 20 68 61 73 20 74 68 65 20 69 6e 64 65 78 20 30   has the index 0
25bc0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53  ..**.** If the S
25bd0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 64 6f 65  QL statement doe
25be0 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
25bf0 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 64  point to a valid
25c00 20 72 6f 77 2c 20 6f 72 20 69 66 20 74 68 65 0a   row, or if the.
25c10 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20  ** column index 
25c20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c  is out of range,
25c30 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 75   the result is u
25c40 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 54 68 65  ndefined..** The
25c50 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 61 79 20  se routines may 
25c60 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 77  only be called w
25c70 68 65 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63  hen the most rec
25c80 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b  ent call to.** [
25c90 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
25ca0 68 61 73 20 72 65 74 75 72 6e 65 64 20 5b 53 51  has returned [SQ
25cb0 4c 49 54 45 5f 52 4f 57 5d 20 61 6e 64 20 6e 65  LITE_ROW] and ne
25cc0 69 74 68 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65  ither.** [sqlite
25cd0 33 5f 72 65 73 65 74 28 29 5d 20 6e 6f 72 20 5b  3_reset()] nor [
25ce0 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
25cf0 28 29 5d 20 68 61 76 65 20 62 65 65 6e 20 63 61  ()] have been ca
25d00 6c 6c 65 64 20 73 75 62 73 65 71 75 65 6e 74 6c  lled subsequentl
25d10 79 2e 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20  y..** If any of 
25d20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  these routines a
25d30 72 65 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20  re called after 
25d40 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29  [sqlite3_reset()
25d50 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  ] or.** [sqlite3
25d60 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20  _finalize()] or 
25d70 61 66 74 65 72 20 5b 73 71 6c 69 74 65 33 5f 73  after [sqlite3_s
25d80 74 65 70 28 29 5d 20 68 61 73 20 72 65 74 75 72  tep()] has retur
25d90 6e 65 64 0a 2a 2a 20 73 6f 6d 65 74 68 69 6e 67  ned.** something
25da0 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c   other than [SQL
25db0 49 54 45 5f 52 4f 57 5d 2c 20 74 68 65 20 72 65  ITE_ROW], the re
25dc0 73 75 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69  sults are undefi
25dd0 6e 65 64 2e 0a 2a 2a 20 49 66 20 5b 73 71 6c 69  ned..** If [sqli
25de0 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b  te3_step()] or [
25df0 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d  sqlite3_reset()]
25e00 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e   or [sqlite3_fin
25e10 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 61 72 65 20  alize()].** are 
25e20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 20 64 69  called from a di
25e30 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 20 77  fferent thread w
25e40 68 69 6c 65 20 61 6e 79 20 6f 66 20 74 68 65 73  hile any of thes
25e50 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72  e routines.** ar
25e60 65 20 70 65 6e 64 69 6e 67 2c 20 74 68 65 6e 20  e pending, then 
25e70 74 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20  the results are 
25e80 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a  undefined..**.**
25e90 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c   The sqlite3_col
25ea0 75 6d 6e 5f 74 79 70 65 28 29 20 72 6f 75 74 69  umn_type() routi
25eb0 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a  ne returns the.*
25ec0 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  * [SQLITE_INTEGE
25ed0 52 20 7c 20 64 61 74 61 74 79 70 65 20 63 6f 64  R | datatype cod
25ee0 65 5d 20 66 6f 72 20 74 68 65 20 69 6e 69 74 69  e] for the initi
25ef0 61 6c 20 64 61 74 61 20 74 79 70 65 0a 2a 2a 20  al data type.** 
25f00 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f  of the result co
25f10 6c 75 6d 6e 2e 20 20 54 68 65 20 72 65 74 75 72  lumn.  The retur
25f20 6e 65 64 20 76 61 6c 75 65 20 69 73 20 6f 6e 65  ned value is one
25f30 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45   of [SQLITE_INTE
25f40 47 45 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45  GER],.** [SQLITE
25f50 5f 46 4c 4f 41 54 5d 2c 20 5b 53 51 4c 49 54 45  _FLOAT], [SQLITE
25f60 5f 54 45 58 54 5d 2c 20 5b 53 51 4c 49 54 45 5f  _TEXT], [SQLITE_
25f70 42 4c 4f 42 5d 2c 20 6f 72 20 5b 53 51 4c 49 54  BLOB], or [SQLIT
25f80 45 5f 4e 55 4c 4c 5d 2e 20 20 54 68 65 20 76 61  E_NULL].  The va
25f90 6c 75 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  lue.** returned 
25fa0 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  by sqlite3_colum
25fb0 6e 5f 74 79 70 65 28 29 20 69 73 20 6f 6e 6c 79  n_type() is only
25fc0 20 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66 20 6e   meaningful if n
25fd0 6f 20 74 79 70 65 0a 2a 2a 20 63 6f 6e 76 65 72  o type.** conver
25fe0 73 69 6f 6e 73 20 68 61 76 65 20 6f 63 63 75 72  sions have occur
25ff0 72 65 64 20 61 73 20 64 65 73 63 72 69 62 65 64  red as described
26000 20 62 65 6c 6f 77 2e 20 20 41 66 74 65 72 20 61   below.  After a
26010 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e   type conversion
26020 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 72  ,.** the value r
26030 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74  eturned by sqlit
26040 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29  e3_column_type()
26050 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 20   is undefined.  
26060 46 75 74 75 72 65 0a 2a 2a 20 76 65 72 73 69 6f  Future.** versio
26070 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6d 61 79  ns of SQLite may
26080 20 63 68 61 6e 67 65 20 74 68 65 20 62 65 68 61   change the beha
26090 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 65 33 5f  vior of sqlite3_
260a0 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 0a 2a 2a  column_type().**
260b0 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 74 79 70   following a typ
260c0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a  e conversion..**
260d0 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c  .** If the resul
260e0 74 20 69 73 20 61 20 42 4c 4f 42 20 6f 72 20 55  t is a BLOB or U
260f0 54 46 2d 38 20 73 74 72 69 6e 67 20 74 68 65 6e  TF-8 string then
26100 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c   the sqlite3_col
26110 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20 72  umn_bytes().** r
26120 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74  outine returns t
26130 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
26140 65 73 20 69 6e 20 74 68 61 74 20 42 4c 4f 42 20  es in that BLOB 
26150 6f 72 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 49 66  or string..** If
26160 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61   the result is a
26170 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2c 20   UTF-16 string, 
26180 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c  then sqlite3_col
26190 75 6d 6e 5f 62 79 74 65 73 28 29 20 63 6f 6e 76  umn_bytes() conv
261a0 65 72 74 73 0a 2a 2a 20 74 68 65 20 73 74 72 69  erts.** the stri
261b0 6e 67 20 74 6f 20 55 54 46 2d 38 20 61 6e 64 20  ng to UTF-8 and 
261c0 74 68 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65  then returns the
261d0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
261e0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75  ..** If the resu
261f0 6c 74 20 69 73 20 61 20 6e 75 6d 65 72 69 63 20  lt is a numeric 
26200 76 61 6c 75 65 20 74 68 65 6e 20 73 71 6c 69 74  value then sqlit
26210 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
26220 29 20 75 73 65 73 0a 2a 2a 20 5b 73 71 6c 69 74  ) uses.** [sqlit
26230 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 5d 20 74  e3_snprintf()] t
26240 6f 20 63 6f 6e 76 65 72 74 20 74 68 61 74 20 76  o convert that v
26250 61 6c 75 65 20 74 6f 20 61 20 55 54 46 2d 38 20  alue to a UTF-8 
26260 73 74 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72  string and retur
26270 6e 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72  ns.** the number
26280 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 61   of bytes in tha
26290 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 54 68 65  t string..** The
262a0 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
262b0 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65  does not include
262c0 20 74 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e   the zero termin
262d0 61 74 6f 72 20 61 74 20 74 68 65 20 65 6e 64 0a  ator at the end.
262e0 2a 2a 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67  ** of the string
262f0 2e 20 20 46 6f 72 20 63 6c 61 72 69 74 79 3a 20  .  For clarity: 
26300 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
26310 65 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ed is the number
26320 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20   of.** bytes in 
26330 74 68 65 20 73 74 72 69 6e 67 2c 20 6e 6f 74 20  the string, not 
26340 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68  the number of ch
26350 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20  aracters..**.** 
26360 53 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65 64  Strings returned
26370 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   by sqlite3_colu
26380 6d 6e 5f 74 65 78 74 28 29 20 61 6e 64 20 73 71  mn_text() and sq
26390 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
263a0 74 31 36 28 29 2c 0a 2a 2a 20 65 76 65 6e 20 65  t16(),.** even e
263b0 6d 70 74 79 20 73 74 72 69 6e 67 73 2c 20 61 72  mpty strings, ar
263c0 65 20 61 6c 77 61 79 73 20 7a 65 72 6f 20 74 65  e always zero te
263d0 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 65 20 72  rminated.  The r
263e0 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 66  eturn.** value f
263f0 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  rom sqlite3_colu
26400 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f 72 20 61 20  mn_blob() for a 
26410 7a 65 72 6f 2d 6c 65 6e 67 74 68 20 42 4c 4f 42  zero-length BLOB
26420 20 69 73 20 61 6e 20 61 72 62 69 74 72 61 72 79   is an arbitrary
26430 0a 2a 2a 20 70 6f 69 6e 74 65 72 2c 20 70 6f 73  .** pointer, pos
26440 73 69 62 6c 79 20 65 76 65 6e 20 61 20 4e 55 4c  sibly even a NUL
26450 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a  L pointer..**.**
26460 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c   The sqlite3_col
26470 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 20 72 6f  umn_bytes16() ro
26480 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c 61 72  utine is similar
26490 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   to sqlite3_colu
264a0 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20 62 75  mn_bytes().** bu
264b0 74 20 6c 65 61 76 65 73 20 74 68 65 20 72 65 73  t leaves the res
264c0 75 6c 74 20 69 6e 20 55 54 46 2d 31 36 20 69 6e  ult in UTF-16 in
264d0 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64   native byte ord
264e0 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 55 54  er instead of UT
264f0 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 7a 65 72 6f  F-8..** The zero
26500 20 74 65 72 6d 69 6e 61 74 6f 72 20 69 73 20 6e   terminator is n
26510 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74  ot included in t
26520 68 69 73 20 63 6f 75 6e 74 2e 0a 2a 2a 0a 2a 2a  his count..**.**
26530 20 54 68 65 20 6f 62 6a 65 63 74 20 72 65 74 75   The object retu
26540 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33  rned by [sqlite3
26550 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d  _column_value()]
26560 20 69 73 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 6f   is an.** [unpro
26570 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76  tected sqlite3_v
26580 61 6c 75 65 5d 20 6f 62 6a 65 63 74 2e 20 20 41  alue] object.  A
26590 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71  n unprotected sq
265a0 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65  lite3_value obje
265b0 63 74 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62  ct.** may only b
265c0 65 20 75 73 65 64 20 77 69 74 68 20 5b 73 71 6c  e used with [sql
265d0 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28  ite3_bind_value(
265e0 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
265f0 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 5d 2e  result_value()].
26600 0a 2a 2a 20 49 66 20 74 68 65 20 5b 75 6e 70 72  .** If the [unpr
26610 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
26620 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 72 65  value] object re
26630 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 5b 73 71  turned by.** [sq
26640 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c  lite3_column_val
26650 75 65 28 29 5d 20 69 73 20 75 73 65 64 20 69 6e  ue()] is used in
26660 20 61 6e 79 20 6f 74 68 65 72 20 77 61 79 2c 20   any other way, 
26670 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c 6c 73 0a  including calls.
26680 2a 2a 20 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c  ** to routines l
26690 69 6b 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c  ike [sqlite3_val
266a0 75 65 5f 69 6e 74 28 29 5d 2c 20 5b 73 71 6c 69  ue_int()], [sqli
266b0 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29  te3_value_text()
266c0 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65  ],.** or [sqlite
266d0 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 29 5d  3_value_bytes()]
266e0 2c 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76  , then the behav
266f0 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ior is undefined
26700 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  ..**.** These ro
26710 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 20 74  utines attempt t
26720 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20 76 61  o convert the va
26730 6c 75 65 20 77 68 65 72 65 20 61 70 70 72 6f 70  lue where approp
26740 72 69 61 74 65 2e 20 20 46 6f 72 0a 2a 2a 20 65  riate.  For.** e
26750 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 69  xample, if the i
26760 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e  nternal represen
26770 74 61 74 69 6f 6e 20 69 73 20 46 4c 4f 41 54 20  tation is FLOAT 
26780 61 6e 64 20 61 20 74 65 78 74 20 72 65 73 75 6c  and a text resul
26790 74 0a 2a 2a 20 69 73 20 72 65 71 75 65 73 74 65  t.** is requeste
267a0 64 2c 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72  d, [sqlite3_snpr
267b0 69 6e 74 66 28 29 5d 20 69 73 20 75 73 65 64 20  intf()] is used 
267c0 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f 20 70 65  internally to pe
267d0 72 66 6f 72 6d 20 74 68 65 0a 2a 2a 20 63 6f 6e  rform the.** con
267e0 76 65 72 73 69 6f 6e 20 61 75 74 6f 6d 61 74 69  version automati
267f0 63 61 6c 6c 79 2e 20 20 54 68 65 20 66 6f 6c 6c  cally.  The foll
26800 6f 77 69 6e 67 20 74 61 62 6c 65 20 64 65 74 61  owing table deta
26810 69 6c 73 20 74 68 65 20 63 6f 6e 76 65 72 73 69  ils the conversi
26820 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20  ons.** that are 
26830 61 70 70 6c 69 65 64 3a 0a 2a 2a 0a 2a 2a 20 3c  applied:.**.** <
26840 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 20 3c  blockquote>.** <
26850 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 22 31 22  table border="1"
26860 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20 49 6e  >.** <tr><th> In
26870 74 65 72 6e 61 6c 3c 62 72 3e 54 79 70 65 20 3c  ternal<br>Type <
26880 74 68 3e 20 52 65 71 75 65 73 74 65 64 3c 62 72  th> Requested<br
26890 3e 54 79 70 65 20 3c 74 68 3e 20 20 43 6f 6e 76  >Type <th>  Conv
268a0 65 72 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72  ersion.**.** <tr
268b0 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c  ><td>  NULL    <
268c0 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c 74  td> INTEGER   <t
268d0 64 3e 20 52 65 73 75 6c 74 20 69 73 20 30 0a 2a  d> Result is 0.*
268e0 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c  * <tr><td>  NULL
268f0 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20      <td>  FLOAT 
26900 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20 69     <td> Result i
26910 73 20 30 2e 30 0a 2a 2a 20 3c 74 72 3e 3c 74 64  s 0.0.** <tr><td
26920 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20  >  NULL    <td> 
26930 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 52    TEXT    <td> R
26940 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f  esult is NULL po
26950 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64  inter.** <tr><td
26960 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20  >  NULL    <td> 
26970 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 52    BLOB    <td> R
26980 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f  esult is NULL po
26990 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64  inter.** <tr><td
269a0 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e 20  > INTEGER  <td> 
269b0 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 43   FLOAT    <td> C
269c0 6f 6e 76 65 72 74 20 66 72 6f 6d 20 69 6e 74 65  onvert from inte
269d0 67 65 72 20 74 6f 20 66 6c 6f 61 74 0a 2a 2a 20  ger to float.** 
269e0 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52  <tr><td> INTEGER
269f0 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20    <td>   TEXT   
26a00 20 3c 74 64 3e 20 41 53 43 49 49 20 72 65 6e 64   <td> ASCII rend
26a10 65 72 69 6e 67 20 6f 66 20 74 68 65 20 69 6e 74  ering of the int
26a20 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e  eger.** <tr><td>
26a30 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 20   INTEGER  <td>  
26a40 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 53 61   BLOB    <td> Sa
26a50 6d 65 20 61 73 20 49 4e 54 45 47 45 52 2d 3e 54  me as INTEGER->T
26a60 45 58 54 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  EXT.** <tr><td> 
26a70 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 49 4e   FLOAT   <td> IN
26a80 54 45 47 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e  TEGER   <td> Con
26a90 76 65 72 74 20 66 72 6f 6d 20 66 6c 6f 61 74 20  vert from float 
26aa0 74 6f 20 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 74  to integer.** <t
26ab0 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20  r><td>  FLOAT   
26ac0 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c  <td>   TEXT    <
26ad0 74 64 3e 20 41 53 43 49 49 20 72 65 6e 64 65 72  td> ASCII render
26ae0 69 6e 67 20 6f 66 20 74 68 65 20 66 6c 6f 61 74  ing of the float
26af0 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c  .** <tr><td>  FL
26b00 4f 41 54 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f  OAT   <td>   BLO
26b10 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65 20 61  B    <td> Same a
26b20 73 20 46 4c 4f 41 54 2d 3e 54 45 58 54 0a 2a 2a  s FLOAT->TEXT.**
26b30 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58 54 20   <tr><td>  TEXT 
26b40 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20     <td> INTEGER 
26b50 20 20 3c 74 64 3e 20 55 73 65 20 61 74 6f 69 28    <td> Use atoi(
26b60 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54  ).** <tr><td>  T
26b70 45 58 54 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f  EXT    <td>  FLO
26b80 41 54 20 20 20 20 3c 74 64 3e 20 55 73 65 20 61  AT    <td> Use a
26b90 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64  tof().** <tr><td
26ba0 3e 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20  >  TEXT    <td> 
26bb0 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 4e    BLOB    <td> N
26bc0 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 3c 74 72 3e  o change.** <tr>
26bd0 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74  <td>  BLOB    <t
26be0 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64  d> INTEGER   <td
26bf0 3e 20 43 6f 6e 76 65 72 74 20 74 6f 20 54 45 58  > Convert to TEX
26c00 54 20 74 68 65 6e 20 75 73 65 20 61 74 6f 69 28  T then use atoi(
26c10 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 42  ).** <tr><td>  B
26c20 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f  LOB    <td>  FLO
26c30 41 54 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65  AT    <td> Conve
26c40 72 74 20 74 6f 20 54 45 58 54 20 74 68 65 6e 20  rt to TEXT then 
26c50 75 73 65 20 61 74 6f 66 28 29 0a 2a 2a 20 3c 74  use atof().** <t
26c60 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20  r><td>  BLOB    
26c70 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c  <td>   TEXT    <
26c80 74 64 3e 20 41 64 64 20 61 20 7a 65 72 6f 20 74  td> Add a zero t
26c90 65 72 6d 69 6e 61 74 6f 72 20 69 66 20 6e 65 65  erminator if nee
26ca0 64 65 64 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a  ded.** </table>.
26cb0 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e  ** </blockquote>
26cc0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65  .**.** The table
26cd0 20 61 62 6f 76 65 20 6d 61 6b 65 73 20 72 65 66   above makes ref
26ce0 65 72 65 6e 63 65 20 74 6f 20 73 74 61 6e 64 61  erence to standa
26cf0 72 64 20 43 20 6c 69 62 72 61 72 79 20 66 75 6e  rd C library fun
26d00 63 74 69 6f 6e 73 20 61 74 6f 69 28 29 0a 2a 2a  ctions atoi().**
26d10 20 61 6e 64 20 61 74 6f 66 28 29 2e 20 20 53 51   and atof().  SQ
26d20 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 72 65  Lite does not re
26d30 61 6c 6c 79 20 75 73 65 20 74 68 65 73 65 20 66  ally use these f
26d40 75 6e 63 74 69 6f 6e 73 2e 20 20 49 74 20 68 61  unctions.  It ha
26d50 73 20 69 74 73 0a 2a 2a 20 6f 77 6e 20 65 71 75  s its.** own equ
26d60 69 76 61 6c 65 6e 74 20 69 6e 74 65 72 6e 61 6c  ivalent internal
26d70 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 68 65 20   routines.  The 
26d80 61 74 6f 69 28 29 20 61 6e 64 20 61 74 6f 66 28  atoi() and atof(
26d90 29 20 6e 61 6d 65 73 20 61 72 65 0a 2a 2a 20 75  ) names are.** u
26da0 73 65 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65  sed in the table
26db0 20 66 6f 72 20 62 72 65 76 69 74 79 20 61 6e 64   for brevity and
26dc0 20 62 65 63 61 75 73 65 20 74 68 65 79 20 61 72   because they ar
26dd0 65 20 66 61 6d 69 6c 69 61 72 20 74 6f 20 6d 6f  e familiar to mo
26de0 73 74 0a 2a 2a 20 43 20 70 72 6f 67 72 61 6d 6d  st.** C programm
26df0 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  ers..**.** Note 
26e00 74 68 61 74 20 77 68 65 6e 20 74 79 70 65 20 63  that when type c
26e10 6f 6e 76 65 72 73 69 6f 6e 73 20 6f 63 63 75 72  onversions occur
26e20 2c 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72  , pointers retur
26e30 6e 65 64 20 62 79 20 70 72 69 6f 72 0a 2a 2a 20  ned by prior.** 
26e40 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33  calls to sqlite3
26e50 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20  _column_blob(), 
26e60 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
26e70 65 78 74 28 29 2c 20 61 6e 64 2f 6f 72 0a 2a 2a  ext(), and/or.**
26e80 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
26e90 74 65 78 74 31 36 28 29 20 6d 61 79 20 62 65 20  text16() may be 
26ea0 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 20  invalidated..** 
26eb0 54 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73  Type conversions
26ec0 20 61 6e 64 20 70 6f 69 6e 74 65 72 20 69 6e 76   and pointer inv
26ed0 61 6c 69 64 61 74 69 6f 6e 73 20 6d 69 67 68 74  alidations might
26ee0 20 6f 63 63 75 72 0a 2a 2a 20 69 6e 20 74 68 65   occur.** in the
26ef0 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 73 65 73   following cases
26f00 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  :.**.** <ul>.** 
26f10 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61 6c  <li> The initial
26f20 20 63 6f 6e 74 65 6e 74 20 69 73 20 61 20 42 4c   content is a BL
26f30 4f 42 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63  OB and sqlite3_c
26f40 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a  olumn_text() or.
26f50 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  **      sqlite3_
26f60 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20  column_text16() 
26f70 69 73 20 63 61 6c 6c 65 64 2e 20 20 41 20 7a 65  is called.  A ze
26f80 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 20 6d 69  ro-terminator mi
26f90 67 68 74 0a 2a 2a 20 20 20 20 20 20 6e 65 65 64  ght.**      need
26fa0 20 74 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20   to be added to 
26fb0 74 68 65 20 73 74 72 69 6e 67 2e 3c 2f 6c 69 3e  the string.</li>
26fc0 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69  .** <li> The ini
26fd0 74 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20  tial content is 
26fe0 55 54 46 2d 38 20 74 65 78 74 20 61 6e 64 20 73  UTF-8 text and s
26ff0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
27000 74 65 73 31 36 28 29 20 6f 72 0a 2a 2a 20 20 20  tes16() or.**   
27010 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d     sqlite3_colum
27020 6e 5f 74 65 78 74 31 36 28 29 20 69 73 20 63 61  n_text16() is ca
27030 6c 6c 65 64 2e 20 20 54 68 65 20 63 6f 6e 74 65  lled.  The conte
27040 6e 74 20 6d 75 73 74 20 62 65 20 63 6f 6e 76 65  nt must be conve
27050 72 74 65 64 0a 2a 2a 20 20 20 20 20 20 74 6f 20  rted.**      to 
27060 55 54 46 2d 31 36 2e 3c 2f 6c 69 3e 0a 2a 2a 20  UTF-16.</li>.** 
27070 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61 6c  <li> The initial
27080 20 63 6f 6e 74 65 6e 74 20 69 73 20 55 54 46 2d   content is UTF-
27090 31 36 20 74 65 78 74 20 61 6e 64 20 73 71 6c 69  16 text and sqli
270a0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
270b0 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71  () or.**      sq
270c0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
270d0 74 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 20  t() is called.  
270e0 54 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74  The content must
270f0 20 62 65 20 63 6f 6e 76 65 72 74 65 64 0a 2a 2a   be converted.**
27100 20 20 20 20 20 20 74 6f 20 55 54 46 2d 38 2e 3c        to UTF-8.<
27110 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a  /li>.** </ul>.**
27120 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 73 20  .** Conversions 
27130 62 65 74 77 65 65 6e 20 55 54 46 2d 31 36 62 65  between UTF-16be
27140 20 61 6e 64 20 55 54 46 2d 31 36 6c 65 20 61 72   and UTF-16le ar
27150 65 20 61 6c 77 61 79 73 20 64 6f 6e 65 20 69 6e  e always done in
27160 20 70 6c 61 63 65 20 61 6e 64 20 64 6f 0a 2a 2a   place and do.**
27170 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 65 20   not invalidate 
27180 61 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 2c  a prior pointer,
27190 20 74 68 6f 75 67 68 20 6f 66 20 63 6f 75 72 73   though of cours
271a0 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  e the content of
271b0 20 74 68 65 20 62 75 66 66 65 72 0a 2a 2a 20 74   the buffer.** t
271c0 68 61 74 20 74 68 65 20 70 72 69 6f 72 20 70 6f  hat the prior po
271d0 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 20  inter points to 
271e0 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 6d  will have been m
271f0 6f 64 69 66 69 65 64 2e 20 20 4f 74 68 65 72 20  odified.  Other 
27200 6b 69 6e 64 73 0a 2a 2a 20 6f 66 20 63 6f 6e 76  kinds.** of conv
27210 65 72 73 69 6f 6e 20 61 72 65 20 64 6f 6e 65 20  ersion are done 
27220 69 6e 20 70 6c 61 63 65 20 77 68 65 6e 20 69 74  in place when it
27230 20 69 73 20 70 6f 73 73 69 62 6c 65 2c 20 62 75   is possible, bu
27240 74 20 73 6f 6d 65 74 69 6d 65 73 20 74 68 65 79  t sometimes they
27250 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 70 6f 73 73  .** are not poss
27260 69 62 6c 65 20 61 6e 64 20 69 6e 20 74 68 6f 73  ible and in thos
27270 65 20 63 61 73 65 73 20 70 72 69 6f 72 20 70 6f  e cases prior po
27280 69 6e 74 65 72 73 20 61 72 65 20 69 6e 76 61 6c  inters are inval
27290 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  idated..**.** Th
272a0 65 20 73 61 66 65 73 74 20 61 6e 64 20 65 61 73  e safest and eas
272b0 69 65 73 74 20 74 6f 20 72 65 6d 65 6d 62 65 72  iest to remember
272c0 20 70 6f 6c 69 63 79 20 69 73 20 74 6f 20 69 6e   policy is to in
272d0 76 6f 6b 65 20 74 68 65 73 65 20 72 6f 75 74 69  voke these routi
272e0 6e 65 73 0a 2a 2a 20 69 6e 20 6f 6e 65 20 6f 66  nes.** in one of
272f0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77   the following w
27300 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a  ays:.**.** <ul>.
27310 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f  **  <li>sqlite3_
27320 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 66 6f  column_text() fo
27330 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65  llowed by sqlite
27340 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29  3_column_bytes()
27350 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71  </li>.**  <li>sq
27360 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
27370 62 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  b() followed by 
27380 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
27390 79 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 20  ytes()</li>.**  
273a0 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c 75  <li>sqlite3_colu
273b0 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 6f 6c 6c  mn_text16() foll
273c0 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  owed by sqlite3_
273d0 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29  column_bytes16()
273e0 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a  </li>.** </ul>.*
273f0 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f  *.** In other wo
27400 72 64 73 2c 20 79 6f 75 20 73 68 6f 75 6c 64 20  rds, you should 
27410 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 63 6f 6c  call sqlite3_col
27420 75 6d 6e 5f 74 65 78 74 28 29 2c 0a 2a 2a 20 73  umn_text(),.** s
27430 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
27440 6f 62 28 29 2c 20 6f 72 20 73 71 6c 69 74 65 33  ob(), or sqlite3
27450 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29  _column_text16()
27460 20 66 69 72 73 74 20 74 6f 20 66 6f 72 63 65 20   first to force 
27470 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e  the result.** in
27480 74 6f 20 74 68 65 20 64 65 73 69 72 65 64 20 66  to the desired f
27490 6f 72 6d 61 74 2c 20 74 68 65 6e 20 69 6e 76 6f  ormat, then invo
274a0 6b 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ke sqlite3_colum
274b0 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a 2a 2a 20  n_bytes() or.** 
274c0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
274d0 79 74 65 73 31 36 28 29 20 74 6f 20 66 69 6e 64  ytes16() to find
274e0 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
274f0 20 72 65 73 75 6c 74 2e 20 20 44 6f 20 6e 6f 74   result.  Do not
27500 20 6d 69 78 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f   mix calls.** to
27510 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
27520 74 65 78 74 28 29 20 6f 72 20 73 71 6c 69 74 65  text() or sqlite
27530 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20  3_column_blob() 
27540 77 69 74 68 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a  with calls to.**
27550 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
27560 62 79 74 65 73 31 36 28 29 2c 20 61 6e 64 20 64  bytes16(), and d
27570 6f 20 6e 6f 74 20 6d 69 78 20 63 61 6c 6c 73 20  o not mix calls 
27580 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  to sqlite3_colum
27590 6e 5f 74 65 78 74 31 36 28 29 0a 2a 2a 20 77 69  n_text16().** wi
275a0 74 68 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  th calls to sqli
275b0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
275c0 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f  ()..**.** The po
275d0 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 64 20  inters returned 
275e0 61 72 65 20 76 61 6c 69 64 20 75 6e 74 69 6c 20  are valid until 
275f0 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f  a type conversio
27600 6e 20 6f 63 63 75 72 73 20 61 73 0a 2a 2a 20 64  n occurs as.** d
27610 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2c 20  escribed above, 
27620 6f 72 20 75 6e 74 69 6c 20 5b 73 71 6c 69 74 65  or until [sqlite
27630 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b 73 71  3_step()] or [sq
27640 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f  lite3_reset()] o
27650 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69  r.** [sqlite3_fi
27660 6e 61 6c 69 7a 65 28 29 5d 20 69 73 20 63 61 6c  nalize()] is cal
27670 6c 65 64 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79  led.  The memory
27680 20 73 70 61 63 65 20 75 73 65 64 20 74 6f 20 68   space used to h
27690 6f 6c 64 20 73 74 72 69 6e 67 73 0a 2a 2a 20 61  old strings.** a
276a0 6e 64 20 42 4c 4f 42 73 20 69 73 20 66 72 65 65  nd BLOBs is free
276b0 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e  d automatically.
276c0 20 20 44 6f 20 3c 62 3e 6e 6f 74 3c 2f 62 3e 20    Do <b>not</b> 
276d0 70 61 73 73 20 74 68 65 20 70 6f 69 6e 74 65 72  pass the pointer
276e0 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 5b 73  s returned.** [s
276f0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
27700 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f  ob()], [sqlite3_
27710 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 5d 2c 20  column_text()], 
27720 65 74 63 2e 20 69 6e 74 6f 0a 2a 2a 20 5b 73 71  etc. into.** [sq
27730 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a  lite3_free()]..*
27740 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79  *.** If a memory
27750 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f   allocation erro
27760 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20  r occurs during 
27770 74 68 65 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f  the evaluation o
27780 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 65 73  f any.** of thes
27790 65 20 72 6f 75 74 69 6e 65 73 2c 20 61 20 64 65  e routines, a de
277a0 66 61 75 6c 74 20 76 61 6c 75 65 20 69 73 20 72  fault value is r
277b0 65 74 75 72 6e 65 64 2e 20 20 54 68 65 20 64 65  eturned.  The de
277c0 66 61 75 6c 74 20 76 61 6c 75 65 0a 2a 2a 20 69  fault value.** i
277d0 73 20 65 69 74 68 65 72 20 74 68 65 20 69 6e 74  s either the int
277e0 65 67 65 72 20 30 2c 20 74 68 65 20 66 6c 6f 61  eger 0, the floa
277f0 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65  ting point numbe
27800 72 20 30 2e 30 2c 20 6f 72 20 61 20 4e 55 4c 4c  r 0.0, or a NULL
27810 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 20 53 75  .** pointer.  Su
27820 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74  bsequent calls t
27830 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  o [sqlite3_errco
27840 64 65 28 29 5d 20 77 69 6c 6c 20 72 65 74 75 72  de()] will retur
27850 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d  n.** [SQLITE_NOM
27860 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  EM]..**.** Requi
27870 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33  rements:.** [H13
27880 38 30 33 5d 20 5b 48 31 33 38 30 36 5d 20 5b 48  803] [H13806] [H
27890 31 33 38 30 39 5d 20 5b 48 31 33 38 31 32 5d 20  13809] [H13812] 
278a0 5b 48 31 33 38 31 35 5d 20 5b 48 31 33 38 31 38  [H13815] [H13818
278b0 5d 20 5b 48 31 33 38 32 31 5d 20 5b 48 31 33 38  ] [H13821] [H138
278c0 32 34 5d 0a 2a 2a 20 5b 48 31 33 38 32 37 5d 20  24].** [H13827] 
278d0 5b 48 31 33 38 33 30 5d 0a 2a 2f 0a 53 51 4c 49  [H13830].*/.SQLI
278e0 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
278f0 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  d *sqlite3_colum
27900 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73  n_blob(sqlite3_s
27910 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b  tmt*, int iCol);
27920 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
27930 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
27940 79 74 65 73 28 73 71 6c 69 74 65 33 5f 73 74 6d  ytes(sqlite3_stm
27950 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53  t*, int iCol);.S
27960 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
27970 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
27980 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d  es16(sqlite3_stm
27990 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53  t*, int iCol);.S
279a0 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65  QLITE_API double
279b0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
279c0 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73  double(sqlite3_s
279d0 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b  tmt*, int iCol);
279e0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
279f0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
27a00 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  nt(sqlite3_stmt*
27a10 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c  , int iCol);.SQL
27a20 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
27a30 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 63 6f  int64 sqlite3_co
27a40 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c 69 74  lumn_int64(sqlit
27a50 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43  e3_stmt*, int iC
27a60 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  ol);.SQLITE_API 
27a70 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
27a80 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  har *sqlite3_col
27a90 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74 65 33  umn_text(sqlite3
27aa0 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c  _stmt*, int iCol
27ab0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  );.SQLITE_API co
27ac0 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
27ad0 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28  3_column_text16(
27ae0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
27af0 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45  nt iCol);.SQLITE
27b00 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
27b10 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 71 6c  _column_type(sql
27b20 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20  ite3_stmt*, int 
27b30 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50  iCol);.SQLITE_AP
27b40 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  I sqlite3_value 
27b50 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
27b60 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73 74  value(sqlite3_st
27b70 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a  mt*, int iCol);.
27b80 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
27b90 20 44 65 73 74 72 6f 79 20 41 20 50 72 65 70 61   Destroy A Prepa
27ba0 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 4f 62  red Statement Ob
27bb0 6a 65 63 74 20 7b 48 31 33 33 30 30 7d 20 3c 53  ject {H13300} <S
27bc0 37 30 33 30 30 3e 3c 53 33 30 31 30 30 3e 0a 2a  70300><S30100>.*
27bd0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
27be0 5f 66 69 6e 61 6c 69 7a 65 28 29 20 66 75 6e 63  _finalize() func
27bf0 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74  tion is called t
27c00 6f 20 64 65 6c 65 74 65 20 61 20 5b 70 72 65 70  o delete a [prep
27c10 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e  ared statement].
27c20 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 61 74 65  .** If the state
27c30 6d 65 6e 74 20 77 61 73 20 65 78 65 63 75 74 65  ment was execute
27c40 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f  d successfully o
27c50 72 20 6e 6f 74 20 65 78 65 63 75 74 65 64 20 61  r not executed a
27c60 74 20 61 6c 6c 2c 20 74 68 65 6e 0a 2a 2a 20 53  t all, then.** S
27c70 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
27c80 72 6e 65 64 2e 20 49 66 20 65 78 65 63 75 74 69  rned. If executi
27c90 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d  on of the statem
27ca0 65 6e 74 20 66 61 69 6c 65 64 20 74 68 65 6e 20  ent failed then 
27cb0 61 6e 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64  an.** [error cod
27cc0 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65 64 20  e] or [extended 
27cd0 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72  error code] is r
27ce0 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  eturned..**.** T
27cf0 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20  his routine can 
27d00 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61 6e 79  be called at any
27d10 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 20 74 68   point during th
27d20 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74  e execution of t
27d30 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20  he.** [prepared 
27d40 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49 66 20  statement].  If 
27d50 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68  the virtual mach
27d60 69 6e 65 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 63  ine has not.** c
27d70 6f 6d 70 6c 65 74 65 64 20 65 78 65 63 75 74 69  ompleted executi
27d80 6f 6e 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75  on when this rou
27d90 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20  tine is called, 
27da0 74 68 61 74 20 69 73 20 6c 69 6b 65 0a 2a 2a 20  that is like.** 
27db0 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 61 6e 20  encountering an 
27dc0 65 72 72 6f 72 20 6f 72 20 61 6e 20 5b 73 71 6c  error or an [sql
27dd0 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 20 7c  ite3_interrupt |
27de0 20 69 6e 74 65 72 72 75 70 74 5d 2e 0a 2a 2a 20   interrupt]..** 
27df0 49 6e 63 6f 6d 70 6c 65 74 65 20 75 70 64 61 74  Incomplete updat
27e00 65 73 20 6d 61 79 20 62 65 20 72 6f 6c 6c 65 64  es may be rolled
27e10 20 62 61 63 6b 20 61 6e 64 20 74 72 61 6e 73 61   back and transa
27e20 63 74 69 6f 6e 73 20 63 61 6e 63 65 6c 65 64 2c  ctions canceled,
27e30 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  .** depending on
27e40 20 74 68 65 20 63 69 72 63 75 6d 73 74 61 6e 63   the circumstanc
27e50 65 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a 20 5b  es, and the.** [
27e60 65 72 72 6f 72 20 63 6f 64 65 5d 20 72 65 74 75  error code] retu
27e70 72 6e 65 64 20 77 69 6c 6c 20 62 65 20 5b 53 51  rned will be [SQ
27e80 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a  LITE_ABORT]..**.
27e90 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
27ea0 0a 2a 2a 20 5b 48 31 31 33 30 32 5d 20 5b 48 31  .** [H11302] [H1
27eb0 31 33 30 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  1304].*/.SQLITE_
27ec0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
27ed0 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33  finalize(sqlite3
27ee0 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a  _stmt *pStmt);..
27ef0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
27f00 52 65 73 65 74 20 41 20 50 72 65 70 61 72 65 64  Reset A Prepared
27f10 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a 65 63   Statement Objec
27f20 74 20 7b 48 31 33 33 33 30 7d 20 3c 53 37 30 33  t {H13330} <S703
27f30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  00>.**.** The sq
27f40 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20 66 75  lite3_reset() fu
27f50 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
27f60 20 74 6f 20 72 65 73 65 74 20 61 20 5b 70 72 65   to reset a [pre
27f70 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
27f80 0a 2a 2a 20 6f 62 6a 65 63 74 20 62 61 63 6b 20  .** object back 
27f90 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c 20 73  to its initial s
27fa0 74 61 74 65 2c 20 72 65 61 64 79 20 74 6f 20 62  tate, ready to b
27fb0 65 20 72 65 2d 65 78 65 63 75 74 65 64 2e 0a 2a  e re-executed..*
27fc0 2a 20 41 6e 79 20 53 51 4c 20 73 74 61 74 65 6d  * Any SQL statem
27fd0 65 6e 74 20 76 61 72 69 61 62 6c 65 73 20 74 68  ent variables th
27fe0 61 74 20 68 61 64 20 76 61 6c 75 65 73 20 62 6f  at had values bo
27ff0 75 6e 64 20 74 6f 20 74 68 65 6d 20 75 73 69 6e  und to them usin
28000 67 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65  g.** the [sqlite
28010 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 73 71  3_bind_blob | sq
28020 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 41  lite3_bind_*() A
28030 50 49 5d 20 72 65 74 61 69 6e 20 74 68 65 69 72  PI] retain their
28040 20 76 61 6c 75 65 73 2e 0a 2a 2a 20 55 73 65 20   values..** Use 
28050 5b 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62  [sqlite3_clear_b
28060 69 6e 64 69 6e 67 73 28 29 5d 20 74 6f 20 72 65  indings()] to re
28070 73 65 74 20 74 68 65 20 62 69 6e 64 69 6e 67 73  set the bindings
28080 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 33 32 7d  ..**.** {H11332}
28090 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65   The [sqlite3_re
280a0 73 65 74 28 53 29 5d 20 69 6e 74 65 72 66 61 63  set(S)] interfac
280b0 65 20 72 65 73 65 74 73 20 74 68 65 20 5b 70 72  e resets the [pr
280c0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
280d0 5d 20 53 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ] S.**          
280e0 62 61 63 6b 20 74 6f 20 74 68 65 20 62 65 67 69  back to the begi
280f0 6e 6e 69 6e 67 20 6f 66 20 69 74 73 20 70 72 6f  nning of its pro
28100 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31  gram..**.** {H11
28110 33 33 34 7d 20 49 66 20 74 68 65 20 6d 6f 73 74  334} If the most
28120 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20   recent call to 
28130 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29  [sqlite3_step(S)
28140 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20  ] for the.**    
28150 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20        [prepared 
28160 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 72 65 74  statement] S ret
28170 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 52 4f  urned [SQLITE_RO
28180 57 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 4f  W] or [SQLITE_DO
28190 4e 45 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  NE],.**         
281a0 20 6f 72 20 69 66 20 5b 73 71 6c 69 74 65 33 5f   or if [sqlite3_
281b0 73 74 65 70 28 53 29 5d 20 68 61 73 20 6e 65 76  step(S)] has nev
281c0 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e 20 63  er before been c
281d0 61 6c 6c 65 64 20 6f 6e 20 53 2c 0a 2a 2a 20 20  alled on S,.**  
281e0 20 20 20 20 20 20 20 20 74 68 65 6e 20 5b 73 71          then [sq
281f0 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d 20  lite3_reset(S)] 
28200 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f  returns [SQLITE_
28210 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33  OK]..**.** {H113
28220 33 36 7d 20 49 66 20 74 68 65 20 6d 6f 73 74 20  36} If the most 
28230 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 5b  recent call to [
28240 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 5d  sqlite3_step(S)]
28250 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20   for the.**     
28260 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73       [prepared s
28270 74 61 74 65 6d 65 6e 74 5d 20 53 20 69 6e 64 69  tatement] S indi
28280 63 61 74 65 64 20 61 6e 20 65 72 72 6f 72 2c 20  cated an error, 
28290 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  then.**         
282a0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28   [sqlite3_reset(
282b0 53 29 5d 20 72 65 74 75 72 6e 73 20 61 6e 20 61  S)] returns an a
282c0 70 70 72 6f 70 72 69 61 74 65 20 5b 65 72 72 6f  ppropriate [erro
282d0 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a 20 7b  r code]..**.** {
282e0 48 31 31 33 33 38 7d 20 54 68 65 20 5b 73 71 6c  H11338} The [sql
282f0 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d 20 69  ite3_reset(S)] i
28300 6e 74 65 72 66 61 63 65 20 64 6f 65 73 20 6e 6f  nterface does no
28310 74 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c  t change the val
28320 75 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ues.**          
28330 6f 66 20 61 6e 79 20 5b 73 71 6c 69 74 65 33 5f  of any [sqlite3_
28340 62 69 6e 64 5f 62 6c 6f 62 7c 62 69 6e 64 69 6e  bind_blob|bindin
28350 67 73 5d 20 6f 6e 20 74 68 65 20 5b 70 72 65 70  gs] on the [prep
28360 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
28370 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  S..*/.SQLITE_API
28380 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 73   int sqlite3_res
28390 65 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  et(sqlite3_stmt 
283a0 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20  *pStmt);../*.** 
283b0 43 41 50 49 33 52 45 46 3a 20 43 72 65 61 74 65  CAPI3REF: Create
283c0 20 4f 72 20 52 65 64 65 66 69 6e 65 20 53 51 4c   Or Redefine SQL
283d0 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 36 31   Functions {H161
283e0 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 20  00} <S20200>.** 
283f0 4b 45 59 57 4f 52 44 53 3a 20 7b 66 75 6e 63 74  KEYWORDS: {funct
28400 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72 6f 75  ion creation rou
28410 74 69 6e 65 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52  tines}.** KEYWOR
28420 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 69 6f 6e  DS: {application
28430 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e  -defined SQL fun
28440 63 74 69 6f 6e 7d 0a 2a 2a 20 4b 45 59 57 4f 52  ction}.** KEYWOR
28450 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 69 6f 6e  DS: {application
28460 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e  -defined SQL fun
28470 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 54 68  ctions}.**.** Th
28480 65 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e  ese two function
28490 73 20 28 63 6f 6c 6c 65 63 74 69 76 65 6c 79 20  s (collectively 
284a0 6b 6e 6f 77 6e 20 61 73 20 22 66 75 6e 63 74 69  known as "functi
284b0 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72 6f 75 74  on creation rout
284c0 69 6e 65 73 22 29 0a 2a 2a 20 61 72 65 20 75 73  ines").** are us
284d0 65 64 20 74 6f 20 61 64 64 20 53 51 4c 20 66 75  ed to add SQL fu
284e0 6e 63 74 69 6f 6e 73 20 6f 72 20 61 67 67 72 65  nctions or aggre
284f0 67 61 74 65 73 20 6f 72 20 74 6f 20 72 65 64 65  gates or to rede
28500 66 69 6e 65 20 74 68 65 20 62 65 68 61 76 69 6f  fine the behavio
28510 72 0a 2a 2a 20 6f 66 20 65 78 69 73 74 69 6e 67  r.** of existing
28520 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6f   SQL functions o
28530 72 20 61 67 67 72 65 67 61 74 65 73 2e 20 20 54  r aggregates.  T
28540 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e  he only differen
28550 63 65 20 62 65 74 77 65 65 6e 20 74 68 65 0a 2a  ce between the.*
28560 2a 20 74 77 6f 20 69 73 20 74 68 61 74 20 74 68  * two is that th
28570 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
28580 65 72 2c 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  er, the name of 
28590 74 68 65 20 28 73 63 61 6c 61 72 29 20 66 75 6e  the (scalar) fun
285a0 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72  ction or.** aggr
285b0 65 67 61 74 65 2c 20 69 73 20 65 6e 63 6f 64 65  egate, is encode
285c0 64 20 69 6e 20 55 54 46 2d 38 20 66 6f 72 20 73  d in UTF-8 for s
285d0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
285e0 6e 63 74 69 6f 6e 28 29 20 61 6e 64 20 55 54 46  nction() and UTF
285f0 2d 31 36 0a 2a 2a 20 66 6f 72 20 73 71 6c 69 74  -16.** for sqlit
28600 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
28610 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  on16()..**.** Th
28620 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
28630 72 20 69 73 20 74 68 65 20 5b 64 61 74 61 62 61  r is the [databa
28640 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74  se connection] t
28650 6f 20 77 68 69 63 68 20 74 68 65 20 53 51 4c 0a  o which the SQL.
28660 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74  ** function is t
28670 6f 20 62 65 20 61 64 64 65 64 2e 20 20 49 66 20  o be added.  If 
28680 61 20 73 69 6e 67 6c 65 20 70 72 6f 67 72 61 6d  a single program
28690 20 75 73 65 73 20 6d 6f 72 65 20 74 68 61 6e 20   uses more than 
286a0 6f 6e 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  one database.** 
286b0 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 74 65 72  connection inter
286c0 6e 61 6c 6c 79 2c 20 74 68 65 6e 20 53 51 4c 20  nally, then SQL 
286d0 66 75 6e 63 74 69 6f 6e 73 20 6d 75 73 74 20 62  functions must b
286e0 65 20 61 64 64 65 64 20 69 6e 64 69 76 69 64 75  e added individu
286f0 61 6c 6c 79 20 74 6f 0a 2a 2a 20 65 61 63 68 20  ally to.** each 
28700 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
28710 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ion..**.** The s
28720 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
28730 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
28740 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  he SQL function 
28750 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 6f 72  to be created or
28760 0a 2a 2a 20 72 65 64 65 66 69 6e 65 64 2e 20 20  .** redefined.  
28770 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68  The length of th
28780 65 20 6e 61 6d 65 20 69 73 20 6c 69 6d 69 74 65  e name is limite
28790 64 20 74 6f 20 32 35 35 20 62 79 74 65 73 2c 20  d to 255 bytes, 
287a0 65 78 63 6c 75 73 69 76 65 20 6f 66 0a 2a 2a 20  exclusive of.** 
287b0 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61  the zero-termina
287c0 74 6f 72 2e 20 20 4e 6f 74 65 20 74 68 61 74 20  tor.  Note that 
287d0 74 68 65 20 6e 61 6d 65 20 6c 65 6e 67 74 68 20  the name length 
287e0 6c 69 6d 69 74 20 69 73 20 69 6e 20 62 79 74 65  limit is in byte
287f0 73 2c 20 6e 6f 74 0a 2a 2a 20 63 68 61 72 61 63  s, not.** charac
28800 74 65 72 73 2e 20 20 41 6e 79 20 61 74 74 65 6d  ters.  Any attem
28810 70 74 20 74 6f 20 63 72 65 61 74 65 20 61 20 66  pt to create a f
28820 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 20 6c  unction with a l
28830 6f 6e 67 65 72 20 6e 61 6d 65 0a 2a 2a 20 77 69  onger name.** wi
28840 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 5b 53 51  ll result in [SQ
28850 4c 49 54 45 5f 45 52 52 4f 52 5d 20 62 65 69 6e  LITE_ERROR] bein
28860 67 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  g returned..**.*
28870 2a 20 54 68 65 20 74 68 69 72 64 20 70 61 72 61  * The third para
28880 6d 65 74 65 72 20 28 6e 41 72 67 29 0a 2a 2a 20  meter (nArg).** 
28890 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
288a0 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 20   arguments that 
288b0 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  the SQL function
288c0 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65   or.** aggregate
288d0 20 74 61 6b 65 73 2e 20 49 66 20 74 68 69 73 20   takes. If this 
288e0 70 61 72 61 6d 65 74 65 72 20 69 73 20 2d 31 2c  parameter is -1,
288f0 20 74 68 65 6e 20 74 68 65 20 53 51 4c 20 66 75   then the SQL fu
28900 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67  nction or.** agg
28910 72 65 67 61 74 65 20 6d 61 79 20 74 61 6b 65 20  regate may take 
28920 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 61 72  any number of ar
28930 67 75 6d 65 6e 74 73 20 62 65 74 77 65 65 6e 20  guments between 
28940 30 20 61 6e 64 20 74 68 65 20 6c 69 6d 69 74 0a  0 and the limit.
28950 2a 2a 20 73 65 74 20 62 79 20 5b 73 71 6c 69 74  ** set by [sqlit
28960 65 33 5f 6c 69 6d 69 74 5d 28 5b 53 51 4c 49 54  e3_limit]([SQLIT
28970 45 5f 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e  E_LIMIT_FUNCTION
28980 5f 41 52 47 5d 29 2e 20 20 49 66 20 74 68 65 20  _ARG]).  If the 
28990 74 68 69 72 64 0a 2a 2a 20 70 61 72 61 6d 65 74  third.** paramet
289a0 65 72 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20  er is less than 
289b0 2d 31 20 6f 72 20 67 72 65 61 74 65 72 20 74 68  -1 or greater th
289c0 61 6e 20 31 32 37 20 74 68 65 6e 20 74 68 65 20  an 127 then the 
289d0 62 65 68 61 76 69 6f 72 20 69 73 0a 2a 2a 20 75  behavior is.** u
289e0 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  ndefined..**.** 
289f0 54 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d  The fourth param
28a00 65 74 65 72 2c 20 65 54 65 78 74 52 65 70 2c 20  eter, eTextRep, 
28a10 73 70 65 63 69 66 69 65 73 20 77 68 61 74 0a 2a  specifies what.*
28a20 2a 20 5b 53 51 4c 49 54 45 5f 55 54 46 38 20 7c  * [SQLITE_UTF8 |
28a30 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 5d 20   text encoding] 
28a40 74 68 69 73 20 53 51 4c 20 66 75 6e 63 74 69 6f  this SQL functio
28a50 6e 20 70 72 65 66 65 72 73 20 66 6f 72 0a 2a 2a  n prefers for.**
28a60 20 69 74 73 20 70 61 72 61 6d 65 74 65 72 73 2e   its parameters.
28a70 20 20 41 6e 79 20 53 51 4c 20 66 75 6e 63 74 69    Any SQL functi
28a80 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  on implementatio
28a90 6e 20 73 68 6f 75 6c 64 20 62 65 20 61 62 6c 65  n should be able
28aa0 20 74 6f 20 77 6f 72 6b 0a 2a 2a 20 77 6f 72 6b   to work.** work
28ab0 20 77 69 74 68 20 55 54 46 2d 38 2c 20 55 54 46   with UTF-8, UTF
28ac0 2d 31 36 6c 65 2c 20 6f 72 20 55 54 46 2d 31 36  -16le, or UTF-16
28ad0 62 65 2e 20 20 42 75 74 20 73 6f 6d 65 20 69 6d  be.  But some im
28ae0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6d 61  plementations ma
28af0 79 20 62 65 0a 2a 2a 20 6d 6f 72 65 20 65 66 66  y be.** more eff
28b00 69 63 69 65 6e 74 20 77 69 74 68 20 6f 6e 65 20  icient with one 
28b10 65 6e 63 6f 64 69 6e 67 20 74 68 61 6e 20 61 6e  encoding than an
28b20 6f 74 68 65 72 2e 20 20 41 6e 20 61 70 70 6c 69  other.  An appli
28b30 63 61 74 69 6f 6e 20 6d 61 79 0a 2a 2a 20 69 6e  cation may.** in
28b40 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 63 72 65  voke sqlite3_cre
28b50 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 20 6f  ate_function() o
28b60 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  r sqlite3_create
28b70 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 20 6d 75  _function16() mu
28b80 6c 74 69 70 6c 65 0a 2a 2a 20 74 69 6d 65 73 20  ltiple.** times 
28b90 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 66 75  with the same fu
28ba0 6e 63 74 69 6f 6e 20 62 75 74 20 77 69 74 68 20  nction but with 
28bb0 64 69 66 66 65 72 65 6e 74 20 76 61 6c 75 65 73  different values
28bc0 20 6f 66 20 65 54 65 78 74 52 65 70 2e 0a 2a 2a   of eTextRep..**
28bd0 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c 65 20 69   When multiple i
28be0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f  mplementations o
28bf0 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74  f the same funct
28c00 69 6f 6e 20 61 72 65 20 61 76 61 69 6c 61 62 6c  ion are availabl
28c10 65 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c  e, SQLite.** wil
28c20 6c 20 70 69 63 6b 20 74 68 65 20 6f 6e 65 20 74  l pick the one t
28c30 68 61 74 20 69 6e 76 6f 6c 76 65 73 20 74 68 65  hat involves the
28c40 20 6c 65 61 73 74 20 61 6d 6f 75 6e 74 20 6f 66   least amount of
28c50 20 64 61 74 61 20 63 6f 6e 76 65 72 73 69 6f 6e   data conversion
28c60 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73  ..** If there is
28c70 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 69   only a single i
28c80 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 68  mplementation wh
28c90 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 63 61 72  ich does not car
28ca0 65 20 77 68 61 74 20 74 65 78 74 0a 2a 2a 20 65  e what text.** e
28cb0 6e 63 6f 64 69 6e 67 20 69 73 20 75 73 65 64 2c  ncoding is used,
28cc0 20 74 68 65 6e 20 74 68 65 20 66 6f 75 72 74 68   then the fourth
28cd0 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64   argument should
28ce0 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 4e 59 5d   be [SQLITE_ANY]
28cf0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 66 74  ..**.** The fift
28d00 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61  h parameter is a
28d10 6e 20 61 72 62 69 74 72 61 72 79 20 70 6f 69 6e  n arbitrary poin
28d20 74 65 72 2e 20 20 54 68 65 20 69 6d 70 6c 65 6d  ter.  The implem
28d30 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a  entation of the.
28d40 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20  ** function can 
28d50 67 61 69 6e 20 61 63 63 65 73 73 20 74 6f 20 74  gain access to t
28d60 68 69 73 20 70 6f 69 6e 74 65 72 20 75 73 69 6e  his pointer usin
28d70 67 20 5b 73 71 6c 69 74 65 33 5f 75 73 65 72 5f  g [sqlite3_user_
28d80 64 61 74 61 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54  data()]..**.** T
28d90 68 65 20 73 65 76 65 6e 74 68 2c 20 65 69 67 68  he seventh, eigh
28da0 74 68 20 61 6e 64 20 6e 69 6e 74 68 20 70 61 72  th and ninth par
28db0 61 6d 65 74 65 72 73 2c 20 78 46 75 6e 63 2c 20  ameters, xFunc, 
28dc0 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e 61 6c  xStep and xFinal
28dd0 2c 20 61 72 65 0a 2a 2a 20 70 6f 69 6e 74 65 72  , are.** pointer
28de0 73 20 74 6f 20 43 2d 6c 61 6e 67 75 61 67 65 20  s to C-language 
28df0 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69  functions that i
28e00 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c  mplement the SQL
28e10 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20   function or.** 
28e20 61 67 67 72 65 67 61 74 65 2e 20 41 20 73 63 61  aggregate. A sca
28e30 6c 61 72 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  lar SQL function
28e40 20 72 65 71 75 69 72 65 73 20 61 6e 20 69 6d 70   requires an imp
28e50 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
28e60 68 65 20 78 46 75 6e 63 0a 2a 2a 20 63 61 6c 6c  he xFunc.** call
28e70 62 61 63 6b 20 6f 6e 6c 79 2c 20 4e 55 4c 4c 20  back only, NULL 
28e80 70 6f 69 6e 74 65 72 73 20 73 68 6f 75 6c 64 20  pointers should 
28e90 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65  be passed as the
28ea0 20 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e 61   xStep and xFina
28eb0 6c 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 2e  l.** parameters.
28ec0 20 41 6e 20 61 67 67 72 65 67 61 74 65 20 53 51   An aggregate SQ
28ed0 4c 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69  L function requi
28ee0 72 65 73 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74  res an implement
28ef0 61 74 69 6f 6e 20 6f 66 20 78 53 74 65 70 0a 2a  ation of xStep.*
28f00 2a 20 61 6e 64 20 78 46 69 6e 61 6c 20 61 6e 64  * and xFinal and
28f10 20 4e 55 4c 4c 20 73 68 6f 75 6c 64 20 62 65 20   NULL should be 
28f20 70 61 73 73 65 64 20 66 6f 72 20 78 46 75 6e 63  passed for xFunc
28f30 2e 20 54 6f 20 64 65 6c 65 74 65 20 61 6e 20 65  . To delete an e
28f40 78 69 73 74 69 6e 67 0a 2a 2a 20 53 51 4c 20 66  xisting.** SQL f
28f50 75 6e 63 74 69 6f 6e 20 6f 72 20 61 67 67 72 65  unction or aggre
28f60 67 61 74 65 2c 20 70 61 73 73 20 4e 55 4c 4c 20  gate, pass NULL 
28f70 66 6f 72 20 61 6c 6c 20 74 68 72 65 65 20 66 75  for all three fu
28f80 6e 63 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 73  nction callbacks
28f90 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 70 65  ..**.** It is pe
28fa0 72 6d 69 74 74 65 64 20 74 6f 20 72 65 67 69 73  rmitted to regis
28fb0 74 65 72 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70  ter multiple imp
28fc0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20  lementations of 
28fd0 74 68 65 20 73 61 6d 65 0a 2a 2a 20 66 75 6e 63  the same.** func
28fe0 74 69 6f 6e 73 20 77 69 74 68 20 74 68 65 20 73  tions with the s
28ff0 61 6d 65 20 6e 61 6d 65 20 62 75 74 20 77 69 74  ame name but wit
29000 68 20 65 69 74 68 65 72 20 64 69 66 66 65 72 69  h either differi
29010 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 0a 2a 2a  ng numbers of.**
29020 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20 64 69   arguments or di
29030 66 66 65 72 69 6e 67 20 70 72 65 66 65 72 72 65  ffering preferre
29040 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 73  d text encodings
29050 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75  .  SQLite will u
29060 73 65 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d  se.** the implem
29070 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 6d 6f  entation that mo
29080 73 74 20 63 6c 6f 73 65 6c 79 20 6d 61 74 63 68  st closely match
29090 65 73 20 74 68 65 20 77 61 79 20 69 6e 20 77 68  es the way in wh
290a0 69 63 68 20 74 68 65 0a 2a 2a 20 53 51 4c 20 66  ich the.** SQL f
290b0 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 2e  unction is used.
290c0 20 20 41 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70    A function imp
290d0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 74 68  lementation with
290e0 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 0a   a non-negative.
290f0 2a 2a 20 6e 41 72 67 20 70 61 72 61 6d 65 74 65  ** nArg paramete
29100 72 20 69 73 20 61 20 62 65 74 74 65 72 20 6d 61  r is a better ma
29110 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74  tch than a funct
29120 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ion implementati
29130 6f 6e 20 77 69 74 68 0a 2a 2a 20 61 20 6e 65 67  on with.** a neg
29140 61 74 69 76 65 20 6e 41 72 67 2e 20 20 41 20 66  ative nArg.  A f
29150 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20 74 68  unction where th
29160 65 20 70 72 65 66 65 72 72 65 64 20 74 65 78 74  e preferred text
29170 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 6d 61 74   encoding.** mat
29180 63 68 65 73 20 74 68 65 20 64 61 74 61 62 61 73  ches the databas
29190 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 20  e encoding is a 
291a0 62 65 74 74 65 72 0a 2a 2a 20 6d 61 74 63 68 20  better.** match 
291b0 74 68 61 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20  than a function 
291c0 77 68 65 72 65 20 74 68 65 20 65 6e 63 6f 64 69  where the encodi
291d0 6e 67 20 69 73 20 64 69 66 66 65 72 65 6e 74 2e  ng is different.
291e0 20 20 0a 2a 2a 20 41 20 66 75 6e 63 74 69 6f 6e    .** A function
291f0 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 6f 64   where the encod
29200 69 6e 67 20 64 69 66 66 65 72 65 6e 63 65 20 69  ing difference i
29210 73 20 62 65 74 77 65 65 6e 20 55 54 46 31 36 6c  s between UTF16l
29220 65 20 61 6e 64 20 55 54 46 31 36 62 65 0a 2a 2a  e and UTF16be.**
29230 20 69 73 20 61 20 63 6c 6f 73 65 72 20 6d 61 74   is a closer mat
29240 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69  ch than a functi
29250 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63  on where the enc
29260 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e 63 65  oding difference
29270 20 69 73 0a 2a 2a 20 62 65 74 77 65 65 6e 20 55   is.** between U
29280 54 46 38 20 61 6e 64 20 55 54 46 31 36 2e 0a 2a  TF8 and UTF16..*
29290 2a 0a 2a 2a 20 42 75 69 6c 74 2d 69 6e 20 66 75  *.** Built-in fu
292a0 6e 63 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 6f  nctions may be o
292b0 76 65 72 6c 6f 61 64 65 64 20 62 79 20 6e 65 77  verloaded by new
292c0 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66   application-def
292d0 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  ined functions..
292e0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 70 70  ** The first app
292f0 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
29300 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61   function with a
29310 20 67 69 76 65 6e 20 6e 61 6d 65 20 6f 76 65 72   given name over
29320 72 69 64 65 73 20 61 6c 6c 0a 2a 2a 20 62 75 69  rides all.** bui
29330 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20  lt-in functions 
29340 69 6e 20 74 68 65 20 73 61 6d 65 20 5b 64 61 74  in the same [dat
29350 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
29360 5d 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20  ] with the same 
29370 6e 61 6d 65 2e 0a 2a 2a 20 53 75 62 73 65 71 75  name..** Subsequ
29380 65 6e 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  ent application-
29390 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
293a0 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e 61  s of the same na
293b0 6d 65 20 6f 6e 6c 79 20 6f 76 65 72 72 69 64 65  me only override
293c0 20 0a 2a 2a 20 70 72 69 6f 72 20 61 70 70 6c 69   .** prior appli
293d0 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66  cation-defined f
293e0 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 61 72  unctions that ar
293f0 65 20 61 6e 20 65 78 61 63 74 20 6d 61 74 63 68  e an exact match
29400 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6e 75 6d 62   for the.** numb
29410 65 72 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73  er of parameters
29420 20 61 6e 64 20 70 72 65 66 65 72 72 65 64 20 65   and preferred e
29430 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 41  ncoding..**.** A
29440 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  n application-de
29450 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 69  fined function i
29460 73 20 70 65 72 6d 69 74 74 65 64 20 74 6f 20 63  s permitted to c
29470 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 53 51 4c  all other.** SQL
29480 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 2e 20  ite interfaces. 
29490 20 48 6f 77 65 76 65 72 2c 20 73 75 63 68 20 63   However, such c
294a0 61 6c 6c 73 20 6d 75 73 74 20 6e 6f 74 0a 2a 2a  alls must not.**
294b0 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74 61 62   close the datab
294c0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6e  ase connection n
294d0 6f 72 20 66 69 6e 61 6c 69 7a 65 20 6f 72 20 72  or finalize or r
294e0 65 73 65 74 20 74 68 65 20 70 72 65 70 61 72 65  eset the prepare
294f0 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69  d.** statement i
29500 6e 20 77 68 69 63 68 20 74 68 65 20 66 75 6e 63  n which the func
29510 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e  tion is running.
29520 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
29530 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 31 30 33 5d  nts:.** [H16103]
29540 20 5b 48 31 36 31 30 36 5d 20 5b 48 31 36 31 30   [H16106] [H1610
29550 39 5d 20 5b 48 31 36 31 31 32 5d 20 5b 48 31 36  9] [H16112] [H16
29560 31 31 38 5d 20 5b 48 31 36 31 32 31 5d 20 5b 48  118] [H16121] [H
29570 31 36 31 32 37 5d 0a 2a 2a 20 5b 48 31 36 31 33  16127].** [H1613
29580 30 5d 20 5b 48 31 36 31 33 33 5d 20 5b 48 31 36  0] [H16133] [H16
29590 31 33 36 5d 20 5b 48 31 36 31 33 39 5d 20 5b 48  136] [H16139] [H
295a0 31 36 31 34 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45  16142].*/.SQLITE
295b0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
295c0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
295d0 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
295e0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
295f0 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20  FunctionName,.  
29600 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20  int nArg,.  int 
29610 65 54 65 78 74 52 65 70 2c 0a 20 20 76 6f 69 64  eTextRep,.  void
29620 20 2a 70 41 70 70 2c 0a 20 20 76 6f 69 64 20 28   *pApp,.  void (
29630 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f  *xFunc)(sqlite3_
29640 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
29650 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20  ite3_value**),. 
29660 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73   void (*xStep)(s
29670 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
29680 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
29690 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78  e**),.  void (*x
296a0 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63  Final)(sqlite3_c
296b0 6f 6e 74 65 78 74 2a 29 0a 29 3b 0a 53 51 4c 49  ontext*).);.SQLI
296c0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
296d0 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
296e0 6f 6e 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20  on16(.  sqlite3 
296f0 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69  *db,.  const voi
29700 64 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65  d *zFunctionName
29710 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20  ,.  int nArg,.  
29720 69 6e 74 20 65 54 65 78 74 52 65 70 2c 0a 20 20  int eTextRep,.  
29730 76 6f 69 64 20 2a 70 41 70 70 2c 0a 20 20 76 6f  void *pApp,.  vo
29740 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69  id (*xFunc)(sqli
29750 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
29760 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a  ,sqlite3_value**
29770 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65  ),.  void (*xSte
29780 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  p)(sqlite3_conte
29790 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
297a0 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64  value**),.  void
297b0 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74   (*xFinal)(sqlit
297c0 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 3b 0a  e3_context*).);.
297d0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
297e0 20 54 65 78 74 20 45 6e 63 6f 64 69 6e 67 73 20   Text Encodings 
297f0 7b 48 31 30 32 36 37 7d 20 3c 53 35 30 32 30 30  {H10267} <S50200
29800 3e 20 3c 48 31 36 31 30 30 3e 0a 2a 2a 0a 2a 2a  > <H16100>.**.**
29810 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 20   These constant 
29820 64 65 66 69 6e 65 20 69 6e 74 65 67 65 72 20 63  define integer c
29830 6f 64 65 73 20 74 68 61 74 20 72 65 70 72 65 73  odes that repres
29840 65 6e 74 20 74 68 65 20 76 61 72 69 6f 75 73 0a  ent the various.
29850 2a 2a 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  ** text encoding
29860 73 20 73 75 70 70 6f 72 74 65 64 20 62 79 20 53  s supported by S
29870 51 4c 69 74 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e  QLite..*/.#defin
29880 65 20 53 51 4c 49 54 45 5f 55 54 46 38 20 20 20  e SQLITE_UTF8   
29890 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e          1.#defin
298a0 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  e SQLITE_UTF16LE
298b0 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e          2.#defin
298c0 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  e SQLITE_UTF16BE
298d0 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e          3.#defin
298e0 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20 20  e SQLITE_UTF16  
298f0 20 20 20 20 20 20 20 20 34 20 20 20 20 2f 2a 20          4    /* 
29900 55 73 65 20 6e 61 74 69 76 65 20 62 79 74 65 20  Use native byte 
29910 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65  order */.#define
29920 20 53 51 4c 49 54 45 5f 41 4e 59 20 20 20 20 20   SQLITE_ANY     
29930 20 20 20 20 20 20 20 35 20 20 20 20 2f 2a 20 73         5    /* s
29940 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
29950 6e 63 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f 0a 23  nction only */.#
29960 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54  define SQLITE_UT
29970 46 31 36 5f 41 4c 49 47 4e 45 44 20 20 38 20 20  F16_ALIGNED  8  
29980 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63 72 65    /* sqlite3_cre
29990 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 20 6f 6e  ate_collation on
299a0 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  ly */../*.** CAP
299b0 49 33 52 45 46 3a 20 44 65 70 72 65 63 61 74 65  I3REF: Deprecate
299c0 64 20 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 44  d Functions.** D
299d0 45 50 52 45 43 41 54 45 44 0a 2a 2a 0a 2a 2a 20  EPRECATED.**.** 
299e0 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20  These functions 
299f0 61 72 65 20 5b 64 65 70 72 65 63 61 74 65 64 5d  are [deprecated]
29a00 2e 20 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 6d  .  In order to m
29a10 61 69 6e 74 61 69 6e 0a 2a 2a 20 62 61 63 6b 77  aintain.** backw
29a20 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69  ards compatibili
29a30 74 79 20 77 69 74 68 20 6f 6c 64 65 72 20 63 6f  ty with older co
29a40 64 65 2c 20 74 68 65 73 65 20 66 75 6e 63 74 69  de, these functi
29a50 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 0a 2a 2a  ons continue .**
29a60 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74 65 64   to be supported
29a70 2e 20 20 48 6f 77 65 76 65 72 2c 20 6e 65 77 20  .  However, new 
29a80 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f  applications sho
29a90 75 6c 64 20 61 76 6f 69 64 0a 2a 2a 20 74 68 65  uld avoid.** the
29aa0 20 75 73 65 20 6f 66 20 74 68 65 73 65 20 66 75   use of these fu
29ab0 6e 63 74 69 6f 6e 73 2e 20 20 54 6f 20 68 65 6c  nctions.  To hel
29ac0 70 20 65 6e 63 6f 75 72 61 67 65 20 70 65 6f 70  p encourage peop
29ad0 6c 65 20 74 6f 20 61 76 6f 69 64 0a 2a 2a 20 75  le to avoid.** u
29ae0 73 69 6e 67 20 74 68 65 73 65 20 66 75 6e 63 74  sing these funct
29af0 69 6f 6e 73 2c 20 77 65 20 61 72 65 20 6e 6f 74  ions, we are not
29b00 20 67 6f 69 6e 67 20 74 6f 20 74 65 6c 6c 20 79   going to tell y
29b10 6f 75 20 77 68 61 74 20 74 68 65 79 20 64 6f 2e  ou what they do.
29b20 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
29b30 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54  TE_OMIT_DEPRECAT
29b40 45 44 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  ED.SQLITE_API SQ
29b50 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20  LITE_DEPRECATED 
29b60 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 67 67 72  int sqlite3_aggr
29b70 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 71 6c 69  egate_count(sqli
29b80 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53  te3_context*);.S
29b90 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45  QLITE_API SQLITE
29ba0 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e 74 20  _DEPRECATED int 
29bb0 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65 64 28  sqlite3_expired(
29bc0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a  sqlite3_stmt*);.
29bd0 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54  SQLITE_API SQLIT
29be0 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e 74  E_DEPRECATED int
29bf0 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73 66 65   sqlite3_transfe
29c00 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74  r_bindings(sqlit
29c10 65 33 5f 73 74 6d 74 2a 2c 20 73 71 6c 69 74 65  e3_stmt*, sqlite
29c20 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49 54 45  3_stmt*);.SQLITE
29c30 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52  _API SQLITE_DEPR
29c40 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74  ECATED int sqlit
29c50 65 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65  e3_global_recove
29c60 72 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f  r(void);.SQLITE_
29c70 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 45  API SQLITE_DEPRE
29c80 43 41 54 45 44 20 76 6f 69 64 20 73 71 6c 69 74  CATED void sqlit
29c90 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e 75  e3_thread_cleanu
29ca0 70 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f  p(void);.SQLITE_
29cb0 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 45  API SQLITE_DEPRE
29cc0 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74 65  CATED int sqlite
29cd0 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d 28 76  3_memory_alarm(v
29ce0 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c  oid(*)(void*,sql
29cf0 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74 29 2c  ite3_int64,int),
29d00 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 6e  void*,sqlite3_in
29d10 74 36 34 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a  t64);.#endif../*
29d20 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 62  .** CAPI3REF: Ob
29d30 74 61 69 6e 69 6e 67 20 53 51 4c 20 46 75 6e 63  taining SQL Func
29d40 74 69 6f 6e 20 50 61 72 61 6d 65 74 65 72 20 56  tion Parameter V
29d50 61 6c 75 65 73 20 7b 48 31 35 31 30 30 7d 20 3c  alues {H15100} <
29d60 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S20200>.**.** Th
29d70 65 20 43 2d 6c 61 6e 67 75 61 67 65 20 69 6d 70  e C-language imp
29d80 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 53  lementation of S
29d90 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64  QL functions and
29da0 20 61 67 67 72 65 67 61 74 65 73 20 75 73 65 73   aggregates uses
29db0 0a 2a 2a 20 74 68 69 73 20 73 65 74 20 6f 66 20  .** this set of 
29dc0 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e  interface routin
29dd0 65 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 65  es to access the
29de0 20 70 61 72 61 6d 65 74 65 72 20 76 61 6c 75 65   parameter value
29df0 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 66 75 6e 63  s on.** the func
29e00 74 69 6f 6e 20 6f 72 20 61 67 67 72 65 67 61 74  tion or aggregat
29e10 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 46 75  e..**.** The xFu
29e20 6e 63 20 28 66 6f 72 20 73 63 61 6c 61 72 20 66  nc (for scalar f
29e30 75 6e 63 74 69 6f 6e 73 29 20 6f 72 20 78 53 74  unctions) or xSt
29e40 65 70 20 28 66 6f 72 20 61 67 67 72 65 67 61 74  ep (for aggregat
29e50 65 73 29 20 70 61 72 61 6d 65 74 65 72 73 0a 2a  es) parameters.*
29e60 2a 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 72  * to [sqlite3_cr
29e70 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d  eate_function()]
29e80 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72   and [sqlite3_cr
29e90 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28  eate_function16(
29ea0 29 5d 0a 2a 2a 20 64 65 66 69 6e 65 20 63 61 6c  )].** define cal
29eb0 6c 62 61 63 6b 73 20 74 68 61 74 20 69 6d 70 6c  lbacks that impl
29ec0 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20 66 75  ement the SQL fu
29ed0 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72  nctions and aggr
29ee0 65 67 61 74 65 73 2e 0a 2a 2a 20 54 68 65 20 34  egates..** The 4
29ef0 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  th parameter to 
29f00 74 68 65 73 65 20 63 61 6c 6c 62 61 63 6b 73 20  these callbacks 
29f10 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70  is an array of p
29f20 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 5b 70  ointers to.** [p
29f30 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33  rotected sqlite3
29f40 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73 2e  _value] objects.
29f50 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 5b    There is one [
29f60 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f  sqlite3_value] o
29f70 62 6a 65 63 74 20 66 6f 72 0a 2a 2a 20 65 61 63  bject for.** eac
29f80 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  h parameter to t
29f90 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e  he SQL function.
29fa0 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73    These routines
29fb0 20 61 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20   are used to.** 
29fc0 65 78 74 72 61 63 74 20 76 61 6c 75 65 73 20 66  extract values f
29fd0 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74 65 33  rom the [sqlite3
29fe0 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73 2e  _value] objects.
29ff0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
2a000 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6e 6c 79 20  tines work only 
2a010 77 69 74 68 20 5b 70 72 6f 74 65 63 74 65 64 20  with [protected 
2a020 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f  sqlite3_value] o
2a030 62 6a 65 63 74 73 2e 0a 2a 2a 20 41 6e 79 20 61  bjects..** Any a
2a040 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68  ttempt to use th
2a050 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f 6e 20  ese routines on 
2a060 61 6e 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20  an [unprotected 
2a070 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a 2a  sqlite3_value].*
2a080 2a 20 6f 62 6a 65 63 74 20 72 65 73 75 6c 74 73  * object results
2a090 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 62 65   in undefined be
2a0a0 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  havior..**.** Th
2a0b0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 77 6f 72  ese routines wor
2a0c0 6b 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20  k just like the 
2a0d0 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 5b 63  corresponding [c
2a0e0 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75 6e  olumn access fun
2a0f0 63 74 69 6f 6e 73 5d 0a 2a 2a 20 65 78 63 65 70  ctions].** excep
2a100 74 20 74 68 61 74 20 20 74 68 65 73 65 20 72 6f  t that  these ro
2a110 75 74 69 6e 65 73 20 74 61 6b 65 20 61 20 73 69  utines take a si
2a120 6e 67 6c 65 20 5b 70 72 6f 74 65 63 74 65 64 20  ngle [protected 
2a130 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f  sqlite3_value] o
2a140 62 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e 74 65 72  bject.** pointer
2a150 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 5b 73   instead of a [s
2a160 71 6c 69 74 65 33 5f 73 74 6d 74 2a 5d 20 70 6f  qlite3_stmt*] po
2a170 69 6e 74 65 72 20 61 6e 64 20 61 6e 20 69 6e 74  inter and an int
2a180 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62  eger column numb
2a190 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  er..**.** The sq
2a1a0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
2a1b0 31 36 28 29 20 69 6e 74 65 72 66 61 63 65 20 65  16() interface e
2a1c0 78 74 72 61 63 74 73 20 61 20 55 54 46 2d 31 36  xtracts a UTF-16
2a1d0 20 73 74 72 69 6e 67 0a 2a 2a 20 69 6e 20 74 68   string.** in th
2a1e0 65 20 6e 61 74 69 76 65 20 62 79 74 65 2d 6f 72  e native byte-or
2a1f0 64 65 72 20 6f 66 20 74 68 65 20 68 6f 73 74 20  der of the host 
2a200 6d 61 63 68 69 6e 65 2e 20 20 54 68 65 0a 2a 2a  machine.  The.**
2a210 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
2a220 65 78 74 31 36 62 65 28 29 20 61 6e 64 20 73 71  ext16be() and sq
2a230 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
2a240 31 36 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65  16le() interface
2a250 73 0a 2a 2a 20 65 78 74 72 61 63 74 20 55 54 46  s.** extract UTF
2a260 2d 31 36 20 73 74 72 69 6e 67 73 20 61 73 20 62  -16 strings as b
2a270 69 67 2d 65 6e 64 69 61 6e 20 61 6e 64 20 6c 69  ig-endian and li
2a280 74 74 6c 65 2d 65 6e 64 69 61 6e 20 72 65 73 70  ttle-endian resp
2a290 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20  ectively..**.** 
2a2a0 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  The sqlite3_valu
2a2b0 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 29  e_numeric_type()
2a2c0 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d   interface attem
2a2d0 70 74 73 20 74 6f 20 61 70 70 6c 79 0a 2a 2a 20  pts to apply.** 
2a2e0 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69 74 79  numeric affinity
2a2f0 20 74 6f 20 74 68 65 20 76 61 6c 75 65 2e 20 20   to the value.  
2a300 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20  This means that 
2a310 61 6e 20 61 74 74 65 6d 70 74 20 69 73 0a 2a 2a  an attempt is.**
2a320 20 6d 61 64 65 20 74 6f 20 63 6f 6e 76 65 72 74   made to convert
2a330 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 61 6e   the value to an
2a340 20 69 6e 74 65 67 65 72 20 6f 72 20 66 6c 6f 61   integer or floa
2a350 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20 49 66 0a  ting point.  If.
2a360 2a 2a 20 73 75 63 68 20 61 20 63 6f 6e 76 65 72  ** such a conver
2a370 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c 65  sion is possible
2a380 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66   without loss of
2a390 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 28 69 6e   information (in
2a3a0 20 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73 2c   other.** words,
2a3b0 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 73   if the value is
2a3c0 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 6c   a string that l
2a3d0 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75 6d 62  ooks like a numb
2a3e0 65 72 29 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20  er).** then the 
2a3f0 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 65  conversion is pe
2a400 72 66 6f 72 6d 65 64 2e 20 20 4f 74 68 65 72 77  rformed.  Otherw
2a410 69 73 65 20 6e 6f 20 63 6f 6e 76 65 72 73 69 6f  ise no conversio
2a420 6e 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 54 68 65  n occurs..** The
2a430 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52   [SQLITE_INTEGER
2a440 20 7c 20 64 61 74 61 74 79 70 65 5d 20 61 66 74   | datatype] aft
2a450 65 72 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73  er conversion is
2a460 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
2a470 20 50 6c 65 61 73 65 20 70 61 79 20 70 61 72 74   Please pay part
2a480 69 63 75 6c 61 72 20 61 74 74 65 6e 74 69 6f 6e  icular attention
2a490 20 74 6f 20 74 68 65 20 66 61 63 74 20 74 68 61   to the fact tha
2a4a0 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 72 65  t the pointer re
2a4b0 74 75 72 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 5b  turned.** from [
2a4c0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
2a4d0 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f  ob()], [sqlite3_
2a4e0 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c 20 6f  value_text()], o
2a4f0 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 61  r.** [sqlite3_va
2a500 6c 75 65 5f 74 65 78 74 31 36 28 29 5d 20 63 61  lue_text16()] ca
2a510 6e 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65 64  n be invalidated
2a520 20 62 79 20 61 20 73 75 62 73 65 71 75 65 6e 74   by a subsequent
2a530 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c   call to.** [sql
2a540 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
2a550 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61  ()], [sqlite3_va
2a560 6c 75 65 5f 62 79 74 65 73 31 36 28 29 5d 2c 20  lue_bytes16()], 
2a570 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  [sqlite3_value_t
2a580 65 78 74 28 29 5d 2c 0a 2a 2a 20 6f 72 20 5b 73  ext()],.** or [s
2a590 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
2a5a0 74 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  t16()]..**.** Th
2a5b0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 75 73  ese routines mus
2a5c0 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d  t be called from
2a5d0 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
2a5e0 20 61 73 0a 2a 2a 20 74 68 65 20 53 51 4c 20 66   as.** the SQL f
2a5f0 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 73 75 70  unction that sup
2a600 70 6c 69 65 64 20 74 68 65 20 5b 73 71 6c 69 74  plied the [sqlit
2a610 65 33 5f 76 61 6c 75 65 2a 5d 20 70 61 72 61 6d  e3_value*] param
2a620 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  eters..**.** Req
2a630 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
2a640 31 35 31 30 33 5d 20 5b 48 31 35 31 30 36 5d 20  15103] [H15106] 
2a650 5b 48 31 35 31 30 39 5d 20 5b 48 31 35 31 31 32  [H15109] [H15112
2a660 5d 20 5b 48 31 35 31 31 35 5d 20 5b 48 31 35 31  ] [H15115] [H151
2a670 31 38 5d 20 5b 48 31 35 31 32 31 5d 20 5b 48 31  18] [H15121] [H1
2a680 35 31 32 34 5d 0a 2a 2a 20 5b 48 31 35 31 32 37  5124].** [H15127
2a690 5d 20 5b 48 31 35 31 33 30 5d 20 5b 48 31 35 31  ] [H15130] [H151
2a6a0 33 33 5d 20 5b 48 31 35 31 33 36 5d 0a 2a 2f 0a  33] [H15136].*/.
2a6b0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
2a6c0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76   void *sqlite3_v
2a6d0 61 6c 75 65 5f 62 6c 6f 62 28 73 71 6c 69 74 65  alue_blob(sqlite
2a6e0 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54  3_value*);.SQLIT
2a6f0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2a700 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 73 71  3_value_bytes(sq
2a710 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53  lite3_value*);.S
2a720 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2a730 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
2a740 73 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  s16(sqlite3_valu
2a750 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  e*);.SQLITE_API 
2a760 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 5f 76  double sqlite3_v
2a770 61 6c 75 65 5f 64 6f 75 62 6c 65 28 73 71 6c 69  alue_double(sqli
2a780 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c  te3_value*);.SQL
2a790 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2a7a0 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 73 71  te3_value_int(sq
2a7b0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53  lite3_value*);.S
2a7c0 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
2a7d0 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f  3_int64 sqlite3_
2a7e0 76 61 6c 75 65 5f 69 6e 74 36 34 28 73 71 6c 69  value_int64(sqli
2a7f0 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c  te3_value*);.SQL
2a800 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 75 6e  ITE_API const un
2a810 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71 6c  signed char *sql
2a820 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
2a830 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
2a840 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
2a850 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
2a860 76 61 6c 75 65 5f 74 65 78 74 31 36 28 73 71 6c  value_text16(sql
2a870 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51  ite3_value*);.SQ
2a880 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
2a890 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c  oid *sqlite3_val
2a8a0 75 65 5f 74 65 78 74 31 36 6c 65 28 73 71 6c 69  ue_text16le(sqli
2a8b0 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c  te3_value*);.SQL
2a8c0 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
2a8d0 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75  id *sqlite3_valu
2a8e0 65 5f 74 65 78 74 31 36 62 65 28 73 71 6c 69 74  e_text16be(sqlit
2a8f0 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49  e3_value*);.SQLI
2a900 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2a910 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 73 71  e3_value_type(sq
2a920 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53  lite3_value*);.S
2a930 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2a940 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65  lite3_value_nume
2a950 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74 65 33  ric_type(sqlite3
2a960 5f 76 61 6c 75 65 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  _value*);../*.**
2a970 20 43 41 50 49 33 52 45 46 3a 20 4f 62 74 61 69   CAPI3REF: Obtai
2a980 6e 20 41 67 67 72 65 67 61 74 65 20 46 75 6e 63  n Aggregate Func
2a990 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20 7b 48 31  tion Context {H1
2a9a0 36 32 31 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a  6210} <S20200>.*
2a9b0 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65  *.** The impleme
2a9c0 6e 74 61 74 69 6f 6e 20 6f 66 20 61 67 67 72 65  ntation of aggre
2a9d0 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69 6f  gate SQL functio
2a9e0 6e 73 20 75 73 65 20 74 68 69 73 20 72 6f 75 74  ns use this rout
2a9f0 69 6e 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 0a  ine to allocate.
2aa00 2a 2a 20 61 20 73 74 72 75 63 74 75 72 65 20 66  ** a structure f
2aa10 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 69 72  or storing their
2aa20 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   state..**.** Th
2aa30 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 65  e first time the
2aa40 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61   sqlite3_aggrega
2aa50 74 65 5f 63 6f 6e 74 65 78 74 28 29 20 72 6f 75  te_context() rou
2aa60 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66  tine is called f
2aa70 6f 72 20 61 0a 2a 2a 20 70 61 72 74 69 63 75 6c  or a.** particul
2aa80 61 72 20 61 67 67 72 65 67 61 74 65 2c 20 53 51  ar aggregate, SQ
2aa90 4c 69 74 65 20 61 6c 6c 6f 63 61 74 65 73 20 6e  Lite allocates n
2aaa0 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2c  Bytes of memory,
2aab0 20 7a 65 72 6f 65 73 20 6f 75 74 20 74 68 61 74   zeroes out that
2aac0 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 61 6e 64 20  .** memory, and 
2aad0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
2aae0 72 20 74 6f 20 69 74 2e 20 4f 6e 20 73 65 63 6f  r to it. On seco
2aaf0 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e  nd and subsequen
2ab00 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71  t calls to.** sq
2ab10 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f  lite3_aggregate_
2ab20 63 6f 6e 74 65 78 74 28 29 20 66 6f 72 20 74 68  context() for th
2ab30 65 20 73 61 6d 65 20 61 67 67 72 65 67 61 74 65  e same aggregate
2ab40 20 66 75 6e 63 74 69 6f 6e 20 69 6e 64 65 78 2c   function index,
2ab50 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 62 75 66  .** the same buf
2ab60 66 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e  fer is returned.
2ab70 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   The implementat
2ab80 69 6f 6e 20 6f 66 20 74 68 65 20 61 67 67 72 65  ion of the aggre
2ab90 67 61 74 65 20 63 61 6e 20 75 73 65 0a 2a 2a 20  gate can use.** 
2aba0 74 68 65 20 72 65 74 75 72 6e 65 64 20 62 75 66  the returned buf
2abb0 66 65 72 20 74 6f 20 61 63 63 75 6d 75 6c 61 74  fer to accumulat
2abc0 65 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 53 51  e data..**.** SQ
2abd0 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 63 61 6c  Lite automatical
2abe0 6c 79 20 66 72 65 65 73 20 74 68 65 20 61 6c 6c  ly frees the all
2abf0 6f 63 61 74 65 64 20 62 75 66 66 65 72 20 77 68  ocated buffer wh
2ac00 65 6e 20 74 68 65 20 61 67 67 72 65 67 61 74 65  en the aggregate
2ac10 0a 2a 2a 20 71 75 65 72 79 20 63 6f 6e 63 6c 75  .** query conclu
2ac20 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  des..**.** The f
2ac30 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 73  irst parameter s
2ac40 68 6f 75 6c 64 20 62 65 20 61 20 63 6f 70 79 20  hould be a copy 
2ac50 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74  of the.** [sqlit
2ac60 65 33 5f 63 6f 6e 74 65 78 74 20 7c 20 53 51 4c  e3_context | SQL
2ac70 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78   function contex
2ac80 74 5d 20 74 68 61 74 20 69 73 20 74 68 65 20 66  t] that is the f
2ac90 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 0a 2a  irst parameter.*
2aca0 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63  * to the callbac
2acb0 6b 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 69  k routine that i
2acc0 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 61 67  mplements the ag
2acd0 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
2ace0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
2acf0 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c  tine must be cal
2ad00 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d  led from the sam
2ad10 65 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 63  e thread in whic
2ad20 68 0a 2a 2a 20 74 68 65 20 61 67 67 72 65 67 61  h.** the aggrega
2ad30 74 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  te SQL function 
2ad40 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a  is running..**.*
2ad50 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
2ad60 2a 2a 20 5b 48 31 36 32 31 31 5d 20 5b 48 31 36  ** [H16211] [H16
2ad70 32 31 33 5d 20 5b 48 31 36 32 31 35 5d 20 5b 48  213] [H16215] [H
2ad80 31 36 32 31 37 5d 0a 2a 2f 0a 53 51 4c 49 54 45  16217].*/.SQLITE
2ad90 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
2ada0 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
2adb0 74 65 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e  text(sqlite3_con
2adc0 74 65 78 74 2a 2c 20 69 6e 74 20 6e 42 79 74 65  text*, int nByte
2add0 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  s);../*.** CAPI3
2ade0 52 45 46 3a 20 55 73 65 72 20 44 61 74 61 20 46  REF: User Data F
2adf0 6f 72 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31  or Functions {H1
2ae00 36 32 34 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a  6240} <S20200>.*
2ae10 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
2ae20 5f 75 73 65 72 5f 64 61 74 61 28 29 20 69 6e 74  _user_data() int
2ae30 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61  erface returns a
2ae40 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20   copy of.** the 
2ae50 70 6f 69 6e 74 65 72 20 74 68 61 74 20 77 61 73  pointer that was
2ae60 20 74 68 65 20 70 55 73 65 72 44 61 74 61 20 70   the pUserData p
2ae70 61 72 61 6d 65 74 65 72 20 28 74 68 65 20 35 74  arameter (the 5t
2ae80 68 20 70 61 72 61 6d 65 74 65 72 29 0a 2a 2a 20  h parameter).** 
2ae90 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  of the [sqlite3_
2aea0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
2aeb0 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 74  )].** and [sqlit
2aec0 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
2aed0 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69 6e 65 73  on16()] routines
2aee0 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c 6c 79   that originally
2aef0 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 20 74  .** registered t
2af00 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64  he application d
2af10 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 2e  efined function.
2af20 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 69   {END}.**.** Thi
2af30 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62  s routine must b
2af40 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68  e called from th
2af50 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e  e same thread in
2af60 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 61 70   which.** the ap
2af70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
2af80 64 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75  d function is ru
2af90 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  nning..**.** Req
2afa0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
2afb0 31 36 32 34 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45  16243].*/.SQLITE
2afc0 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
2afd0 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71 6c  e3_user_data(sql
2afe0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a  ite3_context*);.
2aff0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
2b000 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63   Database Connec
2b010 74 69 6f 6e 20 46 6f 72 20 46 75 6e 63 74 69 6f  tion For Functio
2b020 6e 73 20 7b 48 31 36 32 35 30 7d 20 3c 53 36 30  ns {H16250} <S60
2b030 36 30 30 3e 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a  600><S20200>.**.
2b040 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63  ** The sqlite3_c
2b050 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
2b060 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  () interface ret
2b070 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a  urns a copy of.*
2b080 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f  * the pointer to
2b090 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
2b0a0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 68 65 20  onnection] (the 
2b0b0 31 73 74 20 70 61 72 61 6d 65 74 65 72 29 0a 2a  1st parameter).*
2b0c0 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65  * of the [sqlite
2b0d0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
2b0e0 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c  n()].** and [sql
2b0f0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
2b100 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69 6e  tion16()] routin
2b110 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c  es that original
2b120 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64  ly.** registered
2b130 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
2b140 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f   defined functio
2b150 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  n..**.** Require
2b160 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 32 35  ments:.** [H1625
2b170 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  3].*/.SQLITE_API
2b180 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65   sqlite3 *sqlite
2b190 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
2b1a0 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  dle(sqlite3_cont
2b1b0 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  ext*);../*.** CA
2b1c0 50 49 33 52 45 46 3a 20 46 75 6e 63 74 69 6f 6e  PI3REF: Function
2b1d0 20 41 75 78 69 6c 69 61 72 79 20 44 61 74 61 20   Auxiliary Data 
2b1e0 7b 48 31 36 32 37 30 7d 20 3c 53 32 30 32 30 30  {H16270} <S20200
2b1f0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  >.**.** The foll
2b200 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e 63 74 69  owing two functi
2b210 6f 6e 73 20 6d 61 79 20 62 65 20 75 73 65 64 20  ons may be used 
2b220 62 79 20 73 63 61 6c 61 72 20 53 51 4c 20 66 75  by scalar SQL fu
2b230 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 61 73  nctions to.** as
2b240 73 6f 63 69 61 74 65 20 6d 65 74 61 64 61 74 61  sociate metadata
2b250 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74 20 76   with argument v
2b260 61 6c 75 65 73 2e 20 49 66 20 74 68 65 20 73 61  alues. If the sa
2b270 6d 65 20 76 61 6c 75 65 20 69 73 20 70 61 73 73  me value is pass
2b280 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74 69 70 6c  ed to.** multipl
2b290 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f 66  e invocations of
2b2a0 20 74 68 65 20 73 61 6d 65 20 53 51 4c 20 66 75   the same SQL fu
2b2b0 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67 20 71 75  nction during qu
2b2c0 65 72 79 20 65 78 65 63 75 74 69 6f 6e 2c 20 75  ery execution, u
2b2d0 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20 63 69 72  nder.** some cir
2b2e0 63 75 6d 73 74 61 6e 63 65 73 20 74 68 65 20 61  cumstances the a
2b2f0 73 73 6f 63 69 61 74 65 64 20 6d 65 74 61 64 61  ssociated metada
2b300 74 61 20 6d 61 79 20 62 65 20 70 72 65 73 65 72  ta may be preser
2b310 76 65 64 2e 20 54 68 69 73 20 6d 61 79 0a 2a 2a  ved. This may.**
2b320 20 62 65 20 75 73 65 64 2c 20 66 6f 72 20 65 78   be used, for ex
2b330 61 6d 70 6c 65 2c 20 74 6f 20 61 64 64 20 61 20  ample, to add a 
2b340 72 65 67 75 6c 61 72 2d 65 78 70 72 65 73 73 69  regular-expressi
2b350 6f 6e 20 6d 61 74 63 68 69 6e 67 20 73 63 61 6c  on matching scal
2b360 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20  ar.** function. 
2b370 54 68 65 20 63 6f 6d 70 69 6c 65 64 20 76 65 72  The compiled ver
2b380 73 69 6f 6e 20 6f 66 20 74 68 65 20 72 65 67 75  sion of the regu
2b390 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69  lar expression i
2b3a0 73 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 6d  s stored as.** m
2b3b0 65 74 61 64 61 74 61 20 61 73 73 6f 63 69 61 74  etadata associat
2b3c0 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 20  ed with the SQL 
2b3d0 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20  value passed as 
2b3e0 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 70 72  the regular expr
2b3f0 65 73 73 69 6f 6e 0a 2a 2a 20 70 61 74 74 65 72  ession.** patter
2b400 6e 2e 20 20 54 68 65 20 63 6f 6d 70 69 6c 65 64  n.  The compiled
2b410 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 73 73   regular express
2b420 69 6f 6e 20 63 61 6e 20 62 65 20 72 65 75 73 65  ion can be reuse
2b430 64 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a  d on multiple.**
2b440 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f 66 20   invocations of 
2b450 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f  the same functio
2b460 6e 20 73 6f 20 74 68 61 74 20 74 68 65 20 6f 72  n so that the or
2b470 69 67 69 6e 61 6c 20 70 61 74 74 65 72 6e 20 73  iginal pattern s
2b480 74 72 69 6e 67 0a 2a 2a 20 64 6f 65 73 20 6e 6f  tring.** does no
2b490 74 20 6e 65 65 64 20 74 6f 20 62 65 20 72 65 63  t need to be rec
2b4a0 6f 6d 70 69 6c 65 64 20 6f 6e 20 65 61 63 68 20  ompiled on each 
2b4b0 69 6e 76 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  invocation..**.*
2b4c0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65  * The sqlite3_ge
2b4d0 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e 74 65  t_auxdata() inte
2b4e0 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20  rface returns a 
2b4f0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d  pointer to the m
2b500 65 74 61 64 61 74 61 0a 2a 2a 20 61 73 73 6f 63  etadata.** assoc
2b510 69 61 74 65 64 20 62 79 20 74 68 65 20 73 71 6c  iated by the sql
2b520 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61  ite3_set_auxdata
2b530 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68  () function with
2b540 20 74 68 65 20 4e 74 68 20 61 72 67 75 6d 65 6e   the Nth argumen
2b550 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f 20 74 68  t.** value to th
2b560 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  e application-de
2b570 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 2e 20  fined function. 
2b580 49 66 20 6e 6f 20 6d 65 74 61 64 61 74 61 20 68  If no metadata h
2b590 61 73 20 62 65 65 6e 20 65 76 65 72 0a 2a 2a 20  as been ever.** 
2b5a0 62 65 65 6e 20 73 65 74 20 66 6f 72 20 74 68 65  been set for the
2b5b0 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 20 6f 66   Nth argument of
2b5c0 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2c 20 6f   the function, o
2b5d0 72 20 69 66 20 74 68 65 20 63 6f 72 72 65 73 70  r if the corresp
2b5e0 6f 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69  onding.** functi
2b5f0 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 68 61 73  on parameter has
2b600 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65 20 74   changed since t
2b610 68 65 20 6d 65 74 61 2d 64 61 74 61 20 77 61 73  he meta-data was
2b620 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20 73 71   set,.** then sq
2b630 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74  lite3_get_auxdat
2b640 61 28 29 20 72 65 74 75 72 6e 73 20 61 20 4e 55  a() returns a NU
2b650 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a  LL pointer..**.*
2b660 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 65  * The sqlite3_se
2b670 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e 74 65  t_auxdata() inte
2b680 72 66 61 63 65 20 73 61 76 65 73 20 74 68 65 20  rface saves the 
2b690 6d 65 74 61 64 61 74 61 0a 2a 2a 20 70 6f 69 6e  metadata.** poin
2b6a0 74 65 64 20 74 6f 20 62 79 20 69 74 73 20 33 72  ted to by its 3r
2b6b0 64 20 70 61 72 61 6d 65 74 65 72 20 61 73 20 74  d parameter as t
2b6c0 68 65 20 6d 65 74 61 64 61 74 61 20 66 6f 72 20  he metadata for 
2b6d0 74 68 65 20 4e 2d 74 68 0a 2a 2a 20 61 72 67 75  the N-th.** argu
2b6e0 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 70 70 6c  ment of the appl
2b6f0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
2b700 66 75 6e 63 74 69 6f 6e 2e 20 20 53 75 62 73 65  function.  Subse
2b710 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 74  quent.** calls t
2b720 6f 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75  o sqlite3_get_au
2b730 78 64 61 74 61 28 29 20 6d 69 67 68 74 20 72 65  xdata() might re
2b740 74 75 72 6e 20 74 68 69 73 20 64 61 74 61 2c 20  turn this data, 
2b750 69 66 20 69 74 20 68 61 73 0a 2a 2a 20 6e 6f 74  if it has.** not
2b760 20 62 65 65 6e 20 64 65 73 74 72 6f 79 65 64 2e   been destroyed.
2b770 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e 6f 74  .** If it is not
2b780 20 4e 55 4c 4c 2c 20 53 51 4c 69 74 65 20 77 69   NULL, SQLite wi
2b790 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 64 65  ll invoke the de
2b7a0 73 74 72 75 63 74 6f 72 0a 2a 2a 20 66 75 6e 63  structor.** func
2b7b0 74 69 6f 6e 20 67 69 76 65 6e 20 62 79 20 74 68  tion given by th
2b7c0 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20  e 4th parameter 
2b7d0 74 6f 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  to sqlite3_set_a
2b7e0 75 78 64 61 74 61 28 29 20 6f 6e 0a 2a 2a 20 74  uxdata() on.** t
2b7f0 68 65 20 6d 65 74 61 64 61 74 61 20 77 68 65 6e  he metadata when
2b800 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
2b810 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61  ng function para
2b820 6d 65 74 65 72 20 63 68 61 6e 67 65 73 0a 2a 2a  meter changes.**
2b830 20 6f 72 20 77 68 65 6e 20 74 68 65 20 53 51 4c   or when the SQL
2b840 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 6c   statement compl
2b850 65 74 65 73 2c 20 77 68 69 63 68 65 76 65 72 20  etes, whichever 
2b860 63 6f 6d 65 73 20 66 69 72 73 74 2e 0a 2a 2a 0a  comes first..**.
2b870 2a 2a 20 53 51 4c 69 74 65 20 69 73 20 66 72 65  ** SQLite is fre
2b880 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 64 65  e to call the de
2b890 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64 72 6f  structor and dro
2b8a0 70 20 6d 65 74 61 64 61 74 61 20 6f 6e 20 61 6e  p metadata on an
2b8b0 79 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 6f  y.** parameter o
2b8c0 66 20 61 6e 79 20 66 75 6e 63 74 69 6f 6e 20 61  f any function a
2b8d0 74 20 61 6e 79 20 74 69 6d 65 2e 20 20 54 68 65  t any time.  The
2b8e0 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 65 65 20   only guarantee 
2b8f0 69 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 64  is that.** the d
2b900 65 73 74 72 75 63 74 6f 72 20 77 69 6c 6c 20 62  estructor will b
2b910 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20  e called before 
2b920 74 68 65 20 6d 65 74 61 64 61 74 61 20 69 73 20  the metadata is 
2b930 64 72 6f 70 70 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  dropped..**.** I
2b940 6e 20 70 72 61 63 74 69 63 65 2c 20 6d 65 74 61  n practice, meta
2b950 64 61 74 61 20 69 73 20 70 72 65 73 65 72 76 65  data is preserve
2b960 64 20 62 65 74 77 65 65 6e 20 66 75 6e 63 74 69  d between functi
2b970 6f 6e 20 63 61 6c 6c 73 20 66 6f 72 0a 2a 2a 20  on calls for.** 
2b980 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74  expressions that
2b990 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 61 74   are constant at
2b9a0 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e 20 54   compile time. T
2b9b0 68 69 73 20 69 6e 63 6c 75 64 65 73 20 6c 69 74  his includes lit
2b9c0 65 72 61 6c 0a 2a 2a 20 76 61 6c 75 65 73 20 61  eral.** values a
2b9d0 6e 64 20 53 51 4c 20 76 61 72 69 61 62 6c 65 73  nd SQL variables
2b9e0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  ..**.** These ro
2b9f0 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65 20 63  utines must be c
2ba00 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73  alled from the s
2ba10 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20 77 68  ame thread in wh
2ba20 69 63 68 0a 2a 2a 20 74 68 65 20 53 51 4c 20 66  ich.** the SQL f
2ba30 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69  unction is runni
2ba40 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ng..**.** Requir
2ba50 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 32  ements:.** [H162
2ba60 37 32 5d 20 5b 48 31 36 32 37 34 5d 20 5b 48 31  72] [H16274] [H1
2ba70 36 32 37 36 5d 20 5b 48 31 36 32 37 37 5d 20 5b  6276] [H16277] [
2ba80 48 31 36 32 37 38 5d 20 5b 48 31 36 32 37 39 5d  H16278] [H16279]
2ba90 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
2baa0 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 67 65 74  oid *sqlite3_get
2bab0 5f 61 75 78 64 61 74 61 28 73 71 6c 69 74 65 33  _auxdata(sqlite3
2bac0 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 4e  _context*, int N
2bad0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  );.SQLITE_API vo
2bae0 69 64 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  id sqlite3_set_a
2baf0 75 78 64 61 74 61 28 73 71 6c 69 74 65 33 5f 63  uxdata(sqlite3_c
2bb00 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 4e 2c 20  ontext*, int N, 
2bb10 76 6f 69 64 2a 2c 20 76 6f 69 64 20 28 2a 29 28  void*, void (*)(
2bb20 76 6f 69 64 2a 29 29 3b 0a 0a 0a 2f 2a 0a 2a 2a  void*));.../*.**
2bb30 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 73 74   CAPI3REF: Const
2bb40 61 6e 74 73 20 44 65 66 69 6e 69 6e 67 20 53 70  ants Defining Sp
2bb50 65 63 69 61 6c 20 44 65 73 74 72 75 63 74 6f 72  ecial Destructor
2bb60 20 42 65 68 61 76 69 6f 72 20 7b 48 31 30 32 38   Behavior {H1028
2bb70 30 7d 20 3c 53 33 30 31 30 30 3e 0a 2a 2a 0a 2a  0} <S30100>.**.*
2bb80 2a 20 54 68 65 73 65 20 61 72 65 20 73 70 65 63  * These are spec
2bb90 69 61 6c 20 76 61 6c 75 65 73 20 66 6f 72 20 74  ial values for t
2bba0 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 74 68  he destructor th
2bbb0 61 74 20 69 73 20 70 61 73 73 65 64 20 69 6e 20  at is passed in 
2bbc0 61 73 20 74 68 65 0a 2a 2a 20 66 69 6e 61 6c 20  as the.** final 
2bbd0 61 72 67 75 6d 65 6e 74 20 74 6f 20 72 6f 75 74  argument to rout
2bbe0 69 6e 65 73 20 6c 69 6b 65 20 5b 73 71 6c 69 74  ines like [sqlit
2bbf0 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 29  e3_result_blob()
2bc00 5d 2e 20 20 49 66 20 74 68 65 20 64 65 73 74 72  ].  If the destr
2bc10 75 63 74 6f 72 0a 2a 2a 20 61 72 67 75 6d 65 6e  uctor.** argumen
2bc20 74 20 69 73 20 53 51 4c 49 54 45 5f 53 54 41 54  t is SQLITE_STAT
2bc30 49 43 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  IC, it means tha
2bc40 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 70 6f  t the content po
2bc50 69 6e 74 65 72 20 69 73 20 63 6f 6e 73 74 61 6e  inter is constan
2bc60 74 0a 2a 2a 20 61 6e 64 20 77 69 6c 6c 20 6e 65  t.** and will ne
2bc70 76 65 72 20 63 68 61 6e 67 65 2e 20 20 49 74 20  ver change.  It 
2bc80 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f  does not need to
2bc90 20 62 65 20 64 65 73 74 72 6f 79 65 64 2e 20 20   be destroyed.  
2bca0 54 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 52  The.** SQLITE_TR
2bcb0 41 4e 53 49 45 4e 54 20 76 61 6c 75 65 20 6d 65  ANSIENT value me
2bcc0 61 6e 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e  ans that the con
2bcd0 74 65 6e 74 20 77 69 6c 6c 20 6c 69 6b 65 6c 79  tent will likely
2bce0 20 63 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 74 68   change in.** th
2bcf0 65 20 6e 65 61 72 20 66 75 74 75 72 65 20 61 6e  e near future an
2bd00 64 20 74 68 61 74 20 53 51 4c 69 74 65 20 73 68  d that SQLite sh
2bd10 6f 75 6c 64 20 6d 61 6b 65 20 69 74 73 20 6f 77  ould make its ow
2bd20 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f  n private copy o
2bd30 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74  f.** the content
2bd40 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
2bd50 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 79 70  g..**.** The typ
2bd60 65 64 65 66 20 69 73 20 6e 65 63 65 73 73 61 72  edef is necessar
2bd70 79 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64  y to work around
2bd80 20 70 72 6f 62 6c 65 6d 73 20 69 6e 20 63 65 72   problems in cer
2bd90 74 61 69 6e 0a 2a 2a 20 43 2b 2b 20 63 6f 6d 70  tain.** C++ comp
2bda0 69 6c 65 72 73 2e 20 20 53 65 65 20 74 69 63 6b  ilers.  See tick
2bdb0 65 74 20 23 32 31 39 31 2e 0a 2a 2f 0a 74 79 70  et #2191..*/.typ
2bdc0 65 64 65 66 20 76 6f 69 64 20 28 2a 73 71 6c 69  edef void (*sqli
2bdd0 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74  te3_destructor_t
2bde0 79 70 65 29 28 76 6f 69 64 2a 29 3b 0a 23 64 65  ype)(void*);.#de
2bdf0 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54  fine SQLITE_STAT
2be00 49 43 20 20 20 20 20 20 28 28 73 71 6c 69 74 65  IC      ((sqlite
2be10 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70  3_destructor_typ
2be20 65 29 30 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  e)0).#define SQL
2be30 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 20 20  ITE_TRANSIENT   
2be40 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75  ((sqlite3_destru
2be50 63 74 6f 72 5f 74 79 70 65 29 2d 31 29 0a 0a 2f  ctor_type)-1)../
2be60 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53  *.** CAPI3REF: S
2be70 65 74 74 69 6e 67 20 54 68 65 20 52 65 73 75 6c  etting The Resul
2be80 74 20 4f 66 20 41 6e 20 53 51 4c 20 46 75 6e 63  t Of An SQL Func
2be90 74 69 6f 6e 20 7b 48 31 36 34 30 30 7d 20 3c 53  tion {H16400} <S
2bea0 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  20200>.**.** The
2beb0 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  se routines are 
2bec0 75 73 65 64 20 62 79 20 74 68 65 20 78 46 75 6e  used by the xFun
2bed0 63 20 6f 72 20 78 46 69 6e 61 6c 20 63 61 6c 6c  c or xFinal call
2bee0 62 61 63 6b 73 20 74 68 61 74 0a 2a 2a 20 69 6d  backs that.** im
2bef0 70 6c 65 6d 65 6e 74 20 53 51 4c 20 66 75 6e 63  plement SQL func
2bf00 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72 65 67  tions and aggreg
2bf10 61 74 65 73 2e 20 20 53 65 65 0a 2a 2a 20 5b 73  ates.  See.** [s
2bf20 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
2bf30 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73  nction()] and [s
2bf40 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
2bf50 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a 20 66  nction16()].** f
2bf60 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
2bf70 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  formation..**.**
2bf80 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73   These functions
2bf90 20 77 6f 72 6b 20 76 65 72 79 20 6d 75 63 68 20   work very much 
2bfa0 6c 69 6b 65 20 74 68 65 20 5b 70 61 72 61 6d 65  like the [parame
2bfb0 74 65 72 20 62 69 6e 64 69 6e 67 5d 20 66 61 6d  ter binding] fam
2bfc0 69 6c 79 20 6f 66 0a 2a 2a 20 66 75 6e 63 74 69  ily of.** functi
2bfd0 6f 6e 73 20 75 73 65 64 20 74 6f 20 62 69 6e 64  ons used to bind
2bfe0 20 76 61 6c 75 65 73 20 74 6f 20 68 6f 73 74 20   values to host 
2bff0 70 61 72 61 6d 65 74 65 72 73 20 69 6e 20 70 72  parameters in pr
2c000 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
2c010 73 2e 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 74  s..** Refer to t
2c020 68 65 20 5b 53 51 4c 20 70 61 72 61 6d 65 74 65  he [SQL paramete
2c030 72 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e  r] documentation
2c040 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
2c050 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a  information..**.
2c060 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72  ** The sqlite3_r
2c070 65 73 75 6c 74 5f 62 6c 6f 62 28 29 20 69 6e 74  esult_blob() int
2c080 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65 20  erface sets the 
2c090 72 65 73 75 6c 74 20 66 72 6f 6d 0a 2a 2a 20 61  result from.** a
2c0a0 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  n application-de
2c0b0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74  fined function t
2c0c0 6f 20 62 65 20 74 68 65 20 42 4c 4f 42 20 77 68  o be the BLOB wh
2c0d0 6f 73 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 70  ose content is p
2c0e0 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20  ointed.** to by 
2c0f0 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  the second param
2c100 65 74 65 72 20 61 6e 64 20 77 68 69 63 68 20 69  eter and which i
2c110 73 20 4e 20 62 79 74 65 73 20 6c 6f 6e 67 20 77  s N bytes long w
2c120 68 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a  here N is the.**
2c130 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72   third parameter
2c140 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
2c150 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62  te3_result_zerob
2c160 6c 6f 62 28 29 20 69 6e 74 65 72 66 61 63 65 73  lob() interfaces
2c170 20 73 65 74 20 74 68 65 20 72 65 73 75 6c 74 20   set the result 
2c180 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63  of.** the applic
2c190 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75  ation-defined fu
2c1a0 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 20 42  nction to be a B
2c1b0 4c 4f 42 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61  LOB containing a
2c1c0 6c 6c 20 7a 65 72 6f 0a 2a 2a 20 62 79 74 65 73  ll zero.** bytes
2c1d0 20 61 6e 64 20 4e 20 62 79 74 65 73 20 69 6e 20   and N bytes in 
2c1e0 73 69 7a 65 2c 20 77 68 65 72 65 20 4e 20 69 73  size, where N is
2c1f0 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
2c200 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 2e  e 2nd parameter.
2c210 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
2c220 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65  e3_result_double
2c230 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74  () interface set
2c240 73 20 74 68 65 20 72 65 73 75 6c 74 20 66 72 6f  s the result fro
2c250 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63 61 74  m.** an applicat
2c260 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
2c270 74 69 6f 6e 20 74 6f 20 62 65 20 61 20 66 6c 6f  tion to be a flo
2c280 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75  ating point valu
2c290 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62  e specified.** b
2c2a0 79 20 69 74 73 20 32 6e 64 20 61 72 67 75 6d 65  y its 2nd argume
2c2b0 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  nt..**.** The sq
2c2c0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
2c2d0 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  or() and sqlite3
2c2e0 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28  _result_error16(
2c2f0 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 63  ) functions.** c
2c300 61 75 73 65 20 74 68 65 20 69 6d 70 6c 65 6d 65  ause the impleme
2c310 6e 74 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f  nted SQL functio
2c320 6e 20 74 6f 20 74 68 72 6f 77 20 61 6e 20 65 78  n to throw an ex
2c330 63 65 70 74 69 6f 6e 2e 0a 2a 2a 20 53 51 4c 69  ception..** SQLi
2c340 74 65 20 75 73 65 73 20 74 68 65 20 73 74 72 69  te uses the stri
2c350 6e 67 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ng pointed to by
2c360 20 74 68 65 0a 2a 2a 20 32 6e 64 20 70 61 72 61   the.** 2nd para
2c370 6d 65 74 65 72 20 6f 66 20 73 71 6c 69 74 65 33  meter of sqlite3
2c380 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20  _result_error() 
2c390 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  or sqlite3_resul
2c3a0 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a 2a 20 61  t_error16().** a
2c3b0 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 61 6e  s the text of an
2c3c0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 20   error message. 
2c3d0 20 53 51 4c 69 74 65 20 69 6e 74 65 72 70 72 65   SQLite interpre
2c3e0 74 73 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20  ts the error.** 
2c3f0 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 20 66  message string f
2c400 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72 65 73 75  rom sqlite3_resu
2c410 6c 74 5f 65 72 72 6f 72 28 29 20 61 73 20 55 54  lt_error() as UT
2c420 46 2d 38 2e 20 53 51 4c 69 74 65 0a 2a 2a 20 69  F-8. SQLite.** i
2c430 6e 74 65 72 70 72 65 74 73 20 74 68 65 20 73 74  nterprets the st
2c440 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69 74 65  ring from sqlite
2c450 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36  3_result_error16
2c460 28 29 20 61 73 20 55 54 46 2d 31 36 20 69 6e 20  () as UTF-16 in 
2c470 6e 61 74 69 76 65 0a 2a 2a 20 62 79 74 65 20 6f  native.** byte o
2c480 72 64 65 72 2e 20 20 49 66 20 74 68 65 20 74 68  rder.  If the th
2c490 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ird parameter to
2c4a0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2c4b0 65 72 72 6f 72 28 29 0a 2a 2a 20 6f 72 20 73 71  error().** or sq
2c4c0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
2c4d0 6f 72 31 36 28 29 20 69 73 20 6e 65 67 61 74 69  or16() is negati
2c4e0 76 65 20 74 68 65 6e 20 53 51 4c 69 74 65 20 74  ve then SQLite t
2c4f0 61 6b 65 73 20 61 73 20 74 68 65 20 65 72 72 6f  akes as the erro
2c500 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 61 6c 6c  r.** message all
2c510 20 74 65 78 74 20 75 70 20 74 68 72 6f 75 67 68   text up through
2c520 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20   the first zero 
2c530 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49 66  character..** If
2c540 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d   the third param
2c550 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f  eter to sqlite3_
2c560 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 6f  result_error() o
2c570 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73  r.** sqlite3_res
2c580 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 69 73  ult_error16() is
2c590 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 74 68   non-negative th
2c5a0 65 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 73 20  en SQLite takes 
2c5b0 74 68 61 74 20 6d 61 6e 79 0a 2a 2a 20 62 79 74  that many.** byt
2c5c0 65 73 20 28 6e 6f 74 20 63 68 61 72 61 63 74 65  es (not characte
2c5d0 72 73 29 20 66 72 6f 6d 20 74 68 65 20 32 6e 64  rs) from the 2nd
2c5e0 20 70 61 72 61 6d 65 74 65 72 20 61 73 20 74 68   parameter as th
2c5f0 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e  e error message.
2c600 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2c610 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 61  result_error() a
2c620 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  nd sqlite3_resul
2c630 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a 2a 20 72  t_error16().** r
2c640 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20 61 20 70  outines make a p
2c650 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66 20 74  rivate copy of t
2c660 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
2c670 20 74 65 78 74 20 62 65 66 6f 72 65 0a 2a 2a 20   text before.** 
2c680 74 68 65 79 20 72 65 74 75 72 6e 2e 20 20 48 65  they return.  He
2c690 6e 63 65 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67  nce, the calling
2c6a0 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 64 65   function can de
2c6b0 61 6c 6c 6f 63 61 74 65 20 6f 72 0a 2a 2a 20 6d  allocate or.** m
2c6c0 6f 64 69 66 79 20 74 68 65 20 74 65 78 74 20 61  odify the text a
2c6d0 66 74 65 72 20 74 68 65 79 20 72 65 74 75 72 6e  fter they return
2c6e0 20 77 69 74 68 6f 75 74 20 68 61 72 6d 2e 0a 2a   without harm..*
2c6f0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65  * The sqlite3_re
2c700 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28  sult_error_code(
2c710 29 20 66 75 6e 63 74 69 6f 6e 20 63 68 61 6e 67  ) function chang
2c720 65 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  es the error cod
2c730 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 62 79  e.** returned by
2c740 20 53 51 4c 69 74 65 20 61 73 20 61 20 72 65 73   SQLite as a res
2c750 75 6c 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 20  ult of an error 
2c760 69 6e 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20  in a function.  
2c770 42 79 20 64 65 66 61 75 6c 74 2c 0a 2a 2a 20 74  By default,.** t
2c780 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  he error code is
2c790 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20 20   SQLITE_ERROR.  
2c7a0 41 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c  A subsequent cal
2c7b0 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 73  l to sqlite3_res
2c7c0 75 6c 74 5f 65 72 72 6f 72 28 29 0a 2a 2a 20 6f  ult_error().** o
2c7d0 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  r sqlite3_result
2c7e0 5f 65 72 72 6f 72 31 36 28 29 20 72 65 73 65 74  _error16() reset
2c7f0 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  s the error code
2c800 20 74 6f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52   to SQLITE_ERROR
2c810 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
2c820 74 65 33 5f 72 65 73 75 6c 74 5f 74 6f 6f 62 69  te3_result_toobi
2c830 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61  g() interface ca
2c840 75 73 65 73 20 53 51 4c 69 74 65 20 74 6f 20 74  uses SQLite to t
2c850 68 72 6f 77 20 61 6e 20 65 72 72 6f 72 0a 2a 2a  hrow an error.**
2c860 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74   indicating that
2c870 20 61 20 73 74 72 69 6e 67 20 6f 72 20 42 4c 4f   a string or BLO
2c880 42 20 69 73 20 74 6f 20 6c 6f 6e 67 20 74 6f 20  B is to long to 
2c890 72 65 70 72 65 73 65 6e 74 2e 0a 2a 2a 0a 2a 2a  represent..**.**
2c8a0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   The sqlite3_res
2c8b0 75 6c 74 5f 6e 6f 6d 65 6d 28 29 20 69 6e 74 65  ult_nomem() inte
2c8c0 72 66 61 63 65 20 63 61 75 73 65 73 20 53 51 4c  rface causes SQL
2c8d0 69 74 65 20 74 6f 20 74 68 72 6f 77 20 61 6e 20  ite to throw an 
2c8e0 65 72 72 6f 72 0a 2a 2a 20 69 6e 64 69 63 61 74  error.** indicat
2c8f0 69 6e 67 20 74 68 61 74 20 61 20 6d 65 6d 6f 72  ing that a memor
2c900 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69  y allocation fai
2c910 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  led..**.** The s
2c920 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
2c930 74 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65  t() interface se
2c940 74 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  ts the return va
2c950 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61 70  lue.** of the ap
2c960 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
2c970 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65  d function to be
2c980 20 74 68 65 20 33 32 2d 62 69 74 20 73 69 67 6e   the 32-bit sign
2c990 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 76 61  ed integer.** va
2c9a0 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65  lue given in the
2c9b0 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a   2nd argument..*
2c9c0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65  * The sqlite3_re
2c9d0 73 75 6c 74 5f 69 6e 74 36 34 28 29 20 69 6e 74  sult_int64() int
2c9e0 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65 20  erface sets the 
2c9f0 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20  return value.** 
2ca00 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  of the applicati
2ca10 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  on-defined funct
2ca20 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 36 34  ion to be the 64
2ca30 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65  -bit signed inte
2ca40 67 65 72 0a 2a 2a 20 76 61 6c 75 65 20 67 69 76  ger.** value giv
2ca50 65 6e 20 69 6e 20 74 68 65 20 32 6e 64 20 61 72  en in the 2nd ar
2ca60 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  gument..**.** Th
2ca70 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
2ca80 5f 6e 75 6c 6c 28 29 20 69 6e 74 65 72 66 61 63  _null() interfac
2ca90 65 20 73 65 74 73 20 74 68 65 20 72 65 74 75 72  e sets the retur
2caa0 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68  n value.** of th
2cab0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  e application-de
2cac0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74  fined function t
2cad0 6f 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  o be NULL..**.**
2cae0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   The sqlite3_res
2caf0 75 6c 74 5f 74 65 78 74 28 29 2c 20 73 71 6c 69  ult_text(), sqli
2cb00 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
2cb10 36 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  6(),.** sqlite3_
2cb20 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 28  result_text16le(
2cb30 29 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72  ), and sqlite3_r
2cb40 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 29  esult_text16be()
2cb50 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 73   interfaces.** s
2cb60 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  et the return va
2cb70 6c 75 65 20 6f 66 20 74 68 65 20 61 70 70 6c 69  lue of the appli
2cb80 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66  cation-defined f
2cb90 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 0a 2a 2a  unction to be.**
2cba0 20 61 20 74 65 78 74 20 73 74 72 69 6e 67 20 77   a text string w
2cbb0 68 69 63 68 20 69 73 20 72 65 70 72 65 73 65 6e  hich is represen
2cbc0 74 65 64 20 61 73 20 55 54 46 2d 38 2c 20 55 54  ted as UTF-8, UT
2cbd0 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79 74 65  F-16 native byte
2cbe0 20 6f 72 64 65 72 2c 0a 2a 2a 20 55 54 46 2d 31   order,.** UTF-1
2cbf0 36 20 6c 69 74 74 6c 65 20 65 6e 64 69 61 6e 2c  6 little endian,
2cc00 20 6f 72 20 55 54 46 2d 31 36 20 62 69 67 20 65   or UTF-16 big e
2cc10 6e 64 69 61 6e 2c 20 72 65 73 70 65 63 74 69 76  ndian, respectiv
2cc20 65 6c 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 74  ely..** SQLite t
2cc30 61 6b 65 73 20 74 68 65 20 74 65 78 74 20 72 65  akes the text re
2cc40 73 75 6c 74 20 66 72 6f 6d 20 74 68 65 20 61 70  sult from the ap
2cc50 70 6c 69 63 61 74 69 6f 6e 20 66 72 6f 6d 0a 2a  plication from.*
2cc60 2a 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65  * the 2nd parame
2cc70 74 65 72 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ter of the sqlit
2cc80 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20  e3_result_text* 
2cc90 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a 20 49  interfaces..** I
2cca0 66 20 74 68 65 20 33 72 64 20 70 61 72 61 6d 65  f the 3rd parame
2ccb0 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74  ter to the sqlit
2ccc0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20  e3_result_text* 
2ccd0 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 69 73  interfaces.** is
2cce0 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20   negative, then 
2ccf0 53 51 4c 69 74 65 20 74 61 6b 65 73 20 72 65 73  SQLite takes res
2cd00 75 6c 74 20 74 65 78 74 20 66 72 6f 6d 20 74 68  ult text from th
2cd10 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 0a  e 2nd parameter.
2cd20 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20 66  ** through the f
2cd30 69 72 73 74 20 7a 65 72 6f 20 63 68 61 72 61 63  irst zero charac
2cd40 74 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20 33  ter..** If the 3
2cd50 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  rd parameter to 
2cd60 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  the sqlite3_resu
2cd70 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61  lt_text* interfa
2cd80 63 65 73 0a 2a 2a 20 69 73 20 6e 6f 6e 2d 6e 65  ces.** is non-ne
2cd90 67 61 74 69 76 65 2c 20 74 68 65 6e 20 61 73 20  gative, then as 
2cda0 6d 61 6e 79 20 62 79 74 65 73 20 28 6e 6f 74 20  many bytes (not 
2cdb0 63 68 61 72 61 63 74 65 72 73 29 20 6f 66 20 74  characters) of t
2cdc0 68 65 20 74 65 78 74 0a 2a 2a 20 70 6f 69 6e 74  he text.** point
2cdd0 65 64 20 74 6f 20 62 79 20 74 68 65 20 32 6e 64  ed to by the 2nd
2cde0 20 70 61 72 61 6d 65 74 65 72 20 61 72 65 20 74   parameter are t
2cdf0 61 6b 65 6e 20 61 73 20 74 68 65 20 61 70 70 6c  aken as the appl
2ce00 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 0a  ication-defined.
2ce10 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 73 75  ** function resu
2ce20 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 34 74  lt..** If the 4t
2ce30 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  h parameter to t
2ce40 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  he sqlite3_resul
2ce50 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63  t_text* interfac
2ce60 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33  es.** or sqlite3
2ce70 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73 20  _result_blob is 
2ce80 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74  a non-NULL point
2ce90 65 72 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20  er, then SQLite 
2cea0 63 61 6c 6c 73 20 74 68 61 74 0a 2a 2a 20 66 75  calls that.** fu
2ceb0 6e 63 74 69 6f 6e 20 61 73 20 74 68 65 20 64 65  nction as the de
2cec0 73 74 72 75 63 74 6f 72 20 6f 6e 20 74 68 65 20  structor on the 
2ced0 74 65 78 74 20 6f 72 20 42 4c 4f 42 20 72 65 73  text or BLOB res
2cee0 75 6c 74 20 77 68 65 6e 20 69 74 20 68 61 73 0a  ult when it has.
2cef0 2a 2a 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e  ** finished usin
2cf00 67 20 74 68 61 74 20 72 65 73 75 6c 74 2e 0a 2a  g that result..*
2cf10 2a 20 49 66 20 74 68 65 20 34 74 68 20 70 61 72  * If the 4th par
2cf20 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71  ameter to the sq
2cf30 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
2cf40 74 2a 20 69 6e 74 65 72 66 61 63 65 73 20 6f 72  t* interfaces or
2cf50 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72   to.** sqlite3_r
2cf60 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73 20 74 68  esult_blob is th
2cf70 65 20 73 70 65 63 69 61 6c 20 63 6f 6e 73 74 61  e special consta
2cf80 6e 74 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  nt SQLITE_STATIC
2cf90 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 0a 2a 2a  , then SQLite.**
2cfa0 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68   assumes that th
2cfb0 65 20 74 65 78 74 20 6f 72 20 42 4c 4f 42 20 72  e text or BLOB r
2cfc0 65 73 75 6c 74 20 69 73 20 69 6e 20 63 6f 6e 73  esult is in cons
2cfd0 74 61 6e 74 20 73 70 61 63 65 20 61 6e 64 20 64  tant space and d
2cfe0 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 6f 70 79 20  oes not.** copy 
2cff0 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74  the content of t
2d000 68 65 20 70 61 72 61 6d 65 74 65 72 20 6e 6f 72  he parameter nor
2d010 20 63 61 6c 6c 20 61 20 64 65 73 74 72 75 63 74   call a destruct
2d020 6f 72 20 6f 6e 20 74 68 65 20 63 6f 6e 74 65 6e  or on the conten
2d030 74 0a 2a 2a 20 77 68 65 6e 20 69 74 20 68 61 73  t.** when it has
2d040 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e 67 20   finished using 
2d050 74 68 61 74 20 72 65 73 75 6c 74 2e 0a 2a 2a 20  that result..** 
2d060 49 66 20 74 68 65 20 34 74 68 20 70 61 72 61 6d  If the 4th param
2d070 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69  eter to the sqli
2d080 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a  te3_result_text*
2d090 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 6f   interfaces.** o
2d0a0 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  r sqlite3_result
2d0b0 5f 62 6c 6f 62 20 69 73 20 74 68 65 20 73 70 65  _blob is the spe
2d0c0 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20 53 51  cial constant SQ
2d0d0 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 0a 2a  LITE_TRANSIENT.*
2d0e0 2a 20 74 68 65 6e 20 53 51 4c 69 74 65 20 6d 61  * then SQLite ma
2d0f0 6b 65 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68  kes a copy of th
2d100 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 73 70  e result into sp
2d110 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ace obtained fro
2d120 6d 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c 69 74  m.** from [sqlit
2d130 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 62 65 66  e3_malloc()] bef
2d140 6f 72 65 20 69 74 20 72 65 74 75 72 6e 73 2e 0a  ore it returns..
2d150 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
2d160 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29  3_result_value()
2d170 20 69 6e 74 65 72 66 61 63 65 20 73 65 74 73 20   interface sets 
2d180 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a  the result of.**
2d190 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
2d1a0 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
2d1b0 6e 20 74 6f 20 62 65 20 61 20 63 6f 70 79 20 74  n to be a copy t
2d1c0 68 65 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65 63 74  he.** [unprotect
2d1d0 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
2d1e0 5d 20 6f 62 6a 65 63 74 20 73 70 65 63 69 66 69  ] object specifi
2d1f0 65 64 20 62 79 20 74 68 65 20 32 6e 64 20 70 61  ed by the 2nd pa
2d200 72 61 6d 65 74 65 72 2e 20 20 54 68 65 0a 2a 2a  rameter.  The.**
2d210 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2d220 76 61 6c 75 65 28 29 20 69 6e 74 65 72 66 61 63  value() interfac
2d230 65 20 6d 61 6b 65 73 20 61 20 63 6f 70 79 20 6f  e makes a copy o
2d240 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76  f the [sqlite3_v
2d250 61 6c 75 65 5d 0a 2a 2a 20 73 6f 20 74 68 61 74  alue].** so that
2d260 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61   the [sqlite3_va
2d270 6c 75 65 5d 20 73 70 65 63 69 66 69 65 64 20 69  lue] specified i
2d280 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20  n the parameter 
2d290 6d 61 79 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a  may change or.**
2d2a0 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 20   be deallocated 
2d2b0 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f 72 65  after sqlite3_re
2d2c0 73 75 6c 74 5f 76 61 6c 75 65 28 29 20 72 65 74  sult_value() ret
2d2d0 75 72 6e 73 20 77 69 74 68 6f 75 74 20 68 61 72  urns without har
2d2e0 6d 2e 0a 2a 2a 20 41 20 5b 70 72 6f 74 65 63 74  m..** A [protect
2d2f0 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
2d300 5d 20 6f 62 6a 65 63 74 20 6d 61 79 20 61 6c 77  ] object may alw
2d310 61 79 73 20 62 65 20 75 73 65 64 20 77 68 65 72  ays be used wher
2d320 65 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65  e an.** [unprote
2d330 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c  cted sqlite3_val
2d340 75 65 5d 20 6f 62 6a 65 63 74 20 69 73 20 72 65  ue] object is re
2d350 71 75 69 72 65 64 2c 20 73 6f 20 65 69 74 68 65  quired, so eithe
2d360 72 0a 2a 2a 20 6b 69 6e 64 20 6f 66 20 5b 73 71  r.** kind of [sq
2d370 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a  lite3_value] obj
2d380 65 63 74 20 63 61 6e 20 62 65 20 75 73 65 64 20  ect can be used 
2d390 77 69 74 68 20 74 68 69 73 20 69 6e 74 65 72 66  with this interf
2d3a0 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ace..**.** If th
2d3b0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ese routines are
2d3c0 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74   called from wit
2d3d0 68 69 6e 20 74 68 65 20 64 69 66 66 65 72 65 6e  hin the differen
2d3e0 74 20 74 68 72 65 61 64 0a 2a 2a 20 74 68 61 6e  t thread.** than
2d3f0 20 74 68 65 20 6f 6e 65 20 63 6f 6e 74 61 69 6e   the one contain
2d400 69 6e 67 20 74 68 65 20 61 70 70 6c 69 63 61 74  ing the applicat
2d410 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
2d420 74 69 6f 6e 20 74 68 61 74 20 72 65 63 65 69 76  tion that receiv
2d430 65 64 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74  ed.** the [sqlit
2d440 65 33 5f 63 6f 6e 74 65 78 74 5d 20 70 6f 69 6e  e3_context] poin
2d450 74 65 72 2c 20 74 68 65 20 72 65 73 75 6c 74 73  ter, the results
2d460 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a   are undefined..
2d470 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
2d480 74 73 3a 0a 2a 2a 20 5b 48 31 36 34 30 33 5d 20  ts:.** [H16403] 
2d490 5b 48 31 36 34 30 36 5d 20 5b 48 31 36 34 30 39  [H16406] [H16409
2d4a0 5d 20 5b 48 31 36 34 31 32 5d 20 5b 48 31 36 34  ] [H16412] [H164
2d4b0 31 35 5d 20 5b 48 31 36 34 31 38 5d 20 5b 48 31  15] [H16418] [H1
2d4c0 36 34 32 31 5d 20 5b 48 31 36 34 32 34 5d 0a 2a  6421] [H16424].*
2d4d0 2a 20 5b 48 31 36 34 32 37 5d 20 5b 48 31 36 34  * [H16427] [H164
2d4e0 33 30 5d 20 5b 48 31 36 34 33 33 5d 20 5b 48 31  30] [H16433] [H1
2d4f0 36 34 33 36 5d 20 5b 48 31 36 34 33 39 5d 20 5b  6436] [H16439] [
2d500 48 31 36 34 34 32 5d 20 5b 48 31 36 34 34 35 5d  H16442] [H16445]
2d510 20 5b 48 31 36 34 34 38 5d 0a 2a 2a 20 5b 48 31   [H16448].** [H1
2d520 36 34 35 31 5d 20 5b 48 31 36 34 35 34 5d 20 5b  6451] [H16454] [
2d530 48 31 36 34 35 37 5d 20 5b 48 31 36 34 36 30 5d  H16457] [H16460]
2d540 20 5b 48 31 36 34 36 33 5d 0a 2a 2f 0a 53 51 4c   [H16463].*/.SQL
2d550 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
2d560 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
2d570 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2d580 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20  *, const void*, 
2d590 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69  int, void(*)(voi
2d5a0 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  d*));.SQLITE_API
2d5b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
2d5c0 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69  sult_double(sqli
2d5d0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 64 6f  te3_context*, do
2d5e0 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50  uble);.SQLITE_AP
2d5f0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
2d600 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c 69  esult_error(sqli
2d610 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f  te3_context*, co
2d620 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b  nst char*, int);
2d630 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
2d640 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2d650 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33 5f  error16(sqlite3_
2d660 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20  context*, const 
2d670 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  void*, int);.SQL
2d680 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
2d690 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
2d6a0 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 65 33  r_toobig(sqlite3
2d6b0 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49  _context*);.SQLI
2d6c0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
2d6d0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2d6e0 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33 5f 63  _nomem(sqlite3_c
2d6f0 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45  ontext*);.SQLITE
2d700 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
2d710 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63  3_result_error_c
2d720 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ode(sqlite3_cont
2d730 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ext*, int);.SQLI
2d740 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
2d750 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 73  te3_result_int(s
2d760 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
2d770 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50   int);.SQLITE_AP
2d780 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
2d790 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 6c 69  esult_int64(sqli
2d7a0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 73 71  te3_context*, sq
2d7b0 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51  lite3_int64);.SQ
2d7c0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
2d7d0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
2d7e0 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  l(sqlite3_contex
2d7f0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  t*);.SQLITE_API 
2d800 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
2d810 75 6c 74 5f 74 65 78 74 28 73 71 6c 69 74 65 33  ult_text(sqlite3
2d820 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74  _context*, const
2d830 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 76 6f 69   char*, int, voi
2d840 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51  d(*)(void*));.SQ
2d850 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
2d860 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
2d870 74 31 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  t16(sqlite3_cont
2d880 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64  ext*, const void
2d890 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28  *, int, void(*)(
2d8a0 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f  void*));.SQLITE_
2d8b0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
2d8c0 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65  _result_text16le
2d8d0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2d8e0 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20  *, const void*, 
2d8f0 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  int,void(*)(void
2d900 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  *));.SQLITE_API 
2d910 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
2d920 75 6c 74 5f 74 65 78 74 31 36 62 65 28 73 71 6c  ult_text16be(sql
2d930 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63  ite3_context*, c
2d940 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c  onst void*, int,
2d950 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
2d960 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
2d970 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2d980 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 63 6f  value(sqlite3_co
2d990 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 33 5f  ntext*, sqlite3_
2d9a0 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  value*);.SQLITE_
2d9b0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
2d9c0 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62  _result_zeroblob
2d9d0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2d9e0 2a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a  *, int n);../*.*
2d9f0 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 66 69  * CAPI3REF: Defi
2da00 6e 65 20 4e 65 77 20 43 6f 6c 6c 61 74 69 6e 67  ne New Collating
2da10 20 53 65 71 75 65 6e 63 65 73 20 7b 48 31 36 36   Sequences {H166
2da20 30 30 7d 20 3c 53 32 30 33 30 30 3e 0a 2a 2a 0a  00} <S20300>.**.
2da30 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f  ** These functio
2da40 6e 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 61  ns are used to a
2da50 64 64 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e  dd new collation
2da60 20 73 65 71 75 65 6e 63 65 73 20 74 6f 20 74 68   sequences to th
2da70 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63  e.** [database c
2da80 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69  onnection] speci
2da90 66 69 65 64 20 61 73 20 74 68 65 20 66 69 72 73  fied as the firs
2daa0 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a  t argument..**.*
2dab0 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  * The name of th
2dac0 65 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20  e new collation 
2dad0 73 65 71 75 65 6e 63 65 20 69 73 20 73 70 65 63  sequence is spec
2dae0 69 66 69 65 64 20 61 73 20 61 20 55 54 46 2d 38  ified as a UTF-8
2daf0 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f 72 20 73   string.** for s
2db00 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
2db10 6c 6c 61 74 69 6f 6e 28 29 20 61 6e 64 20 73 71  llation() and sq
2db20 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
2db30 6c 61 74 69 6f 6e 5f 76 32 28 29 0a 2a 2a 20 61  lation_v2().** a
2db40 6e 64 20 61 20 55 54 46 2d 31 36 20 73 74 72 69  nd a UTF-16 stri
2db50 6e 67 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63  ng for sqlite3_c
2db60 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31  reate_collation1
2db70 36 28 29 2e 20 49 6e 20 61 6c 6c 20 63 61 73 65  6(). In all case
2db80 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 69 73  s.** the name is
2db90 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73   passed as the s
2dba0 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61  econd function a
2dbb0 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  rgument..**.** T
2dbc0 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  he third argumen
2dbd0 74 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66 20  t may be one of 
2dbe0 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 5b 53  the constants [S
2dbf0 51 4c 49 54 45 5f 55 54 46 38 5d 2c 0a 2a 2a 20  QLITE_UTF8],.** 
2dc00 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d  [SQLITE_UTF16LE]
2dc10 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 54 46  , or [SQLITE_UTF
2dc20 31 36 42 45 5d 2c 20 69 6e 64 69 63 61 74 69 6e  16BE], indicatin
2dc30 67 20 74 68 61 74 20 74 68 65 20 75 73 65 72 2d  g that the user-
2dc40 73 75 70 70 6c 69 65 64 0a 2a 2a 20 72 6f 75 74  supplied.** rout
2dc50 69 6e 65 20 65 78 70 65 63 74 73 20 74 6f 20 62  ine expects to b
2dc60 65 20 70 61 73 73 65 64 20 70 6f 69 6e 74 65 72  e passed pointer
2dc70 73 20 74 6f 20 73 74 72 69 6e 67 73 20 65 6e 63  s to strings enc
2dc80 6f 64 65 64 20 75 73 69 6e 67 20 55 54 46 2d 38  oded using UTF-8
2dc90 2c 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69 74 74  ,.** UTF-16 litt
2dca0 6c 65 2d 65 6e 64 69 61 6e 2c 20 6f 72 20 55 54  le-endian, or UT
2dcb0 46 2d 31 36 20 62 69 67 2d 65 6e 64 69 61 6e 2c  F-16 big-endian,
2dcc0 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54   respectively. T
2dcd0 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 72 67 75  he.** third argu
2dce0 6d 65 6e 74 20 6d 69 67 68 74 20 61 6c 73 6f 20  ment might also 
2dcf0 62 65 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36  be [SQLITE_UTF16
2dd00 5d 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68  ] to indicate th
2dd10 61 74 20 74 68 65 20 72 6f 75 74 69 6e 65 0a 2a  at the routine.*
2dd20 2a 20 65 78 70 65 63 74 73 20 70 6f 69 6e 74 65  * expects pointe
2dd30 72 73 20 74 6f 20 62 65 20 55 54 46 2d 31 36 20  rs to be UTF-16 
2dd40 73 74 72 69 6e 67 73 20 69 6e 20 74 68 65 20 6e  strings in the n
2dd50 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72  ative byte order
2dd60 2c 20 6f 72 20 74 68 65 0a 2a 2a 20 61 72 67 75  , or the.** argu
2dd70 6d 65 6e 74 20 63 61 6e 20 62 65 20 5b 53 51 4c  ment can be [SQL
2dd80 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45  ITE_UTF16_ALIGNE
2dd90 44 5d 20 69 66 20 74 68 65 0a 2a 2a 20 74 68 65  D] if the.** the
2dda0 20 72 6f 75 74 69 6e 65 20 65 78 70 65 63 74 73   routine expects
2ddb0 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 31 36 2d   pointers to 16-
2ddc0 62 69 74 20 77 6f 72 64 20 61 6c 69 67 6e 65 64  bit word aligned
2ddd0 20 73 74 72 69 6e 67 73 0a 2a 2a 20 6f 66 20 55   strings.** of U
2dde0 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74  TF-16 in the nat
2ddf0 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 2e 0a  ive byte order..
2de00 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20  **.** A pointer 
2de10 74 6f 20 74 68 65 20 75 73 65 72 20 73 75 70 70  to the user supp
2de20 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 6d 75 73  lied routine mus
2de30 74 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74  t be passed as t
2de40 68 65 20 66 69 66 74 68 0a 2a 2a 20 61 72 67 75  he fifth.** argu
2de50 6d 65 6e 74 2e 20 20 49 66 20 69 74 20 69 73 20  ment.  If it is 
2de60 4e 55 4c 4c 2c 20 74 68 69 73 20 69 73 20 74 68  NULL, this is th
2de70 65 20 73 61 6d 65 20 61 73 20 64 65 6c 65 74 69  e same as deleti
2de80 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  ng the collation
2de90 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 28 73 6f  .** sequence (so
2dea0 20 74 68 61 74 20 53 51 4c 69 74 65 20 63 61 6e   that SQLite can
2deb0 6e 6f 74 20 63 61 6c 6c 20 69 74 20 61 6e 79 6d  not call it anym
2dec0 6f 72 65 29 2e 0a 2a 2a 20 45 61 63 68 20 74 69  ore)..** Each ti
2ded0 6d 65 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  me the applicati
2dee0 6f 6e 20 73 75 70 70 6c 69 65 64 20 66 75 6e 63  on supplied func
2def0 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 2c  tion is invoked,
2df00 20 69 74 20 69 73 20 70 61 73 73 65 64 0a 2a 2a   it is passed.**
2df10 20 61 73 20 69 74 73 20 66 69 72 73 74 20 70 61   as its first pa
2df20 72 61 6d 65 74 65 72 20 61 20 63 6f 70 79 20 6f  rameter a copy o
2df30 66 20 74 68 65 20 76 6f 69 64 2a 20 70 61 73 73  f the void* pass
2df40 65 64 20 61 73 20 74 68 65 20 66 6f 75 72 74 68  ed as the fourth
2df50 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20   argument.** to 
2df60 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
2df70 6f 6c 6c 61 74 69 6f 6e 28 29 20 6f 72 20 73 71  ollation() or sq
2df80 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
2df90 6c 61 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a  lation16()..**.*
2dfa0 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e 67 20  * The remaining 
2dfb0 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65  arguments to the
2dfc0 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 75 70   application-sup
2dfd0 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 61 72  plied routine ar
2dfe0 65 20 74 77 6f 20 73 74 72 69 6e 67 73 2c 0a 2a  e two strings,.*
2dff0 2a 20 65 61 63 68 20 72 65 70 72 65 73 65 6e 74  * each represent
2e000 65 64 20 62 79 20 61 20 28 6c 65 6e 67 74 68 2c  ed by a (length,
2e010 20 64 61 74 61 29 20 70 61 69 72 20 61 6e 64 20   data) pair and 
2e020 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 20 65  encoded in the e
2e030 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 68 61 74 20  ncoding.** that 
2e040 77 61 73 20 70 61 73 73 65 64 20 61 73 20 74 68  was passed as th
2e050 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  e third argument
2e060 20 77 68 65 6e 20 74 68 65 20 63 6f 6c 6c 61 74   when the collat
2e070 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 61 73  ion sequence was
2e080 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 2e 20  .** registered. 
2e090 7b 45 4e 44 7d 20 20 54 68 65 20 61 70 70 6c 69  {END}  The appli
2e0a0 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 63  cation defined c
2e0b0 6f 6c 6c 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65  ollation routine
2e0c0 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 72   should.** retur
2e0d0 6e 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f  n negative, zero
2e0e0 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66 20   or positive if 
2e0f0 74 68 65 20 66 69 72 73 74 20 73 74 72 69 6e 67  the first string
2e100 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 0a 2a   is less than,.*
2e110 2a 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67  * equal to, or g
2e120 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20  reater than the 
2e130 73 65 63 6f 6e 64 20 73 74 72 69 6e 67 2e 20 69  second string. i
2e140 2e 65 2e 20 28 53 54 52 49 4e 47 31 20 2d 20 53  .e. (STRING1 - S
2e150 54 52 49 4e 47 32 29 2e 0a 2a 2a 0a 2a 2a 20 54  TRING2)..**.** T
2e160 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  he sqlite3_creat
2e170 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29  e_collation_v2()
2e180 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69   works like sqli
2e190 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
2e1a0 74 69 6f 6e 28 29 0a 2a 2a 20 65 78 63 65 70 74  tion().** except
2e1b0 20 74 68 61 74 20 69 74 20 74 61 6b 65 73 20 61   that it takes a
2e1c0 6e 20 65 78 74 72 61 20 61 72 67 75 6d 65 6e 74  n extra argument
2e1d0 20 77 68 69 63 68 20 69 73 20 61 20 64 65 73 74   which is a dest
2e1e0 72 75 63 74 6f 72 20 66 6f 72 0a 2a 2a 20 74 68  ructor for.** th
2e1f0 65 20 63 6f 6c 6c 61 74 69 6f 6e 2e 20 20 54 68  e collation.  Th
2e200 65 20 64 65 73 74 72 75 63 74 6f 72 20 69 73 20  e destructor is 
2e210 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20  called when the 
2e220 63 6f 6c 6c 61 74 69 6f 6e 20 69 73 0a 2a 2a 20  collation is.** 
2e230 64 65 73 74 72 6f 79 65 64 20 61 6e 64 20 69 73  destroyed and is
2e240 20 70 61 73 73 65 64 20 61 20 63 6f 70 79 20 6f   passed a copy o
2e250 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72  f the fourth par
2e260 61 6d 65 74 65 72 20 76 6f 69 64 2a 20 70 6f 69  ameter void* poi
2e270 6e 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65 20 73  nter.** of the s
2e280 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
2e290 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 2e 0a 2a 2a  llation_v2()..**
2e2a0 20 43 6f 6c 6c 61 74 69 6f 6e 73 20 61 72 65 20   Collations are 
2e2b0 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e 20 74  destroyed when t
2e2c0 68 65 79 20 61 72 65 20 6f 76 65 72 72 69 64 64  hey are overridd
2e2d0 65 6e 20 62 79 20 6c 61 74 65 72 20 63 61 6c 6c  en by later call
2e2e0 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 6c 6c  s to the.** coll
2e2f0 61 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 66  ation creation f
2e300 75 6e 63 74 69 6f 6e 73 20 6f 72 20 77 68 65 6e  unctions or when
2e310 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
2e320 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20 63 6c  onnection] is cl
2e330 6f 73 65 64 0a 2a 2a 20 75 73 69 6e 67 20 5b 73  osed.** using [s
2e340 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 2e  qlite3_close()].
2e350 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
2e360 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61    [sqlite3_colla
2e370 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 5d 20 61  tion_needed()] a
2e380 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c  nd [sqlite3_coll
2e390 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 29  ation_needed16()
2e3a0 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  ]..**.** Require
2e3b0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 36 30  ments:.** [H1660
2e3c0 33 5d 20 5b 48 31 36 36 30 34 5d 20 5b 48 31 36  3] [H16604] [H16
2e3d0 36 30 36 5d 20 5b 48 31 36 36 30 39 5d 20 5b 48  606] [H16609] [H
2e3e0 31 36 36 31 32 5d 20 5b 48 31 36 36 31 35 5d 20  16612] [H16615] 
2e3f0 5b 48 31 36 36 31 38 5d 20 5b 48 31 36 36 32 31  [H16618] [H16621
2e400 5d 0a 2a 2a 20 5b 48 31 36 36 32 34 5d 20 5b 48  ].** [H16624] [H
2e410 31 36 36 32 37 5d 20 5b 48 31 36 36 33 30 5d 0a  16627] [H16630].
2e420 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
2e430 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  t sqlite3_create
2e440 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 71  _collation(.  sq
2e450 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 74  lite3*, .  const
2e460 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20   char *zName, . 
2e470 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a   int eTextRep, .
2e480 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a    void*,.  int(*
2e490 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c  xCompare)(void*,
2e4a0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
2e4b0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29  int,const void*)
2e4c0 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .);.SQLITE_API i
2e4d0 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  nt sqlite3_creat
2e4e0 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 0a  e_collation_v2(.
2e4f0 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63    sqlite3*, .  c
2e500 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2e510 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65  , .  int eTextRe
2e520 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69  p, .  void*,.  i
2e530 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f  nt(*xCompare)(vo
2e540 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
2e550 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
2e560 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78 44  id*),.  void(*xD
2e570 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 0a 29  estroy)(void*).)
2e580 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
2e590 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2e5a0 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 20 73  collation16(.  s
2e5b0 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73  qlite3*, .  cons
2e5c0 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c 0a 20  t void *zName,. 
2e5d0 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a   int eTextRep, .
2e5e0 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a    void*,.  int(*
2e5f0 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c  xCompare)(void*,
2e600 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
2e610 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29  int,const void*)
2e620 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .);../*.** CAPI3
2e630 52 45 46 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 4e  REF: Collation N
2e640 65 65 64 65 64 20 43 61 6c 6c 62 61 63 6b 73 20  eeded Callbacks 
2e650 7b 48 31 36 37 30 30 7d 20 3c 53 32 30 33 30 30  {H16700} <S20300
2e660 3e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64  >.**.** To avoid
2e670 20 68 61 76 69 6e 67 20 74 6f 20 72 65 67 69 73   having to regis
2e680 74 65 72 20 61 6c 6c 20 63 6f 6c 6c 61 74 69 6f  ter all collatio
2e690 6e 20 73 65 71 75 65 6e 63 65 73 20 62 65 66 6f  n sequences befo
2e6a0 72 65 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a  re a database.**
2e6b0 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20 61 20   can be used, a 
2e6c0 73 69 6e 67 6c 65 20 63 61 6c 6c 62 61 63 6b 20  single callback 
2e6d0 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20  function may be 
2e6e0 72 65 67 69 73 74 65 72 65 64 20 77 69 74 68 20  registered with 
2e6f0 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65  the.** [database
2e700 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 6f 20   connection] to 
2e710 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 65 76  be called whenev
2e720 65 72 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 20  er an undefined 
2e730 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71  collation.** seq
2e740 75 65 6e 63 65 20 69 73 20 72 65 71 75 69 72 65  uence is require
2e750 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
2e760 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 67 69  function is regi
2e770 73 74 65 72 65 64 20 75 73 69 6e 67 20 74 68 65  stered using the
2e780 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69   sqlite3_collati
2e790 6f 6e 5f 6e 65 65 64 65 64 28 29 20 41 50 49 2c  on_needed() API,
2e7a0 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20 70  .** then it is p
2e7b0 61 73 73 65 64 20 74 68 65 20 6e 61 6d 65 73 20  assed the names 
2e7c0 6f 66 20 75 6e 64 65 66 69 6e 65 64 20 63 6f 6c  of undefined col
2e7d0 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73  lation sequences
2e7e0 20 61 73 20 73 74 72 69 6e 67 73 0a 2a 2a 20 65   as strings.** e
2e7f0 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 2e  ncoded in UTF-8.
2e800 20 7b 48 31 36 37 30 33 7d 20 49 66 20 73 71 6c   {H16703} If sql
2e810 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
2e820 65 65 64 65 64 31 36 28 29 20 69 73 20 75 73 65  eeded16() is use
2e830 64 2c 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 73 20  d,.** the names 
2e840 61 72 65 20 70 61 73 73 65 64 20 61 73 20 55 54  are passed as UT
2e850 46 2d 31 36 20 69 6e 20 6d 61 63 68 69 6e 65 20  F-16 in machine 
2e860 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65  native byte orde
2e870 72 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20  r..** A call to 
2e880 65 69 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20  either function 
2e890 72 65 70 6c 61 63 65 73 20 61 6e 79 20 65 78 69  replaces any exi
2e8a0 73 74 69 6e 67 20 63 61 6c 6c 62 61 63 6b 2e 0a  sting callback..
2e8b0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63  **.** When the c
2e8c0 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b  allback is invok
2e8d0 65 64 2c 20 74 68 65 20 66 69 72 73 74 20 61 72  ed, the first ar
2e8e0 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 69 73  gument passed is
2e8f0 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66 20 74 68   a copy.** of th
2e900 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
2e910 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c  t to sqlite3_col
2e920 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20  lation_needed() 
2e930 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f  or.** sqlite3_co
2e940 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36  llation_needed16
2e950 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20  ().  The second 
2e960 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
2e970 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e  database.** conn
2e980 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 68 69  ection.  The thi
2e990 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f  rd argument is o
2e9a0 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 55 54  ne of [SQLITE_UT
2e9b0 46 38 5d 2c 20 5b 53 51 4c 49 54 45 5f 55 54 46  F8], [SQLITE_UTF
2e9c0 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51  16BE],.** or [SQ
2e9d0 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d 2c 20 69  LITE_UTF16LE], i
2e9e0 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 6d 6f  ndicating the mo
2e9f0 73 74 20 64 65 73 69 72 61 62 6c 65 20 66 6f 72  st desirable for
2ea00 6d 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69  m of the collati
2ea10 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 66  on.** sequence f
2ea20 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65 64  unction required
2ea30 2e 20 20 54 68 65 20 66 6f 75 72 74 68 20 70 61  .  The fourth pa
2ea40 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 6e  rameter is the n
2ea50 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65  ame of the.** re
2ea60 71 75 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e  quired collation
2ea70 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a   sequence..**.**
2ea80 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75   The callback fu
2ea90 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72 65  nction should re
2eaa0 67 69 73 74 65 72 20 74 68 65 20 64 65 73 69 72  gister the desir
2eab0 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 75 73 69  ed collation usi
2eac0 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63  ng.** [sqlite3_c
2ead0 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28  reate_collation(
2eae0 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 65  )], [sqlite3_cre
2eaf0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28  ate_collation16(
2eb00 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74  )], or.** [sqlit
2eb10 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
2eb20 69 6f 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a  ion_v2()]..**.**
2eb30 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
2eb40 2a 20 5b 48 31 36 37 30 32 5d 20 5b 48 31 36 37  * [H16702] [H167
2eb50 30 34 5d 20 5b 48 31 36 37 30 36 5d 0a 2a 2f 0a  04] [H16706].*/.
2eb60 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2eb70 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
2eb80 5f 6e 65 65 64 65 64 28 0a 20 20 73 71 6c 69 74  _needed(.  sqlit
2eb90 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c 20 0a  e3*, .  void*, .
2eba0 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c    void(*)(void*,
2ebb0 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65  sqlite3*,int eTe
2ebc0 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 61 72  xtRep,const char
2ebd0 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  *).);.SQLITE_API
2ebe0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c   int sqlite3_col
2ebf0 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28  lation_needed16(
2ec00 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20  .  sqlite3*, .  
2ec10 76 6f 69 64 2a 2c 0a 20 20 76 6f 69 64 28 2a 29  void*,.  void(*)
2ec20 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c  (void*,sqlite3*,
2ec30 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e  int eTextRep,con
2ec40 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a 0a 2f 2a  st void*).);../*
2ec50 0a 2a 2a 20 53 70 65 63 69 66 79 20 74 68 65 20  .** Specify the 
2ec60 6b 65 79 20 66 6f 72 20 61 6e 20 65 6e 63 72 79  key for an encry
2ec70 70 74 65 64 20 64 61 74 61 62 61 73 65 2e 20 20  pted database.  
2ec80 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f  This routine sho
2ec90 75 6c 64 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64  uld be.** called
2eca0 20 72 69 67 68 74 20 61 66 74 65 72 20 73 71 6c   right after sql
2ecb0 69 74 65 33 5f 6f 70 65 6e 28 29 2e 0a 2a 2a 0a  ite3_open()..**.
2ecc0 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 6f 20 69  ** The code to i
2ecd0 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 20 41 50  mplement this AP
2ece0 49 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62  I is not availab
2ecf0 6c 65 20 69 6e 20 74 68 65 20 70 75 62 6c 69 63  le in the public
2ed00 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20 53   release.** of S
2ed10 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  QLite..*/.SQLITE
2ed20 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2ed30 5f 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 33 20  _key(.  sqlite3 
2ed40 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
2ed50 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
2ed60 73 65 20 74 6f 20 62 65 20 72 65 6b 65 79 65 64  se to be rekeyed
2ed70 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
2ed80 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79   *pKey, int nKey
2ed90 20 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20       /* The key 
2eda0 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 68 61  */.);../*.** Cha
2edb0 6e 67 65 20 74 68 65 20 6b 65 79 20 6f 6e 20 61  nge the key on a
2edc0 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 2e  n open database.
2edd0 20 20 49 66 20 74 68 65 20 63 75 72 72 65 6e 74    If the current
2ede0 20 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74   database is not
2edf0 0a 2a 2a 20 65 6e 63 72 79 70 74 65 64 2c 20 74  .** encrypted, t
2ee00 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
2ee10 20 65 6e 63 72 79 70 74 20 69 74 2e 20 20 49 66   encrypt it.  If
2ee20 20 70 4e 65 77 3d 3d 30 20 6f 72 20 6e 4e 65 77   pNew==0 or nNew
2ee30 3d 3d 30 2c 20 74 68 65 0a 2a 2a 20 64 61 74 61  ==0, the.** data
2ee40 62 61 73 65 20 69 73 20 64 65 63 72 79 70 74 65  base is decrypte
2ee50 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64  d..**.** The cod
2ee60 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  e to implement t
2ee70 68 69 73 20 41 50 49 20 69 73 20 6e 6f 74 20 61  his API is not a
2ee80 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20  vailable in the 
2ee90 70 75 62 6c 69 63 20 72 65 6c 65 61 73 65 0a 2a  public release.*
2eea0 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a  * of SQLite..*/.
2eeb0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2eec0 71 6c 69 74 65 33 5f 72 65 6b 65 79 28 0a 20 20  qlite3_rekey(.  
2eed0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
2eee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2eef0 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20 62 65  * Database to be
2ef00 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 20 63 6f   rekeyed */.  co
2ef10 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
2ef20 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 2f 2a 20  int nKey     /* 
2ef30 54 68 65 20 6e 65 77 20 6b 65 79 20 2a 2f 0a 29  The new key */.)
2ef40 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
2ef50 46 3a 20 53 75 73 70 65 6e 64 20 45 78 65 63 75  F: Suspend Execu
2ef60 74 69 6f 6e 20 46 6f 72 20 41 20 53 68 6f 72 74  tion For A Short
2ef70 20 54 69 6d 65 20 7b 48 31 30 35 33 30 7d 20 3c   Time {H10530} <
2ef80 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S40410>.**.** Th
2ef90 65 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 28  e sqlite3_sleep(
2efa0 29 20 66 75 6e 63 74 69 6f 6e 20 63 61 75 73 65  ) function cause
2efb0 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68  s the current th
2efc0 72 65 61 64 20 74 6f 20 73 75 73 70 65 6e 64 20  read to suspend 
2efd0 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 66 6f 72  execution.** for
2efe0 20 61 74 20 6c 65 61 73 74 20 61 20 6e 75 6d 62   at least a numb
2eff0 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e  er of millisecon
2f000 64 73 20 73 70 65 63 69 66 69 65 64 20 69 6e 20  ds specified in 
2f010 69 74 73 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a  its parameter..*
2f020 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 65 72  *.** If the oper
2f030 61 74 69 6e 67 20 73 79 73 74 65 6d 20 64 6f 65  ating system doe
2f040 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 73 6c  s not support sl
2f050 65 65 70 20 72 65 71 75 65 73 74 73 20 77 69 74  eep requests wit
2f060 68 0a 2a 2a 20 6d 69 6c 6c 69 73 65 63 6f 6e 64  h.** millisecond
2f070 20 74 69 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e   time resolution
2f080 2c 20 74 68 65 6e 20 74 68 65 20 74 69 6d 65 20  , then the time 
2f090 77 69 6c 6c 20 62 65 20 72 6f 75 6e 64 65 64 20  will be rounded 
2f0a0 75 70 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 65 61  up to.** the nea
2f0b0 72 65 73 74 20 73 65 63 6f 6e 64 2e 20 54 68 65  rest second. The
2f0c0 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69   number of milli
2f0d0 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70  seconds of sleep
2f0e0 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71   actually.** req
2f0f0 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20  uested from the 
2f100 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
2f110 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
2f120 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6d 70 6c 65  .** SQLite imple
2f130 6d 65 6e 74 73 20 74 68 69 73 20 69 6e 74 65 72  ments this inter
2f140 66 61 63 65 20 62 79 20 63 61 6c 6c 69 6e 67 20  face by calling 
2f150 74 68 65 20 78 53 6c 65 65 70 28 29 0a 2a 2a 20  the xSleep().** 
2f160 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 64 65  method of the de
2f170 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76  fault [sqlite3_v
2f180 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a  fs] object..**.*
2f190 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20  * Requirements: 
2f1a0 5b 48 31 30 35 33 33 5d 20 5b 48 31 30 35 33 36  [H10533] [H10536
2f1b0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
2f1c0 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6c 65 65  int sqlite3_slee
2f1d0 70 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  p(int);../*.** C
2f1e0 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20 4f 66  API3REF: Name Of
2f1f0 20 54 68 65 20 46 6f 6c 64 65 72 20 48 6f 6c 64   The Folder Hold
2f200 69 6e 67 20 54 65 6d 70 6f 72 61 72 79 20 46 69  ing Temporary Fi
2f210 6c 65 73 20 7b 48 31 30 33 31 30 7d 20 3c 53 32  les {H10310} <S2
2f220 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  0000>.**.** If t
2f230 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61  his global varia
2f240 62 6c 65 20 69 73 20 6d 61 64 65 20 74 6f 20 70  ble is made to p
2f250 6f 69 6e 74 20 74 6f 20 61 20 73 74 72 69 6e 67  oint to a string
2f260 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 74 68 65   which is.** the
2f270 20 6e 61 6d 65 20 6f 66 20 61 20 66 6f 6c 64 65   name of a folde
2f280 72 20 28 61 2e 6b 2e 61 2e 20 64 69 72 65 63 74  r (a.k.a. direct
2f290 6f 72 79 29 2c 20 74 68 65 6e 20 61 6c 6c 20 74  ory), then all t
2f2a0 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 0a 2a  emporary files.*
2f2b0 2a 20 63 72 65 61 74 65 64 20 62 79 20 53 51 4c  * created by SQL
2f2c0 69 74 65 20 77 69 6c 6c 20 62 65 20 70 6c 61 63  ite will be plac
2f2d0 65 64 20 69 6e 20 74 68 61 74 20 64 69 72 65 63  ed in that direc
2f2e0 74 6f 72 79 2e 20 20 49 66 20 74 68 69 73 20 76  tory.  If this v
2f2f0 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20 61 20  ariable.** is a 
2f300 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68  NULL pointer, th
2f310 65 6e 20 53 51 4c 69 74 65 20 70 65 72 66 6f 72  en SQLite perfor
2f320 6d 73 20 61 20 73 65 61 72 63 68 20 66 6f 72 20  ms a search for 
2f330 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a  an appropriate.*
2f340 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  * temporary file
2f350 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 0a 2a   directory..**.*
2f360 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65  * It is not safe
2f370 20 74 6f 20 72 65 61 64 20 6f 72 20 6d 6f 64 69   to read or modi
2f380 66 79 20 74 68 69 73 20 76 61 72 69 61 62 6c 65  fy this variable
2f390 20 69 6e 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e   in more than on
2f3a0 65 0a 2a 2a 20 74 68 72 65 61 64 20 61 74 20 61  e.** thread at a
2f3b0 20 74 69 6d 65 2e 20 20 49 74 20 69 73 20 6e 6f   time.  It is no
2f3c0 74 20 73 61 66 65 20 74 6f 20 72 65 61 64 20 6f  t safe to read o
2f3d0 72 20 6d 6f 64 69 66 79 20 74 68 69 73 20 76 61  r modify this va
2f3e0 72 69 61 62 6c 65 0a 2a 2a 20 69 66 20 61 20 5b  riable.** if a [
2f3f0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2f400 69 6f 6e 5d 20 69 73 20 62 65 69 6e 67 20 75 73  ion] is being us
2f410 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 20 74  ed at the same t
2f420 69 6d 65 20 69 6e 20 61 20 73 65 70 61 72 61 74  ime in a separat
2f430 65 0a 2a 2a 20 74 68 72 65 61 64 2e 0a 2a 2a 20  e.** thread..** 
2f440 49 74 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74  It is intended t
2f450 68 61 74 20 74 68 69 73 20 76 61 72 69 61 62 6c  hat this variabl
2f460 65 20 62 65 20 73 65 74 20 6f 6e 63 65 0a 2a 2a  e be set once.**
2f470 20 61 73 20 70 61 72 74 20 6f 66 20 70 72 6f 63   as part of proc
2f480 65 73 73 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  ess initializati
2f490 6f 6e 20 61 6e 64 20 62 65 66 6f 72 65 20 61 6e  on and before an
2f4a0 79 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61  y SQLite interfa
2f4b0 63 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 68  ce.** routines h
2f4c0 61 76 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20  ave been called 
2f4d0 61 6e 64 20 74 68 61 74 20 74 68 69 73 20 76 61  and that this va
2f4e0 72 69 61 62 6c 65 20 72 65 6d 61 69 6e 20 75 6e  riable remain un
2f4f0 63 68 61 6e 67 65 64 0a 2a 2a 20 74 68 65 72 65  changed.** there
2f500 61 66 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  after..**.** The
2f510 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72   [temp_store_dir
2f520 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 6d  ectory pragma] m
2f530 61 79 20 6d 6f 64 69 66 79 20 74 68 69 73 20 76  ay modify this v
2f540 61 72 69 61 62 6c 65 20 61 6e 64 20 63 61 75 73  ariable and caus
2f550 65 0a 2a 2a 20 69 74 20 74 6f 20 70 6f 69 6e 74  e.** it to point
2f560 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69   to memory obtai
2f570 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65  ned from [sqlite
2f580 33 5f 6d 61 6c 6c 6f 63 5d 2e 20 20 46 75 72 74  3_malloc].  Furt
2f590 68 65 72 6d 6f 72 65 2c 0a 2a 2a 20 74 68 65 20  hermore,.** the 
2f5a0 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65  [temp_store_dire
2f5b0 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 61 6c  ctory pragma] al
2f5c0 77 61 79 73 20 61 73 73 75 6d 65 73 20 74 68 61  ways assumes tha
2f5d0 74 20 61 6e 79 20 73 74 72 69 6e 67 0a 2a 2a 20  t any string.** 
2f5e0 74 68 61 74 20 74 68 69 73 20 76 61 72 69 61 62  that this variab
2f5f0 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20  le points to is 
2f600 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f  held in memory o
2f610 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 0a 2a 2a  btained from .**
2f620 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
2f630 5d 20 61 6e 64 20 74 68 65 20 70 72 61 67 6d 61  ] and the pragma
2f640 20 6d 61 79 20 61 74 74 65 6d 70 74 20 74 6f 20   may attempt to 
2f650 66 72 65 65 20 74 68 61 74 20 6d 65 6d 6f 72 79  free that memory
2f660 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  .** using [sqlit
2f670 65 33 5f 66 72 65 65 5d 2e 0a 2a 2a 20 48 65 6e  e3_free]..** Hen
2f680 63 65 2c 20 69 66 20 74 68 69 73 20 76 61 72 69  ce, if this vari
2f690 61 62 6c 65 20 69 73 20 6d 6f 64 69 66 69 65 64  able is modified
2f6a0 20 64 69 72 65 63 74 6c 79 2c 20 65 69 74 68 65   directly, eithe
2f6b0 72 20 69 74 20 73 68 6f 75 6c 64 20 62 65 0a 2a  r it should be.*
2f6c0 2a 20 6d 61 64 65 20 4e 55 4c 4c 20 6f 72 20 6d  * made NULL or m
2f6d0 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  ade to point to 
2f6e0 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20  memory obtained 
2f6f0 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61  from [sqlite3_ma
2f700 6c 6c 6f 63 5d 0a 2a 2a 20 6f 72 20 65 6c 73 65  lloc].** or else
2f710 20 74 68 65 20 75 73 65 20 6f 66 20 74 68 65 20   the use of the 
2f720 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65  [temp_store_dire
2f730 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 73 68  ctory pragma] sh
2f740 6f 75 6c 64 20 62 65 20 61 76 6f 69 64 65 64 2e  ould be avoided.
2f750 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
2f760 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 74 65 6d  har *sqlite3_tem
2f770 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 0a 2f 2a  p_directory;../*
2f780 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65  .** CAPI3REF: Te
2f790 73 74 20 46 6f 72 20 41 75 74 6f 2d 43 6f 6d 6d  st For Auto-Comm
2f7a0 69 74 20 4d 6f 64 65 20 7b 48 31 32 39 33 30 7d  it Mode {H12930}
2f7b0 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 4b 45 59   <S60200>.** KEY
2f7c0 57 4f 52 44 53 3a 20 7b 61 75 74 6f 63 6f 6d 6d  WORDS: {autocomm
2f7d0 69 74 20 6d 6f 64 65 7d 0a 2a 2a 0a 2a 2a 20 54  it mode}.**.** T
2f7e0 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61  he sqlite3_get_a
2f7f0 75 74 6f 63 6f 6d 6d 69 74 28 29 20 69 6e 74 65  utocommit() inte
2f800 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 6e 6f  rface returns no
2f810 6e 2d 7a 65 72 6f 20 6f 72 0a 2a 2a 20 7a 65 72  n-zero or.** zer
2f820 6f 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 64  o if the given d
2f830 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
2f840 6f 6e 20 69 73 20 6f 72 20 69 73 20 6e 6f 74 20  on is or is not 
2f850 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f  in autocommit mo
2f860 64 65 2c 0a 2a 2a 20 72 65 73 70 65 63 74 69 76  de,.** respectiv
2f870 65 6c 79 2e 20 20 41 75 74 6f 63 6f 6d 6d 69 74  ely.  Autocommit
2f880 20 6d 6f 64 65 20 69 73 20 6f 6e 20 62 79 20 64   mode is on by d
2f890 65 66 61 75 6c 74 2e 0a 2a 2a 20 41 75 74 6f 63  efault..** Autoc
2f8a0 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 64 69  ommit mode is di
2f8b0 73 61 62 6c 65 64 20 62 79 20 61 20 5b 42 45 47  sabled by a [BEG
2f8c0 49 4e 5d 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  IN] statement..*
2f8d0 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64  * Autocommit mod
2f8e0 65 20 69 73 20 72 65 2d 65 6e 61 62 6c 65 64 20  e is re-enabled 
2f8f0 62 79 20 61 20 5b 43 4f 4d 4d 49 54 5d 20 6f 72  by a [COMMIT] or
2f900 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a 2a 0a   [ROLLBACK]..**.
2f910 2a 2a 20 49 66 20 63 65 72 74 61 69 6e 20 6b 69  ** If certain ki
2f920 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 20 6f 63  nds of errors oc
2f930 63 75 72 20 6f 6e 20 61 20 73 74 61 74 65 6d 65  cur on a stateme
2f940 6e 74 20 77 69 74 68 69 6e 20 61 20 6d 75 6c 74  nt within a mult
2f950 69 2d 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74  i-statement.** t
2f960 72 61 6e 73 61 63 74 69 6f 6e 20 28 65 72 72 6f  ransaction (erro
2f970 72 73 20 69 6e 63 6c 75 64 69 6e 67 20 5b 53 51  rs including [SQ
2f980 4c 49 54 45 5f 46 55 4c 4c 5d 2c 20 5b 53 51 4c  LITE_FULL], [SQL
2f990 49 54 45 5f 49 4f 45 52 52 5d 2c 0a 2a 2a 20 5b  ITE_IOERR],.** [
2f9a0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2c 20 5b  SQLITE_NOMEM], [
2f9b0 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c 20 61 6e  SQLITE_BUSY], an
2f9c0 64 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 52  d [SQLITE_INTERR
2f9d0 55 50 54 5d 29 20 74 68 65 6e 20 74 68 65 0a 2a  UPT]) then the.*
2f9e0 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 69  * transaction mi
2f9f0 67 68 74 20 62 65 20 72 6f 6c 6c 65 64 20 62 61  ght be rolled ba
2fa00 63 6b 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ck automatically
2fa10 2e 20 20 54 68 65 20 6f 6e 6c 79 20 77 61 79 20  .  The only way 
2fa20 74 6f 0a 2a 2a 20 66 69 6e 64 20 6f 75 74 20 77  to.** find out w
2fa30 68 65 74 68 65 72 20 53 51 4c 69 74 65 20 61 75  hether SQLite au
2fa40 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 6f 6c 6c  tomatically roll
2fa50 65 64 20 62 61 63 6b 20 74 68 65 20 74 72 61 6e  ed back the tran
2fa60 73 61 63 74 69 6f 6e 20 61 66 74 65 72 0a 2a 2a  saction after.**
2fa70 20 61 6e 20 65 72 72 6f 72 20 69 73 20 74 6f 20   an error is to 
2fa80 75 73 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f  use this functio
2fa90 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74  n..**.** If anot
2faa0 68 65 72 20 74 68 72 65 61 64 20 63 68 61 6e 67  her thread chang
2fab0 65 73 20 74 68 65 20 61 75 74 6f 63 6f 6d 6d 69  es the autocommi
2fac0 74 20 73 74 61 74 75 73 20 6f 66 20 74 68 65 20  t status of the 
2fad0 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e  database.** conn
2fae0 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 69  ection while thi
2faf0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 75 6e  s routine is run
2fb00 6e 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 20 72  ning, then the r
2fb10 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69  eturn value.** i
2fb20 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a  s undefined..**.
2fb30 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
2fb40 20 5b 48 31 32 39 33 31 5d 20 5b 48 31 32 39 33   [H12931] [H1293
2fb50 32 5d 20 5b 48 31 32 39 33 33 5d 20 5b 48 31 32  2] [H12933] [H12
2fb60 39 33 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  934].*/.SQLITE_A
2fb70 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67  PI int sqlite3_g
2fb80 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 73 71  et_autocommit(sq
2fb90 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  lite3*);../*.** 
2fba0 43 41 50 49 33 52 45 46 3a 20 46 69 6e 64 20 54  CAPI3REF: Find T
2fbb0 68 65 20 44 61 74 61 62 61 73 65 20 48 61 6e 64  he Database Hand
2fbc0 6c 65 20 4f 66 20 41 20 50 72 65 70 61 72 65 64  le Of A Prepared
2fbd0 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 31   Statement {H131
2fbe0 32 30 7d 20 3c 53 36 30 36 30 30 3e 0a 2a 2a 0a  20} <S60600>.**.
2fbf0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 64  ** The sqlite3_d
2fc00 62 5f 68 61 6e 64 6c 65 20 69 6e 74 65 72 66 61  b_handle interfa
2fc10 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 5b  ce returns the [
2fc20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2fc30 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 74  ion] handle.** t
2fc40 6f 20 77 68 69 63 68 20 61 20 5b 70 72 65 70 61  o which a [prepa
2fc50 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 62  red statement] b
2fc60 65 6c 6f 6e 67 73 2e 20 20 54 68 65 20 5b 64 61  elongs.  The [da
2fc70 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2fc80 6e 5d 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 62  n].** returned b
2fc90 79 20 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e  y sqlite3_db_han
2fca0 64 6c 65 20 69 73 20 74 68 65 20 73 61 6d 65 20  dle is the same 
2fcb0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
2fcc0 74 69 6f 6e 5d 20 74 68 61 74 20 77 61 73 20 74  tion] that was t
2fcd0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
2fce0 74 0a 2a 2a 20 74 6f 20 74 68 65 20 5b 73 71 6c  t.** to the [sql
2fcf0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
2fd00 29 5d 20 63 61 6c 6c 20 28 6f 72 20 69 74 73 20  )] call (or its 
2fd10 76 61 72 69 61 6e 74 73 29 20 74 68 61 74 20 77  variants) that w
2fd20 61 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 72  as used to.** cr
2fd30 65 61 74 65 20 74 68 65 20 73 74 61 74 65 6d 65  eate the stateme
2fd40 6e 74 20 69 6e 20 74 68 65 20 66 69 72 73 74 20  nt in the first 
2fd50 70 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  place..**.** Req
2fd60 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 33 31  uirements: [H131
2fd70 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  23].*/.SQLITE_AP
2fd80 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74  I sqlite3 *sqlit
2fd90 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c  e3_db_handle(sql
2fda0 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a  ite3_stmt*);../*
2fdb0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69  .** CAPI3REF: Fi
2fdc0 6e 64 20 74 68 65 20 6e 65 78 74 20 70 72 65 70  nd the next prep
2fdd0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 7b  ared statement {
2fde0 48 31 33 31 34 30 7d 20 3c 53 36 30 36 30 30 3e  H13140} <S60600>
2fdf0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65  .**.** This inte
2fe00 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20  rface returns a 
2fe10 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e  pointer to the n
2fe20 65 78 74 20 5b 70 72 65 70 61 72 65 64 20 73 74  ext [prepared st
2fe30 61 74 65 6d 65 6e 74 5d 20 61 66 74 65 72 0a 2a  atement] after.*
2fe40 2a 20 70 53 74 6d 74 20 61 73 73 6f 63 69 61 74  * pStmt associat
2fe50 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 61 74  ed with the [dat
2fe60 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2fe70 5d 20 70 44 62 2e 20 20 49 66 20 70 53 74 6d 74  ] pDb.  If pStmt
2fe80 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 65 6e   is NULL.** then
2fe90 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   this interface 
2fea0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
2feb0 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 70  r to the first p
2fec0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
2fed0 74 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20  t.** associated 
2fee0 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73  with the databas
2fef0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 44 62  e connection pDb
2ff00 2e 20 20 49 66 20 6e 6f 20 70 72 65 70 61 72 65  .  If no prepare
2ff10 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 73  d statement.** s
2ff20 61 74 69 73 66 69 65 73 20 74 68 65 20 63 6f 6e  atisfies the con
2ff30 64 69 74 69 6f 6e 73 20 6f 66 20 74 68 69 73 20  ditions of this 
2ff40 72 6f 75 74 69 6e 65 2c 20 69 74 20 72 65 74 75  routine, it retu
2ff50 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20  rns NULL..**.** 
2ff60 54 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  The [database co
2ff70 6e 6e 65 63 74 69 6f 6e 5d 20 70 6f 69 6e 74 65  nnection] pointe
2ff80 72 20 44 20 69 6e 20 61 20 63 61 6c 6c 20 74 6f  r D in a call to
2ff90 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6e 65 78  .** [sqlite3_nex
2ffa0 74 5f 73 74 6d 74 28 44 2c 53 29 5d 20 6d 75 73  t_stmt(D,S)] mus
2ffb0 74 20 72 65 66 65 72 20 74 6f 20 61 6e 20 6f 70  t refer to an op
2ffc0 65 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63  en database.** c
2ffd0 6f 6e 6e 65 63 74 69 6f 6e 20 61 6e 64 20 69 6e  onnection and in
2ffe0 20 70 61 72 74 69 63 75 6c 61 72 20 6d 75 73 74   particular must
2fff0 20 6e 6f 74 20 62 65 20 61 20 4e 55 4c 4c 20 70   not be a NULL p
30000 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65  ointer..**.** Re
30010 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 33  quirements: [H13
30020 31 34 33 5d 20 5b 48 31 33 31 34 36 5d 20 5b 48  143] [H13146] [H
30030 31 33 31 34 39 5d 20 5b 48 31 33 31 35 32 5d 0a  13149] [H13152].
30040 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  */.SQLITE_API sq
30050 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 71 6c 69  lite3_stmt *sqli
30060 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 73 71  te3_next_stmt(sq
30070 6c 69 74 65 33 20 2a 70 44 62 2c 20 73 71 6c 69  lite3 *pDb, sqli
30080 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29  te3_stmt *pStmt)
30090 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
300a0 46 3a 20 43 6f 6d 6d 69 74 20 41 6e 64 20 52 6f  F: Commit And Ro
300b0 6c 6c 62 61 63 6b 20 4e 6f 74 69 66 69 63 61 74  llback Notificat
300c0 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b 48  ion Callbacks {H
300d0 31 32 39 35 30 7d 20 3c 53 36 30 34 30 30 3e 0a  12950} <S60400>.
300e0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
300f0 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 20  3_commit_hook() 
30100 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74  interface regist
30110 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 0a 2a  ers a callback.*
30120 2a 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65  * function to be
30130 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65   invoked wheneve
30140 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  r a transaction 
30150 69 73 20 5b 43 4f 4d 4d 49 54 20 7c 20 63 6f 6d  is [COMMIT | com
30160 6d 69 74 74 65 64 5d 2e 0a 2a 2a 20 41 6e 79 20  mitted]..** Any 
30170 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 79 20  callback set by 
30180 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20  a previous call 
30190 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69  to sqlite3_commi
301a0 74 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66 6f 72 20  t_hook().** for 
301b0 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
301c0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
301d0 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a 20 54  overridden..** T
301e0 68 65 20 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62  he sqlite3_rollb
301f0 61 63 6b 5f 68 6f 6f 6b 28 29 20 69 6e 74 65 72  ack_hook() inter
30200 66 61 63 65 20 72 65 67 69 73 74 65 72 73 20 61  face registers a
30210 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e   callback.** fun
30220 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e 76 6f  ction to be invo
30230 6b 65 64 20 77 68 65 6e 65 76 65 72 20 61 20 74  ked whenever a t
30240 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 5b 52  ransaction is [R
30250 4f 4c 4c 42 41 43 4b 20 7c 20 72 6f 6c 6c 65 64  OLLBACK | rolled
30260 20 62 61 63 6b 5d 2e 0a 2a 2a 20 41 6e 79 20 63   back]..** Any c
30270 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 79 20 61  allback set by a
30280 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 74   previous call t
30290 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74  o sqlite3_commit
302a0 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66 6f 72 20 74  _hook().** for t
302b0 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
302c0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f   connection is o
302d0 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a 20 54 68  verridden..** Th
302e0 65 20 70 41 72 67 20 61 72 67 75 6d 65 6e 74 20  e pArg argument 
302f0 69 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67  is passed throug
30300 68 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63  h to the callbac
30310 6b 2e 0a 2a 2a 20 49 66 20 74 68 65 20 63 61 6c  k..** If the cal
30320 6c 62 61 63 6b 20 6f 6e 20 61 20 63 6f 6d 6d 69  lback on a commi
30330 74 20 68 6f 6f 6b 20 66 75 6e 63 74 69 6f 6e 20  t hook function 
30340 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f  returns non-zero
30350 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 6f  ,.** then the co
30360 6d 6d 69 74 20 69 73 20 63 6f 6e 76 65 72 74 65  mmit is converte
30370 64 20 69 6e 74 6f 20 61 20 72 6f 6c 6c 62 61 63  d into a rollbac
30380 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74  k..**.** If anot
30390 68 65 72 20 66 75 6e 63 74 69 6f 6e 20 77 61 73  her function was
303a0 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69   previously regi
303b0 73 74 65 72 65 64 2c 20 69 74 73 0a 2a 2a 20 70  stered, its.** p
303c0 41 72 67 20 76 61 6c 75 65 20 69 73 20 72 65 74  Arg value is ret
303d0 75 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69 73  urned.  Otherwis
303e0 65 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e  e NULL is return
303f0 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61  ed..**.** The ca
30400 6c 6c 62 61 63 6b 20 69 6d 70 6c 65 6d 65 6e 74  llback implement
30410 61 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 64  ation must not d
30420 6f 20 61 6e 79 74 68 69 6e 67 20 74 68 61 74 20  o anything that 
30430 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74  will modify.** t
30440 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
30450 65 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f  ection that invo
30460 6b 65 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  ked the callback
30470 2e 20 20 41 6e 79 20 61 63 74 69 6f 6e 73 0a 2a  .  Any actions.*
30480 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20  * to modify the 
30490 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
304a0 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 65 66 65  ion must be defe
304b0 72 72 65 64 20 75 6e 74 69 6c 20 61 66 74 65 72  rred until after
304c0 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69   the.** completi
304d0 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74  on of the [sqlit
304e0 65 33 5f 73 74 65 70 28 29 5d 20 63 61 6c 6c 20  e3_step()] call 
304f0 74 68 61 74 20 74 72 69 67 67 65 72 65 64 20 74  that triggered t
30500 68 65 20 63 6f 6d 6d 69 74 0a 2a 2a 20 6f 72 20  he commit.** or 
30510 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f 6b 20 69 6e  rollback hook in
30520 20 74 68 65 20 66 69 72 73 74 20 70 6c 61 63 65   the first place
30530 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b  ..** Note that [
30540 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
30550 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  v2()] and [sqlit
30560 65 33 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20  e3_step()] both 
30570 6d 6f 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20  modify their.** 
30580 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
30590 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61  ions for the mea
305a0 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22  ning of "modify"
305b0 20 69 6e 20 74 68 69 73 20 70 61 72 61 67 72 61   in this paragra
305c0 70 68 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74  ph..**.** Regist
305d0 65 72 69 6e 67 20 61 20 4e 55 4c 4c 20 66 75 6e  ering a NULL fun
305e0 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 74  ction disables t
305f0 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a  he callback..**.
30600 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 6f 6d 6d  ** When the comm
30610 69 74 20 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b  it hook callback
30620 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
30630 20 7a 65 72 6f 2c 20 74 68 65 20 5b 43 4f 4d 4d   zero, the [COMM
30640 49 54 5d 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e  IT].** operation
30650 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 63   is allowed to c
30660 6f 6e 74 69 6e 75 65 20 6e 6f 72 6d 61 6c 6c 79  ontinue normally
30670 2e 20 20 49 66 20 74 68 65 20 63 6f 6d 6d 69 74  .  If the commit
30680 20 68 6f 6f 6b 0a 2a 2a 20 72 65 74 75 72 6e 73   hook.** returns
30690 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20   non-zero, then 
306a0 74 68 65 20 5b 43 4f 4d 4d 49 54 5d 20 69 73 20  the [COMMIT] is 
306b0 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61  converted into a
306c0 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a 2a 20   [ROLLBACK]..** 
306d0 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f  The rollback hoo
306e0 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20  k is invoked on 
306f0 61 20 72 6f 6c 6c 62 61 63 6b 20 74 68 61 74 20  a rollback that 
30700 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 61 20 63  results from a c
30710 6f 6d 6d 69 74 0a 2a 2a 20 68 6f 6f 6b 20 72 65  ommit.** hook re
30720 74 75 72 6e 69 6e 67 20 6e 6f 6e 2d 7a 65 72 6f  turning non-zero
30730 2c 20 6a 75 73 74 20 61 73 20 69 74 20 77 6f 75  , just as it wou
30740 6c 64 20 62 65 20 77 69 74 68 20 61 6e 79 20 6f  ld be with any o
30750 74 68 65 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a  ther rollback..*
30760 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 72  *.** For the pur
30770 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 41 50  poses of this AP
30780 49 2c 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  I, a transaction
30790 20 69 73 20 73 61 69 64 20 74 6f 20 68 61 76 65   is said to have
307a0 20 62 65 65 6e 0a 2a 2a 20 72 6f 6c 6c 65 64 20   been.** rolled 
307b0 62 61 63 6b 20 69 66 20 61 6e 20 65 78 70 6c 69  back if an expli
307c0 63 69 74 20 22 52 4f 4c 4c 42 41 43 4b 22 20 73  cit "ROLLBACK" s
307d0 74 61 74 65 6d 65 6e 74 20 69 73 20 65 78 65 63  tatement is exec
307e0 75 74 65 64 2c 20 6f 72 0a 2a 2a 20 61 6e 20 65  uted, or.** an e
307f0 72 72 6f 72 20 6f 72 20 63 6f 6e 73 74 72 61 69  rror or constrai
30800 6e 74 20 63 61 75 73 65 73 20 61 6e 20 69 6d 70  nt causes an imp
30810 6c 69 63 69 74 20 72 6f 6c 6c 62 61 63 6b 20 74  licit rollback t
30820 6f 20 6f 63 63 75 72 2e 0a 2a 2a 20 54 68 65 20  o occur..** The 
30830 72 6f 6c 6c 62 61 63 6b 20 63 61 6c 6c 62 61 63  rollback callbac
30840 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64  k is not invoked
30850 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f   if a transactio
30860 6e 20 69 73 0a 2a 2a 20 61 75 74 6f 6d 61 74 69  n is.** automati
30870 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63  cally rolled bac
30880 6b 20 62 65 63 61 75 73 65 20 74 68 65 20 64 61  k because the da
30890 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
308a0 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 20  n is closed..** 
308b0 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6c  The rollback cal
308c0 6c 62 61 63 6b 20 69 73 20 6e 6f 74 20 69 6e 76  lback is not inv
308d0 6f 6b 65 64 20 69 66 20 61 20 74 72 61 6e 73 61  oked if a transa
308e0 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 72 6f 6c 6c  ction is.** roll
308f0 65 64 20 62 61 63 6b 20 62 65 63 61 75 73 65 20  ed back because 
30900 61 20 63 6f 6d 6d 69 74 20 63 61 6c 6c 62 61 63  a commit callbac
30910 6b 20 72 65 74 75 72 6e 65 64 20 6e 6f 6e 2d 7a  k returned non-z
30920 65 72 6f 2e 0a 2a 2a 20 3c 74 6f 64 6f 3e 20 43  ero..** <todo> C
30930 68 65 63 6b 20 6f 6e 20 74 68 69 73 20 3c 2f 74  heck on this </t
30940 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  odo>.**.** See a
30950 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33  lso the [sqlite3
30960 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29 5d 20  _update_hook()] 
30970 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a  interface..**.**
30980 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
30990 2a 20 5b 48 31 32 39 35 31 5d 20 5b 48 31 32 39  * [H12951] [H129
309a0 35 32 5d 20 5b 48 31 32 39 35 33 5d 20 5b 48 31  52] [H12953] [H1
309b0 32 39 35 34 5d 20 5b 48 31 32 39 35 35 5d 0a 2a  2954] [H12955].*
309c0 2a 20 5b 48 31 32 39 36 31 5d 20 5b 48 31 32 39  * [H12961] [H129
309d0 36 32 5d 20 5b 48 31 32 39 36 33 5d 20 5b 48 31  62] [H12963] [H1
309e0 32 39 36 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  2964].*/.SQLITE_
309f0 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  API void *sqlite
30a00 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 73 71  3_commit_hook(sq
30a10 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a 29 28 76  lite3*, int(*)(v
30a20 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 53  oid*), void*);.S
30a30 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
30a40 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b  sqlite3_rollback
30a50 5f 68 6f 6f 6b 28 73 71 6c 69 74 65 33 2a 2c 20  _hook(sqlite3*, 
30a60 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a 29 2c  void(*)(void *),
30a70 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20   void*);../*.** 
30a80 43 41 50 49 33 52 45 46 3a 20 44 61 74 61 20 43  CAPI3REF: Data C
30a90 68 61 6e 67 65 20 4e 6f 74 69 66 69 63 61 74 69  hange Notificati
30aa0 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b 48 31  on Callbacks {H1
30ab0 32 39 37 30 7d 20 3c 53 36 30 34 30 30 3e 0a 2a  2970} <S60400>.*
30ac0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
30ad0 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29 20 69  _update_hook() i
30ae0 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74 65  nterface registe
30af0 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75  rs a callback fu
30b00 6e 63 74 69 6f 6e 0a 2a 2a 20 77 69 74 68 20 74  nction.** with t
30b10 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
30b20 6e 65 63 74 69 6f 6e 5d 20 69 64 65 6e 74 69 66  nection] identif
30b30 69 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74  ied by the first
30b40 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20   argument.** to 
30b50 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65  be invoked whene
30b60 76 65 72 20 61 20 72 6f 77 20 69 73 20 75 70 64  ver a row is upd
30b70 61 74 65 64 2c 20 69 6e 73 65 72 74 65 64 20 6f  ated, inserted o
30b80 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 41 6e  r deleted..** An
30b90 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62  y callback set b
30ba0 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c  y a previous cal
30bb0 6c 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  l to this functi
30bc0 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61  on.** for the sa
30bd0 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
30be0 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69  ection is overri
30bf0 64 64 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  dden..**.** The 
30c00 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
30c10 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
30c20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  the function to 
30c30 69 6e 76 6f 6b 65 20 77 68 65 6e 20 61 0a 2a 2a  invoke when a.**
30c40 20 72 6f 77 20 69 73 20 75 70 64 61 74 65 64 2c   row is updated,
30c50 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c   inserted or del
30c60 65 74 65 64 2e 0a 2a 2a 20 54 68 65 20 66 69 72  eted..** The fir
30c70 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  st argument to t
30c80 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61  he callback is a
30c90 20 63 6f 70 79 20 6f 66 20 74 68 65 20 74 68 69   copy of the thi
30ca0 72 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74  rd argument.** t
30cb0 6f 20 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65  o sqlite3_update
30cc0 5f 68 6f 6f 6b 28 29 2e 0a 2a 2a 20 54 68 65 20  _hook()..** The 
30cd0 73 65 63 6f 6e 64 20 63 61 6c 6c 62 61 63 6b 20  second callback 
30ce0 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e 65 20  argument is one 
30cf0 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e 53 45 52  of [SQLITE_INSER
30d00 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 44 45 4c 45  T], [SQLITE_DELE
30d10 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49  TE],.** or [SQLI
30d20 54 45 5f 55 50 44 41 54 45 5d 2c 20 64 65 70 65  TE_UPDATE], depe
30d30 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f 70 65  nding on the ope
30d40 72 61 74 69 6f 6e 20 74 68 61 74 20 63 61 75 73  ration that caus
30d50 65 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a  ed the callback.
30d60 2a 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64  ** to be invoked
30d70 2e 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61  ..** The third a
30d80 6e 64 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65  nd fourth argume
30d90 6e 74 73 20 74 6f 20 74 68 65 20 63 61 6c 6c 62  nts to the callb
30da0 61 63 6b 20 63 6f 6e 74 61 69 6e 20 70 6f 69 6e  ack contain poin
30db0 74 65 72 73 20 74 6f 20 74 68 65 0a 2a 2a 20 64  ters to the.** d
30dc0 61 74 61 62 61 73 65 20 61 6e 64 20 74 61 62 6c  atabase and tabl
30dd0 65 20 6e 61 6d 65 20 63 6f 6e 74 61 69 6e 69 6e  e name containin
30de0 67 20 74 68 65 20 61 66 66 65 63 74 65 64 20 72  g the affected r
30df0 6f 77 2e 0a 2a 2a 20 54 68 65 20 66 69 6e 61 6c  ow..** The final
30e00 20 63 61 6c 6c 62 61 63 6b 20 70 61 72 61 6d 65   callback parame
30e10 74 65 72 20 69 73 20 74 68 65 20 5b 72 6f 77 69  ter is the [rowi
30e20 64 5d 20 6f 66 20 74 68 65 20 72 6f 77 2e 0a 2a  d] of the row..*
30e30 2a 20 49 6e 20 74 68 65 20 63 61 73 65 20 6f 66  * In the case of
30e40 20 61 6e 20 75 70 64 61 74 65 2c 20 74 68 69 73   an update, this
30e50 20 69 73 20 74 68 65 20 5b 72 6f 77 69 64 5d 20   is the [rowid] 
30e60 61 66 74 65 72 20 74 68 65 20 75 70 64 61 74 65  after the update
30e70 20 74 61 6b 65 73 20 70 6c 61 63 65 2e 0a 2a 2a   takes place..**
30e80 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65 20 68  .** The update h
30e90 6f 6f 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b  ook is not invok
30ea0 65 64 20 77 68 65 6e 20 69 6e 74 65 72 6e 61 6c  ed when internal
30eb0 20 73 79 73 74 65 6d 20 74 61 62 6c 65 73 20 61   system tables a
30ec0 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 20 28  re.** modified (
30ed0 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d 61 73 74  i.e. sqlite_mast
30ee0 65 72 20 61 6e 64 20 73 71 6c 69 74 65 5f 73 65  er and sqlite_se
30ef0 71 75 65 6e 63 65 29 2e 0a 2a 2a 0a 2a 2a 20 49  quence)..**.** I
30f00 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 69 6d  n the current im
30f10 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 74 68  plementation, th
30f20 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 0a 2a 2a  e update hook.**
30f30 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20   is not invoked 
30f40 77 68 65 6e 20 64 75 70 6c 69 63 61 74 69 6f 6e  when duplication
30f50 20 72 6f 77 73 20 61 72 65 20 64 65 6c 65 74 65   rows are delete
30f60 64 20 62 65 63 61 75 73 65 20 6f 66 20 61 6e 0a  d because of an.
30f70 2a 2a 20 5b 4f 4e 20 43 4f 4e 46 4c 49 43 54 20  ** [ON CONFLICT 
30f80 7c 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 52 45  | ON CONFLICT RE
30f90 50 4c 41 43 45 5d 20 63 6c 61 75 73 65 2e 20 20  PLACE] clause.  
30fa0 4e 6f 72 20 69 73 20 74 68 65 20 75 70 64 61 74  Nor is the updat
30fb0 65 20 68 6f 6f 6b 0a 2a 2a 20 69 6e 76 6f 6b 65  e hook.** invoke
30fc0 64 20 77 68 65 6e 20 72 6f 77 73 20 61 72 65 20  d when rows are 
30fd0 64 65 6c 65 74 65 64 20 75 73 69 6e 67 20 74 68  deleted using th
30fe0 65 20 5b 74 72 75 6e 63 61 74 65 20 6f 70 74 69  e [truncate opti
30ff0 6d 69 7a 61 74 69 6f 6e 5d 2e 0a 2a 2a 20 54 68  mization]..** Th
31000 65 20 65 78 63 65 70 74 69 6f 6e 73 20 64 65 66  e exceptions def
31010 69 6e 65 64 20 69 6e 20 74 68 69 73 20 70 61 72  ined in this par
31020 61 67 72 61 70 68 20 6d 69 67 68 74 20 63 68 61  agraph might cha
31030 6e 67 65 20 69 6e 20 61 20 66 75 74 75 72 65 0a  nge in a future.
31040 2a 2a 20 72 65 6c 65 61 73 65 20 6f 66 20 53 51  ** release of SQ
31050 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  Lite..**.** The 
31060 75 70 64 61 74 65 20 68 6f 6f 6b 20 69 6d 70 6c  update hook impl
31070 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 73 74 20  ementation must 
31080 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e 67 20  not do anything 
31090 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66 79  that will modify
310a0 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
310b0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74   connection that
310c0 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 75 70 64   invoked the upd
310d0 61 74 65 20 68 6f 6f 6b 2e 20 20 41 6e 79 20 61  ate hook.  Any a
310e0 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 6d 6f 64  ctions.** to mod
310f0 69 66 79 20 74 68 65 20 64 61 74 61 62 61 73 65  ify the database
31100 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 73 74   connection must
31110 20 62 65 20 64 65 66 65 72 72 65 64 20 75 6e 74   be deferred unt
31120 69 6c 20 61 66 74 65 72 20 74 68 65 0a 2a 2a 20  il after the.** 
31130 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 20 74 68  completion of th
31140 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  e [sqlite3_step(
31150 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69  )] call that tri
31160 67 67 65 72 65 64 20 74 68 65 20 75 70 64 61 74  ggered the updat
31170 65 20 68 6f 6f 6b 2e 0a 2a 2a 20 4e 6f 74 65 20  e hook..** Note 
31180 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72  that [sqlite3_pr
31190 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20  epare_v2()] and 
311a0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
311b0 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65   both modify the
311c0 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  ir.** database c
311d0 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74  onnections for t
311e0 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d  he meaning of "m
311f0 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70  odify" in this p
31200 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20  aragraph..**.** 
31210 49 66 20 61 6e 6f 74 68 65 72 20 66 75 6e 63 74  If another funct
31220 69 6f 6e 20 77 61 73 20 70 72 65 76 69 6f 75 73  ion was previous
31230 6c 79 20 72 65 67 69 73 74 65 72 65 64 2c 20 69  ly registered, i
31240 74 73 20 70 41 72 67 20 76 61 6c 75 65 0a 2a 2a  ts pArg value.**
31250 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f   is returned.  O
31260 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 20 69 73  therwise NULL is
31270 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
31280 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 5b 73   See also the [s
31290 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f  qlite3_commit_ho
312a0 6f 6b 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  ok()] and [sqlit
312b0 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b  e3_rollback_hook
312c0 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  ()].** interface
312d0 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  s..**.** Require
312e0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 39 37  ments:.** [H1297
312f0 31 5d 20 5b 48 31 32 39 37 33 5d 20 5b 48 31 32  1] [H12973] [H12
31300 39 37 35 5d 20 5b 48 31 32 39 37 37 5d 20 5b 48  975] [H12977] [H
31310 31 32 39 37 39 5d 20 5b 48 31 32 39 38 31 5d 20  12979] [H12981] 
31320 5b 48 31 32 39 38 33 5d 20 5b 48 31 32 39 38 36  [H12983] [H12986
31330 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
31340 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 70  void *sqlite3_up
31350 64 61 74 65 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c  date_hook(.  sql
31360 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 28 2a  ite3*, .  void(*
31370 29 28 76 6f 69 64 20 2a 2c 69 6e 74 20 2c 63 68  )(void *,int ,ch
31380 61 72 20 63 6f 6e 73 74 20 2a 2c 63 68 61 72 20  ar const *,char 
31390 63 6f 6e 73 74 20 2a 2c 73 71 6c 69 74 65 33 5f  const *,sqlite3_
313a0 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69 64 2a 0a  int64),.  void*.
313b0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
313c0 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72 20 44 69  EF: Enable Or Di
313d0 73 61 62 6c 65 20 53 68 61 72 65 64 20 50 61 67  sable Shared Pag
313e0 65 72 20 43 61 63 68 65 20 7b 48 31 30 33 33 30  er Cache {H10330
313f0 7d 20 3c 53 33 30 39 30 30 3e 0a 2a 2a 20 4b 45  } <S30900>.** KE
31400 59 57 4f 52 44 53 3a 20 7b 73 68 61 72 65 64 20  YWORDS: {shared 
31410 63 61 63 68 65 7d 0a 2a 2a 0a 2a 2a 20 54 68 69  cache}.**.** Thi
31420 73 20 72 6f 75 74 69 6e 65 20 65 6e 61 62 6c 65  s routine enable
31430 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20 74 68  s or disables th
31440 65 20 73 68 61 72 69 6e 67 20 6f 66 20 74 68 65  e sharing of the
31450 20 64 61 74 61 62 61 73 65 20 63 61 63 68 65 0a   database cache.
31460 2a 2a 20 61 6e 64 20 73 63 68 65 6d 61 20 64 61  ** and schema da
31470 74 61 20 73 74 72 75 63 74 75 72 65 73 20 62 65  ta structures be
31480 74 77 65 65 6e 20 5b 64 61 74 61 62 61 73 65 20  tween [database 
31490 63 6f 6e 6e 65 63 74 69 6f 6e 20 7c 20 63 6f 6e  connection | con
314a0 6e 65 63 74 69 6f 6e 73 5d 0a 2a 2a 20 74 6f 20  nections].** to 
314b0 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
314c0 65 2e 20 53 68 61 72 69 6e 67 20 69 73 20 65 6e  e. Sharing is en
314d0 61 62 6c 65 64 20 69 66 20 74 68 65 20 61 72 67  abled if the arg
314e0 75 6d 65 6e 74 20 69 73 20 74 72 75 65 0a 2a 2a  ument is true.**
314f0 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20 69 66   and disabled if
31500 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73   the argument is
31510 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 43 61   false..**.** Ca
31520 63 68 65 20 73 68 61 72 69 6e 67 20 69 73 20 65  che sharing is e
31530 6e 61 62 6c 65 64 20 61 6e 64 20 64 69 73 61 62  nabled and disab
31540 6c 65 64 20 66 6f 72 20 61 6e 20 65 6e 74 69 72  led for an entir
31550 65 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 20 54 68  e process..** Th
31560 69 73 20 69 73 20 61 20 63 68 61 6e 67 65 20 61  is is a change a
31570 73 20 6f 66 20 53 51 4c 69 74 65 20 76 65 72 73  s of SQLite vers
31580 69 6f 6e 20 33 2e 35 2e 30 2e 20 49 6e 20 70 72  ion 3.5.0. In pr
31590 69 6f 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  ior versions of 
315a0 53 51 4c 69 74 65 2c 0a 2a 2a 20 73 68 61 72 69  SQLite,.** shari
315b0 6e 67 20 77 61 73 20 65 6e 61 62 6c 65 64 20 6f  ng was enabled o
315c0 72 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 65  r disabled for e
315d0 61 63 68 20 74 68 72 65 61 64 20 73 65 70 61 72  ach thread separ
315e0 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ately..**.** The
315f0 20 63 61 63 68 65 20 73 68 61 72 69 6e 67 20 6d   cache sharing m
31600 6f 64 65 20 73 65 74 20 62 79 20 74 68 69 73 20  ode set by this 
31610 69 6e 74 65 72 66 61 63 65 20 65 66 66 65 63 74  interface effect
31620 73 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74  s all subsequent
31630 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71  .** calls to [sq
31640 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b  lite3_open()], [
31650 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
31660 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  )], and [sqlite3
31670 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 20 45  _open16()]..** E
31680 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 73 65  xisting database
31690 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 6f 6e   connections con
316a0 74 69 6e 75 65 20 75 73 65 20 74 68 65 20 73 68  tinue use the sh
316b0 61 72 69 6e 67 20 6d 6f 64 65 0a 2a 2a 20 74 68  aring mode.** th
316c0 61 74 20 77 61 73 20 69 6e 20 65 66 66 65 63 74  at was in effect
316d0 20 61 74 20 74 68 65 20 74 69 6d 65 20 74 68 65   at the time the
316e0 79 20 77 65 72 65 20 6f 70 65 6e 65 64 2e 0a 2a  y were opened..*
316f0 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 61 62  *.** Virtual tab
31700 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 20 75 73  les cannot be us
31710 65 64 20 77 69 74 68 20 61 20 73 68 61 72 65 64  ed with a shared
31720 20 63 61 63 68 65 2e 20 20 57 68 65 6e 20 73 68   cache.  When sh
31730 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 69 73  ared.** cache is
31740 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 20 5b 73   enabled, the [s
31750 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f  qlite3_create_mo
31760 64 75 6c 65 28 29 5d 20 41 50 49 20 75 73 65 64  dule()] API used
31770 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a 2a 20   to register.** 
31780 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 77  virtual tables w
31790 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 74 75 72  ill always retur
317a0 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a  n an error..**.*
317b0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
317c0 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f  eturns [SQLITE_O
317d0 4b 5d 20 69 66 20 73 68 61 72 65 64 20 63 61 63  K] if shared cac
317e0 68 65 20 77 61 73 20 65 6e 61 62 6c 65 64 20 6f  he was enabled o
317f0 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 73 75  r disabled.** su
31800 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 41 6e 20  ccessfully.  An 
31810 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20  [error code] is 
31820 72 65 74 75 72 6e 65 64 20 6f 74 68 65 72 77 69  returned otherwi
31830 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65 64  se..**.** Shared
31840 20 63 61 63 68 65 20 69 73 20 64 69 73 61 62 6c   cache is disabl
31850 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e 20 42  ed by default. B
31860 75 74 20 74 68 69 73 20 6d 69 67 68 74 20 63 68  ut this might ch
31870 61 6e 67 65 20 69 6e 0a 2a 2a 20 66 75 74 75 72  ange in.** futur
31880 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51  e releases of SQ
31890 4c 69 74 65 2e 20 20 41 70 70 6c 69 63 61 74 69  Lite.  Applicati
318a0 6f 6e 73 20 74 68 61 74 20 63 61 72 65 20 61 62  ons that care ab
318b0 6f 75 74 20 73 68 61 72 65 64 0a 2a 2a 20 63 61  out shared.** ca
318c0 63 68 65 20 73 65 74 74 69 6e 67 20 73 68 6f 75  che setting shou
318d0 6c 64 20 73 65 74 20 69 74 20 65 78 70 6c 69 63  ld set it explic
318e0 69 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  itly..**.** See 
318f0 41 6c 73 6f 3a 20 20 5b 53 51 4c 69 74 65 20 53  Also:  [SQLite S
31900 68 61 72 65 64 2d 43 61 63 68 65 20 4d 6f 64 65  hared-Cache Mode
31910 5d 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ].**.** Requirem
31920 65 6e 74 73 3a 20 5b 48 31 30 33 33 31 5d 20 5b  ents: [H10331] [
31930 48 31 30 33 33 36 5d 20 5b 48 31 30 33 33 37 5d  H10336] [H10337]
31940 20 5b 48 31 30 33 33 39 5d 0a 2a 2f 0a 53 51 4c   [H10339].*/.SQL
31950 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
31960 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65  te3_enable_share
31970 64 5f 63 61 63 68 65 28 69 6e 74 29 3b 0a 0a 2f  d_cache(int);../
31980 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41  *.** CAPI3REF: A
31990 74 74 65 6d 70 74 20 54 6f 20 46 72 65 65 20 48  ttempt To Free H
319a0 65 61 70 20 4d 65 6d 6f 72 79 20 7b 48 31 37 33  eap Memory {H173
319b0 34 30 7d 20 3c 53 33 30 32 32 30 3e 0a 2a 2a 0a  40} <S30220>.**.
319c0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72  ** The sqlite3_r
319d0 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20  elease_memory() 
319e0 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d 70  interface attemp
319f0 74 73 20 74 6f 20 66 72 65 65 20 4e 20 62 79 74  ts to free N byt
31a00 65 73 0a 2a 2a 20 6f 66 20 68 65 61 70 20 6d 65  es.** of heap me
31a10 6d 6f 72 79 20 62 79 20 64 65 61 6c 6c 6f 63 61  mory by dealloca
31a20 74 69 6e 67 20 6e 6f 6e 2d 65 73 73 65 6e 74 69  ting non-essenti
31a30 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  al memory alloca
31a40 74 69 6f 6e 73 0a 2a 2a 20 68 65 6c 64 20 62 79  tions.** held by
31a50 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c 69   the database li
31a60 62 72 61 72 79 2e 20 7b 45 4e 44 7d 20 20 4d 65  brary. {END}  Me
31a70 6d 6f 72 79 20 75 73 65 64 20 74 6f 20 63 61 63  mory used to cac
31a80 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 70  he database.** p
31a90 61 67 65 73 20 74 6f 20 69 6d 70 72 6f 76 65 20  ages to improve 
31aa0 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 73 20 61  performance is a
31ab0 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 6e 6f 6e  n example of non
31ac0 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d 6f 72  -essential memor
31ad0 79 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65  y..** sqlite3_re
31ae0 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 72  lease_memory() r
31af0 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65  eturns the numbe
31b00 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 75 61  r of bytes actua
31b10 6c 6c 79 20 66 72 65 65 64 2c 0a 2a 2a 20 77 68  lly freed,.** wh
31b20 69 63 68 20 6d 69 67 68 74 20 62 65 20 6d 6f 72  ich might be mor
31b30 65 20 6f 72 20 6c 65 73 73 20 74 68 61 6e 20 74  e or less than t
31b40 68 65 20 61 6d 6f 75 6e 74 20 72 65 71 75 65 73  he amount reques
31b50 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ted..**.** Requi
31b60 72 65 6d 65 6e 74 73 3a 20 5b 48 31 37 33 34 31  rements: [H17341
31b70 5d 20 5b 48 31 37 33 34 32 5d 0a 2a 2f 0a 53 51  ] [H17342].*/.SQ
31b80 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
31b90 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d  ite3_release_mem
31ba0 6f 72 79 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a  ory(int);../*.**
31bb0 20 43 41 50 49 33 52 45 46 3a 20 49 6d 70 6f 73   CAPI3REF: Impos
31bc0 65 20 41 20 4c 69 6d 69 74 20 4f 6e 20 48 65 61  e A Limit On Hea
31bd0 70 20 53 69 7a 65 20 7b 48 31 37 33 35 30 7d 20  p Size {H17350} 
31be0 3c 53 33 30 32 32 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S30220>.**.** T
31bf0 68 65 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f  he sqlite3_soft_
31c00 68 65 61 70 5f 6c 69 6d 69 74 28 29 20 69 6e 74  heap_limit() int
31c10 65 72 66 61 63 65 20 70 6c 61 63 65 73 20 61 20  erface places a 
31c20 22 73 6f 66 74 22 20 6c 69 6d 69 74 0a 2a 2a 20  "soft" limit.** 
31c30 6f 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66  on the amount of
31c40 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 74 68 61   heap memory tha
31c50 74 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63 61 74  t may be allocat
31c60 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a  ed by SQLite..**
31c70 20 49 66 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20   If an internal 
31c80 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 72 65  allocation is re
31c90 71 75 65 73 74 65 64 20 74 68 61 74 20 77 6f 75  quested that wou
31ca0 6c 64 20 65 78 63 65 65 64 20 74 68 65 0a 2a 2a  ld exceed the.**
31cb0 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74   soft heap limit
31cc0 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65 61  , [sqlite3_relea
31cd0 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20 69 73 20  se_memory()] is 
31ce0 69 6e 76 6f 6b 65 64 20 6f 6e 65 20 6f 72 0a 2a  invoked one or.*
31cf0 2a 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f 20  * more times to 
31d00 66 72 65 65 20 75 70 20 73 6f 6d 65 20 73 70 61  free up some spa
31d10 63 65 20 62 65 66 6f 72 65 20 74 68 65 20 61 6c  ce before the al
31d20 6c 6f 63 61 74 69 6f 6e 20 69 73 20 70 65 72 66  location is perf
31d30 6f 72 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ormed..**.** The
31d40 20 6c 69 6d 69 74 20 69 73 20 63 61 6c 6c 65 64   limit is called
31d50 20 22 73 6f 66 74 22 2c 20 62 65 63 61 75 73 65   "soft", because
31d60 20 69 66 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c   if [sqlite3_rel
31d70 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 0a 2a  ease_memory()].*
31d80 2a 20 63 61 6e 6e 6f 74 20 66 72 65 65 20 73 75  * cannot free su
31d90 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f 72 79 20  fficient memory 
31da0 74 6f 20 70 72 65 76 65 6e 74 20 74 68 65 20 6c  to prevent the l
31db0 69 6d 69 74 20 66 72 6f 6d 20 62 65 69 6e 67 20  imit from being 
31dc0 65 78 63 65 65 64 65 64 2c 0a 2a 2a 20 74 68 65  exceeded,.** the
31dd0 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63   memory is alloc
31de0 61 74 65 64 20 61 6e 79 77 61 79 20 61 6e 64 20  ated anyway and 
31df0 74 68 65 20 63 75 72 72 65 6e 74 20 6f 70 65 72  the current oper
31e00 61 74 69 6f 6e 20 70 72 6f 63 65 65 64 73 2e 0a  ation proceeds..
31e10 2a 2a 0a 2a 2a 20 41 20 6e 65 67 61 74 69 76 65  **.** A negative
31e20 20 6f 72 20 7a 65 72 6f 20 76 61 6c 75 65 20 66   or zero value f
31e30 6f 72 20 4e 20 6d 65 61 6e 73 20 74 68 61 74 20  or N means that 
31e40 74 68 65 72 65 20 69 73 20 6e 6f 20 73 6f 66 74  there is no soft
31e50 20 68 65 61 70 20 6c 69 6d 69 74 20 61 6e 64 0a   heap limit and.
31e60 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65  ** [sqlite3_rele
31e70 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20 77 69  ase_memory()] wi
31e80 6c 6c 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65  ll only be calle
31e90 64 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20 69 73  d when memory is
31ea0 20 65 78 68 61 75 73 74 65 64 2e 0a 2a 2a 20 54   exhausted..** T
31eb0 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
31ec0 20 66 6f 72 20 74 68 65 20 73 6f 66 74 20 68 65   for the soft he
31ed0 61 70 20 6c 69 6d 69 74 20 69 73 20 7a 65 72 6f  ap limit is zero
31ee0 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 6d  ..**.** SQLite m
31ef0 61 6b 65 73 20 61 20 62 65 73 74 20 65 66 66 6f  akes a best effo
31f00 72 74 20 74 6f 20 68 6f 6e 6f 72 20 74 68 65 20  rt to honor the 
31f10 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 2e  soft heap limit.
31f20 0a 2a 2a 20 42 75 74 20 69 66 20 74 68 65 20 73  .** But if the s
31f30 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 63  oft heap limit c
31f40 61 6e 6e 6f 74 20 62 65 20 68 6f 6e 6f 72 65 64  annot be honored
31f50 2c 20 65 78 65 63 75 74 69 6f 6e 20 77 69 6c 6c  , execution will
31f60 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 77 69 74  .** continue wit
31f70 68 6f 75 74 20 65 72 72 6f 72 20 6f 72 20 6e 6f  hout error or no
31f80 74 69 66 69 63 61 74 69 6f 6e 2e 20 20 54 68 69  tification.  Thi
31f90 73 20 69 73 20 77 68 79 20 74 68 65 20 6c 69 6d  s is why the lim
31fa0 69 74 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20  it is.** called 
31fb0 61 20 22 73 6f 66 74 22 20 6c 69 6d 69 74 2e 20  a "soft" limit. 
31fc0 20 49 74 20 69 73 20 61 64 76 69 73 6f 72 79 20   It is advisory 
31fd0 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 72 69 6f  only..**.** Prio
31fe0 72 20 74 6f 20 53 51 4c 69 74 65 20 76 65 72 73  r to SQLite vers
31ff0 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 69 73 20  ion 3.5.0, this 
32000 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63 6f 6e  routine only con
32010 73 74 72 61 69 6e 65 64 20 74 68 65 20 6d 65 6d  strained the mem
32020 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64  ory.** allocated
32030 20 62 79 20 61 20 73 69 6e 67 6c 65 20 74 68 72   by a single thr
32040 65 61 64 20 2d 20 74 68 65 20 73 61 6d 65 20 74  ead - the same t
32050 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 20 74  hread in which t
32060 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72  his routine.** r
32070 75 6e 73 2e 20 20 42 65 67 69 6e 6e 69 6e 67 20  uns.  Beginning 
32080 77 69 74 68 20 53 51 4c 69 74 65 20 76 65 72 73  with SQLite vers
32090 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 65 20 73  ion 3.5.0, the s
320a0 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 69  oft heap limit i
320b0 73 0a 2a 2a 20 61 70 70 6c 69 65 64 20 74 6f 20  s.** applied to 
320c0 61 6c 6c 20 74 68 72 65 61 64 73 2e 20 54 68 65  all threads. The
320d0 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 65 64   value specified
320e0 20 66 6f 72 20 74 68 65 20 73 6f 66 74 20 68 65   for the soft he
320f0 61 70 20 6c 69 6d 69 74 0a 2a 2a 20 69 73 20 61  ap limit.** is a
32100 6e 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f 6e  n upper bound on
32110 20 74 68 65 20 74 6f 74 61 6c 20 6d 65 6d 6f 72   the total memor
32120 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 6f 72  y allocation for
32130 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 20 49 6e   all threads. In
32140 0a 2a 2a 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e  .** version 3.5.
32150 30 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 65  0 there is no me
32160 63 68 61 6e 69 73 6d 20 66 6f 72 20 6c 69 6d 69  chanism for limi
32170 74 69 6e 67 20 74 68 65 20 68 65 61 70 20 75 73  ting the heap us
32180 61 67 65 20 66 6f 72 0a 2a 2a 20 69 6e 64 69 76  age for.** indiv
32190 69 64 75 61 6c 20 74 68 72 65 61 64 73 2e 0a 2a  idual threads..*
321a0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
321b0 73 3a 0a 2a 2a 20 5b 48 31 36 33 35 31 5d 20 5b  s:.** [H16351] [
321c0 48 31 36 33 35 32 5d 20 5b 48 31 36 33 35 33 5d  H16352] [H16353]
321d0 20 5b 48 31 36 33 35 34 5d 20 5b 48 31 36 33 35   [H16354] [H1635
321e0 35 5d 20 5b 48 31 36 33 35 38 5d 0a 2a 2f 0a 53  5] [H16358].*/.S
321f0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
32200 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70  qlite3_soft_heap
32210 5f 6c 69 6d 69 74 28 69 6e 74 29 3b 0a 0a 2f 2a  _limit(int);../*
32220 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 78  .** CAPI3REF: Ex
32230 74 72 61 63 74 20 4d 65 74 61 64 61 74 61 20 41  tract Metadata A
32240 62 6f 75 74 20 41 20 43 6f 6c 75 6d 6e 20 4f 66  bout A Column Of
32250 20 41 20 54 61 62 6c 65 20 7b 48 31 32 38 35 30   A Table {H12850
32260 7d 20 3c 53 36 30 33 30 30 3e 0a 2a 2a 0a 2a 2a  } <S60300>.**.**
32270 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
32280 74 75 72 6e 73 20 6d 65 74 61 64 61 74 61 20 61  turns metadata a
32290 62 6f 75 74 20 61 20 73 70 65 63 69 66 69 63 20  bout a specific 
322a0 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 73 70 65 63  column of a spec
322b0 69 66 69 63 0a 2a 2a 20 64 61 74 61 62 61 73 65  ific.** database
322c0 20 74 61 62 6c 65 20 61 63 63 65 73 73 69 62 6c   table accessibl
322d0 65 20 75 73 69 6e 67 20 74 68 65 20 5b 64 61 74  e using the [dat
322e0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
322f0 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 70 61 73 73  ] handle.** pass
32300 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ed as the first 
32310 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e  function argumen
32320 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c  t..**.** The col
32330 75 6d 6e 20 69 73 20 69 64 65 6e 74 69 66 69 65  umn is identifie
32340 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 2c  d by the second,
32350 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74   third and fourt
32360 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 0a  h parameters to.
32370 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** this function
32380 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72  . The second par
32390 61 6d 65 74 65 72 20 69 73 20 65 69 74 68 65 72  ameter is either
323a0 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
323b0 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 28 69 2e   database.** (i.
323c0 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65 6d 70  e. "main", "temp
323d0 22 20 6f 72 20 61 6e 20 61 74 74 61 63 68 65 64  " or an attached
323e0 20 64 61 74 61 62 61 73 65 29 20 63 6f 6e 74 61   database) conta
323f0 69 6e 69 6e 67 20 74 68 65 20 73 70 65 63 69 66  ining the specif
32400 69 65 64 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20  ied.** table or 
32410 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 73 20 4e  NULL. If it is N
32420 55 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 61 74  ULL, then all at
32430 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73  tached databases
32440 20 61 72 65 20 73 65 61 72 63 68 65 64 0a 2a 2a   are searched.**
32450 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 75   for the table u
32460 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 61 6c  sing the same al
32470 67 6f 72 69 74 68 6d 20 75 73 65 64 20 62 79 20  gorithm used by 
32480 74 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 67  the database eng
32490 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 73 6f 6c 76  ine to.** resolv
324a0 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74 61  e unqualified ta
324b0 62 6c 65 20 72 65 66 65 72 65 6e 63 65 73 2e 0a  ble references..
324c0 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20  **.** The third 
324d0 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61 6d  and fourth param
324e0 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66 75  eters to this fu
324f0 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20 74  nction are the t
32500 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a  able and column.
32510 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64  ** name of the d
32520 65 73 69 72 65 64 20 63 6f 6c 75 6d 6e 2c 20 72  esired column, r
32530 65 73 70 65 63 74 69 76 65 6c 79 2e 20 4e 65 69  espectively. Nei
32540 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 70 61  ther of these pa
32550 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6d 61 79 20  rameters.** may 
32560 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 4d  be NULL..**.** M
32570 65 74 61 64 61 74 61 20 69 73 20 72 65 74 75 72  etadata is retur
32580 6e 65 64 20 62 79 20 77 72 69 74 69 6e 67 20 74  ned by writing t
32590 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63  o the memory loc
325a0 61 74 69 6f 6e 73 20 70 61 73 73 65 64 20 61 73  ations passed as
325b0 20 74 68 65 20 35 74 68 0a 2a 2a 20 61 6e 64 20   the 5th.** and 
325c0 73 75 62 73 65 71 75 65 6e 74 20 70 61 72 61 6d  subsequent param
325d0 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66 75  eters to this fu
325e0 6e 63 74 69 6f 6e 2e 20 41 6e 79 20 6f 66 20 74  nction. Any of t
325f0 68 65 73 65 20 61 72 67 75 6d 65 6e 74 73 20 6d  hese arguments m
32600 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2c 20 69  ay be.** NULL, i
32610 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65  n which case the
32620 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 65   corresponding e
32630 6c 65 6d 65 6e 74 20 6f 66 20 6d 65 74 61 64 61  lement of metada
32640 74 61 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a 2a  ta is omitted..*
32650 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65  *.** <blockquote
32660 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64  >.** <table bord
32670 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74 72 3e 3c  er="1">.** <tr><
32680 74 68 3e 20 50 61 72 61 6d 65 74 65 72 20 3c 74  th> Parameter <t
32690 68 3e 20 4f 75 74 70 75 74 3c 62 72 3e 54 79 70  h> Output<br>Typ
326a0 65 20 3c 74 68 3e 20 20 44 65 73 63 72 69 70 74  e <th>  Descript
326b0 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c 74  ion.**.** <tr><t
326c0 64 3e 20 35 74 68 20 3c 74 64 3e 20 63 6f 6e 73  d> 5th <td> cons
326d0 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 44 61 74  t char* <td> Dat
326e0 61 20 74 79 70 65 0a 2a 2a 20 3c 74 72 3e 3c 74  a type.** <tr><t
326f0 64 3e 20 36 74 68 20 3c 74 64 3e 20 63 6f 6e 73  d> 6th <td> cons
32700 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 4e 61 6d  t char* <td> Nam
32710 65 20 6f 66 20 64 65 66 61 75 6c 74 20 63 6f 6c  e of default col
32720 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 0a  lation sequence.
32730 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 37 74 68 20  ** <tr><td> 7th 
32740 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20  <td> int        
32750 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f   <td> True if co
32760 6c 75 6d 6e 20 68 61 73 20 61 20 4e 4f 54 20 4e  lumn has a NOT N
32770 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a  ULL constraint.*
32780 2a 20 3c 74 72 3e 3c 74 64 3e 20 38 74 68 20 3c  * <tr><td> 8th <
32790 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20 20  td> int         
327a0 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f 6c  <td> True if col
327b0 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 20 74  umn is part of t
327c0 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 0a 2a  he PRIMARY KEY.*
327d0 2a 20 3c 74 72 3e 3c 74 64 3e 20 39 74 68 20 3c  * <tr><td> 9th <
327e0 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20 20  td> int         
327f0 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f 6c  <td> True if col
32800 75 6d 6e 20 69 73 20 5b 41 55 54 4f 49 4e 43 52  umn is [AUTOINCR
32810 45 4d 45 4e 54 5d 0a 2a 2a 20 3c 2f 74 61 62 6c  EMENT].** </tabl
32820 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 6f  e>.** </blockquo
32830 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65  te>.**.** The me
32840 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 74 6f 20  mory pointed to 
32850 62 79 20 74 68 65 20 63 68 61 72 61 63 74 65 72  by the character
32860 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e   pointers return
32870 65 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20 64 65  ed for the.** de
32880 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 61  claration type a
32890 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  nd collation seq
328a0 75 65 6e 63 65 20 69 73 20 76 61 6c 69 64 20 6f  uence is valid o
328b0 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65 20 6e 65  nly until the ne
328c0 78 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 61 6e  xt.** call to an
328d0 79 20 53 51 4c 69 74 65 20 41 50 49 20 66 75 6e  y SQLite API fun
328e0 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ction..**.** If 
328f0 74 68 65 20 73 70 65 63 69 66 69 65 64 20 74 61  the specified ta
32900 62 6c 65 20 69 73 20 61 63 74 75 61 6c 6c 79 20  ble is actually 
32910 61 20 76 69 65 77 2c 20 61 6e 20 5b 65 72 72 6f  a view, an [erro
32920 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72  r code] is retur
32930 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ned..**.** If th
32940 65 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c 75  e specified colu
32950 6d 6e 20 69 73 20 22 72 6f 77 69 64 22 2c 20 22  mn is "rowid", "
32960 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77 69 64 5f  oid" or "_rowid_
32970 22 20 61 6e 64 20 61 6e 0a 2a 2a 20 5b 49 4e 54  " and an.** [INT
32980 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
32990 5d 20 63 6f 6c 75 6d 6e 20 68 61 73 20 62 65 65  ] column has bee
329a0 6e 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63  n explicitly dec
329b0 6c 61 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20  lared, then the 
329c0 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72 61 6d 65  output.** parame
329d0 74 65 72 73 20 61 72 65 20 73 65 74 20 66 6f 72  ters are set for
329e0 20 74 68 65 20 65 78 70 6c 69 63 69 74 6c 79 20   the explicitly 
329f0 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 2e  declared column.
32a00 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 0a   If there is no.
32a10 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65  ** explicitly de
32a20 63 6c 61 72 65 64 20 5b 49 4e 54 45 47 45 52 20  clared [INTEGER 
32a30 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 63 6f 6c  PRIMARY KEY] col
32a40 75 6d 6e 2c 20 74 68 65 6e 20 74 68 65 20 6f 75  umn, then the ou
32a50 74 70 75 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65  tput.** paramete
32a60 72 73 20 61 72 65 20 73 65 74 20 61 73 20 66 6f  rs are set as fo
32a70 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72  llows:.**.** <pr
32a80 65 3e 0a 2a 2a 20 20 20 20 20 64 61 74 61 20 74  e>.**     data t
32a90 79 70 65 3a 20 22 49 4e 54 45 47 45 52 22 0a 2a  ype: "INTEGER".*
32aa0 2a 20 20 20 20 20 63 6f 6c 6c 61 74 69 6f 6e 20  *     collation 
32ab0 73 65 71 75 65 6e 63 65 3a 20 22 42 49 4e 41 52  sequence: "BINAR
32ac0 59 22 0a 2a 2a 20 20 20 20 20 6e 6f 74 20 6e 75  Y".**     not nu
32ad0 6c 6c 3a 20 30 0a 2a 2a 20 20 20 20 20 70 72 69  ll: 0.**     pri
32ae0 6d 61 72 79 20 6b 65 79 3a 20 31 0a 2a 2a 20 20  mary key: 1.**  
32af0 20 20 20 61 75 74 6f 20 69 6e 63 72 65 6d 65 6e     auto incremen
32b00 74 3a 20 30 0a 2a 2a 20 3c 2f 70 72 65 3e 0a 2a  t: 0.** </pre>.*
32b10 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
32b20 6f 6e 20 6d 61 79 20 6c 6f 61 64 20 6f 6e 65 20  on may load one 
32b30 6f 72 20 6d 6f 72 65 20 73 63 68 65 6d 61 73 20  or more schemas 
32b40 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 66 69  from database fi
32b50 6c 65 73 2e 20 49 66 20 61 6e 0a 2a 2a 20 65 72  les. If an.** er
32b60 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e  ror occurs durin
32b70 67 20 74 68 69 73 20 70 72 6f 63 65 73 73 2c 20  g this process, 
32b80 6f 72 20 69 66 20 74 68 65 20 72 65 71 75 65 73  or if the reques
32b90 74 65 64 20 74 61 62 6c 65 20 6f 72 20 63 6f 6c  ted table or col
32ba0 75 6d 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65  umn.** cannot be
32bb0 20 66 6f 75 6e 64 2c 20 61 6e 20 5b 65 72 72 6f   found, an [erro
32bc0 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72  r code] is retur
32bd0 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 72 6f 72  ned and an error
32be0 20 6d 65 73 73 61 67 65 20 6c 65 66 74 0a 2a 2a   message left.**
32bf0 20 69 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73   in the [databas
32c00 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74  e connection] (t
32c10 6f 20 62 65 20 72 65 74 72 69 65 76 65 64 20 75  o be retrieved u
32c20 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 65 72 72  sing sqlite3_err
32c30 6d 73 67 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  msg())..**.** Th
32c40 69 73 20 41 50 49 20 69 73 20 6f 6e 6c 79 20 61  is API is only a
32c50 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 20  vailable if the 
32c60 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70  library was comp
32c70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a  iled with the.**
32c80 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f   [SQLITE_ENABLE_
32c90 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 5d  COLUMN_METADATA]
32ca0 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 20   C-preprocessor 
32cb0 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a  symbol defined..
32cc0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
32cd0 74 20 73 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f  t sqlite3_table_
32ce0 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 28  column_metadata(
32cf0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
32d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
32d10 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e  * Connection han
32d20 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  dle */.  const c
32d30 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 20 20  har *zDbName,   
32d40 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
32d50 20 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c 20 2a 2f   name or NULL */
32d60 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
32d70 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20 20 2f  TableName,     /
32d80 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a  * Table name */.
32d90 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43    const char *zC
32da0 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20 20 2f 2a  olumnName,    /*
32db0 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a   Column name */.
32dc0 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a 70    char const **p
32dd0 7a 44 61 74 61 54 79 70 65 2c 20 20 20 20 2f 2a  zDataType,    /*
32de0 20 4f 55 54 50 55 54 3a 20 44 65 63 6c 61 72 65   OUTPUT: Declare
32df0 64 20 64 61 74 61 20 74 79 70 65 20 2a 2f 0a 20  d data type */. 
32e00 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a 70 7a   char const **pz
32e10 43 6f 6c 6c 53 65 71 2c 20 20 20 20 20 2f 2a 20  CollSeq,     /* 
32e20 4f 55 54 50 55 54 3a 20 43 6f 6c 6c 61 74 69 6f  OUTPUT: Collatio
32e30 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 20  n sequence name 
32e40 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f 74 4e 75  */.  int *pNotNu
32e50 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ll,             
32e60 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65   /* OUTPUT: True
32e70 20 69 66 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e   if NOT NULL con
32e80 73 74 72 61 69 6e 74 20 65 78 69 73 74 73 20 2a  straint exists *
32e90 2f 0a 20 20 69 6e 74 20 2a 70 50 72 69 6d 61 72  /.  int *pPrimar
32ea0 79 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20  yKey,           
32eb0 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65 20  /* OUTPUT: True 
32ec0 69 66 20 63 6f 6c 75 6d 6e 20 70 61 72 74 20 6f  if column part o
32ed0 66 20 50 4b 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  f PK */.  int *p
32ee0 41 75 74 6f 69 6e 63 20 20 20 20 20 20 20 20 20  Autoinc         
32ef0 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a        /* OUTPUT:
32f00 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20   True if column 
32f10 69 73 20 61 75 74 6f 2d 69 6e 63 72 65 6d 65 6e  is auto-incremen
32f20 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  t */.);../*.** C
32f30 41 50 49 33 52 45 46 3a 20 4c 6f 61 64 20 41 6e  API3REF: Load An
32f40 20 45 78 74 65 6e 73 69 6f 6e 20 7b 48 31 32 36   Extension {H126
32f50 30 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a  00} <S20500>.**.
32f60 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63  ** This interfac
32f70 65 20 6c 6f 61 64 73 20 61 6e 20 53 51 4c 69 74  e loads an SQLit
32f80 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c 69 62 72  e extension libr
32f90 61 72 79 20 66 72 6f 6d 20 74 68 65 20 6e 61 6d  ary from the nam
32fa0 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b  ed file..**.** {
32fb0 48 31 32 36 30 31 7d 20 54 68 65 20 73 71 6c 69  H12601} The sqli
32fc0 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69  te3_load_extensi
32fd0 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20 61  on() interface a
32fe0 74 74 65 6d 70 74 73 20 74 6f 20 6c 6f 61 64 20  ttempts to load 
32ff0 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 53  an.**          S
33000 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f 6e 20  QLite extension 
33010 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 65  library containe
33020 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 7a 46  d in the file zF
33030 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36  ile..**.** {H126
33040 30 32 7d 20 54 68 65 20 65 6e 74 72 79 20 70 6f  02} The entry po
33050 69 6e 74 20 69 73 20 7a 50 72 6f 63 2e 0a 2a 2a  int is zProc..**
33060 0a 2a 2a 20 7b 48 31 32 36 30 33 7d 20 7a 50 72  .** {H12603} zPr
33070 6f 63 20 6d 61 79 20 62 65 20 30 2c 20 69 6e 20  oc may be 0, in 
33080 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20 6e  which case the n
33090 61 6d 65 20 6f 66 20 74 68 65 20 65 6e 74 72 79  ame of the entry
330a0 20 70 6f 69 6e 74 0a 2a 2a 20 20 20 20 20 20 20   point.**       
330b0 20 20 20 64 65 66 61 75 6c 74 73 20 74 6f 20 22     defaults to "
330c0 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f  sqlite3_extensio
330d0 6e 5f 69 6e 69 74 22 2e 0a 2a 2a 0a 2a 2a 20 7b  n_init"..**.** {
330e0 48 31 32 36 30 34 7d 20 54 68 65 20 73 71 6c 69  H12604} The sqli
330f0 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69  te3_load_extensi
33100 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20 73  on() interface s
33110 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 20  hall return.**  
33120 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f          [SQLITE_
33130 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 20 61  OK] on success a
33140 6e 64 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52  nd [SQLITE_ERROR
33150 5d 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67  ] if something g
33160 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a  oes wrong..**.**
33170 20 7b 48 31 32 36 30 35 7d 20 49 66 20 61 6e 20   {H12605} If an 
33180 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 6e 64  error occurs and
33190 20 70 7a 45 72 72 4d 73 67 20 69 73 20 6e 6f 74   pzErrMsg is not
331a0 20 30 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20   0, then the.** 
331b0 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
331c0 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  3_load_extension
331d0 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 73 68  ()] interface sh
331e0 61 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a  all attempt to.*
331f0 2a 20 20 20 20 20 20 20 20 20 20 66 69 6c 6c 20  *          fill 
33200 2a 70 7a 45 72 72 4d 73 67 20 77 69 74 68 20 65  *pzErrMsg with e
33210 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78  rror message tex
33220 74 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f  t stored in memo
33230 72 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  ry.**          o
33240 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71  btained from [sq
33250 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e  lite3_malloc()].
33260 20 7b 45 4e 44 7d 20 20 54 68 65 20 63 61 6c 6c   {END}  The call
33270 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  ing function.** 
33280 20 20 20 20 20 20 20 20 20 73 68 6f 75 6c 64 20           should 
33290 66 72 65 65 20 74 68 69 73 20 6d 65 6d 6f 72 79  free this memory
332a0 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c   by calling [sql
332b0 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a 2a  ite3_free()]..**
332c0 0a 2a 2a 20 7b 48 31 32 36 30 36 7d 20 45 78 74  .** {H12606} Ext
332d0 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 6d  ension loading m
332e0 75 73 74 20 62 65 20 65 6e 61 62 6c 65 64 20 75  ust be enabled u
332f0 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20  sing.**         
33300 20 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65   [sqlite3_enable
33310 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28  _load_extension(
33320 29 5d 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c  )] prior to call
33330 69 6e 67 20 74 68 69 73 20 41 50 49 2c 0a 2a 2a  ing this API,.**
33340 20 20 20 20 20 20 20 20 20 20 6f 74 68 65 72 77            otherw
33350 69 73 65 20 61 6e 20 65 72 72 6f 72 20 77 69 6c  ise an error wil
33360 6c 20 62 65 20 72 65 74 75 72 6e 65 64 2e 0a 2a  l be returned..*
33370 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
33380 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78   sqlite3_load_ex
33390 74 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c 69 74  tension(.  sqlit
333a0 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
333b0 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 78 74   /* Load the ext
333c0 65 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69 73  ension into this
333d0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
333e0 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  tion */.  const 
333f0 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20 20  char *zFile,    
33400 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 73  /* Name of the s
33410 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 63 6f  hared library co
33420 6e 74 61 69 6e 69 6e 67 20 65 78 74 65 6e 73 69  ntaining extensi
33430 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  on */.  const ch
33440 61 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f 2a  ar *zProc,    /*
33450 20 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20 44   Entry point.  D
33460 65 72 69 76 65 64 20 66 72 6f 6d 20 7a 46 69 6c  erived from zFil
33470 65 20 69 66 20 30 20 2a 2f 0a 20 20 63 68 61 72  e if 0 */.  char
33480 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20   **pzErrMsg     
33490 20 20 2f 2a 20 50 75 74 20 65 72 72 6f 72 20 6d    /* Put error m
334a0 65 73 73 61 67 65 20 68 65 72 65 20 69 66 20 6e  essage here if n
334b0 6f 74 20 30 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a  ot 0 */.);../*.*
334c0 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62  * CAPI3REF: Enab
334d0 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 45 78  le Or Disable Ex
334e0 74 65 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e 67 20  tension Loading 
334f0 7b 48 31 32 36 32 30 7d 20 3c 53 32 30 35 30 30  {H12620} <S20500
33500 3e 0a 2a 2a 0a 2a 2a 20 53 6f 20 61 73 20 6e 6f  >.**.** So as no
33510 74 20 74 6f 20 6f 70 65 6e 20 73 65 63 75 72 69  t to open securi
33520 74 79 20 68 6f 6c 65 73 20 69 6e 20 6f 6c 64 65  ty holes in olde
33530 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74  r applications t
33540 68 61 74 20 61 72 65 0a 2a 2a 20 75 6e 70 72 65  hat are.** unpre
33550 70 61 72 65 64 20 74 6f 20 64 65 61 6c 20 77 69  pared to deal wi
33560 74 68 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61  th extension loa
33570 64 69 6e 67 2c 20 61 6e 64 20 61 73 20 61 20 6d  ding, and as a m
33580 65 61 6e 73 20 6f 66 20 64 69 73 61 62 6c 69 6e  eans of disablin
33590 67 0a 2a 2a 20 65 78 74 65 6e 73 69 6f 6e 20 6c  g.** extension l
335a0 6f 61 64 69 6e 67 20 77 68 69 6c 65 20 65 76 61  oading while eva
335b0 6c 75 61 74 69 6e 67 20 75 73 65 72 2d 65 6e 74  luating user-ent
335c0 65 72 65 64 20 53 51 4c 2c 20 74 68 65 20 66 6f  ered SQL, the fo
335d0 6c 6c 6f 77 69 6e 67 20 41 50 49 0a 2a 2a 20 69  llowing API.** i
335e0 73 20 70 72 6f 76 69 64 65 64 20 74 6f 20 74 75  s provided to tu
335f0 72 6e 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  rn the [sqlite3_
33600 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29  load_extension()
33610 5d 20 6d 65 63 68 61 6e 69 73 6d 20 6f 6e 20 61  ] mechanism on a
33620 6e 64 20 6f 66 66 2e 0a 2a 2a 0a 2a 2a 20 45 78  nd off..**.** Ex
33630 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20  tension loading 
33640 69 73 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c  is off by defaul
33650 74 2e 20 53 65 65 20 74 69 63 6b 65 74 20 23 31  t. See ticket #1
33660 38 36 33 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36  863..**.** {H126
33670 32 31 7d 20 43 61 6c 6c 20 74 68 65 20 73 71 6c  21} Call the sql
33680 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64  ite3_enable_load
33690 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 72 6f 75  _extension() rou
336a0 74 69 6e 65 20 77 69 74 68 20 6f 6e 6f 66 66 3d  tine with onoff=
336b0 3d 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  =1.**          t
336c0 6f 20 74 75 72 6e 20 65 78 74 65 6e 73 69 6f 6e  o turn extension
336d0 20 6c 6f 61 64 69 6e 67 20 6f 6e 20 61 6e 64 20   loading on and 
336e0 63 61 6c 6c 20 69 74 20 77 69 74 68 20 6f 6e 6f  call it with ono
336f0 66 66 3d 3d 30 20 74 6f 20 74 75 72 6e 0a 2a 2a  ff==0 to turn.**
33700 20 20 20 20 20 20 20 20 20 20 69 74 20 62 61 63            it bac
33710 6b 20 6f 66 66 20 61 67 61 69 6e 2e 0a 2a 2a 0a  k off again..**.
33720 2a 2a 20 7b 48 31 32 36 32 32 7d 20 45 78 74 65  ** {H12622} Exte
33730 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73  nsion loading is
33740 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c 74 2e   off by default.
33750 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
33760 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c  nt sqlite3_enabl
33770 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  e_load_extension
33780 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
33790 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a 2a 2a  t onoff);../*.**
337a0 20 43 41 50 49 33 52 45 46 3a 20 41 75 74 6f 6d   CAPI3REF: Autom
337b0 61 74 69 63 61 6c 6c 79 20 4c 6f 61 64 20 41 6e  atically Load An
337c0 20 45 78 74 65 6e 73 69 6f 6e 73 20 7b 48 31 32   Extensions {H12
337d0 36 34 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a  640} <S20500>.**
337e0 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 63 61 6e  .** This API can
337f0 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 74 20 70   be invoked at p
33800 72 6f 67 72 61 6d 20 73 74 61 72 74 75 70 20 69  rogram startup i
33810 6e 20 6f 72 64 65 72 20 74 6f 20 72 65 67 69 73  n order to regis
33820 74 65 72 0a 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f  ter.** one or mo
33830 72 65 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69  re statically li
33840 6e 6b 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 20  nked extensions 
33850 74 68 61 74 20 77 69 6c 6c 20 62 65 20 61 76 61  that will be ava
33860 69 6c 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c  ilable.** to all
33870 20 6e 65 77 20 5b 64 61 74 61 62 61 73 65 20 63   new [database c
33880 6f 6e 6e 65 63 74 69 6f 6e 73 5d 2e 20 7b 45 4e  onnections]. {EN
33890 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  D}.**.** This ro
338a0 75 74 69 6e 65 20 73 74 6f 72 65 73 20 61 20 70  utine stores a p
338b0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 65 78  ointer to the ex
338c0 74 65 6e 73 69 6f 6e 20 69 6e 20 61 6e 20 61 72  tension in an ar
338d0 72 61 79 20 74 68 61 74 20 69 73 0a 2a 2a 20 6f  ray that is.** o
338e0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71  btained from [sq
338f0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e  lite3_malloc()].
33900 20 20 49 66 20 79 6f 75 20 72 75 6e 20 61 20 6d    If you run a m
33910 65 6d 6f 72 79 20 6c 65 61 6b 20 63 68 65 63 6b  emory leak check
33920 65 72 0a 2a 2a 20 6f 6e 20 79 6f 75 72 20 70 72  er.** on your pr
33930 6f 67 72 61 6d 20 61 6e 64 20 69 74 20 72 65 70  ogram and it rep
33940 6f 72 74 73 20 61 20 6c 65 61 6b 20 62 65 63 61  orts a leak beca
33950 75 73 65 20 6f 66 20 74 68 69 73 20 61 72 72 61  use of this arra
33960 79 2c 20 69 6e 76 6f 6b 65 0a 2a 2a 20 5b 73 71  y, invoke.** [sq
33970 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75 74 6f  lite3_reset_auto
33980 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 70 72  _extension()] pr
33990 69 6f 72 20 74 6f 20 73 68 75 74 64 6f 77 6e 20  ior to shutdown 
339a0 74 6f 20 66 72 65 65 20 74 68 65 20 6d 65 6d 6f  to free the memo
339b0 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34  ry..**.** {H1264
339c0 31 7d 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  1} This function
339d0 20 72 65 67 69 73 74 65 72 73 20 61 6e 20 65 78   registers an ex
339e0 74 65 6e 73 69 6f 6e 20 65 6e 74 72 79 20 70 6f  tension entry po
339f0 69 6e 74 20 74 68 61 74 20 69 73 0a 2a 2a 20 20  int that is.**  
33a00 20 20 20 20 20 20 20 20 61 75 74 6f 6d 61 74 69          automati
33a10 63 61 6c 6c 79 20 69 6e 76 6f 6b 65 64 20 77 68  cally invoked wh
33a20 65 6e 65 76 65 72 20 61 20 6e 65 77 20 5b 64 61  enever a new [da
33a30 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
33a40 6e 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69  n].**          i
33a50 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 5b  s opened using [
33a60 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c  sqlite3_open()],
33a70 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36   [sqlite3_open16
33a80 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  ()],.**         
33a90 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65   or [sqlite3_ope
33aa0 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b  n_v2()]..**.** {
33ab0 48 31 32 36 34 32 7d 20 44 75 70 6c 69 63 61 74  H12642} Duplicat
33ac0 65 20 65 78 74 65 6e 73 69 6f 6e 73 20 61 72 65  e extensions are
33ad0 20 64 65 74 65 63 74 65 64 20 73 6f 20 63 61 6c   detected so cal
33ae0 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e  ling this routin
33af0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6d 75  e.**          mu
33b00 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69 74  ltiple times wit
33b10 68 20 74 68 65 20 73 61 6d 65 20 65 78 74 65 6e  h the same exten
33b20 73 69 6f 6e 20 69 73 20 68 61 72 6d 6c 65 73 73  sion is harmless
33b30 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 33 7d  ..**.** {H12643}
33b40 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74   This routine st
33b50 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74  ores a pointer t
33b60 6f 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20  o the extension 
33b70 69 6e 20 61 6e 20 61 72 72 61 79 0a 2a 2a 20 20  in an array.**  
33b80 20 20 20 20 20 20 20 20 74 68 61 74 20 69 73 20          that is 
33b90 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73  obtained from [s
33ba0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
33bb0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 34 7d  ..**.** {H12644}
33bc0 20 41 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e   Automatic exten
33bd0 73 69 6f 6e 73 20 61 70 70 6c 79 20 61 63 72 6f  sions apply acro
33be0 73 73 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 0a  ss all threads..
33bf0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
33c00 74 20 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65  t sqlite3_auto_e
33c10 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 20 28 2a  xtension(void (*
33c20 78 45 6e 74 72 79 50 6f 69 6e 74 29 28 76 6f 69  xEntryPoint)(voi
33c30 64 29 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  d));../*.** CAPI
33c40 33 52 45 46 3a 20 52 65 73 65 74 20 41 75 74 6f  3REF: Reset Auto
33c50 6d 61 74 69 63 20 45 78 74 65 6e 73 69 6f 6e 20  matic Extension 
33c60 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36 36 30 7d  Loading {H12660}
33c70 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S20500>.**.** 
33c80 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 69  This function di
33c90 73 61 62 6c 65 73 20 61 6c 6c 20 70 72 65 76 69  sables all previ
33ca0 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 64  ously registered
33cb0 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a 20 65 78   automatic.** ex
33cc0 74 65 6e 73 69 6f 6e 73 2e 20 7b 45 4e 44 7d 20  tensions. {END} 
33cd0 20 49 74 20 75 6e 64 6f 65 73 20 74 68 65 20 65   It undoes the e
33ce0 66 66 65 63 74 20 6f 66 20 61 6c 6c 20 70 72 69  ffect of all pri
33cf0 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 61  or.** [sqlite3_a
33d00 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d  uto_extension()]
33d10 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48   calls..**.** {H
33d20 31 32 36 36 31 7d 20 54 68 69 73 20 66 75 6e 63  12661} This func
33d30 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 61 6c  tion disables al
33d40 6c 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67  l previously reg
33d50 69 73 74 65 72 65 64 0a 2a 2a 20 20 20 20 20 20  istered.**      
33d60 20 20 20 20 61 75 74 6f 6d 61 74 69 63 20 65 78      automatic ex
33d70 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20  tensions..**.** 
33d80 7b 48 31 32 36 36 32 7d 20 54 68 69 73 20 66 75  {H12662} This fu
33d90 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20  nction disables 
33da0 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73  automatic extens
33db0 69 6f 6e 73 20 69 6e 20 61 6c 6c 20 74 68 72 65  ions in all thre
33dc0 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ads..*/.SQLITE_A
33dd0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
33de0 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74 65 6e  reset_auto_exten
33df0 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a  sion(void);../*.
33e00 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  ****** EXPERIMEN
33e10 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 74 6f  TAL - subject to
33e20 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 20   change without 
33e30 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  notice *********
33e40 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20  *****.**.** The 
33e50 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 65  interface to the
33e60 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d   virtual-table m
33e70 65 63 68 61 6e 69 73 6d 20 69 73 20 63 75 72 72  echanism is curr
33e80 65 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 64  ently considered
33e90 0a 2a 2a 20 74 6f 20 62 65 20 65 78 70 65 72 69  .** to be experi
33ea0 6d 65 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e 74  mental.  The int
33eb0 65 72 66 61 63 65 20 6d 69 67 68 74 20 63 68 61  erface might cha
33ec0 6e 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69  nge in incompati
33ed0 62 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20  ble ways..** If 
33ee0 74 68 69 73 20 69 73 20 61 20 70 72 6f 62 6c 65  this is a proble
33ef0 6d 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f  m for you, do no
33f00 74 20 75 73 65 20 74 68 65 20 69 6e 74 65 72 66  t use the interf
33f10 61 63 65 20 61 74 20 74 68 69 73 20 74 69 6d 65  ace at this time
33f20 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65  ..**.** When the
33f30 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d   virtual-table m
33f40 65 63 68 61 6e 69 73 6d 20 73 74 61 62 69 6c 69  echanism stabili
33f50 7a 65 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 63  zes, we will dec
33f60 6c 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65  lare the.** inte
33f70 72 66 61 63 65 20 66 69 78 65 64 2c 20 73 75 70  rface fixed, sup
33f80 70 6f 72 74 20 69 74 20 69 6e 64 65 66 69 6e 69  port it indefini
33f90 74 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65  tely, and remove
33fa0 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a   this comment..*
33fb0 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75  /../*.** Structu
33fc0 72 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20  res used by the 
33fd0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e  virtual table in
33fe0 74 65 72 66 61 63 65 0a 2a 2f 0a 74 79 70 65 64  terface.*/.typed
33ff0 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
34000 33 5f 76 74 61 62 20 73 71 6c 69 74 65 33 5f 76  3_vtab sqlite3_v
34010 74 61 62 3b 0a 74 79 70 65 64 65 66 20 73 74 72  tab;.typedef str
34020 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
34030 78 5f 69 6e 66 6f 20 73 71 6c 69 74 65 33 5f 69  x_info sqlite3_i
34040 6e 64 65 78 5f 69 6e 66 6f 3b 0a 74 79 70 65 64  ndex_info;.typed
34050 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
34060 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 73 71  3_vtab_cursor sq
34070 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
34080 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  r;.typedef struc
34090 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
340a0 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 3b   sqlite3_module;
340b0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
340c0 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20  : Virtual Table 
340d0 4f 62 6a 65 63 74 20 7b 48 31 38 30 30 30 7d 20  Object {H18000} 
340e0 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57  <S20400>.** KEYW
340f0 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f 6d 6f  ORDS: sqlite3_mo
34100 64 75 6c 65 20 7b 76 69 72 74 75 61 6c 20 74 61  dule {virtual ta
34110 62 6c 65 20 6d 6f 64 75 6c 65 7d 0a 2a 2a 20 45  ble module}.** E
34120 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a  XPERIMENTAL.**.*
34130 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65  * This structure
34140 2c 20 73 6f 6d 65 74 69 6d 65 73 20 63 61 6c 6c  , sometimes call
34150 65 64 20 61 20 61 20 22 76 69 72 74 75 61 6c 20  ed a a "virtual 
34160 74 61 62 6c 65 20 6d 6f 64 75 6c 65 22 2c 20 0a  table module", .
34170 2a 2a 20 64 65 66 69 6e 65 73 20 74 68 65 20 69  ** defines the i
34180 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
34190 20 61 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c   a [virtual tabl
341a0 65 73 5d 2e 20 20 0a 2a 2a 20 54 68 69 73 20 73  es].  .** This s
341b0 74 72 75 63 74 75 72 65 20 63 6f 6e 73 69 73 74  tructure consist
341c0 73 20 6d 6f 73 74 6c 79 20 6f 66 20 6d 65 74 68  s mostly of meth
341d0 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75  ods for the modu
341e0 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72 74  le..**.** A virt
341f0 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65  ual table module
34200 20 69 73 20 63 72 65 61 74 65 64 20 62 79 20 66   is created by f
34210 69 6c 6c 69 6e 67 20 69 6e 20 61 20 70 65 72 73  illing in a pers
34220 69 73 74 65 6e 74 0a 2a 2a 20 69 6e 73 74 61 6e  istent.** instan
34230 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ce of this struc
34240 74 75 72 65 20 61 6e 64 20 70 61 73 73 69 6e 67  ture and passing
34250 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
34260 61 74 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 74  at instance.** t
34270 6f 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74  o [sqlite3_creat
34280 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 6f 72 20 5b  e_module()] or [
34290 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
342a0 6f 64 75 6c 65 5f 76 32 28 29 5d 2e 0a 2a 2a 20  odule_v2()]..** 
342b0 54 68 65 20 72 65 67 69 73 74 72 61 74 69 6f 6e  The registration
342c0 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 20 75   remains valid u
342d0 6e 74 69 6c 20 69 74 20 69 73 20 72 65 70 6c 61  ntil it is repla
342e0 63 65 64 20 62 79 20 61 20 64 69 66 66 65 72 65  ced by a differe
342f0 6e 74 0a 2a 2a 20 6d 6f 64 75 6c 65 20 6f 72 20  nt.** module or 
34300 75 6e 74 69 6c 20 74 68 65 20 5b 64 61 74 61 62  until the [datab
34310 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
34320 63 6c 6f 73 65 73 2e 20 20 54 68 65 20 63 6f 6e  closes.  The con
34330 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 69 73 20  tent.** of this 
34340 73 74 72 75 63 74 75 72 65 20 6d 75 73 74 20 6e  structure must n
34350 6f 74 20 63 68 61 6e 67 65 20 77 68 69 6c 65 20  ot change while 
34360 69 74 20 69 73 20 72 65 67 69 73 74 65 72 65 64  it is registered
34370 20 77 69 74 68 0a 2a 2a 20 61 6e 79 20 64 61 74   with.** any dat
34380 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
34390 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69  ..*/.struct sqli
343a0 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 0a 20 20 69  te3_module {.  i
343b0 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20 69  nt iVersion;.  i
343c0 6e 74 20 28 2a 78 43 72 65 61 74 65 29 28 73 71  nt (*xCreate)(sq
343d0 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20 2a 70 41  lite3*, void *pA
343e0 75 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ux,.            
343f0 20 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e     int argc, con
34400 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61  st char *const*a
34410 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20  rgv,.           
34420 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62      sqlite3_vtab
34430 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61 72 2a   **ppVTab, char*
34440 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e  *);.  int (*xCon
34450 6e 65 63 74 29 28 73 71 6c 69 74 65 33 2a 2c 20  nect)(sqlite3*, 
34460 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 20 20  void *pAux,.    
34470 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 61             int a
34480 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  rgc, const char 
34490 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 20  *const*argv,.   
344a0 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
344b0 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 61  te3_vtab **ppVTa
344c0 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e  b, char**);.  in
344d0 74 20 28 2a 78 42 65 73 74 49 6e 64 65 78 29 28  t (*xBestIndex)(
344e0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
344f0 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64  Tab, sqlite3_ind
34500 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20 20 69 6e 74  ex_info*);.  int
34510 20 28 2a 78 44 69 73 63 6f 6e 6e 65 63 74 29 28   (*xDisconnect)(
34520 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
34530 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44  Tab);.  int (*xD
34540 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f  estroy)(sqlite3_
34550 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20  vtab *pVTab);.  
34560 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c  int (*xOpen)(sql
34570 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62  ite3_vtab *pVTab
34580 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  , sqlite3_vtab_c
34590 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72  ursor **ppCursor
345a0 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f 73  );.  int (*xClos
345b0 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  e)(sqlite3_vtab_
345c0 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20  cursor*);.  int 
345d0 28 2a 78 46 69 6c 74 65 72 29 28 73 71 6c 69 74  (*xFilter)(sqlit
345e0 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c  e3_vtab_cursor*,
345f0 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e   int idxNum, con
34600 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 72 2c  st char *idxStr,
34610 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
34620 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
34630 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29  e3_value **argv)
34640 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 74 29  ;.  int (*xNext)
34650 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75  (sqlite3_vtab_cu
34660 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  rsor*);.  int (*
34670 78 45 6f 66 29 28 73 71 6c 69 74 65 33 5f 76 74  xEof)(sqlite3_vt
34680 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69  ab_cursor*);.  i
34690 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e 29 28 73 71  nt (*xColumn)(sq
346a0 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
346b0 72 2a 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  r*, sqlite3_cont
346c0 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e  ext*, int);.  in
346d0 74 20 28 2a 78 52 6f 77 69 64 29 28 73 71 6c 69  t (*xRowid)(sqli
346e0 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a  te3_vtab_cursor*
346f0 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
34700 2a 70 52 6f 77 69 64 29 3b 0a 20 20 69 6e 74 20  *pRowid);.  int 
34710 28 2a 78 55 70 64 61 74 65 29 28 73 71 6c 69 74  (*xUpdate)(sqlit
34720 65 33 5f 76 74 61 62 20 2a 2c 20 69 6e 74 2c 20  e3_vtab *, int, 
34730 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
34740 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
34750 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 42 65 67  *);.  int (*xBeg
34760 69 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  in)(sqlite3_vtab
34770 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20   *pVTab);.  int 
34780 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74 65 33  (*xSync)(sqlite3
34790 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20  _vtab *pVTab);. 
347a0 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 29 28   int (*xCommit)(
347b0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
347c0 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52  Tab);.  int (*xR
347d0 6f 6c 6c 62 61 63 6b 29 28 73 71 6c 69 74 65 33  ollback)(sqlite3
347e0 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20  _vtab *pVTab);. 
347f0 20 69 6e 74 20 28 2a 78 46 69 6e 64 46 75 6e 63   int (*xFindFunc
34800 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 5f 76 74  tion)(sqlite3_vt
34810 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74 20 6e  ab *pVtab, int n
34820 41 72 67 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Arg, const char 
34830 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  *zName,.        
34840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76                 v
34850 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29 28 73  oid (**pxFunc)(s
34860 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
34870 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
34880 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20 20 20  e**),.          
34890 20 20 20 20 20 20 20 20 20 20 20 20 20 76 6f 69               voi
348a0 64 20 2a 2a 70 70 41 72 67 29 3b 0a 20 20 69 6e  d **ppArg);.  in
348b0 74 20 28 2a 78 52 65 6e 61 6d 65 29 28 73 71 6c  t (*xRename)(sql
348c0 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
348d0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
348e0 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43  ew);.};../*.** C
348f0 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c  API3REF: Virtual
34900 20 54 61 62 6c 65 20 49 6e 64 65 78 69 6e 67 20   Table Indexing 
34910 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 7b 48 31 38  Information {H18
34920 31 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a  100} <S20400>.**
34930 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74   KEYWORDS: sqlit
34940 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a 2a  e3_index_info.**
34950 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
34960 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
34970 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63  index_info struc
34980 74 75 72 65 20 61 6e 64 20 69 74 73 20 73 75 62  ture and its sub
34990 73 74 72 75 63 74 75 72 65 73 20 69 73 20 75 73  structures is us
349a0 65 64 20 74 6f 0a 2a 2a 20 70 61 73 73 20 69 6e  ed to.** pass in
349b0 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 61  formation into a
349c0 6e 64 20 72 65 63 65 69 76 65 20 74 68 65 20 72  nd receive the r
349d0 65 70 6c 79 20 66 72 6f 6d 20 74 68 65 20 5b 78  eply from the [x
349e0 42 65 73 74 49 6e 64 65 78 5d 0a 2a 2a 20 6d 65  BestIndex].** me
349f0 74 68 6f 64 20 6f 66 20 61 20 5b 76 69 72 74 75  thod of a [virtu
34a00 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d  al table module]
34a10 2e 20 20 54 68 65 20 66 69 65 6c 64 73 20 75 6e  .  The fields un
34a20 64 65 72 20 2a 2a 49 6e 70 75 74 73 2a 2a 20 61  der **Inputs** a
34a30 72 65 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 73  re the.** inputs
34a40 20 74 6f 20 78 42 65 73 74 49 6e 64 65 78 20 61   to xBestIndex a
34a50 6e 64 20 61 72 65 20 72 65 61 64 2d 6f 6e 6c 79  nd are read-only
34a60 2e 20 20 78 42 65 73 74 49 6e 64 65 78 20 69 6e  .  xBestIndex in
34a70 73 65 72 74 73 20 69 74 73 0a 2a 2a 20 72 65 73  serts its.** res
34a80 75 6c 74 73 20 69 6e 74 6f 20 74 68 65 20 2a 2a  ults into the **
34a90 4f 75 74 70 75 74 73 2a 2a 20 66 69 65 6c 64 73  Outputs** fields
34aa0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 43 6f 6e  ..**.** The aCon
34ab0 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 79 20  straint[] array 
34ac0 72 65 63 6f 72 64 73 20 57 48 45 52 45 20 63 6c  records WHERE cl
34ad0 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73  ause constraints
34ae0 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a   of the form:.**
34af0 0a 2a 2a 20 3c 70 72 65 3e 63 6f 6c 75 6d 6e 20  .** <pre>column 
34b00 4f 50 20 65 78 70 72 3c 2f 70 72 65 3e 0a 2a 2a  OP expr</pre>.**
34b10 0a 2a 2a 20 77 68 65 72 65 20 4f 50 20 69 73 20  .** where OP is 
34b20 3d 2c 20 26 6c 74 3b 2c 20 26 6c 74 3b 3d 2c 20  =, &lt;, &lt;=, 
34b30 26 67 74 3b 2c 20 6f 72 20 26 67 74 3b 3d 2e 20  &gt;, or &gt;=. 
34b40 20 54 68 65 20 70 61 72 74 69 63 75 6c 61 72 20   The particular 
34b50 6f 70 65 72 61 74 6f 72 20 69 73 0a 2a 2a 20 73  operator is.** s
34b60 74 6f 72 65 64 20 69 6e 20 61 43 6f 6e 73 74 72  tored in aConstr
34b70 61 69 6e 74 5b 5d 2e 6f 70 2e 20 20 54 68 65 20  aint[].op.  The 
34b80 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 6f 6c  index of the col
34b90 75 6d 6e 20 69 73 20 73 74 6f 72 65 64 20 69 6e  umn is stored in
34ba0 0a 2a 2a 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b  .** aConstraint[
34bb0 5d 2e 69 43 6f 6c 75 6d 6e 2e 20 20 61 43 6f 6e  ].iColumn.  aCon
34bc0 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61 62 6c 65  straint[].usable
34bd0 20 69 73 20 54 52 55 45 20 69 66 20 74 68 65 0a   is TRUE if the.
34be0 2a 2a 20 65 78 70 72 20 6f 6e 20 74 68 65 20 72  ** expr on the r
34bf0 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 63  ight-hand side c
34c00 61 6e 20 62 65 20 65 76 61 6c 75 61 74 65 64 20  an be evaluated 
34c10 28 61 6e 64 20 74 68 75 73 20 74 68 65 20 63 6f  (and thus the co
34c20 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 69 73 20 75  nstraint.** is u
34c30 73 61 62 6c 65 29 20 61 6e 64 20 66 61 6c 73 65  sable) and false
34c40 20 69 66 20 69 74 20 63 61 6e 6e 6f 74 2e 0a 2a   if it cannot..*
34c50 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d 69 7a  *.** The optimiz
34c60 65 72 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  er automatically
34c70 20 69 6e 76 65 72 74 73 20 74 65 72 6d 73 20 6f   inverts terms o
34c80 66 20 74 68 65 20 66 6f 72 6d 20 22 65 78 70 72  f the form "expr
34c90 20 4f 50 20 63 6f 6c 75 6d 6e 22 0a 2a 2a 20 61   OP column".** a
34ca0 6e 64 20 6d 61 6b 65 73 20 6f 74 68 65 72 20 73  nd makes other s
34cb0 69 6d 70 6c 69 66 69 63 61 74 69 6f 6e 73 20 74  implifications t
34cc0 6f 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  o the WHERE clau
34cd0 73 65 20 69 6e 20 61 6e 20 61 74 74 65 6d 70 74  se in an attempt
34ce0 20 74 6f 0a 2a 2a 20 67 65 74 20 61 73 20 6d 61   to.** get as ma
34cf0 6e 79 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  ny WHERE clause 
34d00 74 65 72 6d 73 20 69 6e 74 6f 20 74 68 65 20 66  terms into the f
34d10 6f 72 6d 20 73 68 6f 77 6e 20 61 62 6f 76 65 20  orm shown above 
34d20 61 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 20  as possible..** 
34d30 54 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b  The aConstraint[
34d40 5d 20 61 72 72 61 79 20 6f 6e 6c 79 20 72 65 70  ] array only rep
34d50 6f 72 74 73 20 57 48 45 52 45 20 63 6c 61 75 73  orts WHERE claus
34d60 65 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 63  e terms in the c
34d70 6f 72 72 65 63 74 0a 2a 2a 20 66 6f 72 6d 20 74  orrect.** form t
34d80 68 61 74 20 72 65 66 65 72 20 74 6f 20 74 68 65  hat refer to the
34d90 20 70 61 72 74 69 63 75 6c 61 72 20 76 69 72 74   particular virt
34da0 75 61 6c 20 74 61 62 6c 65 20 62 65 69 6e 67 20  ual table being 
34db0 71 75 65 72 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  queried..**.** I
34dc0 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
34dd0 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
34de0 61 75 73 65 20 69 73 20 73 74 6f 72 65 64 20 69  ause is stored i
34df0 6e 20 61 4f 72 64 65 72 42 79 5b 5d 2e 0a 2a 2a  n aOrderBy[]..**
34e00 20 45 61 63 68 20 74 65 72 6d 20 6f 66 20 61 4f   Each term of aO
34e10 72 64 65 72 42 79 20 72 65 63 6f 72 64 73 20 61  rderBy records a
34e20 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 4f   column of the O
34e30 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e 0a  RDER BY clause..
34e40 2a 2a 0a 2a 2a 20 54 68 65 20 5b 78 42 65 73 74  **.** The [xBest
34e50 49 6e 64 65 78 5d 20 6d 65 74 68 6f 64 20 6d 75  Index] method mu
34e60 73 74 20 66 69 6c 6c 20 61 43 6f 6e 73 74 72 61  st fill aConstra
34e70 69 6e 74 55 73 61 67 65 5b 5d 20 77 69 74 68 20  intUsage[] with 
34e80 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61  information.** a
34e90 62 6f 75 74 20 77 68 61 74 20 70 61 72 61 6d 65  bout what parame
34ea0 74 65 72 73 20 74 6f 20 70 61 73 73 20 74 6f 20  ters to pass to 
34eb0 78 46 69 6c 74 65 72 2e 20 20 49 66 20 61 72 67  xFilter.  If arg
34ec0 76 49 6e 64 65 78 3e 30 20 74 68 65 6e 0a 2a 2a  vIndex>0 then.**
34ed0 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20   the right-hand 
34ee0 73 69 64 65 20 6f 66 20 74 68 65 20 63 6f 72 72  side of the corr
34ef0 65 73 70 6f 6e 64 69 6e 67 20 61 43 6f 6e 73 74  esponding aConst
34f00 72 61 69 6e 74 5b 5d 20 69 73 20 65 76 61 6c 75  raint[] is evalu
34f10 61 74 65 64 0a 2a 2a 20 61 6e 64 20 62 65 63 6f  ated.** and beco
34f20 6d 65 73 20 74 68 65 20 61 72 67 76 49 6e 64 65  mes the argvInde
34f30 78 2d 74 68 20 65 6e 74 72 79 20 69 6e 20 61 72  x-th entry in ar
34f40 67 76 2e 20 20 49 66 20 61 43 6f 6e 73 74 72 61  gv.  If aConstra
34f50 69 6e 74 55 73 61 67 65 5b 5d 2e 6f 6d 69 74 0a  intUsage[].omit.
34f60 2a 2a 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e  ** is true, then
34f70 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20   the constraint 
34f80 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65  is assumed to be
34f90 20 66 75 6c 6c 79 20 68 61 6e 64 6c 65 64 20 62   fully handled b
34fa0 79 20 74 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c  y the.** virtual
34fb0 20 74 61 62 6c 65 20 61 6e 64 20 69 73 20 6e 6f   table and is no
34fc0 74 20 63 68 65 63 6b 65 64 20 61 67 61 69 6e 20  t checked again 
34fd0 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a  by SQLite..**.**
34fe0 20 54 68 65 20 69 64 78 4e 75 6d 20 61 6e 64 20   The idxNum and 
34ff0 69 64 78 50 74 72 20 76 61 6c 75 65 73 20 61 72  idxPtr values ar
35000 65 20 72 65 63 6f 72 64 65 64 20 61 6e 64 20 70  e recorded and p
35010 61 73 73 65 64 20 69 6e 74 6f 20 74 68 65 0a 2a  assed into the.*
35020 2a 20 5b 78 46 69 6c 74 65 72 5d 20 6d 65 74 68  * [xFilter] meth
35030 6f 64 2e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  od..** [sqlite3_
35040 66 72 65 65 28 29 5d 20 69 73 20 75 73 65 64 20  free()] is used 
35050 74 6f 20 66 72 65 65 20 69 64 78 50 74 72 20 69  to free idxPtr i
35060 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 66 0a 2a  f and only iff.*
35070 2a 20 6e 65 65 64 54 6f 46 72 65 65 49 64 78 50  * needToFreeIdxP
35080 74 72 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a  tr is true..**.*
35090 2a 20 54 68 65 20 6f 72 64 65 72 42 79 43 6f 6e  * The orderByCon
350a0 73 75 6d 65 64 20 6d 65 61 6e 73 20 74 68 61 74  sumed means that
350b0 20 6f 75 74 70 75 74 20 66 72 6f 6d 20 5b 78 46   output from [xF
350c0 69 6c 74 65 72 5d 2f 5b 78 4e 65 78 74 5d 20 77  ilter]/[xNext] w
350d0 69 6c 6c 20 6f 63 63 75 72 20 69 6e 0a 2a 2a 20  ill occur in.** 
350e0 74 68 65 20 63 6f 72 72 65 63 74 20 6f 72 64 65  the correct orde
350f0 72 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65  r to satisfy the
35100 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
35110 20 73 6f 20 74 68 61 74 20 6e 6f 20 73 65 70 61   so that no sepa
35120 72 61 74 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 20  rate.** sorting 
35130 73 74 65 70 20 69 73 20 72 65 71 75 69 72 65 64  step is required
35140 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 73 74 69  ..**.** The esti
35150 6d 61 74 65 64 43 6f 73 74 20 76 61 6c 75 65 20  matedCost value 
35160 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f  is an estimate o
35170 66 20 74 68 65 20 63 6f 73 74 20 6f 66 20 64 6f  f the cost of do
35180 69 6e 67 20 74 68 65 0a 2a 2a 20 70 61 72 74 69  ing the.** parti
35190 63 75 6c 61 72 20 6c 6f 6f 6b 75 70 2e 20 20 41  cular lookup.  A
351a0 20 66 75 6c 6c 20 73 63 61 6e 20 6f 66 20 61 20   full scan of a 
351b0 74 61 62 6c 65 20 77 69 74 68 20 4e 20 65 6e 74  table with N ent
351c0 72 69 65 73 20 73 68 6f 75 6c 64 20 68 61 76 65  ries should have
351d0 0a 2a 2a 20 61 20 63 6f 73 74 20 6f 66 20 4e 2e  .** a cost of N.
351e0 20 20 41 20 62 69 6e 61 72 79 20 73 65 61 72 63    A binary searc
351f0 68 20 6f 66 20 61 20 74 61 62 6c 65 20 6f 66 20  h of a table of 
35200 4e 20 65 6e 74 72 69 65 73 20 73 68 6f 75 6c 64  N entries should
35210 20 68 61 76 65 20 61 0a 2a 2a 20 63 6f 73 74 20   have a.** cost 
35220 6f 66 20 61 70 70 72 6f 78 69 6d 61 74 65 6c 79  of approximately
35230 20 6c 6f 67 28 4e 29 2e 0a 2a 2f 0a 73 74 72 75   log(N)..*/.stru
35240 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  ct sqlite3_index
35250 5f 69 6e 66 6f 20 7b 0a 20 20 2f 2a 20 49 6e 70  _info {.  /* Inp
35260 75 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  uts */.  int nCo
35270 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20 20  nstraint;       
35280 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
35290 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6e   entries in aCon
352a0 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 73 74 72  straint */.  str
352b0 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
352c0 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 7b 0a 20  x_constraint {. 
352d0 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b      int iColumn;
352e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
352f0 20 43 6f 6c 75 6d 6e 20 6f 6e 20 6c 65 66 74 2d   Column on left-
35300 68 61 6e 64 20 73 69 64 65 20 6f 66 20 63 6f 6e  hand side of con
35310 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20  straint */.     
35320 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6f 70  unsigned char op
35330 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  ;         /* Con
35340 73 74 72 61 69 6e 74 20 6f 70 65 72 61 74 6f 72  straint operator
35350 20 2a 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e 65   */.     unsigne
35360 64 20 63 68 61 72 20 75 73 61 62 6c 65 3b 20 20  d char usable;  
35370 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
35380 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73  is constraint is
35390 20 75 73 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20   usable */.     
353a0 69 6e 74 20 69 54 65 72 6d 4f 66 66 73 65 74 3b  int iTermOffset;
353b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
353c0 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2d 20 78  d internally - x
353d0 42 65 73 74 49 6e 64 65 78 20 73 68 6f 75 6c 64  BestIndex should
353e0 20 69 67 6e 6f 72 65 20 2a 2f 0a 20 20 7d 20 2a   ignore */.  } *
353f0 61 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20  aConstraint;    
35400 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
35410 20 6f 66 20 57 48 45 52 45 20 63 6c 61 75 73 65   of WHERE clause
35420 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a   constraints */.
35430 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b 20    int nOrderBy; 
35440 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35450 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20  Number of terms 
35460 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  in the ORDER BY 
35470 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75  clause */.  stru
35480 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  ct sqlite3_index
35490 5f 6f 72 64 65 72 62 79 20 7b 0a 20 20 20 20 20  _orderby {.     
354a0 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20  int iColumn;    
354b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
354c0 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  umn number */.  
354d0 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72     unsigned char
354e0 20 64 65 73 63 3b 20 20 20 20 20 20 20 2f 2a 20   desc;       /* 
354f0 54 72 75 65 20 66 6f 72 20 44 45 53 43 2e 20 20  True for DESC.  
35500 46 61 6c 73 65 20 66 6f 72 20 41 53 43 2e 20 2a  False for ASC. *
35510 2f 0a 20 20 7d 20 2a 61 4f 72 64 65 72 42 79 3b  /.  } *aOrderBy;
35520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35530 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63  * The ORDER BY c
35540 6c 61 75 73 65 20 2a 2f 0a 20 20 2f 2a 20 4f 75  lause */.  /* Ou
35550 74 70 75 74 73 20 2a 2f 0a 20 20 73 74 72 75 63  tputs */.  struc
35560 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
35570 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65  constraint_usage
35580 20 7b 0a 20 20 20 20 69 6e 74 20 61 72 67 76 49   {.    int argvI
35590 6e 64 65 78 3b 20 20 20 20 20 20 20 20 20 20 20  ndex;           
355a0 2f 2a 20 69 66 20 3e 30 2c 20 63 6f 6e 73 74 72  /* if >0, constr
355b0 61 69 6e 74 20 69 73 20 70 61 72 74 20 6f 66 20  aint is part of 
355c0 61 72 67 76 20 74 6f 20 78 46 69 6c 74 65 72 20  argv to xFilter 
355d0 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  */.    unsigned 
355e0 63 68 61 72 20 6f 6d 69 74 3b 20 20 20 20 20 20  char omit;      
355f0 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f 64 65 20 61  /* Do not code a
35600 20 74 65 73 74 20 66 6f 72 20 74 68 69 73 20 63   test for this c
35610 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 7d  onstraint */.  }
35620 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61   *aConstraintUsa
35630 67 65 3b 0a 20 20 69 6e 74 20 69 64 78 4e 75 6d  ge;.  int idxNum
35640 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
35650 20 2f 2a 20 4e 75 6d 62 65 72 20 75 73 65 64 20   /* Number used 
35660 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68 65 20  to identify the 
35670 69 6e 64 65 78 20 2a 2f 0a 20 20 63 68 61 72 20  index */.  char 
35680 2a 69 64 78 53 74 72 3b 20 20 20 20 20 20 20 20  *idxStr;        
35690 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 2c        /* String,
356a0 20 70 6f 73 73 69 62 6c 79 20 6f 62 74 61 69 6e   possibly obtain
356b0 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ed from sqlite3_
356c0 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 69 6e 74 20  malloc */.  int 
356d0 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72  needToFreeIdxStr
356e0 3b 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 69  ;      /* Free i
356f0 64 78 53 74 72 20 75 73 69 6e 67 20 73 71 6c 69  dxStr using sqli
35700 74 65 33 5f 66 72 65 65 28 29 20 69 66 20 74 72  te3_free() if tr
35710 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 64 65  ue */.  int orde
35720 72 42 79 43 6f 6e 73 75 6d 65 64 3b 20 20 20 20  rByConsumed;    
35730 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6f 75     /* True if ou
35740 74 70 75 74 20 69 73 20 61 6c 72 65 61 64 79 20  tput is already 
35750 6f 72 64 65 72 65 64 20 2a 2f 0a 20 20 64 6f 75  ordered */.  dou
35760 62 6c 65 20 65 73 74 69 6d 61 74 65 64 43 6f 73  ble estimatedCos
35770 74 3b 20 20 20 20 20 20 2f 2a 20 45 73 74 69 6d  t;      /* Estim
35780 61 74 65 64 20 63 6f 73 74 20 6f 66 20 75 73 69  ated cost of usi
35790 6e 67 20 74 68 69 73 20 69 6e 64 65 78 20 2a 2f  ng this index */
357a0 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .};.#define SQLI
357b0 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
357c0 49 4e 54 5f 45 51 20 20 20 20 32 0a 23 64 65 66  INT_EQ    2.#def
357d0 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  ine SQLITE_INDEX
357e0 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 54 20 20  _CONSTRAINT_GT  
357f0 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    4.#define SQLI
35800 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
35810 49 4e 54 5f 4c 45 20 20 20 20 38 0a 23 64 65 66  INT_LE    8.#def
35820 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  ine SQLITE_INDEX
35830 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54 20 20  _CONSTRAINT_LT  
35840 20 20 31 36 0a 23 64 65 66 69 6e 65 20 53 51 4c    16.#define SQL
35850 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
35860 41 49 4e 54 5f 47 45 20 20 20 20 33 32 0a 23 64  AINT_GE    32.#d
35870 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44  efine SQLITE_IND
35880 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4d 41  EX_CONSTRAINT_MA
35890 54 43 48 20 36 34 0a 0a 2f 2a 0a 2a 2a 20 43 41  TCH 64../*.** CA
358a0 50 49 33 52 45 46 3a 20 52 65 67 69 73 74 65 72  PI3REF: Register
358b0 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65   A Virtual Table
358c0 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
358d0 7b 48 31 38 32 30 30 7d 20 3c 53 32 30 34 30 30  {H18200} <S20400
358e0 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  >.** EXPERIMENTA
358f0 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  L.**.** This rou
35900 74 69 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20  tine is used to 
35910 72 65 67 69 73 74 65 72 20 61 20 6e 65 77 20 5b  register a new [
35920 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
35930 64 75 6c 65 5d 20 6e 61 6d 65 2e 0a 2a 2a 20 4d  dule] name..** M
35940 6f 64 75 6c 65 20 6e 61 6d 65 73 20 6d 75 73 74  odule names must
35950 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20 62   be registered b
35960 65 66 6f 72 65 0a 2a 2a 20 63 72 65 61 74 69 6e  efore.** creatin
35970 67 20 61 20 6e 65 77 20 5b 76 69 72 74 75 61 6c  g a new [virtual
35980 20 74 61 62 6c 65 5d 20 75 73 69 6e 67 20 74 68   table] using th
35990 65 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 62 65 66  e module, or bef
359a0 6f 72 65 20 75 73 69 6e 67 20 61 0a 2a 2a 20 70  ore using a.** p
359b0 72 65 65 78 69 73 74 69 6e 67 20 5b 76 69 72 74  reexisting [virt
359c0 75 61 6c 20 74 61 62 6c 65 5d 20 66 6f 72 20 74  ual table] for t
359d0 68 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a  he module..**.**
359e0 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65   The module name
359f0 20 69 73 20 72 65 67 69 73 74 65 72 65 64 20 6f   is registered o
35a00 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  n the [database 
35a10 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63  connection] spec
35a20 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 20  ified.** by the 
35a30 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e  first parameter.
35a40 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68    The name of th
35a50 65 20 6d 6f 64 75 6c 65 20 69 73 20 67 69 76 65  e module is give
35a60 6e 20 62 79 20 74 68 65 20 0a 2a 2a 20 73 65 63  n by the .** sec
35a70 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20  ond parameter.  
35a80 54 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65  The third parame
35a90 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ter is a pointer
35aa0 20 74 6f 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65   to.** the imple
35ab0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
35ac0 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20   [virtual table 
35ad0 6d 6f 64 75 6c 65 5d 2e 20 20 20 54 68 65 20 66  module].   The f
35ae0 6f 75 72 74 68 0a 2a 2a 20 70 61 72 61 6d 65 74  ourth.** paramet
35af0 65 72 20 69 73 20 61 6e 20 61 72 62 69 74 72 61  er is an arbitra
35b00 72 79 20 63 6c 69 65 6e 74 20 64 61 74 61 20 70  ry client data p
35b10 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 20 70  ointer that is p
35b20 61 73 73 65 64 20 74 68 72 6f 75 67 68 0a 2a 2a  assed through.**
35b30 20 69 6e 74 6f 20 74 68 65 20 5b 78 43 72 65 61   into the [xCrea
35b40 74 65 5d 20 61 6e 64 20 5b 78 43 6f 6e 6e 65 63  te] and [xConnec
35b50 74 5d 20 6d 65 74 68 6f 64 73 20 6f 66 20 74 68  t] methods of th
35b60 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
35b70 6d 6f 64 75 6c 65 0a 2a 2a 20 77 68 65 6e 20 61  module.** when a
35b80 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74 61 62   new virtual tab
35b90 6c 65 20 69 73 20 62 65 20 62 65 69 6e 67 20 63  le is be being c
35ba0 72 65 61 74 65 64 20 6f 72 20 72 65 69 6e 69 74  reated or reinit
35bb0 69 61 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  ialized..**.** T
35bc0 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 68 61  his interface ha
35bd0 73 20 65 78 61 63 74 6c 79 20 74 68 65 20 73 61  s exactly the sa
35be0 6d 65 20 65 66 66 65 63 74 20 61 73 20 63 61 6c  me effect as cal
35bf0 6c 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  ling.** [sqlite3
35c00 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76  _create_module_v
35c10 32 28 29 5d 20 77 69 74 68 20 61 20 4e 55 4c 4c  2()] with a NULL
35c20 20 63 6c 69 65 6e 74 20 64 61 74 61 20 64 65 73   client data des
35c30 74 72 75 63 74 6f 72 2e 0a 2a 2f 0a 53 51 4c 49  tructor..*/.SQLI
35c40 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58  TE_API SQLITE_EX
35c50 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73  PERIMENTAL int s
35c60 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f  qlite3_create_mo
35c70 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 20  dule(.  sqlite3 
35c80 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
35c90 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f 6e     /* SQLite con
35ca0 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67 69 73  nection to regis
35cb0 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74 68 20  ter module with 
35cc0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
35cd0 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20  *zName,         
35ce0 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d  /* Name of the m
35cf0 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  odule */.  const
35d00 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
35d10 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f 64 73  *p,   /* Methods
35d20 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 20   for the module 
35d30 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69 65  */.  void *pClie
35d40 6e 74 44 61 74 61 20 20 20 20 20 20 20 20 20 20  ntData          
35d50 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61 20 66  /* Client data f
35d60 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e  or xCreate/xConn
35d70 65 63 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  ect */.);../*.**
35d80 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 69 73   CAPI3REF: Regis
35d90 74 65 72 20 41 20 56 69 72 74 75 61 6c 20 54 61  ter A Virtual Ta
35da0 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  ble Implementati
35db0 6f 6e 20 7b 48 31 38 32 31 30 7d 20 3c 53 32 30  on {H18210} <S20
35dc0 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45  400>.** EXPERIME
35dd0 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  NTAL.**.** This 
35de0 72 6f 75 74 69 6e 65 20 69 73 20 69 64 65 6e 74  routine is ident
35df0 69 63 61 6c 20 74 6f 20 74 68 65 20 5b 73 71 6c  ical to the [sql
35e00 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
35e10 6c 65 28 29 5d 20 6d 65 74 68 6f 64 2c 0a 2a 2a  le()] method,.**
35e20 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20   except that it 
35e30 68 61 73 20 61 6e 20 65 78 74 72 61 20 70 61 72  has an extra par
35e40 61 6d 65 74 65 72 20 74 6f 20 73 70 65 63 69 66  ameter to specif
35e50 79 20 0a 2a 2a 20 61 20 64 65 73 74 72 75 63 74  y .** a destruct
35e60 6f 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20  or function for 
35e70 74 68 65 20 63 6c 69 65 6e 74 20 64 61 74 61 20  the client data 
35e80 70 6f 69 6e 74 65 72 2e 20 20 53 51 4c 69 74 65  pointer.  SQLite
35e90 20 77 69 6c 6c 0a 2a 2a 20 69 6e 76 6f 6b 65 20   will.** invoke 
35ea0 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66  the destructor f
35eb0 75 6e 63 74 69 6f 6e 20 28 69 66 20 69 74 20 69  unction (if it i
35ec0 73 20 6e 6f 74 20 4e 55 4c 4c 29 20 77 68 65 6e  s not NULL) when
35ed0 20 53 51 4c 69 74 65 0a 2a 2a 20 6e 6f 20 6c 6f   SQLite.** no lo
35ee0 6e 67 65 72 20 6e 65 65 64 73 20 74 68 65 20 70  nger needs the p
35ef0 43 6c 69 65 6e 74 44 61 74 61 20 70 6f 69 6e 74  ClientData point
35f00 65 72 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f  er.  .*/.SQLITE_
35f10 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52  API SQLITE_EXPER
35f20 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69  IMENTAL int sqli
35f30 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c  te3_create_modul
35f40 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20  e_v2(.  sqlite3 
35f50 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
35f60 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f 6e     /* SQLite con
35f70 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67 69 73  nection to regis
35f80 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74 68 20  ter module with 
35f90 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
35fa0 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20  *zName,         
35fb0 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d  /* Name of the m
35fc0 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  odule */.  const
35fd0 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
35fe0 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f 64 73  *p,   /* Methods
35ff0 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 20   for the module 
36000 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69 65  */.  void *pClie
36010 6e 74 44 61 74 61 2c 20 20 20 20 20 20 20 20 20  ntData,         
36020 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61 20 66  /* Client data f
36030 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e  or xCreate/xConn
36040 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64 28 2a 78  ect */.  void(*x
36050 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 20  Destroy)(void*) 
36060 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65      /* Module de
36070 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f  structor functio
36080 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  n */.);../*.** C
36090 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c  API3REF: Virtual
360a0 20 54 61 62 6c 65 20 49 6e 73 74 61 6e 63 65 20   Table Instance 
360b0 4f 62 6a 65 63 74 20 7b 48 31 38 30 31 30 7d 20  Object {H18010} 
360c0 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57  <S20400>.** KEYW
360d0 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f 76 74  ORDS: sqlite3_vt
360e0 61 62 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  ab.** EXPERIMENT
360f0 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 5b  AL.**.** Every [
36100 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
36110 64 75 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e 74 61  dule] implementa
36120 74 69 6f 6e 20 75 73 65 73 20 61 20 73 75 62 63  tion uses a subc
36130 6c 61 73 73 0a 2a 2a 20 6f 66 20 74 68 65 20 66  lass.** of the f
36140 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
36150 72 65 20 74 6f 20 64 65 73 63 72 69 62 65 20 61  re to describe a
36160 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e 73 74   particular inst
36170 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 5b  ance.** of the [
36180 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 2e 20  virtual table]. 
36190 20 45 61 63 68 20 73 75 62 63 6c 61 73 73 20 77   Each subclass w
361a0 69 6c 6c 0a 2a 2a 20 62 65 20 74 61 69 6c 6f 72  ill.** be tailor
361b0 65 64 20 74 6f 20 74 68 65 20 73 70 65 63 69 66  ed to the specif
361c0 69 63 20 6e 65 65 64 73 20 6f 66 20 74 68 65 20  ic needs of the 
361d0 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  module implement
361e0 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 70 75  ation..** The pu
361f0 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20 73 75  rpose of this su
36200 70 65 72 63 6c 61 73 73 20 69 73 20 74 6f 20 64  perclass is to d
36210 65 66 69 6e 65 20 63 65 72 74 61 69 6e 20 66 69  efine certain fi
36220 65 6c 64 73 20 74 68 61 74 20 61 72 65 0a 2a 2a  elds that are.**
36230 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6d   common to all m
36240 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  odule implementa
36250 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56 69 72  tions..**.** Vir
36260 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 65 74 68  tual tables meth
36270 6f 64 73 20 63 61 6e 20 73 65 74 20 61 6e 20 65  ods can set an e
36280 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 79 20  rror message by 
36290 61 73 73 69 67 6e 69 6e 67 20 61 0a 2a 2a 20 73  assigning a.** s
362a0 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66  tring obtained f
362b0 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72  rom [sqlite3_mpr
362c0 69 6e 74 66 28 29 5d 20 74 6f 20 7a 45 72 72 4d  intf()] to zErrM
362d0 73 67 2e 20 20 54 68 65 20 6d 65 74 68 6f 64 20  sg.  The method 
362e0 73 68 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65 20 63  should.** take c
362f0 61 72 65 20 74 68 61 74 20 61 6e 79 20 70 72 69  are that any pri
36300 6f 72 20 73 74 72 69 6e 67 20 69 73 20 66 72 65  or string is fre
36310 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20  ed by a call to 
36320 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d  [sqlite3_free()]
36330 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 61 73 73  .** prior to ass
36340 69 67 6e 69 6e 67 20 61 20 6e 65 77 20 73 74 72  igning a new str
36350 69 6e 67 20 74 6f 20 7a 45 72 72 4d 73 67 2e 20  ing to zErrMsg. 
36360 20 41 66 74 65 72 20 74 68 65 20 65 72 72 6f 72   After the error
36370 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 64   message.** is d
36380 65 6c 69 76 65 72 65 64 20 75 70 20 74 6f 20 74  elivered up to t
36390 68 65 20 63 6c 69 65 6e 74 20 61 70 70 6c 69 63  he client applic
363a0 61 74 69 6f 6e 2c 20 74 68 65 20 73 74 72 69 6e  ation, the strin
363b0 67 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61  g will be automa
363c0 74 69 63 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 64  tically.** freed
363d0 20 62 79 20 73 71 6c 69 74 65 33 5f 66 72 65 65   by sqlite3_free
363e0 28 29 20 61 6e 64 20 74 68 65 20 7a 45 72 72 4d  () and the zErrM
363f0 73 67 20 66 69 65 6c 64 20 77 69 6c 6c 20 62 65  sg field will be
36400 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 73 74 72 75   zeroed..*/.stru
36410 63 74 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  ct sqlite3_vtab 
36420 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  {.  const sqlite
36430 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  3_module *pModul
36440 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c  e;  /* The modul
36450 65 20 66 6f 72 20 74 68 69 73 20 76 69 72 74 75  e for this virtu
36460 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  al table */.  in
36470 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20  t nRef;         
36480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
36490 20 4e 4f 20 4c 4f 4e 47 45 52 20 55 53 45 44 20   NO LONGER USED 
364a0 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d  */.  char *zErrM
364b0 73 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  sg;             
364c0 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65       /* Error me
364d0 73 73 61 67 65 20 66 72 6f 6d 20 73 71 6c 69 74  ssage from sqlit
364e0 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 2a 2f 0a  e3_mprintf() */.
364f0 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62    /* Virtual tab
36500 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  le implementatio
36510 6e 73 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c  ns will typicall
36520 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c  y add additional
36530 20 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f   fields */.};../
36540 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56  *.** CAPI3REF: V
36550 69 72 74 75 61 6c 20 54 61 62 6c 65 20 43 75 72  irtual Table Cur
36560 73 6f 72 20 4f 62 6a 65 63 74 20 20 7b 48 31 38  sor Object  {H18
36570 30 32 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a  020} <S20400>.**
36580 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74   KEYWORDS: sqlit
36590 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 7b  e3_vtab_cursor {
365a0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75  virtual table cu
365b0 72 73 6f 72 7d 0a 2a 2a 20 45 58 50 45 52 49 4d  rsor}.** EXPERIM
365c0 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72  ENTAL.**.** Ever
365d0 79 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65  y [virtual table
365e0 20 6d 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 6d 65   module] impleme
365f0 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73  ntation uses a s
36600 75 62 63 6c 61 73 73 20 6f 66 20 74 68 65 0a 2a  ubclass of the.*
36610 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  * following stru
36620 63 74 75 72 65 20 74 6f 20 64 65 73 63 72 69 62  cture to describ
36630 65 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 70  e cursors that p
36640 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65 0a 2a 2a  oint into the.**
36650 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d   [virtual table]
36660 20 61 6e 64 20 61 72 65 20 75 73 65 64 0a 2a 2a   and are used.**
36670 20 74 6f 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68   to loop through
36680 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
36690 6c 65 2e 20 20 43 75 72 73 6f 72 73 20 61 72 65  le.  Cursors are
366a0 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 74   created using t
366b0 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d  he.** [sqlite3_m
366c0 6f 64 75 6c 65 2e 78 4f 70 65 6e 20 7c 20 78 4f  odule.xOpen | xO
366d0 70 65 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 20 74  pen] method of t
366e0 68 65 20 6d 6f 64 75 6c 65 20 61 6e 64 20 61 72  he module and ar
366f0 65 20 64 65 73 74 72 6f 79 65 64 0a 2a 2a 20 62  e destroyed.** b
36700 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d  y the [sqlite3_m
36710 6f 64 75 6c 65 2e 78 43 6c 6f 73 65 20 7c 20 78  odule.xClose | x
36720 43 6c 6f 73 65 5d 20 6d 65 74 68 6f 64 2e 20 20  Close] method.  
36730 43 75 73 73 6f 72 73 20 61 72 65 20 75 73 65 64  Cussors are used
36740 0a 2a 2a 20 62 79 20 74 68 65 20 5b 78 46 69 6c  .** by the [xFil
36750 74 65 72 5d 2c 20 5b 78 4e 65 78 74 5d 2c 20 5b  ter], [xNext], [
36760 78 45 6f 66 5d 2c 20 5b 78 43 6f 6c 75 6d 6e 5d  xEof], [xColumn]
36770 2c 20 61 6e 64 20 5b 78 52 6f 77 69 64 5d 20 6d  , and [xRowid] m
36780 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 74 68 65  ethods.** of the
36790 20 6d 6f 64 75 6c 65 2e 20 20 45 61 63 68 20 6d   module.  Each m
367a0 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  odule implementa
367b0 74 69 6f 6e 20 77 69 6c 6c 20 64 65 66 69 6e 65  tion will define
367c0 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  .** the content 
367d0 6f 66 20 61 20 63 75 72 73 6f 72 20 73 74 72 75  of a cursor stru
367e0 63 74 75 72 65 20 74 6f 20 73 75 69 74 20 69 74  cture to suit it
367f0 73 20 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a 2a 0a  s own needs..**.
36800 2a 2a 20 54 68 69 73 20 73 75 70 65 72 63 6c 61  ** This supercla
36810 73 73 20 65 78 69 73 74 73 20 69 6e 20 6f 72 64  ss exists in ord
36820 65 72 20 74 6f 20 64 65 66 69 6e 65 20 66 69 65  er to define fie
36830 6c 64 73 20 6f 66 20 74 68 65 20 63 75 72 73 6f  lds of the curso
36840 72 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 63 6f  r that.** are co
36850 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d 70 6c  mmon to all impl
36860 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a  ementations..*/.
36870 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76  struct sqlite3_v
36880 74 61 62 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73  tab_cursor {.  s
36890 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
368a0 61 62 3b 20 20 20 20 20 20 2f 2a 20 56 69 72 74  ab;      /* Virt
368b0 75 61 6c 20 74 61 62 6c 65 20 6f 66 20 74 68 69  ual table of thi
368c0 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 2f 2a  s cursor */.  /*
368d0 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69   Virtual table i
368e0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77  mplementations w
368f0 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20 61 64  ill typically ad
36900 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65  d additional fie
36910 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  lds */.};../*.**
36920 20 43 41 50 49 33 52 45 46 3a 20 44 65 63 6c 61   CAPI3REF: Decla
36930 72 65 20 54 68 65 20 53 63 68 65 6d 61 20 4f 66  re The Schema Of
36940 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65   A Virtual Table
36950 20 7b 48 31 38 32 38 30 7d 20 3c 53 32 30 34 30   {H18280} <S2040
36960 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  0>.** EXPERIMENT
36970 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 78 43  AL.**.** The [xC
36980 72 65 61 74 65 5d 20 61 6e 64 20 5b 78 43 6f 6e  reate] and [xCon
36990 6e 65 63 74 5d 20 6d 65 74 68 6f 64 73 20 6f 66  nect] methods of
369a0 20 61 0a 2a 2a 20 5b 76 69 72 74 75 61 6c 20 74   a.** [virtual t
369b0 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 63 61 6c  able module] cal
369c0 6c 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65  l this interface
369d0 0a 2a 2a 20 74 6f 20 64 65 63 6c 61 72 65 20 74  .** to declare t
369e0 68 65 20 66 6f 72 6d 61 74 20 28 74 68 65 20 6e  he format (the n
369f0 61 6d 65 73 20 61 6e 64 20 64 61 74 61 74 79 70  ames and datatyp
36a00 65 73 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  es of the column
36a10 73 29 20 6f 66 0a 2a 2a 20 74 68 65 20 76 69 72  s) of.** the vir
36a20 74 75 61 6c 20 74 61 62 6c 65 73 20 74 68 65 79  tual tables they
36a30 20 69 6d 70 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 53   implement..*/.S
36a40 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45  QLITE_API SQLITE
36a50 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e  _EXPERIMENTAL in
36a60 74 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72  t sqlite3_declar
36a70 65 5f 76 74 61 62 28 73 71 6c 69 74 65 33 2a 2c  e_vtab(sqlite3*,
36a80 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 51   const char *zSQ
36a90 4c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  L);../*.** CAPI3
36aa0 52 45 46 3a 20 4f 76 65 72 6c 6f 61 64 20 41 20  REF: Overload A 
36ab0 46 75 6e 63 74 69 6f 6e 20 46 6f 72 20 41 20 56  Function For A V
36ac0 69 72 74 75 61 6c 20 54 61 62 6c 65 20 7b 48 31  irtual Table {H1
36ad0 38 33 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a  8300} <S20400>.*
36ae0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
36af0 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 61 62  *.** Virtual tab
36b00 6c 65 73 20 63 61 6e 20 70 72 6f 76 69 64 65 20  les can provide 
36b10 61 6c 74 65 72 6e 61 74 69 76 65 20 69 6d 70 6c  alternative impl
36b20 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 66  ementations of f
36b30 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 75 73 69 6e  unctions.** usin
36b40 67 20 74 68 65 20 5b 78 46 69 6e 64 46 75 6e 63  g the [xFindFunc
36b50 74 69 6f 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 20  tion] method of 
36b60 74 68 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62  the [virtual tab
36b70 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20 20 0a 2a 2a  le module].  .**
36b80 20 42 75 74 20 67 6c 6f 62 61 6c 20 76 65 72 73   But global vers
36b90 69 6f 6e 73 20 6f 66 20 74 68 6f 73 65 20 66 75  ions of those fu
36ba0 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6d 75 73 74 20  nctions.** must 
36bb0 65 78 69 73 74 20 69 6e 20 6f 72 64 65 72 20 74  exist in order t
36bc0 6f 20 62 65 20 6f 76 65 72 6c 6f 61 64 65 64 2e  o be overloaded.
36bd0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20  .**.** This API 
36be0 6d 61 6b 65 73 20 73 75 72 65 20 61 20 67 6c 6f  makes sure a glo
36bf0 62 61 6c 20 76 65 72 73 69 6f 6e 20 6f 66 20 61  bal version of a
36c00 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61   function with a
36c10 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 6e   particular.** n
36c20 61 6d 65 20 61 6e 64 20 6e 75 6d 62 65 72 20 6f  ame and number o
36c30 66 20 70 61 72 61 6d 65 74 65 72 73 20 65 78 69  f parameters exi
36c40 73 74 73 2e 20 20 49 66 20 6e 6f 20 73 75 63 68  sts.  If no such
36c50 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73   function exists
36c60 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 69 73 20  .** before this 
36c70 41 50 49 20 69 73 20 63 61 6c 6c 65 64 2c 20 61  API is called, a
36c80 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20 69 73   new function is
36c90 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20 69   created.  The i
36ca0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a  mplementation.**
36cb0 20 6f 66 20 74 68 65 20 6e 65 77 20 66 75 6e 63   of the new func
36cc0 74 69 6f 6e 20 61 6c 77 61 79 73 20 63 61 75 73  tion always caus
36cd0 65 73 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20  es an exception 
36ce0 74 6f 20 62 65 20 74 68 72 6f 77 6e 2e 20 20 53  to be thrown.  S
36cf0 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20 66 75 6e  o.** the new fun
36d00 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 67 6f 6f  ction is not goo
36d10 64 20 66 6f 72 20 61 6e 79 74 68 69 6e 67 20 62  d for anything b
36d20 79 20 69 74 73 65 6c 66 2e 20 20 49 74 73 20 6f  y itself.  Its o
36d30 6e 6c 79 0a 2a 2a 20 70 75 72 70 6f 73 65 20 69  nly.** purpose i
36d40 73 20 74 6f 20 62 65 20 61 20 70 6c 61 63 65 68  s to be a placeh
36d50 6f 6c 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 74  older function t
36d60 68 61 74 20 63 61 6e 20 62 65 20 6f 76 65 72 6c  hat can be overl
36d70 6f 61 64 65 64 0a 2a 2a 20 62 79 20 61 20 5b 76  oaded.** by a [v
36d80 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 2e 0a 2a  irtual table]..*
36d90 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c  /.SQLITE_API SQL
36da0 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c  ITE_EXPERIMENTAL
36db0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 76 65   int sqlite3_ove
36dc0 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 73  rload_function(s
36dd0 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63  qlite3*, const c
36de0 68 61 72 20 2a 7a 46 75 6e 63 4e 61 6d 65 2c 20  har *zFuncName, 
36df0 69 6e 74 20 6e 41 72 67 29 3b 0a 0a 2f 2a 0a 2a  int nArg);../*.*
36e00 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20  * The interface 
36e10 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74  to the virtual-t
36e20 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 64  able mechanism d
36e30 65 66 69 6e 65 64 20 61 62 6f 76 65 20 28 62 61  efined above (ba
36e40 63 6b 20 75 70 0a 2a 2a 20 74 6f 20 61 20 63 6f  ck up.** to a co
36e50 6d 6d 65 6e 74 20 72 65 6d 61 72 6b 61 62 6c 79  mment remarkably
36e60 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 69 73   similar to this
36e70 20 6f 6e 65 29 20 69 73 20 63 75 72 72 65 6e 74   one) is current
36e80 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a 2a  ly considered.**
36e90 20 74 6f 20 62 65 20 65 78 70 65 72 69 6d 65 6e   to be experimen
36ea0 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 65 72 66  tal.  The interf
36eb0 61 63 65 20 6d 69 67 68 74 20 63 68 61 6e 67 65  ace might change
36ec0 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65   in incompatible
36ed0 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20 74 68 69   ways..** If thi
36ee0 73 20 69 73 20 61 20 70 72 6f 62 6c 65 6d 20 66  s is a problem f
36ef0 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 20 75  or you, do not u
36f00 73 65 20 74 68 65 20 69 6e 74 65 72 66 61 63 65  se the interface
36f10 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0a 2a   at this time..*
36f20 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 76 69  *.** When the vi
36f30 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63 68  rtual-table mech
36f40 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 7a 65 73  anism stabilizes
36f50 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c 61 72  , we will declar
36f60 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 66 61  e the.** interfa
36f70 63 65 20 66 69 78 65 64 2c 20 73 75 70 70 6f 72  ce fixed, suppor
36f80 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74 65 6c  t it indefinitel
36f90 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68  y, and remove th
36fa0 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2a 0a 2a  is comment..**.*
36fb0 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  ***** EXPERIMENT
36fc0 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 74 6f 20  AL - subject to 
36fd0 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e  change without n
36fe0 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  otice **********
36ff0 2a 2a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43  ****.*/../*.** C
37000 41 50 49 33 52 45 46 3a 20 41 20 48 61 6e 64 6c  API3REF: A Handl
37010 65 20 54 6f 20 41 6e 20 4f 70 65 6e 20 42 4c 4f  e To An Open BLO
37020 42 20 7b 48 31 37 38 30 30 7d 20 3c 53 33 30 32  B {H17800} <S302
37030 33 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  30>.** KEYWORDS:
37040 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 7d 20 7b   {BLOB handle} {
37050 42 4c 4f 42 20 68 61 6e 64 6c 65 73 7d 0a 2a 2a  BLOB handles}.**
37060 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
37070 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72  of this object r
37080 65 70 72 65 73 65 6e 74 73 20 61 6e 20 6f 70 65  epresents an ope
37090 6e 20 42 4c 4f 42 20 6f 6e 20 77 68 69 63 68 0a  n BLOB on which.
370a0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ** [sqlite3_blob
370b0 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65 6e  _open | incremen
370c0 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 63 61  tal BLOB I/O] ca
370d0 6e 20 62 65 20 70 65 72 66 6f 72 6d 65 64 2e 0a  n be performed..
370e0 2a 2a 20 4f 62 6a 65 63 74 73 20 6f 66 20 74 68  ** Objects of th
370f0 69 73 20 74 79 70 65 20 61 72 65 20 63 72 65 61  is type are crea
37100 74 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f  ted by [sqlite3_
37110 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20  blob_open()].** 
37120 61 6e 64 20 64 65 73 74 72 6f 79 65 64 20 62 79  and destroyed by
37130 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63   [sqlite3_blob_c
37140 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 20  lose()]..** The 
37150 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65  [sqlite3_blob_re
37160 61 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  ad()] and [sqlit
37170 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29 5d  e3_blob_write()]
37180 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 63   interfaces.** c
37190 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 72 65  an be used to re
371a0 61 64 20 6f 72 20 77 72 69 74 65 20 73 6d 61 6c  ad or write smal
371b0 6c 20 73 75 62 73 65 63 74 69 6f 6e 73 20 6f 66  l subsections of
371c0 20 74 68 65 20 42 4c 4f 42 2e 0a 2a 2a 20 54 68   the BLOB..** Th
371d0 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  e [sqlite3_blob_
371e0 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61  bytes()] interfa
371f0 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 73  ce returns the s
37200 69 7a 65 20 6f 66 20 74 68 65 20 42 4c 4f 42 20  ize of the BLOB 
37210 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 74 79 70  in bytes..*/.typ
37220 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
37230 74 65 33 5f 62 6c 6f 62 20 73 71 6c 69 74 65 33  te3_blob sqlite3
37240 5f 62 6c 6f 62 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  _blob;../*.** CA
37250 50 49 33 52 45 46 3a 20 4f 70 65 6e 20 41 20 42  PI3REF: Open A B
37260 4c 4f 42 20 46 6f 72 20 49 6e 63 72 65 6d 65 6e  LOB For Incremen
37270 74 61 6c 20 49 2f 4f 20 7b 48 31 37 38 31 30 7d  tal I/O {H17810}
37280 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20   <S30230>.**.** 
37290 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 73 20  This interfaces 
372a0 6f 70 65 6e 73 20 61 20 5b 42 4c 4f 42 20 68 61  opens a [BLOB ha
372b0 6e 64 6c 65 20 7c 20 68 61 6e 64 6c 65 5d 20 74  ndle | handle] t
372c0 6f 20 74 68 65 20 42 4c 4f 42 20 6c 6f 63 61 74  o the BLOB locat
372d0 65 64 0a 2a 2a 20 69 6e 20 72 6f 77 20 69 52 6f  ed.** in row iRo
372e0 77 2c 20 63 6f 6c 75 6d 6e 20 7a 43 6f 6c 75 6d  w, column zColum
372f0 6e 2c 20 74 61 62 6c 65 20 7a 54 61 62 6c 65 20  n, table zTable 
37300 69 6e 20 64 61 74 61 62 61 73 65 20 7a 44 62 3b  in database zDb;
37310 0a 2a 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f 72  .** in other wor
37320 64 73 2c 20 74 68 65 20 73 61 6d 65 20 42 4c 4f  ds, the same BLO
37330 42 20 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20  B that would be 
37340 73 65 6c 65 63 74 65 64 20 62 79 3a 0a 2a 2a 0a  selected by:.**.
37350 2a 2a 20 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20  ** <pre>.**     
37360 53 45 4c 45 43 54 20 7a 43 6f 6c 75 6d 6e 20 46  SELECT zColumn F
37370 52 4f 4d 20 7a 44 62 2e 7a 54 61 62 6c 65 20 57  ROM zDb.zTable W
37380 48 45 52 45 20 5b 72 6f 77 69 64 5d 20 3d 20 69  HERE [rowid] = i
37390 52 6f 77 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 20 7b  Row;.** </pre> {
373a0 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  END}.**.** If th
373b0 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65  e flags paramete
373c0 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  r is non-zero, t
373d0 68 65 6e 20 74 68 65 20 42 4c 4f 42 20 69 73 20  hen the BLOB is 
373e0 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 0a  opened for read.
373f0 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 61 63 63  ** and write acc
37400 65 73 73 2e 20 49 66 20 69 74 20 69 73 20 7a 65  ess. If it is ze
37410 72 6f 2c 20 74 68 65 20 42 4c 4f 42 20 69 73 20  ro, the BLOB is 
37420 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 20  opened for read 
37430 61 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  access..**.** No
37440 74 65 20 74 68 61 74 20 74 68 65 20 64 61 74 61  te that the data
37450 62 61 73 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74  base name is not
37460 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 74 68   the filename th
37470 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74  at contains.** t
37480 68 65 20 64 61 74 61 62 61 73 65 20 62 75 74 20  he database but 
37490 72 61 74 68 65 72 20 74 68 65 20 73 79 6d 62 6f  rather the symbo
374a0 6c 69 63 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  lic name of the 
374b0 64 61 74 61 62 61 73 65 20 74 68 61 74 0a 2a 2a  database that.**
374c0 20 69 73 20 61 73 73 69 67 6e 65 64 20 77 68 65   is assigned whe
374d0 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  n the database i
374e0 73 20 63 6f 6e 6e 65 63 74 65 64 20 75 73 69 6e  s connected usin
374f0 67 20 5b 41 54 54 41 43 48 5d 2e 0a 2a 2a 20 46  g [ATTACH]..** F
37500 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  or the main data
37510 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 20 64  base file, the d
37520 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20  atabase name is 
37530 22 6d 61 69 6e 22 2e 0a 2a 2a 20 46 6f 72 20 54  "main"..** For T
37540 45 4d 50 20 74 61 62 6c 65 73 2c 20 74 68 65 20  EMP tables, the 
37550 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73  database name is
37560 20 22 74 65 6d 70 22 2e 0a 2a 2a 0a 2a 2a 20 4f   "temp"..**.** O
37570 6e 20 73 75 63 63 65 73 73 2c 20 5b 53 51 4c 49  n success, [SQLI
37580 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75 72 6e  TE_OK] is return
37590 65 64 20 61 6e 64 20 74 68 65 20 6e 65 77 20 5b  ed and the new [
375a0 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 73 20  BLOB handle] is 
375b0 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70  written.** to *p
375c0 70 42 6c 6f 62 2e 20 4f 74 68 65 72 77 69 73 65  pBlob. Otherwise
375d0 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d   an [error code]
375e0 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
375f0 20 2a 70 70 42 6c 6f 62 20 69 73 20 73 65 74 0a   *ppBlob is set.
37600 2a 2a 20 74 6f 20 62 65 20 61 20 6e 75 6c 6c 20  ** to be a null 
37610 70 6f 69 6e 74 65 72 2e 0a 2a 2a 20 54 68 69 73  pointer..** This
37620 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 74   function sets t
37630 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
37640 6e 65 63 74 69 6f 6e 5d 20 65 72 72 6f 72 20 63  nection] error c
37650 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65 0a  ode and message.
37660 2a 2a 20 61 63 63 65 73 73 69 62 6c 65 20 76 69  ** accessible vi
37670 61 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  a [sqlite3_errco
37680 64 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  de()] and [sqlit
37690 65 33 5f 65 72 72 6d 73 67 28 29 5d 20 61 6e 64  e3_errmsg()] and
376a0 20 72 65 6c 61 74 65 64 0a 2a 2a 20 66 75 6e 63   related.** func
376b0 74 69 6f 6e 73 2e 20 20 4e 6f 74 65 20 74 68 61  tions.  Note tha
376c0 74 20 74 68 65 20 2a 70 70 42 6c 6f 62 20 76 61  t the *ppBlob va
376d0 72 69 61 62 6c 65 20 69 73 20 61 6c 77 61 79 73  riable is always
376e0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 6e 20   initialized in 
376f0 61 0a 2a 2a 20 77 61 79 20 74 68 61 74 20 6d 61  a.** way that ma
37700 6b 65 73 20 69 74 20 73 61 66 65 20 74 6f 20 69  kes it safe to i
37710 6e 76 6f 6b 65 20 5b 73 71 6c 69 74 65 33 5f 62  nvoke [sqlite3_b
37720 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 20 6f 6e 20  lob_close()] on 
37730 2a 70 70 42 6c 6f 62 0a 2a 2a 20 72 65 67 61 72  *ppBlob.** regar
37740 64 6c 65 73 73 20 6f 66 20 74 68 65 20 73 75 63  dless of the suc
37750 63 65 73 73 20 6f 72 20 66 61 69 6c 75 72 65 20  cess or failure 
37760 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  of this routine.
37770 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 6f  .**.** If the ro
37780 77 20 74 68 61 74 20 61 20 42 4c 4f 42 20 68 61  w that a BLOB ha
37790 6e 64 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69  ndle points to i
377a0 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e  s modified by an
377b0 0a 2a 2a 20 5b 55 50 44 41 54 45 5d 2c 20 5b 44  .** [UPDATE], [D
377c0 45 4c 45 54 45 5d 2c 20 6f 72 20 62 79 20 5b 4f  ELETE], or by [O
377d0 4e 20 43 4f 4e 46 4c 49 43 54 5d 20 73 69 64 65  N CONFLICT] side
377e0 2d 65 66 66 65 63 74 73 0a 2a 2a 20 74 68 65 6e  -effects.** then
377f0 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65   the BLOB handle
37800 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 22 65   is marked as "e
37810 78 70 69 72 65 64 22 2e 0a 2a 2a 20 54 68 69 73  xpired"..** This
37820 20 69 73 20 74 72 75 65 20 69 66 20 61 6e 79 20   is true if any 
37830 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 6f  column of the ro
37840 77 20 69 73 20 63 68 61 6e 67 65 64 2c 20 65 76  w is changed, ev
37850 65 6e 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f  en a column.** o
37860 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e  ther than the on
37870 65 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c  e the BLOB handl
37880 65 20 69 73 20 6f 70 65 6e 20 6f 6e 2e 0a 2a 2a  e is open on..**
37890 20 43 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74   Calls to [sqlit
378a0 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d 20  e3_blob_read()] 
378b0 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f  and [sqlite3_blo
378c0 62 5f 77 72 69 74 65 28 29 5d 20 66 6f 72 0a 2a  b_write()] for.*
378d0 2a 20 61 20 65 78 70 69 72 65 64 20 42 4c 4f 42  * a expired BLOB
378e0 20 68 61 6e 64 6c 65 20 66 61 69 6c 20 77 69 74   handle fail wit
378f0 68 20 61 6e 20 72 65 74 75 72 6e 20 63 6f 64 65  h an return code
37900 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52   of [SQLITE_ABOR
37910 54 5d 2e 0a 2a 2a 20 43 68 61 6e 67 65 73 20 77  T]..** Changes w
37920 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 42 4c  ritten into a BL
37930 4f 42 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20  OB prior to the 
37940 42 4c 4f 42 20 65 78 70 69 72 69 6e 67 20 61 72  BLOB expiring ar
37950 65 20 6e 6f 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63  e not.** rollbac
37960 6b 20 62 79 20 74 68 65 20 65 78 70 69 72 61 74  k by the expirat
37970 69 6f 6e 20 6f 66 20 74 68 65 20 42 4c 4f 42 2e  ion of the BLOB.
37980 20 20 53 75 63 68 20 63 68 61 6e 67 65 73 20 77    Such changes w
37990 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a  ill eventually.*
379a0 2a 20 63 6f 6d 6d 69 74 20 69 66 20 74 68 65 20  * commit if the 
379b0 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6e 74  transaction cont
379c0 69 6e 75 65 73 20 74 6f 20 63 6f 6d 70 6c 65 74  inues to complet
379d0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74  ion..**.** Use t
379e0 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  he [sqlite3_blob
379f0 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 66  _bytes()] interf
37a00 61 63 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  ace to determine
37a10 20 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20   the size of.** 
37a20 74 68 65 20 6f 70 65 6e 65 64 20 62 6c 6f 62 2e  the opened blob.
37a30 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20 61 20    The size of a 
37a40 62 6c 6f 62 20 6d 61 79 20 6e 6f 74 20 62 65 20  blob may not be 
37a50 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 0a  changed by this.
37a60 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 20 20 55  ** interface.  U
37a70 73 65 20 74 68 65 20 5b 55 50 44 41 54 45 5d 20  se the [UPDATE] 
37a80 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 74 6f 20 63  SQL command to c
37a90 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f  hange the size o
37aa0 66 20 61 0a 2a 2a 20 62 6c 6f 62 2e 0a 2a 2a 0a  f a.** blob..**.
37ab0 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  ** The [sqlite3_
37ac0 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d  bind_zeroblob()]
37ad0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 65   and [sqlite3_re
37ae0 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d  sult_zeroblob()]
37af0 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 61   interfaces.** a
37b00 6e 64 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20  nd the built-in 
37b10 5b 7a 65 72 6f 62 6c 6f 62 5d 20 53 51 4c 20 66  [zeroblob] SQL f
37b20 75 6e 63 74 69 6f 6e 20 63 61 6e 20 62 65 20 75  unction can be u
37b30 73 65 64 2c 20 69 66 20 64 65 73 69 72 65 64 2c  sed, if desired,
37b40 0a 2a 2a 20 74 6f 20 63 72 65 61 74 65 20 61 6e  .** to create an
37b50 20 65 6d 70 74 79 2c 20 7a 65 72 6f 2d 66 69 6c   empty, zero-fil
37b60 6c 65 64 20 62 6c 6f 62 20 69 6e 20 77 68 69 63  led blob in whic
37b70 68 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69  h to read or wri
37b80 74 65 20 75 73 69 6e 67 0a 2a 2a 20 74 68 69 73  te using.** this
37b90 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a   interface..**.*
37ba0 2a 20 54 6f 20 61 76 6f 69 64 20 61 20 72 65 73  * To avoid a res
37bb0 6f 75 72 63 65 20 6c 65 61 6b 2c 20 65 76 65 72  ource leak, ever
37bc0 79 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e  y open [BLOB han
37bd0 64 6c 65 5d 20 73 68 6f 75 6c 64 20 65 76 65 6e  dle] should even
37be0 74 75 61 6c 6c 79 0a 2a 2a 20 62 65 20 72 65 6c  tually.** be rel
37bf0 65 61 73 65 64 20 62 79 20 61 20 63 61 6c 6c 20  eased by a call 
37c00 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  to [sqlite3_blob
37c10 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a  _close()]..**.**
37c20 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
37c30 2a 20 5b 48 31 37 38 31 33 5d 20 5b 48 31 37 38  * [H17813] [H178
37c40 31 34 5d 20 5b 48 31 37 38 31 36 5d 20 5b 48 31  14] [H17816] [H1
37c50 37 38 31 39 5d 20 5b 48 31 37 38 32 31 5d 20 5b  7819] [H17821] [
37c60 48 31 37 38 32 34 5d 0a 2a 2f 0a 53 51 4c 49 54  H17824].*/.SQLIT
37c70 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
37c80 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73  3_blob_open(.  s
37c90 71 6c 69 74 65 33 2a 2c 0a 20 20 63 6f 6e 73 74  qlite3*,.  const
37ca0 20 63 68 61 72 20 2a 7a 44 62 2c 0a 20 20 63 6f   char *zDb,.  co
37cb0 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65  nst char *zTable
37cc0 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
37cd0 7a 43 6f 6c 75 6d 6e 2c 0a 20 20 73 71 6c 69 74  zColumn,.  sqlit
37ce0 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 2c 0a 20  e3_int64 iRow,. 
37cf0 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 73 71   int flags,.  sq
37d00 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 42  lite3_blob **ppB
37d10 6c 6f 62 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  lob.);../*.** CA
37d20 50 49 33 52 45 46 3a 20 43 6c 6f 73 65 20 41 20  PI3REF: Close A 
37d30 42 4c 4f 42 20 48 61 6e 64 6c 65 20 7b 48 31 37  BLOB Handle {H17
37d40 38 33 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a  830} <S30230>.**
37d50 0a 2a 2a 20 43 6c 6f 73 65 73 20 61 6e 20 6f 70  .** Closes an op
37d60 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d  en [BLOB handle]
37d70 2e 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20  ..**.** Closing 
37d80 61 20 42 4c 4f 42 20 73 68 61 6c 6c 20 63 61 75  a BLOB shall cau
37d90 73 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  se the current t
37da0 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 63 6f  ransaction to co
37db0 6d 6d 69 74 0a 2a 2a 20 69 66 20 74 68 65 72 65  mmit.** if there
37dc0 20 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 42 4c   are no other BL
37dd0 4f 42 73 2c 20 6e 6f 20 70 65 6e 64 69 6e 67 20  OBs, no pending 
37de0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
37df0 6e 74 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a 20  nts, and the.** 
37e00 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
37e10 69 6f 6e 20 69 73 20 69 6e 20 5b 61 75 74 6f 63  ion is in [autoc
37e20 6f 6d 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a 2a 20  ommit mode]..** 
37e30 49 66 20 61 6e 79 20 77 72 69 74 65 73 20 77 65  If any writes we
37e40 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20 42  re made to the B
37e50 4c 4f 42 2c 20 74 68 65 79 20 6d 69 67 68 74 20  LOB, they might 
37e60 62 65 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65  be held in cache
37e70 0a 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 63 6c  .** until the cl
37e80 6f 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 66  ose operation if
37e90 20 74 68 65 79 20 77 69 6c 6c 20 66 69 74 2e 0a   they will fit..
37ea0 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 74 68  **.** Closing th
37eb0 65 20 42 4c 4f 42 20 6f 66 74 65 6e 20 66 6f 72  e BLOB often for
37ec0 63 65 73 20 74 68 65 20 63 68 61 6e 67 65 73 0a  ces the changes.
37ed0 2a 2a 20 6f 75 74 20 74 6f 20 64 69 73 6b 20 61  ** out to disk a
37ee0 6e 64 20 73 6f 20 69 66 20 61 6e 79 20 49 2f 4f  nd so if any I/O
37ef0 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20 74   errors occur, t
37f00 68 65 79 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20  hey will likely 
37f10 6f 63 63 75 72 0a 2a 2a 20 61 74 20 74 68 65 20  occur.** at the 
37f20 74 69 6d 65 20 77 68 65 6e 20 74 68 65 20 42 4c  time when the BL
37f30 4f 42 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 41  OB is closed.  A
37f40 6e 79 20 65 72 72 6f 72 73 20 74 68 61 74 20 6f  ny errors that o
37f50 63 63 75 72 20 64 75 72 69 6e 67 0a 2a 2a 20 63  ccur during.** c
37f60 6c 6f 73 69 6e 67 20 61 72 65 20 72 65 70 6f 72  losing are repor
37f70 74 65 64 20 61 73 20 61 20 6e 6f 6e 2d 7a 65 72  ted as a non-zer
37f80 6f 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2e 0a  o return value..
37f90 2a 2a 0a 2a 2a 20 54 68 65 20 42 4c 4f 42 20 69  **.** The BLOB i
37fa0 73 20 63 6c 6f 73 65 64 20 75 6e 63 6f 6e 64 69  s closed uncondi
37fb0 74 69 6f 6e 61 6c 6c 79 2e 20 20 45 76 65 6e 20  tionally.  Even 
37fc0 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  if this routine 
37fd0 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 20 65 72  returns.** an er
37fe0 72 6f 72 20 63 6f 64 65 2c 20 74 68 65 20 42 4c  ror code, the BL
37ff0 4f 42 20 69 73 20 73 74 69 6c 6c 20 63 6c 6f 73  OB is still clos
38000 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e  ed..**.** Callin
38010 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  g this routine w
38020 69 74 68 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74  ith a null point
38030 65 72 20 28 77 68 69 63 68 20 61 73 20 77 6f 75  er (which as wou
38040 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 0a 2a  ld be returned.*
38050 2a 20 62 79 20 66 61 69 6c 65 64 20 63 61 6c 6c  * by failed call
38060 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f   to [sqlite3_blo
38070 62 5f 6f 70 65 6e 28 29 5d 29 20 69 73 20 61 20  b_open()]) is a 
38080 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a  harmless no-op..
38090 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
380a0 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 33 33 5d 20  ts:.** [H17833] 
380b0 5b 48 31 37 38 33 36 5d 20 5b 48 31 37 38 33 39  [H17836] [H17839
380c0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
380d0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
380e0 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 62  _close(sqlite3_b
380f0 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  lob *);../*.** C
38100 41 50 49 33 52 45 46 3a 20 52 65 74 75 72 6e 20  API3REF: Return 
38110 54 68 65 20 53 69 7a 65 20 4f 66 20 41 6e 20 4f  The Size Of An O
38120 70 65 6e 20 42 4c 4f 42 20 7b 48 31 37 38 34 30  pen BLOB {H17840
38130 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a  } <S30230>.**.**
38140 20 52 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a   Returns the siz
38150 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68  e in bytes of th
38160 65 20 42 4c 4f 42 20 61 63 63 65 73 73 69 62 6c  e BLOB accessibl
38170 65 20 76 69 61 20 74 68 65 20 0a 2a 2a 20 73 75  e via the .** su
38180 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65  ccessfully opene
38190 64 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20  d [BLOB handle] 
381a0 69 6e 20 69 74 73 20 6f 6e 6c 79 20 61 72 67 75  in its only argu
381b0 6d 65 6e 74 2e 20 20 54 68 65 0a 2a 2a 20 69 6e  ment.  The.** in
381c0 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 49  cremental blob I
381d0 2f 4f 20 72 6f 75 74 69 6e 65 73 20 63 61 6e 20  /O routines can 
381e0 6f 6e 6c 79 20 72 65 61 64 20 6f 72 20 6f 76 65  only read or ove
381f0 72 77 72 69 74 69 6e 67 20 65 78 69 73 74 69 6e  rwriting existin
38200 67 0a 2a 2a 20 62 6c 6f 62 20 63 6f 6e 74 65 6e  g.** blob conten
38210 74 3b 20 74 68 65 79 20 63 61 6e 6e 6f 74 20 63  t; they cannot c
38220 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f  hange the size o
38230 66 20 61 20 62 6c 6f 62 2e 0a 2a 2a 0a 2a 2a 20  f a blob..**.** 
38240 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c  This routine onl
38250 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 4c  y works on a [BL
38260 4f 42 20 68 61 6e 64 6c 65 5d 20 77 68 69 63 68  OB handle] which
38270 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65   has been create
38280 64 0a 2a 2a 20 62 79 20 61 20 70 72 69 6f 72 20  d.** by a prior 
38290 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20  successful call 
382a0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  to [sqlite3_blob
382b0 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 77 68 69  _open()] and whi
382c0 63 68 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 62 65  ch has not.** be
382d0 65 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 71  en closed by [sq
382e0 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65  lite3_blob_close
382f0 28 29 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 6e  ()].  Passing an
38300 79 20 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 20  y other pointer 
38310 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72 6f  in.** to this ro
38320 75 74 69 6e 65 20 72 65 73 75 6c 74 73 20 69 6e  utine results in
38330 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 70   undefined and p
38340 72 6f 62 61 62 6c 79 20 75 6e 64 65 73 69 72 61  robably undesira
38350 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a  ble behavior..**
38360 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
38370 3a 0a 2a 2a 20 5b 48 31 37 38 34 33 5d 0a 2a 2f  :.** [H17843].*/
38380 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
38390 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74  sqlite3_blob_byt
383a0 65 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20  es(sqlite3_blob 
383b0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  *);../*.** CAPI3
383c0 52 45 46 3a 20 52 65 61 64 20 44 61 74 61 20 46  REF: Read Data F
383d0 72 6f 6d 20 41 20 42 4c 4f 42 20 49 6e 63 72 65  rom A BLOB Incre
383e0 6d 65 6e 74 61 6c 6c 79 20 7b 48 31 37 38 35 30  mentally {H17850
383f0 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a  } <S30230>.**.**
38400 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
38410 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 64  s used to read d
38420 61 74 61 20 66 72 6f 6d 20 61 6e 20 6f 70 65 6e  ata from an open
38430 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69   [BLOB handle] i
38440 6e 74 6f 20 61 0a 2a 2a 20 63 61 6c 6c 65 72 2d  nto a.** caller-
38450 73 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 2e  supplied buffer.
38460 20 4e 20 62 79 74 65 73 20 6f 66 20 64 61 74 61   N bytes of data
38470 20 61 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f   are copied into
38480 20 62 75 66 66 65 72 20 5a 0a 2a 2a 20 66 72 6f   buffer Z.** fro
38490 6d 20 74 68 65 20 6f 70 65 6e 20 42 4c 4f 42 2c  m the open BLOB,
384a0 20 73 74 61 72 74 69 6e 67 20 61 74 20 6f 66 66   starting at off
384b0 73 65 74 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a  set iOffset..**.
384c0 2a 2a 20 49 66 20 6f 66 66 73 65 74 20 69 4f 66  ** If offset iOf
384d0 66 73 65 74 20 69 73 20 6c 65 73 73 20 74 68 61  fset is less tha
384e0 6e 20 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 74  n N bytes from t
384f0 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 42 4c  he end of the BL
38500 4f 42 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45  OB,.** [SQLITE_E
38510 52 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65  RROR] is returne
38520 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73  d and no data is
38530 20 72 65 61 64 2e 20 20 49 66 20 4e 20 6f 72 20   read.  If N or 
38540 69 4f 66 66 73 65 74 20 69 73 0a 2a 2a 20 6c 65  iOffset is.** le
38550 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 5b 53  ss than zero, [S
38560 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20  QLITE_ERROR] is 
38570 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20  returned and no 
38580 64 61 74 61 20 69 73 20 72 65 61 64 2e 0a 2a 2a  data is read..**
38590 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   The size of the
385a0 20 62 6c 6f 62 20 28 61 6e 64 20 68 65 6e 63 65   blob (and hence
385b0 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c   the maximum val
385c0 75 65 20 6f 66 20 4e 2b 69 4f 66 66 73 65 74 29  ue of N+iOffset)
385d0 0a 2a 2a 20 63 61 6e 20 62 65 20 64 65 74 65 72  .** can be deter
385e0 6d 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 20  mined using the 
385f0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79  [sqlite3_blob_by
38600 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65  tes()] interface
38610 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74 74 65 6d  ..**.** An attem
38620 70 74 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20  pt to read from 
38630 61 6e 20 65 78 70 69 72 65 64 20 5b 42 4c 4f 42  an expired [BLOB
38640 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c 73 20 77   handle] fails w
38650 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20  ith an.** error 
38660 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f  code of [SQLITE_
38670 41 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  ABORT]..**.** On
38680 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49 54 45   success, SQLITE
38690 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e  _OK is returned.
386a0 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61  .** Otherwise, a
386b0 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f  n [error code] o
386c0 72 20 61 6e 20 5b 65 78 74 65 6e 64 65 64 20 65  r an [extended e
386d0 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65  rror code] is re
386e0 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  turned..**.** Th
386f0 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20  is routine only 
38700 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f 42  works on a [BLOB
38710 20 68 61 6e 64 6c 65 5d 20 77 68 69 63 68 20 68   handle] which h
38720 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 0a  as been created.
38730 2a 2a 20 62 79 20 61 20 70 72 69 6f 72 20 73 75  ** by a prior su
38740 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f  ccessful call to
38750 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f   [sqlite3_blob_o
38760 70 65 6e 28 29 5d 20 61 6e 64 20 77 68 69 63 68  pen()] and which
38770 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 62 65 65 6e   has not.** been
38780 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 71 6c 69   closed by [sqli
38790 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29  te3_blob_close()
387a0 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 6e 79 20  ].  Passing any 
387b0 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 20 69 6e  other pointer in
387c0 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72 6f 75 74  .** to this rout
387d0 69 6e 65 20 72 65 73 75 6c 74 73 20 69 6e 20 75  ine results in u
387e0 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 70 72 6f  ndefined and pro
387f0 62 61 62 6c 79 20 75 6e 64 65 73 69 72 61 62 6c  bably undesirabl
38800 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a  e behavior..**.*
38810 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c  * See also: [sql
38820 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28  ite3_blob_write(
38830 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  )]..**.** Requir
38840 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38  ements:.** [H178
38850 35 33 5d 20 5b 48 31 37 38 35 36 5d 20 5b 48 31  53] [H17856] [H1
38860 37 38 35 39 5d 20 5b 48 31 37 38 36 32 5d 20 5b  7859] [H17862] [
38870 48 31 37 38 36 33 5d 20 5b 48 31 37 38 36 35 5d  H17863] [H17865]
38880 20 5b 48 31 37 38 36 38 5d 0a 2a 2f 0a 53 51 4c   [H17868].*/.SQL
38890 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
388a0 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 73 71  te3_blob_read(sq
388b0 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2c 20 76 6f  lite3_blob *, vo
388c0 69 64 20 2a 5a 2c 20 69 6e 74 20 4e 2c 20 69 6e  id *Z, int N, in
388d0 74 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a  t iOffset);../*.
388e0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 57 72 69  ** CAPI3REF: Wri
388f0 74 65 20 44 61 74 61 20 49 6e 74 6f 20 41 20 42  te Data Into A B
38900 4c 4f 42 20 49 6e 63 72 65 6d 65 6e 74 61 6c 6c  LOB Incrementall
38910 79 20 7b 48 31 37 38 37 30 7d 20 3c 53 33 30 32  y {H17870} <S302
38920 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  30>.**.** This f
38930 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
38940 74 6f 20 77 72 69 74 65 20 64 61 74 61 20 69 6e  to write data in
38950 74 6f 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42  to an open [BLOB
38960 20 68 61 6e 64 6c 65 5d 20 66 72 6f 6d 20 61 0a   handle] from a.
38970 2a 2a 20 63 61 6c 6c 65 72 2d 73 75 70 70 6c 69  ** caller-suppli
38980 65 64 20 62 75 66 66 65 72 2e 20 4e 20 62 79 74  ed buffer. N byt
38990 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 63  es of data are c
389a0 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65 20 62  opied from the b
389b0 75 66 66 65 72 20 5a 0a 2a 2a 20 69 6e 74 6f 20  uffer Z.** into 
389c0 74 68 65 20 6f 70 65 6e 20 42 4c 4f 42 2c 20 73  the open BLOB, s
389d0 74 61 72 74 69 6e 67 20 61 74 20 6f 66 66 73 65  tarting at offse
389e0 74 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a  t iOffset..**.**
389f0 20 49 66 20 74 68 65 20 5b 42 4c 4f 42 20 68 61   If the [BLOB ha
38a00 6e 64 6c 65 5d 20 70 61 73 73 65 64 20 61 73 20  ndle] passed as 
38a10 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
38a20 6e 74 20 77 61 73 20 6e 6f 74 20 6f 70 65 6e 65  nt was not opene
38a30 64 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67  d for.** writing
38a40 20 28 74 68 65 20 66 6c 61 67 73 20 70 61 72 61   (the flags para
38a50 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65  meter to [sqlite
38a60 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 77  3_blob_open()] w
38a70 61 73 20 7a 65 72 6f 29 2c 0a 2a 2a 20 74 68 69  as zero),.** thi
38a80 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
38a90 6e 73 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f  ns [SQLITE_READO
38aa0 4e 4c 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  NLY]..**.** This
38ab0 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e   function may on
38ac0 6c 79 20 6d 6f 64 69 66 79 20 74 68 65 20 63 6f  ly modify the co
38ad0 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 42 4c  ntents of the BL
38ae0 4f 42 3b 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74  OB; it is.** not
38af0 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 69 6e 63   possible to inc
38b00 72 65 61 73 65 20 74 68 65 20 73 69 7a 65 20 6f  rease the size o
38b10 66 20 61 20 42 4c 4f 42 20 75 73 69 6e 67 20 74  f a BLOB using t
38b20 68 69 73 20 41 50 49 2e 0a 2a 2a 20 49 66 20 6f  his API..** If o
38b30 66 66 73 65 74 20 69 4f 66 66 73 65 74 20 69 73  ffset iOffset is
38b40 20 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 79 74   less than N byt
38b50 65 73 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20  es from the end 
38b60 6f 66 20 74 68 65 20 42 4c 4f 42 2c 0a 2a 2a 20  of the BLOB,.** 
38b70 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69  [SQLITE_ERROR] i
38b80 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e  s returned and n
38b90 6f 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65  o data is writte
38ba0 6e 2e 20 20 49 66 20 4e 20 69 73 0a 2a 2a 20 6c  n.  If N is.** l
38bb0 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 20 5b 53  ess than zero [S
38bc0 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20  QLITE_ERROR] is 
38bd0 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20  returned and no 
38be0 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 2e  data is written.
38bf0 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20  .** The size of 
38c00 74 68 65 20 42 4c 4f 42 20 28 61 6e 64 20 68 65  the BLOB (and he
38c10 6e 63 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  nce the maximum 
38c20 76 61 6c 75 65 20 6f 66 20 4e 2b 69 4f 66 66 73  value of N+iOffs
38c30 65 74 29 0a 2a 2a 20 63 61 6e 20 62 65 20 64 65  et).** can be de
38c40 74 65 72 6d 69 6e 65 64 20 75 73 69 6e 67 20 74  termined using t
38c50 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  he [sqlite3_blob
38c60 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 66  _bytes()] interf
38c70 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74  ace..**.** An at
38c80 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 74  tempt to write t
38c90 6f 20 61 6e 20 65 78 70 69 72 65 64 20 5b 42 4c  o an expired [BL
38ca0 4f 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c 73  OB handle] fails
38cb0 20 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72 6f   with an.** erro
38cc0 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54  r code of [SQLIT
38cd0 45 5f 41 42 4f 52 54 5d 2e 20 20 57 72 69 74 65  E_ABORT].  Write
38ce0 73 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 74 68  s to the BLOB th
38cf0 61 74 20 6f 63 63 75 72 72 65 64 0a 2a 2a 20 62  at occurred.** b
38d00 65 66 6f 72 65 20 74 68 65 20 5b 42 4c 4f 42 20  efore the [BLOB 
38d10 68 61 6e 64 6c 65 5d 20 65 78 70 69 72 65 64 20  handle] expired 
38d20 61 72 65 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62  are not rolled b
38d30 61 63 6b 20 62 79 20 74 68 65 0a 2a 2a 20 65 78  ack by the.** ex
38d40 70 69 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  piration of the 
38d50 68 61 6e 64 6c 65 2c 20 74 68 6f 75 67 68 20 6f  handle, though o
38d60 66 20 63 6f 75 72 73 65 20 74 68 6f 73 65 20 63  f course those c
38d70 68 61 6e 67 65 73 20 6d 69 67 68 74 0a 2a 2a 20  hanges might.** 
38d80 68 61 76 65 20 62 65 65 6e 20 6f 76 65 72 77 72  have been overwr
38d90 69 74 74 65 6e 20 62 79 20 74 68 65 20 73 74 61  itten by the sta
38da0 74 65 6d 65 6e 74 20 74 68 61 74 20 65 78 70 69  tement that expi
38db0 72 65 64 20 74 68 65 20 42 4c 4f 42 20 68 61 6e  red the BLOB han
38dc0 64 6c 65 0a 2a 2a 20 6f 72 20 62 79 20 6f 74 68  dle.** or by oth
38dd0 65 72 20 69 6e 64 65 70 65 6e 64 65 6e 74 20 73  er independent s
38de0 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a  tatements..**.**
38df0 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c   On success, SQL
38e00 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
38e10 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  ed..** Otherwise
38e20 2c 20 61 6e 20 20 5b 65 72 72 6f 72 20 63 6f 64  , an  [error cod
38e30 65 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65 6e 64  e] or an [extend
38e40 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69  ed error code] i
38e50 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
38e60 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f  * This routine o
38e70 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20 5b  nly works on a [
38e80 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 77 68 69  BLOB handle] whi
38e90 63 68 20 68 61 73 20 62 65 65 6e 20 63 72 65 61  ch has been crea
38ea0 74 65 64 0a 2a 2a 20 62 79 20 61 20 70 72 69 6f  ted.** by a prio
38eb0 72 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c  r successful cal
38ec0 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c  l to [sqlite3_bl
38ed0 6f 62 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 77  ob_open()] and w
38ee0 68 69 63 68 20 68 61 73 20 6e 6f 74 0a 2a 2a 20  hich has not.** 
38ef0 62 65 65 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b  been closed by [
38f00 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f  sqlite3_blob_clo
38f10 73 65 28 29 5d 2e 20 20 50 61 73 73 69 6e 67 20  se()].  Passing 
38f20 61 6e 79 20 6f 74 68 65 72 20 70 6f 69 6e 74 65  any other pointe
38f30 72 20 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 20  r in.** to this 
38f40 72 6f 75 74 69 6e 65 20 72 65 73 75 6c 74 73 20  routine results 
38f50 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64  in undefined and
38f60 20 70 72 6f 62 61 62 6c 79 20 75 6e 64 65 73 69   probably undesi
38f70 72 61 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a  rable behavior..
38f80 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
38f90 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65  [sqlite3_blob_re
38fa0 61 64 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  ad()]..**.** Req
38fb0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
38fc0 31 37 38 37 33 5d 20 5b 48 31 37 38 37 34 5d 20  17873] [H17874] 
38fd0 5b 48 31 37 38 37 35 5d 20 5b 48 31 37 38 37 36  [H17875] [H17876
38fe0 5d 20 5b 48 31 37 38 37 37 5d 20 5b 48 31 37 38  ] [H17877] [H178
38ff0 37 39 5d 20 5b 48 31 37 38 38 32 5d 20 5b 48 31  79] [H17882] [H1
39000 37 38 38 35 5d 0a 2a 2a 20 5b 48 31 37 38 38 38  7885].** [H17888
39010 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
39020 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
39030 5f 77 72 69 74 65 28 73 71 6c 69 74 65 33 5f 62  _write(sqlite3_b
39040 6c 6f 62 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69  lob *, const voi
39050 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74  d *z, int n, int
39060 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a   iOffset);../*.*
39070 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74  * CAPI3REF: Virt
39080 75 61 6c 20 46 69 6c 65 20 53 79 73 74 65 6d 20  ual File System 
39090 4f 62 6a 65 63 74 73 20 7b 48 31 31 32 30 30 7d  Objects {H11200}
390a0 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S20100>.**.** 
390b0 41 20 76 69 72 74 75 61 6c 20 66 69 6c 65 73 79  A virtual filesy
390c0 73 74 65 6d 20 28 56 46 53 29 20 69 73 20 61 6e  stem (VFS) is an
390d0 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f   [sqlite3_vfs] o
390e0 62 6a 65 63 74 0a 2a 2a 20 74 68 61 74 20 53 51  bject.** that SQ
390f0 4c 69 74 65 20 75 73 65 73 20 74 6f 20 69 6e 74  Lite uses to int
39100 65 72 61 63 74 0a 2a 2a 20 77 69 74 68 20 74 68  eract.** with th
39110 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65  e underlying ope
39120 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20  rating system.  
39130 4d 6f 73 74 20 53 51 4c 69 74 65 20 62 75 69 6c  Most SQLite buil
39140 64 73 20 63 6f 6d 65 20 77 69 74 68 20 61 0a 2a  ds come with a.*
39150 2a 20 73 69 6e 67 6c 65 20 64 65 66 61 75 6c 74  * single default
39160 20 56 46 53 20 74 68 61 74 20 69 73 20 61 70 70   VFS that is app
39170 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68 65  ropriate for the
39180 20 68 6f 73 74 20 63 6f 6d 70 75 74 65 72 2e 0a   host computer..
39190 2a 2a 20 4e 65 77 20 56 46 53 65 73 20 63 61 6e  ** New VFSes can
391a0 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20 61   be registered a
391b0 6e 64 20 65 78 69 73 74 69 6e 67 20 56 46 53 65  nd existing VFSe
391c0 73 20 63 61 6e 20 62 65 20 75 6e 72 65 67 69 73  s can be unregis
391d0 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 6f  tered..** The fo
391e0 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72 66 61 63  llowing interfac
391f0 65 73 20 61 72 65 20 70 72 6f 76 69 64 65 64 2e  es are provided.
39200 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
39210 65 33 5f 76 66 73 5f 66 69 6e 64 28 29 20 69 6e  e3_vfs_find() in
39220 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
39230 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 56  a pointer to a V
39240 46 53 20 67 69 76 65 6e 20 69 74 73 20 6e 61 6d  FS given its nam
39250 65 2e 0a 2a 2a 20 4e 61 6d 65 73 20 61 72 65 20  e..** Names are 
39260 63 61 73 65 20 73 65 6e 73 69 74 69 76 65 2e 0a  case sensitive..
39270 2a 2a 20 4e 61 6d 65 73 20 61 72 65 20 7a 65 72  ** Names are zer
39280 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46  o-terminated UTF
39290 2d 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 20 49  -8 strings..** I
392a0 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61  f there is no ma
392b0 74 63 68 2c 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  tch, a NULL poin
392c0 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ter is returned.
392d0 0a 2a 2a 20 49 66 20 7a 56 66 73 4e 61 6d 65 20  .** If zVfsName 
392e0 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65  is NULL then the
392f0 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 73 20   default VFS is 
39300 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
39310 4e 65 77 20 56 46 53 65 73 20 61 72 65 20 72 65  New VFSes are re
39320 67 69 73 74 65 72 65 64 20 77 69 74 68 20 73 71  gistered with sq
39330 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74  lite3_vfs_regist
39340 65 72 28 29 2e 0a 2a 2a 20 45 61 63 68 20 6e 65  er()..** Each ne
39350 77 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74 68  w VFS becomes th
39360 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 66  e default VFS if
39370 20 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 66 6c   the makeDflt fl
39380 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 54 68  ag is set..** Th
39390 65 20 73 61 6d 65 20 56 46 53 20 63 61 6e 20 62  e same VFS can b
393a0 65 20 72 65 67 69 73 74 65 72 65 64 20 6d 75 6c  e registered mul
393b0 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69 74 68  tiple times with
393c0 6f 75 74 20 69 6e 6a 75 72 79 2e 0a 2a 2a 20 54  out injury..** T
393d0 6f 20 6d 61 6b 65 20 61 6e 20 65 78 69 73 74 69  o make an existi
393e0 6e 67 20 56 46 53 20 69 6e 74 6f 20 74 68 65 20  ng VFS into the 
393f0 64 65 66 61 75 6c 74 20 56 46 53 2c 20 72 65 67  default VFS, reg
39400 69 73 74 65 72 20 69 74 20 61 67 61 69 6e 0a 2a  ister it again.*
39410 2a 20 77 69 74 68 20 74 68 65 20 6d 61 6b 65 44  * with the makeD
39420 66 6c 74 20 66 6c 61 67 20 73 65 74 2e 20 20 49  flt flag set.  I
39430 66 20 74 77 6f 20 64 69 66 66 65 72 65 6e 74 20  f two different 
39440 56 46 53 65 73 20 77 69 74 68 20 74 68 65 0a 2a  VFSes with the.*
39450 2a 20 73 61 6d 65 20 6e 61 6d 65 20 61 72 65 20  * same name are 
39460 72 65 67 69 73 74 65 72 65 64 2c 20 74 68 65 20  registered, the 
39470 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65  behavior is unde
39480 66 69 6e 65 64 2e 20 20 49 66 20 61 0a 2a 2a 20  fined.  If a.** 
39490 56 46 53 20 69 73 20 72 65 67 69 73 74 65 72 65  VFS is registere
394a0 64 20 77 69 74 68 20 61 20 6e 61 6d 65 20 74 68  d with a name th
394b0 61 74 20 69 73 20 4e 55 4c 4c 20 6f 72 20 61 6e  at is NULL or an
394c0 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c 0a 2a   empty string,.*
394d0 2a 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76  * then the behav
394e0 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ior is undefined
394f0 2e 0a 2a 2a 0a 2a 2a 20 55 6e 72 65 67 69 73 74  ..**.** Unregist
39500 65 72 20 61 20 56 46 53 20 77 69 74 68 20 74 68  er a VFS with th
39510 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e  e sqlite3_vfs_un
39520 72 65 67 69 73 74 65 72 28 29 20 69 6e 74 65 72  register() inter
39530 66 61 63 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20  face..** If the 
39540 64 65 66 61 75 6c 74 20 56 46 53 20 69 73 20 75  default VFS is u
39550 6e 72 65 67 69 73 74 65 72 65 64 2c 20 61 6e 6f  nregistered, ano
39560 74 68 65 72 20 56 46 53 20 69 73 20 63 68 6f 73  ther VFS is chos
39570 65 6e 20 61 73 0a 2a 2a 20 74 68 65 20 64 65 66  en as.** the def
39580 61 75 6c 74 2e 20 20 54 68 65 20 63 68 6f 69 63  ault.  The choic
39590 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 56 46  e for the new VF
395a0 53 20 69 73 20 61 72 62 69 74 72 61 72 79 2e 0a  S is arbitrary..
395b0 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
395c0 74 73 3a 0a 2a 2a 20 5b 48 31 31 32 30 33 5d 20  ts:.** [H11203] 
395d0 5b 48 31 31 32 30 36 5d 20 5b 48 31 31 32 30 39  [H11206] [H11209
395e0 5d 20 5b 48 31 31 32 31 32 5d 20 5b 48 31 31 32  ] [H11212] [H112
395f0 31 35 5d 20 5b 48 31 31 32 31 38 5d 0a 2a 2f 0a  15] [H11218].*/.
39600 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
39610 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 5f  e3_vfs *sqlite3_
39620 76 66 73 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63  vfs_find(const c
39630 68 61 72 20 2a 7a 56 66 73 4e 61 6d 65 29 3b 0a  har *zVfsName);.
39640 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
39650 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73  qlite3_vfs_regis
39660 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 2a  ter(sqlite3_vfs*
39670 2c 20 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29 3b  , int makeDflt);
39680 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
39690 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65  sqlite3_vfs_unre
396a0 67 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76  gister(sqlite3_v
396b0 66 73 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  fs*);../*.** CAP
396c0 49 33 52 45 46 3a 20 4d 75 74 65 78 65 73 20 7b  I3REF: Mutexes {
396d0 48 31 37 30 30 30 7d 20 3c 53 32 30 30 30 30 3e  H17000} <S20000>
396e0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74  .**.** The SQLit
396f0 65 20 63 6f 72 65 20 75 73 65 73 20 74 68 65 73  e core uses thes
39700 65 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 74  e routines for t
39710 68 72 65 61 64 0a 2a 2a 20 73 79 6e 63 68 72 6f  hread.** synchro
39720 6e 69 7a 61 74 69 6f 6e 2e 20 54 68 6f 75 67 68  nization. Though
39730 20 74 68 65 79 20 61 72 65 20 69 6e 74 65 6e 64   they are intend
39740 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a  ed for internal.
39750 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65  ** use by SQLite
39760 2c 20 63 6f 64 65 20 74 68 61 74 20 6c 69 6e 6b  , code that link
39770 73 20 61 67 61 69 6e 73 74 20 53 51 4c 69 74 65  s against SQLite
39780 20 69 73 0a 2a 2a 20 70 65 72 6d 69 74 74 65 64   is.** permitted
39790 20 74 6f 20 75 73 65 20 61 6e 79 20 6f 66 20 74   to use any of t
397a0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2e 0a 2a  hese routines..*
397b0 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20  *.** The SQLite 
397c0 73 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6e 74  source code cont
397d0 61 69 6e 73 20 6d 75 6c 74 69 70 6c 65 20 69 6d  ains multiple im
397e0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a  plementations.**
397f0 20 6f 66 20 74 68 65 73 65 20 6d 75 74 65 78 20   of these mutex 
39800 72 6f 75 74 69 6e 65 73 2e 20 20 41 6e 20 61 70  routines.  An ap
39810 70 72 6f 70 72 69 61 74 65 20 69 6d 70 6c 65 6d  propriate implem
39820 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73  entation.** is s
39830 65 6c 65 63 74 65 64 20 61 75 74 6f 6d 61 74 69  elected automati
39840 63 61 6c 6c 79 20 61 74 20 63 6f 6d 70 69 6c 65  cally at compile
39850 2d 74 69 6d 65 2e 20 20 54 68 65 20 66 6f 6c 6c  -time.  The foll
39860 6f 77 69 6e 67 0a 2a 2a 20 69 6d 70 6c 65 6d 65  owing.** impleme
39870 6e 74 61 74 69 6f 6e 73 20 61 72 65 20 61 76 61  ntations are ava
39880 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 53 51  ilable in the SQ
39890 4c 69 74 65 20 63 6f 72 65 3a 0a 2a 2a 0a 2a 2a  Lite core:.**.**
398a0 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 20   <ul>.** <li>   
398b0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32  SQLITE_MUTEX_OS2
398c0 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54  .** <li>   SQLIT
398d0 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 0a  E_MUTEX_PTHREAD.
398e0 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45  ** <li>   SQLITE
398f0 5f 4d 55 54 45 58 5f 57 33 32 0a 2a 2a 20 3c 6c  _MUTEX_W32.** <l
39900 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  i>   SQLITE_MUTE
39910 58 5f 4e 4f 4f 50 0a 2a 2a 20 3c 2f 75 6c 3e 0a  X_NOOP.** </ul>.
39920 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45  **.** The SQLITE
39930 5f 4d 55 54 45 58 5f 4e 4f 4f 50 20 69 6d 70 6c  _MUTEX_NOOP impl
39940 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 20  ementation is a 
39950 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 0a  set of routines.
39960 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 20  ** that does no 
39970 72 65 61 6c 20 6c 6f 63 6b 69 6e 67 20 61 6e 64  real locking and
39980 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20   is appropriate 
39990 66 6f 72 20 75 73 65 20 69 6e 0a 2a 2a 20 61 20  for use in.** a 
399a0 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20  single-threaded 
399b0 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 54 68  application.  Th
399c0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f  e SQLITE_MUTEX_O
399d0 53 32 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55  S2,.** SQLITE_MU
399e0 54 45 58 5f 50 54 48 52 45 41 44 2c 20 61 6e 64  TEX_PTHREAD, and
399f0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33   SQLITE_MUTEX_W3
39a00 32 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  2 implementation
39a10 73 0a 2a 2a 20 61 72 65 20 61 70 70 72 6f 70 72  s.** are appropr
39a20 69 61 74 65 20 66 6f 72 20 75 73 65 20 6f 6e 20  iate for use on 
39a30 4f 53 2f 32 2c 20 55 6e 69 78 2c 20 61 6e 64 20  OS/2, Unix, and 
39a40 57 69 6e 64 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49  Windows..**.** I
39a50 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70  f SQLite is comp
39a60 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 51  iled with the SQ
39a70 4c 49 54 45 5f 4d 55 54 45 58 5f 41 50 50 44 45  LITE_MUTEX_APPDE
39a80 46 20 70 72 65 70 72 6f 63 65 73 73 6f 72 0a 2a  F preprocessor.*
39a90 2a 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 20  * macro defined 
39aa0 28 77 69 74 68 20 22 2d 44 53 51 4c 49 54 45 5f  (with "-DSQLITE_
39ab0 4d 55 54 45 58 5f 41 50 50 44 45 46 3d 31 22 29  MUTEX_APPDEF=1")
39ac0 2c 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 0a  , then no mutex.
39ad0 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
39ae0 6e 20 69 73 20 69 6e 63 6c 75 64 65 64 20 77 69  n is included wi
39af0 74 68 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20  th the library. 
39b00 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
39b10 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  .** application 
39b20 6d 75 73 74 20 73 75 70 70 6c 79 20 61 20 63 75  must supply a cu
39b30 73 74 6f 6d 20 6d 75 74 65 78 20 69 6d 70 6c 65  stom mutex imple
39b40 6d 65 6e 74 61 74 69 6f 6e 20 75 73 69 6e 67 20  mentation using 
39b50 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43  the.** [SQLITE_C
39b60 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 20 6f 70 74  ONFIG_MUTEX] opt
39b70 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
39b80 65 33 5f 63 6f 6e 66 69 67 28 29 20 66 75 6e 63  e3_config() func
39b90 74 69 6f 6e 0a 2a 2a 20 62 65 66 6f 72 65 20 63  tion.** before c
39ba0 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69  alling sqlite3_i
39bb0 6e 69 74 69 61 6c 69 7a 65 28 29 20 6f 72 20 61  nitialize() or a
39bc0 6e 79 20 6f 74 68 65 72 20 70 75 62 6c 69 63 20  ny other public 
39bd0 73 71 6c 69 74 65 33 5f 0a 2a 2a 20 66 75 6e 63  sqlite3_.** func
39be0 74 69 6f 6e 20 74 68 61 74 20 63 61 6c 6c 73 20  tion that calls 
39bf0 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
39c00 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37  ze()..**.** {H17
39c10 30 31 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33  011} The sqlite3
39c20 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72  _mutex_alloc() r
39c30 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73  outine allocates
39c40 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20   a new.** mutex 
39c50 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f  and returns a po
39c60 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 7b 48 31  inter to it. {H1
39c70 37 30 31 32 7d 20 49 66 20 69 74 20 72 65 74 75  7012} If it retu
39c80 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74  rns NULL.** that
39c90 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75   means that a mu
39ca0 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65  tex could not be
39cb0 20 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 48 31 37   allocated. {H17
39cc0 30 31 33 7d 20 53 51 4c 69 74 65 0a 2a 2a 20 77  013} SQLite.** w
39cd0 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73  ill unwind its s
39ce0 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20  tack and return 
39cf0 61 6e 20 65 72 72 6f 72 2e 20 7b 48 31 37 30 31  an error. {H1701
39d00 34 7d 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a  4} The argument.
39d10 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75  ** to sqlite3_mu
39d20 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f  tex_alloc() is o
39d30 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65  ne of these inte
39d40 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a  ger constants:.*
39d50 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69  *.** <ul>.** <li
39d60 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
39d70 46 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51  FAST.** <li>  SQ
39d80 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
39d90 53 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51  SIVE.** <li>  SQ
39da0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
39db0 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e  C_MASTER.** <li>
39dc0 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
39dd0 54 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69  TATIC_MEM.** <li
39de0 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
39df0 53 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c  STATIC_MEM2.** <
39e00 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
39e10 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a  X_STATIC_PRNG.**
39e20 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
39e30 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a  TEX_STATIC_LRU.*
39e40 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
39e50 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32  UTEX_STATIC_LRU2
39e60 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20  .** </ul>.**.** 
39e70 7b 48 31 37 30 31 35 7d 20 54 68 65 20 66 69 72  {H17015} The fir
39e80 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73  st two constants
39e90 20 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d   cause sqlite3_m
39ea0 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20  utex_alloc() to 
39eb0 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20  create.** a new 
39ec0 6d 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20  mutex.  The new 
39ed0 6d 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69  mutex is recursi
39ee0 76 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d  ve when SQLITE_M
39ef0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a  UTEX_RECURSIVE.*
39f00 2a 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f  * is used but no
39f10 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f  t necessarily so
39f20 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54   when SQLITE_MUT
39f30 45 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e  EX_FAST is used.
39f40 20 7b 45 4e 44 7d 0a 2a 2a 20 54 68 65 20 6d 75   {END}.** The mu
39f50 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  tex implementati
39f60 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  on does not need
39f70 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69   to make a disti
39f80 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65  nction.** betwee
39f90 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  n SQLITE_MUTEX_R
39fa0 45 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c  ECURSIVE and SQL
39fb0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69  ITE_MUTEX_FAST i
39fc0 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74  f it does.** not
39fd0 20 77 61 6e 74 20 74 6f 2e 20 20 7b 48 31 37 30   want to.  {H170
39fe0 31 36 7d 20 42 75 74 20 53 51 4c 69 74 65 20 77  16} But SQLite w
39ff0 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74  ill only request
3a000 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74   a recursive mut
3a010 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77  ex in.** cases w
3a020 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e  here it really n
3a030 65 65 64 73 20 6f 6e 65 2e 20 20 7b 45 4e 44 7d  eeds one.  {END}
3a040 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e   If a faster non
3a050 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78  -recursive mutex
3a060 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
3a070 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20  on is available 
3a080 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74  on the host plat
3a090 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20  form, the mutex 
3a0a0 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67  subsystem.** mig
3a0b0 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61  ht return such a
3a0c0 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e   mutex in respon
3a0d0 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54  se to SQLITE_MUT
3a0e0 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 7b  EX_FAST..**.** {
3a0f0 48 31 37 30 31 37 7d 20 54 68 65 20 6f 74 68 65  H17017} The othe
3a100 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65  r allowed parame
3a110 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f  ters to sqlite3_
3a120 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61  mutex_alloc() ea
3a130 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70  ch return.** a p
3a140 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74  ointer to a stat
3a150 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d  ic preexisting m
3a160 75 74 65 78 2e 20 7b 45 4e 44 7d 20 20 53 69 78  utex. {END}  Six
3a170 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20   static mutexes 
3a180 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 74  are.** used by t
3a190 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 69  he current versi
3a1a0 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46  on of SQLite.  F
3a1b0 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f  uture versions o
3a1c0 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20  f SQLite.** may 
3a1d0 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73  add additional s
3a1e0 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20  tatic mutexes.  
3a1f0 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61  Static mutexes a
3a200 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a  re for internal.
3a210 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65  ** use by SQLite
3a220 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74   only.  Applicat
3a230 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 51  ions that use SQ
3a240 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 6f  Lite mutexes sho
3a250 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20  uld.** use only 
3a260 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65  the dynamic mute
3a270 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20  xes returned by 
3a280 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
3a290 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d  T or.** SQLITE_M
3a2a0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0a  UTEX_RECURSIVE..
3a2b0 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 38 7d 20 4e  **.** {H17018} N
3a2c0 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20  ote that if one 
3a2d0 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d  of the dynamic m
3a2e0 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20  utex parameters 
3a2f0 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41  (SQLITE_MUTEX_FA
3a300 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f  ST.** or SQLITE_
3a310 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29  MUTEX_RECURSIVE)
3a320 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71   is used then sq
3a330 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
3a340 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61  c().** returns a
3a350 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78   different mutex
3a360 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20   on every call. 
3a370 20 7b 48 31 37 30 33 34 7d 20 42 75 74 20 66 6f   {H17034} But fo
3a380 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a 20  r the static.** 
3a390 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65  mutex types, the
3a3a0 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72   same mutex is r
3a3b0 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79  eturned on every
3a3c0 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a   call that has.*
3a3d0 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20  * the same type 
3a3e0 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48  number..**.** {H
3a3f0 31 37 30 31 39 7d 20 54 68 65 20 73 71 6c 69 74  17019} The sqlit
3a400 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 29 20  e3_mutex_free() 
3a410 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61  routine dealloca
3a420 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79  tes a previously
3a430 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 64 79  .** allocated dy
3a440 6e 61 6d 69 63 20 6d 75 74 65 78 2e 20 7b 48 31  namic mutex. {H1
3a450 37 30 32 30 7d 20 53 51 4c 69 74 65 20 69 73 20  7020} SQLite is 
3a460 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c  careful to deall
3a470 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 64  ocate every.** d
3a480 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 74 68 61  ynamic mutex tha
3a490 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 20  t it allocates. 
3a4a0 7b 41 31 37 30 32 31 7d 20 54 68 65 20 64 79 6e  {A17021} The dyn
3a4b0 61 6d 69 63 20 6d 75 74 65 78 65 73 20 6d 75 73  amic mutexes mus
3a4c0 74 20 6e 6f 74 20 62 65 20 69 6e 0a 2a 2a 20 75  t not be in.** u
3a4d0 73 65 20 77 68 65 6e 20 74 68 65 79 20 61 72 65  se when they are
3a4e0 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 41   deallocated. {A
3a4f0 31 37 30 32 32 7d 20 41 74 74 65 6d 70 74 69 6e  17022} Attemptin
3a500 67 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20  g to deallocate 
3a510 61 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 65  a static.** mute
3a520 78 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64  x results in und
3a530 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e  efined behavior.
3a540 20 7b 48 31 37 30 32 33 7d 20 53 51 4c 69 74 65   {H17023} SQLite
3a550 20 6e 65 76 65 72 20 64 65 61 6c 6c 6f 63 61 74   never deallocat
3a560 65 73 0a 2a 2a 20 61 20 73 74 61 74 69 63 20 6d  es.** a static m
3a570 75 74 65 78 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a  utex. {END}.**.*
3a580 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
3a590 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20  tex_enter() and 
3a5a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
3a5b0 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74  y() routines att
3a5c0 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72  empt.** to enter
3a5d0 20 61 20 6d 75 74 65 78 2e 20 7b 48 31 37 30 32   a mutex. {H1702
3a5e0 34 7d 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68  4} If another th
3a5f0 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20  read is already 
3a600 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78  within the mutex
3a610 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74  ,.** sqlite3_mut
3a620 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20  ex_enter() will 
3a630 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65  block and sqlite
3a640 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69  3_mutex_try() wi
3a650 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c  ll return.** SQL
3a660 49 54 45 5f 42 55 53 59 2e 20 7b 48 31 37 30 32  ITE_BUSY. {H1702
3a670 35 7d 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f  5}  The sqlite3_
3a680 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65  mutex_try() inte
3a690 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 5b 53  rface returns [S
3a6a0 51 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 75 70 6f  QLITE_OK].** upo
3a6b0 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74  n successful ent
3a6c0 72 79 2e 20 20 7b 48 31 37 30 32 36 7d 20 4d 75  ry.  {H17026} Mu
3a6d0 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73  texes created us
3a6e0 69 6e 67 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55  ing.** SQLITE_MU
3a6f0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61  TEX_RECURSIVE ca
3a700 6e 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c  n be entered mul
3a710 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74  tiple times by t
3a720 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 0a  he same thread..
3a730 2a 2a 20 7b 48 31 37 30 32 37 7d 20 49 6e 20 73  ** {H17027} In s
3a740 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a  uch cases the,.*
3a750 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  * mutex must be 
3a760 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20  exited an equal 
3a770 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20  number of times 
3a780 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74  before another t
3a790 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74  hread.** can ent
3a7a0 65 72 2e 20 20 7b 41 31 37 30 32 38 7d 20 49 66  er.  {A17028} If
3a7b0 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
3a7c0 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20   tries to enter 
3a7d0 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 6b 69 6e  any other.** kin
3a7e0 64 20 6f 66 20 6d 75 74 65 78 20 6d 6f 72 65 20  d of mutex more 
3a7f0 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62  than once, the b
3a800 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66  ehavior is undef
3a810 69 6e 65 64 2e 0a 2a 2a 20 7b 48 31 37 30 32 39  ined..** {H17029
3a820 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65  } SQLite will ne
3a830 76 65 72 20 65 78 68 69 62 69 74 0a 2a 2a 20 73  ver exhibit.** s
3a840 75 63 68 20 62 65 68 61 76 69 6f 72 20 69 6e 20  uch behavior in 
3a850 69 74 73 20 6f 77 6e 20 75 73 65 20 6f 66 20 6d  its own use of m
3a860 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 6f  utexes..**.** So
3a870 6d 65 20 73 79 73 74 65 6d 73 20 28 66 6f 72 20  me systems (for 
3a880 65 78 61 6d 70 6c 65 2c 20 57 69 6e 64 6f 77 73  example, Windows
3a890 20 39 35 29 20 64 6f 20 6e 6f 74 20 73 75 70 70   95) do not supp
3a8a0 6f 72 74 20 74 68 65 20 6f 70 65 72 61 74 69 6f  ort the operatio
3a8b0 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64  n.** implemented
3a8c0 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   by sqlite3_mute
3a8d0 78 5f 74 72 79 28 29 2e 20 20 4f 6e 20 74 68 6f  x_try().  On tho
3a8e0 73 65 20 73 79 73 74 65 6d 73 2c 20 73 71 6c 69  se systems, sqli
3a8f0 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 0a  te3_mutex_try().
3a900 2a 2a 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72  ** will always r
3a910 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
3a920 59 2e 20 20 7b 48 31 37 30 33 30 7d 20 54 68 65  Y.  {H17030} The
3a930 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6f 6e 6c   SQLite core onl
3a940 79 20 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 73  y ever uses.** s
3a950 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
3a960 28 29 20 61 73 20 61 6e 20 6f 70 74 69 6d 69 7a  () as an optimiz
3a970 61 74 69 6f 6e 20 73 6f 20 74 68 69 73 20 69 73  ation so this is
3a980 20 61 63 63 65 70 74 61 62 6c 65 20 62 65 68 61   acceptable beha
3a990 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37  vior..**.** {H17
3a9a0 30 33 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33  031} The sqlite3
3a9b0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72  _mutex_leave() r
3a9c0 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d  outine exits a m
3a9d0 75 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a  utex that was.**
3a9e0 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65   previously ente
3a9f0 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20  red by the same 
3aa00 74 68 72 65 61 64 2e 20 20 7b 41 31 37 30 33 32  thread.  {A17032
3aa10 7d 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a  } The behavior.*
3aa20 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69  * is undefined i
3aa30 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e  f the mutex is n
3aa40 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74  ot currently ent
3aa50 65 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63  ered by the.** c
3aa60 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 6f 72  alling thread or
3aa70 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
3aa80 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 7b 48  y allocated.  {H
3aa90 31 37 30 33 33 7d 20 53 51 4c 69 74 65 20 77 69  17033} SQLite wi
3aaa0 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 64 6f 20 65  ll.** never do e
3aab0 69 74 68 65 72 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a  ither. {END}.**.
3aac0 2a 2a 20 49 66 20 74 68 65 20 61 72 67 75 6d 65  ** If the argume
3aad0 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75  nt to sqlite3_mu
3aae0 74 65 78 5f 65 6e 74 65 72 28 29 2c 20 73 71 6c  tex_enter(), sql
3aaf0 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29  ite3_mutex_try()
3ab00 2c 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  , or.** sqlite3_
3ab10 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 69 73  mutex_leave() is
3ab20 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c   a NULL pointer,
3ab30 20 74 68 65 6e 20 61 6c 6c 20 74 68 72 65 65 20   then all three 
3ab40 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 62 65 68 61  routines.** beha
3ab50 76 65 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a  ve as no-ops..**
3ab60 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73  .** See also: [s
3ab70 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
3ab80 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  d()] and [sqlite
3ab90 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
3aba0 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  )]..*/.SQLITE_AP
3abb0 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  I sqlite3_mutex 
3abc0 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61  *sqlite3_mutex_a
3abd0 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54  lloc(int);.SQLIT
3abe0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
3abf0 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71  e3_mutex_free(sq
3ac00 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53  lite3_mutex*);.S
3ac10 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
3ac20 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
3ac30 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  er(sqlite3_mutex
3ac40 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  *);.SQLITE_API i
3ac50 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  nt sqlite3_mutex
3ac60 5f 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74  _try(sqlite3_mut
3ac70 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ex*);.SQLITE_API
3ac80 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75   void sqlite3_mu
3ac90 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65  tex_leave(sqlite
3aca0 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a  3_mutex*);../*.*
3acb0 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65  * CAPI3REF: Mute
3acc0 78 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63 74  x Methods Object
3acd0 20 7b 48 31 37 31 32 30 7d 20 3c 53 32 30 31 33   {H17120} <S2013
3ace0 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  0>.** EXPERIMENT
3acf0 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  AL.**.** An inst
3ad00 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
3ad10 75 63 74 75 72 65 20 64 65 66 69 6e 65 73 20 74  ucture defines t
3ad20 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75  he low-level rou
3ad30 74 69 6e 65 73 0a 2a 2a 20 75 73 65 64 20 74 6f  tines.** used to
3ad40 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 75 73   allocate and us
3ad50 65 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a  e mutexes..**.**
3ad60 20 55 73 75 61 6c 6c 79 2c 20 74 68 65 20 64 65   Usually, the de
3ad70 66 61 75 6c 74 20 6d 75 74 65 78 20 69 6d 70 6c  fault mutex impl
3ad80 65 6d 65 6e 74 61 74 69 6f 6e 73 20 70 72 6f 76  ementations prov
3ad90 69 64 65 64 20 62 79 20 53 51 4c 69 74 65 20 61  ided by SQLite a
3ada0 72 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74  re.** sufficient
3adb0 2c 20 68 6f 77 65 76 65 72 20 74 68 65 20 75 73  , however the us
3adc0 65 72 20 68 61 73 20 74 68 65 20 6f 70 74 69 6f  er has the optio
3add0 6e 20 6f 66 20 73 75 62 73 74 69 74 75 74 69 6e  n of substitutin
3ade0 67 20 61 20 63 75 73 74 6f 6d 0a 2a 2a 20 69 6d  g a custom.** im
3adf0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72  plementation for
3ae00 20 73 70 65 63 69 61 6c 69 7a 65 64 20 64 65 70   specialized dep
3ae10 6c 6f 79 6d 65 6e 74 73 20 6f 72 20 73 79 73 74  loyments or syst
3ae20 65 6d 73 20 66 6f 72 20 77 68 69 63 68 20 53 51  ems for which SQ
3ae30 4c 69 74 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74  Lite.** does not
3ae40 20 70 72 6f 76 69 64 65 20 61 20 73 75 69 74 61   provide a suita
3ae50 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ble implementati
3ae60 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  on. In this case
3ae70 2c 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 63 72  , the user.** cr
3ae80 65 61 74 65 73 20 61 6e 64 20 70 6f 70 75 6c 61  eates and popula
3ae90 74 65 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20  tes an instance 
3aea0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
3aeb0 65 20 74 6f 20 70 61 73 73 0a 2a 2a 20 74 6f 20  e to pass.** to 
3aec0 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
3aed0 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20   along with the 
3aee0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  [SQLITE_CONFIG_M
3aef0 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a  UTEX] option..**
3af00 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 61   Additionally, a
3af10 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
3af20 69 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e  is structure can
3af30 20 62 65 20 75 73 65 64 20 61 73 20 61 6e 0a 2a   be used as an.*
3af40 2a 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c  * output variabl
3af50 65 20 77 68 65 6e 20 71 75 65 72 79 69 6e 67 20  e when querying 
3af60 74 68 65 20 73 79 73 74 65 6d 20 66 6f 72 20 74  the system for t
3af70 68 65 20 63 75 72 72 65 6e 74 20 6d 75 74 65 78  he current mutex
3af80 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
3af90 6f 6e 2c 20 75 73 69 6e 67 20 74 68 65 20 5b 53  on, using the [S
3afa0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54  QLITE_CONFIG_GET
3afb0 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a  MUTEX] option..*
3afc0 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78 49  *.** The xMutexI
3afd0 6e 69 74 20 6d 65 74 68 6f 64 20 64 65 66 69 6e  nit method defin
3afe0 65 64 20 62 79 20 74 68 69 73 20 73 74 72 75 63  ed by this struc
3aff0 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 20  ture is invoked 
3b000 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 79  as.** part of sy
3b010 73 74 65 6d 20 69 6e 69 74 69 61 6c 69 7a 61 74  stem initializat
3b020 69 6f 6e 20 62 79 20 74 68 65 20 73 71 6c 69 74  ion by the sqlit
3b030 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
3b040 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 7b 48 31  function..** {H1
3b050 37 30 30 31 7d 20 54 68 65 20 78 4d 75 74 65 78  7001} The xMutex
3b060 49 6e 69 74 20 72 6f 75 74 69 6e 65 20 73 68 61  Init routine sha
3b070 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 79 20  ll be called by 
3b080 53 51 4c 69 74 65 20 6f 6e 63 65 20 66 6f 72 20  SQLite once for 
3b090 65 61 63 68 0a 2a 2a 20 65 66 66 65 63 74 69 76  each.** effectiv
3b0a0 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  e call to [sqlit
3b0b0 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d  e3_initialize()]
3b0c0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74  ..**.** The xMut
3b0d0 65 78 45 6e 64 20 6d 65 74 68 6f 64 20 64 65 66  exEnd method def
3b0e0 69 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 72  ined by this str
3b0f0 75 63 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65  ucture is invoke
3b100 64 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20  d as.** part of 
3b110 73 79 73 74 65 6d 20 73 68 75 74 64 6f 77 6e 20  system shutdown 
3b120 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73  by the sqlite3_s
3b130 68 75 74 64 6f 77 6e 28 29 20 66 75 6e 63 74 69  hutdown() functi
3b140 6f 6e 2e 20 54 68 65 0a 2a 2a 20 69 6d 70 6c 65  on. The.** imple
3b150 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 69  mentation of thi
3b160 73 20 6d 65 74 68 6f 64 20 69 73 20 65 78 70 65  s method is expe
3b170 63 74 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20  cted to release 
3b180 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 0a  all outstanding.
3b190 2a 2a 20 72 65 73 6f 75 72 63 65 73 20 6f 62 74  ** resources obt
3b1a0 61 69 6e 65 64 20 62 79 20 74 68 65 20 6d 75 74  ained by the mut
3b1b0 65 78 20 6d 65 74 68 6f 64 73 20 69 6d 70 6c 65  ex methods imple
3b1c0 6d 65 6e 74 61 74 69 6f 6e 2c 20 65 73 70 65 63  mentation, espec
3b1d0 69 61 6c 6c 79 0a 2a 2a 20 74 68 6f 73 65 20 6f  ially.** those o
3b1e0 62 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 78  btained by the x
3b1f0 4d 75 74 65 78 49 6e 69 74 20 6d 65 74 68 6f 64  MutexInit method
3b200 2e 20 7b 48 31 37 30 30 33 7d 20 54 68 65 20 78  . {H17003} The x
3b210 4d 75 74 65 78 45 6e 64 28 29 0a 2a 2a 20 69 6e  MutexEnd().** in
3b220 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 62 65  terface shall be
3b230 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f   invoked once fo
3b240 72 20 65 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b  r each call to [
3b250 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e  sqlite3_shutdown
3b260 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  ()]..**.** The r
3b270 65 6d 61 69 6e 69 6e 67 20 73 65 76 65 6e 20 6d  emaining seven m
3b280 65 74 68 6f 64 73 20 64 65 66 69 6e 65 64 20 62  ethods defined b
3b290 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  y this structure
3b2a0 20 28 78 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 2a   (xMutexAlloc,.*
3b2b0 2a 20 78 4d 75 74 65 78 46 72 65 65 2c 20 78 4d  * xMutexFree, xM
3b2c0 75 74 65 78 45 6e 74 65 72 2c 20 78 4d 75 74 65  utexEnter, xMute
3b2d0 78 54 72 79 2c 20 78 4d 75 74 65 78 4c 65 61 76  xTry, xMutexLeav
3b2e0 65 2c 20 78 4d 75 74 65 78 48 65 6c 64 20 61 6e  e, xMutexHeld an
3b2f0 64 0a 2a 2a 20 78 4d 75 74 65 78 4e 6f 74 68 65  d.** xMutexNothe
3b300 6c 64 29 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68  ld) implement th
3b310 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65  e following inte
3b320 72 66 61 63 65 73 20 28 72 65 73 70 65 63 74 69  rfaces (respecti
3b330 76 65 6c 79 29 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c  vely):.**.** <ul
3b340 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71  >.**   <li>  [sq
3b350 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
3b360 63 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20  c()] </li>.**   
3b370 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d  <li>  [sqlite3_m
3b380 75 74 65 78 5f 66 72 65 65 28 29 5d 20 3c 2f 6c  utex_free()] </l
3b390 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73  i>.**   <li>  [s
3b3a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
3b3b0 65 72 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20  er()] </li>.**  
3b3c0 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f   <li>  [sqlite3_
3b3d0 6d 75 74 65 78 5f 74 72 79 28 29 5d 20 3c 2f 6c  mutex_try()] </l
3b3e0 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73  i>.**   <li>  [s
3b3f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
3b400 76 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20  ve()] </li>.**  
3b410 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f   <li>  [sqlite3_
3b420 6d 75 74 65 78 5f 68 65 6c 64 28 29 5d 20 3c 2f  mutex_held()] </
3b430 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b  li>.**   <li>  [
3b440 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f  sqlite3_mutex_no
3b450 74 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e 0a 2a  theld()] </li>.*
3b460 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68  * </ul>.**.** Th
3b470 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e 63  e only differenc
3b480 65 20 69 73 20 74 68 61 74 20 74 68 65 20 70 75  e is that the pu
3b490 62 6c 69 63 20 73 71 6c 69 74 65 33 5f 58 58 58  blic sqlite3_XXX
3b4a0 20 66 75 6e 63 74 69 6f 6e 73 20 65 6e 75 6d 65   functions enume
3b4b0 72 61 74 65 64 0a 2a 2a 20 61 62 6f 76 65 20 73  rated.** above s
3b4c0 69 6c 65 6e 74 6c 79 20 69 67 6e 6f 72 65 20 61  ilently ignore a
3b4d0 6e 79 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 74  ny invocations t
3b4e0 68 61 74 20 70 61 73 73 20 61 20 4e 55 4c 4c 20  hat pass a NULL 
3b4f0 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64 0a  pointer instead.
3b500 2a 2a 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75  ** of a valid mu
3b510 74 65 78 20 68 61 6e 64 6c 65 2e 20 54 68 65 20  tex handle. The 
3b520 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
3b530 6f 66 20 74 68 65 20 6d 65 74 68 6f 64 73 20 64  of the methods d
3b540 65 66 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 69  efined.** by thi
3b550 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20  s structure are 
3b560 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20  not required to 
3b570 68 61 6e 64 6c 65 20 74 68 69 73 20 63 61 73 65  handle this case
3b580 2c 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a  , the results.**
3b590 20 6f 66 20 70 61 73 73 69 6e 67 20 61 20 4e 55   of passing a NU
3b5a0 4c 4c 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65  LL pointer inste
3b5b0 61 64 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75  ad of a valid mu
3b5c0 74 65 78 20 68 61 6e 64 6c 65 20 61 72 65 20 75  tex handle are u
3b5d0 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 28 69 2e 65  ndefined.** (i.e
3b5e0 2e 20 69 74 20 69 73 20 61 63 63 65 70 74 61 62  . it is acceptab
3b5f0 6c 65 20 74 6f 20 70 72 6f 76 69 64 65 20 61 6e  le to provide an
3b600 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
3b610 74 68 61 74 20 73 65 67 66 61 75 6c 74 73 20 69  that segfaults i
3b620 66 0a 2a 2a 20 69 74 20 69 73 20 70 61 73 73 65  f.** it is passe
3b630 64 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  d a NULL pointer
3b640 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75  )..**.** The xMu
3b650 74 65 78 49 6e 69 74 28 29 20 6d 65 74 68 6f 64  texInit() method
3b660 20 6d 75 73 74 20 62 65 20 74 68 72 65 61 64 73   must be threads
3b670 61 66 65 2e 20 20 49 74 20 6d 75 73 74 20 62 65  afe.  It must be
3b680 20 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a 2a 20   harmless to.** 
3b690 69 6e 76 6f 6b 65 20 78 4d 75 74 65 78 49 6e 69  invoke xMutexIni
3b6a0 74 28 29 20 6d 75 74 69 70 6c 65 20 74 69 6d 65  t() mutiple time
3b6b0 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d  s within the sam
3b6c0 65 20 70 72 6f 63 65 73 73 20 61 6e 64 20 77 69  e process and wi
3b6d0 74 68 6f 75 74 0a 2a 2a 20 69 6e 74 65 72 76 65  thout.** interve
3b6e0 6e 69 6e 67 20 63 61 6c 6c 73 20 74 6f 20 78 4d  ning calls to xM
3b6f0 75 74 65 78 45 6e 64 28 29 2e 20 20 53 65 63 6f  utexEnd().  Seco
3b700 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e  nd and subsequen
3b710 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 78 4d  t calls to.** xM
3b720 75 74 65 78 49 6e 69 74 28 29 20 6d 75 73 74 20  utexInit() must 
3b730 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a  be no-ops..**.**
3b740 20 78 4d 75 74 65 78 49 6e 69 74 28 29 20 6d 75   xMutexInit() mu
3b750 73 74 20 6e 6f 74 20 75 73 65 20 53 51 4c 69 74  st not use SQLit
3b760 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
3b770 69 6f 6e 20 28 5b 73 71 6c 69 74 65 33 5f 6d 61  ion ([sqlite3_ma
3b780 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 61 6e 64 20 69  lloc()].** and i
3b790 74 73 20 61 73 73 6f 63 69 61 74 65 73 29 2e 20  ts associates). 
3b7a0 20 53 69 6d 69 6c 61 72 6c 79 2c 20 78 4d 75 74   Similarly, xMut
3b7b0 65 78 41 6c 6c 6f 63 28 29 20 6d 75 73 74 20 6e  exAlloc() must n
3b7c0 6f 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 65  ot use SQLite me
3b7d0 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69  mory.** allocati
3b7e0 6f 6e 20 66 6f 72 20 61 20 73 74 61 74 69 63 20  on for a static 
3b7f0 6d 75 74 65 78 2e 20 20 48 6f 77 65 76 65 72 20  mutex.  However 
3b800 78 4d 75 74 65 78 41 6c 6c 6f 63 28 29 20 6d 61  xMutexAlloc() ma
3b810 79 20 75 73 65 20 53 51 4c 69 74 65 0a 2a 2a 20  y use SQLite.** 
3b820 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
3b830 6e 20 66 6f 72 20 61 20 66 61 73 74 20 6f 72 20  n for a fast or 
3b840 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 2e  recursive mutex.
3b850 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69  .**.** SQLite wi
3b860 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 4d  ll invoke the xM
3b870 75 74 65 78 45 6e 64 28 29 20 6d 65 74 68 6f 64  utexEnd() method
3b880 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 73   when [sqlite3_s
3b890 68 75 74 64 6f 77 6e 28 29 5d 20 69 73 0a 2a 2a  hutdown()] is.**
3b8a0 20 63 61 6c 6c 65 64 2c 20 62 75 74 20 6f 6e 6c   called, but onl
3b8b0 79 20 69 66 20 74 68 65 20 70 72 69 6f 72 20 63  y if the prior c
3b8c0 61 6c 6c 20 74 6f 20 78 4d 75 74 65 78 49 6e 69  all to xMutexIni
3b8d0 74 20 72 65 74 75 72 6e 65 64 20 53 51 4c 49 54  t returned SQLIT
3b8e0 45 5f 4f 4b 2e 0a 2a 2a 20 49 66 20 78 4d 75 74  E_OK..** If xMut
3b8f0 65 78 49 6e 69 74 20 66 61 69 6c 73 20 69 6e 20  exInit fails in 
3b900 61 6e 79 20 77 61 79 2c 20 69 74 20 69 73 20 65  any way, it is e
3b910 78 70 65 63 74 65 64 20 74 6f 20 63 6c 65 61 6e  xpected to clean
3b920 20 75 70 20 61 66 74 65 72 20 69 74 73 65 6c 66   up after itself
3b930 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 65 74  .** prior to ret
3b940 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 74 79 70 65 64  urning..*/.typed
3b950 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
3b960 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20  3_mutex_methods 
3b970 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
3b980 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71  thods;.struct sq
3b990 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
3b9a0 6f 64 73 20 7b 0a 20 20 69 6e 74 20 28 2a 78 4d  ods {.  int (*xM
3b9b0 75 74 65 78 49 6e 69 74 29 28 76 6f 69 64 29 3b  utexInit)(void);
3b9c0 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 45  .  int (*xMutexE
3b9d0 6e 64 29 28 76 6f 69 64 29 3b 0a 20 20 73 71 6c  nd)(void);.  sql
3b9e0 69 74 65 33 5f 6d 75 74 65 78 20 2a 28 2a 78 4d  ite3_mutex *(*xM
3b9f0 75 74 65 78 41 6c 6c 6f 63 29 28 69 6e 74 29 3b  utexAlloc)(int);
3ba00 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 78  .  void (*xMutex
3ba10 46 72 65 65 29 28 73 71 6c 69 74 65 33 5f 6d 75  Free)(sqlite3_mu
3ba20 74 65 78 20 2a 29 3b 0a 20 20 76 6f 69 64 20 28  tex *);.  void (
3ba30 2a 78 4d 75 74 65 78 45 6e 74 65 72 29 28 73 71  *xMutexEnter)(sq
3ba40 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a  lite3_mutex *);.
3ba50 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 54 72    int (*xMutexTr
3ba60 79 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  y)(sqlite3_mutex
3ba70 20 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d   *);.  void (*xM
3ba80 75 74 65 78 4c 65 61 76 65 29 28 73 71 6c 69 74  utexLeave)(sqlit
3ba90 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69  e3_mutex *);.  i
3baa0 6e 74 20 28 2a 78 4d 75 74 65 78 48 65 6c 64 29  nt (*xMutexHeld)
3bab0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
3bac0 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65  );.  int (*xMute
3bad0 78 4e 6f 74 68 65 6c 64 29 28 73 71 6c 69 74 65  xNotheld)(sqlite
3bae0 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 7d 3b 0a 0a  3_mutex *);.};..
3baf0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
3bb00 4d 75 74 65 78 20 56 65 72 69 66 69 63 61 74 69  Mutex Verificati
3bb10 6f 6e 20 52 6f 75 74 69 6e 65 73 20 7b 48 31 37  on Routines {H17
3bb20 30 38 30 7d 20 3c 53 32 30 31 33 30 3e 20 3c 53  080} <S20130> <S
3bb30 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  30800>.**.** The
3bb40 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
3bb50 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65  eld() and sqlite
3bb60 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
3bb70 29 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72  ) routines.** ar
3bb80 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75  e intended for u
3bb90 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74  se inside assert
3bba0 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 7b  () statements. {
3bbb0 48 31 37 30 38 31 7d 20 54 68 65 20 53 51 4c 69  H17081} The SQLi
3bbc0 74 65 20 63 6f 72 65 0a 2a 2a 20 6e 65 76 65 72  te core.** never
3bbd0 20 75 73 65 73 20 74 68 65 73 65 20 72 6f 75 74   uses these rout
3bbe0 69 6e 65 73 20 65 78 63 65 70 74 20 69 6e 73 69  ines except insi
3bbf0 64 65 20 61 6e 20 61 73 73 65 72 74 28 29 20 61  de an assert() a
3bc00 6e 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a  nd applications.
3bc10 2a 2a 20 61 72 65 20 61 64 76 69 73 65 64 20 74  ** are advised t
3bc20 6f 20 66 6f 6c 6c 6f 77 20 74 68 65 20 6c 65 61  o follow the lea
3bc30 64 20 6f 66 20 74 68 65 20 63 6f 72 65 2e 20 20  d of the core.  
3bc40 7b 48 31 37 30 38 32 7d 20 54 68 65 20 63 6f 72  {H17082} The cor
3bc50 65 20 6f 6e 6c 79 0a 2a 2a 20 70 72 6f 76 69 64  e only.** provid
3bc60 65 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  es implementatio
3bc70 6e 73 20 66 6f 72 20 74 68 65 73 65 20 72 6f 75  ns for these rou
3bc80 74 69 6e 65 73 20 77 68 65 6e 20 69 74 20 69 73  tines when it is
3bc90 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 77 69 74   compiled.** wit
3bca0 68 20 74 68 65 20 53 51 4c 49 54 45 5f 44 45 42  h the SQLITE_DEB
3bcb0 55 47 20 66 6c 61 67 2e 20 20 7b 41 31 37 30 38  UG flag.  {A1708
3bcc0 37 7d 20 45 78 74 65 72 6e 61 6c 20 6d 75 74 65  7} External mute
3bcd0 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  x implementation
3bce0 73 0a 2a 2a 20 61 72 65 20 6f 6e 6c 79 20 72 65  s.** are only re
3bcf0 71 75 69 72 65 64 20 74 6f 20 70 72 6f 76 69 64  quired to provid
3bd00 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  e these routines
3bd10 20 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47   if SQLITE_DEBUG
3bd20 20 69 73 0a 2a 2a 20 64 65 66 69 6e 65 64 20 61   is.** defined a
3bd30 6e 64 20 69 66 20 4e 44 45 42 55 47 20 69 73 20  nd if NDEBUG is 
3bd40 6e 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a  not defined..**.
3bd50 2a 2a 20 7b 48 31 37 30 38 33 7d 20 54 68 65 73  ** {H17083} Thes
3bd60 65 20 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c  e routines shoul
3bd70 64 20 72 65 74 75 72 6e 20 74 72 75 65 20 69 66  d return true if
3bd80 20 74 68 65 20 6d 75 74 65 78 20 69 6e 20 74 68   the mutex in th
3bd90 65 69 72 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  eir argument.** 
3bda0 69 73 20 68 65 6c 64 20 6f 72 20 6e 6f 74 20 68  is held or not h
3bdb0 65 6c 64 2c 20 72 65 73 70 65 63 74 69 76 65 6c  eld, respectivel
3bdc0 79 2c 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e  y, by the callin
3bdd0 67 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20  g thread..**.** 
3bde0 7b 58 31 37 30 38 34 7d 20 54 68 65 20 69 6d 70  {X17084} The imp
3bdf0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 6e  lementation is n
3be00 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 70  ot required to p
3be10 72 6f 76 69 64 65 64 20 76 65 72 73 69 6f 6e 73  rovided versions
3be20 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 72 6f 75   of these.** rou
3be30 74 69 6e 65 73 20 74 68 61 74 20 61 63 74 75 61  tines that actua
3be40 6c 6c 79 20 77 6f 72 6b 2e 20 49 66 20 74 68 65  lly work. If the
3be50 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
3be60 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76 69 64 65  does not provide
3be70 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20 76 65 72 73   working.** vers
3be80 69 6f 6e 73 20 6f 66 20 74 68 65 73 65 20 72 6f  ions of these ro
3be90 75 74 69 6e 65 73 2c 20 69 74 20 73 68 6f 75 6c  utines, it shoul
3bea0 64 20 61 74 20 6c 65 61 73 74 20 70 72 6f 76 69  d at least provi
3beb0 64 65 20 73 74 75 62 73 20 74 68 61 74 20 61 6c  de stubs that al
3bec0 77 61 79 73 0a 2a 2a 20 72 65 74 75 72 6e 20 74  ways.** return t
3bed0 72 75 65 20 73 6f 20 74 68 61 74 20 6f 6e 65 20  rue so that one 
3bee0 64 6f 65 73 20 6e 6f 74 20 67 65 74 20 73 70 75  does not get spu
3bef0 72 69 6f 75 73 20 61 73 73 65 72 74 69 6f 6e 20  rious assertion 
3bf00 66 61 69 6c 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20  failures..**.** 
3bf10 7b 48 31 37 30 38 35 7d 20 49 66 20 74 68 65 20  {H17085} If the 
3bf20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69  argument to sqli
3bf30 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29  te3_mutex_held()
3bf40 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   is a NULL point
3bf50 65 72 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 72  er then.** the r
3bf60 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 72 65  outine should re
3bf70 74 75 72 6e 20 31 2e 20 20 7b 45 4e 44 7d 20 54  turn 1.  {END} T
3bf80 68 69 73 20 73 65 65 6d 73 20 63 6f 75 6e 74 65  his seems counte
3bf90 72 2d 69 6e 74 75 69 74 69 76 65 20 73 69 6e 63  r-intuitive sinc
3bfa0 65 0a 2a 2a 20 63 6c 65 61 72 6c 79 20 74 68 65  e.** clearly the
3bfb0 20 6d 75 74 65 78 20 63 61 6e 6e 6f 74 20 62 65   mutex cannot be
3bfc0 20 68 65 6c 64 20 69 66 20 69 74 20 64 6f 65 73   held if it does
3bfd0 20 6e 6f 74 20 65 78 69 73 74 2e 20 20 42 75 74   not exist.  But
3bfe0 20 74 68 65 0a 2a 2a 20 74 68 65 20 72 65 61 73   the.** the reas
3bff0 6f 6e 20 74 68 65 20 6d 75 74 65 78 20 64 6f 65  on the mutex doe
3c000 73 20 6e 6f 74 20 65 78 69 73 74 20 69 73 20 62  s not exist is b
3c010 65 63 61 75 73 65 20 74 68 65 20 62 75 69 6c 64  ecause the build
3c020 20 69 73 20 6e 6f 74 0a 2a 2a 20 75 73 69 6e 67   is not.** using
3c030 20 6d 75 74 65 78 65 73 2e 20 20 41 6e 64 20 77   mutexes.  And w
3c040 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68  e do not want th
3c050 65 20 61 73 73 65 72 74 28 29 20 63 6f 6e 74 61  e assert() conta
3c060 69 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 63 61 6c  ining the.** cal
3c070 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74  l to sqlite3_mut
3c080 65 78 5f 68 65 6c 64 28 29 20 74 6f 20 66 61 69  ex_held() to fai
3c090 6c 2c 20 73 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f  l, so a non-zero
3c0a0 20 72 65 74 75 72 6e 20 69 73 0a 2a 2a 20 74 68   return is.** th
3c0b0 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 74 68  e appropriate th
3c0c0 69 6e 67 20 74 6f 20 64 6f 2e 20 20 7b 48 31 37  ing to do.  {H17
3c0d0 30 38 36 7d 20 54 68 65 20 73 71 6c 69 74 65 33  086} The sqlite3
3c0e0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29  _mutex_notheld()
3c0f0 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 73 68  .** interface sh
3c100 6f 75 6c 64 20 61 6c 73 6f 20 72 65 74 75 72 6e  ould also return
3c110 20 31 20 77 68 65 6e 20 67 69 76 65 6e 20 61 20   1 when given a 
3c120 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f  NULL pointer..*/
3c130 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
3c140 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
3c150 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ld(sqlite3_mutex
3c160 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  *);.SQLITE_API i
3c170 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  nt sqlite3_mutex
3c180 5f 6e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33  _notheld(sqlite3
3c190 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  _mutex*);../*.**
3c1a0 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78   CAPI3REF: Mutex
3c1b0 20 54 79 70 65 73 20 7b 48 31 37 30 30 31 7d 20   Types {H17001} 
3c1c0 3c 48 31 37 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <H17000>.**.** T
3c1d0 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  he [sqlite3_mute
3c1e0 78 5f 61 6c 6c 6f 63 28 29 5d 20 69 6e 74 65 72  x_alloc()] inter
3c1f0 66 61 63 65 20 74 61 6b 65 73 20 61 20 73 69 6e  face takes a sin
3c200 67 6c 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  gle argument.** 
3c210 77 68 69 63 68 20 69 73 20 6f 6e 65 20 6f 66 20  which is one of 
3c220 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f  these integer co
3c230 6e 73 74 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54  nstants..**.** T
3c240 68 65 20 73 65 74 20 6f 66 20 73 74 61 74 69 63  he set of static
3c250 20 6d 75 74 65 78 65 73 20 6d 61 79 20 63 68 61   mutexes may cha
3c260 6e 67 65 20 66 72 6f 6d 20 6f 6e 65 20 53 51 4c  nge from one SQL
3c270 69 74 65 20 72 65 6c 65 61 73 65 20 74 6f 20 74  ite release to t
3c280 68 65 0a 2a 2a 20 6e 65 78 74 2e 20 20 41 70 70  he.** next.  App
3c290 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6f  lications that o
3c2a0 76 65 72 72 69 64 65 20 74 68 65 20 62 75 69 6c  verride the buil
3c2b0 74 2d 69 6e 20 6d 75 74 65 78 20 6c 6f 67 69 63  t-in mutex logic
3c2c0 20 6d 75 73 74 20 62 65 0a 2a 2a 20 70 72 65 70   must be.** prep
3c2d0 61 72 65 64 20 74 6f 20 61 63 63 6f 6d 6d 6f 64  ared to accommod
3c2e0 61 74 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73  ate additional s
3c2f0 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 0a 2a  tatic mutexes..*
3c300 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
3c310 5f 4d 55 54 45 58 5f 46 41 53 54 20 20 20 20 20  _MUTEX_FAST     
3c320 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e          0.#defin
3c330 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  e SQLITE_MUTEX_R
3c340 45 43 55 52 53 49 56 45 20 20 20 20 20 20 20 20  ECURSIVE        
3c350 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  1.#define SQLITE
3c360 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
3c370 53 54 45 52 20 20 20 20 32 0a 23 64 65 66 69 6e  STER    2.#defin
3c380 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  e SQLITE_MUTEX_S
3c390 54 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20  TATIC_MEM       
3c3a0 33 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 61  3  /* sqlite3_ma
3c3b0 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e  lloc() */.#defin
3c3c0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  e SQLITE_MUTEX_S
3c3d0 54 41 54 49 43 5f 4d 45 4d 32 20 20 20 20 20 20  TATIC_MEM2      
3c3e0 34 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 20 2a  4  /* NOT USED *
3c3f0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
3c400 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4f 50  _MUTEX_STATIC_OP
3c410 45 4e 20 20 20 20 20 20 34 20 20 2f 2a 20 73 71  EN      4  /* sq
3c420 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28 29  lite3BtreeOpen()
3c430 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
3c440 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
3c450 50 52 4e 47 20 20 20 20 20 20 35 20 20 2f 2a 20  PRNG      5  /* 
3c460 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 28 29  sqlite3_random()
3c470 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
3c480 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
3c490 4c 52 55 20 20 20 20 20 20 20 36 20 20 2f 2a 20  LRU       6  /* 
3c4a0 6c 72 75 20 70 61 67 65 20 6c 69 73 74 20 2a 2f  lru page list */
3c4b0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3c4c0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55  MUTEX_STATIC_LRU
3c4d0 32 20 20 20 20 20 20 37 20 20 2f 2a 20 6c 72 75  2      7  /* lru
3c4e0 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 0a 2f   page list */../
3c4f0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52  *.** CAPI3REF: R
3c500 65 74 72 69 65 76 65 20 74 68 65 20 6d 75 74 65  etrieve the mute
3c510 78 20 66 6f 72 20 61 20 64 61 74 61 62 61 73 65  x for a database
3c520 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48 31 37   connection {H17
3c530 30 30 32 7d 20 3c 48 31 37 30 30 30 3e 0a 2a 2a  002} <H17000>.**
3c540 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61  .** This interfa
3c550 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  ce returns a poi
3c560 6e 74 65 72 20 74 68 65 20 5b 73 71 6c 69 74 65  nter the [sqlite
3c570 33 5f 6d 75 74 65 78 5d 20 6f 62 6a 65 63 74 20  3_mutex] object 
3c580 74 68 61 74 20 0a 2a 2a 20 73 65 72 69 61 6c 69  that .** seriali
3c590 7a 65 73 20 61 63 63 65 73 73 20 74 6f 20 74 68  zes access to th
3c5a0 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
3c5b0 65 63 74 69 6f 6e 5d 20 67 69 76 65 6e 20 69 6e  ection] given in
3c5c0 20 74 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a   the argument.**
3c5d0 20 77 68 65 6e 20 74 68 65 20 5b 74 68 72 65 61   when the [threa
3c5e0 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 53 65  ding mode] is Se
3c5f0 72 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 66 20  rialized..** If 
3c600 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d  the [threading m
3c610 6f 64 65 5d 20 69 73 20 53 69 6e 67 6c 65 2d 74  ode] is Single-t
3c620 68 72 65 61 64 20 6f 72 20 4d 75 6c 74 69 2d 74  hread or Multi-t
3c630 68 72 65 61 64 20 74 68 65 6e 20 74 68 69 73 0a  hread then this.
3c640 2a 2a 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  ** routine retur
3c650 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  ns a NULL pointe
3c660 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  r..*/.SQLITE_API
3c670 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
3c680 73 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78  sqlite3_db_mutex
3c690 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a  (sqlite3*);../*.
3c6a0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4c 6f 77  ** CAPI3REF: Low
3c6b0 2d 4c 65 76 65 6c 20 43 6f 6e 74 72 6f 6c 20 4f  -Level Control O
3c6c0 66 20 44 61 74 61 62 61 73 65 20 46 69 6c 65 73  f Database Files
3c6d0 20 7b 48 31 31 33 30 30 7d 20 3c 53 33 30 38 30   {H11300} <S3080
3c6e0 30 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 31  0>.**.** {H11301
3c6f0 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 66  } The [sqlite3_f
3c700 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69  ile_control()] i
3c710 6e 74 65 72 66 61 63 65 20 6d 61 6b 65 73 20 61  nterface makes a
3c720 20 64 69 72 65 63 74 20 63 61 6c 6c 20 74 6f 20   direct call to 
3c730 74 68 65 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e 74  the.** xFileCont
3c740 72 6f 6c 20 6d 65 74 68 6f 64 20 66 6f 72 20 74  rol method for t
3c750 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  he [sqlite3_io_m
3c760 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 61  ethods] object a
3c770 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74  ssociated.** wit
3c780 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64  h a particular d
3c790 61 74 61 62 61 73 65 20 69 64 65 6e 74 69 66 69  atabase identifi
3c7a0 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64  ed by the second
3c7b0 20 61 72 67 75 6d 65 6e 74 2e 20 7b 48 31 31 33   argument. {H113
3c7c0 30 32 7d 20 54 68 65 0a 2a 2a 20 6e 61 6d 65 20  02} The.** name 
3c7d0 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
3c7e0 69 73 20 74 68 65 20 6e 61 6d 65 20 61 73 73 69  is the name assi
3c7f0 67 6e 65 64 20 74 6f 20 74 68 65 20 64 61 74 61  gned to the data
3c800 62 61 73 65 20 62 79 20 74 68 65 0a 2a 2a 20 3c  base by the.** <
3c810 61 20 68 72 65 66 3d 22 6c 61 6e 67 5f 61 74 74  a href="lang_att
3c820 61 63 68 2e 68 74 6d 6c 22 3e 41 54 54 41 43 48  ach.html">ATTACH
3c830 3c 2f 61 3e 20 53 51 4c 20 63 6f 6d 6d 61 6e 64  </a> SQL command
3c840 20 74 68 61 74 20 6f 70 65 6e 65 64 20 74 68 65   that opened the
3c850 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 7b 48  .** database. {H
3c860 31 31 33 30 33 7d 20 54 6f 20 63 6f 6e 74 72 6f  11303} To contro
3c870 6c 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  l the main datab
3c880 61 73 65 20 66 69 6c 65 2c 20 75 73 65 20 74 68  ase file, use th
3c890 65 20 6e 61 6d 65 20 22 6d 61 69 6e 22 0a 2a 2a  e name "main".**
3c8a0 20 6f 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   or a NULL point
3c8b0 65 72 2e 20 7b 48 31 31 33 30 34 7d 20 54 68 65  er. {H11304} The
3c8c0 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74   third and fourt
3c8d0 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20  h parameters to 
3c8e0 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
3c8f0 61 72 65 20 70 61 73 73 65 64 20 64 69 72 65 63  are passed direc
3c900 74 6c 79 20 74 68 72 6f 75 67 68 20 74 6f 20 74  tly through to t
3c910 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 74 68  he second and th
3c920 69 72 64 20 70 61 72 61 6d 65 74 65 72 73 20 6f  ird parameters o
3c930 66 0a 2a 2a 20 74 68 65 20 78 46 69 6c 65 43 6f  f.** the xFileCo
3c940 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 20 7b  ntrol method.  {
3c950 48 31 31 33 30 35 7d 20 54 68 65 20 72 65 74 75  H11305} The retu
3c960 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  rn value of the 
3c970 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 0a 2a 2a 20  xFileControl.** 
3c980 6d 65 74 68 6f 64 20 62 65 63 6f 6d 65 73 20 74  method becomes t
3c990 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
3c9a0 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  of this routine.
3c9b0 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 36 7d 20  .**.** {H11306} 
3c9c0 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61  If the second pa
3c9d0 72 61 6d 65 74 65 72 20 28 7a 44 62 4e 61 6d 65  rameter (zDbName
3c9e0 29 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68  ) does not match
3c9f0 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 79   the name of any
3ca00 0a 2a 2a 20 6f 70 65 6e 20 64 61 74 61 62 61 73  .** open databas
3ca10 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 53 51 4c  e file, then SQL
3ca20 49 54 45 5f 45 52 52 4f 52 20 69 73 20 72 65 74  ITE_ERROR is ret
3ca30 75 72 6e 65 64 2e 20 7b 48 31 31 33 30 37 7d 20  urned. {H11307} 
3ca40 54 68 69 73 20 65 72 72 6f 72 0a 2a 2a 20 63 6f  This error.** co
3ca50 64 65 20 69 73 20 6e 6f 74 20 72 65 6d 65 6d 62  de is not rememb
3ca60 65 72 65 64 20 61 6e 64 20 77 69 6c 6c 20 6e 6f  ered and will no
3ca70 74 20 62 65 20 72 65 63 61 6c 6c 65 64 20 62 79  t be recalled by
3ca80 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64   [sqlite3_errcod
3ca90 65 28 29 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69  e()].** or [sqli
3caa0 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 20 7b  te3_errmsg()]. {
3cab0 41 31 31 33 30 38 7d 20 54 68 65 20 75 6e 64 65  A11308} The unde
3cac0 72 6c 79 69 6e 67 20 78 46 69 6c 65 43 6f 6e 74  rlying xFileCont
3cad0 72 6f 6c 20 6d 65 74 68 6f 64 20 6d 69 67 68 74  rol method might
3cae0 0a 2a 2a 20 61 6c 73 6f 20 72 65 74 75 72 6e 20  .** also return 
3caf0 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20 20 7b  SQLITE_ERROR.  {
3cb00 41 31 31 33 30 39 7d 20 54 68 65 72 65 20 69 73  A11309} There is
3cb10 20 6e 6f 20 77 61 79 20 74 6f 20 64 69 73 74 69   no way to disti
3cb20 6e 67 75 69 73 68 20 62 65 74 77 65 65 6e 0a 2a  nguish between.*
3cb30 2a 20 61 6e 20 69 6e 63 6f 72 72 65 63 74 20 7a  * an incorrect z
3cb40 44 62 4e 61 6d 65 20 61 6e 64 20 61 6e 20 53 51  DbName and an SQ
3cb50 4c 49 54 45 5f 45 52 52 4f 52 20 72 65 74 75 72  LITE_ERROR retur
3cb60 6e 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 72  n from the under
3cb70 6c 79 69 6e 67 0a 2a 2a 20 78 46 69 6c 65 43 6f  lying.** xFileCo
3cb80 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 7b 45  ntrol method. {E
3cb90 4e 44 7d 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  ND}.**.** See al
3cba0 73 6f 3a 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54  so: [SQLITE_FCNT
3cbb0 4c 5f 4c 4f 43 4b 53 54 41 54 45 5d 0a 2a 2f 0a  L_LOCKSTATE].*/.
3cbc0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3cbd0 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74  qlite3_file_cont
3cbe0 72 6f 6c 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f  rol(sqlite3*, co
3cbf0 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d  nst char *zDbNam
3cc00 65 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 2a  e, int op, void*
3cc10 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
3cc20 45 46 3a 20 54 65 73 74 69 6e 67 20 49 6e 74 65  EF: Testing Inte
3cc30 72 66 61 63 65 20 7b 48 31 31 34 30 30 7d 20 3c  rface {H11400} <
3cc40 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S30800>.**.** Th
3cc50 65 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63  e sqlite3_test_c
3cc60 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66 61  ontrol() interfa
3cc70 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  ce is used to re
3cc80 61 64 20 6f 75 74 20 69 6e 74 65 72 6e 61 6c 0a  ad out internal.
3cc90 2a 2a 20 73 74 61 74 65 20 6f 66 20 53 51 4c 69  ** state of SQLi
3cca0 74 65 20 61 6e 64 20 74 6f 20 69 6e 6a 65 63 74  te and to inject
3ccb0 20 66 61 75 6c 74 73 20 69 6e 74 6f 20 53 51 4c   faults into SQL
3ccc0 69 74 65 20 66 6f 72 20 74 65 73 74 69 6e 67 0a  ite for testing.
3ccd0 2a 2a 20 70 75 72 70 6f 73 65 73 2e 20 20 54 68  ** purposes.  Th
3cce0 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
3ccf0 72 20 69 73 20 61 6e 20 6f 70 65 72 61 74 69 6f  r is an operatio
3cd00 6e 20 63 6f 64 65 20 74 68 61 74 20 64 65 74 65  n code that dete
3cd10 72 6d 69 6e 65 73 0a 2a 2a 20 74 68 65 20 6e 75  rmines.** the nu
3cd20 6d 62 65 72 2c 20 6d 65 61 6e 69 6e 67 2c 20 61  mber, meaning, a
3cd30 6e 64 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20  nd operation of 
3cd40 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 70  all subsequent p
3cd50 61 72 61 6d 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a  arameters..**.**
3cd60 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   This interface 
3cd70 69 73 20 6e 6f 74 20 66 6f 72 20 75 73 65 20 62  is not for use b
3cd80 79 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 20  y applications. 
3cd90 20 49 74 20 65 78 69 73 74 73 20 73 6f 6c 65 6c   It exists solel
3cda0 79 0a 2a 2a 20 66 6f 72 20 76 65 72 69 66 79 69  y.** for verifyi
3cdb0 6e 67 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f  ng the correct o
3cdc0 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  peration of the 
3cdd0 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20  SQLite library. 
3cde0 20 44 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e   Depending.** on
3cdf0 20 68 6f 77 20 74 68 65 20 53 51 4c 69 74 65 20   how the SQLite 
3ce00 6c 69 62 72 61 72 79 20 69 73 20 63 6f 6d 70 69  library is compi
3ce10 6c 65 64 2c 20 74 68 69 73 20 69 6e 74 65 72 66  led, this interf
3ce20 61 63 65 20 6d 69 67 68 74 20 6e 6f 74 20 65 78  ace might not ex
3ce30 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  ist..**.** The d
3ce40 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20 6f 70  etails of the op
3ce50 65 72 61 74 69 6f 6e 20 63 6f 64 65 73 2c 20 74  eration codes, t
3ce60 68 65 69 72 20 6d 65 61 6e 69 6e 67 73 2c 20 74  heir meanings, t
3ce70 68 65 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a  he parameters.**
3ce80 20 74 68 65 79 20 74 61 6b 65 2c 20 61 6e 64 20   they take, and 
3ce90 77 68 61 74 20 74 68 65 79 20 64 6f 20 61 72 65  what they do are
3cea0 20 61 6c 6c 20 73 75 62 6a 65 63 74 20 74 6f 20   all subject to 
3ceb0 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e  change without n
3cec0 6f 74 69 63 65 2e 0a 2a 2a 20 55 6e 6c 69 6b 65  otice..** Unlike
3ced0 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 53 51 4c   most of the SQL
3cee0 69 74 65 20 41 50 49 2c 20 74 68 69 73 20 66 75  ite API, this fu
3cef0 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 67 75  nction is not gu
3cf00 61 72 61 6e 74 65 65 64 20 74 6f 0a 2a 2a 20 6f  aranteed to.** o
3cf10 70 65 72 61 74 65 20 63 6f 6e 73 69 73 74 65 6e  perate consisten
3cf20 74 6c 79 20 66 72 6f 6d 20 6f 6e 65 20 72 65 6c  tly from one rel
3cf30 65 61 73 65 20 74 6f 20 74 68 65 20 6e 65 78 74  ease to the next
3cf40 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
3cf50 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 65 73 74  int sqlite3_test
3cf60 5f 63 6f 6e 74 72 6f 6c 28 69 6e 74 20 6f 70 2c  _control(int op,
3cf70 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   ...);../*.** CA
3cf80 50 49 33 52 45 46 3a 20 54 65 73 74 69 6e 67 20  PI3REF: Testing 
3cf90 49 6e 74 65 72 66 61 63 65 20 4f 70 65 72 61 74  Interface Operat
3cfa0 69 6f 6e 20 43 6f 64 65 73 20 7b 48 31 31 34 31  ion Codes {H1141
3cfb0 30 7d 20 3c 48 31 31 34 30 30 3e 0a 2a 2a 0a 2a  0} <H11400>.**.*
3cfc0 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74  * These constant
3cfd0 73 20 61 72 65 20 74 68 65 20 76 61 6c 69 64 20  s are the valid 
3cfe0 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64 65 20 70  operation code p
3cff0 61 72 61 6d 65 74 65 72 73 20 75 73 65 64 0a 2a  arameters used.*
3d000 2a 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61  * as the first a
3d010 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c 69  rgument to [sqli
3d020 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  te3_test_control
3d030 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  ()]..**.** These
3d040 20 70 61 72 61 6d 65 74 65 72 73 20 61 6e 64 20   parameters and 
3d050 74 68 65 69 72 20 6d 65 61 6e 69 6e 67 73 20 61  their meanings a
3d060 72 65 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68  re subject to ch
3d070 61 6e 67 65 0a 2a 2a 20 77 69 74 68 6f 75 74 20  ange.** without 
3d080 6e 6f 74 69 63 65 2e 20 20 54 68 65 73 65 20 76  notice.  These v
3d090 61 6c 75 65 73 20 61 72 65 20 66 6f 72 20 74 65  alues are for te
3d0a0 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73 20 6f  sting purposes o
3d0b0 6e 6c 79 2e 0a 2a 2a 20 41 70 70 6c 69 63 61 74  nly..** Applicat
3d0c0 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20  ions should not 
3d0d0 75 73 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65  use any of these
3d0e0 20 70 61 72 61 6d 65 74 65 72 73 20 6f 72 20 74   parameters or t
3d0f0 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 74  he.** [sqlite3_t
3d100 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69  est_control()] i
3d110 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 64 65  nterface..*/.#de
3d120 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54  fine SQLITE_TEST
3d130 43 54 52 4c 5f 50 52 4e 47 5f 53 41 56 45 20 20  CTRL_PRNG_SAVE  
3d140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a                5.
3d150 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54  #define SQLITE_T
3d160 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53  ESTCTRL_PRNG_RES
3d170 54 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 20  TORE            
3d180 20 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   6.#define SQLIT
3d190 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f  E_TESTCTRL_PRNG_
3d1a0 52 45 53 45 54 20 20 20 20 20 20 20 20 20 20 20  RESET           
3d1b0 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 53 51      7.#define SQ
3d1c0 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 42 49  LITE_TESTCTRL_BI
3d1d0 54 56 45 43 5f 54 45 53 54 20 20 20 20 20 20 20  TVEC_TEST       
3d1e0 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65         8.#define
3d1f0 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c   SQLITE_TESTCTRL
3d200 5f 46 41 55 4c 54 5f 49 4e 53 54 41 4c 4c 20 20  _FAULT_INSTALL  
3d210 20 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66            9.#def
3d220 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43  ine SQLITE_TESTC
3d230 54 52 4c 5f 42 45 4e 49 47 4e 5f 4d 41 4c 4c 4f  TRL_BENIGN_MALLO
3d240 43 5f 48 4f 4f 4b 53 20 20 20 20 20 31 30 0a 23  C_HOOKS     10.#
3d250 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
3d260 53 54 43 54 52 4c 5f 50 45 4e 44 49 4e 47 5f 42  STCTRL_PENDING_B
3d270 59 54 45 20 20 20 20 20 20 20 20 20 20 20 20 31  YTE            1
3d280 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  1.#define SQLITE
3d290 5f 54 45 53 54 43 54 52 4c 5f 41 53 53 45 52 54  _TESTCTRL_ASSERT
3d2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3d2b0 20 20 31 32 0a 23 64 65 66 69 6e 65 20 53 51 4c    12.#define SQL
3d2c0 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 4c 57  ITE_TESTCTRL_ALW
3d2d0 41 59 53 20 20 20 20 20 20 20 20 20 20 20 20 20  AYS             
3d2e0 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e 65 20       13.#define 
3d2f0 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
3d300 52 45 53 45 52 56 45 20 20 20 20 20 20 20 20 20  RESERVE         
3d310 20 20 20 20 20 20 20 20 31 34 0a 0a 2f 2a 0a 2a          14../*.*
3d320 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c 69  * CAPI3REF: SQLi
3d330 74 65 20 52 75 6e 74 69 6d 65 20 53 74 61 74 75  te Runtime Statu
3d340 73 20 7b 48 31 37 32 30 30 7d 20 3c 53 36 30 32  s {H17200} <S602
3d350 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  00>.** EXPERIMEN
3d360 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  TAL.**.** This i
3d370 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64  nterface is used
3d380 20 74 6f 20 72 65 74 72 69 65 76 65 20 72 75 6e   to retrieve run
3d390 74 69 6d 65 20 73 74 61 74 75 73 20 69 6e 66 6f  time status info
3d3a0 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74  rmation.** about
3d3b0 20 74 68 65 20 70 72 65 66 6f 72 6d 61 6e 63 65   the preformance
3d3c0 20 6f 66 20 53 51 4c 69 74 65 2c 20 61 6e 64 20   of SQLite, and 
3d3d0 6f 70 74 69 6f 6e 61 6c 6c 79 20 74 6f 20 72 65  optionally to re
3d3e0 73 65 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20 68  set various.** h
3d3f0 69 67 68 77 61 74 65 72 20 6d 61 72 6b 73 2e 20  ighwater marks. 
3d400 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
3d410 65 6e 74 20 69 73 20 61 6e 20 69 6e 74 65 67 65  ent is an intege
3d420 72 20 63 6f 64 65 20 66 6f 72 0a 2a 2a 20 74 68  r code for.** th
3d430 65 20 73 70 65 63 69 66 69 63 20 70 61 72 61 6d  e specific param
3d440 65 74 65 72 20 74 6f 20 6d 65 61 73 75 72 65 2e  eter to measure.
3d450 20 20 52 65 63 6f 67 6e 69 7a 65 64 20 69 6e 74    Recognized int
3d460 65 67 65 72 20 63 6f 64 65 73 0a 2a 2a 20 61 72  eger codes.** ar
3d470 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 5b 53  e of the form [S
3d480 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d  QLITE_STATUS_MEM
3d490 4f 52 59 5f 55 53 45 44 20 7c 20 53 51 4c 49 54  ORY_USED | SQLIT
3d4a0 45 5f 53 54 41 54 55 53 5f 2e 2e 2e 5d 2e 0a 2a  E_STATUS_...]..*
3d4b0 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 76 61  * The current va
3d4c0 6c 75 65 20 6f 66 20 74 68 65 20 70 61 72 61 6d  lue of the param
3d4d0 65 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64  eter is returned
3d4e0 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e   into *pCurrent.
3d4f0 0a 2a 2a 20 54 68 65 20 68 69 67 68 65 73 74 20  .** The highest 
3d500 72 65 63 6f 72 64 65 64 20 76 61 6c 75 65 20 69  recorded value i
3d510 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20 2a 70  s returned in *p
3d520 48 69 67 68 77 61 74 65 72 2e 20 20 49 66 20 74  Highwater.  If t
3d530 68 65 0a 2a 2a 20 72 65 73 65 74 46 6c 61 67 20  he.** resetFlag 
3d540 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68  is true, then th
3d550 65 20 68 69 67 68 65 73 74 20 72 65 63 6f 72 64  e highest record
3d560 20 76 61 6c 75 65 20 69 73 20 72 65 73 65 74 20   value is reset 
3d570 61 66 74 65 72 0a 2a 2a 20 2a 70 48 69 67 68 77  after.** *pHighw
3d580 61 74 65 72 20 69 73 20 77 72 69 74 74 65 6e 2e  ater is written.
3d590 20 53 6f 6d 65 20 70 61 72 61 6d 65 74 65 72 73   Some parameters
3d5a0 20 64 6f 20 6e 6f 74 20 72 65 63 6f 72 64 20 74   do not record t
3d5b0 68 65 20 68 69 67 68 65 73 74 0a 2a 2a 20 76 61  he highest.** va
3d5c0 6c 75 65 2e 20 20 46 6f 72 20 74 68 6f 73 65 20  lue.  For those 
3d5d0 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6e 6f  parameters.** no
3d5e0 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e  thing is written
3d5f0 20 69 6e 74 6f 20 2a 70 48 69 67 68 77 61 74 65   into *pHighwate
3d600 72 20 61 6e 64 20 74 68 65 20 72 65 73 65 74 46  r and the resetF
3d610 6c 61 67 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a  lag is ignored..
3d620 2a 2a 20 4f 74 68 65 72 20 70 61 72 61 6d 65 74  ** Other paramet
3d630 65 72 73 20 72 65 63 6f 72 64 20 6f 6e 6c 79 20  ers record only 
3d640 74 68 65 20 68 69 67 68 77 61 74 65 72 20 6d 61  the highwater ma
3d650 72 6b 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 63  rk and not the c
3d660 75 72 72 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e  urrent.** value.
3d670 20 20 46 6f 72 20 74 68 65 73 65 20 6c 61 74 74    For these latt
3d680 65 72 20 70 61 72 61 6d 65 74 65 72 73 20 6e 6f  er parameters no
3d690 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e  thing is written
3d6a0 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e   into *pCurrent.
3d6b0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
3d6c0 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ine returns SQLI
3d6d0 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73  TE_OK on success
3d6e0 20 61 6e 64 20 61 20 6e 6f 6e 2d 7a 65 72 6f 0a   and a non-zero.
3d6f0 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20  ** [error code] 
3d700 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a  on failure..**.*
3d710 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
3d720 73 20 74 68 72 65 61 64 73 61 66 65 20 62 75 74  s threadsafe but
3d730 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2e 20   is not atomic. 
3d740 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61   This routine ca
3d750 6e 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 77  n be.** called w
3d760 68 69 6c 65 20 6f 74 68 65 72 20 74 68 72 65 61  hile other threa
3d770 64 73 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 74  ds are running t
3d780 68 65 20 73 61 6d 65 20 6f 72 20 64 69 66 66 65  he same or diffe
3d790 72 65 6e 74 20 53 51 4c 69 74 65 0a 2a 2a 20 69  rent SQLite.** i
3d7a0 6e 74 65 72 66 61 63 65 73 2e 20 20 48 6f 77 65  nterfaces.  Howe
3d7b0 76 65 72 20 74 68 65 20 76 61 6c 75 65 73 20 72  ver the values r
3d7c0 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 43 75 72  eturned in *pCur
3d7d0 72 65 6e 74 20 61 6e 64 0a 2a 2a 20 2a 70 48 69  rent and.** *pHi
3d7e0 67 68 77 61 74 65 72 20 72 65 66 6c 65 63 74 20  ghwater reflect 
3d7f0 74 68 65 20 73 74 61 74 75 73 20 6f 66 20 53 51  the status of SQ
3d800 4c 69 74 65 20 61 74 20 64 69 66 66 65 72 65 6e  Lite at differen
3d810 74 20 70 6f 69 6e 74 73 20 69 6e 20 74 69 6d 65  t points in time
3d820 0a 2a 2a 20 61 6e 64 20 69 74 20 69 73 20 70 6f  .** and it is po
3d830 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f 74  ssible that anot
3d840 68 65 72 20 74 68 72 65 61 64 20 6d 69 67 68 74  her thread might
3d850 20 63 68 61 6e 67 65 20 74 68 65 20 70 61 72 61   change the para
3d860 6d 65 74 65 72 0a 2a 2a 20 69 6e 20 62 65 74 77  meter.** in betw
3d870 65 65 6e 20 74 68 65 20 74 69 6d 65 73 20 77 68  een the times wh
3d880 65 6e 20 2a 70 43 75 72 72 65 6e 74 20 61 6e 64  en *pCurrent and
3d890 20 2a 70 48 69 67 68 77 61 74 65 72 20 61 72 65   *pHighwater are
3d8a0 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20   written..**.** 
3d8b0 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74  See also: [sqlit
3d8c0 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 0a  e3_db_status()].
3d8d0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  */.SQLITE_API SQ
3d8e0 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41  LITE_EXPERIMENTA
3d8f0 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74  L int sqlite3_st
3d900 61 74 75 73 28 69 6e 74 20 6f 70 2c 20 69 6e 74  atus(int op, int
3d910 20 2a 70 43 75 72 72 65 6e 74 2c 20 69 6e 74 20   *pCurrent, int 
3d920 2a 70 48 69 67 68 77 61 74 65 72 2c 20 69 6e 74  *pHighwater, int
3d930 20 72 65 73 65 74 46 6c 61 67 29 3b 0a 0a 0a 2f   resetFlag);.../
3d940 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53  *.** CAPI3REF: S
3d950 74 61 74 75 73 20 50 61 72 61 6d 65 74 65 72 73  tatus Parameters
3d960 20 7b 48 31 37 32 35 30 7d 20 3c 48 31 37 32 30   {H17250} <H1720
3d970 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  0>.** EXPERIMENT
3d980 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69  AL.**.** These i
3d990 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73  nteger constants
3d9a0 20 64 65 73 69 67 6e 61 74 65 20 76 61 72 69 6f   designate vario
3d9b0 75 73 20 72 75 6e 2d 74 69 6d 65 20 73 74 61 74  us run-time stat
3d9c0 75 73 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a  us parameters.**
3d9d0 20 74 68 61 74 20 63 61 6e 20 62 65 20 72 65 74   that can be ret
3d9e0 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65  urned by [sqlite
3d9f0 33 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2a 0a  3_status()]..**.
3da00 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53  ** <dl>.** <dt>S
3da10 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d  QLITE_STATUS_MEM
3da20 4f 52 59 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a  ORY_USED</dt>.**
3da30 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65   <dd>This parame
3da40 74 65 72 20 69 73 20 74 68 65 20 63 75 72 72 65  ter is the curre
3da50 6e 74 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d  nt amount of mem
3da60 6f 72 79 20 63 68 65 63 6b 65 64 20 6f 75 74 0a  ory checked out.
3da70 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65  ** using [sqlite
3da80 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 65 69 74  3_malloc()], eit
3da90 68 65 72 20 64 69 72 65 63 74 6c 79 20 6f 72 20  her directly or 
3daa0 69 6e 64 69 72 65 63 74 6c 79 2e 20 20 54 68 65  indirectly.  The
3dab0 0a 2a 2a 20 66 69 67 75 72 65 20 69 6e 63 6c 75  .** figure inclu
3dac0 64 65 73 20 63 61 6c 6c 73 20 6d 61 64 65 20 74  des calls made t
3dad0 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  o [sqlite3_mallo
3dae0 63 28 29 5d 20 62 79 20 74 68 65 20 61 70 70 6c  c()] by the appl
3daf0 69 63 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 69  ication.** and i
3db00 6e 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 75  nternal memory u
3db10 73 61 67 65 20 62 79 20 74 68 65 20 53 51 4c 69  sage by the SQLi
3db20 74 65 20 6c 69 62 72 61 72 79 2e 20 20 53 63 72  te library.  Scr
3db30 61 74 63 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 63  atch memory.** c
3db40 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 5b 53 51  ontrolled by [SQ
3db50 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41  LITE_CONFIG_SCRA
3db60 54 43 48 5d 20 61 6e 64 20 61 75 78 69 6c 69 61  TCH] and auxilia
3db70 72 79 20 70 61 67 65 2d 63 61 63 68 65 0a 2a 2a  ry page-cache.**
3db80 20 6d 65 6d 6f 72 79 20 63 6f 6e 74 72 6f 6c 6c   memory controll
3db90 65 64 20 62 79 20 5b 53 51 4c 49 54 45 5f 43 4f  ed by [SQLITE_CO
3dba0 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 20  NFIG_PAGECACHE] 
3dbb0 69 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20  is not included 
3dbc0 69 6e 0a 2a 2a 20 74 68 69 73 20 70 61 72 61 6d  in.** this param
3dbd0 65 74 65 72 2e 20 20 54 68 65 20 61 6d 6f 75 6e  eter.  The amoun
3dbe0 74 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68  t returned is th
3dbf0 65 20 73 75 6d 20 6f 66 20 74 68 65 20 61 6c 6c  e sum of the all
3dc00 6f 63 61 74 69 6f 6e 0a 2a 2a 20 73 69 7a 65 73  ocation.** sizes
3dc10 20 61 73 20 72 65 70 6f 72 74 65 64 20 62 79 20   as reported by 
3dc20 74 68 65 20 78 53 69 7a 65 20 6d 65 74 68 6f 64  the xSize method
3dc30 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d   in [sqlite3_mem
3dc40 5f 6d 65 74 68 6f 64 73 5d 2e 3c 2f 64 64 3e 0a  _methods].</dd>.
3dc50 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
3dc60 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53  _STATUS_MALLOC_S
3dc70 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  IZE</dt>.** <dd>
3dc80 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72  This parameter r
3dc90 65 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65  ecords the large
3dca0 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  st memory alloca
3dcb0 74 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20  tion request.** 
3dcc0 68 61 6e 64 65 64 20 74 6f 20 5b 73 71 6c 69 74  handed to [sqlit
3dcd0 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20  e3_malloc()] or 
3dce0 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63  [sqlite3_realloc
3dcf0 28 29 5d 20 28 6f 72 20 74 68 65 69 72 0a 2a 2a  ()] (or their.**
3dd00 20 69 6e 74 65 72 6e 61 6c 20 65 71 75 69 76 61   internal equiva
3dd10 6c 65 6e 74 73 29 2e 20 20 4f 6e 6c 79 20 74 68  lents).  Only th
3dd20 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
3dd30 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67   in the.** *pHig
3dd40 68 77 61 74 65 72 20 70 61 72 61 6d 65 74 65 72  hwater parameter
3dd50 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 61   to [sqlite3_sta
3dd60 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e 74  tus()] is of int
3dd70 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20  erest.  .** The 
3dd80 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69 6e  value written in
3dd90 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e 74  to the *pCurrent
3dda0 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 6e   parameter is un
3ddb0 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a  defined.</dd>.**
3ddc0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53  .** <dt>SQLITE_S
3ddd0 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f  TATUS_PAGECACHE_
3dde0 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  USED</dt>.** <dd
3ddf0 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20  >This parameter 
3de00 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  returns the numb
3de10 65 72 20 6f 66 20 70 61 67 65 73 20 75 73 65 64  er of pages used
3de20 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 5b   out of the.** [
3de30 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 79  pagecache memory
3de40 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 74 68 61 74   allocator] that
3de50 20 77 61 73 20 63 6f 6e 66 69 67 75 72 65 64 20   was configured 
3de60 75 73 69 6e 67 20 0a 2a 2a 20 5b 53 51 4c 49 54  using .** [SQLIT
3de70 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43  E_CONFIG_PAGECAC
3de80 48 45 5d 2e 20 20 54 68 65 0a 2a 2a 20 76 61 6c  HE].  The.** val
3de90 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 69  ue returned is i
3dea0 6e 20 70 61 67 65 73 2c 20 6e 6f 74 20 69 6e 20  n pages, not in 
3deb0 62 79 74 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  bytes.</dd>.**.*
3dec0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41  * <dt>SQLITE_STA
3ded0 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56  TUS_PAGECACHE_OV
3dee0 45 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c  ERFLOW</dt>.** <
3def0 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65  dd>This paramete
3df00 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  r returns the nu
3df10 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
3df20 20 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 61   page cache.** a
3df30 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 69 63 68 20  llocation which 
3df40 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 74 61  could not be sta
3df50 74 69 73 66 69 65 64 20 62 79 20 74 68 65 20 5b  tisfied by the [
3df60 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41  SQLITE_CONFIG_PA
3df70 47 45 43 41 43 48 45 5d 0a 2a 2a 20 62 75 66 66  GECACHE].** buff
3df80 65 72 20 61 6e 64 20 77 68 65 72 65 20 66 6f 72  er and where for
3df90 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20  ced to overflow 
3dfa0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  to [sqlite3_mall
3dfb0 6f 63 28 29 5d 2e 20 20 54 68 65 0a 2a 2a 20 72  oc()].  The.** r
3dfc0 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 6e  eturned value in
3dfd0 63 6c 75 64 65 73 20 61 6c 6c 6f 63 61 74 69 6f  cludes allocatio
3dfe0 6e 73 20 74 68 61 74 20 6f 76 65 72 66 6c 6f 77  ns that overflow
3dff0 65 64 20 62 65 63 61 75 73 65 20 74 68 65 79 0a  ed because they.
3e000 2a 2a 20 77 68 65 72 65 20 74 6f 6f 20 6c 61 72  ** where too lar
3e010 67 65 20 28 74 68 65 79 20 77 65 72 65 20 6c 61  ge (they were la
3e020 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 22 73  rger than the "s
3e030 7a 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0a  z" parameter to.
3e040 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ** [SQLITE_CONFI
3e050 47 5f 50 41 47 45 43 41 43 48 45 5d 29 20 61 6e  G_PAGECACHE]) an
3e060 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 68  d allocations th
3e070 61 74 20 6f 76 65 72 66 6c 6f 77 65 64 20 62 65  at overflowed be
3e080 63 61 75 73 65 0a 2a 2a 20 6e 6f 20 73 70 61 63  cause.** no spac
3e090 65 20 77 61 73 20 6c 65 66 74 20 69 6e 20 74 68  e was left in th
3e0a0 65 20 70 61 67 65 20 63 61 63 68 65 2e 3c 2f 64  e page cache.</d
3e0b0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
3e0c0 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43  ITE_STATUS_PAGEC
3e0d0 41 43 48 45 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a  ACHE_SIZE</dt>.*
3e0e0 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d  * <dd>This param
3e0f0 65 74 65 72 20 72 65 63 6f 72 64 73 20 74 68 65  eter records the
3e100 20 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 20   largest memory 
3e110 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65  allocation reque
3e120 73 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20  st.** handed to 
3e130 5b 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72  [pagecache memor
3e140 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f  y allocator].  O
3e150 6e 6c 79 20 74 68 65 20 76 61 6c 75 65 20 72 65  nly the value re
3e160 74 75 72 6e 65 64 20 69 6e 20 74 68 65 0a 2a 2a  turned in the.**
3e170 20 2a 70 48 69 67 68 77 61 74 65 72 20 70 61 72   *pHighwater par
3e180 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74  ameter to [sqlit
3e190 65 33 5f 73 74 61 74 75 73 28 29 5d 20 69 73 20  e3_status()] is 
3e1a0 6f 66 20 69 6e 74 65 72 65 73 74 2e 20 20 0a 2a  of interest.  .*
3e1b0 2a 20 54 68 65 20 76 61 6c 75 65 20 77 72 69 74  * The value writ
3e1c0 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a 70 43  ten into the *pC
3e1d0 75 72 72 65 6e 74 20 70 61 72 61 6d 65 74 65 72  urrent parameter
3e1e0 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f   is undefined.</
3e1f0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
3e200 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41  LITE_STATUS_SCRA
3e210 54 43 48 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a  TCH_USED</dt>.**
3e220 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65   <dd>This parame
3e230 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20  ter returns the 
3e240 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61  number of alloca
3e250 74 69 6f 6e 73 20 75 73 65 64 20 6f 75 74 20 6f  tions used out o
3e260 66 20 74 68 65 0a 2a 2a 20 5b 73 63 72 61 74 63  f the.** [scratc
3e270 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  h memory allocat
3e280 6f 72 5d 20 63 6f 6e 66 69 67 75 72 65 64 20 75  or] configured u
3e290 73 69 6e 67 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  sing.** [SQLITE_
3e2a0 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 2e  CONFIG_SCRATCH].
3e2b0 20 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75    The value retu
3e2c0 72 6e 65 64 20 69 73 20 69 6e 20 61 6c 6c 6f 63  rned is in alloc
3e2d0 61 74 69 6f 6e 73 2c 20 6e 6f 74 0a 2a 2a 20 69  ations, not.** i
3e2e0 6e 20 62 79 74 65 73 2e 20 20 53 69 6e 63 65 20  n bytes.  Since 
3e2f0 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 20  a single thread 
3e300 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 6f 6e  may only have on
3e310 65 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 61  e scratch alloca
3e320 74 69 6f 6e 0a 2a 2a 20 6f 75 74 73 74 61 6e 64  tion.** outstand
3e330 69 6e 67 20 61 74 20 74 69 6d 65 2c 20 74 68 69  ing at time, thi
3e340 73 20 70 61 72 61 6d 65 74 65 72 20 61 6c 73 6f  s parameter also
3e350 20 72 65 70 6f 72 74 73 20 74 68 65 20 6e 75 6d   reports the num
3e360 62 65 72 20 6f 66 20 74 68 72 65 61 64 73 0a 2a  ber of threads.*
3e370 2a 20 75 73 69 6e 67 20 73 63 72 61 74 63 68 20  * using scratch 
3e380 6d 65 6d 6f 72 79 20 61 74 20 74 68 65 20 73 61  memory at the sa
3e390 6d 65 20 74 69 6d 65 2e 3c 2f 64 64 3e 0a 2a 2a  me time.</dd>.**
3e3a0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53  .** <dt>SQLITE_S
3e3b0 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56  TATUS_SCRATCH_OV
3e3c0 45 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c  ERFLOW</dt>.** <
3e3d0 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65  dd>This paramete
3e3e0 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  r returns the nu
3e3f0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
3e400 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 0a   scratch memory.
3e410 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 68  ** allocation wh
3e420 69 63 68 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65  ich could not be
3e430 20 73 74 61 74 69 73 66 69 65 64 20 62 79 20 74   statisfied by t
3e440 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  he [SQLITE_CONFI
3e450 47 5f 53 43 52 41 54 43 48 5d 0a 2a 2a 20 62 75  G_SCRATCH].** bu
3e460 66 66 65 72 20 61 6e 64 20 77 68 65 72 65 20 66  ffer and where f
3e470 6f 72 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f  orced to overflo
3e480 77 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61  w to [sqlite3_ma
3e490 6c 6c 6f 63 28 29 5d 2e 20 20 54 68 65 20 76 61  lloc()].  The va
3e4a0 6c 75 65 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64  lues.** returned
3e4b0 20 69 6e 63 6c 75 64 65 20 6f 76 65 72 66 6c 6f   include overflo
3e4c0 77 73 20 62 65 63 61 75 73 65 20 74 68 65 20 72  ws because the r
3e4d0 65 71 75 65 73 74 65 64 20 61 6c 6c 6f 63 61 74  equested allocat
3e4e0 69 6f 6e 20 77 61 73 20 74 6f 6f 0a 2a 2a 20 6c  ion was too.** l
3e4f0 61 72 67 65 72 20 28 74 68 61 74 20 69 73 2c 20  arger (that is, 
3e500 62 65 63 61 75 73 65 20 74 68 65 20 72 65 71 75  because the requ
3e510 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e  ested allocation
3e520 20 77 61 73 20 6c 61 72 67 65 72 20 74 68 61 6e   was larger than
3e530 20 74 68 65 0a 2a 2a 20 22 73 7a 22 20 70 61 72   the.** "sz" par
3e540 61 6d 65 74 65 72 20 74 6f 20 5b 53 51 4c 49 54  ameter to [SQLIT
3e550 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48  E_CONFIG_SCRATCH
3e560 5d 29 20 61 6e 64 20 62 65 63 61 75 73 65 20 6e  ]) and because n
3e570 6f 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72  o scratch buffer
3e580 0a 2a 2a 20 73 6c 6f 74 73 20 77 65 72 65 20 61  .** slots were a
3e590 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 3c 2f 64  vailable..** </d
3e5a0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
3e5b0 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54  ITE_STATUS_SCRAT
3e5c0 43 48 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20  CH_SIZE</dt>.** 
3e5d0 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74  <dd>This paramet
3e5e0 65 72 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c  er records the l
3e5f0 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 61 6c  argest memory al
3e600 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74  location request
3e610 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b 73  .** handed to [s
3e620 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 61 6c  cratch memory al
3e630 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20  locator].  Only 
3e640 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
3e650 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48  ed in the.** *pH
3e660 69 67 68 77 61 74 65 72 20 70 61 72 61 6d 65 74  ighwater paramet
3e670 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73  er to [sqlite3_s
3e680 74 61 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69  tatus()] is of i
3e690 6e 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68  nterest.  .** Th
3e6a0 65 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20  e value written 
3e6b0 69 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65  into the *pCurre
3e6c0 6e 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  nt parameter is 
3e6d0 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a  undefined.</dd>.
3e6e0 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
3e6f0 5f 53 54 41 54 55 53 5f 50 41 52 53 45 52 5f 53  _STATUS_PARSER_S
3e700 54 41 43 4b 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  TACK</dt>.** <dd
3e710 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20  >This parameter 
3e720 72 65 63 6f 72 64 73 20 74 68 65 20 64 65 65 70  records the deep
3e730 65 73 74 20 70 61 72 73 65 72 20 73 74 61 63 6b  est parser stack
3e740 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 0a 2a 2a  .  It is only.**
3e750 20 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66 20 53   meaningful if S
3e760 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65  QLite is compile
3e770 64 20 77 69 74 68 20 5b 59 59 54 52 41 43 4b 4d  d with [YYTRACKM
3e780 41 58 53 54 41 43 4b 44 45 50 54 48 5d 2e 3c 2f  AXSTACKDEPTH].</
3e790 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a  dd>.** </dl>.**.
3e7a0 2a 2a 20 4e 65 77 20 73 74 61 74 75 73 20 70 61  ** New status pa
3e7b0 72 61 6d 65 74 65 72 73 20 6d 61 79 20 62 65 20  rameters may be 
3e7c0 61 64 64 65 64 20 66 72 6f 6d 20 74 69 6d 65 20  added from time 
3e7d0 74 6f 20 74 69 6d 65 2e 0a 2a 2f 0a 23 64 65 66  to time..*/.#def
3e7e0 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55  ine SQLITE_STATU
3e7f0 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 20 20 20  S_MEMORY_USED   
3e800 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65         0.#define
3e810 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50   SQLITE_STATUS_P
3e820 41 47 45 43 41 43 48 45 5f 55 53 45 44 20 20 20  AGECACHE_USED   
3e830 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51      1.#define SQ
3e840 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45  LITE_STATUS_PAGE
3e850 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 20 20  CACHE_OVERFLOW  
3e860 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   2.#define SQLIT
3e870 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48  E_STATUS_SCRATCH
3e880 5f 55 53 45 44 20 20 20 20 20 20 20 20 20 33 0a  _USED         3.
3e890 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
3e8a0 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56  TATUS_SCRATCH_OV
3e8b0 45 52 46 4c 4f 57 20 20 20 20 20 34 0a 23 64 65  ERFLOW     4.#de
3e8c0 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54  fine SQLITE_STAT
3e8d0 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 20 20  US_MALLOC_SIZE  
3e8e0 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e          5.#defin
3e8f0 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  e SQLITE_STATUS_
3e900 50 41 52 53 45 52 5f 53 54 41 43 4b 20 20 20 20  PARSER_STACK    
3e910 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 53       6.#define S
3e920 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47  QLITE_STATUS_PAG
3e930 45 43 41 43 48 45 5f 53 49 5a 45 20 20 20 20 20  ECACHE_SIZE     
3e940 20 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    7.#define SQLI
3e950 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43  TE_STATUS_SCRATC
3e960 48 5f 53 49 5a 45 20 20 20 20 20 20 20 20 20 38  H_SIZE         8
3e970 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
3e980 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65  : Database Conne
3e990 63 74 69 6f 6e 20 53 74 61 74 75 73 20 7b 48 31  ction Status {H1
3e9a0 37 35 30 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a  7500} <S60200>.*
3e9b0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
3e9c0 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66  *.** This interf
3e9d0 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 72  ace is used to r
3e9e0 65 74 72 69 65 76 65 20 72 75 6e 74 69 6d 65 20  etrieve runtime 
3e9f0 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69  status informati
3ea00 6f 6e 20 0a 2a 2a 20 61 62 6f 75 74 20 61 20 73  on .** about a s
3ea10 69 6e 67 6c 65 20 5b 64 61 74 61 62 61 73 65 20  ingle [database 
3ea20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 54 68  connection].  Th
3ea30 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
3ea40 20 69 73 20 74 68 65 0a 2a 2a 20 64 61 74 61 62   is the.** datab
3ea50 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f  ase connection o
3ea60 62 6a 65 63 74 20 74 6f 20 62 65 20 69 6e 74 65  bject to be inte
3ea70 72 72 6f 67 61 74 65 64 2e 20 20 54 68 65 20 73  rrogated.  The s
3ea80 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a  econd argument.*
3ea90 2a 20 69 73 20 74 68 65 20 70 61 72 61 6d 65 74  * is the paramet
3eaa0 65 72 20 74 6f 20 69 6e 74 65 72 72 6f 67 61 74  er to interrogat
3eab0 65 2e 20 20 43 75 72 72 65 6e 74 6c 79 2c 20 74  e.  Currently, t
3eac0 68 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20  he only allowed 
3ead0 76 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65  value.** for the
3eae0 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
3eaf0 72 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 42 53  r is [SQLITE_DBS
3eb00 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f  TATUS_LOOKASIDE_
3eb10 55 53 45 44 5d 2e 0a 2a 2a 20 41 64 64 69 74 69  USED]..** Additi
3eb20 6f 6e 61 6c 20 6f 70 74 69 6f 6e 73 20 77 69 6c  onal options wil
3eb30 6c 20 6c 69 6b 65 6c 79 20 61 70 70 65 61 72 20  l likely appear 
3eb40 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73  in future releas
3eb50 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a  es of SQLite..**
3eb60 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20  .** The current 
3eb70 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 65 71  value of the req
3eb80 75 65 73 74 65 64 20 70 61 72 61 6d 65 74 65 72  uested parameter
3eb90 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f   is written into
3eba0 20 2a 70 43 75 72 0a 2a 2a 20 61 6e 64 20 74 68   *pCur.** and th
3ebb0 65 20 68 69 67 68 65 73 74 20 69 6e 73 74 61 6e  e highest instan
3ebc0 74 61 6e 65 6f 75 73 20 76 61 6c 75 65 20 69 73  taneous value is
3ebd0 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70   written into *p
3ebe0 48 69 77 74 72 2e 20 20 49 66 0a 2a 2a 20 74 68  Hiwtr.  If.** th
3ebf0 65 20 72 65 73 65 74 46 6c 67 20 69 73 20 74 72  e resetFlg is tr
3ec00 75 65 2c 20 74 68 65 6e 20 74 68 65 20 68 69 67  ue, then the hig
3ec10 68 65 73 74 20 69 6e 73 74 61 6e 74 61 6e 65 6f  hest instantaneo
3ec20 75 73 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 72  us value is.** r
3ec30 65 73 65 74 20 62 61 63 6b 20 64 6f 77 6e 20 74  eset back down t
3ec40 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61  o the current va
3ec50 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  lue..**.** See a
3ec60 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74  lso: [sqlite3_st
3ec70 61 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c  atus()] and [sql
3ec80 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73  ite3_stmt_status
3ec90 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ()]..*/.SQLITE_A
3eca0 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49  PI SQLITE_EXPERI
3ecb0 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74  MENTAL int sqlit
3ecc0 65 33 5f 64 62 5f 73 74 61 74 75 73 28 73 71 6c  e3_db_status(sql
3ecd0 69 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 69  ite3*, int op, i
3ece0 6e 74 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70  nt *pCur, int *p
3ecf0 48 69 77 74 72 2c 20 69 6e 74 20 72 65 73 65 74  Hiwtr, int reset
3ed00 46 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  Flg);../*.** CAP
3ed10 49 33 52 45 46 3a 20 53 74 61 74 75 73 20 50 61  I3REF: Status Pa
3ed20 72 61 6d 65 74 65 72 73 20 66 6f 72 20 64 61 74  rameters for dat
3ed30 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
3ed40 73 20 7b 48 31 37 35 32 30 7d 20 3c 48 31 37 35  s {H17520} <H175
3ed50 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  00>.** EXPERIMEN
3ed60 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  TAL.**.** These 
3ed70 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74 68  constants are th
3ed80 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 74 65  e available inte
3ed90 67 65 72 20 22 76 65 72 62 73 22 20 74 68 61 74  ger "verbs" that
3eda0 20 63 61 6e 20 62 65 20 70 61 73 73 65 64 20 61   can be passed a
3edb0 73 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20  s.** the second 
3edc0 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
3edd0 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74  [sqlite3_db_stat
3ede0 75 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e  us()] interface.
3edf0 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 76 65 72 62 73  .**.** New verbs
3ee00 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e   may be added in
3ee10 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73   future releases
3ee20 20 6f 66 20 53 51 4c 69 74 65 2e 20 45 78 69 73   of SQLite. Exis
3ee30 74 69 6e 67 20 76 65 72 62 73 0a 2a 2a 20 6d 69  ting verbs.** mi
3ee40 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74 69 6e  ght be discontin
3ee50 75 65 64 2e 20 41 70 70 6c 69 63 61 74 69 6f 6e  ued. Application
3ee60 73 20 73 68 6f 75 6c 64 20 63 68 65 63 6b 20 74  s should check t
3ee70 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 66  he return code f
3ee80 72 6f 6d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  rom.** [sqlite3_
3ee90 64 62 5f 73 74 61 74 75 73 28 29 5d 20 74 6f 20  db_status()] to 
3eea0 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74  make sure that t
3eeb0 68 65 20 63 61 6c 6c 20 77 6f 72 6b 65 64 2e 0a  he call worked..
3eec0 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  ** The [sqlite3_
3eed0 64 62 5f 73 74 61 74 75 73 28 29 5d 20 69 6e 74  db_status()] int
3eee0 65 72 66 61 63 65 20 77 69 6c 6c 20 72 65 74 75  erface will retu
3eef0 72 6e 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 65 72  rn a non-zero er
3ef00 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 69 66 20 61  ror code.** if a
3ef10 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 20 6f 72   discontinued or
3ef20 20 75 6e 73 75 70 70 6f 72 74 65 64 20 76 65 72   unsupported ver
3ef30 62 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a  b is invoked..**
3ef40 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e  .** <dl>.** <dt>
3ef50 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f  SQLITE_DBSTATUS_
3ef60 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3c 2f  LOOKASIDE_USED</
3ef70 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20  dt>.** <dd>This 
3ef80 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e  parameter return
3ef90 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
3efa0 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79  lookaside memory
3efb0 20 73 6c 6f 74 73 20 63 75 72 72 65 6e 74 6c 79   slots currently
3efc0 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75 74 2e  .** checked out.
3efd0 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a  </dd>.** </dl>.*
3efe0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
3eff0 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53  _DBSTATUS_LOOKAS
3f000 49 44 45 5f 55 53 45 44 20 20 20 20 20 30 0a 0a  IDE_USED     0..
3f010 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
3f020 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d   Prepared Statem
3f030 65 6e 74 20 53 74 61 74 75 73 20 7b 48 31 37 35  ent Status {H175
3f040 35 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20  50} <S60200>.** 
3f050 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
3f060 2a 2a 20 45 61 63 68 20 70 72 65 70 61 72 65 64  ** Each prepared
3f070 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 69 6e 74   statement maint
3f080 61 69 6e 73 20 76 61 72 69 6f 75 73 0a 2a 2a 20  ains various.** 
3f090 5b 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54  [SQLITE_STMTSTAT
3f0a0 55 53 5f 53 4f 52 54 20 7c 20 63 6f 75 6e 74 65  US_SORT | counte
3f0b0 72 73 5d 20 74 68 61 74 20 6d 65 61 73 75 72 65  rs] that measure
3f0c0 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f   the number.** o
3f0d0 66 20 74 69 6d 65 73 20 69 74 20 68 61 73 20 70  f times it has p
3f0e0 65 72 66 6f 72 6d 65 64 20 73 70 65 63 69 66 69  erformed specifi
3f0f0 63 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 54  c operations.  T
3f100 68 65 73 65 20 63 6f 75 6e 74 65 72 73 20 63 61  hese counters ca
3f110 6e 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20  n.** be used to 
3f120 6d 6f 6e 69 74 6f 72 20 74 68 65 20 70 65 72 66  monitor the perf
3f130 6f 72 6d 61 6e 63 65 20 63 68 61 72 61 63 74 65  ormance characte
3f140 72 69 73 74 69 63 73 20 6f 66 20 74 68 65 20 70  ristics of the p
3f150 72 65 70 61 72 65 64 0a 2a 2a 20 73 74 61 74 65  repared.** state
3f160 6d 65 6e 74 73 2e 20 20 46 6f 72 20 65 78 61 6d  ments.  For exam
3f170 70 6c 65 2c 20 69 66 20 74 68 65 20 6e 75 6d 62  ple, if the numb
3f180 65 72 20 6f 66 20 74 61 62 6c 65 20 73 74 65 70  er of table step
3f190 73 20 67 72 65 61 74 6c 79 20 65 78 63 65 65 64  s greatly exceed
3f1a0 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20  s.** the number 
3f1b0 6f 66 20 74 61 62 6c 65 20 73 65 61 72 63 68 65  of table searche
3f1c0 73 20 6f 72 20 72 65 73 75 6c 74 20 72 6f 77 73  s or result rows
3f1d0 2c 20 74 68 61 74 20 77 6f 75 6c 64 20 74 65 6e  , that would ten
3f1e0 64 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a  d to indicate.**
3f1f0 20 74 68 61 74 20 74 68 65 20 70 72 65 70 61 72   that the prepar
3f200 65 64 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  ed statement is 
3f210 75 73 69 6e 67 20 61 20 66 75 6c 6c 20 74 61 62  using a full tab
3f220 6c 65 20 73 63 61 6e 20 72 61 74 68 65 72 20 74  le scan rather t
3f230 68 61 6e 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e  han.** an index.
3f240 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e    .**.** This in
3f250 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20  terface is used 
3f260 74 6f 20 72 65 74 72 69 65 76 65 20 61 6e 64 20  to retrieve and 
3f270 72 65 73 65 74 20 63 6f 75 6e 74 65 72 20 76 61  reset counter va
3f280 6c 75 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 20 5b  lues from.** a [
3f290 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
3f2a0 6e 74 5d 2e 20 20 54 68 65 20 66 69 72 73 74 20  nt].  The first 
3f2b0 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
3f2c0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
3f2d0 6e 74 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 6f 20  nt.** object to 
3f2e0 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65 64 2e  be interrogated.
3f2f0 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67    The second arg
3f300 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 6e 20 69  ument.** is an i
3f310 6e 74 65 67 65 72 20 63 6f 64 65 20 66 6f 72 20  nteger code for 
3f320 61 20 73 70 65 63 69 66 69 63 20 5b 53 51 4c 49  a specific [SQLI
3f330 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f  TE_STMTSTATUS_SO
3f340 52 54 20 7c 20 63 6f 75 6e 74 65 72 5d 0a 2a 2a  RT | counter].**
3f350 20 74 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61   to be interroga
3f360 74 65 64 2e 20 0a 2a 2a 20 54 68 65 20 63 75 72  ted. .** The cur
3f370 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68  rent value of th
3f380 65 20 72 65 71 75 65 73 74 65 64 20 63 6f 75 6e  e requested coun
3f390 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ter is returned.
3f3a0 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 65 74  .** If the reset
3f3b0 46 6c 67 20 69 73 20 74 72 75 65 2c 20 74 68 65  Flg is true, the
3f3c0 6e 20 74 68 65 20 63 6f 75 6e 74 65 72 20 69 73  n the counter is
3f3d0 20 72 65 73 65 74 20 74 6f 20 7a 65 72 6f 20 61   reset to zero a
3f3e0 66 74 65 72 20 74 68 69 73 0a 2a 2a 20 69 6e 74  fter this.** int
3f3f0 65 72 66 61 63 65 20 63 61 6c 6c 20 72 65 74 75  erface call retu
3f400 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  rns..**.** See a
3f410 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74  lso: [sqlite3_st
3f420 61 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c  atus()] and [sql
3f430 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29  ite3_db_status()
3f440 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  ]..*/.SQLITE_API
3f450 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45   SQLITE_EXPERIME
3f460 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33  NTAL int sqlite3
3f470 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 73 71 6c  _stmt_status(sql
3f480 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20  ite3_stmt*, int 
3f490 6f 70 2c 69 6e 74 20 72 65 73 65 74 46 6c 67 29  op,int resetFlg)
3f4a0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
3f4b0 46 3a 20 53 74 61 74 75 73 20 50 61 72 61 6d 65  F: Status Parame
3f4c0 74 65 72 73 20 66 6f 72 20 70 72 65 70 61 72 65  ters for prepare
3f4d0 64 20 73 74 61 74 65 6d 65 6e 74 73 20 7b 48 31  d statements {H1
3f4e0 37 35 37 30 7d 20 3c 48 31 37 35 35 30 3e 0a 2a  7570} <H17550>.*
3f4f0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
3f500 2a 0a 2a 2a 20 54 68 65 73 65 20 70 72 65 70 72  *.** These prepr
3f510 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 73 20 64  ocessor macros d
3f520 65 66 69 6e 65 20 69 6e 74 65 67 65 72 20 63 6f  efine integer co
3f530 64 65 73 20 74 68 61 74 20 6e 61 6d 65 20 63 6f  des that name co
3f540 75 6e 74 65 72 0a 2a 2a 20 76 61 6c 75 65 73 20  unter.** values 
3f550 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
3f560 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d  the [sqlite3_stm
3f570 74 5f 73 74 61 74 75 73 28 29 5d 20 69 6e 74 65  t_status()] inte
3f580 72 66 61 63 65 2e 0a 2a 2a 20 54 68 65 20 6d 65  rface..** The me
3f590 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 76 61  anings of the va
3f5a0 72 69 6f 75 73 20 63 6f 75 6e 74 65 72 73 20 61  rious counters a
3f5b0 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  re as follows:.*
3f5c0 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74  *.** <dl>.** <dt
3f5d0 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54  >SQLITE_STMTSTAT
3f5e0 55 53 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50  US_FULLSCAN_STEP
3f5f0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
3f600 73 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  s is the number 
3f610 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 53 51  of times that SQ
3f620 4c 69 74 65 20 68 61 73 20 73 74 65 70 70 65 64  Lite has stepped
3f630 20 66 6f 72 77 61 72 64 20 69 6e 0a 2a 2a 20 61   forward in.** a
3f640 20 74 61 62 6c 65 20 61 73 20 70 61 72 74 20 6f   table as part o
3f650 66 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 73  f a full table s
3f660 63 61 6e 2e 20 20 4c 61 72 67 65 20 6e 75 6d 62  can.  Large numb
3f670 65 72 73 20 66 6f 72 20 74 68 69 73 20 63 6f 75  ers for this cou
3f680 6e 74 65 72 0a 2a 2a 20 6d 61 79 20 69 6e 64 69  nter.** may indi
3f690 63 61 74 65 20 6f 70 70 6f 72 74 75 6e 69 74 69  cate opportuniti
3f6a0 65 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e  es for performan
3f6b0 63 65 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 74  ce improvement t
3f6c0 68 72 6f 75 67 68 20 0a 2a 2a 20 63 61 72 65 66  hrough .** caref
3f6d0 75 6c 20 75 73 65 20 6f 66 20 69 6e 64 69 63 65  ul use of indice
3f6e0 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  s.</dd>.**.** <d
3f6f0 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41  t>SQLITE_STMTSTA
3f700 54 55 53 5f 53 4f 52 54 3c 2f 64 74 3e 0a 2a 2a  TUS_SORT</dt>.**
3f710 20 3c 64 64 3e 54 68 69 73 20 69 73 20 74 68 65   <dd>This is the
3f720 20 6e 75 6d 62 65 72 20 6f 66 20 73 6f 72 74 20   number of sort 
3f730 6f 70 65 72 61 74 69 6f 6e 73 20 74 68 61 74 20  operations that 
3f740 68 61 76 65 20 6f 63 63 75 72 72 65 64 2e 0a 2a  have occurred..*
3f750 2a 20 41 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c  * A non-zero val
3f760 75 65 20 69 6e 20 74 68 69 73 20 63 6f 75 6e 74  ue in this count
3f770 65 72 20 6d 61 79 20 69 6e 64 69 63 61 74 65 20  er may indicate 
3f780 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74  an opportunity t
3f790 6f 0a 2a 2a 20 69 6d 70 72 6f 76 65 6d 65 6e 74  o.** improvement
3f7a0 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 68 72   performance thr
3f7b0 6f 75 67 68 20 63 61 72 65 66 75 6c 20 75 73 65  ough careful use
3f7c0 20 6f 66 20 69 6e 64 69 63 65 73 2e 3c 2f 64 64   of indices.</dd
3f7d0 3e 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f  >.**.** </dl>.*/
3f7e0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3f7f0 53 54 4d 54 53 54 41 54 55 53 5f 46 55 4c 4c 53  STMTSTATUS_FULLS
3f800 43 41 4e 5f 53 54 45 50 20 20 20 20 20 31 0a 23  CAN_STEP     1.#
3f810 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54  define SQLITE_ST
3f820 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 20 20 20  MTSTATUS_SORT   
3f830 20 20 20 20 20 20 20 20 20 20 20 32 0a 0a 2f 2a             2../*
3f840 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 75  .** CAPI3REF: Cu
3f850 73 74 6f 6d 20 50 61 67 65 20 43 61 63 68 65 20  stom Page Cache 
3f860 4f 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45 52 49  Object.** EXPERI
3f870 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65  MENTAL.**.** The
3f880 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20   sqlite3_pcache 
3f890 74 79 70 65 20 69 73 20 6f 70 61 71 75 65 2e 20  type is opaque. 
3f8a0 20 49 74 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74   It is implement
3f8b0 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 6c 75  ed by.** the plu
3f8c0 67 67 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 20 20  ggable module.  
3f8d0 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20  The SQLite core 
3f8e0 68 61 73 20 6e 6f 20 6b 6e 6f 77 6c 65 64 67 65  has no knowledge
3f8f0 20 6f 66 0a 2a 2a 20 69 74 73 20 73 69 7a 65 20   of.** its size 
3f900 6f 72 20 69 6e 74 65 72 6e 61 6c 20 73 74 72 75  or internal stru
3f910 63 74 75 72 65 20 61 6e 64 20 6e 65 76 65 72 20  cture and never 
3f920 64 65 61 6c 73 20 77 69 74 68 20 74 68 65 0a 2a  deals with the.*
3f930 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  * sqlite3_pcache
3f940 20 6f 62 6a 65 63 74 20 65 78 63 65 70 74 20 62   object except b
3f950 79 20 68 6f 6c 64 69 6e 67 20 61 6e 64 20 70 61  y holding and pa
3f960 73 73 69 6e 67 20 70 6f 69 6e 74 65 72 73 0a 2a  ssing pointers.*
3f970 2a 20 74 6f 20 74 68 65 20 6f 62 6a 65 63 74 2e  * to the object.
3f980 0a 2a 2a 0a 2a 2a 20 53 65 65 20 5b 73 71 6c 69  .**.** See [sqli
3f990 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f  te3_pcache_metho
3f9a0 64 73 5d 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  ds] for addition
3f9b0 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  al information..
3f9c0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
3f9d0 74 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  t sqlite3_pcache
3f9e0 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 3b   sqlite3_pcache;
3f9f0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
3fa00 3a 20 41 70 70 6c 69 63 61 74 69 6f 6e 20 44 65  : Application De
3fa10 66 69 6e 65 64 20 50 61 67 65 20 43 61 63 68 65  fined Page Cache
3fa20 2e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b  ..** KEYWORDS: {
3fa30 70 61 67 65 20 63 61 63 68 65 7d 0a 2a 2a 20 45  page cache}.** E
3fa40 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a  XPERIMENTAL.**.*
3fa50 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63  * The [sqlite3_c
3fa60 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43  onfig]([SQLITE_C
3fa70 4f 4e 46 49 47 5f 50 43 41 43 48 45 5d 2c 20 2e  ONFIG_PCACHE], .
3fa80 2e 2e 29 20 69 6e 74 65 72 66 61 63 65 20 63 61  ..) interface ca
3fa90 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 61 6e  n.** register an
3faa0 20 61 6c 74 65 72 6e 61 74 69 76 65 20 70 61 67   alternative pag
3fab0 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e  e cache implemen
3fac0 74 61 74 69 6f 6e 20 62 79 20 70 61 73 73 69 6e  tation by passin
3fad0 67 20 69 6e 20 61 6e 20 0a 2a 2a 20 69 6e 73 74  g in an .** inst
3fae0 61 6e 63 65 20 6f 66 20 74 68 65 20 73 71 6c 69  ance of the sqli
3faf0 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f  te3_pcache_metho
3fb00 64 73 20 73 74 72 75 63 74 75 72 65 2e 20 54 68  ds structure. Th
3fb10 65 20 6d 61 6a 6f 72 69 74 79 20 6f 66 20 74 68  e majority of th
3fb20 65 20 0a 2a 2a 20 68 65 61 70 20 6d 65 6d 6f 72  e .** heap memor
3fb30 79 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65  y used by SQLite
3fb40 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20   is used by the 
3fb50 70 61 67 65 20 63 61 63 68 65 20 74 6f 20 63 61  page cache to ca
3fb60 63 68 65 20 64 61 74 61 20 72 65 61 64 20 0a 2a  che data read .*
3fb70 2a 20 66 72 6f 6d 2c 20 6f 72 20 72 65 61 64 79  * from, or ready
3fb80 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 74   to be written t
3fb90 6f 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20  o, the database 
3fba0 66 69 6c 65 2e 20 42 79 20 69 6d 70 6c 65 6d 65  file. By impleme
3fbb0 6e 74 69 6e 67 20 61 20 0a 2a 2a 20 63 75 73 74  nting a .** cust
3fbc0 6f 6d 20 70 61 67 65 20 63 61 63 68 65 20 75 73  om page cache us
3fbd0 69 6e 67 20 74 68 69 73 20 41 50 49 2c 20 61 6e  ing this API, an
3fbe0 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e   application can
3fbf0 20 63 6f 6e 74 72 6f 6c 20 6d 6f 72 65 20 0a 2a   control more .*
3fc00 2a 20 70 72 65 63 69 73 65 6c 79 20 74 68 65 20  * precisely the 
3fc10 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79  amount of memory
3fc20 20 63 6f 6e 73 75 6d 65 64 20 62 79 20 53 51 4c   consumed by SQL
3fc30 69 74 65 2c 20 74 68 65 20 77 61 79 20 69 6e 20  ite, the way in 
3fc40 77 68 69 63 68 20 0a 2a 2a 20 74 68 61 74 20 6d  which .** that m
3fc50 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61 74  emory is allocat
3fc60 65 64 20 61 6e 64 20 72 65 6c 65 61 73 65 64 2c  ed and released,
3fc70 20 61 6e 64 20 74 68 65 20 70 6f 6c 69 63 69 65   and the policie
3fc80 73 20 75 73 65 64 20 74 6f 20 0a 2a 2a 20 64 65  s used to .** de
3fc90 74 65 72 6d 69 6e 65 20 65 78 61 63 74 6c 79 20  termine exactly 
3fca0 77 68 69 63 68 20 70 61 72 74 73 20 6f 66 20 61  which parts of a
3fcb0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
3fcc0 72 65 20 63 61 63 68 65 64 20 61 6e 64 20 66 6f  re cached and fo
3fcd0 72 20 0a 2a 2a 20 68 6f 77 20 6c 6f 6e 67 2e 0a  r .** how long..
3fce0 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e  **.** The conten
3fcf0 74 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  ts of the sqlite
3fd00 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73  3_pcache_methods
3fd10 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 63   structure are c
3fd20 6f 70 69 65 64 20 74 6f 20 61 6e 0a 2a 2a 20 69  opied to an.** i
3fd30 6e 74 65 72 6e 61 6c 20 62 75 66 66 65 72 20 62  nternal buffer b
3fd40 79 20 53 51 4c 69 74 65 20 77 69 74 68 69 6e 20  y SQLite within 
3fd50 74 68 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c  the call to [sql
3fd60 69 74 65 33 5f 63 6f 6e 66 69 67 5d 2e 20 20 48  ite3_config].  H
3fd70 65 6e 63 65 0a 2a 2a 20 74 68 65 20 61 70 70 6c  ence.** the appl
3fd80 69 63 61 74 69 6f 6e 20 6d 61 79 20 64 69 73 63  ication may disc
3fd90 61 72 64 20 74 68 65 20 70 61 72 61 6d 65 74 65  ard the paramete
3fda0 72 20 61 66 74 65 72 20 74 68 65 20 63 61 6c 6c  r after the call
3fdb0 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   to.** [sqlite3_
3fdc0 63 6f 6e 66 69 67 28 29 5d 20 72 65 74 75 72 6e  config()] return
3fdd0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e  s..**.** The xIn
3fde0 69 74 28 29 20 6d 65 74 68 6f 64 20 69 73 20 63  it() method is c
3fdf0 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20 65  alled once for e
3fe00 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c  ach call to [sql
3fe10 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
3fe20 29 5d 0a 2a 2a 20 28 75 73 75 61 6c 6c 79 20 6f  )].** (usually o
3fe30 6e 6c 79 20 6f 6e 63 65 20 64 75 72 69 6e 67 20  nly once during 
3fe40 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20  the lifetime of 
3fe50 74 68 65 20 70 72 6f 63 65 73 73 29 2e 20 49 74  the process). It
3fe60 20 69 73 20 70 61 73 73 65 64 0a 2a 2a 20 61 20   is passed.** a 
3fe70 63 6f 70 79 20 6f 66 20 74 68 65 20 73 71 6c 69  copy of the sqli
3fe80 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f  te3_pcache_metho
3fe90 64 73 2e 70 41 72 67 20 76 61 6c 75 65 2e 20 49  ds.pArg value. I
3fea0 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  t can be used to
3feb0 20 73 65 74 0a 2a 2a 20 75 70 20 67 6c 6f 62 61   set.** up globa
3fec0 6c 20 73 74 72 75 63 74 75 72 65 73 20 61 6e 64  l structures and
3fed0 20 6d 75 74 65 78 65 73 20 72 65 71 75 69 72 65   mutexes require
3fee0 64 20 62 79 20 74 68 65 20 63 75 73 74 6f 6d 20  d by the custom 
3fef0 70 61 67 65 20 63 61 63 68 65 20 0a 2a 2a 20 69  page cache .** i
3ff00 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 0a  mplementation. .
3ff10 2a 2a 0a 2a 2a 20 54 68 65 20 78 53 68 75 74 64  **.** The xShutd
3ff20 6f 77 6e 28 29 20 6d 65 74 68 6f 64 20 69 73 20  own() method is 
3ff30 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68  called from with
3ff40 69 6e 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74  in [sqlite3_shut
3ff50 64 6f 77 6e 28 29 5d 2c 20 0a 2a 2a 20 69 66 20  down()], .** if 
3ff60 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
3ff70 69 6e 76 6f 6b 65 73 20 74 68 69 73 20 41 50 49  invokes this API
3ff80 2e 20 49 74 20 63 61 6e 20 62 65 20 75 73 65 64  . It can be used
3ff90 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 0a 2a 2a   to clean up .**
3ffa0 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67   any outstanding
3ffb0 20 72 65 73 6f 75 72 63 65 73 20 62 65 66 6f 72   resources befor
3ffc0 65 20 70 72 6f 63 65 73 73 20 73 68 75 74 64 6f  e process shutdo
3ffd0 77 6e 2c 20 69 66 20 72 65 71 75 69 72 65 64 2e  wn, if required.
3ffe0 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 68 6f  .**.** SQLite ho
3fff0 6c 64 73 20 61 20 5b 53 51 4c 49 54 45 5f 4d 55  lds a [SQLITE_MU
40000 54 45 58 5f 52 45 43 55 52 53 49 56 45 5d 20 6d  TEX_RECURSIVE] m
40010 75 74 65 78 20 77 68 65 6e 20 69 74 20 69 6e 76  utex when it inv
40020 6f 6b 65 73 0a 2a 2a 20 74 68 65 20 78 49 6e 69  okes.** the xIni
40030 74 20 6d 65 74 68 6f 64 2c 20 73 6f 20 74 68 65  t method, so the
40040 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 6e 65   xInit method ne
40050 65 64 20 6e 6f 74 20 62 65 20 74 68 72 65 61 64  ed not be thread
40060 73 61 66 65 2e 20 20 54 68 65 0a 2a 2a 20 78 53  safe.  The.** xS
40070 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 20 69  hutdown method i
40080 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72  s only called fr
40090 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74  om [sqlite3_shut
400a0 64 6f 77 6e 28 29 5d 20 73 6f 20 69 74 20 64 6f  down()] so it do
400b0 65 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74  es.** not need t
400c0 6f 20 62 65 20 74 68 72 65 61 64 73 61 66 65 20  o be threadsafe 
400d0 65 69 74 68 65 72 2e 20 20 41 6c 6c 20 6f 74 68  either.  All oth
400e0 65 72 20 6d 65 74 68 6f 64 73 20 6d 75 73 74 20  er methods must 
400f0 62 65 20 74 68 72 65 61 64 73 61 66 65 0a 2a 2a  be threadsafe.**
40100 20 69 6e 20 6d 75 6c 74 69 74 68 72 65 61 64 65   in multithreade
40110 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 0a  d applications..
40120 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c  **.** SQLite wil
40130 6c 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 20 78  l never invoke x
40140 49 6e 69 74 28 29 20 6d 6f 72 65 20 74 68 61 6e  Init() more than
40150 20 6f 6e 63 65 20 77 69 74 68 6f 75 74 20 61 6e   once without an
40160 20 69 6e 74 65 72 76 65 6e 69 6e 67 0a 2a 2a 20   intervening.** 
40170 63 61 6c 6c 20 74 6f 20 78 53 68 75 74 64 6f 77  call to xShutdow
40180 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  n()..**.** The x
40190 43 72 65 61 74 65 28 29 20 6d 65 74 68 6f 64 20  Create() method 
401a0 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 73 74  is used to const
401b0 72 75 63 74 20 61 20 6e 65 77 20 63 61 63 68 65  ruct a new cache
401c0 20 69 6e 73 74 61 6e 63 65 2e 20 20 53 51 4c 69   instance.  SQLi
401d0 74 65 0a 2a 2a 20 77 69 6c 6c 20 74 79 70 69 63  te.** will typic
401e0 61 6c 6c 79 20 63 72 65 61 74 65 20 6f 6e 65 20  ally create one 
401f0 63 61 63 68 65 20 69 6e 73 74 61 6e 63 65 20 66  cache instance f
40200 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 64 61 74  or each open dat
40210 61 62 61 73 65 20 66 69 6c 65 2c 0a 2a 2a 20 74  abase file,.** t
40220 68 6f 75 67 68 20 74 68 69 73 20 69 73 20 6e 6f  hough this is no
40230 74 20 67 75 61 72 61 6e 74 65 65 64 2e 20 54 68  t guaranteed. Th
40240 65 0a 2a 2a 20 66 69 72 73 74 20 70 61 72 61 6d  e.** first param
40250 65 74 65 72 2c 20 73 7a 50 61 67 65 2c 20 69 73  eter, szPage, is
40260 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62 79 74   the size in byt
40270 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 73 20  es of the pages 
40280 74 68 61 74 20 6d 75 73 74 0a 2a 2a 20 62 65 20  that must.** be 
40290 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65  allocated by the
402a0 20 63 61 63 68 65 2e 20 20 73 7a 50 61 67 65 20   cache.  szPage 
402b0 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 20 70 6f  will not be a po
402c0 77 65 72 20 6f 66 20 74 77 6f 2e 20 20 73 7a 50  wer of two.  szP
402d0 61 67 65 0a 2a 2a 20 77 69 6c 6c 20 74 68 65 20  age.** will the 
402e0 70 61 67 65 20 73 69 7a 65 20 6f 66 20 74 68 65  page size of the
402f0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74   database file t
40300 68 61 74 20 69 73 20 74 6f 20 62 65 20 63 61 63  hat is to be cac
40310 68 65 64 20 70 6c 75 73 20 61 6e 0a 2a 2a 20 69  hed plus an.** i
40320 6e 63 72 65 6d 65 6e 74 20 28 68 65 72 65 20 63  ncrement (here c
40330 61 6c 6c 65 64 20 22 52 22 29 20 6f 66 20 61 62  alled "R") of ab
40340 6f 75 74 20 31 30 30 20 6f 72 20 32 30 30 2e 20  out 100 or 200. 
40350 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 65   SQLite will use
40360 20 74 68 65 0a 2a 2a 20 65 78 74 72 61 20 52 20   the.** extra R 
40370 62 79 74 65 73 20 6f 6e 20 65 61 63 68 20 70 61  bytes on each pa
40380 67 65 20 74 6f 20 73 74 6f 72 65 20 6d 65 74 61  ge to store meta
40390 64 61 74 61 20 61 62 6f 75 74 20 74 68 65 20 75  data about the u
403a0 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 64 61 74  nderlying.** dat
403b0 61 62 61 73 65 20 70 61 67 65 20 6f 6e 20 64 69  abase page on di
403c0 73 6b 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6f  sk.  The value o
403d0 66 20 52 20 64 65 70 65 6e 64 73 0a 2a 2a 20 6f  f R depends.** o
403e0 6e 20 74 68 65 20 53 51 4c 69 74 65 20 76 65 72  n the SQLite ver
403f0 73 69 6f 6e 2c 20 74 68 65 20 74 61 72 67 65 74  sion, the target
40400 20 70 6c 61 74 66 6f 72 6d 2c 20 61 6e 64 20 68   platform, and h
40410 6f 77 20 53 51 4c 69 74 65 20 77 61 73 20 63 6f  ow SQLite was co
40420 6d 70 69 6c 65 64 2e 0a 2a 2a 20 52 20 69 73 20  mpiled..** R is 
40430 63 6f 6e 73 74 61 6e 74 20 66 6f 72 20 61 20 70  constant for a p
40440 61 72 74 69 63 75 6c 61 72 20 62 75 69 6c 64 20  articular build 
40450 6f 66 20 53 51 4c 69 74 65 2e 20 20 54 68 65 20  of SQLite.  The 
40460 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
40470 74 6f 0a 2a 2a 20 78 43 72 65 61 74 65 28 29 2c  to.** xCreate(),
40480 20 62 50 75 72 67 65 61 62 6c 65 2c 20 69 73 20   bPurgeable, is 
40490 74 72 75 65 20 69 66 20 74 68 65 20 63 61 63 68  true if the cach
404a0 65 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 20  e being created 
404b0 77 69 6c 6c 0a 2a 2a 20 62 65 20 75 73 65 64 20  will.** be used 
404c0 74 6f 20 63 61 63 68 65 20 64 61 74 61 62 61 73  to cache databas
404d0 65 20 70 61 67 65 73 20 6f 66 20 61 20 66 69 6c  e pages of a fil
404e0 65 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b  e stored on disk
404f0 2c 20 6f 72 0a 2a 2a 20 66 61 6c 73 65 20 69 66  , or.** false if
40500 20 69 74 20 69 73 20 75 73 65 64 20 66 6f 72 20   it is used for 
40510 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  an in-memory dat
40520 61 62 61 73 65 2e 20 54 68 65 20 63 61 63 68 65  abase. The cache
40530 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a   implementation.
40540 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65  ** does not have
40550 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67 20   to do anything 
40560 73 70 65 63 69 61 6c 20 62 61 73 65 64 20 77 69  special based wi
40570 74 68 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  th the value of 
40580 62 50 75 72 67 65 61 62 6c 65 3b 0a 2a 2a 20 69  bPurgeable;.** i
40590 74 20 69 73 20 70 75 72 65 6c 79 20 61 64 76 69  t is purely advi
405a0 73 6f 72 79 2e 20 20 4f 6e 20 61 20 63 61 63 68  sory.  On a cach
405b0 65 20 77 68 65 72 65 20 62 50 75 72 67 65 61 62  e where bPurgeab
405c0 6c 65 20 69 73 20 66 61 6c 73 65 2c 20 53 51 4c  le is false, SQL
405d0 69 74 65 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65  ite will.** neve
405e0 72 20 69 6e 76 6f 6b 65 20 78 55 6e 70 69 6e 28  r invoke xUnpin(
405f0 29 20 65 78 63 65 70 74 20 74 6f 20 64 65 6c 69  ) except to deli
40600 62 65 72 61 74 65 6c 79 20 64 65 6c 65 74 65 20  berately delete 
40610 61 20 70 61 67 65 2e 0a 2a 2a 20 49 6e 20 6f 74  a page..** In ot
40620 68 65 72 20 77 6f 72 64 73 2c 20 61 20 63 61 63  her words, a cac
40630 68 65 20 63 72 65 61 74 65 64 20 77 69 74 68 20  he created with 
40640 62 50 75 72 67 65 61 62 6c 65 20 73 65 74 20 74  bPurgeable set t
40650 6f 20 66 61 6c 73 65 20 77 69 6c 6c 0a 2a 2a 20  o false will.** 
40660 6e 65 76 65 72 20 63 6f 6e 74 61 69 6e 20 61 6e  never contain an
40670 79 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73  y unpinned pages
40680 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 61 63  ..**.** The xCac
40690 68 65 73 69 7a 65 28 29 20 6d 65 74 68 6f 64 20  hesize() method 
406a0 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 61 74  may be called at
406b0 20 61 6e 79 20 74 69 6d 65 20 62 79 20 53 51 4c   any time by SQL
406c0 69 74 65 20 74 6f 20 73 65 74 20 74 68 65 0a 2a  ite to set the.*
406d0 2a 20 73 75 67 67 65 73 74 65 64 20 6d 61 78 69  * suggested maxi
406e0 6d 75 6d 20 63 61 63 68 65 2d 73 69 7a 65 20 28  mum cache-size (
406f0 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
40700 73 74 6f 72 65 64 20 62 79 29 20 74 68 65 20 63  stored by) the c
40710 61 63 68 65 0a 2a 2a 20 69 6e 73 74 61 6e 63 65  ache.** instance
40720 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66   passed as the f
40730 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 54  irst argument. T
40740 68 69 73 20 69 73 20 74 68 65 20 76 61 6c 75 65  his is the value
40750 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e   configured usin
40760 67 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20  g.** the SQLite 
40770 22 5b 50 52 41 47 4d 41 20 63 61 63 68 65 5f 73  "[PRAGMA cache_s
40780 69 7a 65 5d 22 20 63 6f 6d 6d 61 6e 64 2e 20 41  ize]" command. A
40790 73 20 77 69 74 68 20 74 68 65 20 62 50 75 72 67  s with the bPurg
407a0 65 61 62 6c 65 20 70 61 72 61 6d 65 74 65 72 2c  eable parameter,
407b0 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  .** the implemen
407c0 74 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 65  tation is not re
407d0 71 75 69 72 65 64 20 74 6f 20 64 6f 20 61 6e 79  quired to do any
407e0 74 68 69 6e 67 20 77 69 74 68 20 74 68 69 73 0a  thing with this.
407f0 2a 2a 20 76 61 6c 75 65 3b 20 69 74 20 69 73 20  ** value; it is 
40800 61 64 76 69 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a  advisory only..*
40810 2a 0a 2a 2a 20 54 68 65 20 78 50 61 67 65 63 6f  *.** The xPageco
40820 75 6e 74 28 29 20 6d 65 74 68 6f 64 20 73 68 6f  unt() method sho
40830 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 6e  uld return the n
40840 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 63  umber of pages c
40850 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 73 74 6f 72  urrently.** stor
40860 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e  ed in the cache.
40870 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 78 46 65 74  .** .** The xFet
40880 63 68 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75  ch() method is u
40890 73 65 64 20 74 6f 20 66 65 74 63 68 20 61 20 70  sed to fetch a p
408a0 61 67 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61  age and return a
408b0 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20   pointer to it. 
408c0 0a 2a 2a 20 41 20 27 70 61 67 65 27 2c 20 69 6e  .** A 'page', in
408d0 20 74 68 69 73 20 63 6f 6e 74 65 78 74 2c 20 69   this context, i
408e0 73 20 61 20 62 75 66 66 65 72 20 6f 66 20 73 7a  s a buffer of sz
408f0 50 61 67 65 20 62 79 74 65 73 20 61 6c 69 67 6e  Page bytes align
40900 65 64 20 61 74 20 61 6e 0a 2a 2a 20 38 2d 62 79  ed at an.** 8-by
40910 74 65 20 62 6f 75 6e 64 61 72 79 2e 20 54 68 65  te boundary. The
40920 20 70 61 67 65 20 74 6f 20 62 65 20 66 65 74 63   page to be fetc
40930 68 65 64 20 69 73 20 64 65 74 65 72 6d 69 6e 65  hed is determine
40940 64 20 62 79 20 74 68 65 20 6b 65 79 2e 20 54 68  d by the key. Th
40950 65 0a 2a 2a 20 6d 69 6d 69 6d 75 6d 20 6b 65 79  e.** mimimum key
40960 20 76 61 6c 75 65 20 69 73 20 31 2e 20 41 66 74   value is 1. Aft
40970 65 72 20 69 74 20 68 61 73 20 62 65 65 6e 20 72  er it has been r
40980 65 74 72 69 65 76 65 64 20 75 73 69 6e 67 20 78  etrieved using x
40990 46 65 74 63 68 2c 20 74 68 65 20 70 61 67 65 20  Fetch, the page 
409a0 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65  .** is considere
409b0 64 20 74 6f 20 62 65 20 22 70 69 6e 6e 65 64 22  d to be "pinned"
409c0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72  ..**.** If the r
409d0 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73  equested page is
409e0 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
409f0 70 61 67 65 20 63 61 63 68 65 2c 20 74 68 65 6e  page cache, then
40a00 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 0a   the page cache.
40a10 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
40a20 6e 20 6d 75 73 74 20 72 65 74 75 72 6e 20 61 20  n must return a 
40a30 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70  pointer to the p
40a40 61 67 65 20 62 75 66 66 65 72 20 77 69 74 68 20  age buffer with 
40a50 69 74 73 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69  its content.** i
40a60 6e 74 61 63 74 2e 20 20 49 66 20 74 68 65 20 72  ntact.  If the r
40a70 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73  equested page is
40a80 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20   not already in 
40a90 74 68 65 20 63 61 63 68 65 2c 20 74 68 65 6e 20  the cache, then 
40aa0 74 68 65 0a 2a 2a 20 62 65 68 61 76 69 6f 72 20  the.** behavior 
40ab0 6f 66 20 74 68 65 20 63 61 63 68 65 20 69 6d 70  of the cache imp
40ac0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 64  lementation is d
40ad0 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65  etermined by the
40ae0 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a   value of the.**
40af0 20 63 72 65 61 74 65 46 6c 61 67 20 70 61 72 61   createFlag para
40b00 6d 65 74 65 72 20 70 61 73 73 65 64 20 74 6f 20  meter passed to 
40b10 78 46 65 74 63 68 2c 20 61 63 63 6f 72 64 69 6e  xFetch, accordin
40b20 67 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  g to the followi
40b30 6e 67 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20  ng table:.**.** 
40b40 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 31 20  <table border=1 
40b50 77 69 64 74 68 3d 38 35 25 20 61 6c 69 67 6e 3d  width=85% align=
40b60 63 65 6e 74 65 72 3e 0a 2a 2a 20 3c 74 72 3e 3c  center>.** <tr><
40b70 74 68 3e 20 63 72 65 61 74 65 46 6c 61 67 20 3c  th> createFlag <
40b80 74 68 3e 20 42 65 68 61 76 69 6f 75 72 20 77 68  th> Behaviour wh
40b90 65 6e 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61  en page is not a
40ba0 6c 72 65 61 64 79 20 69 6e 20 63 61 63 68 65 0a  lready in cache.
40bb0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 30 20 3c 74  ** <tr><td> 0 <t
40bc0 64 3e 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61  d> Do not alloca
40bd0 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20 20  te a new page.  
40be0 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 20  Return NULL..** 
40bf0 3c 74 72 3e 3c 74 64 3e 20 31 20 3c 74 64 3e 20  <tr><td> 1 <td> 
40c00 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70  Allocate a new p
40c10 61 67 65 20 69 66 20 69 74 20 65 61 73 79 20 61  age if it easy a
40c20 6e 64 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f  nd convenient to
40c30 20 64 6f 20 73 6f 2e 0a 2a 2a 20 20 20 20 20 20   do so..**      
40c40 20 20 20 20 20 20 20 20 20 20 20 4f 74 68 65 72             Other
40c50 77 69 73 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c  wise return NULL
40c60 2e 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 32 20  ..** <tr><td> 2 
40c70 3c 74 64 3e 20 4d 61 6b 65 20 65 76 65 72 79 20  <td> Make every 
40c80 65 66 66 6f 72 74 20 74 6f 20 61 6c 6c 6f 63 61  effort to alloca
40c90 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20 20  te a new page.  
40ca0 4f 6e 6c 79 20 72 65 74 75 72 6e 0a 2a 2a 20 20  Only return.**  
40cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e                 N
40cc0 55 4c 4c 20 69 66 20 61 6c 6c 6f 63 61 74 69 6e  ULL if allocatin
40cd0 67 20 61 20 6e 65 77 20 70 61 67 65 20 69 73 20  g a new page is 
40ce0 65 66 66 65 63 74 69 76 65 6c 79 20 69 6d 70 6f  effectively impo
40cf0 73 73 69 62 6c 65 2e 0a 2a 2a 20 3c 2f 74 61 62  ssible..** </tab
40d00 6c 65 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  le>.**.** SQLite
40d10 20 77 69 6c 6c 20 6e 6f 72 6d 61 6c 6c 79 20 69   will normally i
40d20 6e 76 6f 6b 65 20 78 46 65 74 63 68 28 29 20 77  nvoke xFetch() w
40d30 69 74 68 20 61 20 63 72 65 61 74 65 46 6c 61 67  ith a createFlag
40d40 20 6f 66 20 30 20 6f 72 20 31 2e 20 20 49 66 0a   of 0 or 1.  If.
40d50 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20 78 46 65  ** a call to xFe
40d60 74 63 68 28 29 20 77 69 74 68 20 63 72 65 61 74  tch() with creat
40d70 65 46 6c 61 67 3d 3d 31 20 72 65 74 75 72 6e 73  eFlag==1 returns
40d80 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 53 51 4c 69   NULL, then SQLi
40d90 74 65 20 77 69 6c 6c 0a 2a 2a 20 61 74 74 65 6d  te will.** attem
40da0 70 74 20 74 6f 20 75 6e 70 69 6e 20 6f 6e 65 20  pt to unpin one 
40db0 6f 72 20 6d 6f 72 65 20 63 61 63 68 65 20 70 61  or more cache pa
40dc0 67 65 73 20 62 79 20 73 70 69 6c 6c 69 6e 67 20  ges by spilling 
40dd0 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a  the content of.*
40de0 2a 20 70 69 6e 6e 65 64 20 70 61 67 65 73 20 74  * pinned pages t
40df0 6f 20 64 69 73 6b 20 61 6e 64 20 73 79 6e 63 68  o disk and synch
40e00 69 6e 67 20 74 68 65 20 6f 70 65 72 61 74 69 6e  ing the operatin
40e10 67 20 73 79 73 74 65 6d 20 64 69 73 6b 20 63 61  g system disk ca
40e20 63 68 65 2e 20 41 66 74 65 72 0a 2a 2a 20 61 74  che. After.** at
40e30 74 65 6d 70 74 69 6e 67 20 74 6f 20 75 6e 70 69  tempting to unpi
40e40 6e 20 70 61 67 65 73 2c 20 74 68 65 20 78 46 65  n pages, the xFe
40e50 74 63 68 28 29 20 6d 65 74 68 6f 64 20 77 69 6c  tch() method wil
40e60 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 67 61  l be invoked aga
40e70 69 6e 20 77 69 74 68 0a 2a 2a 20 61 20 63 72 65  in with.** a cre
40e80 61 74 65 46 6c 61 67 20 6f 66 20 32 2e 0a 2a 2a  ateFlag of 2..**
40e90 0a 2a 2a 20 78 55 6e 70 69 6e 28 29 20 69 73 20  .** xUnpin() is 
40ea0 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74 65  called by SQLite
40eb0 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72 20   with a pointer 
40ec0 74 6f 20 61 20 63 75 72 72 65 6e 74 6c 79 20 70  to a currently p
40ed0 69 6e 6e 65 64 20 70 61 67 65 0a 2a 2a 20 61 73  inned page.** as
40ee0 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75   its second argu
40ef0 6d 65 6e 74 2e 20 49 66 20 74 68 65 20 74 68 69  ment. If the thi
40f00 72 64 20 70 61 72 61 6d 65 74 65 72 2c 20 64 69  rd parameter, di
40f10 73 63 61 72 64 2c 20 69 73 20 6e 6f 6e 2d 7a 65  scard, is non-ze
40f20 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20  ro,.** then the 
40f30 70 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 65  page should be e
40f40 76 69 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20  victed from the 
40f50 63 61 63 68 65 2e 20 49 6e 20 74 68 69 73 20 63  cache. In this c
40f60 61 73 65 20 53 51 4c 69 74 65 20 0a 2a 2a 20 61  ase SQLite .** a
40f70 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20  ssumes that the 
40f80 6e 65 78 74 20 74 69 6d 65 20 74 68 65 20 70 61  next time the pa
40f90 67 65 20 69 73 20 72 65 74 72 69 65 76 65 64 20  ge is retrieved 
40fa0 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 75  from the cache u
40fb0 73 69 6e 67 0a 2a 2a 20 74 68 65 20 78 46 65 74  sing.** the xFet
40fc0 63 68 28 29 20 6d 65 74 68 6f 64 2c 20 69 74 20  ch() method, it 
40fd0 77 69 6c 6c 20 62 65 20 7a 65 72 6f 65 64 2e 20  will be zeroed. 
40fe0 49 66 20 74 68 65 20 64 69 73 63 61 72 64 20 70  If the discard p
40ff0 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 7a  arameter is.** z
41000 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 61  ero, then the pa
41010 67 65 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  ge is considered
41020 20 74 6f 20 62 65 20 75 6e 70 69 6e 6e 65 64 2e   to be unpinned.
41030 20 54 68 65 20 63 61 63 68 65 20 69 6d 70 6c 65   The cache imple
41040 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6d 61 79  mentation.** may
41050 20 63 68 6f 6f 73 65 20 74 6f 20 65 76 69 63 74   choose to evict
41060 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20   unpinned pages 
41070 61 74 20 61 6e 79 20 74 69 6d 65 2e 0a 2a 2a 0a  at any time..**.
41080 2a 2a 20 54 68 65 20 63 61 63 68 65 20 69 73 20  ** The cache is 
41090 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20  not required to 
410a0 70 65 72 66 6f 72 6d 20 61 6e 79 20 72 65 66 65  perform any refe
410b0 72 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67 2e 20  rence counting. 
410c0 41 20 73 69 6e 67 6c 65 20 0a 2a 2a 20 63 61 6c  A single .** cal
410d0 6c 20 74 6f 20 78 55 6e 70 69 6e 28 29 20 75 6e  l to xUnpin() un
410e0 70 69 6e 73 20 74 68 65 20 70 61 67 65 20 72 65  pins the page re
410f0 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20  gardless of the 
41100 6e 75 6d 62 65 72 20 6f 66 20 70 72 69 6f 72 20  number of prior 
41110 63 61 6c 6c 73 20 0a 2a 2a 20 74 6f 20 78 46 65  calls .** to xFe
41120 74 63 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tch()..**.** The
41130 20 78 52 65 6b 65 79 28 29 20 6d 65 74 68 6f 64   xRekey() method
41140 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 61 6e   is used to chan
41150 67 65 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65  ge the key value
41160 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
41170 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 70 61 73   the.** page pas
41180 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  sed as the secon
41190 64 20 61 72 67 75 6d 65 6e 74 20 66 72 6f 6d 20  d argument from 
411a0 6f 6c 64 4b 65 79 20 74 6f 20 6e 65 77 4b 65 79  oldKey to newKey
411b0 2e 20 49 66 20 74 68 65 20 63 61 63 68 65 0a 2a  . If the cache.*
411c0 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6f 6e  * previously con
411d0 74 61 69 6e 73 20 61 6e 20 65 6e 74 72 79 20 61  tains an entry a
411e0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 6e  ssociated with n
411f0 65 77 4b 65 79 2c 20 69 74 20 73 68 6f 75 6c 64  ewKey, it should
41200 20 62 65 0a 2a 2a 20 64 69 73 63 61 72 64 65 64   be.** discarded
41210 2e 20 41 6e 79 20 70 72 69 6f 72 20 63 61 63 68  . Any prior cach
41220 65 20 65 6e 74 72 79 20 61 73 73 6f 63 69 61 74  e entry associat
41230 65 64 20 77 69 74 68 20 6e 65 77 4b 65 79 20 69  ed with newKey i
41240 73 20 67 75 61 72 61 6e 74 65 65 64 20 6e 6f 74  s guaranteed not
41250 0a 2a 2a 20 74 6f 20 62 65 20 70 69 6e 6e 65 64  .** to be pinned
41260 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c  ..**.** When SQL
41270 69 74 65 20 63 61 6c 6c 73 20 74 68 65 20 78 54  ite calls the xT
41280 72 75 6e 63 61 74 65 28 29 20 6d 65 74 68 6f 64  runcate() method
41290 2c 20 74 68 65 20 63 61 63 68 65 20 6d 75 73 74  , the cache must
412a0 20 64 69 73 63 61 72 64 20 61 6c 6c 0a 2a 2a 20   discard all.** 
412b0 65 78 69 73 74 69 6e 67 20 63 61 63 68 65 20 65  existing cache e
412c0 6e 74 72 69 65 73 20 77 69 74 68 20 70 61 67 65  ntries with page
412d0 20 6e 75 6d 62 65 72 73 20 28 6b 65 79 73 29 20   numbers (keys) 
412e0 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20  greater than or 
412f0 65 71 75 61 6c 0a 2a 2a 20 74 6f 20 74 68 65 20  equal.** to the 
41300 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 4c 69  value of the iLi
41310 6d 69 74 20 70 61 72 61 6d 65 74 65 72 20 70 61  mit parameter pa
41320 73 73 65 64 20 74 6f 20 78 54 72 75 6e 63 61 74  ssed to xTruncat
41330 65 28 29 2e 20 49 66 20 61 6e 79 0a 2a 2a 20 6f  e(). If any.** o
41340 66 20 74 68 65 73 65 20 70 61 67 65 73 20 61 72  f these pages ar
41350 65 20 70 69 6e 6e 65 64 2c 20 74 68 65 79 20 61  e pinned, they a
41360 72 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 75 6e  re implicitly un
41370 70 69 6e 6e 65 64 2c 20 6d 65 61 6e 69 6e 67 20  pinned, meaning 
41380 74 68 61 74 0a 2a 2a 20 74 68 65 79 20 63 61 6e  that.** they can
41390 20 62 65 20 73 61 66 65 6c 79 20 64 69 73 63 61   be safely disca
413a0 72 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rded..**.** The 
413b0 78 44 65 73 74 72 6f 79 28 29 20 6d 65 74 68 6f  xDestroy() metho
413c0 64 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 6c  d is used to del
413d0 65 74 65 20 61 20 63 61 63 68 65 20 61 6c 6c 6f  ete a cache allo
413e0 63 61 74 65 64 20 62 79 20 78 43 72 65 61 74 65  cated by xCreate
413f0 28 29 2e 0a 2a 2a 20 41 6c 6c 20 72 65 73 6f 75  ()..** All resou
41400 72 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  rces associated 
41410 77 69 74 68 20 74 68 65 20 73 70 65 63 69 66 69  with the specifi
41420 65 64 20 63 61 63 68 65 20 73 68 6f 75 6c 64 20  ed cache should 
41430 62 65 20 66 72 65 65 64 2e 20 41 66 74 65 72 0a  be freed. After.
41440 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 78  ** calling the x
41450 44 65 73 74 72 6f 79 28 29 20 6d 65 74 68 6f 64  Destroy() method
41460 2c 20 53 51 4c 69 74 65 20 63 6f 6e 73 69 64 65  , SQLite conside
41470 72 73 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  rs the [sqlite3_
41480 70 63 61 63 68 65 2a 5d 0a 2a 2a 20 68 61 6e 64  pcache*].** hand
41490 6c 65 20 69 6e 76 61 6c 69 64 2c 20 61 6e 64 20  le invalid, and 
414a0 77 69 6c 6c 20 6e 6f 74 20 75 73 65 20 69 74 20  will not use it 
414b0 77 69 74 68 20 61 6e 79 20 6f 74 68 65 72 20 73  with any other s
414c0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65  qlite3_pcache_me
414d0 74 68 6f 64 73 0a 2a 2a 20 66 75 6e 63 74 69 6f  thods.** functio
414e0 6e 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ns..*/.typedef s
414f0 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70 63  truct sqlite3_pc
41500 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 73 71 6c  ache_methods sql
41510 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68  ite3_pcache_meth
41520 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c 69  ods;.struct sqli
41530 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f  te3_pcache_metho
41540 64 73 20 7b 0a 20 20 76 6f 69 64 20 2a 70 41 72  ds {.  void *pAr
41550 67 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69 74  g;.  int (*xInit
41560 29 28 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64  )(void*);.  void
41570 20 28 2a 78 53 68 75 74 64 6f 77 6e 29 28 76 6f   (*xShutdown)(vo
41580 69 64 2a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  id*);.  sqlite3_
41590 70 63 61 63 68 65 20 2a 28 2a 78 43 72 65 61 74  pcache *(*xCreat
415a0 65 29 28 69 6e 74 20 73 7a 50 61 67 65 2c 20 69  e)(int szPage, i
415b0 6e 74 20 62 50 75 72 67 65 61 62 6c 65 29 3b 0a  nt bPurgeable);.
415c0 20 20 76 6f 69 64 20 28 2a 78 43 61 63 68 65 73    void (*xCaches
415d0 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 70 63 61  ize)(sqlite3_pca
415e0 63 68 65 2a 2c 20 69 6e 74 20 6e 43 61 63 68 65  che*, int nCache
415f0 73 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78  size);.  int (*x
41600 50 61 67 65 63 6f 75 6e 74 29 28 73 71 6c 69 74  Pagecount)(sqlit
41610 65 33 5f 70 63 61 63 68 65 2a 29 3b 0a 20 20 76  e3_pcache*);.  v
41620 6f 69 64 20 2a 28 2a 78 46 65 74 63 68 29 28 73  oid *(*xFetch)(s
41630 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20  qlite3_pcache*, 
41640 75 6e 73 69 67 6e 65 64 20 6b 65 79 2c 20 69 6e  unsigned key, in
41650 74 20 63 72 65 61 74 65 46 6c 61 67 29 3b 0a 20  t createFlag);. 
41660 20 76 6f 69 64 20 28 2a 78 55 6e 70 69 6e 29 28   void (*xUnpin)(
41670 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c  sqlite3_pcache*,
41680 20 76 6f 69 64 2a 2c 20 69 6e 74 20 64 69 73 63   void*, int disc
41690 61 72 64 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78  ard);.  void (*x
416a0 52 65 6b 65 79 29 28 73 71 6c 69 74 65 33 5f 70  Rekey)(sqlite3_p
416b0 63 61 63 68 65 2a 2c 20 76 6f 69 64 2a 2c 20 75  cache*, void*, u
416c0 6e 73 69 67 6e 65 64 20 6f 6c 64 4b 65 79 2c 20  nsigned oldKey, 
416d0 75 6e 73 69 67 6e 65 64 20 6e 65 77 4b 65 79 29  unsigned newKey)
416e0 3b 0a 20 20 76 6f 69 64 20 28 2a 78 54 72 75 6e  ;.  void (*xTrun
416f0 63 61 74 65 29 28 73 71 6c 69 74 65 33 5f 70 63  cate)(sqlite3_pc
41700 61 63 68 65 2a 2c 20 75 6e 73 69 67 6e 65 64 20  ache*, unsigned 
41710 69 4c 69 6d 69 74 29 3b 0a 20 20 76 6f 69 64 20  iLimit);.  void 
41720 28 2a 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69  (*xDestroy)(sqli
41730 74 65 33 5f 70 63 61 63 68 65 2a 29 3b 0a 7d 3b  te3_pcache*);.};
41740 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
41750 3a 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20  : Online Backup 
41760 4f 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45 52 49  Object.** EXPERI
41770 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65  MENTAL.**.** The
41780 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20   sqlite3_backup 
41790 6f 62 6a 65 63 74 20 72 65 63 6f 72 64 73 20 73  object records s
417a0 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  tate information
417b0 20 61 62 6f 75 74 20 61 6e 20 6f 6e 67 6f 69 6e   about an ongoin
417c0 67 0a 2a 2a 20 6f 6e 6c 69 6e 65 20 62 61 63 6b  g.** online back
417d0 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 54  up operation.  T
417e0 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  he sqlite3_backu
417f0 70 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61  p object is crea
41800 74 65 64 20 62 79 0a 2a 2a 20 61 20 63 61 6c 6c  ted by.** a call
41810 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 61 63   to [sqlite3_bac
41820 6b 75 70 5f 69 6e 69 74 28 29 5d 20 61 6e 64 20  kup_init()] and 
41830 69 73 20 64 65 73 74 72 6f 79 65 64 20 62 79 20  is destroyed by 
41840 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71  a call to.** [sq
41850 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e  lite3_backup_fin
41860 69 73 68 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65  ish()]..**.** Se
41870 65 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74  e Also: [Using t
41880 68 65 20 53 51 4c 69 74 65 20 4f 6e 6c 69 6e 65  he SQLite Online
41890 20 42 61 63 6b 75 70 20 41 50 49 5d 0a 2a 2f 0a   Backup API].*/.
418a0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
418b0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 73 71  qlite3_backup sq
418c0 6c 69 74 65 33 5f 62 61 63 6b 75 70 3b 0a 0a 2f  lite3_backup;../
418d0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f  *.** CAPI3REF: O
418e0 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20 41 50 49  nline Backup API
418f0 2e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  ..** EXPERIMENTA
41900 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49  L.**.** This API
41910 20 69 73 20 75 73 65 64 20 74 6f 20 6f 76 65 72   is used to over
41920 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e  write the conten
41930 74 73 20 6f 66 20 6f 6e 65 20 64 61 74 61 62 61  ts of one databa
41940 73 65 20 77 69 74 68 20 74 68 61 74 0a 2a 2a 20  se with that.** 
41950 6f 66 20 61 6e 6f 74 68 65 72 2e 20 49 74 20 69  of another. It i
41960 73 20 75 73 65 66 75 6c 20 65 69 74 68 65 72 20  s useful either 
41970 66 6f 72 20 63 72 65 61 74 69 6e 67 20 62 61 63  for creating bac
41980 6b 75 70 73 20 6f 66 20 64 61 74 61 62 61 73 65  kups of database
41990 73 20 6f 72 0a 2a 2a 20 66 6f 72 20 63 6f 70 79  s or.** for copy
419a0 69 6e 67 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61  ing in-memory da
419b0 74 61 62 61 73 65 73 20 74 6f 20 6f 72 20 66 72  tabases to or fr
419c0 6f 6d 20 70 65 72 73 69 73 74 65 6e 74 20 66 69  om persistent fi
419d0 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20  les. .**.** See 
419e0 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68 65  Also: [Using the
419f0 20 53 51 4c 69 74 65 20 4f 6e 6c 69 6e 65 20 42   SQLite Online B
41a00 61 63 6b 75 70 20 41 50 49 5d 0a 2a 2a 0a 2a 2a  ackup API].**.**
41a10 20 45 78 63 6c 75 73 69 76 65 20 61 63 63 65 73   Exclusive acces
41a20 73 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f  s is required to
41a30 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
41a40 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 74 68   database for th
41a50 65 20 0a 2a 2a 20 64 75 72 61 74 69 6f 6e 20 6f  e .** duration o
41a60 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 2e  f the operation.
41a70 20 48 6f 77 65 76 65 72 20 74 68 65 20 73 6f 75   However the sou
41a80 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20  rce database is 
41a90 6f 6e 6c 79 0a 2a 2a 20 72 65 61 64 2d 6c 6f 63  only.** read-loc
41aa0 6b 65 64 20 77 68 69 6c 65 20 69 74 20 69 73 20  ked while it is 
41ab0 61 63 74 75 61 6c 6c 79 20 62 65 69 6e 67 20 72  actually being r
41ac0 65 61 64 2c 20 69 74 20 69 73 20 6e 6f 74 20 6c  ead, it is not l
41ad0 6f 63 6b 65 64 0a 2a 2a 20 63 6f 6e 74 69 6e 75  ocked.** continu
41ae0 6f 75 73 6c 79 20 66 6f 72 20 74 68 65 20 65 6e  ously for the en
41af0 74 69 72 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20  tire operation. 
41b00 54 68 75 73 2c 20 74 68 65 20 62 61 63 6b 75 70  Thus, the backup
41b10 20 6d 61 79 20 62 65 0a 2a 2a 20 70 65 72 66 6f   may be.** perfo
41b20 72 6d 65 64 20 6f 6e 20 61 20 6c 69 76 65 20 64  rmed on a live d
41b30 61 74 61 62 61 73 65 20 77 69 74 68 6f 75 74 20  atabase without 
41b40 70 72 65 76 65 6e 74 69 6e 67 20 6f 74 68 65 72  preventing other
41b50 20 75 73 65 72 73 20 66 72 6f 6d 0a 2a 2a 20 77   users from.** w
41b60 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61  riting to the da
41b70 74 61 62 61 73 65 20 66 6f 72 20 61 6e 20 65 78  tabase for an ex
41b80 74 65 6e 64 65 64 20 70 65 72 69 6f 64 20 6f 66  tended period of
41b90 20 74 69 6d 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 6f   time..** .** To
41ba0 20 70 65 72 66 6f 72 6d 20 61 20 62 61 63 6b 75   perform a backu
41bb0 70 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a 2a 2a  p operation: .**
41bc0 20 20 20 3c 6f 6c 3e 0a 2a 2a 20 20 20 20 20 3c     <ol>.**     <
41bd0 6c 69 3e 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61  li><b>sqlite3_ba
41be0 63 6b 75 70 5f 69 6e 69 74 28 29 3c 2f 62 3e 20  ckup_init()</b> 
41bf0 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 74  is called once t
41c00 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  o initialize the
41c10 0a 2a 2a 20 20 20 20 20 20 20 20 20 62 61 63 6b  .**         back
41c20 75 70 2c 20 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e  up, .**     <li>
41c30 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  <b>sqlite3_backu
41c40 70 5f 73 74 65 70 28 29 3c 2f 62 3e 20 69 73 20  p_step()</b> is 
41c50 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20 6d 6f  called one or mo
41c60 72 65 20 74 69 6d 65 73 20 74 6f 20 74 72 61 6e  re times to tran
41c70 73 66 65 72 20 0a 2a 2a 20 20 20 20 20 20 20 20  sfer .**        
41c80 20 74 68 65 20 64 61 74 61 20 62 65 74 77 65 65   the data betwee
41c90 6e 20 74 68 65 20 74 77 6f 20 64 61 74 61 62 61  n the two databa
41ca0 73 65 73 2c 20 61 6e 64 20 66 69 6e 61 6c 6c 79  ses, and finally
41cb0 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c 62 3e 73  .**     <li><b>s
41cc0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69  qlite3_backup_fi
41cd0 6e 69 73 68 28 29 3c 2f 62 3e 20 69 73 20 63 61  nish()</b> is ca
41ce0 6c 6c 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20  lled to release 
41cf0 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 0a 2a  all resources .*
41d00 2a 20 20 20 20 20 20 20 20 20 61 73 73 6f 63 69  *         associ
41d10 61 74 65 64 20 77 69 74 68 20 74 68 65 20 62 61  ated with the ba
41d20 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20  ckup operation. 
41d30 0a 2a 2a 20 20 20 3c 2f 6f 6c 3e 0a 2a 2a 20 54  .**   </ol>.** T
41d40 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20 65  here should be e
41d50 78 61 63 74 6c 79 20 6f 6e 65 20 63 61 6c 6c 20  xactly one call 
41d60 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  to sqlite3_backu
41d70 70 5f 66 69 6e 69 73 68 28 29 20 66 6f 72 20 65  p_finish() for e
41d80 61 63 68 0a 2a 2a 20 73 75 63 63 65 73 73 66 75  ach.** successfu
41d90 6c 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  l call to sqlite
41da0 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2e  3_backup_init().
41db0 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 65  .**.** <b>sqlite
41dc0 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 3c  3_backup_init()<
41dd0 2f 62 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  /b>.**.** The fi
41de0 72 73 74 20 74 77 6f 20 61 72 67 75 6d 65 6e 74  rst two argument
41df0 73 20 70 61 73 73 65 64 20 74 6f 20 5b 73 71 6c  s passed to [sql
41e00 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74  ite3_backup_init
41e10 28 29 5d 20 61 72 65 20 74 68 65 20 64 61 74 61  ()] are the data
41e20 62 61 73 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 61  base.** handle a
41e30 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
41e40 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64  he destination d
41e50 61 74 61 62 61 73 65 20 61 6e 64 20 74 68 65 20  atabase and the 
41e60 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 0a 2a  database name .*
41e70 2a 20 75 73 65 64 20 74 6f 20 61 74 74 61 63 68  * used to attach
41e80 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
41e90 20 64 61 74 61 62 61 73 65 20 74 6f 20 74 68 65   database to the
41ea0 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 64 61 74   handle. The dat
41eb0 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20 69 73  abase name.** is
41ec0 20 22 6d 61 69 6e 22 20 66 6f 72 20 74 68 65 20   "main" for the 
41ed0 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20 22  main database, "
41ee0 74 65 6d 70 22 20 66 6f 72 20 74 68 65 20 74 65  temp" for the te
41ef0 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65  mporary database
41f00 2c 20 6f 72 0a 2a 2a 20 74 68 65 20 6e 61 6d 65  , or.** the name
41f10 20 73 70 65 63 69 66 69 65 64 20 61 73 20 70 61   specified as pa
41f20 72 74 20 6f 66 20 74 68 65 20 5b 41 54 54 41 43  rt of the [ATTAC
41f30 48 5d 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20  H] statement if 
41f40 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
41f50 69 73 0a 2a 2a 20 61 6e 20 61 74 74 61 63 68 65  is.** an attache
41f60 64 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20  d database. The 
41f70 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68  third and fourth
41f80 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73 65   arguments passe
41f90 64 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74 65 33  d to .** sqlite3
41fa0 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 20 69  _backup_init() i
41fb0 64 65 6e 74 69 66 79 20 74 68 65 20 5b 64 61 74  dentify the [dat
41fc0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
41fd0 5d 0a 2a 2a 20 61 6e 64 20 64 61 74 61 62 61 73  ].** and databas
41fe0 65 20 6e 61 6d 65 20 75 73 65 64 0a 2a 2a 20 74  e name used.** t
41ff0 6f 20 61 63 63 65 73 73 20 74 68 65 20 73 6f 75  o access the sou
42000 72 63 65 20 64 61 74 61 62 61 73 65 2e 20 54 68  rce database. Th
42010 65 20 76 61 6c 75 65 73 20 70 61 73 73 65 64 20  e values passed 
42020 66 6f 72 20 74 68 65 20 73 6f 75 72 63 65 20 61  for the source a
42030 6e 64 20 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69  nd .** destinati
42040 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  on [database con
42050 6e 65 63 74 69 6f 6e 5d 20 70 61 72 61 6d 65 74  nection] paramet
42060 65 72 73 20 6d 75 73 74 20 6e 6f 74 20 62 65 20  ers must not be 
42070 74 68 65 20 73 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20  the same..**.** 
42080 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
42090 72 73 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65  rs within sqlite
420a0 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2c  3_backup_init(),
420b0 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 20 72 65   then NULL is re
420c0 74 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 61 6e  turned.** and an
420d0 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20   error code and 
420e0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72  error message wr
420f0 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 5b  itten into the [
42100 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
42110 69 6f 6e 5d 20 0a 2a 2a 20 70 61 73 73 65 64 20  ion] .** passed 
42120 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67  as the first arg
42130 75 6d 65 6e 74 2e 20 54 68 65 79 20 6d 61 79 20  ument. They may 
42140 62 65 20 72 65 74 72 69 65 76 65 64 20 75 73 69  be retrieved usi
42150 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74  ng the.** [sqlit
42160 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 2c 20 5b  e3_errcode()], [
42170 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29  sqlite3_errmsg()
42180 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  ], and [sqlite3_
42190 65 72 72 6d 73 67 31 36 28 29 5d 20 66 75 6e 63  errmsg16()] func
421a0 74 69 6f 6e 73 2e 0a 2a 2a 20 4f 74 68 65 72 77  tions..** Otherw
421b0 69 73 65 2c 20 69 66 20 73 75 63 63 65 73 73 66  ise, if successf
421c0 75 6c 2c 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ul, a pointer to
421d0 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 62 61 63   an [sqlite3_bac
421e0 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 73 0a 2a  kup] object is.*
421f0 2a 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73  * returned. This
42200 20 70 6f 69 6e 74 65 72 20 6d 61 79 20 62 65 20   pointer may be 
42210 75 73 65 64 20 77 69 74 68 20 74 68 65 20 73 71  used with the sq
42220 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
42230 70 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74  p() and.** sqlit
42240 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68  e3_backup_finish
42250 28 29 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20  () functions to 
42260 70 65 72 66 6f 72 6d 20 74 68 65 20 73 70 65 63  perform the spec
42270 69 66 69 65 64 20 62 61 63 6b 75 70 20 0a 2a 2a  ified backup .**
42280 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a   operation..**.*
42290 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63  * <b>sqlite3_bac
422a0 6b 75 70 5f 73 74 65 70 28 29 3c 2f 62 3e 0a 2a  kup_step()</b>.*
422b0 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 5b 73  *.** Function [s
422c0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
422d0 65 70 28 29 5d 20 69 73 20 75 73 65 64 20 74 6f  ep()] is used to
422e0 20 63 6f 70 79 20 75 70 20 74 6f 20 6e 50 61 67   copy up to nPag
422f0 65 20 70 61 67 65 73 20 62 65 74 77 65 65 6e 20  e pages between 
42300 0a 2a 2a 20 74 68 65 20 73 6f 75 72 63 65 20 61  .** the source a
42310 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64  nd destination d
42320 61 74 61 62 61 73 65 73 2c 20 77 68 65 72 65 20  atabases, where 
42330 6e 50 61 67 65 20 69 73 20 74 68 65 20 76 61 6c  nPage is the val
42340 75 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 73 65  ue of the .** se
42350 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 70  cond parameter p
42360 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33  assed to sqlite3
42370 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20  _backup_step(). 
42380 49 66 20 6e 50 61 67 65 20 69 73 20 61 20 6e 65  If nPage is a ne
42390 67 61 74 69 76 65 0a 2a 2a 20 76 61 6c 75 65 2c  gative.** value,
423a0 20 61 6c 6c 20 72 65 6d 61 69 6e 69 6e 67 20 73   all remaining s
423b0 6f 75 72 63 65 20 70 61 67 65 73 20 61 72 65 20  ource pages are 
423c0 63 6f 70 69 65 64 2e 20 49 66 20 74 68 65 20 72  copied. If the r
423d0 65 71 75 69 72 65 64 20 70 61 67 65 73 20 61 72  equired pages ar
423e0 65 20 0a 2a 2a 20 73 75 63 63 65 73 66 75 6c 6c  e .** succesfull
423f0 79 20 63 6f 70 69 65 64 2c 20 62 75 74 20 74 68  y copied, but th
42400 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6d 6f  ere are still mo
42410 72 65 20 70 61 67 65 73 20 74 6f 20 63 6f 70 79  re pages to copy
42420 20 62 65 66 6f 72 65 20 74 68 65 20 0a 2a 2a 20   before the .** 
42430 62 61 63 6b 75 70 20 69 73 20 63 6f 6d 70 6c 65  backup is comple
42440 74 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20 5b  te, it returns [
42450 53 51 4c 49 54 45 5f 4f 4b 5d 2e 20 49 66 20 6e  SQLITE_OK]. If n
42460 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20  o error occured 
42470 61 6e 64 20 74 68 65 72 65 20 0a 2a 2a 20 61 72  and there .** ar
42480 65 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65 73 20  e no more pages 
42490 74 6f 20 63 6f 70 79 2c 20 74 68 65 6e 20 5b 53  to copy, then [S
424a0 51 4c 49 54 45 5f 44 4f 4e 45 5d 20 69 73 20 72  QLITE_DONE] is r
424b0 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65  eturned. If an e
424c0 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 2c  rror .** occurs,
424d0 20 74 68 65 6e 20 61 6e 20 53 51 4c 69 74 65 20   then an SQLite 
424e0 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
424f0 74 75 72 6e 65 64 2e 20 41 73 20 77 65 6c 6c 20  turned. As well 
42500 61 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 61  as [SQLITE_OK] a
42510 6e 64 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f  nd.** [SQLITE_DO
42520 4e 45 5d 2c 20 61 20 63 61 6c 6c 20 74 6f 20 73  NE], a call to s
42530 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
42540 65 70 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20  ep() may return 
42550 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59  [SQLITE_READONLY
42560 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f  ],.** [SQLITE_NO
42570 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 55  MEM], [SQLITE_BU
42580 53 59 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c 4f 43  SY], [SQLITE_LOC
42590 4b 45 44 5d 2c 20 6f 72 20 61 6e 0a 2a 2a 20 5b  KED], or an.** [
425a0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43  SQLITE_IOERR_ACC
425b0 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49 4f 45  ESS | SQLITE_IOE
425c0 52 52 5f 58 58 58 5d 20 65 78 74 65 6e 64 65 64  RR_XXX] extended
425d0 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a   error code..**.
425e0 2a 2a 20 41 73 20 77 65 6c 6c 20 61 73 20 74 68  ** As well as th
425f0 65 20 63 61 73 65 20 77 68 65 72 65 20 74 68 65  e case where the
42600 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74   destination dat
42610 61 62 61 73 65 20 66 69 6c 65 20 77 61 73 20 6f  abase file was o
42620 70 65 6e 65 64 20 66 6f 72 0a 2a 2a 20 72 65 61  pened for.** rea
42630 64 2d 6f 6e 6c 79 20 61 63 63 65 73 73 2c 20 73  d-only access, s
42640 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
42650 65 70 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20  ep() may return 
42660 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59  [SQLITE_READONLY
42670 5d 20 69 66 0a 2a 2a 20 74 68 65 20 64 65 73 74  ] if.** the dest
42680 69 6e 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e  ination is an in
42690 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
426a0 20 77 69 74 68 20 61 20 64 69 66 66 65 72 65 6e   with a differen
426b0 74 20 70 61 67 65 20 73 69 7a 65 0a 2a 2a 20 66  t page size.** f
426c0 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20 64  rom the source d
426d0 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49  atabase..**.** I
426e0 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  f sqlite3_backup
426f0 5f 73 74 65 70 28 29 20 63 61 6e 6e 6f 74 20 6f  _step() cannot o
42700 62 74 61 69 6e 20 61 20 72 65 71 75 69 72 65 64  btain a required
42710 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 6c 6f 63   file-system loc
42720 6b 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 5b  k, then.** the [
42730 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e  sqlite3_busy_han
42740 64 6c 65 72 20 7c 20 62 75 73 79 2d 68 61 6e 64  dler | busy-hand
42750 6c 65 72 20 66 75 6e 63 74 69 6f 6e 5d 0a 2a 2a  ler function].**
42760 20 69 73 20 69 6e 76 6f 6b 65 64 20 28 69 66 20   is invoked (if 
42770 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 65 64  one is specified
42780 29 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20 62 75  ). If the .** bu
42790 73 79 2d 68 61 6e 64 6c 65 72 20 72 65 74 75 72  sy-handler retur
427a0 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 20 62 65 66 6f  ns non-zero befo
427b0 72 65 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61  re the lock is a
427c0 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e 20 0a  vailable, then .
427d0 2a 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d  ** [SQLITE_BUSY]
427e0 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20   is returned to 
427f0 74 68 65 20 63 61 6c 6c 65 72 2e 20 49 6e 20 74  the caller. In t
42800 68 69 73 20 63 61 73 65 20 74 68 65 20 63 61 6c  his case the cal
42810 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  l to.** sqlite3_
42820 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61  backup_step() ca
42830 6e 20 62 65 20 72 65 74 72 69 65 64 20 6c 61 74  n be retried lat
42840 65 72 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63  er. If the sourc
42850 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63  e.** [database c
42860 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 69 73  onnection].** is
42870 20 62 65 69 6e 67 20 75 73 65 64 20 74 6f 20 77   being used to w
42880 72 69 74 65 20 74 6f 20 74 68 65 20 73 6f 75 72  rite to the sour
42890 63 65 20 64 61 74 61 62 61 73 65 20 77 68 65 6e  ce database when
428a0 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
428b0 73 74 65 70 28 29 0a 2a 2a 20 69 73 20 63 61 6c  step().** is cal
428c0 6c 65 64 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54  led, then [SQLIT
428d0 45 5f 4c 4f 43 4b 45 44 5d 20 69 73 20 72 65 74  E_LOCKED] is ret
428e0 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 6c  urned immediatel
428f0 79 2e 20 41 67 61 69 6e 2c 20 69 6e 20 74 68 69  y. Again, in thi
42900 73 0a 2a 2a 20 63 61 73 65 20 74 68 65 20 63 61  s.** case the ca
42910 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61  ll to sqlite3_ba
42920 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61 6e 20  ckup_step() can 
42930 62 65 20 72 65 74 72 69 65 64 20 6c 61 74 65 72  be retried later
42940 20 6f 6e 2e 20 49 66 0a 2a 2a 20 5b 53 51 4c 49   on. If.** [SQLI
42950 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20  TE_IOERR_ACCESS 
42960 7c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58  | SQLITE_IOERR_X
42970 58 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d  XX], [SQLITE_NOM
42980 45 4d 5d 2c 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49  EM], or.** [SQLI
42990 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 20 69 73 20  TE_READONLY] is 
429a0 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 0a  returned, then .
429b0 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70  ** there is no p
429c0 6f 69 6e 74 20 69 6e 20 72 65 74 72 79 69 6e 67  oint in retrying
429d0 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c   the call to sql
429e0 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
429f0 28 29 2e 20 54 68 65 73 65 20 0a 2a 2a 20 65 72  (). These .** er
42a00 72 6f 72 73 20 61 72 65 20 63 6f 6e 73 69 64 65  rors are conside
42a10 72 65 64 20 66 61 74 61 6c 2e 20 41 74 20 74 68  red fatal. At th
42a20 69 73 20 70 6f 69 6e 74 20 74 68 65 20 61 70 70  is point the app
42a30 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 61 63  lication must ac
42a40 63 65 70 74 20 0a 2a 2a 20 74 68 61 74 20 74 68  cept .** that th
42a50 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69  e backup operati
42a60 6f 6e 20 68 61 73 20 66 61 69 6c 65 64 20 61 6e  on has failed an
42a70 64 20 70 61 73 73 20 74 68 65 20 62 61 63 6b 75  d pass the backu
42a80 70 20 6f 70 65 72 61 74 69 6f 6e 20 68 61 6e 64  p operation hand
42a90 6c 65 20 0a 2a 2a 20 74 6f 20 74 68 65 20 73 71  le .** to the sq
42aa0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e  lite3_backup_fin
42ab0 69 73 68 28 29 20 74 6f 20 72 65 6c 65 61 73 65  ish() to release
42ac0 20 61 73 73 6f 63 69 61 74 65 64 20 72 65 73 6f   associated reso
42ad0 75 72 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 6c  urces..**.** Fol
42ae0 6c 6f 77 69 6e 67 20 74 68 65 20 66 69 72 73 74  lowing the first
42af0 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
42b00 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2c 20  _backup_step(), 
42b10 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
42b20 6b 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e 65 64  k is.** obtained
42b30 20 6f 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74   on the destinat
42b40 69 6f 6e 20 66 69 6c 65 2e 20 49 74 20 69 73 20  ion file. It is 
42b50 6e 6f 74 20 72 65 6c 65 61 73 65 64 20 75 6e 74  not released unt
42b60 69 6c 20 65 69 74 68 65 72 20 0a 2a 2a 20 73 71  il either .** sq
42b70 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e  lite3_backup_fin
42b80 69 73 68 28 29 20 69 73 20 63 61 6c 6c 65 64 20  ish() is called 
42b90 6f 72 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70  or the backup op
42ba0 65 72 61 74 69 6f 6e 20 69 73 20 63 6f 6d 70 6c  eration is compl
42bb0 65 74 65 20 0a 2a 2a 20 61 6e 64 20 73 71 6c 69  ete .** and sqli
42bc0 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28  te3_backup_step(
42bd0 29 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54  ) returns [SQLIT
42be0 45 5f 44 4f 4e 45 5d 2e 20 41 64 64 69 74 69 6f  E_DONE]. Additio
42bf0 6e 61 6c 6c 79 2c 20 65 61 63 68 20 74 69 6d 65  nally, each time
42c00 20 0a 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20 73   .** a call to s
42c10 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
42c20 65 70 28 29 20 69 73 20 6d 61 64 65 20 61 20 5b  ep() is made a [
42c30 73 68 61 72 65 64 20 6c 6f 63 6b 5d 20 69 73 20  shared lock] is 
42c40 6f 62 74 61 69 6e 65 64 20 6f 6e 0a 2a 2a 20 74  obtained on.** t
42c50 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
42c60 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20 6c 6f  se file. This lo
42c70 63 6b 20 69 73 20 72 65 6c 65 61 73 65 64 20 62  ck is released b
42c80 65 66 6f 72 65 20 74 68 65 0a 2a 2a 20 73 71 6c  efore the.** sql
42c90 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
42ca0 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e  () call returns.
42cb0 20 42 65 63 61 75 73 65 20 74 68 65 20 73 6f 75   Because the sou
42cc0 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20  rce database is 
42cd0 6e 6f 74 0a 2a 2a 20 6c 6f 63 6b 65 64 20 62 65  not.** locked be
42ce0 74 77 65 65 6e 20 63 61 6c 6c 73 20 74 6f 20 73  tween calls to s
42cf0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
42d00 65 70 28 29 2c 20 69 74 20 6d 61 79 20 62 65 20  ep(), it may be 
42d10 6d 6f 64 69 66 69 65 64 20 6d 69 64 2d 77 61 79  modified mid-way
42d20 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20  .** through the 
42d30 62 61 63 6b 75 70 20 70 72 6f 63 65 64 75 72 65  backup procedure
42d40 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 20  . If the source 
42d50 64 61 74 61 62 61 73 65 20 69 73 20 6d 6f 64 69  database is modi
42d60 66 69 65 64 20 62 79 20 61 6e 0a 2a 2a 20 65 78  fied by an.** ex
42d70 74 65 72 6e 61 6c 20 70 72 6f 63 65 73 73 20 6f  ternal process o
42d80 72 20 76 69 61 20 61 20 64 61 74 61 62 61 73 65  r via a database
42d90 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 74 68 65   connection othe
42da0 72 20 74 68 61 6e 20 74 68 65 20 6f 6e 65 20 62  r than the one b
42db0 65 69 6e 67 0a 2a 2a 20 75 73 65 64 20 62 79 20  eing.** used by 
42dc0 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61  the backup opera
42dd0 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 62  tion, then the b
42de0 61 63 6b 75 70 20 77 69 6c 6c 20 62 65 20 74 72  ackup will be tr
42df0 61 6e 73 70 61 72 65 6e 74 6c 79 0a 2a 2a 20 72  ansparently.** r
42e00 65 73 74 61 72 74 65 64 20 62 79 20 74 68 65 20  estarted by the 
42e10 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c  next call to sql
42e20 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
42e30 28 29 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63  (). If the sourc
42e40 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 69  e .** database i
42e50 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 74 68  s modified by th
42e60 65 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65  e using the same
42e70 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
42e80 74 69 6f 6e 20 61 73 20 69 73 20 75 73 65 64 0a  tion as is used.
42e90 2a 2a 20 62 79 20 74 68 65 20 62 61 63 6b 75 70  ** by the backup
42ea0 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e   operation, then
42eb0 20 74 68 65 20 62 61 63 6b 75 70 20 64 61 74 61   the backup data
42ec0 62 61 73 65 20 69 73 20 74 72 61 6e 73 70 61 72  base is transpar
42ed0 65 6e 74 6c 79 20 0a 2a 2a 20 75 70 64 61 74 65  ently .** update
42ee0 64 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69  d at the same ti
42ef0 6d 65 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c  me..**.** <b>sql
42f00 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69  ite3_backup_fini
42f10 73 68 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 4f  sh()</b>.**.** O
42f20 6e 63 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  nce sqlite3_back
42f30 75 70 5f 73 74 65 70 28 29 20 68 61 73 20 72 65  up_step() has re
42f40 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 44  turned [SQLITE_D
42f50 4f 4e 45 5d 2c 20 6f 72 20 77 68 65 6e 20 74 68  ONE], or when th
42f60 65 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f  e .** applicatio
42f70 6e 20 77 69 73 68 65 73 20 74 6f 20 61 62 61 6e  n wishes to aban
42f80 64 6f 6e 20 74 68 65 20 62 61 63 6b 75 70 20 6f  don the backup o
42f90 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 20 5b 73  peration, the [s
42fa0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 0a 2a  qlite3_backup].*
42fb0 2a 20 6f 62 6a 65 63 74 20 73 68 6f 75 6c 64 20  * object should 
42fc0 62 65 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c  be passed to sql
42fd0 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69  ite3_backup_fini
42fe0 73 68 28 29 2e 20 54 68 69 73 20 72 65 6c 65 61  sh(). This relea
42ff0 73 65 73 20 61 6c 6c 0a 2a 2a 20 72 65 73 6f 75  ses all.** resou
43000 72 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  rces associated 
43010 77 69 74 68 20 74 68 65 20 62 61 63 6b 75 70 20  with the backup 
43020 6f 70 65 72 61 74 69 6f 6e 2e 20 49 66 20 73 71  operation. If sq
43030 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
43040 70 28 29 0a 2a 2a 20 68 61 73 20 6e 6f 74 20 79  p().** has not y
43050 65 74 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c  et returned [SQL
43060 49 54 45 5f 44 4f 4e 45 5d 2c 20 74 68 65 6e 20  ITE_DONE], then 
43070 61 6e 79 20 61 63 74 69 76 65 20 77 72 69 74 65  any active write
43080 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20  -transaction on 
43090 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69  the.** destinati
430a0 6f 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 72  on database is r
430b0 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 65 20  olled back. The 
430c0 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d  [sqlite3_backup]
430d0 20 6f 62 6a 65 63 74 20 69 73 20 69 6e 76 61 6c   object is inval
430e0 69 64 0a 2a 2a 20 61 6e 64 20 6d 61 79 20 6e 6f  id.** and may no
430f0 74 20 62 65 20 75 73 65 64 20 66 6f 6c 6c 6f 77  t be used follow
43100 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  ing a call to sq
43110 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e  lite3_backup_fin
43120 69 73 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ish()..**.** The
43130 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
43140 62 79 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  by sqlite3_backu
43150 70 5f 66 69 6e 69 73 68 20 69 73 20 5b 53 51 4c  p_finish is [SQL
43160 49 54 45 5f 4f 4b 5d 20 69 66 20 6e 6f 20 65 72  ITE_OK] if no er
43170 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 72 65 64 2c  ror.** occurred,
43180 20 72 65 67 61 72 64 6c 65 73 73 20 6f 72 20 77   regardless or w
43190 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 73 71  hether or not sq
431a0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
431b0 70 28 29 20 77 61 73 20 63 61 6c 6c 65 64 0a 2a  p() was called.*
431c0 2a 20 61 20 73 75 66 66 69 63 69 65 6e 74 20 6e  * a sufficient n
431d0 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74  umber of times t
431e0 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 62  o complete the b
431f0 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e  ackup operation.
43200 20 4f 72 2c 20 69 66 0a 2a 2a 20 61 6e 20 6f 75   Or, if.** an ou
43210 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e 64  t-of-memory cond
43220 69 74 69 6f 6e 20 6f 72 20 49 4f 20 65 72 72 6f  ition or IO erro
43230 72 20 6f 63 63 75 72 65 64 20 64 75 72 69 6e 67  r occured during
43240 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71   a call to.** sq
43250 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
43260 70 28 29 20 74 68 65 6e 20 5b 53 51 4c 49 54 45  p() then [SQLITE
43270 5f 4e 4f 4d 45 4d 5d 20 6f 72 20 61 6e 0a 2a 2a  _NOMEM] or an.**
43280 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41   [SQLITE_IOERR_A
43290 43 43 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49  CCESS | SQLITE_I
432a0 4f 45 52 52 5f 58 58 58 5d 20 65 72 72 6f 72 20  OERR_XXX] error 
432b0 63 6f 64 65 0a 2a 2a 20 69 73 20 72 65 74 75 72  code.** is retur
432c0 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ned. In this cas
432d0 65 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  e the error code
432e0 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65   and an error me
432f0 73 73 61 67 65 20 61 72 65 0a 2a 2a 20 77 72 69  ssage are.** wri
43300 74 74 65 6e 20 74 6f 20 74 68 65 20 64 65 73 74  tten to the dest
43310 69 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 61 73  ination [databas
43320 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a  e connection]..*
43330 2a 0a 2a 2a 20 41 20 72 65 74 75 72 6e 20 6f 66  *.** A return of
43340 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f   [SQLITE_BUSY] o
43350 72 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  r [SQLITE_LOCKED
43360 5d 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 62  ] from sqlite3_b
43370 61 63 6b 75 70 5f 73 74 65 70 28 29 20 69 73 0a  ackup_step() is.
43380 2a 2a 20 6e 6f 74 20 61 20 70 65 72 6d 61 6e 65  ** not a permane
43390 6e 74 20 65 72 72 6f 72 20 61 6e 64 20 64 6f 65  nt error and doe
433a0 73 20 6e 6f 74 20 61 66 66 65 63 74 20 74 68 65  s not affect the
433b0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66   return value of
433c0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  .** sqlite3_back
433d0 75 70 5f 66 69 6e 69 73 68 28 29 2e 0a 2a 2a 0a  up_finish()..**.
433e0 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61  ** <b>sqlite3_ba
433f0 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29  ckup_remaining()
43400 2c 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  , sqlite3_backup
43410 5f 70 61 67 65 63 6f 75 6e 74 28 29 3c 2f 62 3e  _pagecount()</b>
43420 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 63 61 6c 6c  .**.** Each call
43430 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   to sqlite3_back
43440 75 70 5f 73 74 65 70 28 29 20 73 65 74 73 20 74  up_step() sets t
43450 77 6f 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64  wo values stored
43460 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 62   internally.** b
43470 79 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 62 61  y an [sqlite3_ba
43480 63 6b 75 70 5d 20 6f 62 6a 65 63 74 2e 20 54 68  ckup] object. Th
43490 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
434a0 73 20 73 74 69 6c 6c 20 74 6f 20 62 65 20 62 61  s still to be ba
434b0 63 6b 65 64 0a 2a 2a 20 75 70 2c 20 77 68 69 63  cked.** up, whic
434c0 68 20 6d 61 79 20 62 65 20 71 75 65 72 69 65 64  h may be queried
434d0 20 62 79 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   by sqlite3_back
434e0 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 2c 20  up_remaining(), 
434f0 61 6e 64 20 74 68 65 20 74 6f 74 61 6c 0a 2a 2a  and the total.**
43500 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
43510 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64   in the source d
43520 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 77 68  atabase file, wh
43530 69 63 68 20 6d 61 79 20 62 65 20 71 75 65 72 69  ich may be queri
43540 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33  ed by.** sqlite3
43550 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e  _backup_pagecoun
43560 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76  t()..**.** The v
43570 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 62  alues returned b
43580 79 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e  y these function
43590 73 20 61 72 65 20 6f 6e 6c 79 20 75 70 64 61 74  s are only updat
435a0 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33  ed by.** sqlite3
435b0 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20  _backup_step(). 
435c0 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 64 61  If the source da
435d0 74 61 62 61 73 65 20 69 73 20 6d 6f 64 69 66 69  tabase is modifi
435e0 65 64 20 64 75 72 69 6e 67 20 61 20 62 61 63 6b  ed during a back
435f0 75 70 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2c  up.** operation,
43600 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 73   then the values
43610 20 61 72 65 20 6e 6f 74 20 75 70 64 61 74 65 64   are not updated
43620 20 74 6f 20 61 63 63 6f 75 6e 74 20 66 6f 72 20   to account for 
43630 61 6e 79 20 65 78 74 72 61 0a 2a 2a 20 70 61 67  any extra.** pag
43640 65 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20  es that need to 
43650 62 65 20 75 70 64 61 74 65 64 20 6f 72 20 74 68  be updated or th
43660 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 73 6f  e size of the so
43670 75 72 63 65 20 64 61 74 61 62 61 73 65 20 66 69  urce database fi
43680 6c 65 0a 2a 2a 20 63 68 61 6e 67 69 6e 67 2e 0a  le.** changing..
43690 2a 2a 0a 2a 2a 20 3c 62 3e 43 6f 6e 63 75 72 72  **.** <b>Concurr
436a0 65 6e 74 20 55 73 61 67 65 20 6f 66 20 44 61 74  ent Usage of Dat
436b0 61 62 61 73 65 20 48 61 6e 64 6c 65 73 3c 2f 62  abase Handles</b
436c0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 6f 75 72  >.**.** The sour
436d0 63 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ce [database con
436e0 6e 65 63 74 69 6f 6e 5d 20 6d 61 79 20 62 65 20  nection] may be 
436f0 75 73 65 64 20 62 79 20 74 68 65 20 61 70 70 6c  used by the appl
43700 69 63 61 74 69 6f 6e 20 66 6f 72 20 6f 74 68 65  ication for othe
43710 72 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 77 68  r.** purposes wh
43720 69 6c 65 20 61 20 62 61 63 6b 75 70 20 6f 70 65  ile a backup ope
43730 72 61 74 69 6f 6e 20 69 73 20 75 6e 64 65 72 77  ration is underw
43740 61 79 20 6f 72 20 62 65 69 6e 67 20 69 6e 69 74  ay or being init
43750 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 66 20 53  ialized..** If S
43760 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65  QLite is compile
43770 64 20 61 6e 64 20 63 6f 6e 66 69 67 75 72 65 64  d and configured
43780 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68 72 65   to support thre
43790 61 64 73 61 66 65 20 64 61 74 61 62 61 73 65 0a  adsafe database.
437a0 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 20  ** connections, 
437b0 74 68 65 6e 20 74 68 65 20 73 6f 75 72 63 65 20  then the source 
437c0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
437d0 69 6f 6e 20 6d 61 79 20 62 65 20 75 73 65 64 20  ion may be used 
437e0 63 6f 6e 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20  concurrently.** 
437f0 66 72 6f 6d 20 77 69 74 68 69 6e 20 6f 74 68 65  from within othe
43800 72 20 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a  r threads..**.**
43810 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20 61 70   However, the ap
43820 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 67  plication must g
43830 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68  uarantee that th
43840 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61  e destination da
43850 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63  tabase.** connec
43860 74 69 6f 6e 20 68 61 6e 64 6c 65 20 69 73 20 6e  tion handle is n
43870 6f 74 20 70 61 73 73 65 64 20 74 6f 20 61 6e 79  ot passed to any
43880 20 6f 74 68 65 72 20 41 50 49 20 28 62 79 20 61   other API (by a
43890 6e 79 20 74 68 72 65 61 64 29 20 61 66 74 65 72  ny thread) after
438a0 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63   .** sqlite3_bac
438b0 6b 75 70 5f 69 6e 69 74 28 29 20 69 73 20 63 61  kup_init() is ca
438c0 6c 6c 65 64 20 61 6e 64 20 62 65 66 6f 72 65 20  lled and before 
438d0 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
438e0 67 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c  g call to.** sql
438f0 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69  ite3_backup_fini
43900 73 68 28 29 2e 20 55 6e 66 6f 72 74 75 6e 61 74  sh(). Unfortunat
43910 65 6c 79 20 53 51 4c 69 74 65 20 64 6f 65 73 20  ely SQLite does 
43920 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 63 68  not currently ch
43930 65 63 6b 0a 2a 2a 20 66 6f 72 20 74 68 69 73 2c  eck.** for this,
43940 20 69 66 20 74 68 65 20 61 70 70 6c 69 63 61 74   if the applicat
43950 69 6f 6e 20 64 6f 65 73 20 75 73 65 20 74 68 65  ion does use the
43960 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 5b 64 61   destination [da
43970 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
43980 6e 5d 0a 2a 2a 20 66 6f 72 20 73 6f 6d 65 20 6f  n].** for some o
43990 74 68 65 72 20 70 75 72 70 6f 73 65 20 64 75 72  ther purpose dur
439a0 69 6e 67 20 61 20 62 61 63 6b 75 70 20 6f 70 65  ing a backup ope
439b0 72 61 74 69 6f 6e 2c 20 74 68 69 6e 67 73 20 6d  ration, things m
439c0 61 79 20 61 70 70 65 61 72 20 74 6f 0a 2a 2a 20  ay appear to.** 
439d0 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 62  work correctly b
439e0 75 74 20 69 6e 20 66 61 63 74 20 62 65 20 73 75  ut in fact be su
439f0 62 74 6c 79 20 6d 61 6c 66 75 6e 63 74 69 6f 6e  btly malfunction
43a00 69 6e 67 2e 20 20 55 73 65 20 6f 66 20 74 68 65  ing.  Use of the
43a10 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  .** destination 
43a20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
43a30 69 6f 6e 20 77 68 69 6c 65 20 61 20 62 61 63 6b  ion while a back
43a40 75 70 20 69 73 20 69 6e 20 70 72 6f 67 72 65 73  up is in progres
43a50 73 20 6d 69 67 68 74 0a 2a 2a 20 61 6c 73 6f 20  s might.** also 
43a60 63 61 75 73 65 20 61 20 6d 75 74 65 78 20 64 65  cause a mutex de
43a70 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 75  adlock..**.** Fu
43a80 72 74 68 65 72 6d 6f 72 65 2c 20 69 66 20 72 75  rthermore, if ru
43a90 6e 6e 69 6e 67 20 69 6e 20 5b 73 68 61 72 65 64  nning in [shared
43aa0 20 63 61 63 68 65 20 6d 6f 64 65 5d 2c 20 74 68   cache mode], th
43ab0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75  e application mu
43ac0 73 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 20  st.** guarantee 
43ad0 74 68 61 74 20 74 68 65 20 73 68 61 72 65 64 20  that the shared 
43ae0 63 61 63 68 65 20 75 73 65 64 20 62 79 20 74 68  cache used by th
43af0 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61  e destination da
43b00 74 61 62 61 73 65 0a 2a 2a 20 69 73 20 6e 6f 74  tabase.** is not
43b10 20 61 63 63 65 73 73 65 64 20 77 68 69 6c 65 20   accessed while 
43b20 74 68 65 20 62 61 63 6b 75 70 20 69 73 20 72 75  the backup is ru
43b30 6e 6e 69 6e 67 2e 20 49 6e 20 70 72 61 63 74 69  nning. In practi
43b40 63 65 20 74 68 69 73 20 6d 65 61 6e 73 0a 2a 2a  ce this means.**
43b50 20 74 68 61 74 20 74 68 65 20 61 70 70 6c 69 63   that the applic
43b60 61 74 69 6f 6e 20 6d 75 73 74 20 67 75 61 72 61  ation must guara
43b70 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 66 69  ntee that the fi
43b80 6c 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 20 62  le-system file b
43b90 65 69 6e 67 20 0a 2a 2a 20 62 61 63 6b 65 64 20  eing .** backed 
43ba0 75 70 20 74 6f 20 69 73 20 6e 6f 74 20 61 63 63  up to is not acc
43bb0 65 73 73 65 64 20 62 79 20 61 6e 79 20 63 6f 6e  essed by any con
43bc0 6e 65 63 74 69 6f 6e 20 77 69 74 68 69 6e 20 74  nection within t
43bd0 68 65 20 70 72 6f 63 65 73 73 2c 0a 2a 2a 20 6e  he process,.** n
43be0 6f 74 20 6a 75 73 74 20 74 68 65 20 73 70 65 63  ot just the spec
43bf0 69 66 69 63 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  ific connection 
43c00 74 68 61 74 20 77 61 73 20 70 61 73 73 65 64 20  that was passed 
43c10 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  to sqlite3_backu
43c20 70 5f 69 6e 69 74 28 29 2e 0a 2a 2a 0a 2a 2a 20  p_init()..**.** 
43c30 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63  The [sqlite3_bac
43c40 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 74 73 65  kup] object itse
43c50 6c 66 20 69 73 20 70 61 72 74 69 61 6c 6c 79 20  lf is partially 
43c60 74 68 72 65 61 64 73 61 66 65 2e 20 4d 75 6c 74  threadsafe. Mult
43c70 69 70 6c 65 20 0a 2a 2a 20 74 68 72 65 61 64 73  iple .** threads
43c80 20 6d 61 79 20 73 61 66 65 6c 79 20 6d 61 6b 65   may safely make
43c90 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 63 75 72   multiple concur
43ca0 72 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71  rent calls to sq
43cb0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
43cc0 70 28 29 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c  p()..** However,
43cd0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63   the sqlite3_bac
43ce0 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 20  kup_remaining() 
43cf0 61 6e 64 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  and sqlite3_back
43d00 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 0a 2a  up_pagecount().*
43d10 2a 20 41 50 49 73 20 61 72 65 20 6e 6f 74 20 73  * APIs are not s
43d20 74 72 69 63 74 6c 79 20 73 70 65 61 6b 69 6e 67  trictly speaking
43d30 20 74 68 72 65 61 64 73 61 66 65 2e 20 49 66 20   threadsafe. If 
43d40 74 68 65 79 20 61 72 65 20 69 6e 76 6f 6b 65 64  they are invoked
43d50 20 61 74 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20   at the.** same 
43d60 74 69 6d 65 20 61 73 20 61 6e 6f 74 68 65 72 20  time as another 
43d70 74 68 72 65 61 64 20 69 73 20 69 6e 76 6f 6b 69  thread is invoki
43d80 6e 67 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ng sqlite3_backu
43d90 70 5f 73 74 65 70 28 29 20 69 74 20 69 73 0a 2a  p_step() it is.*
43da0 2a 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20  * possible that 
43db0 74 68 65 79 20 72 65 74 75 72 6e 20 69 6e 76 61  they return inva
43dc0 6c 69 64 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 53  lid values..*/.S
43dd0 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
43de0 33 5f 62 61 63 6b 75 70 20 2a 73 71 6c 69 74 65  3_backup *sqlite
43df0 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 0a 20  3_backup_init(. 
43e00 20 73 71 6c 69 74 65 33 20 2a 70 44 65 73 74 2c   sqlite3 *pDest,
43e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43e20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69          /* Desti
43e30 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20  nation database 
43e40 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  handle */.  cons
43e50 74 20 63 68 61 72 20 2a 7a 44 65 73 74 4e 61 6d  t char *zDestNam
43e60 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
43e70 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f     /* Destinatio
43e80 6e 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  n database name 
43e90 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 53  */.  sqlite3 *pS
43ea0 6f 75 72 63 65 2c 20 20 20 20 20 20 20 20 20 20  ource,          
43eb0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
43ec0 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 68  ource database h
43ed0 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  andle */.  const
43ee0 20 63 68 61 72 20 2a 7a 53 6f 75 72 63 65 4e 61   char *zSourceNa
43ef0 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  me              
43f00 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 74 61    /* Source data
43f10 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 29 3b 0a  base name */.);.
43f20 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
43f30 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
43f40 65 70 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ep(sqlite3_backu
43f50 70 20 2a 70 2c 20 69 6e 74 20 6e 50 61 67 65 29  p *p, int nPage)
43f60 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
43f70 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
43f80 66 69 6e 69 73 68 28 73 71 6c 69 74 65 33 5f 62  finish(sqlite3_b
43f90 61 63 6b 75 70 20 2a 70 29 3b 0a 53 51 4c 49 54  ackup *p);.SQLIT
43fa0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
43fb0 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69  3_backup_remaini
43fc0 6e 67 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ng(sqlite3_backu
43fd0 70 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 41 50  p *p);.SQLITE_AP
43fe0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61  I int sqlite3_ba
43ff0 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 73  ckup_pagecount(s
44000 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70  qlite3_backup *p
44010 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
44020 45 46 3a 20 55 6e 6c 6f 63 6b 20 4e 6f 74 69 66  EF: Unlock Notif
44030 69 63 61 74 69 6f 6e 0a 2a 2a 20 45 58 50 45 52  ication.** EXPER
44040 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 57 68  IMENTAL.**.** Wh
44050 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20 73 68  en running in sh
44060 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 2c  ared-cache mode,
44070 20 61 20 64 61 74 61 62 61 73 65 20 6f 70 65 72   a database oper
44080 61 74 69 6f 6e 20 6d 61 79 20 66 61 69 6c 20 77  ation may fail w
44090 69 74 68 0a 2a 2a 20 61 6e 20 5b 53 51 4c 49 54  ith.** an [SQLIT
440a0 45 5f 4c 4f 43 4b 45 44 5d 20 65 72 72 6f 72 20  E_LOCKED] error 
440b0 69 66 20 74 68 65 20 72 65 71 75 69 72 65 64 20  if the required 
440c0 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 73 68 61  locks on the sha
440d0 72 65 64 2d 63 61 63 68 65 20 6f 72 0a 2a 2a 20  red-cache or.** 
440e0 69 6e 64 69 76 69 64 75 61 6c 20 74 61 62 6c 65  individual table
440f0 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61  s within the sha
44100 72 65 64 2d 63 61 63 68 65 20 63 61 6e 6e 6f 74  red-cache cannot
44110 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 20 53 65   be obtained. Se
44120 65 0a 2a 2a 20 5b 53 51 4c 69 74 65 20 53 68 61  e.** [SQLite Sha
44130 72 65 64 2d 43 61 63 68 65 20 4d 6f 64 65 5d 20  red-Cache Mode] 
44140 66 6f 72 20 61 20 64 65 73 63 72 69 70 74 69 6f  for a descriptio
44150 6e 20 6f 66 20 73 68 61 72 65 64 2d 63 61 63 68  n of shared-cach
44160 65 20 6c 6f 63 6b 69 6e 67 2e 20 0a 2a 2a 20 54  e locking. .** T
44170 68 69 73 20 41 50 49 20 6d 61 79 20 62 65 20 75  his API may be u
44180 73 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 20  sed to register 
44190 61 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 20  a callback that 
441a0 53 51 4c 69 74 65 20 77 69 6c 6c 20 69 6e 76 6f  SQLite will invo
441b0 6b 65 20 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20  ke .** when the 
441c0 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72 72 65  connection curre
441d0 6e 74 6c 79 20 68 6f 6c 64 69 6e 67 20 74 68 65  ntly holding the
441e0 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 20 72   required lock r
441f0 65 6c 69 6e 71 75 69 73 68 65 73 20 69 74 2e 0a  elinquishes it..
44200 2a 2a 20 54 68 69 73 20 41 50 49 20 69 73 20 6f  ** This API is o
44210 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66  nly available if
44220 20 74 68 65 20 6c 69 62 72 61 72 79 20 77 61 73   the library was
44230 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74   compiled with t
44240 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e  he.** [SQLITE_EN
44250 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49  ABLE_UNLOCK_NOTI
44260 46 59 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73  FY] C-preprocess
44270 6f 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65  or symbol define
44280 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c 73  d..**.** See Als
44290 6f 3a 20 5b 55 73 69 6e 67 20 74 68 65 20 53 51  o: [Using the SQ
442a0 4c 69 74 65 20 55 6e 6c 6f 63 6b 20 4e 6f 74 69  Lite Unlock Noti
442b0 66 69 63 61 74 69 6f 6e 20 46 65 61 74 75 72 65  fication Feature
442c0 5d 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65 64 2d  ]..**.** Shared-
442d0 63 61 63 68 65 20 6c 6f 63 6b 73 20 61 72 65 20  cache locks are 
442e0 72 65 6c 65 61 73 65 64 20 77 68 65 6e 20 61 20  released when a 
442f0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
44300 69 6f 6e 20 63 6f 6e 63 6c 75 64 65 73 0a 2a 2a  ion concludes.**
44310 20 69 74 73 20 63 75 72 72 65 6e 74 20 74 72 61   its current tra
44320 6e 73 61 63 74 69 6f 6e 2c 20 65 69 74 68 65 72  nsaction, either
44330 20 62 79 20 63 6f 6d 6d 69 74 74 69 6e 67 20 69   by committing i
44340 74 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 69 74 20  t or rolling it 
44350 62 61 63 6b 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65  back. .**.** Whe
44360 6e 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28  n a connection (
44370 6b 6e 6f 77 6e 20 61 73 20 74 68 65 20 62 6c 6f  known as the blo
44380 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 29  cked connection)
44390 20 66 61 69 6c 73 20 74 6f 20 6f 62 74 61 69 6e   fails to obtain
443a0 20 61 0a 2a 2a 20 73 68 61 72 65 64 2d 63 61 63   a.** shared-cac
443b0 68 65 20 6c 6f 63 6b 20 61 6e 64 20 53 51 4c 49  he lock and SQLI
443c0 54 45 5f 4c 4f 43 4b 45 44 20 69 73 20 72 65 74  TE_LOCKED is ret
443d0 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c  urned to the cal
443e0 6c 65 72 2c 20 74 68 65 0a 2a 2a 20 69 64 65 6e  ler, the.** iden
443f0 74 69 74 79 20 6f 66 20 74 68 65 20 64 61 74 61  tity of the data
44400 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
44410 28 74 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f  (the blocking co
44420 6e 6e 65 63 74 69 6f 6e 29 20 74 68 61 74 0a 2a  nnection) that.*
44430 2a 20 68 61 73 20 6c 6f 63 6b 65 64 20 74 68 65  * has locked the
44440 20 72 65 71 75 69 72 65 64 20 72 65 73 6f 75 72   required resour
44450 63 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 74  ce is stored int
44460 65 72 6e 61 6c 6c 79 2e 20 41 66 74 65 72 20 61  ernally. After a
44470 6e 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f  n .** applicatio
44480 6e 20 72 65 63 65 69 76 65 73 20 61 6e 20 53 51  n receives an SQ
44490 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 65 72 72 6f  LITE_LOCKED erro
444a0 72 2c 20 69 74 20 6d 61 79 20 63 61 6c 6c 20 74  r, it may call t
444b0 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 75 6e  he.** sqlite3_un
444c0 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 6d 65  lock_notify() me
444d0 74 68 6f 64 20 77 69 74 68 20 74 68 65 20 62 6c  thod with the bl
444e0 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e  ocked connection
444f0 20 68 61 6e 64 6c 65 20 61 73 20 0a 2a 2a 20 74   handle as .** t
44500 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
44510 74 20 74 6f 20 72 65 67 69 73 74 65 72 20 66 6f  t to register fo
44520 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 68 61  r a callback tha
44530 74 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65  t will be invoke
44540 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 62 6c  d.** when the bl
44550 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f  ocking connectio
44560 6e 73 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73  ns current trans
44570 61 63 74 69 6f 6e 20 69 73 20 63 6f 6e 63 6c 75  action is conclu
44580 64 65 64 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c  ded. The.** call
44590 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20  back is invoked 
445a0 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20  from within the 
445b0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 5d 20 6f  [sqlite3_step] o
445c0 72 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65  r [sqlite3_close
445d0 5d 0a 2a 2a 20 63 61 6c 6c 20 74 68 61 74 20 63  ].** call that c
445e0 6f 6e 63 6c 75 64 65 73 20 74 68 65 20 62 6c 6f  oncludes the blo
445f0 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e  cking connection
44600 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a  s transaction..*
44610 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f  *.** If sqlite3_
44620 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20  unlock_notify() 
44630 69 73 20 63 61 6c 6c 65 64 20 69 6e 20 61 20 6d  is called in a m
44640 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70  ulti-threaded ap
44650 70 6c 69 63 61 74 69 6f 6e 2c 0a 2a 2a 20 74 68  plication,.** th
44660 65 72 65 20 69 73 20 61 20 63 68 61 6e 63 65 20  ere is a chance 
44670 74 68 61 74 20 74 68 65 20 62 6c 6f 63 6b 69 6e  that the blockin
44680 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 6c  g connection wil
44690 6c 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a 2a  l have already.*
446a0 2a 20 63 6f 6e 63 6c 75 64 65 64 20 69 74 73 20  * concluded its 
446b0 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 79 20 74  transaction by t
446c0 68 65 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f  he time sqlite3_
446d0 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20  unlock_notify() 
446e0 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 49  is invoked..** I
446f0 66 20 74 68 69 73 20 68 61 70 70 65 6e 73 2c 20  f this happens, 
44700 74 68 65 6e 20 74 68 65 20 73 70 65 63 69 66 69  then the specifi
44710 65 64 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69  ed callback is i
44720 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74 65  nvoked immediate
44730 6c 79 2c 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68  ly,.** from with
44740 69 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73  in the call to s
44750 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f  qlite3_unlock_no
44760 74 69 66 79 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66  tify()..**.** If
44770 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e   the blocked con
44780 6e 65 63 74 69 6f 6e 20 69 73 20 61 74 74 65 6d  nection is attem
44790 70 74 69 6e 67 20 74 6f 20 6f 62 74 61 69 6e 20  pting to obtain 
447a0 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20  a write-lock on 
447b0 61 0a 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68  a.** shared-cach
447c0 65 20 74 61 62 6c 65 2c 20 61 6e 64 20 6d 6f 72  e table, and mor
447d0 65 20 74 68 61 6e 20 6f 6e 65 20 6f 74 68 65 72  e than one other
447e0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72 72   connection curr
447f0 65 6e 74 6c 79 20 68 6f 6c 64 73 0a 2a 2a 20 61  ently holds.** a
44800 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68   read-lock on th
44810 65 20 73 61 6d 65 20 74 61 62 6c 65 2c 20 74 68  e same table, th
44820 65 6e 20 53 51 4c 69 74 65 20 61 72 62 69 74 72  en SQLite arbitr
44830 61 72 69 6c 79 20 73 65 6c 65 63 74 73 20 6f 6e  arily selects on
44840 65 20 6f 66 20 0a 2a 2a 20 74 68 65 20 6f 74 68  e of .** the oth
44850 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74  er connections t
44860 6f 20 75 73 65 20 61 73 20 74 68 65 20 62 6c 6f  o use as the blo
44870 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e  cking connection
44880 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 61  ..**.** There ma
44890 79 20 62 65 20 61 74 20 6d 6f 73 74 20 6f 6e 65  y be at most one
448a0 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63   unlock-notify c
448b0 61 6c 6c 62 61 63 6b 20 72 65 67 69 73 74 65 72  allback register
448c0 65 64 20 62 79 20 61 20 0a 2a 2a 20 62 6c 6f 63  ed by a .** bloc
448d0 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  ked connection. 
448e0 49 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63  If sqlite3_unloc
448f0 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 20 63 61  k_notify() is ca
44900 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a  lled when the.**
44910 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74   blocked connect
44920 69 6f 6e 20 61 6c 72 65 61 64 79 20 68 61 73 20  ion already has 
44930 61 20 72 65 67 69 73 74 65 72 65 64 20 75 6e 6c  a registered unl
44940 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
44950 61 63 6b 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65  ack,.** then the
44960 20 6e 65 77 20 63 61 6c 6c 62 61 63 6b 20 72 65   new callback re
44970 70 6c 61 63 65 73 20 74 68 65 20 6f 6c 64 2e 20  places the old. 
44980 49 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63  If sqlite3_unloc
44990 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 0a 2a 2a  k_notify() is.**
449a0 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 4e   called with a N
449b0 55 4c 4c 20 70 6f 69 6e 74 65 72 20 61 73 20 69  ULL pointer as i
449c0 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  ts second argume
449d0 6e 74 2c 20 74 68 65 6e 20 61 6e 79 20 65 78 69  nt, then any exi
449e0 73 74 69 6e 67 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d  sting.** unlock-
449f0 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20  notify callback 
44a00 69 73 20 63 61 6e 63 65 6c 6c 65 64 2e 20 54 68  is cancelled. Th
44a10 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63  e blocked connec
44a20 74 69 6f 6e 73 20 0a 2a 2a 20 75 6e 6c 6f 63 6b  tions .** unlock
44a30 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
44a40 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 63 61 6e   may also be can
44a50 63 65 6c 65 64 20 62 79 20 63 6c 6f 73 69 6e 67  celed by closing
44a60 20 74 68 65 20 62 6c 6f 63 6b 65 64 0a 2a 2a 20   the blocked.** 
44a70 63 6f 6e 6e 65 63 74 69 6f 6e 20 75 73 69 6e 67  connection using
44a80 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28   [sqlite3_close(
44a90 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 75 6e  )]..**.** The un
44aa0 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c  lock-notify call
44ab0 62 61 63 6b 20 69 73 20 6e 6f 74 20 72 65 65 6e  back is not reen
44ac0 74 72 61 6e 74 2e 20 49 66 20 61 6e 20 61 70 70  trant. If an app
44ad0 6c 69 63 61 74 69 6f 6e 20 69 6e 76 6f 6b 65 73  lication invokes
44ae0 0a 2a 2a 20 61 6e 79 20 73 71 6c 69 74 65 33 5f  .** any sqlite3_
44af0 78 78 78 20 41 50 49 20 66 75 6e 63 74 69 6f 6e  xxx API function
44b00 73 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 6e  s from within an
44b10 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63   unlock-notify c
44b20 61 6c 6c 62 61 63 6b 2c 20 61 0a 2a 2a 20 63 72  allback, a.** cr
44b30 61 73 68 20 6f 72 20 64 65 61 64 6c 6f 63 6b 20  ash or deadlock 
44b40 6d 61 79 20 62 65 20 74 68 65 20 72 65 73 75 6c  may be the resul
44b50 74 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20  t..**.** Unless 
44b60 64 65 61 64 6c 6f 63 6b 20 69 73 20 64 65 74 65  deadlock is dete
44b70 63 74 65 64 20 28 73 65 65 20 62 65 6c 6f 77 29  cted (see below)
44b80 2c 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b  , sqlite3_unlock
44b90 5f 6e 6f 74 69 66 79 28 29 20 61 6c 77 61 79 73  _notify() always
44ba0 0a 2a 2a 20 72 65 74 75 72 6e 73 20 53 51 4c 49  .** returns SQLI
44bb0 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e  TE_OK..**.** <b>
44bc0 43 61 6c 6c 62 61 63 6b 20 49 6e 76 6f 63 61 74  Callback Invocat
44bd0 69 6f 6e 20 44 65 74 61 69 6c 73 3c 2f 62 3e 0a  ion Details</b>.
44be0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 75 6e  **.** When an un
44bf0 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c  lock-notify call
44c00 62 61 63 6b 20 69 73 20 72 65 67 69 73 74 65 72  back is register
44c10 65 64 2c 20 74 68 65 20 61 70 70 6c 69 63 61 74  ed, the applicat
44c20 69 6f 6e 20 70 72 6f 76 69 64 65 73 20 61 20 0a  ion provides a .
44c30 2a 2a 20 73 69 6e 67 6c 65 20 76 6f 69 64 2a 20  ** single void* 
44c40 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 20  pointer that is 
44c50 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 63 61  passed to the ca
44c60 6c 6c 62 61 63 6b 20 77 68 65 6e 20 69 74 20 69  llback when it i
44c70 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 48 6f  s invoked..** Ho
44c80 77 65 76 65 72 2c 20 74 68 65 20 73 69 67 6e 61  wever, the signa
44c90 74 75 72 65 20 6f 66 20 74 68 65 20 63 61 6c 6c  ture of the call
44ca0 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 61 6c  back function al
44cb0 6c 6f 77 73 20 53 51 4c 69 74 65 20 74 6f 20 70  lows SQLite to p
44cc0 61 73 73 0a 2a 2a 20 69 74 20 61 6e 20 61 72 72  ass.** it an arr
44cd0 61 79 20 6f 66 20 76 6f 69 64 2a 20 63 6f 6e 74  ay of void* cont
44ce0 65 78 74 20 70 6f 69 6e 74 65 72 73 2e 20 54 68  ext pointers. Th
44cf0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
44d00 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20 61 6e   passed to.** an
44d10 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63   unlock-notify c
44d20 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 70 6f 69  allback is a poi
44d30 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79  nter to an array
44d40 20 6f 66 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65   of void* pointe
44d50 72 73 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73  rs,.** and the s
44d60 65 63 6f 6e 64 20 69 73 20 74 68 65 20 6e 75 6d  econd is the num
44d70 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
44d80 6e 20 74 68 65 20 61 72 72 61 79 2e 0a 2a 2a 0a  n the array..**.
44d90 2a 2a 20 57 68 65 6e 20 61 20 62 6c 6f 63 6b 69  ** When a blocki
44da0 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74  ng connections t
44db0 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f  ransaction is co
44dc0 6e 63 6c 75 64 65 64 2c 20 74 68 65 72 65 20 6d  ncluded, there m
44dd0 61 79 20 62 65 0a 2a 2a 20 6d 6f 72 65 20 74 68  ay be.** more th
44de0 61 6e 20 6f 6e 65 20 62 6c 6f 63 6b 65 64 20 63  an one blocked c
44df0 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 68  onnection that h
44e00 61 73 20 72 65 67 69 73 74 65 72 65 64 20 66 6f  as registered fo
44e10 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69  r an unlock-noti
44e20 66 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 2e 20  fy.** callback. 
44e30 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 73  If two or more s
44e40 75 63 68 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e  uch blocked conn
44e50 65 63 74 69 6f 6e 73 20 68 61 76 65 20 73 70 65  ections have spe
44e60 63 69 66 69 65 64 20 74 68 65 0a 2a 2a 20 73 61  cified the.** sa
44e70 6d 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  me callback func
44e80 74 69 6f 6e 2c 20 74 68 65 6e 20 69 6e 73 74 65  tion, then inste
44e90 61 64 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 74  ad of invoking t
44ea0 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  he callback func
44eb0 74 69 6f 6e 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65  tion.** multiple
44ec0 20 74 69 6d 65 73 2c 20 69 74 20 69 73 20 69 6e   times, it is in
44ed0 76 6f 6b 65 64 20 6f 6e 63 65 20 77 69 74 68 20  voked once with 
44ee0 74 68 65 20 73 65 74 20 6f 66 20 76 6f 69 64 2a  the set of void*
44ef0 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72   context pointer
44f00 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62  s.** specified b
44f10 79 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f  y the blocked co
44f20 6e 6e 65 63 74 69 6f 6e 73 20 62 75 6e 64 6c 65  nnections bundle
44f30 64 20 74 6f 67 65 74 68 65 72 20 69 6e 74 6f 20  d together into 
44f40 61 6e 20 61 72 72 61 79 2e 0a 2a 2a 20 54 68 69  an array..** Thi
44f50 73 20 67 69 76 65 73 20 74 68 65 20 61 70 70 6c  s gives the appl
44f60 69 63 61 74 69 6f 6e 20 61 6e 20 6f 70 70 6f 72  ication an oppor
44f70 74 75 6e 69 74 79 20 74 6f 20 70 72 69 6f 72 69  tunity to priori
44f80 74 69 7a 65 20 61 6e 79 20 61 63 74 69 6f 6e 73  tize any actions
44f90 20 0a 2a 2a 20 72 65 6c 61 74 65 64 20 74 6f 20   .** related to 
44fa0 74 68 65 20 73 65 74 20 6f 66 20 75 6e 62 6c 6f  the set of unblo
44fb0 63 6b 65 64 20 64 61 74 61 62 61 73 65 20 63 6f  cked database co
44fc0 6e 6e 65 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  nnections..**.**
44fd0 20 3c 62 3e 44 65 61 64 6c 6f 63 6b 20 44 65 74   <b>Deadlock Det
44fe0 65 63 74 69 6f 6e 3c 2f 62 3e 0a 2a 2a 0a 2a 2a  ection</b>.**.**
44ff0 20 41 73 73 75 6d 69 6e 67 20 74 68 61 74 20 61   Assuming that a
45000 66 74 65 72 20 72 65 67 69 73 74 65 72 69 6e 67  fter registering
45010 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e   for an unlock-n
45020 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 61  otify callback a
45030 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 77 61   .** database wa
45040 69 74 73 20 66 6f 72 20 74 68 65 20 63 61 6c 6c  its for the call
45050 62 61 63 6b 20 74 6f 20 62 65 20 69 73 73 75 65  back to be issue
45060 64 20 62 65 66 6f 72 65 20 74 61 6b 69 6e 67 20  d before taking 
45070 61 6e 79 20 66 75 72 74 68 65 72 0a 2a 2a 20 61  any further.** a
45080 63 74 69 6f 6e 20 28 61 20 72 65 61 73 6f 6e 61  ction (a reasona
45090 62 6c 65 20 61 73 73 75 6d 70 74 69 6f 6e 29 2c  ble assumption),
450a0 20 74 68 65 6e 20 75 73 69 6e 67 20 74 68 69 73   then using this
450b0 20 41 50 49 20 6d 61 79 20 63 61 75 73 65 20 74   API may cause t
450c0 68 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f  he.** applicatio
450d0 6e 20 74 6f 20 64 65 61 64 6c 6f 63 6b 2e 20 46  n to deadlock. F
450e0 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 63  or example, if c
450f0 6f 6e 6e 65 63 74 69 6f 6e 20 58 20 69 73 20 77  onnection X is w
45100 61 69 74 69 6e 67 20 66 6f 72 0a 2a 2a 20 63 6f  aiting for.** co
45110 6e 6e 65 63 74 69 6f 6e 20 59 27 73 20 74 72 61  nnection Y's tra
45120 6e 73 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 63  nsaction to be c
45130 6f 6e 63 6c 75 64 65 64 2c 20 61 6e 64 20 73 69  oncluded, and si
45140 6d 69 6c 61 72 6c 79 20 63 6f 6e 6e 65 63 74 69  milarly connecti
45150 6f 6e 0a 2a 2a 20 59 20 69 73 20 77 61 69 74 69  on.** Y is waiti
45160 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e  ng on connection
45170 20 58 27 73 20 74 72 61 6e 73 61 63 74 69 6f 6e   X's transaction
45180 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20 63  , then neither c
45190 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c  onnection.** wil
451a0 6c 20 70 72 6f 63 65 65 64 20 61 6e 64 20 74 68  l proceed and th
451b0 65 20 73 79 73 74 65 6d 20 6d 61 79 20 72 65 6d  e system may rem
451c0 61 69 6e 20 64 65 61 64 6c 6f 63 6b 65 64 20 69  ain deadlocked i
451d0 6e 64 65 66 69 6e 69 74 65 6c 79 2e 0a 2a 2a 0a  ndefinitely..**.
451e0 2a 2a 20 54 6f 20 61 76 6f 69 64 20 74 68 69 73  ** To avoid this
451f0 20 73 63 65 6e 61 72 69 6f 2c 20 74 68 65 20 73   scenario, the s
45200 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f  qlite3_unlock_no
45210 74 69 66 79 28 29 20 70 65 72 66 6f 72 6d 73 20  tify() performs 
45220 64 65 61 64 6c 6f 63 6b 0a 2a 2a 20 64 65 74 65  deadlock.** dete
45230 63 74 69 6f 6e 2e 20 49 66 20 61 20 67 69 76 65  ction. If a give
45240 6e 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  n call to sqlite
45250 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28  3_unlock_notify(
45260 29 20 77 6f 75 6c 64 20 70 75 74 20 74 68 65 0a  ) would put the.
45270 2a 2a 20 73 79 73 74 65 6d 20 69 6e 20 61 20 64  ** system in a d
45280 65 61 64 6c 6f 63 6b 65 64 20 73 74 61 74 65 2c  eadlocked state,
45290 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4c 4f 43   then SQLITE_LOC
452a0 4b 45 44 20 69 73 20 72 65 74 75 72 6e 65 64 20  KED is returned 
452b0 61 6e 64 20 6e 6f 0a 2a 2a 20 75 6e 6c 6f 63 6b  and no.** unlock
452c0 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
452d0 20 69 73 20 72 65 67 69 73 74 65 72 65 64 2e 20   is registered. 
452e0 54 68 65 20 73 79 73 74 65 6d 20 69 73 20 73 61  The system is sa
452f0 69 64 20 74 6f 20 62 65 20 69 6e 0a 2a 2a 20 61  id to be in.** a
45300 20 64 65 61 64 6c 6f 63 6b 65 64 20 73 74 61 74   deadlocked stat
45310 65 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  e if connection 
45320 41 20 68 61 73 20 72 65 67 69 73 74 65 72 65 64  A has registered
45330 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e   for an unlock-n
45340 6f 74 69 66 79 0a 2a 2a 20 63 61 6c 6c 62 61 63  otify.** callbac
45350 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 6c 75 73  k on the conclus
45360 69 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f  ion of connectio
45370 6e 20 42 27 73 20 74 72 61 6e 73 61 63 74 69 6f  n B's transactio
45380 6e 2c 20 61 6e 64 20 63 6f 6e 6e 65 63 74 69 6f  n, and connectio
45390 6e 0a 2a 2a 20 42 20 68 61 73 20 69 74 73 65 6c  n.** B has itsel
453a0 66 20 72 65 67 69 73 74 65 72 65 64 20 66 6f 72  f registered for
453b0 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66   an unlock-notif
453c0 79 20 63 61 6c 6c 62 61 63 6b 20 77 68 65 6e 20  y callback when 
453d0 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 41 27  connection.** A'
453e0 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  s transaction is
453f0 20 63 6f 6e 63 6c 75 64 65 64 2e 20 49 6e 64 69   concluded. Indi
45400 72 65 63 74 20 64 65 61 64 6c 6f 63 6b 20 69 73  rect deadlock is
45410 20 61 6c 73 6f 20 64 65 74 65 63 74 65 64 2c 20   also detected, 
45420 73 6f 0a 2a 2a 20 74 68 65 20 73 79 73 74 65 6d  so.** the system
45430 20 69 73 20 61 6c 73 6f 20 63 6f 6e 73 69 64 65   is also conside
45440 72 65 64 20 74 6f 20 62 65 20 64 65 61 64 6c 6f  red to be deadlo
45450 63 6b 65 64 20 69 66 20 63 6f 6e 6e 65 63 74 69  cked if connecti
45460 6f 6e 20 42 20 68 61 73 0a 2a 2a 20 72 65 67 69  on B has.** regi
45470 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20 75 6e  stered for an un
45480 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c  lock-notify call
45490 62 61 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63  back on the conc
454a0 6c 75 73 69 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63  lusion of connec
454b0 74 69 6f 6e 0a 2a 2a 20 43 27 73 20 74 72 61 6e  tion.** C's tran
454c0 73 61 63 74 69 6f 6e 2c 20 77 68 65 72 65 20 63  saction, where c
454d0 6f 6e 6e 65 63 74 69 6f 6e 20 43 20 69 73 20 77  onnection C is w
454e0 61 69 74 69 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63  aiting on connec
454f0 74 69 6f 6e 20 41 2e 20 41 6e 79 0a 2a 2a 20 6e  tion A. Any.** n
45500 75 6d 62 65 72 20 6f 66 20 6c 65 76 65 6c 73 20  umber of levels 
45510 6f 66 20 69 6e 64 69 72 65 63 74 69 6f 6e 20 61  of indirection a
45520 72 65 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2a 0a 2a  re allowed..**.*
45530 2a 20 3c 62 3e 54 68 65 20 22 44 52 4f 50 20 54  * <b>The "DROP T
45540 41 42 4c 45 22 20 45 78 63 65 70 74 69 6f 6e 3c  ABLE" Exception<
45550 2f 62 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61  /b>.**.** When a
45560 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
45570 33 5f 73 74 65 70 28 29 5d 20 72 65 74 75 72 6e  3_step()] return
45580 73 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2c  s SQLITE_LOCKED,
45590 20 69 74 20 69 73 20 61 6c 6d 6f 73 74 20 0a 2a   it is almost .*
455a0 2a 20 61 6c 77 61 79 73 20 61 70 70 72 6f 70 72  * always appropr
455b0 69 61 74 65 20 74 6f 20 63 61 6c 6c 20 73 71 6c  iate to call sql
455c0 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69  ite3_unlock_noti
455d0 66 79 28 29 2e 20 54 68 65 72 65 20 69 73 20 68  fy(). There is h
455e0 6f 77 65 76 65 72 2c 0a 2a 2a 20 6f 6e 65 20 65  owever,.** one e
455f0 78 63 65 70 74 69 6f 6e 2e 20 57 68 65 6e 20 65  xception. When e
45600 78 65 63 75 74 69 6e 67 20 61 20 22 44 52 4f 50  xecuting a "DROP
45610 20 54 41 42 4c 45 22 20 6f 72 20 22 44 52 4f 50   TABLE" or "DROP
45620 20 49 4e 44 45 58 22 20 73 74 61 74 65 6d 65 6e   INDEX" statemen
45630 74 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 63 68 65  t,.** SQLite che
45640 63 6b 73 20 69 66 20 74 68 65 72 65 20 61 72 65  cks if there are
45650 20 61 6e 79 20 63 75 72 72 65 6e 74 6c 79 20 65   any currently e
45660 78 65 63 75 74 69 6e 67 20 53 45 4c 45 43 54 20  xecuting SELECT 
45670 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68  statements.** th
45680 61 74 20 62 65 6c 6f 6e 67 20 74 6f 20 74 68 65  at belong to the
45690 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e   same connection
456a0 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 2c 20  . If there are, 
456b0 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 73  SQLITE_LOCKED is
456c0 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 49 6e  .** returned. In
456d0 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65   this case there
456e0 20 69 73 20 6e 6f 20 22 62 6c 6f 63 6b 69 6e 67   is no "blocking
456f0 20 63 6f 6e 6e 65 63 74 69 6f 6e 22 2c 20 73 6f   connection", so
45700 20 69 6e 76 6f 6b 69 6e 67 0a 2a 2a 20 73 71 6c   invoking.** sql
45710 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69  ite3_unlock_noti
45720 66 79 28 29 20 72 65 73 75 6c 74 73 20 69 6e 20  fy() results in 
45730 74 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66  the unlock-notif
45740 79 20 63 61 6c 6c 62 61 63 6b 20 62 65 69 6e 67  y callback being
45750 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 69 6d 6d 65  .** invoked imme
45760 64 69 61 74 65 6c 79 2e 20 49 66 20 74 68 65 20  diately. If the 
45770 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 68 65 6e  application then
45780 20 72 65 2d 61 74 74 65 6d 70 74 73 20 74 68 65   re-attempts the
45790 20 22 44 52 4f 50 20 54 41 42 4c 45 22 0a 2a 2a   "DROP TABLE".**
457a0 20 6f 72 20 22 44 52 4f 50 20 49 4e 44 45 58 22   or "DROP INDEX"
457b0 20 71 75 65 72 79 2c 20 61 6e 20 69 6e 66 69 6e   query, an infin
457c0 69 74 65 20 6c 6f 6f 70 20 6d 69 67 68 74 20 62  ite loop might b
457d0 65 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a  e the result..**
457e0 0a 2a 2a 20 4f 6e 65 20 77 61 79 20 61 72 6f 75  .** One way arou
457f0 6e 64 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20  nd this problem 
45800 69 73 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20  is to check the 
45810 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63  extended error c
45820 6f 64 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20  ode returned.** 
45830 62 79 20 61 6e 20 73 71 6c 69 74 65 33 5f 73 74  by an sqlite3_st
45840 65 70 28 29 20 63 61 6c 6c 2e 20 49 66 20 74 68  ep() call. If th
45850 65 72 65 20 69 73 20 61 20 62 6c 6f 63 6b 69 6e  ere is a blockin
45860 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 68  g connection, th
45870 65 6e 20 74 68 65 0a 2a 2a 20 65 78 74 65 6e 64  en the.** extend
45880 65 64 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  ed error code is
45890 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4c   set to SQLITE_L
458a0 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48  OCKED_SHAREDCACH
458b0 45 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 6e  E. Otherwise, in
458c0 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 61 6c 20  .** the special 
458d0 22 44 52 4f 50 20 54 41 42 4c 45 2f 49 4e 44 45  "DROP TABLE/INDE
458e0 58 22 20 63 61 73 65 2c 20 74 68 65 20 65 78 74  X" case, the ext
458f0 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65  ended error code
45900 20 69 73 20 6a 75 73 74 20 0a 2a 2a 20 53 51 4c   is just .** SQL
45910 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 53  ITE_LOCKED..*/.S
45920 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
45930 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74  lite3_unlock_not
45940 69 66 79 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ify(.  sqlite3 *
45950 70 42 6c 6f 63 6b 65 64 2c 20 20 20 20 20 20 20  pBlocked,       
45960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45970 20 20 20 2f 2a 20 57 61 69 74 69 6e 67 20 63 6f     /* Waiting co
45980 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f  nnection */.  vo
45990 69 64 20 28 2a 78 4e 6f 74 69 66 79 29 28 76 6f  id (*xNotify)(vo
459a0 69 64 20 2a 2a 61 70 41 72 67 2c 20 69 6e 74 20  id **apArg, int 
459b0 6e 41 72 67 29 2c 20 20 20 20 2f 2a 20 43 61 6c  nArg),    /* Cal
459c0 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 74  lback function t
459d0 6f 20 69 6e 76 6f 6b 65 20 2a 2f 0a 20 20 76 6f  o invoke */.  vo
459e0 69 64 20 2a 70 4e 6f 74 69 66 79 41 72 67 20 20  id *pNotifyArg  
459f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45a00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67            /* Arg
45a10 75 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 74 6f  ument to pass to
45a20 20 78 4e 6f 74 69 66 79 20 2a 2f 0a 29 3b 0a 0a   xNotify */.);..
45a30 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
45a40 20 53 74 72 69 6e 67 20 43 6f 6d 70 61 72 69 73   String Comparis
45a50 6f 6e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  on.** EXPERIMENT
45a60 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71  AL.**.** The [sq
45a70 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 28 29  lite3_strnicmp()
45a80 5d 20 41 50 49 20 61 6c 6c 6f 77 73 20 61 70 70  ] API allows app
45a90 6c 69 63 61 74 69 6f 6e 73 20 61 6e 64 20 65 78  lications and ex
45aa0 74 65 6e 73 69 6f 6e 73 20 74 6f 0a 2a 2a 20 63  tensions to.** c
45ab0 6f 6d 70 61 72 65 20 74 68 65 20 63 6f 6e 74 65  ompare the conte
45ac0 6e 74 73 20 6f 66 20 74 77 6f 20 62 75 66 66 65  nts of two buffe
45ad0 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 55 54  rs containing UT
45ae0 46 2d 38 20 73 74 72 69 6e 67 73 20 69 6e 20 61  F-8 strings in a
45af0 0a 2a 2a 20 63 61 73 65 2d 69 6e 64 65 6e 64 65  .** case-indende
45b00 6e 74 20 66 61 73 68 69 6f 6e 2c 20 75 73 69 6e  nt fashion, usin
45b10 67 20 74 68 65 20 73 61 6d 65 20 64 65 66 69 6e  g the same defin
45b20 69 74 69 6f 6e 20 6f 66 20 63 61 73 65 20 69 6e  ition of case in
45b30 64 65 70 65 6e 64 65 6e 63 65 20 0a 2a 2a 20 74  dependence .** t
45b40 68 61 74 20 53 51 4c 69 74 65 20 75 73 65 73 20  hat SQLite uses 
45b50 69 6e 74 65 72 6e 61 6c 6c 79 20 77 68 65 6e 20  internally when 
45b60 63 6f 6d 70 61 72 69 6e 67 20 69 64 65 6e 74 69  comparing identi
45b70 66 69 65 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  fiers..*/.SQLITE
45b80 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
45b90 5f 73 74 72 6e 69 63 6d 70 28 63 6f 6e 73 74 20  _strnicmp(const 
45ba0 63 68 61 72 20 2a 2c 20 63 6f 6e 73 74 20 63 68  char *, const ch
45bb0 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a  ar *, int);../*.
45bc0 2a 2a 20 55 6e 64 6f 20 74 68 65 20 68 61 63 6b  ** Undo the hack
45bd0 20 74 68 61 74 20 63 6f 6e 76 65 72 74 73 20 66   that converts f
45be0 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 74 79  loating point ty
45bf0 70 65 73 20 74 6f 20 69 6e 74 65 67 65 72 20 66  pes to integer f
45c00 6f 72 0a 2a 2a 20 62 75 69 6c 64 73 20 6f 6e 20  or.** builds on 
45c10 70 72 6f 63 65 73 73 6f 72 73 20 77 69 74 68 6f  processors witho
45c20 75 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  ut floating poin
45c30 74 20 73 75 70 70 6f 72 74 2e 0a 2a 2f 0a 23 69  t support..*/.#i
45c40 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
45c50 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
45c60 23 20 75 6e 64 65 66 20 64 6f 75 62 6c 65 0a 23  # undef double.#
45c70 65 6e 64 69 66 0a 0a 23 69 66 20 30 0a 7d 20 20  endif..#if 0.}  
45c80 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 27 65  /* End of the 'e
45c90 78 74 65 72 6e 20 22 43 22 27 20 62 6c 6f 63 6b  xtern "C"' block
45ca0 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69   */.#endif.#endi
45cb0 66 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f.../***********
45cc0 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74  *** End of sqlit
45cd0 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e3.h ***********
45ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45d00 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
45d10 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
45d20 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
45d30 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   in sqliteInt.h 
45d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45d50 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
45d60 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 61 73 68  *** Include hash
45d70 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
45d80 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   of sqliteInt.h 
45d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45da0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
45db0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68  *** Begin file h
45dc0 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ash.h **********
45dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45df0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
45e00 65 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a  eptember 22.**.*
45e10 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
45e20 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
45e30 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
45e40 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
45e50 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
45e60 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
45e70 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
45e80 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
45e90 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
45ea0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
45eb0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
45ec0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
45ed0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
45ee0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
45ef0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
45f00 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
45f10 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
45f20 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
45f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
45f70 69 73 20 69 73 20 74 68 65 20 68 65 61 64 65 72  is is the header
45f80 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 67 65   file for the ge
45f90 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c 65  neric hash-table
45fa0 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 0a 2a   implemenation.*
45fb0 2a 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65  * used in SQLite
45fc0 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 61 73  ..**.** $Id: has
45fd0 68 2e 68 2c 76 20 31 2e 31 35 20 32 30 30 39 2f  h.h,v 1.15 2009/
45fe0 30 35 2f 30 32 20 31 33 3a 32 39 3a 33 38 20 64  05/02 13:29:38 d
45ff0 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e  rh Exp $.*/.#ifn
46000 64 65 66 20 5f 53 51 4c 49 54 45 5f 48 41 53 48  def _SQLITE_HASH
46010 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c  _H_.#define _SQL
46020 49 54 45 5f 48 41 53 48 5f 48 5f 0a 0a 2f 2a 20  ITE_HASH_H_../* 
46030 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74  Forward declarat
46040 69 6f 6e 73 20 6f 66 20 73 74 72 75 63 74 75 72  ions of structur
46050 65 73 2e 20 2a 2f 0a 74 79 70 65 64 65 66 20 73  es. */.typedef s
46060 74 72 75 63 74 20 48 61 73 68 20 48 61 73 68 3b  truct Hash Hash;
46070 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
46080 48 61 73 68 45 6c 65 6d 20 48 61 73 68 45 6c 65  HashElem HashEle
46090 6d 3b 0a 0a 2f 2a 20 41 20 63 6f 6d 70 6c 65 74  m;../* A complet
460a0 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20  e hash table is 
460b0 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
460c0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
460d0 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20 69  ucture..** The i
460e0 6e 74 65 72 6e 61 6c 73 20 6f 66 20 74 68 69 73  nternals of this
460f0 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 69   structure are i
46100 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70  ntended to be op
46110 61 71 75 65 20 2d 2d 20 63 6c 69 65 6e 74 0a 2a  aque -- client.*
46120 2a 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e 6f  * code should no
46130 74 20 61 74 74 65 6d 70 74 20 74 6f 20 61 63 63  t attempt to acc
46140 65 73 73 20 6f 72 20 6d 6f 64 69 66 79 20 74 68  ess or modify th
46150 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68 69 73  e fields of this
46160 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 64 69   structure.** di
46170 72 65 63 74 6c 79 2e 20 20 43 68 61 6e 67 65 20  rectly.  Change 
46180 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6f  this structure o
46190 6e 6c 79 20 62 79 20 75 73 69 6e 67 20 74 68 65  nly by using the
461a0 20 72 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77 2e   routines below.
461b0 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 73 6f 6d  .** However, som
461c0 65 20 6f 66 20 74 68 65 20 22 70 72 6f 63 65 64  e of the "proced
461d0 75 72 65 73 22 20 61 6e 64 20 22 66 75 6e 63 74  ures" and "funct
461e0 69 6f 6e 73 22 20 66 6f 72 20 6d 6f 64 69 66 79  ions" for modify
461f0 69 6e 67 20 61 6e 64 0a 2a 2a 20 61 63 63 65 73  ing and.** acces
46200 73 69 6e 67 20 74 68 69 73 20 73 74 72 75 63 74  sing this struct
46210 75 72 65 20 61 72 65 20 72 65 61 6c 6c 79 20 6d  ure are really m
46220 61 63 72 6f 73 2c 20 73 6f 20 77 65 20 63 61 6e  acros, so we can
46230 27 74 20 72 65 61 6c 6c 79 20 6d 61 6b 65 0a 2a  't really make.*
46240 2a 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  * this structure
46250 20 6f 70 61 71 75 65 2e 0a 2a 2a 0a 2a 2a 20 41   opaque..**.** A
46260 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74  ll elements of t
46270 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 61 72  he hash table ar
46280 65 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 6f  e on a single do
46290 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74  ubly-linked list
462a0 2e 0a 2a 2a 20 48 61 73 68 2e 66 69 72 73 74 20  ..** Hash.first 
462b0 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 68 65  points to the he
462c0 61 64 20 6f 66 20 74 68 69 73 20 6c 69 73 74 2e  ad of this list.
462d0 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65  .**.** There are
462e0 20 48 61 73 68 2e 68 74 73 69 7a 65 20 62 75 63   Hash.htsize buc
462f0 6b 65 74 73 2e 20 20 45 61 63 68 20 62 75 63 6b  kets.  Each buck
46300 65 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73  et points to a s
46310 70 6f 74 20 69 6e 0a 2a 2a 20 74 68 65 20 67 6c  pot in.** the gl
46320 6f 62 61 6c 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b  obal doubly-link
46330 65 64 20 6c 69 73 74 2e 20 20 54 68 65 20 63 6f  ed list.  The co
46340 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 62 75  ntents of the bu
46350 63 6b 65 74 20 61 72 65 20 74 68 65 0a 2a 2a 20  cket are the.** 
46360 65 6c 65 6d 65 6e 74 20 70 6f 69 6e 74 65 64 20  element pointed 
46370 74 6f 20 70 6c 75 73 20 74 68 65 20 6e 65 78 74  to plus the next
46380 20 5f 68 74 2e 63 6f 75 6e 74 2d 31 20 65 6c 65   _ht.count-1 ele
46390 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 6c 69 73  ments in the lis
463a0 74 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68 2e 68 74  t..**.** Hash.ht
463b0 73 69 7a 65 20 61 6e 64 20 48 61 73 68 2e 68 74  size and Hash.ht
463c0 20 6d 61 79 20 62 65 20 7a 65 72 6f 2e 20 20 49   may be zero.  I
463d0 6e 20 74 68 61 74 20 63 61 73 65 20 6c 6f 6f 6b  n that case look
463e0 75 70 20 69 73 20 64 6f 6e 65 0a 2a 2a 20 62 79  up is done.** by
463f0 20 61 20 6c 69 6e 65 61 72 20 73 65 61 72 63 68   a linear search
46400 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c 20 6c   of the global l
46410 69 73 74 2e 20 20 46 6f 72 20 73 6d 61 6c 6c 20  ist.  For small 
46420 74 61 62 6c 65 73 2c 20 74 68 65 20 0a 2a 2a 20  tables, the .** 
46430 48 61 73 68 2e 68 74 20 74 61 62 6c 65 20 69 73  Hash.ht table is
46440 20 6e 65 76 65 72 20 61 6c 6c 6f 63 61 74 65 64   never allocated
46450 20 62 65 63 61 75 73 65 20 69 66 20 74 68 65 72   because if ther
46460 65 20 61 72 65 20 66 65 77 20 65 6c 65 6d 65 6e  e are few elemen
46470 74 73 0a 2a 2a 20 69 6e 20 74 68 65 20 74 61 62  ts.** in the tab
46480 6c 65 2c 20 69 74 20 69 73 20 66 61 73 74 65 72  le, it is faster
46490 20 74 6f 20 64 6f 20 61 20 6c 69 6e 65 61 72 20   to do a linear 
464a0 73 65 61 72 63 68 20 74 68 61 6e 20 74 6f 20 6d  search than to m
464b0 61 6e 61 67 65 0a 2a 2a 20 74 68 65 20 68 61 73  anage.** the has
464c0 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75  h table..*/.stru
464d0 63 74 20 48 61 73 68 20 7b 0a 20 20 75 6e 73 69  ct Hash {.  unsi
464e0 67 6e 65 64 20 69 6e 74 20 68 74 73 69 7a 65 3b  gned int htsize;
464f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
46500 6f 66 20 62 75 63 6b 65 74 73 20 69 6e 20 74 68  of buckets in th
46510 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a  e hash table */.
46520 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63    unsigned int c
46530 6f 75 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 4e  ount;       /* N
46540 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
46550 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a   in this table *
46560 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 66 69  /.  HashElem *fi
46570 72 73 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  rst;          /*
46580 20 54 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65   The first eleme
46590 6e 74 20 6f 66 20 74 68 65 20 61 72 72 61 79 20  nt of the array 
465a0 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 68 74 20  */.  struct _ht 
465b0 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  {              /
465c0 2a 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  * the hash table
465d0 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 6f 75 6e   */.    int coun
465e0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
465f0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
46600 65 6e 74 72 69 65 73 20 77 69 74 68 20 74 68 69  entries with thi
46610 73 20 68 61 73 68 20 2a 2f 0a 20 20 20 20 48 61  s hash */.    Ha
46620 73 68 45 6c 65 6d 20 2a 63 68 61 69 6e 3b 20 20  shElem *chain;  
46630 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
46640 74 65 72 20 74 6f 20 66 69 72 73 74 20 65 6e 74  ter to first ent
46650 72 79 20 77 69 74 68 20 74 68 69 73 20 68 61 73  ry with this has
46660 68 20 2a 2f 0a 20 20 7d 20 2a 68 74 3b 0a 7d 3b  h */.  } *ht;.};
46670 0a 0a 2f 2a 20 45 61 63 68 20 65 6c 65 6d 65 6e  ../* Each elemen
46680 74 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61  t in the hash ta
46690 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e  ble is an instan
466a0 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
466b0 69 6e 67 20 0a 2a 2a 20 73 74 72 75 63 74 75 72  ing .** structur
466c0 65 2e 20 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73  e.  All elements
466d0 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 61   are stored on a
466e0 20 73 69 6e 67 6c 65 20 64 6f 75 62 6c 79 2d 6c   single doubly-l
466f0 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a  inked list..**.*
46700 2a 20 41 67 61 69 6e 2c 20 74 68 69 73 20 73 74  * Again, this st
46710 72 75 63 74 75 72 65 20 69 73 20 69 6e 74 65 6e  ructure is inten
46720 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65  ded to be opaque
46730 2c 20 62 75 74 20 69 74 20 63 61 6e 27 74 20 72  , but it can't r
46740 65 61 6c 6c 79 0a 2a 2a 20 62 65 20 6f 70 61 71  eally.** be opaq
46750 75 65 20 62 65 63 61 75 73 65 20 69 74 20 69 73  ue because it is
46760 20 75 73 65 64 20 62 79 20 6d 61 63 72 6f 73 2e   used by macros.
46770 0a 2a 2f 0a 73 74 72 75 63 74 20 48 61 73 68 45  .*/.struct HashE
46780 6c 65 6d 20 7b 0a 20 20 48 61 73 68 45 6c 65 6d  lem {.  HashElem
46790 20 2a 6e 65 78 74 2c 20 2a 70 72 65 76 3b 20 20   *next, *prev;  
467a0 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 61 6e 64       /* Next and
467b0 20 70 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e   previous elemen
467c0 74 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20  ts in the table 
467d0 2a 2f 0a 20 20 76 6f 69 64 20 2a 64 61 74 61 3b  */.  void *data;
467e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
467f0 20 20 2f 2a 20 44 61 74 61 20 61 73 73 6f 63 69    /* Data associ
46800 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 65  ated with this e
46810 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73  lement */.  cons
46820 74 20 63 68 61 72 20 2a 70 4b 65 79 3b 20 69 6e  t char *pKey; in
46830 74 20 6e 4b 65 79 3b 20 20 2f 2a 20 4b 65 79 20  t nKey;  /* Key 
46840 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
46850 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a  this element */.
46860 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73 73  };../*.** Access
46870 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 6f 20 64   routines.  To d
46880 65 6c 65 74 65 2c 20 69 6e 73 65 72 74 20 61 20  elete, insert a 
46890 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f  NULL pointer..*/
468a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
468b0 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68  void sqlite3Hash
468c0 49 6e 69 74 28 48 61 73 68 2a 29 3b 0a 53 51 4c  Init(Hash*);.SQL
468d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
468e0 20 2a 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73   *sqlite3HashIns
468f0 65 72 74 28 48 61 73 68 2a 2c 20 63 6f 6e 73 74  ert(Hash*, const
46900 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 69 6e 74   char *pKey, int
46910 20 6e 4b 65 79 2c 20 76 6f 69 64 20 2a 70 44 61   nKey, void *pDa
46920 74 61 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ta);.SQLITE_PRIV
46930 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
46940 33 48 61 73 68 46 69 6e 64 28 63 6f 6e 73 74 20  3HashFind(const 
46950 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 63 68 61  Hash*, const cha
46960 72 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65  r *pKey, int nKe
46970 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  y);.SQLITE_PRIVA
46980 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48  TE void sqlite3H
46990 61 73 68 43 6c 65 61 72 28 48 61 73 68 2a 29 3b  ashClear(Hash*);
469a0 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66  ../*.** Macros f
469b0 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
469c0 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20  all elements of 
469d0 61 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54  a hash table.  T
469e0 68 65 20 69 64 69 6f 6d 20 69 73 0a 2a 2a 20 6c  he idiom is.** l
469f0 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
46a00 20 20 48 61 73 68 20 68 3b 0a 2a 2a 20 20 20 48    Hash h;.**   H
46a10 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 2a 2a 20 20  ashElem *p;.**  
46a20 20 2e 2e 2e 0a 2a 2a 20 20 20 66 6f 72 28 70 3d   ....**   for(p=
46a30 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28  sqliteHashFirst(
46a40 26 68 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65  &h); p; p=sqlite
46a50 48 61 73 68 4e 65 78 74 28 70 29 29 7b 0a 2a 2a  HashNext(p)){.**
46a60 20 20 20 20 20 53 6f 6d 65 53 74 72 75 63 74 75       SomeStructu
46a70 72 65 20 2a 70 44 61 74 61 20 3d 20 73 71 6c 69  re *pData = sqli
46a80 74 65 48 61 73 68 44 61 74 61 28 70 29 3b 0a 2a  teHashData(p);.*
46a90 2a 20 20 20 20 20 2f 2f 20 64 6f 20 73 6f 6d 65  *     // do some
46aa0 74 68 69 6e 67 20 77 69 74 68 20 70 44 61 74 61  thing with pData
46ab0 0a 2a 2a 20 20 20 7d 0a 2a 2f 0a 23 64 65 66 69  .**   }.*/.#defi
46ac0 6e 65 20 73 71 6c 69 74 65 48 61 73 68 46 69 72  ne sqliteHashFir
46ad0 73 74 28 48 29 20 20 28 28 48 29 2d 3e 66 69 72  st(H)  ((H)->fir
46ae0 73 74 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  st).#define sqli
46af0 74 65 48 61 73 68 4e 65 78 74 28 45 29 20 20 20  teHashNext(E)   
46b00 28 28 45 29 2d 3e 6e 65 78 74 29 0a 23 64 65 66  ((E)->next).#def
46b10 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 44 61  ine sqliteHashDa
46b20 74 61 28 45 29 20 20 20 28 28 45 29 2d 3e 64 61  ta(E)   ((E)->da
46b30 74 61 29 0a 2f 2a 20 23 64 65 66 69 6e 65 20 73  ta)./* #define s
46b40 71 6c 69 74 65 48 61 73 68 4b 65 79 28 45 29 20  qliteHashKey(E) 
46b50 20 20 20 28 28 45 29 2d 3e 70 4b 65 79 29 20 2f     ((E)->pKey) /
46b60 2f 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 2f 2a  / NOT USED */./*
46b70 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48   #define sqliteH
46b80 61 73 68 4b 65 79 73 69 7a 65 28 45 29 20 28 28  ashKeysize(E) ((
46b90 45 29 2d 3e 6e 4b 65 79 29 20 20 2f 2f 20 4e 4f  E)->nKey)  // NO
46ba0 54 20 55 53 45 44 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  T USED */../*.**
46bb0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
46bc0 65 73 20 69 6e 20 61 20 68 61 73 68 20 74 61 62  es in a hash tab
46bd0 6c 65 0a 2a 2f 0a 2f 2a 20 23 64 65 66 69 6e 65  le.*/./* #define
46be0 20 73 71 6c 69 74 65 48 61 73 68 43 6f 75 6e 74   sqliteHashCount
46bf0 28 48 29 20 20 28 28 48 29 2d 3e 63 6f 75 6e 74  (H)  ((H)->count
46c00 29 20 2f 2f 20 4e 4f 54 20 55 53 45 44 20 2a 2f  ) // NOT USED */
46c10 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51 4c  ..#endif /* _SQL
46c20 49 54 45 5f 48 41 53 48 5f 48 5f 20 2a 2f 0a 0a  ITE_HASH_H_ */..
46c30 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
46c40 45 6e 64 20 6f 66 20 68 61 73 68 2e 68 20 2a 2a  End of hash.h **
46c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
46c80 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
46c90 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
46ca0 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
46cb0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
46cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
46cd0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
46ce0 49 6e 63 6c 75 64 65 20 70 61 72 73 65 2e 68 20  Include parse.h 
46cf0 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
46d00 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
46d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
46d20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
46d30 42 65 67 69 6e 20 66 69 6c 65 20 70 61 72 73 65  Begin file parse
46d40 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
46d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
46d70 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 4d 49 20  #define TK_SEMI 
46d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46d90 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65             1.#de
46da0 66 69 6e 65 20 54 4b 5f 45 58 50 4c 41 49 4e 20  fine TK_EXPLAIN 
46db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46dc0 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e          2.#defin
46dd0 65 20 54 4b 5f 51 55 45 52 59 20 20 20 20 20 20  e TK_QUERY      
46de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46df0 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 54       3.#define T
46e00 4b 5f 50 4c 41 4e 20 20 20 20 20 20 20 20 20 20  K_PLAN          
46e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46e20 20 20 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42    4.#define TK_B
46e30 45 47 49 4e 20 20 20 20 20 20 20 20 20 20 20 20  EGIN            
46e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
46e50 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 52 41 4e  .#define TK_TRAN
46e60 53 41 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20  SACTION         
46e70 20 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 64              6.#d
46e80 65 66 69 6e 65 20 54 4b 5f 44 45 46 45 52 52 45  efine TK_DEFERRE
46e90 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  D               
46ea0 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 69           7.#defi
46eb0 6e 65 20 54 4b 5f 49 4d 4d 45 44 49 41 54 45 20  ne TK_IMMEDIATE 
46ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46ed0 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20        8.#define 
46ee0 54 4b 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20  TK_EXCLUSIVE    
46ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46f00 20 20 20 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f     9.#define TK_
46f10 43 4f 4d 4d 49 54 20 20 20 20 20 20 20 20 20 20  COMMIT          
46f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
46f30 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4e 44  0.#define TK_END
46f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46f50 20 20 20 20 20 20 20 20 20 20 20 20 31 31 0a 23              11.#
46f60 64 65 66 69 6e 65 20 54 4b 5f 52 4f 4c 4c 42 41  define TK_ROLLBA
46f70 43 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  CK              
46f80 20 20 20 20 20 20 20 20 20 31 32 0a 23 64 65 66           12.#def
46f90 69 6e 65 20 54 4b 5f 53 41 56 45 50 4f 49 4e 54  ine TK_SAVEPOINT
46fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46fb0 20 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e 65        13.#define
46fc0 20 54 4b 5f 52 45 4c 45 41 53 45 20 20 20 20 20   TK_RELEASE     
46fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46fe0 20 20 20 31 34 0a 23 64 65 66 69 6e 65 20 54 4b     14.#define TK
46ff0 5f 54 4f 20 20 20 20 20 20 20 20 20 20 20 20 20  _TO             
47000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47010 31 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 41  15.#define TK_TA
47020 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20  BLE             
47030 20 20 20 20 20 20 20 20 20 20 20 20 20 31 36 0a               16.
47040 23 64 65 66 69 6e 65 20 54 4b 5f 43 52 45 41 54  #define TK_CREAT
47050 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
47060 20 20 20 20 20 20 20 20 20 20 31 37 0a 23 64 65            17.#de
47070 66 69 6e 65 20 54 4b 5f 49 46 20 20 20 20 20 20  fine TK_IF      
47080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47090 20 20 20 20 20 20 20 31 38 0a 23 64 65 66 69 6e         18.#defin
470a0 65 20 54 4b 5f 4e 4f 54 20 20 20 20 20 20 20 20  e TK_NOT        
470b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
470c0 20 20 20 20 31 39 0a 23 64 65 66 69 6e 65 20 54      19.#define T
470d0 4b 5f 45 58 49 53 54 53 20 20 20 20 20 20 20 20  K_EXISTS        
470e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
470f0 20 32 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54   20.#define TK_T
47100 45 4d 50 20 20 20 20 20 20 20 20 20 20 20 20 20  EMP             
47110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 31                21
47120 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 50 20 20  .#define TK_LP  
47130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47140 20 20 20 20 20 20 20 20 20 20 20 32 32 0a 23 64             22.#d
47150 65 66 69 6e 65 20 54 4b 5f 52 50 20 20 20 20 20  efine TK_RP     
47160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47170 20 20 20 20 20 20 20 20 32 33 0a 23 64 65 66 69          23.#defi
47180 6e 65 20 54 4b 5f 41 53 20 20 20 20 20 20 20 20  ne TK_AS        
47190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
471a0 20 20 20 20 20 32 34 0a 23 64 65 66 69 6e 65 20       24.#define 
471b0 54 4b 5f 43 4f 4d 4d 41 20 20 20 20 20 20 20 20  TK_COMMA        
471c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
471d0 20 20 32 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f    25.#define TK_
471e0 49 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ID              
471f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
47200 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 44  6.#define TK_IND
47210 45 58 45 44 20 20 20 20 20 20 20 20 20 20 20 20  EXED            
47220 20 20 20 20 20 20 20 20 20 20 20 20 32 37 0a 23              27.#
47230 64 65 66 69 6e 65 20 54 4b 5f 41 42 4f 52 54 20  define TK_ABORT 
47240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47250 20 20 20 20 20 20 20 20 20 32 38 0a 23 64 65 66           28.#def
47260 69 6e 65 20 54 4b 5f 41 46 54 45 52 20 20 20 20  ine TK_AFTER    
47270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47280 20 20 20 20 20 20 32 39 0a 23 64 65 66 69 6e 65        29.#define
47290 20 54 4b 5f 41 4e 41 4c 59 5a 45 20 20 20 20 20   TK_ANALYZE     
472a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
472b0 20 20 20 33 30 0a 23 64 65 66 69 6e 65 20 54 4b     30.#define TK
472c0 5f 41 53 43 20 20 20 20 20 20 20 20 20 20 20 20  _ASC            
472d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
472e0 33 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 54  31.#define TK_AT
472f0 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20  TACH            
47300 20 20 20 20 20 20 20 20 20 20 20 20 20 33 32 0a               32.
47310 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 46 4f 52  #define TK_BEFOR
47320 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
47330 20 20 20 20 20 20 20 20 20 20 33 33 0a 23 64 65            33.#de
47340 66 69 6e 65 20 54 4b 5f 42 59 20 20 20 20 20 20  fine TK_BY      
47350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47360 20 20 20 20 20 20 20 33 34 0a 23 64 65 66 69 6e         34.#defin
47370 65 20 54 4b 5f 43 41 53 43 41 44 45 20 20 20 20  e TK_CASCADE    
47380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47390 20 20 20 20 33 35 0a 23 64 65 66 69 6e 65 20 54      35.#define T
473a0 4b 5f 43 41 53 54 20 20 20 20 20 20 20 20 20 20  K_CAST          
473b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
473c0 20 33 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43   36.#define TK_C
473d0 4f 4c 55 4d 4e 4b 57 20 20 20 20 20 20 20 20 20  OLUMNKW         
473e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 37                37
473f0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 46  .#define TK_CONF
47400 4c 49 43 54 20 20 20 20 20 20 20 20 20 20 20 20  LICT            
47410 20 20 20 20 20 20 20 20 20 20 20 33 38 0a 23 64             38.#d
47420 65 66 69 6e 65 20 54 4b 5f 44 41 54 41 42 41 53  efine TK_DATABAS
47430 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
47440 20 20 20 20 20 20 20 20 33 39 0a 23 64 65 66 69          39.#defi
47450 6e 65 20 54 4b 5f 44 45 53 43 20 20 20 20 20 20  ne TK_DESC      
47460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47470 20 20 20 20 20 34 30 0a 23 64 65 66 69 6e 65 20       40.#define 
47480 54 4b 5f 44 45 54 41 43 48 20 20 20 20 20 20 20  TK_DETACH       
47490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
474a0 20 20 34 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f    41.#define TK_
474b0 45 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20  EACH            
474c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
474d0 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 41 49  2.#define TK_FAI
474e0 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  L               
474f0 20 20 20 20 20 20 20 20 20 20 20 20 34 33 0a 23              43.#
47500 64 65 66 69 6e 65 20 54 4b 5f 46 4f 52 20 20 20  define TK_FOR   
47510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47520 20 20 20 20 20 20 20 20 20 34 34 0a 23 64 65 66           44.#def
47530 69 6e 65 20 54 4b 5f 49 47 4e 4f 52 45 20 20 20  ine TK_IGNORE   
47540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47550 20 20 20 20 20 20 34 35 0a 23 64 65 66 69 6e 65        45.#define
47560 20 54 4b 5f 49 4e 49 54 49 41 4c 4c 59 20 20 20   TK_INITIALLY   
47570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47580 20 20 20 34 36 0a 23 64 65 66 69 6e 65 20 54 4b     46.#define TK
47590 5f 49 4e 53 54 45 41 44 20 20 20 20 20 20 20 20  _INSTEAD        
475a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
475b0 34 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 49  47.#define TK_LI
475c0 4b 45 5f 4b 57 20 20 20 20 20 20 20 20 20 20 20  KE_KW           
475d0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 38 0a               48.
475e0 23 64 65 66 69 6e 65 20 54 4b 5f 4d 41 54 43 48  #define TK_MATCH
475f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47600 20 20 20 20 20 20 20 20 20 20 34 39 0a 23 64 65            49.#de
47610 66 69 6e 65 20 54 4b 5f 4b 45 59 20 20 20 20 20  fine TK_KEY     
47620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47630 20 20 20 20 20 20 20 35 30 0a 23 64 65 66 69 6e         50.#defin
47640 65 20 54 4b 5f 4f 46 20 20 20 20 20 20 20 20 20  e TK_OF         
47650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47660 20 20 20 20 35 31 0a 23 64 65 66 69 6e 65 20 54      51.#define T
47670 4b 5f 4f 46 46 53 45 54 20 20 20 20 20 20 20 20  K_OFFSET        
47680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47690 20 35 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50   52.#define TK_P
476a0 52 41 47 4d 41 20 20 20 20 20 20 20 20 20 20 20  RAGMA           
476b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 33                53
476c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 41 49 53  .#define TK_RAIS
476d0 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
476e0 20 20 20 20 20 20 20 20 20 20 20 35 34 0a 23 64             54.#d
476f0 65 66 69 6e 65 20 54 4b 5f 52 45 50 4c 41 43 45  efine TK_REPLACE
47700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47710 20 20 20 20 20 20 20 20 35 35 0a 23 64 65 66 69          55.#defi
47720 6e 65 20 54 4b 5f 52 45 53 54 52 49 43 54 20 20  ne TK_RESTRICT  
47730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47740 20 20 20 20 20 35 36 0a 23 64 65 66 69 6e 65 20       56.#define 
47750 54 4b 5f 52 4f 57 20 20 20 20 20 20 20 20 20 20  TK_ROW          
47760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47770 20 20 35 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f    57.#define TK_
47780 54 52 49 47 47 45 52 20 20 20 20 20 20 20 20 20  TRIGGER         
47790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
477a0 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 43  8.#define TK_VAC
477b0 55 55 4d 20 20 20 20 20 20 20 20 20 20 20 20 20  UUM             
477c0 20 20 20 20 20 20 20 20 20 20 20 20 35 39 0a 23              59.#
477d0 64 65 66 69 6e 65 20 54 4b 5f 56 49 45 57 20 20  define TK_VIEW  
477e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
477f0 20 20 20 20 20 20 20 20 20 36 30 0a 23 64 65 66           60.#def
47800 69 6e 65 20 54 4b 5f 56 49 52 54 55 41 4c 20 20  ine TK_VIRTUAL  
47810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47820 20 20 20 20 20 20 36 31 0a 23 64 65 66 69 6e 65        61.#define
47830 20 54 4b 5f 52 45 49 4e 44 45 58 20 20 20 20 20   TK_REINDEX     
47840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47850 20 20 20 36 32 0a 23 64 65 66 69 6e 65 20 54 4b     62.#define TK
47860 5f 52 45 4e 41 4d 45 20 20 20 20 20 20 20 20 20  _RENAME         
47870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47880 36 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 54  63.#define TK_CT
47890 49 4d 45 5f 4b 57 20 20 20 20 20 20 20 20 20 20  IME_KW          
478a0 20 20 20 20 20 20 20 20 20 20 20 20 20 36 34 0a               64.
478b0 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e 59 20 20  #define TK_ANY  
478c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
478d0 20 20 20 20 20 20 20 20 20 20 36 35 0a 23 64 65            65.#de
478e0 66 69 6e 65 20 54 4b 5f 4f 52 20 20 20 20 20 20  fine TK_OR      
478f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47900 20 20 20 20 20 20 20 36 36 0a 23 64 65 66 69 6e         66.#defin
47910 65 20 54 4b 5f 41 4e 44 20 20 20 20 20 20 20 20  e TK_AND        
47920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47930 20 20 20 20 36 37 0a 23 64 65 66 69 6e 65 20 54      67.#define T
47940 4b 5f 49 53 20 20 20 20 20 20 20 20 20 20 20 20  K_IS            
47950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47960 20 36 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42   68.#define TK_B
47970 45 54 57 45 45 4e 20 20 20 20 20 20 20 20 20 20  ETWEEN          
47980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 39                69
47990 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 20 20  .#define TK_IN  
479a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
479b0 20 20 20 20 20 20 20 20 20 20 20 37 30 0a 23 64             70.#d
479c0 65 66 69 6e 65 20 54 4b 5f 49 53 4e 55 4c 4c 20  efine TK_ISNULL 
479d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
479e0 20 20 20 20 20 20 20 20 37 31 0a 23 64 65 66 69          71.#defi
479f0 6e 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 20  ne TK_NOTNULL   
47a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a10 20 20 20 20 20 37 32 0a 23 64 65 66 69 6e 65 20       72.#define 
47a20 54 4b 5f 4e 45 20 20 20 20 20 20 20 20 20 20 20  TK_NE           
47a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a40 20 20 37 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f    73.#define TK_
47a50 45 51 20 20 20 20 20 20 20 20 20 20 20 20 20 20  EQ              
47a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
47a70 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 54 20  4.#define TK_GT 
47a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a90 20 20 20 20 20 20 20 20 20 20 20 20 37 35 0a 23              75.#
47aa0 64 65 66 69 6e 65 20 54 4b 5f 4c 45 20 20 20 20  define TK_LE    
47ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ac0 20 20 20 20 20 20 20 20 20 37 36 0a 23 64 65 66           76.#def
47ad0 69 6e 65 20 54 4b 5f 4c 54 20 20 20 20 20 20 20  ine TK_LT       
47ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47af0 20 20 20 20 20 20 37 37 0a 23 64 65 66 69 6e 65        77.#define
47b00 20 54 4b 5f 47 45 20 20 20 20 20 20 20 20 20 20   TK_GE          
47b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47b20 20 20 20 37 38 0a 23 64 65 66 69 6e 65 20 54 4b     78.#define TK
47b30 5f 45 53 43 41 50 45 20 20 20 20 20 20 20 20 20  _ESCAPE         
47b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47b50 37 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 49  79.#define TK_BI
47b60 54 41 4e 44 20 20 20 20 20 20 20 20 20 20 20 20  TAND            
47b70 20 20 20 20 20 20 20 20 20 20 20 20 20 38 30 0a               80.
47b80 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 4f 52  #define TK_BITOR
47b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ba0 20 20 20 20 20 20 20 20 20 20 38 31 0a 23 64 65            81.#de
47bb0 66 69 6e 65 20 54 4b 5f 4c 53 48 49 46 54 20 20  fine TK_LSHIFT  
47bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47bd0 20 20 20 20 20 20 20 38 32 0a 23 64 65 66 69 6e         82.#defin
47be0 65 20 54 4b 5f 52 53 48 49 46 54 20 20 20 20 20  e TK_RSHIFT     
47bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c00 20 20 20 20 38 33 0a 23 64 65 66 69 6e 65 20 54      83.#define T
47c10 4b 5f 50 4c 55 53 20 20 20 20 20 20 20 20 20 20  K_PLUS          
47c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c30 20 38 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4d   84.#define TK_M
47c40 49 4e 55 53 20 20 20 20 20 20 20 20 20 20 20 20  INUS            
47c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 35                85
47c60 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 54 41 52  .#define TK_STAR
47c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c80 20 20 20 20 20 20 20 20 20 20 20 38 36 0a 23 64             86.#d
47c90 65 66 69 6e 65 20 54 4b 5f 53 4c 41 53 48 20 20  efine TK_SLASH  
47ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47cb0 20 20 20 20 20 20 20 20 38 37 0a 23 64 65 66 69          87.#defi
47cc0 6e 65 20 54 4b 5f 52 45 4d 20 20 20 20 20 20 20  ne TK_REM       
47cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ce0 20 20 20 20 20 38 38 0a 23 64 65 66 69 6e 65 20       88.#define 
47cf0 54 4b 5f 43 4f 4e 43 41 54 20 20 20 20 20 20 20  TK_CONCAT       
47d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47d10 20 20 38 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f    89.#define TK_
47d20 43 4f 4c 4c 41 54 45 20 20 20 20 20 20 20 20 20  COLLATE         
47d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
47d40 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4d 49  0.#define TK_UMI
47d50 4e 55 53 20 20 20 20 20 20 20 20 20 20 20 20 20  NUS             
47d60 20 20 20 20 20 20 20 20 20 20 20 20 39 31 0a 23              91.#
47d70 64 65 66 69 6e 65 20 54 4b 5f 55 50 4c 55 53 20  define TK_UPLUS 
47d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47d90 20 20 20 20 20 20 20 20 20 39 32 0a 23 64 65 66           92.#def
47da0 69 6e 65 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20  ine TK_BITNOT   
47db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47dc0 20 20 20 20 20 20 39 33 0a 23 64 65 66 69 6e 65        93.#define
47dd0 20 54 4b 5f 53 54 52 49 4e 47 20 20 20 20 20 20   TK_STRING      
47de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47df0 20 20 20 39 34 0a 23 64 65 66 69 6e 65 20 54 4b     94.#define TK
47e00 5f 4a 4f 49 4e 5f 4b 57 20 20 20 20 20 20 20 20  _JOIN_KW        
47e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e20 39 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f  95.#define TK_CO
47e30 4e 53 54 52 41 49 4e 54 20 20 20 20 20 20 20 20  NSTRAINT        
47e40 20 20 20 20 20 20 20 20 20 20 20 20 20 39 36 0a               96.
47e50 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 41 55  #define TK_DEFAU
47e60 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LT              
47e70 20 20 20 20 20 20 20 20 20 20 39 37 0a 23 64 65            97.#de
47e80 66 69 6e 65 20 54 4b 5f 4e 55 4c 4c 20 20 20 20  fine TK_NULL    
47e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ea0 20 20 20 20 20 20 20 39 38 0a 23 64 65 66 69 6e         98.#defin
47eb0 65 20 54 4b 5f 50 52 49 4d 41 52 59 20 20 20 20  e TK_PRIMARY    
47ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ed0 20 20 20 20 39 39 0a 23 64 65 66 69 6e 65 20 54      99.#define T
47ee0 4b 5f 55 4e 49 51 55 45 20 20 20 20 20 20 20 20  K_UNIQUE        
47ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f00 20 31 30 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f   100.#define TK_
47f10 43 48 45 43 4b 20 20 20 20 20 20 20 20 20 20 20  CHECK           
47f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
47f30 30 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45  01.#define TK_RE
47f40 46 45 52 45 4e 43 45 53 20 20 20 20 20 20 20 20  FERENCES        
47f50 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 32               102
47f60 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 55 54 4f  .#define TK_AUTO
47f70 49 4e 43 52 20 20 20 20 20 20 20 20 20 20 20 20  INCR            
47f80 20 20 20 20 20 20 20 20 20 20 20 31 30 33 0a 23             103.#
47f90 64 65 66 69 6e 65 20 54 4b 5f 4f 4e 20 20 20 20  define TK_ON    
47fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47fb0 20 20 20 20 20 20 20 20 20 31 30 34 0a 23 64 65           104.#de
47fc0 66 69 6e 65 20 54 4b 5f 44 45 4c 45 54 45 20 20  fine TK_DELETE  
47fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47fe0 20 20 20 20 20 20 20 31 30 35 0a 23 64 65 66 69         105.#defi
47ff0 6e 65 20 54 4b 5f 55 50 44 41 54 45 20 20 20 20  ne TK_UPDATE    
48000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48010 20 20 20 20 20 31 30 36 0a 23 64 65 66 69 6e 65       106.#define
48020 20 54 4b 5f 49 4e 53 45 52 54 20 20 20 20 20 20   TK_INSERT      
48030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48040 20 20 20 31 30 37 0a 23 64 65 66 69 6e 65 20 54     107.#define T
48050 4b 5f 53 45 54 20 20 20 20 20 20 20 20 20 20 20  K_SET           
48060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48070 20 31 30 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f   108.#define TK_
48080 44 45 46 45 52 52 41 42 4c 45 20 20 20 20 20 20  DEFERRABLE      
48090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
480a0 30 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 4f  09.#define TK_FO
480b0 52 45 49 47 4e 20 20 20 20 20 20 20 20 20 20 20  REIGN           
480c0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 30               110
480d0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 52 4f 50  .#define TK_DROP
480e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
480f0 20 20 20 20 20 20 20 20 20 20 20 31 31 31 0a 23             111.#
48100 64 65 66 69 6e 65 20 54 4b 5f 55 4e 49 4f 4e 20  define TK_UNION 
48110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48120 20 20 20 20 20 20 20 20 20 31 31 32 0a 23 64 65           112.#de
48130 66 69 6e 65 20 54 4b 5f 41 4c 4c 20 20 20 20 20  fine TK_ALL     
48140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48150 20 20 20 20 20 20 20 31 31 33 0a 23 64 65 66 69         113.#defi
48160 6e 65 20 54 4b 5f 45 58 43 45 50 54 20 20 20 20  ne TK_EXCEPT    
48170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48180 20 20 20 20 20 31 31 34 0a 23 64 65 66 69 6e 65       114.#define
48190 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 20 20   TK_INTERSECT   
481a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
481b0 20 20 20 31 31 35 0a 23 64 65 66 69 6e 65 20 54     115.#define T
481c0 4b 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 20  K_SELECT        
481d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
481e0 20 31 31 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f   116.#define TK_
481f0 44 49 53 54 49 4e 43 54 20 20 20 20 20 20 20 20  DISTINCT        
48200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48210 31 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 4f  17.#define TK_DO
48220 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
48230 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 38               118
48240 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 52 4f 4d  .#define TK_FROM
48250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48260 20 20 20 20 20 20 20 20 20 20 20 31 31 39 0a 23             119.#
48270 64 65 66 69 6e 65 20 54 4b 5f 4a 4f 49 4e 20 20  define TK_JOIN  
48280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48290 20 20 20 20 20 20 20 20 20 31 32 30 0a 23 64 65           120.#de
482a0 66 69 6e 65 20 54 4b 5f 55 53 49 4e 47 20 20 20  fine TK_USING   
482b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
482c0 20 20 20 20 20 20 20 31 32 31 0a 23 64 65 66 69         121.#defi
482d0 6e 65 20 54 4b 5f 4f 52 44 45 52 20 20 20 20 20  ne TK_ORDER     
482e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
482f0 20 20 20 20 20 31 32 32 0a 23 64 65 66 69 6e 65       122.#define
48300 20 54 4b 5f 47 52 4f 55 50 20 20 20 20 20 20 20   TK_GROUP       
48310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48320 20 20 20 31 32 33 0a 23 64 65 66 69 6e 65 20 54     123.#define T
48330 4b 5f 48 41 56 49 4e 47 20 20 20 20 20 20 20 20  K_HAVING        
48340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48350 20 31 32 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f   124.#define TK_
48360 4c 49 4d 49 54 20 20 20 20 20 20 20 20 20 20 20  LIMIT           
48370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48380 32 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 57 48  25.#define TK_WH
48390 45 52 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ERE             
483a0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 36               126
483b0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 54 4f  .#define TK_INTO
483c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
483d0 20 20 20 20 20 20 20 20 20 20 20 31 32 37 0a 23             127.#
483e0 64 65 66 69 6e 65 20 54 4b 5f 56 41 4c 55 45 53  define TK_VALUES
483f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48400 20 20 20 20 20 20 20 20 20 31 32 38 0a 23 64 65           128.#de
48410 66 69 6e 65 20 54 4b 5f 49 4e 54 45 47 45 52 20  fine TK_INTEGER 
48420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48430 20 20 20 20 20 20 20 31 32 39 0a 23 64 65 66 69         129.#defi
48440 6e 65 20 54 4b 5f 46 4c 4f 41 54 20 20 20 20 20  ne TK_FLOAT     
48450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48460 20 20 20 20 20 31 33 30 0a 23 64 65 66 69 6e 65       130.#define
48470 20 54 4b 5f 42 4c 4f 42 20 20 20 20 20 20 20 20   TK_BLOB        
48480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48490 20 20 20 31 33 31 0a 23 64 65 66 69 6e 65 20 54     131.#define T
484a0 4b 5f 52 45 47 49 53 54 45 52 20 20 20 20 20 20  K_REGISTER      
484b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
484c0 20 31 33 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f   132.#define TK_
484d0 56 41 52 49 41 42 4c 45 20 20 20 20 20 20 20 20  VARIABLE        
484e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
484f0 33 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 41  33.#define TK_CA
48500 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  SE              
48510 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 34               134
48520 0a 23 64 65 66 69 6e 65 20 54 4b 5f 57 48 45 4e  .#define TK_WHEN
48530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48540 20 20 20 20 20 20 20 20 20 20 20 31 33 35 0a 23             135.#
48550 64 65 66 69 6e 65 20 54 4b 5f 54 48 45 4e 20 20  define TK_THEN  
48560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48570 20 20 20 20 20 20 20 20 20 31 33 36 0a 23 64 65           136.#de
48580 66 69 6e 65 20 54 4b 5f 45 4c 53 45 20 20 20 20  fine TK_ELSE    
48590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
485a0 20 20 20 20 20 20 20 31 33 37 0a 23 64 65 66 69         137.#defi
485b0 6e 65 20 54 4b 5f 49 4e 44 45 58 20 20 20 20 20  ne TK_INDEX     
485c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
485d0 20 20 20 20 20 31 33 38 0a 23 64 65 66 69 6e 65       138.#define
485e0 20 54 4b 5f 41 4c 54 45 52 20 20 20 20 20 20 20   TK_ALTER       
485f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48600 20 20 20 31 33 39 0a 23 64 65 66 69 6e 65 20 54     139.#define T
48610 4b 5f 41 44 44 20 20 20 20 20 20 20 20 20 20 20  K_ADD           
48620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48630 20 31 34 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f   140.#define TK_
48640 54 4f 5f 54 45 58 54 20 20 20 20 20 20 20 20 20  TO_TEXT         
48650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48660 34 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f  41.#define TK_TO
48670 5f 42 4c 4f 42 20 20 20 20 20 20 20 20 20 20 20  _BLOB           
48680 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 32               142
48690 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 4e  .#define TK_TO_N
486a0 55 4d 45 52 49 43 20 20 20 20 20 20 20 20 20 20  UMERIC          
486b0 20 20 20 20 20 20 20 20 20 20 20 31 34 33 0a 23             143.#
486c0 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 49 4e 54  define TK_TO_INT
486d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
486e0 20 20 20 20 20 20 20 20 20 31 34 34 0a 23 64 65           144.#de
486f0 66 69 6e 65 20 54 4b 5f 54 4f 5f 52 45 41 4c 20  fine TK_TO_REAL 
48700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48710 20 20 20 20 20 20 20 31 34 35 0a 23 64 65 66 69         145.#defi
48720 6e 65 20 54 4b 5f 45 4e 44 5f 4f 46 5f 46 49 4c  ne TK_END_OF_FIL
48730 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
48740 20 20 20 20 20 31 34 36 0a 23 64 65 66 69 6e 65       146.#define
48750 20 54 4b 5f 49 4c 4c 45 47 41 4c 20 20 20 20 20   TK_ILLEGAL     
48760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48770 20 20 20 31 34 37 0a 23 64 65 66 69 6e 65 20 54     147.#define T
48780 4b 5f 53 50 41 43 45 20 20 20 20 20 20 20 20 20  K_SPACE         
48790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
487a0 20 31 34 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f   148.#define TK_
487b0 55 4e 43 4c 4f 53 45 44 5f 53 54 52 49 4e 47 20  UNCLOSED_STRING 
487c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
487d0 34 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 55  49.#define TK_FU
487e0 4e 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20  NCTION          
487f0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 30               150
48800 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 55  .#define TK_COLU
48810 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  MN              
48820 20 20 20 20 20 20 20 20 20 20 20 31 35 31 0a 23             151.#
48830 64 65 66 69 6e 65 20 54 4b 5f 41 47 47 5f 46 55  define TK_AGG_FU
48840 4e 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20  NCTION          
48850 20 20 20 20 20 20 20 20 20 31 35 32 0a 23 64 65           152.#de
48860 66 69 6e 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55  fine TK_AGG_COLU
48870 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  MN              
48880 20 20 20 20 20 20 20 31 35 33 0a 23 64 65 66 69         153.#defi
48890 6e 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43  ne TK_CONST_FUNC
488a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
488b0 20 20 20 20 20 31 35 34 0a 0a 2f 2a 2a 2a 2a 2a       154../*****
488c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
488d0 20 70 61 72 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a   parse.h *******
488e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
488f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48900 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
48910 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
48920 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
48930 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65  ft off in sqlite
48940 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
48950 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 6e 63 6c 75  ********/.#inclu
48960 64 65 20 3c 73 74 64 69 6f 2e 68 3e 0a 23 69 6e  de <stdio.h>.#in
48970 63 6c 75 64 65 20 3c 73 74 64 6c 69 62 2e 68 3e  clude <stdlib.h>
48980 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e  .#include <strin
48990 67 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 61  g.h>.#include <a
489a0 73 73 65 72 74 2e 68 3e 0a 23 69 6e 63 6c 75 64  ssert.h>.#includ
489b0 65 20 3c 73 74 64 64 65 66 2e 68 3e 0a 0a 2f 2a  e <stddef.h>../*
489c0 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 69 6e 67  .** If compiling
489d0 20 66 6f 72 20 61 20 70 72 6f 63 65 73 73 6f 72   for a processor
489e0 20 74 68 61 74 20 6c 61 63 6b 73 20 66 6c 6f 61   that lacks floa
489f0 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 6f  ting point suppo
48a00 72 74 2c 0a 2a 2a 20 73 75 62 73 74 69 74 75 74  rt,.** substitut
48a10 65 20 69 6e 74 65 67 65 72 20 66 6f 72 20 66 6c  e integer for fl
48a20 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 0a 2a 2f 0a  oating-point.*/.
48a30 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
48a40 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
48a50 54 0a 23 20 64 65 66 69 6e 65 20 64 6f 75 62 6c  T.# define doubl
48a60 65 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 0a 23  e sqlite_int64.#
48a70 20 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42   define LONGDOUB
48a80 4c 45 5f 54 59 50 45 20 73 71 6c 69 74 65 5f 69  LE_TYPE sqlite_i
48a90 6e 74 36 34 0a 23 20 69 66 6e 64 65 66 20 53 51  nt64.# ifndef SQ
48aa0 4c 49 54 45 5f 42 49 47 5f 44 42 4c 0a 23 20 20  LITE_BIG_DBL.#  
48ab0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42   define SQLITE_B
48ac0 49 47 5f 44 42 4c 20 28 28 28 73 71 6c 69 74 65  IG_DBL (((sqlite
48ad0 33 5f 69 6e 74 36 34 29 31 29 3c 3c 35 30 29 0a  3_int64)1)<<50).
48ae0 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65  # endif.# define
48af0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54   SQLITE_OMIT_DAT
48b00 45 54 49 4d 45 5f 46 55 4e 43 53 20 31 0a 23 20  ETIME_FUNCS 1.# 
48b10 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4d  define SQLITE_OM
48b20 49 54 5f 54 52 41 43 45 20 31 0a 23 20 75 6e 64  IT_TRACE 1.# und
48b30 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f  ef SQLITE_MIXED_
48b40 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f  ENDIAN_64BIT_FLO
48b50 41 54 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54  AT.# undef SQLIT
48b60 45 5f 48 41 56 45 5f 49 53 4e 41 4e 0a 23 65 6e  E_HAVE_ISNAN.#en
48b70 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  dif.#ifndef SQLI
48b80 54 45 5f 42 49 47 5f 44 42 4c 0a 23 20 64 65 66  TE_BIG_DBL.# def
48b90 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44  ine SQLITE_BIG_D
48ba0 42 4c 20 28 31 65 39 39 29 0a 23 65 6e 64 69 66  BL (1e99).#endif
48bb0 0a 0a 2f 2a 0a 2a 2a 20 4f 4d 49 54 5f 54 45 4d  ../*.** OMIT_TEM
48bc0 50 44 42 20 69 73 20 73 65 74 20 74 6f 20 31 20  PDB is set to 1 
48bd0 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  if SQLITE_OMIT_T
48be0 45 4d 50 44 42 20 69 73 20 64 65 66 69 6e 65 64  EMPDB is defined
48bf0 2c 20 6f 72 20 30 0a 2a 2a 20 61 66 74 65 72 77  , or 0.** afterw
48c00 61 72 64 2e 20 48 61 76 69 6e 67 20 74 68 69 73  ard. Having this
48c10 20 6d 61 63 72 6f 20 61 6c 6c 6f 77 73 20 75 73   macro allows us
48c20 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 43 20   to cause the C 
48c30 63 6f 6d 70 69 6c 65 72 20 0a 2a 2a 20 74 6f 20  compiler .** to 
48c40 6f 6d 69 74 20 63 6f 64 65 20 75 73 65 64 20 62  omit code used b
48c50 79 20 54 45 4d 50 20 74 61 62 6c 65 73 20 77 69  y TEMP tables wi
48c60 74 68 6f 75 74 20 6d 65 73 73 79 20 23 69 66 6e  thout messy #ifn
48c70 64 65 66 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  def statements..
48c80 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
48c90 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 23 64 65  _OMIT_TEMPDB.#de
48ca0 66 69 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 44 42  fine OMIT_TEMPDB
48cb0 20 31 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65   1.#else.#define
48cc0 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 30 0a 23   OMIT_TEMPDB 0.#
48cd0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  endif../*.** If 
48ce0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61  the following ma
48cf0 63 72 6f 20 69 73 20 73 65 74 20 74 6f 20 31 2c  cro is set to 1,
48d00 20 74 68 65 6e 20 4e 55 4c 4c 20 76 61 6c 75 65   then NULL value
48d10 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64  s are considered
48d20 0a 2a 2a 20 64 69 73 74 69 6e 63 74 20 77 68 65  .** distinct whe
48d30 6e 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 77 68  n determining wh
48d40 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 77 6f  ether or not two
48d50 20 65 6e 74 72 69 65 73 20 61 72 65 20 74 68 65   entries are the
48d60 20 73 61 6d 65 0a 2a 2a 20 69 6e 20 61 20 55 4e   same.** in a UN
48d70 49 51 55 45 20 69 6e 64 65 78 2e 20 20 54 68 69  IQUE index.  Thi
48d80 73 20 69 73 20 74 68 65 20 77 61 79 20 50 6f 73  s is the way Pos
48d90 74 67 72 65 53 51 4c 2c 20 4f 72 61 63 6c 65 2c  tgreSQL, Oracle,
48da0 20 44 42 32 2c 20 4d 79 53 51 4c 2c 0a 2a 2a 20   DB2, MySQL,.** 
48db0 4f 43 45 4c 4f 54 2c 20 61 6e 64 20 46 69 72 65  OCELOT, and Fire
48dc0 62 69 72 64 20 61 6c 6c 20 77 6f 72 6b 2e 20 20  bird all work.  
48dd0 54 68 65 20 53 51 4c 39 32 20 73 70 65 63 20 65  The SQL92 spec e
48de0 78 70 6c 69 63 69 74 6c 79 20 73 61 79 73 20 74  xplicitly says t
48df0 68 69 73 0a 2a 2a 20 69 73 20 74 68 65 20 77 61  his.** is the wa
48e00 79 20 74 68 69 6e 67 73 20 61 72 65 20 73 75 70  y things are sup
48e10 70 6f 73 65 20 74 6f 20 77 6f 72 6b 2e 0a 2a 2a  pose to work..**
48e20 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f  .** If the follo
48e30 77 69 6e 67 20 6d 61 63 72 6f 20 69 73 20 73 65  wing macro is se
48e40 74 20 74 6f 20 30 2c 20 74 68 65 20 4e 55 4c 4c  t to 0, the NULL
48e50 73 20 61 72 65 20 69 6e 64 69 73 74 69 6e 63 74  s are indistinct
48e60 20 66 6f 72 0a 2a 2a 20 61 20 55 4e 49 51 55 45   for.** a UNIQUE
48e70 20 69 6e 64 65 78 2e 20 20 49 6e 20 74 68 69 73   index.  In this
48e80 20 6d 6f 64 65 2c 20 79 6f 75 20 63 61 6e 20 6f   mode, you can o
48e90 6e 6c 79 20 68 61 76 65 20 61 20 73 69 6e 67 6c  nly have a singl
48ea0 65 20 4e 55 4c 4c 20 65 6e 74 72 79 0a 2a 2a 20  e NULL entry.** 
48eb0 66 6f 72 20 61 20 63 6f 6c 75 6d 6e 20 64 65 63  for a column dec
48ec0 6c 61 72 65 64 20 55 4e 49 51 55 45 2e 20 20 54  lared UNIQUE.  T
48ed0 68 69 73 20 69 73 20 74 68 65 20 77 61 79 20 49  his is the way I
48ee0 6e 66 6f 72 6d 69 78 20 61 6e 64 20 53 51 4c 20  nformix and SQL 
48ef0 53 65 72 76 65 72 0a 2a 2a 20 77 6f 72 6b 2e 0a  Server.** work..
48f00 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 55 4c 4c 5f  */.#define NULL_
48f10 44 49 53 54 49 4e 43 54 5f 46 4f 52 5f 55 4e 49  DISTINCT_FOR_UNI
48f20 51 55 45 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  QUE 1../*.** The
48f30 20 22 66 69 6c 65 20 66 6f 72 6d 61 74 22 20 6e   "file format" n
48f40 75 6d 62 65 72 20 69 73 20 61 6e 20 69 6e 74 65  umber is an inte
48f50 67 65 72 20 74 68 61 74 20 69 73 20 69 6e 63 72  ger that is incr
48f60 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72  emented whenever
48f70 0a 2a 2a 20 74 68 65 20 56 44 42 45 2d 6c 65 76  .** the VDBE-lev
48f80 65 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 63  el file format c
48f90 68 61 6e 67 65 73 2e 20 20 54 68 65 20 66 6f 6c  hanges.  The fol
48fa0 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 64 65  lowing macros de
48fb0 66 69 6e 65 20 74 68 65 0a 2a 2a 20 74 68 65 20  fine the.** the 
48fc0 64 65 66 61 75 6c 74 20 66 69 6c 65 20 66 6f 72  default file for
48fd0 6d 61 74 20 66 6f 72 20 6e 65 77 20 64 61 74 61  mat for new data
48fe0 62 61 73 65 73 20 61 6e 64 20 74 68 65 20 6d 61  bases and the ma
48ff0 78 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61  ximum file forma
49000 74 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 6c 69  t.** that the li
49010 62 72 61 72 79 20 63 61 6e 20 72 65 61 64 2e 0a  brary can read..
49020 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
49030 45 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41  E_MAX_FILE_FORMA
49040 54 20 34 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  T 4.#ifndef SQLI
49050 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f  TE_DEFAULT_FILE_
49060 46 4f 52 4d 41 54 0a 23 20 64 65 66 69 6e 65 20  FORMAT.# define 
49070 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46  SQLITE_DEFAULT_F
49080 49 4c 45 5f 46 4f 52 4d 41 54 20 31 0a 23 65 6e  ILE_FORMAT 1.#en
49090 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
490a0 49 54 45 5f 44 45 46 41 55 4c 54 5f 52 45 43 55  ITE_DEFAULT_RECU
490b0 52 53 49 56 45 5f 54 52 49 47 47 45 52 53 0a 23  RSIVE_TRIGGERS.#
490c0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44   define SQLITE_D
490d0 45 46 41 55 4c 54 5f 52 45 43 55 52 53 49 56 45  EFAULT_RECURSIVE
490e0 5f 54 52 49 47 47 45 52 53 20 30 0a 23 65 6e 64  _TRIGGERS 0.#end
490f0 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 76 69 64  if../*.** Provid
49100 65 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75  e a default valu
49110 65 20 66 6f 72 20 53 51 4c 49 54 45 5f 54 45 4d  e for SQLITE_TEM
49120 50 5f 53 54 4f 52 45 20 69 6e 20 63 61 73 65 20  P_STORE in case 
49130 69 74 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66  it is not specif
49140 69 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f  ied.** on the co
49150 6d 6d 61 6e 64 2d 6c 69 6e 65 0a 2a 2f 0a 23 69  mmand-line.*/.#i
49160 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d  fndef SQLITE_TEM
49170 50 5f 53 54 4f 52 45 0a 23 20 64 65 66 69 6e 65  P_STORE.# define
49180 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f   SQLITE_TEMP_STO
49190 52 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  RE 1.#endif../*.
491a0 2a 2a 20 47 43 43 20 64 6f 65 73 20 6e 6f 74 20  ** GCC does not 
491b0 64 65 66 69 6e 65 20 74 68 65 20 6f 66 66 73 65  define the offse
491c0 74 6f 66 28 29 20 6d 61 63 72 6f 20 73 6f 20 77  tof() macro so w
491d0 65 27 6c 6c 20 68 61 76 65 20 74 6f 20 64 6f 20  e'll have to do 
491e0 69 74 0a 2a 2a 20 6f 75 72 73 65 6c 76 65 73 2e  it.** ourselves.
491f0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 6f 66 66 73  .*/.#ifndef offs
49200 65 74 6f 66 0a 23 64 65 66 69 6e 65 20 6f 66 66  etof.#define off
49210 73 65 74 6f 66 28 53 54 52 55 43 54 55 52 45 2c  setof(STRUCTURE,
49220 46 49 45 4c 44 29 20 28 28 69 6e 74 29 28 28 63  FIELD) ((int)((c
49230 68 61 72 2a 29 26 28 28 53 54 52 55 43 54 55 52  har*)&((STRUCTUR
49240 45 2a 29 30 29 2d 3e 46 49 45 4c 44 29 29 0a 23  E*)0)->FIELD)).#
49250 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65  endif../*.** Che
49260 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 69  ck to see if thi
49270 73 20 6d 61 63 68 69 6e 65 20 75 73 65 73 20 45  s machine uses E
49280 42 43 44 49 43 2e 20 20 28 59 65 73 2c 20 62 65  BCDIC.  (Yes, be
49290 6c 69 65 76 65 20 69 74 20 6f 72 0a 2a 2a 20 6e  lieve it or.** n
492a0 6f 74 2c 20 74 68 65 72 65 20 61 72 65 20 73 74  ot, there are st
492b0 69 6c 6c 20 6d 61 63 68 69 6e 65 73 20 6f 75 74  ill machines out
492c0 20 74 68 65 72 65 20 74 68 61 74 20 75 73 65 20   there that use 
492d0 45 42 43 44 49 43 2e 29 0a 2a 2f 0a 23 69 66 20  EBCDIC.).*/.#if 
492e0 27 41 27 20 3d 3d 20 27 5c 33 30 31 27 0a 23 20  'A' == '\301'.# 
492f0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 42  define SQLITE_EB
49300 43 44 49 43 20 31 0a 23 65 6c 73 65 0a 23 20 64  CDIC 1.#else.# d
49310 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 53 43  efine SQLITE_ASC
49320 49 49 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  II 1.#endif../*.
49330 2a 2a 20 49 6e 74 65 67 65 72 73 20 6f 66 20 6b  ** Integers of k
49340 6e 6f 77 6e 20 73 69 7a 65 73 2e 20 20 54 68 65  nown sizes.  The
49350 73 65 20 74 79 70 65 64 65 66 73 20 6d 69 67 68  se typedefs migh
49360 74 20 63 68 61 6e 67 65 20 66 6f 72 20 61 72 63  t change for arc
49370 68 69 74 65 63 74 75 72 65 73 0a 2a 2a 20 77 68  hitectures.** wh
49380 65 72 65 20 74 68 65 20 73 69 7a 65 73 20 76 65  ere the sizes ve
49390 72 79 2e 20 20 50 72 65 70 72 6f 63 65 73 73 6f  ry.  Preprocesso
493a0 72 20 6d 61 63 72 6f 73 20 61 72 65 20 61 76 61  r macros are ava
493b0 69 6c 61 62 6c 65 20 73 6f 20 74 68 61 74 20 74  ilable so that t
493c0 68 65 0a 2a 2a 20 74 79 70 65 73 20 63 61 6e 20  he.** types can 
493d0 62 65 20 63 6f 6e 76 65 6e 69 65 6e 74 6c 79 20  be conveniently 
493e0 72 65 64 65 66 69 6e 65 64 20 61 74 20 63 6f 6d  redefined at com
493f0 70 69 6c 65 2d 74 79 70 65 2e 20 20 4c 69 6b 65  pile-type.  Like
49400 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   this:.**.**    
49410 20 20 20 20 20 63 63 20 27 2d 44 55 49 4e 54 50       cc '-DUINTP
49420 54 52 5f 54 59 50 45 3d 6c 6f 6e 67 20 6c 6f 6e  TR_TYPE=long lon
49430 67 20 69 6e 74 27 20 2e 2e 2e 0a 2a 2f 0a 23 69  g int' ....*/.#i
49440 66 6e 64 65 66 20 55 49 4e 54 33 32 5f 54 59 50  fndef UINT32_TYP
49450 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 55  E.# ifdef HAVE_U
49460 49 4e 54 33 32 5f 54 0a 23 20 20 64 65 66 69 6e  INT32_T.#  defin
49470 65 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 69  e UINT32_TYPE ui
49480 6e 74 33 32 5f 74 0a 23 20 65 6c 73 65 0a 23 20  nt32_t.# else.# 
49490 20 64 65 66 69 6e 65 20 55 49 4e 54 33 32 5f 54   define UINT32_T
494a0 59 50 45 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  YPE unsigned int
494b0 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  .# endif.#endif.
494c0 23 69 66 6e 64 65 66 20 55 49 4e 54 31 36 5f 54  #ifndef UINT16_T
494d0 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45  YPE.# ifdef HAVE
494e0 5f 55 49 4e 54 31 36 5f 54 0a 23 20 20 64 65 66  _UINT16_T.#  def
494f0 69 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45 20  ine UINT16_TYPE 
49500 75 69 6e 74 31 36 5f 74 0a 23 20 65 6c 73 65 0a  uint16_t.# else.
49510 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 31 36  #  define UINT16
49520 5f 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 73  _TYPE unsigned s
49530 68 6f 72 74 20 69 6e 74 0a 23 20 65 6e 64 69 66  hort int.# endif
49540 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
49550 49 4e 54 31 36 5f 54 59 50 45 0a 23 20 69 66 64  INT16_TYPE.# ifd
49560 65 66 20 48 41 56 45 5f 49 4e 54 31 36 5f 54 0a  ef HAVE_INT16_T.
49570 23 20 20 64 65 66 69 6e 65 20 49 4e 54 31 36 5f  #  define INT16_
49580 54 59 50 45 20 69 6e 74 31 36 5f 74 0a 23 20 65  TYPE int16_t.# e
49590 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49 4e  lse.#  define IN
495a0 54 31 36 5f 54 59 50 45 20 73 68 6f 72 74 20 69  T16_TYPE short i
495b0 6e 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69  nt.# endif.#endi
495c0 66 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 38 5f  f.#ifndef UINT8_
495d0 54 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56  TYPE.# ifdef HAV
495e0 45 5f 55 49 4e 54 38 5f 54 0a 23 20 20 64 65 66  E_UINT8_T.#  def
495f0 69 6e 65 20 55 49 4e 54 38 5f 54 59 50 45 20 75  ine UINT8_TYPE u
49600 69 6e 74 38 5f 74 0a 23 20 65 6c 73 65 0a 23 20  int8_t.# else.# 
49610 20 64 65 66 69 6e 65 20 55 49 4e 54 38 5f 54 59   define UINT8_TY
49620 50 45 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  PE unsigned char
49630 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  .# endif.#endif.
49640 23 69 66 6e 64 65 66 20 49 4e 54 38 5f 54 59 50  #ifndef INT8_TYP
49650 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 49  E.# ifdef HAVE_I
49660 4e 54 38 5f 54 0a 23 20 20 64 65 66 69 6e 65 20  NT8_T.#  define 
49670 49 4e 54 38 5f 54 59 50 45 20 69 6e 74 38 5f 74  INT8_TYPE int8_t
49680 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e  .# else.#  defin
49690 65 20 49 4e 54 38 5f 54 59 50 45 20 73 69 67 6e  e INT8_TYPE sign
496a0 65 64 20 63 68 61 72 0a 23 20 65 6e 64 69 66 0a  ed char.# endif.
496b0 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4c  #endif.#ifndef L
496c0 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 0a 23  ONGDOUBLE_TYPE.#
496d0 20 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42   define LONGDOUB
496e0 4c 45 5f 54 59 50 45 20 6c 6f 6e 67 20 64 6f 75  LE_TYPE long dou
496f0 62 6c 65 0a 23 65 6e 64 69 66 0a 74 79 70 65 64  ble.#endif.typed
49700 65 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  ef sqlite_int64 
49710 69 36 34 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  i64;          /*
49720 20 38 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69   8-byte signed i
49730 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65  nteger */.typede
49740 66 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  f sqlite_uint64 
49750 75 36 34 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  u64;         /* 
49760 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20  8-byte unsigned 
49770 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64  integer */.typed
49780 65 66 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75  ef UINT32_TYPE u
49790 33 32 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  32;           /*
497a0 20 34 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64   4-byte unsigned
497b0 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65   integer */.type
497c0 64 65 66 20 55 49 4e 54 31 36 5f 54 59 50 45 20  def UINT16_TYPE 
497d0 75 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 2f  u16;           /
497e0 2a 20 32 2d 62 79 74 65 20 75 6e 73 69 67 6e 65  * 2-byte unsigne
497f0 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70  d integer */.typ
49800 65 64 65 66 20 49 4e 54 31 36 5f 54 59 50 45 20  edef INT16_TYPE 
49810 69 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20  i16;            
49820 2f 2a 20 32 2d 62 79 74 65 20 73 69 67 6e 65 64  /* 2-byte signed
49830 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65   integer */.type
49840 64 65 66 20 55 49 4e 54 38 5f 54 59 50 45 20 75  def UINT8_TYPE u
49850 38 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  8;             /
49860 2a 20 31 2d 62 79 74 65 20 75 6e 73 69 67 6e 65  * 1-byte unsigne
49870 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70  d integer */.typ
49880 65 64 65 66 20 49 4e 54 38 5f 54 59 50 45 20 69  edef INT8_TYPE i
49890 38 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  8;              
498a0 2f 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64  /* 1-byte signed
498b0 20 69 6e 74 65 67 65 72 20 2a 2f 0a 0a 2f 2a 0a   integer */../*.
498c0 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33  ** SQLITE_MAX_U3
498d0 32 20 69 73 20 61 20 75 36 34 20 63 6f 6e 73 74  2 is a u64 const
498e0 61 6e 74 20 74 68 61 74 20 69 73 20 74 68 65 20  ant that is the 
498f0 6d 61 78 69 6d 75 6d 20 75 36 34 20 76 61 6c 75  maximum u64 valu
49900 65 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65  e.** that can be
49910 20 73 74 6f 72 65 64 20 69 6e 20 61 20 75 33 32   stored in a u32
49920 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66   without loss of
49930 20 64 61 74 61 2e 20 20 54 68 65 20 76 61 6c 75   data.  The valu
49940 65 0a 2a 2a 20 69 73 20 30 78 30 30 30 30 30 30  e.** is 0x000000
49950 30 30 66 66 66 66 66 66 66 66 2e 20 20 42 75 74  00ffffffff.  But
49960 20 62 65 63 61 75 73 65 20 6f 66 20 71 75 69 72   because of quir
49970 6b 73 20 6f 66 20 73 6f 6d 65 20 63 6f 6d 70 69  ks of some compi
49980 6c 65 72 73 2c 20 77 65 0a 2a 2a 20 68 61 76 65  lers, we.** have
49990 20 74 6f 20 73 70 65 63 69 66 79 20 74 68 65 20   to specify the 
499a0 76 61 6c 75 65 20 69 6e 20 74 68 65 20 6c 65 73  value in the les
499b0 73 20 69 6e 74 75 69 74 69 76 65 20 6d 61 6e 6e  s intuitive mann
499c0 65 72 20 73 68 6f 77 6e 3a 0a 2a 2f 0a 23 64 65  er shown:.*/.#de
499d0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  fine SQLITE_MAX_
499e0 55 33 32 20 20 28 28 28 28 75 36 34 29 31 29 3c  U32  ((((u64)1)<
499f0 3c 33 32 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 4d  <32)-1)../*.** M
49a00 61 63 72 6f 73 20 74 6f 20 64 65 74 65 72 6d 69  acros to determi
49a10 6e 65 20 77 68 65 74 68 65 72 20 74 68 65 20 6d  ne whether the m
49a20 61 63 68 69 6e 65 20 69 73 20 62 69 67 20 6f 72  achine is big or
49a30 20 6c 69 74 74 6c 65 20 65 6e 64 69 61 6e 2c 0a   little endian,.
49a40 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 61 74 20  ** evaluated at 
49a50 72 75 6e 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64  runtime..*/.#ifd
49a60 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41  ef SQLITE_AMALGA
49a70 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52  MATION.SQLITE_PR
49a80 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20  IVATE const int 
49a90 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20 31 3b 0a  sqlite3one = 1;.
49aa0 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49  #else.SQLITE_PRI
49ab0 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73  VATE const int s
49ac0 71 6c 69 74 65 33 6f 6e 65 3b 0a 23 65 6e 64 69  qlite3one;.#endi
49ad0 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28 69 33  f.#if defined(i3
49ae0 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  86) || defined(_
49af0 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69  _i386__) || defi
49b00 6e 65 64 28 5f 4d 5f 49 58 38 36 29 5c 0a 20 20  ned(_M_IX86)\.  
49b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49b20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 64 65             || de
49b30 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 29 20  fined(__x86_64) 
49b40 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36  || defined(__x86
49b50 5f 36 34 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20  _64__).# define 
49b60 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e  SQLITE_BIGENDIAN
49b70 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 53      0.# define S
49b80 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49  QLITE_LITTLEENDI
49b90 41 4e 20 31 0a 23 20 64 65 66 69 6e 65 20 53 51  AN 1.# define SQ
49ba0 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
49bb0 20 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45    SQLITE_UTF16LE
49bc0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
49bd0 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e  SQLITE_BIGENDIAN
49be0 20 20 20 20 28 2a 28 63 68 61 72 20 2a 29 28 26      (*(char *)(&
49bf0 73 71 6c 69 74 65 33 6f 6e 65 29 3d 3d 30 29 0a  sqlite3one)==0).
49c00 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
49c10 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 28 2a 28  LITTLEENDIAN (*(
49c20 63 68 61 72 20 2a 29 28 26 73 71 6c 69 74 65 33  char *)(&sqlite3
49c30 6f 6e 65 29 3d 3d 31 29 0a 23 20 64 65 66 69 6e  one)==1).# defin
49c40 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  e SQLITE_UTF16NA
49c50 54 49 56 45 20 28 53 51 4c 49 54 45 5f 42 49 47  TIVE (SQLITE_BIG
49c60 45 4e 44 49 41 4e 3f 53 51 4c 49 54 45 5f 55 54  ENDIAN?SQLITE_UT
49c70 46 31 36 42 45 3a 53 51 4c 49 54 45 5f 55 54 46  F16BE:SQLITE_UTF
49c80 31 36 4c 45 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  16LE).#endif../*
49c90 0a 2a 2a 20 43 6f 6e 73 74 61 6e 74 73 20 66 6f  .** Constants fo
49ca0 72 20 74 68 65 20 6c 61 72 67 65 73 74 20 61 6e  r the largest an
49cb0 64 20 73 6d 61 6c 6c 65 73 74 20 70 6f 73 73 69  d smallest possi
49cc0 62 6c 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65  ble 64-bit signe
49cd0 64 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 54  d integers..** T
49ce0 68 65 73 65 20 6d 61 63 72 6f 73 20 61 72 65 20  hese macros are 
49cf0 64 65 73 69 67 6e 65 64 20 74 6f 20 77 6f 72 6b  designed to work
49d00 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 62 6f   correctly on bo
49d10 74 68 20 33 32 2d 62 69 74 20 61 6e 64 20 36 34  th 32-bit and 64
49d20 2d 62 69 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72  -bit.** compiler
49d30 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 41  s..*/.#define LA
49d40 52 47 45 53 54 5f 49 4e 54 36 34 20 20 28 30 78  RGEST_INT64  (0x
49d50 66 66 66 66 66 66 66 66 7c 28 28 28 69 36 34 29  ffffffff|(((i64)
49d60 30 78 37 66 66 66 66 66 66 66 29 3c 3c 33 32 29  0x7fffffff)<<32)
49d70 29 0a 23 64 65 66 69 6e 65 20 53 4d 41 4c 4c 45  ).#define SMALLE
49d80 53 54 5f 49 4e 54 36 34 20 28 28 28 69 36 34 29  ST_INT64 (((i64)
49d90 2d 31 29 20 2d 20 4c 41 52 47 45 53 54 5f 49 4e  -1) - LARGEST_IN
49da0 54 36 34 29 0a 0a 2f 2a 20 0a 2a 2a 20 52 6f 75  T64)../* .** Rou
49db0 6e 64 20 75 70 20 61 20 6e 75 6d 62 65 72 20 74  nd up a number t
49dc0 6f 20 74 68 65 20 6e 65 78 74 20 6c 61 72 67 65  o the next large
49dd0 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 2e  r multiple of 8.
49de0 20 20 54 68 69 73 20 69 73 20 75 73 65 64 0a 2a    This is used.*
49df0 2a 20 74 6f 20 66 6f 72 63 65 20 38 2d 62 79 74  * to force 8-byt
49e00 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 6e 20 36  e alignment on 6
49e10 34 2d 62 69 74 20 61 72 63 68 69 74 65 63 74 75  4-bit architectu
49e20 72 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  res..*/.#define 
49e30 52 4f 55 4e 44 38 28 78 29 20 20 20 20 20 28 28  ROUND8(x)     ((
49e40 28 78 29 2b 37 29 26 7e 37 29 0a 0a 2f 2a 0a 2a  (x)+7)&~7)../*.*
49e50 2a 20 52 6f 75 6e 64 20 64 6f 77 6e 20 74 6f 20  * Round down to 
49e60 74 68 65 20 6e 65 61 72 65 73 74 20 6d 75 6c 74  the nearest mult
49e70 69 70 6c 65 20 6f 66 20 38 0a 2a 2f 0a 23 64 65  iple of 8.*/.#de
49e80 66 69 6e 65 20 52 4f 55 4e 44 44 4f 57 4e 38 28  fine ROUNDDOWN8(
49e90 78 29 20 28 28 78 29 26 7e 37 29 0a 0a 2f 2a 0a  x) ((x)&~7)../*.
49ea0 2a 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74  ** Assert that t
49eb0 68 65 20 70 6f 69 6e 74 65 72 20 58 20 69 73 20  he pointer X is 
49ec0 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d  aligned to an 8-
49ed0 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 0a 2a  byte boundary..*
49ee0 2f 0a 23 64 65 66 69 6e 65 20 45 49 47 48 54 5f  /.#define EIGHT_
49ef0 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 58  BYTE_ALIGNMENT(X
49f00 29 20 20 20 28 28 28 28 63 68 61 72 2a 29 28 58  )   ((((char*)(X
49f10 29 20 2d 20 28 63 68 61 72 2a 29 30 29 26 37 29  ) - (char*)0)&7)
49f20 3d 3d 30 29 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  ==0).../*.** An 
49f30 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
49f40 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
49f50 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 73  ure is used to s
49f60 74 6f 72 65 20 74 68 65 20 62 75 73 79 2d 68 61  tore the busy-ha
49f70 6e 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 63  ndler.** callbac
49f80 6b 20 66 6f 72 20 61 20 67 69 76 65 6e 20 73 71  k for a given sq
49f90 6c 69 74 65 20 68 61 6e 64 6c 65 2e 20 0a 2a 2a  lite handle. .**
49fa0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 62  .** The sqlite.b
49fb0 75 73 79 48 61 6e 64 6c 65 72 20 6d 65 6d 62 65  usyHandler membe
49fc0 72 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 20  r of the sqlite 
49fd0 73 74 72 75 63 74 20 63 6f 6e 74 61 69 6e 73 20  struct contains 
49fe0 74 68 65 20 62 75 73 79 0a 2a 2a 20 63 61 6c 6c  the busy.** call
49ff0 62 61 63 6b 20 66 6f 72 20 74 68 65 20 64 61 74  back for the dat
4a000 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 45 61  abase handle. Ea
4a010 63 68 20 70 61 67 65 72 20 6f 70 65 6e 65 64 20  ch pager opened 
4a020 76 69 61 20 74 68 65 20 73 71 6c 69 74 65 0a 2a  via the sqlite.*
4a030 2a 20 68 61 6e 64 6c 65 20 69 73 20 70 61 73 73  * handle is pass
4a040 65 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ed a pointer to 
4a050 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e 64 6c  sqlite.busyHandl
4a060 65 72 2e 20 54 68 65 20 62 75 73 79 2d 68 61 6e  er. The busy-han
4a070 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b  dler.** callback
4a080 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e   is currently in
4a090 76 6f 6b 65 64 20 6f 6e 6c 79 20 66 72 6f 6d 20  voked only from 
4a0a0 77 69 74 68 69 6e 20 70 61 67 65 72 2e 63 2e 0a  within pager.c..
4a0b0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
4a0c0 74 20 42 75 73 79 48 61 6e 64 6c 65 72 20 42 75  t BusyHandler Bu
4a0d0 73 79 48 61 6e 64 6c 65 72 3b 0a 73 74 72 75 63  syHandler;.struc
4a0e0 74 20 42 75 73 79 48 61 6e 64 6c 65 72 20 7b 0a  t BusyHandler {.
4a0f0 20 20 69 6e 74 20 28 2a 78 46 75 6e 63 29 28 76    int (*xFunc)(v
4a100 6f 69 64 20 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20  oid *,int);  /* 
4a110 54 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63  The busy callbac
4a120 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72  k */.  void *pAr
4a130 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
4a140 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74    /* First arg t
4a150 6f 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20  o busy callback 
4a160 2a 2f 0a 20 20 69 6e 74 20 6e 42 75 73 79 3b 20  */.  int nBusy; 
4a170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a180 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 65 64 20 77  /* Incremented w
4a190 69 74 68 20 65 61 63 68 20 62 75 73 79 20 63 61  ith each busy ca
4a1a0 6c 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ll */.};../*.** 
4a1b0 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 61 73 74  Name of the mast
4a1c0 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62 6c  er database tabl
4a1d0 65 2e 20 20 54 68 65 20 6d 61 73 74 65 72 20 64  e.  The master d
4a1e0 61 74 61 62 61 73 65 20 74 61 62 6c 65 0a 2a 2a  atabase table.**
4a1f0 20 69 73 20 61 20 73 70 65 63 69 61 6c 20 74 61   is a special ta
4a200 62 6c 65 20 74 68 61 74 20 68 6f 6c 64 73 20 74  ble that holds t
4a210 68 65 20 6e 61 6d 65 73 20 61 6e 64 20 61 74 74  he names and att
4a220 72 69 62 75 74 65 73 20 6f 66 20 61 6c 6c 0a 2a  ributes of all.*
4a230 2a 20 75 73 65 72 20 74 61 62 6c 65 73 20 61 6e  * user tables an
4a240 64 20 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a 23 64  d indices..*/.#d
4a250 65 66 69 6e 65 20 4d 41 53 54 45 52 5f 4e 41 4d  efine MASTER_NAM
4a260 45 20 20 20 20 20 20 20 22 73 71 6c 69 74 65 5f  E       "sqlite_
4a270 6d 61 73 74 65 72 22 0a 23 64 65 66 69 6e 65 20  master".#define 
4a280 54 45 4d 50 5f 4d 41 53 54 45 52 5f 4e 41 4d 45  TEMP_MASTER_NAME
4a290 20 20 22 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d    "sqlite_temp_m
4a2a0 61 73 74 65 72 22 0a 0a 2f 2a 0a 2a 2a 20 54 68  aster"../*.** Th
4a2b0 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 20 74  e root-page of t
4a2c0 68 65 20 6d 61 73 74 65 72 20 64 61 74 61 62 61  he master databa
4a2d0 73 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65  se table..*/.#de
4a2e0 66 69 6e 65 20 4d 41 53 54 45 52 5f 52 4f 4f 54  fine MASTER_ROOT
4a2f0 20 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20         1../*.** 
4a300 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  The name of the 
4a310 73 63 68 65 6d 61 20 74 61 62 6c 65 2e 0a 2a 2f  schema table..*/
4a320 0a 23 64 65 66 69 6e 65 20 53 43 48 45 4d 41 5f  .#define SCHEMA_
4a330 54 41 42 4c 45 28 78 29 20 20 28 28 21 4f 4d 49  TABLE(x)  ((!OMI
4a340 54 5f 54 45 4d 50 44 42 29 26 26 28 78 3d 3d 31  T_TEMPDB)&&(x==1
4a350 29 3f 54 45 4d 50 5f 4d 41 53 54 45 52 5f 4e 41  )?TEMP_MASTER_NA
4a360 4d 45 3a 4d 41 53 54 45 52 5f 4e 41 4d 45 29 0a  ME:MASTER_NAME).
4a370 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 6e 76 65 6e 69  ./*.** A conveni
4a380 65 6e 63 65 20 6d 61 63 72 6f 20 74 68 61 74 20  ence macro that 
4a390 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  returns the numb
4a3a0 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69  er of elements i
4a3b0 6e 0a 2a 2a 20 61 6e 20 61 72 72 61 79 2e 0a 2a  n.** an array..*
4a3c0 2f 0a 23 64 65 66 69 6e 65 20 41 72 72 61 79 53  /.#define ArrayS
4a3d0 69 7a 65 28 58 29 20 20 20 20 28 28 69 6e 74 29  ize(X)    ((int)
4a3e0 28 73 69 7a 65 6f 66 28 58 29 2f 73 69 7a 65 6f  (sizeof(X)/sizeo
4a3f0 66 28 58 5b 30 5d 29 29 29 0a 0a 2f 2a 0a 2a 2a  f(X[0])))../*.**
4a400 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76   The following v
4a410 61 6c 75 65 20 61 73 20 61 20 64 65 73 74 72 75  alue as a destru
4a420 63 74 6f 72 20 6d 65 61 6e 73 20 74 6f 20 75 73  ctor means to us
4a430 65 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28  e sqlite3DbFree(
4a440 29 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e  )..** This is an
4a450 20 69 6e 74 65 72 6e 61 6c 20 65 78 74 65 6e 73   internal extens
4a460 69 6f 6e 20 74 6f 20 53 51 4c 49 54 45 5f 53 54  ion to SQLITE_ST
4a470 41 54 49 43 20 61 6e 64 20 53 51 4c 49 54 45 5f  ATIC and SQLITE_
4a480 54 52 41 4e 53 49 45 4e 54 2e 0a 2a 2f 0a 23 64  TRANSIENT..*/.#d
4a490 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 59 4e  efine SQLITE_DYN
4a4a0 41 4d 49 43 20 20 20 28 28 73 71 6c 69 74 65 33  AMIC   ((sqlite3
4a4b0 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65  _destructor_type
4a4c0 29 73 71 6c 69 74 65 33 44 62 46 72 65 65 29 0a  )sqlite3DbFree).
4a4d0 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 49  ./*.** When SQLI
4a4e0 54 45 5f 4f 4d 49 54 5f 57 53 44 20 69 73 20 64  TE_OMIT_WSD is d
4a4f0 65 66 69 6e 65 64 2c 20 69 74 20 6d 65 61 6e 73  efined, it means
4a500 20 74 68 61 74 20 74 68 65 20 74 61 72 67 65 74   that the target
4a510 20 70 6c 61 74 66 6f 72 6d 20 64 6f 65 73 0a 2a   platform does.*
4a520 2a 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 57 72  * not support Wr
4a530 69 74 61 62 6c 65 20 53 74 61 74 69 63 20 44 61  itable Static Da
4a540 74 61 20 28 57 53 44 29 20 73 75 63 68 20 61 73  ta (WSD) such as
4a550 20 67 6c 6f 62 61 6c 20 61 6e 64 20 73 74 61 74   global and stat
4a560 69 63 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a  ic variables..**
4a570 20 41 6c 6c 20 76 61 72 69 61 62 6c 65 73 20 6d   All variables m
4a580 75 73 74 20 65 69 74 68 65 72 20 62 65 20 6f 6e  ust either be on
4a590 20 74 68 65 20 73 74 61 63 6b 20 6f 72 20 64 79   the stack or dy
4a5a0 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
4a5b0 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20  ted from.** the 
4a5c0 68 65 61 70 2e 20 20 57 68 65 6e 20 57 53 44 20  heap.  When WSD 
4a5d0 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 2c 20  is unsupported, 
4a5e0 74 68 65 20 76 61 72 69 61 62 6c 65 20 64 65 63  the variable dec
4a5f0 6c 61 72 61 74 69 6f 6e 73 20 73 63 61 74 74 65  larations scatte
4a600 72 65 64 0a 2a 2a 20 74 68 72 6f 75 67 68 6f 75  red.** throughou
4a610 74 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f 64  t the SQLite cod
4a620 65 20 6d 75 73 74 20 62 65 63 6f 6d 65 20 63 6f  e must become co
4a630 6e 73 74 61 6e 74 73 20 69 6e 73 74 65 61 64 2e  nstants instead.
4a640 20 20 54 68 65 20 53 51 4c 49 54 45 5f 57 53 44    The SQLITE_WSD
4a650 0a 2a 2a 20 6d 61 63 72 6f 20 69 73 20 75 73 65  .** macro is use
4a660 64 20 66 6f 72 20 74 68 69 73 20 70 75 72 70 6f  d for this purpo
4a670 73 65 2e 20 20 41 6e 64 20 69 6e 73 74 65 61 64  se.  And instead
4a680 20 6f 66 20 72 65 66 65 72 65 6e 63 69 6e 67 20   of referencing 
4a690 74 68 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20  the variable.** 
4a6a0 64 69 72 65 63 74 6c 79 2c 20 77 65 20 75 73 65  directly, we use
4a6b0 20 69 74 73 20 63 6f 6e 73 74 61 6e 74 20 61 73   its constant as
4a6c0 20 61 20 6b 65 79 20 74 6f 20 6c 6f 6f 6b 75 70   a key to lookup
4a6d0 20 74 68 65 20 72 75 6e 2d 74 69 6d 65 20 61 6c   the run-time al
4a6e0 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 75 66 66 65  located.** buffe
4a6f0 72 20 74 68 61 74 20 68 6f 6c 64 73 20 72 65 61  r that holds rea
4a700 6c 20 76 61 72 69 61 62 6c 65 2e 20 20 54 68 65  l variable.  The
4a710 20 63 6f 6e 73 74 61 6e 74 20 69 73 20 61 6c 73   constant is als
4a720 6f 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 65  o the initialize
4a730 72 0a 2a 2a 20 66 6f 72 20 74 68 65 20 72 75 6e  r.** for the run
4a740 2d 74 69 6d 65 20 61 6c 6c 6f 63 61 74 65 64 20  -time allocated 
4a750 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e  buffer..**.** In
4a760 20 74 68 65 20 75 73 75 61 6c 20 63 61 73 65 20   the usual case 
4a770 77 68 65 72 65 20 57 53 44 20 69 73 20 73 75 70  where WSD is sup
4a780 70 6f 72 74 65 64 2c 20 74 68 65 20 53 51 4c 49  ported, the SQLI
4a790 54 45 5f 57 53 44 20 61 6e 64 20 47 4c 4f 42 41  TE_WSD and GLOBA
4a7a0 4c 0a 2a 2a 20 6d 61 63 72 6f 73 20 62 65 63 6f  L.** macros beco
4a7b0 6d 65 20 6e 6f 2d 6f 70 73 20 61 6e 64 20 68 61  me no-ops and ha
4a7c0 76 65 20 7a 65 72 6f 20 70 65 72 66 6f 72 6d 61  ve zero performa
4a7d0 6e 63 65 20 69 6d 70 61 63 74 2e 0a 2a 2f 0a 23  nce impact..*/.#
4a7e0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
4a7f0 54 5f 57 53 44 0a 20 20 23 64 65 66 69 6e 65 20  T_WSD.  #define 
4a800 53 51 4c 49 54 45 5f 57 53 44 20 63 6f 6e 73 74  SQLITE_WSD const
4a810 0a 20 20 23 64 65 66 69 6e 65 20 47 4c 4f 42 41  .  #define GLOBA
4a820 4c 28 74 2c 76 29 20 28 2a 28 74 2a 29 73 71 6c  L(t,v) (*(t*)sql
4a830 69 74 65 33 5f 77 73 64 5f 66 69 6e 64 28 28 76  ite3_wsd_find((v
4a840 6f 69 64 2a 29 26 28 76 29 2c 20 73 69 7a 65 6f  oid*)&(v), sizeo
4a850 66 28 76 29 29 29 0a 20 20 23 64 65 66 69 6e 65  f(v))).  #define
4a860 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
4a870 6e 66 69 67 20 47 4c 4f 42 41 4c 28 73 74 72 75  nfig GLOBAL(stru
4a880 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67  ct Sqlite3Config
4a890 2c 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 29  , sqlite3Config)
4a8a0 0a 53 51 4c 49 54 45 5f 41 50 49 20 20 20 69 6e  .SQLITE_API   in
4a8b0 74 20 73 71 6c 69 74 65 33 5f 77 73 64 5f 69 6e  t sqlite3_wsd_in
4a8c0 69 74 28 69 6e 74 20 4e 2c 20 69 6e 74 20 4a 29  it(int N, int J)
4a8d0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 20 20 76  ;.SQLITE_API   v
4a8e0 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 77 73 64  oid *sqlite3_wsd
4a8f0 5f 66 69 6e 64 28 76 6f 69 64 20 2a 4b 2c 20 69  _find(void *K, i
4a900 6e 74 20 4c 29 3b 0a 23 65 6c 73 65 0a 20 20 23  nt L);.#else.  #
4a910 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 53  define SQLITE_WS
4a920 44 20 0a 20 20 23 64 65 66 69 6e 65 20 47 4c 4f  D .  #define GLO
4a930 42 41 4c 28 74 2c 76 29 20 76 0a 20 20 23 64 65  BAL(t,v) v.  #de
4a940 66 69 6e 65 20 73 71 6c 69 74 65 33 47 6c 6f 62  fine sqlite3Glob
4a950 61 6c 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33  alConfig sqlite3
4a960 43 6f 6e 66 69 67 0a 23 65 6e 64 69 66 0a 0a 2f  Config.#endif../
4a970 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
4a980 6e 67 20 6d 61 63 72 6f 73 20 61 72 65 20 75 73  ng macros are us
4a990 65 64 20 74 6f 20 73 75 70 70 72 65 73 73 20 63  ed to suppress c
4a9a0 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 73  ompiler warnings
4a9b0 20 61 6e 64 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20   and to.** make 
4a9c0 69 74 20 63 6c 65 61 72 20 74 6f 20 68 75 6d 61  it clear to huma
4a9d0 6e 20 72 65 61 64 65 72 73 20 77 68 65 6e 20 61  n readers when a
4a9e0 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d 65   function parame
4a9f0 74 65 72 20 69 73 20 64 65 6c 69 62 65 72 61 74  ter is deliberat
4aa00 65 6c 79 20 0a 2a 2a 20 6c 65 66 74 20 75 6e 75  ely .** left unu
4aa10 73 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 62  sed within the b
4aa20 6f 64 79 20 6f 66 20 61 20 66 75 6e 63 74 69 6f  ody of a functio
4aa30 6e 2e 20 54 68 69 73 20 75 73 75 61 6c 6c 79 20  n. This usually 
4aa40 68 61 70 70 65 6e 73 20 77 68 65 6e 0a 2a 2a 20  happens when.** 
4aa50 61 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  a function is ca
4aa60 6c 6c 65 64 20 76 69 61 20 61 20 66 75 6e 63 74  lled via a funct
4aa70 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 20 46 6f 72  ion pointer. For
4aa80 20 65 78 61 6d 70 6c 65 20 74 68 65 20 0a 2a 2a   example the .**
4aa90 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
4aaa0 6f 66 20 61 6e 20 53 51 4c 20 61 67 67 72 65 67  of an SQL aggreg
4aab0 61 74 65 20 73 74 65 70 20 63 61 6c 6c 62 61 63  ate step callbac
4aac0 6b 20 6d 61 79 20 6e 6f 74 20 75 73 65 20 74 68  k may not use th
4aad0 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69  e.** parameter i
4aae0 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 6e 75  ndicating the nu
4aaf0 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
4ab00 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20  s passed to the 
4ab10 61 67 67 72 65 67 61 74 65 2c 0a 2a 2a 20 69 66  aggregate,.** if
4ab20 20 69 74 20 6b 6e 6f 77 73 20 74 68 61 74 20 74   it knows that t
4ab30 68 69 73 20 69 73 20 65 6e 66 6f 72 63 65 64 20  his is enforced 
4ab40 65 6c 73 65 77 68 65 72 65 2e 0a 2a 2a 0a 2a 2a  elsewhere..**.**
4ab50 20 57 68 65 6e 20 61 20 66 75 6e 63 74 69 6f 6e   When a function
4ab60 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f   parameter is no
4ab70 74 20 75 73 65 64 20 61 74 20 61 6c 6c 20 77 69  t used at all wi
4ab80 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66  thin the body of
4ab90 20 61 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20   a function,.** 
4aba0 69 74 20 69 73 20 67 65 6e 65 72 61 6c 6c 79 20  it is generally 
4abb0 6e 61 6d 65 64 20 22 4e 6f 74 55 73 65 64 22 20  named "NotUsed" 
4abc0 6f 72 20 22 4e 6f 74 55 73 65 64 32 22 20 74 6f  or "NotUsed2" to
4abd0 20 6d 61 6b 65 20 74 68 69 6e 67 73 20 65 76 65   make things eve
4abe0 6e 20 63 6c 65 61 72 65 72 2e 0a 2a 2a 20 48 6f  n clearer..** Ho
4abf0 77 65 76 65 72 2c 20 74 68 65 73 65 20 6d 61 63  wever, these mac
4ac00 72 6f 73 20 6d 61 79 20 61 6c 73 6f 20 62 65 20  ros may also be 
4ac10 75 73 65 64 20 74 6f 20 73 75 70 70 72 65 73 73  used to suppress
4ac20 20 77 61 72 6e 69 6e 67 73 20 72 65 6c 61 74 65   warnings relate
4ac30 64 20 74 6f 0a 2a 2a 20 70 61 72 61 6d 65 74 65  d to.** paramete
4ac40 72 73 20 74 68 61 74 20 6d 61 79 20 6f 72 20 6d  rs that may or m
4ac50 61 79 20 6e 6f 74 20 62 65 20 75 73 65 64 20 64  ay not be used d
4ac60 65 70 65 6e 64 69 6e 67 20 6f 6e 20 63 6f 6d 70  epending on comp
4ac70 69 6c 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 2e  ilation options.
4ac80 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 20  .** For example 
4ac90 74 68 6f 73 65 20 70 61 72 61 6d 65 74 65 72 73  those parameters
4aca0 20 6f 6e 6c 79 20 75 73 65 64 20 69 6e 20 61 73   only used in as
4acb0 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
4acc0 73 2e 20 49 6e 20 74 68 65 73 65 0a 2a 2a 20 63  s. In these.** c
4acd0 61 73 65 73 20 74 68 65 20 70 61 72 61 6d 65 74  ases the paramet
4ace0 65 72 73 20 61 72 65 20 6e 61 6d 65 64 20 61 73  ers are named as
4acf0 20 70 65 72 20 74 68 65 20 75 73 75 61 6c 20 63   per the usual c
4ad00 6f 6e 76 65 6e 74 69 6f 6e 73 2e 0a 2a 2f 0a 23  onventions..*/.#
4ad10 64 65 66 69 6e 65 20 55 4e 55 53 45 44 5f 50 41  define UNUSED_PA
4ad20 52 41 4d 45 54 45 52 28 78 29 20 28 76 6f 69 64  RAMETER(x) (void
4ad30 29 28 78 29 0a 23 64 65 66 69 6e 65 20 55 4e 55  )(x).#define UNU
4ad40 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 78  SED_PARAMETER2(x
4ad50 2c 79 29 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ,y) UNUSED_PARAM
4ad60 45 54 45 52 28 78 29 2c 55 4e 55 53 45 44 5f 50  ETER(x),UNUSED_P
4ad70 41 52 41 4d 45 54 45 52 28 79 29 0a 0a 2f 2a 0a  ARAMETER(y)../*.
4ad80 2a 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72  ** Forward refer
4ad90 65 6e 63 65 73 20 74 6f 20 73 74 72 75 63 74 75  ences to structu
4ada0 72 65 73 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  res.*/.typedef s
4adb0 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 41 67  truct AggInfo Ag
4adc0 67 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73  gInfo;.typedef s
4add0 74 72 75 63 74 20 41 75 74 68 43 6f 6e 74 65 78  truct AuthContex
4ade0 74 20 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 74  t AuthContext;.t
4adf0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 41 75  ypedef struct Au
4ae00 74 6f 69 6e 63 49 6e 66 6f 20 41 75 74 6f 69 6e  toincInfo Autoin
4ae10 63 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73  cInfo;.typedef s
4ae20 74 72 75 63 74 20 42 69 74 76 65 63 20 42 69 74  truct Bitvec Bit
4ae30 76 65 63 3b 0a 74 79 70 65 64 65 66 20 73 74 72  vec;.typedef str
4ae40 75 63 74 20 52 6f 77 53 65 74 20 52 6f 77 53 65  uct RowSet RowSe
4ae50 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  t;.typedef struc
4ae60 74 20 43 6f 6c 6c 53 65 71 20 43 6f 6c 6c 53 65  t CollSeq CollSe
4ae70 71 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  q;.typedef struc
4ae80 74 20 43 6f 6c 75 6d 6e 20 43 6f 6c 75 6d 6e 3b  t Column Column;
4ae90 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4aea0 44 62 20 44 62 3b 0a 74 79 70 65 64 65 66 20 73  Db Db;.typedef s
4aeb0 74 72 75 63 74 20 53 63 68 65 6d 61 20 53 63 68  truct Schema Sch
4aec0 65 6d 61 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ema;.typedef str
4aed0 75 63 74 20 45 78 70 72 20 45 78 70 72 3b 0a 74  uct Expr Expr;.t
4aee0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 45 78  ypedef struct Ex
4aef0 70 72 4c 69 73 74 20 45 78 70 72 4c 69 73 74 3b  prList ExprList;
4af00 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4af10 45 78 70 72 53 70 61 6e 20 45 78 70 72 53 70 61  ExprSpan ExprSpa
4af20 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  n;.typedef struc
4af30 74 20 46 4b 65 79 20 46 4b 65 79 3b 0a 74 79 70  t FKey FKey;.typ
4af40 65 64 65 66 20 73 74 72 75 63 74 20 46 75 6e 63  edef struct Func
4af50 44 65 66 20 46 75 6e 63 44 65 66 3b 0a 74 79 70  Def FuncDef;.typ
4af60 65 64 65 66 20 73 74 72 75 63 74 20 46 75 6e 63  edef struct Func
4af70 44 65 66 48 61 73 68 20 46 75 6e 63 44 65 66 48  DefHash FuncDefH
4af80 61 73 68 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ash;.typedef str
4af90 75 63 74 20 49 64 4c 69 73 74 20 49 64 4c 69 73  uct IdList IdLis
4afa0 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  t;.typedef struc
4afb0 74 20 49 6e 64 65 78 20 49 6e 64 65 78 3b 0a 74  t Index Index;.t
4afc0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e  ypedef struct In
4afd0 64 65 78 53 61 6d 70 6c 65 20 49 6e 64 65 78 53  dexSample IndexS
4afe0 61 6d 70 6c 65 3b 0a 74 79 70 65 64 65 66 20 73  ample;.typedef s
4aff0 74 72 75 63 74 20 4b 65 79 43 6c 61 73 73 20 4b  truct KeyClass K
4b000 65 79 43 6c 61 73 73 3b 0a 74 79 70 65 64 65 66  eyClass;.typedef
4b010 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20   struct KeyInfo 
4b020 4b 65 79 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66  KeyInfo;.typedef
4b030 20 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64   struct Lookasid
4b040 65 20 4c 6f 6f 6b 61 73 69 64 65 3b 0a 74 79 70  e Lookaside;.typ
4b050 65 64 65 66 20 73 74 72 75 63 74 20 4c 6f 6f 6b  edef struct Look
4b060 61 73 69 64 65 53 6c 6f 74 20 4c 6f 6f 6b 61 73  asideSlot Lookas
4b070 69 64 65 53 6c 6f 74 3b 0a 74 79 70 65 64 65 66  ideSlot;.typedef
4b080 20 73 74 72 75 63 74 20 4d 6f 64 75 6c 65 20 4d   struct Module M
4b090 6f 64 75 6c 65 3b 0a 74 79 70 65 64 65 66 20 73  odule;.typedef s
4b0a0 74 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78  truct NameContex
4b0b0 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 3b 0a 74  t NameContext;.t
4b0c0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61  ypedef struct Pa
4b0d0 72 73 65 20 50 61 72 73 65 3b 0a 74 79 70 65 64  rse Parse;.typed
4b0e0 65 66 20 73 74 72 75 63 74 20 53 61 76 65 70 6f  ef struct Savepo
4b0f0 69 6e 74 20 53 61 76 65 70 6f 69 6e 74 3b 0a 74  int Savepoint;.t
4b100 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 65  ypedef struct Se
4b110 6c 65 63 74 20 53 65 6c 65 63 74 3b 0a 74 79 70  lect Select;.typ
4b120 65 64 65 66 20 73 74 72 75 63 74 20 53 72 63 4c  edef struct SrcL
4b130 69 73 74 20 53 72 63 4c 69 73 74 3b 0a 74 79 70  ist SrcList;.typ
4b140 65 64 65 66 20 73 74 72 75 63 74 20 53 74 72 41  edef struct StrA
4b150 63 63 75 6d 20 53 74 72 41 63 63 75 6d 3b 0a 74  ccum StrAccum;.t
4b160 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 61  ypedef struct Ta
4b170 62 6c 65 20 54 61 62 6c 65 3b 0a 74 79 70 65 64  ble Table;.typed
4b180 65 66 20 73 74 72 75 63 74 20 54 61 62 6c 65 4c  ef struct TableL
4b190 6f 63 6b 20 54 61 62 6c 65 4c 6f 63 6b 3b 0a 74  ock TableLock;.t
4b1a0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 6f  ypedef struct To
4b1b0 6b 65 6e 20 54 6f 6b 65 6e 3b 0a 74 79 70 65 64  ken Token;.typed
4b1c0 65 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65  ef struct Trigge
4b1d0 72 50 72 67 20 54 72 69 67 67 65 72 50 72 67 3b  rPrg TriggerPrg;
4b1e0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b1f0 54 72 69 67 67 65 72 53 74 65 70 20 54 72 69 67  TriggerStep Trig
4b200 67 65 72 53 74 65 70 3b 0a 74 79 70 65 64 65 66  gerStep;.typedef
4b210 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20   struct Trigger 
4b220 54 72 69 67 67 65 72 3b 0a 74 79 70 65 64 65 66  Trigger;.typedef
4b230 20 73 74 72 75 63 74 20 55 6e 70 61 63 6b 65 64   struct Unpacked
4b240 52 65 63 6f 72 64 20 55 6e 70 61 63 6b 65 64 52  Record UnpackedR
4b250 65 63 6f 72 64 3b 0a 74 79 70 65 64 65 66 20 73  ecord;.typedef s
4b260 74 72 75 63 74 20 56 54 61 62 6c 65 20 56 54 61  truct VTable VTa
4b270 62 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ble;.typedef str
4b280 75 63 74 20 57 61 6c 6b 65 72 20 57 61 6c 6b 65  uct Walker Walke
4b290 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  r;.typedef struc
4b2a0 74 20 57 68 65 72 65 50 6c 61 6e 20 57 68 65 72  t WherePlan Wher
4b2b0 65 50 6c 61 6e 3b 0a 74 79 70 65 64 65 66 20 73  ePlan;.typedef s
4b2c0 74 72 75 63 74 20 57 68 65 72 65 49 6e 66 6f 20  truct WhereInfo 
4b2d0 57 68 65 72 65 49 6e 66 6f 3b 0a 74 79 70 65 64  WhereInfo;.typed
4b2e0 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 4c  ef struct WhereL
4b2f0 65 76 65 6c 20 57 68 65 72 65 4c 65 76 65 6c 3b  evel WhereLevel;
4b300 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 65 72 20 73 6f  ../*.** Defer so
4b310 75 72 63 69 6e 67 20 76 64 62 65 2e 68 20 61 6e  urcing vdbe.h an
4b320 64 20 62 74 72 65 65 2e 68 20 75 6e 74 69 6c 20  d btree.h until 
4b330 61 66 74 65 72 20 74 68 65 20 22 75 38 22 20 61  after the "u8" a
4b340 6e 64 20 0a 2a 2a 20 22 42 75 73 79 48 61 6e 64  nd .** "BusyHand
4b350 6c 65 72 22 20 74 79 70 65 64 65 66 73 2e 20 76  ler" typedefs. v
4b360 64 62 65 2e 68 20 61 6c 73 6f 20 72 65 71 75 69  dbe.h also requi
4b370 72 65 73 20 61 20 66 65 77 20 6f 66 20 74 68 65  res a few of the
4b380 20 6f 70 61 71 75 65 0a 2a 2a 20 70 6f 69 6e 74   opaque.** point
4b390 65 72 20 74 79 70 65 73 20 28 69 2e 65 2e 20 46  er types (i.e. F
4b3a0 75 6e 63 44 65 66 29 20 64 65 66 69 6e 65 64 20  uncDef) defined 
4b3b0 61 62 6f 76 65 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a  above..*/./*****
4b3c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
4b3d0 65 20 62 74 72 65 65 2e 68 20 69 6e 20 74 68 65  e btree.h in the
4b3e0 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74   middle of sqlit
4b3f0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
4b400 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
4b410 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
4b420 66 69 6c 65 20 62 74 72 65 65 2e 68 20 2a 2a 2a  file btree.h ***
4b430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b450 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
4b460 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
4b470 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
4b480 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
4b490 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
4b4a0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
4b4b0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
4b4c0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
4b4d0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
4b4e0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
4b4f0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
4b500 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
4b510 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
4b520 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
4b530 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
4b540 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
4b550 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
4b560 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
4b570 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
4b580 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
4b590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b5d0 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20  .** This header 
4b5e0 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65  file defines the
4b5f0 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20   interface that 
4b600 74 68 65 20 73 71 6c 69 74 65 20 42 2d 54 72 65  the sqlite B-Tre
4b610 65 20 66 69 6c 65 0a 2a 2a 20 73 75 62 73 79 73  e file.** subsys
4b620 74 65 6d 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e  tem.  See commen
4b630 74 73 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65  ts in the source
4b640 20 63 6f 64 65 20 66 6f 72 20 61 20 64 65 74 61   code for a deta
4b650 69 6c 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e  iled description
4b660 0a 2a 2a 20 6f 66 20 77 68 61 74 20 65 61 63 68  .** of what each
4b670 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69   interface routi
4b680 6e 65 20 64 6f 65 73 2e 0a 2a 2a 0a 2a 2a 20 40  ne does..**.** @
4b690 28 23 29 20 24 49 64 3a 20 62 74 72 65 65 2e 68  (#) $Id: btree.h
4b6a0 2c 76 20 31 2e 31 32 30 20 32 30 30 39 2f 30 37  ,v 1.120 2009/07
4b6b0 2f 32 32 20 30 30 3a 33 35 3a 32 34 20 64 72 68  /22 00:35:24 drh
4b6c0 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65   Exp $.*/.#ifnde
4b6d0 66 20 5f 42 54 52 45 45 5f 48 5f 0a 23 64 65 66  f _BTREE_H_.#def
4b6e0 69 6e 65 20 5f 42 54 52 45 45 5f 48 5f 0a 0a 2f  ine _BTREE_H_../
4b6f0 2a 20 54 4f 44 4f 3a 20 54 68 69 73 20 64 65 66  * TODO: This def
4b700 69 6e 69 74 69 6f 6e 20 69 73 20 6a 75 73 74 20  inition is just 
4b710 69 6e 63 6c 75 64 65 64 20 73 6f 20 6f 74 68 65  included so othe
4b720 72 20 6d 6f 64 75 6c 65 73 20 63 6f 6d 70 69 6c  r modules compil
4b730 65 2e 20 49 74 0a 2a 2a 20 6e 65 65 64 73 20 74  e. It.** needs t
4b740 6f 20 62 65 20 72 65 76 69 73 69 74 65 64 2e 0a  o be revisited..
4b750 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
4b760 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41 20 31  E_N_BTREE_META 1
4b770 30 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 64 65 66 69  0../*.** If defi
4b780 6e 65 64 20 61 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  ned as non-zero,
4b790 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 73 20   auto-vacuum is 
4b7a0 65 6e 61 62 6c 65 64 20 62 79 20 64 65 66 61 75  enabled by defau
4b7b0 6c 74 2e 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a  lt. Otherwise.**
4b7c0 20 69 74 20 6d 75 73 74 20 62 65 20 74 75 72 6e   it must be turn
4b7d0 65 64 20 6f 6e 20 66 6f 72 20 65 61 63 68 20 64  ed on for each d
4b7e0 61 74 61 62 61 73 65 20 75 73 69 6e 67 20 22 50  atabase using "P
4b7f0 52 41 47 4d 41 20 61 75 74 6f 5f 76 61 63 75 75  RAGMA auto_vacuu
4b800 6d 20 3d 20 31 22 2e 0a 2a 2f 0a 23 69 66 6e 64  m = 1"..*/.#ifnd
4b810 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ef SQLITE_DEFAUL
4b820 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 23  T_AUTOVACUUM.  #
4b830 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45  define SQLITE_DE
4b840 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d  FAULT_AUTOVACUUM
4b850 20 30 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69   0.#endif..#defi
4b860 6e 65 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43  ne BTREE_AUTOVAC
4b870 55 55 4d 5f 4e 4f 4e 45 20 30 20 20 20 20 20 20  UUM_NONE 0      
4b880 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 6f 20 61    /* Do not do a
4b890 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a 23 64  uto-vacuum */.#d
4b8a0 65 66 69 6e 65 20 42 54 52 45 45 5f 41 55 54 4f  efine BTREE_AUTO
4b8b0 56 41 43 55 55 4d 5f 46 55 4c 4c 20 31 20 20 20  VACUUM_FULL 1   
4b8c0 20 20 20 20 20 2f 2a 20 44 6f 20 66 75 6c 6c 20       /* Do full 
4b8d0 61 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a 23  auto-vacuum */.#
4b8e0 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41 55 54  define BTREE_AUT
4b8f0 4f 56 41 43 55 55 4d 5f 49 4e 43 52 20 32 20 20  OVACUUM_INCR 2  
4b900 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65        /* Increme
4b910 6e 74 61 6c 20 76 61 63 75 75 6d 20 2a 2f 0a 0a  ntal vacuum */..
4b920 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 64 65  /*.** Forward de
4b930 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 73 74  clarations of st
4b940 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70 65 64  ructure.*/.typed
4b950 65 66 20 73 74 72 75 63 74 20 42 74 72 65 65 20  ef struct Btree 
4b960 42 74 72 65 65 3b 0a 74 79 70 65 64 65 66 20 73  Btree;.typedef s
4b970 74 72 75 63 74 20 42 74 43 75 72 73 6f 72 20 42  truct BtCursor B
4b980 74 43 75 72 73 6f 72 3b 0a 74 79 70 65 64 65 66  tCursor;.typedef
4b990 20 73 74 72 75 63 74 20 42 74 53 68 61 72 65 64   struct BtShared
4b9a0 20 42 74 53 68 61 72 65 64 3b 0a 74 79 70 65 64   BtShared;.typed
4b9b0 65 66 20 73 74 72 75 63 74 20 42 74 72 65 65 4d  ef struct BtreeM
4b9c0 75 74 65 78 41 72 72 61 79 20 42 74 72 65 65 4d  utexArray BtreeM
4b9d0 75 74 65 78 41 72 72 61 79 3b 0a 0a 2f 2a 0a 2a  utexArray;../*.*
4b9e0 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65  * This structure
4b9f0 20 72 65 63 6f 72 64 73 20 61 6c 6c 20 6f 66 20   records all of 
4ba00 74 68 65 20 42 74 72 65 65 73 20 74 68 61 74 20  the Btrees that 
4ba10 6e 65 65 64 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20  need to hold.** 
4ba20 61 20 6d 75 74 65 78 20 62 65 66 6f 72 65 20 77  a mutex before w
4ba30 65 20 65 6e 74 65 72 20 73 71 6c 69 74 65 33 56  e enter sqlite3V
4ba40 64 62 65 45 78 65 63 28 29 2e 20 20 54 68 65 20  dbeExec().  The 
4ba50 42 74 72 65 65 73 20 61 72 65 0a 2a 2a 20 61 72  Btrees are.** ar
4ba60 65 20 70 6c 61 63 65 64 20 69 6e 20 61 42 74 72  e placed in aBtr
4ba70 65 65 5b 5d 20 69 6e 20 6f 72 64 65 72 20 6f 66  ee[] in order of
4ba80 20 61 42 74 72 65 65 5b 5d 2d 3e 70 42 74 2e 20   aBtree[]->pBt. 
4ba90 20 54 68 61 74 20 77 61 79 2c 0a 2a 2a 20 77 65   That way,.** we
4baa0 20 63 61 6e 20 61 6c 77 61 79 73 20 6c 6f 63 6b   can always lock
4bab0 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 6d   and unlock them
4bac0 20 61 6c 6c 20 71 75 69 63 6b 6c 79 2e 0a 2a 2f   all quickly..*/
4bad0 0a 73 74 72 75 63 74 20 42 74 72 65 65 4d 75 74  .struct BtreeMut
4bae0 65 78 41 72 72 61 79 20 7b 0a 20 20 69 6e 74 20  exArray {.  int 
4baf0 6e 4d 75 74 65 78 3b 0a 20 20 42 74 72 65 65 20  nMutex;.  Btree 
4bb00 2a 61 42 74 72 65 65 5b 53 51 4c 49 54 45 5f 4d  *aBtree[SQLITE_M
4bb10 41 58 5f 41 54 54 41 43 48 45 44 2b 31 5d 3b 0a  AX_ATTACHED+1];.
4bb20 7d 3b 0a 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  };...SQLITE_PRIV
4bb30 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
4bb40 74 72 65 65 4f 70 65 6e 28 0a 20 20 63 6f 6e 73  treeOpen(.  cons
4bb50 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
4bb60 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  e,   /* Name of 
4bb70 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f  database file to
4bb80 20 6f 70 65 6e 20 2a 2f 0a 20 20 73 71 6c 69 74   open */.  sqlit
4bb90 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
4bba0 20 20 20 20 2f 2a 20 41 73 73 6f 63 69 61 74 65      /* Associate
4bbb0 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  d database conne
4bbc0 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 72 65 65  ction */.  Btree
4bbd0 20 2a 2a 70 70 42 74 72 65 65 2c 20 20 20 20 20   **ppBtree,     
4bbe0 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 6f 70      /* Return op
4bbf0 65 6e 20 42 74 72 65 65 2a 20 68 65 72 65 20 2a  en Btree* here *
4bc00 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  /.  int flags,  
4bc10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4bc20 46 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 76  Flags */.  int v
4bc30 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 20  fsFlags         
4bc40 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73      /* Flags pas
4bc50 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 56  sed through to V
4bc60 46 53 20 6f 70 65 6e 20 2a 2f 0a 29 3b 0a 0a 2f  FS open */.);../
4bc70 2a 20 54 68 65 20 66 6c 61 67 73 20 70 61 72 61  * The flags para
4bc80 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33  meter to sqlite3
4bc90 42 74 72 65 65 4f 70 65 6e 20 63 61 6e 20 62 65  BtreeOpen can be
4bca0 20 74 68 65 20 62 69 74 77 69 73 65 20 6f 72 20   the bitwise or 
4bcb0 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77  of the.** follow
4bcc0 69 6e 67 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a  ing values..**.*
4bcd0 2a 20 4e 4f 54 45 3a 20 20 54 68 65 73 65 20 76  * NOTE:  These v
4bce0 61 6c 75 65 73 20 6d 75 73 74 20 6d 61 74 63 68  alues must match
4bcf0 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
4bd00 6e 67 20 50 41 47 45 52 5f 20 76 61 6c 75 65 73  ng PAGER_ values
4bd10 20 69 6e 0a 2a 2a 20 70 61 67 65 72 2e 68 2e 0a   in.** pager.h..
4bd20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45  */.#define BTREE
4bd30 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 20 31  _OMIT_JOURNAL  1
4bd40 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65 20    /* Do not use 
4bd50 6a 6f 75 72 6e 61 6c 2e 20 20 4e 6f 20 61 72 67  journal.  No arg
4bd60 75 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65  ument */.#define
4bd70 20 42 54 52 45 45 5f 4e 4f 5f 52 45 41 44 4c 4f   BTREE_NO_READLO
4bd80 43 4b 20 20 20 32 20 20 2f 2a 20 4f 6d 69 74 20  CK   2  /* Omit 
4bd90 72 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 65 61  readlocks on rea
4bda0 64 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f 0a 23  donly files */.#
4bdb0 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4d 45 4d  define BTREE_MEM
4bdc0 4f 52 59 20 20 20 20 20 20 20 20 34 20 20 2f 2a  ORY        4  /*
4bdd0 20 49 6e 2d 6d 65 6d 6f 72 79 20 44 42 2e 20 20   In-memory DB.  
4bde0 4e 6f 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 23  No argument */.#
4bdf0 64 65 66 69 6e 65 20 42 54 52 45 45 5f 52 45 41  define BTREE_REA
4be00 44 4f 4e 4c 59 20 20 20 20 20 20 38 20 20 2f 2a  DONLY      8  /*
4be10 20 4f 70 65 6e 20 74 68 65 20 64 61 74 61 62 61   Open the databa
4be20 73 65 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20  se in read-only 
4be30 6d 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  mode */.#define 
4be40 42 54 52 45 45 5f 52 45 41 44 57 52 49 54 45 20  BTREE_READWRITE 
4be50 20 20 20 31 36 20 20 2f 2a 20 4f 70 65 6e 20 66     16  /* Open f
4be60 6f 72 20 62 6f 74 68 20 72 65 61 64 69 6e 67 20  or both reading 
4be70 61 6e 64 20 77 72 69 74 69 6e 67 20 2a 2f 0a 23  and writing */.#
4be80 64 65 66 69 6e 65 20 42 54 52 45 45 5f 43 52 45  define BTREE_CRE
4be90 41 54 45 20 20 20 20 20 20 20 33 32 20 20 2f 2a  ATE       32  /*
4bea0 20 43 72 65 61 74 65 20 74 68 65 20 64 61 74 61   Create the data
4beb0 62 61 73 65 20 69 66 20 69 74 20 64 6f 65 73 20  base if it does 
4bec0 6e 6f 74 20 65 78 69 73 74 20 2a 2f 0a 0a 53 51  not exist */..SQ
4bed0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4bee0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f   sqlite3BtreeClo
4bef0 73 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49  se(Btree*);.SQLI
4bf00 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4bf10 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61  qlite3BtreeSetCa
4bf20 63 68 65 53 69 7a 65 28 42 74 72 65 65 2a 2c 69  cheSize(Btree*,i
4bf30 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
4bf40 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
4bf50 74 72 65 65 53 65 74 53 61 66 65 74 79 4c 65 76  treeSetSafetyLev
4bf60 65 6c 28 42 74 72 65 65 2a 2c 69 6e 74 2c 69 6e  el(Btree*,int,in
4bf70 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
4bf80 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
4bf90 72 65 65 53 79 6e 63 44 69 73 61 62 6c 65 64 28  reeSyncDisabled(
4bfa0 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Btree*);.SQLITE_
4bfb0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4bfc0 74 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53  te3BtreeSetPageS
4bfd0 69 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e  ize(Btree *p, in
4bfe0 74 20 6e 50 61 67 65 73 69 7a 65 2c 20 69 6e 74  t nPagesize, int
4bff0 20 6e 52 65 73 65 72 76 65 2c 20 69 6e 74 20 65   nReserve, int e
4c000 46 69 78 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  Fix);.SQLITE_PRI
4c010 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4c020 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65  BtreeGetPageSize
4c030 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
4c040 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4c050 69 74 65 33 42 74 72 65 65 4d 61 78 50 61 67 65  ite3BtreeMaxPage
4c060 43 6f 75 6e 74 28 42 74 72 65 65 2a 2c 69 6e 74  Count(Btree*,int
4c070 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4c080 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4c090 65 65 47 65 74 52 65 73 65 72 76 65 28 42 74 72  eeGetReserve(Btr
4c0a0 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ee*);.SQLITE_PRI
4c0b0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4c0c0 42 74 72 65 65 53 65 74 41 75 74 6f 56 61 63 75  BtreeSetAutoVacu
4c0d0 75 6d 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 29  um(Btree *, int)
4c0e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4c0f0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4c100 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42  eGetAutoVacuum(B
4c110 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  tree *);.SQLITE_
4c120 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4c130 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61  te3BtreeBeginTra
4c140 6e 73 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a  ns(Btree*,int);.
4c150 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4c160 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
4c170 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74  ommitPhaseOne(Bt
4c180 72 65 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  ree*, const char
4c190 20 2a 7a 4d 61 73 74 65 72 29 3b 0a 53 51 4c 49   *zMaster);.SQLI
4c1a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4c1b0 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
4c1c0 74 50 68 61 73 65 54 77 6f 28 42 74 72 65 65 2a  tPhaseTwo(Btree*
4c1d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4c1e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4c1f0 65 65 43 6f 6d 6d 69 74 28 42 74 72 65 65 2a 29  eeCommit(Btree*)
4c200 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4c210 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4c220 65 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65 2a  eRollback(Btree*
4c230 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4c240 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4c250 65 65 42 65 67 69 6e 53 74 6d 74 28 42 74 72 65  eeBeginStmt(Btre
4c260 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  e*,int);.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 43 72 65 61 74 65 54 61  te3BtreeCreateTa
4c290 62 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2a  ble(Btree*, int*
4c2a0 2c 20 69 6e 74 20 66 6c 61 67 73 29 3b 0a 53 51  , int flags);.SQ
4c2b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4c2c0 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49   sqlite3BtreeIsI
4c2d0 6e 54 72 61 6e 73 28 42 74 72 65 65 2a 29 3b 0a  nTrans(Btree*);.
4c2e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4c2f0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  nt sqlite3BtreeI
4c300 73 49 6e 52 65 61 64 54 72 61 6e 73 28 42 74 72  sInReadTrans(Btr
4c310 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ee*);.SQLITE_PRI
4c320 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4c330 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28  BtreeIsInBackup(
4c340 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Btree*);.SQLITE_
4c350 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
4c360 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61  lite3BtreeSchema
4c370 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c 20 76  (Btree *, int, v
4c380 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a 29 29 3b  oid(*)(void *));
4c390 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4c3a0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4c3b0 53 63 68 65 6d 61 4c 6f 63 6b 65 64 28 42 74 72  SchemaLocked(Btr
4c3c0 65 65 20 2a 70 42 74 72 65 65 29 3b 0a 53 51 4c  ee *pBtree);.SQL
4c3d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4c3e0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f 63 6b  sqlite3BtreeLock
4c3f0 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 42 74  Table(Btree *pBt
4c400 72 65 65 2c 20 69 6e 74 20 69 54 61 62 2c 20 75  ree, int iTab, u
4c410 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b 0a  8 isWriteLock);.
4c420 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4c430 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  nt sqlite3BtreeS
4c440 61 76 65 70 6f 69 6e 74 28 42 74 72 65 65 20 2a  avepoint(Btree *
4c450 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 53 51  , int, int);..SQ
4c460 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
4c470 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
4c480 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65  BtreeGetFilename
4c490 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54  (Btree *);.SQLIT
4c4a0 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
4c4b0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72  char *sqlite3Btr
4c4c0 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65  eeGetJournalname
4c4d0 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54  (Btree *);.SQLIT
4c4e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4c4f0 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 69  lite3BtreeCopyFi
4c500 6c 65 28 42 74 72 65 65 20 2a 2c 20 42 74 72 65  le(Btree *, Btre
4c510 65 20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  e *);..SQLITE_PR
4c520 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4c530 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d  3BtreeIncrVacuum
4c540 28 42 74 72 65 65 20 2a 29 3b 0a 0a 2f 2a 20 54  (Btree *);../* T
4c550 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74  he flags paramet
4c560 65 72 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72  er to sqlite3Btr
4c570 65 65 43 72 65 61 74 65 54 61 62 6c 65 20 63 61  eeCreateTable ca
4c580 6e 20 62 65 20 74 68 65 20 62 69 74 77 69 73 65  n be the bitwise
4c590 20 4f 52 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f   OR.** of the fo
4c5a0 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 3a 0a 2a  llowing flags:.*
4c5b0 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f  /.#define BTREE_
4c5c0 49 4e 54 4b 45 59 20 20 20 20 20 31 20 20 20 20  INTKEY     1    
4c5d0 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 6f 6e 6c  /* Table has onl
4c5e0 79 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20  y 64-bit signed 
4c5f0 69 6e 74 65 67 65 72 20 6b 65 79 73 20 2a 2f 0a  integer keys */.
4c600 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 5a 45  #define BTREE_ZE
4c610 52 4f 44 41 54 41 20 20 20 32 20 20 20 20 2f 2a  RODATA   2    /*
4c620 20 54 61 62 6c 65 20 68 61 73 20 6b 65 79 73 20   Table has keys 
4c630 6f 6e 6c 79 20 2d 20 6e 6f 20 64 61 74 61 20 2a  only - no data *
4c640 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f  /.#define BTREE_
4c650 4c 45 41 46 44 41 54 41 20 20 20 34 20 20 20 20  LEAFDATA   4    
4c660 2f 2a 20 44 61 74 61 20 73 74 6f 72 65 64 20 69  /* Data stored i
4c670 6e 20 6c 65 61 76 65 73 20 6f 6e 6c 79 2e 20 20  n leaves only.  
4c680 49 6d 70 6c 69 65 73 20 49 4e 54 4b 45 59 20 2a  Implies INTKEY *
4c690 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
4c6a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4c6b0 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74 72 65  eeDropTable(Btre
4c6c0 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a  e*, int, int*);.
4c6d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4c6e0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
4c6f0 6c 65 61 72 54 61 62 6c 65 28 42 74 72 65 65 2a  learTable(Btree*
4c700 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51  , int, int*);.SQ
4c710 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
4c720 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 72  d sqlite3BtreeTr
4c730 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 72  ipAllCursors(Btr
4c740 65 65 2a 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c 49  ee*, int);..SQLI
4c750 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
4c760 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d  sqlite3BtreeGetM
4c770 65 74 61 28 42 74 72 65 65 20 2a 70 42 74 72 65  eta(Btree *pBtre
4c780 65 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20  e, int idx, u32 
4c790 2a 70 56 61 6c 75 65 29 3b 0a 53 51 4c 49 54 45  *pValue);.SQLITE
4c7a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4c7b0 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d  ite3BtreeUpdateM
4c7c0 65 74 61 28 42 74 72 65 65 2a 2c 20 69 6e 74 20  eta(Btree*, int 
4c7d0 69 64 78 2c 20 75 33 32 20 76 61 6c 75 65 29 3b  idx, u32 value);
4c7e0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f  ../*.** The seco
4c7f0 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  nd parameter to 
4c800 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d  sqlite3BtreeGetM
4c810 65 74 61 20 6f 72 20 73 71 6c 69 74 65 33 42 74  eta or sqlite3Bt
4c820 72 65 65 55 70 64 61 74 65 4d 65 74 61 0a 2a 2a  reeUpdateMeta.**
4c830 20 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20 6f   should be one o
4c840 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
4c850 76 61 6c 75 65 73 2e 20 54 68 65 20 69 6e 74 65  values. The inte
4c860 67 65 72 20 76 61 6c 75 65 73 20 61 72 65 20 61  ger values are a
4c870 73 73 69 67 6e 65 64 20 0a 2a 2a 20 74 6f 20 63  ssigned .** to c
4c880 6f 6e 73 74 61 6e 74 73 20 73 6f 20 74 68 61 74  onstants so that
4c890 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66 20 74   the offset of t
4c8a0 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
4c8b0 20 66 69 65 6c 64 20 69 6e 20 61 6e 0a 2a 2a 20   field in an.** 
4c8c0 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20  SQLite database 
4c8d0 68 65 61 64 65 72 20 6d 61 79 20 62 65 20 66 6f  header may be fo
4c8e0 75 6e 64 20 75 73 69 6e 67 20 74 68 65 20 66 6f  und using the fo
4c8f0 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 75 6c 61 3a  llowing formula:
4c900 0a 2a 2a 0a 2a 2a 20 20 20 6f 66 66 73 65 74 20  .**.**   offset 
4c910 3d 20 33 36 20 2b 20 28 69 64 78 20 2a 20 34 29  = 36 + (idx * 4)
4c920 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70  .**.** For examp
4c930 6c 65 2c 20 74 68 65 20 66 72 65 65 2d 70 61 67  le, the free-pag
4c940 65 2d 63 6f 75 6e 74 20 66 69 65 6c 64 20 69 73  e-count field is
4c950 20 6c 6f 63 61 74 65 64 20 61 74 20 62 79 74 65   located at byte
4c960 20 6f 66 66 73 65 74 20 33 36 20 6f 66 0a 2a 2a   offset 36 of.**
4c970 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
4c980 6c 65 20 68 65 61 64 65 72 2e 20 54 68 65 20 69  le header. The i
4c990 6e 63 72 2d 76 61 63 75 75 6d 2d 66 6c 61 67 20  ncr-vacuum-flag 
4c9a0 66 69 65 6c 64 20 69 73 20 6c 6f 63 61 74 65 64  field is located
4c9b0 20 61 74 0a 2a 2a 20 62 79 74 65 20 6f 66 66 73   at.** byte offs
4c9c0 65 74 20 36 34 20 28 3d 3d 20 33 36 2b 34 2a 37  et 64 (== 36+4*7
4c9d0 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54  )..*/.#define BT
4c9e0 52 45 45 5f 46 52 45 45 5f 50 41 47 45 5f 43 4f  REE_FREE_PAGE_CO
4c9f0 55 4e 54 20 20 20 20 20 30 0a 23 64 65 66 69 6e  UNT     0.#defin
4ca00 65 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f 56  e BTREE_SCHEMA_V
4ca10 45 52 53 49 4f 4e 20 20 20 20 20 20 31 0a 23 64  ERSION      1.#d
4ca20 65 66 69 6e 65 20 42 54 52 45 45 5f 46 49 4c 45  efine BTREE_FILE
4ca30 5f 46 4f 52 4d 41 54 20 20 20 20 20 20 20 20 20  _FORMAT         
4ca40 32 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f  2.#define BTREE_
4ca50 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49  DEFAULT_CACHE_SI
4ca60 5a 45 20 20 33 0a 23 64 65 66 69 6e 65 20 42 54  ZE  3.#define BT
4ca70 52 45 45 5f 4c 41 52 47 45 53 54 5f 52 4f 4f 54  REE_LARGEST_ROOT
4ca80 5f 50 41 47 45 20 20 20 34 0a 23 64 65 66 69 6e  _PAGE   4.#defin
4ca90 65 20 42 54 52 45 45 5f 54 45 58 54 5f 45 4e 43  e BTREE_TEXT_ENC
4caa0 4f 44 49 4e 47 20 20 20 20 20 20 20 35 0a 23 64  ODING       5.#d
4cab0 65 66 69 6e 65 20 42 54 52 45 45 5f 55 53 45 52  efine BTREE_USER
4cac0 5f 56 45 52 53 49 4f 4e 20 20 20 20 20 20 20 20  _VERSION        
4cad0 36 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f  6.#define BTREE_
4cae0 49 4e 43 52 5f 56 41 43 55 55 4d 20 20 20 20 20  INCR_VACUUM     
4caf0 20 20 20 20 37 0a 0a 53 51 4c 49 54 45 5f 50 52      7..SQLITE_PR
4cb00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4cb10 33 42 74 72 65 65 43 75 72 73 6f 72 28 0a 20 20  3BtreeCursor(.  
4cb20 42 74 72 65 65 2a 2c 20 20 20 20 20 20 20 20 20  Btree*,         
4cb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cb40 20 20 20 20 20 2f 2a 20 42 54 72 65 65 20 63 6f       /* BTree co
4cb50 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 20 74  ntaining table t
4cb60 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20  o open */.  int 
4cb70 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20  iTable,         
4cb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cb90 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 6f 6f   /* Index of roo
4cba0 74 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  t page */.  int 
4cbb0 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20  wrFlag,         
4cbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cbd0 20 2f 2a 20 31 20 66 6f 72 20 77 72 69 74 69 6e   /* 1 for writin
4cbe0 67 2e 20 20 30 20 66 6f 72 20 72 65 61 64 2d 6f  g.  0 for read-o
4cbf0 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  nly */.  struct 
4cc00 4b 65 79 49 6e 66 6f 2a 2c 20 20 20 20 20 20 20  KeyInfo*,       
4cc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4cc20 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   First argument 
4cc30 74 6f 20 63 6f 6d 70 61 72 65 20 66 75 6e 63 74  to compare funct
4cc40 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f  ion */.  BtCurso
4cc50 72 20 2a 70 43 75 72 73 6f 72 20 20 20 20 20 20  r *pCursor      
4cc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4cc70 20 53 70 61 63 65 20 74 6f 20 77 72 69 74 65 20   Space to write 
4cc80 63 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65  cursor structure
4cc90 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52   */.);.SQLITE_PR
4cca0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4ccb0 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65  3BtreeCursorSize
4ccc0 28 76 6f 69 64 29 3b 0a 0a 53 51 4c 49 54 45 5f  (void);..SQLITE_
4ccd0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4cce0 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72  te3BtreeCloseCur
4ccf0 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a  sor(BtCursor*);.
4cd00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4cd10 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  nt sqlite3BtreeM
4cd20 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a 20  ovetoUnpacked(. 
4cd30 20 42 74 43 75 72 73 6f 72 2a 2c 0a 20 20 55 6e   BtCursor*,.  Un
4cd40 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 55  packedRecord *pU
4cd50 6e 4b 65 79 2c 0a 20 20 69 36 34 20 69 6e 74 4b  nKey,.  i64 intK
4cd60 65 79 2c 0a 20 20 69 6e 74 20 62 69 61 73 2c 0a  ey,.  int bias,.
4cd70 20 20 69 6e 74 20 2a 70 52 65 73 0a 29 3b 0a 53    int *pRes.);.S
4cd80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4cd90 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  t sqlite3BtreeCu
4cda0 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 74 43  rsorHasMoved(BtC
4cdb0 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53  ursor*, int*);.S
4cdc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4cdd0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65  t sqlite3BtreeDe
4cde0 6c 65 74 65 28 42 74 43 75 72 73 6f 72 2a 29 3b  lete(BtCursor*);
4cdf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4ce00 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4ce10 49 6e 73 65 72 74 28 42 74 43 75 72 73 6f 72 2a  Insert(BtCursor*
4ce20 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b  , const void *pK
4ce30 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c 0a 20 20  ey, i64 nKey,.  
4ce40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ce50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ce60 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74  const void *pDat
4ce70 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20  a, int nData,.  
4ce80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ce90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cea0 69 6e 74 20 6e 5a 65 72 6f 2c 20 69 6e 74 20 62  int nZero, int b
4ceb0 69 61 73 2c 20 69 6e 74 20 73 65 65 6b 52 65 73  ias, int seekRes
4cec0 75 6c 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ult);.SQLITE_PRI
4ced0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4cee0 42 74 72 65 65 46 69 72 73 74 28 42 74 43 75 72  BtreeFirst(BtCur
4cef0 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29  sor*, int *pRes)
4cf00 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4cf10 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4cf20 65 4c 61 73 74 28 42 74 43 75 72 73 6f 72 2a 2c  eLast(BtCursor*,
4cf30 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c   int *pRes);.SQL
4cf40 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4cf50 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74  sqlite3BtreeNext
4cf60 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20  (BtCursor*, int 
4cf70 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50  *pRes);.SQLITE_P
4cf80 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4cf90 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 75 72  e3BtreeEof(BtCur
4cfa0 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  sor*);.SQLITE_PR
4cfb0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4cfc0 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 42  3BtreePrevious(B
4cfd0 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70  tCursor*, int *p
4cfe0 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  Res);.SQLITE_PRI
4cff0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4d000 42 74 72 65 65 4b 65 79 53 69 7a 65 28 42 74 43  BtreeKeySize(BtC
4d010 75 72 73 6f 72 2a 2c 20 69 36 34 20 2a 70 53 69  ursor*, i64 *pSi
4d020 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ze);.SQLITE_PRIV
4d030 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
4d040 74 72 65 65 4b 65 79 28 42 74 43 75 72 73 6f 72  treeKey(BtCursor
4d050 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75  *, u32 offset, u
4d060 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a  32 amt, void*);.
4d070 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
4d080 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
4d090 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28  e3BtreeKeyFetch(
4d0a0 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a  BtCursor*, int *
4d0b0 70 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  pAmt);.SQLITE_PR
4d0c0 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64  IVATE const void
4d0d0 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 44 61   *sqlite3BtreeDa
4d0e0 74 61 46 65 74 63 68 28 42 74 43 75 72 73 6f 72  taFetch(BtCursor
4d0f0 2a 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a 53  *, int *pAmt);.S
4d100 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4d110 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61  t sqlite3BtreeDa
4d120 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a  taSize(BtCursor*
4d130 2c 20 75 33 32 20 2a 70 53 69 7a 65 29 3b 0a 53  , u32 *pSize);.S
4d140 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4d150 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61  t sqlite3BtreeDa
4d160 74 61 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33  ta(BtCursor*, u3
4d170 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d  2 offset, u32 am
4d180 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54  t, void*);.SQLIT
4d190 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
4d1a0 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61  qlite3BtreeSetCa
4d1b0 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72 73  chedRowid(BtCurs
4d1c0 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74  or*, sqlite3_int
4d1d0 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  64);.SQLITE_PRIV
4d1e0 41 54 45 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ATE sqlite3_int6
4d1f0 34 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  4 sqlite3BtreeGe
4d200 74 43 61 63 68 65 64 52 6f 77 69 64 28 42 74 43  tCachedRowid(BtC
4d210 75 72 73 6f 72 2a 29 3b 0a 0a 53 51 4c 49 54 45  ursor*);..SQLITE
4d220 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
4d230 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 65 67  qlite3BtreeInteg
4d240 72 69 74 79 43 68 65 63 6b 28 42 74 72 65 65 2a  rityCheck(Btree*
4d250 2c 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 69 6e  , int *aRoot, in
4d260 74 20 6e 52 6f 6f 74 2c 20 69 6e 74 2c 20 69 6e  t nRoot, int, in
4d270 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
4d280 41 54 45 20 73 74 72 75 63 74 20 50 61 67 65 72  ATE struct Pager
4d290 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 50 61   *sqlite3BtreePa
4d2a0 67 65 72 28 42 74 72 65 65 2a 29 3b 0a 0a 53 51  ger(Btree*);..SQ
4d2b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4d2c0 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74   sqlite3BtreePut
4d2d0 44 61 74 61 28 42 74 43 75 72 73 6f 72 2a 2c 20  Data(BtCursor*, 
4d2e0 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20  u32 offset, u32 
4d2f0 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c  amt, void*);.SQL
4d300 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
4d310 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63   sqlite3BtreeCac
4d320 68 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75 72  heOverflow(BtCur
4d330 73 6f 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  sor *);.SQLITE_P
4d340 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
4d350 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72  te3BtreeClearCur
4d360 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 29 3b  sor(BtCursor *);
4d370 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
4d380 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4d390 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4d3a0 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 42 74  CursorIsValid(Bt
4d3b0 43 75 72 73 6f 72 2a 29 3b 0a 23 65 6e 64 69 66  Cursor*);.#endif
4d3c0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
4d3d0 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54  _OMIT_BTREECOUNT
4d3e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4d3f0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4d400 43 6f 75 6e 74 28 42 74 43 75 72 73 6f 72 20 2a  Count(BtCursor *
4d410 2c 20 69 36 34 20 2a 29 3b 0a 23 65 6e 64 69 66  , i64 *);.#endif
4d420 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
4d430 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56  TEST.SQLITE_PRIV
4d440 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
4d450 74 72 65 65 43 75 72 73 6f 72 49 6e 66 6f 28 42  treeCursorInfo(B
4d460 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 2c 20  tCursor*, int*, 
4d470 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
4d480 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
4d490 33 42 74 72 65 65 43 75 72 73 6f 72 4c 69 73 74  3BtreeCursorList
4d4a0 28 42 74 72 65 65 2a 29 3b 0a 23 65 6e 64 69 66  (Btree*);.#endif
4d4b0 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72  ../*.** If we ar
4d4c0 65 20 6e 6f 74 20 75 73 69 6e 67 20 73 68 61 72  e not using shar
4d4d0 65 64 20 63 61 63 68 65 2c 20 74 68 65 6e 20 74  ed cache, then t
4d4e0 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20  here is no need 
4d4f0 74 6f 0a 2a 2a 20 75 73 65 20 6d 75 74 65 78 65  to.** use mutexe
4d500 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20  s to access the 
4d510 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74 75  BtShared structu
4d520 72 65 73 2e 20 20 53 6f 20 6d 61 6b 65 20 74 68  res.  So make th
4d530 65 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64 20 4c  e.** Enter and L
4d540 65 61 76 65 20 70 72 6f 63 65 64 75 72 65 73 20  eave procedures 
4d550 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 69 66 6e 64  no-ops..*/.#ifnd
4d560 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
4d570 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49  HARED_CACHE.SQLI
4d580 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
4d590 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e  d sqlite3BtreeEn
4d5a0 74 65 72 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c  ter(Btree*);.SQL
4d5b0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
4d5c0 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  id sqlite3BtreeE
4d5d0 6e 74 65 72 41 6c 6c 28 73 71 6c 69 74 65 33 2a  nterAll(sqlite3*
4d5e0 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  );.#else.# defin
4d5f0 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e  e sqlite3BtreeEn
4d600 74 65 72 28 58 29 20 0a 23 20 64 65 66 69 6e 65  ter(X) .# define
4d610 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
4d620 65 72 41 6c 6c 28 58 29 0a 23 65 6e 64 69 66 0a  erAll(X).#endif.
4d630 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
4d640 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
4d650 5f 43 41 43 48 45 29 20 26 26 20 53 51 4c 49 54  _CACHE) && SQLIT
4d660 45 5f 54 48 52 45 41 44 53 41 46 45 0a 53 51 4c  E_THREADSAFE.SQL
4d670 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
4d680 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  id sqlite3BtreeL
4d690 65 61 76 65 28 42 74 72 65 65 2a 29 3b 0a 53 51  eave(Btree*);.SQ
4d6a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
4d6b0 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
4d6c0 45 6e 74 65 72 43 75 72 73 6f 72 28 42 74 43 75  EnterCursor(BtCu
4d6d0 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  rsor*);.SQLITE_P
4d6e0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
4d6f0 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 43  lite3BtreeLeaveC
4d700 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29  ursor(BtCursor*)
4d710 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4d720 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42     void sqlite3B
4d730 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c  treeLeaveAll(sql
4d740 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ite3*);.SQLITE_P
4d750 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
4d760 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41  lite3BtreeMutexA
4d770 72 72 61 79 45 6e 74 65 72 28 42 74 72 65 65 4d  rrayEnter(BtreeM
4d780 75 74 65 78 41 72 72 61 79 2a 29 3b 0a 53 51 4c  utexArray*);.SQL
4d790 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
4d7a0 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  id sqlite3BtreeM
4d7b0 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 42  utexArrayLeave(B
4d7c0 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 2a 29  treeMutexArray*)
4d7d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4d7e0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42     void sqlite3B
4d7f0 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e  treeMutexArrayIn
4d800 73 65 72 74 28 42 74 72 65 65 4d 75 74 65 78 41  sert(BtreeMutexA
4d810 72 72 61 79 2a 2c 20 42 74 72 65 65 2a 29 3b 0a  rray*, Btree*);.
4d820 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
4d830 20 2f 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e   /* These routin
4d840 65 73 20 61 72 65 20 75 73 65 64 20 69 6e 73 69  es are used insi
4d850 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74  de assert() stat
4d860 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f 0a  ements only. */.
4d870 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
4d880 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4d890 65 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 72 65  eHoldsMutex(Btre
4d8a0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
4d8b0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
4d8c0 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75  3BtreeHoldsAllMu
4d8d0 74 65 78 65 73 28 73 71 6c 69 74 65 33 2a 29 3b  texes(sqlite3*);
4d8e0 0a 23 65 6e 64 69 66 0a 23 65 6c 73 65 0a 0a 23  .#endif.#else..#
4d8f0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42   define sqlite3B
4d900 74 72 65 65 4c 65 61 76 65 28 58 29 0a 23 20 64  treeLeave(X).# d
4d910 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72  efine sqlite3Btr
4d920 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 58 29  eeEnterCursor(X)
4d930 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
4d940 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f  3BtreeLeaveCurso
4d950 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71  r(X).# define sq
4d960 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41  lite3BtreeLeaveA
4d970 6c 6c 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73  ll(X).# define s
4d980 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
4d990 41 72 72 61 79 45 6e 74 65 72 28 58 29 0a 23 20  ArrayEnter(X).# 
4d9a0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74  define sqlite3Bt
4d9b0 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61  reeMutexArrayLea
4d9c0 76 65 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73  ve(X).# define s
4d9d0 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
4d9e0 41 72 72 61 79 49 6e 73 65 72 74 28 58 2c 59 29  ArrayInsert(X,Y)
4d9f0 0a 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ..# define sqlit
4da00 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65  e3BtreeHoldsMute
4da10 78 28 58 29 20 31 0a 23 20 64 65 66 69 6e 65 20  x(X) 1.# define 
4da20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
4da30 73 41 6c 6c 4d 75 74 65 78 65 73 28 58 29 20 31  sAllMutexes(X) 1
4da40 0a 23 65 6e 64 69 66 0a 0a 0a 23 65 6e 64 69 66  .#endif...#endif
4da50 20 2f 2a 20 5f 42 54 52 45 45 5f 48 5f 20 2a 2f   /* _BTREE_H_ */
4da60 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
4da70 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65 2e 68  * End of btree.h
4da80 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
4da90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4daa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dab0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
4dac0 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
4dad0 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
4dae0 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  n sqliteInt.h **
4daf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4db00 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
4db10 2a 20 49 6e 63 6c 75 64 65 20 76 64 62 65 2e 68  * Include vdbe.h
4db20 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
4db30 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  f sqliteInt.h **
4db40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4db50 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
4db60 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62  * Begin file vdb
4db70 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
4db80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4db90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dba0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
4dbb0 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
4dbc0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
4dbd0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
4dbe0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
4dbf0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
4dc00 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
4dc10 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
4dc20 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
4dc30 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
4dc40 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
4dc50 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
4dc60 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
4dc70 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
4dc80 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
4dc90 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
4dca0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
4dcb0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
4dcc0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
4dcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dd00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dd10 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 61 64  ********.** Head
4dd20 65 72 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20  er file for the 
4dd30 56 69 72 74 75 61 6c 20 44 61 74 61 42 61 73 65  Virtual DataBase
4dd40 20 45 6e 67 69 6e 65 20 28 56 44 42 45 29 0a 2a   Engine (VDBE).*
4dd50 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72  *.** This header
4dd60 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74   defines the int
4dd70 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 76 69  erface to the vi
4dd80 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65  rtual database e
4dd90 6e 67 69 6e 65 0a 2a 2a 20 6f 72 20 56 44 42 45  ngine.** or VDBE
4dda0 2e 20 20 54 68 65 20 56 44 42 45 20 69 6d 70 6c  .  The VDBE impl
4ddb0 65 6d 65 6e 74 73 20 61 6e 20 61 62 73 74 72 61  ements an abstra
4ddc0 63 74 20 6d 61 63 68 69 6e 65 20 74 68 61 74 20  ct machine that 
4ddd0 72 75 6e 73 20 61 0a 2a 2a 20 73 69 6d 70 6c 65  runs a.** simple
4dde0 20 70 72 6f 67 72 61 6d 20 74 6f 20 61 63 63 65   program to acce
4ddf0 73 73 20 61 6e 64 20 6d 6f 64 69 66 79 20 74 68  ss and modify th
4de00 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74  e underlying dat
4de10 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  abase..**.** $Id
4de20 3a 20 76 64 62 65 2e 68 2c 76 20 31 2e 31 34 32  : vdbe.h,v 1.142
4de30 20 32 30 30 39 2f 30 37 2f 32 34 20 31 37 3a 35   2009/07/24 17:5
4de40 38 3a 35 33 20 64 61 6e 69 65 6c 6b 31 39 37 37  8:53 danielk1977
4de50 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65   Exp $.*/.#ifnde
4de60 66 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f 48  f _SQLITE_VDBE_H
4de70 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54  _.#define _SQLIT
4de80 45 5f 56 44 42 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a  E_VDBE_H_../*.**
4de90 20 41 20 73 69 6e 67 6c 65 20 56 44 42 45 20 69   A single VDBE i
4dea0 73 20 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75  s an opaque stru
4deb0 63 74 75 72 65 20 6e 61 6d 65 64 20 22 56 64 62  cture named "Vdb
4dec0 65 22 2e 20 20 4f 6e 6c 79 20 72 6f 75 74 69 6e  e".  Only routin
4ded0 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 6f 75  es.** in the sou
4dee0 72 63 65 20 66 69 6c 65 20 73 71 6c 69 74 65 56  rce file sqliteV
4def0 64 62 65 2e 63 20 61 72 65 20 61 6c 6c 6f 77 65  dbe.c are allowe
4df00 64 20 74 6f 20 73 65 65 20 74 68 65 20 69 6e 73  d to see the ins
4df10 69 64 65 73 0a 2a 2a 20 6f 66 20 74 68 69 73 20  ides.** of this 
4df20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79  structure..*/.ty
4df30 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62  pedef struct Vdb
4df40 65 20 56 64 62 65 3b 0a 0a 2f 2a 0a 2a 2a 20 54  e Vdbe;../*.** T
4df50 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 20  he names of the 
4df60 66 6f 6c 6c 6f 77 69 6e 67 20 74 79 70 65 73 20  following types 
4df70 64 65 63 6c 61 72 65 64 20 69 6e 20 76 64 62 65  declared in vdbe
4df80 49 6e 74 2e 68 20 61 72 65 20 72 65 71 75 69 72  Int.h are requir
4df90 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 56 64  ed.** for the Vd
4dfa0 62 65 4f 70 20 64 65 66 69 6e 69 74 69 6f 6e 2e  beOp definition.
4dfb0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
4dfc0 63 74 20 56 64 62 65 46 75 6e 63 20 56 64 62 65  ct VdbeFunc Vdbe
4dfd0 46 75 6e 63 3b 0a 74 79 70 65 64 65 66 20 73 74  Func;.typedef st
4dfe0 72 75 63 74 20 4d 65 6d 20 4d 65 6d 3b 0a 74 79  ruct Mem Mem;.ty
4dff0 70 65 64 65 66 20 73 74 72 75 63 74 20 53 75 62  pedef struct Sub
4e000 50 72 6f 67 72 61 6d 20 53 75 62 50 72 6f 67 72  Program SubProgr
4e010 61 6d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 69 6e  am;../*.** A sin
4e020 67 6c 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  gle instruction 
4e030 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d  of the virtual m
4e040 61 63 68 69 6e 65 20 68 61 73 20 61 6e 20 6f 70  achine has an op
4e050 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 61 73 20 6d  code.** and as m
4e060 61 6e 79 20 61 73 20 74 68 72 65 65 20 6f 70 65  any as three ope
4e070 72 61 6e 64 73 2e 20 20 54 68 65 20 69 6e 73 74  rands.  The inst
4e080 72 75 63 74 69 6f 6e 20 69 73 20 72 65 63 6f 72  ruction is recor
4e090 64 65 64 0a 2a 2a 20 61 73 20 61 6e 20 69 6e 73  ded.** as an ins
4e0a0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
4e0b0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
4e0c0 3a 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65  :.*/.struct Vdbe
4e0d0 4f 70 20 7b 0a 20 20 75 38 20 6f 70 63 6f 64 65  Op {.  u8 opcode
4e0e0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68  ;          /* Wh
4e0f0 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20  at operation to 
4e100 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20 73 69 67  perform */.  sig
4e110 6e 65 64 20 63 68 61 72 20 70 34 74 79 70 65 3b  ned char p4type;
4e120 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 50   /* One of the P
4e130 34 5f 78 78 78 20 63 6f 6e 73 74 61 6e 74 73 20  4_xxx constants 
4e140 66 6f 72 20 70 34 20 2a 2f 0a 20 20 75 38 20 6f  for p4 */.  u8 o
4e150 70 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20  pflags;         
4e160 2f 2a 20 4e 6f 74 20 63 75 72 72 65 6e 74 6c 79  /* Not currently
4e170 20 75 73 65 64 20 2a 2f 0a 20 20 75 38 20 70 35   used */.  u8 p5
4e180 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
4e190 2a 20 46 69 66 74 68 20 70 61 72 61 6d 65 74 65  * Fifth paramete
4e1a0 72 20 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 64  r is an unsigned
4e1b0 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20   character */.  
4e1c0 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 20  int p1;         
4e1d0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f 70 65      /* First ope
4e1e0 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 32  rand */.  int p2
4e1f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
4e200 20 53 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   Second paramete
4e210 72 20 28 6f 66 74 65 6e 20 74 68 65 20 6a 75 6d  r (often the jum
4e220 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 29 20 2a  p destination) *
4e230 2f 0a 20 20 69 6e 74 20 70 33 3b 20 20 20 20 20  /.  int p3;     
4e240 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
4e250 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 2a  hird parameter *
4e260 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20 20  /.  union {     
4e270 20 20 20 20 20 20 20 20 2f 2a 20 66 6f 75 72 74          /* fourt
4e280 68 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20  h parameter */. 
4e290 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20     int i;       
4e2a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74            /* Int
4e2b0 65 67 65 72 20 76 61 6c 75 65 20 69 66 20 70 34  eger value if p4
4e2c0 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 2a  type==P4_INT32 *
4e2d0 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 3b 20 20  /.    void *p;  
4e2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4e2f0 47 65 6e 65 72 69 63 20 70 6f 69 6e 74 65 72 20  Generic pointer 
4e300 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 20  */.    char *z; 
4e310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4e320 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 61   Pointer to data
4e330 20 66 6f 72 20 73 74 72 69 6e 67 20 28 63 68 61   for string (cha
4e340 72 20 61 72 72 61 79 29 20 74 79 70 65 73 20 2a  r array) types *
4e350 2f 0a 20 20 20 20 69 36 34 20 2a 70 49 36 34 3b  /.    i64 *pI64;
4e360 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4e370 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65  Used when p4type
4e380 20 69 73 20 50 34 5f 49 4e 54 36 34 20 2a 2f 0a   is P4_INT64 */.
4e390 20 20 20 20 64 6f 75 62 6c 65 20 2a 70 52 65 61      double *pRea
4e3a0 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73  l;         /* Us
4e3b0 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69  ed when p4type i
4e3c0 73 20 50 34 5f 52 45 41 4c 20 2a 2f 0a 20 20 20  s P4_REAL */.   
4e3d0 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 3b   FuncDef *pFunc;
4e3e0 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20          /* Used 
4e3f0 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50  when p4type is P
4e400 34 5f 46 55 4e 43 44 45 46 20 2a 2f 0a 20 20 20  4_FUNCDEF */.   
4e410 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65   VdbeFunc *pVdbe
4e420 46 75 6e 63 3b 20 20 20 2f 2a 20 55 73 65 64 20  Func;   /* Used 
4e430 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50  when p4type is P
4e440 34 5f 56 44 42 45 46 55 4e 43 20 2a 2f 0a 20 20  4_VDBEFUNC */.  
4e450 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
4e460 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64  ;        /* Used
4e470 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20   when p4type is 
4e480 50 34 5f 43 4f 4c 4c 53 45 51 20 2a 2f 0a 20 20  P4_COLLSEQ */.  
4e490 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20    Mem *pMem;    
4e4a0 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64           /* Used
4e4b0 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20   when p4type is 
4e4c0 50 34 5f 4d 45 4d 20 2a 2f 0a 20 20 20 20 56 54  P4_MEM */.    VT
4e4d0 61 62 6c 65 20 2a 70 56 74 61 62 3b 20 20 20 20  able *pVtab;    
4e4e0 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65       /* Used whe
4e4f0 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 56  n p4type is P4_V
4e500 54 41 42 20 2a 2f 0a 20 20 20 20 4b 65 79 49 6e  TAB */.    KeyIn
4e510 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 20  fo *pKeyInfo;   
4e520 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70    /* Used when p
4e530 34 74 79 70 65 20 69 73 20 50 34 5f 4b 45 59 49  4type is P4_KEYI
4e540 4e 46 4f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a  NFO */.    int *
4e550 61 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ai;             
4e560 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70    /* Used when p
4e570 34 74 79 70 65 20 69 73 20 50 34 5f 49 4e 54 41  4type is P4_INTA
4e580 52 52 41 59 20 2a 2f 0a 20 20 20 20 53 75 62 50  RRAY */.    SubP
4e590 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d  rogram *pProgram
4e5a0 3b 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20  ;  /* Used when 
4e5b0 70 34 74 79 70 65 20 69 73 20 50 34 5f 53 55 42  p4type is P4_SUB
4e5c0 50 52 4f 47 52 41 4d 20 2a 2f 0a 20 20 7d 20 70  PROGRAM */.  } p
4e5d0 34 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  4;.#ifdef SQLITE
4e5e0 5f 44 45 42 55 47 0a 20 20 63 68 61 72 20 2a 7a  _DEBUG.  char *z
4e5f0 43 6f 6d 6d 65 6e 74 3b 20 20 20 20 20 20 20 20  Comment;        
4e600 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 74 6f 20    /* Comment to 
4e610 69 6d 70 72 6f 76 65 20 72 65 61 64 61 62 69 6c  improve readabil
4e620 69 74 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  ity */.#endif.#i
4e630 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c  fdef VDBE_PROFIL
4e640 45 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20  E.  int cnt;    
4e650 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4e660 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20  Number of times 
4e670 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  this instruction
4e680 20 77 61 73 20 65 78 65 63 75 74 65 64 20 2a 2f   was executed */
4e690 0a 20 20 75 36 34 20 63 79 63 6c 65 73 3b 20 20  .  u64 cycles;  
4e6a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
4e6b0 6f 74 61 6c 20 74 69 6d 65 20 73 70 65 6e 74 20  otal time spent 
4e6c0 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 69  executing this i
4e6d0 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 23 65  nstruction */.#e
4e6e0 6e 64 69 66 0a 7d 3b 0a 74 79 70 65 64 65 66 20  ndif.};.typedef 
4e6f0 73 74 72 75 63 74 20 56 64 62 65 4f 70 20 56 64  struct VdbeOp Vd
4e700 62 65 4f 70 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20  beOp;.../*.** A 
4e710 73 75 62 2d 72 6f 75 74 69 6e 65 20 75 73 65 64  sub-routine used
4e720 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20   to implement a 
4e730 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e  trigger program.
4e740 0a 2a 2f 0a 73 74 72 75 63 74 20 53 75 62 50 72  .*/.struct SubPr
4e750 6f 67 72 61 6d 20 7b 0a 20 20 56 64 62 65 4f 70  ogram {.  VdbeOp
4e760 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20 20 20   *aOp;          
4e770 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79          /* Array
4e780 20 6f 66 20 6f 70 63 6f 64 65 73 20 66 6f 72 20   of opcodes for 
4e790 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20  sub-program */. 
4e7a0 20 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20 20   int nOp;       
4e7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4e7c0 2a 20 45 6c 65 6d 65 6e 74 73 20 69 6e 20 61 4f  * Elements in aO
4e7d0 70 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65  p[] */.  int nMe
4e7e0 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
4e7f0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
4e800 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73   of memory cells
4e810 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69   required */.  i
4e820 6e 74 20 6e 43 73 72 3b 20 20 20 20 20 20 20 20  nt nCsr;        
4e830 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4e840 4e 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72  Number of cursor
4e850 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20  s required */.  
4e860 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20  int nRef;       
4e870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4e880 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74   Number of point
4e890 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 72 75  ers to this stru
4e8a0 63 74 75 72 65 20 2a 2f 0a 20 20 76 6f 69 64 20  cture */.  void 
4e8b0 2a 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20  *token;         
4e8c0 20 20 20 20 20 20 20 20 20 2f 2a 20 69 64 20 74           /* id t
4e8d0 68 61 74 20 6d 61 79 20 62 65 20 75 73 65 64 20  hat may be used 
4e8e0 74 6f 20 72 65 63 75 72 73 69 76 65 20 74 72 69  to recursive tri
4e8f0 67 67 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  ggers */.};../*.
4e900 2a 2a 20 41 20 73 6d 61 6c 6c 65 72 20 76 65 72  ** A smaller ver
4e910 73 69 6f 6e 20 6f 66 20 56 64 62 65 4f 70 20 75  sion of VdbeOp u
4e920 73 65 64 20 66 6f 72 20 74 68 65 20 56 64 62 65  sed for the Vdbe
4e930 41 64 64 4f 70 4c 69 73 74 28 29 20 66 75 6e 63  AddOpList() func
4e940 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20  tion because.** 
4e950 69 74 20 74 61 6b 65 73 20 75 70 20 6c 65 73 73  it takes up less
4e960 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 72 75 63   space..*/.struc
4e970 74 20 56 64 62 65 4f 70 4c 69 73 74 20 7b 0a 20  t VdbeOpList {. 
4e980 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20   u8 opcode;     
4e990 20 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65       /* What ope
4e9a0 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72  ration to perfor
4e9b0 6d 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68  m */.  signed ch
4e9c0 61 72 20 70 31 3b 20 20 20 20 20 2f 2a 20 46 69  ar p1;     /* Fi
4e9d0 72 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  rst operand */. 
4e9e0 20 73 69 67 6e 65 64 20 63 68 61 72 20 70 32 3b   signed char p2;
4e9f0 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70       /* Second p
4ea00 61 72 61 6d 65 74 65 72 20 28 6f 66 74 65 6e 20  arameter (often 
4ea10 74 68 65 20 6a 75 6d 70 20 64 65 73 74 69 6e 61  the jump destina
4ea20 74 69 6f 6e 29 20 2a 2f 0a 20 20 73 69 67 6e 65  tion) */.  signe
4ea30 64 20 63 68 61 72 20 70 33 3b 20 20 20 20 20 2f  d char p3;     /
4ea40 2a 20 54 68 69 72 64 20 70 61 72 61 6d 65 74 65  * Third paramete
4ea50 72 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20  r */.};.typedef 
4ea60 73 74 72 75 63 74 20 56 64 62 65 4f 70 4c 69 73  struct VdbeOpLis
4ea70 74 20 56 64 62 65 4f 70 4c 69 73 74 3b 0a 0a 2f  t VdbeOpList;../
4ea80 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c  *.** Allowed val
4ea90 75 65 73 20 6f 66 20 56 64 62 65 4f 70 2e 70 34  ues of VdbeOp.p4
4eaa0 74 79 70 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  type.*/.#define 
4eab0 50 34 5f 4e 4f 54 55 53 45 44 20 20 20 20 30 20  P4_NOTUSED    0 
4eac0 20 20 2f 2a 20 54 68 65 20 50 34 20 70 61 72 61    /* The P4 para
4ead0 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 75 73 65  meter is not use
4eae0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f  d */.#define P4_
4eaf0 44 59 4e 41 4d 49 43 20 20 28 2d 31 29 20 20 2f  DYNAMIC  (-1)  /
4eb00 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 73  * Pointer to a s
4eb10 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66  tring obtained f
4eb20 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  rom sqliteMalloc
4eb30 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34  () */.#define P4
4eb40 5f 53 54 41 54 49 43 20 20 20 28 2d 32 29 20 20  _STATIC   (-2)  
4eb50 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20  /* Pointer to a 
4eb60 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f  static string */
4eb70 0a 23 64 65 66 69 6e 65 20 50 34 5f 43 4f 4c 4c  .#define P4_COLL
4eb80 53 45 51 20 20 28 2d 34 29 20 20 2f 2a 20 50 34  SEQ  (-4)  /* P4
4eb90 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
4eba0 20 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63   a CollSeq struc
4ebb0 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ture */.#define 
4ebc0 50 34 5f 46 55 4e 43 44 45 46 20 20 28 2d 35 29  P4_FUNCDEF  (-5)
4ebd0 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69    /* P4 is a poi
4ebe0 6e 74 65 72 20 74 6f 20 61 20 46 75 6e 63 44 65  nter to a FuncDe
4ebf0 66 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23  f structure */.#
4ec00 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46  define P4_KEYINF
4ec10 4f 20 20 28 2d 36 29 20 20 2f 2a 20 50 34 20 69  O  (-6)  /* P4 i
4ec20 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
4ec30 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75   KeyInfo structu
4ec40 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34  re */.#define P4
4ec50 5f 56 44 42 45 46 55 4e 43 20 28 2d 37 29 20 20  _VDBEFUNC (-7)  
4ec60 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74  /* P4 is a point
4ec70 65 72 20 74 6f 20 61 20 56 64 62 65 46 75 6e 63  er to a VdbeFunc
4ec80 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64   structure */.#d
4ec90 65 66 69 6e 65 20 50 34 5f 4d 45 4d 20 20 20 20  efine P4_MEM    
4eca0 20 20 28 2d 38 29 20 20 2f 2a 20 50 34 20 69 73    (-8)  /* P4 is
4ecb0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
4ecc0 4d 65 6d 2a 20 20 20 20 73 74 72 75 63 74 75 72  Mem*    structur
4ecd0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f  e */.#define P4_
4ece0 54 52 41 4e 53 49 45 4e 54 20 28 2d 39 29 20 2f  TRANSIENT (-9) /
4ecf0 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65  * P4 is a pointe
4ed00 72 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74  r to a transient
4ed10 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69   string */.#defi
4ed20 6e 65 20 50 34 5f 56 54 41 42 20 20 20 20 20 28  ne P4_VTAB     (
4ed30 2d 31 30 29 20 2f 2a 20 50 34 20 69 73 20 61 20  -10) /* P4 is a 
4ed40 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71  pointer to an sq
4ed50 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63  lite3_vtab struc
4ed60 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ture */.#define 
4ed70 50 34 5f 4d 50 52 49 4e 54 46 20 20 28 2d 31 31  P4_MPRINTF  (-11
4ed80 29 20 2f 2a 20 50 34 20 69 73 20 61 20 73 74 72  ) /* P4 is a str
4ed90 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ing obtained fro
4eda0 6d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  m sqlite3_mprint
4edb0 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  f() */.#define P
4edc0 34 5f 52 45 41 4c 20 20 20 20 20 28 2d 31 32 29  4_REAL     (-12)
4edd0 20 2f 2a 20 50 34 20 69 73 20 61 20 36 34 2d 62   /* P4 is a 64-b
4ede0 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  it floating poin
4edf0 74 20 76 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69  t value */.#defi
4ee00 6e 65 20 50 34 5f 49 4e 54 36 34 20 20 20 20 28  ne P4_INT64    (
4ee10 2d 31 33 29 20 2f 2a 20 50 34 20 69 73 20 61 20  -13) /* P4 is a 
4ee20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e  64-bit signed in
4ee30 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65  teger */.#define
4ee40 20 50 34 5f 49 4e 54 33 32 20 20 20 20 28 2d 31   P4_INT32    (-1
4ee50 34 29 20 2f 2a 20 50 34 20 69 73 20 61 20 33 32  4) /* P4 is a 32
4ee60 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65  -bit signed inte
4ee70 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ger */.#define P
4ee80 34 5f 49 4e 54 41 52 52 41 59 20 28 2d 31 35 29  4_INTARRAY (-15)
4ee90 20 2f 2a 20 50 34 20 69 73 20 61 20 76 65 63 74   /* P4 is a vect
4eea0 6f 72 20 6f 66 20 33 32 2d 62 69 74 20 69 6e 74  or of 32-bit int
4eeb0 65 67 65 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65  egers */.#define
4eec0 20 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 20 20   P4_SUBPROGRAM  
4eed0 28 2d 31 38 29 20 2f 2a 20 50 34 20 69 73 20 61  (-18) /* P4 is a
4eee0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 53 75   pointer to a Su
4eef0 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63 74 75  bProgram structu
4ef00 72 65 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 20 61  re */../* When a
4ef10 64 64 69 6e 67 20 61 20 50 34 20 61 72 67 75 6d  dding a P4 argum
4ef20 65 6e 74 20 75 73 69 6e 67 20 50 34 5f 4b 45 59  ent using P4_KEY
4ef30 49 4e 46 4f 2c 20 61 20 63 6f 70 79 20 6f 66 20  INFO, a copy of 
4ef40 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  the KeyInfo stru
4ef50 63 74 75 72 65 0a 2a 2a 20 69 73 20 6d 61 64 65  cture.** is made
4ef60 2e 20 20 54 68 61 74 20 63 6f 70 79 20 69 73 20  .  That copy is 
4ef70 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56  freed when the V
4ef80 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64  dbe is finalized
4ef90 2e 20 20 42 75 74 20 69 66 20 74 68 65 0a 2a 2a  .  But if the.**
4efa0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 50 34 5f   argument is P4_
4efb0 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 2c  KEYINFO_HANDOFF,
4efc0 20 74 68 65 20 70 61 73 73 65 64 20 69 6e 20 70   the passed in p
4efd0 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 2e 20  ointer is used. 
4efe0 20 49 74 20 73 74 69 6c 6c 0a 2a 2a 20 67 65 74   It still.** get
4eff0 73 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65  s freed when the
4f000 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a   Vdbe is finaliz
4f010 65 64 20 73 6f 20 69 74 20 73 74 69 6c 6c 20 73  ed so it still s
4f020 68 6f 75 6c 64 20 62 65 20 6f 62 74 61 69 6e 65  hould be obtaine
4f030 64 0a 2a 2a 20 66 72 6f 6d 20 61 20 73 69 6e 67  d.** from a sing
4f040 6c 65 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28  le sqliteMalloc(
4f050 29 2e 20 20 42 75 74 20 6e 6f 20 63 6f 70 79 20  ).  But no copy 
4f060 69 73 20 6d 61 64 65 20 61 6e 64 20 74 68 65 20  is made and the 
4f070 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74  calling.** funct
4f080 69 6f 6e 20 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a  ion should *not*
4f090 20 74 72 79 20 74 6f 20 66 72 65 65 20 74 68 65   try to free the
4f0a0 20 4b 65 79 49 6e 66 6f 2e 0a 2a 2f 0a 23 64 65   KeyInfo..*/.#de
4f0b0 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  fine P4_KEYINFO_
4f0c0 48 41 4e 44 4f 46 46 20 28 2d 31 36 29 0a 23 64  HANDOFF (-16).#d
4f0d0 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f  efine P4_KEYINFO
4f0e0 5f 53 54 41 54 49 43 20 20 28 2d 31 37 29 0a 0a  _STATIC  (-17)..
4f0f0 2f 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65 2e 61  /*.** The Vdbe.a
4f100 43 6f 6c 4e 61 6d 65 20 61 72 72 61 79 20 63 6f  ColName array co
4f110 6e 74 61 69 6e 73 20 35 6e 20 4d 65 6d 20 73 74  ntains 5n Mem st
4f120 72 75 63 74 75 72 65 73 2c 20 77 68 65 72 65 20  ructures, where 
4f130 6e 20 69 73 20 74 68 65 20 0a 2a 2a 20 6e 75 6d  n is the .** num
4f140 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f  ber of columns o
4f150 66 20 64 61 74 61 20 72 65 74 75 72 6e 65 64 20  f data returned 
4f160 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  by the statement
4f170 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 4f 4c  ..*/.#define COL
4f180 4e 41 4d 45 5f 4e 41 4d 45 20 20 20 20 20 30 0a  NAME_NAME     0.
4f190 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f  #define COLNAME_
4f1a0 44 45 43 4c 54 59 50 45 20 31 0a 23 64 65 66 69  DECLTYPE 1.#defi
4f1b0 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42  ne COLNAME_DATAB
4f1c0 41 53 45 20 32 0a 23 64 65 66 69 6e 65 20 43 4f  ASE 2.#define CO
4f1d0 4c 4e 41 4d 45 5f 54 41 42 4c 45 20 20 20 20 33  LNAME_TABLE    3
4f1e0 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45  .#define COLNAME
4f1f0 5f 43 4f 4c 55 4d 4e 20 20 20 34 0a 23 69 66 64  _COLUMN   4.#ifd
4f200 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
4f210 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41  _COLUMN_METADATA
4f220 0a 23 20 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d  .# define COLNAM
4f230 45 5f 4e 20 20 20 20 20 20 20 20 35 20 20 20 20  E_N        5    
4f240 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 43    /* Number of C
4f250 4f 4c 4e 41 4d 45 5f 78 78 78 20 73 79 6d 62 6f  OLNAME_xxx symbo
4f260 6c 73 20 2a 2f 0a 23 65 6c 73 65 0a 23 20 69 66  ls */.#else.# if
4f270 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
4f280 44 45 43 4c 54 59 50 45 0a 23 20 20 20 64 65 66  DECLTYPE.#   def
4f290 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20  ine COLNAME_N   
4f2a0 20 20 20 31 20 20 20 20 20 20 2f 2a 20 53 74 6f     1      /* Sto
4f2b0 72 65 20 6f 6e 6c 79 20 74 68 65 20 6e 61 6d 65  re only the name
4f2c0 20 2a 2f 0a 23 20 65 6c 73 65 0a 23 20 20 20 64   */.# else.#   d
4f2d0 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20  efine COLNAME_N 
4f2e0 20 20 20 20 20 32 20 20 20 20 20 20 2f 2a 20 53       2      /* S
4f2f0 74 6f 72 65 20 74 68 65 20 6e 61 6d 65 20 61 6e  tore the name an
4f300 64 20 64 65 63 6c 74 79 70 65 20 2a 2f 0a 23 20  d decltype */.# 
4f310 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a  endif.#endif../*
4f320 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
4f330 67 20 6d 61 63 72 6f 20 63 6f 6e 76 65 72 74 73  g macro converts
4f340 20 61 20 72 65 6c 61 74 69 76 65 20 61 64 64 72   a relative addr
4f350 65 73 73 20 69 6e 20 74 68 65 20 70 32 20 66 69  ess in the p2 fi
4f360 65 6c 64 0a 2a 2a 20 6f 66 20 61 20 56 64 62 65  eld.** of a Vdbe
4f370 4f 70 20 73 74 72 75 63 74 75 72 65 20 69 6e 74  Op structure int
4f380 6f 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d  o a negative num
4f390 62 65 72 20 73 6f 20 74 68 61 74 20 0a 2a 2a 20  ber so that .** 
4f3a0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
4f3b0 4c 69 73 74 28 29 20 6b 6e 6f 77 73 20 74 68 61  List() knows tha
4f3c0 74 20 74 68 65 20 61 64 64 72 65 73 73 20 69 73  t the address is
4f3d0 20 72 65 6c 61 74 69 76 65 2e 20 20 43 61 6c 6c   relative.  Call
4f3e0 69 6e 67 0a 2a 2a 20 74 68 65 20 6d 61 63 72 6f  ing.** the macro
4f3f0 20 61 67 61 69 6e 20 72 65 73 74 6f 72 65 73 20   again restores 
4f400 74 68 65 20 61 64 64 72 65 73 73 2e 0a 2a 2f 0a  the address..*/.
4f410 23 64 65 66 69 6e 65 20 41 44 44 52 28 58 29 20  #define ADDR(X) 
4f420 20 28 2d 31 2d 28 58 29 29 0a 0a 2f 2a 0a 2a 2a   (-1-(X))../*.**
4f430 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 73 63   The makefile sc
4f440 61 6e 73 20 74 68 65 20 76 64 62 65 2e 63 20 73  ans the vdbe.c s
4f450 6f 75 72 63 65 20 66 69 6c 65 20 61 6e 64 20 63  ource file and c
4f460 72 65 61 74 65 73 20 74 68 65 20 22 6f 70 63 6f  reates the "opco
4f470 64 65 73 2e 68 22 0a 2a 2a 20 68 65 61 64 65 72  des.h".** header
4f480 20 66 69 6c 65 20 74 68 61 74 20 64 65 66 69 6e   file that defin
4f490 65 73 20 61 20 6e 75 6d 62 65 72 20 66 6f 72 20  es a number for 
4f4a0 65 61 63 68 20 6f 70 63 6f 64 65 20 75 73 65 64  each opcode used
4f4b0 20 62 79 20 74 68 65 20 56 44 42 45 2e 0a 2a 2f   by the VDBE..*/
4f4c0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
4f4d0 20 49 6e 63 6c 75 64 65 20 6f 70 63 6f 64 65 73   Include opcodes
4f4e0 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
4f4f0 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a   of vdbe.h *****
4f500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
4f510 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
4f520 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63 6f   Begin file opco
4f530 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  des.h **********
4f540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
4f560 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c  ./* Automaticall
4f570 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44 6f  y generated.  Do
4f580 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a 20   not edit */./* 
4f590 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65  See the mkopcode
4f5a0 68 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72  h.awk script for
4f5b0 20 64 65 74 61 69 6c 73 20 2a 2f 0a 23 64 65 66   details */.#def
4f5c0 69 6e 65 20 4f 50 5f 56 4e 65 78 74 20 20 20 20  ine OP_VNext    
4f5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f5e0 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64              1.#d
4f5f0 65 66 69 6e 65 20 4f 50 5f 41 66 66 69 6e 69 74  efine OP_Affinit
4f600 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y               
4f610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a                2.
4f620 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6c 75 6d  #define OP_Colum
4f630 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
4f640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f650 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 74  3.#define OP_Set
4f660 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 20 20 20  Cookie          
4f670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f680 20 20 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53    4.#define OP_S
4f690 65 65 6b 20 20 20 20 20 20 20 20 20 20 20 20 20  eek             
4f6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f6b0 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 4f 50      5.#define OP
4f6c0 5f 52 65 61 6c 20 20 20 20 20 20 20 20 20 20 20  _Real           
4f6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f6e0 20 20 20 20 31 33 30 20 20 20 2f 2a 20 73 61 6d      130   /* sam
4f6f0 65 20 61 73 20 54 4b 5f 46 4c 4f 41 54 20 20 20  e as TK_FLOAT   
4f700 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53   */.#define OP_S
4f710 65 71 75 65 6e 63 65 20 20 20 20 20 20 20 20 20  equence         
4f720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f730 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 4f 50      6.#define OP
4f740 5f 53 61 76 65 70 6f 69 6e 74 20 20 20 20 20 20  _Savepoint      
4f750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f760 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20        7.#define 
4f770 4f 50 5f 47 65 20 20 20 20 20 20 20 20 20 20 20  OP_Ge           
4f780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f790 20 20 20 20 20 20 20 37 38 20 20 20 2f 2a 20 73         78   /* s
4f7a0 61 6d 65 20 61 73 20 54 4b 5f 47 45 20 20 20 20  ame as TK_GE    
4f7b0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
4f7c0 5f 52 6f 77 4b 65 79 20 20 20 20 20 20 20 20 20  _RowKey         
4f7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f7e0 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20        8.#define 
4f7f0 4f 50 5f 53 43 6f 70 79 20 20 20 20 20 20 20 20  OP_SCopy        
4f800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f810 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e          9.#defin
4f820 65 20 4f 50 5f 45 71 20 20 20 20 20 20 20 20 20  e OP_Eq         
4f830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f840 20 20 20 20 20 20 20 20 20 37 34 20 20 20 2f 2a           74   /*
4f850 20 73 61 6d 65 20 61 73 20 54 4b 5f 45 51 20 20   same as TK_EQ  
4f860 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
4f870 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 20 20 20  OP_OpenWrite    
4f880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f890 20 20 20 20 20 20 20 31 30 0a 23 64 65 66 69 6e         10.#defin
4f8a0 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 20 20 20  e OP_NotNull    
4f8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f8c0 20 20 20 20 20 20 20 20 20 37 32 20 20 20 2f 2a           72   /*
4f8d0 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 4e   same as TK_NOTN
4f8e0 55 4c 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ULL  */.#define 
4f8f0 4f 50 5f 49 66 20 20 20 20 20 20 20 20 20 20 20  OP_If           
4f900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f910 20 20 20 20 20 20 20 31 31 0a 23 64 65 66 69 6e         11.#defin
4f920 65 20 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20 20  e OP_ToInt      
4f930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f940 20 20 20 20 20 20 20 20 31 34 34 20 20 20 2f 2a          144   /*
4f950 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 49   same as TK_TO_I
4f960 4e 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  NT   */.#define 
4f970 4f 50 5f 53 74 72 69 6e 67 38 20 20 20 20 20 20  OP_String8      
4f980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f990 20 20 20 20 20 20 20 39 34 20 20 20 2f 2a 20 73         94   /* s
4f9a0 61 6d 65 20 61 73 20 54 4b 5f 53 54 52 49 4e 47  ame as TK_STRING
4f9b0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
4f9c0 5f 43 6f 6c 6c 53 65 71 20 20 20 20 20 20 20 20  _CollSeq        
4f9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f9e0 20 20 20 20 20 31 32 0a 23 64 65 66 69 6e 65 20       12.#define 
4f9f0 4f 50 5f 4f 70 65 6e 52 65 61 64 20 20 20 20 20  OP_OpenRead     
4fa00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fa10 20 20 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e         13.#defin
4fa20 65 20 4f 50 5f 45 78 70 69 72 65 20 20 20 20 20  e OP_Expire     
4fa30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fa40 20 20 20 20 20 20 20 20 20 31 34 0a 23 64 65 66           14.#def
4fa50 69 6e 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69  ine OP_AutoCommi
4fa60 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
4fa70 20 20 20 20 20 20 20 20 20 20 20 31 35 0a 23 64             15.#d
4fa80 65 66 69 6e 65 20 4f 50 5f 47 74 20 20 20 20 20  efine OP_Gt     
4fa90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4faa0 20 20 20 20 20 20 20 20 20 20 20 20 20 37 35 20               75 
4fab0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
4fac0 47 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  GT       */.#def
4fad0 69 6e 65 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74  ine OP_Pagecount
4fae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4faf0 20 20 20 20 20 20 20 20 20 20 20 31 36 0a 23 64             16.#d
4fb00 65 66 69 6e 65 20 4f 50 5f 49 6e 74 65 67 72 69  efine OP_Integri
4fb10 74 79 43 6b 20 20 20 20 20 20 20 20 20 20 20 20  tyCk            
4fb20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 37 0a               17.
4fb30 23 64 65 66 69 6e 65 20 4f 50 5f 53 6f 72 74 20  #define OP_Sort 
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 20 20 20 20 20 20 20 20 31                 1
4fb60 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 70  8.#define OP_Cop
4fb70 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y               
4fb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fb90 20 32 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54   20.#define OP_T
4fba0 72 61 63 65 20 20 20 20 20 20 20 20 20 20 20 20  race            
4fbb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fbc0 20 20 20 32 31 0a 23 64 65 66 69 6e 65 20 4f 50     21.#define OP
4fbd0 5f 46 75 6e 63 74 69 6f 6e 20 20 20 20 20 20 20  _Function       
4fbe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fbf0 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e 65 20       22.#define 
4fc00 4f 50 5f 49 66 4e 65 67 20 20 20 20 20 20 20 20  OP_IfNeg        
4fc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fc20 20 20 20 20 20 20 20 32 33 0a 23 64 65 66 69 6e         23.#defin
4fc30 65 20 4f 50 5f 41 6e 64 20 20 20 20 20 20 20 20  e OP_And        
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 20 20 36 37 20 20 20 2f 2a           67   /*
4fc60 20 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e 44 20   same as TK_AND 
4fc70 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
4fc80 4f 50 5f 53 75 62 74 72 61 63 74 20 20 20 20 20  OP_Subtract     
4fc90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fca0 20 20 20 20 20 20 20 38 35 20 20 20 2f 2a 20 73         85   /* s
4fcb0 61 6d 65 20 61 73 20 54 4b 5f 4d 49 4e 55 53 20  ame as TK_MINUS 
4fcc0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
4fcd0 5f 4e 6f 6f 70 20 20 20 20 20 20 20 20 20 20 20  _Noop           
4fce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fcf0 20 20 20 20 20 32 34 0a 23 64 65 66 69 6e 65 20       24.#define 
4fd00 4f 50 5f 50 72 6f 67 72 61 6d 20 20 20 20 20 20  OP_Program      
4fd10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd20 20 20 20 20 20 20 20 32 35 0a 23 64 65 66 69 6e         25.#defin
4fd30 65 20 4f 50 5f 52 65 74 75 72 6e 20 20 20 20 20  e OP_Return     
4fd40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd50 20 20 20 20 20 20 20 20 20 32 36 0a 23 64 65 66           26.#def
4fd60 69 6e 65 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72  ine OP_Remainder
4fd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd80 20 20 20 20 20 20 20 20 20 20 20 38 38 20 20 20             88   
4fd90 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 45  /* same as TK_RE
4fda0 4d 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  M      */.#defin
4fdb0 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 20 20  e OP_NewRowid   
4fdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fdd0 20 20 20 20 20 20 20 20 20 32 37 0a 23 64 65 66           27.#def
4fde0 69 6e 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 20  ine OP_Multiply 
4fdf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fe00 20 20 20 20 20 20 20 20 20 20 20 38 36 20 20 20             86   
4fe10 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54  /* same as TK_ST
4fe20 41 52 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  AR     */.#defin
4fe30 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 20 20  e OP_Variable   
4fe40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fe50 20 20 20 20 20 20 20 20 20 32 38 0a 23 64 65 66           28.#def
4fe60 69 6e 65 20 4f 50 5f 53 74 72 69 6e 67 20 20 20  ine OP_String   
4fe70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fe80 20 20 20 20 20 20 20 20 20 20 20 32 39 0a 23 64             29.#d
4fe90 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c 41 66 66  efine OP_RealAff
4fea0 69 6e 69 74 79 20 20 20 20 20 20 20 20 20 20 20  inity           
4feb0 20 20 20 20 20 20 20 20 20 20 20 20 20 33 30 0a               30.
4fec0 23 64 65 66 69 6e 65 20 4f 50 5f 56 52 65 6e 61  #define OP_VRena
4fed0 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  me              
4fee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
4fef0 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 61 72  1.#define OP_Par
4ff00 73 65 53 63 68 65 6d 61 20 20 20 20 20 20 20 20  seSchema        
4ff10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ff20 20 33 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56   32.#define OP_V
4ff30 4f 70 65 6e 20 20 20 20 20 20 20 20 20 20 20 20  Open            
4ff40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ff50 20 20 20 33 33 0a 23 64 65 66 69 6e 65 20 4f 50     33.#define OP
4ff60 5f 43 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20  _Close          
4ff70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ff80 20 20 20 20 20 33 34 0a 23 64 65 66 69 6e 65 20       34.#define 
4ff90 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78 20 20  OP_CreateIndex  
4ffa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ffb0 20 20 20 20 20 20 20 33 35 0a 23 64 65 66 69 6e         35.#defin
4ffc0 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 20 20  e OP_IsUnique   
4ffd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ffe0 20 20 20 20 20 20 20 20 20 33 36 0a 23 64 65 66           36.#def
4fff0 69 6e 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20  ine OP_NotFound 
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 20 20 33 37 0a 23 64             37.#d
50020 65 66 69 6e 65 20 4f 50 5f 49 6e 74 36 34 20 20  efine OP_Int64  
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 20 20 33 38 0a               38.
50050 23 64 65 66 69 6e 65 20 4f 50 5f 4d 75 73 74 42  #define OP_MustB
50060 65 49 6e 74 20 20 20 20 20 20 20 20 20 20 20 20  eInt            
50070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
50080 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c  9.#define OP_Hal
50090 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
500a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
500b0 20 34 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52   40.#define OP_R
500c0 6f 77 69 64 20 20 20 20 20 20 20 20 20 20 20 20  owid            
500d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
500e0 20 20 20 34 31 0a 23 64 65 66 69 6e 65 20 4f 50     41.#define OP
500f0 5f 49 64 78 4c 54 20 20 20 20 20 20 20 20 20 20  _IdxLT          
50100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50110 20 20 20 20 20 34 32 0a 23 64 65 66 69 6e 65 20       42.#define 
50120 4f 50 5f 41 64 64 49 6d 6d 20 20 20 20 20 20 20  OP_AddImm       
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 34 33 0a 23 64 65 66 69 6e         43.#defin
50150 65 20 4f 50 5f 52 6f 77 44 61 74 61 20 20 20 20  e OP_RowData    
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 34 34 0a 23 64 65 66           44.#def
50180 69 6e 65 20 4f 50 5f 4d 65 6d 4d 61 78 20 20 20  ine OP_MemMax   
50190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
501a0 20 20 20 20 20 20 20 20 20 20 20 34 35 0a 23 64             45.#d
501b0 65 66 69 6e 65 20 4f 50 5f 4f 72 20 20 20 20 20  efine OP_Or     
501c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
501d0 20 20 20 20 20 20 20 20 20 20 20 20 20 36 36 20               66 
501e0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
501f0 4f 52 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  OR       */.#def
50200 69 6e 65 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73  ine OP_NotExists
50210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50220 20 20 20 20 20 20 20 20 20 20 20 34 36 0a 23 64             46.#d
50230 65 66 69 6e 65 20 4f 50 5f 47 6f 73 75 62 20 20  efine OP_Gosub  
50240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50250 20 20 20 20 20 20 20 20 20 20 20 20 20 34 37 0a               47.
50260 23 64 65 66 69 6e 65 20 4f 50 5f 44 69 76 69 64  #define OP_Divid
50270 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
50280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
50290 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  7   /* same as T
502a0 4b 5f 53 4c 41 53 48 20 20 20 20 2a 2f 0a 23 64  K_SLASH    */.#d
502b0 65 66 69 6e 65 20 4f 50 5f 49 6e 74 65 67 65 72  efine OP_Integer
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 20 20 34 38 0a               48.
502e0 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 4e 75 6d  #define OP_ToNum
502f0 65 72 69 63 20 20 20 20 20 20 20 20 20 20 20 20  eric            
50300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34                14
50310 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  3   /* same as T
50320 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2a 2f 0a 23  K_TO_NUMERIC*/.#
50330 64 65 66 69 6e 65 20 4f 50 5f 50 72 65 76 20 20  define OP_Prev  
50340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 39                49
50360 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 53  .#define OP_RowS
50370 65 74 52 65 61 64 20 20 20 20 20 20 20 20 20 20  etRead          
50380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50390 35 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f  50.#define OP_Co
503a0 6e 63 61 74 20 20 20 20 20 20 20 20 20 20 20 20  ncat            
503b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
503c0 20 20 38 39 20 20 20 2f 2a 20 73 61 6d 65 20 61    89   /* same a
503d0 73 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20 2a 2f  s TK_CONCAT   */
503e0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 53  .#define OP_RowS
503f0 65 74 41 64 64 20 20 20 20 20 20 20 20 20 20 20  etAdd           
50400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50410 35 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69  51.#define OP_Bi
50420 74 41 6e 64 20 20 20 20 20 20 20 20 20 20 20 20  tAnd            
50430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50440 20 20 38 30 20 20 20 2f 2a 20 73 61 6d 65 20 61    80   /* same a
50450 73 20 54 4b 5f 42 49 54 41 4e 44 20 20 20 2a 2f  s TK_BITAND   */
50460 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 6f 6c  .#define OP_VCol
50470 75 6d 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  umn             
50480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50490 35 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 72  52.#define OP_Cr
504a0 65 61 74 65 54 61 62 6c 65 20 20 20 20 20 20 20  eateTable       
504b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
504c0 20 20 35 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f    53.#define OP_
504d0 4c 61 73 74 20 20 20 20 20 20 20 20 20 20 20 20  Last            
504e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
504f0 20 20 20 20 35 34 0a 23 64 65 66 69 6e 65 20 4f      54.#define O
50500 50 5f 53 65 65 6b 4c 65 20 20 20 20 20 20 20 20  P_SeekLe        
50510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50520 20 20 20 20 20 20 35 35 0a 23 64 65 66 69 6e 65        55.#define
50530 20 4f 50 5f 49 73 4e 75 6c 6c 20 20 20 20 20 20   OP_IsNull      
50540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50550 20 20 20 20 20 20 20 20 37 31 20 20 20 2f 2a 20          71   /* 
50560 73 61 6d 65 20 61 73 20 54 4b 5f 49 53 4e 55 4c  same as TK_ISNUL
50570 4c 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  L   */.#define O
50580 50 5f 49 6e 63 72 56 61 63 75 75 6d 20 20 20 20  P_IncrVacuum    
50590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
505a0 20 20 20 20 20 20 35 36 0a 23 64 65 66 69 6e 65        56.#define
505b0 20 4f 50 5f 49 64 78 52 6f 77 69 64 20 20 20 20   OP_IdxRowid    
505c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
505d0 20 20 20 20 20 20 20 20 35 37 0a 23 64 65 66 69          57.#defi
505e0 6e 65 20 4f 50 5f 53 68 69 66 74 52 69 67 68 74  ne OP_ShiftRight
505f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50600 20 20 20 20 20 20 20 20 20 20 38 33 20 20 20 2f            83   /
50610 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 53 48  * same as TK_RSH
50620 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  IFT   */.#define
50630 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 20 20   OP_ResetCount  
50640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50650 20 20 20 20 20 20 20 20 35 38 0a 23 64 65 66 69          58.#defi
50660 6e 65 20 4f 50 5f 59 69 65 6c 64 20 20 20 20 20  ne OP_Yield     
50670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50680 20 20 20 20 20 20 20 20 20 20 35 39 0a 23 64 65            59.#de
50690 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54 72 69 67  fine OP_DropTrig
506a0 67 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20  ger             
506b0 20 20 20 20 20 20 20 20 20 20 20 20 36 30 0a 23              60.#
506c0 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 49 6e  define OP_DropIn
506d0 64 65 78 20 20 20 20 20 20 20 20 20 20 20 20 20  dex             
506e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 31                61
506f0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 61 72 61  .#define OP_Para
50700 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m               
50710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50720 36 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64  62.#define OP_Id
50730 78 47 45 20 20 20 20 20 20 20 20 20 20 20 20 20  xGE             
50740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50750 20 20 36 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f    63.#define OP_
50760 49 64 78 44 65 6c 65 74 65 20 20 20 20 20 20 20  IdxDelete       
50770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50780 20 20 20 20 36 34 0a 23 64 65 66 69 6e 65 20 4f      64.#define O
50790 50 5f 56 61 63 75 75 6d 20 20 20 20 20 20 20 20  P_Vacuum        
507a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
507b0 20 20 20 20 20 20 36 35 0a 23 64 65 66 69 6e 65        65.#define
507c0 20 4f 50 5f 49 66 4e 6f 74 20 20 20 20 20 20 20   OP_IfNot       
507d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
507e0 20 20 20 20 20 20 20 20 36 38 0a 23 64 65 66 69          68.#defi
507f0 6e 65 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 20  ne OP_DropTable 
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 20 20 36 39 0a 23 64 65            69.#de
50820 66 69 6e 65 20 4f 50 5f 53 65 65 6b 4c 74 20 20  fine OP_SeekLt  
50830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50840 20 20 20 20 20 20 20 20 20 20 20 20 37 30 0a 23              70.#
50850 64 65 66 69 6e 65 20 4f 50 5f 4d 61 6b 65 52 65  define OP_MakeRe
50860 63 6f 72 64 20 20 20 20 20 20 20 20 20 20 20 20  cord            
50870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 39                79
50880 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 42 6c  .#define OP_ToBl
50890 6f 62 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ob              
508a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
508b0 34 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  42   /* same as 
508c0 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20 2a 2f 0a 23  TK_TO_BLOB  */.#
508d0 64 65 66 69 6e 65 20 4f 50 5f 52 65 73 75 6c 74  define OP_Result
508e0 52 6f 77 20 20 20 20 20 20 20 20 20 20 20 20 20  Row             
508f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 30                90
50900 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 6c 65  .#define OP_Dele
50910 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  te              
50920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50930 39 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 67  91.#define OP_Ag
50940 67 46 69 6e 61 6c 20 20 20 20 20 20 20 20 20 20  gFinal          
50950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50960 20 20 39 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f    92.#define OP_
50970 43 6f 6d 70 61 72 65 20 20 20 20 20 20 20 20 20  Compare         
50980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50990 20 20 20 20 39 35 0a 23 64 65 66 69 6e 65 20 4f      95.#define O
509a0 50 5f 53 68 69 66 74 4c 65 66 74 20 20 20 20 20  P_ShiftLeft     
509b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
509c0 20 20 20 20 20 20 38 32 20 20 20 2f 2a 20 73 61        82   /* sa
509d0 6d 65 20 61 73 20 54 4b 5f 4c 53 48 49 46 54 20  me as TK_LSHIFT 
509e0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
509f0 47 6f 74 6f 20 20 20 20 20 20 20 20 20 20 20 20  Goto            
50a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a10 20 20 20 20 39 36 0a 23 64 65 66 69 6e 65 20 4f      96.#define O
50a20 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 20 20 20 20  P_TableLock     
50a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a40 20 20 20 20 20 20 39 37 0a 23 64 65 66 69 6e 65        97.#define
50a50 20 4f 50 5f 43 6c 65 61 72 20 20 20 20 20 20 20   OP_Clear       
50a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a70 20 20 20 20 20 20 20 20 39 38 0a 23 64 65 66 69          98.#defi
50a80 6e 65 20 4f 50 5f 4c 65 20 20 20 20 20 20 20 20  ne OP_Le        
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 20 20 37 36 20 20 20 2f            76   /
50ab0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 45 20  * same as TK_LE 
50ac0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
50ad0 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65   OP_VerifyCookie
50ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50af0 20 20 20 20 20 20 20 20 39 39 0a 23 64 65 66 69          99.#defi
50b00 6e 65 20 4f 50 5f 41 67 67 53 74 65 70 20 20 20  ne OP_AggStep   
50b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b20 20 20 20 20 20 20 20 20 20 31 30 30 0a 23 64 65           100.#de
50b30 66 69 6e 65 20 4f 50 5f 54 6f 54 65 78 74 20 20  fine OP_ToText  
50b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b50 20 20 20 20 20 20 20 20 20 20 20 31 34 31 20 20             141  
50b60 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54   /* same as TK_T
50b70 4f 5f 54 45 58 54 20 20 2a 2f 0a 23 64 65 66 69  O_TEXT  */.#defi
50b80 6e 65 20 4f 50 5f 4e 6f 74 20 20 20 20 20 20 20  ne OP_Not       
50b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ba0 20 20 20 20 20 20 20 20 20 20 31 39 20 20 20 2f            19   /
50bb0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54  * same as TK_NOT
50bc0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
50bd0 20 4f 50 5f 54 6f 52 65 61 6c 20 20 20 20 20 20   OP_ToReal      
50be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50bf0 20 20 20 20 20 20 20 31 34 35 20 20 20 2f 2a 20         145   /* 
50c00 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 52 45  same as TK_TO_RE
50c10 41 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  AL  */.#define O
50c20 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20 20 20  P_Transaction   
50c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c40 20 20 20 20 20 31 30 31 0a 23 64 65 66 69 6e 65       101.#define
50c50 20 4f 50 5f 56 46 69 6c 74 65 72 20 20 20 20 20   OP_VFilter     
50c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c70 20 20 20 20 20 20 20 31 30 32 0a 23 64 65 66 69         102.#defi
50c80 6e 65 20 4f 50 5f 4e 65 20 20 20 20 20 20 20 20  ne OP_Ne        
50c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ca0 20 20 20 20 20 20 20 20 20 20 37 33 20 20 20 2f            73   /
50cb0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 45 20  * same as TK_NE 
50cc0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
50cd0 20 4f 50 5f 56 44 65 73 74 72 6f 79 20 20 20 20   OP_VDestroy    
50ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50cf0 20 20 20 20 20 20 20 31 30 33 0a 23 64 65 66 69         103.#defi
50d00 6e 65 20 4f 50 5f 42 69 74 4f 72 20 20 20 20 20  ne OP_BitOr     
50d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d20 20 20 20 20 20 20 20 20 20 20 38 31 20 20 20 2f            81   /
50d30 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54  * same as TK_BIT
50d40 4f 52 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  OR    */.#define
50d50 20 4f 50 5f 4e 65 78 74 20 20 20 20 20 20 20 20   OP_Next        
50d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d70 20 20 20 20 20 20 20 31 30 34 0a 23 64 65 66 69         104.#defi
50d80 6e 65 20 4f 50 5f 43 6f 75 6e 74 20 20 20 20 20  ne OP_Count     
50d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50da0 20 20 20 20 20 20 20 20 20 31 30 35 0a 23 64 65           105.#de
50db0 66 69 6e 65 20 4f 50 5f 49 64 78 49 6e 73 65 72  fine OP_IdxInser
50dc0 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
50dd0 20 20 20 20 20 20 20 20 20 20 20 31 30 36 0a 23             106.#
50de0 64 65 66 69 6e 65 20 4f 50 5f 4c 74 20 20 20 20  define OP_Lt    
50df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 37                77
50e10 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
50e20 5f 4c 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65  _LT       */.#de
50e30 66 69 6e 65 20 4f 50 5f 53 65 65 6b 47 65 20 20  fine OP_SeekGe  
50e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e50 20 20 20 20 20 20 20 20 20 20 20 31 30 37 0a 23             107.#
50e60 64 65 66 69 6e 65 20 4f 50 5f 49 6e 73 65 72 74  define OP_Insert
50e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e80 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 38               108
50e90 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 73 74  .#define OP_Dest
50ea0 72 6f 79 20 20 20 20 20 20 20 20 20 20 20 20 20  roy             
50eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
50ec0 30 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65  09.#define OP_Re
50ed0 61 64 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 20  adCookie        
50ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ef0 20 31 31 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f   110.#define OP_
50f00 52 6f 77 53 65 74 54 65 73 74 20 20 20 20 20 20  RowSetTest      
50f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f20 20 20 20 31 31 31 0a 23 64 65 66 69 6e 65 20 4f     111.#define O
50f30 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 20 20  P_LoadAnalysis  
50f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f50 20 20 20 20 20 31 31 32 0a 23 64 65 66 69 6e 65       112.#define
50f60 20 4f 50 5f 45 78 70 6c 61 69 6e 20 20 20 20 20   OP_Explain     
50f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f80 20 20 20 20 20 20 20 31 31 33 0a 23 64 65 66 69         113.#defi
50f90 6e 65 20 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c  ne OP_HaltIfNull
50fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50fb0 20 20 20 20 20 20 20 20 20 31 31 34 0a 23 64 65           114.#de
50fc0 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 50 73 65 75  fine OP_OpenPseu
50fd0 64 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  do              
50fe0 20 20 20 20 20 20 20 20 20 20 20 31 31 35 0a 23             115.#
50ff0 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 45 70  define OP_OpenEp
51000 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20 20 20  hemeral         
51010 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 36               116
51020 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 75 6c 6c  .#define OP_Null
51030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51050 31 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f  17.#define OP_Mo
51060 76 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ve              
51070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51080 20 31 31 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f   118.#define OP_
51090 42 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20 20  Blob            
510a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
510b0 20 20 20 31 31 39 0a 23 64 65 66 69 6e 65 20 4f     119.#define O
510c0 50 5f 41 64 64 20 20 20 20 20 20 20 20 20 20 20  P_Add           
510d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
510e0 20 20 20 20 20 20 38 34 20 20 20 2f 2a 20 73 61        84   /* sa
510f0 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53 20 20 20  me as TK_PLUS   
51100 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
51110 52 65 77 69 6e 64 20 20 20 20 20 20 20 20 20 20  Rewind          
51120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51130 20 20 20 31 32 30 0a 23 64 65 66 69 6e 65 20 4f     120.#define O
51140 50 5f 53 65 65 6b 47 74 20 20 20 20 20 20 20 20  P_SeekGt        
51150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51160 20 20 20 20 20 31 32 31 0a 23 64 65 66 69 6e 65       121.#define
51170 20 4f 50 5f 56 42 65 67 69 6e 20 20 20 20 20 20   OP_VBegin      
51180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51190 20 20 20 20 20 20 20 31 32 32 0a 23 64 65 66 69         122.#defi
511a0 6e 65 20 4f 50 5f 56 55 70 64 61 74 65 20 20 20  ne OP_VUpdate   
511b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
511c0 20 20 20 20 20 20 20 20 20 31 32 33 0a 23 64 65           123.#de
511d0 66 69 6e 65 20 4f 50 5f 49 66 5a 65 72 6f 20 20  fine OP_IfZero  
511e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
511f0 20 20 20 20 20 20 20 20 20 20 20 31 32 34 0a 23             124.#
51200 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 4e 6f 74  define OP_BitNot
51210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 33                93
51230 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
51240 5f 42 49 54 4e 4f 54 20 20 20 2a 2f 0a 23 64 65  _BITNOT   */.#de
51250 66 69 6e 65 20 4f 50 5f 56 43 72 65 61 74 65 20  fine OP_VCreate 
51260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51270 20 20 20 20 20 20 20 20 20 20 20 31 32 35 0a 23             125.#
51280 64 65 66 69 6e 65 20 4f 50 5f 46 6f 75 6e 64 20  define OP_Found 
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 20 20 31 32 36               126
512b0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 50 6f  .#define OP_IfPo
512c0 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s               
512d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
512e0 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 75  27.#define OP_Nu
512f0 6c 6c 52 6f 77 20 20 20 20 20 20 20 20 20 20 20  llRow           
51300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51310 20 31 32 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f   128.#define OP_
51320 4a 75 6d 70 20 20 20 20 20 20 20 20 20 20 20 20  Jump            
51330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51340 20 20 20 31 32 39 0a 23 64 65 66 69 6e 65 20 4f     129.#define O
51350 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 20 20 20  P_Permutation   
51360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51370 20 20 20 20 20 31 33 31 0a 0a 2f 2a 20 54 68 65       131../* The
51380 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 63 6f 64   following opcod
51390 65 20 76 61 6c 75 65 73 20 61 72 65 20 6e 65 76  e values are nev
513a0 65 72 20 75 73 65 64 20 2a 2f 0a 23 64 65 66 69  er used */.#defi
513b0 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33  ne OP_NotUsed_13
513c0 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2               
513d0 20 20 20 20 20 20 20 20 20 31 33 32 0a 23 64 65           132.#de
513e0 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f  fine OP_NotUsed_
513f0 31 33 33 20 20 20 20 20 20 20 20 20 20 20 20 20  133             
51400 20 20 20 20 20 20 20 20 20 20 20 31 33 33 0a 23             133.#
51410 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65  define OP_NotUse
51420 64 5f 31 33 34 20 20 20 20 20 20 20 20 20 20 20  d_134           
51430 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 34               134
51440 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55  .#define OP_NotU
51450 73 65 64 5f 31 33 35 20 20 20 20 20 20 20 20 20  sed_135         
51460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51470 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f  35.#define OP_No
51480 74 55 73 65 64 5f 31 33 36 20 20 20 20 20 20 20  tUsed_136       
51490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
514a0 20 31 33 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f   136.#define OP_
514b0 4e 6f 74 55 73 65 64 5f 31 33 37 20 20 20 20 20  NotUsed_137     
514c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
514d0 20 20 20 31 33 37 0a 23 64 65 66 69 6e 65 20 4f     137.#define O
514e0 50 5f 4e 6f 74 55 73 65 64 5f 31 33 38 20 20 20  P_NotUsed_138   
514f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51500 20 20 20 20 20 31 33 38 0a 23 64 65 66 69 6e 65       138.#define
51510 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 39 20   OP_NotUsed_139 
51520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51530 20 20 20 20 20 20 20 31 33 39 0a 23 64 65 66 69         139.#defi
51540 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 34  ne OP_NotUsed_14
51550 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
51560 20 20 20 20 20 20 20 20 20 31 34 30 0a 0a 0a 2f           140.../
51570 2a 20 50 72 6f 70 65 72 74 69 65 73 20 73 75 63  * Properties suc
51580 68 20 61 73 20 22 6f 75 74 32 22 20 6f 72 20 22  h as "out2" or "
51590 6a 75 6d 70 22 20 74 68 61 74 20 61 72 65 20 73  jump" that are s
515a0 70 65 63 69 66 69 65 64 20 69 6e 0a 2a 2a 20 63  pecified in.** c
515b0 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77 69 6e  omments followin
515c0 67 20 74 68 65 20 22 63 61 73 65 22 20 66 6f 72  g the "case" for
515d0 20 65 61 63 68 20 6f 70 63 6f 64 65 20 69 6e 20   each opcode in 
515e0 74 68 65 20 76 64 62 65 2e 63 0a 2a 2a 20 61 72  the vdbe.c.** ar
515f0 65 20 65 6e 63 6f 64 65 64 20 69 6e 74 6f 20 62  e encoded into b
51600 69 74 76 65 63 74 6f 72 73 20 61 73 20 66 6f 6c  itvectors as fol
51610 6c 6f 77 73 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65  lows:.*/.#define
51620 20 4f 50 46 4c 47 5f 4a 55 4d 50 20 20 20 20 20   OPFLG_JUMP     
51630 20 20 20 20 20 20 20 30 78 30 30 30 31 20 20 2f         0x0001  /
51640 2a 20 6a 75 6d 70 3a 20 20 50 32 20 68 6f 6c 64  * jump:  P2 hold
51650 73 20 6a 6d 70 20 74 61 72 67 65 74 20 2a 2f 0a  s jmp target */.
51660 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 4f 55  #define OPFLG_OU
51670 54 32 5f 50 52 45 52 45 4c 45 41 53 45 20 30 78  T2_PRERELEASE 0x
51680 30 30 30 32 20 20 2f 2a 20 6f 75 74 32 2d 70 72  0002  /* out2-pr
51690 65 72 65 6c 65 61 73 65 3a 20 2a 2f 0a 23 64 65  erelease: */.#de
516a0 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 31 20 20  fine OPFLG_IN1  
516b0 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30             0x000
516c0 34 20 20 2f 2a 20 69 6e 31 3a 20 20 20 50 31 20  4  /* in1:   P1 
516d0 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f 0a 23  is an input */.#
516e0 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 32  define OPFLG_IN2
516f0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30               0x0
51700 30 30 38 20 20 2f 2a 20 69 6e 32 3a 20 20 20 50  008  /* in2:   P
51710 32 20 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f  2 is an input */
51720 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49  .#define OPFLG_I
51730 4e 33 20 20 20 20 20 20 20 20 20 20 20 20 20 30  N3             0
51740 78 30 30 31 30 20 20 2f 2a 20 69 6e 33 3a 20 20  x0010  /* in3:  
51750 20 50 33 20 69 73 20 61 6e 20 69 6e 70 75 74 20   P3 is an input 
51760 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47  */.#define OPFLG
51770 5f 4f 55 54 33 20 20 20 20 20 20 20 20 20 20 20  _OUT3           
51780 20 30 78 30 30 32 30 20 20 2f 2a 20 6f 75 74 33   0x0020  /* out3
51790 3a 20 20 50 33 20 69 73 20 61 6e 20 6f 75 74 70  :  P3 is an outp
517a0 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  ut */.#define OP
517b0 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20  FLG_INITIALIZER 
517c0 7b 5c 0a 2f 2a 20 20 20 30 20 2a 2f 20 30 78 30  {\./*   0 */ 0x0
517d0 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30  0, 0x01, 0x00, 0
517e0 78 30 30 2c 20 30 78 31 30 2c 20 30 78 30 38 2c  x00, 0x10, 0x08,
517f0 20 30 78 30 32 2c 20 30 78 30 30 2c 5c 0a 2f 2a   0x02, 0x00,\./*
51800 20 20 20 38 20 2a 2f 20 30 78 30 30 2c 20 30 78     8 */ 0x00, 0x
51810 30 34 2c 20 30 78 30 30 2c 20 30 78 30 35 2c 20  04, 0x00, 0x05, 
51820 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
51830 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 31 36 20  , 0x00,\./*  16 
51840 2a 2f 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30  */ 0x02, 0x00, 0
51850 78 30 31 2c 20 30 78 30 34 2c 20 30 78 30 34 2c  x01, 0x04, 0x04,
51860 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
51870 35 2c 5c 0a 2f 2a 20 20 32 34 20 2a 2f 20 30 78  5,\./*  24 */ 0x
51880 30 30 2c 20 30 78 30 31 2c 20 30 78 30 34 2c 20  00, 0x01, 0x04, 
51890 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 32  0x02, 0x00, 0x02
518a0 2c 20 30 78 30 34 2c 20 30 78 30 30 2c 5c 0a 2f  , 0x04, 0x00,\./
518b0 2a 20 20 33 32 20 2a 2f 20 30 78 30 30 2c 20 30  *  32 */ 0x00, 0
518c0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 32 2c  x00, 0x00, 0x02,
518d0 20 30 78 31 31 2c 20 30 78 31 31 2c 20 30 78 30   0x11, 0x11, 0x0
518e0 32 2c 20 30 78 30 35 2c 5c 0a 2f 2a 20 20 34 30  2, 0x05,\./*  40
518f0 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 32 2c 20   */ 0x00, 0x02, 
51900 30 78 31 31 2c 20 30 78 30 34 2c 20 30 78 30 30  0x11, 0x04, 0x00
51910 2c 20 30 78 30 38 2c 20 30 78 31 31 2c 20 30 78  , 0x08, 0x11, 0x
51920 30 31 2c 5c 0a 2f 2a 20 20 34 38 20 2a 2f 20 30  01,\./*  48 */ 0
51930 78 30 32 2c 20 30 78 30 31 2c 20 30 78 32 31 2c  x02, 0x01, 0x21,
51940 20 30 78 30 38 2c 20 30 78 30 30 2c 20 30 78 30   0x08, 0x00, 0x0
51950 32 2c 20 30 78 30 31 2c 20 30 78 31 31 2c 5c 0a  2, 0x01, 0x11,\.
51960 2f 2a 20 20 35 36 20 2a 2f 20 30 78 30 31 2c 20  /*  56 */ 0x01, 
51970 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 34  0x02, 0x00, 0x04
51980 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
51990 30 32 2c 20 30 78 31 31 2c 5c 0a 2f 2a 20 20 36  02, 0x11,\./*  6
519a0 34 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 30 2c  4 */ 0x00, 0x00,
519b0 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 30   0x2c, 0x2c, 0x0
519c0 35 2c 20 30 78 30 30 2c 20 30 78 31 31 2c 20 30  5, 0x00, 0x11, 0
519d0 78 30 35 2c 5c 0a 2f 2a 20 20 37 32 20 2a 2f 20  x05,\./*  72 */ 
519e0 30 78 30 35 2c 20 30 78 31 35 2c 20 30 78 31 35  0x05, 0x15, 0x15
519f0 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78  , 0x15, 0x15, 0x
51a00 31 35 2c 20 30 78 31 35 2c 20 30 78 30 30 2c 5c  15, 0x15, 0x00,\
51a10 0a 2f 2a 20 20 38 30 20 2a 2f 20 30 78 32 63 2c  ./*  80 */ 0x2c,
51a20 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32   0x2c, 0x2c, 0x2
51a30 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30  c, 0x2c, 0x2c, 0
51a40 78 32 63 2c 20 30 78 32 63 2c 5c 0a 2f 2a 20 20  x2c, 0x2c,\./*  
51a50 38 38 20 2a 2f 20 30 78 32 63 2c 20 30 78 32 63  88 */ 0x2c, 0x2c
51a60 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
51a70 30 30 2c 20 30 78 30 34 2c 20 30 78 30 32 2c 20  00, 0x04, 0x02, 
51a80 30 78 30 30 2c 5c 0a 2f 2a 20 20 39 36 20 2a 2f  0x00,\./*  96 */
51a90 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30   0x01, 0x00, 0x0
51aa0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
51ab0 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c  x00, 0x01, 0x00,
51ac0 5c 0a 2f 2a 20 31 30 34 20 2a 2f 20 30 78 30 31  \./* 104 */ 0x01
51ad0 2c 20 30 78 30 32 2c 20 30 78 30 38 2c 20 30 78  , 0x02, 0x08, 0x
51ae0 31 31 2c 20 30 78 30 30 2c 20 30 78 30 32 2c 20  11, 0x00, 0x02, 
51af0 30 78 30 32 2c 20 30 78 31 35 2c 5c 0a 2f 2a 20  0x02, 0x15,\./* 
51b00 31 31 32 20 2a 2f 20 30 78 30 30 2c 20 30 78 30  112 */ 0x00, 0x0
51b10 30 2c 20 30 78 31 30 2c 20 30 78 30 30 2c 20 30  0, 0x10, 0x00, 0
51b20 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30 30 2c  x00, 0x02, 0x00,
51b30 20 30 78 30 32 2c 5c 0a 2f 2a 20 31 32 30 20 2a   0x02,\./* 120 *
51b40 2f 20 30 78 30 31 2c 20 30 78 31 31 2c 20 30 78  / 0x01, 0x11, 0x
51b50 30 30 2c 20 30 78 30 30 2c 20 30 78 30 35 2c 20  00, 0x00, 0x05, 
51b60 30 78 30 30 2c 20 30 78 31 31 2c 20 30 78 30 35  0x00, 0x11, 0x05
51b70 2c 5c 0a 2f 2a 20 31 32 38 20 2a 2f 20 30 78 30  ,\./* 128 */ 0x0
51b80 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30  0, 0x01, 0x02, 0
51b90 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
51ba0 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a   0x00, 0x00,\./*
51bb0 20 31 33 36 20 2a 2f 20 30 78 30 30 2c 20 30 78   136 */ 0x00, 0x
51bc0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
51bd0 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78 30 34  0x00, 0x04, 0x04
51be0 2c 20 30 78 30 34 2c 5c 0a 2f 2a 20 31 34 34 20  , 0x04,\./* 144 
51bf0 2a 2f 20 30 78 30 34 2c 20 30 78 30 34 2c 7d 0a  */ 0x04, 0x04,}.
51c00 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
51c10 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 65 73 2e   End of opcodes.
51c20 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
51c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
51c50 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
51c60 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
51c70 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
51c80 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a   vdbe.h ********
51c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
51ca0 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 74 6f 74 79 70  ../*.** Prototyp
51cb0 65 73 20 66 6f 72 20 74 68 65 20 56 44 42 45 20  es for the VDBE 
51cc0 69 6e 74 65 72 66 61 63 65 2e 20 20 53 65 65 20  interface.  See 
51cd0 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20  comments on the 
51ce0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a  implementation.*
51cf0 2a 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 74  * for a descript
51d00 69 6f 6e 20 6f 66 20 77 68 61 74 20 65 61 63 68  ion of what each
51d10 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e   of these routin
51d20 65 73 20 64 6f 65 73 2e 0a 2a 2f 0a 53 51 4c 49  es does..*/.SQLI
51d30 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 20  TE_PRIVATE Vdbe 
51d40 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 61  *sqlite3VdbeCrea
51d50 74 65 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51  te(sqlite3*);.SQ
51d60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
51d70 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
51d80 70 30 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53  p0(Vdbe*,int);.S
51d90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
51da0 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  t sqlite3VdbeAdd
51db0 4f 70 31 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e  Op1(Vdbe*,int,in
51dc0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
51dd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
51de0 62 65 41 64 64 4f 70 32 28 56 64 62 65 2a 2c 69  beAddOp2(Vdbe*,i
51df0 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c  nt,int,int);.SQL
51e00 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
51e10 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
51e20 33 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c  3(Vdbe*,int,int,
51e30 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  int,int);.SQLITE
51e40 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
51e50 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 56  ite3VdbeAddOp4(V
51e60 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  dbe*,int,int,int
51e70 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 20  ,int,const char 
51e80 2a 7a 50 34 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  *zP4,int);.SQLIT
51e90 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
51ea0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69  lite3VdbeAddOpLi
51eb0 73 74 28 56 64 62 65 2a 2c 20 69 6e 74 20 6e 4f  st(Vdbe*, int nO
51ec0 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6f  p, VdbeOpList co
51ed0 6e 73 74 20 2a 61 4f 70 29 3b 0a 53 51 4c 49 54  nst *aOp);.SQLIT
51ee0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
51ef0 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
51f00 50 31 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64  P1(Vdbe*, int ad
51f10 64 72 2c 20 69 6e 74 20 50 31 29 3b 0a 53 51 4c  dr, int P1);.SQL
51f20 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
51f30 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
51f40 67 65 50 32 28 56 64 62 65 2a 2c 20 69 6e 74 20  geP2(Vdbe*, int 
51f50 61 64 64 72 2c 20 69 6e 74 20 50 32 29 3b 0a 53  addr, int P2);.S
51f60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
51f70 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  id sqlite3VdbeCh
51f80 61 6e 67 65 50 33 28 56 64 62 65 2a 2c 20 69 6e  angeP3(Vdbe*, in
51f90 74 20 61 64 64 72 2c 20 69 6e 74 20 50 33 29 3b  t addr, int P3);
51fa0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
51fb0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
51fc0 43 68 61 6e 67 65 50 35 28 56 64 62 65 2a 2c 20  ChangeP5(Vdbe*, 
51fd0 75 38 20 50 35 29 3b 0a 53 51 4c 49 54 45 5f 50  u8 P5);.SQLITE_P
51fe0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
51ff0 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
52000 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 29  Vdbe*, int addr)
52010 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
52020 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
52030 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 56 64  eChangeToNoop(Vd
52040 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 69  be*, int addr, i
52050 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f 50 52  nt N);.SQLITE_PR
52060 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
52070 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 56  e3VdbeChangeP4(V
52080 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20  dbe*, int addr, 
52090 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 2c  const char *zP4,
520a0 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f   int N);.SQLITE_
520b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
520c0 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65  ite3VdbeUsesBtre
520d0 65 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53  e(Vdbe*, int);.S
520e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64  QLITE_PRIVATE Vd
520f0 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56 64 62  beOp *sqlite3Vdb
52100 65 47 65 74 4f 70 28 56 64 62 65 2a 2c 20 69 6e  eGetOp(Vdbe*, in
52110 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
52120 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
52130 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64 62 65  beMakeLabel(Vdbe
52140 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
52150 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
52160 64 62 65 44 65 6c 65 74 65 28 56 64 62 65 2a 29  dbeDelete(Vdbe*)
52170 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
52180 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
52190 65 4d 61 6b 65 52 65 61 64 79 28 56 64 62 65 2a  eMakeReady(Vdbe*
521a0 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  ,int,int,int,int
521b0 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ,int,int);.SQLIT
521c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
521d0 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a  lite3VdbeFinaliz
521e0 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45  e(Vdbe*);.SQLITE
521f0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
52200 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
52210 4c 61 62 65 6c 28 56 64 62 65 2a 2c 20 69 6e 74  Label(Vdbe*, int
52220 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
52230 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
52240 65 43 75 72 72 65 6e 74 41 64 64 72 28 56 64 62  eCurrentAddr(Vdb
52250 65 2a 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  e*);.#ifdef SQLI
52260 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f  TE_DEBUG.SQLITE_
52270 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
52280 6c 69 74 65 33 56 64 62 65 4d 61 79 41 62 6f 72  lite3VdbeMayAbor
52290 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45  t(Vdbe*);.SQLITE
522a0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
522b0 73 71 6c 69 74 65 33 56 64 62 65 54 72 61 63 65  sqlite3VdbeTrace
522c0 28 56 64 62 65 2a 2c 46 49 4c 45 2a 29 3b 0a 23  (Vdbe*,FILE*);.#
522d0 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49  endif.SQLITE_PRI
522e0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
522f0 33 56 64 62 65 52 65 73 65 74 53 74 65 70 52 65  3VdbeResetStepRe
52300 73 75 6c 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c  sult(Vdbe*);.SQL
52310 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
52320 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74  sqlite3VdbeReset
52330 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  (Vdbe*);.SQLITE_
52340 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
52350 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
52360 6c 73 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53  ls(Vdbe*,int);.S
52370 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
52380 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74  t sqlite3VdbeSet
52390 43 6f 6c 4e 61 6d 65 28 56 64 62 65 2a 2c 20 69  ColName(Vdbe*, i
523a0 6e 74 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63  nt, int, const c
523b0 68 61 72 20 2a 2c 20 76 6f 69 64 28 2a 29 28 76  har *, void(*)(v
523c0 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50  oid*));.SQLITE_P
523d0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
523e0 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e  te3VdbeCountChan
523f0 67 65 73 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49  ges(Vdbe*);.SQLI
52400 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74  TE_PRIVATE sqlit
52410 65 33 20 2a 73 71 6c 69 74 65 33 56 64 62 65 44  e3 *sqlite3VdbeD
52420 62 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45  b(Vdbe*);.SQLITE
52430 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
52440 6c 69 74 65 33 56 64 62 65 53 65 74 53 71 6c 28  lite3VdbeSetSql(
52450 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61  Vdbe*, const cha
52460 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74  r *z, int n, int
52470 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
52480 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
52490 62 65 53 77 61 70 28 56 64 62 65 2a 2c 56 64 62  beSwap(Vdbe*,Vdb
524a0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
524b0 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69  ATE VdbeOp *sqli
524c0 74 65 33 56 64 62 65 54 61 6b 65 4f 70 41 72 72  te3VdbeTakeOpArr
524d0 61 79 28 56 64 62 65 2a 2c 20 69 6e 74 2a 2c 20  ay(Vdbe*, int*, 
524e0 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  int*);.SQLITE_PR
524f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
52500 65 33 56 64 62 65 50 72 6f 67 72 61 6d 44 65 6c  e3VdbeProgramDel
52510 65 74 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 53  ete(sqlite3 *, S
52520 75 62 50 72 6f 67 72 61 6d 20 2a 2c 20 69 6e 74  ubProgram *, int
52530 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  );..#ifdef SQLIT
52540 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f  E_ENABLE_MEMORY_
52550 4d 41 4e 41 47 45 4d 45 4e 54 0a 53 51 4c 49 54  MANAGEMENT.SQLIT
52560 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
52570 6c 69 74 65 33 56 64 62 65 52 65 6c 65 61 73 65  lite3VdbeRelease
52580 4d 65 6d 6f 72 79 28 69 6e 74 29 3b 0a 23 65 6e  Memory(int);.#en
52590 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  dif.SQLITE_PRIVA
525a0 54 45 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  TE UnpackedRecor
525b0 64 20 2a 73 71 6c 69 74 65 33 56 64 62 65 52 65  d *sqlite3VdbeRe
525c0 63 6f 72 64 55 6e 70 61 63 6b 28 4b 65 79 49 6e  cordUnpack(KeyIn
525d0 66 6f 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  fo*,int,const vo
525e0 69 64 2a 2c 63 68 61 72 2a 2c 69 6e 74 29 3b 0a  id*,char*,int);.
525f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
52600 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44  oid sqlite3VdbeD
52610 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63  eleteUnpackedRec
52620 6f 72 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f  ord(UnpackedReco
52630 72 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  rd*);.SQLITE_PRI
52640 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
52650 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72  VdbeRecordCompar
52660 65 28 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  e(int,const void
52670 2a 2c 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64  *,UnpackedRecord
52680 2a 29 3b 0a 0a 0a 23 69 66 6e 64 65 66 20 4e 44  *);...#ifndef ND
52690 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56  EBUG.SQLITE_PRIV
526a0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
526b0 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64  e3VdbeComment(Vd
526c0 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  be*, const char*
526d0 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65  , ...);.# define
526e0 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 20   VdbeComment(X) 
526f0 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 6d 6d   sqlite3VdbeComm
52700 65 6e 74 20 58 0a 53 51 4c 49 54 45 5f 50 52 49  ent X.SQLITE_PRI
52710 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
52720 74 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65  te3VdbeNoopComme
52730 6e 74 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20  nt(Vdbe*, const 
52740 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 20 64  char*, ...);.# d
52750 65 66 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f  efine VdbeNoopCo
52760 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c 69 74 65  mment(X)  sqlite
52770 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74  3VdbeNoopComment
52780 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e   X.#else.# defin
52790 65 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29  e VdbeComment(X)
527a0 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 4e 6f  .# define VdbeNo
527b0 6f 70 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 65 6e  opComment(X).#en
527c0 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a  dif..#endif../**
527d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
527e0 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a   of vdbe.h *****
527f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
52820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
52830 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
52840 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c   left off in sql
52850 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
52860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
52870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
52880 6c 75 64 65 20 70 61 67 65 72 2e 68 20 69 6e 20  lude pager.h in 
52890 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71  the middle of sq
528a0 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  liteInt.h ******
528b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
528c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
528d0 69 6e 20 66 69 6c 65 20 70 61 67 65 72 2e 68 20  in file pager.h 
528e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
528f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
52910 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
52920 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
52930 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
52940 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
52950 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
52960 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
52970 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
52980 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
52990 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
529a0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
529b0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
529c0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
529d0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
529e0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
529f0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
52a00 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
52a10 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
52a20 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
52a30 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
52a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52a80 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64  ***.** This head
52a90 65 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20  er file defines 
52aa0 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 68  the interface th
52ab0 61 74 20 74 68 65 20 73 71 6c 69 74 65 20 70 61  at the sqlite pa
52ac0 67 65 20 63 61 63 68 65 0a 2a 2a 20 73 75 62 73  ge cache.** subs
52ad0 79 73 74 65 6d 2e 20 20 54 68 65 20 70 61 67 65  ystem.  The page
52ae0 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d   cache subsystem
52af0 20 72 65 61 64 73 20 61 6e 64 20 77 72 69 74 65   reads and write
52b00 73 20 61 20 66 69 6c 65 20 61 20 70 61 67 65 0a  s a file a page.
52b10 2a 2a 20 61 74 20 61 20 74 69 6d 65 20 61 6e 64  ** at a time and
52b20 20 70 72 6f 76 69 64 65 73 20 61 20 6a 6f 75 72   provides a jour
52b30 6e 61 6c 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b  nal for rollback
52b40 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64  ..**.** @(#) $Id
52b50 3a 20 70 61 67 65 72 2e 68 2c 76 20 31 2e 31 30  : pager.h,v 1.10
52b60 34 20 32 30 30 39 2f 30 37 2f 32 34 20 31 39 3a  4 2009/07/24 19:
52b70 30 31 3a 31 39 20 64 72 68 20 45 78 70 20 24 0a  01:19 drh Exp $.
52b80 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 41 47  */..#ifndef _PAG
52b90 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 50  ER_H_.#define _P
52ba0 41 47 45 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 44  AGER_H_../*.** D
52bb0 65 66 61 75 6c 74 20 6d 61 78 69 6d 75 6d 20 73  efault maximum s
52bc0 69 7a 65 20 66 6f 72 20 70 65 72 73 69 73 74 65  ize for persiste
52bd0 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73  nt journal files
52be0 2e 20 41 20 6e 65 67 61 74 69 76 65 20 0a 2a 2a  . A negative .**
52bf0 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 6e 6f 20   value means no 
52c00 6c 69 6d 69 74 2e 20 54 68 69 73 20 76 61 6c 75  limit. This valu
52c10 65 20 6d 61 79 20 62 65 20 6f 76 65 72 72 69 64  e may be overrid
52c20 64 65 6e 20 75 73 69 6e 67 20 74 68 65 20 0a 2a  den using the .*
52c30 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f  * sqlite3PagerJo
52c40 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 29  urnalSizeLimit()
52c50 20 41 50 49 2e 20 53 65 65 20 61 6c 73 6f 20 22   API. See also "
52c60 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 73  PRAGMA journal_s
52c70 69 7a 65 5f 6c 69 6d 69 74 22 2e 0a 2a 2f 0a 23  ize_limit"..*/.#
52c80 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45  ifndef SQLITE_DE
52c90 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49  FAULT_JOURNAL_SI
52ca0 5a 45 5f 4c 49 4d 49 54 0a 20 20 23 64 65 66 69  ZE_LIMIT.  #defi
52cb0 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ne SQLITE_DEFAUL
52cc0 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c  T_JOURNAL_SIZE_L
52cd0 49 4d 49 54 20 2d 31 0a 23 65 6e 64 69 66 0a 0a  IMIT -1.#endif..
52ce0 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65 20 75  /*.** The type u
52cf0 73 65 64 20 74 6f 20 72 65 70 72 65 73 65 6e 74  sed to represent
52d00 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20   a page number. 
52d10 20 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20   The first page 
52d20 69 6e 20 61 20 66 69 6c 65 0a 2a 2a 20 69 73 20  in a file.** is 
52d30 63 61 6c 6c 65 64 20 70 61 67 65 20 31 2e 20 20  called page 1.  
52d40 30 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 70  0 is used to rep
52d50 72 65 73 65 6e 74 20 22 6e 6f 74 20 61 20 70 61  resent "not a pa
52d60 67 65 22 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ge"..*/.typedef 
52d70 75 33 32 20 50 67 6e 6f 3b 0a 0a 2f 2a 0a 2a 2a  u32 Pgno;../*.**
52d80 20 45 61 63 68 20 6f 70 65 6e 20 66 69 6c 65 20   Each open file 
52d90 69 73 20 6d 61 6e 61 67 65 64 20 62 79 20 61 20  is managed by a 
52da0 73 65 70 61 72 61 74 65 20 69 6e 73 74 61 6e 63  separate instanc
52db0 65 20 6f 66 20 74 68 65 20 22 50 61 67 65 72 22  e of the "Pager"
52dc0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74   structure..*/.t
52dd0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61  ypedef struct Pa
52de0 67 65 72 20 50 61 67 65 72 3b 0a 0a 2f 2a 0a 2a  ger Pager;../*.*
52df0 2a 20 48 61 6e 64 6c 65 20 74 79 70 65 20 66 6f  * Handle type fo
52e00 72 20 70 61 67 65 73 2e 0a 2a 2f 0a 74 79 70 65  r pages..*/.type
52e10 64 65 66 20 73 74 72 75 63 74 20 50 67 48 64 72  def struct PgHdr
52e20 20 44 62 50 61 67 65 3b 0a 0a 2f 2a 0a 2a 2a 20   DbPage;../*.** 
52e30 50 61 67 65 20 6e 75 6d 62 65 72 20 50 41 47 45  Page number PAGE
52e40 52 5f 4d 4a 5f 50 47 4e 4f 20 69 73 20 6e 65 76  R_MJ_PGNO is nev
52e50 65 72 20 75 73 65 64 20 69 6e 20 61 6e 20 53 51  er used in an SQ
52e60 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 28 69  Lite database (i
52e70 74 20 69 73 0a 2a 2a 20 72 65 73 65 72 76 65 64  t is.** reserved
52e80 20 66 6f 72 20 77 6f 72 6b 69 6e 67 20 61 72 6f   for working aro
52e90 75 6e 64 20 61 20 77 69 6e 64 6f 77 73 2f 70 6f  und a windows/po
52ea0 73 69 78 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c  six incompatibil
52eb0 69 74 79 29 2e 20 49 74 20 69 73 0a 2a 2a 20 75  ity). It is.** u
52ec0 73 65 64 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  sed in the journ
52ed0 61 6c 20 74 6f 20 73 69 67 6e 69 66 79 20 74 68  al to signify th
52ee0 61 74 20 74 68 65 20 72 65 6d 61 69 6e 64 65 72  at the remainder
52ef0 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
52f00 66 69 6c 65 20 0a 2a 2a 20 69 73 20 64 65 76 6f  file .** is devo
52f10 74 65 64 20 74 6f 20 73 74 6f 72 69 6e 67 20 61  ted to storing a
52f20 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
52f30 6e 61 6d 65 20 2d 20 74 68 65 72 65 20 61 72 65  name - there are
52f40 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65 73 20 74   no more pages t
52f50 6f 0a 2a 2a 20 72 6f 6c 6c 20 62 61 63 6b 2e 20  o.** roll back. 
52f60 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72  See comments for
52f70 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 4d   function writeM
52f80 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 69  asterJournal() i
52f90 6e 20 70 61 67 65 72 2e 63 20 0a 2a 2a 20 66 6f  n pager.c .** fo
52fa0 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64  r details..*/.#d
52fb0 65 66 69 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50  efine PAGER_MJ_P
52fc0 47 4e 4f 28 78 29 20 28 28 50 67 6e 6f 29 28 28  GNO(x) ((Pgno)((
52fd0 50 45 4e 44 49 4e 47 5f 42 59 54 45 2f 28 28 78  PENDING_BYTE/((x
52fe0 29 2d 3e 70 61 67 65 53 69 7a 65 29 29 2b 31 29  )->pageSize))+1)
52ff0 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64  )../*.** Allowed
53000 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20   values for the 
53010 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20  flags parameter 
53020 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f  to sqlite3PagerO
53030 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54  pen()..**.** NOT
53040 45 3a 20 54 68 65 73 65 20 76 61 6c 75 65 73 20  E: These values 
53050 6d 75 73 74 20 6d 61 74 63 68 20 74 68 65 20 63  must match the c
53060 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 42 54 52  orresponding BTR
53070 45 45 5f 20 76 61 6c 75 65 73 20 69 6e 20 62 74  EE_ values in bt
53080 72 65 65 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ree.h..*/.#defin
53090 65 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55  e PAGER_OMIT_JOU
530a0 52 4e 41 4c 20 20 30 78 30 30 30 31 20 20 20 20  RNAL  0x0001    
530b0 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65 20 61 20  /* Do not use a 
530c0 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
530d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45   */.#define PAGE
530e0 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20  R_NO_READLOCK   
530f0 30 78 30 30 30 32 20 20 20 20 2f 2a 20 4f 6d 69  0x0002    /* Omi
53100 74 20 72 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72  t readlocks on r
53110 65 61 64 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f  eadonly files */
53120 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61  ../*.** Valid va
53130 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 65 63  lues for the sec
53140 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  ond argument to 
53150 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b  sqlite3PagerLock
53160 69 6e 67 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64  ingMode()..*/.#d
53170 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b  efine PAGER_LOCK
53180 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 20 20 20  INGMODE_QUERY   
53190 20 20 20 2d 31 0a 23 64 65 66 69 6e 65 20 50 41     -1.#define PA
531a0 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
531b0 4e 4f 52 4d 41 4c 20 20 20 20 20 20 30 0a 23 64  NORMAL      0.#d
531c0 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b  efine PAGER_LOCK
531d0 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56  INGMODE_EXCLUSIV
531e0 45 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c  E   1../*.** Val
531f0 69 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68  id values for th
53200 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
53210 74 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65  t to sqlite3Page
53220 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 29 2e 0a  rJournalMode()..
53230 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  */.#define PAGER
53240 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45  _JOURNALMODE_QUE
53250 52 59 20 20 20 20 20 20 2d 31 0a 23 64 65 66 69  RY      -1.#defi
53260 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  ne PAGER_JOURNAL
53270 4d 4f 44 45 5f 44 45 4c 45 54 45 20 20 20 20 20  MODE_DELETE     
53280 20 30 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62   0   /* Commit b
53290 79 20 64 65 6c 65 74 69 6e 67 20 6a 6f 75 72 6e  y deleting journ
532a0 61 6c 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69  al file */.#defi
532b0 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  ne PAGER_JOURNAL
532c0 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 20 20 20  MODE_PERSIST    
532d0 20 31 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62   1   /* Commit b
532e0 79 20 7a 65 72 6f 69 6e 67 20 6a 6f 75 72 6e 61  y zeroing journa
532f0 6c 20 68 65 61 64 65 72 20 2a 2f 0a 23 64 65 66  l header */.#def
53300 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41  ine PAGER_JOURNA
53310 4c 4d 4f 44 45 5f 4f 46 46 20 20 20 20 20 20 20  LMODE_OFF       
53320 20 20 32 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c    2   /* Journal
53330 20 6f 6d 69 74 74 65 64 2e 20 20 2a 2f 0a 23 64   omitted.  */.#d
53340 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52  efine PAGER_JOUR
53350 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45  NALMODE_TRUNCATE
53360 20 20 20 20 33 20 20 20 2f 2a 20 43 6f 6d 6d 69      3   /* Commi
53370 74 20 62 79 20 74 72 75 6e 63 61 74 69 6e 67 20  t by truncating 
53380 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64 65 66 69  journal */.#defi
53390 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  ne PAGER_JOURNAL
533a0 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20  MODE_MEMORY     
533b0 20 34 20 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72   4   /* In-memor
533c0 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a  y journal file *
533d0 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 6d  /../*.** The rem
533e0 61 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20 66  ainder of this f
533f0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ile contains the
53400 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66   declarations of
53410 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a   the functions.*
53420 2a 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20 74  * that make up t
53430 68 65 20 50 61 67 65 72 20 73 75 62 2d 73 79 73  he Pager sub-sys
53440 74 65 6d 20 41 50 49 2e 20 53 65 65 20 73 6f 75  tem API. See sou
53450 72 63 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74  rce code comment
53460 73 20 66 6f 72 20 0a 2a 2a 20 61 20 64 65 74 61  s for .** a deta
53470 69 6c 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e  iled description
53480 20 6f 66 20 65 61 63 68 20 72 6f 75 74 69 6e 65   of each routine
53490 2e 0a 2a 2f 0a 0a 2f 2a 20 4f 70 65 6e 20 61 6e  ..*/../* Open an
534a0 64 20 63 6c 6f 73 65 20 61 20 50 61 67 65 72 20  d close a Pager 
534b0 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 2a 2f 20 0a  connection. */ .
534c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
534d0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f  nt sqlite3PagerO
534e0 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  pen(.  sqlite3_v
534f0 66 73 2a 2c 0a 20 20 50 61 67 65 72 20 2a 2a 70  fs*,.  Pager **p
53500 70 50 61 67 65 72 2c 0a 20 20 63 6f 6e 73 74 20  pPager,.  const 
53510 63 68 61 72 2a 2c 0a 20 20 69 6e 74 2c 0a 20 20  char*,.  int,.  
53520 69 6e 74 2c 0a 20 20 69 6e 74 2c 0a 20 20 76 6f  int,.  int,.  vo
53530 69 64 28 2a 29 28 44 62 50 61 67 65 2a 29 0a 29  id(*)(DbPage*).)
53540 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
53550 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
53560 72 43 6c 6f 73 65 28 50 61 67 65 72 20 2a 70 50  rClose(Pager *pP
53570 61 67 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ager);.SQLITE_PR
53580 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
53590 33 50 61 67 65 72 52 65 61 64 46 69 6c 65 68 65  3PagerReadFilehe
535a0 61 64 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74  ader(Pager*, int
535b0 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a  , unsigned char*
535c0 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73  );../* Functions
535d0 20 75 73 65 64 20 74 6f 20 63 6f 6e 66 69 67 75   used to configu
535e0 72 65 20 61 20 50 61 67 65 72 20 6f 62 6a 65 63  re a Pager objec
535f0 74 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t. */.SQLITE_PRI
53600 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
53610 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e  3PagerSetBusyhan
53620 64 6c 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74  dler(Pager*, int
53630 28 2a 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69  (*)(void *), voi
53640 64 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  d *);.SQLITE_PRI
53650 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
53660 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65  PagerSetPagesize
53670 28 50 61 67 65 72 2a 2c 20 75 31 36 2a 2c 20 69  (Pager*, u16*, i
53680 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
53690 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
536a0 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74  agerMaxPageCount
536b0 28 50 61 67 65 72 2a 2c 20 69 6e 74 29 3b 0a 53  (Pager*, int);.S
536c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
536d0 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  id sqlite3PagerS
536e0 65 74 43 61 63 68 65 73 69 7a 65 28 50 61 67 65  etCachesize(Page
536f0 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  r*, int);.SQLITE
53700 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
53710 6c 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66  lite3PagerSetSaf
53720 65 74 79 4c 65 76 65 6c 28 50 61 67 65 72 2a 2c  etyLevel(Pager*,
53730 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  int,int);.SQLITE
53740 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
53750 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67  ite3PagerLocking
53760 4d 6f 64 65 28 50 61 67 65 72 20 2a 2c 20 69 6e  Mode(Pager *, in
53770 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
53780 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
53790 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50  gerJournalMode(P
537a0 61 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51  ager *, int);.SQ
537b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34  LITE_PRIVATE i64
537c0 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75   sqlite3PagerJou
537d0 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50 61  rnalSizeLimit(Pa
537e0 67 65 72 20 2a 2c 20 69 36 34 29 3b 0a 53 51 4c  ger *, i64);.SQL
537f0 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69  ITE_PRIVATE sqli
53800 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a 73 71 6c  te3_backup **sql
53810 69 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 50  ite3PagerBackupP
53820 74 72 28 50 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20  tr(Pager*);../* 
53830 46 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74  Functions used t
53840 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 6c  o obtain and rel
53850 65 61 73 65 20 70 61 67 65 20 72 65 66 65 72 65  ease page refere
53860 6e 63 65 73 2e 20 2a 2f 20 0a 53 51 4c 49 54 45  nces. */ .SQLITE
53870 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
53880 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65  ite3PagerAcquire
53890 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
538a0 50 67 6e 6f 20 70 67 6e 6f 2c 20 44 62 50 61 67  Pgno pgno, DbPag
538b0 65 20 2a 2a 70 70 50 61 67 65 2c 20 69 6e 74 20  e **ppPage, int 
538c0 63 6c 72 46 6c 61 67 29 3b 0a 23 64 65 66 69 6e  clrFlag);.#defin
538d0 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  e sqlite3PagerGe
538e0 74 28 41 2c 42 2c 43 29 20 73 71 6c 69 74 65 33  t(A,B,C) sqlite3
538f0 50 61 67 65 72 41 63 71 75 69 72 65 28 41 2c 42  PagerAcquire(A,B
53900 2c 43 2c 30 29 0a 53 51 4c 49 54 45 5f 50 52 49  ,C,0).SQLITE_PRI
53910 56 41 54 45 20 44 62 50 61 67 65 20 2a 73 71 6c  VATE DbPage *sql
53920 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28  ite3PagerLookup(
53930 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50  Pager *pPager, P
53940 67 6e 6f 20 70 67 6e 6f 29 3b 0a 53 51 4c 49 54  gno pgno);.SQLIT
53950 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
53960 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 44  qlite3PagerRef(D
53970 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  bPage*);.SQLITE_
53980 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
53990 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 44  ite3PagerUnref(D
539a0 62 50 61 67 65 2a 29 3b 0a 0a 2f 2a 20 4f 70 65  bPage*);../* Ope
539b0 72 61 74 69 6f 6e 73 20 6f 6e 20 70 61 67 65 20  rations on page 
539c0 72 65 66 65 72 65 6e 63 65 73 2e 20 2a 2f 0a 53  references. */.S
539d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
539e0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  t sqlite3PagerWr
539f0 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51  ite(DbPage*);.SQ
53a00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
53a10 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f  d sqlite3PagerDo
53a20 6e 74 57 72 69 74 65 28 44 62 50 61 67 65 2a 29  ntWrite(DbPage*)
53a30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
53a40 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
53a50 72 4d 6f 76 65 70 61 67 65 28 50 61 67 65 72 2a  rMovepage(Pager*
53a60 2c 44 62 50 61 67 65 2a 2c 50 67 6e 6f 2c 69 6e  ,DbPage*,Pgno,in
53a70 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
53a80 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
53a90 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28  gerPageRefcount(
53aa0 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45  DbPage*);.SQLITE
53ab0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
53ac0 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61  qlite3PagerGetDa
53ad0 74 61 28 44 62 50 61 67 65 20 2a 29 3b 20 0a 53  ta(DbPage *); .S
53ae0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
53af0 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  id *sqlite3Pager
53b00 47 65 74 45 78 74 72 61 28 44 62 50 61 67 65 20  GetExtra(DbPage 
53b10 2a 29 3b 20 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f  *); ../* Functio
53b20 6e 73 20 75 73 65 64 20 74 6f 20 6d 61 6e 61 67  ns used to manag
53b30 65 20 70 61 67 65 72 20 74 72 61 6e 73 61 63 74  e pager transact
53b40 69 6f 6e 73 20 61 6e 64 20 73 61 76 65 70 6f 69  ions and savepoi
53b50 6e 74 73 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  nts. */.SQLITE_P
53b60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
53b70 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
53b80 28 50 61 67 65 72 2a 2c 20 69 6e 74 2a 29 3b 0a  (Pager*, int*);.
53b90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
53ba0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 42  nt sqlite3PagerB
53bb0 65 67 69 6e 28 50 61 67 65 72 2a 2c 20 69 6e 74  egin(Pager*, int
53bc0 20 65 78 46 6c 61 67 2c 20 69 6e 74 29 3b 0a 53   exFlag, int);.S
53bd0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
53be0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  t sqlite3PagerCo
53bf0 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 50 61 67  mmitPhaseOne(Pag
53c00 65 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 20 2a  er*,const char *
53c10 7a 4d 61 73 74 65 72 2c 20 69 6e 74 29 3b 0a 53  zMaster, int);.S
53c20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
53c30 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79  t sqlite3PagerSy
53c40 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  nc(Pager *pPager
53c50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
53c60 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
53c70 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  erCommitPhaseTwo
53c80 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  (Pager*);.SQLITE
53c90 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
53ca0 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63  ite3PagerRollbac
53cb0 6b 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54  k(Pager*);.SQLIT
53cc0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
53cd0 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61  lite3PagerOpenSa
53ce0 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70  vepoint(Pager *p
53cf0 50 61 67 65 72 2c 20 69 6e 74 20 6e 29 3b 0a 53  Pager, int n);.S
53d00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
53d10 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61  t sqlite3PagerSa
53d20 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70  vepoint(Pager *p
53d30 50 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69  Pager, int op, i
53d40 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a  nt iSavepoint);.
53d50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
53d60 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  nt sqlite3PagerS
53d70 68 61 72 65 64 4c 6f 63 6b 28 50 61 67 65 72 20  haredLock(Pager 
53d80 2a 70 50 61 67 65 72 29 3b 0a 0a 2f 2a 20 46 75  *pPager);../* Fu
53d90 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20  nctions used to 
53da0 71 75 65 72 79 20 70 61 67 65 72 20 73 74 61 74  query pager stat
53db0 65 20 61 6e 64 20 63 6f 6e 66 69 67 75 72 61 74  e and configurat
53dc0 69 6f 6e 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion. */.SQLITE_P
53dd0 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65  RIVATE u8 sqlite
53de0 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79  3PagerIsreadonly
53df0 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  (Pager*);.SQLITE
53e00 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
53e10 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e  ite3PagerRefcoun
53e20 74 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54  t(Pager*);.SQLIT
53e30 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
53e40 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67  char *sqlite3Pag
53e50 65 72 46 69 6c 65 6e 61 6d 65 28 50 61 67 65 72  erFilename(Pager
53e60 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
53e70 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  TE const sqlite3
53e80 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61 67  _vfs *sqlite3Pag
53e90 65 72 56 66 73 28 50 61 67 65 72 2a 29 3b 0a 53  erVfs(Pager*);.S
53ea0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71  QLITE_PRIVATE sq
53eb0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c 69  lite3_file *sqli
53ec0 74 65 33 50 61 67 65 72 46 69 6c 65 28 50 61 67  te3PagerFile(Pag
53ed0 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  er*);.SQLITE_PRI
53ee0 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
53ef0 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75  *sqlite3PagerJou
53f00 72 6e 61 6c 6e 61 6d 65 28 50 61 67 65 72 2a 29  rnalname(Pager*)
53f10 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
53f20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
53f30 72 4e 6f 73 79 6e 63 28 50 61 67 65 72 2a 29 3b  rNosync(Pager*);
53f40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
53f50 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67  void *sqlite3Pag
53f60 65 72 54 65 6d 70 53 70 61 63 65 28 50 61 67 65  erTempSpace(Page
53f70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
53f80 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
53f90 61 67 65 72 49 73 4d 65 6d 64 62 28 50 61 67 65  agerIsMemdb(Page
53fa0 72 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f  r*);../* Functio
53fb0 6e 73 20 75 73 65 64 20 74 6f 20 74 72 75 6e 63  ns used to trunc
53fc0 61 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ate the database
53fd0 20 66 69 6c 65 2e 20 2a 2f 0a 53 51 4c 49 54 45   file. */.SQLITE
53fe0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
53ff0 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61  lite3PagerTrunca
54000 74 65 49 6d 61 67 65 28 50 61 67 65 72 2a 2c 50  teImage(Pager*,P
54010 67 6e 6f 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69  gno);../* Functi
54020 6f 6e 73 20 74 6f 20 73 75 70 70 6f 72 74 20 74  ons to support t
54030 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67  esting and debug
54040 67 69 6e 67 2e 20 2a 2f 0a 23 69 66 20 21 64 65  ging. */.#if !de
54050 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c  fined(NDEBUG) ||
54060 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
54070 54 45 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 49  TEST).SQLITE_PRI
54080 56 41 54 45 20 20 20 50 67 6e 6f 20 73 71 6c 69  VATE   Pgno sqli
54090 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62  te3PagerPagenumb
540a0 65 72 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c  er(DbPage*);.SQL
540b0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
540c0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  t sqlite3PagerIs
540d0 77 72 69 74 65 61 62 6c 65 28 44 62 50 61 67 65  writeable(DbPage
540e0 2a 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  *);.#endif.#ifde
540f0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
54100 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
54110 6e 74 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  nt *sqlite3Pager
54120 53 74 61 74 73 28 50 61 67 65 72 2a 29 3b 0a 53  Stats(Pager*);.S
54130 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
54140 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
54150 72 52 65 66 64 75 6d 70 28 50 61 67 65 72 2a 29  rRefdump(Pager*)
54160 3b 0a 20 20 76 6f 69 64 20 64 69 73 61 62 6c 65  ;.  void disable
54170 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72  _simulated_io_er
54180 72 6f 72 73 28 76 6f 69 64 29 3b 0a 20 20 76 6f  rors(void);.  vo
54190 69 64 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61  id enable_simula
541a0 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f  ted_io_errors(vo
541b0 69 64 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  id);.#else.# def
541c0 69 6e 65 20 64 69 73 61 62 6c 65 5f 73 69 6d 75  ine disable_simu
541d0 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28  lated_io_errors(
541e0 29 0a 23 20 64 65 66 69 6e 65 20 65 6e 61 62 6c  ).# define enabl
541f0 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65  e_simulated_io_e
54200 72 72 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a  rrors().#endif..
54210 23 65 6e 64 69 66 20 2f 2a 20 5f 50 41 47 45 52  #endif /* _PAGER
54220 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  _H_ */../*******
54230 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70  ******* End of p
54240 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ager.h *********
54250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54270 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
54280 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
54290 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
542a0 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e   off in sqliteIn
542b0 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
542c0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
542d0 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
542e0 70 63 61 63 68 65 2e 68 20 69 6e 20 74 68 65 20  pcache.h in the 
542f0 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65  middle of sqlite
54300 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
54310 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
54320 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
54330 6c 65 20 70 63 61 63 68 65 2e 68 20 2a 2a 2a 2a  le pcache.h ****
54340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54360 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
54370 30 38 20 41 75 67 75 73 74 20 30 35 0a 2a 2a 0a  08 August 05.**.
54380 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
54390 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
543a0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
543b0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
543c0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
543d0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
543e0 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
543f0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
54400 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
54410 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
54420 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
54430 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
54440 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
54450 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
54460 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
54470 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
54480 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
54490 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
544a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
544b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
544c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
544d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
544e0 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20  his header file 
544f0 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65  defines the inte
54500 72 66 61 63 65 20 74 68 61 74 20 74 68 65 20 73  rface that the s
54510 71 6c 69 74 65 20 70 61 67 65 20 63 61 63 68 65  qlite page cache
54520 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 0a  .** subsystem. .
54530 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20  **.** @(#) $Id: 
54540 70 63 61 63 68 65 2e 68 2c 76 20 31 2e 32 30 20  pcache.h,v 1.20 
54550 32 30 30 39 2f 30 37 2f 32 35 20 31 31 3a 34 36  2009/07/25 11:46
54560 3a 34 39 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :49 danielk1977 
54570 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65  Exp $.*/..#ifnde
54580 66 20 5f 50 43 41 43 48 45 5f 48 5f 0a 0a 74 79  f _PCACHE_H_..ty
54590 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 48  pedef struct PgH
545a0 64 72 20 50 67 48 64 72 3b 0a 74 79 70 65 64 65  dr PgHdr;.typede
545b0 66 20 73 74 72 75 63 74 20 50 43 61 63 68 65 20  f struct PCache 
545c0 50 43 61 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45  PCache;../*.** E
545d0 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65  very page in the
545e0 20 63 61 63 68 65 20 69 73 20 63 6f 6e 74 72 6f   cache is contro
545f0 6c 6c 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61  lled by an insta
54600 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
54610 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72  wing.** structur
54620 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 67 48  e..*/.struct PgH
54630 64 72 20 7b 0a 20 20 76 6f 69 64 20 2a 70 44 61  dr {.  void *pDa
54640 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ta;             
54650 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74        /* Content
54660 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 2a 2f   of this page */
54670 0a 20 20 76 6f 69 64 20 2a 70 45 78 74 72 61 3b  .  void *pExtra;
54680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54690 20 20 2f 2a 20 45 78 74 72 61 20 63 6f 6e 74 65    /* Extra conte
546a0 6e 74 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70  nt */.  PgHdr *p
546b0 44 69 72 74 79 3b 20 20 20 20 20 20 20 20 20 20  Dirty;          
546c0 20 20 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 69         /* Transi
546d0 65 6e 74 20 6c 69 73 74 20 6f 66 20 64 69 72 74  ent list of dirt
546e0 79 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 6e  y pages */.  Pgn
546f0 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20  o pgno;         
54700 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
54710 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  age number for t
54720 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 61  his page */.  Pa
54730 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20  ger *pPager;    
54740 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
54750 54 68 65 20 70 61 67 65 72 20 74 68 69 73 20 70  The pager this p
54760 61 67 65 20 69 73 20 70 61 72 74 20 6f 66 20 2a  age is part of *
54770 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
54780 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 75 33  CHECK_PAGES.  u3
54790 32 20 70 61 67 65 48 61 73 68 3b 20 20 20 20 20  2 pageHash;     
547a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
547b0 48 61 73 68 20 6f 66 20 70 61 67 65 20 63 6f 6e  Hash of page con
547c0 74 65 6e 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 20  tent */.#endif. 
547d0 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20   u16 flags;     
547e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
547f0 2f 2a 20 50 47 48 44 52 20 66 6c 61 67 73 20 64  /* PGHDR flags d
54800 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 2a 2f 0a  efined below */.
54810 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .  /************
54820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20  **********.  ** 
54860 45 6c 65 6d 65 6e 74 73 20 61 62 6f 76 65 20 61  Elements above a
54870 72 65 20 70 75 62 6c 69 63 2e 20 20 41 6c 6c 20  re public.  All 
54880 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20  that follows is 
54890 70 72 69 76 61 74 65 20 74 6f 20 70 63 61 63 68  private to pcach
548a0 65 2e 63 0a 20 20 2a 2a 20 61 6e 64 20 73 68 6f  e.c.  ** and sho
548b0 75 6c 64 20 6e 6f 74 20 62 65 20 61 63 63 65 73  uld not be acces
548c0 73 65 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64  sed by other mod
548d0 75 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 31 36  ules..  */.  i16
548e0 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20   nRef;          
548f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
54900 75 6d 62 65 72 20 6f 66 20 75 73 65 72 73 20 6f  umber of users o
54910 66 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20  f this page */. 
54920 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 3b   PCache *pCache;
54930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54940 2f 2a 20 43 61 63 68 65 20 74 68 61 74 20 6f 77  /* Cache that ow
54950 6e 73 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a  ns this page */.
54960 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79  .  PgHdr *pDirty
54970 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20  Next;           
54980 20 20 2f 2a 20 4e 65 78 74 20 65 6c 65 6d 65 6e    /* Next elemen
54990 74 20 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72  t in list of dir
549a0 74 79 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67  ty pages */.  Pg
549b0 48 64 72 20 2a 70 44 69 72 74 79 50 72 65 76 3b  Hdr *pDirtyPrev;
549c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
549d0 50 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74  Previous element
549e0 20 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74   in list of dirt
549f0 79 20 70 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f  y pages */.};../
54a00 2a 20 42 69 74 20 76 61 6c 75 65 73 20 66 6f 72  * Bit values for
54a10 20 50 67 48 64 72 2e 66 6c 61 67 73 20 2a 2f 0a   PgHdr.flags */.
54a20 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f 44 49  #define PGHDR_DI
54a30 52 54 59 20 20 20 20 20 20 20 20 20 20 20 20 20  RTY             
54a40 30 78 30 30 32 20 20 2f 2a 20 50 61 67 65 20 68  0x002  /* Page h
54a50 61 73 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64  as changed */.#d
54a60 65 66 69 6e 65 20 50 47 48 44 52 5f 4e 45 45 44  efine PGHDR_NEED
54a70 5f 53 59 4e 43 20 20 20 20 20 20 20 20 20 30 78  _SYNC         0x
54a80 30 30 34 20 20 2f 2a 20 46 73 79 6e 63 20 74 68  004  /* Fsync th
54a90 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  e rollback journ
54aa0 61 6c 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20  al before.      
54ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54ad0 20 2a 2a 20 77 72 69 74 69 6e 67 20 74 68 69 73   ** writing this
54ae0 20 70 61 67 65 20 74 6f 20 74 68 65 20 64 61 74   page to the dat
54af0 61 62 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65  abase */.#define
54b00 20 50 47 48 44 52 5f 4e 45 45 44 5f 52 45 41 44   PGHDR_NEED_READ
54b10 20 20 20 20 20 20 20 20 20 30 78 30 30 38 20 20           0x008  
54b20 2f 2a 20 43 6f 6e 74 65 6e 74 20 69 73 20 75 6e  /* Content is un
54b30 72 65 61 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  read */.#define 
54b40 50 47 48 44 52 5f 52 45 55 53 45 5f 55 4e 4c 49  PGHDR_REUSE_UNLI
54b50 4b 45 4c 59 20 20 20 20 30 78 30 31 30 20 20 2f  KELY    0x010  /
54b60 2a 20 41 20 68 69 6e 74 20 74 68 61 74 20 72 65  * A hint that re
54b70 75 73 65 20 69 73 20 75 6e 6c 69 6b 65 6c 79 20  use is unlikely 
54b80 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52  */.#define PGHDR
54b90 5f 44 4f 4e 54 5f 57 52 49 54 45 20 20 20 20 20  _DONT_WRITE     
54ba0 20 20 20 30 78 30 32 30 20 20 2f 2a 20 44 6f 20     0x020  /* Do 
54bb0 6e 6f 74 20 77 72 69 74 65 20 63 6f 6e 74 65 6e  not write conten
54bc0 74 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 0a 2f 2a  t to disk */../*
54bd0 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20   Initialize and 
54be0 73 68 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67  shutdown the pag
54bf0 65 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65  e cache subsyste
54c00 6d 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  m */.SQLITE_PRIV
54c10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
54c20 63 61 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28  cacheInitialize(
54c30 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52  void);.SQLITE_PR
54c40 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
54c50 65 33 50 63 61 63 68 65 53 68 75 74 64 6f 77 6e  e3PcacheShutdown
54c60 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 50 61 67 65  (void);../* Page
54c70 20 63 61 63 68 65 20 62 75 66 66 65 72 20 6d 61   cache buffer ma
54c80 6e 61 67 65 6d 65 6e 74 3a 0a 2a 2a 20 54 68 65  nagement:.** The
54c90 73 65 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c  se routines impl
54ca0 65 6d 65 6e 74 20 53 51 4c 49 54 45 5f 43 4f 4e  ement SQLITE_CON
54cb0 46 49 47 5f 50 41 47 45 43 41 43 48 45 2e 0a 2a  FIG_PAGECACHE..*
54cc0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
54cd0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61   void sqlite3PCa
54ce0 63 68 65 42 75 66 66 65 72 53 65 74 75 70 28 76  cheBufferSetup(v
54cf0 6f 69 64 20 2a 2c 20 69 6e 74 20 73 7a 2c 20 69  oid *, int sz, i
54d00 6e 74 20 6e 29 3b 0a 0a 2f 2a 20 43 72 65 61 74  nt n);../* Creat
54d10 65 20 61 20 6e 65 77 20 70 61 67 65 72 20 63 61  e a new pager ca
54d20 63 68 65 2e 0a 2a 2a 20 55 6e 64 65 72 20 6d 65  che..** Under me
54d30 6d 6f 72 79 20 73 74 72 65 73 73 2c 20 69 6e 76  mory stress, inv
54d40 6f 6b 65 20 78 53 74 72 65 73 73 20 74 6f 20 74  oke xStress to t
54d50 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 73  ry to make pages
54d60 20 63 6c 65 61 6e 2e 0a 2a 2a 20 4f 6e 6c 79 20   clean..** Only 
54d70 63 6c 65 61 6e 20 61 6e 64 20 75 6e 70 69 6e 6e  clean and unpinn
54d80 65 64 20 70 61 67 65 73 20 63 61 6e 20 62 65 20  ed pages can be 
54d90 72 65 63 6c 61 69 6d 65 64 2e 0a 2a 2f 0a 53 51  reclaimed..*/.SQ
54da0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
54db0 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4f  d sqlite3PcacheO
54dc0 70 65 6e 28 0a 20 20 69 6e 74 20 73 7a 50 61 67  pen(.  int szPag
54dd0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
54de0 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
54df0 20 65 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20   every page */. 
54e00 20 69 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20   int szExtra,   
54e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54e20 2f 2a 20 45 78 74 72 61 20 73 70 61 63 65 20 61  /* Extra space a
54e30 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 65  ssociated with e
54e40 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e  ach page */.  in
54e50 74 20 62 50 75 72 67 65 61 62 6c 65 2c 20 20 20  t bPurgeable,   
54e60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
54e70 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72  True if pages ar
54e80 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f  e on backing sto
54e90 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53  re */.  int (*xS
54ea0 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 20 50 67  tress)(void*, Pg
54eb0 48 64 72 2a 29 2c 20 2f 2a 20 43 61 6c 6c 20 74  Hdr*), /* Call t
54ec0 6f 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61  o try to make pa
54ed0 67 65 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76  ges clean */.  v
54ee0 6f 69 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20  oid *pStress,   
54ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
54f00 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74   Argument to xSt
54f10 72 65 73 73 20 2a 2f 0a 20 20 50 43 61 63 68 65  ress */.  PCache
54f20 20 2a 70 54 6f 49 6e 69 74 20 20 20 20 20 20 20   *pToInit       
54f30 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61           /* Prea
54f40 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 66  llocated space f
54f50 6f 72 20 74 68 65 20 50 43 61 63 68 65 20 2a 2f  or the PCache */
54f60 0a 29 3b 0a 0a 2f 2a 20 4d 6f 64 69 66 79 20 74  .);../* Modify t
54f70 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 66 74  he page-size aft
54f80 65 72 20 74 68 65 20 63 61 63 68 65 20 68 61 73  er the cache has
54f90 20 62 65 65 6e 20 63 72 65 61 74 65 64 2e 20 2a   been created. *
54fa0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
54fb0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
54fc0 63 68 65 53 65 74 50 61 67 65 53 69 7a 65 28 50  cheSetPageSize(P
54fd0 43 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 0a  Cache *, int);..
54fe0 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69  /* Return the si
54ff0 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 61  ze in bytes of a
55000 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20   PCache object. 
55010 20 55 73 65 64 20 74 6f 20 70 72 65 61 6c 6c 6f   Used to preallo
55020 63 61 74 65 0a 2a 2a 20 73 74 6f 72 61 67 65 20  cate.** storage 
55030 73 70 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  space..*/.SQLITE
55040 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
55050 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 76  ite3PcacheSize(v
55060 6f 69 64 29 3b 0a 0a 2f 2a 20 4f 6e 65 20 72 65  oid);../* One re
55070 6c 65 61 73 65 20 70 65 72 20 73 75 63 63 65 73  lease per succes
55080 73 66 75 6c 20 66 65 74 63 68 2e 20 20 50 61 67  sful fetch.  Pag
55090 65 20 69 73 20 70 69 6e 6e 65 64 20 75 6e 74 69  e is pinned unti
550a0 6c 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 20 52  l released..** R
550b0 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 65 64  eference counted
550c0 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  . .*/.SQLITE_PRI
550d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
550e0 50 63 61 63 68 65 46 65 74 63 68 28 50 43 61 63  PcacheFetch(PCac
550f0 68 65 2a 2c 20 50 67 6e 6f 2c 20 69 6e 74 20 63  he*, Pgno, int c
55100 72 65 61 74 65 46 6c 61 67 2c 20 50 67 48 64 72  reateFlag, PgHdr
55110 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
55120 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
55130 50 63 61 63 68 65 52 65 6c 65 61 73 65 28 50 67  PcacheRelease(Pg
55140 48 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50  Hdr*);..SQLITE_P
55150 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
55160 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 50 67  te3PcacheDrop(Pg
55170 48 64 72 2a 29 3b 20 20 20 20 20 20 20 20 20 2f  Hdr*);         /
55180 2a 20 52 65 6d 6f 76 65 20 70 61 67 65 20 66 72  * Remove page fr
55190 6f 6d 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49  om cache */.SQLI
551a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
551b0 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b  sqlite3PcacheMak
551c0 65 44 69 72 74 79 28 50 67 48 64 72 2a 29 3b 20  eDirty(PgHdr*); 
551d0 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20     /* Make sure 
551e0 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 64  page is marked d
551f0 69 72 74 79 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  irty */.SQLITE_P
55200 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
55210 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65  te3PcacheMakeCle
55220 61 6e 28 50 67 48 64 72 2a 29 3b 20 20 20 20 2f  an(PgHdr*);    /
55230 2a 20 4d 61 72 6b 20 61 20 73 69 6e 67 6c 65 20  * Mark a single 
55240 70 61 67 65 20 61 73 20 63 6c 65 61 6e 20 2a 2f  page as clean */
55250 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
55260 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
55270 68 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68  heCleanAll(PCach
55280 65 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 72 6b 20  e*);    /* Mark 
55290 61 6c 6c 20 64 69 72 74 79 20 6c 69 73 74 20 70  all dirty list p
552a0 61 67 65 73 20 61 73 20 63 6c 65 61 6e 20 2a 2f  ages as clean */
552b0 0a 0a 2f 2a 20 43 68 61 6e 67 65 20 61 20 70 61  ../* Change a pa
552c0 67 65 20 6e 75 6d 62 65 72 2e 20 20 55 73 65 64  ge number.  Used
552d0 20 62 79 20 69 6e 63 72 2d 76 61 63 75 75 6d 2e   by incr-vacuum.
552e0 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
552f0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
55300 63 61 63 68 65 4d 6f 76 65 28 50 67 48 64 72 2a  cacheMove(PgHdr*
55310 2c 20 50 67 6e 6f 29 3b 0a 0a 2f 2a 20 52 65 6d  , Pgno);../* Rem
55320 6f 76 65 20 61 6c 6c 20 70 61 67 65 73 20 77 69  ove all pages wi
55330 74 68 20 70 67 6e 6f 3e 78 2e 20 20 52 65 73 65  th pgno>x.  Rese
55340 74 20 74 68 65 20 63 61 63 68 65 20 69 66 20 78  t the cache if x
55350 3d 3d 30 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ==0 */.SQLITE_PR
55360 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
55370 65 33 50 63 61 63 68 65 54 72 75 6e 63 61 74 65  e3PcacheTruncate
55380 28 50 43 61 63 68 65 2a 2c 20 50 67 6e 6f 20 78  (PCache*, Pgno x
55390 29 3b 0a 0a 2f 2a 20 47 65 74 20 61 20 6c 69 73  );../* Get a lis
553a0 74 20 6f 66 20 61 6c 6c 20 64 69 72 74 79 20 70  t of all dirty p
553b0 61 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68  ages in the cach
553c0 65 2c 20 73 6f 72 74 65 64 20 62 79 20 70 61 67  e, sorted by pag
553d0 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 53 51 4c 49  e number */.SQLI
553e0 54 45 5f 50 52 49 56 41 54 45 20 50 67 48 64 72  TE_PRIVATE PgHdr
553f0 20 2a 73 71 6c 69 74 65 33 50 63 61 63 68 65 44   *sqlite3PcacheD
55400 69 72 74 79 4c 69 73 74 28 50 43 61 63 68 65 2a  irtyList(PCache*
55410 29 3b 0a 0a 2f 2a 20 52 65 73 65 74 20 61 6e 64  );../* Reset and
55420 20 63 6c 6f 73 65 20 74 68 65 20 63 61 63 68 65   close the cache
55430 20 6f 62 6a 65 63 74 20 2a 2f 0a 53 51 4c 49 54   object */.SQLIT
55440 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
55450 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73  qlite3PcacheClos
55460 65 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20  e(PCache*);../* 
55470 43 6c 65 61 72 20 66 6c 61 67 73 20 66 72 6f 6d  Clear flags from
55480 20 70 61 67 65 73 20 6f 66 20 74 68 65 20 70 61   pages of the pa
55490 67 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49  ge cache */.SQLI
554a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
554b0 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65  sqlite3PcacheCle
554c0 61 72 53 79 6e 63 46 6c 61 67 73 28 50 43 61 63  arSyncFlags(PCac
554d0 68 65 20 2a 29 3b 0a 0a 2f 2a 20 44 69 73 63 61  he *);../* Disca
554e0 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  rd the contents 
554f0 6f 66 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a  of the cache */.
55500 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
55510 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
55520 65 43 6c 65 61 72 28 50 43 61 63 68 65 2a 29 3b  eClear(PCache*);
55530 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ../* Return the 
55540 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
55550 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65  outstanding page
55560 20 72 65 66 65 72 65 6e 63 65 73 20 2a 2f 0a 53   references */.S
55570 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
55580 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52  t sqlite3PcacheR
55590 65 66 43 6f 75 6e 74 28 50 43 61 63 68 65 2a 29  efCount(PCache*)
555a0 3b 0a 0a 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20  ;../* Increment 
555b0 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f  the reference co
555c0 75 6e 74 20 6f 66 20 61 6e 20 65 78 69 73 74 69  unt of an existi
555d0 6e 67 20 70 61 67 65 20 2a 2f 0a 53 51 4c 49 54  ng page */.SQLIT
555e0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
555f0 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 28  qlite3PcacheRef(
55600 50 67 48 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45  PgHdr*);..SQLITE
55610 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
55620 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65  ite3PcachePageRe
55630 66 63 6f 75 6e 74 28 50 67 48 64 72 2a 29 3b 0a  fcount(PgHdr*);.
55640 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74  ./* Return the t
55650 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70  otal number of p
55660 61 67 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74  ages stored in t
55670 68 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49  he cache */.SQLI
55680 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
55690 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65  qlite3PcachePage
556a0 63 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a  count(PCache*);.
556b0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
556c0 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 29  ITE_CHECK_PAGES)
556d0 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
556e0 54 45 5f 44 45 42 55 47 29 0a 2f 2a 20 49 74 65  TE_DEBUG)./* Ite
556f0 72 61 74 65 20 74 68 72 6f 75 67 68 20 61 6c 6c  rate through all
55700 20 64 69 72 74 79 20 70 61 67 65 73 20 63 75 72   dirty pages cur
55710 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e  rently stored in
55720 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 69 73   the cache. This
55730 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73  .** interface is
55740 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20   only available 
55750 69 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f  if SQLITE_CHECK_
55760 50 41 47 45 53 20 69 73 20 64 65 66 69 6e 65 64  PAGES is defined
55770 20 77 68 65 6e 20 74 68 65 20 0a 2a 2a 20 6c 69   when the .** li
55780 62 72 61 72 79 20 69 73 20 62 75 69 6c 74 2e 0a  brary is built..
55790 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
557a0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
557b0 61 63 68 65 49 74 65 72 61 74 65 44 69 72 74 79  acheIterateDirty
557c0 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c  (PCache *pCache,
557d0 20 76 6f 69 64 20 28 2a 78 49 74 65 72 29 28 50   void (*xIter)(P
557e0 67 48 64 72 20 2a 29 29 3b 0a 23 65 6e 64 69 66  gHdr *));.#endif
557f0 0a 0a 2f 2a 20 53 65 74 20 61 6e 64 20 67 65 74  ../* Set and get
55800 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20 63   the suggested c
55810 61 63 68 65 2d 73 69 7a 65 20 66 6f 72 20 74 68  ache-size for th
55820 65 20 73 70 65 63 69 66 69 65 64 20 70 61 67 65  e specified page
55830 72 2d 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 49  r-cache..**.** I
55840 66 20 6e 6f 20 67 6c 6f 62 61 6c 20 6d 61 78 69  f no global maxi
55850 6d 75 6d 20 69 73 20 63 6f 6e 66 69 67 75 72 65  mum is configure
55860 64 2c 20 74 68 65 6e 20 74 68 65 20 73 79 73 74  d, then the syst
55870 65 6d 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c  em attempts to l
55880 69 6d 69 74 0a 2a 2a 20 74 68 65 20 74 6f 74 61  imit.** the tota
55890 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  l number of page
558a0 73 20 63 61 63 68 65 64 20 62 79 20 70 75 72 67  s cached by purg
558b0 65 61 62 6c 65 20 70 61 67 65 72 2d 63 61 63 68  eable pager-cach
558c0 65 73 20 74 6f 20 74 68 65 20 73 75 6d 0a 2a 2a  es to the sum.**
558d0 20 6f 66 20 74 68 65 20 73 75 67 67 65 73 74 65   of the suggeste
558e0 64 20 63 61 63 68 65 2d 73 69 7a 65 73 2e 0a 2a  d cache-sizes..*
558f0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
55900 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
55910 63 68 65 53 65 74 43 61 63 68 65 73 69 7a 65 28  cheSetCachesize(
55920 50 43 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a  PCache *, int);.
55930 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
55940 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ST.SQLITE_PRIVAT
55950 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61  E int sqlite3Pca
55960 63 68 65 47 65 74 43 61 63 68 65 73 69 7a 65 28  cheGetCachesize(
55970 50 43 61 63 68 65 20 2a 29 3b 0a 23 65 6e 64 69  PCache *);.#endi
55980 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
55990 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d  _ENABLE_MEMORY_M
559a0 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 20 54 72 79  ANAGEMENT./* Try
559b0 20 74 6f 20 72 65 74 75 72 6e 20 6d 65 6d 6f 72   to return memor
559c0 79 20 75 73 65 64 20 62 79 20 74 68 65 20 70 63  y used by the pc
559d0 61 63 68 65 20 6d 6f 64 75 6c 65 20 74 6f 20 74  ache module to t
559e0 68 65 20 6d 61 69 6e 20 6d 65 6d 6f 72 79 20 68  he main memory h
559f0 65 61 70 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  eap */.SQLITE_PR
55a00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
55a10 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 4d 65  3PcacheReleaseMe
55a20 6d 6f 72 79 28 69 6e 74 29 3b 0a 23 65 6e 64 69  mory(int);.#endi
55a30 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
55a40 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49  _TEST.SQLITE_PRI
55a50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
55a60 33 50 63 61 63 68 65 53 74 61 74 73 28 69 6e 74  3PcacheStats(int
55a70 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a  *,int*,int*,int*
55a80 29 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54  );.#endif..SQLIT
55a90 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
55aa0 71 6c 69 74 65 33 50 43 61 63 68 65 53 65 74 44  qlite3PCacheSetD
55ab0 65 66 61 75 6c 74 28 76 6f 69 64 29 3b 0a 0a 23  efault(void);..#
55ac0 65 6e 64 69 66 20 2f 2a 20 5f 50 43 41 43 48 45  endif /* _PCACHE
55ad0 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  _H_ */../*******
55ae0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70  ******* End of p
55af0 63 61 63 68 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  cache.h ********
55b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55b20 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
55b30 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
55b40 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
55b50 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e   off in sqliteIn
55b60 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
55b70 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  ******/../******
55b80 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
55b90 20 6f 73 2e 68 20 69 6e 20 74 68 65 20 6d 69 64   os.h in the mid
55ba0 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74  dle of sqliteInt
55bb0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
55bc0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
55bd0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
55be0 69 6c 65 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a  ile os.h *******
55bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55c10 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
55c20 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 36  001 September 16
55c30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
55c40 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
55c50 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
55c60 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
55c70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
55c80 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
55c90 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
55ca0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
55cb0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
55cc0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
55cd0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
55ce0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
55cf0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
55d00 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
55d10 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
55d20 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
55d30 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
55d40 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
55d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55d90 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
55da0 68 65 61 64 65 72 20 66 69 6c 65 20 28 74 6f 67  header file (tog
55db0 65 74 68 65 72 20 77 69 74 68 20 69 73 20 63 6f  ether with is co
55dc0 6d 70 61 6e 69 6f 6e 20 43 20 73 6f 75 72 63 65  mpanion C source
55dd0 2d 63 6f 64 65 20 66 69 6c 65 0a 2a 2a 20 22 6f  -code file.** "o
55de0 73 2e 63 22 29 20 61 74 74 65 6d 70 74 20 74 6f  s.c") attempt to
55df0 20 61 62 73 74 72 61 63 74 20 74 68 65 20 75 6e   abstract the un
55e00 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69  derlying operati
55e10 6e 67 20 73 79 73 74 65 6d 20 73 6f 20 74 68 61  ng system so tha
55e20 74 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20  t.** the SQLite 
55e30 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 77 6f 72  library will wor
55e40 6b 20 6f 6e 20 62 6f 74 68 20 50 4f 53 49 58 20  k on both POSIX 
55e50 61 6e 64 20 77 69 6e 64 6f 77 73 20 73 79 73 74  and windows syst
55e60 65 6d 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ems..**.** This 
55e70 68 65 61 64 65 72 20 66 69 6c 65 20 69 73 20 23  header file is #
55e80 69 6e 63 6c 75 64 65 2d 65 64 20 62 79 20 73 71  include-ed by sq
55e90 6c 69 74 65 49 6e 74 2e 68 20 61 6e 64 20 74 68  liteInt.h and th
55ea0 75 73 20 65 6e 64 73 20 75 70 0a 2a 2a 20 62 65  us ends up.** be
55eb0 69 6e 67 20 69 6e 63 6c 75 64 65 64 20 62 79 20  ing included by 
55ec0 65 76 65 72 79 20 73 6f 75 72 63 65 20 66 69 6c  every source fil
55ed0 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73  e..**.** $Id: os
55ee0 2e 68 2c 76 20 31 2e 31 30 38 20 32 30 30 39 2f  .h,v 1.108 2009/
55ef0 30 32 2f 30 35 20 31 36 3a 33 31 3a 34 36 20 64  02/05 16:31:46 d
55f00 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e  rh Exp $.*/.#ifn
55f10 64 65 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48  def _SQLITE_OS_H
55f20 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54  _.#define _SQLIT
55f30 45 5f 4f 53 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 46  E_OS_H_../*.** F
55f40 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20  igure out if we 
55f50 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68  are dealing with
55f60 20 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c 20   Unix, Windows, 
55f70 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a  or some other.**
55f80 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
55f90 6d 2e 20 20 41 66 74 65 72 20 74 68 65 20 66 6f  m.  After the fo
55fa0 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f 66  llowing block of
55fb0 20 70 72 65 70 72 6f 63 65 73 73 20 6d 61 63 72   preprocess macr
55fc0 6f 73 2c 0a 2a 2a 20 61 6c 6c 20 6f 66 20 53 51  os,.** all of SQ
55fd0 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 2c 20 53 51  LITE_OS_UNIX, SQ
55fe0 4c 49 54 45 5f 4f 53 5f 57 49 4e 2c 20 53 51 4c  LITE_OS_WIN, SQL
55ff0 49 54 45 5f 4f 53 5f 4f 53 32 2c 20 61 6e 64 20  ITE_OS_OS2, and 
56000 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 20  SQLITE_OS_OTHER 
56010 0a 2a 2a 20 77 69 6c 6c 20 64 65 66 69 6e 65 64  .** will defined
56020 20 74 6f 20 65 69 74 68 65 72 20 31 20 6f 72 20   to either 1 or 
56030 30 2e 20 20 4f 6e 65 20 6f 66 20 74 68 65 20 66  0.  One of the f
56040 6f 75 72 20 77 69 6c 6c 20 62 65 20 31 2e 20 20  our will be 1.  
56050 54 68 65 20 6f 74 68 65 72 20 0a 2a 2a 20 74 68  The other .** th
56060 72 65 65 20 77 69 6c 6c 20 62 65 20 30 2e 0a 2a  ree will be 0..*
56070 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  /.#if defined(SQ
56080 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23  LITE_OS_OTHER).#
56090 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54   if SQLITE_OS_OT
560a0 48 45 52 3d 3d 31 0a 23 20 20 20 75 6e 64 65 66  HER==1.#   undef
560b0 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 0a   SQLITE_OS_UNIX.
560c0 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54  #   define SQLIT
560d0 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20  E_OS_UNIX 0.#   
560e0 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f  undef SQLITE_OS_
560f0 57 49 4e 0a 23 20 20 20 64 65 66 69 6e 65 20 53  WIN.#   define S
56100 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23  QLITE_OS_WIN 0.#
56110 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f     undef SQLITE_
56120 4f 53 5f 4f 53 32 0a 23 20 20 20 64 65 66 69 6e  OS_OS2.#   defin
56130 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20  e SQLITE_OS_OS2 
56140 30 0a 23 20 65 6c 73 65 0a 23 20 20 20 75 6e 64  0.# else.#   und
56150 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48  ef SQLITE_OS_OTH
56160 45 52 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69  ER.# endif.#endi
56170 66 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  f.#if !defined(S
56180 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 29 20 26  QLITE_OS_UNIX) &
56190 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
561a0 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 20 64 65  E_OS_OTHER).# de
561b0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f  fine SQLITE_OS_O
561c0 54 48 45 52 20 30 0a 23 20 69 66 6e 64 65 66 20  THER 0.# ifndef 
561d0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20  SQLITE_OS_WIN.# 
561e0 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49    if defined(_WI
561f0 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  N32) || defined(
56200 57 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65  WIN32) || define
56210 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 20 7c 7c  d(__CYGWIN__) ||
56220 20 64 65 66 69 6e 65 64 28 5f 5f 4d 49 4e 47 57   defined(__MINGW
56230 33 32 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64  32__) || defined
56240 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 23  (__BORLANDC__).#
56250 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49       define SQLI
56260 54 45 5f 4f 53 5f 57 49 4e 20 31 0a 23 20 20 20  TE_OS_WIN 1.#   
56270 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
56280 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 20 20  OS_UNIX 0.#     
56290 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
562a0 5f 4f 53 32 20 30 0a 23 20 20 20 65 6c 69 66 20  _OS2 0.#   elif 
562b0 64 65 66 69 6e 65 64 28 5f 5f 45 4d 58 5f 5f 29  defined(__EMX__)
562c0 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53 32   || defined(_OS2
562d0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 4f 53 32  ) || defined(OS2
562e0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53  ) || defined(_OS
562f0 32 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  2_) || defined(_
56300 5f 4f 53 32 5f 5f 29 0a 23 20 20 20 20 20 64 65  _OS2__).#     de
56310 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57  fine SQLITE_OS_W
56320 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e  IN 0.#     defin
56330 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58  e SQLITE_OS_UNIX
56340 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20   0.#     define 
56350 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 31 0a  SQLITE_OS_OS2 1.
56360 23 20 20 20 65 6c 73 65 0a 23 20 20 20 20 20 64  #   else.#     d
56370 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f  efine SQLITE_OS_
56380 57 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69  WIN 0.#     defi
56390 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49  ne SQLITE_OS_UNI
563a0 58 20 31 0a 23 20 20 20 20 20 64 65 66 69 6e 65  X 1.#     define
563b0 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30   SQLITE_OS_OS2 0
563c0 0a 23 20 20 65 6e 64 69 66 0a 23 20 65 6c 73 65  .#  endif.# else
563d0 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54  .#  define SQLIT
563e0 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 64  E_OS_UNIX 0.#  d
563f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f  efine SQLITE_OS_
56400 4f 53 32 20 30 0a 23 20 65 6e 64 69 66 0a 23 65  OS2 0.# endif.#e
56410 6c 73 65 0a 23 20 69 66 6e 64 65 66 20 53 51 4c  lse.# ifndef SQL
56420 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 64 65  ITE_OS_WIN.#  de
56430 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57  fine SQLITE_OS_W
56440 49 4e 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e  IN 0.# endif.#en
56450 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72  dif../*.** Deter
56460 6d 69 6e 65 20 69 66 20 77 65 20 61 72 65 20 64  mine if we are d
56470 65 61 6c 69 6e 67 20 77 69 74 68 20 57 69 6e 64  ealing with Wind
56480 6f 77 73 43 45 20 2d 20 77 68 69 63 68 20 68 61  owsCE - which ha
56490 73 20 61 20 6d 75 63 68 0a 2a 2a 20 72 65 64 75  s a much.** redu
564a0 63 65 64 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 20  ced API..*/.#if 
564b0 64 65 66 69 6e 65 64 28 5f 57 49 4e 33 32 5f 57  defined(_WIN32_W
564c0 43 45 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  CE).# define SQL
564d0 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 31 0a 23  ITE_OS_WINCE 1.#
564e0 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51  else.# define SQ
564f0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 30 0a  LITE_OS_WINCE 0.
56500 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44  #endif.../*.** D
56510 65 66 69 6e 65 20 74 68 65 20 6d 61 78 69 6d 75  efine the maximu
56520 6d 20 73 69 7a 65 20 6f 66 20 61 20 74 65 6d 70  m size of a temp
56530 6f 72 61 72 79 20 66 69 6c 65 6e 61 6d 65 0a 2a  orary filename.*
56540 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  /.#if SQLITE_OS_
56550 57 49 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 77  WIN.# include <w
56560 69 6e 64 6f 77 73 2e 68 3e 0a 23 20 64 65 66 69  indows.h>.# defi
56570 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41  ne SQLITE_TEMPNA
56580 4d 45 5f 53 49 5a 45 20 28 4d 41 58 5f 50 41 54  ME_SIZE (MAX_PAT
56590 48 2b 35 30 29 0a 23 65 6c 69 66 20 53 51 4c 49  H+50).#elif SQLI
565a0 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 69 66 20 28  TE_OS_OS2.# if (
565b0 5f 5f 47 4e 55 43 5f 5f 20 3e 20 33 20 7c 7c 20  __GNUC__ > 3 || 
565c0 5f 5f 47 4e 55 43 5f 5f 20 3d 3d 20 33 20 26 26  __GNUC__ == 3 &&
565d0 20 5f 5f 47 4e 55 43 5f 4d 49 4e 4f 52 5f 5f 20   __GNUC_MINOR__ 
565e0 3e 3d 20 33 29 20 26 26 20 64 65 66 69 6e 65 64  >= 3) && defined
565f0 28 4f 53 32 5f 48 49 47 48 5f 4d 45 4d 4f 52 59  (OS2_HIGH_MEMORY
56600 29 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 6f 73  ).#  include <os
56610 32 73 61 66 65 2e 68 3e 20 2f 2a 20 68 61 73 20  2safe.h> /* has 
56620 74 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 62  to be included b
56630 65 66 6f 72 65 20 6f 73 32 2e 68 20 66 6f 72 20  efore os2.h for 
56640 6c 69 6e 6b 69 6e 67 20 74 6f 20 77 6f 72 6b 20  linking to work 
56650 2a 2f 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66  */.# endif.# def
56660 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 44 41 54 45  ine INCL_DOSDATE
56670 54 49 4d 45 0a 23 20 64 65 66 69 6e 65 20 49 4e  TIME.# define IN
56680 43 4c 5f 44 4f 53 46 49 4c 45 4d 47 52 0a 23 20  CL_DOSFILEMGR.# 
56690 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 45  define INCL_DOSE
566a0 52 52 4f 52 53 0a 23 20 64 65 66 69 6e 65 20 49  RRORS.# define I
566b0 4e 43 4c 5f 44 4f 53 4d 49 53 43 0a 23 20 64 65  NCL_DOSMISC.# de
566c0 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 50 52 4f  fine INCL_DOSPRO
566d0 43 45 53 53 0a 23 20 64 65 66 69 6e 65 20 49 4e  CESS.# define IN
566e0 43 4c 5f 44 4f 53 4d 4f 44 55 4c 45 4d 47 52 0a  CL_DOSMODULEMGR.
566f0 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f  # define INCL_DO
56700 53 53 45 4d 41 50 48 4f 52 45 53 0a 23 20 69 6e  SSEMAPHORES.# in
56710 63 6c 75 64 65 20 3c 6f 73 32 2e 68 3e 0a 23 20  clude <os2.h>.# 
56720 69 6e 63 6c 75 64 65 20 3c 75 63 6f 6e 76 2e 68  include <uconv.h
56730 3e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  >.# define SQLIT
56740 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20  E_TEMPNAME_SIZE 
56750 28 43 43 48 4d 41 58 50 41 54 48 43 4f 4d 50 29  (CCHMAXPATHCOMP)
56760 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
56770 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f  SQLITE_TEMPNAME_
56780 53 49 5a 45 20 32 30 30 0a 23 65 6e 64 69 66 0a  SIZE 200.#endif.
56790 0a 2f 2a 20 49 66 20 74 68 65 20 53 45 54 5f 46  ./* If the SET_F
567a0 55 4c 4c 53 59 4e 43 20 6d 61 63 72 6f 20 69 73  ULLSYNC macro is
567b0 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 61 62 6f   not defined abo
567c0 76 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 69 74  ve, then make it
567d0 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 0a 2a 2f 0a 23  .** a no-op.*/.#
567e0 69 66 6e 64 65 66 20 53 45 54 5f 46 55 4c 4c 53  ifndef SET_FULLS
567f0 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 53 45 54  YNC.# define SET
56800 5f 46 55 4c 4c 53 59 4e 43 28 78 2c 79 29 0a 23  _FULLSYNC(x,y).#
56810 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
56820 20 64 65 66 61 75 6c 74 20 73 69 7a 65 20 6f 66   default size of
56830 20 61 20 64 69 73 6b 20 73 65 63 74 6f 72 0a 2a   a disk sector.*
56840 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
56850 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f  _DEFAULT_SECTOR_
56860 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51  SIZE.# define SQ
56870 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43  LITE_DEFAULT_SEC
56880 54 4f 52 5f 53 49 5a 45 20 35 31 32 0a 23 65 6e  TOR_SIZE 512.#en
56890 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 6d 70 6f  dif../*.** Tempo
568a0 72 61 72 79 20 66 69 6c 65 73 20 61 72 65 20 6e  rary files are n
568b0 61 6d 65 64 20 73 74 61 72 74 69 6e 67 20 77 69  amed starting wi
568c0 74 68 20 74 68 69 73 20 70 72 65 66 69 78 20 66  th this prefix f
568d0 6f 6c 6c 6f 77 65 64 20 62 79 20 31 36 20 72 61  ollowed by 16 ra
568e0 6e 64 6f 6d 0a 2a 2a 20 61 6c 70 68 61 6e 75 6d  ndom.** alphanum
568f0 65 72 69 63 20 63 68 61 72 61 63 74 65 72 73 2c  eric characters,
56900 20 61 6e 64 20 6e 6f 20 66 69 6c 65 20 65 78 74   and no file ext
56910 65 6e 73 69 6f 6e 2e 20 54 68 65 79 20 61 72 65  ension. They are
56920 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 0a 2a   stored in the.*
56930 2a 20 4f 53 27 73 20 73 74 61 6e 64 61 72 64 20  * OS's standard 
56940 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 64  temporary file d
56950 69 72 65 63 74 6f 72 79 2c 20 61 6e 64 20 61 72  irectory, and ar
56960 65 20 64 65 6c 65 74 65 64 20 70 72 69 6f 72 20  e deleted prior 
56970 74 6f 20 65 78 69 74 2e 0a 2a 2a 20 49 66 20 73  to exit..** If s
56980 71 6c 69 74 65 20 69 73 20 62 65 69 6e 67 20 65  qlite is being e
56990 6d 62 65 64 64 65 64 20 69 6e 20 61 6e 6f 74 68  mbedded in anoth
569a0 65 72 20 70 72 6f 67 72 61 6d 2c 20 79 6f 75 20  er program, you 
569b0 6d 61 79 20 77 69 73 68 20 74 6f 20 63 68 61 6e  may wish to chan
569c0 67 65 20 74 68 65 0a 2a 2a 20 70 72 65 66 69 78  ge the.** prefix
569d0 20 74 6f 20 72 65 66 6c 65 63 74 20 79 6f 75 72   to reflect your
569e0 20 70 72 6f 67 72 61 6d 27 73 20 6e 61 6d 65 2c   program's name,
569f0 20 73 6f 20 74 68 61 74 20 69 66 20 79 6f 75 72   so that if your
56a00 20 70 72 6f 67 72 61 6d 20 65 78 69 74 73 0a 2a   program exits.*
56a10 2a 20 70 72 65 6d 61 74 75 72 65 6c 79 2c 20 6f  * prematurely, o
56a20 6c 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  ld temporary fil
56a30 65 73 20 63 61 6e 20 62 65 20 65 61 73 69 6c 79  es can be easily
56a40 20 69 64 65 6e 74 69 66 69 65 64 2e 20 54 68 69   identified. Thi
56a50 73 20 63 61 6e 20 62 65 20 64 6f 6e 65 0a 2a 2a  s can be done.**
56a60 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 5f   using -DSQLITE_
56a70 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58  TEMP_FILE_PREFIX
56a80 3d 6d 79 70 72 65 66 69 78 5f 20 6f 6e 20 74 68  =myprefix_ on th
56a90 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61  e compiler comma
56aa0 6e 64 20 6c 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 32  nd line..**.** 2
56ab0 30 30 36 2d 31 30 2d 33 31 3a 20 20 54 68 65 20  006-10-31:  The 
56ac0 64 65 66 61 75 6c 74 20 70 72 65 66 69 78 20 75  default prefix u
56ad0 73 65 64 20 74 6f 20 62 65 20 22 73 71 6c 69 74  sed to be "sqlit
56ae0 65 5f 22 2e 20 20 42 75 74 20 74 68 65 6e 0a 2a  e_".  But then.*
56af0 2a 20 4d 63 61 66 65 65 20 73 74 61 72 74 65 64  * Mcafee started
56b00 20 75 73 69 6e 67 20 53 51 4c 69 74 65 20 69 6e   using SQLite in
56b10 20 74 68 65 69 72 20 61 6e 74 69 2d 76 69 72 75   their anti-viru
56b20 73 20 70 72 6f 64 75 63 74 20 61 6e 64 20 69 74  s product and it
56b30 0a 2a 2a 20 73 74 61 72 74 65 64 20 70 75 74 74  .** started putt
56b40 69 6e 67 20 66 69 6c 65 73 20 77 69 74 68 20 74  ing files with t
56b50 68 65 20 22 73 71 6c 69 74 65 22 20 6e 61 6d 65  he "sqlite" name
56b60 20 69 6e 20 74 68 65 20 63 3a 2f 74 65 6d 70 20   in the c:/temp 
56b70 66 6f 6c 64 65 72 2e 0a 2a 2a 20 54 68 69 73 20  folder..** This 
56b80 61 6e 6e 6f 79 65 64 20 6d 61 6e 79 20 77 69 6e  annoyed many win
56b90 64 6f 77 73 20 75 73 65 72 73 2e 20 20 54 68 6f  dows users.  Tho
56ba0 73 65 20 75 73 65 72 73 20 77 6f 75 6c 64 20 74  se users would t
56bb0 68 65 6e 20 64 6f 20 61 20 0a 2a 2a 20 47 6f 6f  hen do a .** Goo
56bc0 67 6c 65 20 73 65 61 72 63 68 20 66 6f 72 20 22  gle search for "
56bd0 73 71 6c 69 74 65 22 2c 20 66 69 6e 64 20 74 68  sqlite", find th
56be0 65 20 74 65 6c 65 70 68 6f 6e 65 20 6e 75 6d 62  e telephone numb
56bf0 65 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 65  ers of the.** de
56c00 76 65 6c 6f 70 65 72 73 20 61 6e 64 20 63 61 6c  velopers and cal
56c10 6c 20 74 6f 20 77 61 6b 65 20 74 68 65 6d 20 75  l to wake them u
56c20 70 20 61 74 20 6e 69 67 68 74 20 61 6e 64 20 63  p at night and c
56c30 6f 6d 70 6c 61 69 6e 2e 0a 2a 2a 20 46 6f 72 20  omplain..** For 
56c40 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 65  this reason, the
56c50 20 64 65 66 61 75 6c 74 20 6e 61 6d 65 20 70 72   default name pr
56c60 65 66 69 78 20 69 73 20 63 68 61 6e 67 65 64 20  efix is changed 
56c70 74 6f 20 62 65 20 22 73 71 6c 69 74 65 22 20 0a  to be "sqlite" .
56c80 2a 2a 20 73 70 65 6c 6c 65 64 20 62 61 63 6b 77  ** spelled backw
56c90 61 72 64 73 2e 20 20 53 6f 20 74 68 65 20 74 65  ards.  So the te
56ca0 6d 70 20 66 69 6c 65 73 20 61 72 65 20 73 74 69  mp files are sti
56cb0 6c 6c 20 69 64 65 6e 74 69 66 69 65 64 2c 20 62  ll identified, b
56cc0 75 74 0a 2a 2a 20 61 6e 79 62 6f 64 79 20 73 6d  ut.** anybody sm
56cd0 61 72 74 20 65 6e 6f 75 67 68 20 74 6f 20 66 69  art enough to fi
56ce0 67 75 72 65 20 6f 75 74 20 74 68 65 20 63 6f 64  gure out the cod
56cf0 65 20 69 73 20 61 6c 73 6f 20 6c 69 6b 65 6c 79  e is also likely
56d00 20 73 6d 61 72 74 0a 2a 2a 20 65 6e 6f 75 67 68   smart.** enough
56d10 20 74 6f 20 6b 6e 6f 77 20 74 68 61 74 20 63 61   to know that ca
56d20 6c 6c 69 6e 67 20 74 68 65 20 64 65 76 65 6c 6f  lling the develo
56d30 70 65 72 20 77 69 6c 6c 20 6e 6f 74 20 68 65 6c  per will not hel
56d40 70 20 67 65 74 20 72 69 64 0a 2a 2a 20 6f 66 20  p get rid.** of 
56d50 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66  the file..*/.#if
56d60 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d 50  ndef SQLITE_TEMP
56d70 5f 46 49 4c 45 5f 50 52 45 46 49 58 0a 23 20 64  _FILE_PREFIX.# d
56d80 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d  efine SQLITE_TEM
56d90 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 20 22 65  P_FILE_PREFIX "e
56da0 74 69 6c 71 73 5f 22 0a 23 65 6e 64 69 66 0a 0a  tilqs_".#endif..
56db0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
56dc0 69 6e 67 20 76 61 6c 75 65 73 20 6d 61 79 20 62  ing values may b
56dd0 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
56de0 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
56df0 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 4c  to.** sqlite3OsL
56e00 6f 63 6b 28 29 2e 20 54 68 65 20 76 61 72 69 6f  ock(). The vario
56e10 75 73 20 6c 6f 63 6b 73 20 65 78 68 69 62 69 74  us locks exhibit
56e20 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
56e30 65 6d 61 6e 74 69 63 73 3a 0a 2a 2a 0a 2a 2a 20  emantics:.**.** 
56e40 53 48 41 52 45 44 3a 20 20 20 20 41 6e 79 20 6e  SHARED:    Any n
56e50 75 6d 62 65 72 20 6f 66 20 70 72 6f 63 65 73 73  umber of process
56e60 65 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 53 48  es may hold a SH
56e70 41 52 45 44 20 6c 6f 63 6b 20 73 69 6d 75 6c 74  ARED lock simult
56e80 61 6e 65 6f 75 73 6c 79 2e 0a 2a 2a 20 52 45 53  aneously..** RES
56e90 45 52 56 45 44 3a 20 20 41 20 73 69 6e 67 6c 65  ERVED:  A single
56ea0 20 70 72 6f 63 65 73 73 20 6d 61 79 20 68 6f 6c   process may hol
56eb0 64 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  d a RESERVED loc
56ec0 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 74 0a 2a  k on a file at.*
56ed0 2a 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 79  *            any
56ee0 20 74 69 6d 65 2e 20 4f 74 68 65 72 20 70 72 6f   time. Other pro
56ef0 63 65 73 73 65 73 20 6d 61 79 20 68 6f 6c 64 20  cesses may hold 
56f00 61 6e 64 20 6f 62 74 61 69 6e 20 6e 65 77 20 53  and obtain new S
56f10 48 41 52 45 44 20 6c 6f 63 6b 73 2e 0a 2a 2a 20  HARED locks..** 
56f20 50 45 4e 44 49 4e 47 3a 20 20 20 41 20 73 69 6e  PENDING:   A sin
56f30 67 6c 65 20 70 72 6f 63 65 73 73 20 6d 61 79 20  gle process may 
56f40 68 6f 6c 64 20 61 20 50 45 4e 44 49 4e 47 20 6c  hold a PENDING l
56f50 6f 63 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 74  ock on a file at
56f60 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 61  .**            a
56f70 6e 79 20 6f 6e 65 20 74 69 6d 65 2e 20 45 78 69  ny one time. Exi
56f80 73 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63  sting SHARED loc
56f90 6b 73 20 6d 61 79 20 70 65 72 73 69 73 74 2c 20  ks may persist, 
56fa0 62 75 74 20 6e 6f 20 6e 65 77 0a 2a 2a 20 20 20  but no new.**   
56fb0 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 20           SHARED 
56fc0 6c 6f 63 6b 73 20 6d 61 79 20 62 65 20 6f 62 74  locks may be obt
56fd0 61 69 6e 65 64 20 62 79 20 6f 74 68 65 72 20 70  ained by other p
56fe0 72 6f 63 65 73 73 65 73 2e 0a 2a 2a 20 45 58 43  rocesses..** EXC
56ff0 4c 55 53 49 56 45 3a 20 41 6e 20 45 58 43 4c 55  LUSIVE: An EXCLU
57000 53 49 56 45 20 6c 6f 63 6b 20 70 72 65 63 6c 75  SIVE lock preclu
57010 64 65 73 20 61 6c 6c 20 6f 74 68 65 72 20 6c 6f  des all other lo
57020 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 50 45 4e 44 49  cks..**.** PENDI
57030 4e 47 5f 4c 4f 43 4b 20 6d 61 79 20 6e 6f 74 20  NG_LOCK may not 
57040 62 65 20 70 61 73 73 65 64 20 64 69 72 65 63 74  be passed direct
57050 6c 79 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 4c  ly to sqlite3OsL
57060 6f 63 6b 28 29 2e 20 49 6e 73 74 65 61 64 2c 20  ock(). Instead, 
57070 61 0a 2a 2a 20 70 72 6f 63 65 73 73 20 74 68 61  a.** process tha
57080 74 20 72 65 71 75 65 73 74 73 20 61 6e 20 45 58  t requests an EX
57090 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6d 61 79  CLUSIVE lock may
570a0 20 61 63 74 75 61 6c 6c 79 20 6f 62 74 61 69 6e   actually obtain
570b0 20 61 20 50 45 4e 44 49 4e 47 0a 2a 2a 20 6c 6f   a PENDING.** lo
570c0 63 6b 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20  ck. This can be 
570d0 75 70 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45  upgraded to an E
570e0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 62 79  XCLUSIVE lock by
570f0 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 63 61   a subsequent ca
57100 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33  ll to.** sqlite3
57110 4f 73 4c 6f 63 6b 28 29 2e 0a 2a 2f 0a 23 64 65  OsLock()..*/.#de
57120 66 69 6e 65 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20  fine NO_LOCK    
57130 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53       0.#define S
57140 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20 31  HARED_LOCK     1
57150 0a 23 64 65 66 69 6e 65 20 52 45 53 45 52 56 45  .#define RESERVE
57160 44 5f 4c 4f 43 4b 20 20 20 32 0a 23 64 65 66 69  D_LOCK   2.#defi
57170 6e 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20  ne PENDING_LOCK 
57180 20 20 20 33 0a 23 64 65 66 69 6e 65 20 45 58 43     3.#define EXC
57190 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 34 0a 0a  LUSIVE_LOCK  4..
571a0 2f 2a 0a 2a 2a 20 46 69 6c 65 20 4c 6f 63 6b 69  /*.** File Locki
571b0 6e 67 20 4e 6f 74 65 73 3a 20 20 28 4d 6f 73 74  ng Notes:  (Most
571c0 6c 79 20 61 62 6f 75 74 20 77 69 6e 64 6f 77 73  ly about windows
571d0 20 62 75 74 20 61 6c 73 6f 20 73 6f 6d 65 20 69   but also some i
571e0 6e 66 6f 20 66 6f 72 20 55 6e 69 78 29 0a 2a 2a  nfo for Unix).**
571f0 0a 2a 2a 20 57 65 20 63 61 6e 6e 6f 74 20 75 73  .** We cannot us
57200 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 6f  e LockFileEx() o
57210 72 20 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29  r UnlockFileEx()
57220 20 6f 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20   on Win95/98/ME 
57230 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 6f 73 65  because.** those
57240 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 6e   functions are n
57250 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 53  ot available.  S
57260 6f 20 77 65 20 75 73 65 20 6f 6e 6c 79 20 4c 6f  o we use only Lo
57270 63 6b 46 69 6c 65 28 29 20 61 6e 64 0a 2a 2a 20  ckFile() and.** 
57280 55 6e 6c 6f 63 6b 46 69 6c 65 28 29 2e 0a 2a 2a  UnlockFile()..**
57290 0a 2a 2a 20 4c 6f 63 6b 46 69 6c 65 28 29 20 70  .** LockFile() p
572a0 72 65 76 65 6e 74 73 20 6e 6f 74 20 6a 75 73 74  revents not just
572b0 20 77 72 69 74 69 6e 67 20 62 75 74 20 61 6c 73   writing but als
572c0 6f 20 72 65 61 64 69 6e 67 20 62 79 20 6f 74 68  o reading by oth
572d0 65 72 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a 2a  er processes..**
572e0 20 41 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 69   A SHARED_LOCK i
572f0 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f  s obtained by lo
57300 63 6b 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 72  cking a single r
57310 61 6e 64 6f 6d 6c 79 2d 63 68 6f 73 65 6e 20 0a  andomly-chosen .
57320 2a 2a 20 62 79 74 65 20 6f 75 74 20 6f 66 20 61  ** byte out of a
57330 20 73 70 65 63 69 66 69 63 20 72 61 6e 67 65 20   specific range 
57340 6f 66 20 62 79 74 65 73 2e 20 54 68 65 20 6c 6f  of bytes. The lo
57350 63 6b 20 62 79 74 65 20 69 73 20 6f 62 74 61 69  ck byte is obtai
57360 6e 65 64 20 61 74 20 0a 2a 2a 20 72 61 6e 64 6f  ned at .** rando
57370 6d 20 73 6f 20 74 77 6f 20 73 65 70 61 72 61 74  m so two separat
57380 65 20 72 65 61 64 65 72 73 20 63 61 6e 20 70 72  e readers can pr
57390 6f 62 61 62 6c 79 20 61 63 63 65 73 73 20 74 68  obably access th
573a0 65 20 66 69 6c 65 20 61 74 20 74 68 65 20 0a 2a  e file at the .*
573b0 2a 20 73 61 6d 65 20 74 69 6d 65 2c 20 75 6e 6c  * same time, unl
573c0 65 73 73 20 74 68 65 79 20 61 72 65 20 75 6e 6c  ess they are unl
573d0 75 63 6b 79 20 61 6e 64 20 63 68 6f 6f 73 65 20  ucky and choose 
573e0 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 20 62 79  the same lock by
573f0 74 65 2e 0a 2a 2a 20 41 6e 20 45 58 43 4c 55 53  te..** An EXCLUS
57400 49 56 45 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61  IVE_LOCK is obta
57410 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20  ined by locking 
57420 61 6c 6c 20 62 79 74 65 73 20 69 6e 20 74 68 65  all bytes in the
57430 20 72 61 6e 67 65 2e 0a 2a 2a 20 54 68 65 72 65   range..** There
57440 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 6f 6e 65   can only be one
57450 20 77 72 69 74 65 72 2e 20 20 41 20 52 45 53 45   writer.  A RESE
57460 52 56 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62 74  RVED_LOCK is obt
57470 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67  ained by locking
57480 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 62 79 74  .** a single byt
57490 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 68  e of the file th
574a0 61 74 20 69 73 20 64 65 73 69 67 6e 61 74 65 64  at is designated
574b0 20 61 73 20 74 68 65 20 72 65 73 65 72 76 65 64   as the reserved
574c0 20 6c 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 41   lock byte..** A
574d0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 69 73   PENDING_LOCK is
574e0 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63   obtained by loc
574f0 6b 69 6e 67 20 61 20 64 65 73 69 67 6e 61 74 65  king a designate
57500 64 20 62 79 74 65 20 64 69 66 66 65 72 65 6e 74  d byte different
57510 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 52 45 53   from.** the RES
57520 45 52 56 45 44 5f 4c 4f 43 4b 20 62 79 74 65 2e  ERVED_LOCK byte.
57530 0a 2a 2a 0a 2a 2a 20 4f 6e 20 57 69 6e 4e 54 2f  .**.** On WinNT/
57540 32 4b 2f 58 50 20 73 79 73 74 65 6d 73 2c 20 4c  2K/XP systems, L
57550 6f 63 6b 46 69 6c 65 45 78 28 29 20 61 6e 64 20  ockFileEx() and 
57560 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29 20 61  UnlockFileEx() a
57570 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 0a 2a 2a  re available,.**
57580 20 77 68 69 63 68 20 6d 65 61 6e 73 20 77 65 20   which means we 
57590 63 61 6e 20 75 73 65 20 72 65 61 64 65 72 2f 77  can use reader/w
575a0 72 69 74 65 72 20 6c 6f 63 6b 73 2e 20 20 57 68  riter locks.  Wh
575b0 65 6e 20 72 65 61 64 65 72 2f 77 72 69 74 65 72  en reader/writer
575c0 20 6c 6f 63 6b 73 0a 2a 2a 20 61 72 65 20 75 73   locks.** are us
575d0 65 64 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20  ed, the lock is 
575e0 70 6c 61 63 65 64 20 6f 6e 20 74 68 65 20 73 61  placed on the sa
575f0 6d 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65  me range of byte
57600 73 20 74 68 61 74 20 69 73 20 75 73 65 64 0a 2a  s that is used.*
57610 2a 20 66 6f 72 20 70 72 6f 62 61 62 69 6c 69 73  * for probabilis
57620 74 69 63 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 57  tic locking in W
57630 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 48 65 6e  in95/98/ME.  Hen
57640 63 65 2c 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  ce, the locking 
57650 73 63 68 65 6d 65 0a 2a 2a 20 77 69 6c 6c 20 73  scheme.** will s
57660 75 70 70 6f 72 74 20 74 77 6f 20 6f 72 20 6d 6f  upport two or mo
57670 72 65 20 57 69 6e 39 35 20 72 65 61 64 65 72 73  re Win95 readers
57680 20 6f 72 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20   or two or more 
57690 57 69 6e 4e 54 20 72 65 61 64 65 72 73 2e 0a 2a  WinNT readers..*
576a0 2a 20 42 75 74 20 61 20 73 69 6e 67 6c 65 20 57  * But a single W
576b0 69 6e 39 35 20 72 65 61 64 65 72 20 77 69 6c 6c  in95 reader will
576c0 20 6c 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 57 69   lock out all Wi
576d0 6e 4e 54 20 72 65 61 64 65 72 73 20 61 6e 64 20  nNT readers and 
576e0 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 57 69 6e 4e  a single.** WinN
576f0 54 20 72 65 61 64 65 72 20 77 69 6c 6c 20 6c 6f  T reader will lo
57700 63 6b 20 6f 75 74 20 61 6c 6c 20 6f 74 68 65 72  ck out all other
57710 20 57 69 6e 39 35 20 72 65 61 64 65 72 73 2e 0a   Win95 readers..
57720 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  **.** The follow
57730 69 6e 67 20 23 64 65 66 69 6e 65 73 20 73 70 65  ing #defines spe
57740 63 69 66 79 20 74 68 65 20 72 61 6e 67 65 20 6f  cify the range o
57750 66 20 62 79 74 65 73 20 75 73 65 64 20 66 6f 72  f bytes used for
57760 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 53 48 41   locking..** SHA
57770 52 45 44 5f 53 49 5a 45 20 69 73 20 74 68 65 20  RED_SIZE is the 
57780 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
57790 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65  available in the
577a0 20 70 6f 6f 6c 20 66 72 6f 6d 20 77 68 69 63 68   pool from which
577b0 0a 2a 2a 20 61 20 72 61 6e 64 6f 6d 20 62 79 74  .** a random byt
577c0 65 20 69 73 20 73 65 6c 65 63 74 65 64 20 66 6f  e is selected fo
577d0 72 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e  r a shared lock.
577e0 20 20 54 68 65 20 70 6f 6f 6c 20 6f 66 20 62 79    The pool of by
577f0 74 65 73 20 66 6f 72 0a 2a 2a 20 73 68 61 72 65  tes for.** share
57800 64 20 6c 6f 63 6b 73 20 62 65 67 69 6e 73 20 61  d locks begins a
57810 74 20 53 48 41 52 45 44 5f 46 49 52 53 54 2e 20  t SHARED_FIRST. 
57820 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6d 65 20  .**.** The same 
57830 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79  locking strategy
57840 20 61 6e 64 0a 2a 2a 20 62 79 74 65 20 72 61 6e   and.** byte ran
57850 67 65 73 20 61 72 65 20 75 73 65 64 20 66 6f 72  ges are used for
57860 20 55 6e 69 78 2e 20 20 54 68 69 73 20 6c 65 61   Unix.  This lea
57870 76 65 73 20 6f 70 65 6e 20 74 68 65 20 70 6f 73  ves open the pos
57880 73 69 62 6c 69 74 79 20 6f 66 20 68 61 76 69 6e  siblity of havin
57890 67 0a 2a 2a 20 63 6c 69 65 6e 74 73 20 6f 6e 20  g.** clients on 
578a0 77 69 6e 39 35 2c 20 77 69 6e 4e 54 2c 20 61 6e  win95, winNT, an
578b0 64 20 75 6e 69 78 20 61 6c 6c 20 74 61 6c 6b 69  d unix all talki
578c0 6e 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20 73  ng to the same s
578d0 68 61 72 65 64 20 66 69 6c 65 0a 2a 2a 20 61 6e  hared file.** an
578e0 64 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f  d all locking co
578f0 72 72 65 63 74 6c 79 2e 20 20 54 6f 20 64 6f 20  rrectly.  To do 
57900 73 6f 20 77 6f 75 6c 64 20 72 65 71 75 69 72 65  so would require
57910 20 74 68 61 74 20 73 61 6d 62 61 20 28 6f 72 20   that samba (or 
57920 77 68 61 74 65 76 65 72 0a 2a 2a 20 74 6f 6f 6c  whatever.** tool
57930 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 66   is being used f
57940 6f 72 20 66 69 6c 65 20 73 68 61 72 69 6e 67 29  or file sharing)
57950 20 69 6d 70 6c 65 6d 65 6e 74 73 20 6c 6f 63 6b   implements lock
57960 73 20 63 6f 72 72 65 63 74 6c 79 20 62 65 74 77  s correctly betw
57970 65 65 6e 0a 2a 2a 20 77 69 6e 64 6f 77 73 20 61  een.** windows a
57980 6e 64 20 75 6e 69 78 2e 20 20 49 27 6d 20 67 75  nd unix.  I'm gu
57990 65 73 73 69 6e 67 20 74 68 61 74 20 69 73 6e 27  essing that isn'
579a0 74 20 6c 69 6b 65 6c 79 20 74 6f 20 68 61 70 70  t likely to happ
579b0 65 6e 2c 20 62 75 74 20 62 79 0a 2a 2a 20 75 73  en, but by.** us
579c0 69 6e 67 20 74 68 65 20 73 61 6d 65 20 6c 6f 63  ing the same loc
579d0 6b 69 6e 67 20 72 61 6e 67 65 20 77 65 20 61 72  king range we ar
579e0 65 20 61 74 20 6c 65 61 73 74 20 6f 70 65 6e 20  e at least open 
579f0 74 6f 20 74 68 65 20 70 6f 73 73 69 62 69 6c 69  to the possibili
57a00 74 79 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 69 6e  ty..**.** Lockin
57a10 67 20 69 6e 20 77 69 6e 64 6f 77 73 20 69 73 20  g in windows is 
57a20 6d 61 6e 64 69 74 6f 72 79 2e 20 20 46 6f 72 20  manditory.  For 
57a30 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 77 65 20  this reason, we 
57a40 63 61 6e 6e 6f 74 20 73 74 6f 72 65 0a 2a 2a 20  cannot store.** 
57a50 61 63 74 75 61 6c 20 64 61 74 61 20 69 6e 20 74  actual data in t
57a60 68 65 20 62 79 74 65 73 20 75 73 65 64 20 66 6f  he bytes used fo
57a70 72 20 6c 6f 63 6b 69 6e 67 2e 20 20 54 68 65 20  r locking.  The 
57a80 70 61 67 65 72 20 6e 65 76 65 72 20 61 6c 6c 6f  pager never allo
57a90 63 61 74 65 73 0a 2a 2a 20 74 68 65 20 70 61 67  cates.** the pag
57aa0 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 6c  es involved in l
57ab0 6f 63 6b 69 6e 67 20 74 68 65 72 65 66 6f 72 65  ocking therefore
57ac0 2e 20 20 53 48 41 52 45 44 5f 53 49 5a 45 20 69  .  SHARED_SIZE i
57ad0 73 20 73 65 6c 65 63 74 65 64 20 73 6f 0a 2a 2a  s selected so.**
57ae0 20 74 68 61 74 20 61 6c 6c 20 6c 6f 63 6b 73 20   that all locks 
57af0 77 69 6c 6c 20 66 69 74 20 6f 6e 20 61 20 73 69  will fit on a si
57b00 6e 67 6c 65 20 70 61 67 65 20 65 76 65 6e 20 61  ngle page even a
57b10 74 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 70 61  t the minimum pa
57b20 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 50 45 4e 44  ge size..** PEND
57b30 49 4e 47 5f 42 59 54 45 20 64 65 66 69 6e 65 73  ING_BYTE defines
57b40 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
57b50 66 20 74 68 65 20 6c 6f 63 6b 73 2e 20 20 42 79  f the locks.  By
57b60 20 64 65 66 61 75 6c 74 20 50 45 4e 44 49 4e 47   default PENDING
57b70 5f 42 59 54 45 0a 2a 2a 20 69 73 20 73 65 74 20  _BYTE.** is set 
57b80 68 69 67 68 20 73 6f 20 74 68 61 74 20 77 65 20  high so that we 
57b90 64 6f 6e 27 74 20 68 61 76 65 20 74 6f 20 61 6c  don't have to al
57ba0 6c 6f 63 61 74 65 20 61 6e 20 75 6e 75 73 65 64  locate an unused
57bb0 20 70 61 67 65 20 65 78 63 65 70 74 0a 2a 2a 20   page except.** 
57bc0 66 6f 72 20 76 65 72 79 20 6c 61 72 67 65 20 64  for very large d
57bd0 61 74 61 62 61 73 65 73 2e 20 20 42 75 74 20 6f  atabases.  But o
57be0 6e 65 20 73 68 6f 75 6c 64 20 74 65 73 74 20 74  ne should test t
57bf0 68 65 20 70 61 67 65 20 73 6b 69 70 70 69 6e 67  he page skipping
57c00 20 6c 6f 67 69 63 20 0a 2a 2a 20 62 79 20 73 65   logic .** by se
57c10 74 74 69 6e 67 20 50 45 4e 44 49 4e 47 5f 42 59  tting PENDING_BY
57c20 54 45 20 6c 6f 77 20 61 6e 64 20 72 75 6e 6e 69  TE low and runni
57c30 6e 67 20 74 68 65 20 65 6e 74 69 72 65 20 72 65  ng the entire re
57c40 67 72 65 73 73 69 6f 6e 20 73 75 69 74 65 2e 0a  gression suite..
57c50 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74  **.** Changing t
57c60 68 65 20 76 61 6c 75 65 20 6f 66 20 50 45 4e 44  he value of PEND
57c70 49 4e 47 5f 42 59 54 45 20 72 65 73 75 6c 74 73  ING_BYTE results
57c80 20 69 6e 20 61 20 73 75 62 74 6c 79 20 69 6e 63   in a subtly inc
57c90 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a 20 66 69 6c  ompatible.** fil
57ca0 65 20 66 6f 72 6d 61 74 2e 20 20 44 65 70 65 6e  e format.  Depen
57cb0 64 69 6e 67 20 6f 6e 20 68 6f 77 20 69 74 20 69  ding on how it i
57cc0 73 20 63 68 61 6e 67 65 64 2c 20 79 6f 75 20 6d  s changed, you m
57cd0 69 67 68 74 20 6e 6f 74 20 6e 6f 74 69 63 65 0a  ight not notice.
57ce0 2a 2a 20 74 68 65 20 69 6e 63 6f 6d 70 61 74 69  ** the incompati
57cf0 62 69 6c 69 74 79 20 72 69 67 68 74 20 61 77 61  bility right awa
57d00 79 2c 20 65 76 65 6e 20 72 75 6e 6e 69 6e 67 20  y, even running 
57d10 61 20 66 75 6c 6c 20 72 65 67 72 65 73 73 69 6f  a full regressio
57d20 6e 20 74 65 73 74 2e 0a 2a 2a 20 54 68 65 20 64  n test..** The d
57d30 65 66 61 75 6c 74 20 6c 6f 63 61 74 69 6f 6e 20  efault location 
57d40 6f 66 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20  of PENDING_BYTE 
57d50 69 73 20 74 68 65 20 66 69 72 73 74 20 62 79 74  is the first byt
57d60 65 20 70 61 73 74 20 74 68 65 0a 2a 2a 20 31 47  e past the.** 1G
57d70 42 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2a 0a 2a  B boundary..**.*
57d80 2f 0a 23 64 65 66 69 6e 65 20 50 45 4e 44 49 4e  /.#define PENDIN
57d90 47 5f 42 59 54 45 20 20 20 20 20 20 73 71 6c 69  G_BYTE      sqli
57da0 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 0a 23  te3PendingByte.#
57db0 64 65 66 69 6e 65 20 52 45 53 45 52 56 45 44 5f  define RESERVED_
57dc0 42 59 54 45 20 20 20 20 20 28 50 45 4e 44 49 4e  BYTE     (PENDIN
57dd0 47 5f 42 59 54 45 2b 31 29 0a 23 64 65 66 69 6e  G_BYTE+1).#defin
57de0 65 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 20  e SHARED_FIRST  
57df0 20 20 20 20 28 50 45 4e 44 49 4e 47 5f 42 59 54      (PENDING_BYT
57e00 45 2b 32 29 0a 23 64 65 66 69 6e 65 20 53 48 41  E+2).#define SHA
57e10 52 45 44 5f 53 49 5a 45 20 20 20 20 20 20 20 35  RED_SIZE       5
57e20 31 30 0a 0a 2f 2a 0a 2a 2a 20 57 72 61 70 70 65  10../*.** Wrappe
57e30 72 20 61 72 6f 75 6e 64 20 4f 53 20 73 70 65 63  r around OS spec
57e40 69 66 69 63 20 73 71 6c 69 74 65 33 5f 6f 73 5f  ific sqlite3_os_
57e50 69 6e 69 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e  init() function.
57e60 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
57e70 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
57e80 49 6e 69 74 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20  Init(void);../* 
57e90 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f  .** Functions fo
57ea0 72 20 61 63 63 65 73 73 69 6e 67 20 73 71 6c 69  r accessing sqli
57eb0 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73  te3_file methods
57ec0 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
57ed0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
57ee0 73 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66  sClose(sqlite3_f
57ef0 69 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ile*);.SQLITE_PR
57f00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
57f10 33 4f 73 52 65 61 64 28 73 71 6c 69 74 65 33 5f  3OsRead(sqlite3_
57f20 66 69 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e  file*, void*, in
57f30 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65  t amt, i64 offse
57f40 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
57f50 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
57f60 57 72 69 74 65 28 73 71 6c 69 74 65 33 5f 66 69  Write(sqlite3_fi
57f70 6c 65 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a  le*, const void*
57f80 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f  , int amt, i64 o
57f90 66 66 73 65 74 29 3b 0a 53 51 4c 49 54 45 5f 50  ffset);.SQLITE_P
57fa0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
57fb0 65 33 4f 73 54 72 75 6e 63 61 74 65 28 73 71 6c  e3OsTruncate(sql
57fc0 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 36 34 20  ite3_file*, i64 
57fd0 73 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  size);.SQLITE_PR
57fe0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
57ff0 33 4f 73 53 79 6e 63 28 73 71 6c 69 74 65 33 5f  3OsSync(sqlite3_
58000 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  file*, int);.SQL
58010 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
58020 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a  sqlite3OsFileSiz
58030 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  e(sqlite3_file*,
58040 20 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51   i64 *pSize);.SQ
58050 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
58060 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73   sqlite3OsLock(s
58070 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e  qlite3_file*, in
58080 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
58090 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
580a0 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  Unlock(sqlite3_f
580b0 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ile*, int);.SQLI
580c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
580d0 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73  qlite3OsCheckRes
580e0 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65  ervedLock(sqlite
580f0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
58100 2a 70 52 65 73 4f 75 74 29 3b 0a 53 51 4c 49 54  *pResOut);.SQLIT
58110 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
58120 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72  lite3OsFileContr
58130 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  ol(sqlite3_file*
58140 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 64 65  ,int,void*);.#de
58150 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54  fine SQLITE_FCNT
58160 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 20 30  L_DB_UNCHANGED 0
58170 78 63 61 30 39 33 66 61 30 0a 53 51 4c 49 54 45  xca093fa0.SQLITE
58180 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
58190 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65  ite3OsSectorSize
581a0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
581b0 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  d);.SQLITE_PRIVA
581c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
581d0 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
581e0 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69  stics(sqlite3_fi
581f0 6c 65 20 2a 69 64 29 3b 0a 0a 2f 2a 20 0a 2a 2a  le *id);../* .**
58200 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61   Functions for a
58210 63 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33  ccessing sqlite3
58220 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 0a 2a 2f  _vfs methods .*/
58230 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
58240 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  int sqlite3OsOpe
58250 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  n(sqlite3_vfs *,
58260 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73   const char *, s
58270 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e  qlite3_file*, in
58280 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54  t, int *);.SQLIT
58290 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
582a0 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71  lite3OsDelete(sq
582b0 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e  lite3_vfs *, con
582c0 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b  st char *, int);
582d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
582e0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63 63  int sqlite3OsAcc
582f0 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ess(sqlite3_vfs 
58300 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  *, const char *,
58310 20 69 6e 74 2c 20 69 6e 74 20 2a 70 52 65 73 4f   int, int *pResO
58320 75 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ut);.SQLITE_PRIV
58330 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
58340 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 73 71  sFullPathname(sq
58350 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e  lite3_vfs *, con
58360 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20  st char *, int, 
58370 63 68 61 72 20 2a 29 3b 0a 23 69 66 6e 64 65 66  char *);.#ifndef
58380 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41   SQLITE_OMIT_LOA
58390 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49  D_EXTENSION.SQLI
583a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
583b0 2a 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e  *sqlite3OsDlOpen
583c0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20  (sqlite3_vfs *, 
583d0 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53  const char *);.S
583e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
583f0 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72  id sqlite3OsDlEr
58400 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ror(sqlite3_vfs 
58410 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b  *, int, char *);
58420 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
58430 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 4f 73  void (*sqlite3Os
58440 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66  DlSym(sqlite3_vf
58450 73 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 63 6f 6e  s *, void *, con
58460 73 74 20 63 68 61 72 20 2a 29 29 28 76 6f 69 64  st char *))(void
58470 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
58480 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73  E void sqlite3Os
58490 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  DlClose(sqlite3_
584a0 76 66 73 20 2a 2c 20 76 6f 69 64 20 2a 29 3b 0a  vfs *, void *);.
584b0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
584c0 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e  _OMIT_LOAD_EXTEN
584d0 53 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  SION */.SQLITE_P
584e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
584f0 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73  e3OsRandomness(s
58500 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e  qlite3_vfs *, in
58510 74 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49  t, char *);.SQLI
58520 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
58530 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 73 71  qlite3OsSleep(sq
58540 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74  lite3_vfs *, int
58550 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
58560 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43  E int sqlite3OsC
58570 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74  urrentTime(sqlit
58580 65 33 5f 76 66 73 20 2a 2c 20 64 6f 75 62 6c 65  e3_vfs *, double
58590 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  *);../*.** Conve
585a0 6e 69 65 6e 63 65 20 66 75 6e 63 74 69 6f 6e 73  nience functions
585b0 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 6e 64   for opening and
585c0 20 63 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20 75   closing files u
585d0 73 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65 33  sing .** sqlite3
585e0 5f 6d 61 6c 6c 6f 63 28 29 20 74 6f 20 6f 62 74  _malloc() to obt
585f0 61 69 6e 20 73 70 61 63 65 20 66 6f 72 20 74 68  ain space for th
58600 65 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 73 74  e file-handle st
58610 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49  ructure..*/.SQLI
58620 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
58630 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c  qlite3OsOpenMall
58640 6f 63 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  oc(sqlite3_vfs *
58650 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  , const char *, 
58660 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2a 2c  sqlite3_file **,
58670 20 69 6e 74 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49   int,int*);.SQLI
58680 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
58690 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65  qlite3OsCloseFre
586a0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
586b0 29 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53  );..#endif /* _S
586c0 51 4c 49 54 45 5f 4f 53 5f 48 5f 20 2a 2f 0a 0a  QLITE_OS_H_ */..
586d0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
586e0 45 6e 64 20 6f 66 20 6f 73 2e 68 20 2a 2a 2a 2a  End of os.h ****
586f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
58720 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
58730 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
58740 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
58750 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
58760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
58770 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
58780 49 6e 63 6c 75 64 65 20 6d 75 74 65 78 2e 68 20  Include mutex.h 
58790 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
587a0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
587b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
587c0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
587d0 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78  Begin file mutex
587e0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
587f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
58810 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73  /*.** 2007 Augus
58820 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  t 28.**.** The a
58830 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
58840 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
58850 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
58860 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
58870 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
58880 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
58890 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
588a0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
588b0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
588c0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
588d0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
588e0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
588f0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
58900 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
58910 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
58920 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
58930 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
58940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58980 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ***.**.** This f
58990 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ile contains the
589a0 20 63 6f 6d 6d 6f 6e 20 68 65 61 64 65 72 20 66   common header f
589b0 6f 72 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70  or all mutex imp
589c0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a  lementations..**
589d0 20 54 68 65 20 73 71 6c 69 74 65 49 6e 74 2e 68   The sqliteInt.h
589e0 20 68 65 61 64 65 72 20 23 69 6e 63 6c 75 64 65   header #include
589f0 73 20 74 68 69 73 20 66 69 6c 65 20 73 6f 20 74  s this file so t
58a00 68 61 74 20 69 74 20 69 73 20 61 76 61 69 6c 61  hat it is availa
58a10 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 73 6f  ble.** to all so
58a20 75 72 63 65 20 66 69 6c 65 73 2e 20 20 57 65 20  urce files.  We 
58a30 62 72 65 61 6b 20 69 74 20 6f 75 74 20 69 6e 20  break it out in 
58a40 61 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b 65 65  an effort to kee
58a50 70 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 62 65  p the code.** be
58a60 74 74 65 72 20 6f 72 67 61 6e 69 7a 65 64 2e 0a  tter organized..
58a70 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20 73 6f 75  **.** NOTE:  sou
58a80 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75 6c 64  rce files should
58a90 20 2a 6e 6f 74 2a 20 23 69 6e 63 6c 75 64 65 20   *not* #include 
58aa0 74 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65  this header file
58ab0 20 64 69 72 65 63 74 6c 79 2e 0a 2a 2a 20 53 6f   directly..** So
58ac0 75 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75 6c  urce files shoul
58ad0 64 20 23 69 6e 63 6c 75 64 65 20 74 68 65 20 73  d #include the s
58ae0 71 6c 69 74 65 49 6e 74 2e 68 20 66 69 6c 65 20  qliteInt.h file 
58af0 61 6e 64 20 6c 65 74 20 74 68 61 74 20 66 69 6c  and let that fil
58b00 65 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 74 68 69  e.** include thi
58b10 73 20 6f 6e 65 20 69 6e 64 69 72 65 63 74 6c 79  s one indirectly
58b20 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74  ..**.** $Id: mut
58b30 65 78 2e 68 2c 76 20 31 2e 39 20 32 30 30 38 2f  ex.h,v 1.9 2008/
58b40 31 30 2f 30 37 20 31 35 3a 32 35 3a 34 38 20 64  10/07 15:25:48 d
58b50 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a  rh Exp $.*/.../*
58b60 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 77  .** Figure out w
58b70 68 61 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  hat version of t
58b80 68 65 20 63 6f 64 65 20 74 6f 20 75 73 65 2e 20  he code to use. 
58b90 20 54 68 65 20 63 68 6f 69 63 65 73 20 61 72 65   The choices are
58ba0 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f  .**.**   SQLITE_
58bb0 4d 55 54 45 58 5f 4f 4d 49 54 20 20 20 20 20 20  MUTEX_OMIT      
58bc0 20 20 20 4e 6f 20 6d 75 74 65 78 20 6c 6f 67 69     No mutex logi
58bd0 63 2e 20 20 4e 6f 74 20 65 76 65 6e 20 73 74 75  c.  Not even stu
58be0 62 73 2e 20 20 54 68 65 0a 2a 2a 20 20 20 20 20  bs.  The.**     
58bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58c00 20 20 20 20 20 20 20 20 6d 75 74 65 78 65 73 20          mutexes 
58c10 69 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20 63 61 6e  implemention can
58c20 6e 6f 74 20 62 65 20 6f 76 65 72 72 69 64 64 65  not be overridde
58c30 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  n.**            
58c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58c50 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a   at start-time..
58c60 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d  **.**   SQLITE_M
58c70 55 54 45 58 5f 4e 4f 4f 50 20 20 20 20 20 20 20  UTEX_NOOP       
58c80 20 20 46 6f 72 20 73 69 6e 67 6c 65 2d 74 68 72    For single-thr
58c90 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f  eaded applicatio
58ca0 6e 73 2e 20 20 4e 6f 0a 2a 2a 20 20 20 20 20 20  ns.  No.**      
58cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58cc0 20 20 20 20 20 20 20 6d 75 74 75 61 6c 20 65 78         mutual ex
58cd0 63 6c 75 73 69 6f 6e 20 69 73 20 70 72 6f 76 69  clusion is provi
58ce0 64 65 64 2e 20 20 42 75 74 20 74 68 69 73 0a 2a  ded.  But this.*
58cf0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
58d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6d                im
58d10 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 63 61 6e  plementation can
58d20 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 61   be overridden a
58d30 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
58d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58d50 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a   start-time..**.
58d60 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  **   SQLITE_MUTE
58d70 58 5f 50 54 48 52 45 41 44 53 20 20 20 20 20 46  X_PTHREADS     F
58d80 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65  or multi-threade
58d90 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f  d applications o
58da0 6e 20 55 6e 69 78 2e 0a 2a 2a 0a 2a 2a 20 20 20  n Unix..**.**   
58db0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32  SQLITE_MUTEX_W32
58dc0 20 20 20 20 20 20 20 20 20 20 46 6f 72 20 6d 75            For mu
58dd0 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70 70  lti-threaded app
58de0 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 57 69 6e  lications on Win
58df0 33 32 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49  32..**.**   SQLI
58e00 54 45 5f 4d 55 54 45 58 5f 4f 53 32 20 20 20 20  TE_MUTEX_OS2    
58e10 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d        For multi-
58e20 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61  threaded applica
58e30 74 69 6f 6e 73 20 6f 6e 20 4f 53 2f 32 2e 0a 2a  tions on OS/2..*
58e40 2f 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 54 48  /.#if !SQLITE_TH
58e50 52 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e  READSAFE.# defin
58e60 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f  e SQLITE_MUTEX_O
58e70 4d 49 54 0a 23 65 6e 64 69 66 0a 23 69 66 20 53  MIT.#endif.#if S
58e80 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
58e90 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
58ea0 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 0a  ITE_MUTEX_NOOP).
58eb0 23 20 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  #  if SQLITE_OS_
58ec0 55 4e 49 58 0a 23 20 20 20 20 64 65 66 69 6e 65  UNIX.#    define
58ed0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54   SQLITE_MUTEX_PT
58ee0 48 52 45 41 44 53 0a 23 20 20 65 6c 69 66 20 53  HREADS.#  elif S
58ef0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20  QLITE_OS_WIN.#  
58f00 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
58f10 4d 55 54 45 58 5f 57 33 32 0a 23 20 20 65 6c 69  MUTEX_W32.#  eli
58f20 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a  f SQLITE_OS_OS2.
58f30 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49  #    define SQLI
58f40 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 23 20 20  TE_MUTEX_OS2.#  
58f50 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e 65  else.#    define
58f60 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f   SQLITE_MUTEX_NO
58f70 4f 50 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64  OP.#  endif.#end
58f80 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
58f90 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a  E_MUTEX_OMIT./*.
58fa0 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20  ** If this is a 
58fb0 6e 6f 2d 6f 70 20 69 6d 70 6c 65 6d 65 6e 74 61  no-op implementa
58fc0 74 69 6f 6e 2c 20 69 6d 70 6c 65 6d 65 6e 74 20  tion, implement 
58fd0 65 76 65 72 79 74 68 69 6e 67 20 61 73 20 6d 61  everything as ma
58fe0 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  cros..*/.#define
58ff0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
59000 6c 6c 6f 63 28 58 29 20 20 20 20 28 28 73 71 6c  lloc(X)    ((sql
59010 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 29 0a 23  ite3_mutex*)8).#
59020 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d  define sqlite3_m
59030 75 74 65 78 5f 66 72 65 65 28 58 29 0a 23 64 65  utex_free(X).#de
59040 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74  fine sqlite3_mut
59050 65 78 5f 65 6e 74 65 72 28 58 29 0a 23 64 65 66  ex_enter(X).#def
59060 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ine sqlite3_mute
59070 78 5f 74 72 79 28 58 29 20 20 20 20 20 20 53 51  x_try(X)      SQ
59080 4c 49 54 45 5f 4f 4b 0a 23 64 65 66 69 6e 65 20  LITE_OK.#define 
59090 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
590a0 61 76 65 28 58 29 0a 23 64 65 66 69 6e 65 20 73  ave(X).#define s
590b0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
590c0 64 28 58 29 20 20 20 20 20 31 0a 23 64 65 66 69  d(X)     1.#defi
590d0 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
590e0 5f 6e 6f 74 68 65 6c 64 28 58 29 20 20 31 0a 23  _notheld(X)  1.#
590f0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75  define sqlite3Mu
59100 74 65 78 41 6c 6c 6f 63 28 58 29 20 20 20 20 20  texAlloc(X)     
59110 20 28 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78   ((sqlite3_mutex
59120 2a 29 38 29 0a 23 64 65 66 69 6e 65 20 73 71 6c  *)8).#define sql
59130 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 29 20  ite3MutexInit() 
59140 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b         SQLITE_OK
59150 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
59160 4d 75 74 65 78 45 6e 64 28 29 0a 23 65 6e 64 69  MutexEnd().#endi
59170 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c  f /* defined(SQL
59180 49 54 45 5f 4f 4d 49 54 5f 4d 55 54 45 58 29 20  ITE_OMIT_MUTEX) 
59190 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
591a0 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78  *** End of mutex
591b0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
591c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
591d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
591e0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
591f0 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
59200 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
59210 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   in sqliteInt.h 
59220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59230 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68  **/.../*.** Each
59240 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74   database file t
59250 6f 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79  o be accessed by
59260 20 74 68 65 20 73 79 73 74 65 6d 20 69 73 20 61   the system is a
59270 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66  n instance.** of
59280 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
59290 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 72 65  tructure.  There
592a0 20 61 72 65 20 6e 6f 72 6d 61 6c 6c 79 20 74 77   are normally tw
592b0 6f 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63  o of these struc
592c0 74 75 72 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20  tures.** in the 
592d0 73 71 6c 69 74 65 2e 61 44 62 5b 5d 20 61 72 72  sqlite.aDb[] arr
592e0 61 79 2e 20 20 61 44 62 5b 30 5d 20 69 73 20 74  ay.  aDb[0] is t
592f0 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
59300 20 66 69 6c 65 20 61 6e 64 0a 2a 2a 20 61 44 62   file and.** aDb
59310 5b 31 5d 20 69 73 20 74 68 65 20 64 61 74 61 62  [1] is the datab
59320 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 74 6f  ase file used to
59330 20 68 6f 6c 64 20 74 65 6d 70 6f 72 61 72 79 20   hold temporary 
59340 74 61 62 6c 65 73 2e 20 20 41 64 64 69 74 69 6f  tables.  Additio
59350 6e 61 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 73  nal.** databases
59360 20 6d 61 79 20 62 65 20 61 74 74 61 63 68 65 64   may be attached
59370 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 44 62 20 7b  ..*/.struct Db {
59380 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  .  char *zName; 
59390 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
593a0 6f 66 20 74 68 69 73 20 64 61 74 61 62 61 73 65  of this database
593b0 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74   */.  Btree *pBt
593c0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ;          /* Th
593d0 65 20 42 2a 54 72 65 65 20 73 74 72 75 63 74 75  e B*Tree structu
593e0 72 65 20 66 6f 72 20 74 68 69 73 20 64 61 74 61  re for this data
593f0 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75  base file */.  u
59400 38 20 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 20  8 inTrans;      
59410 20 20 20 20 2f 2a 20 30 3a 20 6e 6f 74 20 77 72      /* 0: not wr
59420 69 74 61 62 6c 65 2e 20 20 31 3a 20 54 72 61 6e  itable.  1: Tran
59430 73 61 63 74 69 6f 6e 2e 20 20 32 3a 20 43 68 65  saction.  2: Che
59440 63 6b 70 6f 69 6e 74 20 2a 2f 0a 20 20 75 38 20  ckpoint */.  u8 
59450 73 61 66 65 74 79 5f 6c 65 76 65 6c 3b 20 20 20  safety_level;   
59460 20 20 2f 2a 20 48 6f 77 20 61 67 67 72 65 73 73    /* How aggress
59470 69 76 65 20 61 74 20 73 79 6e 63 69 6e 67 20 64  ive at syncing d
59480 61 74 61 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 20  ata to disk */. 
59490 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61   Schema *pSchema
594a0 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72  ;     /* Pointer
594b0 20 74 6f 20 64 61 74 61 62 61 73 65 20 73 63 68   to database sch
594c0 65 6d 61 20 28 70 6f 73 73 69 62 6c 79 20 73 68  ema (possibly sh
594d0 61 72 65 64 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  ared) */.};../*.
594e0 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
594f0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
59500 73 74 72 75 63 74 75 72 65 20 73 74 6f 72 65 73  structure stores
59510 20 61 20 64 61 74 61 62 61 73 65 20 73 63 68 65   a database sche
59520 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ma..**.** If the
59530 72 65 20 61 72 65 20 6e 6f 20 76 69 72 74 75 61  re are no virtua
59540 6c 20 74 61 62 6c 65 73 20 63 6f 6e 66 69 67 75  l tables configu
59550 72 65 64 20 69 6e 20 74 68 69 73 20 73 63 68 65  red in this sche
59560 6d 61 2c 20 74 68 65 0a 2a 2a 20 53 63 68 65 6d  ma, the.** Schem
59570 61 2e 64 62 20 76 61 72 69 61 62 6c 65 20 69 73  a.db variable is
59580 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 41 66   set to NULL. Af
59590 74 65 72 20 74 68 65 20 66 69 72 73 74 20 76 69  ter the first vi
595a0 72 74 75 61 6c 20 74 61 62 6c 65 0a 2a 2a 20 68  rtual table.** h
595b0 61 73 20 62 65 65 6e 20 61 64 64 65 64 2c 20 69  as been added, i
595c0 74 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e  t is set to poin
595d0 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  t to the databas
595e0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a  e connection .**
595f0 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20   used to create 
59600 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  the connection. 
59610 4f 6e 63 65 20 61 20 76 69 72 74 75 61 6c 20 74  Once a virtual t
59620 61 62 6c 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a  able has been.**
59630 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 53 63   added to the Sc
59640 68 65 6d 61 20 73 74 72 75 63 74 75 72 65 20 61  hema structure a
59650 6e 64 20 74 68 65 20 53 63 68 65 6d 61 2e 64 62  nd the Schema.db
59660 20 76 61 72 69 61 62 6c 65 20 70 6f 70 75 6c 61   variable popula
59670 74 65 64 2c 20 0a 2a 2a 20 6f 6e 6c 79 20 74 68  ted, .** only th
59680 61 74 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  at database conn
59690 65 63 74 69 6f 6e 20 6d 61 79 20 75 73 65 20 74  ection may use t
596a0 68 65 20 53 63 68 65 6d 61 20 74 6f 20 70 72 65  he Schema to pre
596b0 70 61 72 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65  pare .** stateme
596c0 6e 74 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53  nts..*/.struct S
596d0 63 68 65 6d 61 20 7b 0a 20 20 69 6e 74 20 73 63  chema {.  int sc
596e0 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b 20 20 20 2f  hema_cookie;   /
596f0 2a 20 44 61 74 61 62 61 73 65 20 73 63 68 65 6d  * Database schem
59700 61 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72  a version number
59710 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 2a   for this file *
59720 2f 0a 20 20 48 61 73 68 20 74 62 6c 48 61 73 68  /.  Hash tblHash
59730 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20  ;        /* All 
59740 74 61 62 6c 65 73 20 69 6e 64 65 78 65 64 20 62  tables indexed b
59750 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68  y name */.  Hash
59760 20 69 64 78 48 61 73 68 3b 20 20 20 20 20 20 20   idxHash;       
59770 20 2f 2a 20 41 6c 6c 20 28 6e 61 6d 65 64 29 20   /* All (named) 
59780 69 6e 64 69 63 65 73 20 69 6e 64 65 78 65 64 20  indices indexed 
59790 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73  by name */.  Has
597a0 68 20 74 72 69 67 48 61 73 68 3b 20 20 20 20 20  h trigHash;     
597b0 20 20 2f 2a 20 41 6c 6c 20 74 72 69 67 67 65 72    /* All trigger
597c0 73 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d  s indexed by nam
597d0 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 53  e */.  Table *pS
597e0 65 71 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 54  eqTab;      /* T
597f0 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  he sqlite_sequen
59800 63 65 20 74 61 62 6c 65 20 75 73 65 64 20 62 79  ce table used by
59810 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a   AUTOINCREMENT *
59820 2f 0a 20 20 75 38 20 66 69 6c 65 5f 66 6f 72 6d  /.  u8 file_form
59830 61 74 3b 20 20 20 20 20 20 2f 2a 20 53 63 68 65  at;      /* Sche
59840 6d 61 20 66 6f 72 6d 61 74 20 76 65 72 73 69 6f  ma format versio
59850 6e 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20  n for this file 
59860 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 20  */.  u8 enc;    
59870 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78            /* Tex
59880 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20  t encoding used 
59890 62 79 20 74 68 69 73 20 64 61 74 61 62 61 73 65  by this database
598a0 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 3b   */.  u16 flags;
598b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c             /* Fl
598c0 61 67 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ags associated w
598d0 69 74 68 20 74 68 69 73 20 73 63 68 65 6d 61 20  ith this schema 
598e0 2a 2f 0a 20 20 69 6e 74 20 63 61 63 68 65 5f 73  */.  int cache_s
598f0 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ize;      /* Num
59900 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20  ber of pages to 
59910 75 73 65 20 69 6e 20 74 68 65 20 63 61 63 68 65  use in the cache
59920 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
59930 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
59940 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 20 2a  ABLE.  sqlite3 *
59950 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 22  db;         /* "
59960 4f 77 6e 65 72 22 20 63 6f 6e 6e 65 63 74 69 6f  Owner" connectio
59970 6e 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 20 61  n. See comment a
59980 62 6f 76 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d  bove */.#endif.}
59990 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6d  ;../*.** These m
599a0 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65  acros can be use
599b0 64 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c 20  d to test, set, 
599c0 6f 72 20 63 6c 65 61 72 20 62 69 74 73 20 69 6e  or clear bits in
599d0 20 74 68 65 20 0a 2a 2a 20 44 62 2e 66 6c 61 67   the .** Db.flag
599e0 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66  s field..*/.#def
599f0 69 6e 65 20 44 62 48 61 73 50 72 6f 70 65 72 74  ine DbHasPropert
59a00 79 28 44 2c 49 2c 50 29 20 20 20 20 20 28 28 28  y(D,I,P)     (((
59a10 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65  D)->aDb[I].pSche
59a20 6d 61 2d 3e 66 6c 61 67 73 26 28 50 29 29 3d 3d  ma->flags&(P))==
59a30 28 50 29 29 0a 23 64 65 66 69 6e 65 20 44 62 48  (P)).#define DbH
59a40 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 44 2c  asAnyProperty(D,
59a50 49 2c 50 29 20 20 28 28 28 44 29 2d 3e 61 44 62  I,P)  (((D)->aDb
59a60 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61  [I].pSchema->fla
59a70 67 73 26 28 50 29 29 21 3d 30 29 0a 23 64 65 66  gs&(P))!=0).#def
59a80 69 6e 65 20 44 62 53 65 74 50 72 6f 70 65 72 74  ine DbSetPropert
59a90 79 28 44 2c 49 2c 50 29 20 20 20 20 20 28 44 29  y(D,I,P)     (D)
59aa0 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61  ->aDb[I].pSchema
59ab0 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a 23 64 65  ->flags|=(P).#de
59ac0 66 69 6e 65 20 44 62 43 6c 65 61 72 50 72 6f 70  fine DbClearProp
59ad0 65 72 74 79 28 44 2c 49 2c 50 29 20 20 20 28 44  erty(D,I,P)   (D
59ae0 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d  )->aDb[I].pSchem
59af0 61 2d 3e 66 6c 61 67 73 26 3d 7e 28 50 29 0a 0a  a->flags&=~(P)..
59b00 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61  /*.** Allowed va
59b10 6c 75 65 73 20 66 6f 72 20 74 68 65 20 44 42 2e  lues for the DB.
59b20 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2a 0a  flags field..**.
59b30 2a 2a 20 54 68 65 20 44 42 5f 53 63 68 65 6d 61  ** The DB_Schema
59b40 4c 6f 61 64 65 64 20 66 6c 61 67 20 69 73 20 73  Loaded flag is s
59b50 65 74 20 61 66 74 65 72 20 74 68 65 20 64 61 74  et after the dat
59b60 61 62 61 73 65 20 73 63 68 65 6d 61 20 68 61 73  abase schema has
59b70 20 62 65 65 6e 0a 2a 2a 20 72 65 61 64 20 69 6e   been.** read in
59b80 74 6f 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68  to internal hash
59b90 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 44   tables..**.** D
59ba0 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 20 6d  B_UnresetViews m
59bb0 65 61 6e 73 20 74 68 61 74 20 6f 6e 65 20 6f 72  eans that one or
59bc0 20 6d 6f 72 65 20 76 69 65 77 73 20 68 61 76 65   more views have
59bd0 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 68   column names th
59be0 61 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20  at.** have been 
59bf0 66 69 6c 6c 65 64 20 6f 75 74 2e 20 20 49 66 20  filled out.  If 
59c00 74 68 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67  the schema chang
59c10 65 73 2c 20 74 68 65 73 65 20 63 6f 6c 75 6d 6e  es, these column
59c20 20 6e 61 6d 65 73 20 6d 69 67 68 74 0a 2a 2a 20   names might.** 
59c30 63 68 61 6e 67 65 73 20 61 6e 64 20 73 6f 20 74  changes and so t
59c40 68 65 20 76 69 65 77 20 77 69 6c 6c 20 6e 65 65  he view will nee
59c50 64 20 74 6f 20 62 65 20 72 65 73 65 74 2e 0a 2a  d to be reset..*
59c60 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f 53 63 68  /.#define DB_Sch
59c70 65 6d 61 4c 6f 61 64 65 64 20 20 20 20 30 78 30  emaLoaded    0x0
59c80 30 30 31 20 20 2f 2a 20 54 68 65 20 73 63 68 65  001  /* The sche
59c90 6d 61 20 68 61 73 20 62 65 65 6e 20 6c 6f 61 64  ma has been load
59ca0 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42  ed */.#define DB
59cb0 5f 55 6e 72 65 73 65 74 56 69 65 77 73 20 20 20  _UnresetViews   
59cc0 20 30 78 30 30 30 32 20 20 2f 2a 20 53 6f 6d 65   0x0002  /* Some
59cd0 20 76 69 65 77 73 20 68 61 76 65 20 64 65 66 69   views have defi
59ce0 6e 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  ned column names
59cf0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f 45   */.#define DB_E
59d00 6d 70 74 79 20 20 20 20 20 20 20 20 20 20 20 30  mpty           0
59d10 78 30 30 30 34 20 20 2f 2a 20 54 68 65 20 66 69  x0004  /* The fi
59d20 6c 65 20 69 73 20 65 6d 70 74 79 20 28 6c 65 6e  le is empty (len
59d30 67 74 68 20 30 20 62 79 74 65 73 29 20 2a 2f 0a  gth 0 bytes) */.
59d40 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65  ./*.** The numbe
59d50 72 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 6b  r of different k
59d60 69 6e 64 73 20 6f 66 20 74 68 69 6e 67 73 20 74  inds of things t
59d70 68 61 74 20 63 61 6e 20 62 65 20 6c 69 6d 69 74  hat can be limit
59d80 65 64 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20  ed.** using the 
59d90 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 20  sqlite3_limit() 
59da0 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 64  interface..*/.#d
59db0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 4c  efine SQLITE_N_L
59dc0 49 4d 49 54 20 28 53 51 4c 49 54 45 5f 4c 49 4d  IMIT (SQLITE_LIM
59dd0 49 54 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48  IT_TRIGGER_DEPTH
59de0 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b 61  +1)../*.** Looka
59df0 73 69 64 65 20 6d 61 6c 6c 6f 63 20 69 73 20 61  side malloc is a
59e00 20 73 65 74 20 6f 66 20 66 69 78 65 64 2d 73 69   set of fixed-si
59e10 7a 65 20 62 75 66 66 65 72 73 20 74 68 61 74 20  ze buffers that 
59e20 63 61 6e 20 62 65 20 75 73 65 64 0a 2a 2a 20 74  can be used.** t
59e30 6f 20 73 61 74 69 73 66 79 20 73 6d 61 6c 6c 20  o satisfy small 
59e40 74 72 61 6e 73 69 65 6e 74 20 6d 65 6d 6f 72 79  transient memory
59e50 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75   allocation requ
59e60 65 73 74 73 20 66 6f 72 20 6f 62 6a 65 63 74 73  ests for objects
59e70 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77  .** associated w
59e80 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72  ith a particular
59e90 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
59ea0 74 69 6f 6e 2e 20 20 54 68 65 20 75 73 65 20 6f  tion.  The use o
59eb0 66 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 6d  f.** lookaside m
59ec0 61 6c 6c 6f 63 20 70 72 6f 76 69 64 65 73 20 61  alloc provides a
59ed0 20 73 69 67 6e 69 66 69 63 61 6e 74 20 70 65 72   significant per
59ee0 66 6f 72 6d 61 6e 63 65 20 65 6e 68 61 6e 63 65  formance enhance
59ef0 6d 65 6e 74 0a 2a 2a 20 28 61 70 70 72 6f 78 20  ment.** (approx 
59f00 31 30 25 29 20 62 79 20 61 76 6f 69 64 69 6e 67  10%) by avoiding
59f10 20 6e 75 6d 65 72 6f 75 73 20 6d 61 6c 6c 6f 63   numerous malloc
59f20 2f 66 72 65 65 20 72 65 71 75 65 73 74 73 20 77  /free requests w
59f30 68 69 6c 65 20 70 61 72 73 69 6e 67 0a 2a 2a 20  hile parsing.** 
59f40 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  SQL statements..
59f50 2a 2a 0a 2a 2a 20 54 68 65 20 4c 6f 6f 6b 61 73  **.** The Lookas
59f60 69 64 65 20 73 74 72 75 63 74 75 72 65 20 68 6f  ide structure ho
59f70 6c 64 73 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  lds configuratio
59f80 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  n information ab
59f90 6f 75 74 20 74 68 65 0a 2a 2a 20 6c 6f 6f 6b 61  out the.** looka
59fa0 73 69 64 65 20 6d 61 6c 6c 6f 63 20 73 75 62 73  side malloc subs
59fb0 79 73 74 65 6d 2e 20 20 45 61 63 68 20 61 76 61  ystem.  Each ava
59fc0 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 20 61 6c  ilable memory al
59fd0 6c 6f 63 61 74 69 6f 6e 20 69 6e 0a 2a 2a 20 74  location in.** t
59fe0 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 73 75 62  he lookaside sub
59ff0 73 79 73 74 65 6d 20 69 73 20 73 74 6f 72 65 64  system is stored
5a000 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73   on a linked lis
5a010 74 20 6f 66 20 4c 6f 6f 6b 61 73 69 64 65 53 6c  t of LookasideSl
5a020 6f 74 0a 2a 2a 20 6f 62 6a 65 63 74 73 2e 0a 2a  ot.** objects..*
5a030 2a 0a 2a 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 61  *.** Lookaside a
5a040 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 6f  llocations are o
5a050 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20  nly allowed for 
5a060 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61 72 65  objects that are
5a070 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77   associated.** w
5a080 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72  ith a particular
5a090 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
5a0a0 74 69 6f 6e 2e 20 20 48 65 6e 63 65 2c 20 73 63  tion.  Hence, sc
5a0b0 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  hema information
5a0c0 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20 73 74   cannot.** be st
5a0d0 6f 72 65 64 20 69 6e 20 6c 6f 6f 6b 61 73 69 64  ored in lookasid
5a0e0 65 20 62 65 63 61 75 73 65 20 69 6e 20 73 68 61  e because in sha
5a0f0 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 20 74  red cache mode t
5a100 68 65 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d  he schema inform
5a110 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 68 61 72  ation.** is shar
5a120 65 64 20 62 79 20 6d 75 6c 74 69 70 6c 65 20 64  ed by multiple d
5a130 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
5a140 6f 6e 73 2e 20 20 54 68 65 72 65 66 6f 72 65 2c  ons.  Therefore,
5a150 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67 0a 2a   while parsing.*
5a160 2a 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61  * schema informa
5a170 74 69 6f 6e 2c 20 74 68 65 20 4c 6f 6f 6b 61 73  tion, the Lookas
5a180 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 66 6c 61  ide.bEnabled fla
5a190 67 20 69 73 20 63 6c 65 61 72 65 64 20 73 6f 20  g is cleared so 
5a1a0 74 68 61 74 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64  that.** lookasid
5a1b0 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72  e allocations ar
5a1c0 65 20 6e 6f 74 20 75 73 65 64 20 74 6f 20 63 6f  e not used to co
5a1d0 6e 73 74 72 75 63 74 20 74 68 65 20 73 63 68 65  nstruct the sche
5a1e0 6d 61 20 6f 62 6a 65 63 74 73 2e 0a 2a 2f 0a 73  ma objects..*/.s
5a1f0 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 20  truct Lookaside 
5a200 7b 0a 20 20 75 31 36 20 73 7a 3b 20 20 20 20 20  {.  u16 sz;     
5a210 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
5a220 69 7a 65 20 6f 66 20 65 61 63 68 20 62 75 66 66  ize of each buff
5a230 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  er in bytes */. 
5a240 20 75 38 20 62 45 6e 61 62 6c 65 64 3b 20 20 20   u8 bEnabled;   
5a250 20 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c 73           /* Fals
5a260 65 20 74 6f 20 64 69 73 61 62 6c 65 20 6e 65 77  e to disable new
5a270 20 6c 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f 63   lookaside alloc
5a280 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 75 38 20 62  ations */.  u8 b
5a290 4d 61 6c 6c 6f 63 65 64 3b 20 20 20 20 20 20 20  Malloced;       
5a2a0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70      /* True if p
5a2b0 53 74 61 72 74 20 6f 62 74 61 69 6e 65 64 20 66  Start obtained f
5a2c0 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  rom sqlite3_mall
5a2d0 6f 63 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f  oc() */.  int nO
5a2e0 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ut;             
5a2f0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
5a300 75 66 66 65 72 73 20 63 75 72 72 65 6e 74 6c 79  uffers currently
5a310 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a   checked out */.
5a320 20 20 69 6e 74 20 6d 78 4f 75 74 3b 20 20 20 20    int mxOut;    
5a330 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 69 67            /* Hig
5a340 68 77 61 74 65 72 20 6d 61 72 6b 20 66 6f 72 20  hwater mark for 
5a350 6e 4f 75 74 20 2a 2f 0a 20 20 4c 6f 6f 6b 61 73  nOut */.  Lookas
5a360 69 64 65 53 6c 6f 74 20 2a 70 46 72 65 65 3b 20  ideSlot *pFree; 
5a370 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 76 61    /* List of ava
5a380 69 6c 61 62 6c 65 20 62 75 66 66 65 72 73 20 2a  ilable buffers *
5a390 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 61 72 74  /.  void *pStart
5a3a0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  ;           /* F
5a3b0 69 72 73 74 20 62 79 74 65 20 6f 66 20 61 76 61  irst byte of ava
5a3c0 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 20 73 70  ilable memory sp
5a3d0 61 63 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ace */.  void *p
5a3e0 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  End;            
5a3f0 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 70   /* First byte p
5a400 61 73 74 20 65 6e 64 20 6f 66 20 61 76 61 69 6c  ast end of avail
5a410 61 62 6c 65 20 73 70 61 63 65 20 2a 2f 0a 7d 3b  able space */.};
5a420 0a 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64  .struct Lookasid
5a430 65 53 6c 6f 74 20 7b 0a 20 20 4c 6f 6f 6b 61 73  eSlot {.  Lookas
5a440 69 64 65 53 6c 6f 74 20 2a 70 4e 65 78 74 3b 20  ideSlot *pNext; 
5a450 20 20 20 2f 2a 20 4e 65 78 74 20 62 75 66 66 65     /* Next buffe
5a460 72 20 69 6e 20 74 68 65 20 6c 69 73 74 20 6f 66  r in the list of
5a470 20 66 72 65 65 20 62 75 66 66 65 72 73 20 2a 2f   free buffers */
5a480 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 68 61 73  .};../*.** A has
5a490 68 20 74 61 62 6c 65 20 66 6f 72 20 66 75 6e 63  h table for func
5a4a0 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 73  tion definitions
5a4b0 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68 20 65 61 63  ..**.** Hash eac
5a4c0 68 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74  h FuncDef struct
5a4d0 75 72 65 20 69 6e 74 6f 20 6f 6e 65 20 6f 66 20  ure into one of 
5a4e0 74 68 65 20 46 75 6e 63 44 65 66 48 61 73 68 2e  the FuncDefHash.
5a4f0 61 5b 5d 20 73 6c 6f 74 73 2e 0a 2a 2a 20 43 6f  a[] slots..** Co
5a500 6c 6c 69 73 69 6f 6e 73 20 61 72 65 20 6f 6e 20  llisions are on 
5a510 74 68 65 20 46 75 6e 63 44 65 66 2e 70 48 61 73  the FuncDef.pHas
5a520 68 20 63 68 61 69 6e 2e 0a 2a 2f 0a 73 74 72 75  h chain..*/.stru
5a530 63 74 20 46 75 6e 63 44 65 66 48 61 73 68 20 7b  ct FuncDefHash {
5a540 0a 20 20 46 75 6e 63 44 65 66 20 2a 61 5b 32 33  .  FuncDef *a[23
5a550 5d 3b 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68  ];       /* Hash
5a560 20 74 61 62 6c 65 20 66 6f 72 20 66 75 6e 63 74   table for funct
5a570 69 6f 6e 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  ions */.};../*.*
5a580 2a 20 45 61 63 68 20 64 61 74 61 62 61 73 65 20  * Each database 
5a590 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
5a5a0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
5a5b0 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a  structure..**.**
5a5c0 20 54 68 65 20 73 71 6c 69 74 65 2e 6c 61 73 74   The sqlite.last
5a5d0 52 6f 77 69 64 20 72 65 63 6f 72 64 73 20 74 68  Rowid records th
5a5e0 65 20 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f  e last insert ro
5a5f0 77 69 64 20 67 65 6e 65 72 61 74 65 64 20 62 79  wid generated by
5a600 20 61 6e 0a 2a 2a 20 69 6e 73 65 72 74 20 73 74   an.** insert st
5a610 61 74 65 6d 65 6e 74 2e 20 20 49 6e 73 65 72 74  atement.  Insert
5a620 73 20 6f 6e 20 76 69 65 77 73 20 64 6f 20 6e 6f  s on views do no
5a630 74 20 61 66 66 65 63 74 20 69 74 73 20 76 61 6c  t affect its val
5a640 75 65 2e 20 20 45 61 63 68 0a 2a 2a 20 74 72 69  ue.  Each.** tri
5a650 67 67 65 72 20 68 61 73 20 69 74 73 20 6f 77 6e  gger has its own
5a660 20 63 6f 6e 74 65 78 74 2c 20 73 6f 20 74 68 61   context, so tha
5a670 74 20 6c 61 73 74 52 6f 77 69 64 20 63 61 6e 20  t lastRowid can 
5a680 62 65 20 75 70 64 61 74 65 64 20 69 6e 73 69 64  be updated insid
5a690 65 0a 2a 2a 20 74 72 69 67 67 65 72 73 20 61 73  e.** triggers as
5a6a0 20 75 73 75 61 6c 2e 20 20 54 68 65 20 70 72 65   usual.  The pre
5a6b0 76 69 6f 75 73 20 76 61 6c 75 65 20 77 69 6c 6c  vious value will
5a6c0 20 62 65 20 72 65 73 74 6f 72 65 64 20 6f 6e 63   be restored onc
5a6d0 65 20 74 68 65 20 74 72 69 67 67 65 72 0a 2a 2a  e the trigger.**
5a6e0 20 65 78 69 74 73 2e 20 20 55 70 6f 6e 20 65 6e   exits.  Upon en
5a6f0 74 65 72 69 6e 67 20 61 20 62 65 66 6f 72 65 20  tering a before 
5a700 6f 72 20 69 6e 73 74 65 61 64 20 6f 66 20 74 72  or instead of tr
5a710 69 67 67 65 72 2c 20 6c 61 73 74 52 6f 77 69 64  igger, lastRowid
5a720 20 69 73 20 6e 6f 0a 2a 2a 20 6c 6f 6e 67 65 72   is no.** longer
5a730 20 28 73 69 6e 63 65 20 61 66 74 65 72 20 76 65   (since after ve
5a740 72 73 69 6f 6e 20 32 2e 38 2e 31 32 29 20 72 65  rsion 2.8.12) re
5a750 73 65 74 20 74 6f 20 2d 31 2e 0a 2a 2a 0a 2a 2a  set to -1..**.**
5a760 20 54 68 65 20 73 71 6c 69 74 65 2e 6e 43 68 61   The sqlite.nCha
5a770 6e 67 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 75  nge does not cou
5a780 6e 74 20 63 68 61 6e 67 65 73 20 77 69 74 68 69  nt changes withi
5a790 6e 20 74 72 69 67 67 65 72 73 20 61 6e 64 20 6b  n triggers and k
5a7a0 65 65 70 73 20 6e 6f 0a 2a 2a 20 63 6f 6e 74 65  eeps no.** conte
5a7b0 78 74 2e 20 20 49 74 20 69 73 20 72 65 73 65 74  xt.  It is reset
5a7c0 20 61 74 20 73 74 61 72 74 20 6f 66 20 73 71 6c   at start of sql
5a7d0 69 74 65 33 5f 65 78 65 63 2e 0a 2a 2a 20 54 68  ite3_exec..** Th
5a7e0 65 20 73 71 6c 69 74 65 2e 6c 73 43 68 61 6e 67  e sqlite.lsChang
5a7f0 65 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 65  e represents the
5a800 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67   number of chang
5a810 65 73 20 6d 61 64 65 20 62 79 20 74 68 65 20 6c  es made by the l
5a820 61 73 74 0a 2a 2a 20 69 6e 73 65 72 74 2c 20 75  ast.** insert, u
5a830 70 64 61 74 65 2c 20 6f 72 20 64 65 6c 65 74 65  pdate, or delete
5a840 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 74 20   statement.  It 
5a850 72 65 6d 61 69 6e 73 20 63 6f 6e 73 74 61 6e 74  remains constant
5a860 20 74 68 72 6f 75 67 68 6f 75 74 20 74 68 65 0a   throughout the.
5a870 2a 2a 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 73  ** length of a s
5a880 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 69 73 20  tatement and is 
5a890 74 68 65 6e 20 75 70 64 61 74 65 64 20 62 79 20  then updated by 
5a8a0 4f 50 5f 53 65 74 43 6f 75 6e 74 73 2e 20 20 49  OP_SetCounts.  I
5a8b0 74 20 6b 65 65 70 73 20 61 0a 2a 2a 20 63 6f 6e  t keeps a.** con
5a8c0 74 65 78 74 20 73 74 61 63 6b 20 6a 75 73 74 20  text stack just 
5a8d0 6c 69 6b 65 20 6c 61 73 74 52 6f 77 69 64 20 73  like lastRowid s
5a8e0 6f 20 74 68 61 74 20 74 68 65 20 63 6f 75 6e 74  o that the count
5a8f0 20 6f 66 20 63 68 61 6e 67 65 73 0a 2a 2a 20 77   of changes.** w
5a900 69 74 68 69 6e 20 61 20 74 72 69 67 67 65 72 20  ithin a trigger 
5a910 69 73 20 6e 6f 74 20 73 65 65 6e 20 6f 75 74 73  is not seen outs
5a920 69 64 65 20 74 68 65 20 74 72 69 67 67 65 72 2e  ide the trigger.
5a930 20 20 43 68 61 6e 67 65 73 20 74 6f 20 76 69 65    Changes to vie
5a940 77 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 61 66 66  ws do not.** aff
5a950 65 63 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66  ect the value of
5a960 20 6c 73 43 68 61 6e 67 65 2e 0a 2a 2a 20 54 68   lsChange..** Th
5a970 65 20 73 71 6c 69 74 65 2e 63 73 43 68 61 6e 67  e sqlite.csChang
5a980 65 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66  e keeps track of
5a990 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
5a9a0 75 72 72 65 6e 74 20 63 68 61 6e 67 65 73 20 28  urrent changes (
5a9b0 73 69 6e 63 65 0a 2a 2a 20 74 68 65 20 6c 61 73  since.** the las
5a9c0 74 20 73 74 61 74 65 6d 65 6e 74 29 20 61 6e 64  t statement) and
5a9d0 20 69 73 20 75 73 65 64 20 74 6f 20 75 70 64 61   is used to upda
5a9e0 74 65 20 73 71 6c 69 74 65 5f 6c 73 43 68 61 6e  te sqlite_lsChan
5a9f0 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65  ge..**.** The me
5aa00 6d 62 65 72 20 76 61 72 69 61 62 6c 65 73 20 73  mber variables s
5aa10 71 6c 69 74 65 2e 65 72 72 43 6f 64 65 2c 20 73  qlite.errCode, s
5aa20 71 6c 69 74 65 2e 7a 45 72 72 4d 73 67 20 61 6e  qlite.zErrMsg an
5aa30 64 20 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73 67  d sqlite.zErrMsg
5aa40 31 36 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20  16.** store the 
5aa50 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f  most recent erro
5aa60 72 20 63 6f 64 65 20 61 6e 64 2c 20 69 66 20 61  r code and, if a
5aa70 70 70 6c 69 63 61 62 6c 65 2c 20 73 74 72 69 6e  pplicable, strin
5aa80 67 2e 20 54 68 65 0a 2a 2a 20 69 6e 74 65 72 6e  g. The.** intern
5aa90 61 6c 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69  al function sqli
5aaa0 74 65 33 45 72 72 6f 72 28 29 20 69 73 20 75 73  te3Error() is us
5aab0 65 64 20 74 6f 20 73 65 74 20 74 68 65 73 65 20  ed to set these 
5aac0 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 63 6f 6e  variables.** con
5aad0 73 69 73 74 65 6e 74 6c 79 2e 0a 2a 2f 0a 73 74  sistently..*/.st
5aae0 72 75 63 74 20 73 71 6c 69 74 65 33 20 7b 0a 20  ruct sqlite3 {. 
5aaf0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
5ab00 66 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  fs;            /
5ab10 2a 20 4f 53 20 49 6e 74 65 72 66 61 63 65 20 2a  * OS Interface *
5ab20 2f 0a 20 20 69 6e 74 20 6e 44 62 3b 20 20 20 20  /.  int nDb;    
5ab30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ab40 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
5ab50 61 63 6b 65 6e 64 73 20 63 75 72 72 65 6e 74 6c  ackends currentl
5ab60 79 20 69 6e 20 75 73 65 20 2a 2f 0a 20 20 44 62  y in use */.  Db
5ab70 20 2a 61 44 62 3b 20 20 20 20 20 20 20 20 20 20   *aDb;          
5ab80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
5ab90 6c 6c 20 62 61 63 6b 65 6e 64 73 20 2a 2f 0a 20  ll backends */. 
5aba0 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20   int flags;     
5abb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5abc0 2a 20 4d 69 73 63 65 6c 6c 61 6e 65 6f 75 73 20  * Miscellaneous 
5abd0 66 6c 61 67 73 2e 20 53 65 65 20 62 65 6c 6f 77  flags. See below
5abe0 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e 46 6c   */.  int openFl
5abf0 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
5ac00 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73      /* Flags pas
5ac10 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 76  sed to sqlite3_v
5ac20 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20  fs.xOpen() */.  
5ac30 69 6e 74 20 65 72 72 43 6f 64 65 3b 20 20 20 20  int errCode;    
5ac40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5ac50 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72   Most recent err
5ac60 6f 72 20 63 6f 64 65 20 28 53 51 4c 49 54 45 5f  or code (SQLITE_
5ac70 2a 29 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72 4d  *) */.  int errM
5ac80 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  ask;            
5ac90 20 20 20 20 20 20 2f 2a 20 26 20 72 65 73 75 6c        /* & resul
5aca0 74 20 63 6f 64 65 73 20 77 69 74 68 20 74 68 69  t codes with thi
5acb0 73 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  s before returni
5acc0 6e 67 20 2a 2f 0a 20 20 75 38 20 61 75 74 6f 43  ng */.  u8 autoC
5acd0 6f 6d 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20  ommit;          
5ace0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 75 74        /* The aut
5acf0 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 2e 20 2a  o-commit flag. *
5ad00 2f 0a 20 20 75 38 20 74 65 6d 70 5f 73 74 6f 72  /.  u8 temp_stor
5ad10 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
5ad20 20 20 2f 2a 20 31 3a 20 66 69 6c 65 20 32 3a 20    /* 1: file 2: 
5ad30 6d 65 6d 6f 72 79 20 30 3a 20 64 65 66 61 75 6c  memory 0: defaul
5ad40 74 20 2a 2f 0a 20 20 75 38 20 6d 61 6c 6c 6f 63  t */.  u8 malloc
5ad50 46 61 69 6c 65 64 3b 20 20 20 20 20 20 20 20 20  Failed;         
5ad60 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
5ad70 77 65 20 68 61 76 65 20 73 65 65 6e 20 61 20 6d  we have seen a m
5ad80 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 2a 2f  alloc failure */
5ad90 0a 20 20 75 38 20 64 66 6c 74 4c 6f 63 6b 4d 6f  .  u8 dfltLockMo
5ada0 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  de;             
5adb0 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f 63 6b   /* Default lock
5adc0 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 61 74 74  ing-mode for att
5add0 61 63 68 65 64 20 64 62 73 20 2a 2f 0a 20 20 75  ached dbs */.  u
5ade0 38 20 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64  8 dfltJournalMod
5adf0 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e;           /* 
5ae00 44 65 66 61 75 6c 74 20 6a 6f 75 72 6e 61 6c 20  Default journal 
5ae10 6d 6f 64 65 20 66 6f 72 20 61 74 74 61 63 68 65  mode for attache
5ae20 64 20 64 62 73 20 2a 2f 0a 20 20 73 69 67 6e 65  d dbs */.  signe
5ae30 64 20 63 68 61 72 20 6e 65 78 74 41 75 74 6f 76  d char nextAutov
5ae40 61 63 3b 20 20 20 20 20 20 2f 2a 20 41 75 74 6f  ac;      /* Auto
5ae50 76 61 63 20 73 65 74 74 69 6e 67 20 61 66 74 65  vac setting afte
5ae60 72 20 56 41 43 55 55 4d 20 69 66 20 3e 3d 30 20  r VACUUM if >=0 
5ae70 2a 2f 0a 20 20 69 6e 74 20 6e 65 78 74 50 61 67  */.  int nextPag
5ae80 65 73 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20  esize;          
5ae90 20 20 20 2f 2a 20 50 61 67 65 73 69 7a 65 20 61     /* Pagesize a
5aea0 66 74 65 72 20 56 41 43 55 55 4d 20 69 66 20 3e  fter VACUUM if >
5aeb0 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 61 62 6c  0 */.  int nTabl
5aec0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
5aed0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
5aee0 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  f tables in the 
5aef0 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 43 6f  database */.  Co
5af00 6c 6c 53 65 71 20 2a 70 44 66 6c 74 43 6f 6c 6c  llSeq *pDfltColl
5af10 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
5af20 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61  he default colla
5af30 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 28 42  ting sequence (B
5af40 49 4e 41 52 59 29 20 2a 2f 0a 20 20 69 36 34 20  INARY) */.  i64 
5af50 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20 20  lastRowid;      
5af60 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 4f 57            /* ROW
5af70 49 44 20 6f 66 20 6d 6f 73 74 20 72 65 63 65 6e  ID of most recen
5af80 74 20 69 6e 73 65 72 74 20 28 73 65 65 20 61 62  t insert (see ab
5af90 6f 76 65 29 20 2a 2f 0a 20 20 75 33 32 20 6d 61  ove) */.  u32 ma
5afa0 67 69 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  gic;            
5afb0 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 67 69 63          /* Magic
5afc0 20 6e 75 6d 62 65 72 20 66 6f 72 20 64 65 74 65   number for dete
5afd0 63 74 20 6c 69 62 72 61 72 79 20 6d 69 73 75 73  ct library misus
5afe0 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e  e */.  int nChan
5aff0 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
5b000 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65       /* Value re
5b010 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65  turned by sqlite
5b020 33 5f 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a 20  3_changes() */. 
5b030 20 69 6e 74 20 6e 54 6f 74 61 6c 43 68 61 6e 67   int nTotalChang
5b040 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e;             /
5b050 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64  * Value returned
5b060 20 62 79 20 73 71 6c 69 74 65 33 5f 74 6f 74 61   by sqlite3_tota
5b070 6c 5f 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a 20  l_changes() */. 
5b080 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
5b090 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 2f  mutex;         /
5b0a0 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 74  * Connection mut
5b0b0 65 78 20 2a 2f 0a 20 20 69 6e 74 20 61 4c 69 6d  ex */.  int aLim
5b0c0 69 74 5b 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d 49  it[SQLITE_N_LIMI
5b0d0 54 5d 3b 20 20 20 2f 2a 20 4c 69 6d 69 74 73 20  T];   /* Limits 
5b0e0 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69  */.  struct sqli
5b0f0 74 65 33 49 6e 69 74 49 6e 66 6f 20 7b 20 20 20  te3InitInfo {   
5b100 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f     /* Informatio
5b110 6e 20 75 73 65 64 20 64 75 72 69 6e 67 20 69 6e  n used during in
5b120 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0a  itialization */.
5b130 20 20 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20      int iDb;    
5b140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b150 2f 2a 20 57 68 65 6e 20 62 61 63 6b 20 69 73 20  /* When back is 
5b160 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65  being initialize
5b170 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 65 77  d */.    int new
5b180 54 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20  Tnum;           
5b190 20 20 20 20 20 2f 2a 20 52 6f 6f 74 70 61 67 65       /* Rootpage
5b1a0 20 6f 66 20 74 61 62 6c 65 20 62 65 69 6e 67 20   of table being 
5b1b0 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20  initialized */. 
5b1c0 20 20 20 75 38 20 62 75 73 79 3b 20 20 20 20 20     u8 busy;     
5b1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5b1e0 2a 20 54 52 55 45 20 69 66 20 63 75 72 72 65 6e  * TRUE if curren
5b1f0 74 6c 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67  tly initializing
5b200 20 2a 2f 0a 20 20 20 20 75 38 20 6f 72 70 68 61   */.    u8 orpha
5b210 6e 54 72 69 67 67 65 72 3b 20 20 20 20 20 20 20  nTrigger;       
5b220 20 20 20 20 2f 2a 20 4c 61 73 74 20 73 74 61 74      /* Last stat
5b230 65 6d 65 6e 74 20 69 73 20 6f 72 70 68 61 6e 65  ement is orphane
5b240 64 20 54 45 4d 50 20 74 72 69 67 67 65 72 20 2a  d TEMP trigger *
5b250 2f 0a 20 20 7d 20 69 6e 69 74 3b 0a 20 20 69 6e  /.  } init;.  in
5b260 74 20 6e 45 78 74 65 6e 73 69 6f 6e 3b 20 20 20  t nExtension;   
5b270 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
5b280 75 6d 62 65 72 20 6f 66 20 6c 6f 61 64 65 64 20  umber of loaded 
5b290 65 78 74 65 6e 73 69 6f 6e 73 20 2a 2f 0a 20 20  extensions */.  
5b2a0 76 6f 69 64 20 2a 2a 61 45 78 74 65 6e 73 69 6f  void **aExtensio
5b2b0 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  n;            /*
5b2c0 20 41 72 72 61 79 20 6f 66 20 73 68 61 72 65 64   Array of shared
5b2d0 20 6c 69 62 72 61 72 79 20 68 61 6e 64 6c 65 73   library handles
5b2e0 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 56 64 62   */.  struct Vdb
5b2f0 65 20 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20  e *pVdbe;       
5b300 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61      /* List of a
5b310 63 74 69 76 65 20 76 69 72 74 75 61 6c 20 6d 61  ctive virtual ma
5b320 63 68 69 6e 65 73 20 2a 2f 0a 20 20 69 6e 74 20  chines */.  int 
5b330 61 63 74 69 76 65 56 64 62 65 43 6e 74 3b 20 20  activeVdbeCnt;  
5b340 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
5b350 62 65 72 20 6f 66 20 56 44 42 45 73 20 63 75 72  ber of VDBEs cur
5b360 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67  rently executing
5b370 20 2a 2f 0a 20 20 69 6e 74 20 77 72 69 74 65 56   */.  int writeV
5b380 64 62 65 43 6e 74 3b 20 20 20 20 20 20 20 20 20  dbeCnt;         
5b390 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
5b3a0 20 61 63 74 69 76 65 20 56 44 42 45 73 20 74 68   active VDBEs th
5b3b0 61 74 20 61 72 65 20 77 72 69 74 69 6e 67 20 2a  at are writing *
5b3c0 2f 0a 20 20 76 6f 69 64 20 28 2a 78 54 72 61 63  /.  void (*xTrac
5b3d0 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63  e)(void*,const c
5b3e0 68 61 72 2a 29 3b 20 20 20 20 20 20 20 20 2f 2a  har*);        /*
5b3f0 20 54 72 61 63 65 20 66 75 6e 63 74 69 6f 6e 20   Trace function 
5b400 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 54 72 61 63  */.  void *pTrac
5b410 65 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20  eArg;           
5b420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5b430 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  * Argument to th
5b440 65 20 74 72 61 63 65 20 66 75 6e 63 74 69 6f 6e  e trace function
5b450 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 50 72   */.  void (*xPr
5b460 6f 66 69 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e  ofile)(void*,con
5b470 73 74 20 63 68 61 72 2a 2c 75 36 34 29 3b 20 20  st char*,u64);  
5b480 2f 2a 20 50 72 6f 66 69 6c 69 6e 67 20 66 75 6e  /* Profiling fun
5b490 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20  ction */.  void 
5b4a0 2a 70 50 72 6f 66 69 6c 65 41 72 67 3b 20 20 20  *pProfileArg;   
5b4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b4c0 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
5b4d0 20 74 6f 20 70 72 6f 66 69 6c 65 20 66 75 6e 63   to profile func
5b4e0 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  tion */.  void *
5b4f0 70 43 6f 6d 6d 69 74 41 72 67 3b 20 20 20 20 20  pCommitArg;     
5b500 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
5b510 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 6f 6d 6d  rgument to xComm
5b520 69 74 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f 20  itCallback() */ 
5b530 20 20 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d    .  int (*xComm
5b540 69 74 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64  itCallback)(void
5b550 2a 29 3b 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65  *);    /* Invoke
5b560 64 20 61 74 20 65 76 65 72 79 20 63 6f 6d 6d 69  d at every commi
5b570 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 52  t. */.  void *pR
5b580 6f 6c 6c 62 61 63 6b 41 72 67 3b 20 20 20 20 20  ollbackArg;     
5b590 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67            /* Arg
5b5a0 75 6d 65 6e 74 20 74 6f 20 78 52 6f 6c 6c 62 61  ument to xRollba
5b5b0 63 6b 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f 20  ckCallback() */ 
5b5c0 20 20 0a 20 20 76 6f 69 64 20 28 2a 78 52 6f 6c    .  void (*xRol
5b5d0 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 29 28 76  lbackCallback)(v
5b5e0 6f 69 64 2a 29 3b 20 2f 2a 20 49 6e 76 6f 6b 65  oid*); /* Invoke
5b5f0 64 20 61 74 20 65 76 65 72 79 20 63 6f 6d 6d 69  d at every commi
5b600 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55  t. */.  void *pU
5b610 70 64 61 74 65 41 72 67 3b 0a 20 20 76 6f 69 64  pdateArg;.  void
5b620 20 28 2a 78 55 70 64 61 74 65 43 61 6c 6c 62 61   (*xUpdateCallba
5b630 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 20 63  ck)(void*,int, c
5b640 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
5b650 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 5f 69 6e   char*,sqlite_in
5b660 74 36 34 29 3b 0a 20 20 76 6f 69 64 28 2a 78 43  t64);.  void(*xC
5b670 6f 6c 6c 4e 65 65 64 65 64 29 28 76 6f 69 64 2a  ollNeeded)(void*
5b680 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54  ,sqlite3*,int eT
5b690 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 61  extRep,const cha
5b6a0 72 2a 29 3b 0a 20 20 76 6f 69 64 28 2a 78 43 6f  r*);.  void(*xCo
5b6b0 6c 6c 4e 65 65 64 65 64 31 36 29 28 76 6f 69 64  llNeeded16)(void
5b6c0 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65  *,sqlite3*,int e
5b6d0 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f  TextRep,const vo
5b6e0 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 70 43  id*);.  void *pC
5b6f0 6f 6c 6c 4e 65 65 64 65 64 41 72 67 3b 0a 20 20  ollNeededArg;.  
5b700 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
5b710 45 72 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Err;          /*
5b720 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72   Most recent err
5b730 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20 20  or message */.  
5b740 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20  char *zErrMsg;  
5b750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5b760 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72   Most recent err
5b770 6f 72 20 6d 65 73 73 61 67 65 20 28 55 54 46 2d  or message (UTF-
5b780 38 20 65 6e 63 6f 64 65 64 29 20 2a 2f 0a 20 20  8 encoded) */.  
5b790 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 31 36 3b  char *zErrMsg16;
5b7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5b7b0 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72   Most recent err
5b7c0 6f 72 20 6d 65 73 73 61 67 65 20 28 55 54 46 2d  or message (UTF-
5b7d0 31 36 20 65 6e 63 6f 64 65 64 29 20 2a 2f 0a 20  16 encoded) */. 
5b7e0 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 76 6f 6c   union {.    vol
5b7f0 61 74 69 6c 65 20 69 6e 74 20 69 73 49 6e 74 65  atile int isInte
5b800 72 72 75 70 74 65 64 3b 20 2f 2a 20 54 72 75 65  rrupted; /* True
5b810 20 69 66 20 73 71 6c 69 74 65 33 5f 69 6e 74 65   if sqlite3_inte
5b820 72 72 75 70 74 20 68 61 73 20 62 65 65 6e 20 63  rrupt has been c
5b830 61 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 64 6f 75  alled */.    dou
5b840 62 6c 65 20 6e 6f 74 55 73 65 64 31 3b 20 20 20  ble notUsed1;   
5b850 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63           /* Spac
5b860 65 72 20 2a 2f 0a 20 20 7d 20 75 31 3b 0a 20 20  er */.  } u1;.  
5b870 4c 6f 6f 6b 61 73 69 64 65 20 6c 6f 6f 6b 61 73  Lookaside lookas
5b880 69 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ide;          /*
5b890 20 4c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f   Lookaside mallo
5b8a0 63 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  c configuration 
5b8b0 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
5b8c0 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41  E_OMIT_AUTHORIZA
5b8d0 54 49 4f 4e 0a 20 20 69 6e 74 20 28 2a 78 41 75  TION.  int (*xAu
5b8e0 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  th)(void*,int,co
5b8f0 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
5b900 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
5b910 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  *,const char*);.
5b920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b940 2f 2a 20 41 63 63 65 73 73 20 61 75 74 68 6f 72  /* Access author
5b950 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e  ization function
5b960 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 74   */.  void *pAut
5b970 68 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20  hArg;           
5b980 20 20 20 20 2f 2a 20 31 73 74 20 61 72 67 75 6d      /* 1st argum
5b990 65 6e 74 20 74 6f 20 74 68 65 20 61 63 63 65 73  ent to the acces
5b9a0 73 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20  s auth function 
5b9b0 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65  */.#endif.#ifnde
5b9c0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52  f SQLITE_OMIT_PR
5b9d0 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a  OGRESS_CALLBACK.
5b9e0 20 20 69 6e 74 20 28 2a 78 50 72 6f 67 72 65 73    int (*xProgres
5b9f0 73 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 20  s)(void *);     
5ba00 2f 2a 20 54 68 65 20 70 72 6f 67 72 65 73 73 20  /* The progress 
5ba10 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 76 6f  callback */.  vo
5ba20 69 64 20 2a 70 50 72 6f 67 72 65 73 73 41 72 67  id *pProgressArg
5ba30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  ;           /* A
5ba40 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 70  rgument to the p
5ba50 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b  rogress callback
5ba60 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 72 6f 67 72   */.  int nProgr
5ba70 65 73 73 4f 70 73 3b 20 20 20 20 20 20 20 20 20  essOps;         
5ba80 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
5ba90 20 6f 70 63 6f 64 65 73 20 66 6f 72 20 70 72 6f   opcodes for pro
5baa0 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 2a  gress callback *
5bab0 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  /.#endif.#ifndef
5bac0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
5bad0 54 55 41 4c 54 41 42 4c 45 0a 20 20 48 61 73 68  TUALTABLE.  Hash
5bae0 20 61 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 20   aModule;       
5baf0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 6f 70            /* pop
5bb00 75 6c 61 74 65 64 20 62 79 20 73 71 6c 69 74 65  ulated by sqlite
5bb10 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28  3_create_module(
5bb20 29 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 56  ) */.  Table *pV
5bb30 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  Tab;            
5bb40 20 20 20 20 20 2f 2a 20 76 74 61 62 20 77 69 74       /* vtab wit
5bb50 68 20 61 63 74 69 76 65 20 43 6f 6e 6e 65 63 74  h active Connect
5bb60 2f 43 72 65 61 74 65 20 6d 65 74 68 6f 64 20 2a  /Create method *
5bb70 2f 0a 20 20 56 54 61 62 6c 65 20 2a 2a 61 56 54  /.  VTable **aVT
5bb80 72 61 6e 73 3b 20 20 20 20 20 20 20 20 20 20 20  rans;           
5bb90 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62    /* Virtual tab
5bba0 6c 65 73 20 77 69 74 68 20 6f 70 65 6e 20 74 72  les with open tr
5bbb0 61 6e 73 61 63 74 69 6f 6e 73 20 2a 2f 0a 20 20  ansactions */.  
5bbc0 69 6e 74 20 6e 56 54 72 61 6e 73 3b 20 20 20 20  int nVTrans;    
5bbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5bbe0 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20   Allocated size 
5bbf0 6f 66 20 61 56 54 72 61 6e 73 20 2a 2f 0a 20 20  of aVTrans */.  
5bc00 56 54 61 62 6c 65 20 2a 70 44 69 73 63 6f 6e 6e  VTable *pDisconn
5bc10 65 63 74 3b 20 20 20 20 2f 2a 20 44 69 73 63 6f  ect;    /* Disco
5bc20 6e 6e 65 63 74 20 74 68 65 73 65 20 69 6e 20 6e  nnect these in n
5bc30 65 78 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70  ext sqlite3_prep
5bc40 61 72 65 28 29 20 2a 2f 0a 23 65 6e 64 69 66 0a  are() */.#endif.
5bc50 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 61 46    FuncDefHash aF
5bc60 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  unc;            
5bc70 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 6f 66  /* Hash table of
5bc80 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 75 6e 63   connection func
5bc90 74 69 6f 6e 73 20 2a 2f 0a 20 20 48 61 73 68 20  tions */.  Hash 
5bca0 61 43 6f 6c 6c 53 65 71 3b 20 20 20 20 20 20 20  aCollSeq;       
5bcb0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20           /* All 
5bcc0 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
5bcd0 63 65 73 20 2a 2f 0a 20 20 42 75 73 79 48 61 6e  ces */.  BusyHan
5bce0 64 6c 65 72 20 62 75 73 79 48 61 6e 64 6c 65 72  dler busyHandler
5bcf0 3b 20 20 20 20 20 20 2f 2a 20 42 75 73 79 20 63  ;      /* Busy c
5bd00 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74  allback */.  int
5bd10 20 62 75 73 79 54 69 6d 65 6f 75 74 3b 20 20 20   busyTimeout;   
5bd20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 75             /* Bu
5bd30 73 79 20 68 61 6e 64 6c 65 72 20 74 69 6d 65 6f  sy handler timeo
5bd40 75 74 2c 20 69 6e 20 6d 73 65 63 20 2a 2f 0a 20  ut, in msec */. 
5bd50 20 44 62 20 61 44 62 53 74 61 74 69 63 5b 32 5d   Db aDbStatic[2]
5bd60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
5bd70 2a 20 53 74 61 74 69 63 20 73 70 61 63 65 20 66  * Static space f
5bd80 6f 72 20 74 68 65 20 32 20 64 65 66 61 75 6c 74  or the 2 default
5bd90 20 62 61 63 6b 65 6e 64 73 20 2a 2f 0a 20 20 53   backends */.  S
5bda0 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70  avepoint *pSavep
5bdb0 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20  oint;        /* 
5bdc0 4c 69 73 74 20 6f 66 20 61 63 74 69 76 65 20 73  List of active s
5bdd0 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69  avepoints */.  i
5bde0 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20  nt nSavepoint;  
5bdf0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5be00 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 74 72  Number of non-tr
5be10 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f  ansaction savepo
5be20 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53  ints */.  int nS
5be30 74 61 74 65 6d 65 6e 74 3b 20 20 20 20 20 20 20  tatement;       
5be40 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
5be50 72 20 6f 66 20 6e 65 73 74 65 64 20 73 74 61 74  r of nested stat
5be60 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f  ement-transactio
5be70 6e 73 20 20 2a 2f 0a 20 20 75 38 20 69 73 54 72  ns  */.  u8 isTr
5be80 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69  ansactionSavepoi
5be90 6e 74 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69  nt;    /* True i
5bea0 66 20 74 68 65 20 6f 75 74 65 72 6d 6f 73 74 20  f the outermost 
5beb0 73 61 76 65 70 6f 69 6e 74 20 69 73 20 61 20 54  savepoint is a T
5bec0 53 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c  S */..#ifdef SQL
5bed0 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43  ITE_ENABLE_UNLOC
5bee0 4b 5f 4e 4f 54 49 46 59 0a 20 20 2f 2a 20 54 68  K_NOTIFY.  /* Th
5bef0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69  e following vari
5bf00 61 62 6c 65 73 20 61 72 65 20 61 6c 6c 20 70 72  ables are all pr
5bf10 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 20 53  otected by the S
5bf20 54 41 54 49 43 5f 4d 41 53 54 45 52 20 0a 20 20  TATIC_MASTER .  
5bf30 2a 2a 20 6d 75 74 65 78 2c 20 6e 6f 74 20 62 79  ** mutex, not by
5bf40 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e 20   sqlite3.mutex. 
5bf50 54 68 65 79 20 61 72 65 20 75 73 65 64 20 62 79  They are used by
5bf60 20 63 6f 64 65 20 69 6e 20 6e 6f 74 69 66 79 2e   code in notify.
5bf70 63 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68  c. .  **.  ** Wh
5bf80 65 6e 20 58 2e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e  en X.pUnlockConn
5bf90 65 63 74 69 6f 6e 3d 3d 59 2c 20 74 68 61 74 20  ection==Y, that 
5bfa0 6d 65 61 6e 73 20 74 68 61 74 20 58 20 69 73 20  means that X is 
5bfb0 77 61 69 74 69 6e 67 20 66 6f 72 20 59 20 74 6f  waiting for Y to
5bfc0 0a 20 20 2a 2a 20 75 6e 6c 6f 63 6b 20 73 6f 20  .  ** unlock so 
5bfd0 74 68 61 74 20 69 74 20 63 61 6e 20 70 72 6f 63  that it can proc
5bfe0 65 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57  eed..  **.  ** W
5bff0 68 65 6e 20 58 2e 70 42 6c 6f 63 6b 69 6e 67 43  hen X.pBlockingC
5c000 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 59 2c 20 74 68  onnection==Y, th
5c010 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 73 6f  at means that so
5c020 6d 65 74 68 69 6e 67 20 74 68 61 74 20 58 20 74  mething that X t
5c030 72 69 65 64 0a 20 20 2a 2a 20 74 72 69 65 64 20  ried.  ** tried 
5c040 74 6f 20 64 6f 20 72 65 63 65 6e 74 6c 79 20 66  to do recently f
5c050 61 69 6c 65 64 20 77 69 74 68 20 61 6e 20 53 51  ailed with an SQ
5c060 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 65 72 72 6f  LITE_LOCKED erro
5c070 72 20 64 75 65 20 74 6f 20 6c 6f 63 6b 73 0a 20  r due to locks. 
5c080 20 2a 2a 20 68 65 6c 64 20 62 79 20 59 2e 0a 20   ** held by Y.. 
5c090 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70   */.  sqlite3 *p
5c0a0 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69  BlockingConnecti
5c0b0 6f 6e 3b 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f  on; /* Connectio
5c0c0 6e 20 74 68 61 74 20 63 61 75 73 65 64 20 53 51  n that caused SQ
5c0d0 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 2a 2f 0a 20  LITE_LOCKED */. 
5c0e0 20 73 71 6c 69 74 65 33 20 2a 70 55 6e 6c 6f 63   sqlite3 *pUnloc
5c0f0 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3b 20 20 20 20  kConnection;    
5c100 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63         /* Connec
5c110 74 69 6f 6e 20 74 6f 20 77 61 74 63 68 20 66 6f  tion to watch fo
5c120 72 20 75 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 76 6f  r unlock */.  vo
5c130 69 64 20 2a 70 55 6e 6c 6f 63 6b 41 72 67 3b 20  id *pUnlockArg; 
5c140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c150 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20      /* Argument 
5c160 74 6f 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79  to xUnlockNotify
5c170 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 55 6e   */.  void (*xUn
5c180 6c 6f 63 6b 4e 6f 74 69 66 79 29 28 76 6f 69 64  lockNotify)(void
5c190 20 2a 2a 2c 20 69 6e 74 29 3b 20 20 2f 2a 20 55   **, int);  /* U
5c1a0 6e 6c 6f 63 6b 20 6e 6f 74 69 66 79 20 63 61 6c  nlock notify cal
5c1b0 6c 62 61 63 6b 20 2a 2f 0a 20 20 73 71 6c 69 74  lback */.  sqlit
5c1c0 65 33 20 2a 70 4e 65 78 74 42 6c 6f 63 6b 65 64  e3 *pNextBlocked
5c1d0 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74  ;        /* Next
5c1e0 20 69 6e 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20   in list of all 
5c1f0 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69  blocked connecti
5c200 6f 6e 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b  ons */.#endif.};
5c210 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61 63 72 6f 20  ../*.** A macro 
5c220 74 6f 20 64 69 73 63 6f 76 65 72 20 74 68 65 20  to discover the 
5c230 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 64 61  encoding of a da
5c240 74 61 62 61 73 65 2e 0a 2a 2f 0a 23 64 65 66 69  tabase..*/.#defi
5c250 6e 65 20 45 4e 43 28 64 62 29 20 28 28 64 62 29  ne ENC(db) ((db)
5c260 2d 3e 61 44 62 5b 30 5d 2e 70 53 63 68 65 6d 61  ->aDb[0].pSchema
5c270 2d 3e 65 6e 63 29 0a 0a 2f 2a 0a 2a 2a 20 50 6f  ->enc)../*.** Po
5c280 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f  ssible values fo
5c290 72 20 74 68 65 20 73 71 6c 69 74 65 2e 66 6c 61  r the sqlite.fla
5c2a0 67 73 20 61 6e 64 20 6f 72 20 44 62 2e 66 6c 61  gs and or Db.fla
5c2b0 67 73 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a 2a 2a  gs fields..**.**
5c2c0 20 4f 6e 20 73 71 6c 69 74 65 2e 66 6c 61 67 73   On sqlite.flags
5c2d0 2c 20 74 68 65 20 53 51 4c 49 54 45 5f 49 6e 54  , the SQLITE_InT
5c2e0 72 61 6e 73 20 76 61 6c 75 65 20 6d 65 61 6e 73  rans value means
5c2f0 20 74 68 61 74 20 77 65 20 68 61 76 65 0a 2a 2a   that we have.**
5c300 20 65 78 65 63 75 74 65 64 20 61 20 42 45 47 49   executed a BEGI
5c310 4e 2e 20 20 4f 6e 20 44 62 2e 66 6c 61 67 73 2c  N.  On Db.flags,
5c320 20 53 51 4c 49 54 45 5f 49 6e 54 72 61 6e 73 20   SQLITE_InTrans 
5c330 6d 65 61 6e 73 20 61 20 73 74 61 74 65 6d 65 6e  means a statemen
5c340 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e  t.** transaction
5c350 20 69 73 20 61 63 74 69 76 65 20 6f 6e 20 74 68   is active on th
5c360 61 74 20 70 61 72 74 69 63 75 6c 61 72 20 64 61  at particular da
5c370 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a  tabase file..*/.
5c380 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56  #define SQLITE_V
5c390 64 62 65 54 72 61 63 65 20 20 20 20 20 20 30 78  dbeTrace      0x
5c3a0 30 30 30 30 30 30 30 31 20 20 2f 2a 20 54 72 75  00000001  /* Tru
5c3b0 65 20 74 6f 20 74 72 61 63 65 20 56 44 42 45 20  e to trace VDBE 
5c3c0 65 78 65 63 75 74 69 6f 6e 20 2a 2f 0a 23 64 65  execution */.#de
5c3d0 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 6e 54 72  fine SQLITE_InTr
5c3e0 61 6e 73 20 20 20 20 20 20 20 20 30 78 30 30 30  ans        0x000
5c3f0 30 30 30 30 38 20 20 2f 2a 20 54 72 75 65 20 69  00008  /* True i
5c400 66 20 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69  f in a transacti
5c410 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  on */.#define SQ
5c420 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67  LITE_InternChang
5c430 65 73 20 20 30 78 30 30 30 30 30 30 31 30 20 20  es  0x00000010  
5c440 2f 2a 20 55 6e 63 6f 6d 6d 69 74 74 65 64 20 48  /* Uncommitted H
5c450 61 73 68 20 74 61 62 6c 65 20 63 68 61 6e 67 65  ash table change
5c460 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  s */.#define SQL
5c470 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73  ITE_FullColNames
5c480 20 20 20 30 78 30 30 30 30 30 30 32 30 20 20 2f     0x00000020  /
5c490 2a 20 53 68 6f 77 20 66 75 6c 6c 20 63 6f 6c 75  * Show full colu
5c4a0 6d 6e 20 6e 61 6d 65 73 20 6f 6e 20 53 45 4c 45  mn names on SELE
5c4b0 43 54 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  CT */.#define SQ
5c4c0 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d  LITE_ShortColNam
5c4d0 65 73 20 20 30 78 30 30 30 30 30 30 34 30 20 20  es  0x00000040  
5c4e0 2f 2a 20 53 68 6f 77 20 73 68 6f 72 74 20 63 6f  /* Show short co
5c4f0 6c 75 6d 6e 73 20 6e 61 6d 65 73 20 2a 2f 0a 23  lumns names */.#
5c500 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 6f  define SQLITE_Co
5c510 75 6e 74 52 6f 77 73 20 20 20 20 20 20 30 78 30  untRows      0x0
5c520 30 30 30 30 30 38 30 20 20 2f 2a 20 43 6f 75 6e  0000080  /* Coun
5c530 74 20 72 6f 77 73 20 63 68 61 6e 67 65 64 20 62  t rows changed b
5c540 79 20 49 4e 53 45 52 54 2c 20 2a 2f 0a 20 20 20  y INSERT, */.   
5c550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c570 20 20 20 20 20 20 20 2f 2a 20 20 20 44 45 4c 45         /*   DELE
5c580 54 45 2c 20 6f 72 20 55 50 44 41 54 45 20 61 6e  TE, or UPDATE an
5c590 64 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 20 20  d return */.    
5c5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c5c0 20 20 20 20 20 20 2f 2a 20 20 20 74 68 65 20 63        /*   the c
5c5d0 6f 75 6e 74 20 75 73 69 6e 67 20 61 20 63 61 6c  ount using a cal
5c5e0 6c 62 61 63 6b 2e 20 2a 2f 0a 23 64 65 66 69 6e  lback. */.#defin
5c5f0 65 20 53 51 4c 49 54 45 5f 4e 75 6c 6c 43 61 6c  e SQLITE_NullCal
5c600 6c 62 61 63 6b 20 20 20 30 78 30 30 30 30 30 31  lback   0x000001
5c610 30 30 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68  00  /* Invoke th
5c620 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 65 20  e callback once 
5c630 69 66 20 74 68 65 20 2a 2f 0a 20 20 20 20 20 20  if the */.      
5c640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c660 20 20 20 20 2f 2a 20 20 20 72 65 73 75 6c 74 20      /*   result 
5c670 73 65 74 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a  set is empty */.
5c680 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
5c690 71 6c 54 72 61 63 65 20 20 20 20 20 20 20 30 78  qlTrace       0x
5c6a0 30 30 30 30 30 32 30 30 20 20 2f 2a 20 44 65 62  00000200  /* Deb
5c6b0 75 67 20 70 72 69 6e 74 20 53 51 4c 20 61 73 20  ug print SQL as 
5c6c0 69 74 20 65 78 65 63 75 74 65 73 20 2a 2f 0a 23  it executes */.#
5c6d0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 64  define SQLITE_Vd
5c6e0 62 65 4c 69 73 74 69 6e 67 20 20 20 20 30 78 30  beListing    0x0
5c6f0 30 30 30 30 34 30 30 20 20 2f 2a 20 44 65 62 75  0000400  /* Debu
5c700 67 20 6c 69 73 74 69 6e 67 73 20 6f 66 20 56 44  g listings of VD
5c710 42 45 20 70 72 6f 67 72 61 6d 73 20 2a 2f 0a 23  BE programs */.#
5c720 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 72  define SQLITE_Wr
5c730 69 74 65 53 63 68 65 6d 61 20 20 20 20 30 78 30  iteSchema    0x0
5c740 30 30 30 30 38 30 30 20 20 2f 2a 20 4f 4b 20 74  0000800  /* OK t
5c750 6f 20 75 70 64 61 74 65 20 53 51 4c 49 54 45 5f  o update SQLITE_
5c760 4d 41 53 54 45 52 20 2a 2f 0a 23 64 65 66 69 6e  MASTER */.#defin
5c770 65 20 53 51 4c 49 54 45 5f 4e 6f 52 65 61 64 6c  e SQLITE_NoReadl
5c780 6f 63 6b 20 20 20 20 20 30 78 30 30 30 30 31 30  ock     0x000010
5c790 30 30 20 20 2f 2a 20 52 65 61 64 6c 6f 63 6b 73  00  /* Readlocks
5c7a0 20 61 72 65 20 6f 6d 69 74 74 65 64 20 77 68 65   are omitted whe
5c7b0 6e 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n .             
5c7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
5c7e0 61 63 63 65 73 73 69 6e 67 20 72 65 61 64 2d 6f  accessing read-o
5c7f0 6e 6c 79 20 64 61 74 61 62 61 73 65 73 20 2a 2f  nly databases */
5c800 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5c810 49 67 6e 6f 72 65 43 68 65 63 6b 73 20 20 20 30  IgnoreChecks   0
5c820 78 30 30 30 30 32 30 30 30 20 20 2f 2a 20 44 6f  x00002000  /* Do
5c830 20 6e 6f 74 20 65 6e 66 6f 72 63 65 20 63 68 65   not enforce che
5c840 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a  ck constraints *
5c850 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
5c860 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64  _ReadUncommitted
5c870 20 30 78 30 30 30 30 34 30 30 30 20 2f 2a 20 46   0x00004000 /* F
5c880 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68 65 20  or shared-cache 
5c890 6d 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  mode */.#define 
5c8a0 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c  SQLITE_LegacyFil
5c8b0 65 46 6d 74 20 20 30 78 30 30 30 30 38 30 30 30  eFmt  0x00008000
5c8c0 20 20 2f 2a 20 43 72 65 61 74 65 20 6e 65 77 20    /* Create new 
5c8d0 64 61 74 61 62 61 73 65 73 20 69 6e 20 66 6f 72  databases in for
5c8e0 6d 61 74 20 31 20 2a 2f 0a 23 64 65 66 69 6e 65  mat 1 */.#define
5c8f0 20 53 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e   SQLITE_FullFSyn
5c900 63 20 20 20 20 20 20 30 78 30 30 30 31 30 30 30  c      0x0001000
5c910 30 20 20 2f 2a 20 55 73 65 20 66 75 6c 6c 20 66  0  /* Use full f
5c920 73 79 6e 63 20 6f 6e 20 74 68 65 20 62 61 63 6b  sync on the back
5c930 65 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  end */.#define S
5c940 51 4c 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73  QLITE_LoadExtens
5c950 69 6f 6e 20 20 30 78 30 30 30 32 30 30 30 30 20  ion  0x00020000 
5c960 20 2f 2a 20 45 6e 61 62 6c 65 20 6c 6f 61 64 5f   /* Enable load_
5c970 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 0a 23 64  extension */..#d
5c980 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 65 63  efine SQLITE_Rec
5c990 6f 76 65 72 79 4d 6f 64 65 20 20 20 30 78 30 30  overyMode   0x00
5c9a0 30 34 30 30 30 30 20 20 2f 2a 20 49 67 6e 6f 72  040000  /* Ignor
5c9b0 65 20 73 63 68 65 6d 61 20 65 72 72 6f 72 73 20  e schema errors 
5c9c0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
5c9d0 45 5f 52 65 76 65 72 73 65 4f 72 64 65 72 20 20  E_ReverseOrder  
5c9e0 20 30 78 30 30 31 30 30 30 30 30 20 20 2f 2a 20   0x00100000  /* 
5c9f0 52 65 76 65 72 73 65 20 75 6e 6f 72 64 65 72 65  Reverse unordere
5ca00 64 20 53 45 4c 45 43 54 73 20 2a 2f 0a 23 64 65  d SELECTs */.#de
5ca10 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 65 63 54  fine SQLITE_RecT
5ca20 72 69 67 67 65 72 73 20 20 20 20 30 78 30 30 32  riggers    0x002
5ca30 30 30 30 30 30 20 20 2f 2a 20 45 6e 61 62 6c 65  00000  /* Enable
5ca40 20 72 65 63 75 72 73 69 76 65 20 74 72 69 67 67   recursive trigg
5ca50 65 72 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 6f  ers */../*.** Po
5ca60 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f  ssible values fo
5ca70 72 20 74 68 65 20 73 71 6c 69 74 65 2e 6d 61 67  r the sqlite.mag
5ca80 69 63 20 66 69 65 6c 64 2e 0a 2a 2a 20 54 68 65  ic field..** The
5ca90 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6f 62 74   numbers are obt
5caa0 61 69 6e 65 64 20 61 74 20 72 61 6e 64 6f 6d 20  ained at random 
5cab0 61 6e 64 20 68 61 76 65 20 6e 6f 20 73 70 65 63  and have no spec
5cac0 69 61 6c 20 6d 65 61 6e 69 6e 67 2c 20 6f 74 68  ial meaning, oth
5cad0 65 72 0a 2a 2a 20 74 68 61 6e 20 62 65 69 6e 67  er.** than being
5cae0 20 64 69 73 74 69 6e 63 74 20 66 72 6f 6d 20 6f   distinct from o
5caf0 6e 65 20 61 6e 6f 74 68 65 72 2e 0a 2a 2f 0a 23  ne another..*/.#
5cb00 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
5cb10 47 49 43 5f 4f 50 45 4e 20 20 20 20 20 30 78 61  GIC_OPEN     0xa
5cb20 30 32 39 61 36 39 37 20 20 2f 2a 20 44 61 74 61  029a697  /* Data
5cb30 62 61 73 65 20 69 73 20 6f 70 65 6e 20 2a 2f 0a  base is open */.
5cb40 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d  #define SQLITE_M
5cb50 41 47 49 43 5f 43 4c 4f 53 45 44 20 20 20 30 78  AGIC_CLOSED   0x
5cb60 39 66 33 63 32 64 33 33 20 20 2f 2a 20 44 61 74  9f3c2d33  /* Dat
5cb70 61 62 61 73 65 20 69 73 20 63 6c 6f 73 65 64 20  abase is closed 
5cb80 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
5cb90 45 5f 4d 41 47 49 43 5f 53 49 43 4b 20 20 20 20  E_MAGIC_SICK    
5cba0 20 30 78 34 62 37 37 31 32 39 30 20 20 2f 2a 20   0x4b771290  /* 
5cbb0 45 72 72 6f 72 20 61 6e 64 20 61 77 61 69 74 69  Error and awaiti
5cbc0 6e 67 20 63 6c 6f 73 65 20 2a 2f 0a 23 64 65 66  ng close */.#def
5cbd0 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  ine SQLITE_MAGIC
5cbe0 5f 42 55 53 59 20 20 20 20 20 30 78 66 30 33 62  _BUSY     0xf03b
5cbf0 37 39 30 36 20 20 2f 2a 20 44 61 74 61 62 61 73  7906  /* Databas
5cc00 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75  e currently in u
5cc10 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  se */.#define SQ
5cc20 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52  LITE_MAGIC_ERROR
5cc30 20 20 20 20 30 78 62 35 33 35 37 39 33 30 20 20      0xb5357930  
5cc40 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f 4d 49 53  /* An SQLITE_MIS
5cc50 55 53 45 20 65 72 72 6f 72 20 6f 63 63 75 72 72  USE error occurr
5cc60 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63  ed */../*.** Eac
5cc70 68 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69  h SQL function i
5cc80 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e 20  s defined by an 
5cc90 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
5cca0 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72  following.** str
5ccb0 75 63 74 75 72 65 2e 20 20 41 20 70 6f 69 6e 74  ucture.  A point
5ccc0 65 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 63  er to this struc
5ccd0 74 75 72 65 20 69 73 20 73 74 6f 72 65 64 20 69  ture is stored i
5cce0 6e 20 74 68 65 20 73 71 6c 69 74 65 2e 61 46 75  n the sqlite.aFu
5ccf0 6e 63 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65  nc.** hash table
5cd00 2e 20 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c 65  .  When multiple
5cd10 20 66 75 6e 63 74 69 6f 6e 73 20 68 61 76 65 20   functions have 
5cd20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2c 20 74  the same name, t
5cd30 68 65 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2a  he hash table.**
5cd40 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 6c 69 6e   points to a lin
5cd50 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 68 65 73  ked list of thes
5cd60 65 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f  e structures..*/
5cd70 0a 73 74 72 75 63 74 20 46 75 6e 63 44 65 66 20  .struct FuncDef 
5cd80 7b 0a 20 20 69 31 36 20 6e 41 72 67 3b 20 20 20  {.  i16 nArg;   
5cd90 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
5cda0 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e  er of arguments.
5cdb0 20 20 2d 31 20 6d 65 61 6e 73 20 75 6e 6c 69 6d    -1 means unlim
5cdc0 69 74 65 64 20 2a 2f 0a 20 20 75 38 20 69 50 72  ited */.  u8 iPr
5cdd0 65 66 45 6e 63 3b 20 20 20 20 20 20 20 20 20 2f  efEnc;         /
5cde0 2a 20 50 72 65 66 65 72 72 65 64 20 74 65 78 74  * Preferred text
5cdf0 20 65 6e 63 6f 64 69 6e 67 20 28 53 51 4c 49 54   encoding (SQLIT
5ce00 45 5f 55 54 46 38 2c 20 31 36 4c 45 2c 20 31 36  E_UTF8, 16LE, 16
5ce10 42 45 29 20 2a 2f 0a 20 20 75 38 20 66 6c 61 67  BE) */.  u8 flag
5ce20 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  s;            /*
5ce30 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f   Some combinatio
5ce40 6e 20 6f 66 20 53 51 4c 49 54 45 5f 46 55 4e 43  n of SQLITE_FUNC
5ce50 5f 2a 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55  _* */.  void *pU
5ce60 73 65 72 44 61 74 61 3b 20 20 20 20 20 2f 2a 20  serData;     /* 
5ce70 55 73 65 72 20 64 61 74 61 20 70 61 72 61 6d 65  User data parame
5ce80 74 65 72 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66  ter */.  FuncDef
5ce90 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a   *pNext;      /*
5cea0 20 4e 65 78 74 20 66 75 6e 63 74 69 6f 6e 20 77   Next function w
5ceb0 69 74 68 20 73 61 6d 65 20 6e 61 6d 65 20 2a 2f  ith same name */
5cec0 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29  .  void (*xFunc)
5ced0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
5cee0 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
5cef0 6c 75 65 2a 2a 29 3b 20 2f 2a 20 52 65 67 75 6c  lue**); /* Regul
5cf00 61 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  ar function */. 
5cf10 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73   void (*xStep)(s
5cf20 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
5cf30 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
5cf40 65 2a 2a 29 3b 20 2f 2a 20 41 67 67 72 65 67 61  e**); /* Aggrega
5cf50 74 65 20 73 74 65 70 20 2a 2f 0a 20 20 76 6f 69  te step */.  voi
5cf60 64 20 28 2a 78 46 69 6e 61 6c 69 7a 65 29 28 73  d (*xFinalize)(s
5cf70 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
5cf80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5cf90 20 2f 2a 20 41 67 67 72 65 67 61 74 65 20 66 69   /* Aggregate fi
5cfa0 6e 61 6c 69 7a 65 72 20 2a 2f 0a 20 20 63 68 61  nalizer */.  cha
5cfb0 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20  r *zName;       
5cfc0 20 20 2f 2a 20 53 51 4c 20 6e 61 6d 65 20 6f 66    /* SQL name of
5cfd0 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 2a   the function. *
5cfe0 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 48 61  /.  FuncDef *pHa
5cff0 73 68 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 74  sh;      /* Next
5d000 20 77 69 74 68 20 61 20 64 69 66 66 65 72 65 6e   with a differen
5d010 74 20 6e 61 6d 65 20 62 75 74 20 74 68 65 20 73  t name but the s
5d020 61 6d 65 20 68 61 73 68 20 2a 2f 0a 7d 3b 0a 0a  ame hash */.};..
5d030 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76  /*.** Possible v
5d040 61 6c 75 65 73 20 66 6f 72 20 46 75 6e 63 44 65  alues for FuncDe
5d050 66 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69  f.flags.*/.#defi
5d060 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c  ne SQLITE_FUNC_L
5d070 49 4b 45 20 20 20 20 20 30 78 30 31 20 2f 2a 20  IKE     0x01 /* 
5d080 43 61 6e 64 69 64 61 74 65 20 66 6f 72 20 74 68  Candidate for th
5d090 65 20 4c 49 4b 45 20 6f 70 74 69 6d 69 7a 61 74  e LIKE optimizat
5d0a0 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ion */.#define S
5d0b0 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 20  QLITE_FUNC_CASE 
5d0c0 20 20 20 20 30 78 30 32 20 2f 2a 20 43 61 73 65      0x02 /* Case
5d0d0 2d 73 65 6e 73 69 74 69 76 65 20 4c 49 4b 45 2d  -sensitive LIKE-
5d0e0 74 79 70 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  type function */
5d0f0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5d100 46 55 4e 43 5f 45 50 48 45 4d 20 20 20 20 30 78  FUNC_EPHEM    0x
5d110 30 34 20 2f 2a 20 45 70 68 65 6d 65 72 61 6c 2e  04 /* Ephemeral.
5d120 20 20 44 65 6c 65 74 65 20 77 69 74 68 20 56 44    Delete with VD
5d130 42 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  BE */.#define SQ
5d140 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f  LITE_FUNC_NEEDCO
5d150 4c 4c 20 30 78 30 38 20 2f 2a 20 73 71 6c 69 74  LL 0x08 /* sqlit
5d160 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71  e3GetFuncCollSeq
5d170 28 29 20 6d 69 67 68 74 20 62 65 20 63 61 6c 6c  () might be call
5d180 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ed */.#define SQ
5d190 4c 49 54 45 5f 46 55 4e 43 5f 50 52 49 56 41 54  LITE_FUNC_PRIVAT
5d1a0 45 20 20 30 78 31 30 20 2f 2a 20 41 6c 6c 6f 77  E  0x10 /* Allow
5d1b0 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20  ed for internal 
5d1c0 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66  use only */.#def
5d1d0 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  ine SQLITE_FUNC_
5d1e0 43 4f 55 4e 54 20 20 20 20 30 78 32 30 20 2f 2a  COUNT    0x20 /*
5d1f0 20 42 75 69 6c 74 2d 69 6e 20 63 6f 75 6e 74 28   Built-in count(
5d200 2a 29 20 61 67 67 72 65 67 61 74 65 20 2a 2f 0a  *) aggregate */.
5d210 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
5d220 77 69 6e 67 20 74 68 72 65 65 20 6d 61 63 72 6f  wing three macro
5d230 73 2c 20 46 55 4e 43 54 49 4f 4e 28 29 2c 20 4c  s, FUNCTION(), L
5d240 49 4b 45 46 55 4e 43 28 29 20 61 6e 64 20 41 47  IKEFUNC() and AG
5d250 47 52 45 47 41 54 45 28 29 20 61 72 65 0a 2a 2a  GREGATE() are.**
5d260 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20   used to create 
5d270 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 65 72 73  the initializers
5d280 20 66 6f 72 20 74 68 65 20 46 75 6e 63 44 65 66   for the FuncDef
5d290 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2a 0a   structures..**.
5d2a0 2a 2a 20 20 20 46 55 4e 43 54 49 4f 4e 28 7a 4e  **   FUNCTION(zN
5d2b0 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c  ame, nArg, iArg,
5d2c0 20 62 4e 43 2c 20 78 46 75 6e 63 29 0a 2a 2a 20   bNC, xFunc).** 
5d2d0 20 20 20 20 55 73 65 64 20 74 6f 20 63 72 65 61      Used to crea
5d2e0 74 65 20 61 20 73 63 61 6c 61 72 20 66 75 6e 63  te a scalar func
5d2f0 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20  tion definition 
5d300 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 7a 4e  of a function zN
5d310 61 6d 65 20 0a 2a 2a 20 20 20 20 20 69 6d 70 6c  ame .**     impl
5d320 65 6d 65 6e 74 65 64 20 62 79 20 43 20 66 75 6e  emented by C fun
5d330 63 74 69 6f 6e 20 78 46 75 6e 63 20 74 68 61 74  ction xFunc that
5d340 20 61 63 63 65 70 74 73 20 6e 41 72 67 20 61 72   accepts nArg ar
5d350 67 75 6d 65 6e 74 73 2e 20 54 68 65 0a 2a 2a 20  guments. The.** 
5d360 20 20 20 20 76 61 6c 75 65 20 70 61 73 73 65 64      value passed
5d370 20 61 73 20 69 41 72 67 20 69 73 20 63 61 73 74   as iArg is cast
5d380 20 74 6f 20 61 20 28 76 6f 69 64 2a 29 20 61 6e   to a (void*) an
5d390 64 20 6d 61 64 65 20 61 76 61 69 6c 61 62 6c 65  d made available
5d3a0 0a 2a 2a 20 20 20 20 20 61 73 20 74 68 65 20 75  .**     as the u
5d3b0 73 65 72 2d 64 61 74 61 20 28 73 71 6c 69 74 65  ser-data (sqlite
5d3c0 33 5f 75 73 65 72 5f 64 61 74 61 28 29 29 20 66  3_user_data()) f
5d3d0 6f 72 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e  or the function.
5d3e0 20 49 66 20 0a 2a 2a 20 20 20 20 20 61 72 67 75   If .**     argu
5d3f0 6d 65 6e 74 20 62 4e 43 20 69 73 20 74 72 75 65  ment bNC is true
5d400 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c 49 54  , then the SQLIT
5d410 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20  E_FUNC_NEEDCOLL 
5d420 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 0a  flag is set..**.
5d430 2a 2a 20 20 20 41 47 47 52 45 47 41 54 45 28 7a  **   AGGREGATE(z
5d440 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72 67  Name, nArg, iArg
5d450 2c 20 62 4e 43 2c 20 78 53 74 65 70 2c 20 78 46  , bNC, xStep, xF
5d460 69 6e 61 6c 29 0a 2a 2a 20 20 20 20 20 55 73 65  inal).**     Use
5d470 64 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20 61  d to create an a
5d480 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
5d490 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 6d 70  n definition imp
5d4a0 6c 65 6d 65 6e 74 65 64 20 62 79 0a 2a 2a 20 20  lemented by.**  
5d4b0 20 20 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f     the C functio
5d4c0 6e 73 20 78 53 74 65 70 20 61 6e 64 20 78 46 69  ns xStep and xFi
5d4d0 6e 61 6c 2e 20 54 68 65 20 66 69 72 73 74 20 66  nal. The first f
5d4e0 6f 75 72 20 70 61 72 61 6d 65 74 65 72 73 0a 2a  our parameters.*
5d4f0 2a 20 20 20 20 20 61 72 65 20 69 6e 74 65 72 70  *     are interp
5d500 72 65 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d  reted in the sam
5d510 65 20 77 61 79 20 61 73 20 74 68 65 20 66 69 72  e way as the fir
5d520 73 74 20 34 20 70 61 72 61 6d 65 74 65 72 73 20  st 4 parameters 
5d530 74 6f 0a 2a 2a 20 20 20 20 20 46 55 4e 43 54 49  to.**     FUNCTI
5d540 4f 4e 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 4c 49  ON()..**.**   LI
5d550 4b 45 46 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e 41  KEFUNC(zName, nA
5d560 72 67 2c 20 70 41 72 67 2c 20 66 6c 61 67 73 29  rg, pArg, flags)
5d570 0a 2a 2a 20 20 20 20 20 55 73 65 64 20 74 6f 20  .**     Used to 
5d580 63 72 65 61 74 65 20 61 20 73 63 61 6c 61 72 20  create a scalar 
5d590 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74  function definit
5d5a0 69 6f 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 6f  ion of a functio
5d5b0 6e 20 7a 4e 61 6d 65 20 0a 2a 2a 20 20 20 20 20  n zName .**     
5d5c0 74 68 61 74 20 61 63 63 65 70 74 73 20 6e 41 72  that accepts nAr
5d5d0 67 20 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 20  g arguments and 
5d5e0 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62  is implemented b
5d5f0 79 20 61 20 63 61 6c 6c 20 74 6f 20 43 20 0a 2a  y a call to C .*
5d600 2a 20 20 20 20 20 66 75 6e 63 74 69 6f 6e 20 6c  *     function l
5d610 69 6b 65 46 75 6e 63 2e 20 41 72 67 75 6d 65 6e  ikeFunc. Argumen
5d620 74 20 70 41 72 67 20 69 73 20 63 61 73 74 20 74  t pArg is cast t
5d630 6f 20 61 20 28 76 6f 69 64 20 2a 29 20 61 6e 64  o a (void *) and
5d640 20 6d 61 64 65 0a 2a 2a 20 20 20 20 20 61 76 61   made.**     ava
5d650 69 6c 61 62 6c 65 20 61 73 20 74 68 65 20 66 75  ilable as the fu
5d660 6e 63 74 69 6f 6e 20 75 73 65 72 2d 64 61 74 61  nction user-data
5d670 20 28 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64   (sqlite3_user_d
5d680 61 74 61 28 29 29 2e 20 54 68 65 0a 2a 2a 20 20  ata()). The.**  
5d690 20 20 20 46 75 6e 63 44 65 66 2e 66 6c 61 67 73     FuncDef.flags
5d6a0 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74   variable is set
5d6b0 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 70 61   to the value pa
5d6c0 73 73 65 64 20 61 73 20 74 68 65 20 66 6c 61 67  ssed as the flag
5d6d0 73 0a 2a 2a 20 20 20 20 20 70 61 72 61 6d 65 74  s.**     paramet
5d6e0 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46  er..*/.#define F
5d6f0 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e  UNCTION(zName, n
5d700 41 72 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20  Arg, iArg, bNC, 
5d710 78 46 75 6e 63 29 20 5c 0a 20 20 7b 6e 41 72 67  xFunc) \.  {nArg
5d720 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 62  , SQLITE_UTF8, b
5d730 4e 43 2a 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e  NC*SQLITE_FUNC_N
5d740 45 45 44 43 4f 4c 4c 2c 20 5c 0a 20 20 20 53 51  EEDCOLL, \.   SQ
5d750 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28  LITE_INT_TO_PTR(
5d760 69 41 72 67 29 2c 20 30 2c 20 78 46 75 6e 63 2c  iArg), 0, xFunc,
5d770 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30   0, 0, #zName, 0
5d780 7d 0a 23 64 65 66 69 6e 65 20 53 54 52 5f 46 55  }.#define STR_FU
5d790 4e 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41  NCTION(zName, nA
5d7a0 72 67 2c 20 70 41 72 67 2c 20 62 4e 43 2c 20 78  rg, pArg, bNC, x
5d7b0 46 75 6e 63 29 20 5c 0a 20 20 7b 6e 41 72 67 2c  Func) \.  {nArg,
5d7c0 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 62 4e   SQLITE_UTF8, bN
5d7d0 43 2a 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45  C*SQLITE_FUNC_NE
5d7e0 45 44 43 4f 4c 4c 2c 20 5c 0a 20 20 20 70 41 72  EDCOLL, \.   pAr
5d7f0 67 2c 20 30 2c 20 78 46 75 6e 63 2c 20 30 2c 20  g, 0, xFunc, 0, 
5d800 30 2c 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64  0, #zName, 0}.#d
5d810 65 66 69 6e 65 20 4c 49 4b 45 46 55 4e 43 28 7a  efine LIKEFUNC(z
5d820 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 61 72 67 2c  Name, nArg, arg,
5d830 20 66 6c 61 67 73 29 20 5c 0a 20 20 7b 6e 41 72   flags) \.  {nAr
5d840 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  g, SQLITE_UTF8, 
5d850 66 6c 61 67 73 2c 20 28 76 6f 69 64 20 2a 29 61  flags, (void *)a
5d860 72 67 2c 20 30 2c 20 6c 69 6b 65 46 75 6e 63 2c  rg, 0, likeFunc,
5d870 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30   0, 0, #zName, 0
5d880 7d 0a 23 64 65 66 69 6e 65 20 41 47 47 52 45 47  }.#define AGGREG
5d890 41 54 45 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c  ATE(zName, nArg,
5d8a0 20 61 72 67 2c 20 6e 63 2c 20 78 53 74 65 70 2c   arg, nc, xStep,
5d8b0 20 78 46 69 6e 61 6c 29 20 5c 0a 20 20 7b 6e 41   xFinal) \.  {nA
5d8c0 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  rg, SQLITE_UTF8,
5d8d0 20 6e 63 2a 53 51 4c 49 54 45 5f 46 55 4e 43 5f   nc*SQLITE_FUNC_
5d8e0 4e 45 45 44 43 4f 4c 4c 2c 20 5c 0a 20 20 20 53  NEEDCOLL, \.   S
5d8f0 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52  QLITE_INT_TO_PTR
5d900 28 61 72 67 29 2c 20 30 2c 20 30 2c 20 78 53 74  (arg), 0, 0, xSt
5d910 65 70 2c 78 46 69 6e 61 6c 2c 23 7a 4e 61 6d 65  ep,xFinal,#zName
5d920 2c 30 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63  ,0}../*.** All c
5d930 75 72 72 65 6e 74 20 73 61 76 65 70 6f 69 6e 74  urrent savepoint
5d940 73 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20  s are stored in 
5d950 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 74  a linked list st
5d960 61 72 74 69 6e 67 20 61 74 0a 2a 2a 20 73 71 6c  arting at.** sql
5d970 69 74 65 33 2e 70 53 61 76 65 70 6f 69 6e 74 2e  ite3.pSavepoint.
5d980 20 54 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65   The first eleme
5d990 6e 74 20 69 6e 20 74 68 65 20 6c 69 73 74 20 69  nt in the list i
5d9a0 73 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  s the most recen
5d9b0 74 6c 79 0a 2a 2a 20 6f 70 65 6e 65 64 20 73 61  tly.** opened sa
5d9c0 76 65 70 6f 69 6e 74 2e 20 53 61 76 65 70 6f 69  vepoint. Savepoi
5d9d0 6e 74 73 20 61 72 65 20 61 64 64 65 64 20 74 6f  nts are added to
5d9e0 20 74 68 65 20 6c 69 73 74 20 62 79 20 74 68 65   the list by the
5d9f0 20 76 64 62 65 0a 2a 2a 20 4f 50 5f 53 61 76 65   vdbe.** OP_Save
5da00 70 6f 69 6e 74 20 69 6e 73 74 72 75 63 74 69 6f  point instructio
5da10 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 61 76  n..*/.struct Sav
5da20 65 70 6f 69 6e 74 20 7b 0a 20 20 63 68 61 72 20  epoint {.  char 
5da30 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  *zName;         
5da40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5da50 2a 20 53 61 76 65 70 6f 69 6e 74 20 6e 61 6d 65  * Savepoint name
5da60 20 28 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64   (nul-terminated
5da70 29 20 2a 2f 0a 20 20 53 61 76 65 70 6f 69 6e 74  ) */.  Savepoint
5da80 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20   *pNext;        
5da90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
5daa0 72 65 6e 74 20 73 61 76 65 70 6f 69 6e 74 20 28  rent savepoint (
5dab0 69 66 20 61 6e 79 29 20 2a 2f 0a 7d 3b 0a 0a 2f  if any) */.};../
5dac0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
5dad0 6e 67 20 61 72 65 20 75 73 65 64 20 61 73 20 74  ng are used as t
5dae0 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  he second parame
5daf0 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 53 61  ter to sqlite3Sa
5db00 76 65 70 6f 69 6e 74 28 29 2c 0a 2a 2a 20 61 6e  vepoint(),.** an
5db10 64 20 61 73 20 74 68 65 20 50 31 20 61 72 67 75  d as the P1 argu
5db20 6d 65 6e 74 20 74 6f 20 74 68 65 20 4f 50 5f 53  ment to the OP_S
5db30 61 76 65 70 6f 69 6e 74 20 69 6e 73 74 72 75 63  avepoint instruc
5db40 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  tion..*/.#define
5db50 20 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49 4e   SAVEPOINT_BEGIN
5db60 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20        0.#define 
5db70 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53  SAVEPOINT_RELEAS
5db80 45 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53  E    1.#define S
5db90 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
5dba0 4b 20 20 20 32 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61  K   2.../*.** Ea
5dbb0 63 68 20 53 51 4c 69 74 65 20 6d 6f 64 75 6c 65  ch SQLite module
5dbc0 20 28 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20   (virtual table 
5dbd0 64 65 66 69 6e 69 74 69 6f 6e 29 20 69 73 20 64  definition) is d
5dbe0 65 66 69 6e 65 64 20 62 79 20 61 6e 0a 2a 2a 20  efined by an.** 
5dbf0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
5dc00 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
5dc10 75 72 65 2c 20 73 74 6f 72 65 64 20 69 6e 20 74  ure, stored in t
5dc20 68 65 20 73 71 6c 69 74 65 33 2e 61 4d 6f 64 75  he sqlite3.aModu
5dc30 6c 65 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65  le.** hash table
5dc40 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 6f 64 75  ..*/.struct Modu
5dc50 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c  le {.  const sql
5dc60 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
5dc70 64 75 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20 43  dule;       /* C
5dc80 61 6c 6c 62 61 63 6b 20 70 6f 69 6e 74 65 72 73  allback pointers
5dc90 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
5dca0 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20   *zName;        
5dcb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
5dcc0 6d 65 20 70 61 73 73 65 64 20 74 6f 20 63 72 65  me passed to cre
5dcd0 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a  ate_module() */.
5dce0 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20 20 20    void *pAux;   
5dcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5dd00 20 20 20 20 20 20 20 2f 2a 20 70 41 75 78 20 70         /* pAux p
5dd10 61 73 73 65 64 20 74 6f 20 63 72 65 61 74 65 5f  assed to create_
5dd20 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20 76 6f  module() */.  vo
5dd30 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 76  id (*xDestroy)(v
5dd40 6f 69 64 20 2a 29 3b 20 20 20 20 20 20 20 20 20  oid *);         
5dd50 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73     /* Module des
5dd60 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e  tructor function
5dd70 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 69 6e   */.};../*.** in
5dd80 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
5dd90 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 61  each column of a
5dda0 6e 20 53 51 4c 20 74 61 62 6c 65 20 69 73 20 68  n SQL table is h
5ddb0 65 6c 64 20 69 6e 20 61 6e 20 69 6e 73 74 61 6e  eld in an instan
5ddc0 63 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74  ce.** of this st
5ddd0 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75  ructure..*/.stru
5dde0 63 74 20 43 6f 6c 75 6d 6e 20 7b 0a 20 20 63 68  ct Column {.  ch
5ddf0 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 2f  ar *zName;     /
5de00 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 63  * Name of this c
5de10 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45 78 70 72 20  olumn */.  Expr 
5de20 2a 70 44 66 6c 74 3b 20 20 20 20 20 2f 2a 20 44  *pDflt;     /* D
5de30 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20  efault value of 
5de40 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20  this column */. 
5de50 20 63 68 61 72 20 2a 7a 44 66 6c 74 3b 20 20 20   char *zDflt;   
5de60 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 74 65    /* Original te
5de70 78 74 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c  xt of the defaul
5de80 74 20 76 61 6c 75 65 20 2a 2f 0a 20 20 63 68 61  t value */.  cha
5de90 72 20 2a 7a 54 79 70 65 3b 20 20 20 20 20 2f 2a  r *zType;     /*
5dea0 20 44 61 74 61 20 74 79 70 65 20 66 6f 72 20 74   Data type for t
5deb0 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20  his column */.  
5dec0 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20  char *zColl;    
5ded0 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65   /* Collating se
5dee0 71 75 65 6e 63 65 2e 20 20 49 66 20 4e 55 4c 4c  quence.  If NULL
5def0 2c 20 75 73 65 20 74 68 65 20 64 65 66 61 75 6c  , use the defaul
5df00 74 20 2a 2f 0a 20 20 75 38 20 6e 6f 74 4e 75 6c  t */.  u8 notNul
5df10 6c 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  l;      /* True 
5df20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 4e 4f  if there is a NO
5df30 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e  T NULL constrain
5df40 74 20 2a 2f 0a 20 20 75 38 20 69 73 50 72 69 6d  t */.  u8 isPrim
5df50 4b 65 79 3b 20 20 20 20 2f 2a 20 54 72 75 65 20  Key;    /* True 
5df60 69 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 69  if this column i
5df70 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 50 52  s part of the PR
5df80 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 63  IMARY KEY */.  c
5df90 68 61 72 20 61 66 66 69 6e 69 74 79 3b 20 20 20  har affinity;   
5dfa0 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 53 51  /* One of the SQ
5dfb0 4c 49 54 45 5f 41 46 46 5f 2e 2e 2e 20 76 61 6c  LITE_AFF_... val
5dfc0 75 65 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ues */.#ifndef S
5dfd0 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
5dfe0 41 4c 54 41 42 4c 45 0a 20 20 75 38 20 69 73 48  ALTABLE.  u8 isH
5dff0 69 64 64 65 6e 3b 20 20 20 20 20 2f 2a 20 54 72  idden;     /* Tr
5e000 75 65 20 69 66 20 74 68 69 73 20 63 6f 6c 75 6d  ue if this colum
5e010 6e 20 69 73 20 27 68 69 64 64 65 6e 27 20 2a 2f  n is 'hidden' */
5e020 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a  .#endif.};../*.*
5e030 2a 20 41 20 22 43 6f 6c 6c 61 74 69 6e 67 20 53  * A "Collating S
5e040 65 71 75 65 6e 63 65 22 20 69 73 20 64 65 66 69  equence" is defi
5e050 6e 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e  ned by an instan
5e060 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
5e070 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  ing.** structure
5e080 2e 20 43 6f 6e 63 65 70 74 75 61 6c 6c 79 2c 20  . Conceptually, 
5e090 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  a collating sequ
5e0a0 65 6e 63 65 20 63 6f 6e 73 69 73 74 73 20 6f 66  ence consists of
5e0b0 20 61 20 6e 61 6d 65 20 61 6e 64 0a 2a 2a 20 61   a name and.** a
5e0c0 20 63 6f 6d 70 61 72 69 73 6f 6e 20 72 6f 75 74   comparison rout
5e0d0 69 6e 65 20 74 68 61 74 20 64 65 66 69 6e 65 73  ine that defines
5e0e0 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20 74 68   the order of th
5e0f0 61 74 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a  at sequence..**.
5e100 2a 2a 20 54 68 65 72 65 20 6d 61 79 20 74 77 6f  ** There may two
5e110 20 73 65 70 61 72 61 74 65 20 69 6d 70 6c 65 6d   separate implem
5e120 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65  entations of the
5e130 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74   collation funct
5e140 69 6f 6e 2c 20 6f 6e 65 0a 2a 2a 20 74 68 61 74  ion, one.** that
5e150 20 70 72 6f 63 65 73 73 65 73 20 74 65 78 74 20   processes text 
5e160 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e  in UTF-8 encodin
5e170 67 20 28 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 29  g (CollSeq.xCmp)
5e180 20 61 6e 64 20 61 6e 6f 74 68 65 72 20 74 68 61   and another tha
5e190 74 0a 2a 2a 20 70 72 6f 63 65 73 73 65 73 20 74  t.** processes t
5e1a0 65 78 74 20 65 6e 63 6f 64 65 64 20 69 6e 20 55  ext encoded in U
5e1b0 54 46 2d 31 36 20 28 43 6f 6c 6c 53 65 71 2e 78  TF-16 (CollSeq.x
5e1c0 43 6d 70 31 36 29 2c 20 75 73 69 6e 67 20 74 68  Cmp16), using th
5e1d0 65 20 6d 61 63 68 69 6e 65 0a 2a 2a 20 6e 61 74  e machine.** nat
5e1e0 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 2e 20  ive byte order. 
5e1f0 57 68 65 6e 20 61 20 63 6f 6c 6c 61 74 69 6f 6e  When a collation
5e200 20 73 65 71 75 65 6e 63 65 20 69 73 20 69 6e 76   sequence is inv
5e210 6f 6b 65 64 2c 20 53 51 4c 69 74 65 20 73 65 6c  oked, SQLite sel
5e220 65 63 74 73 0a 2a 2a 20 74 68 65 20 76 65 72 73  ects.** the vers
5e230 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 72 65  ion that will re
5e240 71 75 69 72 65 20 74 68 65 20 6c 65 61 73 74 20  quire the least 
5e250 65 78 70 65 6e 73 69 76 65 20 65 6e 63 6f 64 69  expensive encodi
5e260 6e 67 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6f  ng.** translatio
5e270 6e 73 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a  ns, if any..**.*
5e280 2a 20 54 68 65 20 43 6f 6c 6c 53 65 71 2e 70 55  * The CollSeq.pU
5e290 73 65 72 20 6d 65 6d 62 65 72 20 76 61 72 69 61  ser member varia
5e2a0 62 6c 65 20 69 73 20 61 6e 20 65 78 74 72 61 20  ble is an extra 
5e2b0 70 61 72 61 6d 65 74 65 72 20 74 68 61 74 20 70  parameter that p
5e2c0 61 73 73 65 64 20 69 6e 0a 2a 2a 20 61 73 20 74  assed in.** as t
5e2d0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
5e2e0 74 20 74 6f 20 74 68 65 20 55 54 46 2d 38 20 63  t to the UTF-8 c
5e2f0 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69  omparison functi
5e300 6f 6e 2c 20 78 43 6d 70 2e 0a 2a 2a 20 43 6f 6c  on, xCmp..** Col
5e310 6c 53 65 71 2e 70 55 73 65 72 31 36 20 69 73 20  lSeq.pUser16 is 
5e320 74 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20 66  the equivalent f
5e330 6f 72 20 74 68 65 20 55 54 46 2d 31 36 20 63 6f  or the UTF-16 co
5e340 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f  mparison functio
5e350 6e 2c 0a 2a 2a 20 78 43 6d 70 31 36 2e 0a 2a 2a  n,.** xCmp16..**
5e360 0a 2a 2a 20 49 66 20 62 6f 74 68 20 43 6f 6c 6c  .** If both Coll
5e370 53 65 71 2e 78 43 6d 70 20 61 6e 64 20 43 6f 6c  Seq.xCmp and Col
5e380 6c 53 65 71 2e 78 43 6d 70 31 36 20 61 72 65 20  lSeq.xCmp16 are 
5e390 4e 55 4c 4c 2c 20 69 74 20 6d 65 61 6e 73 20 74  NULL, it means t
5e3a0 68 61 74 20 74 68 65 0a 2a 2a 20 63 6f 6c 6c 61  hat the.** colla
5e3b0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73  ting sequence is
5e3c0 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 49 6e 64   undefined.  Ind
5e3d0 69 63 65 73 20 62 75 69 6c 74 20 6f 6e 20 61 6e  ices built on an
5e3e0 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 63 6f   undefined.** co
5e3f0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
5e400 20 6d 61 79 20 6e 6f 74 20 62 65 20 72 65 61 64   may not be read
5e410 20 6f 72 20 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a   or written..*/.
5e420 73 74 72 75 63 74 20 43 6f 6c 6c 53 65 71 20 7b  struct CollSeq {
5e430 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  .  char *zName; 
5e440 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
5e450 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e   of the collatin
5e460 67 20 73 65 71 75 65 6e 63 65 2c 20 55 54 46 2d  g sequence, UTF-
5e470 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 75  8 encoded */.  u
5e480 38 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20 20  8 enc;          
5e490 20 20 20 20 20 2f 2a 20 54 65 78 74 20 65 6e 63       /* Text enc
5e4a0 6f 64 69 6e 67 20 68 61 6e 64 6c 65 64 20 62 79  oding handled by
5e4b0 20 78 43 6d 70 28 29 20 2a 2f 0a 20 20 75 38 20   xCmp() */.  u8 
5e4c0 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20  type;           
5e4d0 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65     /* One of the
5e4e0 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 2e 2e 2e   SQLITE_COLL_...
5e4f0 20 76 61 6c 75 65 73 20 62 65 6c 6f 77 20 2a 2f   values below */
5e500 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 72 3b 20  .  void *pUser; 
5e510 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
5e520 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 43  t argument to xC
5e530 6d 70 28 29 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  mp() */.  int (*
5e540 78 43 6d 70 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  xCmp)(void*,int,
5e550 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e   const void*, in
5e560 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 29 3b  t, const void*);
5e570 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28  .  void (*xDel)(
5e580 76 6f 69 64 2a 29 3b 20 20 2f 2a 20 44 65 73 74  void*);  /* Dest
5e590 72 75 63 74 6f 72 20 66 6f 72 20 70 55 73 65 72  ructor for pUser
5e5a0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c   */.};../*.** Al
5e5b0 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66 20  lowed values of 
5e5c0 43 6f 6c 6c 53 65 71 2e 74 79 70 65 3a 0a 2a 2f  CollSeq.type:.*/
5e5d0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5e5e0 43 4f 4c 4c 5f 42 49 4e 41 52 59 20 20 31 20 20  COLL_BINARY  1  
5e5f0 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 6d  /* The default m
5e600 65 6d 63 6d 70 28 29 20 63 6f 6c 6c 61 74 69 6e  emcmp() collatin
5e610 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64  g sequence */.#d
5e620 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c  efine SQLITE_COL
5e630 4c 5f 4e 4f 43 41 53 45 20 20 32 20 20 2f 2a 20  L_NOCASE  2  /* 
5e640 54 68 65 20 62 75 69 6c 74 2d 69 6e 20 4e 4f 43  The built-in NOC
5e650 41 53 45 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  ASE collating se
5e660 71 75 65 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e  quence */.#defin
5e670 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 52 45  e SQLITE_COLL_RE
5e680 56 45 52 53 45 20 33 20 20 2f 2a 20 54 68 65 20  VERSE 3  /* The 
5e690 62 75 69 6c 74 2d 69 6e 20 52 45 56 45 52 53 45  built-in REVERSE
5e6a0 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
5e6b0 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  nce */.#define S
5e6c0 51 4c 49 54 45 5f 43 4f 4c 4c 5f 55 53 45 52 20  QLITE_COLL_USER 
5e6d0 20 20 20 30 20 20 2f 2a 20 41 6e 79 20 6f 74 68     0  /* Any oth
5e6e0 65 72 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20  er user-defined 
5e6f0 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
5e700 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73  ce */../*.** A s
5e710 6f 72 74 20 6f 72 64 65 72 20 63 61 6e 20 62 65  ort order can be
5e720 20 65 69 74 68 65 72 20 41 53 43 20 6f 72 20 44   either ASC or D
5e730 45 53 43 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ESC..*/.#define 
5e740 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 20 20 20  SQLITE_SO_ASC   
5e750 20 20 20 20 30 20 20 2f 2a 20 53 6f 72 74 20 69      0  /* Sort i
5e760 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65  n ascending orde
5e770 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  r */.#define SQL
5e780 49 54 45 5f 53 4f 5f 44 45 53 43 20 20 20 20 20  ITE_SO_DESC     
5e790 20 31 20 20 2f 2a 20 53 6f 72 74 20 69 6e 20 61   1  /* Sort in a
5e7a0 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a  scending order *
5e7b0 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c 75 6d 6e 20  /../*.** Column 
5e7c0 61 66 66 69 6e 69 74 79 20 74 79 70 65 73 2e 0a  affinity types..
5e7d0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 75 73 65 64  **.** These used
5e7e0 20 74 6f 20 68 61 76 65 20 6d 6e 65 6d 6f 6e 69   to have mnemoni
5e7f0 63 20 6e 61 6d 65 20 6c 69 6b 65 20 27 69 27 20  c name like 'i' 
5e800 66 6f 72 20 53 51 4c 49 54 45 5f 41 46 46 5f 49  for SQLITE_AFF_I
5e810 4e 54 45 47 45 52 20 61 6e 64 0a 2a 2a 20 27 74  NTEGER and.** 't
5e820 27 20 66 6f 72 20 53 51 4c 49 54 45 5f 41 46 46  ' for SQLITE_AFF
5e830 5f 54 45 58 54 2e 20 20 42 75 74 20 77 65 20 63  _TEXT.  But we c
5e840 61 6e 20 73 61 76 65 20 61 20 6c 69 74 74 6c 65  an save a little
5e850 20 73 70 61 63 65 20 61 6e 64 20 69 6d 70 72 6f   space and impro
5e860 76 65 0a 2a 2a 20 74 68 65 20 73 70 65 65 64 20  ve.** the speed 
5e870 61 20 6c 69 74 74 6c 65 20 62 79 20 6e 75 6d 62  a little by numb
5e880 65 72 69 6e 67 20 74 68 65 20 76 61 6c 75 65 73  ering the values
5e890 20 63 6f 6e 73 65 63 75 74 69 76 65 6c 79 2e 20   consecutively. 
5e8a0 20 0a 2a 2a 0a 2a 2a 20 42 75 74 20 72 61 74 68   .**.** But rath
5e8b0 65 72 20 74 68 61 6e 20 73 74 61 72 74 20 77 69  er than start wi
5e8c0 74 68 20 30 20 6f 72 20 31 2c 20 77 65 20 62 65  th 0 or 1, we be
5e8d0 67 69 6e 20 77 69 74 68 20 27 61 27 2e 20 20 54  gin with 'a'.  T
5e8e0 68 61 74 20 77 61 79 2c 0a 2a 2a 20 77 68 65 6e  hat way,.** when
5e8f0 20 6d 75 6c 74 69 70 6c 65 20 61 66 66 69 6e 69   multiple affini
5e900 74 79 20 74 79 70 65 73 20 61 72 65 20 63 6f 6e  ty types are con
5e910 63 61 74 65 6e 61 74 65 64 20 69 6e 74 6f 20 61  catenated into a
5e920 20 73 74 72 69 6e 67 20 61 6e 64 0a 2a 2a 20 75   string and.** u
5e930 73 65 64 20 61 73 20 74 68 65 20 50 34 20 6f 70  sed as the P4 op
5e940 65 72 61 6e 64 2c 20 74 68 65 79 20 77 69 6c 6c  erand, they will
5e950 20 62 65 20 6d 6f 72 65 20 72 65 61 64 61 62 6c   be more readabl
5e960 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 61 6c  e..**.** Note al
5e970 73 6f 20 74 68 61 74 20 74 68 65 20 6e 75 6d 65  so that the nume
5e980 72 69 63 20 74 79 70 65 73 20 61 72 65 20 67 72  ric types are gr
5e990 6f 75 70 65 64 20 74 6f 67 65 74 68 65 72 20 73  ouped together s
5e9a0 6f 20 74 68 61 74 20 74 65 73 74 69 6e 67 0a 2a  o that testing.*
5e9b0 2a 20 66 6f 72 20 61 20 6e 75 6d 65 72 69 63 20  * for a numeric 
5e9c0 74 79 70 65 20 69 73 20 61 20 73 69 6e 67 6c 65  type is a single
5e9d0 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 0a 2a 2f 0a   comparison..*/.
5e9e0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
5e9f0 46 46 5f 54 45 58 54 20 20 20 20 20 27 61 27 0a  FF_TEXT     'a'.
5ea00 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
5ea10 46 46 5f 4e 4f 4e 45 20 20 20 20 20 27 62 27 0a  FF_NONE     'b'.
5ea20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
5ea30 46 46 5f 4e 55 4d 45 52 49 43 20 20 27 63 27 0a  FF_NUMERIC  'c'.
5ea40 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
5ea50 46 46 5f 49 4e 54 45 47 45 52 20 20 27 64 27 0a  FF_INTEGER  'd'.
5ea60 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
5ea70 46 46 5f 52 45 41 4c 20 20 20 20 20 27 65 27 0a  FF_REAL     'e'.
5ea80 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
5ea90 49 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74  IsNumericAffinit
5eaa0 79 28 58 29 20 20 28 28 58 29 3e 3d 53 51 4c 49  y(X)  ((X)>=SQLI
5eab0 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 29 0a  TE_AFF_NUMERIC).
5eac0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54  ./*.** The SQLIT
5ead0 45 5f 41 46 46 5f 4d 41 53 4b 20 76 61 6c 75 65  E_AFF_MASK value
5eae0 73 20 6d 61 73 6b 73 20 6f 66 66 20 74 68 65 20  s masks off the 
5eaf0 73 69 67 6e 69 66 69 63 61 6e 74 20 62 69 74 73  significant bits
5eb00 20 6f 66 20 61 6e 0a 2a 2a 20 61 66 66 69 6e 69   of an.** affini
5eb10 74 79 20 76 61 6c 75 65 2e 20 0a 2a 2f 0a 23 64  ty value. .*/.#d
5eb20 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46  efine SQLITE_AFF
5eb30 5f 4d 41 53 4b 20 20 20 20 20 30 78 36 37 0a 0a  _MASK     0x67..
5eb40 2f 2a 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c  /*.** Additional
5eb50 20 62 69 74 20 76 61 6c 75 65 73 20 74 68 61 74   bit values that
5eb60 20 63 61 6e 20 62 65 20 4f 52 65 64 20 77 69 74   can be ORed wit
5eb70 68 20 61 6e 20 61 66 66 69 6e 69 74 79 20 77 69  h an affinity wi
5eb80 74 68 6f 75 74 0a 2a 2a 20 63 68 61 6e 67 69 6e  thout.** changin
5eb90 67 20 74 68 65 20 61 66 66 69 6e 69 74 79 2e 0a  g the affinity..
5eba0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
5ebb0 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 20 20 30  E_JUMPIFNULL   0
5ebc0 78 30 38 20 20 2f 2a 20 6a 75 6d 70 73 20 69 66  x08  /* jumps if
5ebd0 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20   either operand 
5ebe0 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69  is NULL */.#defi
5ebf0 6e 65 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50  ne SQLITE_STOREP
5ec00 32 20 20 20 20 20 20 30 78 31 30 20 20 2f 2a 20  2      0x10  /* 
5ec10 53 74 6f 72 65 20 72 65 73 75 6c 74 20 69 6e 20  Store result in 
5ec20 72 65 67 5b 50 32 5d 20 72 61 74 68 65 72 20 74  reg[P2] rather t
5ec30 68 61 6e 20 6a 75 6d 70 20 2a 2f 0a 0a 2f 2a 0a  han jump */../*.
5ec40 2a 2a 20 41 6e 20 6f 62 6a 65 63 74 20 6f 66 20  ** An object of 
5ec50 74 68 69 73 20 74 79 70 65 20 69 73 20 63 72 65  this type is cre
5ec60 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 76 69  ated for each vi
5ec70 72 74 75 61 6c 20 74 61 62 6c 65 20 70 72 65 73  rtual table pres
5ec80 65 6e 74 20 69 6e 0a 2a 2a 20 74 68 65 20 64 61  ent in.** the da
5ec90 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 0a  tabase schema. .
5eca0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74  **.** If the dat
5ecb0 61 62 61 73 65 20 73 63 68 65 6d 61 20 69 73 20  abase schema is 
5ecc0 73 68 61 72 65 64 2c 20 74 68 65 6e 20 74 68 65  shared, then the
5ecd0 72 65 20 69 73 20 6f 6e 65 20 69 6e 73 74 61 6e  re is one instan
5ece0 63 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73 74  ce of this.** st
5ecf0 72 75 63 74 75 72 65 20 66 6f 72 20 65 61 63 68  ructure for each
5ed00 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
5ed10 74 69 6f 6e 20 28 73 71 6c 69 74 65 33 2a 29 20  tion (sqlite3*) 
5ed20 74 68 61 74 20 75 73 65 73 20 74 68 65 20 73 68  that uses the sh
5ed30 61 72 65 64 0a 2a 2a 20 73 63 68 65 6d 61 2e 20  ared.** schema. 
5ed40 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20  This is because 
5ed50 65 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f  each database co
5ed60 6e 6e 65 63 74 69 6f 6e 20 72 65 71 75 69 72 65  nnection require
5ed70 73 20 69 74 73 20 6f 77 6e 20 75 6e 69 71 75 65  s its own unique
5ed80 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  .** instance of 
5ed90 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62  the sqlite3_vtab
5eda0 2a 20 68 61 6e 64 6c 65 20 75 73 65 64 20 74 6f  * handle used to
5edb0 20 61 63 63 65 73 73 20 74 68 65 20 76 69 72 74   access the virt
5edc0 75 61 6c 20 74 61 62 6c 65 20 0a 2a 2a 20 69 6d  ual table .** im
5edd0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 73 71  plementation. sq
5ede0 6c 69 74 65 33 5f 76 74 61 62 2a 20 68 61 6e 64  lite3_vtab* hand
5edf0 6c 65 73 20 63 61 6e 20 6e 6f 74 20 62 65 20 73  les can not be s
5ee00 68 61 72 65 64 20 62 65 74 77 65 65 6e 20 0a 2a  hared between .*
5ee10 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * database conne
5ee20 63 74 69 6f 6e 73 2c 20 65 76 65 6e 20 77 68 65  ctions, even whe
5ee30 6e 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 68  n the rest of th
5ee40 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  e in-memory data
5ee50 62 61 73 65 20 0a 2a 2a 20 73 63 68 65 6d 61 20  base .** schema 
5ee60 69 73 20 73 68 61 72 65 64 2c 20 61 73 20 74 68  is shared, as th
5ee70 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
5ee80 20 6f 66 74 65 6e 20 73 74 6f 72 65 73 20 74 68   often stores th
5ee90 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f  e database.** co
5eea0 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20  nnection handle 
5eeb0 70 61 73 73 65 64 20 74 6f 20 69 74 20 76 69 61  passed to it via
5eec0 20 74 68 65 20 78 43 6f 6e 6e 65 63 74 28 29 20   the xConnect() 
5eed0 6f 72 20 78 43 72 65 61 74 65 28 29 20 6d 65 74  or xCreate() met
5eee0 68 6f 64 0a 2a 2a 20 64 75 72 69 6e 67 20 69 6e  hod.** during in
5eef0 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69 6e 74  itialization int
5ef00 65 72 6e 61 6c 6c 79 2e 20 54 68 69 73 20 64 61  ernally. This da
5ef10 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
5ef20 6e 20 68 61 6e 64 6c 65 20 6d 61 79 0a 2a 2a 20  n handle may.** 
5ef30 74 68 65 6e 20 75 73 65 64 20 62 79 20 74 68 65  then used by the
5ef40 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69   virtual table i
5ef50 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 6f  mplementation to
5ef60 20 61 63 63 65 73 73 20 72 65 61 6c 20 74 61 62   access real tab
5ef70 6c 65 73 20 0a 2a 2a 20 77 69 74 68 69 6e 20 74  les .** within t
5ef80 68 65 20 64 61 74 61 62 61 73 65 2e 20 53 6f 20  he database. So 
5ef90 74 68 61 74 20 74 68 65 79 20 61 70 70 65 61 72  that they appear
5efa0 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   as part of the 
5efb0 63 61 6c 6c 65 72 73 20 0a 2a 2a 20 74 72 61 6e  callers .** tran
5efc0 73 61 63 74 69 6f 6e 2c 20 74 68 65 73 65 20 61  saction, these a
5efd0 63 63 65 73 73 65 73 20 6e 65 65 64 20 74 6f 20  ccesses need to 
5efe0 62 65 20 6d 61 64 65 20 76 69 61 20 74 68 65 20  be made via the 
5eff0 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 0a 2a  same database .*
5f000 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20  * connection as 
5f010 74 68 61 74 20 75 73 65 64 20 74 6f 20 65 78 65  that used to exe
5f020 63 75 74 65 20 53 51 4c 20 6f 70 65 72 61 74 69  cute SQL operati
5f030 6f 6e 73 20 6f 6e 20 74 68 65 20 76 69 72 74 75  ons on the virtu
5f040 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  al table..**.** 
5f050 41 6c 6c 20 56 54 61 62 6c 65 20 6f 62 6a 65 63  All VTable objec
5f060 74 73 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f  ts that correspo
5f070 6e 64 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 74  nd to a single t
5f080 61 62 6c 65 20 69 6e 20 61 20 73 68 61 72 65 64  able in a shared
5f090 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63 68  .** database sch
5f0a0 65 6d 61 20 61 72 65 20 69 6e 69 74 69 61 6c 6c  ema are initiall
5f0b0 79 20 73 74 6f 72 65 64 20 69 6e 20 61 20 6c 69  y stored in a li
5f0c0 6e 6b 65 64 2d 6c 69 73 74 20 70 6f 69 6e 74 65  nked-list pointe
5f0d0 64 20 74 6f 20 62 79 0a 2a 2a 20 74 68 65 20 54  d to by.** the T
5f0e0 61 62 6c 65 2e 70 56 54 61 62 6c 65 20 6d 65 6d  able.pVTable mem
5f0f0 62 65 72 20 76 61 72 69 61 62 6c 65 20 6f 66 20  ber variable of 
5f100 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
5f110 67 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 2e 0a  g Table object..
5f120 2a 2a 20 57 68 65 6e 20 61 6e 20 73 71 6c 69 74  ** When an sqlit
5f130 65 33 5f 70 72 65 70 61 72 65 28 29 20 6f 70 65  e3_prepare() ope
5f140 72 61 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72  ration is requir
5f150 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 65  ed to access the
5f160 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c   virtual.** tabl
5f170 65 2c 20 69 74 20 73 65 61 72 63 68 65 73 20 74  e, it searches t
5f180 68 65 20 6c 69 73 74 20 66 6f 72 20 74 68 65 20  he list for the 
5f190 56 54 61 62 6c 65 20 74 68 61 74 20 63 6f 72 72  VTable that corr
5f1a0 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 0a 2a  esponds to the.*
5f1b0 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * database conne
5f1c0 63 74 69 6f 6e 20 64 6f 69 6e 67 20 74 68 65 20  ction doing the 
5f1d0 70 72 65 70 61 72 69 6e 67 20 73 6f 20 61 73 20  preparing so as 
5f1e0 74 6f 20 75 73 65 20 74 68 65 20 63 6f 72 72 65  to use the corre
5f1f0 63 74 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 74  ct.** sqlite3_vt
5f200 61 62 2a 20 68 61 6e 64 6c 65 20 69 6e 20 74 68  ab* handle in th
5f210 65 20 63 6f 6d 70 69 6c 65 64 20 71 75 65 72 79  e compiled query
5f220 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 6e 20  ..**.** When an 
5f230 69 6e 2d 6d 65 6d 6f 72 79 20 54 61 62 6c 65 20  in-memory Table 
5f240 6f 62 6a 65 63 74 20 69 73 20 64 65 6c 65 74 65  object is delete
5f250 64 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 77  d (for example w
5f260 68 65 6e 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d  hen the.** schem
5f270 61 20 69 73 20 62 65 69 6e 67 20 72 65 6c 6f 61  a is being reloa
5f280 64 65 64 20 66 6f 72 20 73 6f 6d 65 20 72 65 61  ded for some rea
5f290 73 6f 6e 29 2c 20 74 68 65 20 56 54 61 62 6c 65  son), the VTable
5f2a0 20 6f 62 6a 65 63 74 73 20 61 72 65 20 6e 6f 74   objects are not
5f2b0 20 0a 2a 2a 20 64 65 6c 65 74 65 64 20 61 6e 64   .** deleted and
5f2c0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61   the sqlite3_vta
5f2d0 62 2a 20 68 61 6e 64 6c 65 73 20 61 72 65 20 6e  b* handles are n
5f2e0 6f 74 20 78 44 69 73 63 6f 6e 6e 65 63 74 28 29  ot xDisconnect()
5f2f0 65 64 20 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65  ed .** immediate
5f300 6c 79 2e 20 49 6e 73 74 65 61 64 2c 20 74 68 65  ly. Instead, the
5f310 79 20 61 72 65 20 6d 6f 76 65 64 20 66 72 6f 6d  y are moved from
5f320 20 74 68 65 20 54 61 62 6c 65 2e 70 56 54 61 62   the Table.pVTab
5f330 6c 65 20 6c 69 73 74 20 74 6f 0a 2a 2a 20 61 6e  le list to.** an
5f340 6f 74 68 65 72 20 6c 69 6e 6b 65 64 20 6c 69 73  other linked lis
5f350 74 20 68 65 61 64 65 64 20 62 79 20 74 68 65 20  t headed by the 
5f360 73 71 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e  sqlite3.pDisconn
5f370 65 63 74 20 6d 65 6d 62 65 72 20 6f 66 20 74 68  ect member of th
5f380 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69  e.** correspondi
5f390 6e 67 20 73 71 6c 69 74 65 33 20 73 74 72 75 63  ng sqlite3 struc
5f3a0 74 75 72 65 2e 20 54 68 65 79 20 61 72 65 20 74  ture. They are t
5f3b0 68 65 6e 20 64 65 6c 65 74 65 64 2f 78 44 69 73  hen deleted/xDis
5f3c0 63 6f 6e 6e 65 63 74 65 64 20 0a 2a 2a 20 6e 65  connected .** ne
5f3d0 78 74 20 74 69 6d 65 20 61 20 73 74 61 74 65 6d  xt time a statem
5f3e0 65 6e 74 20 69 73 20 70 72 65 70 61 72 65 64 20  ent is prepared 
5f3f0 75 73 69 6e 67 20 73 61 69 64 20 73 71 6c 69 74  using said sqlit
5f400 65 33 2a 2e 20 54 68 69 73 20 69 73 20 64 6f 6e  e3*. This is don
5f410 65 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 20 64 65  e.** to avoid de
5f420 61 64 6c 6f 63 6b 20 69 73 73 75 65 73 20 69 6e  adlock issues in
5f430 76 6f 6c 76 69 6e 67 20 6d 75 6c 74 69 70 6c 65  volving multiple
5f440 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 20 6d   sqlite3.mutex m
5f450 75 74 65 78 65 73 2e 0a 2a 2a 20 52 65 66 65 72  utexes..** Refer
5f460 20 74 6f 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f   to comments abo
5f470 76 65 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69  ve function sqli
5f480 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69 73  te3VtabUnlockLis
5f490 74 28 29 20 66 6f 72 20 61 6e 0a 2a 2a 20 65 78  t() for an.** ex
5f4a0 70 6c 61 6e 61 74 69 6f 6e 20 61 73 20 74 6f 20  planation as to 
5f4b0 77 68 79 20 69 74 20 69 73 20 73 61 66 65 20 74  why it is safe t
5f4c0 6f 20 61 64 64 20 61 6e 20 65 6e 74 72 79 20 74  o add an entry t
5f4d0 6f 20 61 6e 20 73 71 6c 69 74 65 33 2e 70 44 69  o an sqlite3.pDi
5f4e0 73 63 6f 6e 6e 65 63 74 0a 2a 2a 20 6c 69 73 74  sconnect.** list
5f4f0 20 77 69 74 68 6f 75 74 20 68 6f 6c 64 69 6e 67   without holding
5f500 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
5f510 6e 67 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78  ng sqlite3.mutex
5f520 20 6d 75 74 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68   mutex..**.** Th
5f530 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 6f 62 6a  e memory for obj
5f540 65 63 74 73 20 6f 66 20 74 68 69 73 20 74 79 70  ects of this typ
5f550 65 20 69 73 20 61 6c 77 61 79 73 20 61 6c 6c 6f  e is always allo
5f560 63 61 74 65 64 20 62 79 20 0a 2a 2a 20 73 71 6c  cated by .** sql
5f570 69 74 65 33 44 62 4d 61 6c 6c 6f 63 28 29 2c 20  ite3DbMalloc(), 
5f580 75 73 69 6e 67 20 74 68 65 20 63 6f 6e 6e 65 63  using the connec
5f590 74 69 6f 6e 20 68 61 6e 64 6c 65 20 73 74 6f 72  tion handle stor
5f5a0 65 64 20 69 6e 20 56 54 61 62 6c 65 2e 64 62 20  ed in VTable.db 
5f5b0 61 73 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 74  as .** the first
5f5c0 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74   argument..*/.st
5f5d0 72 75 63 74 20 56 54 61 62 6c 65 20 7b 0a 20 20  ruct VTable {.  
5f5e0 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
5f5f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
5f600 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
5f610 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
5f620 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20   this table */. 
5f630 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 20 20   Module *pMod;  
5f640 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
5f650 69 6e 74 65 72 20 74 6f 20 6d 6f 64 75 6c 65 20  inter to module 
5f660 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
5f670 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  /.  sqlite3_vtab
5f680 20 2a 70 56 74 61 62 3b 20 20 20 20 20 20 2f 2a   *pVtab;      /*
5f690 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 74 61 62   Pointer to vtab
5f6a0 20 69 6e 73 74 61 6e 63 65 20 2a 2f 0a 20 20 69   instance */.  i
5f6b0 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20  nt nRef;        
5f6c0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
5f6d0 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74  er of pointers t
5f6e0 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  o this structure
5f6f0 20 2a 2f 0a 20 20 56 54 61 62 6c 65 20 2a 70 4e   */.  VTable *pN
5f700 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ext;            
5f710 2f 2a 20 4e 65 78 74 20 69 6e 20 6c 69 6e 6b 65  /* Next in linke
5f720 64 20 6c 69 73 74 20 28 73 65 65 20 61 62 6f 76  d list (see abov
5f730 65 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  e) */.};../*.** 
5f740 45 61 63 68 20 53 51 4c 20 74 61 62 6c 65 20 69  Each SQL table i
5f750 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 69 6e  s represented in
5f760 20 6d 65 6d 6f 72 79 20 62 79 20 61 6e 20 69 6e   memory by an in
5f770 73 74 61 6e 63 65 20 6f 66 20 74 68 65 0a 2a 2a  stance of the.**
5f780 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
5f790 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c  ture..**.** Tabl
5f7a0 65 2e 7a 4e 61 6d 65 20 69 73 20 74 68 65 20 6e  e.zName is the n
5f7b0 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
5f7c0 2e 20 20 54 68 65 20 63 61 73 65 20 6f 66 20 74  .  The case of t
5f7d0 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 43  he original.** C
5f7e0 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
5f7f0 65 6d 65 6e 74 20 69 73 20 73 74 6f 72 65 64 2c  ement is stored,
5f800 20 62 75 74 20 63 61 73 65 20 69 73 20 6e 6f 74   but case is not
5f810 20 73 69 67 6e 69 66 69 63 61 6e 74 20 66 6f 72   significant for
5f820 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e  .** comparisons.
5f830 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 6e 43 6f  .**.** Table.nCo
5f840 6c 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  l is the number 
5f850 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
5f860 69 73 20 74 61 62 6c 65 2e 20 20 54 61 62 6c 65  is table.  Table
5f870 2e 61 43 6f 6c 20 69 73 20 61 0a 2a 2a 20 70 6f  .aCol is a.** po
5f880 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61  inter to an arra
5f890 79 20 6f 66 20 43 6f 6c 75 6d 6e 20 73 74 72 75  y of Column stru
5f8a0 63 74 75 72 65 73 2c 20 6f 6e 65 20 66 6f 72 20  ctures, one for 
5f8b0 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a  each column..**.
5f8c0 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20  ** If the table 
5f8d0 68 61 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50  has an INTEGER P
5f8e0 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e  RIMARY KEY, then
5f8f0 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20   Table.iPKey is 
5f900 74 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20  the index of.** 
5f910 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20  the column that 
5f920 69 73 20 74 68 61 74 20 6b 65 79 2e 20 20 20 4f  is that key.   O
5f930 74 68 65 72 77 69 73 65 20 54 61 62 6c 65 2e 69  therwise Table.i
5f940 50 4b 65 79 20 69 73 20 6e 65 67 61 74 69 76 65  PKey is negative
5f950 2e 20 20 4e 6f 74 65 0a 2a 2a 20 74 68 61 74 20  .  Note.** that 
5f960 74 68 65 20 64 61 74 61 74 79 70 65 20 6f 66 20  the datatype of 
5f970 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20  the PRIMARY KEY 
5f980 6d 75 73 74 20 62 65 20 49 4e 54 45 47 45 52 20  must be INTEGER 
5f990 66 6f 72 20 74 68 69 73 20 66 69 65 6c 64 20 74  for this field t
5f9a0 6f 0a 2a 2a 20 62 65 20 73 65 74 2e 20 20 41 6e  o.** be set.  An
5f9b0 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
5f9c0 20 4b 45 59 20 69 73 20 75 73 65 64 20 61 73 20   KEY is used as 
5f9d0 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20 65 61  the rowid for ea
5f9e0 63 68 20 72 6f 77 20 6f 66 0a 2a 2a 20 74 68 65  ch row of.** the
5f9f0 20 74 61 62 6c 65 2e 20 20 49 66 20 61 20 74 61   table.  If a ta
5fa00 62 6c 65 20 68 61 73 20 6e 6f 20 49 4e 54 45 47  ble has no INTEG
5fa10 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20  ER PRIMARY KEY, 
5fa20 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d 20 72 6f  then a random ro
5fa30 77 69 64 0a 2a 2a 20 69 73 20 67 65 6e 65 72 61  wid.** is genera
5fa40 74 65 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77  ted for each row
5fa50 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20   of the table.  
5fa60 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79  TF_HasPrimaryKey
5fa70 20 69 73 20 73 65 74 20 69 66 0a 2a 2a 20 74 68   is set if.** th
5fa80 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 79 20  e table has any 
5fa90 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 49 4e 54  PRIMARY KEY, INT
5faa0 45 47 45 52 20 6f 72 20 6f 74 68 65 72 77 69 73  EGER or otherwis
5fab0 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 74  e..**.** Table.t
5fac0 6e 75 6d 20 69 73 20 74 68 65 20 70 61 67 65 20  num is the page 
5fad0 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72  number for the r
5fae0 6f 6f 74 20 42 54 72 65 65 20 70 61 67 65 20 6f  oot BTree page o
5faf0 66 20 74 68 65 20 74 61 62 6c 65 20 69 6e 20 74  f the table in t
5fb00 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66  he.** database f
5fb10 69 6c 65 2e 20 20 49 66 20 54 61 62 6c 65 2e 69  ile.  If Table.i
5fb20 44 62 20 69 73 20 74 68 65 20 69 6e 64 65 78 20  Db is the index 
5fb30 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
5fb40 74 61 62 6c 65 20 62 61 63 6b 65 6e 64 0a 2a 2a  table backend.**
5fb50 20 69 6e 20 73 71 6c 69 74 65 2e 61 44 62 5b 5d   in sqlite.aDb[]
5fb60 2e 20 20 30 20 69 73 20 66 6f 72 20 74 68 65 20  .  0 is for the 
5fb70 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 61 6e  main database an
5fb80 64 20 31 20 69 73 20 66 6f 72 20 74 68 65 20 66  d 1 is for the f
5fb90 69 6c 65 20 74 68 61 74 0a 2a 2a 20 68 6f 6c 64  ile that.** hold
5fba0 73 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  s temporary tabl
5fbb0 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73 2e 20  es and indices. 
5fbc0 20 49 66 20 54 46 5f 45 70 68 65 6d 65 72 61 6c   If TF_Ephemeral
5fbd0 20 69 73 20 73 65 74 0a 2a 2a 20 74 68 65 6e 20   is set.** then 
5fbe0 74 68 65 20 74 61 62 6c 65 20 69 73 20 73 74 6f  the table is sto
5fbf0 72 65 64 20 69 6e 20 61 20 66 69 6c 65 20 74 68  red in a file th
5fc00 61 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61  at is automatica
5fc10 6c 6c 79 20 64 65 6c 65 74 65 64 0a 2a 2a 20 77  lly deleted.** w
5fc20 68 65 6e 20 74 68 65 20 56 44 42 45 20 63 75 72  hen the VDBE cur
5fc30 73 6f 72 20 74 6f 20 74 68 65 20 74 61 62 6c 65  sor to the table
5fc40 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 49 6e 20   is closed.  In 
5fc50 74 68 69 73 20 63 61 73 65 20 54 61 62 6c 65 2e  this case Table.
5fc60 74 6e 75 6d 20 0a 2a 2a 20 72 65 66 65 72 73 20  tnum .** refers 
5fc70 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62  VDBE cursor numb
5fc80 65 72 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68  er that holds th
5fc90 65 20 74 61 62 6c 65 20 6f 70 65 6e 2c 20 6e 6f  e table open, no
5fca0 74 20 74 6f 20 74 68 65 20 72 6f 6f 74 0a 2a 2a  t to the root.**
5fcb0 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 54   page number.  T
5fcc0 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 73 20  ransient tables 
5fcd0 61 72 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64  are used to hold
5fce0 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20   the results of 
5fcf0 61 0a 2a 2a 20 73 75 62 2d 71 75 65 72 79 20 74  a.** sub-query t
5fd00 68 61 74 20 61 70 70 65 61 72 73 20 69 6e 73 74  hat appears inst
5fd10 65 61 64 20 6f 66 20 61 20 72 65 61 6c 20 74 61  ead of a real ta
5fd20 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20  ble name in the 
5fd30 46 52 4f 4d 20 63 6c 61 75 73 65 20 0a 2a 2a 20  FROM clause .** 
5fd40 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  of a SELECT stat
5fd50 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74  ement..*/.struct
5fd60 20 54 61 62 6c 65 20 7b 0a 20 20 73 71 6c 69 74   Table {.  sqlit
5fd70 65 33 20 2a 64 62 4d 65 6d 3b 20 20 20 20 20 20  e3 *dbMem;      
5fd80 2f 2a 20 44 42 20 63 6f 6e 6e 65 63 74 69 6f 6e  /* DB connection
5fd90 20 75 73 65 64 20 66 6f 72 20 6c 6f 6f 6b 61 73   used for lookas
5fda0 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e  ide allocations.
5fdb0 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d   */.  char *zNam
5fdc0 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  e;         /* Na
5fdd0 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
5fde0 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74  or view */.  int
5fdf0 20 69 50 4b 65 79 3b 20 20 20 20 20 20 20 20 20   iPKey;         
5fe00 20 20 2f 2a 20 49 66 20 6e 6f 74 20 6e 65 67 61    /* If not nega
5fe10 74 69 76 65 2c 20 75 73 65 20 61 43 6f 6c 5b 69  tive, use aCol[i
5fe20 50 4b 65 79 5d 20 61 73 20 74 68 65 20 70 72 69  PKey] as the pri
5fe30 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 20 20 69 6e  mary key */.  in
5fe40 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20  t nCol;         
5fe50 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
5fe60 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20  columns in this 
5fe70 74 61 62 6c 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d  table */.  Colum
5fe80 6e 20 2a 61 43 6f 6c 3b 20 20 20 20 20 20 20 20  n *aCol;        
5fe90 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  /* Information a
5fea0 62 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e  bout each column
5feb0 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e   */.  Index *pIn
5fec0 64 65 78 3b 20 20 20 20 20 20 20 2f 2a 20 4c 69  dex;       /* Li
5fed0 73 74 20 6f 66 20 53 51 4c 20 69 6e 64 65 78 65  st of SQL indexe
5fee0 73 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e  s on this table.
5fef0 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20   */.  int tnum; 
5ff00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f             /* Ro
5ff10 6f 74 20 42 54 72 65 65 20 6e 6f 64 65 20 66 6f  ot BTree node fo
5ff20 72 20 74 68 69 73 20 74 61 62 6c 65 20 28 73 65  r this table (se
5ff30 65 20 6e 6f 74 65 20 61 62 6f 76 65 29 20 2a 2f  e note above) */
5ff40 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  .  Select *pSele
5ff50 63 74 3b 20 20 20 20 20 2f 2a 20 4e 55 4c 4c 20  ct;     /* NULL 
5ff60 66 6f 72 20 74 61 62 6c 65 73 2e 20 20 50 6f 69  for tables.  Poi
5ff70 6e 74 73 20 74 6f 20 64 65 66 69 6e 69 74 69 6f  nts to definitio
5ff80 6e 20 69 66 20 61 20 76 69 65 77 2e 20 2a 2f 0a  n if a view. */.
5ff90 20 20 75 31 36 20 6e 52 65 66 3b 20 20 20 20 20    u16 nRef;     
5ffa0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
5ffb0 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20   of pointers to 
5ffc0 74 68 69 73 20 54 61 62 6c 65 20 2a 2f 0a 20 20  this Table */.  
5ffd0 75 38 20 74 61 62 46 6c 61 67 73 3b 20 20 20 20  u8 tabFlags;    
5ffe0 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20       /* Mask of 
5fff0 54 46 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20  TF_* values */. 
60000 20 75 38 20 6b 65 79 43 6f 6e 66 3b 20 20 20 20   u8 keyConf;    
60010 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f        /* What to
60020 20 64 6f 20 69 6e 20 63 61 73 65 20 6f 66 20 75   do in case of u
60030 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e 66 6c 69  niqueness confli
60040 63 74 20 6f 6e 20 69 50 4b 65 79 20 2a 2f 0a 20  ct on iPKey */. 
60050 20 46 4b 65 79 20 2a 70 46 4b 65 79 3b 20 20 20   FKey *pFKey;   
60060 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 20        /* Linked 
60070 6c 69 73 74 20 6f 66 20 61 6c 6c 20 66 6f 72 65  list of all fore
60080 69 67 6e 20 6b 65 79 73 20 69 6e 20 74 68 69 73  ign keys in this
60090 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72   table */.  char
600a0 20 2a 7a 43 6f 6c 41 66 66 3b 20 20 20 20 20 20   *zColAff;      
600b0 20 2f 2a 20 53 74 72 69 6e 67 20 64 65 66 69 6e   /* String defin
600c0 69 6e 67 20 74 68 65 20 61 66 66 69 6e 69 74 79  ing the affinity
600d0 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20   of each column 
600e0 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
600f0 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 45  E_OMIT_CHECK.  E
60100 78 70 72 20 2a 70 43 68 65 63 6b 3b 20 20 20 20  xpr *pCheck;    
60110 20 20 20 20 2f 2a 20 54 68 65 20 41 4e 44 20 6f      /* The AND o
60120 66 20 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73  f all CHECK cons
60130 74 72 61 69 6e 74 73 20 2a 2f 0a 23 65 6e 64 69  traints */.#endi
60140 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  f.#ifndef SQLITE
60150 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c 45  _OMIT_ALTERTABLE
60160 0a 20 20 69 6e 74 20 61 64 64 43 6f 6c 4f 66 66  .  int addColOff
60170 73 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65  set;    /* Offse
60180 74 20 69 6e 20 43 52 45 41 54 45 20 54 41 42 4c  t in CREATE TABL
60190 45 20 73 74 6d 74 20 74 6f 20 61 64 64 20 61 20  E stmt to add a 
601a0 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 65  new column */.#e
601b0 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c  ndif.#ifndef SQL
601c0 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
601d0 54 41 42 4c 45 0a 20 20 56 54 61 62 6c 65 20 2a  TABLE.  VTable *
601e0 70 56 54 61 62 6c 65 3b 20 20 20 20 20 2f 2a 20  pVTable;     /* 
601f0 4c 69 73 74 20 6f 66 20 56 54 61 62 6c 65 20 6f  List of VTable o
60200 62 6a 65 63 74 73 2e 20 2a 2f 0a 20 20 69 6e 74  bjects. */.  int
60210 20 6e 4d 6f 64 75 6c 65 41 72 67 3b 20 20 20 20   nModuleArg;    
60220 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
60230 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  rguments to the 
60240 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 68 61 72  module */.  char
60250 20 2a 2a 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 20   **azModuleArg; 
60260 20 2f 2a 20 54 65 78 74 20 6f 66 20 61 6c 6c 20   /* Text of all 
60270 6d 6f 64 75 6c 65 20 61 72 67 73 2e 20 5b 30 5d  module args. [0]
60280 20 69 73 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20   is module name 
60290 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 54 72 69 67  */.#endif.  Trig
602a0 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20  ger *pTrigger;  
602b0 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67   /* List of trig
602c0 67 65 72 73 20 73 74 6f 72 65 64 20 69 6e 20 70  gers stored in p
602d0 53 63 68 65 6d 61 20 2a 2f 0a 20 20 53 63 68 65  Schema */.  Sche
602e0 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20  ma *pSchema;    
602f0 20 2f 2a 20 53 63 68 65 6d 61 20 74 68 61 74 20   /* Schema that 
60300 63 6f 6e 74 61 69 6e 73 20 74 68 69 73 20 74 61  contains this ta
60310 62 6c 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ble */.  Table *
60320 70 4e 65 78 74 5a 6f 6d 62 69 65 3b 20 20 2f 2a  pNextZombie;  /*
60330 20 4e 65 78 74 20 6f 6e 20 74 68 65 20 50 61 72   Next on the Par
60340 73 65 2e 70 5a 6f 6d 62 69 65 54 61 62 20 6c 69  se.pZombieTab li
60350 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  st */.};../*.** 
60360 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66  Allowed values f
60370 6f 72 20 54 61 62 65 2e 74 61 62 46 6c 61 67 73  or Tabe.tabFlags
60380 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f  ..*/.#define TF_
60390 52 65 61 64 6f 6e 6c 79 20 20 20 20 20 20 20 20  Readonly        
603a0 30 78 30 31 20 20 20 20 2f 2a 20 52 65 61 64 2d  0x01    /* Read-
603b0 6f 6e 6c 79 20 73 79 73 74 65 6d 20 74 61 62 6c  only system tabl
603c0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f  e */.#define TF_
603d0 45 70 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20  Ephemeral       
603e0 30 78 30 32 20 20 20 20 2f 2a 20 41 6e 20 65 70  0x02    /* An ep
603f0 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 2a 2f  hemeral table */
60400 0a 23 64 65 66 69 6e 65 20 54 46 5f 48 61 73 50  .#define TF_HasP
60410 72 69 6d 61 72 79 4b 65 79 20 20 20 30 78 30 34  rimaryKey   0x04
60420 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 73      /* Table has
60430 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a   a primary key *
60440 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 41 75 74  /.#define TF_Aut
60450 6f 69 6e 63 72 65 6d 65 6e 74 20 20 20 30 78 30  oincrement   0x0
60460 38 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20  8    /* Integer 
60470 70 72 69 6d 61 72 79 20 6b 65 79 20 69 73 20 61  primary key is a
60480 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a  utoincrement */.
60490 23 64 65 66 69 6e 65 20 54 46 5f 56 69 72 74 75  #define TF_Virtu
604a0 61 6c 20 20 20 20 20 20 20 20 20 30 78 31 30 20  al         0x10 
604b0 20 20 20 2f 2a 20 49 73 20 61 20 76 69 72 74 75     /* Is a virtu
604c0 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66  al table */.#def
604d0 69 6e 65 20 54 46 5f 4e 65 65 64 4d 65 74 61 64  ine TF_NeedMetad
604e0 61 74 61 20 20 20 20 30 78 32 30 20 20 20 20 2f  ata    0x20    /
604f0 2a 20 61 43 6f 6c 5b 5d 2e 7a 54 79 70 65 20 61  * aCol[].zType a
60500 6e 64 20 61 43 6f 6c 5b 5d 2e 70 43 6f 6c 6c 20  nd aCol[].pColl 
60510 6d 69 73 73 69 6e 67 20 2a 2f 0a 0a 0a 0a 2f 2a  missing */..../*
60520 0a 2a 2a 20 54 65 73 74 20 74 6f 20 73 65 65 20  .** Test to see 
60530 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61  whether or not a
60540 20 74 61 62 6c 65 20 69 73 20 61 20 76 69 72 74   table is a virt
60550 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69 73  ual table.  This
60560 20 69 73 0a 2a 2a 20 64 6f 6e 65 20 61 73 20 61   is.** done as a
60570 20 6d 61 63 72 6f 20 73 6f 20 74 68 61 74 20 69   macro so that i
60580 74 20 77 69 6c 6c 20 62 65 20 6f 70 74 69 6d 69  t will be optimi
60590 7a 65 64 20 6f 75 74 20 77 68 65 6e 20 76 69 72  zed out when vir
605a0 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 73 75  tual.** table su
605b0 70 70 6f 72 74 20 69 73 20 6f 6d 69 74 74 65 64  pport is omitted
605c0 20 66 72 6f 6d 20 74 68 65 20 62 75 69 6c 64 2e   from the build.
605d0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
605e0 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
605f0 41 42 4c 45 0a 23 20 20 64 65 66 69 6e 65 20 49  ABLE.#  define I
60600 73 56 69 72 74 75 61 6c 28 58 29 20 20 20 20 20  sVirtual(X)     
60610 20 28 28 28 58 29 2d 3e 74 61 62 46 6c 61 67 73   (((X)->tabFlags
60620 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29 21 3d   & TF_Virtual)!=
60630 30 29 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48  0).#  define IsH
60640 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 28  iddenColumn(X) (
60650 28 58 29 2d 3e 69 73 48 69 64 64 65 6e 29 0a 23  (X)->isHidden).#
60660 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49  else.#  define I
60670 73 56 69 72 74 75 61 6c 28 58 29 20 20 20 20 20  sVirtual(X)     
60680 20 30 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48   0.#  define IsH
60690 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 30  iddenColumn(X) 0
606a0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45  .#endif../*.** E
606b0 61 63 68 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  ach foreign key 
606c0 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 61 6e  constraint is an
606d0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
606e0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
606f0 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 66 6f  ture..**.** A fo
60700 72 65 69 67 6e 20 6b 65 79 20 69 73 20 61 73 73  reign key is ass
60710 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 77 6f  ociated with two
60720 20 74 61 62 6c 65 73 2e 20 20 54 68 65 20 22 66   tables.  The "f
60730 72 6f 6d 22 20 74 61 62 6c 65 20 69 73 0a 2a 2a  rom" table is.**
60740 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20   the table that 
60750 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 52 45 46  contains the REF
60760 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 74  ERENCES clause t
60770 68 61 74 20 63 72 65 61 74 65 73 20 74 68 65 20  hat creates the 
60780 66 6f 72 65 69 67 6e 0a 2a 2a 20 6b 65 79 2e 20  foreign.** key. 
60790 20 54 68 65 20 22 74 6f 22 20 74 61 62 6c 65 20   The "to" table 
607a0 69 73 20 74 68 65 20 74 61 62 6c 65 20 74 68 61  is the table tha
607b0 74 20 69 73 20 6e 61 6d 65 64 20 69 6e 20 74 68  t is named in th
607c0 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61  e REFERENCES cla
607d0 75 73 65 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72  use..** Consider
607e0 20 74 68 69 73 20 65 78 61 6d 70 6c 65 3a 0a 2a   this example:.*
607f0 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20  *.**     CREATE 
60800 54 41 42 4c 45 20 65 78 31 28 0a 2a 2a 20 20 20  TABLE ex1(.**   
60810 20 20 20 20 61 20 49 4e 54 45 47 45 52 20 50 52      a INTEGER PR
60820 49 4d 41 52 59 20 4b 45 59 2c 0a 2a 2a 20 20 20  IMARY KEY,.**   
60830 20 20 20 20 62 20 49 4e 54 45 47 45 52 20 43 4f      b INTEGER CO
60840 4e 53 54 52 41 49 4e 54 20 66 6b 31 20 52 45 46  NSTRAINT fk1 REF
60850 45 52 45 4e 43 45 53 20 65 78 32 28 78 29 0a 2a  ERENCES ex2(x).*
60860 2a 20 20 20 20 20 29 3b 0a 2a 2a 0a 2a 2a 20 46  *     );.**.** F
60870 6f 72 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 22  or foreign key "
60880 66 6b 31 22 2c 20 74 68 65 20 66 72 6f 6d 2d 74  fk1", the from-t
60890 61 62 6c 65 20 69 73 20 22 65 78 31 22 20 61 6e  able is "ex1" an
608a0 64 20 74 68 65 20 74 6f 2d 74 61 62 6c 65 20 69  d the to-table i
608b0 73 20 22 65 78 32 22 2e 0a 2a 2a 0a 2a 2a 20 45  s "ex2"..**.** E
608c0 61 63 68 20 52 45 46 45 52 45 4e 43 45 53 20 63  ach REFERENCES c
608d0 6c 61 75 73 65 20 67 65 6e 65 72 61 74 65 73 20  lause generates 
608e0 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
608f0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
60900 75 63 74 75 72 65 0a 2a 2a 20 77 68 69 63 68 20  ucture.** which 
60910 69 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 74  is attached to t
60920 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65 2e 20 20  he from-table.  
60930 54 68 65 20 74 6f 2d 74 61 62 6c 65 20 6e 65 65  The to-table nee
60940 64 20 6e 6f 74 20 65 78 69 73 74 20 77 68 65 6e  d not exist when
60950 0a 2a 2a 20 74 68 65 20 66 72 6f 6d 2d 74 61 62  .** the from-tab
60960 6c 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 20  le is created.  
60970 54 68 65 20 65 78 69 73 74 65 6e 63 65 20 6f 66  The existence of
60980 20 74 68 65 20 74 6f 2d 74 61 62 6c 65 20 69 73   the to-table is
60990 20 6e 6f 74 20 63 68 65 63 6b 65 64 2e 0a 2a 2f   not checked..*/
609a0 0a 73 74 72 75 63 74 20 46 4b 65 79 20 7b 0a 20  .struct FKey {. 
609b0 20 54 61 62 6c 65 20 2a 70 46 72 6f 6d 3b 20 20   Table *pFrom;  
609c0 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
609d0 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
609e0 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61  e REFERENCES cla
609f0 75 73 65 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70  use */.  FKey *p
60a00 4e 65 78 74 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65  NextFrom;  /* Ne
60a10 78 74 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69  xt foreign key i
60a20 6e 20 70 46 72 6f 6d 20 2a 2f 0a 20 20 63 68 61  n pFrom */.  cha
60a30 72 20 2a 7a 54 6f 3b 20 20 20 20 20 20 20 20 2f  r *zTo;        /
60a40 2a 20 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20  * Name of table 
60a50 74 68 61 74 20 74 68 65 20 6b 65 79 20 70 6f 69  that the key poi
60a60 6e 74 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20  nts to */.  int 
60a70 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a  nCol;         /*
60a80 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   Number of colum
60a90 6e 73 20 69 6e 20 74 68 69 73 20 6b 65 79 20 2a  ns in this key *
60aa0 2f 0a 20 20 75 38 20 69 73 44 65 66 65 72 72 65  /.  u8 isDeferre
60ab0 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  d;    /* True if
60ac0 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63   constraint chec
60ad0 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72 65 64  king is deferred
60ae0 20 74 69 6c 6c 20 43 4f 4d 4d 49 54 20 2a 2f 0a   till COMMIT */.
60af0 20 20 75 38 20 75 70 64 61 74 65 43 6f 6e 66 3b    u8 updateConf;
60b00 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65      /* How to re
60b10 73 6f 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20  solve conflicts 
60b20 74 68 61 74 20 6f 63 63 75 72 20 6f 6e 20 55 50  that occur on UP
60b30 44 41 54 45 20 2a 2f 0a 20 20 75 38 20 64 65 6c  DATE */.  u8 del
60b40 65 74 65 43 6f 6e 66 3b 20 20 20 20 2f 2a 20 48  eteConf;    /* H
60b50 6f 77 20 74 6f 20 72 65 73 6f 6c 76 65 20 63 6f  ow to resolve co
60b60 6e 66 6c 69 63 74 73 20 74 68 61 74 20 6f 63 63  nflicts that occ
60b70 75 72 20 6f 6e 20 44 45 4c 45 54 45 20 2a 2f 0a  ur on DELETE */.
60b80 20 20 75 38 20 69 6e 73 65 72 74 43 6f 6e 66 3b    u8 insertConf;
60b90 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65      /* How to re
60ba0 73 6f 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20  solve conflicts 
60bb0 74 68 61 74 20 6f 63 63 75 72 20 6f 6e 20 49 4e  that occur on IN
60bc0 53 45 52 54 20 2a 2f 0a 20 20 73 74 72 75 63 74  SERT */.  struct
60bd0 20 73 43 6f 6c 4d 61 70 20 7b 20 20 2f 2a 20 4d   sColMap {  /* M
60be0 61 70 70 69 6e 67 20 6f 66 20 63 6f 6c 75 6d 6e  apping of column
60bf0 73 20 69 6e 20 70 46 72 6f 6d 20 74 6f 20 63 6f  s in pFrom to co
60c00 6c 75 6d 6e 73 20 69 6e 20 7a 54 6f 20 2a 2f 0a  lumns in zTo */.
60c10 20 20 20 20 69 6e 74 20 69 46 72 6f 6d 3b 20 20      int iFrom;  
60c20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
60c30 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 46 72  of column in pFr
60c40 6f 6d 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a  om */.    char *
60c50 7a 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20  zCol;        /* 
60c60 4e 61 6d 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 69  Name of column i
60c70 6e 20 7a 54 6f 2e 20 20 49 66 20 30 20 75 73 65  n zTo.  If 0 use
60c80 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a   PRIMARY KEY */.
60c90 20 20 7d 20 61 43 6f 6c 5b 31 5d 3b 20 20 20 20    } aCol[1];    
60ca0 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79      /* One entry
60cb0 20 66 6f 72 20 65 61 63 68 20 6f 66 20 6e 43 6f   for each of nCo
60cc0 6c 20 63 6f 6c 75 6d 6e 20 73 20 2a 2f 0a 7d 3b  l column s */.};
60cd0 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 73  ../*.** SQLite s
60ce0 75 70 70 6f 72 74 73 20 6d 61 6e 79 20 64 69 66  upports many dif
60cf0 66 65 72 65 6e 74 20 77 61 79 73 20 74 6f 20 72  ferent ways to r
60d00 65 73 6f 6c 76 65 20 61 20 63 6f 6e 73 74 72 61  esolve a constra
60d10 69 6e 74 0a 2a 2a 20 65 72 72 6f 72 2e 20 20 52  int.** error.  R
60d20 4f 4c 4c 42 41 43 4b 20 70 72 6f 63 65 73 73 69  OLLBACK processi
60d30 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20  ng means that a 
60d40 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61  constraint viola
60d50 74 69 6f 6e 0a 2a 2a 20 63 61 75 73 65 73 20 74  tion.** causes t
60d60 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 6e 20  he operation in 
60d70 70 72 6f 63 65 73 73 20 74 6f 20 66 61 69 6c 20  process to fail 
60d80 61 6e 64 20 66 6f 72 20 74 68 65 20 63 75 72 72  and for the curr
60d90 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a  ent transaction.
60da0 2a 2a 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20  ** to be rolled 
60db0 62 61 63 6b 2e 20 20 41 42 4f 52 54 20 70 72 6f  back.  ABORT pro
60dc0 63 65 73 73 69 6e 67 20 6d 65 61 6e 73 20 74 68  cessing means th
60dd0 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 6e 20 70  e operation in p
60de0 72 6f 63 65 73 73 0a 2a 2a 20 66 61 69 6c 73 20  rocess.** fails 
60df0 61 6e 64 20 61 6e 79 20 70 72 69 6f 72 20 63 68  and any prior ch
60e00 61 6e 67 65 73 20 66 72 6f 6d 20 74 68 61 74 20  anges from that 
60e10 6f 6e 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 72  one operation ar
60e20 65 20 62 61 63 6b 65 64 20 6f 75 74 2c 0a 2a 2a  e backed out,.**
60e30 20 62 75 74 20 74 68 65 20 74 72 61 6e 73 61 63   but the transac
60e40 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 6f 6c 6c  tion is not roll
60e50 65 64 20 62 61 63 6b 2e 20 20 46 41 49 4c 20 70  ed back.  FAIL p
60e60 72 6f 63 65 73 73 69 6e 67 20 6d 65 61 6e 73 20  rocessing means 
60e70 74 68 61 74 0a 2a 2a 20 74 68 65 20 6f 70 65 72  that.** the oper
60e80 61 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73  ation in progres
60e90 73 20 73 74 6f 70 73 20 61 6e 64 20 72 65 74 75  s stops and retu
60ea0 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  rns an error cod
60eb0 65 2e 20 20 42 75 74 20 70 72 69 6f 72 0a 2a 2a  e.  But prior.**
60ec0 20 63 68 61 6e 67 65 73 20 64 75 65 20 74 6f 20   changes due to 
60ed0 74 68 65 20 73 61 6d 65 20 6f 70 65 72 61 74 69  the same operati
60ee0 6f 6e 20 61 72 65 20 6e 6f 74 20 62 61 63 6b 65  on are not backe
60ef0 64 20 6f 75 74 20 61 6e 64 20 6e 6f 20 72 6f 6c  d out and no rol
60f00 6c 62 61 63 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e  lback.** occurs.
60f10 20 20 49 47 4e 4f 52 45 20 6d 65 61 6e 73 20 74    IGNORE means t
60f20 68 61 74 20 74 68 65 20 70 61 72 74 69 63 75 6c  hat the particul
60f30 61 72 20 72 6f 77 20 74 68 61 74 20 63 61 75 73  ar row that caus
60f40 65 64 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  ed the constrain
60f50 74 0a 2a 2a 20 65 72 72 6f 72 20 69 73 20 6e 6f  t.** error is no
60f60 74 20 69 6e 73 65 72 74 65 64 20 6f 72 20 75 70  t inserted or up
60f70 64 61 74 65 64 2e 20 20 50 72 6f 63 65 73 73 69  dated.  Processi
60f80 6e 67 20 63 6f 6e 74 69 6e 75 65 73 20 61 6e 64  ng continues and
60f90 20 6e 6f 20 65 72 72 6f 72 0a 2a 2a 20 69 73 20   no error.** is 
60fa0 72 65 74 75 72 6e 65 64 2e 20 20 52 45 50 4c 41  returned.  REPLA
60fb0 43 45 20 6d 65 61 6e 73 20 74 68 61 74 20 70 72  CE means that pr
60fc0 65 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 61  eexisting databa
60fd0 73 65 20 72 6f 77 73 20 74 68 61 74 20 63 61 75  se rows that cau
60fe0 73 65 64 0a 2a 2a 20 61 20 55 4e 49 51 55 45 20  sed.** a UNIQUE 
60ff0 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61  constraint viola
61000 74 69 6f 6e 20 61 72 65 20 72 65 6d 6f 76 65 64  tion are removed
61010 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e 65 77   so that the new
61020 20 69 6e 73 65 72 74 20 6f 72 0a 2a 2a 20 75 70   insert or.** up
61030 64 61 74 65 20 63 61 6e 20 70 72 6f 63 65 65 64  date can proceed
61040 2e 20 20 50 72 6f 63 65 73 73 69 6e 67 20 63 6f  .  Processing co
61050 6e 74 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65  ntinues and no e
61060 72 72 6f 72 20 69 73 20 72 65 70 6f 72 74 65 64  rror is reported
61070 2e 0a 2a 2a 0a 2a 2a 20 52 45 53 54 52 49 43 54  ..**.** RESTRICT
61080 2c 20 53 45 54 4e 55 4c 4c 2c 20 61 6e 64 20 43  , SETNULL, and C
61090 41 53 43 41 44 45 20 61 63 74 69 6f 6e 73 20 61  ASCADE actions a
610a0 70 70 6c 79 20 6f 6e 6c 79 20 74 6f 20 66 6f 72  pply only to for
610b0 65 69 67 6e 20 6b 65 79 73 2e 0a 2a 2a 20 52 45  eign keys..** RE
610c0 53 54 52 49 43 54 20 69 73 20 74 68 65 20 73 61  STRICT is the sa
610d0 6d 65 20 61 73 20 41 42 4f 52 54 20 66 6f 72 20  me as ABORT for 
610e0 49 4d 4d 45 44 49 41 54 45 20 66 6f 72 65 69 67  IMMEDIATE foreig
610f0 6e 20 6b 65 79 73 20 61 6e 64 20 74 68 65 0a 2a  n keys and the.*
61100 2a 20 73 61 6d 65 20 61 73 20 52 4f 4c 4c 42 41  * same as ROLLBA
61110 43 4b 20 66 6f 72 20 44 45 46 45 52 52 45 44 20  CK for DEFERRED 
61120 6b 65 79 73 2e 20 20 53 45 54 4e 55 4c 4c 20 6d  keys.  SETNULL m
61130 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 66 6f  eans that the fo
61140 72 65 69 67 6e 0a 2a 2a 20 6b 65 79 20 69 73 20  reign.** key is 
61150 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 20 43 41  set to NULL.  CA
61160 53 43 41 44 45 20 6d 65 61 6e 73 20 74 68 61 74  SCADE means that
61170 20 61 20 44 45 4c 45 54 45 20 6f 72 20 55 50 44   a DELETE or UPD
61180 41 54 45 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65  ATE of the.** re
61190 66 65 72 65 6e 63 65 64 20 74 61 62 6c 65 20 72  ferenced table r
611a0 6f 77 20 69 73 20 70 72 6f 70 61 67 61 74 65 64  ow is propagated
611b0 20 69 6e 74 6f 20 74 68 65 20 72 6f 77 20 74 68   into the row th
611c0 61 74 20 68 6f 6c 64 73 20 74 68 65 0a 2a 2a 20  at holds the.** 
611d0 66 6f 72 65 69 67 6e 20 6b 65 79 2e 0a 2a 2a 20  foreign key..** 
611e0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
611f0 67 20 73 79 6d 62 6f 6c 69 63 20 76 61 6c 75 65  g symbolic value
61200 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 72 65  s are used to re
61210 63 6f 72 64 20 77 68 69 63 68 20 74 79 70 65 0a  cord which type.
61220 2a 2a 20 6f 66 20 61 63 74 69 6f 6e 20 74 6f 20  ** of action to 
61230 74 61 6b 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  take..*/.#define
61240 20 4f 45 5f 4e 6f 6e 65 20 20 20 20 20 30 20 20   OE_None     0  
61250 20 2f 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20   /* There is no 
61260 63 6f 6e 73 74 72 61 69 6e 74 20 74 6f 20 63 68  constraint to ch
61270 65 63 6b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  eck */.#define O
61280 45 5f 52 6f 6c 6c 62 61 63 6b 20 31 20 20 20 2f  E_Rollback 1   /
61290 2a 20 46 61 69 6c 20 74 68 65 20 6f 70 65 72 61  * Fail the opera
612a0 74 69 6f 6e 20 61 6e 64 20 72 6f 6c 6c 62 61 63  tion and rollbac
612b0 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  k the transactio
612c0 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f  n */.#define OE_
612d0 41 62 6f 72 74 20 20 20 20 32 20 20 20 2f 2a 20  Abort    2   /* 
612e0 42 61 63 6b 20 6f 75 74 20 63 68 61 6e 67 65 73  Back out changes
612f0 20 62 75 74 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62   but do no rollb
61300 61 63 6b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ack transaction 
61310 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 46 61  */.#define OE_Fa
61320 69 6c 20 20 20 20 20 33 20 20 20 2f 2a 20 53 74  il     3   /* St
61330 6f 70 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  op the operation
61340 20 62 75 74 20 6c 65 61 76 65 20 61 6c 6c 20 70   but leave all p
61350 72 69 6f 72 20 63 68 61 6e 67 65 73 20 2a 2f 0a  rior changes */.
61360 23 64 65 66 69 6e 65 20 4f 45 5f 49 67 6e 6f 72  #define OE_Ignor
61370 65 20 20 20 34 20 20 20 2f 2a 20 49 67 6e 6f 72  e   4   /* Ignor
61380 65 20 74 68 65 20 65 72 72 6f 72 2e 20 44 6f 20  e the error. Do 
61390 6e 6f 74 20 64 6f 20 74 68 65 20 49 4e 53 45 52  not do the INSER
613a0 54 20 6f 72 20 55 50 44 41 54 45 20 2a 2f 0a 23  T or UPDATE */.#
613b0 64 65 66 69 6e 65 20 4f 45 5f 52 65 70 6c 61 63  define OE_Replac
613c0 65 20 20 35 20 20 20 2f 2a 20 44 65 6c 65 74 65  e  5   /* Delete
613d0 20 65 78 69 73 74 69 6e 67 20 72 65 63 6f 72 64   existing record
613e0 2c 20 74 68 65 6e 20 64 6f 20 49 4e 53 45 52 54  , then do INSERT
613f0 20 6f 72 20 55 50 44 41 54 45 20 2a 2f 0a 0a 23   or UPDATE */..#
61400 64 65 66 69 6e 65 20 4f 45 5f 52 65 73 74 72 69  define OE_Restri
61410 63 74 20 36 20 20 20 2f 2a 20 4f 45 5f 41 62 6f  ct 6   /* OE_Abo
61420 72 74 20 66 6f 72 20 49 4d 4d 45 44 49 41 54 45  rt for IMMEDIATE
61430 2c 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 66 6f  , OE_Rollback fo
61440 72 20 44 45 46 45 52 52 45 44 20 2a 2f 0a 23 64  r DEFERRED */.#d
61450 65 66 69 6e 65 20 4f 45 5f 53 65 74 4e 75 6c 6c  efine OE_SetNull
61460 20 20 37 20 20 20 2f 2a 20 53 65 74 20 74 68 65    7   /* Set the
61470 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 76 61 6c   foreign key val
61480 75 65 20 74 6f 20 4e 55 4c 4c 20 2a 2f 0a 23 64  ue to NULL */.#d
61490 65 66 69 6e 65 20 4f 45 5f 53 65 74 44 66 6c 74  efine OE_SetDflt
614a0 20 20 38 20 20 20 2f 2a 20 53 65 74 20 74 68 65    8   /* Set the
614b0 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 76 61 6c   foreign key val
614c0 75 65 20 74 6f 20 69 74 73 20 64 65 66 61 75 6c  ue to its defaul
614d0 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f  t */.#define OE_
614e0 43 61 73 63 61 64 65 20 20 39 20 20 20 2f 2a 20  Cascade  9   /* 
614f0 43 61 73 63 61 64 65 20 74 68 65 20 63 68 61 6e  Cascade the chan
61500 67 65 73 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20  ges */..#define 
61510 4f 45 5f 44 65 66 61 75 6c 74 20 20 39 39 20 20  OE_Default  99  
61520 2f 2a 20 44 6f 20 77 68 61 74 65 76 65 72 20 74  /* Do whatever t
61530 68 65 20 64 65 66 61 75 6c 74 20 61 63 74 69 6f  he default actio
61540 6e 20 69 73 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  n is */.../*.** 
61550 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
61560 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
61570 75 63 74 75 72 65 20 69 73 20 70 61 73 73 65 64  ucture is passed
61580 20 61 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a   as the first.**
61590 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c   argument to sql
615a0 69 74 65 33 56 64 62 65 4b 65 79 43 6f 6d 70 61  ite3VdbeKeyCompa
615b0 72 65 20 61 6e 64 20 69 73 20 75 73 65 64 20 74  re and is used t
615c0 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 0a 2a  o control the .*
615d0 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20  * comparison of 
615e0 74 68 65 20 74 77 6f 20 69 6e 64 65 78 20 6b 65  the two index ke
615f0 79 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4b 65  ys..*/.struct Ke
61600 79 49 6e 66 6f 20 7b 0a 20 20 73 71 6c 69 74 65  yInfo {.  sqlite
61610 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 2f 2a  3 *db;        /*
61620 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
61630 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38  nnection */.  u8
61640 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20   enc;           
61650 20 20 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69    /* Text encodi
61660 6e 67 20 2d 20 6f 6e 65 20 6f 66 20 74 68 65 20  ng - one of the 
61670 54 45 58 54 5f 55 74 66 2a 20 76 61 6c 75 65 73  TEXT_Utf* values
61680 20 2a 2f 0a 20 20 75 31 36 20 6e 46 69 65 6c 64   */.  u16 nField
61690 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ;         /* Num
616a0 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
616b0 6e 20 61 43 6f 6c 6c 5b 5d 20 2a 2f 0a 20 20 75  n aColl[] */.  u
616c0 38 20 2a 61 53 6f 72 74 4f 72 64 65 72 3b 20 20  8 *aSortOrder;  
616d0 20 20 20 2f 2a 20 49 66 20 64 65 66 69 6e 65 64     /* If defined
616e0 20 61 6e 20 61 53 6f 72 74 4f 72 64 65 72 5b 69   an aSortOrder[i
616f0 5d 20 69 73 20 74 72 75 65 2c 20 73 6f 72 74 20  ] is true, sort 
61700 44 45 53 43 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65  DESC */.  CollSe
61710 71 20 2a 61 43 6f 6c 6c 5b 31 5d 3b 20 20 2f 2a  q *aColl[1];  /*
61720 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   Collating seque
61730 6e 63 65 20 66 6f 72 20 65 61 63 68 20 74 65 72  nce for each ter
61740 6d 20 6f 66 20 74 68 65 20 6b 65 79 20 2a 2f 0a  m of the key */.
61750 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  };../*.** An ins
61760 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
61770 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
61780 20 68 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69   holds informati
61790 6f 6e 20 61 62 6f 75 74 20 61 0a 2a 2a 20 73 69  on about a.** si
617a0 6e 67 6c 65 20 69 6e 64 65 78 20 72 65 63 6f 72  ngle index recor
617b0 64 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61  d that has alrea
617c0 64 79 20 62 65 65 6e 20 70 61 72 73 65 64 20 6f  dy been parsed o
617d0 75 74 20 69 6e 74 6f 20 69 6e 64 69 76 69 64 75  ut into individu
617e0 61 6c 0a 2a 2a 20 76 61 6c 75 65 73 2e 0a 2a 2a  al.** values..**
617f0 0a 2a 2a 20 41 20 72 65 63 6f 72 64 20 69 73 20  .** A record is 
61800 61 6e 20 6f 62 6a 65 63 74 20 74 68 61 74 20 63  an object that c
61810 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d  ontains one or m
61820 6f 72 65 20 66 69 65 6c 64 73 20 6f 66 20 64 61  ore fields of da
61830 74 61 2e 0a 2a 2a 20 52 65 63 6f 72 64 73 20 61  ta..** Records a
61840 72 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  re used to store
61850 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
61860 61 20 74 61 62 6c 65 20 72 6f 77 20 61 6e 64 20  a table row and 
61870 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 68 65 20  to store.** the 
61880 6b 65 79 20 6f 66 20 61 6e 20 69 6e 64 65 78 2e  key of an index.
61890 20 20 41 20 62 6c 6f 62 20 65 6e 63 6f 64 69 6e    A blob encodin
618a0 67 20 6f 66 20 61 20 72 65 63 6f 72 64 20 69 73  g of a record is
618b0 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a 20 74   created by.** t
618c0 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  he OP_MakeRecord
618d0 20 6f 70 63 6f 64 65 20 6f 66 20 74 68 65 20 56   opcode of the V
618e0 44 42 45 20 61 6e 64 20 69 73 20 64 69 73 61 73  DBE and is disas
618f0 73 65 6d 62 6c 65 64 20 62 79 20 74 68 65 0a 2a  sembled by the.*
61900 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f  * OP_Column opco
61910 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73  de..**.** This s
61920 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 61  tructure holds a
61930 20 72 65 63 6f 72 64 20 74 68 61 74 20 68 61 73   record that has
61940 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 64 69   already been di
61950 73 61 73 73 65 6d 62 6c 65 64 0a 2a 2a 20 69 6e  sassembled.** in
61960 74 6f 20 69 74 73 20 63 6f 6e 73 74 69 74 75 65  to its constitue
61970 6e 74 20 66 69 65 6c 64 73 2e 0a 2a 2f 0a 73 74  nt fields..*/.st
61980 72 75 63 74 20 55 6e 70 61 63 6b 65 64 52 65 63  ruct UnpackedRec
61990 6f 72 64 20 7b 0a 20 20 4b 65 79 49 6e 66 6f 20  ord {.  KeyInfo 
619a0 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 2f 2a 20 43  *pKeyInfo;  /* C
619b0 6f 6c 6c 61 74 69 6f 6e 20 61 6e 64 20 73 6f 72  ollation and sor
619c0 74 2d 6f 72 64 65 72 20 69 6e 66 6f 72 6d 61 74  t-order informat
619d0 69 6f 6e 20 2a 2f 0a 20 20 75 31 36 20 6e 46 69  ion */.  u16 nFi
619e0 65 6c 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  eld;         /* 
619f0 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
61a00 73 20 69 6e 20 61 70 4d 65 6d 5b 5d 20 2a 2f 0a  s in apMem[] */.
61a10 20 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20    u16 flags;    
61a20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e        /* Boolean
61a30 20 73 65 74 74 69 6e 67 73 2e 20 20 55 4e 50 41   settings.  UNPA
61a40 43 4b 45 44 5f 2e 2e 2e 20 62 65 6c 6f 77 20 2a  CKED_... below *
61a50 2f 0a 20 20 69 36 34 20 72 6f 77 69 64 3b 20 20  /.  i64 rowid;  
61a60 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20          /* Used 
61a70 62 79 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46  by UNPACKED_PREF
61a80 49 58 5f 53 45 41 52 43 48 20 2a 2f 0a 20 20 4d  IX_SEARCH */.  M
61a90 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20  em *aMem;       
61aa0 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 2a 2f 0a     /* Values */.
61ab0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65  };../*.** Allowe
61ac0 64 20 76 61 6c 75 65 73 20 6f 66 20 55 6e 70 61  d values of Unpa
61ad0 63 6b 65 64 52 65 63 6f 72 64 2e 66 6c 61 67 73  ckedRecord.flags
61ae0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41  .*/.#define UNPA
61af0 43 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 20  CKED_NEED_FREE  
61b00 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4d 65     0x0001  /* Me
61b10 6d 6f 72 79 20 69 73 20 66 72 6f 6d 20 73 71 6c  mory is from sql
61b20 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a  ite3Malloc() */.
61b30 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44  #define UNPACKED
61b40 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 20 30  _NEED_DESTROY  0
61b50 78 30 30 30 32 20 20 2f 2a 20 61 70 4d 65 6d 5b  x0002  /* apMem[
61b60 5d 73 20 73 68 6f 75 6c 64 20 61 6c 6c 20 62 65  ]s should all be
61b70 20 64 65 73 74 72 6f 79 65 64 20 2a 2f 0a 23 64   destroyed */.#d
61b80 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 49  efine UNPACKED_I
61b90 47 4e 4f 52 45 5f 52 4f 57 49 44 20 20 30 78 30  GNORE_ROWID  0x0
61ba0 30 30 34 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74  004  /* Ignore t
61bb0 72 61 69 6c 69 6e 67 20 72 6f 77 69 64 20 6f 6e  railing rowid on
61bc0 20 6b 65 79 31 20 2a 2f 0a 23 64 65 66 69 6e 65   key1 */.#define
61bd0 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45   UNPACKED_INCRKE
61be0 59 20 20 20 20 20 20 20 30 78 30 30 30 38 20 20  Y       0x0008  
61bf0 2f 2a 20 4d 61 6b 65 20 74 68 69 73 20 6b 65 79  /* Make this key
61c00 20 61 6e 20 65 70 73 69 6c 6f 6e 20 6c 61 72 67   an epsilon larg
61c10 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e  er */.#define UN
61c20 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41  PACKED_PREFIX_MA
61c30 54 43 48 20 20 30 78 30 30 31 30 20 20 2f 2a 20  TCH  0x0010  /* 
61c40 41 20 70 72 65 66 69 78 20 6d 61 74 63 68 20 69  A prefix match i
61c50 73 20 63 6f 6e 73 69 64 65 72 65 64 20 4f 4b 20  s considered OK 
61c60 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43  */.#define UNPAC
61c70 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52 43  KED_PREFIX_SEARC
61c80 48 20 30 78 30 30 32 30 20 20 2f 2a 20 41 20 70  H 0x0020  /* A p
61c90 72 65 66 69 78 20 6d 61 74 63 68 20 69 73 20 63  refix match is c
61ca0 6f 6e 73 69 64 65 72 65 64 20 4f 4b 20 2a 2f 0a  onsidered OK */.
61cb0 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20  ./*.** Each SQL 
61cc0 69 6e 64 65 78 20 69 73 20 72 65 70 72 65 73 65  index is represe
61cd0 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 62  nted in memory b
61ce0 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65  y an.** instance
61cf0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
61d00 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  g structure..**.
61d10 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 6f  ** The columns o
61d20 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74  f the table that
61d30 20 61 72 65 20 74 6f 20 62 65 20 69 6e 64 65 78   are to be index
61d40 65 64 20 61 72 65 20 64 65 73 63 72 69 62 65 64  ed are described
61d50 0a 2a 2a 20 62 79 20 74 68 65 20 61 69 43 6f 6c  .** by the aiCol
61d60 75 6d 6e 5b 5d 20 66 69 65 6c 64 20 6f 66 20 74  umn[] field of t
61d70 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20  his structure.  
61d80 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 73 75 70  For example, sup
61d90 70 6f 73 65 0a 2a 2a 20 77 65 20 68 61 76 65 20  pose.** we have 
61da0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61  the following ta
61db0 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 3a 0a 2a  ble and index:.*
61dc0 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20  *.**     CREATE 
61dd0 54 41 42 4c 45 20 45 78 31 28 63 31 20 69 6e 74  TABLE Ex1(c1 int
61de0 2c 20 63 32 20 69 6e 74 2c 20 63 33 20 74 65 78  , c2 int, c3 tex
61df0 74 29 3b 0a 2a 2a 20 20 20 20 20 43 52 45 41 54  t);.**     CREAT
61e00 45 20 49 4e 44 45 58 20 45 78 32 20 4f 4e 20 45  E INDEX Ex2 ON E
61e10 78 31 28 63 33 2c 63 31 29 3b 0a 2a 2a 0a 2a 2a  x1(c3,c1);.**.**
61e20 20 49 6e 20 74 68 65 20 54 61 62 6c 65 20 73 74   In the Table st
61e30 72 75 63 74 75 72 65 20 64 65 73 63 72 69 62 69  ructure describi
61e40 6e 67 20 45 78 31 2c 20 6e 43 6f 6c 3d 3d 33 20  ng Ex1, nCol==3 
61e50 62 65 63 61 75 73 65 20 74 68 65 72 65 20 61 72  because there ar
61e60 65 0a 2a 2a 20 74 68 72 65 65 20 63 6f 6c 75 6d  e.** three colum
61e70 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e  ns in the table.
61e80 20 20 49 6e 20 74 68 65 20 49 6e 64 65 78 20 73    In the Index s
61e90 74 72 75 63 74 75 72 65 20 64 65 73 63 72 69 62  tructure describ
61ea0 69 6e 67 0a 2a 2a 20 45 78 32 2c 20 6e 43 6f 6c  ing.** Ex2, nCol
61eb0 75 6d 6e 3d 3d 32 20 73 69 6e 63 65 20 32 20 6f  umn==2 since 2 o
61ec0 66 20 74 68 65 20 33 20 63 6f 6c 75 6d 6e 73 20  f the 3 columns 
61ed0 6f 66 20 45 78 31 20 61 72 65 20 69 6e 64 65 78  of Ex1 are index
61ee0 65 64 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65  ed..** The value
61ef0 20 6f 66 20 61 69 43 6f 6c 75 6d 6e 20 69 73 20   of aiColumn is 
61f00 7b 32 2c 20 30 7d 2e 20 20 61 69 43 6f 6c 75 6d  {2, 0}.  aiColum
61f10 6e 5b 30 5d 3d 3d 32 20 62 65 63 61 75 73 65 20  n[0]==2 because 
61f20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20 63 6f  the .** first co
61f30 6c 75 6d 6e 20 74 6f 20 62 65 20 69 6e 64 65 78  lumn to be index
61f40 65 64 20 28 63 33 29 20 68 61 73 20 61 6e 20 69  ed (c3) has an i
61f50 6e 64 65 78 20 6f 66 20 32 20 69 6e 20 45 78 31  ndex of 2 in Ex1
61f60 2e 61 43 6f 6c 5b 5d 2e 0a 2a 2a 20 54 68 65 20  .aCol[]..** The 
61f70 73 65 63 6f 6e 64 20 63 6f 6c 75 6d 6e 20 74 6f  second column to
61f80 20 62 65 20 69 6e 64 65 78 65 64 20 28 63 31 29   be indexed (c1)
61f90 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66   has an index of
61fa0 20 30 20 69 6e 0a 2a 2a 20 45 78 31 2e 61 43 6f   0 in.** Ex1.aCo
61fb0 6c 5b 5d 2c 20 68 65 6e 63 65 20 45 78 32 2e 61  l[], hence Ex2.a
61fc0 69 43 6f 6c 75 6d 6e 5b 31 5d 3d 3d 30 2e 0a 2a  iColumn[1]==0..*
61fd0 2a 0a 2a 2a 20 54 68 65 20 49 6e 64 65 78 2e 6f  *.** The Index.o
61fe0 6e 45 72 72 6f 72 20 66 69 65 6c 64 20 64 65 74  nError field det
61ff0 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65 72 20  ermines whether 
62000 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e 64 65 78  or not the index
62010 65 64 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 6d 75  ed columns.** mu
62020 73 74 20 62 65 20 75 6e 69 71 75 65 20 61 6e 64  st be unique and
62030 20 77 68 61 74 20 74 6f 20 64 6f 20 69 66 20 74   what to do if t
62040 68 65 79 20 61 72 65 20 6e 6f 74 2e 20 20 57 68  hey are not.  Wh
62050 65 6e 20 49 6e 64 65 78 2e 6f 6e 45 72 72 6f 72  en Index.onError
62060 3d 4f 45 5f 4e 6f 6e 65 2c 0a 2a 2a 20 69 74 20  =OE_None,.** it 
62070 6d 65 61 6e 73 20 74 68 69 73 20 69 73 20 6e 6f  means this is no
62080 74 20 61 20 75 6e 69 71 75 65 20 69 6e 64 65 78  t a unique index
62090 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69 74 20  .  Otherwise it 
620a0 69 73 20 61 20 75 6e 69 71 75 65 20 69 6e 64 65  is a unique inde
620b0 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 76 61 6c  x.** and the val
620c0 75 65 20 6f 66 20 49 6e 64 65 78 2e 6f 6e 45 72  ue of Index.onEr
620d0 72 6f 72 20 69 6e 64 69 63 61 74 65 20 74 68 65  ror indicate the
620e0 20 77 68 69 63 68 20 63 6f 6e 66 6c 69 63 74 20   which conflict 
620f0 72 65 73 6f 6c 75 74 69 6f 6e 20 0a 2a 2a 20 61  resolution .** a
62100 6c 67 6f 72 69 74 68 6d 20 74 6f 20 65 6d 70 6c  lgorithm to empl
62110 6f 79 20 77 68 65 6e 65 76 65 72 20 61 6e 20 61  oy whenever an a
62120 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74  ttempt is made t
62130 6f 20 69 6e 73 65 72 74 20 61 20 6e 6f 6e 2d 75  o insert a non-u
62140 6e 69 71 75 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74  nique.** element
62150 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 6e 64 65  ..*/.struct Inde
62160 78 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d  x {.  char *zNam
62170 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  e;     /* Name o
62180 66 20 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a  f this index */.
62190 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20    int nColumn;  
621a0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
621b0 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 74  columns in the t
621c0 61 62 6c 65 20 75 73 65 64 20 62 79 20 74 68 69  able used by thi
621d0 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74  s index */.  int
621e0 20 2a 61 69 43 6f 6c 75 6d 6e 3b 20 20 20 2f 2a   *aiColumn;   /*
621f0 20 57 68 69 63 68 20 63 6f 6c 75 6d 6e 73 20 61   Which columns a
62200 72 65 20 75 73 65 64 20 62 79 20 74 68 69 73 20  re used by this 
62210 69 6e 64 65 78 2e 20 20 31 73 74 20 69 73 20 30  index.  1st is 0
62220 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 2a   */.  unsigned *
62230 61 69 52 6f 77 45 73 74 3b 20 2f 2a 20 52 65 73  aiRowEst; /* Res
62240 75 6c 74 20 6f 66 20 41 4e 41 4c 59 5a 45 3a 20  ult of ANALYZE: 
62250 45 73 74 2e 20 72 6f 77 73 20 73 65 6c 65 63 74  Est. rows select
62260 65 64 20 62 79 20 65 61 63 68 20 63 6f 6c 75 6d  ed by each colum
62270 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  n */.  Table *pT
62280 61 62 6c 65 3b 20 20 20 2f 2a 20 54 68 65 20 53  able;   /* The S
62290 51 4c 20 74 61 62 6c 65 20 62 65 69 6e 67 20 69  QL table being i
622a0 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20  ndexed */.  int 
622b0 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20  tnum;        /* 
622c0 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  Page containing 
622d0 72 6f 6f 74 20 6f 66 20 74 68 69 73 20 69 6e 64  root of this ind
622e0 65 78 20 69 6e 20 64 61 74 61 62 61 73 65 20 66  ex in database f
622f0 69 6c 65 20 2a 2f 0a 20 20 75 38 20 6f 6e 45 72  ile */.  u8 onEr
62300 72 6f 72 3b 20 20 20 20 20 20 2f 2a 20 4f 45 5f  ror;      /* OE_
62310 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65  Abort, OE_Ignore
62320 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20 6f 72  , OE_Replace, or
62330 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20 20 75 38   OE_None */.  u8
62340 20 61 75 74 6f 49 6e 64 65 78 3b 20 20 20 20 2f   autoIndex;    /
62350 2a 20 54 72 75 65 20 69 66 20 69 73 20 61 75 74  * True if is aut
62360 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 61 74  omatically creat
62370 65 64 20 28 65 78 3a 20 62 79 20 55 4e 49 51 55  ed (ex: by UNIQU
62380 45 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43  E) */.  char *zC
62390 6f 6c 41 66 66 3b 20 20 20 2f 2a 20 53 74 72 69  olAff;   /* Stri
623a0 6e 67 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20  ng defining the 
623b0 61 66 66 69 6e 69 74 79 20 6f 66 20 65 61 63 68  affinity of each
623c0 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49 6e 64   column */.  Ind
623d0 65 78 20 2a 70 4e 65 78 74 3b 20 20 20 20 2f 2a  ex *pNext;    /*
623e0 20 54 68 65 20 6e 65 78 74 20 69 6e 64 65 78 20   The next index 
623f0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
62400 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 20 2a  the same table *
62410 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68  /.  Schema *pSch
62420 65 6d 61 3b 20 2f 2a 20 53 63 68 65 6d 61 20 63  ema; /* Schema c
62430 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 73 20 69  ontaining this i
62440 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 2a 61 53  ndex */.  u8 *aS
62450 6f 72 74 4f 72 64 65 72 3b 20 20 2f 2a 20 41 72  ortOrder;  /* Ar
62460 72 61 79 20 6f 66 20 73 69 7a 65 20 49 6e 64 65  ray of size Inde
62470 78 2e 6e 43 6f 6c 75 6d 6e 2e 20 54 72 75 65 3d  x.nColumn. True=
62480 3d 44 45 53 43 2c 20 46 61 6c 73 65 3d 3d 41 53  =DESC, False==AS
62490 43 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a  C */.  char **az
624a0 43 6f 6c 6c 3b 20 20 20 2f 2a 20 41 72 72 61 79  Coll;   /* Array
624b0 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65   of collation se
624c0 71 75 65 6e 63 65 20 6e 61 6d 65 73 20 66 6f 72  quence names for
624d0 20 69 6e 64 65 78 20 2a 2f 0a 20 20 49 6e 64 65   index */.  Inde
624e0 78 53 61 6d 70 6c 65 20 2a 61 53 61 6d 70 6c 65  xSample *aSample
624f0 3b 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66  ;    /* Array of
62500 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41   SQLITE_INDEX_SA
62510 4d 50 4c 45 53 20 73 61 6d 70 6c 65 73 20 2a 2f  MPLES samples */
62520 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20  .};../*.** Each 
62530 73 61 6d 70 6c 65 20 73 74 6f 72 65 64 20 69 6e  sample stored in
62540 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
62550 32 20 74 61 62 6c 65 20 69 73 20 72 65 70 72 65  2 table is repre
62560 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79  sented in memory
62570 20 0a 2a 2a 20 75 73 69 6e 67 20 61 20 73 74 72   .** using a str
62580 75 63 74 75 72 65 20 6f 66 20 74 68 69 73 20 74  ucture of this t
62590 79 70 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49  ype..*/.struct I
625a0 6e 64 65 78 53 61 6d 70 6c 65 20 7b 0a 20 20 75  ndexSample {.  u
625b0 6e 69 6f 6e 20 7b 0a 20 20 20 20 63 68 61 72 20  nion {.    char 
625c0 2a 7a 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 61  *z;        /* Va
625d0 6c 75 65 20 69 66 20 65 54 79 70 65 20 69 73 20  lue if eType is 
625e0 53 51 4c 49 54 45 5f 54 45 58 54 20 6f 72 20 53  SQLITE_TEXT or S
625f0 51 4c 49 54 45 5f 42 4c 4f 42 20 2a 2f 0a 20 20  QLITE_BLOB */.  
62600 20 20 64 6f 75 62 6c 65 20 72 3b 20 20 20 20 20    double r;     
62610 20 20 2f 2a 20 56 61 6c 75 65 20 69 66 20 65 54    /* Value if eT
62620 79 70 65 20 69 73 20 53 51 4c 49 54 45 5f 46 4c  ype is SQLITE_FL
62630 4f 41 54 20 6f 72 20 53 51 4c 49 54 45 5f 49 4e  OAT or SQLITE_IN
62640 54 45 47 45 52 20 2a 2f 0a 20 20 7d 20 75 3b 0a  TEGER */.  } u;.
62650 20 20 75 38 20 65 54 79 70 65 3b 20 20 20 20 20    u8 eType;     
62660 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4e 55      /* SQLITE_NU
62670 4c 4c 2c 20 53 51 4c 49 54 45 5f 49 4e 54 45 47  LL, SQLITE_INTEG
62680 45 52 20 2e 2e 2e 20 65 74 63 2e 20 2a 2f 0a 20  ER ... etc. */. 
62690 20 75 38 20 6e 42 79 74 65 3b 20 20 20 20 20 20   u8 nByte;      
626a0 20 20 20 2f 2a 20 53 69 7a 65 20 69 6e 20 62 79     /* Size in by
626b0 74 65 20 6f 66 20 74 65 78 74 20 6f 72 20 62 6c  te of text or bl
626c0 6f 62 2e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ob. */.};../*.**
626d0 20 45 61 63 68 20 74 6f 6b 65 6e 20 63 6f 6d 69   Each token comi
626e0 6e 67 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 65  ng out of the le
626f0 78 65 72 20 69 73 20 61 6e 20 69 6e 73 74 61 6e  xer is an instan
62700 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74  ce of.** this st
62710 72 75 63 74 75 72 65 2e 20 20 54 6f 6b 65 6e 73  ructure.  Tokens
62720 20 61 72 65 20 61 6c 73 6f 20 75 73 65 64 20 61   are also used a
62730 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65 78 70  s part of an exp
62740 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e  ression..**.** N
62750 6f 74 65 20 69 66 20 54 6f 6b 65 6e 2e 7a 3d 3d  ote if Token.z==
62760 30 20 74 68 65 6e 20 54 6f 6b 65 6e 2e 64 79 6e  0 then Token.dyn
62770 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 61 72 65   and Token.n are
62780 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 0a 2a   undefined and.*
62790 2a 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 72 61  * may contain ra
627a0 6e 64 6f 6d 20 76 61 6c 75 65 73 2e 20 20 44 6f  ndom values.  Do
627b0 20 6e 6f 74 20 6d 61 6b 65 20 61 6e 79 20 61 73   not make any as
627c0 73 75 6d 70 74 69 6f 6e 73 20 61 62 6f 75 74 20  sumptions about 
627d0 54 6f 6b 65 6e 2e 64 79 6e 0a 2a 2a 20 61 6e 64  Token.dyn.** and
627e0 20 54 6f 6b 65 6e 2e 6e 20 77 68 65 6e 20 54 6f   Token.n when To
627f0 6b 65 6e 2e 7a 3d 3d 30 2e 0a 2a 2f 0a 73 74 72  ken.z==0..*/.str
62800 75 63 74 20 54 6f 6b 65 6e 20 7b 0a 20 20 63 6f  uct Token {.  co
62810 6e 73 74 20 63 68 61 72 20 2a 7a 3b 20 20 20 20  nst char *z;    
62820 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20   /* Text of the 
62830 74 6f 6b 65 6e 2e 20 20 4e 6f 74 20 4e 55 4c 4c  token.  Not NULL
62840 2d 74 65 72 6d 69 6e 61 74 65 64 21 20 2a 2f 0a  -terminated! */.
62850 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e    unsigned int n
62860 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f  ;    /* Number o
62870 66 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20  f characters in 
62880 74 68 69 73 20 74 6f 6b 65 6e 20 2a 2f 0a 7d 3b  this token */.};
62890 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
628a0 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75  nce of this stru
628b0 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 69  cture contains i
628c0 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e 65 65 64 65  nformation neede
628d0 64 20 74 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a  d to generate.**
628e0 20 63 6f 64 65 20 66 6f 72 20 61 20 53 45 4c 45   code for a SELE
628f0 43 54 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  CT that contains
62900 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
62910 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 45  ions..**.** If E
62920 78 70 72 2e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43  xpr.op==TK_AGG_C
62930 4f 4c 55 4d 4e 20 6f 72 20 54 4b 5f 41 47 47 5f  OLUMN or TK_AGG_
62940 46 55 4e 43 54 49 4f 4e 20 74 68 65 6e 20 45 78  FUNCTION then Ex
62950 70 72 2e 70 41 67 67 49 6e 66 6f 20 69 73 20 61  pr.pAggInfo is a
62960 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  .** pointer to t
62970 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20  his structure.  
62980 54 68 65 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e  The Expr.iColumn
62990 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 69 6e   field is the in
629a0 64 65 78 20 69 6e 0a 2a 2a 20 41 67 67 49 6e 66  dex in.** AggInf
629b0 6f 2e 61 43 6f 6c 5b 5d 20 6f 72 20 41 67 67 49  o.aCol[] or AggI
629c0 6e 66 6f 2e 61 46 75 6e 63 5b 5d 20 6f 66 20 69  nfo.aFunc[] of i
629d0 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e 65 65 64 65  nformation neede
629e0 64 20 74 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a  d to generate.**
629f0 20 63 6f 64 65 20 66 6f 72 20 74 68 61 74 20 6e   code for that n
62a00 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 67 67 49 6e  ode..**.** AggIn
62a10 66 6f 2e 70 47 72 6f 75 70 42 79 20 61 6e 64 20  fo.pGroupBy and 
62a20 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 2e 70 45  AggInfo.aFunc.pE
62a30 78 70 72 20 70 6f 69 6e 74 20 74 6f 20 66 69 65  xpr point to fie
62a40 6c 64 73 20 77 69 74 68 69 6e 20 74 68 65 0a 2a  lds within the.*
62a50 2a 20 6f 72 69 67 69 6e 61 6c 20 53 65 6c 65 63  * original Selec
62a60 74 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  t structure that
62a70 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 53   describes the S
62a80 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
62a90 20 20 54 68 65 73 65 0a 2a 2a 20 66 69 65 6c 64    These.** field
62aa0 73 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f  s do not need to
62ab0 20 62 65 20 66 72 65 65 64 20 77 68 65 6e 20 64   be freed when d
62ac0 65 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68 65 20  eallocating the 
62ad0 41 67 67 49 6e 66 6f 20 73 74 72 75 63 74 75 72  AggInfo structur
62ae0 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 41 67 67  e..*/.struct Agg
62af0 49 6e 66 6f 20 7b 0a 20 20 75 38 20 64 69 72 65  Info {.  u8 dire
62b00 63 74 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20  ctMode;         
62b10 20 2f 2a 20 44 69 72 65 63 74 20 72 65 6e 64 65   /* Direct rende
62b20 72 69 6e 67 20 6d 6f 64 65 20 6d 65 61 6e 73 20  ring mode means 
62b30 74 61 6b 65 20 64 61 74 61 20 64 69 72 65 63 74  take data direct
62b40 6c 79 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ly.             
62b50 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
62b60 66 72 6f 6d 20 73 6f 75 72 63 65 20 74 61 62 6c  from source tabl
62b70 65 73 20 72 61 74 68 65 72 20 74 68 61 6e 20 66  es rather than f
62b80 72 6f 6d 20 61 63 63 75 6d 75 6c 61 74 6f 72 73  rom accumulators
62b90 20 2a 2f 0a 20 20 75 38 20 75 73 65 53 6f 72 74   */.  u8 useSort
62ba0 69 6e 67 49 64 78 3b 20 20 20 20 20 20 20 2f 2a  ingIdx;       /*
62bb0 20 49 6e 20 64 69 72 65 63 74 20 6d 6f 64 65 2c   In direct mode,
62bc0 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 73   reference the s
62bd0 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 72 61 74  orting index rat
62be0 68 65 72 0a 20 20 20 20 20 20 20 20 20 20 20 20  her.            
62bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
62c00 20 74 68 61 6e 20 74 68 65 20 73 6f 75 72 63 65   than the source
62c10 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
62c20 73 6f 72 74 69 6e 67 49 64 78 3b 20 20 20 20 20  sortingIdx;     
62c30 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75      /* Cursor nu
62c40 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f 72 74  mber of the sort
62c50 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 45  ing index */.  E
62c60 78 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42  xprList *pGroupB
62c70 79 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 67 72  y;     /* The gr
62c80 6f 75 70 20 62 79 20 63 6c 61 75 73 65 20 2a 2f  oup by clause */
62c90 0a 20 20 69 6e 74 20 6e 53 6f 72 74 69 6e 67 43  .  int nSortingC
62ca0 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f 2a 20 4e 75  olumn;     /* Nu
62cb0 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
62cc0 69 6e 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69  in the sorting i
62cd0 6e 64 65 78 20 2a 2f 0a 20 20 73 74 72 75 63 74  ndex */.  struct
62ce0 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 7b 20 20   AggInfo_col {  
62cf0 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 63 6f    /* For each co
62d00 6c 75 6d 6e 20 75 73 65 64 20 69 6e 20 73 6f 75  lumn used in sou
62d10 72 63 65 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20  rce tables */.  
62d20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20    Table *pTab;  
62d30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f             /* So
62d40 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20  urce table */.  
62d50 20 20 69 6e 74 20 69 54 61 62 6c 65 3b 20 20 20    int iTable;   
62d60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
62d70 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74  rsor number of t
62d80 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20  he source table 
62d90 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 75  */.    int iColu
62da0 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  mn;             
62db0 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  /* Column number
62dc0 20 77 69 74 68 69 6e 20 74 68 65 20 73 6f 75 72   within the sour
62dd0 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  ce table */.    
62de0 69 6e 74 20 69 53 6f 72 74 65 72 43 6f 6c 75 6d  int iSorterColum
62df0 6e 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75  n;       /* Colu
62e00 6d 6e 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65  mn number in the
62e10 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 2a   sorting index *
62e20 2f 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20  /.    int iMem; 
62e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
62e40 2a 20 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  * Memory locatio
62e50 6e 20 74 68 61 74 20 61 63 74 73 20 61 73 20 61  n that acts as a
62e60 63 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20  ccumulator */.  
62e70 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20    Expr *pExpr;  
62e80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
62e90 65 20 6f 72 69 67 69 6e 61 6c 20 65 78 70 72 65  e original expre
62ea0 73 73 69 6f 6e 20 2a 2f 0a 20 20 7d 20 2a 61 43  ssion */.  } *aC
62eb0 6f 6c 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d  ol;.  int nColum
62ec0 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  n;            /*
62ed0 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 65 64 20   Number of used 
62ee0 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6c 5b  entries in aCol[
62ef0 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75  ] */.  int nColu
62f00 6d 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 2f  mnAlloc;       /
62f10 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74  * Number of slot
62f20 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  s allocated for 
62f30 61 43 6f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  aCol[] */.  int 
62f40 6e 41 63 63 75 6d 75 6c 61 74 6f 72 3b 20 20 20  nAccumulator;   
62f50 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
62f60 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 73 68   columns that sh
62f70 6f 77 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68  ow through to th
62f80 65 20 6f 75 74 70 75 74 2e 0a 20 20 20 20 20 20  e output..      
62f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62fa0 20 20 20 20 2a 2a 20 41 64 64 69 74 69 6f 6e 61      ** Additiona
62fb0 6c 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 75 73  l columns are us
62fc0 65 64 20 6f 6e 6c 79 20 61 73 20 70 61 72 61 6d  ed only as param
62fd0 65 74 65 72 73 20 74 6f 0a 20 20 20 20 20 20 20  eters to.       
62fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62ff0 20 20 20 2a 2a 20 61 67 67 72 65 67 61 74 65 20     ** aggregate 
63000 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 73  functions */.  s
63010 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75  truct AggInfo_fu
63020 6e 63 20 7b 20 20 20 2f 2a 20 46 6f 72 20 65 61  nc {   /* For ea
63030 63 68 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  ch aggregate fun
63040 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 45 78 70  ction */.    Exp
63050 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20  r *pExpr;       
63060 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73        /* Express
63070 69 6f 6e 20 65 6e 63 6f 64 69 6e 67 20 74 68 65  ion encoding the
63080 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20   function */.   
63090 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 3b   FuncDef *pFunc;
630a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
630b0 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
630c0 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ion implementati
630d0 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 4d  on */.    int iM
630e0 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  em;             
630f0 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 6c 6f 63     /* Memory loc
63100 61 74 69 6f 6e 20 74 68 61 74 20 61 63 74 73 20  ation that acts 
63110 61 73 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 2a  as accumulator *
63120 2f 0a 20 20 20 20 69 6e 74 20 69 44 69 73 74 69  /.    int iDisti
63130 6e 63 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  nct;           /
63140 2a 20 45 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  * Ephemeral tabl
63150 65 20 75 73 65 64 20 74 6f 20 65 6e 66 6f 72 63  e used to enforc
63160 65 20 44 49 53 54 49 4e 43 54 20 2a 2f 0a 20 20  e DISTINCT */.  
63170 7d 20 2a 61 46 75 6e 63 3b 0a 20 20 69 6e 74 20  } *aFunc;.  int 
63180 6e 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20  nFunc;          
63190 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
631a0 20 65 6e 74 72 69 65 73 20 69 6e 20 61 46 75 6e   entries in aFun
631b0 63 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75  c[] */.  int nFu
631c0 6e 63 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20  ncAlloc;        
631d0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c   /* Number of sl
631e0 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f  ots allocated fo
631f0 72 20 61 46 75 6e 63 5b 5d 20 2a 2f 0a 7d 3b 0a  r aFunc[] */.};.
63200 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 6e 6f 64 65  ./*.** Each node
63210 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f   of an expressio
63220 6e 20 69 6e 20 74 68 65 20 70 61 72 73 65 20 74  n in the parse t
63230 72 65 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e  ree is an instan
63240 63 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74  ce.** of this st
63250 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 45  ructure..**.** E
63260 78 70 72 2e 6f 70 20 69 73 20 74 68 65 20 6f 70  xpr.op is the op
63270 63 6f 64 65 2e 20 54 68 65 20 69 6e 74 65 67 65  code. The intege
63280 72 20 70 61 72 73 65 72 20 74 6f 6b 65 6e 20 63  r parser token c
63290 6f 64 65 73 20 61 72 65 20 72 65 75 73 65 64 0a  odes are reused.
632a0 2a 2a 20 61 73 20 6f 70 63 6f 64 65 73 20 68 65  ** as opcodes he
632b0 72 65 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  re. For example,
632c0 20 74 68 65 20 70 61 72 73 65 72 20 64 65 66 69   the parser defi
632d0 6e 65 73 20 54 4b 5f 47 45 20 74 6f 20 62 65 20  nes TK_GE to be 
632e0 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 63 6f  an integer.** co
632f0 64 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20  de representing 
63300 74 68 65 20 22 3e 3d 22 20 6f 70 65 72 61 74 6f  the ">=" operato
63310 72 2e 20 54 68 69 73 20 73 61 6d 65 20 69 6e 74  r. This same int
63320 65 67 65 72 20 63 6f 64 65 20 69 73 20 72 65 75  eger code is reu
63330 73 65 64 0a 2a 2a 20 74 6f 20 72 65 70 72 65 73  sed.** to repres
63340 65 6e 74 20 74 68 65 20 67 72 65 61 74 65 72 2d  ent the greater-
63350 74 68 61 6e 2d 6f 72 2d 65 71 75 61 6c 2d 74 6f  than-or-equal-to
63360 20 6f 70 65 72 61 74 6f 72 20 69 6e 20 74 68 65   operator in the
63370 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74   expression.** t
63380 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ree..**.** If th
63390 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
633a0 61 6e 20 53 51 4c 20 6c 69 74 65 72 61 6c 20 28  an SQL literal (
633b0 54 4b 5f 49 4e 54 45 47 45 52 2c 20 54 4b 5f 46  TK_INTEGER, TK_F
633c0 4c 4f 41 54 2c 20 54 4b 5f 42 4c 4f 42 2c 20 0a  LOAT, TK_BLOB, .
633d0 2a 2a 20 6f 72 20 54 4b 5f 53 54 52 49 4e 47 29  ** or TK_STRING)
633e0 2c 20 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65  , then Expr.toke
633f0 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74  n contains the t
63400 65 78 74 20 6f 66 20 74 68 65 20 53 51 4c 20 6c  ext of the SQL l
63410 69 74 65 72 61 6c 2e 20 49 66 0a 2a 2a 20 74 68  iteral. If.** th
63420 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
63430 61 20 76 61 72 69 61 62 6c 65 20 28 54 4b 5f 56  a variable (TK_V
63440 41 52 49 41 42 4c 45 29 2c 20 74 68 65 6e 20 45  ARIABLE), then E
63450 78 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61 69  xpr.token contai
63460 6e 73 20 74 68 65 20 0a 2a 2a 20 76 61 72 69 61  ns the .** varia
63470 62 6c 65 20 6e 61 6d 65 2e 20 46 69 6e 61 6c 6c  ble name. Finall
63480 79 2c 20 69 66 20 74 68 65 20 65 78 70 72 65 73  y, if the expres
63490 73 69 6f 6e 20 69 73 20 61 6e 20 53 51 4c 20 66  sion is an SQL f
634a0 75 6e 63 74 69 6f 6e 20 28 54 4b 5f 46 55 4e 43  unction (TK_FUNC
634b0 54 49 4f 4e 29 2c 0a 2a 2a 20 74 68 65 6e 20 45  TION),.** then E
634c0 78 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61 69  xpr.token contai
634d0 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  ns the name of t
634e0 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a  he function..**.
634f0 2a 2a 20 45 78 70 72 2e 70 52 69 67 68 74 20 61  ** Expr.pRight a
63500 6e 64 20 45 78 70 72 2e 70 4c 65 66 74 20 61 72  nd Expr.pLeft ar
63510 65 20 74 68 65 20 6c 65 66 74 20 61 6e 64 20 72  e the left and r
63520 69 67 68 74 20 73 75 62 65 78 70 72 65 73 73 69  ight subexpressi
63530 6f 6e 73 20 6f 66 20 61 0a 2a 2a 20 62 69 6e 61  ons of a.** bina
63540 72 79 20 6f 70 65 72 61 74 6f 72 2e 20 45 69 74  ry operator. Eit
63550 68 65 72 20 6f 72 20 62 6f 74 68 20 6d 61 79 20  her or both may 
63560 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 45  be NULL..**.** E
63570 78 70 72 2e 78 2e 70 4c 69 73 74 20 69 73 20 61  xpr.x.pList is a
63580 20 6c 69 73 74 20 6f 66 20 61 72 67 75 6d 65 6e   list of argumen
63590 74 73 20 69 66 20 74 68 65 20 65 78 70 72 65 73  ts if the expres
635a0 73 69 6f 6e 20 69 73 20 61 6e 20 53 51 4c 20 66  sion is an SQL f
635b0 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 61 20 43 41  unction,.** a CA
635c0 53 45 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72  SE expression or
635d0 20 61 6e 20 49 4e 20 65 78 70 72 65 73 73 69 6f   an IN expressio
635e0 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3c  n of the form "<
635f0 6c 68 73 3e 20 49 4e 20 28 3c 79 3e 2c 20 3c 7a  lhs> IN (<y>, <z
63600 3e 2e 2e 2e 29 22 2e 0a 2a 2a 20 45 78 70 72 2e  >...)"..** Expr.
63610 78 2e 70 53 65 6c 65 63 74 20 69 73 20 75 73 65  x.pSelect is use
63620 64 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73  d if the express
63630 69 6f 6e 20 69 73 20 61 20 73 75 62 2d 73 65 6c  ion is a sub-sel
63640 65 63 74 20 6f 72 20 61 6e 20 65 78 70 72 65 73  ect or an expres
63650 73 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 66  sion of.** the f
63660 6f 72 6d 20 22 3c 6c 68 73 3e 20 49 4e 20 28 53  orm "<lhs> IN (S
63670 45 4c 45 43 54 20 2e 2e 2e 29 22 2e 20 49 66 20  ELECT ...)". If 
63680 74 68 65 20 45 50 5f 78 49 73 53 65 6c 65 63 74  the EP_xIsSelect
63690 20 62 69 74 20 69 73 20 73 65 74 20 69 6e 20 74   bit is set in t
636a0 68 65 0a 2a 2a 20 45 78 70 72 2e 66 6c 61 67 73  he.** Expr.flags
636b0 20 6d 61 73 6b 2c 20 74 68 65 6e 20 45 78 70 72   mask, then Expr
636c0 2e 78 2e 70 53 65 6c 65 63 74 20 69 73 20 76 61  .x.pSelect is va
636d0 6c 69 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  lid. Otherwise, 
636e0 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 69 73 20  Expr.x.pList is 
636f0 0a 2a 2a 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a  .** valid..**.**
63700 20 41 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f   An expression o
63710 66 20 74 68 65 20 66 6f 72 6d 20 49 44 20 6f 72  f the form ID or
63720 20 49 44 2e 49 44 20 72 65 66 65 72 73 20 74 6f   ID.ID refers to
63730 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 74   a column in a t
63740 61 62 6c 65 2e 0a 2a 2a 20 46 6f 72 20 73 75 63  able..** For suc
63750 68 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 20 45  h expressions, E
63760 78 70 72 2e 6f 70 20 69 73 20 73 65 74 20 74 6f  xpr.op is set to
63770 20 54 4b 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 45   TK_COLUMN and E
63780 78 70 72 2e 69 54 61 62 6c 65 20 69 73 0a 2a 2a  xpr.iTable is.**
63790 20 74 68 65 20 69 6e 74 65 67 65 72 20 63 75 72   the integer cur
637a0 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 61 20  sor number of a 
637b0 56 44 42 45 20 63 75 72 73 6f 72 20 70 6f 69 6e  VDBE cursor poin
637c0 74 69 6e 67 20 74 6f 20 74 68 61 74 20 74 61 62  ting to that tab
637d0 6c 65 20 61 6e 64 0a 2a 2a 20 45 78 70 72 2e 69  le and.** Expr.i
637e0 43 6f 6c 75 6d 6e 20 69 73 20 74 68 65 20 63 6f  Column is the co
637f0 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20  lumn number for 
63800 74 68 65 20 73 70 65 63 69 66 69 63 20 63 6f 6c  the specific col
63810 75 6d 6e 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20  umn.  If the.** 
63820 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 75 73  expression is us
63830 65 64 20 61 73 20 61 20 72 65 73 75 6c 74 20 69  ed as a result i
63840 6e 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 53  n an aggregate S
63850 45 4c 45 43 54 2c 20 74 68 65 6e 20 74 68 65 0a  ELECT, then the.
63860 2a 2a 20 76 61 6c 75 65 20 69 73 20 61 6c 73 6f  ** value is also
63870 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 45   stored in the E
63880 78 70 72 2e 69 41 67 67 20 63 6f 6c 75 6d 6e 20  xpr.iAgg column 
63890 69 6e 20 74 68 65 20 61 67 67 72 65 67 61 74 65  in the aggregate
638a0 20 73 6f 20 74 68 61 74 0a 2a 2a 20 69 74 20 63   so that.** it c
638b0 61 6e 20 62 65 20 61 63 63 65 73 73 65 64 20 61  an be accessed a
638c0 66 74 65 72 20 61 6c 6c 20 61 67 67 72 65 67 61  fter all aggrega
638d0 74 65 73 20 61 72 65 20 63 6f 6d 70 75 74 65 64  tes are computed
638e0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65  ..**.** If the e
638f0 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6e 20  xpression is an 
63900 75 6e 62 6f 75 6e 64 20 76 61 72 69 61 62 6c 65  unbound variable
63910 20 6d 61 72 6b 65 72 20 28 61 20 71 75 65 73 74   marker (a quest
63920 69 6f 6e 20 6d 61 72 6b 20 0a 2a 2a 20 63 68 61  ion mark .** cha
63930 72 61 63 74 65 72 20 27 3f 27 20 69 6e 20 74 68  racter '?' in th
63940 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 29 20  e original SQL) 
63950 74 68 65 6e 20 74 68 65 20 45 78 70 72 2e 69 54  then the Expr.iT
63960 61 62 6c 65 20 68 6f 6c 64 73 20 74 68 65 20 69  able holds the i
63970 6e 64 65 78 20 0a 2a 2a 20 6e 75 6d 62 65 72 20  ndex .** number 
63980 66 6f 72 20 74 68 61 74 20 76 61 72 69 61 62 6c  for that variabl
63990 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
639a0 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20  expression is a 
639b0 73 75 62 71 75 65 72 79 20 74 68 65 6e 20 45 78  subquery then Ex
639c0 70 72 2e 69 43 6f 6c 75 6d 6e 20 68 6f 6c 64 73  pr.iColumn holds
639d0 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 72   an integer.** r
639e0 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 63  egister number c
639f0 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72 65  ontaining the re
63a00 73 75 6c 74 20 6f 66 20 74 68 65 20 73 75 62 71  sult of the subq
63a10 75 65 72 79 2e 20 20 49 66 20 74 68 65 0a 2a 2a  uery.  If the.**
63a20 20 73 75 62 71 75 65 72 79 20 67 69 76 65 73 20   subquery gives 
63a30 61 20 63 6f 6e 73 74 61 6e 74 20 72 65 73 75 6c  a constant resul
63a40 74 2c 20 74 68 65 6e 20 69 54 61 62 6c 65 20 69  t, then iTable i
63a50 73 20 2d 31 2e 20 20 49 66 20 74 68 65 20 73 75  s -1.  If the su
63a60 62 71 75 65 72 79 0a 2a 2a 20 67 69 76 65 73 20  bquery.** gives 
63a70 61 20 64 69 66 66 65 72 65 6e 74 20 61 6e 73 77  a different answ
63a80 65 72 20 61 74 20 64 69 66 66 65 72 65 6e 74 20  er at different 
63a90 74 69 6d 65 73 20 64 75 72 69 6e 67 20 73 74 61  times during sta
63aa0 74 65 6d 65 6e 74 20 70 72 6f 63 65 73 73 69 6e  tement processin
63ab0 67 0a 2a 2a 20 74 68 65 6e 20 69 54 61 62 6c 65  g.** then iTable
63ac0 20 69 73 20 74 68 65 20 61 64 64 72 65 73 73 20   is the address 
63ad0 6f 66 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20  of a subroutine 
63ae0 74 68 61 74 20 63 6f 6d 70 75 74 65 73 20 74 68  that computes th
63af0 65 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a 0a 2a  e subquery..**.*
63b00 2a 20 49 66 20 74 68 65 20 45 78 70 72 20 69 73  * If the Expr is
63b10 20 6f 66 20 74 79 70 65 20 4f 50 5f 43 6f 6c 75   of type OP_Colu
63b20 6d 6e 2c 20 61 6e 64 20 74 68 65 20 74 61 62 6c  mn, and the tabl
63b30 65 20 69 74 20 69 73 20 73 65 6c 65 63 74 69 6e  e it is selectin
63b40 67 20 66 72 6f 6d 0a 2a 2a 20 69 73 20 61 20 64  g from.** is a d
63b50 69 73 6b 20 74 61 62 6c 65 20 6f 72 20 74 68 65  isk table or the
63b60 20 22 6f 6c 64 2e 2a 22 20 70 73 65 75 64 6f 2d   "old.*" pseudo-
63b70 74 61 62 6c 65 2c 20 74 68 65 6e 20 70 54 61 62  table, then pTab
63b80 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a 2a   points to the.*
63b90 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  * corresponding 
63ba0 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e  table definition
63bb0 2e 0a 2a 2a 0a 2a 2a 20 41 4c 4c 4f 43 41 54 49  ..**.** ALLOCATI
63bc0 4f 4e 20 4e 4f 54 45 53 3a 0a 2a 2a 0a 2a 2a 20  ON NOTES:.**.** 
63bd0 45 78 70 72 20 6f 62 6a 65 63 74 73 20 63 61 6e  Expr objects can
63be0 20 75 73 65 20 61 20 6c 6f 74 20 6f 66 20 6d 65   use a lot of me
63bf0 6d 6f 72 79 20 73 70 61 63 65 20 69 6e 20 64 61  mory space in da
63c00 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 20  tabase schema.  
63c10 54 6f 0a 2a 2a 20 68 65 6c 70 20 72 65 64 75 63  To.** help reduc
63c20 65 20 6d 65 6d 6f 72 79 20 72 65 71 75 69 72 65  e memory require
63c30 6d 65 6e 74 73 2c 20 73 6f 6d 65 74 69 6d 65 73  ments, sometimes
63c40 20 61 6e 20 45 78 70 72 20 6f 62 6a 65 63 74 20   an Expr object 
63c50 77 69 6c 6c 20 62 65 0a 2a 2a 20 74 72 75 6e 63  will be.** trunc
63c60 61 74 65 64 2e 20 20 41 6e 64 20 74 6f 20 72 65  ated.  And to re
63c70 64 75 63 65 20 74 68 65 20 6e 75 6d 62 65 72 20  duce the number 
63c80 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  of memory alloca
63c90 74 69 6f 6e 73 2c 20 73 6f 6d 65 74 69 6d 65 73  tions, sometimes
63ca0 0a 2a 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20  .** two or more 
63cb0 45 78 70 72 20 6f 62 6a 65 63 74 73 20 77 69 6c  Expr objects wil
63cc0 6c 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 61  l be stored in a
63cd0 20 73 69 6e 67 6c 65 20 6d 65 6d 6f 72 79 20 61   single memory a
63ce0 6c 6c 6f 63 61 74 69 6f 6e 2c 0a 2a 2a 20 74 6f  llocation,.** to
63cf0 67 65 74 68 65 72 20 77 69 74 68 20 45 78 70 72  gether with Expr
63d00 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67 73 2e  .zToken strings.
63d10 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 50  .**.** If the EP
63d20 5f 52 65 64 75 63 65 64 20 61 6e 64 20 45 50 5f  _Reduced and EP_
63d30 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61 67 73 20  TokenOnly flags 
63d40 61 72 65 20 73 65 74 20 77 68 65 6e 0a 2a 2a 20  are set when.** 
63d50 61 6e 20 45 78 70 72 20 6f 62 6a 65 63 74 20 69  an Expr object i
63d60 73 20 74 72 75 6e 63 61 74 65 64 2e 20 20 57 68  s truncated.  Wh
63d70 65 6e 20 45 50 5f 52 65 64 75 63 65 64 20 69 73  en EP_Reduced is
63d80 20 73 65 74 2c 20 74 68 65 6e 20 61 6c 6c 0a 2a   set, then all.*
63d90 2a 20 74 68 65 20 63 68 69 6c 64 20 45 78 70 72  * the child Expr
63da0 20 6f 62 6a 65 63 74 73 20 69 6e 20 74 68 65 20   objects in the 
63db0 45 78 70 72 2e 70 4c 65 66 74 20 61 6e 64 20 45  Expr.pLeft and E
63dc0 78 70 72 2e 70 52 69 67 68 74 20 73 75 62 74 72  xpr.pRight subtr
63dd0 65 65 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 74 61  ees.** are conta
63de0 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 20  ined within the 
63df0 73 61 6d 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  same memory allo
63e00 63 61 74 69 6f 6e 2e 20 20 4e 6f 74 65 2c 20 68  cation.  Note, h
63e10 6f 77 65 76 65 72 2c 20 74 68 61 74 0a 2a 2a 20  owever, that.** 
63e20 74 68 65 20 73 75 62 74 72 65 65 73 20 69 6e 20  the subtrees in 
63e30 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 6f 72 20  Expr.x.pList or 
63e40 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74 20 61  Expr.x.pSelect a
63e50 72 65 20 61 6c 77 61 79 73 20 73 65 70 61 72 61  re always separa
63e60 74 65 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65  tely.** allocate
63e70 64 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66  d, regardless of
63e80 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
63e90 45 50 5f 52 65 64 75 63 65 64 20 69 73 20 73 65  EP_Reduced is se
63ea0 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78 70  t..*/.struct Exp
63eb0 72 20 7b 0a 20 20 75 38 20 6f 70 3b 20 20 20 20  r {.  u8 op;    
63ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
63ed0 4f 70 65 72 61 74 69 6f 6e 20 70 65 72 66 6f 72  Operation perfor
63ee0 6d 65 64 20 62 79 20 74 68 69 73 20 6e 6f 64 65  med by this node
63ef0 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e   */.  char affin
63f00 69 74 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ity;         /* 
63f10 54 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20  The affinity of 
63f20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20 30 20  the column or 0 
63f30 69 66 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20  if not a column 
63f40 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20  */.  u16 flags; 
63f50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
63f60 61 72 69 6f 75 73 20 66 6c 61 67 73 2e 20 20 45  arious flags.  E
63f70 50 5f 2a 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f  P_* See below */
63f80 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 63  .  union {.    c
63f90 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 20 20 20 20  har *zToken;    
63fa0 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 76        /* Token v
63fb0 61 6c 75 65 2e 20 5a 65 72 6f 20 74 65 72 6d 69  alue. Zero termi
63fc0 6e 61 74 65 64 20 61 6e 64 20 64 65 71 75 6f 74  nated and dequot
63fd0 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 56  ed */.    int iV
63fe0 61 6c 75 65 3b 20 20 20 20 20 20 20 20 20 20 20  alue;           
63ff0 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75   /* Integer valu
64000 65 20 69 66 20 45 50 5f 49 6e 74 56 61 6c 75 65  e if EP_IntValue
64010 20 2a 2f 0a 20 20 7d 20 75 3b 0a 0a 20 20 2f 2a   */.  } u;..  /*
64020 20 49 66 20 74 68 65 20 45 50 5f 54 6f 6b 65 6e   If the EP_Token
64030 4f 6e 6c 79 20 66 6c 61 67 20 69 73 20 73 65 74  Only flag is set
64040 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66 6c 61   in the Expr.fla
64050 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e 20 6e 6f  gs mask, then no
64060 0a 20 20 2a 2a 20 73 70 61 63 65 20 69 73 20 61  .  ** space is a
64070 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65  llocated for the
64080 20 66 69 65 6c 64 73 20 62 65 6c 6f 77 20 74 68   fields below th
64090 69 73 20 70 6f 69 6e 74 2e 20 41 6e 20 61 74 74  is point. An att
640a0 65 6d 70 74 20 74 6f 0a 20 20 2a 2a 20 61 63 63  empt to.  ** acc
640b0 65 73 73 20 74 68 65 6d 20 77 69 6c 6c 20 72 65  ess them will re
640c0 73 75 6c 74 20 69 6e 20 61 20 73 65 67 66 61 75  sult in a segfau
640d0 6c 74 20 6f 72 20 6d 61 6c 66 75 6e 63 74 69 6f  lt or malfunctio
640e0 6e 2e 20 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n. .  **********
640f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 20  ***********/..  
64130 45 78 70 72 20 2a 70 4c 65 66 74 3b 20 20 20 20  Expr *pLeft;    
64140 20 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 73         /* Left s
64150 75 62 6e 6f 64 65 20 2a 2f 0a 20 20 45 78 70 72  ubnode */.  Expr
64160 20 2a 70 52 69 67 68 74 3b 20 20 20 20 20 20 20   *pRight;       
64170 20 20 20 2f 2a 20 52 69 67 68 74 20 73 75 62 6e     /* Right subn
64180 6f 64 65 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b  ode */.  union {
64190 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70  .    ExprList *p
641a0 4c 69 73 74 3b 20 20 20 20 20 2f 2a 20 46 75 6e  List;     /* Fun
641b0 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20  ction arguments 
641c0 6f 72 20 69 6e 20 22 3c 65 78 70 72 3e 20 49 4e  or in "<expr> IN
641d0 20 28 3c 65 78 70 72 2d 6c 69 73 74 29 22 20 2a   (<expr-list)" *
641e0 2f 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53  /.    Select *pS
641f0 65 6c 65 63 74 3b 20 20 20 20 20 2f 2a 20 55 73  elect;     /* Us
64200 65 64 20 66 6f 72 20 73 75 62 2d 73 65 6c 65 63  ed for sub-selec
64210 74 73 20 61 6e 64 20 22 3c 65 78 70 72 3e 20 49  ts and "<expr> I
64220 4e 20 28 3c 73 65 6c 65 63 74 3e 29 22 20 2a 2f  N (<select>)" */
64230 0a 20 20 7d 20 78 3b 0a 20 20 43 6f 6c 6c 53 65  .  } x;.  CollSe
64240 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20  q *pColl;       
64250 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f   /* The collatio
64260 6e 20 74 79 70 65 20 6f 66 20 74 68 65 20 63 6f  n type of the co
64270 6c 75 6d 6e 20 6f 72 20 30 20 2a 2f 0a 0a 20 20  lumn or 0 */..  
64280 2f 2a 20 49 66 20 74 68 65 20 45 50 5f 52 65 64  /* If the EP_Red
64290 75 63 65 64 20 66 6c 61 67 20 69 73 20 73 65 74  uced flag is set
642a0 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66 6c 61   in the Expr.fla
642b0 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e 20 6e 6f  gs mask, then no
642c0 0a 20 20 2a 2a 20 73 70 61 63 65 20 69 73 20 61  .  ** space is a
642d0 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65  llocated for the
642e0 20 66 69 65 6c 64 73 20 62 65 6c 6f 77 20 74 68   fields below th
642f0 69 73 20 70 6f 69 6e 74 2e 20 41 6e 20 61 74 74  is point. An att
64300 65 6d 70 74 20 74 6f 0a 20 20 2a 2a 20 61 63 63  empt to.  ** acc
64310 65 73 73 20 74 68 65 6d 20 77 69 6c 6c 20 72 65  ess them will re
64320 73 75 6c 74 20 69 6e 20 61 20 73 65 67 66 61 75  sult in a segfau
64330 6c 74 20 6f 72 20 6d 61 6c 66 75 6e 63 74 69 6f  lt or malfunctio
64340 6e 2e 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n..  ***********
64350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 20 69  **********/..  i
64390 6e 74 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20  nt iTable;      
643a0 20 20 20 20 20 20 2f 2a 20 54 4b 5f 43 4f 4c 55        /* TK_COLU
643b0 4d 4e 3a 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  MN: cursor numbe
643c0 72 20 6f 66 20 74 61 62 6c 65 20 68 6f 6c 64 69  r of table holdi
643d0 6e 67 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 20 20  ng column.      
643e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
643f0 20 20 20 2a 2a 20 54 4b 5f 52 45 47 49 53 54 45     ** TK_REGISTE
64400 52 3a 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62  R: register numb
64410 65 72 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  er.             
64420 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54              ** T
64430 4b 5f 54 52 49 47 47 45 52 3a 20 31 20 2d 3e 20  K_TRIGGER: 1 -> 
64440 6e 65 77 2c 20 30 20 2d 3e 20 6f 6c 64 20 2a 2f  new, 0 -> old */
64450 0a 20 20 69 31 36 20 69 43 6f 6c 75 6d 6e 3b 20  .  i16 iColumn; 
64460 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f            /* TK_
64470 43 4f 4c 55 4d 4e 3a 20 63 6f 6c 75 6d 6e 20 69  COLUMN: column i
64480 6e 64 65 78 2e 20 20 2d 31 20 66 6f 72 20 72 6f  ndex.  -1 for ro
64490 77 69 64 20 2a 2f 0a 20 20 69 31 36 20 69 41 67  wid */.  i16 iAg
644a0 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
644b0 2f 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20 69  /* Which entry i
644c0 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c  n pAggInfo->aCol
644d0 5b 5d 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d 20  [] or ->aFunc[] 
644e0 2a 2f 0a 20 20 69 31 36 20 69 52 69 67 68 74 4a  */.  i16 iRightJ
644f0 6f 69 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20 49  oinTable;   /* I
64500 66 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20 74  f EP_FromJoin, t
64510 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f  he right table o
64520 66 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20  f the join */.  
64530 75 38 20 66 6c 61 67 73 32 3b 20 20 20 20 20 20  u8 flags2;      
64540 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64         /* Second
64550 20 73 65 74 20 6f 66 20 66 6c 61 67 73 2e 20 20   set of flags.  
64560 45 50 32 5f 2e 2e 2e 20 2a 2f 0a 20 20 75 38 20  EP2_... */.  u8 
64570 6f 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20  op2;            
64580 20 20 20 20 2f 2a 20 49 66 20 61 20 54 4b 5f 52      /* If a TK_R
64590 45 47 49 53 54 45 52 2c 20 74 68 65 20 6f 72 69  EGISTER, the ori
645a0 67 69 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20 45  ginal value of E
645b0 78 70 72 2e 6f 70 20 2a 2f 0a 20 20 41 67 67 49  xpr.op */.  AggI
645c0 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 3b 20 20  nfo *pAggInfo;  
645d0 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20 54 4b     /* Used by TK
645e0 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20  _AGG_COLUMN and 
645f0 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20  TK_AGG_FUNCTION 
64600 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
64610 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
64620 61 62 6c 65 20 66 6f 72 20 54 4b 5f 43 4f 4c 55  able for TK_COLU
64630 4d 4e 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20  MN expressions. 
64640 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41  */.#if SQLITE_MA
64650 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20  X_EXPR_DEPTH>0. 
64660 20 69 6e 74 20 6e 48 65 69 67 68 74 3b 20 20 20   int nHeight;   
64670 20 20 20 20 20 20 20 20 2f 2a 20 48 65 69 67 68          /* Heigh
64680 74 20 6f 66 20 74 68 65 20 74 72 65 65 20 68 65  t of the tree he
64690 61 64 65 64 20 62 79 20 74 68 69 73 20 6e 6f 64  aded by this nod
646a0 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a  e */.#endif.};..
646b0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
646c0 69 6e 67 20 61 72 65 20 74 68 65 20 6d 65 61 6e  ing are the mean
646d0 69 6e 67 73 20 6f 66 20 62 69 74 73 20 69 6e 20  ings of bits in 
646e0 74 68 65 20 45 78 70 72 2e 66 6c 61 67 73 20 66  the Expr.flags f
646f0 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ield..*/.#define
64700 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 20 20 20 30   EP_FromJoin   0
64710 78 30 30 30 31 20 20 2f 2a 20 4f 72 69 67 69 6e  x0001  /* Origin
64720 61 74 65 64 20 69 6e 20 4f 4e 20 6f 72 20 55 53  ated in ON or US
64730 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 20 61 20  ING clause of a 
64740 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  join */.#define 
64750 45 50 5f 41 67 67 20 20 20 20 20 20 20 20 30 78  EP_Agg        0x
64760 30 30 30 32 20 20 2f 2a 20 43 6f 6e 74 61 69 6e  0002  /* Contain
64770 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 61 67  s one or more ag
64780 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
64790 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f  s */.#define EP_
647a0 52 65 73 6f 6c 76 65 64 20 20 20 30 78 30 30 30  Resolved   0x000
647b0 34 20 20 2f 2a 20 49 44 73 20 68 61 76 65 20 62  4  /* IDs have b
647c0 65 65 6e 20 72 65 73 6f 6c 76 65 64 20 74 6f 20  een resolved to 
647d0 43 4f 4c 55 4d 4e 73 20 2a 2f 0a 23 64 65 66 69  COLUMNs */.#defi
647e0 6e 65 20 45 50 5f 45 72 72 6f 72 20 20 20 20 20  ne EP_Error     
647f0 20 30 78 30 30 30 38 20 20 2f 2a 20 45 78 70 72   0x0008  /* Expr
64800 65 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20  ession contains 
64810 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 65 72 72 6f  one or more erro
64820 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50  rs */.#define EP
64830 5f 44 69 73 74 69 6e 63 74 20 20 20 30 78 30 30  _Distinct   0x00
64840 31 30 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65  10  /* Aggregate
64850 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 44   function with D
64860 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20  ISTINCT keyword 
64870 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 56 61  */.#define EP_Va
64880 72 53 65 6c 65 63 74 20 20 30 78 30 30 32 30 20  rSelect  0x0020 
64890 20 2f 2a 20 70 53 65 6c 65 63 74 20 69 73 20 63   /* pSelect is c
648a0 6f 72 72 65 6c 61 74 65 64 2c 20 6e 6f 74 20 63  orrelated, not c
648b0 6f 6e 73 74 61 6e 74 20 2a 2f 0a 23 64 65 66 69  onstant */.#defi
648c0 6e 65 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 20  ne EP_DblQuoted 
648d0 20 30 78 30 30 34 30 20 20 2f 2a 20 74 6f 6b 65   0x0040  /* toke
648e0 6e 2e 7a 20 77 61 73 20 6f 72 69 67 69 6e 61 6c  n.z was original
648f0 6c 79 20 69 6e 20 22 2e 2e 2e 22 20 2a 2f 0a 23  ly in "..." */.#
64900 64 65 66 69 6e 65 20 45 50 5f 49 6e 66 69 78 46  define EP_InfixF
64910 75 6e 63 20 20 30 78 30 30 38 30 20 20 2f 2a 20  unc  0x0080  /* 
64920 54 72 75 65 20 66 6f 72 20 61 6e 20 69 6e 66 69  True for an infi
64930 78 20 66 75 6e 63 74 69 6f 6e 3a 20 4c 49 4b 45  x function: LIKE
64940 2c 20 47 4c 4f 42 2c 20 65 74 63 20 2a 2f 0a 23  , GLOB, etc */.#
64950 64 65 66 69 6e 65 20 45 50 5f 45 78 70 43 6f 6c  define EP_ExpCol
64960 6c 61 74 65 20 30 78 30 31 30 30 20 20 2f 2a 20  late 0x0100  /* 
64970 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  Collating sequen
64980 63 65 20 73 70 65 63 69 66 69 65 64 20 65 78 70  ce specified exp
64990 6c 69 63 69 74 6c 79 20 2a 2f 0a 23 64 65 66 69  licitly */.#defi
649a0 6e 65 20 45 50 5f 41 6e 79 41 66 66 20 20 20 20  ne EP_AnyAff    
649b0 20 30 78 30 32 30 30 20 20 2f 2a 20 43 61 6e 20   0x0200  /* Can 
649c0 74 61 6b 65 20 61 20 63 61 63 68 65 64 20 63 6f  take a cached co
649d0 6c 75 6d 6e 20 6f 66 20 61 6e 79 20 61 66 66 69  lumn of any affi
649e0 6e 69 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  nity */.#define 
649f0 45 50 5f 46 69 78 65 64 44 65 73 74 20 20 30 78  EP_FixedDest  0x
64a00 30 34 30 30 20 20 2f 2a 20 52 65 73 75 6c 74 20  0400  /* Result 
64a10 6e 65 65 64 65 64 20 69 6e 20 61 20 73 70 65 63  needed in a spec
64a20 69 66 69 63 20 72 65 67 69 73 74 65 72 20 2a 2f  ific register */
64a30 0a 23 64 65 66 69 6e 65 20 45 50 5f 49 6e 74 56  .#define EP_IntV
64a40 61 6c 75 65 20 20 20 30 78 30 38 30 30 20 20 2f  alue   0x0800  /
64a50 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20  * Integer value 
64a60 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 75 2e 69  contained in u.i
64a70 56 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69 6e 65  Value */.#define
64a80 20 45 50 5f 78 49 73 53 65 6c 65 63 74 20 20 30   EP_xIsSelect  0
64a90 78 31 30 30 30 20 20 2f 2a 20 78 2e 70 53 65 6c  x1000  /* x.pSel
64aa0 65 63 74 20 69 73 20 76 61 6c 69 64 20 28 6f 74  ect is valid (ot
64ab0 68 65 72 77 69 73 65 20 78 2e 70 4c 69 73 74 20  herwise x.pList 
64ac0 69 73 29 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20  is) */..#define 
64ad0 45 50 5f 52 65 64 75 63 65 64 20 20 20 20 30 78  EP_Reduced    0x
64ae0 32 30 30 30 20 20 2f 2a 20 45 78 70 72 20 73 74  2000  /* Expr st
64af0 72 75 63 74 20 69 73 20 45 58 50 52 5f 52 45 44  ruct is EXPR_RED
64b00 55 43 45 44 53 49 5a 45 20 62 79 74 65 73 20 6f  UCEDSIZE bytes o
64b10 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45  nly */.#define E
64b20 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 20 30 78 34  P_TokenOnly  0x4
64b30 30 30 30 20 20 2f 2a 20 45 78 70 72 20 73 74 72  000  /* Expr str
64b40 75 63 74 20 69 73 20 45 58 50 52 5f 54 4f 4b 45  uct is EXPR_TOKE
64b50 4e 4f 4e 4c 59 53 49 5a 45 20 62 79 74 65 73 20  NONLYSIZE bytes 
64b60 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  only */.#define 
64b70 45 50 5f 53 74 61 74 69 63 20 20 20 20 20 30 78  EP_Static     0x
64b80 38 30 30 30 20 20 2f 2a 20 48 65 6c 64 20 69 6e  8000  /* Held in
64b90 20 6d 65 6d 6f 72 79 20 6e 6f 74 20 6f 62 74 61   memory not obta
64ba0 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63  ined from malloc
64bb0 28 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  () */../*.** The
64bc0 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74   following are t
64bd0 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 62  he meanings of b
64be0 69 74 73 20 69 6e 20 74 68 65 20 45 78 70 72 2e  its in the Expr.
64bf0 66 6c 61 67 73 32 20 66 69 65 6c 64 2e 0a 2a 2f  flags2 field..*/
64c00 0a 23 64 65 66 69 6e 65 20 45 50 32 5f 4d 61 6c  .#define EP2_Mal
64c10 6c 6f 63 65 64 54 6f 6b 65 6e 20 20 30 78 30 30  locedToken  0x00
64c20 30 31 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 73  01  /* Need to s
64c30 71 6c 69 74 65 33 44 62 46 72 65 65 28 29 20 45  qlite3DbFree() E
64c40 78 70 72 2e 7a 54 6f 6b 65 6e 20 2a 2f 0a 23 64  xpr.zToken */.#d
64c50 65 66 69 6e 65 20 45 50 32 5f 49 72 72 65 64 75  efine EP2_Irredu
64c60 63 69 62 6c 65 20 20 20 20 30 78 30 30 30 32 20  cible    0x0002 
64c70 20 2f 2a 20 43 61 6e 6e 6f 74 20 45 58 50 52 44   /* Cannot EXPRD
64c80 55 50 5f 52 45 44 55 43 45 20 74 68 69 73 20 45  UP_REDUCE this E
64c90 78 70 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  xpr */../*.** Th
64ca0 65 20 70 73 65 75 64 6f 2d 72 6f 75 74 69 6e 65  e pseudo-routine
64cb0 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 49   sqlite3ExprSetI
64cc0 72 72 65 64 75 63 69 62 6c 65 20 73 65 74 73 20  rreducible sets 
64cd0 74 68 65 20 45 50 32 5f 49 72 72 65 64 75 63 69  the EP2_Irreduci
64ce0 62 6c 65 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 61  ble.** flag on a
64cf0 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 73 74 72  n expression str
64d00 75 63 74 75 72 65 2e 20 20 54 68 69 73 20 66 6c  ucture.  This fl
64d10 61 67 20 69 73 20 75 73 65 64 20 66 6f 72 20 56  ag is used for V
64d20 56 26 41 20 6f 6e 6c 79 2e 20 20 54 68 65 0a 2a  V&A only.  The.*
64d30 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6d 70  * routine is imp
64d40 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20 6d 61  lemented as a ma
64d50 63 72 6f 20 74 68 61 74 20 6f 6e 6c 79 20 77 6f  cro that only wo
64d60 72 6b 73 20 77 68 65 6e 20 69 6e 20 64 65 62 75  rks when in debu
64d70 67 67 69 6e 67 20 6d 6f 64 65 2c 0a 2a 2a 20 73  gging mode,.** s
64d80 6f 20 61 73 20 6e 6f 74 20 74 6f 20 62 75 72 64  o as not to burd
64d90 65 6e 20 70 72 6f 64 75 63 74 69 6f 6e 20 63 6f  en production co
64da0 64 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  de..*/.#ifdef SQ
64db0 4c 49 54 45 5f 44 45 42 55 47 0a 23 20 64 65 66  LITE_DEBUG.# def
64dc0 69 6e 65 20 45 78 70 72 53 65 74 49 72 72 65 64  ine ExprSetIrred
64dd0 75 63 69 62 6c 65 28 58 29 20 20 28 58 29 2d 3e  ucible(X)  (X)->
64de0 66 6c 61 67 73 32 20 7c 3d 20 45 50 32 5f 49 72  flags2 |= EP2_Ir
64df0 72 65 64 75 63 69 62 6c 65 0a 23 65 6c 73 65 0a  reducible.#else.
64e00 23 20 64 65 66 69 6e 65 20 45 78 70 72 53 65 74  # define ExprSet
64e10 49 72 72 65 64 75 63 69 62 6c 65 28 58 29 0a 23  Irreducible(X).#
64e20 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
64e30 73 65 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65  se macros can be
64e40 20 75 73 65 64 20 74 6f 20 74 65 73 74 2c 20 73   used to test, s
64e50 65 74 2c 20 6f 72 20 63 6c 65 61 72 20 62 69 74  et, or clear bit
64e60 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 45 78 70  s in the .** Exp
64e70 72 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a  r.flags field..*
64e80 2f 0a 23 64 65 66 69 6e 65 20 45 78 70 72 48 61  /.#define ExprHa
64e90 73 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20  sProperty(E,P)  
64ea0 20 20 20 28 28 28 45 29 2d 3e 66 6c 61 67 73 26     (((E)->flags&
64eb0 28 50 29 29 3d 3d 28 50 29 29 0a 23 64 65 66 69  (P))==(P)).#defi
64ec0 6e 65 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f  ne ExprHasAnyPro
64ed0 70 65 72 74 79 28 45 2c 50 29 20 20 28 28 28 45  perty(E,P)  (((E
64ee0 29 2d 3e 66 6c 61 67 73 26 28 50 29 29 21 3d 30  )->flags&(P))!=0
64ef0 29 0a 23 64 65 66 69 6e 65 20 45 78 70 72 53 65  ).#define ExprSe
64f00 74 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20  tProperty(E,P)  
64f10 20 20 20 28 45 29 2d 3e 66 6c 61 67 73 7c 3d 28     (E)->flags|=(
64f20 50 29 0a 23 64 65 66 69 6e 65 20 45 78 70 72 43  P).#define ExprC
64f30 6c 65 61 72 50 72 6f 70 65 72 74 79 28 45 2c 50  learProperty(E,P
64f40 29 20 20 20 28 45 29 2d 3e 66 6c 61 67 73 26 3d  )   (E)->flags&=
64f50 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72  ~(P)../*.** Macr
64f60 6f 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  os to determine 
64f70 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
64f80 74 65 73 20 72 65 71 75 69 72 65 64 20 62 79 20  tes required by 
64f90 61 20 6e 6f 72 6d 61 6c 20 45 78 70 72 20 0a 2a  a normal Expr .*
64fa0 2a 20 73 74 72 75 63 74 2c 20 61 6e 20 45 78 70  * struct, an Exp
64fb0 72 20 73 74 72 75 63 74 20 77 69 74 68 20 74 68  r struct with th
64fc0 65 20 45 50 5f 52 65 64 75 63 65 64 20 66 6c 61  e EP_Reduced fla
64fd0 67 20 73 65 74 20 69 6e 20 45 78 70 72 2e 66 6c  g set in Expr.fl
64fe0 61 67 73 20 0a 2a 2a 20 61 6e 64 20 61 6e 20 45  ags .** and an E
64ff0 78 70 72 20 73 74 72 75 63 74 20 77 69 74 68 20  xpr struct with 
65000 74 68 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79  the EP_TokenOnly
65010 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 23 64   flag set..*/.#d
65020 65 66 69 6e 65 20 45 58 50 52 5f 46 55 4c 4c 53  efine EXPR_FULLS
65030 49 5a 45 20 20 20 20 20 20 20 20 20 20 20 73 69  IZE           si
65040 7a 65 6f 66 28 45 78 70 72 29 20 20 20 20 20 20  zeof(Expr)      
65050 20 20 20 20 20 2f 2a 20 46 75 6c 6c 20 73 69 7a       /* Full siz
65060 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50  e */.#define EXP
65070 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 20 20  R_REDUCEDSIZE   
65080 20 20 20 20 20 6f 66 66 73 65 74 6f 66 28 45 78       offsetof(Ex
65090 70 72 2c 69 54 61 62 6c 65 29 20 20 2f 2a 20 43  pr,iTable)  /* C
650a0 6f 6d 6d 6f 6e 20 66 65 61 74 75 72 65 73 20 2a  ommon features *
650b0 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52 5f 54  /.#define EXPR_T
650c0 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20 20 20 20  OKENONLYSIZE    
650d0 20 20 6f 66 66 73 65 74 6f 66 28 45 78 70 72 2c    offsetof(Expr,
650e0 70 4c 65 66 74 29 20 20 20 2f 2a 20 46 65 77 65  pLeft)   /* Fewe
650f0 72 20 66 65 61 74 75 72 65 73 20 2a 2f 0a 0a 2f  r features */../
65100 2a 0a 2a 2a 20 46 6c 61 67 73 20 70 61 73 73 65  *.** Flags passe
65110 64 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  d to the sqlite3
65120 45 78 70 72 44 75 70 28 29 20 66 75 6e 63 74 69  ExprDup() functi
65130 6f 6e 2e 20 53 65 65 20 74 68 65 20 68 65 61 64  on. See the head
65140 65 72 20 63 6f 6d 6d 65 6e 74 20 0a 2a 2a 20 61  er comment .** a
65150 62 6f 76 65 20 73 71 6c 69 74 65 33 45 78 70 72  bove sqlite3Expr
65160 44 75 70 28 29 20 66 6f 72 20 64 65 74 61 69 6c  Dup() for detail
65170 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58  s..*/.#define EX
65180 50 52 44 55 50 5f 52 45 44 55 43 45 20 20 20 20  PRDUP_REDUCE    
65190 20 20 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20       0x0001  /* 
651a0 55 73 65 64 20 72 65 64 75 63 65 64 2d 73 69 7a  Used reduced-siz
651b0 65 20 45 78 70 72 20 6e 6f 64 65 73 20 2a 2f 0a  e Expr nodes */.
651c0 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66  ./*.** A list of
651d0 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 20 45   expressions.  E
651e0 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20 6d  ach expression m
651f0 61 79 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 68 61  ay optionally ha
65200 76 65 20 61 0a 2a 2a 20 6e 61 6d 65 2e 20 20 41  ve a.** name.  A
65210 6e 20 65 78 70 72 2f 6e 61 6d 65 20 63 6f 6d 62  n expr/name comb
65220 69 6e 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 75  ination can be u
65230 73 65 64 20 69 6e 20 73 65 76 65 72 61 6c 20 77  sed in several w
65240 61 79 73 2c 20 73 75 63 68 0a 2a 2a 20 61 73 20  ays, such.** as 
65250 74 68 65 20 6c 69 73 74 20 6f 66 20 22 65 78 70  the list of "exp
65260 72 20 41 53 20 49 44 22 20 66 69 65 6c 64 73 20  r AS ID" fields 
65270 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 22 53 45 4c  following a "SEL
65280 45 43 54 22 20 6f 72 20 69 6e 20 74 68 65 0a 2a  ECT" or in the.*
65290 2a 20 6c 69 73 74 20 6f 66 20 22 49 44 20 3d 20  * list of "ID = 
652a0 65 78 70 72 22 20 69 74 65 6d 73 20 69 6e 20 61  expr" items in a
652b0 6e 20 55 50 44 41 54 45 2e 20 20 41 20 6c 69 73  n UPDATE.  A lis
652c0 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73  t of expressions
652d0 20 63 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65 20   can.** also be 
652e0 75 73 65 64 20 61 73 20 74 68 65 20 61 72 67 75  used as the argu
652f0 6d 65 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 69  ment to a functi
65300 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73  on, in which cas
65310 65 20 74 68 65 20 61 2e 7a 4e 61 6d 65 0a 2a 2a  e the a.zName.**
65320 20 66 69 65 6c 64 20 69 73 20 6e 6f 74 20 75 73   field is not us
65330 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78  ed..*/.struct Ex
65340 70 72 4c 69 73 74 20 7b 0a 20 20 69 6e 74 20 6e  prList {.  int n
65350 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20  Expr;           
65360 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
65370 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e 20 74 68  xpressions on th
65380 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20  e list */.  int 
65390 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20  nAlloc;         
653a0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
653b0 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 74 65  entries allocate
653c0 64 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74  d below */.  int
653d0 20 69 45 43 75 72 73 6f 72 3b 20 20 20 20 20 20   iECursor;      
653e0 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75 72 73      /* VDBE Curs
653f0 6f 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  or associated wi
65400 74 68 20 74 68 69 73 20 45 78 70 72 4c 69 73 74  th this ExprList
65410 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70   */.  struct Exp
65420 72 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20  rList_item {.   
65430 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20   Expr *pExpr;   
65440 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c          /* The l
65450 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f  ist of expressio
65460 6e 73 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a  ns */.    char *
65470 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  zName;          
65480 20 2f 2a 20 54 6f 6b 65 6e 20 61 73 73 6f 63 69   /* Token associ
65490 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 65  ated with this e
654a0 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20  xpression */.   
654b0 20 63 68 61 72 20 2a 7a 53 70 61 6e 3b 20 20 20   char *zSpan;   
654c0 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 69          /* Origi
654d0 6e 61 6c 20 74 65 78 74 20 6f 66 20 74 68 65 20  nal text of the 
654e0 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
654f0 20 20 75 38 20 73 6f 72 74 4f 72 64 65 72 3b 20    u8 sortOrder; 
65500 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66 6f           /* 1 fo
65510 72 20 44 45 53 43 20 6f 72 20 30 20 66 6f 72 20  r DESC or 0 for 
65520 41 53 43 20 2a 2f 0a 20 20 20 20 75 38 20 64 6f  ASC */.    u8 do
65530 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ne;             
65540 20 20 2f 2a 20 41 20 66 6c 61 67 20 74 6f 20 69    /* A flag to i
65550 6e 64 69 63 61 74 65 20 77 68 65 6e 20 70 72 6f  ndicate when pro
65560 63 65 73 73 69 6e 67 20 69 73 20 66 69 6e 69 73  cessing is finis
65570 68 65 64 20 2a 2f 0a 20 20 20 20 75 31 36 20 69  hed */.    u16 i
65580 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
65590 20 20 2f 2a 20 46 6f 72 20 4f 52 44 45 52 20 42    /* For ORDER B
655a0 59 2c 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  Y, column number
655b0 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74 20 2a   in result set *
655c0 2f 0a 20 20 20 20 75 31 36 20 69 41 6c 69 61 73  /.    u16 iAlias
655d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
655e0 49 6e 64 65 78 20 69 6e 74 6f 20 50 61 72 73 65  Index into Parse
655f0 2e 61 41 6c 69 61 73 5b 5d 20 66 6f 72 20 7a 4e  .aAlias[] for zN
65600 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 20 20  ame */.  } *a;  
65610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
65620 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72  /* One entry for
65630 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e   each expression
65640 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
65650 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
65660 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75  s structure is u
65670 73 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65  sed by the parse
65680 72 20 74 6f 20 72 65 63 6f 72 64 20 62 6f 74 68  r to record both
65690 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 20 74 72  .** the parse tr
656a0 65 65 20 66 6f 72 20 61 6e 20 65 78 70 72 65 73  ee for an expres
656b0 73 69 6f 6e 20 61 6e 64 20 74 68 65 20 73 70 61  sion and the spa
656c0 6e 20 6f 66 20 69 6e 70 75 74 20 74 65 78 74 20  n of input text 
656d0 66 6f 72 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73  for an.** expres
656e0 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  sion..*/.struct 
656f0 45 78 70 72 53 70 61 6e 20 7b 0a 20 20 45 78 70  ExprSpan {.  Exp
65700 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20  r *pExpr;       
65710 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73     /* The expres
65720 73 69 6f 6e 20 70 61 72 73 65 20 74 72 65 65 20  sion parse tree 
65730 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
65740 2a 7a 53 74 61 72 74 3b 20 20 20 2f 2a 20 46 69  *zStart;   /* Fi
65750 72 73 74 20 63 68 61 72 61 63 74 65 72 20 6f 66  rst character of
65760 20 69 6e 70 75 74 20 74 65 78 74 20 2a 2f 0a 20   input text */. 
65770 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e   const char *zEn
65780 64 3b 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63 68  d;     /* One ch
65790 61 72 61 63 74 65 72 20 70 61 73 74 20 74 68 65  aracter past the
657a0 20 65 6e 64 20 6f 66 20 69 6e 70 75 74 20 74 65   end of input te
657b0 78 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  xt */.};../*.** 
657c0 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
657d0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63 61  his structure ca
657e0 6e 20 68 6f 6c 64 20 61 20 73 69 6d 70 6c 65 20  n hold a simple 
657f0 6c 69 73 74 20 6f 66 20 69 64 65 6e 74 69 66 69  list of identifi
65800 65 72 73 2c 0a 2a 2a 20 73 75 63 68 20 61 73 20  ers,.** such as 
65810 74 68 65 20 6c 69 73 74 20 22 61 2c 62 2c 63 22  the list "a,b,c"
65820 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   in the followin
65830 67 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a  g statements:.**
65840 0a 2a 2a 20 20 20 20 20 20 49 4e 53 45 52 54 20  .**      INSERT 
65850 49 4e 54 4f 20 74 28 61 2c 62 2c 63 29 20 56 41  INTO t(a,b,c) VA
65860 4c 55 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 20 20 20  LUES ...;.**    
65870 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 69    CREATE INDEX i
65880 64 78 20 4f 4e 20 74 28 61 2c 62 2c 63 29 3b 0a  dx ON t(a,b,c);.
65890 2a 2a 20 20 20 20 20 20 43 52 45 41 54 45 20 54  **      CREATE T
658a0 52 49 47 47 45 52 20 74 72 69 67 20 42 45 46 4f  RIGGER trig BEFO
658b0 52 45 20 55 50 44 41 54 45 20 4f 4e 20 74 28 61  RE UPDATE ON t(a
658c0 2c 62 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a  ,b,c) ...;.**.**
658d0 20 54 68 65 20 49 64 4c 69 73 74 2e 61 2e 69 64   The IdList.a.id
658e0 78 20 66 69 65 6c 64 20 69 73 20 75 73 65 64 20  x field is used 
658f0 77 68 65 6e 20 74 68 65 20 49 64 4c 69 73 74 20  when the IdList 
65900 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 6c  represents the l
65910 69 73 74 20 6f 66 0a 2a 2a 20 63 6f 6c 75 6d 6e  ist of.** column
65920 20 6e 61 6d 65 73 20 61 66 74 65 72 20 61 20 74   names after a t
65930 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 61 6e 20  able name in an 
65940 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74  INSERT statement
65950 2e 20 20 49 6e 20 74 68 65 20 73 74 61 74 65 6d  .  In the statem
65960 65 6e 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e  ent.**.**     IN
65970 53 45 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c  SERT INTO t(a,b,
65980 63 29 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  c) ....**.** If 
65990 22 61 22 20 69 73 20 74 68 65 20 6b 2d 74 68 20  "a" is the k-th 
659a0 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20  column of table 
659b0 22 74 22 2c 20 74 68 65 6e 20 49 64 4c 69 73 74  "t", then IdList
659c0 2e 61 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0a 2a 2f  .a[0].idx==k..*/
659d0 0a 73 74 72 75 63 74 20 49 64 4c 69 73 74 20 7b  .struct IdList {
659e0 0a 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 74  .  struct IdList
659f0 5f 69 74 65 6d 20 7b 0a 20 20 20 20 63 68 61 72  _item {.    char
65a00 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a   *zName;      /*
65a10 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 69 64 65   Name of the ide
65a20 6e 74 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69  ntifier */.    i
65a30 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20  nt idx;         
65a40 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 73 6f 6d   /* Index in som
65a50 65 20 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d 20 6f  e Table.aCol[] o
65a60 66 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64  f a column named
65a70 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61   zName */.  } *a
65a80 3b 0a 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 20  ;.  int nId;    
65a90 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
65aa0 66 20 69 64 65 6e 74 69 66 69 65 72 73 20 6f 6e  f identifiers on
65ab0 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69   the list */.  i
65ac0 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20  nt nAlloc;      
65ad0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
65ae0 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66  ries allocated f
65af0 6f 72 20 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f 0a  or a[] below */.
65b00 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 69  };../*.** The bi
65b10 74 6d 61 73 6b 20 64 61 74 61 74 79 70 65 20 64  tmask datatype d
65b20 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 73 20  efined below is 
65b30 75 73 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73  used for various
65b40 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2e 0a   optimizations..
65b50 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74  **.** Changing t
65b60 68 69 73 20 66 72 6f 6d 20 61 20 36 34 2d 62 69  his from a 64-bi
65b70 74 20 74 6f 20 61 20 33 32 2d 62 69 74 20 74 79  t to a 32-bit ty
65b80 70 65 20 6c 69 6d 69 74 73 20 74 68 65 20 6e 75  pe limits the nu
65b90 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65  mber of.** table
65ba0 73 20 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20 33  s in a join to 3
65bb0 32 20 69 6e 73 74 65 61 64 20 6f 66 20 36 34 2e  2 instead of 64.
65bc0 20 20 42 75 74 20 69 74 20 61 6c 73 6f 20 72 65    But it also re
65bd0 64 75 63 65 73 20 74 68 65 20 73 69 7a 65 0a 2a  duces the size.*
65be0 2a 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79  * of the library
65bf0 20 62 79 20 37 33 38 20 62 79 74 65 73 20 6f 6e   by 738 bytes on
65c00 20 69 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 64 65   ix86..*/.typede
65c10 66 20 75 36 34 20 42 69 74 6d 61 73 6b 3b 0a 0a  f u64 Bitmask;..
65c20 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72  /*.** The number
65c30 20 6f 66 20 62 69 74 73 20 69 6e 20 61 20 42 69   of bits in a Bi
65c40 74 6d 61 73 6b 2e 20 20 22 42 4d 53 22 20 6d 65  tmask.  "BMS" me
65c50 61 6e 73 20 22 42 69 74 4d 61 73 6b 20 53 69 7a  ans "BitMask Siz
65c60 65 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42  e"..*/.#define B
65c70 4d 53 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f  MS  ((int)(sizeo
65c80 66 28 42 69 74 6d 61 73 6b 29 2a 38 29 29 0a 0a  f(Bitmask)*8))..
65c90 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
65ca0 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 64 65  ing structure de
65cb0 73 63 72 69 62 65 73 20 74 68 65 20 46 52 4f 4d  scribes the FROM
65cc0 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c   clause of a SEL
65cd0 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ECT statement..*
65ce0 2a 20 45 61 63 68 20 74 61 62 6c 65 20 6f 72 20  * Each table or 
65cf0 73 75 62 71 75 65 72 79 20 69 6e 20 74 68 65 20  subquery in the 
65d00 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61  FROM clause is a
65d10 20 73 65 70 61 72 61 74 65 20 65 6c 65 6d 65 6e   separate elemen
65d20 74 20 6f 66 0a 2a 2a 20 74 68 65 20 53 72 63 4c  t of.** the SrcL
65d30 69 73 74 2e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a  ist.a[] array..*
65d40 2a 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 61 64  *.** With the ad
65d50 64 69 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69 70  dition of multip
65d60 6c 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70  le database supp
65d70 6f 72 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69  ort, the followi
65d80 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  ng structure.** 
65d90 63 61 6e 20 61 6c 73 6f 20 62 65 20 75 73 65 64  can also be used
65da0 20 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 70   to describe a p
65db0 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20  articular table 
65dc0 73 75 63 68 20 61 73 20 74 68 65 20 74 61 62 6c  such as the tabl
65dd0 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 6d 6f 64  e that.** is mod
65de0 69 66 69 65 64 20 62 79 20 61 6e 20 49 4e 53 45  ified by an INSE
65df0 52 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55  RT, DELETE, or U
65e00 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e  PDATE statement.
65e10 20 20 49 6e 20 73 74 61 6e 64 61 72 64 20 53 51    In standard SQ
65e20 4c 2c 0a 2a 2a 20 73 75 63 68 20 61 20 74 61 62  L,.** such a tab
65e30 6c 65 20 6d 75 73 74 20 62 65 20 61 20 73 69 6d  le must be a sim
65e40 70 6c 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20 42  ple name: ID.  B
65e50 75 74 20 69 6e 20 53 51 4c 69 74 65 2c 20 74 68  ut in SQLite, th
65e60 65 20 74 61 62 6c 65 20 63 61 6e 0a 2a 2a 20 6e  e table can.** n
65e70 6f 77 20 62 65 20 69 64 65 6e 74 69 66 69 65 64  ow be identified
65e80 20 62 79 20 61 20 64 61 74 61 62 61 73 65 20 6e   by a database n
65e90 61 6d 65 2c 20 61 20 64 6f 74 2c 20 74 68 65 6e  ame, a dot, then
65ea0 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 3a   the table name:
65eb0 20 49 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68   ID.ID..**.** Th
65ec0 65 20 6a 6f 69 6e 74 79 70 65 20 73 74 61 72 74  e jointype start
65ed0 73 20 6f 75 74 20 73 68 6f 77 69 6e 67 20 74 68  s out showing th
65ee0 65 20 6a 6f 69 6e 20 74 79 70 65 20 62 65 74 77  e join type betw
65ef0 65 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20  een the current 
65f00 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65  table.** and the
65f10 20 6e 65 78 74 20 74 61 62 6c 65 20 6f 6e 20 74   next table on t
65f20 68 65 20 6c 69 73 74 2e 20 20 54 68 65 20 70 61  he list.  The pa
65f30 72 73 65 72 20 62 75 69 6c 64 73 20 74 68 65 20  rser builds the 
65f40 6c 69 73 74 20 74 68 69 73 20 77 61 79 2e 0a 2a  list this way..*
65f50 2a 20 42 75 74 20 73 71 6c 69 74 65 33 53 72 63  * But sqlite3Src
65f60 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70  ListShiftJoinTyp
65f70 65 28 29 20 6c 61 74 65 72 20 73 68 69 66 74 73  e() later shifts
65f80 20 74 68 65 20 6a 6f 69 6e 74 79 70 65 73 20 73   the jointypes s
65f90 6f 20 74 68 61 74 20 65 61 63 68 0a 2a 2a 20 6a  o that each.** j
65fa0 6f 69 6e 74 79 70 65 20 65 78 70 72 65 73 73 65  ointype expresse
65fb0 73 20 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65  s the join betwe
65fc0 65 6e 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64  en the table and
65fd0 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 74 61   the previous ta
65fe0 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53  ble..*/.struct S
65ff0 72 63 4c 69 73 74 20 7b 0a 20 20 69 31 36 20 6e  rcList {.  i16 n
66000 53 72 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  Src;        /* N
66010 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20  umber of tables 
66020 6f 72 20 73 75 62 71 75 65 72 69 65 73 20 69 6e  or subqueries in
66030 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
66040 20 2a 2f 0a 20 20 69 31 36 20 6e 41 6c 6c 6f 63   */.  i16 nAlloc
66050 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  ;      /* Number
66060 20 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f   of entries allo
66070 63 61 74 65 64 20 69 6e 20 61 5b 5d 20 62 65 6c  cated in a[] bel
66080 6f 77 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53  ow */.  struct S
66090 72 63 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20  rcList_item {.  
660a0 20 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73    char *zDatabas
660b0 65 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64  e;  /* Name of d
660c0 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20  atabase holding 
660d0 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20  this table */.  
660e0 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
660f0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
66100 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  he table */.    
66110 63 68 61 72 20 2a 7a 41 6c 69 61 73 3b 20 20 20  char *zAlias;   
66120 20 20 2f 2a 20 54 68 65 20 22 42 22 20 70 61 72    /* The "B" par
66130 74 20 6f 66 20 61 20 22 41 20 41 53 20 42 22 20  t of a "A AS B" 
66140 70 68 72 61 73 65 2e 20 20 7a 4e 61 6d 65 20 69  phrase.  zName i
66150 73 20 74 68 65 20 22 41 22 20 2a 2f 0a 20 20 20  s the "A" */.   
66160 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20   Table *pTab;   
66170 20 20 20 2f 2a 20 41 6e 20 53 51 4c 20 74 61 62     /* An SQL tab
66180 6c 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  le corresponding
66190 20 74 6f 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20   to zName */.   
661a0 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
661b0 3b 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73  ;  /* A SELECT s
661c0 74 61 74 65 6d 65 6e 74 20 75 73 65 64 20 69 6e  tatement used in
661d0 20 70 6c 61 63 65 20 6f 66 20 61 20 74 61 62 6c   place of a tabl
661e0 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 75 38  e name */.    u8
661f0 20 69 73 50 6f 70 75 6c 61 74 65 64 3b 20 20 20   isPopulated;   
66200 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 74 61 62  /* Temporary tab
66210 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  le associated wi
66220 74 68 20 53 45 4c 45 43 54 20 69 73 20 70 6f 70  th SELECT is pop
66230 75 6c 61 74 65 64 20 2a 2f 0a 20 20 20 20 75 38  ulated */.    u8
66240 20 6a 6f 69 6e 74 79 70 65 3b 20 20 20 20 20 20   jointype;      
66250 2f 2a 20 54 79 70 65 20 6f 66 20 6a 6f 69 6e 20  /* Type of join 
66260 62 65 74 77 65 65 6e 20 74 68 69 73 20 61 62 6c  between this abl
66270 65 20 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f  e and the previo
66280 75 73 20 2a 2f 0a 20 20 20 20 75 38 20 6e 6f 74  us */.    u8 not
66290 49 6e 64 65 78 65 64 3b 20 20 20 20 2f 2a 20 54  Indexed;    /* T
662a0 72 75 65 20 69 66 20 74 68 65 72 65 20 69 73 20  rue if there is 
662b0 61 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c  a NOT INDEXED cl
662c0 61 75 73 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ause */.    int 
662d0 69 43 75 72 73 6f 72 3b 20 20 20 20 20 20 2f 2a  iCursor;      /*
662e0 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72   The VDBE cursor
662f0 20 6e 75 6d 62 65 72 20 75 73 65 64 20 74 6f 20   number used to 
66300 61 63 63 65 73 73 20 74 68 69 73 20 74 61 62 6c  access this tabl
66310 65 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70  e */.    Expr *p
66320 4f 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68  On;        /* Th
66330 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61  e ON clause of a
66340 20 6a 6f 69 6e 20 2a 2f 0a 20 20 20 20 49 64 4c   join */.    IdL
66350 69 73 74 20 2a 70 55 73 69 6e 67 3b 20 20 20 2f  ist *pUsing;   /
66360 2a 20 54 68 65 20 55 53 49 4e 47 20 63 6c 61 75  * The USING clau
66370 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a  se of a join */.
66380 20 20 20 20 42 69 74 6d 61 73 6b 20 63 6f 6c 55      Bitmask colU
66390 73 65 64 3b 20 20 2f 2a 20 42 69 74 20 4e 20 28  sed;  /* Bit N (
663a0 31 3c 3c 4e 29 20 73 65 74 20 69 66 20 63 6f 6c  1<<N) set if col
663b0 75 6d 6e 20 4e 20 6f 66 20 70 54 61 62 20 69 73  umn N of pTab is
663c0 20 75 73 65 64 20 2a 2f 0a 20 20 20 20 63 68 61   used */.    cha
663d0 72 20 2a 7a 49 6e 64 65 78 3b 20 20 20 20 20 2f  r *zIndex;     /
663e0 2a 20 49 64 65 6e 74 69 66 69 65 72 20 66 72 6f  * Identifier fro
663f0 6d 20 22 49 4e 44 45 58 45 44 20 42 59 20 3c 7a  m "INDEXED BY <z
66400 49 6e 64 65 78 3e 22 20 63 6c 61 75 73 65 20 2a  Index>" clause *
66410 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e  /.    Index *pIn
66420 64 65 78 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78  dex;    /* Index
66430 20 73 74 72 75 63 74 75 72 65 20 63 6f 72 72 65   structure corre
66440 73 70 6f 6e 64 69 6e 67 20 74 6f 20 7a 49 6e 64  sponding to zInd
66450 65 78 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20  ex, if any */.  
66460 7d 20 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20  } a[1];         
66470 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79      /* One entry
66480 20 66 6f 72 20 65 61 63 68 20 69 64 65 6e 74 69   for each identi
66490 66 69 65 72 20 6f 6e 20 74 68 65 20 6c 69 73 74  fier on the list
664a0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65   */.};../*.** Pe
664b0 72 6d 69 74 74 65 64 20 76 61 6c 75 65 73 20 6f  rmitted values o
664c0 66 20 74 68 65 20 53 72 63 4c 69 73 74 2e 61 2e  f the SrcList.a.
664d0 6a 6f 69 6e 74 79 70 65 20 66 69 65 6c 64 0a 2a  jointype field.*
664e0 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 49 4e 4e  /.#define JT_INN
664f0 45 52 20 20 20 20 20 30 78 30 30 30 31 20 20 20  ER     0x0001   
66500 20 2f 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66 20   /* Any kind of 
66510 69 6e 6e 65 72 20 6f 72 20 63 72 6f 73 73 20 6a  inner or cross j
66520 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a  oin */.#define J
66530 54 5f 43 52 4f 53 53 20 20 20 20 20 30 78 30 30  T_CROSS     0x00
66540 30 32 20 20 20 20 2f 2a 20 45 78 70 6c 69 63 69  02    /* Explici
66550 74 20 75 73 65 20 6f 66 20 74 68 65 20 43 52 4f  t use of the CRO
66560 53 53 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64  SS keyword */.#d
66570 65 66 69 6e 65 20 4a 54 5f 4e 41 54 55 52 41 4c  efine JT_NATURAL
66580 20 20 20 30 78 30 30 30 34 20 20 20 20 2f 2a 20     0x0004    /* 
66590 54 72 75 65 20 66 6f 72 20 61 20 22 6e 61 74 75  True for a "natu
665a0 72 61 6c 22 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65  ral" join */.#de
665b0 66 69 6e 65 20 4a 54 5f 4c 45 46 54 20 20 20 20  fine JT_LEFT    
665c0 20 20 30 78 30 30 30 38 20 20 20 20 2f 2a 20 4c    0x0008    /* L
665d0 65 66 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a  eft outer join *
665e0 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 52 49 47  /.#define JT_RIG
665f0 48 54 20 20 20 20 20 30 78 30 30 31 30 20 20 20  HT     0x0010   
66600 20 2f 2a 20 52 69 67 68 74 20 6f 75 74 65 72 20   /* Right outer 
66610 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  join */.#define 
66620 4a 54 5f 4f 55 54 45 52 20 20 20 20 20 30 78 30  JT_OUTER     0x0
66630 30 32 30 20 20 20 20 2f 2a 20 54 68 65 20 22 4f  020    /* The "O
66640 55 54 45 52 22 20 6b 65 79 77 6f 72 64 20 69 73  UTER" keyword is
66650 20 70 72 65 73 65 6e 74 20 2a 2f 0a 23 64 65 66   present */.#def
66660 69 6e 65 20 4a 54 5f 45 52 52 4f 52 20 20 20 20  ine JT_ERROR    
66670 20 30 78 30 30 34 30 20 20 20 20 2f 2a 20 75 6e   0x0040    /* un
66680 6b 6e 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70 6f  known or unsuppo
66690 72 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 20 2a  rted join type *
666a0 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 72  /.../*.** A Wher
666b0 65 50 6c 61 6e 20 6f 62 6a 65 63 74 20 68 6f 6c  ePlan object hol
666c0 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74  ds information t
666d0 68 61 74 20 64 65 73 63 72 69 62 65 73 20 61 20  hat describes a 
666e0 6c 6f 6f 6b 75 70 0a 2a 2a 20 73 74 72 61 74 65  lookup.** strate
666f0 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f  gy..**.** This o
66700 62 6a 65 63 74 20 69 73 20 69 6e 74 65 6e 64 65  bject is intende
66710 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65 20 6f  d to be opaque o
66720 75 74 73 69 64 65 20 6f 66 20 74 68 65 20 77 68  utside of the wh
66730 65 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a 2a  ere.c module..**
66740 20 49 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20   It is included 
66750 68 65 72 65 20 6f 6e 6c 79 20 73 6f 20 74 68 61  here only so tha
66760 74 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 20  t that compiler 
66770 77 69 6c 6c 20 6b 6e 6f 77 20 68 6f 77 20 62 69  will know how bi
66780 67 20 69 74 0a 2a 2a 20 69 73 2e 20 20 4e 6f 6e  g it.** is.  Non
66790 65 20 6f 66 20 74 68 65 20 66 69 65 6c 64 73 20  e of the fields 
667a0 69 6e 20 74 68 69 73 20 6f 62 6a 65 63 74 20 73  in this object s
667b0 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 6f 75  hould be used ou
667c0 74 73 69 64 65 20 6f 66 0a 2a 2a 20 74 68 65 20  tside of.** the 
667d0 77 68 65 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a  where.c module..
667e0 2a 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 65  **.** Within the
667f0 20 75 6e 69 6f 6e 2c 20 70 49 64 78 20 69 73 20   union, pIdx is 
66800 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77  only used when w
66810 73 46 6c 61 67 73 26 57 48 45 52 45 5f 49 4e 44  sFlags&WHERE_IND
66820 45 58 45 44 20 69 73 20 74 72 75 65 2e 0a 2a 2a  EXED is true..**
66830 20 70 54 65 72 6d 20 69 73 20 6f 6e 6c 79 20 75   pTerm is only u
66840 73 65 64 20 77 68 65 6e 20 77 73 46 6c 61 67 73  sed when wsFlags
66850 26 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20  &WHERE_MULTI_OR 
66860 69 73 20 74 72 75 65 2e 20 20 41 6e 64 20 70 56  is true.  And pV
66870 74 61 62 49 64 78 0a 2a 2a 20 69 73 20 6f 6e 6c  tabIdx.** is onl
66880 79 20 75 73 65 64 20 77 68 65 6e 20 77 73 46 6c  y used when wsFl
66890 61 67 73 26 57 48 45 52 45 5f 56 49 52 54 55 41  ags&WHERE_VIRTUA
668a0 4c 54 41 42 4c 45 20 69 73 20 74 72 75 65 2e 20  LTABLE is true. 
668b0 20 49 74 20 69 73 20 6e 65 76 65 72 20 74 68 65   It is never the
668c0 0a 2a 2a 20 63 61 73 65 20 74 68 61 74 20 6d 6f  .** case that mo
668d0 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20 74  re than one of t
668e0 68 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20  hese conditions 
668f0 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 72 75  is true..*/.stru
66900 63 74 20 57 68 65 72 65 50 6c 61 6e 20 7b 0a 20  ct WherePlan {. 
66910 20 75 33 32 20 77 73 46 6c 61 67 73 3b 20 20 20   u32 wsFlags;   
66920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
66930 2f 2a 20 57 48 45 52 45 5f 2a 20 66 6c 61 67 73  /* WHERE_* flags
66940 20 74 68 61 74 20 64 65 73 63 72 69 62 65 20 74   that describe t
66950 68 65 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 20  he strategy */. 
66960 20 75 33 32 20 6e 45 71 3b 20 20 20 20 20 20 20   u32 nEq;       
66970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
66980 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 3d 3d 20  /* Number of == 
66990 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20  constraints */. 
669a0 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 49 6e 64   union {.    Ind
669b0 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20  ex *pIdx;       
669c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
669d0 6e 64 65 78 20 77 68 65 6e 20 57 48 45 52 45 5f  ndex when WHERE_
669e0 49 4e 44 45 58 45 44 20 69 73 20 74 72 75 65 20  INDEXED is true 
669f0 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20 57 68  */.    struct Wh
66a00 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20  ereTerm *pTerm; 
66a10 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63        /* WHERE c
66a20 6c 61 75 73 65 20 74 65 72 6d 20 66 6f 72 20 4f  lause term for O
66a30 52 2d 73 65 61 72 63 68 20 2a 2f 0a 20 20 20 20  R-search */.    
66a40 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
66a50 66 6f 20 2a 70 56 74 61 62 49 64 78 3b 20 20 2f  fo *pVtabIdx;  /
66a60 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20  * Virtual table 
66a70 69 6e 64 65 78 20 74 6f 20 75 73 65 20 2a 2f 0a  index to use */.
66a80 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a    } u;.};../*.**
66a90 20 46 6f 72 20 65 61 63 68 20 6e 65 73 74 65 64   For each nested
66aa0 20 6c 6f 6f 70 20 69 6e 20 61 20 57 48 45 52 45   loop in a WHERE
66ab0 20 63 6c 61 75 73 65 20 69 6d 70 6c 65 6d 65 6e   clause implemen
66ac0 74 61 74 69 6f 6e 2c 20 74 68 65 20 57 68 65 72  tation, the Wher
66ad0 65 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75  eInfo.** structu
66ae0 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69  re contains a si
66af0 6e 67 6c 65 20 69 6e 73 74 61 6e 63 65 20 6f 66  ngle instance of
66b00 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e   this structure.
66b10 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65    This structure
66b20 0a 2a 2a 20 69 73 20 69 6e 74 65 6e 64 65 64 20  .** is intended 
66b30 74 6f 20 62 65 20 70 72 69 76 61 74 65 20 74 68  to be private th
66b40 65 20 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f  e the where.c mo
66b50 64 75 6c 65 20 61 6e 64 20 73 68 6f 75 6c 64 20  dule and should 
66b60 6e 6f 74 20 62 65 0a 2a 2a 20 61 63 63 65 73 73  not be.** access
66b70 20 6f 72 20 6d 6f 64 69 66 69 65 64 20 62 79 20   or modified by 
66b80 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0a 2a  other modules..*
66b90 2a 0a 2a 2a 20 54 68 65 20 70 49 64 78 49 6e 66  *.** The pIdxInf
66ba0 6f 20 66 69 65 6c 64 20 69 73 20 75 73 65 64 20  o field is used 
66bb0 74 6f 20 68 65 6c 70 20 70 69 63 6b 20 74 68 65  to help pick the
66bc0 20 62 65 73 74 20 69 6e 64 65 78 20 6f 6e 20 61   best index on a
66bd0 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c  .** virtual tabl
66be0 65 2e 20 20 54 68 65 20 70 49 64 78 49 6e 66 6f  e.  The pIdxInfo
66bf0 20 70 6f 69 6e 74 65 72 20 63 6f 6e 74 61 69 6e   pointer contain
66c00 73 20 69 6e 64 65 78 69 6e 67 0a 2a 2a 20 69 6e  s indexing.** in
66c10 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68  formation for th
66c20 65 20 69 2d 74 68 20 74 61 62 6c 65 20 69 6e 20  e i-th table in 
66c30 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
66c40 62 65 66 6f 72 65 20 72 65 6f 72 64 65 72 69 6e  before reorderin
66c50 67 2e 0a 2a 2a 20 41 6c 6c 20 74 68 65 20 70 49  g..** All the pI
66c60 64 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 73 20  dxInfo pointers 
66c70 61 72 65 20 66 72 65 65 64 20 62 79 20 77 68 65  are freed by whe
66c80 72 65 49 6e 66 6f 46 72 65 65 28 29 20 69 6e 20  reInfoFree() in 
66c90 77 68 65 72 65 2e 63 2e 0a 2a 2a 20 41 6c 6c 20  where.c..** All 
66ca0 6f 74 68 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f  other informatio
66cb0 6e 20 69 6e 20 74 68 65 20 69 2d 74 68 20 57 68  n in the i-th Wh
66cc0 65 72 65 4c 65 76 65 6c 20 6f 62 6a 65 63 74 20  ereLevel object 
66cd0 66 6f 72 20 74 68 65 20 69 2d 74 68 20 74 61 62  for the i-th tab
66ce0 6c 65 0a 2a 2a 20 61 66 74 65 72 20 46 52 4f 4d  le.** after FROM
66cf0 20 63 6c 61 75 73 65 20 6f 72 64 65 72 69 6e 67   clause ordering
66d00 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72  ..*/.struct Wher
66d10 65 4c 65 76 65 6c 20 7b 0a 20 20 57 68 65 72 65  eLevel {.  Where
66d20 50 6c 61 6e 20 70 6c 61 6e 3b 20 20 20 20 20 20  Plan plan;      
66d30 20 2f 2a 20 71 75 65 72 79 20 70 6c 61 6e 20 66   /* query plan f
66d40 6f 72 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20  or this element 
66d50 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  of the FROM clau
66d60 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 66  se */.  int iLef
66d70 74 4a 6f 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a  tJoin;        /*
66d80 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65   Memory cell use
66d90 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 4c  d to implement L
66da0 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 2a  EFT OUTER JOIN *
66db0 2f 0a 20 20 69 6e 74 20 69 54 61 62 43 75 72 3b  /.  int iTabCur;
66dc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
66dd0 20 56 44 42 45 20 63 75 72 73 6f 72 20 75 73 65   VDBE cursor use
66de0 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20  d to access the 
66df0 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69  table */.  int i
66e00 49 64 78 43 75 72 3b 20 20 20 20 20 20 20 20 20  IdxCur;         
66e10 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72   /* The VDBE cur
66e20 73 6f 72 20 75 73 65 64 20 74 6f 20 61 63 63 65  sor used to acce
66e30 73 73 20 70 49 64 78 20 2a 2f 0a 20 20 69 6e 74  ss pIdx */.  int
66e40 20 61 64 64 72 42 72 6b 3b 20 20 20 20 20 20 20   addrBrk;       
66e50 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20     /* Jump here 
66e60 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20  to break out of 
66e70 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e  the loop */.  in
66e80 74 20 61 64 64 72 4e 78 74 3b 20 20 20 20 20 20  t addrNxt;      
66e90 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65      /* Jump here
66ea0 20 74 6f 20 73 74 61 72 74 20 74 68 65 20 6e 65   to start the ne
66eb0 78 74 20 49 4e 20 63 6f 6d 62 69 6e 61 74 69 6f  xt IN combinatio
66ec0 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 43  n */.  int addrC
66ed0 6f 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ont;         /* 
66ee0 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e  Jump here to con
66ef0 74 69 6e 75 65 20 77 69 74 68 20 74 68 65 20 6e  tinue with the n
66f00 65 78 74 20 6c 6f 6f 70 20 63 79 63 6c 65 20 2a  ext loop cycle *
66f10 2f 0a 20 20 69 6e 74 20 61 64 64 72 46 69 72 73  /.  int addrFirs
66f20 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72  t;        /* Fir
66f30 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f  st instruction o
66f40 66 20 69 6e 74 65 72 69 6f 72 20 6f 66 20 74 68  f interior of th
66f50 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 75 38 20 69  e loop */.  u8 i
66f60 46 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20  From;           
66f70 20 20 2f 2a 20 57 68 69 63 68 20 65 6e 74 72 79    /* Which entry
66f80 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
66f90 75 73 65 20 2a 2f 0a 20 20 75 38 20 6f 70 2c 20  use */.  u8 op, 
66fa0 70 35 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  p5;            /
66fb0 2a 20 4f 70 63 6f 64 65 20 61 6e 64 20 50 35 20  * Opcode and P5 
66fc0 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 20 74 68  of the opcode th
66fd0 61 74 20 65 6e 64 73 20 74 68 65 20 6c 6f 6f 70  at ends the loop
66fe0 20 2a 2f 0a 20 20 69 6e 74 20 70 31 2c 20 70 32   */.  int p1, p2
66ff0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  ;           /* O
67000 70 65 72 61 6e 64 73 20 6f 66 20 74 68 65 20 6f  perands of the o
67010 70 63 6f 64 65 20 75 73 65 64 20 74 6f 20 65 6e  pcode used to en
67020 64 73 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  ds the loop */. 
67030 20 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20   union {        
67040 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d         /* Inform
67050 61 74 69 6f 6e 20 74 68 61 74 20 64 65 70 65 6e  ation that depen
67060 64 73 20 6f 6e 20 70 6c 61 6e 2e 77 73 46 6c 61  ds on plan.wsFla
67070 67 73 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 74  gs */.    struct
67080 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 6e   {.      int nIn
67090 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
670a0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
670b0 69 65 73 20 69 6e 20 61 49 6e 4c 6f 6f 70 5b 5d  ies in aInLoop[]
670c0 20 2a 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74   */.      struct
670d0 20 49 6e 4c 6f 6f 70 20 7b 0a 20 20 20 20 20 20   InLoop {.      
670e0 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20    int iCur;     
670f0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
67100 56 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 64  VDBE cursor used
67110 20 62 79 20 74 68 69 73 20 49 4e 20 6f 70 65 72   by this IN oper
67120 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20  ator */.        
67130 69 6e 74 20 61 64 64 72 49 6e 54 6f 70 3b 20 20  int addrInTop;  
67140 20 20 20 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66         /* Top of
67150 20 74 68 65 20 49 4e 20 6c 6f 6f 70 20 2a 2f 0a   the IN loop */.
67160 20 20 20 20 20 20 7d 20 2a 61 49 6e 4c 6f 6f 70        } *aInLoop
67170 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  ;           /* I
67180 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
67190 20 65 61 63 68 20 6e 65 73 74 65 64 20 49 4e 20   each nested IN 
671a0 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20  operator */.    
671b0 7d 20 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20  } in;           
671c0 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68        /* Used wh
671d0 65 6e 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73 26  en plan.wsFlags&
671e0 57 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20 2a 2f  WHERE_IN_ABLE */
671f0 0a 20 20 7d 20 75 3b 0a 0a 20 20 2f 2a 20 54 68  .  } u;..  /* Th
67200 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c  e following fiel
67210 64 20 69 73 20 72 65 61 6c 6c 79 20 6e 6f 74 20  d is really not 
67220 70 61 72 74 20 6f 66 20 74 68 65 20 63 75 72 72  part of the curr
67230 65 6e 74 20 6c 65 76 65 6c 2e 20 20 42 75 74 0a  ent level.  But.
67240 20 20 2a 2a 20 77 65 20 6e 65 65 64 20 61 20 70    ** we need a p
67250 6c 61 63 65 20 74 6f 20 63 61 63 68 65 20 76 69  lace to cache vi
67260 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 64 65  rtual table inde
67270 78 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f  x information fo
67280 72 20 65 61 63 68 0a 20 20 2a 2a 20 76 69 72 74  r each.  ** virt
67290 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 74 68 65  ual table in the
672a0 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 6e 64   FROM clause and
672b0 20 74 68 65 20 57 68 65 72 65 4c 65 76 65 6c 20   the WhereLevel 
672c0 73 74 72 75 63 74 75 72 65 20 69 73 0a 20 20 2a  structure is.  *
672d0 2a 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 70  * a convenient p
672e0 6c 61 63 65 20 73 69 6e 63 65 20 74 68 65 72 65  lace since there
672f0 20 69 73 20 6f 6e 65 20 57 68 65 72 65 4c 65 76   is one WhereLev
67300 65 6c 20 66 6f 72 20 65 61 63 68 20 46 52 4f 4d  el for each FROM
67310 20 63 6c 61 75 73 65 0a 20 20 2a 2a 20 65 6c 65   clause.  ** ele
67320 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  ment..  */.  sql
67330 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
67340 2a 70 49 64 78 49 6e 66 6f 3b 20 20 2f 2a 20 49  *pIdxInfo;  /* I
67350 6e 64 65 78 20 69 6e 66 6f 20 66 6f 72 20 6e 2d  ndex info for n-
67360 74 68 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20  th source table 
67370 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6c 61  */.};../*.** Fla
67380 67 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66  gs appropriate f
67390 6f 72 20 74 68 65 20 77 63 74 72 6c 46 6c 61 67  or the wctrlFlag
673a0 73 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73  s parameter of s
673b0 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
673c0 28 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 57 68  ().** and the Wh
673d0 65 72 65 49 6e 66 6f 2e 77 63 74 72 6c 46 6c 61  ereInfo.wctrlFla
673e0 67 73 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64  gs member..*/.#d
673f0 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45  efine WHERE_ORDE
67400 52 42 59 5f 4e 4f 52 4d 41 4c 20 20 20 30 78 30  RBY_NORMAL   0x0
67410 30 30 30 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a  000 /* No-op */.
67420 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52  #define WHERE_OR
67430 44 45 52 42 59 5f 4d 49 4e 20 20 20 20 20 20 30  DERBY_MIN      0
67440 78 30 30 30 31 20 2f 2a 20 4f 52 44 45 52 20 42  x0001 /* ORDER B
67450 59 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72  Y processing for
67460 20 6d 69 6e 28 29 20 66 75 6e 63 20 2a 2f 0a 23   min() func */.#
67470 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44  define WHERE_ORD
67480 45 52 42 59 5f 4d 41 58 20 20 20 20 20 20 30 78  ERBY_MAX      0x
67490 30 30 30 32 20 2f 2a 20 4f 52 44 45 52 20 42 59  0002 /* ORDER BY
674a0 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20   processing for 
674b0 6d 61 78 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64  max() func */.#d
674c0 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 4e 45 50  efine WHERE_ONEP
674d0 41 53 53 5f 44 45 53 49 52 45 44 20 20 30 78 30  ASS_DESIRED  0x0
674e0 30 30 34 20 2f 2a 20 57 61 6e 74 20 74 6f 20 64  004 /* Want to d
674f0 6f 20 6f 6e 65 2d 70 61 73 73 20 55 50 44 41 54  o one-pass UPDAT
67500 45 2f 44 45 4c 45 54 45 20 2a 2f 0a 23 64 65 66  E/DELETE */.#def
67510 69 6e 65 20 57 48 45 52 45 5f 44 55 50 4c 49 43  ine WHERE_DUPLIC
67520 41 54 45 53 5f 4f 4b 20 20 20 20 30 78 30 30 30  ATES_OK    0x000
67530 38 20 2f 2a 20 4f 6b 20 74 6f 20 72 65 74 75 72  8 /* Ok to retur
67540 6e 20 61 20 72 6f 77 20 6d 6f 72 65 20 74 68 61  n a row more tha
67550 6e 20 6f 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e  n once */.#defin
67560 65 20 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45  e WHERE_OMIT_OPE
67570 4e 20 20 20 20 20 20 20 20 30 78 30 30 31 30 20  N        0x0010 
67580 2f 2a 20 54 61 62 6c 65 20 63 75 72 73 6f 72 20  /* Table cursor 
67590 61 72 65 20 61 6c 72 65 61 64 79 20 6f 70 65 6e  are already open
675a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52   */.#define WHER
675b0 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45 20 20 20 20  E_OMIT_CLOSE    
675c0 20 20 20 30 78 30 30 32 30 20 2f 2a 20 4f 6d 69     0x0020 /* Omi
675d0 74 20 63 6c 6f 73 65 20 6f 66 20 74 61 62 6c 65  t close of table
675e0 20 26 20 69 6e 64 65 78 20 63 75 72 73 6f 72 73   & index cursors
675f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52   */.#define WHER
67600 45 5f 46 4f 52 43 45 5f 54 41 42 4c 45 20 20 20  E_FORCE_TABLE   
67610 20 20 20 30 78 30 30 34 30 20 2f 2a 20 44 6f 20     0x0040 /* Do 
67620 6e 6f 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78  not use an index
67630 2d 6f 6e 6c 79 20 73 65 61 72 63 68 20 2a 2f 0a  -only search */.
67640 0a 2f 2a 0a 2a 2a 20 54 68 65 20 57 48 45 52 45  ./*.** The WHERE
67650 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69   clause processi
67660 6e 67 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74  ng routine has t
67670 77 6f 20 68 61 6c 76 65 73 2e 20 20 54 68 65 0a  wo halves.  The.
67680 2a 2a 20 66 69 72 73 74 20 70 61 72 74 20 64 6f  ** first part do
67690 65 73 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  es the start of 
676a0 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 61  the WHERE loop a
676b0 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a  nd the second.**
676c0 20 68 61 6c 66 20 64 6f 65 73 20 74 68 65 20 74   half does the t
676d0 61 69 6c 20 6f 66 20 74 68 65 20 57 48 45 52 45  ail of the WHERE
676e0 20 6c 6f 6f 70 2e 20 20 41 6e 20 69 6e 73 74 61   loop.  An insta
676f0 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73  nce of.** this s
67700 74 72 75 63 74 75 72 65 20 69 73 20 72 65 74 75  tructure is retu
67710 72 6e 65 64 20 62 79 20 74 68 65 20 66 69 72 73  rned by the firs
67720 74 20 68 61 6c 66 20 61 6e 64 20 70 61 73 73 65  t half and passe
67730 64 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 65  d.** into the se
67740 63 6f 6e 64 20 68 61 6c 66 20 74 6f 20 67 69 76  cond half to giv
67750 65 20 73 6f 6d 65 20 63 6f 6e 74 69 6e 75 69 74  e some continuit
67760 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65  y..*/.struct Whe
67770 72 65 49 6e 66 6f 20 7b 0a 20 20 50 61 72 73 65  reInfo {.  Parse
67780 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20   *pParse;       
67790 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63  /* Parsing and c
677a0 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63  ode generating c
677b0 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 75 31 36 20  ontext */.  u16 
677c0 77 63 74 72 6c 46 6c 61 67 73 3b 20 20 20 20 20  wctrlFlags;     
677d0 20 2f 2a 20 46 6c 61 67 73 20 6f 72 69 67 69 6e   /* Flags origin
677e0 61 6c 6c 79 20 70 61 73 73 65 64 20 74 6f 20 73  ally passed to s
677f0 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
67800 28 29 20 2a 2f 0a 20 20 75 38 20 6f 6b 4f 6e 65  () */.  u8 okOne
67810 50 61 73 73 3b 20 20 20 20 20 20 20 20 2f 2a 20  Pass;        /* 
67820 4f 6b 20 74 6f 20 75 73 65 20 6f 6e 65 2d 70 61  Ok to use one-pa
67830 73 73 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72  ss algorithm for
67840 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54   UPDATE or DELET
67850 45 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  E */.  SrcList *
67860 70 54 61 62 4c 69 73 74 3b 20 20 20 20 20 20 20  pTabList;       
67870 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66        /* List of
67880 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6a   tables in the j
67890 6f 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f  oin */.  int iTo
678a0 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
678b0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76          /* The v
678c0 65 72 79 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  ery beginning of
678d0 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20   the WHERE loop 
678e0 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e  */.  int iContin
678f0 75 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ue;             
67900 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65      /* Jump here
67910 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74   to continue wit
67920 68 20 6e 65 78 74 20 72 65 63 6f 72 64 20 2a 2f  h next record */
67930 0a 20 20 69 6e 74 20 69 42 72 65 61 6b 3b 20 20  .  int iBreak;  
67940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67950 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
67960 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74  o break out of t
67970 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74  he loop */.  int
67980 20 6e 4c 65 76 65 6c 3b 20 20 20 20 20 20 20 20   nLevel;        
67990 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
679a0 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20  umber of nested 
679b0 6c 6f 6f 70 20 2a 2f 0a 20 20 73 74 72 75 63 74  loop */.  struct
679c0 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57   WhereClause *pW
679d0 43 3b 20 20 20 20 20 20 20 2f 2a 20 44 65 63 6f  C;       /* Deco
679e0 6d 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65  mposition of the
679f0 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
67a00 0a 20 20 57 68 65 72 65 4c 65 76 65 6c 20 61 5b  .  WhereLevel a[
67a10 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  1];             
67a20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e    /* Information
67a30 20 61 62 6f 75 74 20 65 61 63 68 20 6e 65 73 74   about each nest
67a40 20 6c 6f 6f 70 20 69 6e 20 57 48 45 52 45 20 2a   loop in WHERE *
67a50 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 61  /.};../*.** A Na
67a60 6d 65 43 6f 6e 74 65 78 74 20 64 65 66 69 6e 65  meContext define
67a70 73 20 61 20 63 6f 6e 74 65 78 74 20 69 6e 20 77  s a context in w
67a80 68 69 63 68 20 74 6f 20 72 65 73 6f 6c 76 65 20  hich to resolve 
67a90 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e  table and column
67aa0 0a 2a 2a 20 6e 61 6d 65 73 2e 20 20 54 68 65 20  .** names.  The 
67ab0 63 6f 6e 74 65 78 74 20 63 6f 6e 73 69 73 74 73  context consists
67ac0 20 6f 66 20 61 20 6c 69 73 74 20 6f 66 20 74 61   of a list of ta
67ad0 62 6c 65 73 20 28 74 68 65 20 70 53 72 63 4c 69  bles (the pSrcLi
67ae0 73 74 29 20 66 69 65 6c 64 20 61 6e 64 0a 2a 2a  st) field and.**
67af0 20 61 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64   a list of named
67b00 20 65 78 70 72 65 73 73 69 6f 6e 20 28 70 45 4c   expression (pEL
67b10 69 73 74 29 2e 20 20 54 68 65 20 6e 61 6d 65 64  ist).  The named
67b20 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
67b30 20 6d 61 79 0a 2a 2a 20 62 65 20 4e 55 4c 4c 2e   may.** be NULL.
67b40 20 20 54 68 65 20 70 53 72 63 20 63 6f 72 72 65    The pSrc corre
67b50 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 46 52  sponds to the FR
67b60 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53  OM clause of a S
67b70 45 4c 45 43 54 20 6f 72 0a 2a 2a 20 74 6f 20 74  ELECT or.** to t
67b80 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6f  he table being o
67b90 70 65 72 61 74 65 64 20 6f 6e 20 62 79 20 49 4e  perated on by IN
67ba0 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72  SERT, UPDATE, or
67bb0 20 44 45 4c 45 54 45 2e 20 20 54 68 65 0a 2a 2a   DELETE.  The.**
67bc0 20 70 45 4c 69 73 74 20 63 6f 72 72 65 73 70 6f   pEList correspo
67bd0 6e 64 73 20 74 6f 20 74 68 65 20 72 65 73 75 6c  nds to the resul
67be0 74 20 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43  t set of a SELEC
67bf0 54 20 61 6e 64 20 69 73 20 4e 55 4c 4c 20 66 6f  T and is NULL fo
67c00 72 0a 2a 2a 20 6f 74 68 65 72 20 73 74 61 74 65  r.** other state
67c10 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4e 61 6d  ments..**.** Nam
67c20 65 43 6f 6e 74 65 78 74 73 20 63 61 6e 20 62 65  eContexts can be
67c30 20 6e 65 73 74 65 64 2e 20 20 57 68 65 6e 20 72   nested.  When r
67c40 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 2c 20  esolving names, 
67c50 74 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 0a  the inner-most .
67c60 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 20 73 65  ** context is se
67c70 61 72 63 68 65 64 20 66 69 72 73 74 2e 20 20 49  arched first.  I
67c80 66 20 6e 6f 20 6d 61 74 63 68 20 69 73 20 66 6f  f no match is fo
67c90 75 6e 64 2c 20 74 68 65 20 6e 65 78 74 20 6f 75  und, the next ou
67ca0 74 65 72 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69  ter.** context i
67cb0 73 20 63 68 65 63 6b 65 64 2e 20 20 49 66 20 74  s checked.  If t
67cc0 68 65 72 65 20 69 73 20 73 74 69 6c 6c 20 6e 6f  here is still no
67cd0 20 6d 61 74 63 68 2c 20 74 68 65 20 6e 65 78 74   match, the next
67ce0 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 69 73 20 63   context.** is c
67cf0 68 65 63 6b 65 64 2e 20 20 54 68 69 73 20 70 72  hecked.  This pr
67d00 6f 63 65 73 73 20 63 6f 6e 74 69 6e 75 65 73 20  ocess continues 
67d10 75 6e 74 69 6c 20 65 69 74 68 65 72 20 61 20 6d  until either a m
67d20 61 74 63 68 20 69 73 20 66 6f 75 6e 64 0a 2a 2a  atch is found.**
67d30 20 6f 72 20 61 6c 6c 20 63 6f 6e 74 65 78 74 73   or all contexts
67d40 20 61 72 65 20 63 68 65 63 6b 2e 20 20 57 68 65   are check.  Whe
67d50 6e 20 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75  n a match is fou
67d60 6e 64 2c 20 74 68 65 20 6e 52 65 66 20 6d 65 6d  nd, the nRef mem
67d70 62 65 72 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f  ber of.** the co
67d80 6e 74 65 78 74 20 63 6f 6e 74 61 69 6e 69 6e 67  ntext containing
67d90 20 74 68 65 20 6d 61 74 63 68 20 69 73 20 69 6e   the match is in
67da0 63 72 65 6d 65 6e 74 65 64 2e 20 0a 2a 2a 0a 2a  cremented. .**.*
67db0 2a 20 45 61 63 68 20 73 75 62 71 75 65 72 79 20  * Each subquery 
67dc0 67 65 74 73 20 61 20 6e 65 77 20 4e 61 6d 65 43  gets a new NameC
67dd0 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 70 4e 65  ontext.  The pNe
67de0 78 74 20 66 69 65 6c 64 20 70 6f 69 6e 74 73 20  xt field points 
67df0 74 6f 20 74 68 65 0a 2a 2a 20 4e 61 6d 65 43 6f  to the.** NameCo
67e00 6e 74 65 78 74 20 69 6e 20 74 68 65 20 70 61 72  ntext in the par
67e10 65 6e 74 20 71 75 65 72 79 2e 20 20 54 68 75 73  ent query.  Thus
67e20 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66 20   the process of 
67e30 73 63 61 6e 6e 69 6e 67 20 74 68 65 0a 2a 2a 20  scanning the.** 
67e40 4e 61 6d 65 43 6f 6e 74 65 78 74 20 6c 69 73 74  NameContext list
67e50 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20   corresponds to 
67e60 73 65 61 72 63 68 69 6e 67 20 74 68 72 6f 75 67  searching throug
67e70 68 20 73 75 63 63 65 73 73 69 76 65 6c 79 20 6f  h successively o
67e80 75 74 65 72 0a 2a 2a 20 73 75 62 71 75 65 72 69  uter.** subqueri
67e90 65 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61  es looking for a
67ea0 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73 74 72 75 63   match..*/.struc
67eb0 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 7b 0a  t NameContext {.
67ec0 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b    Parse *pParse;
67ed0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
67ee0 72 73 65 72 20 2a 2f 0a 20 20 53 72 63 4c 69 73  rser */.  SrcLis
67ef0 74 20 2a 70 53 72 63 4c 69 73 74 3b 20 20 20 2f  t *pSrcList;   /
67f00 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 61  * One or more ta
67f10 62 6c 65 73 20 75 73 65 64 20 74 6f 20 72 65 73  bles used to res
67f20 6f 6c 76 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20  olve names */.  
67f30 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
67f40 3b 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c  ;    /* Optional
67f50 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64 20 65   list of named e
67f60 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20  xpressions */.  
67f70 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20  int nRef;       
67f80 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
67f90 66 20 6e 61 6d 65 73 20 72 65 73 6f 6c 76 65 64  f names resolved
67fa0 20 62 79 20 74 68 69 73 20 63 6f 6e 74 65 78 74   by this context
67fb0 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20   */.  int nErr; 
67fc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
67fd0 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65  mber of errors e
67fe0 6e 63 6f 75 6e 74 65 72 65 64 20 77 68 69 6c 65  ncountered while
67ff0 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73   resolving names
68000 20 2a 2f 0a 20 20 75 38 20 61 6c 6c 6f 77 41 67   */.  u8 allowAg
68010 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 67  g;         /* Ag
68020 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
68030 73 20 61 6c 6c 6f 77 65 64 20 68 65 72 65 20 2a  s allowed here *
68040 2f 0a 20 20 75 38 20 68 61 73 41 67 67 3b 20 20  /.  u8 hasAgg;  
68050 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
68060 20 69 66 20 61 67 67 72 65 67 61 74 65 73 20 61   if aggregates a
68070 72 65 20 73 65 65 6e 20 2a 2f 0a 20 20 75 38 20  re seen */.  u8 
68080 69 73 43 68 65 63 6b 3b 20 20 20 20 20 20 20 20  isCheck;        
68090 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72 65 73    /* True if res
680a0 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e 20  olving names in 
680b0 61 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69  a CHECK constrai
680c0 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 65 70  nt */.  int nDep
680d0 74 68 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  th;          /* 
680e0 44 65 70 74 68 20 6f 66 20 73 75 62 71 75 65 72  Depth of subquer
680f0 79 20 72 65 63 75 72 73 69 6f 6e 2e 20 31 20 66  y recursion. 1 f
68100 6f 72 20 6e 6f 20 72 65 63 75 72 73 69 6f 6e 20  or no recursion 
68110 2a 2f 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41  */.  AggInfo *pA
68120 67 67 49 6e 66 6f 3b 20 20 20 2f 2a 20 49 6e 66  ggInfo;   /* Inf
68130 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61  ormation about a
68140 67 67 72 65 67 61 74 65 73 20 61 74 20 74 68 69  ggregates at thi
68150 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 4e 61 6d  s level */.  Nam
68160 65 43 6f 6e 74 65 78 74 20 2a 70 4e 65 78 74 3b  eContext *pNext;
68170 20 20 2f 2a 20 4e 65 78 74 20 6f 75 74 65 72 20    /* Next outer 
68180 6e 61 6d 65 20 63 6f 6e 74 65 78 74 2e 20 20 4e  name context.  N
68190 55 4c 4c 20 66 6f 72 20 6f 75 74 65 72 6d 6f 73  ULL for outermos
681a0 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  t */.};../*.** A
681b0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
681c0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
681d0 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61  cture contains a
681e0 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a  ll information.*
681f0 2a 20 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65  * needed to gene
68200 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20  rate code for a 
68210 73 69 6e 67 6c 65 20 53 45 4c 45 43 54 20 73 74  single SELECT st
68220 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 6e  atement..**.** n
68230 4c 69 6d 69 74 20 69 73 20 73 65 74 20 74 6f 20  Limit is set to 
68240 2d 31 20 69 66 20 74 68 65 72 65 20 69 73 20 6e  -1 if there is n
68250 6f 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 20  o LIMIT clause. 
68260 20 6e 4f 66 66 73 65 74 20 69 73 20 73 65 74 20   nOffset is set 
68270 74 6f 20 30 2e 0a 2a 2a 20 49 66 20 74 68 65 72  to 0..** If ther
68280 65 20 69 73 20 61 20 4c 49 4d 49 54 20 63 6c 61  e is a LIMIT cla
68290 75 73 65 2c 20 74 68 65 20 70 61 72 73 65 72 20  use, the parser 
682a0 73 65 74 73 20 6e 4c 69 6d 69 74 20 74 6f 20 74  sets nLimit to t
682b0 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a  he value of the.
682c0 2a 2a 20 6c 69 6d 69 74 20 61 6e 64 20 6e 4f 66  ** limit and nOf
682d0 66 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75  fset to the valu
682e0 65 20 6f 66 20 74 68 65 20 6f 66 66 73 65 74 20  e of the offset 
682f0 28 6f 72 20 30 20 69 66 20 74 68 65 72 65 20 69  (or 0 if there i
68300 73 20 6e 6f 74 0a 2a 2a 20 6f 66 66 73 65 74 29  s not.** offset)
68310 2e 20 20 42 75 74 20 6c 61 74 65 72 20 6f 6e 2c  .  But later on,
68320 20 6e 4c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66   nLimit and nOff
68330 73 65 74 20 62 65 63 6f 6d 65 20 74 68 65 20 6d  set become the m
68340 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 0a  emory locations.
68350 2a 2a 20 69 6e 20 74 68 65 20 56 44 42 45 20 74  ** in the VDBE t
68360 68 61 74 20 72 65 63 6f 72 64 20 74 68 65 20 6c  hat record the l
68370 69 6d 69 74 20 61 6e 64 20 6f 66 66 73 65 74 20  imit and offset 
68380 63 6f 75 6e 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20  counters..**.** 
68390 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 5d 20 65  addrOpenEphm[] e
683a0 6e 74 72 69 65 73 20 63 6f 6e 74 61 69 6e 20 74  ntries contain t
683b0 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 4f 50  he address of OP
683c0 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 6f  _OpenEphemeral o
683d0 70 63 6f 64 65 73 2e 0a 2a 2a 20 54 68 65 73 65  pcodes..** These
683e0 20 61 64 64 72 65 73 73 65 73 20 6d 75 73 74 20   addresses must 
683f0 62 65 20 73 74 6f 72 65 64 20 73 6f 20 74 68 61  be stored so tha
68400 74 20 77 65 20 63 61 6e 20 67 6f 20 62 61 63 6b  t we can go back
68410 20 61 6e 64 20 66 69 6c 6c 20 69 6e 0a 2a 2a 20   and fill in.** 
68420 74 68 65 20 50 34 5f 4b 45 59 49 4e 46 4f 20 61  the P4_KEYINFO a
68430 6e 64 20 50 32 20 70 61 72 61 6d 65 74 65 72 73  nd P2 parameters
68440 20 6c 61 74 65 72 2e 20 20 4e 65 69 74 68 65 72   later.  Neither
68450 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 6e 6f 72   the KeyInfo nor
68460 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  .** the number o
68470 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 50 32 20  f columns in P2 
68480 63 61 6e 20 62 65 20 63 6f 6d 70 75 74 65 64 20  can be computed 
68490 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  at the same time
684a0 0a 2a 2a 20 61 73 20 74 68 65 20 4f 50 5f 4f 70  .** as the OP_Op
684b0 65 6e 45 70 68 6d 20 69 6e 73 74 72 75 63 74 69  enEphm instructi
684c0 6f 6e 20 69 73 20 63 6f 64 65 64 20 62 65 63 61  on is coded beca
684d0 75 73 65 20 6e 6f 74 0a 2a 2a 20 65 6e 6f 75 67  use not.** enoug
684e0 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  h information ab
684f0 6f 75 74 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64  out the compound
68500 20 71 75 65 72 79 20 69 73 20 6b 6e 6f 77 6e 20   query is known 
68510 61 74 20 74 68 61 74 20 70 6f 69 6e 74 2e 0a 2a  at that point..*
68520 2a 20 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f  * The KeyInfo fo
68530 72 20 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b 30  r addrOpenTran[0
68540 5d 20 61 6e 64 20 5b 31 5d 20 63 6f 6e 74 61 69  ] and [1] contai
68550 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  ns collating seq
68560 75 65 6e 63 65 73 0a 2a 2a 20 66 6f 72 20 74 68  uences.** for th
68570 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 54  e result set.  T
68580 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 61  he KeyInfo for a
68590 64 64 72 4f 70 65 6e 54 72 61 6e 5b 32 5d 20 63  ddrOpenTran[2] c
685a0 6f 6e 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e  ontains collatin
685b0 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 73 20 66  g.** sequences f
685c0 6f 72 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  or the ORDER BY 
685d0 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73 74 72 75 63  clause..*/.struc
685e0 74 20 53 65 6c 65 63 74 20 7b 0a 20 20 45 78 70  t Select {.  Exp
685f0 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20  rList *pEList;  
68600 20 20 20 20 2f 2a 20 54 68 65 20 66 69 65 6c 64      /* The field
68610 73 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20  s of the result 
68620 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20  */.  u8 op;     
68630 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
68640 6e 65 20 6f 66 3a 20 54 4b 5f 55 4e 49 4f 4e 20  ne of: TK_UNION 
68650 54 4b 5f 41 4c 4c 20 54 4b 5f 49 4e 54 45 52 53  TK_ALL TK_INTERS
68660 45 43 54 20 54 4b 5f 45 58 43 45 50 54 20 2a 2f  ECT TK_EXCEPT */
68670 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79  .  char affinity
68680 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b  ;         /* Mak
68690 65 52 65 63 6f 72 64 20 77 69 74 68 20 74 68 69  eRecord with thi
686a0 73 20 61 66 66 69 6e 69 74 79 20 66 6f 72 20 53  s affinity for S
686b0 52 54 5f 53 65 74 20 2a 2f 0a 20 20 75 31 36 20  RT_Set */.  u16 
686c0 73 65 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 20  selFlags;       
686d0 20 20 20 2f 2a 20 56 61 72 69 6f 75 73 20 53 46     /* Various SF
686e0 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 53  _* values */.  S
686f0 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 20  rcList *pSrc;   
68700 20 20 20 20 20 20 2f 2a 20 54 68 65 20 46 52 4f        /* The FRO
68710 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78  M clause */.  Ex
68720 70 72 20 2a 70 57 68 65 72 65 3b 20 20 20 20 20  pr *pWhere;     
68730 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52       /* The WHER
68740 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78  E clause */.  Ex
68750 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79  prList *pGroupBy
68760 3b 20 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55  ;    /* The GROU
68770 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20  P BY clause */. 
68780 20 45 78 70 72 20 2a 70 48 61 76 69 6e 67 3b 20   Expr *pHaving; 
68790 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 48          /* The H
687a0 41 56 49 4e 47 20 63 6c 61 75 73 65 20 2a 2f 0a  AVING clause */.
687b0 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64    ExprList *pOrd
687c0 65 72 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20  erBy;    /* The 
687d0 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
687e0 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 72  */.  Select *pPr
687f0 69 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 50  ior;        /* P
68800 72 69 6f 72 20 73 65 6c 65 63 74 20 69 6e 20 61  rior select in a
68810 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74   compound select
68820 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
68830 53 65 6c 65 63 74 20 2a 70 4e 65 78 74 3b 20 20  Select *pNext;  
68840 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 73         /* Next s
68850 65 6c 65 63 74 20 74 6f 20 74 68 65 20 6c 65 66  elect to the lef
68860 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20  t in a compound 
68870 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 52 69  */.  Select *pRi
68880 67 68 74 6d 6f 73 74 3b 20 20 20 20 2f 2a 20 52  ghtmost;    /* R
68890 69 67 68 74 2d 6d 6f 73 74 20 73 65 6c 65 63 74  ight-most select
688a0 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73   in a compound s
688b0 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20  elect statement 
688c0 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69  */.  Expr *pLimi
688d0 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  t;          /* L
688e0 49 4d 49 54 20 65 78 70 72 65 73 73 69 6f 6e 2e  IMIT expression.
688f0 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20   NULL means not 
68900 75 73 65 64 2e 20 2a 2f 0a 20 20 45 78 70 72 20  used. */.  Expr 
68910 2a 70 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20  *pOffset;       
68920 20 20 2f 2a 20 4f 46 46 53 45 54 20 65 78 70 72    /* OFFSET expr
68930 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61  ession. NULL mea
68940 6e 73 20 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a  ns not used. */.
68950 20 20 69 6e 74 20 69 4c 69 6d 69 74 2c 20 69 4f    int iLimit, iO
68960 66 66 73 65 74 3b 20 20 20 2f 2a 20 4d 65 6d 6f  ffset;   /* Memo
68970 72 79 20 72 65 67 69 73 74 65 72 73 20 68 6f 6c  ry registers hol
68980 64 69 6e 67 20 4c 49 4d 49 54 20 26 20 4f 46 46  ding LIMIT & OFF
68990 53 45 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a  SET counters */.
689a0 20 20 69 6e 74 20 61 64 64 72 4f 70 65 6e 45 70    int addrOpenEp
689b0 68 6d 5b 33 5d 3b 20 20 20 2f 2a 20 4f 50 5f 4f  hm[3];   /* OP_O
689c0 70 65 6e 45 70 68 65 6d 20 6f 70 63 6f 64 65 73  penEphem opcodes
689d0 20 72 65 6c 61 74 65 64 20 74 6f 20 74 68 69 73   related to this
689e0 20 73 65 6c 65 63 74 20 2a 2f 0a 7d 3b 0a 0a 2f   select */.};../
689f0 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c  *.** Allowed val
68a00 75 65 73 20 66 6f 72 20 53 65 6c 65 63 74 2e 73  ues for Select.s
68a10 65 6c 46 6c 61 67 73 2e 20 20 54 68 65 20 22 53  elFlags.  The "S
68a20 46 22 20 70 72 65 66 69 78 20 73 74 61 6e 64 73  F" prefix stands
68a30 20 66 6f 72 0a 2a 2a 20 22 53 65 6c 65 63 74 20   for.** "Select 
68a40 46 6c 61 67 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e  Flag"..*/.#defin
68a50 65 20 53 46 5f 44 69 73 74 69 6e 63 74 20 20 20  e SF_Distinct   
68a60 20 20 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20       0x0001  /* 
68a70 4f 75 74 70 75 74 20 73 68 6f 75 6c 64 20 62 65  Output should be
68a80 20 44 49 53 54 49 4e 43 54 20 2a 2f 0a 23 64 65   DISTINCT */.#de
68a90 66 69 6e 65 20 53 46 5f 52 65 73 6f 6c 76 65 64  fine SF_Resolved
68aa0 20 20 20 20 20 20 20 20 30 78 30 30 30 32 20 20          0x0002  
68ab0 2f 2a 20 49 64 65 6e 74 69 66 69 65 72 73 20 68  /* Identifiers h
68ac0 61 76 65 20 62 65 65 6e 20 72 65 73 6f 6c 76 65  ave been resolve
68ad0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f  d */.#define SF_
68ae0 41 67 67 72 65 67 61 74 65 20 20 20 20 20 20 20  Aggregate       
68af0 30 78 30 30 30 34 20 20 2f 2a 20 43 6f 6e 74 61  0x0004  /* Conta
68b00 69 6e 73 20 61 67 67 72 65 67 61 74 65 20 66 75  ins aggregate fu
68b10 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69  nctions */.#defi
68b20 6e 65 20 53 46 5f 55 73 65 73 45 70 68 65 6d 65  ne SF_UsesEpheme
68b30 72 61 6c 20 20 20 30 78 30 30 30 38 20 20 2f 2a  ral   0x0008  /*
68b40 20 55 73 65 73 20 74 68 65 20 4f 70 65 6e 45 70   Uses the OpenEp
68b50 68 65 6d 65 72 61 6c 20 6f 70 63 6f 64 65 20 2a  hemeral opcode *
68b60 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 45 78 70  /.#define SF_Exp
68b70 61 6e 64 65 64 20 20 20 20 20 20 20 20 30 78 30  anded        0x0
68b80 30 31 30 20 20 2f 2a 20 73 71 6c 69 74 65 33 53  010  /* sqlite3S
68b90 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 63 61  electExpand() ca
68ba0 6c 6c 65 64 20 6f 6e 20 74 68 69 73 20 2a 2f 0a  lled on this */.
68bb0 23 64 65 66 69 6e 65 20 53 46 5f 48 61 73 54 79  #define SF_HasTy
68bc0 70 65 49 6e 66 6f 20 20 20 20 20 30 78 30 30 32  peInfo     0x002
68bd0 30 20 20 2f 2a 20 46 52 4f 4d 20 73 75 62 71 75  0  /* FROM subqu
68be0 65 72 69 65 73 20 68 61 76 65 20 54 61 62 6c 65  eries have Table
68bf0 20 6d 65 74 61 64 61 74 61 20 2a 2f 0a 0a 0a 2f   metadata */.../
68c00 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73  *.** The results
68c10 20 6f 66 20 61 20 73 65 6c 65 63 74 20 63 61 6e   of a select can
68c20 20 62 65 20 64 69 73 74 72 69 62 75 74 65 64 20   be distributed 
68c30 69 6e 20 73 65 76 65 72 61 6c 20 77 61 79 73 2e  in several ways.
68c40 20 20 54 68 65 0a 2a 2a 20 22 53 52 54 22 20 70    The.** "SRT" p
68c50 72 65 66 69 78 20 6d 65 61 6e 73 20 22 53 45 4c  refix means "SEL
68c60 45 43 54 20 52 65 73 75 6c 74 20 54 79 70 65 22  ECT Result Type"
68c70 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54  ..*/.#define SRT
68c80 5f 55 6e 69 6f 6e 20 20 20 20 20 20 20 20 31 20  _Union        1 
68c90 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74   /* Store result
68ca0 20 61 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 69   as keys in an i
68cb0 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ndex */.#define 
68cc0 53 52 54 5f 45 78 63 65 70 74 20 20 20 20 20 20  SRT_Except      
68cd0 20 32 20 20 2f 2a 20 52 65 6d 6f 76 65 20 72 65   2  /* Remove re
68ce0 73 75 6c 74 20 66 72 6f 6d 20 61 20 55 4e 49 4f  sult from a UNIO
68cf0 4e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69  N index */.#defi
68d00 6e 65 20 53 52 54 5f 45 78 69 73 74 73 20 20 20  ne SRT_Exists   
68d10 20 20 20 20 33 20 20 2f 2a 20 53 74 6f 72 65 20      3  /* Store 
68d20 31 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 20  1 if the result 
68d30 69 73 20 6e 6f 74 20 65 6d 70 74 79 20 2a 2f 0a  is not empty */.
68d40 23 64 65 66 69 6e 65 20 53 52 54 5f 44 69 73 63  #define SRT_Disc
68d50 61 72 64 20 20 20 20 20 20 34 20 20 2f 2a 20 44  ard      4  /* D
68d60 6f 20 6e 6f 74 20 73 61 76 65 20 74 68 65 20 72  o not save the r
68d70 65 73 75 6c 74 73 20 61 6e 79 77 68 65 72 65 20  esults anywhere 
68d80 2a 2f 0a 0a 2f 2a 20 54 68 65 20 4f 52 44 45 52  */../* The ORDER
68d90 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 69 67   BY clause is ig
68da0 6e 6f 72 65 64 20 66 6f 72 20 61 6c 6c 20 6f 66  nored for all of
68db0 20 74 68 65 20 61 62 6f 76 65 20 2a 2f 0a 23 64   the above */.#d
68dc0 65 66 69 6e 65 20 49 67 6e 6f 72 61 62 6c 65 4f  efine IgnorableO
68dd0 72 64 65 72 62 79 28 58 29 20 28 28 58 2d 3e 65  rderby(X) ((X->e
68de0 44 65 73 74 29 3c 3d 53 52 54 5f 44 69 73 63 61  Dest)<=SRT_Disca
68df0 72 64 29 0a 0a 23 64 65 66 69 6e 65 20 53 52 54  rd)..#define SRT
68e00 5f 4f 75 74 70 75 74 20 20 20 20 20 20 20 35 20  _Output       5 
68e10 20 2f 2a 20 4f 75 74 70 75 74 20 65 61 63 68 20   /* Output each 
68e20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f  row of result */
68e30 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 4d 65 6d  .#define SRT_Mem
68e40 20 20 20 20 20 20 20 20 20 20 36 20 20 2f 2a 20            6  /* 
68e50 53 74 6f 72 65 20 72 65 73 75 6c 74 20 69 6e 20  Store result in 
68e60 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 2a 2f  a memory cell */
68e70 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 53 65 74  .#define SRT_Set
68e80 20 20 20 20 20 20 20 20 20 20 37 20 20 2f 2a 20            7  /* 
68e90 53 74 6f 72 65 20 72 65 73 75 6c 74 73 20 61 73  Store results as
68ea0 20 6b 65 79 73 20 69 6e 20 61 6e 20 69 6e 64 65   keys in an inde
68eb0 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54  x */.#define SRT
68ec0 5f 54 61 62 6c 65 20 20 20 20 20 20 20 20 38 20  _Table        8 
68ed0 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74   /* Store result
68ee0 20 61 73 20 64 61 74 61 20 77 69 74 68 20 61 6e   as data with an
68ef0 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 77 69 64   automatic rowid
68f00 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f   */.#define SRT_
68f10 45 70 68 65 6d 54 61 62 20 20 20 20 20 39 20 20  EphemTab     9  
68f20 2f 2a 20 43 72 65 61 74 65 20 74 72 61 6e 73 69  /* Create transi
68f30 65 6e 74 20 74 61 62 20 61 6e 64 20 73 74 6f 72  ent tab and stor
68f40 65 20 6c 69 6b 65 20 53 52 54 5f 54 61 62 6c 65  e like SRT_Table
68f50 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f   */.#define SRT_
68f60 43 6f 72 6f 75 74 69 6e 65 20 20 20 31 30 20 20  Coroutine   10  
68f70 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73 69  /* Generate a si
68f80 6e 67 6c 65 20 72 6f 77 20 6f 66 20 72 65 73 75  ngle row of resu
68f90 6c 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73  lt */../*.** A s
68fa0 74 72 75 63 74 75 72 65 20 75 73 65 64 20 74 6f  tructure used to
68fb0 20 63 75 73 74 6f 6d 69 7a 65 20 74 68 65 20 62   customize the b
68fc0 65 68 61 76 69 6f 72 20 6f 66 20 73 71 6c 69 74  ehavior of sqlit
68fd0 65 33 53 65 6c 65 63 74 28 29 2e 20 53 65 65 0a  e3Select(). See.
68fe0 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76  ** comments abov
68ff0 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28  e sqlite3Select(
69000 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a  ) for details..*
69010 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
69020 20 53 65 6c 65 63 74 44 65 73 74 20 53 65 6c 65   SelectDest Sele
69030 63 74 44 65 73 74 3b 0a 73 74 72 75 63 74 20 53  ctDest;.struct S
69040 65 6c 65 63 74 44 65 73 74 20 7b 0a 20 20 75 38  electDest {.  u8
69050 20 65 44 65 73 74 3b 20 20 20 20 20 20 20 20 20   eDest;         
69060 2f 2a 20 48 6f 77 20 74 6f 20 64 69 73 70 6f 73  /* How to dispos
69070 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 73  e of the results
69080 20 2a 2f 0a 20 20 75 38 20 61 66 66 69 6e 69 74   */.  u8 affinit
69090 79 3b 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e  y;      /* Affin
690a0 69 74 79 20 75 73 65 64 20 77 68 65 6e 20 65 44  ity used when eD
690b0 65 73 74 3d 3d 53 52 54 5f 53 65 74 20 2a 2f 0a  est==SRT_Set */.
690c0 20 20 69 6e 74 20 69 50 61 72 6d 3b 20 20 20 20    int iParm;    
690d0 20 20 20 20 2f 2a 20 41 20 70 61 72 61 6d 65 74      /* A paramet
690e0 65 72 20 75 73 65 64 20 62 79 20 74 68 65 20 65  er used by the e
690f0 44 65 73 74 20 64 69 73 70 6f 73 61 6c 20 6d 65  Dest disposal me
69100 74 68 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 69 4d  thod */.  int iM
69110 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42  em;         /* B
69120 61 73 65 20 72 65 67 69 73 74 65 72 20 77 68 65  ase register whe
69130 72 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 77  re results are w
69140 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20  ritten */.  int 
69150 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a  nMem;         /*
69160 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73   Number of regis
69170 74 65 72 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a  ters allocated *
69180 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 44 75 72 69  /.};../*.** Duri
69190 6e 67 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69  ng code generati
691a0 6f 6e 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 73  on of statements
691b0 20 74 68 61 74 20 64 6f 20 69 6e 73 65 72 74 73   that do inserts
691c0 20 69 6e 74 6f 20 41 55 54 4f 49 4e 43 52 45 4d   into AUTOINCREM
691d0 45 4e 54 20 0a 2a 2a 20 74 61 62 6c 65 73 2c 20  ENT .** tables, 
691e0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e  the following in
691f0 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 61 74 74  formation is att
69200 61 63 68 65 64 20 74 6f 20 74 68 65 20 54 61 62  ached to the Tab
69210 6c 65 2e 75 2e 61 75 74 6f 49 6e 63 2e 70 0a 2a  le.u.autoInc.p.*
69220 2a 20 70 6f 69 6e 74 65 72 20 6f 66 20 65 61 63  * pointer of eac
69230 68 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20  h autoincrement 
69240 74 61 62 6c 65 20 74 6f 20 72 65 63 6f 72 64 20  table to record 
69250 73 6f 6d 65 20 73 69 64 65 20 69 6e 66 6f 72 6d  some side inform
69260 61 74 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 74 68  ation that.** th
69270 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72  e code generator
69280 20 6e 65 65 64 73 2e 20 20 57 65 20 68 61 76 65   needs.  We have
69290 20 74 6f 20 6b 65 65 70 20 70 65 72 2d 74 61 62   to keep per-tab
692a0 6c 65 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74  le autoincrement
692b0 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  .** information 
692c0 69 6e 20 63 61 73 65 20 69 6e 73 65 72 74 73 20  in case inserts 
692d0 61 72 65 20 64 6f 77 6e 20 77 69 74 68 69 6e 20  are down within 
692e0 74 72 69 67 67 65 72 73 2e 20 20 54 72 69 67 67  triggers.  Trigg
692f0 65 72 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 6e 6f  ers do not.** no
69300 72 6d 61 6c 6c 79 20 63 6f 6f 72 64 69 6e 61 74  rmally coordinat
69310 65 20 74 68 65 69 72 20 61 63 74 69 76 69 74 69  e their activiti
69320 65 73 2c 20 62 75 74 20 77 65 20 64 6f 20 6e 65  es, but we do ne
69330 65 64 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 65  ed to coordinate
69340 20 74 68 65 0a 2a 2a 20 6c 6f 61 64 69 6e 67 20   the.** loading 
69350 61 6e 64 20 73 61 76 69 6e 67 20 6f 66 20 61 75  and saving of au
69360 74 6f 69 6e 63 72 65 6d 65 6e 74 20 69 6e 66 6f  toincrement info
69370 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75  rmation..*/.stru
69380 63 74 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 7b  ct AutoincInfo {
69390 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 2a  .  AutoincInfo *
693a0 70 4e 65 78 74 3b 20 20 20 2f 2a 20 4e 65 78 74  pNext;   /* Next
693b0 20 69 6e 66 6f 20 62 6c 6f 63 6b 20 69 6e 20 61   info block in a
693c0 20 6c 69 73 74 20 6f 66 20 74 68 65 6d 20 61 6c   list of them al
693d0 6c 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  l */.  Table *pT
693e0 61 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ab;          /* 
693f0 54 61 62 6c 65 20 74 68 69 73 20 69 6e 66 6f 20  Table this info 
69400 62 6c 6f 63 6b 20 72 65 66 65 72 73 20 74 6f 20  block refers to 
69410 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  */.  int iDb;   
69420 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
69430 64 65 78 20 69 6e 20 73 71 6c 69 74 65 33 2e 61  dex in sqlite3.a
69440 44 62 5b 5d 20 6f 66 20 64 61 74 61 62 61 73 65  Db[] of database
69450 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a 2f   holding pTab */
69460 0a 20 20 69 6e 74 20 72 65 67 43 74 72 3b 20 20  .  int regCtr;  
69470 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f           /* Memo
69480 72 79 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64  ry register hold
69490 69 6e 67 20 74 68 65 20 72 6f 77 69 64 20 63 6f  ing the rowid co
694a0 75 6e 74 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  unter */.};../*.
694b0 2a 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 63  ** Size of the c
694c0 6f 6c 75 6d 6e 20 63 61 63 68 65 0a 2a 2f 0a 23  olumn cache.*/.#
694d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4e 5f  ifndef SQLITE_N_
694e0 43 4f 4c 43 41 43 48 45 0a 23 20 64 65 66 69 6e  COLCACHE.# defin
694f0 65 20 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41  e SQLITE_N_COLCA
69500 43 48 45 20 31 30 0a 23 65 6e 64 69 66 0a 0a 2f  CHE 10.#endif../
69510 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 6f 6e  *.** At least on
69520 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  e instance of th
69530 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
69540 63 74 75 72 65 20 69 73 20 63 72 65 61 74 65 64  cture is created
69550 20 66 6f 72 20 65 61 63 68 20 0a 2a 2a 20 74 72   for each .** tr
69560 69 67 67 65 72 20 74 68 61 74 20 6d 61 79 20 62  igger that may b
69570 65 20 66 69 72 65 64 20 77 68 69 6c 65 20 70 61  e fired while pa
69580 72 73 69 6e 67 20 61 6e 20 49 4e 53 45 52 54 2c  rsing an INSERT,
69590 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54   UPDATE or DELET
695a0 45 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20  E.** statement. 
695b0 41 6c 6c 20 73 75 63 68 20 6f 62 6a 65 63 74 73  All such objects
695c0 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74   are stored in t
695d0 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68  he linked list h
695e0 65 61 64 65 64 20 61 74 0a 2a 2a 20 50 61 72 73  eaded at.** Pars
695f0 65 2e 70 54 72 69 67 67 65 72 50 72 67 20 61 6e  e.pTriggerPrg an
69600 64 20 64 65 6c 65 74 65 64 20 6f 6e 63 65 20 73  d deleted once s
69610 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 61  tatement compila
69620 74 69 6f 6e 20 68 61 73 20 62 65 65 6e 0a 2a 2a  tion has been.**
69630 20 63 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a 2a   completed..**.*
69640 2a 20 41 20 56 64 62 65 20 73 75 62 2d 70 72 6f  * A Vdbe sub-pro
69650 67 72 61 6d 20 74 68 61 74 20 69 6d 70 6c 65 6d  gram that implem
69660 65 6e 74 73 20 74 68 65 20 62 6f 64 79 20 61 6e  ents the body an
69670 64 20 57 48 45 4e 20 63 6c 61 75 73 65 20 6f 66  d WHEN clause of
69680 20 74 72 69 67 67 65 72 0a 2a 2a 20 54 72 69 67   trigger.** Trig
69690 67 65 72 50 72 67 2e 70 54 72 69 67 67 65 72 2c  gerPrg.pTrigger,
696a0 20 61 73 73 75 6d 69 6e 67 20 61 20 64 65 66 61   assuming a defa
696b0 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20  ult ON CONFLICT 
696c0 63 6c 61 75 73 65 20 6f 66 0a 2a 2a 20 54 72 69  clause of.** Tri
696d0 67 67 65 72 50 72 67 2e 6f 72 63 6f 6e 66 2c 20  ggerPrg.orconf, 
696e0 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  is stored in the
696f0 20 54 72 69 67 67 65 72 50 72 67 2e 70 50 72 6f   TriggerPrg.pPro
69700 67 72 61 6d 20 76 61 72 69 61 62 6c 65 2e 0a 2a  gram variable..*
69710 2a 20 54 68 65 20 50 61 72 73 65 2e 70 54 72 69  * The Parse.pTri
69720 67 67 65 72 50 72 67 20 6c 69 73 74 20 6e 65 76  ggerPrg list nev
69730 65 72 20 63 6f 6e 74 61 69 6e 73 20 74 77 6f 20  er contains two 
69740 65 6e 74 72 69 65 73 20 77 69 74 68 20 74 68 65  entries with the
69750 20 73 61 6d 65 0a 2a 2a 20 76 61 6c 75 65 73 20   same.** values 
69760 66 6f 72 20 62 6f 74 68 20 70 54 72 69 67 67 65  for both pTrigge
69770 72 20 61 6e 64 20 6f 72 63 6f 6e 66 2e 0a 2a 2a  r and orconf..**
69780 0a 2a 2a 20 54 68 65 20 54 72 69 67 67 65 72 50  .** The TriggerP
69790 72 67 2e 6f 6c 64 6d 61 73 6b 20 76 61 72 69 61  rg.oldmask varia
697a0 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20 61 20  ble is set to a 
697b0 6d 61 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f  mask of old.* co
697c0 6c 75 6d 6e 73 0a 2a 2a 20 61 63 63 65 73 73 65  lumns.** accesse
697d0 64 20 28 6f 72 20 73 65 74 20 74 6f 20 30 20 66  d (or set to 0 f
697e0 6f 72 20 74 72 69 67 67 65 72 73 20 66 69 72 65  or triggers fire
697f0 64 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66  d as a result of
69800 20 49 4e 53 45 52 54 20 0a 2a 2a 20 73 74 61 74   INSERT .** stat
69810 65 6d 65 6e 74 73 29 2e 0a 2a 2f 0a 73 74 72 75  ements)..*/.stru
69820 63 74 20 54 72 69 67 67 65 72 50 72 67 20 7b 0a  ct TriggerPrg {.
69830 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67    Trigger *pTrig
69840 67 65 72 3b 20 20 20 20 20 20 2f 2a 20 54 72 69  ger;      /* Tri
69850 67 67 65 72 20 74 68 69 73 20 70 72 6f 67 72 61  gger this progra
69860 6d 20 77 61 73 20 63 6f 64 65 64 20 66 72 6f 6d  m was coded from
69870 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66   */.  int orconf
69880 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
69890 20 44 65 66 61 75 6c 74 20 4f 4e 20 43 4f 4e 46   Default ON CONF
698a0 4c 49 43 54 20 70 6f 6c 69 63 79 20 2a 2f 0a 20  LICT policy */. 
698b0 20 53 75 62 50 72 6f 67 72 61 6d 20 2a 70 50 72   SubProgram *pPr
698c0 6f 67 72 61 6d 3b 20 20 20 2f 2a 20 50 72 6f 67  ogram;   /* Prog
698d0 72 61 6d 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67  ram implementing
698e0 20 70 54 72 69 67 67 65 72 2f 6f 72 63 6f 6e 66   pTrigger/orconf
698f0 20 2a 2f 0a 20 20 75 33 32 20 6f 6c 64 6d 61 73   */.  u32 oldmas
69900 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  k;            /*
69910 20 4d 61 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63   Mask of old.* c
69920 6f 6c 75 6d 6e 73 20 61 63 63 65 73 73 65 64 20  olumns accessed 
69930 2a 2f 0a 20 20 54 72 69 67 67 65 72 50 72 67 20  */.  TriggerPrg 
69940 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20  *pNext;      /* 
69950 4e 65 78 74 20 65 6e 74 72 79 20 69 6e 20 50 61  Next entry in Pa
69960 72 73 65 2e 70 54 72 69 67 67 65 72 50 72 67 20  rse.pTriggerPrg 
69970 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  list */.};../*.*
69980 2a 20 41 6e 20 53 51 4c 20 70 61 72 73 65 72 20  * An SQL parser 
69990 63 6f 6e 74 65 78 74 2e 20 20 41 20 63 6f 70 79  context.  A copy
699a0 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
699b0 72 65 20 69 73 20 70 61 73 73 65 64 20 74 68 72  re is passed thr
699c0 6f 75 67 68 0a 2a 2a 20 74 68 65 20 70 61 72 73  ough.** the pars
699d0 65 72 20 61 6e 64 20 64 6f 77 6e 20 69 6e 74 6f  er and down into
699e0 20 61 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20   all the parser 
699f0 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69  action routine i
69a00 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 61  n order to.** ca
69a10 72 72 79 20 61 72 6f 75 6e 64 20 69 6e 66 6f 72  rry around infor
69a20 6d 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 67  mation that is g
69a30 6c 6f 62 61 6c 20 74 6f 20 74 68 65 20 65 6e 74  lobal to the ent
69a40 69 72 65 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a  ire parse..**.**
69a50 20 54 68 65 20 73 74 72 75 63 74 75 72 65 20 69   The structure i
69a60 73 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74  s divided into t
69a70 77 6f 20 70 61 72 74 73 2e 20 20 57 68 65 6e 20  wo parts.  When 
69a80 74 68 65 20 70 61 72 73 65 72 20 61 6e 64 20 63  the parser and c
69a90 6f 64 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 20  ode.** generate 
69aa0 63 61 6c 6c 20 74 68 65 6d 73 65 6c 76 65 73 20  call themselves 
69ab0 72 65 63 75 72 73 69 76 65 6c 79 2c 20 74 68 65  recursively, the
69ac0 20 66 69 72 73 74 20 70 61 72 74 20 6f 66 20 74   first part of t
69ad0 68 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  he structure.** 
69ae0 69 73 20 63 6f 6e 73 74 61 6e 74 20 62 75 74 20  is constant but 
69af0 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20  the second part 
69b00 69 73 20 72 65 73 65 74 20 61 74 20 74 68 65 20  is reset at the 
69b10 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e  beginning and en
69b20 64 20 6f 66 0a 2a 2a 20 65 61 63 68 20 72 65 63  d of.** each rec
69b30 75 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  ursion..**.** Th
69b40 65 20 6e 54 61 62 6c 65 4c 6f 63 6b 20 61 6e 64  e nTableLock and
69b50 20 61 54 61 62 6c 65 4c 6f 63 6b 20 76 61 72 69   aTableLock vari
69b60 61 62 6c 65 73 20 61 72 65 20 6f 6e 6c 79 20 75  ables are only u
69b70 73 65 64 20 69 66 20 74 68 65 20 73 68 61 72 65  sed if the share
69b80 64 2d 63 61 63 68 65 20 0a 2a 2a 20 66 65 61 74  d-cache .** feat
69b90 75 72 65 20 69 73 20 65 6e 61 62 6c 65 64 20 28  ure is enabled (
69ba0 69 66 20 73 71 6c 69 74 65 33 54 73 64 28 29 2d  if sqlite3Tsd()-
69bb0 3e 75 73 65 53 68 61 72 65 64 44 61 74 61 20 69  >useSharedData i
69bc0 73 20 74 72 75 65 29 2e 20 54 68 65 79 20 61 72  s true). They ar
69bd0 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f  e.** used to sto
69be0 72 65 20 74 68 65 20 73 65 74 20 6f 66 20 74 61  re the set of ta
69bf0 62 6c 65 2d 6c 6f 63 6b 73 20 72 65 71 75 69 72  ble-locks requir
69c00 65 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d  ed by the statem
69c10 65 6e 74 20 62 65 69 6e 67 0a 2a 2a 20 63 6f 6d  ent being.** com
69c20 70 69 6c 65 64 2e 20 46 75 6e 63 74 69 6f 6e 20  piled. Function 
69c30 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b  sqlite3TableLock
69c40 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 61 64  () is used to ad
69c50 64 20 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65  d entries to the
69c60 0a 2a 2a 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72  .** list..*/.str
69c70 75 63 74 20 50 61 72 73 65 20 7b 0a 20 20 73 71  uct Parse {.  sq
69c80 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
69c90 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64     /* The main d
69ca0 61 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72  atabase structur
69cb0 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  e */.  int rc;  
69cc0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
69cd0 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20  eturn code from 
69ce0 65 78 65 63 75 74 69 6f 6e 20 2a 2f 0a 20 20 63  execution */.  c
69cf0 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20  har *zErrMsg;   
69d00 20 20 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20      /* An error 
69d10 6d 65 73 73 61 67 65 20 2a 2f 0a 20 20 56 64 62  message */.  Vdb
69d20 65 20 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20  e *pVdbe;       
69d30 20 20 2f 2a 20 41 6e 20 65 6e 67 69 6e 65 20 66    /* An engine f
69d40 6f 72 20 65 78 65 63 75 74 69 6e 67 20 64 61 74  or executing dat
69d50 61 62 61 73 65 20 62 79 74 65 63 6f 64 65 20 2a  abase bytecode *
69d60 2f 0a 20 20 75 38 20 63 6f 6c 4e 61 6d 65 73 53  /.  u8 colNamesS
69d70 65 74 3b 20 20 20 20 20 20 2f 2a 20 54 52 55 45  et;      /* TRUE
69d80 20 61 66 74 65 72 20 4f 50 5f 43 6f 6c 75 6d 6e   after OP_Column
69d90 4e 61 6d 65 20 68 61 73 20 62 65 65 6e 20 69 73  Name has been is
69da0 73 75 65 64 20 74 6f 20 70 56 64 62 65 20 2a 2f  sued to pVdbe */
69db0 0a 20 20 75 38 20 6e 61 6d 65 43 6c 61 73 68 3b  .  u8 nameClash;
69dc0 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70 65 72          /* A per
69dd0 6d 61 6e 65 6e 74 20 74 61 62 6c 65 20 6e 61 6d  manent table nam
69de0 65 20 63 6c 61 73 68 65 73 20 77 69 74 68 20 74  e clashes with t
69df0 65 6d 70 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a  emp table name *
69e00 2f 0a 20 20 75 38 20 63 68 65 63 6b 53 63 68 65  /.  u8 checkSche
69e10 6d 61 3b 20 20 20 20 20 20 2f 2a 20 43 61 75 73  ma;      /* Caus
69e20 65 73 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65  es schema cookie
69e30 20 63 68 65 63 6b 20 61 66 74 65 72 20 61 6e 20   check after an 
69e40 65 72 72 6f 72 20 2a 2f 0a 20 20 75 38 20 6e 65  error */.  u8 ne
69e50 73 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 20  sted;           
69e60 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73  /* Number of nes
69e70 74 65 64 20 63 61 6c 6c 73 20 74 6f 20 74 68 65  ted calls to the
69e80 20 70 61 72 73 65 72 2f 63 6f 64 65 20 67 65 6e   parser/code gen
69e90 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20 70  erator */.  u8 p
69ea0 61 72 73 65 45 72 72 6f 72 3b 20 20 20 20 20 20  arseError;      
69eb0 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 61   /* True after a
69ec0 20 70 61 72 73 69 6e 67 20 65 72 72 6f 72 2e 20   parsing error. 
69ed0 20 54 69 63 6b 65 74 20 23 31 37 39 34 20 2a 2f   Ticket #1794 */
69ee0 0a 20 20 75 38 20 6e 54 65 6d 70 52 65 67 3b 20  .  u8 nTempReg; 
69ef0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
69f00 72 20 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72  r of temporary r
69f10 65 67 69 73 74 65 72 73 20 69 6e 20 61 54 65 6d  egisters in aTem
69f20 70 52 65 67 5b 5d 20 2a 2f 0a 20 20 75 38 20 6e  pReg[] */.  u8 n
69f30 54 65 6d 70 49 6e 55 73 65 3b 20 20 20 20 20 20  TempInUse;      
69f40 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 54   /* Number of aT
69f50 65 6d 70 52 65 67 5b 5d 20 63 75 72 72 65 6e 74  empReg[] current
69f60 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a  ly checked out *
69f70 2f 0a 20 20 69 6e 74 20 61 54 65 6d 70 52 65 67  /.  int aTempReg
69f80 5b 38 5d 3b 20 20 20 20 20 2f 2a 20 48 6f 6c 64  [8];     /* Hold
69f90 69 6e 67 20 61 72 65 61 20 66 6f 72 20 74 65 6d  ing area for tem
69fa0 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 73  porary registers
69fb0 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 61 6e 67 65   */.  int nRange
69fc0 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 53 69  Reg;       /* Si
69fd0 7a 65 20 6f 66 20 74 68 65 20 74 65 6d 70 6f 72  ze of the tempor
69fe0 61 72 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f  ary register blo
69ff0 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 52 61 6e  ck */.  int iRan
6a000 67 65 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20  geReg;       /* 
6a010 46 69 72 73 74 20 72 65 67 69 73 74 65 72 20 69  First register i
6a020 6e 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69  n temporary regi
6a030 73 74 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20  ster block */.  
6a040 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20  int nErr;       
6a050 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
6a060 66 20 65 72 72 6f 72 73 20 73 65 65 6e 20 2a 2f  f errors seen */
6a070 0a 20 20 69 6e 74 20 6e 54 61 62 3b 20 20 20 20  .  int nTab;    
6a080 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
6a090 72 20 6f 66 20 70 72 65 76 69 6f 75 73 6c 79 20  r of previously 
6a0a0 61 6c 6c 6f 63 61 74 65 64 20 56 44 42 45 20 63  allocated VDBE c
6a0b0 75 72 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20  ursors */.  int 
6a0c0 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20  nMem;           
6a0d0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65   /* Number of me
6a0e0 6d 6f 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20  mory cells used 
6a0f0 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20  so far */.  int 
6a100 6e 53 65 74 3b 20 20 20 20 20 20 20 20 20 20 20  nSet;           
6a110 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65   /* Number of se
6a120 74 73 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a  ts used so far *
6a130 2f 0a 20 20 69 6e 74 20 63 6b 42 61 73 65 3b 20  /.  int ckBase; 
6a140 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65           /* Base
6a150 20 72 65 67 69 73 74 65 72 20 6f 66 20 64 61 74   register of dat
6a160 61 20 64 75 72 69 6e 67 20 63 68 65 63 6b 20 63  a during check c
6a170 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20  onstraints */.  
6a180 69 6e 74 20 69 43 61 63 68 65 4c 65 76 65 6c 3b  int iCacheLevel;
6a190 20 20 20 20 20 2f 2a 20 43 6f 6c 43 61 63 68 65       /* ColCache
6a1a0 20 76 61 6c 69 64 20 77 68 65 6e 20 61 43 6f 6c   valid when aCol
6a1b0 43 61 63 68 65 5b 5d 2e 69 4c 65 76 65 6c 3c 3d  Cache[].iLevel<=
6a1c0 69 43 61 63 68 65 4c 65 76 65 6c 20 2a 2f 0a 20  iCacheLevel */. 
6a1d0 20 69 6e 74 20 69 43 61 63 68 65 43 6e 74 3b 20   int iCacheCnt; 
6a1e0 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72        /* Counter
6a1f0 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74   used to generat
6a200 65 20 61 43 6f 6c 43 61 63 68 65 5b 5d 2e 6c 72  e aColCache[].lr
6a210 75 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38  u values */.  u8
6a220 20 6e 43 6f 6c 43 61 63 68 65 3b 20 20 20 20 20   nColCache;     
6a230 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
6a240 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 63  entries in the c
6a250 6f 6c 75 6d 6e 20 63 61 63 68 65 20 2a 2f 0a 20  olumn cache */. 
6a260 20 75 38 20 69 43 6f 6c 43 61 63 68 65 3b 20 20   u8 iColCache;  
6a270 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e        /* Next en
6a280 74 72 79 20 6f 66 20 74 68 65 20 63 61 63 68 65  try of the cache
6a290 20 74 6f 20 72 65 70 6c 61 63 65 20 2a 2f 0a 20   to replace */. 
6a2a0 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68   struct yColCach
6a2b0 65 20 7b 0a 20 20 20 20 69 6e 74 20 69 54 61 62  e {.    int iTab
6a2c0 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  le;           /*
6a2d0 20 54 61 62 6c 65 20 63 75 72 73 6f 72 20 6e 75   Table cursor nu
6a2e0 6d 62 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  mber */.    int 
6a2f0 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20  iColumn;        
6a300 20 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6c 75 6d    /* Table colum
6a310 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20  n number */.    
6a320 75 38 20 61 66 66 43 68 61 6e 67 65 3b 20 20 20  u8 affChange;   
6a330 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
6a340 20 74 68 69 73 20 72 65 67 69 73 74 65 72 20 68   this register h
6a350 61 73 20 68 61 64 20 61 6e 20 61 66 66 69 6e 69  as had an affini
6a360 74 79 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 20  ty change */.   
6a370 20 75 38 20 74 65 6d 70 52 65 67 3b 20 20 20 20   u8 tempReg;    
6a380 20 20 20 20 20 20 20 2f 2a 20 69 52 65 67 20 69         /* iReg i
6a390 73 20 61 20 74 65 6d 70 20 72 65 67 69 73 74 65  s a temp registe
6a3a0 72 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20  r that needs to 
6a3b0 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20  be freed */.    
6a3c0 69 6e 74 20 69 4c 65 76 65 6c 3b 20 20 20 20 20  int iLevel;     
6a3d0 20 20 20 20 20 20 2f 2a 20 4e 65 73 74 69 6e 67        /* Nesting
6a3e0 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 20 20 69 6e   level */.    in
6a3f0 74 20 69 52 65 67 3b 20 20 20 20 20 20 20 20 20  t iReg;         
6a400 20 20 20 20 2f 2a 20 52 65 67 20 77 69 74 68 20      /* Reg with 
6a410 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f  value of this co
6a420 6c 75 6d 6e 2e 20 30 20 6d 65 61 6e 73 20 6e 6f  lumn. 0 means no
6a430 6e 65 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c  ne. */.    int l
6a440 72 75 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ru;             
6a450 20 2f 2a 20 4c 65 61 73 74 20 72 65 63 65 6e 74   /* Least recent
6a460 6c 79 20 75 73 65 64 20 65 6e 74 72 79 20 68 61  ly used entry ha
6a470 73 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 76  s the smallest v
6a480 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c  alue */.  } aCol
6a490 43 61 63 68 65 5b 53 51 4c 49 54 45 5f 4e 5f 43  Cache[SQLITE_N_C
6a4a0 4f 4c 43 41 43 48 45 5d 3b 20 20 2f 2a 20 4f 6e  OLCACHE];  /* On
6a4b0 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d  e for each colum
6a4c0 6e 20 63 61 63 68 65 20 65 6e 74 72 79 20 2a 2f  n cache entry */
6a4d0 0a 20 20 75 33 32 20 77 72 69 74 65 4d 61 73 6b  .  u32 writeMask
6a4e0 3b 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74  ;       /* Start
6a4f0 20 61 20 77 72 69 74 65 20 74 72 61 6e 73 61 63   a write transac
6a500 74 69 6f 6e 20 6f 6e 20 74 68 65 73 65 20 64 61  tion on these da
6a510 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 75 33 32  tabases */.  u32
6a520 20 63 6f 6f 6b 69 65 4d 61 73 6b 3b 20 20 20 20   cookieMask;    
6a530 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20    /* Bitmask of 
6a540 73 63 68 65 6d 61 20 76 65 72 69 66 69 65 64 20  schema verified 
6a550 64 61 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 75  databases */.  u
6a560 38 20 69 73 4d 75 6c 74 69 57 72 69 74 65 3b 20  8 isMultiWrite; 
6a570 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73      /* True if s
6a580 74 61 74 65 6d 65 6e 74 20 6d 61 79 20 61 66 66  tatement may aff
6a590 65 63 74 2f 69 6e 73 65 72 74 20 6d 75 6c 74 69  ect/insert multi
6a5a0 70 6c 65 20 72 6f 77 73 20 2a 2f 0a 20 20 75 38  ple rows */.  u8
6a5b0 20 6d 61 79 41 62 6f 72 74 3b 20 20 20 20 20 20   mayAbort;      
6a5c0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 74     /* True if st
6a5d0 61 74 65 6d 65 6e 74 20 6d 61 79 20 74 68 72 6f  atement may thro
6a5e0 77 20 61 6e 20 41 42 4f 52 54 20 65 78 63 65 70  w an ABORT excep
6a5f0 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 63 6f  tion */.  int co
6a600 6f 6b 69 65 47 6f 74 6f 3b 20 20 20 20 20 20 2f  okieGoto;      /
6a610 2a 20 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f  * Address of OP_
6a620 47 6f 74 6f 20 74 6f 20 63 6f 6f 6b 69 65 20 76  Goto to cookie v
6a630 65 72 69 66 69 65 72 20 73 75 62 72 6f 75 74 69  erifier subrouti
6a640 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b  ne */.  int cook
6a650 69 65 56 61 6c 75 65 5b 53 51 4c 49 54 45 5f 4d  ieValue[SQLITE_M
6a660 41 58 5f 41 54 54 41 43 48 45 44 2b 32 5d 3b 20  AX_ATTACHED+2]; 
6a670 20 2f 2a 20 56 61 6c 75 65 73 20 6f 66 20 63 6f   /* Values of co
6a680 6f 6b 69 65 73 20 74 6f 20 76 65 72 69 66 79 20  okies to verify 
6a690 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
6a6a0 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
6a6b0 43 48 45 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65  CHE.  int nTable
6a6c0 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20  Lock;        /* 
6a6d0 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20  Number of locks 
6a6e0 69 6e 20 61 54 61 62 6c 65 4c 6f 63 6b 20 2a 2f  in aTableLock */
6a6f0 0a 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 61 54  .  TableLock *aT
6a700 61 62 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 52 65 71  ableLock; /* Req
6a710 75 69 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b  uired table lock
6a720 73 20 66 6f 72 20 73 68 61 72 65 64 2d 63 61 63  s for shared-cac
6a730 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 65 6e 64 69  he mode */.#endi
6a740 66 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64  f.  int regRowid
6a750 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69  ;        /* Regi
6a760 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77  ster holding row
6a770 69 64 20 6f 66 20 43 52 45 41 54 45 20 54 41 42  id of CREATE TAB
6a780 4c 45 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e  LE entry */.  in
6a790 74 20 72 65 67 52 6f 6f 74 3b 20 20 20 20 20 20  t regRoot;      
6a7a0 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68     /* Register h
6a7b0 6f 6c 64 69 6e 67 20 72 6f 6f 74 20 70 61 67 65  olding root page
6a7c0 20 6e 75 6d 62 65 72 20 66 6f 72 20 6e 65 77 20   number for new 
6a7d0 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 41 75 74  objects */.  Aut
6a7e0 6f 69 6e 63 49 6e 66 6f 20 2a 70 41 69 6e 63 3b  oincInfo *pAinc;
6a7f0 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e    /* Information
6a800 20 61 62 6f 75 74 20 41 55 54 4f 49 4e 43 52 45   about AUTOINCRE
6a810 4d 45 4e 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f  MENT counters */
6a820 0a 20 20 69 6e 74 20 6e 4d 61 78 41 72 67 3b 20  .  int nMaxArg; 
6a830 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 20 61          /* Max a
6a840 72 67 73 20 70 61 73 73 65 64 20 74 6f 20 75 73  rgs passed to us
6a850 65 72 20 66 75 6e 63 74 69 6f 6e 20 62 79 20 73  er function by s
6a860 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 0a 20  ub-program */.. 
6a870 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
6a880 75 73 65 64 20 77 68 69 6c 65 20 63 6f 64 69 6e  used while codin
6a890 67 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61  g trigger progra
6a8a0 6d 73 2e 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a  ms. */.  Parse *
6a8b0 70 54 6f 70 6c 65 76 65 6c 3b 20 20 20 20 2f 2a  pToplevel;    /*
6a8c0 20 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65   Parse structure
6a8d0 20 66 6f 72 20 6d 61 69 6e 20 70 72 6f 67 72 61   for main progra
6a8e0 6d 20 28 6f 72 20 4e 55 4c 4c 29 20 2a 2f 0a 20  m (or NULL) */. 
6a8f0 20 54 61 62 6c 65 20 2a 70 54 72 69 67 67 65 72   Table *pTrigger
6a900 54 61 62 3b 20 20 2f 2a 20 54 61 62 6c 65 20 74  Tab;  /* Table t
6a910 72 69 67 67 65 72 73 20 61 72 65 20 62 65 69 6e  riggers are bein
6a920 67 20 63 6f 64 65 64 20 66 6f 72 20 2a 2f 0a 20  g coded for */. 
6a930 20 75 33 32 20 6f 6c 64 6d 61 73 6b 3b 20 20 20   u32 oldmask;   
6a940 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66        /* Mask of
6a950 20 6f 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73 20 72   old.* columns r
6a960 65 66 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 75  eferenced */.  u
6a970 38 20 65 54 72 69 67 67 65 72 4f 70 3b 20 20 20  8 eTriggerOp;   
6a980 20 20 20 20 2f 2a 20 54 4b 5f 55 50 44 41 54 45      /* TK_UPDATE
6a990 2c 20 54 4b 5f 49 4e 53 45 52 54 20 6f 72 20 54  , TK_INSERT or T
6a9a0 4b 5f 44 45 4c 45 54 45 20 2a 2f 0a 20 20 75 38  K_DELETE */.  u8
6a9b0 20 65 4f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20   eOrconf;       
6a9c0 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 4f 4e     /* Default ON
6a9d0 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79   CONFLICT policy
6a9e0 20 66 6f 72 20 74 72 69 67 67 65 72 20 73 74 65   for trigger ste
6a9f0 70 73 20 2a 2f 0a 0a 20 20 2f 2a 20 41 62 6f 76  ps */..  /* Abov
6aa00 65 20 69 73 20 63 6f 6e 73 74 61 6e 74 20 62 65  e is constant be
6aa10 74 77 65 65 6e 20 72 65 63 75 72 73 69 6f 6e 73  tween recursions
6aa20 2e 20 20 42 65 6c 6f 77 20 69 73 20 72 65 73 65  .  Below is rese
6aa30 74 20 62 65 66 6f 72 65 20 61 6e 64 20 61 66 74  t before and aft
6aa40 65 72 0a 20 20 2a 2a 20 65 61 63 68 20 72 65 63  er.  ** each rec
6aa50 75 72 73 69 6f 6e 20 2a 2f 0a 0a 20 20 69 6e 74  ursion */..  int
6aa60 20 6e 56 61 72 3b 20 20 20 20 20 20 20 20 20 20   nVar;          
6aa70 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 27    /* Number of '
6aa80 3f 27 20 76 61 72 69 61 62 6c 65 73 20 73 65 65  ?' variables see
6aa90 6e 20 69 6e 20 74 68 65 20 53 51 4c 20 73 6f 20  n in the SQL so 
6aaa0 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61  far */.  int nVa
6aab0 72 45 78 70 72 3b 20 20 20 20 20 20 20 20 2f 2a  rExpr;        /*
6aac0 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 65 64 20   Number of used 
6aad0 73 6c 6f 74 73 20 69 6e 20 61 70 56 61 72 45 78  slots in apVarEx
6aae0 70 72 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 56  pr[] */.  int nV
6aaf0 61 72 45 78 70 72 41 6c 6c 6f 63 3b 20 20 20 2f  arExprAlloc;   /
6ab00 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f  * Number of allo
6ab10 63 61 74 65 64 20 73 6c 6f 74 73 20 69 6e 20 61  cated slots in a
6ab20 70 56 61 72 45 78 70 72 5b 5d 20 2a 2f 0a 20 20  pVarExpr[] */.  
6ab30 45 78 70 72 20 2a 2a 61 70 56 61 72 45 78 70 72  Expr **apVarExpr
6ab40 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73  ;    /* Pointers
6ab50 20 74 6f 20 3a 61 61 61 20 61 6e 64 20 24 61 61   to :aaa and $aa
6ab60 61 61 20 77 69 6c 64 63 61 72 64 20 65 78 70 72  aa wildcard expr
6ab70 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74  essions */.  int
6ab80 20 6e 41 6c 69 61 73 3b 20 20 20 20 20 20 20 20   nAlias;        
6ab90 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
6aba0 6c 69 61 73 65 64 20 72 65 73 75 6c 74 20 73 65  liased result se
6abb0 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69  t columns */.  i
6abc0 6e 74 20 6e 41 6c 69 61 73 41 6c 6c 6f 63 3b 20  nt nAliasAlloc; 
6abd0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
6abe0 20 61 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73   allocated slots
6abf0 20 66 6f 72 20 61 41 6c 69 61 73 5b 5d 20 2a 2f   for aAlias[] */
6ac00 0a 20 20 69 6e 74 20 2a 61 41 6c 69 61 73 3b 20  .  int *aAlias; 
6ac10 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
6ac20 74 65 72 20 75 73 65 64 20 74 6f 20 68 6f 6c 64  ter used to hold
6ac30 20 61 6c 69 61 73 65 64 20 72 65 73 75 6c 74 20   aliased result 
6ac40 2a 2f 0a 20 20 75 38 20 65 78 70 6c 61 69 6e 3b  */.  u8 explain;
6ac50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
6ac60 65 20 69 66 20 74 68 65 20 45 58 50 4c 41 49 4e  e if the EXPLAIN
6ac70 20 66 6c 61 67 20 69 73 20 66 6f 75 6e 64 20 6f   flag is found o
6ac80 6e 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20  n the query */. 
6ac90 20 54 6f 6b 65 6e 20 73 4e 61 6d 65 54 6f 6b 65   Token sNameToke
6aca0 6e 3b 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 77  n;    /* Token w
6acb0 69 74 68 20 75 6e 71 75 61 6c 69 66 69 65 64 20  ith unqualified 
6acc0 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 20 6e 61  schema object na
6acd0 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 4c  me */.  Token sL
6ace0 61 73 74 54 6f 6b 65 6e 3b 20 20 20 20 2f 2a 20  astToken;    /* 
6acf0 54 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e 20 70  The last token p
6ad00 61 72 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  arsed */.  const
6ad10 20 63 68 61 72 20 2a 7a 54 61 69 6c 3b 20 20 20   char *zTail;   
6ad20 2f 2a 20 41 6c 6c 20 53 51 4c 20 74 65 78 74 20  /* All SQL text 
6ad30 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 73 65  past the last se
6ad40 6d 69 63 6f 6c 6f 6e 20 70 61 72 73 65 64 20 2a  micolon parsed *
6ad50 2f 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 54  /.  Table *pNewT
6ad60 61 62 6c 65 3b 20 20 20 20 2f 2a 20 41 20 74 61  able;    /* A ta
6ad70 62 6c 65 20 62 65 69 6e 67 20 63 6f 6e 73 74 72  ble being constr
6ad80 75 63 74 65 64 20 62 79 20 43 52 45 41 54 45 20  ucted by CREATE 
6ad90 54 41 42 4c 45 20 2a 2f 0a 20 20 54 72 69 67 67  TABLE */.  Trigg
6ada0 65 72 20 2a 70 4e 65 77 54 72 69 67 67 65 72 3b  er *pNewTrigger;
6adb0 20 20 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20       /* Trigger 
6adc0 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 20  under construct 
6add0 62 79 20 61 20 43 52 45 41 54 45 20 54 52 49 47  by a CREATE TRIG
6ade0 47 45 52 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  GER */.  const c
6adf0 68 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78  har *zAuthContex
6ae00 74 3b 20 2f 2a 20 54 68 65 20 36 74 68 20 70 61  t; /* The 6th pa
6ae10 72 61 6d 65 74 65 72 20 74 6f 20 64 62 2d 3e 78  rameter to db->x
6ae20 41 75 74 68 20 63 61 6c 6c 62 61 63 6b 73 20 2a  Auth callbacks *
6ae30 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
6ae40 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
6ae50 4c 45 0a 20 20 54 6f 6b 65 6e 20 73 41 72 67 3b  LE.  Token sArg;
6ae60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ae70 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74  /* Complete text
6ae80 20 6f 66 20 61 20 6d 6f 64 75 6c 65 20 61 72 67   of a module arg
6ae90 75 6d 65 6e 74 20 2a 2f 0a 20 20 75 38 20 64 65  ument */.  u8 de
6aea0 63 6c 61 72 65 56 74 61 62 3b 20 20 20 20 20 20  clareVtab;      
6aeb0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
6aec0 20 69 6e 73 69 64 65 20 73 71 6c 69 74 65 33 5f   inside sqlite3_
6aed0 64 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20 2a  declare_vtab() *
6aee0 2f 0a 20 20 69 6e 74 20 6e 56 74 61 62 4c 6f 63  /.  int nVtabLoc
6aef0 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  k;             /
6af00 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76 69 72 74  * Number of virt
6af10 75 61 6c 20 74 61 62 6c 65 73 20 74 6f 20 6c 6f  ual tables to lo
6af20 63 6b 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 2a  ck */.  Table **
6af30 61 70 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20 20  apVtabLock;     
6af40 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
6af50 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20   virtual tables 
6af60 6e 65 65 64 69 6e 67 20 6c 6f 63 6b 69 6e 67 20  needing locking 
6af70 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20  */.#endif.  int 
6af80 6e 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20  nHeight;        
6af90 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
6afa0 6e 20 74 72 65 65 20 68 65 69 67 68 74 20 6f 66  n tree height of
6afb0 20 63 75 72 72 65 6e 74 20 73 75 62 2d 73 65 6c   current sub-sel
6afc0 65 63 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ect */.  Table *
6afd0 70 5a 6f 6d 62 69 65 54 61 62 3b 20 20 20 20 20  pZombieTab;     
6afe0 20 2f 2a 20 4c 69 73 74 20 6f 66 20 54 61 62 6c   /* List of Tabl
6aff0 65 20 6f 62 6a 65 63 74 73 20 74 6f 20 64 65 6c  e objects to del
6b000 65 74 65 20 61 66 74 65 72 20 63 6f 64 65 20 67  ete after code g
6b010 65 6e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 50  en */.  TriggerP
6b020 72 67 20 2a 70 54 72 69 67 67 65 72 50 72 67 3b  rg *pTriggerPrg;
6b030 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69      /* Linked li
6b040 73 74 20 6f 66 20 63 6f 64 65 64 20 74 72 69 67  st of coded trig
6b050 67 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 64  gers */.};..#ifd
6b060 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
6b070 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 23 64  IRTUALTABLE.  #d
6b080 65 66 69 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45  efine IN_DECLARE
6b090 5f 56 54 41 42 20 30 0a 23 65 6c 73 65 0a 20 20  _VTAB 0.#else.  
6b0a0 23 64 65 66 69 6e 65 20 49 4e 5f 44 45 43 4c 41  #define IN_DECLA
6b0b0 52 45 5f 56 54 41 42 20 28 70 50 61 72 73 65 2d  RE_VTAB (pParse-
6b0c0 3e 64 65 63 6c 61 72 65 56 74 61 62 29 0a 23 65  >declareVtab).#e
6b0d0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  ndif../*.** An i
6b0e0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
6b0f0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
6b100 72 65 20 63 61 6e 20 62 65 20 64 65 63 6c 61 72  re can be declar
6b110 65 64 20 6f 6e 20 61 20 73 74 61 63 6b 20 61 6e  ed on a stack an
6b120 64 20 75 73 65 64 0a 2a 2a 20 74 6f 20 73 61 76  d used.** to sav
6b130 65 20 74 68 65 20 50 61 72 73 65 2e 7a 41 75 74  e the Parse.zAut
6b140 68 43 6f 6e 74 65 78 74 20 76 61 6c 75 65 20 73  hContext value s
6b150 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 62 65  o that it can be
6b160 20 72 65 73 74 6f 72 65 64 20 6c 61 74 65 72 2e   restored later.
6b170 0a 2a 2f 0a 73 74 72 75 63 74 20 41 75 74 68 43  .*/.struct AuthC
6b180 6f 6e 74 65 78 74 20 7b 0a 20 20 63 6f 6e 73 74  ontext {.  const
6b190 20 63 68 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74   char *zAuthCont
6b1a0 65 78 74 3b 20 20 20 2f 2a 20 50 75 74 20 73 61  ext;   /* Put sa
6b1b0 76 65 64 20 50 61 72 73 65 2e 7a 41 75 74 68 43  ved Parse.zAuthC
6b1c0 6f 6e 74 65 78 74 20 68 65 72 65 20 2a 2f 0a 20  ontext here */. 
6b1d0 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20   Parse *pParse; 
6b1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6b1f0 54 68 65 20 50 61 72 73 65 20 73 74 72 75 63 74  The Parse struct
6b200 75 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ure */.};../*.**
6b210 20 42 69 74 66 69 65 6c 64 20 66 6c 61 67 73 20   Bitfield flags 
6b220 66 6f 72 20 50 35 20 76 61 6c 75 65 20 69 6e 20  for P5 value in 
6b230 4f 50 5f 49 6e 73 65 72 74 20 61 6e 64 20 4f 50  OP_Insert and OP
6b240 5f 44 65 6c 65 74 65 0a 2a 2f 0a 23 64 65 66 69  _Delete.*/.#defi
6b250 6e 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47  ne OPFLAG_NCHANG
6b260 45 20 20 20 20 20 20 20 30 78 30 31 20 20 20 20  E       0x01    
6b270 2f 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74 65  /* Set to update
6b280 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f 0a   db->nChange */.
6b290 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 4c  #define OPFLAG_L
6b2a0 41 53 54 52 4f 57 49 44 20 20 20 20 20 30 78 30  ASTROWID     0x0
6b2b0 32 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 75  2    /* Set to u
6b2c0 70 64 61 74 65 20 64 62 2d 3e 6c 61 73 74 52 6f  pdate db->lastRo
6b2d0 77 69 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  wid */.#define O
6b2e0 50 46 4c 41 47 5f 49 53 55 50 44 41 54 45 20 20  PFLAG_ISUPDATE  
6b2f0 20 20 20 20 30 78 30 34 20 20 20 20 2f 2a 20 54      0x04    /* T
6b300 68 69 73 20 4f 50 5f 49 6e 73 65 72 74 20 69 73  his OP_Insert is
6b310 20 61 6e 20 73 71 6c 20 55 50 44 41 54 45 20 2a   an sql UPDATE *
6b320 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47  /.#define OPFLAG
6b330 5f 41 50 50 45 4e 44 20 20 20 20 20 20 20 20 30  _APPEND        0
6b340 78 30 38 20 20 20 20 2f 2a 20 54 68 69 73 20 69  x08    /* This i
6b350 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61  s likely to be a
6b360 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 23 64 65 66  n append */.#def
6b370 69 6e 65 20 4f 50 46 4c 41 47 5f 55 53 45 53 45  ine OPFLAG_USESE
6b380 45 4b 52 45 53 55 4c 54 20 30 78 31 30 20 20 20  EKRESULT 0x10   
6b390 20 2f 2a 20 54 72 79 20 74 6f 20 61 76 6f 69 64   /* Try to avoid
6b3a0 20 61 20 73 65 65 6b 20 69 6e 20 42 74 72 65 65   a seek in Btree
6b3b0 49 6e 73 65 72 74 28 29 20 2a 2f 0a 23 64 65 66  Insert() */.#def
6b3c0 69 6e 65 20 4f 50 46 4c 41 47 5f 43 4c 45 41 52  ine OPFLAG_CLEAR
6b3d0 43 41 43 48 45 20 20 20 20 30 78 32 30 20 20 20  CACHE    0x20   
6b3e0 20 2f 2a 20 43 6c 65 61 72 20 70 73 65 75 64 6f   /* Clear pseudo
6b3f0 2d 74 61 62 6c 65 20 63 61 63 68 65 20 69 6e 20  -table cache in 
6b400 4f 50 5f 43 6f 6c 75 6d 6e 20 2a 2f 0a 0a 2f 2a  OP_Column */../*
6b410 0a 20 2a 20 45 61 63 68 20 74 72 69 67 67 65 72  . * Each trigger
6b420 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20   present in the 
6b430 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
6b440 69 73 20 73 74 6f 72 65 64 20 61 73 20 61 6e 20  is stored as an 
6b450 69 6e 73 74 61 6e 63 65 20 6f 66 0a 20 2a 20 73  instance of. * s
6b460 74 72 75 63 74 20 54 72 69 67 67 65 72 2e 20 0a  truct Trigger. .
6b470 20 2a 0a 20 2a 20 50 6f 69 6e 74 65 72 73 20 74   *. * Pointers t
6b480 6f 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 73  o instances of s
6b490 74 72 75 63 74 20 54 72 69 67 67 65 72 20 61 72  truct Trigger ar
6b4a0 65 20 73 74 6f 72 65 64 20 69 6e 20 74 77 6f 20  e stored in two 
6b4b0 77 61 79 73 2e 0a 20 2a 20 31 2e 20 49 6e 20 74  ways.. * 1. In t
6b4c0 68 65 20 22 74 72 69 67 48 61 73 68 22 20 68 61  he "trigHash" ha
6b4d0 73 68 20 74 61 62 6c 65 20 28 70 61 72 74 20 6f  sh table (part o
6b4e0 66 20 74 68 65 20 73 71 6c 69 74 65 33 2a 20 74  f the sqlite3* t
6b4f0 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 20 74  hat represents t
6b500 68 65 20 0a 20 2a 20 20 20 20 64 61 74 61 62 61  he . *    databa
6b510 73 65 29 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73  se). This allows
6b520 20 54 72 69 67 67 65 72 20 73 74 72 75 63 74 75   Trigger structu
6b530 72 65 73 20 74 6f 20 62 65 20 72 65 74 72 69 65  res to be retrie
6b540 76 65 64 20 62 79 20 6e 61 6d 65 2e 0a 20 2a 20  ved by name.. * 
6b550 32 2e 20 41 6c 6c 20 74 72 69 67 67 65 72 73 20  2. All triggers 
6b560 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
6b570 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 66  a single table f
6b580 6f 72 6d 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73  orm a linked lis
6b590 74 2c 20 75 73 69 6e 67 20 74 68 65 0a 20 2a 20  t, using the. * 
6b5a0 20 20 20 70 4e 65 78 74 20 6d 65 6d 62 65 72 20     pNext member 
6b5b0 6f 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65  of struct Trigge
6b5c0 72 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  r. A pointer to 
6b5d0 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e  the first elemen
6b5e0 74 20 6f 66 20 74 68 65 0a 20 2a 20 20 20 20 6c  t of the. *    l
6b5f0 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 73 74  inked list is st
6b600 6f 72 65 64 20 61 73 20 74 68 65 20 22 70 54 72  ored as the "pTr
6b610 69 67 67 65 72 22 20 6d 65 6d 62 65 72 20 6f 66  igger" member of
6b620 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 0a   the associated.
6b630 20 2a 20 20 20 20 73 74 72 75 63 74 20 54 61 62   *    struct Tab
6b640 6c 65 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 22 73  le.. *. * The "s
6b650 74 65 70 5f 6c 69 73 74 22 20 6d 65 6d 62 65 72  tep_list" member
6b660 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66   points to the f
6b670 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20  irst element of 
6b680 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 0a 20 2a  a linked list. *
6b690 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
6b6a0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 73  SQL statements s
6b6b0 70 65 63 69 66 69 65 64 20 61 73 20 74 68 65 20  pecified as the 
6b6c0 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e  trigger program.
6b6d0 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67  . */.struct Trig
6b6e0 67 65 72 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e  ger {.  char *zN
6b6f0 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ame;            
6b700 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74  /* The name of t
6b710 68 65 20 74 72 69 67 67 65 72 20 20 20 20 20 20  he trigger      
6b720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6b730 20 20 2a 2f 0a 20 20 63 68 61 72 20 2a 74 61 62    */.  char *tab
6b740 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  le;            /
6b750 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76  * The table or v
6b760 69 65 77 20 74 6f 20 77 68 69 63 68 20 74 68 65  iew to which the
6b770 20 74 72 69 67 67 65 72 20 61 70 70 6c 69 65 73   trigger applies
6b780 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 20   */.  u8 op;    
6b790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6b7a0 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54   One of TK_DELET
6b7b0 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b  E, TK_UPDATE, TK
6b7c0 5f 49 4e 53 45 52 54 20 20 20 20 20 20 20 20 20  _INSERT         
6b7d0 2a 2f 0a 20 20 75 38 20 74 72 5f 74 6d 3b 20 20  */.  u8 tr_tm;  
6b7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6b7f0 4f 6e 65 20 6f 66 20 54 52 49 47 47 45 52 5f 42  One of TRIGGER_B
6b800 45 46 4f 52 45 2c 20 54 52 49 47 47 45 52 5f 41  EFORE, TRIGGER_A
6b810 46 54 45 52 20 2a 2f 0a 20 20 45 78 70 72 20 2a  FTER */.  Expr *
6b820 70 57 68 65 6e 3b 20 20 20 20 20 20 20 20 20 20  pWhen;          
6b830 20 20 2f 2a 20 54 68 65 20 57 48 45 4e 20 63 6c    /* The WHEN cl
6b840 61 75 73 65 20 6f 66 20 74 68 65 20 65 78 70 72  ause of the expr
6b850 65 73 73 69 6f 6e 20 28 6d 61 79 20 62 65 20 4e  ession (may be N
6b860 55 4c 4c 29 20 2a 2f 0a 20 20 49 64 4c 69 73 74  ULL) */.  IdList
6b870 20 2a 70 43 6f 6c 75 6d 6e 73 3b 20 20 20 20 20   *pColumns;     
6b880 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
6b890 61 6e 20 55 50 44 41 54 45 20 4f 46 20 3c 63 6f  an UPDATE OF <co
6b8a0 6c 75 6d 6e 2d 6c 69 73 74 3e 20 74 72 69 67 67  lumn-list> trigg
6b8b0 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  er,.            
6b8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6b8d0 20 74 68 65 20 3c 63 6f 6c 75 6d 6e 2d 6c 69 73   the <column-lis
6b8e0 74 3e 20 69 73 20 73 74 6f 72 65 64 20 68 65 72  t> is stored her
6b8f0 65 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70  e */.  Schema *p
6b900 53 63 68 65 6d 61 3b 20 20 20 20 20 20 20 20 2f  Schema;        /
6b910 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e  * Schema contain
6b920 69 6e 67 20 74 68 65 20 74 72 69 67 67 65 72 20  ing the trigger 
6b930 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 54 61  */.  Schema *pTa
6b940 62 53 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20  bSchema;     /* 
6b950 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e  Schema containin
6b960 67 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20  g the table */. 
6b970 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 74   TriggerStep *st
6b980 65 70 5f 6c 69 73 74 3b 20 2f 2a 20 4c 69 6e 6b  ep_list; /* Link
6b990 20 6c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72   list of trigger
6b9a0 20 70 72 6f 67 72 61 6d 20 73 74 65 70 73 20 20   program steps  
6b9b0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20             */.  
6b9c0 54 72 69 67 67 65 72 20 2a 70 4e 65 78 74 3b 20  Trigger *pNext; 
6b9d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20          /* Next 
6b9e0 74 72 69 67 67 65 72 20 61 73 73 6f 63 69 61 74  trigger associat
6b9f0 65 64 20 77 69 74 68 20 74 68 65 20 74 61 62 6c  ed with the tabl
6ba00 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  e */.};../*.** A
6ba10 20 74 72 69 67 67 65 72 20 69 73 20 65 69 74 68   trigger is eith
6ba20 65 72 20 61 20 42 45 46 4f 52 45 20 6f 72 20 61  er a BEFORE or a
6ba30 6e 20 41 46 54 45 52 20 74 72 69 67 67 65 72 2e  n AFTER trigger.
6ba40 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
6ba50 63 6f 6e 73 74 61 6e 74 73 0a 2a 2a 20 64 65 74  constants.** det
6ba60 65 72 6d 69 6e 65 20 77 68 69 63 68 2e 20 0a 2a  ermine which. .*
6ba70 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72  *.** If there ar
6ba80 65 20 6d 75 6c 74 69 70 6c 65 20 74 72 69 67 67  e multiple trigg
6ba90 65 72 73 2c 20 79 6f 75 20 6d 69 67 68 74 20 6f  ers, you might o
6baa0 66 20 73 6f 6d 65 20 42 45 46 4f 52 45 20 61 6e  f some BEFORE an
6bab0 64 20 73 6f 6d 65 20 41 46 54 45 52 2e 0a 2a 2a  d some AFTER..**
6bac0 20 49 6e 20 74 68 61 74 20 63 61 73 65 73 2c 20   In that cases, 
6bad0 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 62 65  the constants be
6bae0 6c 6f 77 20 63 61 6e 20 62 65 20 4f 52 65 64 20  low can be ORed 
6baf0 74 6f 67 65 74 68 65 72 2e 0a 2a 2f 0a 23 64 65  together..*/.#de
6bb00 66 69 6e 65 20 54 52 49 47 47 45 52 5f 42 45 46  fine TRIGGER_BEF
6bb10 4f 52 45 20 20 31 0a 23 64 65 66 69 6e 65 20 54  ORE  1.#define T
6bb20 52 49 47 47 45 52 5f 41 46 54 45 52 20 20 20 32  RIGGER_AFTER   2
6bb30 0a 0a 2f 2a 0a 20 2a 20 41 6e 20 69 6e 73 74 61  ../*. * An insta
6bb40 6e 63 65 20 6f 66 20 73 74 72 75 63 74 20 54 72  nce of struct Tr
6bb50 69 67 67 65 72 53 74 65 70 20 69 73 20 75 73 65  iggerStep is use
6bb60 64 20 74 6f 20 73 74 6f 72 65 20 61 20 73 69 6e  d to store a sin
6bb70 67 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  gle SQL statemen
6bb80 74 0a 20 2a 20 74 68 61 74 20 69 73 20 61 20 70  t. * that is a p
6bb90 61 72 74 20 6f 66 20 61 20 74 72 69 67 67 65 72  art of a trigger
6bba0 2d 70 72 6f 67 72 61 6d 2e 20 0a 20 2a 0a 20 2a  -program. . *. *
6bbb0 20 49 6e 73 74 61 6e 63 65 73 20 6f 66 20 73 74   Instances of st
6bbc0 72 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70  ruct TriggerStep
6bbd0 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61   are stored in a
6bbe0 20 73 69 6e 67 6c 79 20 6c 69 6e 6b 65 64 20 6c   singly linked l
6bbf0 69 73 74 20 28 6c 69 6e 6b 65 64 0a 20 2a 20 75  ist (linked. * u
6bc00 73 69 6e 67 20 74 68 65 20 22 70 4e 65 78 74 22  sing the "pNext"
6bc10 20 6d 65 6d 62 65 72 29 20 72 65 66 65 72 65 6e   member) referen
6bc20 63 65 64 20 62 79 20 74 68 65 20 22 73 74 65 70  ced by the "step
6bc30 5f 6c 69 73 74 22 20 6d 65 6d 62 65 72 20 6f 66  _list" member of
6bc40 20 74 68 65 20 0a 20 2a 20 61 73 73 6f 63 69 61   the . * associa
6bc50 74 65 64 20 73 74 72 75 63 74 20 54 72 69 67 67  ted struct Trigg
6bc60 65 72 20 69 6e 73 74 61 6e 63 65 2e 20 54 68 65  er instance. The
6bc70 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f   first element o
6bc80 66 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73  f the linked lis
6bc90 74 20 69 73 0a 20 2a 20 74 68 65 20 66 69 72 73  t is. * the firs
6bca0 74 20 73 74 65 70 20 6f 66 20 74 68 65 20 74 72  t step of the tr
6bcb0 69 67 67 65 72 2d 70 72 6f 67 72 61 6d 2e 0a 20  igger-program.. 
6bcc0 2a 20 0a 20 2a 20 54 68 65 20 22 6f 70 22 20 6d  * . * The "op" m
6bcd0 65 6d 62 65 72 20 69 6e 64 69 63 61 74 65 73 20  ember indicates 
6bce0 77 68 65 74 68 65 72 20 74 68 69 73 20 69 73 20  whether this is 
6bcf0 61 20 22 44 45 4c 45 54 45 22 2c 20 22 49 4e 53  a "DELETE", "INS
6bd00 45 52 54 22 2c 20 22 55 50 44 41 54 45 22 20 6f  ERT", "UPDATE" o
6bd10 72 0a 20 2a 20 22 53 45 4c 45 43 54 22 20 73 74  r. * "SELECT" st
6bd20 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 6d 65 61  atement. The mea
6bd30 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 6f 74 68  nings of the oth
6bd40 65 72 20 6d 65 6d 62 65 72 73 20 69 73 20 64 65  er members is de
6bd50 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20  termined by the 
6bd60 0a 20 2a 20 76 61 6c 75 65 20 6f 66 20 22 6f 70  . * value of "op
6bd70 22 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 2a  " as follows:. *
6bd80 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 49 4e  . * (op == TK_IN
6bd90 53 45 52 54 29 0a 20 2a 20 6f 72 63 6f 6e 66 20  SERT). * orconf 
6bda0 20 20 20 2d 3e 20 73 74 6f 72 65 73 20 74 68 65     -> stores the
6bdb0 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67   ON CONFLICT alg
6bdc0 6f 72 69 74 68 6d 0a 20 2a 20 70 53 65 6c 65 63  orithm. * pSelec
6bdd0 74 20 20 20 2d 3e 20 49 66 20 74 68 69 73 20 69  t   -> If this i
6bde0 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f  s an INSERT INTO
6bdf0 20 2e 2e 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20   ... SELECT ... 
6be00 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a  statement, then.
6be10 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   *              
6be20 74 68 69 73 20 73 74 6f 72 65 73 20 61 20 70 6f  this stores a po
6be30 69 6e 74 65 72 20 74 6f 20 74 68 65 20 53 45 4c  inter to the SEL
6be40 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 4f  ECT statement. O
6be50 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20  therwise NULL.. 
6be60 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41  * target    -> A
6be70 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74   token holding t
6be80 68 65 20 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f  he quoted name o
6be90 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 69  f the table to i
6bea0 6e 73 65 72 74 20 69 6e 74 6f 2e 0a 20 2a 20 70  nsert into.. * p
6beb0 45 78 70 72 4c 69 73 74 20 2d 3e 20 49 66 20 74  ExprList -> If t
6bec0 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54  his is an INSERT
6bed0 20 49 4e 54 4f 20 2e 2e 2e 20 56 41 4c 55 45 53   INTO ... VALUES
6bee0 20 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 2c 20   ... statement, 
6bef0 74 68 65 6e 0a 20 2a 20 20 20 20 20 20 20 20 20  then. *         
6bf00 20 20 20 20 20 74 68 69 73 20 73 74 6f 72 65 73       this stores
6bf10 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 69 6e   values to be in
6bf20 73 65 72 74 65 64 2e 20 4f 74 68 65 72 77 69 73  serted. Otherwis
6bf30 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 49 64 4c 69  e NULL.. * pIdLi
6bf40 73 74 20 20 20 2d 3e 20 49 66 20 74 68 69 73 20  st   -> If this 
6bf50 69 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54  is an INSERT INT
6bf60 4f 20 2e 2e 2e 20 28 3c 63 6f 6c 75 6d 6e 2d 6e  O ... (<column-n
6bf70 61 6d 65 73 3e 29 20 56 41 4c 55 45 53 20 2e 2e  ames>) VALUES ..
6bf80 2e 20 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20  . . *           
6bf90 20 20 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68     statement, th
6bfa0 65 6e 20 74 68 69 73 20 73 74 6f 72 65 73 20 74  en this stores t
6bfb0 68 65 20 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 20  he column-names 
6bfc0 74 6f 20 62 65 0a 20 2a 20 20 20 20 20 20 20 20  to be. *        
6bfd0 20 20 20 20 20 20 69 6e 73 65 72 74 65 64 20 69        inserted i
6bfe0 6e 74 6f 2e 0a 20 2a 0a 20 2a 20 28 6f 70 20 3d  nto.. *. * (op =
6bff0 3d 20 54 4b 5f 44 45 4c 45 54 45 29 0a 20 2a 20  = TK_DELETE). * 
6c000 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74  target    -> A t
6c010 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65  oken holding the
6c020 20 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20   quoted name of 
6c030 74 68 65 20 74 61 62 6c 65 20 74 6f 20 64 65 6c  the table to del
6c040 65 74 65 20 66 72 6f 6d 2e 0a 20 2a 20 70 57 68  ete from.. * pWh
6c050 65 72 65 20 20 20 20 2d 3e 20 54 68 65 20 57 48  ere    -> The WH
6c060 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 68  ERE clause of th
6c070 65 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65  e DELETE stateme
6c080 6e 74 20 69 66 20 6f 6e 65 20 69 73 20 73 70 65  nt if one is spe
6c090 63 69 66 69 65 64 2e 0a 20 2a 20 20 20 20 20 20  cified.. *      
6c0a0 20 20 20 20 20 20 20 20 4f 74 68 65 72 77 69 73          Otherwis
6c0b0 65 20 4e 55 4c 4c 2e 0a 20 2a 20 0a 20 2a 20 28  e NULL.. * . * (
6c0c0 6f 70 20 3d 3d 20 54 4b 5f 55 50 44 41 54 45 29  op == TK_UPDATE)
6c0d0 0a 20 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e  . * target    ->
6c0e0 20 41 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67   A token holding
6c0f0 20 74 68 65 20 71 75 6f 74 65 64 20 6e 61 6d 65   the quoted name
6c100 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
6c110 20 75 70 64 61 74 65 20 72 6f 77 73 20 6f 66 2e   update rows of.
6c120 0a 20 2a 20 70 57 68 65 72 65 20 20 20 20 2d 3e  . * pWhere    ->
6c130 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
6c140 65 20 6f 66 20 74 68 65 20 55 50 44 41 54 45 20  e of the UPDATE 
6c150 73 74 61 74 65 6d 65 6e 74 20 69 66 20 6f 6e 65  statement if one
6c160 20 69 73 20 73 70 65 63 69 66 69 65 64 2e 0a 20   is specified.. 
6c170 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f  *              O
6c180 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20  therwise NULL.. 
6c190 2a 20 70 45 78 70 72 4c 69 73 74 20 2d 3e 20 41  * pExprList -> A
6c1a0 20 6c 69 73 74 20 6f 66 20 74 68 65 20 63 6f 6c   list of the col
6c1b0 75 6d 6e 73 20 74 6f 20 75 70 64 61 74 65 20 61  umns to update a
6c1c0 6e 64 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  nd the expressio
6c1d0 6e 73 20 74 6f 20 75 70 64 61 74 65 0a 20 2a 20  ns to update. * 
6c1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65               the
6c1f0 6d 20 74 6f 2e 20 53 65 65 20 73 71 6c 69 74 65  m to. See sqlite
6c200 33 55 70 64 61 74 65 28 29 20 64 6f 63 75 6d 65  3Update() docume
6c210 6e 74 61 74 69 6f 6e 20 6f 66 20 22 70 43 68 61  ntation of "pCha
6c220 6e 67 65 73 22 0a 20 2a 20 20 20 20 20 20 20 20  nges". *        
6c230 20 20 20 20 20 20 61 72 67 75 6d 65 6e 74 2e 0a        argument..
6c240 20 2a 20 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54   * . */.struct T
6c250 72 69 67 67 65 72 53 74 65 70 20 7b 0a 20 20 75  riggerStep {.  u
6c260 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20  8 op;           
6c270 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b      /* One of TK
6c280 5f 44 45 4c 45 54 45 2c 20 54 4b 5f 55 50 44 41  _DELETE, TK_UPDA
6c290 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54  TE, TK_INSERT, T
6c2a0 4b 5f 53 45 4c 45 43 54 20 2a 2f 0a 20 20 75 38  K_SELECT */.  u8
6c2b0 20 6f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20   orconf;        
6c2c0 20 20 20 2f 2a 20 4f 45 5f 52 6f 6c 6c 62 61 63     /* OE_Rollbac
6c2d0 6b 20 65 74 63 2e 20 2a 2f 0a 20 20 54 72 69 67  k etc. */.  Trig
6c2e0 67 65 72 20 2a 70 54 72 69 67 3b 20 20 20 20 20  ger *pTrig;     
6c2f0 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72 20   /* The trigger 
6c300 74 68 61 74 20 74 68 69 73 20 73 74 65 70 20 69  that this step i
6c310 73 20 61 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20  s a part of */. 
6c320 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
6c330 3b 20 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 20  ;     /* SELECT 
6c340 73 74 61 74 6d 65 6e 74 20 6f 72 20 52 48 53 20  statment or RHS 
6c350 6f 66 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e  of INSERT INTO .
6c360 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20 2a 2f 0a  . SELECT ... */.
6c370 20 20 54 6f 6b 65 6e 20 74 61 72 67 65 74 3b 20    Token target; 
6c380 20 20 20 20 20 20 20 2f 2a 20 54 61 72 67 65 74         /* Target
6c390 20 74 61 62 6c 65 20 66 6f 72 20 44 45 4c 45 54   table for DELET
6c3a0 45 2c 20 55 50 44 41 54 45 2c 20 49 4e 53 45 52  E, UPDATE, INSER
6c3b0 54 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  T */.  Expr *pWh
6c3c0 65 72 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  ere;        /* T
6c3d0 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
6c3e0 66 6f 72 20 44 45 4c 45 54 45 20 6f 72 20 55 50  for DELETE or UP
6c3f0 44 41 54 45 20 73 74 65 70 73 20 2a 2f 0a 20 20  DATE steps */.  
6c400 45 78 70 72 4c 69 73 74 20 2a 70 45 78 70 72 4c  ExprList *pExprL
6c410 69 73 74 3b 20 2f 2a 20 53 45 54 20 63 6c 61 75  ist; /* SET clau
6c420 73 65 20 66 6f 72 20 55 50 44 41 54 45 2e 20 20  se for UPDATE.  
6c430 56 41 4c 55 45 53 20 63 6c 61 75 73 65 20 66 6f  VALUES clause fo
6c440 72 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 49 64  r INSERT */.  Id
6c450 4c 69 73 74 20 2a 70 49 64 4c 69 73 74 3b 20 20  List *pIdList;  
6c460 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d     /* Column nam
6c470 65 73 20 66 6f 72 20 49 4e 53 45 52 54 20 2a 2f  es for INSERT */
6c480 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a  .  TriggerStep *
6c490 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20  pNext;  /* Next 
6c4a0 69 6e 20 74 68 65 20 6c 69 6e 6b 2d 6c 69 73 74  in the link-list
6c4b0 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65   */.  TriggerSte
6c4c0 70 20 2a 70 4c 61 73 74 3b 20 20 2f 2a 20 4c 61  p *pLast;  /* La
6c4d0 73 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69  st element in li
6c4e0 6e 6b 2d 6c 69 73 74 2e 20 56 61 6c 69 64 20 66  nk-list. Valid f
6c4f0 6f 72 20 31 73 74 20 65 6c 65 6d 20 6f 6e 6c 79  or 1st elem only
6c500 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68   */.};../*.** Th
6c510 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
6c520 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 69  cture contains i
6c530 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20  nformation used 
6c540 62 79 20 74 68 65 20 73 71 6c 69 74 65 46 69 78  by the sqliteFix
6c550 2e 2e 2e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20  ....** routines 
6c560 61 73 20 74 68 65 79 20 77 61 6c 6b 20 74 68 65  as they walk the
6c570 20 70 61 72 73 65 20 74 72 65 65 20 74 6f 20 6d   parse tree to m
6c580 61 6b 65 20 64 61 74 61 62 61 73 65 20 72 65 66  ake database ref
6c590 65 72 65 6e 63 65 73 0a 2a 2a 20 65 78 70 6c 69  erences.** expli
6c5a0 63 69 74 2e 20 20 0a 2a 2f 0a 74 79 70 65 64 65  cit.  .*/.typede
6c5b0 66 20 73 74 72 75 63 74 20 44 62 46 69 78 65 72  f struct DbFixer
6c5c0 20 44 62 46 69 78 65 72 3b 0a 73 74 72 75 63 74   DbFixer;.struct
6c5d0 20 44 62 46 69 78 65 72 20 7b 0a 20 20 50 61 72   DbFixer {.  Par
6c5e0 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20  se *pParse;     
6c5f0 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
6c600 63 6f 6e 74 65 78 74 2e 20 20 45 72 72 6f 72 20  context.  Error 
6c610 6d 65 73 73 61 67 65 73 20 77 72 69 74 74 65 6e  messages written
6c620 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74   here */.  const
6c630 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 2f   char *zDb;    /
6c640 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20  * Make sure all 
6c650 6f 62 6a 65 63 74 73 20 61 72 65 20 63 6f 6e 74  objects are cont
6c660 61 69 6e 65 64 20 69 6e 20 74 68 69 73 20 64 61  ained in this da
6c670 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73  tabase */.  cons
6c680 74 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20  t char *zType;  
6c690 2f 2a 20 54 79 70 65 20 6f 66 20 74 68 65 20 63  /* Type of the c
6c6a0 6f 6e 74 61 69 6e 65 72 20 2d 20 75 73 65 64 20  ontainer - used 
6c6b0 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67  for error messag
6c6c0 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f  es */.  const To
6c6d0 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 2f 2a 20 4e  ken *pName; /* N
6c6e0 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61  ame of the conta
6c6f0 69 6e 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20  iner - used for 
6c700 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a  error messages *
6c710 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f  /.};../*.** An o
6c720 62 6a 65 63 74 65 64 20 75 73 65 64 20 74 6f 20  bjected used to 
6c730 61 63 63 75 6d 75 6c 61 74 65 20 74 68 65 20 74  accumulate the t
6c740 65 78 74 20 6f 66 20 61 20 73 74 72 69 6e 67 20  ext of a string 
6c750 77 68 65 72 65 20 77 65 0a 2a 2a 20 64 6f 20 6e  where we.** do n
6c760 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 6b  ot necessarily k
6c770 6e 6f 77 20 68 6f 77 20 62 69 67 20 74 68 65 20  now how big the 
6c780 73 74 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 69  string will be i
6c790 6e 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74  n the end..*/.st
6c7a0 72 75 63 74 20 53 74 72 41 63 63 75 6d 20 7b 0a  ruct StrAccum {.
6c7b0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
6c7c0 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e         /* Option
6c7d0 61 6c 20 64 61 74 61 62 61 73 65 20 66 6f 72 20  al database for 
6c7e0 6c 6f 6f 6b 61 73 69 64 65 2e 20 20 43 61 6e 20  lookaside.  Can 
6c7f0 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 68 61  be NULL */.  cha
6c800 72 20 2a 7a 42 61 73 65 3b 20 20 20 20 20 20 20  r *zBase;       
6c810 20 20 2f 2a 20 41 20 62 61 73 65 20 61 6c 6c 6f    /* A base allo
6c820 63 61 74 69 6f 6e 2e 20 20 4e 6f 74 20 66 72 6f  cation.  Not fro
6c830 6d 20 6d 61 6c 6c 6f 63 2e 20 2a 2f 0a 20 20 63  m malloc. */.  c
6c840 68 61 72 20 2a 7a 54 65 78 74 3b 20 20 20 20 20  har *zText;     
6c850 20 20 20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e      /* The strin
6c860 67 20 63 6f 6c 6c 65 63 74 65 64 20 73 6f 20 66  g collected so f
6c870 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 43 68  ar */.  int  nCh
6c880 61 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ar;          /* 
6c890 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 74  Length of the st
6c8a0 72 69 6e 67 20 73 6f 20 66 61 72 20 2a 2f 0a 20  ring so far */. 
6c8b0 20 69 6e 74 20 20 6e 41 6c 6c 6f 63 3b 20 20 20   int  nAlloc;   
6c8c0 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20        /* Amount 
6c8d0 6f 66 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74  of space allocat
6c8e0 65 64 20 69 6e 20 7a 54 65 78 74 20 2a 2f 0a 20  ed in zText */. 
6c8f0 20 69 6e 74 20 20 6d 78 41 6c 6c 6f 63 3b 20 20   int  mxAlloc;  
6c900 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d        /* Maximum
6c910 20 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20   allowed string 
6c920 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 75 38 20 20  length */.  u8  
6c930 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20   mallocFailed;  
6c940 20 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 65   /* Becomes true
6c950 20 69 66 20 61 6e 79 20 6d 65 6d 6f 72 79 20 61   if any memory a
6c960 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 20  llocation fails 
6c970 2a 2f 0a 20 20 75 38 20 20 20 75 73 65 4d 61 6c  */.  u8   useMal
6c980 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 54 72 75  loc;      /* Tru
6c990 65 20 69 66 20 7a 54 65 78 74 20 69 73 20 65 6e  e if zText is en
6c9a0 6c 61 72 67 65 61 62 6c 65 20 75 73 69 6e 67 20  largeable using 
6c9b0 72 65 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 38 20  realloc */.  u8 
6c9c0 20 20 74 6f 6f 42 69 67 3b 20 20 20 20 20 20 20    tooBig;       
6c9d0 20 20 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 75    /* Becomes tru
6c9e0 65 20 69 66 20 73 74 72 69 6e 67 20 73 69 7a 65  e if string size
6c9f0 20 65 78 63 65 65 64 73 20 6c 69 6d 69 74 73 20   exceeds limits 
6ca00 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 70  */.};../*.** A p
6ca10 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73  ointer to this s
6ca20 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64  tructure is used
6ca30 20 74 6f 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20   to communicate 
6ca40 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66  information.** f
6ca50 72 6f 6d 20 73 71 6c 69 74 65 33 49 6e 69 74 20  rom sqlite3Init 
6ca60 61 6e 64 20 4f 50 5f 50 61 72 73 65 53 63 68 65  and OP_ParseSche
6ca70 6d 61 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69  ma into the sqli
6ca80 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2e  te3InitCallback.
6ca90 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
6caa0 63 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  ct {.  sqlite3 *
6cab0 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68  db;        /* Th
6cac0 65 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67  e database being
6cad0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a   initialized */.
6cae0 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
6caf0 20 20 20 20 20 20 2f 2a 20 30 20 66 6f 72 20 6d        /* 0 for m
6cb00 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20 20 31  ain database.  1
6cb10 20 66 6f 72 20 54 45 4d 50 2c 20 32 2e 2e 20 66   for TEMP, 2.. f
6cb20 6f 72 20 41 54 54 41 43 48 65 64 20 2a 2f 0a 20  or ATTACHed */. 
6cb30 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67   char **pzErrMsg
6cb40 3b 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65  ;    /* Error me
6cb50 73 73 61 67 65 20 73 74 6f 72 65 64 20 68 65 72  ssage stored her
6cb60 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  e */.  int rc;  
6cb70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
6cb80 73 75 6c 74 20 63 6f 64 65 20 73 74 6f 72 65 64  sult code stored
6cb90 20 68 65 72 65 20 2a 2f 0a 7d 20 49 6e 69 74 44   here */.} InitD
6cba0 61 74 61 3b 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75  ata;../*.** Stru
6cbb0 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67  cture containing
6cbc0 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72   global configur
6cbd0 61 74 69 6f 6e 20 64 61 74 61 20 66 6f 72 20 74  ation data for t
6cbe0 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72  he SQLite librar
6cbf0 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74  y..**.** This st
6cc00 72 75 63 74 75 72 65 20 61 6c 73 6f 20 63 6f 6e  ructure also con
6cc10 74 61 69 6e 73 20 73 6f 6d 65 20 73 74 61 74 65  tains some state
6cc20 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f   information..*/
6cc30 0a 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43  .struct Sqlite3C
6cc40 6f 6e 66 69 67 20 7b 0a 20 20 69 6e 74 20 62 4d  onfig {.  int bM
6cc50 65 6d 73 74 61 74 3b 20 20 20 20 20 20 20 20 20  emstat;         
6cc60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
6cc70 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 6d 65  rue to enable me
6cc80 6d 6f 72 79 20 73 74 61 74 75 73 20 2a 2f 0a 20  mory status */. 
6cc90 20 69 6e 74 20 62 43 6f 72 65 4d 75 74 65 78 3b   int bCoreMutex;
6cca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ccb0 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e     /* True to en
6ccc0 61 62 6c 65 20 63 6f 72 65 20 6d 75 74 65 78 69  able core mutexi
6ccd0 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 46 75 6c  ng */.  int bFul
6cce0 6c 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20  lMutex;         
6ccf0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
6cd00 65 20 74 6f 20 65 6e 61 62 6c 65 20 66 75 6c 6c  e to enable full
6cd10 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20 69   mutexing */.  i
6cd20 6e 74 20 6d 78 53 74 72 6c 65 6e 3b 20 20 20 20  nt mxStrlen;    
6cd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6cd40 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 73 74 72 69   /* Maximum stri
6cd50 6e 67 20 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 69  ng length */.  i
6cd60 6e 74 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 3b 20  nt szLookaside; 
6cd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6cd80 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f 6f 6b   /* Default look
6cd90 61 73 69 64 65 20 62 75 66 66 65 72 20 73 69 7a  aside buffer siz
6cda0 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 6f 6b  e */.  int nLook
6cdb0 61 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 20  aside;          
6cdc0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61           /* Defa
6cdd0 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75  ult lookaside bu
6cde0 66 66 65 72 20 63 6f 75 6e 74 20 2a 2f 0a 20 20  ffer count */.  
6cdf0 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
6ce00 6f 64 73 20 6d 3b 20 20 20 20 20 20 20 20 20 20  ods m;          
6ce10 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d    /* Low-level m
6ce20 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
6ce30 20 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20   interface */.  
6ce40 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
6ce50 74 68 6f 64 73 20 6d 75 74 65 78 3b 20 20 20 20  thods mutex;    
6ce60 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d    /* Low-level m
6ce70 75 74 65 78 20 69 6e 74 65 72 66 61 63 65 20 2a  utex interface *
6ce80 2f 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63  /.  sqlite3_pcac
6ce90 68 65 5f 6d 65 74 68 6f 64 73 20 70 63 61 63 68  he_methods pcach
6cea0 65 3b 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76  e;    /* Low-lev
6ceb0 65 6c 20 70 61 67 65 2d 63 61 63 68 65 20 69 6e  el page-cache in
6cec0 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 76 6f 69  terface */.  voi
6ced0 64 20 2a 70 48 65 61 70 3b 20 20 20 20 20 20 20  d *pHeap;       
6cee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6cef0 2a 20 48 65 61 70 20 73 74 6f 72 61 67 65 20 73  * Heap storage s
6cf00 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48  pace */.  int nH
6cf10 65 61 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  eap;            
6cf20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
6cf30 69 7a 65 20 6f 66 20 70 48 65 61 70 5b 5d 20 2a  ize of pHeap[] *
6cf40 2f 0a 20 20 69 6e 74 20 6d 6e 52 65 71 2c 20 6d  /.  int mnReq, m
6cf50 78 52 65 71 3b 20 20 20 20 20 20 20 20 20 20 20  xReq;           
6cf60 20 20 20 20 20 20 2f 2a 20 4d 69 6e 20 61 6e 64        /* Min and
6cf70 20 6d 61 78 20 68 65 61 70 20 72 65 71 75 65 73   max heap reques
6cf80 74 73 20 73 69 7a 65 73 20 2a 2f 0a 20 20 76 6f  ts sizes */.  vo
6cf90 69 64 20 2a 70 53 63 72 61 74 63 68 3b 20 20 20  id *pScratch;   
6cfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6cfb0 2f 2a 20 53 63 72 61 74 63 68 20 6d 65 6d 6f 72  /* Scratch memor
6cfc0 79 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72  y */.  int szScr
6cfd0 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20  atch;           
6cfe0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
6cff0 20 6f 66 20 65 61 63 68 20 73 63 72 61 74 63 68   of each scratch
6d000 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74   buffer */.  int
6d010 20 6e 53 63 72 61 74 63 68 3b 20 20 20 20 20 20   nScratch;      
6d020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6d030 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 63 72 61  * Number of scra
6d040 74 63 68 20 62 75 66 66 65 72 73 20 2a 2f 0a 20  tch buffers */. 
6d050 20 76 6f 69 64 20 2a 70 50 61 67 65 3b 20 20 20   void *pPage;   
6d060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d070 20 20 20 2f 2a 20 50 61 67 65 20 63 61 63 68 65     /* Page cache
6d080 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74   memory */.  int
6d090 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20   szPage;        
6d0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6d0b0 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 70  * Size of each p
6d0c0 61 67 65 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a  age in pPage[] *
6d0d0 2f 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 20  /.  int nPage;  
6d0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d0f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
6d100 6f 66 20 70 61 67 65 73 20 69 6e 20 70 50 61 67  of pages in pPag
6d110 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50  e[] */.  int mxP
6d120 61 72 73 65 72 53 74 61 63 6b 3b 20 20 20 20 20  arserStack;     
6d130 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 61             /* ma
6d140 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74  ximum depth of t
6d150 68 65 20 70 61 72 73 65 72 20 73 74 61 63 6b 20  he parser stack 
6d160 2a 2f 0a 20 20 69 6e 74 20 73 68 61 72 65 64 43  */.  int sharedC
6d170 61 63 68 65 45 6e 61 62 6c 65 64 3b 20 20 20 20  acheEnabled;    
6d180 20 20 20 20 20 20 20 2f 2a 20 74 72 75 65 20 69         /* true i
6d190 66 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d  f shared-cache m
6d1a0 6f 64 65 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20  ode enabled */. 
6d1b0 20 2f 2a 20 54 68 65 20 61 62 6f 76 65 20 6d 69   /* The above mi
6d1c0 67 68 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a  ght be initializ
6d1d0 65 64 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 2e 20  ed to non-zero. 
6d1e0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e   The following n
6d1f0 65 65 64 20 74 6f 20 61 6c 77 61 79 73 0a 20 20  eed to always.  
6d200 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 20 62 65 20  ** initially be 
6d210 7a 65 72 6f 2c 20 68 6f 77 65 76 65 72 2e 20 2a  zero, however. *
6d220 2f 0a 20 20 69 6e 74 20 69 73 49 6e 69 74 3b 20  /.  int isInit; 
6d230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d240 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66        /* True af
6d250 74 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  ter initializati
6d260 6f 6e 20 68 61 73 20 66 69 6e 69 73 68 65 64 20  on has finished 
6d270 2a 2f 0a 20 20 69 6e 74 20 69 6e 50 72 6f 67 72  */.  int inProgr
6d280 65 73 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ess;            
6d290 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 77         /* True w
6d2a0 68 69 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61 74  hile initializat
6d2b0 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 20  ion in progress 
6d2c0 2a 2f 0a 20 20 69 6e 74 20 69 73 4d 75 74 65 78  */.  int isMutex
6d2d0 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20  Init;           
6d2e0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61         /* True a
6d2f0 66 74 65 72 20 6d 75 74 65 78 65 73 20 61 72 65  fter mutexes are
6d300 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a   initialized */.
6d310 20 20 69 6e 74 20 69 73 4d 61 6c 6c 6f 63 49 6e    int isMallocIn
6d320 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  it;             
6d330 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65      /* True afte
6d340 72 20 6d 61 6c 6c 6f 63 20 69 73 20 69 6e 69 74  r malloc is init
6d350 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74  ialized */.  int
6d360 20 69 73 50 43 61 63 68 65 49 6e 69 74 3b 20 20   isPCacheInit;  
6d370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6d380 2a 20 54 72 75 65 20 61 66 74 65 72 20 6d 61 6c  * True after mal
6d390 6c 6f 63 20 69 73 20 69 6e 69 74 69 61 6c 69 7a  loc is initializ
6d3a0 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ed */.  sqlite3_
6d3b0 6d 75 74 65 78 20 2a 70 49 6e 69 74 4d 75 74 65  mutex *pInitMute
6d3c0 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74  x;        /* Mut
6d3d0 65 78 20 75 73 65 64 20 62 79 20 73 71 6c 69 74  ex used by sqlit
6d3e0 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
6d3f0 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 49 6e 69  */.  int nRefIni
6d400 74 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20  tMutex;         
6d410 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
6d420 20 6f 66 20 75 73 65 72 73 20 6f 66 20 70 49 6e   of users of pIn
6d430 69 74 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f  itMutex */.};../
6d440 2a 0a 2a 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69  *.** Context poi
6d450 6e 74 65 72 20 70 61 73 73 65 64 20 64 6f 77 6e  nter passed down
6d460 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 72 65   through the tre
6d470 65 2d 77 61 6c 6b 2e 0a 2a 2f 0a 73 74 72 75 63  e-walk..*/.struc
6d480 74 20 57 61 6c 6b 65 72 20 7b 0a 20 20 69 6e 74  t Walker {.  int
6d490 20 28 2a 78 45 78 70 72 43 61 6c 6c 62 61 63 6b   (*xExprCallback
6d4a0 29 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 2a  )(Walker*, Expr*
6d4b0 29 3b 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61  );     /* Callba
6d4c0 63 6b 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f  ck for expressio
6d4d0 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53  ns */.  int (*xS
6d4e0 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 29 28 57  electCallback)(W
6d4f0 61 6c 6b 65 72 2a 2c 53 65 6c 65 63 74 2a 29 3b  alker*,Select*);
6d500 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f    /* Callback fo
6d510 72 20 53 45 4c 45 43 54 73 20 2a 2f 0a 20 20 50  r SELECTs */.  P
6d520 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20  arse *pParse;   
6d530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d540 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
6d550 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 2a 2f 0a  er context.  */.
6d560 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20    union {       
6d570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d580 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
6d590 78 74 72 61 20 64 61 74 61 20 66 6f 72 20 63 61  xtra data for ca
6d5a0 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 20 20 4e 61  llback */.    Na
6d5b0 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 20  meContext *pNC; 
6d5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d5d0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 69           /* Nami
6d5e0 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
6d5f0 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
6d600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d610 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6d620 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 2a 2f  Integer value */
6d630 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 20 46  .  } u;.};../* F
6d640 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
6d650 6f 6e 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ons */.SQLITE_PR
6d660 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6d670 33 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72  3WalkExpr(Walker
6d680 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54  *, Expr*);.SQLIT
6d690 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6d6a0 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73  lite3WalkExprLis
6d6b0 74 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 4c  t(Walker*, ExprL
6d6c0 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ist*);.SQLITE_PR
6d6d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6d6e0 33 57 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b  3WalkSelect(Walk
6d6f0 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53  er*, Select*);.S
6d700 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6d710 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c  t sqlite3WalkSel
6d720 65 63 74 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c  ectExpr(Walker*,
6d730 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54   Select*);.SQLIT
6d740 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6d750 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46  lite3WalkSelectF
6d760 72 6f 6d 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c  rom(Walker*, Sel
6d770 65 63 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65  ect*);../*.** Re
6d780 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74  turn code from t
6d790 68 65 20 70 61 72 73 65 2d 74 72 65 65 20 77 61  he parse-tree wa
6d7a0 6c 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 65 73  lking primitives
6d7b0 20 61 6e 64 20 74 68 65 69 72 0a 2a 2a 20 63 61   and their.** ca
6d7c0 6c 6c 62 61 63 6b 73 2e 0a 2a 2f 0a 23 64 65 66  llbacks..*/.#def
6d7d0 69 6e 65 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65  ine WRC_Continue
6d7e0 20 20 20 20 30 20 20 20 2f 2a 20 43 6f 6e 74 69      0   /* Conti
6d7f0 6e 75 65 20 64 6f 77 6e 20 69 6e 74 6f 20 63 68  nue down into ch
6d800 69 6c 64 72 65 6e 20 2a 2f 0a 23 64 65 66 69 6e  ildren */.#defin
6d810 65 20 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20  e WRC_Prune     
6d820 20 20 31 20 20 20 2f 2a 20 4f 6d 69 74 20 63 68    1   /* Omit ch
6d830 69 6c 64 72 65 6e 20 62 75 74 20 63 6f 6e 74 69  ildren but conti
6d840 6e 75 65 20 77 61 6c 6b 69 6e 67 20 73 69 62 6c  nue walking sibl
6d850 69 6e 67 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ings */.#define 
6d860 57 52 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20  WRC_Abort       
6d870 32 20 20 20 2f 2a 20 41 62 61 6e 64 6f 6e 20 74  2   /* Abandon t
6d880 68 65 20 74 72 65 65 20 77 61 6c 6b 20 2a 2f 0a  he tree walk */.
6d890 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20  ./*.** Assuming 
6d8a0 7a 49 6e 20 70 6f 69 6e 74 73 20 74 6f 20 74 68  zIn points to th
6d8b0 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20  e first byte of 
6d8c0 61 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65  a UTF-8 characte
6d8d0 72 2c 0a 2a 2a 20 61 64 76 61 6e 63 65 20 7a 49  r,.** advance zI
6d8e0 6e 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  n to point to th
6d8f0 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20  e first byte of 
6d900 74 68 65 20 6e 65 78 74 20 55 54 46 2d 38 20 63  the next UTF-8 c
6d910 68 61 72 61 63 74 65 72 2e 0a 2a 2f 0a 23 64 65  haracter..*/.#de
6d920 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4b 49 50  fine SQLITE_SKIP
6d930 5f 55 54 46 38 28 7a 49 6e 29 20 7b 20 20 20 20  _UTF8(zIn) {    
6d940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d950 20 20 20 20 5c 0a 20 20 69 66 28 20 28 2a 28 7a      \.  if( (*(z
6d960 49 6e 2b 2b 29 29 3e 3d 30 78 63 30 20 29 7b 20  In++))>=0xc0 ){ 
6d970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d980 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
6d990 20 20 20 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20     while( (*zIn 
6d9a0 26 20 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b  & 0xc0)==0x80 ){
6d9b0 20 7a 49 6e 2b 2b 3b 20 7d 20 20 20 20 20 20 20   zIn++; }       
6d9c0 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20        \.  }     
6d9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
6da00 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51  .}../*.** The SQ
6da10 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
6da20 54 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 65  T macro can be e
6da30 69 74 68 65 72 20 61 20 63 6f 6e 73 74 61 6e 74  ither a constant
6da40 20 28 66 6f 72 20 70 72 6f 64 75 63 74 69 6f 6e   (for production
6da50 0a 2a 2a 20 62 75 69 6c 64 73 29 20 6f 72 20 61  .** builds) or a
6da60 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 28   function call (
6da70 66 6f 72 20 64 65 62 75 67 67 69 6e 67 29 2e 20  for debugging). 
6da80 20 49 66 20 69 74 20 69 73 20 61 20 66 75 6e 63   If it is a func
6da90 74 69 6f 6e 20 63 61 6c 6c 2c 0a 2a 2a 20 69 74  tion call,.** it
6daa0 20 61 6c 6c 6f 77 73 20 74 68 65 20 6f 70 65 72   allows the oper
6dab0 61 74 6f 72 20 74 6f 20 73 65 74 20 61 20 62 72  ator to set a br
6dac0 65 61 6b 70 6f 69 6e 74 20 61 74 20 74 68 65 20  eakpoint at the 
6dad0 73 70 6f 74 20 77 68 65 72 65 20 64 61 74 61 62  spot where datab
6dae0 61 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f  ase.** corruptio
6daf0 6e 20 69 73 20 66 69 72 73 74 20 64 65 74 65 63  n is first detec
6db00 74 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ted..*/.#ifdef S
6db10 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49  QLITE_DEBUG.SQLI
6db20 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
6db30 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28   sqlite3Corrupt(
6db40 76 6f 69 64 29 3b 0a 23 20 64 65 66 69 6e 65 20  void);.# define 
6db50 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
6db60 4b 50 54 20 73 71 6c 69 74 65 33 43 6f 72 72 75  KPT sqlite3Corru
6db70 70 74 28 29 0a 23 65 6c 73 65 0a 23 20 64 65 66  pt().#else.# def
6db80 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  ine SQLITE_CORRU
6db90 50 54 5f 42 4b 50 54 20 53 51 4c 49 54 45 5f 43  PT_BKPT SQLITE_C
6dba0 4f 52 52 55 50 54 0a 23 65 6e 64 69 66 0a 0a 2f  ORRUPT.#endif../
6dbb0 2a 0a 2a 2a 20 54 68 65 20 63 74 79 70 65 2e 68  *.** The ctype.h
6dbc0 20 68 65 61 64 65 72 20 69 73 20 6e 65 65 64 65   header is neede
6dbd0 64 20 66 6f 72 20 6e 6f 6e 2d 41 53 43 49 49 20  d for non-ASCII 
6dbe0 73 79 73 74 65 6d 73 2e 20 20 49 74 20 69 73 20  systems.  It is 
6dbf0 61 6c 73 6f 0a 2a 2a 20 6e 65 65 64 65 64 20 62  also.** needed b
6dc00 79 20 46 54 53 33 20 77 68 65 6e 20 46 54 53 33  y FTS3 when FTS3
6dc10 20 69 73 20 69 6e 63 6c 75 64 65 64 20 69 6e 20   is included in 
6dc20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e  the amalgamation
6dc30 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65  ..*/.#if !define
6dc40 64 28 53 51 4c 49 54 45 5f 41 53 43 49 49 29 20  d(SQLITE_ASCII) 
6dc50 7c 7c 20 5c 0a 20 20 20 20 28 64 65 66 69 6e 65  || \.    (define
6dc60 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
6dc70 46 54 53 33 29 20 26 26 20 64 65 66 69 6e 65 64  FTS3) && defined
6dc80 28 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41  (SQLITE_AMALGAMA
6dc90 54 49 4f 4e 29 29 0a 23 20 69 6e 63 6c 75 64 65  TION)).# include
6dca0 20 3c 63 74 79 70 65 2e 68 3e 0a 23 65 6e 64 69   <ctype.h>.#endi
6dcb0 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  f../*.** The fol
6dcc0 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 69  lowing macros mi
6dcd0 6d 69 63 20 74 68 65 20 73 74 61 6e 64 61 72 64  mic the standard
6dce0 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f   library functio
6dcf0 6e 73 20 74 6f 75 70 70 65 72 28 29 2c 0a 2a 2a  ns toupper(),.**
6dd00 20 69 73 73 70 61 63 65 28 29 2c 20 69 73 61 6c   isspace(), isal
6dd10 6e 75 6d 28 29 2c 20 69 73 64 69 67 69 74 28 29  num(), isdigit()
6dd20 20 61 6e 64 20 69 73 78 64 69 67 69 74 28 29 2c   and isxdigit(),
6dd30 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54   respectively. T
6dd40 68 65 0a 2a 2a 20 73 71 6c 69 74 65 20 76 65 72  he.** sqlite ver
6dd50 73 69 6f 6e 73 20 6f 6e 6c 79 20 77 6f 72 6b 20  sions only work 
6dd60 66 6f 72 20 41 53 43 49 49 20 63 68 61 72 61 63  for ASCII charac
6dd70 74 65 72 73 2c 20 72 65 67 61 72 64 6c 65 73 73  ters, regardless
6dd80 20 6f 66 20 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a 23   of locale..*/.#
6dd90 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43  ifdef SQLITE_ASC
6dda0 49 49 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  II.# define sqli
6ddb0 74 65 33 54 6f 75 70 70 65 72 28 78 29 20 20 28  te3Toupper(x)  (
6ddc0 28 78 29 26 7e 28 73 71 6c 69 74 65 33 43 74 79  (x)&~(sqlite3Cty
6ddd0 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20  peMap[(unsigned 
6dde0 63 68 61 72 29 28 78 29 5d 26 30 78 32 30 29 29  char)(x)]&0x20))
6ddf0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6de00 33 49 73 73 70 61 63 65 28 78 29 20 20 20 28 73  3Isspace(x)   (s
6de10 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28  qlite3CtypeMap[(
6de20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78  unsigned char)(x
6de30 29 5d 26 30 78 30 31 29 0a 23 20 64 65 66 69 6e  )]&0x01).# defin
6de40 65 20 73 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d  e sqlite3Isalnum
6de50 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 43 74  (x)   (sqlite3Ct
6de60 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64  ypeMap[(unsigned
6de70 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 36 29   char)(x)]&0x06)
6de80 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6de90 33 49 73 61 6c 70 68 61 28 78 29 20 20 20 28 73  3Isalpha(x)   (s
6dea0 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28  qlite3CtypeMap[(
6deb0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78  unsigned char)(x
6dec0 29 5d 26 30 78 30 32 29 0a 23 20 64 65 66 69 6e  )]&0x02).# defin
6ded0 65 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  e sqlite3Isdigit
6dee0 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 43 74  (x)   (sqlite3Ct
6def0 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64  ypeMap[(unsigned
6df00 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 34 29   char)(x)]&0x04)
6df10 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6df20 33 49 73 78 64 69 67 69 74 28 78 29 20 20 28 73  3Isxdigit(x)  (s
6df30 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28  qlite3CtypeMap[(
6df40 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78  unsigned char)(x
6df50 29 5d 26 30 78 30 38 29 0a 23 20 64 65 66 69 6e  )]&0x08).# defin
6df60 65 20 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72  e sqlite3Tolower
6df70 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 55 70  (x)   (sqlite3Up
6df80 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69  perToLower[(unsi
6df90 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 29 0a  gned char)(x)]).
6dfa0 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73  #else.# define s
6dfb0 71 6c 69 74 65 33 54 6f 75 70 70 65 72 28 78 29  qlite3Toupper(x)
6dfc0 20 20 20 74 6f 75 70 70 65 72 28 28 75 6e 73 69     toupper((unsi
6dfd0 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a 23  gned char)(x)).#
6dfe0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49   define sqlite3I
6dff0 73 73 70 61 63 65 28 78 29 20 20 20 69 73 73 70  sspace(x)   issp
6e000 61 63 65 28 28 75 6e 73 69 67 6e 65 64 20 63 68  ace((unsigned ch
6e010 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65  ar)(x)).# define
6e020 20 73 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28   sqlite3Isalnum(
6e030 78 29 20 20 20 69 73 61 6c 6e 75 6d 28 28 75 6e  x)   isalnum((un
6e040 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29  signed char)(x))
6e050 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6e060 33 49 73 61 6c 70 68 61 28 78 29 20 20 20 69 73  3Isalpha(x)   is
6e070 61 6c 70 68 61 28 28 75 6e 73 69 67 6e 65 64 20  alpha((unsigned 
6e080 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 69  char)(x)).# defi
6e090 6e 65 20 73 71 6c 69 74 65 33 49 73 64 69 67 69  ne sqlite3Isdigi
6e0a0 74 28 78 29 20 20 20 69 73 64 69 67 69 74 28 28  t(x)   isdigit((
6e0b0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78  unsigned char)(x
6e0c0 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  )).# define sqli
6e0d0 74 65 33 49 73 78 64 69 67 69 74 28 78 29 20 20  te3Isxdigit(x)  
6e0e0 69 73 78 64 69 67 69 74 28 28 75 6e 73 69 67 6e  isxdigit((unsign
6e0f0 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64  ed char)(x)).# d
6e100 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 6f 6c  efine sqlite3Tol
6e110 6f 77 65 72 28 78 29 20 20 20 74 6f 6c 6f 77 65  ower(x)   tolowe
6e120 72 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  r((unsigned char
6e130 29 28 78 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  )(x)).#endif../*
6e140 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e  .** Internal fun
6e150 63 74 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73  ction prototypes
6e160 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
6e170 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74  TE int sqlite3St
6e180 72 49 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72  rICmp(const char
6e190 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
6e1a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6e1b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e  E int sqlite3IsN
6e1c0 75 6d 62 65 72 28 63 6f 6e 73 74 20 63 68 61 72  umber(const char
6e1d0 2a 2c 20 69 6e 74 2a 2c 20 75 38 29 3b 0a 53 51  *, int*, u8);.SQ
6e1e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6e1f0 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
6e200 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23  (const char*);.#
6e210 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74  define sqlite3St
6e220 72 4e 49 43 6d 70 20 73 71 6c 69 74 65 33 5f 73  rNICmp sqlite3_s
6e230 74 72 6e 69 63 6d 70 0a 0a 53 51 4c 49 54 45 5f  trnicmp..SQLITE_
6e240 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6e250 74 65 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f  te3MallocInit(vo
6e260 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  id);.SQLITE_PRIV
6e270 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6e280 4d 61 6c 6c 6f 63 45 6e 64 28 76 6f 69 64 29 3b  MallocEnd(void);
6e290 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6e2a0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c  void *sqlite3Mal
6e2b0 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45  loc(int);.SQLITE
6e2c0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
6e2d0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
6e2e0 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  (int);.SQLITE_PR
6e2f0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
6e300 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
6e310 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a  sqlite3*, int);.
6e320 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6e330 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61  oid *sqlite3DbMa
6e340 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74 65 33 2a  llocRaw(sqlite3*
6e350 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
6e360 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
6e370 69 74 65 33 44 62 53 74 72 44 75 70 28 73 71 6c  ite3DbStrDup(sql
6e380 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ite3*,const char
6e390 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6e3a0 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
6e3b0 44 62 53 74 72 4e 44 75 70 28 73 71 6c 69 74 65  DbStrNDup(sqlite
6e3c0 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  3*,const char*, 
6e3d0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6e3e0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
6e3f0 65 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c  e3Realloc(void*,
6e400 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
6e410 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
6e420 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72  te3DbReallocOrFr
6e430 65 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f  ee(sqlite3 *, vo
6e440 69 64 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  id *, int);.SQLI
6e450 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6e460 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f  *sqlite3DbReallo
6e470 63 28 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69  c(sqlite3 *, voi
6e480 64 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  d *, int);.SQLIT
6e490 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6e4a0 71 6c 69 74 65 33 44 62 46 72 65 65 28 73 71 6c  qlite3DbFree(sql
6e4b0 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53  ite3*, void*);.S
6e4c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6e4d0 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53  t sqlite3MallocS
6e4e0 69 7a 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49  ize(void*);.SQLI
6e4f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6e500 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69  qlite3DbMallocSi
6e510 7a 65 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69  ze(sqlite3*, voi
6e520 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  d*);.SQLITE_PRIV
6e530 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
6e540 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 69  3ScratchMalloc(i
6e550 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6e560 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6e570 53 63 72 61 74 63 68 46 72 65 65 28 76 6f 69 64  ScratchFree(void
6e580 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6e590 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
6e5a0 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b  PageMalloc(int);
6e5b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6e5c0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
6e5d0 46 72 65 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c  Free(void*);.SQL
6e5e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6e5f0 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65   sqlite3MemSetDe
6e600 66 61 75 6c 74 28 76 6f 69 64 29 3b 0a 53 51 4c  fault(void);.SQL
6e610 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6e620 20 73 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61   sqlite3BenignMa
6e630 6c 6c 6f 63 48 6f 6f 6b 73 28 76 6f 69 64 20 28  llocHooks(void (
6e640 2a 29 28 76 6f 69 64 29 2c 20 76 6f 69 64 20 28  *)(void), void (
6e650 2a 29 28 76 6f 69 64 29 29 3b 0a 53 51 4c 49 54  *)(void));.SQLIT
6e660 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6e670 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d  lite3MemoryAlarm
6e680 28 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a 2c  (void (*)(void*,
6e690 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20   sqlite3_int64, 
6e6a0 69 6e 74 29 2c 20 76 6f 69 64 2a 2c 20 73 71 6c  int), void*, sql
6e6b0 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 0a 2f 2a  ite3_int64);../*
6e6c0 0a 2a 2a 20 4f 6e 20 73 79 73 74 65 6d 73 20 77  .** On systems w
6e6d0 69 74 68 20 61 6d 70 6c 65 20 73 74 61 63 6b 20  ith ample stack 
6e6e0 73 70 61 63 65 20 61 6e 64 20 74 68 61 74 20 73  space and that s
6e6f0 75 70 70 6f 72 74 20 61 6c 6c 6f 63 61 28 29 2c  upport alloca(),
6e700 20 6d 61 6b 65 0a 2a 2a 20 75 73 65 20 6f 66 20   make.** use of 
6e710 61 6c 6c 6f 63 61 28 29 20 74 6f 20 6f 62 74 61  alloca() to obta
6e720 69 6e 20 73 70 61 63 65 20 66 6f 72 20 6c 61 72  in space for lar
6e730 67 65 20 61 75 74 6f 6d 61 74 69 63 20 6f 62 6a  ge automatic obj
6e740 65 63 74 73 2e 20 20 42 79 20 64 65 66 61 75 6c  ects.  By defaul
6e750 74 2c 0a 2a 2a 20 6f 62 74 61 69 6e 20 73 70 61  t,.** obtain spa
6e760 63 65 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29  ce from malloc()
6e770 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f  ..**.** The allo
6e780 63 61 28 29 20 72 6f 75 74 69 6e 65 20 6e 65 76  ca() routine nev
6e790 65 72 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e  er returns NULL.
6e7a0 20 20 54 68 69 73 20 77 69 6c 6c 20 63 61 75 73    This will caus
6e7b0 65 20 63 6f 64 65 20 70 61 74 68 73 0a 2a 2a 20  e code paths.** 
6e7c0 74 68 61 74 20 64 65 61 6c 20 77 69 74 68 20 73  that deal with s
6e7d0 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63  qlite3StackAlloc
6e7e0 28 29 20 66 61 69 6c 75 72 65 73 20 74 6f 20 62  () failures to b
6e7f0 65 20 75 6e 72 65 61 63 68 61 62 6c 65 2e 0a 2a  e unreachable..*
6e800 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
6e810 55 53 45 5f 41 4c 4c 4f 43 41 0a 23 20 64 65 66  USE_ALLOCA.# def
6e820 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b  ine sqlite3Stack
6e830 41 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 20 20 20  AllocRaw(D,N)   
6e840 61 6c 6c 6f 63 61 28 4e 29 0a 23 20 64 65 66 69  alloca(N).# defi
6e850 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41  ne sqlite3StackA
6e860 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29 20 20 6d  llocZero(D,N)  m
6e870 65 6d 73 65 74 28 61 6c 6c 6f 63 61 28 4e 29 2c  emset(alloca(N),
6e880 20 30 2c 20 4e 29 0a 23 20 64 65 66 69 6e 65 20   0, N).# define 
6e890 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65  sqlite3StackFree
6e8a0 28 44 2c 50 29 20 20 20 20 20 20 20 0a 23 65 6c  (D,P)       .#el
6e8b0 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  se.# define sqli
6e8c0 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77  te3StackAllocRaw
6e8d0 28 44 2c 4e 29 20 20 20 73 71 6c 69 74 65 33 44  (D,N)   sqlite3D
6e8e0 62 4d 61 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 0a  bMallocRaw(D,N).
6e8f0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
6e900 53 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f 28 44  StackAllocZero(D
6e910 2c 4e 29 20 20 73 71 6c 69 74 65 33 44 62 4d 61  ,N)  sqlite3DbMa
6e920 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29 0a 23 20  llocZero(D,N).# 
6e930 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74  define sqlite3St
6e940 61 63 6b 46 72 65 65 28 44 2c 50 29 20 20 20 20  ackFree(D,P)    
6e950 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
6e960 28 44 2c 50 29 0a 23 65 6e 64 69 66 0a 0a 23 69  (D,P).#endif..#i
6e970 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
6e980 4c 45 5f 4d 45 4d 53 59 53 33 0a 53 51 4c 49 54  LE_MEMSYS3.SQLIT
6e990 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
6e9a0 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
6e9b0 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47  ods *sqlite3MemG
6e9c0 65 74 4d 65 6d 73 79 73 33 28 76 6f 69 64 29 3b  etMemsys3(void);
6e9d0 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
6e9e0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
6e9f0 53 59 53 35 0a 53 51 4c 49 54 45 5f 50 52 49 56  SYS5.SQLITE_PRIV
6ea00 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ATE const sqlite
6ea10 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73  3_mem_methods *s
6ea20 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73  qlite3MemGetMems
6ea30 79 73 35 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69  ys5(void);.#endi
6ea40 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  f...#ifndef SQLI
6ea50 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 53 51  TE_MUTEX_OMIT.SQ
6ea60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73  LITE_PRIVATE   s
6ea70 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
6ea80 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66  hods *sqlite3Def
6ea90 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 3b  aultMutex(void);
6eaa0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6eab0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
6eac0 2a 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c  *sqlite3MutexAll
6ead0 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  oc(int);.SQLITE_
6eae0 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
6eaf0 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 76  lite3MutexInit(v
6eb00 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  oid);.SQLITE_PRI
6eb10 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
6eb20 65 33 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29  e3MutexEnd(void)
6eb30 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45  ;.#endif..SQLITE
6eb40 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6eb50 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 28  ite3StatusValue(
6eb60 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6eb70 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6eb80 33 53 74 61 74 75 73 41 64 64 28 69 6e 74 2c 20  3StatusAdd(int, 
6eb90 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6eba0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6ebb0 33 53 74 61 74 75 73 53 65 74 28 69 6e 74 2c 20  3StatusSet(int, 
6ebc0 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  int);..SQLITE_PR
6ebd0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6ebe0 33 49 73 4e 61 4e 28 64 6f 75 62 6c 65 29 3b 0a  3IsNaN(double);.
6ebf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6ec00 76 6f 69 64 20 73 71 6c 69 74 65 33 56 58 50 72  void sqlite3VXPr
6ec10 69 6e 74 66 28 53 74 72 41 63 63 75 6d 2a 2c 20  intf(StrAccum*, 
6ec20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  int, const char*
6ec30 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49  , va_list);.SQLI
6ec40 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20  TE_PRIVATE char 
6ec50 2a 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28  *sqlite3MPrintf(
6ec60 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
6ec70 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49  har*, ...);.SQLI
6ec80 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20  TE_PRIVATE char 
6ec90 2a 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66  *sqlite3VMPrintf
6eca0 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
6ecb0 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b  char*, va_list);
6ecc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6ecd0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4d 41 70  char *sqlite3MAp
6ece0 70 65 6e 64 66 28 73 71 6c 69 74 65 33 2a 2c 63  pendf(sqlite3*,c
6ecf0 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
6ed00 2c 2e 2e 2e 29 3b 0a 23 69 66 20 64 65 66 69 6e  ,...);.#if defin
6ed10 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20  ed(SQLITE_TEST) 
6ed20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
6ed30 45 5f 44 45 42 55 47 29 0a 53 51 4c 49 54 45 5f  E_DEBUG).SQLITE_
6ed40 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
6ed50 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
6ed60 66 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e  f(const char*, .
6ed70 2e 2e 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20  ..);.#endif.#if 
6ed80 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54  defined(SQLITE_T
6ed90 45 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 49 56  EST).SQLITE_PRIV
6eda0 41 54 45 20 20 20 76 6f 69 64 20 2a 73 71 6c 69  ATE   void *sqli
6edb0 74 65 33 54 65 73 74 54 65 78 74 54 6f 50 74 72  te3TestTextToPtr
6edc0 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23  (const char*);.#
6edd0 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49  endif.SQLITE_PRI
6ede0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6edf0 33 53 65 74 53 74 72 69 6e 67 28 63 68 61 72 20  3SetString(char 
6ee00 2a 2a 2c 20 73 71 6c 69 74 65 33 2a 2c 20 63 6f  **, sqlite3*, co
6ee10 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b  nst char*, ...);
6ee20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6ee30 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f  void sqlite3Erro
6ee40 72 4d 73 67 28 50 61 72 73 65 2a 2c 20 63 6f 6e  rMsg(Parse*, con
6ee50 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a  st char*, ...);.
6ee60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6ee70 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72  oid sqlite3Error
6ee80 43 6c 65 61 72 28 50 61 72 73 65 2a 29 3b 0a 53  Clear(Parse*);.S
6ee90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6eea0 74 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65  t sqlite3Dequote
6eeb0 28 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  (char*);.SQLITE_
6eec0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6eed0 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65 28 63  te3KeywordCode(c
6eee0 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
6eef0 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  ar*, int);.SQLIT
6ef00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6ef10 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72 28 50  lite3RunParser(P
6ef20 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61  arse*, const cha
6ef30 72 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51  r*, char **);.SQ
6ef40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6ef50 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43  d sqlite3FinishC
6ef60 6f 64 69 6e 67 28 50 61 72 73 65 2a 29 3b 0a 53  oding(Parse*);.S
6ef70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6ef80 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  t sqlite3GetTemp
6ef90 52 65 67 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c  Reg(Parse*);.SQL
6efa0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6efb0 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
6efc0 65 6d 70 52 65 67 28 50 61 72 73 65 2a 2c 69 6e  empReg(Parse*,in
6efd0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6efe0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65  TE int sqlite3Ge
6eff0 74 54 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65  tTempRange(Parse
6f000 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  *,int);.SQLITE_P
6f010 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6f020 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61  te3ReleaseTempRa
6f030 6e 67 65 28 50 61 72 73 65 2a 2c 69 6e 74 2c 69  nge(Parse*,int,i
6f040 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6f050 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
6f060 33 45 78 70 72 41 6c 6c 6f 63 28 73 71 6c 69 74  3ExprAlloc(sqlit
6f070 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 54 6f  e3*,int,const To
6f080 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ken*,int);.SQLIT
6f090 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
6f0a0 73 71 6c 69 74 65 33 45 78 70 72 28 73 71 6c 69  sqlite3Expr(sqli
6f0b0 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63  te3*,int,const c
6f0c0 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
6f0d0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6f0e0 65 33 45 78 70 72 41 74 74 61 63 68 53 75 62 74  e3ExprAttachSubt
6f0f0 72 65 65 73 28 73 71 6c 69 74 65 33 2a 2c 45 78  rees(sqlite3*,Ex
6f100 70 72 2a 2c 45 78 70 72 2a 2c 45 78 70 72 2a 29  pr*,Expr*,Expr*)
6f110 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6f120 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 50 45   Expr *sqlite3PE
6f130 78 70 72 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c  xpr(Parse*, int,
6f140 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 2c 20 63   Expr*, Expr*, c
6f150 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51  onst Token*);.SQ
6f160 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
6f170 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6e  r *sqlite3ExprAn
6f180 64 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a  d(sqlite3*,Expr*
6f190 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
6f1a0 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73  _PRIVATE Expr *s
6f1b0 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74 69  qlite3ExprFuncti
6f1c0 6f 6e 28 50 61 72 73 65 2a 2c 45 78 70 72 4c 69  on(Parse*,ExprLi
6f1d0 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51  st*, Token*);.SQ
6f1e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6f1f0 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 73 73  d sqlite3ExprAss
6f200 69 67 6e 56 61 72 4e 75 6d 62 65 72 28 50 61 72  ignVarNumber(Par
6f210 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c  se*, Expr*);.SQL
6f220 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6f230 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61   sqlite3ExprClea
6f240 72 28 73 71 6c 69 74 65 33 2a 2c 20 45 78 70 72  r(sqlite3*, Expr
6f250 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6f260 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
6f270 78 70 72 44 65 6c 65 74 65 28 73 71 6c 69 74 65  xprDelete(sqlite
6f280 33 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49  3*, Expr*);.SQLI
6f290 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c  TE_PRIVATE ExprL
6f2a0 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72  ist *sqlite3Expr
6f2b0 4c 69 73 74 41 70 70 65 6e 64 28 50 61 72 73 65  ListAppend(Parse
6f2c0 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 45 78 70 72  *,ExprList*,Expr
6f2d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6f2e0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
6f2f0 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 50  xprListSetName(P
6f300 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c  arse*,ExprList*,
6f310 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c  Token*,int);.SQL
6f320 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6f330 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
6f340 53 65 74 53 70 61 6e 28 50 61 72 73 65 2a 2c 45  SetSpan(Parse*,E
6f350 78 70 72 4c 69 73 74 2a 2c 45 78 70 72 53 70 61  xprList*,ExprSpa
6f360 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
6f370 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6f380 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 73  ExprListDelete(s
6f390 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 4c 69 73  qlite3*, ExprLis
6f3a0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
6f3b0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
6f3c0 6e 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 63 68  nit(sqlite3*, ch
6f3d0 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ar**);.SQLITE_PR
6f3e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6f3f0 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f  3InitCallback(vo
6f400 69 64 2a 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2a  id*, int, char**
6f410 2c 20 63 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54  , char**);.SQLIT
6f420 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6f430 71 6c 69 74 65 33 50 72 61 67 6d 61 28 50 61 72  qlite3Pragma(Par
6f440 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e  se*,Token*,Token
6f450 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53  *,Token*,int);.S
6f460 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6f470 69 64 20 73 71 6c 69 74 65 33 52 65 73 65 74 49  id sqlite3ResetI
6f480 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71  nternalSchema(sq
6f490 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51  lite3*, int);.SQ
6f4a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6f4b0 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61  d sqlite3BeginPa
6f4c0 72 73 65 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b  rse(Parse*,int);
6f4d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f4e0 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d  void sqlite3Comm
6f4f0 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65  itInternalChange
6f500 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c  s(sqlite3*);.SQL
6f510 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c  ITE_PRIVATE Tabl
6f520 65 20 2a 73 71 6c 69 74 65 33 52 65 73 75 6c 74  e *sqlite3Result
6f530 53 65 74 4f 66 53 65 6c 65 63 74 28 50 61 72 73  SetOfSelect(Pars
6f540 65 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c  e*,Select*);.SQL
6f550 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6f560 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74   sqlite3OpenMast
6f570 65 72 54 61 62 6c 65 28 50 61 72 73 65 20 2a 2c  erTable(Parse *,
6f580 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
6f590 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6f5a0 65 33 53 74 61 72 74 54 61 62 6c 65 28 50 61 72  e3StartTable(Par
6f5b0 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e  se*,Token*,Token
6f5c0 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e  *,int,int,int,in
6f5d0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6f5e0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
6f5f0 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c  ddColumn(Parse*,
6f600 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
6f610 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6f620 69 74 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50  ite3AddNotNull(P
6f630 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  arse*, int);.SQL
6f640 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6f650 20 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61   sqlite3AddPrima
6f660 72 79 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78  ryKey(Parse*, Ex
6f670 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e  prList*, int, in
6f680 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
6f690 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6f6a0 69 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73  ite3AddCheckCons
6f6b0 74 72 61 69 6e 74 28 50 61 72 73 65 2a 2c 20 45  traint(Parse*, E
6f6c0 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
6f6d0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6f6e0 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28  e3AddColumnType(
6f6f0 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a  Parse*,Token*);.
6f700 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6f710 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 44 65  oid sqlite3AddDe
6f720 66 61 75 6c 74 56 61 6c 75 65 28 50 61 72 73 65  faultValue(Parse
6f730 2a 2c 45 78 70 72 53 70 61 6e 2a 29 3b 0a 53 51  *,ExprSpan*);.SQ
6f740 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6f750 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c  d sqlite3AddColl
6f760 61 74 65 54 79 70 65 28 50 61 72 73 65 2a 2c 20  ateType(Parse*, 
6f770 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
6f780 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6f790 69 74 65 33 45 6e 64 54 61 62 6c 65 28 50 61 72  ite3EndTable(Par
6f7a0 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e  se*,Token*,Token
6f7b0 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 0a 53 51 4c  *,Select*);..SQL
6f7c0 49 54 45 5f 50 52 49 56 41 54 45 20 42 69 74 76  ITE_PRIVATE Bitv
6f7d0 65 63 20 2a 73 71 6c 69 74 65 33 42 69 74 76 65  ec *sqlite3Bitve
6f7e0 63 43 72 65 61 74 65 28 75 33 32 29 3b 0a 53 51  cCreate(u32);.SQ
6f7f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6f800 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65   sqlite3BitvecTe
6f810 73 74 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29  st(Bitvec*, u32)
6f820 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6f830 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76   int sqlite3Bitv
6f840 65 63 53 65 74 28 42 69 74 76 65 63 2a 2c 20 75  ecSet(Bitvec*, u
6f850 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  32);.SQLITE_PRIV
6f860 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6f870 42 69 74 76 65 63 43 6c 65 61 72 28 42 69 74 76  BitvecClear(Bitv
6f880 65 63 2a 2c 20 75 33 32 2c 20 76 6f 69 64 2a 29  ec*, u32, void*)
6f890 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6f8a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74   void sqlite3Bit
6f8b0 76 65 63 44 65 73 74 72 6f 79 28 42 69 74 76 65  vecDestroy(Bitve
6f8c0 63 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  c*);.SQLITE_PRIV
6f8d0 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 42  ATE u32 sqlite3B
6f8e0 69 74 76 65 63 53 69 7a 65 28 42 69 74 76 65 63  itvecSize(Bitvec
6f8f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6f900 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69  TE int sqlite3Bi
6f910 74 76 65 63 42 75 69 6c 74 69 6e 54 65 73 74 28  tvecBuiltinTest(
6f920 69 6e 74 2c 69 6e 74 2a 29 3b 0a 0a 53 51 4c 49  int,int*);..SQLI
6f930 54 45 5f 50 52 49 56 41 54 45 20 52 6f 77 53 65  TE_PRIVATE RowSe
6f940 74 20 2a 73 71 6c 69 74 65 33 52 6f 77 53 65 74  t *sqlite3RowSet
6f950 49 6e 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 76  Init(sqlite3*, v
6f960 6f 69 64 2a 2c 20 75 6e 73 69 67 6e 65 64 20 69  oid*, unsigned i
6f970 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6f980 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6f990 52 6f 77 53 65 74 43 6c 65 61 72 28 52 6f 77 53  RowSetClear(RowS
6f9a0 65 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  et*);.SQLITE_PRI
6f9b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6f9c0 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 52 6f  3RowSetInsert(Ro
6f9d0 77 53 65 74 2a 2c 20 69 36 34 29 3b 0a 53 51 4c  wSet*, i64);.SQL
6f9e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6f9f0 73 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65 73  sqlite3RowSetTes
6fa00 74 28 52 6f 77 53 65 74 2a 2c 20 75 38 20 69 42  t(RowSet*, u8 iB
6fa10 61 74 63 68 2c 20 69 36 34 29 3b 0a 53 51 4c 49  atch, i64);.SQLI
6fa20 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6fa30 71 6c 69 74 65 33 52 6f 77 53 65 74 4e 65 78 74  qlite3RowSetNext
6fa40 28 52 6f 77 53 65 74 2a 2c 20 69 36 34 2a 29 3b  (RowSet*, i64*);
6fa50 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
6fa60 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65   void sqlite3Cre
6fa70 61 74 65 56 69 65 77 28 50 61 72 73 65 2a 2c 54  ateView(Parse*,T
6fa80 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b  oken*,Token*,Tok
6fa90 65 6e 2a 2c 53 65 6c 65 63 74 2a 2c 69 6e 74 2c  en*,Select*,int,
6faa0 69 6e 74 29 3b 0a 0a 23 69 66 20 21 64 65 66 69  int);..#if !defi
6fab0 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
6fac0 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65  VIEW) || !define
6fad0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  d(SQLITE_OMIT_VI
6fae0 52 54 55 41 4c 54 41 42 4c 45 29 0a 53 51 4c 49  RTUALTABLE).SQLI
6faf0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
6fb00 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43   sqlite3ViewGetC
6fb10 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50 61 72 73 65  olumnNames(Parse
6fb20 2a 2c 54 61 62 6c 65 2a 29 3b 0a 23 65 6c 73 65  *,Table*);.#else
6fb30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6fb40 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61  3ViewGetColumnNa
6fb50 6d 65 73 28 41 2c 42 29 20 30 0a 23 65 6e 64 69  mes(A,B) 0.#endi
6fb60 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  f..SQLITE_PRIVAT
6fb70 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72  E void sqlite3Dr
6fb80 6f 70 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 20  opTable(Parse*, 
6fb90 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69  SrcList*, int, i
6fba0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6fbb0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6fbc0 44 65 6c 65 74 65 54 61 62 6c 65 28 54 61 62 6c  DeleteTable(Tabl
6fbd0 65 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  e*);.#ifndef SQL
6fbe0 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
6fbf0 52 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52  REMENT.SQLITE_PR
6fc00 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
6fc10 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e  ite3Autoincremen
6fc20 74 42 65 67 69 6e 28 50 61 72 73 65 20 2a 70 50  tBegin(Parse *pP
6fc30 61 72 73 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  arse);.SQLITE_PR
6fc40 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
6fc50 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e  ite3Autoincremen
6fc60 74 45 6e 64 28 50 61 72 73 65 20 2a 70 50 61 72  tEnd(Parse *pPar
6fc70 73 65 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  se);.#else.# def
6fc80 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 6f 69  ine sqlite3Autoi
6fc90 6e 63 72 65 6d 65 6e 74 42 65 67 69 6e 28 58 29  ncrementBegin(X)
6fca0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6fcb0 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e  3AutoincrementEn
6fcc0 64 28 58 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49  d(X).#endif.SQLI
6fcd0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6fce0 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 50 61  sqlite3Insert(Pa
6fcf0 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20  rse*, SrcList*, 
6fd00 45 78 70 72 4c 69 73 74 2a 2c 20 53 65 6c 65 63  ExprList*, Selec
6fd10 74 2a 2c 20 49 64 4c 69 73 74 2a 2c 20 69 6e 74  t*, IdList*, int
6fd20 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6fd30 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 41  E void *sqlite3A
6fd40 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 73 71 6c  rrayAllocate(sql
6fd50 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c  ite3*,void*,int,
6fd60 69 6e 74 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e  int,int*,int*,in
6fd70 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
6fd80 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69  ATE IdList *sqli
6fd90 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28  te3IdListAppend(
6fda0 73 71 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 74  sqlite3*, IdList
6fdb0 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  *, Token*);.SQLI
6fdc0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6fdd0 71 6c 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65  qlite3IdListInde
6fde0 78 28 49 64 4c 69 73 74 2a 2c 63 6f 6e 73 74 20  x(IdList*,const 
6fdf0 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  char*);.SQLITE_P
6fe00 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a  RIVATE SrcList *
6fe10 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 45 6e  sqlite3SrcListEn
6fe20 6c 61 72 67 65 28 73 71 6c 69 74 65 33 2a 2c 20  large(sqlite3*, 
6fe30 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69  SrcList*, int, i
6fe40 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6fe50 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c  ATE SrcList *sql
6fe60 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
6fe70 64 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c  d(sqlite3*, SrcL
6fe80 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f  ist*, Token*, To
6fe90 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
6fea0 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73  IVATE SrcList *s
6feb0 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
6fec0 65 6e 64 46 72 6f 6d 54 65 72 6d 28 50 61 72 73  endFromTerm(Pars
6fed0 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f  e*, SrcList*, To
6fee0 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 0a 20 20  ken*, Token*,.  
6fef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ff00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ff10 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 53 65 6c 65      Token*, Sele
6ff20 63 74 2a 2c 20 45 78 70 72 2a 2c 20 49 64 4c 69  ct*, Expr*, IdLi
6ff30 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
6ff40 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6ff50 33 53 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42  3SrcListIndexedB
6ff60 79 28 50 61 72 73 65 20 2a 2c 20 53 72 63 4c 69  y(Parse *, SrcLi
6ff70 73 74 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a  st *, Token *);.
6ff80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6ff90 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78 65  nt sqlite3Indexe
6ffa0 64 42 79 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20  dByLookup(Parse 
6ffb0 2a 2c 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  *, struct SrcLis
6ffc0 74 5f 69 74 65 6d 20 2a 29 3b 0a 53 51 4c 49 54  t_item *);.SQLIT
6ffd0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6ffe0 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68 69  qlite3SrcListShi
6fff0 66 74 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c 69  ftJoinType(SrcLi
70000 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
70010 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
70020 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75  3SrcListAssignCu
70030 72 73 6f 72 73 28 50 61 72 73 65 2a 2c 20 53 72  rsors(Parse*, Sr
70040 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  cList*);.SQLITE_
70050 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
70060 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65  ite3IdListDelete
70070 28 73 71 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73  (sqlite3*, IdLis
70080 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
70090 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
700a0 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 73 71  SrcListDelete(sq
700b0 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a  lite3*, SrcList*
700c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
700d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72  E void sqlite3Cr
700e0 65 61 74 65 49 6e 64 65 78 28 50 61 72 73 65 2a  eateIndex(Parse*
700f0 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53  ,Token*,Token*,S
70100 72 63 4c 69 73 74 2a 2c 45 78 70 72 4c 69 73 74  rcList*,ExprList
70110 2a 2c 69 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 20  *,int,Token*,.  
70120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70130 20 20 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e        Token*, in
70140 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
70150 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
70160 69 74 65 33 44 72 6f 70 49 6e 64 65 78 28 50 61  ite3DropIndex(Pa
70170 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20  rse*, SrcList*, 
70180 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
70190 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
701a0 53 65 6c 65 63 74 28 50 61 72 73 65 2a 2c 20 53  Select(Parse*, S
701b0 65 6c 65 63 74 2a 2c 20 53 65 6c 65 63 74 44 65  elect*, SelectDe
701c0 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
701d0 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c  VATE Select *sql
701e0 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 50 61  ite3SelectNew(Pa
701f0 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 53  rse*,ExprList*,S
70200 72 63 4c 69 73 74 2a 2c 45 78 70 72 2a 2c 45 78  rcList*,Expr*,Ex
70210 70 72 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20  prList*,.       
70220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70230 20 20 45 78 70 72 2a 2c 45 78 70 72 4c 69 73 74    Expr*,ExprList
70240 2a 2c 69 6e 74 2c 45 78 70 72 2a 2c 45 78 70 72  *,int,Expr*,Expr
70250 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
70260 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
70270 65 6c 65 63 74 44 65 6c 65 74 65 28 73 71 6c 69  electDelete(sqli
70280 74 65 33 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a  te3*, Select*);.
70290 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54  SQLITE_PRIVATE T
702a0 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 53 72 63  able *sqlite3Src
702b0 4c 69 73 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65  ListLookup(Parse
702c0 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51  *, SrcList*);.SQ
702d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
702e0 20 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e   sqlite3IsReadOn
702f0 6c 79 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65  ly(Parse*, Table
70300 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
70310 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
70320 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 50 61  ite3OpenTable(Pa
70330 72 73 65 2a 2c 20 69 6e 74 20 69 43 75 72 2c 20  rse*, int iCur, 
70340 69 6e 74 20 69 44 62 2c 20 54 61 62 6c 65 2a 2c  int iDb, Table*,
70350 20 69 6e 74 29 3b 0a 23 69 66 20 64 65 66 69 6e   int);.#if defin
70360 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
70370 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c  _UPDATE_DELETE_L
70380 49 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65  IMIT) && !define
70390 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  d(SQLITE_OMIT_SU
703a0 42 51 55 45 52 59 29 0a 53 51 4c 49 54 45 5f 50  BQUERY).SQLITE_P
703b0 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
703c0 69 74 65 33 4c 69 6d 69 74 57 68 65 72 65 28 50  ite3LimitWhere(P
703d0 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20  arse *, SrcList 
703e0 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72 4c  *, Expr *, ExprL
703f0 69 73 74 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45  ist *, Expr *, E
70400 78 70 72 20 2a 2c 20 63 68 61 72 20 2a 29 3b 0a  xpr *, char *);.
70410 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52  #endif.SQLITE_PR
70420 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
70430 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 50 61 72  e3DeleteFrom(Par
70440 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45  se*, SrcList*, E
70450 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
70460 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
70470 65 33 55 70 64 61 74 65 28 50 61 72 73 65 2a 2c  e3Update(Parse*,
70480 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c   SrcList*, ExprL
70490 69 73 74 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74  ist*, Expr*, int
704a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
704b0 45 20 57 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c  E WhereInfo *sql
704c0 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 50  ite3WhereBegin(P
704d0 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c  arse*, SrcList*,
704e0 20 45 78 70 72 2a 2c 20 45 78 70 72 4c 69 73 74   Expr*, ExprList
704f0 2a 2a 2c 20 75 31 36 29 3b 0a 53 51 4c 49 54 45  **, u16);.SQLITE
70500 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
70510 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 57 68  lite3WhereEnd(Wh
70520 65 72 65 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54  ereInfo*);.SQLIT
70530 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
70540 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74  lite3ExprCodeGet
70550 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c 20 54  Column(Parse*, T
70560 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c  able*, int, int,
70570 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
70580 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
70590 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d  sqlite3ExprCodeM
705a0 6f 76 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c  ove(Parse*, int,
705b0 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
705c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
705d0 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43  sqlite3ExprCodeC
705e0 6f 70 79 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c  opy(Parse*, int,
705f0 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
70600 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
70610 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
70620 53 74 6f 72 65 28 50 61 72 73 65 2a 2c 20 69 6e  Store(Parse*, in
70630 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  t, int, int);.SQ
70640 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
70650 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63  d sqlite3ExprCac
70660 68 65 50 75 73 68 28 50 61 72 73 65 2a 29 3b 0a  hePush(Parse*);.
70670 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
70680 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
70690 61 63 68 65 50 6f 70 28 50 61 72 73 65 2a 2c 20  achePop(Parse*, 
706a0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
706b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
706c0 33 45 78 70 72 43 61 63 68 65 52 65 6d 6f 76 65  3ExprCacheRemove
706d0 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53  (Parse*, int);.S
706e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
706f0 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61  id sqlite3ExprCa
70700 63 68 65 43 6c 65 61 72 28 50 61 72 73 65 2a 29  cheClear(Parse*)
70710 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70720 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
70730 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68  rCacheAffinityCh
70740 61 6e 67 65 28 50 61 72 73 65 2a 2c 20 69 6e 74  ange(Parse*, int
70750 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
70760 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
70770 74 65 33 45 78 70 72 48 61 72 64 43 6f 70 79 28  te3ExprHardCopy(
70780 50 61 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b  Parse*,int,int);
70790 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
707a0 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43  int sqlite3ExprC
707b0 6f 64 65 28 50 61 72 73 65 2a 2c 20 45 78 70 72  ode(Parse*, Expr
707c0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
707d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
707e0 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28  te3ExprCodeTemp(
707f0 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69  Parse*, Expr*, i
70800 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  nt*);.SQLITE_PRI
70810 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
70820 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 50  ExprCodeTarget(P
70830 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e  arse*, Expr*, in
70840 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
70850 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78  TE int sqlite3Ex
70860 70 72 43 6f 64 65 41 6e 64 43 61 63 68 65 28 50  prCodeAndCache(P
70870 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e  arse*, Expr*, in
70880 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
70890 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
708a0 78 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73  xprCodeConstants
708b0 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b  (Parse*, Expr*);
708c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
708d0 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43  int sqlite3ExprC
708e0 6f 64 65 45 78 70 72 4c 69 73 74 28 50 61 72 73  odeExprList(Pars
708f0 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69  e*, ExprList*, i
70900 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
70910 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
70920 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28  lite3ExprIfTrue(
70930 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69  Parse*, Expr*, i
70940 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
70950 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
70960 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65  lite3ExprIfFalse
70970 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20  (Parse*, Expr*, 
70980 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  int, int);.SQLIT
70990 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20  E_PRIVATE Table 
709a0 2a 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c  *sqlite3FindTabl
709b0 65 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  e(sqlite3*,const
709c0 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68   char*, const ch
709d0 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ar*);.SQLITE_PRI
709e0 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69  VATE Table *sqli
709f0 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 50  te3LocateTable(P
70a00 61 72 73 65 2a 2c 69 6e 74 20 69 73 56 69 65 77  arse*,int isView
70a10 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f  ,const char*, co
70a20 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
70a30 54 45 5f 50 52 49 56 41 54 45 20 49 6e 64 65 78  TE_PRIVATE Index
70a40 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64   *sqlite3FindInd
70a50 65 78 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  ex(sqlite3*,cons
70a60 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63  t char*, const c
70a70 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
70a80 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
70a90 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74  e3UnlinkAndDelet
70aa0 65 54 61 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c  eTable(sqlite3*,
70ab0 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  int,const char*)
70ac0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70ad0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c   void sqlite3Unl
70ae0 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65  inkAndDeleteInde
70af0 78 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63  x(sqlite3*,int,c
70b00 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c  onst char*);.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 56 61 63 75 75 6d 28 50   sqlite3Vacuum(P
70b30 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  arse*);.SQLITE_P
70b40 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
70b50 65 33 52 75 6e 56 61 63 75 75 6d 28 63 68 61 72  e3RunVacuum(char
70b60 2a 2a 2c 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53  **, sqlite3*);.S
70b70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
70b80 61 72 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46  ar *sqlite3NameF
70b90 72 6f 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33  romToken(sqlite3
70ba0 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  *, Token*);.SQLI
70bb0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
70bc0 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72  qlite3ExprCompar
70bd0 65 28 45 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b  e(Expr*, Expr*);
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 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65  AnalyzeAggregate
70c10 73 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20  s(NameContext*, 
70c20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
70c30 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
70c40 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67  te3ExprAnalyzeAg
70c50 67 4c 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78  gList(NameContex
70c60 74 2a 2c 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53  t*,ExprList*);.S
70c70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64  QLITE_PRIVATE Vd
70c80 62 65 20 2a 73 71 6c 69 74 65 33 47 65 74 56 64  be *sqlite3GetVd
70c90 62 65 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49  be(Parse*);.SQLI
70ca0 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
70cb0 2a 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 64  *sqlite3CreateId
70cc0 45 78 70 72 28 50 61 72 73 65 20 2a 2c 20 63 6f  Expr(Parse *, co
70cd0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
70ce0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
70cf0 73 71 6c 69 74 65 33 50 72 6e 67 53 61 76 65 53  sqlite3PrngSaveS
70d00 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49  tate(void);.SQLI
70d10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
70d20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f  sqlite3PrngResto
70d30 72 65 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53  reState(void);.S
70d40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
70d50 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65  id sqlite3PrngRe
70d60 73 65 74 53 74 61 74 65 28 76 6f 69 64 29 3b 0a  setState(void);.
70d70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
70d80 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62  oid sqlite3Rollb
70d90 61 63 6b 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29  ackAll(sqlite3*)
70da0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70db0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64   void sqlite3Cod
70dc0 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 50 61  eVerifySchema(Pa
70dd0 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  rse*, int);.SQLI
70de0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
70df0 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e  sqlite3BeginTran
70e00 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20  saction(Parse*, 
70e10 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
70e20 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
70e30 33 43 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74 69  3CommitTransacti
70e40 6f 6e 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49  on(Parse*);.SQLI
70e50 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
70e60 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54  sqlite3RollbackT
70e70 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65  ransaction(Parse
70e80 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
70e90 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
70ea0 61 76 65 70 6f 69 6e 74 28 50 61 72 73 65 2a 2c  avepoint(Parse*,
70eb0 20 69 6e 74 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53   int, Token*);.S
70ec0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
70ed0 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53  id sqlite3CloseS
70ee0 61 76 65 70 6f 69 6e 74 73 28 73 71 6c 69 74 65  avepoints(sqlite
70ef0 33 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  3 *);.SQLITE_PRI
70f00 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
70f10 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 45  ExprIsConstant(E
70f20 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
70f30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
70f40 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e  3ExprIsConstantN
70f50 6f 74 4a 6f 69 6e 28 45 78 70 72 2a 29 3b 0a 53  otJoin(Expr*);.S
70f60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
70f70 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43  t sqlite3ExprIsC
70f80 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f  onstantOrFunctio
70f90 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  n(Expr*);.SQLITE
70fa0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
70fb0 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65  ite3ExprIsIntege
70fc0 72 28 45 78 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a  r(Expr*, int*);.
70fd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
70fe0 6e 74 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69  nt sqlite3IsRowi
70ff0 64 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  d(const char*);.
71000 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
71010 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72  oid sqlite3Gener
71020 61 74 65 52 6f 77 44 65 6c 65 74 65 28 50 61 72  ateRowDelete(Par
71030 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74  se*, Table*, int
71040 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 54 72 69 67  , int, int, Trig
71050 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  ger *, int);.SQL
71060 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
71070 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
71080 52 6f 77 49 6e 64 65 78 44 65 6c 65 74 65 28 50  RowIndexDelete(P
71090 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69  arse*, Table*, i
710a0 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54  nt, int*);.SQLIT
710b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
710c0 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64  lite3GenerateInd
710d0 65 78 4b 65 79 28 50 61 72 73 65 2a 2c 20 49 6e  exKey(Parse*, In
710e0 64 65 78 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20  dex*, int, int, 
710f0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
71100 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
71110 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61  3GenerateConstra
71120 69 6e 74 43 68 65 63 6b 73 28 50 61 72 73 65 2a  intChecks(Parse*
71130 2c 54 61 62 6c 65 2a 2c 69 6e 74 2c 69 6e 74 2c  ,Table*,int,int,
71140 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
71150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71160 20 20 20 20 20 20 69 6e 74 2a 2c 69 6e 74 2c 69        int*,int,i
71170 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2a 29  nt,int,int,int*)
71180 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
71190 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d   void sqlite3Com
711a0 70 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 50  pleteInsertion(P
711b0 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69  arse*, Table*, i
711c0 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2a 2c 20 69  nt, int, int*, i
711d0 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53  nt, int, int);.S
711e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
711f0 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62  t sqlite3OpenTab
71200 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 50 61 72  leAndIndices(Par
71210 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74  se*, Table*, int
71220 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
71230 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
71240 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
71250 72 61 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69  ration(Parse*, i
71260 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
71270 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
71280 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28 50 61  lite3MayAbort(Pa
71290 72 73 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  rse *);.SQLITE_P
712a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
712b0 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e  te3HaltConstrain
712c0 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 63  t(Parse*, int, c
712d0 68 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  har*, int);.SQLI
712e0 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
712f0 2a 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28  *sqlite3ExprDup(
71300 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 69  sqlite3*,Expr*,i
71310 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
71320 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73 71  ATE ExprList *sq
71330 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70  lite3ExprListDup
71340 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 4c 69  (sqlite3*,ExprLi
71350 73 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  st*,int);.SQLITE
71360 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 74  _PRIVATE SrcList
71370 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74   *sqlite3SrcList
71380 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 53 72 63  Dup(sqlite3*,Src
71390 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  List*,int);.SQLI
713a0 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73  TE_PRIVATE IdLis
713b0 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74  t *sqlite3IdList
713c0 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 49 64 4c  Dup(sqlite3*,IdL
713d0 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ist*);.SQLITE_PR
713e0 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71  IVATE Select *sq
713f0 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 73  lite3SelectDup(s
71400 71 6c 69 74 65 33 2a 2c 53 65 6c 65 63 74 2a 2c  qlite3*,Select*,
71410 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
71420 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
71430 33 46 75 6e 63 44 65 66 49 6e 73 65 72 74 28 46  3FuncDefInsert(F
71440 75 6e 63 44 65 66 48 61 73 68 2a 2c 20 46 75 6e  uncDefHash*, Fun
71450 63 44 65 66 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  cDef*);.SQLITE_P
71460 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a  RIVATE FuncDef *
71470 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74  sqlite3FindFunct
71480 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  ion(sqlite3*,con
71490 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 69 6e 74  st char*,int,int
714a0 2c 75 38 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  ,u8,int);.SQLITE
714b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
714c0 6c 69 74 65 33 52 65 67 69 73 74 65 72 42 75 69  lite3RegisterBui
714d0 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73 28 73 71  ltinFunctions(sq
714e0 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f  lite3*);.SQLITE_
714f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
71500 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74 65  ite3RegisterDate
71510 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 76 6f  TimeFunctions(vo
71520 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  id);.SQLITE_PRIV
71530 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
71540 52 65 67 69 73 74 65 72 47 6c 6f 62 61 6c 46 75  RegisterGlobalFu
71550 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 3b 0a 23  nctions(void);.#
71560 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
71570 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UG.SQLITE_PRIVAT
71580 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53  E   int sqlite3S
71590 61 66 65 74 79 4f 6e 28 73 71 6c 69 74 65 33 2a  afetyOn(sqlite3*
715a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
715b0 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53  E   int sqlite3S
715c0 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74 65 33  afetyOff(sqlite3
715d0 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  *);.#else.# defi
715e0 6e 65 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  ne sqlite3Safety
715f0 4f 6e 28 41 29 20 30 0a 23 20 64 65 66 69 6e 65  On(A) 0.# define
71600 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
71610 66 28 41 29 20 30 0a 23 65 6e 64 69 66 0a 53 51  f(A) 0.#endif.SQ
71620 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
71630 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68   sqlite3SafetyCh
71640 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b  eckOk(sqlite3*);
71650 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71660 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74  int sqlite3Safet
71670 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73  yCheckSickOrOk(s
71680 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45  qlite3*);.SQLITE
71690 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
716a0 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69  lite3ChangeCooki
716b0 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a  e(Parse*, int);.
716c0 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
716d0 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20  LITE_OMIT_VIEW) 
716e0 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
716f0 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29  TE_OMIT_TRIGGER)
71700 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71710 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 74 65  void sqlite3Mate
71720 72 69 61 6c 69 7a 65 56 69 65 77 28 50 61 72 73  rializeView(Pars
71730 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78 70 72  e*, Table*, Expr
71740 2a 2c 20 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a  *, int);.#endif.
71750 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
71760 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 53 51 4c  OMIT_TRIGGER.SQL
71770 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
71780 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54  id sqlite3BeginT
71790 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 54  rigger(Parse*, T
717a0 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74  oken*,Token*,int
717b0 2c 69 6e 74 2c 49 64 4c 69 73 74 2a 2c 53 72 63  ,int,IdList*,Src
717c0 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20  List*,.         
717d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
717e0 20 20 45 78 70 72 2a 2c 69 6e 74 2c 20 69 6e 74    Expr*,int, int
717f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71800 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
71810 46 69 6e 69 73 68 54 72 69 67 67 65 72 28 50 61  FinishTrigger(Pa
71820 72 73 65 2a 2c 20 54 72 69 67 67 65 72 53 74 65  rse*, TriggerSte
71830 70 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c  p*, Token*);.SQL
71840 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
71850 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72  id sqlite3DropTr
71860 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 53 72  igger(Parse*, Sr
71870 63 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51  cList*, int);.SQ
71880 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
71890 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54  oid sqlite3DropT
718a0 72 69 67 67 65 72 50 74 72 28 50 61 72 73 65 2a  riggerPtr(Parse*
718b0 2c 20 54 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c  , Trigger*);.SQL
718c0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72  ITE_PRIVATE   Tr
718d0 69 67 67 65 72 20 2a 73 71 6c 69 74 65 33 54 72  igger *sqlite3Tr
718e0 69 67 67 65 72 73 45 78 69 73 74 28 50 61 72 73  iggersExist(Pars
718f0 65 20 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74  e *, Table*, int
71900 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74  , ExprList*, int
71910 20 2a 70 4d 61 73 6b 29 3b 0a 53 51 4c 49 54 45   *pMask);.SQLITE
71920 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67  _PRIVATE   Trigg
71930 65 72 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67  er *sqlite3Trigg
71940 65 72 4c 69 73 74 28 50 61 72 73 65 20 2a 2c 20  erList(Parse *, 
71950 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45  Table *);.SQLITE
71960 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
71970 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72  sqlite3CodeRowTr
71980 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 54 72  igger(Parse*, Tr
71990 69 67 67 65 72 20 2a 2c 20 69 6e 74 2c 20 45 78  igger *, int, Ex
719a0 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 54 61  prList*, int, Ta
719b0 62 6c 65 20 2a 2c 0a 20 20 20 20 20 20 20 20 20  ble *,.         
719c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
719d0 20 20 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74     int, int, int
719e0 2c 20 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 73  , int);.  void s
719f0 71 6c 69 74 65 56 69 65 77 54 72 69 67 67 65 72  qliteViewTrigger
71a00 73 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a  s(Parse*, Table*
71a10 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 45 78  , Expr*, int, Ex
71a20 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45  prList*);.SQLITE
71a30 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
71a40 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
71a50 67 67 65 72 53 74 65 70 28 73 71 6c 69 74 65 33  ggerStep(sqlite3
71a60 2a 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a 29  *, TriggerStep*)
71a70 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
71a80 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a     TriggerStep *
71a90 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 53 65  sqlite3TriggerSe
71aa0 6c 65 63 74 53 74 65 70 28 73 71 6c 69 74 65 33  lectStep(sqlite3
71ab0 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49  *,Select*);.SQLI
71ac0 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69  TE_PRIVATE   Tri
71ad0 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65  ggerStep *sqlite
71ae0 33 54 72 69 67 67 65 72 49 6e 73 65 72 74 53 74  3TriggerInsertSt
71af0 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65  ep(sqlite3*,Toke
71b00 6e 2a 2c 20 49 64 4c 69 73 74 2a 2c 0a 20 20 20  n*, IdList*,.   
71b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71b30 20 20 20 20 20 45 78 70 72 4c 69 73 74 2a 2c 53       ExprList*,S
71b40 65 6c 65 63 74 2a 2c 75 38 29 3b 0a 53 51 4c 49  elect*,u8);.SQLI
71b50 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69  TE_PRIVATE   Tri
71b60 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65  ggerStep *sqlite
71b70 33 54 72 69 67 67 65 72 55 70 64 61 74 65 53 74  3TriggerUpdateSt
71b80 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65  ep(sqlite3*,Toke
71b90 6e 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 45 78  n*,ExprList*, Ex
71ba0 70 72 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45  pr*, u8);.SQLITE
71bb0 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67  _PRIVATE   Trigg
71bc0 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54  erStep *sqlite3T
71bd0 72 69 67 67 65 72 44 65 6c 65 74 65 53 74 65 70  riggerDeleteStep
71be0 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a  (sqlite3*,Token*
71bf0 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
71c00 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
71c10 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
71c20 67 67 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 54  gger(sqlite3*, T
71c30 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  rigger*);.SQLITE
71c40 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
71c50 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64  sqlite3UnlinkAnd
71c60 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 73 71  DeleteTrigger(sq
71c70 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74  lite3*,int,const
71c80 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   char*);.SQLITE_
71c90 50 52 49 56 41 54 45 20 20 20 75 33 32 20 73 71  PRIVATE   u32 sq
71ca0 6c 69 74 65 33 54 72 69 67 67 65 72 4f 6c 64 6d  lite3TriggerOldm
71cb0 61 73 6b 28 50 61 72 73 65 2a 2c 54 72 69 67 67  ask(Parse*,Trigg
71cc0 65 72 2a 2c 69 6e 74 2c 45 78 70 72 4c 69 73 74  er*,int,ExprList
71cd0 2a 2c 54 61 62 6c 65 2a 2c 69 6e 74 29 3b 0a 23  *,Table*,int);.#
71ce0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50   define sqlite3P
71cf0 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 29 20  arseToplevel(p) 
71d00 28 28 70 29 2d 3e 70 54 6f 70 6c 65 76 65 6c 20  ((p)->pToplevel 
71d10 3f 20 28 70 29 2d 3e 70 54 6f 70 6c 65 76 65 6c  ? (p)->pToplevel
71d20 20 3a 20 28 70 29 29 0a 23 65 6c 73 65 0a 23 20   : (p)).#else.# 
71d30 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72  define sqlite3Tr
71d40 69 67 67 65 72 73 45 78 69 73 74 28 42 2c 43 2c  iggersExist(B,C,
71d50 44 2c 45 2c 46 29 20 30 0a 23 20 64 65 66 69 6e  D,E,F) 0.# defin
71d60 65 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54  e sqlite3DeleteT
71d70 72 69 67 67 65 72 28 41 2c 42 29 0a 23 20 64 65  rigger(A,B).# de
71d80 66 69 6e 65 20 73 71 6c 69 74 65 33 44 72 6f 70  fine sqlite3Drop
71d90 54 72 69 67 67 65 72 50 74 72 28 41 2c 42 29 0a  TriggerPtr(A,B).
71da0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
71db0 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54  UnlinkAndDeleteT
71dc0 72 69 67 67 65 72 28 41 2c 42 2c 43 29 0a 23 20  rigger(A,B,C).# 
71dd0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f  define sqlite3Co
71de0 64 65 52 6f 77 54 72 69 67 67 65 72 28 41 2c 42  deRowTrigger(A,B
71df0 2c 43 2c 44 2c 45 2c 46 2c 47 2c 48 2c 49 2c 4a  ,C,D,E,F,G,H,I,J
71e00 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ).# define sqlit
71e10 65 33 54 72 69 67 67 65 72 4c 69 73 74 28 58 2c  e3TriggerList(X,
71e20 20 59 29 20 30 0a 23 20 64 65 66 69 6e 65 20 73   Y) 0.# define s
71e30 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65  qlite3ParseTople
71e40 76 65 6c 28 70 29 20 70 0a 23 20 64 65 66 69 6e  vel(p) p.# defin
71e50 65 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  e sqlite3Trigger
71e60 4f 6c 64 6d 61 73 6b 28 41 2c 42 2c 43 2c 44 2c  Oldmask(A,B,C,D,
71e70 45 2c 46 29 20 30 0a 23 65 6e 64 69 66 0a 0a 53  E,F) 0.#endif..S
71e80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
71e90 74 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70  t sqlite3JoinTyp
71ea0 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a  e(Parse*, Token*
71eb0 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a  , Token*, Token*
71ec0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71ed0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72  E void sqlite3Cr
71ee0 65 61 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 50  eateForeignKey(P
71ef0 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a  arse*, ExprList*
71f00 2c 20 54 6f 6b 65 6e 2a 2c 20 45 78 70 72 4c 69  , Token*, ExprLi
71f10 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  st*, int);.SQLIT
71f20 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
71f30 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 69  qlite3DeferForei
71f40 67 6e 4b 65 79 28 50 61 72 73 65 2a 2c 20 69 6e  gnKey(Parse*, in
71f50 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  t);.#ifndef SQLI
71f60 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
71f70 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49  ATION.SQLITE_PRI
71f80 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
71f90 74 65 33 41 75 74 68 52 65 61 64 28 50 61 72 73  te3AuthRead(Pars
71fa0 65 2a 2c 45 78 70 72 2a 2c 53 63 68 65 6d 61 2a  e*,Expr*,Schema*
71fb0 2c 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49  ,SrcList*);.SQLI
71fc0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
71fd0 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
71fe0 6b 28 50 61 72 73 65 2a 2c 69 6e 74 2c 20 63 6f  k(Parse*,int, co
71ff0 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74  nst char*, const
72000 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68   char*, const ch
72010 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ar*);.SQLITE_PRI
72020 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
72030 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75  te3AuthContextPu
72040 73 68 28 50 61 72 73 65 2a 2c 20 41 75 74 68 43  sh(Parse*, AuthC
72050 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 63  ontext*, const c
72060 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
72070 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
72080 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50  ite3AuthContextP
72090 6f 70 28 41 75 74 68 43 6f 6e 74 65 78 74 2a 29  op(AuthContext*)
720a0 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ;.#else.# define
720b0 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64   sqlite3AuthRead
720c0 28 61 2c 62 2c 63 2c 64 29 0a 23 20 64 65 66 69  (a,b,c,d).# defi
720d0 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  ne sqlite3AuthCh
720e0 65 63 6b 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20  eck(a,b,c,d,e)  
720f0 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 64 65    SQLITE_OK.# de
72100 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68  fine sqlite3Auth
72110 43 6f 6e 74 65 78 74 50 75 73 68 28 61 2c 62 2c  ContextPush(a,b,
72120 63 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  c).# define sqli
72130 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f  te3AuthContextPo
72140 70 28 61 29 20 20 28 28 76 6f 69 64 29 28 61 29  p(a)  ((void)(a)
72150 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f  ).#endif.SQLITE_
72160 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
72170 69 74 65 33 41 74 74 61 63 68 28 50 61 72 73 65  ite3Attach(Parse
72180 2a 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 2c  *, Expr*, Expr*,
72190 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
721a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
721b0 69 74 65 33 44 65 74 61 63 68 28 50 61 72 73 65  ite3Detach(Parse
721c0 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54  *, Expr*);.SQLIT
721d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
721e0 6c 69 74 65 33 42 74 72 65 65 46 61 63 74 6f 72  lite3BtreeFactor
721f0 79 28 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 20  y(const sqlite3 
72200 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *db, const char 
72210 2a 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 20  *zFilename,.    
72220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72230 20 20 20 69 6e 74 20 6f 6d 69 74 4a 6f 75 72 6e     int omitJourn
72240 61 6c 2c 20 69 6e 74 20 6e 43 61 63 68 65 2c 20  al, int nCache, 
72250 69 6e 74 20 66 6c 61 67 73 2c 20 42 74 72 65 65  int flags, Btree
72260 20 2a 2a 70 70 42 74 72 65 65 29 3b 0a 53 51 4c   **ppBtree);.SQL
72270 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
72280 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28 44  sqlite3FixInit(D
72290 62 46 69 78 65 72 2a 2c 20 50 61 72 73 65 2a 2c  bFixer*, Parse*,
722a0 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72   int, const char
722b0 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29  *, const Token*)
722c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
722d0 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53   int sqlite3FixS
722e0 72 63 4c 69 73 74 28 44 62 46 69 78 65 72 2a 2c  rcList(DbFixer*,
722f0 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49   SrcList*);.SQLI
72300 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
72310 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28  qlite3FixSelect(
72320 44 62 46 69 78 65 72 2a 2c 20 53 65 6c 65 63 74  DbFixer*, Select
72330 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
72340 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69  TE int sqlite3Fi
72350 78 45 78 70 72 28 44 62 46 69 78 65 72 2a 2c 20  xExpr(DbFixer*, 
72360 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
72370 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
72380 65 33 46 69 78 45 78 70 72 4c 69 73 74 28 44 62  e3FixExprList(Db
72390 46 69 78 65 72 2a 2c 20 45 78 70 72 4c 69 73 74  Fixer*, ExprList
723a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
723b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69  TE int sqlite3Fi
723c0 78 54 72 69 67 67 65 72 53 74 65 70 28 44 62 46  xTriggerStep(DbF
723d0 69 78 65 72 2a 2c 20 54 72 69 67 67 65 72 53 74  ixer*, TriggerSt
723e0 65 70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ep*);.SQLITE_PRI
723f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
72400 41 74 6f 46 28 63 6f 6e 73 74 20 63 68 61 72 20  AtoF(const char 
72410 2a 7a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a 53 51  *z, double*);.SQ
72420 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
72430 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32   sqlite3GetInt32
72440 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69  (const char *, i
72450 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  nt*);.SQLITE_PRI
72460 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
72470 46 69 74 73 49 6e 36 34 42 69 74 73 28 63 6f 6e  FitsIn64Bits(con
72480 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b  st char *, int);
72490 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
724a0 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 31 36  int sqlite3Utf16
724b0 42 79 74 65 4c 65 6e 28 63 6f 6e 73 74 20 76 6f  ByteLen(const vo
724c0 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e  id *pData, int n
724d0 43 68 61 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Char);.SQLITE_PR
724e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
724f0 33 55 74 66 38 43 68 61 72 4c 65 6e 28 63 6f 6e  3Utf8CharLen(con
72500 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
72510 69 6e 74 20 6e 42 79 74 65 29 3b 0a 53 51 4c 49  int nByte);.SQLI
72520 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
72530 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 63  qlite3Utf8Read(c
72540 6f 6e 73 74 20 75 38 2a 2c 20 63 6f 6e 73 74 20  onst u8*, const 
72550 75 38 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f  u8**);../*.** Ro
72560 75 74 69 6e 65 73 20 74 6f 20 72 65 61 64 20 61  utines to read a
72570 6e 64 20 77 72 69 74 65 20 76 61 72 69 61 62 6c  nd write variabl
72580 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
72590 73 2e 20 20 54 68 65 73 65 20 75 73 65 64 20 74  s.  These used t
725a0 6f 0a 2a 2a 20 62 65 20 64 65 66 69 6e 65 64 20  o.** be defined 
725b0 6c 6f 63 61 6c 6c 79 2c 20 62 75 74 20 6e 6f 77  locally, but now
725c0 20 77 65 20 75 73 65 20 74 68 65 20 76 61 72 69   we use the vari
725d0 6e 74 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74  nt routines in t
725e0 68 65 20 75 74 69 6c 2e 63 0a 2a 2a 20 66 69 6c  he util.c.** fil
725f0 65 2e 20 20 43 6f 64 65 20 73 68 6f 75 6c 64 20  e.  Code should 
72600 75 73 65 20 74 68 65 20 4d 41 43 52 4f 20 66 6f  use the MACRO fo
72610 72 6d 73 20 62 65 6c 6f 77 2c 20 61 73 20 74 68  rms below, as th
72620 65 20 56 61 72 69 6e 74 33 32 20 76 65 72 73 69  e Varint32 versi
72630 6f 6e 73 0a 2a 2a 20 61 72 65 20 63 6f 64 65 64  ons.** are coded
72640 20 74 6f 20 61 73 73 75 6d 65 20 74 68 65 20 73   to assume the s
72650 69 6e 67 6c 65 20 62 79 74 65 20 63 61 73 65 20  ingle byte case 
72660 69 73 20 61 6c 72 65 61 64 79 20 68 61 6e 64 6c  is already handl
72670 65 64 20 28 77 68 69 63 68 20 0a 2a 2a 20 74 68  ed (which .** th
72680 65 20 4d 41 43 52 4f 20 66 6f 72 6d 20 64 6f 65  e MACRO form doe
72690 73 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  s)..*/.SQLITE_PR
726a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
726b0 33 50 75 74 56 61 72 69 6e 74 28 75 6e 73 69 67  3PutVarint(unsig
726c0 6e 65 64 20 63 68 61 72 2a 2c 20 75 36 34 29 3b  ned char*, u64);
726d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
726e0 69 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56 61  int sqlite3PutVa
726f0 72 69 6e 74 33 32 28 75 6e 73 69 67 6e 65 64 20  rint32(unsigned 
72700 63 68 61 72 2a 2c 20 75 33 32 29 3b 0a 53 51 4c  char*, u32);.SQL
72710 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73  ITE_PRIVATE u8 s
72720 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28  qlite3GetVarint(
72730 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
72740 68 61 72 20 2a 2c 20 75 36 34 20 2a 29 3b 0a 53  har *, u64 *);.S
72750 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 38  QLITE_PRIVATE u8
72760 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e   sqlite3GetVarin
72770 74 33 32 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e  t32(const unsign
72780 65 64 20 63 68 61 72 20 2a 2c 20 75 33 32 20 2a  ed char *, u32 *
72790 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
727a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 72  E int sqlite3Var
727b0 69 6e 74 4c 65 6e 28 75 36 34 20 76 29 3b 0a 0a  intLen(u64 v);..
727c0 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61 64 65 72  /*.** The header
727d0 20 6f 66 20 61 20 72 65 63 6f 72 64 20 63 6f 6e   of a record con
727e0 73 69 73 74 73 20 6f 66 20 61 20 73 65 71 75 65  sists of a seque
727f0 6e 63 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e  nce variable-len
72800 67 74 68 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a  gth integers..**
72810 20 54 68 65 73 65 20 69 6e 74 65 67 65 72 73 20   These integers 
72820 61 72 65 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79  are almost alway
72830 73 20 73 6d 61 6c 6c 20 61 6e 64 20 61 72 65 20  s small and are 
72840 65 6e 63 6f 64 65 64 20 61 73 20 61 20 73 69 6e  encoded as a sin
72850 67 6c 65 20 62 79 74 65 2e 0a 2a 2a 20 54 68 65  gle byte..** The
72860 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f   following macro
72870 73 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65  s take advantage
72880 20 74 68 69 73 20 66 61 63 74 20 74 6f 20 70 72   this fact to pr
72890 6f 76 69 64 65 20 61 20 66 61 73 74 20 65 6e 63  ovide a fast enc
728a0 6f 64 65 0a 2a 2a 20 61 6e 64 20 64 65 63 6f 64  ode.** and decod
728b0 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72  e of the integer
728c0 73 20 69 6e 20 61 20 72 65 63 6f 72 64 20 68 65  s in a record he
728d0 61 64 65 72 2e 20 20 49 74 20 69 73 20 66 61 73  ader.  It is fas
728e0 74 65 72 20 66 6f 72 20 74 68 65 20 63 6f 6d 6d  ter for the comm
728f0 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 65 72 65  on.** case where
72900 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 73 20   the integer is 
72910 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 20 20  a single byte.  
72920 49 74 20 69 73 20 61 20 6c 69 74 74 6c 65 20 73  It is a little s
72930 6c 6f 77 65 72 20 77 68 65 6e 20 74 68 65 0a 2a  lower when the.*
72940 2a 20 69 6e 74 65 67 65 72 20 69 73 20 74 77 6f  * integer is two
72950 20 6f 72 20 6d 6f 72 65 20 62 79 74 65 73 2e 20   or more bytes. 
72960 20 42 75 74 20 6f 76 65 72 61 6c 6c 20 69 74 20   But overall it 
72970 69 73 20 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a  is faster..**.**
72980 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65   The following e
72990 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20 65  xpressions are e
729a0 71 75 69 76 61 6c 65 6e 74 3a 0a 2a 2a 0a 2a 2a  quivalent:.**.**
729b0 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74 65 33       x = sqlite3
729c0 47 65 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20  GetVarint32( A, 
729d0 26 42 20 29 3b 0a 2a 2a 20 20 20 20 20 78 20 3d  &B );.**     x =
729e0 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e   sqlite3PutVarin
729f0 74 33 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 0a  t32( A, B );.**.
72a00 2a 2a 20 20 20 20 20 78 20 3d 20 67 65 74 56 61  **     x = getVa
72a10 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29 3b 0a  rint32( A, B );.
72a20 2a 2a 20 20 20 20 20 78 20 3d 20 70 75 74 56 61  **     x = putVa
72a30 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29 3b 0a  rint32( A, B );.
72a40 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65  **.*/.#define ge
72a50 74 56 61 72 69 6e 74 33 32 28 41 2c 42 29 20 20  tVarint32(A,B)  
72a60 28 75 38 29 28 28 2a 28 41 29 3c 28 75 38 29 30  (u8)((*(A)<(u8)0
72a70 78 38 30 29 20 3f 20 28 28 42 29 20 3d 20 28 75  x80) ? ((B) = (u
72a80 33 32 29 2a 28 41 29 29 2c 31 20 3a 20 73 71 6c  32)*(A)),1 : sql
72a90 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28  ite3GetVarint32(
72aa0 28 41 29 2c 20 28 75 33 32 20 2a 29 26 28 42 29  (A), (u32 *)&(B)
72ab0 29 29 0a 23 64 65 66 69 6e 65 20 70 75 74 56 61  )).#define putVa
72ac0 72 69 6e 74 33 32 28 41 2c 42 29 20 20 28 75 38  rint32(A,B)  (u8
72ad0 29 28 28 28 75 33 32 29 28 42 29 3c 28 75 33 32  )(((u32)(B)<(u32
72ae0 29 30 78 38 30 29 20 3f 20 28 2a 28 41 29 20 3d  )0x80) ? (*(A) =
72af0 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29   (unsigned char)
72b00 28 42 29 29 2c 31 20 3a 20 73 71 6c 69 74 65 33  (B)),1 : sqlite3
72b10 50 75 74 56 61 72 69 6e 74 33 32 28 28 41 29 2c  PutVarint32((A),
72b20 20 28 42 29 29 29 0a 23 64 65 66 69 6e 65 20 67   (B))).#define g
72b30 65 74 56 61 72 69 6e 74 20 20 20 20 73 71 6c 69  etVarint    sqli
72b40 74 65 33 47 65 74 56 61 72 69 6e 74 0a 23 64 65  te3GetVarint.#de
72b50 66 69 6e 65 20 70 75 74 56 61 72 69 6e 74 20 20  fine putVarint  
72b60 20 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69    sqlite3PutVari
72b70 6e 74 0a 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt...SQLITE_PRIV
72b80 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
72b90 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69  sqlite3IndexAffi
72ba0 6e 69 74 79 53 74 72 28 56 64 62 65 20 2a 2c 20  nityStr(Vdbe *, 
72bb0 49 6e 64 65 78 20 2a 29 3b 0a 53 51 4c 49 54 45  Index *);.SQLITE
72bc0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
72bd0 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69  lite3TableAffini
72be0 74 79 53 74 72 28 56 64 62 65 20 2a 2c 20 54 61  tyStr(Vdbe *, Ta
72bf0 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ble *);.SQLITE_P
72c00 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69  RIVATE char sqli
72c10 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69  te3CompareAffini
72c20 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20  ty(Expr *pExpr, 
72c30 63 68 61 72 20 61 66 66 32 29 3b 0a 53 51 4c 49  char aff2);.SQLI
72c40 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
72c50 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e  qlite3IndexAffin
72c60 69 74 79 4f 6b 28 45 78 70 72 20 2a 70 45 78 70  ityOk(Expr *pExp
72c70 72 2c 20 63 68 61 72 20 69 64 78 5f 61 66 66 69  r, char idx_affi
72c80 6e 69 74 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52  nity);.SQLITE_PR
72c90 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74  IVATE char sqlit
72ca0 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 45  e3ExprAffinity(E
72cb0 78 70 72 20 2a 70 45 78 70 72 29 3b 0a 53 51 4c  xpr *pExpr);.SQL
72cc0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
72cd0 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 63 6f  sqlite3Atoi64(co
72ce0 6e 73 74 20 63 68 61 72 2a 2c 20 69 36 34 2a 29  nst char*, i64*)
72cf0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
72d00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72   void sqlite3Err
72d10 6f 72 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  or(sqlite3*, int
72d20 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e  , const char*,..
72d30 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .);.SQLITE_PRIVA
72d40 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
72d50 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65  HexToBlob(sqlite
72d60 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  3*, const char *
72d70 7a 2c 20 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 54  z, int n);.SQLIT
72d80 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
72d90 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65  lite3TwoPartName
72da0 28 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e 20  (Parse *, Token 
72db0 2a 2c 20 54 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65  *, Token *, Toke
72dc0 6e 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  n **);.SQLITE_PR
72dd0 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72  IVATE const char
72de0 20 2a 73 71 6c 69 74 65 33 45 72 72 53 74 72 28   *sqlite3ErrStr(
72df0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
72e00 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
72e10 52 65 61 64 53 63 68 65 6d 61 28 50 61 72 73 65  ReadSchema(Parse
72e20 20 2a 70 50 61 72 73 65 29 3b 0a 53 51 4c 49 54   *pParse);.SQLIT
72e30 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65  E_PRIVATE CollSe
72e40 71 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 43 6f  q *sqlite3FindCo
72e50 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 2a 2c 75  llSeq(sqlite3*,u
72e60 38 20 65 6e 63 2c 20 63 6f 6e 73 74 20 63 68 61  8 enc, const cha
72e70 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  r*,int);.SQLITE_
72e80 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20  PRIVATE CollSeq 
72e90 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f  *sqlite3LocateCo
72ea0 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50 61  llSeq(Parse *pPa
72eb0 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  rse, const char*
72ec0 7a 4e 61 6d 65 29 3b 0a 53 51 4c 49 54 45 5f 50  zName);.SQLITE_P
72ed0 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a  RIVATE CollSeq *
72ee0 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53  sqlite3ExprCollS
72ef0 65 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  eq(Parse *pParse
72f00 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 3b 0a  , Expr *pExpr);.
72f10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45  SQLITE_PRIVATE E
72f20 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72  xpr *sqlite3Expr
72f30 53 65 74 43 6f 6c 6c 28 50 61 72 73 65 20 2a 70  SetColl(Parse *p
72f40 50 61 72 73 65 2c 20 45 78 70 72 20 2a 2c 20 54  Parse, Expr *, T
72f50 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  oken *);.SQLITE_
72f60 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
72f70 74 65 33 43 68 65 63 6b 43 6f 6c 6c 53 65 71 28  te3CheckCollSeq(
72f80 50 61 72 73 65 20 2a 2c 20 43 6f 6c 6c 53 65 71  Parse *, CollSeq
72f90 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
72fa0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43  ATE int sqlite3C
72fb0 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 50  heckObjectName(P
72fc0 61 72 73 65 20 2a 2c 20 63 6f 6e 73 74 20 63 68  arse *, const ch
72fd0 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ar *);.SQLITE_PR
72fe0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
72ff0 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73  e3VdbeSetChanges
73000 28 73 71 6c 69 74 65 33 20 2a 2c 20 69 6e 74 29  (sqlite3 *, int)
73010 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
73020 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  E const void *sq
73030 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 73  lite3ValueText(s
73040 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 75  qlite3_value*, u
73050 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  8);.SQLITE_PRIVA
73060 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61  TE int sqlite3Va
73070 6c 75 65 42 79 74 65 73 28 73 71 6c 69 74 65 33  lueBytes(sqlite3
73080 5f 76 61 6c 75 65 2a 2c 20 75 38 29 3b 0a 53 51  _value*, u8);.SQ
73090 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
730a0 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65  d sqlite3ValueSe
730b0 74 53 74 72 28 73 71 6c 69 74 65 33 5f 76 61 6c  tStr(sqlite3_val
730c0 75 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20  ue*, int, const 
730d0 76 6f 69 64 20 2a 2c 75 38 2c 20 0a 20 20 20 20  void *,u8, .    
730e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
730f0 20 20 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64      void(*)(void
73100 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  *));.SQLITE_PRIV
73110 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
73120 56 61 6c 75 65 46 72 65 65 28 73 71 6c 69 74 65  ValueFree(sqlite
73130 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54  3_value*);.SQLIT
73140 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65  E_PRIVATE sqlite
73150 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33  3_value *sqlite3
73160 56 61 6c 75 65 4e 65 77 28 73 71 6c 69 74 65 33  ValueNew(sqlite3
73170 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
73180 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
73190 33 55 74 66 31 36 74 6f 38 28 73 71 6c 69 74 65  3Utf16to8(sqlite
731a0 33 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a  3 *, const void*
731b0 2c 20 69 6e 74 29 3b 0a 23 69 66 64 65 66 20 53  , int);.#ifdef S
731c0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41  QLITE_ENABLE_STA
731d0 54 32 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  T2.SQLITE_PRIVAT
731e0 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 55  E char *sqlite3U
731f0 74 66 38 74 6f 31 36 28 73 71 6c 69 74 65 33 20  tf8to16(sqlite3 
73200 2a 2c 20 75 38 2c 20 63 68 61 72 20 2a 2c 20 69  *, u8, char *, i
73210 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 23 65 6e 64  nt, int *);.#end
73220 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  if.SQLITE_PRIVAT
73230 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c  E int sqlite3Val
73240 75 65 46 72 6f 6d 45 78 70 72 28 73 71 6c 69 74  ueFromExpr(sqlit
73250 65 33 20 2a 2c 20 45 78 70 72 20 2a 2c 20 75 38  e3 *, Expr *, u8
73260 2c 20 75 38 2c 20 73 71 6c 69 74 65 33 5f 76 61  , u8, sqlite3_va
73270 6c 75 65 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f  lue **);.SQLITE_
73280 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
73290 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66  ite3ValueApplyAf
732a0 66 69 6e 69 74 79 28 73 71 6c 69 74 65 33 5f 76  finity(sqlite3_v
732b0 61 6c 75 65 20 2a 2c 20 75 38 2c 20 75 38 29 3b  alue *, u8, u8);
732c0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
732d0 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 53 51 4c  AMALGAMATION.SQL
732e0 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
732f0 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
73300 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f  sqlite3UpperToLo
73310 77 65 72 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50 52  wer[];.SQLITE_PR
73320 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69  IVATE const unsi
73330 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74 65  gned char sqlite
73340 33 43 74 79 70 65 4d 61 70 5b 5d 3b 0a 53 51 4c  3CtypeMap[];.SQL
73350 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49  ITE_PRIVATE SQLI
73360 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 53 71  TE_WSD struct Sq
73370 6c 69 74 65 33 43 6f 6e 66 69 67 20 73 71 6c 69  lite3Config sqli
73380 74 65 33 43 6f 6e 66 69 67 3b 0a 53 51 4c 49 54  te3Config;.SQLIT
73390 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49 54 45  E_PRIVATE SQLITE
733a0 5f 57 53 44 20 46 75 6e 63 44 65 66 48 61 73 68  _WSD FuncDefHash
733b0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75   sqlite3GlobalFu
733c0 6e 63 74 69 6f 6e 73 3b 0a 53 51 4c 49 54 45 5f  nctions;.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 50 65 6e 64 69 6e 67 42 79 74 65 3b 0a  te3PendingByte;.
733f0 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52  #endif.SQLITE_PR
73400 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
73410 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28  e3RootPageMoved(
73420 44 62 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  Db*, int, int);.
73430 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
73440 6f 69 64 20 73 71 6c 69 74 65 33 52 65 69 6e 64  oid sqlite3Reind
73450 65 78 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e  ex(Parse*, Token
73460 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  *, Token*);.SQLI
73470 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
73480 73 71 6c 69 74 65 33 41 6c 74 65 72 46 75 6e 63  sqlite3AlterFunc
73490 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b  tions(sqlite3*);
734a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
734b0 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65  void sqlite3Alte
734c0 72 52 65 6e 61 6d 65 54 61 62 6c 65 28 50 61 72  rRenameTable(Par
734d0 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54  se*, SrcList*, T
734e0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
734f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
73500 65 33 47 65 74 54 6f 6b 65 6e 28 63 6f 6e 73 74  e3GetToken(const
73510 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
73520 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45  , int *);.SQLITE
73530 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
73540 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
73550 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63  (Parse*, const c
73560 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49  har*, ...);.SQLI
73570 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
73580 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65  sqlite3ExpirePre
73590 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28  paredStatements(
735a0 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54  sqlite3*);.SQLIT
735b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
735c0 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c  qlite3CodeSubsel
735d0 65 63 74 28 50 61 72 73 65 20 2a 2c 20 45 78 70  ect(Parse *, Exp
735e0 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  r *, int, int);.
735f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
73600 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63  oid sqlite3Selec
73610 74 50 72 65 70 28 50 61 72 73 65 2a 2c 20 53 65  tPrep(Parse*, Se
73620 6c 65 63 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65  lect*, NameConte
73630 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  xt*);.SQLITE_PRI
73640 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
73650 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
73660 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45  (NameContext*, E
73670 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
73680 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
73690 65 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e  e3ResolveSelectN
736a0 61 6d 65 73 28 50 61 72 73 65 2a 2c 20 53 65 6c  ames(Parse*, Sel
736b0 65 63 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78  ect*, NameContex
736c0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
736d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52  ATE int sqlite3R
736e0 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70  esolveOrderGroup
736f0 42 79 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63  By(Parse*, Selec
73700 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 63  t*, ExprList*, c
73710 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c  onst char*);.SQL
73720 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
73730 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65   sqlite3ColumnDe
73740 66 61 75 6c 74 28 56 64 62 65 20 2a 2c 20 54 61  fault(Vdbe *, Ta
73750 62 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29  ble *, int, int)
73760 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73770 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74   void sqlite3Alt
73780 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d  erFinishAddColum
73790 6e 28 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e  n(Parse *, Token
737a0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
737b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
737c0 41 6c 74 65 72 42 65 67 69 6e 41 64 64 43 6f 6c  AlterBeginAddCol
737d0 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20 53 72 63  umn(Parse *, Src
737e0 4c 69 73 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  List *);.SQLITE_
737f0 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20  PRIVATE CollSeq 
73800 2a 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53  *sqlite3GetCollS
73810 65 71 28 73 71 6c 69 74 65 33 2a 2c 20 75 38 2c  eq(sqlite3*, u8,
73820 20 43 6f 6c 6c 53 65 71 20 2a 2c 20 63 6f 6e 73   CollSeq *, cons
73830 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45  t char*);.SQLITE
73840 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71  _PRIVATE char sq
73850 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70  lite3AffinityTyp
73860 65 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  e(const char*);.
73870 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
73880 6f 69 64 20 73 71 6c 69 74 65 33 41 6e 61 6c 79  oid sqlite3Analy
73890 7a 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e  ze(Parse*, Token
738a0 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  *, Token*);.SQLI
738b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
738c0 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75 73 79  qlite3InvokeBusy
738d0 48 61 6e 64 6c 65 72 28 42 75 73 79 48 61 6e 64  Handler(BusyHand
738e0 6c 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ler*);.SQLITE_PR
738f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
73900 33 46 69 6e 64 44 62 28 73 71 6c 69 74 65 33 2a  3FindDb(sqlite3*
73910 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  , Token*);.SQLIT
73920 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
73930 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 28  lite3FindDbName(
73940 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74  sqlite3 *, const
73950 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45   char *);.SQLITE
73960 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
73970 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64  ite3AnalysisLoad
73980 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 69 44  (sqlite3*,int iD
73990 42 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  B);.SQLITE_PRIVA
739a0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
739b0 65 6c 65 74 65 49 6e 64 65 78 53 61 6d 70 6c 65  eleteIndexSample
739c0 73 28 49 6e 64 65 78 2a 29 3b 0a 53 51 4c 49 54  s(Index*);.SQLIT
739d0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
739e0 71 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77  qlite3DefaultRow
739f0 45 73 74 28 49 6e 64 65 78 2a 29 3b 0a 53 51 4c  Est(Index*);.SQL
73a00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
73a10 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
73a20 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 73 28 73 71  LikeFunctions(sq
73a30 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51  lite3*, int);.SQ
73a40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
73a50 20 73 71 6c 69 74 65 33 49 73 4c 69 6b 65 46 75   sqlite3IsLikeFu
73a60 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c  nction(sqlite3*,
73a70 45 78 70 72 2a 2c 69 6e 74 2a 2c 63 68 61 72 2a  Expr*,int*,char*
73a80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
73a90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 69  E void sqlite3Mi
73aa0 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28  nimumFileFormat(
73ab0 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74  Parse*, int, int
73ac0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
73ad0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63  E void sqlite3Sc
73ae0 68 65 6d 61 46 72 65 65 28 76 6f 69 64 20 2a 29  hemaFree(void *)
73af0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73b00 20 53 63 68 65 6d 61 20 2a 73 71 6c 69 74 65 33   Schema *sqlite3
73b10 53 63 68 65 6d 61 47 65 74 28 73 71 6c 69 74 65  SchemaGet(sqlite
73b20 33 20 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a 53  3 *, Btree *);.S
73b30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
73b40 74 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  t sqlite3SchemaT
73b50 6f 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a  oIndex(sqlite3 *
73b60 64 62 2c 20 53 63 68 65 6d 61 20 2a 29 3b 0a 53  db, Schema *);.S
73b70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 4b 65  QLITE_PRIVATE Ke
73b80 79 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 49 6e  yInfo *sqlite3In
73b90 64 65 78 4b 65 79 69 6e 66 6f 28 50 61 72 73 65  dexKeyinfo(Parse
73ba0 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b 0a 53 51   *, Index *);.SQ
73bb0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
73bc0 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75   sqlite3CreateFu
73bd0 6e 63 28 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f  nc(sqlite3 *, co
73be0 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c  nst char *, int,
73bf0 20 69 6e 74 2c 20 76 6f 69 64 20 2a 2c 20 0a 20   int, void *, . 
73c00 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 65   void (*)(sqlite
73c10 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
73c20 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29  qlite3_value **)
73c30 2c 0a 20 20 76 6f 69 64 20 28 2a 29 28 73 71 6c  ,.  void (*)(sql
73c40 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
73c50 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  t,sqlite3_value 
73c60 2a 2a 29 2c 20 76 6f 69 64 20 28 2a 29 28 73 71  **), void (*)(sq
73c70 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 29  lite3_context*))
73c80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73c90 20 69 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45   int sqlite3ApiE
73ca0 78 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  xit(sqlite3 *db,
73cb0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
73cc0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
73cd0 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73  3OpenTempDatabas
73ce0 65 28 50 61 72 73 65 20 2a 29 3b 0a 0a 53 51 4c  e(Parse *);..SQL
73cf0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
73d00 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
73d10 49 6e 69 74 28 53 74 72 41 63 63 75 6d 2a 2c 20  Init(StrAccum*, 
73d20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29  char*, int, int)
73d30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73d40 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72   void sqlite3Str
73d50 41 63 63 75 6d 41 70 70 65 6e 64 28 53 74 72 41  AccumAppend(StrA
73d60 63 63 75 6d 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ccum*,const char
73d70 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  *,int);.SQLITE_P
73d80 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
73d90 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69  ite3StrAccumFini
73da0 73 68 28 53 74 72 41 63 63 75 6d 2a 29 3b 0a 53  sh(StrAccum*);.S
73db0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
73dc0 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 63  id sqlite3StrAcc
73dd0 75 6d 52 65 73 65 74 28 53 74 72 41 63 63 75 6d  umReset(StrAccum
73de0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
73df0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
73e00 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 53 65  electDestInit(Se
73e10 6c 65 63 74 44 65 73 74 2a 2c 69 6e 74 2c 69 6e  lectDest*,int,in
73e20 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  t);..SQLITE_PRIV
73e30 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
73e40 42 61 63 6b 75 70 52 65 73 74 61 72 74 28 73 71  BackupRestart(sq
73e50 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 29 3b  lite3_backup *);
73e60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
73e70 76 6f 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b  void sqlite3Back
73e80 75 70 55 70 64 61 74 65 28 73 71 6c 69 74 65 33  upUpdate(sqlite3
73e90 5f 62 61 63 6b 75 70 20 2a 2c 20 50 67 6e 6f 2c  _backup *, Pgno,
73ea0 20 63 6f 6e 73 74 20 75 38 20 2a 29 3b 0a 0a 2f   const u8 *);../
73eb0 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61  *.** The interfa
73ec0 63 65 20 74 6f 20 74 68 65 20 4c 45 4d 4f 4e 2d  ce to the LEMON-
73ed0 67 65 6e 65 72 61 74 65 64 20 70 61 72 73 65 72  generated parser
73ee0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
73ef0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
73f00 50 61 72 73 65 72 41 6c 6c 6f 63 28 76 6f 69 64  ParserAlloc(void
73f10 2a 28 2a 29 28 73 69 7a 65 5f 74 29 29 3b 0a 53  *(*)(size_t));.S
73f20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
73f30 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  id sqlite3Parser
73f40 46 72 65 65 28 76 6f 69 64 2a 2c 20 76 6f 69 64  Free(void*, void
73f50 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c  (*)(void*));.SQL
73f60 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
73f70 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 28 76   sqlite3Parser(v
73f80 6f 69 64 2a 2c 20 69 6e 74 2c 20 54 6f 6b 65 6e  oid*, int, Token
73f90 2c 20 50 61 72 73 65 2a 29 3b 0a 23 69 66 64 65  , Parse*);.#ifde
73fa0 66 20 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43  f YYTRACKMAXSTAC
73fb0 4b 44 45 50 54 48 0a 53 51 4c 49 54 45 5f 50 52  KDEPTH.SQLITE_PR
73fc0 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
73fd0 74 65 33 50 61 72 73 65 72 53 74 61 63 6b 50 65  te3ParserStackPe
73fe0 61 6b 28 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 69  ak(void*);.#endi
73ff0 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  f..SQLITE_PRIVAT
74000 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75  E void sqlite3Au
74010 74 6f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 73  toLoadExtensions
74020 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 69 66 6e  (sqlite3*);.#ifn
74030 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
74040 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53  LOAD_EXTENSION.S
74050 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
74060 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73  void sqlite3Clos
74070 65 45 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69  eExtensions(sqli
74080 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64  te3*);.#else.# d
74090 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6c 6f  efine sqlite3Clo
740a0 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 58 29 0a  seExtensions(X).
740b0 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20  #endif..#ifndef 
740c0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
740d0 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f  ED_CACHE.SQLITE_
740e0 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
740f0 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28  qlite3TableLock(
74100 50 61 72 73 65 20 2a 2c 20 69 6e 74 2c 20 69 6e  Parse *, int, in
74110 74 2c 20 75 38 2c 20 63 6f 6e 73 74 20 63 68 61  t, u8, const cha
74120 72 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64  r *);.#else.  #d
74130 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 61 62  efine sqlite3Tab
74140 6c 65 4c 6f 63 6b 28 76 2c 77 2c 78 2c 79 2c 7a  leLock(v,w,x,y,z
74150 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  ).#endif..#ifdef
74160 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
74170 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
74180 74 20 73 71 6c 69 74 65 33 55 74 66 38 54 6f 38  t sqlite3Utf8To8
74190 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29  (unsigned char*)
741a0 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  ;.#endif..#ifdef
741b0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
741c0 54 55 41 4c 54 41 42 4c 45 0a 23 20 20 64 65 66  TUALTABLE.#  def
741d0 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 43  ine sqlite3VtabC
741e0 6c 65 61 72 28 59 29 0a 23 20 20 64 65 66 69 6e  lear(Y).#  defin
741f0 65 20 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e  e sqlite3VtabSyn
74200 63 28 58 2c 59 29 20 53 51 4c 49 54 45 5f 4f 4b  c(X,Y) SQLITE_OK
74210 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74  .#  define sqlit
74220 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 58  e3VtabRollback(X
74230 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69  ).#  define sqli
74240 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 58 29  te3VtabCommit(X)
74250 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74  .#  define sqlit
74260 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29  e3VtabInSync(db)
74270 20 30 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c   0.#  define sql
74280 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 58 29 20  ite3VtabLock(X) 
74290 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74  .#  define sqlit
742a0 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 58 29 0a  e3VtabUnlock(X).
742b0 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  #  define sqlite
742c0 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28  3VtabUnlockList(
742d0 58 29 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f  X).#else.SQLITE_
742e0 50 52 49 56 41 54 45 20 20 20 20 76 6f 69 64 20  PRIVATE    void 
742f0 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72  sqlite3VtabClear
74300 28 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45  (Table*);.SQLITE
74310 5f 50 52 49 56 41 54 45 20 20 20 20 69 6e 74 20  _PRIVATE    int 
74320 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28  sqlite3VtabSync(
74330 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 68 61  sqlite3 *db, cha
74340 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  r **);.SQLITE_PR
74350 49 56 41 54 45 20 20 20 20 69 6e 74 20 73 71 6c  IVATE    int sql
74360 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b  ite3VtabRollback
74370 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53  (sqlite3 *db);.S
74380 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
74390 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62   int sqlite3Vtab
743a0 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a  Commit(sqlite3 *
743b0 64 62 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  db);.SQLITE_PRIV
743c0 41 54 45 20 20 20 20 76 6f 69 64 20 73 71 6c 69  ATE    void sqli
743d0 74 65 33 56 74 61 62 4c 6f 63 6b 28 56 54 61 62  te3VtabLock(VTab
743e0 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  le *);.SQLITE_PR
743f0 49 56 41 54 45 20 20 20 20 76 6f 69 64 20 73 71  IVATE    void sq
74400 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28  lite3VtabUnlock(
74410 56 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54  VTable *);.SQLIT
74420 45 5f 50 52 49 56 41 54 45 20 20 20 20 76 6f 69  E_PRIVATE    voi
74430 64 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c  d sqlite3VtabUnl
74440 6f 63 6b 4c 69 73 74 28 73 71 6c 69 74 65 33 2a  ockList(sqlite3*
74450 29 3b 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c  );.#  define sql
74460 69 74 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64  ite3VtabInSync(d
74470 62 29 20 28 28 64 62 29 2d 3e 6e 56 54 72 61 6e  b) ((db)->nVTran
74480 73 3e 30 20 26 26 20 28 64 62 29 2d 3e 61 56 54  s>0 && (db)->aVT
74490 72 61 6e 73 3d 3d 30 29 0a 23 65 6e 64 69 66 0a  rans==0).#endif.
744a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
744b0 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 4d  oid sqlite3VtabM
744c0 61 6b 65 57 72 69 74 61 62 6c 65 28 50 61 72 73  akeWritable(Pars
744d0 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49  e*,Table*);.SQLI
744e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
744f0 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e  sqlite3VtabBegin
74500 50 61 72 73 65 28 50 61 72 73 65 2a 2c 20 54 6f  Parse(Parse*, To
74510 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f  ken*, Token*, To
74520 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
74530 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
74540 65 33 56 74 61 62 46 69 6e 69 73 68 50 61 72 73  e3VtabFinishPars
74550 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a  e(Parse*, Token*
74560 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
74570 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
74580 61 62 41 72 67 49 6e 69 74 28 50 61 72 73 65 2a  abArgInit(Parse*
74590 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
745a0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
745b0 61 62 41 72 67 45 78 74 65 6e 64 28 50 61 72 73  abArgExtend(Pars
745c0 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c  e*, Token*);.SQL
745d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
745e0 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43  sqlite3VtabCallC
745f0 72 65 61 74 65 28 73 71 6c 69 74 65 33 2a 2c 20  reate(sqlite3*, 
74600 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  int, const char 
74610 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c  *, char **);.SQL
74620 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
74630 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43  sqlite3VtabCallC
74640 6f 6e 6e 65 63 74 28 50 61 72 73 65 2a 2c 20 54  onnect(Parse*, T
74650 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  able*);.SQLITE_P
74660 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
74670 65 33 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f  e3VtabCallDestro
74680 79 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c  y(sqlite3*, int,
74690 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a   const char *);.
746a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
746b0 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 42 65  nt sqlite3VtabBe
746c0 67 69 6e 28 73 71 6c 69 74 65 33 20 2a 2c 20 56  gin(sqlite3 *, V
746d0 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45  Table *);.SQLITE
746e0 5f 50 52 49 56 41 54 45 20 46 75 6e 63 44 65 66  _PRIVATE FuncDef
746f0 20 2a 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65   *sqlite3VtabOve
74700 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 73 71  rloadFunction(sq
74710 6c 69 74 65 33 20 2a 2c 46 75 6e 63 44 65 66 2a  lite3 *,FuncDef*
74720 2c 20 69 6e 74 20 6e 41 72 67 2c 20 45 78 70 72  , int nArg, Expr
74730 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
74740 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49  TE void sqlite3I
74750 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f 6e 28 73  nvalidFunction(s
74760 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
74770 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
74780 65 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  e**);.SQLITE_PRI
74790 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
747a0 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73  TransferBindings
747b0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2c  (sqlite3_stmt *,
747c0 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29   sqlite3_stmt *)
747d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
747e0 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 70 72   int sqlite3Repr
747f0 65 70 61 72 65 28 56 64 62 65 2a 29 3b 0a 53 51  epare(Vdbe*);.SQ
74800 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
74810 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  d sqlite3ExprLis
74820 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 50 61 72  tCheckLength(Par
74830 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20  se*, ExprList*, 
74840 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51  const char*);.SQ
74850 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c  LITE_PRIVATE Col
74860 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 42 69 6e  lSeq *sqlite3Bin
74870 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65  aryCompareCollSe
74880 71 28 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20  q(Parse *, Expr 
74890 2a 2c 20 45 78 70 72 20 2a 29 3b 0a 53 51 4c 49  *, Expr *);.SQLI
748a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
748b0 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f  qlite3TempInMemo
748c0 72 79 28 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ry(const sqlite3
748d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
748e0 54 45 20 56 54 61 62 6c 65 20 2a 73 71 6c 69 74  TE VTable *sqlit
748f0 65 33 47 65 74 56 54 61 62 6c 65 28 73 71 6c 69  e3GetVTable(sqli
74900 74 65 33 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 0a  te3*, Table*);..
74910 0a 0a 2f 2a 0a 2a 2a 20 41 76 61 69 6c 61 62 6c  ../*.** Availabl
74920 65 20 66 61 75 6c 74 20 69 6e 6a 65 63 74 6f 72  e fault injector
74930 73 2e 20 20 53 68 6f 75 6c 64 20 62 65 20 6e 75  s.  Should be nu
74940 6d 62 65 72 65 64 20 62 65 67 69 6e 6e 69 6e 67  mbered beginning
74950 20 77 69 74 68 20 30 2e 0a 2a 2f 0a 23 64 65 66   with 0..*/.#def
74960 69 6e 65 20 53 51 4c 49 54 45 5f 46 41 55 4c 54  ine SQLITE_FAULT
74970 49 4e 4a 45 43 54 4f 52 5f 4d 41 4c 4c 4f 43 20  INJECTOR_MALLOC 
74980 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51      0.#define SQ
74990 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43 54  LITE_FAULTINJECT
749a0 4f 52 5f 43 4f 55 4e 54 20 20 20 20 20 20 31 0a  OR_COUNT      1.
749b0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72  ./*.** The inter
749c0 66 61 63 65 20 74 6f 20 74 68 65 20 63 6f 64 65  face to the code
749d0 20 69 6e 20 66 61 75 6c 74 2e 63 20 75 73 65 64   in fault.c used
749e0 20 66 6f 72 20 69 64 65 6e 74 69 66 79 69 6e 67   for identifying
749f0 20 22 62 65 6e 69 67 6e 22 0a 2a 2a 20 6d 61 6c   "benign".** mal
74a00 6c 6f 63 20 66 61 69 6c 75 72 65 73 2e 20 54 68  loc failures. Th
74a10 69 73 20 69 73 20 6f 6e 6c 79 20 70 72 65 73 65  is is only prese
74a20 6e 74 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49  nt if SQLITE_OMI
74a30 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 2a  T_BUILTIN_TEST.*
74a40 2a 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64  * is not defined
74a50 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
74a60 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e  ITE_OMIT_BUILTIN
74a70 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49  _TEST.SQLITE_PRI
74a80 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
74a90 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61  te3BeginBenignMa
74aa0 6c 6c 6f 63 28 76 6f 69 64 29 3b 0a 53 51 4c 49  lloc(void);.SQLI
74ab0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
74ac0 64 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69  d sqlite3EndBeni
74ad0 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 3b 0a  gnMalloc(void);.
74ae0 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20  #else.  #define 
74af0 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69  sqlite3BeginBeni
74b00 67 6e 4d 61 6c 6c 6f 63 28 29 0a 20 20 23 64 65  gnMalloc().  #de
74b10 66 69 6e 65 20 73 71 6c 69 74 65 33 45 6e 64 42  fine sqlite3EndB
74b20 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 23 65  enignMalloc().#e
74b30 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 49 4e  ndif..#define IN
74b40 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 20 20 20  _INDEX_ROWID    
74b50 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65         1.#define
74b60 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 20 20 20   IN_INDEX_EPH   
74b70 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66            2.#def
74b80 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44  ine IN_INDEX_IND
74b90 45 58 20 20 20 20 20 20 20 20 20 20 20 33 0a 53  EX           3.S
74ba0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
74bb0 74 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49  t sqlite3FindInI
74bc0 6e 64 65 78 28 50 61 72 73 65 20 2a 2c 20 45 78  ndex(Parse *, Ex
74bd0 70 72 20 2a 2c 20 69 6e 74 2a 29 3b 0a 0a 23 69  pr *, int*);..#i
74be0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
74bf0 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a  LE_ATOMIC_WRITE.
74c00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
74c10 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72   int sqlite3Jour
74c20 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f  nalOpen(sqlite3_
74c30 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61  vfs *, const cha
74c40 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c  r *, sqlite3_fil
74c50 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  e *, int, int);.
74c60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
74c70 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72   int sqlite3Jour
74c80 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74 65 33 5f  nalSize(sqlite3_
74c90 76 66 73 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  vfs *);.SQLITE_P
74ca0 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c  RIVATE   int sql
74cb0 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74  ite3JournalCreat
74cc0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
74cd0 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69  );.#else.  #defi
74ce0 6e 65 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61  ne sqlite3Journa
74cf0 6c 53 69 7a 65 28 70 56 66 73 29 20 28 28 70 56  lSize(pVfs) ((pV
74d00 66 73 29 2d 3e 73 7a 4f 73 46 69 6c 65 29 0a 23  fs)->szOsFile).#
74d10 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52  endif..SQLITE_PR
74d20 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
74d30 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e  e3MemJournalOpen
74d40 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29  (sqlite3_file *)
74d50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74d60 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 4a   int sqlite3MemJ
74d70 6f 75 72 6e 61 6c 53 69 7a 65 28 76 6f 69 64 29  ournalSize(void)
74d80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74d90 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4d 65   int sqlite3IsMe
74da0 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 33  mJournal(sqlite3
74db0 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 69 66 20 53  _file *);..#if S
74dc0 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44  QLITE_MAX_EXPR_D
74dd0 45 50 54 48 3e 30 0a 53 51 4c 49 54 45 5f 50 52  EPTH>0.SQLITE_PR
74de0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
74df0 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68  ite3ExprSetHeigh
74e00 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  t(Parse *pParse,
74e10 20 45 78 70 72 20 2a 70 29 3b 0a 53 51 4c 49 54   Expr *p);.SQLIT
74e20 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
74e30 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70  sqlite3SelectExp
74e40 72 48 65 69 67 68 74 28 53 65 6c 65 63 74 20 2a  rHeight(Select *
74e50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
74e60 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 45  E   int sqlite3E
74e70 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 50  xprCheckHeight(P
74e80 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c  arse*, int);.#el
74e90 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c  se.  #define sql
74ea0 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68  ite3ExprSetHeigh
74eb0 74 28 78 2c 79 29 0a 20 20 23 64 65 66 69 6e 65  t(x,y).  #define
74ec0 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78   sqlite3SelectEx
74ed0 70 72 48 65 69 67 68 74 28 78 29 20 30 0a 20 20  prHeight(x) 0.  
74ee0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45  #define sqlite3E
74ef0 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 78  xprCheckHeight(x
74f00 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49  ,y).#endif..SQLI
74f10 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73  TE_PRIVATE u32 s
74f20 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 63  qlite3Get4byte(c
74f30 6f 6e 73 74 20 75 38 2a 29 3b 0a 53 51 4c 49 54  onst u8*);.SQLIT
74f40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
74f50 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 75  qlite3Put4byte(u
74f60 38 2a 2c 20 75 33 32 29 3b 0a 0a 23 69 66 64 65  8*, u32);..#ifde
74f70 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
74f80 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 53 51  UNLOCK_NOTIFY.SQ
74f90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
74fa0 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65  oid sqlite3Conne
74fb0 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 73 71 6c  ctionBlocked(sql
74fc0 69 74 65 33 20 2a 2c 20 73 71 6c 69 74 65 33 20  ite3 *, sqlite3 
74fd0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
74fe0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
74ff0 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63  3ConnectionUnloc
75000 6b 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ked(sqlite3 *db)
75010 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
75020 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43     void sqlite3C
75030 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28  onnectionClosed(
75040 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 23 65  sqlite3 *db);.#e
75050 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71  lse.  #define sq
75060 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42  lite3ConnectionB
75070 6c 6f 63 6b 65 64 28 78 2c 79 29 0a 20 20 23 64  locked(x,y).  #d
75080 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f 6e  efine sqlite3Con
75090 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28  nectionUnlocked(
750a0 78 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c  x).  #define sql
750b0 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c  ite3ConnectionCl
750c0 6f 73 65 64 28 78 29 0a 23 65 6e 64 69 66 0a 0a  osed(x).#endif..
750d0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
750e0 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
750f0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
75100 33 50 61 72 73 65 72 54 72 61 63 65 28 46 49 4c  3ParserTrace(FIL
75110 45 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 6e  E*, char *);.#en
75120 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  dif../*.** If th
75130 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 20  e SQLITE_ENABLE 
75140 49 4f 54 52 41 43 45 20 65 78 69 73 74 73 20 74  IOTRACE exists t
75150 68 65 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20 76  hen the global v
75160 61 72 69 61 62 6c 65 0a 2a 2a 20 73 71 6c 69 74  ariable.** sqlit
75170 65 33 49 6f 54 72 61 63 65 20 69 73 20 61 20 70  e3IoTrace is a p
75180 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 72 69 6e  ointer to a prin
75190 74 66 2d 6c 69 6b 65 20 72 6f 75 74 69 6e 65 20  tf-like routine 
751a0 75 73 65 64 20 74 6f 0a 2a 2a 20 70 72 69 6e 74  used to.** print
751b0 20 49 2f 4f 20 74 72 61 63 69 6e 67 20 6d 65 73   I/O tracing mes
751c0 73 61 67 65 73 2e 20 0a 2a 2f 0a 23 69 66 64 65  sages. .*/.#ifde
751d0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
751e0 49 4f 54 52 41 43 45 0a 23 20 64 65 66 69 6e 65  IOTRACE.# define
751f0 20 49 4f 54 52 41 43 45 28 41 29 20 20 69 66 28   IOTRACE(A)  if(
75200 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 20   sqlite3IoTrace 
75210 29 7b 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63  ){ sqlite3IoTrac
75220 65 20 41 3b 20 7d 0a 53 51 4c 49 54 45 5f 50 52  e A; }.SQLITE_PR
75230 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
75240 69 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53  ite3VdbeIOTraceS
75250 71 6c 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54  ql(Vdbe*);.SQLIT
75260 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 28  E_PRIVATE void (
75270 2a 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 29  *sqlite3IoTrace)
75280 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e  (const char*,...
75290 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  );.#else.# defin
752a0 65 20 49 4f 54 52 41 43 45 28 41 29 0a 23 20 64  e IOTRACE(A).# d
752b0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62  efine sqlite3Vdb
752c0 65 49 4f 54 72 61 63 65 53 71 6c 28 58 29 0a 23  eIOTraceSql(X).#
752d0 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f  endif..#endif../
752e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
752f0 6e 64 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e  nd of sqliteInt.
75300 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
75310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
75320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
75330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
75340 65 67 69 6e 20 66 69 6c 65 20 67 6c 6f 62 61 6c  egin file global
75350 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
75360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
75370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
75380 2a 0a 2a 2a 20 32 30 30 38 20 4a 75 6e 65 20 31  *.** 2008 June 1
75390 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  3.**.** The auth
753a0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
753b0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
753c0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
753d0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
753e0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
753f0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
75400 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
75410 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
75420 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
75430 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
75440 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
75450 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
75460 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
75470 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
75480 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
75490 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
754a0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
754b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
754c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
754d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
754e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
754f0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
75500 20 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69   contains defini
75510 74 69 6f 6e 73 20 6f 66 20 67 6c 6f 62 61 6c 20  tions of global 
75520 76 61 72 69 61 62 6c 65 73 20 61 6e 64 20 63 6f  variables and co
75530 6e 74 61 6e 74 73 2e 0a 2a 2f 0a 0a 0a 2f 2a 20  ntants..*/.../* 
75540 41 6e 20 61 72 72 61 79 20 74 6f 20 6d 61 70 20  An array to map 
75550 61 6c 6c 20 75 70 70 65 72 2d 63 61 73 65 20 63  all upper-case c
75560 68 61 72 61 63 74 65 72 73 20 69 6e 74 6f 20 74  haracters into t
75570 68 65 69 72 20 63 6f 72 72 65 73 70 6f 6e 64 69  heir correspondi
75580 6e 67 0a 2a 2a 20 6c 6f 77 65 72 2d 63 61 73 65  ng.** lower-case
75590 20 63 68 61 72 61 63 74 65 72 2e 20 0a 2a 2a 0a   character. .**.
755a0 2a 2a 20 53 51 4c 69 74 65 20 6f 6e 6c 79 20 63  ** SQLite only c
755b0 6f 6e 73 69 64 65 72 73 20 55 53 2d 41 53 43 49  onsiders US-ASCI
755c0 49 20 28 6f 72 20 45 42 43 44 49 43 29 20 63 68  I (or EBCDIC) ch
755d0 61 72 61 63 74 65 72 73 2e 20 20 57 65 20 64 6f  aracters.  We do
755e0 20 6e 6f 74 0a 2a 2a 20 68 61 6e 64 6c 65 20 63   not.** handle c
755f0 61 73 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20  ase conversions 
75600 66 6f 72 20 74 68 65 20 55 54 46 20 63 68 61 72  for the UTF char
75610 61 63 74 65 72 20 73 65 74 20 73 69 6e 63 65 20  acter set since 
75620 74 68 65 20 74 61 62 6c 65 73 0a 2a 2a 20 69 6e  the tables.** in
75630 76 6f 6c 76 65 64 20 61 72 65 20 6e 65 61 72 6c  volved are nearl
75640 79 20 61 73 20 62 69 67 20 6f 72 20 62 69 67 67  y as big or bigg
75650 65 72 20 74 68 61 6e 20 53 51 4c 69 74 65 20 69  er than SQLite i
75660 74 73 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45  tself..*/.SQLITE
75670 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75  _PRIVATE const u
75680 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c  nsigned char sql
75690 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72  ite3UpperToLower
756a0 5b 5d 20 3d 20 7b 0a 23 69 66 64 65 66 20 53 51  [] = {.#ifdef SQ
756b0 4c 49 54 45 5f 41 53 43 49 49 0a 20 20 20 20 20  LITE_ASCII.     
756c0 20 30 2c 20 20 31 2c 20 20 32 2c 20 20 33 2c 20   0,  1,  2,  3, 
756d0 20 34 2c 20 20 35 2c 20 20 36 2c 20 20 37 2c 20   4,  5,  6,  7, 
756e0 20 38 2c 20 20 39 2c 20 31 30 2c 20 31 31 2c 20   8,  9, 10, 11, 
756f0 31 32 2c 20 31 33 2c 20 31 34 2c 20 31 35 2c 20  12, 13, 14, 15, 
75700 31 36 2c 20 31 37 2c 0a 20 20 20 20 20 31 38 2c  16, 17,.     18,
75710 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 32 32 2c   19, 20, 21, 22,
75720 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 32 36 2c   23, 24, 25, 26,
75730 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 33 30 2c   27, 28, 29, 30,
75740 20 33 31 2c 20 33 32 2c 20 33 33 2c 20 33 34 2c   31, 32, 33, 34,
75750 20 33 35 2c 0a 20 20 20 20 20 33 36 2c 20 33 37   35,.     36, 37
75760 2c 20 33 38 2c 20 33 39 2c 20 34 30 2c 20 34 31  , 38, 39, 40, 41
75770 2c 20 34 32 2c 20 34 33 2c 20 34 34 2c 20 34 35  , 42, 43, 44, 45
75780 2c 20 34 36 2c 20 34 37 2c 20 34 38 2c 20 34 39  , 46, 47, 48, 49
75790 2c 20 35 30 2c 20 35 31 2c 20 35 32 2c 20 35 33  , 50, 51, 52, 53
757a0 2c 0a 20 20 20 20 20 35 34 2c 20 35 35 2c 20 35  ,.     54, 55, 5
757b0 36 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c 20 36  6, 57, 58, 59, 6
757c0 30 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c 20 36  0, 61, 62, 63, 6
757d0 34 2c 20 39 37 2c 20 39 38 2c 20 39 39 2c 31 30  4, 97, 98, 99,10
757e0 30 2c 31 30 31 2c 31 30 32 2c 31 30 33 2c 0a 20  0,101,102,103,. 
757f0 20 20 20 31 30 34 2c 31 30 35 2c 31 30 36 2c 31     104,105,106,1
75800 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31  07,108,109,110,1
75810 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 31  11,112,113,114,1
75820 31 35 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c 31  15,116,117,118,1
75830 31 39 2c 31 32 30 2c 31 32 31 2c 0a 20 20 20 20  19,120,121,.    
75840 31 32 32 2c 20 39 31 2c 20 39 32 2c 20 39 33 2c  122, 91, 92, 93,
75850 20 39 34 2c 20 39 35 2c 20 39 36 2c 20 39 37 2c   94, 95, 96, 97,
75860 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c   98, 99,100,101,
75870 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30 35 2c  102,103,104,105,
75880 31 30 36 2c 31 30 37 2c 0a 20 20 20 20 31 30 38  106,107,.    108
75890 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31 32  ,109,110,111,112
758a0 2c 31 31 33 2c 31 31 34 2c 31 31 35 2c 31 31 36  ,113,114,115,116
758b0 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32 30  ,117,118,119,120
758c0 2c 31 32 31 2c 31 32 32 2c 31 32 33 2c 31 32 34  ,121,122,123,124
758d0 2c 31 32 35 2c 0a 20 20 20 20 31 32 36 2c 31 32  ,125,.    126,12
758e0 37 2c 31 32 38 2c 31 32 39 2c 31 33 30 2c 31 33  7,128,129,130,13
758f0 31 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 31 33  1,132,133,134,13
75900 35 2c 31 33 36 2c 31 33 37 2c 31 33 38 2c 31 33  5,136,137,138,13
75910 39 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 34  9,140,141,142,14
75920 33 2c 0a 20 20 20 20 31 34 34 2c 31 34 35 2c 31  3,.    144,145,1
75930 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31  46,147,148,149,1
75940 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 31  50,151,152,153,1
75950 35 34 2c 31 35 35 2c 31 35 36 2c 31 35 37 2c 31  54,155,156,157,1
75960 35 38 2c 31 35 39 2c 31 36 30 2c 31 36 31 2c 0a  58,159,160,161,.
75970 20 20 20 20 31 36 32 2c 31 36 33 2c 31 36 34 2c      162,163,164,
75980 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c  165,166,167,168,
75990 31 36 39 2c 31 37 30 2c 31 37 31 2c 31 37 32 2c  169,170,171,172,
759a0 31 37 33 2c 31 37 34 2c 31 37 35 2c 31 37 36 2c  173,174,175,176,
759b0 31 37 37 2c 31 37 38 2c 31 37 39 2c 0a 20 20 20  177,178,179,.   
759c0 20 31 38 30 2c 31 38 31 2c 31 38 32 2c 31 38 33   180,181,182,183
759d0 2c 31 38 34 2c 31 38 35 2c 31 38 36 2c 31 38 37  ,184,185,186,187
759e0 2c 31 38 38 2c 31 38 39 2c 31 39 30 2c 31 39 31  ,188,189,190,191
759f0 2c 31 39 32 2c 31 39 33 2c 31 39 34 2c 31 39 35  ,192,193,194,195
75a00 2c 31 39 36 2c 31 39 37 2c 0a 20 20 20 20 31 39  ,196,197,.    19
75a10 38 2c 31 39 39 2c 32 30 30 2c 32 30 31 2c 32 30  8,199,200,201,20
75a20 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 32 30  2,203,204,205,20
75a30 36 2c 32 30 37 2c 32 30 38 2c 32 30 39 2c 32 31  6,207,208,209,21
75a40 30 2c 32 31 31 2c 32 31 32 2c 32 31 33 2c 32 31  0,211,212,213,21
75a50 34 2c 32 31 35 2c 0a 20 20 20 20 32 31 36 2c 32  4,215,.    216,2
75a60 31 37 2c 32 31 38 2c 32 31 39 2c 32 32 30 2c 32  17,218,219,220,2
75a70 32 31 2c 32 32 32 2c 32 32 33 2c 32 32 34 2c 32  21,222,223,224,2
75a80 32 35 2c 32 32 36 2c 32 32 37 2c 32 32 38 2c 32  25,226,227,228,2
75a90 32 39 2c 32 33 30 2c 32 33 31 2c 32 33 32 2c 32  29,230,231,232,2
75aa0 33 33 2c 0a 20 20 20 20 32 33 34 2c 32 33 35 2c  33,.    234,235,
75ab0 32 33 36 2c 32 33 37 2c 32 33 38 2c 32 33 39 2c  236,237,238,239,
75ac0 32 34 30 2c 32 34 31 2c 32 34 32 2c 32 34 33 2c  240,241,242,243,
75ad0 32 34 34 2c 32 34 35 2c 32 34 36 2c 32 34 37 2c  244,245,246,247,
75ae0 32 34 38 2c 32 34 39 2c 32 35 30 2c 32 35 31 2c  248,249,250,251,
75af0 0a 20 20 20 20 32 35 32 2c 32 35 33 2c 32 35 34  .    252,253,254
75b00 2c 32 35 35 0a 23 65 6e 64 69 66 0a 23 69 66 64  ,255.#endif.#ifd
75b10 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43  ef SQLITE_EBCDIC
75b20 0a 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20 32  .      0,  1,  2
75b30 2c 20 20 33 2c 20 20 34 2c 20 20 35 2c 20 20 36  ,  3,  4,  5,  6
75b40 2c 20 20 37 2c 20 20 38 2c 20 20 39 2c 20 31 30  ,  7,  8,  9, 10
75b50 2c 20 31 31 2c 20 31 32 2c 20 31 33 2c 20 31 34  , 11, 12, 13, 14
75b60 2c 20 31 35 2c 20 2f 2a 20 30 78 20 2a 2f 0a 20  , 15, /* 0x */. 
75b70 20 20 20 20 31 36 2c 20 31 37 2c 20 31 38 2c 20      16, 17, 18, 
75b80 31 39 2c 20 32 30 2c 20 32 31 2c 20 32 32 2c 20  19, 20, 21, 22, 
75b90 32 33 2c 20 32 34 2c 20 32 35 2c 20 32 36 2c 20  23, 24, 25, 26, 
75ba0 32 37 2c 20 32 38 2c 20 32 39 2c 20 33 30 2c 20  27, 28, 29, 30, 
75bb0 33 31 2c 20 2f 2a 20 31 78 20 2a 2f 0a 20 20 20  31, /* 1x */.   
75bc0 20 20 33 32 2c 20 33 33 2c 20 33 34 2c 20 33 35    32, 33, 34, 35
75bd0 2c 20 33 36 2c 20 33 37 2c 20 33 38 2c 20 33 39  , 36, 37, 38, 39
75be0 2c 20 34 30 2c 20 34 31 2c 20 34 32 2c 20 34 33  , 40, 41, 42, 43
75bf0 2c 20 34 34 2c 20 34 35 2c 20 34 36 2c 20 34 37  , 44, 45, 46, 47
75c00 2c 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 20 20  , /* 2x */.     
75c10 34 38 2c 20 34 39 2c 20 35 30 2c 20 35 31 2c 20  48, 49, 50, 51, 
75c20 35 32 2c 20 35 33 2c 20 35 34 2c 20 35 35 2c 20  52, 53, 54, 55, 
75c30 35 36 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c 20  56, 57, 58, 59, 
75c40 36 30 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c 20  60, 61, 62, 63, 
75c50 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 20 20 36 34  /* 3x */.     64
75c60 2c 20 36 35 2c 20 36 36 2c 20 36 37 2c 20 36 38  , 65, 66, 67, 68
75c70 2c 20 36 39 2c 20 37 30 2c 20 37 31 2c 20 37 32  , 69, 70, 71, 72
75c80 2c 20 37 33 2c 20 37 34 2c 20 37 35 2c 20 37 36  , 73, 74, 75, 76
75c90 2c 20 37 37 2c 20 37 38 2c 20 37 39 2c 20 2f 2a  , 77, 78, 79, /*
75ca0 20 34 78 20 2a 2f 0a 20 20 20 20 20 38 30 2c 20   4x */.     80, 
75cb0 38 31 2c 20 38 32 2c 20 38 33 2c 20 38 34 2c 20  81, 82, 83, 84, 
75cc0 38 35 2c 20 38 36 2c 20 38 37 2c 20 38 38 2c 20  85, 86, 87, 88, 
75cd0 38 39 2c 20 39 30 2c 20 39 31 2c 20 39 32 2c 20  89, 90, 91, 92, 
75ce0 39 33 2c 20 39 34 2c 20 39 35 2c 20 2f 2a 20 35  93, 94, 95, /* 5
75cf0 78 20 2a 2f 0a 20 20 20 20 20 39 36 2c 20 39 37  x */.     96, 97
75d00 2c 20 36 36 2c 20 36 37 2c 20 36 38 2c 20 36 39  , 66, 67, 68, 69
75d10 2c 20 37 30 2c 20 37 31 2c 20 37 32 2c 20 37 33  , 70, 71, 72, 73
75d20 2c 31 30 36 2c 31 30 37 2c 31 30 38 2c 31 30 39  ,106,107,108,109
75d30 2c 31 31 30 2c 31 31 31 2c 20 2f 2a 20 36 78 20  ,110,111, /* 6x 
75d40 2a 2f 0a 20 20 20 20 31 31 32 2c 20 38 31 2c 20  */.    112, 81, 
75d50 38 32 2c 20 38 33 2c 20 38 34 2c 20 38 35 2c 20  82, 83, 84, 85, 
75d60 38 36 2c 20 38 37 2c 20 38 38 2c 20 38 39 2c 31  86, 87, 88, 89,1
75d70 32 32 2c 31 32 33 2c 31 32 34 2c 31 32 35 2c 31  22,123,124,125,1
75d80 32 36 2c 31 32 37 2c 20 2f 2a 20 37 78 20 2a 2f  26,127, /* 7x */
75d90 0a 20 20 20 20 31 32 38 2c 31 32 39 2c 31 33 30  .    128,129,130
75da0 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 34  ,131,132,133,134
75db0 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 31 33 38  ,135,136,137,138
75dc0 2c 31 33 39 2c 31 34 30 2c 31 34 31 2c 31 34 32  ,139,140,141,142
75dd0 2c 31 34 33 2c 20 2f 2a 20 38 78 20 2a 2f 0a 20  ,143, /* 8x */. 
75de0 20 20 20 31 34 34 2c 31 34 35 2c 31 34 36 2c 31     144,145,146,1
75df0 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 31  47,148,149,150,1
75e00 35 31 2c 31 35 32 2c 31 35 33 2c 31 35 34 2c 31  51,152,153,154,1
75e10 35 35 2c 31 35 36 2c 31 35 37 2c 31 35 36 2c 31  55,156,157,156,1
75e20 35 39 2c 20 2f 2a 20 39 78 20 2a 2f 0a 20 20 20  59, /* 9x */.   
75e30 20 31 36 30 2c 31 36 31 2c 31 36 32 2c 31 36 33   160,161,162,163
75e40 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c 31 36 37  ,164,165,166,167
75e50 2c 31 36 38 2c 31 36 39 2c 31 37 30 2c 31 37 31  ,168,169,170,171
75e60 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 37 35  ,140,141,142,175
75e70 2c 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 20 20 31  , /* Ax */.    1
75e80 37 36 2c 31 37 37 2c 31 37 38 2c 31 37 39 2c 31  76,177,178,179,1
75e90 38 30 2c 31 38 31 2c 31 38 32 2c 31 38 33 2c 31  80,181,182,183,1
75ea0 38 34 2c 31 38 35 2c 31 38 36 2c 31 38 37 2c 31  84,185,186,187,1
75eb0 38 38 2c 31 38 39 2c 31 39 30 2c 31 39 31 2c 20  88,189,190,191, 
75ec0 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 20 31 39 32  /* Bx */.    192
75ed0 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 32  ,129,130,131,132
75ee0 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 36  ,133,134,135,136
75ef0 2c 31 33 37 2c 32 30 32 2c 32 30 33 2c 32 30 34  ,137,202,203,204
75f00 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c 20 2f 2a  ,205,206,207, /*
75f10 20 43 78 20 2a 2f 0a 20 20 20 20 32 30 38 2c 31   Cx */.    208,1
75f20 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c 31  45,146,147,148,1
75f30 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c 31  49,150,151,152,1
75f40 35 33 2c 32 31 38 2c 32 31 39 2c 32 32 30 2c 32  53,218,219,220,2
75f50 32 31 2c 32 32 32 2c 32 32 33 2c 20 2f 2a 20 44  21,222,223, /* D
75f60 78 20 2a 2f 0a 20 20 20 20 32 32 34 2c 32 32 35  x */.    224,225
75f70 2c 31 36 32 2c 31 36 33 2c 31 36 34 2c 31 36 35  ,162,163,164,165
75f80 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 31 36 39  ,166,167,168,169
75f90 2c 32 33 32 2c 32 30 33 2c 32 30 34 2c 32 30 35  ,232,203,204,205
75fa0 2c 32 30 36 2c 32 30 37 2c 20 2f 2a 20 45 78 20  ,206,207, /* Ex 
75fb0 2a 2f 0a 20 20 20 20 32 33 39 2c 32 34 30 2c 32  */.    239,240,2
75fc0 34 31 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c 32  41,242,243,244,2
75fd0 34 35 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c 32  45,246,247,248,2
75fe0 34 39 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32  49,219,220,221,2
75ff0 32 32 2c 32 35 35 2c 20 2f 2a 20 46 78 20 2a 2f  22,255, /* Fx */
76000 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a  .#endif.};../*.*
76010 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
76020 32 35 36 20 62 79 74 65 20 6c 6f 6f 6b 75 70 20  256 byte lookup 
76030 74 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f  table is used to
76040 20 73 75 70 70 6f 72 74 20 53 51 4c 69 74 65 73   support SQLites
76050 20 62 75 69 6c 74 2d 69 6e 0a 2a 2a 20 65 71 75   built-in.** equ
76060 69 76 61 6c 65 6e 74 73 20 74 6f 20 74 68 65 20  ivalents to the 
76070 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 6e 64 61  following standa
76080 72 64 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74  rd library funct
76090 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 73  ions:.**.**   is
760a0 73 70 61 63 65 28 29 20 20 20 20 20 20 20 20 20  space()         
760b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
760c0 78 30 31 0a 2a 2a 20 20 20 69 73 61 6c 70 68 61  x01.**   isalpha
760d0 28 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ()              
760e0 20 20 20 20 20 20 20 20 20 20 30 78 30 32 0a 2a            0x02.*
760f0 2a 20 20 20 69 73 64 69 67 69 74 28 29 20 20 20  *   isdigit()   
76100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76110 20 20 20 20 20 30 78 30 34 0a 2a 2a 20 20 20 69       0x04.**   i
76120 73 61 6c 6e 75 6d 28 29 20 20 20 20 20 20 20 20  salnum()        
76130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76140 30 78 30 36 0a 2a 2a 20 20 20 69 73 78 64 69 67  0x06.**   isxdig
76150 69 74 28 29 20 20 20 20 20 20 20 20 20 20 20 20  it()            
76160 20 20 20 20 20 20 20 20 20 20 20 30 78 30 38 0a             0x08.
76170 2a 2a 20 20 20 74 6f 75 70 70 65 72 28 29 20 20  **   toupper()  
76180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76190 20 20 20 20 20 20 30 78 32 30 0a 2a 2a 0a 2a 2a        0x20.**.**
761a0 20 42 69 74 20 30 78 32 30 20 69 73 20 73 65 74   Bit 0x20 is set
761b0 20 69 66 20 74 68 65 20 6d 61 70 70 65 64 20 63   if the mapped c
761c0 68 61 72 61 63 74 65 72 20 72 65 71 75 69 72 65  haracter require
761d0 73 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 74 6f  s translation to
761e0 20 75 70 70 65 72 0a 2a 2a 20 63 61 73 65 2e 20   upper.** case. 
761f0 69 2e 65 2e 20 69 66 20 74 68 65 20 63 68 61 72  i.e. if the char
76200 61 63 74 65 72 20 69 73 20 61 20 6c 6f 77 65 72  acter is a lower
76210 2d 63 61 73 65 20 41 53 43 49 49 20 63 68 61 72  -case ASCII char
76220 61 63 74 65 72 2e 0a 2a 2a 20 49 66 20 78 20 69  acter..** If x i
76230 73 20 61 20 6c 6f 77 65 72 2d 63 61 73 65 20 41  s a lower-case A
76240 53 43 49 49 20 63 68 61 72 61 63 74 65 72 2c 20  SCII character, 
76250 74 68 65 6e 20 69 74 73 20 75 70 70 65 72 2d 63  then its upper-c
76260 61 73 65 20 65 71 75 69 76 61 6c 65 6e 74 0a 2a  ase equivalent.*
76270 2a 20 69 73 20 28 78 20 2d 20 30 78 32 30 29 2e  * is (x - 0x20).
76280 20 54 68 65 72 65 66 6f 72 65 20 74 6f 75 70 70   Therefore toupp
76290 65 72 28 29 20 63 61 6e 20 62 65 20 69 6d 70 6c  er() can be impl
762a0 65 6d 65 6e 74 65 64 20 61 73 3a 0a 2a 2a 0a 2a  emented as:.**.*
762b0 2a 20 20 20 28 78 20 26 20 7e 28 6d 61 70 5b 78  *   (x & ~(map[x
762c0 5d 26 30 78 32 30 29 29 0a 2a 2a 0a 2a 2a 20 53  ]&0x20)).**.** S
762d0 74 61 6e 64 61 72 64 20 66 75 6e 63 74 69 6f 6e  tandard function
762e0 20 74 6f 6c 6f 77 65 72 28 29 20 69 73 20 69 6d   tolower() is im
762f0 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 20  plemented using 
76300 74 68 65 20 73 71 6c 69 74 65 33 55 70 70 65 72  the sqlite3Upper
76310 54 6f 4c 6f 77 65 72 5b 5d 0a 2a 2a 20 61 72 72  ToLower[].** arr
76320 61 79 2e 20 74 6f 6c 6f 77 65 72 28 29 20 69 73  ay. tolower() is
76330 20 75 73 65 64 20 6d 6f 72 65 20 6f 66 74 65 6e   used more often
76340 20 74 68 61 6e 20 74 6f 75 70 70 65 72 28 29 20   than toupper() 
76350 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a  by SQLite..**.**
76360 20 53 51 4c 69 74 65 27 73 20 76 65 72 73 69 6f   SQLite's versio
76370 6e 73 20 61 72 65 20 69 64 65 6e 74 69 63 61 6c  ns are identical
76380 20 74 6f 20 74 68 65 20 73 74 61 6e 64 61 72 64   to the standard
76390 20 76 65 72 73 69 6f 6e 73 20 61 73 73 75 6d 69   versions assumi
763a0 6e 67 20 61 0a 2a 2a 20 6c 6f 63 61 6c 65 20 6f  ng a.** locale o
763b0 66 20 22 43 22 2e 20 54 68 65 79 20 61 72 65 20  f "C". They are 
763c0 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 6d  implemented as m
763d0 61 63 72 6f 73 20 69 6e 20 73 71 6c 69 74 65 49  acros in sqliteI
763e0 6e 74 2e 68 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  nt.h..*/.#ifdef 
763f0 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 53 51 4c  SQLITE_ASCII.SQL
76400 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
76410 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
76420 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b  sqlite3CtypeMap[
76430 32 35 36 5d 20 3d 20 7b 0a 20 20 30 78 30 30 2c  256] = {.  0x00,
76440 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
76450 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
76460 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 30  x00, 0x00,  /* 0
76470 30 2e 2e 30 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e  0..07    .......
76480 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30  . */.  0x00, 0x0
76490 31 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20 30  1, 0x01, 0x01, 0
764a0 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 30 2c  x01, 0x01, 0x00,
764b0 20 30 78 30 30 2c 20 20 2f 2a 20 30 38 2e 2e 30   0x00,  /* 08..0
764c0 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f  f    ........ */
764d0 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  .  0x00, 0x00, 0
764e0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
764f0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
76500 30 2c 20 20 2f 2a 20 31 30 2e 2e 31 37 20 20 20  0,  /* 10..17   
76510 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30   ........ */.  0
76520 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
76530 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
76540 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20  0, 0x00, 0x00,  
76550 2f 2a 20 31 38 2e 2e 31 66 20 20 20 20 2e 2e 2e  /* 18..1f    ...
76560 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 31 2c  ..... */.  0x01,
76570 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
76580 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
76590 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 32  x00, 0x00,  /* 2
765a0 30 2e 2e 32 37 20 20 20 20 20 21 22 23 24 25 26  0..27     !"#$%&
765b0 27 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30  ' */.  0x00, 0x0
765c0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
765d0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
765e0 20 30 78 30 30 2c 20 20 2f 2a 20 32 38 2e 2e 32   0x00,  /* 28..2
765f0 66 20 20 20 20 28 29 2a 2b 2c 2d 2e 2f 20 2a 2f  f    ()*+,-./ */
76600 0a 20 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30  .  0x0c, 0x0c, 0
76610 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c  x0c, 0x0c, 0x0c,
76620 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30   0x0c, 0x0c, 0x0
76630 63 2c 20 20 2f 2a 20 33 30 2e 2e 33 37 20 20 20  c,  /* 30..37   
76640 20 30 31 32 33 34 35 36 37 20 2a 2f 0a 20 20 30   01234567 */.  0
76650 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 30 2c  x0c, 0x0c, 0x00,
76660 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
76670 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20  0, 0x00, 0x00,  
76680 2f 2a 20 33 38 2e 2e 33 66 20 20 20 20 38 39 3a  /* 38..3f    89:
76690 3b 3c 3d 3e 3f 20 2a 2f 0a 0a 20 20 30 78 30 30  ;<=>? */..  0x00
766a0 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20 30 78  , 0x0a, 0x0a, 0x
766b0 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20  0a, 0x0a, 0x0a, 
766c0 30 78 30 61 2c 20 30 78 30 32 2c 20 20 2f 2a 20  0x0a, 0x02,  /* 
766d0 34 30 2e 2e 34 37 20 20 20 20 40 41 42 43 44 45  40..47    @ABCDE
766e0 46 47 20 2a 2f 0a 20 20 30 78 30 32 2c 20 30 78  FG */.  0x02, 0x
766f0 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20  02, 0x02, 0x02, 
76700 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32  0x02, 0x02, 0x02
76710 2c 20 30 78 30 32 2c 20 20 2f 2a 20 34 38 2e 2e  , 0x02,  /* 48..
76720 34 66 20 20 20 20 48 49 4a 4b 4c 4d 4e 4f 20 2a  4f    HIJKLMNO *
76730 2f 0a 20 20 30 78 30 32 2c 20 30 78 30 32 2c 20  /.  0x02, 0x02, 
76740 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32  0x02, 0x02, 0x02
76750 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78  , 0x02, 0x02, 0x
76760 30 32 2c 20 20 2f 2a 20 35 30 2e 2e 35 37 20 20  02,  /* 50..57  
76770 20 20 50 51 52 53 54 55 56 57 20 2a 2f 0a 20 20    PQRSTUVW */.  
76780 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32  0x02, 0x02, 0x02
76790 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
767a0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
767b0 20 2f 2a 20 35 38 2e 2e 35 66 20 20 20 20 58 59   /* 58..5f    XY
767c0 5a 5b 5c 5d 5e 5f 20 2a 2f 0a 20 20 30 78 30 30  Z[\]^_ */.  0x00
767d0 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20 30 78  , 0x2a, 0x2a, 0x
767e0 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20  2a, 0x2a, 0x2a, 
767f0 30 78 32 61 2c 20 30 78 32 32 2c 20 20 2f 2a 20  0x2a, 0x22,  /* 
76800 36 30 2e 2e 36 37 20 20 20 20 60 61 62 63 64 65  60..67    `abcde
76810 66 67 20 2a 2f 0a 20 20 30 78 32 32 2c 20 30 78  fg */.  0x22, 0x
76820 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20  22, 0x22, 0x22, 
76830 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32  0x22, 0x22, 0x22
76840 2c 20 30 78 32 32 2c 20 20 2f 2a 20 36 38 2e 2e  , 0x22,  /* 68..
76850 36 66 20 20 20 20 68 69 6a 6b 6c 6d 6e 6f 20 2a  6f    hijklmno *
76860 2f 0a 20 20 30 78 32 32 2c 20 30 78 32 32 2c 20  /.  0x22, 0x22, 
76870 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32  0x22, 0x22, 0x22
76880 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78  , 0x22, 0x22, 0x
76890 32 32 2c 20 20 2f 2a 20 37 30 2e 2e 37 37 20 20  22,  /* 70..77  
768a0 20 20 70 71 72 73 74 75 76 77 20 2a 2f 0a 20 20    pqrstuvw */.  
768b0 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32  0x22, 0x22, 0x22
768c0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
768d0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
768e0 20 2f 2a 20 37 38 2e 2e 37 66 20 20 20 20 78 79   /* 78..7f    xy
768f0 7a 7b 7c 7d 7e 2e 20 2a 2f 0a 0a 20 20 30 78 30  z{|}~. */..  0x0
76900 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
76910 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
76920 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a   0x00, 0x00,  /*
76930 20 38 30 2e 2e 38 37 20 20 20 20 2e 2e 2e 2e 2e   80..87    .....
76940 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30  ... */.  0x00, 0
76950 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
76960 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
76970 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 38 38 2e  0, 0x00,  /* 88.
76980 2e 38 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20  .8f    ........ 
76990 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c  */.  0x00, 0x00,
769a0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
769b0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
769c0 78 30 30 2c 20 20 2f 2a 20 39 30 2e 2e 39 37 20  x00,  /* 90..97 
769d0 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20     ........ */. 
769e0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
769f0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
76a00 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
76a10 20 20 2f 2a 20 39 38 2e 2e 39 66 20 20 20 20 2e    /* 98..9f    .
76a20 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30  ....... */.  0x0
76a30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
76a40 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
76a50 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a   0x00, 0x00,  /*
76a60 20 61 30 2e 2e 61 37 20 20 20 20 2e 2e 2e 2e 2e   a0..a7    .....
76a70 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30  ... */.  0x00, 0
76a80 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
76a90 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
76aa0 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 61 38 2e  0, 0x00,  /* a8.
76ab0 2e 61 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20  .af    ........ 
76ac0 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c  */.  0x00, 0x00,
76ad0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
76ae0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
76af0 78 30 30 2c 20 20 2f 2a 20 62 30 2e 2e 62 37 20  x00,  /* b0..b7 
76b00 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20     ........ */. 
76b10 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
76b20 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
76b30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
76b40 20 20 2f 2a 20 62 38 2e 2e 62 66 20 20 20 20 2e    /* b8..bf    .
76b50 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 0a 20 20 30 78  ....... */..  0x
76b60 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
76b70 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
76b80 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f  , 0x00, 0x00,  /
76b90 2a 20 63 30 2e 2e 63 37 20 20 20 20 2e 2e 2e 2e  * c0..c7    ....
76ba0 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20  .... */.  0x00, 
76bb0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
76bc0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
76bd0 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 63 38  00, 0x00,  /* c8
76be0 2e 2e 63 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e  ..cf    ........
76bf0 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30   */.  0x00, 0x00
76c00 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
76c10 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
76c20 30 78 30 30 2c 20 20 2f 2a 20 64 30 2e 2e 64 37  0x00,  /* d0..d7
76c30 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a      ........ */.
76c40 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78    0x00, 0x00, 0x
76c50 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
76c60 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
76c70 2c 20 20 2f 2a 20 64 38 2e 2e 64 66 20 20 20 20  ,  /* d8..df    
76c80 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78  ........ */.  0x
76c90 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
76ca0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
76cb0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f  , 0x00, 0x00,  /
76cc0 2a 20 65 30 2e 2e 65 37 20 20 20 20 2e 2e 2e 2e  * e0..e7    ....
76cd0 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20  .... */.  0x00, 
76ce0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
76cf0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
76d00 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 65 38  00, 0x00,  /* e8
76d10 2e 2e 65 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e  ..ef    ........
76d20 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30   */.  0x00, 0x00
76d30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
76d40 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
76d50 30 78 30 30 2c 20 20 2f 2a 20 66 30 2e 2e 66 37  0x00,  /* f0..f7
76d60 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a      ........ */.
76d70 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78    0x00, 0x00, 0x
76d80 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
76d90 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
76da0 20 20 20 2f 2a 20 66 38 2e 2e 66 66 20 20 20 20     /* f8..ff    
76db0 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 7d 3b 0a 23  ........ */.};.#
76dc0 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54  endif..../*.** T
76dd0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 69 6e  he following sin
76de0 67 6c 65 74 6f 6e 20 63 6f 6e 74 61 69 6e 73 20  gleton contains 
76df0 74 68 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69  the global confi
76e00 67 75 72 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20  guration for.** 
76e10 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
76e20 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ry..*/.SQLITE_PR
76e30 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44  IVATE SQLITE_WSD
76e40 20 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43   struct Sqlite3C
76e50 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 43 6f 6e  onfig sqlite3Con
76e60 66 69 67 20 3d 20 7b 0a 20 20 20 53 51 4c 49 54  fig = {.   SQLIT
76e70 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41  E_DEFAULT_MEMSTA
76e80 54 55 53 2c 20 20 2f 2a 20 62 4d 65 6d 73 74 61  TUS,  /* bMemsta
76e90 74 20 2a 2f 0a 20 20 20 31 2c 20 20 20 20 20 20  t */.   1,      
76ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76eb0 20 20 20 2f 2a 20 62 43 6f 72 65 4d 75 74 65 78     /* bCoreMutex
76ec0 20 2a 2f 0a 20 20 20 53 51 4c 49 54 45 5f 54 48   */.   SQLITE_TH
76ed0 52 45 41 44 53 41 46 45 3d 3d 31 2c 20 20 20 20  READSAFE==1,    
76ee0 20 20 2f 2a 20 62 46 75 6c 6c 4d 75 74 65 78 20    /* bFullMutex 
76ef0 2a 2f 0a 20 20 20 30 78 37 66 66 66 66 66 66 65  */.   0x7ffffffe
76f00 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
76f10 20 2f 2a 20 6d 78 53 74 72 6c 65 6e 20 2a 2f 0a   /* mxStrlen */.
76f20 20 20 20 31 30 30 2c 20 20 20 20 20 20 20 20 20     100,         
76f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
76f40 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a   szLookaside */.
76f50 20 20 20 35 30 30 2c 20 20 20 20 20 20 20 20 20     500,         
76f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
76f70 20 6e 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20   nLookaside */. 
76f80 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30    {0,0,0,0,0,0,0
76f90 2c 30 7d 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ,0},         /* 
76fa0 6d 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c 30  m */.   {0,0,0,0
76fb0 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 20  ,0,0,0,0,0},    
76fc0 20 20 20 2f 2a 20 6d 75 74 65 78 20 2a 2f 0a 20     /* mutex */. 
76fd0 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30    {0,0,0,0,0,0,0
76fe0 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 2f 2a 20  ,0,0,0,0},   /* 
76ff0 70 63 61 63 68 65 20 2a 2f 0a 20 20 20 28 76 6f  pcache */.   (vo
77000 69 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20  id*)0,          
77010 20 20 20 20 20 20 20 20 2f 2a 20 70 48 65 61 70          /* pHeap
77020 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20   */.   0,       
77030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77040 20 20 2f 2a 20 6e 48 65 61 70 20 2a 2f 0a 20 20    /* nHeap */.  
77050 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 20 20   0, 0,          
77060 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d              /* m
77070 6e 48 65 61 70 2c 20 6d 78 48 65 61 70 20 2a 2f  nHeap, mxHeap */
77080 0a 20 20 20 28 76 6f 69 64 2a 29 30 2c 20 20 20  .   (void*)0,   
77090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
770a0 2a 20 70 53 63 72 61 74 63 68 20 2a 2f 0a 20 20  * pScratch */.  
770b0 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
770c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
770d0 7a 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 30  zScratch */.   0
770e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
770f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 53 63            /* nSc
77100 72 61 74 63 68 20 2a 2f 0a 20 20 20 28 76 6f 69  ratch */.   (voi
77110 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20 20  d*)0,           
77120 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20         /* pPage 
77130 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20  */.   0,        
77140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77150 20 2f 2a 20 73 7a 50 61 67 65 20 2a 2f 0a 20 20   /* szPage */.  
77160 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
77170 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e              /* n
77180 50 61 67 65 20 2a 2f 0a 20 20 20 30 2c 20 20 20  Page */.   0,   
77190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
771a0 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 72 73 65        /* mxParse
771b0 72 53 74 61 63 6b 20 2a 2f 0a 20 20 20 30 2c 20  rStack */.   0, 
771c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
771d0 20 20 20 20 20 20 20 20 2f 2a 20 73 68 61 72 65          /* share
771e0 64 43 61 63 68 65 45 6e 61 62 6c 65 64 20 2a 2f  dCacheEnabled */
771f0 0a 20 20 20 2f 2a 20 41 6c 6c 20 74 68 65 20 72  .   /* All the r
77200 65 73 74 20 73 68 6f 75 6c 64 20 61 6c 77 61 79  est should alway
77210 73 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64  s be initialized
77220 20 74 6f 20 7a 65 72 6f 20 2a 2f 0a 20 20 20 30   to zero */.   0
77230 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
77240 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 73 49            /* isI
77250 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20  nit */.   0,    
77260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77270 20 20 20 20 20 2f 2a 20 69 6e 50 72 6f 67 72 65       /* inProgre
77280 73 73 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20  ss */.   0,     
77290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
772a0 20 20 20 20 2f 2a 20 69 73 4d 75 74 65 78 49 6e      /* isMutexIn
772b0 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20  it */.   0,     
772c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
772d0 20 20 20 20 2f 2a 20 69 73 4d 61 6c 6c 6f 63 49      /* isMallocI
772e0 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20  nit */.   0,    
772f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77300 20 20 20 20 20 2f 2a 20 69 73 50 43 61 63 68 65       /* isPCache
77310 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20  Init */.   0,   
77320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77330 20 20 20 20 20 20 2f 2a 20 70 49 6e 69 74 4d 75        /* pInitMu
77340 74 65 78 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20  tex */.   0,    
77350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77360 20 20 20 20 20 2f 2a 20 6e 52 65 66 49 6e 69 74       /* nRefInit
77370 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a  Mutex */.};.../*
77380 0a 2a 2a 20 48 61 73 68 20 74 61 62 6c 65 20 66  .** Hash table f
77390 6f 72 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69  or global functi
773a0 6f 6e 73 20 2d 20 66 75 6e 63 74 69 6f 6e 73 20  ons - functions 
773b0 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a  common to all.**
773c0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
773d0 74 69 6f 6e 73 2e 20 20 41 66 74 65 72 20 69 6e  tions.  After in
773e0 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2c 20 74 68  itialization, th
773f0 69 73 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 72  is table is.** r
77400 65 61 64 2d 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c  ead-only..*/.SQL
77410 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49  ITE_PRIVATE SQLI
77420 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 48 61  TE_WSD FuncDefHa
77430 73 68 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  sh sqlite3Global
77440 46 75 6e 63 74 69 6f 6e 73 3b 0a 0a 2f 2a 0a 2a  Functions;../*.*
77450 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74  * The value of t
77460 68 65 20 22 70 65 6e 64 69 6e 67 22 20 62 79 74  he "pending" byt
77470 65 20 6d 75 73 74 20 62 65 20 30 78 34 30 30 30  e must be 0x4000
77480 30 30 30 30 20 28 31 20 62 79 74 65 20 70 61 73  0000 (1 byte pas
77490 74 20 74 68 65 0a 2a 2a 20 31 2d 67 69 62 61 62  t the.** 1-gibab
774a0 79 74 65 20 62 6f 75 6e 64 61 72 79 29 20 69 6e  yte boundary) in
774b0 20 61 20 63 6f 6d 70 61 74 69 62 6c 65 20 64 61   a compatible da
774c0 74 61 62 61 73 65 2e 20 20 53 51 4c 69 74 65 20  tabase.  SQLite 
774d0 6e 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 74 68  never uses.** th
774e0 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20  e database page 
774f0 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
77500 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 2e 20  e pending byte. 
77510 20 49 74 20 6e 65 76 65 72 20 61 74 74 65 6d 70   It never attemp
77520 74 73 0a 2a 2a 20 74 6f 20 72 65 61 64 20 6f 72  ts.** to read or
77530 20 77 72 69 74 65 20 74 68 61 74 20 70 61 67 65   write that page
77540 2e 20 20 54 68 65 20 70 65 6e 64 69 6e 67 20 62  .  The pending b
77550 79 74 65 20 70 61 67 65 20 69 73 20 73 65 74 20  yte page is set 
77560 61 73 73 69 67 6e 0a 2a 2a 20 66 6f 72 20 75 73  assign.** for us
77570 65 20 62 79 20 74 68 65 20 56 46 53 20 6c 61 79  e by the VFS lay
77580 65 72 73 20 61 73 20 73 70 61 63 65 20 66 6f 72  ers as space for
77590 20 6d 61 6e 61 67 69 6e 67 20 66 69 6c 65 20 6c   managing file l
775a0 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 44 75 72 69  ocks..**.** Duri
775b0 6e 67 20 74 65 73 74 69 6e 67 2c 20 69 74 20 69  ng testing, it i
775c0 73 20 6f 66 74 65 6e 20 64 65 73 69 72 61 62 6c  s often desirabl
775d0 65 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 70 65  e to move the pe
775e0 6e 64 69 6e 67 20 62 79 74 65 20 74 6f 0a 2a 2a  nding byte to.**
775f0 20 61 20 64 69 66 66 65 72 65 6e 74 20 70 6f 73   a different pos
77600 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 66 69 6c  ition in the fil
77610 65 2e 20 20 54 68 69 73 20 61 6c 6c 6f 77 73 20  e.  This allows 
77620 63 6f 64 65 20 74 68 61 74 20 68 61 73 20 74 6f  code that has to
77630 0a 2a 2a 20 64 65 61 6c 20 77 69 74 68 20 74 68  .** deal with th
77640 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20 74  e pending byte t
77650 6f 20 72 75 6e 20 6f 6e 20 66 69 6c 65 73 20 74  o run on files t
77660 68 61 74 20 61 72 65 20 6d 75 63 68 20 73 6d 61  hat are much sma
77670 6c 6c 65 72 0a 2a 2a 20 74 68 61 6e 20 31 20 47  ller.** than 1 G
77680 69 42 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  iB.  The sqlite3
77690 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20  _test_control() 
776a0 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62 65  interface can be
776b0 20 75 73 65 64 20 74 6f 0a 2a 2a 20 6d 6f 76 65   used to.** move
776c0 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74   the pending byt
776d0 65 2e 0a 2a 2a 0a 2a 2a 20 49 4d 50 4f 52 54 41  e..**.** IMPORTA
776e0 4e 54 3a 20 20 43 68 61 6e 67 69 6e 67 20 74 68  NT:  Changing th
776f0 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20 74  e pending byte t
77700 6f 20 61 6e 79 20 76 61 6c 75 65 20 6f 74 68 65  o any value othe
77710 72 20 74 68 61 6e 0a 2a 2a 20 30 78 34 30 30 30  r than.** 0x4000
77720 30 30 30 30 20 72 65 73 75 6c 74 73 20 69 6e 20  0000 results in 
77730 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20  an incompatible 
77740 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f  database file fo
77750 72 6d 61 74 21 0a 2a 2a 20 43 68 61 6e 67 69 6e  rmat!.** Changin
77760 67 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79  g the pending by
77770 74 65 20 64 75 72 69 6e 67 20 6f 70 65 72 61 74  te during operat
77780 69 6e 67 20 72 65 73 75 6c 74 73 20 69 6e 20 75  ing results in u
77790 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 61 6e 64 20  ndefined.** and 
777a0 64 69 6c 65 74 65 72 69 6f 75 73 20 62 65 68 61  dileterious beha
777b0 76 69 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  vior..*/.SQLITE_
777c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
777d0 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 20 3d  te3PendingByte =
777e0 20 30 78 34 30 30 30 30 30 30 30 3b 0a 0a 2f 2a   0x40000000;../*
777f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
77800 64 20 6f 66 20 67 6c 6f 62 61 6c 2e 63 20 2a 2a  d of global.c **
77810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
77820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
77830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
77840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
77850 67 69 6e 20 66 69 6c 65 20 73 74 61 74 75 73 2e  gin file status.
77860 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
77870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
77880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
77890 0a 2a 2a 20 32 30 30 38 20 4a 75 6e 65 20 31 38  .** 2008 June 18
778a0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
778b0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
778c0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
778d0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
778e0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
778f0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
77900 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
77910 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
77920 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
77930 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
77940 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
77950 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
77960 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
77970 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
77980 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
77990 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
779a0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
779b0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
779c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
779d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
779e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
779f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
77a00 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75 6c  **.** This modul
77a10 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  e implements the
77a20 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28   sqlite3_status(
77a30 29 20 69 6e 74 65 72 66 61 63 65 20 61 6e 64 20  ) interface and 
77a40 72 65 6c 61 74 65 64 0a 2a 2a 20 66 75 6e 63 74  related.** funct
77a50 69 6f 6e 61 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20  ionality..**.** 
77a60 24 49 64 3a 20 73 74 61 74 75 73 2e 63 2c 76 20  $Id: status.c,v 
77a70 31 2e 39 20 32 30 30 38 2f 30 39 2f 30 32 20 30  1.9 2008/09/02 0
77a80 30 3a 35 32 3a 35 32 20 64 72 68 20 45 78 70 20  0:52:52 drh Exp 
77a90 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 72 69  $.*/../*.** Vari
77aa0 61 62 6c 65 73 20 69 6e 20 77 68 69 63 68 20 74  ables in which t
77ab0 6f 20 72 65 63 6f 72 64 20 73 74 61 74 75 73 20  o record status 
77ac0 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  information..*/.
77ad0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
77ae0 71 6c 69 74 65 33 53 74 61 74 54 79 70 65 20 73  qlite3StatType s
77af0 71 6c 69 74 65 33 53 74 61 74 54 79 70 65 3b 0a  qlite3StatType;.
77b00 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53  static SQLITE_WS
77b10 44 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  D struct sqlite3
77b20 53 74 61 74 54 79 70 65 20 7b 0a 20 20 69 6e 74  StatType {.  int
77b30 20 6e 6f 77 56 61 6c 75 65 5b 39 5d 3b 20 20 20   nowValue[9];   
77b40 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
77b50 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20   value */.  int 
77b60 6d 78 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20  mxValue[9];     
77b70 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20       /* Maximum 
77b80 76 61 6c 75 65 20 2a 2f 0a 7d 20 73 71 6c 69 74  value */.} sqlit
77b90 65 33 53 74 61 74 20 3d 20 7b 20 7b 30 2c 7d 2c  e3Stat = { {0,},
77ba0 20 7b 30 2c 7d 20 7d 3b 0a 0a 0a 2f 2a 20 54 68   {0,} };.../* Th
77bb0 65 20 22 77 73 64 53 74 61 74 22 20 6d 61 63 72  e "wsdStat" macr
77bc0 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74  o will resolve t
77bd0 6f 20 74 68 65 20 73 74 61 74 75 73 20 69 6e 66  o the status inf
77be0 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 74 61 74  ormation.** stat
77bf0 65 20 76 65 63 74 6f 72 2e 20 20 49 66 20 77 72  e vector.  If wr
77c00 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61  itable static da
77c10 74 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65  ta is unsupporte
77c20 64 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c  d on the target,
77c30 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c  .** we have to l
77c40 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 65 20  ocate the state 
77c50 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69  vector at run-ti
77c60 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65  me.  In the more
77c70 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20   common.** case 
77c80 77 68 65 72 65 20 77 72 69 74 61 62 6c 65 20 73  where writable s
77c90 74 61 74 69 63 20 64 61 74 61 20 69 73 20 73 75  tatic data is su
77ca0 70 70 6f 72 74 65 64 2c 20 77 73 64 53 74 61 74  pported, wsdStat
77cb0 20 63 61 6e 20 72 65 66 65 72 20 64 69 72 65 63   can refer direc
77cc0 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22 73  tly.** to the "s
77cd0 71 6c 69 74 65 33 53 74 61 74 22 20 73 74 61 74  qlite3Stat" stat
77ce0 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65  e vector declare
77cf0 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64  d above..*/.#ifd
77d00 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57  ef SQLITE_OMIT_W
77d10 53 44 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53  SD.# define wsdS
77d20 74 61 74 49 6e 69 74 20 20 73 71 6c 69 74 65 33  tatInit  sqlite3
77d30 53 74 61 74 54 79 70 65 20 2a 78 20 3d 20 26 47  StatType *x = &G
77d40 4c 4f 42 41 4c 28 73 71 6c 69 74 65 33 53 74 61  LOBAL(sqlite3Sta
77d50 74 54 79 70 65 2c 73 71 6c 69 74 65 33 53 74 61  tType,sqlite3Sta
77d60 74 29 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53  t).# define wsdS
77d70 74 61 74 20 78 5b 30 5d 0a 23 65 6c 73 65 0a 23  tat x[0].#else.#
77d80 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 49   define wsdStatI
77d90 6e 69 74 0a 23 20 64 65 66 69 6e 65 20 77 73 64  nit.# define wsd
77da0 53 74 61 74 20 73 71 6c 69 74 65 33 53 74 61 74  Stat sqlite3Stat
77db0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52  .#endif../*.** R
77dc0 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e  eturn the curren
77dd0 74 20 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61  t value of a sta
77de0 74 75 73 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a  tus parameter..*
77df0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
77e00 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 61 74   int sqlite3Stat
77e10 75 73 56 61 6c 75 65 28 69 6e 74 20 6f 70 29 7b  usValue(int op){
77e20 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a  .  wsdStatInit;.
77e30 20 20 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 20    assert( op>=0 
77e40 26 26 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 28  && op<ArraySize(
77e50 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65  wsdStat.nowValue
77e60 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 77 73  ) );.  return ws
77e70 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f  dStat.nowValue[o
77e80 70 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  p];.}../*.** Add
77e90 20 4e 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20   N to the value 
77ea0 6f 66 20 61 20 73 74 61 74 75 73 20 72 65 63 6f  of a status reco
77eb0 72 64 2e 20 20 49 74 20 69 73 20 61 73 73 75 6d  rd.  It is assum
77ec0 65 64 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 63  ed that the.** c
77ed0 61 6c 6c 65 72 20 68 6f 6c 64 73 20 61 70 70 72  aller holds appr
77ee0 6f 70 72 69 61 74 65 20 6c 6f 63 6b 73 2e 0a 2a  opriate locks..*
77ef0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
77f00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61   void sqlite3Sta
77f10 74 75 73 41 64 64 28 69 6e 74 20 6f 70 2c 20 69  tusAdd(int op, i
77f20 6e 74 20 4e 29 7b 0a 20 20 77 73 64 53 74 61 74  nt N){.  wsdStat
77f30 49 6e 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20  Init;.  assert( 
77f40 6f 70 3e 3d 30 20 26 26 20 6f 70 3c 41 72 72 61  op>=0 && op<Arra
77f50 79 53 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f  ySize(wsdStat.no
77f60 77 56 61 6c 75 65 29 20 29 3b 0a 20 20 77 73 64  wValue) );.  wsd
77f70 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70  Stat.nowValue[op
77f80 5d 20 2b 3d 20 4e 3b 0a 20 20 69 66 28 20 77 73  ] += N;.  if( ws
77f90 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f  dStat.nowValue[o
77fa0 70 5d 3e 77 73 64 53 74 61 74 2e 6d 78 56 61 6c  p]>wsdStat.mxVal
77fb0 75 65 5b 6f 70 5d 20 29 7b 0a 20 20 20 20 77 73  ue[op] ){.    ws
77fc0 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70  dStat.mxValue[op
77fd0 5d 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 56  ] = wsdStat.nowV
77fe0 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a  alue[op];.  }.}.
77ff0 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76  ./*.** Set the v
78000 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 73  alue of a status
78010 20 74 6f 20 58 2e 0a 2a 2f 0a 53 51 4c 49 54 45   to X..*/.SQLITE
78020 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
78030 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28 69  lite3StatusSet(i
78040 6e 74 20 6f 70 2c 20 69 6e 74 20 58 29 7b 0a 20  nt op, int X){. 
78050 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20   wsdStatInit;.  
78060 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26  assert( op>=0 &&
78070 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73   op<ArraySize(ws
78080 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20  dStat.nowValue) 
78090 29 3b 0a 20 20 77 73 64 53 74 61 74 2e 6e 6f 77  );.  wsdStat.now
780a0 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 58 3b 0a 20  Value[op] = X;. 
780b0 20 69 66 28 20 77 73 64 53 74 61 74 2e 6e 6f 77   if( wsdStat.now
780c0 56 61 6c 75 65 5b 6f 70 5d 3e 77 73 64 53 74 61  Value[op]>wsdSta
780d0 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 29 7b  t.mxValue[op] ){
780e0 0a 20 20 20 20 77 73 64 53 74 61 74 2e 6d 78 56  .    wsdStat.mxV
780f0 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74  alue[op] = wsdSt
78100 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b  at.nowValue[op];
78110 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75  .  }.}../*.** Qu
78120 65 72 79 20 73 74 61 74 75 73 20 69 6e 66 6f 72  ery status infor
78130 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  mation..**.** Th
78140 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  is implementatio
78150 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 72  n assumes that r
78160 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e  eading or writin
78170 67 20 61 6e 20 61 6c 69 67 6e 65 64 0a 2a 2a 20  g an aligned.** 
78180 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69  32-bit integer i
78190 73 20 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65 72  s an atomic oper
781a0 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 61 74 20  ation.  If that 
781b0 61 73 73 75 6d 70 74 69 6f 6e 20 69 73 20 6e 6f  assumption is no
781c0 74 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20  t true,.** then 
781d0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
781e0 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 2e 0a  not threadsafe..
781f0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
78200 74 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73  t sqlite3_status
78210 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43  (int op, int *pC
78220 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69  urrent, int *pHi
78230 67 68 77 61 74 65 72 2c 20 69 6e 74 20 72 65 73  ghwater, int res
78240 65 74 46 6c 61 67 29 7b 0a 20 20 77 73 64 53 74  etFlag){.  wsdSt
78250 61 74 49 6e 69 74 3b 0a 20 20 69 66 28 20 6f 70  atInit;.  if( op
78260 3c 30 20 7c 7c 20 6f 70 3e 3d 41 72 72 61 79 53  <0 || op>=ArrayS
78270 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56  ize(wsdStat.nowV
78280 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 72 65 74  alue) ){.    ret
78290 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
782a0 45 3b 0a 20 20 7d 0a 20 20 2a 70 43 75 72 72 65  E;.  }.  *pCurre
782b0 6e 74 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77  nt = wsdStat.now
782c0 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 2a 70 48  Value[op];.  *pH
782d0 69 67 68 77 61 74 65 72 20 3d 20 77 73 64 53 74  ighwater = wsdSt
782e0 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 3b 0a  at.mxValue[op];.
782f0 20 20 69 66 28 20 72 65 73 65 74 46 6c 61 67 20    if( resetFlag 
78300 29 7b 0a 20 20 20 20 77 73 64 53 74 61 74 2e 6d  ){.    wsdStat.m
78310 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64  xValue[op] = wsd
78320 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70  Stat.nowValue[op
78330 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  ];.  }.  return 
78340 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
78350 0a 2a 2a 20 51 75 65 72 79 20 73 74 61 74 75 73  .** Query status
78360 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72   information for
78370 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61   a single databa
78380 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2f  se connection.*/
78390 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
783a0 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75  sqlite3_db_statu
783b0 73 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  s(.  sqlite3 *db
783c0 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
783d0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
783e0 63 74 69 6f 6e 20 77 68 6f 73 65 20 73 74 61 74  ction whose stat
783f0 75 73 20 69 73 20 64 65 73 69 72 65 64 20 2a 2f  us is desired */
78400 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20  .  int op,      
78410 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74           /* Stat
78420 75 73 20 76 65 72 62 20 2a 2f 0a 20 20 69 6e 74  us verb */.  int
78430 20 2a 70 43 75 72 72 65 6e 74 2c 20 20 20 20 20   *pCurrent,     
78440 20 20 20 2f 2a 20 57 72 69 74 65 20 63 75 72 72     /* Write curr
78450 65 6e 74 20 76 61 6c 75 65 20 68 65 72 65 20 2a  ent value here *
78460 2f 0a 20 20 69 6e 74 20 2a 70 48 69 67 68 77 61  /.  int *pHighwa
78470 74 65 72 2c 20 20 20 20 20 20 2f 2a 20 57 72 69  ter,      /* Wri
78480 74 65 20 68 69 67 68 2d 77 61 74 65 72 20 6d 61  te high-water ma
78490 72 6b 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  rk here */.  int
784a0 20 72 65 73 65 74 46 6c 61 67 20 20 20 20 20 20   resetFlag      
784b0 20 20 20 2f 2a 20 52 65 73 65 74 20 68 69 67 68     /* Reset high
784c0 2d 77 61 74 65 72 20 6d 61 72 6b 20 69 66 20 74  -water mark if t
784d0 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 73 77 69 74  rue */.){.  swit
784e0 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61  ch( op ){.    ca
784f0 73 65 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54  se SQLITE_DBSTAT
78500 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45  US_LOOKASIDE_USE
78510 44 3a 20 7b 0a 20 20 20 20 20 20 2a 70 43 75 72  D: {.      *pCur
78520 72 65 6e 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61  rent = db->looka
78530 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20  side.nOut;.     
78540 20 2a 70 48 69 67 68 77 61 74 65 72 20 3d 20 64   *pHighwater = d
78550 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f  b->lookaside.mxO
78560 75 74 3b 0a 20 20 20 20 20 20 69 66 28 20 72 65  ut;.      if( re
78570 73 65 74 46 6c 61 67 20 29 7b 0a 20 20 20 20 20  setFlag ){.     
78580 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65     db->lookaside
78590 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f 6f  .mxOut = db->loo
785a0 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20  kaside.nOut;.   
785b0 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
785c0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
785d0 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 65 74  ult: {.      ret
785e0 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
785f0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
78600 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
78610 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
78620 2a 2a 20 45 6e 64 20 6f 66 20 73 74 61 74 75 73  ** End of status
78630 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
78640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78660 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
78670 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 64 61  ** Begin file da
78680 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  te.c ***********
78690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
786a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
786b0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4f 63  */./*.** 2003 Oc
786c0 74 6f 62 65 72 20 33 31 0a 2a 2a 0a 2a 2a 20 54  tober 31.**.** T
786d0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
786e0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
786f0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
78700 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
78710 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
78720 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
78730 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
78740 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
78750 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
78760 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
78770 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
78780 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
78790 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
787a0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
787b0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
787c0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
787d0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
787e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
787f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78820 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
78830 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  file contains th
78840 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68  e C functions th
78850 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 64 61 74  at implement dat
78860 65 20 61 6e 64 20 74 69 6d 65 0a 2a 2a 20 66 75  e and time.** fu
78870 6e 63 74 69 6f 6e 73 20 66 6f 72 20 53 51 4c 69  nctions for SQLi
78880 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 72  te.  .**.** Ther
78890 65 20 69 73 20 6f 6e 6c 79 20 6f 6e 65 20 65 78  e is only one ex
788a0 70 6f 72 74 65 64 20 73 79 6d 62 6f 6c 20 69 6e  ported symbol in
788b0 20 74 68 69 73 20 66 69 6c 65 20 2d 20 74 68 65   this file - the
788c0 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71 6c   function.** sql
788d0 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74 65  ite3RegisterDate
788e0 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 29 20  TimeFunctions() 
788f0 66 6f 75 6e 64 20 61 74 20 74 68 65 20 62 6f 74  found at the bot
78900 74 6f 6d 20 6f 66 20 74 68 65 20 66 69 6c 65 2e  tom of the file.
78910 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 63 6f  .** All other co
78920 64 65 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70  de has file scop
78930 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 64 61  e..**.** $Id: da
78940 74 65 2e 63 2c 76 20 31 2e 31 30 37 20 32 30 30  te.c,v 1.107 200
78950 39 2f 30 35 2f 30 33 20 32 30 3a 32 33 3a 35 33  9/05/03 20:23:53
78960 20 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a   drh Exp $.**.**
78970 20 53 51 4c 69 74 65 20 70 72 6f 63 65 73 73 65   SQLite processe
78980 73 20 61 6c 6c 20 74 69 6d 65 73 20 61 6e 64 20  s all times and 
78990 64 61 74 65 73 20 61 73 20 4a 75 6c 69 61 6e 20  dates as Julian 
789a0 44 61 79 20 6e 75 6d 62 65 72 73 2e 20 20 54 68  Day numbers.  Th
789b0 65 0a 2a 2a 20 64 61 74 65 73 20 61 6e 64 20 74  e.** dates and t
789c0 69 6d 65 73 20 61 72 65 20 73 74 6f 72 65 64 20  imes are stored 
789d0 61 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  as the number of
789e0 20 64 61 79 73 20 73 69 6e 63 65 20 6e 6f 6f 6e   days since noon
789f0 0a 2a 2a 20 69 6e 20 47 72 65 65 6e 77 69 63 68  .** in Greenwich
78a00 20 6f 6e 20 4e 6f 76 65 6d 62 65 72 20 32 34 2c   on November 24,
78a10 20 34 37 31 34 20 42 2e 43 2e 20 61 63 63 6f 72   4714 B.C. accor
78a20 64 69 6e 67 20 74 6f 20 74 68 65 20 47 72 65 67  ding to the Greg
78a30 6f 72 69 61 6e 0a 2a 2a 20 63 61 6c 65 6e 64 61  orian.** calenda
78a40 72 20 73 79 73 74 65 6d 2e 20 0a 2a 2a 0a 2a 2a  r system. .**.**
78a50 20 31 39 37 30 2d 30 31 2d 30 31 20 30 30 3a 30   1970-01-01 00:0
78a60 30 3a 30 30 20 69 73 20 4a 44 20 32 34 34 30 35  0:00 is JD 24405
78a70 38 37 2e 35 0a 2a 2a 20 32 30 30 30 2d 30 31 2d  87.5.** 2000-01-
78a80 30 31 20 30 30 3a 30 30 3a 30 30 20 69 73 20 4a  01 00:00:00 is J
78a90 44 20 32 34 35 31 35 34 34 2e 35 0a 2a 2a 0a 2a  D 2451544.5.**.*
78aa0 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74  * This implement
78ab0 69 6f 6e 20 72 65 71 75 69 72 65 73 20 79 65 61  ion requires yea
78ac0 72 73 20 74 6f 20 62 65 20 65 78 70 72 65 73 73  rs to be express
78ad0 65 64 20 61 73 20 61 20 34 2d 64 69 67 69 74 20  ed as a 4-digit 
78ae0 6e 75 6d 62 65 72 0a 2a 2a 20 77 68 69 63 68 20  number.** which 
78af0 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 6c 79 20  means that only 
78b00 64 61 74 65 73 20 62 65 74 77 65 65 6e 20 30 30  dates between 00
78b10 30 30 2d 30 31 2d 30 31 20 61 6e 64 20 39 39 39  00-01-01 and 999
78b20 39 2d 31 32 2d 33 31 20 63 61 6e 0a 2a 2a 20 62  9-12-31 can.** b
78b30 65 20 72 65 70 72 65 73 65 6e 74 65 64 2c 20 65  e represented, e
78b40 76 65 6e 20 74 68 6f 75 67 68 20 6a 75 6c 69 61  ven though julia
78b50 6e 20 64 61 79 20 6e 75 6d 62 65 72 73 20 61 6c  n day numbers al
78b60 6c 6f 77 20 61 20 6d 75 63 68 20 77 69 64 65 72  low a much wider
78b70 0a 2a 2a 20 72 61 6e 67 65 20 6f 66 20 64 61 74  .** range of dat
78b80 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 47 72  es..**.** The Gr
78b90 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 72  egorian calendar
78ba0 20 73 79 73 74 65 6d 20 69 73 20 75 73 65 64 20   system is used 
78bb0 66 6f 72 20 61 6c 6c 20 64 61 74 65 73 20 61 6e  for all dates an
78bc0 64 20 74 69 6d 65 73 2c 0a 2a 2a 20 65 76 65 6e  d times,.** even
78bd0 20 74 68 6f 73 65 20 74 68 61 74 20 70 72 65 64   those that pred
78be0 61 74 65 20 74 68 65 20 47 72 65 67 6f 72 69 61  ate the Gregoria
78bf0 6e 20 63 61 6c 65 6e 64 61 72 2e 20 20 48 69 73  n calendar.  His
78c00 74 6f 72 69 61 6e 73 20 75 73 75 61 6c 6c 79 0a  torians usually.
78c10 2a 2a 20 75 73 65 20 74 68 65 20 4a 75 6c 69 61  ** use the Julia
78c20 6e 20 63 61 6c 65 6e 64 61 72 20 66 6f 72 20 64  n calendar for d
78c30 61 74 65 73 20 70 72 69 6f 72 20 74 6f 20 31 35  ates prior to 15
78c40 38 32 2d 31 30 2d 31 35 20 61 6e 64 20 66 6f 72  82-10-15 and for
78c50 20 73 6f 6d 65 0a 2a 2a 20 64 61 74 65 73 20 61   some.** dates a
78c60 66 74 65 72 77 61 72 64 73 2c 20 64 65 70 65 6e  fterwards, depen
78c70 64 69 6e 67 20 6f 6e 20 6c 6f 63 61 6c 65 2e 20  ding on locale. 
78c80 20 42 65 77 61 72 65 20 6f 66 20 74 68 69 73 20   Beware of this 
78c90 64 69 66 66 65 72 65 6e 63 65 2e 0a 2a 2a 0a 2a  difference..**.*
78ca0 2a 20 54 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  * The conversion
78cb0 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72 65 20   algorithms are 
78cc0 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 61 73 65  implemented base
78cd0 64 20 6f 6e 20 64 65 73 63 72 69 70 74 69 6f 6e  d on description
78ce0 73 0a 2a 2a 20 69 6e 20 74 68 65 20 66 6f 6c 6c  s.** in the foll
78cf0 6f 77 69 6e 67 20 74 65 78 74 3a 0a 2a 2a 0a 2a  owing text:.**.*
78d00 2a 20 20 20 20 20 20 4a 65 61 6e 20 4d 65 65 75  *      Jean Meeu
78d10 73 0a 2a 2a 20 20 20 20 20 20 41 73 74 72 6f 6e  s.**      Astron
78d20 6f 6d 69 63 61 6c 20 41 6c 67 6f 72 69 74 68 6d  omical Algorithm
78d30 73 2c 20 32 6e 64 20 45 64 69 74 69 6f 6e 2c 20  s, 2nd Edition, 
78d40 31 39 39 38 0a 2a 2a 20 20 20 20 20 20 49 53 42  1998.**      ISB
78d50 4d 20 30 2d 39 34 33 33 39 36 2d 36 31 2d 31 0a  M 0-943396-61-1.
78d60 2a 2a 20 20 20 20 20 20 57 69 6c 6c 6d 61 6e 6e  **      Willmann
78d70 2d 42 65 6c 6c 2c 20 49 6e 63 0a 2a 2a 20 20 20  -Bell, Inc.**   
78d80 20 20 20 52 69 63 68 6d 6f 6e 64 2c 20 56 69 72     Richmond, Vir
78d90 67 69 6e 69 61 20 28 55 53 41 29 0a 2a 2f 0a 23  ginia (USA).*/.#
78da0 69 6e 63 6c 75 64 65 20 3c 74 69 6d 65 2e 68 3e  include <time.h>
78db0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
78dc0 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46  _OMIT_DATETIME_F
78dd0 55 4e 43 53 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 72  UNCS../*.** On r
78de0 65 63 65 6e 74 20 57 69 6e 64 6f 77 73 20 70 6c  ecent Windows pl
78df0 61 74 66 6f 72 6d 73 2c 20 74 68 65 20 6c 6f 63  atforms, the loc
78e00 61 6c 74 69 6d 65 5f 73 28 29 20 66 75 6e 63 74  altime_s() funct
78e10 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65  ion is available
78e20 0a 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20 74  .** as part of t
78e30 68 65 20 22 53 65 63 75 72 65 20 43 52 54 22 2e  he "Secure CRT".
78e40 20 49 74 20 69 73 20 65 73 73 65 6e 74 69 61 6c   It is essential
78e50 6c 79 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f  ly equivalent to
78e60 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f 72   .** localtime_r
78e70 28 29 20 61 76 61 69 6c 61 62 6c 65 20 75 6e 64  () available und
78e80 65 72 20 6d 6f 73 74 20 50 4f 53 49 58 20 70 6c  er most POSIX pl
78e90 61 74 66 6f 72 6d 73 2c 20 65 78 63 65 70 74 20  atforms, except 
78ea0 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 6f 72 64  that the .** ord
78eb0 65 72 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65  er of the parame
78ec0 74 65 72 73 20 69 73 20 72 65 76 65 72 73 65 64  ters is reversed
78ed0 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 68 74 74 70  ..**.** See http
78ee0 3a 2f 2f 6d 73 64 6e 2e 6d 69 63 72 6f 73 6f 66  ://msdn.microsof
78ef0 74 2e 63 6f 6d 2f 65 6e 2d 75 73 2f 6c 69 62 72  t.com/en-us/libr
78f00 61 72 79 2f 61 34 34 32 78 33 79 65 28 56 53 2e  ary/a442x3ye(VS.
78f10 38 30 29 2e 61 73 70 78 2e 0a 2a 2a 0a 2a 2a 20  80).aspx..**.** 
78f20 49 66 20 74 68 65 20 75 73 65 72 20 68 61 73 20  If the user has 
78f30 6e 6f 74 20 69 6e 64 69 63 61 74 65 64 20 74 6f  not indicated to
78f40 20 75 73 65 20 6c 6f 63 61 6c 74 69 6d 65 5f 72   use localtime_r
78f50 28 29 20 6f 72 20 6c 6f 63 61 6c 74 69 6d 65 5f  () or localtime_
78f60 73 28 29 0a 2a 2a 20 61 6c 72 65 61 64 79 2c 20  s().** already, 
78f70 63 68 65 63 6b 20 66 6f 72 20 61 6e 20 4d 53 56  check for an MSV
78f80 43 20 62 75 69 6c 64 20 65 6e 76 69 72 6f 6e 6d  C build environm
78f90 65 6e 74 20 74 68 61 74 20 70 72 6f 76 69 64 65  ent that provide
78fa0 73 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f  s .** localtime_
78fb0 73 28 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66  s()..*/.#if !def
78fc0 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54  ined(HAVE_LOCALT
78fd0 49 4d 45 5f 52 29 20 26 26 20 21 64 65 66 69 6e  IME_R) && !defin
78fe0 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d  ed(HAVE_LOCALTIM
78ff0 45 5f 53 29 20 26 26 20 5c 0a 20 20 20 20 20 64  E_S) && \.     d
79000 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29  efined(_MSC_VER)
79010 20 26 26 20 64 65 66 69 6e 65 64 28 5f 43 52 54   && defined(_CRT
79020 5f 49 4e 53 45 43 55 52 45 5f 44 45 50 52 45 43  _INSECURE_DEPREC
79030 41 54 45 29 0a 23 64 65 66 69 6e 65 20 48 41 56  ATE).#define HAV
79040 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 20 31 0a  E_LOCALTIME_S 1.
79050 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 20  #endif../*.** A 
79060 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 68 6f  structure for ho
79070 6c 64 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 64  lding a single d
79080 61 74 65 20 61 6e 64 20 74 69 6d 65 2e 0a 2a 2f  ate and time..*/
79090 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
790a0 44 61 74 65 54 69 6d 65 20 44 61 74 65 54 69 6d  DateTime DateTim
790b0 65 3b 0a 73 74 72 75 63 74 20 44 61 74 65 54 69  e;.struct DateTi
790c0 6d 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69  me {.  sqlite3_i
790d0 6e 74 36 34 20 69 4a 44 3b 20 2f 2a 20 54 68 65  nt64 iJD; /* The
790e0 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62   julian day numb
790f0 65 72 20 74 69 6d 65 73 20 38 36 34 30 30 30 30  er times 8640000
79100 30 20 2a 2f 0a 20 20 69 6e 74 20 59 2c 20 4d 2c  0 */.  int Y, M,
79110 20 44 3b 20 20 20 20 20 20 20 2f 2a 20 59 65 61   D;       /* Yea
79120 72 2c 20 6d 6f 6e 74 68 2c 20 61 6e 64 20 64 61  r, month, and da
79130 79 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 6d 3b  y */.  int h, m;
79140 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 75            /* Hou
79150 72 20 61 6e 64 20 6d 69 6e 75 74 65 73 20 2a 2f  r and minutes */
79160 0a 20 20 69 6e 74 20 74 7a 3b 20 20 20 20 20 20  .  int tz;      
79170 20 20 20 20 20 20 2f 2a 20 54 69 6d 65 7a 6f 6e        /* Timezon
79180 65 20 6f 66 66 73 65 74 20 69 6e 20 6d 69 6e 75  e offset in minu
79190 74 65 73 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20  tes */.  double 
791a0 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  s;          /* S
791b0 65 63 6f 6e 64 73 20 2a 2f 0a 20 20 63 68 61 72  econds */.  char
791c0 20 76 61 6c 69 64 59 4d 44 3b 20 20 20 20 20 2f   validYMD;     /
791d0 2a 20 54 72 75 65 20 28 31 29 20 69 66 20 59 2c  * True (1) if Y,
791e0 4d 2c 44 20 61 72 65 20 76 61 6c 69 64 20 2a 2f  M,D are valid */
791f0 0a 20 20 63 68 61 72 20 76 61 6c 69 64 48 4d 53  .  char validHMS
79200 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 31  ;     /* True (1
79210 29 20 69 66 20 68 2c 6d 2c 73 20 61 72 65 20 76  ) if h,m,s are v
79220 61 6c 69 64 20 2a 2f 0a 20 20 63 68 61 72 20 76  alid */.  char v
79230 61 6c 69 64 4a 44 3b 20 20 20 20 20 20 2f 2a 20  alidJD;      /* 
79240 54 72 75 65 20 28 31 29 20 69 66 20 69 4a 44 20  True (1) if iJD 
79250 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68  is valid */.  ch
79260 61 72 20 76 61 6c 69 64 54 5a 3b 20 20 20 20 20  ar validTZ;     
79270 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66 20   /* True (1) if 
79280 74 7a 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 7d  tz is valid */.}
79290 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  ;.../*.** Conver
792a0 74 20 7a 44 61 74 65 20 69 6e 74 6f 20 6f 6e 65  t zDate into one
792b0 20 6f 72 20 6d 6f 72 65 20 69 6e 74 65 67 65 72   or more integer
792c0 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 61  s.  Additional a
792d0 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 63 6f 6d 65  rguments.** come
792e0 20 69 6e 20 67 72 6f 75 70 73 20 6f 66 20 35 20   in groups of 5 
792f0 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
79300 2a 20 20 20 20 20 20 20 4e 20 20 20 20 20 20 20  *       N       
79310 6e 75 6d 62 65 72 20 6f 66 20 64 69 67 69 74 73  number of digits
79320 20 69 6e 20 74 68 65 20 69 6e 74 65 67 65 72 0a   in the integer.
79330 2a 2a 20 20 20 20 20 20 20 6d 69 6e 20 20 20 20  **       min    
79340 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 65 64   minimum allowed
79350 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e   value of the in
79360 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6d  teger.**       m
79370 61 78 20 20 20 20 20 6d 61 78 69 6d 75 6d 20 61  ax     maximum a
79380 6c 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 20  llowed value of 
79390 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20  the integer.**  
793a0 20 20 20 20 20 6e 65 78 74 43 20 20 20 66 69 72       nextC   fir
793b0 73 74 20 63 68 61 72 61 63 74 65 72 20 61 66 74  st character aft
793c0 65 72 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a  er the integer.*
793d0 2a 20 20 20 20 20 20 20 70 56 61 6c 20 20 20 20  *       pVal    
793e0 77 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 74  where to write t
793f0 68 65 20 69 6e 74 65 67 65 72 73 20 76 61 6c 75  he integers valu
79400 65 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73  e..**.** Convers
79410 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 75 6e  ions continue un
79420 74 69 6c 20 6f 6e 65 20 77 69 74 68 20 6e 65 78  til one with nex
79430 74 43 3d 3d 30 20 69 73 20 65 6e 63 6f 75 6e 74  tC==0 is encount
79440 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 75 6e  ered..** The fun
79450 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68  ction returns th
79460 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 75 63 63  e number of succ
79470 65 73 73 66 75 6c 20 63 6f 6e 76 65 72 73 69 6f  essful conversio
79480 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ns..*/.static in
79490 74 20 67 65 74 44 69 67 69 74 73 28 63 6f 6e 73  t getDigits(cons
794a0 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 2e  t char *zDate, .
794b0 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61  ..){.  va_list a
794c0 70 3b 0a 20 20 69 6e 74 20 76 61 6c 3b 0a 20 20  p;.  int val;.  
794d0 69 6e 74 20 4e 3b 0a 20 20 69 6e 74 20 6d 69 6e  int N;.  int min
794e0 3b 0a 20 20 69 6e 74 20 6d 61 78 3b 0a 20 20 69  ;.  int max;.  i
794f0 6e 74 20 6e 65 78 74 43 3b 0a 20 20 69 6e 74 20  nt nextC;.  int 
79500 2a 70 56 61 6c 3b 0a 20 20 69 6e 74 20 63 6e 74  *pVal;.  int cnt
79510 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74   = 0;.  va_start
79520 28 61 70 2c 20 7a 44 61 74 65 29 3b 0a 20 20 64  (ap, zDate);.  d
79530 6f 7b 0a 20 20 20 20 4e 20 3d 20 76 61 5f 61 72  o{.    N = va_ar
79540 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20  g(ap, int);.    
79550 6d 69 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  min = va_arg(ap,
79560 20 69 6e 74 29 3b 0a 20 20 20 20 6d 61 78 20 3d   int);.    max =
79570 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29   va_arg(ap, int)
79580 3b 0a 20 20 20 20 6e 65 78 74 43 20 3d 20 76 61  ;.    nextC = va
79590 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20  _arg(ap, int);. 
795a0 20 20 20 70 56 61 6c 20 3d 20 76 61 5f 61 72 67     pVal = va_arg
795b0 28 61 70 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20  (ap, int*);.    
795c0 76 61 6c 20 3d 20 30 3b 0a 20 20 20 20 77 68 69  val = 0;.    whi
795d0 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 20  le( N-- ){.     
795e0 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73 64   if( !sqlite3Isd
795f0 69 67 69 74 28 2a 7a 44 61 74 65 29 20 29 7b 0a  igit(*zDate) ){.
79600 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64          goto end
79610 5f 67 65 74 44 69 67 69 74 73 3b 0a 20 20 20 20  _getDigits;.    
79620 20 20 7d 0a 20 20 20 20 20 20 76 61 6c 20 3d 20    }.      val = 
79630 76 61 6c 2a 31 30 20 2b 20 2a 7a 44 61 74 65 20  val*10 + *zDate 
79640 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 7a 44 61  - '0';.      zDa
79650 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  te++;.    }.    
79660 69 66 28 20 76 61 6c 3c 6d 69 6e 20 7c 7c 20 76  if( val<min || v
79670 61 6c 3e 6d 61 78 20 7c 7c 20 28 6e 65 78 74 43  al>max || (nextC
79680 21 3d 30 20 26 26 20 6e 65 78 74 43 21 3d 2a 7a  !=0 && nextC!=*z
79690 44 61 74 65 29 20 29 7b 0a 20 20 20 20 20 20 67  Date) ){.      g
796a0 6f 74 6f 20 65 6e 64 5f 67 65 74 44 69 67 69 74  oto end_getDigit
796b0 73 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 56  s;.    }.    *pV
796c0 61 6c 20 3d 20 76 61 6c 3b 0a 20 20 20 20 7a 44  al = val;.    zD
796d0 61 74 65 2b 2b 3b 0a 20 20 20 20 63 6e 74 2b 2b  ate++;.    cnt++
796e0 3b 0a 20 20 7d 77 68 69 6c 65 28 20 6e 65 78 74  ;.  }while( next
796f0 43 20 29 3b 0a 65 6e 64 5f 67 65 74 44 69 67 69  C );.end_getDigi
79700 74 73 3a 0a 20 20 76 61 5f 65 6e 64 28 61 70 29  ts:.  va_end(ap)
79710 3b 0a 20 20 72 65 74 75 72 6e 20 63 6e 74 3b 0a  ;.  return cnt;.
79720 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 65  }../*.** Read te
79730 78 74 20 66 72 6f 6d 20 7a 5b 5d 20 61 6e 64 20  xt from z[] and 
79740 63 6f 6e 76 65 72 74 20 69 6e 74 6f 20 61 20 66  convert into a f
79750 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75  loating point nu
79760 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a 2a  mber.  Return.**
79770 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64   the number of d
79780 69 67 69 74 73 20 63 6f 6e 76 65 72 74 65 64 2e  igits converted.
79790 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 56  .*/.#define getV
797a0 61 6c 75 65 20 73 71 6c 69 74 65 33 41 74 6f 46  alue sqlite3AtoF
797b0 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 61 20  ../*.** Parse a 
797c0 74 69 6d 65 7a 6f 6e 65 20 65 78 74 65 6e 73 69  timezone extensi
797d0 6f 6e 20 6f 6e 20 74 68 65 20 65 6e 64 20 6f 66  on on the end of
797e0 20 61 20 64 61 74 65 2d 74 69 6d 65 2e 0a 2a 2a   a date-time..**
797f0 20 54 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69   The extension i
79800 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a  s of the form:.*
79810 2a 0a 2a 2a 20 20 20 20 20 20 20 20 28 2b 2f 2d  *.**        (+/-
79820 29 48 48 3a 4d 4d 0a 2a 2a 0a 2a 2a 20 4f 72 20  )HH:MM.**.** Or 
79830 74 68 65 20 22 7a 75 6c 75 22 20 6e 6f 74 61 74  the "zulu" notat
79840 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  ion:.**.**      
79850 20 20 5a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65    Z.**.** If the
79860 20 70 61 72 73 65 20 69 73 20 73 75 63 63 65 73   parse is succes
79870 73 66 75 6c 2c 20 77 72 69 74 65 20 74 68 65 20  sful, write the 
79880 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6e 75 74 65  number of minute
79890 73 0a 2a 2a 20 6f 66 20 63 68 61 6e 67 65 20 69  s.** of change i
798a0 6e 20 70 2d 3e 74 7a 20 61 6e 64 20 72 65 74 75  n p->tz and retu
798b0 72 6e 20 30 2e 20 20 49 66 20 61 20 70 61 72 73  rn 0.  If a pars
798c0 65 72 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  er error occurs,
798d0 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a  .** return non-z
798e0 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 69 73  ero..**.** A mis
798f0 73 69 6e 67 20 73 70 65 63 69 66 69 65 72 20 69  sing specifier i
79900 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65 64  s not considered
79910 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74   an error..*/.st
79920 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 54 69  atic int parseTi
79930 6d 65 7a 6f 6e 65 28 63 6f 6e 73 74 20 63 68 61  mezone(const cha
79940 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69  r *zDate, DateTi
79950 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 73 67  me *p){.  int sg
79960 6e 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 48 72  n = 0;.  int nHr
79970 2c 20 6e 4d 6e 3b 0a 20 20 69 6e 74 20 63 3b 0a  , nMn;.  int c;.
79980 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33    while( sqlite3
79990 49 73 73 70 61 63 65 28 2a 7a 44 61 74 65 29 20  Isspace(*zDate) 
799a0 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 20  ){ zDate++; }.  
799b0 70 2d 3e 74 7a 20 3d 20 30 3b 0a 20 20 63 20 3d  p->tz = 0;.  c =
799c0 20 2a 7a 44 61 74 65 3b 0a 20 20 69 66 28 20 63   *zDate;.  if( c
799d0 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 73 67 6e  =='-' ){.    sgn
799e0 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 20 69   = -1;.  }else i
799f0 66 28 20 63 3d 3d 27 2b 27 20 29 7b 0a 20 20 20  f( c=='+' ){.   
79a00 20 73 67 6e 20 3d 20 2b 31 3b 0a 20 20 7d 65 6c   sgn = +1;.  }el
79a10 73 65 20 69 66 28 20 63 3d 3d 27 5a 27 20 7c 7c  se if( c=='Z' ||
79a20 20 63 3d 3d 27 7a 27 20 29 7b 0a 20 20 20 20 7a   c=='z' ){.    z
79a30 44 61 74 65 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f  Date++;.    goto
79a40 20 7a 75 6c 75 5f 74 69 6d 65 3b 0a 20 20 7d 65   zulu_time;.  }e
79a50 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
79a60 63 21 3d 30 3b 0a 20 20 7d 0a 20 20 7a 44 61 74  c!=0;.  }.  zDat
79a70 65 2b 2b 3b 0a 20 20 69 66 28 20 67 65 74 44 69  e++;.  if( getDi
79a80 67 69 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 30  gits(zDate, 2, 0
79a90 2c 20 31 34 2c 20 27 3a 27 2c 20 26 6e 48 72 2c  , 14, ':', &nHr,
79aa0 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 6e   2, 0, 59, 0, &n
79ab0 4d 6e 29 21 3d 32 20 29 7b 0a 20 20 20 20 72 65  Mn)!=2 ){.    re
79ac0 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44  turn 1;.  }.  zD
79ad0 61 74 65 20 2b 3d 20 35 3b 0a 20 20 70 2d 3e 74  ate += 5;.  p->t
79ae0 7a 20 3d 20 73 67 6e 2a 28 6e 4d 6e 20 2b 20 6e  z = sgn*(nMn + n
79af0 48 72 2a 36 30 29 3b 0a 7a 75 6c 75 5f 74 69 6d  Hr*60);.zulu_tim
79b00 65 3a 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69  e:.  while( sqli
79b10 74 65 33 49 73 73 70 61 63 65 28 2a 7a 44 61 74  te3Isspace(*zDat
79b20 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d  e) ){ zDate++; }
79b30 0a 20 20 72 65 74 75 72 6e 20 2a 7a 44 61 74 65  .  return *zDate
79b40 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61  !=0;.}../*.** Pa
79b50 72 73 65 20 74 69 6d 65 73 20 6f 66 20 74 68 65  rse times of the
79b60 20 66 6f 72 6d 20 48 48 3a 4d 4d 20 6f 72 20 48   form HH:MM or H
79b70 48 3a 4d 4d 3a 53 53 20 6f 72 20 48 48 3a 4d 4d  H:MM:SS or HH:MM
79b80 3a 53 53 2e 46 46 46 46 2e 0a 2a 2a 20 54 68 65  :SS.FFFF..** The
79b90 20 48 48 2c 20 4d 4d 2c 20 61 6e 64 20 53 53 20   HH, MM, and SS 
79ba0 6d 75 73 74 20 65 61 63 68 20 62 65 20 65 78 61  must each be exa
79bb0 63 74 6c 79 20 32 20 64 69 67 69 74 73 2e 20 20  ctly 2 digits.  
79bc0 54 68 65 0a 2a 2a 20 66 72 61 63 74 69 6f 6e 61  The.** fractiona
79bd0 6c 20 73 65 63 6f 6e 64 73 20 46 46 46 46 20 63  l seconds FFFF c
79be0 61 6e 20 62 65 20 6f 6e 65 20 6f 72 20 6d 6f 72  an be one or mor
79bf0 65 20 64 69 67 69 74 73 2e 0a 2a 2a 0a 2a 2a 20  e digits..**.** 
79c00 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 72  Return 1 if ther
79c10 65 20 69 73 20 61 20 70 61 72 73 69 6e 67 20 65  e is a parsing e
79c20 72 72 6f 72 20 61 6e 64 20 30 20 6f 6e 20 73 75  rror and 0 on su
79c30 63 63 65 73 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  ccess..*/.static
79c40 20 69 6e 74 20 70 61 72 73 65 48 68 4d 6d 53 73   int parseHhMmSs
79c50 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61  (const char *zDa
79c60 74 65 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29  te, DateTime *p)
79c70 7b 0a 20 20 69 6e 74 20 68 2c 20 6d 2c 20 73 3b  {.  int h, m, s;
79c80 0a 20 20 64 6f 75 62 6c 65 20 6d 73 20 3d 20 30  .  double ms = 0
79c90 2e 30 3b 0a 20 20 69 66 28 20 67 65 74 44 69 67  .0;.  if( getDig
79ca0 69 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 30 2c  its(zDate, 2, 0,
79cb0 20 32 34 2c 20 27 3a 27 2c 20 26 68 2c 20 32 2c   24, ':', &h, 2,
79cc0 20 30 2c 20 35 39 2c 20 30 2c 20 26 6d 29 21 3d   0, 59, 0, &m)!=
79cd0 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  2 ){.    return 
79ce0 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 65 20 2b  1;.  }.  zDate +
79cf0 3d 20 35 3b 0a 20 20 69 66 28 20 2a 7a 44 61 74  = 5;.  if( *zDat
79d00 65 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 7a 44  e==':' ){.    zD
79d10 61 74 65 2b 2b 3b 0a 20 20 20 20 69 66 28 20 67  ate++;.    if( g
79d20 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 20  etDigits(zDate, 
79d30 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 73 29  2, 0, 59, 0, &s)
79d40 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 72 65 74  !=1 ){.      ret
79d50 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
79d60 20 7a 44 61 74 65 20 2b 3d 20 32 3b 0a 20 20 20   zDate += 2;.   
79d70 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d 27 2e 27   if( *zDate=='.'
79d80 20 26 26 20 73 71 6c 69 74 65 33 49 73 64 69 67   && sqlite3Isdig
79d90 69 74 28 7a 44 61 74 65 5b 31 5d 29 20 29 7b 0a  it(zDate[1]) ){.
79da0 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 53 63        double rSc
79db0 61 6c 65 20 3d 20 31 2e 30 3b 0a 20 20 20 20 20  ale = 1.0;.     
79dc0 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 20 20   zDate++;.      
79dd0 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73  while( sqlite3Is
79de0 64 69 67 69 74 28 2a 7a 44 61 74 65 29 20 29 7b  digit(*zDate) ){
79df0 0a 20 20 20 20 20 20 20 20 6d 73 20 3d 20 6d 73  .        ms = ms
79e00 2a 31 30 2e 30 20 2b 20 2a 7a 44 61 74 65 20 2d  *10.0 + *zDate -
79e10 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 72 53   '0';.        rS
79e20 63 61 6c 65 20 2a 3d 20 31 30 2e 30 3b 0a 20 20  cale *= 10.0;.  
79e30 20 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20        zDate++;. 
79e40 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 73 20       }.      ms 
79e50 2f 3d 20 72 53 63 61 6c 65 3b 0a 20 20 20 20 7d  /= rScale;.    }
79e60 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 20  .  }else{.    s 
79e70 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61  = 0;.  }.  p->va
79e80 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 70 2d 3e  lidJD = 0;.  p->
79e90 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 20  validHMS = 1;.  
79ea0 70 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 2d 3e 6d  p->h = h;.  p->m
79eb0 20 3d 20 6d 3b 0a 20 20 70 2d 3e 73 20 3d 20 73   = m;.  p->s = s
79ec0 20 2b 20 6d 73 3b 0a 20 20 69 66 28 20 70 61 72   + ms;.  if( par
79ed0 73 65 54 69 6d 65 7a 6f 6e 65 28 7a 44 61 74 65  seTimezone(zDate
79ee0 2c 20 70 29 20 29 20 72 65 74 75 72 6e 20 31 3b  , p) ) return 1;
79ef0 0a 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20  .  p->validTZ = 
79f00 28 70 2d 3e 74 7a 21 3d 30 29 3f 31 3a 30 3b 0a  (p->tz!=0)?1:0;.
79f10 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
79f20 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 66 72 6f  *.** Convert fro
79f30 6d 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a  m YYYY-MM-DD HH:
79f40 4d 4d 3a 53 53 20 74 6f 20 6a 75 6c 69 61 6e 20  MM:SS to julian 
79f50 64 61 79 2e 20 20 57 65 20 61 6c 77 61 79 73 20  day.  We always 
79f60 61 73 73 75 6d 65 0a 2a 2a 20 74 68 61 74 20 74  assume.** that t
79f70 68 65 20 59 59 59 59 2d 4d 4d 2d 44 44 20 69 73  he YYYY-MM-DD is
79f80 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68   according to th
79f90 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61 6c 65  e Gregorian cale
79fa0 6e 64 61 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 66 65  ndar..**.** Refe
79fb0 72 65 6e 63 65 3a 20 20 4d 65 65 75 73 20 70 61  rence:  Meeus pa
79fc0 67 65 20 36 31 0a 2a 2f 0a 73 74 61 74 69 63 20  ge 61.*/.static 
79fd0 76 6f 69 64 20 63 6f 6d 70 75 74 65 4a 44 28 44  void computeJD(D
79fe0 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69  ateTime *p){.  i
79ff0 6e 74 20 59 2c 20 4d 2c 20 44 2c 20 41 2c 20 42  nt Y, M, D, A, B
7a000 2c 20 58 31 2c 20 58 32 3b 0a 0a 20 20 69 66 28  , X1, X2;..  if(
7a010 20 70 2d 3e 76 61 6c 69 64 4a 44 20 29 20 72 65   p->validJD ) re
7a020 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 76  turn;.  if( p->v
7a030 61 6c 69 64 59 4d 44 20 29 7b 0a 20 20 20 20 59  alidYMD ){.    Y
7a040 20 3d 20 70 2d 3e 59 3b 0a 20 20 20 20 4d 20 3d   = p->Y;.    M =
7a050 20 70 2d 3e 4d 3b 0a 20 20 20 20 44 20 3d 20 70   p->M;.    D = p
7a060 2d 3e 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ->D;.  }else{.  
7a070 20 20 59 20 3d 20 32 30 30 30 3b 20 20 2f 2a 20    Y = 2000;  /* 
7a080 49 66 20 6e 6f 20 59 4d 44 20 73 70 65 63 69 66  If no YMD specif
7a090 69 65 64 2c 20 61 73 73 75 6d 65 20 32 30 30 30  ied, assume 2000
7a0a0 2d 4a 61 6e 2d 30 31 20 2a 2f 0a 20 20 20 20 4d  -Jan-01 */.    M
7a0b0 20 3d 20 31 3b 0a 20 20 20 20 44 20 3d 20 31 3b   = 1;.    D = 1;
7a0c0 0a 20 20 7d 0a 20 20 69 66 28 20 4d 3c 3d 32 20  .  }.  if( M<=2 
7a0d0 29 7b 0a 20 20 20 20 59 2d 2d 3b 0a 20 20 20 20  ){.    Y--;.    
7a0e0 4d 20 2b 3d 20 31 32 3b 0a 20 20 7d 0a 20 20 41  M += 12;.  }.  A
7a0f0 20 3d 20 59 2f 31 30 30 3b 0a 20 20 42 20 3d 20   = Y/100;.  B = 
7a100 32 20 2d 20 41 20 2b 20 28 41 2f 34 29 3b 0a 20  2 - A + (A/4);. 
7a110 20 58 31 20 3d 20 33 36 35 32 35 2a 28 59 2b 34   X1 = 36525*(Y+4
7a120 37 31 36 29 2f 31 30 30 3b 0a 20 20 58 32 20 3d  716)/100;.  X2 =
7a130 20 33 30 36 30 30 31 2a 28 4d 2b 31 29 2f 31 30   306001*(M+1)/10
7a140 30 30 30 3b 0a 20 20 70 2d 3e 69 4a 44 20 3d 20  000;.  p->iJD = 
7a150 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28  (sqlite3_int64)(
7a160 28 58 31 20 2b 20 58 32 20 2b 20 44 20 2b 20 42  (X1 + X2 + D + B
7a170 20 2d 20 31 35 32 34 2e 35 20 29 20 2a 20 38 36   - 1524.5 ) * 86
7a180 34 30 30 30 30 30 29 3b 0a 20 20 70 2d 3e 76 61  400000);.  p->va
7a190 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 69 66 28  lidJD = 1;.  if(
7a1a0 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 29 7b 0a   p->validHMS ){.
7a1b0 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 70 2d      p->iJD += p-
7a1c0 3e 68 2a 33 36 30 30 30 30 30 20 2b 20 70 2d 3e  >h*3600000 + p->
7a1d0 6d 2a 36 30 30 30 30 20 2b 20 28 73 71 6c 69 74  m*60000 + (sqlit
7a1e0 65 33 5f 69 6e 74 36 34 29 28 70 2d 3e 73 2a 31  e3_int64)(p->s*1
7a1f0 30 30 30 29 3b 0a 20 20 20 20 69 66 28 20 70 2d  000);.    if( p-
7a200 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20 20  >validTZ ){.    
7a210 20 20 70 2d 3e 69 4a 44 20 2d 3d 20 70 2d 3e 74    p->iJD -= p->t
7a220 7a 2a 36 30 30 30 30 3b 0a 20 20 20 20 20 20 70  z*60000;.      p
7a230 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 30 3b 0a  ->validYMD = 0;.
7a240 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d        p->validHM
7a250 53 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e  S = 0;.      p->
7a260 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 20  validTZ = 0;.   
7a270 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
7a280 50 61 72 73 65 20 64 61 74 65 73 20 6f 66 20 74  Parse dates of t
7a290 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20  he form.**.**   
7a2a0 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a    YYYY-MM-DD HH:
7a2b0 4d 4d 3a 53 53 2e 46 46 46 0a 2a 2a 20 20 20 20  MM:SS.FFF.**    
7a2c0 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d   YYYY-MM-DD HH:M
7a2d0 4d 3a 53 53 0a 2a 2a 20 20 20 20 20 59 59 59 59  M:SS.**     YYYY
7a2e0 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 0a 2a 2a 20  -MM-DD HH:MM.** 
7a2f0 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 0a 2a      YYYY-MM-DD.*
7a300 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 72  *.** Write the r
7a310 65 73 75 6c 74 20 69 6e 74 6f 20 74 68 65 20 44  esult into the D
7a320 61 74 65 54 69 6d 65 20 73 74 72 75 63 74 75 72  ateTime structur
7a330 65 20 61 6e 64 20 72 65 74 75 72 6e 20 30 0a 2a  e and return 0.*
7a340 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64  * on success and
7a350 20 31 20 69 66 20 74 68 65 20 69 6e 70 75 74 20   1 if the input 
7a360 73 74 72 69 6e 67 20 69 73 20 6e 6f 74 20 61 20  string is not a 
7a370 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20 64  well-formed.** d
7a380 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ate..*/.static i
7a390 6e 74 20 70 61 72 73 65 59 79 79 79 4d 6d 44 64  nt parseYyyyMmDd
7a3a0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61  (const char *zDa
7a3b0 74 65 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29  te, DateTime *p)
7a3c0 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 2c  {.  int Y, M, D,
7a3d0 20 6e 65 67 3b 0a 0a 20 20 69 66 28 20 7a 44 61   neg;..  if( zDa
7a3e0 74 65 5b 30 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20  te[0]=='-' ){.  
7a3f0 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 6e    zDate++;.    n
7a400 65 67 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b  eg = 1;.  }else{
7a410 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20 20  .    neg = 0;.  
7a420 7d 0a 20 20 69 66 28 20 67 65 74 44 69 67 69 74  }.  if( getDigit
7a430 73 28 7a 44 61 74 65 2c 34 2c 30 2c 39 39 39 39  s(zDate,4,0,9999
7a440 2c 27 2d 27 2c 26 59 2c 32 2c 31 2c 31 32 2c 27  ,'-',&Y,2,1,12,'
7a450 2d 27 2c 26 4d 2c 32 2c 31 2c 33 31 2c 30 2c 26  -',&M,2,1,31,0,&
7a460 44 29 21 3d 33 20 29 7b 0a 20 20 20 20 72 65 74  D)!=3 ){.    ret
7a470 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 61  urn 1;.  }.  zDa
7a480 74 65 20 2b 3d 20 31 30 3b 0a 20 20 77 68 69 6c  te += 10;.  whil
7a490 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63  e( sqlite3Isspac
7a4a0 65 28 2a 7a 44 61 74 65 29 20 7c 7c 20 27 54 27  e(*zDate) || 'T'
7a4b0 3d 3d 2a 28 75 38 2a 29 7a 44 61 74 65 20 29 7b  ==*(u8*)zDate ){
7a4c0 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 69 66   zDate++; }.  if
7a4d0 28 20 70 61 72 73 65 48 68 4d 6d 53 73 28 7a 44  ( parseHhMmSs(zD
7a4e0 61 74 65 2c 20 70 29 3d 3d 30 20 29 7b 0a 20 20  ate, p)==0 ){.  
7a4f0 20 20 2f 2a 20 57 65 20 67 6f 74 20 74 68 65 20    /* We got the 
7a500 74 69 6d 65 20 2a 2f 0a 20 20 7d 65 6c 73 65 20  time */.  }else 
7a510 69 66 28 20 2a 7a 44 61 74 65 3d 3d 30 20 29 7b  if( *zDate==0 ){
7a520 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53  .    p->validHMS
7a530 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
7a540 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
7a550 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20  .  p->validJD = 
7a560 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44  0;.  p->validYMD
7a570 20 3d 20 31 3b 0a 20 20 70 2d 3e 59 20 3d 20 6e   = 1;.  p->Y = n
7a580 65 67 20 3f 20 2d 59 20 3a 20 59 3b 0a 20 20 70  eg ? -Y : Y;.  p
7a590 2d 3e 4d 20 3d 20 4d 3b 0a 20 20 70 2d 3e 44 20  ->M = M;.  p->D 
7a5a0 3d 20 44 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61  = D;.  if( p->va
7a5b0 6c 69 64 54 5a 20 29 7b 0a 20 20 20 20 63 6f 6d  lidTZ ){.    com
7a5c0 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 7d 0a 20  puteJD(p);.  }. 
7a5d0 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
7a5e0 0a 2a 2a 20 53 65 74 20 74 68 65 20 74 69 6d 65  .** Set the time
7a5f0 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20   to the current 
7a600 74 69 6d 65 20 72 65 70 6f 72 74 65 64 20 62 79  time reported by
7a610 20 74 68 65 20 56 46 53 0a 2a 2f 0a 73 74 61 74   the VFS.*/.stat
7a620 69 63 20 76 6f 69 64 20 73 65 74 44 61 74 65 54  ic void setDateT
7a630 69 6d 65 54 6f 43 75 72 72 65 6e 74 28 73 71 6c  imeToCurrent(sql
7a640 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
7a650 6e 74 65 78 74 2c 20 44 61 74 65 54 69 6d 65 20  ntext, DateTime 
7a660 2a 70 29 7b 0a 20 20 64 6f 75 62 6c 65 20 72 3b  *p){.  double r;
7a670 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
7a680 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
7a690 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65  _db_handle(conte
7a6a0 78 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73  xt);.  sqlite3Os
7a6b0 43 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e  CurrentTime(db->
7a6c0 70 56 66 73 2c 20 26 72 29 3b 0a 20 20 70 2d 3e  pVfs, &r);.  p->
7a6d0 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69  iJD = (sqlite3_i
7a6e0 6e 74 36 34 29 28 72 2a 38 36 34 30 30 30 30 30  nt64)(r*86400000
7a6f0 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 70 2d 3e  .0 + 0.5);.  p->
7a700 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 7d 0a 0a  validJD = 1;.}..
7a710 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f  /*.** Attempt to
7a720 20 70 61 72 73 65 20 74 68 65 20 67 69 76 65 6e   parse the given
7a730 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 4a   string into a J
7a740 75 6c 69 61 6e 20 44 61 79 20 4e 75 6d 62 65 72  ulian Day Number
7a750 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65  .  Return.** the
7a760 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72   number of error
7a770 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  s..**.** The fol
7a780 6c 6f 77 69 6e 67 20 61 72 65 20 61 63 63 65 70  lowing are accep
7a790 74 61 62 6c 65 20 66 6f 72 6d 73 20 66 6f 72 20  table forms for 
7a7a0 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67  the input string
7a7b0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 59 59 59  :.**.**      YYY
7a7c0 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53  Y-MM-DD HH:MM:SS
7a7d0 2e 46 46 46 20 20 2b 2f 2d 48 48 3a 4d 4d 0a 2a  .FFF  +/-HH:MM.*
7a7e0 2a 20 20 20 20 20 20 44 44 44 44 2e 44 44 20 0a  *      DDDD.DD .
7a7f0 2a 2a 20 20 20 20 20 20 6e 6f 77 0a 2a 2a 0a 2a  **      now.**.*
7a800 2a 20 49 6e 20 74 68 65 20 66 69 72 73 74 20 66  * In the first f
7a810 6f 72 6d 2c 20 74 68 65 20 2b 2f 2d 48 48 3a 4d  orm, the +/-HH:M
7a820 4d 20 69 73 20 61 6c 77 61 79 73 20 6f 70 74 69  M is always opti
7a830 6f 6e 61 6c 2e 20 20 54 68 65 20 66 72 61 63 74  onal.  The fract
7a840 69 6f 6e 61 6c 0a 2a 2a 20 73 65 63 6f 6e 64 73  ional.** seconds
7a850 20 65 78 74 65 6e 73 69 6f 6e 20 28 74 68 65 20   extension (the 
7a860 22 2e 46 46 46 22 29 20 69 73 20 6f 70 74 69 6f  ".FFF") is optio
7a870 6e 61 6c 2e 20 20 54 68 65 20 73 65 63 6f 6e 64  nal.  The second
7a880 73 20 70 6f 72 74 69 6f 6e 0a 2a 2a 20 28 22 3a  s portion.** (":
7a890 53 53 2e 46 46 46 22 29 20 69 73 20 6f 70 74 69  SS.FFF") is opti
7a8a0 6f 6e 2e 20 20 54 68 65 20 79 65 61 72 20 61 6e  on.  The year an
7a8b0 64 20 64 61 74 65 20 63 61 6e 20 62 65 20 6f 6d  d date can be om
7a8c0 69 74 74 65 64 20 61 73 20 6c 6f 6e 67 0a 2a 2a  itted as long.**
7a8d0 20 61 73 20 74 68 65 72 65 20 69 73 20 61 20 74   as there is a t
7a8e0 69 6d 65 20 73 74 72 69 6e 67 2e 20 20 54 68 65  ime string.  The
7a8f0 20 74 69 6d 65 20 73 74 72 69 6e 67 20 63 61 6e   time string can
7a900 20 62 65 20 6f 6d 69 74 74 65 64 20 61 73 20 6c   be omitted as l
7a910 6f 6e 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 20  ong.** as there 
7a920 69 73 20 61 20 79 65 61 72 20 61 6e 64 20 64 61  is a year and da
7a930 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  te..*/.static in
7a940 74 20 70 61 72 73 65 44 61 74 65 4f 72 54 69 6d  t parseDateOrTim
7a950 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  e(.  sqlite3_con
7a960 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a  text *context, .
7a970 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
7a980 61 74 65 2c 20 0a 20 20 44 61 74 65 54 69 6d 65  ate, .  DateTime
7a990 20 2a 70 0a 29 7b 0a 20 20 69 6e 74 20 69 73 52   *p.){.  int isR
7a9a0 65 61 6c 4e 75 6d 3b 20 20 20 20 2f 2a 20 52 65  ealNum;    /* Re
7a9b0 74 75 72 6e 20 66 72 6f 6d 20 73 71 6c 69 74 65  turn from sqlite
7a9c0 33 49 73 4e 75 6d 62 65 72 28 29 2e 20 20 4e 6f  3IsNumber().  No
7a9d0 74 20 75 73 65 64 20 2a 2f 0a 20 20 69 66 28 20  t used */.  if( 
7a9e0 70 61 72 73 65 59 79 79 79 4d 6d 44 64 28 7a 44  parseYyyyMmDd(zD
7a9f0 61 74 65 2c 70 29 3d 3d 30 20 29 7b 0a 20 20 20  ate,p)==0 ){.   
7aa00 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c   return 0;.  }el
7aa10 73 65 20 69 66 28 20 70 61 72 73 65 48 68 4d 6d  se if( parseHhMm
7aa20 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 20  Ss(zDate, p)==0 
7aa30 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
7aa40 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c  .  }else if( sql
7aa50 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44 61 74  ite3StrICmp(zDat
7aa60 65 2c 22 6e 6f 77 22 29 3d 3d 30 29 7b 0a 20 20  e,"now")==0){.  
7aa70 20 20 73 65 74 44 61 74 65 54 69 6d 65 54 6f 43    setDateTimeToC
7aa80 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74 2c 20  urrent(context, 
7aa90 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  p);.    return 0
7aaa0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71  ;.  }else if( sq
7aab0 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 7a 44  lite3IsNumber(zD
7aac0 61 74 65 2c 20 26 69 73 52 65 61 6c 4e 75 6d 2c  ate, &isRealNum,
7aad0 20 53 51 4c 49 54 45 5f 55 54 46 38 29 20 29 7b   SQLITE_UTF8) ){
7aae0 0a 20 20 20 20 64 6f 75 62 6c 65 20 72 3b 0a 20  .    double r;. 
7aaf0 20 20 20 67 65 74 56 61 6c 75 65 28 7a 44 61 74     getValue(zDat
7ab00 65 2c 20 26 72 29 3b 0a 20 20 20 20 70 2d 3e 69  e, &r);.    p->i
7ab10 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e  JD = (sqlite3_in
7ab20 74 36 34 29 28 72 2a 38 36 34 30 30 30 30 30 2e  t64)(r*86400000.
7ab30 30 20 2b 20 30 2e 35 29 3b 0a 20 20 20 20 70 2d  0 + 0.5);.    p-
7ab40 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20  >validJD = 1;.  
7ab50 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
7ab60 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f    return 1;.}../
7ab70 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65  *.** Compute the
7ab80 20 59 65 61 72 2c 20 4d 6f 6e 74 68 2c 20 61 6e   Year, Month, an
7ab90 64 20 44 61 79 20 66 72 6f 6d 20 74 68 65 20 6a  d Day from the j
7aba0 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72  ulian day number
7abb0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
7abc0 20 63 6f 6d 70 75 74 65 59 4d 44 28 44 61 74 65   computeYMD(Date
7abd0 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  Time *p){.  int 
7abe0 5a 2c 20 41 2c 20 42 2c 20 43 2c 20 44 2c 20 45  Z, A, B, C, D, E
7abf0 2c 20 58 31 3b 0a 20 20 69 66 28 20 70 2d 3e 76  , X1;.  if( p->v
7ac00 61 6c 69 64 59 4d 44 20 29 20 72 65 74 75 72 6e  alidYMD ) return
7ac10 3b 0a 20 20 69 66 28 20 21 70 2d 3e 76 61 6c 69  ;.  if( !p->vali
7ac20 64 4a 44 20 29 7b 0a 20 20 20 20 70 2d 3e 59 20  dJD ){.    p->Y 
7ac30 3d 20 32 30 30 30 3b 0a 20 20 20 20 70 2d 3e 4d  = 2000;.    p->M
7ac40 20 3d 20 31 3b 0a 20 20 20 20 70 2d 3e 44 20 3d   = 1;.    p->D =
7ac50 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   1;.  }else{.   
7ac60 20 5a 20 3d 20 28 69 6e 74 29 28 28 70 2d 3e 69   Z = (int)((p->i
7ac70 4a 44 20 2b 20 34 33 32 30 30 30 30 30 29 2f 38  JD + 43200000)/8
7ac80 36 34 30 30 30 30 30 29 3b 0a 20 20 20 20 41 20  6400000);.    A 
7ac90 3d 20 28 69 6e 74 29 28 28 5a 20 2d 20 31 38 36  = (int)((Z - 186
7aca0 37 32 31 36 2e 32 35 29 2f 33 36 35 32 34 2e 32  7216.25)/36524.2
7acb0 35 29 3b 0a 20 20 20 20 41 20 3d 20 5a 20 2b 20  5);.    A = Z + 
7acc0 31 20 2b 20 41 20 2d 20 28 41 2f 34 29 3b 0a 20  1 + A - (A/4);. 
7acd0 20 20 20 42 20 3d 20 41 20 2b 20 31 35 32 34 3b     B = A + 1524;
7ace0 0a 20 20 20 20 43 20 3d 20 28 69 6e 74 29 28 28  .    C = (int)((
7acf0 42 20 2d 20 31 32 32 2e 31 29 2f 33 36 35 2e 32  B - 122.1)/365.2
7ad00 35 29 3b 0a 20 20 20 20 44 20 3d 20 28 33 36 35  5);.    D = (365
7ad10 32 35 2a 43 29 2f 31 30 30 3b 0a 20 20 20 20 45  25*C)/100;.    E
7ad20 20 3d 20 28 69 6e 74 29 28 28 42 2d 44 29 2f 33   = (int)((B-D)/3
7ad30 30 2e 36 30 30 31 29 3b 0a 20 20 20 20 58 31 20  0.6001);.    X1 
7ad40 3d 20 28 69 6e 74 29 28 33 30 2e 36 30 30 31 2a  = (int)(30.6001*
7ad50 45 29 3b 0a 20 20 20 20 70 2d 3e 44 20 3d 20 42  E);.    p->D = B
7ad60 20 2d 20 44 20 2d 20 58 31 3b 0a 20 20 20 20 70   - D - X1;.    p
7ad70 2d 3e 4d 20 3d 20 45 3c 31 34 20 3f 20 45 2d 31  ->M = E<14 ? E-1
7ad80 20 3a 20 45 2d 31 33 3b 0a 20 20 20 20 70 2d 3e   : E-13;.    p->
7ad90 59 20 3d 20 70 2d 3e 4d 3e 32 20 3f 20 43 20 2d  Y = p->M>2 ? C -
7ada0 20 34 37 31 36 20 3a 20 43 20 2d 20 34 37 31 35   4716 : C - 4715
7adb0 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 64  ;.  }.  p->valid
7adc0 59 4d 44 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  YMD = 1;.}../*.*
7add0 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 48 6f  * Compute the Ho
7ade0 75 72 2c 20 4d 69 6e 75 74 65 2c 20 61 6e 64 20  ur, Minute, and 
7adf0 53 65 63 6f 6e 64 73 20 66 72 6f 6d 20 74 68 65  Seconds from the
7ae00 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62   julian day numb
7ae10 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  er..*/.static vo
7ae20 69 64 20 63 6f 6d 70 75 74 65 48 4d 53 28 44 61  id computeHMS(Da
7ae30 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e  teTime *p){.  in
7ae40 74 20 73 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61  t s;.  if( p->va
7ae50 6c 69 64 48 4d 53 20 29 20 72 65 74 75 72 6e 3b  lidHMS ) return;
7ae60 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b  .  computeJD(p);
7ae70 0a 20 20 73 20 3d 20 28 69 6e 74 29 28 28 70 2d  .  s = (int)((p-
7ae80 3e 69 4a 44 20 2b 20 34 33 32 30 30 30 30 30 29  >iJD + 43200000)
7ae90 20 25 20 38 36 34 30 30 30 30 30 29 3b 0a 20 20   % 86400000);.  
7aea0 70 2d 3e 73 20 3d 20 73 2f 31 30 30 30 2e 30 3b  p->s = s/1000.0;
7aeb0 0a 20 20 73 20 3d 20 28 69 6e 74 29 70 2d 3e 73  .  s = (int)p->s
7aec0 3b 0a 20 20 70 2d 3e 73 20 2d 3d 20 73 3b 0a 20  ;.  p->s -= s;. 
7aed0 20 70 2d 3e 68 20 3d 20 73 2f 33 36 30 30 3b 0a   p->h = s/3600;.
7aee0 20 20 73 20 2d 3d 20 70 2d 3e 68 2a 33 36 30 30    s -= p->h*3600
7aef0 3b 0a 20 20 70 2d 3e 6d 20 3d 20 73 2f 36 30 3b  ;.  p->m = s/60;
7af00 0a 20 20 70 2d 3e 73 20 2b 3d 20 73 20 2d 20 70  .  p->s += s - p
7af10 2d 3e 6d 2a 36 30 3b 0a 20 20 70 2d 3e 76 61 6c  ->m*60;.  p->val
7af20 69 64 48 4d 53 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a  idHMS = 1;.}../*
7af30 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 62 6f 74 68  .** Compute both
7af40 20 59 4d 44 20 61 6e 64 20 48 4d 53 0a 2a 2f 0a   YMD and HMS.*/.
7af50 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70  static void comp
7af60 75 74 65 59 4d 44 5f 48 4d 53 28 44 61 74 65 54  uteYMD_HMS(DateT
7af70 69 6d 65 20 2a 70 29 7b 0a 20 20 63 6f 6d 70 75  ime *p){.  compu
7af80 74 65 59 4d 44 28 70 29 3b 0a 20 20 63 6f 6d 70  teYMD(p);.  comp
7af90 75 74 65 48 4d 53 28 70 29 3b 0a 7d 0a 0a 2f 2a  uteHMS(p);.}../*
7afa0 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 59 4d  .** Clear the YM
7afb0 44 20 61 6e 64 20 48 4d 53 20 61 6e 64 20 74 68  D and HMS and th
7afc0 65 20 54 5a 0a 2a 2f 0a 73 74 61 74 69 63 20 76  e TZ.*/.static v
7afd0 6f 69 64 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53  oid clearYMD_HMS
7afe0 5f 54 5a 28 44 61 74 65 54 69 6d 65 20 2a 70 29  _TZ(DateTime *p)
7aff0 7b 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20  {.  p->validYMD 
7b000 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 48  = 0;.  p->validH
7b010 4d 53 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c  MS = 0;.  p->val
7b020 69 64 54 5a 20 3d 20 30 3b 0a 7d 0a 0a 23 69 66  idTZ = 0;.}..#if
7b030 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
7b040 5f 4c 4f 43 41 4c 54 49 4d 45 0a 2f 2a 0a 2a 2a  _LOCALTIME./*.**
7b050 20 43 6f 6d 70 75 74 65 20 74 68 65 20 64 69 66   Compute the dif
7b060 66 65 72 65 6e 63 65 20 28 69 6e 20 6d 69 6c 6c  ference (in mill
7b070 69 73 65 63 6f 6e 64 73 29 0a 2a 2a 20 62 65 74  iseconds).** bet
7b080 77 65 65 6e 20 6c 6f 63 61 6c 74 69 6d 65 20 61  ween localtime a
7b090 6e 64 20 55 54 43 20 28 61 2e 6b 2e 61 2e 20 47  nd UTC (a.k.a. G
7b0a0 4d 54 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74  MT).** for the t
7b0b0 69 6d 65 20 76 61 6c 75 65 20 70 20 77 68 65 72  ime value p wher
7b0c0 65 20 70 20 69 73 20 69 6e 20 55 54 43 2e 0a 2a  e p is in UTC..*
7b0d0 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33  /.static sqlite3
7b0e0 5f 69 6e 74 36 34 20 6c 6f 63 61 6c 74 69 6d 65  _int64 localtime
7b0f0 4f 66 66 73 65 74 28 44 61 74 65 54 69 6d 65 20  Offset(DateTime 
7b100 2a 70 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20  *p){.  DateTime 
7b110 78 2c 20 79 3b 0a 20 20 74 69 6d 65 5f 74 20 74  x, y;.  time_t t
7b120 3b 0a 20 20 78 20 3d 20 2a 70 3b 0a 20 20 63 6f  ;.  x = *p;.  co
7b130 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 26 78 29  mputeYMD_HMS(&x)
7b140 3b 0a 20 20 69 66 28 20 78 2e 59 3c 31 39 37 31  ;.  if( x.Y<1971
7b150 20 7c 7c 20 78 2e 59 3e 3d 32 30 33 38 20 29 7b   || x.Y>=2038 ){
7b160 0a 20 20 20 20 78 2e 59 20 3d 20 32 30 30 30 3b  .    x.Y = 2000;
7b170 0a 20 20 20 20 78 2e 4d 20 3d 20 31 3b 0a 20 20  .    x.M = 1;.  
7b180 20 20 78 2e 44 20 3d 20 31 3b 0a 20 20 20 20 78    x.D = 1;.    x
7b190 2e 68 20 3d 20 30 3b 0a 20 20 20 20 78 2e 6d 20  .h = 0;.    x.m 
7b1a0 3d 20 30 3b 0a 20 20 20 20 78 2e 73 20 3d 20 30  = 0;.    x.s = 0
7b1b0 2e 30 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20  .0;.  } else {. 
7b1c0 20 20 20 69 6e 74 20 73 20 3d 20 28 69 6e 74 29     int s = (int)
7b1d0 28 78 2e 73 20 2b 20 30 2e 35 29 3b 0a 20 20 20  (x.s + 0.5);.   
7b1e0 20 78 2e 73 20 3d 20 73 3b 0a 20 20 7d 0a 20 20   x.s = s;.  }.  
7b1f0 78 2e 74 7a 20 3d 20 30 3b 0a 20 20 78 2e 76 61  x.tz = 0;.  x.va
7b200 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 63 6f 6d  lidJD = 0;.  com
7b210 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 20 74 20  puteJD(&x);.  t 
7b220 3d 20 78 2e 69 4a 44 2f 31 30 30 30 20 2d 20 32  = x.iJD/1000 - 2
7b230 31 30 38 36 36 37 36 2a 28 69 36 34 29 31 30 30  1086676*(i64)100
7b240 30 30 3b 0a 23 69 66 64 65 66 20 48 41 56 45 5f  00;.#ifdef HAVE_
7b250 4c 4f 43 41 4c 54 49 4d 45 5f 52 0a 20 20 7b 0a  LOCALTIME_R.  {.
7b260 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 73 4c      struct tm sL
7b270 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f 63 61 6c 74  ocal;.    localt
7b280 69 6d 65 5f 72 28 26 74 2c 20 26 73 4c 6f 63 61  ime_r(&t, &sLoca
7b290 6c 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 73 4c  l);.    y.Y = sL
7b2a0 6f 63 61 6c 2e 74 6d 5f 79 65 61 72 20 2b 20 31  ocal.tm_year + 1
7b2b0 39 30 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 73  900;.    y.M = s
7b2c0 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b 20 31  Local.tm_mon + 1
7b2d0 3b 0a 20 20 20 20 79 2e 44 20 3d 20 73 4c 6f 63  ;.    y.D = sLoc
7b2e0 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20  al.tm_mday;.    
7b2f0 79 2e 68 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f  y.h = sLocal.tm_
7b300 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20  hour;.    y.m = 
7b310 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20  sLocal.tm_min;. 
7b320 20 20 20 79 2e 73 20 3d 20 73 4c 6f 63 61 6c 2e     y.s = sLocal.
7b330 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a 23 65 6c 69  tm_sec;.  }.#eli
7b340 66 20 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c  f defined(HAVE_L
7b350 4f 43 41 4c 54 49 4d 45 5f 53 29 0a 20 20 7b 0a  OCALTIME_S).  {.
7b360 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 73 4c      struct tm sL
7b370 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f 63 61 6c 74  ocal;.    localt
7b380 69 6d 65 5f 73 28 26 73 4c 6f 63 61 6c 2c 20 26  ime_s(&sLocal, &
7b390 74 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 73 4c  t);.    y.Y = sL
7b3a0 6f 63 61 6c 2e 74 6d 5f 79 65 61 72 20 2b 20 31  ocal.tm_year + 1
7b3b0 39 30 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 73  900;.    y.M = s
7b3c0 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b 20 31  Local.tm_mon + 1
7b3d0 3b 0a 20 20 20 20 79 2e 44 20 3d 20 73 4c 6f 63  ;.    y.D = sLoc
7b3e0 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20  al.tm_mday;.    
7b3f0 79 2e 68 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f  y.h = sLocal.tm_
7b400 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20  hour;.    y.m = 
7b410 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20  sLocal.tm_min;. 
7b420 20 20 20 79 2e 73 20 3d 20 73 4c 6f 63 61 6c 2e     y.s = sLocal.
7b430 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a 23 65 6c 73  tm_sec;.  }.#els
7b440 65 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74  e.  {.    struct
7b450 20 74 6d 20 2a 70 54 6d 3b 0a 20 20 20 20 73 71   tm *pTm;.    sq
7b460 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
7b470 72 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  r(sqlite3MutexAl
7b480 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
7b490 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29  _STATIC_MASTER))
7b4a0 3b 0a 20 20 20 20 70 54 6d 20 3d 20 6c 6f 63 61  ;.    pTm = loca
7b4b0 6c 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20 79  ltime(&t);.    y
7b4c0 2e 59 20 3d 20 70 54 6d 2d 3e 74 6d 5f 79 65 61  .Y = pTm->tm_yea
7b4d0 72 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 79 2e  r + 1900;.    y.
7b4e0 4d 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 6f 6e 20  M = pTm->tm_mon 
7b4f0 2b 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20 70  + 1;.    y.D = p
7b500 54 6d 2d 3e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20  Tm->tm_mday;.   
7b510 20 79 2e 68 20 3d 20 70 54 6d 2d 3e 74 6d 5f 68   y.h = pTm->tm_h
7b520 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 70  our;.    y.m = p
7b530 54 6d 2d 3e 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20  Tm->tm_min;.    
7b540 79 2e 73 20 3d 20 70 54 6d 2d 3e 74 6d 5f 73 65  y.s = pTm->tm_se
7b550 63 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  c;.    sqlite3_m
7b560 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74  utex_leave(sqlit
7b570 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
7b580 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
7b590 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 7d 0a 23  _MASTER));.  }.#
7b5a0 65 6e 64 69 66 0a 20 20 79 2e 76 61 6c 69 64 59  endif.  y.validY
7b5b0 4d 44 20 3d 20 31 3b 0a 20 20 79 2e 76 61 6c 69  MD = 1;.  y.vali
7b5c0 64 48 4d 53 20 3d 20 31 3b 0a 20 20 79 2e 76 61  dHMS = 1;.  y.va
7b5d0 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 79 2e 76  lidJD = 0;.  y.v
7b5e0 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 63 6f  alidTZ = 0;.  co
7b5f0 6d 70 75 74 65 4a 44 28 26 79 29 3b 0a 20 20 72  mputeJD(&y);.  r
7b600 65 74 75 72 6e 20 79 2e 69 4a 44 20 2d 20 78 2e  eturn y.iJD - x.
7b610 69 4a 44 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  iJD;.}.#endif /*
7b620 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43   SQLITE_OMIT_LOC
7b630 41 4c 54 49 4d 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  ALTIME */../*.**
7b640 20 50 72 6f 63 65 73 73 20 61 20 6d 6f 64 69 66   Process a modif
7b650 69 65 72 20 74 6f 20 61 20 64 61 74 65 2d 74 69  ier to a date-ti
7b660 6d 65 20 73 74 61 6d 70 2e 20 20 54 68 65 20 6d  me stamp.  The m
7b670 6f 64 69 66 69 65 72 73 20 61 72 65 0a 2a 2a 20  odifiers are.** 
7b680 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
7b690 2a 20 20 20 20 20 4e 4e 4e 20 64 61 79 73 0a 2a  *     NNN days.*
7b6a0 2a 20 20 20 20 20 4e 4e 4e 20 68 6f 75 72 73 0a  *     NNN hours.
7b6b0 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d 69 6e 75 74  **     NNN minut
7b6c0 65 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 2e 4e 4e  es.**     NNN.NN
7b6d0 4e 4e 20 73 65 63 6f 6e 64 73 0a 2a 2a 20 20 20  NN seconds.**   
7b6e0 20 20 4e 4e 4e 20 6d 6f 6e 74 68 73 0a 2a 2a 20    NNN months.** 
7b6f0 20 20 20 20 4e 4e 4e 20 79 65 61 72 73 0a 2a 2a      NNN years.**
7b700 20 20 20 20 20 73 74 61 72 74 20 6f 66 20 6d 6f       start of mo
7b710 6e 74 68 0a 2a 2a 20 20 20 20 20 73 74 61 72 74  nth.**     start
7b720 20 6f 66 20 79 65 61 72 0a 2a 2a 20 20 20 20 20   of year.**     
7b730 73 74 61 72 74 20 6f 66 20 77 65 65 6b 0a 2a 2a  start of week.**
7b740 20 20 20 20 20 73 74 61 72 74 20 6f 66 20 64 61       start of da
7b750 79 0a 2a 2a 20 20 20 20 20 77 65 65 6b 64 61 79  y.**     weekday
7b760 20 4e 0a 2a 2a 20 20 20 20 20 75 6e 69 78 65 70   N.**     unixep
7b770 6f 63 68 0a 2a 2a 20 20 20 20 20 6c 6f 63 61 6c  och.**     local
7b780 74 69 6d 65 0a 2a 2a 20 20 20 20 20 75 74 63 0a  time.**     utc.
7b790 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 6f  **.** Return 0 o
7b7a0 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 31 20  n success and 1 
7b7b0 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 20  if there is any 
7b7c0 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 2e 0a 2a  kind of error..*
7b7d0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72  /.static int par
7b7e0 73 65 4d 6f 64 69 66 69 65 72 28 63 6f 6e 73 74  seModifier(const
7b7f0 20 63 68 61 72 20 2a 7a 4d 6f 64 2c 20 44 61 74   char *zMod, Dat
7b800 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74  eTime *p){.  int
7b810 20 72 63 20 3d 20 31 3b 0a 20 20 69 6e 74 20 6e   rc = 1;.  int n
7b820 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 0a 20 20  ;.  double r;.  
7b830 63 68 61 72 20 2a 7a 2c 20 7a 42 75 66 5b 33 30  char *z, zBuf[30
7b840 5d 3b 0a 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20  ];.  z = zBuf;. 
7b850 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 41 72 72 61   for(n=0; n<Arra
7b860 79 53 69 7a 65 28 7a 42 75 66 29 2d 31 20 26 26  ySize(zBuf)-1 &&
7b870 20 7a 4d 6f 64 5b 6e 5d 3b 20 6e 2b 2b 29 7b 0a   zMod[n]; n++){.
7b880 20 20 20 20 7a 5b 6e 5d 20 3d 20 28 63 68 61 72      z[n] = (char
7b890 29 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c  )sqlite3UpperToL
7b8a0 6f 77 65 72 5b 28 75 38 29 7a 4d 6f 64 5b 6e 5d  ower[(u8)zMod[n]
7b8b0 5d 3b 0a 20 20 7d 0a 20 20 7a 5b 6e 5d 20 3d 20  ];.  }.  z[n] = 
7b8c0 30 3b 0a 20 20 73 77 69 74 63 68 28 20 7a 5b 30  0;.  switch( z[0
7b8d0 5d 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  ] ){.#ifndef SQL
7b8e0 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49  ITE_OMIT_LOCALTI
7b8f0 4d 45 0a 20 20 20 20 63 61 73 65 20 27 6c 27 3a  ME.    case 'l':
7b900 20 7b 0a 20 20 20 20 20 20 2f 2a 20 20 20 20 6c   {.      /*    l
7b910 6f 63 61 6c 74 69 6d 65 0a 20 20 20 20 20 20 2a  ocaltime.      *
7b920 2a 0a 20 20 20 20 20 20 2a 2a 20 41 73 73 75 6d  *.      ** Assum
7b930 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20  ing the current 
7b940 74 69 6d 65 20 76 61 6c 75 65 20 69 73 20 55 54  time value is UT
7b950 43 20 28 61 2e 6b 2e 61 2e 20 47 4d 54 29 2c 20  C (a.k.a. GMT), 
7b960 73 68 69 66 74 20 69 74 20 74 6f 0a 20 20 20 20  shift it to.    
7b970 20 20 2a 2a 20 73 68 6f 77 20 6c 6f 63 61 6c 20    ** show local 
7b980 74 69 6d 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  time..      */. 
7b990 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28       if( strcmp(
7b9a0 7a 2c 20 22 6c 6f 63 61 6c 74 69 6d 65 22 29 3d  z, "localtime")=
7b9b0 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f  =0 ){.        co
7b9c0 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20  mputeJD(p);.    
7b9d0 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 6c 6f      p->iJD += lo
7b9e0 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 29  caltimeOffset(p)
7b9f0 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59  ;.        clearY
7ba00 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20  MD_HMS_TZ(p);.  
7ba10 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20        rc = 0;.  
7ba20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
7ba30 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  k;.    }.#endif.
7ba40 20 20 20 20 63 61 73 65 20 27 75 27 3a 20 7b 0a      case 'u': {.
7ba50 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a        /*.      *
7ba60 2a 20 20 20 20 75 6e 69 78 65 70 6f 63 68 0a 20  *    unixepoch. 
7ba70 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
7ba80 20 54 72 65 61 74 20 74 68 65 20 63 75 72 72 65   Treat the curre
7ba90 6e 74 20 76 61 6c 75 65 20 6f 66 20 70 2d 3e 69  nt value of p->i
7baa0 4a 44 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72  JD as the number
7bab0 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 73 65 63   of.      ** sec
7bac0 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37 30 2e  onds since 1970.
7bad0 20 20 43 6f 6e 76 65 72 74 20 74 6f 20 61 20 72    Convert to a r
7bae0 65 61 6c 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e  eal julian day n
7baf0 75 6d 62 65 72 2e 0a 20 20 20 20 20 20 2a 2f 0a  umber..      */.
7bb00 20 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70        if( strcmp
7bb10 28 7a 2c 20 22 75 6e 69 78 65 70 6f 63 68 22 29  (z, "unixepoch")
7bb20 3d 3d 30 20 26 26 20 70 2d 3e 76 61 6c 69 64 4a  ==0 && p->validJ
7bb30 44 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  D ){.        p->
7bb40 69 4a 44 20 3d 20 28 70 2d 3e 69 4a 44 20 2b 20  iJD = (p->iJD + 
7bb50 34 33 32 30 30 29 2f 38 36 34 30 30 20 2b 20 32  43200)/86400 + 2
7bb60 31 30 38 36 36 37 36 2a 28 69 36 34 29 31 30 30  1086676*(i64)100
7bb70 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 63  00000;.        c
7bb80 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70  learYMD_HMS_TZ(p
7bb90 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
7bba0 30 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64  0;.      }.#ifnd
7bbb0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  ef SQLITE_OMIT_L
7bbc0 4f 43 41 4c 54 49 4d 45 0a 20 20 20 20 20 20 65  OCALTIME.      e
7bbd0 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a  lse if( strcmp(z
7bbe0 2c 20 22 75 74 63 22 29 3d 3d 30 20 29 7b 0a 20  , "utc")==0 ){. 
7bbf0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69         sqlite3_i
7bc00 6e 74 36 34 20 63 31 3b 0a 20 20 20 20 20 20 20  nt64 c1;.       
7bc10 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20   computeJD(p);. 
7bc20 20 20 20 20 20 20 20 63 31 20 3d 20 6c 6f 63 61         c1 = loca
7bc30 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b 0a  ltimeOffset(p);.
7bc40 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d          p->iJD -
7bc50 3d 20 63 31 3b 0a 20 20 20 20 20 20 20 20 63 6c  = c1;.        cl
7bc60 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29  earYMD_HMS_TZ(p)
7bc70 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44  ;.        p->iJD
7bc80 20 2b 3d 20 63 31 20 2d 20 6c 6f 63 61 6c 74 69   += c1 - localti
7bc90 6d 65 4f 66 66 73 65 74 28 70 29 3b 0a 20 20 20  meOffset(p);.   
7bca0 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20       rc = 0;.   
7bcb0 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
7bcc0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
7bcd0 20 20 20 63 61 73 65 20 27 77 27 3a 20 7b 0a 20     case 'w': {. 
7bce0 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a       /*.      **
7bcf0 20 20 20 20 77 65 65 6b 64 61 79 20 4e 0a 20 20      weekday N.  
7bd00 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
7bd10 4d 6f 76 65 20 74 68 65 20 64 61 74 65 20 74 6f  Move the date to
7bd20 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 6f   the same time o
7bd30 6e 20 74 68 65 20 6e 65 78 74 20 6f 63 63 75 72  n the next occur
7bd40 72 65 6e 63 65 20 6f 66 0a 20 20 20 20 20 20 2a  rence of.      *
7bd50 2a 20 77 65 65 6b 64 61 79 20 4e 20 77 68 65 72  * weekday N wher
7bd60 65 20 30 3d 3d 53 75 6e 64 61 79 2c 20 31 3d 3d  e 0==Sunday, 1==
7bd70 4d 6f 6e 64 61 79 2c 20 61 6e 64 20 73 6f 20 66  Monday, and so f
7bd80 6f 72 74 68 2e 20 20 49 66 20 74 68 65 0a 20 20  orth.  If the.  
7bd90 20 20 20 20 2a 2a 20 64 61 74 65 20 69 73 20 61      ** date is a
7bda0 6c 72 65 61 64 79 20 6f 6e 20 74 68 65 20 61 70  lready on the ap
7bdb0 70 72 6f 70 72 69 61 74 65 20 77 65 65 6b 64 61  propriate weekda
7bdc0 79 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d  y, this is a no-
7bdd0 6f 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  op..      */.   
7bde0 20 20 20 69 66 28 20 73 74 72 6e 63 6d 70 28 7a     if( strncmp(z
7bdf0 2c 20 22 77 65 65 6b 64 61 79 20 22 2c 20 38 29  , "weekday ", 8)
7be00 3d 3d 30 20 26 26 20 67 65 74 56 61 6c 75 65 28  ==0 && getValue(
7be10 26 7a 5b 38 5d 2c 26 72 29 3e 30 0a 20 20 20 20  &z[8],&r)>0.    
7be20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20               && 
7be30 28 6e 3d 28 69 6e 74 29 72 29 3d 3d 72 20 26 26  (n=(int)r)==r &&
7be40 20 6e 3e 3d 30 20 26 26 20 72 3c 37 20 29 7b 0a   n>=0 && r<7 ){.
7be50 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
7be60 69 6e 74 36 34 20 5a 3b 0a 20 20 20 20 20 20 20  int64 Z;.       
7be70 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28   computeYMD_HMS(
7be80 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76  p);.        p->v
7be90 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20  alidTZ = 0;.    
7bea0 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d      p->validJD =
7beb0 20 30 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70   0;.        comp
7bec0 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20  uteJD(p);.      
7bed0 20 20 5a 20 3d 20 28 28 70 2d 3e 69 4a 44 20 2b    Z = ((p->iJD +
7bee0 20 31 32 39 36 30 30 30 30 30 29 2f 38 36 34 30   129600000)/8640
7bef0 30 30 30 30 29 20 25 20 37 3b 0a 20 20 20 20 20  0000) % 7;.     
7bf00 20 20 20 69 66 28 20 5a 3e 6e 20 29 20 5a 20 2d     if( Z>n ) Z -
7bf10 3d 20 37 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  = 7;.        p->
7bf20 69 4a 44 20 2b 3d 20 28 6e 20 2d 20 5a 29 2a 38  iJD += (n - Z)*8
7bf30 36 34 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20  6400000;.       
7bf40 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a   clearYMD_HMS_TZ
7bf50 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  (p);.        rc 
7bf60 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
7bf70 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
7bf80 20 20 20 20 63 61 73 65 20 27 73 27 3a 20 7b 0a      case 's': {.
7bf90 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a        /*.      *
7bfa0 2a 20 20 20 20 73 74 61 72 74 20 6f 66 20 54 54  *    start of TT
7bfb0 54 54 54 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  TTT.      **.   
7bfc0 20 20 20 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64     ** Move the d
7bfd0 61 74 65 20 62 61 63 6b 77 61 72 64 73 20 74 6f  ate backwards to
7bfe0 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
7bff0 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61  f the current da
7c000 79 2c 0a 20 20 20 20 20 20 2a 2a 20 6f 72 20 6d  y,.      ** or m
7c010 6f 6e 74 68 20 6f 72 20 79 65 61 72 2e 0a 20 20  onth or year..  
7c020 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
7c030 20 73 74 72 6e 63 6d 70 28 7a 2c 20 22 73 74 61   strncmp(z, "sta
7c040 72 74 20 6f 66 20 22 2c 20 39 29 21 3d 30 20 29  rt of ", 9)!=0 )
7c050 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7a 20   break;.      z 
7c060 2b 3d 20 39 3b 0a 20 20 20 20 20 20 63 6f 6d 70  += 9;.      comp
7c070 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 20 20 20  uteYMD(p);.     
7c080 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31   p->validHMS = 1
7c090 3b 0a 20 20 20 20 20 20 70 2d 3e 68 20 3d 20 70  ;.      p->h = p
7c0a0 2d 3e 6d 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  ->m = 0;.      p
7c0b0 2d 3e 73 20 3d 20 30 2e 30 3b 0a 20 20 20 20 20  ->s = 0.0;.     
7c0c0 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b   p->validTZ = 0;
7c0d0 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a  .      p->validJ
7c0e0 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28  D = 0;.      if(
7c0f0 20 73 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68   strcmp(z,"month
7c100 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ")==0 ){.       
7c110 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 20 20 20   p->D = 1;.     
7c120 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20     rc = 0;.     
7c130 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d   }else if( strcm
7c140 70 28 7a 2c 22 79 65 61 72 22 29 3d 3d 30 20 29  p(z,"year")==0 )
7c150 7b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74  {.        comput
7c160 65 59 4d 44 28 70 29 3b 0a 20 20 20 20 20 20 20  eYMD(p);.       
7c170 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 20 20   p->M = 1;.     
7c180 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 20     p->D = 1;.   
7c190 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20       rc = 0;.   
7c1a0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72     }else if( str
7c1b0 63 6d 70 28 7a 2c 22 64 61 79 22 29 3d 3d 30 20  cmp(z,"day")==0 
7c1c0 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
7c1d0 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
7c1e0 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
7c1f0 20 20 63 61 73 65 20 27 2b 27 3a 0a 20 20 20 20    case '+':.    
7c200 63 61 73 65 20 27 2d 27 3a 0a 20 20 20 20 63 61  case '-':.    ca
7c210 73 65 20 27 30 27 3a 0a 20 20 20 20 63 61 73 65  se '0':.    case
7c220 20 27 31 27 3a 0a 20 20 20 20 63 61 73 65 20 27   '1':.    case '
7c230 32 27 3a 0a 20 20 20 20 63 61 73 65 20 27 33 27  2':.    case '3'
7c240 3a 0a 20 20 20 20 63 61 73 65 20 27 34 27 3a 0a  :.    case '4':.
7c250 20 20 20 20 63 61 73 65 20 27 35 27 3a 0a 20 20      case '5':.  
7c260 20 20 63 61 73 65 20 27 36 27 3a 0a 20 20 20 20    case '6':.    
7c270 63 61 73 65 20 27 37 27 3a 0a 20 20 20 20 63 61  case '7':.    ca
7c280 73 65 20 27 38 27 3a 0a 20 20 20 20 63 61 73 65  se '8':.    case
7c290 20 27 39 27 3a 20 7b 0a 20 20 20 20 20 20 64 6f   '9': {.      do
7c2a0 75 62 6c 65 20 72 52 6f 75 6e 64 65 72 3b 0a 20  uble rRounder;. 
7c2b0 20 20 20 20 20 6e 20 3d 20 67 65 74 56 61 6c 75       n = getValu
7c2c0 65 28 7a 2c 20 26 72 29 3b 0a 20 20 20 20 20 20  e(z, &r);.      
7c2d0 61 73 73 65 72 74 28 20 6e 3e 3d 31 20 29 3b 0a  assert( n>=1 );.
7c2e0 20 20 20 20 20 20 69 66 28 20 7a 5b 6e 5d 3d 3d        if( z[n]==
7c2f0 27 3a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ':' ){.        /
7c300 2a 20 41 20 6d 6f 64 69 66 69 65 72 20 6f 66 20  * A modifier of 
7c310 74 68 65 20 66 6f 72 6d 20 28 2b 7c 2d 29 48 48  the form (+|-)HH
7c320 3a 4d 4d 3a 53 53 2e 46 46 46 20 61 64 64 73 20  :MM:SS.FFF adds 
7c330 28 6f 72 20 73 75 62 74 72 61 63 74 73 29 20 74  (or subtracts) t
7c340 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 70  he.        ** sp
7c350 65 63 69 66 69 65 64 20 6e 75 6d 62 65 72 20 6f  ecified number o
7c360 66 20 68 6f 75 72 73 2c 20 6d 69 6e 75 74 65 73  f hours, minutes
7c370 2c 20 73 65 63 6f 6e 64 73 2c 20 61 6e 64 20 66  , seconds, and f
7c380 72 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64  ractional second
7c390 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20  s.        ** to 
7c3a0 74 68 65 20 74 69 6d 65 2e 20 20 54 68 65 20 22  the time.  The "
7c3b0 2e 46 46 46 22 20 6d 61 79 20 62 65 20 6f 6d 69  .FFF" may be omi
7c3c0 74 74 65 64 2e 20 20 54 68 65 20 22 3a 53 53 2e  tted.  The ":SS.
7c3d0 46 46 46 22 20 6d 61 79 20 62 65 0a 20 20 20 20  FFF" may be.    
7c3e0 20 20 20 20 2a 2a 20 6f 6d 69 74 74 65 64 2e 0a      ** omitted..
7c3f0 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
7c400 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
7c410 32 20 3d 20 7a 3b 0a 20 20 20 20 20 20 20 20 44  2 = z;.        D
7c420 61 74 65 54 69 6d 65 20 74 78 3b 0a 20 20 20 20  ateTime tx;.    
7c430 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36      sqlite3_int6
7c440 34 20 64 61 79 3b 0a 20 20 20 20 20 20 20 20 69  4 day;.        i
7c450 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67  f( !sqlite3Isdig
7c460 69 74 28 2a 7a 32 29 20 29 20 7a 32 2b 2b 3b 0a  it(*z2) ) z2++;.
7c470 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26          memset(&
7c480 74 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 74 78  tx, 0, sizeof(tx
7c490 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ));.        if( 
7c4a0 70 61 72 73 65 48 68 4d 6d 53 73 28 7a 32 2c 20  parseHhMmSs(z2, 
7c4b0 26 74 78 29 20 29 20 62 72 65 61 6b 3b 0a 20 20  &tx) ) break;.  
7c4c0 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28        computeJD(
7c4d0 26 74 78 29 3b 0a 20 20 20 20 20 20 20 20 74 78  &tx);.        tx
7c4e0 2e 69 4a 44 20 2d 3d 20 34 33 32 30 30 30 30 30  .iJD -= 43200000
7c4f0 3b 0a 20 20 20 20 20 20 20 20 64 61 79 20 3d 20  ;.        day = 
7c500 74 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 3b  tx.iJD/86400000;
7c510 0a 20 20 20 20 20 20 20 20 74 78 2e 69 4a 44 20  .        tx.iJD 
7c520 2d 3d 20 64 61 79 2a 38 36 34 30 30 30 30 30 3b  -= day*86400000;
7c530 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b 30  .        if( z[0
7c540 5d 3d 3d 27 2d 27 20 29 20 74 78 2e 69 4a 44 20  ]=='-' ) tx.iJD 
7c550 3d 20 2d 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20  = -tx.iJD;.     
7c560 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b     computeJD(p);
7c570 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d  .        clearYM
7c580 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20  D_HMS_TZ(p);.   
7c590 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 74       p->iJD += t
7c5a0 78 2e 69 4a 44 3b 0a 20 20 20 20 20 20 20 20 72  x.iJD;.        r
7c5b0 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62  c = 0;.        b
7c5c0 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
7c5d0 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20 20 20 20      z += n;.    
7c5e0 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33    while( sqlite3
7c5f0 49 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b  Isspace(*z) ) z+
7c600 2b 3b 0a 20 20 20 20 20 20 6e 20 3d 20 73 71 6c  +;.      n = sql
7c610 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b  ite3Strlen30(z);
7c620 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 31 30 20  .      if( n>10 
7c630 7c 7c 20 6e 3c 33 20 29 20 62 72 65 61 6b 3b 0a  || n<3 ) break;.
7c640 20 20 20 20 20 20 69 66 28 20 7a 5b 6e 2d 31 5d        if( z[n-1]
7c650 3d 3d 27 73 27 20 29 7b 20 7a 5b 6e 2d 31 5d 20  =='s' ){ z[n-1] 
7c660 3d 20 30 3b 20 6e 2d 2d 3b 20 7d 0a 20 20 20 20  = 0; n--; }.    
7c670 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a    computeJD(p);.
7c680 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20        rc = 0;.  
7c690 20 20 20 20 72 52 6f 75 6e 64 65 72 20 3d 20 72      rRounder = r
7c6a0 3c 30 20 3f 20 2d 30 2e 35 20 3a 20 2b 30 2e 35  <0 ? -0.5 : +0.5
7c6b0 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3d 3d 33  ;.      if( n==3
7c6c0 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 64 61   && strcmp(z,"da
7c6d0 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  y")==0 ){.      
7c6e0 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c    p->iJD += (sql
7c6f0 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36  ite3_int64)(r*86
7c700 34 30 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e  400000.0 + rRoun
7c710 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  der);.      }els
7c720 65 20 69 66 28 20 6e 3d 3d 34 20 26 26 20 73 74  e if( n==4 && st
7c730 72 63 6d 70 28 7a 2c 22 68 6f 75 72 22 29 3d 3d  rcmp(z,"hour")==
7c740 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  0 ){.        p->
7c750 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f  iJD += (sqlite3_
7c760 69 6e 74 36 34 29 28 72 2a 28 38 36 34 30 30 30  int64)(r*(864000
7c770 30 30 2e 30 2f 32 34 2e 30 29 20 2b 20 72 52 6f  00.0/24.0) + rRo
7c780 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65  under);.      }e
7c790 6c 73 65 20 69 66 28 20 6e 3d 3d 36 20 26 26 20  lse if( n==6 && 
7c7a0 73 74 72 63 6d 70 28 7a 2c 22 6d 69 6e 75 74 65  strcmp(z,"minute
7c7b0 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ")==0 ){.       
7c7c0 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69   p->iJD += (sqli
7c7d0 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36  te3_int64)(r*(86
7c7e0 34 30 30 30 30 30 2e 30 2f 28 32 34 2e 30 2a 36  400000.0/(24.0*6
7c7f0 30 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 65 72  0.0)) + rRounder
7c800 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  );.      }else i
7c810 66 28 20 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d  f( n==6 && strcm
7c820 70 28 7a 2c 22 73 65 63 6f 6e 64 22 29 3d 3d 30  p(z,"second")==0
7c830 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69   ){.        p->i
7c840 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69  JD += (sqlite3_i
7c850 6e 74 36 34 29 28 72 2a 28 38 36 34 30 30 30 30  nt64)(r*(8640000
7c860 30 2e 30 2f 28 32 34 2e 30 2a 36 30 2e 30 2a 36  0.0/(24.0*60.0*6
7c870 30 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 65 72  0.0)) + rRounder
7c880 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  );.      }else i
7c890 66 28 20 6e 3d 3d 35 20 26 26 20 73 74 72 63 6d  f( n==5 && strcm
7c8a0 70 28 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20  p(z,"month")==0 
7c8b0 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78  ){.        int x
7c8c0 2c 20 79 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d  , y;.        com
7c8d0 70 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a  puteYMD_HMS(p);.
7c8e0 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 2b 3d 20          p->M += 
7c8f0 28 69 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20  (int)r;.        
7c900 78 20 3d 20 70 2d 3e 4d 3e 30 20 3f 20 28 70 2d  x = p->M>0 ? (p-
7c910 3e 4d 2d 31 29 2f 31 32 20 3a 20 28 70 2d 3e 4d  >M-1)/12 : (p->M
7c920 2d 31 32 29 2f 31 32 3b 0a 20 20 20 20 20 20 20  -12)/12;.       
7c930 20 70 2d 3e 59 20 2b 3d 20 78 3b 0a 20 20 20 20   p->Y += x;.    
7c940 20 20 20 20 70 2d 3e 4d 20 2d 3d 20 78 2a 31 32      p->M -= x*12
7c950 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c  ;.        p->val
7c960 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20  idJD = 0;.      
7c970 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a    computeJD(p);.
7c980 20 20 20 20 20 20 20 20 79 20 3d 20 28 69 6e 74          y = (int
7c990 29 72 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  )r;.        if( 
7c9a0 79 21 3d 72 20 29 7b 0a 20 20 20 20 20 20 20 20  y!=r ){.        
7c9b0 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c    p->iJD += (sql
7c9c0 69 74 65 33 5f 69 6e 74 36 34 29 28 28 72 20 2d  ite3_int64)((r -
7c9d0 20 79 29 2a 33 30 2e 30 2a 38 36 34 30 30 30 30   y)*30.0*8640000
7c9e0 30 2e 30 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b  0.0 + rRounder);
7c9f0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
7ca00 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20   }else if( n==4 
7ca10 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 79 65 61  && strcmp(z,"yea
7ca20 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  r")==0 ){.      
7ca30 20 20 69 6e 74 20 79 20 3d 20 28 69 6e 74 29 72    int y = (int)r
7ca40 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74  ;.        comput
7ca50 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20  eYMD_HMS(p);.   
7ca60 20 20 20 20 20 70 2d 3e 59 20 2b 3d 20 79 3b 0a       p->Y += y;.
7ca70 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64          p->valid
7ca80 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  JD = 0;.        
7ca90 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20  computeJD(p);.  
7caa0 20 20 20 20 20 20 69 66 28 20 79 21 3d 72 20 29        if( y!=r )
7cab0 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69  {.          p->i
7cac0 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69  JD += (sqlite3_i
7cad0 6e 74 36 34 29 28 28 72 20 2d 20 79 29 2a 33 36  nt64)((r - y)*36
7cae0 35 2e 30 2a 38 36 34 30 30 30 30 30 2e 30 20 2b  5.0*86400000.0 +
7caf0 20 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20   rRounder);.    
7cb00 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
7cb10 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  e{.        rc = 
7cb20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
7cb30 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a   clearYMD_HMS_TZ
7cb40 28 70 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  (p);.      break
7cb50 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
7cb60 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 62 72 65  ult: {.      bre
7cb70 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ak;.    }.  }.  
7cb80 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
7cb90 0a 2a 2a 20 50 72 6f 63 65 73 73 20 74 69 6d 65  .** Process time
7cba0 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65   function argume
7cbb0 6e 74 73 2e 20 20 61 72 67 76 5b 30 5d 20 69 73  nts.  argv[0] is
7cbc0 20 61 20 64 61 74 65 2d 74 69 6d 65 20 73 74 61   a date-time sta
7cbd0 6d 70 2e 0a 2a 2a 20 61 72 67 76 5b 31 5d 20 61  mp..** argv[1] a
7cbe0 6e 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65  nd following are
7cbf0 20 6d 6f 64 69 66 69 65 72 73 2e 20 20 50 61 72   modifiers.  Par
7cc00 73 65 20 74 68 65 6d 20 61 6c 6c 20 61 6e 64 20  se them all and 
7cc10 77 72 69 74 65 0a 2a 2a 20 74 68 65 20 72 65 73  write.** the res
7cc20 75 6c 74 69 6e 67 20 74 69 6d 65 20 69 6e 74 6f  ulting time into
7cc30 20 74 68 65 20 44 61 74 65 54 69 6d 65 20 73 74   the DateTime st
7cc40 72 75 63 74 75 72 65 20 70 2e 20 20 52 65 74 75  ructure p.  Retu
7cc50 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65  rn 0.** on succe
7cc60 73 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 72  ss and 1 if ther
7cc70 65 20 61 72 65 20 61 6e 79 20 65 72 72 6f 72 73  e are any errors
7cc80 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65  ..**.** If there
7cc90 20 61 72 65 20 7a 65 72 6f 20 70 61 72 61 6d 65   are zero parame
7cca0 74 65 72 73 20 28 69 66 20 65 76 65 6e 20 61 72  ters (if even ar
7ccb0 67 76 5b 30 5d 20 69 73 20 75 6e 64 65 66 69 6e  gv[0] is undefin
7ccc0 65 64 29 0a 2a 2a 20 74 68 65 6e 20 61 73 73 75  ed).** then assu
7ccd0 6d 65 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c  me a default val
7cce0 75 65 20 6f 66 20 22 6e 6f 77 22 20 66 6f 72 20  ue of "now" for 
7ccf0 61 72 67 76 5b 30 5d 2e 0a 2a 2f 0a 73 74 61 74  argv[0]..*/.stat
7cd00 69 63 20 69 6e 74 20 69 73 44 61 74 65 28 0a 20  ic int isDate(. 
7cd10 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
7cd20 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e   *context, .  in
7cd30 74 20 61 72 67 63 2c 20 0a 20 20 73 71 6c 69 74  t argc, .  sqlit
7cd40 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 2c  e3_value **argv,
7cd50 20 0a 20 20 44 61 74 65 54 69 6d 65 20 2a 70 0a   .  DateTime *p.
7cd60 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f  ){.  int i;.  co
7cd70 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
7cd80 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 65 54 79 70  r *z;.  int eTyp
7cd90 65 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30  e;.  memset(p, 0
7cda0 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20  , sizeof(*p));. 
7cdb0 20 69 66 28 20 61 72 67 63 3d 3d 30 20 29 7b 0a   if( argc==0 ){.
7cdc0 20 20 20 20 73 65 74 44 61 74 65 54 69 6d 65 54      setDateTimeT
7cdd0 6f 43 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74  oCurrent(context
7cde0 2c 20 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  , p);.  }else if
7cdf0 28 20 28 65 54 79 70 65 20 3d 20 73 71 6c 69 74  ( (eType = sqlit
7ce00 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
7ce10 67 76 5b 30 5d 29 29 3d 3d 53 51 4c 49 54 45 5f  gv[0]))==SQLITE_
7ce20 46 4c 4f 41 54 0a 20 20 20 20 20 20 20 20 20 20  FLOAT.          
7ce30 20 20 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70           || eTyp
7ce40 65 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  e==SQLITE_INTEGE
7ce50 52 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 20  R ){.    p->iJD 
7ce60 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  = (sqlite3_int64
7ce70 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  )(sqlite3_value_
7ce80 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 2a  double(argv[0])*
7ce90 38 36 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35  86400000.0 + 0.5
7cea0 29 3b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a  );.    p->validJ
7ceb0 44 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  D = 1;.  }else{.
7cec0 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f      z = sqlite3_
7ced0 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
7cee0 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 20  0]);.    if( !z 
7cef0 7c 7c 20 70 61 72 73 65 44 61 74 65 4f 72 54 69  || parseDateOrTi
7cf00 6d 65 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61  me(context, (cha
7cf10 72 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 20 20 20  r*)z, p) ){.    
7cf20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
7cf30 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 3b  }.  }.  for(i=1;
7cf40 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20   i<argc; i++){. 
7cf50 20 20 20 69 66 28 20 28 7a 20 3d 20 73 71 6c 69     if( (z = sqli
7cf60 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
7cf70 72 67 76 5b 69 5d 29 29 3d 3d 30 20 7c 7c 20 70  rgv[i]))==0 || p
7cf80 61 72 73 65 4d 6f 64 69 66 69 65 72 28 28 63 68  arseModifier((ch
7cf90 61 72 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 20 20  ar*)z, p) ){.   
7cfa0 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
7cfb0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
7cfc0 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  0;.}.../*.** The
7cfd0 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69   following routi
7cfe0 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68  nes implement th
7cff0 65 20 76 61 72 69 6f 75 73 20 64 61 74 65 20 61  e various date a
7d000 6e 64 20 74 69 6d 65 20 66 75 6e 63 74 69 6f 6e  nd time function
7d010 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a  s.** of SQLite..
7d020 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 6a 75 6c  */../*.**    jul
7d030 69 61 6e 64 61 79 28 20 54 49 4d 45 53 54 52 49  ianday( TIMESTRI
7d040 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e  NG, MOD, MOD, ..
7d050 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  .).**.** Return 
7d060 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e  the julian day n
7d070 75 6d 62 65 72 20 6f 66 20 74 68 65 20 64 61 74  umber of the dat
7d080 65 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74  e specified in t
7d090 68 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2f 0a  he arguments.*/.
7d0a0 73 74 61 74 69 63 20 76 6f 69 64 20 6a 75 6c 69  static void juli
7d0b0 61 6e 64 61 79 46 75 6e 63 28 0a 20 20 73 71 6c  andayFunc(.  sql
7d0c0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
7d0d0 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
7d0e0 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
7d0f0 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44  ue **argv.){.  D
7d100 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28  ateTime x;.  if(
7d110 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c   isDate(context,
7d120 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29   argc, argv, &x)
7d130 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6d 70 75  ==0 ){.    compu
7d140 74 65 4a 44 28 26 78 29 3b 0a 20 20 20 20 73 71  teJD(&x);.    sq
7d150 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75  lite3_result_dou
7d160 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 78 2e 69  ble(context, x.i
7d170 4a 44 2f 38 36 34 30 30 30 30 30 2e 30 29 3b 0a  JD/86400000.0);.
7d180 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20    }.}../*.**    
7d190 64 61 74 65 74 69 6d 65 28 20 54 49 4d 45 53 54  datetime( TIMEST
7d1a0 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20  RING, MOD, MOD, 
7d1b0 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ...).**.** Retur
7d1c0 6e 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a  n YYYY-MM-DD HH:
7d1d0 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 63 20  MM:SS.*/.static 
7d1e0 76 6f 69 64 20 64 61 74 65 74 69 6d 65 46 75 6e  void datetimeFun
7d1f0 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
7d200 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
7d210 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
7d220 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
7d230 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20  v.){.  DateTime 
7d240 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28  x;.  if( isDate(
7d250 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61  context, argc, a
7d260 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20  rgv, &x)==0 ){. 
7d270 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30     char zBuf[100
7d280 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 59 4d  ];.    computeYM
7d290 44 5f 48 4d 53 28 26 78 29 3b 0a 20 20 20 20 73  D_HMS(&x);.    s
7d2a0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
7d2b0 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42  sizeof(zBuf), zB
7d2c0 75 66 2c 20 22 25 30 34 64 2d 25 30 32 64 2d 25  uf, "%04d-%02d-%
7d2d0 30 32 64 20 25 30 32 64 3a 25 30 32 64 3a 25 30  02d %02d:%02d:%0
7d2e0 32 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  2d",.           
7d2f0 20 20 20 20 20 20 20 20 20 20 78 2e 59 2c 20 78            x.Y, x
7d300 2e 4d 2c 20 78 2e 44 2c 20 78 2e 68 2c 20 78 2e  .M, x.D, x.h, x.
7d310 6d 2c 20 28 69 6e 74 29 28 78 2e 73 29 29 3b 0a  m, (int)(x.s));.
7d320 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
7d330 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
7d340 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54   zBuf, -1, SQLIT
7d350 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  E_TRANSIENT);.  
7d360 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 74 69  }.}../*.**    ti
7d370 6d 65 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20  me( TIMESTRING, 
7d380 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a  MOD, MOD, ...).*
7d390 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 48 48 3a 4d  *.** Return HH:M
7d3a0 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76  M:SS.*/.static v
7d3b0 6f 69 64 20 74 69 6d 65 46 75 6e 63 28 0a 20 20  oid timeFunc(.  
7d3c0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
7d3d0 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
7d3e0 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
7d3f0 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
7d400 20 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20    DateTime x;.  
7d410 69 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65  if( isDate(conte
7d420 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20  xt, argc, argv, 
7d430 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68  &x)==0 ){.    ch
7d440 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20  ar zBuf[100];.  
7d450 20 20 63 6f 6d 70 75 74 65 48 4d 53 28 26 78 29    computeHMS(&x)
7d460 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  ;.    sqlite3_sn
7d470 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42  printf(sizeof(zB
7d480 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 30 32 64  uf), zBuf, "%02d
7d490 3a 25 30 32 64 3a 25 30 32 64 22 2c 20 78 2e 68  :%02d:%02d", x.h
7d4a0 2c 20 78 2e 6d 2c 20 28 69 6e 74 29 78 2e 73 29  , x.m, (int)x.s)
7d4b0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
7d4c0 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
7d4d0 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c  t, zBuf, -1, SQL
7d4e0 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
7d4f0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20    }.}../*.**    
7d500 64 61 74 65 28 20 54 49 4d 45 53 54 52 49 4e 47  date( TIMESTRING
7d510 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29  , MOD, MOD, ...)
7d520 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 59 59  .**.** Return YY
7d530 59 59 2d 4d 4d 2d 44 44 0a 2a 2f 0a 73 74 61 74  YY-MM-DD.*/.stat
7d540 69 63 20 76 6f 69 64 20 64 61 74 65 46 75 6e 63  ic void dateFunc
7d550 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
7d560 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
7d570 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
7d580 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
7d590 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78  .){.  DateTime x
7d5a0 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63  ;.  if( isDate(c
7d5b0 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72  ontext, argc, ar
7d5c0 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20  gv, &x)==0 ){.  
7d5d0 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d    char zBuf[100]
7d5e0 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44  ;.    computeYMD
7d5f0 28 26 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  (&x);.    sqlite
7d600 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
7d610 66 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22  f(zBuf), zBuf, "
7d620 25 30 34 64 2d 25 30 32 64 2d 25 30 32 64 22 2c  %04d-%02d-%02d",
7d630 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44 29 3b   x.Y, x.M, x.D);
7d640 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
7d650 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
7d660 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49  , zBuf, -1, SQLI
7d670 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
7d680 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 73   }.}../*.**    s
7d690 74 72 66 74 69 6d 65 28 20 46 4f 52 4d 41 54 2c  trftime( FORMAT,
7d6a0 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44   TIMESTRING, MOD
7d6b0 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a  , MOD, ...).**.*
7d6c0 2a 20 52 65 74 75 72 6e 20 61 20 73 74 72 69 6e  * Return a strin
7d6d0 67 20 64 65 73 63 72 69 62 65 64 20 62 79 20 46  g described by F
7d6e0 4f 52 4d 41 54 2e 20 20 43 6f 6e 76 65 72 73 69  ORMAT.  Conversi
7d6f0 6f 6e 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  ons as follows:.
7d700 2a 2a 0a 2a 2a 20 20 20 25 64 20 20 64 61 79 20  **.**   %d  day 
7d710 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20 25 66  of month.**   %f
7d720 20 20 2a 2a 20 66 72 61 63 74 69 6f 6e 61 6c 20    ** fractional 
7d730 73 65 63 6f 6e 64 73 20 20 53 53 2e 53 53 53 0a  seconds  SS.SSS.
7d740 2a 2a 20 20 20 25 48 20 20 68 6f 75 72 20 30 30  **   %H  hour 00
7d750 2d 32 34 0a 2a 2a 20 20 20 25 6a 20 20 64 61 79  -24.**   %j  day
7d760 20 6f 66 20 79 65 61 72 20 30 30 30 2d 33 36 36   of year 000-366
7d770 0a 2a 2a 20 20 20 25 4a 20 20 2a 2a 20 4a 75 6c  .**   %J  ** Jul
7d780 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 0a 2a  ian day number.*
7d790 2a 20 20 20 25 6d 20 20 6d 6f 6e 74 68 20 30 31  *   %m  month 01
7d7a0 2d 31 32 0a 2a 2a 20 20 20 25 4d 20 20 6d 69 6e  -12.**   %M  min
7d7b0 75 74 65 20 30 30 2d 35 39 0a 2a 2a 20 20 20 25  ute 00-59.**   %
7d7c0 73 20 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65  s  seconds since
7d7d0 20 31 39 37 30 2d 30 31 2d 30 31 0a 2a 2a 20 20   1970-01-01.**  
7d7e0 20 25 53 20 20 73 65 63 6f 6e 64 73 20 30 30 2d   %S  seconds 00-
7d7f0 35 39 0a 2a 2a 20 20 20 25 77 20 20 64 61 79 20  59.**   %w  day 
7d800 6f 66 20 77 65 65 6b 20 30 2d 36 20 20 73 75 6e  of week 0-6  sun
7d810 64 61 79 3d 3d 30 0a 2a 2a 20 20 20 25 57 20 20  day==0.**   %W  
7d820 77 65 65 6b 20 6f 66 20 79 65 61 72 20 30 30 2d  week of year 00-
7d830 35 33 0a 2a 2a 20 20 20 25 59 20 20 79 65 61 72  53.**   %Y  year
7d840 20 30 30 30 30 2d 39 39 39 39 0a 2a 2a 20 20 20   0000-9999.**   
7d850 25 25 20 20 25 0a 2a 2f 0a 73 74 61 74 69 63 20  %%  %.*/.static 
7d860 76 6f 69 64 20 73 74 72 66 74 69 6d 65 46 75 6e  void strftimeFun
7d870 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
7d880 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
7d890 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
7d8a0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
7d8b0 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20  v.){.  DateTime 
7d8c0 78 3b 0a 20 20 75 36 34 20 6e 3b 0a 20 20 73 69  x;.  u64 n;.  si
7d8d0 7a 65 5f 74 20 69 2c 6a 3b 0a 20 20 63 68 61 72  ze_t i,j;.  char
7d8e0 20 2a 7a 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a   *z;.  sqlite3 *
7d8f0 64 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  db;.  const char
7d900 20 2a 7a 46 6d 74 20 3d 20 28 63 6f 6e 73 74 20   *zFmt = (const 
7d910 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61  char*)sqlite3_va
7d920 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
7d930 29 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 31  );.  char zBuf[1
7d940 30 30 5d 3b 0a 20 20 69 66 28 20 7a 46 6d 74 3d  00];.  if( zFmt=
7d950 3d 30 20 7c 7c 20 69 73 44 61 74 65 28 63 6f 6e  =0 || isDate(con
7d960 74 65 78 74 2c 20 61 72 67 63 2d 31 2c 20 61 72  text, argc-1, ar
7d970 67 76 2b 31 2c 20 26 78 29 20 29 20 72 65 74 75  gv+1, &x) ) retu
7d980 72 6e 3b 0a 20 20 64 62 20 3d 20 73 71 6c 69 74  rn;.  db = sqlit
7d990 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
7d9a0 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20  ndle(context);. 
7d9b0 20 66 6f 72 28 69 3d 30 2c 20 6e 3d 31 3b 20 7a   for(i=0, n=1; z
7d9c0 46 6d 74 5b 69 5d 3b 20 69 2b 2b 2c 20 6e 2b 2b  Fmt[i]; i++, n++
7d9d0 29 7b 0a 20 20 20 20 69 66 28 20 7a 46 6d 74 5b  ){.    if( zFmt[
7d9e0 69 5d 3d 3d 27 25 27 20 29 7b 0a 20 20 20 20 20  i]=='%' ){.     
7d9f0 20 73 77 69 74 63 68 28 20 7a 46 6d 74 5b 69 2b   switch( zFmt[i+
7da00 31 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61  1] ){.        ca
7da10 73 65 20 27 64 27 3a 0a 20 20 20 20 20 20 20 20  se 'd':.        
7da20 63 61 73 65 20 27 48 27 3a 0a 20 20 20 20 20 20  case 'H':.      
7da30 20 20 63 61 73 65 20 27 6d 27 3a 0a 20 20 20 20    case 'm':.    
7da40 20 20 20 20 63 61 73 65 20 27 4d 27 3a 0a 20 20      case 'M':.  
7da50 20 20 20 20 20 20 63 61 73 65 20 27 53 27 3a 0a        case 'S':.
7da60 20 20 20 20 20 20 20 20 63 61 73 65 20 27 57 27          case 'W'
7da70 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 2b 2b 3b  :.          n++;
7da80 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 61  .          /* fa
7da90 6c 6c 20 74 68 72 75 20 2a 2f 0a 20 20 20 20 20  ll thru */.     
7daa0 20 20 20 63 61 73 65 20 27 77 27 3a 0a 20 20 20     case 'w':.   
7dab0 20 20 20 20 20 63 61 73 65 20 27 25 27 3a 0a 20       case '%':. 
7dac0 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
7dad0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 66 27          case 'f'
7dae0 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d  :.          n +=
7daf0 20 38 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72   8;.          br
7db00 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
7db10 65 20 27 6a 27 3a 0a 20 20 20 20 20 20 20 20 20  e 'j':.         
7db20 20 6e 20 2b 3d 20 33 3b 0a 20 20 20 20 20 20 20   n += 3;.       
7db30 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
7db40 20 20 63 61 73 65 20 27 59 27 3a 0a 20 20 20 20    case 'Y':.    
7db50 20 20 20 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20        n += 8;.  
7db60 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
7db70 20 20 20 20 20 20 20 63 61 73 65 20 27 73 27 3a         case 's':
7db80 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4a  .        case 'J
7db90 27 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b  ':.          n +
7dba0 3d 20 35 30 3b 0a 20 20 20 20 20 20 20 20 20 20  = 50;.          
7dbb0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 64  break;.        d
7dbc0 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20  efault:.        
7dbd0 20 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 45 52    return;  /* ER
7dbe0 52 4f 52 2e 20 20 72 65 74 75 72 6e 20 61 20 4e  ROR.  return a N
7dbf0 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20  ULL */.      }. 
7dc00 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 7d 0a       i++;.    }.
7dc10 20 20 7d 0a 20 20 74 65 73 74 63 61 73 65 28 20    }.  testcase( 
7dc20 6e 3d 3d 73 69 7a 65 6f 66 28 7a 42 75 66 29 2d  n==sizeof(zBuf)-
7dc30 31 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  1 );.  testcase(
7dc40 20 6e 3d 3d 73 69 7a 65 6f 66 28 7a 42 75 66 29   n==sizeof(zBuf)
7dc50 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
7dc60 6e 3d 3d 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d  n==(u64)db->aLim
7dc70 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
7dc80 4c 45 4e 47 54 48 5d 2b 31 20 29 3b 0a 20 20 74  LENGTH]+1 );.  t
7dc90 65 73 74 63 61 73 65 28 20 6e 3d 3d 28 75 36 34  estcase( n==(u64
7dca0 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  )db->aLimit[SQLI
7dcb0 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
7dcc0 20 29 3b 0a 20 20 69 66 28 20 6e 3c 73 69 7a 65   );.  if( n<size
7dcd0 6f 66 28 7a 42 75 66 29 20 29 7b 0a 20 20 20 20  of(zBuf) ){.    
7dce0 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 7d 65 6c 73  z = zBuf;.  }els
7dcf0 65 20 69 66 28 20 6e 3e 28 75 36 34 29 64 62 2d  e if( n>(u64)db-
7dd00 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
7dd10 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a  IMIT_LENGTH] ){.
7dd20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
7dd30 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28  lt_error_toobig(
7dd40 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 72 65  context);.    re
7dd50 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  turn;.  }else{. 
7dd60 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 44 62     z = sqlite3Db
7dd70 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 28 69  MallocRaw(db, (i
7dd80 6e 74 29 6e 29 3b 0a 20 20 20 20 69 66 28 20 7a  nt)n);.    if( z
7dd90 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
7dda0 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
7ddb0 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29  r_nomem(context)
7ddc0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
7ddd0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 63 6f 6d 70      }.  }.  comp
7dde0 75 74 65 4a 44 28 26 78 29 3b 0a 20 20 63 6f 6d  uteJD(&x);.  com
7ddf0 70 75 74 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b  puteYMD_HMS(&x);
7de00 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 7a 46  .  for(i=j=0; zF
7de10 6d 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20  mt[i]; i++){.   
7de20 20 69 66 28 20 7a 46 6d 74 5b 69 5d 21 3d 27 25   if( zFmt[i]!='%
7de30 27 20 29 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b  ' ){.      z[j++
7de40 5d 20 3d 20 7a 46 6d 74 5b 69 5d 3b 0a 20 20 20  ] = zFmt[i];.   
7de50 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 2b   }else{.      i+
7de60 2b 3b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28  +;.      switch(
7de70 20 7a 46 6d 74 5b 69 5d 20 29 7b 0a 20 20 20 20   zFmt[i] ){.    
7de80 20 20 20 20 63 61 73 65 20 27 64 27 3a 20 20 73      case 'd':  s
7de90 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
7dea0 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c  3, &z[j],"%02d",
7deb0 78 2e 44 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61  x.D); j+=2; brea
7dec0 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k;.        case 
7ded0 27 66 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20  'f': {.         
7dee0 20 64 6f 75 62 6c 65 20 73 20 3d 20 78 2e 73 3b   double s = x.s;
7def0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73  .          if( s
7df00 3e 35 39 2e 39 39 39 20 29 20 73 20 3d 20 35 39  >59.999 ) s = 59
7df10 2e 39 39 39 3b 0a 20 20 20 20 20 20 20 20 20 20  .999;.          
7df20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
7df30 28 37 2c 20 26 7a 5b 6a 5d 2c 22 25 30 36 2e 33  (7, &z[j],"%06.3
7df40 66 22 2c 20 73 29 3b 0a 20 20 20 20 20 20 20 20  f", s);.        
7df50 20 20 6a 20 2b 3d 20 73 71 6c 69 74 65 33 53 74    j += sqlite3St
7df60 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20  rlen30(&z[j]);. 
7df70 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
7df80 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
7df90 20 20 63 61 73 65 20 27 48 27 3a 20 20 73 71 6c    case 'H':  sql
7dfa0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c  ite3_snprintf(3,
7dfb0 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e   &z[j],"%02d",x.
7dfc0 68 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b  h); j+=2; break;
7dfd0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 57  .        case 'W
7dfe0 27 3a 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20  ': /* Fall thru 
7dff0 2a 2f 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  */.        case 
7e000 27 6a 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20  'j': {.         
7e010 20 69 6e 74 20 6e 44 61 79 3b 20 20 20 20 20 20   int nDay;      
7e020 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
7e030 20 6f 66 20 64 61 79 73 20 73 69 6e 63 65 20 31   of days since 1
7e040 73 74 20 64 61 79 20 6f 66 20 79 65 61 72 20 2a  st day of year *
7e050 2f 0a 20 20 20 20 20 20 20 20 20 20 44 61 74 65  /.          Date
7e060 54 69 6d 65 20 79 20 3d 20 78 3b 0a 20 20 20 20  Time y = x;.    
7e070 20 20 20 20 20 20 79 2e 76 61 6c 69 64 4a 44 20        y.validJD 
7e080 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 79  = 0;.          y
7e090 2e 4d 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  .M = 1;.        
7e0a0 20 20 79 2e 44 20 3d 20 31 3b 0a 20 20 20 20 20    y.D = 1;.     
7e0b0 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26       computeJD(&
7e0c0 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 44  y);.          nD
7e0d0 61 79 20 3d 20 28 69 6e 74 29 28 28 78 2e 69 4a  ay = (int)((x.iJ
7e0e0 44 2d 79 2e 69 4a 44 2b 34 33 32 30 30 30 30 30  D-y.iJD+43200000
7e0f0 29 2f 38 36 34 30 30 30 30 30 29 3b 0a 20 20 20  )/86400000);.   
7e100 20 20 20 20 20 20 20 69 66 28 20 7a 46 6d 74 5b         if( zFmt[
7e110 69 5d 3d 3d 27 57 27 20 29 7b 0a 20 20 20 20 20  i]=='W' ){.     
7e120 20 20 20 20 20 20 20 69 6e 74 20 77 64 3b 20 20         int wd;  
7e130 20 2f 2a 20 30 3d 4d 6f 6e 64 61 79 2c 20 31 3d   /* 0=Monday, 1=
7e140 54 75 65 73 64 61 79 2c 20 2e 2e 2e 20 36 3d 53  Tuesday, ... 6=S
7e150 75 6e 64 61 79 20 2a 2f 0a 20 20 20 20 20 20 20  unday */.       
7e160 20 20 20 20 20 77 64 20 3d 20 28 69 6e 74 29 28       wd = (int)(
7e170 28 28 78 2e 69 4a 44 2b 34 33 32 30 30 30 30 30  ((x.iJD+43200000
7e180 29 2f 38 36 34 30 30 30 30 30 29 25 37 29 3b 0a  )/86400000)%7);.
7e190 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
7e1a0 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20  te3_snprintf(3, 
7e1b0 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 28 6e 44  &z[j],"%02d",(nD
7e1c0 61 79 2b 37 2d 77 64 29 2f 37 29 3b 0a 20 20 20  ay+7-wd)/7);.   
7e1d0 20 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 32 3b           j += 2;
7e1e0 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
7e1f0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  {.            sq
7e200 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 34  lite3_snprintf(4
7e210 2c 20 26 7a 5b 6a 5d 2c 22 25 30 33 64 22 2c 6e  , &z[j],"%03d",n
7e220 44 61 79 2b 31 29 3b 0a 20 20 20 20 20 20 20 20  Day+1);.        
7e230 20 20 20 20 6a 20 2b 3d 20 33 3b 0a 20 20 20 20      j += 3;.    
7e240 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
7e250 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
7e260 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20   }.        case 
7e270 27 4a 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20  'J': {.         
7e280 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
7e290 66 28 32 30 2c 20 26 7a 5b 6a 5d 2c 22 25 2e 31  f(20, &z[j],"%.1
7e2a0 36 67 22 2c 78 2e 69 4a 44 2f 38 36 34 30 30 30  6g",x.iJD/864000
7e2b0 30 30 2e 30 29 3b 0a 20 20 20 20 20 20 20 20 20  00.0);.         
7e2c0 20 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72 6c 65   j+=sqlite3Strle
7e2d0 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20  n30(&z[j]);.    
7e2e0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
7e2f0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63       }.        c
7e300 61 73 65 20 27 6d 27 3a 20 20 73 71 6c 69 74 65  ase 'm':  sqlite
7e310 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a  3_snprintf(3, &z
7e320 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 4d 29 3b  [j],"%02d",x.M);
7e330 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20   j+=2; break;.  
7e340 20 20 20 20 20 20 63 61 73 65 20 27 4d 27 3a 20        case 'M': 
7e350 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
7e360 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64  f(3, &z[j],"%02d
7e370 22 2c 78 2e 6d 29 3b 20 6a 2b 3d 32 3b 20 62 72  ",x.m); j+=2; br
7e380 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
7e390 65 20 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 20  e 's': {.       
7e3a0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
7e3b0 6e 74 66 28 33 30 2c 26 7a 5b 6a 5d 2c 22 25 6c  ntf(30,&z[j],"%l
7e3c0 6c 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ld",.           
7e3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7e3e0 28 69 36 34 29 28 78 2e 69 4a 44 2f 31 30 30 30  (i64)(x.iJD/1000
7e3f0 20 2d 20 32 31 30 38 36 36 37 36 2a 28 69 36 34   - 21086676*(i64
7e400 29 31 30 30 30 30 29 29 3b 0a 20 20 20 20 20 20  )10000));.      
7e410 20 20 20 20 6a 20 2b 3d 20 73 71 6c 69 74 65 33      j += sqlite3
7e420 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b  Strlen30(&z[j]);
7e430 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
7e440 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
7e450 20 20 20 20 63 61 73 65 20 27 53 27 3a 20 20 73      case 'S':  s
7e460 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
7e470 33 2c 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 28  3,&z[j],"%02d",(
7e480 69 6e 74 29 78 2e 73 29 3b 20 6a 2b 3d 32 3b 20  int)x.s); j+=2; 
7e490 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
7e4a0 61 73 65 20 27 77 27 3a 20 7b 0a 20 20 20 20 20  ase 'w': {.     
7e4b0 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 28 63       z[j++] = (c
7e4c0 68 61 72 29 28 28 28 78 2e 69 4a 44 2b 31 32 39  har)(((x.iJD+129
7e4d0 36 30 30 30 30 30 29 2f 38 36 34 30 30 30 30 30  600000)/86400000
7e4e0 29 20 25 20 37 29 20 2b 20 27 30 27 3b 0a 20 20  ) % 7) + '0';.  
7e4f0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
7e500 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
7e510 20 63 61 73 65 20 27 59 27 3a 20 7b 0a 20 20 20   case 'Y': {.   
7e520 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73         sqlite3_s
7e530 6e 70 72 69 6e 74 66 28 35 2c 26 7a 5b 6a 5d 2c  nprintf(5,&z[j],
7e540 22 25 30 34 64 22 2c 78 2e 59 29 3b 20 6a 2b 3d  "%04d",x.Y); j+=
7e550 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
7e560 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20  &z[j]);.        
7e570 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
7e580 20 7d 0a 20 20 20 20 20 20 20 20 64 65 66 61 75   }.        defau
7e590 6c 74 3a 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27  lt:   z[j++] = '
7e5a0 25 27 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  %'; break;.     
7e5b0 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 7a   }.    }.  }.  z
7e5c0 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  [j] = 0;.  sqlit
7e5d0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63  e3_result_text(c
7e5e0 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d 31 2c 0a 20  ontext, z, -1,. 
7e5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7e600 20 20 20 20 20 7a 3d 3d 7a 42 75 66 20 3f 20 53       z==zBuf ? S
7e610 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20  QLITE_TRANSIENT 
7e620 3a 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43  : SQLITE_DYNAMIC
7e630 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72  );.}../*.** curr
7e640 65 6e 74 5f 74 69 6d 65 28 29 0a 2a 2a 0a 2a 2a  ent_time().**.**
7e650 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   This function r
7e660 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20  eturns the same 
7e670 76 61 6c 75 65 20 61 73 20 74 69 6d 65 28 27 6e  value as time('n
7e680 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ow')..*/.static 
7e690 76 6f 69 64 20 63 74 69 6d 65 46 75 6e 63 28 0a  void ctimeFunc(.
7e6a0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
7e6b0 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
7e6c0 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c  t NotUsed,.  sql
7e6d0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74  ite3_value **Not
7e6e0 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45  Used2.){.  UNUSE
7e6f0 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74  D_PARAMETER2(Not
7e700 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b  Used, NotUsed2);
7e710 0a 20 20 74 69 6d 65 46 75 6e 63 28 63 6f 6e 74  .  timeFunc(cont
7e720 65 78 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f  ext, 0, 0);.}../
7e730 2a 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 64 61 74  *.** current_dat
7e740 65 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  e().**.** This f
7e750 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
7e760 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61  the same value a
7e770 73 20 64 61 74 65 28 27 6e 6f 77 27 29 2e 0a 2a  s date('now')..*
7e780 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 64  /.static void cd
7e790 61 74 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  ateFunc(.  sqlit
7e7a0 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
7e7b0 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73  ext,.  int NotUs
7e7c0 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  ed,.  sqlite3_va
7e7d0 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29  lue **NotUsed2.)
7e7e0 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
7e7f0 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e  ETER2(NotUsed, N
7e800 6f 74 55 73 65 64 32 29 3b 0a 20 20 64 61 74 65  otUsed2);.  date
7e810 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c  Func(context, 0,
7e820 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75   0);.}../*.** cu
7e830 72 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 28  rrent_timestamp(
7e840 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ).**.** This fun
7e850 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68  ction returns th
7e860 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73 20  e same value as 
7e870 64 61 74 65 74 69 6d 65 28 27 6e 6f 77 27 29 2e  datetime('now').
7e880 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
7e890 63 74 69 6d 65 73 74 61 6d 70 46 75 6e 63 28 0a  ctimestampFunc(.
7e8a0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
7e8b0 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
7e8c0 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c  t NotUsed,.  sql
7e8d0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74  ite3_value **Not
7e8e0 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45  Used2.){.  UNUSE
7e8f0 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74  D_PARAMETER2(Not
7e900 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b  Used, NotUsed2);
7e910 0a 20 20 64 61 74 65 74 69 6d 65 46 75 6e 63 28  .  datetimeFunc(
7e920 63 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a  context, 0, 0);.
7e930 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  }.#endif /* !def
7e940 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
7e950 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 29  _DATETIME_FUNCS)
7e960 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   */..#ifdef SQLI
7e970 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45  TE_OMIT_DATETIME
7e980 5f 46 55 4e 43 53 0a 2f 2a 0a 2a 2a 20 49 66 20  _FUNCS./*.** If 
7e990 74 68 65 20 6c 69 62 72 61 72 79 20 69 73 20 63  the library is c
7e9a0 6f 6d 70 69 6c 65 64 20 74 6f 20 6f 6d 69 74 20  ompiled to omit 
7e9b0 74 68 65 20 66 75 6c 6c 2d 73 63 61 6c 65 20 64  the full-scale d
7e9c0 61 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a 2a 20  ate and time.** 
7e9d0 68 61 6e 64 6c 69 6e 67 20 28 74 6f 20 67 65 74  handling (to get
7e9e0 20 61 20 73 6d 61 6c 6c 65 72 20 62 69 6e 61 72   a smaller binar
7e9f0 79 29 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  y), the followin
7ea00 67 20 6d 69 6e 69 6d 61 6c 20 76 65 72 73 69 6f  g minimal versio
7ea10 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 66 75 6e 63  n.** of the func
7ea20 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 5f 74 69  tions current_ti
7ea30 6d 65 28 29 2c 20 63 75 72 72 65 6e 74 5f 64 61  me(), current_da
7ea40 74 65 28 29 20 61 6e 64 20 63 75 72 72 65 6e 74  te() and current
7ea50 5f 74 69 6d 65 73 74 61 6d 70 28 29 0a 2a 2a 20  _timestamp().** 
7ea60 61 72 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 73  are included ins
7ea70 74 65 61 64 2e 20 54 68 69 73 20 69 73 20 74 6f  tead. This is to
7ea80 20 73 75 70 70 6f 72 74 20 63 6f 6c 75 6d 6e 20   support column 
7ea90 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 74 68 61  declarations tha
7eaa0 74 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 22 44 45  t.** include "DE
7eab0 46 41 55 4c 54 20 43 55 52 52 45 4e 54 5f 54 49  FAULT CURRENT_TI
7eac0 4d 45 22 20 65 74 63 2e 0a 2a 2a 0a 2a 2a 20 54  ME" etc..**.** T
7ead0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 75 73 65  his function use
7eae0 73 20 74 68 65 20 43 2d 6c 69 62 72 61 72 79 20  s the C-library 
7eaf0 66 75 6e 63 74 69 6f 6e 73 20 74 69 6d 65 28 29  functions time()
7eb00 2c 20 67 6d 74 69 6d 65 28 29 0a 2a 2a 20 61 6e  , gmtime().** an
7eb10 64 20 73 74 72 66 74 69 6d 65 28 29 2e 20 54 68  d strftime(). Th
7eb20 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20  e format string 
7eb30 74 6f 20 70 61 73 73 20 74 6f 20 73 74 72 66 74  to pass to strft
7eb40 69 6d 65 28 29 20 69 73 20 73 75 70 70 6c 69 65  ime() is supplie
7eb50 64 0a 2a 2a 20 61 73 20 74 68 65 20 75 73 65 72  d.** as the user
7eb60 2d 64 61 74 61 20 66 6f 72 20 74 68 65 20 66 75  -data for the fu
7eb70 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  nction..*/.stati
7eb80 63 20 76 6f 69 64 20 63 75 72 72 65 6e 74 54 69  c void currentTi
7eb90 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  meFunc(.  sqlite
7eba0 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
7ebb0 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
7ebc0 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
7ebd0 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 74 69 6d 65  **argv.){.  time
7ebe0 5f 74 20 74 3b 0a 20 20 63 68 61 72 20 2a 7a 46  _t t;.  char *zF
7ebf0 6f 72 6d 61 74 20 3d 20 28 63 68 61 72 20 2a 29  ormat = (char *)
7ec00 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74  sqlite3_user_dat
7ec10 61 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71  a(context);.  sq
7ec20 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 64 6f 75  lite3 *db;.  dou
7ec30 62 6c 65 20 72 54 3b 0a 20 20 63 68 61 72 20 7a  ble rT;.  char z
7ec40 42 75 66 5b 32 30 5d 3b 0a 0a 20 20 55 4e 55 53  Buf[20];..  UNUS
7ec50 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67  ED_PARAMETER(arg
7ec60 63 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  c);.  UNUSED_PAR
7ec70 41 4d 45 54 45 52 28 61 72 67 76 29 3b 0a 0a 20  AMETER(argv);.. 
7ec80 20 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f   db = sqlite3_co
7ec90 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
7eca0 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69  context);.  sqli
7ecb0 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65  te3OsCurrentTime
7ecc0 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 54 29 3b  (db->pVfs, &rT);
7ecd0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
7ece0 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
7ecf0 49 4e 54 0a 20 20 74 20 3d 20 38 36 34 30 30 2e  INT.  t = 86400.
7ed00 30 2a 28 72 54 20 2d 20 32 34 34 30 35 38 37 2e  0*(rT - 2440587.
7ed10 35 29 20 2b 20 30 2e 35 3b 0a 23 65 6c 73 65 0a  5) + 0.5;.#else.
7ed20 20 20 2f 2a 20 77 69 74 68 6f 75 74 20 66 6c 6f    /* without flo
7ed30 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70  ating point supp
7ed40 6f 72 74 2c 20 72 54 20 77 69 6c 6c 20 68 61 76  ort, rT will hav
7ed50 65 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 6c  e.  ** already l
7ed60 6f 73 74 20 66 72 61 63 74 69 6f 6e 61 6c 20 64  ost fractional d
7ed70 61 79 20 70 72 65 63 69 73 69 6f 6e 2e 0a 20 20  ay precision..  
7ed80 2a 2f 0a 20 20 74 20 3d 20 38 36 34 30 30 20 2a  */.  t = 86400 *
7ed90 20 28 72 54 20 2d 20 32 34 34 30 35 38 37 29 20   (rT - 2440587) 
7eda0 2d 20 34 33 32 30 30 3b 0a 23 65 6e 64 69 66 0a  - 43200;.#endif.
7edb0 23 69 66 64 65 66 20 48 41 56 45 5f 47 4d 54 49  #ifdef HAVE_GMTI
7edc0 4d 45 5f 52 0a 20 20 7b 0a 20 20 20 20 73 74 72  ME_R.  {.    str
7edd0 75 63 74 20 74 6d 20 73 4e 6f 77 3b 0a 20 20 20  uct tm sNow;.   
7ede0 20 67 6d 74 69 6d 65 5f 72 28 26 74 2c 20 26 73   gmtime_r(&t, &s
7edf0 4e 6f 77 29 3b 0a 20 20 20 20 73 74 72 66 74 69  Now);.    strfti
7ee00 6d 65 28 7a 42 75 66 2c 20 32 30 2c 20 7a 46 6f  me(zBuf, 20, zFo
7ee10 72 6d 61 74 2c 20 26 73 4e 6f 77 29 3b 0a 20 20  rmat, &sNow);.  
7ee20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a 20 20 20 20  }.#else.  {.    
7ee30 73 74 72 75 63 74 20 74 6d 20 2a 70 54 6d 3b 0a  struct tm *pTm;.
7ee40 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
7ee50 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d  x_enter(sqlite3M
7ee60 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
7ee70 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
7ee80 53 54 45 52 29 29 3b 0a 20 20 20 20 70 54 6d 20  STER));.    pTm 
7ee90 3d 20 67 6d 74 69 6d 65 28 26 74 29 3b 0a 20 20  = gmtime(&t);.  
7eea0 20 20 73 74 72 66 74 69 6d 65 28 7a 42 75 66 2c    strftime(zBuf,
7eeb0 20 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 70 54   20, zFormat, pT
7eec0 6d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  m);.    sqlite3_
7eed0 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69  mutex_leave(sqli
7eee0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
7eef0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
7ef00 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 7d 0a  C_MASTER));.  }.
7ef10 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65  #endif..  sqlite
7ef20 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
7ef30 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c  ntext, zBuf, -1,
7ef40 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
7ef50 54 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  T);.}.#endif../*
7ef60 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
7ef70 6e 20 72 65 67 69 73 74 65 72 65 64 20 61 6c 6c  n registered all
7ef80 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 43 20   of the above C 
7ef90 66 75 6e 63 74 69 6f 6e 73 20 61 73 20 53 51 4c  functions as SQL
7efa0 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20  .** functions.  
7efb0 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 74  This should be t
7efc0 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20  he only routine 
7efd0 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 74  in this file wit
7efe0 68 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 6c 69  h.** external li
7eff0 6e 6b 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  nkage..*/.SQLITE
7f000 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
7f010 6c 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74  lite3RegisterDat
7f020 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 76  eTimeFunctions(v
7f030 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 53  oid){.  static S
7f040 51 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65  QLITE_WSD FuncDe
7f050 66 20 61 44 61 74 65 54 69 6d 65 46 75 6e 63 73  f aDateTimeFuncs
7f060 5b 5d 20 3d 20 7b 0a 23 69 66 6e 64 65 66 20 53  [] = {.#ifndef S
7f070 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54  QLITE_OMIT_DATET
7f080 49 4d 45 5f 46 55 4e 43 53 0a 20 20 20 20 46 55  IME_FUNCS.    FU
7f090 4e 43 54 49 4f 4e 28 6a 75 6c 69 61 6e 64 61 79  NCTION(julianday
7f0a0 2c 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20  ,        -1, 0, 
7f0b0 30 2c 20 6a 75 6c 69 61 6e 64 61 79 46 75 6e 63  0, juliandayFunc
7f0c0 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
7f0d0 28 64 61 74 65 2c 20 20 20 20 20 20 20 20 20 20  (date,          
7f0e0 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 64 61 74     -1, 0, 0, dat
7f0f0 65 46 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20  eFunc      ),.  
7f100 20 20 46 55 4e 43 54 49 4f 4e 28 74 69 6d 65 2c    FUNCTION(time,
7f110 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c               -1,
7f120 20 30 2c 20 30 2c 20 74 69 6d 65 46 75 6e 63 20   0, 0, timeFunc 
7f130 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
7f140 54 49 4f 4e 28 64 61 74 65 74 69 6d 65 2c 20 20  TION(datetime,  
7f150 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c         -1, 0, 0,
7f160 20 64 61 74 65 74 69 6d 65 46 75 6e 63 20 20 29   datetimeFunc  )
7f170 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73  ,.    FUNCTION(s
7f180 74 72 66 74 69 6d 65 2c 20 20 20 20 20 20 20 20  trftime,        
7f190 20 2d 31 2c 20 30 2c 20 30 2c 20 73 74 72 66 74   -1, 0, 0, strft
7f1a0 69 6d 65 46 75 6e 63 20 20 29 2c 0a 20 20 20 20  imeFunc  ),.    
7f1b0 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74  FUNCTION(current
7f1c0 5f 74 69 6d 65 2c 20 20 20 20 20 20 30 2c 20 30  _time,      0, 0
7f1d0 2c 20 30 2c 20 63 74 69 6d 65 46 75 6e 63 20 20  , 0, ctimeFunc  
7f1e0 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
7f1f0 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 73  ON(current_times
7f200 74 61 6d 70 2c 20 30 2c 20 30 2c 20 30 2c 20 63  tamp, 0, 0, 0, c
7f210 74 69 6d 65 73 74 61 6d 70 46 75 6e 63 29 2c 0a  timestampFunc),.
7f220 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72      FUNCTION(cur
7f230 72 65 6e 74 5f 64 61 74 65 2c 20 20 20 20 20 20  rent_date,      
7f240 30 2c 20 30 2c 20 30 2c 20 63 64 61 74 65 46 75  0, 0, 0, cdateFu
7f250 6e 63 20 20 20 20 20 29 2c 0a 23 65 6c 73 65 0a  nc     ),.#else.
7f260 20 20 20 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e      STR_FUNCTION
7f270 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 2c 20 20  (current_time,  
7f280 20 20 20 20 30 2c 20 22 25 48 3a 25 4d 3a 25 53      0, "%H:%M:%S
7f290 22 2c 20 20 20 20 20 20 20 20 20 20 30 2c 20 63  ",          0, c
7f2a0 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 29 2c  urrentTimeFunc),
7f2b0 0a 20 20 20 20 53 54 52 5f 46 55 4e 43 54 49 4f  .    STR_FUNCTIO
7f2c0 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74  N(current_timest
7f2d0 61 6d 70 2c 20 30 2c 20 22 25 59 2d 25 6d 2d 25  amp, 0, "%Y-%m-%
7f2e0 64 22 2c 20 20 20 20 20 20 20 20 20 20 30 2c 20  d",          0, 
7f2f0 63 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 29  currentTimeFunc)
7f300 2c 0a 20 20 20 20 53 54 52 5f 46 55 4e 43 54 49  ,.    STR_FUNCTI
7f310 4f 4e 28 63 75 72 72 65 6e 74 5f 64 61 74 65 2c  ON(current_date,
7f320 20 20 20 20 20 20 30 2c 20 22 25 59 2d 25 6d 2d        0, "%Y-%m-
7f330 25 64 20 25 48 3a 25 4d 3a 25 53 22 2c 20 30 2c  %d %H:%M:%S", 0,
7f340 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63   currentTimeFunc
7f350 29 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 20  ),.#endif.  };. 
7f360 20 69 6e 74 20 69 3b 0a 20 20 46 75 6e 63 44 65   int i;.  FuncDe
7f370 66 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26  fHash *pHash = &
7f380 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 48 61  GLOBAL(FuncDefHa
7f390 73 68 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  sh, sqlite3Globa
7f3a0 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a 20 20 46  lFunctions);.  F
7f3b0 75 6e 63 44 65 66 20 2a 61 46 75 6e 63 20 3d 20  uncDef *aFunc = 
7f3c0 28 46 75 6e 63 44 65 66 2a 29 26 47 4c 4f 42 41  (FuncDef*)&GLOBA
7f3d0 4c 28 46 75 6e 63 44 65 66 2c 20 61 44 61 74 65  L(FuncDef, aDate
7f3e0 54 69 6d 65 46 75 6e 63 73 29 3b 0a 0a 20 20 66  TimeFuncs);..  f
7f3f0 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 53  or(i=0; i<ArrayS
7f400 69 7a 65 28 61 44 61 74 65 54 69 6d 65 46 75 6e  ize(aDateTimeFun
7f410 63 73 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  cs); i++){.    s
7f420 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73  qlite3FuncDefIns
7f430 65 72 74 28 70 48 61 73 68 2c 20 26 61 46 75 6e  ert(pHash, &aFun
7f440 63 5b 69 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  c[i]);.  }.}../*
7f450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
7f460 64 20 6f 66 20 64 61 74 65 2e 63 20 2a 2a 2a 2a  d of date.c ****
7f470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7f480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7f490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
7f4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
7f4b0 67 69 6e 20 66 69 6c 65 20 6f 73 2e 63 20 2a 2a  gin file os.c **
7f4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7f4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7f4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
7f4f0 0a 2a 2a 20 32 30 30 35 20 4e 6f 76 65 6d 62 65  .** 2005 Novembe
7f500 72 20 32 39 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 29.**.** The a
7f510 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
7f520 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
7f530 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
7f540 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
7f550 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
7f560 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
7f570 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
7f580 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
7f590 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
7f5a0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
7f5b0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
7f5c0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
7f5d0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
7f5e0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
7f5f0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
7f600 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
7f610 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
7f620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7f630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7f640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7f650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7f660 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
7f670 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
7f680 73 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20 63  s OS interface c
7f690 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d  ode that is comm
7f6a0 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a 20 61 72 63  on to all.** arc
7f6b0 68 69 74 65 63 74 75 72 65 73 2e 0a 2a 2a 0a 2a  hitectures..**.*
7f6c0 2a 20 24 49 64 3a 20 6f 73 2e 63 2c 76 20 31 2e  * $Id: os.c,v 1.
7f6d0 31 32 37 20 32 30 30 39 2f 30 37 2f 32 37 20 31  127 2009/07/27 1
7f6e0 31 3a 34 31 3a 32 31 20 64 61 6e 69 65 6c 6b 31  1:41:21 danielk1
7f6f0 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 64 65  977 Exp $.*/.#de
7f700 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 4f 53 5f  fine _SQLITE_OS_
7f710 43 5f 20 31 0a 23 75 6e 64 65 66 20 5f 53 51 4c  C_ 1.#undef _SQL
7f720 49 54 45 5f 4f 53 5f 43 5f 0a 0a 2f 2a 0a 2a 2a  ITE_OS_C_../*.**
7f730 20 54 68 65 20 64 65 66 61 75 6c 74 20 53 51 4c   The default SQL
7f740 69 74 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20  ite sqlite3_vfs 
7f750 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
7f760 64 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 0a  do not allocate.
7f770 2a 2a 20 6d 65 6d 6f 72 79 20 28 61 63 74 75 61  ** memory (actua
7f780 6c 6c 79 2c 20 6f 73 5f 75 6e 69 78 2e 63 20 61  lly, os_unix.c a
7f790 6c 6c 6f 63 61 74 65 73 20 61 20 73 6d 61 6c 6c  llocates a small
7f7a0 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72   amount of memor
7f7b0 79 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69 6e  y.** from within
7f7c0 20 4f 73 4f 70 65 6e 28 29 29 2c 20 62 75 74 20   OsOpen()), but 
7f7d0 73 6f 6d 65 20 74 68 69 72 64 2d 70 61 72 74 79  some third-party
7f7e0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
7f7f0 20 6d 61 79 2e 0a 2a 2a 20 53 6f 20 77 65 20 74   may..** So we t
7f800 65 73 74 20 74 68 65 20 65 66 66 65 63 74 73 20  est the effects 
7f810 6f 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61  of a malloc() fa
7f820 69 6c 69 6e 67 20 61 6e 64 20 74 68 65 20 73 71  iling and the sq
7f830 6c 69 74 65 33 4f 73 58 58 58 28 29 0a 2a 2a 20  lite3OsXXX().** 
7f840 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 69  function returni
7f850 6e 67 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  ng SQLITE_IOERR_
7f860 4e 4f 4d 45 4d 20 75 73 69 6e 67 20 74 68 65 20  NOMEM using the 
7f870 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53  DO_OS_MALLOC_TES
7f880 54 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54  T macro..**.** T
7f890 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e  he following fun
7f8a0 63 74 69 6f 6e 73 20 61 72 65 20 69 6e 73 74 72  ctions are instr
7f8b0 75 6d 65 6e 74 65 64 20 66 6f 72 20 6d 61 6c 6c  umented for mall
7f8c0 6f 63 28 29 20 66 61 69 6c 75 72 65 20 0a 2a 2a  oc() failure .**
7f8d0 20 74 65 73 74 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20   testing:.**.** 
7f8e0 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65      sqlite3OsOpe
7f8f0 6e 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  n().**     sqlit
7f900 65 33 4f 73 52 65 61 64 28 29 0a 2a 2a 20 20 20  e3OsRead().**   
7f910 20 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65    sqlite3OsWrite
7f920 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65  ().**     sqlite
7f930 33 4f 73 53 79 6e 63 28 29 0a 2a 2a 20 20 20 20  3OsSync().**    
7f940 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29   sqlite3OsLock()
7f950 0a 2a 2a 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e  .**.*/.#if defin
7f960 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20  ed(SQLITE_TEST) 
7f970 26 26 20 28 53 51 4c 49 54 45 5f 4f 53 5f 57 49  && (SQLITE_OS_WI
7f980 4e 3d 3d 30 29 0a 20 20 23 64 65 66 69 6e 65 20  N==0).  #define 
7f990 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53  DO_OS_MALLOC_TES
7f9a0 54 28 78 29 20 69 66 20 28 21 78 20 7c 7c 20 21  T(x) if (!x || !
7f9b0 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72  sqlite3IsMemJour
7f9c0 6e 61 6c 28 78 29 29 20 7b 20 20 20 20 20 5c 0a  nal(x)) {     \.
7f9d0 20 20 20 20 76 6f 69 64 20 2a 70 54 73 74 41 6c      void *pTstAl
7f9e0 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  loc = sqlite3Mal
7f9f0 6c 6f 63 28 31 30 29 3b 20 20 20 20 20 20 20 20  loc(10);        
7fa00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7fa10 20 20 20 20 20 5c 0a 20 20 20 20 69 66 20 28 21       \.    if (!
7fa20 70 54 73 74 41 6c 6c 6f 63 29 20 72 65 74 75 72  pTstAlloc) retur
7fa30 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e  n SQLITE_IOERR_N
7fa40 4f 4d 45 4d 3b 20 20 20 20 20 20 20 20 20 20 20  OMEM;           
7fa50 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
7fa60 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
7fa70 54 73 74 41 6c 6c 6f 63 29 3b 20 20 20 20 20 20  TstAlloc);      
7fa80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7fa90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7faa0 20 20 20 5c 0a 20 20 7d 0a 23 65 6c 73 65 0a 20     \.  }.#else. 
7fab0 20 23 64 65 66 69 6e 65 20 44 4f 5f 4f 53 5f 4d   #define DO_OS_M
7fac0 41 4c 4c 4f 43 5f 54 45 53 54 28 78 29 0a 23 65  ALLOC_TEST(x).#e
7fad0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
7fae0 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e  following routin
7faf0 65 73 20 61 72 65 20 63 6f 6e 76 65 6e 69 65 6e  es are convenien
7fb00 63 65 20 77 72 61 70 70 65 72 73 20 61 72 6f 75  ce wrappers arou
7fb10 6e 64 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66  nd methods.** of
7fb20 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c   the sqlite3_fil
7fb30 65 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 20  e object.  This 
7fb40 69 73 20 6d 6f 73 74 6c 79 20 6a 75 73 74 20 73  is mostly just s
7fb50 79 6e 74 61 63 74 69 63 20 73 75 67 61 72 2e 20  yntactic sugar. 
7fb60 41 6c 6c 0a 2a 2a 20 6f 66 20 74 68 69 73 20 77  All.** of this w
7fb70 6f 75 6c 64 20 62 65 20 63 6f 6d 70 6c 65 74 65  ould be complete
7fb80 6c 79 20 61 75 74 6f 6d 61 74 69 63 20 69 66 20  ly automatic if 
7fb90 53 51 4c 69 74 65 20 77 65 72 65 20 63 6f 64 65  SQLite were code
7fba0 64 20 75 73 69 6e 67 0a 2a 2a 20 43 2b 2b 20 69  d using.** C++ i
7fbb0 6e 73 74 65 61 64 20 6f 66 20 70 6c 61 69 6e 20  nstead of plain 
7fbc0 6f 6c 64 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54 45  old C..*/.SQLITE
7fbd0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
7fbe0 69 74 65 33 4f 73 43 6c 6f 73 65 28 73 71 6c 69  ite3OsClose(sqli
7fbf0 74 65 33 5f 66 69 6c 65 20 2a 70 49 64 29 7b 0a  te3_file *pId){.
7fc00 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
7fc10 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 49 64 2d  E_OK;.  if( pId-
7fc20 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20  >pMethods ){.   
7fc30 20 72 63 20 3d 20 70 49 64 2d 3e 70 4d 65 74 68   rc = pId->pMeth
7fc40 6f 64 73 2d 3e 78 43 6c 6f 73 65 28 70 49 64 29  ods->xClose(pId)
7fc50 3b 0a 20 20 20 20 70 49 64 2d 3e 70 4d 65 74 68  ;.    pId->pMeth
7fc60 6f 64 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72  ods = 0;.  }.  r
7fc70 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49  eturn rc;.}.SQLI
7fc80 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
7fc90 71 6c 69 74 65 33 4f 73 52 65 61 64 28 73 71 6c  qlite3OsRead(sql
7fca0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 76  ite3_file *id, v
7fcb0 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 61  oid *pBuf, int a
7fcc0 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 7b  mt, i64 offset){
7fcd0 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f  .  DO_OS_MALLOC_
7fce0 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75  TEST(id);.  retu
7fcf0 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d  rn id->pMethods-
7fd00 3e 78 52 65 61 64 28 69 64 2c 20 70 42 75 66 2c  >xRead(id, pBuf,
7fd10 20 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b 0a 7d   amt, offset);.}
7fd20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7fd30 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72 69  int sqlite3OsWri
7fd40 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  te(sqlite3_file 
7fd50 2a 69 64 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  *id, const void 
7fd60 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d 74 2c 20  *pBuf, int amt, 
7fd70 69 36 34 20 6f 66 66 73 65 74 29 7b 0a 20 20 44  i64 offset){.  D
7fd80 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54  O_OS_MALLOC_TEST
7fd90 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69  (id);.  return i
7fda0 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 57 72  d->pMethods->xWr
7fdb0 69 74 65 28 69 64 2c 20 70 42 75 66 2c 20 61 6d  ite(id, pBuf, am
7fdc0 74 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 53 51  t, offset);.}.SQ
7fdd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
7fde0 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61   sqlite3OsTrunca
7fdf0 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  te(sqlite3_file 
7fe00 2a 69 64 2c 20 69 36 34 20 73 69 7a 65 29 7b 0a  *id, i64 size){.
7fe10 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65    return id->pMe
7fe20 74 68 6f 64 73 2d 3e 78 54 72 75 6e 63 61 74 65  thods->xTruncate
7fe30 28 69 64 2c 20 73 69 7a 65 29 3b 0a 7d 0a 53 51  (id, size);.}.SQ
7fe40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
7fe50 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 73   sqlite3OsSync(s
7fe60 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
7fe70 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 44   int flags){.  D
7fe80 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54  O_OS_MALLOC_TEST
7fe90 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69  (id);.  return i
7fea0 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 79  d->pMethods->xSy
7feb0 6e 63 28 69 64 2c 20 66 6c 61 67 73 29 3b 0a 7d  nc(id, flags);.}
7fec0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7fed0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69 6c  int sqlite3OsFil
7fee0 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69  eSize(sqlite3_fi
7fef0 6c 65 20 2a 69 64 2c 20 69 36 34 20 2a 70 53 69  le *id, i64 *pSi
7ff00 7a 65 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c  ze){.  DO_OS_MAL
7ff10 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20  LOC_TEST(id);.  
7ff20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68  return id->pMeth
7ff30 6f 64 73 2d 3e 78 46 69 6c 65 53 69 7a 65 28 69  ods->xFileSize(i
7ff40 64 2c 20 70 53 69 7a 65 29 3b 0a 7d 0a 53 51 4c  d, pSize);.}.SQL
7ff50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
7ff60 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73 71  sqlite3OsLock(sq
7ff70 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
7ff80 69 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0a 20  int lockType){. 
7ff90 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45   DO_OS_MALLOC_TE
7ffa0 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e  ST(id);.  return
7ffb0 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78   id->pMethods->x
7ffc0 4c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b 54 79 70  Lock(id, lockTyp
7ffd0 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  e);.}.SQLITE_PRI
7ffe0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
7fff0 4f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33  OsUnlock(sqlite3
80000 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c  _file *id, int l
80010 6f 63 6b 54 79 70 65 29 7b 0a 20 20 72 65 74 75  ockType){.  retu
80020 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d  rn id->pMethods-
80030 3e 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 6c 6f 63  >xUnlock(id, loc
80040 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45  kType);.}.SQLITE
80050 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
80060 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72  ite3OsCheckReser
80070 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  vedLock(sqlite3_
80080 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70  file *id, int *p
80090 52 65 73 4f 75 74 29 7b 0a 20 20 44 4f 5f 4f 53  ResOut){.  DO_OS
800a0 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29  _MALLOC_TEST(id)
800b0 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70  ;.  return id->p
800c0 4d 65 74 68 6f 64 73 2d 3e 78 43 68 65 63 6b 52  Methods->xCheckR
800d0 65 73 65 72 76 65 64 4c 6f 63 6b 28 69 64 2c 20  eservedLock(id, 
800e0 70 52 65 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49  pResOut);.}.SQLI
800f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
80100 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74  qlite3OsFileCont
80110 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  rol(sqlite3_file
80120 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f   *id, int op, vo
80130 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 72 65 74  id *pArg){.  ret
80140 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73  urn id->pMethods
80150 2d 3e 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 69  ->xFileControl(i
80160 64 2c 20 6f 70 2c 20 70 41 72 67 29 3b 0a 7d 0a  d, op, pArg);.}.
80170 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
80180 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74  nt sqlite3OsSect
80190 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66  orSize(sqlite3_f
801a0 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20  ile *id){.  int 
801b0 28 2a 78 53 65 63 74 6f 72 53 69 7a 65 29 28 73  (*xSectorSize)(s
801c0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 20 3d 20  qlite3_file*) = 
801d0 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53  id->pMethods->xS
801e0 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 72 65 74  ectorSize;.  ret
801f0 75 72 6e 20 28 78 53 65 63 74 6f 72 53 69 7a 65  urn (xSectorSize
80200 20 3f 20 78 53 65 63 74 6f 72 53 69 7a 65 28 69   ? xSectorSize(i
80210 64 29 20 3a 20 53 51 4c 49 54 45 5f 44 45 46 41  d) : SQLITE_DEFA
80220 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 29  ULT_SECTOR_SIZE)
80230 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
80240 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
80250 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
80260 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69  stics(sqlite3_fi
80270 6c 65 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 72  le *id){.  retur
80280 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e  n id->pMethods->
80290 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  xDeviceCharacter
802a0 69 73 74 69 63 73 28 69 64 29 3b 0a 7d 0a 0a 2f  istics(id);.}../
802b0 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 72  *.** The next gr
802c0 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20  oup of routines 
802d0 61 72 65 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20  are convenience 
802e0 77 72 61 70 70 65 72 73 20 61 72 6f 75 6e 64 20  wrappers around 
802f0 74 68 65 0a 2a 2a 20 56 46 53 20 6d 65 74 68 6f  the.** VFS metho
80300 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ds..*/.SQLITE_PR
80310 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
80320 33 4f 73 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74  3OsOpen(.  sqlit
80330 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20  e3_vfs *pVfs, . 
80340 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61   const char *zPa
80350 74 68 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66  th, .  sqlite3_f
80360 69 6c 65 20 2a 70 46 69 6c 65 2c 20 0a 20 20 69  ile *pFile, .  i
80370 6e 74 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74  nt flags, .  int
80380 20 2a 70 46 6c 61 67 73 4f 75 74 0a 29 7b 0a 20   *pFlagsOut.){. 
80390 20 69 6e 74 20 72 63 3b 0a 20 20 44 4f 5f 4f 53   int rc;.  DO_OS
803a0 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 30 29 3b  _MALLOC_TEST(0);
803b0 0a 20 20 2f 2a 20 30 78 37 66 31 66 20 69 73 20  .  /* 0x7f1f is 
803c0 61 20 6d 61 73 6b 20 6f 66 20 53 51 4c 49 54 45  a mask of SQLITE
803d0 5f 4f 50 45 4e 5f 20 66 6c 61 67 73 20 74 68 61  _OPEN_ flags tha
803e0 74 20 61 72 65 20 76 61 6c 69 64 20 74 6f 20 62  t are valid to b
803f0 65 20 70 61 73 73 65 64 0a 20 20 2a 2a 20 64 6f  e passed.  ** do
80400 77 6e 20 69 6e 74 6f 20 74 68 65 20 56 46 53 20  wn into the VFS 
80410 6c 61 79 65 72 2e 20 20 53 6f 6d 65 20 53 51 4c  layer.  Some SQL
80420 49 54 45 5f 4f 50 45 4e 5f 20 66 6c 61 67 73 20  ITE_OPEN_ flags 
80430 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 20 20  (for example,.  
80440 2a 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46  ** SQLITE_OPEN_F
80450 55 4c 4c 4d 55 54 45 58 20 6f 72 20 53 51 4c 49  ULLMUTEX or SQLI
80460 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41  TE_OPEN_SHAREDCA
80470 43 48 45 29 20 61 72 65 20 62 6c 6f 63 6b 65 64  CHE) are blocked
80480 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 72 65 61   before.  ** rea
80490 63 68 69 6e 67 20 74 68 65 20 56 46 53 2e 20 2a  ching the VFS. *
804a0 2f 0a 20 20 72 63 20 3d 20 70 56 66 73 2d 3e 78  /.  rc = pVfs->x
804b0 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68  Open(pVfs, zPath
804c0 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 20 26  , pFile, flags &
804d0 20 30 78 37 66 31 66 2c 20 70 46 6c 61 67 73 4f   0x7f1f, pFlagsO
804e0 75 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72  ut);.  assert( r
804f0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
80500 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 73 3d  pFile->pMethods=
80510 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  =0 );.  return r
80520 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  c;.}.SQLITE_PRIV
80530 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
80540 73 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 5f  sDelete(sqlite3_
80550 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74  vfs *pVfs, const
80560 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 69 6e   char *zPath, in
80570 74 20 64 69 72 53 79 6e 63 29 7b 0a 20 20 72 65  t dirSync){.  re
80580 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 65 6c 65  turn pVfs->xDele
80590 74 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20  te(pVfs, zPath, 
805a0 64 69 72 53 79 6e 63 29 3b 0a 7d 0a 53 51 4c 49  dirSync);.}.SQLI
805b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
805c0 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 0a  qlite3OsAccess(.
805d0 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
805e0 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68  Vfs, .  const ch
805f0 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 69 6e  ar *zPath, .  in
80600 74 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74 20  t flags, .  int 
80610 2a 70 52 65 73 4f 75 74 0a 29 7b 0a 20 20 44 4f  *pResOut.){.  DO
80620 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28  _OS_MALLOC_TEST(
80630 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66  0);.  return pVf
80640 73 2d 3e 78 41 63 63 65 73 73 28 70 56 66 73 2c  s->xAccess(pVfs,
80650 20 7a 50 61 74 68 2c 20 66 6c 61 67 73 2c 20 70   zPath, flags, p
80660 52 65 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54  ResOut);.}.SQLIT
80670 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
80680 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e  lite3OsFullPathn
80690 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  ame(.  sqlite3_v
806a0 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e  fs *pVfs, .  con
806b0 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20  st char *zPath, 
806c0 0a 20 20 69 6e 74 20 6e 50 61 74 68 4f 75 74 2c  .  int nPathOut,
806d0 20 0a 20 20 63 68 61 72 20 2a 7a 50 61 74 68 4f   .  char *zPathO
806e0 75 74 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  ut.){.  return p
806f0 56 66 73 2d 3e 78 46 75 6c 6c 50 61 74 68 6e 61  Vfs->xFullPathna
80700 6d 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20  me(pVfs, zPath, 
80710 6e 50 61 74 68 4f 75 74 2c 20 7a 50 61 74 68 4f  nPathOut, zPathO
80720 75 74 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53  ut);.}.#ifndef S
80730 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f  QLITE_OMIT_LOAD_
80740 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45  EXTENSION.SQLITE
80750 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
80760 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73  qlite3OsDlOpen(s
80770 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
80780 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50  , const char *zP
80790 61 74 68 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  ath){.  return p
807a0 56 66 73 2d 3e 78 44 6c 4f 70 65 6e 28 70 56 66  Vfs->xDlOpen(pVf
807b0 73 2c 20 7a 50 61 74 68 29 3b 0a 7d 0a 53 51 4c  s, zPath);.}.SQL
807c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
807d0 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f   sqlite3OsDlErro
807e0 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  r(sqlite3_vfs *p
807f0 56 66 73 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20  Vfs, int nByte, 
80800 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a  char *zBufOut){.
80810 20 20 70 56 66 73 2d 3e 78 44 6c 45 72 72 6f 72    pVfs->xDlError
80820 28 70 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42  (pVfs, nByte, zB
80830 75 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45  ufOut);.}.SQLITE
80840 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 28 2a  _PRIVATE void (*
80850 73 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28 73  sqlite3OsDlSym(s
80860 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
80870 2c 20 76 6f 69 64 20 2a 70 48 64 6c 65 2c 20 63  , void *pHdle, c
80880 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 29  onst char *zSym)
80890 29 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72  )(void){.  retur
808a0 6e 20 70 56 66 73 2d 3e 78 44 6c 53 79 6d 28 70  n pVfs->xDlSym(p
808b0 56 66 73 2c 20 70 48 64 6c 65 2c 20 7a 53 79 6d  Vfs, pHdle, zSym
808c0 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
808d0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
808e0 4f 73 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65  OsDlClose(sqlite
808f0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69  3_vfs *pVfs, voi
80900 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 70  d *pHandle){.  p
80910 56 66 73 2d 3e 78 44 6c 43 6c 6f 73 65 28 70 56  Vfs->xDlClose(pV
80920 66 73 2c 20 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a  fs, pHandle);.}.
80930 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
80940 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e  _OMIT_LOAD_EXTEN
80950 53 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  SION */.SQLITE_P
80960 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
80970 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73  e3OsRandomness(s
80980 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
80990 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61  , int nByte, cha
809a0 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 72  r *zBufOut){.  r
809b0 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 52 61 6e  eturn pVfs->xRan
809c0 64 6f 6d 6e 65 73 73 28 70 56 66 73 2c 20 6e 42  domness(pVfs, nB
809d0 79 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d  yte, zBufOut);.}
809e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
809f0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 6c 65  int sqlite3OsSle
80a00 65 70 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  ep(sqlite3_vfs *
80a10 70 56 66 73 2c 20 69 6e 74 20 6e 4d 69 63 72 6f  pVfs, int nMicro
80a20 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73  ){.  return pVfs
80a30 2d 3e 78 53 6c 65 65 70 28 70 56 66 73 2c 20 6e  ->xSleep(pVfs, n
80a40 4d 69 63 72 6f 29 3b 0a 7d 0a 53 51 4c 49 54 45  Micro);.}.SQLITE
80a50 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
80a60 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d  ite3OsCurrentTim
80a70 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  e(sqlite3_vfs *p
80a80 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70 54 69  Vfs, double *pTi
80a90 6d 65 4f 75 74 29 7b 0a 20 20 72 65 74 75 72 6e  meOut){.  return
80aa0 20 70 56 66 73 2d 3e 78 43 75 72 72 65 6e 74 54   pVfs->xCurrentT
80ab0 69 6d 65 28 70 56 66 73 2c 20 70 54 69 6d 65 4f  ime(pVfs, pTimeO
80ac0 75 74 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50  ut);.}..SQLITE_P
80ad0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
80ae0 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 0a  e3OsOpenMalloc(.
80af0 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
80b00 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68  Vfs, .  const ch
80b10 61 72 20 2a 7a 46 69 6c 65 2c 20 0a 20 20 73 71  ar *zFile, .  sq
80b20 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2a 70 70 46  lite3_file **ppF
80b30 69 6c 65 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67  ile, .  int flag
80b40 73 2c 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c  s,.  int *pOutFl
80b50 61 67 73 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20  ags.){.  int rc 
80b60 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
80b70 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
80b80 70 46 69 6c 65 3b 0a 20 20 70 46 69 6c 65 20 3d  pFile;.  pFile =
80b90 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a   (sqlite3_file *
80ba0 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 70  )sqlite3Malloc(p
80bb0 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a  Vfs->szOsFile);.
80bc0 20 20 69 66 28 20 70 46 69 6c 65 20 29 7b 0a 20    if( pFile ){. 
80bd0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
80be0 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a 46 69 6c  sOpen(pVfs, zFil
80bf0 65 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c  e, pFile, flags,
80c00 20 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 20   pOutFlags);.   
80c10 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
80c20 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  OK ){.      sqli
80c30 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 29 3b  te3_free(pFile);
80c40 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
80c50 20 20 2a 70 70 46 69 6c 65 20 3d 20 70 46 69 6c    *ppFile = pFil
80c60 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  e;.    }.  }.  r
80c70 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49  eturn rc;.}.SQLI
80c80 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
80c90 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65  qlite3OsCloseFre
80ca0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
80cb0 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63  pFile){.  int rc
80cc0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
80cd0 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
80ce0 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f  .  rc = sqlite3O
80cf0 73 43 6c 6f 73 65 28 70 46 69 6c 65 29 3b 0a 20  sClose(pFile);. 
80d00 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46   sqlite3_free(pF
80d10 69 6c 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ile);.  return r
80d20 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
80d30 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 77   function is a w
80d40 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74 68  rapper around th
80d50 65 20 4f 53 20 73 70 65 63 69 66 69 63 20 69 6d  e OS specific im
80d60 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 0a  plementation of.
80d70 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  ** sqlite3_os_in
80d80 69 74 28 29 2e 20 54 68 65 20 70 75 72 70 6f 73  it(). The purpos
80d90 65 20 6f 66 20 74 68 65 20 77 72 61 70 70 65 72  e of the wrapper
80da0 20 69 73 20 74 6f 20 70 72 6f 76 69 64 65 20 74   is to provide t
80db0 68 65 0a 2a 2a 20 61 62 69 6c 69 74 79 20 74 6f  he.** ability to
80dc0 20 73 69 6d 75 6c 61 74 65 20 61 20 6d 61 6c 6c   simulate a mall
80dd0 6f 63 20 66 61 69 6c 75 72 65 2c 20 73 6f 20 74  oc failure, so t
80de0 68 61 74 20 74 68 65 20 68 61 6e 64 6c 69 6e 67  hat the handling
80df0 20 6f 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20   of an.** error 
80e00 69 6e 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  in sqlite3_os_in
80e10 69 74 28 29 20 62 79 20 74 68 65 20 75 70 70 65  it() by the uppe
80e20 72 20 6c 61 79 65 72 73 20 63 61 6e 20 62 65 20  r layers can be 
80e30 74 65 73 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  tested..*/.SQLIT
80e40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
80e50 6c 69 74 65 33 4f 73 49 6e 69 74 28 76 6f 69 64  lite3OsInit(void
80e60 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73  ){.  void *p = s
80e70 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 31 30  qlite3_malloc(10
80e80 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  );.  if( p==0 ) 
80e90 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
80ea0 4d 45 4d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  MEM;.  sqlite3_f
80eb0 72 65 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ree(p);.  return
80ec0 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74   sqlite3_os_init
80ed0 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ();.}../*.** The
80ee0 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 72 65 67   list of all reg
80ef0 69 73 74 65 72 65 64 20 56 46 53 20 69 6d 70 6c  istered VFS impl
80f00 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a  ementations..*/.
80f10 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76  static sqlite3_v
80f20 66 73 20 2a 20 53 51 4c 49 54 45 5f 57 53 44 20  fs * SQLITE_WSD 
80f30 76 66 73 4c 69 73 74 20 3d 20 30 3b 0a 23 64 65  vfsList = 0;.#de
80f40 66 69 6e 65 20 76 66 73 4c 69 73 74 20 47 4c 4f  fine vfsList GLO
80f50 42 41 4c 28 73 71 6c 69 74 65 33 5f 76 66 73 20  BAL(sqlite3_vfs 
80f60 2a 2c 20 76 66 73 4c 69 73 74 29 0a 0a 2f 2a 0a  *, vfsList)../*.
80f70 2a 2a 20 4c 6f 63 61 74 65 20 61 20 56 46 53 20  ** Locate a VFS 
80f80 62 79 20 6e 61 6d 65 2e 20 20 49 66 20 6e 6f 20  by name.  If no 
80f90 6e 61 6d 65 20 69 73 20 67 69 76 65 6e 2c 20 73  name is given, s
80fa0 69 6d 70 6c 79 20 72 65 74 75 72 6e 20 74 68 65  imply return the
80fb0 0a 2a 2a 20 66 69 72 73 74 20 56 46 53 20 6f 6e  .** first VFS on
80fc0 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51   the list..*/.SQ
80fd0 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
80fe0 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 5f 76 66  _vfs *sqlite3_vf
80ff0 73 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63 68 61  s_find(const cha
81000 72 20 2a 7a 56 66 73 29 7b 0a 20 20 73 71 6c 69  r *zVfs){.  sqli
81010 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20  te3_vfs *pVfs = 
81020 30 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  0;.#if SQLITE_TH
81030 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74  READSAFE.  sqlit
81040 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b  e3_mutex *mutex;
81050 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
81060 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
81070 49 4e 49 54 0a 20 20 69 6e 74 20 72 63 20 3d 20  INIT.  int rc = 
81080 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
81090 7a 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29  ze();.  if( rc )
810a0 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69   return 0;.#endi
810b0 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  f.#if SQLITE_THR
810c0 45 41 44 53 41 46 45 0a 20 20 6d 75 74 65 78 20  EADSAFE.  mutex 
810d0 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  = sqlite3MutexAl
810e0 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
810f0 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b  _STATIC_MASTER);
81100 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65  .#endif.  sqlite
81110 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75  3_mutex_enter(mu
81120 74 65 78 29 3b 0a 20 20 66 6f 72 28 70 56 66 73  tex);.  for(pVfs
81130 20 3d 20 76 66 73 4c 69 73 74 3b 20 70 56 66 73   = vfsList; pVfs
81140 3b 20 70 56 66 73 3d 70 56 66 73 2d 3e 70 4e 65  ; pVfs=pVfs->pNe
81150 78 74 29 7b 0a 20 20 20 20 69 66 28 20 7a 56 66  xt){.    if( zVf
81160 73 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20  s==0 ) break;.  
81170 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a 56 66    if( strcmp(zVf
81180 73 2c 20 70 56 66 73 2d 3e 7a 4e 61 6d 65 29 3d  s, pVfs->zName)=
81190 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  =0 ) break;.  }.
811a0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
811b0 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20  leave(mutex);.  
811c0 72 65 74 75 72 6e 20 70 56 66 73 3b 0a 7d 0a 0a  return pVfs;.}..
811d0 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 61 20 56  /*.** Unlink a V
811e0 46 53 20 66 72 6f 6d 20 74 68 65 20 6c 69 6e 6b  FS from the link
811f0 65 64 20 6c 69 73 74 0a 2a 2f 0a 73 74 61 74 69  ed list.*/.stati
81200 63 20 76 6f 69 64 20 76 66 73 55 6e 6c 69 6e 6b  c void vfsUnlink
81210 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
81220 66 73 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73  fs){.  assert( s
81230 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
81240 64 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  d(sqlite3MutexAl
81250 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
81260 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29  _STATIC_MASTER))
81270 20 29 3b 0a 20 20 69 66 28 20 70 56 66 73 3d 3d   );.  if( pVfs==
81280 30 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 2d 6f  0 ){.    /* No-o
81290 70 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28  p */.  }else if(
812a0 20 76 66 73 4c 69 73 74 3d 3d 70 56 66 73 20 29   vfsList==pVfs )
812b0 7b 0a 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20  {.    vfsList = 
812c0 70 56 66 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d  pVfs->pNext;.  }
812d0 65 6c 73 65 20 69 66 28 20 76 66 73 4c 69 73 74  else if( vfsList
812e0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
812f0 76 66 73 20 2a 70 20 3d 20 76 66 73 4c 69 73 74  vfs *p = vfsList
81300 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e  ;.    while( p->
81310 70 4e 65 78 74 20 26 26 20 70 2d 3e 70 4e 65 78  pNext && p->pNex
81320 74 21 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 20  t!=pVfs ){.     
81330 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20   p = p->pNext;. 
81340 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e     }.    if( p->
81350 70 4e 65 78 74 3d 3d 70 56 66 73 20 29 7b 0a 20  pNext==pVfs ){. 
81360 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20       p->pNext = 
81370 70 56 66 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  pVfs->pNext;.   
81380 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
81390 52 65 67 69 73 74 65 72 20 61 20 56 46 53 20 77  Register a VFS w
813a0 69 74 68 20 74 68 65 20 73 79 73 74 65 6d 2e 20  ith the system. 
813b0 20 49 74 20 69 73 20 68 61 72 6d 6c 65 73 73 20   It is harmless 
813c0 74 6f 20 72 65 67 69 73 74 65 72 20 74 68 65 20  to register the 
813d0 73 61 6d 65 0a 2a 2a 20 56 46 53 20 6d 75 6c 74  same.** VFS mult
813e0 69 70 6c 65 20 74 69 6d 65 73 2e 20 20 54 68 65  iple times.  The
813f0 20 6e 65 77 20 56 46 53 20 62 65 63 6f 6d 65 73   new VFS becomes
81400 20 74 68 65 20 64 65 66 61 75 6c 74 20 69 66 20   the default if 
81410 6d 61 6b 65 44 66 6c 74 20 69 73 0a 2a 2a 20 74  makeDflt is.** t
81420 72 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  rue..*/.SQLITE_A
81430 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76  PI int sqlite3_v
81440 66 73 5f 72 65 67 69 73 74 65 72 28 73 71 6c 69  fs_register(sqli
81450 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69  te3_vfs *pVfs, i
81460 6e 74 20 6d 61 6b 65 44 66 6c 74 29 7b 0a 20 20  nt makeDflt){.  
81470 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d  sqlite3_mutex *m
81480 75 74 65 78 20 3d 20 30 3b 0a 23 69 66 6e 64 65  utex = 0;.#ifnde
81490 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
814a0 54 4f 49 4e 49 54 0a 20 20 69 6e 74 20 72 63 20  TOINIT.  int rc 
814b0 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  = sqlite3_initia
814c0 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 72 63  lize();.  if( rc
814d0 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65   ) return rc;.#e
814e0 6e 64 69 66 0a 20 20 6d 75 74 65 78 20 3d 20 73  ndif.  mutex = s
814f0 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
81500 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
81510 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20  ATIC_MASTER);.  
81520 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
81530 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 76 66  ter(mutex);.  vf
81540 73 55 6e 6c 69 6e 6b 28 70 56 66 73 29 3b 0a 20  sUnlink(pVfs);. 
81550 20 69 66 28 20 6d 61 6b 65 44 66 6c 74 20 7c 7c   if( makeDflt ||
81560 20 76 66 73 4c 69 73 74 3d 3d 30 20 29 7b 0a 20   vfsList==0 ){. 
81570 20 20 20 70 56 66 73 2d 3e 70 4e 65 78 74 20 3d     pVfs->pNext =
81580 20 76 66 73 4c 69 73 74 3b 0a 20 20 20 20 76 66   vfsList;.    vf
81590 73 4c 69 73 74 20 3d 20 70 56 66 73 3b 0a 20 20  sList = pVfs;.  
815a0 7d 65 6c 73 65 7b 0a 20 20 20 20 70 56 66 73 2d  }else{.    pVfs-
815b0 3e 70 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 74  >pNext = vfsList
815c0 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 76 66 73  ->pNext;.    vfs
815d0 4c 69 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 56  List->pNext = pV
815e0 66 73 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  fs;.  }.  assert
815f0 28 76 66 73 4c 69 73 74 29 3b 0a 20 20 73 71 6c  (vfsList);.  sql
81600 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
81610 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  (mutex);.  retur
81620 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
81630 2f 2a 0a 2a 2a 20 55 6e 72 65 67 69 73 74 65 72  /*.** Unregister
81640 20 61 20 56 46 53 20 73 6f 20 74 68 61 74 20 69   a VFS so that i
81650 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 61  t is no longer a
81660 63 63 65 73 73 69 62 6c 65 2e 0a 2a 2f 0a 53 51  ccessible..*/.SQ
81670 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
81680 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73  ite3_vfs_unregis
81690 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ter(sqlite3_vfs 
816a0 2a 70 56 66 73 29 7b 0a 23 69 66 20 53 51 4c 49  *pVfs){.#if SQLI
816b0 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20  TE_THREADSAFE.  
816c0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d  sqlite3_mutex *m
816d0 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75  utex = sqlite3Mu
816e0 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
816f0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
81700 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73  TER);.#endif.  s
81710 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
81720 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 76 66 73  er(mutex);.  vfs
81730 55 6e 6c 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20  Unlink(pVfs);.  
81740 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
81750 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65  ave(mutex);.  re
81760 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
81770 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
81780 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 2e 63 20 2a  ** End of os.c *
81790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
817a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
817b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
817c0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
817d0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 61  ** Begin file fa
817e0 75 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ult.c **********
817f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81810 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 61  */./*.** 2008 Ja
81820 6e 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  n 22.**.** The a
81830 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
81840 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
81850 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
81860 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
81870 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
81880 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
81890 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
818a0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
818b0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
818c0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
818d0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
818e0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
818f0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
81900 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
81910 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
81920 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
81930 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
81940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81980 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 66  ***.**.** $Id: f
81990 61 75 6c 74 2e 63 2c 76 20 31 2e 31 31 20 32 30  ault.c,v 1.11 20
819a0 30 38 2f 30 39 2f 30 32 20 30 30 3a 35 32 3a 35  08/09/02 00:52:5
819b0 32 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  2 drh Exp $.*/..
819c0 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  /*.** This file 
819d0 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 6f  contains code to
819e0 20 73 75 70 70 6f 72 74 20 74 68 65 20 63 6f 6e   support the con
819f0 63 65 70 74 20 6f 66 20 22 62 65 6e 69 67 6e 22  cept of "benign"
81a00 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c   .** malloc fail
81a10 75 72 65 73 20 28 77 68 65 6e 20 74 68 65 20 78  ures (when the x
81a20 4d 61 6c 6c 6f 63 28 29 20 6f 72 20 78 52 65 61  Malloc() or xRea
81a30 6c 6c 6f 63 28 29 20 6d 65 74 68 6f 64 20 6f 66  lloc() method of
81a40 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   the.** sqlite3_
81a50 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 73 74 72 75  mem_methods stru
81a60 63 74 75 72 65 20 66 61 69 6c 73 20 74 6f 20 61  cture fails to a
81a70 6c 6c 6f 63 61 74 65 20 61 20 62 6c 6f 63 6b 20  llocate a block 
81a80 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6e 64  of memory.** and
81a90 20 72 65 74 75 72 6e 73 20 30 29 2e 20 0a 2a 2a   returns 0). .**
81aa0 0a 2a 2a 20 4d 6f 73 74 20 6d 61 6c 6c 6f 63 20  .** Most malloc 
81ab0 66 61 69 6c 75 72 65 73 20 61 72 65 20 6e 6f 6e  failures are non
81ac0 2d 62 65 6e 69 67 6e 2e 20 41 66 74 65 72 20 74  -benign. After t
81ad0 68 65 79 20 6f 63 63 75 72 2c 20 53 51 4c 69 74  hey occur, SQLit
81ae0 65 0a 2a 2a 20 61 62 61 6e 64 6f 6e 73 20 74 68  e.** abandons th
81af0 65 20 63 75 72 72 65 6e 74 20 6f 70 65 72 61 74  e current operat
81b00 69 6f 6e 20 61 6e 64 20 72 65 74 75 72 6e 73 20  ion and returns 
81b10 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 28 75  an error code (u
81b20 73 75 61 6c 6c 79 0a 2a 2a 20 53 51 4c 49 54 45  sually.** SQLITE
81b30 5f 4e 4f 4d 45 4d 29 20 74 6f 20 74 68 65 20 75  _NOMEM) to the u
81b40 73 65 72 2e 20 48 6f 77 65 76 65 72 2c 20 73 6f  ser. However, so
81b50 6d 65 74 69 6d 65 73 20 61 20 66 61 75 6c 74 20  metimes a fault 
81b60 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69  is not necessari
81b70 6c 79 0a 2a 2a 20 66 61 74 61 6c 2e 20 46 6f 72  ly.** fatal. For
81b80 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 61 20 6d   example, if a m
81b90 61 6c 6c 6f 63 20 66 61 69 6c 73 20 77 68 69 6c  alloc fails whil
81ba0 65 20 72 65 73 69 7a 69 6e 67 20 61 20 68 61 73  e resizing a has
81bb0 68 20 74 61 62 6c 65 2c 20 74 68 69 73 20 0a 2a  h table, this .*
81bc0 2a 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20  * is completely 
81bd0 72 65 63 6f 76 65 72 61 62 6c 65 20 73 69 6d 70  recoverable simp
81be0 6c 79 20 62 79 20 6e 6f 74 20 63 61 72 72 79 69  ly by not carryi
81bf0 6e 67 20 6f 75 74 20 74 68 65 20 72 65 73 69 7a  ng out the resiz
81c00 65 2e 20 54 68 65 20 0a 2a 2a 20 68 61 73 68 20  e. The .** hash 
81c10 74 61 62 6c 65 20 77 69 6c 6c 20 63 6f 6e 74 69  table will conti
81c20 6e 75 65 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20  nue to function 
81c30 6e 6f 72 6d 61 6c 6c 79 2e 20 20 53 6f 20 61 20  normally.  So a 
81c40 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 0a  malloc failure .
81c50 2a 2a 20 64 75 72 69 6e 67 20 61 20 68 61 73 68  ** during a hash
81c60 20 74 61 62 6c 65 20 72 65 73 69 7a 65 20 69 73   table resize is
81c70 20 61 20 62 65 6e 69 67 6e 20 66 61 75 6c 74 2e   a benign fault.
81c80 0a 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51  .*/...#ifndef SQ
81c90 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49  LITE_OMIT_BUILTI
81ca0 4e 5f 54 45 53 54 0a 0a 2f 2a 0a 2a 2a 20 47 6c  N_TEST../*.** Gl
81cb0 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 2e 0a  obal variables..
81cc0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
81cd0 74 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f  t BenignMallocHo
81ce0 6f 6b 73 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  oks BenignMalloc
81cf0 48 6f 6f 6b 73 3b 0a 73 74 61 74 69 63 20 53 51  Hooks;.static SQ
81d00 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20  LITE_WSD struct 
81d10 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b  BenignMallocHook
81d20 73 20 7b 0a 20 20 76 6f 69 64 20 28 2a 78 42 65  s {.  void (*xBe
81d30 6e 69 67 6e 42 65 67 69 6e 29 28 76 6f 69 64 29  nignBegin)(void)
81d40 3b 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69  ;.  void (*xBeni
81d50 67 6e 45 6e 64 29 28 76 6f 69 64 29 3b 0a 7d 20  gnEnd)(void);.} 
81d60 73 71 6c 69 74 65 33 48 6f 6f 6b 73 20 3d 20 7b  sqlite3Hooks = {
81d70 20 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 20 54 68 65   0, 0 };../* The
81d80 20 22 77 73 64 48 6f 6f 6b 73 22 20 6d 61 63 72   "wsdHooks" macr
81d90 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74  o will resolve t
81da0 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  o the appropriat
81db0 65 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f  e BenignMallocHo
81dc0 6f 6b 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  oks.** structure
81dd0 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65 20 73  .  If writable s
81de0 74 61 74 69 63 20 64 61 74 61 20 69 73 20 75 6e  tatic data is un
81df0 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65  supported on the
81e00 20 74 61 72 67 65 74 2c 0a 2a 2a 20 77 65 20 68   target,.** we h
81e10 61 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68  ave to locate th
81e20 65 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 61  e state vector a
81e30 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20  t run-time.  In 
81e40 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a  the more common.
81e50 2a 2a 20 63 61 73 65 20 77 68 65 72 65 20 77 72  ** case where wr
81e60 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61  itable static da
81e70 74 61 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c  ta is supported,
81e80 20 77 73 64 48 6f 6f 6b 73 20 63 61 6e 20 72 65   wsdHooks can re
81e90 66 65 72 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20  fer directly.** 
81ea0 74 6f 20 74 68 65 20 22 73 71 6c 69 74 65 33 48  to the "sqlite3H
81eb0 6f 6f 6b 73 22 20 73 74 61 74 65 20 76 65 63 74  ooks" state vect
81ec0 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62 6f 76  or declared abov
81ed0 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  e..*/.#ifdef SQL
81ee0 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 20 64  ITE_OMIT_WSD.# d
81ef0 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 49 6e  efine wsdHooksIn
81f00 69 74 20 5c 0a 20 20 42 65 6e 69 67 6e 4d 61 6c  it \.  BenignMal
81f10 6c 6f 63 48 6f 6f 6b 73 20 2a 78 20 3d 20 26 47  locHooks *x = &G
81f20 4c 4f 42 41 4c 28 42 65 6e 69 67 6e 4d 61 6c 6c  LOBAL(BenignMall
81f30 6f 63 48 6f 6f 6b 73 2c 73 71 6c 69 74 65 33 48  ocHooks,sqlite3H
81f40 6f 6f 6b 73 29 0a 23 20 64 65 66 69 6e 65 20 77  ooks).# define w
81f50 73 64 48 6f 6f 6b 73 20 78 5b 30 5d 0a 23 65 6c  sdHooks x[0].#el
81f60 73 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48  se.# define wsdH
81f70 6f 6f 6b 73 49 6e 69 74 0a 23 20 64 65 66 69 6e  ooksInit.# defin
81f80 65 20 77 73 64 48 6f 6f 6b 73 20 73 71 6c 69 74  e wsdHooks sqlit
81f90 65 33 48 6f 6f 6b 73 0a 23 65 6e 64 69 66 0a 0a  e3Hooks.#endif..
81fa0 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
81fb0 68 6f 6f 6b 73 20 74 6f 20 63 61 6c 6c 20 77 68  hooks to call wh
81fc0 65 6e 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42  en sqlite3BeginB
81fd0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 6e  enignMalloc() an
81fe0 64 0a 2a 2a 20 73 71 6c 69 74 65 33 45 6e 64 42  d.** sqlite3EndB
81ff0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 72  enignMalloc() ar
82000 65 20 63 61 6c 6c 65 64 2c 20 72 65 73 70 65 63  e called, respec
82010 74 69 76 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54  tively..*/.SQLIT
82020 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
82030 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c  qlite3BenignMall
82040 6f 63 48 6f 6f 6b 73 28 0a 20 20 76 6f 69 64 20  ocHooks(.  void 
82050 28 2a 78 42 65 6e 69 67 6e 42 65 67 69 6e 29 28  (*xBenignBegin)(
82060 76 6f 69 64 29 2c 0a 20 20 76 6f 69 64 20 28 2a  void),.  void (*
82070 78 42 65 6e 69 67 6e 45 6e 64 29 28 76 6f 69 64  xBenignEnd)(void
82080 29 0a 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49  ).){.  wsdHooksI
82090 6e 69 74 3b 0a 20 20 77 73 64 48 6f 6f 6b 73 2e  nit;.  wsdHooks.
820a0 78 42 65 6e 69 67 6e 42 65 67 69 6e 20 3d 20 78  xBenignBegin = x
820b0 42 65 6e 69 67 6e 42 65 67 69 6e 3b 0a 20 20 77  BenignBegin;.  w
820c0 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45  sdHooks.xBenignE
820d0 6e 64 20 3d 20 78 42 65 6e 69 67 6e 45 6e 64 3b  nd = xBenignEnd;
820e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 28  .}../*.** This (
820f0 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e  sqlite3EndBenign
82100 4d 61 6c 6c 6f 63 28 29 29 20 69 73 20 63 61 6c  Malloc()) is cal
82110 6c 65 64 20 62 79 20 53 51 4c 69 74 65 20 63 6f  led by SQLite co
82120 64 65 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74  de to indicate t
82130 68 61 74 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e  hat.** subsequen
82140 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  t malloc failure
82150 73 20 61 72 65 20 62 65 6e 69 67 6e 2e 20 41 20  s are benign. A 
82160 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 45  call to sqlite3E
82170 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  ndBenignMalloc()
82180 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73 20 74 68  .** indicates th
82190 61 74 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61  at subsequent ma
821a0 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 61 72  lloc failures ar
821b0 65 20 6e 6f 6e 2d 62 65 6e 69 67 6e 2e 0a 2a 2f  e non-benign..*/
821c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
821d0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69  void sqlite3Begi
821e0 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f  nBenignMalloc(vo
821f0 69 64 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49  id){.  wsdHooksI
82200 6e 69 74 3b 0a 20 20 69 66 28 20 77 73 64 48 6f  nit;.  if( wsdHo
82210 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e  oks.xBenignBegin
82220 20 29 7b 0a 20 20 20 20 77 73 64 48 6f 6f 6b 73   ){.    wsdHooks
82230 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e 28 29 3b  .xBenignBegin();
82240 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  .  }.}.SQLITE_PR
82250 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
82260 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f  e3EndBenignMallo
82270 63 28 76 6f 69 64 29 7b 0a 20 20 77 73 64 48 6f  c(void){.  wsdHo
82280 6f 6b 73 49 6e 69 74 3b 0a 20 20 69 66 28 20 77  oksInit;.  if( w
82290 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45  sdHooks.xBenignE
822a0 6e 64 20 29 7b 0a 20 20 20 20 77 73 64 48 6f 6f  nd ){.    wsdHoo
822b0 6b 73 2e 78 42 65 6e 69 67 6e 45 6e 64 28 29 3b  ks.xBenignEnd();
822c0 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 20  .  }.}..#endif  
822d0 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49   /* #ifndef SQLI
822e0 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f  TE_OMIT_BUILTIN_
822f0 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  TEST */../******
82300 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
82310 66 61 75 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  fault.c ********
82320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82340 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
82350 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
82360 69 6c 65 20 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a  ile mem0.c *****
82370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82390 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
823a0 30 30 38 20 4f 63 74 6f 62 65 72 20 32 38 0a 2a  008 October 28.*
823b0 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
823c0 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
823d0 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
823e0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
823f0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
82400 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
82410 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
82420 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
82430 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
82440 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
82450 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
82460 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
82470 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
82480 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
82490 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
824a0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
824b0 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
824c0 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
824d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
824e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
824f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
82510 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
82520 6e 74 61 69 6e 73 20 61 20 6e 6f 2d 6f 70 20 6d  ntains a no-op m
82530 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
82540 20 64 72 69 76 65 72 73 20 66 6f 72 20 75 73 65   drivers for use
82550 20 77 68 65 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f   when.** SQLITE_
82560 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 20 69 73 20 64  ZERO_MALLOC is d
82570 65 66 69 6e 65 64 2e 20 20 54 68 65 20 61 6c 6c  efined.  The all
82580 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73 20  ocation drivers 
82590 69 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 68  implemented.** h
825a0 65 72 65 20 61 6c 77 61 79 73 20 66 61 69 6c 2e  ere always fail.
825b0 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f    SQLite will no
825c0 74 20 6f 70 65 72 61 74 65 20 77 69 74 68 20 74  t operate with t
825d0 68 65 73 65 20 64 72 69 76 65 72 73 2e 20 20 54  hese drivers.  T
825e0 68 65 73 65 0a 2a 2a 20 61 72 65 20 6d 65 72 65  hese.** are mere
825f0 6c 79 20 70 6c 61 63 65 68 6f 6c 64 65 72 73 2e  ly placeholders.
82600 20 20 52 65 61 6c 20 64 72 69 76 65 72 73 20 6d    Real drivers m
82610 75 73 74 20 62 65 20 73 75 62 73 74 69 74 75 74  ust be substitut
82620 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69  ed using.** sqli
82630 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 62 65 66  te3_config() bef
82640 6f 72 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  ore SQLite will 
82650 6f 70 65 72 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 24  operate..**.** $
82660 49 64 3a 20 6d 65 6d 30 2e 63 2c 76 20 31 2e 31  Id: mem0.c,v 1.1
82670 20 32 30 30 38 2f 31 30 2f 32 38 20 31 38 3a 35   2008/10/28 18:5
82680 38 3a 32 30 20 64 72 68 20 45 78 70 20 24 0a 2a  8:20 drh Exp $.*
82690 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65  /../*.** This ve
826a0 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d  rsion of the mem
826b0 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73  ory allocator is
826c0 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20 49   the default.  I
826d0 74 20 69 73 0a 2a 2a 20 75 73 65 64 20 77 68 65  t is.** used whe
826e0 6e 20 6e 6f 20 6f 74 68 65 72 20 6d 65 6d 6f 72  n no other memor
826f0 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 73  y allocator is s
82700 70 65 63 69 66 69 65 64 20 75 73 69 6e 67 20 63  pecified using c
82710 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6d  ompile-time.** m
82720 61 63 72 6f 73 2e 0a 2a 2f 0a 23 69 66 64 65 66  acros..*/.#ifdef
82730 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c   SQLITE_ZERO_MAL
82740 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4e 6f 2d 6f 70  LOC../*.** No-op
82750 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 61 6c 6c   versions of all
82760 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
82770 6f 6e 20 72 6f 75 74 69 6e 65 73 0a 2a 2f 0a 73  on routines.*/.s
82780 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69  tatic void *sqli
82790 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74  te3MemMalloc(int
827a0 20 6e 42 79 74 65 29 7b 20 72 65 74 75 72 6e 20   nByte){ return 
827b0 30 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  0; }.static void
827c0 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 28   sqlite3MemFree(
827d0 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 20 72  void *pPrior){ r
827e0 65 74 75 72 6e 3b 20 7d 0a 73 74 61 74 69 63 20  eturn; }.static 
827f0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d  void *sqlite3Mem
82800 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50  Realloc(void *pP
82810 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29  rior, int nByte)
82820 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74  { return 0; }.st
82830 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
82840 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 50  MemSize(void *pP
82850 72 69 6f 72 29 7b 20 72 65 74 75 72 6e 20 30 3b  rior){ return 0;
82860 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71   }.static int sq
82870 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28  lite3MemRoundup(
82880 69 6e 74 20 6e 29 7b 20 72 65 74 75 72 6e 20 6e  int n){ return n
82890 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73  ; }.static int s
828a0 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 76 6f  qlite3MemInit(vo
828b0 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 20 72 65  id *NotUsed){ re
828c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  turn SQLITE_OK; 
828d0 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  }.static void sq
828e0 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e  lite3MemShutdown
828f0 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b  (void *NotUsed){
82900 20 72 65 74 75 72 6e 3b 20 7d 0a 0a 2f 2a 0a 2a   return; }../*.*
82910 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
82920 73 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69  s the only routi
82930 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  ne in this file 
82940 77 69 74 68 20 65 78 74 65 72 6e 61 6c 20 6c 69  with external li
82950 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70  nkage..**.** Pop
82960 75 6c 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65  ulate the low-le
82970 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  vel memory alloc
82980 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70  ation function p
82990 6f 69 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71  ointers in.** sq
829a0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
829b0 67 2e 6d 20 77 69 74 68 20 70 6f 69 6e 74 65 72  g.m with pointer
829c0 73 20 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65  s to the routine
829d0 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a  s in this file..
829e0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
829f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65  E void sqlite3Me
82a00 6d 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64  mSetDefault(void
82a10 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ){.  static cons
82a20 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65  t sqlite3_mem_me
82a30 74 68 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74  thods defaultMet
82a40 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 73 71  hods = {.     sq
82a50 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a  lite3MemMalloc,.
82a60 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46       sqlite3MemF
82a70 72 65 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65  ree,.     sqlite
82a80 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20  3MemRealloc,.   
82a90 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65    sqlite3MemSize
82aa0 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65  ,.     sqlite3Me
82ab0 6d 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 73  mRoundup,.     s
82ac0 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20  qlite3MemInit,. 
82ad0 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 68      sqlite3MemSh
82ae0 75 74 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20  utdown,.     0. 
82af0 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f   };.  sqlite3_co
82b00 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46  nfig(SQLITE_CONF
82b10 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61  IG_MALLOC, &defa
82b20 75 6c 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a  ultMethods);.}..
82b30 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
82b40 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a  _ZERO_MALLOC */.
82b50 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
82b60 20 45 6e 64 20 6f 66 20 6d 65 6d 30 2e 63 20 2a   End of mem0.c *
82b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
82ba0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
82bb0 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 31   Begin file mem1
82bc0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
82bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
82bf0 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75  ./*.** 2007 Augu
82c00 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20  st 14.**.** The 
82c10 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
82c20 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
82c30 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
82c40 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
82c50 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
82c60 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
82c70 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
82c80 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
82c90 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
82ca0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
82cb0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
82cc0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
82cd0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
82ce0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
82cf0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
82d00 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
82d10 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
82d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82d60 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
82d70 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6c 6f  file contains lo
82d80 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61  w-level memory a
82d90 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72  llocation driver
82da0 73 20 66 6f 72 20 77 68 65 6e 0a 2a 2a 20 53 51  s for when.** SQ
82db0 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68  Lite will use th
82dc0 65 20 73 74 61 6e 64 61 72 64 20 43 2d 6c 69 62  e standard C-lib
82dd0 72 61 72 79 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c  rary malloc/real
82de0 6c 6f 63 2f 66 72 65 65 20 69 6e 74 65 72 66 61  loc/free interfa
82df0 63 65 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20  ce.** to obtain 
82e00 74 68 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65  the memory it ne
82e10 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  eds..**.** This 
82e20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6d  file contains im
82e30 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66  plementations of
82e40 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d   the low-level m
82e50 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
82e60 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 73 70 65  .** routines spe
82e70 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 73 71  cified in the sq
82e80 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
82e90 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20  s object..**.** 
82ea0 24 49 64 3a 20 6d 65 6d 31 2e 63 2c 76 20 31 2e  $Id: mem1.c,v 1.
82eb0 33 30 20 32 30 30 39 2f 30 33 2f 32 33 20 30 34  30 2009/03/23 04
82ec0 3a 33 33 3a 33 33 20 64 61 6e 69 65 6c 6b 31 39  :33:33 danielk19
82ed0 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a  77 Exp $.*/../*.
82ee0 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20  ** This version 
82ef0 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  of the memory al
82f00 6c 6f 63 61 74 6f 72 20 69 73 20 74 68 65 20 64  locator is the d
82f10 65 66 61 75 6c 74 2e 20 20 49 74 20 69 73 0a 2a  efault.  It is.*
82f20 2a 20 75 73 65 64 20 77 68 65 6e 20 6e 6f 20 6f  * used when no o
82f30 74 68 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ther memory allo
82f40 63 61 74 6f 72 20 69 73 20 73 70 65 63 69 66 69  cator is specifi
82f50 65 64 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c 65  ed using compile
82f60 2d 74 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e  -time.** macros.
82f70 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
82f80 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 0a  E_SYSTEM_MALLOC.
82f90 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 6d 61 6c 6c  ./*.** Like mall
82fa0 6f 63 28 29 2c 20 62 75 74 20 72 65 6d 65 6d 62  oc(), but rememb
82fb0 65 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  er the size of t
82fc0 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a  he allocation.**
82fd0 20 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20   so that we can 
82fe0 66 69 6e 64 20 69 74 20 6c 61 74 65 72 20 75 73  find it later us
82ff0 69 6e 67 20 73 71 6c 69 74 65 33 4d 65 6d 53 69  ing sqlite3MemSi
83000 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  ze()..**.** For 
83010 74 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 72  this low-level r
83020 6f 75 74 69 6e 65 2c 20 77 65 20 61 72 65 20 67  outine, we are g
83030 75 61 72 61 6e 74 65 65 64 20 74 68 61 74 20 6e  uaranteed that n
83040 42 79 74 65 3e 30 20 62 65 63 61 75 73 65 0a 2a  Byte>0 because.*
83050 2a 20 63 61 73 65 73 20 6f 66 20 6e 42 79 74 65  * cases of nByte
83060 3c 3d 30 20 77 69 6c 6c 20 62 65 20 69 6e 74 65  <=0 will be inte
83070 72 63 65 70 74 65 64 20 61 6e 64 20 64 65 61 6c  rcepted and deal
83080 74 20 77 69 74 68 20 62 79 20 68 69 67 68 65 72  t with by higher
83090 20 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75 74 69 6e   level.** routin
830a0 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  es..*/.static vo
830b0 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61  id *sqlite3MemMa
830c0 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b  lloc(int nByte){
830d0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
830e0 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e   *p;.  assert( n
830f0 42 79 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79 74  Byte>0 );.  nByt
83100 65 20 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65  e = ROUND8(nByte
83110 29 3b 0a 20 20 70 20 3d 20 6d 61 6c 6c 6f 63 28  );.  p = malloc(
83120 20 6e 42 79 74 65 2b 38 20 29 3b 0a 20 20 69 66   nByte+8 );.  if
83130 28 20 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20  ( p ){.    p[0] 
83140 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b  = nByte;.    p++
83150 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  ;.  }.  return (
83160 76 6f 69 64 20 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a  void *)p;.}../*.
83170 2a 2a 20 4c 69 6b 65 20 66 72 65 65 28 29 20 62  ** Like free() b
83180 75 74 20 77 6f 72 6b 73 20 66 6f 72 20 61 6c 6c  ut works for all
83190 6f 63 61 74 69 6f 6e 73 20 6f 62 74 61 69 6e 65  ocations obtaine
831a0 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 65  d from sqlite3Me
831b0 6d 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20  mMalloc().** or 
831c0 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f  sqlite3MemReallo
831d0 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74  c()..**.** For t
831e0 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f  his low-level ro
831f0 75 74 69 6e 65 2c 20 77 65 20 61 6c 72 65 61 64  utine, we alread
83200 79 20 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 69  y know that pPri
83210 6f 72 21 3d 30 20 73 69 6e 63 65 0a 2a 2a 20 63  or!=0 since.** c
83220 61 73 65 73 20 77 68 65 72 65 20 70 50 72 69 6f  ases where pPrio
83230 72 3d 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 62  r==0 will have b
83240 65 65 6e 20 69 6e 74 65 63 65 70 74 65 64 20 61  een intecepted a
83250 6e 64 20 64 65 61 6c 74 20 77 69 74 68 0a 2a 2a  nd dealt with.**
83260 20 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c   by higher-level
83270 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74   routines..*/.st
83280 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65  atic void sqlite
83290 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70  3MemFree(void *p
832a0 50 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69 74 65  Prior){.  sqlite
832b0 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20 28 73 71  3_int64 *p = (sq
832c0 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72  lite3_int64*)pPr
832d0 69 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ior;.  assert( p
832e0 50 72 69 6f 72 21 3d 30 20 29 3b 0a 20 20 70 2d  Prior!=0 );.  p-
832f0 2d 3b 0a 20 20 66 72 65 65 28 70 29 3b 0a 7d 0a  -;.  free(p);.}.
83300 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 72 65 61 6c  ./*.** Like real
83310 6c 6f 63 28 29 2e 20 20 52 65 73 69 7a 65 20 61  loc().  Resize a
83320 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65  n allocation pre
83330 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64  viously obtained
83340 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33   from.** sqlite3
83350 4d 65 6d 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a  MemMalloc()..**.
83360 2a 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d  ** For this low-
83370 6c 65 76 65 6c 20 69 6e 74 65 72 66 61 63 65 2c  level interface,
83380 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 70 50   we know that pP
83390 72 69 6f 72 21 3d 30 2e 20 20 43 61 73 65 73 20  rior!=0.  Cases 
833a0 77 68 65 72 65 0a 2a 2a 20 70 50 72 69 6f 72 3d  where.** pPrior=
833b0 3d 30 20 77 68 69 6c 65 20 68 61 76 65 20 62 65  =0 while have be
833c0 65 6e 20 69 6e 74 65 72 63 65 70 74 65 64 20 62  en intercepted b
833d0 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 72  y higher-level r
833e0 6f 75 74 69 6e 65 20 61 6e 64 0a 2a 2a 20 72 65  outine and.** re
833f0 64 69 72 65 63 74 65 64 20 74 6f 20 78 4d 61 6c  directed to xMal
83400 6c 6f 63 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c  loc.  Similarly,
83410 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 6e 42   we know that nB
83420 79 74 65 3e 30 20 62 65 63 61 75 73 65 73 0a 2a  yte>0 becauses.*
83430 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 6e 42  * cases where nB
83440 79 74 65 3c 3d 30 20 77 69 6c 6c 20 68 61 76 65  yte<=0 will have
83450 20 62 65 65 6e 20 69 6e 74 65 72 63 65 70 74 65   been intercepte
83460 64 20 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65  d by higher-leve
83470 6c 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 61 6e  l.** routines an
83480 64 20 72 65 64 69 72 65 63 74 65 64 20 74 6f 20  d redirected to 
83490 78 46 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  xFree..*/.static
834a0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65   void *sqlite3Me
834b0 6d 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70  mRealloc(void *p
834c0 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65  Prior, int nByte
834d0 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  ){.  sqlite3_int
834e0 36 34 20 2a 70 20 3d 20 28 73 71 6c 69 74 65 33  64 *p = (sqlite3
834f0 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a  _int64*)pPrior;.
83500 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72    assert( pPrior
83510 21 3d 30 20 26 26 20 6e 42 79 74 65 3e 30 20 29  !=0 && nByte>0 )
83520 3b 0a 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e  ;.  nByte = ROUN
83530 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 70 20 3d  D8(nByte);.  p =
83540 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a   (sqlite3_int64*
83550 29 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a  )pPrior;.  p--;.
83560 20 20 70 20 3d 20 72 65 61 6c 6c 6f 63 28 70 2c    p = realloc(p,
83570 20 6e 42 79 74 65 2b 38 20 29 3b 0a 20 20 69 66   nByte+8 );.  if
83580 28 20 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20  ( p ){.    p[0] 
83590 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b  = nByte;.    p++
835a0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  ;.  }.  return (
835b0 76 6f 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  void*)p;.}../*.*
835c0 2a 20 52 65 70 6f 72 74 20 74 68 65 20 61 6c 6c  * Report the all
835d0 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61  ocated size of a
835e0 20 70 72 69 6f 72 20 72 65 74 75 72 6e 20 66 72   prior return fr
835f0 6f 6d 20 78 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20  om xMalloc().** 
83600 6f 72 20 78 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a  or xRealloc()..*
83610 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  /.static int sql
83620 69 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64  ite3MemSize(void
83630 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 71 6c   *pPrior){.  sql
83640 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20  ite3_int64 *p;. 
83650 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29   if( pPrior==0 )
83660 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 20 3d   return 0;.  p =
83670 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a   (sqlite3_int64*
83680 29 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a  )pPrior;.  p--;.
83690 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70 5b    return (int)p[
836a0 30 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75  0];.}../*.** Rou
836b0 6e 64 20 75 70 20 61 20 72 65 71 75 65 73 74 20  nd up a request 
836c0 73 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74  size to the next
836d0 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f   valid allocatio
836e0 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69  n size..*/.stati
836f0 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d  c int sqlite3Mem
83700 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a  Roundup(int n){.
83710 20 20 72 65 74 75 72 6e 20 52 4f 55 4e 44 38 28    return ROUND8(
83720 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69  n);.}../*.** Ini
83730 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64  tialize this mod
83740 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ule..*/.static i
83750 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69  nt sqlite3MemIni
83760 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29  t(void *NotUsed)
83770 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
83780 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
83790 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
837a0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e  K;.}../*.** Dein
837b0 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f  itialize this mo
837c0 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  dule..*/.static 
837d0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53  void sqlite3MemS
837e0 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f  hutdown(void *No
837f0 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44  tUsed){.  UNUSED
83800 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
83810 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d  ed);.  return;.}
83820 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
83830 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79  tine is the only
83840 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73   routine in this
83850 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 72   file with exter
83860 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a  nal linkage..**.
83870 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20  ** Populate the 
83880 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79  low-level memory
83890 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63   allocation func
838a0 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e  tion pointers in
838b0 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  .** sqlite3Globa
838c0 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70  lConfig.m with p
838d0 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72  ointers to the r
838e0 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20  outines in this 
838f0 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  file..*/.SQLITE_
83900 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
83910 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c  ite3MemSetDefaul
83920 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69  t(void){.  stati
83930 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
83940 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61  mem_methods defa
83950 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20  ultMethods = {. 
83960 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61      sqlite3MemMa
83970 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74  lloc,.     sqlit
83980 65 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20  e3MemFree,.     
83990 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f  sqlite3MemReallo
839a0 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d  c,.     sqlite3M
839b0 65 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c  emSize,.     sql
839c0 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a  ite3MemRoundup,.
839d0 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49       sqlite3MemI
839e0 6e 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65  nit,.     sqlite
839f0 33 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20  3MemShutdown,.  
83a00 20 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69     0.  };.  sqli
83a10 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54  te3_config(SQLIT
83a20 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c  E_CONFIG_MALLOC,
83a30 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73   &defaultMethods
83a40 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  );.}..#endif /* 
83a50 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41  SQLITE_SYSTEM_MA
83a60 4c 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  LLOC */../******
83a70 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
83a80 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mem1.c *********
83a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83ab0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
83ac0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
83ad0 69 6c 65 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a  ile mem2.c *****
83ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83b00 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
83b10 30 30 37 20 41 75 67 75 73 74 20 31 35 0a 2a 2a  007 August 15.**
83b20 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
83b30 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
83b40 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
83b50 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
83b60 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
83b70 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
83b80 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
83b90 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
83ba0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
83bb0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
83bc0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
83bd0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
83be0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
83bf0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
83c00 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
83c10 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
83c20 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
83c30 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
83c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
83c80 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
83c90 74 61 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20  tains low-level 
83ca0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
83cb0 6e 20 64 72 69 76 65 72 73 20 66 6f 72 20 77 68  n drivers for wh
83cc0 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c  en.** SQLite wil
83cd0 6c 20 75 73 65 20 74 68 65 20 73 74 61 6e 64 61  l use the standa
83ce0 72 64 20 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c  rd C-library mal
83cf0 6c 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65  loc/realloc/free
83d00 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f   interface.** to
83d10 20 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f   obtain the memo
83d20 72 79 20 69 74 20 6e 65 65 64 73 20 77 68 69 6c  ry it needs whil
83d30 65 20 61 64 64 69 6e 67 20 6c 6f 74 73 20 6f 66  e adding lots of
83d40 20 61 64 64 69 74 69 6f 6e 61 6c 20 64 65 62 75   additional debu
83d50 67 67 69 6e 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61  gging.** informa
83d60 74 69 6f 6e 20 74 6f 20 65 61 63 68 20 61 6c 6c  tion to each all
83d70 6f 63 61 74 69 6f 6e 20 69 6e 20 6f 72 64 65 72  ocation in order
83d80 20 74 6f 20 68 65 6c 70 20 64 65 74 65 63 74 20   to help detect 
83d90 61 6e 64 20 66 69 78 20 6d 65 6d 6f 72 79 0a 2a  and fix memory.*
83da0 2a 20 6c 65 61 6b 73 20 61 6e 64 20 6d 65 6d 6f  * leaks and memo
83db0 72 79 20 75 73 61 67 65 20 65 72 72 6f 72 73 2e  ry usage errors.
83dc0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
83dd0 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d   contains implem
83de0 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65  entations of the
83df0 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72   low-level memor
83e00 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20  y allocation.** 
83e10 72 6f 75 74 69 6e 65 73 20 73 70 65 63 69 66 69  routines specifi
83e20 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65  ed in the sqlite
83e30 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6f 62  3_mem_methods ob
83e40 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  ject..**.** $Id:
83e50 20 6d 65 6d 32 2e 63 2c 76 20 31 2e 34 35 20 32   mem2.c,v 1.45 2
83e60 30 30 39 2f 30 33 2f 32 33 20 30 34 3a 33 33 3a  009/03/23 04:33:
83e70 33 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45  33 danielk1977 E
83e80 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  xp $.*/../*.** T
83e90 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  his version of t
83ea0 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
83eb0 74 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79  tor is used only
83ec0 20 69 66 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54   if the.** SQLIT
83ed0 45 5f 4d 45 4d 44 45 42 55 47 20 6d 61 63 72 6f  E_MEMDEBUG macro
83ee0 20 69 73 20 64 65 66 69 6e 65 64 0a 2a 2f 0a 23   is defined.*/.#
83ef0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 45 4d  ifdef SQLITE_MEM
83f00 44 45 42 55 47 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  DEBUG../*.** The
83f10 20 62 61 63 6b 74 72 61 63 65 20 66 75 6e 63 74   backtrace funct
83f20 69 6f 6e 61 6c 69 74 79 20 69 73 20 6f 6e 6c 79  ionality is only
83f30 20 61 76 61 69 6c 61 62 6c 65 20 77 69 74 68 20   available with 
83f40 47 4c 49 42 43 0a 2a 2f 0a 23 69 66 64 65 66 20  GLIBC.*/.#ifdef 
83f50 5f 5f 47 4c 49 42 43 5f 5f 0a 20 20 65 78 74 65  __GLIBC__.  exte
83f60 72 6e 20 69 6e 74 20 62 61 63 6b 74 72 61 63 65  rn int backtrace
83f70 28 76 6f 69 64 2a 2a 2c 69 6e 74 29 3b 0a 20 20  (void**,int);.  
83f80 65 78 74 65 72 6e 20 76 6f 69 64 20 62 61 63 6b  extern void back
83f90 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64  trace_symbols_fd
83fa0 28 76 6f 69 64 2a 63 6f 6e 73 74 2a 2c 69 6e 74  (void*const*,int
83fb0 2c 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 23 20 64  ,int);.#else.# d
83fc0 65 66 69 6e 65 20 62 61 63 6b 74 72 61 63 65 28  efine backtrace(
83fd0 41 2c 42 29 20 31 0a 23 20 64 65 66 69 6e 65 20  A,B) 1.# define 
83fe0 62 61 63 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c  backtrace_symbol
83ff0 73 5f 66 64 28 41 2c 42 2c 43 29 0a 23 65 6e 64  s_fd(A,B,C).#end
84000 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 6d  if../*.** Each m
84010 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
84020 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73   looks like this
84030 3a 0a 2a 2a 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d  :.**.**  -------
84040 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84050 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84060 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84070 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84080 2d 0a 2a 2a 20 20 7c 20 54 69 74 6c 65 20 7c 20  -.**  | Title | 
84090 20 62 61 63 6b 74 72 61 63 65 20 70 6f 69 6e 74   backtrace point
840a0 65 72 73 20 7c 20 20 4d 65 6d 42 6c 6f 63 6b 48  ers |  MemBlockH
840b0 64 72 20 7c 20 20 61 6c 6c 6f 63 61 74 69 6f 6e  dr |  allocation
840c0 20 7c 20 20 45 6e 64 47 75 61 72 64 20 7c 0a 2a   |  EndGuard |.*
840d0 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *  -------------
840e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
840f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84100 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84110 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a  -----------.**.*
84120 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f  * The applicatio
84130 6e 20 63 6f 64 65 20 73 65 65 73 20 6f 6e 6c 79  n code sees only
84140 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
84150 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 57  e allocation.  W
84160 65 20 68 61 76 65 0a 2a 2a 20 74 6f 20 62 61 63  e have.** to bac
84170 6b 20 75 70 20 66 72 6f 6d 20 74 68 65 20 61 6c  k up from the al
84180 6c 6f 63 61 74 69 6f 6e 20 70 6f 69 6e 74 65 72  location pointer
84190 20 74 6f 20 66 69 6e 64 20 74 68 65 20 4d 65 6d   to find the Mem
841a0 42 6c 6f 63 6b 48 64 72 2e 20 20 54 68 65 0a 2a  BlockHdr.  The.*
841b0 2a 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 74 65  * MemBlockHdr te
841c0 6c 6c 73 20 75 73 20 74 68 65 20 73 69 7a 65 20  lls us the size 
841d0 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f  of the allocatio
841e0 6e 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72  n and the number
841f0 20 6f 66 0a 2a 2a 20 62 61 63 6b 74 72 61 63 65   of.** backtrace
84200 20 70 6f 69 6e 74 65 72 73 2e 20 20 54 68 65 72   pointers.  Ther
84210 65 20 69 73 20 61 6c 73 6f 20 61 20 67 75 61 72  e is also a guar
84220 64 20 77 6f 72 64 20 61 74 20 74 68 65 20 65 6e  d word at the en
84230 64 20 6f 66 20 74 68 65 0a 2a 2a 20 4d 65 6d 42  d of the.** MemB
84240 6c 6f 63 6b 48 64 72 2e 0a 2a 2f 0a 73 74 72 75  lockHdr..*/.stru
84250 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 7b  ct MemBlockHdr {
84260 0a 20 20 69 36 34 20 69 53 69 7a 65 3b 20 20 20  .  i64 iSize;   
84270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
84280 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
84290 66 20 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f  f this allocatio
842a0 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4d 65  n */.  struct Me
842b0 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 4e 65 78 74  mBlockHdr *pNext
842c0 2c 20 2a 70 50 72 65 76 3b 20 20 2f 2a 20 4c 69  , *pPrev;  /* Li
842d0 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c  nked list of all
842e0 20 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20   unfreed memory 
842f0 2a 2f 0a 20 20 63 68 61 72 20 6e 42 61 63 6b 74  */.  char nBackt
84300 72 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20  race;           
84310 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
84320 65 72 20 6f 66 20 62 61 63 6b 74 72 61 63 65 73  er of backtraces
84330 20 6f 6e 20 74 68 69 73 20 61 6c 6c 6f 63 20 2a   on this alloc *
84340 2f 0a 20 20 63 68 61 72 20 6e 42 61 63 6b 74 72  /.  char nBacktr
84350 61 63 65 53 6c 6f 74 73 3b 20 20 20 20 20 20 20  aceSlots;       
84360 20 20 20 20 20 20 20 20 2f 2a 20 41 76 61 69 6c          /* Avail
84370 61 62 6c 65 20 62 61 63 6b 74 72 61 63 65 20 73  able backtrace s
84380 6c 6f 74 73 20 2a 2f 0a 20 20 73 68 6f 72 74 20  lots */.  short 
84390 6e 54 69 74 6c 65 3b 20 20 20 20 20 20 20 20 20  nTitle;         
843a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
843b0 20 42 79 74 65 73 20 6f 66 20 74 69 74 6c 65 3b   Bytes of title;
843c0 20 69 6e 63 6c 75 64 65 73 20 27 5c 30 27 20 2a   includes '\0' *
843d0 2f 0a 20 20 69 6e 74 20 69 46 6f 72 65 47 75 61  /.  int iForeGua
843e0 72 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rd;             
843f0 20 20 20 20 20 20 20 20 2f 2a 20 47 75 61 72 64          /* Guard
84400 20 77 6f 72 64 20 66 6f 72 20 73 61 6e 69 74 79   word for sanity
84410 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 75   */.};../*.** Gu
84420 61 72 64 20 77 6f 72 64 73 0a 2a 2f 0a 23 64 65  ard words.*/.#de
84430 66 69 6e 65 20 46 4f 52 45 47 55 41 52 44 20 30  fine FOREGUARD 0
84440 78 38 30 46 35 45 31 35 33 0a 23 64 65 66 69 6e  x80F5E153.#defin
84450 65 20 52 45 41 52 47 55 41 52 44 20 30 78 45 34  e REARGUARD 0xE4
84460 36 37 36 42 35 33 0a 0a 2f 2a 0a 2a 2a 20 4e 75  676B53../*.** Nu
84470 6d 62 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 73  mber of malloc s
84480 69 7a 65 20 69 6e 63 72 65 6d 65 6e 74 73 20 74  ize increments t
84490 6f 20 74 72 61 63 6b 2e 0a 2a 2f 0a 23 64 65 66  o track..*/.#def
844a0 69 6e 65 20 4e 43 53 49 5a 45 20 20 31 30 30 30  ine NCSIZE  1000
844b0 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74  ../*.** All of t
844c0 68 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62  he static variab
844d0 6c 65 73 20 75 73 65 64 20 62 79 20 74 68 69 73  les used by this
844e0 20 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c   module are coll
844f0 65 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20  ected.** into a 
84500 73 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72 65  single structure
84510 20 6e 61 6d 65 64 20 22 6d 65 6d 22 2e 20 20 54   named "mem".  T
84520 68 69 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74  his is to keep t
84530 68 65 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72  he.** static var
84540 69 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64  iables organized
84550 20 61 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e   and to reduce n
84560 61 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69  amespace polluti
84570 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20  on.** when this 
84580 6d 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e  module is combin
84590 65 64 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e  ed with other in
845a0 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f   the amalgamatio
845b0 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72  n..*/.static str
845c0 75 63 74 20 7b 0a 20 20 0a 20 20 2f 2a 0a 20 20  uct {.  .  /*.  
845d0 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74  ** Mutex to cont
845e0 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68  rol access to th
845f0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
84600 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20  ion subsystem.. 
84610 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   */.  sqlite3_mu
84620 74 65 78 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f  tex *mutex;..  /
84630 2a 0a 20 20 2a 2a 20 48 65 61 64 20 61 6e 64 20  *.  ** Head and 
84640 74 61 69 6c 20 6f 66 20 61 20 6c 69 6e 6b 65 64  tail of a linked
84650 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 6f 75 74   list of all out
84660 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74  standing allocat
84670 69 6f 6e 73 0a 20 20 2a 2f 0a 20 20 73 74 72 75  ions.  */.  stru
84680 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a  ct MemBlockHdr *
84690 70 46 69 72 73 74 3b 0a 20 20 73 74 72 75 63 74  pFirst;.  struct
846a0 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 4c   MemBlockHdr *pL
846b0 61 73 74 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a  ast;.  .  /*.  *
846c0 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * The number of 
846d0 6c 65 76 65 6c 73 20 6f 66 20 62 61 63 6b 74 72  levels of backtr
846e0 61 63 65 20 74 6f 20 73 61 76 65 20 69 6e 20 6e  ace to save in n
846f0 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a  ew allocations..
84700 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b    */.  int nBack
84710 74 72 61 63 65 3b 0a 20 20 76 6f 69 64 20 28 2a  trace;.  void (*
84720 78 42 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c  xBacktrace)(int,
84730 20 69 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 3b 0a   int, void **);.
84740 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 69 74 6c 65  .  /*.  ** Title
84750 20 74 65 78 74 20 74 6f 20 69 6e 73 65 72 74 20   text to insert 
84760 69 6e 20 66 72 6f 6e 74 20 6f 66 20 65 61 63 68  in front of each
84770 20 62 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 6e   block.  */.  in
84780 74 20 6e 54 69 74 6c 65 3b 20 20 20 20 20 20 20  t nTitle;       
84790 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 7a 54 69   /* Bytes of zTi
847a0 74 6c 65 20 74 6f 20 73 61 76 65 2e 20 20 49 6e  tle to save.  In
847b0 63 6c 75 64 65 73 20 27 5c 30 27 20 61 6e 64 20  cludes '\0' and 
847c0 70 61 64 64 69 6e 67 20 2a 2f 0a 20 20 63 68 61  padding */.  cha
847d0 72 20 7a 54 69 74 6c 65 5b 31 30 30 5d 3b 20 20  r zTitle[100];  
847e0 2f 2a 20 54 68 65 20 74 69 74 6c 65 20 74 65 78  /* The title tex
847f0 74 20 2a 2f 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a  t */..  /* .  **
84800 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 44 69   sqlite3MallocDi
84810 73 61 6c 6c 6f 77 28 29 20 69 6e 63 72 65 6d 65  sallow() increme
84820 6e 74 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  nts the followin
84830 67 20 63 6f 75 6e 74 65 72 2e 0a 20 20 2a 2a 20  g counter..  ** 
84840 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c  sqlite3MallocAll
84850 6f 77 28 29 20 64 65 63 72 65 6d 65 6e 74 73 20  ow() decrements 
84860 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 64  it..  */.  int d
84870 69 73 61 6c 6c 6f 77 3b 20 2f 2a 20 44 6f 20 6e  isallow; /* Do n
84880 6f 74 20 61 6c 6c 6f 77 20 6d 65 6d 6f 72 79 20  ot allow memory 
84890 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 0a 20  allocation */.. 
848a0 20 2f 2a 0a 20 20 2a 2a 20 47 61 74 68 65 72 20   /*.  ** Gather 
848b0 73 74 61 74 69 73 74 69 63 73 20 6f 6e 20 74 68  statistics on th
848c0 65 20 73 69 7a 65 73 20 6f 66 20 6d 65 6d 6f 72  e sizes of memor
848d0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20  y allocations.. 
848e0 20 2a 2a 20 6e 41 6c 6c 6f 63 5b 69 5d 20 69 73   ** nAlloc[i] is
848f0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61   the number of a
84900 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70  llocation attemp
84910 74 73 20 6f 66 20 69 2a 38 0a 20 20 2a 2a 20 62  ts of i*8.  ** b
84920 79 74 65 73 2e 20 20 69 3d 3d 4e 43 53 49 5a 45  ytes.  i==NCSIZE
84930 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
84940 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74  f allocation att
84950 65 6d 70 74 73 20 66 6f 72 0a 20 20 2a 2a 20 73  empts for.  ** s
84960 69 7a 65 73 20 6d 6f 72 65 20 74 68 61 6e 20 4e  izes more than N
84970 43 53 49 5a 45 2a 38 20 62 79 74 65 73 2e 0a 20  CSIZE*8 bytes.. 
84980 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63   */.  int nAlloc
84990 5b 4e 43 53 49 5a 45 5d 3b 20 20 20 20 20 20 2f  [NCSIZE];      /
849a0 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  * Total number o
849b0 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f  f allocations */
849c0 0a 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 5b  .  int nCurrent[
849d0 4e 43 53 49 5a 45 5d 3b 20 20 20 20 2f 2a 20 43  NCSIZE];    /* C
849e0 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66  urrent number of
849f0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a   allocations */.
84a00 20 20 69 6e 74 20 6d 78 43 75 72 72 65 6e 74 5b    int mxCurrent[
84a10 4e 43 53 49 5a 45 5d 3b 20 20 20 2f 2a 20 48 69  NCSIZE];   /* Hi
84a20 67 68 77 61 74 65 72 20 6d 61 72 6b 20 66 6f 72  ghwater mark for
84a30 20 6e 43 75 72 72 65 6e 74 20 2a 2f 0a 0a 7d 20   nCurrent */..} 
84a40 6d 65 6d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 6a  mem;.../*.** Adj
84a50 75 73 74 20 6d 65 6d 6f 72 79 20 75 73 61 67 65  ust memory usage
84a60 20 73 74 61 74 69 73 74 69 63 73 0a 2a 2f 0a 73   statistics.*/.s
84a70 74 61 74 69 63 20 76 6f 69 64 20 61 64 6a 75 73  tatic void adjus
84a80 74 53 74 61 74 73 28 69 6e 74 20 69 53 69 7a 65  tStats(int iSize
84a90 2c 20 69 6e 74 20 69 6e 63 72 65 6d 65 6e 74 29  , int increment)
84aa0 7b 0a 20 20 69 6e 74 20 69 20 3d 20 52 4f 55 4e  {.  int i = ROUN
84ab0 44 38 28 69 53 69 7a 65 29 2f 38 3b 0a 20 20 69  D8(iSize)/8;.  i
84ac0 66 28 20 69 3e 4e 43 53 49 5a 45 2d 31 20 29 7b  f( i>NCSIZE-1 ){
84ad0 0a 20 20 20 20 69 20 3d 20 4e 43 53 49 5a 45 20  .    i = NCSIZE 
84ae0 2d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69  - 1;.  }.  if( i
84af0 6e 63 72 65 6d 65 6e 74 3e 30 20 29 7b 0a 20 20  ncrement>0 ){.  
84b00 20 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2b    mem.nAlloc[i]+
84b10 2b 3b 0a 20 20 20 20 6d 65 6d 2e 6e 43 75 72 72  +;.    mem.nCurr
84b20 65 6e 74 5b 69 5d 2b 2b 3b 0a 20 20 20 20 69 66  ent[i]++;.    if
84b30 28 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69  ( mem.nCurrent[i
84b40 5d 3e 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b  ]>mem.mxCurrent[
84b50 69 5d 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e  i] ){.      mem.
84b60 6d 78 43 75 72 72 65 6e 74 5b 69 5d 20 3d 20 6d  mxCurrent[i] = m
84b70 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3b 0a  em.nCurrent[i];.
84b80 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
84b90 20 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b     mem.nCurrent[
84ba0 69 5d 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74  i]--;.    assert
84bb0 28 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69  ( mem.nCurrent[i
84bc0 5d 3e 3d 30 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ]>=0 );.  }.}../
84bd0 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 6e 20 61 6c  *.** Given an al
84be0 6c 6f 63 61 74 69 6f 6e 2c 20 66 69 6e 64 20 74  location, find t
84bf0 68 65 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 66  he MemBlockHdr f
84c00 6f 72 20 74 68 61 74 20 61 6c 6c 6f 63 61 74 69  or that allocati
84c10 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  on..**.** This r
84c20 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 68  outine checks th
84c30 65 20 67 75 61 72 64 73 20 61 74 20 65 69 74 68  e guards at eith
84c40 65 72 20 65 6e 64 20 6f 66 20 74 68 65 20 61 6c  er end of the al
84c50 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20  location and.** 
84c60 69 66 20 74 68 65 79 20 61 72 65 20 69 6e 63 6f  if they are inco
84c70 72 72 65 63 74 20 69 74 20 61 73 73 65 72 74 73  rrect it asserts
84c80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75  ..*/.static stru
84c90 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a  ct MemBlockHdr *
84ca0 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74  sqlite3MemsysGet
84cb0 48 65 61 64 65 72 28 76 6f 69 64 20 2a 70 41 6c  Header(void *pAl
84cc0 6c 6f 63 61 74 69 6f 6e 29 7b 0a 20 20 73 74 72  location){.  str
84cd0 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20  uct MemBlockHdr 
84ce0 2a 70 3b 0a 20 20 69 6e 74 20 2a 70 49 6e 74 3b  *p;.  int *pInt;
84cf0 0a 20 20 75 38 20 2a 70 55 38 3b 0a 20 20 69 6e  .  u8 *pU8;.  in
84d00 74 20 6e 52 65 73 65 72 76 65 3b 0a 0a 20 20 70  t nReserve;..  p
84d10 20 3d 20 28 73 74 72 75 63 74 20 4d 65 6d 42 6c   = (struct MemBl
84d20 6f 63 6b 48 64 72 2a 29 70 41 6c 6c 6f 63 61 74  ockHdr*)pAllocat
84d30 69 6f 6e 3b 0a 20 20 70 2d 2d 3b 0a 20 20 61 73  ion;.  p--;.  as
84d40 73 65 72 74 28 20 70 2d 3e 69 46 6f 72 65 47 75  sert( p->iForeGu
84d50 61 72 64 3d 3d 28 69 6e 74 29 46 4f 52 45 47 55  ard==(int)FOREGU
84d60 41 52 44 20 29 3b 0a 20 20 6e 52 65 73 65 72 76  ARD );.  nReserv
84d70 65 20 3d 20 52 4f 55 4e 44 38 28 70 2d 3e 69 53  e = ROUND8(p->iS
84d80 69 7a 65 29 3b 0a 20 20 70 49 6e 74 20 3d 20 28  ize);.  pInt = (
84d90 69 6e 74 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e  int*)pAllocation
84da0 3b 0a 20 20 70 55 38 20 3d 20 28 75 38 2a 29 70  ;.  pU8 = (u8*)p
84db0 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 61 73  Allocation;.  as
84dc0 73 65 72 74 28 20 70 49 6e 74 5b 6e 52 65 73 65  sert( pInt[nRese
84dd0 72 76 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d  rve/sizeof(int)]
84de0 3d 3d 28 69 6e 74 29 52 45 41 52 47 55 41 52 44  ==(int)REARGUARD
84df0 20 29 3b 0a 20 20 2f 2a 20 54 68 69 73 20 63 68   );.  /* This ch
84e00 65 63 6b 73 20 61 6e 79 20 6f 66 20 74 68 65 20  ecks any of the 
84e10 22 65 78 74 72 61 22 20 62 79 74 65 73 20 61 6c  "extra" bytes al
84e20 6c 6f 63 61 74 65 64 20 64 75 65 0a 20 20 2a 2a  located due.  **
84e30 20 74 6f 20 72 6f 75 6e 64 69 6e 67 20 75 70 20   to rounding up 
84e40 74 6f 20 61 6e 20 38 20 62 79 74 65 20 62 6f 75  to an 8 byte bou
84e50 6e 64 61 72 79 20 74 6f 20 65 6e 73 75 72 65 20  ndary to ensure 
84e60 0a 20 20 2a 2a 20 74 68 65 79 20 68 61 76 65 6e  .  ** they haven
84e70 27 74 20 62 65 65 6e 20 6f 76 65 72 77 72 69 74  't been overwrit
84e80 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c  ten..  */.  whil
84e90 65 28 20 6e 52 65 73 65 72 76 65 2d 2d 20 3e 20  e( nReserve-- > 
84ea0 70 2d 3e 69 53 69 7a 65 20 29 20 61 73 73 65 72  p->iSize ) asser
84eb0 74 28 20 70 55 38 5b 6e 52 65 73 65 72 76 65 5d  t( pU8[nReserve]
84ec0 3d 3d 30 78 36 35 20 29 3b 0a 20 20 72 65 74 75  ==0x65 );.  retu
84ed0 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  rn p;.}../*.** R
84ee0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
84ef0 20 6f 66 20 62 79 74 65 73 20 63 75 72 72 65 6e   of bytes curren
84f00 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 61 74  tly allocated at
84f10 20 61 64 64 72 65 73 73 20 70 2e 0a 2a 2f 0a 73   address p..*/.s
84f20 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
84f30 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70  3MemSize(void *p
84f40 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42  ){.  struct MemB
84f50 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20  lockHdr *pHdr;. 
84f60 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72   if( !p ){.    r
84f70 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70  eturn 0;.  }.  p
84f80 48 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d  Hdr = sqlite3Mem
84f90 73 79 73 47 65 74 48 65 61 64 65 72 28 70 29 3b  sysGetHeader(p);
84fa0 0a 20 20 72 65 74 75 72 6e 20 70 48 64 72 2d 3e  .  return pHdr->
84fb0 69 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  iSize;.}../*.** 
84fc0 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d  Initialize the m
84fd0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
84fe0 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73   subsystem..*/.s
84ff0 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
85000 33 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e  3MemInit(void *N
85010 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45  otUsed){.  UNUSE
85020 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
85030 73 65 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20  sed);.  assert( 
85040 28 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 4d  (sizeof(struct M
85050 65 6d 42 6c 6f 63 6b 48 64 72 29 26 37 29 20 3d  emBlockHdr)&7) =
85060 3d 20 30 20 29 3b 0a 20 20 69 66 28 20 21 73 71  = 0 );.  if( !sq
85070 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
85080 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20  g.bMemstat ){.  
85090 20 20 2f 2a 20 49 66 20 6d 65 6d 6f 72 79 20 73    /* If memory s
850a0 74 61 74 75 73 20 69 73 20 65 6e 61 62 6c 65 64  tatus is enabled
850b0 2c 20 74 68 65 6e 20 74 68 65 20 6d 61 6c 6c 6f  , then the mallo
850c0 63 2e 63 20 77 72 61 70 70 65 72 20 77 69 6c 6c  c.c wrapper will
850d0 20 61 6c 72 65 61 64 79 0a 20 20 20 20 2a 2a 20   already.    ** 
850e0 68 6f 6c 64 20 74 68 65 20 53 54 41 54 49 43 5f  hold the STATIC_
850f0 4d 45 4d 20 6d 75 74 65 78 20 77 68 65 6e 20 74  MEM mutex when t
85100 68 65 20 72 6f 75 74 69 6e 65 73 20 68 65 72 65  he routines here
85110 20 61 72 65 20 69 6e 76 6f 6b 65 64 2e 20 2a 2f   are invoked. */
85120 0a 20 20 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d  .    mem.mutex =
85130 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c   sqlite3MutexAll
85140 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
85150 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d  STATIC_MEM);.  }
85160 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
85170 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  _OK;.}../*.** De
85180 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d  initialize the m
85190 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
851a0 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73   subsystem..*/.s
851b0 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
851c0 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f  e3MemShutdown(vo
851d0 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20  id *NotUsed){.  
851e0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
851f0 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d  (NotUsed);.  mem
85200 2e 6d 75 74 65 78 20 3d 20 30 3b 0a 7d 0a 0a 2f  .mutex = 0;.}../
85210 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20  *.** Round up a 
85220 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20  request size to 
85230 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61  the next valid a
85240 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a  llocation size..
85250 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  */.static int sq
85260 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28  lite3MemRoundup(
85270 69 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e  int n){.  return
85280 20 52 4f 55 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f   ROUND8(n);.}../
85290 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42  *.** Allocate nB
852a0 79 74 65 20 62 79 74 65 73 20 6f 66 20 6d 65 6d  yte bytes of mem
852b0 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ory..*/.static v
852c0 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d  oid *sqlite3MemM
852d0 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29  alloc(int nByte)
852e0 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c  {.  struct MemBl
852f0 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20  ockHdr *pHdr;.  
85300 76 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 20 63 68  void **pBt;.  ch
85310 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 2a 70 49  ar *z;.  int *pI
85320 6e 74 3b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20  nt;.  void *p = 
85330 30 3b 0a 20 20 69 6e 74 20 74 6f 74 61 6c 53 69  0;.  int totalSi
85340 7a 65 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 72  ze;.  int nReser
85350 76 65 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  ve;.  sqlite3_mu
85360 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75  tex_enter(mem.mu
85370 74 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20  tex);.  assert( 
85380 6d 65 6d 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30 20  mem.disallow==0 
85390 29 3b 0a 20 20 6e 52 65 73 65 72 76 65 20 3d 20  );.  nReserve = 
853a0 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20  ROUND8(nByte);. 
853b0 20 74 6f 74 61 6c 53 69 7a 65 20 3d 20 6e 52 65   totalSize = nRe
853c0 73 65 72 76 65 20 2b 20 73 69 7a 65 6f 66 28 2a  serve + sizeof(*
853d0 70 48 64 72 29 20 2b 20 73 69 7a 65 6f 66 28 69  pHdr) + sizeof(i
853e0 6e 74 29 20 2b 0a 20 20 20 20 20 20 20 20 20 20  nt) +.          
853f0 20 20 20 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72       mem.nBacktr
85400 61 63 65 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a  ace*sizeof(void*
85410 29 20 2b 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a  ) + mem.nTitle;.
85420 20 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 74 6f 74    p = malloc(tot
85430 61 6c 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 70  alSize);.  if( p
85440 20 29 7b 0a 20 20 20 20 7a 20 3d 20 70 3b 0a 20   ){.    z = p;. 
85450 20 20 20 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a     pBt = (void**
85460 29 26 7a 5b 6d 65 6d 2e 6e 54 69 74 6c 65 5d 3b  )&z[mem.nTitle];
85470 0a 20 20 20 20 70 48 64 72 20 3d 20 28 73 74 72  .    pHdr = (str
85480 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a  uct MemBlockHdr*
85490 29 26 70 42 74 5b 6d 65 6d 2e 6e 42 61 63 6b 74  )&pBt[mem.nBackt
854a0 72 61 63 65 5d 3b 0a 20 20 20 20 70 48 64 72 2d  race];.    pHdr-
854b0 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20  >pNext = 0;.    
854c0 70 48 64 72 2d 3e 70 50 72 65 76 20 3d 20 6d 65  pHdr->pPrev = me
854d0 6d 2e 70 4c 61 73 74 3b 0a 20 20 20 20 69 66 28  m.pLast;.    if(
854e0 20 6d 65 6d 2e 70 4c 61 73 74 20 29 7b 0a 20 20   mem.pLast ){.  
854f0 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74 2d 3e 70      mem.pLast->p
85500 4e 65 78 74 20 3d 20 70 48 64 72 3b 0a 20 20 20  Next = pHdr;.   
85510 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65   }else{.      me
85520 6d 2e 70 46 69 72 73 74 20 3d 20 70 48 64 72 3b  m.pFirst = pHdr;
85530 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 2e 70  .    }.    mem.p
85540 4c 61 73 74 20 3d 20 70 48 64 72 3b 0a 20 20 20  Last = pHdr;.   
85550 20 70 48 64 72 2d 3e 69 46 6f 72 65 47 75 61 72   pHdr->iForeGuar
85560 64 20 3d 20 46 4f 52 45 47 55 41 52 44 3b 0a 20  d = FOREGUARD;. 
85570 20 20 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72     pHdr->nBacktr
85580 61 63 65 53 6c 6f 74 73 20 3d 20 6d 65 6d 2e 6e  aceSlots = mem.n
85590 42 61 63 6b 74 72 61 63 65 3b 0a 20 20 20 20 70  Backtrace;.    p
855a0 48 64 72 2d 3e 6e 54 69 74 6c 65 20 3d 20 6d 65  Hdr->nTitle = me
855b0 6d 2e 6e 54 69 74 6c 65 3b 0a 20 20 20 20 69 66  m.nTitle;.    if
855c0 28 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65  ( mem.nBacktrace
855d0 20 29 7b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a   ){.      void *
855e0 61 41 64 64 72 5b 34 30 5d 3b 0a 20 20 20 20 20  aAddr[40];.     
855f0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63   pHdr->nBacktrac
85600 65 20 3d 20 62 61 63 6b 74 72 61 63 65 28 61 41  e = backtrace(aA
85610 64 64 72 2c 20 6d 65 6d 2e 6e 42 61 63 6b 74 72  ddr, mem.nBacktr
85620 61 63 65 2b 31 29 2d 31 3b 0a 20 20 20 20 20 20  ace+1)-1;.      
85630 6d 65 6d 63 70 79 28 70 42 74 2c 20 26 61 41 64  memcpy(pBt, &aAd
85640 64 72 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e 42 61  dr[1], pHdr->nBa
85650 63 6b 74 72 61 63 65 2a 73 69 7a 65 6f 66 28 76  cktrace*sizeof(v
85660 6f 69 64 2a 29 29 3b 0a 20 20 20 20 20 20 61 73  oid*));.      as
85670 73 65 72 74 28 70 42 74 5b 30 5d 29 3b 0a 20 20  sert(pBt[0]);.  
85680 20 20 20 20 69 66 28 20 6d 65 6d 2e 78 42 61 63      if( mem.xBac
85690 6b 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20  ktrace ){.      
856a0 20 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65    mem.xBacktrace
856b0 28 6e 42 79 74 65 2c 20 70 48 64 72 2d 3e 6e 42  (nByte, pHdr->nB
856c0 61 63 6b 74 72 61 63 65 2d 31 2c 20 26 61 41 64  acktrace-1, &aAd
856d0 64 72 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d 0a  dr[1]);.      }.
856e0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
856f0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63   pHdr->nBacktrac
85700 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  e = 0;.    }.   
85710 20 69 66 28 20 6d 65 6d 2e 6e 54 69 74 6c 65 20   if( mem.nTitle 
85720 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
85730 7a 2c 20 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d  z, mem.zTitle, m
85740 65 6d 2e 6e 54 69 74 6c 65 29 3b 0a 20 20 20 20  em.nTitle);.    
85750 7d 0a 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a  }.    pHdr->iSiz
85760 65 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 61  e = nByte;.    a
85770 64 6a 75 73 74 53 74 61 74 73 28 6e 42 79 74 65  djustStats(nByte
85780 2c 20 2b 31 29 3b 0a 20 20 20 20 70 49 6e 74 20  , +1);.    pInt 
85790 3d 20 28 69 6e 74 2a 29 26 70 48 64 72 5b 31 5d  = (int*)&pHdr[1]
857a0 3b 0a 20 20 20 20 70 49 6e 74 5b 6e 52 65 73 65  ;.    pInt[nRese
857b0 72 76 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d  rve/sizeof(int)]
857c0 20 3d 20 52 45 41 52 47 55 41 52 44 3b 0a 20 20   = REARGUARD;.  
857d0 20 20 6d 65 6d 73 65 74 28 70 49 6e 74 2c 20 30    memset(pInt, 0
857e0 78 36 35 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a  x65, nReserve);.
857f0 20 20 20 20 70 20 3d 20 28 76 6f 69 64 2a 29 70      p = (void*)p
85800 49 6e 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  Int;.  }.  sqlit
85810 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
85820 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  em.mutex);.  ret
85830 75 72 6e 20 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn p; .}../*.**
85840 20 46 72 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f   Free memory..*/
85850 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c  .static void sql
85860 69 74 65 33 4d 65 6d 46 72 65 65 28 76 6f 69 64  ite3MemFree(void
85870 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 74 72   *pPrior){.  str
85880 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20  uct MemBlockHdr 
85890 2a 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a  *pHdr;.  void **
858a0 70 42 74 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a  pBt;.  char *z;.
858b0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
858c0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d  3GlobalConfig.bM
858d0 65 6d 73 74 61 74 20 7c 7c 20 6d 65 6d 2e 6d 75  emstat || mem.mu
858e0 74 65 78 21 3d 30 20 29 3b 0a 20 20 70 48 64 72  tex!=0 );.  pHdr
858f0 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73   = sqlite3Memsys
85900 47 65 74 48 65 61 64 65 72 28 70 50 72 69 6f 72  GetHeader(pPrior
85910 29 3b 0a 20 20 70 42 74 20 3d 20 28 76 6f 69 64  );.  pBt = (void
85920 2a 2a 29 70 48 64 72 3b 0a 20 20 70 42 74 20 2d  **)pHdr;.  pBt -
85930 3d 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61  = pHdr->nBacktra
85940 63 65 53 6c 6f 74 73 3b 0a 20 20 73 71 6c 69 74  ceSlots;.  sqlit
85950 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
85960 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28  em.mutex);.  if(
85970 20 70 48 64 72 2d 3e 70 50 72 65 76 20 29 7b 0a   pHdr->pPrev ){.
85980 20 20 20 20 61 73 73 65 72 74 28 20 70 48 64 72      assert( pHdr
85990 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d  ->pPrev->pNext==
859a0 70 48 64 72 20 29 3b 0a 20 20 20 20 70 48 64 72  pHdr );.    pHdr
859b0 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d  ->pPrev->pNext =
859c0 20 70 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20   pHdr->pNext;.  
859d0 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
859e0 74 28 20 6d 65 6d 2e 70 46 69 72 73 74 3d 3d 70  t( mem.pFirst==p
859f0 48 64 72 20 29 3b 0a 20 20 20 20 6d 65 6d 2e 70  Hdr );.    mem.p
85a00 46 69 72 73 74 20 3d 20 70 48 64 72 2d 3e 70 4e  First = pHdr->pN
85a10 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  ext;.  }.  if( p
85a20 48 64 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20  Hdr->pNext ){.  
85a30 20 20 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e    assert( pHdr->
85a40 70 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 48  pNext->pPrev==pH
85a50 64 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e  dr );.    pHdr->
85a60 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70  pNext->pPrev = p
85a70 48 64 72 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 65  Hdr->pPrev;.  }e
85a80 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
85a90 20 6d 65 6d 2e 70 4c 61 73 74 3d 3d 70 48 64 72   mem.pLast==pHdr
85aa0 20 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 73   );.    mem.pLas
85ab0 74 20 3d 20 70 48 64 72 2d 3e 70 50 72 65 76 3b  t = pHdr->pPrev;
85ac0 0a 20 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72  .  }.  z = (char
85ad0 2a 29 70 42 74 3b 0a 20 20 7a 20 2d 3d 20 70 48  *)pBt;.  z -= pH
85ae0 64 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 61 64  dr->nTitle;.  ad
85af0 6a 75 73 74 53 74 61 74 73 28 70 48 64 72 2d 3e  justStats(pHdr->
85b00 69 53 69 7a 65 2c 20 2d 31 29 3b 0a 20 20 6d 65  iSize, -1);.  me
85b10 6d 73 65 74 28 7a 2c 20 30 78 32 62 2c 20 73 69  mset(z, 0x2b, si
85b20 7a 65 6f 66 28 76 6f 69 64 2a 29 2a 70 48 64 72  zeof(void*)*pHdr
85b30 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74  ->nBacktraceSlot
85b40 73 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48 64 72  s + sizeof(*pHdr
85b50 29 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ) +.            
85b60 20 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a        pHdr->iSiz
85b70 65 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74 29 20  e + sizeof(int) 
85b80 2b 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 29 3b  + pHdr->nTitle);
85b90 0a 20 20 66 72 65 65 28 7a 29 3b 0a 20 20 73 71  .  free(z);.  sq
85ba0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
85bb0 65 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 20 20 0a  e(mem.mutex);  .
85bc0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  }../*.** Change 
85bd0 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65  the size of an e
85be0 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61  xisting memory a
85bf0 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  llocation..**.**
85c00 20 46 6f 72 20 74 68 69 73 20 64 65 62 75 67 67   For this debugg
85c10 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ing implementati
85c20 6f 6e 2c 20 77 65 20 2a 61 6c 77 61 79 73 2a 20  on, we *always* 
85c30 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74  make a copy of t
85c40 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e  he.** allocation
85c50 20 69 6e 74 6f 20 61 20 6e 65 77 20 70 6c 61 63   into a new plac
85c60 65 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 49 6e  e in memory.  In
85c70 20 74 68 69 73 20 77 61 79 2c 20 69 66 20 74 68   this way, if th
85c80 65 20 0a 2a 2a 20 68 69 67 68 65 72 20 6c 65 76  e .** higher lev
85c90 65 6c 20 63 6f 64 65 20 69 73 20 75 73 69 6e 67  el code is using
85ca0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
85cb0 6f 6c 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20  old allocation, 
85cc0 69 74 20 69 73 20 0a 2a 2a 20 6d 75 63 68 20 6d  it is .** much m
85cd0 6f 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 72  ore likely to br
85ce0 65 61 6b 20 61 6e 64 20 77 65 20 61 72 65 20 6d  eak and we are m
85cf0 75 63 68 20 6d 6f 72 65 20 6c 69 6b 69 6e 67 20  uch more liking 
85d00 74 6f 20 66 69 6e 64 0a 2a 2a 20 74 68 65 20 65  to find.** the e
85d10 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rror..*/.static 
85d20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d  void *sqlite3Mem
85d30 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50  Realloc(void *pP
85d40 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29  rior, int nByte)
85d50 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c  {.  struct MemBl
85d60 6f 63 6b 48 64 72 20 2a 70 4f 6c 64 48 64 72 3b  ockHdr *pOldHdr;
85d70 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20  .  void *pNew;. 
85d80 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73   assert( mem.dis
85d90 61 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 70 4f  allow==0 );.  pO
85da0 6c 64 48 64 72 20 3d 20 73 71 6c 69 74 65 33 4d  ldHdr = sqlite3M
85db0 65 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 70  emsysGetHeader(p
85dc0 50 72 69 6f 72 29 3b 0a 20 20 70 4e 65 77 20 3d  Prior);.  pNew =
85dd0 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f   sqlite3MemMallo
85de0 63 28 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20  c(nByte);.  if( 
85df0 70 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63  pNew ){.    memc
85e00 70 79 28 70 4e 65 77 2c 20 70 50 72 69 6f 72 2c  py(pNew, pPrior,
85e10 20 6e 42 79 74 65 3c 70 4f 6c 64 48 64 72 2d 3e   nByte<pOldHdr->
85e20 69 53 69 7a 65 20 3f 20 6e 42 79 74 65 20 3a 20  iSize ? nByte : 
85e30 70 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 29 3b  pOldHdr->iSize);
85e40 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3e 70  .    if( nByte>p
85e50 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20 29 7b  OldHdr->iSize ){
85e60 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 28  .      memset(&(
85e70 28 63 68 61 72 2a 29 70 4e 65 77 29 5b 70 4f 6c  (char*)pNew)[pOl
85e80 64 48 64 72 2d 3e 69 53 69 7a 65 5d 2c 20 30 78  dHdr->iSize], 0x
85e90 32 62 2c 20 6e 42 79 74 65 20 2d 20 70 4f 6c 64  2b, nByte - pOld
85ea0 48 64 72 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 20  Hdr->iSize);.   
85eb0 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65   }.    sqlite3Me
85ec0 6d 46 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20  mFree(pPrior);. 
85ed0 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77   }.  return pNew
85ee0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 75 6c  ;.}../*.** Popul
85ef0 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65  ate the low-leve
85f00 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  l memory allocat
85f10 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69  ion function poi
85f20 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69  nters in.** sqli
85f30 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
85f40 6d 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20  m with pointers 
85f50 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20  to the routines 
85f60 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f  in this file..*/
85f70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
85f80 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53  void sqlite3MemS
85f90 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b  etDefault(void){
85fa0 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
85fb0 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
85fc0 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f  ods defaultMetho
85fd0 64 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69  ds = {.     sqli
85fe0 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20  te3MemMalloc,.  
85ff0 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65     sqlite3MemFre
86000 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d  e,.     sqlite3M
86010 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20  emRealloc,.     
86020 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a  sqlite3MemSize,.
86030 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52       sqlite3MemR
86040 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c  oundup,.     sql
86050 69 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20  ite3MemInit,.   
86060 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74    sqlite3MemShut
86070 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d  down,.     0.  }
86080 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  ;.  sqlite3_conf
86090 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ig(SQLITE_CONFIG
860a0 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c  _MALLOC, &defaul
860b0 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 2f 2a  tMethods);.}../*
860c0 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62  .** Set the numb
860d0 65 72 20 6f 66 20 62 61 63 6b 74 72 61 63 65 20  er of backtrace 
860e0 6c 65 76 65 6c 73 20 6b 65 70 74 20 66 6f 72 20  levels kept for 
860f0 65 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e  each allocation.
86100 0a 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 7a  .** A value of z
86110 65 72 6f 20 74 75 72 6e 73 20 6f 66 66 20 62 61  ero turns off ba
86120 63 6b 74 72 61 63 69 6e 67 2e 20 20 54 68 65 20  cktracing.  The 
86130 6e 75 6d 62 65 72 20 69 73 20 61 6c 77 61 79 73  number is always
86140 20 72 6f 75 6e 64 65 64 0a 2a 2a 20 75 70 20 74   rounded.** up t
86150 6f 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20  o a multiple of 
86160 32 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  2..*/.SQLITE_PRI
86170 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
86180 33 4d 65 6d 64 65 62 75 67 42 61 63 6b 74 72 61  3MemdebugBacktra
86190 63 65 28 69 6e 74 20 64 65 70 74 68 29 7b 0a 20  ce(int depth){. 
861a0 20 69 66 28 20 64 65 70 74 68 3c 30 20 29 7b 20   if( depth<0 ){ 
861b0 64 65 70 74 68 20 3d 20 30 3b 20 7d 0a 20 20 69  depth = 0; }.  i
861c0 66 28 20 64 65 70 74 68 3e 32 30 20 29 7b 20 64  f( depth>20 ){ d
861d0 65 70 74 68 20 3d 20 32 30 3b 20 7d 0a 20 20 64  epth = 20; }.  d
861e0 65 70 74 68 20 3d 20 28 64 65 70 74 68 2b 31 29  epth = (depth+1)
861f0 26 30 78 66 65 3b 0a 20 20 6d 65 6d 2e 6e 42 61  &0xfe;.  mem.nBa
86200 63 6b 74 72 61 63 65 20 3d 20 64 65 70 74 68 3b  cktrace = depth;
86210 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .}..SQLITE_PRIVA
86220 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  TE void sqlite3M
86230 65 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63 65  emdebugBacktrace
86240 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64 20 28 2a  Callback(void (*
86250 78 42 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c  xBacktrace)(int,
86260 20 69 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 29 7b   int, void **)){
86270 0a 20 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63  .  mem.xBacktrac
86280 65 20 3d 20 78 42 61 63 6b 74 72 61 63 65 3b 0a  e = xBacktrace;.
86290 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
862a0 20 74 69 74 6c 65 20 73 74 72 69 6e 67 20 66 6f   title string fo
862b0 72 20 73 75 62 73 65 71 75 65 6e 74 20 61 6c 6c  r subsequent all
862c0 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c  ocations..*/.SQL
862d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
862e0 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67   sqlite3Memdebug
862f0 53 65 74 74 69 74 6c 65 28 63 6f 6e 73 74 20 63  Settitle(const c
86300 68 61 72 20 2a 7a 54 69 74 6c 65 29 7b 0a 20 20  har *zTitle){.  
86310 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 20 3d  unsigned int n =
86320 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
86330 28 7a 54 69 74 6c 65 29 20 2b 20 31 3b 0a 20 20  (zTitle) + 1;.  
86340 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
86350 74 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a  ter(mem.mutex);.
86360 20 20 69 66 28 20 6e 3e 3d 73 69 7a 65 6f 66 28    if( n>=sizeof(
86370 6d 65 6d 2e 7a 54 69 74 6c 65 29 20 29 20 6e 20  mem.zTitle) ) n 
86380 3d 20 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69  = sizeof(mem.zTi
86390 74 6c 65 29 2d 31 3b 0a 20 20 6d 65 6d 63 70 79  tle)-1;.  memcpy
863a0 28 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 7a 54 69  (mem.zTitle, zTi
863b0 74 6c 65 2c 20 6e 29 3b 0a 20 20 6d 65 6d 2e 7a  tle, n);.  mem.z
863c0 54 69 74 6c 65 5b 6e 5d 20 3d 20 30 3b 0a 20 20  Title[n] = 0;.  
863d0 6d 65 6d 2e 6e 54 69 74 6c 65 20 3d 20 52 4f 55  mem.nTitle = ROU
863e0 4e 44 38 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65  ND8(n);.  sqlite
863f0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
86400 6d 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c  m.mutex);.}..SQL
86410 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
86420 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67   sqlite3Memdebug
86430 53 79 6e 63 28 29 7b 0a 20 20 73 74 72 75 63 74  Sync(){.  struct
86440 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48   MemBlockHdr *pH
86450 64 72 3b 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d  dr;.  for(pHdr=m
86460 65 6d 2e 70 46 69 72 73 74 3b 20 70 48 64 72 3b  em.pFirst; pHdr;
86470 20 70 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78   pHdr=pHdr->pNex
86480 74 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 2a 70  t){.    void **p
86490 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64  Bt = (void**)pHd
864a0 72 3b 0a 20 20 20 20 70 42 74 20 2d 3d 20 70 48  r;.    pBt -= pH
864b0 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c  dr->nBacktraceSl
864c0 6f 74 73 3b 0a 20 20 20 20 6d 65 6d 2e 78 42 61  ots;.    mem.xBa
864d0 63 6b 74 72 61 63 65 28 70 48 64 72 2d 3e 69 53  cktrace(pHdr->iS
864e0 69 7a 65 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b  ize, pHdr->nBack
864f0 74 72 61 63 65 2d 31 2c 20 26 70 42 74 5b 31 5d  trace-1, &pBt[1]
86500 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
86510 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e  Open the file in
86520 64 69 63 61 74 65 64 20 61 6e 64 20 77 72 69 74  dicated and writ
86530 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75  e a log of all u
86540 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a  nfreed memory .*
86550 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e  * allocations in
86560 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a  to that log..*/.
86570 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
86580 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65  oid sqlite3Memde
86590 62 75 67 44 75 6d 70 28 63 6f 6e 73 74 20 63 68  bugDump(const ch
865a0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a  ar *zFilename){.
865b0 20 20 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 73    FILE *out;.  s
865c0 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64  truct MemBlockHd
865d0 72 20 2a 70 48 64 72 3b 0a 20 20 76 6f 69 64 20  r *pHdr;.  void 
865e0 2a 2a 70 42 74 3b 0a 20 20 69 6e 74 20 69 3b 0a  **pBt;.  int i;.
865f0 20 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46    out = fopen(zF
86600 69 6c 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20  ilename, "w");. 
86610 20 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20   if( out==0 ){. 
86620 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72     fprintf(stder
86630 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f  r, "** Unable to
86640 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64   output memory d
86650 65 62 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a  ebug output log:
86660 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20   %s **\n",.     
86670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a                 z
86680 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 72  Filename);.    r
86690 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72  eturn;.  }.  for
866a0 28 70 48 64 72 3d 6d 65 6d 2e 70 46 69 72 73 74  (pHdr=mem.pFirst
866b0 3b 20 70 48 64 72 3b 20 70 48 64 72 3d 70 48 64  ; pHdr; pHdr=pHd
866c0 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 63  r->pNext){.    c
866d0 68 61 72 20 2a 7a 20 3d 20 28 63 68 61 72 2a 29  har *z = (char*)
866e0 70 48 64 72 3b 0a 20 20 20 20 7a 20 2d 3d 20 70  pHdr;.    z -= p
866f0 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53  Hdr->nBacktraceS
86700 6c 6f 74 73 2a 73 69 7a 65 6f 66 28 76 6f 69 64  lots*sizeof(void
86710 2a 29 20 2b 20 70 48 64 72 2d 3e 6e 54 69 74 6c  *) + pHdr->nTitl
86720 65 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f  e;.    fprintf(o
86730 75 74 2c 20 22 2a 2a 2a 2a 20 25 6c 6c 64 20 62  ut, "**** %lld b
86740 79 74 65 73 20 61 74 20 25 70 20 66 72 6f 6d 20  ytes at %p from 
86750 25 73 20 2a 2a 2a 2a 5c 6e 22 2c 20 0a 20 20 20  %s ****\n", .   
86760 20 20 20 20 20 20 20 20 20 70 48 64 72 2d 3e 69           pHdr->i
86770 53 69 7a 65 2c 20 26 70 48 64 72 5b 31 5d 2c 20  Size, &pHdr[1], 
86780 70 48 64 72 2d 3e 6e 54 69 74 6c 65 20 3f 20 7a  pHdr->nTitle ? z
86790 20 3a 20 22 3f 3f 3f 22 29 3b 0a 20 20 20 20 69   : "???");.    i
867a0 66 28 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72  f( pHdr->nBacktr
867b0 61 63 65 20 29 7b 0a 20 20 20 20 20 20 66 66 6c  ace ){.      ffl
867c0 75 73 68 28 6f 75 74 29 3b 0a 20 20 20 20 20 20  ush(out);.      
867d0 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48  pBt = (void**)pH
867e0 64 72 3b 0a 20 20 20 20 20 20 70 42 74 20 2d 3d  dr;.      pBt -=
867f0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63   pHdr->nBacktrac
86800 65 53 6c 6f 74 73 3b 0a 20 20 20 20 20 20 62 61  eSlots;.      ba
86810 63 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f  cktrace_symbols_
86820 66 64 28 70 42 74 2c 20 70 48 64 72 2d 3e 6e 42  fd(pBt, pHdr->nB
86830 61 63 6b 74 72 61 63 65 2c 20 66 69 6c 65 6e 6f  acktrace, fileno
86840 28 6f 75 74 29 29 3b 0a 20 20 20 20 20 20 66 70  (out));.      fp
86850 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29  rintf(out, "\n")
86860 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 70  ;.    }.  }.  fp
86870 72 69 6e 74 66 28 6f 75 74 2c 20 22 43 4f 55 4e  rintf(out, "COUN
86880 54 53 3a 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69  TS:\n");.  for(i
86890 3d 30 3b 20 69 3c 4e 43 53 49 5a 45 2d 31 3b 20  =0; i<NCSIZE-1; 
868a0 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d 65  i++){.    if( me
868b0 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 20 29 7b 0a 20  m.nAlloc[i] ){. 
868c0 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74       fprintf(out
868d0 2c 20 22 20 20 20 25 35 64 3a 20 25 31 30 64 20  , "   %5d: %10d 
868e0 25 31 30 64 20 25 31 30 64 5c 6e 22 2c 20 0a 20  %10d %10d\n", . 
868f0 20 20 20 20 20 20 20 20 20 20 20 69 2a 38 2c 20             i*8, 
86900 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2c 20 6d  mem.nAlloc[i], m
86910 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2c 20  em.nCurrent[i], 
86920 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d  mem.mxCurrent[i]
86930 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  );.    }.  }.  i
86940 66 28 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43  f( mem.nAlloc[NC
86950 53 49 5a 45 2d 31 5d 20 29 7b 0a 20 20 20 20 66  SIZE-1] ){.    f
86960 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 20 20  printf(out, "   
86970 25 35 64 3a 20 25 31 30 64 20 25 31 30 64 20 25  %5d: %10d %10d %
86980 31 30 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20  10d\n",.        
86990 20 20 20 20 20 4e 43 53 49 5a 45 2a 38 2d 38 2c       NCSIZE*8-8,
869a0 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49   mem.nAlloc[NCSI
869b0 5a 45 2d 31 5d 2c 0a 20 20 20 20 20 20 20 20 20  ZE-1],.         
869c0 20 20 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74      mem.nCurrent
869d0 5b 4e 43 53 49 5a 45 2d 31 5d 2c 20 6d 65 6d 2e  [NCSIZE-1], mem.
869e0 6d 78 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45  mxCurrent[NCSIZE
869f0 2d 31 5d 29 3b 0a 20 20 7d 0a 20 20 66 63 6c 6f  -1]);.  }.  fclo
86a00 73 65 28 6f 75 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  se(out);.}../*.*
86a10 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
86a20 62 65 72 20 6f 66 20 74 69 6d 65 73 20 73 71 6c  ber of times sql
86a30 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 20  ite3MemMalloc() 
86a40 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e  has been called.
86a50 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
86a60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65  TE int sqlite3Me
86a70 6d 64 65 62 75 67 4d 61 6c 6c 6f 63 43 6f 75 6e  mdebugMallocCoun
86a80 74 28 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  t(){.  int i;.  
86a90 69 6e 74 20 6e 54 6f 74 61 6c 20 3d 20 30 3b 0a  int nTotal = 0;.
86aa0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 53    for(i=0; i<NCS
86ab0 49 5a 45 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e  IZE; i++){.    n
86ac0 54 6f 74 61 6c 20 2b 3d 20 6d 65 6d 2e 6e 41 6c  Total += mem.nAl
86ad0 6c 6f 63 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65  loc[i];.  }.  re
86ae0 74 75 72 6e 20 6e 54 6f 74 61 6c 3b 0a 7d 0a 0a  turn nTotal;.}..
86af0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
86b00 45 5f 4d 45 4d 44 45 42 55 47 20 2a 2f 0a 0a 2f  E_MEMDEBUG */../
86b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
86b20 6e 64 20 6f 66 20 6d 65 6d 32 2e 63 20 2a 2a 2a  nd of mem2.c ***
86b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
86b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
86b70 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 33 2e 63  egin file mem3.c
86b80 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
86b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
86bb0 2a 0a 2a 2a 20 32 30 30 37 20 4f 63 74 6f 62 65  *.** 2007 Octobe
86bc0 72 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 14.**.** The a
86bd0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
86be0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
86bf0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
86c00 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
86c10 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
86c20 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
86c30 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
86c40 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
86c50 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
86c60 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
86c70 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
86c80 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
86c90 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
86ca0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
86cb0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
86cc0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
86cd0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
86ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86d20 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
86d30 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20   contains the C 
86d40 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69  functions that i
86d50 6d 70 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72  mplement a memor
86d60 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  y.** allocation 
86d70 73 75 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73  subsystem for us
86d80 65 20 62 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a  e by SQLite. .**
86d90 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e  .** This version
86da0 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   of the memory a
86db0 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
86dc0 74 65 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a  tem omits all.**
86dd0 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29   use of malloc()
86de0 2e 20 54 68 65 20 53 51 4c 69 74 65 20 75 73 65  . The SQLite use
86df0 72 20 73 75 70 70 6c 69 65 73 20 61 20 62 6c 6f  r supplies a blo
86e00 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20  ck of memory.** 
86e10 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73  before calling s
86e20 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
86e30 65 28 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61  e() from which a
86e40 6c 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72  llocations.** ar
86e50 65 20 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72  e made and retur
86e60 6e 65 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c  ned by the xMall
86e70 6f 63 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f  oc() and xReallo
86e80 63 28 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  c() .** implemen
86e90 74 61 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71  tations. Once sq
86ea0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
86eb0 28 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c  () has been call
86ec0 65 64 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e  ed,.** the amoun
86ed0 74 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69  t of memory avai
86ee0 6c 61 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20  lable to SQLite 
86ef0 69 73 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e  is fixed and can
86f00 6e 6f 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65  not.** be change
86f10 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65  d..**.** This ve
86f20 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d  rsion of the mem
86f30 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  ory allocation s
86f40 75 62 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c  ubsystem is incl
86f50 75 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62  uded.** in the b
86f60 75 69 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c  uild only if SQL
86f70 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  ITE_ENABLE_MEMSY
86f80 53 33 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a  S3 is defined..*
86f90 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 33 2e 63  *.** $Id: mem3.c
86fa0 2c 76 20 31 2e 32 35 20 32 30 30 38 2f 31 31 2f  ,v 1.25 2008/11/
86fb0 31 39 20 31 36 3a 35 32 3a 34 34 20 64 61 6e 69  19 16:52:44 dani
86fc0 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f  elk1977 Exp $.*/
86fd0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72  ../*.** This ver
86fe0 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f  sion of the memo
86ff0 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20  ry allocator is 
87000 6f 6e 6c 79 20 62 75 69 6c 74 20 69 6e 74 6f 20  only built into 
87010 74 68 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 53  the library.** S
87020 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
87030 53 59 53 33 20 69 73 20 64 65 66 69 6e 65 64 2e  SYS3 is defined.
87040 20 44 65 66 69 6e 69 6e 67 20 74 68 69 73 20 73   Defining this s
87050 79 6d 62 6f 6c 20 64 6f 65 73 20 6e 6f 74 0a 2a  ymbol does not.*
87060 2a 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20  * mean that the 
87070 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 75 73 65  library will use
87080 20 61 20 6d 65 6d 6f 72 79 2d 70 6f 6f 6c 20 62   a memory-pool b
87090 79 20 64 65 66 61 75 6c 74 2c 20 6a 75 73 74 20  y default, just 
870a0 74 68 61 74 0a 2a 2a 20 69 74 20 69 73 20 61 76  that.** it is av
870b0 61 69 6c 61 62 6c 65 2e 20 54 68 65 20 6d 65 6d  ailable. The mem
870c0 70 6f 6f 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 69  pool allocator i
870d0 73 20 61 63 74 69 76 61 74 65 64 20 62 79 20 63  s activated by c
870e0 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65  alling.** sqlite
870f0 33 5f 63 6f 6e 66 69 67 28 29 2e 0a 2a 2f 0a 23  3_config()..*/.#
87100 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
87110 42 4c 45 5f 4d 45 4d 53 59 53 33 0a 0a 2f 2a 0a  BLE_MEMSYS3../*.
87120 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20  ** Maximum size 
87130 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20  (in Mem3Blocks) 
87140 6f 66 20 61 20 22 73 6d 61 6c 6c 22 20 63 68 75  of a "small" chu
87150 6e 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  nk..*/.#define M
87160 58 5f 53 4d 41 4c 4c 20 31 30 0a 0a 0a 2f 2a 0a  X_SMALL 10.../*.
87170 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65  ** Number of fre
87180 65 6c 69 73 74 20 68 61 73 68 20 73 6c 6f 74 73  elist hash slots
87190 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 48 41  .*/.#define N_HA
871a0 53 48 20 20 36 31 0a 0a 2f 2a 0a 2a 2a 20 41 20  SH  61../*.** A 
871b0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
871c0 6e 20 28 61 6c 73 6f 20 63 61 6c 6c 65 64 20 61  n (also called a
871d0 20 22 63 68 75 6e 6b 22 29 20 63 6f 6e 73 69 73   "chunk") consis
871e0 74 73 20 6f 66 20 74 77 6f 20 6f 72 20 0a 2a 2a  ts of two or .**
871f0 20 6d 6f 72 65 20 62 6c 6f 63 6b 73 20 77 68 65   more blocks whe
87200 72 65 20 65 61 63 68 20 62 6c 6f 63 6b 20 69 73  re each block is
87210 20 38 20 62 79 74 65 73 2e 20 20 54 68 65 20 66   8 bytes.  The f
87220 69 72 73 74 20 38 20 62 79 74 65 73 20 61 72 65  irst 8 bytes are
87230 20 0a 2a 2a 20 61 20 68 65 61 64 65 72 20 74 68   .** a header th
87240 61 74 20 69 73 20 6e 6f 74 20 72 65 74 75 72 6e  at is not return
87250 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 0a  ed to the user..
87260 2a 2a 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69 73  **.** A chunk is
87270 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 6c 6f   two or more blo
87280 63 6b 73 20 74 68 61 74 20 69 73 20 65 69 74 68  cks that is eith
87290 65 72 20 63 68 65 63 6b 65 64 20 6f 75 74 20 6f  er checked out o
872a0 72 0a 2a 2a 20 66 72 65 65 2e 20 20 54 68 65 20  r.** free.  The 
872b0 66 69 72 73 74 20 62 6c 6f 63 6b 20 68 61 73 20  first block has 
872c0 66 6f 72 6d 61 74 20 75 2e 68 64 72 2e 20 20 75  format u.hdr.  u
872d0 2e 68 64 72 2e 73 69 7a 65 34 78 20 69 73 20 34  .hdr.size4x is 4
872e0 20 74 69 6d 65 73 20 74 68 65 0a 2a 2a 20 73 69   times the.** si
872f0 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61  ze of the alloca
87300 74 69 6f 6e 20 69 6e 20 62 6c 6f 63 6b 73 20 69  tion in blocks i
87310 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  f the allocation
87320 20 69 73 20 66 72 65 65 2e 0a 2a 2a 20 54 68 65   is free..** The
87330 20 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 20   u.hdr.size4x&1 
87340 62 69 74 20 69 73 20 74 72 75 65 20 69 66 20 74  bit is true if t
87350 68 65 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63  he chunk is chec
87360 6b 65 64 20 6f 75 74 20 61 6e 64 0a 2a 2a 20 66  ked out and.** f
87370 61 6c 73 65 20 69 66 20 74 68 65 20 63 68 75 6e  alse if the chun
87380 6b 20 69 73 20 6f 6e 20 74 68 65 20 66 72 65 65  k is on the free
87390 6c 69 73 74 2e 20 20 54 68 65 20 75 2e 68 64 72  list.  The u.hdr
873a0 2e 73 69 7a 65 34 78 26 32 20 62 69 74 0a 2a 2a  .size4x&2 bit.**
873b0 20 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20   is true if the 
873c0 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69  previous chunk i
873d0 73 20 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e  s checked out an
873e0 64 20 66 61 6c 73 65 20 69 66 20 74 68 65 0a 2a  d false if the.*
873f0 2a 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b  * previous chunk
87400 20 69 73 20 66 72 65 65 2e 20 20 54 68 65 20 75   is free.  The u
87410 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 66 69  .hdr.prevSize fi
87420 65 6c 64 20 69 73 20 74 68 65 20 73 69 7a 65 20  eld is the size 
87430 6f 66 0a 2a 2a 20 74 68 65 20 70 72 65 76 69 6f  of.** the previo
87440 75 73 20 63 68 75 6e 6b 20 69 6e 20 62 6c 6f 63  us chunk in bloc
87450 6b 73 20 69 66 20 74 68 65 20 70 72 65 76 69 6f  ks if the previo
87460 75 73 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20 74  us chunk is on t
87470 68 65 0a 2a 2a 20 66 72 65 65 6c 69 73 74 2e 20  he.** freelist. 
87480 49 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  If the previous 
87490 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 64  chunk is checked
874a0 20 6f 75 74 2c 20 74 68 65 6e 0a 2a 2a 20 75 2e   out, then.** u.
874b0 68 64 72 2e 70 72 65 76 53 69 7a 65 20 63 61 6e  hdr.prevSize can
874c0 20 62 65 20 70 61 72 74 20 6f 66 20 74 68 65 20   be part of the 
874d0 64 61 74 61 20 66 6f 72 20 74 68 61 74 20 63 68  data for that ch
874e0 75 6e 6b 20 61 6e 64 20 73 68 6f 75 6c 64 0a 2a  unk and should.*
874f0 2a 20 6e 6f 74 20 62 65 20 72 65 61 64 20 6f 72  * not be read or
87500 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20   written..**.** 
87510 57 65 20 6f 66 74 65 6e 20 69 64 65 6e 74 69 66  We often identif
87520 79 20 61 20 63 68 75 6e 6b 20 62 79 20 69 74 73  y a chunk by its
87530 20 69 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61   index in mem3.a
87540 50 6f 6f 6c 5b 5d 2e 20 20 57 68 65 6e 0a 2a 2a  Pool[].  When.**
87550 20 74 68 69 73 20 69 73 20 64 6f 6e 65 2c 20 74   this is done, t
87560 68 65 20 63 68 75 6e 6b 20 69 6e 64 65 78 20 72  he chunk index r
87570 65 66 65 72 73 20 74 6f 20 74 68 65 20 73 65 63  efers to the sec
87580 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20  ond block of.** 
87590 74 68 65 20 63 68 75 6e 6b 2e 20 20 49 6e 20 74  the chunk.  In t
875a0 68 69 73 20 77 61 79 2c 20 74 68 65 20 66 69 72  his way, the fir
875b0 73 74 20 63 68 75 6e 6b 20 68 61 73 20 61 6e 20  st chunk has an 
875c0 69 6e 64 65 78 20 6f 66 20 31 2e 0a 2a 2a 20 41  index of 1..** A
875d0 20 63 68 75 6e 6b 20 69 6e 64 65 78 20 6f 66 20   chunk index of 
875e0 30 20 6d 65 61 6e 73 20 22 6e 6f 20 73 75 63 68  0 means "no such
875f0 20 63 68 75 6e 6b 22 20 61 6e 64 20 69 73 20 74   chunk" and is t
87600 68 65 20 65 71 75 69 76 61 6c 65 6e 74 0a 2a 2a  he equivalent.**
87610 20 6f 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   of a NULL point
87620 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65  er..**.** The se
87630 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 66 72  cond block of fr
87640 65 65 20 63 68 75 6e 6b 73 20 69 73 20 6f 66 20  ee chunks is of 
87650 74 68 65 20 66 6f 72 6d 20 75 2e 6c 69 73 74 2e  the form u.list.
87660 20 20 54 68 65 0a 2a 2a 20 74 77 6f 20 66 69 65    The.** two fie
87670 6c 64 73 20 66 6f 72 6d 20 61 20 64 6f 75 62 6c  lds form a doubl
87680 65 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66  e-linked list of
87690 20 63 68 75 6e 6b 73 20 6f 66 20 72 65 6c 61 74   chunks of relat
876a0 65 64 20 73 69 7a 65 73 2e 0a 2a 2a 20 50 6f 69  ed sizes..** Poi
876b0 6e 74 65 72 73 20 74 6f 20 74 68 65 20 68 65 61  nters to the hea
876c0 64 20 6f 66 20 74 68 65 20 6c 69 73 74 20 61 72  d of the list ar
876d0 65 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 33  e stored in mem3
876e0 2e 61 69 53 6d 61 6c 6c 5b 5d 20 0a 2a 2a 20 66  .aiSmall[] .** f
876f0 6f 72 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b  or smaller chunk
87700 73 20 61 6e 64 20 6d 65 6d 33 2e 61 69 48 61 73  s and mem3.aiHas
87710 68 5b 5d 20 66 6f 72 20 6c 61 72 67 65 72 20 63  h[] for larger c
87720 68 75 6e 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  hunks..**.** The
87730 20 73 65 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66   second block of
87740 20 61 20 63 68 75 6e 6b 20 69 73 20 75 73 65 72   a chunk is user
87750 20 64 61 74 61 20 69 66 20 74 68 65 20 63 68 75   data if the chu
87760 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20 0a 2a  nk is checked .*
87770 2a 20 6f 75 74 2e 20 20 49 66 20 61 20 63 68 75  * out.  If a chu
87780 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75  nk is checked ou
87790 74 2c 20 74 68 65 20 75 73 65 72 20 64 61 74 61  t, the user data
877a0 20 6d 61 79 20 65 78 74 65 6e 64 20 69 6e 74 6f   may extend into
877b0 0a 2a 2a 20 74 68 65 20 75 2e 68 64 72 2e 70 72  .** the u.hdr.pr
877c0 65 76 53 69 7a 65 20 76 61 6c 75 65 20 6f 66 20  evSize value of 
877d0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68  the following ch
877e0 75 6e 6b 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  unk..*/.typedef 
877f0 73 74 72 75 63 74 20 4d 65 6d 33 42 6c 6f 63 6b  struct Mem3Block
87800 20 4d 65 6d 33 42 6c 6f 63 6b 3b 0a 73 74 72 75   Mem3Block;.stru
87810 63 74 20 4d 65 6d 33 42 6c 6f 63 6b 20 7b 0a 20  ct Mem3Block {. 
87820 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 73 74 72   union {.    str
87830 75 63 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20  uct {.      u32 
87840 70 72 65 76 53 69 7a 65 3b 20 20 20 2f 2a 20 53  prevSize;   /* S
87850 69 7a 65 20 6f 66 20 70 72 65 76 69 6f 75 73 20  ize of previous 
87860 63 68 75 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f  chunk in Mem3Blo
87870 63 6b 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20  ck elements */. 
87880 20 20 20 20 20 75 33 32 20 73 69 7a 65 34 78 3b       u32 size4x;
87890 20 20 20 20 20 2f 2a 20 34 78 20 74 68 65 20 73       /* 4x the s
878a0 69 7a 65 20 6f 66 20 63 75 72 72 65 6e 74 20 63  ize of current c
878b0 68 75 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63  hunk in Mem3Bloc
878c0 6b 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20  k elements */.  
878d0 20 20 7d 20 68 64 72 3b 0a 20 20 20 20 73 74 72    } hdr;.    str
878e0 75 63 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20  uct {.      u32 
878f0 6e 65 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49  next;       /* I
87900 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f  ndex in mem3.aPo
87910 6f 6c 5b 5d 20 6f 66 20 6e 65 78 74 20 66 72 65  ol[] of next fre
87920 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 20 20  e chunk */.     
87930 20 75 33 32 20 70 72 65 76 3b 20 20 20 20 20 20   u32 prev;      
87940 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 6d 65 6d   /* Index in mem
87950 33 2e 61 50 6f 6f 6c 5b 5d 20 6f 66 20 70 72 65  3.aPool[] of pre
87960 76 69 6f 75 73 20 66 72 65 65 20 63 68 75 6e 6b  vious free chunk
87970 20 2a 2f 0a 20 20 20 20 7d 20 6c 69 73 74 3b 0a   */.    } list;.
87980 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a    } u;.};../*.**
87990 20 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74   All of the stat
879a0 69 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65  ic variables use
879b0 64 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65  d by this module
879c0 20 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a   are collected.*
879d0 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20  * into a single 
879e0 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20  structure named 
879f0 22 6d 65 6d 33 22 2e 20 20 54 68 69 73 20 69 73  "mem3".  This is
87a00 20 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20   to keep the.** 
87a10 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73  static variables
87a20 20 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74   organized and t
87a30 6f 20 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61  o reduce namespa
87a40 63 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20  ce pollution.** 
87a50 77 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65  when this module
87a60 20 69 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74   is combined wit
87a70 68 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61  h other in the a
87a80 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  malgamation..*/.
87a90 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53  static SQLITE_WS
87aa0 44 20 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f  D struct Mem3Glo
87ab0 62 61 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20  bal {.  /*.  ** 
87ac0 4d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65  Memory available
87ad0 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e   for allocation.
87ae0 20 6e 50 6f 6f 6c 20 69 73 20 74 68 65 20 73 69   nPool is the si
87af0 7a 65 20 6f 66 20 74 68 65 20 61 72 72 61 79 0a  ze of the array.
87b00 20 20 2a 2a 20 28 69 6e 20 4d 65 6d 33 42 6c 6f    ** (in Mem3Blo
87b10 63 6b 73 29 20 70 6f 69 6e 74 65 64 20 74 6f 20  cks) pointed to 
87b20 62 79 20 61 50 6f 6f 6c 20 6c 65 73 73 20 32 2e  by aPool less 2.
87b30 0a 20 20 2a 2f 0a 20 20 75 33 32 20 6e 50 6f 6f  .  */.  u32 nPoo
87b40 6c 3b 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a  l;.  Mem3Block *
87b50 61 50 6f 6f 6c 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  aPool;..  /*.  *
87b60 2a 20 54 72 75 65 20 69 66 20 77 65 20 61 72 65  * True if we are
87b70 20 65 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 6f   evaluating an o
87b80 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 61 6c  ut-of-memory cal
87b90 6c 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e  lback..  */.  in
87ba0 74 20 61 6c 61 72 6d 42 75 73 79 3b 0a 20 20 0a  t alarmBusy;.  .
87bb0 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20    /*.  ** Mutex 
87bc0 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73  to control acces
87bd0 73 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20  s to the memory 
87be0 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
87bf0 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  stem..  */.  sql
87c00 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65  ite3_mutex *mute
87c10 78 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20  x;.  .  /*.  ** 
87c20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 61 6d 6f 75  The minimum amou
87c30 6e 74 20 6f 66 20 66 72 65 65 20 73 70 61 63 65  nt of free space
87c40 20 74 68 61 74 20 77 65 20 68 61 76 65 20 73 65   that we have se
87c50 65 6e 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 6d  en..  */.  u32 m
87c60 6e 4d 61 73 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20  nMaster;..  /*. 
87c70 20 2a 2a 20 69 4d 61 73 74 65 72 20 69 73 20 74   ** iMaster is t
87c80 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
87c90 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 4d  master chunk.  M
87ca0 6f 73 74 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69  ost new allocati
87cb0 6f 6e 73 0a 20 20 2a 2a 20 6f 63 63 75 72 20 6f  ons.  ** occur o
87cc0 66 66 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b  ff of this chunk
87cd0 2e 20 20 73 7a 4d 61 73 74 65 72 20 69 73 20 74  .  szMaster is t
87ce0 68 65 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33  he size (in Mem3
87cf0 42 6c 6f 63 6b 73 29 0a 20 20 2a 2a 20 6f 66 20  Blocks).  ** of 
87d00 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61 73 74  the current mast
87d10 65 72 2e 20 20 69 4d 61 73 74 65 72 20 69 73 20  er.  iMaster is 
87d20 30 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f  0 if there is no
87d30 74 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 0a  t master chunk..
87d40 20 20 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20    ** The master 
87d50 63 68 75 6e 6b 20 69 73 20 6e 6f 74 20 69 6e 20  chunk is not in 
87d60 65 69 74 68 65 72 20 74 68 65 20 61 69 48 61 73  either the aiHas
87d70 68 5b 5d 20 6f 72 20 61 69 53 6d 61 6c 6c 5b 5d  h[] or aiSmall[]
87d80 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 69 4d 61  ..  */.  u32 iMa
87d90 73 74 65 72 3b 0a 20 20 75 33 32 20 73 7a 4d 61  ster;.  u32 szMa
87da0 73 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  ster;..  /*.  **
87db0 20 41 72 72 61 79 20 6f 66 20 6c 69 73 74 73 20   Array of lists 
87dc0 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 61  of free blocks a
87dd0 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20  ccording to the 
87de0 62 6c 6f 63 6b 20 73 69 7a 65 20 0a 20 20 2a 2a  block size .  **
87df0 20 66 6f 72 20 73 6d 61 6c 6c 65 72 20 63 68 75   for smaller chu
87e00 6e 6b 73 2c 20 6f 72 20 61 20 68 61 73 68 20 6f  nks, or a hash o
87e10 6e 20 74 68 65 20 62 6c 6f 63 6b 20 73 69 7a 65  n the block size
87e20 20 66 6f 72 20 6c 61 72 67 65 72 0a 20 20 2a 2a   for larger.  **
87e30 20 63 68 75 6e 6b 73 2e 0a 20 20 2a 2f 0a 20 20   chunks..  */.  
87e40 75 33 32 20 61 69 53 6d 61 6c 6c 5b 4d 58 5f 53  u32 aiSmall[MX_S
87e50 4d 41 4c 4c 2d 31 5d 3b 20 20 20 2f 2a 20 46 6f  MALL-1];   /* Fo
87e60 72 20 73 69 7a 65 73 20 32 20 74 68 72 6f 75 67  r sizes 2 throug
87e70 68 20 4d 58 5f 53 4d 41 4c 4c 2c 20 69 6e 63 6c  h MX_SMALL, incl
87e80 75 73 69 76 65 20 2a 2f 0a 20 20 75 33 32 20 61  usive */.  u32 a
87e90 69 48 61 73 68 5b 4e 5f 48 41 53 48 5d 3b 20 20  iHash[N_HASH];  
87ea0 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a        /* For siz
87eb0 65 73 20 4d 58 5f 53 4d 41 4c 4c 2b 31 20 61 6e  es MX_SMALL+1 an
87ec0 64 20 6c 61 72 67 65 72 20 2a 2f 0a 7d 20 6d 65  d larger */.} me
87ed0 6d 33 20 3d 20 7b 20 39 37 35 33 35 35 37 35 20  m3 = { 97535575 
87ee0 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 6d 65 6d 33  };..#define mem3
87ef0 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d   GLOBAL(struct M
87f00 65 6d 33 47 6c 6f 62 61 6c 2c 20 6d 65 6d 33 29  em3Global, mem3)
87f10 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74  ../*.** Unlink t
87f20 68 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 33  he chunk at mem3
87f30 2e 61 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c  .aPool[i] from l
87f40 69 73 74 20 69 74 20 69 73 20 63 75 72 72 65 6e  ist it is curren
87f50 74 6c 79 0a 2a 2a 20 6f 6e 2e 20 20 2a 70 52 6f  tly.** on.  *pRo
87f60 6f 74 20 69 73 20 74 68 65 20 6c 69 73 74 20 74  ot is the list t
87f70 68 61 74 20 69 20 69 73 20 61 20 6d 65 6d 62 65  hat i is a membe
87f80 72 20 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  r of..*/.static 
87f90 76 6f 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69  void memsys3Unli
87fa0 6e 6b 46 72 6f 6d 4c 69 73 74 28 75 33 32 20 69  nkFromList(u32 i
87fb0 2c 20 75 33 32 20 2a 70 52 6f 6f 74 29 7b 0a 20  , u32 *pRoot){. 
87fc0 20 75 33 32 20 6e 65 78 74 20 3d 20 6d 65 6d 33   u32 next = mem3
87fd0 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74  .aPool[i].u.list
87fe0 2e 6e 65 78 74 3b 0a 20 20 75 33 32 20 70 72 65  .next;.  u32 pre
87ff0 76 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  v = mem3.aPool[i
88000 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 3b 0a 20  ].u.list.prev;. 
88010 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
88020 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33  _mutex_held(mem3
88030 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28  .mutex) );.  if(
88040 20 70 72 65 76 3d 3d 30 20 29 7b 0a 20 20 20 20   prev==0 ){.    
88050 2a 70 52 6f 6f 74 20 3d 20 6e 65 78 74 3b 0a 20  *pRoot = next;. 
88060 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 33   }else{.    mem3
88070 2e 61 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c  .aPool[prev].u.l
88080 69 73 74 2e 6e 65 78 74 20 3d 20 6e 65 78 74 3b  ist.next = next;
88090 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 20  .  }.  if( next 
880a0 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f  ){.    mem3.aPoo
880b0 6c 5b 6e 65 78 74 5d 2e 75 2e 6c 69 73 74 2e 70  l[next].u.list.p
880c0 72 65 76 20 3d 20 70 72 65 76 3b 0a 20 20 7d 0a  rev = prev;.  }.
880d0 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e    mem3.aPool[i].
880e0 75 2e 6c 69 73 74 2e 6e 65 78 74 20 3d 20 30 3b  u.list.next = 0;
880f0 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d  .  mem3.aPool[i]
88100 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 30  .u.list.prev = 0
88110 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e  ;.}../*.** Unlin
88120 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69  k the chunk at i
88130 6e 64 65 78 20 69 20 66 72 6f 6d 20 0a 2a 2a 20  ndex i from .** 
88140 77 68 61 74 65 76 65 72 20 6c 69 73 74 20 69 73  whatever list is
88150 20 63 75 72 72 65 6e 74 6c 79 20 61 20 6d 65 6d   currently a mem
88160 62 65 72 20 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69  ber of..*/.stati
88170 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 55 6e  c void memsys3Un
88180 6c 69 6e 6b 28 75 33 32 20 69 29 7b 0a 20 20 75  link(u32 i){.  u
88190 33 32 20 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20  32 size, hash;. 
881a0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
881b0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33  _mutex_held(mem3
881c0 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  .mutex) );.  ass
881d0 65 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c  ert( (mem3.aPool
881e0 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  [i-1].u.hdr.size
881f0 34 78 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20  4x & 1)==0 );.  
88200 61 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a  assert( i>=1 );.
88210 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50    size = mem3.aP
88220 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73  ool[i-1].u.hdr.s
88230 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72  ize4x/4;.  asser
88240 74 28 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50  t( size==mem3.aP
88250 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e  ool[i+size-1].u.
88260 68 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a  hdr.prevSize );.
88270 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d    assert( size>=
88280 32 20 29 3b 0a 20 20 69 66 28 20 73 69 7a 65 20  2 );.  if( size 
88290 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20  <= MX_SMALL ){. 
882a0 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b     memsys3Unlink
882b0 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d  FromList(i, &mem
882c0 33 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32  3.aiSmall[size-2
882d0 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ]);.  }else{.   
882e0 20 68 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e   hash = size % N
882f0 5f 48 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79  _HASH;.    memsy
88300 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74  s3UnlinkFromList
88310 28 69 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68  (i, &mem3.aiHash
88320 5b 68 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a  [hash]);.  }.}..
88330 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63  /*.** Link the c
88340 68 75 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f  hunk at mem3.aPo
88350 6f 6c 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73  ol[i] so that is
88360 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 72 6f 6f   on the list roo
88370 74 65 64 0a 2a 2a 20 61 74 20 2a 70 52 6f 6f 74  ted.** at *pRoot
88380 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
88390 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f   memsys3LinkInto
883a0 4c 69 73 74 28 75 33 32 20 69 2c 20 75 33 32 20  List(u32 i, u32 
883b0 2a 70 52 6f 6f 74 29 7b 0a 20 20 61 73 73 65 72  *pRoot){.  asser
883c0 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
883d0 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78  _held(mem3.mutex
883e0 29 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f  ) );.  mem3.aPoo
883f0 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74  l[i].u.list.next
88400 20 3d 20 2a 70 52 6f 6f 74 3b 0a 20 20 6d 65 6d   = *pRoot;.  mem
88410 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73  3.aPool[i].u.lis
88420 74 2e 70 72 65 76 20 3d 20 30 3b 0a 20 20 69 66  t.prev = 0;.  if
88430 28 20 2a 70 52 6f 6f 74 20 29 7b 0a 20 20 20 20  ( *pRoot ){.    
88440 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 2a 70 52 6f 6f  mem3.aPool[*pRoo
88450 74 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d  t].u.list.prev =
88460 20 69 3b 0a 20 20 7d 0a 20 20 2a 70 52 6f 6f 74   i;.  }.  *pRoot
88470 20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c   = i;.}../*.** L
88480 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74  ink the chunk at
88490 20 69 6e 64 65 78 20 69 20 69 6e 74 6f 20 65 69   index i into ei
884a0 74 68 65 72 20 74 68 65 20 61 70 70 72 6f 70 72  ther the appropr
884b0 69 61 74 65 0a 2a 2a 20 73 6d 61 6c 6c 20 63 68  iate.** small ch
884c0 75 6e 6b 20 6c 69 73 74 2c 20 6f 72 20 69 6e 74  unk list, or int
884d0 6f 20 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e  o the large chun
884e0 6b 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f  k hash table..*/
884f0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
88500 73 79 73 33 4c 69 6e 6b 28 75 33 32 20 69 29 7b  sys3Link(u32 i){
88510 0a 20 20 75 33 32 20 73 69 7a 65 2c 20 68 61 73  .  u32 size, has
88520 68 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  h;.  assert( sql
88530 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
88540 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20  mem3.mutex) );. 
88550 20 61 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b   assert( i>=1 );
88560 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33  .  assert( (mem3
88570 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64  .aPool[i-1].u.hd
88580 72 2e 73 69 7a 65 34 78 20 26 20 31 29 3d 3d 30  r.size4x & 1)==0
88590 20 29 3b 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d   );.  size = mem
885a0 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  3.aPool[i-1].u.h
885b0 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61  dr.size4x/4;.  a
885c0 73 73 65 72 74 28 20 73 69 7a 65 3d 3d 6d 65 6d  ssert( size==mem
885d0 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31  3.aPool[i+size-1
885e0 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65  ].u.hdr.prevSize
885f0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69   );.  assert( si
88600 7a 65 3e 3d 32 20 29 3b 0a 20 20 69 66 28 20 73  ze>=2 );.  if( s
88610 69 7a 65 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20  ize <= MX_SMALL 
88620 29 7b 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69  ){.    memsys3Li
88630 6e 6b 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d  nkIntoList(i, &m
88640 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a 65  em3.aiSmall[size
88650 2d 32 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  -2]);.  }else{. 
88660 20 20 20 68 61 73 68 20 3d 20 73 69 7a 65 20 25     hash = size %
88670 20 4e 5f 48 41 53 48 3b 0a 20 20 20 20 6d 65 6d   N_HASH;.    mem
88680 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74  sys3LinkIntoList
88690 28 69 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68  (i, &mem3.aiHash
886a0 5b 68 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a  [hash]);.  }.}..
886b0 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41  /*.** If the STA
886c0 54 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73  TIC_MEM mutex is
886d0 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c   not already hel
886e0 64 2c 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77  d, obtain it now
886f0 2e 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77  . The mutex.** w
88700 69 6c 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68  ill already be h
88710 65 6c 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79  eld (obtained by
88720 20 63 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e   code in malloc.
88730 63 29 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33  c) if.** sqlite3
88740 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65  GlobalConfig.bMe
88750 6d 53 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a  mStat is true..*
88760 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65  /.static void me
88770 6d 73 79 73 33 45 6e 74 65 72 28 76 6f 69 64 29  msys3Enter(void)
88780 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  {.  if( sqlite3G
88790 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d  lobalConfig.bMem
887a0 73 74 61 74 3d 3d 30 20 26 26 20 6d 65 6d 33 2e  stat==0 && mem3.
887b0 6d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20  mutex==0 ){.    
887c0 6d 65 6d 33 2e 6d 75 74 65 78 20 3d 20 73 71 6c  mem3.mutex = sql
887d0 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
887e0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
887f0 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 73  IC_MEM);.  }.  s
88800 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
88810 65 72 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a  er(mem3.mutex);.
88820 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65  }.static void me
88830 6d 73 79 73 33 4c 65 61 76 65 28 76 6f 69 64 29  msys3Leave(void)
88840 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  {.  sqlite3_mute
88850 78 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 74  x_leave(mem3.mut
88860 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61  ex);.}../*.** Ca
88870 6c 6c 65 64 20 77 68 65 6e 20 77 65 20 61 72 65  lled when we are
88880 20 75 6e 61 62 6c 65 20 74 6f 20 73 61 74 69 73   unable to satis
88890 66 79 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e  fy an allocation
888a0 20 6f 66 20 6e 42 79 74 65 73 2e 0a 2a 2f 0a 73   of nBytes..*/.s
888b0 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79  tatic void memsy
888c0 73 33 4f 75 74 4f 66 4d 65 6d 6f 72 79 28 69 6e  s3OutOfMemory(in
888d0 74 20 6e 42 79 74 65 29 7b 0a 20 20 69 66 28 20  t nByte){.  if( 
888e0 21 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20  !mem3.alarmBusy 
888f0 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 72  ){.    mem3.alar
88900 6d 42 75 73 79 20 3d 20 31 3b 0a 20 20 20 20 61  mBusy = 1;.    a
88910 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
88920 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d  utex_held(mem3.m
88930 75 74 65 78 29 20 29 3b 0a 20 20 20 20 73 71 6c  utex) );.    sql
88940 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
88950 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem3.mutex);.  
88960 20 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73    sqlite3_releas
88970 65 5f 6d 65 6d 6f 72 79 28 6e 42 79 74 65 29 3b  e_memory(nByte);
88980 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
88990 65 78 5f 65 6e 74 65 72 28 6d 65 6d 33 2e 6d 75  ex_enter(mem3.mu
889a0 74 65 78 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 61  tex);.    mem3.a
889b0 6c 61 72 6d 42 75 73 79 20 3d 20 30 3b 0a 20 20  larmBusy = 0;.  
889c0 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 75 6e  }.}.../*.** Chun
889d0 6b 20 69 20 69 73 20 61 20 66 72 65 65 20 63 68  k i is a free ch
889e0 75 6e 6b 20 74 68 61 74 20 68 61 73 20 62 65 65  unk that has bee
889f0 6e 20 75 6e 6c 69 6e 6b 65 64 2e 20 20 41 64 6a  n unlinked.  Adj
88a00 75 73 74 20 69 74 73 20 0a 2a 2a 20 73 69 7a 65  ust its .** size
88a10 20 70 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20   parameters for 
88a20 63 68 65 63 6b 2d 6f 75 74 20 61 6e 64 20 72 65  check-out and re
88a30 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
88a40 6f 20 74 68 65 20 0a 2a 2a 20 75 73 65 72 20 70  o the .** user p
88a50 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 68  ortion of the ch
88a60 75 6e 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  unk..*/.static v
88a70 6f 69 64 20 2a 6d 65 6d 73 79 73 33 43 68 65 63  oid *memsys3Chec
88a80 6b 6f 75 74 28 75 33 32 20 69 2c 20 75 33 32 20  kout(u32 i, u32 
88a90 6e 42 6c 6f 63 6b 29 7b 0a 20 20 75 33 32 20 78  nBlock){.  u32 x
88aa0 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
88ab0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d  te3_mutex_held(m
88ac0 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20  em3.mutex) );.  
88ad0 61 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a  assert( i>=1 );.
88ae0 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 61    assert( mem3.a
88af0 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[i-1].u.hdr.
88b00 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b  size4x/4==nBlock
88b10 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65   );.  assert( me
88b20 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63  m3.aPool[i+nBloc
88b30 6b 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53  k-1].u.hdr.prevS
88b40 69 7a 65 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20  ize==nBlock );. 
88b50 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b   x = mem3.aPool[
88b60 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  i-1].u.hdr.size4
88b70 78 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  x;.  mem3.aPool[
88b80 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  i-1].u.hdr.size4
88b90 78 20 3d 20 6e 42 6c 6f 63 6b 2a 34 20 7c 20 31  x = nBlock*4 | 1
88ba0 20 7c 20 28 78 26 32 29 3b 0a 20 20 6d 65 6d 33   | (x&2);.  mem3
88bb0 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d  .aPool[i+nBlock-
88bc0 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a  1].u.hdr.prevSiz
88bd0 65 20 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 6d 65  e = nBlock;.  me
88be0 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63  m3.aPool[i+nBloc
88bf0 6b 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  k-1].u.hdr.size4
88c00 78 20 7c 3d 20 32 3b 0a 20 20 72 65 74 75 72 6e  x |= 2;.  return
88c10 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 3b   &mem3.aPool[i];
88c20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 72 76 65 20  .}../*.** Carve 
88c30 61 20 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74  a piece off of t
88c40 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 65  he end of the me
88c50 6d 33 2e 69 4d 61 73 74 65 72 20 66 72 65 65 20  m3.iMaster free 
88c60 63 68 75 6e 6b 2e 0a 2a 2a 20 52 65 74 75 72 6e  chunk..** Return
88c70 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
88c80 65 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e  e new allocation
88c90 2e 20 20 4f 72 2c 20 69 66 20 74 68 65 20 6d 61  .  Or, if the ma
88ca0 73 74 65 72 20 63 68 75 6e 6b 0a 2a 2a 20 69 73  ster chunk.** is
88cb0 20 6e 6f 74 20 6c 61 72 67 65 20 65 6e 6f 75 67   not large enoug
88cc0 68 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a  h, return 0..*/.
88cd0 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d  static void *mem
88ce0 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 75  sys3FromMaster(u
88cf0 33 32 20 6e 42 6c 6f 63 6b 29 7b 0a 20 20 61 73  32 nBlock){.  as
88d00 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
88d10 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75  tex_held(mem3.mu
88d20 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
88d30 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3e  ( mem3.szMaster>
88d40 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 69 66 28  =nBlock );.  if(
88d50 20 6e 42 6c 6f 63 6b 3e 3d 6d 65 6d 33 2e 73 7a   nBlock>=mem3.sz
88d60 4d 61 73 74 65 72 2d 31 20 29 7b 0a 20 20 20 20  Master-1 ){.    
88d70 2f 2a 20 55 73 65 20 74 68 65 20 65 6e 74 69 72  /* Use the entir
88d80 65 20 6d 61 73 74 65 72 20 2a 2f 0a 20 20 20 20  e master */.    
88d90 76 6f 69 64 20 2a 70 20 3d 20 6d 65 6d 73 79 73  void *p = memsys
88da0 33 43 68 65 63 6b 6f 75 74 28 6d 65 6d 33 2e 69  3Checkout(mem3.i
88db0 4d 61 73 74 65 72 2c 20 6d 65 6d 33 2e 73 7a 4d  Master, mem3.szM
88dc0 61 73 74 65 72 29 3b 0a 20 20 20 20 6d 65 6d 33  aster);.    mem3
88dd0 2e 69 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20  .iMaster = 0;.  
88de0 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20    mem3.szMaster 
88df0 3d 20 30 3b 0a 20 20 20 20 6d 65 6d 33 2e 6d 6e  = 0;.    mem3.mn
88e00 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20  Master = 0;.    
88e10 72 65 74 75 72 6e 20 70 3b 0a 20 20 7d 65 6c 73  return p;.  }els
88e20 65 7b 0a 20 20 20 20 2f 2a 20 53 70 6c 69 74 20  e{.    /* Split 
88e30 74 68 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b  the master block
88e40 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 74 61  .  Return the ta
88e50 69 6c 2e 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e  il. */.    u32 n
88e60 65 77 69 2c 20 78 3b 0a 20 20 20 20 6e 65 77 69  ewi, x;.    newi
88e70 20 3d 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20   = mem3.iMaster 
88e80 2b 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20  + mem3.szMaster 
88e90 2d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 20 20 61 73  - nBlock;.    as
88ea0 73 65 72 74 28 20 6e 65 77 69 20 3e 20 6d 65 6d  sert( newi > mem
88eb0 33 2e 69 4d 61 73 74 65 72 2b 31 20 29 3b 0a 20  3.iMaster+1 );. 
88ec0 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65     mem3.aPool[me
88ed0 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e  m3.iMaster+mem3.
88ee0 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64  szMaster-1].u.hd
88ef0 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c  r.prevSize = nBl
88f00 6f 63 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50  ock;.    mem3.aP
88f10 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72  ool[mem3.iMaster
88f20 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31  +mem3.szMaster-1
88f30 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 7c  ].u.hdr.size4x |
88f40 3d 20 32 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50  = 2;.    mem3.aP
88f50 6f 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64  ool[newi-1].u.hd
88f60 72 2e 73 69 7a 65 34 78 20 3d 20 6e 42 6c 6f 63  r.size4x = nBloc
88f70 6b 2a 34 20 2b 20 31 3b 0a 20 20 20 20 6d 65 6d  k*4 + 1;.    mem
88f80 33 2e 73 7a 4d 61 73 74 65 72 20 2d 3d 20 6e 42  3.szMaster -= nB
88f90 6c 6f 63 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61  lock;.    mem3.a
88fa0 50 6f 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68  Pool[newi-1].u.h
88fb0 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65  dr.prevSize = me
88fc0 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20  m3.szMaster;.   
88fd0 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b   x = mem3.aPool[
88fe0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e  mem3.iMaster-1].
88ff0 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32  u.hdr.size4x & 2
89000 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ;.    mem3.aPool
89010 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d  [mem3.iMaster-1]
89020 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20  .u.hdr.size4x = 
89030 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20  mem3.szMaster*4 
89040 7c 20 78 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d  | x;.    if( mem
89050 33 2e 73 7a 4d 61 73 74 65 72 20 3c 20 6d 65 6d  3.szMaster < mem
89060 33 2e 6d 6e 4d 61 73 74 65 72 20 29 7b 0a 20 20  3.mnMaster ){.  
89070 20 20 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65      mem3.mnMaste
89080 72 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  r = mem3.szMaste
89090 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  r;.    }.    ret
890a0 75 72 6e 20 28 76 6f 69 64 2a 29 26 6d 65 6d 33  urn (void*)&mem3
890b0 2e 61 50 6f 6f 6c 5b 6e 65 77 69 5d 3b 0a 20 20  .aPool[newi];.  
890c0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 2a 70 52 6f 6f  }.}../*.** *pRoo
890d0 74 20 69 73 20 74 68 65 20 68 65 61 64 20 6f 66  t is the head of
890e0 20 61 20 6c 69 73 74 20 6f 66 20 66 72 65 65 20   a list of free 
890f0 63 68 75 6e 6b 73 20 6f 66 20 74 68 65 20 73 61  chunks of the sa
89100 6d 65 20 73 69 7a 65 0a 2a 2a 20 6f 72 20 73 61  me size.** or sa
89110 6d 65 20 73 69 7a 65 20 68 61 73 68 2e 20 20 49  me size hash.  I
89120 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 2a  n other words, *
89130 70 52 6f 6f 74 20 69 73 20 61 6e 20 65 6e 74 72  pRoot is an entr
89140 79 20 69 6e 20 65 69 74 68 65 72 0a 2a 2a 20 6d  y in either.** m
89150 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 5d 20 6f 72  em3.aiSmall[] or
89160 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 5d 2e 20   mem3.aiHash[]. 
89170 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75   .**.** This rou
89180 74 69 6e 65 20 65 78 61 6d 69 6e 65 73 20 61 6c  tine examines al
89190 6c 20 65 6e 74 72 69 65 73 20 6f 6e 20 74 68 65  l entries on the
891a0 20 67 69 76 65 6e 20 6c 69 73 74 20 61 6e 64 20   given list and 
891b0 74 72 69 65 73 0a 2a 2a 20 74 6f 20 63 6f 61 6c  tries.** to coal
891c0 65 73 63 65 20 65 61 63 68 20 65 6e 74 72 69 65  esce each entrie
891d0 73 20 77 69 74 68 20 61 64 6a 61 63 65 6e 74 20  s with adjacent 
891e0 66 72 65 65 20 63 68 75 6e 6b 73 2e 20 20 0a 2a  free chunks.  .*
891f0 2a 0a 2a 2a 20 49 66 20 69 74 20 73 65 65 73 20  *.** If it sees 
89200 61 20 63 68 75 6e 6b 20 74 68 61 74 20 69 73 20  a chunk that is 
89210 6c 61 72 67 65 72 20 74 68 61 6e 20 6d 65 6d 33  larger than mem3
89220 2e 69 4d 61 73 74 65 72 2c 20 69 74 20 72 65 70  .iMaster, it rep
89230 6c 61 63 65 73 20 0a 2a 2a 20 74 68 65 20 63 75  laces .** the cu
89240 72 72 65 6e 74 20 6d 65 6d 33 2e 69 4d 61 73 74  rrent mem3.iMast
89250 65 72 20 77 69 74 68 20 74 68 65 20 6e 65 77 20  er with the new 
89260 6c 61 72 67 65 72 20 63 68 75 6e 6b 2e 20 20 49  larger chunk.  I
89270 6e 20 6f 72 64 65 72 20 66 6f 72 0a 2a 2a 20 74  n order for.** t
89280 68 69 73 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72  his mem3.iMaster
89290 20 72 65 70 6c 61 63 65 6d 65 6e 74 20 74 6f 20   replacement to 
892a0 77 6f 72 6b 2c 20 74 68 65 20 6d 61 73 74 65 72  work, the master
892b0 20 63 68 75 6e 6b 20 6d 75 73 74 20 62 65 0a 2a   chunk must be.*
892c0 2a 20 6c 69 6e 6b 65 64 20 69 6e 74 6f 20 74 68  * linked into th
892d0 65 20 68 61 73 68 20 74 61 62 6c 65 73 2e 20 20  e hash tables.  
892e0 54 68 61 74 20 69 73 20 6e 6f 74 20 74 68 65 20  That is not the 
892f0 6e 6f 72 6d 61 6c 20 73 74 61 74 65 20 6f 66 0a  normal state of.
89300 2a 2a 20 61 66 66 61 69 72 73 2c 20 6f 66 20 63  ** affairs, of c
89310 6f 75 72 73 65 2e 20 20 54 68 65 20 63 61 6c 6c  ourse.  The call
89320 69 6e 67 20 72 6f 75 74 69 6e 65 20 6d 75 73 74  ing routine must
89330 20 6c 69 6e 6b 20 74 68 65 20 6d 61 73 74 65 72   link the master
89340 0a 2a 2a 20 63 68 75 6e 6b 20 62 65 66 6f 72 65  .** chunk before
89350 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 72   invoking this r
89360 6f 75 74 69 6e 65 2c 20 74 68 65 6e 20 6d 75 73  outine, then mus
89370 74 20 75 6e 6c 69 6e 6b 20 74 68 65 20 28 70 6f  t unlink the (po
89380 73 73 69 62 6c 79 0a 2a 2a 20 63 68 61 6e 67 65  ssibly.** change
89390 64 29 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20  d) master chunk 
893a0 6f 6e 63 65 20 74 68 69 73 20 72 6f 75 74 69 6e  once this routin
893b0 65 20 68 61 73 20 66 69 6e 69 73 68 65 64 2e 0a  e has finished..
893c0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d  */.static void m
893d0 65 6d 73 79 73 33 4d 65 72 67 65 28 75 33 32 20  emsys3Merge(u32 
893e0 2a 70 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 69  *pRoot){.  u32 i
893f0 4e 65 78 74 2c 20 70 72 65 76 2c 20 73 69 7a 65  Next, prev, size
89400 2c 20 69 2c 20 78 3b 0a 0a 20 20 61 73 73 65 72  , i, x;..  asser
89410 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
89420 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78  _held(mem3.mutex
89430 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 2a 70 52  ) );.  for(i=*pR
89440 6f 6f 74 3b 20 69 3e 30 3b 20 69 3d 69 4e 65 78  oot; i>0; i=iNex
89450 74 29 7b 0a 20 20 20 20 69 4e 65 78 74 20 3d 20  t){.    iNext = 
89460 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e  mem3.aPool[i].u.
89470 6c 69 73 74 2e 6e 65 78 74 3b 0a 20 20 20 20 73  list.next;.    s
89480 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ize = mem3.aPool
89490 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  [i-1].u.hdr.size
894a0 34 78 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  4x;.    assert( 
894b0 28 73 69 7a 65 26 31 29 3d 3d 30 20 29 3b 0a 20  (size&1)==0 );. 
894c0 20 20 20 69 66 28 20 28 73 69 7a 65 26 32 29 3d     if( (size&2)=
894d0 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73  =0 ){.      mems
894e0 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73  ys3UnlinkFromLis
894f0 74 28 69 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20  t(i, pRoot);.   
89500 20 20 20 61 73 73 65 72 74 28 20 69 20 3e 20 6d     assert( i > m
89510 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75  em3.aPool[i-1].u
89520 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b  .hdr.prevSize );
89530 0a 20 20 20 20 20 20 70 72 65 76 20 3d 20 69 20  .      prev = i 
89540 2d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31  - mem3.aPool[i-1
89550 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65  ].u.hdr.prevSize
89560 3b 0a 20 20 20 20 20 20 69 66 28 20 70 72 65 76  ;.      if( prev
89570 3d 3d 69 4e 65 78 74 20 29 7b 0a 20 20 20 20 20  ==iNext ){.     
89580 20 20 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e     iNext = mem3.
89590 61 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69  aPool[prev].u.li
895a0 73 74 2e 6e 65 78 74 3b 0a 20 20 20 20 20 20 7d  st.next;.      }
895b0 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e  .      memsys3Un
895c0 6c 69 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20  link(prev);.    
895d0 20 20 73 69 7a 65 20 3d 20 69 20 2b 20 73 69 7a    size = i + siz
895e0 65 2f 34 20 2d 20 70 72 65 76 3b 0a 20 20 20 20  e/4 - prev;.    
895f0 20 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c    x = mem3.aPool
89600 5b 70 72 65 76 2d 31 5d 2e 75 2e 68 64 72 2e 73  [prev-1].u.hdr.s
89610 69 7a 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20  ize4x & 2;.     
89620 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76   mem3.aPool[prev
89630 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
89640 20 3d 20 73 69 7a 65 2a 34 20 7c 20 78 3b 0a 20   = size*4 | x;. 
89650 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b       mem3.aPool[
89660 70 72 65 76 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68  prev+size-1].u.h
89670 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 73 69  dr.prevSize = si
89680 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73  ze;.      memsys
89690 33 4c 69 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20  3Link(prev);.   
896a0 20 20 20 69 20 3d 20 70 72 65 76 3b 0a 20 20 20     i = prev;.   
896b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 69   }else{.      si
896c0 7a 65 20 2f 3d 20 34 3b 0a 20 20 20 20 7d 0a 20  ze /= 4;.    }. 
896d0 20 20 20 69 66 28 20 73 69 7a 65 3e 6d 65 6d 33     if( size>mem3
896e0 2e 73 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20  .szMaster ){.   
896f0 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20     mem3.iMaster 
89700 3d 20 69 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e  = i;.      mem3.
89710 73 7a 4d 61 73 74 65 72 20 3d 20 73 69 7a 65 3b  szMaster = size;
89720 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
89730 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 62 6c 6f  .** Return a blo
89740 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 6f 66 20  ck of memory of 
89750 61 74 20 6c 65 61 73 74 20 6e 42 79 74 65 73 20  at least nBytes 
89760 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 74 75  in size..** Retu
89770 72 6e 20 4e 55 4c 4c 20 69 66 20 75 6e 61 62 6c  rn NULL if unabl
89780 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  e..**.** This fu
89790 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74  nction assumes t
897a0 68 61 74 20 74 68 65 20 6e 65 63 65 73 73 61 72  hat the necessar
897b0 79 20 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e  y mutexes, if an
897c0 79 2c 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64  y, are.** alread
897d0 79 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61  y held by the ca
897e0 6c 6c 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73  ller. Hence "Uns
897f0 61 66 65 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  afe"..*/.static 
89800 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 4d 61 6c  void *memsys3Mal
89810 6c 6f 63 55 6e 73 61 66 65 28 69 6e 74 20 6e 42  locUnsafe(int nB
89820 79 74 65 29 7b 0a 20 20 75 33 32 20 69 3b 0a 20  yte){.  u32 i;. 
89830 20 75 33 32 20 6e 42 6c 6f 63 6b 3b 0a 20 20 75   u32 nBlock;.  u
89840 33 32 20 74 6f 46 72 65 65 3b 0a 0a 20 20 61 73  32 toFree;..  as
89850 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
89860 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75  tex_held(mem3.mu
89870 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
89880 28 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f  ( sizeof(Mem3Blo
89890 63 6b 29 3d 3d 38 20 29 3b 0a 20 20 69 66 28 20  ck)==8 );.  if( 
898a0 6e 42 79 74 65 3c 3d 31 32 20 29 7b 0a 20 20 20  nByte<=12 ){.   
898b0 20 6e 42 6c 6f 63 6b 20 3d 20 32 3b 0a 20 20 7d   nBlock = 2;.  }
898c0 65 6c 73 65 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b  else{.    nBlock
898d0 20 3d 20 28 6e 42 79 74 65 20 2b 20 31 31 29 2f   = (nByte + 11)/
898e0 38 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  8;.  }.  assert(
898f0 20 6e 42 6c 6f 63 6b 3e 3d 32 20 29 3b 0a 0a 20   nBlock>=2 );.. 
89900 20 2f 2a 20 53 54 45 50 20 31 3a 0a 20 20 2a 2a   /* STEP 1:.  **
89910 20 4c 6f 6f 6b 20 66 6f 72 20 61 6e 20 65 6e 74   Look for an ent
89920 72 79 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63  ry of the correc
89930 74 20 73 69 7a 65 20 69 6e 20 65 69 74 68 65 72  t size in either
89940 20 74 68 65 20 73 6d 61 6c 6c 0a 20 20 2a 2a 20   the small.  ** 
89950 63 68 75 6e 6b 20 74 61 62 6c 65 20 6f 72 20 69  chunk table or i
89960 6e 20 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e  n the large chun
89970 6b 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54  k hash table.  T
89980 68 69 73 20 69 73 0a 20 20 2a 2a 20 73 75 63 63  his is.  ** succ
89990 65 73 73 66 75 6c 20 6d 6f 73 74 20 6f 66 20 74  essful most of t
899a0 68 65 20 74 69 6d 65 20 28 61 62 6f 75 74 20 39  he time (about 9
899b0 20 74 69 6d 65 73 20 6f 75 74 20 6f 66 20 31 30   times out of 10
899c0 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42  )..  */.  if( nB
899d0 6c 6f 63 6b 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c  lock <= MX_SMALL
899e0 20 29 7b 0a 20 20 20 20 69 20 3d 20 6d 65 6d 33   ){.    i = mem3
899f0 2e 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d  .aiSmall[nBlock-
89a00 32 5d 3b 0a 20 20 20 20 69 66 28 20 69 3e 30 20  2];.    if( i>0 
89a10 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33  ){.      memsys3
89a20 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69  UnlinkFromList(i
89a30 2c 20 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b  , &mem3.aiSmall[
89a40 6e 42 6c 6f 63 6b 2d 32 5d 29 3b 0a 20 20 20 20  nBlock-2]);.    
89a50 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33    return memsys3
89a60 43 68 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f  Checkout(i, nBlo
89a70 63 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  ck);.    }.  }el
89a80 73 65 7b 0a 20 20 20 20 69 6e 74 20 68 61 73 68  se{.    int hash
89a90 20 3d 20 6e 42 6c 6f 63 6b 20 25 20 4e 5f 48 41   = nBlock % N_HA
89aa0 53 48 3b 0a 20 20 20 20 66 6f 72 28 69 3d 6d 65  SH;.    for(i=me
89ab0 6d 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 3b  m3.aiHash[hash];
89ac0 20 69 3e 30 3b 20 69 3d 6d 65 6d 33 2e 61 50 6f   i>0; i=mem3.aPo
89ad0 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78  ol[i].u.list.nex
89ae0 74 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6d 65  t){.      if( me
89af0 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e  m3.aPool[i-1].u.
89b00 68 64 72 2e 73 69 7a 65 34 78 2f 34 3d 3d 6e 42  hdr.size4x/4==nB
89b10 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20  lock ){.        
89b20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f  memsys3UnlinkFro
89b30 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61  mList(i, &mem3.a
89b40 69 48 61 73 68 5b 68 61 73 68 5d 29 3b 0a 20 20  iHash[hash]);.  
89b50 20 20 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d        return mem
89b60 73 79 73 33 43 68 65 63 6b 6f 75 74 28 69 2c 20  sys3Checkout(i, 
89b70 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d  nBlock);.      }
89b80 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
89b90 20 53 54 45 50 20 32 3a 0a 20 20 2a 2a 20 54 72   STEP 2:.  ** Tr
89ba0 79 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65  y to satisfy the
89bb0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 62 79 20 63   allocation by c
89bc0 61 72 76 69 6e 67 20 61 20 70 69 65 63 65 20 6f  arving a piece o
89bd0 66 66 20 6f 66 20 74 68 65 20 65 6e 64 0a 20 20  ff of the end.  
89be0 2a 2a 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72  ** of the master
89bf0 20 63 68 75 6e 6b 2e 20 20 54 68 69 73 20 73 74   chunk.  This st
89c00 65 70 20 75 73 75 61 6c 6c 79 20 77 6f 72 6b 73  ep usually works
89c10 20 69 66 20 73 74 65 70 20 31 20 66 61 69 6c 73   if step 1 fails
89c20 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d  ..  */.  if( mem
89c30 33 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f  3.szMaster>=nBlo
89c40 63 6b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ck ){.    return
89c50 20 6d 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74   memsys3FromMast
89c60 65 72 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a  er(nBlock);.  }.
89c70 0a 0a 20 20 2f 2a 20 53 54 45 50 20 33 3a 20 20  ..  /* STEP 3:  
89c80 0a 20 20 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75  .  ** Loop throu
89c90 67 68 20 74 68 65 20 65 6e 74 69 72 65 20 6d 65  gh the entire me
89ca0 6d 6f 72 79 20 70 6f 6f 6c 2e 20 20 43 6f 61 6c  mory pool.  Coal
89cb0 65 73 63 65 20 61 64 6a 61 63 65 6e 74 20 66 72  esce adjacent fr
89cc0 65 65 0a 20 20 2a 2a 20 63 68 75 6e 6b 73 2e 20  ee.  ** chunks. 
89cd0 20 52 65 63 6f 6d 70 75 74 65 20 74 68 65 20 6d   Recompute the m
89ce0 61 73 74 65 72 20 63 68 75 6e 6b 20 61 73 20 74  aster chunk as t
89cf0 68 65 20 6c 61 72 67 65 73 74 20 66 72 65 65 20  he largest free 
89d00 63 68 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65 6e  chunk..  ** Then
89d10 20 74 72 79 20 61 67 61 69 6e 20 74 6f 20 73 61   try again to sa
89d20 74 69 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61  tisfy the alloca
89d30 74 69 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20  tion by carving 
89d40 61 20 70 69 65 63 65 20 6f 66 66 0a 20 20 2a 2a  a piece off.  **
89d50 20 6f 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74   of the end of t
89d60 68 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e  he master chunk.
89d70 20 20 54 68 69 73 20 73 74 65 70 20 68 61 70 70    This step happ
89d80 65 6e 73 20 76 65 72 79 0a 20 20 2a 2a 20 72 61  ens very.  ** ra
89d90 72 65 6c 79 20 28 77 65 20 68 6f 70 65 21 29 0a  rely (we hope!).
89da0 20 20 2a 2f 0a 20 20 66 6f 72 28 74 6f 46 72 65    */.  for(toFre
89db0 65 3d 6e 42 6c 6f 63 6b 2a 31 36 3b 20 74 6f 46  e=nBlock*16; toF
89dc0 72 65 65 3c 28 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a  ree<(mem3.nPool*
89dd0 31 36 29 3b 20 74 6f 46 72 65 65 20 2a 3d 20 32  16); toFree *= 2
89de0 29 7b 0a 20 20 20 20 6d 65 6d 73 79 73 33 4f 75  ){.    memsys3Ou
89df0 74 4f 66 4d 65 6d 6f 72 79 28 74 6f 46 72 65 65  tOfMemory(toFree
89e00 29 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e  );.    if( mem3.
89e10 69 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20  iMaster ){.     
89e20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 6d 65 6d   memsys3Link(mem
89e30 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  3.iMaster);.    
89e40 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d    mem3.iMaster =
89e50 20 30 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73   0;.      mem3.s
89e60 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20  zMaster = 0;.   
89e70 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20   }.    for(i=0; 
89e80 69 3c 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a  i<N_HASH; i++){.
89e90 20 20 20 20 20 20 6d 65 6d 73 79 73 33 4d 65 72        memsys3Mer
89ea0 67 65 28 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b  ge(&mem3.aiHash[
89eb0 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  i]);.    }.    f
89ec0 6f 72 28 69 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41  or(i=0; i<MX_SMA
89ed0 4c 4c 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  LL-1; i++){.    
89ee0 20 20 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 26    memsys3Merge(&
89ef0 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 29  mem3.aiSmall[i])
89f00 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
89f10 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 29 7b  mem3.szMaster ){
89f20 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e  .      memsys3Un
89f30 6c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65  link(mem3.iMaste
89f40 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65  r);.      if( me
89f50 6d 33 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c  m3.szMaster>=nBl
89f60 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ock ){.        r
89f70 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 46 72 6f  eturn memsys3Fro
89f80 6d 4d 61 73 74 65 72 28 6e 42 6c 6f 63 6b 29 3b  mMaster(nBlock);
89f90 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
89fa0 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 6e 65   }..  /* If none
89fb0 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 77 6f   of the above wo
89fc0 72 6b 65 64 2c 20 74 68 65 6e 20 77 65 20 66 61  rked, then we fa
89fd0 69 6c 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  il. */.  return 
89fe0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65  0;.}../*.** Free
89ff0 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   an outstanding 
8a000 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
8a010 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  n..**.** This fu
8a020 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74  nction assumes t
8a030 68 61 74 20 74 68 65 20 6e 65 63 65 73 73 61 72  hat the necessar
8a040 79 20 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e  y mutexes, if an
8a050 79 2c 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64  y, are.** alread
8a060 79 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61  y held by the ca
8a070 6c 6c 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73  ller. Hence "Uns
8a080 61 66 65 22 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65  afe"..*/.void me
8a090 6d 73 79 73 33 46 72 65 65 55 6e 73 61 66 65 28  msys3FreeUnsafe(
8a0a0 76 6f 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 4d  void *pOld){.  M
8a0b0 65 6d 33 42 6c 6f 63 6b 20 2a 70 20 3d 20 28 4d  em3Block *p = (M
8a0c0 65 6d 33 42 6c 6f 63 6b 2a 29 70 4f 6c 64 3b 0a  em3Block*)pOld;.
8a0d0 20 20 69 6e 74 20 69 3b 0a 20 20 75 33 32 20 73    int i;.  u32 s
8a0e0 69 7a 65 2c 20 78 3b 0a 20 20 61 73 73 65 72 74  ize, x;.  assert
8a0f0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
8a100 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29  held(mem3.mutex)
8a110 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 3e   );.  assert( p>
8a120 6d 65 6d 33 2e 61 50 6f 6f 6c 20 26 26 20 70 3c  mem3.aPool && p<
8a130 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33  &mem3.aPool[mem3
8a140 2e 6e 50 6f 6f 6c 5d 20 29 3b 0a 20 20 69 20 3d  .nPool] );.  i =
8a150 20 70 20 2d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 3b   p - mem3.aPool;
8a160 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33  .  assert( (mem3
8a170 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64  .aPool[i-1].u.hd
8a180 72 2e 73 69 7a 65 34 78 26 31 29 3d 3d 31 20 29  r.size4x&1)==1 )
8a190 3b 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e  ;.  size = mem3.
8a1a0 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72  aPool[i-1].u.hdr
8a1b0 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73  .size4x/4;.  ass
8a1c0 65 72 74 28 20 69 2b 73 69 7a 65 3c 3d 6d 65 6d  ert( i+size<=mem
8a1d0 33 2e 6e 50 6f 6f 6c 2b 31 20 29 3b 0a 20 20 6d  3.nPool+1 );.  m
8a1e0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75  em3.aPool[i-1].u
8a1f0 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 3d 20 7e  .hdr.size4x &= ~
8a200 31 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  1;.  mem3.aPool[
8a210 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e  i+size-1].u.hdr.
8a220 70 72 65 76 53 69 7a 65 20 3d 20 73 69 7a 65 3b  prevSize = size;
8a230 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b  .  mem3.aPool[i+
8a240 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  size-1].u.hdr.si
8a250 7a 65 34 78 20 26 3d 20 7e 32 3b 0a 20 20 6d 65  ze4x &= ~2;.  me
8a260 6d 73 79 73 33 4c 69 6e 6b 28 69 29 3b 0a 0a 20  msys3Link(i);.. 
8a270 20 2f 2a 20 54 72 79 20 74 6f 20 65 78 70 61 6e   /* Try to expan
8a280 64 20 74 68 65 20 6d 61 73 74 65 72 20 75 73 69  d the master usi
8a290 6e 67 20 74 68 65 20 6e 65 77 6c 79 20 66 72 65  ng the newly fre
8a2a0 65 64 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 66  ed chunk */.  if
8a2b0 28 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 29  ( mem3.iMaster )
8a2c0 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65  {.    while( (me
8a2d0 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d  m3.aPool[mem3.iM
8a2e0 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73  aster-1].u.hdr.s
8a2f0 69 7a 65 34 78 26 32 29 3d 3d 30 20 29 7b 0a 20  ize4x&2)==0 ){. 
8a300 20 20 20 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33       size = mem3
8a310 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73  .aPool[mem3.iMas
8a320 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65  ter-1].u.hdr.pre
8a330 76 53 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d  vSize;.      mem
8a340 33 2e 69 4d 61 73 74 65 72 20 2d 3d 20 73 69 7a  3.iMaster -= siz
8a350 65 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a  e;.      mem3.sz
8a360 4d 61 73 74 65 72 20 2b 3d 20 73 69 7a 65 3b 0a  Master += size;.
8a370 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c        memsys3Unl
8a380 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72  ink(mem3.iMaster
8a390 29 3b 0a 20 20 20 20 20 20 78 20 3d 20 6d 65 6d  );.      x = mem
8a3a0 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61  3.aPool[mem3.iMa
8a3b0 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ster-1].u.hdr.si
8a3c0 7a 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20  ze4x & 2;.      
8a3d0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e  mem3.aPool[mem3.
8a3e0 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72  iMaster-1].u.hdr
8a3f0 2e 73 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73  .size4x = mem3.s
8a400 7a 4d 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20  zMaster*4 | x;. 
8a410 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b       mem3.aPool[
8a420 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d  mem3.iMaster+mem
8a430 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e  3.szMaster-1].u.
8a440 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d  hdr.prevSize = m
8a450 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20  em3.szMaster;.  
8a460 20 20 7d 0a 20 20 20 20 78 20 3d 20 6d 65 6d 33    }.    x = mem3
8a470 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73  .aPool[mem3.iMas
8a480 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  ter-1].u.hdr.siz
8a490 65 34 78 20 26 20 32 3b 0a 20 20 20 20 77 68 69  e4x & 2;.    whi
8a4a0 6c 65 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  le( (mem3.aPool[
8a4b0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d  mem3.iMaster+mem
8a4c0 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e  3.szMaster-1].u.
8a4d0 68 64 72 2e 73 69 7a 65 34 78 26 31 29 3d 3d 30  hdr.size4x&1)==0
8a4e0 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73   ){.      memsys
8a4f0 33 55 6e 6c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61  3Unlink(mem3.iMa
8a500 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74  ster+mem3.szMast
8a510 65 72 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e  er);.      mem3.
8a520 73 7a 4d 61 73 74 65 72 20 2b 3d 20 6d 65 6d 33  szMaster += mem3
8a530 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73  .aPool[mem3.iMas
8a540 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  ter+mem3.szMaste
8a550 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  r-1].u.hdr.size4
8a560 78 2f 34 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e  x/4;.      mem3.
8a570 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74  aPool[mem3.iMast
8a580 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  er-1].u.hdr.size
8a590 34 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74  4x = mem3.szMast
8a5a0 65 72 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 20  er*4 | x;.      
8a5b0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e  mem3.aPool[mem3.
8a5c0 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d  iMaster+mem3.szM
8a5d0 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70  aster-1].u.hdr.p
8a5e0 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73  revSize = mem3.s
8a5f0 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20  zMaster;.    }. 
8a600 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75   }.}../*.** Retu
8a610 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61  rn the size of a
8a620 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c  n outstanding al
8a630 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 62 79 74  location, in byt
8a640 65 73 2e 20 20 54 68 65 0a 2a 2a 20 73 69 7a 65  es.  The.** size
8a650 20 72 65 74 75 72 6e 65 64 20 6f 6d 69 74 73 20   returned omits 
8a660 74 68 65 20 38 2d 62 79 74 65 20 68 65 61 64 65  the 8-byte heade
8a670 72 20 6f 76 65 72 68 65 61 64 2e 20 20 54 68 69  r overhead.  Thi
8a680 73 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20  s only.** works 
8a690 66 6f 72 20 63 68 75 6e 6b 73 20 74 68 61 74 20  for chunks that 
8a6a0 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 63 68  are currently ch
8a6b0 65 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74  ecked out..*/.st
8a6c0 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33  atic int memsys3
8a6d0 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20  Size(void *p){. 
8a6e0 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 70 42 6c 6f   Mem3Block *pBlo
8a6f0 63 6b 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29  ck;.  if( p==0 )
8a700 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 42 6c   return 0;.  pBl
8a710 6f 63 6b 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b  ock = (Mem3Block
8a720 2a 29 70 3b 0a 20 20 61 73 73 65 72 74 28 20 28  *)p;.  assert( (
8a730 70 42 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e 68 64 72  pBlock[-1].u.hdr
8a740 2e 73 69 7a 65 34 78 26 31 29 21 3d 30 20 29 3b  .size4x&1)!=0 );
8a750 0a 20 20 72 65 74 75 72 6e 20 28 70 42 6c 6f 63  .  return (pBloc
8a760 6b 5b 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  k[-1].u.hdr.size
8a770 34 78 26 7e 33 29 2a 32 20 2d 20 34 3b 0a 7d 0a  4x&~3)*2 - 4;.}.
8a780 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20  ./*.** Round up 
8a790 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74  a request size t
8a7a0 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64  o the next valid
8a7b0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65   allocation size
8a7c0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
8a7d0 6d 65 6d 73 79 73 33 52 6f 75 6e 64 75 70 28 69  memsys3Roundup(i
8a7e0 6e 74 20 6e 29 7b 0a 20 20 69 66 28 20 6e 3c 3d  nt n){.  if( n<=
8a7f0 31 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  12 ){.    return
8a800 20 31 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20   12;.  }else{.  
8a810 20 20 72 65 74 75 72 6e 20 28 28 6e 2b 31 31 29    return ((n+11)
8a820 26 7e 37 29 20 2d 20 34 3b 0a 20 20 7d 0a 7d 0a  &~7) - 4;.  }.}.
8a830 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  ./*.** Allocate 
8a840 6e 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79  nBytes of memory
8a850 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
8a860 20 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 28   *memsys3Malloc(
8a870 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 73  int nBytes){.  s
8a880 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b  qlite3_int64 *p;
8a890 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65  .  assert( nByte
8a8a0 73 3e 30 20 29 3b 20 20 20 20 20 20 20 20 20 20  s>0 );          
8a8b0 2f 2a 20 6d 61 6c 6c 6f 63 2e 63 20 66 69 6c 74  /* malloc.c filt
8a8c0 65 72 73 20 6f 75 74 20 30 20 62 79 74 65 20 72  ers out 0 byte r
8a8d0 65 71 75 65 73 74 73 20 2a 2f 0a 20 20 6d 65 6d  equests */.  mem
8a8e0 73 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 70  sys3Enter();.  p
8a8f0 20 3d 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63   = memsys3Malloc
8a900 55 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a  Unsafe(nBytes);.
8a910 20 20 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29    memsys3Leave()
8a920 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64  ;.  return (void
8a930 2a 29 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  *)p; .}../*.** F
8a940 72 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 76  ree memory..*/.v
8a950 6f 69 64 20 6d 65 6d 73 79 73 33 46 72 65 65 28  oid memsys3Free(
8a960 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20  void *pPrior){. 
8a970 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 20   assert( pPrior 
8a980 29 3b 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65  );.  memsys3Ente
8a990 72 28 29 3b 0a 20 20 6d 65 6d 73 79 73 33 46 72  r();.  memsys3Fr
8a9a0 65 65 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29  eeUnsafe(pPrior)
8a9b0 3b 0a 20 20 6d 65 6d 73 79 73 33 4c 65 61 76 65  ;.  memsys3Leave
8a9c0 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61  ();.}../*.** Cha
8a9d0 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20  nge the size of 
8a9e0 61 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f  an existing memo
8a9f0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f  ry allocation.*/
8aa00 0a 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 52 65  .void *memsys3Re
8aa10 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69  alloc(void *pPri
8aa20 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 7b  or, int nBytes){
8aa30 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76  .  int nOld;.  v
8aa40 6f 69 64 20 2a 70 3b 0a 20 20 69 66 28 20 70 50  oid *p;.  if( pP
8aa50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72  rior==0 ){.    r
8aa60 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d 61  eturn sqlite3_ma
8aa70 6c 6c 6f 63 28 6e 42 79 74 65 73 29 3b 0a 20 20  lloc(nBytes);.  
8aa80 7d 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d  }.  if( nBytes<=
8aa90 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
8aaa0 5f 66 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20  _free(pPrior);. 
8aab0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
8aac0 0a 20 20 6e 4f 6c 64 20 3d 20 6d 65 6d 73 79 73  .  nOld = memsys
8aad0 33 53 69 7a 65 28 70 50 72 69 6f 72 29 3b 0a 20  3Size(pPrior);. 
8aae0 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c   if( nBytes<=nOl
8aaf0 64 20 26 26 20 6e 42 79 74 65 73 3e 3d 6e 4f 6c  d && nBytes>=nOl
8ab00 64 2d 31 32 38 20 29 7b 0a 20 20 20 20 72 65 74  d-128 ){.    ret
8ab10 75 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d 0a  urn pPrior;.  }.
8ab20 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29    memsys3Enter()
8ab30 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 33 4d  ;.  p = memsys3M
8ab40 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74  allocUnsafe(nByt
8ab50 65 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a  es);.  if( p ){.
8ab60 20 20 20 20 69 66 28 20 6e 4f 6c 64 3c 6e 42 79      if( nOld<nBy
8ab70 74 65 73 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  tes ){.      mem
8ab80 63 70 79 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e  cpy(p, pPrior, n
8ab90 4f 6c 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  Old);.    }else{
8aba0 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c  .      memcpy(p,
8abb0 20 70 50 72 69 6f 72 2c 20 6e 42 79 74 65 73 29   pPrior, nBytes)
8abc0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73  ;.    }.    mems
8abd0 79 73 33 46 72 65 65 55 6e 73 61 66 65 28 70 50  ys3FreeUnsafe(pP
8abe0 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d  rior);.  }.  mem
8abf0 73 79 73 33 4c 65 61 76 65 28 29 3b 0a 20 20 72  sys3Leave();.  r
8ac00 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
8ac10 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69  * Initialize thi
8ac20 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61  s module..*/.sta
8ac30 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 49  tic int memsys3I
8ac40 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65  nit(void *NotUse
8ac50 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  d){.  UNUSED_PAR
8ac60 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
8ac70 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 47  .  if( !sqlite3G
8ac80 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61  lobalConfig.pHea
8ac90 70 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  p ){.    return 
8aca0 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
8acb0 7d 0a 0a 20 20 2f 2a 20 53 74 6f 72 65 20 61 20  }..  /* Store a 
8acc0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d  pointer to the m
8acd0 65 6d 6f 72 79 20 62 6c 6f 63 6b 20 69 6e 20 67  emory block in g
8ace0 6c 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65 20  lobal structure 
8acf0 6d 65 6d 33 2e 20 2a 2f 0a 20 20 61 73 73 65 72  mem3. */.  asser
8ad00 74 28 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c  t( sizeof(Mem3Bl
8ad10 6f 63 6b 29 3d 3d 38 20 29 3b 0a 20 20 6d 65 6d  ock)==8 );.  mem
8ad20 33 2e 61 50 6f 6f 6c 20 3d 20 28 4d 65 6d 33 42  3.aPool = (Mem3B
8ad30 6c 6f 63 6b 20 2a 29 73 71 6c 69 74 65 33 47 6c  lock *)sqlite3Gl
8ad40 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70  obalConfig.pHeap
8ad50 3b 0a 20 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 20 3d  ;.  mem3.nPool =
8ad60 20 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43   (sqlite3GlobalC
8ad70 6f 6e 66 69 67 2e 6e 48 65 61 70 20 2f 20 73 69  onfig.nHeap / si
8ad80 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 29  zeof(Mem3Block))
8ad90 20 2d 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74   - 2;..  /* Init
8ada0 69 61 6c 69 7a 65 20 74 68 65 20 6d 61 73 74 65  ialize the maste
8adb0 72 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 6d 65  r block. */.  me
8adc0 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20 6d 65  m3.szMaster = me
8add0 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33  m3.nPool;.  mem3
8ade0 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33  .mnMaster = mem3
8adf0 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 6d 65 6d  .szMaster;.  mem
8ae00 33 2e 69 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20  3.iMaster = 1;. 
8ae10 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 30 5d 2e 75   mem3.aPool[0].u
8ae20 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 28 6d  .hdr.size4x = (m
8ae30 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3c 3c 32 29  em3.szMaster<<2)
8ae40 20 2b 20 32 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f   + 2;.  mem3.aPo
8ae50 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75  ol[mem3.nPool].u
8ae60 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20  .hdr.prevSize = 
8ae70 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65  mem3.nPool;.  me
8ae80 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50  m3.aPool[mem3.nP
8ae90 6f 6f 6c 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  ool].u.hdr.size4
8aea0 78 20 3d 20 31 3b 0a 0a 20 20 72 65 74 75 72 6e  x = 1;..  return
8aeb0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
8aec0 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a  *.** Deinitializ
8aed0 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a  e this module..*
8aee0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65  /.static void me
8aef0 6d 73 79 73 33 53 68 75 74 64 6f 77 6e 28 76 6f  msys3Shutdown(vo
8af00 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20  id *NotUsed){.  
8af10 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
8af20 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74  (NotUsed);.  ret
8af30 75 72 6e 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20  urn;.}..../*.** 
8af40 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e  Open the file in
8af50 64 69 63 61 74 65 64 20 61 6e 64 20 77 72 69 74  dicated and writ
8af60 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75  e a log of all u
8af70 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a  nfreed memory .*
8af80 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e  * allocations in
8af90 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a  to that log..*/.
8afa0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
8afb0 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 73 79  oid sqlite3Memsy
8afc0 73 33 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61  s3Dump(const cha
8afd0 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 23  r *zFilename){.#
8afe0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
8aff0 55 47 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b 0a  UG.  FILE *out;.
8b000 20 20 75 33 32 20 69 2c 20 6a 3b 0a 20 20 75 33    u32 i, j;.  u3
8b010 32 20 73 69 7a 65 3b 0a 20 20 69 66 28 20 7a 46  2 size;.  if( zF
8b020 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46  ilename==0 || zF
8b030 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b  ilename[0]==0 ){
8b040 0a 20 20 20 20 6f 75 74 20 3d 20 73 74 64 6f 75  .    out = stdou
8b050 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  t;.  }else{.    
8b060 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c  out = fopen(zFil
8b070 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 20  ename, "w");.   
8b080 20 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20   if( out==0 ){. 
8b090 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64       fprintf(std
8b0a0 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20  err, "** Unable 
8b0b0 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79  to output memory
8b0c0 20 64 65 62 75 67 20 6f 75 74 70 75 74 20 6c 6f   debug output lo
8b0d0 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20  g: %s **\n",.   
8b0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8b0f0 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20     zFilename);. 
8b100 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
8b110 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 33   }.  }.  memsys3
8b120 45 6e 74 65 72 28 29 3b 0a 20 20 66 70 72 69 6e  Enter();.  fprin
8b130 74 66 28 6f 75 74 2c 20 22 43 48 55 4e 4b 53 3a  tf(out, "CHUNKS:
8b140 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d 31 3b  \n");.  for(i=1;
8b150 20 69 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 20   i<=mem3.nPool; 
8b160 69 2b 3d 73 69 7a 65 2f 34 29 7b 0a 20 20 20 20  i+=size/4){.    
8b170 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f  size = mem3.aPoo
8b180 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  l[i-1].u.hdr.siz
8b190 65 34 78 3b 0a 20 20 20 20 69 66 28 20 73 69 7a  e4x;.    if( siz
8b1a0 65 2f 34 3c 3d 31 20 29 7b 0a 20 20 20 20 20 20  e/4<=1 ){.      
8b1b0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70  fprintf(out, "%p
8b1c0 20 73 69 7a 65 20 65 72 72 6f 72 5c 6e 22 2c 20   size error\n", 
8b1d0 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b  &mem3.aPool[i]);
8b1e0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30  .      assert( 0
8b1f0 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   );.      break;
8b200 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28  .    }.    if( (
8b210 73 69 7a 65 26 31 29 3d 3d 30 20 26 26 20 6d 65  size&1)==0 && me
8b220 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f  m3.aPool[i+size/
8b230 34 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53  4-1].u.hdr.prevS
8b240 69 7a 65 21 3d 73 69 7a 65 2f 34 20 29 7b 0a 20  ize!=size/4 ){. 
8b250 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74       fprintf(out
8b260 2c 20 22 25 70 20 74 61 69 6c 20 73 69 7a 65 20  , "%p tail size 
8b270 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 5c 6e  does not match\n
8b280 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  ", &mem3.aPool[i
8b290 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ]);.      assert
8b2a0 28 20 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65  ( 0 );.      bre
8b2b0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ak;.    }.    if
8b2c0 28 20 28 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  ( ((mem3.aPool[i
8b2d0 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68 64 72  +size/4-1].u.hdr
8b2e0 2e 73 69 7a 65 34 78 26 32 29 3e 3e 31 29 21 3d  .size4x&2)>>1)!=
8b2f0 28 73 69 7a 65 26 31 29 20 29 7b 0a 20 20 20 20  (size&1) ){.    
8b300 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
8b310 25 70 20 74 61 69 6c 20 63 68 65 63 6b 6f 75 74  %p tail checkout
8b320 20 62 69 74 20 69 73 20 69 6e 63 6f 72 72 65 63   bit is incorrec
8b330 74 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f  t\n", &mem3.aPoo
8b340 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73  l[i]);.      ass
8b350 65 72 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20  ert( 0 );.      
8b360 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
8b370 20 69 66 28 20 73 69 7a 65 26 31 20 29 7b 0a 20   if( size&1 ){. 
8b380 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74       fprintf(out
8b390 2c 20 22 25 70 20 25 36 64 20 62 79 74 65 73 20  , "%p %6d bytes 
8b3a0 63 68 65 63 6b 65 64 20 6f 75 74 5c 6e 22 2c 20  checked out\n", 
8b3b0 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20  &mem3.aPool[i], 
8b3c0 28 73 69 7a 65 2f 34 29 2a 38 2d 38 29 3b 0a 20  (size/4)*8-8);. 
8b3d0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
8b3e0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70  fprintf(out, "%p
8b3f0 20 25 36 64 20 62 79 74 65 73 20 66 72 65 65 25   %6d bytes free%
8b400 73 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f  s\n", &mem3.aPoo
8b410 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f 34 29 2a 38  l[i], (size/4)*8
8b420 2d 38 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  -8,.            
8b430 20 20 20 20 20 20 69 3d 3d 6d 65 6d 33 2e 69 4d        i==mem3.iM
8b440 61 73 74 65 72 20 3f 20 22 20 2a 2a 6d 61 73 74  aster ? " **mast
8b450 65 72 2a 2a 22 20 3a 20 22 22 29 3b 0a 20 20 20  er**" : "");.   
8b460 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30   }.  }.  for(i=0
8b470 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20  ; i<MX_SMALL-1; 
8b480 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d 65  i++){.    if( me
8b490 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3d 3d 30  m3.aiSmall[i]==0
8b4a0 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
8b4b0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 73   fprintf(out, "s
8b4c0 6d 61 6c 6c 28 25 32 64 29 3a 22 2c 20 69 29 3b  mall(%2d):", i);
8b4d0 0a 20 20 20 20 66 6f 72 28 6a 20 3d 20 6d 65 6d  .    for(j = mem
8b4e0 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3b 20 6a 3e  3.aiSmall[i]; j>
8b4f0 30 3b 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  0; j=mem3.aPool[
8b500 6a 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 7b  j].u.list.next){
8b510 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f  .      fprintf(o
8b520 75 74 2c 20 22 20 25 70 28 25 64 29 22 2c 20 26  ut, " %p(%d)", &
8b530 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a 20  mem3.aPool[j],. 
8b540 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6d 65               (me
8b550 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75 2e  m3.aPool[j-1].u.
8b560 68 64 72 2e 73 69 7a 65 34 78 2f 34 29 2a 38 2d  hdr.size4x/4)*8-
8b570 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 70  8);.    }.    fp
8b580 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29  rintf(out, "\n")
8b590 3b 20 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  ; .  }.  for(i=0
8b5a0 3b 20 69 3c 4e 5f 48 41 53 48 3b 20 69 2b 2b 29  ; i<N_HASH; i++)
8b5b0 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 61  {.    if( mem3.a
8b5c0 69 48 61 73 68 5b 69 5d 3d 3d 30 20 29 20 63 6f  iHash[i]==0 ) co
8b5d0 6e 74 69 6e 75 65 3b 0a 20 20 20 20 66 70 72 69  ntinue;.    fpri
8b5e0 6e 74 66 28 6f 75 74 2c 20 22 68 61 73 68 28 25  ntf(out, "hash(%
8b5f0 32 64 29 3a 22 2c 20 69 29 3b 0a 20 20 20 20 66  2d):", i);.    f
8b600 6f 72 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 48 61  or(j = mem3.aiHa
8b610 73 68 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65  sh[i]; j>0; j=me
8b620 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69  m3.aPool[j].u.li
8b630 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20 20  st.next){.      
8b640 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 25  fprintf(out, " %
8b650 70 28 25 64 29 22 2c 20 26 6d 65 6d 33 2e 61 50  p(%d)", &mem3.aP
8b660 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20 20 20  ool[j],.        
8b670 20 20 20 20 20 20 28 6d 65 6d 33 2e 61 50 6f 6f        (mem3.aPoo
8b680 6c 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  l[j-1].u.hdr.siz
8b690 65 34 78 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20  e4x/4)*8-8);.   
8b6a0 20 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f   }.    fprintf(o
8b6b0 75 74 2c 20 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a  ut, "\n"); .  }.
8b6c0 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
8b6d0 6d 61 73 74 65 72 3d 25 64 5c 6e 22 2c 20 6d 65  master=%d\n", me
8b6e0 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 66  m3.iMaster);.  f
8b6f0 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6e 6f 77  printf(out, "now
8b700 55 73 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33  Used=%d\n", mem3
8b710 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e  .nPool*8 - mem3.
8b720 73 7a 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20 66  szMaster*8);.  f
8b730 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 78 55  printf(out, "mxU
8b740 73 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e  sed=%d\n", mem3.
8b750 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e 6d  nPool*8 - mem3.m
8b760 6e 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20 73 71  nMaster*8);.  sq
8b770 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
8b780 65 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20  e(mem3.mutex);. 
8b790 20 69 66 28 20 6f 75 74 3d 3d 73 74 64 6f 75 74   if( out==stdout
8b7a0 20 29 7b 0a 20 20 20 20 66 66 6c 75 73 68 28 73   ){.    fflush(s
8b7b0 74 64 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b  tdout);.  }else{
8b7c0 0a 20 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 29  .    fclose(out)
8b7d0 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e  ;.  }.#else.  UN
8b7e0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 7a  USED_PARAMETER(z
8b7f0 46 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 69  Filename);.#endi
8b800 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  f.}../*.** This 
8b810 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f  routine is the o
8b820 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74  nly routine in t
8b830 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78  his file with ex
8b840 74 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61  ternal .** linka
8b850 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61  ge..**.** Popula
8b860 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c  te the low-level
8b870 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
8b880 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e  on function poin
8b890 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74  ters in.** sqlit
8b8a0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
8b8b0 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74   with pointers t
8b8c0 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69  o the routines i
8b8d0 6e 20 74 68 69 73 20 66 69 6c 65 2e 20 54 68 65  n this file. The
8b8e0 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 73 70  .** arguments sp
8b8f0 65 63 69 66 79 20 74 68 65 20 62 6c 6f 63 6b 20  ecify the block 
8b900 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20 6d 61 6e  of memory to man
8b910 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  age..**.** This 
8b920 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20  routine is only 
8b930 63 61 6c 6c 65 64 20 62 79 20 73 71 6c 69 74 65  called by sqlite
8b940 33 5f 63 6f 6e 66 69 67 28 29 2c 20 61 6e 64 20  3_config(), and 
8b950 74 68 65 72 65 66 6f 72 65 0a 2a 2a 20 69 73 20  therefore.** is 
8b960 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20  not required to 
8b970 62 65 20 74 68 72 65 61 64 73 61 66 65 20 28 69  be threadsafe (i
8b980 74 20 69 73 20 6e 6f 74 29 2e 0a 2a 2f 0a 53 51  t is not)..*/.SQ
8b990 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
8b9a0 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  st sqlite3_mem_m
8b9b0 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d  ethods *sqlite3M
8b9c0 65 6d 47 65 74 4d 65 6d 73 79 73 33 28 76 6f 69  emGetMemsys3(voi
8b9d0 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  d){.  static con
8b9e0 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  st sqlite3_mem_m
8b9f0 65 74 68 6f 64 73 20 6d 65 6d 70 6f 6f 6c 4d 65  ethods mempoolMe
8ba00 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 6d  thods = {.     m
8ba10 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 2c 0a 20 20  emsys3Malloc,.  
8ba20 20 20 20 6d 65 6d 73 79 73 33 46 72 65 65 2c 0a     memsys3Free,.
8ba30 20 20 20 20 20 6d 65 6d 73 79 73 33 52 65 61 6c       memsys3Real
8ba40 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73  loc,.     memsys
8ba50 33 53 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d 73  3Size,.     mems
8ba60 79 73 33 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20  ys3Roundup,.    
8ba70 20 6d 65 6d 73 79 73 33 49 6e 69 74 2c 0a 20 20   memsys3Init,.  
8ba80 20 20 20 6d 65 6d 73 79 73 33 53 68 75 74 64 6f     memsys3Shutdo
8ba90 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a  wn,.     0.  };.
8baa0 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 70 6f 6f    return &mempoo
8bab0 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e  lMethods;.}..#en
8bac0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e  dif /* SQLITE_EN
8bad0 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 2a 2f 0a  ABLE_MEMSYS3 */.
8bae0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
8baf0 20 45 6e 64 20 6f 66 20 6d 65 6d 33 2e 63 20 2a   End of mem3.c *
8bb00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8bb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8bb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
8bb30 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
8bb40 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 35   Begin file mem5
8bb50 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
8bb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8bb70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
8bb80 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4f 63 74 6f  ./*.** 2007 Octo
8bb90 62 65 72 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 14.**.** The
8bba0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
8bbb0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
8bbc0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
8bbd0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
8bbe0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
8bbf0 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
8bc00 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
8bc10 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
8bc20 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
8bc30 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
8bc40 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
8bc50 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
8bc60 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
8bc70 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
8bc80 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
8bc90 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
8bca0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
8bcb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8bcc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8bcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8bce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8bcf0 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
8bd00 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  le contains the 
8bd10 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  C functions that
8bd20 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 6d 65 6d   implement a mem
8bd30 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f  ory.** allocatio
8bd40 6e 20 73 75 62 73 79 73 74 65 6d 20 66 6f 72 20  n subsystem for 
8bd50 75 73 65 20 62 79 20 53 51 4c 69 74 65 2e 20 0a  use by SQLite. .
8bd60 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69  **.** This versi
8bd70 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79  on of the memory
8bd80 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73   allocation subs
8bd90 79 73 74 65 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a  ystem omits all.
8bda0 2a 2a 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63  ** use of malloc
8bdb0 28 29 2e 20 54 68 65 20 61 70 70 6c 69 63 61 74  (). The applicat
8bdc0 69 6f 6e 20 67 69 76 65 73 20 53 51 4c 69 74 65  ion gives SQLite
8bdd0 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f   a block of memo
8bde0 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61 6c  ry.** before cal
8bdf0 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e 69  ling sqlite3_ini
8be00 74 69 61 6c 69 7a 65 28 29 20 66 72 6f 6d 20 77  tialize() from w
8be10 68 69 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  hich allocations
8be20 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 61 6e 64  .** are made and
8be30 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65   returned by the
8be40 20 78 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 78   xMalloc() and x
8be50 52 65 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 69 6d  Realloc() .** im
8be60 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 20 4f  plementations. O
8be70 6e 63 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  nce sqlite3_init
8be80 69 61 6c 69 7a 65 28 29 20 68 61 73 20 62 65 65  ialize() has bee
8be90 6e 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74 68 65  n called,.** the
8bea0 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72   amount of memor
8beb0 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 53  y available to S
8bec0 51 4c 69 74 65 20 69 73 20 66 69 78 65 64 20 61  QLite is fixed a
8bed0 6e 64 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20  nd cannot.** be 
8bee0 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  changed..**.** T
8bef0 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  his version of t
8bf00 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
8bf10 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 69  tion subsystem i
8bf20 73 20 69 6e 63 6c 75 64 65 64 0a 2a 2a 20 69 6e  s included.** in
8bf30 20 74 68 65 20 62 75 69 6c 64 20 6f 6e 6c 79 20   the build only 
8bf40 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  if SQLITE_ENABLE
8bf50 5f 4d 45 4d 53 59 53 35 20 69 73 20 64 65 66 69  _MEMSYS5 is defi
8bf60 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ned..**.** This 
8bf70 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
8bf80 20 75 73 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77   uses the follow
8bf90 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 3a 0a 2a  ing algorithm:.*
8bfa0 2a 0a 2a 2a 20 20 20 31 2e 20 20 41 6c 6c 20 6d  *.**   1.  All m
8bfb0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
8bfc0 73 20 73 69 7a 65 73 20 61 72 65 20 72 6f 75 6e  s sizes are roun
8bfd0 64 65 64 20 75 70 20 74 6f 20 61 20 70 6f 77 65  ded up to a powe
8bfe0 72 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 20 20  r of 2..**.**   
8bff0 32 2e 20 20 49 66 20 74 77 6f 20 61 64 6a 61 63  2.  If two adjac
8c000 65 6e 74 20 66 72 65 65 20 62 6c 6f 63 6b 73 20  ent free blocks 
8c010 61 72 65 20 74 68 65 20 68 61 6c 76 65 73 20 6f  are the halves o
8c020 66 20 61 20 6c 61 72 67 65 72 20 62 6c 6f 63 6b  f a larger block
8c030 2c 0a 2a 2a 20 20 20 20 20 20 20 74 68 65 6e 20  ,.**       then 
8c040 74 68 65 20 74 77 6f 20 62 6c 6f 63 6b 73 20 61  the two blocks a
8c050 72 65 20 63 6f 61 6c 65 73 65 64 20 69 6e 74 6f  re coalesed into
8c060 20 74 68 65 20 73 69 6e 67 6c 65 20 6c 61 72 67   the single larg
8c070 65 72 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20  er block..**.** 
8c080 20 20 33 2e 20 20 4e 65 77 20 6d 65 6d 6f 72 79    3.  New memory
8c090 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 72   is allocated fr
8c0a0 6f 6d 20 74 68 65 20 66 69 72 73 74 20 61 76 61  om the first ava
8c0b0 69 6c 61 62 6c 65 20 66 72 65 65 20 62 6c 6f 63  ilable free bloc
8c0c0 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 61 6c  k..**.** This al
8c0d0 67 6f 72 69 74 68 6d 20 69 73 20 64 65 73 63 72  gorithm is descr
8c0e0 69 62 65 64 20 69 6e 3a 20 4a 2e 20 4d 2e 20 52  ibed in: J. M. R
8c0f0 6f 62 73 6f 6e 2e 20 22 42 6f 75 6e 64 73 20 66  obson. "Bounds f
8c100 6f 72 20 53 6f 6d 65 20 46 75 6e 63 74 69 6f 6e  or Some Function
8c110 73 0a 2a 2a 20 43 6f 6e 63 65 72 6e 69 6e 67 20  s.** Concerning 
8c120 44 79 6e 61 6d 69 63 20 53 74 6f 72 61 67 65 20  Dynamic Storage 
8c130 41 6c 6c 6f 63 61 74 69 6f 6e 22 2e 20 4a 6f 75  Allocation". Jou
8c140 72 6e 61 6c 20 6f 66 20 74 68 65 20 41 73 73 6f  rnal of the Asso
8c150 63 69 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 43  ciation for.** C
8c160 6f 6d 70 75 74 69 6e 67 20 4d 61 63 68 69 6e 65  omputing Machine
8c170 72 79 2c 20 56 6f 6c 75 6d 65 20 32 31 2c 20 4e  ry, Volume 21, N
8c180 75 6d 62 65 72 20 38 2c 20 4a 75 6c 79 20 31 39  umber 8, July 19
8c190 37 34 2c 20 70 61 67 65 73 20 34 39 31 2d 34 39  74, pages 491-49
8c1a0 39 2e 0a 2a 2a 20 0a 2a 2a 20 4c 65 74 20 6e 20  9..** .** Let n 
8c1b0 62 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  be the size of t
8c1c0 68 65 20 6c 61 72 67 65 73 74 20 61 6c 6c 6f 63  he largest alloc
8c1d0 61 74 69 6f 6e 20 64 69 76 69 64 65 64 20 62 79  ation divided by
8c1e0 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0a 2a 2a 20   the minimum.** 
8c1f0 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20  allocation size 
8c200 28 61 66 74 65 72 20 72 6f 75 6e 64 69 6e 67 20  (after rounding 
8c210 61 6c 6c 20 73 69 7a 65 73 20 75 70 20 74 6f 20  all sizes up to 
8c220 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 29 20 20  a power of 2.)  
8c230 4c 65 74 20 4d 0a 2a 2a 20 62 65 20 74 68 65 20  Let M.** be the 
8c240 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f  maximum amount o
8c250 66 20 6d 65 6d 6f 72 79 20 65 76 65 72 20 6f 75  f memory ever ou
8c260 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65  tstanding at one
8c270 20 74 69 6d 65 2e 20 20 4c 65 74 0a 2a 2a 20 4e   time.  Let.** N
8c280 20 62 65 20 74 68 65 20 74 6f 74 61 6c 20 61 6d   be the total am
8c290 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 61  ount of memory a
8c2a0 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c 6c  vailable for all
8c2b0 6f 63 61 74 69 6f 6e 2e 20 20 52 6f 62 73 6f 6e  ocation.  Robson
8c2c0 0a 2a 2a 20 70 72 6f 76 65 64 20 74 68 61 74 20  .** proved that 
8c2d0 74 68 69 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  this memory allo
8c2e0 63 61 74 6f 72 20 77 69 6c 6c 20 6e 65 76 65 72  cator will never
8c2f0 20 62 72 65 61 6b 64 6f 77 6e 20 64 75 65 20 74   breakdown due t
8c300 6f 20 0a 2a 2a 20 66 72 61 67 6d 65 6e 74 61 74  o .** fragmentat
8c310 69 6f 6e 20 61 73 20 6c 6f 6e 67 20 61 73 20 74  ion as long as t
8c320 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e  he following con
8c330 73 74 72 61 69 6e 74 20 68 6f 6c 64 73 3a 0a 2a  straint holds:.*
8c340 2a 0a 2a 2a 20 20 20 20 20 20 4e 20 3e 3d 20 20  *.**      N >=  
8c350 4d 2a 28 31 20 2b 20 6c 6f 67 32 28 6e 29 2f 32  M*(1 + log2(n)/2
8c360 29 20 2d 20 6e 20 2b 20 31 0a 2a 2a 0a 2a 2a 20  ) - n + 1.**.** 
8c370 54 68 65 20 73 71 6c 69 74 65 33 5f 73 74 61 74  The sqlite3_stat
8c380 75 73 28 29 20 6c 6f 67 69 63 20 74 72 61 63 6b  us() logic track
8c390 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61  s the maximum va
8c3a0 6c 75 65 73 20 6f 66 20 6e 20 61 6e 64 20 4d 20  lues of n and M 
8c3b0 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6e 20 61 70  so.** that an ap
8c3c0 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e 2c 20 61  plication can, a
8c3d0 74 20 61 6e 79 20 74 69 6d 65 2c 20 76 65 72 69  t any time, veri
8c3e0 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69  fy this constrai
8c3f0 6e 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  nt..*/../*.** Th
8c400 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68  is version of th
8c410 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
8c420 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20  or is used only 
8c430 77 68 65 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f  when .** SQLITE_
8c440 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 69  ENABLE_MEMSYS5 i
8c450 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69  s defined..*/.#i
8c460 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
8c470 4c 45 5f 4d 45 4d 53 59 53 35 0a 0a 2f 2a 0a 2a  LE_MEMSYS5../*.*
8c480 2a 20 41 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f  * A minimum allo
8c490 63 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e 73  cation is an ins
8c4a0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
8c4b0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
8c4c0 2e 0a 2a 2a 20 4c 61 72 67 65 72 20 61 6c 6c 6f  ..** Larger allo
8c4d0 63 61 74 69 6f 6e 73 20 61 72 65 20 61 6e 20 61  cations are an a
8c4e0 72 72 61 79 20 6f 66 20 74 68 65 73 65 20 73 74  rray of these st
8c4f0 72 75 63 74 75 72 65 73 20 77 68 65 72 65 20 74  ructures where t
8c500 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68  he.** size of th
8c510 65 20 61 72 72 61 79 20 69 73 20 61 20 70 6f 77  e array is a pow
8c520 65 72 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 54  er of 2..**.** T
8c530 68 65 20 73 69 7a 65 20 6f 66 20 74 68 69 73 20  he size of this 
8c540 6f 62 6a 65 63 74 20 6d 75 73 74 20 62 65 20 61  object must be a
8c550 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20 20   power of two.  
8c560 54 68 61 74 20 66 61 63 74 20 69 73 0a 2a 2a 20  That fact is.** 
8c570 76 65 72 69 66 69 65 64 20 69 6e 20 6d 65 6d 73  verified in mems
8c580 79 73 35 49 6e 69 74 28 29 2e 0a 2a 2f 0a 74 79  ys5Init()..*/.ty
8c590 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d  pedef struct Mem
8c5a0 35 4c 69 6e 6b 20 4d 65 6d 35 4c 69 6e 6b 3b 0a  5Link Mem5Link;.
8c5b0 73 74 72 75 63 74 20 4d 65 6d 35 4c 69 6e 6b 20  struct Mem5Link 
8c5c0 7b 0a 20 20 69 6e 74 20 6e 65 78 74 3b 20 20 20  {.  int next;   
8c5d0 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
8c5e0 6e 65 78 74 20 66 72 65 65 20 63 68 75 6e 6b 20  next free chunk 
8c5f0 2a 2f 0a 20 20 69 6e 74 20 70 72 65 76 3b 20 20  */.  int prev;  
8c600 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
8c610 20 70 72 65 76 69 6f 75 73 20 66 72 65 65 20 63   previous free c
8c620 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  hunk */.};../*.*
8c630 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f  * Maximum size o
8c640 66 20 61 6e 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  f any allocation
8c650 20 69 73 20 28 28 31 3c 3c 4c 4f 47 4d 41 58 29   is ((1<<LOGMAX)
8c660 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 29 2e 20 53  *mem5.szAtom). S
8c670 69 6e 63 65 0a 2a 2a 20 6d 65 6d 35 2e 73 7a 41  ince.** mem5.szA
8c680 74 6f 6d 20 69 73 20 61 6c 77 61 79 73 20 61 74  tom is always at
8c690 20 6c 65 61 73 74 20 38 20 61 6e 64 20 33 32 2d   least 8 and 32-
8c6a0 62 69 74 20 69 6e 74 65 67 65 72 73 20 61 72 65  bit integers are
8c6b0 20 75 73 65 64 2c 0a 2a 2a 20 69 74 20 69 73 20   used,.** it is 
8c6c0 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 70 6f 73  not actually pos
8c6d0 73 69 62 6c 65 20 74 6f 20 72 65 61 63 68 20 74  sible to reach t
8c6e0 68 69 73 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 64  his limit..*/.#d
8c6f0 65 66 69 6e 65 20 4c 4f 47 4d 41 58 20 33 30 0a  efine LOGMAX 30.
8c700 0a 2f 2a 0a 2a 2a 20 4d 61 73 6b 73 20 75 73 65  ./*.** Masks use
8c710 64 20 66 6f 72 20 6d 65 6d 35 2e 61 43 74 72 6c  d for mem5.aCtrl
8c720 5b 5d 20 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2f 0a  [] elements..*/.
8c730 23 64 65 66 69 6e 65 20 43 54 52 4c 5f 4c 4f 47  #define CTRL_LOG
8c740 53 49 5a 45 20 20 30 78 31 66 20 20 20 20 2f 2a  SIZE  0x1f    /*
8c750 20 4c 6f 67 32 20 53 69 7a 65 20 6f 66 20 74 68   Log2 Size of th
8c760 69 73 20 62 6c 6f 63 6b 20 2a 2f 0a 23 64 65 66  is block */.#def
8c770 69 6e 65 20 43 54 52 4c 5f 46 52 45 45 20 20 20  ine CTRL_FREE   
8c780 20 20 30 78 32 30 20 20 20 20 2f 2a 20 54 72 75    0x20    /* Tru
8c790 65 20 69 66 20 6e 6f 74 20 63 68 65 63 6b 65 64  e if not checked
8c7a0 20 6f 75 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41   out */../*.** A
8c7b0 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 63  ll of the static
8c7c0 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20   variables used 
8c7d0 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61  by this module a
8c7e0 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20  re collected.** 
8c7f0 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74  into a single st
8c800 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d  ructure named "m
8c810 65 6d 35 22 2e 20 20 54 68 69 73 20 69 73 20 74  em5".  This is t
8c820 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74  o keep the.** st
8c830 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f  atic variables o
8c840 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20  rganized and to 
8c850 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65  reduce namespace
8c860 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68   pollution.** wh
8c870 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69  en this module i
8c880 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20  s combined with 
8c890 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61  other in the ama
8c8a0 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74  lgamation..*/.st
8c8b0 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20  atic SQLITE_WSD 
8c8c0 73 74 72 75 63 74 20 4d 65 6d 35 47 6c 6f 62 61  struct Mem5Globa
8c8d0 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65  l {.  /*.  ** Me
8c8e0 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66  mory available f
8c8f0 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 20 20  or allocation.  
8c900 2a 2f 0a 20 20 69 6e 74 20 73 7a 41 74 6f 6d 3b  */.  int szAtom;
8c910 20 20 20 20 20 20 2f 2a 20 53 6d 61 6c 6c 65 73        /* Smalles
8c920 74 20 70 6f 73 73 69 62 6c 65 20 61 6c 6c 6f 63  t possible alloc
8c930 61 74 69 6f 6e 20 69 6e 20 62 79 74 65 73 20 2a  ation in bytes *
8c940 2f 0a 20 20 69 6e 74 20 6e 42 6c 6f 63 6b 3b 20  /.  int nBlock; 
8c950 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
8c960 66 20 73 7a 41 74 6f 6d 20 73 69 7a 65 64 20 62  f szAtom sized b
8c970 6c 6f 63 6b 73 20 69 6e 20 7a 50 6f 6f 6c 20 2a  locks in zPool *
8c980 2f 0a 20 20 75 38 20 2a 7a 50 6f 6f 6c 3b 20 20  /.  u8 *zPool;  
8c990 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 61       /* Memory a
8c9a0 76 61 69 6c 61 62 6c 65 20 74 6f 20 62 65 20 61  vailable to be a
8c9b0 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 0a 20  llocated */.  . 
8c9c0 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74   /*.  ** Mutex t
8c9d0 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73  o control access
8c9e0 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   to the memory a
8c9f0 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
8ca00 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  tem..  */.  sqli
8ca10 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
8ca20 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 65 72  ;..  /*.  ** Per
8ca30 66 6f 72 6d 61 6e 63 65 20 73 74 61 74 69 73 74  formance statist
8ca40 69 63 73 0a 20 20 2a 2f 0a 20 20 75 36 34 20 6e  ics.  */.  u64 n
8ca50 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 2f  Alloc;         /
8ca60 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  * Total number o
8ca70 66 20 63 61 6c 6c 73 20 74 6f 20 6d 61 6c 6c 6f  f calls to mallo
8ca80 63 20 2a 2f 0a 20 20 75 36 34 20 74 6f 74 61 6c  c */.  u64 total
8ca90 41 6c 6c 6f 63 3b 20 20 20 20 20 2f 2a 20 54 6f  Alloc;     /* To
8caa0 74 61 6c 20 6f 66 20 61 6c 6c 20 6d 61 6c 6c 6f  tal of all mallo
8cab0 63 20 63 61 6c 6c 73 20 2d 20 69 6e 63 6c 75 64  c calls - includ
8cac0 65 73 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67  es internal frag
8cad0 20 2a 2f 0a 20 20 75 36 34 20 74 6f 74 61 6c 45   */.  u64 totalE
8cae0 78 63 65 73 73 3b 20 20 20 20 2f 2a 20 54 6f 74  xcess;    /* Tot
8caf0 61 6c 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67  al internal frag
8cb00 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 75  mentation */.  u
8cb10 33 32 20 63 75 72 72 65 6e 74 4f 75 74 3b 20 20  32 currentOut;  
8cb20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 63 68     /* Current ch
8cb30 65 63 6b 6f 75 74 2c 20 69 6e 63 6c 75 64 69 6e  eckout, includin
8cb40 67 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67 6d  g internal fragm
8cb50 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 75 33  entation */.  u3
8cb60 32 20 63 75 72 72 65 6e 74 43 6f 75 6e 74 3b 20  2 currentCount; 
8cb70 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d    /* Current num
8cb80 62 65 72 20 6f 66 20 64 69 73 74 69 6e 63 74 20  ber of distinct 
8cb90 63 68 65 63 6b 6f 75 74 73 20 2a 2f 0a 20 20 75  checkouts */.  u
8cba0 33 32 20 6d 61 78 4f 75 74 3b 20 20 20 20 20 20  32 maxOut;      
8cbb0 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 69 6e     /* Maximum in
8cbc0 73 74 61 6e 74 61 6e 65 6f 75 73 20 63 75 72 72  stantaneous curr
8cbd0 65 6e 74 4f 75 74 20 2a 2f 0a 20 20 75 33 32 20  entOut */.  u32 
8cbe0 6d 61 78 43 6f 75 6e 74 3b 20 20 20 20 20 20 20  maxCount;       
8cbf0 2f 2a 20 4d 61 78 69 6d 75 6d 20 69 6e 73 74 61  /* Maximum insta
8cc00 6e 74 61 6e 65 6f 75 73 20 63 75 72 72 65 6e 74  ntaneous current
8cc10 43 6f 75 6e 74 20 2a 2f 0a 20 20 75 33 32 20 6d  Count */.  u32 m
8cc20 61 78 52 65 71 75 65 73 74 3b 20 20 20 20 20 2f  axRequest;     /
8cc30 2a 20 4c 61 72 67 65 73 74 20 61 6c 6c 6f 63 61  * Largest alloca
8cc40 74 69 6f 6e 20 28 65 78 63 6c 75 73 69 76 65 20  tion (exclusive 
8cc50 6f 66 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67  of internal frag
8cc60 29 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a  ) */.  .  /*.  *
8cc70 2a 20 4c 69 73 74 73 20 6f 66 20 66 72 65 65 20  * Lists of free 
8cc80 62 6c 6f 63 6b 73 2e 20 20 61 69 46 72 65 65 6c  blocks.  aiFreel
8cc90 69 73 74 5b 30 5d 20 69 73 20 61 20 6c 69 73 74  ist[0] is a list
8cca0 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 73 20   of free blocks 
8ccb0 6f 66 0a 20 20 2a 2a 20 73 69 7a 65 20 6d 65 6d  of.  ** size mem
8ccc0 35 2e 73 7a 41 74 6f 6d 2e 20 20 61 69 46 72 65  5.szAtom.  aiFre
8ccd0 65 6c 69 73 74 5b 31 5d 20 68 6f 6c 64 73 20 62  elist[1] holds b
8cce0 6c 6f 63 6b 73 20 6f 66 20 73 69 7a 65 20 73 7a  locks of size sz
8ccf0 41 74 6f 6d 2a 32 2e 0a 20 20 2a 2a 20 61 6e 64  Atom*2..  ** and
8cd00 20 73 6f 20 66 6f 72 74 68 2e 0a 20 20 2a 2f 0a   so forth..  */.
8cd10 20 20 69 6e 74 20 61 69 46 72 65 65 6c 69 73 74    int aiFreelist
8cd20 5b 4c 4f 47 4d 41 58 2b 31 5d 3b 0a 0a 20 20 2f  [LOGMAX+1];..  /
8cd30 2a 0a 20 20 2a 2a 20 53 70 61 63 65 20 66 6f 72  *.  ** Space for
8cd40 20 74 72 61 63 6b 69 6e 67 20 77 68 69 63 68 20   tracking which 
8cd50 62 6c 6f 63 6b 73 20 61 72 65 20 63 68 65 63 6b  blocks are check
8cd60 65 64 20 6f 75 74 20 61 6e 64 20 74 68 65 20 73  ed out and the s
8cd70 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 65 61 63 68  ize.  ** of each
8cd80 20 62 6c 6f 63 6b 2e 20 20 4f 6e 65 20 62 79 74   block.  One byt
8cd90 65 20 70 65 72 20 62 6c 6f 63 6b 2e 0a 20 20 2a  e per block..  *
8cda0 2f 0a 20 20 75 38 20 2a 61 43 74 72 6c 3b 0a 0a  /.  u8 *aCtrl;..
8cdb0 7d 20 6d 65 6d 35 20 3d 20 7b 20 30 20 7d 3b 0a  } mem5 = { 0 };.
8cdc0 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74 68  ./*.** Access th
8cdd0 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c  e static variabl
8cde0 65 20 74 68 72 6f 75 67 68 20 61 20 6d 61 63 72  e through a macr
8cdf0 6f 20 66 6f 72 20 53 51 4c 49 54 45 5f 4f 4d 49  o for SQLITE_OMI
8ce00 54 5f 57 53 44 0a 2a 2f 0a 23 64 65 66 69 6e 65  T_WSD.*/.#define
8ce10 20 6d 65 6d 35 20 47 4c 4f 42 41 4c 28 73 74 72   mem5 GLOBAL(str
8ce20 75 63 74 20 4d 65 6d 35 47 6c 6f 62 61 6c 2c 20  uct Mem5Global, 
8ce30 6d 65 6d 35 29 0a 0a 2f 2a 0a 2a 2a 20 41 73 73  mem5)../*.** Ass
8ce40 75 6d 69 6e 67 20 6d 65 6d 35 2e 7a 50 6f 6f 6c  uming mem5.zPool
8ce50 20 69 73 20 64 69 76 69 64 65 64 20 75 70 20 69   is divided up i
8ce60 6e 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20  nto an array of 
8ce70 4d 65 6d 35 4c 69 6e 6b 0a 2a 2a 20 73 74 72 75  Mem5Link.** stru
8ce80 63 74 75 72 65 73 2c 20 72 65 74 75 72 6e 20 61  ctures, return a
8ce90 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
8cea0 69 64 78 2d 74 68 20 73 75 63 68 20 6c 69 6b 2e  idx-th such lik.
8ceb0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 35  .*/.#define MEM5
8cec0 4c 49 4e 4b 28 69 64 78 29 20 28 28 4d 65 6d 35  LINK(idx) ((Mem5
8ced0 4c 69 6e 6b 20 2a 29 28 26 6d 65 6d 35 2e 7a 50  Link *)(&mem5.zP
8cee0 6f 6f 6c 5b 28 69 64 78 29 2a 6d 65 6d 35 2e 73  ool[(idx)*mem5.s
8cef0 7a 41 74 6f 6d 5d 29 29 0a 0a 2f 2a 0a 2a 2a 20  zAtom]))../*.** 
8cf00 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b  Unlink the chunk
8cf10 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 69   at mem5.aPool[i
8cf20 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69 74 20 69  ] from list it i
8cf30 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f  s currently.** o
8cf40 6e 2e 20 20 49 74 20 73 68 6f 75 6c 64 20 62 65  n.  It should be
8cf50 20 66 6f 75 6e 64 20 6f 6e 20 6d 65 6d 35 2e 61   found on mem5.a
8cf60 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69  iFreelist[iLogsi
8cf70 7a 65 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ze]..*/.static v
8cf80 6f 69 64 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e  oid memsys5Unlin
8cf90 6b 28 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c 6f  k(int i, int iLo
8cfa0 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 6e 65  gsize){.  int ne
8cfb0 78 74 2c 20 70 72 65 76 3b 0a 20 20 61 73 73 65  xt, prev;.  asse
8cfc0 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d 65  rt( i>=0 && i<me
8cfd0 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61  m5.nBlock );.  a
8cfe0 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65 3e  ssert( iLogsize>
8cff0 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c 3d  =0 && iLogsize<=
8d000 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61 73 73 65  LOGMAX );.  asse
8d010 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b  rt( (mem5.aCtrl[
8d020 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49 5a  i] & CTRL_LOGSIZ
8d030 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 29 3b 0a  E)==iLogsize );.
8d040 0a 20 20 6e 65 78 74 20 3d 20 4d 45 4d 35 4c 49  .  next = MEM5LI
8d050 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 70  NK(i)->next;.  p
8d060 72 65 76 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69  rev = MEM5LINK(i
8d070 29 2d 3e 70 72 65 76 3b 0a 20 20 69 66 28 20 70  )->prev;.  if( p
8d080 72 65 76 3c 30 20 29 7b 0a 20 20 20 20 6d 65 6d  rev<0 ){.    mem
8d090 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f  5.aiFreelist[iLo
8d0a0 67 73 69 7a 65 5d 20 3d 20 6e 65 78 74 3b 0a 20  gsize] = next;. 
8d0b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 45 4d 35   }else{.    MEM5
8d0c0 4c 49 4e 4b 28 70 72 65 76 29 2d 3e 6e 65 78 74  LINK(prev)->next
8d0d0 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69   = next;.  }.  i
8d0e0 66 28 20 6e 65 78 74 3e 3d 30 20 29 7b 0a 20 20  f( next>=0 ){.  
8d0f0 20 20 4d 45 4d 35 4c 49 4e 4b 28 6e 65 78 74 29    MEM5LINK(next)
8d100 2d 3e 70 72 65 76 20 3d 20 70 72 65 76 3b 0a 20  ->prev = prev;. 
8d110 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b   }.}../*.** Link
8d120 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 6d 65   the chunk at me
8d130 6d 35 2e 61 50 6f 6f 6c 5b 69 5d 20 73 6f 20 74  m5.aPool[i] so t
8d140 68 61 74 20 69 73 20 6f 6e 20 74 68 65 20 69 4c  hat is on the iL
8d150 6f 67 73 69 7a 65 0a 2a 2a 20 66 72 65 65 20 6c  ogsize.** free l
8d160 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ist..*/.static v
8d170 6f 69 64 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28  oid memsys5Link(
8d180 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c 6f 67 73  int i, int iLogs
8d190 69 7a 65 29 7b 0a 20 20 69 6e 74 20 78 3b 0a 20  ize){.  int x;. 
8d1a0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
8d1b0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 35  _mutex_held(mem5
8d1c0 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  .mutex) );.  ass
8d1d0 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d  ert( i>=0 && i<m
8d1e0 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20  em5.nBlock );.  
8d1f0 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65  assert( iLogsize
8d200 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c  >=0 && iLogsize<
8d210 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61 73 73  =LOGMAX );.  ass
8d220 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c  ert( (mem5.aCtrl
8d230 5b 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49  [i] & CTRL_LOGSI
8d240 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 29 3b  ZE)==iLogsize );
8d250 0a 0a 20 20 78 20 3d 20 4d 45 4d 35 4c 49 4e 4b  ..  x = MEM5LINK
8d260 28 69 29 2d 3e 6e 65 78 74 20 3d 20 6d 65 6d 35  (i)->next = mem5
8d270 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67  .aiFreelist[iLog
8d280 73 69 7a 65 5d 3b 0a 20 20 4d 45 4d 35 4c 49 4e  size];.  MEM5LIN
8d290 4b 28 69 29 2d 3e 70 72 65 76 20 3d 20 2d 31 3b  K(i)->prev = -1;
8d2a0 0a 20 20 69 66 28 20 78 3e 3d 30 20 29 7b 0a 20  .  if( x>=0 ){. 
8d2b0 20 20 20 61 73 73 65 72 74 28 20 78 3c 6d 65 6d     assert( x<mem
8d2c0 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 20 20  5.nBlock );.    
8d2d0 4d 45 4d 35 4c 49 4e 4b 28 78 29 2d 3e 70 72 65  MEM5LINK(x)->pre
8d2e0 76 20 3d 20 69 3b 0a 20 20 7d 0a 20 20 6d 65 6d  v = i;.  }.  mem
8d2f0 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f  5.aiFreelist[iLo
8d300 67 73 69 7a 65 5d 20 3d 20 69 3b 0a 7d 0a 0a 2f  gsize] = i;.}../
8d310 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41 54  *.** If the STAT
8d320 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 20  IC_MEM mutex is 
8d330 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64  not already held
8d340 2c 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e  , obtain it now.
8d350 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69   The mutex.** wi
8d360 6c 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 65  ll already be he
8d370 6c 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 20  ld (obtained by 
8d380 63 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63  code in malloc.c
8d390 29 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47  ) if.** sqlite3G
8d3a0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d  lobalConfig.bMem
8d3b0 53 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f  Stat is true..*/
8d3c0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
8d3d0 73 79 73 35 45 6e 74 65 72 28 76 6f 69 64 29 7b  sys5Enter(void){
8d3e0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
8d3f0 5f 65 6e 74 65 72 28 6d 65 6d 35 2e 6d 75 74 65  _enter(mem5.mute
8d400 78 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69  x);.}.static voi
8d410 64 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 76  d memsys5Leave(v
8d420 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  oid){.  sqlite3_
8d430 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 35  mutex_leave(mem5
8d440 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  .mutex);.}../*.*
8d450 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a  * Return the siz
8d460 65 20 6f 66 20 61 6e 20 6f 75 74 73 74 61 6e 64  e of an outstand
8d470 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20  ing allocation, 
8d480 69 6e 20 62 79 74 65 73 2e 20 20 54 68 65 0a 2a  in bytes.  The.*
8d490 2a 20 73 69 7a 65 20 72 65 74 75 72 6e 65 64 20  * size returned 
8d4a0 6f 6d 69 74 73 20 74 68 65 20 38 2d 62 79 74 65  omits the 8-byte
8d4b0 20 68 65 61 64 65 72 20 6f 76 65 72 68 65 61 64   header overhead
8d4c0 2e 20 20 54 68 69 73 20 6f 6e 6c 79 0a 2a 2a 20  .  This only.** 
8d4d0 77 6f 72 6b 73 20 66 6f 72 20 63 68 75 6e 6b 73  works for chunks
8d4e0 20 74 68 61 74 20 61 72 65 20 63 75 72 72 65 6e   that are curren
8d4f0 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 2e  tly checked out.
8d500 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
8d510 65 6d 73 79 73 35 53 69 7a 65 28 76 6f 69 64 20  emsys5Size(void 
8d520 2a 70 29 7b 0a 20 20 69 6e 74 20 69 53 69 7a 65  *p){.  int iSize
8d530 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 20 29 7b   = 0;.  if( p ){
8d540 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 28 28 75  .    int i = ((u
8d550 38 20 2a 29 70 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c  8 *)p-mem5.zPool
8d560 29 2f 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0a 20  )/mem5.szAtom;. 
8d570 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20     assert( i>=0 
8d580 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b  && i<mem5.nBlock
8d590 20 29 3b 0a 20 20 20 20 69 53 69 7a 65 20 3d 20   );.    iSize = 
8d5a0 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 2a 20 28 31  mem5.szAtom * (1
8d5b0 20 3c 3c 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b   << (mem5.aCtrl[
8d5c0 69 5d 26 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 29  i]&CTRL_LOGSIZE)
8d5d0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
8d5e0 69 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  iSize;.}../*.** 
8d5f0 46 69 6e 64 20 74 68 65 20 66 69 72 73 74 20 65  Find the first e
8d600 6e 74 72 79 20 6f 6e 20 74 68 65 20 66 72 65 65  ntry on the free
8d610 6c 69 73 74 20 69 4c 6f 67 73 69 7a 65 2e 20 20  list iLogsize.  
8d620 55 6e 6c 69 6e 6b 20 74 68 61 74 0a 2a 2a 20 65  Unlink that.** e
8d630 6e 74 72 79 20 61 6e 64 20 72 65 74 75 72 6e 20  ntry and return 
8d640 69 74 73 20 69 6e 64 65 78 2e 20 0a 2a 2f 0a 73  its index. .*/.s
8d650 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73  tatic int memsys
8d660 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28 69 6e 74  5UnlinkFirst(int
8d670 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e   iLogsize){.  in
8d680 74 20 69 3b 0a 20 20 69 6e 74 20 69 46 69 72 73  t i;.  int iFirs
8d690 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 4c  t;..  assert( iL
8d6a0 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f  ogsize>=0 && iLo
8d6b0 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b  gsize<=LOGMAX );
8d6c0 0a 20 20 69 20 3d 20 69 46 69 72 73 74 20 3d 20  .  i = iFirst = 
8d6d0 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b  mem5.aiFreelist[
8d6e0 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20 61 73 73  iLogsize];.  ass
8d6f0 65 72 74 28 20 69 46 69 72 73 74 3e 3d 30 20 29  ert( iFirst>=0 )
8d700 3b 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 20 29  ;.  while( i>0 )
8d710 7b 0a 20 20 20 20 69 66 28 20 69 3c 69 46 69 72  {.    if( i<iFir
8d720 73 74 20 29 20 69 46 69 72 73 74 20 3d 20 69 3b  st ) iFirst = i;
8d730 0a 20 20 20 20 69 20 3d 20 4d 45 4d 35 4c 49 4e  .    i = MEM5LIN
8d740 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a  K(i)->next;.  }.
8d750 20 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28    memsys5Unlink(
8d760 69 46 69 72 73 74 2c 20 69 4c 6f 67 73 69 7a 65  iFirst, iLogsize
8d770 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 46 69 72  );.  return iFir
8d780 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  st;.}../*.** Ret
8d790 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d  urn a block of m
8d7a0 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61 73  emory of at leas
8d7b0 74 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a 65  t nBytes in size
8d7c0 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c  ..** Return NULL
8d7d0 20 69 66 20 75 6e 61 62 6c 65 2e 20 20 52 65 74   if unable.  Ret
8d7e0 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 42 79 74  urn NULL if nByt
8d7f0 65 73 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  es==0..**.** The
8d800 20 63 61 6c 6c 65 72 20 67 75 61 72 61 6e 74 65   caller guarante
8d810 65 73 20 74 68 61 74 20 6e 42 79 74 65 20 70 6f  es that nByte po
8d820 73 69 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  sitive..**.** Th
8d830 65 20 63 61 6c 6c 65 72 20 68 61 73 20 6f 62 74  e caller has obt
8d840 61 69 6e 65 64 20 61 20 6d 75 74 65 78 20 70 72  ained a mutex pr
8d850 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20  ior to invoking 
8d860 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  this.** routine 
8d870 73 6f 20 74 68 65 72 65 20 69 73 20 6e 65 76 65  so there is neve
8d880 72 20 61 6e 79 20 63 68 61 6e 63 65 20 74 68 61  r any chance tha
8d890 74 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a 2a 2a  t two or more.**
8d8a0 20 74 68 72 65 61 64 73 20 63 61 6e 20 62 65 20   threads can be 
8d8b0 69 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  in this routine 
8d8c0 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  at the same time
8d8d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
8d8e0 20 2a 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 55   *memsys5MallocU
8d8f0 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 74 65 29  nsafe(int nByte)
8d900 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  {.  int i;      
8d910 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
8d920 20 61 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 5d 20   a mem5.aPool[] 
8d930 73 6c 6f 74 20 2a 2f 0a 20 20 69 6e 74 20 69 42  slot */.  int iB
8d940 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  in;        /* In
8d950 64 65 78 20 69 6e 74 6f 20 6d 65 6d 35 2e 61 69  dex into mem5.ai
8d960 46 72 65 65 6c 69 73 74 5b 5d 20 2a 2f 0a 20 20  Freelist[] */.  
8d970 69 6e 74 20 69 46 75 6c 6c 53 7a 3b 20 20 20 20  int iFullSz;    
8d980 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 6c 6c 6f   /* Size of allo
8d990 63 61 74 69 6f 6e 20 72 6f 75 6e 64 65 64 20 75  cation rounded u
8d9a0 70 20 74 6f 20 70 6f 77 65 72 20 6f 66 20 32 20  p to power of 2 
8d9b0 2a 2f 0a 20 20 69 6e 74 20 69 4c 6f 67 73 69 7a  */.  int iLogsiz
8d9c0 65 3b 20 20 20 20 2f 2a 20 4c 6f 67 32 20 6f 66  e;    /* Log2 of
8d9d0 20 69 46 75 6c 6c 53 7a 2f 50 4f 57 32 5f 4d 49   iFullSz/POW2_MI
8d9e0 4e 20 2a 2f 0a 0a 20 20 2f 2a 20 6e 42 79 74 65  N */..  /* nByte
8d9f0 20 6d 75 73 74 20 62 65 20 61 20 70 6f 73 69 74   must be a posit
8da00 69 76 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ive */.  assert(
8da10 20 6e 42 79 74 65 3e 30 20 29 3b 0a 0a 20 20 2f   nByte>0 );..  /
8da20 2a 20 4b 65 65 70 20 74 72 61 63 6b 20 6f 66 20  * Keep track of 
8da30 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f  the maximum allo
8da40 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 2e 20  cation request. 
8da50 20 45 76 65 6e 20 75 6e 66 75 6c 66 69 6c 6c 65   Even unfulfille
8da60 64 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 73 20  d.  ** requests 
8da70 61 72 65 20 63 6f 75 6e 74 65 64 20 2a 2f 0a 20  are counted */. 
8da80 20 69 66 28 20 28 75 33 32 29 6e 42 79 74 65 3e   if( (u32)nByte>
8da90 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 20  mem5.maxRequest 
8daa0 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 61 78 52  ){.    mem5.maxR
8dab0 65 71 75 65 73 74 20 3d 20 6e 42 79 74 65 3b 0a  equest = nByte;.
8dac0 20 20 7d 0a 0a 20 20 2f 2a 20 41 62 6f 72 74 20    }..  /* Abort 
8dad0 69 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64  if the requested
8dae0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65   allocation size
8daf0 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20   is larger than 
8db00 74 68 65 20 6c 61 72 67 65 73 74 0a 20 20 2a 2a  the largest.  **
8db10 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 74 68   power of two th
8db20 61 74 20 77 65 20 63 61 6e 20 72 65 70 72 65 73  at we can repres
8db30 65 6e 74 20 75 73 69 6e 67 20 33 32 2d 62 69 74  ent using 32-bit
8db40 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73   signed integers
8db50 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42 79  ..  */.  if( nBy
8db60 74 65 20 3e 20 30 78 34 30 30 30 30 30 30 30 20  te > 0x40000000 
8db70 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
8db80 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 6f 75 6e 64  .  }..  /* Round
8db90 20 6e 42 79 74 65 20 75 70 20 74 6f 20 74 68 65   nByte up to the
8dba0 20 6e 65 78 74 20 76 61 6c 69 64 20 70 6f 77 65   next valid powe
8dbb0 72 20 6f 66 20 74 77 6f 20 2a 2f 0a 20 20 66 6f  r of two */.  fo
8dbc0 72 28 69 46 75 6c 6c 53 7a 3d 6d 65 6d 35 2e 73  r(iFullSz=mem5.s
8dbd0 7a 41 74 6f 6d 2c 20 69 4c 6f 67 73 69 7a 65 3d  zAtom, iLogsize=
8dbe0 30 3b 20 69 46 75 6c 6c 53 7a 3c 6e 42 79 74 65  0; iFullSz<nByte
8dbf0 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d 20 32 2c 20  ; iFullSz *= 2, 
8dc00 69 4c 6f 67 73 69 7a 65 2b 2b 29 7b 7d 0a 0a 20  iLogsize++){}.. 
8dc10 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 6d 65   /* Make sure me
8dc20 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c  m5.aiFreelist[iL
8dc30 6f 67 73 69 7a 65 5d 20 63 6f 6e 74 61 69 6e 73  ogsize] contains
8dc40 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 66 72   at least one fr
8dc50 65 65 0a 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20 20  ee.  ** block.  
8dc60 49 66 20 6e 6f 74 2c 20 74 68 65 6e 20 73 70 6c  If not, then spl
8dc70 69 74 20 61 20 62 6c 6f 63 6b 20 6f 66 20 74 68  it a block of th
8dc80 65 20 6e 65 78 74 20 6c 61 72 67 65 72 20 70 6f  e next larger po
8dc90 77 65 72 20 6f 66 0a 20 20 2a 2a 20 74 77 6f 20  wer of.  ** two 
8dca0 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65 61  in order to crea
8dcb0 74 65 20 61 20 6e 65 77 20 66 72 65 65 20 62 6c  te a new free bl
8dcc0 6f 63 6b 20 6f 66 20 73 69 7a 65 20 69 4c 6f 67  ock of size iLog
8dcd0 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  size..  */.  for
8dce0 28 69 42 69 6e 3d 69 4c 6f 67 73 69 7a 65 3b 20  (iBin=iLogsize; 
8dcf0 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b  mem5.aiFreelist[
8dd00 69 42 69 6e 5d 3c 30 20 26 26 20 69 42 69 6e 3c  iBin]<0 && iBin<
8dd10 3d 4c 4f 47 4d 41 58 3b 20 69 42 69 6e 2b 2b 29  =LOGMAX; iBin++)
8dd20 7b 7d 0a 20 20 69 66 28 20 69 42 69 6e 3e 4c 4f  {}.  if( iBin>LO
8dd30 47 4d 41 58 20 29 20 72 65 74 75 72 6e 20 30 3b  GMAX ) return 0;
8dd40 0a 20 20 69 20 3d 20 6d 65 6d 73 79 73 35 55 6e  .  i = memsys5Un
8dd50 6c 69 6e 6b 46 69 72 73 74 28 69 42 69 6e 29 3b  linkFirst(iBin);
8dd60 0a 20 20 77 68 69 6c 65 28 20 69 42 69 6e 3e 69  .  while( iBin>i
8dd70 4c 6f 67 73 69 7a 65 20 29 7b 0a 20 20 20 20 69  Logsize ){.    i
8dd80 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 0a 20 20 20  nt newSize;..   
8dd90 20 69 42 69 6e 2d 2d 3b 0a 20 20 20 20 6e 65 77   iBin--;.    new
8dda0 53 69 7a 65 20 3d 20 31 20 3c 3c 20 69 42 69 6e  Size = 1 << iBin
8ddb0 3b 0a 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c  ;.    mem5.aCtrl
8ddc0 5b 69 2b 6e 65 77 53 69 7a 65 5d 20 3d 20 43 54  [i+newSize] = CT
8ddd0 52 4c 5f 46 52 45 45 20 7c 20 69 42 69 6e 3b 0a  RL_FREE | iBin;.
8dde0 20 20 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28      memsys5Link(
8ddf0 69 2b 6e 65 77 53 69 7a 65 2c 20 69 42 69 6e 29  i+newSize, iBin)
8de00 3b 0a 20 20 7d 0a 20 20 6d 65 6d 35 2e 61 43 74  ;.  }.  mem5.aCt
8de10 72 6c 5b 69 5d 20 3d 20 69 4c 6f 67 73 69 7a 65  rl[i] = iLogsize
8de20 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 61  ;..  /* Update a
8de30 6c 6c 6f 63 61 74 6f 72 20 70 65 72 66 6f 72 6d  llocator perform
8de40 61 6e 63 65 20 73 74 61 74 69 73 74 69 63 73 2e  ance statistics.
8de50 20 2a 2f 0a 20 20 6d 65 6d 35 2e 6e 41 6c 6c 6f   */.  mem5.nAllo
8de60 63 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 61  c++;.  mem5.tota
8de70 6c 41 6c 6c 6f 63 20 2b 3d 20 69 46 75 6c 6c 53  lAlloc += iFullS
8de80 7a 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 6c 45  z;.  mem5.totalE
8de90 78 63 65 73 73 20 2b 3d 20 69 46 75 6c 6c 53 7a  xcess += iFullSz
8dea0 20 2d 20 6e 42 79 74 65 3b 0a 20 20 6d 65 6d 35   - nByte;.  mem5
8deb0 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 2b 2b 3b  .currentCount++;
8dec0 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f  .  mem5.currentO
8ded0 75 74 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b 0a 20  ut += iFullSz;. 
8dee0 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78 43 6f 75   if( mem5.maxCou
8def0 6e 74 3c 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43  nt<mem5.currentC
8df00 6f 75 6e 74 20 29 20 6d 65 6d 35 2e 6d 61 78 43  ount ) mem5.maxC
8df10 6f 75 6e 74 20 3d 20 6d 65 6d 35 2e 63 75 72 72  ount = mem5.curr
8df20 65 6e 74 43 6f 75 6e 74 3b 0a 20 20 69 66 28 20  entCount;.  if( 
8df30 6d 65 6d 35 2e 6d 61 78 4f 75 74 3c 6d 65 6d 35  mem5.maxOut<mem5
8df40 2e 63 75 72 72 65 6e 74 4f 75 74 20 29 20 6d 65  .currentOut ) me
8df50 6d 35 2e 6d 61 78 4f 75 74 20 3d 20 6d 65 6d 35  m5.maxOut = mem5
8df60 2e 63 75 72 72 65 6e 74 4f 75 74 3b 0a 0a 20 20  .currentOut;..  
8df70 2f 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  /* Return a poin
8df80 74 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63  ter to the alloc
8df90 61 74 65 64 20 6d 65 6d 6f 72 79 2e 20 2a 2f 0a  ated memory. */.
8dfa0 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29    return (void*)
8dfb0 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 69 2a 6d 65  &mem5.zPool[i*me
8dfc0 6d 35 2e 73 7a 41 74 6f 6d 5d 3b 0a 7d 0a 0a 2f  m5.szAtom];.}../
8dfd0 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f 75 74  *.** Free an out
8dfe0 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20  standing memory 
8dff0 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 73  allocation..*/.s
8e000 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79  tatic void memsy
8e010 73 35 46 72 65 65 55 6e 73 61 66 65 28 76 6f 69  s5FreeUnsafe(voi
8e020 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 75 33 32 20  d *pOld){.  u32 
8e030 73 69 7a 65 2c 20 69 4c 6f 67 73 69 7a 65 3b 0a  size, iLogsize;.
8e040 20 20 69 6e 74 20 69 42 6c 6f 63 6b 3b 0a 0a 20    int iBlock;.. 
8e050 20 2f 2a 20 53 65 74 20 69 42 6c 6f 63 6b 20 74   /* Set iBlock t
8e060 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  o the index of t
8e070 68 65 20 62 6c 6f 63 6b 20 70 6f 69 6e 74 65 64  he block pointed
8e080 20 74 6f 20 62 79 20 70 4f 6c 64 20 69 6e 20 0a   to by pOld in .
8e090 20 20 2a 2a 20 74 68 65 20 61 72 72 61 79 20 6f    ** the array o
8e0a0 66 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 62 79  f mem5.szAtom by
8e0b0 74 65 20 62 6c 6f 63 6b 73 20 70 6f 69 6e 74 65  te blocks pointe
8e0c0 64 20 74 6f 20 62 79 20 6d 65 6d 35 2e 7a 50 6f  d to by mem5.zPo
8e0d0 6f 6c 2e 0a 20 20 2a 2f 0a 20 20 69 42 6c 6f 63  ol..  */.  iBloc
8e0e0 6b 20 3d 20 28 28 75 38 20 2a 29 70 4f 6c 64 2d  k = ((u8 *)pOld-
8e0f0 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d 35  mem5.zPool)/mem5
8e100 2e 73 7a 41 74 6f 6d 3b 0a 0a 20 20 2f 2a 20 43  .szAtom;..  /* C
8e110 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 6f  heck that the po
8e120 69 6e 74 65 72 20 70 4f 6c 64 20 70 6f 69 6e 74  inter pOld point
8e130 73 20 74 6f 20 61 20 76 61 6c 69 64 2c 20 6e 6f  s to a valid, no
8e140 6e 2d 66 72 65 65 20 62 6c 6f 63 6b 2e 20 2a 2f  n-free block. */
8e150 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f 63  .  assert( iBloc
8e160 6b 3e 3d 30 20 26 26 20 69 42 6c 6f 63 6b 3c 6d  k>=0 && iBlock<m
8e170 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20  em5.nBlock );.  
8e180 61 73 73 65 72 74 28 20 28 28 75 38 20 2a 29 70  assert( ((u8 *)p
8e190 4f 6c 64 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 25  Old-mem5.zPool)%
8e1a0 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3d 3d 30 20 29  mem5.szAtom==0 )
8e1b0 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d  ;.  assert( (mem
8e1c0 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20  5.aCtrl[iBlock] 
8e1d0 26 20 43 54 52 4c 5f 46 52 45 45 29 3d 3d 30 20  & CTRL_FREE)==0 
8e1e0 29 3b 0a 0a 20 20 69 4c 6f 67 73 69 7a 65 20 3d  );..  iLogsize =
8e1f0 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f   mem5.aCtrl[iBlo
8e200 63 6b 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49  ck] & CTRL_LOGSI
8e210 5a 45 3b 0a 20 20 73 69 7a 65 20 3d 20 31 3c 3c  ZE;.  size = 1<<
8e220 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 61 73 73 65  iLogsize;.  asse
8e230 72 74 28 20 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d  rt( iBlock+size-
8e240 31 3c 28 75 33 32 29 6d 65 6d 35 2e 6e 42 6c 6f  1<(u32)mem5.nBlo
8e250 63 6b 20 29 3b 0a 0a 20 20 6d 65 6d 35 2e 61 43  ck );..  mem5.aC
8e260 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 7c 3d 20 43  trl[iBlock] |= C
8e270 54 52 4c 5f 46 52 45 45 3b 0a 20 20 6d 65 6d 35  TRL_FREE;.  mem5
8e280 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 2b 73 69  .aCtrl[iBlock+si
8e290 7a 65 2d 31 5d 20 7c 3d 20 43 54 52 4c 5f 46 52  ze-1] |= CTRL_FR
8e2a0 45 45 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65  EE;.  assert( me
8e2b0 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3e  m5.currentCount>
8e2c0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d  0 );.  assert( m
8e2d0 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3e 3d  em5.currentOut>=
8e2e0 28 73 69 7a 65 2a 6d 65 6d 35 2e 73 7a 41 74 6f  (size*mem5.szAto
8e2f0 6d 29 20 29 3b 0a 20 20 6d 65 6d 35 2e 63 75 72  m) );.  mem5.cur
8e300 72 65 6e 74 43 6f 75 6e 74 2d 2d 3b 0a 20 20 6d  rentCount--;.  m
8e310 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 2d  em5.currentOut -
8e320 3d 20 73 69 7a 65 2a 6d 65 6d 35 2e 73 7a 41 74  = size*mem5.szAt
8e330 6f 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65  om;.  assert( me
8e340 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3e 30 20  m5.currentOut>0 
8e350 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43  || mem5.currentC
8e360 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73  ount==0 );.  ass
8e370 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 6e  ert( mem5.curren
8e380 74 43 6f 75 6e 74 3e 30 20 7c 7c 20 6d 65 6d 35  tCount>0 || mem5
8e390 2e 63 75 72 72 65 6e 74 4f 75 74 3d 3d 30 20 29  .currentOut==0 )
8e3a0 3b 0a 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b  ;..  mem5.aCtrl[
8e3b0 69 42 6c 6f 63 6b 5d 20 3d 20 43 54 52 4c 5f 46  iBlock] = CTRL_F
8e3c0 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a  REE | iLogsize;.
8e3d0 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 53 28    while( ALWAYS(
8e3e0 69 4c 6f 67 73 69 7a 65 3c 4c 4f 47 4d 41 58 29  iLogsize<LOGMAX)
8e3f0 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 42 75 64   ){.    int iBud
8e400 64 79 3b 0a 20 20 20 20 69 66 28 20 28 69 42 6c  dy;.    if( (iBl
8e410 6f 63 6b 3e 3e 69 4c 6f 67 73 69 7a 65 29 20 26  ock>>iLogsize) &
8e420 20 31 20 29 7b 0a 20 20 20 20 20 20 69 42 75 64   1 ){.      iBud
8e430 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2d 20 73 69  dy = iBlock - si
8e440 7a 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ze;.    }else{. 
8e450 20 20 20 20 20 69 42 75 64 64 79 20 3d 20 69 42       iBuddy = iB
8e460 6c 6f 63 6b 20 2b 20 73 69 7a 65 3b 0a 20 20 20  lock + size;.   
8e470 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69   }.    assert( i
8e480 42 75 64 64 79 3e 3d 30 20 29 3b 0a 20 20 20 20  Buddy>=0 );.    
8e490 69 66 28 20 28 69 42 75 64 64 79 2b 28 31 3c 3c  if( (iBuddy+(1<<
8e4a0 69 4c 6f 67 73 69 7a 65 29 29 3e 6d 65 6d 35 2e  iLogsize))>mem5.
8e4b0 6e 42 6c 6f 63 6b 20 29 20 62 72 65 61 6b 3b 0a  nBlock ) break;.
8e4c0 20 20 20 20 69 66 28 20 6d 65 6d 35 2e 61 43 74      if( mem5.aCt
8e4d0 72 6c 5b 69 42 75 64 64 79 5d 21 3d 28 43 54 52  rl[iBuddy]!=(CTR
8e4e0 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a  L_FREE | iLogsiz
8e4f0 65 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  e) ) break;.    
8e500 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 42  memsys5Unlink(iB
8e510 75 64 64 79 2c 20 69 4c 6f 67 73 69 7a 65 29 3b  uddy, iLogsize);
8e520 0a 20 20 20 20 69 4c 6f 67 73 69 7a 65 2b 2b 3b  .    iLogsize++;
8e530 0a 20 20 20 20 69 66 28 20 69 42 75 64 64 79 3c  .    if( iBuddy<
8e540 69 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20  iBlock ){.      
8e550 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 64  mem5.aCtrl[iBudd
8e560 79 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c  y] = CTRL_FREE |
8e570 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20 20   iLogsize;.     
8e580 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f   mem5.aCtrl[iBlo
8e590 63 6b 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  ck] = 0;.      i
8e5a0 42 6c 6f 63 6b 20 3d 20 69 42 75 64 64 79 3b 0a  Block = iBuddy;.
8e5b0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
8e5c0 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f   mem5.aCtrl[iBlo
8e5d0 63 6b 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20  ck] = CTRL_FREE 
8e5e0 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20  | iLogsize;.    
8e5f0 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75    mem5.aCtrl[iBu
8e600 64 64 79 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  ddy] = 0;.    }.
8e610 20 20 20 20 73 69 7a 65 20 2a 3d 20 32 3b 0a 20      size *= 2;. 
8e620 20 7d 0a 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b   }.  memsys5Link
8e630 28 69 42 6c 6f 63 6b 2c 20 69 4c 6f 67 73 69 7a  (iBlock, iLogsiz
8e640 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  e);.}../*.** All
8e650 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f 66 20  ocate nBytes of 
8e660 6d 65 6d 6f 72 79 0a 2a 2f 0a 73 74 61 74 69 63  memory.*/.static
8e670 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 4d 61   void *memsys5Ma
8e680 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 73 29  lloc(int nBytes)
8e690 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  {.  sqlite3_int6
8e6a0 34 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28 20  4 *p = 0;.  if( 
8e6b0 6e 42 79 74 65 73 3e 30 20 29 7b 0a 20 20 20 20  nBytes>0 ){.    
8e6c0 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b 0a  memsys5Enter();.
8e6d0 20 20 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 4d      p = memsys5M
8e6e0 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74  allocUnsafe(nByt
8e6f0 65 73 29 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35  es);.    memsys5
8e700 4c 65 61 76 65 28 29 3b 0a 20 20 7d 0a 20 20 72  Leave();.  }.  r
8e710 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b 20  eturn (void*)p; 
8e720 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d  .}../*.** Free m
8e730 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  emory..**.** The
8e740 20 6f 75 74 65 72 20 6c 61 79 65 72 20 6d 65 6d   outer layer mem
8e750 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 70 72  ory allocator pr
8e760 65 76 65 6e 74 73 20 74 68 69 73 20 72 6f 75 74  events this rout
8e770 69 6e 65 20 66 72 6f 6d 0a 2a 2a 20 62 65 69 6e  ine from.** bein
8e780 67 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70 50  g called with pP
8e790 72 69 6f 72 3d 3d 30 2e 0a 2a 2f 0a 73 74 61 74  rior==0..*/.stat
8e7a0 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 46  ic void memsys5F
8e7b0 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  ree(void *pPrior
8e7c0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 72  ){.  assert( pPr
8e7d0 69 6f 72 21 3d 30 20 29 3b 0a 20 20 6d 65 6d 73  ior!=0 );.  mems
8e7e0 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 6d 65  ys5Enter();.  me
8e7f0 6d 73 79 73 35 46 72 65 65 55 6e 73 61 66 65 28  msys5FreeUnsafe(
8e800 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65 6d 73 79  pPrior);.  memsy
8e810 73 35 4c 65 61 76 65 28 29 3b 20 20 0a 7d 0a 0a  s5Leave();  .}..
8e820 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
8e830 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73   size of an exis
8e840 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ting memory allo
8e850 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  cation..**.** Th
8e860 65 20 6f 75 74 65 72 20 6c 61 79 65 72 20 6d 65  e outer layer me
8e870 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 70  mory allocator p
8e880 72 65 76 65 6e 74 73 20 74 68 69 73 20 72 6f 75  revents this rou
8e890 74 69 6e 65 20 66 72 6f 6d 0a 2a 2a 20 62 65 69  tine from.** bei
8e8a0 6e 67 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70  ng called with p
8e8b0 50 72 69 6f 72 3d 3d 30 2e 20 20 0a 2a 2a 0a 2a  Prior==0.  .**.*
8e8c0 2a 20 6e 42 79 74 65 73 20 69 73 20 61 6c 77 61  * nBytes is alwa
8e8d0 79 73 20 61 20 76 61 6c 75 65 20 6f 62 74 61 69  ys a value obtai
8e8e0 6e 65 64 20 66 72 6f 6d 20 61 20 70 72 69 6f 72  ned from a prior
8e8f0 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 6d 65 6d 73   call to.** mems
8e900 79 73 35 52 6f 75 6e 64 28 29 2e 20 20 48 65 6e  ys5Round().  Hen
8e910 63 65 20 6e 42 79 74 65 73 20 69 73 20 61 6c 77  ce nBytes is alw
8e920 61 79 73 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69  ays a non-negati
8e930 76 65 20 70 6f 77 65 72 0a 2a 2a 20 6f 66 20 74  ve power.** of t
8e940 77 6f 2e 20 20 49 66 20 6e 42 79 74 65 73 3d 3d  wo.  If nBytes==
8e950 30 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61  0 that means tha
8e960 74 20 61 6e 20 6f 76 65 72 73 69 7a 65 20 61 6c  t an oversize al
8e970 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 28 61 6e 20  location.** (an 
8e980 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67 65  allocation large
8e990 72 20 74 68 61 6e 20 30 78 34 30 30 30 30 30 30  r than 0x4000000
8e9a0 30 29 20 77 61 73 20 72 65 71 75 65 73 74 65 64  0) was requested
8e9b0 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 72 6f 75   and this.** rou
8e9c0 74 69 6e 65 20 73 68 6f 75 6c 64 20 72 65 74 75  tine should retu
8e9d0 72 6e 20 30 20 77 69 74 68 6f 75 74 20 66 72 65  rn 0 without fre
8e9e0 65 69 6e 67 20 70 50 72 69 6f 72 2e 0a 2a 2f 0a  eing pPrior..*/.
8e9f0 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d  static void *mem
8ea00 73 79 73 35 52 65 61 6c 6c 6f 63 28 76 6f 69 64  sys5Realloc(void
8ea10 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42   *pPrior, int nB
8ea20 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c  ytes){.  int nOl
8ea30 64 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20  d;.  void *p;.  
8ea40 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d  assert( pPrior!=
8ea50 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  0 );.  assert( (
8ea60 6e 42 79 74 65 73 26 28 6e 42 79 74 65 73 2d 31  nBytes&(nBytes-1
8ea70 29 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ))==0 );.  asser
8ea80 74 28 20 6e 42 79 74 65 73 3e 3d 30 20 29 3b 0a  t( nBytes>=0 );.
8ea90 20 20 69 66 28 20 6e 42 79 74 65 73 3d 3d 30 20    if( nBytes==0 
8eaa0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
8eab0 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d 65  .  }.  nOld = me
8eac0 6d 73 79 73 35 53 69 7a 65 28 70 50 72 69 6f 72  msys5Size(pPrior
8ead0 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c  );.  if( nBytes<
8eae0 3d 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 72 65 74  =nOld ){.    ret
8eaf0 75 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d 0a  urn pPrior;.  }.
8eb00 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29    memsys5Enter()
8eb10 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 4d  ;.  p = memsys5M
8eb20 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74  allocUnsafe(nByt
8eb30 65 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a  es);.  if( p ){.
8eb40 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 50      memcpy(p, pP
8eb50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20 20  rior, nOld);.   
8eb60 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73 61   memsys5FreeUnsa
8eb70 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a  fe(pPrior);.  }.
8eb80 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29    memsys5Leave()
8eb90 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  ;.  return p;.}.
8eba0 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20  ./*.** Round up 
8ebb0 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74  a request size t
8ebc0 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64  o the next valid
8ebd0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65   allocation size
8ebe0 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 61 6c 6c  .  If.** the all
8ebf0 6f 63 61 74 69 6f 6e 20 69 73 20 74 6f 6f 20 6c  ocation is too l
8ec00 61 72 67 65 20 74 6f 20 62 65 20 68 61 6e 64 6c  arge to be handl
8ec10 65 64 20 62 79 20 74 68 69 73 20 61 6c 6c 6f 63  ed by this alloc
8ec20 61 74 69 6f 6e 20 73 79 73 74 65 6d 2c 0a 2a 2a  ation system,.**
8ec30 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a   return 0..**.**
8ec40 20 41 6c 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e 73   All allocations
8ec50 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72   must be a power
8ec60 20 6f 66 20 74 77 6f 20 61 6e 64 20 6d 75 73 74   of two and must
8ec70 20 62 65 20 65 78 70 72 65 73 73 65 64 20 62 79   be expressed by
8ec80 20 61 0a 2a 2a 20 33 32 2d 62 69 74 20 73 69 67   a.** 32-bit sig
8ec90 6e 65 64 20 69 6e 74 65 67 65 72 2e 20 20 48 65  ned integer.  He
8eca0 6e 63 65 20 74 68 65 20 6c 61 72 67 65 73 74 20  nce the largest 
8ecb0 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 30 78  allocation is 0x
8ecc0 34 30 30 30 30 30 30 30 0a 2a 2a 20 6f 72 20 31  40000000.** or 1
8ecd0 30 37 33 37 34 31 38 32 34 20 62 79 74 65 73 2e  073741824 bytes.
8ece0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
8ecf0 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 28 69 6e  emsys5Roundup(in
8ed00 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 46 75 6c  t n){.  int iFul
8ed10 6c 53 7a 3b 0a 20 20 69 66 28 20 6e 20 3e 20 30  lSz;.  if( n > 0
8ed20 78 34 30 30 30 30 30 30 30 20 29 20 72 65 74 75  x40000000 ) retu
8ed30 72 6e 20 30 3b 0a 20 20 66 6f 72 28 69 46 75 6c  rn 0;.  for(iFul
8ed40 6c 53 7a 3d 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b  lSz=mem5.szAtom;
8ed50 20 69 46 75 6c 6c 53 7a 3c 6e 3b 20 69 46 75 6c   iFullSz<n; iFul
8ed60 6c 53 7a 20 2a 3d 20 32 29 3b 0a 20 20 72 65 74  lSz *= 2);.  ret
8ed70 75 72 6e 20 69 46 75 6c 6c 53 7a 3b 0a 7d 0a 0a  urn iFullSz;.}..
8ed80 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
8ed90 20 63 65 69 6c 69 6e 67 20 6f 66 20 74 68 65 20   ceiling of the 
8eda0 6c 6f 67 61 72 69 74 68 6d 20 62 61 73 65 20 32  logarithm base 2
8edb0 20 6f 66 20 69 56 61 6c 75 65 2e 0a 2a 2a 0a 2a   of iValue..**.*
8edc0 2a 20 45 78 61 6d 70 6c 65 73 3a 20 20 20 6d 65  * Examples:   me
8edd0 6d 73 79 73 35 4c 6f 67 28 31 29 20 2d 3e 20 30  msys5Log(1) -> 0
8ede0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
8edf0 6d 65 6d 73 79 73 35 4c 6f 67 28 32 29 20 2d 3e  memsys5Log(2) ->
8ee00 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20   1.**           
8ee10 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 34 29 20    memsys5Log(4) 
8ee20 2d 3e 20 32 0a 2a 2a 20 20 20 20 20 20 20 20 20  -> 2.**         
8ee30 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 35      memsys5Log(5
8ee40 29 20 2d 3e 20 33 0a 2a 2a 20 20 20 20 20 20 20  ) -> 3.**       
8ee50 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67        memsys5Log
8ee60 28 38 29 20 2d 3e 20 33 0a 2a 2a 20 20 20 20 20  (8) -> 3.**     
8ee70 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c          memsys5L
8ee80 6f 67 28 39 29 20 2d 3e 20 34 0a 2a 2f 0a 73 74  og(9) -> 4.*/.st
8ee90 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35  atic int memsys5
8eea0 4c 6f 67 28 69 6e 74 20 69 56 61 6c 75 65 29 7b  Log(int iValue){
8eeb0 0a 20 20 69 6e 74 20 69 4c 6f 67 3b 0a 20 20 66  .  int iLog;.  f
8eec0 6f 72 28 69 4c 6f 67 3d 30 3b 20 28 31 3c 3c 69  or(iLog=0; (1<<i
8eed0 4c 6f 67 29 3c 69 56 61 6c 75 65 3b 20 69 4c 6f  Log)<iValue; iLo
8eee0 67 2b 2b 29 3b 0a 20 20 72 65 74 75 72 6e 20 69  g++);.  return i
8eef0 4c 6f 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  Log;.}../*.** In
8ef00 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d  itialize the mem
8ef10 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e 0a 2a  ory allocator..*
8ef20 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
8ef30 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73  e is not threads
8ef40 61 66 65 2e 20 20 54 68 65 20 63 61 6c 6c 65 72  afe.  The caller
8ef50 20 6d 75 73 74 20 62 65 20 68 6f 6c 64 69 6e 67   must be holding
8ef60 20 61 20 6d 75 74 65 78 0a 2a 2a 20 74 6f 20 70   a mutex.** to p
8ef70 72 65 76 65 6e 74 20 6d 75 6c 74 69 70 6c 65 20  revent multiple 
8ef80 74 68 72 65 61 64 73 20 66 72 6f 6d 20 65 6e 74  threads from ent
8ef90 65 72 69 6e 67 20 61 74 20 74 68 65 20 73 61 6d  ering at the sam
8efa0 65 20 74 69 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69  e time..*/.stati
8efb0 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 49 6e 69  c int memsys5Ini
8efc0 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29  t(void *NotUsed)
8efd0 7b 0a 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20  {.  int ii;     
8efe0 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
8eff0 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  ounter */.  int 
8f000 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 2f  nByte;         /
8f010 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
8f020 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69  s of memory avai
8f030 6c 61 62 6c 65 20 74 6f 20 74 68 69 73 20 61 6c  lable to this al
8f040 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20  locator */.  u8 
8f050 2a 7a 42 79 74 65 3b 20 20 20 20 20 20 20 20 20  *zByte;         
8f060 2f 2a 20 4d 65 6d 6f 72 79 20 75 73 61 62 6c 65  /* Memory usable
8f070 20 62 79 20 74 68 69 73 20 61 6c 6c 6f 63 61 74   by this allocat
8f080 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 69 6e  or */.  int nMin
8f090 4c 6f 67 3b 20 20 20 20 20 20 20 2f 2a 20 4c 6f  Log;       /* Lo
8f0a0 67 20 62 61 73 65 20 32 20 6f 66 20 6d 69 6e 69  g base 2 of mini
8f0b0 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  mum allocation s
8f0c0 69 7a 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  ize in bytes */.
8f0d0 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 20    int iOffset;  
8f0e0 20 20 20 20 20 2f 2a 20 41 6e 20 6f 66 66 73 65       /* An offse
8f0f0 74 20 69 6e 74 6f 20 6d 65 6d 35 2e 61 43 74 72  t into mem5.aCtr
8f100 6c 5b 5d 20 2a 2f 0a 0a 20 20 55 4e 55 53 45 44  l[] */..  UNUSED
8f110 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
8f120 65 64 29 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20 74  ed);..  /* For t
8f130 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74  he purposes of t
8f140 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 64 69 73  his routine, dis
8f150 61 62 6c 65 20 74 68 65 20 6d 75 74 65 78 20 2a  able the mutex *
8f160 2f 0a 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 3d  /.  mem5.mutex =
8f170 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 73 69   0;..  /* The si
8f180 7a 65 20 6f 66 20 61 20 4d 65 6d 35 4c 69 6e 6b  ze of a Mem5Link
8f190 20 6f 62 6a 65 63 74 20 6d 75 73 74 20 62 65 20   object must be 
8f1a0 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20  a power of two. 
8f1b0 20 56 65 72 69 66 79 20 74 68 61 74 0a 20 20 2a   Verify that.  *
8f1c0 2a 20 74 68 69 73 20 69 73 20 63 61 73 65 2e 0a  * this is case..
8f1d0 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28    */.  assert( (
8f1e0 73 69 7a 65 6f 66 28 4d 65 6d 35 4c 69 6e 6b 29  sizeof(Mem5Link)
8f1f0 26 28 73 69 7a 65 6f 66 28 4d 65 6d 35 4c 69 6e  &(sizeof(Mem5Lin
8f200 6b 29 2d 31 29 29 3d 3d 30 20 29 3b 0a 0a 20 20  k)-1))==0 );..  
8f210 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 65 33 47  nByte = sqlite3G
8f220 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 61  lobalConfig.nHea
8f230 70 3b 0a 20 20 7a 42 79 74 65 20 3d 20 28 75 38  p;.  zByte = (u8
8f240 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  *)sqlite3GlobalC
8f250 6f 6e 66 69 67 2e 70 48 65 61 70 3b 0a 20 20 61  onfig.pHeap;.  a
8f260 73 73 65 72 74 28 20 7a 42 79 74 65 21 3d 30 20  ssert( zByte!=0 
8f270 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63  );  /* sqlite3_c
8f280 6f 6e 66 69 67 28 29 20 64 6f 65 73 20 6e 6f 74  onfig() does not
8f290 20 61 6c 6c 6f 77 20 6f 74 68 65 72 77 69 73 65   allow otherwise
8f2a0 20 2a 2f 0a 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d   */..  nMinLog =
8f2b0 20 6d 65 6d 73 79 73 35 4c 6f 67 28 73 71 6c 69   memsys5Log(sqli
8f2c0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
8f2d0 6d 6e 52 65 71 29 3b 0a 20 20 6d 65 6d 35 2e 73  mnReq);.  mem5.s
8f2e0 7a 41 74 6f 6d 20 3d 20 28 31 3c 3c 6e 4d 69 6e  zAtom = (1<<nMin
8f2f0 4c 6f 67 29 3b 0a 20 20 77 68 69 6c 65 28 20 28  Log);.  while( (
8f300 69 6e 74 29 73 69 7a 65 6f 66 28 4d 65 6d 35 4c  int)sizeof(Mem5L
8f310 69 6e 6b 29 3e 6d 65 6d 35 2e 73 7a 41 74 6f 6d  ink)>mem5.szAtom
8f320 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 73 7a 41   ){.    mem5.szA
8f330 74 6f 6d 20 3d 20 6d 65 6d 35 2e 73 7a 41 74 6f  tom = mem5.szAto
8f340 6d 20 3c 3c 20 31 3b 0a 20 20 7d 0a 0a 20 20 6d  m << 1;.  }..  m
8f350 65 6d 35 2e 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42  em5.nBlock = (nB
8f360 79 74 65 20 2f 20 28 6d 65 6d 35 2e 73 7a 41 74  yte / (mem5.szAt
8f370 6f 6d 2b 73 69 7a 65 6f 66 28 75 38 29 29 29 3b  om+sizeof(u8)));
8f380 0a 20 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 3d 20  .  mem5.zPool = 
8f390 7a 42 79 74 65 3b 0a 20 20 6d 65 6d 35 2e 61 43  zByte;.  mem5.aC
8f3a0 74 72 6c 20 3d 20 28 75 38 20 2a 29 26 6d 65 6d  trl = (u8 *)&mem
8f3b0 35 2e 7a 50 6f 6f 6c 5b 6d 65 6d 35 2e 6e 42 6c  5.zPool[mem5.nBl
8f3c0 6f 63 6b 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 5d  ock*mem5.szAtom]
8f3d0 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  ;..  for(ii=0; i
8f3e0 69 3c 3d 4c 4f 47 4d 41 58 3b 20 69 69 2b 2b 29  i<=LOGMAX; ii++)
8f3f0 7b 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72 65  {.    mem5.aiFre
8f400 65 6c 69 73 74 5b 69 69 5d 20 3d 20 2d 31 3b 0a  elist[ii] = -1;.
8f410 20 20 7d 0a 0a 20 20 69 4f 66 66 73 65 74 20 3d    }..  iOffset =
8f420 20 30 3b 0a 20 20 66 6f 72 28 69 69 3d 4c 4f 47   0;.  for(ii=LOG
8f430 4d 41 58 3b 20 69 69 3e 3d 30 3b 20 69 69 2d 2d  MAX; ii>=0; ii--
8f440 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c 6f  ){.    int nAllo
8f450 63 20 3d 20 28 31 3c 3c 69 69 29 3b 0a 20 20 20  c = (1<<ii);.   
8f460 20 69 66 28 20 28 69 4f 66 66 73 65 74 2b 6e 41   if( (iOffset+nA
8f470 6c 6c 6f 63 29 3c 3d 6d 65 6d 35 2e 6e 42 6c 6f  lloc)<=mem5.nBlo
8f480 63 6b 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 35  ck ){.      mem5
8f490 2e 61 43 74 72 6c 5b 69 4f 66 66 73 65 74 5d 20  .aCtrl[iOffset] 
8f4a0 3d 20 69 69 20 7c 20 43 54 52 4c 5f 46 52 45 45  = ii | CTRL_FREE
8f4b0 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c  ;.      memsys5L
8f4c0 69 6e 6b 28 69 4f 66 66 73 65 74 2c 20 69 69 29  ink(iOffset, ii)
8f4d0 3b 0a 20 20 20 20 20 20 69 4f 66 66 73 65 74 20  ;.      iOffset 
8f4e0 2b 3d 20 6e 41 6c 6c 6f 63 3b 0a 20 20 20 20 7d  += nAlloc;.    }
8f4f0 0a 20 20 20 20 61 73 73 65 72 74 28 28 69 4f 66  .    assert((iOf
8f500 66 73 65 74 2b 6e 41 6c 6c 6f 63 29 3e 6d 65 6d  fset+nAlloc)>mem
8f510 35 2e 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a  5.nBlock);.  }..
8f520 20 20 2f 2a 20 49 66 20 61 20 6d 75 74 65 78 20    /* If a mutex 
8f530 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20  is required for 
8f540 6e 6f 72 6d 61 6c 20 6f 70 65 72 61 74 69 6f 6e  normal operation
8f550 2c 20 61 6c 6c 6f 63 61 74 65 20 6f 6e 65 20 2a  , allocate one *
8f560 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  /.  if( sqlite3G
8f570 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d  lobalConfig.bMem
8f580 73 74 61 74 3d 3d 30 20 29 7b 0a 20 20 20 20 6d  stat==0 ){.    m
8f590 65 6d 35 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69  em5.mutex = sqli
8f5a0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
8f5b0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
8f5c0 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 0a 20 20 72  C_MEM);.  }..  r
8f5d0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
8f5e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74  .}../*.** Deinit
8f5f0 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75  ialize this modu
8f600 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  le..*/.static vo
8f610 69 64 20 6d 65 6d 73 79 73 35 53 68 75 74 64 6f  id memsys5Shutdo
8f620 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64  wn(void *NotUsed
8f630 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ){.  UNUSED_PARA
8f640 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
8f650 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 3d 20 30    mem5.mutex = 0
8f660 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 23  ;.  return;.}..#
8f670 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
8f680 54 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65  T./*.** Open the
8f690 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 20   file indicated 
8f6a0 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 20  and write a log 
8f6b0 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d  of all unfreed m
8f6c0 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61  emory .** alloca
8f6d0 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 20  tions into that 
8f6e0 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  log..*/.SQLITE_P
8f6f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
8f700 74 65 33 4d 65 6d 73 79 73 35 44 75 6d 70 28 63  te3Memsys5Dump(c
8f710 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
8f720 6e 61 6d 65 29 7b 0a 20 20 46 49 4c 45 20 2a 6f  name){.  FILE *o
8f730 75 74 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20  ut;.  int i, j, 
8f740 6e 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f 67  n;.  int nMinLog
8f750 3b 0a 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61  ;..  if( zFilena
8f760 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e 61  me==0 || zFilena
8f770 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20  me[0]==0 ){.    
8f780 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20  out = stdout;.  
8f790 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20 3d  }else{.    out =
8f7a0 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65   fopen(zFilename
8f7b0 2c 20 22 77 22 29 3b 0a 20 20 20 20 69 66 28 20  , "w");.    if( 
8f7c0 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  out==0 ){.      
8f7d0 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20  fprintf(stderr, 
8f7e0 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75  "** Unable to ou
8f7f0 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 75  tput memory debu
8f800 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 73  g output log: %s
8f810 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20   **\n",.        
8f820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 46                zF
8f830 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ilename);.      
8f840 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
8f850 7d 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72  }.  memsys5Enter
8f860 28 29 3b 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d 20  ();.  nMinLog = 
8f870 6d 65 6d 73 79 73 35 4c 6f 67 28 6d 65 6d 35 2e  memsys5Log(mem5.
8f880 73 7a 41 74 6f 6d 29 3b 0a 20 20 66 6f 72 28 69  szAtom);.  for(i
8f890 3d 30 3b 20 69 3c 3d 4c 4f 47 4d 41 58 20 26 26  =0; i<=LOGMAX &&
8f8a0 20 69 2b 6e 4d 69 6e 4c 6f 67 3c 33 32 3b 20 69   i+nMinLog<32; i
8f8b0 2b 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6e 3d 30  ++){.    for(n=0
8f8c0 2c 20 6a 3d 6d 65 6d 35 2e 61 69 46 72 65 65 6c  , j=mem5.aiFreel
8f8d0 69 73 74 5b 69 5d 3b 20 6a 3e 3d 30 3b 20 6a 20  ist[i]; j>=0; j 
8f8e0 3d 20 4d 45 4d 35 4c 49 4e 4b 28 6a 29 2d 3e 6e  = MEM5LINK(j)->n
8f8f0 65 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20  ext, n++){}.    
8f900 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 66 72  fprintf(out, "fr
8f910 65 65 6c 69 73 74 20 69 74 65 6d 73 20 6f 66 20  eelist items of 
8f920 73 69 7a 65 20 25 64 3a 20 25 64 5c 6e 22 2c 20  size %d: %d\n", 
8f930 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3c 3c 20 69  mem5.szAtom << i
8f940 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 66 70 72 69  , n);.  }.  fpri
8f950 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6e  ntf(out, "mem5.n
8f960 41 6c 6c 6f 63 20 20 20 20 20 20 20 3d 20 25 6c  Alloc       = %l
8f970 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6e 41 6c 6c  lu\n", mem5.nAll
8f980 6f 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  oc);.  fprintf(o
8f990 75 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 41  ut, "mem5.totalA
8f9a0 6c 6c 6f 63 20 20 20 3d 20 25 6c 6c 75 5c 6e 22  lloc   = %llu\n"
8f9b0 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f  , mem5.totalAllo
8f9c0 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75  c);.  fprintf(ou
8f9d0 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 45 78  t, "mem5.totalEx
8f9e0 63 65 73 73 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c  cess  = %llu\n",
8f9f0 20 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 73   mem5.totalExces
8fa00 73 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75  s);.  fprintf(ou
8fa10 74 2c 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74  t, "mem5.current
8fa20 4f 75 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d  Out   = %u\n", m
8fa30 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 29 3b  em5.currentOut);
8fa40 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20  .  fprintf(out, 
8fa50 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75  "mem5.currentCou
8fa60 6e 74 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35  nt = %u\n", mem5
8fa70 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 29 3b 0a  .currentCount);.
8fa80 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
8fa90 6d 65 6d 35 2e 6d 61 78 4f 75 74 20 20 20 20 20  mem5.maxOut     
8faa0 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e    = %u\n", mem5.
8fab0 6d 61 78 4f 75 74 29 3b 0a 20 20 66 70 72 69 6e  maxOut);.  fprin
8fac0 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d 61  tf(out, "mem5.ma
8fad0 78 43 6f 75 6e 74 20 20 20 20 20 3d 20 25 75 5c  xCount     = %u\
8fae0 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e  n", mem5.maxCoun
8faf0 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75  t);.  fprintf(ou
8fb00 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 52 65 71 75  t, "mem5.maxRequ
8fb10 65 73 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d  est   = %u\n", m
8fb20 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 29 3b  em5.maxRequest);
8fb30 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28  .  memsys5Leave(
8fb40 29 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d 73 74  );.  if( out==st
8fb50 64 6f 75 74 20 29 7b 0a 20 20 20 20 66 66 6c 75  dout ){.    fflu
8fb60 73 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 7d 65  sh(stdout);.  }e
8fb70 6c 73 65 7b 0a 20 20 20 20 66 63 6c 6f 73 65 28  lse{.    fclose(
8fb80 6f 75 74 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  out);.  }.}.#end
8fb90 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  if../*.** This r
8fba0 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e  outine is the on
8fbb0 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68  ly routine in th
8fbc0 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74  is file with ext
8fbd0 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 67  ernal .** linkag
8fbe0 65 2e 20 49 74 20 72 65 74 75 72 6e 73 20 61 20  e. It returns a 
8fbf0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61  pointer to a sta
8fc00 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  tic sqlite3_mem_
8fc10 6d 65 74 68 6f 64 73 0a 2a 2a 20 73 74 72 75 63  methods.** struc
8fc20 74 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68  t populated with
8fc30 20 74 68 65 20 6d 65 6d 73 79 73 35 20 6d 65 74   the memsys5 met
8fc40 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  hods..*/.SQLITE_
8fc50 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71  PRIVATE const sq
8fc60 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
8fc70 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74  s *sqlite3MemGet
8fc80 4d 65 6d 73 79 73 35 28 76 6f 69 64 29 7b 0a 20  Memsys5(void){. 
8fc90 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71   static const sq
8fca0 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
8fcb0 73 20 6d 65 6d 73 79 73 35 4d 65 74 68 6f 64 73  s memsys5Methods
8fcc0 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79 73   = {.     memsys
8fcd0 35 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65  5Malloc,.     me
8fce0 6d 73 79 73 35 46 72 65 65 2c 0a 20 20 20 20 20  msys5Free,.     
8fcf0 6d 65 6d 73 79 73 35 52 65 61 6c 6c 6f 63 2c 0a  memsys5Realloc,.
8fd00 20 20 20 20 20 6d 65 6d 73 79 73 35 53 69 7a 65       memsys5Size
8fd10 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52 6f  ,.     memsys5Ro
8fd20 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d 73  undup,.     mems
8fd30 79 73 35 49 6e 69 74 2c 0a 20 20 20 20 20 6d 65  ys5Init,.     me
8fd40 6d 73 79 73 35 53 68 75 74 64 6f 77 6e 2c 0a 20  msys5Shutdown,. 
8fd50 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65 74      0.  };.  ret
8fd60 75 72 6e 20 26 6d 65 6d 73 79 73 35 4d 65 74 68  urn &memsys5Meth
8fd70 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  ods;.}..#endif /
8fd80 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  * SQLITE_ENABLE_
8fd90 4d 45 4d 53 59 53 35 20 2a 2f 0a 0a 2f 2a 2a 2a  MEMSYS5 */../***
8fda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
8fdb0 6f 66 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a 2a  of mem5.c ******
8fdc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8fdd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8fde0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
8fdf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
8fe00 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e 63 20 2a  n file mutex.c *
8fe10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8fe20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8fe30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
8fe40 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31 34  * 2007 August 14
8fe50 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
8fe60 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
8fe70 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
8fe80 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
8fe90 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
8fea0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
8feb0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
8fec0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
8fed0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
8fee0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
8fef0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
8ff00 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
8ff10 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
8ff20 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
8ff30 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
8ff40 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
8ff50 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
8ff60 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
8ff70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ff80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ff90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ffa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
8ffb0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
8ffc0 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63  tains the C func
8ffd0 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65  tions that imple
8ffe0 6d 65 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a 2a  ment mutexes..**
8fff0 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
90000 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74  ntains code that
90010 20 69 73 20 63 6f 6d 6d 6f 6e 20 61 63 72 6f 73   is common acros
90020 73 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70 6c  s all mutex impl
90030 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 0a 2a 2a  ementations...**
90040 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 2e 63  .** $Id: mutex.c
90050 2c 76 20 31 2e 33 31 20 32 30 30 39 2f 30 37 2f  ,v 1.31 2009/07/
90060 31 36 20 31 38 3a 32 31 3a 31 38 20 64 72 68 20  16 18:21:18 drh 
90070 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65  Exp $.*/..#ifnde
90080 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f  f SQLITE_MUTEX_O
90090 4d 49 54 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  MIT./*.** Initia
900a0 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73  lize the mutex s
900b0 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ystem..*/.SQLITE
900c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
900d0 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 76 6f  ite3MutexInit(vo
900e0 69 64 29 7b 20 0a 20 20 69 6e 74 20 72 63 20 3d  id){ .  int rc =
900f0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
90100 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
90110 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78  onfig.bCoreMutex
90120 20 29 7b 0a 20 20 20 20 69 66 28 20 21 73 71 6c   ){.    if( !sql
90130 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
90140 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c  .mutex.xMutexAll
90150 6f 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49  oc ){.      /* I
90160 66 20 74 68 65 20 78 4d 75 74 65 78 41 6c 6c 6f  f the xMutexAllo
90170 63 20 6d 65 74 68 6f 64 20 68 61 73 20 6e 6f 74  c method has not
90180 20 62 65 65 6e 20 73 65 74 2c 20 74 68 65 6e 20   been set, then 
90190 74 68 65 20 75 73 65 72 20 64 69 64 20 6e 6f 74  the user did not
901a0 0a 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 61 6c  .      ** instal
901b0 6c 20 61 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d  l a mutex implem
901c0 65 6e 74 61 74 69 6f 6e 20 76 69 61 20 73 71 6c  entation via sql
901d0 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 70 72  ite3_config() pr
901e0 69 6f 72 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a  ior to .      **
901f0 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
90200 69 7a 65 28 29 20 62 65 69 6e 67 20 63 61 6c 6c  ize() being call
90210 65 64 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 63  ed. This block c
90220 6f 70 69 65 73 20 70 6f 69 6e 74 65 72 73 20 74  opies pointers t
90230 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64  o.      ** the d
90240 65 66 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e 74  efault implement
90250 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 73  ation into the s
90260 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
90270 69 67 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20  ig structure..  
90280 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c      */.      sql
90290 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
902a0 64 73 20 2a 70 46 72 6f 6d 20 3d 20 73 71 6c 69  ds *pFrom = sqli
902b0 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28  te3DefaultMutex(
902c0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
902d0 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a  _mutex_methods *
902e0 70 54 6f 20 3d 20 26 73 71 6c 69 74 65 33 47 6c  pTo = &sqlite3Gl
902f0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78  obalConfig.mutex
90300 3b 0a 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ;..      memcpy(
90310 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 6f 66 66 73  pTo, pFrom, offs
90320 65 74 6f 66 28 73 71 6c 69 74 65 33 5f 6d 75 74  etof(sqlite3_mut
90330 65 78 5f 6d 65 74 68 6f 64 73 2c 20 78 4d 75 74  ex_methods, xMut
90340 65 78 41 6c 6c 6f 63 29 29 3b 0a 20 20 20 20 20  exAlloc));.     
90350 20 6d 65 6d 63 70 79 28 26 70 54 6f 2d 3e 78 4d   memcpy(&pTo->xM
90360 75 74 65 78 46 72 65 65 2c 20 26 70 46 72 6f 6d  utexFree, &pFrom
90370 2d 3e 78 4d 75 74 65 78 46 72 65 65 2c 0a 20 20  ->xMutexFree,.  
90380 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f             sizeo
90390 66 28 2a 70 54 6f 29 20 2d 20 6f 66 66 73 65 74  f(*pTo) - offset
903a0 6f 66 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  of(sqlite3_mutex
903b0 5f 6d 65 74 68 6f 64 73 2c 20 78 4d 75 74 65 78  _methods, xMutex
903c0 46 72 65 65 29 29 3b 0a 20 20 20 20 20 20 70 54  Free));.      pT
903d0 6f 2d 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 20 3d  o->xMutexAlloc =
903e0 20 70 46 72 6f 6d 2d 3e 78 4d 75 74 65 78 41 6c   pFrom->xMutexAl
903f0 6c 6f 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  loc;.    }.    r
90400 63 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  c = sqlite3Globa
90410 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d  lConfig.mutex.xM
90420 75 74 65 78 49 6e 69 74 28 29 3b 0a 20 20 7d 0a  utexInit();.  }.
90430 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
90440 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20  ./*.** Shutdown 
90450 74 68 65 20 6d 75 74 65 78 20 73 79 73 74 65 6d  the mutex system
90460 2e 20 54 68 69 73 20 63 61 6c 6c 20 66 72 65 65  . This call free
90470 73 20 72 65 73 6f 75 72 63 65 73 20 61 6c 6c 6f  s resources allo
90480 63 61 74 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69  cated by.** sqli
90490 74 65 33 4d 75 74 65 78 49 6e 69 74 28 29 2e 0a  te3MutexInit()..
904a0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
904b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74  E int sqlite3Mut
904c0 65 78 45 6e 64 28 76 6f 69 64 29 7b 0a 20 20 69  exEnd(void){.  i
904d0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
904e0 4b 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  K;.  if( sqlite3
904f0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
90500 65 78 2e 78 4d 75 74 65 78 45 6e 64 20 29 7b 0a  ex.xMutexEnd ){.
90510 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
90520 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
90530 65 78 2e 78 4d 75 74 65 78 45 6e 64 28 29 3b 0a  ex.xMutexEnd();.
90540 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
90550 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 72 69 65  .}../*.** Retrie
90560 76 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ve a pointer to 
90570 61 20 73 74 61 74 69 63 20 6d 75 74 65 78 20 6f  a static mutex o
90580 72 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  r allocate a new
90590 20 64 79 6e 61 6d 69 63 20 6f 6e 65 2e 0a 2a 2f   dynamic one..*/
905a0 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
905b0 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74  te3_mutex *sqlit
905c0 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 69  e3_mutex_alloc(i
905d0 6e 74 20 69 64 29 7b 0a 23 69 66 6e 64 65 66 20  nt id){.#ifndef 
905e0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
905f0 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74  INIT.  if( sqlit
90600 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
90610 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64  ) return 0;.#end
90620 69 66 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  if.  return sqli
90630 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
90640 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f  mutex.xMutexAllo
90650 63 28 69 64 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45  c(id);.}..SQLITE
90660 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33  _PRIVATE sqlite3
90670 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 4d  _mutex *sqlite3M
90680 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64  utexAlloc(int id
90690 29 7b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65  ){.  if( !sqlite
906a0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43  3GlobalConfig.bC
906b0 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20  oreMutex ){.    
906c0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
906d0 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c  return sqlite3Gl
906e0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78  obalConfig.mutex
906f0 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64 29  .xMutexAlloc(id)
90700 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20  ;.}../*.** Free 
90710 61 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 2e  a dynamic mutex.
90720 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
90730 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  oid sqlite3_mute
90740 78 5f 66 72 65 65 28 73 71 6c 69 74 65 33 5f 6d  x_free(sqlite3_m
90750 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20  utex *p){.  if( 
90760 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  p ){.    sqlite3
90770 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
90780 65 78 2e 78 4d 75 74 65 78 46 72 65 65 28 70 29  ex.xMutexFree(p)
90790 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f  ;.  }.}../*.** O
907a0 62 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78 20  btain the mutex 
907b0 70 2e 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72  p. If some other
907c0 20 74 68 72 65 61 64 20 61 6c 72 65 61 64 79 20   thread already 
907d0 68 61 73 20 74 68 65 20 6d 75 74 65 78 2c 20 62  has the mutex, b
907e0 6c 6f 63 6b 0a 2a 2a 20 75 6e 74 69 6c 20 69 74  lock.** until it
907f0 20 63 61 6e 20 62 65 20 6f 62 74 61 69 6e 65 64   can be obtained
90800 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
90810 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74  void sqlite3_mut
90820 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33  ex_enter(sqlite3
90830 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66  _mutex *p){.  if
90840 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ( p ){.    sqlit
90850 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
90860 75 74 65 78 2e 78 4d 75 74 65 78 45 6e 74 65 72  utex.xMutexEnter
90870 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  (p);.  }.}../*.*
90880 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 6d 75 74  * Obtain the mut
90890 65 78 20 70 2e 20 49 66 20 73 75 63 63 65 73 73  ex p. If success
908a0 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  ful, return SQLI
908b0 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65  TE_OK. Otherwise
908c0 2c 20 69 66 20 61 6e 6f 74 68 65 72 0a 2a 2a 20  , if another.** 
908d0 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68 65  thread holds the
908e0 20 6d 75 74 65 78 20 61 6e 64 20 69 74 20 63 61   mutex and it ca
908f0 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64  nnot be obtained
90900 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
90910 42 55 53 59 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  BUSY..*/.SQLITE_
90920 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
90930 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74 65  mutex_try(sqlite
90940 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69  3_mutex *p){.  i
90950 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
90960 4b 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  K;.  if( p ){.  
90970 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
90980 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
90990 65 78 2e 78 4d 75 74 65 78 54 72 79 28 70 29 3b  ex.xMutexTry(p);
909a0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
909b0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  ;.}../*.** The s
909c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
909d0 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69  ve() routine exi
909e0 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20  ts a mutex that 
909f0 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a  was previously.*
90a00 2a 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65  * entered by the
90a10 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54   same thread.  T
90a20 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75  he behavior is u
90a30 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20  ndefined if the 
90a40 6d 75 74 65 78 20 0a 2a 2a 20 69 73 20 6e 6f 74  mutex .** is not
90a50 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72   currently enter
90a60 65 64 2e 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f  ed. If a NULL po
90a70 69 6e 74 65 72 20 69 73 20 70 61 73 73 65 64 20  inter is passed 
90a80 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 0a 2a  as an argument.*
90a90 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * this function 
90aa0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53  is a no-op..*/.S
90ab0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
90ac0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
90ad0 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ve(sqlite3_mutex
90ae0 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b   *p){.  if( p ){
90af0 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
90b00 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
90b10 4d 75 74 65 78 4c 65 61 76 65 28 70 29 3b 0a 20  MutexLeave(p);. 
90b20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44   }.}..#ifndef ND
90b30 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  EBUG./*.** The s
90b40 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
90b50 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  d() and sqlite3_
90b60 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20  mutex_notheld() 
90b70 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69  routine are.** i
90b80 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20  ntended for use 
90b90 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20  inside assert() 
90ba0 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53  statements..*/.S
90bb0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
90bc0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
90bd0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
90be0 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d  p){.  return p==
90bf0 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62  0 || sqlite3Glob
90c00 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
90c10 4d 75 74 65 78 48 65 6c 64 28 70 29 3b 0a 7d 0a  MutexHeld(p);.}.
90c20 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
90c30 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
90c40 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74  held(sqlite3_mut
90c50 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  ex *p){.  return
90c60 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33   p==0 || sqlite3
90c70 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
90c80 65 78 2e 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64  ex.xMutexNotheld
90c90 28 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  (p);.}.#endif..#
90ca0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
90cb0 4f 4d 49 54 5f 4d 55 54 45 58 20 2a 2f 0a 0a 2f  OMIT_MUTEX */../
90cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
90cd0 6e 64 20 6f 66 20 6d 75 74 65 78 2e 63 20 2a 2a  nd of mutex.c **
90ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
90d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
90d20 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f  egin file mutex_
90d30 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  noop.c *********
90d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
90d60 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f 62 65  *.** 2008 Octobe
90d70 72 20 30 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 07.**.** The a
90d80 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
90d90 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
90da0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
90db0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
90dc0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
90dd0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
90de0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
90df0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
90e00 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
90e10 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
90e20 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
90e30 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
90e40 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
90e50 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
90e60 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
90e70 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
90e80 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
90e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90ed0 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
90ee0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20   contains the C 
90ef0 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69  functions that i
90f00 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73  mplement mutexes
90f10 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70  ..**.** This imp
90f20 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 20 74  lementation in t
90f30 68 69 73 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f  his file does no
90f40 74 20 70 72 6f 76 69 64 65 20 61 6e 79 20 6d 75  t provide any mu
90f50 74 75 61 6c 0a 2a 2a 20 65 78 63 6c 75 73 69 6f  tual.** exclusio
90f60 6e 20 61 6e 64 20 69 73 20 74 68 75 73 20 73 75  n and is thus su
90f70 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 6f  itable for use o
90f80 6e 6c 79 20 69 6e 20 61 70 70 6c 69 63 61 74 69  nly in applicati
90f90 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 75 73 65 20  ons.** that use 
90fa0 53 51 4c 69 74 65 20 69 6e 20 61 20 73 69 6e 67  SQLite in a sing
90fb0 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20  le thread.  The 
90fc0 72 6f 75 74 69 6e 65 73 20 64 65 66 69 6e 65 64  routines defined
90fd0 0a 2a 2a 20 68 65 72 65 20 61 72 65 20 70 6c 61  .** here are pla
90fe0 63 65 2d 68 6f 6c 64 65 72 73 2e 20 20 41 70 70  ce-holders.  App
90ff0 6c 69 63 61 74 69 6f 6e 73 20 63 61 6e 20 73 75  lications can su
91000 62 73 74 69 74 75 74 65 20 77 6f 72 6b 69 6e 67  bstitute working
91010 0a 2a 2a 20 6d 75 74 65 78 20 72 6f 75 74 69 6e  .** mutex routin
91020 65 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65  es at start-time
91030 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 0a 2a 2a   using the.**.**
91040 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e       sqlite3_con
91050 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49  fig(SQLITE_CONFI
91060 47 5f 4d 55 54 45 58 2c 2e 2e 2e 29 0a 2a 2a 0a  G_MUTEX,...).**.
91070 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a  ** interface..**
91080 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 64 20  .** If compiled 
91090 77 69 74 68 20 53 51 4c 49 54 45 5f 44 45 42 55  with SQLITE_DEBU
910a0 47 2c 20 74 68 65 6e 20 61 64 64 69 74 69 6f 6e  G, then addition
910b0 61 6c 20 6c 6f 67 69 63 20 69 73 20 69 6e 73 65  al logic is inse
910c0 72 74 65 64 0a 2a 2a 20 74 68 61 74 20 64 6f 65  rted.** that doe
910d0 73 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67  s error checking
910e0 20 6f 6e 20 6d 75 74 65 78 65 73 20 74 6f 20 6d   on mutexes to m
910f0 61 6b 65 20 73 75 72 65 20 74 68 65 79 20 61 72  ake sure they ar
91100 65 20 62 65 69 6e 67 0a 2a 2a 20 63 61 6c 6c 65  e being.** calle
91110 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a  d correctly..**.
91120 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f 6e 6f  ** $Id: mutex_no
91130 6f 70 2e 63 2c 76 20 31 2e 33 20 32 30 30 38 2f  op.c,v 1.3 2008/
91140 31 32 2f 30 35 20 31 37 3a 31 37 3a 30 38 20 64  12/05 17:17:08 d
91150 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 23 69  rh Exp $.*/...#i
91160 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
91170 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20  _MUTEX_NOOP) && 
91180 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
91190 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 53 74 75  DEBUG)./*.** Stu
911a0 62 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 61  b routines for a
911b0 6c 6c 20 6d 75 74 65 78 20 6d 65 74 68 6f 64 73  ll mutex methods
911c0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
911d0 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20 6e 6f  tines provide no
911e0 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f   mutual exclusio
911f0 6e 20 6f 72 20 65 72 72 6f 72 20 63 68 65 63 6b  n or error check
91200 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ing..*/.static i
91210 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 48 65 6c 64  nt noopMutexHeld
91220 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
91230 70 29 7b 20 72 65 74 75 72 6e 20 31 3b 20 7d 0a  p){ return 1; }.
91240 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d  static int noopM
91250 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69  utexNotheld(sqli
91260 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72  te3_mutex *p){ r
91270 65 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61 74 69  eturn 1; }.stati
91280 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 49  c int noopMutexI
91290 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72  nit(void){ retur
912a0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73  n SQLITE_OK; }.s
912b0 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75  tatic int noopMu
912c0 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65  texEnd(void){ re
912d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  turn SQLITE_OK; 
912e0 7d 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33  }.static sqlite3
912f0 5f 6d 75 74 65 78 20 2a 6e 6f 6f 70 4d 75 74 65  _mutex *noopMute
91300 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 20  xAlloc(int id){ 
91310 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f  return (sqlite3_
91320 6d 75 74 65 78 2a 29 38 3b 20 7d 0a 73 74 61 74  mutex*)8; }.stat
91330 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65  ic void noopMute
91340 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75  xFree(sqlite3_mu
91350 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b  tex *p){ return;
91360 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e   }.static void n
91370 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72 28 73 71  oopMutexEnter(sq
91380 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
91390 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61 74 69   return; }.stati
913a0 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 54  c int noopMutexT
913b0 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ry(sqlite3_mutex
913c0 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 53 51 4c   *p){ return SQL
913d0 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63  ITE_OK; }.static
913e0 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78 4c   void noopMutexL
913f0 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  eave(sqlite3_mut
91400 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20  ex *p){ return; 
91410 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  }..SQLITE_PRIVAT
91420 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  E sqlite3_mutex_
91430 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33  methods *sqlite3
91440 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69  DefaultMutex(voi
91450 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c  d){.  static sql
91460 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
91470 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20  ds sMutex = {.  
91480 20 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 74 2c    noopMutexInit,
91490 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 45 6e  .    noopMutexEn
914a0 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78  d,.    noopMutex
914b0 41 6c 6c 6f 63 2c 0a 20 20 20 20 6e 6f 6f 70 4d  Alloc,.    noopM
914c0 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6e 6f  utexFree,.    no
914d0 6f 70 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20  opMutexEnter,.  
914e0 20 20 6e 6f 6f 70 4d 75 74 65 78 54 72 79 2c 0a    noopMutexTry,.
914f0 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4c 65 61      noopMutexLea
91500 76 65 2c 0a 0a 20 20 20 20 6e 6f 6f 70 4d 75 74  ve,..    noopMut
91510 65 78 48 65 6c 64 2c 0a 20 20 20 20 6e 6f 6f 70  exHeld,.    noop
91520 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20 20 7d  MutexNotheld.  }
91530 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75  ;..  return &sMu
91540 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  tex;.}.#endif /*
91550 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
91560 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 21  MUTEX_NOOP) && !
91570 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
91580 45 42 55 47 29 20 2a 2f 0a 0a 23 69 66 20 64 65  EBUG) */..#if de
91590 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54  fined(SQLITE_MUT
915a0 45 58 5f 4e 4f 4f 50 29 20 26 26 20 64 65 66 69  EX_NOOP) && defi
915b0 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
915c0 29 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20  )./*.** In this 
915d0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20  implementation, 
915e0 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 69  error checking i
915f0 73 20 70 72 6f 76 69 64 65 64 20 66 6f 72 20 74  s provided for t
91600 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 65  esting.** and de
91610 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73 65 73  bugging purposes
91620 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 20 73  .  The mutexes s
91630 74 69 6c 6c 20 64 6f 20 6e 6f 74 20 70 72 6f 76  till do not prov
91640 69 64 65 20 61 6e 79 0a 2a 2a 20 6d 75 74 75 61  ide any.** mutua
91650 6c 20 65 78 63 6c 75 73 69 6f 6e 2e 0a 2a 2f 0a  l exclusion..*/.
91660 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78  ./*.** The mutex
91670 20 6f 62 6a 65 63 74 0a 2a 2f 0a 73 74 72 75 63   object.*/.struc
91680 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  t sqlite3_mutex 
91690 7b 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20 20  {.  int id;     
916a0 2f 2a 20 54 68 65 20 6d 75 74 65 78 20 74 79 70  /* The mutex typ
916b0 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20  e */.  int cnt; 
916c0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
916d0 65 6e 74 72 69 65 73 20 77 69 74 68 6f 75 74 20  entries without 
916e0 61 20 6d 61 74 63 68 69 6e 67 20 6c 65 61 76 65  a matching leave
916f0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68   */.};../*.** Th
91700 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
91710 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74  held() and sqlit
91720 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64  e3_mutex_notheld
91730 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a  () routine are.*
91740 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75  * intended for u
91750 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74  se inside assert
91760 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  () statements..*
91770 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 62  /.static int deb
91780 75 67 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69  ugMutexHeld(sqli
91790 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
917a0 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20   return p==0 || 
917b0 70 2d 3e 63 6e 74 3e 30 3b 0a 7d 0a 73 74 61 74  p->cnt>0;.}.stat
917c0 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65  ic int debugMute
917d0 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33  xNotheld(sqlite3
917e0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65  _mutex *p){.  re
917f0 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e  turn p==0 || p->
91800 63 6e 74 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  cnt==0;.}../*.**
91810 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20   Initialize and 
91820 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  deinitialize the
91830 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d   mutex subsystem
91840 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
91850 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 28 76  debugMutexInit(v
91860 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c  oid){ return SQL
91870 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63  ITE_OK; }.static
91880 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 45   int debugMutexE
91890 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  nd(void){ return
918a0 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f   SQLITE_OK; }../
918b0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
918c0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72  _mutex_alloc() r
918d0 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73  outine allocates
918e0 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20   a new.** mutex 
918f0 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f  and returns a po
91900 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66  inter to it.  If
91910 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c   it returns NULL
91920 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74  .** that means t
91930 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c  hat a mutex coul
91940 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74  d not be allocat
91950 65 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 73  ed. .*/.static s
91960 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 64 65  qlite3_mutex *de
91970 62 75 67 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e  bugMutexAlloc(in
91980 74 20 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  t id){.  static 
91990 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 61 53  sqlite3_mutex aS
919a0 74 61 74 69 63 5b 36 5d 3b 0a 20 20 73 71 6c 69  tatic[6];.  sqli
919b0 74 65 33 5f 6d 75 74 65 78 20 2a 70 4e 65 77 20  te3_mutex *pNew 
919c0 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 20 69  = 0;.  switch( i
919d0 64 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  d ){.    case SQ
919e0 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a  LITE_MUTEX_FAST:
919f0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
91a00 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
91a10 3a 20 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d  : {.      pNew =
91a20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 73   sqlite3Malloc(s
91a30 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20  izeof(*pNew));. 
91a40 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b       if( pNew ){
91a50 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69  .        pNew->i
91a60 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 20 20  d = id;.        
91a70 70 4e 65 77 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20  pNew->cnt = 0;. 
91a80 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
91a90 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65  ak;.    }.    de
91aa0 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61  fault: {.      a
91ab0 73 73 65 72 74 28 20 69 64 2d 32 20 3e 3d 20 30  ssert( id-2 >= 0
91ac0 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
91ad0 28 20 69 64 2d 32 20 3c 20 28 69 6e 74 29 28 73  ( id-2 < (int)(s
91ae0 69 7a 65 6f 66 28 61 53 74 61 74 69 63 29 2f 73  izeof(aStatic)/s
91af0 69 7a 65 6f 66 28 61 53 74 61 74 69 63 5b 30 5d  izeof(aStatic[0]
91b00 29 29 20 29 3b 0a 20 20 20 20 20 20 70 4e 65 77  )) );.      pNew
91b10 20 3d 20 26 61 53 74 61 74 69 63 5b 69 64 2d 32   = &aStatic[id-2
91b20 5d 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 69  ];.      pNew->i
91b30 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 62 72  d = id;.      br
91b40 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
91b50 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a   return pNew;.}.
91b60 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
91b70 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20  ine deallocates 
91b80 61 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c  a previously all
91b90 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2f  ocated mutex..*/
91ba0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62  .static void deb
91bb0 75 67 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69  ugMutexFree(sqli
91bc0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
91bd0 20 61 73 73 65 72 74 28 20 70 2d 3e 63 6e 74 3d   assert( p->cnt=
91be0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
91bf0 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
91c00 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69  TEX_FAST || p->i
91c10 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
91c20 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 73  RECURSIVE );.  s
91c30 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
91c40 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  }../*.** The sql
91c50 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
91c60 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d  () and sqlite3_m
91c70 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69  utex_try() routi
91c80 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74  nes attempt.** t
91c90 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e  o enter a mutex.
91ca0 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72    If another thr
91cb0 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77  ead is already w
91cc0 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c  ithin the mutex,
91cd0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  .** sqlite3_mute
91ce0 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62  x_enter() will b
91cf0 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33  lock and sqlite3
91d00 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c  _mutex_try() wil
91d10 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49  l return.** SQLI
91d20 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71  TE_BUSY.  The sq
91d30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
91d40 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  ) interface retu
91d50 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a  rns SQLITE_OK.**
91d60 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c   upon successful
91d70 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73   entry.  Mutexes
91d80 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 53   created using S
91d90 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
91da0 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20  RSIVE can.** be 
91db0 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65  entered multiple
91dc0 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 61   times by the sa
91dd0 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73  me thread.  In s
91de0 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a  uch cases the,.*
91df0 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  * mutex must be 
91e00 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20  exited an equal 
91e10 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20  number of times 
91e20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74  before another t
91e30 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74  hread.** can ent
91e40 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65  er.  If the same
91e50 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f   thread tries to
91e60 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72   enter any other
91e70 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a   kind of mutex.*
91e80 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  * more than once
91e90 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69  , the behavior i
91ea0 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a  s undefined..*/.
91eb0 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62 75  static void debu
91ec0 67 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69  gMutexEnter(sqli
91ed0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
91ee0 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d   assert( p->id==
91ef0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
91f00 55 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d  URSIVE || debugM
91f10 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29  utexNotheld(p) )
91f20 3b 0a 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 7d 0a  ;.  p->cnt++;.}.
91f30 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 75 67  static int debug
91f40 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65 33  MutexTry(sqlite3
91f50 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73  _mutex *p){.  as
91f60 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c  sert( p->id==SQL
91f70 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
91f80 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65  IVE || debugMute
91f90 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20  xNotheld(p) );. 
91fa0 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 72 65 74   p->cnt++;.  ret
91fb0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
91fc0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ../*.** The sqli
91fd0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
91fe0 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20  ) routine exits 
91ff0 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73  a mutex that was
92000 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65  .** previously e
92010 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61  ntered by the sa
92020 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20  me thread.  The 
92030 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75  behavior.** is u
92040 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20  ndefined if the 
92050 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72  mutex is not cur
92060 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f  rently entered o
92070 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72  r.** is not curr
92080 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e  ently allocated.
92090 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65    SQLite will ne
920a0 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a  ver do either..*
920b0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65  /.static void de
920c0 62 75 67 4d 75 74 65 78 4c 65 61 76 65 28 73 71  bugMutexLeave(sq
920d0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
920e0 0a 20 20 61 73 73 65 72 74 28 20 64 65 62 75 67  .  assert( debug
920f0 4d 75 74 65 78 48 65 6c 64 28 70 29 20 29 3b 0a  MutexHeld(p) );.
92100 20 20 70 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 61 73    p->cnt--;.  as
92110 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c  sert( p->id==SQL
92120 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
92130 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65  IVE || debugMute
92140 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 7d  xNotheld(p) );.}
92150 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
92160 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
92170 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44  ethods *sqlite3D
92180 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64  efaultMutex(void
92190 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69  ){.  static sqli
921a0 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64  te3_mutex_method
921b0 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20  s sMutex = {.   
921c0 20 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 2c   debugMutexInit,
921d0 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 45  .    debugMutexE
921e0 6e 64 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74  nd,.    debugMut
921f0 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 64 65 62  exAlloc,.    deb
92200 75 67 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20  ugMutexFree,.   
92210 20 64 65 62 75 67 4d 75 74 65 78 45 6e 74 65 72   debugMutexEnter
92220 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78  ,.    debugMutex
92230 54 72 79 2c 0a 20 20 20 20 64 65 62 75 67 4d 75  Try,.    debugMu
92240 74 65 78 4c 65 61 76 65 2c 0a 0a 20 20 20 20 64  texLeave,..    d
92250 65 62 75 67 4d 75 74 65 78 48 65 6c 64 2c 0a 20  ebugMutexHeld,. 
92260 20 20 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74     debugMutexNot
92270 68 65 6c 64 0a 20 20 7d 3b 0a 0a 20 20 72 65 74  held.  };..  ret
92280 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23  urn &sMutex;.}.#
92290 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64  endif /* defined
922a0 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f  (SQLITE_MUTEX_NO
922b0 4f 50 29 20 26 26 20 64 65 66 69 6e 65 64 28 53  OP) && defined(S
922c0 51 4c 49 54 45 5f 44 45 42 55 47 29 20 2a 2f 0a  QLITE_DEBUG) */.
922d0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
922e0 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 6e 6f   End of mutex_no
922f0 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  op.c ***********
92300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
92320 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
92330 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65   Begin file mute
92340 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  x_os2.c ********
92350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
92370 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75  ./*.** 2007 Augu
92380 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20  st 28.**.** The 
92390 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
923a0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
923b0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
923c0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
923d0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
923e0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
923f0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
92400 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
92410 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
92420 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
92430 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
92440 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
92450 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
92460 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
92470 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
92480 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
92490 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
924a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
924b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
924c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
924d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
924e0 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
924f0 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43  e contains the C
92500 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20   functions that 
92510 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65  implement mutexe
92520 73 20 66 6f 72 20 4f 53 2f 32 0a 2a 2a 0a 2a 2a  s for OS/2.**.**
92530 20 24 49 64 3a 20 6d 75 74 65 78 5f 6f 73 32 2e   $Id: mutex_os2.
92540 63 2c 76 20 31 2e 31 31 20 32 30 30 38 2f 31 31  c,v 1.11 2008/11
92550 2f 32 32 20 31 39 3a 35 30 3a 35 34 20 70 77 65  /22 19:50:54 pwe
92560 69 6c 62 61 63 68 65 72 20 45 78 70 20 24 0a 2a  ilbacher Exp $.*
92570 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64  /../*.** The cod
92580 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69  e in this file i
92590 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 53  s only used if S
925a0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 20  QLITE_MUTEX_OS2 
925b0 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 53  is defined..** S
925c0 65 65 20 74 68 65 20 6d 75 74 65 78 2e 68 20 66  ee the mutex.h f
925d0 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73 2e  ile for details.
925e0 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
925f0 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 0a 2f 2a 2a  E_MUTEX_OS2../**
92600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92610 2a 2a 2a 2a 20 4f 53 2f 32 20 4d 75 74 65 78 20  **** OS/2 Mutex 
92620 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  Implementation *
92630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92640 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
92650 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
92660 6f 66 20 6d 75 74 65 78 65 73 20 69 73 20 62 75  of mutexes is bu
92670 69 6c 74 20 75 73 69 6e 67 20 74 68 65 20 4f 53  ilt using the OS
92680 2f 32 20 41 50 49 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  /2 API..*/../*.*
92690 2a 20 54 68 65 20 6d 75 74 65 78 20 6f 62 6a 65  * The mutex obje
926a0 63 74 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72  ct.** Each recur
926b0 73 69 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e  sive mutex is an
926c0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
926d0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
926e0 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ture..*/.struct 
926f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a  sqlite3_mutex {.
92700 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 20 20 20    HMTX mutex;   
92710 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e      /* Mutex con
92720 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f 63  trolling the loc
92730 6b 20 2a 2f 0a 20 20 69 6e 74 20 20 69 64 3b 20  k */.  int  id; 
92740 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65           /* Mute
92750 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20  x type */.  int 
92760 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 2f 2a   nRef;        /*
92770 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72   Number of refer
92780 65 6e 63 65 73 20 2a 2f 0a 20 20 54 49 44 20 20  ences */.  TID  
92790 6f 77 6e 65 72 3b 20 20 20 20 20 20 20 2f 2a 20  owner;       /* 
927a0 54 68 72 65 61 64 20 68 6f 6c 64 69 6e 67 20 74  Thread holding t
927b0 68 69 73 20 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a  his mutex */.};.
927c0 0a 23 64 65 66 69 6e 65 20 4f 53 32 5f 4d 55 54  .#define OS2_MUT
927d0 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 20  EX_INITIALIZER  
927e0 20 30 2c 30 2c 30 2c 30 0a 0a 2f 2a 0a 2a 2a 20   0,0,0,0../*.** 
927f0 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64  Initialize and d
92800 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20  einitialize the 
92810 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e  mutex subsystem.
92820 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
92830 73 32 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64  s2MutexInit(void
92840 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ){ return SQLITE
92850 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e  _OK; }.static in
92860 74 20 6f 73 32 4d 75 74 65 78 45 6e 64 28 76 6f  t os2MutexEnd(vo
92870 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49  id){ return SQLI
92880 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK; }../*.** 
92890 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
928a0 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e  x_alloc() routin
928b0 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65  e allocates a ne
928c0 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72  w.** mutex and r
928d0 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
928e0 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72   to it.  If it r
928f0 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74  eturns NULL.** t
92900 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61  hat means that a
92910 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74   mutex could not
92920 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 0a   be allocated. .
92930 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75  ** SQLite will u
92940 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20  nwind its stack 
92950 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72  and return an er
92960 72 6f 72 2e 20 20 54 68 65 20 61 72 67 75 6d 65  ror.  The argume
92970 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33  nt.** to sqlite3
92980 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69  _mutex_alloc() i
92990 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69  s one of these i
929a0 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73  nteger constants
929b0 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  :.**.** <ul>.** 
929c0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
929d0 45 58 5f 46 41 53 54 20 20 20 20 20 20 20 20 20  EX_FAST         
929e0 20 20 20 20 20 20 30 0a 2a 2a 20 3c 6c 69 3e 20        0.** <li> 
929f0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
92a00 43 55 52 53 49 56 45 20 20 20 20 20 20 20 20 20  CURSIVE         
92a10 20 31 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49   1.** <li>  SQLI
92a20 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
92a30 4d 41 53 54 45 52 20 20 20 20 20 20 32 0a 2a 2a  MASTER      2.**
92a40 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
92a50 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 20 20  TEX_STATIC_MEM  
92a60 20 20 20 20 20 20 20 33 0a 2a 2a 20 3c 6c 69 3e         3.** <li>
92a70 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
92a80 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20 20 20  TATIC_PRNG      
92a90 20 20 34 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a    4.** </ul>.**.
92aa0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 77 6f  ** The first two
92ab0 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65   constants cause
92ac0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
92ad0 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65  lloc() to create
92ae0 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e  .** a new mutex.
92af0 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20    The new mutex 
92b00 69 73 20 72 65 63 75 72 73 69 76 65 20 77 68 65  is recursive whe
92b10 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  n SQLITE_MUTEX_R
92b20 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 75  ECURSIVE.** is u
92b30 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65  sed but not nece
92b40 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20  ssarily so when 
92b50 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
92b60 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68  T is used..** Th
92b70 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e  e mutex implemen
92b80 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20  tation does not 
92b90 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64  need to make a d
92ba0 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65  istinction.** be
92bb0 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54  tween SQLITE_MUT
92bc0 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64  EX_RECURSIVE and
92bd0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
92be0 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a  ST if it does.**
92bf0 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 42   not want to.  B
92c00 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f  ut SQLite will o
92c10 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 65  nly request a re
92c20 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e  cursive mutex in
92c30 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20  .** cases where 
92c40 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20  it really needs 
92c50 6f 6e 65 2e 20 20 49 66 20 61 20 66 61 73 74 65  one.  If a faste
92c60 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20  r non-recursive 
92c70 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65  mutex.** impleme
92c80 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c  ntation is avail
92c90 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74  able on the host
92ca0 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d   platform, the m
92cb0 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a 2a  utex subsystem.*
92cc0 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 73  * might return s
92cd0 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20 72  uch a mutex in r
92ce0 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54  esponse to SQLIT
92cf0 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a  E_MUTEX_FAST..**
92d00 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72 20 61 6c  .** The other al
92d10 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73  lowed parameters
92d20 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   to sqlite3_mute
92d30 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72  x_alloc() each r
92d40 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74  eturn.** a point
92d50 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70  er to a static p
92d60 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78  reexisting mutex
92d70 2e 20 20 54 68 72 65 65 20 73 74 61 74 69 63 20  .  Three static 
92d80 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 75  mutexes are.** u
92d90 73 65 64 20 62 79 20 74 68 65 20 63 75 72 72 65  sed by the curre
92da0 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51  nt version of SQ
92db0 4c 69 74 65 2e 20 20 46 75 74 75 72 65 20 76 65  Lite.  Future ve
92dc0 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65  rsions of SQLite
92dd0 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 64 64 69  .** may add addi
92de0 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75  tional static mu
92df0 74 65 78 65 73 2e 20 20 53 74 61 74 69 63 20 6d  texes.  Static m
92e00 75 74 65 78 65 73 20 61 72 65 20 66 6f 72 20 69  utexes are for i
92e10 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62  nternal.** use b
92e20 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20  y SQLite only.  
92e30 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61  Applications tha
92e40 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 75 74  t use SQLite mut
92e50 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75  exes should.** u
92e60 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61  se only the dyna
92e70 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65 74 75  mic mutexes retu
92e80 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d  rned by SQLITE_M
92e90 55 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20  UTEX_FAST or.** 
92ea0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
92eb0 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  URSIVE..**.** No
92ec0 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f  te that if one o
92ed0 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75  f the dynamic mu
92ee0 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 28  tex parameters (
92ef0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
92f00 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d  T.** or SQLITE_M
92f10 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 20  UTEX_RECURSIVE) 
92f20 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c  is used then sql
92f30 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
92f40 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20  ().** returns a 
92f50 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20  different mutex 
92f60 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20  on every call.  
92f70 42 75 74 20 66 6f 72 20 74 68 65 20 73 74 61 74  But for the stat
92f80 69 63 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65  ic.** mutex type
92f90 73 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65  s, the same mute
92fa0 78 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e  x is returned on
92fb0 20 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74   every call that
92fc0 20 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65   has.** the same
92fd0 20 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f   type number..*/
92fe0 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f  .static sqlite3_
92ff0 6d 75 74 65 78 20 2a 6f 73 32 4d 75 74 65 78 41  mutex *os2MutexA
93000 6c 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b  lloc(int iType){
93010 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
93020 20 2a 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20 73 77   *p = NULL;.  sw
93030 69 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20  itch( iType ){. 
93040 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d     case SQLITE_M
93050 55 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63  UTEX_FAST:.    c
93060 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ase SQLITE_MUTEX
93070 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20  _RECURSIVE: {.  
93080 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d      p = sqlite3M
93090 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f  allocZero( sizeo
930a0 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69  f(*p) );.      i
930b0 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20  f( p ){.        
930c0 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20  p->id = iType;. 
930d0 20 20 20 20 20 20 20 69 66 28 20 44 6f 73 43 72         if( DosCr
930e0 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20 30 2c  eateMutexSem( 0,
930f0 20 26 70 2d 3e 6d 75 74 65 78 2c 20 30 2c 20 46   &p->mutex, 0, F
93100 41 4c 53 45 20 29 20 21 3d 20 4e 4f 5f 45 52 52  ALSE ) != NO_ERR
93110 4f 52 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OR ){.          
93120 73 71 6c 69 74 65 33 5f 66 72 65 65 28 20 70 20  sqlite3_free( p 
93130 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 20 3d  );.          p =
93140 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 7d   NULL;.        }
93150 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
93160 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
93170 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
93180 20 73 74 61 74 69 63 20 76 6f 6c 61 74 69 6c 65   static volatile
93190 20 69 6e 74 20 69 73 49 6e 69 74 20 3d 20 30 3b   int isInit = 0;
931a0 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 73 71  .      static sq
931b0 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 74 61 74  lite3_mutex stat
931c0 69 63 4d 75 74 65 78 65 73 5b 5d 20 3d 20 7b 0a  icMutexes[] = {.
931d0 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55          { OS2_MU
931e0 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c  TEX_INITIALIZER,
931f0 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53   },.        { OS
93200 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  2_MUTEX_INITIALI
93210 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20  ZER, },.        
93220 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54  { OS2_MUTEX_INIT
93230 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20  IALIZER, },.    
93240 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f      { OS2_MUTEX_
93250 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a  INITIALIZER, },.
93260 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55          { OS2_MU
93270 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c  TEX_INITIALIZER,
93280 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53   },.        { OS
93290 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  2_MUTEX_INITIALI
932a0 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 7d 3b  ZER, },.      };
932b0 0a 20 20 20 20 20 20 69 66 20 28 20 21 69 73 49  .      if ( !isI
932c0 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 41  nit ){.        A
932d0 50 49 52 45 54 20 72 63 3b 0a 20 20 20 20 20 20  PIRET rc;.      
932e0 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 20    PTIB ptib;.   
932f0 20 20 20 20 20 50 50 49 42 20 70 70 69 62 3b 0a       PPIB ppib;.
93300 20 20 20 20 20 20 20 20 48 4d 54 58 20 6d 75 74          HMTX mut
93310 65 78 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72  ex;.        char
93320 20 6e 61 6d 65 5b 33 32 5d 3b 0a 20 20 20 20 20   name[32];.     
93330 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f     DosGetInfoBlo
93340 63 6b 73 28 20 26 70 74 69 62 2c 20 26 70 70 69  cks( &ptib, &ppi
93350 62 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  b );.        sql
93360 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 73  ite3_snprintf( s
93370 69 7a 65 6f 66 28 6e 61 6d 65 29 2c 20 6e 61 6d  izeof(name), nam
93380 65 2c 20 22 5c 5c 53 45 4d 33 32 5c 5c 53 51 4c  e, "\\SEM32\\SQL
93390 49 54 45 25 30 34 78 22 2c 0a 20 20 20 20 20 20  ITE%04x",.      
933a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
933b0 20 20 20 20 70 70 69 62 2d 3e 70 69 62 5f 75 6c      ppib->pib_ul
933c0 70 69 64 20 29 3b 0a 20 20 20 20 20 20 20 20 77  pid );.        w
933d0 68 69 6c 65 28 20 21 69 73 49 6e 69 74 20 29 7b  hile( !isInit ){
933e0 0a 20 20 20 20 20 20 20 20 20 20 6d 75 74 65 78  .          mutex
933f0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
93400 72 63 20 3d 20 44 6f 73 43 72 65 61 74 65 4d 75  rc = DosCreateMu
93410 74 65 78 53 65 6d 28 20 6e 61 6d 65 2c 20 26 6d  texSem( name, &m
93420 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 29 3b  utex, 0, FALSE);
93430 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72  .          if( r
93440 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b  c == NO_ERROR ){
93450 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 6e 73  .            uns
93460 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20 20  igned int i;.   
93470 20 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73           if( !is
93480 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Init ){.        
93490 20 20 20 20 20 20 66 6f 72 28 20 69 20 3d 20 30        for( i = 0
934a0 3b 20 69 20 3c 20 73 69 7a 65 6f 66 28 73 74 61  ; i < sizeof(sta
934b0 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69 7a 65  ticMutexes)/size
934c0 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73  of(staticMutexes
934d0 5b 30 5d 29 3b 20 69 2b 2b 20 29 7b 0a 20 20 20  [0]); i++ ){.   
934e0 20 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73               Dos
934f0 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20  CreateMutexSem( 
93500 30 2c 20 26 73 74 61 74 69 63 4d 75 74 65 78 65  0, &staticMutexe
93510 73 5b 69 5d 2e 6d 75 74 65 78 2c 20 30 2c 20 46  s[i].mutex, 0, F
93520 41 4c 53 45 20 29 3b 0a 20 20 20 20 20 20 20 20  ALSE );.        
93530 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
93540 20 20 20 20 20 20 69 73 49 6e 69 74 20 3d 20 31        isInit = 1
93550 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
93560 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 43              DosC
93570 6c 6f 73 65 4d 75 74 65 78 53 65 6d 28 20 6d 75  loseMutexSem( mu
93580 74 65 78 20 29 3b 0a 20 20 20 20 20 20 20 20 20  tex );.         
93590 20 7d 65 6c 73 65 20 69 66 28 20 72 63 20 3d 3d   }else if( rc ==
935a0 20 45 52 52 4f 52 5f 44 55 50 4c 49 43 41 54 45   ERROR_DUPLICATE
935b0 5f 4e 41 4d 45 20 29 7b 0a 20 20 20 20 20 20 20  _NAME ){.       
935c0 20 20 20 20 20 44 6f 73 53 6c 65 65 70 28 20 31       DosSleep( 1
935d0 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65   );.          }e
935e0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
935f0 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20 20   return p;.     
93600 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
93610 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61  .      }.      a
93620 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3e  ssert( iType-2 >
93630 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  = 0 );.      ass
93640 65 72 74 28 20 69 54 79 70 65 2d 32 20 3c 20 73  ert( iType-2 < s
93650 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65  izeof(staticMute
93660 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 74  xes)/sizeof(stat
93670 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 20 29 3b  icMutexes[0]) );
93680 0a 20 20 20 20 20 20 70 20 3d 20 26 73 74 61 74  .      p = &stat
93690 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d  icMutexes[iType-
936a0 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20  2];.      p->id 
936b0 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62  = iType;.      b
936c0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
936d0 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a    return p;.}...
936e0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
936f0 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61  ne deallocates a
93700 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f   previously allo
93710 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2a 20  cated mutex..** 
93720 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75  SQLite is carefu
93730 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20  l to deallocate 
93740 65 76 65 72 79 20 6d 75 74 65 78 20 74 68 61 74  every mutex that
93750 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a   it allocates..*
93760 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73  /.static void os
93770 32 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74  2MutexFree(sqlit
93780 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
93790 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
937a0 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  n;.  assert( p->
937b0 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73  nRef==0 );.  ass
937c0 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  ert( p->id==SQLI
937d0 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c  TE_MUTEX_FAST ||
937e0 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
937f0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29  UTEX_RECURSIVE )
93800 3b 0a 20 20 44 6f 73 43 6c 6f 73 65 4d 75 74 65  ;.  DosCloseMute
93810 78 53 65 6d 28 20 70 2d 3e 6d 75 74 65 78 20 29  xSem( p->mutex )
93820 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
93830 28 20 70 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ( p );.}../*.** 
93840 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
93850 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71  x_enter() and sq
93860 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
93870 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d  ) routines attem
93880 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61  pt.** to enter a
93890 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74   mutex.  If anot
938a0 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c  her thread is al
938b0 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65  ready within the
938c0 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74   mutex,.** sqlit
938d0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
938e0 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20   will block and 
938f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
93900 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a  y() will return.
93910 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20  ** SQLITE_BUSY. 
93920 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
93930 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61  ex_try() interfa
93940 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  ce returns SQLIT
93950 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63  E_OK.** upon suc
93960 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20  cessful entry.  
93970 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20  Mutexes created 
93980 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54  using SQLITE_MUT
93990 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e  EX_RECURSIVE can
939a0 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d  .** be entered m
939b0 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79  ultiple times by
939c0 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
939d0 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73  .  In such cases
939e0 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d   the,.** mutex m
939f0 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e  ust be exited an
93a00 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66   equal number of
93a10 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e   times before an
93a20 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20  other thread.** 
93a30 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74  can enter.  If t
93a40 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74  he same thread t
93a50 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e  ries to enter an
93a60 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20  y other kind of 
93a70 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68  mutex.** more th
93a80 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68  an once, the beh
93a90 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e  avior is undefin
93aa0 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
93ab0 69 64 20 6f 73 32 4d 75 74 65 78 45 6e 74 65 72  id os2MutexEnter
93ac0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
93ad0 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20  p){.  TID tid;. 
93ae0 20 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a 20 20   PID holder1;.  
93af0 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b 0a 20  ULONG holder2;. 
93b00 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
93b10 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  rn;.  assert( p-
93b20 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
93b30 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 6f  X_RECURSIVE || o
93b40 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70  s2MutexNotheld(p
93b50 29 20 29 3b 0a 20 20 44 6f 73 52 65 71 75 65 73  ) );.  DosReques
93b60 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74  tMutexSem(p->mut
93b70 65 78 2c 20 53 45 4d 5f 49 4e 44 45 46 49 4e 49  ex, SEM_INDEFINI
93b80 54 45 5f 57 41 49 54 29 3b 0a 20 20 44 6f 73 51  TE_WAIT);.  DosQ
93b90 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e  ueryMutexSem(p->
93ba0 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 31 2c  mutex, &holder1,
93bb0 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 32 29   &tid, &holder2)
93bc0 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74  ;.  p->owner = t
93bd0 69 64 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b  id;.  p->nRef++;
93be0 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  .}.static int os
93bf0 32 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65  2MutexTry(sqlite
93c00 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69  3_mutex *p){.  i
93c10 6e 74 20 72 63 3b 0a 20 20 54 49 44 20 74 69 64  nt rc;.  TID tid
93c20 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 31 3b  ;.  PID holder1;
93c30 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32  .  ULONG holder2
93c40 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
93c50 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
93c60 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64  .  assert( p->id
93c70 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  ==SQLITE_MUTEX_R
93c80 45 43 55 52 53 49 56 45 20 7c 7c 20 6f 73 32 4d  ECURSIVE || os2M
93c90 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29  utexNotheld(p) )
93ca0 3b 0a 20 20 69 66 28 20 44 6f 73 52 65 71 75 65  ;.  if( DosReque
93cb0 73 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75  stMutexSem(p->mu
93cc0 74 65 78 2c 20 53 45 4d 5f 49 4d 4d 45 44 49 41  tex, SEM_IMMEDIA
93cd0 54 45 5f 52 45 54 55 52 4e 29 20 3d 3d 20 4e 4f  TE_RETURN) == NO
93ce0 5f 45 52 52 4f 52 29 20 7b 0a 20 20 20 20 44 6f  _ERROR) {.    Do
93cf0 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70  sQueryMutexSem(p
93d00 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72  ->mutex, &holder
93d10 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72  1, &tid, &holder
93d20 32 29 3b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72  2);.    p->owner
93d30 20 3d 20 74 69 64 3b 0a 20 20 20 20 70 2d 3e 6e   = tid;.    p->n
93d40 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20  Ref++;.    rc = 
93d50 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 20 65  SQLITE_OK;.  } e
93d60 6c 73 65 20 7b 0a 20 20 20 20 72 63 20 3d 20 53  lse {.    rc = S
93d70 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a  QLITE_BUSY;.  }.
93d80 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
93d90 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
93da0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29  e3_mutex_leave()
93db0 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61   routine exits a
93dc0 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a   mutex that was.
93dd0 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e  ** previously en
93de0 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d  tered by the sam
93df0 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62  e thread.  The b
93e00 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e  ehavior.** is un
93e10 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d  defined if the m
93e20 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72  utex is not curr
93e30 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72  ently entered or
93e40 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65  .** is not curre
93e50 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20  ntly allocated. 
93e60 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76   SQLite will nev
93e70 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f  er do either..*/
93e80 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32  .static void os2
93e90 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74  MutexLeave(sqlit
93ea0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
93eb0 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 68  TID tid;.  PID h
93ec0 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 20  older1;.  ULONG 
93ed0 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 20 70  holder2;.  if( p
93ee0 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
93ef0 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e  assert( p->nRef>
93f00 30 20 29 3b 0a 20 20 44 6f 73 51 75 65 72 79 4d  0 );.  DosQueryM
93f10 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78  utexSem(p->mutex
93f20 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69 64  , &holder1, &tid
93f30 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20 61  , &holder2);.  a
93f40 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 3d  ssert( p->owner=
93f50 3d 74 69 64 20 29 3b 0a 20 20 70 2d 3e 6e 52 65  =tid );.  p->nRe
93f60 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  f--;.  assert( p
93f70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e  ->nRef==0 || p->
93f80 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
93f90 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20  _RECURSIVE );.  
93fa0 44 6f 73 52 65 6c 65 61 73 65 4d 75 74 65 78 53  DosReleaseMutexS
93fb0 65 6d 28 70 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a  em(p->mutex);.}.
93fc0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
93fd0 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  EBUG./*.** The s
93fe0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
93ff0 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  d() and sqlite3_
94000 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20  mutex_notheld() 
94010 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69  routine are.** i
94020 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20  ntended for use 
94030 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20  inside assert() 
94040 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73  statements..*/.s
94050 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74  tatic int os2Mut
94060 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d  exHeld(sqlite3_m
94070 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20  utex *p){.  TID 
94080 74 69 64 3b 0a 20 20 50 49 44 20 70 69 64 3b 0a  tid;.  PID pid;.
94090 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b    ULONG ulCount;
940a0 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20  .  PTIB ptib;.  
940b0 69 66 28 20 70 21 3d 30 20 29 20 7b 0a 20 20 20  if( p!=0 ) {.   
940c0 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65   DosQueryMutexSe
940d0 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 70 69 64  m(p->mutex, &pid
940e0 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f 75 6e 74  , &tid, &ulCount
940f0 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  );.  } else {.  
94100 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63    DosGetInfoBloc
94110 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b  ks(&ptib, NULL);
94120 0a 20 20 20 20 74 69 64 20 3d 20 70 74 69 62 2d  .    tid = ptib-
94130 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32  >tib_ptib2->tib2
94140 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 20 72 65  _ultid;.  }.  re
94150 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 28 70 2d  turn p==0 || (p-
94160 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 2d 3e 6f  >nRef!=0 && p->o
94170 77 6e 65 72 3d 3d 74 69 64 29 3b 0a 7d 0a 73 74  wner==tid);.}.st
94180 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65  atic int os2Mute
94190 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33  xNotheld(sqlite3
941a0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49  _mutex *p){.  TI
941b0 44 20 74 69 64 3b 0a 20 20 50 49 44 20 70 69 64  D tid;.  PID pid
941c0 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e  ;.  ULONG ulCoun
941d0 74 3b 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a  t;.  PTIB ptib;.
941e0 20 20 69 66 28 20 70 21 3d 20 30 20 29 20 7b 0a    if( p!= 0 ) {.
941f0 20 20 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65      DosQueryMute
94200 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26  xSem(p->mutex, &
94210 70 69 64 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f  pid, &tid, &ulCo
94220 75 6e 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b  unt);.  } else {
94230 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42  .    DosGetInfoB
94240 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c  locks(&ptib, NUL
94250 4c 29 3b 0a 20 20 20 20 74 69 64 20 3d 20 70 74  L);.    tid = pt
94260 69 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74  ib->tib_ptib2->t
94270 69 62 32 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20  ib2_ultid;.  }. 
94280 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20   return p==0 || 
94290 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d  p->nRef==0 || p-
942a0 3e 6f 77 6e 65 72 21 3d 74 69 64 3b 0a 7d 0a 23  >owner!=tid;.}.#
942b0 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52  endif..SQLITE_PR
942c0 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75  IVATE sqlite3_mu
942d0 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c  tex_methods *sql
942e0 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78  ite3DefaultMutex
942f0 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63  (void){.  static
94300 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
94310 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20  ethods sMutex = 
94320 7b 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 49 6e  {.    os2MutexIn
94330 69 74 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78  it,.    os2Mutex
94340 45 6e 64 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65  End,.    os2Mute
94350 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6f 73 32 4d  xAlloc,.    os2M
94360 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6f 73  utexFree,.    os
94370 32 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20  2MutexEnter,.   
94380 20 6f 73 32 4d 75 74 65 78 54 72 79 2c 0a 20 20   os2MutexTry,.  
94390 20 20 6f 73 32 4d 75 74 65 78 4c 65 61 76 65 2c    os2MutexLeave,
943a0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
943b0 45 42 55 47 0a 20 20 20 20 6f 73 32 4d 75 74 65  EBUG.    os2Mute
943c0 78 48 65 6c 64 2c 0a 20 20 20 20 6f 73 32 4d 75  xHeld,.    os2Mu
943d0 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6e 64 69  texNotheld.#endi
943e0 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e  f.  };..  return
943f0 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64   &sMutex;.}.#end
94400 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54  if /* SQLITE_MUT
94410 45 58 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  EX_OS2 */../****
94420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
94430 66 20 6d 75 74 65 78 5f 6f 73 32 2e 63 20 2a 2a  f mutex_os2.c **
94440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
94470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
94480 20 66 69 6c 65 20 6d 75 74 65 78 5f 75 6e 69 78   file mutex_unix
94490 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
944a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
944b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
944c0 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38 0a   2007 August 28.
944d0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
944e0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
944f0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
94500 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
94510 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
94520 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
94530 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
94540 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
94550 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
94560 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
94570 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
94580 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
94590 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
945a0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
945b0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
945c0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
945d0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
945e0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
945f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
94630 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
94640 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74  ains the C funct
94650 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d  ions that implem
94660 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20  ent mutexes for 
94670 70 74 68 72 65 61 64 73 0a 2a 2a 0a 2a 2a 20 24  pthreads.**.** $
94680 49 64 3a 20 6d 75 74 65 78 5f 75 6e 69 78 2e 63  Id: mutex_unix.c
94690 2c 76 20 31 2e 31 36 20 32 30 30 38 2f 31 32 2f  ,v 1.16 2008/12/
946a0 30 38 20 31 38 3a 31 39 3a 31 38 20 64 72 68 20  08 18:19:18 drh 
946b0 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
946c0 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  The code in this
946d0 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73   file is only us
946e0 65 64 20 69 66 20 77 65 20 61 72 65 20 63 6f 6d  ed if we are com
946f0 70 69 6c 69 6e 67 20 74 68 72 65 61 64 73 61 66  piling threadsaf
94700 65 0a 2a 2a 20 75 6e 64 65 72 20 75 6e 69 78 20  e.** under unix 
94710 77 69 74 68 20 70 74 68 72 65 61 64 73 2e 0a 2a  with pthreads..*
94720 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74  *.** Note that t
94730 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  his implementati
94740 6f 6e 20 72 65 71 75 69 72 65 73 20 61 20 76 65  on requires a ve
94750 72 73 69 6f 6e 20 6f 66 20 70 74 68 72 65 61 64  rsion of pthread
94760 73 20 74 68 61 74 0a 2a 2a 20 73 75 70 70 6f 72  s that.** suppor
94770 74 73 20 72 65 63 75 72 73 69 76 65 20 6d 75 74  ts recursive mut
94780 65 78 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  exes..*/.#ifdef 
94790 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48  SQLITE_MUTEX_PTH
947a0 52 45 41 44 53 0a 0a 23 69 6e 63 6c 75 64 65 20  READS..#include 
947b0 3c 70 74 68 72 65 61 64 2e 68 3e 0a 0a 0a 2f 2a  <pthread.h>.../*
947c0 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72 73 69  .** Each recursi
947d0 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e 20 69  ve mutex is an i
947e0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
947f0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
94800 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71  re..*/.struct sq
94810 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20  lite3_mutex {.  
94820 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 20  pthread_mutex_t 
94830 6d 75 74 65 78 3b 20 20 20 20 20 2f 2a 20 4d 75  mutex;     /* Mu
94840 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20  tex controlling 
94850 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e  the lock */.  in
94860 74 20 69 64 3b 20 20 20 20 20 20 20 20 20 20 20  t id;           
94870 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65           /* Mute
94880 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20  x type */.  int 
94890 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20  nRef;           
948a0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
948b0 20 6f 66 20 65 6e 74 72 61 6e 63 65 73 20 2a 2f   of entrances */
948c0 0a 20 20 70 74 68 72 65 61 64 5f 74 20 6f 77 6e  .  pthread_t own
948d0 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  er;           /*
948e0 20 54 68 72 65 61 64 20 74 68 61 74 20 69 73 20   Thread that is 
948f0 77 69 74 68 69 6e 20 74 68 69 73 20 6d 75 74 65  within this mute
94900 78 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  x */.#ifdef SQLI
94910 54 45 5f 44 45 42 55 47 0a 20 20 69 6e 74 20 74  TE_DEBUG.  int t
94920 72 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20  race;           
94930 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f        /* True to
94940 20 74 72 61 63 65 20 63 68 61 6e 67 65 73 20 2a   trace changes *
94950 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 23 69 66 64  /.#endif.};.#ifd
94960 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
94970 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f  #define SQLITE3_
94980 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45  MUTEX_INITIALIZE
94990 52 20 7b 20 50 54 48 52 45 41 44 5f 4d 55 54 45  R { PTHREAD_MUTE
949a0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30  X_INITIALIZER, 0
949b0 2c 20 30 2c 20 28 70 74 68 72 65 61 64 5f 74 29  , 0, (pthread_t)
949c0 30 2c 20 30 20 7d 0a 23 65 6c 73 65 0a 23 64 65  0, 0 }.#else.#de
949d0 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55 54  fine SQLITE3_MUT
949e0 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b  EX_INITIALIZER {
949f0 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 49   PTHREAD_MUTEX_I
94a00 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30  NITIALIZER, 0, 0
94a10 2c 20 28 70 74 68 72 65 61 64 5f 74 29 30 20 7d  , (pthread_t)0 }
94a20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
94a30 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
94a40 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69  _held() and sqli
94a50 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c  te3_mutex_nothel
94a60 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a  d() routine are.
94a70 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20  ** intended for 
94a80 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64 65 20  use only inside 
94a90 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
94aa0 6e 74 73 2e 20 20 4f 6e 20 73 6f 6d 65 20 70 6c  nts.  On some pl
94ab0 61 74 66 6f 72 6d 73 2c 0a 2a 2a 20 74 68 65 72  atforms,.** ther
94ac0 65 20 6d 69 67 68 74 20 62 65 20 72 61 63 65 20  e might be race 
94ad0 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 20  conditions that 
94ae0 63 61 6e 20 63 61 75 73 65 20 74 68 65 73 65 20  can cause these 
94af0 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a 20 64  routines to.** d
94b00 65 6c 69 76 65 72 20 69 6e 63 6f 72 72 65 63 74  eliver incorrect
94b10 20 72 65 73 75 6c 74 73 2e 20 20 49 6e 20 70 61   results.  In pa
94b20 72 74 69 63 75 6c 61 72 2c 20 69 66 20 70 74 68  rticular, if pth
94b30 72 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73 0a  read_equal() is.
94b40 2a 2a 20 6e 6f 74 20 61 6e 20 61 74 6f 6d 69 63  ** not an atomic
94b50 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e   operation, then
94b60 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   these routines 
94b70 6d 69 67 68 74 20 64 65 6c 69 76 65 72 79 0a 2a  might delivery.*
94b80 2a 20 69 6e 63 6f 72 72 65 63 74 20 72 65 73 75  * incorrect resu
94b90 6c 74 73 2e 20 20 4f 6e 20 6d 6f 73 74 20 70 6c  lts.  On most pl
94ba0 61 74 66 6f 72 6d 73 2c 20 70 74 68 72 65 61 64  atforms, pthread
94bb0 5f 65 71 75 61 6c 28 29 20 69 73 20 61 20 0a 2a  _equal() is a .*
94bc0 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20  * comparison of 
94bd0 74 77 6f 20 69 6e 74 65 67 65 72 73 20 61 6e 64  two integers and
94be0 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 61 74   is therefore at
94bf0 6f 6d 69 63 2e 20 20 42 75 74 20 77 65 20 61 72  omic.  But we ar
94c00 65 0a 2a 2a 20 74 6f 6c 64 20 74 68 61 74 20 48  e.** told that H
94c10 50 55 58 20 69 73 20 6e 6f 74 20 73 75 63 68 20  PUX is not such 
94c20 61 20 70 6c 61 74 66 6f 72 6d 2e 20 20 49 66 20  a platform.  If 
94c30 73 6f 2c 20 74 68 65 6e 20 74 68 65 73 65 20 72  so, then these r
94c40 6f 75 74 69 6e 65 73 0a 2a 2a 20 77 69 6c 6c 20  outines.** will 
94c50 6e 6f 74 20 61 6c 77 61 79 73 20 77 6f 72 6b 20  not always work 
94c60 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 48 50 55  correctly on HPU
94c70 58 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74 68 6f 73  X..**.** On thos
94c80 65 20 70 6c 61 74 66 6f 72 6d 73 20 77 68 65 72  e platforms wher
94c90 65 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28  e pthread_equal(
94ca0 29 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2c  ) is not atomic,
94cb0 20 53 51 4c 69 74 65 0a 2a 2a 20 73 68 6f 75 6c   SQLite.** shoul
94cc0 64 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 77 69  d be compiled wi
94cd0 74 68 6f 75 74 20 2d 44 53 51 4c 49 54 45 5f 44  thout -DSQLITE_D
94ce0 45 42 55 47 20 61 6e 64 20 77 69 74 68 20 2d 44  EBUG and with -D
94cf0 4e 44 45 42 55 47 20 74 6f 0a 2a 2a 20 6d 61 6b  NDEBUG to.** mak
94d00 65 20 73 75 72 65 20 6e 6f 20 61 73 73 65 72 74  e sure no assert
94d10 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72  () statements ar
94d20 65 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20  e evaluated and 
94d30 68 65 6e 63 65 20 74 68 65 73 65 0a 2a 2a 20 72  hence these.** r
94d40 6f 75 74 69 6e 65 73 20 61 72 65 20 6e 65 76 65  outines are neve
94d50 72 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66  r called..*/.#if
94d60 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47   !defined(NDEBUG
94d70 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
94d80 49 54 45 5f 44 45 42 55 47 29 0a 73 74 61 74 69  ITE_DEBUG).stati
94d90 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74  c int pthreadMut
94da0 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d  exHeld(sqlite3_m
94db0 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75  utex *p){.  retu
94dc0 72 6e 20 28 70 2d 3e 6e 52 65 66 21 3d 30 20 26  rn (p->nRef!=0 &
94dd0 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28  & pthread_equal(
94de0 70 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65 61  p->owner, pthrea
94df0 64 5f 73 65 6c 66 28 29 29 29 3b 0a 7d 0a 73 74  d_self()));.}.st
94e00 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64  atic int pthread
94e10 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c  MutexNotheld(sql
94e20 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
94e30 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66    return p->nRef
94e40 3d 3d 30 20 7c 7c 20 70 74 68 72 65 61 64 5f 65  ==0 || pthread_e
94e50 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 70  qual(p->owner, p
94e60 74 68 72 65 61 64 5f 73 65 6c 66 28 29 29 3d 3d  thread_self())==
94e70 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  0;.}.#endif../*.
94e80 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e  ** Initialize an
94e90 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74  d deinitialize t
94ea0 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74  he mutex subsyst
94eb0 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  em..*/.static in
94ec0 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 49 6e  t pthreadMutexIn
94ed0 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  it(void){ return
94ee0 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74   SQLITE_OK; }.st
94ef0 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64  atic int pthread
94f00 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20  MutexEnd(void){ 
94f10 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
94f20 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  ; }../*.** The s
94f30 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
94f40 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c  oc() routine all
94f50 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20  ocates a new.** 
94f60 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e  mutex and return
94f70 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  s a pointer to i
94f80 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 6e  t.  If it return
94f90 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d  s NULL.** that m
94fa0 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65  eans that a mute
94fb0 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61  x could not be a
94fc0 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74  llocated.  SQLit
94fd0 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e 64  e.** will unwind
94fe0 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72   its stack and r
94ff0 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20  eturn an error. 
95000 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a   The argument.**
95010 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   to sqlite3_mute
95020 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65  x_alloc() is one
95030 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65   of these intege
95040 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a  r constants:.**.
95050 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20  ** <ul>.** <li> 
95060 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
95070 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  ST.** <li>  SQLI
95080 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
95090 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  VE.** <li>  SQLI
950a0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
950b0 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20  MASTER.** <li>  
950c0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
950d0 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20  TIC_MEM.** <li> 
950e0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
950f0 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69  ATIC_MEM2.** <li
95100 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
95110 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c  STATIC_PRNG.** <
95120 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
95130 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a 20  X_STATIC_LRU.** 
95140 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
95150 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a 2a  EX_STATIC_LRU2.*
95160 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68  * </ul>.**.** Th
95170 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73  e first two cons
95180 74 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69  tants cause sqli
95190 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
951a0 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61  ) to create.** a
951b0 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65   new mutex.  The
951c0 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65   new mutex is re
951d0 63 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c  cursive when SQL
951e0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
951f0 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62  IVE.** is used b
95200 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69  ut not necessari
95210 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54  ly so when SQLIT
95220 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20  E_MUTEX_FAST is 
95230 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74  used..** The mut
95240 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ex implementatio
95250 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  n does not need 
95260 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e  to make a distin
95270 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e  ction.** between
95280 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
95290 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49  CURSIVE and SQLI
952a0 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66  TE_MUTEX_FAST if
952b0 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20   it does.** not 
952c0 77 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51  want to.  But SQ
952d0 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72  Lite will only r
952e0 65 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69  equest a recursi
952f0 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63  ve mutex in.** c
95300 61 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65  ases where it re
95310 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20  ally needs one. 
95320 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e   If a faster non
95330 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78  -recursive mutex
95340 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
95350 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20  on is available 
95360 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74  on the host plat
95370 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20  form, the mutex 
95380 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67  subsystem.** mig
95390 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61  ht return such a
953a0 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e   mutex in respon
953b0 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54  se to SQLITE_MUT
953c0 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54  EX_FAST..**.** T
953d0 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64  he other allowed
953e0 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73   parameters to s
953f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
95400 6f 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e  oc() each return
95410 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  .** a pointer to
95420 20 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69   a static preexi
95430 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 53 69  sting mutex.  Si
95440 78 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73  x static mutexes
95450 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20   are.** used by 
95460 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73  the current vers
95470 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20  ion of SQLite.  
95480 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20  Future versions 
95490 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79  of SQLite.** may
954a0 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20   add additional 
954b0 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20  static mutexes. 
954c0 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20   Static mutexes 
954d0 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c  are for internal
954e0 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74  .** use by SQLit
954f0 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61  e only.  Applica
95500 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53  tions that use S
95510 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68  QLite mutexes sh
95520 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79  ould.** use only
95530 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74   the dynamic mut
95540 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79  exes returned by
95550 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
95560 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f  ST or.** SQLITE_
95570 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e  MUTEX_RECURSIVE.
95580 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  .**.** Note that
95590 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 64   if one of the d
955a0 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 72  ynamic mutex par
955b0 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 5f  ameters (SQLITE_
955c0 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72  MUTEX_FAST.** or
955d0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
955e0 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65 64  CURSIVE) is used
955f0 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75   then sqlite3_mu
95600 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72  tex_alloc().** r
95610 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 65  eturns a differe
95620 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72  nt mutex on ever
95630 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f 72  y call.  But for
95640 20 74 68 65 20 73 74 61 74 69 63 20 0a 2a 2a 20   the static .** 
95650 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65  mutex types, the
95660 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72   same mutex is r
95670 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79  eturned on every
95680 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a   call that has.*
95690 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20  * the same type 
956a0 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  number..*/.stati
956b0 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  c sqlite3_mutex 
956c0 2a 70 74 68 72 65 61 64 4d 75 74 65 78 41 6c 6c  *pthreadMutexAll
956d0 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a 20  oc(int iType){. 
956e0 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f   static sqlite3_
956f0 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 74 65  mutex staticMute
95700 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 53 51  xes[] = {.    SQ
95710 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54  LITE3_MUTEX_INIT
95720 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c  IALIZER,.    SQL
95730 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49  ITE3_MUTEX_INITI
95740 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49  ALIZER,.    SQLI
95750 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41  TE3_MUTEX_INITIA
95760 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54  LIZER,.    SQLIT
95770 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c  E3_MUTEX_INITIAL
95780 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45  IZER,.    SQLITE
95790 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  3_MUTEX_INITIALI
957a0 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33  ZER,.    SQLITE3
957b0 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a  _MUTEX_INITIALIZ
957c0 45 52 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65  ER.  };.  sqlite
957d0 33 5f 6d 75 74 65 78 20 2a 70 3b 0a 20 20 73 77  3_mutex *p;.  sw
957e0 69 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20  itch( iType ){. 
957f0 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d     case SQLITE_M
95800 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a 20  UTEX_RECURSIVE: 
95810 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69  {.      p = sqli
95820 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73  te3MallocZero( s
95830 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20  izeof(*p) );.   
95840 20 20 20 69 66 28 20 70 20 29 7b 0a 23 69 66 64     if( p ){.#ifd
95850 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52  ef SQLITE_HOMEGR
95860 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55  OWN_RECURSIVE_MU
95870 54 45 58 0a 20 20 20 20 20 20 20 20 2f 2a 20 49  TEX.        /* I
95880 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  f recursive mute
95890 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69  xes are not avai
958a0 6c 61 62 6c 65 2c 20 77 65 20 77 69 6c 6c 20 68  lable, we will h
958b0 61 76 65 20 74 6f 0a 20 20 20 20 20 20 20 20 2a  ave to.        *
958c0 2a 20 62 75 69 6c 64 20 6f 75 72 20 6f 77 6e 2e  * build our own.
958d0 20 20 53 65 65 20 62 65 6c 6f 77 2e 20 2a 2f 0a    See below. */.
958e0 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f          pthread_
958f0 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d  mutex_init(&p->m
95900 75 74 65 78 2c 20 30 29 3b 0a 23 65 6c 73 65 0a  utex, 0);.#else.
95910 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 61          /* Use a
95920 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78   recursive mutex
95930 20 69 66 20 69 74 20 69 73 20 61 76 61 69 6c 61   if it is availa
95940 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 70  ble */.        p
95950 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72  thread_mutexattr
95960 5f 74 20 72 65 63 75 72 73 69 76 65 41 74 74 72  _t recursiveAttr
95970 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61  ;.        pthrea
95980 64 5f 6d 75 74 65 78 61 74 74 72 5f 69 6e 69 74  d_mutexattr_init
95990 28 26 72 65 63 75 72 73 69 76 65 41 74 74 72 29  (&recursiveAttr)
959a0 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61  ;.        pthrea
959b0 64 5f 6d 75 74 65 78 61 74 74 72 5f 73 65 74 74  d_mutexattr_sett
959c0 79 70 65 28 26 72 65 63 75 72 73 69 76 65 41 74  ype(&recursiveAt
959d0 74 72 2c 20 50 54 48 52 45 41 44 5f 4d 55 54 45  tr, PTHREAD_MUTE
959e0 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0a 20 20  X_RECURSIVE);.  
959f0 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75        pthread_mu
95a00 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 74  tex_init(&p->mut
95a10 65 78 2c 20 26 72 65 63 75 72 73 69 76 65 41 74  ex, &recursiveAt
95a20 74 72 29 3b 0a 20 20 20 20 20 20 20 20 70 74 68  tr);.        pth
95a30 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 64  read_mutexattr_d
95a40 65 73 74 72 6f 79 28 26 72 65 63 75 72 73 69 76  estroy(&recursiv
95a50 65 41 74 74 72 29 3b 0a 23 65 6e 64 69 66 0a 20  eAttr);.#endif. 
95a60 20 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69         p->id = i
95a70 54 79 70 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Type;.      }.  
95a80 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
95a90 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
95aa0 5f 4d 55 54 45 58 5f 46 41 53 54 3a 20 7b 0a 20  _MUTEX_FAST: {. 
95ab0 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33       p = sqlite3
95ac0 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65  MallocZero( size
95ad0 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20  of(*p) );.      
95ae0 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20  if( p ){.       
95af0 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a   p->id = iType;.
95b00 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f          pthread_
95b10 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d  mutex_init(&p->m
95b20 75 74 65 78 2c 20 30 29 3b 0a 20 20 20 20 20 20  utex, 0);.      
95b30 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
95b40 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74     }.    default
95b50 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  : {.      assert
95b60 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 29  ( iType-2 >= 0 )
95b70 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
95b80 69 54 79 70 65 2d 32 20 3c 20 41 72 72 61 79 53  iType-2 < ArrayS
95b90 69 7a 65 28 73 74 61 74 69 63 4d 75 74 65 78 65  ize(staticMutexe
95ba0 73 29 20 29 3b 0a 20 20 20 20 20 20 70 20 3d 20  s) );.      p = 
95bb0 26 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69  &staticMutexes[i
95bc0 54 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20 20 70  Type-2];.      p
95bd0 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20  ->id = iType;.  
95be0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
95bf0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
95c00 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  .}.../*.** This 
95c10 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61  routine dealloca
95c20 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79  tes a previously
95c30 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75  .** allocated mu
95c40 74 65 78 2e 20 20 53 51 4c 69 74 65 20 69 73 20  tex.  SQLite is 
95c50 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c  careful to deall
95c60 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 6d  ocate every.** m
95c70 75 74 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c  utex that it all
95c80 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  ocates..*/.stati
95c90 63 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d 75  c void pthreadMu
95ca0 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f  texFree(sqlite3_
95cb0 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73  mutex *p){.  ass
95cc0 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20  ert( p->nRef==0 
95cd0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
95ce0 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
95cf0 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d  _FAST || p->id==
95d00 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
95d10 55 52 53 49 56 45 20 29 3b 0a 20 20 70 74 68 72  URSIVE );.  pthr
95d20 65 61 64 5f 6d 75 74 65 78 5f 64 65 73 74 72 6f  ead_mutex_destro
95d30 79 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  y(&p->mutex);.  
95d40 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
95d50 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71  .}../*.** The sq
95d60 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
95d70 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  r() and sqlite3_
95d80 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74  mutex_try() rout
95d90 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20  ines attempt.** 
95da0 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78  to enter a mutex
95db0 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68  .  If another th
95dc0 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20  read is already 
95dd0 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78  within the mutex
95de0 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74  ,.** sqlite3_mut
95df0 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20  ex_enter() will 
95e00 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65  block and sqlite
95e10 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69  3_mutex_try() wi
95e20 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c  ll return.** SQL
95e30 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73  ITE_BUSY.  The s
95e40 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
95e50 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  () interface ret
95e60 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a  urns SQLITE_OK.*
95e70 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75  * upon successfu
95e80 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65  l entry.  Mutexe
95e90 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20  s created using 
95ea0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
95eb0 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65  URSIVE can.** be
95ec0 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c   entered multipl
95ed0 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73  e times by the s
95ee0 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20  ame thread.  In 
95ef0 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a  such cases the,.
95f00 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65  ** mutex must be
95f10 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c   exited an equal
95f20 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73   number of times
95f30 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20   before another 
95f40 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e  thread.** can en
95f50 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d  ter.  If the sam
95f60 65 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74  e thread tries t
95f70 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65  o enter any othe
95f80 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a  r kind of mutex.
95f90 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63  ** more than onc
95fa0 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20  e, the behavior 
95fb0 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f  is undefined..*/
95fc0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 68  .static void pth
95fd0 72 65 61 64 4d 75 74 65 78 45 6e 74 65 72 28 73  readMutexEnter(s
95fe0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
95ff0 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  {.  assert( p->i
96000 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
96010 52 45 43 55 52 53 49 56 45 20 7c 7c 20 70 74 68  RECURSIVE || pth
96020 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64  readMutexNotheld
96030 28 70 29 20 29 3b 0a 0a 23 69 66 64 65 66 20 53  (p) );..#ifdef S
96040 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f  QLITE_HOMEGROWN_
96050 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a  RECURSIVE_MUTEX.
96060 20 20 2f 2a 20 49 66 20 72 65 63 75 72 73 69 76    /* If recursiv
96070 65 20 6d 75 74 65 78 65 73 20 61 72 65 20 6e 6f  e mutexes are no
96080 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 65  t available, the
96090 6e 20 77 65 20 68 61 76 65 20 74 6f 20 67 72 6f  n we have to gro
960a0 77 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e 2e 20  w.  ** our own. 
960b0 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61   This implementa
960c0 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61  tion assumes tha
960d0 74 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28  t pthread_equal(
960e0 29 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d 69 63  ).  ** is atomic
960f0 20 2d 20 74 68 61 74 20 69 74 20 63 61 6e 6e 6f   - that it canno
96100 74 20 62 65 20 64 65 63 65 69 76 65 64 20 69 6e  t be deceived in
96110 74 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65 6c 66  to thinking self
96120 0a 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f 77 6e  .  ** and p->own
96130 65 72 20 61 72 65 20 65 71 75 61 6c 20 69 66 20  er are equal if 
96140 70 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67 65 73  p->owner changes
96150 20 62 65 74 77 65 65 6e 20 74 77 6f 20 76 61 6c   between two val
96160 75 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 61 72  ues.  ** that ar
96170 65 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 73  e not equal to s
96180 65 6c 66 20 77 68 69 6c 65 20 74 68 65 20 63 6f  elf while the co
96190 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 61 6b 69  mparison is taki
961a0 6e 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a 20 54  ng place..  ** T
961b0 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  his implementati
961c0 6f 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20  on also assumes 
961d0 61 20 63 6f 68 65 72 65 6e 74 20 63 61 63 68 65  a coherent cache
961e0 20 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20 73 65   - that .  ** se
961f0 70 61 72 61 74 65 20 70 72 6f 63 65 73 73 65 73  parate processes
96200 20 63 61 6e 6e 6f 74 20 72 65 61 64 20 64 69 66   cannot read dif
96210 66 65 72 65 6e 74 20 76 61 6c 75 65 73 20 66 72  ferent values fr
96220 6f 6d 20 74 68 65 20 73 61 6d 65 0a 20 20 2a 2a  om the same.  **
96230 20 61 64 64 72 65 73 73 20 61 74 20 74 68 65 20   address at the 
96240 73 61 6d 65 20 74 69 6d 65 2e 20 20 49 66 20 65  same time.  If e
96250 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 74  ither of these t
96260 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a 20 20  wo conditions.  
96270 2a 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74 2c 20  ** are not met, 
96280 74 68 65 6e 20 74 68 65 20 6d 75 74 65 78 65 73  then the mutexes
96290 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 20 70   will fail and p
962a0 72 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72 65 73  roblems will res
962b0 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20  ult..  */.  {.  
962c0 20 20 70 74 68 72 65 61 64 5f 74 20 73 65 6c 66    pthread_t self
962d0 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28   = pthread_self(
962e0 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52  );.    if( p->nR
962f0 65 66 3e 30 20 26 26 20 70 74 68 72 65 61 64 5f  ef>0 && pthread_
96300 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20  equal(p->owner, 
96310 73 65 6c 66 29 20 29 7b 0a 20 20 20 20 20 20 70  self) ){.      p
96320 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 65  ->nRef++;.    }e
96330 6c 73 65 7b 0a 20 20 20 20 20 20 70 74 68 72 65  lse{.      pthre
96340 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 70  ad_mutex_lock(&p
96350 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20  ->mutex);.      
96360 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d  assert( p->nRef=
96370 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f  =0 );.      p->o
96380 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20  wner = self;.   
96390 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a     p->nRef = 1;.
963a0 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a      }.  }.#else.
963b0 20 20 2f 2a 20 55 73 65 20 74 68 65 20 62 75 69    /* Use the bui
963c0 6c 74 2d 69 6e 20 72 65 63 75 72 73 69 76 65 20  lt-in recursive 
963d0 6d 75 74 65 78 65 73 20 69 66 20 74 68 65 79 20  mutexes if they 
963e0 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20  are available.. 
963f0 20 2a 2f 0a 20 20 70 74 68 72 65 61 64 5f 6d 75   */.  pthread_mu
96400 74 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74  tex_lock(&p->mut
96410 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20  ex);.  p->owner 
96420 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29  = pthread_self()
96430 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 23  ;.  p->nRef++;.#
96440 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
96450 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28  LITE_DEBUG.  if(
96460 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20   p->trace ){.   
96470 20 70 72 69 6e 74 66 28 22 65 6e 74 65 72 20 6d   printf("enter m
96480 75 74 65 78 20 25 70 20 28 25 64 29 20 77 69 74  utex %p (%d) wit
96490 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c  h nRef=%d\n", p,
964a0 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52   p->trace, p->nR
964b0 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ef);.  }.#endif.
964c0 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 68  }.static int pth
964d0 72 65 61 64 4d 75 74 65 78 54 72 79 28 73 71 6c  readMutexTry(sql
964e0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
964f0 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65    int rc;.  asse
96500 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54  rt( p->id==SQLIT
96510 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
96520 45 20 7c 7c 20 70 74 68 72 65 61 64 4d 75 74 65  E || pthreadMute
96530 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 0a  xNotheld(p) );..
96540 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f  #ifdef SQLITE_HO
96550 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56  MEGROWN_RECURSIV
96560 45 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49 66 20  E_MUTEX.  /* If 
96570 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65  recursive mutexe
96580 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61  s are not availa
96590 62 6c 65 2c 20 74 68 65 6e 20 77 65 20 68 61 76  ble, then we hav
965a0 65 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a 20 6f  e to grow.  ** o
965b0 75 72 20 6f 77 6e 2e 20 20 54 68 69 73 20 69 6d  ur own.  This im
965c0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73  plementation ass
965d0 75 6d 65 73 20 74 68 61 74 20 70 74 68 72 65 61  umes that pthrea
965e0 64 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a 20 69  d_equal().  ** i
965f0 73 20 61 74 6f 6d 69 63 20 2d 20 74 68 61 74 20  s atomic - that 
96600 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 65 63  it cannot be dec
96610 65 69 76 65 64 20 69 6e 74 6f 20 74 68 69 6e 6b  eived into think
96620 69 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20 61 6e  ing self.  ** an
96630 64 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65 20 65  d p->owner are e
96640 71 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e 65 72  qual if p->owner
96650 20 63 68 61 6e 67 65 73 20 62 65 74 77 65 65 6e   changes between
96660 20 74 77 6f 20 76 61 6c 75 65 73 0a 20 20 2a 2a   two values.  **
96670 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 65 71   that are not eq
96680 75 61 6c 20 74 6f 20 73 65 6c 66 20 77 68 69 6c  ual to self whil
96690 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  e the comparison
966a0 20 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 63 65   is taking place
966b0 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d 70 6c  ..  ** This impl
966c0 65 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73 6f 20  ementation also 
966d0 61 73 73 75 6d 65 73 20 61 20 63 6f 68 65 72 65  assumes a cohere
966e0 6e 74 20 63 61 63 68 65 20 2d 20 74 68 61 74 20  nt cache - that 
966f0 0a 20 20 2a 2a 20 73 65 70 61 72 61 74 65 20 70  .  ** separate p
96700 72 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f 74 20  rocesses cannot 
96710 72 65 61 64 20 64 69 66 66 65 72 65 6e 74 20 76  read different v
96720 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 20 73  alues from the s
96730 61 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65 73 73  ame.  ** address
96740 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   at the same tim
96750 65 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 66  e.  If either of
96760 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 64 69   these two condi
96770 74 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65 20 6e  tions.  ** are n
96780 6f 74 20 6d 65 74 2c 20 74 68 65 6e 20 74 68 65  ot met, then the
96790 20 6d 75 74 65 78 65 73 20 77 69 6c 6c 20 66 61   mutexes will fa
967a0 69 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d 73 20  il and problems 
967b0 77 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20 20 2a  will result..  *
967c0 2f 0a 20 20 7b 0a 20 20 20 20 70 74 68 72 65 61  /.  {.    pthrea
967d0 64 5f 74 20 73 65 6c 66 20 3d 20 70 74 68 72 65  d_t self = pthre
967e0 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 69  ad_self();.    i
967f0 66 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26 26 20  f( p->nRef>0 && 
96800 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d  pthread_equal(p-
96810 3e 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20 29 7b  >owner, self) ){
96820 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b  .      p->nRef++
96830 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ;.      rc = SQL
96840 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73  ITE_OK;.    }els
96850 65 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d 75  e if( pthread_mu
96860 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e  tex_trylock(&p->
96870 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 20 20  mutex)==0 ){.   
96880 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52     assert( p->nR
96890 65 66 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  ef==0 );.      p
968a0 2d 3e 6f 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a  ->owner = self;.
968b0 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20        p->nRef = 
968c0 31 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  1;.      rc = SQ
968d0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c  LITE_OK;.    }el
968e0 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  se{.      rc = S
968f0 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20  QLITE_BUSY;.    
96900 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a  }.  }.#else.  /*
96910 20 55 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69   Use the built-i
96920 6e 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  n recursive mute
96930 78 65 73 20 69 66 20 74 68 65 79 20 61 72 65 20  xes if they are 
96940 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a  available..  */.
96950 20 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d 75    if( pthread_mu
96960 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e  tex_trylock(&p->
96970 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 20 20  mutex)==0 ){.   
96980 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72   p->owner = pthr
96990 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20  ead_self();.    
969a0 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 72  p->nRef++;.    r
969b0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
969c0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d   }else{.    rc =
969d0 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
969e0 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  }.#endif..#ifdef
969f0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
96a00 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
96a10 4b 20 26 26 20 70 2d 3e 74 72 61 63 65 20 29 7b  K && p->trace ){
96a20 0a 20 20 20 20 70 72 69 6e 74 66 28 22 65 6e 74  .    printf("ent
96a30 65 72 20 6d 75 74 65 78 20 25 70 20 28 25 64 29  er mutex %p (%d)
96a40 20 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22   with nRef=%d\n"
96a50 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70  , p, p->trace, p
96a60 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e  ->nRef);.  }.#en
96a70 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  dif.  return rc;
96a80 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71  .}../*.** The sq
96a90 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
96aa0 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74  e() routine exit
96ab0 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77  s a mutex that w
96ac0 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79  as.** previously
96ad0 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20   entered by the 
96ae0 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68  same thread.  Th
96af0 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73  e behavior.** is
96b00 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68   undefined if th
96b10 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63  e mutex is not c
96b20 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64  urrently entered
96b30 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75   or.** is not cu
96b40 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65  rrently allocate
96b50 64 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  d.  SQLite will 
96b60 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e  never do either.
96b70 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
96b80 70 74 68 72 65 61 64 4d 75 74 65 78 4c 65 61 76  pthreadMutexLeav
96b90 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  e(sqlite3_mutex 
96ba0 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
96bb0 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64 28  threadMutexHeld(
96bc0 70 29 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d  p) );.  p->nRef-
96bd0 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  -;.  assert( p->
96be0 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64  nRef==0 || p->id
96bf0 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  ==SQLITE_MUTEX_R
96c00 45 43 55 52 53 49 56 45 20 29 3b 0a 0a 23 69 66  ECURSIVE );..#if
96c10 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47  def SQLITE_HOMEG
96c20 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d  ROWN_RECURSIVE_M
96c30 55 54 45 58 0a 20 20 69 66 28 20 70 2d 3e 6e 52  UTEX.  if( p->nR
96c40 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 70 74 68  ef==0 ){.    pth
96c50 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63  read_mutex_unloc
96c60 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  k(&p->mutex);.  
96c70 7d 0a 23 65 6c 73 65 0a 20 20 70 74 68 72 65 61  }.#else.  pthrea
96c80 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26  d_mutex_unlock(&
96c90 70 2d 3e 6d 75 74 65 78 29 3b 0a 23 65 6e 64 69  p->mutex);.#endi
96ca0 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
96cb0 5f 44 45 42 55 47 0a 20 20 69 66 28 20 70 2d 3e  _DEBUG.  if( p->
96cc0 74 72 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69  trace ){.    pri
96cd0 6e 74 66 28 22 6c 65 61 76 65 20 6d 75 74 65 78  ntf("leave mutex
96ce0 20 25 70 20 28 25 64 29 20 77 69 74 68 20 6e 52   %p (%d) with nR
96cf0 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e  ef=%d\n", p, p->
96d00 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b  trace, p->nRef);
96d10 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 53  .  }.#endif.}..S
96d20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71  QLITE_PRIVATE sq
96d30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
96d40 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61  ods *sqlite3Defa
96d50 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a  ultMutex(void){.
96d60 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33    static sqlite3
96d70 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73  _mutex_methods s
96d80 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 70 74  Mutex = {.    pt
96d90 68 72 65 61 64 4d 75 74 65 78 49 6e 69 74 2c 0a  hreadMutexInit,.
96da0 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78      pthreadMutex
96db0 45 6e 64 2c 0a 20 20 20 20 70 74 68 72 65 61 64  End,.    pthread
96dc0 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20  MutexAlloc,.    
96dd0 70 74 68 72 65 61 64 4d 75 74 65 78 46 72 65 65  pthreadMutexFree
96de0 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74  ,.    pthreadMut
96df0 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 70 74 68  exEnter,.    pth
96e00 72 65 61 64 4d 75 74 65 78 54 72 79 2c 0a 20 20  readMutexTry,.  
96e10 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 4c 65    pthreadMutexLe
96e20 61 76 65 2c 0a 23 69 66 64 65 66 20 53 51 4c 49  ave,.#ifdef SQLI
96e30 54 45 5f 44 45 42 55 47 0a 20 20 20 20 70 74 68  TE_DEBUG.    pth
96e40 72 65 61 64 4d 75 74 65 78 48 65 6c 64 2c 0a 20  readMutexHeld,. 
96e50 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 4e     pthreadMutexN
96e60 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20 20 20  otheld.#else.   
96e70 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64 69 66   0,.    0.#endif
96e80 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20  .  };..  return 
96e90 26 73 4d 75 74 65 78 3b 0a 7d 0a 0a 23 65 6e 64  &sMutex;.}..#end
96ea0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54  if /* SQLITE_MUT
96eb0 45 58 5f 50 54 48 52 45 41 44 20 2a 2f 0a 0a 2f  EX_PTHREAD */../
96ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
96ed0 6e 64 20 6f 66 20 6d 75 74 65 78 5f 75 6e 69 78  nd of mutex_unix
96ee0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
96ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
96f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
96f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
96f20 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f  egin file mutex_
96f30 77 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  w32.c **********
96f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
96f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
96f60 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74  *.** 2007 August
96f70 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   14.**.** The au
96f80 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
96f90 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
96fa0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
96fb0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
96fc0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
96fd0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
96fe0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
96ff0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
97000 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
97010 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
97020 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
97030 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
97040 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
97050 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
97060 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
97070 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
97080 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
97090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
970a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
970b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
970c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
970d0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
970e0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66  contains the C f
970f0 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d  unctions that im
97100 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20  plement mutexes 
97110 66 6f 72 20 77 69 6e 33 32 0a 2a 2a 0a 2a 2a 20  for win32.**.** 
97120 24 49 64 3a 20 6d 75 74 65 78 5f 77 33 32 2e 63  $Id: mutex_w32.c
97130 2c 76 20 31 2e 31 38 20 32 30 30 39 2f 30 38 2f  ,v 1.18 2009/08/
97140 31 30 20 30 33 3a 32 33 3a 32 31 20 73 68 61 6e  10 03:23:21 shan
97150 65 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  e Exp $.*/../*.*
97160 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68  * The code in th
97170 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20  is file is only 
97180 75 73 65 64 20 69 66 20 77 65 20 61 72 65 20 63  used if we are c
97190 6f 6d 70 69 6c 69 6e 67 20 6d 75 6c 74 69 74 68  ompiling multith
971a0 72 65 61 64 65 64 0a 2a 2a 20 6f 6e 20 61 20 77  readed.** on a w
971b0 69 6e 33 32 20 73 79 73 74 65 6d 2e 0a 2a 2f 0a  in32 system..*/.
971c0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55  #ifdef SQLITE_MU
971d0 54 45 58 5f 57 33 32 0a 0a 2f 2a 0a 2a 2a 20 45  TEX_W32../*.** E
971e0 61 63 68 20 72 65 63 75 72 73 69 76 65 20 6d 75  ach recursive mu
971f0 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e  tex is an instan
97200 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
97210 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ing structure..*
97220 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  /.struct sqlite3
97230 5f 6d 75 74 65 78 20 7b 0a 20 20 43 52 49 54 49  _mutex {.  CRITI
97240 43 41 4c 5f 53 45 43 54 49 4f 4e 20 6d 75 74 65  CAL_SECTION mute
97250 78 3b 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63  x;    /* Mutex c
97260 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c  ontrolling the l
97270 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 64 3b  ock */.  int id;
97280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
97290 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70      /* Mutex typ
972a0 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b  e */.  int nRef;
972b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
972c0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
972d0 6e 74 65 72 61 6e 63 65 73 20 2a 2f 0a 20 20 44  nterances */.  D
972e0 57 4f 52 44 20 6f 77 6e 65 72 3b 20 20 20 20 20  WORD owner;     
972f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 72            /* Thr
97300 65 61 64 20 68 6f 6c 64 69 6e 67 20 74 68 69 73  ead holding this
97310 20 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   mutex */.};../*
97320 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
97330 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77 65  (non-zero) if we
97340 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e 64   are running und
97350 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c  er WinNT, Win2K,
97360 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57 69   WinXP,.** or Wi
97370 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61 6c  nCE.  Return fal
97380 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57 69  se (zero) for Wi
97390 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20 57  n95, Win98, or W
973a0 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65  inME..**.** Here
973b0 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74 69   is an interesti
973c0 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a 20  ng observation: 
973d0 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 61   Win95, Win98, a
973e0 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a  nd WinME lack.**
973f0 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28   the LockFileEx(
97400 29 20 41 50 49 2e 20 20 42 75 74 20 77 65 20 63  ) API.  But we c
97410 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63 61  an still statica
97420 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74  lly link against
97430 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73 20   that.** API as 
97440 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27 74  long as we don't
97450 20 63 61 6c 6c 20 69 74 20 77 69 6e 20 72 75 6e   call it win run
97460 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f 4d 45  ning Win95/98/ME
97470 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20  .  A call to.** 
97480 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
97490 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e  used to determin
974a0 65 20 69 66 20 74 68 65 20 68 6f 73 74 20 69 73  e if the host is
974b0 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f 72 0a   Win95/98/ME or.
974c0 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73  ** WinNT/2K/XP s
974d0 6f 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 6b  o that we will k
974e0 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20 6e  now whether or n
974f0 6f 74 20 77 65 20 63 61 6e 20 73 61 66 65 6c 79  ot we can safely
97500 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63   call.** the Loc
97510 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e 0a 2a  kFileEx() API..*
97520 2a 0a 2a 2a 20 6d 75 74 65 78 49 73 4e 54 28 29  *.** mutexIsNT()
97530 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f   is only used fo
97540 72 20 74 68 65 20 54 72 79 45 6e 74 65 72 43 72  r the TryEnterCr
97550 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20  iticalSection() 
97560 41 50 49 20 63 61 6c 6c 2c 0a 2a 2a 20 77 68 69  API call,.** whi
97570 63 68 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c  ch is only avail
97580 61 62 6c 65 20 69 66 20 79 6f 75 72 20 61 70 70  able if your app
97590 6c 69 63 61 74 69 6f 6e 20 77 61 73 20 63 6f 6d  lication was com
975a0 70 69 6c 65 64 20 77 69 74 68 20 0a 2a 2a 20 5f  piled with .** _
975b0 57 49 4e 33 32 5f 57 49 4e 4e 54 20 64 65 66 69  WIN32_WINNT defi
975c0 6e 65 64 20 74 6f 20 61 20 76 61 6c 75 65 20 3e  ned to a value >
975d0 3d 20 30 78 30 34 30 30 2e 20 20 43 75 72 72 65  = 0x0400.  Curre
975e0 6e 74 6c 79 2c 20 74 68 65 20 6f 6e 6c 79 0a 2a  ntly, the only.*
975f0 2a 20 63 61 6c 6c 20 74 6f 20 54 72 79 45 6e 74  * call to TryEnt
97600 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f  erCriticalSectio
97610 6e 28 29 20 69 73 20 23 69 66 64 65 66 27 65 64  n() is #ifdef'ed
97620 20 6f 75 74 2c 20 73 6f 20 23 69 66 64 65 66 20   out, so #ifdef 
97630 0a 2a 2a 20 74 68 69 73 20 6f 75 74 20 61 73 20  .** this out as 
97640 77 65 6c 6c 2e 0a 2a 2f 0a 23 69 66 20 30 0a 23  well..*/.#if 0.#
97650 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
97660 43 45 0a 23 20 64 65 66 69 6e 65 20 6d 75 74 65  CE.# define mute
97670 78 49 73 4e 54 28 29 20 20 28 31 29 0a 23 65 6c  xIsNT()  (1).#el
97680 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20  se.  static int 
97690 6d 75 74 65 78 49 73 4e 54 28 76 6f 69 64 29 7b  mutexIsNT(void){
976a0 0a 20 20 20 20 73 74 61 74 69 63 20 69 6e 74 20  .    static int 
976b0 6f 73 54 79 70 65 20 3d 20 30 3b 0a 20 20 20 20  osType = 0;.    
976c0 69 66 28 20 6f 73 54 79 70 65 3d 3d 30 20 29 7b  if( osType==0 ){
976d0 0a 20 20 20 20 20 20 4f 53 56 45 52 53 49 4f 4e  .      OSVERSION
976e0 49 4e 46 4f 20 73 49 6e 66 6f 3b 0a 20 20 20 20  INFO sInfo;.    
976f0 20 20 73 49 6e 66 6f 2e 64 77 4f 53 56 65 72 73    sInfo.dwOSVers
97700 69 6f 6e 49 6e 66 6f 53 69 7a 65 20 3d 20 73 69  ionInfoSize = si
97710 7a 65 6f 66 28 73 49 6e 66 6f 29 3b 0a 20 20 20  zeof(sInfo);.   
97720 20 20 20 47 65 74 56 65 72 73 69 6f 6e 45 78 28     GetVersionEx(
97730 26 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 6f  &sInfo);.      o
97740 73 54 79 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77  sType = sInfo.dw
97750 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f  PlatformId==VER_
97760 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e  PLATFORM_WIN32_N
97770 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d  T ? 2 : 1;.    }
97780 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 73 54 79  .    return osTy
97790 70 65 3d 3d 32 3b 0a 20 20 7d 0a 23 65 6e 64 69  pe==2;.  }.#endi
977a0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57  f /* SQLITE_OS_W
977b0 49 4e 43 45 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a  INCE */.#endif..
977c0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
977d0 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71  BUG./*.** The sq
977e0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
977f0 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d  () and sqlite3_m
97800 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72  utex_notheld() r
97810 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e  outine are.** in
97820 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 6f  tended for use o
97830 6e 6c 79 20 69 6e 73 69 64 65 20 61 73 73 65 72  nly inside asser
97840 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  t() statements..
97850 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
97860 6e 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74  nMutexHeld(sqlit
97870 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
97880 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 21 3d  return p->nRef!=
97890 30 20 26 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d 47  0 && p->owner==G
978a0 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 49  etCurrentThreadI
978b0 64 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e  d();.}.static in
978c0 74 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c  t winMutexNothel
978d0 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  d(sqlite3_mutex 
978e0 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d  *p){.  return p-
978f0 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 6f  >nRef==0 || p->o
97900 77 6e 65 72 21 3d 47 65 74 43 75 72 72 65 6e 74  wner!=GetCurrent
97910 54 68 72 65 61 64 49 64 28 29 3b 0a 7d 0a 23 65  ThreadId();.}.#e
97920 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69  ndif.../*.** Ini
97930 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e  tialize and dein
97940 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74  itialize the mut
97950 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f  ex subsystem..*/
97960 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f  .static sqlite3_
97970 6d 75 74 65 78 20 77 69 6e 4d 75 74 65 78 5f 73  mutex winMutex_s
97980 74 61 74 69 63 4d 75 74 65 78 65 73 5b 36 5d 3b  taticMutexes[6];
97990 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d  .static int winM
979a0 75 74 65 78 5f 69 73 49 6e 69 74 20 3d 20 30 3b  utex_isInit = 0;
979b0 0a 2f 2a 20 41 73 20 77 69 6e 4d 75 74 65 78 49  ./* As winMutexI
979c0 6e 69 74 28 29 20 61 6e 64 20 77 69 6e 4d 75 74  nit() and winMut
979d0 65 78 45 6e 64 28 29 20 61 72 65 20 63 61 6c 6c  exEnd() are call
979e0 65 64 20 61 73 20 70 61 72 74 0a 2a 2a 20 6f 66  ed as part.** of
979f0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69   the sqlite3_ini
97a00 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 71 6c 69  tialize and sqli
97a10 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 0a 2a  te3_shutdown().*
97a20 2a 20 70 72 6f 63 65 73 73 69 6e 67 2c 20 74 68  * processing, th
97a30 65 20 22 69 6e 74 65 72 6c 6f 63 6b 65 64 22 20  e "interlocked" 
97a40 6d 61 67 69 63 20 69 73 20 70 72 6f 62 61 62 6c  magic is probabl
97a50 79 20 6e 6f 74 0a 2a 2a 20 73 74 72 69 63 74 6c  y not.** strictl
97a60 79 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a  y necessary..*/.
97a70 73 74 61 74 69 63 20 6c 6f 6e 67 20 77 69 6e 4d  static long winM
97a80 75 74 65 78 5f 6c 6f 63 6b 20 3d 20 30 3b 0a 0a  utex_lock = 0;..
97a90 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75  static int winMu
97aa0 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 0a  texInit(void){ .
97ab0 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74    /* The first t
97ac0 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 6f 20 31  o increment to 1
97ad0 20 64 6f 65 73 20 61 63 74 75 61 6c 20 69 6e 69   does actual ini
97ae0 74 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0a 20  tialization */. 
97af0 20 69 66 28 20 49 6e 74 65 72 6c 6f 63 6b 65 64   if( Interlocked
97b00 43 6f 6d 70 61 72 65 45 78 63 68 61 6e 67 65 28  CompareExchange(
97b10 26 77 69 6e 4d 75 74 65 78 5f 6c 6f 63 6b 2c 20  &winMutex_lock, 
97b20 31 2c 20 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20  1, 0)==0 ){.    
97b30 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69  int i;.    for(i
97b40 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28  =0; i<ArraySize(
97b50 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d  winMutex_staticM
97b60 75 74 65 78 65 73 29 3b 20 69 2b 2b 29 7b 0a 20  utexes); i++){. 
97b70 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65 43       InitializeC
97b80 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26  riticalSection(&
97b90 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d  winMutex_staticM
97ba0 75 74 65 78 65 73 5b 69 5d 2e 6d 75 74 65 78 29  utexes[i].mutex)
97bb0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 69 6e 4d  ;.    }.    winM
97bc0 75 74 65 78 5f 69 73 49 6e 69 74 20 3d 20 31 3b  utex_isInit = 1;
97bd0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
97be0 20 53 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 69 73   Someone else is
97bf0 20 69 6e 20 74 68 65 20 70 72 6f 63 65 73 73 20   in the process 
97c00 6f 66 20 69 6e 69 74 69 6e 67 20 74 68 65 20 73  of initing the s
97c10 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 2a 2f  tatic mutexes */
97c20 0a 20 20 20 20 77 68 69 6c 65 28 20 21 77 69 6e  .    while( !win
97c30 4d 75 74 65 78 5f 69 73 49 6e 69 74 20 29 7b 0a  Mutex_isInit ){.
97c40 20 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b 0a        Sleep(1);.
97c50 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
97c60 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d  rn SQLITE_OK; .}
97c70 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e  ..static int win
97c80 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20  MutexEnd(void){ 
97c90 0a 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20  .  /* The first 
97ca0 74 6f 20 64 65 63 72 65 6d 65 6e 74 20 74 6f 20  to decrement to 
97cb0 30 20 64 6f 65 73 20 61 63 74 75 61 6c 20 73 68  0 does actual sh
97cc0 75 74 64 6f 77 6e 20 0a 20 20 2a 2a 20 28 77 68  utdown .  ** (wh
97cd0 69 63 68 20 73 68 6f 75 6c 64 20 62 65 20 74 68  ich should be th
97ce0 65 20 6c 61 73 74 20 74 6f 20 73 68 75 74 64 6f  e last to shutdo
97cf0 77 6e 2e 29 20 2a 2f 0a 20 20 69 66 28 20 49 6e  wn.) */.  if( In
97d00 74 65 72 6c 6f 63 6b 65 64 43 6f 6d 70 61 72 65  terlockedCompare
97d10 45 78 63 68 61 6e 67 65 28 26 77 69 6e 4d 75 74  Exchange(&winMut
97d20 65 78 5f 6c 6f 63 6b 2c 20 30 2c 20 31 29 3d 3d  ex_lock, 0, 1)==
97d30 31 20 29 7b 0a 20 20 20 20 69 66 28 20 77 69 6e  1 ){.    if( win
97d40 4d 75 74 65 78 5f 69 73 49 6e 69 74 3d 3d 31 20  Mutex_isInit==1 
97d50 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a  ){.      int i;.
97d60 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
97d70 3c 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d 75  <ArraySize(winMu
97d80 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65  tex_staticMutexe
97d90 73 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  s); i++){.      
97da0 20 20 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c    DeleteCritical
97db0 53 65 63 74 69 6f 6e 28 26 77 69 6e 4d 75 74 65  Section(&winMute
97dc0 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b  x_staticMutexes[
97dd0 69 5d 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  i].mutex);.     
97de0 20 7d 0a 20 20 20 20 20 20 77 69 6e 4d 75 74 65   }.      winMute
97df0 78 5f 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20  x_isInit = 0;.  
97e00 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
97e10 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a   SQLITE_OK; .}..
97e20 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  /*.** The sqlite
97e30 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20  3_mutex_alloc() 
97e40 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65  routine allocate
97e50 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78  s a new.** mutex
97e60 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70   and returns a p
97e70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49  ointer to it.  I
97e80 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c  f it returns NUL
97e90 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20  L.** that means 
97ea0 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75  that a mutex cou
97eb0 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61  ld not be alloca
97ec0 74 65 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20  ted.  SQLite.** 
97ed0 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20  will unwind its 
97ee0 73 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e  stack and return
97ef0 20 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20   an error.  The 
97f00 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73  argument.** to s
97f10 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
97f20 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74  oc() is one of t
97f30 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e  hese integer con
97f40 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75  stants:.**.** <u
97f50 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  l>.** <li>  SQLI
97f60 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a  TE_MUTEX_FAST.**
97f70 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
97f80 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a  TEX_RECURSIVE.**
97f90 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
97fa0 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
97fb0 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  R.** <li>  SQLIT
97fc0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
97fd0 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  EM.** <li>  SQLI
97fe0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
97ff0 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51  MEM2.** <li>  SQ
98000 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
98010 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20  C_PRNG.** <li>  
98020 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
98030 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e 20  TIC_LRU.** <li> 
98040 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
98050 41 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 3c 2f 75  ATIC_LRU2.** </u
98060 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72  l>.**.** The fir
98070 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73  st two constants
98080 20 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d   cause sqlite3_m
98090 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20  utex_alloc() to 
980a0 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20  create.** a new 
980b0 6d 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20  mutex.  The new 
980c0 6d 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69  mutex is recursi
980d0 76 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d  ve when SQLITE_M
980e0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a  UTEX_RECURSIVE.*
980f0 2a 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f  * is used but no
98100 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f  t necessarily so
98110 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54   when SQLITE_MUT
98120 45 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e  EX_FAST is used.
98130 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d  .** The mutex im
98140 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65  plementation doe
98150 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61  s not need to ma
98160 6b 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e  ke a distinction
98170 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49  .** between SQLI
98180 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
98190 56 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55  VE and SQLITE_MU
981a0 54 45 58 5f 46 41 53 54 20 69 66 20 69 74 20 64  TEX_FAST if it d
981b0 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20  oes.** not want 
981c0 74 6f 2e 20 20 42 75 74 20 53 51 4c 69 74 65 20  to.  But SQLite 
981d0 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73  will only reques
981e0 74 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75  t a recursive mu
981f0 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20  tex in.** cases 
98200 77 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20  where it really 
98210 6e 65 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 61  needs one.  If a
98220 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75   faster non-recu
98230 72 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69  rsive mutex.** i
98240 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73  mplementation is
98250 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68   available on th
98260 65 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c  e host platform,
98270 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79   the mutex subsy
98280 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65  stem.** might re
98290 74 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65  turn such a mute
982a0 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f  x in response to
982b0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
982c0 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74  ST..**.** The ot
982d0 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61  her allowed para
982e0 6d 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65  meters to sqlite
982f0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20  3_mutex_alloc() 
98300 65 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61  each return.** a
98310 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74   pointer to a st
98320 61 74 69 63 20 70 72 65 65 78 69 73 74 69 6e 67  atic preexisting
98330 20 6d 75 74 65 78 2e 20 20 53 69 78 20 73 74 61   mutex.  Six sta
98340 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a  tic mutexes are.
98350 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63  ** used by the c
98360 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f  urrent version o
98370 66 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72  f SQLite.  Futur
98380 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  e versions of SQ
98390 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20  Lite.** may add 
983a0 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69  additional stati
983b0 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74  c mutexes.  Stat
983c0 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66  ic mutexes are f
983d0 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75  or internal.** u
983e0 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c  se by SQLite onl
983f0 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  y.  Applications
98400 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65   that use SQLite
98410 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a   mutexes should.
98420 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20  ** use only the 
98430 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20  dynamic mutexes 
98440 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49  returned by SQLI
98450 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72  TE_MUTEX_FAST or
98460 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  .** SQLITE_MUTEX
98470 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a  _RECURSIVE..**.*
98480 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f  * Note that if o
98490 6e 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69  ne of the dynami
984a0 63 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65  c mutex paramete
984b0 72 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  rs (SQLITE_MUTEX
984c0 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49  _FAST.** or SQLI
984d0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
984e0 56 45 29 20 69 73 20 75 73 65 64 20 74 68 65 6e  VE) is used then
984f0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
98500 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e  lloc().** return
98510 73 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75  s a different mu
98520 74 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c  tex on every cal
98530 6c 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65 20  l.  But for the 
98540 73 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78  static .** mutex
98550 20 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65   types, the same
98560 20 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 6e   mutex is return
98570 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c  ed on every call
98580 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65   that has.** the
98590 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65   same type numbe
985a0 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c  r..*/.static sql
985b0 69 74 65 33 5f 6d 75 74 65 78 20 2a 77 69 6e 4d  ite3_mutex *winM
985c0 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 54  utexAlloc(int iT
985d0 79 70 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  ype){.  sqlite3_
985e0 6d 75 74 65 78 20 2a 70 3b 0a 0a 20 20 73 77 69  mutex *p;..  swi
985f0 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 20  tch( iType ){.  
98600 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55    case SQLITE_MU
98610 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 61  TEX_FAST:.    ca
98620 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  se SQLITE_MUTEX_
98630 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20  RECURSIVE: {.   
98640 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61     p = sqlite3Ma
98650 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66  llocZero( sizeof
98660 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66  (*p) );.      if
98670 28 20 70 20 29 7b 20 20 0a 20 20 20 20 20 20 20  ( p ){  .       
98680 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a   p->id = iType;.
98690 20 20 20 20 20 20 20 20 49 6e 69 74 69 61 6c 69          Initiali
986a0 7a 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f  zeCriticalSectio
986b0 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  n(&p->mutex);.  
986c0 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
986d0 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
986e0 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73  ault: {.      as
986f0 73 65 72 74 28 20 77 69 6e 4d 75 74 65 78 5f 69  sert( winMutex_i
98700 73 49 6e 69 74 3d 3d 31 20 29 3b 0a 20 20 20 20  sInit==1 );.    
98710 20 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d    assert( iType-
98720 32 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 20  2 >= 0 );.      
98730 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20  assert( iType-2 
98740 3c 20 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d  < ArraySize(winM
98750 75 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78  utex_staticMutex
98760 65 73 29 20 29 3b 0a 20 20 20 20 20 20 70 20 3d  es) );.      p =
98770 20 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69   &winMutex_stati
98780 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d 32  cMutexes[iType-2
98790 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d  ];.      p->id =
987a0 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 72   iType;.      br
987b0 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
987c0 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f   return p;.}.../
987d0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
987e0 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20  e deallocates a 
987f0 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c  previously.** al
98800 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20 20  located mutex.  
98810 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75  SQLite is carefu
98820 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20  l to deallocate 
98830 65 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74  every.** mutex t
98840 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73  hat it allocates
98850 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
98860 20 77 69 6e 4d 75 74 65 78 46 72 65 65 28 73 71   winMutexFree(sq
98870 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
98880 0a 20 20 61 73 73 65 72 74 28 20 70 20 29 3b 0a  .  assert( p );.
98890 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65    assert( p->nRe
988a0 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  f==0 );.  assert
988b0 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f  ( p->id==SQLITE_
988c0 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d  MUTEX_FAST || p-
988d0 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
988e0 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20  X_RECURSIVE );. 
988f0 20 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c 53   DeleteCriticalS
98900 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78  ection(&p->mutex
98910 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
98920 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  e(p);.}../*.** T
98930 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
98940 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c  _enter() and sql
98950 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29  ite3_mutex_try()
98960 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70   routines attemp
98970 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20  t.** to enter a 
98980 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 68  mutex.  If anoth
98990 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c 72  er thread is alr
989a0 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 20  eady within the 
989b0 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65  mutex,.** sqlite
989c0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20  3_mutex_enter() 
989d0 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73  will block and s
989e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
989f0 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a  () will return.*
98a00 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20  * SQLITE_BUSY.  
98a10 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
98a20 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63  x_try() interfac
98a30 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  e returns SQLITE
98a40 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63  _OK.** upon succ
98a50 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d  essful entry.  M
98a60 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75  utexes created u
98a70 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45  sing SQLITE_MUTE
98a80 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e 0a  X_RECURSIVE can.
98a90 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75  ** be entered mu
98aa0 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20  ltiple times by 
98ab0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e  the same thread.
98ac0 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20    In such cases 
98ad0 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75  the,.** mutex mu
98ae0 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e 20  st be exited an 
98af0 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  equal number of 
98b00 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f  times before ano
98b10 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63  ther thread.** c
98b20 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68  an enter.  If th
98b30 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72  e same thread tr
98b40 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79  ies to enter any
98b50 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d   other kind of m
98b60 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61  utex.** more tha
98b70 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61  n once, the beha
98b80 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65  vior is undefine
98b90 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
98ba0 64 20 77 69 6e 4d 75 74 65 78 45 6e 74 65 72 28  d winMutexEnter(
98bb0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
98bc0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  ){.  assert( p->
98bd0 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
98be0 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 77 69  _RECURSIVE || wi
98bf0 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29  nMutexNotheld(p)
98c00 20 29 3b 0a 20 20 45 6e 74 65 72 43 72 69 74 69   );.  EnterCriti
98c10 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d  calSection(&p->m
98c20 75 74 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65  utex);.  p->owne
98c30 72 20 3d 20 47 65 74 43 75 72 72 65 6e 74 54 68  r = GetCurrentTh
98c40 72 65 61 64 49 64 28 29 3b 20 0a 20 20 70 2d 3e  readId(); .  p->
98c50 6e 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63  nRef++;.}.static
98c60 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 54 72 79   int winMutexTry
98c70 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
98c80 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  p){.  int rc = S
98c90 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 61 73  QLITE_BUSY;.  as
98ca0 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c  sert( p->id==SQL
98cb0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
98cc0 49 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78 4e  IVE || winMutexN
98cd0 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 2f  otheld(p) );.  /
98ce0 2a 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 74  *.  ** The sqlit
98cf0 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72  e3_mutex_try() r
98d00 6f 75 74 69 6e 65 20 69 73 20 76 65 72 79 20 72  outine is very r
98d10 61 72 65 6c 79 20 75 73 65 64 2c 20 61 6e 64 20  arely used, and 
98d20 77 68 65 6e 20 69 74 0a 20 20 2a 2a 20 69 73 20  when it.  ** is 
98d30 75 73 65 64 20 69 74 20 69 73 20 6d 65 72 65 6c  used it is merel
98d40 79 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  y an optimizatio
98d50 6e 2e 20 20 53 6f 20 69 74 20 69 73 20 4f 4b 20  n.  So it is OK 
98d60 66 6f 72 20 69 74 20 74 6f 20 61 6c 77 61 79 73  for it to always
98d70 0a 20 20 2a 2a 20 66 61 69 6c 2e 20 20 0a 20 20  .  ** fail.  .  
98d80 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 54 72 79 45  **.  ** The TryE
98d90 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74  nterCriticalSect
98da0 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20  ion() interface 
98db0 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c  is only availabl
98dc0 65 20 6f 6e 20 57 69 6e 4e 54 2e 0a 20 20 2a 2a  e on WinNT..  **
98dd0 20 41 6e 64 20 73 6f 6d 65 20 77 69 6e 64 6f 77   And some window
98de0 73 20 63 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70  s compilers comp
98df0 6c 61 69 6e 20 69 66 20 79 6f 75 20 74 72 79 20  lain if you try 
98e00 74 6f 20 75 73 65 20 69 74 20 77 69 74 68 6f 75  to use it withou
98e10 74 0a 20 20 2a 2a 20 66 69 72 73 74 20 64 6f 69  t.  ** first doi
98e20 6e 67 20 73 6f 6d 65 20 23 64 65 66 69 6e 65 73  ng some #defines
98e30 20 74 68 61 74 20 70 72 65 76 65 6e 74 20 53 51   that prevent SQ
98e40 4c 69 74 65 20 66 72 6f 6d 20 62 75 69 6c 64 69  Lite from buildi
98e50 6e 67 20 6f 6e 20 57 69 6e 39 38 2e 0a 20 20 2a  ng on Win98..  *
98e60 2a 20 46 6f 72 20 74 68 61 74 20 72 65 61 73 6f  * For that reaso
98e70 6e 2c 20 77 65 20 77 69 6c 6c 20 6f 6d 69 74 20  n, we will omit 
98e80 74 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f  this optimizatio
98e90 6e 20 66 6f 72 20 6e 6f 77 2e 20 20 53 65 65 0a  n for now.  See.
98ea0 20 20 2a 2a 20 74 69 63 6b 65 74 20 23 32 36 38    ** ticket #268
98eb0 35 2e 0a 20 20 2a 2f 0a 23 69 66 20 30 0a 20 20  5..  */.#if 0.  
98ec0 69 66 28 20 6d 75 74 65 78 49 73 4e 54 28 29 20  if( mutexIsNT() 
98ed0 26 26 20 54 72 79 45 6e 74 65 72 43 72 69 74 69  && TryEnterCriti
98ee0 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d  calSection(&p->m
98ef0 75 74 65 78 29 20 29 7b 0a 20 20 20 20 70 2d 3e  utex) ){.    p->
98f00 6f 77 6e 65 72 20 3d 20 47 65 74 43 75 72 72 65  owner = GetCurre
98f10 6e 74 54 68 72 65 61 64 49 64 28 29 3b 0a 20 20  ntThreadId();.  
98f20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20    p->nRef++;.   
98f30 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
98f40 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e 55  .  }.#else.  UNU
98f50 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 29  SED_PARAMETER(p)
98f60 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72  ;.#endif.  retur
98f70 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
98f80 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
98f90 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65  _leave() routine
98fa0 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 20 74   exits a mutex t
98fb0 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69  hat was.** previ
98fc0 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79  ously entered by
98fd0 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
98fe0 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a  .  The behavior.
98ff0 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20  ** is undefined 
99000 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20  if the mutex is 
99010 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e  not currently en
99020 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e  tered or.** is n
99030 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c  ot currently all
99040 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 20  ocated.  SQLite 
99050 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 69  will never do ei
99060 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ther..*/.static 
99070 76 6f 69 64 20 77 69 6e 4d 75 74 65 78 4c 65 61  void winMutexLea
99080 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ve(sqlite3_mutex
99090 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
990a0 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 61  p->nRef>0 );.  a
990b0 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 3d  ssert( p->owner=
990c0 3d 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61  =GetCurrentThrea
990d0 64 49 64 28 29 20 29 3b 0a 20 20 70 2d 3e 6e 52  dId() );.  p->nR
990e0 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20  ef--;.  assert( 
990f0 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d  p->nRef==0 || p-
99100 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
99110 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20  X_RECURSIVE );. 
99120 20 4c 65 61 76 65 43 72 69 74 69 63 61 6c 53 65   LeaveCriticalSe
99130 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29  ction(&p->mutex)
99140 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  ;.}..SQLITE_PRIV
99150 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ATE sqlite3_mute
99160 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74  x_methods *sqlit
99170 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76  e3DefaultMutex(v
99180 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73  oid){.  static s
99190 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
991a0 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a  hods sMutex = {.
991b0 20 20 20 20 77 69 6e 4d 75 74 65 78 49 6e 69 74      winMutexInit
991c0 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 45 6e  ,.    winMutexEn
991d0 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 41  d,.    winMutexA
991e0 6c 6c 6f 63 2c 0a 20 20 20 20 77 69 6e 4d 75 74  lloc,.    winMut
991f0 65 78 46 72 65 65 2c 0a 20 20 20 20 77 69 6e 4d  exFree,.    winM
99200 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 77  utexEnter,.    w
99210 69 6e 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20  inMutexTry,.    
99220 77 69 6e 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23  winMutexLeave,.#
99230 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
99240 55 47 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 48  UG.    winMutexH
99250 65 6c 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65  eld,.    winMute
99260 78 4e 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20  xNotheld.#else. 
99270 20 20 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64     0,.    0.#end
99280 69 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72  if.  };..  retur
99290 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e  n &sMutex;.}.#en
992a0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55  dif /* SQLITE_MU
992b0 54 45 58 5f 57 33 32 20 2a 2f 0a 0a 2f 2a 2a 2a  TEX_W32 */../***
992c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
992d0 6f 66 20 6d 75 74 65 78 5f 77 33 32 2e 63 20 2a  of mutex_w32.c *
992e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
992f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
99310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
99320 6e 20 66 69 6c 65 20 6d 61 6c 6c 6f 63 2e 63 20  n file malloc.c 
99330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
99360 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
99370 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
99380 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
99390 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
993a0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
993b0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
993c0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
993d0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
993e0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
993f0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
99400 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
99410 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
99420 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
99430 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
99440 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
99450 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
99460 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
99470 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
99480 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
99490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
994a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
994b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
994c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
994d0 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20  **.**.** Memory 
994e0 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74  allocation funct
994f0 69 6f 6e 73 20 75 73 65 64 20 74 68 72 6f 75 67  ions used throug
99500 68 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a 2a 0a  hout sqlite..**.
99510 2a 2a 20 24 49 64 3a 20 6d 61 6c 6c 6f 63 2e 63  ** $Id: malloc.c
99520 2c 76 20 31 2e 36 36 20 32 30 30 39 2f 30 37 2f  ,v 1.66 2009/07/
99530 31 37 20 31 31 3a 34 34 3a 30 37 20 64 72 68 20  17 11:44:07 drh 
99540 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
99550 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e  This routine run
99560 73 20 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f 72  s when the memor
99570 79 20 61 6c 6c 6f 63 61 74 6f 72 20 73 65 65 73  y allocator sees
99580 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 74 6f 74   that the.** tot
99590 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  al memory alloca
995a0 74 69 6f 6e 20 69 73 20 61 62 6f 75 74 20 74 6f  tion is about to
995b0 20 65 78 63 65 65 64 20 74 68 65 20 73 6f 66 74   exceed the soft
995c0 20 68 65 61 70 0a 2a 2a 20 6c 69 6d 69 74 2e 0a   heap.** limit..
995d0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
995e0 6f 66 74 48 65 61 70 4c 69 6d 69 74 45 6e 66 6f  oftHeapLimitEnfo
995f0 72 63 65 72 28 0a 20 20 76 6f 69 64 20 2a 4e 6f  rcer(.  void *No
99600 74 55 73 65 64 2c 20 0a 20 20 73 71 6c 69 74 65  tUsed, .  sqlite
99610 33 5f 69 6e 74 36 34 20 4e 6f 74 55 73 65 64 32  3_int64 NotUsed2
99620 2c 0a 20 20 69 6e 74 20 61 6c 6c 6f 63 53 69 7a  ,.  int allocSiz
99630 65 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  e.){.  UNUSED_PA
99640 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64  RAMETER2(NotUsed
99650 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 73  , NotUsed2);.  s
99660 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
99670 65 6d 6f 72 79 28 61 6c 6c 6f 63 53 69 7a 65 29  emory(allocSize)
99680 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  ;.}../*.** Set t
99690 68 65 20 73 6f 66 74 20 68 65 61 70 2d 73 69 7a  he soft heap-siz
996a0 65 20 6c 69 6d 69 74 20 66 6f 72 20 74 68 65 20  e limit for the 
996b0 6c 69 62 72 61 72 79 2e 20 50 61 73 73 69 6e 67  library. Passing
996c0 20 61 20 7a 65 72 6f 20 6f 72 20 0a 2a 2a 20 6e   a zero or .** n
996d0 65 67 61 74 69 76 65 20 76 61 6c 75 65 20 69 6e  egative value in
996e0 64 69 63 61 74 65 73 20 6e 6f 20 6c 69 6d 69 74  dicates no limit
996f0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
99700 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 6f 66  void sqlite3_sof
99710 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 69 6e 74  t_heap_limit(int
99720 20 6e 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 75   n){.  sqlite3_u
99730 69 6e 74 36 34 20 69 4c 69 6d 69 74 3b 0a 20 20  int64 iLimit;.  
99740 69 6e 74 20 6f 76 65 72 61 67 65 3b 0a 20 20 69  int overage;.  i
99750 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20 69 4c  f( n<0 ){.    iL
99760 69 6d 69 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  imit = 0;.  }els
99770 65 7b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20  e{.    iLimit = 
99780 6e 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  n;.  }.#ifndef S
99790 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
997a0 4e 49 54 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  NIT.  sqlite3_in
997b0 69 74 69 61 6c 69 7a 65 28 29 3b 0a 23 65 6e 64  itialize();.#end
997c0 69 66 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 3e  if.  if( iLimit>
997d0 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
997e0 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 73 6f 66 74  MemoryAlarm(soft
997f0 48 65 61 70 4c 69 6d 69 74 45 6e 66 6f 72 63 65  HeapLimitEnforce
99800 72 2c 20 30 2c 20 69 4c 69 6d 69 74 29 3b 0a 20  r, 0, iLimit);. 
99810 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
99820 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 30  te3MemoryAlarm(0
99830 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 6f  , 0, 0);.  }.  o
99840 76 65 72 61 67 65 20 3d 20 28 69 6e 74 29 28 73  verage = (int)(s
99850 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73  qlite3_memory_us
99860 65 64 28 29 20 2d 20 28 69 36 34 29 6e 29 3b 0a  ed() - (i64)n);.
99870 20 20 69 66 28 20 6f 76 65 72 61 67 65 3e 30 20    if( overage>0 
99880 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
99890 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 6f 76  elease_memory(ov
998a0 65 72 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  erage);.  }.}../
998b0 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20  *.** Attempt to 
998c0 72 65 6c 65 61 73 65 20 75 70 20 74 6f 20 6e 20  release up to n 
998d0 62 79 74 65 73 20 6f 66 20 6e 6f 6e 2d 65 73 73  bytes of non-ess
998e0 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 20 63 75  ential memory cu
998f0 72 72 65 6e 74 6c 79 0a 2a 2a 20 68 65 6c 64 20  rrently.** held 
99900 62 79 20 53 51 4c 69 74 65 2e 20 41 6e 20 65 78  by SQLite. An ex
99910 61 6d 70 6c 65 20 6f 66 20 6e 6f 6e 2d 65 73 73  ample of non-ess
99920 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 20 69 73  ential memory is
99930 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 0a   memory used to.
99940 2a 2a 20 63 61 63 68 65 20 64 61 74 61 62 61 73  ** cache databas
99950 65 20 70 61 67 65 73 20 74 68 61 74 20 61 72 65  e pages that are
99960 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 69   not currently i
99970 6e 20 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  n use..*/.SQLITE
99980 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
99990 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28  _release_memory(
999a0 69 6e 74 20 6e 29 7b 0a 23 69 66 64 65 66 20 53  int n){.#ifdef S
999b0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
999c0 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 20  ORY_MANAGEMENT. 
999d0 20 69 6e 74 20 6e 52 65 74 20 3d 20 30 3b 0a 23   int nRet = 0;.#
999e0 69 66 20 30 0a 20 20 6e 52 65 74 20 2b 3d 20 73  if 0.  nRet += s
999f0 71 6c 69 74 65 33 56 64 62 65 52 65 6c 65 61 73  qlite3VdbeReleas
99a00 65 4d 65 6d 6f 72 79 28 6e 29 3b 0a 23 65 6e 64  eMemory(n);.#end
99a10 69 66 0a 20 20 6e 52 65 74 20 2b 3d 20 73 71 6c  if.  nRet += sql
99a20 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73  ite3PcacheReleas
99a30 65 4d 65 6d 6f 72 79 28 6e 2d 6e 52 65 74 29 3b  eMemory(n-nRet);
99a40 0a 20 20 72 65 74 75 72 6e 20 6e 52 65 74 3b 0a  .  return nRet;.
99a50 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50  #else.  UNUSED_P
99a60 41 52 41 4d 45 54 45 52 28 6e 29 3b 0a 20 20 72  ARAMETER(n);.  r
99a70 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
99a80 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
99a90 20 53 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69   State informati
99aa0 6f 6e 20 6c 6f 63 61 6c 20 74 6f 20 74 68 65 20  on local to the 
99ab0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
99ac0 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a  n subsystem..*/.
99ad0 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53  static SQLITE_WS
99ae0 44 20 73 74 72 75 63 74 20 4d 65 6d 30 47 6c 6f  D struct Mem0Glo
99af0 62 61 6c 20 7b 0a 20 20 2f 2a 20 4e 75 6d 62 65  bal {.  /* Numbe
99b00 72 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 20  r of free pages 
99b10 66 6f 72 20 73 63 72 61 74 63 68 20 61 6e 64 20  for scratch and 
99b20 70 61 67 65 2d 63 61 63 68 65 20 6d 65 6d 6f 72  page-cache memor
99b30 79 20 2a 2f 0a 20 20 75 33 32 20 6e 53 63 72 61  y */.  u32 nScra
99b40 74 63 68 46 72 65 65 3b 0a 20 20 75 33 32 20 6e  tchFree;.  u32 n
99b50 50 61 67 65 46 72 65 65 3b 0a 0a 20 20 73 71 6c  PageFree;..  sql
99b60 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65  ite3_mutex *mute
99b70 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75  x;         /* Mu
99b80 74 65 78 20 74 6f 20 73 65 72 69 61 6c 69 7a 65  tex to serialize
99b90 20 61 63 63 65 73 73 20 2a 2f 0a 0a 20 20 2f 2a   access */..  /*
99ba0 0a 20 20 2a 2a 20 54 68 65 20 61 6c 61 72 6d 20  .  ** The alarm 
99bb0 63 61 6c 6c 62 61 63 6b 20 61 6e 64 20 69 74 73  callback and its
99bc0 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 54 68 65   arguments.  The
99bd0 20 6d 65 6d 30 2e 6d 75 74 65 78 20 6c 6f 63 6b   mem0.mutex lock
99be0 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 68 65   will.  ** be he
99bf0 6c 64 20 77 68 69 6c 65 20 74 68 65 20 63 61 6c  ld while the cal
99c00 6c 62 61 63 6b 20 69 73 20 72 75 6e 6e 69 6e 67  lback is running
99c10 2e 20 20 52 65 63 75 72 73 69 76 65 20 63 61 6c  .  Recursive cal
99c20 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65  ls into.  ** the
99c30 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65   memory subsyste
99c40 6d 20 61 72 65 20 61 6c 6c 6f 77 65 64 2c 20 62  m are allowed, b
99c50 75 74 20 6e 6f 20 6e 65 77 20 63 61 6c 6c 62 61  ut no new callba
99c60 63 6b 73 20 77 69 6c 6c 20 62 65 0a 20 20 2a 2a  cks will be.  **
99c70 20 69 73 73 75 65 64 2e 0a 20 20 2a 2f 0a 20 20   issued..  */.  
99c80 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 61 6c  sqlite3_int64 al
99c90 61 72 6d 54 68 72 65 73 68 6f 6c 64 3b 0a 20 20  armThreshold;.  
99ca0 76 6f 69 64 20 28 2a 61 6c 61 72 6d 43 61 6c 6c  void (*alarmCall
99cb0 62 61 63 6b 29 28 76 6f 69 64 2a 2c 20 73 71 6c  back)(void*, sql
99cc0 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74 29 3b  ite3_int64,int);
99cd0 0a 20 20 76 6f 69 64 20 2a 61 6c 61 72 6d 41 72  .  void *alarmAr
99ce0 67 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 6f  g;..  /*.  ** Po
99cf0 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 65 6e  inters to the en
99d00 64 20 6f 66 20 73 71 6c 69 74 65 33 47 6c 6f 62  d of sqlite3Glob
99d10 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63  alConfig.pScratc
99d20 68 20 61 6e 64 0a 20 20 2a 2a 20 73 71 6c 69 74  h and.  ** sqlit
99d30 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
99d40 50 61 67 65 20 74 6f 20 61 20 62 6c 6f 63 6b 20  Page to a block 
99d50 6f 66 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 72  of memory that r
99d60 65 63 6f 72 64 73 0a 20 20 2a 2a 20 77 68 69 63  ecords.  ** whic
99d70 68 20 70 61 67 65 73 20 61 72 65 20 61 76 61 69  h pages are avai
99d80 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 75 33  lable..  */.  u3
99d90 32 20 2a 61 53 63 72 61 74 63 68 46 72 65 65 3b  2 *aScratchFree;
99da0 0a 20 20 75 33 32 20 2a 61 50 61 67 65 46 72 65  .  u32 *aPageFre
99db0 65 3b 0a 7d 20 6d 65 6d 30 20 3d 20 7b 20 30 2c  e;.} mem0 = { 0,
99dc0 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
99dd0 30 2c 20 30 20 7d 3b 0a 0a 23 64 65 66 69 6e 65  0, 0 };..#define
99de0 20 6d 65 6d 30 20 47 4c 4f 42 41 4c 28 73 74 72   mem0 GLOBAL(str
99df0 75 63 74 20 4d 65 6d 30 47 6c 6f 62 61 6c 2c 20  uct Mem0Global, 
99e00 6d 65 6d 30 29 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69  mem0)../*.** Ini
99e10 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f  tialize the memo
99e20 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75  ry allocation su
99e30 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49  bsystem..*/.SQLI
99e40 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
99e50 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 49 6e 69 74  qlite3MallocInit
99e60 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71  (void){.  if( sq
99e70 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
99e80 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 3d 3d 30 20 29  g.m.xMalloc==0 )
99e90 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  {.    sqlite3Mem
99ea0 53 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20  SetDefault();.  
99eb0 7d 0a 20 20 6d 65 6d 73 65 74 28 26 6d 65 6d 30  }.  memset(&mem0
99ec0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 65 6d 30  , 0, sizeof(mem0
99ed0 29 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  ));.  if( sqlite
99ee0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43  3GlobalConfig.bC
99ef0 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20  oreMutex ){.    
99f00 6d 65 6d 30 2e 6d 75 74 65 78 20 3d 20 73 71 6c  mem0.mutex = sql
99f10 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
99f20 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
99f30 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 69  IC_MEM);.  }.  i
99f40 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
99f50 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 20  Config.pScratch 
99f60 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  && sqlite3Global
99f70 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68  Config.szScratch
99f80 3e 3d 31 30 30 0a 20 20 20 20 20 20 26 26 20 73  >=100.      && s
99f90 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
99fa0 69 67 2e 6e 53 63 72 61 74 63 68 3e 3d 30 20 29  ig.nScratch>=0 )
99fb0 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  {.    int i;.   
99fc0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
99fd0 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 20 3d  nfig.szScratch =
99fe0 20 52 4f 55 4e 44 44 4f 57 4e 38 28 73 71 6c 69   ROUNDDOWN8(sqli
99ff0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9a000 73 7a 53 63 72 61 74 63 68 2d 34 29 3b 0a 20 20  szScratch-4);.  
9a010 20 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46    mem0.aScratchF
9a020 72 65 65 20 3d 20 28 75 33 32 2a 29 26 28 28 63  ree = (u32*)&((c
9a030 68 61 72 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62  har*)sqlite3Glob
9a040 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63  alConfig.pScratc
9a050 68 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  h).             
9a060 20 20 20 20 20 5b 73 71 6c 69 74 65 33 47 6c 6f       [sqlite3Glo
9a070 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61  balConfig.szScra
9a080 74 63 68 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61  tch*sqlite3Globa
9a090 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68  lConfig.nScratch
9a0a0 5d 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ];.    for(i=0; 
9a0b0 69 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  i<sqlite3GlobalC
9a0c0 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 3b 20  onfig.nScratch; 
9a0d0 69 2b 2b 29 7b 20 6d 65 6d 30 2e 61 53 63 72 61  i++){ mem0.aScra
9a0e0 74 63 68 46 72 65 65 5b 69 5d 20 3d 20 69 3b 20  tchFree[i] = i; 
9a0f0 7d 0a 20 20 20 20 6d 65 6d 30 2e 6e 53 63 72 61  }.    mem0.nScra
9a100 74 63 68 46 72 65 65 20 3d 20 73 71 6c 69 74 65  tchFree = sqlite
9a110 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53  3GlobalConfig.nS
9a120 63 72 61 74 63 68 3b 0a 20 20 7d 65 6c 73 65 7b  cratch;.  }else{
9a130 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
9a140 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63  alConfig.pScratc
9a150 68 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74  h = 0;.    sqlit
9a160 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73  e3GlobalConfig.s
9a170 7a 53 63 72 61 74 63 68 20 3d 20 30 3b 0a 20 20  zScratch = 0;.  
9a180 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  }.  if( sqlite3G
9a190 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67  lobalConfig.pPag
9a1a0 65 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62  e && sqlite3Glob
9a1b0 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 3e  alConfig.szPage>
9a1c0 3d 35 31 32 0a 20 20 20 20 20 20 26 26 20 73 71  =512.      && sq
9a1d0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9a1e0 67 2e 6e 50 61 67 65 3e 3d 31 20 29 7b 0a 20 20  g.nPage>=1 ){.  
9a1f0 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 6e 74    int i;.    int
9a200 20 6f 76 65 72 68 65 61 64 3b 0a 20 20 20 20 69   overhead;.    i
9a210 6e 74 20 73 7a 20 3d 20 52 4f 55 4e 44 44 4f 57  nt sz = ROUNDDOW
9a220 4e 38 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  N8(sqlite3Global
9a230 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 29 3b 0a  Config.szPage);.
9a240 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69      int n = sqli
9a250 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9a260 6e 50 61 67 65 3b 0a 20 20 20 20 6f 76 65 72 68  nPage;.    overh
9a270 65 61 64 20 3d 20 28 34 2a 6e 20 2b 20 73 7a 20  ead = (4*n + sz 
9a280 2d 20 31 29 2f 73 7a 3b 0a 20 20 20 20 73 71 6c  - 1)/sz;.    sql
9a290 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9a2a0 2e 6e 50 61 67 65 20 2d 3d 20 6f 76 65 72 68 65  .nPage -= overhe
9a2b0 61 64 3b 0a 20 20 20 20 6d 65 6d 30 2e 61 50 61  ad;.    mem0.aPa
9a2c0 67 65 46 72 65 65 20 3d 20 28 75 33 32 2a 29 26  geFree = (u32*)&
9a2d0 28 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 47  ((char*)sqlite3G
9a2e0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67  lobalConfig.pPag
9a2f0 65 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e).             
9a300 20 20 20 20 20 5b 73 71 6c 69 74 65 33 47 6c 6f       [sqlite3Glo
9a310 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65  balConfig.szPage
9a320 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  *sqlite3GlobalCo
9a330 6e 66 69 67 2e 6e 50 61 67 65 5d 3b 0a 20 20 20  nfig.nPage];.   
9a340 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 71 6c 69   for(i=0; i<sqli
9a350 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9a360 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 6d 65 6d  nPage; i++){ mem
9a370 30 2e 61 50 61 67 65 46 72 65 65 5b 69 5d 20 3d  0.aPageFree[i] =
9a380 20 69 3b 20 7d 0a 20 20 20 20 6d 65 6d 30 2e 6e   i; }.    mem0.n
9a390 50 61 67 65 46 72 65 65 20 3d 20 73 71 6c 69 74  PageFree = sqlit
9a3a0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
9a3b0 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Page;.  }else{. 
9a3c0 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
9a3d0 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 3d 20 30  Config.pPage = 0
9a3e0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  ;.    sqlite3Glo
9a3f0 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65  balConfig.szPage
9a400 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 0;.  }.  retu
9a410 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  rn sqlite3Global
9a420 43 6f 6e 66 69 67 2e 6d 2e 78 49 6e 69 74 28 73  Config.m.xInit(s
9a430 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9a440 69 67 2e 6d 2e 70 41 70 70 44 61 74 61 29 3b 0a  ig.m.pAppData);.
9a450 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69  }../*.** Deiniti
9a460 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79  alize the memory
9a470 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73   allocation subs
9a480 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ystem..*/.SQLITE
9a490 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
9a4a0 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 28 76  lite3MallocEnd(v
9a4b0 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69  oid){.  if( sqli
9a4c0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9a4d0 6d 2e 78 53 68 75 74 64 6f 77 6e 20 29 7b 0a 20  m.xShutdown ){. 
9a4e0 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
9a4f0 43 6f 6e 66 69 67 2e 6d 2e 78 53 68 75 74 64 6f  Config.m.xShutdo
9a500 77 6e 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  wn(sqlite3Global
9a510 43 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44 61 74  Config.m.pAppDat
9a520 61 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74  a);.  }.  memset
9a530 28 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a 65 6f  (&mem0, 0, sizeo
9a540 66 28 6d 65 6d 30 29 29 3b 0a 7d 0a 0a 2f 2a 0a  f(mem0));.}../*.
9a550 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d  ** Return the am
9a560 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63  ount of memory c
9a570 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64  urrently checked
9a580 20 6f 75 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   out..*/.SQLITE_
9a590 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  API sqlite3_int6
9a5a0 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79  4 sqlite3_memory
9a5b0 5f 75 73 65 64 28 76 6f 69 64 29 7b 0a 20 20 69  _used(void){.  i
9a5c0 6e 74 20 6e 2c 20 6d 78 3b 0a 20 20 73 71 6c 69  nt n, mx;.  sqli
9a5d0 74 65 33 5f 69 6e 74 36 34 20 72 65 73 3b 0a 20  te3_int64 res;. 
9a5e0 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28   sqlite3_status(
9a5f0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45  SQLITE_STATUS_ME
9a600 4d 4f 52 59 5f 55 53 45 44 2c 20 26 6e 2c 20 26  MORY_USED, &n, &
9a610 6d 78 2c 20 30 29 3b 0a 20 20 72 65 73 20 3d 20  mx, 0);.  res = 
9a620 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 6e  (sqlite3_int64)n
9a630 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f 75 6e  ;  /* Work aroun
9a640 64 20 62 75 67 20 69 6e 20 42 6f 72 6c 61 6e 64  d bug in Borland
9a650 20 43 2e 20 54 69 63 6b 65 74 20 23 33 32 31 36   C. Ticket #3216
9a660 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 65 73   */.  return res
9a670 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
9a680 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d  n the maximum am
9a690 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 74  ount of memory t
9a6a0 68 61 74 20 68 61 73 20 65 76 65 72 20 62 65 65  hat has ever bee
9a6b0 6e 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75 74  n.** checked out
9a6c0 20 73 69 6e 63 65 20 65 69 74 68 65 72 20 74 68   since either th
9a6d0 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74  e beginning of t
9a6e0 68 69 73 20 70 72 6f 63 65 73 73 0a 2a 2a 20 6f  his process.** o
9a6f0 72 20 73 69 6e 63 65 20 74 68 65 20 6d 6f 73 74  r since the most
9a700 20 72 65 63 65 6e 74 20 72 65 73 65 74 2e 0a 2a   recent reset..*
9a710 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  /.SQLITE_API sql
9a720 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74  ite3_int64 sqlit
9a730 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61  e3_memory_highwa
9a740 74 65 72 28 69 6e 74 20 72 65 73 65 74 46 6c 61  ter(int resetFla
9a750 67 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6d 78 3b  g){.  int n, mx;
9a760 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
9a770 20 72 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f   res;.  sqlite3_
9a780 73 74 61 74 75 73 28 53 51 4c 49 54 45 5f 53 54  status(SQLITE_ST
9a790 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44  ATUS_MEMORY_USED
9a7a0 2c 20 26 6e 2c 20 26 6d 78 2c 20 72 65 73 65 74  , &n, &mx, reset
9a7b0 46 6c 61 67 29 3b 0a 20 20 72 65 73 20 3d 20 28  Flag);.  res = (
9a7c0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 6d 78  sqlite3_int64)mx
9a7d0 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f 75 6e  ;  /* Work aroun
9a7e0 64 20 62 75 67 20 69 6e 20 42 6f 72 6c 61 6e 64  d bug in Borland
9a7f0 20 43 2e 20 54 69 63 6b 65 74 20 23 33 32 31 36   C. Ticket #3216
9a800 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 65 73   */.  return res
9a810 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  ;.}../*.** Chang
9a820 65 20 74 68 65 20 61 6c 61 72 6d 20 63 61 6c 6c  e the alarm call
9a830 62 61 63 6b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  back.*/.SQLITE_P
9a840 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
9a850 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 0a 20  e3MemoryAlarm(. 
9a860 20 76 6f 69 64 28 2a 78 43 61 6c 6c 62 61 63 6b   void(*xCallback
9a870 29 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 73 71  )(void *pArg, sq
9a880 6c 69 74 65 33 5f 69 6e 74 36 34 20 75 73 65 64  lite3_int64 used
9a890 2c 69 6e 74 20 4e 29 2c 0a 20 20 76 6f 69 64 20  ,int N),.  void 
9a8a0 2a 70 41 72 67 2c 0a 20 20 73 71 6c 69 74 65 33  *pArg,.  sqlite3
9a8b0 5f 69 6e 74 36 34 20 69 54 68 72 65 73 68 6f 6c  _int64 iThreshol
9a8c0 64 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  d.){.  sqlite3_m
9a8d0 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e  utex_enter(mem0.
9a8e0 6d 75 74 65 78 29 3b 0a 20 20 6d 65 6d 30 2e 61  mutex);.  mem0.a
9a8f0 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 3d 20 78  larmCallback = x
9a900 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6d 65 6d 30  Callback;.  mem0
9a910 2e 61 6c 61 72 6d 41 72 67 20 3d 20 70 41 72 67  .alarmArg = pArg
9a920 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68  ;.  mem0.alarmTh
9a930 72 65 73 68 6f 6c 64 20 3d 20 69 54 68 72 65 73  reshold = iThres
9a940 68 6f 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f  hold;.  sqlite3_
9a950 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30  mutex_leave(mem0
9a960 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  .mutex);.  retur
9a970 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
9a980 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
9a990 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f  MIT_DEPRECATED./
9a9a0 2a 0a 2a 2a 20 44 65 70 72 65 63 61 74 65 64 20  *.** Deprecated 
9a9b0 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66 61  external interfa
9a9c0 63 65 2e 20 20 49 6e 74 65 72 6e 61 6c 2f 63 6f  ce.  Internal/co
9a9d0 72 65 20 53 51 4c 69 74 65 20 63 6f 64 65 0a 2a  re SQLite code.*
9a9e0 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73 71  * should call sq
9a9f0 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d  lite3MemoryAlarm
9aa00 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
9aa10 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f  int sqlite3_memo
9aa20 72 79 5f 61 6c 61 72 6d 28 0a 20 20 76 6f 69 64  ry_alarm(.  void
9aa30 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69  (*xCallback)(voi
9aa40 64 20 2a 70 41 72 67 2c 20 73 71 6c 69 74 65 33  d *pArg, sqlite3
9aa50 5f 69 6e 74 36 34 20 75 73 65 64 2c 69 6e 74 20  _int64 used,int 
9aa60 4e 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67  N),.  void *pArg
9aa70 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ,.  sqlite3_int6
9aa80 34 20 69 54 68 72 65 73 68 6f 6c 64 0a 29 7b 0a  4 iThreshold.){.
9aa90 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
9aaa0 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 78 43 61 6c  MemoryAlarm(xCal
9aab0 6c 62 61 63 6b 2c 20 70 41 72 67 2c 20 69 54 68  lback, pArg, iTh
9aac0 72 65 73 68 6f 6c 64 29 3b 0a 7d 0a 23 65 6e 64  reshold);.}.#end
9aad0 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 69 67 67 65  if../*.** Trigge
9aae0 72 20 74 68 65 20 61 6c 61 72 6d 20 0a 2a 2f 0a  r the alarm .*/.
9aaf0 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
9ab00 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 69  te3MallocAlarm(i
9ab10 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 76 6f 69  nt nByte){.  voi
9ab20 64 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76  d (*xCallback)(v
9ab30 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74  oid*,sqlite3_int
9ab40 36 34 2c 69 6e 74 29 3b 0a 20 20 73 71 6c 69 74  64,int);.  sqlit
9ab50 65 33 5f 69 6e 74 36 34 20 6e 6f 77 55 73 65 64  e3_int64 nowUsed
9ab60 3b 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 3b 0a  ;.  void *pArg;.
9ab70 20 20 69 66 28 20 6d 65 6d 30 2e 61 6c 61 72 6d    if( mem0.alarm
9ab80 43 61 6c 6c 62 61 63 6b 3d 3d 30 20 29 20 72 65  Callback==0 ) re
9ab90 74 75 72 6e 3b 0a 20 20 78 43 61 6c 6c 62 61 63  turn;.  xCallbac
9aba0 6b 20 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61  k = mem0.alarmCa
9abb0 6c 6c 62 61 63 6b 3b 0a 20 20 6e 6f 77 55 73 65  llback;.  nowUse
9abc0 64 20 3d 20 73 71 6c 69 74 65 33 53 74 61 74 75  d = sqlite3Statu
9abd0 73 56 61 6c 75 65 28 53 51 4c 49 54 45 5f 53 54  sValue(SQLITE_ST
9abe0 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44  ATUS_MEMORY_USED
9abf0 29 3b 0a 20 20 70 41 72 67 20 3d 20 6d 65 6d 30  );.  pArg = mem0
9ac00 2e 61 6c 61 72 6d 41 72 67 3b 0a 20 20 6d 65 6d  .alarmArg;.  mem
9ac10 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20  0.alarmCallback 
9ac20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  = 0;.  sqlite3_m
9ac30 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e  utex_leave(mem0.
9ac40 6d 75 74 65 78 29 3b 0a 20 20 78 43 61 6c 6c 62  mutex);.  xCallb
9ac50 61 63 6b 28 70 41 72 67 2c 20 6e 6f 77 55 73 65  ack(pArg, nowUse
9ac60 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 73 71 6c  d, nByte);.  sql
9ac70 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
9ac80 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
9ac90 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61  mem0.alarmCallba
9aca0 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a  ck = xCallback;.
9acb0 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 67 20    mem0.alarmArg 
9acc0 3d 20 70 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  = pArg;.}../*.**
9acd0 20 44 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c   Do a memory all
9ace0 6f 63 61 74 69 6f 6e 20 77 69 74 68 20 73 74 61  ocation with sta
9acf0 74 69 73 74 69 63 73 20 61 6e 64 20 61 6c 61 72  tistics and alar
9ad00 6d 73 2e 20 20 41 73 73 75 6d 65 20 74 68 65 0a  ms.  Assume the.
9ad10 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61  ** lock is alrea
9ad20 64 79 20 68 65 6c 64 2e 0a 2a 2f 0a 73 74 61 74  dy held..*/.stat
9ad30 69 63 20 69 6e 74 20 6d 61 6c 6c 6f 63 57 69 74  ic int mallocWit
9ad40 68 41 6c 61 72 6d 28 69 6e 74 20 6e 2c 20 76 6f  hAlarm(int n, vo
9ad50 69 64 20 2a 2a 70 70 29 7b 0a 20 20 69 6e 74 20  id **pp){.  int 
9ad60 6e 46 75 6c 6c 3b 0a 20 20 76 6f 69 64 20 2a 70  nFull;.  void *p
9ad70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
9ad80 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d  te3_mutex_held(m
9ad90 65 6d 30 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20  em0.mutex) );.  
9ada0 6e 46 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47  nFull = sqlite3G
9adb0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52  lobalConfig.m.xR
9adc0 6f 75 6e 64 75 70 28 6e 29 3b 0a 20 20 73 71 6c  oundup(n);.  sql
9add0 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51  ite3StatusSet(SQ
9ade0 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c  LITE_STATUS_MALL
9adf0 4f 43 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 69  OC_SIZE, n);.  i
9ae00 66 28 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c  f( mem0.alarmCal
9ae10 6c 62 61 63 6b 21 3d 30 20 29 7b 0a 20 20 20 20  lback!=0 ){.    
9ae20 69 6e 74 20 6e 55 73 65 64 20 3d 20 73 71 6c 69  int nUsed = sqli
9ae30 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 53  te3StatusValue(S
9ae40 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d  QLITE_STATUS_MEM
9ae50 4f 52 59 5f 55 53 45 44 29 3b 0a 20 20 20 20 69  ORY_USED);.    i
9ae60 66 28 20 6e 55 73 65 64 2b 6e 46 75 6c 6c 20 3e  f( nUsed+nFull >
9ae70 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 72 65  = mem0.alarmThre
9ae80 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20 20 20 73  shold ){.      s
9ae90 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72  qlite3MallocAlar
9aea0 6d 28 6e 46 75 6c 6c 29 3b 0a 20 20 20 20 7d 0a  m(nFull);.    }.
9aeb0 20 20 7d 0a 20 20 70 20 3d 20 73 71 6c 69 74 65    }.  p = sqlite
9aec0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
9aed0 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a  xMalloc(nFull);.
9aee0 20 20 69 66 28 20 70 3d 3d 30 20 26 26 20 6d 65    if( p==0 && me
9aef0 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b  m0.alarmCallback
9af00 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d   ){.    sqlite3M
9af10 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 46 75 6c 6c  allocAlarm(nFull
9af20 29 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74  );.    p = sqlit
9af30 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
9af40 2e 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b  .xMalloc(nFull);
9af50 0a 20 20 7d 0a 20 20 69 66 28 20 70 20 29 7b 0a  .  }.  if( p ){.
9af60 20 20 20 20 6e 46 75 6c 6c 20 3d 20 73 71 6c 69      nFull = sqli
9af70 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29  te3MallocSize(p)
9af80 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61  ;.    sqlite3Sta
9af90 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
9afa0 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44  ATUS_MEMORY_USED
9afb0 2c 20 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20 20  , nFull);.  }.  
9afc0 2a 70 70 20 3d 20 70 3b 0a 20 20 72 65 74 75 72  *pp = p;.  retur
9afd0 6e 20 6e 46 75 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a  n nFull;.}../*.*
9afe0 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72  * Allocate memor
9aff0 79 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  y.  This routine
9b000 20 69 73 20 6c 69 6b 65 20 73 71 6c 69 74 65 33   is like sqlite3
9b010 5f 6d 61 6c 6c 6f 63 28 29 20 65 78 63 65 70 74  _malloc() except
9b020 20 74 68 61 74 20 69 74 0a 2a 2a 20 61 73 73 75   that it.** assu
9b030 6d 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 73  mes the memory s
9b040 75 62 73 79 73 74 65 6d 20 68 61 73 20 61 6c 72  ubsystem has alr
9b050 65 61 64 79 20 62 65 65 6e 20 69 6e 69 74 69 61  eady been initia
9b060 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  lized..*/.SQLITE
9b070 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
9b080 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 69 6e 74  qlite3Malloc(int
9b090 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a   n){.  void *p;.
9b0a0 20 20 69 66 28 20 6e 3c 3d 30 20 7c 7c 20 6e 3e    if( n<=0 || n>
9b0b0 3d 30 78 37 66 66 66 66 66 30 30 20 29 7b 0a 20  =0x7fffff00 ){. 
9b0c0 20 20 20 2f 2a 20 41 20 6d 65 6d 6f 72 79 20 61     /* A memory a
9b0d0 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 20 6e  llocation of a n
9b0e0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 77  umber of bytes w
9b0f0 68 69 63 68 20 69 73 20 6e 65 61 72 20 74 68 65  hich is near the
9b100 20 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a 20   maximum.    ** 
9b110 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 76  signed integer v
9b120 61 6c 75 65 20 6d 69 67 68 74 20 63 61 75 73 65  alue might cause
9b130 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 76 65 72   an integer over
9b140 66 6c 6f 77 20 69 6e 73 69 64 65 20 6f 66 20 74  flow inside of t
9b150 68 65 0a 20 20 20 20 2a 2a 20 78 4d 61 6c 6c 6f  he.    ** xMallo
9b160 63 28 29 2e 20 20 48 65 6e 63 65 20 77 65 20 6c  c().  Hence we l
9b170 69 6d 69 74 20 74 68 65 20 6d 61 78 69 6d 75 6d  imit the maximum
9b180 20 73 69 7a 65 20 74 6f 20 30 78 37 66 66 66 66   size to 0x7ffff
9b190 66 30 30 2c 20 67 69 76 69 6e 67 0a 20 20 20 20  f00, giving.    
9b1a0 2a 2a 20 32 35 35 20 62 79 74 65 73 20 6f 66 20  ** 255 bytes of 
9b1b0 6f 76 65 72 68 65 61 64 2e 20 20 53 51 4c 69 74  overhead.  SQLit
9b1c0 65 20 69 74 73 65 6c 66 20 77 69 6c 6c 20 6e 65  e itself will ne
9b1d0 76 65 72 20 75 73 65 20 61 6e 79 74 68 69 6e 67  ver use anything
9b1e0 20 6e 65 61 72 0a 20 20 20 20 2a 2a 20 74 68 69   near.    ** thi
9b1f0 73 20 61 6d 6f 75 6e 74 2e 20 20 54 68 65 20 6f  s amount.  The o
9b200 6e 6c 79 20 77 61 79 20 74 6f 20 72 65 61 63 68  nly way to reach
9b210 20 74 68 65 20 6c 69 6d 69 74 20 69 73 20 77 69   the limit is wi
9b220 74 68 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  th sqlite3_mallo
9b230 63 28 29 20 2a 2f 0a 20 20 20 20 70 20 3d 20 30  c() */.    p = 0
9b240 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71  ;.  }else if( sq
9b250 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9b260 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20  g.bMemstat ){.  
9b270 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
9b280 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78  enter(mem0.mutex
9b290 29 3b 0a 20 20 20 20 6d 61 6c 6c 6f 63 57 69 74  );.    mallocWit
9b2a0 68 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b 0a 20  hAlarm(n, &p);. 
9b2b0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
9b2c0 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65  _leave(mem0.mute
9b2d0 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  x);.  }else{.   
9b2e0 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62   p = sqlite3Glob
9b2f0 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c  alConfig.m.xMall
9b300 6f 63 28 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74  oc(n);.  }.  ret
9b310 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
9b320 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20  This version of 
9b330 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
9b340 61 74 69 6f 6e 20 69 73 20 66 6f 72 20 75 73 65  ation is for use
9b350 20 62 79 20 74 68 65 20 61 70 70 6c 69 63 61 74   by the applicat
9b360 69 6f 6e 2e 0a 2a 2a 20 46 69 72 73 74 20 6d 61  ion..** First ma
9b370 6b 65 20 73 75 72 65 20 74 68 65 20 6d 65 6d 6f  ke sure the memo
9b380 72 79 20 73 75 62 73 79 73 74 65 6d 20 69 73 20  ry subsystem is 
9b390 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68 65  initialized, the
9b3a0 6e 20 64 6f 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f  n do the.** allo
9b3b0 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  cation..*/.SQLIT
9b3c0 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
9b3d0 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74 20 6e  te3_malloc(int n
9b3e0 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ){.#ifndef SQLIT
9b3f0 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a  E_OMIT_AUTOINIT.
9b400 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e    if( sqlite3_in
9b410 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74  itialize() ) ret
9b420 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20  urn 0;.#endif.  
9b430 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4d 61  return sqlite3Ma
9b440 6c 6c 6f 63 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  lloc(n);.}../*.*
9b450 2a 20 45 61 63 68 20 74 68 72 65 61 64 20 6d 61  * Each thread ma
9b460 79 20 6f 6e 6c 79 20 68 61 76 65 20 61 20 73 69  y only have a si
9b470 6e 67 6c 65 20 6f 75 74 73 74 61 6e 64 69 6e 67  ngle outstanding
9b480 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 72 6f 6d   allocation from
9b490 0a 2a 2a 20 78 53 63 72 61 74 63 68 4d 61 6c 6c  .** xScratchMall
9b4a0 6f 63 28 29 2e 20 20 57 65 20 76 65 72 69 66 79  oc().  We verify
9b4b0 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74   this constraint
9b4c0 20 69 6e 20 74 68 65 20 73 69 6e 67 6c 65 2d 74   in the single-t
9b4d0 68 72 65 61 64 65 64 0a 2a 2a 20 63 61 73 65 20  hreaded.** case 
9b4e0 62 79 20 73 65 74 74 69 6e 67 20 73 63 72 61 74  by setting scrat
9b4f0 63 68 41 6c 6c 6f 63 4f 75 74 20 74 6f 20 31 20  chAllocOut to 1 
9b500 77 68 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 69  when an allocati
9b510 6f 6e 0a 2a 2a 20 69 73 20 6f 75 74 73 74 61 6e  on.** is outstan
9b520 64 69 6e 67 20 63 6c 65 61 72 69 6e 67 20 69 74  ding clearing it
9b530 20 77 68 65 6e 20 74 68 65 20 61 6c 6c 6f 63 61   when the alloca
9b540 74 69 6f 6e 20 69 73 20 66 72 65 65 64 2e 0a 2a  tion is freed..*
9b550 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  /.#if SQLITE_THR
9b560 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64  EADSAFE==0 && !d
9b570 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 73  efined(NDEBUG).s
9b580 74 61 74 69 63 20 69 6e 74 20 73 63 72 61 74 63  tatic int scratc
9b590 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 30 3b 0a 23  hAllocOut = 0;.#
9b5a0 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c  endif.../*.** Al
9b5b0 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 74 68  locate memory th
9b5c0 61 74 20 69 73 20 74 6f 20 62 65 20 75 73 65 64  at is to be used
9b5d0 20 61 6e 64 20 72 65 6c 65 61 73 65 64 20 72 69   and released ri
9b5e0 67 68 74 20 61 77 61 79 2e 0a 2a 2a 20 54 68 69  ght away..** Thi
9b5f0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 73 69 6d  s routine is sim
9b600 69 6c 61 72 20 74 6f 20 61 6c 6c 6f 63 61 28 29  ilar to alloca()
9b610 20 69 6e 20 74 68 61 74 20 69 74 20 69 73 20 6e   in that it is n
9b620 6f 74 20 69 6e 74 65 6e 64 65 64 0a 2a 2a 20 66  ot intended.** f
9b630 6f 72 20 73 69 74 75 61 74 69 6f 6e 73 20 77 68  or situations wh
9b640 65 72 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 6d  ere the memory m
9b650 69 67 68 74 20 62 65 20 68 65 6c 64 20 6c 6f 6e  ight be held lon
9b660 67 2d 74 65 72 6d 2e 20 20 54 68 69 73 0a 2a 2a  g-term.  This.**
9b670 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e 74 65   routine is inte
9b680 6e 64 65 64 20 74 6f 20 67 65 74 20 6d 65 6d 6f  nded to get memo
9b690 72 79 20 74 6f 20 6f 6c 64 20 6c 61 72 67 65 20  ry to old large 
9b6a0 74 72 61 6e 73 69 65 6e 74 20 64 61 74 61 0a 2a  transient data.*
9b6b0 2a 20 73 74 72 75 63 74 75 72 65 73 20 74 68 61  * structures tha
9b6c0 74 20 77 6f 75 6c 64 20 6e 6f 74 20 6e 6f 72 6d  t would not norm
9b6d0 61 6c 6c 79 20 66 69 74 20 6f 6e 20 74 68 65 20  ally fit on the 
9b6e0 73 74 61 63 6b 20 6f 66 20 61 6e 0a 2a 2a 20 65  stack of an.** e
9b6f0 6d 62 65 64 64 65 64 20 70 72 6f 63 65 73 73 6f  mbedded processo
9b700 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
9b710 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
9b720 65 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28  e3ScratchMalloc(
9b730 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a  int n){.  void *
9b740 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30  p;.  assert( n>0
9b750 20 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f   );..#if SQLITE_
9b760 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26  THREADSAFE==0 &&
9b770 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47   !defined(NDEBUG
9b780 29 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68  ).  /* Verify th
9b790 61 74 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20  at no more than 
9b7a0 6f 6e 65 20 73 63 72 61 74 63 68 20 61 6c 6c 6f  one scratch allo
9b7b0 63 61 74 69 6f 6e 20 70 65 72 20 74 68 72 65 61  cation per threa
9b7c0 64 0a 20 20 2a 2a 20 69 73 20 6f 75 74 73 74 61  d.  ** is outsta
9b7d0 6e 64 69 6e 67 20 61 74 20 6f 6e 65 20 74 69 6d  nding at one tim
9b7e0 65 2e 20 20 28 54 68 69 73 20 69 73 20 6f 6e 6c  e.  (This is onl
9b7f0 79 20 63 68 65 63 6b 65 64 20 69 6e 20 74 68 65  y checked in the
9b800 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 2d 74 68 72  .  ** single-thr
9b810 65 61 64 65 64 20 63 61 73 65 20 73 69 6e 63 65  eaded case since
9b820 20 63 68 65 63 6b 69 6e 67 20 69 6e 20 74 68 65   checking in the
9b830 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20   multi-threaded 
9b840 63 61 73 65 0a 20 20 2a 2a 20 77 6f 75 6c 64 20  case.  ** would 
9b850 62 65 20 6d 75 63 68 20 6d 6f 72 65 20 63 6f 6d  be much more com
9b860 70 6c 69 63 61 74 65 64 2e 29 20 2a 2f 0a 20 20  plicated.) */.  
9b870 61 73 73 65 72 74 28 20 73 63 72 61 74 63 68 41  assert( scratchA
9b880 6c 6c 6f 63 4f 75 74 3d 3d 30 20 29 3b 0a 23 65  llocOut==0 );.#e
9b890 6e 64 69 66 0a 0a 20 20 69 66 28 20 73 71 6c 69  ndif..  if( sqli
9b8a0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9b8b0 73 7a 53 63 72 61 74 63 68 3c 6e 20 29 7b 0a 20  szScratch<n ){. 
9b8c0 20 20 20 67 6f 74 6f 20 73 63 72 61 74 63 68 5f     goto scratch_
9b8d0 6f 76 65 72 66 6c 6f 77 3b 0a 20 20 7d 65 6c 73  overflow;.  }els
9b8e0 65 7b 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33  e{  .    sqlite3
9b8f0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d  _mutex_enter(mem
9b900 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 69 66  0.mutex);.    if
9b910 28 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46  ( mem0.nScratchF
9b920 72 65 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ree==0 ){.      
9b930 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
9b940 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b  ave(mem0.mutex);
9b950 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 63 72 61  .      goto scra
9b960 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20  tch_overflow;.  
9b970 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
9b980 6e 74 20 69 3b 0a 20 20 20 20 20 20 69 20 3d 20  nt i;.      i = 
9b990 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72 65  mem0.aScratchFre
9b9a0 65 5b 2d 2d 6d 65 6d 30 2e 6e 53 63 72 61 74 63  e[--mem0.nScratc
9b9b0 68 46 72 65 65 5d 3b 0a 20 20 20 20 20 20 69 20  hFree];.      i 
9b9c0 2a 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  *= sqlite3Global
9b9d0 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68  Config.szScratch
9b9e0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  ;.      sqlite3S
9b9f0 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f  tatusAdd(SQLITE_
9ba00 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55  STATUS_SCRATCH_U
9ba10 53 45 44 2c 20 31 29 3b 0a 20 20 20 20 20 20 73  SED, 1);.      s
9ba20 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28  qlite3StatusSet(
9ba30 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43  SQLITE_STATUS_SC
9ba40 52 41 54 43 48 5f 53 49 5a 45 2c 20 6e 29 3b 0a  RATCH_SIZE, n);.
9ba50 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
9ba60 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d  tex_leave(mem0.m
9ba70 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70 20 3d  utex);.      p =
9ba80 20 28 76 6f 69 64 2a 29 26 28 28 63 68 61 72 2a   (void*)&((char*
9ba90 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  )sqlite3GlobalCo
9baa0 6e 66 69 67 2e 70 53 63 72 61 74 63 68 29 5b 69  nfig.pScratch)[i
9bab0 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ];.      assert(
9bac0 20 20 28 28 28 75 38 2a 29 70 20 2d 20 28 75 38    (((u8*)p - (u8
9bad0 2a 29 30 29 20 26 20 37 29 3d 3d 30 20 29 3b 0a  *)0) & 7)==0 );.
9bae0 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 20 53 51      }.  }.#if SQ
9baf0 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d  LITE_THREADSAFE=
9bb00 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e  =0 && !defined(N
9bb10 44 45 42 55 47 29 0a 20 20 73 63 72 61 74 63 68  DEBUG).  scratch
9bb20 41 6c 6c 6f 63 4f 75 74 20 3d 20 70 21 3d 30 3b  AllocOut = p!=0;
9bb30 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72  .#endif..  retur
9bb40 6e 20 70 3b 0a 0a 73 63 72 61 74 63 68 5f 6f 76  n p;..scratch_ov
9bb50 65 72 66 6c 6f 77 3a 0a 20 20 69 66 28 20 73 71  erflow:.  if( sq
9bb60 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9bb70 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20  g.bMemstat ){.  
9bb80 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
9bb90 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78  enter(mem0.mutex
9bba0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74  );.    sqlite3St
9bbb0 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53  atusSet(SQLITE_S
9bbc0 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49  TATUS_SCRATCH_SI
9bbd0 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 6e 20 3d 20  ZE, n);.    n = 
9bbe0 6d 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d 28  mallocWithAlarm(
9bbf0 6e 2c 20 26 70 29 3b 0a 20 20 20 20 69 66 28 20  n, &p);.    if( 
9bc00 70 20 29 20 73 71 6c 69 74 65 33 53 74 61 74 75  p ) sqlite3Statu
9bc10 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54  sAdd(SQLITE_STAT
9bc20 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46  US_SCRATCH_OVERF
9bc30 4c 4f 57 2c 20 6e 29 3b 0a 20 20 20 20 73 71 6c  LOW, n);.    sql
9bc40 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
9bc50 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
9bc60 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 73  }else{.    p = s
9bc70 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9bc80 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 29 3b  ig.m.xMalloc(n);
9bc90 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f  .  }.#if SQLITE_
9bca0 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26  THREADSAFE==0 &&
9bcb0 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47   !defined(NDEBUG
9bcc0 29 0a 20 20 73 63 72 61 74 63 68 41 6c 6c 6f 63  ).  scratchAlloc
9bcd0 4f 75 74 20 3d 20 70 21 3d 30 3b 0a 23 65 6e 64  Out = p!=0;.#end
9bce0 69 66 0a 20 20 72 65 74 75 72 6e 20 70 3b 20 20  if.  return p;  
9bcf0 20 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56    .}.SQLITE_PRIV
9bd00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
9bd10 53 63 72 61 74 63 68 46 72 65 65 28 76 6f 69 64  ScratchFree(void
9bd20 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b   *p){.  if( p ){
9bd30 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  ..#if SQLITE_THR
9bd40 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64  EADSAFE==0 && !d
9bd50 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 20  efined(NDEBUG). 
9bd60 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61     /* Verify tha
9bd70 74 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f  t no more than o
9bd80 6e 65 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63  ne scratch alloc
9bd90 61 74 69 6f 6e 20 70 65 72 20 74 68 72 65 61 64  ation per thread
9bda0 0a 20 20 20 20 2a 2a 20 69 73 20 6f 75 74 73 74  .    ** is outst
9bdb0 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65 20 74 69  anding at one ti
9bdc0 6d 65 2e 20 20 28 54 68 69 73 20 69 73 20 6f 6e  me.  (This is on
9bdd0 6c 79 20 63 68 65 63 6b 65 64 20 69 6e 20 74 68  ly checked in th
9bde0 65 0a 20 20 20 20 2a 2a 20 73 69 6e 67 6c 65 2d  e.    ** single-
9bdf0 74 68 72 65 61 64 65 64 20 63 61 73 65 20 73 69  threaded case si
9be00 6e 63 65 20 63 68 65 63 6b 69 6e 67 20 69 6e 20  nce checking in 
9be10 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 64  the multi-thread
9be20 65 64 20 63 61 73 65 0a 20 20 20 20 2a 2a 20 77  ed case.    ** w
9be30 6f 75 6c 64 20 62 65 20 6d 75 63 68 20 6d 6f 72  ould be much mor
9be40 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 29 20  e complicated.) 
9be50 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  */.    assert( s
9be60 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 3d 3d  cratchAllocOut==
9be70 31 20 29 3b 0a 20 20 20 20 73 63 72 61 74 63 68  1 );.    scratch
9be80 41 6c 6c 6f 63 4f 75 74 20 3d 20 30 3b 0a 23 65  AllocOut = 0;.#e
9be90 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20 73 71  ndif..    if( sq
9bea0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9beb0 67 2e 70 53 63 72 61 74 63 68 3d 3d 30 0a 20 20  g.pScratch==0.  
9bec0 20 20 20 20 20 20 20 20 20 7c 7c 20 70 3c 73 71           || p<sq
9bed0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9bee0 67 2e 70 53 63 72 61 74 63 68 0a 20 20 20 20 20  g.pScratch.     
9bef0 20 20 20 20 20 20 7c 7c 20 70 3e 3d 28 76 6f 69        || p>=(voi
9bf00 64 2a 29 6d 65 6d 30 2e 61 53 63 72 61 74 63 68  d*)mem0.aScratch
9bf10 46 72 65 65 20 29 7b 0a 20 20 20 20 20 20 69 66  Free ){.      if
9bf20 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
9bf30 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29  onfig.bMemstat )
9bf40 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 53  {.        int iS
9bf50 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  ize = sqlite3Mal
9bf60 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20  locSize(p);.    
9bf70 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
9bf80 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74  x_enter(mem0.mut
9bf90 65 78 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ex);.        sql
9bfa0 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51  ite3StatusAdd(SQ
9bfb0 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41  LITE_STATUS_SCRA
9bfc0 54 43 48 5f 4f 56 45 52 46 4c 4f 57 2c 20 2d 69  TCH_OVERFLOW, -i
9bfd0 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 73  Size);.        s
9bfe0 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28  qlite3StatusAdd(
9bff0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45  SQLITE_STATUS_ME
9c000 4d 4f 52 59 5f 55 53 45 44 2c 20 2d 69 53 69 7a  MORY_USED, -iSiz
9c010 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  e);.        sqli
9c020 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9c030 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 20 20  m.xFree(p);.    
9c040 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
9c050 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74  x_leave(mem0.mut
9c060 65 78 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ex);.      }else
9c070 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
9c080 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
9c090 78 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20  xFree(p);.      
9c0a0 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
9c0b0 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
9c0c0 69 20 3d 20 28 69 6e 74 29 28 28 75 38 2a 29 70  i = (int)((u8*)p
9c0d0 20 2d 20 28 75 38 2a 29 73 71 6c 69 74 65 33 47   - (u8*)sqlite3G
9c0e0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72  lobalConfig.pScr
9c0f0 61 74 63 68 29 3b 0a 20 20 20 20 20 20 69 20 2f  atch);.      i /
9c100 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
9c110 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3b  onfig.szScratch;
9c120 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
9c130 3e 3d 30 20 26 26 20 69 3c 73 71 6c 69 74 65 33  >=0 && i<sqlite3
9c140 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63  GlobalConfig.nSc
9c150 72 61 74 63 68 20 29 3b 0a 20 20 20 20 20 20 73  ratch );.      s
9c160 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
9c170 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a  er(mem0.mutex);.
9c180 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6d 65        assert( me
9c190 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65 3c  m0.nScratchFree<
9c1a0 28 75 33 32 29 73 71 6c 69 74 65 33 47 6c 6f 62  (u32)sqlite3Glob
9c1b0 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63  alConfig.nScratc
9c1c0 68 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 30 2e  h );.      mem0.
9c1d0 61 53 63 72 61 74 63 68 46 72 65 65 5b 6d 65 6d  aScratchFree[mem
9c1e0 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65 2b 2b  0.nScratchFree++
9c1f0 5d 20 3d 20 69 3b 0a 20 20 20 20 20 20 73 71 6c  ] = i;.      sql
9c200 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51  ite3StatusAdd(SQ
9c210 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41  LITE_STATUS_SCRA
9c220 54 43 48 5f 55 53 45 44 2c 20 2d 31 29 3b 0a 20  TCH_USED, -1);. 
9c230 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
9c240 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75  ex_leave(mem0.mu
9c250 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  tex);.    }.  }.
9c260 7d 0a 0a 2f 2a 0a 2a 2a 20 54 52 55 45 20 69 66  }../*.** TRUE if
9c270 20 70 20 69 73 20 61 20 6c 6f 6f 6b 61 73 69 64   p is a lookasid
9c280 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
9c290 69 6f 6e 20 66 72 6f 6d 20 64 62 0a 2a 2f 0a 23  ion from db.*/.#
9c2a0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
9c2b0 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a 73 74 61  IT_LOOKASIDE.sta
9c2c0 74 69 63 20 69 6e 74 20 69 73 4c 6f 6f 6b 61 73  tic int isLookas
9c2d0 69 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ide(sqlite3 *db,
9c2e0 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 72 65 74   void *p){.  ret
9c2f0 75 72 6e 20 64 62 20 26 26 20 70 20 26 26 20 70  urn db && p && p
9c300 3e 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  >=db->lookaside.
9c310 70 53 74 61 72 74 20 26 26 20 70 3c 64 62 2d 3e  pStart && p<db->
9c320 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e 64 3b 0a  lookaside.pEnd;.
9c330 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  }.#else.#define 
9c340 69 73 4c 6f 6f 6b 61 73 69 64 65 28 41 2c 42 29  isLookaside(A,B)
9c350 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   0.#endif../*.**
9c360 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65   Return the size
9c370 20 6f 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c   of a memory all
9c380 6f 63 61 74 69 6f 6e 20 70 72 65 76 69 6f 75 73  ocation previous
9c390 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ly obtained from
9c3a0 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  .** sqlite3Mallo
9c3b0 63 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 6d  c() or sqlite3_m
9c3c0 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49  alloc()..*/.SQLI
9c3d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
9c3e0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65  qlite3MallocSize
9c3f0 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 72 65 74  (void *p){.  ret
9c400 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  urn sqlite3Globa
9c410 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69 7a 65 28  lConfig.m.xSize(
9c420 70 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  p);.}.SQLITE_PRI
9c430 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
9c440 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 73 71 6c  DbMallocSize(sql
9c450 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a  ite3 *db, void *
9c460 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 64 62  p){.  assert( db
9c470 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  ==0 || sqlite3_m
9c480 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75  utex_held(db->mu
9c490 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 69 73  tex) );.  if( is
9c4a0 4c 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 29  Lookaside(db, p)
9c4b0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 64   ){.    return d
9c4c0 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 3b  b->lookaside.sz;
9c4d0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
9c4e0 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62  turn sqlite3Glob
9c4f0 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69 7a 65  alConfig.m.xSize
9c500 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  (p);.  }.}../*.*
9c510 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 20 70 72  * Free memory pr
9c520 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65  eviously obtaine
9c530 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 61  d from sqlite3Ma
9c540 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  lloc()..*/.SQLIT
9c550 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
9c560 65 33 5f 66 72 65 65 28 76 6f 69 64 20 2a 70 29  e3_free(void *p)
9c570 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  {.  if( p==0 ) r
9c580 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c  eturn;.  if( sql
9c590 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9c5a0 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20  .bMemstat ){.   
9c5b0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
9c5c0 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29  nter(mem0.mutex)
9c5d0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61  ;.    sqlite3Sta
9c5e0 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
9c5f0 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44  ATUS_MEMORY_USED
9c600 2c 20 2d 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  , -sqlite3Malloc
9c610 53 69 7a 65 28 70 29 29 3b 0a 20 20 20 20 73 71  Size(p));.    sq
9c620 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9c630 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20  g.m.xFree(p);.  
9c640 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
9c650 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78  leave(mem0.mutex
9c660 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
9c670 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9c680 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a  fig.m.xFree(p);.
9c690 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65    }.}../*.** Fre
9c6a0 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d 69  e memory that mi
9c6b0 67 68 74 20 62 65 20 61 73 73 6f 63 69 61 74 65  ght be associate
9c6c0 64 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75  d with a particu
9c6d0 6c 61 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  lar database.** 
9c6e0 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53  connection..*/.S
9c6f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
9c700 69 64 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  id sqlite3DbFree
9c710 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f  (sqlite3 *db, vo
9c720 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  id *p){.  assert
9c730 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ( db==0 || sqlit
9c740 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
9c750 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66  ->mutex) );.  if
9c760 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64 62  ( isLookaside(db
9c770 2c 20 70 29 20 29 7b 0a 20 20 20 20 4c 6f 6f 6b  , p) ){.    Look
9c780 61 73 69 64 65 53 6c 6f 74 20 2a 70 42 75 66 20  asideSlot *pBuf 
9c790 3d 20 28 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74  = (LookasideSlot
9c7a0 2a 29 70 3b 0a 20 20 20 20 70 42 75 66 2d 3e 70  *)p;.    pBuf->p
9c7b0 4e 65 78 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61  Next = db->looka
9c7c0 73 69 64 65 2e 70 46 72 65 65 3b 0a 20 20 20 20  side.pFree;.    
9c7d0 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46  db->lookaside.pF
9c7e0 72 65 65 20 3d 20 70 42 75 66 3b 0a 20 20 20 20  ree = pBuf;.    
9c7f0 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f  db->lookaside.nO
9c800 75 74 2d 2d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ut--;.  }else{. 
9c810 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
9c820 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  p);.  }.}../*.**
9c830 20 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65   Change the size
9c840 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20   of an existing 
9c850 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
9c860 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  n.*/.SQLITE_PRIV
9c870 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
9c880 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70  3Realloc(void *p
9c890 4f 6c 64 2c 20 69 6e 74 20 6e 42 79 74 65 73 29  Old, int nBytes)
9c8a0 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 2c 20 6e 4e  {.  int nOld, nN
9c8b0 65 77 3b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77  ew;.  void *pNew
9c8c0 3b 0a 20 20 69 66 28 20 70 4f 6c 64 3d 3d 30 20  ;.  if( pOld==0 
9c8d0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71  ){.    return sq
9c8e0 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 79 74  lite3Malloc(nByt
9c8f0 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e  es);.  }.  if( n
9c900 42 79 74 65 73 3c 3d 30 20 29 7b 0a 20 20 20 20  Bytes<=0 ){.    
9c910 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4f 6c  sqlite3_free(pOl
9c920 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  d);.    return 0
9c930 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 79 74  ;.  }.  if( nByt
9c940 65 73 3e 3d 30 78 37 66 66 66 66 66 30 30 20 29  es>=0x7fffff00 )
9c950 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 30 78 37  {.    /* The 0x7
9c960 66 66 66 66 30 30 20 6c 69 6d 69 74 20 74 65 72  ffff00 limit ter
9c970 6d 20 69 73 20 65 78 70 6c 61 69 6e 65 64 20 69  m is explained i
9c980 6e 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 73 71  n comments on sq
9c990 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 2a 2f  lite3Malloc() */
9c9a0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
9c9b0 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 73 71 6c 69   }.  nOld = sqli
9c9c0 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4f  te3MallocSize(pO
9c9d0 6c 64 29 3b 0a 20 20 6e 4e 65 77 20 3d 20 73 71  ld);.  nNew = sq
9c9e0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9c9f0 67 2e 6d 2e 78 52 6f 75 6e 64 75 70 28 6e 42 79  g.m.xRoundup(nBy
9ca00 74 65 73 29 3b 0a 20 20 69 66 28 20 6e 4f 6c 64  tes);.  if( nOld
9ca10 3d 3d 6e 4e 65 77 20 29 7b 0a 20 20 20 20 70 4e  ==nNew ){.    pN
9ca20 65 77 20 3d 20 70 4f 6c 64 3b 0a 20 20 7d 65 6c  ew = pOld;.  }el
9ca30 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  se if( sqlite3Gl
9ca40 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73  obalConfig.bMems
9ca50 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  tat ){.    sqlit
9ca60 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
9ca70 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  em0.mutex);.    
9ca80 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74  sqlite3StatusSet
9ca90 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d  (SQLITE_STATUS_M
9caa0 41 4c 4c 4f 43 5f 53 49 5a 45 2c 20 6e 42 79 74  ALLOC_SIZE, nByt
9cab0 65 73 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  es);.    if( sql
9cac0 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 28  ite3StatusValue(
9cad0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45  SQLITE_STATUS_ME
9cae0 4d 4f 52 59 5f 55 53 45 44 29 2b 6e 4e 65 77 2d  MORY_USED)+nNew-
9caf0 6e 4f 6c 64 20 3e 3d 20 0a 20 20 20 20 20 20 20  nOld >= .       
9cb00 20 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 72     mem0.alarmThr
9cb10 65 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20 20 20  eshold ){.      
9cb20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61  sqlite3MallocAla
9cb30 72 6d 28 6e 4e 65 77 2d 6e 4f 6c 64 29 3b 0a 20  rm(nNew-nOld);. 
9cb40 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 20 3d 20     }.    pNew = 
9cb50 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9cb60 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70  fig.m.xRealloc(p
9cb70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20  Old, nNew);.    
9cb80 69 66 28 20 70 4e 65 77 3d 3d 30 20 26 26 20 6d  if( pNew==0 && m
9cb90 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63  em0.alarmCallbac
9cba0 6b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  k ){.      sqlit
9cbb0 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 42  e3MallocAlarm(nB
9cbc0 79 74 65 73 29 3b 0a 20 20 20 20 20 20 70 4e 65  ytes);.      pNe
9cbd0 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  w = sqlite3Globa
9cbe0 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c  lConfig.m.xReall
9cbf0 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a  oc(pOld, nNew);.
9cc00 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4e      }.    if( pN
9cc10 65 77 20 29 7b 0a 20 20 20 20 20 20 6e 4e 65 77  ew ){.      nNew
9cc20 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
9cc30 53 69 7a 65 28 70 4e 65 77 29 3b 0a 20 20 20 20  Size(pNew);.    
9cc40 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41    sqlite3StatusA
9cc50 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  dd(SQLITE_STATUS
9cc60 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 6e 4e  _MEMORY_USED, nN
9cc70 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20 20 20 7d 0a  ew-nOld);.    }.
9cc80 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
9cc90 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74  x_leave(mem0.mut
9cca0 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ex);.  }else{.  
9ccb0 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
9ccc0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
9ccd0 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e  Realloc(pOld, nN
9cce0 65 77 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ew);.  }.  retur
9ccf0 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pNew;.}../*.**
9cd00 20 54 68 65 20 70 75 62 6c 69 63 20 69 6e 74 65   The public inte
9cd10 72 66 61 63 65 20 74 6f 20 73 71 6c 69 74 65 33  rface to sqlite3
9cd20 52 65 61 6c 6c 6f 63 2e 20 20 4d 61 6b 65 20 73  Realloc.  Make s
9cd30 75 72 65 20 74 68 61 74 20 74 68 65 20 6d 65 6d  ure that the mem
9cd40 6f 72 79 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d  ory.** subsystem
9cd50 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   is initialized 
9cd60 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e  prior to invokin
9cd70 67 20 73 71 6c 69 74 65 52 65 61 6c 6c 6f 63 2e  g sqliteRealloc.
9cd80 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
9cd90 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 65 61  oid *sqlite3_rea
9cda0 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 4f 6c 64 2c  lloc(void *pOld,
9cdb0 20 69 6e 74 20 6e 29 7b 0a 23 69 66 6e 64 65 66   int n){.#ifndef
9cdc0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
9cdd0 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69  OINIT.  if( sqli
9cde0 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
9cdf0 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e   ) return 0;.#en
9ce00 64 69 66 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  dif.  return sql
9ce10 69 74 65 33 52 65 61 6c 6c 6f 63 28 70 4f 6c 64  ite3Realloc(pOld
9ce20 2c 20 6e 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  , n);.}.../*.** 
9ce30 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a 65 72  Allocate and zer
9ce40 6f 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 20 0a 53 51  o memory..*/ .SQ
9ce50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
9ce60 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  d *sqlite3Malloc
9ce70 5a 65 72 6f 28 69 6e 74 20 6e 29 7b 0a 20 20 76  Zero(int n){.  v
9ce80 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33  oid *p = sqlite3
9ce90 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69 66 28  Malloc(n);.  if(
9cea0 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74   p ){.    memset
9ceb0 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 20  (p, 0, n);.  }. 
9cec0 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
9ced0 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64  .** Allocate and
9cee0 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 20 20 49   zero memory.  I
9cef0 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  f the allocation
9cf00 20 66 61 69 6c 73 2c 20 6d 61 6b 65 0a 2a 2a 20   fails, make.** 
9cf10 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  the mallocFailed
9cf20 20 66 6c 61 67 20 69 6e 20 74 68 65 20 63 6f 6e   flag in the con
9cf30 6e 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e  nection pointer.
9cf40 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
9cf50 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
9cf60 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c  DbMallocZero(sql
9cf70 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e 29  ite3 *db, int n)
9cf80 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73 71  {.  void *p = sq
9cf90 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
9cfa0 28 64 62 2c 20 6e 29 3b 0a 20 20 69 66 28 20 70  (db, n);.  if( p
9cfb0 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70   ){.    memset(p
9cfc0 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72  , 0, n);.  }.  r
9cfd0 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
9cfe0 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a  * Allocate and z
9cff0 65 72 6f 20 6d 65 6d 6f 72 79 2e 20 20 49 66 20  ero memory.  If 
9d000 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  the allocation f
9d010 61 69 6c 73 2c 20 6d 61 6b 65 0a 2a 2a 20 74 68  ails, make.** th
9d020 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66  e mallocFailed f
9d030 6c 61 67 20 69 6e 20 74 68 65 20 63 6f 6e 6e 65  lag in the conne
9d040 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a  ction pointer..*
9d050 2a 0a 2a 2a 20 49 66 20 64 62 21 3d 30 20 61 6e  *.** If db!=0 an
9d060 64 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  d db->mallocFail
9d070 65 64 20 69 73 20 74 72 75 65 20 28 69 6e 64 69  ed is true (indi
9d080 63 61 74 69 6e 67 20 61 20 70 72 69 6f 72 20 6d  cating a prior m
9d090 61 6c 6c 6f 63 0a 2a 2a 20 66 61 69 6c 75 72 65  alloc.** failure
9d0a0 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74   on the same dat
9d0b0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
9d0c0 29 20 74 68 65 6e 20 61 6c 77 61 79 73 20 72 65  ) then always re
9d0d0 74 75 72 6e 20 30 2e 0a 2a 2a 20 48 65 6e 63 65  turn 0..** Hence
9d0e0 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61   for a particula
9d0f0 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  r database conne
9d100 63 74 69 6f 6e 2c 20 6f 6e 63 65 20 6d 61 6c 6c  ction, once mall
9d110 6f 63 20 73 74 61 72 74 73 0a 2a 2a 20 66 61 69  oc starts.** fai
9d120 6c 69 6e 67 2c 20 69 74 20 66 61 69 6c 73 20 63  ling, it fails c
9d130 6f 6e 73 69 73 74 65 6e 74 6c 79 20 75 6e 74 69  onsistently unti
9d140 6c 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 69  l mallocFailed i
9d150 73 20 72 65 73 65 74 2e 0a 2a 2a 20 54 68 69 73  s reset..** This
9d160 20 69 73 20 61 6e 20 69 6d 70 6f 72 74 61 6e 74   is an important
9d170 20 61 73 73 75 6d 70 74 69 6f 6e 2e 20 20 54 68   assumption.  Th
9d180 65 72 65 20 61 72 65 20 6d 61 6e 79 20 70 6c 61  ere are many pla
9d190 63 65 73 20 69 6e 20 74 68 65 0a 2a 2a 20 63 6f  ces in the.** co
9d1a0 64 65 20 74 68 61 74 20 64 6f 20 74 68 69 6e 67  de that do thing
9d1b0 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a  s like this:.**.
9d1c0 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 74 20 2a  **         int *
9d1d0 61 20 3d 20 28 69 6e 74 2a 29 73 71 6c 69 74 65  a = (int*)sqlite
9d1e0 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
9d1f0 20 31 30 30 29 3b 0a 2a 2a 20 20 20 20 20 20 20   100);.**       
9d200 20 20 69 6e 74 20 2a 62 20 3d 20 28 69 6e 74 2a    int *b = (int*
9d210 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63  )sqlite3DbMalloc
9d220 52 61 77 28 64 62 2c 20 32 30 30 29 3b 0a 2a 2a  Raw(db, 200);.**
9d230 20 20 20 20 20 20 20 20 20 69 66 28 20 62 20 29           if( b )
9d240 20 61 5b 31 30 5d 20 3d 20 39 3b 0a 2a 2a 0a 2a   a[10] = 9;.**.*
9d250 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  * In other words
9d260 2c 20 69 66 20 61 20 73 75 62 73 65 71 75 65 6e  , if a subsequen
9d270 74 20 6d 61 6c 6c 6f 63 20 28 65 78 3a 20 22 62  t malloc (ex: "b
9d280 22 29 20 77 6f 72 6b 65 64 2c 20 69 74 20 69 73  ") worked, it is
9d290 20 61 73 73 75 6d 65 64 0a 2a 2a 20 74 68 61 74   assumed.** that
9d2a0 20 61 6c 6c 20 70 72 69 6f 72 20 6d 61 6c 6c 6f   all prior mallo
9d2b0 63 73 20 28 65 78 3a 20 22 61 22 29 20 77 6f 72  cs (ex: "a") wor
9d2c0 6b 65 64 20 74 6f 6f 2e 0a 2a 2f 0a 53 51 4c 49  ked too..*/.SQLI
9d2d0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
9d2e0 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63  *sqlite3DbMalloc
9d2f0 52 61 77 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  Raw(sqlite3 *db,
9d300 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20   int n){.  void 
9d310 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62  *p;.  assert( db
9d320 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  ==0 || sqlite3_m
9d330 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75  utex_held(db->mu
9d340 74 65 78 29 20 29 3b 0a 23 69 66 6e 64 65 66 20  tex) );.#ifndef 
9d350 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 4f 4b  SQLITE_OMIT_LOOK
9d360 41 53 49 44 45 0a 20 20 69 66 28 20 64 62 20 29  ASIDE.  if( db )
9d370 7b 0a 20 20 20 20 4c 6f 6f 6b 61 73 69 64 65 53  {.    LookasideS
9d380 6c 6f 74 20 2a 70 42 75 66 3b 0a 20 20 20 20 69  lot *pBuf;.    i
9d390 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
9d3a0 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 72 65 74  led ){.      ret
9d3b0 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 0;.    }.   
9d3c0 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69   if( db->lookasi
9d3d0 64 65 2e 62 45 6e 61 62 6c 65 64 20 26 26 20 6e  de.bEnabled && n
9d3e0 3c 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  <=db->lookaside.
9d3f0 73 7a 0a 20 20 20 20 20 20 20 20 20 26 26 20 28  sz.         && (
9d400 70 42 75 66 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61  pBuf = db->looka
9d410 73 69 64 65 2e 70 46 72 65 65 29 21 3d 30 20 29  side.pFree)!=0 )
9d420 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b  {.      db->look
9d430 61 73 69 64 65 2e 70 46 72 65 65 20 3d 20 70 42  aside.pFree = pB
9d440 75 66 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  uf->pNext;.     
9d450 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e   db->lookaside.n
9d460 4f 75 74 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28  Out++;.      if(
9d470 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e   db->lookaside.n
9d480 4f 75 74 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  Out>db->lookasid
9d490 65 2e 6d 78 4f 75 74 20 29 7b 0a 20 20 20 20 20  e.mxOut ){.     
9d4a0 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65     db->lookaside
9d4b0 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f 6f  .mxOut = db->loo
9d4c0 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20  kaside.nOut;.   
9d4d0 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72     }.      retur
9d4e0 6e 20 28 76 6f 69 64 2a 29 70 42 75 66 3b 0a 20  n (void*)pBuf;. 
9d4f0 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20     }.  }.#else. 
9d500 20 69 66 28 20 64 62 20 26 26 20 64 62 2d 3e 6d   if( db && db->m
9d510 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
9d520 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
9d530 0a 23 65 6e 64 69 66 0a 20 20 70 20 3d 20 73 71  .#endif.  p = sq
9d540 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a  lite3Malloc(n);.
9d550 20 20 69 66 28 20 21 70 20 26 26 20 64 62 20 29    if( !p && db )
9d560 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63  {.    db->malloc
9d570 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  Failed = 1;.  }.
9d580 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
9d590 2a 0a 2a 2a 20 52 65 73 69 7a 65 20 74 68 65 20  *.** Resize the 
9d5a0 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20  block of memory 
9d5b0 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 20  pointed to by p 
9d5c0 74 6f 20 6e 20 62 79 74 65 73 2e 20 49 66 20 74  to n bytes. If t
9d5d0 68 65 0a 2a 2a 20 72 65 73 69 7a 65 20 66 61 69  he.** resize fai
9d5e0 6c 73 2c 20 73 65 74 20 74 68 65 20 6d 61 6c 6c  ls, set the mall
9d5f0 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e  ocFailed flag in
9d600 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20   the connection 
9d610 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54  object..*/.SQLIT
9d620 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
9d630 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
9d640 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f  (sqlite3 *db, vo
9d650 69 64 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a 20  id *p, int n){. 
9d660 20 76 6f 69 64 20 2a 70 4e 65 77 20 3d 20 30 3b   void *pNew = 0;
9d670 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30  .  assert( db!=0
9d680 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
9d690 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
9d6a0 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
9d6b0 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
9d6c0 61 69 6c 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20  ailed==0 ){.    
9d6d0 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20  if( p==0 ){.    
9d6e0 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
9d6f0 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
9d700 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  n);.    }.    if
9d710 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64 62  ( isLookaside(db
9d720 2c 20 70 29 20 29 7b 0a 20 20 20 20 20 20 69 66  , p) ){.      if
9d730 28 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69  ( n<=db->lookasi
9d740 64 65 2e 73 7a 20 29 7b 0a 20 20 20 20 20 20 20  de.sz ){.       
9d750 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20 20   return p;.     
9d760 20 7d 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20   }.      pNew = 
9d770 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
9d780 61 77 28 64 62 2c 20 6e 29 3b 0a 20 20 20 20 20  aw(db, n);.     
9d790 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20   if( pNew ){.   
9d7a0 20 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 65 77       memcpy(pNew
9d7b0 2c 20 70 2c 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  , p, db->lookasi
9d7c0 64 65 2e 73 7a 29 3b 0a 20 20 20 20 20 20 20 20  de.sz);.        
9d7d0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
9d7e0 2c 20 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , p);.      }.  
9d7f0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
9d800 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65  New = sqlite3_re
9d810 61 6c 6c 6f 63 28 70 2c 20 6e 29 3b 0a 20 20 20  alloc(p, n);.   
9d820 20 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a     if( !pNew ){.
9d830 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c          db->mall
9d840 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
9d850 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
9d860 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d    return pNew;.}
9d870 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20  ../*.** Attempt 
9d880 74 6f 20 72 65 61 6c 6c 6f 63 61 74 65 20 70 2e  to reallocate p.
9d890 20 20 49 66 20 74 68 65 20 72 65 61 6c 6c 6f 63    If the realloc
9d8a0 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65  ation fails, the
9d8b0 6e 20 66 72 65 65 20 70 0a 2a 2a 20 61 6e 64 20  n free p.** and 
9d8c0 73 65 74 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61  set the mallocFa
9d8d0 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65  iled flag in the
9d8e0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
9d8f0 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
9d900 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
9d910 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72  lite3DbReallocOr
9d920 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a 64 62  Free(sqlite3 *db
9d930 2c 20 76 6f 69 64 20 2a 70 2c 20 69 6e 74 20 6e  , void *p, int n
9d940 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b  ){.  void *pNew;
9d950 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  pNew = sqlite
9d960 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70  3DbRealloc(db, p
9d970 2c 20 6e 29 3b 0a 20 20 69 66 28 20 21 70 4e 65  , n);.  if( !pNe
9d980 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  w ){.    sqlite3
9d990 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20  DbFree(db, p);. 
9d9a0 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77   }.  return pNew
9d9b0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  ;.}../*.** Make 
9d9c0 61 20 63 6f 70 79 20 6f 66 20 61 20 73 74 72 69  a copy of a stri
9d9d0 6e 67 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74  ng in memory obt
9d9e0 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
9d9f0 65 4d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 73 65  eMalloc(). These
9da00 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 63   .** functions c
9da10 61 6c 6c 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  all sqlite3Mallo
9da20 63 52 61 77 28 29 20 64 69 72 65 63 74 6c 79 20  cRaw() directly 
9da30 69 6e 73 74 65 61 64 20 6f 66 20 73 71 6c 69 74  instead of sqlit
9da40 65 4d 61 6c 6c 6f 63 28 29 2e 20 54 68 69 73 0a  eMalloc(). This.
9da50 2a 2a 20 69 73 20 62 65 63 61 75 73 65 20 77 68  ** is because wh
9da60 65 6e 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 67  en memory debugg
9da70 69 6e 67 20 69 73 20 74 75 72 6e 65 64 20 6f 6e  ing is turned on
9da80 2c 20 74 68 65 73 65 20 74 77 6f 20 66 75 6e 63  , these two func
9da90 74 69 6f 6e 73 20 61 72 65 20 0a 2a 2a 20 63 61  tions are .** ca
9daa0 6c 6c 65 64 20 76 69 61 20 6d 61 63 72 6f 73 20  lled via macros 
9dab0 74 68 61 74 20 72 65 63 6f 72 64 20 74 68 65 20  that record the 
9dac0 63 75 72 72 65 6e 74 20 66 69 6c 65 20 61 6e 64  current file and
9dad0 20 6c 69 6e 65 20 6e 75 6d 62 65 72 20 69 6e 20   line number in 
9dae0 74 68 65 0a 2a 2a 20 54 68 72 65 61 64 44 61 74  the.** ThreadDat
9daf0 61 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  a structure..*/.
9db00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
9db10 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62 53 74  har *sqlite3DbSt
9db20 72 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62  rDup(sqlite3 *db
9db30 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  , const char *z)
9db40 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a  {.  char *zNew;.
9db50 20 20 73 69 7a 65 5f 74 20 6e 3b 0a 20 20 69 66    size_t n;.  if
9db60 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ( z==0 ){.    re
9db70 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 20  turn 0;.  }.  n 
9db80 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
9db90 30 28 7a 29 20 2b 20 31 3b 0a 20 20 61 73 73 65  0(z) + 1;.  asse
9dba0 72 74 28 20 28 6e 26 30 78 37 66 66 66 66 66 66  rt( (n&0x7ffffff
9dbb0 66 29 3d 3d 6e 20 29 3b 0a 20 20 7a 4e 65 77 20  f)==n );.  zNew 
9dbc0 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
9dbd0 63 52 61 77 28 64 62 2c 20 28 69 6e 74 29 6e 29  cRaw(db, (int)n)
9dbe0 3b 0a 20 20 69 66 28 20 7a 4e 65 77 20 29 7b 0a  ;.  if( zNew ){.
9dbf0 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c      memcpy(zNew,
9dc00 20 7a 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65   z, n);.  }.  re
9dc10 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 53 51 4c  turn zNew;.}.SQL
9dc20 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
9dc30 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44   *sqlite3DbStrND
9dc40 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  up(sqlite3 *db, 
9dc50 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
9dc60 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72 20 2a 7a  nt n){.  char *z
9dc70 4e 65 77 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20  New;.  if( z==0 
9dc80 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
9dc90 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28  .  }.  assert( (
9dca0 6e 26 30 78 37 66 66 66 66 66 66 66 29 3d 3d 6e  n&0x7fffffff)==n
9dcb0 20 29 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c   );.  zNew = sql
9dcc0 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
9dcd0 64 62 2c 20 6e 2b 31 29 3b 0a 20 20 69 66 28 20  db, n+1);.  if( 
9dce0 7a 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63  zNew ){.    memc
9dcf0 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29 3b 0a  py(zNew, z, n);.
9dd00 20 20 20 20 7a 4e 65 77 5b 6e 5d 20 3d 20 30 3b      zNew[n] = 0;
9dd10 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e  .  }.  return zN
9dd20 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  ew;.}../*.** Cre
9dd30 61 74 65 20 61 20 73 74 72 69 6e 67 20 66 72 6f  ate a string fro
9dd40 6d 20 74 68 65 20 7a 46 72 6f 6d 61 74 20 61 72  m the zFromat ar
9dd50 67 75 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 76  gument and the v
9dd60 61 5f 6c 69 73 74 20 74 68 61 74 20 66 6f 6c 6c  a_list that foll
9dd70 6f 77 73 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68  ows..** Store th
9dd80 65 20 73 74 72 69 6e 67 20 69 6e 20 6d 65 6d 6f  e string in memo
9dd90 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
9dda0 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20   sqliteMalloc() 
9ddb0 61 6e 64 20 6d 61 6b 65 20 2a 70 7a 0a 2a 2a 20  and make *pz.** 
9ddc0 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 73 74  point to that st
9ddd0 72 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ring..*/.SQLITE_
9dde0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
9ddf0 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 63 68  ite3SetString(ch
9de00 61 72 20 2a 2a 70 7a 2c 20 73 71 6c 69 74 65 33  ar **pz, sqlite3
9de10 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   *db, const char
9de20 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b   *zFormat, ...){
9de30 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20  .  va_list ap;. 
9de40 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 76 61 5f   char *z;..  va_
9de50 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61  start(ap, zForma
9de60 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65  t);.  z = sqlite
9de70 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46  3VMPrintf(db, zF
9de80 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61  ormat, ap);.  va
9de90 5f 65 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69  _end(ap);.  sqli
9dea0 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 2a 70  te3DbFree(db, *p
9deb0 7a 29 3b 0a 20 20 2a 70 7a 20 3d 20 7a 3b 0a 7d  z);.  *pz = z;.}
9dec0 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  .../*.** This fu
9ded0 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 63  nction must be c
9dee0 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 65 78 69  alled before exi
9def0 74 69 6e 67 20 61 6e 79 20 41 50 49 20 66 75 6e  ting any API fun
9df00 63 74 69 6f 6e 20 28 69 2e 65 2e 20 0a 2a 2a 20  ction (i.e. .** 
9df10 72 65 74 75 72 6e 69 6e 67 20 63 6f 6e 74 72 6f  returning contro
9df20 6c 20 74 6f 20 74 68 65 20 75 73 65 72 29 20 74  l to the user) t
9df30 68 61 74 20 68 61 73 20 63 61 6c 6c 65 64 20 73  hat has called s
9df40 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 6f 72  qlite3_malloc or
9df50 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  .** sqlite3_real
9df60 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  loc..**.** The r
9df70 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73  eturned value is
9df80 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 63 6f 70 79   normally a copy
9df90 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61   of the second a
9dfa0 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 0a  rgument to this.
9dfb0 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 48 6f 77  ** function. How
9dfc0 65 76 65 72 2c 20 69 66 20 61 20 6d 61 6c 6c 6f  ever, if a mallo
9dfd0 63 28 29 20 66 61 69 6c 75 72 65 20 68 61 73 20  c() failure has 
9dfe0 6f 63 63 75 72 72 65 64 20 73 69 6e 63 65 20 74  occurred since t
9dff0 68 65 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20 69  he previous.** i
9e000 6e 76 6f 63 61 74 69 6f 6e 20 53 51 4c 49 54 45  nvocation SQLITE
9e010 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e  _NOMEM is return
9e020 65 64 20 69 6e 73 74 65 61 64 2e 20 0a 2a 2a 0a  ed instead. .**.
9e030 2a 2a 20 49 66 20 74 68 65 20 66 69 72 73 74 20  ** If the first 
9e040 61 72 67 75 6d 65 6e 74 2c 20 64 62 2c 20 69 73  argument, db, is
9e050 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e 64 20 61 20   not NULL and a 
9e060 6d 61 6c 6c 6f 63 28 29 20 65 72 72 6f 72 20 68  malloc() error h
9e070 61 73 20 6f 63 63 75 72 72 65 64 2c 0a 2a 2a 20  as occurred,.** 
9e080 74 68 65 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74  then the connect
9e090 69 6f 6e 20 65 72 72 6f 72 2d 63 6f 64 65 20 28  ion error-code (
9e0a0 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
9e0b0 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 65 72  ed by sqlite3_er
9e0c0 72 63 6f 64 65 28 29 29 0a 2a 2a 20 69 73 20 73  rcode()).** is s
9e0d0 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d  et to SQLITE_NOM
9e0e0 45 4d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  EM..*/.SQLITE_PR
9e0f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
9e100 33 41 70 69 45 78 69 74 28 73 71 6c 69 74 65 33  3ApiExit(sqlite3
9e110 2a 20 64 62 2c 20 69 6e 74 20 72 63 29 7b 0a 20  * db, int rc){. 
9e120 20 2f 2a 20 49 66 20 74 68 65 20 64 62 20 68 61   /* If the db ha
9e130 6e 64 6c 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  ndle is not NULL
9e140 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74 20 68  , then we must h
9e150 6f 6c 64 20 74 68 65 20 63 6f 6e 6e 65 63 74 69  old the connecti
9e160 6f 6e 20 68 61 6e 64 6c 65 0a 20 20 2a 2a 20 6d  on handle.  ** m
9e170 75 74 65 78 20 68 65 72 65 2e 20 4f 74 68 65 72  utex here. Other
9e180 77 69 73 65 20 74 68 65 20 72 65 61 64 20 28 61  wise the read (a
9e190 6e 64 20 70 6f 73 73 69 62 6c 65 20 77 72 69 74  nd possible writ
9e1a0 65 29 20 6f 66 20 64 62 2d 3e 6d 61 6c 6c 6f 63  e) of db->malloc
9e1b0 46 61 69 6c 65 64 20 0a 20 20 2a 2a 20 69 73 20  Failed .  ** is 
9e1c0 75 6e 73 61 66 65 2c 20 61 73 20 69 73 20 74 68  unsafe, as is th
9e1d0 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  e call to sqlite
9e1e0 33 45 72 72 6f 72 28 29 2e 0a 20 20 2a 2f 0a 20  3Error()..  */. 
9e1f0 20 61 73 73 65 72 74 28 20 21 64 62 20 7c 7c 20   assert( !db || 
9e200 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
9e210 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
9e220 0a 20 20 69 66 28 20 64 62 20 26 26 20 28 64 62  .  if( db && (db
9e230 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c  ->mallocFailed |
9e240 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45  | rc==SQLITE_IOE
9e250 52 52 5f 4e 4f 4d 45 4d 29 20 29 7b 0a 20 20 20  RR_NOMEM) ){.   
9e260 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
9e270 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20  , SQLITE_NOMEM, 
9e280 30 29 3b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c  0);.    db->mall
9e290 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20  ocFailed = 0;.  
9e2a0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
9e2b0 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  MEM;.  }.  retur
9e2c0 6e 20 72 63 20 26 20 28 64 62 20 3f 20 64 62 2d  n rc & (db ? db-
9e2d0 3e 65 72 72 4d 61 73 6b 20 3a 20 30 78 66 66 29  >errMask : 0xff)
9e2e0 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
9e2f0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 61 6c 6c  **** End of mall
9e300 6f 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  oc.c ***********
9e310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e330 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
9e340 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
9e350 70 72 69 6e 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a  printf.c *******
9e360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e380 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22  ***/./*.** The "
9e390 70 72 69 6e 74 66 22 20 63 6f 64 65 20 74 68 61  printf" code tha
9e3a0 74 20 66 6f 6c 6c 6f 77 73 20 64 61 74 65 73 20  t follows dates 
9e3b0 66 72 6f 6d 20 74 68 65 20 31 39 38 30 27 73 2e  from the 1980's.
9e3c0 20 20 49 74 20 69 73 20 69 6e 0a 2a 2a 20 74 68    It is in.** th
9e3d0 65 20 70 75 62 6c 69 63 20 64 6f 6d 61 69 6e 2e  e public domain.
9e3e0 20 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 63    The original c
9e3f0 6f 6d 6d 65 6e 74 73 20 61 72 65 20 69 6e 63 6c  omments are incl
9e400 75 64 65 64 20 68 65 72 65 20 66 6f 72 0a 2a 2a  uded here for.**
9e410 20 63 6f 6d 70 6c 65 74 65 6e 65 73 73 2e 20 20   completeness.  
9e420 54 68 65 79 20 61 72 65 20 76 65 72 79 20 6f 75  They are very ou
9e430 74 2d 6f 66 2d 64 61 74 65 20 62 75 74 20 6d 69  t-of-date but mi
9e440 67 68 74 20 62 65 20 75 73 65 66 75 6c 20 61 73  ght be useful as
9e450 0a 2a 2a 20 61 6e 20 68 69 73 74 6f 72 69 63 61  .** an historica
9e460 6c 20 72 65 66 65 72 65 6e 63 65 2e 20 20 4d 6f  l reference.  Mo
9e470 73 74 20 6f 66 20 74 68 65 20 22 65 6e 68 61 6e  st of the "enhan
9e480 63 65 6d 65 6e 74 73 22 20 68 61 76 65 20 62 65  cements" have be
9e490 65 6e 20 62 61 63 6b 65 64 0a 2a 2a 20 6f 75 74  en backed.** out
9e4a0 20 73 6f 20 74 68 61 74 20 74 68 65 20 66 75 6e   so that the fun
9e4b0 63 74 69 6f 6e 61 6c 69 74 79 20 69 73 20 6e 6f  ctionality is no
9e4c0 77 20 74 68 65 20 73 61 6d 65 20 61 73 20 73 74  w the same as st
9e4d0 61 6e 64 61 72 64 20 70 72 69 6e 74 66 28 29 2e  andard printf().
9e4e0 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 70 72 69 6e  .**.** $Id: prin
9e4f0 74 66 2e 63 2c 76 20 31 2e 31 30 34 20 32 30 30  tf.c,v 1.104 200
9e500 39 2f 30 36 2f 30 33 20 30 31 3a 32 34 3a 35 34  9/06/03 01:24:54
9e510 20 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a   drh Exp $.**.**
9e520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e560 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
9e570 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 6f 64  he following mod
9e580 75 6c 65 73 20 69 73 20 61 6e 20 65 6e 68 61 6e  ules is an enhan
9e590 63 65 64 20 72 65 70 6c 61 63 65 6d 65 6e 74 20  ced replacement 
9e5a0 66 6f 72 20 74 68 65 20 22 70 72 69 6e 74 66 22  for the "printf"
9e5b0 20 73 75 62 72 6f 75 74 69 6e 65 73 0a 2a 2a 20   subroutines.** 
9e5c0 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 73 74 61  found in the sta
9e5d0 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e  ndard C library.
9e5e0 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
9e5f0 65 6e 68 61 6e 63 65 6d 65 6e 74 73 20 61 72 65  enhancements are
9e600 0a 2a 2a 20 73 75 70 70 6f 72 74 65 64 3a 0a 2a  .** supported:.*
9e610 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 64 64  *.**      +  Add
9e620 69 74 69 6f 6e 61 6c 20 66 75 6e 63 74 69 6f 6e  itional function
9e630 73 2e 20 20 54 68 65 20 73 74 61 6e 64 61 72 64  s.  The standard
9e640 20 73 65 74 20 6f 66 20 22 70 72 69 6e 74 66 22   set of "printf"
9e650 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 20   functions.**   
9e660 20 20 20 20 20 20 69 6e 63 6c 75 64 65 73 20 70        includes p
9e670 72 69 6e 74 66 2c 20 66 70 72 69 6e 74 66 2c 20  rintf, fprintf, 
9e680 73 70 72 69 6e 74 66 2c 20 76 70 72 69 6e 74 66  sprintf, vprintf
9e690 2c 20 76 66 70 72 69 6e 74 66 2c 20 61 6e 64 0a  , vfprintf, and.
9e6a0 2a 2a 20 20 20 20 20 20 20 20 20 76 73 70 72 69  **         vspri
9e6b0 6e 74 66 2e 20 20 54 68 69 73 20 6d 6f 64 75 6c  ntf.  This modul
9e6c0 65 20 61 64 64 73 20 74 68 65 20 66 6f 6c 6c 6f  e adds the follo
9e6d0 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  wing:.**.**     
9e6e0 20 20 20 20 20 20 2a 20 20 73 6e 70 72 69 6e 74        *  snprint
9e6f0 66 20 2d 2d 20 57 6f 72 6b 73 20 6c 69 6b 65 20  f -- Works like 
9e700 73 70 72 69 6e 74 66 2c 20 62 75 74 20 68 61 73  sprintf, but has
9e710 20 61 6e 20 65 78 74 72 61 20 61 72 67 75 6d 65   an extra argume
9e720 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  nt.**           
9e730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77                 w
9e740 68 69 63 68 20 69 73 20 74 68 65 20 73 69 7a 65  hich is the size
9e750 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 77   of the buffer w
9e760 72 69 74 74 65 6e 20 74 6f 2e 0a 2a 2a 0a 2a 2a  ritten to..**.**
9e770 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 6d 70             *  mp
9e780 72 69 6e 74 66 20 2d 2d 20 20 53 69 6d 69 6c 61  rintf --  Simila
9e790 72 20 74 6f 20 73 70 72 69 6e 74 66 2e 20 20 57  r to sprintf.  W
9e7a0 72 69 74 65 73 20 6f 75 74 70 75 74 20 74 6f 20  rites output to 
9e7b0 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20  memory.**       
9e7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9e7d0 20 20 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d     obtained from
9e7e0 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20 20   malloc..**.**  
9e7f0 20 20 20 20 20 20 20 20 20 2a 20 20 78 70 72 69           *  xpri
9e800 6e 74 66 20 2d 2d 20 20 43 61 6c 6c 73 20 61 20  ntf --  Calls a 
9e810 66 75 6e 63 74 69 6f 6e 20 74 6f 20 64 69 73 70  function to disp
9e820 6f 73 65 20 6f 66 20 6f 75 74 70 75 74 2e 0a 2a  ose of output..*
9e830 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a  *.**           *
9e840 20 20 6e 70 72 69 6e 74 66 20 2d 2d 20 20 4e 6f    nprintf --  No
9e850 20 6f 75 74 70 75 74 2c 20 62 75 74 20 72 65 74   output, but ret
9e860 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  urns the number 
9e870 6f 66 20 63 68 61 72 61 63 74 65 72 73 0a 2a 2a  of characters.**
9e880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9e890 20 20 20 20 20 20 20 20 20 20 74 68 61 74 20 77            that w
9e8a0 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 6f  ould have been o
9e8b0 75 74 70 75 74 20 62 79 20 70 72 69 6e 74 66 2e  utput by printf.
9e8c0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .**.**          
9e8d0 20 2a 20 20 41 20 76 2d 20 76 65 72 73 69 6f 6e   *  A v- version
9e8e0 20 28 65 78 3a 20 76 73 6e 70 72 69 6e 74 66 29   (ex: vsnprintf)
9e8f0 20 6f 66 20 65 76 65 72 79 20 66 75 6e 63 74 69   of every functi
9e900 6f 6e 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 20 20  on is also.**   
9e910 20 20 20 20 20 20 20 20 20 20 20 73 75 70 70 6c             suppl
9e920 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  ied..**.**      
9e930 2b 20 20 41 20 66 65 77 20 65 78 74 65 6e 73 69  +  A few extensi
9e940 6f 6e 73 20 74 6f 20 74 68 65 20 66 6f 72 6d 61  ons to the forma
9e950 74 74 69 6e 67 20 6e 6f 74 61 74 69 6f 6e 20 61  tting notation a
9e960 72 65 20 73 75 70 70 6f 72 74 65 64 3a 0a 2a 2a  re supported:.**
9e970 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20  .**           * 
9e980 20 54 68 65 20 22 3d 22 20 66 6c 61 67 20 28 73   The "=" flag (s
9e990 69 6d 69 6c 61 72 20 74 6f 20 22 2d 22 29 20 63  imilar to "-") c
9e9a0 61 75 73 65 73 20 74 68 65 20 6f 75 74 70 75 74  auses the output
9e9b0 20 74 6f 20 62 65 0a 2a 2a 20 20 20 20 20 20 20   to be.**       
9e9c0 20 20 20 20 20 20 20 62 65 20 63 65 6e 74 65 72         be center
9e9d0 65 64 20 69 6e 20 74 68 65 20 61 70 70 72 6f 70  ed in the approp
9e9e0 72 69 61 74 65 6c 79 20 73 69 7a 65 64 20 66 69  riately sized fi
9e9f0 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  eld..**.**      
9ea00 20 20 20 20 20 2a 20 20 54 68 65 20 25 62 20 66       *  The %b f
9ea10 69 65 6c 64 20 6f 75 74 70 75 74 73 20 61 6e 20  ield outputs an 
9ea20 69 6e 74 65 67 65 72 20 69 6e 20 62 69 6e 61 72  integer in binar
9ea30 79 20 6e 6f 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  y notation..**.*
9ea40 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 54  *           *  T
9ea50 68 65 20 25 63 20 66 69 65 6c 64 20 6e 6f 77 20  he %c field now 
9ea60 61 63 63 65 70 74 73 20 61 20 70 72 65 63 69 73  accepts a precis
9ea70 69 6f 6e 2e 20 20 54 68 65 20 63 68 61 72 61 63  ion.  The charac
9ea80 74 65 72 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20  ter output.**   
9ea90 20 20 20 20 20 20 20 20 20 20 20 69 73 20 72 65             is re
9eaa0 70 65 61 74 65 64 20 62 79 20 74 68 65 20 6e 75  peated by the nu
9eab0 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68  mber of times th
9eac0 65 20 70 72 65 63 69 73 69 6f 6e 20 73 70 65 63  e precision spec
9ead0 69 66 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  ifies..**.**    
9eae0 20 20 20 20 20 20 20 2a 20 20 54 68 65 20 25 27         *  The %'
9eaf0 20 66 69 65 6c 64 20 77 6f 72 6b 73 20 6c 69 6b   field works lik
9eb00 65 20 25 63 2c 20 62 75 74 20 74 61 6b 65 73 20  e %c, but takes 
9eb10 61 73 20 69 74 73 20 63 68 61 72 61 63 74 65 72  as its character
9eb20 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
9eb30 20 20 20 20 20 6e 65 78 74 20 63 68 61 72 61 63       next charac
9eb40 74 65 72 20 6f 66 20 74 68 65 20 66 6f 72 6d 61  ter of the forma
9eb50 74 20 73 74 72 69 6e 67 2c 20 69 6e 73 74 65 61  t string, instea
9eb60 64 20 6f 66 20 74 68 65 20 6e 65 78 74 0a 2a 2a  d of the next.**
9eb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 72                ar
9eb80 67 75 6d 65 6e 74 2e 20 20 46 6f 72 20 65 78 61  gument.  For exa
9eb90 6d 70 6c 65 2c 20 20 70 72 69 6e 74 66 28 22 25  mple,  printf("%
9eba0 2e 37 38 27 2d 22 29 20 20 70 72 69 6e 74 73 20  .78'-")  prints 
9ebb0 37 38 20 6d 69 6e 75 73 0a 2a 2a 20 20 20 20 20  78 minus.**     
9ebc0 20 20 20 20 20 20 20 20 20 73 69 67 6e 73 2c 20           signs, 
9ebd0 74 68 65 20 73 61 6d 65 20 61 73 20 20 70 72 69  the same as  pri
9ebe0 6e 74 66 28 22 25 2e 37 38 63 22 2c 27 2d 27 29  ntf("%.78c",'-')
9ebf0 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20  ..**.**      +  
9ec00 57 68 65 6e 20 63 6f 6d 70 69 6c 65 64 20 75 73  When compiled us
9ec10 69 6e 67 20 47 43 43 20 6f 6e 20 61 20 53 50 41  ing GCC on a SPA
9ec20 52 43 2c 20 74 68 69 73 20 76 65 72 73 69 6f 6e  RC, this version
9ec30 20 6f 66 20 70 72 69 6e 74 66 20 69 73 0a 2a 2a   of printf is.**
9ec40 20 20 20 20 20 20 20 20 20 66 61 73 74 65 72 20           faster 
9ec50 74 68 61 6e 20 74 68 65 20 6c 69 62 72 61 72 79  than the library
9ec60 20 70 72 69 6e 74 66 20 66 6f 72 20 53 55 4e 20   printf for SUN 
9ec70 4f 53 20 34 2e 31 2e 0a 2a 2a 0a 2a 2a 20 20 20  OS 4.1..**.**   
9ec80 20 20 20 2b 20 20 41 6c 6c 20 66 75 6e 63 74 69     +  All functi
9ec90 6f 6e 73 20 61 72 65 20 66 75 6c 6c 79 20 72 65  ons are fully re
9eca0 65 6e 74 72 61 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 0a  entrant..**.*/..
9ecb0 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f 6e  /*.** Conversion
9ecc0 20 74 79 70 65 73 20 66 61 6c 6c 20 69 6e 74 6f   types fall into
9ecd0 20 76 61 72 69 6f 75 73 20 63 61 74 65 67 6f 72   various categor
9ece0 69 65 73 20 61 73 20 64 65 66 69 6e 65 64 20 62  ies as defined b
9ecf0 79 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69  y the.** followi
9ed00 6e 67 20 65 6e 75 6d 65 72 61 74 69 6f 6e 2e 0a  ng enumeration..
9ed10 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 52 41 44  */.#define etRAD
9ed20 49 58 20 20 20 20 20 20 20 31 20 2f 2a 20 49 6e  IX       1 /* In
9ed30 74 65 67 65 72 20 74 79 70 65 73 2e 20 20 25 64  teger types.  %d
9ed40 2c 20 25 78 2c 20 25 6f 2c 20 61 6e 64 20 73 6f  , %x, %o, and so
9ed50 20 66 6f 72 74 68 20 2a 2f 0a 23 64 65 66 69 6e   forth */.#defin
9ed60 65 20 65 74 46 4c 4f 41 54 20 20 20 20 20 20 20  e etFLOAT       
9ed70 32 20 2f 2a 20 46 6c 6f 61 74 69 6e 67 20 70 6f  2 /* Floating po
9ed80 69 6e 74 2e 20 20 25 66 20 2a 2f 0a 23 64 65 66  int.  %f */.#def
9ed90 69 6e 65 20 65 74 45 58 50 20 20 20 20 20 20 20  ine etEXP       
9eda0 20 20 33 20 2f 2a 20 45 78 70 6f 6e 65 6e 74 69    3 /* Exponenti
9edb0 6f 6e 61 6c 20 6e 6f 74 61 74 69 6f 6e 2e 20 25  onal notation. %
9edc0 65 20 61 6e 64 20 25 45 20 2a 2f 0a 23 64 65 66  e and %E */.#def
9edd0 69 6e 65 20 65 74 47 45 4e 45 52 49 43 20 20 20  ine etGENERIC   
9ede0 20 20 34 20 2f 2a 20 46 6c 6f 61 74 69 6e 67 20    4 /* Floating 
9edf0 6f 72 20 65 78 70 6f 6e 65 6e 74 69 61 6c 2c 20  or exponential, 
9ee00 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 65 78 70  depending on exp
9ee10 6f 6e 65 6e 74 2e 20 25 67 20 2a 2f 0a 23 64 65  onent. %g */.#de
9ee20 66 69 6e 65 20 65 74 53 49 5a 45 20 20 20 20 20  fine etSIZE     
9ee30 20 20 20 35 20 2f 2a 20 52 65 74 75 72 6e 20 6e     5 /* Return n
9ee40 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
9ee50 65 72 73 20 70 72 6f 63 65 73 73 65 64 20 73 6f  ers processed so
9ee60 20 66 61 72 2e 20 25 6e 20 2a 2f 0a 23 64 65 66   far. %n */.#def
9ee70 69 6e 65 20 65 74 53 54 52 49 4e 47 20 20 20 20  ine etSTRING    
9ee80 20 20 36 20 2f 2a 20 53 74 72 69 6e 67 73 2e 20    6 /* Strings. 
9ee90 25 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74  %s */.#define et
9eea0 44 59 4e 53 54 52 49 4e 47 20 20 20 37 20 2f 2a  DYNSTRING   7 /*
9eeb0 20 44 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c   Dynamically all
9eec0 6f 63 61 74 65 64 20 73 74 72 69 6e 67 73 2e 20  ocated strings. 
9eed0 25 7a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74  %z */.#define et
9eee0 50 45 52 43 45 4e 54 20 20 20 20 20 38 20 2f 2a  PERCENT     8 /*
9eef0 20 50 65 72 63 65 6e 74 20 73 79 6d 62 6f 6c 2e   Percent symbol.
9ef00 20 25 25 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65   %% */.#define e
9ef10 74 43 48 41 52 58 20 20 20 20 20 20 20 39 20 2f  tCHARX       9 /
9ef20 2a 20 43 68 61 72 61 63 74 65 72 73 2e 20 25 63  * Characters. %c
9ef30 20 2a 2f 0a 2f 2a 20 54 68 65 20 72 65 73 74 20   */./* The rest 
9ef40 61 72 65 20 65 78 74 65 6e 73 69 6f 6e 73 2c 20  are extensions, 
9ef50 6e 6f 74 20 6e 6f 72 6d 61 6c 6c 79 20 66 6f 75  not normally fou
9ef60 6e 64 20 69 6e 20 70 72 69 6e 74 66 28 29 20 2a  nd in printf() *
9ef70 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 51 4c 45  /.#define etSQLE
9ef80 53 43 41 50 45 20 20 31 30 20 2f 2a 20 53 74 72  SCAPE  10 /* Str
9ef90 69 6e 67 73 20 77 69 74 68 20 27 5c 27 27 20 64  ings with '\'' d
9efa0 6f 75 62 6c 65 64 2e 20 20 25 71 20 2a 2f 0a 23  oubled.  %q */.#
9efb0 64 65 66 69 6e 65 20 65 74 53 51 4c 45 53 43 41  define etSQLESCA
9efc0 50 45 32 20 31 31 20 2f 2a 20 53 74 72 69 6e 67  PE2 11 /* String
9efd0 73 20 77 69 74 68 20 27 5c 27 27 20 64 6f 75 62  s with '\'' doub
9efe0 6c 65 64 20 61 6e 64 20 65 6e 63 6c 6f 73 65 64  led and enclosed
9eff0 20 69 6e 20 27 27 2c 0a 20 20 20 20 20 20 20 20   in '',.        
9f000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9f010 20 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 20    NULL pointers 
9f020 72 65 70 6c 61 63 65 64 20 62 79 20 53 51 4c 20  replaced by SQL 
9f030 4e 55 4c 4c 2e 20 20 25 51 20 2a 2f 0a 23 64 65  NULL.  %Q */.#de
9f040 66 69 6e 65 20 65 74 54 4f 4b 45 4e 20 20 20 20  fine etTOKEN    
9f050 20 20 31 32 20 2f 2a 20 61 20 70 6f 69 6e 74 65    12 /* a pointe
9f060 72 20 74 6f 20 61 20 54 6f 6b 65 6e 20 73 74 72  r to a Token str
9f070 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e  ucture */.#defin
9f080 65 20 65 74 53 52 43 4c 49 53 54 20 20 20 20 31  e etSRCLIST    1
9f090 33 20 2f 2a 20 61 20 70 6f 69 6e 74 65 72 20 74  3 /* a pointer t
9f0a0 6f 20 61 20 53 72 63 4c 69 73 74 20 2a 2f 0a 23  o a SrcList */.#
9f0b0 64 65 66 69 6e 65 20 65 74 50 4f 49 4e 54 45 52  define etPOINTER
9f0c0 20 20 20 20 31 34 20 2f 2a 20 54 68 65 20 25 70      14 /* The %p
9f0d0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a 23   conversion */.#
9f0e0 64 65 66 69 6e 65 20 65 74 53 51 4c 45 53 43 41  define etSQLESCA
9f0f0 50 45 33 20 31 35 20 2f 2a 20 25 77 20 2d 3e 20  PE3 15 /* %w -> 
9f100 53 74 72 69 6e 67 73 20 77 69 74 68 20 27 5c 22  Strings with '\"
9f110 27 20 64 6f 75 62 6c 65 64 20 2a 2f 0a 23 64 65  ' doubled */.#de
9f120 66 69 6e 65 20 65 74 4f 52 44 49 4e 41 4c 20 20  fine etORDINAL  
9f130 20 20 31 36 20 2f 2a 20 25 72 20 2d 3e 20 31 73    16 /* %r -> 1s
9f140 74 2c 20 32 6e 64 2c 20 33 72 64 2c 20 34 74 68  t, 2nd, 3rd, 4th
9f150 2c 20 65 74 63 2e 20 20 45 6e 67 6c 69 73 68 20  , etc.  English 
9f160 6f 6e 6c 79 20 2a 2f 0a 0a 23 64 65 66 69 6e 65  only */..#define
9f170 20 65 74 49 4e 56 41 4c 49 44 20 20 20 20 20 30   etINVALID     0
9f180 20 2f 2a 20 41 6e 79 20 75 6e 72 65 63 6f 67 6e   /* Any unrecogn
9f190 69 7a 65 64 20 63 6f 6e 76 65 72 73 69 6f 6e 20  ized conversion 
9f1a0 74 79 70 65 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  type */.../*.** 
9f1b0 41 6e 20 22 65 74 42 79 74 65 22 20 69 73 20 61  An "etByte" is a
9f1c0 6e 20 38 2d 62 69 74 20 75 6e 73 69 67 6e 65 64  n 8-bit unsigned
9f1d0 20 76 61 6c 75 65 2e 0a 2a 2f 0a 74 79 70 65 64   value..*/.typed
9f1e0 65 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  ef unsigned char
9f1f0 20 65 74 42 79 74 65 3b 0a 0a 2f 2a 0a 2a 2a 20   etByte;../*.** 
9f200 45 61 63 68 20 62 75 69 6c 74 69 6e 20 63 6f 6e  Each builtin con
9f210 76 65 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65  version characte
9f220 72 20 28 65 78 3a 20 74 68 65 20 27 64 27 20 69  r (ex: the 'd' i
9f230 6e 20 22 25 64 22 29 20 69 73 20 64 65 73 63 72  n "%d") is descr
9f240 69 62 65 64 0a 2a 2a 20 62 79 20 61 6e 20 69 6e  ibed.** by an in
9f250 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
9f260 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
9f270 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  e.*/.typedef str
9f280 75 63 74 20 65 74 5f 69 6e 66 6f 20 7b 20 20 20  uct et_info {   
9f290 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  /* Information a
9f2a0 62 6f 75 74 20 65 61 63 68 20 66 6f 72 6d 61 74  bout each format
9f2b0 20 66 69 65 6c 64 20 2a 2f 0a 20 20 63 68 61 72   field */.  char
9f2c0 20 66 6d 74 74 79 70 65 3b 20 20 20 20 20 20 20   fmttype;       
9f2d0 20 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 72 6d       /* The form
9f2e0 61 74 20 66 69 65 6c 64 20 63 6f 64 65 20 6c 65  at field code le
9f2f0 74 74 65 72 20 2a 2f 0a 20 20 65 74 42 79 74 65  tter */.  etByte
9f300 20 62 61 73 65 3b 20 20 20 20 20 20 20 20 20 20   base;          
9f310 20 20 20 2f 2a 20 54 68 65 20 62 61 73 65 20 66     /* The base f
9f320 6f 72 20 72 61 64 69 78 20 63 6f 6e 76 65 72 73  or radix convers
9f330 69 6f 6e 20 2a 2f 0a 20 20 65 74 42 79 74 65 20  ion */.  etByte 
9f340 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  flags;          
9f350 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65    /* One or more
9f360 20 6f 66 20 46 4c 41 47 5f 20 63 6f 6e 73 74 61   of FLAG_ consta
9f370 6e 74 73 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 65  nts below */.  e
9f380 74 42 79 74 65 20 74 79 70 65 3b 20 20 20 20 20  tByte type;     
9f390 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65          /* Conve
9f3a0 72 73 69 6f 6e 20 70 61 72 61 64 69 67 6d 20 2a  rsion paradigm *
9f3b0 2f 0a 20 20 65 74 42 79 74 65 20 63 68 61 72 73  /.  etByte chars
9f3c0 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  et;          /* 
9f3d0 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 44 69 67  Offset into aDig
9f3e0 69 74 73 5b 5d 20 6f 66 20 74 68 65 20 64 69 67  its[] of the dig
9f3f0 69 74 73 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  its string */.  
9f400 65 74 42 79 74 65 20 70 72 65 66 69 78 3b 20 20  etByte prefix;  
9f410 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
9f420 65 74 20 69 6e 74 6f 20 61 50 72 65 66 69 78 5b  et into aPrefix[
9f430 5d 20 6f 66 20 74 68 65 20 70 72 65 66 69 78 20  ] of the prefix 
9f440 73 74 72 69 6e 67 20 2a 2f 0a 7d 20 65 74 5f 69  string */.} et_i
9f450 6e 66 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  nfo;../*.** Allo
9f460 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 65  wed values for e
9f470 74 5f 69 6e 66 6f 2e 66 6c 61 67 73 0a 2a 2f 0a  t_info.flags.*/.
9f480 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 53 49 47  #define FLAG_SIG
9f490 4e 45 44 20 20 31 20 20 20 20 20 2f 2a 20 54 72  NED  1     /* Tr
9f4a0 75 65 20 69 66 20 74 68 65 20 76 61 6c 75 65 20  ue if the value 
9f4b0 74 6f 20 63 6f 6e 76 65 72 74 20 69 73 20 73 69  to convert is si
9f4c0 67 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  gned */.#define 
9f4d0 46 4c 41 47 5f 49 4e 54 45 52 4e 20 20 32 20 20  FLAG_INTERN  2  
9f4e0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 66 6f     /* True if fo
9f4f0 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f  r internal use o
9f500 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 46  nly */.#define F
9f510 4c 41 47 5f 53 54 52 49 4e 47 20 20 34 20 20 20  LAG_STRING  4   
9f520 20 20 2f 2a 20 41 6c 6c 6f 77 20 69 6e 66 69 6e    /* Allow infin
9f530 69 74 79 20 70 72 65 63 69 73 69 6f 6e 20 2a 2f  ity precision */
9f540 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  .../*.** The fol
9f550 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 69 73 20  lowing table is 
9f560 73 65 61 72 63 68 65 64 20 6c 69 6e 65 61 72 6c  searched linearl
9f570 79 2c 20 73 6f 20 69 74 20 69 73 20 67 6f 6f 64  y, so it is good
9f580 20 74 6f 20 70 75 74 20 74 68 65 0a 2a 2a 20 6d   to put the.** m
9f590 6f 73 74 20 66 72 65 71 75 65 6e 74 6c 79 20 75  ost frequently u
9f5a0 73 65 64 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74  sed conversion t
9f5b0 79 70 65 73 20 66 69 72 73 74 2e 0a 2a 2f 0a 73  ypes first..*/.s
9f5c0 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
9f5d0 20 61 44 69 67 69 74 73 5b 5d 20 3d 20 22 30 31   aDigits[] = "01
9f5e0 32 33 34 35 36 37 38 39 41 42 43 44 45 46 30 31  23456789ABCDEF01
9f5f0 32 33 34 35 36 37 38 39 61 62 63 64 65 66 22 3b  23456789abcdef";
9f600 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68  .static const ch
9f610 61 72 20 61 50 72 65 66 69 78 5b 5d 20 3d 20 22  ar aPrefix[] = "
9f620 2d 78 30 5c 30 30 30 58 30 22 3b 0a 73 74 61 74  -x0\000X0";.stat
9f630 69 63 20 63 6f 6e 73 74 20 65 74 5f 69 6e 66 6f  ic const et_info
9f640 20 66 6d 74 69 6e 66 6f 5b 5d 20 3d 20 7b 0a 20   fmtinfo[] = {. 
9f650 20 7b 20 20 27 64 27 2c 20 31 30 2c 20 31 2c 20   {  'd', 10, 1, 
9f660 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c  etRADIX,      0,
9f670 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 73 27 2c    0 },.  {  's',
9f680 20 20 30 2c 20 34 2c 20 65 74 53 54 52 49 4e 47    0, 4, etSTRING
9f690 2c 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20  ,     0,  0 },. 
9f6a0 20 7b 20 20 27 67 27 2c 20 20 30 2c 20 31 2c 20   {  'g',  0, 1, 
9f6b0 65 74 47 45 4e 45 52 49 43 2c 20 20 20 20 33 30  etGENERIC,    30
9f6c0 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27 7a 27 2c  , 0 },.  {  'z',
9f6d0 20 20 30 2c 20 34 2c 20 65 74 44 59 4e 53 54 52    0, 4, etDYNSTR
9f6e0 49 4e 47 2c 20 20 30 2c 20 20 30 20 7d 2c 0a 20  ING,  0,  0 },. 
9f6f0 20 7b 20 20 27 71 27 2c 20 20 30 2c 20 34 2c 20   {  'q',  0, 4, 
9f700 65 74 53 51 4c 45 53 43 41 50 45 2c 20 20 30 2c  etSQLESCAPE,  0,
9f710 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 51 27 2c    0 },.  {  'Q',
9f720 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43    0, 4, etSQLESC
9f730 41 50 45 32 2c 20 30 2c 20 20 30 20 7d 2c 0a 20  APE2, 0,  0 },. 
9f740 20 7b 20 20 27 77 27 2c 20 20 30 2c 20 34 2c 20   {  'w',  0, 4, 
9f750 65 74 53 51 4c 45 53 43 41 50 45 33 2c 20 30 2c  etSQLESCAPE3, 0,
9f760 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 63 27 2c    0 },.  {  'c',
9f770 20 20 30 2c 20 30 2c 20 65 74 43 48 41 52 58 2c    0, 0, etCHARX,
9f780 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20        0,  0 },. 
9f790 20 7b 20 20 27 6f 27 2c 20 20 38 2c 20 30 2c 20   {  'o',  8, 0, 
9f7a0 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c  etRADIX,      0,
9f7b0 20 20 32 20 7d 2c 0a 20 20 7b 20 20 27 75 27 2c    2 },.  {  'u',
9f7c0 20 31 30 2c 20 30 2c 20 65 74 52 41 44 49 58 2c   10, 0, etRADIX,
9f7d0 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20        0,  0 },. 
9f7e0 20 7b 20 20 27 78 27 2c 20 31 36 2c 20 30 2c 20   {  'x', 16, 0, 
9f7f0 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 31 36  etRADIX,      16
9f800 2c 20 31 20 7d 2c 0a 20 20 7b 20 20 27 58 27 2c  , 1 },.  {  'X',
9f810 20 31 36 2c 20 30 2c 20 65 74 52 41 44 49 58 2c   16, 0, etRADIX,
9f820 20 20 20 20 20 20 30 2c 20 20 34 20 7d 2c 0a 23        0,  4 },.#
9f830 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
9f840 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
9f850 54 0a 20 20 7b 20 20 27 66 27 2c 20 20 30 2c 20  T.  {  'f',  0, 
9f860 31 2c 20 65 74 46 4c 4f 41 54 2c 20 20 20 20 20  1, etFLOAT,     
9f870 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
9f880 65 27 2c 20 20 30 2c 20 31 2c 20 65 74 45 58 50  e',  0, 1, etEXP
9f890 2c 20 20 20 20 20 20 20 20 33 30 2c 20 30 20 7d  ,        30, 0 }
9f8a0 2c 0a 20 20 7b 20 20 27 45 27 2c 20 20 30 2c 20  ,.  {  'E',  0, 
9f8b0 31 2c 20 65 74 45 58 50 2c 20 20 20 20 20 20 20  1, etEXP,       
9f8c0 20 31 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27   14, 0 },.  {  '
9f8d0 47 27 2c 20 20 30 2c 20 31 2c 20 65 74 47 45 4e  G',  0, 1, etGEN
9f8e0 45 52 49 43 2c 20 20 20 20 31 34 2c 20 30 20 7d  ERIC,    14, 0 }
9f8f0 2c 0a 23 65 6e 64 69 66 0a 20 20 7b 20 20 27 69  ,.#endif.  {  'i
9f900 27 2c 20 31 30 2c 20 31 2c 20 65 74 52 41 44 49  ', 10, 1, etRADI
9f910 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c  X,      0,  0 },
9f920 0a 20 20 7b 20 20 27 6e 27 2c 20 20 30 2c 20 30  .  {  'n',  0, 0
9f930 2c 20 65 74 53 49 5a 45 2c 20 20 20 20 20 20 20  , etSIZE,       
9f940 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 25  0,  0 },.  {  '%
9f950 27 2c 20 20 30 2c 20 30 2c 20 65 74 50 45 52 43  ',  0, 0, etPERC
9f960 45 4e 54 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c  ENT,    0,  0 },
9f970 0a 20 20 7b 20 20 27 70 27 2c 20 31 36 2c 20 30  .  {  'p', 16, 0
9f980 2c 20 65 74 50 4f 49 4e 54 45 52 2c 20 20 20 20  , etPOINTER,    
9f990 30 2c 20 20 31 20 7d 2c 0a 0a 2f 2a 20 41 6c 6c  0,  1 },../* All
9f9a0 20 74 68 65 20 72 65 73 74 20 68 61 76 65 20 74   the rest have t
9f9b0 68 65 20 46 4c 41 47 5f 49 4e 54 45 52 4e 20 62  he FLAG_INTERN b
9f9c0 69 74 20 73 65 74 20 61 6e 64 20 61 72 65 20 74  it set and are t
9f9d0 68 75 73 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c  hus for internal
9f9e0 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a  .** use only */.
9f9f0 20 20 7b 20 20 27 54 27 2c 20 20 30 2c 20 32 2c    {  'T',  0, 2,
9fa00 20 65 74 54 4f 4b 45 4e 2c 20 20 20 20 20 20 30   etTOKEN,      0
9fa10 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 53 27  ,  0 },.  {  'S'
9fa20 2c 20 20 30 2c 20 32 2c 20 65 74 53 52 43 4c 49  ,  0, 2, etSRCLI
9fa30 53 54 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a  ST,    0,  0 },.
9fa40 20 20 7b 20 20 27 72 27 2c 20 31 30 2c 20 33 2c    {  'r', 10, 3,
9fa50 20 65 74 4f 52 44 49 4e 41 4c 2c 20 20 20 20 30   etORDINAL,    0
9fa60 2c 20 20 30 20 7d 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a  ,  0 },.};../*.*
9fa70 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  * If SQLITE_OMIT
9fa80 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 20  _FLOATING_POINT 
9fa90 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e  is defined, then
9faa0 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66 6c 6f   none of the flo
9fab0 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 63  ating point.** c
9fac0 6f 6e 76 65 72 73 69 6f 6e 73 20 77 69 6c 6c 20  onversions will 
9fad0 77 6f 72 6b 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  work..*/.#ifndef
9fae0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
9faf0 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 2f 2a 0a 2a  ATING_POINT./*.*
9fb00 2a 20 22 2a 76 61 6c 22 20 69 73 20 61 20 64 6f  * "*val" is a do
9fb10 75 62 6c 65 20 73 75 63 68 20 74 68 61 74 20 30  uble such that 0
9fb20 2e 31 20 3c 3d 20 2a 76 61 6c 20 3c 20 31 30 2e  .1 <= *val < 10.
9fb30 30 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  0.** Return the 
9fb40 61 73 63 69 69 20 63 6f 64 65 20 66 6f 72 20 74  ascii code for t
9fb50 68 65 20 6c 65 61 64 69 6e 67 20 64 69 67 69 74  he leading digit
9fb60 20 6f 66 20 2a 76 61 6c 2c 20 74 68 65 6e 0a 2a   of *val, then.*
9fb70 2a 20 6d 75 6c 74 69 70 6c 79 20 22 2a 76 61 6c  * multiply "*val
9fb80 22 20 62 79 20 31 30 2e 30 20 74 6f 20 72 65 6e  " by 10.0 to ren
9fb90 6f 72 6d 61 6c 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20  ormalize..**.** 
9fba0 45 78 61 6d 70 6c 65 3a 0a 2a 2a 20 20 20 20 20  Example:.**     
9fbb0 69 6e 70 75 74 3a 20 20 20 20 20 2a 76 61 6c 20  input:     *val 
9fbc0 3d 20 33 2e 31 34 31 35 39 0a 2a 2a 20 20 20 20  = 3.14159.**    
9fbd0 20 6f 75 74 70 75 74 3a 20 20 20 20 2a 76 61 6c   output:    *val
9fbe0 20 3d 20 31 2e 34 31 35 39 20 20 20 20 66 75 6e   = 1.4159    fun
9fbf0 63 74 69 6f 6e 20 72 65 74 75 72 6e 20 3d 20 27  ction return = '
9fc00 33 27 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 75  3'.**.** The cou
9fc10 6e 74 65 72 20 2a 63 6e 74 20 69 73 20 69 6e 63  nter *cnt is inc
9fc20 72 65 6d 65 6e 74 65 64 20 65 61 63 68 20 74 69  remented each ti
9fc30 6d 65 2e 20 20 41 66 74 65 72 20 63 6f 75 6e 74  me.  After count
9fc40 65 72 20 65 78 63 65 65 64 73 0a 2a 2a 20 31 36  er exceeds.** 16
9fc50 20 28 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20   (the number of 
9fc60 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69  significant digi
9fc70 74 73 20 69 6e 20 61 20 36 34 2d 62 69 74 20 66  ts in a 64-bit f
9fc80 6c 6f 61 74 29 20 27 30 27 20 69 73 0a 2a 2a 20  loat) '0' is.** 
9fc90 61 6c 77 61 79 73 20 72 65 74 75 72 6e 65 64 2e  always returned.
9fca0 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20  .*/.static char 
9fcb0 65 74 5f 67 65 74 64 69 67 69 74 28 4c 4f 4e 47  et_getdigit(LONG
9fcc0 44 4f 55 42 4c 45 5f 54 59 50 45 20 2a 76 61 6c  DOUBLE_TYPE *val
9fcd0 2c 20 69 6e 74 20 2a 63 6e 74 29 7b 0a 20 20 69  , int *cnt){.  i
9fce0 6e 74 20 64 69 67 69 74 3b 0a 20 20 4c 4f 4e 47  nt digit;.  LONG
9fcf0 44 4f 55 42 4c 45 5f 54 59 50 45 20 64 3b 0a 20  DOUBLE_TYPE d;. 
9fd00 20 69 66 28 20 28 2a 63 6e 74 29 2b 2b 20 3e 3d   if( (*cnt)++ >=
9fd10 20 31 36 20 29 20 72 65 74 75 72 6e 20 27 30 27   16 ) return '0'
9fd20 3b 0a 20 20 64 69 67 69 74 20 3d 20 28 69 6e 74  ;.  digit = (int
9fd30 29 2a 76 61 6c 3b 0a 20 20 64 20 3d 20 64 69 67  )*val;.  d = dig
9fd40 69 74 3b 0a 20 20 64 69 67 69 74 20 2b 3d 20 27  it;.  digit += '
9fd50 30 27 3b 0a 20 20 2a 76 61 6c 20 3d 20 28 2a 76  0';.  *val = (*v
9fd60 61 6c 20 2d 20 64 29 2a 31 30 2e 30 3b 0a 20 20  al - d)*10.0;.  
9fd70 72 65 74 75 72 6e 20 28 63 68 61 72 29 64 69 67  return (char)dig
9fd80 69 74 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  it;.}.#endif /* 
9fd90 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
9fda0 54 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 0a 2f  TING_POINT */../
9fdb0 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 4e 20 73 70  *.** Append N sp
9fdc0 61 63 65 20 63 68 61 72 61 63 74 65 72 73 20 74  ace characters t
9fdd0 6f 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69  o the given stri
9fde0 6e 67 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74  ng buffer..*/.st
9fdf0 61 74 69 63 20 76 6f 69 64 20 61 70 70 65 6e 64  atic void append
9fe00 53 70 61 63 65 28 53 74 72 41 63 63 75 6d 20 2a  Space(StrAccum *
9fe10 70 41 63 63 75 6d 2c 20 69 6e 74 20 4e 29 7b 0a  pAccum, int N){.
9fe20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
9fe30 68 61 72 20 7a 53 70 61 63 65 73 5b 5d 20 3d 20  har zSpaces[] = 
9fe40 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  "               
9fe50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 3b                ";
9fe60 0a 20 20 77 68 69 6c 65 28 20 4e 3e 3d 28 69 6e  .  while( N>=(in
9fe70 74 29 73 69 7a 65 6f 66 28 7a 53 70 61 63 65 73  t)sizeof(zSpaces
9fe80 29 2d 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  )-1 ){.    sqlit
9fe90 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64  e3StrAccumAppend
9fea0 28 70 41 63 63 75 6d 2c 20 7a 53 70 61 63 65 73  (pAccum, zSpaces
9feb0 2c 20 73 69 7a 65 6f 66 28 7a 53 70 61 63 65 73  , sizeof(zSpaces
9fec0 29 2d 31 29 3b 0a 20 20 20 20 4e 20 2d 3d 20 73  )-1);.    N -= s
9fed0 69 7a 65 6f 66 28 7a 53 70 61 63 65 73 29 2d 31  izeof(zSpaces)-1
9fee0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3e 30 20  ;.  }.  if( N>0 
9fef0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74  ){.    sqlite3St
9ff00 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63  rAccumAppend(pAc
9ff10 63 75 6d 2c 20 7a 53 70 61 63 65 73 2c 20 4e 29  cum, zSpaces, N)
9ff20 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f  ;.  }.}../*.** O
9ff30 6e 20 6d 61 63 68 69 6e 65 73 20 77 69 74 68 20  n machines with 
9ff40 61 20 73 6d 61 6c 6c 20 73 74 61 63 6b 20 73 69  a small stack si
9ff50 7a 65 2c 20 79 6f 75 20 63 61 6e 20 72 65 64 65  ze, you can rede
9ff60 66 69 6e 65 20 74 68 65 0a 2a 2a 20 53 51 4c 49  fine the.** SQLI
9ff70 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a  TE_PRINT_BUF_SIZ
9ff80 45 20 74 6f 20 62 65 20 6c 65 73 73 20 74 68 61  E to be less tha
9ff90 6e 20 33 35 30 2e 0a 2a 2f 0a 23 69 66 6e 64 65  n 350..*/.#ifnde
9ffa0 66 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42  f SQLITE_PRINT_B
9ffb0 55 46 5f 53 49 5a 45 0a 23 20 69 66 20 64 65 66  UF_SIZE.# if def
9ffc0 69 6e 65 64 28 53 51 4c 49 54 45 5f 53 4d 41 4c  ined(SQLITE_SMAL
9ffd0 4c 5f 53 54 41 43 4b 29 0a 23 20 20 20 64 65 66  L_STACK).#   def
9ffe0 69 6e 65 20 53 51 4c 49 54 45 5f 50 52 49 4e 54  ine SQLITE_PRINT
9fff0 5f 42 55 46 5f 53 49 5a 45 20 35 30 0a 23 20 65  _BUF_SIZE 50.# e
a0000 6c 73 65 0a 23 20 20 20 64 65 66 69 6e 65 20 53  lse.#   define S
a0010 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f  QLITE_PRINT_BUF_
a0020 53 49 5a 45 20 33 35 30 0a 23 20 65 6e 64 69 66  SIZE 350.# endif
a0030 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20  .#endif.#define 
a0040 65 74 42 55 46 53 49 5a 45 20 53 51 4c 49 54 45  etBUFSIZE SQLITE
a0050 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20  _PRINT_BUF_SIZE 
a0060 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20   /* Size of the 
a0070 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f  output buffer */
a0080 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74  ../*.** The root
a0090 20 70 72 6f 67 72 61 6d 2e 20 20 41 6c 6c 20 76   program.  All v
a00a0 61 72 69 61 74 69 6f 6e 73 20 63 61 6c 6c 20 74  ariations call t
a00b0 68 69 73 20 63 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20  his core..**.** 
a00c0 49 4e 50 55 54 53 3a 0a 2a 2a 20 20 20 66 75 6e  INPUTS:.**   fun
a00d0 63 20 20 20 54 68 69 73 20 69 73 20 61 20 70 6f  c   This is a po
a00e0 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74  inter to a funct
a00f0 69 6f 6e 20 74 61 6b 69 6e 67 20 74 68 72 65 65  ion taking three
a0100 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 20 20   arguments.**   
a0110 20 20 20 20 20 20 20 20 20 31 2e 20 41 20 70 6f           1. A po
a0120 69 6e 74 65 72 20 74 6f 20 61 6e 79 74 68 69 6e  inter to anythin
a0130 67 2e 20 20 53 61 6d 65 20 61 73 20 74 68 65 20  g.  Same as the 
a0140 22 61 72 67 22 20 70 61 72 61 6d 65 74 65 72 2e  "arg" parameter.
a0150 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 32  .**            2
a0160 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  . A pointer to t
a0170 68 65 20 6c 69 73 74 20 6f 66 20 63 68 61 72 61  he list of chara
a0180 63 74 65 72 73 20 74 6f 20 62 65 20 6f 75 74 70  cters to be outp
a0190 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ut.**           
a01a0 20 20 20 20 28 4e 6f 74 65 2c 20 74 68 69 73 20      (Note, this 
a01b0 6c 69 73 74 20 69 73 20 4e 4f 54 20 6e 75 6c 6c  list is NOT null
a01c0 20 74 65 72 6d 69 6e 61 74 65 64 2e 29 0a 2a 2a   terminated.).**
a01d0 20 20 20 20 20 20 20 20 20 20 20 20 33 2e 20 41              3. A
a01e0 6e 20 69 6e 74 65 67 65 72 20 6e 75 6d 62 65 72  n integer number
a01f0 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 74   of characters t
a0200 6f 20 62 65 20 6f 75 74 70 75 74 2e 0a 2a 2a 20  o be output..** 
a0210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 4e                (N
a0220 6f 74 65 3a 20 54 68 69 73 20 6e 75 6d 62 65 72  ote: This number
a0230 20 6d 69 67 68 74 20 62 65 20 7a 65 72 6f 2e 29   might be zero.)
a0240 0a 2a 2a 0a 2a 2a 20 20 20 61 72 67 20 20 20 20  .**.**   arg    
a0250 54 68 69 73 20 69 73 20 74 68 65 20 70 6f 69 6e  This is the poin
a0260 74 65 72 20 74 6f 20 61 6e 79 74 68 69 6e 67 20  ter to anything 
a0270 77 68 69 63 68 20 77 69 6c 6c 20 62 65 20 70 61  which will be pa
a0280 73 73 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 20  ssed as the.**  
a0290 20 20 20 20 20 20 20 20 66 69 72 73 74 20 61 72          first ar
a02a0 67 75 6d 65 6e 74 20 74 6f 20 22 66 75 6e 63 22  gument to "func"
a02b0 2e 20 20 55 73 65 20 69 74 20 66 6f 72 20 77 68  .  Use it for wh
a02c0 61 74 65 76 65 72 20 79 6f 75 20 6c 69 6b 65 2e  atever you like.
a02d0 0a 2a 2a 0a 2a 2a 20 20 20 66 6d 74 20 20 20 20  .**.**   fmt    
a02e0 54 68 69 73 20 69 73 20 74 68 65 20 66 6f 72 6d  This is the form
a02f0 61 74 20 73 74 72 69 6e 67 2c 20 61 73 20 69 6e  at string, as in
a0300 20 74 68 65 20 75 73 75 61 6c 20 70 72 69 6e 74   the usual print
a0310 2e 0a 2a 2a 0a 2a 2a 20 20 20 61 70 20 20 20 20  ..**.**   ap    
a0320 20 54 68 69 73 20 69 73 20 61 20 70 6f 69 6e 74   This is a point
a0330 65 72 20 74 6f 20 61 20 6c 69 73 74 20 6f 66 20  er to a list of 
a0340 61 72 67 75 6d 65 6e 74 73 2e 20 20 53 61 6d 65  arguments.  Same
a0350 20 61 73 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20   as in.**       
a0360 20 20 20 76 66 70 72 69 6e 74 2e 0a 2a 2a 0a 2a     vfprint..**.*
a0370 2a 20 4f 55 54 50 55 54 53 3a 0a 2a 2a 20 20 20  * OUTPUTS:.**   
a0380 20 20 20 20 20 20 20 54 68 65 20 72 65 74 75 72         The retur
a0390 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 74  n value is the t
a03a0 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63  otal number of c
a03b0 68 61 72 61 63 74 65 72 73 20 73 65 6e 74 20 74  haracters sent t
a03c0 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  o.**          th
a03d0 65 20 66 75 6e 63 74 69 6f 6e 20 22 66 75 6e 63  e function "func
a03e0 22 2e 20 20 52 65 74 75 72 6e 73 20 2d 31 20 6f  ".  Returns -1 o
a03f0 6e 20 61 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a  n a error..**.**
a0400 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 6f   Note that the o
a0410 72 64 65 72 20 69 6e 20 77 68 69 63 68 20 61 75  rder in which au
a0420 74 6f 6d 61 74 69 63 20 76 61 72 69 61 62 6c 65  tomatic variable
a0430 73 20 61 72 65 20 64 65 63 6c 61 72 65 64 20 62  s are declared b
a0440 65 6c 6f 77 0a 2a 2a 20 73 65 65 6d 73 20 74 6f  elow.** seems to
a0450 20 6d 61 6b 65 20 61 20 62 69 67 20 64 69 66 66   make a big diff
a0460 65 72 65 6e 63 65 20 69 6e 20 64 65 74 65 72 6d  erence in determ
a0470 69 6e 69 6e 67 20 68 6f 77 20 66 61 73 74 20 74  ining how fast t
a0480 68 69 73 20 62 65 61 73 74 0a 2a 2a 20 77 69 6c  his beast.** wil
a0490 6c 20 72 75 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  l run..*/.SQLITE
a04a0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
a04b0 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 0a 20  lite3VXPrintf(. 
a04c0 20 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75   StrAccum *pAccu
a04d0 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m,              
a04e0 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74      /* Accumulat
a04f0 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a  e results here *
a0500 2f 0a 20 20 69 6e 74 20 75 73 65 45 78 74 65 6e  /.  int useExten
a0510 64 65 64 2c 20 20 20 20 20 20 20 20 20 20 20 20  ded,            
a0520 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 77 20         /* Allow 
a0530 65 78 74 65 6e 64 65 64 20 25 2d 63 6f 6e 76 65  extended %-conve
a0540 72 73 69 6f 6e 73 20 2a 2f 0a 20 20 63 6f 6e 73  rsions */.  cons
a0550 74 20 63 68 61 72 20 2a 66 6d 74 2c 20 20 20 20  t char *fmt,    
a0560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
a0570 2a 20 46 6f 72 6d 61 74 20 73 74 72 69 6e 67 20  * Format string 
a0580 2a 2f 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 20  */.  va_list ap 
a0590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a05a0 20 20 20 20 20 20 20 20 2f 2a 20 61 72 67 75 6d          /* argum
a05b0 65 6e 74 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ents */.){.  int
a05c0 20 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   c;             
a05d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20          /* Next 
a05e0 63 68 61 72 61 63 74 65 72 20 69 6e 20 74 68 65  character in the
a05f0 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a   format string *
a0600 2f 0a 20 20 63 68 61 72 20 2a 62 75 66 70 74 3b  /.  char *bufpt;
a0610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
a0620 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  * Pointer to the
a0630 20 63 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66 66   conversion buff
a0640 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 72 65 63  er */.  int prec
a0650 69 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20  ision;          
a0660 20 20 20 2f 2a 20 50 72 65 63 69 73 69 6f 6e 20     /* Precision 
a0670 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 66  of the current f
a0680 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 65  ield */.  int le
a0690 6e 67 74 68 3b 20 20 20 20 20 20 20 20 20 20 20  ngth;           
a06a0 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
a06b0 66 20 74 68 65 20 66 69 65 6c 64 20 2a 2f 0a 20  f the field */. 
a06c0 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20   int idx;       
a06d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
a06e0 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65   general purpose
a06f0 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   loop counter */
a0700 0a 20 20 69 6e 74 20 77 69 64 74 68 3b 20 20 20  .  int width;   
a0710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a0720 20 57 69 64 74 68 20 6f 66 20 74 68 65 20 63 75   Width of the cu
a0730 72 72 65 6e 74 20 66 69 65 6c 64 20 2a 2f 0a 20  rrent field */. 
a0740 20 65 74 42 79 74 65 20 66 6c 61 67 5f 6c 65 66   etByte flag_lef
a0750 74 6a 75 73 74 69 66 79 3b 20 20 20 2f 2a 20 54  tjustify;   /* T
a0760 72 75 65 20 69 66 20 22 2d 22 20 66 6c 61 67 20  rue if "-" flag 
a0770 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20  is present */.  
a0780 65 74 42 79 74 65 20 66 6c 61 67 5f 70 6c 75 73  etByte flag_plus
a0790 73 69 67 6e 3b 20 20 20 20 20 20 2f 2a 20 54 72  sign;      /* Tr
a07a0 75 65 20 69 66 20 22 2b 22 20 66 6c 61 67 20 69  ue if "+" flag i
a07b0 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65  s present */.  e
a07c0 74 42 79 74 65 20 66 6c 61 67 5f 62 6c 61 6e 6b  tByte flag_blank
a07d0 73 69 67 6e 3b 20 20 20 20 20 2f 2a 20 54 72 75  sign;     /* Tru
a07e0 65 20 69 66 20 22 20 22 20 66 6c 61 67 20 69 73  e if " " flag is
a07f0 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74   present */.  et
a0800 42 79 74 65 20 66 6c 61 67 5f 61 6c 74 65 72 6e  Byte flag_altern
a0810 61 74 65 66 6f 72 6d 3b 20 2f 2a 20 54 72 75 65  ateform; /* True
a0820 20 69 66 20 22 23 22 20 66 6c 61 67 20 69 73 20   if "#" flag is 
a0830 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42  present */.  etB
a0840 79 74 65 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d  yte flag_altform
a0850 32 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  2;      /* True 
a0860 69 66 20 22 21 22 20 66 6c 61 67 20 69 73 20 70  if "!" flag is p
a0870 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79  resent */.  etBy
a0880 74 65 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 3b  te flag_zeropad;
a0890 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
a08a0 66 20 66 69 65 6c 64 20 77 69 64 74 68 20 63 6f  f field width co
a08b0 6e 73 74 61 6e 74 20 73 74 61 72 74 73 20 77 69  nstant starts wi
a08c0 74 68 20 7a 65 72 6f 20 2a 2f 0a 20 20 65 74 42  th zero */.  etB
a08d0 79 74 65 20 66 6c 61 67 5f 6c 6f 6e 67 3b 20 20  yte flag_long;  
a08e0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
a08f0 69 66 20 22 6c 22 20 66 6c 61 67 20 69 73 20 70  if "l" flag is p
a0900 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79  resent */.  etBy
a0910 74 65 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67  te flag_longlong
a0920 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  ;      /* True i
a0930 66 20 74 68 65 20 22 6c 6c 22 20 66 6c 61 67 20  f the "ll" flag 
a0940 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20  is present */.  
a0950 65 74 42 79 74 65 20 64 6f 6e 65 3b 20 20 20 20  etByte done;    
a0960 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
a0970 6f 70 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20 66  op termination f
a0980 6c 61 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f  lag */.  sqlite_
a0990 75 69 6e 74 36 34 20 6c 6f 6e 67 76 61 6c 75 65  uint64 longvalue
a09a0 3b 20 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72  ;   /* Value for
a09b0 20 69 6e 74 65 67 65 72 20 74 79 70 65 73 20 2a   integer types *
a09c0 2f 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54  /.  LONGDOUBLE_T
a09d0 59 50 45 20 72 65 61 6c 76 61 6c 75 65 3b 20 2f  YPE realvalue; /
a09e0 2a 20 56 61 6c 75 65 20 66 6f 72 20 72 65 61 6c  * Value for real
a09f0 20 74 79 70 65 73 20 2a 2f 0a 20 20 63 6f 6e 73   types */.  cons
a0a00 74 20 65 74 5f 69 6e 66 6f 20 2a 69 6e 66 6f 70  t et_info *infop
a0a10 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65  ;      /* Pointe
a0a20 72 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72  r to the appropr
a0a30 69 61 74 65 20 69 6e 66 6f 20 73 74 72 75 63 74  iate info struct
a0a40 75 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 62 75  ure */.  char bu
a0a50 66 5b 65 74 42 55 46 53 49 5a 45 5d 3b 20 20 20  f[etBUFSIZE];   
a0a60 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f      /* Conversio
a0a70 6e 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 63 68  n buffer */.  ch
a0a80 61 72 20 70 72 65 66 69 78 3b 20 20 20 20 20 20  ar prefix;      
a0a90 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66           /* Pref
a0aa0 69 78 20 63 68 61 72 61 63 74 65 72 2e 20 20 22  ix character.  "
a0ab0 2b 22 20 6f 72 20 22 2d 22 20 6f 72 20 22 20 22  +" or "-" or " "
a0ac0 20 6f 72 20 27 5c 30 27 2e 20 2a 2f 0a 20 20 65   or '\0'. */.  e
a0ad0 74 42 79 74 65 20 78 74 79 70 65 20 3d 20 30 3b  tByte xtype = 0;
a0ae0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e            /* Con
a0af0 76 65 72 73 69 6f 6e 20 70 61 72 61 64 69 67 6d  version paradigm
a0b00 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 78 74   */.  char *zExt
a0b10 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ra;             
a0b20 20 2f 2a 20 45 78 74 72 61 20 6d 65 6d 6f 72 79   /* Extra memory
a0b30 20 75 73 65 64 20 66 6f 72 20 65 74 54 43 4c 45   used for etTCLE
a0b40 53 43 41 50 45 20 63 6f 6e 76 65 72 73 69 6f 6e  SCAPE conversion
a0b50 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  s */.#ifndef SQL
a0b60 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
a0b70 47 5f 50 4f 49 4e 54 0a 20 20 69 6e 74 20 20 65  G_POINT.  int  e
a0b80 78 70 2c 20 65 32 3b 20 20 20 20 20 20 20 20 20  xp, e2;         
a0b90 20 20 20 20 20 2f 2a 20 65 78 70 6f 6e 65 6e 74       /* exponent
a0ba0 20 6f 66 20 72 65 61 6c 20 6e 75 6d 62 65 72 73   of real numbers
a0bb0 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 6f 75   */.  double rou
a0bc0 6e 64 65 72 3b 20 20 20 20 20 20 20 20 20 20 20  nder;           
a0bd0 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 72 6f 75   /* Used for rou
a0be0 6e 64 69 6e 67 20 66 6c 6f 61 74 69 6e 67 20 70  nding floating p
a0bf0 6f 69 6e 74 20 76 61 6c 75 65 73 20 2a 2f 0a 20  oint values */. 
a0c00 20 65 74 42 79 74 65 20 66 6c 61 67 5f 64 70 3b   etByte flag_dp;
a0c10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
a0c20 72 75 65 20 69 66 20 64 65 63 69 6d 61 6c 20 70  rue if decimal p
a0c30 6f 69 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 73  oint should be s
a0c40 68 6f 77 6e 20 2a 2f 0a 20 20 65 74 42 79 74 65  hown */.  etByte
a0c50 20 66 6c 61 67 5f 72 74 7a 3b 20 20 20 20 20 20   flag_rtz;      
a0c60 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
a0c70 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20 73  trailing zeros s
a0c80 68 6f 75 6c 64 20 62 65 20 72 65 6d 6f 76 65 64  hould be removed
a0c90 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61   */.  etByte fla
a0ca0 67 5f 65 78 70 3b 20 20 20 20 20 20 20 20 20 20  g_exp;          
a0cb0 20 2f 2a 20 54 72 75 65 20 74 6f 20 66 6f 72 63   /* True to forc
a0cc0 65 20 64 69 73 70 6c 61 79 20 6f 66 20 74 68 65  e display of the
a0cd0 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 69   exponent */.  i
a0ce0 6e 74 20 6e 73 64 3b 20 20 20 20 20 20 20 20 20  nt nsd;         
a0cf0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
a0d00 62 65 72 20 6f 66 20 73 69 67 6e 69 66 69 63 61  ber of significa
a0d10 6e 74 20 64 69 67 69 74 73 20 72 65 74 75 72 6e  nt digits return
a0d20 65 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20  ed */.#endif..  
a0d30 6c 65 6e 67 74 68 20 3d 20 30 3b 0a 20 20 62 75  length = 0;.  bu
a0d40 66 70 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 3b  fpt = 0;.  for(;
a0d50 20 28 63 3d 28 2a 66 6d 74 29 29 21 3d 30 3b 20   (c=(*fmt))!=0; 
a0d60 2b 2b 66 6d 74 29 7b 0a 20 20 20 20 69 66 28 20  ++fmt){.    if( 
a0d70 63 21 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20  c!='%' ){.      
a0d80 69 6e 74 20 61 6d 74 3b 0a 20 20 20 20 20 20 62  int amt;.      b
a0d90 75 66 70 74 20 3d 20 28 63 68 61 72 20 2a 29 66  ufpt = (char *)f
a0da0 6d 74 3b 0a 20 20 20 20 20 20 61 6d 74 20 3d 20  mt;.      amt = 
a0db0 31 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  1;.      while( 
a0dc0 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 27 25  (c=(*++fmt))!='%
a0dd0 27 20 26 26 20 63 21 3d 30 20 29 20 61 6d 74 2b  ' && c!=0 ) amt+
a0de0 2b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  +;.      sqlite3
a0df0 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70  StrAccumAppend(p
a0e00 41 63 63 75 6d 2c 20 62 75 66 70 74 2c 20 61 6d  Accum, bufpt, am
a0e10 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d  t);.      if( c=
a0e20 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =0 ) break;.    
a0e30 7d 0a 20 20 20 20 69 66 28 20 28 63 3d 28 2a 2b  }.    if( (c=(*+
a0e40 2b 66 6d 74 29 29 3d 3d 30 20 29 7b 0a 20 20 20  +fmt))==0 ){.   
a0e50 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
a0e60 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c  umAppend(pAccum,
a0e70 20 22 25 22 2c 20 31 29 3b 0a 20 20 20 20 20 20   "%", 1);.      
a0e80 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
a0e90 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20 77 68 61   /* Find out wha
a0ea0 74 20 66 6c 61 67 73 20 61 72 65 20 70 72 65 73  t flags are pres
a0eb0 65 6e 74 20 2a 2f 0a 20 20 20 20 66 6c 61 67 5f  ent */.    flag_
a0ec0 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20 66 6c  leftjustify = fl
a0ed0 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d 20 66 6c  ag_plussign = fl
a0ee0 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 0a  ag_blanksign = .
a0ef0 20 20 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e       flag_altern
a0f00 61 74 65 66 6f 72 6d 20 3d 20 66 6c 61 67 5f 61  ateform = flag_a
a0f10 6c 74 66 6f 72 6d 32 20 3d 20 66 6c 61 67 5f 7a  ltform2 = flag_z
a0f20 65 72 6f 70 61 64 20 3d 20 30 3b 0a 20 20 20 20  eropad = 0;.    
a0f30 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20 64 6f  done = 0;.    do
a0f40 7b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 20  {.      switch( 
a0f50 63 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73  c ){.        cas
a0f60 65 20 27 2d 27 3a 20 20 20 66 6c 61 67 5f 6c 65  e '-':   flag_le
a0f70 66 74 6a 75 73 74 69 66 79 20 3d 20 31 3b 20 20  ftjustify = 1;  
a0f80 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
a0f90 20 20 63 61 73 65 20 27 2b 27 3a 20 20 20 66 6c    case '+':   fl
a0fa0 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d 20 31 3b  ag_plussign = 1;
a0fb0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
a0fc0 20 20 20 20 20 20 20 63 61 73 65 20 27 20 27 3a         case ' ':
a0fd0 20 20 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67     flag_blanksig
a0fe0 6e 20 3d 20 31 3b 20 20 20 20 20 20 20 62 72 65  n = 1;       bre
a0ff0 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
a1000 20 27 23 27 3a 20 20 20 66 6c 61 67 5f 61 6c 74   '#':   flag_alt
a1010 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20 31 3b 20  ernateform = 1; 
a1020 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
a1030 20 63 61 73 65 20 27 21 27 3a 20 20 20 66 6c 61   case '!':   fla
a1040 67 5f 61 6c 74 66 6f 72 6d 32 20 3d 20 31 3b 20  g_altform2 = 1; 
a1050 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
a1060 20 20 20 20 20 20 63 61 73 65 20 27 30 27 3a 20        case '0': 
a1070 20 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 3d    flag_zeropad =
a1080 20 31 3b 20 20 20 20 20 20 20 20 20 62 72 65 61   1;         brea
a1090 6b 3b 0a 20 20 20 20 20 20 20 20 64 65 66 61 75  k;.        defau
a10a0 6c 74 3a 20 20 20 20 64 6f 6e 65 20 3d 20 31 3b  lt:    done = 1;
a10b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a10c0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
a10d0 20 20 20 20 7d 77 68 69 6c 65 28 20 21 64 6f 6e      }while( !don
a10e0 65 20 26 26 20 28 63 3d 28 2a 2b 2b 66 6d 74 29  e && (c=(*++fmt)
a10f0 29 21 3d 30 20 29 3b 0a 20 20 20 20 2f 2a 20 47  )!=0 );.    /* G
a1100 65 74 20 74 68 65 20 66 69 65 6c 64 20 77 69 64  et the field wid
a1110 74 68 20 2a 2f 0a 20 20 20 20 77 69 64 74 68 20  th */.    width 
a1120 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 63 3d 3d  = 0;.    if( c==
a1130 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 77 69 64  '*' ){.      wid
a1140 74 68 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69  th = va_arg(ap,i
a1150 6e 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 77  nt);.      if( w
a1160 69 64 74 68 3c 30 20 29 7b 0a 20 20 20 20 20 20  idth<0 ){.      
a1170 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69    flag_leftjusti
a1180 66 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  fy = 1;.        
a1190 77 69 64 74 68 20 3d 20 2d 77 69 64 74 68 3b 0a  width = -width;.
a11a0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 20        }.      c 
a11b0 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 7d 65  = *++fmt;.    }e
a11c0 6c 73 65 7b 0a 20 20 20 20 20 20 77 68 69 6c 65  lse{.      while
a11d0 28 20 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27  ( c>='0' && c<='
a11e0 39 27 20 29 7b 0a 20 20 20 20 20 20 20 20 77 69  9' ){.        wi
a11f0 64 74 68 20 3d 20 77 69 64 74 68 2a 31 30 20 2b  dth = width*10 +
a1200 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20   c - '0';.      
a1210 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20    c = *++fmt;.  
a1220 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
a1230 69 66 28 20 77 69 64 74 68 20 3e 20 65 74 42 55  if( width > etBU
a1240 46 53 49 5a 45 2d 31 30 20 29 7b 0a 20 20 20 20  FSIZE-10 ){.    
a1250 20 20 77 69 64 74 68 20 3d 20 65 74 42 55 46 53    width = etBUFS
a1260 49 5a 45 2d 31 30 3b 0a 20 20 20 20 7d 0a 20 20  IZE-10;.    }.  
a1270 20 20 2f 2a 20 47 65 74 20 74 68 65 20 70 72 65    /* Get the pre
a1280 63 69 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66  cision */.    if
a1290 28 20 63 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20  ( c=='.' ){.    
a12a0 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 30 3b    precision = 0;
a12b0 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d  .      c = *++fm
a12c0 74 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d  t;.      if( c==
a12d0 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 70  '*' ){.        p
a12e0 72 65 63 69 73 69 6f 6e 20 3d 20 76 61 5f 61 72  recision = va_ar
a12f0 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20  g(ap,int);.     
a1300 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e     if( precision
a1310 3c 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 3d  <0 ) precision =
a1320 20 2d 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20 20   -precision;.   
a1330 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b       c = *++fmt;
a1340 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
a1350 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 3e 3d        while( c>=
a1360 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b  '0' && c<='9' ){
a1370 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 63 69  .          preci
a1380 73 69 6f 6e 20 3d 20 70 72 65 63 69 73 69 6f 6e  sion = precision
a1390 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20  *10 + c - '0';. 
a13a0 20 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b           c = *++
a13b0 66 6d 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  fmt;.        }. 
a13c0 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
a13d0 7b 0a 20 20 20 20 20 20 70 72 65 63 69 73 69 6f  {.      precisio
a13e0 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20  n = -1;.    }.  
a13f0 20 20 2f 2a 20 47 65 74 20 74 68 65 20 63 6f 6e    /* Get the con
a1400 76 65 72 73 69 6f 6e 20 74 79 70 65 20 6d 6f 64  version type mod
a1410 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 66 28  ifier */.    if(
a1420 20 63 3d 3d 27 6c 27 20 29 7b 0a 20 20 20 20 20   c=='l' ){.     
a1430 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 31 3b 0a   flag_long = 1;.
a1440 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74        c = *++fmt
a1450 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27  ;.      if( c=='
a1460 6c 27 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6c  l' ){.        fl
a1470 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 31 3b  ag_longlong = 1;
a1480 0a 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b  .        c = *++
a1490 66 6d 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  fmt;.      }else
a14a0 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c  {.        flag_l
a14b0 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20 20  onglong = 0;.   
a14c0 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
a14d0 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 20        flag_long 
a14e0 3d 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20  = flag_longlong 
a14f0 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f  = 0;.    }.    /
a1500 2a 20 46 65 74 63 68 20 74 68 65 20 69 6e 66 6f  * Fetch the info
a1510 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 66   entry for the f
a1520 69 65 6c 64 20 2a 2f 0a 20 20 20 20 69 6e 66 6f  ield */.    info
a1530 70 20 3d 20 26 66 6d 74 69 6e 66 6f 5b 30 5d 3b  p = &fmtinfo[0];
a1540 0a 20 20 20 20 78 74 79 70 65 20 3d 20 65 74 49  .    xtype = etI
a1550 4e 56 41 4c 49 44 3b 0a 20 20 20 20 66 6f 72 28  NVALID;.    for(
a1560 69 64 78 3d 30 3b 20 69 64 78 3c 41 72 72 61 79  idx=0; idx<Array
a1570 53 69 7a 65 28 66 6d 74 69 6e 66 6f 29 3b 20 69  Size(fmtinfo); i
a1580 64 78 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28  dx++){.      if(
a1590 20 63 3d 3d 66 6d 74 69 6e 66 6f 5b 69 64 78 5d   c==fmtinfo[idx]
a15a0 2e 66 6d 74 74 79 70 65 20 29 7b 0a 20 20 20 20  .fmttype ){.    
a15b0 20 20 20 20 69 6e 66 6f 70 20 3d 20 26 66 6d 74      infop = &fmt
a15c0 69 6e 66 6f 5b 69 64 78 5d 3b 0a 20 20 20 20 20  info[idx];.     
a15d0 20 20 20 69 66 28 20 75 73 65 45 78 74 65 6e 64     if( useExtend
a15e0 65 64 20 7c 7c 20 28 69 6e 66 6f 70 2d 3e 66 6c  ed || (infop->fl
a15f0 61 67 73 20 26 20 46 4c 41 47 5f 49 4e 54 45 52  ags & FLAG_INTER
a1600 4e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  N)==0 ){.       
a1610 20 20 20 78 74 79 70 65 20 3d 20 69 6e 66 6f 70     xtype = infop
a1620 2d 3e 74 79 70 65 3b 0a 20 20 20 20 20 20 20 20  ->type;.        
a1630 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
a1640 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20   return;.       
a1650 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
a1660 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
a1670 20 20 20 20 7a 45 78 74 72 61 20 3d 20 30 3b 0a      zExtra = 0;.
a1680 0a 0a 20 20 20 20 2f 2a 20 4c 69 6d 69 74 20 74  ..    /* Limit t
a1690 68 65 20 70 72 65 63 69 73 69 6f 6e 20 74 6f 20  he precision to 
a16a0 70 72 65 76 65 6e 74 20 6f 76 65 72 66 6c 6f 77  prevent overflow
a16b0 69 6e 67 20 62 75 66 5b 5d 20 64 75 72 69 6e 67  ing buf[] during
a16c0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a 20   conversion */. 
a16d0 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e     if( precision
a16e0 3e 65 74 42 55 46 53 49 5a 45 2d 34 30 20 26 26  >etBUFSIZE-40 &&
a16f0 20 28 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26   (infop->flags &
a1700 20 46 4c 41 47 5f 53 54 52 49 4e 47 29 3d 3d 30   FLAG_STRING)==0
a1710 20 29 7b 0a 20 20 20 20 20 20 70 72 65 63 69 73   ){.      precis
a1720 69 6f 6e 20 3d 20 65 74 42 55 46 53 49 5a 45 2d  ion = etBUFSIZE-
a1730 34 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  40;.    }..    /
a1740 2a 0a 20 20 20 20 2a 2a 20 41 74 20 74 68 69 73  *.    ** At this
a1750 20 70 6f 69 6e 74 2c 20 76 61 72 69 61 62 6c 65   point, variable
a1760 73 20 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65  s are initialize
a1770 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20  d as follows:.  
a1780 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 66 6c    **.    **   fl
a1790 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d  ag_alternateform
a17a0 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69            TRUE i
a17b0 66 20 61 20 27 23 27 20 69 73 20 70 72 65 73 65  f a '#' is prese
a17c0 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61  nt..    **   fla
a17d0 67 5f 61 6c 74 66 6f 72 6d 32 20 20 20 20 20 20  g_altform2      
a17e0 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66           TRUE if
a17f0 20 61 20 27 21 27 20 69 73 20 70 72 65 73 65 6e   a '!' is presen
a1800 74 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67  t..    **   flag
a1810 5f 70 6c 75 73 73 69 67 6e 20 20 20 20 20 20 20  _plussign       
a1820 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20          TRUE if 
a1830 61 20 27 2b 27 20 69 73 20 70 72 65 73 65 6e 74  a '+' is present
a1840 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f  ..    **   flag_
a1850 6c 65 66 74 6a 75 73 74 69 66 79 20 20 20 20 20  leftjustify     
a1860 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 61         TRUE if a
a1870 20 27 2d 27 20 69 73 20 70 72 65 73 65 6e 74 20   '-' is present 
a1880 6f 72 20 69 66 20 74 68 65 0a 20 20 20 20 2a 2a  or if the.    **
a1890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a18a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66                 f
a18b0 69 65 6c 64 20 77 69 64 74 68 20 77 61 73 20 6e  ield width was n
a18c0 65 67 61 74 69 76 65 2e 0a 20 20 20 20 2a 2a 20  egative..    ** 
a18d0 20 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 20    flag_zeropad  
a18e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52                TR
a18f0 55 45 20 69 66 20 74 68 65 20 77 69 64 74 68 20  UE if the width 
a1900 62 65 67 61 6e 20 77 69 74 68 20 30 2e 0a 20 20  began with 0..  
a1910 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f 6e 67    **   flag_long
a1920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1930 20 20 20 54 52 55 45 20 69 66 20 74 68 65 20 6c     TRUE if the l
a1940 65 74 74 65 72 20 27 6c 27 20 28 65 6c 6c 29 20  etter 'l' (ell) 
a1950 70 72 65 66 69 78 65 64 0a 20 20 20 20 2a 2a 20  prefixed.    ** 
a1960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
a1980 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 63 68 61  e conversion cha
a1990 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20 20  racter..    **  
a19a0 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 20   flag_longlong  
a19b0 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55               TRU
a19c0 45 20 69 66 20 74 68 65 20 6c 65 74 74 65 72 20  E if the letter 
a19d0 27 6c 6c 27 20 28 65 6c 6c 20 65 6c 6c 29 20 70  'll' (ell ell) p
a19e0 72 65 66 69 78 65 64 0a 20 20 20 20 2a 2a 20 20  refixed.    **  
a19f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1a00 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65               the
a1a10 20 63 6f 6e 76 65 72 73 69 6f 6e 20 63 68 61 72   conversion char
a1a20 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20 20 20  acter..    **   
a1a30 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 20  flag_blanksign  
a1a40 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55 45              TRUE
a1a50 20 69 66 20 61 20 27 20 27 20 69 73 20 70 72 65   if a ' ' is pre
a1a60 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 20 77  sent..    **   w
a1a70 69 64 74 68 20 20 20 20 20 20 20 20 20 20 20 20  idth            
a1a80 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 73             The s
a1a90 70 65 63 69 66 69 65 64 20 66 69 65 6c 64 20 77  pecified field w
a1aa0 69 64 74 68 2e 20 20 54 68 69 73 20 69 73 0a 20  idth.  This is. 
a1ab0 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20     **           
a1ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1ad0 20 20 20 20 61 6c 77 61 79 73 20 6e 6f 6e 2d 6e      always non-n
a1ae0 65 67 61 74 69 76 65 2e 20 20 5a 65 72 6f 20 69  egative.  Zero i
a1af0 73 20 74 68 65 20 64 65 66 61 75 6c 74 2e 0a 20  s the default.. 
a1b00 20 20 20 2a 2a 20 20 20 70 72 65 63 69 73 69 6f     **   precisio
a1b10 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
a1b20 20 20 20 20 54 68 65 20 73 70 65 63 69 66 69 65      The specifie
a1b30 64 20 70 72 65 63 69 73 69 6f 6e 2e 20 20 54 68  d precision.  Th
a1b40 65 20 64 65 66 61 75 6c 74 0a 20 20 20 20 2a 2a  e default.    **
a1b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
a1b70 73 20 2d 31 2e 0a 20 20 20 20 2a 2a 20 20 20 78  s -1..    **   x
a1b80 74 79 70 65 20 20 20 20 20 20 20 20 20 20 20 20  type            
a1b90 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 63             The c
a1ba0 6c 61 73 73 20 6f 66 20 74 68 65 20 63 6f 6e 76  lass of the conv
a1bb0 65 72 73 69 6f 6e 2e 0a 20 20 20 20 2a 2a 20 20  ersion..    **  
a1bc0 20 69 6e 66 6f 70 20 20 20 20 20 20 20 20 20 20   infop          
a1bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 50 6f 69               Poi
a1be0 6e 74 65 72 20 74 6f 20 74 68 65 20 61 70 70 72  nter to the appr
a1bf0 6f 70 72 69 61 74 65 20 69 6e 66 6f 20 73 74 72  opriate info str
a1c00 75 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  uct..    */.    
a1c10 73 77 69 74 63 68 28 20 78 74 79 70 65 20 29 7b  switch( xtype ){
a1c20 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 50 4f  .      case etPO
a1c30 49 4e 54 45 52 3a 0a 20 20 20 20 20 20 20 20 66  INTER:.        f
a1c40 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 73  lag_longlong = s
a1c50 69 7a 65 6f 66 28 63 68 61 72 2a 29 3d 3d 73 69  izeof(char*)==si
a1c60 7a 65 6f 66 28 69 36 34 29 3b 0a 20 20 20 20 20  zeof(i64);.     
a1c70 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 73     flag_long = s
a1c80 69 7a 65 6f 66 28 63 68 61 72 2a 29 3d 3d 73 69  izeof(char*)==si
a1c90 7a 65 6f 66 28 6c 6f 6e 67 20 69 6e 74 29 3b 0a  zeof(long int);.
a1ca0 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20          /* Fall 
a1cb0 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 74 68 65  through into the
a1cc0 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a 20 20   next case */.  
a1cd0 20 20 20 20 63 61 73 65 20 65 74 4f 52 44 49 4e      case etORDIN
a1ce0 41 4c 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65  AL:.      case e
a1cf0 74 52 41 44 49 58 3a 0a 20 20 20 20 20 20 20 20  tRADIX:.        
a1d00 69 66 28 20 69 6e 66 6f 70 2d 3e 66 6c 61 67 73  if( infop->flags
a1d10 20 26 20 46 4c 41 47 5f 53 49 47 4e 45 44 20 29   & FLAG_SIGNED )
a1d20 7b 0a 20 20 20 20 20 20 20 20 20 20 69 36 34 20  {.          i64 
a1d30 76 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  v;.          if(
a1d40 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 29   flag_longlong )
a1d50 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 76 20  {.            v 
a1d60 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 36 34 29  = va_arg(ap,i64)
a1d70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
a1d80 65 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 20  e if( flag_long 
a1d90 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 76  ){.            v
a1da0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 6c 6f 6e   = va_arg(ap,lon
a1db0 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20  g int);.        
a1dc0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
a1dd0 20 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67 28       v = va_arg(
a1de0 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 20  ap,int);.       
a1df0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69     }.          i
a1e00 66 28 20 76 3c 30 20 29 7b 0a 20 20 20 20 20 20  f( v<0 ){.      
a1e10 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20        longvalue 
a1e20 3d 20 2d 76 3b 0a 20 20 20 20 20 20 20 20 20 20  = -v;.          
a1e30 20 20 70 72 65 66 69 78 20 3d 20 27 2d 27 3b 0a    prefix = '-';.
a1e40 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
a1e50 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e  .            lon
a1e60 67 76 61 6c 75 65 20 3d 20 76 3b 0a 20 20 20 20  gvalue = v;.    
a1e70 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67          if( flag
a1e80 5f 70 6c 75 73 73 69 67 6e 20 29 20 20 20 20 20  _plussign )     
a1e90 20 20 20 70 72 65 66 69 78 20 3d 20 27 2b 27 3b     prefix = '+';
a1ea0 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 6c 73  .            els
a1eb0 65 20 69 66 28 20 66 6c 61 67 5f 62 6c 61 6e 6b  e if( flag_blank
a1ec0 73 69 67 6e 20 29 20 20 70 72 65 66 69 78 20 3d  sign )  prefix =
a1ed0 20 27 20 27 3b 0a 20 20 20 20 20 20 20 20 20 20   ' ';.          
a1ee0 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 20    else          
a1ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 70 72 65               pre
a1f00 66 69 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  fix = 0;.       
a1f10 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c     }.        }el
a1f20 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  se{.          if
a1f30 28 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20  ( flag_longlong 
a1f40 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c  ){.            l
a1f50 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 72  ongvalue = va_ar
a1f60 67 28 61 70 2c 75 36 34 29 3b 0a 20 20 20 20 20  g(ap,u64);.     
a1f70 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66       }else if( f
a1f80 6c 61 67 5f 6c 6f 6e 67 20 29 7b 0a 20 20 20 20  lag_long ){.    
a1f90 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75          longvalu
a1fa0 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 6e  e = va_arg(ap,un
a1fb0 73 69 67 6e 65 64 20 6c 6f 6e 67 20 69 6e 74 29  signed long int)
a1fc0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
a1fd0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c  e{.            l
a1fe0 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 72  ongvalue = va_ar
a1ff0 67 28 61 70 2c 75 6e 73 69 67 6e 65 64 20 69 6e  g(ap,unsigned in
a2000 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  t);.          }.
a2010 20 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78            prefix
a2020 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 0;.        }.
a2030 20 20 20 20 20 20 20 20 69 66 28 20 6c 6f 6e 67          if( long
a2040 76 61 6c 75 65 3d 3d 30 20 29 20 66 6c 61 67 5f  value==0 ) flag_
a2050 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20  alternateform = 
a2060 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 66  0;.        if( f
a2070 6c 61 67 5f 7a 65 72 6f 70 61 64 20 26 26 20 70  lag_zeropad && p
a2080 72 65 63 69 73 69 6f 6e 3c 77 69 64 74 68 2d 28  recision<width-(
a2090 70 72 65 66 69 78 21 3d 30 29 20 29 7b 0a 20 20  prefix!=0) ){.  
a20a0 20 20 20 20 20 20 20 20 70 72 65 63 69 73 69 6f          precisio
a20b0 6e 20 3d 20 77 69 64 74 68 2d 28 70 72 65 66 69  n = width-(prefi
a20c0 78 21 3d 30 29 3b 0a 20 20 20 20 20 20 20 20 7d  x!=0);.        }
a20d0 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d  .        bufpt =
a20e0 20 26 62 75 66 5b 65 74 42 55 46 53 49 5a 45 2d   &buf[etBUFSIZE-
a20f0 31 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  1];.        if( 
a2100 78 74 79 70 65 3d 3d 65 74 4f 52 44 49 4e 41 4c  xtype==etORDINAL
a2110 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 74   ){.          st
a2120 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
a2130 7a 4f 72 64 5b 5d 20 3d 20 22 74 68 73 74 6e 64  zOrd[] = "thstnd
a2140 72 64 22 3b 0a 20 20 20 20 20 20 20 20 20 20 69  rd";.          i
a2150 6e 74 20 78 20 3d 20 28 69 6e 74 29 28 6c 6f 6e  nt x = (int)(lon
a2160 67 76 61 6c 75 65 20 25 20 31 30 29 3b 0a 20 20  gvalue % 10);.  
a2170 20 20 20 20 20 20 20 20 69 66 28 20 78 3e 3d 34          if( x>=4
a2180 20 7c 7c 20 28 6c 6f 6e 67 76 61 6c 75 65 2f 31   || (longvalue/1
a2190 30 29 25 31 30 3d 3d 31 20 29 7b 0a 20 20 20 20  0)%10==1 ){.    
a21a0 20 20 20 20 20 20 20 20 78 20 3d 20 30 3b 0a 20          x = 0;. 
a21b0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
a21c0 20 20 20 20 20 62 75 66 5b 65 74 42 55 46 53 49       buf[etBUFSI
a21d0 5a 45 2d 33 5d 20 3d 20 7a 4f 72 64 5b 78 2a 32  ZE-3] = zOrd[x*2
a21e0 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 75 66  ];.          buf
a21f0 5b 65 74 42 55 46 53 49 5a 45 2d 32 5d 20 3d 20  [etBUFSIZE-2] = 
a2200 7a 4f 72 64 5b 78 2a 32 2b 31 5d 3b 0a 20 20 20  zOrd[x*2+1];.   
a2210 20 20 20 20 20 20 20 62 75 66 70 74 20 2d 3d 20         bufpt -= 
a2220 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  2;.        }.   
a2230 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20       {.         
a2240 20 72 65 67 69 73 74 65 72 20 63 6f 6e 73 74 20   register const 
a2250 63 68 61 72 20 2a 63 73 65 74 3b 20 20 20 20 20  char *cset;     
a2260 20 2f 2a 20 55 73 65 20 72 65 67 69 73 74 65 72   /* Use register
a2270 73 20 66 6f 72 20 73 70 65 65 64 20 2a 2f 0a 20  s for speed */. 
a2280 20 20 20 20 20 20 20 20 20 72 65 67 69 73 74 65           registe
a2290 72 20 69 6e 74 20 62 61 73 65 3b 0a 20 20 20 20  r int base;.    
a22a0 20 20 20 20 20 20 63 73 65 74 20 3d 20 26 61 44        cset = &aD
a22b0 69 67 69 74 73 5b 69 6e 66 6f 70 2d 3e 63 68 61  igits[infop->cha
a22c0 72 73 65 74 5d 3b 0a 20 20 20 20 20 20 20 20 20  rset];.         
a22d0 20 62 61 73 65 20 3d 20 69 6e 66 6f 70 2d 3e 62   base = infop->b
a22e0 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20 64  ase;.          d
a22f0 6f 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o{              
a2300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2310 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a2320 43 6f 6e 76 65 72 74 20 74 6f 20 61 73 63 69 69  Convert to ascii
a2330 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
a2340 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 63 73 65  *(--bufpt) = cse
a2350 74 5b 6c 6f 6e 67 76 61 6c 75 65 25 62 61 73 65  t[longvalue%base
a2360 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c  ];.            l
a2370 6f 6e 67 76 61 6c 75 65 20 3d 20 6c 6f 6e 67 76  ongvalue = longv
a2380 61 6c 75 65 2f 62 61 73 65 3b 0a 20 20 20 20 20  alue/base;.     
a2390 20 20 20 20 20 7d 77 68 69 6c 65 28 20 6c 6f 6e       }while( lon
a23a0 67 76 61 6c 75 65 3e 30 20 29 3b 0a 20 20 20 20  gvalue>0 );.    
a23b0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6c 65      }.        le
a23c0 6e 67 74 68 20 3d 20 28 69 6e 74 29 28 26 62 75  ngth = (int)(&bu
a23d0 66 5b 65 74 42 55 46 53 49 5a 45 2d 31 5d 2d 62  f[etBUFSIZE-1]-b
a23e0 75 66 70 74 29 3b 0a 20 20 20 20 20 20 20 20 66  ufpt);.        f
a23f0 6f 72 28 69 64 78 3d 70 72 65 63 69 73 69 6f 6e  or(idx=precision
a2400 2d 6c 65 6e 67 74 68 3b 20 69 64 78 3e 30 3b 20  -length; idx>0; 
a2410 69 64 78 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20  idx--){.        
a2420 20 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 27    *(--bufpt) = '
a2430 30 27 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  0';             
a2440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2450 2f 2a 20 5a 65 72 6f 20 70 61 64 20 2a 2f 0a 20  /* Zero pad */. 
a2460 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a2470 20 69 66 28 20 70 72 65 66 69 78 20 29 20 2a 28   if( prefix ) *(
a2480 2d 2d 62 75 66 70 74 29 20 3d 20 70 72 65 66 69  --bufpt) = prefi
a2490 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
a24a0 20 2f 2a 20 41 64 64 20 73 69 67 6e 20 2a 2f 0a   /* Add sign */.
a24b0 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67          if( flag
a24c0 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 26  _alternateform &
a24d0 26 20 69 6e 66 6f 70 2d 3e 70 72 65 66 69 78 20  & infop->prefix 
a24e0 29 7b 20 20 20 20 20 20 2f 2a 20 41 64 64 20 22  ){      /* Add "
a24f0 30 22 20 6f 72 20 22 30 78 22 20 2a 2f 0a 20 20  0" or "0x" */.  
a2500 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
a2510 61 72 20 2a 70 72 65 3b 0a 20 20 20 20 20 20 20  ar *pre;.       
a2520 20 20 20 63 68 61 72 20 78 3b 0a 20 20 20 20 20     char x;.     
a2530 20 20 20 20 20 70 72 65 20 3d 20 26 61 50 72 65       pre = &aPre
a2540 66 69 78 5b 69 6e 66 6f 70 2d 3e 70 72 65 66 69  fix[infop->prefi
a2550 78 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f  x];.          fo
a2560 72 28 3b 20 28 78 3d 28 2a 70 72 65 29 29 21 3d  r(; (x=(*pre))!=
a2570 30 3b 20 70 72 65 2b 2b 29 20 2a 28 2d 2d 62 75  0; pre++) *(--bu
a2580 66 70 74 29 20 3d 20 78 3b 0a 20 20 20 20 20 20  fpt) = x;.      
a2590 20 20 7d 0a 20 20 20 20 20 20 20 20 6c 65 6e 67    }.        leng
a25a0 74 68 20 3d 20 28 69 6e 74 29 28 26 62 75 66 5b  th = (int)(&buf[
a25b0 65 74 42 55 46 53 49 5a 45 2d 31 5d 2d 62 75 66  etBUFSIZE-1]-buf
a25c0 70 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  pt);.        bre
a25d0 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65  ak;.      case e
a25e0 74 46 4c 4f 41 54 3a 0a 20 20 20 20 20 20 63 61  tFLOAT:.      ca
a25f0 73 65 20 65 74 45 58 50 3a 0a 20 20 20 20 20 20  se etEXP:.      
a2600 63 61 73 65 20 65 74 47 45 4e 45 52 49 43 3a 0a  case etGENERIC:.
a2610 20 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75          realvalu
a2620 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 64 6f  e = va_arg(ap,do
a2630 75 62 6c 65 29 3b 0a 23 69 66 6e 64 65 66 20 53  uble);.#ifndef S
a2640 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
a2650 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 20 20  ING_POINT.      
a2660 20 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3c    if( precision<
a2670 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 3d 20  0 ) precision = 
a2680 36 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65  6;         /* Se
a2690 74 20 64 65 66 61 75 6c 74 20 70 72 65 63 69 73  t default precis
a26a0 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  ion */.        i
a26b0 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 65 74 42  f( precision>etB
a26c0 55 46 53 49 5a 45 2f 32 2d 31 30 20 29 20 70 72  UFSIZE/2-10 ) pr
a26d0 65 63 69 73 69 6f 6e 20 3d 20 65 74 42 55 46 53  ecision = etBUFS
a26e0 49 5a 45 2f 32 2d 31 30 3b 0a 20 20 20 20 20 20  IZE/2-10;.      
a26f0 20 20 69 66 28 20 72 65 61 6c 76 61 6c 75 65 3c    if( realvalue<
a2700 30 2e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  0.0 ){.         
a2710 20 72 65 61 6c 76 61 6c 75 65 20 3d 20 2d 72 65   realvalue = -re
a2720 61 6c 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 20  alvalue;.       
a2730 20 20 20 70 72 65 66 69 78 20 3d 20 27 2d 27 3b     prefix = '-';
a2740 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
a2750 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66 6c            if( fl
a2760 61 67 5f 70 6c 75 73 73 69 67 6e 20 29 20 20 20  ag_plussign )   
a2770 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d 20         prefix = 
a2780 27 2b 27 3b 0a 20 20 20 20 20 20 20 20 20 20 65  '+';.          e
a2790 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 62 6c 61  lse if( flag_bla
a27a0 6e 6b 73 69 67 6e 20 29 20 20 20 20 70 72 65 66  nksign )    pref
a27b0 69 78 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 20  ix = ' ';.      
a27c0 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 20 20      else        
a27d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a27e0 20 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 20   prefix = 0;.   
a27f0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
a2800 66 28 20 78 74 79 70 65 3d 3d 65 74 47 45 4e 45  f( xtype==etGENE
a2810 52 49 43 20 26 26 20 70 72 65 63 69 73 69 6f 6e  RIC && precision
a2820 3e 30 20 29 20 70 72 65 63 69 73 69 6f 6e 2d 2d  >0 ) precision--
a2830 3b 0a 23 69 66 20 30 0a 20 20 20 20 20 20 20 20  ;.#if 0.        
a2840 2f 2a 20 52 6f 75 6e 64 69 6e 67 20 77 6f 72 6b  /* Rounding work
a2850 73 20 6c 69 6b 65 20 42 53 44 20 77 68 65 6e 20  s like BSD when 
a2860 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 30 2e 34  the constant 0.4
a2870 39 39 39 20 69 73 20 75 73 65 64 2e 20 20 57 69  999 is used.  Wi
a2880 65 72 64 21 20 2a 2f 0a 20 20 20 20 20 20 20 20  erd! */.        
a2890 66 6f 72 28 69 64 78 3d 70 72 65 63 69 73 69 6f  for(idx=precisio
a28a0 6e 2c 20 72 6f 75 6e 64 65 72 3d 30 2e 34 39 39  n, rounder=0.499
a28b0 39 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 2c  9; idx>0; idx--,
a28c0 20 72 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 3b 0a   rounder*=0.1);.
a28d0 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 2f 2a  #else.        /*
a28e0 20 49 74 20 6d 61 6b 65 73 20 6d 6f 72 65 20 73   It makes more s
a28f0 65 6e 73 65 20 74 6f 20 75 73 65 20 30 2e 35 20  ense to use 0.5 
a2900 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  */.        for(i
a2910 64 78 3d 70 72 65 63 69 73 69 6f 6e 2c 20 72 6f  dx=precision, ro
a2920 75 6e 64 65 72 3d 30 2e 35 3b 20 69 64 78 3e 30  under=0.5; idx>0
a2930 3b 20 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65 72  ; idx--, rounder
a2940 2a 3d 30 2e 31 29 7b 7d 0a 23 65 6e 64 69 66 0a  *=0.1){}.#endif.
a2950 20 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70          if( xtyp
a2960 65 3d 3d 65 74 46 4c 4f 41 54 20 29 20 72 65 61  e==etFLOAT ) rea
a2970 6c 76 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 65  lvalue += rounde
a2980 72 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f  r;.        /* No
a2990 72 6d 61 6c 69 7a 65 20 72 65 61 6c 76 61 6c 75  rmalize realvalu
a29a0 65 20 74 6f 20 77 69 74 68 69 6e 20 31 30 2e 30  e to within 10.0
a29b0 20 3e 20 72 65 61 6c 76 61 6c 75 65 20 3e 3d 20   > realvalue >= 
a29c0 31 2e 30 20 2a 2f 0a 20 20 20 20 20 20 20 20 65  1.0 */.        e
a29d0 78 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  xp = 0;.        
a29e0 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e  if( sqlite3IsNaN
a29f0 28 28 64 6f 75 62 6c 65 29 72 65 61 6c 76 61 6c  ((double)realval
a2a00 75 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ue) ){.         
a2a10 20 62 75 66 70 74 20 3d 20 22 4e 61 4e 22 3b 0a   bufpt = "NaN";.
a2a20 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68            length
a2a30 20 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20   = 3;.          
a2a40 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
a2a50 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 65 61  .        if( rea
a2a60 6c 76 61 6c 75 65 3e 30 2e 30 20 29 7b 0a 20 20  lvalue>0.0 ){.  
a2a70 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72          while( r
a2a80 65 61 6c 76 61 6c 75 65 3e 3d 31 65 33 32 20 26  ealvalue>=1e32 &
a2a90 26 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 72 65  & exp<=350 ){ re
a2aa0 61 6c 76 61 6c 75 65 20 2a 3d 20 31 65 2d 33 32  alvalue *= 1e-32
a2ab0 3b 20 65 78 70 2b 3d 33 32 3b 20 7d 0a 20 20 20  ; exp+=32; }.   
a2ac0 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 65         while( re
a2ad0 61 6c 76 61 6c 75 65 3e 3d 31 65 38 20 26 26 20  alvalue>=1e8 && 
a2ae0 65 78 70 3c 3d 33 35 30 20 29 7b 20 72 65 61 6c  exp<=350 ){ real
a2af0 76 61 6c 75 65 20 2a 3d 20 31 65 2d 38 3b 20 65  value *= 1e-8; e
a2b00 78 70 2b 3d 38 3b 20 7d 0a 20 20 20 20 20 20 20  xp+=8; }.       
a2b10 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61     while( realva
a2b20 6c 75 65 3e 3d 31 30 2e 30 20 26 26 20 65 78 70  lue>=10.0 && exp
a2b30 3c 3d 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c  <=350 ){ realval
a2b40 75 65 20 2a 3d 20 30 2e 31 3b 20 65 78 70 2b 2b  ue *= 0.1; exp++
a2b50 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68  ; }.          wh
a2b60 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3c 31  ile( realvalue<1
a2b70 65 2d 38 20 29 7b 20 72 65 61 6c 76 61 6c 75 65  e-8 ){ realvalue
a2b80 20 2a 3d 20 31 65 38 3b 20 65 78 70 2d 3d 38 3b   *= 1e8; exp-=8;
a2b90 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69   }.          whi
a2ba0 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3c 31 2e  le( realvalue<1.
a2bb0 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a  0 ){ realvalue *
a2bc0 3d 20 31 30 2e 30 3b 20 65 78 70 2d 2d 3b 20 7d  = 10.0; exp--; }
a2bd0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65  .          if( e
a2be0 78 70 3e 33 35 30 20 29 7b 0a 20 20 20 20 20 20  xp>350 ){.      
a2bf0 20 20 20 20 20 20 69 66 28 20 70 72 65 66 69 78        if( prefix
a2c00 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 20  =='-' ){.       
a2c10 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22         bufpt = "
a2c20 2d 49 6e 66 22 3b 0a 20 20 20 20 20 20 20 20 20  -Inf";.         
a2c30 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 72 65     }else if( pre
a2c40 66 69 78 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20  fix=='+' ){.    
a2c50 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20            bufpt 
a2c60 3d 20 22 2b 49 6e 66 22 3b 0a 20 20 20 20 20 20  = "+Inf";.      
a2c70 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
a2c80 20 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74             bufpt
a2c90 20 3d 20 22 49 6e 66 22 3b 0a 20 20 20 20 20 20   = "Inf";.      
a2ca0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a2cb0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 73 71 6c      length = sql
a2cc0 69 74 65 33 53 74 72 6c 65 6e 33 30 28 62 75 66  ite3Strlen30(buf
a2cd0 70 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  pt);.           
a2ce0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
a2cf0 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
a2d00 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 75        bufpt = bu
a2d10 66 3b 0a 20 20 20 20 20 20 20 20 2f 2a 0a 20 20  f;.        /*.  
a2d20 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20        ** If the 
a2d30 66 69 65 6c 64 20 74 79 70 65 20 69 73 20 65 74  field type is et
a2d40 47 45 4e 45 52 49 43 2c 20 74 68 65 6e 20 63 6f  GENERIC, then co
a2d50 6e 76 65 72 74 20 74 6f 20 65 69 74 68 65 72 20  nvert to either 
a2d60 65 74 45 58 50 0a 20 20 20 20 20 20 20 20 2a 2a  etEXP.        **
a2d70 20 6f 72 20 65 74 46 4c 4f 41 54 2c 20 61 73 20   or etFLOAT, as 
a2d80 61 70 70 72 6f 70 72 69 61 74 65 2e 0a 20 20 20  appropriate..   
a2d90 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
a2da0 66 6c 61 67 5f 65 78 70 20 3d 20 78 74 79 70 65  flag_exp = xtype
a2db0 3d 3d 65 74 45 58 50 3b 0a 20 20 20 20 20 20 20  ==etEXP;.       
a2dc0 20 69 66 28 20 78 74 79 70 65 21 3d 65 74 46 4c   if( xtype!=etFL
a2dd0 4f 41 54 20 29 7b 0a 20 20 20 20 20 20 20 20 20  OAT ){.         
a2de0 20 72 65 61 6c 76 61 6c 75 65 20 2b 3d 20 72 6f   realvalue += ro
a2df0 75 6e 64 65 72 3b 0a 20 20 20 20 20 20 20 20 20  under;.         
a2e00 20 69 66 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d   if( realvalue>=
a2e10 31 30 2e 30 20 29 7b 20 72 65 61 6c 76 61 6c 75  10.0 ){ realvalu
a2e20 65 20 2a 3d 20 30 2e 31 3b 20 65 78 70 2b 2b 3b  e *= 0.1; exp++;
a2e30 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
a2e40 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d       if( xtype==
a2e50 65 74 47 45 4e 45 52 49 43 20 29 7b 0a 20 20 20  etGENERIC ){.   
a2e60 20 20 20 20 20 20 20 66 6c 61 67 5f 72 74 7a 20         flag_rtz 
a2e70 3d 20 21 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74  = !flag_alternat
a2e80 65 66 6f 72 6d 3b 0a 20 20 20 20 20 20 20 20 20  eform;.         
a2e90 20 69 66 28 20 65 78 70 3c 2d 34 20 7c 7c 20 65   if( exp<-4 || e
a2ea0 78 70 3e 70 72 65 63 69 73 69 6f 6e 20 29 7b 0a  xp>precision ){.
a2eb0 20 20 20 20 20 20 20 20 20 20 20 20 78 74 79 70              xtyp
a2ec0 65 20 3d 20 65 74 45 58 50 3b 0a 20 20 20 20 20  e = etEXP;.     
a2ed0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
a2ee0 20 20 20 20 20 20 20 20 70 72 65 63 69 73 69 6f          precisio
a2ef0 6e 20 3d 20 70 72 65 63 69 73 69 6f 6e 20 2d 20  n = precision - 
a2f00 65 78 70 3b 0a 20 20 20 20 20 20 20 20 20 20 20  exp;.           
a2f10 20 78 74 79 70 65 20 3d 20 65 74 46 4c 4f 41 54   xtype = etFLOAT
a2f20 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
a2f30 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
a2f40 20 20 20 20 20 20 20 66 6c 61 67 5f 72 74 7a 20         flag_rtz 
a2f50 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 0;.        }. 
a2f60 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65         if( xtype
a2f70 3d 3d 65 74 45 58 50 20 29 7b 0a 20 20 20 20 20  ==etEXP ){.     
a2f80 20 20 20 20 20 65 32 20 3d 20 30 3b 0a 20 20 20       e2 = 0;.   
a2f90 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
a2fa0 20 20 20 20 20 20 65 32 20 3d 20 65 78 70 3b 0a        e2 = exp;.
a2fb0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a2fc0 20 20 6e 73 64 20 3d 20 30 3b 0a 20 20 20 20 20    nsd = 0;.     
a2fd0 20 20 20 66 6c 61 67 5f 64 70 20 3d 20 28 70 72     flag_dp = (pr
a2fe0 65 63 69 73 69 6f 6e 3e 30 20 3f 31 3a 30 29 20  ecision>0 ?1:0) 
a2ff0 7c 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65  | flag_alternate
a3000 66 6f 72 6d 20 7c 20 66 6c 61 67 5f 61 6c 74 66  form | flag_altf
a3010 6f 72 6d 32 3b 0a 20 20 20 20 20 20 20 20 2f 2a  orm2;.        /*
a3020 20 54 68 65 20 73 69 67 6e 20 69 6e 20 66 72 6f   The sign in fro
a3030 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  nt of the number
a3040 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
a3050 70 72 65 66 69 78 20 29 7b 0a 20 20 20 20 20 20  prefix ){.      
a3060 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d      *(bufpt++) =
a3070 20 70 72 65 66 69 78 3b 0a 20 20 20 20 20 20 20   prefix;.       
a3080 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 44 69   }.        /* Di
a3090 67 69 74 73 20 70 72 69 6f 72 20 74 6f 20 74 68  gits prior to th
a30a0 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20  e decimal point 
a30b0 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 65  */.        if( e
a30c0 32 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  2<0 ){.         
a30d0 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 30   *(bufpt++) = '0
a30e0 27 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  ';.        }else
a30f0 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28  {.          for(
a3100 3b 20 65 32 3e 3d 30 3b 20 65 32 2d 2d 29 7b 0a  ; e2>=0; e2--){.
a3110 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 62 75              *(bu
a3120 66 70 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74 64  fpt++) = et_getd
a3130 69 67 69 74 28 26 72 65 61 6c 76 61 6c 75 65 2c  igit(&realvalue,
a3140 26 6e 73 64 29 3b 0a 20 20 20 20 20 20 20 20 20  &nsd);.         
a3150 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
a3160 20 20 20 20 20 2f 2a 20 54 68 65 20 64 65 63 69       /* The deci
a3170 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20  mal point */.   
a3180 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 64 70       if( flag_dp
a3190 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28   ){.          *(
a31a0 62 75 66 70 74 2b 2b 29 20 3d 20 27 2e 27 3b 0a  bufpt++) = '.';.
a31b0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a31c0 20 20 2f 2a 20 22 30 22 20 64 69 67 69 74 73 20    /* "0" digits 
a31d0 61 66 74 65 72 20 74 68 65 20 64 65 63 69 6d 61  after the decima
a31e0 6c 20 70 6f 69 6e 74 20 62 75 74 20 62 65 66 6f  l point but befo
a31f0 72 65 20 74 68 65 20 66 69 72 73 74 0a 20 20 20  re the first.   
a3200 20 20 20 20 20 2a 2a 20 73 69 67 6e 69 66 69 63       ** signific
a3210 61 6e 74 20 64 69 67 69 74 20 6f 66 20 74 68 65  ant digit of the
a3220 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20   number */.     
a3230 20 20 20 66 6f 72 28 65 32 2b 2b 3b 20 65 32 3c     for(e2++; e2<
a3240 30 3b 20 70 72 65 63 69 73 69 6f 6e 2d 2d 2c 20  0; precision--, 
a3250 65 32 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  e2++){.         
a3260 20 61 73 73 65 72 74 28 20 70 72 65 63 69 73 69   assert( precisi
a3270 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20  on>0 );.        
a3280 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27    *(bufpt++) = '
a3290 30 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  0';.        }.  
a32a0 20 20 20 20 20 20 2f 2a 20 53 69 67 6e 69 66 69        /* Signifi
a32b0 63 61 6e 74 20 64 69 67 69 74 73 20 61 66 74 65  cant digits afte
a32c0 72 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f  r the decimal po
a32d0 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 77  int */.        w
a32e0 68 69 6c 65 28 20 28 70 72 65 63 69 73 69 6f 6e  hile( (precision
a32f0 2d 2d 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  --)>0 ){.       
a3300 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20     *(bufpt++) = 
a3310 65 74 5f 67 65 74 64 69 67 69 74 28 26 72 65 61  et_getdigit(&rea
a3320 6c 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20 20  lvalue,&nsd);.  
a3330 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a3340 2f 2a 20 52 65 6d 6f 76 65 20 74 72 61 69 6c 69  /* Remove traili
a3350 6e 67 20 7a 65 72 6f 73 20 61 6e 64 20 74 68 65  ng zeros and the
a3360 20 22 2e 22 20 69 66 20 6e 6f 20 64 69 67 69 74   "." if no digit
a3370 73 20 66 6f 6c 6c 6f 77 20 74 68 65 20 22 2e 22  s follow the "."
a3380 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
a3390 66 6c 61 67 5f 72 74 7a 20 26 26 20 66 6c 61 67  flag_rtz && flag
a33a0 5f 64 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _dp ){.         
a33b0 20 77 68 69 6c 65 28 20 62 75 66 70 74 5b 2d 31   while( bufpt[-1
a33c0 5d 3d 3d 27 30 27 20 29 20 2a 28 2d 2d 62 75 66  ]=='0' ) *(--buf
a33d0 70 74 29 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  pt) = 0;.       
a33e0 20 20 20 61 73 73 65 72 74 28 20 62 75 66 70 74     assert( bufpt
a33f0 3e 62 75 66 20 29 3b 0a 20 20 20 20 20 20 20 20  >buf );.        
a3400 20 20 69 66 28 20 62 75 66 70 74 5b 2d 31 5d 3d    if( bufpt[-1]=
a3410 3d 27 2e 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='.' ){.        
a3420 20 20 20 20 69 66 28 20 66 6c 61 67 5f 61 6c 74      if( flag_alt
a3430 66 6f 72 6d 32 20 29 7b 0a 20 20 20 20 20 20 20  form2 ){.       
a3440 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b         *(bufpt++
a3450 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 20  ) = '0';.       
a3460 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
a3470 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75            *(--bu
a3480 66 70 74 29 20 3d 20 30 3b 0a 20 20 20 20 20 20  fpt) = 0;.      
a3490 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a34a0 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
a34b0 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65        /* Add the
a34c0 20 22 65 4e 4e 4e 22 20 73 75 66 66 69 78 20 2a   "eNNN" suffix *
a34d0 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c  /.        if( fl
a34e0 61 67 5f 65 78 70 20 7c 7c 20 78 74 79 70 65 3d  ag_exp || xtype=
a34f0 3d 65 74 45 58 50 20 29 7b 0a 20 20 20 20 20 20  =etEXP ){.      
a3500 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d      *(bufpt++) =
a3510 20 61 44 69 67 69 74 73 5b 69 6e 66 6f 70 2d 3e   aDigits[infop->
a3520 63 68 61 72 73 65 74 5d 3b 0a 20 20 20 20 20 20  charset];.      
a3530 20 20 20 20 69 66 28 20 65 78 70 3c 30 20 29 7b      if( exp<0 ){
a3540 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 62  .            *(b
a3550 75 66 70 74 2b 2b 29 20 3d 20 27 2d 27 3b 20 65  ufpt++) = '-'; e
a3560 78 70 20 3d 20 2d 65 78 70 3b 0a 20 20 20 20 20  xp = -exp;.     
a3570 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
a3580 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b          *(bufpt+
a3590 2b 29 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 20  +) = '+';.      
a35a0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
a35b0 69 66 28 20 65 78 70 3e 3d 31 30 30 20 29 7b 0a  if( exp>=100 ){.
a35c0 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 62 75              *(bu
a35d0 66 70 74 2b 2b 29 20 3d 20 28 63 68 61 72 29 28  fpt++) = (char)(
a35e0 28 65 78 70 2f 31 30 30 29 2b 27 30 27 29 3b 20  (exp/100)+'0'); 
a35f0 20 20 20 20 20 20 20 2f 2a 20 31 30 30 27 73 20         /* 100's 
a3600 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 20 20 20  digit */.       
a3610 20 20 20 20 20 65 78 70 20 25 3d 20 31 30 30 3b       exp %= 100;
a3620 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
a3630 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b         *(bufpt++
a3640 29 20 3d 20 28 63 68 61 72 29 28 65 78 70 2f 31  ) = (char)(exp/1
a3650 30 2b 27 30 27 29 3b 20 20 20 20 20 20 20 20 20  0+'0');         
a3660 20 20 20 20 2f 2a 20 31 30 27 73 20 64 69 67 69      /* 10's digi
a3670 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 2a  t */.          *
a3680 28 62 75 66 70 74 2b 2b 29 20 3d 20 28 63 68 61  (bufpt++) = (cha
a3690 72 29 28 65 78 70 25 31 30 2b 27 30 27 29 3b 20  r)(exp%10+'0'); 
a36a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31              /* 1
a36b0 27 73 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20  's digit */.    
a36c0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2a 62      }.        *b
a36d0 75 66 70 74 20 3d 20 30 3b 0a 0a 20 20 20 20 20  ufpt = 0;..     
a36e0 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 76 65 72     /* The conver
a36f0 74 65 64 20 6e 75 6d 62 65 72 20 69 73 20 69 6e  ted number is in
a3700 20 62 75 66 5b 5d 20 61 6e 64 20 7a 65 72 6f 20   buf[] and zero 
a3710 74 65 72 6d 69 6e 61 74 65 64 2e 20 4f 75 74 70  terminated. Outp
a3720 75 74 20 69 74 2e 0a 20 20 20 20 20 20 20 20 2a  ut it..        *
a3730 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20  * Note that the 
a3740 6e 75 6d 62 65 72 20 69 73 20 69 6e 20 74 68 65  number is in the
a3750 20 75 73 75 61 6c 20 6f 72 64 65 72 2c 20 6e 6f   usual order, no
a3760 74 20 72 65 76 65 72 73 65 64 20 61 73 20 77 69  t reversed as wi
a3770 74 68 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e  th.        ** in
a3780 74 65 67 65 72 20 63 6f 6e 76 65 72 73 69 6f 6e  teger conversion
a3790 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 6c 65  s. */.        le
a37a0 6e 67 74 68 20 3d 20 28 69 6e 74 29 28 62 75 66  ngth = (int)(buf
a37b0 70 74 2d 62 75 66 29 3b 0a 20 20 20 20 20 20 20  pt-buf);.       
a37c0 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 0a 20   bufpt = buf;.. 
a37d0 20 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69 61         /* Specia
a37e0 6c 20 63 61 73 65 3a 20 20 41 64 64 20 6c 65 61  l case:  Add lea
a37f0 64 69 6e 67 20 7a 65 72 6f 73 20 69 66 20 74 68  ding zeros if th
a3800 65 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 66  e flag_zeropad f
a3810 6c 61 67 20 69 73 0a 20 20 20 20 20 20 20 20 2a  lag is.        *
a3820 2a 20 73 65 74 20 61 6e 64 20 77 65 20 61 72 65  * set and we are
a3830 20 6e 6f 74 20 6c 65 66 74 20 6a 75 73 74 69 66   not left justif
a3840 69 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  ied */.        i
a3850 66 28 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20  f( flag_zeropad 
a3860 26 26 20 21 66 6c 61 67 5f 6c 65 66 74 6a 75 73  && !flag_leftjus
a3870 74 69 66 79 20 26 26 20 6c 65 6e 67 74 68 20 3c  tify && length <
a3880 20 77 69 64 74 68 29 7b 0a 20 20 20 20 20 20 20   width){.       
a3890 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
a38a0 20 20 20 20 69 6e 74 20 6e 50 61 64 20 3d 20 77      int nPad = w
a38b0 69 64 74 68 20 2d 20 6c 65 6e 67 74 68 3b 0a 20  idth - length;. 
a38c0 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 77           for(i=w
a38d0 69 64 74 68 3b 20 69 3e 3d 6e 50 61 64 3b 20 69  idth; i>=nPad; i
a38e0 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  --){.           
a38f0 20 62 75 66 70 74 5b 69 5d 20 3d 20 62 75 66 70   bufpt[i] = bufp
a3900 74 5b 69 2d 6e 50 61 64 5d 3b 0a 20 20 20 20 20  t[i-nPad];.     
a3910 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
a3920 20 69 20 3d 20 70 72 65 66 69 78 21 3d 30 3b 0a   i = prefix!=0;.
a3930 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28            while(
a3940 20 6e 50 61 64 2d 2d 20 29 20 62 75 66 70 74 5b   nPad-- ) bufpt[
a3950 69 2b 2b 5d 20 3d 20 27 30 27 3b 0a 20 20 20 20  i++] = '0';.    
a3960 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 77        length = w
a3970 69 64 74 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a  idth;.        }.
a3980 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 62  #endif.        b
a3990 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
a39a0 20 65 74 53 49 5a 45 3a 0a 20 20 20 20 20 20 20   etSIZE:.       
a39b0 20 2a 28 76 61 5f 61 72 67 28 61 70 2c 69 6e 74   *(va_arg(ap,int
a39c0 2a 29 29 20 3d 20 70 41 63 63 75 6d 2d 3e 6e 43  *)) = pAccum->nC
a39d0 68 61 72 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e  har;.        len
a39e0 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30 3b  gth = width = 0;
a39f0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
a3a00 20 20 20 20 20 20 63 61 73 65 20 65 74 50 45 52        case etPER
a3a10 43 45 4e 54 3a 0a 20 20 20 20 20 20 20 20 62 75  CENT:.        bu
a3a20 66 5b 30 5d 20 3d 20 27 25 27 3b 0a 20 20 20 20  f[0] = '%';.    
a3a30 20 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b      bufpt = buf;
a3a40 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20  .        length 
a3a50 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62 72 65  = 1;.        bre
a3a60 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65  ak;.      case e
a3a70 74 43 48 41 52 58 3a 0a 20 20 20 20 20 20 20 20  tCHARX:.        
a3a80 63 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e  c = va_arg(ap,in
a3a90 74 29 3b 0a 20 20 20 20 20 20 20 20 62 75 66 5b  t);.        buf[
a3aa0 30 5d 20 3d 20 28 63 68 61 72 29 63 3b 0a 20 20  0] = (char)c;.  
a3ab0 20 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73        if( precis
a3ac0 69 6f 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  ion>=0 ){.      
a3ad0 20 20 20 20 66 6f 72 28 69 64 78 3d 31 3b 20 69      for(idx=1; i
a3ae0 64 78 3c 70 72 65 63 69 73 69 6f 6e 3b 20 69 64  dx<precision; id
a3af0 78 2b 2b 29 20 62 75 66 5b 69 64 78 5d 20 3d 20  x++) buf[idx] = 
a3b00 28 63 68 61 72 29 63 3b 0a 20 20 20 20 20 20 20  (char)c;.       
a3b10 20 20 20 6c 65 6e 67 74 68 20 3d 20 70 72 65 63     length = prec
a3b20 69 73 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20 7d  ision;.        }
a3b30 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
a3b40 6c 65 6e 67 74 68 20 3d 31 3b 0a 20 20 20 20 20  length =1;.     
a3b50 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 75 66     }.        buf
a3b60 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20  pt = buf;.      
a3b70 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
a3b80 61 73 65 20 65 74 53 54 52 49 4e 47 3a 0a 20 20  ase etSTRING:.  
a3b90 20 20 20 20 63 61 73 65 20 65 74 44 59 4e 53 54      case etDYNST
a3ba0 52 49 4e 47 3a 0a 20 20 20 20 20 20 20 20 62 75  RING:.        bu
a3bb0 66 70 74 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  fpt = va_arg(ap,
a3bc0 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20  char*);.        
a3bd0 69 66 28 20 62 75 66 70 74 3d 3d 30 20 29 7b 0a  if( bufpt==0 ){.
a3be0 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20            bufpt 
a3bf0 3d 20 22 22 3b 0a 20 20 20 20 20 20 20 20 7d 65  = "";.        }e
a3c00 6c 73 65 20 69 66 28 20 78 74 79 70 65 3d 3d 65  lse if( xtype==e
a3c10 74 44 59 4e 53 54 52 49 4e 47 20 29 7b 0a 20 20  tDYNSTRING ){.  
a3c20 20 20 20 20 20 20 20 20 7a 45 78 74 72 61 20 3d          zExtra =
a3c30 20 62 75 66 70 74 3b 0a 20 20 20 20 20 20 20 20   bufpt;.        
a3c40 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72  }.        if( pr
a3c50 65 63 69 73 69 6f 6e 3e 3d 30 20 29 7b 0a 20 20  ecision>=0 ){.  
a3c60 20 20 20 20 20 20 20 20 66 6f 72 28 6c 65 6e 67          for(leng
a3c70 74 68 3d 30 3b 20 6c 65 6e 67 74 68 3c 70 72 65  th=0; length<pre
a3c80 63 69 73 69 6f 6e 20 26 26 20 62 75 66 70 74 5b  cision && bufpt[
a3c90 6c 65 6e 67 74 68 5d 3b 20 6c 65 6e 67 74 68 2b  length]; length+
a3ca0 2b 29 7b 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c  +){}.        }el
a3cb0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 65  se{.          le
a3cc0 6e 67 74 68 20 3d 20 73 71 6c 69 74 65 33 53 74  ngth = sqlite3St
a3cd0 72 6c 65 6e 33 30 28 62 75 66 70 74 29 3b 0a 20  rlen30(bufpt);. 
a3ce0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a3cf0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
a3d00 73 65 20 65 74 53 51 4c 45 53 43 41 50 45 3a 0a  se etSQLESCAPE:.
a3d10 20 20 20 20 20 20 63 61 73 65 20 65 74 53 51 4c        case etSQL
a3d20 45 53 43 41 50 45 32 3a 0a 20 20 20 20 20 20 63  ESCAPE2:.      c
a3d30 61 73 65 20 65 74 53 51 4c 45 53 43 41 50 45 33  ase etSQLESCAPE3
a3d40 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  : {.        int 
a3d50 69 2c 20 6a 2c 20 6e 2c 20 69 73 6e 75 6c 6c 3b  i, j, n, isnull;
a3d60 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 65 65  .        int nee
a3d70 64 51 75 6f 74 65 3b 0a 20 20 20 20 20 20 20 20  dQuote;.        
a3d80 63 68 61 72 20 63 68 3b 0a 20 20 20 20 20 20 20  char ch;.       
a3d90 20 63 68 61 72 20 71 20 3d 20 28 28 78 74 79 70   char q = ((xtyp
a3da0 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 33 29  e==etSQLESCAPE3)
a3db0 3f 27 22 27 3a 27 5c 27 27 29 3b 20 20 20 2f 2a  ?'"':'\'');   /*
a3dc0 20 51 75 6f 74 65 20 63 68 61 72 61 63 74 65 72   Quote character
a3dd0 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 68 61 72   */.        char
a3de0 20 2a 65 73 63 61 72 67 20 3d 20 76 61 5f 61 72   *escarg = va_ar
a3df0 67 28 61 70 2c 63 68 61 72 2a 29 3b 0a 20 20 20  g(ap,char*);.   
a3e00 20 20 20 20 20 69 73 6e 75 6c 6c 20 3d 20 65 73       isnull = es
a3e10 63 61 72 67 3d 3d 30 3b 0a 20 20 20 20 20 20 20  carg==0;.       
a3e20 20 69 66 28 20 69 73 6e 75 6c 6c 20 29 20 65 73   if( isnull ) es
a3e30 63 61 72 67 20 3d 20 28 78 74 79 70 65 3d 3d 65  carg = (xtype==e
a3e40 74 53 51 4c 45 53 43 41 50 45 32 20 3f 20 22 4e  tSQLESCAPE2 ? "N
a3e50 55 4c 4c 22 20 3a 20 22 28 4e 55 4c 4c 29 22 29  ULL" : "(NULL)")
a3e60 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d  ;.        for(i=
a3e70 6e 3d 30 3b 20 28 63 68 3d 65 73 63 61 72 67 5b  n=0; (ch=escarg[
a3e80 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20  i])!=0; i++){.  
a3e90 20 20 20 20 20 20 20 20 69 66 28 20 63 68 3d 3d          if( ch==
a3ea0 71 20 29 20 20 6e 2b 2b 3b 0a 20 20 20 20 20 20  q )  n++;.      
a3eb0 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 65 65 64    }.        need
a3ec0 51 75 6f 74 65 20 3d 20 21 69 73 6e 75 6c 6c 20  Quote = !isnull 
a3ed0 26 26 20 78 74 79 70 65 3d 3d 65 74 53 51 4c 45  && xtype==etSQLE
a3ee0 53 43 41 50 45 32 3b 0a 20 20 20 20 20 20 20 20  SCAPE2;.        
a3ef0 6e 20 2b 3d 20 69 20 2b 20 31 20 2b 20 6e 65 65  n += i + 1 + nee
a3f00 64 51 75 6f 74 65 2a 32 3b 0a 20 20 20 20 20 20  dQuote*2;.      
a3f10 20 20 69 66 28 20 6e 3e 65 74 42 55 46 53 49 5a    if( n>etBUFSIZ
a3f20 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62  E ){.          b
a3f30 75 66 70 74 20 3d 20 7a 45 78 74 72 61 20 3d 20  ufpt = zExtra = 
a3f40 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e  sqlite3Malloc( n
a3f50 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66   );.          if
a3f60 28 20 62 75 66 70 74 3d 3d 30 20 29 7b 0a 20 20  ( bufpt==0 ){.  
a3f70 20 20 20 20 20 20 20 20 20 20 70 41 63 63 75 6d            pAccum
a3f80 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
a3f90 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   1;.            
a3fa0 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20  return;.        
a3fb0 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73    }.        }els
a3fc0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 62 75 66  e{.          buf
a3fd0 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20  pt = buf;.      
a3fe0 20 20 7d 0a 20 20 20 20 20 20 20 20 6a 20 3d 20    }.        j = 
a3ff0 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e  0;.        if( n
a4000 65 65 64 51 75 6f 74 65 20 29 20 62 75 66 70 74  eedQuote ) bufpt
a4010 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a 20 20 20 20 20  [j++] = q;.     
a4020 20 20 20 66 6f 72 28 69 3d 30 3b 20 28 63 68 3d     for(i=0; (ch=
a4030 65 73 63 61 72 67 5b 69 5d 29 21 3d 30 3b 20 69  escarg[i])!=0; i
a4040 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62  ++){.          b
a4050 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63 68 3b 0a  ufpt[j++] = ch;.
a4060 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 68            if( ch
a4070 3d 3d 71 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d  ==q ) bufpt[j++]
a4080 20 3d 20 63 68 3b 0a 20 20 20 20 20 20 20 20 7d   = ch;.        }
a4090 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 65 65  .        if( nee
a40a0 64 51 75 6f 74 65 20 29 20 62 75 66 70 74 5b 6a  dQuote ) bufpt[j
a40b0 2b 2b 5d 20 3d 20 71 3b 0a 20 20 20 20 20 20 20  ++] = q;.       
a40c0 20 62 75 66 70 74 5b 6a 5d 20 3d 20 30 3b 0a 20   bufpt[j] = 0;. 
a40d0 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20         length = 
a40e0 6a 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  j;.        /* Th
a40f0 65 20 70 72 65 63 69 73 69 6f 6e 20 69 73 20 69  e precision is i
a4100 67 6e 6f 72 65 64 20 6f 6e 20 25 71 20 61 6e 64  gnored on %q and
a4110 20 25 51 20 2a 2f 0a 20 20 20 20 20 20 20 20 2f   %Q */.        /
a4120 2a 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e  * if( precision>
a4130 3d 30 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3c  =0 && precision<
a4140 6c 65 6e 67 74 68 20 29 20 6c 65 6e 67 74 68 20  length ) length 
a4150 3d 20 70 72 65 63 69 73 69 6f 6e 3b 20 2a 2f 0a  = precision; */.
a4160 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
a4170 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
a4180 65 20 65 74 54 4f 4b 45 4e 3a 20 7b 0a 20 20 20  e etTOKEN: {.   
a4190 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b       Token *pTok
a41a0 65 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  en = va_arg(ap, 
a41b0 54 6f 6b 65 6e 2a 29 3b 0a 20 20 20 20 20 20 20  Token*);.       
a41c0 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a 20   if( pToken ){. 
a41d0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
a41e0 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70  StrAccumAppend(p
a41f0 41 63 63 75 6d 2c 20 28 63 6f 6e 73 74 20 63 68  Accum, (const ch
a4200 61 72 2a 29 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 70  ar*)pToken->z, p
a4210 54 6f 6b 65 6e 2d 3e 6e 29 3b 0a 20 20 20 20 20  Token->n);.     
a4220 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6c 65 6e     }.        len
a4230 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30 3b  gth = width = 0;
a4240 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
a4250 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
a4260 73 65 20 65 74 53 52 43 4c 49 53 54 3a 20 7b 0a  se etSRCLIST: {.
a4270 20 20 20 20 20 20 20 20 53 72 63 4c 69 73 74 20          SrcList 
a4280 2a 70 53 72 63 20 3d 20 76 61 5f 61 72 67 28 61  *pSrc = va_arg(a
a4290 70 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 20 20  p, SrcList*);.  
a42a0 20 20 20 20 20 20 69 6e 74 20 6b 20 3d 20 76 61        int k = va
a42b0 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20  _arg(ap, int);. 
a42c0 20 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72         struct Sr
a42d0 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  cList_item *pIte
a42e0 6d 20 3d 20 26 70 53 72 63 2d 3e 61 5b 6b 5d 3b  m = &pSrc->a[k];
a42f0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
a4300 20 6b 3e 3d 30 20 26 26 20 6b 3c 70 53 72 63 2d   k>=0 && k<pSrc-
a4310 3e 6e 53 72 63 20 29 3b 0a 20 20 20 20 20 20 20  >nSrc );.       
a4320 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 44 61 74   if( pItem->zDat
a4330 61 62 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20  abase ){.       
a4340 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
a4350 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c  umAppend(pAccum,
a4360 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73   pItem->zDatabas
a4370 65 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20  e, -1);.        
a4380 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75    sqlite3StrAccu
a4390 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20  mAppend(pAccum, 
a43a0 22 2e 22 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  ".", 1);.       
a43b0 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   }.        sqlit
a43c0 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64  e3StrAccumAppend
a43d0 28 70 41 63 63 75 6d 2c 20 70 49 74 65 6d 2d 3e  (pAccum, pItem->
a43e0 7a 4e 61 6d 65 2c 20 2d 31 29 3b 0a 20 20 20 20  zName, -1);.    
a43f0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64      length = wid
a4400 74 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  th = 0;.        
a4410 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
a4420 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a       default: {.
a4430 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
a4440 78 74 79 70 65 3d 3d 65 74 49 4e 56 41 4c 49 44  xtype==etINVALID
a4450 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75   );.        retu
a4460 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
a4470 7d 2f 2a 20 45 6e 64 20 73 77 69 74 63 68 20 6f  }/* End switch o
a4480 76 65 72 20 74 68 65 20 66 6f 72 6d 61 74 20 74  ver the format t
a4490 79 70 65 20 2a 2f 0a 20 20 20 20 2f 2a 0a 20 20  ype */.    /*.  
a44a0 20 20 2a 2a 20 54 68 65 20 74 65 78 74 20 6f 66    ** The text of
a44b0 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20   the conversion 
a44c0 69 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  is pointed to by
a44d0 20 22 62 75 66 70 74 22 20 61 6e 64 20 69 73 0a   "bufpt" and is.
a44e0 20 20 20 20 2a 2a 20 22 6c 65 6e 67 74 68 22 20      ** "length" 
a44f0 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e  characters long.
a4500 20 20 54 68 65 20 66 69 65 6c 64 20 77 69 64 74    The field widt
a4510 68 20 69 73 20 22 77 69 64 74 68 22 2e 20 20 44  h is "width".  D
a4520 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 6f 75 74  o.    ** the out
a4530 70 75 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  put..    */.    
a4540 69 66 28 20 21 66 6c 61 67 5f 6c 65 66 74 6a 75  if( !flag_leftju
a4550 73 74 69 66 79 20 29 7b 0a 20 20 20 20 20 20 72  stify ){.      r
a4560 65 67 69 73 74 65 72 20 69 6e 74 20 6e 73 70 61  egister int nspa
a4570 63 65 3b 0a 20 20 20 20 20 20 6e 73 70 61 63 65  ce;.      nspace
a4580 20 3d 20 77 69 64 74 68 2d 6c 65 6e 67 74 68 3b   = width-length;
a4590 0a 20 20 20 20 20 20 69 66 28 20 6e 73 70 61 63  .      if( nspac
a45a0 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61  e>0 ){.        a
a45b0 70 70 65 6e 64 53 70 61 63 65 28 70 41 63 63 75  ppendSpace(pAccu
a45c0 6d 2c 20 6e 73 70 61 63 65 29 3b 0a 20 20 20 20  m, nspace);.    
a45d0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
a45e0 28 20 6c 65 6e 67 74 68 3e 30 20 29 7b 0a 20 20  ( length>0 ){.  
a45f0 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
a4600 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d  cumAppend(pAccum
a4610 2c 20 62 75 66 70 74 2c 20 6c 65 6e 67 74 68 29  , bufpt, length)
a4620 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
a4630 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79  flag_leftjustify
a4640 20 29 7b 0a 20 20 20 20 20 20 72 65 67 69 73 74   ){.      regist
a4650 65 72 20 69 6e 74 20 6e 73 70 61 63 65 3b 0a 20  er int nspace;. 
a4660 20 20 20 20 20 6e 73 70 61 63 65 20 3d 20 77 69       nspace = wi
a4670 64 74 68 2d 6c 65 6e 67 74 68 3b 0a 20 20 20 20  dth-length;.    
a4680 20 20 69 66 28 20 6e 73 70 61 63 65 3e 30 20 29    if( nspace>0 )
a4690 7b 0a 20 20 20 20 20 20 20 20 61 70 70 65 6e 64  {.        append
a46a0 53 70 61 63 65 28 70 41 63 63 75 6d 2c 20 6e 73  Space(pAccum, ns
a46b0 70 61 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  pace);.      }. 
a46c0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 7a 45 78     }.    if( zEx
a46d0 74 72 61 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  tra ){.      sql
a46e0 69 74 65 33 5f 66 72 65 65 28 7a 45 78 74 72 61  ite3_free(zExtra
a46f0 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 2f 2a 20 45  );.    }.  }/* E
a4700 6e 64 20 66 6f 72 20 6c 6f 6f 70 20 6f 76 65 72  nd for loop over
a4710 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69   the format stri
a4720 6e 67 20 2a 2f 0a 7d 20 2f 2a 20 45 6e 64 20 6f  ng */.} /* End o
a4730 66 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 2f  f function */../
a4740 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 4e 20 62 79  *.** Append N by
a4750 74 65 73 20 6f 66 20 74 65 78 74 20 66 72 6f 6d  tes of text from
a4760 20 7a 20 74 6f 20 74 68 65 20 53 74 72 41 63 63   z to the StrAcc
a4770 75 6d 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51  um object..*/.SQ
a4780 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
a4790 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75  d sqlite3StrAccu
a47a0 6d 41 70 70 65 6e 64 28 53 74 72 41 63 63 75 6d  mAppend(StrAccum
a47b0 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20   *p, const char 
a47c0 2a 7a 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 61 73  *z, int N){.  as
a47d0 73 65 72 74 28 20 7a 21 3d 30 20 7c 7c 20 4e 3d  sert( z!=0 || N=
a47e0 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 74  =0 );.  if( p->t
a47f0 6f 6f 42 69 67 20 7c 20 70 2d 3e 6d 61 6c 6c 6f  ooBig | p->mallo
a4800 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 74  cFailed ){.    t
a4810 65 73 74 63 61 73 65 28 70 2d 3e 74 6f 6f 42 69  estcase(p->tooBi
a4820 67 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65  g);.    testcase
a4830 28 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  (p->mallocFailed
a4840 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
a4850 20 7d 0a 20 20 69 66 28 20 4e 3c 30 20 29 7b 0a   }.  if( N<0 ){.
a4860 20 20 20 20 4e 20 3d 20 73 71 6c 69 74 65 33 53      N = sqlite3S
a4870 74 72 6c 65 6e 33 30 28 7a 29 3b 0a 20 20 7d 0a  trlen30(z);.  }.
a4880 20 20 69 66 28 20 4e 3d 3d 30 20 7c 7c 20 4e 45    if( N==0 || NE
a4890 56 45 52 28 7a 3d 3d 30 29 20 29 7b 0a 20 20 20  VER(z==0) ){.   
a48a0 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69   return;.  }.  i
a48b0 66 28 20 70 2d 3e 6e 43 68 61 72 2b 4e 20 3e 3d  f( p->nChar+N >=
a48c0 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20   p->nAlloc ){.  
a48d0 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20    char *zNew;.  
a48e0 20 20 69 66 28 20 21 70 2d 3e 75 73 65 4d 61 6c    if( !p->useMal
a48f0 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  loc ){.      p->
a4900 74 6f 6f 42 69 67 20 3d 20 31 3b 0a 20 20 20 20  tooBig = 1;.    
a4910 20 20 4e 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20    N = p->nAlloc 
a4920 2d 20 70 2d 3e 6e 43 68 61 72 20 2d 20 31 3b 0a  - p->nChar - 1;.
a4930 20 20 20 20 20 20 69 66 28 20 4e 3c 3d 30 20 29        if( N<=0 )
a4940 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
a4950 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
a4960 6c 73 65 7b 0a 20 20 20 20 20 20 69 36 34 20 73  lse{.      i64 s
a4970 7a 4e 65 77 20 3d 20 70 2d 3e 6e 43 68 61 72 3b  zNew = p->nChar;
a4980 0a 20 20 20 20 20 20 73 7a 4e 65 77 20 2b 3d 20  .      szNew += 
a4990 4e 20 2b 20 31 3b 0a 20 20 20 20 20 20 69 66 28  N + 1;.      if(
a49a0 20 73 7a 4e 65 77 20 3e 20 70 2d 3e 6d 78 41 6c   szNew > p->mxAl
a49b0 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73  loc ){.        s
a49c0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65  qlite3StrAccumRe
a49d0 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20  set(p);.        
a49e0 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 31 3b 0a 20  p->tooBig = 1;. 
a49f0 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
a4a00 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
a4a10 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20      p->nAlloc = 
a4a20 28 69 6e 74 29 73 7a 4e 65 77 3b 0a 20 20 20 20  (int)szNew;.    
a4a30 20 20 7d 0a 20 20 20 20 20 20 7a 4e 65 77 20 3d    }.      zNew =
a4a40 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
a4a50 52 61 77 28 70 2d 3e 64 62 2c 20 70 2d 3e 6e 41  Raw(p->db, p->nA
a4a60 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 20 20 69 66  lloc );.      if
a4a70 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 20 20  ( zNew ){.      
a4a80 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20 70    memcpy(zNew, p
a4a90 2d 3e 7a 54 65 78 74 2c 20 70 2d 3e 6e 43 68 61  ->zText, p->nCha
a4aa0 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  r);.        sqli
a4ab0 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74  te3StrAccumReset
a4ac0 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  (p);.        p->
a4ad0 7a 54 65 78 74 20 3d 20 7a 4e 65 77 3b 0a 20 20  zText = zNew;.  
a4ae0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
a4af0 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c     p->mallocFail
a4b00 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ed = 1;.        
a4b10 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52  sqlite3StrAccumR
a4b20 65 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20  eset(p);.       
a4b30 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
a4b40 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d  .    }.  }.  mem
a4b50 63 70 79 28 26 70 2d 3e 7a 54 65 78 74 5b 70 2d  cpy(&p->zText[p-
a4b60 3e 6e 43 68 61 72 5d 2c 20 7a 2c 20 4e 29 3b 0a  >nChar], z, N);.
a4b70 20 20 70 2d 3e 6e 43 68 61 72 20 2b 3d 20 4e 3b    p->nChar += N;
a4b80 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 69 73 68  .}../*.** Finish
a4b90 20 6f 66 66 20 61 20 73 74 72 69 6e 67 20 62 79   off a string by
a4ba0 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 69 74 20   making sure it 
a4bb0 69 73 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74  is zero-terminat
a4bc0 65 64 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ed..** Return a 
a4bd0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72  pointer to the r
a4be0 65 73 75 6c 74 69 6e 67 20 73 74 72 69 6e 67 2e  esulting string.
a4bf0 20 20 52 65 74 75 72 6e 20 61 20 4e 55 4c 4c 0a    Return a NULL.
a4c00 2a 2a 20 70 6f 69 6e 74 65 72 20 69 66 20 61 6e  ** pointer if an
a4c10 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 20  y kind of error 
a4c20 77 61 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e  was encountered.
a4c30 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
a4c40 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
a4c50 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 53  StrAccumFinish(S
a4c60 74 72 41 63 63 75 6d 20 2a 70 29 7b 0a 20 20 69  trAccum *p){.  i
a4c70 66 28 20 70 2d 3e 7a 54 65 78 74 20 29 7b 0a 20  f( p->zText ){. 
a4c80 20 20 20 70 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e     p->zText[p->n
a4c90 43 68 61 72 5d 20 3d 20 30 3b 0a 20 20 20 20 69  Char] = 0;.    i
a4ca0 66 28 20 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20  f( p->useMalloc 
a4cb0 26 26 20 70 2d 3e 7a 54 65 78 74 3d 3d 70 2d 3e  && p->zText==p->
a4cc0 7a 42 61 73 65 20 29 7b 0a 20 20 20 20 20 20 70  zBase ){.      p
a4cd0 2d 3e 7a 54 65 78 74 20 3d 20 73 71 6c 69 74 65  ->zText = sqlite
a4ce0 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e  3DbMallocRaw(p->
a4cf0 64 62 2c 20 70 2d 3e 6e 43 68 61 72 2b 31 20 29  db, p->nChar+1 )
a4d00 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 7a  ;.      if( p->z
a4d10 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Text ){.        
a4d20 6d 65 6d 63 70 79 28 70 2d 3e 7a 54 65 78 74 2c  memcpy(p->zText,
a4d30 20 70 2d 3e 7a 42 61 73 65 2c 20 70 2d 3e 6e 43   p->zBase, p->nC
a4d40 68 61 72 2b 31 29 3b 0a 20 20 20 20 20 20 7d 65  har+1);.      }e
a4d50 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  lse{.        p->
a4d60 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
a4d70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
a4d80 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e    }.  return p->
a4d90 7a 54 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  zText;.}../*.** 
a4da0 52 65 73 65 74 20 61 6e 20 53 74 72 41 63 63 75  Reset an StrAccu
a4db0 6d 20 73 74 72 69 6e 67 2e 20 20 52 65 63 6c 61  m string.  Recla
a4dc0 69 6d 20 61 6c 6c 20 6d 61 6c 6c 6f 63 65 64 20  im all malloced 
a4dd0 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54  memory..*/.SQLIT
a4de0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
a4df0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65  qlite3StrAccumRe
a4e00 73 65 74 28 53 74 72 41 63 63 75 6d 20 2a 70 29  set(StrAccum *p)
a4e10 7b 0a 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 74  {.  if( p->zText
a4e20 21 3d 70 2d 3e 7a 42 61 73 65 20 29 7b 0a 20 20  !=p->zBase ){.  
a4e30 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
a4e40 70 2d 3e 64 62 2c 20 70 2d 3e 7a 54 65 78 74 29  p->db, p->zText)
a4e50 3b 0a 20 20 7d 0a 20 20 70 2d 3e 7a 54 65 78 74  ;.  }.  p->zText
a4e60 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49   = 0;.}../*.** I
a4e70 6e 69 74 69 61 6c 69 7a 65 20 61 20 73 74 72 69  nitialize a stri
a4e80 6e 67 20 61 63 63 75 6d 75 6c 61 74 6f 72 0a 2a  ng accumulator.*
a4e90 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
a4ea0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72   void sqlite3Str
a4eb0 41 63 63 75 6d 49 6e 69 74 28 53 74 72 41 63 63  AccumInit(StrAcc
a4ec0 75 6d 20 2a 70 2c 20 63 68 61 72 20 2a 7a 42 61  um *p, char *zBa
a4ed0 73 65 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 6d  se, int n, int m
a4ee0 78 29 7b 0a 20 20 70 2d 3e 7a 54 65 78 74 20 3d  x){.  p->zText =
a4ef0 20 70 2d 3e 7a 42 61 73 65 20 3d 20 7a 42 61 73   p->zBase = zBas
a4f00 65 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 30 3b 0a  e;.  p->db = 0;.
a4f10 20 20 70 2d 3e 6e 43 68 61 72 20 3d 20 30 3b 0a    p->nChar = 0;.
a4f20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 6e 3b    p->nAlloc = n;
a4f30 0a 20 20 70 2d 3e 6d 78 41 6c 6c 6f 63 20 3d 20  .  p->mxAlloc = 
a4f40 6d 78 3b 0a 20 20 70 2d 3e 75 73 65 4d 61 6c 6c  mx;.  p->useMall
a4f50 6f 63 20 3d 20 31 3b 0a 20 20 70 2d 3e 74 6f 6f  oc = 1;.  p->too
a4f60 42 69 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 6d 61  Big = 0;.  p->ma
a4f70 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a  llocFailed = 0;.
a4f80 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69  }../*.** Print i
a4f90 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69  nto memory obtai
a4fa0 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d  ned from sqliteM
a4fb0 61 6c 6c 6f 63 28 29 2e 20 20 55 73 65 20 74 68  alloc().  Use th
a4fc0 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d  e internal.** %-
a4fd0 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e  conversion exten
a4fe0 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  sions..*/.SQLITE
a4ff0 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
a5000 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 73  qlite3VMPrintf(s
a5010 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73  qlite3 *db, cons
a5020 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c  t char *zFormat,
a5030 20 76 61 5f 6c 69 73 74 20 61 70 29 7b 0a 20 20   va_list ap){.  
a5040 63 68 61 72 20 2a 7a 3b 0a 20 20 63 68 61 72 20  char *z;.  char 
a5050 7a 42 61 73 65 5b 53 51 4c 49 54 45 5f 50 52 49  zBase[SQLITE_PRI
a5060 4e 54 5f 42 55 46 5f 53 49 5a 45 5d 3b 0a 20 20  NT_BUF_SIZE];.  
a5070 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20 20  StrAccum acc;.  
a5080 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b  assert( db!=0 );
a5090 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63  .  sqlite3StrAcc
a50a0 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 61  umInit(&acc, zBa
a50b0 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61 73 65  se, sizeof(zBase
a50c0 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
a50d0 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 4c 69           db->aLi
a50e0 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
a50f0 5f 4c 45 4e 47 54 48 5d 29 3b 0a 20 20 61 63 63  _LENGTH]);.  acc
a5100 2e 64 62 20 3d 20 64 62 3b 0a 20 20 73 71 6c 69  .db = db;.  sqli
a5110 74 65 33 56 58 50 72 69 6e 74 66 28 26 61 63 63  te3VXPrintf(&acc
a5120 2c 20 31 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70  , 1, zFormat, ap
a5130 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33  );.  z = sqlite3
a5140 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26  StrAccumFinish(&
a5150 61 63 63 29 3b 0a 20 20 69 66 28 20 61 63 63 2e  acc);.  if( acc.
a5160 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
a5170 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
a5180 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  iled = 1;.  }.  
a5190 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a  return z;.}../*.
a51a0 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65  ** Print into me
a51b0 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
a51c0 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28  om sqliteMalloc(
a51d0 29 2e 20 20 55 73 65 20 74 68 65 20 69 6e 74 65  ).  Use the inte
a51e0 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72  rnal.** %-conver
a51f0 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e  sion extensions.
a5200 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
a5210 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
a5220 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33 20  MPrintf(sqlite3 
a5230 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *db, const char 
a5240 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a  *zFormat, ...){.
a5250 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
a5260 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 73 74  char *z;.  va_st
a5270 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29  art(ap, zFormat)
a5280 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 56  ;.  z = sqlite3V
a5290 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72  MPrintf(db, zFor
a52a0 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65  mat, ap);.  va_e
a52b0 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e  nd(ap);.  return
a52c0 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b   z;.}../*.** Lik
a52d0 65 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  e sqlite3MPrintf
a52e0 28 29 2c 20 62 75 74 20 63 61 6c 6c 20 73 71 6c  (), but call sql
a52f0 69 74 65 33 44 62 46 72 65 65 28 29 20 6f 6e 20  ite3DbFree() on 
a5300 7a 53 74 72 20 61 66 74 65 72 20 66 6f 72 6d 61  zStr after forma
a5310 74 74 69 6e 67 0a 2a 2a 20 74 68 65 20 73 74 72  tting.** the str
a5320 69 6e 67 20 61 6e 64 20 62 65 66 6f 72 65 20 72  ing and before r
a5330 65 74 75 72 6e 6e 69 6e 67 2e 20 20 54 68 69 73  eturnning.  This
a5340 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e 74 65   routine is inte
a5350 6e 64 65 64 20 74 6f 20 62 65 20 75 73 65 64 0a  nded to be used.
a5360 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 61 6e 20  ** to modify an 
a5370 65 78 69 73 74 69 6e 67 20 73 74 72 69 6e 67 2e  existing string.
a5380 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a    For example:.*
a5390 2a 0a 2a 2a 20 20 20 20 20 20 20 78 20 3d 20 73  *.**       x = s
a53a0 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
a53b0 2c 20 78 2c 20 22 70 72 65 66 69 78 20 25 73 20  , x, "prefix %s 
a53c0 73 75 66 66 69 78 22 2c 20 78 29 3b 0a 2a 2a 0a  suffix", x);.**.
a53d0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
a53e0 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4d  E char *sqlite3M
a53f0 41 70 70 65 6e 64 66 28 73 71 6c 69 74 65 33 20  Appendf(sqlite3 
a5400 2a 64 62 2c 20 63 68 61 72 20 2a 7a 53 74 72 2c  *db, char *zStr,
a5410 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
a5420 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  rmat, ...){.  va
a5430 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72  _list ap;.  char
a5440 20 2a 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74 28   *z;.  va_start(
a5450 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20  ap, zFormat);.  
a5460 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69  z = sqlite3VMPri
a5470 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c  ntf(db, zFormat,
a5480 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61   ap);.  va_end(a
a5490 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  p);.  sqlite3DbF
a54a0 72 65 65 28 64 62 2c 20 7a 53 74 72 29 3b 0a 20  ree(db, zStr);. 
a54b0 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a   return z;.}../*
a54c0 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d  .** Print into m
a54d0 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
a54e0 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  rom sqlite3_mall
a54f0 6f 63 28 29 2e 20 20 4f 6d 69 74 20 74 68 65 20  oc().  Omit the 
a5500 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f  internal.** %-co
a5510 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e 73 69  nversion extensi
a5520 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ons..*/.SQLITE_A
a5530 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  PI char *sqlite3
a5540 5f 76 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20  _vmprintf(const 
a5550 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 76  char *zFormat, v
a5560 61 5f 6c 69 73 74 20 61 70 29 7b 0a 20 20 63 68  a_list ap){.  ch
a5570 61 72 20 2a 7a 3b 0a 20 20 63 68 61 72 20 7a 42  ar *z;.  char zB
a5580 61 73 65 5b 53 51 4c 49 54 45 5f 50 52 49 4e 54  ase[SQLITE_PRINT
a5590 5f 42 55 46 5f 53 49 5a 45 5d 3b 0a 20 20 53 74  _BUF_SIZE];.  St
a55a0 72 41 63 63 75 6d 20 61 63 63 3b 0a 23 69 66 6e  rAccum acc;.#ifn
a55b0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
a55c0 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73  AUTOINIT.  if( s
a55d0 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
a55e0 65 28 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  e() ) return 0;.
a55f0 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33  #endif.  sqlite3
a5600 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63  StrAccumInit(&ac
a5610 63 2c 20 7a 42 61 73 65 2c 20 73 69 7a 65 6f 66  c, zBase, sizeof
a5620 28 7a 42 61 73 65 29 2c 20 53 51 4c 49 54 45 5f  (zBase), SQLITE_
a5630 4d 41 58 5f 4c 45 4e 47 54 48 29 3b 0a 20 20 73  MAX_LENGTH);.  s
a5640 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 26  qlite3VXPrintf(&
a5650 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74 2c  acc, 0, zFormat,
a5660 20 61 70 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69   ap);.  z = sqli
a5670 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73  te3StrAccumFinis
a5680 68 28 26 61 63 63 29 3b 0a 20 20 72 65 74 75 72  h(&acc);.  retur
a5690 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72  n z;.}../*.** Pr
a56a0 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20  int into memory 
a56b0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
a56c0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 28 29  lite3_malloc()()
a56d0 2e 20 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 65  .  Omit the inte
a56e0 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72  rnal.** %-conver
a56f0 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e  sion extensions.
a5700 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
a5710 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6d 70 72  har *sqlite3_mpr
a5720 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20  intf(const char 
a5730 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a  *zFormat, ...){.
a5740 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
a5750 63 68 61 72 20 2a 7a 3b 0a 23 69 66 6e 64 65 66  char *z;.#ifndef
a5760 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
a5770 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69  OINIT.  if( sqli
a5780 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
a5790 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e   ) return 0;.#en
a57a0 64 69 66 0a 20 20 76 61 5f 73 74 61 72 74 28 61  dif.  va_start(a
a57b0 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a  p, zFormat);.  z
a57c0 20 3d 20 73 71 6c 69 74 65 33 5f 76 6d 70 72 69   = sqlite3_vmpri
a57d0 6e 74 66 28 7a 46 6f 72 6d 61 74 2c 20 61 70 29  ntf(zFormat, ap)
a57e0 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  ;.  va_end(ap);.
a57f0 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f    return z;.}../
a5800 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 6e 70  *.** sqlite3_snp
a5810 72 69 6e 74 66 28 29 20 77 6f 72 6b 73 20 6c 69  rintf() works li
a5820 6b 65 20 73 6e 70 72 69 6e 74 66 28 29 20 65 78  ke snprintf() ex
a5830 63 65 70 74 20 74 68 61 74 20 69 74 20 69 67 6e  cept that it ign
a5840 6f 72 65 73 20 74 68 65 0a 2a 2a 20 63 75 72 72  ores the.** curr
a5850 65 6e 74 20 6c 6f 63 61 6c 65 20 73 65 74 74 69  ent locale setti
a5860 6e 67 73 2e 20 20 54 68 69 73 20 69 73 20 69 6d  ngs.  This is im
a5870 70 6f 72 74 61 6e 74 20 66 6f 72 20 53 51 4c 69  portant for SQLi
a5880 74 65 20 62 65 63 61 75 73 65 20 77 65 0a 2a 2a  te because we.**
a5890 20 61 72 65 20 6e 6f 74 20 61 62 6c 65 20 74 6f   are not able to
a58a0 20 75 73 65 20 61 20 22 2c 22 20 61 73 20 74 68   use a "," as th
a58b0 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20  e decimal point 
a58c0 69 6e 20 70 6c 61 63 65 20 6f 66 20 22 2e 22 20  in place of "." 
a58d0 61 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20  as.** specified 
a58e0 62 79 20 73 6f 6d 65 20 6c 6f 63 61 6c 65 73 2e  by some locales.
a58f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
a5900 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 6e 70  har *sqlite3_snp
a5910 72 69 6e 74 66 28 69 6e 74 20 6e 2c 20 63 68 61  rintf(int n, cha
a5920 72 20 2a 7a 42 75 66 2c 20 63 6f 6e 73 74 20 63  r *zBuf, const c
a5930 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e  har *zFormat, ..
a5940 2e 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20  .){.  char *z;. 
a5950 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 53   va_list ap;.  S
a5960 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 0a 20 20  trAccum acc;..  
a5970 69 66 28 20 6e 3c 3d 30 20 29 7b 0a 20 20 20 20  if( n<=0 ){.    
a5980 72 65 74 75 72 6e 20 7a 42 75 66 3b 0a 20 20 7d  return zBuf;.  }
a5990 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63  .  sqlite3StrAcc
a59a0 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 75  umInit(&acc, zBu
a59b0 66 2c 20 6e 2c 20 30 29 3b 0a 20 20 61 63 63 2e  f, n, 0);.  acc.
a59c0 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20  useMalloc = 0;. 
a59d0 20 76 61 5f 73 74 61 72 74 28 61 70 2c 7a 46 6f   va_start(ap,zFo
a59e0 72 6d 61 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  rmat);.  sqlite3
a59f0 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20 30  VXPrintf(&acc, 0
a5a00 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a  , zFormat, ap);.
a5a10 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20    va_end(ap);.  
a5a20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 41 63  z = sqlite3StrAc
a5a30 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b  cumFinish(&acc);
a5a40 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a  .  return z;.}..
a5a50 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
a5a60 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20  TE_DEBUG)./*.** 
a5a70 41 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 72 69  A version of pri
a5a80 6e 74 66 28 29 20 74 68 61 74 20 75 6e 64 65 72  ntf() that under
a5a90 73 74 61 6e 64 73 20 25 6c 6c 64 2e 20 20 55 73  stands %lld.  Us
a5aa0 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67  ed for debugging
a5ab0 2e 0a 2a 2a 20 54 68 65 20 70 72 69 6e 74 66 28  ..** The printf(
a5ac0 29 20 62 75 69 6c 74 20 69 6e 74 6f 20 73 6f 6d  ) built into som
a5ad0 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 77 69  e versions of wi
a5ae0 6e 64 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 75  ndows does not u
a5af0 6e 64 65 72 73 74 61 6e 64 20 25 6c 6c 64 0a 2a  nderstand %lld.*
a5b00 2a 20 61 6e 64 20 73 65 67 66 61 75 6c 74 73 20  * and segfaults 
a5b10 69 66 20 79 6f 75 20 67 69 76 65 20 69 74 20 61  if you give it a
a5b20 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 2e 0a   long long int..
a5b30 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
a5b40 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65  E void sqlite3De
a5b50 62 75 67 50 72 69 6e 74 66 28 63 6f 6e 73 74 20  bugPrintf(const 
a5b60 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e  char *zFormat, .
a5b70 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61  ..){.  va_list a
a5b80 70 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63  p;.  StrAccum ac
a5b90 63 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 35  c;.  char zBuf[5
a5ba0 30 30 5d 3b 0a 20 20 73 71 6c 69 74 65 33 53 74  00];.  sqlite3St
a5bb0 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63 63 2c  rAccumInit(&acc,
a5bc0 20 7a 42 75 66 2c 20 73 69 7a 65 6f 66 28 7a 42   zBuf, sizeof(zB
a5bd0 75 66 29 2c 20 30 29 3b 0a 20 20 61 63 63 2e 75  uf), 0);.  acc.u
a5be0 73 65 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20  seMalloc = 0;.  
a5bf0 76 61 5f 73 74 61 72 74 28 61 70 2c 7a 46 6f 72  va_start(ap,zFor
a5c00 6d 61 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  mat);.  sqlite3V
a5c10 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20 30 2c  XPrintf(&acc, 0,
a5c20 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20   zFormat, ap);. 
a5c30 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 73   va_end(ap);.  s
a5c40 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69  qlite3StrAccumFi
a5c50 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 66 70  nish(&acc);.  fp
a5c60 72 69 6e 74 66 28 73 74 64 6f 75 74 2c 22 25 73  rintf(stdout,"%s
a5c70 22 2c 20 7a 42 75 66 29 3b 0a 20 20 66 66 6c 75  ", zBuf);.  fflu
a5c80 73 68 28 73 74 64 6f 75 74 29 3b 0a 7d 0a 23 65  sh(stdout);.}.#e
a5c90 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
a5ca0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 72 69  ***** End of pri
a5cb0 6e 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ntf.c **********
a5cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5ce0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
a5cf0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
a5d00 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a   random.c ******
a5d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5d30 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
a5d40 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
a5d50 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
a5d60 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
a5d70 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
a5d80 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
a5d90 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
a5da0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
a5db0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
a5dc0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
a5dd0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
a5de0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
a5df0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
a5e00 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
a5e10 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
a5e20 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
a5e30 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
a5e40 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
a5e50 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
a5e60 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
a5e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
a5eb0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
a5ec0 6e 73 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65  ns code to imple
a5ed0 6d 65 6e 74 20 61 20 70 73 65 75 64 6f 2d 72 61  ment a pseudo-ra
a5ee0 6e 64 6f 6d 20 6e 75 6d 62 65 72 0a 2a 2a 20 67  ndom number.** g
a5ef0 65 6e 65 72 61 74 6f 72 20 28 50 52 4e 47 29 20  enerator (PRNG) 
a5f00 66 6f 72 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a  for SQLite..**.*
a5f10 2a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 73  * Random numbers
a5f20 20 61 72 65 20 75 73 65 64 20 62 79 20 73 6f 6d   are used by som
a5f30 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
a5f40 65 20 62 61 63 6b 65 6e 64 73 20 69 6e 20 6f 72  e backends in or
a5f50 64 65 72 0a 2a 2a 20 74 6f 20 67 65 6e 65 72 61  der.** to genera
a5f60 74 65 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67 65  te random intege
a5f70 72 20 6b 65 79 73 20 66 6f 72 20 74 61 62 6c 65  r keys for table
a5f80 73 20 6f 72 20 72 61 6e 64 6f 6d 20 66 69 6c 65  s or random file
a5f90 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  names..**.** $Id
a5fa0 3a 20 72 61 6e 64 6f 6d 2e 63 2c 76 20 31 2e 32  : random.c,v 1.2
a5fb0 39 20 32 30 30 38 2f 31 32 2f 31 30 20 31 39 3a  9 2008/12/10 19:
a5fc0 32 36 3a 32 34 20 64 72 68 20 45 78 70 20 24 0a  26:24 drh Exp $.
a5fd0 2a 2f 0a 0a 0a 2f 2a 20 41 6c 6c 20 74 68 72 65  */.../* All thre
a5fe0 61 64 73 20 73 68 61 72 65 20 61 20 73 69 6e 67  ads share a sing
a5ff0 6c 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72  le random number
a6000 20 67 65 6e 65 72 61 74 6f 72 2e 0a 2a 2a 20 54   generator..** T
a6010 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73  his structure is
a6020 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61   the current sta
a6030 74 65 20 6f 66 20 74 68 65 20 67 65 6e 65 72 61  te of the genera
a6040 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53  tor..*/.static S
a6050 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74  QLITE_WSD struct
a6060 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65   sqlite3PrngType
a6070 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68   {.  unsigned ch
a6080 61 72 20 69 73 49 6e 69 74 3b 20 20 20 20 20 20  ar isInit;      
a6090 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69      /* True if i
a60a0 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20  nitialized */.  
a60b0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 2c  unsigned char i,
a60c0 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   j;            /
a60d0 2a 20 53 74 61 74 65 20 76 61 72 69 61 62 6c 65  * State variable
a60e0 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  s */.  unsigned 
a60f0 63 68 61 72 20 73 5b 32 35 36 5d 3b 20 20 20 20  char s[256];    
a6100 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 20 76        /* State v
a6110 61 72 69 61 62 6c 65 73 20 2a 2f 0a 7d 20 73 71  ariables */.} sq
a6120 6c 69 74 65 33 50 72 6e 67 3b 0a 0a 2f 2a 0a 2a  lite3Prng;../*.*
a6130 2a 20 47 65 74 20 61 20 73 69 6e 67 6c 65 20 38  * Get a single 8
a6140 2d 62 69 74 20 72 61 6e 64 6f 6d 20 76 61 6c 75  -bit random valu
a6150 65 20 66 72 6f 6d 20 74 68 65 20 52 43 34 20 50  e from the RC4 P
a6160 52 4e 47 2e 20 20 54 68 65 20 4d 75 74 65 78 0a  RNG.  The Mutex.
a6170 2a 2a 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20  ** must be held 
a6180 77 68 69 6c 65 20 65 78 65 63 75 74 69 6e 67 20  while executing 
a6190 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a  this routine..**
a61a0 0a 2a 2a 20 57 68 79 20 6e 6f 74 20 6a 75 73 74  .** Why not just
a61b0 20 75 73 65 20 61 20 6c 69 62 72 61 72 79 20 72   use a library r
a61c0 61 6e 64 6f 6d 20 67 65 6e 65 72 61 74 6f 72 20  andom generator 
a61d0 6c 69 6b 65 20 6c 72 61 6e 64 34 38 28 29 20 66  like lrand48() f
a61e0 6f 72 20 74 68 69 73 3f 0a 2a 2a 20 42 65 63 61  or this?.** Beca
a61f0 75 73 65 20 74 68 65 20 4f 50 5f 4e 65 77 52 6f  use the OP_NewRo
a6200 77 69 64 20 6f 70 63 6f 64 65 20 69 6e 20 74 68  wid opcode in th
a6210 65 20 56 44 42 45 20 64 65 70 65 6e 64 73 20 6f  e VDBE depends o
a6220 6e 20 68 61 76 69 6e 67 20 61 20 76 65 72 79 0a  n having a very.
a6230 2a 2a 20 67 6f 6f 64 20 73 6f 75 72 63 65 20 6f  ** good source o
a6240 66 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 73  f random numbers
a6250 2e 20 20 54 68 65 20 6c 72 61 6e 64 34 38 28 29  .  The lrand48()
a6260 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f   library functio
a6270 6e 20 6d 61 79 0a 2a 2a 20 77 65 6c 6c 20 62 65  n may.** well be
a6280 20 67 6f 6f 64 20 65 6e 6f 75 67 68 2e 20 20 42   good enough.  B
a6290 75 74 20 6d 61 79 62 65 20 6e 6f 74 2e 20 20 4f  ut maybe not.  O
a62a0 72 20 6d 61 79 62 65 20 6c 72 61 6e 64 34 38 28  r maybe lrand48(
a62b0 29 20 68 61 73 20 73 6f 6d 65 0a 2a 2a 20 73 75  ) has some.** su
a62c0 62 74 6c 65 20 70 72 6f 62 6c 65 6d 73 20 6f 6e  btle problems on
a62d0 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 20 74 68   some systems th
a62e0 61 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20 70  at could cause p
a62f0 72 6f 62 6c 65 6d 73 2e 20 20 49 74 20 69 73 20  roblems.  It is 
a6300 68 61 72 64 0a 2a 2a 20 74 6f 20 6b 6e 6f 77 2e  hard.** to know.
a6310 20 20 54 6f 20 6d 69 6e 69 6d 69 7a 65 20 74 68    To minimize th
a6320 65 20 72 69 73 6b 20 6f 66 20 70 72 6f 62 6c 65  e risk of proble
a6330 6d 73 20 64 75 65 20 74 6f 20 62 61 64 20 6c 72  ms due to bad lr
a6340 61 6e 64 34 38 28 29 0a 2a 2a 20 69 6d 70 6c 65  and48().** imple
a6350 6d 65 6e 74 61 74 69 6f 6e 73 2c 20 53 51 4c 69  mentations, SQLi
a6360 74 65 20 75 73 65 73 20 74 68 69 73 20 72 61 6e  te uses this ran
a6370 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 72  dom number gener
a6380 61 74 6f 72 20 62 61 73 65 64 0a 2a 2a 20 6f 6e  ator based.** on
a6390 20 52 43 34 2c 20 77 68 69 63 68 20 77 65 20 6b   RC4, which we k
a63a0 6e 6f 77 20 77 6f 72 6b 73 20 76 65 72 79 20 77  now works very w
a63b0 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 28 4c 61 74 65  ell..**.** (Late
a63c0 72 29 3a 20 20 41 63 74 75 61 6c 6c 79 2c 20 4f  r):  Actually, O
a63d0 50 5f 4e 65 77 52 6f 77 69 64 20 64 6f 65 73 20  P_NewRowid does 
a63e0 6e 6f 74 20 64 65 70 65 6e 64 20 6f 6e 20 61 20  not depend on a 
a63f0 67 6f 6f 64 20 73 6f 75 72 63 65 20 6f 66 0a 2a  good source of.*
a6400 2a 20 72 61 6e 64 6f 6d 6e 65 73 73 20 61 6e 79  * randomness any
a6410 20 6d 6f 72 65 2e 20 20 42 75 74 20 77 65 20 77   more.  But we w
a6420 69 6c 6c 20 6c 65 61 76 65 20 74 68 69 73 20 63  ill leave this c
a6430 6f 64 65 20 69 6e 20 61 6c 6c 20 74 68 65 20 73  ode in all the s
a6440 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75  ame..*/.static u
a6450 38 20 72 61 6e 64 6f 6d 42 79 74 65 28 76 6f 69  8 randomByte(voi
a6460 64 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  d){.  unsigned c
a6470 68 61 72 20 74 3b 0a 0a 0a 20 20 2f 2a 20 54 68  har t;...  /* Th
a6480 65 20 22 77 73 64 50 72 6e 67 22 20 6d 61 63 72  e "wsdPrng" macr
a6490 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74  o will resolve t
a64a0 6f 20 74 68 65 20 70 73 65 75 64 6f 2d 72 61 6e  o the pseudo-ran
a64b0 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 72  dom number gener
a64c0 61 74 6f 72 0a 20 20 2a 2a 20 73 74 61 74 65 20  ator.  ** state 
a64d0 76 65 63 74 6f 72 2e 20 20 49 66 20 77 72 69 74  vector.  If writ
a64e0 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61  able static data
a64f0 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20   is unsupported 
a6500 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c 0a 20  on the target,. 
a6510 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c   ** we have to l
a6520 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 65 20  ocate the state 
a6530 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69  vector at run-ti
a6540 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65  me.  In the more
a6550 20 63 6f 6d 6d 6f 6e 0a 20 20 2a 2a 20 63 61 73   common.  ** cas
a6560 65 20 77 68 65 72 65 20 77 72 69 74 61 62 6c 65  e where writable
a6570 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20   static data is 
a6580 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64 50 72  supported, wsdPr
a6590 6e 67 20 63 61 6e 20 72 65 66 65 72 20 64 69 72  ng can refer dir
a65a0 65 63 74 6c 79 0a 20 20 2a 2a 20 74 6f 20 74 68  ectly.  ** to th
a65b0 65 20 22 73 71 6c 69 74 65 33 50 72 6e 67 22 20  e "sqlite3Prng" 
a65c0 73 74 61 74 65 20 76 65 63 74 6f 72 20 64 65 63  state vector dec
a65d0 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a 20 20 2a  lared above..  *
a65e0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
a65f0 4f 4d 49 54 5f 57 53 44 0a 20 20 73 74 72 75 63  OMIT_WSD.  struc
a6600 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70  t sqlite3PrngTyp
a6610 65 20 2a 70 20 3d 20 26 47 4c 4f 42 41 4c 28 73  e *p = &GLOBAL(s
a6620 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e  truct sqlite3Prn
a6630 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72  gType, sqlite3Pr
a6640 6e 67 29 3b 0a 23 20 64 65 66 69 6e 65 20 77 73  ng);.# define ws
a6650 64 50 72 6e 67 20 70 5b 30 5d 0a 23 65 6c 73 65  dPrng p[0].#else
a6660 0a 23 20 64 65 66 69 6e 65 20 77 73 64 50 72 6e  .# define wsdPrn
a6670 67 20 73 71 6c 69 74 65 33 50 72 6e 67 0a 23 65  g sqlite3Prng.#e
a6680 6e 64 69 66 0a 0a 0a 20 20 2f 2a 20 49 6e 69 74  ndif...  /* Init
a6690 69 61 6c 69 7a 65 20 74 68 65 20 73 74 61 74 65  ialize the state
a66a0 20 6f 66 20 74 68 65 20 72 61 6e 64 6f 6d 20 6e   of the random n
a66b0 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20  umber generator 
a66c0 6f 6e 63 65 2c 0a 20 20 2a 2a 20 74 68 65 20 66  once,.  ** the f
a66d0 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 72  irst time this r
a66e0 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
a66f0 2e 20 20 54 68 65 20 73 65 65 64 20 76 61 6c 75  .  The seed valu
a6700 65 20 64 6f 65 73 0a 20 20 2a 2a 20 6e 6f 74 20  e does.  ** not 
a6710 6e 65 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20  need to contain 
a6720 61 20 6c 6f 74 20 6f 66 20 72 61 6e 64 6f 6d 6e  a lot of randomn
a6730 65 73 73 20 73 69 6e 63 65 20 77 65 20 61 72 65  ess since we are
a6740 20 6e 6f 74 0a 20 20 2a 2a 20 74 72 79 69 6e 67   not.  ** trying
a6750 20 74 6f 20 64 6f 20 73 65 63 75 72 65 20 65 6e   to do secure en
a6760 63 72 79 70 74 69 6f 6e 20 6f 72 20 61 6e 79 74  cryption or anyt
a6770 68 69 6e 67 20 6c 69 6b 65 20 74 68 61 74 2e 2e  hing like that..
a6780 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 68  ..  **.  ** Noth
a6790 69 6e 67 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ing in this file
a67a0 20 6f 72 20 61 6e 79 77 68 65 72 65 20 65 6c 73   or anywhere els
a67b0 65 20 69 6e 20 53 51 4c 69 74 65 20 64 6f 65 73  e in SQLite does
a67c0 20 61 6e 79 20 6b 69 6e 64 20 6f 66 0a 20 20 2a   any kind of.  *
a67d0 2a 20 65 6e 63 72 79 70 74 69 6f 6e 2e 20 20 54  * encryption.  T
a67e0 68 65 20 52 43 34 20 61 6c 67 6f 72 69 74 68 6d  he RC4 algorithm
a67f0 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 61   is being used a
a6800 73 20 61 20 50 52 4e 47 20 28 70 73 65 75 64 6f  s a PRNG (pseudo
a6810 2d 72 61 6e 64 6f 6d 0a 20 20 2a 2a 20 6e 75 6d  -random.  ** num
a6820 62 65 72 20 67 65 6e 65 72 61 74 6f 72 29 20 6e  ber generator) n
a6830 6f 74 20 61 73 20 61 6e 20 65 6e 63 72 79 70 74  ot as an encrypt
a6840 69 6f 6e 20 64 65 76 69 63 65 2e 0a 20 20 2a 2f  ion device..  */
a6850 0a 20 20 69 66 28 20 21 77 73 64 50 72 6e 67 2e  .  if( !wsdPrng.
a6860 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 69 6e  isInit ){.    in
a6870 74 20 69 3b 0a 20 20 20 20 63 68 61 72 20 6b 5b  t i;.    char k[
a6880 32 35 36 5d 3b 0a 20 20 20 20 77 73 64 50 72 6e  256];.    wsdPrn
a6890 67 2e 6a 20 3d 20 30 3b 0a 20 20 20 20 77 73 64  g.j = 0;.    wsd
a68a0 50 72 6e 67 2e 69 20 3d 20 30 3b 0a 20 20 20 20  Prng.i = 0;.    
a68b0 73 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f 6d 6e  sqlite3OsRandomn
a68c0 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 5f  ess(sqlite3_vfs_
a68d0 66 69 6e 64 28 30 29 2c 20 32 35 36 2c 20 6b 29  find(0), 256, k)
a68e0 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
a68f0 3c 32 35 36 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  <256; i++){.    
a6900 20 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20 3d    wsdPrng.s[i] =
a6910 20 28 75 38 29 69 3b 0a 20 20 20 20 7d 0a 20 20   (u8)i;.    }.  
a6920 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35 36    for(i=0; i<256
a6930 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 77 73  ; i++){.      ws
a6940 64 50 72 6e 67 2e 6a 20 2b 3d 20 77 73 64 50 72  dPrng.j += wsdPr
a6950 6e 67 2e 73 5b 69 5d 20 2b 20 6b 5b 69 5d 3b 0a  ng.s[i] + k[i];.
a6960 20 20 20 20 20 20 74 20 3d 20 77 73 64 50 72 6e        t = wsdPrn
a6970 67 2e 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a  g.s[wsdPrng.j];.
a6980 20 20 20 20 20 20 77 73 64 50 72 6e 67 2e 73 5b        wsdPrng.s[
a6990 77 73 64 50 72 6e 67 2e 6a 5d 20 3d 20 77 73 64  wsdPrng.j] = wsd
a69a0 50 72 6e 67 2e 73 5b 69 5d 3b 0a 20 20 20 20 20  Prng.s[i];.     
a69b0 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20 3d 20   wsdPrng.s[i] = 
a69c0 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 73 64  t;.    }.    wsd
a69d0 50 72 6e 67 2e 69 73 49 6e 69 74 20 3d 20 31 3b  Prng.isInit = 1;
a69e0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  .  }..  /* Gener
a69f0 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 73  ate and return s
a6a00 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 20 62 79 74  ingle random byt
a6a10 65 0a 20 20 2a 2f 0a 20 20 77 73 64 50 72 6e 67  e.  */.  wsdPrng
a6a20 2e 69 2b 2b 3b 0a 20 20 74 20 3d 20 77 73 64 50  .i++;.  t = wsdP
a6a30 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 69 5d  rng.s[wsdPrng.i]
a6a40 3b 0a 20 20 77 73 64 50 72 6e 67 2e 6a 20 2b 3d  ;.  wsdPrng.j +=
a6a50 20 74 3b 0a 20 20 77 73 64 50 72 6e 67 2e 73 5b   t;.  wsdPrng.s[
a6a60 77 73 64 50 72 6e 67 2e 69 5d 20 3d 20 77 73 64  wsdPrng.i] = wsd
a6a70 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 6a  Prng.s[wsdPrng.j
a6a80 5d 3b 0a 20 20 77 73 64 50 72 6e 67 2e 73 5b 77  ];.  wsdPrng.s[w
a6a90 73 64 50 72 6e 67 2e 6a 5d 20 3d 20 74 3b 0a 20  sdPrng.j] = t;. 
a6aa0 20 74 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73 5b   t += wsdPrng.s[
a6ab0 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 72 65  wsdPrng.i];.  re
a6ac0 74 75 72 6e 20 77 73 64 50 72 6e 67 2e 73 5b 74  turn wsdPrng.s[t
a6ad0 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ];.}../*.** Retu
a6ae0 72 6e 20 4e 20 72 61 6e 64 6f 6d 20 62 79 74 65  rn N random byte
a6af0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
a6b00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 61   void sqlite3_ra
a6b10 6e 64 6f 6d 6e 65 73 73 28 69 6e 74 20 4e 2c 20  ndomness(int N, 
a6b20 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20 20 75  void *pBuf){.  u
a6b30 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 42  nsigned char *zB
a6b40 75 66 20 3d 20 70 42 75 66 3b 0a 23 69 66 20 53  uf = pBuf;.#if S
a6b50 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
a6b60 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
a6b70 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65   *mutex = sqlite
a6b80 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
a6b90 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
a6ba0 50 52 4e 47 29 3b 0a 23 65 6e 64 69 66 0a 20 20  PRNG);.#endif.  
a6bb0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
a6bc0 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 77 68  ter(mutex);.  wh
a6bd0 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20  ile( N-- ){.    
a6be0 2a 28 7a 42 75 66 2b 2b 29 20 3d 20 72 61 6e 64  *(zBuf++) = rand
a6bf0 6f 6d 42 79 74 65 28 29 3b 0a 20 20 7d 0a 20 20  omByte();.  }.  
a6c00 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
a6c10 61 76 65 28 6d 75 74 65 78 29 3b 0a 7d 0a 0a 23  ave(mutex);.}..#
a6c20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
a6c30 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a  IT_BUILTIN_TEST.
a6c40 2f 2a 0a 2a 2a 20 46 6f 72 20 74 65 73 74 69 6e  /*.** For testin
a6c50 67 20 70 75 72 70 6f 73 65 73 2c 20 77 65 20 73  g purposes, we s
a6c60 6f 6d 65 74 69 6d 65 73 20 77 61 6e 74 20 74 6f  ometimes want to
a6c70 20 70 72 65 73 65 72 76 65 20 74 68 65 20 73 74   preserve the st
a6c80 61 74 65 20 6f 66 0a 2a 2a 20 50 52 4e 47 20 61  ate of.** PRNG a
a6c90 6e 64 20 72 65 73 74 6f 72 65 20 74 68 65 20 50  nd restore the P
a6ca0 52 4e 47 20 74 6f 20 69 74 73 20 73 61 76 65 64  RNG to its saved
a6cb0 20 73 74 61 74 65 20 61 74 20 61 20 6c 61 74 65   state at a late
a6cc0 72 20 74 69 6d 65 2c 20 6f 72 0a 2a 2a 20 74 6f  r time, or.** to
a6cd0 20 72 65 73 65 74 20 74 68 65 20 50 52 4e 47 20   reset the PRNG 
a6ce0 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c 20 73  to its initial s
a6cf0 74 61 74 65 2e 20 20 54 68 65 73 65 20 72 6f 75  tate.  These rou
a6d00 74 69 6e 65 73 20 61 63 63 6f 6d 70 6c 69 73 68  tines accomplish
a6d10 0a 2a 2a 20 74 68 6f 73 65 20 74 61 73 6b 73 2e  .** those tasks.
a6d20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
a6d30 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28  e3_test_control(
a6d40 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 6c 6c  ) interface call
a6d50 73 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  s these routines
a6d60 20 74 6f 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 20 74   to.** control t
a6d70 68 65 20 50 52 4e 47 2e 0a 2a 2f 0a 73 74 61 74  he PRNG..*/.stat
a6d80 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74  ic SQLITE_WSD st
a6d90 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67  ruct sqlite3Prng
a6da0 54 79 70 65 20 73 71 6c 69 74 65 33 53 61 76 65  Type sqlite3Save
a6db0 64 50 72 6e 67 3b 0a 53 51 4c 49 54 45 5f 50 52  dPrng;.SQLITE_PR
a6dc0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
a6dd0 65 33 50 72 6e 67 53 61 76 65 53 74 61 74 65 28  e3PrngSaveState(
a6de0 76 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70 79 28  void){.  memcpy(
a6df0 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 72  .    &GLOBAL(str
a6e00 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54  uct sqlite3PrngT
a6e10 79 70 65 2c 20 73 71 6c 69 74 65 33 53 61 76 65  ype, sqlite3Save
a6e20 64 50 72 6e 67 29 2c 0a 20 20 20 20 26 47 4c 4f  dPrng),.    &GLO
a6e30 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74  BAL(struct sqlit
a6e40 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69  e3PrngType, sqli
a6e50 74 65 33 50 72 6e 67 29 2c 0a 20 20 20 20 73 69  te3Prng),.    si
a6e60 7a 65 6f 66 28 73 71 6c 69 74 65 33 50 72 6e 67  zeof(sqlite3Prng
a6e70 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  ).  );.}.SQLITE_
a6e80 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
a6e90 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65 53  ite3PrngRestoreS
a6ea0 74 61 74 65 28 76 6f 69 64 29 7b 0a 20 20 6d 65  tate(void){.  me
a6eb0 6d 63 70 79 28 0a 20 20 20 20 26 47 4c 4f 42 41  mcpy(.    &GLOBA
a6ec0 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  L(struct sqlite3
a6ed0 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65  PrngType, sqlite
a6ee0 33 50 72 6e 67 29 2c 0a 20 20 20 20 26 47 4c 4f  3Prng),.    &GLO
a6ef0 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74  BAL(struct sqlit
a6f00 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69  e3PrngType, sqli
a6f10 74 65 33 53 61 76 65 64 50 72 6e 67 29 2c 0a 20  te3SavedPrng),. 
a6f20 20 20 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65     sizeof(sqlite
a6f30 33 50 72 6e 67 29 0a 20 20 29 3b 0a 7d 0a 53 51  3Prng).  );.}.SQ
a6f40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
a6f50 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73  d sqlite3PrngRes
a6f60 65 74 53 74 61 74 65 28 76 6f 69 64 29 7b 0a 20  etState(void){. 
a6f70 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73   GLOBAL(struct s
a6f80 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20  qlite3PrngType, 
a6f90 73 71 6c 69 74 65 33 50 72 6e 67 29 2e 69 73 49  sqlite3Prng).isI
a6fa0 6e 69 74 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69  nit = 0;.}.#endi
a6fb0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
a6fc0 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a 2f  _BUILTIN_TEST */
a6fd0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
a6fe0 2a 20 45 6e 64 20 6f 66 20 72 61 6e 64 6f 6d 2e  * End of random.
a6ff0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
a7000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7020 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
a7030 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 75 74 66  * Begin file utf
a7040 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
a7050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7070 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 70 72  /./*.** 2004 Apr
a7080 69 6c 20 31 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20  il 13.**.** The 
a7090 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
a70a0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
a70b0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
a70c0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
a70d0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
a70e0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
a70f0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
a7100 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
a7110 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
a7120 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
a7130 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
a7140 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
a7150 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
a7160 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
a7170 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
a7180 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
a7190 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
a71a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a71b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a71c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a71d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a71e0 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
a71f0 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 69  e contains routi
a7200 6e 65 73 20 75 73 65 64 20 74 6f 20 74 72 61 6e  nes used to tran
a7210 73 6c 61 74 65 20 62 65 74 77 65 65 6e 20 55 54  slate between UT
a7220 46 2d 38 2c 20 0a 2a 2a 20 55 54 46 2d 31 36 2c  F-8, .** UTF-16,
a7230 20 55 54 46 2d 31 36 42 45 2c 20 61 6e 64 20 55   UTF-16BE, and U
a7240 54 46 2d 31 36 4c 45 2e 0a 2a 2a 0a 2a 2a 20 24  TF-16LE..**.** $
a7250 49 64 3a 20 75 74 66 2e 63 2c 76 20 31 2e 37 33  Id: utf.c,v 1.73
a7260 20 32 30 30 39 2f 30 34 2f 30 31 20 31 38 3a 34   2009/04/01 18:4
a7270 30 3a 33 32 20 64 72 68 20 45 78 70 20 24 0a 2a  0:32 drh Exp $.*
a7280 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20 55 54  *.** Notes on UT
a7290 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 20 42 79 74  F-8:.**.**   Byt
a72a0 65 2d 30 20 20 20 20 42 79 74 65 2d 31 20 20 20  e-0    Byte-1   
a72b0 20 42 79 74 65 2d 32 20 20 20 20 42 79 74 65 2d   Byte-2    Byte-
a72c0 33 20 20 20 20 56 61 6c 75 65 0a 2a 2a 20 20 30  3    Value.**  0
a72d0 78 78 78 78 78 78 78 20 20 20 20 20 20 20 20 20  xxxxxxx         
a72e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a72f0 20 20 20 20 20 20 20 20 30 30 30 30 30 30 30 30          00000000
a7300 20 30 30 30 30 30 30 30 30 20 30 78 78 78 78 78   00000000 0xxxxx
a7310 78 78 0a 2a 2a 20 20 31 31 30 79 79 79 79 79 20  xx.**  110yyyyy 
a7320 20 31 30 78 78 78 78 78 78 20 20 20 20 20 20 20   10xxxxxx       
a7330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7340 30 30 30 30 30 30 30 30 20 30 30 30 30 30 79 79  00000000 00000yy
a7350 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 20 20 31  y yyxxxxxx.**  1
a7360 31 31 30 7a 7a 7a 7a 20 20 31 30 79 79 79 79 79  110zzzz  10yyyyy
a7370 79 20 20 31 30 78 78 78 78 78 78 20 20 20 20 20  y  10xxxxxx     
a7380 20 20 20 20 20 20 20 20 30 30 30 30 30 30 30 30          00000000
a7390 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78   zzzzyyyy yyxxxx
a73a0 78 78 0a 2a 2a 20 20 31 31 31 31 30 75 75 75 20  xx.**  11110uuu 
a73b0 20 31 30 75 75 7a 7a 7a 7a 20 20 31 30 79 79 79   10uuzzzz  10yyy
a73c0 79 79 79 20 20 31 30 78 78 78 78 78 78 20 20 20  yyy  10xxxxxx   
a73d0 30 30 30 75 75 75 75 75 20 7a 7a 7a 7a 79 79 79  000uuuuu zzzzyyy
a73e0 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 0a 2a 2a  y yyxxxxxx.**.**
a73f0 0a 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20 55 54 46  .** Notes on UTF
a7400 2d 31 36 3a 20 20 28 77 69 74 68 20 77 77 77 77  -16:  (with wwww
a7410 2b 31 3d 3d 75 75 75 75 75 29 0a 2a 2a 0a 2a 2a  +1==uuuuu).**.**
a7420 20 20 20 20 20 20 57 6f 72 64 2d 30 20 20 20 20        Word-0    
a7430 20 20 20 20 20 20 20 20 20 20 20 57 6f 72 64 2d             Word-
a7440 31 20 20 20 20 20 20 20 20 20 20 56 61 6c 75 65  1          Value
a7450 0a 2a 2a 20 20 31 31 30 31 31 30 77 77 20 77 77  .**  110110ww ww
a7460 7a 7a 7a 7a 79 79 20 20 20 31 31 30 31 31 31 79  zzzzyy   110111y
a7470 79 20 79 79 78 78 78 78 78 78 20 20 20 20 30 30  y yyxxxxxx    00
a7480 30 75 75 75 75 75 20 7a 7a 7a 7a 79 79 79 79 20  0uuuuu zzzzyyyy 
a7490 79 79 78 78 78 78 78 78 0a 2a 2a 20 20 7a 7a 7a  yyxxxxxx.**  zzz
a74a0 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 20 20  zyyyy yyxxxxxx  
a74b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a74c0 20 20 20 20 20 20 30 30 30 30 30 30 30 30 20 7a        00000000 z
a74d0 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 78  zzzyyyy yyxxxxxx
a74e0 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 42 4f 4d 20 6f 72  .**.**.** BOM or
a74f0 20 42 79 74 65 20 4f 72 64 65 72 20 4d 61 72 6b   Byte Order Mark
a7500 3a 0a 2a 2a 20 20 20 20 20 30 78 66 66 20 30 78  :.**     0xff 0x
a7510 66 65 20 20 20 6c 69 74 74 6c 65 2d 65 6e 64 69  fe   little-endi
a7520 61 6e 20 75 74 66 2d 31 36 20 66 6f 6c 6c 6f 77  an utf-16 follow
a7530 73 0a 2a 2a 20 20 20 20 20 30 78 66 65 20 30 78  s.**     0xfe 0x
a7540 66 66 20 20 20 62 69 67 2d 65 6e 64 69 61 6e 20  ff   big-endian 
a7550 75 74 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a 2a  utf-16 follows.*
a7560 2a 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.*/./**********
a7570 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 76 64 62  **** Include vdb
a7580 65 49 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d 69  eInt.h in the mi
a7590 64 64 6c 65 20 6f 66 20 75 74 66 2e 63 20 2a 2a  ddle of utf.c **
a75a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a75b0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
a75c0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
a75d0 76 64 62 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  vdbeInt.h ******
a75e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a75f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7600 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20  ***/./*.** 2003 
a7610 53 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a  September 6.**.*
a7620 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
a7630 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
a7640 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
a7650 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
a7660 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
a7670 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
a7680 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
a7690 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
a76a0 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
a76b0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
a76c0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
a76d0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
a76e0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
a76f0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
a7700 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
a7710 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
a7720 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
a7730 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
a7740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
a7780 69 73 20 69 73 20 74 68 65 20 68 65 61 64 65 72  is is the header
a7790 20 66 69 6c 65 20 66 6f 72 20 69 6e 66 6f 72 6d   file for inform
a77a0 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 70 72  ation that is pr
a77b0 69 76 61 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20  ivate to the.** 
a77c0 56 44 42 45 2e 20 20 54 68 69 73 20 69 6e 66 6f  VDBE.  This info
a77d0 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 74 6f 20  rmation used to 
a77e0 61 6c 6c 20 62 65 20 61 74 20 74 68 65 20 74 6f  all be at the to
a77f0 70 20 6f 66 20 74 68 65 20 73 69 6e 67 6c 65 0a  p of the single.
a7800 2a 2a 20 73 6f 75 72 63 65 20 63 6f 64 65 20 66  ** source code f
a7810 69 6c 65 20 22 76 64 62 65 2e 63 22 2e 20 20 57  ile "vdbe.c".  W
a7820 68 65 6e 20 74 68 61 74 20 66 69 6c 65 20 62 65  hen that file be
a7830 63 61 6d 65 20 74 6f 6f 20 62 69 67 20 28 6f 76  came too big (ov
a7840 65 72 0a 2a 2a 20 36 30 30 30 20 6c 69 6e 65 73  er.** 6000 lines
a7850 20 6c 6f 6e 67 29 20 69 74 20 77 61 73 20 73 70   long) it was sp
a7860 6c 69 74 20 75 70 20 69 6e 74 6f 20 73 65 76 65  lit up into seve
a7870 72 61 6c 20 73 6d 61 6c 6c 65 72 20 66 69 6c 65  ral smaller file
a7880 73 20 61 6e 64 0a 2a 2a 20 74 68 69 73 20 68 65  s and.** this he
a7890 61 64 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ader information
a78a0 20 77 61 73 20 66 61 63 74 6f 72 65 64 20 6f 75   was factored ou
a78b0 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64  t..**.** $Id: vd
a78c0 62 65 49 6e 74 2e 68 2c 76 20 31 2e 31 37 34 20  beInt.h,v 1.174 
a78d0 32 30 30 39 2f 30 36 2f 32 33 20 31 34 3a 31 35  2009/06/23 14:15
a78e0 3a 30 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :04 drh Exp $.*/
a78f0 0a 23 69 66 6e 64 65 66 20 5f 56 44 42 45 49 4e  .#ifndef _VDBEIN
a7900 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 56 44  T_H_.#define _VD
a7910 42 45 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20  BEINT_H_../*.** 
a7920 53 51 4c 20 69 73 20 74 72 61 6e 73 6c 61 74 65  SQL is translate
a7930 64 20 69 6e 74 6f 20 61 20 73 65 71 75 65 6e 63  d into a sequenc
a7940 65 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e  e of instruction
a7950 73 20 74 6f 20 62 65 0a 2a 2a 20 65 78 65 63 75  s to be.** execu
a7960 74 65 64 20 62 79 20 61 20 76 69 72 74 75 61 6c  ted by a virtual
a7970 20 6d 61 63 68 69 6e 65 2e 20 20 45 61 63 68 20   machine.  Each 
a7980 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61  instruction is a
a7990 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66  n instance.** of
a79a0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
a79b0 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70  tructure..*/.typ
a79c0 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65  edef struct Vdbe
a79d0 4f 70 20 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20 42 6f  Op Op;../*.** Bo
a79e0 6f 6c 65 61 6e 20 76 61 6c 75 65 73 0a 2a 2f 0a  olean values.*/.
a79f0 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64  typedef unsigned
a7a00 20 63 68 61 72 20 42 6f 6f 6c 3b 0a 0a 2f 2a 0a   char Bool;../*.
a7a10 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20 61  ** A cursor is a
a7a20 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 61 20   pointer into a 
a7a30 73 69 6e 67 6c 65 20 42 54 72 65 65 20 77 69 74  single BTree wit
a7a40 68 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 66  hin a database f
a7a50 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73  ile..** The curs
a7a60 6f 72 20 63 61 6e 20 73 65 65 6b 20 74 6f 20 61  or can seek to a
a7a70 20 42 54 72 65 65 20 65 6e 74 72 79 20 77 69 74   BTree entry wit
a7a80 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 6b  h a particular k
a7a90 65 79 2c 20 6f 72 0a 2a 2a 20 6c 6f 6f 70 20 6f  ey, or.** loop o
a7aa0 76 65 72 20 61 6c 6c 20 65 6e 74 72 69 65 73 20  ver all entries 
a7ab0 6f 66 20 74 68 65 20 42 74 72 65 65 2e 20 20 59  of the Btree.  Y
a7ac0 6f 75 20 63 61 6e 20 61 6c 73 6f 20 69 6e 73 65  ou can also inse
a7ad0 72 74 20 6e 65 77 20 42 54 72 65 65 0a 2a 2a 20  rt new BTree.** 
a7ae0 65 6e 74 72 69 65 73 20 6f 72 20 72 65 74 72 69  entries or retri
a7af0 65 76 65 20 74 68 65 20 6b 65 79 20 6f 72 20 64  eve the key or d
a7b00 61 74 61 20 66 72 6f 6d 20 74 68 65 20 65 6e 74  ata from the ent
a7b10 72 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73  ry that the curs
a7b20 6f 72 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e 74  or.** is current
a7b30 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a  ly pointing to..
a7b40 2a 2a 20 0a 2a 2a 20 45 76 65 72 79 20 63 75 72  ** .** Every cur
a7b50 73 6f 72 20 74 68 61 74 20 74 68 65 20 76 69 72  sor that the vir
a7b60 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61 73  tual machine has
a7b70 20 6f 70 65 6e 20 69 73 20 72 65 70 72 65 73 65   open is represe
a7b80 6e 74 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e  nted by an.** in
a7b90 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
a7ba0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
a7bb0 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
a7bc0 56 64 62 65 43 75 72 73 6f 72 2e 69 73 54 72 69  VdbeCursor.isTri
a7bd0 67 67 65 72 52 6f 77 20 66 6c 61 67 20 69 73 20  ggerRow flag is 
a7be0 73 65 74 20 69 74 20 6d 65 61 6e 73 20 74 68 61  set it means tha
a7bf0 74 20 74 68 69 73 20 63 75 72 73 6f 72 20 69 73  t this cursor is
a7c00 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 20 73 69 6e  .** really a sin
a7c10 67 6c 65 20 72 6f 77 20 74 68 61 74 20 72 65 70  gle row that rep
a7c20 72 65 73 65 6e 74 73 20 74 68 65 20 4e 45 57 20  resents the NEW 
a7c30 6f 72 20 4f 4c 44 20 70 73 65 75 64 6f 2d 74 61  or OLD pseudo-ta
a7c40 62 6c 65 20 6f 66 0a 2a 2a 20 61 20 72 6f 77 20  ble of.** a row 
a7c50 74 72 69 67 67 65 72 2e 20 20 54 68 65 20 64 61  trigger.  The da
a7c60 74 61 20 66 6f 72 20 74 68 65 20 72 6f 77 20 69  ta for the row i
a7c70 73 20 73 74 6f 72 65 64 20 69 6e 20 56 64 62 65  s stored in Vdbe
a7c80 43 75 72 73 6f 72 2e 70 44 61 74 61 20 61 6e 64  Cursor.pData and
a7c90 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64 20 69 73  .** the rowid is
a7ca0 20 69 6e 20 56 64 62 65 43 75 72 73 6f 72 2e 69   in VdbeCursor.i
a7cb0 4b 65 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56  Key..*/.struct V
a7cc0 64 62 65 43 75 72 73 6f 72 20 7b 0a 20 20 42 74  dbeCursor {.  Bt
a7cd0 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b  Cursor *pCursor;
a7ce0 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f      /* The curso
a7cf0 72 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 74  r structure of t
a7d00 68 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 20 20  he backend */.  
a7d10 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
a7d20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
a7d30 66 20 63 75 72 73 6f 72 20 64 61 74 61 62 61 73  f cursor databas
a7d40 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 28  e in db->aDb[] (
a7d50 6f 72 20 2d 31 29 20 2a 2f 0a 20 20 69 36 34 20  or -1) */.  i64 
a7d60 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20 20  lastRowid;      
a7d70 20 20 2f 2a 20 4c 61 73 74 20 72 6f 77 69 64 20    /* Last rowid 
a7d80 66 72 6f 6d 20 61 20 4e 65 78 74 20 6f 72 20 4e  from a Next or N
a7d90 65 78 74 49 64 78 20 6f 70 65 72 61 74 69 6f 6e  extIdx operation
a7da0 20 2a 2f 0a 20 20 42 6f 6f 6c 20 7a 65 72 6f 65   */.  Bool zeroe
a7db0 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  d;          /* T
a7dc0 72 75 65 20 69 66 20 7a 65 72 6f 65 64 20 6f 75  rue if zeroed ou
a7dd0 74 20 61 6e 64 20 72 65 61 64 79 20 66 6f 72 20  t and ready for 
a7de0 72 65 75 73 65 20 2a 2f 0a 20 20 42 6f 6f 6c 20  reuse */.  Bool 
a7df0 72 6f 77 69 64 49 73 56 61 6c 69 64 3b 20 20 20  rowidIsValid;   
a7e00 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 61 73 74   /* True if last
a7e10 52 6f 77 69 64 20 69 73 20 76 61 6c 69 64 20 2a  Rowid is valid *
a7e20 2f 0a 20 20 42 6f 6f 6c 20 61 74 46 69 72 73 74  /.  Bool atFirst
a7e30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ;         /* Tru
a7e40 65 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f  e if pointing to
a7e50 20 66 69 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a   first entry */.
a7e60 20 20 42 6f 6f 6c 20 75 73 65 52 61 6e 64 6f 6d    Bool useRandom
a7e70 52 6f 77 69 64 3b 20 20 2f 2a 20 47 65 6e 65 72  Rowid;  /* Gener
a7e80 61 74 65 20 6e 65 77 20 72 65 63 6f 72 64 20 6e  ate new record n
a7e90 75 6d 62 65 72 73 20 73 65 6d 69 2d 72 61 6e 64  umbers semi-rand
a7ea0 6f 6d 6c 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20 6e  omly */.  Bool n
a7eb0 75 6c 6c 52 6f 77 3b 20 20 20 20 20 20 20 20 20  ullRow;         
a7ec0 2f 2a 20 54 72 75 65 20 69 66 20 70 6f 69 6e 74  /* True if point
a7ed0 69 6e 67 20 74 6f 20 61 20 72 6f 77 20 77 69 74  ing to a row wit
a7ee0 68 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 20 20 42  h no data */.  B
a7ef0 6f 6f 6c 20 64 65 66 65 72 72 65 64 4d 6f 76 65  ool deferredMove
a7f00 74 6f 3b 20 20 2f 2a 20 41 20 63 61 6c 6c 20 74  to;  /* A call t
a7f10 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f  o sqlite3BtreeMo
a7f20 76 65 74 6f 28 29 20 69 73 20 6e 65 65 64 65 64  veto() is needed
a7f30 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 54 61 62   */.  Bool isTab
a7f40 6c 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  le;         /* T
a7f50 72 75 65 20 69 66 20 61 20 74 61 62 6c 65 20 72  rue if a table r
a7f60 65 71 75 69 72 69 6e 67 20 69 6e 74 65 67 65 72  equiring integer
a7f70 20 6b 65 79 73 20 2a 2f 0a 20 20 42 6f 6f 6c 20   keys */.  Bool 
a7f80 69 73 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20  isIndex;        
a7f90 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20 69   /* True if an i
a7fa0 6e 64 65 78 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ndex containing 
a7fb0 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20 64  keys only - no d
a7fc0 61 74 61 20 2a 2f 0a 20 20 69 36 34 20 6d 6f 76  ata */.  i64 mov
a7fd0 65 74 6f 54 61 72 67 65 74 3b 20 20 20 20 20 2f  etoTarget;     /
a7fe0 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  * Argument to th
a7ff0 65 20 64 65 66 65 72 72 65 64 20 73 71 6c 69 74  e deferred sqlit
a8000 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 20  e3BtreeMoveto() 
a8010 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b  */.  Btree *pBt;
a8020 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65             /* Se
a8030 70 61 72 61 74 65 20 66 69 6c 65 20 68 6f 6c 64  parate file hold
a8040 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61  ing temporary ta
a8050 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 70 73 65  ble */.  int pse
a8060 75 64 6f 54 61 62 6c 65 52 65 67 3b 20 20 20 2f  udoTableReg;   /
a8070 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69  * Register holdi
a8080 6e 67 20 70 73 65 75 64 6f 74 61 62 6c 65 20 63  ng pseudotable c
a8090 6f 6e 74 65 6e 74 2e 20 2a 2f 0a 20 20 4b 65 79  ontent. */.  Key
a80a0 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20  Info *pKeyInfo; 
a80b0 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 74     /* Info about
a80c0 20 69 6e 64 65 78 20 6b 65 79 73 20 6e 65 65 64   index keys need
a80d0 65 64 20 62 79 20 69 6e 64 65 78 20 63 75 72 73  ed by index curs
a80e0 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69  ors */.  int nFi
a80f0 65 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f  eld;           /
a8100 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c  * Number of fiel
a8110 64 73 20 69 6e 20 74 68 65 20 68 65 61 64 65 72  ds in the header
a8120 20 2a 2f 0a 20 20 69 36 34 20 73 65 71 43 6f 75   */.  i64 seqCou
a8130 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53  nt;         /* S
a8140 65 71 75 65 6e 63 65 20 63 6f 75 6e 74 65 72 20  equence counter 
a8150 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  */.  sqlite3_vta
a8160 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43  b_cursor *pVtabC
a8170 75 72 73 6f 72 3b 20 20 2f 2a 20 54 68 65 20 63  ursor;  /* The c
a8180 75 72 73 6f 72 20 66 6f 72 20 61 20 76 69 72 74  ursor for a virt
a8190 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63  ual table */.  c
a81a0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
a81b0 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 20  ule *pModule;   
a81c0 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 66 6f 72 20    /* Module for 
a81d0 63 75 72 73 6f 72 20 70 56 74 61 62 43 75 72 73  cursor pVtabCurs
a81e0 6f 72 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 73 75  or */..  /* Resu
a81f0 6c 74 20 6f 66 20 6c 61 73 74 20 73 71 6c 69 74  lt of last sqlit
a8200 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 20  e3BtreeMoveto() 
a8210 64 6f 6e 65 20 62 79 20 61 6e 20 4f 50 5f 4e 6f  done by an OP_No
a8220 74 45 78 69 73 74 73 20 6f 72 20 0a 20 20 2a 2a  tExists or .  **
a8230 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 6f 70 63   OP_IsUnique opc
a8240 6f 64 65 20 6f 6e 20 74 68 69 73 20 63 75 72 73  ode on this curs
a8250 6f 72 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 65 65  or. */.  int see
a8260 6b 52 65 73 75 6c 74 3b 0a 0a 20 20 2f 2a 20 43  kResult;..  /* C
a8270 61 63 68 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f  ached informatio
a8280 6e 20 61 62 6f 75 74 20 74 68 65 20 68 65 61 64  n about the head
a8290 65 72 20 66 6f 72 20 74 68 65 20 64 61 74 61 20  er for the data 
a82a0 72 65 63 6f 72 64 20 74 68 61 74 20 74 68 65 0a  record that the.
a82b0 20 20 2a 2a 20 63 75 72 73 6f 72 20 69 73 20 63    ** cursor is c
a82c0 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e  urrently pointin
a82d0 67 20 74 6f 2e 20 20 4f 6e 6c 79 20 76 61 6c 69  g to.  Only vali
a82e0 64 20 69 66 20 63 61 63 68 65 53 74 61 74 75 73  d if cacheStatus
a82f0 20 6d 61 74 63 68 65 73 0a 20 20 2a 2a 20 56 64   matches.  ** Vd
a8300 62 65 2e 63 61 63 68 65 43 74 72 2e 20 20 56 64  be.cacheCtr.  Vd
a8310 62 65 2e 63 61 63 68 65 43 74 72 20 77 69 6c 6c  be.cacheCtr will
a8320 20 6e 65 76 65 72 20 74 61 6b 65 20 6f 6e 20 74   never take on t
a8330 68 65 20 76 61 6c 75 65 20 6f 66 0a 20 20 2a 2a  he value of.  **
a8340 20 43 41 43 48 45 5f 53 54 41 4c 45 20 61 6e 64   CACHE_STALE and
a8350 20 73 6f 20 73 65 74 74 69 6e 67 20 63 61 63 68   so setting cach
a8360 65 53 74 61 74 75 73 3d 43 41 43 48 45 5f 53 54  eStatus=CACHE_ST
a8370 41 4c 45 20 67 75 61 72 61 6e 74 65 65 73 20 74  ALE guarantees t
a8380 68 61 74 0a 20 20 2a 2a 20 74 68 65 20 63 61 63  hat.  ** the cac
a8390 68 65 20 69 73 20 6f 75 74 20 6f 66 20 64 61 74  he is out of dat
a83a0 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61 52 6f  e..  **.  ** aRo
a83b0 77 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f  w might point to
a83c0 20 28 65 70 68 65 6d 65 72 61 6c 29 20 64 61 74   (ephemeral) dat
a83d0 61 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e  a for the curren
a83e0 74 20 72 6f 77 2c 20 6f 72 20 69 74 20 6d 69 67  t row, or it mig
a83f0 68 74 0a 20 20 2a 2a 20 62 65 20 4e 55 4c 4c 2e  ht.  ** be NULL.
a8400 0a 20 20 2a 2f 0a 20 20 75 33 32 20 63 61 63 68  .  */.  u32 cach
a8410 65 53 74 61 74 75 73 3b 20 20 20 20 20 20 2f 2a  eStatus;      /*
a8420 20 43 61 63 68 65 20 69 73 20 76 61 6c 69 64 20   Cache is valid 
a8430 69 66 20 74 68 69 73 20 6d 61 74 63 68 65 73 20  if this matches 
a8440 56 64 62 65 2e 63 61 63 68 65 43 74 72 20 2a 2f  Vdbe.cacheCtr */
a8450 0a 20 20 69 6e 74 20 70 61 79 6c 6f 61 64 53 69  .  int payloadSi
a8460 7a 65 3b 20 20 20 20 20 20 2f 2a 20 54 6f 74 61  ze;      /* Tota
a8470 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  l number of byte
a8480 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20  s in the record 
a8490 2a 2f 0a 20 20 75 33 32 20 2a 61 54 79 70 65 3b  */.  u32 *aType;
a84a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 79             /* Ty
a84b0 70 65 20 76 61 6c 75 65 73 20 66 6f 72 20 61 6c  pe values for al
a84c0 6c 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65  l entries in the
a84d0 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 33 32   record */.  u32
a84e0 20 2a 61 4f 66 66 73 65 74 3b 20 20 20 20 20 20   *aOffset;      
a84f0 20 20 20 2f 2a 20 43 61 63 68 65 64 20 6f 66 66     /* Cached off
a8500 73 65 74 73 20 74 6f 20 74 68 65 20 73 74 61 72  sets to the star
a8510 74 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e  t of each column
a8520 73 20 64 61 74 61 20 2a 2f 0a 20 20 75 38 20 2a  s data */.  u8 *
a8530 61 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20 20  aRow;           
a8540 20 20 2f 2a 20 44 61 74 61 20 66 6f 72 20 74 68    /* Data for th
a8550 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2c 20 69  e current row, i
a8560 66 20 61 6c 6c 20 6f 6e 20 6f 6e 65 20 70 61 67  f all on one pag
a8570 65 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20  e */.};.typedef 
a8580 73 74 72 75 63 74 20 56 64 62 65 43 75 72 73 6f  struct VdbeCurso
a8590 72 20 56 64 62 65 43 75 72 73 6f 72 3b 0a 0a 2f  r VdbeCursor;../
a85a0 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 75 62 2d  *.** When a sub-
a85b0 70 72 6f 67 72 61 6d 20 69 73 20 65 78 65 63 75  program is execu
a85c0 74 65 64 20 28 4f 50 5f 50 72 6f 67 72 61 6d 29  ted (OP_Program)
a85d0 2c 20 61 20 73 74 72 75 63 74 75 72 65 20 6f 66  , a structure of
a85e0 20 74 68 69 73 20 74 79 70 65 0a 2a 2a 20 69 73   this type.** is
a85f0 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 73 74   allocated to st
a8600 6f 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ore the current 
a8610 76 61 6c 75 65 20 6f 66 20 74 68 65 20 70 72 6f  value of the pro
a8620 67 72 61 6d 20 63 6f 75 6e 74 65 72 2c 20 61 73  gram counter, as
a8630 0a 2a 2a 20 77 65 6c 6c 20 61 73 20 74 68 65 20  .** well as the 
a8640 63 75 72 72 65 6e 74 20 6d 65 6d 6f 72 79 20 63  current memory c
a8650 65 6c 6c 20 61 72 72 61 79 20 61 6e 64 20 76 61  ell array and va
a8660 72 69 6f 75 73 20 6f 74 68 65 72 20 66 72 61 6d  rious other fram
a8670 65 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 76 61  e specific.** va
a8680 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74  lues stored in t
a8690 68 65 20 56 64 62 65 20 73 74 72 75 63 74 2e 20  he Vdbe struct. 
a86a0 57 68 65 6e 20 74 68 65 20 73 75 62 2d 70 72 6f  When the sub-pro
a86b0 67 72 61 6d 20 69 73 20 66 69 6e 69 73 68 65 64  gram is finished
a86c0 2c 20 0a 2a 2a 20 74 68 65 73 65 20 76 61 6c 75  , .** these valu
a86d0 65 73 20 61 72 65 20 63 6f 70 69 65 64 20 62 61  es are copied ba
a86e0 63 6b 20 74 6f 20 74 68 65 20 56 64 62 65 20 66  ck to the Vdbe f
a86f0 72 6f 6d 20 74 68 65 20 56 64 62 65 46 72 61 6d  rom the VdbeFram
a8700 65 20 73 74 72 75 63 74 75 72 65 2c 0a 2a 2a 20  e structure,.** 
a8710 72 65 73 74 6f 72 69 6e 67 20 74 68 65 20 73 74  restoring the st
a8720 61 74 65 20 6f 66 20 74 68 65 20 56 4d 20 74 6f  ate of the VM to
a8730 20 61 73 20 69 74 20 77 61 73 20 62 65 66 6f 72   as it was befor
a8740 65 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61  e the sub-progra
a8750 6d 0a 2a 2a 20 62 65 67 61 6e 20 65 78 65 63 75  m.** began execu
a8760 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 46 72 61 6d  ting..**.** Fram
a8770 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e  es are stored in
a8780 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68   a linked list h
a8790 65 61 64 65 64 20 61 74 20 56 64 62 65 2e 70 50  eaded at Vdbe.pP
a87a0 61 72 65 6e 74 2e 20 56 64 62 65 2e 70 50 61 72  arent. Vdbe.pPar
a87b0 65 6e 74 0a 2a 2a 20 69 73 20 74 68 65 20 70 61  ent.** is the pa
a87c0 72 65 6e 74 20 6f 66 20 74 68 65 20 63 75 72 72  rent of the curr
a87d0 65 6e 74 20 66 72 61 6d 65 2c 20 6f 72 20 7a 65  ent frame, or ze
a87e0 72 6f 20 69 66 20 74 68 65 20 63 75 72 72 65 6e  ro if the curren
a87f0 74 20 66 72 61 6d 65 0a 2a 2a 20 69 73 20 74 68  t frame.** is th
a8800 65 20 6d 61 69 6e 20 56 64 62 65 20 70 72 6f 67  e main Vdbe prog
a8810 72 61 6d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ram..*/.typedef 
a8820 73 74 72 75 63 74 20 56 64 62 65 46 72 61 6d 65  struct VdbeFrame
a8830 20 56 64 62 65 46 72 61 6d 65 3b 0a 73 74 72 75   VdbeFrame;.stru
a8840 63 74 20 56 64 62 65 46 72 61 6d 65 20 7b 0a 20  ct VdbeFrame {. 
a8850 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20   Vdbe *v;       
a8860 20 20 20 20 20 20 20 20 20 2f 2a 20 56 4d 20 74           /* VM t
a8870 68 69 73 20 66 72 61 6d 65 20 62 65 6c 6f 6e 67  his frame belong
a8880 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 70 63  s to */.  int pc
a8890 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a88a0 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 43 6f 75    /* Program Cou
a88b0 6e 74 65 72 20 2a 2f 0a 20 20 4f 70 20 2a 61 4f  nter */.  Op *aO
a88c0 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
a88d0 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 69 6e 73    /* Program ins
a88e0 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 69  tructions */.  i
a88f0 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20 20 20 20  nt nOp;         
a8900 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
a8910 66 20 61 4f 70 20 61 72 72 61 79 20 2a 2f 0a 20  f aOp array */. 
a8920 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20   Mem *aMem;     
a8930 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61           /* Arra
a8940 79 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  y of memory cell
a8950 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b  s */.  int nMem;
a8960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
a8970 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
a8980 69 65 73 20 69 6e 20 61 4d 65 6d 20 2a 2f 0a 20  ies in aMem */. 
a8990 20 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 70   VdbeCursor **ap
a89a0 43 73 72 3b 20 20 20 20 20 2f 2a 20 45 6c 65 6d  Csr;     /* Elem
a89b0 65 6e 74 20 6f 66 20 56 64 62 65 20 63 75 72 73  ent of Vdbe curs
a89c0 6f 72 73 20 2a 2f 0a 20 20 75 31 36 20 6e 43 75  ors */.  u16 nCu
a89d0 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20  rsor;           
a89e0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
a89f0 74 72 69 65 73 20 69 6e 20 61 70 43 73 72 20 2a  tries in apCsr *
a8a00 2f 0a 20 20 76 6f 69 64 20 2a 74 6f 6b 65 6e 3b  /.  void *token;
a8a10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
a8a20 6f 70 79 20 6f 66 20 53 75 62 50 72 6f 67 72 61  opy of SubProgra
a8a30 6d 2e 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74  m.token */.  int
a8a40 20 6e 43 68 69 6c 64 4d 65 6d 3b 20 20 20 20 20   nChildMem;     
a8a50 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
a8a60 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66  f memory cells f
a8a70 6f 72 20 63 68 69 6c 64 20 66 72 61 6d 65 20 2a  or child frame *
a8a80 2f 0a 20 20 69 6e 74 20 6e 43 68 69 6c 64 43 73  /.  int nChildCs
a8a90 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  r;          /* N
a8aa0 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73  umber of cursors
a8ab0 20 66 6f 72 20 63 68 69 6c 64 20 66 72 61 6d 65   for child frame
a8ac0 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52 6f   */.  i64 lastRo
a8ad0 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  wid;          /*
a8ae0 20 4c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77   Last insert row
a8af0 69 64 20 28 73 71 6c 69 74 65 33 2e 6c 61 73 74  id (sqlite3.last
a8b00 52 6f 77 69 64 29 20 2a 2f 0a 20 20 69 6e 74 20  Rowid) */.  int 
a8b10 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20  nChange;        
a8b20 20 20 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74      /* Statement
a8b30 20 63 68 61 6e 67 65 73 20 28 56 64 62 65 2e 6e   changes (Vdbe.n
a8b40 43 68 61 6e 67 65 73 29 20 20 20 20 20 2a 2f 0a  Changes)     */.
a8b50 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 50 61    VdbeFrame *pPa
a8b60 72 65 6e 74 3b 20 20 20 20 20 2f 2a 20 50 61 72  rent;     /* Par
a8b70 65 6e 74 20 6f 66 20 74 68 69 73 20 66 72 61 6d  ent of this fram
a8b80 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65  e */.};..#define
a8b90 20 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 70 29   VdbeFrameMem(p)
a8ba0 20 28 28 4d 65 6d 20 2a 29 26 28 28 75 38 20 2a   ((Mem *)&((u8 *
a8bb0 29 70 29 5b 52 4f 55 4e 44 38 28 73 69 7a 65 6f  )p)[ROUND8(sizeo
a8bc0 66 28 56 64 62 65 46 72 61 6d 65 29 29 5d 29 0a  f(VdbeFrame))]).
a8bd0 0a 2f 2a 0a 2a 2a 20 41 20 76 61 6c 75 65 20 66  ./*.** A value f
a8be0 6f 72 20 56 64 62 65 43 75 72 73 6f 72 2e 63 61  or VdbeCursor.ca
a8bf0 63 68 65 56 61 6c 69 64 20 74 68 61 74 20 6d 65  cheValid that me
a8c00 61 6e 73 20 74 68 65 20 63 61 63 68 65 20 69 73  ans the cache is
a8c10 20 61 6c 77 61 79 73 20 69 6e 76 61 6c 69 64 2e   always invalid.
a8c20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 41 43 48  .*/.#define CACH
a8c30 45 5f 53 54 41 4c 45 20 30 0a 0a 2f 2a 0a 2a 2a  E_STALE 0../*.**
a8c40 20 49 6e 74 65 72 6e 61 6c 6c 79 2c 20 74 68 65   Internally, the
a8c50 20 76 64 62 65 20 6d 61 6e 69 70 75 6c 61 74 65   vdbe manipulate
a8c60 73 20 6e 65 61 72 6c 79 20 61 6c 6c 20 53 51 4c  s nearly all SQL
a8c70 20 76 61 6c 75 65 73 20 61 73 20 4d 65 6d 0a 2a   values as Mem.*
a8c80 2a 20 73 74 72 75 63 74 75 72 65 73 2e 20 45 61  * structures. Ea
a8c90 63 68 20 4d 65 6d 20 73 74 72 75 63 74 20 6d 61  ch Mem struct ma
a8ca0 79 20 63 61 63 68 65 20 6d 75 6c 74 69 70 6c 65  y cache multiple
a8cb0 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73   representations
a8cc0 20 28 73 74 72 69 6e 67 2c 0a 2a 2a 20 69 6e 74   (string,.** int
a8cd0 65 67 65 72 20 65 74 63 2e 29 20 6f 66 20 74 68  eger etc.) of th
a8ce0 65 20 73 61 6d 65 20 76 61 6c 75 65 2e 20 20 41  e same value.  A
a8cf0 20 76 61 6c 75 65 20 28 61 6e 64 20 74 68 65 72   value (and ther
a8d00 65 66 6f 72 65 20 4d 65 6d 20 73 74 72 75 63 74  efore Mem struct
a8d10 75 72 65 29 0a 2a 2a 20 68 61 73 20 74 68 65 20  ure).** has the 
a8d20 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f 70 65 72  following proper
a8d30 74 69 65 73 3a 0a 2a 2a 0a 2a 2a 20 45 61 63 68  ties:.**.** Each
a8d40 20 76 61 6c 75 65 20 68 61 73 20 61 20 6d 61 6e   value has a man
a8d50 69 66 65 73 74 20 74 79 70 65 2e 20 54 68 65 20  ifest type. The 
a8d60 6d 61 6e 69 66 65 73 74 20 74 79 70 65 20 6f 66  manifest type of
a8d70 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65   the value store
a8d80 64 0a 2a 2a 20 69 6e 20 61 20 4d 65 6d 20 73 74  d.** in a Mem st
a8d90 72 75 63 74 20 69 73 20 72 65 74 75 72 6e 65 64  ruct is returned
a8da0 20 62 79 20 74 68 65 20 4d 65 6d 54 79 70 65 28   by the MemType(
a8db0 4d 65 6d 2a 29 20 6d 61 63 72 6f 2e 20 54 68 65  Mem*) macro. The
a8dc0 20 74 79 70 65 20 69 73 0a 2a 2a 20 6f 6e 65 20   type is.** one 
a8dd0 6f 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20  of SQLITE_NULL, 
a8de0 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 2c 20  SQLITE_INTEGER, 
a8df0 53 51 4c 49 54 45 5f 52 45 41 4c 2c 20 53 51 4c  SQLITE_REAL, SQL
a8e00 49 54 45 5f 54 45 58 54 20 6f 72 0a 2a 2a 20 53  ITE_TEXT or.** S
a8e10 51 4c 49 54 45 5f 42 4c 4f 42 2e 0a 2a 2f 0a 73  QLITE_BLOB..*/.s
a8e20 74 72 75 63 74 20 4d 65 6d 20 7b 0a 20 20 75 6e  truct Mem {.  un
a8e30 69 6f 6e 20 7b 0a 20 20 20 20 69 36 34 20 69 3b  ion {.    i64 i;
a8e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a8e50 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 20   Integer value. 
a8e60 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f  */.    int nZero
a8e70 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73  ;          /* Us
a8e80 65 64 20 77 68 65 6e 20 62 69 74 20 4d 45 4d 5f  ed when bit MEM_
a8e90 5a 65 72 6f 20 69 73 20 73 65 74 20 69 6e 20 66  Zero is set in f
a8ea0 6c 61 67 73 20 2a 2f 0a 20 20 20 20 46 75 6e 63  lags */.    Func
a8eb0 44 65 66 20 2a 70 44 65 66 3b 20 20 20 20 20 20  Def *pDef;      
a8ec0 2f 2a 20 55 73 65 64 20 6f 6e 6c 79 20 77 68 65  /* Used only whe
a8ed0 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 41 67 67  n flags==MEM_Agg
a8ee0 20 2a 2f 0a 20 20 20 20 52 6f 77 53 65 74 20 2a   */.    RowSet *
a8ef0 70 52 6f 77 53 65 74 3b 20 20 20 20 2f 2a 20 55  pRowSet;    /* U
a8f00 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 66 6c  sed only when fl
a8f10 61 67 73 3d 3d 4d 45 4d 5f 52 6f 77 53 65 74 20  ags==MEM_RowSet 
a8f20 2a 2f 0a 20 20 20 20 56 64 62 65 46 72 61 6d 65  */.    VdbeFrame
a8f30 20 2a 70 46 72 61 6d 65 3b 20 20 2f 2a 20 55 73   *pFrame;  /* Us
a8f40 65 64 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d  ed when flags==M
a8f50 45 4d 5f 46 72 61 6d 65 20 2a 2f 0a 20 20 7d 20  EM_Frame */.  } 
a8f60 75 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 20 20  u;.  double r;  
a8f70 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61 6c           /* Real
a8f80 20 76 61 6c 75 65 20 2a 2f 0a 20 20 73 71 6c 69   value */.  sqli
a8f90 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
a8fa0 2f 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 65  /* The associate
a8fb0 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  d database conne
a8fc0 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 20  ction */.  char 
a8fd0 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  *z;            /
a8fe0 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42  * String or BLOB
a8ff0 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20   value */.  int 
a9000 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
a9010 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61  /* Number of cha
a9020 72 61 63 74 65 72 73 20 69 6e 20 73 74 72 69 6e  racters in strin
a9030 67 20 76 61 6c 75 65 2c 20 65 78 63 6c 75 64 69  g value, excludi
a9040 6e 67 20 27 5c 30 27 20 2a 2f 0a 20 20 75 31 36  ng '\0' */.  u16
a9050 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
a9060 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61   /* Some combina
a9070 74 69 6f 6e 20 6f 66 20 4d 45 4d 5f 4e 75 6c 6c  tion of MEM_Null
a9080 2c 20 4d 45 4d 5f 53 74 72 2c 20 4d 45 4d 5f 44  , MEM_Str, MEM_D
a9090 79 6e 2c 20 65 74 63 2e 20 2a 2f 0a 20 20 75 38  yn, etc. */.  u8
a90a0 20 20 74 79 70 65 3b 20 20 20 20 20 20 20 20 20    type;         
a90b0 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 51 4c 49    /* One of SQLI
a90c0 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f  TE_NULL, SQLITE_
a90d0 54 45 58 54 2c 20 53 51 4c 49 54 45 5f 49 4e 54  TEXT, SQLITE_INT
a90e0 45 47 45 52 2c 20 65 74 63 20 2a 2f 0a 20 20 75  EGER, etc */.  u
a90f0 38 20 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20  8  enc;         
a9100 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54 46     /* SQLITE_UTF
a9110 38 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42  8, SQLITE_UTF16B
a9120 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  E, SQLITE_UTF16L
a9130 45 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44  E */.  void (*xD
a9140 65 6c 29 28 76 6f 69 64 20 2a 29 3b 20 20 2f 2a  el)(void *);  /*
a9150 20 49 66 20 6e 6f 74 20 6e 75 6c 6c 2c 20 63 61   If not null, ca
a9160 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ll this function
a9170 20 74 6f 20 64 65 6c 65 74 65 20 4d 65 6d 2e 7a   to delete Mem.z
a9180 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c   */.  char *zMal
a9190 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 44 79 6e  loc;      /* Dyn
a91a0 61 6d 69 63 20 62 75 66 66 65 72 20 61 6c 6c 6f  amic buffer allo
a91b0 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33  cated by sqlite3
a91c0 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 7d 3b 0a  _malloc() */.};.
a91d0 0a 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20  ./* One or more 
a91e0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
a91f0 20 66 6c 61 67 73 20 61 72 65 20 73 65 74 20 74   flags are set t
a9200 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 76  o indicate the v
a9210 61 6c 69 64 4f 4b 0a 2a 2a 20 72 65 70 72 65 73  alidOK.** repres
a9220 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65  entations of the
a9230 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e   value stored in
a9240 20 74 68 65 20 4d 65 6d 20 73 74 72 75 63 74 2e   the Mem struct.
a9250 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45  .**.** If the ME
a9260 4d 5f 4e 75 6c 6c 20 66 6c 61 67 20 69 73 20 73  M_Null flag is s
a9270 65 74 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c  et, then the val
a9280 75 65 20 69 73 20 61 6e 20 53 51 4c 20 4e 55 4c  ue is an SQL NUL
a9290 4c 20 76 61 6c 75 65 2e 0a 2a 2a 20 4e 6f 20 6f  L value..** No o
a92a0 74 68 65 72 20 66 6c 61 67 73 20 6d 61 79 20 62  ther flags may b
a92b0 65 20 73 65 74 20 69 6e 20 74 68 69 73 20 63 61  e set in this ca
a92c0 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  se..**.** If the
a92d0 20 4d 45 4d 5f 53 74 72 20 66 6c 61 67 20 69 73   MEM_Str flag is
a92e0 20 73 65 74 20 74 68 65 6e 20 4d 65 6d 2e 7a 20   set then Mem.z 
a92f0 70 6f 69 6e 74 73 20 61 74 20 61 20 73 74 72 69  points at a stri
a9300 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ng representatio
a9310 6e 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 74 68  n..** Usually th
a9320 69 73 20 69 73 20 65 6e 63 6f 64 65 64 20 69 6e  is is encoded in
a9330 20 74 68 65 20 73 61 6d 65 20 75 6e 69 63 6f 64   the same unicod
a9340 65 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 74 68  e encoding as th
a9350 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62 61  e main.** databa
a9360 73 65 20 28 73 65 65 20 62 65 6c 6f 77 20 66 6f  se (see below fo
a9370 72 20 65 78 63 65 70 74 69 6f 6e 73 29 2e 20 49  r exceptions). I
a9380 66 20 74 68 65 20 4d 45 4d 5f 54 65 72 6d 20 66  f the MEM_Term f
a9390 6c 61 67 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 73  lag is also.** s
a93a0 65 74 2c 20 74 68 65 6e 20 74 68 65 20 73 74 72  et, then the str
a93b0 69 6e 67 20 69 73 20 6e 75 6c 20 74 65 72 6d 69  ing is nul termi
a93c0 6e 61 74 65 64 2e 20 54 68 65 20 4d 45 4d 5f 49  nated. The MEM_I
a93d0 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20  nt and MEM_Real 
a93e0 0a 2a 2a 20 66 6c 61 67 73 20 6d 61 79 20 63 6f  .** flags may co
a93f0 65 78 69 73 74 20 77 69 74 68 20 74 68 65 20 4d  exist with the M
a9400 45 4d 5f 53 74 72 20 66 6c 61 67 2e 0a 2a 2a 0a  EM_Str flag..**.
a9410 2a 2a 20 4d 75 6c 74 69 70 6c 65 20 6f 66 20 74  ** Multiple of t
a9420 68 65 73 65 20 76 61 6c 75 65 73 20 63 61 6e 20  hese values can 
a9430 61 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e 66 6c  appear in Mem.fl
a9440 61 67 73 2e 20 20 42 75 74 20 6f 6e 6c 79 20 6f  ags.  But only o
a9450 6e 65 0a 2a 2a 20 61 74 20 61 20 74 69 6d 65 20  ne.** at a time 
a9460 63 61 6e 20 61 70 70 65 61 72 20 69 6e 20 4d 65  can appear in Me
a9470 6d 2e 74 79 70 65 2e 0a 2a 2f 0a 23 64 65 66 69  m.type..*/.#defi
a9480 6e 65 20 4d 45 4d 5f 4e 75 6c 6c 20 20 20 20 20  ne MEM_Null     
a9490 20 30 78 30 30 30 31 20 20 20 2f 2a 20 56 61 6c   0x0001   /* Val
a94a0 75 65 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64  ue is NULL */.#d
a94b0 65 66 69 6e 65 20 4d 45 4d 5f 53 74 72 20 20 20  efine MEM_Str   
a94c0 20 20 20 20 30 78 30 30 30 32 20 20 20 2f 2a 20      0x0002   /* 
a94d0 56 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e  Value is a strin
a94e0 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d  g */.#define MEM
a94f0 5f 49 6e 74 20 20 20 20 20 20 20 30 78 30 30 30  _Int       0x000
a9500 34 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20  4   /* Value is 
a9510 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 23 64  an integer */.#d
a9520 65 66 69 6e 65 20 4d 45 4d 5f 52 65 61 6c 20 20  efine MEM_Real  
a9530 20 20 20 20 30 78 30 30 30 38 20 20 20 2f 2a 20      0x0008   /* 
a9540 56 61 6c 75 65 20 69 73 20 61 20 72 65 61 6c 20  Value is a real 
a9550 6e 75 6d 62 65 72 20 2a 2f 0a 23 64 65 66 69 6e  number */.#defin
a9560 65 20 4d 45 4d 5f 42 6c 6f 62 20 20 20 20 20 20  e MEM_Blob      
a9570 30 78 30 30 31 30 20 20 20 2f 2a 20 56 61 6c 75  0x0010   /* Valu
a9580 65 20 69 73 20 61 20 42 4c 4f 42 20 2a 2f 0a 23  e is a BLOB */.#
a9590 64 65 66 69 6e 65 20 4d 45 4d 5f 52 6f 77 53 65  define MEM_RowSe
a95a0 74 20 20 20 20 30 78 30 30 32 30 20 20 20 2f 2a  t    0x0020   /*
a95b0 20 56 61 6c 75 65 20 69 73 20 61 20 52 6f 77 53   Value is a RowS
a95c0 65 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64 65  et object */.#de
a95d0 66 69 6e 65 20 4d 45 4d 5f 46 72 61 6d 65 20 20  fine MEM_Frame  
a95e0 20 20 20 30 78 30 30 34 30 20 20 20 2f 2a 20 56     0x0040   /* V
a95f0 61 6c 75 65 20 69 73 20 61 20 56 64 62 65 46 72  alue is a VdbeFr
a9600 61 6d 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64  ame object */.#d
a9610 65 66 69 6e 65 20 4d 45 4d 5f 54 79 70 65 4d 61  efine MEM_TypeMa
a9620 73 6b 20 20 30 78 30 30 66 66 20 20 20 2f 2a 20  sk  0x00ff   /* 
a9630 4d 61 73 6b 20 6f 66 20 74 79 70 65 20 62 69 74  Mask of type bit
a9640 73 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 65 76 65  s */../* Wheneve
a9650 72 20 4d 65 6d 20 63 6f 6e 74 61 69 6e 73 20 61  r Mem contains a
a9660 20 76 61 6c 69 64 20 73 74 72 69 6e 67 20 6f 72   valid string or
a9670 20 62 6c 6f 62 20 72 65 70 72 65 73 65 6e 74 61   blob representa
a9680 74 69 6f 6e 2c 20 6f 6e 65 20 6f 66 0a 2a 2a 20  tion, one of.** 
a9690 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c  the following fl
a96a0 61 67 73 20 6d 75 73 74 20 62 65 20 73 65 74 20  ags must be set 
a96b0 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  to determine the
a96c0 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65   memory manageme
a96d0 6e 74 0a 2a 2a 20 70 6f 6c 69 63 79 20 66 6f 72  nt.** policy for
a96e0 20 4d 65 6d 2e 7a 2e 20 20 54 68 65 20 4d 45 4d   Mem.z.  The MEM
a96f0 5f 54 65 72 6d 20 66 6c 61 67 20 74 65 6c 6c 73  _Term flag tells
a9700 20 75 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e   us whether or n
a9710 6f 74 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67  ot the.** string
a9720 20 69 73 20 5c 30 30 30 20 6f 72 20 5c 75 30 30   is \000 or \u00
a9730 30 30 20 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2f  00 terminated.*/
a9740 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 54 65 72  .#define MEM_Ter
a9750 6d 20 20 20 20 20 20 30 78 30 32 30 30 20 20 20  m      0x0200   
a9760 2f 2a 20 53 74 72 69 6e 67 20 72 65 70 20 69 73  /* String rep is
a9770 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 20   nul terminated 
a9780 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 44  */.#define MEM_D
a9790 79 6e 20 20 20 20 20 20 20 30 78 30 34 30 30 20  yn       0x0400 
a97a0 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 63 61 6c    /* Need to cal
a97b0 6c 20 73 71 6c 69 74 65 46 72 65 65 28 29 20 6f  l sqliteFree() o
a97c0 6e 20 4d 65 6d 2e 7a 20 2a 2f 0a 23 64 65 66 69  n Mem.z */.#defi
a97d0 6e 65 20 4d 45 4d 5f 53 74 61 74 69 63 20 20 20  ne MEM_Static   
a97e0 20 30 78 30 38 30 30 20 20 20 2f 2a 20 4d 65 6d   0x0800   /* Mem
a97f0 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73  .z points to a s
a9800 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f 0a  tatic string */.
a9810 23 64 65 66 69 6e 65 20 4d 45 4d 5f 45 70 68 65  #define MEM_Ephe
a9820 6d 20 20 20 20 20 30 78 31 30 30 30 20 20 20 2f  m     0x1000   /
a9830 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74  * Mem.z points t
a9840 6f 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 73  o an ephemeral s
a9850 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65  tring */.#define
a9860 20 4d 45 4d 5f 41 67 67 20 20 20 20 20 20 20 30   MEM_Agg       0
a9870 78 32 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a  x2000   /* Mem.z
a9880 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 67   points to an ag
a9890 67 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65  g function conte
a98a0 78 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45  xt */.#define ME
a98b0 4d 5f 5a 65 72 6f 20 20 20 20 20 20 30 78 34 30  M_Zero      0x40
a98c0 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 69 20 63 6f  00   /* Mem.i co
a98d0 6e 74 61 69 6e 73 20 63 6f 75 6e 74 20 6f 66 20  ntains count of 
a98e0 30 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 62  0s appended to b
a98f0 6c 6f 62 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53  lob */..#ifdef S
a9900 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42  QLITE_OMIT_INCRB
a9910 4c 4f 42 0a 20 20 23 75 6e 64 65 66 20 4d 45 4d  LOB.  #undef MEM
a9920 5f 5a 65 72 6f 0a 20 20 23 64 65 66 69 6e 65 20  _Zero.  #define 
a9930 4d 45 4d 5f 5a 65 72 6f 20 30 78 30 30 30 30 0a  MEM_Zero 0x0000.
a9940 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43  #endif.../*.** C
a9950 6c 65 61 72 20 61 6e 79 20 65 78 69 73 74 69 6e  lear any existin
a9960 67 20 74 79 70 65 20 66 6c 61 67 73 20 66 72 6f  g type flags fro
a9970 6d 20 61 20 4d 65 6d 20 61 6e 64 20 72 65 70 6c  m a Mem and repl
a9980 61 63 65 20 74 68 65 6d 20 77 69 74 68 20 66 0a  ace them with f.
a9990 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 65 6d 53 65  */.#define MemSe
a99a0 74 54 79 70 65 46 6c 61 67 28 70 2c 20 66 29 20  tTypeFlag(p, f) 
a99b0 5c 0a 20 20 20 28 28 70 29 2d 3e 66 6c 61 67 73  \.   ((p)->flags
a99c0 20 3d 20 28 28 70 29 2d 3e 66 6c 61 67 73 26 7e   = ((p)->flags&~
a99d0 28 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 7c 4d 45  (MEM_TypeMask|ME
a99e0 4d 5f 5a 65 72 6f 29 29 7c 66 29 0a 0a 0a 2f 2a  M_Zero))|f).../*
a99f0 20 41 20 56 64 62 65 46 75 6e 63 20 69 73 20 6a   A VdbeFunc is j
a9a00 75 73 74 20 61 20 46 75 6e 63 44 65 66 20 28 64  ust a FuncDef (d
a9a10 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65  efined in sqlite
a9a20 49 6e 74 2e 68 29 20 74 68 61 74 20 63 6f 6e 74  Int.h) that cont
a9a30 61 69 6e 73 0a 2a 2a 20 61 64 64 69 74 69 6f 6e  ains.** addition
a9a40 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  al information a
a9a50 62 6f 75 74 20 61 75 78 69 6c 69 61 72 79 20 69  bout auxiliary i
a9a60 6e 66 6f 72 6d 61 74 69 6f 6e 20 62 6f 75 6e 64  nformation bound
a9a70 20 74 6f 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a   to arguments.**
a9a80 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
a9a90 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20  .  This is used 
a9aa0 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
a9ab0 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78   sqlite3_get_aux
a9ac0 64 61 74 61 28 29 0a 2a 2a 20 61 6e 64 20 73 71  data().** and sq
a9ad0 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74  lite3_set_auxdat
a9ae0 61 28 29 20 41 50 49 73 2e 20 20 54 68 65 20 22  a() APIs.  The "
a9af0 61 75 78 64 61 74 61 22 20 69 73 20 73 6f 6d 65  auxdata" is some
a9b00 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 0a   auxiliary data.
a9b10 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 61  ** that can be a
a9b20 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
a9b30 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d 65   constant argume
a9b40 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e  nt to a function
a9b50 2e 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c 6f 77  .  This.** allow
a9b60 73 20 66 75 6e 63 74 69 6f 6e 73 20 73 75 63 68  s functions such
a9b70 20 61 73 20 22 72 65 67 65 78 70 22 20 74 6f 20   as "regexp" to 
a9b80 63 6f 6d 70 69 6c 65 20 74 68 65 69 72 20 63 6f  compile their co
a9b90 6e 73 74 61 6e 74 20 72 65 67 75 6c 61 72 0a 2a  nstant regular.*
a9ba0 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 61 72 67  * expression arg
a9bb0 75 6d 65 6e 74 20 6f 6e 63 65 20 61 6e 64 20 72  ument once and r
a9bc0 65 75 73 65 64 20 74 68 65 20 63 6f 6d 70 69 6c  eused the compil
a9bd0 65 64 20 63 6f 64 65 20 66 6f 72 20 6d 75 6c 74  ed code for mult
a9be0 69 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74 69  iple.** invocati
a9bf0 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56  ons..*/.struct V
a9c00 64 62 65 46 75 6e 63 20 7b 0a 20 20 46 75 6e 63  dbeFunc {.  Func
a9c10 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20  Def *pFunc;     
a9c20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
a9c30 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74   definition of t
a9c40 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  he function */. 
a9c50 20 69 6e 74 20 6e 41 75 78 3b 20 20 20 20 20 20   int nAux;      
a9c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
a9c70 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
a9c80 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f  ies allocated fo
a9c90 72 20 61 70 41 75 78 5b 5d 20 2a 2f 0a 20 20 73  r apAux[] */.  s
a9ca0 74 72 75 63 74 20 41 75 78 44 61 74 61 20 7b 0a  truct AuxData {.
a9cb0 20 20 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20      void *pAux; 
a9cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a9cd0 20 20 2f 2a 20 41 75 78 20 64 61 74 61 20 66 6f    /* Aux data fo
a9ce0 72 20 74 68 65 20 69 2d 74 68 20 61 72 67 75 6d  r the i-th argum
a9cf0 65 6e 74 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20  ent */.    void 
a9d00 28 2a 78 44 65 6c 65 74 65 29 28 76 6f 69 64 20  (*xDelete)(void 
a9d10 2a 29 3b 20 20 20 20 20 20 2f 2a 20 44 65 73 74  *);      /* Dest
a9d20 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 61  ructor for the a
a9d30 75 78 20 64 61 74 61 20 2a 2f 0a 20 20 7d 20 61  ux data */.  } a
a9d40 70 41 75 78 5b 31 5d 3b 20 20 20 20 20 20 20 20  pAux[1];        
a9d50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
a9d60 65 20 73 6c 6f 74 20 66 6f 72 20 65 61 63 68 20  e slot for each 
a9d70 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e  function argumen
a9d80 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  t */.};../*.** T
a9d90 68 65 20 22 63 6f 6e 74 65 78 74 22 20 61 72 67  he "context" arg
a9da0 75 6d 65 6e 74 20 66 6f 72 20 61 20 69 6e 73 74  ument for a inst
a9db0 61 6c 6c 61 62 6c 65 20 66 75 6e 63 74 69 6f 6e  allable function
a9dc0 2e 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  .  A pointer to 
a9dd0 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f  an.** instance o
a9de0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
a9df0 20 69 73 20 74 68 65 20 66 69 72 73 74 20 61 72   is the first ar
a9e00 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 72 6f  gument to the ro
a9e10 75 74 69 6e 65 73 20 75 73 65 64 0a 2a 2a 20 69  utines used.** i
a9e20 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c  mplement the SQL
a9e30 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a   functions..**.*
a9e40 2a 20 54 68 65 72 65 20 69 73 20 61 20 74 79 70  * There is a typ
a9e50 65 64 65 66 20 66 6f 72 20 74 68 69 73 20 73 74  edef for this st
a9e60 72 75 63 74 75 72 65 20 69 6e 20 73 71 6c 69 74  ructure in sqlit
a9e70 65 2e 68 2e 20 20 53 6f 20 61 6c 6c 20 72 6f 75  e.h.  So all rou
a9e80 74 69 6e 65 73 2c 0a 2a 2a 20 65 76 65 6e 20 74  tines,.** even t
a9e90 68 65 20 70 75 62 6c 69 63 20 69 6e 74 65 72 66  he public interf
a9ea0 61 63 65 20 74 6f 20 53 51 4c 69 74 65 2c 20 63  ace to SQLite, c
a9eb0 61 6e 20 75 73 65 20 61 20 70 6f 69 6e 74 65 72  an use a pointer
a9ec0 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75   to this structu
a9ed0 72 65 2e 0a 2a 2a 20 42 75 74 20 74 68 69 73 20  re..** But this 
a9ee0 66 69 6c 65 20 69 73 20 74 68 65 20 6f 6e 6c 79  file is the only
a9ef0 20 70 6c 61 63 65 20 77 68 65 72 65 20 74 68 65   place where the
a9f00 20 69 6e 74 65 72 6e 61 6c 20 64 65 74 61 69 6c   internal detail
a9f10 73 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73 74 72  s of this.** str
a9f20 75 63 74 75 72 65 20 61 72 65 20 6b 6e 6f 77 6e  ucture are known
a9f30 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72  ..**.** This str
a9f40 75 63 74 75 72 65 20 69 73 20 64 65 66 69 6e 65  ucture is define
a9f50 64 20 69 6e 73 69 64 65 20 6f 66 20 76 64 62 65  d inside of vdbe
a9f60 49 6e 74 2e 68 20 62 65 63 61 75 73 65 20 69 74  Int.h because it
a9f70 20 75 73 65 73 20 73 75 62 73 74 72 75 63 74 75   uses substructu
a9f80 72 65 73 0a 2a 2a 20 28 4d 65 6d 29 20 77 68 69  res.** (Mem) whi
a9f90 63 68 20 61 72 65 20 6f 6e 6c 79 20 64 65 66 69  ch are only defi
a9fa0 6e 65 64 20 74 68 65 72 65 2e 0a 2a 2f 0a 73 74  ned there..*/.st
a9fb0 72 75 63 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e  ruct sqlite3_con
a9fc0 74 65 78 74 20 7b 0a 20 20 46 75 6e 63 44 65 66  text {.  FuncDef
a9fd0 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 2f   *pFunc;       /
a9fe0 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 75 6e  * Pointer to fun
a9ff0 63 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f  ction informatio
aa000 6e 2e 20 20 4d 55 53 54 20 42 45 20 46 49 52 53  n.  MUST BE FIRS
aa010 54 20 2a 2f 0a 20 20 56 64 62 65 46 75 6e 63 20  T */.  VdbeFunc 
aa020 2a 70 56 64 62 65 46 75 6e 63 3b 20 20 2f 2a 20  *pVdbeFunc;  /* 
aa030 41 75 78 69 6c 61 72 79 20 64 61 74 61 2c 20 69  Auxilary data, i
aa040 66 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a 20 20  f created. */.  
aa050 4d 65 6d 20 73 3b 20 20 20 20 20 20 20 20 20 20  Mem s;          
aa060 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74        /* The ret
aa070 75 72 6e 20 76 61 6c 75 65 20 69 73 20 73 74 6f  urn value is sto
aa080 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 4d 65  red here */.  Me
aa090 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20  m *pMem;        
aa0a0 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65      /* Memory ce
aa0b0 6c 6c 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  ll used to store
aa0c0 20 61 67 67 72 65 67 61 74 65 20 63 6f 6e 74 65   aggregate conte
aa0d0 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 72  xt */.  int isEr
aa0e0 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ror;          /*
aa0f0 20 45 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75   Error code retu
aa100 72 6e 65 64 20 62 79 20 74 68 65 20 66 75 6e 63  rned by the func
aa110 74 69 6f 6e 2e 20 2a 2f 0a 20 20 43 6f 6c 6c 53  tion. */.  CollS
aa120 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20  eq *pColl;      
aa130 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65   /* Collating se
aa140 71 75 65 6e 63 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  quence */.};../*
aa150 0a 2a 2a 20 41 20 53 65 74 20 73 74 72 75 63 74  .** A Set struct
aa160 75 72 65 20 69 73 20 75 73 65 64 20 66 6f 72 20  ure is used for 
aa170 71 75 69 63 6b 20 74 65 73 74 69 6e 67 20 74 6f  quick testing to
aa180 20 73 65 65 20 69 66 20 61 20 76 61 6c 75 65 0a   see if a value.
aa190 2a 2a 20 69 73 20 70 61 72 74 20 6f 66 20 61 20  ** is part of a 
aa1a0 73 6d 61 6c 6c 20 73 65 74 2e 20 20 53 65 74 73  small set.  Sets
aa1b0 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d 70   are used to imp
aa1c0 6c 65 6d 65 6e 74 20 63 6f 64 65 20 6c 69 6b 65  lement code like
aa1d0 0a 2a 2a 20 74 68 69 73 3a 0a 2a 2a 20 20 20 20  .** this:.**    
aa1e0 20 20 20 20 20 20 20 20 78 2e 79 20 49 4e 20 28          x.y IN (
aa1f0 27 68 69 27 2c 27 68 6f 6f 27 2c 27 68 75 6d 27  'hi','hoo','hum'
aa200 29 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ).*/.typedef str
aa210 75 63 74 20 53 65 74 20 53 65 74 3b 0a 73 74 72  uct Set Set;.str
aa220 75 63 74 20 53 65 74 20 7b 0a 20 20 48 61 73 68  uct Set {.  Hash
aa230 20 68 61 73 68 3b 20 20 20 20 20 20 20 20 20 20   hash;          
aa240 20 20 20 2f 2a 20 41 20 73 65 74 20 69 73 20 6a     /* A set is j
aa250 75 73 74 20 61 20 68 61 73 68 20 74 61 62 6c 65  ust a hash table
aa260 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a   */.  HashElem *
aa270 70 72 65 76 3b 20 20 20 20 20 20 20 20 2f 2a 20  prev;        /* 
aa280 50 72 65 76 69 6f 75 73 6c 79 20 61 63 63 65 73  Previously acces
aa290 73 65 64 20 68 61 73 68 20 65 6c 65 6d 65 6e 20  sed hash elemen 
aa2a0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  */.};../*.** An 
aa2b0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
aa2c0 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e  virtual machine.
aa2d0 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65    This structure
aa2e0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f   contains the co
aa2f0 6d 70 6c 65 74 65 0a 2a 2a 20 73 74 61 74 65 20  mplete.** state 
aa300 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d  of the virtual m
aa310 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  achine..**.** Th
aa320 65 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74 22  e "sqlite3_stmt"
aa330 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e 74   structure point
aa340 65 72 20 74 68 61 74 20 69 73 20 72 65 74 75 72  er that is retur
aa350 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63  ned by sqlite3_c
aa360 6f 6d 70 69 6c 65 28 29 0a 2a 2a 20 69 73 20 72  ompile().** is r
aa370 65 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20  eally a pointer 
aa380 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  to an instance o
aa390 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
aa3a0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65  ..**.** The Vdbe
aa3b0 2e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 76 61  .inVtabMethod va
aa3c0 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f  riable is set to
aa3d0 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f 72 20 74 68   non-zero for th
aa3e0 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 0a 2a 2a  e duration of.**
aa3f0 20 61 6e 79 20 76 69 72 74 75 61 6c 20 74 61 62   any virtual tab
aa400 6c 65 20 6d 65 74 68 6f 64 20 69 6e 76 6f 63 61  le method invoca
aa410 74 69 6f 6e 73 20 6d 61 64 65 20 62 79 20 74 68  tions made by th
aa420 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e 20  e vdbe program. 
aa430 49 74 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20  It is.** set to 
aa440 32 20 66 6f 72 20 78 44 65 73 74 72 6f 79 20 6d  2 for xDestroy m
aa450 65 74 68 6f 64 20 63 61 6c 6c 73 20 61 6e 64 20  ethod calls and 
aa460 31 20 66 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20  1 for all other 
aa470 6d 65 74 68 6f 64 73 2e 20 54 68 69 73 0a 2a 2a  methods. This.**
aa480 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 65   variable is use
aa490 64 20 66 6f 72 20 74 77 6f 20 70 75 72 70 6f 73  d for two purpos
aa4a0 65 73 3a 20 74 6f 20 61 6c 6c 6f 77 20 78 44 65  es: to allow xDe
aa4b0 73 74 72 6f 79 20 6d 65 74 68 6f 64 73 20 74 6f  stroy methods to
aa4c0 20 65 78 65 63 75 74 65 0a 2a 2a 20 22 44 52 4f   execute.** "DRO
aa4d0 50 20 54 41 42 4c 45 22 20 73 74 61 74 65 6d 65  P TABLE" stateme
aa4e0 6e 74 73 20 61 6e 64 20 74 6f 20 70 72 65 76 65  nts and to preve
aa4f0 6e 74 20 73 6f 6d 65 20 6e 61 73 74 79 20 73 69  nt some nasty si
aa500 64 65 20 65 66 66 65 63 74 73 20 6f 66 0a 2a 2a  de effects of.**
aa510 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20   malloc failure 
aa520 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 69  when SQLite is i
aa530 6e 76 6f 6b 65 64 20 72 65 63 75 72 73 69 76 65  nvoked recursive
aa540 6c 79 20 62 79 20 61 20 76 69 72 74 75 61 6c 20  ly by a virtual 
aa550 74 61 62 6c 65 20 0a 2a 2a 20 6d 65 74 68 6f 64  table .** method
aa560 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74   function..*/.st
aa570 72 75 63 74 20 56 64 62 65 20 7b 0a 20 20 73 71  ruct Vdbe {.  sq
aa580 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
aa590 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
aa5a0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
aa5b0 20 74 68 61 74 20 6f 77 6e 73 20 74 68 69 73 20   that owns this 
aa5c0 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 56  statement */.  V
aa5d0 64 62 65 20 2a 70 50 72 65 76 2c 2a 70 4e 65 78  dbe *pPrev,*pNex
aa5e0 74 3b 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64  t;     /* Linked
aa5f0 20 6c 69 73 74 20 6f 66 20 56 44 42 45 73 20 77   list of VDBEs w
aa600 69 74 68 20 74 68 65 20 73 61 6d 65 20 56 64 62  ith the same Vdb
aa610 65 2e 64 62 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f  e.db */.  int nO
aa620 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
aa630 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69    /* Number of i
aa640 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 6e 20 74  nstructions in t
aa650 68 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20  he program */.  
aa660 69 6e 74 20 6e 4f 70 41 6c 6c 6f 63 3b 20 20 20  int nOpAlloc;   
aa670 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
aa680 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63  r of slots alloc
aa690 61 74 65 64 20 66 6f 72 20 61 4f 70 5b 5d 20 2a  ated for aOp[] *
aa6a0 2f 0a 20 20 4f 70 20 2a 61 4f 70 3b 20 20 20 20  /.  Op *aOp;    
aa6b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
aa6c0 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65  pace to hold the
aa6d0 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
aa6e0 27 73 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20  's program */.  
aa6f0 69 6e 74 20 6e 4c 61 62 65 6c 3b 20 20 20 20 20  int nLabel;     
aa700 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
aa710 72 20 6f 66 20 6c 61 62 65 6c 73 20 75 73 65 64  r of labels used
aa720 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c   */.  int nLabel
aa730 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 2f 2a  Alloc;        /*
aa740 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73   Number of slots
aa750 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 61 4c   allocated in aL
aa760 61 62 65 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  abel[] */.  int 
aa770 2a 61 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 20  *aLabel;        
aa780 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20      /* Space to 
aa790 68 6f 6c 64 20 74 68 65 20 6c 61 62 65 6c 73 20  hold the labels 
aa7a0 2a 2f 0a 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67  */.  Mem **apArg
aa7b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
aa7c0 41 72 67 75 6d 65 6e 74 73 20 74 6f 20 63 75 72  Arguments to cur
aa7d0 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67  rently executing
aa7e0 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 2a   user function *
aa7f0 2f 0a 20 20 4d 65 6d 20 2a 61 43 6f 6c 4e 61 6d  /.  Mem *aColNam
aa800 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43  e;          /* C
aa810 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 6f 20 72  olumn names to r
aa820 65 74 75 72 6e 20 2a 2f 0a 20 20 4d 65 6d 20 2a  eturn */.  Mem *
aa830 70 52 65 73 75 6c 74 53 65 74 3b 20 20 20 20 20  pResultSet;     
aa840 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
aa850 20 61 6e 20 61 72 72 61 79 20 6f 66 20 72 65 73   an array of res
aa860 75 6c 74 73 20 2a 2f 0a 20 20 75 31 36 20 6e 52  ults */.  u16 nR
aa870 65 73 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20  esColumn;       
aa880 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
aa890 6f 6c 75 6d 6e 73 20 69 6e 20 6f 6e 65 20 72 6f  olumns in one ro
aa8a0 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20  w of the result 
aa8b0 73 65 74 20 2a 2f 0a 20 20 75 31 36 20 6e 43 75  set */.  u16 nCu
aa8c0 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20  rsor;           
aa8d0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c   /* Number of sl
aa8e0 6f 74 73 20 69 6e 20 61 70 43 73 72 5b 5d 20 2a  ots in apCsr[] *
aa8f0 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  /.  VdbeCursor *
aa900 2a 61 70 43 73 72 3b 20 20 20 20 20 2f 2a 20 4f  *apCsr;     /* O
aa910 6e 65 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68  ne element of th
aa920 69 73 20 61 72 72 61 79 20 66 6f 72 20 65 61 63  is array for eac
aa930 68 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 2a 2f  h open cursor */
aa940 0a 20 20 75 38 20 65 72 72 6f 72 41 63 74 69 6f  .  u8 errorActio
aa950 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  n;         /* Re
aa960 63 6f 76 65 72 79 20 61 63 74 69 6f 6e 20 74 6f  covery action to
aa970 20 64 6f 20 69 6e 20 63 61 73 65 20 6f 66 20 61   do in case of a
aa980 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 75 38 20  n error */.  u8 
aa990 6f 6b 56 61 72 3b 20 20 20 20 20 20 20 20 20 20  okVar;          
aa9a0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
aa9b0 61 7a 56 61 72 5b 5d 20 68 61 73 20 62 65 65 6e  azVar[] has been
aa9c0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a   initialized */.
aa9d0 20 20 75 31 36 20 6e 56 61 72 3b 20 20 20 20 20    u16 nVar;     
aa9e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
aa9f0 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
aaa00 6e 20 61 56 61 72 5b 5d 20 2a 2f 0a 20 20 4d 65  n aVar[] */.  Me
aaa10 6d 20 2a 61 56 61 72 3b 20 20 20 20 20 20 20 20  m *aVar;        
aaa20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20        /* Values 
aaa30 66 6f 72 20 74 68 65 20 4f 50 5f 56 61 72 69 61  for the OP_Varia
aaa40 62 6c 65 20 6f 70 63 6f 64 65 2e 20 2a 2f 0a 20  ble opcode. */. 
aaa50 20 63 68 61 72 20 2a 2a 61 7a 56 61 72 3b 20 20   char **azVar;  
aaa60 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
aaa70 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 2a 2f   of variables */
aaa80 0a 20 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20  .  u32 magic;   
aaa90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
aaaa0 67 69 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 73  gic number for s
aaab0 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a  anity checking *
aaac0 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20  /.  int nMem;   
aaad0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
aaae0 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20  umber of memory 
aaaf0 6c 6f 63 61 74 69 6f 6e 73 20 63 75 72 72 65 6e  locations curren
aab00 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f  tly allocated */
aab10 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 20  .  Mem *aMem;   
aab20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
aab30 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  e memory locatio
aab40 6e 73 20 2a 2f 0a 20 20 75 33 32 20 63 61 63 68  ns */.  u32 cach
aab50 65 43 74 72 3b 20 20 20 20 20 20 20 20 20 20 20  eCtr;           
aab60 2f 2a 20 56 64 62 65 43 75 72 73 6f 72 20 72 6f  /* VdbeCursor ro
aab70 77 20 63 61 63 68 65 20 67 65 6e 65 72 61 74 69  w cache generati
aab80 6f 6e 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  on counter */.  
aab90 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20  int pc;         
aaba0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
aabb0 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 2a  rogram counter *
aabc0 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  /.  int rc;     
aabd0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
aabe0 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a  alue to return *
aabf0 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73  /.  char *zErrMs
aac00 67 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45  g;          /* E
aac10 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72 69  rror message wri
aac20 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 75  tten here */.  u
aac30 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20 20  8 explain;      
aac40 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
aac50 66 20 45 58 50 4c 41 49 4e 20 70 72 65 73 65 6e  f EXPLAIN presen
aac60 74 20 6f 6e 20 53 51 4c 20 63 6f 6d 6d 61 6e 64  t on SQL command
aac70 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67 65 43   */.  u8 changeC
aac80 6e 74 4f 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a  ntOn;         /*
aac90 20 54 72 75 65 20 74 6f 20 75 70 64 61 74 65 20   True to update 
aaca0 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  the change-count
aacb0 65 72 20 2a 2f 0a 20 20 75 38 20 65 78 70 69 72  er */.  u8 expir
aacc0 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ed;             
aacd0 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 56  /* True if the V
aace0 4d 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65  M needs to be re
aacf0 63 6f 6d 70 69 6c 65 64 20 2a 2f 0a 20 20 75 38  compiled */.  u8
aad00 20 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72   minWriteFileFor
aad10 6d 61 74 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d  mat;  /* Minimum
aad20 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 66 6f 72   file format for
aad30 20 77 72 69 74 61 62 6c 65 20 64 61 74 61 62 61   writable databa
aad40 73 65 20 66 69 6c 65 73 20 2a 2f 0a 20 20 75 38  se files */.  u8
aad50 20 69 6e 56 74 61 62 4d 65 74 68 6f 64 3b 20 20   inVtabMethod;  
aad60 20 20 20 20 20 20 2f 2a 20 53 65 65 20 63 6f 6d        /* See com
aad70 6d 65 6e 74 73 20 61 62 6f 76 65 20 2a 2f 0a 20  ments above */. 
aad80 20 75 38 20 75 73 65 73 53 74 6d 74 4a 6f 75 72   u8 usesStmtJour
aad90 6e 61 6c 3b 20 20 20 20 20 2f 2a 20 54 72 75 65  nal;     /* True
aada0 20 69 66 20 75 73 65 73 20 61 20 73 74 61 74 65   if uses a state
aadb0 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  ment journal */.
aadc0 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20    u8 readOnly;  
aadd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
aade0 65 20 66 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20  e for read-only 
aadf0 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 20 20  statements */.  
aae00 75 38 20 69 73 50 72 65 70 61 72 65 56 32 3b 20  u8 isPrepareV2; 
aae10 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
aae20 69 66 20 70 72 65 70 61 72 65 64 20 77 69 74 68  if prepared with
aae30 20 70 72 65 70 61 72 65 5f 76 32 28 29 20 2a 2f   prepare_v2() */
aae40 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 20  .  int nChange; 
aae50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
aae60 6d 62 65 72 20 6f 66 20 64 62 20 63 68 61 6e 67  mber of db chang
aae70 65 73 20 6d 61 64 65 20 73 69 6e 63 65 20 6c 61  es made since la
aae80 73 74 20 72 65 73 65 74 20 2a 2f 0a 20 20 69 6e  st reset */.  in
aae90 74 20 62 74 72 65 65 4d 61 73 6b 3b 20 20 20 20  t btreeMask;    
aaea0 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b        /* Bitmask
aaeb0 20 6f 66 20 64 62 2d 3e 61 44 62 5b 5d 20 65 6e   of db->aDb[] en
aaec0 74 72 69 65 73 20 72 65 66 65 72 65 6e 63 65 64  tries referenced
aaed0 20 2a 2f 0a 20 20 69 36 34 20 73 74 61 72 74 54   */.  i64 startT
aaee0 69 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ime;          /*
aaef0 20 54 69 6d 65 20 77 68 65 6e 20 71 75 65 72 79   Time when query
aaf00 20 73 74 61 72 74 65 64 20 2d 20 75 73 65 64 20   started - used 
aaf10 66 6f 72 20 70 72 6f 66 69 6c 69 6e 67 20 2a 2f  for profiling */
aaf20 0a 20 20 42 74 72 65 65 4d 75 74 65 78 41 72 72  .  BtreeMutexArr
aaf30 61 79 20 61 4d 75 74 65 78 3b 20 2f 2a 20 41 6e  ay aMutex; /* An
aaf40 20 61 72 72 61 79 20 6f 66 20 42 74 72 65 65 20   array of Btree 
aaf50 75 73 65 64 20 68 65 72 65 20 61 6e 64 20 6e 65  used here and ne
aaf60 65 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20  eding locks */. 
aaf70 20 69 6e 74 20 61 43 6f 75 6e 74 65 72 5b 32 5d   int aCounter[2]
aaf80 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e  ;        /* Coun
aaf90 74 65 72 73 20 75 73 65 64 20 62 79 20 73 71 6c  ters used by sql
aafa0 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73  ite3_stmt_status
aafb0 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53  () */.  char *zS
aafc0 71 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ql;             
aafd0 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 53  /* Text of the S
aafe0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  QL statement tha
aaff0 74 20 67 65 6e 65 72 61 74 65 64 20 74 68 69 73  t generated this
ab000 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 46 72 65   */.  void *pFre
ab010 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
ab020 20 46 72 65 65 20 74 68 69 73 20 77 68 65 6e 20   Free this when 
ab030 64 65 6c 65 74 69 6e 67 20 74 68 65 20 76 64 62  deleting the vdb
ab040 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 74  e */.  int iStat
ab050 65 6d 65 6e 74 3b 20 20 20 20 20 20 20 20 20 2f  ement;         /
ab060 2a 20 53 74 61 74 65 6d 65 6e 74 20 6e 75 6d 62  * Statement numb
ab070 65 72 20 28 6f 72 20 30 20 69 66 20 68 61 73 20  er (or 0 if has 
ab080 6e 6f 74 20 6f 70 65 6e 65 64 20 73 74 6d 74 29  not opened stmt)
ab090 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
ab0a0 45 5f 44 45 42 55 47 0a 20 20 46 49 4c 45 20 2a  E_DEBUG.  FILE *
ab0b0 74 72 61 63 65 3b 20 20 20 20 20 20 20 20 20 20  trace;          
ab0c0 20 20 2f 2a 20 57 72 69 74 65 20 61 6e 20 65 78    /* Write an ex
ab0d0 65 63 75 74 69 6f 6e 20 74 72 61 63 65 20 68 65  ecution trace he
ab0e0 72 65 2c 20 69 66 20 6e 6f 74 20 4e 55 4c 4c 20  re, if not NULL 
ab0f0 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 56 64 62 65  */.#endif.  Vdbe
ab100 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20  Frame *pFrame;  
ab110 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 66 72      /* Parent fr
ab120 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 72  ame */.  int nFr
ab130 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ame;            
ab140 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72   /* Number of fr
ab150 61 6d 65 73 20 69 6e 20 70 46 72 61 6d 65 20 6c  ames in pFrame l
ab160 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ist */.};../*.**
ab170 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   The following a
ab180 72 65 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65  re allowed value
ab190 73 20 66 6f 72 20 56 64 62 65 2e 6d 61 67 69 63  s for Vdbe.magic
ab1a0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45  .*/.#define VDBE
ab1b0 5f 4d 41 47 49 43 5f 49 4e 49 54 20 20 20 20 20  _MAGIC_INIT     
ab1c0 30 78 32 36 62 63 65 61 61 35 20 20 20 20 2f 2a  0x26bceaa5    /*
ab1d0 20 42 75 69 6c 64 69 6e 67 20 61 20 56 44 42 45   Building a VDBE
ab1e0 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 23 64 65 66   program */.#def
ab1f0 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 52  ine VDBE_MAGIC_R
ab200 55 4e 20 20 20 20 20 20 30 78 62 64 66 32 30 64  UN      0xbdf20d
ab210 61 33 20 20 20 20 2f 2a 20 56 44 42 45 20 69 73  a3    /* VDBE is
ab220 20 72 65 61 64 79 20 74 6f 20 65 78 65 63 75 74   ready to execut
ab230 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42  e */.#define VDB
ab240 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 20 20 20  E_MAGIC_HALT    
ab250 20 30 78 35 31 39 63 32 39 37 33 20 20 20 20 2f   0x519c2973    /
ab260 2a 20 56 44 42 45 20 68 61 73 20 63 6f 6d 70 6c  * VDBE has compl
ab270 65 74 65 64 20 65 78 65 63 75 74 69 6f 6e 20 2a  eted execution *
ab280 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d  /.#define VDBE_M
ab290 41 47 49 43 5f 44 45 41 44 20 20 20 20 20 30 78  AGIC_DEAD     0x
ab2a0 62 36 30 36 63 33 63 38 20 20 20 20 2f 2a 20 54  b606c3c8    /* T
ab2b0 68 65 20 56 44 42 45 20 68 61 73 20 62 65 65 6e  he VDBE has been
ab2c0 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a   deallocated */.
ab2d0 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20  ./*.** Function 
ab2e0 70 72 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51  prototypes.*/.SQ
ab2f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
ab300 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65  d sqlite3VdbeFre
ab310 65 43 75 72 73 6f 72 28 56 64 62 65 20 2a 2c 20  eCursor(Vdbe *, 
ab320 56 64 62 65 43 75 72 73 6f 72 2a 29 3b 0a 76 6f  VdbeCursor*);.vo
ab330 69 64 20 73 71 6c 69 74 65 56 64 62 65 50 6f 70  id sqliteVdbePop
ab340 53 74 61 63 6b 28 56 64 62 65 2a 2c 69 6e 74 29  Stack(Vdbe*,int)
ab350 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
ab360 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
ab370 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62  CursorMoveto(Vdb
ab380 65 43 75 72 73 6f 72 2a 29 3b 0a 23 69 66 20 64  eCursor*);.#if d
ab390 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
ab3a0 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  BUG) || defined(
ab3b0 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 0a 53 51  VDBE_PROFILE).SQ
ab3c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
ab3d0 64 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69  d sqlite3VdbePri
ab3e0 6e 74 4f 70 28 46 49 4c 45 2a 2c 20 69 6e 74 2c  ntOp(FILE*, int,
ab3f0 20 4f 70 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51   Op*);.#endif.SQ
ab400 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32  LITE_PRIVATE u32
ab410 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
ab420 61 6c 54 79 70 65 4c 65 6e 28 75 33 32 29 3b 0a  alTypeLen(u32);.
ab430 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75  SQLITE_PRIVATE u
ab440 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  32 sqlite3VdbeSe
ab450 72 69 61 6c 54 79 70 65 28 4d 65 6d 2a 2c 20 69  rialType(Mem*, i
ab460 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
ab470 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56  ATE u32 sqlite3V
ab480 64 62 65 53 65 72 69 61 6c 50 75 74 28 75 6e 73  dbeSerialPut(uns
ab490 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e 74  igned char*, int
ab4a0 2c 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51  , Mem*, int);.SQ
ab4b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32  LITE_PRIVATE u32
ab4c0 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
ab4d0 61 6c 47 65 74 28 63 6f 6e 73 74 20 75 6e 73 69  alGet(const unsi
ab4e0 67 6e 65 64 20 63 68 61 72 2a 2c 20 75 33 32 2c  gned char*, u32,
ab4f0 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50   Mem*);.SQLITE_P
ab500 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
ab510 74 65 33 56 64 62 65 44 65 6c 65 74 65 41 75 78  te3VdbeDeleteAux
ab520 44 61 74 61 28 56 64 62 65 46 75 6e 63 2a 2c 20  Data(VdbeFunc*, 
ab530 69 6e 74 29 3b 0a 0a 69 6e 74 20 73 71 6c 69 74  int);..int sqlit
ab540 65 32 42 74 72 65 65 4b 65 79 43 6f 6d 70 61 72  e2BtreeKeyCompar
ab550 65 28 42 74 43 75 72 73 6f 72 20 2a 2c 20 63 6f  e(BtCursor *, co
ab560 6e 73 74 20 76 6f 69 64 20 2a 2c 20 69 6e 74 2c  nst void *, int,
ab570 20 69 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51   int, int *);.SQ
ab580 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
ab590 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b   sqlite3VdbeIdxK
ab5a0 65 79 43 6f 6d 70 61 72 65 28 56 64 62 65 43 75  eyCompare(VdbeCu
ab5b0 72 73 6f 72 2a 2c 55 6e 70 61 63 6b 65 64 52 65  rsor*,UnpackedRe
ab5c0 63 6f 72 64 2a 2c 69 6e 74 2a 29 3b 0a 53 51 4c  cord*,int*);.SQL
ab5d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
ab5e0 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f  sqlite3VdbeIdxRo
ab5f0 77 69 64 28 73 71 6c 69 74 65 33 2a 2c 20 42 74  wid(sqlite3*, Bt
ab600 43 75 72 73 6f 72 20 2a 2c 20 69 36 34 20 2a 29  Cursor *, i64 *)
ab610 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
ab620 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 43   int sqlite3MemC
ab630 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 4d 65 6d  ompare(const Mem
ab640 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63  *, const Mem*, c
ab650 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 2a 29 3b 0a  onst CollSeq*);.
ab660 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
ab670 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 45 78  nt sqlite3VdbeEx
ab680 65 63 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54  ec(Vdbe*);.SQLIT
ab690 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
ab6a0 6c 69 74 65 33 56 64 62 65 4c 69 73 74 28 56 64  lite3VdbeList(Vd
ab6b0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  be*);.SQLITE_PRI
ab6c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
ab6d0 56 64 62 65 48 61 6c 74 28 56 64 62 65 2a 29 3b  VdbeHalt(Vdbe*);
ab6e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ab6f0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43  int sqlite3VdbeC
ab700 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65  hangeEncoding(Me
ab710 6d 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  m *, int);.SQLIT
ab720 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
ab730 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42  lite3VdbeMemTooB
ab740 69 67 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  ig(Mem*);.SQLITE
ab750 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
ab760 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28  ite3VdbeMemCopy(
ab770 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a  Mem*, const Mem*
ab780 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
ab790 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
ab7a0 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79  beMemShallowCopy
ab7b0 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d  (Mem*, const Mem
ab7c0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
ab7d0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
ab7e0 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28  ite3VdbeMemMove(
ab7f0 4d 65 6d 2a 2c 20 4d 65 6d 2a 29 3b 0a 53 51 4c  Mem*, Mem*);.SQL
ab800 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
ab810 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75  sqlite3VdbeMemNu
ab820 6c 54 65 72 6d 69 6e 61 74 65 28 4d 65 6d 2a 29  lTerminate(Mem*)
ab830 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
ab840 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
ab850 4d 65 6d 53 65 74 53 74 72 28 4d 65 6d 2a 2c 20  MemSetStr(Mem*, 
ab860 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74  const char*, int
ab870 2c 20 75 38 2c 20 76 6f 69 64 28 2a 29 28 76 6f  , u8, void(*)(vo
ab880 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52  id*));.SQLITE_PR
ab890 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
ab8a0 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36  e3VdbeMemSetInt6
ab8b0 34 28 4d 65 6d 2a 2c 20 69 36 34 29 3b 0a 53 51  4(Mem*, i64);.SQ
ab8c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
ab8d0 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
ab8e0 53 65 74 44 6f 75 62 6c 65 28 4d 65 6d 2a 2c 20  SetDouble(Mem*, 
ab8f0 64 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f  double);.SQLITE_
ab900 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
ab910 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75  ite3VdbeMemSetNu
ab920 6c 6c 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  ll(Mem*);.SQLITE
ab930 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
ab940 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a  lite3VdbeMemSetZ
ab950 65 72 6f 42 6c 6f 62 28 4d 65 6d 2a 2c 69 6e 74  eroBlob(Mem*,int
ab960 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
ab970 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
ab980 62 65 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 4d  beMemSetRowSet(M
ab990 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  em*);.SQLITE_PRI
ab9a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
ab9b0 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65  VdbeMemMakeWrite
ab9c0 61 62 6c 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49  able(Mem*);.SQLI
ab9d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
ab9e0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72  qlite3VdbeMemStr
ab9f0 69 6e 67 69 66 79 28 4d 65 6d 2a 2c 20 69 6e 74  ingify(Mem*, int
aba00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
aba10 45 20 69 36 34 20 73 71 6c 69 74 65 33 56 64 62  E i64 sqlite3Vdb
aba20 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d 2a 29 3b  eIntValue(Mem*);
aba30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
aba40 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
aba50 65 6d 49 6e 74 65 67 65 72 69 66 79 28 4d 65 6d  emIntegerify(Mem
aba60 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
aba70 54 45 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65  TE double sqlite
aba80 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 4d  3VdbeRealValue(M
aba90 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  em*);.SQLITE_PRI
abaa0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
abab0 33 56 64 62 65 49 6e 74 65 67 65 72 41 66 66 69  3VdbeIntegerAffi
abac0 6e 69 74 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49  nity(Mem*);.SQLI
abad0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
abae0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61  qlite3VdbeMemRea
abaf0 6c 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49  lify(Mem*);.SQLI
abb00 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
abb10 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d  qlite3VdbeMemNum
abb20 65 72 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c  erify(Mem*);.SQL
abb30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
abb40 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72  sqlite3VdbeMemFr
abb50 6f 6d 42 74 72 65 65 28 42 74 43 75 72 73 6f 72  omBtree(BtCursor
abb60 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 4d 65  *,int,int,int,Me
abb70 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
abb80 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
abb90 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d  VdbeMemRelease(M
abba0 65 6d 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50  em *p);.SQLITE_P
abbb0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
abbc0 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
abbd0 65 45 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a 70  eExternal(Mem *p
abbe0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
abbf0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
abc00 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 4d 65 6d  eMemFinalize(Mem
abc10 2a 2c 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51  *, FuncDef*);.SQ
abc20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
abc30 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
abc40 4f 70 63 6f 64 65 4e 61 6d 65 28 69 6e 74 29 3b  OpcodeName(int);
abc50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
abc60 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4f  int sqlite3VdbeO
abc70 70 63 6f 64 65 48 61 73 50 72 6f 70 65 72 74 79  pcodeHasProperty
abc80 28 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  (int, int);.SQLI
abc90 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
abca0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f  qlite3VdbeMemGro
abcb0 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74  w(Mem *pMem, int
abcc0 20 6e 2c 20 69 6e 74 20 70 72 65 73 65 72 76 65   n, int preserve
abcd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
abce0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
abcf0 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 28  eCloseStatement(
abd00 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51  Vdbe *, int);.SQ
abd10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
abd20 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 61  d sqlite3VdbeFra
abd30 6d 65 44 65 6c 65 74 65 28 56 64 62 65 46 72 61  meDelete(VdbeFra
abd40 6d 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  me*);.SQLITE_PRI
abd50 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
abd60 56 64 62 65 46 72 61 6d 65 52 65 73 74 6f 72 65  VdbeFrameRestore
abd70 28 56 64 62 65 46 72 61 6d 65 20 2a 29 3b 0a 23  (VdbeFrame *);.#
abd80 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
abd90 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47  BLE_MEMORY_MANAG
abda0 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49  EMENT.SQLITE_PRI
abdb0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
abdc0 56 64 62 65 52 65 6c 65 61 73 65 42 75 66 66 65  VdbeReleaseBuffe
abdd0 72 73 28 56 64 62 65 20 2a 70 29 3b 0a 23 65 6e  rs(Vdbe *p);.#en
abde0 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
abdf0 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
abe00 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 49  CACHE.SQLITE_PRI
abe10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
abe20 33 56 64 62 65 4d 75 74 65 78 41 72 72 61 79 45  3VdbeMutexArrayE
abe30 6e 74 65 72 28 56 64 62 65 20 2a 70 29 3b 0a 23  nter(Vdbe *p);.#
abe40 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71  else.# define sq
abe50 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 41 72  lite3VdbeMutexAr
abe60 72 61 79 45 6e 74 65 72 28 70 29 0a 23 65 6e 64  rayEnter(p).#end
abe70 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  if..SQLITE_PRIVA
abe80 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
abe90 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 4d  beMemTranslate(M
abea0 65 6d 2a 2c 20 75 38 29 3b 0a 23 69 66 64 65 66  em*, u8);.#ifdef
abeb0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51   SQLITE_DEBUG.SQ
abec0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
abed0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 50  oid sqlite3VdbeP
abee0 72 69 6e 74 53 71 6c 28 56 64 62 65 2a 29 3b 0a  rintSql(Vdbe*);.
abef0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
abf00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
abf10 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28  eMemPrettyPrint(
abf20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 63 68 61 72 20  Mem *pMem, char 
abf30 2a 7a 42 75 66 29 3b 0a 23 65 6e 64 69 66 0a 53  *zBuf);.#endif.S
abf40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
abf50 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
abf60 48 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70  HandleBom(Mem *p
abf70 4d 65 6d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  Mem);..#ifndef S
abf80 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42  QLITE_OMIT_INCRB
abf90 4c 4f 42 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  LOB.SQLITE_PRIVA
abfa0 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33  TE   int sqlite3
abfb0 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f  VdbeMemExpandBlo
abfc0 62 28 4d 65 6d 20 2a 29 3b 0a 23 65 6c 73 65 0a  b(Mem *);.#else.
abfd0 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65    #define sqlite
abfe0 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c  3VdbeMemExpandBl
abff0 6f 62 28 78 29 20 53 51 4c 49 54 45 5f 4f 4b 0a  ob(x) SQLITE_OK.
ac000 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f  #endif..#endif /
ac010 2a 20 21 64 65 66 69 6e 65 64 28 5f 56 44 42 45  * !defined(_VDBE
ac020 49 4e 54 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a  INT_H_) */../***
ac030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
ac040 6f 66 20 76 64 62 65 49 6e 74 2e 68 20 2a 2a 2a  of vdbeInt.h ***
ac050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ac060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ac070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
ac080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
ac090 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
ac0a0 6c 65 66 74 20 6f 66 66 20 69 6e 20 75 74 66 2e  left off in utf.
ac0b0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
ac0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66  **********/..#if
ac0d0 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c  ndef SQLITE_AMAL
ac0e0 47 41 4d 41 54 49 4f 4e 0a 2f 2a 0a 2a 2a 20 54  GAMATION./*.** T
ac0f0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e  he following con
ac100 73 74 61 6e 74 20 76 61 6c 75 65 20 69 73 20 75  stant value is u
ac110 73 65 64 20 62 79 20 74 68 65 20 53 51 4c 49 54  sed by the SQLIT
ac120 45 5f 42 49 47 45 4e 44 49 41 4e 20 61 6e 64 0a  E_BIGENDIAN and.
ac130 2a 2a 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45  ** SQLITE_LITTLE
ac140 45 4e 44 49 41 4e 20 6d 61 63 72 6f 73 2e 0a 2a  ENDIAN macros..*
ac150 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
ac160 20 63 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74   const int sqlit
ac170 65 33 6f 6e 65 20 3d 20 31 3b 0a 23 65 6e 64 69  e3one = 1;.#endi
ac180 66 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4d 41 4c  f /* SQLITE_AMAL
ac190 47 41 4d 41 54 49 4f 4e 20 2a 2f 0a 0a 2f 2a 0a  GAMATION */../*.
ac1a0 2a 2a 20 54 68 69 73 20 6c 6f 6f 6b 75 70 20 74  ** This lookup t
ac1b0 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20  able is used to 
ac1c0 68 65 6c 70 20 64 65 63 6f 64 65 20 74 68 65 20  help decode the 
ac1d0 66 69 72 73 74 20 62 79 74 65 20 6f 66 0a 2a 2a  first byte of.**
ac1e0 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 55 54   a multi-byte UT
ac1f0 46 38 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2f  F8 character..*/
ac200 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e  .static const un
ac210 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69  signed char sqli
ac220 74 65 33 55 74 66 38 54 72 61 6e 73 31 5b 5d 20  te3Utf8Trans1[] 
ac230 3d 20 7b 0a 20 20 30 78 30 30 2c 20 30 78 30 31  = {.  0x00, 0x01
ac240 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78  , 0x02, 0x03, 0x
ac250 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20  04, 0x05, 0x06, 
ac260 30 78 30 37 2c 0a 20 20 30 78 30 38 2c 20 30 78  0x07,.  0x08, 0x
ac270 30 39 2c 20 30 78 30 61 2c 20 30 78 30 62 2c 20  09, 0x0a, 0x0b, 
ac280 30 78 30 63 2c 20 30 78 30 64 2c 20 30 78 30 65  0x0c, 0x0d, 0x0e
ac290 2c 20 30 78 30 66 2c 0a 20 20 30 78 31 30 2c 20  , 0x0f,.  0x10, 
ac2a0 30 78 31 31 2c 20 30 78 31 32 2c 20 30 78 31 33  0x11, 0x12, 0x13
ac2b0 2c 20 30 78 31 34 2c 20 30 78 31 35 2c 20 30 78  , 0x14, 0x15, 0x
ac2c0 31 36 2c 20 30 78 31 37 2c 0a 20 20 30 78 31 38  16, 0x17,.  0x18
ac2d0 2c 20 30 78 31 39 2c 20 30 78 31 61 2c 20 30 78  , 0x19, 0x1a, 0x
ac2e0 31 62 2c 20 30 78 31 63 2c 20 30 78 31 64 2c 20  1b, 0x1c, 0x1d, 
ac2f0 30 78 31 65 2c 20 30 78 31 66 2c 0a 20 20 30 78  0x1e, 0x1f,.  0x
ac300 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20  00, 0x01, 0x02, 
ac310 30 78 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35  0x03, 0x04, 0x05
ac320 2c 20 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20  , 0x06, 0x07,.  
ac330 30 78 30 38 2c 20 30 78 30 39 2c 20 30 78 30 61  0x08, 0x09, 0x0a
ac340 2c 20 30 78 30 62 2c 20 30 78 30 63 2c 20 30 78  , 0x0b, 0x0c, 0x
ac350 30 64 2c 20 30 78 30 65 2c 20 30 78 30 66 2c 0a  0d, 0x0e, 0x0f,.
ac360 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78    0x00, 0x01, 0x
ac370 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34 2c 20  02, 0x03, 0x04, 
ac380 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78 30 37  0x05, 0x06, 0x07
ac390 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20  ,.  0x00, 0x01, 
ac3a0 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 30  0x02, 0x03, 0x00
ac3b0 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78  , 0x01, 0x00, 0x
ac3c0 30 30 2c 0a 7d 3b 0a 0a 0a 23 64 65 66 69 6e 65  00,.};...#define
ac3d0 20 57 52 49 54 45 5f 55 54 46 38 28 7a 4f 75 74   WRITE_UTF8(zOut
ac3e0 2c 20 63 29 20 7b 20 20 20 20 20 20 20 20 20 20  , c) {          
ac3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac400 5c 0a 20 20 69 66 28 20 63 3c 30 78 30 30 30 38  \.  if( c<0x0008
ac410 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  0 ){            
ac420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac430 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
ac440 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26  zOut++ = (u8)(c&
ac450 30 78 46 46 29 3b 20 20 20 20 20 20 20 20 20 20  0xFF);          
ac460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac470 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20    \.  }         
ac480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac4a0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 65             \.  e
ac4b0 6c 73 65 20 69 66 28 20 63 3c 30 78 30 30 38 30  lse if( c<0x0080
ac4c0 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  0 ){            
ac4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac4e0 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
ac4f0 2b 20 3d 20 30 78 43 30 20 2b 20 28 75 38 29 28  + = 0xC0 + (u8)(
ac500 28 63 3e 3e 36 29 26 30 78 31 46 29 3b 20 20 20  (c>>6)&0x1F);   
ac510 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ac520 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38     *zOut++ = 0x8
ac530 30 20 2b 20 28 75 38 29 28 63 20 26 20 30 78 33  0 + (u8)(c & 0x3
ac540 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  F);             
ac550 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20        \.  }     
ac560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ac590 0a 20 20 65 6c 73 65 20 69 66 28 20 63 3c 30 78  .  else if( c<0x
ac5a0 31 30 30 30 30 20 29 7b 20 20 20 20 20 20 20 20  10000 ){        
ac5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac5c0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a          \.    *z
ac5d0 4f 75 74 2b 2b 20 3d 20 30 78 45 30 20 2b 20 28  Out++ = 0xE0 + (
ac5e0 75 38 29 28 28 63 3e 3e 31 32 29 26 30 78 30 46  u8)((c>>12)&0x0F
ac5f0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
ac600 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
ac610 20 30 78 38 30 20 2b 20 28 75 38 29 28 28 63 3e   0x80 + (u8)((c>
ac620 3e 36 29 20 26 20 30 78 33 46 29 3b 20 20 20 20  >6) & 0x3F);    
ac630 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
ac640 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b  *zOut++ = 0x80 +
ac650 20 28 75 38 29 28 63 20 26 20 30 78 33 46 29 3b   (u8)(c & 0x3F);
ac660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac670 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20     \.  }else{   
ac680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac6a0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
ac6b0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 46 30    *zOut++ = 0xF0
ac6c0 20 2b 20 28 75 38 29 28 28 63 3e 3e 31 38 29 20   + (u8)((c>>18) 
ac6d0 26 20 30 78 30 37 29 3b 20 20 20 20 20 20 20 20  & 0x07);        
ac6e0 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
ac6f0 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29  ++ = 0x80 + (u8)
ac700 28 28 63 3e 3e 31 32 29 20 26 20 30 78 33 46 29  ((c>>12) & 0x3F)
ac710 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a  ;             \.
ac720 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78      *zOut++ = 0x
ac730 38 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 36 29  80 + (u8)((c>>6)
ac740 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20   & 0x3F);       
ac750 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f         \.    *zO
ac760 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75  ut++ = 0x80 + (u
ac770 38 29 28 63 20 26 20 30 78 33 46 29 3b 20 20 20  8)(c & 0x3F);   
ac780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac790 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20  \.  }           
ac7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac7c0 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64           \.}..#d
ac7d0 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46 31  efine WRITE_UTF1
ac7e0 36 4c 45 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20  6LE(zOut, c) {  
ac7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac810 20 20 5c 0a 20 20 69 66 28 20 63 3c 3d 30 78 46    \.  if( c<=0xF
ac820 46 46 46 20 29 7b 20 20 20 20 20 20 20 20 20 20  FFF ){          
ac830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac850 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a          \.    *z
ac860 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 30  Out++ = (u8)(c&0
ac870 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20  x00FF);         
ac880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ac8a0 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75      *zOut++ = (u
ac8b0 38 29 28 28 63 3e 3e 38 29 26 30 78 30 30 46 46  8)((c>>8)&0x00FF
ac8c0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
ac8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac8e0 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20      \.  }else{  
ac8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac920 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
ac930 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 28  *zOut++ = (u8)((
ac940 28 63 3e 3e 31 30 29 26 30 78 30 30 33 46 29 20  (c>>10)&0x003F) 
ac950 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 29 3e  + (((c-0x10000)>
ac960 3e 31 30 29 26 30 78 30 30 43 30 29 29 3b 20 20  >10)&0x00C0));  
ac970 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
ac980 28 75 38 29 28 30 78 30 30 44 38 20 2b 20 28 28  (u8)(0x00D8 + ((
ac990 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 38 29  (c-0x10000)>>18)
ac9a0 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20  &0x03));        
ac9b0 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75        \.    *zOu
ac9c0 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 78 30  t++ = (u8)(c&0x0
ac9d0 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20  0FF);           
ac9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac9f0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
aca00 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29    *zOut++ = (u8)
aca10 28 30 78 30 30 44 43 20 2b 20 28 28 63 3e 3e 38  (0x00DC + ((c>>8
aca20 29 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 20  )&0x03));       
aca30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aca40 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20    \.  }         
aca50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aca60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aca70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aca80 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65          \.}..#de
aca90 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46 31 36  fine WRITE_UTF16
acaa0 42 45 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20  BE(zOut, c) {   
acab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acad0 20 5c 0a 20 20 69 66 28 20 63 3c 3d 30 78 46 46   \.  if( c<=0xFF
acae0 46 46 20 29 7b 20 20 20 20 20 20 20 20 20 20 20  FF ){           
acaf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acb00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acb10 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f         \.    *zO
acb20 75 74 2b 2b 20 3d 20 28 75 38 29 28 28 63 3e 3e  ut++ = (u8)((c>>
acb30 38 29 26 30 78 30 30 46 46 29 3b 20 20 20 20 20  8)&0x00FF);     
acb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acb50 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
acb60 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38     *zOut++ = (u8
acb70 29 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 20  )(c&0x00FF);    
acb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acba0 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20     \.  }else{   
acbb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acbe0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
acbf0 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 30 78  zOut++ = (u8)(0x
acc00 30 30 44 38 20 2b 20 28 28 28 63 2d 30 78 31 30  00D8 + (((c-0x10
acc10 30 30 30 29 3e 3e 31 38 29 26 30 78 30 33 29 29  000)>>18)&0x03))
acc20 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c  ;              \
acc30 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28  .    *zOut++ = (
acc40 75 38 29 28 28 28 63 3e 3e 31 30 29 26 30 78 30  u8)(((c>>10)&0x0
acc50 30 33 46 29 20 2b 20 28 28 28 63 2d 30 78 31 30  03F) + (((c-0x10
acc60 30 30 30 29 3e 3e 31 30 29 26 30 78 30 30 43 30  000)>>10)&0x00C0
acc70 29 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74  ));  \.    *zOut
acc80 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44 43  ++ = (u8)(0x00DC
acc90 20 2b 20 28 28 63 3e 3e 38 29 26 30 78 30 33 29   + ((c>>8)&0x03)
acca0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
accb0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
accc0 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28   *zOut++ = (u8)(
accd0 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20  c&0x00FF);      
acce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
accf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acd00 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20   \.  }          
acd10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acd20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acd40 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66         \.}..#def
acd50 69 6e 65 20 52 45 41 44 5f 55 54 46 31 36 4c 45  ine READ_UTF16LE
acd60 28 7a 49 6e 2c 20 63 29 7b 20 20 20 20 20 20 20  (zIn, c){       
acd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acd80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acd90 20 20 5c 0a 20 20 63 20 3d 20 28 2a 7a 49 6e 2b    \.  c = (*zIn+
acda0 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  +);             
acdb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acdd0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20            \.  c 
acde0 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29  += ((*zIn++)<<8)
acdf0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
ace00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ace10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ace20 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 44    \.  if( c>=0xD
ace30 38 30 30 20 26 26 20 63 3c 30 78 45 30 30 30 20  800 && c<0xE000 
ace40 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ){              
ace50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ace60 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
ace70 69 6e 74 20 63 32 20 3d 20 28 2a 7a 49 6e 2b 2b  int c2 = (*zIn++
ace80 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
ace90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aceb0 20 20 5c 0a 20 20 20 20 63 32 20 2b 3d 20 28 28    \.    c2 += ((
acec0 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20  *zIn++)<<8);    
aced0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acef0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
acf00 63 20 3d 20 28 63 32 26 30 78 30 33 46 46 29 20  c = (c2&0x03FF) 
acf10 2b 20 28 28 63 26 30 78 30 30 33 46 29 3c 3c 31  + ((c&0x003F)<<1
acf20 30 29 20 2b 20 28 28 28 63 26 30 78 30 33 43 30  0) + (((c&0x03C0
acf30 29 2b 30 78 30 30 34 30 29 3c 3c 31 30 29 3b 20  )+0x0040)<<10); 
acf40 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20    \.  }         
acf50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acf60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acf80 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23            \.}..#
acf90 64 65 66 69 6e 65 20 52 45 41 44 5f 55 54 46 31  define READ_UTF1
acfa0 36 42 45 28 7a 49 6e 2c 20 63 29 7b 20 20 20 20  6BE(zIn, c){    
acfb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acfc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acfd0 20 20 20 20 20 5c 0a 20 20 63 20 3d 20 28 28 2a       \.  c = ((*
acfe0 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20  zIn++)<<8);     
acff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad010 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ad020 20 63 20 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20   c += (*zIn++); 
ad030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad060 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d       \.  if( c>=
ad070 30 78 44 38 30 30 20 26 26 20 63 3c 30 78 45 30  0xD800 && c<0xE0
ad080 30 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20  00 ){           
ad090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ad0b0 20 20 20 69 6e 74 20 63 32 20 3d 20 28 28 2a 7a     int c2 = ((*z
ad0c0 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20  In++)<<8);      
ad0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad0f0 20 20 20 20 20 5c 0a 20 20 20 20 63 32 20 2b 3d       \.    c2 +=
ad100 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20   (*zIn++);      
ad110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad130 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ad140 20 20 20 63 20 3d 20 28 63 32 26 30 78 30 33 46     c = (c2&0x03F
ad150 46 29 20 2b 20 28 28 63 26 30 78 30 30 33 46 29  F) + ((c&0x003F)
ad160 3c 3c 31 30 29 20 2b 20 28 28 28 63 26 30 78 30  <<10) + (((c&0x0
ad170 33 43 30 29 2b 30 78 30 30 34 30 29 3c 3c 31 30  3C0)+0x0040)<<10
ad180 29 3b 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20  );   \.  }      
ad190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d               \.}
ad1d0 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74  ../*.** Translat
ad1e0 65 20 61 20 73 69 6e 67 6c 65 20 55 54 46 2d 38  e a single UTF-8
ad1f0 20 63 68 61 72 61 63 74 65 72 2e 20 20 52 65 74   character.  Ret
ad200 75 72 6e 20 74 68 65 20 75 6e 69 63 6f 64 65 20  urn the unicode 
ad210 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 44 75 72  value..**.** Dur
ad220 69 6e 67 20 74 72 61 6e 73 6c 61 74 69 6f 6e 2c  ing translation,
ad230 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 65   assume that the
ad240 20 62 79 74 65 20 74 68 61 74 20 7a 54 65 72 6d   byte that zTerm
ad250 20 70 6f 69 6e 74 73 0a 2a 2a 20 69 73 20 61 20   points.** is a 
ad260 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74  0x00..**.** Writ
ad270 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  e a pointer to t
ad280 68 65 20 6e 65 78 74 20 75 6e 72 65 61 64 20 62  he next unread b
ad290 79 74 65 20 62 61 63 6b 20 69 6e 74 6f 20 2a 70  yte back into *p
ad2a0 7a 4e 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74  zNext..**.** Not
ad2b0 65 73 20 4f 6e 20 49 6e 76 61 6c 69 64 20 55 54  es On Invalid UT
ad2c0 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54  F-8:.**.**  *  T
ad2d0 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 76 65  his routine neve
ad2e0 72 20 61 6c 6c 6f 77 73 20 61 20 37 2d 62 69 74  r allows a 7-bit
ad2f0 20 63 68 61 72 61 63 74 65 72 20 28 30 78 30 30   character (0x00
ad300 20 74 68 72 6f 75 67 68 20 30 78 37 66 29 20 74   through 0x7f) t
ad310 6f 0a 2a 2a 20 20 20 20 20 62 65 20 65 6e 63 6f  o.**     be enco
ad320 64 65 64 20 61 73 20 61 20 6d 75 6c 74 69 2d 62  ded as a multi-b
ad330 79 74 65 20 63 68 61 72 61 63 74 65 72 2e 20 20  yte character.  
ad340 41 6e 79 20 6d 75 6c 74 69 2d 62 79 74 65 20 63  Any multi-byte c
ad350 68 61 72 61 63 74 65 72 20 74 68 61 74 0a 2a 2a  haracter that.**
ad360 20 20 20 20 20 61 74 74 65 6d 70 74 73 20 74 6f       attempts to
ad370 20 65 6e 63 6f 64 65 20 61 20 76 61 6c 75 65 20   encode a value 
ad380 62 65 74 77 65 65 6e 20 30 78 30 30 20 61 6e 64  between 0x00 and
ad390 20 30 78 37 66 20 69 73 20 72 65 6e 64 65 72 65   0x7f is rendere
ad3a0 64 20 61 73 20 30 78 66 66 66 64 2e 0a 2a 2a 0a  d as 0xfffd..**.
ad3b0 2a 2a 20 20 2a 20 20 54 68 69 73 20 72 6f 75 74  **  *  This rout
ad3c0 69 6e 65 20 6e 65 76 65 72 20 61 6c 6c 6f 77 73  ine never allows
ad3d0 20 61 20 55 54 46 31 36 20 73 75 72 72 6f 67 61   a UTF16 surroga
ad3e0 74 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20 65  te value to be e
ad3f0 6e 63 6f 64 65 64 2e 0a 2a 2a 20 20 20 20 20 49  ncoded..**     I
ad400 66 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 63  f a multi-byte c
ad410 68 61 72 61 63 74 65 72 20 61 74 74 65 6d 70 74  haracter attempt
ad420 73 20 74 6f 20 65 6e 63 6f 64 65 20 61 20 76 61  s to encode a va
ad430 6c 75 65 20 62 65 74 77 65 65 6e 0a 2a 2a 20 20  lue between.**  
ad440 20 20 20 30 78 64 38 30 30 20 61 6e 64 20 30 78     0xd800 and 0x
ad450 65 30 30 30 20 74 68 65 6e 20 69 74 20 69 73 20  e000 then it is 
ad460 72 65 6e 64 65 72 65 64 20 61 73 20 30 78 66 66  rendered as 0xff
ad470 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 42 79  fd..**.**  *  By
ad480 74 65 73 20 69 6e 20 74 68 65 20 72 61 6e 67 65  tes in the range
ad490 20 6f 66 20 30 78 38 30 20 74 68 72 6f 75 67 68   of 0x80 through
ad4a0 20 30 78 62 66 20 77 68 69 63 68 20 6f 63 63 75   0xbf which occu
ad4b0 72 20 61 73 20 74 68 65 20 66 69 72 73 74 0a 2a  r as the first.*
ad4c0 2a 20 20 20 20 20 62 79 74 65 20 6f 66 20 61 20  *     byte of a 
ad4d0 63 68 61 72 61 63 74 65 72 20 61 72 65 20 69 6e  character are in
ad4e0 74 65 72 70 72 65 74 65 64 20 61 73 20 73 69 6e  terpreted as sin
ad4f0 67 6c 65 2d 62 79 74 65 20 63 68 61 72 61 63 74  gle-byte charact
ad500 65 72 73 0a 2a 2a 20 20 20 20 20 61 6e 64 20 72  ers.**     and r
ad510 65 6e 64 65 72 65 64 20 61 73 20 74 68 65 6d 73  endered as thems
ad520 65 6c 76 65 73 20 65 76 65 6e 20 74 68 6f 75 67  elves even thoug
ad530 68 20 74 68 65 79 20 61 72 65 20 74 65 63 68 6e  h they are techn
ad540 69 63 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 69 6e  ically.**     in
ad550 76 61 6c 69 64 20 63 68 61 72 61 63 74 65 72 73  valid characters
ad560 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73  ..**.**  *  This
ad570 20 72 6f 75 74 69 6e 65 20 61 63 63 65 70 74 73   routine accepts
ad580 20 61 6e 20 69 6e 66 69 6e 69 74 65 20 6e 75 6d   an infinite num
ad590 62 65 72 20 6f 66 20 64 69 66 66 65 72 65 6e 74  ber of different
ad5a0 20 55 54 46 38 20 65 6e 63 6f 64 69 6e 67 73 0a   UTF8 encodings.
ad5b0 2a 2a 20 20 20 20 20 66 6f 72 20 75 6e 69 63 6f  **     for unico
ad5c0 64 65 20 76 61 6c 75 65 73 20 30 78 38 30 20 61  de values 0x80 a
ad5d0 6e 64 20 67 72 65 61 74 65 72 2e 20 20 49 74 20  nd greater.  It 
ad5e0 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20 6f 76  do not change ov
ad5f0 65 72 2d 6c 65 6e 67 74 68 0a 2a 2a 20 20 20 20  er-length.**    
ad600 20 65 6e 63 6f 64 69 6e 67 73 20 74 6f 20 30 78   encodings to 0x
ad610 66 66 66 64 20 61 73 20 73 6f 6d 65 20 73 79 73  fffd as some sys
ad620 74 65 6d 73 20 72 65 63 6f 6d 6d 65 6e 64 2e 0a  tems recommend..
ad630 2a 2f 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f  */.#define READ_
ad640 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c  UTF8(zIn, zTerm,
ad650 20 63 29 20 20 20 20 20 20 20 20 20 20 20 20 20   c)             
ad660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ad670 20 20 63 20 3d 20 2a 28 7a 49 6e 2b 2b 29 3b 20    c = *(zIn++); 
ad680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad6a0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69             \.  i
ad6b0 66 28 20 63 3e 3d 30 78 63 30 20 29 7b 20 20 20  f( c>=0xc0 ){   
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 20 20 20 20                  
ad6e0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 20          \.    c 
ad6f0 3d 20 73 71 6c 69 74 65 33 55 74 66 38 54 72 61  = sqlite3Utf8Tra
ad700 6e 73 31 5b 63 2d 30 78 63 30 5d 3b 20 20 20 20  ns1[c-0xc0];    
ad710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad720 20 20 20 20 20 5c 0a 20 20 20 20 77 68 69 6c 65       \.    while
ad730 28 20 7a 49 6e 21 3d 7a 54 65 72 6d 20 26 26 20  ( zIn!=zTerm && 
ad740 28 2a 7a 49 6e 20 26 20 30 78 63 30 29 3d 3d 30  (*zIn & 0xc0)==0
ad750 78 38 30 20 29 7b 20 20 20 20 20 20 20 20 20 20  x80 ){          
ad760 20 20 5c 0a 20 20 20 20 20 20 63 20 3d 20 28 63    \.      c = (c
ad770 3c 3c 36 29 20 2b 20 28 30 78 33 66 20 26 20 2a  <<6) + (0x3f & *
ad780 28 7a 49 6e 2b 2b 29 29 3b 20 20 20 20 20 20 20  (zIn++));       
ad790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ad7a0 0a 20 20 20 20 7d 20 20 20 20 20 20 20 20 20 20  .    }          
ad7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad7d0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
ad7e0 20 20 69 66 28 20 63 3c 30 78 38 30 20 20 20 20    if( c<0x80    
ad7f0 20 20 20 20 20 20 20 20 20 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 5c 0a 20 20 20 20 20           \.     
ad820 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46 46     || (c&0xFFFFF
ad830 38 30 30 29 3d 3d 30 78 44 38 30 30 20 20 20 20  800)==0xD800    
ad840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad850 20 20 20 20 20 20 5c 0a 20 20 20 20 20 20 20 20        \.        
ad860 7c 7c 20 28 63 26 30 78 46 46 46 46 46 46 46 45  || (c&0xFFFFFFFE
ad870 29 3d 3d 30 78 46 46 46 45 20 29 7b 20 20 63 20  )==0xFFFE ){  c 
ad880 3d 20 30 78 46 46 46 44 3b 20 7d 20 20 20 20 20  = 0xFFFD; }     
ad890 20 20 20 5c 0a 20 20 7d 0a 53 51 4c 49 54 45 5f     \.  }.SQLITE_
ad8a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
ad8b0 74 65 33 55 74 66 38 52 65 61 64 28 0a 20 20 63  te3Utf8Read(.  c
ad8c0 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
ad8d0 61 72 20 2a 7a 49 6e 2c 20 20 20 20 20 20 20 2f  ar *zIn,       /
ad8e0 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20  * First byte of 
ad8f0 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 20  UTF-8 character 
ad900 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  */.  const unsig
ad910 6e 65 64 20 63 68 61 72 20 2a 2a 70 7a 4e 65 78  ned char **pzNex
ad920 74 20 20 20 20 2f 2a 20 57 72 69 74 65 20 66 69  t    /* Write fi
ad930 72 73 74 20 62 79 74 65 20 70 61 73 74 20 55 54  rst byte past UT
ad940 46 2d 38 20 63 68 61 72 20 68 65 72 65 20 2a 2f  F-8 char here */
ad950 0a 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 0a 20 20  .){.  int c;..  
ad960 2f 2a 20 53 61 6d 65 20 61 73 20 52 45 41 44 5f  /* Same as READ_
ad970 55 54 46 38 28 29 20 61 62 6f 76 65 20 62 75 74  UTF8() above but
ad980 20 77 69 74 68 6f 75 74 20 74 68 65 20 7a 54 65   without the zTe
ad990 72 6d 20 70 61 72 61 6d 65 74 65 72 2e 0a 20 20  rm parameter..  
ad9a0 2a 2a 20 46 6f 72 20 74 68 69 73 20 72 6f 75 74  ** For this rout
ad9b0 69 6e 65 2c 20 77 65 20 61 73 73 75 6d 65 20 74  ine, we assume t
ad9c0 68 65 20 55 54 46 38 20 73 74 72 69 6e 67 20 69  he UTF8 string i
ad9d0 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d 74 65  s always zero-te
ad9e0 72 6d 69 6e 61 74 65 64 2e 0a 20 20 2a 2f 0a 20  rminated..  */. 
ad9f0 20 63 20 3d 20 2a 28 7a 49 6e 2b 2b 29 3b 0a 20   c = *(zIn++);. 
ada00 20 69 66 28 20 63 3e 3d 30 78 63 30 20 29 7b 0a   if( c>=0xc0 ){.
ada10 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55      c = sqlite3U
ada20 74 66 38 54 72 61 6e 73 31 5b 63 2d 30 78 63 30  tf8Trans1[c-0xc0
ada30 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 2a  ];.    while( (*
ada40 7a 49 6e 20 26 20 30 78 63 30 29 3d 3d 30 78 38  zIn & 0xc0)==0x8
ada50 30 20 29 7b 0a 20 20 20 20 20 20 63 20 3d 20 28  0 ){.      c = (
ada60 63 3c 3c 36 29 20 2b 20 28 30 78 33 66 20 26 20  c<<6) + (0x3f & 
ada70 2a 28 7a 49 6e 2b 2b 29 29 3b 0a 20 20 20 20 7d  *(zIn++));.    }
ada80 0a 20 20 20 20 69 66 28 20 63 3c 30 78 38 30 0a  .    if( c<0x80.
ada90 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30 78          || (c&0x
adaa0 46 46 46 46 46 38 30 30 29 3d 3d 30 78 44 38 30  FFFFF800)==0xD80
adab0 30 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26  0.        || (c&
adac0 30 78 46 46 46 46 46 46 46 45 29 3d 3d 30 78 46  0xFFFFFFFE)==0xF
adad0 46 46 45 20 29 7b 20 20 63 20 3d 20 30 78 46 46  FFE ){  c = 0xFF
adae0 46 44 3b 20 7d 0a 20 20 7d 0a 20 20 2a 70 7a 4e  FD; }.  }.  *pzN
adaf0 65 78 74 20 3d 20 7a 49 6e 3b 0a 20 20 72 65 74  ext = zIn;.  ret
adb00 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 0a 0a 2f 2a 0a  urn c;.}...../*.
adb10 2a 2a 20 49 66 20 74 68 65 20 54 52 41 4e 53 4c  ** If the TRANSL
adb20 41 54 45 5f 54 52 41 43 45 20 6d 61 63 72 6f 20  ATE_TRACE macro 
adb30 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 20  is defined, the 
adb40 76 61 6c 75 65 20 6f 66 20 65 61 63 68 20 4d 65  value of each Me
adb50 6d 20 69 73 0a 2a 2a 20 70 72 69 6e 74 65 64 20  m is.** printed 
adb60 6f 6e 20 73 74 64 65 72 72 20 6f 6e 20 74 68 65  on stderr on the
adb70 20 77 61 79 20 69 6e 74 6f 20 61 6e 64 20 6f 75   way into and ou
adb80 74 20 6f 66 20 73 71 6c 69 74 65 33 56 64 62 65  t of sqlite3Vdbe
adb90 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 29 2e 0a  MemTranslate()..
adba0 2a 2f 20 0a 2f 2a 20 23 64 65 66 69 6e 65 20 54  */ ./* #define T
adbb0 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 20 31  RANSLATE_TRACE 1
adbc0 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
adbd0 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f  ITE_OMIT_UTF16./
adbe0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
adbf0 65 20 74 72 61 6e 73 66 6f 72 6d 73 20 74 68 65  e transforms the
adc00 20 69 6e 74 65 72 6e 61 6c 20 74 65 78 74 20 65   internal text e
adc10 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 20  ncoding used by 
adc20 70 4d 65 6d 20 74 6f 0a 2a 2a 20 64 65 73 69 72  pMem to.** desir
adc30 65 64 45 6e 63 2e 20 49 74 20 69 73 20 61 6e 20  edEnc. It is an 
adc40 65 72 72 6f 72 20 69 66 20 74 68 65 20 73 74 72  error if the str
adc50 69 6e 67 20 69 73 20 61 6c 72 65 61 64 79 20 6f  ing is already o
adc60 66 20 74 68 65 20 64 65 73 69 72 65 64 0a 2a 2a  f the desired.**
adc70 20 65 6e 63 6f 64 69 6e 67 2c 20 6f 72 20 69 66   encoding, or if
adc80 20 2a 70 4d 65 6d 20 64 6f 65 73 20 6e 6f 74 20   *pMem does not 
adc90 63 6f 6e 74 61 69 6e 20 61 20 73 74 72 69 6e 67  contain a string
adca0 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54   value..*/.SQLIT
adcb0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
adcc0 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e  lite3VdbeMemTran
adcd0 73 6c 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c  slate(Mem *pMem,
adce0 20 75 38 20 64 65 73 69 72 65 64 45 6e 63 29 7b   u8 desiredEnc){
adcf0 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20  .  int len;     
add00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
add10 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68  * Maximum length
add20 20 6f 66 20 6f 75 74 70 75 74 20 73 74 72 69 6e   of output strin
add30 67 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20  g in bytes */.  
add40 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
add50 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  Out;            
add60 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20        /* Output 
add70 62 75 66 66 65 72 20 2a 2f 0a 20 20 75 6e 73 69  buffer */.  unsi
add80 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 3b 20  gned char *zIn; 
add90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adda0 20 20 2f 2a 20 49 6e 70 75 74 20 69 74 65 72 61    /* Input itera
addb0 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  tor */.  unsigne
addc0 64 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b 20 20  d char *zTerm;  
addd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
adde0 2a 20 45 6e 64 20 6f 66 20 69 6e 70 75 74 20 2a  * End of input *
addf0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
ade00 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20  r *z;           
ade10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74            /* Out
ade20 70 75 74 20 69 74 65 72 61 74 6f 72 20 2a 2f 0a  put iterator */.
ade30 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63    unsigned int c
ade40 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  ;..  assert( pMe
ade50 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  m->db==0 || sqli
ade60 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
ade70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  Mem->db->mutex) 
ade80 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  );.  assert( pMe
ade90 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72  m->flags&MEM_Str
adea0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d   );.  assert( pM
adeb0 65 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 72 65 64  em->enc!=desired
adec0 45 6e 63 20 29 3b 0a 20 20 61 73 73 65 72 74 28  Enc );.  assert(
aded0 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 30 20 29 3b   pMem->enc!=0 );
adee0 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d  .  assert( pMem-
adef0 3e 6e 3e 3d 30 20 29 3b 0a 0a 23 69 66 20 64 65  >n>=0 );..#if de
adf00 66 69 6e 65 64 28 54 52 41 4e 53 4c 41 54 45 5f  fined(TRANSLATE_
adf10 54 52 41 43 45 29 20 26 26 20 64 65 66 69 6e 65  TRACE) && define
adf20 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a  d(SQLITE_DEBUG).
adf30 20 20 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75    {.    char zBu
adf40 66 5b 31 30 30 5d 3b 0a 20 20 20 20 73 71 6c 69  f[100];.    sqli
adf50 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79  te3VdbeMemPretty
adf60 50 72 69 6e 74 28 70 4d 65 6d 2c 20 7a 42 75 66  Print(pMem, zBuf
adf70 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73  );.    fprintf(s
adf80 74 64 65 72 72 2c 20 22 49 4e 50 55 54 3a 20 20  tderr, "INPUT:  
adf90 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20 20  %s\n", zBuf);.  
adfa0 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49  }.#endif..  /* I
adfb0 66 20 74 68 65 20 74 72 61 6e 73 6c 61 74 69 6f  f the translatio
adfc0 6e 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46  n is between UTF
adfd0 2d 31 36 20 6c 69 74 74 6c 65 20 61 6e 64 20 62  -16 little and b
adfe0 69 67 20 65 6e 64 69 61 6e 2c 20 74 68 65 6e 20  ig endian, then 
adff0 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 69  .  ** all that i
ae000 73 20 72 65 71 75 69 72 65 64 20 69 73 20 74 6f  s required is to
ae010 20 73 77 61 70 20 74 68 65 20 62 79 74 65 20 6f   swap the byte o
ae020 72 64 65 72 2e 20 54 68 69 73 20 63 61 73 65 20  rder. This case 
ae030 69 73 20 68 61 6e 64 6c 65 64 0a 20 20 2a 2a 20  is handled.  ** 
ae040 64 69 66 66 65 72 65 6e 74 6c 79 20 66 72 6f 6d  differently from
ae050 20 74 68 65 20 6f 74 68 65 72 73 2e 0a 20 20 2a   the others..  *
ae060 2f 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e  /.  if( pMem->en
ae070 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 26  c!=SQLITE_UTF8 &
ae080 26 20 64 65 73 69 72 65 64 45 6e 63 21 3d 53 51  & desiredEnc!=SQ
ae090 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20  LITE_UTF8 ){.   
ae0a0 20 75 38 20 74 65 6d 70 3b 0a 20 20 20 20 69 6e   u8 temp;.    in
ae0b0 74 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 73  t rc;.    rc = s
ae0c0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b  qlite3VdbeMemMak
ae0d0 65 57 72 69 74 65 61 62 6c 65 28 70 4d 65 6d 29  eWriteable(pMem)
ae0e0 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
ae0f0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
ae100 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
ae110 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20  ITE_NOMEM );.   
ae120 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
ae130 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
ae140 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d 65    zIn = (u8*)pMe
ae150 6d 2d 3e 7a 3b 0a 20 20 20 20 7a 54 65 72 6d 20  m->z;.    zTerm 
ae160 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 26 7e  = &zIn[pMem->n&~
ae170 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 7a  1];.    while( z
ae180 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20  In<zTerm ){.    
ae190 20 20 74 65 6d 70 20 3d 20 2a 7a 49 6e 3b 0a 20    temp = *zIn;. 
ae1a0 20 20 20 20 20 2a 7a 49 6e 20 3d 20 2a 28 7a 49       *zIn = *(zI
ae1b0 6e 2b 31 29 3b 0a 20 20 20 20 20 20 7a 49 6e 2b  n+1);.      zIn+
ae1c0 2b 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 2b 2b 20  +;.      *zIn++ 
ae1d0 3d 20 74 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 20  = temp;.    }.  
ae1e0 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 65    pMem->enc = de
ae1f0 73 69 72 65 64 45 6e 63 3b 0a 20 20 20 20 67 6f  siredEnc;.    go
ae200 74 6f 20 74 72 61 6e 73 6c 61 74 65 5f 6f 75 74  to translate_out
ae210 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20  ;.  }..  /* Set 
ae220 6c 65 6e 20 74 6f 20 74 68 65 20 6d 61 78 69 6d  len to the maxim
ae230 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  um number of byt
ae240 65 73 20 72 65 71 75 69 72 65 64 20 69 6e 20 74  es required in t
ae250 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72  he output buffer
ae260 2e 20 2a 2f 0a 20 20 69 66 28 20 64 65 73 69 72  . */.  if( desir
ae270 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  edEnc==SQLITE_UT
ae280 46 38 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 65  F8 ){.    /* Whe
ae290 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 6f  n converting fro
ae2a0 6d 20 55 54 46 2d 31 36 2c 20 74 68 65 20 6d 61  m UTF-16, the ma
ae2b0 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 72 65 73  ximum growth res
ae2c0 75 6c 74 73 20 66 72 6f 6d 0a 20 20 20 20 2a 2a  ults from.    **
ae2d0 20 74 72 61 6e 73 6c 61 74 69 6e 67 20 61 20 32   translating a 2
ae2e0 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20  -byte character 
ae2f0 74 6f 20 61 20 34 2d 62 79 74 65 20 55 54 46 2d  to a 4-byte UTF-
ae300 38 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20  8 character..   
ae310 20 2a 2a 20 41 20 73 69 6e 67 6c 65 20 62 79 74   ** A single byt
ae320 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f  e is required fo
ae330 72 20 74 68 65 20 6f 75 74 70 75 74 20 73 74 72  r the output str
ae340 69 6e 67 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d 74  ing.    ** nul-t
ae350 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20 2a  erminator..    *
ae360 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 26 3d  /.    pMem->n &=
ae370 20 7e 31 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 70   ~1;.    len = p
ae380 4d 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 31 3b 0a  Mem->n * 2 + 1;.
ae390 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
ae3a0 57 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20  When converting 
ae3b0 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 55 54  from UTF-8 to UT
ae3c0 46 2d 31 36 20 74 68 65 20 6d 61 78 69 6d 75 6d  F-16 the maximum
ae3d0 20 67 72 6f 77 74 68 20 69 73 20 63 61 75 73 65   growth is cause
ae3e0 64 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 61 20  d.    ** when a 
ae3f0 31 2d 62 79 74 65 20 55 54 46 2d 38 20 63 68 61  1-byte UTF-8 cha
ae400 72 61 63 74 65 72 20 69 73 20 74 72 61 6e 73 6c  racter is transl
ae410 61 74 65 64 20 69 6e 74 6f 20 61 20 32 2d 62 79  ated into a 2-by
ae420 74 65 20 55 54 46 2d 31 36 0a 20 20 20 20 2a 2a  te UTF-16.    **
ae430 20 63 68 61 72 61 63 74 65 72 2e 20 54 77 6f 20   character. Two 
ae440 62 79 74 65 73 20 61 72 65 20 72 65 71 75 69 72  bytes are requir
ae450 65 64 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74  ed in the output
ae460 20 62 75 66 66 65 72 20 66 6f 72 20 74 68 65 0a   buffer for the.
ae470 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69      ** nul-termi
ae480 6e 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20  nator..    */.  
ae490 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 20    len = pMem->n 
ae4a0 2a 20 32 20 2b 20 32 3b 0a 20 20 7d 0a 0a 20 20  * 2 + 2;.  }..  
ae4b0 2f 2a 20 53 65 74 20 7a 49 6e 20 74 6f 20 70 6f  /* Set zIn to po
ae4c0 69 6e 74 20 61 74 20 74 68 65 20 73 74 61 72 74  int at the start
ae4d0 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 62 75   of the input bu
ae4e0 66 66 65 72 20 61 6e 64 20 7a 54 65 72 6d 20 74  ffer and zTerm t
ae4f0 6f 20 70 6f 69 6e 74 20 31 0a 20 20 2a 2a 20 62  o point 1.  ** b
ae500 79 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64  yte past the end
ae510 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 72 69  ..  **.  ** Vari
ae520 61 62 6c 65 20 7a 4f 75 74 20 69 73 20 73 65 74  able zOut is set
ae530 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65   to point at the
ae540 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2c 20   output buffer, 
ae550 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 0a 20  space obtained. 
ae560 20 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 33   ** from sqlite3
ae570 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 20 20 2a 2f 0a  _malloc()..  */.
ae580 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d 65    zIn = (u8*)pMe
ae590 6d 2d 3e 7a 3b 0a 20 20 7a 54 65 72 6d 20 3d 20  m->z;.  zTerm = 
ae5a0 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 5d 3b 0a 20  &zIn[pMem->n];. 
ae5b0 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 44   zOut = sqlite3D
ae5c0 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d 2d  bMallocRaw(pMem-
ae5d0 3e 64 62 2c 20 6c 65 6e 29 3b 0a 20 20 69 66 28  >db, len);.  if(
ae5e0 20 21 7a 4f 75 74 20 29 7b 0a 20 20 20 20 72 65   !zOut ){.    re
ae5f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
ae600 4d 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 7a 4f 75  M;.  }.  z = zOu
ae610 74 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e  t;..  if( pMem->
ae620 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38  enc==SQLITE_UTF8
ae630 20 29 7b 0a 20 20 20 20 69 66 28 20 64 65 73 69   ){.    if( desi
ae640 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  redEnc==SQLITE_U
ae650 54 46 31 36 4c 45 20 29 7b 0a 20 20 20 20 20 20  TF16LE ){.      
ae660 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54 46 2d  /* UTF-8 -> UTF-
ae670 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 6e  16 Little-endian
ae680 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28   */.      while(
ae690 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20   zIn<zTerm ){.  
ae6a0 20 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 71 6c        /* c = sql
ae6b0 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 6e  ite3Utf8Read(zIn
ae6c0 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 20  , zTerm, (const 
ae6d0 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f 0a 20  u8**)&zIn); */. 
ae6e0 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 38         READ_UTF8
ae6f0 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 3b  (zIn, zTerm, c);
ae700 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55  .        WRITE_U
ae710 54 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 20  TF16LE(z, c);.  
ae720 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
ae730 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64  .      assert( d
ae740 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54  esiredEnc==SQLIT
ae750 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 20  E_UTF16BE );.   
ae760 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55     /* UTF-8 -> U
ae770 54 46 2d 31 36 20 42 69 67 2d 65 6e 64 69 61 6e  TF-16 Big-endian
ae780 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28   */.      while(
ae790 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20   zIn<zTerm ){.  
ae7a0 20 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 71 6c        /* c = sql
ae7b0 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 6e  ite3Utf8Read(zIn
ae7c0 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 20  , zTerm, (const 
ae7d0 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f 0a 20  u8**)&zIn); */. 
ae7e0 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 38         READ_UTF8
ae7f0 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 3b  (zIn, zTerm, c);
ae800 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55  .        WRITE_U
ae810 54 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20 20  TF16BE(z, c);.  
ae820 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
ae830 70 4d 65 6d 2d 3e 6e 20 3d 20 28 69 6e 74 29 28  pMem->n = (int)(
ae840 7a 20 2d 20 7a 4f 75 74 29 3b 0a 20 20 20 20 2a  z - zOut);.    *
ae850 7a 2b 2b 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  z++ = 0;.  }else
ae860 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 65  {.    assert( de
ae870 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45  siredEnc==SQLITE
ae880 5f 55 54 46 38 20 29 3b 0a 20 20 20 20 69 66 28  _UTF8 );.    if(
ae890 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49   pMem->enc==SQLI
ae8a0 54 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20 20  TE_UTF16LE ){.  
ae8b0 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 4c 69      /* UTF-16 Li
ae8c0 74 74 6c 65 2d 65 6e 64 69 61 6e 20 2d 3e 20 55  ttle-endian -> U
ae8d0 54 46 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68  TF-8 */.      wh
ae8e0 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29  ile( zIn<zTerm )
ae8f0 7b 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55  {.        READ_U
ae900 54 46 31 36 4c 45 28 7a 49 6e 2c 20 63 29 3b 20  TF16LE(zIn, c); 
ae910 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55  .        WRITE_U
ae920 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20  TF8(z, c);.     
ae930 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
ae940 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 42 69      /* UTF-16 Bi
ae950 67 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46 2d  g-endian -> UTF-
ae960 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65  8 */.      while
ae970 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20  ( zIn<zTerm ){. 
ae980 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31         READ_UTF1
ae990 36 42 45 28 7a 49 6e 2c 20 63 29 3b 20 0a 20 20  6BE(zIn, c); .  
ae9a0 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 46 38        WRITE_UTF8
ae9b0 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 7d 0a  (z, c);.      }.
ae9c0 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e      }.    pMem->
ae9d0 6e 20 3d 20 28 69 6e 74 29 28 7a 20 2d 20 7a 4f  n = (int)(z - zO
ae9e0 75 74 29 3b 0a 20 20 7d 0a 20 20 2a 7a 20 3d 20  ut);.  }.  *z = 
ae9f0 30 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d  0;.  assert( (pM
aea00 65 6d 2d 3e 6e 2b 28 64 65 73 69 72 65 64 45 6e  em->n+(desiredEn
aea10 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31  c==SQLITE_UTF8?1
aea20 3a 32 29 29 3c 3d 6c 65 6e 20 29 3b 0a 0a 20 20  :2))<=len );..  
aea30 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
aea40 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70  lease(pMem);.  p
aea50 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28  Mem->flags &= ~(
aea60 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44  MEM_Static|MEM_D
aea70 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20  yn|MEM_Ephem);. 
aea80 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 65 73   pMem->enc = des
aea90 69 72 65 64 45 6e 63 3b 0a 20 20 70 4d 65 6d 2d  iredEnc;.  pMem-
aeaa0 3e 66 6c 61 67 73 20 7c 3d 20 28 4d 45 4d 5f 54  >flags |= (MEM_T
aeab0 65 72 6d 7c 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20  erm|MEM_Dyn);.  
aeac0 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a  pMem->z = (char*
aead0 29 7a 4f 75 74 3b 0a 20 20 70 4d 65 6d 2d 3e 7a  )zOut;.  pMem->z
aeae0 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e 7a  Malloc = pMem->z
aeaf0 3b 0a 0a 74 72 61 6e 73 6c 61 74 65 5f 6f 75 74  ;..translate_out
aeb00 3a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 52  :.#if defined(TR
aeb10 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 29 20 26  ANSLATE_TRACE) &
aeb20 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  & defined(SQLITE
aeb30 5f 44 45 42 55 47 29 0a 20 20 7b 0a 20 20 20 20  _DEBUG).  {.    
aeb40 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a  char zBuf[100];.
aeb50 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
aeb60 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 70 4d  emPrettyPrint(pM
aeb70 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 66  em, zBuf);.    f
aeb80 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22  printf(stderr, "
aeb90 4f 55 54 50 55 54 3a 20 25 73 5c 6e 22 2c 20 7a  OUTPUT: %s\n", z
aeba0 42 75 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  Buf);.  }.#endif
aebb0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
aebc0 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  _OK;.}../*.** Th
aebd0 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b  is routine check
aebe0 73 20 66 6f 72 20 61 20 62 79 74 65 2d 6f 72 64  s for a byte-ord
aebf0 65 72 20 6d 61 72 6b 20 61 74 20 74 68 65 20 62  er mark at the b
aec00 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20  eginning of the 
aec10 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 69 6e  .** UTF-16 strin
aec20 67 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65  g stored in *pMe
aec30 6d 2e 20 49 66 20 6f 6e 65 20 69 73 20 70 72 65  m. If one is pre
aec40 73 65 6e 74 2c 20 69 74 20 69 73 20 72 65 6d 6f  sent, it is remo
aec50 76 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 20 65  ved and.** the e
aec60 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 4d  ncoding of the M
aec70 65 6d 20 61 64 6a 75 73 74 65 64 2e 20 54 68 69  em adjusted. Thi
aec80 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e  s routine does n
aec90 6f 74 20 64 6f 20 61 6e 79 0a 2a 2a 20 62 79 74  ot do any.** byt
aeca0 65 2d 73 77 61 70 70 69 6e 67 2c 20 69 74 20 6a  e-swapping, it j
aecb0 75 73 74 20 73 65 74 73 20 4d 65 6d 2e 65 6e 63  ust sets Mem.enc
aecc0 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a   appropriately..
aecd0 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f 63 61  **.** The alloca
aece0 74 69 6f 6e 20 28 73 74 61 74 69 63 2c 20 64 79  tion (static, dy
aecf0 6e 61 6d 69 63 20 65 74 63 2e 29 20 61 6e 64 20  namic etc.) and 
aed00 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20  encoding of the 
aed10 4d 65 6d 20 6d 61 79 20 62 65 0a 2a 2a 20 63 68  Mem may be.** ch
aed20 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 66 75  anged by this fu
aed30 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  nction..*/.SQLIT
aed40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
aed50 6c 69 74 65 33 56 64 62 65 4d 65 6d 48 61 6e 64  lite3VdbeMemHand
aed60 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d 65 6d 29  leBom(Mem *pMem)
aed70 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
aed80 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 62 6f 6d  ITE_OK;.  u8 bom
aed90 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28   = 0;..  assert(
aeda0 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 20   pMem->n>=0 );. 
aedb0 20 69 66 28 20 70 4d 65 6d 2d 3e 6e 3e 31 20 29   if( pMem->n>1 )
aedc0 7b 0a 20 20 20 20 75 38 20 62 31 20 3d 20 2a 28  {.    u8 b1 = *(
aedd0 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20  u8 *)pMem->z;.  
aede0 20 20 75 38 20 62 32 20 3d 20 2a 28 28 28 75 38    u8 b2 = *(((u8
aedf0 20 2a 29 70 4d 65 6d 2d 3e 7a 29 20 2b 20 31 29   *)pMem->z) + 1)
aee00 3b 0a 20 20 20 20 69 66 28 20 62 31 3d 3d 30 78  ;.    if( b1==0x
aee10 46 45 20 26 26 20 62 32 3d 3d 30 78 46 46 20 29  FE && b2==0xFF )
aee20 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d 20 53 51  {.      bom = SQ
aee30 4c 49 54 45 5f 55 54 46 31 36 42 45 3b 0a 20 20  LITE_UTF16BE;.  
aee40 20 20 7d 0a 20 20 20 20 69 66 28 20 62 31 3d 3d    }.    if( b1==
aee50 30 78 46 46 20 26 26 20 62 32 3d 3d 30 78 46 45  0xFF && b2==0xFE
aee60 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d 20   ){.      bom = 
aee70 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 3b 0a  SQLITE_UTF16LE;.
aee80 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 69      }.  }.  .  i
aee90 66 28 20 62 6f 6d 20 29 7b 0a 20 20 20 20 72 63  f( bom ){.    rc
aeea0 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65   = sqlite3VdbeMe
aeeb0 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70  mMakeWriteable(p
aeec0 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63  Mem);.    if( rc
aeed0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
aeee0 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 2d 3d 20       pMem->n -= 
aeef0 32 3b 0a 20 20 20 20 20 20 6d 65 6d 6d 6f 76 65  2;.      memmove
aef00 28 70 4d 65 6d 2d 3e 7a 2c 20 26 70 4d 65 6d 2d  (pMem->z, &pMem-
aef10 3e 7a 5b 32 5d 2c 20 70 4d 65 6d 2d 3e 6e 29 3b  >z[2], pMem->n);
aef20 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70  .      pMem->z[p
aef30 4d 65 6d 2d 3e 6e 5d 20 3d 20 27 5c 30 27 3b 0a  Mem->n] = '\0';.
aef40 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d        pMem->z[pM
aef50 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 27 5c 30 27 3b  em->n+1] = '\0';
aef60 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  .      pMem->fla
aef70 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a  gs |= MEM_Term;.
aef80 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20        pMem->enc 
aef90 3d 20 62 6f 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d  = bom;.    }.  }
aefa0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
aefb0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
aefc0 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a  _OMIT_UTF16 */..
aefd0 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 61 20 55 54  /*.** pZ is a UT
aefe0 46 2d 38 20 65 6e 63 6f 64 65 64 20 75 6e 69 63  F-8 encoded unic
aeff0 6f 64 65 20 73 74 72 69 6e 67 2e 20 49 66 20 6e  ode string. If n
af000 42 79 74 65 20 69 73 20 6c 65 73 73 20 74 68 61  Byte is less tha
af010 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 74 75 72  n zero,.** retur
af020 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
af030 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63 74 65  unicode characte
af040 72 73 20 69 6e 20 70 5a 20 75 70 20 74 6f 20 28  rs in pZ up to (
af050 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75 64 69 6e  but not includin
af060 67 29 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20  g).** the first 
af070 30 78 30 30 20 62 79 74 65 2e 20 49 66 20 6e 42  0x00 byte. If nB
af080 79 74 65 20 69 73 20 6e 6f 74 20 6c 65 73 73 20  yte is not less 
af090 74 68 61 6e 20 7a 65 72 6f 2c 20 72 65 74 75 72  than zero, retur
af0a0 6e 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20  n the.** number 
af0b0 6f 66 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61  of unicode chara
af0c0 63 74 65 72 73 20 69 6e 20 74 68 65 20 66 69 72  cters in the fir
af0d0 73 74 20 6e 42 79 74 65 20 6f 66 20 70 5a 20 28  st nByte of pZ (
af0e0 6f 72 20 75 70 20 74 6f 20 0a 2a 2a 20 74 68 65  or up to .** the
af0f0 20 66 69 72 73 74 20 30 78 30 30 2c 20 77 68 69   first 0x00, whi
af100 63 68 65 76 65 72 20 63 6f 6d 65 73 20 66 69 72  chever comes fir
af110 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  st)..*/.SQLITE_P
af120 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
af130 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 63 6f  e3Utf8CharLen(co
af140 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c 20 69  nst char *zIn, i
af150 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e 74  nt nByte){.  int
af160 20 72 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20   r = 0;.  const 
af170 75 38 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 75  u8 *z = (const u
af180 38 2a 29 7a 49 6e 3b 0a 20 20 63 6f 6e 73 74 20  8*)zIn;.  const 
af190 75 38 20 2a 7a 54 65 72 6d 3b 0a 20 20 69 66 28  u8 *zTerm;.  if(
af1a0 20 6e 42 79 74 65 3e 3d 30 20 29 7b 0a 20 20 20   nByte>=0 ){.   
af1b0 20 7a 54 65 72 6d 20 3d 20 26 7a 5b 6e 42 79 74   zTerm = &z[nByt
af1c0 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  e];.  }else{.   
af1d0 20 7a 54 65 72 6d 20 3d 20 28 63 6f 6e 73 74 20   zTerm = (const 
af1e0 75 38 2a 29 28 2d 31 29 3b 0a 20 20 7d 0a 20 20  u8*)(-1);.  }.  
af1f0 61 73 73 65 72 74 28 20 7a 3c 3d 7a 54 65 72 6d  assert( z<=zTerm
af200 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 21   );.  while( *z!
af210 3d 30 20 26 26 20 7a 3c 7a 54 65 72 6d 20 29 7b  =0 && z<zTerm ){
af220 0a 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50  .    SQLITE_SKIP
af230 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 72 2b  _UTF8(z);.    r+
af240 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  +;.  }.  return 
af250 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 74 65  r;.}../* This te
af260 73 74 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e  st function is n
af270 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 75 73 65  ot currently use
af280 64 20 62 79 20 74 68 65 20 61 75 74 6f 6d 61 74  d by the automat
af290 65 64 20 74 65 73 74 2d 73 75 69 74 65 2e 20 0a  ed test-suite. .
af2a0 2a 2a 20 48 65 6e 63 65 20 69 74 20 69 73 20 6f  ** Hence it is o
af2b0 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 6e  nly available in
af2c0 20 64 65 62 75 67 20 62 75 69 6c 64 73 2e 0a 2a   debug builds..*
af2d0 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  /.#if defined(SQ
af2e0 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 64 65  LITE_TEST) && de
af2f0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
af300 55 47 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c  UG)./*.** Transl
af310 61 74 65 20 55 54 46 2d 38 20 74 6f 20 55 54 46  ate UTF-8 to UTF
af320 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68  -8..**.** This h
af330 61 73 20 74 68 65 20 65 66 66 65 63 74 20 6f 66  as the effect of
af340 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 74 68 61   making sure tha
af350 74 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20  t the string is 
af360 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20 55  well-formed.** U
af370 54 46 2d 38 2e 20 20 4d 69 73 63 6f 64 65 64 20  TF-8.  Miscoded 
af380 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20 72  characters are r
af390 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  emoved..**.** Th
af3a0 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 69 73  e translation is
af3b0 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 20 28   done in-place (
af3c0 73 69 6e 63 65 20 69 74 20 69 73 20 69 6d 70 6f  since it is impo
af3d0 73 73 69 62 6c 65 20 66 6f 72 20 74 68 65 0a 2a  ssible for the.*
af3e0 2a 20 63 6f 72 72 65 63 74 20 55 54 46 2d 38 20  * correct UTF-8 
af3f0 65 6e 63 6f 64 69 6e 67 20 74 6f 20 62 65 20 6c  encoding to be l
af400 6f 6e 67 65 72 20 74 68 61 6e 20 61 20 6d 61 6c  onger than a mal
af410 66 6f 72 6d 65 64 20 65 6e 63 6f 64 69 6e 67 29  formed encoding)
af420 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
af430 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55  ATE int sqlite3U
af440 74 66 38 54 6f 38 28 75 6e 73 69 67 6e 65 64 20  tf8To8(unsigned 
af450 63 68 61 72 20 2a 7a 49 6e 29 7b 0a 20 20 75 6e  char *zIn){.  un
af460 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 4f 75  signed char *zOu
af470 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 6e 73 69 67  t = zIn;.  unsig
af480 6e 65 64 20 63 68 61 72 20 2a 7a 53 74 61 72 74  ned char *zStart
af490 20 3d 20 7a 49 6e 3b 0a 20 20 75 33 32 20 63 3b   = zIn;.  u32 c;
af4a0 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e 5b 30  ..  while( zIn[0
af4b0 5d 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 71 6c  ] ){.    c = sql
af4c0 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 6e  ite3Utf8Read(zIn
af4d0 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a  , (const u8**)&z
af4e0 49 6e 29 3b 0a 20 20 20 20 69 66 28 20 63 21 3d  In);.    if( c!=
af4f0 30 78 66 66 66 64 20 29 7b 0a 20 20 20 20 20 20  0xfffd ){.      
af500 57 52 49 54 45 5f 55 54 46 38 28 7a 4f 75 74 2c  WRITE_UTF8(zOut,
af510 20 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20   c);.    }.  }. 
af520 20 2a 7a 4f 75 74 20 3d 20 30 3b 0a 20 20 72 65   *zOut = 0;.  re
af530 74 75 72 6e 20 28 69 6e 74 29 28 7a 4f 75 74 20  turn (int)(zOut 
af540 2d 20 7a 53 74 61 72 74 29 3b 0a 7d 0a 23 65 6e  - zStart);.}.#en
af550 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
af560 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f  ITE_OMIT_UTF16./
af570 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55  *.** Convert a U
af580 54 46 2d 31 36 20 73 74 72 69 6e 67 20 69 6e 20  TF-16 string in 
af590 74 68 65 20 6e 61 74 69 76 65 20 65 6e 63 6f 64  the native encod
af5a0 69 6e 67 20 69 6e 74 6f 20 61 20 55 54 46 2d 38  ing into a UTF-8
af5b0 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 4d 65 6d 6f   string..** Memo
af5c0 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 55  ry to hold the U
af5d0 54 46 2d 38 20 73 74 72 69 6e 67 20 69 73 20 6f  TF-8 string is o
af5e0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c  btained from sql
af5f0 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 61 6e 64 20  ite3_malloc and 
af600 6d 75 73 74 0a 2a 2a 20 62 65 20 66 72 65 65 64  must.** be freed
af610 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20   by the calling 
af620 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
af630 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64  NULL is returned
af640 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20   if there is an 
af650 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72  allocation error
af660 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
af670 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
af680 33 55 74 66 31 36 74 6f 38 28 73 71 6c 69 74 65  3Utf16to8(sqlite
af690 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 76 6f 69  3 *db, const voi
af6a0 64 20 2a 7a 2c 20 69 6e 74 20 6e 42 79 74 65 29  d *z, int nByte)
af6b0 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d 65 6d  {.  Mem m;.  mem
af6c0 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65 6f  set(&m, 0, sizeo
af6d0 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20 3d 20  f(m));.  m.db = 
af6e0 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  db;.  sqlite3Vdb
af6f0 65 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c 20 7a  eMemSetStr(&m, z
af700 2c 20 6e 42 79 74 65 2c 20 53 51 4c 49 54 45 5f  , nByte, SQLITE_
af710 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53 51 4c  UTF16NATIVE, SQL
af720 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 73  ITE_STATIC);.  s
af730 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
af740 45 6e 63 6f 64 69 6e 67 28 26 6d 2c 20 53 51 4c  Encoding(&m, SQL
af750 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 69 66 28  ITE_UTF8);.  if(
af760 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
af770 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  d ){.    sqlite3
af780 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26  VdbeMemRelease(&
af790 6d 29 3b 0a 20 20 20 20 6d 2e 7a 20 3d 20 30 3b  m);.    m.z = 0;
af7a0 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28  .  }.  assert( (
af7b0 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 65  m.flags & MEM_Te
af7c0 72 6d 29 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61  rm)!=0 || db->ma
af7d0 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
af7e0 61 73 73 65 72 74 28 20 28 6d 2e 66 6c 61 67 73  assert( (m.flags
af7f0 20 26 20 4d 45 4d 5f 53 74 72 29 21 3d 30 20 7c   & MEM_Str)!=0 |
af800 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
af810 65 64 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28  ed );.  return (
af820 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79  m.flags & MEM_Dy
af830 6e 29 21 3d 30 20 3f 20 6d 2e 7a 20 3a 20 73 71  n)!=0 ? m.z : sq
af840 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
af850 2c 20 6d 2e 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  , m.z);.}../*.**
af860 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d 38   Convert a UTF-8
af870 20 73 74 72 69 6e 67 20 74 6f 20 74 68 65 20 55   string to the U
af880 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e 67 20 73  TF-16 encoding s
af890 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61  pecified by para
af8a0 6d 65 74 65 72 0a 2a 2a 20 65 6e 63 2e 20 41 20  meter.** enc. A 
af8b0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e  pointer to the n
af8c0 65 77 20 73 74 72 69 6e 67 20 69 73 20 72 65 74  ew string is ret
af8d0 75 72 6e 65 64 2c 20 61 6e 64 20 74 68 65 20 76  urned, and the v
af8e0 61 6c 75 65 20 6f 66 20 2a 70 6e 4f 75 74 0a 2a  alue of *pnOut.*
af8f0 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  * is set to the 
af900 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 72 65  length of the re
af910 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 6e  turned string in
af920 20 62 79 74 65 73 2e 20 54 68 65 20 63 61 6c 6c   bytes. The call
af930 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 72 72 61 6e   should.** arran
af940 67 65 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74  ge to call sqlit
af950 65 33 44 62 46 72 65 65 28 29 20 6f 6e 20 74 68  e3DbFree() on th
af960 65 20 72 65 74 75 72 6e 65 64 20 70 6f 69 6e 74  e returned point
af970 65 72 20 77 68 65 6e 20 69 74 20 69 73 0a 2a 2a  er when it is.**
af980 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 71 75 69   no longer requi
af990 72 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 61  red..** .** If a
af9a0 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20   malloc failure 
af9b0 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73 20  occurs, NULL is 
af9c0 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65  returned and the
af9d0 20 64 62 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64   db.mallocFailed
af9e0 0a 2a 2a 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2f  .** flag set..*/
af9f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
afa00 4e 41 42 4c 45 5f 53 54 41 54 32 0a 53 51 4c 49  NABLE_STAT2.SQLI
afa10 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20  TE_PRIVATE char 
afa20 2a 73 71 6c 69 74 65 33 55 74 66 38 74 6f 31 36  *sqlite3Utf8to16
afa30 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 75 38  (sqlite3 *db, u8
afa40 20 65 6e 63 2c 20 63 68 61 72 20 2a 7a 2c 20 69   enc, char *z, i
afa50 6e 74 20 6e 2c 20 69 6e 74 20 2a 70 6e 4f 75 74  nt n, int *pnOut
afa60 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d 65  ){.  Mem m;.  me
afa70 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65  mset(&m, 0, size
afa80 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20 3d  of(m));.  m.db =
afa90 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 64   db;.  sqlite3Vd
afaa0 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c 20  beMemSetStr(&m, 
afab0 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46  z, n, SQLITE_UTF
afac0 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  8, SQLITE_STATIC
afad0 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
afae0 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65  VdbeMemTranslate
afaf0 28 26 6d 2c 20 65 6e 63 29 20 29 7b 0a 20 20 20  (&m, enc) ){.   
afb00 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c   assert( db->mal
afb10 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20  locFailed );.   
afb20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
afb30 20 61 73 73 65 72 74 28 20 6d 2e 7a 3d 3d 6d 2e   assert( m.z==m.
afb40 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 2a 70 6e  zMalloc );.  *pn
afb50 4f 75 74 20 3d 20 6d 2e 6e 3b 0a 20 20 72 65 74  Out = m.n;.  ret
afb60 75 72 6e 20 6d 2e 7a 3b 0a 7d 0a 23 65 6e 64 69  urn m.z;.}.#endi
afb70 66 0a 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 61  f../*.** pZ is a
afb80 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20   UTF-16 encoded 
afb90 75 6e 69 63 6f 64 65 20 73 74 72 69 6e 67 20 61  unicode string a
afba0 74 20 6c 65 61 73 74 20 6e 43 68 61 72 20 63 68  t least nChar ch
afbb0 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 0a 2a  aracters long..*
afbc0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
afbd0 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
afbe0 74 68 65 20 66 69 72 73 74 20 6e 43 68 61 72 20  the first nChar 
afbf0 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63 74 65  unicode characte
afc00 72 73 0a 2a 2a 20 69 6e 20 70 5a 2e 20 20 6e 43  rs.** in pZ.  nC
afc10 68 61 72 20 6d 75 73 74 20 62 65 20 6e 6f 6e 2d  har must be non-
afc20 6e 65 67 61 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c  negative..*/.SQL
afc30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
afc40 73 71 6c 69 74 65 33 55 74 66 31 36 42 79 74 65  sqlite3Utf16Byte
afc50 4c 65 6e 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a  Len(const void *
afc60 7a 49 6e 2c 20 69 6e 74 20 6e 43 68 61 72 29 7b  zIn, int nChar){
afc70 0a 20 20 69 6e 74 20 63 3b 0a 20 20 75 6e 73 69  .  int c;.  unsi
afc80 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20  gned char const 
afc90 2a 7a 20 3d 20 7a 49 6e 3b 0a 20 20 69 6e 74 20  *z = zIn;.  int 
afca0 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 53 51 4c  n = 0;.  if( SQL
afcb0 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3d  ITE_UTF16NATIVE=
afcc0 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20  =SQLITE_UTF16BE 
afcd0 29 7b 0a 20 20 20 20 2f 2a 20 55 73 69 6e 67 20  ){.    /* Using 
afce0 61 6e 20 22 69 66 20 28 53 51 4c 49 54 45 5f 55  an "if (SQLITE_U
afcf0 54 46 31 36 4e 41 54 49 56 45 3d 3d 53 51 4c 49  TF16NATIVE==SQLI
afd00 54 45 5f 55 54 46 31 36 42 45 29 22 20 63 6f 6e  TE_UTF16BE)" con
afd10 73 74 72 75 63 74 20 68 65 72 65 0a 20 20 20 20  struct here.    
afd20 2a 2a 20 61 6e 64 20 69 6e 20 6f 74 68 65 72 20  ** and in other 
afd30 70 61 72 74 73 20 6f 66 20 74 68 69 73 20 66 69  parts of this fi
afd40 6c 65 20 6d 65 61 6e 73 20 74 68 61 74 20 61 74  le means that at
afd50 20 6f 6e 65 20 62 72 61 6e 63 68 20 77 69 6c 6c   one branch will
afd60 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 62 65 20 63  .    ** not be c
afd70 6f 76 65 72 65 64 20 62 79 20 63 6f 76 65 72 61  overed by covera
afd80 67 65 20 74 65 73 74 69 6e 67 20 6f 6e 20 61 6e  ge testing on an
afd90 79 20 73 69 6e 67 6c 65 20 68 6f 73 74 2e 20 42  y single host. B
afda0 75 74 20 63 6f 76 65 72 61 67 65 0a 20 20 20 20  ut coverage.    
afdb0 2a 2a 20 77 69 6c 6c 20 62 65 20 63 6f 6d 70 6c  ** will be compl
afdc0 65 74 65 20 69 66 20 74 68 65 20 74 65 73 74 73  ete if the tests
afdd0 20 61 72 65 20 72 75 6e 20 6f 6e 20 62 6f 74 68   are run on both
afde0 20 61 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e   a little-endian
afdf0 20 61 6e 64 20 0a 20 20 20 20 2a 2a 20 62 69 67   and .    ** big
afe00 2d 65 6e 64 69 61 6e 20 68 6f 73 74 2e 20 42 65  -endian host. Be
afe10 63 61 75 73 65 20 62 6f 74 68 20 74 68 65 20 55  cause both the U
afe20 54 46 31 36 4e 41 54 49 56 45 20 61 6e 64 20 53  TF16NATIVE and S
afe30 51 4c 49 54 45 5f 55 54 46 31 36 42 45 0a 20 20  QLITE_UTF16BE.  
afe40 20 20 2a 2a 20 6d 61 63 72 6f 73 20 61 72 65 20    ** macros are 
afe50 63 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f 6d 70  constant at comp
afe60 69 6c 65 20 74 69 6d 65 20 74 68 65 20 63 6f 6d  ile time the com
afe70 70 69 6c 65 72 20 63 61 6e 20 64 65 74 65 72 6d  piler can determ
afe80 69 6e 65 0a 20 20 20 20 2a 2a 20 77 68 69 63 68  ine.    ** which
afe90 20 62 72 61 6e 63 68 20 77 69 6c 6c 20 62 65 20   branch will be 
afea0 66 6f 6c 6c 6f 77 65 64 2e 20 49 74 20 69 73 20  followed. It is 
afeb0 74 68 65 72 65 66 6f 72 65 20 61 73 73 75 6d 65  therefore assume
afec0 64 20 74 68 61 74 20 6e 6f 20 72 75 6e 74 69 6d  d that no runtim
afed0 65 0a 20 20 20 20 2a 2a 20 70 65 6e 61 6c 74 79  e.    ** penalty
afee0 20 69 73 20 70 61 69 64 20 66 6f 72 20 74 68 69   is paid for thi
afef0 73 20 22 69 66 22 20 73 74 61 74 65 6d 65 6e 74  s "if" statement
aff00 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68 69  ..    */.    whi
aff10 6c 65 28 20 6e 3c 6e 43 68 61 72 20 29 7b 0a 20  le( n<nChar ){. 
aff20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 42       READ_UTF16B
aff30 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 6e  E(z, c);.      n
aff40 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  ++;.    }.  }els
aff50 65 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 3c  e{.    while( n<
aff60 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 20 20 52  nChar ){.      R
aff70 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 2c 20 63  EAD_UTF16LE(z, c
aff80 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 20  );.      n++;.  
aff90 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
affa0 20 28 69 6e 74 29 28 7a 2d 28 75 6e 73 69 67 6e   (int)(z-(unsign
affb0 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 29  ed char const *)
affc0 7a 49 6e 29 3b 0a 7d 0a 0a 23 69 66 20 64 65 66  zIn);.}..#if def
affd0 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
affe0 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  )./*.** This rou
afff0 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66  tine is called f
b0000 72 6f 6d 20 74 68 65 20 54 43 4c 20 74 65 73 74  rom the TCL test
b0010 20 66 75 6e 63 74 69 6f 6e 20 22 74 72 61 6e 73   function "trans
b0020 6c 61 74 65 5f 73 65 6c 66 74 65 73 74 22 2e 0a  late_selftest"..
b0030 2a 2a 20 49 74 20 63 68 65 63 6b 73 20 74 68 61  ** It checks tha
b0040 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 73  t the primitives
b0050 20 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e 67   for serializing
b0060 20 61 6e 64 20 64 65 73 65 72 69 61 6c 69 7a 69   and deserializi
b0070 6e 67 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73  ng.** characters
b0080 20 69 6e 20 65 61 63 68 20 65 6e 63 6f 64 69 6e   in each encodin
b0090 67 20 61 72 65 20 69 6e 76 65 72 73 65 73 20 6f  g are inverses o
b00a0 66 20 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a 2f  f each other..*/
b00b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b00c0 76 6f 69 64 20 73 71 6c 69 74 65 33 55 74 66 53  void sqlite3UtfS
b00d0 65 6c 66 54 65 73 74 28 76 6f 69 64 29 7b 0a 20  elfTest(void){. 
b00e0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 2c   unsigned int i,
b00f0 20 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   t;.  unsigned c
b0100 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 20 20  har zBuf[20];.  
b0110 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
b0120 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73  ;.  int n;.  uns
b0130 69 67 6e 65 64 20 69 6e 74 20 63 3b 0a 0a 20 20  igned int c;..  
b0140 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31  for(i=0; i<0x001
b0150 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20  10000; i++){.   
b0160 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57   z = zBuf;.    W
b0170 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 69 29 3b  RITE_UTF8(z, i);
b0180 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a  .    n = (int)(z
b0190 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 73 65  -zBuf);.    asse
b01a0 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20  rt( n>0 && n<=4 
b01b0 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b  );.    z[0] = 0;
b01c0 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20  .    z = zBuf;. 
b01d0 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74     c = sqlite3Ut
b01e0 66 38 52 65 61 64 28 7a 2c 20 28 63 6f 6e 73 74  f8Read(z, (const
b01f0 20 75 38 2a 2a 29 26 7a 29 3b 0a 20 20 20 20 74   u8**)&z);.    t
b0200 20 3d 20 69 3b 0a 20 20 20 20 69 66 28 20 69 3e   = i;.    if( i>
b0210 3d 30 78 44 38 30 30 20 26 26 20 69 3c 3d 30 78  =0xD800 && i<=0x
b0220 44 46 46 46 20 29 20 74 20 3d 20 30 78 46 46 46  DFFF ) t = 0xFFF
b0230 44 3b 0a 20 20 20 20 69 66 28 20 28 69 26 30 78  D;.    if( (i&0x
b0240 46 46 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46  FFFFFFFE)==0xFFF
b0250 45 20 29 20 74 20 3d 20 30 78 46 46 46 44 3b 0a  E ) t = 0xFFFD;.
b0260 20 20 20 20 61 73 73 65 72 74 28 20 63 3d 3d 74      assert( c==t
b0270 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
b0280 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20  (z-zBuf)==n );. 
b0290 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c   }.  for(i=0; i<
b02a0 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b 29  0x00110000; i++)
b02b0 7b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78 44  {.    if( i>=0xD
b02c0 38 30 30 20 26 26 20 69 3c 30 78 45 30 30 30 20  800 && i<0xE000 
b02d0 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
b02e0 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 52  z = zBuf;.    WR
b02f0 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 69  ITE_UTF16LE(z, i
b0300 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29  );.    n = (int)
b0310 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73  (z-zBuf);.    as
b0320 73 65 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d  sert( n>0 && n<=
b0330 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20  4 );.    z[0] = 
b0340 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b  0;.    z = zBuf;
b0350 0a 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 4c  .    READ_UTF16L
b0360 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 61 73 73  E(z, c);.    ass
b0370 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a 20 20 20  ert( c==i );.   
b0380 20 61 73 73 65 72 74 28 20 28 7a 2d 7a 42 75 66   assert( (z-zBuf
b0390 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20 66 6f  )==n );.  }.  fo
b03a0 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31 31 30  r(i=0; i<0x00110
b03b0 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  000; i++){.    i
b03c0 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26 26 20  f( i>=0xD800 && 
b03d0 69 3c 30 78 45 30 30 30 20 29 20 63 6f 6e 74 69  i<0xE000 ) conti
b03e0 6e 75 65 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75  nue;.    z = zBu
b03f0 66 3b 0a 20 20 20 20 57 52 49 54 45 5f 55 54 46  f;.    WRITE_UTF
b0400 31 36 42 45 28 7a 2c 20 69 29 3b 0a 20 20 20 20  16BE(z, i);.    
b0410 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a 42 75 66  n = (int)(z-zBuf
b0420 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e  );.    assert( n
b0430 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b 0a 20 20  >0 && n<=4 );.  
b0440 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20    z[0] = 0;.    
b0450 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 52 45  z = zBuf;.    RE
b0460 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 63 29  AD_UTF16BE(z, c)
b0470 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 3d  ;.    assert( c=
b0480 3d 69 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =i );.    assert
b0490 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b  ( (z-zBuf)==n );
b04a0 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  .  }.}.#endif /*
b04b0 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f 0a   SQLITE_TEST */.
b04c0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
b04d0 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a  _OMIT_UTF16 */..
b04e0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
b04f0 45 6e 64 20 6f 66 20 75 74 66 2e 63 20 2a 2a 2a  End of utf.c ***
b0500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
b0530 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
b0540 42 65 67 69 6e 20 66 69 6c 65 20 75 74 69 6c 2e  Begin file util.
b0550 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
b0560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
b0580 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
b0590 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
b05a0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
b05b0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
b05c0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
b05d0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
b05e0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
b05f0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
b0600 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
b0610 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
b0620 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
b0630 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
b0640 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
b0650 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
b0660 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
b0670 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
b0680 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
b0690 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
b06a0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
b06b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b06c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b06d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b06e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b06f0 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 55 74 69 6c 69 74  ******.** Utilit
b0700 79 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64  y functions used
b0710 20 74 68 72 6f 75 67 68 6f 75 74 20 73 71 6c 69   throughout sqli
b0720 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  te..**.** This f
b0730 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 66 75 6e  ile contains fun
b0740 63 74 69 6f 6e 73 20 66 6f 72 20 61 6c 6c 6f 63  ctions for alloc
b0750 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 63 6f  ating memory, co
b0760 6d 70 61 72 69 6e 67 0a 2a 2a 20 73 74 72 69 6e  mparing.** strin
b0770 67 73 2c 20 61 6e 64 20 73 74 75 66 66 20 6c 69  gs, and stuff li
b0780 6b 65 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2f 0a 23  ke that..**.*/.#
b0790 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 56  ifdef SQLITE_HAV
b07a0 45 5f 49 53 4e 41 4e 0a 23 20 69 6e 63 6c 75 64  E_ISNAN.# includ
b07b0 65 20 3c 6d 61 74 68 2e 68 3e 0a 23 65 6e 64 69  e <math.h>.#endi
b07c0 66 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65  f../*.** Routine
b07d0 20 6e 65 65 64 65 64 20 74 6f 20 73 75 70 70 6f   needed to suppo
b07e0 72 74 20 74 68 65 20 74 65 73 74 63 61 73 65 28  rt the testcase(
b07f0 29 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 64  ) macro..*/.#ifd
b0800 65 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41  ef SQLITE_COVERA
b0810 47 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50  GE_TEST.SQLITE_P
b0820 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
b0830 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e 74 20  te3Coverage(int 
b0840 78 29 7b 0a 20 20 73 74 61 74 69 63 20 69 6e 74  x){.  static int
b0850 20 64 75 6d 6d 79 20 3d 20 30 3b 0a 20 20 64 75   dummy = 0;.  du
b0860 6d 6d 79 20 2b 3d 20 78 3b 0a 7d 0a 23 65 6e 64  mmy += x;.}.#end
b0870 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  if../*.** Return
b0880 20 74 72 75 65 20 69 66 20 74 68 65 20 66 6c 6f   true if the flo
b0890 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75  ating point valu
b08a0 65 20 69 73 20 4e 6f 74 20 61 20 4e 75 6d 62 65  e is Not a Numbe
b08b0 72 20 28 4e 61 4e 29 2e 0a 2a 2a 0a 2a 2a 20 55  r (NaN)..**.** U
b08c0 73 65 20 74 68 65 20 6d 61 74 68 20 6c 69 62 72  se the math libr
b08d0 61 72 79 20 69 73 6e 61 6e 28 29 20 66 75 6e 63  ary isnan() func
b08e0 74 69 6f 6e 20 69 66 20 63 6f 6d 70 69 6c 65 64  tion if compiled
b08f0 20 77 69 74 68 20 53 51 4c 49 54 45 5f 48 41 56   with SQLITE_HAV
b0900 45 5f 49 53 4e 41 4e 2e 0a 2a 2a 20 4f 74 68 65  E_ISNAN..** Othe
b0910 72 77 69 73 65 2c 20 77 65 20 68 61 76 65 20 6f  rwise, we have o
b0920 75 72 20 6f 77 6e 20 69 6d 70 6c 65 6d 65 6e 74  ur own implement
b0930 61 74 69 6f 6e 20 74 68 61 74 20 77 6f 72 6b 73  ation that works
b0940 20 6f 6e 20 6d 6f 73 74 20 73 79 73 74 65 6d 73   on most systems
b0950 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
b0960 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
b0970 73 4e 61 4e 28 64 6f 75 62 6c 65 20 78 29 7b 0a  sNaN(double x){.
b0980 20 20 69 6e 74 20 72 63 3b 20 20 20 2f 2a 20 54    int rc;   /* T
b0990 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 20  he value return 
b09a0 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  */.#if !defined(
b09b0 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e 41  SQLITE_HAVE_ISNA
b09c0 4e 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53 79 73  N).  /*.  ** Sys
b09d0 74 65 6d 73 20 74 68 61 74 20 73 75 70 70 6f 72  tems that suppor
b09e0 74 20 74 68 65 20 69 73 6e 61 6e 28 29 20 6c 69  t the isnan() li
b09f0 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 73  brary function s
b0a00 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 0a 20  hould probably. 
b0a10 20 2a 2a 20 6d 61 6b 65 20 75 73 65 20 6f 66 20   ** make use of 
b0a20 69 74 20 62 79 20 63 6f 6d 70 69 6c 69 6e 67 20  it by compiling 
b0a30 77 69 74 68 20 2d 44 53 51 4c 49 54 45 5f 48 41  with -DSQLITE_HA
b0a40 56 45 5f 49 53 4e 41 4e 2e 20 20 42 75 74 20 77  VE_ISNAN.  But w
b0a50 65 20 68 61 76 65 0a 20 20 2a 2a 20 66 6f 75 6e  e have.  ** foun
b0a60 64 20 74 68 61 74 20 6d 61 6e 79 20 73 79 73 74  d that many syst
b0a70 65 6d 73 20 64 6f 20 6e 6f 74 20 68 61 76 65 20  ems do not have 
b0a80 61 20 77 6f 72 6b 69 6e 67 20 69 73 6e 61 6e 28  a working isnan(
b0a90 29 20 66 75 6e 63 74 69 6f 6e 20 73 6f 0a 20 20  ) function so.  
b0aa0 2a 2a 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e  ** this implemen
b0ab0 74 61 74 69 6f 6e 20 69 73 20 70 72 6f 76 69 64  tation is provid
b0ac0 65 64 20 61 73 20 61 6e 20 61 6c 74 65 72 6e 61  ed as an alterna
b0ad0 74 69 76 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  tive..  **.  ** 
b0ae0 54 68 69 73 20 4e 61 4e 20 74 65 73 74 20 73 6f  This NaN test so
b0af0 6d 65 74 69 6d 65 73 20 66 61 69 6c 73 20 69 66  metimes fails if
b0b00 20 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 47 43 43   compiled on GCC
b0b10 20 77 69 74 68 20 2d 66 66 61 73 74 2d 6d 61 74   with -ffast-mat
b0b20 68 2e 0a 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f  h..  ** On the o
b0b30 74 68 65 72 20 68 61 6e 64 2c 20 74 68 65 20 75  ther hand, the u
b0b40 73 65 20 6f 66 20 2d 66 66 61 73 74 2d 6d 61 74  se of -ffast-mat
b0b50 68 20 63 6f 6d 65 73 20 77 69 74 68 20 74 68 65  h comes with the
b0b60 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20   following.  ** 
b0b70 77 61 72 6e 69 6e 67 3a 0a 20 20 2a 2a 0a 20 20  warning:.  **.  
b0b80 2a 2a 20 20 20 20 20 20 54 68 69 73 20 6f 70 74  **      This opt
b0b90 69 6f 6e 20 5b 2d 66 66 61 73 74 2d 6d 61 74 68  ion [-ffast-math
b0ba0 5d 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62  ] should never b
b0bb0 65 20 74 75 72 6e 65 64 20 6f 6e 20 62 79 20 61  e turned on by a
b0bc0 6e 79 0a 20 20 2a 2a 20 20 20 20 20 20 2d 4f 20  ny.  **      -O 
b0bd0 6f 70 74 69 6f 6e 20 73 69 6e 63 65 20 69 74 20  option since it 
b0be0 63 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 69 6e  can result in in
b0bf0 63 6f 72 72 65 63 74 20 6f 75 74 70 75 74 20 66  correct output f
b0c00 6f 72 20 70 72 6f 67 72 61 6d 73 0a 20 20 2a 2a  or programs.  **
b0c10 20 20 20 20 20 20 77 68 69 63 68 20 64 65 70 65        which depe
b0c20 6e 64 20 6f 6e 20 61 6e 20 65 78 61 63 74 20 69  nd on an exact i
b0c30 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
b0c40 20 49 45 45 45 20 6f 72 20 49 53 4f 20 0a 20 20   IEEE or ISO .  
b0c50 2a 2a 20 20 20 20 20 20 72 75 6c 65 73 2f 73 70  **      rules/sp
b0c60 65 63 69 66 69 63 61 74 69 6f 6e 73 20 66 6f 72  ecifications for
b0c70 20 6d 61 74 68 20 66 75 6e 63 74 69 6f 6e 73 2e   math functions.
b0c80 0a 20 20 2a 2a 0a 20 20 2a 2a 20 55 6e 64 65 72  .  **.  ** Under
b0c90 20 4d 53 56 43 2c 20 74 68 69 73 20 4e 61 4e 20   MSVC, this NaN 
b0ca0 74 65 73 74 20 6d 61 79 20 66 61 69 6c 20 69 66  test may fail if
b0cb0 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 61   compiled with a
b0cc0 20 66 6c 6f 61 74 69 6e 67 2d 0a 20 20 2a 2a 20   floating-.  ** 
b0cd0 70 6f 69 6e 74 20 70 72 65 63 69 73 69 6f 6e 20  point precision 
b0ce0 6d 6f 64 65 20 6f 74 68 65 72 20 74 68 61 6e 20  mode other than 
b0cf0 2f 66 70 3a 70 72 65 63 69 73 65 2e 20 20 46 72  /fp:precise.  Fr
b0d00 6f 6d 20 74 68 65 20 4d 53 44 4e 20 0a 20 20 2a  om the MSDN .  *
b0d10 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 3a  * documentation:
b0d20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20  .  **.  **      
b0d30 54 68 65 20 63 6f 6d 70 69 6c 65 72 20 5b 77 69  The compiler [wi
b0d40 74 68 20 2f 66 70 3a 70 72 65 63 69 73 65 5d 20  th /fp:precise] 
b0d50 77 69 6c 6c 20 70 72 6f 70 65 72 6c 79 20 68 61  will properly ha
b0d60 6e 64 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 73  ndle comparisons
b0d70 20 0a 20 20 2a 2a 20 20 20 20 20 20 69 6e 76 6f   .  **      invo
b0d80 6c 76 69 6e 67 20 4e 61 4e 2e 20 46 6f 72 20 65  lving NaN. For e
b0d90 78 61 6d 70 6c 65 2c 20 78 20 21 3d 20 78 20 65  xample, x != x e
b0da0 76 61 6c 75 61 74 65 73 20 74 6f 20 74 72 75 65  valuates to true
b0db0 20 69 66 20 78 20 69 73 20 4e 61 4e 20 0a 20 20   if x is NaN .  
b0dc0 2a 2a 20 20 20 20 20 20 2e 2e 2e 0a 20 20 2a 2f  **      ....  */
b0dd0 0a 23 69 66 64 65 66 20 5f 5f 46 41 53 54 5f 4d  .#ifdef __FAST_M
b0de0 41 54 48 5f 5f 0a 23 20 65 72 72 6f 72 20 53 51  ATH__.# error SQ
b0df0 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 77 6f  Lite will not wo
b0e00 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 77 69 74  rk correctly wit
b0e10 68 20 74 68 65 20 2d 66 66 61 73 74 2d 6d 61 74  h the -ffast-mat
b0e20 68 20 6f 70 74 69 6f 6e 20 6f 66 20 47 43 43 2e  h option of GCC.
b0e30 0a 23 65 6e 64 69 66 0a 20 20 76 6f 6c 61 74 69  .#endif.  volati
b0e40 6c 65 20 64 6f 75 62 6c 65 20 79 20 3d 20 78 3b  le double y = x;
b0e50 0a 20 20 76 6f 6c 61 74 69 6c 65 20 64 6f 75 62  .  volatile doub
b0e60 6c 65 20 7a 20 3d 20 79 3b 0a 20 20 72 63 20 3d  le z = y;.  rc =
b0e70 20 28 79 21 3d 7a 29 3b 0a 23 65 6c 73 65 20 20   (y!=z);.#else  
b0e80 2f 2a 20 69 66 20 64 65 66 69 6e 65 64 28 53 51  /* if defined(SQ
b0e90 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 29  LITE_HAVE_ISNAN)
b0ea0 20 2a 2f 0a 20 20 72 63 20 3d 20 69 73 6e 61 6e   */.  rc = isnan
b0eb0 28 78 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53  (x);.#endif /* S
b0ec0 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e  QLITE_HAVE_ISNAN
b0ed0 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 28 20   */.  testcase( 
b0ee0 72 63 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  rc );.  return r
b0ef0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  c;.}../*.** Comp
b0f00 75 74 65 20 61 20 73 74 72 69 6e 67 20 6c 65 6e  ute a string len
b0f10 67 74 68 20 74 68 61 74 20 69 73 20 6c 69 6d 69  gth that is limi
b0f20 74 65 64 20 74 6f 20 77 68 61 74 20 63 61 6e 20  ted to what can 
b0f30 62 65 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20  be stored in.** 
b0f40 6c 6f 77 65 72 20 33 30 20 62 69 74 73 20 6f 66  lower 30 bits of
b0f50 20 61 20 33 32 2d 62 69 74 20 73 69 67 6e 65 64   a 32-bit signed
b0f60 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20   integer..**.** 
b0f70 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  The value return
b0f80 65 64 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65  ed will never be
b0f90 20 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f 72 20   negative.  Nor 
b0fa0 77 69 6c 6c 20 69 74 20 65 76 65 72 20 62 65 20  will it ever be 
b0fb0 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20  greater.** than 
b0fc0 74 68 65 20 61 63 74 75 61 6c 20 6c 65 6e 67 74  the actual lengt
b0fd0 68 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 2e  h of the string.
b0fe0 20 20 46 6f 72 20 76 65 72 79 20 6c 6f 6e 67 20    For very long 
b0ff0 73 74 72 69 6e 67 73 20 28 67 72 65 61 74 65 72  strings (greater
b1000 0a 2a 2a 20 74 68 61 6e 20 31 47 69 42 29 20 74  .** than 1GiB) t
b1010 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
b1020 64 20 6d 69 67 68 74 20 62 65 20 6c 65 73 73 20  d might be less 
b1030 74 68 61 6e 20 74 68 65 20 74 72 75 65 20 73 74  than the true st
b1040 72 69 6e 67 20 6c 65 6e 67 74 68 2e 0a 2a 2f 0a  ring length..*/.
b1050 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
b1060 6e 74 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  nt sqlite3Strlen
b1070 33 30 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  30(const char *z
b1080 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
b1090 2a 7a 32 20 3d 20 7a 3b 0a 20 20 69 66 28 20 7a  *z2 = z;.  if( z
b10a0 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
b10b0 20 20 77 68 69 6c 65 28 20 2a 7a 32 20 29 7b 20    while( *z2 ){ 
b10c0 7a 32 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e  z2++; }.  return
b10d0 20 30 78 33 66 66 66 66 66 66 66 20 26 20 28 69   0x3fffffff & (i
b10e0 6e 74 29 28 7a 32 20 2d 20 7a 29 3b 0a 7d 0a 0a  nt)(z2 - z);.}..
b10f0 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6d 6f  /*.** Set the mo
b1100 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20  st recent error 
b1110 63 6f 64 65 20 61 6e 64 20 65 72 72 6f 72 20 73  code and error s
b1120 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 73 71  tring for the sq
b1130 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 22  lite.** handle "
b1140 64 62 22 2e 20 54 68 65 20 65 72 72 6f 72 20 63  db". The error c
b1150 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 22 65  ode is set to "e
b1160 72 72 5f 63 6f 64 65 22 2e 0a 2a 2a 0a 2a 2a 20  rr_code"..**.** 
b1170 49 66 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c  If it is not NUL
b1180 4c 2c 20 73 74 72 69 6e 67 20 7a 46 6f 72 6d 61  L, string zForma
b1190 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20  t specifies the 
b11a0 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 0a 2a 2a  format of the.**
b11b0 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20 69 6e   error string in
b11c0 20 74 68 65 20 73 74 79 6c 65 20 6f 66 20 74 68   the style of th
b11d0 65 20 70 72 69 6e 74 66 20 66 75 6e 63 74 69 6f  e printf functio
b11e0 6e 73 3a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  ns: The followin
b11f0 67 0a 2a 2a 20 66 6f 72 6d 61 74 20 63 68 61 72  g.** format char
b1200 61 63 74 65 72 73 20 61 72 65 20 61 6c 6c 6f 77  acters are allow
b1210 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 25  ed:.**.**      %
b1220 73 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20  s      Insert a 
b1230 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 25  string.**      %
b1240 7a 20 20 20 20 20 20 41 20 73 74 72 69 6e 67 20  z      A string 
b1250 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 66  that should be f
b1260 72 65 65 64 20 61 66 74 65 72 20 75 73 65 0a 2a  reed after use.*
b1270 2a 20 20 20 20 20 20 25 64 20 20 20 20 20 20 49  *      %d      I
b1280 6e 73 65 72 74 20 61 6e 20 69 6e 74 65 67 65 72  nsert an integer
b1290 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 20 20 20  .**      %T     
b12a0 20 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e 0a   Insert a token.
b12b0 2a 2a 20 20 20 20 20 20 25 53 20 20 20 20 20 20  **      %S      
b12c0 49 6e 73 65 72 74 20 74 68 65 20 66 69 72 73 74  Insert the first
b12d0 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53 72   element of a Sr
b12e0 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 7a 46 6f 72  cList.**.** zFor
b12f0 6d 61 74 20 61 6e 64 20 61 6e 79 20 73 74 72 69  mat and any stri
b1300 6e 67 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 66  ng tokens that f
b1310 6f 6c 6c 6f 77 20 69 74 20 61 72 65 20 61 73 73  ollow it are ass
b1320 75 6d 65 64 20 74 6f 20 62 65 0a 2a 2a 20 65 6e  umed to be.** en
b1330 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 2e 0a  coded in UTF-8..
b1340 2a 2a 0a 2a 2a 20 54 6f 20 63 6c 65 61 72 20 74  **.** To clear t
b1350 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65  he most recent e
b1360 72 72 6f 72 20 66 6f 72 20 73 71 6c 69 74 65 20  rror for sqlite 
b1370 68 61 6e 64 6c 65 20 22 64 62 22 2c 20 73 71 6c  handle "db", sql
b1380 69 74 65 33 45 72 72 6f 72 0a 2a 2a 20 73 68 6f  ite3Error.** sho
b1390 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77 69  uld be called wi
b13a0 74 68 20 65 72 72 5f 63 6f 64 65 20 73 65 74 20  th err_code set 
b13b0 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64  to SQLITE_OK and
b13c0 20 7a 46 6f 72 6d 61 74 20 73 65 74 0a 2a 2a 20   zFormat set.** 
b13d0 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49  to NULL..*/.SQLI
b13e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
b13f0 73 71 6c 69 74 65 33 45 72 72 6f 72 28 73 71 6c  sqlite3Error(sql
b1400 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 65 72  ite3 *db, int er
b1410 72 5f 63 6f 64 65 2c 20 63 6f 6e 73 74 20 63 68  r_code, const ch
b1420 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e  ar *zFormat, ...
b1430 29 7b 0a 20 20 69 66 28 20 64 62 20 26 26 20 28  ){.  if( db && (
b1440 64 62 2d 3e 70 45 72 72 20 7c 7c 20 28 64 62 2d  db->pErr || (db-
b1450 3e 70 45 72 72 20 3d 20 73 71 6c 69 74 65 33 56  >pErr = sqlite3V
b1460 61 6c 75 65 4e 65 77 28 64 62 29 29 21 3d 30 29  alueNew(db))!=0)
b1470 20 29 7b 0a 20 20 20 20 64 62 2d 3e 65 72 72 43   ){.    db->errC
b1480 6f 64 65 20 3d 20 65 72 72 5f 63 6f 64 65 3b 0a  ode = err_code;.
b1490 20 20 20 20 69 66 28 20 7a 46 6f 72 6d 61 74 20      if( zFormat 
b14a0 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  ){.      char *z
b14b0 3b 0a 20 20 20 20 20 20 76 61 5f 6c 69 73 74 20  ;.      va_list 
b14c0 61 70 3b 0a 20 20 20 20 20 20 76 61 5f 73 74 61  ap;.      va_sta
b14d0 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b  rt(ap, zFormat);
b14e0 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  .      z = sqlit
b14f0 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a  e3VMPrintf(db, z
b1500 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 20  Format, ap);.   
b1510 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20     va_end(ap);. 
b1520 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75       sqlite3Valu
b1530 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72 72  eSetStr(db->pErr
b1540 2c 20 2d 31 2c 20 7a 2c 20 53 51 4c 49 54 45 5f  , -1, z, SQLITE_
b1550 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 44 59 4e  UTF8, SQLITE_DYN
b1560 41 4d 49 43 29 3b 0a 20 20 20 20 7d 65 6c 73 65  AMIC);.    }else
b1570 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
b1580 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70  alueSetStr(db->p
b1590 45 72 72 2c 20 30 2c 20 30 2c 20 53 51 4c 49 54  Err, 0, 0, SQLIT
b15a0 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53  E_UTF8, SQLITE_S
b15b0 54 41 54 49 43 29 3b 0a 20 20 20 20 7d 0a 20 20  TATIC);.    }.  
b15c0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61  }.}../*.** Add a
b15d0 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
b15e0 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  to pParse->zErrM
b15f0 73 67 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74  sg and increment
b1600 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2e 0a 2a   pParse->nErr..*
b1610 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
b1620 66 6f 72 6d 61 74 74 69 6e 67 20 63 68 61 72 61  formatting chara
b1630 63 74 65 72 73 20 61 72 65 20 61 6c 6c 6f 77 65  cters are allowe
b1640 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 25 73  d:.**.**      %s
b1650 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 73        Insert a s
b1660 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 25 7a  tring.**      %z
b1670 20 20 20 20 20 20 41 20 73 74 72 69 6e 67 20 74        A string t
b1680 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 66 72  hat should be fr
b1690 65 65 64 20 61 66 74 65 72 20 75 73 65 0a 2a 2a  eed after use.**
b16a0 20 20 20 20 20 20 25 64 20 20 20 20 20 20 49 6e        %d      In
b16b0 73 65 72 74 20 61 6e 20 69 6e 74 65 67 65 72 0a  sert an integer.
b16c0 2a 2a 20 20 20 20 20 20 25 54 20 20 20 20 20 20  **      %T      
b16d0 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e 0a 2a  Insert a token.*
b16e0 2a 20 20 20 20 20 20 25 53 20 20 20 20 20 20 49  *      %S      I
b16f0 6e 73 65 72 74 20 74 68 65 20 66 69 72 73 74 20  nsert the first 
b1700 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53 72 63  element of a Src
b1710 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  List.**.** This 
b1720 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20  function should 
b1730 62 65 20 75 73 65 64 20 74 6f 20 72 65 70 6f 72  be used to repor
b1740 74 20 61 6e 79 20 65 72 72 6f 72 20 74 68 61 74  t any error that
b1750 20 6f 63 63 75 72 73 20 77 68 69 6c 73 74 0a 2a   occurs whilst.*
b1760 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 61 6e 20 53  * compiling an S
b1770 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 28 69 2e  QL statement (i.
b1780 65 2e 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65  e. within sqlite
b1790 33 5f 70 72 65 70 61 72 65 28 29 29 2e 20 54 68  3_prepare()). Th
b17a0 65 0a 2a 2a 20 6c 61 73 74 20 74 68 69 6e 67 20  e.** last thing 
b17b0 74 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70  the sqlite3_prep
b17c0 61 72 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 64  are() function d
b17d0 6f 65 73 20 69 73 20 63 6f 70 79 20 74 68 65 20  oes is copy the 
b17e0 65 72 72 6f 72 0a 2a 2a 20 73 74 6f 72 65 64 20  error.** stored 
b17f0 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  by this function
b1800 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61   into the databa
b1810 73 65 20 68 61 6e 64 6c 65 20 75 73 69 6e 67 20  se handle using 
b1820 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 2e 0a  sqlite3Error()..
b1830 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 73 71 6c 69  ** Function sqli
b1840 74 65 33 45 72 72 6f 72 28 29 20 73 68 6f 75 6c  te3Error() shoul
b1850 64 20 62 65 20 75 73 65 64 20 64 75 72 69 6e 67  d be used during
b1860 20 73 74 61 74 65 6d 65 6e 74 20 65 78 65 63 75   statement execu
b1870 74 69 6f 6e 0a 2a 2a 20 28 73 71 6c 69 74 65 33  tion.** (sqlite3
b1880 5f 73 74 65 70 28 29 20 65 74 63 2e 29 2e 0a 2a  _step() etc.)..*
b1890 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b18a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72   void sqlite3Err
b18b0 6f 72 4d 73 67 28 50 61 72 73 65 20 2a 70 50 61  orMsg(Parse *pPa
b18c0 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  rse, const char 
b18d0 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a  *zFormat, ...){.
b18e0 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
b18f0 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
b1900 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 50 61 72  arse->db;.  pPar
b1910 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 73 71  se->nErr++;.  sq
b1920 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
b1930 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 29  pParse->zErrMsg)
b1940 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c  ;.  va_start(ap,
b1950 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 70 50 61   zFormat);.  pPa
b1960 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 73  rse->zErrMsg = s
b1970 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64  qlite3VMPrintf(d
b1980 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b  b, zFormat, ap);
b1990 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20  .  va_end(ap);. 
b19a0 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51   pParse->rc = SQ
b19b0 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f  LITE_ERROR;.}../
b19c0 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 65  *.** Clear the e
b19d0 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20  rror message in 
b19e0 70 50 61 72 73 65 2c 20 69 66 20 61 6e 79 0a 2a  pParse, if any.*
b19f0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b1a00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72   void sqlite3Err
b1a10 6f 72 43 6c 65 61 72 28 50 61 72 73 65 20 2a 70  orClear(Parse *p
b1a20 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65  Parse){.  sqlite
b1a30 33 44 62 46 72 65 65 28 70 50 61 72 73 65 2d 3e  3DbFree(pParse->
b1a40 64 62 2c 20 70 50 61 72 73 65 2d 3e 7a 45 72 72  db, pParse->zErr
b1a50 4d 73 67 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  Msg);.  pParse->
b1a60 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 70  zErrMsg = 0;.  p
b1a70 50 61 72 73 65 2d 3e 6e 45 72 72 20 3d 20 30 3b  Parse->nErr = 0;
b1a80 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
b1a90 74 20 61 6e 20 53 51 4c 2d 73 74 79 6c 65 20 71  t an SQL-style q
b1aa0 75 6f 74 65 64 20 73 74 72 69 6e 67 20 69 6e 74  uoted string int
b1ab0 6f 20 61 20 6e 6f 72 6d 61 6c 20 73 74 72 69 6e  o a normal strin
b1ac0 67 20 62 79 20 72 65 6d 6f 76 69 6e 67 0a 2a 2a  g by removing.**
b1ad0 20 74 68 65 20 71 75 6f 74 65 20 63 68 61 72 61   the quote chara
b1ae0 63 74 65 72 73 2e 20 20 54 68 65 20 63 6f 6e 76  cters.  The conv
b1af0 65 72 73 69 6f 6e 20 69 73 20 64 6f 6e 65 20 69  ersion is done i
b1b00 6e 2d 70 6c 61 63 65 2e 20 20 49 66 20 74 68 65  n-place.  If the
b1b10 0a 2a 2a 20 69 6e 70 75 74 20 64 6f 65 73 20 6e  .** input does n
b1b20 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20 61 20  ot begin with a 
b1b30 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 2c  quote character,
b1b40 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
b1b50 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70  ne.** is a no-op
b1b60 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75  ..**.** The inpu
b1b70 74 20 73 74 72 69 6e 67 20 6d 75 73 74 20 62 65  t string must be
b1b80 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
b1b90 2e 20 20 41 20 6e 65 77 20 7a 65 72 6f 2d 74 65  .  A new zero-te
b1ba0 72 6d 69 6e 61 74 6f 72 0a 2a 2a 20 69 73 20 61  rminator.** is a
b1bb0 64 64 65 64 20 74 6f 20 74 68 65 20 64 65 71 75  dded to the dequ
b1bc0 6f 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a  oted string..**.
b1bd0 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  ** The return va
b1be0 6c 75 65 20 69 73 20 2d 31 20 69 66 20 6e 6f 20  lue is -1 if no 
b1bf0 64 65 71 75 6f 74 69 6e 67 20 6f 63 63 75 72 73  dequoting occurs
b1c00 20 6f 72 20 74 68 65 20 6c 65 6e 67 74 68 20 6f   or the length o
b1c10 66 20 74 68 65 0a 2a 2a 20 64 65 71 75 6f 74 65  f the.** dequote
b1c20 64 20 73 74 72 69 6e 67 2c 20 65 78 63 6c 75 73  d string, exclus
b1c30 69 76 65 20 6f 66 20 74 68 65 20 7a 65 72 6f 20  ive of the zero 
b1c40 74 65 72 6d 69 6e 61 74 6f 72 2c 20 69 66 20 64  terminator, if d
b1c50 65 71 75 6f 74 69 6e 67 20 64 6f 65 73 0a 2a 2a  equoting does.**
b1c60 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a 2a 20 32 30   occur..**.** 20
b1c70 30 32 2d 46 65 62 2d 31 34 3a 20 54 68 69 73 20  02-Feb-14: This 
b1c80 72 6f 75 74 69 6e 65 20 69 73 20 65 78 74 65 6e  routine is exten
b1c90 64 65 64 20 74 6f 20 72 65 6d 6f 76 65 20 4d 53  ded to remove MS
b1ca0 2d 41 63 63 65 73 73 20 73 74 79 6c 65 0a 2a 2a  -Access style.**
b1cb0 20 62 72 61 63 6b 65 74 73 20 66 72 6f 6d 20 61   brackets from a
b1cc0 72 6f 75 6e 64 20 69 64 65 6e 74 69 66 65 72 73  round identifers
b1cd0 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 20  .  For example: 
b1ce0 20 22 5b 61 2d 62 2d 63 5d 22 20 62 65 63 6f 6d   "[a-b-c]" becom
b1cf0 65 73 0a 2a 2a 20 22 61 2d 62 2d 63 22 2e 0a 2a  es.** "a-b-c"..*
b1d00 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b1d10 20 69 6e 74 20 73 71 6c 69 74 65 33 44 65 71 75   int sqlite3Dequ
b1d20 6f 74 65 28 63 68 61 72 20 2a 7a 29 7b 0a 20 20  ote(char *z){.  
b1d30 63 68 61 72 20 71 75 6f 74 65 3b 0a 20 20 69 6e  char quote;.  in
b1d40 74 20 69 2c 20 6a 3b 0a 20 20 69 66 28 20 7a 3d  t i, j;.  if( z=
b1d50 3d 30 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a  =0 ) return -1;.
b1d60 20 20 71 75 6f 74 65 20 3d 20 7a 5b 30 5d 3b 0a    quote = z[0];.
b1d70 20 20 73 77 69 74 63 68 28 20 71 75 6f 74 65 20    switch( quote 
b1d80 29 7b 0a 20 20 20 20 63 61 73 65 20 27 5c 27 27  ){.    case '\''
b1d90 3a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61  :  break;.    ca
b1da0 73 65 20 27 22 27 3a 20 20 20 62 72 65 61 6b 3b  se '"':   break;
b1db0 0a 20 20 20 20 63 61 73 65 20 27 60 27 3a 20 20  .    case '`':  
b1dc0 20 62 72 65 61 6b 3b 20 20 20 20 20 20 20 20 20   break;         
b1dd0 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 4d 79         /* For My
b1de0 53 51 4c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74  SQL compatibilit
b1df0 79 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 27 5b  y */.    case '[
b1e00 27 3a 20 20 20 71 75 6f 74 65 20 3d 20 27 5d 27  ':   quote = ']'
b1e10 3b 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20 46 6f  ;  break;  /* Fo
b1e20 72 20 4d 53 20 53 71 6c 53 65 72 76 65 72 20 63  r MS SqlServer c
b1e30 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a  ompatibility */.
b1e40 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20      default:    
b1e50 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20  return -1;.  }. 
b1e60 20 66 6f 72 28 69 3d 31 2c 20 6a 3d 30 3b 20 41   for(i=1, j=0; A
b1e70 4c 57 41 59 53 28 7a 5b 69 5d 29 3b 20 69 2b 2b  LWAYS(z[i]); i++
b1e80 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d  ){.    if( z[i]=
b1e90 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 20  =quote ){.      
b1ea0 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 71 75 6f 74  if( z[i+1]==quot
b1eb0 65 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 5b 6a  e ){.        z[j
b1ec0 2b 2b 5d 20 3d 20 71 75 6f 74 65 3b 0a 20 20 20  ++] = quote;.   
b1ed0 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20       i++;.      
b1ee0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 62  }else{.        b
b1ef0 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
b1f00 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a    }else{.      z
b1f10 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20  [j++] = z[i];.  
b1f20 20 20 7d 0a 20 20 7d 0a 20 20 7a 5b 6a 5d 20 3d    }.  }.  z[j] =
b1f30 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 6a 3b 0a   0;.  return j;.
b1f40 7d 0a 0a 2f 2a 20 43 6f 6e 76 65 6e 69 65 6e 74  }../* Convenient
b1f50 20 73 68 6f 72 74 2d 68 61 6e 64 20 2a 2f 0a 23   short-hand */.#
b1f60 64 65 66 69 6e 65 20 55 70 70 65 72 54 6f 4c 6f  define UpperToLo
b1f70 77 65 72 20 73 71 6c 69 74 65 33 55 70 70 65 72  wer sqlite3Upper
b1f80 54 6f 4c 6f 77 65 72 0a 0a 2f 2a 0a 2a 2a 20 53  ToLower../*.** S
b1f90 6f 6d 65 20 73 79 73 74 65 6d 73 20 68 61 76 65  ome systems have
b1fa0 20 73 74 72 69 63 6d 70 28 29 2e 20 20 4f 74 68   stricmp().  Oth
b1fb0 65 72 73 20 68 61 76 65 20 73 74 72 63 61 73 65  ers have strcase
b1fc0 63 6d 70 28 29 2e 20 20 42 65 63 61 75 73 65 0a  cmp().  Because.
b1fd0 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63  ** there is no c
b1fe0 6f 6e 73 69 73 74 65 6e 63 79 2c 20 77 65 20 77  onsistency, we w
b1ff0 69 6c 6c 20 64 65 66 69 6e 65 20 6f 75 72 20 6f  ill define our o
b2000 77 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  wn..*/.SQLITE_PR
b2010 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b2020 33 53 74 72 49 43 6d 70 28 63 6f 6e 73 74 20 63  3StrICmp(const c
b2030 68 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73  har *zLeft, cons
b2040 74 20 63 68 61 72 20 2a 7a 52 69 67 68 74 29 7b  t char *zRight){
b2050 0a 20 20 72 65 67 69 73 74 65 72 20 75 6e 73 69  .  register unsi
b2060 67 6e 65 64 20 63 68 61 72 20 2a 61 2c 20 2a 62  gned char *a, *b
b2070 3b 0a 20 20 61 20 3d 20 28 75 6e 73 69 67 6e 65  ;.  a = (unsigne
b2080 64 20 63 68 61 72 20 2a 29 7a 4c 65 66 74 3b 0a  d char *)zLeft;.
b2090 20 20 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20    b = (unsigned 
b20a0 63 68 61 72 20 2a 29 7a 52 69 67 68 74 3b 0a 20  char *)zRight;. 
b20b0 20 77 68 69 6c 65 28 20 2a 61 21 3d 30 20 26 26   while( *a!=0 &&
b20c0 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61   UpperToLower[*a
b20d0 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  ]==UpperToLower[
b20e0 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b 20  *b]){ a++; b++; 
b20f0 7d 0a 20 20 72 65 74 75 72 6e 20 55 70 70 65 72  }.  return Upper
b2100 54 6f 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20 55 70  ToLower[*a] - Up
b2110 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 3b 0a  perToLower[*b];.
b2120 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
b2130 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d   sqlite3_strnicm
b2140 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c  p(const char *zL
b2150 65 66 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  eft, const char 
b2160 2a 7a 52 69 67 68 74 2c 20 69 6e 74 20 4e 29 7b  *zRight, int N){
b2170 0a 20 20 72 65 67 69 73 74 65 72 20 75 6e 73 69  .  register unsi
b2180 67 6e 65 64 20 63 68 61 72 20 2a 61 2c 20 2a 62  gned char *a, *b
b2190 3b 0a 20 20 61 20 3d 20 28 75 6e 73 69 67 6e 65  ;.  a = (unsigne
b21a0 64 20 63 68 61 72 20 2a 29 7a 4c 65 66 74 3b 0a  d char *)zLeft;.
b21b0 20 20 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20    b = (unsigned 
b21c0 63 68 61 72 20 2a 29 7a 52 69 67 68 74 3b 0a 20  char *)zRight;. 
b21d0 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20 30 20   while( N-- > 0 
b21e0 26 26 20 2a 61 21 3d 30 20 26 26 20 55 70 70 65  && *a!=0 && Uppe
b21f0 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 3d 3d 55 70  rToLower[*a]==Up
b2200 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 29 7b  perToLower[*b]){
b2210 20 61 2b 2b 3b 20 62 2b 2b 3b 20 7d 0a 20 20 72   a++; b++; }.  r
b2220 65 74 75 72 6e 20 4e 3c 30 20 3f 20 30 20 3a 20  eturn N<0 ? 0 : 
b2230 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d  UpperToLower[*a]
b2240 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b   - UpperToLower[
b2250 2a 62 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  *b];.}../*.** Re
b2260 74 75 72 6e 20 54 52 55 45 20 69 66 20 7a 20 69  turn TRUE if z i
b2270 73 20 61 20 70 75 72 65 20 6e 75 6d 65 72 69 63  s a pure numeric
b2280 20 73 74 72 69 6e 67 2e 20 20 52 65 74 75 72 6e   string.  Return
b2290 20 46 41 4c 53 45 20 61 6e 64 20 6c 65 61 76 65   FALSE and leave
b22a0 0a 2a 2a 20 2a 72 65 61 6c 6e 75 6d 20 75 6e 63  .** *realnum unc
b22b0 68 61 6e 67 65 64 20 69 66 20 74 68 65 20 73 74  hanged if the st
b22c0 72 69 6e 67 20 63 6f 6e 74 61 69 6e 73 20 61 6e  ring contains an
b22d0 79 20 63 68 61 72 61 63 74 65 72 20 77 68 69 63  y character whic
b22e0 68 20 69 73 20 6e 6f 74 0a 2a 2a 20 70 61 72 74  h is not.** part
b22f0 20 6f 66 20 61 20 6e 75 6d 62 65 72 2e 0a 2a 2a   of a number..**
b2300 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 72 69 6e  .** If the strin
b2310 67 20 69 73 20 70 75 72 65 20 6e 75 6d 65 72 69  g is pure numeri
b2320 63 2c 20 73 65 74 20 2a 72 65 61 6c 6e 75 6d 20  c, set *realnum 
b2330 74 6f 20 54 52 55 45 20 69 66 20 74 68 65 20 73  to TRUE if the s
b2340 74 72 69 6e 67 0a 2a 2a 20 63 6f 6e 74 61 69 6e  tring.** contain
b2350 73 20 74 68 65 20 27 2e 27 20 63 68 61 72 61 63  s the '.' charac
b2360 74 65 72 20 6f 72 20 61 6e 20 22 45 2b 30 30 30  ter or an "E+000
b2370 22 20 73 74 79 6c 65 20 65 78 70 6f 6e 65 6e 74  " style exponent
b2380 69 61 74 69 6f 6e 20 73 75 66 66 69 78 2e 0a 2a  iation suffix..*
b2390 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65 74 20  * Otherwise set 
b23a0 2a 72 65 61 6c 6e 75 6d 20 74 6f 20 46 41 4c 53  *realnum to FALS
b23b0 45 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 6a 75  E.  Note that ju
b23c0 73 74 20 62 65 63 61 75 65 20 2a 72 65 61 6c 6e  st becaue *realn
b23d0 75 6d 20 69 73 0a 2a 2a 20 66 61 6c 73 65 20 64  um is.** false d
b23e0 6f 65 73 20 6e 6f 74 20 6d 65 61 6e 20 74 68 61  oes not mean tha
b23f0 74 20 74 68 65 20 6e 75 6d 62 65 72 20 63 61 6e  t the number can
b2400 20 62 65 20 73 75 63 63 65 73 73 66 75 6c 6c 79   be successfully
b2410 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 0a   converted into.
b2420 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 20 2d 20  ** an integer - 
b2430 69 74 20 6d 69 67 68 74 20 62 65 20 74 6f 6f 20  it might be too 
b2440 62 69 67 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 6d  big..**.** An em
b2450 70 74 79 20 73 74 72 69 6e 67 20 69 73 20 63 6f  pty string is co
b2460 6e 73 69 64 65 72 65 64 20 6e 6f 6e 2d 6e 75 6d  nsidered non-num
b2470 65 72 69 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eric..*/.SQLITE_
b2480 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
b2490 74 65 33 49 73 4e 75 6d 62 65 72 28 63 6f 6e 73  te3IsNumber(cons
b24a0 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a  t char *z, int *
b24b0 72 65 61 6c 6e 75 6d 2c 20 75 38 20 65 6e 63 29  realnum, u8 enc)
b24c0 7b 0a 20 20 69 6e 74 20 69 6e 63 72 20 3d 20 28  {.  int incr = (
b24d0 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38  enc==SQLITE_UTF8
b24e0 3f 31 3a 32 29 3b 0a 20 20 69 66 28 20 65 6e 63  ?1:2);.  if( enc
b24f0 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  ==SQLITE_UTF16BE
b2500 20 29 20 7a 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a   ) z++;.  if( *z
b2510 3d 3d 27 2d 27 20 7c 7c 20 2a 7a 3d 3d 27 2b 27  =='-' || *z=='+'
b2520 20 29 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20   ) z += incr;.  
b2530 69 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 69  if( !sqlite3Isdi
b2540 67 69 74 28 2a 7a 29 20 29 7b 0a 20 20 20 20 72  git(*z) ){.    r
b2550 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a  eturn 0;.  }.  z
b2560 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 2a 72 65 61   += incr;.  *rea
b2570 6c 6e 75 6d 20 3d 20 30 3b 0a 20 20 77 68 69 6c  lnum = 0;.  whil
b2580 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69  e( sqlite3Isdigi
b2590 74 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20 69 6e  t(*z) ){ z += in
b25a0 63 72 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d  cr; }.  if( *z==
b25b0 27 2e 27 20 29 7b 0a 20 20 20 20 7a 20 2b 3d 20  '.' ){.    z += 
b25c0 69 6e 63 72 3b 0a 20 20 20 20 69 66 28 20 21 73  incr;.    if( !s
b25d0 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a  qlite3Isdigit(*z
b25e0 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  ) ) return 0;.  
b25f0 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33    while( sqlite3
b2600 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 20 7a  Isdigit(*z) ){ z
b2610 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 20 20   += incr; }.    
b2620 2a 72 65 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20  *realnum = 1;.  
b2630 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 65 27 20  }.  if( *z=='e' 
b2640 7c 7c 20 2a 7a 3d 3d 27 45 27 20 29 7b 0a 20 20  || *z=='E' ){.  
b2650 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20    z += incr;.   
b2660 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 7c 7c 20   if( *z=='+' || 
b2670 2a 7a 3d 3d 27 2d 27 20 29 20 7a 20 2b 3d 20 69  *z=='-' ) z += i
b2680 6e 63 72 3b 0a 20 20 20 20 69 66 28 20 21 73 71  ncr;.    if( !sq
b2690 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29  lite3Isdigit(*z)
b26a0 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
b26b0 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49   while( sqlite3I
b26c0 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 20 7a 20  sdigit(*z) ){ z 
b26d0 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 20 20 2a  += incr; }.    *
b26e0 72 65 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d  realnum = 1;.  }
b26f0 0a 20 20 72 65 74 75 72 6e 20 2a 7a 3d 3d 30 3b  .  return *z==0;
b2700 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 74  .}../*.** The st
b2710 72 69 6e 67 20 7a 5b 5d 20 69 73 20 61 6e 20 41  ring z[] is an A
b2720 53 43 49 49 20 72 65 70 72 65 73 65 6e 74 61 74  SCII representat
b2730 69 6f 6e 20 6f 66 20 61 20 72 65 61 6c 20 6e 75  ion of a real nu
b2740 6d 62 65 72 2e 0a 2a 2a 20 43 6f 6e 76 65 72 74  mber..** Convert
b2750 20 74 68 69 73 20 73 74 72 69 6e 67 20 74 6f 20   this string to 
b2760 61 20 64 6f 75 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  a double..**.** 
b2770 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 73 73  This routine ass
b2780 75 6d 65 73 20 74 68 61 74 20 7a 5b 5d 20 72 65  umes that z[] re
b2790 61 6c 6c 79 20 69 73 20 61 20 76 61 6c 69 64 20  ally is a valid 
b27a0 6e 75 6d 62 65 72 2e 20 20 49 66 20 69 74 0a 2a  number.  If it.*
b27b0 2a 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 72 65  * is not, the re
b27c0 73 75 6c 74 20 69 73 20 75 6e 64 65 66 69 6e 65  sult is undefine
b27d0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  d..**.** This ro
b27e0 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e  utine is used in
b27f0 73 74 65 61 64 20 6f 66 20 74 68 65 20 6c 69 62  stead of the lib
b2800 72 61 72 79 20 61 74 6f 66 28 29 20 66 75 6e 63  rary atof() func
b2810 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20  tion because.** 
b2820 74 68 65 20 6c 69 62 72 61 72 79 20 61 74 6f 66  the library atof
b2830 28 29 20 6d 69 67 68 74 20 77 61 6e 74 20 74 6f  () might want to
b2840 20 75 73 65 20 22 2c 22 20 61 73 20 74 68 65 20   use "," as the 
b2850 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 69 6e  decimal point in
b2860 73 74 65 61 64 0a 2a 2a 20 6f 66 20 22 2e 22 20  stead.** of "." 
b2870 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68 6f 77  depending on how
b2880 20 6c 6f 63 61 6c 65 20 69 73 20 73 65 74 2e 20   locale is set. 
b2890 20 42 75 74 20 74 68 61 74 20 77 6f 75 6c 64 20   But that would 
b28a0 63 61 75 73 65 20 70 72 6f 62 6c 65 6d 73 0a 2a  cause problems.*
b28b0 2a 20 66 6f 72 20 53 51 4c 2e 20 20 53 6f 20 74  * for SQL.  So t
b28c0 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 77 61  his routine alwa
b28d0 79 73 20 75 73 65 73 20 22 2e 22 20 72 65 67 61  ys uses "." rega
b28e0 72 64 6c 65 73 73 20 6f 66 20 6c 6f 63 61 6c 65  rdless of locale
b28f0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
b2900 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41  ATE int sqlite3A
b2910 74 6f 46 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  toF(const char *
b2920 7a 2c 20 64 6f 75 62 6c 65 20 2a 70 52 65 73 75  z, double *pResu
b2930 6c 74 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  lt){.#ifndef SQL
b2940 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
b2950 47 5f 50 4f 49 4e 54 0a 20 20 63 6f 6e 73 74 20  G_POINT.  const 
b2960 63 68 61 72 20 2a 7a 42 65 67 69 6e 20 3d 20 7a  char *zBegin = z
b2970 3b 0a 20 20 2f 2a 20 73 69 67 6e 20 2a 20 73 69  ;.  /* sign * si
b2980 67 6e 69 66 69 63 61 6e 64 20 2a 20 28 31 30 20  gnificand * (10 
b2990 5e 20 28 65 73 69 67 6e 20 2a 20 65 78 70 6f 6e  ^ (esign * expon
b29a0 65 6e 74 29 29 20 2a 2f 0a 20 20 69 6e 74 20 73  ent)) */.  int s
b29b0 69 67 6e 20 3d 20 31 3b 20 20 20 2f 2a 20 73 69  ign = 1;   /* si
b29c0 67 6e 20 6f 66 20 73 69 67 6e 69 66 69 63 61 6e  gn of significan
b29d0 64 20 2a 2f 0a 20 20 69 36 34 20 73 20 3d 20 30  d */.  i64 s = 0
b29e0 3b 20 20 20 20 20 20 2f 2a 20 73 69 67 6e 69 66  ;      /* signif
b29f0 69 63 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 64  icand */.  int d
b2a00 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 61 64   = 0;      /* ad
b2a10 6a 75 73 74 20 65 78 70 6f 6e 65 6e 74 20 66 6f  just exponent fo
b2a20 72 20 73 68 69 66 74 69 6e 67 20 64 65 63 69 6d  r shifting decim
b2a30 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 69 6e  al point */.  in
b2a40 74 20 65 73 69 67 6e 20 3d 20 31 3b 20 20 2f 2a  t esign = 1;  /*
b2a50 20 73 69 67 6e 20 6f 66 20 65 78 70 6f 6e 65 6e   sign of exponen
b2a60 74 20 2a 2f 0a 20 20 69 6e 74 20 65 20 3d 20 30  t */.  int e = 0
b2a70 3b 20 20 20 20 20 20 2f 2a 20 65 78 70 6f 6e 65  ;      /* expone
b2a80 6e 74 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72  nt */.  double r
b2a90 65 73 75 6c 74 3b 0a 20 20 69 6e 74 20 6e 44 69  esult;.  int nDi
b2aa0 67 69 74 73 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20  gits = 0;..  /* 
b2ab0 73 6b 69 70 20 6c 65 61 64 69 6e 67 20 73 70 61  skip leading spa
b2ac0 63 65 73 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20  ces */.  while( 
b2ad0 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a  sqlite3Isspace(*
b2ae0 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 2f 2a 20 67  z) ) z++;.  /* g
b2af0 65 74 20 73 69 67 6e 20 6f 66 20 73 69 67 6e 69  et sign of signi
b2b00 66 69 63 61 6e 64 20 2a 2f 0a 20 20 69 66 28 20  ficand */.  if( 
b2b10 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 73  *z=='-' ){.    s
b2b20 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 7a 2b  ign = -1;.    z+
b2b30 2b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 2a  +;.  }else if( *
b2b40 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 7a 2b  z=='+' ){.    z+
b2b50 2b 3b 0a 20 20 7d 0a 20 20 2f 2a 20 73 6b 69 70  +;.  }.  /* skip
b2b60 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 65 73 20   leading zeroes 
b2b70 2a 2f 0a 20 20 77 68 69 6c 65 28 20 7a 5b 30 5d  */.  while( z[0]
b2b80 3d 3d 27 30 27 20 29 20 7a 2b 2b 2c 20 6e 44 69  =='0' ) z++, nDi
b2b90 67 69 74 73 2b 2b 3b 0a 0a 20 20 2f 2a 20 63 6f  gits++;..  /* co
b2ba0 70 79 20 6d 61 78 20 73 69 67 6e 69 66 69 63 61  py max significa
b2bb0 6e 74 20 64 69 67 69 74 73 20 74 6f 20 73 69 67  nt digits to sig
b2bc0 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 77 68  nificand */.  wh
b2bd0 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69  ile( sqlite3Isdi
b2be0 67 69 74 28 2a 7a 29 20 26 26 20 73 3c 28 28 4c  git(*z) && s<((L
b2bf0 41 52 47 45 53 54 5f 49 4e 54 36 34 2d 39 29 2f  ARGEST_INT64-9)/
b2c00 31 30 29 20 29 7b 0a 20 20 20 20 73 20 3d 20 73  10) ){.    s = s
b2c10 2a 31 30 20 2b 20 28 2a 7a 20 2d 20 27 30 27 29  *10 + (*z - '0')
b2c20 3b 0a 20 20 20 20 7a 2b 2b 2c 20 6e 44 69 67 69  ;.    z++, nDigi
b2c30 74 73 2b 2b 3b 0a 20 20 7d 0a 20 20 2f 2a 20 73  ts++;.  }.  /* s
b2c40 6b 69 70 20 6e 6f 6e 2d 73 69 67 6e 69 66 69 63  kip non-signific
b2c50 61 6e 74 20 73 69 67 6e 69 66 69 63 61 6e 64 20  ant significand 
b2c60 64 69 67 69 74 73 0a 20 20 2a 2a 20 28 69 6e 63  digits.  ** (inc
b2c70 72 65 61 73 65 20 65 78 70 6f 6e 65 6e 74 20 62  rease exponent b
b2c80 79 20 64 20 74 6f 20 73 68 69 66 74 20 64 65 63  y d to shift dec
b2c90 69 6d 61 6c 20 6c 65 66 74 29 20 2a 2f 0a 20 20  imal left) */.  
b2ca0 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73  while( sqlite3Is
b2cb0 64 69 67 69 74 28 2a 7a 29 20 29 20 7a 2b 2b 2c  digit(*z) ) z++,
b2cc0 20 6e 44 69 67 69 74 73 2b 2b 2c 20 64 2b 2b 3b   nDigits++, d++;
b2cd0 0a 0a 20 20 2f 2a 20 69 66 20 64 65 63 69 6d 61  ..  /* if decima
b2ce0 6c 20 70 6f 69 6e 74 20 69 73 20 70 72 65 73 65  l point is prese
b2cf0 6e 74 20 2a 2f 0a 20 20 69 66 28 20 2a 7a 3d 3d  nt */.  if( *z==
b2d00 27 2e 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a  '.' ){.    z++;.
b2d10 20 20 20 20 2f 2a 20 63 6f 70 79 20 64 69 67 69      /* copy digi
b2d20 74 73 20 66 72 6f 6d 20 61 66 74 65 72 20 64 65  ts from after de
b2d30 63 69 6d 61 6c 20 74 6f 20 73 69 67 6e 69 66 69  cimal to signifi
b2d40 63 61 6e 64 0a 20 20 20 20 2a 2a 20 28 64 65 63  cand.    ** (dec
b2d50 72 65 61 73 65 20 65 78 70 6f 6e 65 6e 74 20 62  rease exponent b
b2d60 79 20 64 20 74 6f 20 73 68 69 66 74 20 64 65 63  y d to shift dec
b2d70 69 6d 61 6c 20 72 69 67 68 74 29 20 2a 2f 0a 20  imal right) */. 
b2d80 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65     while( sqlite
b2d90 33 49 73 64 69 67 69 74 28 2a 7a 29 20 26 26 20  3Isdigit(*z) && 
b2da0 73 3c 28 28 4c 41 52 47 45 53 54 5f 49 4e 54 36  s<((LARGEST_INT6
b2db0 34 2d 39 29 2f 31 30 29 20 29 7b 0a 20 20 20 20  4-9)/10) ){.    
b2dc0 20 20 73 20 3d 20 73 2a 31 30 20 2b 20 28 2a 7a    s = s*10 + (*z
b2dd0 20 2d 20 27 30 27 29 3b 0a 20 20 20 20 20 20 7a   - '0');.      z
b2de0 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b 2c 20 64  ++, nDigits++, d
b2df0 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a  --;.    }.    /*
b2e00 20 73 6b 69 70 20 6e 6f 6e 2d 73 69 67 6e 69 66   skip non-signif
b2e10 69 63 61 6e 74 20 64 69 67 69 74 73 20 2a 2f 0a  icant digits */.
b2e20 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74      while( sqlit
b2e30 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 20  e3Isdigit(*z) ) 
b2e40 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b 3b 0a  z++, nDigits++;.
b2e50 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 65 78 70    }..  /* if exp
b2e60 6f 6e 65 6e 74 20 69 73 20 70 72 65 73 65 6e 74  onent is present
b2e70 20 2a 2f 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 65   */.  if( *z=='e
b2e80 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27 20 29 7b 0a  ' || *z=='E' ){.
b2e90 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 2f 2a 20      z++;.    /* 
b2ea0 67 65 74 20 73 69 67 6e 20 6f 66 20 65 78 70 6f  get sign of expo
b2eb0 6e 65 6e 74 20 2a 2f 0a 20 20 20 20 69 66 28 20  nent */.    if( 
b2ec0 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20  *z=='-' ){.     
b2ed0 20 65 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 20   esign = -1;.   
b2ee0 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73     z++;.    }els
b2ef0 65 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 29 7b  e if( *z=='+' ){
b2f00 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20  .      z++;.    
b2f10 7d 0a 20 20 20 20 2f 2a 20 63 6f 70 79 20 64 69  }.    /* copy di
b2f20 67 69 74 73 20 74 6f 20 65 78 70 6f 6e 65 6e 74  gits to exponent
b2f30 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 73   */.    while( s
b2f40 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a  qlite3Isdigit(*z
b2f50 29 20 29 7b 0a 20 20 20 20 20 20 65 20 3d 20 65  ) ){.      e = e
b2f60 2a 31 30 20 2b 20 28 2a 7a 20 2d 20 27 30 27 29  *10 + (*z - '0')
b2f70 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20  ;.      z++;.   
b2f80 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 64 6a   }.  }..  /* adj
b2f90 75 73 74 20 65 78 70 6f 6e 65 6e 74 20 62 79 20  ust exponent by 
b2fa0 64 2c 20 61 6e 64 20 75 70 64 61 74 65 20 73 69  d, and update si
b2fb0 67 6e 20 2a 2f 0a 20 20 65 20 3d 20 28 65 2a 65  gn */.  e = (e*e
b2fc0 73 69 67 6e 29 20 2b 20 64 3b 0a 20 20 69 66 28  sign) + d;.  if(
b2fd0 20 65 3c 30 20 29 20 7b 0a 20 20 20 20 65 73 69   e<0 ) {.    esi
b2fe0 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 65 20 2a  gn = -1;.    e *
b2ff0 3d 20 2d 31 3b 0a 20 20 7d 20 65 6c 73 65 20 7b  = -1;.  } else {
b3000 0a 20 20 20 20 65 73 69 67 6e 20 3d 20 31 3b 0a  .    esign = 1;.
b3010 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 30 20 73    }..  /* if 0 s
b3020 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20  ignificand */.  
b3030 69 66 28 20 21 73 20 29 20 7b 0a 20 20 20 20 2f  if( !s ) {.    /
b3040 2a 20 49 6e 20 74 68 65 20 49 45 45 45 20 37 35  * In the IEEE 75
b3050 34 20 73 74 61 6e 64 61 72 64 2c 20 7a 65 72 6f  4 standard, zero
b3060 20 69 73 20 73 69 67 6e 65 64 2e 0a 20 20 20 20   is signed..    
b3070 2a 2a 20 41 64 64 20 74 68 65 20 73 69 67 6e 20  ** Add the sign 
b3080 69 66 20 77 65 27 76 65 20 73 65 65 6e 20 61 74  if we've seen at
b3090 20 6c 65 61 73 74 20 6f 6e 65 20 64 69 67 69 74   least one digit
b30a0 20 2a 2f 0a 20 20 20 20 72 65 73 75 6c 74 20 3d   */.    result =
b30b0 20 28 73 69 67 6e 3c 30 20 26 26 20 6e 44 69 67   (sign<0 && nDig
b30c0 69 74 73 29 20 3f 20 2d 28 64 6f 75 62 6c 65 29  its) ? -(double)
b30d0 30 20 3a 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20  0 : (double)0;. 
b30e0 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a   } else {.    /*
b30f0 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 64 75   attempt to redu
b3100 63 65 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20  ce exponent */. 
b3110 20 20 20 69 66 28 20 65 73 69 67 6e 3e 30 20 29     if( esign>0 )
b3120 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 73  {.      while( s
b3130 3c 28 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 2f  <(LARGEST_INT64/
b3140 31 30 29 20 26 26 20 65 3e 30 20 29 20 65 2d 2d  10) && e>0 ) e--
b3150 2c 73 2a 3d 31 30 3b 0a 20 20 20 20 7d 65 6c 73  ,s*=10;.    }els
b3160 65 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  e{.      while( 
b3170 21 28 73 25 31 30 29 20 26 26 20 65 3e 30 20 29  !(s%10) && e>0 )
b3180 20 65 2d 2d 2c 73 2f 3d 31 30 3b 0a 20 20 20 20   e--,s/=10;.    
b3190 7d 0a 0a 20 20 20 20 2f 2a 20 61 64 6a 75 73 74  }..    /* adjust
b31a0 20 74 68 65 20 73 69 67 6e 20 6f 66 20 73 69 67   the sign of sig
b31b0 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 20 20  nificand */.    
b31c0 73 20 3d 20 73 69 67 6e 3c 30 20 3f 20 2d 73 20  s = sign<0 ? -s 
b31d0 3a 20 73 3b 0a 0a 20 20 20 20 2f 2a 20 69 66 20  : s;..    /* if 
b31e0 65 78 70 6f 6e 65 6e 74 2c 20 73 63 61 6c 65 20  exponent, scale 
b31f0 73 69 67 6e 69 66 69 63 61 6e 64 20 61 73 20 61  significand as a
b3200 70 70 72 6f 70 72 69 61 74 65 0a 20 20 20 20 2a  ppropriate.    *
b3210 2a 20 61 6e 64 20 73 74 6f 72 65 20 69 6e 20 72  * and store in r
b3220 65 73 75 6c 74 2e 20 2a 2f 0a 20 20 20 20 69 66  esult. */.    if
b3230 28 20 65 20 29 7b 0a 20 20 20 20 20 20 64 6f 75  ( e ){.      dou
b3240 62 6c 65 20 73 63 61 6c 65 20 3d 20 31 2e 30 3b  ble scale = 1.0;
b3250 0a 20 20 20 20 20 20 2f 2a 20 61 74 74 65 6d 70  .      /* attemp
b3260 74 20 74 6f 20 68 61 6e 64 6c 65 20 65 78 74 72  t to handle extr
b3270 65 6d 65 6c 79 20 73 6d 61 6c 6c 2f 6c 61 72 67  emely small/larg
b3280 65 20 6e 75 6d 62 65 72 73 20 62 65 74 74 65 72  e numbers better
b3290 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 65 3e   */.      if( e>
b32a0 33 30 37 20 26 26 20 65 3c 33 34 32 20 29 7b 0a  307 && e<342 ){.
b32b0 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 65          while( e
b32c0 25 33 30 38 20 29 20 7b 20 73 63 61 6c 65 20 2a  %308 ) { scale *
b32d0 3d 20 31 2e 30 65 2b 31 3b 20 65 20 2d 3d 20 31  = 1.0e+1; e -= 1
b32e0 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ; }.        if( 
b32f0 65 73 69 67 6e 3c 30 20 29 7b 0a 20 20 20 20 20  esign<0 ){.     
b3300 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 73 20       result = s 
b3310 2f 20 73 63 61 6c 65 3b 0a 20 20 20 20 20 20 20  / scale;.       
b3320 20 20 20 72 65 73 75 6c 74 20 2f 3d 20 31 2e 30     result /= 1.0
b3330 65 2b 33 30 38 3b 0a 20 20 20 20 20 20 20 20 7d  e+308;.        }
b3340 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
b3350 72 65 73 75 6c 74 20 3d 20 73 20 2a 20 73 63 61  result = s * sca
b3360 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65  le;.          re
b3370 73 75 6c 74 20 2a 3d 20 31 2e 30 65 2b 33 30 38  sult *= 1.0e+308
b3380 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
b3390 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
b33a0 20 2f 2a 20 31 2e 30 65 2b 32 32 20 69 73 20 74   /* 1.0e+22 is t
b33b0 68 65 20 6c 61 72 67 65 73 74 20 70 6f 77 65 72  he largest power
b33c0 20 6f 66 20 31 30 20 74 68 61 6e 20 63 61 6e 20   of 10 than can 
b33d0 62 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 72  be .        ** r
b33e0 65 70 72 65 73 65 6e 74 65 64 20 65 78 61 63 74  epresented exact
b33f0 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 77  ly. */.        w
b3400 68 69 6c 65 28 20 65 25 32 32 20 29 20 7b 20 73  hile( e%22 ) { s
b3410 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 3b 20  cale *= 1.0e+1; 
b3420 65 20 2d 3d 20 31 3b 20 7d 0a 20 20 20 20 20 20  e -= 1; }.      
b3430 20 20 77 68 69 6c 65 28 20 65 3e 30 20 29 20 7b    while( e>0 ) {
b3440 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 32   scale *= 1.0e+2
b3450 32 3b 20 65 20 2d 3d 20 32 32 3b 20 7d 0a 20 20  2; e -= 22; }.  
b3460 20 20 20 20 20 20 69 66 28 20 65 73 69 67 6e 3c        if( esign<
b3470 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  0 ){.          r
b3480 65 73 75 6c 74 20 3d 20 73 20 2f 20 73 63 61 6c  esult = s / scal
b3490 65 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  e;.        }else
b34a0 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 75  {.          resu
b34b0 6c 74 20 3d 20 73 20 2a 20 73 63 61 6c 65 3b 0a  lt = s * scale;.
b34c0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
b34d0 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20  }.    } else {. 
b34e0 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 28 64       result = (d
b34f0 6f 75 62 6c 65 29 73 3b 0a 20 20 20 20 7d 0a 20  ouble)s;.    }. 
b3500 20 7d 0a 0a 20 20 2f 2a 20 73 74 6f 72 65 20 74   }..  /* store t
b3510 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 2a  he result */.  *
b3520 70 52 65 73 75 6c 74 20 3d 20 72 65 73 75 6c 74  pResult = result
b3530 3b 0a 0a 20 20 2f 2a 20 72 65 74 75 72 6e 20 6e  ;..  /* return n
b3540 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
b3550 65 72 73 20 75 73 65 64 20 2a 2f 0a 20 20 72 65  ers used */.  re
b3560 74 75 72 6e 20 28 69 6e 74 29 28 7a 20 2d 20 7a  turn (int)(z - z
b3570 42 65 67 69 6e 29 3b 0a 23 65 6c 73 65 0a 20 20  Begin);.#else.  
b3580 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 74  return sqlite3At
b3590 6f 69 36 34 28 7a 2c 20 70 52 65 73 75 6c 74 29  oi64(z, pResult)
b35a0 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ;.#endif /* SQLI
b35b0 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
b35c0 5f 50 4f 49 4e 54 20 2a 2f 0a 7d 0a 0a 2f 2a 0a  _POINT */.}../*.
b35d0 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 31  ** Compare the 1
b35e0 39 2d 63 68 61 72 61 63 74 65 72 20 73 74 72 69  9-character stri
b35f0 6e 67 20 7a 4e 75 6d 20 61 67 61 69 6e 73 74 20  ng zNum against 
b3600 74 68 65 20 74 65 78 74 20 72 65 70 72 65 73 65  the text represe
b3610 6e 74 61 74 69 6f 6e 0a 2a 2a 20 76 61 6c 75 65  ntation.** value
b3620 20 32 5e 36 33 3a 20 20 39 32 32 33 33 37 32 30   2^63:  92233720
b3630 33 36 38 35 34 37 37 35 38 30 38 2e 20 20 52 65  36854775808.  Re
b3640 74 75 72 6e 20 6e 65 67 61 74 69 76 65 2c 20 7a  turn negative, z
b3650 65 72 6f 2c 20 6f 72 20 70 6f 73 69 74 69 76 65  ero, or positive
b3660 0a 2a 2a 20 69 66 20 7a 4e 75 6d 20 69 73 20 6c  .** if zNum is l
b3670 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c 20  ess than, equal 
b3680 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72 20 74  to, or greater t
b3690 68 61 6e 20 74 68 65 20 73 74 72 69 6e 67 2e 0a  han the string..
b36a0 2a 2a 0a 2a 2a 20 55 6e 6c 69 6b 65 20 6d 65 6d  **.** Unlike mem
b36b0 63 6d 70 28 29 20 74 68 69 73 20 72 6f 75 74 69  cmp() this routi
b36c0 6e 65 20 69 73 20 67 75 61 72 61 6e 74 65 65 64  ne is guaranteed
b36d0 20 74 6f 20 72 65 74 75 72 6e 20 74 68 65 20 64   to return the d
b36e0 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20 69 6e 20  ifference.** in 
b36f0 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 74 68  the values of th
b3700 65 20 6c 61 73 74 20 64 69 67 69 74 20 69 66 20  e last digit if 
b3710 74 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65  the only differe
b3720 6e 63 65 20 69 73 20 69 6e 20 74 68 65 0a 2a 2a  nce is in the.**
b3730 20 6c 61 73 74 20 64 69 67 69 74 2e 20 20 53 6f   last digit.  So
b3740 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a  , for example,.*
b3750 2a 0a 2a 2a 20 20 20 20 20 20 63 6f 6d 70 61 72  *.**      compar
b3760 65 32 70 6f 77 36 33 28 22 39 32 32 33 33 37 32  e2pow63("9223372
b3770 30 33 36 38 35 34 37 37 35 38 30 30 22 29 0a 2a  036854775800").*
b3780 2a 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75 72 6e  *.** will return
b3790 20 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69   -8..*/.static i
b37a0 6e 74 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33  nt compare2pow63
b37b0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75  (const char *zNu
b37c0 6d 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 20 20 63  m){.  int c;.  c
b37d0 20 3d 20 6d 65 6d 63 6d 70 28 7a 4e 75 6d 2c 22   = memcmp(zNum,"
b37e0 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35  9223372036854775
b37f0 38 30 22 2c 31 38 29 2a 31 30 3b 0a 20 20 69 66  80",18)*10;.  if
b3800 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 63 20  ( c==0 ){.    c 
b3810 3d 20 7a 4e 75 6d 5b 31 38 5d 20 2d 20 27 38 27  = zNum[18] - '8'
b3820 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63  ;.  }.  return c
b3830 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ;.}.../*.** Retu
b3840 72 6e 20 54 52 55 45 20 69 66 20 7a 4e 75 6d 20  rn TRUE if zNum 
b3850 69 73 20 61 20 36 34 2d 62 69 74 20 73 69 67 6e  is a 64-bit sign
b3860 65 64 20 69 6e 74 65 67 65 72 20 61 6e 64 20 77  ed integer and w
b3870 72 69 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c 75  rite.** the valu
b3880 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72  e of the integer
b3890 20 69 6e 74 6f 20 2a 70 4e 75 6d 2e 20 20 49 66   into *pNum.  If
b38a0 20 7a 4e 75 6d 20 69 73 20 6e 6f 74 20 61 6e 20   zNum is not an 
b38b0 69 6e 74 65 67 65 72 0a 2a 2a 20 6f 72 20 69 73  integer.** or is
b38c0 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61 74   an integer that
b38d0 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f   is too large to
b38e0 20 62 65 20 65 78 70 72 65 73 73 65 64 20 77 69   be expressed wi
b38f0 74 68 20 36 34 20 62 69 74 73 2c 0a 2a 2a 20 74  th 64 bits,.** t
b3900 68 65 6e 20 72 65 74 75 72 6e 20 66 61 6c 73 65  hen return false
b3910 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69  ..**.** When thi
b3920 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 6f 72  s routine was or
b3930 69 67 69 6e 61 6c 6c 79 20 77 72 69 74 74 65 6e  iginally written
b3940 20 69 74 20 64 65 61 6c 74 20 77 69 74 68 20 6f   it dealt with o
b3950 6e 6c 79 0a 2a 2a 20 33 32 2d 62 69 74 20 6e 75  nly.** 32-bit nu
b3960 6d 62 65 72 73 2e 20 20 41 74 20 74 68 61 74 20  mbers.  At that 
b3970 74 69 6d 65 2c 20 69 74 20 77 61 73 20 6d 75 63  time, it was muc
b3980 68 20 66 61 73 74 65 72 20 74 68 61 6e 20 74 68  h faster than th
b3990 65 0a 2a 2a 20 61 74 6f 69 28 29 20 6c 69 62 72  e.** atoi() libr
b39a0 61 72 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 52  ary routine in R
b39b0 65 64 48 61 74 20 37 2e 32 2e 0a 2a 2f 0a 53 51  edHat 7.2..*/.SQ
b39c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
b39d0 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 63   sqlite3Atoi64(c
b39e0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 2c  onst char *zNum,
b39f0 20 69 36 34 20 2a 70 4e 75 6d 29 7b 0a 20 20 69   i64 *pNum){.  i
b3a00 36 34 20 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20  64 v = 0;.  int 
b3a10 6e 65 67 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b  neg;.  int i, c;
b3a20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
b3a30 53 74 61 72 74 3b 0a 20 20 77 68 69 6c 65 28 20  Start;.  while( 
b3a40 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a  sqlite3Isspace(*
b3a50 7a 4e 75 6d 29 20 29 20 7a 4e 75 6d 2b 2b 3b 0a  zNum) ) zNum++;.
b3a60 20 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2d 27    if( *zNum=='-'
b3a70 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b   ){.    neg = 1;
b3a80 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d  .    zNum++;.  }
b3a90 65 6c 73 65 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d  else if( *zNum==
b3aa0 27 2b 27 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d  '+' ){.    neg =
b3ab0 20 30 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a   0;.    zNum++;.
b3ac0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 67    }else{.    neg
b3ad0 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 7a 53 74 61   = 0;.  }.  zSta
b3ae0 72 74 20 3d 20 7a 4e 75 6d 3b 0a 20 20 77 68 69  rt = zNum;.  whi
b3af0 6c 65 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 27  le( zNum[0]=='0'
b3b00 20 29 7b 20 7a 4e 75 6d 2b 2b 3b 20 7d 20 2f 2a   ){ zNum++; } /*
b3b10 20 53 6b 69 70 20 6f 76 65 72 20 6c 65 61 64 69   Skip over leadi
b3b20 6e 67 20 7a 65 72 6f 73 2e 20 54 69 63 6b 65 74  ng zeros. Ticket
b3b30 20 23 32 34 35 34 20 2a 2f 0a 20 20 66 6f 72 28   #2454 */.  for(
b3b40 69 3d 30 3b 20 28 63 3d 7a 4e 75 6d 5b 69 5d 29  i=0; (c=zNum[i])
b3b50 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 3b  >='0' && c<='9';
b3b60 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20 76   i++){.    v = v
b3b70 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20  *10 + c - '0';. 
b3b80 20 7d 0a 20 20 2a 70 4e 75 6d 20 3d 20 6e 65 67   }.  *pNum = neg
b3b90 20 3f 20 2d 76 20 3a 20 76 3b 0a 20 20 69 66 28   ? -v : v;.  if(
b3ba0 20 63 21 3d 30 20 7c 7c 20 28 69 3d 3d 30 20 26   c!=0 || (i==0 &
b3bb0 26 20 7a 53 74 61 72 74 3d 3d 7a 4e 75 6d 29 20  & zStart==zNum) 
b3bc0 7c 7c 20 69 3e 31 39 20 29 7b 0a 20 20 20 20 2f  || i>19 ){.    /
b3bd0 2a 20 7a 4e 75 6d 20 69 73 20 65 6d 70 74 79 20  * zNum is empty 
b3be0 6f 72 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 6e 2d  or contains non-
b3bf0 6e 75 6d 65 72 69 63 20 74 65 78 74 20 6f 72 20  numeric text or 
b3c00 69 73 20 6c 6f 6e 67 65 72 0a 20 20 20 20 2a 2a  is longer.    **
b3c10 20 74 68 61 6e 20 31 39 20 64 69 67 69 74 73 20   than 19 digits 
b3c20 28 74 68 75 73 20 67 75 61 72 61 6e 74 69 6e 67  (thus guaranting
b3c30 20 74 68 61 74 20 69 74 20 69 73 20 74 6f 6f 20   that it is too 
b3c40 6c 61 72 67 65 29 20 2a 2f 0a 20 20 20 20 72 65  large) */.    re
b3c50 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20  turn 0;.  }else 
b3c60 69 66 28 20 69 3c 31 39 20 29 7b 0a 20 20 20 20  if( i<19 ){.    
b3c70 2f 2a 20 4c 65 73 73 20 74 68 61 6e 20 31 39 20  /* Less than 19 
b3c80 64 69 67 69 74 73 2c 20 73 6f 20 77 65 20 6b 6e  digits, so we kn
b3c90 6f 77 20 74 68 61 74 20 69 74 20 66 69 74 73 20  ow that it fits 
b3ca0 69 6e 20 36 34 20 62 69 74 73 20 2a 2f 0a 20 20  in 64 bits */.  
b3cb0 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65    return 1;.  }e
b3cc0 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 31 39 2d 64  lse{.    /* 19-d
b3cd0 69 67 69 74 20 6e 75 6d 62 65 72 73 20 6d 75 73  igit numbers mus
b3ce0 74 20 62 65 20 6e 6f 20 6c 61 72 67 65 72 20 74  t be no larger t
b3cf0 68 61 6e 20 39 32 32 33 33 37 32 30 33 36 38 35  han 922337203685
b3d00 34 37 37 35 38 30 37 20 69 66 20 70 6f 73 69 74  4775807 if posit
b3d10 69 76 65 0a 20 20 20 20 2a 2a 20 6f 72 20 39 32  ive.    ** or 92
b3d20 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30  2337203685477580
b3d30 38 20 69 66 20 6e 65 67 61 74 69 76 65 2e 20 20  8 if negative.  
b3d40 4e 6f 74 65 20 74 68 61 74 20 39 32 32 33 33 37  Note that 922337
b3d50 32 30 33 36 38 35 34 36 36 35 38 30 38 0a 20 20  2036854665808.  
b3d60 20 20 2a 2a 20 69 73 20 32 5e 36 33 2e 20 2a 2f    ** is 2^63. */
b3d70 0a 20 20 20 20 72 65 74 75 72 6e 20 63 6f 6d 70  .    return comp
b3d80 61 72 65 32 70 6f 77 36 33 28 7a 4e 75 6d 29 3c  are2pow63(zNum)<
b3d90 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  neg;.  }.}../*.*
b3da0 2a 20 54 68 65 20 73 74 72 69 6e 67 20 7a 4e 75  * The string zNu
b3db0 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20  m represents an 
b3dc0 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  unsigned integer
b3dd0 2e 20 20 54 68 65 20 7a 4e 75 6d 20 73 74 72 69  .  The zNum stri
b3de0 6e 67 0a 2a 2a 20 63 6f 6e 73 69 73 74 73 20 6f  ng.** consists o
b3df0 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 64 69  f one or more di
b3e00 67 69 74 20 63 68 61 72 61 63 74 65 72 73 20 61  git characters a
b3e10 6e 64 20 69 73 20 74 65 72 6d 69 6e 61 74 65 64  nd is terminated
b3e20 20 62 79 0a 2a 2a 20 61 20 7a 65 72 6f 20 63 68   by.** a zero ch
b3e30 61 72 61 63 74 65 72 2e 20 20 41 6e 79 20 73 74  aracter.  Any st
b3e40 72 61 79 20 63 68 61 72 61 63 74 65 72 73 20 69  ray characters i
b3e50 6e 20 7a 4e 75 6d 20 72 65 73 75 6c 74 20 69 6e  n zNum result in
b3e60 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 62 65   undefined.** be
b3e70 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66  havior..**.** If
b3e80 20 74 68 65 20 75 6e 73 69 67 6e 65 64 20 69 6e   the unsigned in
b3e90 74 65 67 65 72 20 74 68 61 74 20 7a 4e 75 6d 20  teger that zNum 
b3ea0 72 65 70 72 65 73 65 6e 74 73 20 77 69 6c 6c 20  represents will 
b3eb0 66 69 74 20 69 6e 20 61 0a 2a 2a 20 36 34 2d 62  fit in a.** 64-b
b3ec0 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  it signed intege
b3ed0 72 2c 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20  r, return TRUE. 
b3ee0 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72   Otherwise retur
b3ef0 6e 20 46 41 4c 53 45 2e 0a 2a 2a 0a 2a 2a 20 49  n FALSE..**.** I
b3f00 66 20 74 68 65 20 6e 65 67 46 6c 61 67 20 70 61  f the negFlag pa
b3f10 72 61 6d 65 74 65 72 20 69 73 20 74 72 75 65 2c  rameter is true,
b3f20 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74   that means that
b3f30 20 7a 4e 75 6d 20 72 65 61 6c 6c 79 20 72 65 70   zNum really rep
b3f40 72 65 73 65 6e 74 73 0a 2a 2a 20 61 20 6e 65 67  resents.** a neg
b3f50 61 74 69 76 65 20 6e 75 6d 62 65 72 2e 20 20 28  ative number.  (
b3f60 54 68 65 20 6c 65 61 64 69 6e 67 20 22 2d 22 20  The leading "-" 
b3f70 69 73 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20  is omitted from 
b3f80 7a 4e 75 6d 2e 29 20 20 54 68 69 73 0a 2a 2a 20  zNum.)  This.** 
b3f90 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 65 65  parameter is nee
b3fa0 64 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  ded to determine
b3fb0 20 61 20 62 6f 75 6e 64 61 72 79 20 63 61 73 65   a boundary case
b3fc0 2e 20 20 41 20 73 74 72 69 6e 67 0a 2a 2a 20 6f  .  A string.** o
b3fd0 66 20 22 39 32 32 33 33 37 33 30 33 36 38 35 34  f "9223373036854
b3fe0 37 37 35 38 30 38 22 20 72 65 74 75 72 6e 73 20  775808" returns 
b3ff0 66 61 6c 73 65 20 69 66 20 6e 65 67 46 6c 61 67  false if negFlag
b4000 20 69 73 20 66 61 6c 73 65 20 6f 72 20 74 72 75   is false or tru
b4010 65 0a 2a 2a 20 69 66 20 6e 65 67 46 6c 61 67 20  e.** if negFlag 
b4020 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 4c  is true..**.** L
b4030 65 61 64 69 6e 67 20 7a 65 72 6f 73 20 61 72 65  eading zeros are
b4040 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51 4c   ignored..*/.SQL
b4050 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b4060 73 71 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42  sqlite3FitsIn64B
b4070 69 74 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  its(const char *
b4080 7a 4e 75 6d 2c 20 69 6e 74 20 6e 65 67 46 6c 61  zNum, int negFla
b4090 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  g){.  int i;.  i
b40a0 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 0a 20 20 61  nt neg = 0;..  a
b40b0 73 73 65 72 74 28 20 7a 4e 75 6d 5b 30 5d 3e 3d  ssert( zNum[0]>=
b40c0 27 30 27 20 26 26 20 7a 4e 75 6d 5b 30 5d 3c 3d  '0' && zNum[0]<=
b40d0 27 39 27 20 29 3b 20 2f 2a 20 7a 4e 75 6d 20 69  '9' ); /* zNum i
b40e0 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 6e 75  s an unsigned nu
b40f0 6d 62 65 72 20 2a 2f 0a 0a 20 20 69 66 28 20 6e  mber */..  if( n
b4100 65 67 46 6c 61 67 20 29 20 6e 65 67 20 3d 20 31  egFlag ) neg = 1
b4110 2d 6e 65 67 3b 0a 20 20 77 68 69 6c 65 28 20 2a  -neg;.  while( *
b4120 7a 4e 75 6d 3d 3d 27 30 27 20 29 7b 0a 20 20 20  zNum=='0' ){.   
b4130 20 7a 4e 75 6d 2b 2b 3b 20 20 20 2f 2a 20 53 6b   zNum++;   /* Sk
b4140 69 70 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73  ip leading zeros
b4150 2e 20 20 54 69 63 6b 65 74 20 23 32 34 35 34 20  .  Ticket #2454 
b4160 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  */.  }.  for(i=0
b4170 3b 20 7a 4e 75 6d 5b 69 5d 3b 20 69 2b 2b 29 7b  ; zNum[i]; i++){
b4180 20 61 73 73 65 72 74 28 20 7a 4e 75 6d 5b 69 5d   assert( zNum[i]
b4190 3e 3d 27 30 27 20 26 26 20 7a 4e 75 6d 5b 69 5d  >='0' && zNum[i]
b41a0 3c 3d 27 39 27 20 29 3b 20 7d 0a 20 20 69 66 28  <='9' ); }.  if(
b41b0 20 69 3c 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20   i<19 ){.    /* 
b41c0 47 75 61 72 61 6e 74 65 65 64 20 74 6f 20 66 69  Guaranteed to fi
b41d0 74 20 69 66 20 6c 65 73 73 20 74 68 61 6e 20 31  t if less than 1
b41e0 39 20 64 69 67 69 74 73 20 2a 2f 0a 20 20 20 20  9 digits */.    
b41f0 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73  return 1;.  }els
b4200 65 20 69 66 28 20 69 3e 31 39 20 29 7b 0a 20 20  e if( i>19 ){.  
b4210 20 20 2f 2a 20 47 75 61 72 61 6e 74 65 65 64 20    /* Guaranteed 
b4220 74 6f 20 62 65 20 74 6f 6f 20 62 69 67 20 69 66  to be too big if
b4230 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 31 39   greater than 19
b4240 20 64 69 67 69 74 73 20 2a 2f 0a 20 20 20 20 72   digits */.    r
b4250 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65  eturn 0;.  }else
b4260 7b 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 61 72 65  {.    /* Compare
b4270 20 61 67 61 69 6e 73 74 20 32 5e 36 33 2e 20 2a   against 2^63. *
b4280 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 63 6f 6d  /.    return com
b4290 70 61 72 65 32 70 6f 77 36 33 28 7a 4e 75 6d 29  pare2pow63(zNum)
b42a0 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  <neg;.  }.}../*.
b42b0 2a 2a 20 49 66 20 7a 4e 75 6d 20 72 65 70 72 65  ** If zNum repre
b42c0 73 65 6e 74 73 20 61 6e 20 69 6e 74 65 67 65 72  sents an integer
b42d0 20 74 68 61 74 20 77 69 6c 6c 20 66 69 74 20 69   that will fit i
b42e0 6e 20 33 32 2d 62 69 74 73 2c 20 74 68 65 6e 20  n 32-bits, then 
b42f0 73 65 74 0a 2a 2a 20 2a 70 56 61 6c 75 65 20 74  set.** *pValue t
b4300 6f 20 74 68 61 74 20 69 6e 74 65 67 65 72 20 61  o that integer a
b4310 6e 64 20 72 65 74 75 72 6e 20 74 72 75 65 2e 20  nd return true. 
b4320 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72   Otherwise retur
b4330 6e 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 41  n false..**.** A
b4340 6e 79 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 20 63  ny non-numeric c
b4350 68 61 72 61 63 74 65 72 73 20 74 68 61 74 20 66  haracters that f
b4360 6f 6c 6c 6f 77 69 6e 67 20 7a 4e 75 6d 20 61 72  ollowing zNum ar
b4370 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20 54 68  e ignored..** Th
b4380 69 73 20 69 73 20 64 69 66 66 65 72 65 6e 74 20  is is different 
b4390 66 72 6f 6d 20 73 71 6c 69 74 65 33 41 74 6f 69  from sqlite3Atoi
b43a0 36 34 28 29 20 77 68 69 63 68 20 72 65 71 75 69  64() which requi
b43b0 72 65 73 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74  res the.** input
b43c0 20 6e 75 6d 62 65 72 20 74 6f 20 62 65 20 7a 65   number to be ze
b43d0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a 2a  ro-terminated..*
b43e0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b43f0 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 49   int sqlite3GetI
b4400 6e 74 33 32 28 63 6f 6e 73 74 20 63 68 61 72 20  nt32(const char 
b4410 2a 7a 4e 75 6d 2c 20 69 6e 74 20 2a 70 56 61 6c  *zNum, int *pVal
b4420 75 65 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e  ue){.  sqlite_in
b4430 74 36 34 20 76 20 3d 20 30 3b 0a 20 20 69 6e 74  t64 v = 0;.  int
b4440 20 69 2c 20 63 3b 0a 20 20 69 6e 74 20 6e 65 67   i, c;.  int neg
b4450 20 3d 20 30 3b 0a 20 20 69 66 28 20 7a 4e 75 6d   = 0;.  if( zNum
b4460 5b 30 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20  [0]=='-' ){.    
b4470 6e 65 67 20 3d 20 31 3b 0a 20 20 20 20 7a 4e 75  neg = 1;.    zNu
b4480 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  m++;.  }else if(
b4490 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2b 27 20 29 7b   zNum[0]=='+' ){
b44a0 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d  .    zNum++;.  }
b44b0 0a 20 20 77 68 69 6c 65 28 20 7a 4e 75 6d 5b 30  .  while( zNum[0
b44c0 5d 3d 3d 27 30 27 20 29 20 7a 4e 75 6d 2b 2b 3b  ]=='0' ) zNum++;
b44d0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 31  .  for(i=0; i<11
b44e0 20 26 26 20 28 63 20 3d 20 7a 4e 75 6d 5b 69 5d   && (c = zNum[i]
b44f0 20 2d 20 27 30 27 29 3e 3d 30 20 26 26 20 63 3c   - '0')>=0 && c<
b4500 3d 39 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20  =9; i++){.    v 
b4510 3d 20 76 2a 31 30 20 2b 20 63 3b 0a 20 20 7d 0a  = v*10 + c;.  }.
b4520 0a 20 20 2f 2a 20 54 68 65 20 6c 6f 6e 67 65 73  .  /* The longes
b4530 74 20 64 65 63 69 6d 61 6c 20 72 65 70 72 65 73  t decimal repres
b4540 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 33 32  entation of a 32
b4550 20 62 69 74 20 69 6e 74 65 67 65 72 20 69 73 20   bit integer is 
b4560 31 30 20 64 69 67 69 74 73 3a 0a 20 20 2a 2a 0a  10 digits:.  **.
b4570 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20    **            
b4580 20 31 32 33 34 35 36 37 38 39 30 0a 20 20 2a 2a   1234567890.  **
b4590 20 20 20 20 20 32 5e 33 31 20 2d 3e 20 32 31 34       2^31 -> 214
b45a0 37 34 38 33 36 34 38 0a 20 20 2a 2f 0a 20 20 69  7483648.  */.  i
b45b0 66 28 20 69 3e 31 30 20 29 7b 0a 20 20 20 20 72  f( i>10 ){.    r
b45c0 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69  eturn 0;.  }.  i
b45d0 66 28 20 76 2d 6e 65 67 3e 32 31 34 37 34 38 33  f( v-neg>2147483
b45e0 36 34 37 20 29 7b 0a 20 20 20 20 72 65 74 75 72  647 ){.    retur
b45f0 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e  n 0;.  }.  if( n
b4600 65 67 20 29 7b 0a 20 20 20 20 76 20 3d 20 2d 76  eg ){.    v = -v
b4610 3b 0a 20 20 7d 0a 20 20 2a 70 56 61 6c 75 65 20  ;.  }.  *pValue 
b4620 3d 20 28 69 6e 74 29 76 3b 0a 20 20 72 65 74 75  = (int)v;.  retu
b4630 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  rn 1;.}../*.** T
b4640 68 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67  he variable-leng
b4650 74 68 20 69 6e 74 65 67 65 72 20 65 6e 63 6f 64  th integer encod
b4660 69 6e 67 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77  ing is as follow
b4670 73 3a 0a 2a 2a 0a 2a 2a 20 4b 45 59 3a 0a 2a 2a  s:.**.** KEY:.**
b4680 20 20 20 20 20 20 20 20 20 41 20 3d 20 30 78 78           A = 0xx
b4690 78 78 78 78 78 20 20 20 20 37 20 62 69 74 73 20  xxxxx    7 bits 
b46a0 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 20  of data and one 
b46b0 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20 20 20 20  flag bit.**     
b46c0 20 20 20 20 42 20 3d 20 31 78 78 78 78 78 78 78      B = 1xxxxxxx
b46d0 20 20 20 20 37 20 62 69 74 73 20 6f 66 20 64 61      7 bits of da
b46e0 74 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20  ta and one flag 
b46f0 62 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 43  bit.**         C
b4700 20 3d 20 78 78 78 78 78 78 78 78 20 20 20 20 38   = xxxxxxxx    8
b4710 20 62 69 74 73 20 6f 66 20 64 61 74 61 0a 2a 2a   bits of data.**
b4720 0a 2a 2a 20 20 37 20 62 69 74 73 20 2d 20 41 0a  .**  7 bits - A.
b4730 2a 2a 20 31 34 20 62 69 74 73 20 2d 20 42 41 0a  ** 14 bits - BA.
b4740 2a 2a 20 32 31 20 62 69 74 73 20 2d 20 42 42 41  ** 21 bits - BBA
b4750 0a 2a 2a 20 32 38 20 62 69 74 73 20 2d 20 42 42  .** 28 bits - BB
b4760 42 41 0a 2a 2a 20 33 35 20 62 69 74 73 20 2d 20  BA.** 35 bits - 
b4770 42 42 42 42 41 0a 2a 2a 20 34 32 20 62 69 74 73  BBBBA.** 42 bits
b4780 20 2d 20 42 42 42 42 42 41 0a 2a 2a 20 34 39 20   - BBBBBA.** 49 
b4790 62 69 74 73 20 2d 20 42 42 42 42 42 42 41 0a 2a  bits - BBBBBBA.*
b47a0 2a 20 35 36 20 62 69 74 73 20 2d 20 42 42 42 42  * 56 bits - BBBB
b47b0 42 42 42 41 0a 2a 2a 20 36 34 20 62 69 74 73 20  BBBA.** 64 bits 
b47c0 2d 20 42 42 42 42 42 42 42 42 43 0a 2a 2f 0a 0a  - BBBBBBBBC.*/..
b47d0 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 36 34  /*.** Write a 64
b47e0 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65  -bit variable-le
b47f0 6e 67 74 68 20 69 6e 74 65 67 65 72 20 74 6f 20  ngth integer to 
b4800 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20  memory starting 
b4810 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 54 68 65 20  at p[0]..** The 
b4820 6c 65 6e 67 74 68 20 6f 66 20 64 61 74 61 20 77  length of data w
b4830 72 69 74 65 20 77 69 6c 6c 20 62 65 20 62 65 74  rite will be bet
b4840 77 65 65 6e 20 31 20 61 6e 64 20 39 20 62 79 74  ween 1 and 9 byt
b4850 65 73 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 0a  es.  The number.
b4860 2a 2a 20 6f 66 20 62 79 74 65 73 20 77 72 69 74  ** of bytes writ
b4870 74 65 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ten is returned.
b4880 0a 2a 2a 0a 2a 2a 20 41 20 76 61 72 69 61 62 6c  .**.** A variabl
b4890 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
b48a0 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 65   consists of the
b48b0 20 6c 6f 77 65 72 20 37 20 62 69 74 73 20 6f 66   lower 7 bits of
b48c0 20 65 61 63 68 20 62 79 74 65 0a 2a 2a 20 66 6f   each byte.** fo
b48d0 72 20 61 6c 6c 20 62 79 74 65 73 20 74 68 61 74  r all bytes that
b48e0 20 68 61 76 65 20 74 68 65 20 38 74 68 20 62 69   have the 8th bi
b48f0 74 20 73 65 74 20 61 6e 64 20 6f 6e 65 20 62 79  t set and one by
b4900 74 65 20 77 69 74 68 20 74 68 65 20 38 74 68 0a  te with the 8th.
b4910 2a 2a 20 62 69 74 20 63 6c 65 61 72 2e 20 20 45  ** bit clear.  E
b4920 78 63 65 70 74 2c 20 69 66 20 77 65 20 67 65 74  xcept, if we get
b4930 20 74 6f 20 74 68 65 20 39 74 68 20 62 79 74 65   to the 9th byte
b4940 2c 20 69 74 20 73 74 6f 72 65 73 20 74 68 65 20  , it stores the 
b4950 66 75 6c 6c 0a 2a 2a 20 38 20 62 69 74 73 20 61  full.** 8 bits a
b4960 6e 64 20 69 73 20 74 68 65 20 6c 61 73 74 20 62  nd is the last b
b4970 79 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  yte..*/.SQLITE_P
b4980 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
b4990 65 33 50 75 74 56 61 72 69 6e 74 28 75 6e 73 69  e3PutVarint(unsi
b49a0 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 36  gned char *p, u6
b49b0 34 20 76 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a  4 v){.  int i, j
b49c0 2c 20 6e 3b 0a 20 20 75 38 20 62 75 66 5b 31 30  , n;.  u8 buf[10
b49d0 5d 3b 0a 20 20 69 66 28 20 76 20 26 20 28 28 28  ];.  if( v & (((
b49e0 75 36 34 29 30 78 66 66 30 30 30 30 30 30 29 3c  u64)0xff000000)<
b49f0 3c 33 32 29 20 29 7b 0a 20 20 20 20 70 5b 38 5d  <32) ){.    p[8]
b4a00 20 3d 20 28 75 38 29 76 3b 0a 20 20 20 20 76 20   = (u8)v;.    v 
b4a10 3e 3e 3d 20 38 3b 0a 20 20 20 20 66 6f 72 28 69  >>= 8;.    for(i
b4a20 3d 37 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a  =7; i>=0; i--){.
b4a30 20 20 20 20 20 20 70 5b 69 5d 20 3d 20 28 75 38        p[i] = (u8
b4a40 29 28 28 76 20 26 20 30 78 37 66 29 20 7c 20 30  )((v & 0x7f) | 0
b4a50 78 38 30 29 3b 0a 20 20 20 20 20 20 76 20 3e 3e  x80);.      v >>
b4a60 3d 20 37 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  = 7;.    }.    r
b4a70 65 74 75 72 6e 20 39 3b 0a 20 20 7d 20 20 20 20  eturn 9;.  }    
b4a80 0a 20 20 6e 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a  .  n = 0;.  do{.
b4a90 20 20 20 20 62 75 66 5b 6e 2b 2b 5d 20 3d 20 28      buf[n++] = (
b4aa0 75 38 29 28 28 76 20 26 20 30 78 37 66 29 20 7c  u8)((v & 0x7f) |
b4ab0 20 30 78 38 30 29 3b 0a 20 20 20 20 76 20 3e 3e   0x80);.    v >>
b4ac0 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76  = 7;.  }while( v
b4ad0 21 3d 30 20 29 3b 0a 20 20 62 75 66 5b 30 5d 20  !=0 );.  buf[0] 
b4ae0 26 3d 20 30 78 37 66 3b 0a 20 20 61 73 73 65 72  &= 0x7f;.  asser
b4af0 74 28 20 6e 3c 3d 39 20 29 3b 0a 20 20 66 6f 72  t( n<=9 );.  for
b4b00 28 69 3d 30 2c 20 6a 3d 6e 2d 31 3b 20 6a 3e 3d  (i=0, j=n-1; j>=
b4b10 30 3b 20 6a 2d 2d 2c 20 69 2b 2b 29 7b 0a 20 20  0; j--, i++){.  
b4b20 20 20 70 5b 69 5d 20 3d 20 62 75 66 5b 6a 5d 3b    p[i] = buf[j];
b4b30 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3b  .  }.  return n;
b4b40 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
b4b50 6f 75 74 69 6e 65 20 69 73 20 61 20 66 61 73 74  outine is a fast
b4b60 65 72 20 76 65 72 73 69 6f 6e 20 6f 66 20 73 71  er version of sq
b4b70 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 28 29  lite3PutVarint()
b4b80 20 74 68 61 74 20 6f 6e 6c 79 0a 2a 2a 20 77 6f   that only.** wo
b4b90 72 6b 73 20 66 6f 72 20 33 32 2d 62 69 74 20 70  rks for 32-bit p
b4ba0 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 73  ositive integers
b4bb0 20 61 6e 64 20 77 68 69 63 68 20 69 73 20 6f 70   and which is op
b4bc0 74 69 6d 69 7a 65 64 20 66 6f 72 0a 2a 2a 20 74  timized for.** t
b4bd0 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 6f  he common case o
b4be0 66 20 73 6d 61 6c 6c 20 69 6e 74 65 67 65 72 73  f small integers
b4bf0 2e 20 20 41 20 4d 41 43 52 4f 20 76 65 72 73 69  .  A MACRO versi
b4c00 6f 6e 2c 20 70 75 74 56 61 72 69 6e 74 33 32 2c  on, putVarint32,
b4c10 0a 2a 2a 20 69 73 20 70 72 6f 76 69 64 65 64 20  .** is provided 
b4c20 77 68 69 63 68 20 69 6e 6c 69 6e 65 73 20 74 68  which inlines th
b4c30 65 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61  e single-byte ca
b4c40 73 65 2e 20 20 41 6c 6c 20 63 6f 64 65 20 73 68  se.  All code sh
b4c50 6f 75 6c 64 20 75 73 65 0a 2a 2a 20 74 68 65 20  ould use.** the 
b4c60 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 20 61 73  MACRO version as
b4c70 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   this function a
b4c80 73 73 75 6d 65 73 20 74 68 65 20 73 69 6e 67 6c  ssumes the singl
b4c90 65 2d 62 79 74 65 20 63 61 73 65 20 68 61 73 0a  e-byte case has.
b4ca0 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ** already been 
b4cb0 68 61 6e 64 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49  handled..*/.SQLI
b4cc0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
b4cd0 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 33  qlite3PutVarint3
b4ce0 32 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  2(unsigned char 
b4cf0 2a 70 2c 20 75 33 32 20 76 29 7b 0a 23 69 66 6e  *p, u32 v){.#ifn
b4d00 64 65 66 20 70 75 74 56 61 72 69 6e 74 33 32 0a  def putVarint32.
b4d10 20 20 69 66 28 20 28 76 20 26 20 7e 30 78 37 66    if( (v & ~0x7f
b4d20 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 5b 30 5d  )==0 ){.    p[0]
b4d30 20 3d 20 76 3b 0a 20 20 20 20 72 65 74 75 72 6e   = v;.    return
b4d40 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20   1;.  }.#endif. 
b4d50 20 69 66 28 20 28 76 20 26 20 7e 30 78 33 66 66   if( (v & ~0x3ff
b4d60 66 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 5b 30  f)==0 ){.    p[0
b4d70 5d 20 3d 20 28 75 38 29 28 28 76 3e 3e 37 29 20  ] = (u8)((v>>7) 
b4d80 7c 20 30 78 38 30 29 3b 0a 20 20 20 20 70 5b 31  | 0x80);.    p[1
b4d90 5d 20 3d 20 28 75 38 29 28 76 20 26 20 30 78 37  ] = (u8)(v & 0x7
b4da0 66 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 32  f);.    return 2
b4db0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73  ;.  }.  return s
b4dc0 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 28  qlite3PutVarint(
b4dd0 70 2c 20 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  p, v);.}../*.** 
b4de0 52 65 61 64 20 61 20 36 34 2d 62 69 74 20 76 61  Read a 64-bit va
b4df0 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e  riable-length in
b4e00 74 65 67 65 72 20 66 72 6f 6d 20 6d 65 6d 6f 72  teger from memor
b4e10 79 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 5b  y starting at p[
b4e20 30 5d 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  0]..** Return th
b4e30 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
b4e40 73 20 72 65 61 64 2e 20 20 54 68 65 20 76 61 6c  s read.  The val
b4e50 75 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  ue is stored in 
b4e60 2a 76 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  *v..*/.SQLITE_PR
b4e70 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 33  IVATE u8 sqlite3
b4e80 47 65 74 56 61 72 69 6e 74 28 63 6f 6e 73 74 20  GetVarint(const 
b4e90 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
b4ea0 2c 20 75 36 34 20 2a 76 29 7b 0a 20 20 75 33 32  , u64 *v){.  u32
b4eb0 20 61 2c 62 2c 73 3b 0a 0a 20 20 61 20 3d 20 2a   a,b,s;..  a = *
b4ec0 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 20 28 75  p;.  /* a: p0 (u
b4ed0 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66  nmasked) */.  if
b4ee0 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b   (!(a&0x80)).  {
b4ef0 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20  .    *v = a;.   
b4f00 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a   return 1;.  }..
b4f10 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b    p++;.  b = *p;
b4f20 0a 20 20 2f 2a 20 62 3a 20 70 31 20 28 75 6e 6d  .  /* b: p1 (unm
b4f30 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28  asked) */.  if (
b4f40 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20  !(b&0x80)).  {. 
b4f50 20 20 20 61 20 26 3d 20 30 78 37 66 3b 0a 20 20     a &= 0x7f;.  
b4f60 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20    a = a<<7;.    
b4f70 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d  a |= b;.    *v =
b4f80 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 32   a;.    return 2
b4f90 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20  ;.  }..  p++;.  
b4fa0 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c  a = a<<14;.  a |
b4fb0 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30  = *p;.  /* a: p0
b4fc0 3c 3c 31 34 20 7c 20 70 32 20 28 75 6e 6d 61 73  <<14 | p2 (unmas
b4fd0 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28  ked) */.  if (!(
b4fe0 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20  a&0x80)).  {.   
b4ff0 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29   a &= (0x7f<<14)
b5000 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20 26  |(0x7f);.    b &
b5010 3d 20 30 78 37 66 3b 0a 20 20 20 20 62 20 3d 20  = 0x7f;.    b = 
b5020 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62  b<<7;.    a |= b
b5030 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20  ;.    *v = a;.  
b5040 20 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 7d 0a    return 3;.  }.
b5050 0a 20 20 2f 2a 20 43 53 45 31 20 66 72 6f 6d 20  .  /* CSE1 from 
b5060 62 65 6c 6f 77 20 2a 2f 0a 20 20 61 20 26 3d 20  below */.  a &= 
b5070 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66  (0x7f<<14)|(0x7f
b5080 29 3b 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20  );.  p++;.  b = 
b5090 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70  b<<14;.  b |= *p
b50a0 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 31 34  ;.  /* b: p1<<14
b50b0 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b 65 64 29   | p3 (unmasked)
b50c0 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78   */.  if (!(b&0x
b50d0 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 62 20 26  80)).  {.    b &
b50e0 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  = (0x7f<<14)|(0x
b50f0 37 66 29 3b 0a 20 20 20 20 2f 2a 20 6d 6f 76 65  7f);.    /* move
b5100 64 20 43 53 45 31 20 75 70 20 2a 2f 0a 20 20 20  d CSE1 up */.   
b5110 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c   /* a &= (0x7f<<
b5120 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20  14)|(0x7f); */. 
b5130 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20     a = a<<7;.   
b5140 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 20   a |= b;.    *v 
b5150 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = a;.    return 
b5160 34 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 3a 20  4;.  }..  /* a: 
b5170 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 6d 61 73  p0<<14 | p2 (mas
b5180 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 62 3a 20  ked) */.  /* b: 
b5190 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 6e 6d  p1<<14 | p3 (unm
b51a0 61 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 31  asked) */.  /* 1
b51b0 3a 73 61 76 65 20 6f 66 66 20 70 30 3c 3c 32 31  :save off p0<<21
b51c0 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c   | p1<<14 | p2<<
b51d0 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 29 20  7 | p3 (masked) 
b51e0 2a 2f 0a 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53  */.  /* moved CS
b51f0 45 31 20 75 70 20 2a 2f 0a 20 20 2f 2a 20 61 20  E1 up */.  /* a 
b5200 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30  &= (0x7f<<14)|(0
b5210 78 37 66 29 3b 20 2a 2f 0a 20 20 62 20 26 3d 20  x7f); */.  b &= 
b5220 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66  (0x7f<<14)|(0x7f
b5230 29 3b 0a 20 20 73 20 3d 20 61 3b 0a 20 20 2f 2a  );.  s = a;.  /*
b5240 20 73 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20   s: p0<<14 | p2 
b5250 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 70  (masked) */..  p
b5260 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b  ++;.  a = a<<14;
b5270 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a  .  a |= *p;.  /*
b5280 20 61 3a 20 70 30 3c 3c 32 38 20 7c 20 70 32 3c   a: p0<<28 | p2<
b5290 3c 31 34 20 7c 20 70 34 20 28 75 6e 6d 61 73 6b  <14 | p4 (unmask
b52a0 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61  ed) */.  if (!(a
b52b0 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20  &0x80)).  {.    
b52c0 2f 2a 20 77 65 20 63 61 6e 20 73 6b 69 70 20 74  /* we can skip t
b52d0 68 65 73 65 20 63 61 75 73 65 20 74 68 65 79 20  hese cause they 
b52e0 77 65 72 65 20 28 65 66 66 65 63 74 69 76 65 6c  were (effectivel
b52f0 79 29 20 64 6f 6e 65 20 61 62 6f 76 65 20 69 6e  y) done above in
b5300 20 63 61 6c 63 27 69 6e 67 20 73 20 2a 2f 0a 20   calc'ing s */. 
b5310 20 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66     /* a &= (0x7f
b5320 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29  <<28)|(0x7f<<14)
b5330 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20  |(0x7f); */.    
b5340 2f 2a 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31  /* b &= (0x7f<<1
b5350 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20  4)|(0x7f); */.  
b5360 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20    b = b<<7;.    
b5370 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20  a |= b;.    s = 
b5380 73 3e 3e 31 38 3b 0a 20 20 20 20 2a 76 20 3d 20  s>>18;.    *v = 
b5390 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61  ((u64)s)<<32 | a
b53a0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 35 3b 0a  ;.    return 5;.
b53b0 20 20 7d 0a 0a 20 20 2f 2a 20 32 3a 73 61 76 65    }..  /* 2:save
b53c0 20 6f 66 66 20 70 30 3c 3c 32 31 20 7c 20 70 31   off p0<<21 | p1
b53d0 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70  <<14 | p2<<7 | p
b53e0 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20  3 (masked) */.  
b53f0 73 20 3d 20 73 3c 3c 37 3b 0a 20 20 73 20 7c 3d  s = s<<7;.  s |=
b5400 20 62 3b 0a 20 20 2f 2a 20 73 3a 20 70 30 3c 3c   b;.  /* s: p0<<
b5410 32 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32  21 | p1<<14 | p2
b5420 3c 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 64  <<7 | p3 (masked
b5430 29 20 2a 2f 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62  ) */..  p++;.  b
b5440 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d   = b<<14;.  b |=
b5450 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 3c   *p;.  /* b: p1<
b5460 3c 32 38 20 7c 20 70 33 3c 3c 31 34 20 7c 20 70  <28 | p3<<14 | p
b5470 35 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a  5 (unmasked) */.
b5480 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29    if (!(b&0x80))
b5490 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 77 65 20 63  .  {.    /* we c
b54a0 61 6e 20 73 6b 69 70 20 74 68 69 73 20 63 61 75  an skip this cau
b54b0 73 65 20 69 74 20 77 61 73 20 28 65 66 66 65 63  se it was (effec
b54c0 74 69 76 65 6c 79 29 20 64 6f 6e 65 20 61 62 6f  tively) done abo
b54d0 76 65 20 69 6e 20 63 61 6c 63 27 69 6e 67 20 73  ve in calc'ing s
b54e0 20 2a 2f 0a 20 20 20 20 2f 2a 20 62 20 26 3d 20   */.    /* b &= 
b54f0 28 30 78 37 66 3c 3c 32 38 29 7c 28 30 78 37 66  (0x7f<<28)|(0x7f
b5500 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f  <<14)|(0x7f); */
b5510 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c  .    a &= (0x7f<
b5520 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20  <14)|(0x7f);.   
b5530 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61   a = a<<7;.    a
b5540 20 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73   |= b;.    s = s
b5550 3e 3e 31 38 3b 0a 20 20 20 20 2a 76 20 3d 20 28  >>18;.    *v = (
b5560 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b  (u64)s)<<32 | a;
b5570 0a 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20  .    return 6;. 
b5580 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d   }..  p++;.  a =
b5590 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a   a<<14;.  a |= *
b55a0 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 32 3c 3c 32  p;.  /* a: p2<<2
b55b0 38 20 7c 20 70 34 3c 3c 31 34 20 7c 20 70 36 20  8 | p4<<14 | p6 
b55c0 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20  (unmasked) */.  
b55d0 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20  if (!(a&0x80)). 
b55e0 20 7b 0a 20 20 20 20 61 20 26 3d 20 28 30 78 31   {.    a &= (0x1
b55f0 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34  f<<28)|(0x7f<<14
b5600 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20  )|(0x7f);.    b 
b5610 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30  &= (0x7f<<14)|(0
b5620 78 37 66 29 3b 0a 20 20 20 20 62 20 3d 20 62 3c  x7f);.    b = b<
b5630 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a  <7;.    a |= b;.
b5640 20 20 20 20 73 20 3d 20 73 3e 3e 31 31 3b 0a 20      s = s>>11;. 
b5650 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29     *v = ((u64)s)
b5660 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65  <<32 | a;.    re
b5670 74 75 72 6e 20 37 3b 0a 20 20 7d 0a 0a 20 20 2f  turn 7;.  }..  /
b5680 2a 20 43 53 45 32 20 66 72 6f 6d 20 62 65 6c 6f  * CSE2 from belo
b5690 77 20 2a 2f 0a 20 20 61 20 26 3d 20 28 30 78 37  w */.  a &= (0x7
b56a0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20  f<<14)|(0x7f);. 
b56b0 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31   p++;.  b = b<<1
b56c0 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20  4;.  b |= *p;.  
b56d0 2f 2a 20 62 3a 20 70 33 3c 3c 32 38 20 7c 20 70  /* b: p3<<28 | p
b56e0 35 3c 3c 31 34 20 7c 20 70 37 20 28 75 6e 6d 61  5<<14 | p7 (unma
b56f0 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21  sked) */.  if (!
b5700 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20  (b&0x80)).  {.  
b5710 20 20 62 20 26 3d 20 28 30 78 31 66 3c 3c 32 38    b &= (0x1f<<28
b5720 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  )|(0x7f<<14)|(0x
b5730 37 66 29 3b 0a 20 20 20 20 2f 2a 20 6d 6f 76 65  7f);.    /* move
b5740 64 20 43 53 45 32 20 75 70 20 2a 2f 0a 20 20 20  d CSE2 up */.   
b5750 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c   /* a &= (0x7f<<
b5760 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20  14)|(0x7f); */. 
b5770 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20     a = a<<7;.   
b5780 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d   a |= b;.    s =
b5790 20 73 3e 3e 34 3b 0a 20 20 20 20 2a 76 20 3d 20   s>>4;.    *v = 
b57a0 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61  ((u64)s)<<32 | a
b57b0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 38 3b 0a  ;.    return 8;.
b57c0 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20    }..  p++;.  a 
b57d0 3d 20 61 3c 3c 31 35 3b 0a 20 20 61 20 7c 3d 20  = a<<15;.  a |= 
b57e0 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 34 3c 3c  *p;.  /* a: p4<<
b57f0 32 39 20 7c 20 70 36 3c 3c 31 35 20 7c 20 70 38  29 | p6<<15 | p8
b5800 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a   (unmasked) */..
b5810 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 32 20    /* moved CSE2 
b5820 75 70 20 2a 2f 0a 20 20 2f 2a 20 61 20 26 3d 20  up */.  /* a &= 
b5830 28 30 78 37 66 3c 3c 32 39 29 7c 28 30 78 37 66  (0x7f<<29)|(0x7f
b5840 3c 3c 31 35 29 7c 28 30 78 66 66 29 3b 20 2a 2f  <<15)|(0xff); */
b5850 0a 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31  .  b &= (0x7f<<1
b5860 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 62 20 3d  4)|(0x7f);.  b =
b5870 20 62 3c 3c 38 3b 0a 20 20 61 20 7c 3d 20 62 3b   b<<8;.  a |= b;
b5880 0a 0a 20 20 73 20 3d 20 73 3c 3c 34 3b 0a 20 20  ..  s = s<<4;.  
b5890 62 20 3d 20 70 5b 2d 34 5d 3b 0a 20 20 62 20 26  b = p[-4];.  b &
b58a0 3d 20 30 78 37 66 3b 0a 20 20 62 20 3d 20 62 3e  = 0x7f;.  b = b>
b58b0 3e 33 3b 0a 20 20 73 20 7c 3d 20 62 3b 0a 0a 20  >3;.  s |= b;.. 
b58c0 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c   *v = ((u64)s)<<
b58d0 33 32 20 7c 20 61 3b 0a 0a 20 20 72 65 74 75 72  32 | a;..  retur
b58e0 6e 20 39 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  n 9;.}../*.** Re
b58f0 61 64 20 61 20 33 32 2d 62 69 74 20 76 61 72 69  ad a 32-bit vari
b5900 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65  able-length inte
b5910 67 65 72 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20  ger from memory 
b5920 73 74 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d  starting at p[0]
b5930 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ..** Return the 
b5940 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
b5950 72 65 61 64 2e 20 20 54 68 65 20 76 61 6c 75 65  read.  The value
b5960 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 2a 76   is stored in *v
b5970 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76  ..**.** If the v
b5980 61 72 69 6e 74 20 73 74 6f 72 65 64 20 69 6e 20  arint stored in 
b5990 70 5b 30 5d 20 69 73 20 6c 61 72 67 65 72 20 74  p[0] is larger t
b59a0 68 61 6e 20 63 61 6e 20 66 69 74 20 69 6e 20 61  han can fit in a
b59b0 20 33 32 2d 62 69 74 20 75 6e 73 69 67 6e 65 64   32-bit unsigned
b59c0 0a 2a 2a 20 69 6e 74 65 67 65 72 2c 20 74 68 65  .** integer, the
b59d0 6e 20 73 65 74 20 2a 76 20 74 6f 20 30 78 66 66  n set *v to 0xff
b59e0 66 66 66 66 66 66 2e 0a 2a 2a 0a 2a 2a 20 41 20  ffffff..**.** A 
b59f0 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 2c 20 67  MACRO version, g
b5a00 65 74 56 61 72 69 6e 74 33 32 2c 20 69 73 20 70  etVarint32, is p
b5a10 72 6f 76 69 64 65 64 20 77 68 69 63 68 20 69 6e  rovided which in
b5a20 6c 69 6e 65 73 20 74 68 65 20 0a 2a 2a 20 73 69  lines the .** si
b5a30 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 2e 20  ngle-byte case. 
b5a40 20 41 6c 6c 20 63 6f 64 65 20 73 68 6f 75 6c 64   All code should
b5a50 20 75 73 65 20 74 68 65 20 4d 41 43 52 4f 20 76   use the MACRO v
b5a60 65 72 73 69 6f 6e 20 61 73 20 0a 2a 2a 20 74 68  ersion as .** th
b5a70 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75  is function assu
b5a80 6d 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62  mes the single-b
b5a90 79 74 65 20 63 61 73 65 20 68 61 73 20 61 6c 72  yte case has alr
b5aa0 65 61 64 79 20 62 65 65 6e 20 68 61 6e 64 6c 65  eady been handle
b5ab0 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
b5ac0 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 47  VATE u8 sqlite3G
b5ad0 65 74 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74  etVarint32(const
b5ae0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
b5af0 70 2c 20 75 33 32 20 2a 76 29 7b 0a 20 20 75 33  p, u32 *v){.  u3
b5b00 32 20 61 2c 62 3b 0a 0a 20 20 2f 2a 20 54 68 65  2 a,b;..  /* The
b5b10 20 31 2d 62 79 74 65 20 63 61 73 65 2e 20 20 4f   1-byte case.  O
b5b20 76 65 72 77 68 65 6c 6d 69 6e 67 6c 79 20 74 68  verwhelmingly th
b5b30 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 2e 20 20  e most common.  
b5b40 48 61 6e 64 6c 65 64 20 69 6e 6c 69 6e 65 0a 20  Handled inline. 
b5b50 20 2a 2a 20 62 79 20 74 68 65 20 67 65 74 56 61   ** by the getVa
b5b60 72 69 6e 33 32 28 29 20 6d 61 63 72 6f 20 2a 2f  rin32() macro */
b5b70 0a 20 20 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20  .  a = *p;.  /* 
b5b80 61 3a 20 70 30 20 28 75 6e 6d 61 73 6b 65 64 29  a: p0 (unmasked)
b5b90 20 2a 2f 0a 23 69 66 6e 64 65 66 20 67 65 74 56   */.#ifndef getV
b5ba0 61 72 69 6e 74 33 32 0a 20 20 69 66 20 28 21 28  arint32.  if (!(
b5bb0 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20  a&0x80)).  {.   
b5bc0 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77 65   /* Values betwe
b5bd0 65 6e 20 30 20 61 6e 64 20 31 32 37 20 2a 2f 0a  en 0 and 127 */.
b5be0 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20      *v = a;.    
b5bf0 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65  return 1;.  }.#e
b5c00 6e 64 69 66 0a 0a 20 20 2f 2a 20 54 68 65 20 32  ndif..  /* The 2
b5c10 2d 62 79 74 65 20 63 61 73 65 20 2a 2f 0a 20 20  -byte case */.  
b5c20 70 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a 20  p++;.  b = *p;. 
b5c30 20 2f 2a 20 62 3a 20 70 31 20 28 75 6e 6d 61 73   /* b: p1 (unmas
b5c40 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28  ked) */.  if (!(
b5c50 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20  b&0x80)).  {.   
b5c60 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77 65   /* Values betwe
b5c70 65 6e 20 31 32 38 20 61 6e 64 20 31 36 33 38 33  en 128 and 16383
b5c80 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20 30 78 37   */.    a &= 0x7
b5c90 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b  f;.    a = a<<7;
b5ca0 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 62 3b  .    *v = a | b;
b5cb0 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20  .    return 2;. 
b5cc0 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 33 2d 62   }..  /* The 3-b
b5cd0 79 74 65 20 63 61 73 65 20 2a 2f 0a 20 20 70 2b  yte case */.  p+
b5ce0 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a  +;.  a = a<<14;.
b5cf0 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20    a |= *p;.  /* 
b5d00 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28  a: p0<<14 | p2 (
b5d10 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69  unmasked) */.  i
b5d20 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20  f (!(a&0x80)).  
b5d30 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20  {.    /* Values 
b5d40 62 65 74 77 65 65 6e 20 31 36 33 38 34 20 61 6e  between 16384 an
b5d50 64 20 32 30 39 37 31 35 31 20 2a 2f 0a 20 20 20  d 2097151 */.   
b5d60 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29   a &= (0x7f<<14)
b5d70 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20 26  |(0x7f);.    b &
b5d80 3d 20 30 78 37 66 3b 0a 20 20 20 20 62 20 3d 20  = 0x7f;.    b = 
b5d90 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61  b<<7;.    *v = a
b5da0 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e   | b;.    return
b5db0 20 33 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 20   3;.  }..  /* A 
b5dc0 33 32 2d 62 69 74 20 76 61 72 69 6e 74 20 69 73  32-bit varint is
b5dd0 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 73   used to store s
b5de0 69 7a 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ize information 
b5df0 69 6e 20 62 74 72 65 65 73 2e 0a 20 20 2a 2a 20  in btrees..  ** 
b5e00 4f 62 6a 65 63 74 73 20 61 72 65 20 72 61 72 65  Objects are rare
b5e10 6c 79 20 6c 61 72 67 65 72 20 74 68 61 6e 20 32  ly larger than 2
b5e20 4d 69 42 20 6c 69 6d 69 74 20 6f 66 20 61 20 33  MiB limit of a 3
b5e30 2d 62 79 74 65 20 76 61 72 69 6e 74 2e 0a 20 20  -byte varint..  
b5e40 2a 2a 20 41 20 33 2d 62 79 74 65 20 76 61 72 69  ** A 3-byte vari
b5e50 6e 74 20 69 73 20 73 75 66 66 69 63 69 65 6e 74  nt is sufficient
b5e60 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74  , for example, t
b5e70 6f 20 72 65 63 6f 72 64 20 74 68 65 20 73 69 7a  o record the siz
b5e80 65 0a 20 20 2a 2a 20 6f 66 20 61 20 31 30 34 38  e.  ** of a 1048
b5e90 35 36 39 2d 62 79 74 65 20 42 4c 4f 42 20 6f 72  569-byte BLOB or
b5ea0 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2a 0a 20 20   string..  **.  
b5eb0 2a 2a 20 57 65 20 6f 6e 6c 79 20 75 6e 72 6f 6c  ** We only unrol
b5ec0 6c 20 74 68 65 20 66 69 72 73 74 20 31 2d 2c 20  l the first 1-, 
b5ed0 32 2d 2c 20 61 6e 64 20 33 2d 20 62 79 74 65 20  2-, and 3- byte 
b5ee0 63 61 73 65 73 2e 20 20 54 68 65 20 76 65 72 79  cases.  The very
b5ef0 0a 20 20 2a 2a 20 72 61 72 65 20 6c 61 72 67 65  .  ** rare large
b5f00 72 20 63 61 73 65 73 20 63 61 6e 20 62 65 20 68  r cases can be h
b5f10 61 6e 64 6c 65 64 20 62 79 20 74 68 65 20 73 6c  andled by the sl
b5f20 6f 77 65 72 20 36 34 2d 62 69 74 20 76 61 72 69  ower 64-bit vari
b5f30 6e 74 0a 20 20 2a 2a 20 72 6f 75 74 69 6e 65 2e  nt.  ** routine.
b5f40 0a 20 20 2a 2f 0a 23 69 66 20 31 0a 20 20 7b 0a  .  */.#if 1.  {.
b5f50 20 20 20 20 75 36 34 20 76 36 34 3b 0a 20 20 20      u64 v64;.   
b5f60 20 75 38 20 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d   u8 n;..    p -=
b5f70 20 32 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69   2;.    n = sqli
b5f80 74 65 33 47 65 74 56 61 72 69 6e 74 28 70 2c 20  te3GetVarint(p, 
b5f90 26 76 36 34 29 3b 0a 20 20 20 20 61 73 73 65 72  &v64);.    asser
b5fa0 74 28 20 6e 3e 33 20 26 26 20 6e 3c 3d 39 20 29  t( n>3 && n<=9 )
b5fb0 3b 0a 20 20 20 20 69 66 28 20 28 76 36 34 20 26  ;.    if( (v64 &
b5fc0 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29   SQLITE_MAX_U32)
b5fd0 21 3d 76 36 34 20 29 7b 0a 20 20 20 20 20 20 2a  !=v64 ){.      *
b5fe0 76 20 3d 20 30 78 66 66 66 66 66 66 66 66 3b 0a  v = 0xffffffff;.
b5ff0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
b6000 20 2a 76 20 3d 20 28 75 33 32 29 76 36 34 3b 0a   *v = (u32)v64;.
b6010 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
b6020 20 6e 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 20   n;.  }..#else. 
b6030 20 2f 2a 20 46 6f 72 20 66 6f 6c 6c 6f 77 69 6e   /* For followin
b6040 67 20 63 6f 64 65 20 28 6b 65 70 74 20 66 6f 72  g code (kept for
b6050 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65 63 6f   historical reco
b6060 72 64 20 6f 6e 6c 79 29 20 73 68 6f 77 73 20 61  rd only) shows a
b6070 6e 0a 20 20 2a 2a 20 75 6e 72 6f 6c 6c 69 6e 67  n.  ** unrolling
b6080 20 66 6f 72 20 74 68 65 20 33 2d 20 61 6e 64 20   for the 3- and 
b6090 34 2d 62 79 74 65 20 76 61 72 69 6e 74 20 63 61  4-byte varint ca
b60a0 73 65 73 2e 20 20 54 68 69 73 20 63 6f 64 65 20  ses.  This code 
b60b0 69 73 0a 20 20 2a 2a 20 73 6c 69 67 68 74 6c 79  is.  ** slightly
b60c0 20 66 61 73 74 65 72 2c 20 62 75 74 20 69 74 20   faster, but it 
b60d0 69 73 20 61 6c 73 6f 20 6c 61 72 67 65 72 20 61  is also larger a
b60e0 6e 64 20 6d 75 63 68 20 68 61 72 64 65 72 20 74  nd much harder t
b60f0 6f 20 74 65 73 74 2e 0a 20 20 2a 2f 0a 20 20 70  o test..  */.  p
b6100 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b  ++;.  b = b<<14;
b6110 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a  .  b |= *p;.  /*
b6120 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 20   b: p1<<14 | p3 
b6130 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20  (unmasked) */.  
b6140 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20  if (!(b&0x80)). 
b6150 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73   {.    /* Values
b6160 20 62 65 74 77 65 65 6e 20 32 30 39 37 31 35 32   between 2097152
b6170 20 61 6e 64 20 32 36 38 34 33 35 34 35 35 20 2a   and 268435455 *
b6180 2f 0a 20 20 20 20 62 20 26 3d 20 28 30 78 37 66  /.    b &= (0x7f
b6190 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20  <<14)|(0x7f);.  
b61a0 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34    a &= (0x7f<<14
b61b0 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20  )|(0x7f);.    a 
b61c0 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d  = a<<7;.    *v =
b61d0 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75   a | b;.    retu
b61e0 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b  rn 4;.  }..  p++
b61f0 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20  ;.  a = a<<14;. 
b6200 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61   a |= *p;.  /* a
b6210 3a 20 70 30 3c 3c 32 38 20 7c 20 70 32 3c 3c 31  : p0<<28 | p2<<1
b6220 34 20 7c 20 70 34 20 28 75 6e 6d 61 73 6b 65 64  4 | p4 (unmasked
b6230 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30  ) */.  if (!(a&0
b6240 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a  x80)).  {.    /*
b6250 20 57 61 6c 75 65 73 20 20 62 65 74 77 65 65 6e   Walues  between
b6260 20 32 36 38 34 33 35 34 35 36 20 61 6e 64 20 33   268435456 and 3
b6270 34 33 35 39 37 33 38 33 36 37 20 2a 2f 0a 20 20  4359738367 */.  
b6280 20 20 61 20 26 3d 20 28 30 78 31 66 3c 3c 32 38    a &= (0x1f<<28
b6290 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  )|(0x7f<<14)|(0x
b62a0 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 28 30  7f);.    b &= (0
b62b0 78 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c  x1f<<28)|(0x7f<<
b62c0 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20  14)|(0x7f);.    
b62d0 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 2a 76  b = b<<7;.    *v
b62e0 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65   = a | b;.    re
b62f0 74 75 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f  turn 5;.  }..  /
b6300 2a 20 57 65 20 63 61 6e 20 6f 6e 6c 79 20 72 65  * We can only re
b6310 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 20 77  ach this point w
b6320 68 65 6e 20 72 65 61 64 69 6e 67 20 61 20 63 6f  hen reading a co
b6330 72 72 75 70 74 20 64 61 74 61 62 61 73 65 0a 20  rrupt database. 
b6340 20 2a 2a 20 66 69 6c 65 2e 20 20 49 6e 20 74 68   ** file.  In th
b6350 61 74 20 63 61 73 65 20 77 65 20 61 72 65 20 6e  at case we are n
b6360 6f 74 20 69 6e 20 61 6e 79 20 68 75 72 72 79 2e  ot in any hurry.
b6370 20 20 55 73 65 20 74 68 65 20 28 72 65 6c 61 74    Use the (relat
b6380 69 76 65 6c 79 0a 20 20 2a 2a 20 73 6c 6f 77 29  ively.  ** slow)
b6390 20 67 65 6e 65 72 61 6c 2d 70 75 72 70 6f 73 65   general-purpose
b63a0 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e   sqlite3GetVarin
b63b0 74 28 29 20 72 6f 75 74 69 6e 65 20 74 6f 20 65  t() routine to e
b63c0 78 74 72 61 63 74 20 74 68 65 0a 20 20 2a 2a 20  xtract the.  ** 
b63d0 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 7b 0a 20 20  value. */.  {.  
b63e0 20 20 75 36 34 20 76 36 34 3b 0a 20 20 20 20 75    u64 v64;.    u
b63f0 38 20 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d 20 34  8 n;..    p -= 4
b6400 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65  ;.    n = sqlite
b6410 33 47 65 74 56 61 72 69 6e 74 28 70 2c 20 26 76  3GetVarint(p, &v
b6420 36 34 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  64);.    assert(
b6430 20 6e 3e 35 20 26 26 20 6e 3c 3d 39 20 29 3b 0a   n>5 && n<=9 );.
b6440 20 20 20 20 2a 76 20 3d 20 28 75 33 32 29 76 36      *v = (u32)v6
b6450 34 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 3b  4;.    return n;
b6460 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  .  }.#endif.}../
b6470 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
b6480 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
b6490 74 68 61 74 20 77 69 6c 6c 20 62 65 20 6e 65 65  that will be nee
b64a0 64 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65  ded to store the
b64b0 20 67 69 76 65 6e 0a 2a 2a 20 36 34 2d 62 69 74   given.** 64-bit
b64c0 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 53 51 4c   integer..*/.SQL
b64d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b64e0 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e  sqlite3VarintLen
b64f0 28 75 36 34 20 76 29 7b 0a 20 20 69 6e 74 20 69  (u64 v){.  int i
b6500 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 20 20   = 0;.  do{.    
b6510 69 2b 2b 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 37  i++;.    v >>= 7
b6520 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76 21 3d 30  ;.  }while( v!=0
b6530 20 26 26 20 41 4c 57 41 59 53 28 69 3c 39 29 20   && ALWAYS(i<9) 
b6540 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d  );.  return i;.}
b6550 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 6f 72  .../*.** Read or
b6560 20 77 72 69 74 65 20 61 20 66 6f 75 72 2d 62 79   write a four-by
b6570 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e  te big-endian in
b6580 74 65 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2f 0a  teger value..*/.
b6590 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75  SQLITE_PRIVATE u
b65a0 33 32 20 73 71 6c 69 74 65 33 47 65 74 34 62 79  32 sqlite3Get4by
b65b0 74 65 28 63 6f 6e 73 74 20 75 38 20 2a 70 29 7b  te(const u8 *p){
b65c0 0a 20 20 72 65 74 75 72 6e 20 28 70 5b 30 5d 3c  .  return (p[0]<
b65d0 3c 32 34 29 20 7c 20 28 70 5b 31 5d 3c 3c 31 36  <24) | (p[1]<<16
b65e0 29 20 7c 20 28 70 5b 32 5d 3c 3c 38 29 20 7c 20  ) | (p[2]<<8) | 
b65f0 70 5b 33 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  p[3];.}.SQLITE_P
b6600 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
b6610 74 65 33 50 75 74 34 62 79 74 65 28 75 6e 73 69  te3Put4byte(unsi
b6620 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 33  gned char *p, u3
b6630 32 20 76 29 7b 0a 20 20 70 5b 30 5d 20 3d 20 28  2 v){.  p[0] = (
b6640 75 38 29 28 76 3e 3e 32 34 29 3b 0a 20 20 70 5b  u8)(v>>24);.  p[
b6650 31 5d 20 3d 20 28 75 38 29 28 76 3e 3e 31 36 29  1] = (u8)(v>>16)
b6660 3b 0a 20 20 70 5b 32 5d 20 3d 20 28 75 38 29 28  ;.  p[2] = (u8)(
b6670 76 3e 3e 38 29 3b 0a 20 20 70 5b 33 5d 20 3d 20  v>>8);.  p[3] = 
b6680 28 75 38 29 76 3b 0a 7d 0a 0a 0a 0a 23 69 66 20  (u8)v;.}....#if 
b6690 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
b66a0 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41  OMIT_BLOB_LITERA
b66b0 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  L) || defined(SQ
b66c0 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 29 0a  LITE_HAS_CODEC).
b66d0 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20  /*.** Translate 
b66e0 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 6f 66  a single byte of
b66f0 20 48 65 78 20 69 6e 74 6f 20 61 6e 20 69 6e 74   Hex into an int
b6700 65 67 65 72 2e 0a 2a 2a 20 54 68 69 73 20 72 6f  eger..** This ro
b6710 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73  utine only works
b6720 20 69 66 20 68 20 72 65 61 6c 6c 79 20 69 73 20   if h really is 
b6730 61 20 76 61 6c 69 64 20 68 65 78 61 64 65 63 69  a valid hexadeci
b6740 6d 61 6c 0a 2a 2a 20 63 68 61 72 61 63 74 65 72  mal.** character
b6750 3a 20 20 30 2e 2e 39 61 2e 2e 66 41 2e 2e 46 0a  :  0..9a..fA..F.
b6760 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 68 65 78  */.static u8 hex
b6770 54 6f 49 6e 74 28 69 6e 74 20 68 29 7b 0a 20 20  ToInt(int h){.  
b6780 61 73 73 65 72 74 28 20 28 68 3e 3d 27 30 27 20  assert( (h>='0' 
b6790 26 26 20 68 3c 3d 27 39 27 29 20 7c 7c 20 20 28  && h<='9') ||  (
b67a0 68 3e 3d 27 61 27 20 26 26 20 68 3c 3d 27 66 27  h>='a' && h<='f'
b67b0 29 20 7c 7c 20 20 28 68 3e 3d 27 41 27 20 26 26  ) ||  (h>='A' &&
b67c0 20 68 3c 3d 27 46 27 29 20 29 3b 0a 23 69 66 64   h<='F') );.#ifd
b67d0 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a  ef SQLITE_ASCII.
b67e0 20 20 68 20 2b 3d 20 39 2a 28 31 26 28 68 3e 3e    h += 9*(1&(h>>
b67f0 36 29 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64  6));.#endif.#ifd
b6800 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43  ef SQLITE_EBCDIC
b6810 0a 20 20 68 20 2b 3d 20 39 2a 28 31 26 7e 28 68  .  h += 9*(1&~(h
b6820 3e 3e 34 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20  >>4));.#endif.  
b6830 72 65 74 75 72 6e 20 28 75 38 29 28 68 20 26 20  return (u8)(h & 
b6840 30 78 66 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  0xf);.}.#endif /
b6850 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  * !SQLITE_OMIT_B
b6860 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c 7c 20 53  LOB_LITERAL || S
b6870 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 20  QLITE_HAS_CODEC 
b6880 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  */..#if !defined
b6890 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f  (SQLITE_OMIT_BLO
b68a0 42 5f 4c 49 54 45 52 41 4c 29 20 7c 7c 20 64 65  B_LITERAL) || de
b68b0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 53  fined(SQLITE_HAS
b68c0 5f 43 4f 44 45 43 29 0a 2f 2a 0a 2a 2a 20 43 6f  _CODEC)./*.** Co
b68d0 6e 76 65 72 74 20 61 20 42 4c 4f 42 20 6c 69 74  nvert a BLOB lit
b68e0 65 72 61 6c 20 6f 66 20 74 68 65 20 66 6f 72 6d  eral of the form
b68f0 20 22 78 27 68 68 68 68 68 68 27 22 20 69 6e 74   "x'hhhhhh'" int
b6900 6f 20 69 74 73 20 62 69 6e 61 72 79 0a 2a 2a 20  o its binary.** 
b6910 76 61 6c 75 65 2e 20 20 52 65 74 75 72 6e 20 61  value.  Return a
b6920 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73 20   pointer to its 
b6930 62 69 6e 61 72 79 20 76 61 6c 75 65 2e 20 20 53  binary value.  S
b6940 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65  pace to hold the
b6950 0a 2a 2a 20 62 69 6e 61 72 79 20 76 61 6c 75 65  .** binary value
b6960 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e   has been obtain
b6970 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 61  ed from malloc a
b6980 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65 65 64  nd must be freed
b6990 20 62 79 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69   by.** the calli
b69a0 6e 67 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53  ng routine..*/.S
b69b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
b69c0 69 64 20 2a 73 71 6c 69 74 65 33 48 65 78 54 6f  id *sqlite3HexTo
b69d0 42 6c 6f 62 28 73 71 6c 69 74 65 33 20 2a 64 62  Blob(sqlite3 *db
b69e0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c  , const char *z,
b69f0 20 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72 20   int n){.  char 
b6a00 2a 7a 42 6c 6f 62 3b 0a 20 20 69 6e 74 20 69 3b  *zBlob;.  int i;
b6a10 0a 0a 20 20 7a 42 6c 6f 62 20 3d 20 28 63 68 61  ..  zBlob = (cha
b6a20 72 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c  r *)sqlite3DbMal
b6a30 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2f 32 20 2b  locRaw(db, n/2 +
b6a40 20 31 29 3b 0a 20 20 6e 2d 2d 3b 0a 20 20 69 66   1);.  n--;.  if
b6a50 28 20 7a 42 6c 6f 62 20 29 7b 0a 20 20 20 20 66  ( zBlob ){.    f
b6a60 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 3d  or(i=0; i<n; i+=
b6a70 32 29 7b 0a 20 20 20 20 20 20 7a 42 6c 6f 62 5b  2){.      zBlob[
b6a80 69 2f 32 5d 20 3d 20 28 68 65 78 54 6f 49 6e 74  i/2] = (hexToInt
b6a90 28 7a 5b 69 5d 29 3c 3c 34 29 20 7c 20 68 65 78  (z[i])<<4) | hex
b6aa0 54 6f 49 6e 74 28 7a 5b 69 2b 31 5d 29 3b 0a 20  ToInt(z[i+1]);. 
b6ab0 20 20 20 7d 0a 20 20 20 20 7a 42 6c 6f 62 5b 69     }.    zBlob[i
b6ac0 2f 32 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72  /2] = 0;.  }.  r
b6ad0 65 74 75 72 6e 20 7a 42 6c 6f 62 3b 0a 7d 0a 23  eturn zBlob;.}.#
b6ae0 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45  endif /* !SQLITE
b6af0 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52  _OMIT_BLOB_LITER
b6b00 41 4c 20 7c 7c 20 53 51 4c 49 54 45 5f 48 41 53  AL || SQLITE_HAS
b6b10 5f 43 4f 44 45 43 20 2a 2f 0a 0a 0a 2f 2a 0a 2a  _CODEC */.../*.*
b6b20 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 71 6c  * Change the sql
b6b30 69 74 65 2e 6d 61 67 69 63 20 66 72 6f 6d 20 53  ite.magic from S
b6b40 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e  QLITE_MAGIC_OPEN
b6b50 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43   to SQLITE_MAGIC
b6b60 5f 42 55 53 59 2e 0a 2a 2a 20 52 65 74 75 72 6e  _BUSY..** Return
b6b70 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f 6e 2d 7a   an error (non-z
b6b80 65 72 6f 29 20 69 66 20 74 68 65 20 6d 61 67 69  ero) if the magi
b6b90 63 20 77 61 73 20 6e 6f 74 20 53 51 4c 49 54 45  c was not SQLITE
b6ba0 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20 77  _MAGIC_OPEN.** w
b6bb0 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
b6bc0 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a   is called..**.*
b6bd0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
b6be0 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 65 6e  s called when en
b6bf0 74 65 72 69 6e 67 20 61 6e 20 53 51 4c 69 74 65  tering an SQLite
b6c00 20 41 50 49 2e 20 20 54 68 65 20 53 51 4c 49 54   API.  The SQLIT
b6c10 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20  E_MAGIC_OPEN.** 
b6c20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20  value indicates 
b6c30 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73  that the databas
b6c40 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 61 73  e connection pas
b6c50 73 65 64 20 69 6e 74 6f 20 74 68 65 20 41 50 49  sed into the API
b6c60 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 61 6e 64 20   is.** open and 
b6c70 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 75 73 65  is not being use
b6c80 64 20 62 79 20 61 6e 6f 74 68 65 72 20 74 68 72  d by another thr
b6c90 65 61 64 2e 20 20 42 79 20 63 68 61 6e 67 69 6e  ead.  By changin
b6ca0 67 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 74  g the value.** t
b6cb0 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42  o SQLITE_MAGIC_B
b6cc0 55 53 59 20 77 65 20 69 6e 64 69 63 61 74 65 20  USY we indicate 
b6cd0 74 68 61 74 20 74 68 65 20 63 6f 6e 6e 65 63 74  that the connect
b6ce0 69 6f 6e 20 69 73 20 69 6e 20 75 73 65 2e 0a 2a  ion is in use..*
b6cf0 2a 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  * sqlite3SafetyO
b6d00 66 66 28 29 20 62 65 6c 6f 77 20 77 69 6c 6c 20  ff() below will 
b6d10 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65  change the value
b6d20 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 45 5f   back to SQLITE_
b6d30 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20 77 68  MAGIC_OPEN.** wh
b6d40 65 6e 20 74 68 65 20 41 50 49 20 65 78 69 74 73  en the API exits
b6d50 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  . .**.** This ro
b6d60 75 74 69 6e 65 20 69 73 20 61 20 61 74 74 65 6d  utine is a attem
b6d70 70 74 20 74 6f 20 64 65 74 65 63 74 20 69 66 20  pt to detect if 
b6d80 74 77 6f 20 74 68 72 65 61 64 73 20 75 73 65 20  two threads use 
b6d90 74 68 65 0a 2a 2a 20 73 61 6d 65 20 73 71 6c 69  the.** same sqli
b6da0 74 65 2a 20 70 6f 69 6e 74 65 72 20 61 74 20 74  te* pointer at t
b6db0 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 54  he same time.  T
b6dc0 68 65 72 65 20 69 73 20 61 20 72 61 63 65 20 0a  here is a race .
b6dd0 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 20 73 6f 20  ** condition so 
b6de0 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74  it is possible t
b6df0 68 61 74 20 74 68 65 20 65 72 72 6f 72 20 69 73  hat the error is
b6e00 20 6e 6f 74 20 64 65 74 65 63 74 65 64 2e 0a 2a   not detected..*
b6e10 2a 20 42 75 74 20 75 73 75 61 6c 6c 79 20 74 68  * But usually th
b6e20 65 20 70 72 6f 62 6c 65 6d 20 77 69 6c 6c 20 62  e problem will b
b6e30 65 20 73 65 65 6e 2e 20 20 54 68 65 20 72 65 73  e seen.  The res
b6e40 75 6c 74 20 77 69 6c 6c 20 62 65 20 61 6e 0a 2a  ult will be an.*
b6e50 2a 20 65 72 72 6f 72 20 77 68 69 63 68 20 63 61  * error which ca
b6e60 6e 20 62 65 20 75 73 65 64 20 74 6f 20 64 65 62  n be used to deb
b6e70 75 67 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  ug the applicati
b6e80 6f 6e 20 74 68 61 74 20 69 73 0a 2a 2a 20 75 73  on that is.** us
b6e90 69 6e 67 20 53 51 4c 69 74 65 20 69 6e 63 6f 72  ing SQLite incor
b6ea0 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 69  rectly..**.** Ti
b6eb0 63 6b 65 74 20 23 32 30 32 3a 20 20 49 66 20 64  cket #202:  If d
b6ec0 62 2d 3e 6d 61 67 69 63 20 69 73 20 6e 6f 74 20  b->magic is not 
b6ed0 61 20 76 61 6c 69 64 20 6f 70 65 6e 20 76 61 6c  a valid open val
b6ee0 75 65 2c 20 74 61 6b 65 20 63 61 72 65 20 6e 6f  ue, take care no
b6ef0 74 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74  t.** to modify t
b6f00 68 65 20 64 62 20 73 74 72 75 63 74 75 72 65 20  he db structure 
b6f10 61 74 20 61 6c 6c 2e 20 20 49 74 20 63 6f 75 6c  at all.  It coul
b6f20 64 20 62 65 20 74 68 61 74 20 64 62 20 69 73 20  d be that db is 
b6f30 61 20 73 74 61 6c 65 0a 2a 2a 20 70 6f 69 6e 74  a stale.** point
b6f40 65 72 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f  er.  In other wo
b6f50 72 64 73 2c 20 69 74 20 63 6f 75 6c 64 20 62 65  rds, it could be
b6f60 20 74 68 61 74 20 74 68 65 72 65 20 68 61 73 20   that there has 
b6f70 62 65 65 6e 20 61 20 70 72 69 6f 72 0a 2a 2a 20  been a prior.** 
b6f80 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
b6f90 63 6c 6f 73 65 28 64 62 29 20 61 6e 64 20 64 62  close(db) and db
b6fa0 20 68 61 73 20 62 65 65 6e 20 64 65 61 6c 6c 6f   has been deallo
b6fb0 63 61 74 65 64 2e 20 20 41 6e 64 20 77 65 20 64  cated.  And we d
b6fc0 6f 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f  o.** not want to
b6fd0 20 77 72 69 74 65 20 69 6e 74 6f 20 64 65 61 6c   write into deal
b6fe0 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 0a  located memory..
b6ff0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
b7000 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52  _DEBUG.SQLITE_PR
b7010 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b7020 33 53 61 66 65 74 79 4f 6e 28 73 71 6c 69 74 65  3SafetyOn(sqlite
b7030 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 62  3 *db){.  if( db
b7040 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f  ->magic==SQLITE_
b7050 4d 41 47 49 43 5f 4f 50 45 4e 20 29 7b 0a 20 20  MAGIC_OPEN ){.  
b7060 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51    db->magic = SQ
b7070 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 3b  LITE_MAGIC_BUSY;
b7080 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
b7090 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
b70a0 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
b70b0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65    return 0;.  }e
b70c0 6c 73 65 20 69 66 28 20 64 62 2d 3e 6d 61 67 69  lse if( db->magi
b70d0 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  c==SQLITE_MAGIC_
b70e0 42 55 53 59 20 29 7b 0a 20 20 20 20 64 62 2d 3e  BUSY ){.    db->
b70f0 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d  magic = SQLITE_M
b7100 41 47 49 43 5f 45 52 52 4f 52 3b 0a 20 20 20 20  AGIC_ERROR;.    
b7110 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75  db->u1.isInterru
b7120 70 74 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  pted = 1;.  }.  
b7130 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64  return 1;.}.#end
b7140 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  if../*.** Change
b7150 20 74 68 65 20 6d 61 67 69 63 20 66 72 6f 6d 20   the magic from 
b7160 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53  SQLITE_MAGIC_BUS
b7170 59 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49  Y to SQLITE_MAGI
b7180 43 5f 4f 50 45 4e 2e 0a 2a 2a 20 52 65 74 75 72  C_OPEN..** Retur
b7190 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f 6e 2d  n an error (non-
b71a0 7a 65 72 6f 29 20 69 66 20 74 68 65 20 6d 61 67  zero) if the mag
b71b0 69 63 20 77 61 73 20 6e 6f 74 20 53 51 4c 49 54  ic was not SQLIT
b71c0 45 5f 4d 41 47 49 43 5f 42 55 53 59 0a 2a 2a 20  E_MAGIC_BUSY.** 
b71d0 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  when this routin
b71e0 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a  e is called..*/.
b71f0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
b7200 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
b7210 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61  TE int sqlite3Sa
b7220 66 65 74 79 4f 66 66 28 73 71 6c 69 74 65 33 20  fetyOff(sqlite3 
b7230 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 62 2d 3e  *db){.  if( db->
b7240 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41  magic==SQLITE_MA
b7250 47 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 20 20  GIC_BUSY ){.    
b7260 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49  db->magic = SQLI
b7270 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 3b 0a 20  TE_MAGIC_OPEN;. 
b7280 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
b7290 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
b72a0 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20  ->mutex) );.    
b72b0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73  return 0;.  }els
b72c0 65 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63  e{.    db->magic
b72d0 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f   = SQLITE_MAGIC_
b72e0 45 52 52 4f 52 3b 0a 20 20 20 20 64 62 2d 3e 75  ERROR;.    db->u
b72f0 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20  1.isInterrupted 
b7300 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 1;.    return 
b7310 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a  1;.  }.}.#endif.
b7320 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
b7330 6d 61 6b 65 20 73 75 72 65 20 77 65 20 68 61 76  make sure we hav
b7340 65 20 61 20 76 61 6c 69 64 20 64 62 20 70 6f 69  e a valid db poi
b7350 6e 74 65 72 2e 20 20 54 68 69 73 20 74 65 73 74  nter.  This test
b7360 20 69 73 20 6e 6f 74 0a 2a 2a 20 66 6f 6f 6c 70   is not.** foolp
b7370 72 6f 6f 66 20 62 75 74 20 69 74 20 64 6f 65 73  roof but it does
b7380 20 70 72 6f 76 69 64 65 20 73 6f 6d 65 20 6d 65   provide some me
b7390 61 73 75 72 65 20 6f 66 20 70 72 6f 74 65 63 74  asure of protect
b73a0 69 6f 6e 20 61 67 61 69 6e 73 74 0a 2a 2a 20 6d  ion against.** m
b73b0 69 73 75 73 65 20 6f 66 20 74 68 65 20 69 6e 74  isuse of the int
b73c0 65 72 66 61 63 65 20 73 75 63 68 20 61 73 20 70  erface such as p
b73d0 61 73 73 69 6e 67 20 69 6e 20 64 62 20 70 6f 69  assing in db poi
b73e0 6e 74 65 72 73 20 74 68 61 74 20 61 72 65 0a 2a  nters that are.*
b73f0 2a 20 4e 55 4c 4c 20 6f 72 20 77 68 69 63 68 20  * NULL or which 
b7400 68 61 76 65 20 62 65 65 6e 20 70 72 65 76 69 6f  have been previo
b7410 75 73 6c 79 20 63 6c 6f 73 65 64 2e 20 20 49 66  usly closed.  If
b7420 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
b7430 74 75 72 6e 73 0a 2a 2a 20 31 20 69 74 20 6d 65  turns.** 1 it me
b7440 61 6e 73 20 74 68 61 74 20 74 68 65 20 64 62 20  ans that the db 
b7450 70 6f 69 6e 74 65 72 20 69 73 20 76 61 6c 69 64  pointer is valid
b7460 20 61 6e 64 20 30 20 69 66 20 69 74 20 73 68 6f   and 0 if it sho
b7470 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 64 65  uld not be.** de
b7480 72 65 66 65 72 65 6e 63 65 64 20 66 6f 72 20 61  referenced for a
b7490 6e 79 20 72 65 61 73 6f 6e 2e 20 20 54 68 65 20  ny reason.  The 
b74a0 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
b74b0 20 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 0a 2a   should invoke.*
b74c0 2a 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20  * SQLITE_MISUSE 
b74d0 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a  immediately..**.
b74e0 2a 2a 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  ** sqlite3Safety
b74f0 43 68 65 63 6b 4f 6b 28 29 20 72 65 71 75 69 72  CheckOk() requir
b7500 65 73 20 74 68 61 74 20 74 68 65 20 64 62 20 70  es that the db p
b7510 6f 69 6e 74 65 72 20 62 65 20 76 61 6c 69 64 20  ointer be valid 
b7520 66 6f 72 0a 2a 2a 20 75 73 65 2e 20 20 73 71 6c  for.** use.  sql
b7530 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 53  ite3SafetyCheckS
b7540 69 63 6b 4f 72 4f 6b 28 29 20 61 6c 6c 6f 77 73  ickOrOk() allows
b7550 20 61 20 64 62 20 70 6f 69 6e 74 65 72 20 74 68   a db pointer th
b7560 61 74 20 66 61 69 6c 65 64 20 74 6f 0a 2a 2a 20  at failed to.** 
b7570 6f 70 65 6e 20 70 72 6f 70 65 72 6c 79 20 61 6e  open properly an
b7580 64 20 69 73 20 6e 6f 74 20 66 69 74 20 66 6f 72  d is not fit for
b7590 20 67 65 6e 65 72 61 6c 20 75 73 65 20 62 75 74   general use but
b75a0 20 77 68 69 63 68 20 63 61 6e 20 62 65 0a 2a 2a   which can be.**
b75b0 20 75 73 65 64 20 61 73 20 61 6e 20 61 72 67 75   used as an argu
b75c0 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f  ment to sqlite3_
b75d0 65 72 72 6d 73 67 28 29 20 6f 72 20 73 71 6c 69  errmsg() or sqli
b75e0 74 65 33 5f 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a  te3_close()..*/.
b75f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
b7600 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  nt sqlite3Safety
b7610 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33 20  CheckOk(sqlite3 
b7620 2a 64 62 29 7b 0a 20 20 75 33 32 20 6d 61 67 69  *db){.  u32 magi
b7630 63 3b 0a 20 20 69 66 28 20 64 62 3d 3d 30 20 29  c;.  if( db==0 )
b7640 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6d 61 67   return 0;.  mag
b7650 69 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63 3b 0a  ic = db->magic;.
b7660 20 20 69 66 28 20 6d 61 67 69 63 21 3d 53 51 4c    if( magic!=SQL
b7670 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 0a  ITE_MAGIC_OPEN .
b7680 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
b7690 42 55 47 0a 20 20 20 20 20 26 26 20 6d 61 67 69  BUG.     && magi
b76a0 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  c!=SQLITE_MAGIC_
b76b0 42 55 53 59 0a 23 65 6e 64 69 66 0a 20 20 29 7b  BUSY.#endif.  ){
b76c0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
b76d0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
b76e0 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49  rn 1;.  }.}.SQLI
b76f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
b7700 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63  qlite3SafetyChec
b7710 6b 53 69 63 6b 4f 72 4f 6b 28 73 71 6c 69 74 65  kSickOrOk(sqlite
b7720 33 20 2a 64 62 29 7b 0a 20 20 75 33 32 20 6d 61  3 *db){.  u32 ma
b7730 67 69 63 3b 0a 20 20 6d 61 67 69 63 20 3d 20 64  gic;.  magic = d
b7740 62 2d 3e 6d 61 67 69 63 3b 0a 20 20 69 66 28 20  b->magic;.  if( 
b7750 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41  magic!=SQLITE_MA
b7760 47 49 43 5f 53 49 43 4b 20 26 26 0a 20 20 20 20  GIC_SICK &&.    
b7770 20 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f    magic!=SQLITE_
b7780 4d 41 47 49 43 5f 4f 50 45 4e 20 26 26 0a 20 20  MAGIC_OPEN &&.  
b7790 20 20 20 20 6d 61 67 69 63 21 3d 53 51 4c 49 54      magic!=SQLIT
b77a0 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 20 72  E_MAGIC_BUSY ) r
b77b0 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 75 72  eturn 0;.  retur
b77c0 6e 20 31 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  n 1;.}../*******
b77d0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 75  ******* End of u
b77e0 74 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  til.c **********
b77f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7810 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
b7820 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
b7830 6c 65 20 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a  le hash.c ******
b7840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7860 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
b7870 30 31 20 53 65 70 74 65 6d 62 65 72 20 32 32 0a  01 September 22.
b7880 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
b7890 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
b78a0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
b78b0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
b78c0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
b78d0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
b78e0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
b78f0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
b7900 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
b7910 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
b7920 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
b7930 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
b7940 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
b7950 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
b7960 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
b7970 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
b7980 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
b7990 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
b79a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b79b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b79c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b79d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
b79e0 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6d  * This is the im
b79f0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
b7a00 67 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62  generic hash-tab
b7a10 6c 65 73 0a 2a 2a 20 75 73 65 64 20 69 6e 20 53  les.** used in S
b7a20 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  QLite..**.** $Id
b7a30 3a 20 68 61 73 68 2e 63 2c 76 20 31 2e 33 38 20  : hash.c,v 1.38 
b7a40 32 30 30 39 2f 30 35 2f 30 39 20 32 33 3a 32 39  2009/05/09 23:29
b7a50 3a 31 32 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :12 drh Exp $.*/
b7a60 0a 0a 2f 2a 20 54 75 72 6e 20 62 75 6c 6b 20 6d  ../* Turn bulk m
b7a70 65 6d 6f 72 79 20 69 6e 74 6f 20 61 20 68 61 73  emory into a has
b7a80 68 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 62  h table object b
b7a90 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74  y initializing t
b7aa0 68 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6f 66 20  he.** fields of 
b7ab0 74 68 65 20 48 61 73 68 20 73 74 72 75 63 74 75  the Hash structu
b7ac0 72 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e 65 77 22  re..**.** "pNew"
b7ad0 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
b7ae0 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
b7af0 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e  that is to be in
b7b00 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 51  itialized..*/.SQ
b7b10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
b7b20 64 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69  d sqlite3HashIni
b7b30 74 28 48 61 73 68 20 2a 70 4e 65 77 29 7b 0a 20  t(Hash *pNew){. 
b7b40 20 61 73 73 65 72 74 28 20 70 4e 65 77 21 3d 30   assert( pNew!=0
b7b50 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 72 73   );.  pNew->firs
b7b60 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 63  t = 0;.  pNew->c
b7b70 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77  ount = 0;.  pNew
b7b80 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20  ->htsize = 0;.  
b7b90 70 4e 65 77 2d 3e 68 74 20 3d 20 30 3b 0a 7d 0a  pNew->ht = 0;.}.
b7ba0 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 65  ./* Remove all e
b7bb0 6e 74 72 69 65 73 20 66 72 6f 6d 20 61 20 68 61  ntries from a ha
b7bc0 73 68 20 74 61 62 6c 65 2e 20 20 52 65 63 6c 61  sh table.  Recla
b7bd0 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e 0a 2a  im all memory..*
b7be0 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74  * Call this rout
b7bf0 69 6e 65 20 74 6f 20 64 65 6c 65 74 65 20 61 20  ine to delete a 
b7c00 68 61 73 68 20 74 61 62 6c 65 20 6f 72 20 74 6f  hash table or to
b7c10 20 72 65 73 65 74 20 61 20 68 61 73 68 20 74 61   reset a hash ta
b7c20 62 6c 65 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6d  ble.** to the em
b7c30 70 74 79 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51  pty state..*/.SQ
b7c40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
b7c50 64 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65  d sqlite3HashCle
b7c60 61 72 28 48 61 73 68 20 2a 70 48 29 7b 0a 20 20  ar(Hash *pH){.  
b7c70 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20  HashElem *elem; 
b7c80 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c          /* For l
b7c90 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20  ooping over all 
b7ca0 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20  elements of the 
b7cb0 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65  table */..  asse
b7cc0 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 65  rt( pH!=0 );.  e
b7cd0 6c 65 6d 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b  lem = pH->first;
b7ce0 0a 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 30  .  pH->first = 0
b7cf0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
b7d00 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e  (pH->ht);.  pH->
b7d10 68 74 20 3d 20 30 3b 0a 20 20 70 48 2d 3e 68 74  ht = 0;.  pH->ht
b7d20 73 69 7a 65 20 3d 20 30 3b 0a 20 20 77 68 69 6c  size = 0;.  whil
b7d30 65 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 48  e( elem ){.    H
b7d40 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c  ashElem *next_el
b7d50 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b  em = elem->next;
b7d60 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
b7d70 65 28 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c 65  e(elem);.    ele
b7d80 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a 20  m = next_elem;. 
b7d90 20 7d 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d   }.  pH->count =
b7da0 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65   0;.}../*.** The
b7db0 20 68 61 73 68 69 6e 67 20 66 75 6e 63 74 69 6f   hashing functio
b7dc0 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 6e 73  n..*/.static uns
b7dd0 69 67 6e 65 64 20 69 6e 74 20 73 74 72 48 61 73  igned int strHas
b7de0 68 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c  h(const char *z,
b7df0 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 69 6e   int nKey){.  in
b7e00 74 20 68 20 3d 20 30 3b 0a 20 20 61 73 73 65 72  t h = 0;.  asser
b7e10 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20  t( nKey>=0 );.  
b7e20 77 68 69 6c 65 28 20 6e 4b 65 79 20 3e 20 30 20  while( nKey > 0 
b7e30 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c   ){.    h = (h<<
b7e40 33 29 20 5e 20 68 20 5e 20 73 71 6c 69 74 65 33  3) ^ h ^ sqlite3
b7e50 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e  UpperToLower[(un
b7e60 73 69 67 6e 65 64 20 63 68 61 72 29 2a 7a 2b 2b  signed char)*z++
b7e70 5d 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b 0a 20  ];.    nKey--;. 
b7e80 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 3b 0a 7d   }.  return h;.}
b7e90 0a 0a 0a 2f 2a 20 4c 69 6e 6b 20 70 4e 65 77 20  .../* Link pNew 
b7ea0 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65  element into the
b7eb0 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e 20   hash table pH. 
b7ec0 20 49 66 20 70 45 6e 74 72 79 21 3d 30 20 74 68   If pEntry!=0 th
b7ed0 65 6e 20 61 6c 73 6f 0a 2a 2a 20 69 6e 73 65 72  en also.** inser
b7ee0 74 20 70 4e 65 77 20 69 6e 74 6f 20 74 68 65 20  t pNew into the 
b7ef0 70 45 6e 74 72 79 20 68 61 73 68 20 62 75 63 6b  pEntry hash buck
b7f00 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  et..*/.static vo
b7f10 69 64 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74  id insertElement
b7f20 28 0a 20 20 48 61 73 68 20 2a 70 48 2c 20 20 20  (.  Hash *pH,   
b7f30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
b7f40 65 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20  e complete hash 
b7f50 74 61 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63  table */.  struc
b7f60 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 2c 20 20  t _ht *pEntry,  
b7f70 20 20 2f 2a 20 54 68 65 20 65 6e 74 72 79 20 69    /* The entry i
b7f80 6e 74 6f 20 77 68 69 63 68 20 70 4e 65 77 20 69  nto which pNew i
b7f90 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20  s inserted */.  
b7fa0 48 61 73 68 45 6c 65 6d 20 2a 70 4e 65 77 20 20  HashElem *pNew  
b7fb0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 6c         /* The el
b7fc0 65 6d 65 6e 74 20 74 6f 20 62 65 20 69 6e 73 65  ement to be inse
b7fd0 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 48 61 73  rted */.){.  Has
b7fe0 68 45 6c 65 6d 20 2a 70 48 65 61 64 3b 20 20 20  hElem *pHead;   
b7ff0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 65 6c 65      /* First ele
b8000 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 69 6e 20  ment already in 
b8010 70 45 6e 74 72 79 20 2a 2f 0a 20 20 69 66 28 20  pEntry */.  if( 
b8020 70 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 70 48  pEntry ){.    pH
b8030 65 61 64 20 3d 20 70 45 6e 74 72 79 2d 3e 63 6f  ead = pEntry->co
b8040 75 6e 74 20 3f 20 70 45 6e 74 72 79 2d 3e 63 68  unt ? pEntry->ch
b8050 61 69 6e 20 3a 20 30 3b 0a 20 20 20 20 70 45 6e  ain : 0;.    pEn
b8060 74 72 79 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20  try->count++;.  
b8070 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20    pEntry->chain 
b8080 3d 20 70 4e 65 77 3b 0a 20 20 7d 65 6c 73 65 7b  = pNew;.  }else{
b8090 0a 20 20 20 20 70 48 65 61 64 20 3d 20 30 3b 0a  .    pHead = 0;.
b80a0 20 20 7d 0a 20 20 69 66 28 20 70 48 65 61 64 20    }.  if( pHead 
b80b0 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78  ){.    pNew->nex
b80c0 74 20 3d 20 70 48 65 61 64 3b 0a 20 20 20 20 70  t = pHead;.    p
b80d0 4e 65 77 2d 3e 70 72 65 76 20 3d 20 70 48 65 61  New->prev = pHea
b80e0 64 2d 3e 70 72 65 76 3b 0a 20 20 20 20 69 66 28  d->prev;.    if(
b80f0 20 70 48 65 61 64 2d 3e 70 72 65 76 20 29 7b 20   pHead->prev ){ 
b8100 70 48 65 61 64 2d 3e 70 72 65 76 2d 3e 6e 65 78  pHead->prev->nex
b8110 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20  t = pNew; }.    
b8120 65 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20  else            
b8130 20 7b 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 70   { pH->first = p
b8140 4e 65 77 3b 20 7d 0a 20 20 20 20 70 48 65 61 64  New; }.    pHead
b8150 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 0a 20  ->prev = pNew;. 
b8160 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77   }else{.    pNew
b8170 2d 3e 6e 65 78 74 20 3d 20 70 48 2d 3e 66 69 72  ->next = pH->fir
b8180 73 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d 3e  st;.    if( pH->
b8190 66 69 72 73 74 20 29 7b 20 70 48 2d 3e 66 69 72  first ){ pH->fir
b81a0 73 74 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b  st->prev = pNew;
b81b0 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72 65   }.    pNew->pre
b81c0 76 20 3d 20 30 3b 0a 20 20 20 20 70 48 2d 3e 66  v = 0;.    pH->f
b81d0 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d  irst = pNew;.  }
b81e0 0a 7d 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 20 74  .}.../* Resize t
b81f0 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 73 6f  he hash table so
b8200 20 74 68 61 74 20 69 74 20 63 61 6e 74 61 69 6e   that it cantain
b8210 73 20 22 6e 65 77 5f 73 69 7a 65 22 20 62 75 63  s "new_size" buc
b8220 6b 65 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  kets..**.** The 
b8230 68 61 73 68 20 74 61 62 6c 65 20 6d 69 67 68 74  hash table might
b8240 20 66 61 69 6c 20 74 6f 20 72 65 73 69 7a 65 20   fail to resize 
b8250 69 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  if sqlite3_mallo
b8260 63 28 29 20 66 61 69 6c 73 20 6f 72 0a 2a 2a 20  c() fails or.** 
b8270 69 66 20 74 68 65 20 6e 65 77 20 73 69 7a 65 20  if the new size 
b8280 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  is the same as t
b8290 68 65 20 70 72 69 6f 72 20 73 69 7a 65 2e 0a 2a  he prior size..*
b82a0 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66  * Return TRUE if
b82b0 20 74 68 65 20 72 65 73 69 7a 65 20 6f 63 63 75   the resize occu
b82c0 72 73 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20  rs and false if 
b82d0 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  not..*/.static i
b82e0 6e 74 20 72 65 68 61 73 68 28 48 61 73 68 20 2a  nt rehash(Hash *
b82f0 70 48 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  pH, unsigned int
b8300 20 6e 65 77 5f 73 69 7a 65 29 7b 0a 20 20 73 74   new_size){.  st
b8310 72 75 63 74 20 5f 68 74 20 2a 6e 65 77 5f 68 74  ruct _ht *new_ht
b8320 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
b8330 54 68 65 20 6e 65 77 20 68 61 73 68 20 74 61 62  The new hash tab
b8340 6c 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d  le */.  HashElem
b8350 20 2a 65 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c   *elem, *next_el
b8360 65 6d 3b 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f  em;    /* For lo
b8370 6f 70 69 6e 67 20 6f 76 65 72 20 65 78 69 73 74  oping over exist
b8380 69 6e 67 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a  ing elements */.
b8390 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c  .#if SQLITE_MALL
b83a0 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 3e 30 0a  OC_SOFT_LIMIT>0.
b83b0 20 20 69 66 28 20 6e 65 77 5f 73 69 7a 65 2a 73    if( new_size*s
b83c0 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74  izeof(struct _ht
b83d0 29 3e 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f  )>SQLITE_MALLOC_
b83e0 53 4f 46 54 5f 4c 49 4d 49 54 20 29 7b 0a 20 20  SOFT_LIMIT ){.  
b83f0 20 20 6e 65 77 5f 73 69 7a 65 20 3d 20 53 51 4c    new_size = SQL
b8400 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f  ITE_MALLOC_SOFT_
b8410 4c 49 4d 49 54 2f 73 69 7a 65 6f 66 28 73 74 72  LIMIT/sizeof(str
b8420 75 63 74 20 5f 68 74 29 3b 0a 20 20 7d 0a 20 20  uct _ht);.  }.  
b8430 69 66 28 20 6e 65 77 5f 73 69 7a 65 3d 3d 70 48  if( new_size==pH
b8440 2d 3e 68 74 73 69 7a 65 20 29 20 72 65 74 75 72  ->htsize ) retur
b8450 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  n 0;.#endif..  /
b8460 2a 20 54 68 65 20 69 6e 61 62 69 6c 69 74 79 20  * The inability 
b8470 74 6f 20 61 6c 6c 6f 63 61 74 65 73 20 73 70 61  to allocates spa
b8480 63 65 20 66 6f 72 20 61 20 6c 61 72 67 65 72 20  ce for a larger 
b8490 68 61 73 68 20 74 61 62 6c 65 20 69 73 0a 20 20  hash table is.  
b84a0 2a 2a 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 65  ** a performance
b84b0 20 68 69 74 20 62 75 74 20 69 74 20 69 73 20 6e   hit but it is n
b84c0 6f 74 20 61 20 66 61 74 61 6c 20 65 72 72 6f 72  ot a fatal error
b84d0 2e 20 20 53 6f 20 6d 61 72 6b 20 74 68 65 0a 20  .  So mark the. 
b84e0 20 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61   ** allocation a
b84f0 73 20 61 20 62 65 6e 69 67 6e 2e 0a 20 20 2a 2f  s a benign..  */
b8500 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42  .  sqlite3BeginB
b8510 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20  enignMalloc();. 
b8520 20 6e 65 77 5f 68 74 20 3d 20 28 73 74 72 75 63   new_ht = (struc
b8530 74 20 5f 68 74 20 2a 29 73 71 6c 69 74 65 33 4d  t _ht *)sqlite3M
b8540 61 6c 6c 6f 63 28 20 6e 65 77 5f 73 69 7a 65 2a  alloc( new_size*
b8550 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68  sizeof(struct _h
b8560 74 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  t) );.  sqlite3E
b8570 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  ndBenignMalloc()
b8580 3b 0a 0a 20 20 69 66 28 20 6e 65 77 5f 68 74 3d  ;..  if( new_ht=
b8590 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
b85a0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48   sqlite3_free(pH
b85b0 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20  ->ht);.  pH->ht 
b85c0 3d 20 6e 65 77 5f 68 74 3b 0a 20 20 70 48 2d 3e  = new_ht;.  pH->
b85d0 68 74 73 69 7a 65 20 3d 20 6e 65 77 5f 73 69 7a  htsize = new_siz
b85e0 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  e = sqlite3Mallo
b85f0 63 53 69 7a 65 28 6e 65 77 5f 68 74 29 2f 73 69  cSize(new_ht)/si
b8600 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29  zeof(struct _ht)
b8610 3b 0a 20 20 6d 65 6d 73 65 74 28 6e 65 77 5f 68  ;.  memset(new_h
b8620 74 2c 20 30 2c 20 6e 65 77 5f 73 69 7a 65 2a 73  t, 0, new_size*s
b8630 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74  izeof(struct _ht
b8640 29 29 3b 0a 20 20 66 6f 72 28 65 6c 65 6d 3d 70  ));.  for(elem=p
b8650 48 2d 3e 66 69 72 73 74 2c 20 70 48 2d 3e 66 69  H->first, pH->fi
b8660 72 73 74 3d 30 3b 20 65 6c 65 6d 3b 20 65 6c 65  rst=0; elem; ele
b8670 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 29 7b 0a  m = next_elem){.
b8680 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74      unsigned int
b8690 20 68 20 3d 20 73 74 72 48 61 73 68 28 65 6c 65   h = strHash(ele
b86a0 6d 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d 3e 6e  m->pKey, elem->n
b86b0 4b 65 79 29 20 25 20 6e 65 77 5f 73 69 7a 65 3b  Key) % new_size;
b86c0 0a 20 20 20 20 6e 65 78 74 5f 65 6c 65 6d 20 3d  .    next_elem =
b86d0 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20   elem->next;.   
b86e0 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70   insertElement(p
b86f0 48 2c 20 26 6e 65 77 5f 68 74 5b 68 5d 2c 20 65  H, &new_ht[h], e
b8700 6c 65 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  lem);.  }.  retu
b8710 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73  rn 1;.}../* This
b8720 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20 69   function (for i
b8730 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79  nternal use only
b8740 29 20 6c 6f 63 61 74 65 73 20 61 6e 20 65 6c 65  ) locates an ele
b8750 6d 65 6e 74 20 69 6e 20 61 6e 0a 2a 2a 20 68 61  ment in an.** ha
b8760 73 68 20 74 61 62 6c 65 20 74 68 61 74 20 6d 61  sh table that ma
b8770 74 63 68 65 73 20 74 68 65 20 67 69 76 65 6e 20  tches the given 
b8780 6b 65 79 2e 20 20 54 68 65 20 68 61 73 68 20 66  key.  The hash f
b8790 6f 72 20 74 68 69 73 20 6b 65 79 20 68 61 73 0a  or this key has.
b87a0 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ** already been 
b87b0 63 6f 6d 70 75 74 65 64 20 61 6e 64 20 69 73 20  computed and is 
b87c0 70 61 73 73 65 64 20 61 73 20 74 68 65 20 34 74  passed as the 4t
b87d0 68 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a  h parameter..*/.
b87e0 73 74 61 74 69 63 20 48 61 73 68 45 6c 65 6d 20  static HashElem 
b87f0 2a 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65  *findElementGive
b8800 6e 48 61 73 68 28 0a 20 20 63 6f 6e 73 74 20 48  nHash(.  const H
b8810 61 73 68 20 2a 70 48 2c 20 20 20 20 20 2f 2a 20  ash *pH,     /* 
b8820 54 68 65 20 70 48 20 74 6f 20 62 65 20 73 65 61  The pH to be sea
b8830 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  rched */.  const
b8840 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 20 20 2f   char *pKey,   /
b8850 2a 20 54 68 65 20 6b 65 79 20 77 65 20 61 72 65  * The key we are
b8860 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a   searching for *
b8870 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20  /.  int nKey,   
b8880 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73          /* Bytes
b8890 20 69 6e 20 6b 65 79 20 28 6e 6f 74 20 63 6f 75   in key (not cou
b88a0 6e 74 69 6e 67 20 7a 65 72 6f 20 74 65 72 6d 69  nting zero termi
b88b0 6e 61 74 6f 72 29 20 2a 2f 0a 20 20 75 6e 73 69  nator) */.  unsi
b88c0 67 6e 65 64 20 69 6e 74 20 68 20 20 20 20 20 20  gned int h      
b88d0 2f 2a 20 54 68 65 20 68 61 73 68 20 66 6f 72 20  /* The hash for 
b88e0 74 68 69 73 20 6b 65 79 2e 20 2a 2f 0a 29 7b 0a  this key. */.){.
b88f0 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d    HashElem *elem
b8900 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
b8910 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70   /* Used to loop
b8920 20 74 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e   thru the elemen
b8930 74 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20  t list */.  int 
b8940 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20  count;          
b8950 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
b8960 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73  mber of elements
b8970 20 6c 65 66 74 20 74 6f 20 74 65 73 74 20 2a 2f   left to test */
b8980 0a 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29  ..  if( pH->ht )
b8990 7b 0a 20 20 20 20 73 74 72 75 63 74 20 5f 68 74  {.    struct _ht
b89a0 20 2a 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e   *pEntry = &pH->
b89b0 68 74 5b 68 5d 3b 0a 20 20 20 20 65 6c 65 6d 20  ht[h];.    elem 
b89c0 3d 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3b  = pEntry->chain;
b89d0 0a 20 20 20 20 63 6f 75 6e 74 20 3d 20 70 45 6e  .    count = pEn
b89e0 74 72 79 2d 3e 63 6f 75 6e 74 3b 0a 20 20 7d 65  try->count;.  }e
b89f0 6c 73 65 7b 0a 20 20 20 20 65 6c 65 6d 20 3d 20  lse{.    elem = 
b8a00 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 20 20 63  pH->first;.    c
b8a10 6f 75 6e 74 20 3d 20 70 48 2d 3e 63 6f 75 6e 74  ount = pH->count
b8a20 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 63  ;.  }.  while( c
b8a30 6f 75 6e 74 2d 2d 20 26 26 20 41 4c 57 41 59 53  ount-- && ALWAYS
b8a40 28 65 6c 65 6d 29 20 29 7b 0a 20 20 20 20 69 66  (elem) ){.    if
b8a50 28 20 65 6c 65 6d 2d 3e 6e 4b 65 79 3d 3d 6e 4b  ( elem->nKey==nK
b8a60 65 79 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  ey && sqlite3Str
b8a70 4e 49 43 6d 70 28 65 6c 65 6d 2d 3e 70 4b 65 79  NICmp(elem->pKey
b8a80 2c 70 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30 20 29  ,pKey,nKey)==0 )
b8a90 7b 20 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  { .      return 
b8aa0 65 6c 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  elem;.    }.    
b8ab0 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78  elem = elem->nex
b8ac0 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  t;.  }.  return 
b8ad0 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20  0;.}../* Remove 
b8ae0 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 66  a single entry f
b8af0 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62  rom the hash tab
b8b00 6c 65 20 67 69 76 65 6e 20 61 20 70 6f 69 6e 74  le given a point
b8b10 65 72 20 74 6f 20 74 68 61 74 0a 2a 2a 20 65 6c  er to that.** el
b8b20 65 6d 65 6e 74 20 61 6e 64 20 61 20 68 61 73 68  ement and a hash
b8b30 20 6f 6e 20 74 68 65 20 65 6c 65 6d 65 6e 74 27   on the element'
b8b40 73 20 6b 65 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  s key..*/.static
b8b50 20 76 6f 69 64 20 72 65 6d 6f 76 65 45 6c 65 6d   void removeElem
b8b60 65 6e 74 47 69 76 65 6e 48 61 73 68 28 0a 20 20  entGivenHash(.  
b8b70 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20  Hash *pH,       
b8b80 20 20 2f 2a 20 54 68 65 20 70 48 20 63 6f 6e 74    /* The pH cont
b8b90 61 69 6e 69 6e 67 20 22 65 6c 65 6d 22 20 2a 2f  aining "elem" */
b8ba0 0a 20 20 48 61 73 68 45 6c 65 6d 2a 20 65 6c 65  .  HashElem* ele
b8bb0 6d 2c 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d  m,   /* The elem
b8bc0 65 6e 74 20 74 6f 20 62 65 20 72 65 6d 6f 76 65  ent to be remove
b8bd0 64 20 66 72 6f 6d 20 74 68 65 20 70 48 20 2a 2f  d from the pH */
b8be0 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
b8bf0 68 20 20 20 20 2f 2a 20 48 61 73 68 20 76 61 6c  h    /* Hash val
b8c00 75 65 20 66 6f 72 20 74 68 65 20 65 6c 65 6d 65  ue for the eleme
b8c10 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63  nt */.){.  struc
b8c20 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 3b 0a 20  t _ht *pEntry;. 
b8c30 20 69 66 28 20 65 6c 65 6d 2d 3e 70 72 65 76 20   if( elem->prev 
b8c40 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 70 72 65  ){.    elem->pre
b8c50 76 2d 3e 6e 65 78 74 20 3d 20 65 6c 65 6d 2d 3e  v->next = elem->
b8c60 6e 65 78 74 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a  next; .  }else{.
b8c70 20 20 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20      pH->first = 
b8c80 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a  elem->next;.  }.
b8c90 20 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 65 78 74    if( elem->next
b8ca0 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 6e 65   ){.    elem->ne
b8cb0 78 74 2d 3e 70 72 65 76 20 3d 20 65 6c 65 6d 2d  xt->prev = elem-
b8cc0 3e 70 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28  >prev;.  }.  if(
b8cd0 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 70   pH->ht ){.    p
b8ce0 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b  Entry = &pH->ht[
b8cf0 68 5d 3b 0a 20 20 20 20 69 66 28 20 70 45 6e 74  h];.    if( pEnt
b8d00 72 79 2d 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d 20  ry->chain==elem 
b8d10 29 7b 0a 20 20 20 20 20 20 70 45 6e 74 72 79 2d  ){.      pEntry-
b8d20 3e 63 68 61 69 6e 20 3d 20 65 6c 65 6d 2d 3e 6e  >chain = elem->n
b8d30 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ext;.    }.    p
b8d40 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a  Entry->count--;.
b8d50 20 20 20 20 61 73 73 65 72 74 28 20 70 45 6e 74      assert( pEnt
b8d60 72 79 2d 3e 63 6f 75 6e 74 3e 3d 30 20 29 3b 0a  ry->count>=0 );.
b8d70 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72    }.  sqlite3_fr
b8d80 65 65 28 20 65 6c 65 6d 20 29 3b 0a 20 20 70 48  ee( elem );.  pH
b8d90 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28  ->count--;.  if(
b8da0 20 70 48 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b   pH->count<=0 ){
b8db0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 48 2d  .    assert( pH-
b8dc0 3e 66 69 72 73 74 3d 3d 30 20 29 3b 0a 20 20 20  >first==0 );.   
b8dd0 20 61 73 73 65 72 74 28 20 70 48 2d 3e 63 6f 75   assert( pH->cou
b8de0 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c  nt==0 );.    sql
b8df0 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 70 48  ite3HashClear(pH
b8e00 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 74 74  );.  }.}../* Att
b8e10 65 6d 70 74 20 74 6f 20 6c 6f 63 61 74 65 20 61  empt to locate a
b8e20 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65  n element of the
b8e30 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 20 77   hash table pH w
b8e40 69 74 68 20 61 20 6b 65 79 0a 2a 2a 20 74 68 61  ith a key.** tha
b8e50 74 20 6d 61 74 63 68 65 73 20 70 4b 65 79 2c 6e  t matches pKey,n
b8e60 4b 65 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65  Key.  Return the
b8e70 20 64 61 74 61 20 66 6f 72 20 74 68 69 73 20 65   data for this e
b8e80 6c 65 6d 65 6e 74 20 69 66 20 69 74 20 69 73 0a  lement if it is.
b8e90 2a 2a 20 66 6f 75 6e 64 2c 20 6f 72 20 4e 55 4c  ** found, or NUL
b8ea0 4c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f  L if there is no
b8eb0 20 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54   match..*/.SQLIT
b8ec0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
b8ed0 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28  sqlite3HashFind(
b8ee0 63 6f 6e 73 74 20 48 61 73 68 20 2a 70 48 2c 20  const Hash *pH, 
b8ef0 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79  const char *pKey
b8f00 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 48  , int nKey){.  H
b8f10 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20  ashElem *elem;  
b8f20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74    /* The element
b8f30 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 6b 65   that matches ke
b8f40 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  y */.  unsigned 
b8f50 69 6e 74 20 68 3b 20 20 20 20 2f 2a 20 41 20 68  int h;    /* A h
b8f60 61 73 68 20 6f 6e 20 6b 65 79 20 2a 2f 0a 0a 20  ash on key */.. 
b8f70 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29   assert( pH!=0 )
b8f80 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 79  ;.  assert( pKey
b8f90 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
b8fa0 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 69 66   nKey>=0 );.  if
b8fb0 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20  ( pH->ht ){.    
b8fc0 68 20 3d 20 73 74 72 48 61 73 68 28 70 4b 65 79  h = strHash(pKey
b8fd0 2c 20 6e 4b 65 79 29 20 25 20 70 48 2d 3e 68 74  , nKey) % pH->ht
b8fe0 73 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  size;.  }else{. 
b8ff0 20 20 20 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 20     h = 0;.  }.  
b9000 65 6c 65 6d 20 3d 20 66 69 6e 64 45 6c 65 6d 65  elem = findEleme
b9010 6e 74 47 69 76 65 6e 48 61 73 68 28 70 48 2c 20  ntGivenHash(pH, 
b9020 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 68 29 3b 0a  pKey, nKey, h);.
b9030 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 20 3f 20    return elem ? 
b9040 65 6c 65 6d 2d 3e 64 61 74 61 20 3a 20 30 3b 0a  elem->data : 0;.
b9050 7d 0a 0a 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20  }../* Insert an 
b9060 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65  element into the
b9070 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e 20   hash table pH. 
b9080 20 54 68 65 20 6b 65 79 20 69 73 20 70 4b 65 79   The key is pKey
b9090 2c 6e 4b 65 79 0a 2a 2a 20 61 6e 64 20 74 68 65  ,nKey.** and the
b90a0 20 64 61 74 61 20 69 73 20 22 64 61 74 61 22 2e   data is "data".
b90b0 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c 65  .**.** If no ele
b90c0 6d 65 6e 74 20 65 78 69 73 74 73 20 77 69 74 68  ment exists with
b90d0 20 61 20 6d 61 74 63 68 69 6e 67 20 6b 65 79 2c   a matching key,
b90e0 20 74 68 65 6e 20 61 20 6e 65 77 0a 2a 2a 20 65   then a new.** e
b90f0 6c 65 6d 65 6e 74 20 69 73 20 63 72 65 61 74 65  lement is create
b9100 64 20 61 6e 64 20 4e 55 4c 4c 20 69 73 20 72 65  d and NULL is re
b9110 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  turned..**.** If
b9120 20 61 6e 6f 74 68 65 72 20 65 6c 65 6d 65 6e 74   another element
b9130 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20   already exists 
b9140 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6b 65  with the same ke
b9150 79 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6e  y, then the.** n
b9160 65 77 20 64 61 74 61 20 72 65 70 6c 61 63 65 73  ew data replaces
b9170 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 61 6e   the old data an
b9180 64 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 69  d the old data i
b9190 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54  s returned..** T
b91a0 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 20 63 6f  he key is not co
b91b0 70 69 65 64 20 69 6e 20 74 68 69 73 20 69 6e 73  pied in this ins
b91c0 74 61 6e 63 65 2e 20 20 49 66 20 61 20 6d 61 6c  tance.  If a mal
b91d0 6c 6f 63 20 66 61 69 6c 73 2c 20 74 68 65 6e 0a  loc fails, then.
b91e0 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 61 20  ** the new data 
b91f0 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
b9200 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 69  the hash table i
b9210 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a  s unchanged..**.
b9220 2a 2a 20 49 66 20 74 68 65 20 22 64 61 74 61 22  ** If the "data"
b9230 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68   parameter to th
b9240 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e  is function is N
b9250 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a  ULL, then the.**
b9260 20 65 6c 65 6d 65 6e 74 20 63 6f 72 72 65 73 70   element corresp
b9270 6f 6e 64 69 6e 67 20 74 6f 20 22 6b 65 79 22 20  onding to "key" 
b9280 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20  is removed from 
b9290 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a  the hash table..
b92a0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b92b0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48  E void *sqlite3H
b92c0 61 73 68 49 6e 73 65 72 74 28 48 61 73 68 20 2a  ashInsert(Hash *
b92d0 70 48 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  pH, const char *
b92e0 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c 20  pKey, int nKey, 
b92f0 76 6f 69 64 20 2a 64 61 74 61 29 7b 0a 20 20 75  void *data){.  u
b9300 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 3b 20 20  nsigned int h;  
b9310 20 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73 68       /* the hash
b9320 20 6f 66 20 74 68 65 20 6b 65 79 20 6d 6f 64 75   of the key modu
b9330 6c 6f 20 68 61 73 68 20 74 61 62 6c 65 20 73 69  lo hash table si
b9340 7a 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d  ze */.  HashElem
b9350 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 2f 2a   *elem;       /*
b9360 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68   Used to loop th
b9370 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c  ru the element l
b9380 69 73 74 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65  ist */.  HashEle
b9390 6d 20 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 2f  m *new_elem;   /
b93a0 2a 20 4e 65 77 20 65 6c 65 6d 65 6e 74 20 61 64  * New element ad
b93b0 64 65 64 20 74 6f 20 74 68 65 20 70 48 20 2a 2f  ded to the pH */
b93c0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 48 21 3d  ..  assert( pH!=
b93d0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
b93e0 4b 65 79 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  Key!=0 );.  asse
b93f0 72 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20  rt( nKey>=0 );. 
b9400 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 20   if( pH->htsize 
b9410 29 7b 0a 20 20 20 20 68 20 3d 20 73 74 72 48 61  ){.    h = strHa
b9420 73 68 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25  sh(pKey, nKey) %
b9430 20 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 7d   pH->htsize;.  }
b9440 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 30 3b  else{.    h = 0;
b9450 0a 20 20 7d 0a 20 20 65 6c 65 6d 20 3d 20 66 69  .  }.  elem = fi
b9460 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61  ndElementGivenHa
b9470 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c  sh(pH,pKey,nKey,
b9480 68 29 3b 0a 20 20 69 66 28 20 65 6c 65 6d 20 29  h);.  if( elem )
b9490 7b 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64 5f  {.    void *old_
b94a0 64 61 74 61 20 3d 20 65 6c 65 6d 2d 3e 64 61 74  data = elem->dat
b94b0 61 3b 0a 20 20 20 20 69 66 28 20 64 61 74 61 3d  a;.    if( data=
b94c0 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 6d 6f  =0 ){.      remo
b94d0 76 65 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61  veElementGivenHa
b94e0 73 68 28 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a 20  sh(pH,elem,h);. 
b94f0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
b9500 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74  elem->data = dat
b9510 61 3b 0a 20 20 20 20 20 20 65 6c 65 6d 2d 3e 70  a;.      elem->p
b9520 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20 20  Key = pKey;.    
b9530 20 20 61 73 73 65 72 74 28 6e 4b 65 79 3d 3d 65    assert(nKey==e
b9540 6c 65 6d 2d 3e 6e 4b 65 79 29 3b 0a 20 20 20 20  lem->nKey);.    
b9550 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 6c 64  }.    return old
b9560 5f 64 61 74 61 3b 0a 20 20 7d 0a 20 20 69 66 28  _data;.  }.  if(
b9570 20 64 61 74 61 3d 3d 30 20 29 20 72 65 74 75 72   data==0 ) retur
b9580 6e 20 30 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 20  n 0;.  new_elem 
b9590 3d 20 28 48 61 73 68 45 6c 65 6d 2a 29 73 71 6c  = (HashElem*)sql
b95a0 69 74 65 33 4d 61 6c 6c 6f 63 28 20 73 69 7a 65  ite3Malloc( size
b95b0 6f 66 28 48 61 73 68 45 6c 65 6d 29 20 29 3b 0a  of(HashElem) );.
b95c0 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 3d 3d    if( new_elem==
b95d0 30 20 29 20 72 65 74 75 72 6e 20 64 61 74 61 3b  0 ) return data;
b95e0 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65  .  new_elem->pKe
b95f0 79 20 3d 20 70 4b 65 79 3b 0a 20 20 6e 65 77 5f  y = pKey;.  new_
b9600 65 6c 65 6d 2d 3e 6e 4b 65 79 20 3d 20 6e 4b 65  elem->nKey = nKe
b9610 79 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 64  y;.  new_elem->d
b9620 61 74 61 20 3d 20 64 61 74 61 3b 0a 20 20 70 48  ata = data;.  pH
b9630 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 69 66 28  ->count++;.  if(
b9640 20 70 48 2d 3e 63 6f 75 6e 74 3e 3d 31 30 20 26   pH->count>=10 &
b9650 26 20 70 48 2d 3e 63 6f 75 6e 74 20 3e 20 32 2a  & pH->count > 2*
b9660 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a 20 20  pH->htsize ){.  
b9670 20 20 69 66 28 20 72 65 68 61 73 68 28 70 48 2c    if( rehash(pH,
b9680 20 70 48 2d 3e 63 6f 75 6e 74 2a 32 29 20 29 7b   pH->count*2) ){
b9690 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
b96a0 48 2d 3e 68 74 73 69 7a 65 3e 30 20 29 3b 0a 20  H->htsize>0 );. 
b96b0 20 20 20 20 20 68 20 3d 20 73 74 72 48 61 73 68       h = strHash
b96c0 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20 70  (pKey, nKey) % p
b96d0 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 20 20 7d  H->htsize;.    }
b96e0 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e 68  .  }.  if( pH->h
b96f0 74 20 29 7b 0a 20 20 20 20 69 6e 73 65 72 74 45  t ){.    insertE
b9700 6c 65 6d 65 6e 74 28 70 48 2c 20 26 70 48 2d 3e  lement(pH, &pH->
b9710 68 74 5b 68 5d 2c 20 6e 65 77 5f 65 6c 65 6d 29  ht[h], new_elem)
b9720 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
b9730 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c  nsertElement(pH,
b9740 20 30 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20   0, new_elem);. 
b9750 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
b9760 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
b9770 2a 20 45 6e 64 20 6f 66 20 68 61 73 68 2e 63 20  * End of hash.c 
b9780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b97a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b97b0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
b97c0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63  * Begin file opc
b97d0 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  odes.c *********
b97e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b97f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9800 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c  /./* Automatical
b9810 6c 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44  ly generated.  D
b9820 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a  o not edit */./*
b9830 20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64   See the mkopcod
b9840 65 63 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f  ec.awk script fo
b9850 72 20 64 65 74 61 69 6c 73 2e 20 2a 2f 0a 23 69  r details. */.#i
b9860 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
b9870 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20  E_OMIT_EXPLAIN) 
b9880 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42  || !defined(NDEB
b9890 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56  UG) || defined(V
b98a0 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20  DBE_PROFILE) || 
b98b0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
b98c0 45 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49  EBUG).SQLITE_PRI
b98d0 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
b98e0 2a 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61  *sqlite3OpcodeNa
b98f0 6d 65 28 69 6e 74 20 69 29 7b 0a 20 73 74 61 74  me(int i){. stat
b9900 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  ic const char *c
b9910 6f 6e 73 74 20 61 7a 4e 61 6d 65 5b 5d 20 3d 20  onst azName[] = 
b9920 7b 20 22 3f 22 2c 0a 20 20 20 20 20 2f 2a 20 20  { "?",.     /*  
b9930 20 31 20 2a 2f 20 22 56 4e 65 78 74 22 2c 0a 20   1 */ "VNext",. 
b9940 20 20 20 20 2f 2a 20 20 20 32 20 2a 2f 20 22 41      /*   2 */ "A
b9950 66 66 69 6e 69 74 79 22 2c 0a 20 20 20 20 20 2f  ffinity",.     /
b9960 2a 20 20 20 33 20 2a 2f 20 22 43 6f 6c 75 6d 6e  *   3 */ "Column
b9970 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 34 20 2a  ",.     /*   4 *
b9980 2f 20 22 53 65 74 43 6f 6f 6b 69 65 22 2c 0a 20  / "SetCookie",. 
b9990 20 20 20 20 2f 2a 20 20 20 35 20 2a 2f 20 22 53      /*   5 */ "S
b99a0 65 65 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20  eek",.     /*   
b99b0 36 20 2a 2f 20 22 53 65 71 75 65 6e 63 65 22 2c  6 */ "Sequence",
b99c0 0a 20 20 20 20 20 2f 2a 20 20 20 37 20 2a 2f 20  .     /*   7 */ 
b99d0 22 53 61 76 65 70 6f 69 6e 74 22 2c 0a 20 20 20  "Savepoint",.   
b99e0 20 20 2f 2a 20 20 20 38 20 2a 2f 20 22 52 6f 77    /*   8 */ "Row
b99f0 4b 65 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20  Key",.     /*   
b9a00 39 20 2a 2f 20 22 53 43 6f 70 79 22 2c 0a 20 20  9 */ "SCopy",.  
b9a10 20 20 20 2f 2a 20 20 31 30 20 2a 2f 20 22 4f 70     /*  10 */ "Op
b9a20 65 6e 57 72 69 74 65 22 2c 0a 20 20 20 20 20 2f  enWrite",.     /
b9a30 2a 20 20 31 31 20 2a 2f 20 22 49 66 22 2c 0a 20  *  11 */ "If",. 
b9a40 20 20 20 20 2f 2a 20 20 31 32 20 2a 2f 20 22 43      /*  12 */ "C
b9a50 6f 6c 6c 53 65 71 22 2c 0a 20 20 20 20 20 2f 2a  ollSeq",.     /*
b9a60 20 20 31 33 20 2a 2f 20 22 4f 70 65 6e 52 65 61    13 */ "OpenRea
b9a70 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 34 20  d",.     /*  14 
b9a80 2a 2f 20 22 45 78 70 69 72 65 22 2c 0a 20 20 20  */ "Expire",.   
b9a90 20 20 2f 2a 20 20 31 35 20 2a 2f 20 22 41 75 74    /*  15 */ "Aut
b9aa0 6f 43 6f 6d 6d 69 74 22 2c 0a 20 20 20 20 20 2f  oCommit",.     /
b9ab0 2a 20 20 31 36 20 2a 2f 20 22 50 61 67 65 63 6f  *  16 */ "Pageco
b9ac0 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31  unt",.     /*  1
b9ad0 37 20 2a 2f 20 22 49 6e 74 65 67 72 69 74 79 43  7 */ "IntegrityC
b9ae0 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 38 20  k",.     /*  18 
b9af0 2a 2f 20 22 53 6f 72 74 22 2c 0a 20 20 20 20 20  */ "Sort",.     
b9b00 2f 2a 20 20 31 39 20 2a 2f 20 22 4e 6f 74 22 2c  /*  19 */ "Not",
b9b10 0a 20 20 20 20 20 2f 2a 20 20 32 30 20 2a 2f 20  .     /*  20 */ 
b9b20 22 43 6f 70 79 22 2c 0a 20 20 20 20 20 2f 2a 20  "Copy",.     /* 
b9b30 20 32 31 20 2a 2f 20 22 54 72 61 63 65 22 2c 0a   21 */ "Trace",.
b9b40 20 20 20 20 20 2f 2a 20 20 32 32 20 2a 2f 20 22       /*  22 */ "
b9b50 46 75 6e 63 74 69 6f 6e 22 2c 0a 20 20 20 20 20  Function",.     
b9b60 2f 2a 20 20 32 33 20 2a 2f 20 22 49 66 4e 65 67  /*  23 */ "IfNeg
b9b70 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 34 20 2a  ",.     /*  24 *
b9b80 2f 20 22 4e 6f 6f 70 22 2c 0a 20 20 20 20 20 2f  / "Noop",.     /
b9b90 2a 20 20 32 35 20 2a 2f 20 22 50 72 6f 67 72 61  *  25 */ "Progra
b9ba0 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 36 20  m",.     /*  26 
b9bb0 2a 2f 20 22 52 65 74 75 72 6e 22 2c 0a 20 20 20  */ "Return",.   
b9bc0 20 20 2f 2a 20 20 32 37 20 2a 2f 20 22 4e 65 77    /*  27 */ "New
b9bd0 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f 2a 20  Rowid",.     /* 
b9be0 20 32 38 20 2a 2f 20 22 56 61 72 69 61 62 6c 65   28 */ "Variable
b9bf0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 39 20 2a  ",.     /*  29 *
b9c00 2f 20 22 53 74 72 69 6e 67 22 2c 0a 20 20 20 20  / "String",.    
b9c10 20 2f 2a 20 20 33 30 20 2a 2f 20 22 52 65 61 6c   /*  30 */ "Real
b9c20 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 20 20 20  Affinity",.     
b9c30 2f 2a 20 20 33 31 20 2a 2f 20 22 56 52 65 6e 61  /*  31 */ "VRena
b9c40 6d 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 32  me",.     /*  32
b9c50 20 2a 2f 20 22 50 61 72 73 65 53 63 68 65 6d 61   */ "ParseSchema
b9c60 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 33 20 2a  ",.     /*  33 *
b9c70 2f 20 22 56 4f 70 65 6e 22 2c 0a 20 20 20 20 20  / "VOpen",.     
b9c80 2f 2a 20 20 33 34 20 2a 2f 20 22 43 6c 6f 73 65  /*  34 */ "Close
b9c90 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 35 20 2a  ",.     /*  35 *
b9ca0 2f 20 22 43 72 65 61 74 65 49 6e 64 65 78 22 2c  / "CreateIndex",
b9cb0 0a 20 20 20 20 20 2f 2a 20 20 33 36 20 2a 2f 20  .     /*  36 */ 
b9cc0 22 49 73 55 6e 69 71 75 65 22 2c 0a 20 20 20 20  "IsUnique",.    
b9cd0 20 2f 2a 20 20 33 37 20 2a 2f 20 22 4e 6f 74 46   /*  37 */ "NotF
b9ce0 6f 75 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20  ound",.     /*  
b9cf0 33 38 20 2a 2f 20 22 49 6e 74 36 34 22 2c 0a 20  38 */ "Int64",. 
b9d00 20 20 20 20 2f 2a 20 20 33 39 20 2a 2f 20 22 4d      /*  39 */ "M
b9d10 75 73 74 42 65 49 6e 74 22 2c 0a 20 20 20 20 20  ustBeInt",.     
b9d20 2f 2a 20 20 34 30 20 2a 2f 20 22 48 61 6c 74 22  /*  40 */ "Halt"
b9d30 2c 0a 20 20 20 20 20 2f 2a 20 20 34 31 20 2a 2f  ,.     /*  41 */
b9d40 20 22 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f   "Rowid",.     /
b9d50 2a 20 20 34 32 20 2a 2f 20 22 49 64 78 4c 54 22  *  42 */ "IdxLT"
b9d60 2c 0a 20 20 20 20 20 2f 2a 20 20 34 33 20 2a 2f  ,.     /*  43 */
b9d70 20 22 41 64 64 49 6d 6d 22 2c 0a 20 20 20 20 20   "AddImm",.     
b9d80 2f 2a 20 20 34 34 20 2a 2f 20 22 52 6f 77 44 61  /*  44 */ "RowDa
b9d90 74 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 35  ta",.     /*  45
b9da0 20 2a 2f 20 22 4d 65 6d 4d 61 78 22 2c 0a 20 20   */ "MemMax",.  
b9db0 20 20 20 2f 2a 20 20 34 36 20 2a 2f 20 22 4e 6f     /*  46 */ "No
b9dc0 74 45 78 69 73 74 73 22 2c 0a 20 20 20 20 20 2f  tExists",.     /
b9dd0 2a 20 20 34 37 20 2a 2f 20 22 47 6f 73 75 62 22  *  47 */ "Gosub"
b9de0 2c 0a 20 20 20 20 20 2f 2a 20 20 34 38 20 2a 2f  ,.     /*  48 */
b9df0 20 22 49 6e 74 65 67 65 72 22 2c 0a 20 20 20 20   "Integer",.    
b9e00 20 2f 2a 20 20 34 39 20 2a 2f 20 22 50 72 65 76   /*  49 */ "Prev
b9e10 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 30 20 2a  ",.     /*  50 *
b9e20 2f 20 22 52 6f 77 53 65 74 52 65 61 64 22 2c 0a  / "RowSetRead",.
b9e30 20 20 20 20 20 2f 2a 20 20 35 31 20 2a 2f 20 22       /*  51 */ "
b9e40 52 6f 77 53 65 74 41 64 64 22 2c 0a 20 20 20 20  RowSetAdd",.    
b9e50 20 2f 2a 20 20 35 32 20 2a 2f 20 22 56 43 6f 6c   /*  52 */ "VCol
b9e60 75 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35  umn",.     /*  5
b9e70 33 20 2a 2f 20 22 43 72 65 61 74 65 54 61 62 6c  3 */ "CreateTabl
b9e80 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 34 20  e",.     /*  54 
b9e90 2a 2f 20 22 4c 61 73 74 22 2c 0a 20 20 20 20 20  */ "Last",.     
b9ea0 2f 2a 20 20 35 35 20 2a 2f 20 22 53 65 65 6b 4c  /*  55 */ "SeekL
b9eb0 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 36 20  e",.     /*  56 
b9ec0 2a 2f 20 22 49 6e 63 72 56 61 63 75 75 6d 22 2c  */ "IncrVacuum",
b9ed0 0a 20 20 20 20 20 2f 2a 20 20 35 37 20 2a 2f 20  .     /*  57 */ 
b9ee0 22 49 64 78 52 6f 77 69 64 22 2c 0a 20 20 20 20  "IdxRowid",.    
b9ef0 20 2f 2a 20 20 35 38 20 2a 2f 20 22 52 65 73 65   /*  58 */ "Rese
b9f00 74 43 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a  tCount",.     /*
b9f10 20 20 35 39 20 2a 2f 20 22 59 69 65 6c 64 22 2c    59 */ "Yield",
b9f20 0a 20 20 20 20 20 2f 2a 20 20 36 30 20 2a 2f 20  .     /*  60 */ 
b9f30 22 44 72 6f 70 54 72 69 67 67 65 72 22 2c 0a 20  "DropTrigger",. 
b9f40 20 20 20 20 2f 2a 20 20 36 31 20 2a 2f 20 22 44      /*  61 */ "D
b9f50 72 6f 70 49 6e 64 65 78 22 2c 0a 20 20 20 20 20  ropIndex",.     
b9f60 2f 2a 20 20 36 32 20 2a 2f 20 22 50 61 72 61 6d  /*  62 */ "Param
b9f70 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 33 20 2a  ",.     /*  63 *
b9f80 2f 20 22 49 64 78 47 45 22 2c 0a 20 20 20 20 20  / "IdxGE",.     
b9f90 2f 2a 20 20 36 34 20 2a 2f 20 22 49 64 78 44 65  /*  64 */ "IdxDe
b9fa0 6c 65 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20  lete",.     /*  
b9fb0 36 35 20 2a 2f 20 22 56 61 63 75 75 6d 22 2c 0a  65 */ "Vacuum",.
b9fc0 20 20 20 20 20 2f 2a 20 20 36 36 20 2a 2f 20 22       /*  66 */ "
b9fd0 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 37  Or",.     /*  67
b9fe0 20 2a 2f 20 22 41 6e 64 22 2c 0a 20 20 20 20 20   */ "And",.     
b9ff0 2f 2a 20 20 36 38 20 2a 2f 20 22 49 66 4e 6f 74  /*  68 */ "IfNot
ba000 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 39 20 2a  ",.     /*  69 *
ba010 2f 20 22 44 72 6f 70 54 61 62 6c 65 22 2c 0a 20  / "DropTable",. 
ba020 20 20 20 20 2f 2a 20 20 37 30 20 2a 2f 20 22 53      /*  70 */ "S
ba030 65 65 6b 4c 74 22 2c 0a 20 20 20 20 20 2f 2a 20  eekLt",.     /* 
ba040 20 37 31 20 2a 2f 20 22 49 73 4e 75 6c 6c 22 2c   71 */ "IsNull",
ba050 0a 20 20 20 20 20 2f 2a 20 20 37 32 20 2a 2f 20  .     /*  72 */ 
ba060 22 4e 6f 74 4e 75 6c 6c 22 2c 0a 20 20 20 20 20  "NotNull",.     
ba070 2f 2a 20 20 37 33 20 2a 2f 20 22 4e 65 22 2c 0a  /*  73 */ "Ne",.
ba080 20 20 20 20 20 2f 2a 20 20 37 34 20 2a 2f 20 22       /*  74 */ "
ba090 45 71 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 35  Eq",.     /*  75
ba0a0 20 2a 2f 20 22 47 74 22 2c 0a 20 20 20 20 20 2f   */ "Gt",.     /
ba0b0 2a 20 20 37 36 20 2a 2f 20 22 4c 65 22 2c 0a 20  *  76 */ "Le",. 
ba0c0 20 20 20 20 2f 2a 20 20 37 37 20 2a 2f 20 22 4c      /*  77 */ "L
ba0d0 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 38 20  t",.     /*  78 
ba0e0 2a 2f 20 22 47 65 22 2c 0a 20 20 20 20 20 2f 2a  */ "Ge",.     /*
ba0f0 20 20 37 39 20 2a 2f 20 22 4d 61 6b 65 52 65 63    79 */ "MakeRec
ba100 6f 72 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38  ord",.     /*  8
ba110 30 20 2a 2f 20 22 42 69 74 41 6e 64 22 2c 0a 20  0 */ "BitAnd",. 
ba120 20 20 20 20 2f 2a 20 20 38 31 20 2a 2f 20 22 42      /*  81 */ "B
ba130 69 74 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20  itOr",.     /*  
ba140 38 32 20 2a 2f 20 22 53 68 69 66 74 4c 65 66 74  82 */ "ShiftLeft
ba150 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 33 20 2a  ",.     /*  83 *
ba160 2f 20 22 53 68 69 66 74 52 69 67 68 74 22 2c 0a  / "ShiftRight",.
ba170 20 20 20 20 20 2f 2a 20 20 38 34 20 2a 2f 20 22       /*  84 */ "
ba180 41 64 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38  Add",.     /*  8
ba190 35 20 2a 2f 20 22 53 75 62 74 72 61 63 74 22 2c  5 */ "Subtract",
ba1a0 0a 20 20 20 20 20 2f 2a 20 20 38 36 20 2a 2f 20  .     /*  86 */ 
ba1b0 22 4d 75 6c 74 69 70 6c 79 22 2c 0a 20 20 20 20  "Multiply",.    
ba1c0 20 2f 2a 20 20 38 37 20 2a 2f 20 22 44 69 76 69   /*  87 */ "Divi
ba1d0 64 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 38  de",.     /*  88
ba1e0 20 2a 2f 20 22 52 65 6d 61 69 6e 64 65 72 22 2c   */ "Remainder",
ba1f0 0a 20 20 20 20 20 2f 2a 20 20 38 39 20 2a 2f 20  .     /*  89 */ 
ba200 22 43 6f 6e 63 61 74 22 2c 0a 20 20 20 20 20 2f  "Concat",.     /
ba210 2a 20 20 39 30 20 2a 2f 20 22 52 65 73 75 6c 74  *  90 */ "Result
ba220 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39  Row",.     /*  9
ba230 31 20 2a 2f 20 22 44 65 6c 65 74 65 22 2c 0a 20  1 */ "Delete",. 
ba240 20 20 20 20 2f 2a 20 20 39 32 20 2a 2f 20 22 41      /*  92 */ "A
ba250 67 67 46 69 6e 61 6c 22 2c 0a 20 20 20 20 20 2f  ggFinal",.     /
ba260 2a 20 20 39 33 20 2a 2f 20 22 42 69 74 4e 6f 74  *  93 */ "BitNot
ba270 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 34 20 2a  ",.     /*  94 *
ba280 2f 20 22 53 74 72 69 6e 67 38 22 2c 0a 20 20 20  / "String8",.   
ba290 20 20 2f 2a 20 20 39 35 20 2a 2f 20 22 43 6f 6d    /*  95 */ "Com
ba2a0 70 61 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20  pare",.     /*  
ba2b0 39 36 20 2a 2f 20 22 47 6f 74 6f 22 2c 0a 20 20  96 */ "Goto",.  
ba2c0 20 20 20 2f 2a 20 20 39 37 20 2a 2f 20 22 54 61     /*  97 */ "Ta
ba2d0 62 6c 65 4c 6f 63 6b 22 2c 0a 20 20 20 20 20 2f  bleLock",.     /
ba2e0 2a 20 20 39 38 20 2a 2f 20 22 43 6c 65 61 72 22  *  98 */ "Clear"
ba2f0 2c 0a 20 20 20 20 20 2f 2a 20 20 39 39 20 2a 2f  ,.     /*  99 */
ba300 20 22 56 65 72 69 66 79 43 6f 6f 6b 69 65 22 2c   "VerifyCookie",
ba310 0a 20 20 20 20 20 2f 2a 20 31 30 30 20 2a 2f 20  .     /* 100 */ 
ba320 22 41 67 67 53 74 65 70 22 2c 0a 20 20 20 20 20  "AggStep",.     
ba330 2f 2a 20 31 30 31 20 2a 2f 20 22 54 72 61 6e 73  /* 101 */ "Trans
ba340 61 63 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a  action",.     /*
ba350 20 31 30 32 20 2a 2f 20 22 56 46 69 6c 74 65 72   102 */ "VFilter
ba360 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 33 20 2a  ",.     /* 103 *
ba370 2f 20 22 56 44 65 73 74 72 6f 79 22 2c 0a 20 20  / "VDestroy",.  
ba380 20 20 20 2f 2a 20 31 30 34 20 2a 2f 20 22 4e 65     /* 104 */ "Ne
ba390 78 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 35  xt",.     /* 105
ba3a0 20 2a 2f 20 22 43 6f 75 6e 74 22 2c 0a 20 20 20   */ "Count",.   
ba3b0 20 20 2f 2a 20 31 30 36 20 2a 2f 20 22 49 64 78    /* 106 */ "Idx
ba3c0 49 6e 73 65 72 74 22 2c 0a 20 20 20 20 20 2f 2a  Insert",.     /*
ba3d0 20 31 30 37 20 2a 2f 20 22 53 65 65 6b 47 65 22   107 */ "SeekGe"
ba3e0 2c 0a 20 20 20 20 20 2f 2a 20 31 30 38 20 2a 2f  ,.     /* 108 */
ba3f0 20 22 49 6e 73 65 72 74 22 2c 0a 20 20 20 20 20   "Insert",.     
ba400 2f 2a 20 31 30 39 20 2a 2f 20 22 44 65 73 74 72  /* 109 */ "Destr
ba410 6f 79 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 30  oy",.     /* 110
ba420 20 2a 2f 20 22 52 65 61 64 43 6f 6f 6b 69 65 22   */ "ReadCookie"
ba430 2c 0a 20 20 20 20 20 2f 2a 20 31 31 31 20 2a 2f  ,.     /* 111 */
ba440 20 22 52 6f 77 53 65 74 54 65 73 74 22 2c 0a 20   "RowSetTest",. 
ba450 20 20 20 20 2f 2a 20 31 31 32 20 2a 2f 20 22 4c      /* 112 */ "L
ba460 6f 61 64 41 6e 61 6c 79 73 69 73 22 2c 0a 20 20  oadAnalysis",.  
ba470 20 20 20 2f 2a 20 31 31 33 20 2a 2f 20 22 45 78     /* 113 */ "Ex
ba480 70 6c 61 69 6e 22 2c 0a 20 20 20 20 20 2f 2a 20  plain",.     /* 
ba490 31 31 34 20 2a 2f 20 22 48 61 6c 74 49 66 4e 75  114 */ "HaltIfNu
ba4a0 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 35  ll",.     /* 115
ba4b0 20 2a 2f 20 22 4f 70 65 6e 50 73 65 75 64 6f 22   */ "OpenPseudo"
ba4c0 2c 0a 20 20 20 20 20 2f 2a 20 31 31 36 20 2a 2f  ,.     /* 116 */
ba4d0 20 22 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 22   "OpenEphemeral"
ba4e0 2c 0a 20 20 20 20 20 2f 2a 20 31 31 37 20 2a 2f  ,.     /* 117 */
ba4f0 20 22 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a   "Null",.     /*
ba500 20 31 31 38 20 2a 2f 20 22 4d 6f 76 65 22 2c 0a   118 */ "Move",.
ba510 20 20 20 20 20 2f 2a 20 31 31 39 20 2a 2f 20 22       /* 119 */ "
ba520 42 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31  Blob",.     /* 1
ba530 32 30 20 2a 2f 20 22 52 65 77 69 6e 64 22 2c 0a  20 */ "Rewind",.
ba540 20 20 20 20 20 2f 2a 20 31 32 31 20 2a 2f 20 22       /* 121 */ "
ba550 53 65 65 6b 47 74 22 2c 0a 20 20 20 20 20 2f 2a  SeekGt",.     /*
ba560 20 31 32 32 20 2a 2f 20 22 56 42 65 67 69 6e 22   122 */ "VBegin"
ba570 2c 0a 20 20 20 20 20 2f 2a 20 31 32 33 20 2a 2f  ,.     /* 123 */
ba580 20 22 56 55 70 64 61 74 65 22 2c 0a 20 20 20 20   "VUpdate",.    
ba590 20 2f 2a 20 31 32 34 20 2a 2f 20 22 49 66 5a 65   /* 124 */ "IfZe
ba5a0 72 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 35  ro",.     /* 125
ba5b0 20 2a 2f 20 22 56 43 72 65 61 74 65 22 2c 0a 20   */ "VCreate",. 
ba5c0 20 20 20 20 2f 2a 20 31 32 36 20 2a 2f 20 22 46      /* 126 */ "F
ba5d0 6f 75 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 31  ound",.     /* 1
ba5e0 32 37 20 2a 2f 20 22 49 66 50 6f 73 22 2c 0a 20  27 */ "IfPos",. 
ba5f0 20 20 20 20 2f 2a 20 31 32 38 20 2a 2f 20 22 4e      /* 128 */ "N
ba600 75 6c 6c 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a  ullRow",.     /*
ba610 20 31 32 39 20 2a 2f 20 22 4a 75 6d 70 22 2c 0a   129 */ "Jump",.
ba620 20 20 20 20 20 2f 2a 20 31 33 30 20 2a 2f 20 22       /* 130 */ "
ba630 52 65 61 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 31  Real",.     /* 1
ba640 33 31 20 2a 2f 20 22 50 65 72 6d 75 74 61 74 69  31 */ "Permutati
ba650 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 32  on",.     /* 132
ba660 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 32   */ "NotUsed_132
ba670 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 33 20 2a  ",.     /* 133 *
ba680 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 33 22 2c  / "NotUsed_133",
ba690 0a 20 20 20 20 20 2f 2a 20 31 33 34 20 2a 2f 20  .     /* 134 */ 
ba6a0 22 4e 6f 74 55 73 65 64 5f 31 33 34 22 2c 0a 20  "NotUsed_134",. 
ba6b0 20 20 20 20 2f 2a 20 31 33 35 20 2a 2f 20 22 4e      /* 135 */ "N
ba6c0 6f 74 55 73 65 64 5f 31 33 35 22 2c 0a 20 20 20  otUsed_135",.   
ba6d0 20 20 2f 2a 20 31 33 36 20 2a 2f 20 22 4e 6f 74    /* 136 */ "Not
ba6e0 55 73 65 64 5f 31 33 36 22 2c 0a 20 20 20 20 20  Used_136",.     
ba6f0 2f 2a 20 31 33 37 20 2a 2f 20 22 4e 6f 74 55 73  /* 137 */ "NotUs
ba700 65 64 5f 31 33 37 22 2c 0a 20 20 20 20 20 2f 2a  ed_137",.     /*
ba710 20 31 33 38 20 2a 2f 20 22 4e 6f 74 55 73 65 64   138 */ "NotUsed
ba720 5f 31 33 38 22 2c 0a 20 20 20 20 20 2f 2a 20 31  _138",.     /* 1
ba730 33 39 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31  39 */ "NotUsed_1
ba740 33 39 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 30  39",.     /* 140
ba750 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 34 30   */ "NotUsed_140
ba760 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 31 20 2a  ",.     /* 141 *
ba770 2f 20 22 54 6f 54 65 78 74 22 2c 0a 20 20 20 20  / "ToText",.    
ba780 20 2f 2a 20 31 34 32 20 2a 2f 20 22 54 6f 42 6c   /* 142 */ "ToBl
ba790 6f 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 33  ob",.     /* 143
ba7a0 20 2a 2f 20 22 54 6f 4e 75 6d 65 72 69 63 22 2c   */ "ToNumeric",
ba7b0 0a 20 20 20 20 20 2f 2a 20 31 34 34 20 2a 2f 20  .     /* 144 */ 
ba7c0 22 54 6f 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a  "ToInt",.     /*
ba7d0 20 31 34 35 20 2a 2f 20 22 54 6f 52 65 61 6c 22   145 */ "ToReal"
ba7e0 2c 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e 20  ,.  };.  return 
ba7f0 61 7a 4e 61 6d 65 5b 69 5d 3b 0a 7d 0a 23 65 6e  azName[i];.}.#en
ba800 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
ba810 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f  **** End of opco
ba820 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  des.c **********
ba830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba850 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
ba860 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
ba870 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a  os_os2.c *******
ba880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba8a0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20  ***/./*.** 2006 
ba8b0 46 65 62 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65  Feb 14.**.** The
ba8c0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
ba8d0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
ba8e0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
ba8f0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
ba900 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
ba910 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
ba920 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
ba930 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
ba940 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
ba950 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
ba960 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
ba970 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
ba980 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
ba990 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
ba9a0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
ba9b0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
ba9c0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
ba9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
baa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
baa10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
baa20 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
baa30 69 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69 73  ins code that is
baa40 20 73 70 65 63 69 66 69 63 20 74 6f 20 4f 53 2f   specific to OS/
baa50 32 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73  2..**.** $Id: os
baa60 5f 6f 73 32 2e 63 2c 76 20 31 2e 36 33 20 32 30  _os2.c,v 1.63 20
baa70 30 38 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a 32  08/12/10 19:26:2
baa80 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  4 drh Exp $.*/..
baa90 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f  .#if SQLITE_OS_O
baaa0 53 32 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 65  S2../*.** A Note
baab0 20 41 62 6f 75 74 20 4d 65 6d 6f 72 79 20 41 6c   About Memory Al
baac0 6c 6f 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20  location:.**.** 
baad0 54 68 69 73 20 64 72 69 76 65 72 20 75 73 65 73  This driver uses
baae0 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29   malloc()/free()
baaf0 20 64 69 72 65 63 74 6c 79 20 72 61 74 68 65 72   directly rather
bab00 20 74 68 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f   than going thro
bab10 75 67 68 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74  ugh.** the SQLit
bab20 65 2d 77 72 61 70 70 65 72 73 20 73 71 6c 69 74  e-wrappers sqlit
bab30 65 33 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c 69  e3_malloc()/sqli
bab40 74 65 33 5f 66 72 65 65 28 29 2e 20 20 54 68 6f  te3_free().  Tho
bab50 73 65 20 77 72 61 70 70 65 72 73 0a 2a 2a 20 61  se wrappers.** a
bab60 72 65 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20  re designed for 
bab70 75 73 65 20 6f 6e 20 65 6d 62 65 64 64 65 64 20  use on embedded 
bab80 73 79 73 74 65 6d 73 20 77 68 65 72 65 20 6d 65  systems where me
bab90 6d 6f 72 79 20 69 73 20 73 63 61 72 63 65 20 61  mory is scarce a
baba0 6e 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69  nd.** malloc fai
babb0 6c 75 72 65 73 20 68 61 70 70 65 6e 20 66 72 65  lures happen fre
babc0 71 75 65 6e 74 6c 79 2e 20 20 4f 53 2f 32 20 64  quently.  OS/2 d
babd0 6f 65 73 20 6e 6f 74 20 74 79 70 69 63 61 6c 6c  oes not typicall
babe0 79 20 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65  y run on.** embe
babf0 64 64 65 64 20 73 79 73 74 65 6d 73 2c 20 61 6e  dded systems, an
bac00 64 20 77 68 65 6e 20 69 74 20 64 6f 65 73 20 74  d when it does t
bac10 68 65 20 64 65 76 65 6c 6f 70 65 72 73 20 6e 6f  he developers no
bac20 72 6d 61 6c 6c 79 20 68 61 76 65 20 62 69 67 67  rmally have bigg
bac30 65 72 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 74  er.** problems t
bac40 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 74 68  o worry about th
bac50 61 6e 20 72 75 6e 6e 69 6e 67 20 6f 75 74 20 6f  an running out o
bac60 66 20 6d 65 6d 6f 72 79 2e 20 20 53 6f 20 74 68  f memory.  So th
bac70 65 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20  ere is not.** a 
bac80 63 6f 6d 70 65 6c 6c 69 6e 67 20 6e 65 65 64 20  compelling need 
bac90 74 6f 20 75 73 65 20 74 68 65 20 77 72 61 70 70  to use the wrapp
baca0 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74  ers..**.** But t
bacb0 68 65 72 65 20 69 73 20 61 20 67 6f 6f 64 20 72  here is a good r
bacc0 65 61 73 6f 6e 20 74 6f 20 6e 6f 74 20 75 73 65  eason to not use
bacd0 20 74 68 65 20 77 72 61 70 70 65 72 73 2e 20 20   the wrappers.  
bace0 49 66 20 77 65 20 75 73 65 20 74 68 65 0a 2a 2a  If we use the.**
bacf0 20 77 72 61 70 70 65 72 73 20 74 68 65 6e 20 77   wrappers then w
bad00 65 20 77 69 6c 6c 20 67 65 74 20 73 69 6d 75 6c  e will get simul
bad10 61 74 65 64 20 6d 61 6c 6c 6f 63 28 29 20 66 61  ated malloc() fa
bad20 69 6c 75 72 65 73 20 77 69 74 68 69 6e 20 74 68  ilures within th
bad30 69 73 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20 41  is.** driver.  A
bad40 6e 64 20 74 68 61 74 20 63 61 75 73 65 73 20 61  nd that causes a
bad50 6c 6c 20 6b 69 6e 64 73 20 6f 66 20 70 72 6f 62  ll kinds of prob
bad60 6c 65 6d 73 20 66 6f 72 20 6f 75 72 20 74 65 73  lems for our tes
bad70 74 73 2e 20 20 57 65 0a 2a 2a 20 63 6f 75 6c 64  ts.  We.** could
bad80 20 65 6e 68 61 6e 63 65 20 53 51 4c 69 74 65 20   enhance SQLite 
bad90 74 6f 20 64 65 61 6c 20 77 69 74 68 20 73 69 6d  to deal with sim
bada0 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 20 66 61  ulated malloc fa
badb0 69 6c 75 72 65 73 20 77 69 74 68 69 6e 0a 2a 2a  ilures within.**
badc0 20 74 68 65 20 4f 53 20 64 72 69 76 65 72 2c 20   the OS driver, 
badd0 62 75 74 20 74 68 65 20 63 6f 64 65 20 74 6f 20  but the code to 
bade0 64 65 61 6c 20 77 69 74 68 20 74 68 6f 73 65 20  deal with those 
badf0 66 61 69 6c 75 72 65 20 77 6f 75 6c 64 20 6e 6f  failure would no
bae00 74 0a 2a 2a 20 62 65 20 65 78 65 72 63 69 73 65  t.** be exercise
bae10 64 20 6f 6e 20 4c 69 6e 75 78 20 28 77 68 69 63  d on Linux (whic
bae20 68 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  h does not need 
bae30 74 6f 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 20 74  to malloc() in t
bae40 68 65 20 64 72 69 76 65 72 29 0a 2a 2a 20 61 6e  he driver).** an
bae50 64 20 73 6f 20 77 65 20 77 6f 75 6c 64 20 68 61  d so we would ha
bae60 76 65 20 64 69 66 66 69 63 75 6c 74 79 20 77 72  ve difficulty wr
bae70 69 74 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74  iting coverage t
bae80 65 73 74 73 20 66 6f 72 20 74 68 61 74 0a 2a 2a  ests for that.**
bae90 20 63 6f 64 65 2e 20 20 42 65 74 74 65 72 20 74   code.  Better t
baea0 6f 20 6c 65 61 76 65 20 74 68 65 20 63 6f 64 65  o leave the code
baeb0 20 6f 75 74 2c 20 77 65 20 74 68 69 6e 6b 2e 0a   out, we think..
baec0 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 20  **.** The point 
baed0 6f 66 20 74 68 69 73 20 64 69 73 63 75 73 73 69  of this discussi
baee0 6f 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73  on is as follows
baef0 3a 20 20 57 68 65 6e 20 63 72 65 61 74 69 6e 67  :  When creating
baf00 20 61 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61 79   a new.** OS lay
baf10 65 72 20 66 6f 72 20 61 6e 20 65 6d 62 65 64 64  er for an embedd
baf20 65 64 20 73 79 73 74 65 6d 2c 20 69 66 20 79 6f  ed system, if yo
baf30 75 20 75 73 65 20 74 68 69 73 20 66 69 6c 65 20  u use this file 
baf40 61 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a  as an example,.*
baf50 2a 20 61 76 6f 69 64 20 74 68 65 20 75 73 65 20  * avoid the use 
baf60 6f 66 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65  of malloc()/free
baf70 28 29 2e 20 20 54 68 6f 73 65 20 72 6f 75 74 69  ().  Those routi
baf80 6e 65 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20 4f  nes work ok on O
baf90 53 2f 32 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20  S/2.** desktops 
bafa0 62 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20  but not so well 
bafb0 69 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74  in embedded syst
bafc0 65 6d 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d  ems..*/../*.** M
bafd0 61 63 72 6f 73 20 75 73 65 64 20 74 6f 20 64 65  acros used to de
bafe0 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20  termine whether 
baff0 6f 72 20 6e 6f 74 20 74 6f 20 75 73 65 20 74 68  or not to use th
bb000 72 65 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65  reads..*/.#if de
bb010 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 48 52  fined(SQLITE_THR
bb020 45 41 44 53 41 46 45 29 20 26 26 20 53 51 4c 49  EADSAFE) && SQLI
bb030 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23 20  TE_THREADSAFE.# 
bb040 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
bb050 32 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e 64  2_THREADS 1.#end
bb060 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64  if../*.** Includ
bb070 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63  e code that is c
bb080 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f  ommon to all os_
bb090 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a  *.c files.*/./**
bb0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
bb0b0 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68  lude os_common.h
bb0c0 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
bb0d0 66 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a  f os_os2.c *****
bb0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
bb0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
bb100 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f  in file os_commo
bb110 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.h ************
bb120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
bb140 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a  ** 2004 May 22.*
bb150 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
bb160 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
bb170 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
bb180 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
bb190 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
bb1a0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
bb1b0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
bb1c0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
bb1d0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
bb1e0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
bb1f0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
bb200 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
bb210 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
bb220 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
bb230 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
bb240 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
bb250 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
bb260 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
bb270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb2b0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
bb2c0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72  le contains macr
bb2d0 6f 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 20  os and a little 
bb2e0 62 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61 74  bit of code that
bb2f0 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a   is common to.**
bb300 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 74   all of the plat
bb310 66 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66 69  form-specific fi
bb320 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64  les (os_*.c) and
bb330 20 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69 6e   is #included in
bb340 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65  to those.** file
bb350 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  s..**.** This fi
bb360 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e  le should be #in
bb370 63 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f 73  cluded by the os
bb380 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e  _*.c files only.
bb390 20 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a    It is not a.**
bb3a0 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65   general purpose
bb3b0 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2a   header file..**
bb3c0 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 63 6f 6d 6d  .** $Id: os_comm
bb3d0 6f 6e 2e 68 2c 76 20 31 2e 33 38 20 32 30 30 39  on.h,v 1.38 2009
bb3e0 2f 30 32 2f 32 34 20 31 38 3a 34 30 3a 35 30 20  /02/24 18:40:50 
bb3f0 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
bb400 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f 53  $.*/.#ifndef _OS
bb410 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69  _COMMON_H_.#defi
bb420 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f  ne _OS_COMMON_H_
bb430 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74  ../*.** At least
bb440 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20 73   two bugs have s
bb450 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75 73  lipped in becaus
bb460 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 65  e we changed the
bb470 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a 2a   MEMORY_DEBUG.**
bb480 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 45   macro to SQLITE
bb490 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 20  _DEBUG and some 
bb4a0 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 20  older makefiles 
bb4b0 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 64  have not yet mad
bb4c0 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 2e  e the.** switch.
bb4d0 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
bb4e0 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74 63  code should catc
bb4f0 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 61  h this problem a
bb500 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a  t compile-time..
bb510 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 59  */.#ifdef MEMORY
bb520 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20 22  _DEBUG.# error "
bb530 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47  The MEMORY_DEBUG
bb540 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c 65   macro is obsole
bb550 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45 5f  te.  Use SQLITE_
bb560 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22 0a  DEBUG instead.".
bb570 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
bb580 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49  QLITE_DEBUG.SQLI
bb590 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
bb5a0 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d 20  qlite3OSTrace = 
bb5b0 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  0;.#define OSTRA
bb5c0 43 45 31 28 58 29 20 20 20 20 20 20 20 20 20 69  CE1(X)         i
bb5d0 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63  f( sqlite3OSTrac
bb5e0 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  e ) sqlite3Debug
bb5f0 50 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69 6e  Printf(X).#defin
bb600 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 20  e OSTRACE2(X,Y) 
bb610 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
bb620 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74  3OSTrace ) sqlit
bb630 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
bb640 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  Y).#define OSTRA
bb650 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20 69  CE3(X,Y,Z)     i
bb660 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63  f( sqlite3OSTrac
bb670 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  e ) sqlite3Debug
bb680 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 64  Printf(X,Y,Z).#d
bb690 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58  efine OSTRACE4(X
bb6a0 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73 71  ,Y,Z,A)   if( sq
bb6b0 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73  lite3OSTrace ) s
bb6c0 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
bb6d0 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69  f(X,Y,Z,A).#defi
bb6e0 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c  ne OSTRACE5(X,Y,
bb6f0 5a 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69 74  Z,A,B) if( sqlit
bb700 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69  e3OSTrace ) sqli
bb710 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
bb720 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e  ,Y,Z,A,B).#defin
bb730 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a  e OSTRACE6(X,Y,Z
bb740 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69 66  ,A,B,C) \.    if
bb750 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29  (sqlite3OSTrace)
bb760 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
bb770 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29  ntf(X,Y,Z,A,B,C)
bb780 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
bb790 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29  7(X,Y,Z,A,B,C,D)
bb7a0 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65   \.    if(sqlite
bb7b0 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65  3OSTrace) sqlite
bb7c0 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59  3DebugPrintf(X,Y
bb7d0 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c 73  ,Z,A,B,C,D).#els
bb7e0 65 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  e.#define OSTRAC
bb7f0 45 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53  E1(X).#define OS
bb800 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65 66  TRACE2(X,Y).#def
bb810 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59  ine OSTRACE3(X,Y
bb820 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,Z).#define OSTR
bb830 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 64  ACE4(X,Y,Z,A).#d
bb840 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58  efine OSTRACE5(X
bb850 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e  ,Y,Z,A,B).#defin
bb860 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a  e OSTRACE6(X,Y,Z
bb870 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20  ,A,B,C).#define 
bb880 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41  OSTRACE7(X,Y,Z,A
bb890 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a 0a  ,B,C,D).#endif..
bb8a0 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72  /*.** Macros for
bb8b0 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72 61   performance tra
bb8c0 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20  cing.  Normally 
bb8d0 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e 6c  turned off.  Onl
bb8e0 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34  y works.** on i4
bb8f0 38 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f 0a  86 hardware..*/.
bb900 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 45  #ifdef SQLITE_PE
bb910 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 0a  RFORMANCE_TRACE.
bb920 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68  ./* .** hwtime.h
bb930 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65   contains inline
bb940 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 20   assembler code 
bb950 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67  for implementing
bb960 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f 72   .** high-perfor
bb970 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f 75  mance timing rou
bb980 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a  tines..*/./*****
bb990 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
bb9a0 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 68  e hwtime.h in th
bb9b0 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 63  e middle of os_c
bb9c0 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  ommon.h ********
bb9d0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
bb9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
bb9f0 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a  file hwtime.h **
bba00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bba10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bba20 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
bba30 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a  2008 May 27.**.*
bba40 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
bba50 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
bba60 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
bba70 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
bba80 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
bba90 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
bbaa0 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
bbab0 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
bbac0 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
bbad0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
bbae0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
bbaf0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
bbb00 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
bbb10 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
bbb20 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
bbb30 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
bbb40 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
bbb50 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
bbb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbb70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
bbba0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
bbbb0 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20  contains inline 
bbbc0 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 74  asm code for ret
bbbd0 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70 65  rieving "high-pe
bbbe0 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f  rformance".** co
bbbf0 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20 63  unters for x86 c
bbc00 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a  lass CPUs..**.**
bbc10 20 24 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c 76   $Id: hwtime.h,v
bbc20 20 31 2e 33 20 32 30 30 38 2f 30 38 2f 30 31 20   1.3 2008/08/01 
bbc30 31 34 3a 33 33 3a 31 35 20 73 68 61 6e 65 20 45  14:33:15 shane E
bbc40 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  xp $.*/.#ifndef 
bbc50 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69  _HWTIME_H_.#defi
bbc60 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f  ne _HWTIME_H_../
bbc70 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
bbc80 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20  ng routine only 
bbc90 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d  works on pentium
bbca0 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65 72  -class (or newer
bbcb0 29 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a  ) processors..**
bbcc0 20 49 74 20 75 73 65 73 20 74 68 65 20 52 44 54   It uses the RDT
bbcd0 53 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 61  SC opcode to rea
bbce0 64 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75 6e  d the cycle coun
bbcf0 74 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20 74  t value out of t
bbd00 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20  he.** processor 
bbd10 61 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61 74  and returns that
bbd20 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63 61   value.  This ca
bbd30 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 68 69  n be used for hi
bbd40 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c  gh-res.** profil
bbd50 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66  ing..*/.#if (def
bbd60 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c  ined(__GNUC__) |
bbd70 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56  | defined(_MSC_V
bbd80 45 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20 20  ER)) && \.      
bbd90 28 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c  (defined(i386) |
bbda0 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36  | defined(__i386
bbdb0 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  __) || defined(_
bbdc0 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66 20  M_IX86))..  #if 
bbdd0 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f  defined(__GNUC__
bbde0 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20  )..  __inline__ 
bbdf0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71  sqlite_uint64 sq
bbe00 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
bbe10 29 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64  ){.     unsigned
bbe20 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20   int lo, hi;.   
bbe30 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61    __asm__ __vola
bbe40 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20  tile__ ("rdtsc" 
bbe50 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64  : "=a" (lo), "=d
bbe60 22 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72 65  " (hi));.     re
bbe70 74 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e  turn (sqlite_uin
bbe80 74 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c  t64)hi << 32 | l
bbe90 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20  o;.  }..  #elif 
bbea0 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52  defined(_MSC_VER
bbeb0 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63 28  )..  __declspec(
bbec0 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20  naked) __inline 
bbed0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f  sqlite_uint64 __
bbee0 63 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77 74  cdecl sqlite3Hwt
bbef0 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20  ime(void){.     
bbf00 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20 20  __asm {.        
bbf10 72 64 74 73 63 0a 20 20 20 20 20 20 20 20 72 65  rdtsc.        re
bbf20 74 20 20 20 20 20 20 20 3b 20 72 65 74 75 72 6e  t       ; return
bbf30 20 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45 41   value at EDX:EA
bbf40 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  X.     }.  }..  
bbf50 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28 64  #endif..#elif (d
bbf60 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29  efined(__GNUC__)
bbf70 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 38   && defined(__x8
bbf80 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e  6_64__))..  __in
bbf90 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69  line__ sqlite_ui
bbfa0 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69  nt64 sqlite3Hwti
bbfb0 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20  me(void){.      
bbfc0 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61  unsigned long va
bbfd0 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f  l;.      __asm__
bbfe0 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22   __volatile__ ("
bbff0 72 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28 76  rdtsc" : "=A" (v
bc000 61 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75  al));.      retu
bc010 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65  rn val;.  }. .#e
bc020 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47  lif (defined(__G
bc030 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65  NUC__) && define
bc040 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f  d(__ppc__))..  _
bc050 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65  _inline__ sqlite
bc060 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48  _uint64 sqlite3H
bc070 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20  wtime(void){.   
bc080 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67     unsigned long
bc090 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20   long retval;.  
bc0a0 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e      unsigned lon
bc0b0 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f  g junk;.      __
bc0c0 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65  asm__ __volatile
bc0d0 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20  __ ("\n\.       
bc0e0 20 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62 75     1:      mftbu
bc0f0 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20     %1\n\.       
bc100 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 20             mftb 
bc110 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20     %L0\n\.      
bc120 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62              mftb
bc130 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20  u   %0\n\.      
bc140 20 20 20 20 20 20 20 20 20 20 20 20 63 6d 70 77              cmpw
bc150 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20      %0,%1\n\.   
bc160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
bc170 6e 65 20 20 20 20 20 31 62 22 0a 20 20 20 20 20  ne     1b".     
bc180 20 20 20 20 20 20 20 20 20 20 20 20 20 3a 20 22               : "
bc190 3d 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22 3d  =r" (retval), "=
bc1a0 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20  r" (junk));.    
bc1b0 20 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c 3b    return retval;
bc1c0 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23  .  }..#else..  #
bc1d0 65 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65  error Need imple
bc1e0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c  mentation of sql
bc1f0 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72  ite3Hwtime() for
bc200 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a   your platform..
bc210 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f  .  /*.  ** To co
bc220 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69 6d  mpile without im
bc230 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74  plementing sqlit
bc240 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79  e3Hwtime() for y
bc250 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20  our platform,.  
bc260 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76  ** you can remov
bc270 65 20 74 68 65 20 61 62 6f 76 65 20 23 65 72 72  e the above #err
bc280 6f 72 20 61 6e 64 20 75 73 65 20 74 68 65 20 66  or and use the f
bc290 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74  ollowing.  ** st
bc2a0 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f  ub function.  Yo
bc2b0 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69  u will lose timi
bc2c0 6e 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6d  ng support for m
bc2d0 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  any.  ** of the 
bc2e0 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 65  debugging and te
bc2f0 73 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73 2c  sting utilities,
bc300 20 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20 61   but it should a
bc310 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d  t.  ** least com
bc320 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20  pile and run..  
bc330 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
bc340 45 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  E   sqlite_uint6
bc350 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  4 sqlite3Hwtime(
bc360 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28 28  void){ return ((
bc370 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29  sqlite_uint64)0)
bc380 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e  ; }..#endif..#en
bc390 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
bc3a0 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a  _HWTIME_H_) */..
bc3b0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
bc3c0 45 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68 20  End of hwtime.h 
bc3d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc3e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc3f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
bc400 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
bc410 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
bc420 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
bc430 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a  os_common.h ****
bc440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
bc450 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75  .static sqlite_u
bc460 69 6e 74 36 34 20 67 5f 73 74 61 72 74 3b 0a 73  int64 g_start;.s
bc470 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e  tatic sqlite_uin
bc480 74 36 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a 23  t64 g_elapsed;.#
bc490 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41  define TIMER_STA
bc4a0 52 54 20 20 20 20 20 20 20 67 5f 73 74 61 72 74  RT       g_start
bc4b0 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29  =sqlite3Hwtime()
bc4c0 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45  .#define TIMER_E
bc4d0 4e 44 20 20 20 20 20 20 20 20 20 67 5f 65 6c 61  ND         g_ela
bc4e0 70 73 65 64 3d 73 71 6c 69 74 65 33 48 77 74 69  psed=sqlite3Hwti
bc4f0 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64 65  me()-g_start.#de
bc500 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53  fine TIMER_ELAPS
bc510 45 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64  ED     g_elapsed
bc520 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 54  .#else.#define T
bc530 49 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66 69  IMER_START.#defi
bc540 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64 65  ne TIMER_END.#de
bc550 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53  fine TIMER_ELAPS
bc560 45 44 20 20 20 20 20 28 28 73 71 6c 69 74 65 5f  ED     ((sqlite_
bc570 75 69 6e 74 36 34 29 30 29 0a 23 65 6e 64 69 66  uint64)0).#endif
bc580 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f  ../*.** If we co
bc590 6d 70 69 6c 65 20 77 69 74 68 20 74 68 65 20 53  mpile with the S
bc5a0 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72 6f  QLITE_TEST macro
bc5b0 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 66   set, then the f
bc5c0 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a  ollowing block.*
bc5d0 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 67  * of code will g
bc5e0 69 76 65 20 75 73 20 74 68 65 20 61 62 69 6c 69  ive us the abili
bc5f0 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61  ty to simulate a
bc600 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 2e   disk I/O error.
bc610 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 65    This.** is use
bc620 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74 68  d for testing th
bc630 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20 6c  e I/O recovery l
bc640 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  ogic..*/.#ifdef 
bc650 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49  SQLITE_TEST.SQLI
bc660 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
bc670 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20  e3_io_error_hit 
bc680 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
bc690 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20  /* Total number 
bc6a0 6f 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a 2f  of I/O Errors */
bc6b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
bc6c0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
bc6d0 5f 68 61 72 64 68 69 74 20 3d 20 30 3b 20 20 20  _hardhit = 0;   
bc6e0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
bc6f0 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72  f non-benign err
bc700 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ors */.SQLITE_AP
bc710 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f  I int sqlite3_io
bc720 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d  _error_pending =
bc730 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f   0;        /* Co
bc740 75 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 72 73  unt down to firs
bc750 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 53  t I/O error */.S
bc760 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
bc770 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
bc780 65 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20 20  ersist = 0;     
bc790 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49 2f     /* True if I/
bc7a0 4f 20 65 72 72 6f 72 73 20 70 65 72 73 69 73 74  O errors persist
bc7b0 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
bc7c0 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  nt sqlite3_io_er
bc7d0 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20  ror_benign = 0; 
bc7e0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
bc7f0 69 66 20 65 72 72 6f 72 73 20 61 72 65 20 62 65  if errors are be
bc800 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41  nign */.SQLITE_A
bc810 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64  PI int sqlite3_d
bc820 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20  iskfull_pending 
bc830 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  = 0;.SQLITE_API 
bc840 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b  int sqlite3_disk
bc850 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e  full = 0;.#defin
bc860 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f  e SimulateIOErro
bc870 72 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69 74  rBenign(X) sqlit
bc880 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69  e3_io_error_beni
bc890 67 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 20 53  gn=(X).#define S
bc8a0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 43  imulateIOError(C
bc8b0 4f 44 45 29 20 20 5c 0a 20 20 69 66 28 20 28 73  ODE)  \.  if( (s
bc8c0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
bc8d0 70 65 72 73 69 73 74 20 26 26 20 73 71 6c 69 74  persist && sqlit
bc8e0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29  e3_io_error_hit)
bc8f0 20 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c   \.       || sql
bc900 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65  ite3_io_error_pe
bc910 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 20  nding-- == 1 )  
bc920 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
bc930 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b  { local_ioerr();
bc940 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63 20   CODE; }.static 
bc950 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 72  void local_ioerr
bc960 28 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 28 22  (){.  IOTRACE(("
bc970 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73 71  IOERR\n"));.  sq
bc980 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68  lite3_io_error_h
bc990 69 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71 6c  it++;.  if( !sql
bc9a0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65  ite3_io_error_be
bc9b0 6e 69 67 6e 20 29 20 73 71 6c 69 74 65 33 5f 69  nign ) sqlite3_i
bc9c0 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 2b  o_error_hardhit+
bc9d0 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69 6d  +;.}.#define Sim
bc9e0 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72  ulateDiskfullErr
bc9f0 6f 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 69 66  or(CODE) \.   if
bca00 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75  ( sqlite3_diskfu
bca10 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0a  ll_pending ){ \.
bca20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
bca30 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e  _diskfull_pendin
bca40 67 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20 20  g == 1 ){ \.    
bca50 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29     local_ioerr()
bca60 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74  ; \.       sqlit
bca70 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31 3b  e3_diskfull = 1;
bca80 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65   \.       sqlite
bca90 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d  3_io_error_hit =
bcaa0 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f 44   1; \.       COD
bcab0 45 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65 7b  E; \.     }else{
bcac0 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65   \.       sqlite
bcad0 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69  3_diskfull_pendi
bcae0 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 5c  ng--; \.     } \
bcaf0 0a 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66  .   }.#else.#def
bcb00 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ine SimulateIOEr
bcb10 72 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 64 65  rorBenign(X).#de
bcb20 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45  fine SimulateIOE
bcb30 72 72 6f 72 28 41 29 0a 23 64 65 66 69 6e 65 20  rror(A).#define 
bcb40 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c  SimulateDiskfull
bcb50 45 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 66 0a  Error(A).#endif.
bcb60 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74  ./*.** When test
bcb70 69 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e  ing, keep a coun
bcb80 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20  t of the number 
bcb90 6f 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a 2a  of open files..*
bcba0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
bcbb0 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20  TEST.SQLITE_API 
bcbc0 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  int sqlite3_open
bcbd0 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b  _file_count = 0;
bcbe0 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75  .#define OpenCou
bcbf0 6e 74 65 72 28 58 29 20 20 73 71 6c 69 74 65 33  nter(X)  sqlite3
bcc00 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74  _open_file_count
bcc10 2b 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 66  +=(X).#else.#def
bcc20 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28  ine OpenCounter(
bcc30 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69  X).#endif..#endi
bcc40 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 4f  f /* !defined(_O
bcc50 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a  S_COMMON_H_) */.
bcc60 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
bcc70 20 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f   End of os_commo
bcc80 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.h ************
bcc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
bccb0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
bccc0 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
bccd0 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
bcce0 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a   os_os2.c ******
bccf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
bcd00 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f 73 32 46  ../*.** The os2F
bcd10 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 73  ile structure is
bcd20 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c   subclass of sql
bcd30 69 74 65 33 5f 66 69 6c 65 20 73 70 65 63 69 66  ite3_file specif
bcd40 69 63 20 66 6f 72 20 74 68 65 20 4f 53 2f 32 0a  ic for the OS/2.
bcd50 2a 2a 20 70 72 6f 74 61 62 69 6c 69 74 79 20 6c  ** protability l
bcd60 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ayer..*/.typedef
bcd70 20 73 74 72 75 63 74 20 6f 73 32 46 69 6c 65 20   struct os2File 
bcd80 6f 73 32 46 69 6c 65 3b 0a 73 74 72 75 63 74 20  os2File;.struct 
bcd90 6f 73 32 46 69 6c 65 20 7b 0a 20 20 63 6f 6e 73  os2File {.  cons
bcda0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
bcdb0 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20  hods *pMethod;  
bcdc0 2f 2a 20 41 6c 77 61 79 73 20 74 68 65 20 66 69  /* Always the fi
bcdd0 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 48  rst entry */.  H
bcde0 46 49 4c 45 20 68 3b 20 20 20 20 20 20 20 20 20  FILE h;         
bcdf0 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64           /* Hand
bce00 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67  le for accessing
bce10 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63   the file */.  c
bce20 68 61 72 2a 20 70 61 74 68 54 6f 44 65 6c 3b 20  har* pathToDel; 
bce30 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
bce40 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65   of file to dele
bce50 74 65 20 6f 6e 20 63 6c 6f 73 65 2c 20 4e 55 4c  te on close, NUL
bce60 4c 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 75 6e  L if not */.  un
bce70 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b  signed char lock
bce80 74 79 70 65 3b 20 20 20 2f 2a 20 54 79 70 65 20  type;   /* Type 
bce90 6f 66 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c  of lock currentl
bcea0 79 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66  y held on this f
bceb0 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69  ile */.};..#defi
bcec0 6e 65 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 20  ne LOCK_TIMEOUT 
bced0 31 30 4c 20 2f 2a 20 74 68 65 20 64 65 66 61 75  10L /* the defau
bcee0 6c 74 20 6c 6f 63 6b 69 6e 67 20 74 69 6d 65 6f  lt locking timeo
bcef0 75 74 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ut */../********
bcf00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcf10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcf20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcf30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcf40 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78  *****.** The nex
bcf50 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69  t group of routi
bcf60 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68  nes implement th
bcf70 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 73 70  e I/O methods sp
bcf80 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68  ecified.** by th
bcf90 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  e sqlite3_io_met
bcfa0 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 2a  hods object..***
bcfb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcfc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcfd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcfe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
bd000 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65  .** Close a file
bd010 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
bd020 6f 73 32 43 6c 6f 73 65 28 20 73 71 6c 69 74 65  os2Close( sqlite
bd030 33 5f 66 69 6c 65 20 2a 69 64 20 29 7b 0a 20 20  3_file *id ){.  
bd040 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45  APIRET rc = NO_E
bd050 52 52 4f 52 3b 0a 20 20 6f 73 32 46 69 6c 65 20  RROR;.  os2File 
bd060 2a 70 46 69 6c 65 3b 0a 20 20 69 66 28 20 69 64  *pFile;.  if( id
bd070 20 26 26 20 28 70 46 69 6c 65 20 3d 20 28 6f 73   && (pFile = (os
bd080 32 46 69 6c 65 2a 29 69 64 29 20 21 3d 20 30 20  2File*)id) != 0 
bd090 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28  ){.    OSTRACE2(
bd0a0 20 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 70   "CLOSE %d\n", p
bd0b0 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 72  File->h );.    r
bd0c0 63 20 3d 20 44 6f 73 43 6c 6f 73 65 28 20 70 46  c = DosClose( pF
bd0d0 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 70 46  ile->h );.    pF
bd0e0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
bd0f0 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 69 66 28  NO_LOCK;.    if(
bd100 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65   pFile->pathToDe
bd110 6c 20 21 3d 20 4e 55 4c 4c 20 29 7b 0a 20 20 20  l != NULL ){.   
bd120 20 20 20 72 63 20 3d 20 44 6f 73 46 6f 72 63 65     rc = DosForce
bd130 44 65 6c 65 74 65 28 20 28 50 53 5a 29 70 46 69  Delete( (PSZ)pFi
bd140 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b  le->pathToDel );
bd150 0a 20 20 20 20 20 20 66 72 65 65 28 20 70 46 69  .      free( pFi
bd160 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b  le->pathToDel );
bd170 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 61  .      pFile->pa
bd180 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a  thToDel = NULL;.
bd190 20 20 20 20 7d 0a 20 20 20 20 69 64 20 3d 20 30      }.    id = 0
bd1a0 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65  ;.    OpenCounte
bd1b0 72 28 20 2d 31 20 29 3b 0a 20 20 7d 0a 0a 20 20  r( -1 );.  }..  
bd1c0 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f  return rc == NO_
bd1d0 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f  ERROR ? SQLITE_O
bd1e0 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  K : SQLITE_IOERR
bd1f0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  ;.}../*.** Read 
bd200 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65  data from a file
bd210 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20   into a buffer. 
bd220 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
bd230 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65  K if all.** byte
bd240 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63 63  s were read succ
bd250 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c  essfully and SQL
bd260 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79  ITE_IOERR if any
bd270 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72  thing goes.** wr
bd280 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ong..*/.static i
bd290 6e 74 20 6f 73 32 52 65 61 64 28 0a 20 20 73 71  nt os2Read(.  sq
bd2a0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
bd2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
bd2c0 20 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72   File to read fr
bd2d0 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42  om */.  void *pB
bd2e0 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  uf,             
bd2f0 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
bd300 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68   content into th
bd310 69 73 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69  is buffer */.  i
bd320 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20  nt amt,         
bd330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
bd340 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
bd350 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73  s to read */.  s
bd360 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66  qlite3_int64 off
bd370 73 65 74 20 20 20 20 20 20 20 20 20 20 20 20 2f  set            /
bd380 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20  * Begin reading 
bd390 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a  at this offset *
bd3a0 2f 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c  /.){.  ULONG fil
bd3b0 65 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a  eLocation = 0L;.
bd3c0 20 20 55 4c 4f 4e 47 20 67 6f 74 3b 0a 20 20 6f    ULONG got;.  o
bd3d0 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  s2File *pFile = 
bd3e0 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20  (os2File*)id;.  
bd3f0 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b  assert( id!=0 );
bd400 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
bd410 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54  or( return SQLIT
bd420 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 29 3b 0a  E_IOERR_READ );.
bd430 20 20 4f 53 54 52 41 43 45 33 28 20 22 52 45 41    OSTRACE3( "REA
bd440 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c  D %d lock=%d\n",
bd450 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65   pFile->h, pFile
bd460 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20  ->locktype );.  
bd470 69 66 28 20 44 6f 73 53 65 74 46 69 6c 65 50 74  if( DosSetFilePt
bd480 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 73  r(pFile->h, offs
bd490 65 74 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c 20  et, FILE_BEGIN, 
bd4a0 26 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 21  &fileLocation) !
bd4b0 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
bd4c0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
bd4d0 49 4f 45 52 52 3b 0a 20 20 7d 0a 20 20 69 66 28  IOERR;.  }.  if(
bd4e0 20 44 6f 73 52 65 61 64 28 20 70 46 69 6c 65 2d   DosRead( pFile-
bd4f0 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 26  >h, pBuf, amt, &
bd500 67 6f 74 20 29 20 21 3d 20 4e 4f 5f 45 52 52 4f  got ) != NO_ERRO
bd510 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  R ){.    return 
bd520 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41  SQLITE_IOERR_REA
bd530 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 6f 74  D;.  }.  if( got
bd540 20 3d 3d 20 28 55 4c 4f 4e 47 29 61 6d 74 20 29   == (ULONG)amt )
bd550 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
bd560 54 45 5f 4f 4b 3b 0a 20 20 65 6c 73 65 20 7b 0a  TE_OK;.  else {.
bd570 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 70 6f      /* Unread po
bd580 72 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 69 6e  rtions of the in
bd590 70 75 74 20 62 75 66 66 65 72 20 6d 75 73 74 20  put buffer must 
bd5a0 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a  be zero-filled *
bd5b0 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28  /.    memset(&((
bd5c0 63 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d  char*)pBuf)[got]
bd5d0 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20  , 0, amt-got);. 
bd5e0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
bd5f0 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41  _IOERR_SHORT_REA
bd600 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  D;.  }.}../*.** 
bd610 57 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d 20  Write data from 
bd620 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61 20  a buffer into a 
bd630 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51  file.  Return SQ
bd640 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
bd650 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74  ss.** or some ot
bd660 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 6f  her error code o
bd670 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74  n failure..*/.st
bd680 61 74 69 63 20 69 6e 74 20 6f 73 32 57 72 69 74  atic int os2Writ
bd690 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  e(.  sqlite3_fil
bd6a0 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20  e *id,          
bd6b0 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20       /* File to 
bd6c0 77 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0a 20 20  write into */.  
bd6d0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66  const void *pBuf
bd6e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
bd6f0 2f 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f 20  /* The bytes to 
bd700 62 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20  be written */.  
bd710 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20  int amt,        
bd720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bd730 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
bd740 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20  es to write */. 
bd750 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f   sqlite3_int64 o
bd760 66 66 73 65 74 20 20 20 20 20 20 20 20 20 20 20  ffset           
bd770 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20   /* Offset into 
bd780 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 67 69  the file to begi
bd790 6e 20 77 72 69 74 69 6e 67 20 61 74 20 2a 2f 0a  n writing at */.
bd7a0 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65 4c  ){.  ULONG fileL
bd7b0 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20 20  ocation = 0L;.  
bd7c0 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45  APIRET rc = NO_E
bd7d0 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 77 72  RROR;.  ULONG wr
bd7e0 6f 74 65 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a  ote;.  os2File *
bd7f0 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65  pFile = (os2File
bd800 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20  *)id;.  assert( 
bd810 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c  id!=0 );.  Simul
bd820 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75  ateIOError( retu
bd830 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
bd840 57 52 49 54 45 20 29 3b 0a 20 20 53 69 6d 75 6c  WRITE );.  Simul
bd850 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72  ateDiskfullError
bd860 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ( return SQLITE_
bd870 46 55 4c 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43  FULL );.  OSTRAC
bd880 45 33 28 20 22 57 52 49 54 45 20 25 64 20 6c 6f  E3( "WRITE %d lo
bd890 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  ck=%d\n", pFile-
bd8a0 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  >h, pFile->lockt
bd8b0 79 70 65 20 29 3b 0a 20 20 69 66 28 20 44 6f 73  ype );.  if( Dos
bd8c0 53 65 74 46 69 6c 65 50 74 72 28 70 46 69 6c 65  SetFilePtr(pFile
bd8d0 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 46 49 4c  ->h, offset, FIL
bd8e0 45 5f 42 45 47 49 4e 2c 20 26 66 69 6c 65 4c 6f  E_BEGIN, &fileLo
bd8f0 63 61 74 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45 52  cation) != NO_ER
bd900 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ROR ){.    retur
bd910 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a  n SQLITE_IOERR;.
bd920 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61 6d    }.  assert( am
bd930 74 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 20  t>0 );.  while( 
bd940 61 6d 74 20 3e 20 30 20 26 26 0a 20 20 20 20 20  amt > 0 &&.     
bd950 20 20 20 20 28 20 72 63 20 3d 20 44 6f 73 57 72      ( rc = DosWr
bd960 69 74 65 28 20 70 46 69 6c 65 2d 3e 68 2c 20 28  ite( pFile->h, (
bd970 50 56 4f 49 44 29 70 42 75 66 2c 20 61 6d 74 2c  PVOID)pBuf, amt,
bd980 20 26 77 72 6f 74 65 20 29 20 29 20 3d 3d 20 4e   &wrote ) ) == N
bd990 4f 5f 45 52 52 4f 52 20 26 26 0a 20 20 20 20 20  O_ERROR &&.     
bd9a0 20 20 20 20 77 72 6f 74 65 20 3e 20 30 0a 20 20      wrote > 0.  
bd9b0 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77 72  ){.    amt -= wr
bd9c0 6f 74 65 3b 0a 20 20 20 20 70 42 75 66 20 3d 20  ote;.    pBuf = 
bd9d0 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b 77  &((char*)pBuf)[w
bd9e0 72 6f 74 65 5d 3b 0a 20 20 7d 0a 0a 20 20 72 65  rote];.  }..  re
bd9f0 74 75 72 6e 20 28 20 72 63 20 21 3d 20 4e 4f 5f  turn ( rc != NO_
bda00 45 52 52 4f 52 20 7c 7c 20 61 6d 74 20 3e 20 28  ERROR || amt > (
bda10 69 6e 74 29 77 72 6f 74 65 20 29 20 3f 20 53 51  int)wrote ) ? SQ
bda20 4c 49 54 45 5f 46 55 4c 4c 20 3a 20 53 51 4c 49  LITE_FULL : SQLI
bda30 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
bda40 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e  Truncate an open
bda50 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69   file to a speci
bda60 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61  fied size.*/.sta
bda70 74 69 63 20 69 6e 74 20 6f 73 32 54 72 75 6e 63  tic int os2Trunc
bda80 61 74 65 28 20 73 71 6c 69 74 65 33 5f 66 69 6c  ate( sqlite3_fil
bda90 65 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 65  e *id, i64 nByte
bdaa0 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20   ){.  APIRET rc 
bdab0 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f 73  = NO_ERROR;.  os
bdac0 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  2File *pFile = (
bdad0 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 4f  os2File*)id;.  O
bdae0 53 54 52 41 43 45 33 28 20 22 54 52 55 4e 43 41  STRACE3( "TRUNCA
bdaf0 54 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70  TE %d %lld\n", p
bdb00 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29  File->h, nByte )
bdb10 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
bdb20 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49  ror( return SQLI
bdb30 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54  TE_IOERR_TRUNCAT
bdb40 45 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 53  E );.  rc = DosS
bdb50 65 74 46 69 6c 65 53 69 7a 65 28 20 70 46 69 6c  etFileSize( pFil
bdb60 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0a 20  e->h, nByte );. 
bdb70 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f   return rc == NO
bdb80 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f  _ERROR ? SQLITE_
bdb90 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52  OK : SQLITE_IOER
bdba0 52 5f 54 52 55 4e 43 41 54 45 3b 0a 7d 0a 0a 23  R_TRUNCATE;.}..#
bdbb0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
bdbc0 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68  T./*.** Count th
bdbd0 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c  e number of full
bdbe0 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c  syncs and normal
bdbf0 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69 73   syncs.  This is
bdc00 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a   used to test.**
bdc10 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64 20   that syncs and 
bdc20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f 63  fullsyncs are oc
bdc30 63 75 72 69 6e 67 20 61 74 20 74 68 65 20 72 69  curing at the ri
bdc40 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51  ght times..*/.SQ
bdc50 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
bdc60 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20  ite3_sync_count 
bdc70 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  = 0;.SQLITE_API 
bdc80 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c  int sqlite3_full
bdc90 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a  sync_count = 0;.
bdca0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61  #endif../*.** Ma
bdcb0 6b 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69 74  ke sure all writ
bdcc0 65 73 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c  es to a particul
bdcd0 61 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d  ar file are comm
bdce0 69 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a  itted to disk..*
bdcf0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  /.static int os2
bdd00 53 79 6e 63 28 20 73 71 6c 69 74 65 33 5f 66 69  Sync( sqlite3_fi
bdd10 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67  le *id, int flag
bdd20 73 20 29 7b 0a 20 20 6f 73 32 46 69 6c 65 20 2a  s ){.  os2File *
bdd30 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65  pFile = (os2File
bdd40 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 33  *)id;.  OSTRACE3
bdd50 28 20 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3d  ( "SYNC %d lock=
bdd60 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
bdd70 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
bdd80 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54   );.#ifdef SQLIT
bdd90 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 6c 61  E_TEST.  if( fla
bdda0 67 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e 43  gs & SQLITE_SYNC
bddb0 5f 46 55 4c 4c 29 7b 0a 20 20 20 20 73 71 6c 69  _FULL){.    sqli
bddc0 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75  te3_fullsync_cou
bddd0 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  nt++;.  }.  sqli
bdde0 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b  te3_sync_count++
bddf0 3b 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49 66  ;.#endif.  /* If
bde00 20 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74   we compiled wit
bde10 68 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f  h the SQLITE_NO_
bde20 53 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20  SYNC flag, then 
bde30 73 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 2a  syncing is a.  *
bde40 2a 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66  * no-op.  */.#if
bde50 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59  def SQLITE_NO_SY
bde60 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  NC.  UNUSED_PARA
bde70 4d 45 54 45 52 28 70 46 69 6c 65 29 3b 0a 20 20  METER(pFile);.  
bde80 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
bde90 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72 6e  ;.#else.  return
bdea0 20 44 6f 73 52 65 73 65 74 42 75 66 66 65 72 28   DosResetBuffer(
bdeb0 20 70 46 69 6c 65 2d 3e 68 20 29 20 3d 3d 20 4e   pFile->h ) == N
bdec0 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45  O_ERROR ? SQLITE
bded0 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45  _OK : SQLITE_IOE
bdee0 52 52 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a  RR;.#endif.}../*
bdef0 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68  .** Determine th
bdf00 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f  e current size o
bdf10 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65  f a file in byte
bdf20 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  s.*/.static int 
bdf30 6f 73 32 46 69 6c 65 53 69 7a 65 28 20 73 71 6c  os2FileSize( sql
bdf40 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73  ite3_file *id, s
bdf50 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53  qlite3_int64 *pS
bdf60 69 7a 65 20 29 7b 0a 20 20 41 50 49 52 45 54 20  ize ){.  APIRET 
bdf70 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20  rc = NO_ERROR;. 
bdf80 20 46 49 4c 45 53 54 41 54 55 53 33 20 66 73 74   FILESTATUS3 fst
bdf90 73 33 46 69 6c 65 49 6e 66 6f 3b 0a 20 20 6d 65  s3FileInfo;.  me
bdfa0 6d 73 65 74 28 26 66 73 74 73 33 46 69 6c 65 49  mset(&fsts3FileI
bdfb0 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66  nfo, 0, sizeof(f
bdfc0 73 74 73 33 46 69 6c 65 49 6e 66 6f 29 29 3b 0a  sts3FileInfo));.
bdfd0 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20    assert( id!=0 
bdfe0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
bdff0 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c  rror( return SQL
be000 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 20  ITE_IOERR_FSTAT 
be010 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 65  );.  rc = DosQue
be020 72 79 46 69 6c 65 49 6e 66 6f 28 20 28 28 6f 73  ryFileInfo( ((os
be030 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 46  2File*)id)->h, F
be040 49 4c 5f 53 54 41 4e 44 41 52 44 2c 20 26 66 73  IL_STANDARD, &fs
be050 74 73 33 46 69 6c 65 49 6e 66 6f 2c 20 73 69 7a  ts3FileInfo, siz
be060 65 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33 29  eof(FILESTATUS3)
be070 20 29 3b 0a 20 20 69 66 28 20 72 63 20 3d 3d 20   );.  if( rc == 
be080 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
be090 2a 70 53 69 7a 65 20 3d 20 66 73 74 73 33 46 69  *pSize = fsts3Fi
be0a0 6c 65 49 6e 66 6f 2e 63 62 46 69 6c 65 3b 0a 20  leInfo.cbFile;. 
be0b0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
be0c0 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  _OK;.  }else{.  
be0d0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
be0e0 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 7d  IOERR_FSTAT;.  }
be0f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72  .}../*.** Acquir
be100 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e  e a reader lock.
be110 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67  .*/.static int g
be120 65 74 52 65 61 64 4c 6f 63 6b 28 20 6f 73 32 46  etReadLock( os2F
be130 69 6c 65 20 2a 70 46 69 6c 65 20 29 7b 0a 20 20  ile *pFile ){.  
be140 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72  FILELOCK  LockAr
be150 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ea,.            
be160 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 41 50  UnlockArea;.  AP
be170 49 52 45 54 20 72 65 73 3b 0a 20 20 6d 65 6d 73  IRET res;.  mems
be180 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c  et(&LockArea, 0,
be190 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61   sizeof(LockArea
be1a0 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e  ));.  memset(&Un
be1b0 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a  lockArea, 0, siz
be1c0 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29  eof(UnlockArea))
be1d0 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66  ;.  LockArea.lOf
be1e0 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49  fset = SHARED_FI
be1f0 52 53 54 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e  RST;.  LockArea.
be200 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f  lRange = SHARED_
be210 53 49 5a 45 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72  SIZE;.  UnlockAr
be220 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b  ea.lOffset = 0L;
be230 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52  .  UnlockArea.lR
be240 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 72 65 73  ange = 0L;.  res
be250 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63   = DosSetFileLoc
be260 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55  ks( pFile->h, &U
be270 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b  nlockArea, &Lock
be280 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f  Area, LOCK_TIMEO
be290 55 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53 54 52  UT, 1L );.  OSTR
be2a0 41 43 45 33 28 20 22 47 45 54 52 45 41 44 4c 4f  ACE3( "GETREADLO
be2b0 43 4b 20 25 64 20 72 65 73 3d 25 64 5c 6e 22 2c  CK %d res=%d\n",
be2c0 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29   pFile->h, res )
be2d0 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a  ;.  return res;.
be2e0 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 20  }../*.** Undo a 
be2f0 72 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74  readlock.*/.stat
be300 69 63 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 61  ic int unlockRea
be310 64 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 20 2a  dLock( os2File *
be320 69 64 20 29 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b  id ){.  FILELOCK
be330 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20    LockArea,.    
be340 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72          UnlockAr
be350 65 61 3b 0a 20 20 41 50 49 52 45 54 20 72 65 73  ea;.  APIRET res
be360 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b  ;.  memset(&Lock
be370 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Area, 0, sizeof(
be380 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65  LockArea));.  me
be390 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61  mset(&UnlockArea
be3a0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f  , 0, sizeof(Unlo
be3b0 63 6b 41 72 65 61 29 29 3b 0a 20 20 4c 6f 63 6b  ckArea));.  Lock
be3c0 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30  Area.lOffset = 0
be3d0 4c 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52  L;.  LockArea.lR
be3e0 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 55 6e 6c  ange = 0L;.  Unl
be3f0 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
be400 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a  = SHARED_FIRST;.
be410 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61    UnlockArea.lRa
be420 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a  nge = SHARED_SIZ
be430 45 3b 0a 20 20 72 65 73 20 3d 20 44 6f 73 53 65  E;.  res = DosSe
be440 74 46 69 6c 65 4c 6f 63 6b 73 28 20 69 64 2d 3e  tFileLocks( id->
be450 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20  h, &UnlockArea, 
be460 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f  &LockArea, LOCK_
be470 54 49 4d 45 4f 55 54 2c 20 31 4c 20 29 3b 0a 20  TIMEOUT, 1L );. 
be480 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f   OSTRACE3( "UNLO
be490 43 4b 2d 52 45 41 44 4c 4f 43 4b 20 66 69 6c 65  CK-READLOCK file
be4a0 20 68 61 6e 64 6c 65 3d 25 64 20 72 65 73 3d 25   handle=%d res=%
be4b0 64 3f 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 72 65  d?\n", id->h, re
be4c0 73 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65  s );.  return re
be4d0 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b  s;.}../*.** Lock
be4e0 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74   the file with t
be4f0 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65  he lock specifie
be500 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c  d by parameter l
be510 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a  ocktype - one.**
be520 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
be530 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29  g:.**.**     (1)
be540 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20   SHARED_LOCK.** 
be550 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44      (2) RESERVED
be560 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29  _LOCK.**     (3)
be570 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a   PENDING_LOCK.**
be580 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49       (4) EXCLUSI
be590 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f  VE_LOCK.**.** So
be5a0 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71  metimes when req
be5b0 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b  uesting one lock
be5c0 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e   state, addition
be5d0 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a  al lock states.*
be5e0 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69  * are inserted i
be5f0 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20  n between.  The 
be600 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61  locking might fa
be610 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65  il on one of the
be620 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69   later.** transi
be630 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68  tions leaving th
be640 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66  e lock state dif
be650 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74  ferent from what
be660 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a   it started but.
be670 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f  ** still short o
be680 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65  f its goal.  The
be690 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74   following chart
be6a0 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77   shows the allow
be6b0 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e  ed.** transition
be6c0 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74  s and the insert
be6d0 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  ed intermediate 
be6e0 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  states:.**.**   
be6f0 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41   UNLOCKED -> SHA
be700 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44  RED.**    SHARED
be710 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20   -> RESERVED.** 
be720 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45     SHARED -> (PE
be730 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53  NDING) -> EXCLUS
be740 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56  IVE.**    RESERV
be750 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20  ED -> (PENDING) 
be760 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20  -> EXCLUSIVE.** 
be770 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58     PENDING -> EX
be780 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68  CLUSIVE.**.** Th
be790 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
be7a0 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20  only increase a 
be7b0 6c 6f 63 6b 2e 20 20 54 68 65 20 6f 73 32 55 6e  lock.  The os2Un
be7c0 6c 6f 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a  lock() routine.*
be7d0 2a 20 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63  * erases all loc
be7e0 6b 73 20 61 74 20 6f 6e 63 65 20 61 6e 64 20 72  ks at once and r
be7f0 65 74 75 72 6e 73 20 75 73 20 69 6d 6d 65 64 69  eturns us immedi
be800 61 74 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67  ately to locking
be810 20 6c 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20   level 0..** It 
be820 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
be830 74 6f 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63  to lower the loc
be840 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73  king level one s
be850 74 65 70 20 61 74 20 61 20 74 69 6d 65 2e 20 20  tep at a time.  
be860 59 6f 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73  You.** must go s
be870 74 72 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69  traight to locki
be880 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73  ng level 0..*/.s
be890 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4c 6f 63  tatic int os2Loc
be8a0 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  k( sqlite3_file 
be8b0 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70  *id, int locktyp
be8c0 65 20 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  e ){.  int rc = 
be8d0 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
be8e0 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
be8f0 66 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e 65 73  from subroutines
be900 20 2a 2f 0a 20 20 41 50 49 52 45 54 20 72 65 73   */.  APIRET res
be910 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 20 20 20 20   = NO_ERROR;    
be920 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 6e 20  /* Result of an 
be930 4f 53 2f 32 20 6c 6f 63 6b 20 63 61 6c 6c 20 2a  OS/2 lock call *
be940 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b 74  /.  int newLockt
be950 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 53 65  ype;       /* Se
be960 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  t pFile->locktyp
be970 65 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65 20  e to this value 
be980 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 2a  before exiting *
be990 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e 64 69  /.  int gotPendi
be9a0 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 72  ngLock = 0;/* Tr
be9b0 75 65 20 69 66 20 77 65 20 61 63 71 75 69 72 65  ue if we acquire
be9c0 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b  d a PENDING lock
be9d0 20 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20 20   this time */.  
be9e0 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72  FILELOCK  LockAr
be9f0 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ea,.            
bea00 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 6f 73  UnlockArea;.  os
bea10 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  2File *pFile = (
bea20 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 6d  os2File*)id;.  m
bea30 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c  emset(&LockArea,
bea40 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41   0, sizeof(LockA
bea50 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28  rea));.  memset(
bea60 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20  &UnlockArea, 0, 
bea70 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65  sizeof(UnlockAre
bea80 61 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  a));.  assert( p
bea90 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 4f 53 54  File!=0 );.  OST
beaa0 52 41 43 45 34 28 20 22 4c 4f 43 4b 20 25 64 20  RACE4( "LOCK %d 
beab0 25 64 20 77 61 73 20 25 64 5c 6e 22 2c 20 70 46  %d was %d\n", pF
beac0 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65  ile->h, locktype
bead0 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  , pFile->locktyp
beae0 65 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  e );..  /* If th
beaf0 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61  ere is already a
beb00 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79   lock of this ty
beb10 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72  pe or more restr
beb20 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20  ictive on the.  
beb30 2a 2a 20 6f 73 32 46 69 6c 65 2c 20 64 6f 20 6e  ** os2File, do n
beb40 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73  othing. Don't us
beb50 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20  e the end_lock: 
beb60 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20  exit path, as.  
beb70 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ** sqlite3_mutex
beb80 5f 65 6e 74 65 72 28 29 20 68 61 73 6e 27 74 20  _enter() hasn't 
beb90 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e  been called yet.
beba0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c  .  */.  if( pFil
bebb0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63  e->locktype>=loc
bebc0 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54  ktype ){.    OST
bebd0 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20  RACE3( "LOCK %d 
bebe0 25 64 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 68  %d ok (already h
bebf0 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  eld)\n", pFile->
bec00 68 2c 20 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20  h, locktype );. 
bec10 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
bec20 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d  _OK;.  }..  /* M
bec30 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63  ake sure the loc
bec40 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73  king sequence is
bec50 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20   correct.  */.  
bec60 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c  assert( pFile->l
bec70 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b  ocktype!=NO_LOCK
bec80 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48   || locktype==SH
bec90 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61  ARED_LOCK );.  a
beca0 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21  ssert( locktype!
becb0 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b  =PENDING_LOCK );
becc0 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
becd0 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f  ype!=RESERVED_LO
bece0 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63  CK || pFile->loc
becf0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
bed00 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b  CK );..  /* Lock
bed10 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43   the PENDING_LOC
bed20 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e 65 65  K byte if we nee
bed30 64 20 74 6f 20 61 63 71 75 69 72 65 20 61 20 50  d to acquire a P
bed40 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20  ENDING lock or. 
bed50 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f 63   ** a SHARED loc
bed60 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 63  k.  If we are ac
bed70 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 44  quiring a SHARED
bed80 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 69   lock, the acqui
bed90 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74  sition of.  ** t
beda0 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20  he PENDING_LOCK 
bedb0 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61 72  byte is temporar
bedc0 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63  y..  */.  newLoc
bedd0 6b 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c  ktype = pFile->l
bede0 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 70  ocktype;.  if( p
bedf0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  File->locktype==
bee00 4e 4f 5f 4c 4f 43 4b 0a 20 20 20 20 20 20 7c 7c  NO_LOCK.      ||
bee10 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c   (locktype==EXCL
bee20 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46  USIVE_LOCK && pF
bee30 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52  ile->locktype==R
bee40 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 0a 20 20  ESERVED_LOCK).  
bee50 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e  ){.    LockArea.
bee60 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 4e  lOffset = PENDIN
bee70 47 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f 63 6b  G_BYTE;.    Lock
bee80 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c  Area.lRange = 1L
bee90 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ;.    UnlockArea
beea0 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20  .lOffset = 0L;. 
beeb0 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52     UnlockArea.lR
beec0 61 6e 67 65 20 3d 20 30 4c 3b 0a 0a 20 20 20 20  ange = 0L;..    
beed0 2f 2a 20 77 61 69 74 20 6c 6f 6e 67 65 72 20 74  /* wait longer t
beee0 68 61 6e 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  han LOCK_TIMEOUT
beef0 20 68 65 72 65 20 6e 6f 74 20 74 6f 20 68 61 76   here not to hav
bef00 65 20 74 6f 20 74 72 79 20 6d 75 6c 74 69 70 6c  e to try multipl
bef10 65 20 74 69 6d 65 73 20 2a 2f 0a 20 20 20 20 72  e times */.    r
bef20 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c  es = DosSetFileL
bef30 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20  ocks( pFile->h, 
bef40 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f  &UnlockArea, &Lo
bef50 63 6b 41 72 65 61 2c 20 31 30 30 4c 2c 20 30 4c  ckArea, 100L, 0L
bef60 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20   );.    if( res 
bef70 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  == NO_ERROR ){. 
bef80 20 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c       gotPendingL
bef90 6f 63 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 4f  ock = 1;.      O
befa0 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25  STRACE3( "LOCK %
befb0 64 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62  d pending lock b
befc0 6f 6f 6c 65 61 6e 20 73 65 74 2e 20 20 72 65 73  oolean set.  res
befd0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  =%d\n", pFile->h
befe0 2c 20 72 65 73 20 29 3b 0a 20 20 20 20 7d 0a 20  , res );.    }. 
beff0 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65   }..  /* Acquire
bf000 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 0a 20   a shared lock. 
bf010 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79   */.  if( lockty
bf020 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe==SHARED_LOCK 
bf030 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52  && res == NO_ERR
bf040 4f 52 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  OR ){.    assert
bf050 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
bf060 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20  e==NO_LOCK );.  
bf070 20 20 72 65 73 20 3d 20 67 65 74 52 65 61 64 4c    res = getReadL
bf080 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20  ock(pFile);.    
bf090 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52  if( res == NO_ER
bf0a0 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e 65 77  ROR ){.      new
bf0b0 4c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45  Locktype = SHARE
bf0c0 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20  D_LOCK;.    }.  
bf0d0 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43    OSTRACE3( "LOC
bf0e0 4b 20 25 64 20 61 63 71 75 69 72 65 20 73 68 61  K %d acquire sha
bf0f0 72 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64  red lock. res=%d
bf100 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
bf110 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  es );.  }..  /* 
bf120 41 63 71 75 69 72 65 20 61 20 52 45 53 45 52 56  Acquire a RESERV
bf130 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69  ED lock.  */.  i
bf140 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53  f( locktype==RES
bf150 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65  ERVED_LOCK && re
bf160 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b  s == NO_ERROR ){
bf170 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69  .    assert( pFi
bf180 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  le->locktype==SH
bf190 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20  ARED_LOCK );.   
bf1a0 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65   LockArea.lOffse
bf1b0 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54  t = RESERVED_BYT
bf1c0 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e  E;.    LockArea.
bf1d0 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20  lRange = 1L;.   
bf1e0 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66   UnlockArea.lOff
bf1f0 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e  set = 0L;.    Un
bf200 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  lockArea.lRange 
bf210 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20  = 0L;.    res = 
bf220 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28  DosSetFileLocks(
bf230 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f   pFile->h, &Unlo
bf240 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
bf250 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c  a, LOCK_TIMEOUT,
bf260 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72   0L );.    if( r
bf270 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29  es == NO_ERROR )
bf280 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74  {.      newLockt
bf290 79 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f 4c  ype = RESERVED_L
bf2a0 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f  OCK;.    }.    O
bf2b0 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25  STRACE3( "LOCK %
bf2c0 64 20 61 63 71 75 69 72 65 20 72 65 73 65 72 76  d acquire reserv
bf2d0 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c  ed lock. res=%d\
bf2e0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65  n", pFile->h, re
bf2f0 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  s );.  }..  /* A
bf300 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47  cquire a PENDING
bf310 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28   lock.  */.  if(
bf320 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55   locktype==EXCLU
bf330 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73  SIVE_LOCK && res
bf340 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   == NO_ERROR ){.
bf350 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20      newLocktype 
bf360 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a  = PENDING_LOCK;.
bf370 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f      gotPendingLo
bf380 63 6b 20 3d 20 30 3b 0a 20 20 20 20 4f 53 54 52  ck = 0;.    OSTR
bf390 41 43 45 32 28 20 22 4c 4f 43 4b 20 25 64 20 61  ACE2( "LOCK %d a
bf3a0 63 71 75 69 72 65 20 70 65 6e 64 69 6e 67 20 6c  cquire pending l
bf3b0 6f 63 6b 2e 20 70 65 6e 64 69 6e 67 20 6c 6f 63  ock. pending loc
bf3c0 6b 20 62 6f 6f 6c 65 61 6e 20 75 6e 73 65 74 2e  k boolean unset.
bf3d0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29 3b  \n", pFile->h );
bf3e0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69  .  }..  /* Acqui
bf3f0 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20  re an EXCLUSIVE 
bf400 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20  lock.  */.  if( 
bf410 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53  locktype==EXCLUS
bf420 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20  IVE_LOCK && res 
bf430 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  == NO_ERROR ){. 
bf440 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65     assert( pFile
bf450 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52  ->locktype>=SHAR
bf460 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72  ED_LOCK );.    r
bf470 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c  es = unlockReadL
bf480 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20  ock(pFile);.    
bf490 4f 53 54 52 41 43 45 32 28 20 22 75 6e 72 65 61  OSTRACE2( "unrea
bf4a0 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72  dlock = %d\n", r
bf4b0 65 73 20 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72  es );.    LockAr
bf4c0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48 41  ea.lOffset = SHA
bf4d0 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 4c  RED_FIRST;.    L
bf4e0 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d  ockArea.lRange =
bf4f0 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20   SHARED_SIZE;.  
bf500 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66    UnlockArea.lOf
bf510 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55  fset = 0L;.    U
bf520 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
bf530 20 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d   = 0L;.    res =
bf540 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73   DosSetFileLocks
bf550 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c  ( pFile->h, &Unl
bf560 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72  ockArea, &LockAr
bf570 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  ea, LOCK_TIMEOUT
bf580 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20  , 0L );.    if( 
bf590 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20  res == NO_ERROR 
bf5a0 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b  ){.      newLock
bf5b0 74 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45  type = EXCLUSIVE
bf5c0 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65  _LOCK;.    }else
bf5d0 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32  {.      OSTRACE2
bf5e0 28 20 22 4f 53 2f 32 20 65 72 72 6f 72 2d 63 6f  ( "OS/2 error-co
bf5f0 64 65 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20  de = %d\n", res 
bf600 29 3b 0a 20 20 20 20 20 20 67 65 74 52 65 61 64  );.      getRead
bf610 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20  Lock(pFile);.   
bf620 20 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28   }.    OSTRACE3(
bf630 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 72   "LOCK %d acquir
bf640 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  e exclusive lock
bf650 2e 20 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46  .  res=%d\n", pF
bf660 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20  ile->h, res );. 
bf670 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 61   }..  /* If we a
bf680 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e  re holding a PEN
bf690 44 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f  DING lock that o
bf6a0 75 67 68 74 20 74 6f 20 62 65 20 72 65 6c 65 61  ught to be relea
bf6b0 73 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72  sed, then.  ** r
bf6c0 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20  elease it now.. 
bf6d0 20 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 65 6e   */.  if( gotPen
bf6e0 64 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b  dingLock && lock
bf6f0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
bf700 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 3b 0a  K ){.    int r;.
bf710 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66      LockArea.lOf
bf720 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c  fset = 0L;.    L
bf730 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d  ockArea.lRange =
bf740 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41   0L;.    UnlockA
bf750 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45  rea.lOffset = PE
bf760 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20  NDING_BYTE;.    
bf770 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67  UnlockArea.lRang
bf780 65 20 3d 20 31 4c 3b 0a 20 20 20 20 72 20 3d 20  e = 1L;.    r = 
bf790 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28  DosSetFileLocks(
bf7a0 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f   pFile->h, &Unlo
bf7b0 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
bf7c0 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c  a, LOCK_TIMEOUT,
bf7d0 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41   0L );.    OSTRA
bf7e0 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 75 6e  CE3( "LOCK %d un
bf7f0 6c 6f 63 6b 69 6e 67 20 70 65 6e 64 69 6e 67 2f  locking pending/
bf800 69 73 20 73 68 61 72 65 64 2e 20 72 3d 25 64 5c  is shared. r=%d\
bf810 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 20  n", pFile->h, r 
bf820 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64  );.  }..  /* Upd
bf830 61 74 65 20 74 68 65 20 73 74 61 74 65 20 6f 66  ate the state of
bf840 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 65   the lock has he
bf850 6c 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64  ld in the file d
bf860 65 73 63 72 69 70 74 6f 72 20 74 68 65 6e 0a 20  escriptor then. 
bf870 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 61   ** return the a
bf880 70 70 72 6f 70 72 69 61 74 65 20 72 65 73 75 6c  ppropriate resul
bf890 74 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69  t code..  */.  i
bf8a0 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52  f( res == NO_ERR
bf8b0 4f 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  OR ){.    rc = S
bf8c0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73  QLITE_OK;.  }els
bf8d0 65 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28  e{.    OSTRACE4(
bf8e0 20 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25 64   "LOCK FAILED %d
bf8f0 20 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20 62   trying for %d b
bf900 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70 46  ut got %d\n", pF
bf910 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20  ile->h,.        
bf920 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20        locktype, 
bf930 6e 65 77 4c 6f 63 6b 74 79 70 65 20 29 3b 0a 20  newLocktype );. 
bf940 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
bf950 55 53 59 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65  USY;.  }.  pFile
bf960 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6e 65 77  ->locktype = new
bf970 4c 6f 63 6b 74 79 70 65 3b 0a 20 20 4f 53 54 52  Locktype;.  OSTR
bf980 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 6e  ACE3( "LOCK %d n
bf990 6f 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  ow %d\n", pFile-
bf9a0 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  >h, pFile->lockt
bf9b0 79 70 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ype );.  return 
bf9c0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
bf9d0 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73  s routine checks
bf9e0 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52   if there is a R
bf9f0 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c  ESERVED lock hel
bfa00 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69  d on the specifi
bfa10 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68  ed.** file by th
bfa20 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20  is or any other 
bfa30 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68  process. If such
bfa40 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c   a lock is held,
bfa50 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a   return.** non-z
bfa60 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 7a  ero, otherwise z
bfa70 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ero..*/.static i
bfa80 6e 74 20 6f 73 32 43 68 65 63 6b 52 65 73 65 72  nt os2CheckReser
bfa90 76 65 64 4c 6f 63 6b 28 20 73 71 6c 69 74 65 33  vedLock( sqlite3
bfaa0 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a  _file *id, int *
bfab0 70 4f 75 74 20 29 7b 0a 20 20 69 6e 74 20 72 20  pOut ){.  int r 
bfac0 3d 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a  = 0;.  os2File *
bfad0 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65  pFile = (os2File
bfae0 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20  *)id;.  assert( 
bfaf0 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 69 66  pFile!=0 );.  if
bfb00 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
bfb10 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e>=RESERVED_LOCK
bfb20 20 29 7b 0a 20 20 20 20 72 20 3d 20 31 3b 0a 20   ){.    r = 1;. 
bfb30 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45     OSTRACE3( "TE
bfb40 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64  ST WR-LOCK %d %d
bfb50 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69   (local)\n", pFi
bfb60 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 65  le->h, r );.  }e
bfb70 6c 73 65 7b 0a 20 20 20 20 46 49 4c 45 4c 4f 43  lse{.    FILELOC
bfb80 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20  K  LockArea,.   
bfb90 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f 63             Unloc
bfba0 6b 41 72 65 61 3b 0a 20 20 20 20 41 50 49 52 45  kArea;.    APIRE
bfbb0 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b  T rc = NO_ERROR;
bfbc0 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63  .    memset(&Loc
bfbd0 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66  kArea, 0, sizeof
bfbe0 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 20  (LockArea));.   
bfbf0 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41   memset(&UnlockA
bfc00 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55  rea, 0, sizeof(U
bfc10 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 20  nlockArea));.   
bfc20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65   LockArea.lOffse
bfc30 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54  t = RESERVED_BYT
bfc40 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e  E;.    LockArea.
bfc50 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20  lRange = 1L;.   
bfc60 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66   UnlockArea.lOff
bfc70 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e  set = 0L;.    Un
bfc80 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  lockArea.lRange 
bfc90 3d 20 30 4c 3b 0a 20 20 20 20 72 63 20 3d 20 44  = 0L;.    rc = D
bfca0 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20  osSetFileLocks( 
bfcb0 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63  pFile->h, &Unloc
bfcc0 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61  kArea, &LockArea
bfcd0 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20  , LOCK_TIMEOUT, 
bfce0 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43  0L );.    OSTRAC
bfcf0 45 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f 43  E3( "TEST WR-LOC
bfd00 4b 20 25 64 20 6c 6f 63 6b 20 72 65 73 65 72 76  K %d lock reserv
bfd10 65 64 20 62 79 74 65 20 72 63 3d 25 64 5c 6e 22  ed byte rc=%d\n"
bfd20 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 20 29  , pFile->h, rc )
bfd30 3b 0a 20 20 20 20 69 66 28 20 72 63 20 3d 3d 20  ;.    if( rc == 
bfd40 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
bfd50 20 20 41 50 49 52 45 54 20 72 63 75 20 3d 20 4e    APIRET rcu = N
bfd60 4f 5f 45 52 52 4f 52 3b 20 2f 2a 20 72 65 74 75  O_ERROR; /* retu
bfd70 72 6e 20 63 6f 64 65 20 66 6f 72 20 75 6e 6c 6f  rn code for unlo
bfd80 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 4c  cking */.      L
bfd90 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
bfda0 3d 20 30 4c 3b 0a 20 20 20 20 20 20 4c 6f 63 6b  = 0L;.      Lock
bfdb0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c  Area.lRange = 0L
bfdc0 3b 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72  ;.      UnlockAr
bfdd0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53  ea.lOffset = RES
bfde0 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20  ERVED_BYTE;.    
bfdf0 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61    UnlockArea.lRa
bfe00 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 20 20  nge = 1L;.      
bfe10 72 63 75 20 3d 20 44 6f 73 53 65 74 46 69 6c 65  rcu = DosSetFile
bfe20 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c  Locks( pFile->h,
bfe30 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c   &UnlockArea, &L
bfe40 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49  ockArea, LOCK_TI
bfe50 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20  MEOUT, 0L );.   
bfe60 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45     OSTRACE3( "TE
bfe70 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 75 6e  ST WR-LOCK %d un
bfe80 6c 6f 63 6b 20 72 65 73 65 72 76 65 64 20 62 79  lock reserved by
bfe90 74 65 20 72 3d 25 64 5c 6e 22 2c 20 70 46 69 6c  te r=%d\n", pFil
bfea0 65 2d 3e 68 2c 20 72 63 75 20 29 3b 0a 20 20 20  e->h, rcu );.   
bfeb0 20 7d 0a 20 20 20 20 72 20 3d 20 21 28 72 63 20   }.    r = !(rc 
bfec0 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 20  == NO_ERROR);.  
bfed0 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 53    OSTRACE3( "TES
bfee0 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20  T WR-LOCK %d %d 
bfef0 28 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 69  (remote)\n", pFi
bff00 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 0a  le->h, r );.  }.
bff10 20 20 2a 70 4f 75 74 20 3d 20 72 3b 0a 20 20 72    *pOut = r;.  r
bff20 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
bff30 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20  .}../*.** Lower 
bff40 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  the locking leve
bff50 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69  l on file descri
bff60 70 74 6f 72 20 69 64 20 74 6f 20 6c 6f 63 6b 74  ptor id to lockt
bff70 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a  ype.  locktype.*
bff80 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72  * must be either
bff90 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52   NO_LOCK or SHAR
bffa0 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49  ED_LOCK..**.** I
bffb0 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65  f the locking le
bffc0 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20  vel of the file 
bffd0 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c  descriptor is al
bffe0 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f  ready at or belo
bfff0 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74  w.** the request
c0000 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  ed locking level
c0010 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
c0020 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a  s a no-op..**.**
c0030 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69   It is not possi
c0040 62 6c 65 20 66 6f 72 20 74 68 69 73 20 72 6f 75  ble for this rou
c0050 74 69 6e 65 20 74 6f 20 66 61 69 6c 20 69 66 20  tine to fail if 
c0060 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
c0070 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43  ent.** is NO_LOC
c0080 4b 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f 6e  K.  If the secon
c0090 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 48  d argument is SH
c00a0 41 52 45 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 74  ARED_LOCK then t
c00b0 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d  his routine.** m
c00c0 69 67 68 74 20 72 65 74 75 72 6e 20 53 51 4c 49  ight return SQLI
c00d0 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61  TE_IOERR;.*/.sta
c00e0 74 69 63 20 69 6e 74 20 6f 73 32 55 6e 6c 6f 63  tic int os2Unloc
c00f0 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  k( sqlite3_file 
c0100 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70  *id, int locktyp
c0110 65 20 29 7b 0a 20 20 69 6e 74 20 74 79 70 65 3b  e ){.  int type;
c0120 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c  .  os2File *pFil
c0130 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64  e = (os2File*)id
c0140 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20  ;.  APIRET rc = 
c0150 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 41 50 49  SQLITE_OK;.  API
c0160 52 45 54 20 72 65 73 20 3d 20 4e 4f 5f 45 52 52  RET res = NO_ERR
c0170 4f 52 3b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20  OR;.  FILELOCK  
c0180 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20  LockArea,.      
c0190 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61        UnlockArea
c01a0 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b  ;.  memset(&Lock
c01b0 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Area, 0, sizeof(
c01c0 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65  LockArea));.  me
c01d0 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61  mset(&UnlockArea
c01e0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f  , 0, sizeof(Unlo
c01f0 63 6b 41 72 65 61 29 29 3b 0a 20 20 61 73 73 65  ckArea));.  asse
c0200 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a  rt( pFile!=0 );.
c0210 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
c0220 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe<=SHARED_LOCK 
c0230 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22  );.  OSTRACE4( "
c0240 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20  UNLOCK %d to %d 
c0250 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65  was %d\n", pFile
c0260 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70  ->h, locktype, p
c0270 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29  File->locktype )
c0280 3b 0a 20 20 74 79 70 65 20 3d 20 70 46 69 6c 65  ;.  type = pFile
c0290 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66  ->locktype;.  if
c02a0 28 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 49 56  ( type>=EXCLUSIV
c02b0 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f  E_LOCK ){.    Lo
c02c0 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d  ckArea.lOffset =
c02d0 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65   0L;.    LockAre
c02e0 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20  a.lRange = 0L;. 
c02f0 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f     UnlockArea.lO
c0300 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46  ffset = SHARED_F
c0310 49 52 53 54 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b  IRST;.    Unlock
c0320 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48  Area.lRange = SH
c0330 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 72  ARED_SIZE;.    r
c0340 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c  es = DosSetFileL
c0350 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20  ocks( pFile->h, 
c0360 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f  &UnlockArea, &Lo
c0370 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d  ckArea, LOCK_TIM
c0380 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20  EOUT, 0L );.    
c0390 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43  OSTRACE3( "UNLOC
c03a0 4b 20 25 64 20 65 78 63 6c 75 73 69 76 65 20 6c  K %d exclusive l
c03b0 6f 63 6b 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70  ock res=%d\n", p
c03c0 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a  File->h, res );.
c03d0 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65      if( locktype
c03e0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26  ==SHARED_LOCK &&
c03f0 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69   getReadLock(pFi
c0400 6c 65 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20  le) != NO_ERROR 
c0410 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73  ){.      /* This
c0420 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61   should never ha
c0430 70 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c 64  ppen.  We should
c0440 20 61 6c 77 61 79 73 20 62 65 20 61 62 6c 65 20   always be able 
c0450 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 63  to.      ** reac
c0460 71 75 69 72 65 20 74 68 65 20 72 65 61 64 20 6c  quire the read l
c0470 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 4f 53 54  ock */.      OST
c0480 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25  RACE3( "UNLOCK %
c0490 64 20 74 6f 20 25 64 20 67 65 74 52 65 61 64 4c  d to %d getReadL
c04a0 6f 63 6b 28 29 20 66 61 69 6c 65 64 5c 6e 22 2c  ock() failed\n",
c04b0 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74   pFile->h, lockt
c04c0 79 70 65 20 29 3b 0a 20 20 20 20 20 20 72 63 20  ype );.      rc 
c04d0 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55  = SQLITE_IOERR_U
c04e0 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d  NLOCK;.    }.  }
c04f0 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45 53  .  if( type>=RES
c0500 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20  ERVED_LOCK ){.  
c0510 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
c0520 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63  et = 0L;.    Loc
c0530 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
c0540 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65  L;.    UnlockAre
c0550 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45  a.lOffset = RESE
c0560 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 55  RVED_BYTE;.    U
c0570 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
c0580 20 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d   = 1L;.    res =
c0590 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73   DosSetFileLocks
c05a0 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c  ( pFile->h, &Unl
c05b0 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72  ockArea, &LockAr
c05c0 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  ea, LOCK_TIMEOUT
c05d0 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52  , 0L );.    OSTR
c05e0 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64  ACE3( "UNLOCK %d
c05f0 20 72 65 73 65 72 76 65 64 20 72 65 73 3d 25 64   reserved res=%d
c0600 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
c0610 65 73 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  es );.  }.  if( 
c0620 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43  locktype==NO_LOC
c0630 4b 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52 45  K && type>=SHARE
c0640 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65  D_LOCK ){.    re
c0650 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f  s = unlockReadLo
c0660 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f  ck(pFile);.    O
c0670 53 54 52 41 43 45 35 28 20 22 55 4e 4c 4f 43 4b  STRACE5( "UNLOCK
c0680 20 25 64 20 69 73 20 25 64 20 77 61 6e 74 20 25   %d is %d want %
c0690 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69  d res=%d\n", pFi
c06a0 6c 65 2d 3e 68 2c 20 74 79 70 65 2c 20 6c 6f 63  le->h, type, loc
c06b0 6b 74 79 70 65 2c 20 72 65 73 20 29 3b 0a 20 20  ktype, res );.  
c06c0 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 50 45  }.  if( type>=PE
c06d0 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20  NDING_LOCK ){.  
c06e0 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
c06f0 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63  et = 0L;.    Loc
c0700 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
c0710 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65  L;.    UnlockAre
c0720 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44  a.lOffset = PEND
c0730 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e  ING_BYTE;.    Un
c0740 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  lockArea.lRange 
c0750 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20  = 1L;.    res = 
c0760 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28  DosSetFileLocks(
c0770 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f   pFile->h, &Unlo
c0780 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
c0790 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c  a, LOCK_TIMEOUT,
c07a0 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41   0L );.    OSTRA
c07b0 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20  CE3( "UNLOCK %d 
c07c0 70 65 6e 64 69 6e 67 20 72 65 73 3d 25 64 5c 6e  pending res=%d\n
c07d0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73  ", pFile->h, res
c07e0 20 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d   );.  }.  pFile-
c07f0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b  >locktype = lock
c0800 74 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45 33  type;.  OSTRACE3
c0810 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 6e 6f 77  ( "UNLOCK %d now
c0820 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68   %d\n", pFile->h
c0830 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  , pFile->locktyp
c0840 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  e );.  return rc
c0850 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72  ;.}../*.** Contr
c0860 6f 6c 20 61 6e 64 20 71 75 65 72 79 20 6f 66 20  ol and query of 
c0870 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 68 61  the open file ha
c0880 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ndle..*/.static 
c0890 69 6e 74 20 6f 73 32 46 69 6c 65 43 6f 6e 74 72  int os2FileContr
c08a0 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ol(sqlite3_file 
c08b0 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69  *id, int op, voi
c08c0 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74  d *pArg){.  swit
c08d0 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61  ch( op ){.    ca
c08e0 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f  se SQLITE_FCNTL_
c08f0 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20  LOCKSTATE: {.   
c0900 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d     *(int*)pArg =
c0910 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d   ((os2File*)id)-
c0920 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20  >locktype;.     
c0930 20 4f 53 54 52 41 43 45 33 28 20 22 46 43 4e 54   OSTRACE3( "FCNT
c0940 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 25 64 20 6c  L_LOCKSTATE %d l
c0950 6f 63 6b 3d 25 64 5c 6e 22 2c 20 28 28 6f 73 32  ock=%d\n", ((os2
c0960 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28 28  File*)id)->h, ((
c0970 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f  os2File*)id)->lo
c0980 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 20 20  cktype );.      
c0990 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
c09a0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
c09b0 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
c09c0 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  R;.}../*.** Retu
c09d0 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 69  rn the sector si
c09e0 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74  ze in bytes of t
c09f0 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c  he underlying bl
c0a00 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a  ock device for.*
c0a10 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  * the specified 
c0a20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c  file. This is al
c0a30 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 20  most always 512 
c0a40 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62  bytes, but may b
c0a50 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20  e.** larger for 
c0a60 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a  some devices..**
c0a70 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20  .** SQLite code 
c0a80 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e  assumes this fun
c0a90 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69  ction cannot fai
c0aa0 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d  l. It also assum
c0ab0 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77  es that.** if tw
c0ac0 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 61  o files are crea
c0ad0 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20  ted in the same 
c0ae0 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65  file-system dire
c0af0 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61  ctory (i.e..** a
c0b00 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 74   database and it
c0b10 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20  s journal file) 
c0b20 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 20  that the sector 
c0b30 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65  size will be the
c0b40 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74  .** same for bot
c0b50 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  h..*/.static int
c0b60 20 6f 73 32 53 65 63 74 6f 72 53 69 7a 65 28 73   os2SectorSize(s
c0b70 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
c0b80 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  {.  return SQLIT
c0b90 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52  E_DEFAULT_SECTOR
c0ba0 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  _SIZE;.}../*.** 
c0bb0 52 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72 20  Return a vector 
c0bc0 6f 66 20 64 65 76 69 63 65 20 63 68 61 72 61 63  of device charac
c0bd0 74 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 74  teristics..*/.st
c0be0 61 74 69 63 20 69 6e 74 20 6f 73 32 44 65 76 69  atic int os2Devi
c0bf0 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
c0c00 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  s(sqlite3_file *
c0c10 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b  id){.  return 0;
c0c20 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 72 61  .}.../*.** Chara
c0c30 63 74 65 72 20 73 65 74 20 63 6f 6e 76 65 72 73  cter set convers
c0c40 69 6f 6e 20 6f 62 6a 65 63 74 73 20 75 73 65 64  ion objects used
c0c50 20 62 79 20 63 6f 6e 76 65 72 73 69 6f 6e 20 72   by conversion r
c0c60 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74  outines..*/.stat
c0c70 69 63 20 55 63 6f 6e 76 4f 62 6a 65 63 74 20 75  ic UconvObject u
c0c80 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 20 2f 2a  cUtf8 = NULL; /*
c0c90 20 63 6f 6e 76 65 72 74 20 62 65 74 77 65 65 6e   convert between
c0ca0 20 55 54 46 2d 38 20 61 6e 64 20 55 43 53 2d 32   UTF-8 and UCS-2
c0cb0 20 2a 2f 0a 73 74 61 74 69 63 20 55 63 6f 6e 76   */.static Uconv
c0cc0 4f 62 6a 65 63 74 20 75 63 6c 43 70 20 3d 20 4e  Object uclCp = N
c0cd0 55 4c 4c 3b 20 20 2f 2a 20 63 6f 6e 76 65 72 74  ULL;  /* convert
c0ce0 20 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c 20 63   between local c
c0cf0 6f 64 65 70 61 67 65 20 61 6e 64 20 55 43 53 2d  odepage and UCS-
c0d00 32 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70  2 */../*.** Help
c0d10 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69  er function to i
c0d20 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f  nitialize the co
c0d30 6e 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63 74 73  nversion objects
c0d40 20 66 72 6f 6d 20 61 6e 64 20 74 6f 20 55 54 46   from and to UTF
c0d50 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  -8..*/.static vo
c0d60 69 64 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65  id initUconvObje
c0d70 63 74 73 28 20 76 6f 69 64 20 29 7b 0a 20 20 69  cts( void ){.  i
c0d80 66 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f 6e  f( UniCreateUcon
c0d90 76 4f 62 6a 65 63 74 28 20 55 54 46 5f 38 2c 20  vObject( UTF_8, 
c0da0 26 75 63 55 74 66 38 20 29 20 21 3d 20 55 4c 53  &ucUtf8 ) != ULS
c0db0 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 75  _SUCCESS ).    u
c0dc0 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20 20  cUtf8 = NULL;.  
c0dd0 69 66 20 28 20 55 6e 69 43 72 65 61 74 65 55 63  if ( UniCreateUc
c0de0 6f 6e 76 4f 62 6a 65 63 74 28 20 28 55 6e 69 43  onvObject( (UniC
c0df0 68 61 72 20 2a 29 4c 22 40 70 61 74 68 3d 79 65  har *)L"@path=ye
c0e00 73 22 2c 20 26 75 63 6c 43 70 20 29 20 21 3d 20  s", &uclCp ) != 
c0e10 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20  ULS_SUCCESS ).  
c0e20 20 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b 0a    uclCp = NULL;.
c0e30 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20  }../*.** Helper 
c0e40 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 72 65 65  function to free
c0e50 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20   the conversion 
c0e60 6f 62 6a 65 63 74 73 20 66 72 6f 6d 20 61 6e 64  objects from and
c0e70 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73 74   to UTF-8..*/.st
c0e80 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 55 63  atic void freeUc
c0e90 6f 6e 76 4f 62 6a 65 63 74 73 28 20 76 6f 69 64  onvObjects( void
c0ea0 20 29 7b 0a 20 20 69 66 20 28 20 75 63 55 74 66   ){.  if ( ucUtf
c0eb0 38 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65 55  8 ).    UniFreeU
c0ec0 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 55 74  convObject( ucUt
c0ed0 66 38 20 29 3b 0a 20 20 69 66 20 28 20 75 63 6c  f8 );.  if ( ucl
c0ee0 43 70 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65  Cp ).    UniFree
c0ef0 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 6c  UconvObject( ucl
c0f00 43 70 20 29 3b 0a 20 20 75 63 55 74 66 38 20 3d  Cp );.  ucUtf8 =
c0f10 20 4e 55 4c 4c 3b 0a 20 20 75 63 6c 43 70 20 3d   NULL;.  uclCp =
c0f20 20 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   NULL;.}../*.** 
c0f30 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20  Helper function 
c0f40 74 6f 20 63 6f 6e 76 65 72 74 20 55 54 46 2d 38  to convert UTF-8
c0f50 20 66 69 6c 65 6e 61 6d 65 73 20 74 6f 20 6c 6f   filenames to lo
c0f60 63 61 6c 20 4f 53 2f 32 20 63 6f 64 65 70 61 67  cal OS/2 codepag
c0f70 65 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74  e..** The two-st
c0f80 65 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73  ep process: firs
c0f90 74 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e  t convert the in
c0fa0 63 6f 6d 69 6e 67 20 55 54 46 2d 38 20 73 74 72  coming UTF-8 str
c0fb0 69 6e 67 0a 2a 2a 20 69 6e 74 6f 20 55 43 53 2d  ing.** into UCS-
c0fc0 32 20 61 6e 64 20 74 68 65 6e 20 66 72 6f 6d 20  2 and then from 
c0fd0 55 43 53 2d 32 20 74 6f 20 74 68 65 20 63 75 72  UCS-2 to the cur
c0fe0 72 65 6e 74 20 63 6f 64 65 70 61 67 65 2e 0a 2a  rent codepage..*
c0ff0 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 63  * The returned c
c1000 68 61 72 20 70 6f 69 6e 74 65 72 20 68 61 73 20  har pointer has 
c1010 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2f 0a  to be freed..*/.
c1020 73 74 61 74 69 63 20 63 68 61 72 20 2a 63 6f 6e  static char *con
c1030 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70  vertUtf8PathToCp
c1040 28 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e  ( const char *in
c1050 20 29 7b 0a 20 20 55 6e 69 43 68 61 72 20 74 65   ){.  UniChar te
c1060 6d 70 50 61 74 68 5b 43 43 48 4d 41 58 50 41 54  mpPath[CCHMAXPAT
c1070 48 5d 3b 0a 20 20 63 68 61 72 20 2a 6f 75 74 20  H];.  char *out 
c1080 3d 20 28 63 68 61 72 20 2a 29 63 61 6c 6c 6f 63  = (char *)calloc
c1090 28 20 43 43 48 4d 41 58 50 41 54 48 2c 20 31 20  ( CCHMAXPATH, 1 
c10a0 29 3b 0a 0a 20 20 69 66 28 20 21 6f 75 74 20 29  );..  if( !out )
c10b0 0a 20 20 20 20 72 65 74 75 72 6e 20 4e 55 4c 4c  .    return NULL
c10c0 3b 0a 0a 20 20 69 66 28 20 21 75 63 55 74 66 38  ;..  if( !ucUtf8
c10d0 20 7c 7c 20 21 75 63 6c 43 70 20 29 0a 20 20 20   || !uclCp ).   
c10e0 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74   initUconvObject
c10f0 73 28 29 3b 0a 0a 20 20 2f 2a 20 64 65 74 65 72  s();..  /* deter
c1100 6d 69 6e 65 20 73 74 72 69 6e 67 20 66 6f 72 20  mine string for 
c1110 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f  the conversion o
c1120 66 20 55 54 46 2d 38 20 77 68 69 63 68 20 69 73  f UTF-8 which is
c1130 20 43 50 31 32 30 38 20 2a 2f 0a 20 20 69 66 28   CP1208 */.  if(
c1140 20 55 6e 69 53 74 72 54 6f 55 63 73 28 20 75 63   UniStrToUcs( uc
c1150 55 74 66 38 2c 20 74 65 6d 70 50 61 74 68 2c 20  Utf8, tempPath, 
c1160 28 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 48 4d  (char *)in, CCHM
c1170 41 58 50 41 54 48 20 29 20 21 3d 20 55 4c 53 5f  AXPATH ) != ULS_
c1180 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 72 65  SUCCESS ).    re
c1190 74 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 66 20  turn out; /* if 
c11a0 63 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 6c 73  conversion fails
c11b0 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 6d 70  , return the emp
c11c0 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20  ty string */..  
c11d0 2f 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f  /* conversion fo
c11e0 72 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61  r current codepa
c11f0 67 65 20 77 68 69 63 68 20 63 61 6e 20 62 65 20  ge which can be 
c1200 75 73 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a  used for paths *
c1210 2f 0a 20 20 55 6e 69 53 74 72 46 72 6f 6d 55 63  /.  UniStrFromUc
c1220 73 28 20 75 63 6c 43 70 2c 20 6f 75 74 2c 20 74  s( uclCp, out, t
c1230 65 6d 70 50 61 74 68 2c 20 43 43 48 4d 41 58 50  empPath, CCHMAXP
c1240 41 54 48 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e  ATH );..  return
c1250 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48   out;.}../*.** H
c1260 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74  elper function t
c1270 6f 20 63 6f 6e 76 65 72 74 20 66 69 6c 65 6e 61  o convert filena
c1280 6d 65 73 20 66 72 6f 6d 20 6c 6f 63 61 6c 20 63  mes from local c
c1290 6f 64 65 70 61 67 65 20 74 6f 20 55 54 46 2d 38  odepage to UTF-8
c12a0 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 65  ..** The two-ste
c12b0 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73 74  p process: first
c12c0 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e 63   convert the inc
c12d0 6f 6d 69 6e 67 20 63 6f 64 65 70 61 67 65 2d 73  oming codepage-s
c12e0 70 65 63 69 66 69 63 0a 2a 2a 20 73 74 72 69 6e  pecific.** strin
c12f0 67 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 6e 64  g into UCS-2 and
c1300 20 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 2d 32   then from UCS-2
c1310 20 74 6f 20 74 68 65 20 63 6f 64 65 70 61 67 65   to the codepage
c1320 20 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 68   of UTF-8..** Th
c1330 65 20 72 65 74 75 72 6e 65 64 20 63 68 61 72 20  e returned char 
c1340 70 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20 62  pointer has to b
c1350 65 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  e freed..**.** T
c1360 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
c1370 6e 6f 6e 2d 73 74 61 74 69 63 20 74 6f 20 62 65  non-static to be
c1380 20 61 62 6c 65 20 74 6f 20 75 73 65 20 74 68 69   able to use thi
c1390 73 20 69 6e 20 73 68 65 6c 6c 2e 63 20 61 6e 64  s in shell.c and
c13a0 0a 2a 2a 20 73 69 6d 69 6c 61 72 20 61 70 70 6c  .** similar appl
c13b0 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 74 61  ications that ta
c13c0 6b 65 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 20  ke command line 
c13d0 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 63 68  arguments..*/.ch
c13e0 61 72 20 2a 63 6f 6e 76 65 72 74 43 70 50 61 74  ar *convertCpPat
c13f0 68 54 6f 55 74 66 38 28 20 63 6f 6e 73 74 20 63  hToUtf8( const c
c1400 68 61 72 20 2a 69 6e 20 29 7b 0a 20 20 55 6e 69  har *in ){.  Uni
c1410 43 68 61 72 20 74 65 6d 70 50 61 74 68 5b 43 43  Char tempPath[CC
c1420 48 4d 41 58 50 41 54 48 5d 3b 0a 20 20 63 68 61  HMAXPATH];.  cha
c1430 72 20 2a 6f 75 74 20 3d 20 28 63 68 61 72 20 2a  r *out = (char *
c1440 29 63 61 6c 6c 6f 63 28 20 43 43 48 4d 41 58 50  )calloc( CCHMAXP
c1450 41 54 48 2c 20 31 20 29 3b 0a 0a 20 20 69 66 28  ATH, 1 );..  if(
c1460 20 21 6f 75 74 20 29 0a 20 20 20 20 72 65 74 75   !out ).    retu
c1470 72 6e 20 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28 20  rn NULL;..  if( 
c1480 21 75 63 55 74 66 38 20 7c 7c 20 21 75 63 6c 43  !ucUtf8 || !uclC
c1490 70 20 29 0a 20 20 20 20 69 6e 69 74 55 63 6f 6e  p ).    initUcon
c14a0 76 4f 62 6a 65 63 74 73 28 29 3b 0a 0a 20 20 2f  vObjects();..  /
c14b0 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72  * conversion for
c14c0 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67   current codepag
c14d0 65 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 75  e which can be u
c14e0 73 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a 2f  sed for paths */
c14f0 0a 20 20 69 66 28 20 55 6e 69 53 74 72 54 6f 55  .  if( UniStrToU
c1500 63 73 28 20 75 63 6c 43 70 2c 20 74 65 6d 70 50  cs( uclCp, tempP
c1510 61 74 68 2c 20 28 63 68 61 72 20 2a 29 69 6e 2c  ath, (char *)in,
c1520 20 43 43 48 4d 41 58 50 41 54 48 20 29 20 21 3d   CCHMAXPATH ) !=
c1530 20 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20   ULS_SUCCESS ). 
c1540 20 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 20 2f     return out; /
c1550 2a 20 69 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20  * if conversion 
c1560 66 61 69 6c 73 2c 20 72 65 74 75 72 6e 20 74 68  fails, return th
c1570 65 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20 2a  e empty string *
c1580 2f 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e  /..  /* determin
c1590 65 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65  e string for the
c15a0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55   conversion of U
c15b0 54 46 2d 38 20 77 68 69 63 68 20 69 73 20 43 50  TF-8 which is CP
c15c0 31 32 30 38 20 2a 2f 0a 20 20 55 6e 69 53 74 72  1208 */.  UniStr
c15d0 46 72 6f 6d 55 63 73 28 20 75 63 55 74 66 38 2c  FromUcs( ucUtf8,
c15e0 20 6f 75 74 2c 20 74 65 6d 70 50 61 74 68 2c 20   out, tempPath, 
c15f0 43 43 48 4d 41 58 50 41 54 48 20 29 3b 0a 0a 20  CCHMAXPATH );.. 
c1600 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a   return out;.}..
c1610 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 63 74 6f  /*.** This vecto
c1620 72 20 64 65 66 69 6e 65 73 20 61 6c 6c 20 74 68  r defines all th
c1630 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 63  e methods that c
c1640 61 6e 20 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e  an operate on an
c1650 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  .** sqlite3_file
c1660 20 66 6f 72 20 6f 73 32 2e 0a 2a 2f 0a 73 74 61   for os2..*/.sta
c1670 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
c1680 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 73 32  3_io_methods os2
c1690 49 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31  IoMethod = {.  1
c16a0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
c16b0 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72           /* iVer
c16c0 73 69 6f 6e 20 2a 2f 0a 20 20 6f 73 32 43 6c 6f  sion */.  os2Clo
c16d0 73 65 2c 0a 20 20 6f 73 32 52 65 61 64 2c 0a 20  se,.  os2Read,. 
c16e0 20 6f 73 32 57 72 69 74 65 2c 0a 20 20 6f 73 32   os2Write,.  os2
c16f0 54 72 75 6e 63 61 74 65 2c 0a 20 20 6f 73 32 53  Truncate,.  os2S
c1700 79 6e 63 2c 0a 20 20 6f 73 32 46 69 6c 65 53 69  ync,.  os2FileSi
c1710 7a 65 2c 0a 20 20 6f 73 32 4c 6f 63 6b 2c 0a 20  ze,.  os2Lock,. 
c1720 20 6f 73 32 55 6e 6c 6f 63 6b 2c 0a 20 20 6f 73   os2Unlock,.  os
c1730 32 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  2CheckReservedLo
c1740 63 6b 2c 0a 20 20 6f 73 32 46 69 6c 65 43 6f 6e  ck,.  os2FileCon
c1750 74 72 6f 6c 2c 0a 20 20 6f 73 32 53 65 63 74 6f  trol,.  os2Secto
c1760 72 53 69 7a 65 2c 0a 20 20 6f 73 32 44 65 76 69  rSize,.  os2Devi
c1770 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
c1780 73 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  s.};../*********
c1790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c17a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c17b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c17c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c17d0 2a 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 20  **.** Here ends 
c17e0 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20  the I/O methods 
c17f0 74 68 61 74 20 66 6f 72 6d 20 74 68 65 20 73 71  that form the sq
c1800 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
c1810 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54   object..**.** T
c1820 68 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66  he next block of
c1830 20 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 73   code implements
c1840 20 74 68 65 20 56 46 53 20 6d 65 74 68 6f 64 73   the VFS methods
c1850 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**************
c1860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
c18a0 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
c18b0 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e  temporary file n
c18c0 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42  ame in zBuf.  zB
c18d0 75 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 65  uf must be big e
c18e0 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64  nough to.** hold
c18f0 20 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68   at pVfs->mxPath
c1900 6e 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 2e  name characters.
c1910 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67  .*/.static int g
c1920 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e  etTempname(int n
c1930 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 20  Buf, char *zBuf 
c1940 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ){.  static cons
c1950 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
c1960 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22  zChars[] =.    "
c1970 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70  abcdefghijklmnop
c1980 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20  qrstuvwxyz".    
c1990 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  "ABCDEFGHIJKLMNO
c19a0 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20  PQRSTUVWXYZ".   
c19b0 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20   "0123456789";. 
c19c0 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 63 68 61   int i, j;.  cha
c19d0 72 20 7a 54 65 6d 70 50 61 74 68 42 75 66 5b 33  r zTempPathBuf[3
c19e0 5d 3b 0a 20 20 50 53 5a 20 7a 54 65 6d 70 50 61  ];.  PSZ zTempPa
c19f0 74 68 20 3d 20 28 50 53 5a 29 26 7a 54 65 6d 70  th = (PSZ)&zTemp
c1a00 50 61 74 68 42 75 66 3b 0a 20 20 69 66 28 20 73  PathBuf;.  if( s
c1a10 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65  qlite3_temp_dire
c1a20 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 7a 54 65  ctory ){.    zTe
c1a30 6d 70 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33  mpPath = sqlite3
c1a40 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b  _temp_directory;
c1a50 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66  .  }else{.    if
c1a60 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50  ( DosScanEnv( (P
c1a70 53 5a 29 22 54 45 4d 50 22 2c 20 26 7a 54 65 6d  SZ)"TEMP", &zTem
c1a80 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20 20  pPath ) ){.     
c1a90 20 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 28   if( DosScanEnv(
c1aa0 20 28 50 53 5a 29 22 54 4d 50 22 2c 20 26 7a 54   (PSZ)"TMP", &zT
c1ab0 65 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20  empPath ) ){.   
c1ac0 20 20 20 20 20 69 66 28 20 44 6f 73 53 63 61 6e       if( DosScan
c1ad0 45 6e 76 28 20 28 50 53 5a 29 22 54 4d 50 44 49  Env( (PSZ)"TMPDI
c1ae0 52 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 20 29  R", &zTempPath )
c1af0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 55   ){.           U
c1b00 4c 4f 4e 47 20 75 6c 44 72 69 76 65 4e 75 6d 20  LONG ulDriveNum 
c1b10 3d 20 30 2c 20 75 6c 44 72 69 76 65 4d 61 70 20  = 0, ulDriveMap 
c1b20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = 0;.           
c1b30 44 6f 73 51 75 65 72 79 43 75 72 72 65 6e 74 44  DosQueryCurrentD
c1b40 69 73 6b 28 20 26 75 6c 44 72 69 76 65 4e 75 6d  isk( &ulDriveNum
c1b50 2c 20 26 75 6c 44 72 69 76 65 4d 61 70 20 29 3b  , &ulDriveMap );
c1b60 0a 20 20 20 20 20 20 20 20 20 20 20 73 70 72 69  .           spri
c1b70 6e 74 66 28 20 28 63 68 61 72 2a 29 7a 54 65 6d  ntf( (char*)zTem
c1b80 70 50 61 74 68 2c 20 22 25 63 3a 22 2c 20 28 63  pPath, "%c:", (c
c1b90 68 61 72 29 28 20 27 41 27 20 2b 20 75 6c 44 72  har)( 'A' + ulDr
c1ba0 69 76 65 4e 75 6d 20 2d 20 31 20 29 20 29 3b 0a  iveNum - 1 ) );.
c1bb0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
c1bc0 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a  }.    }.  }.  /*
c1bd0 20 53 74 72 69 70 20 6f 66 66 20 61 20 74 72 61   Strip off a tra
c1be0 69 6c 69 6e 67 20 73 6c 61 73 68 65 73 20 6f 72  iling slashes or
c1bf0 20 62 61 63 6b 73 6c 61 73 68 65 73 2c 20 6f 74   backslashes, ot
c1c00 68 65 72 77 69 73 65 20 77 65 20 77 6f 75 6c 64  herwise we would
c1c10 20 67 65 74 20 2a 0a 20 20 20 2a 20 6d 75 6c 74   get *.   * mult
c1c20 69 70 6c 65 20 28 62 61 63 6b 29 73 6c 61 73 68  iple (back)slash
c1c30 65 73 20 77 68 69 63 68 20 63 61 75 73 65 73 20  es which causes 
c1c40 44 6f 73 4f 70 65 6e 28 29 20 74 6f 20 66 61 69  DosOpen() to fai
c1c50 6c 2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l.              
c1c60 2a 0a 20 20 20 2a 20 54 72 61 69 6c 69 6e 67 20  *.   * Trailing 
c1c70 73 70 61 63 65 73 20 61 72 65 20 6e 6f 74 20 61  spaces are not a
c1c80 6c 6c 6f 77 65 64 2c 20 65 69 74 68 65 72 2e 20  llowed, either. 
c1c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c1ca0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20             */.  
c1cb0 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  j = sqlite3Strle
c1cc0 6e 33 30 28 7a 54 65 6d 70 50 61 74 68 29 3b 0a  n30(zTempPath);.
c1cd0 20 20 77 68 69 6c 65 28 20 6a 20 3e 20 30 20 26    while( j > 0 &
c1ce0 26 20 28 20 7a 54 65 6d 70 50 61 74 68 5b 6a 2d  & ( zTempPath[j-
c1cf0 31 5d 20 3d 3d 20 27 5c 5c 27 20 7c 7c 20 7a 54  1] == '\\' || zT
c1d00 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20  empPath[j-1] == 
c1d10 27 2f 27 0a 20 20 20 20 20 20 20 20 20 20 20 20  '/'.            
c1d20 20 20 20 20 20 20 20 20 7c 7c 20 7a 54 65 6d 70          || zTemp
c1d30 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 20 27  Path[j-1] == ' '
c1d40 20 29 20 29 7b 0a 20 20 20 20 6a 2d 2d 3b 0a 20   ) ){.    j--;. 
c1d50 20 7d 0a 20 20 7a 54 65 6d 70 50 61 74 68 5b 6a   }.  zTempPath[j
c1d60 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 20  ] = '\0';.  if( 
c1d70 21 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69  !sqlite3_temp_di
c1d80 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 63  rectory ){.    c
c1d90 68 61 72 20 2a 7a 54 65 6d 70 50 61 74 68 55 54  har *zTempPathUT
c1da0 46 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 61 74  F = convertCpPat
c1db0 68 54 6f 55 74 66 38 28 20 7a 54 65 6d 70 50 61  hToUtf8( zTempPa
c1dc0 74 68 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  th );.    sqlite
c1dd0 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42 75 66  3_snprintf( nBuf
c1de0 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20  -30, zBuf,.     
c1df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c1e00 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45   "%s\\"SQLITE_TE
c1e10 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20  MP_FILE_PREFIX, 
c1e20 7a 54 65 6d 70 50 61 74 68 55 54 46 20 29 3b 0a  zTempPathUTF );.
c1e30 20 20 20 20 66 72 65 65 28 20 7a 54 65 6d 70 50      free( zTempP
c1e40 61 74 68 55 54 46 20 29 3b 0a 20 20 7d 65 6c 73  athUTF );.  }els
c1e50 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  e{.    sqlite3_s
c1e60 6e 70 72 69 6e 74 66 28 20 6e 42 75 66 2d 33 30  nprintf( nBuf-30
c1e70 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 20  , zBuf,.        
c1e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 25                "%
c1e90 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f  s\\"SQLITE_TEMP_
c1ea0 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54 65  FILE_PREFIX, zTe
c1eb0 6d 70 50 61 74 68 20 29 3b 0a 20 20 7d 0a 20 20  mpPath );.  }.  
c1ec0 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  j = sqlite3Strle
c1ed0 6e 33 30 28 20 7a 42 75 66 20 29 3b 0a 20 20 73  n30( zBuf );.  s
c1ee0 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73  qlite3_randomnes
c1ef0 73 28 20 32 30 2c 20 26 7a 42 75 66 5b 6a 5d 20  s( 20, &zBuf[j] 
c1f00 29 3b 0a 20 20 66 6f 72 28 20 69 20 3d 20 30 3b  );.  for( i = 0;
c1f10 20 69 20 3c 20 32 30 3b 20 69 2b 2b 2c 20 6a 2b   i < 20; i++, j+
c1f20 2b 20 29 7b 0a 20 20 20 20 7a 42 75 66 5b 6a 5d  + ){.    zBuf[j]
c1f30 20 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73 5b   = (char)zChars[
c1f40 20 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72   ((unsigned char
c1f50 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65 6f  )zBuf[j])%(sizeo
c1f60 66 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b 0a  f(zChars)-1) ];.
c1f70 20 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d 20    }.  zBuf[j] = 
c1f80 30 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 20 22  0;.  OSTRACE2( "
c1f90 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a 20 25  TEMP FILENAME: %
c1fa0 73 5c 6e 22 2c 20 7a 42 75 66 20 29 3b 0a 20 20  s\n", zBuf );.  
c1fb0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
c1fc0 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e  ;.}.../*.** Turn
c1fd0 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 74 68   a relative path
c1fe0 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c  name into a full
c1ff0 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 69 74   pathname.  Writ
c2000 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 61  e the full.** pa
c2010 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 46 75 6c  thname into zFul
c2020 6c 5b 5d 2e 20 20 7a 46 75 6c 6c 5b 5d 20 77 69  l[].  zFull[] wi
c2030 6c 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20 70  ll be at least p
c2040 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a  Vfs->mxPathname.
c2050 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65  ** bytes in size
c2060 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
c2070 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28  os2FullPathname(
c2080 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
c2090 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 2f  pVfs,          /
c20a0 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73  * Pointer to vfs
c20b0 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e   object */.  con
c20c0 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69  st char *zRelati
c20d0 76 65 2c 20 20 20 20 20 20 2f 2a 20 50 6f 73 73  ve,      /* Poss
c20e0 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e  ibly relative in
c20f0 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e  put path */.  in
c2100 74 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20 20  t nFull,        
c2110 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
c2120 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66 66  e of output buff
c2130 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  er in bytes */. 
c2140 20 63 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20 20   char *zFull    
c2150 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c2160 4f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f  Output buffer */
c2170 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 52 65 6c  .){.  char *zRel
c2180 61 74 69 76 65 43 70 20 3d 20 63 6f 6e 76 65 72  ativeCp = conver
c2190 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a  tUtf8PathToCp( z
c21a0 52 65 6c 61 74 69 76 65 20 29 3b 0a 20 20 63 68  Relative );.  ch
c21b0 61 72 20 7a 46 75 6c 6c 43 70 5b 43 43 48 4d 41  ar zFullCp[CCHMA
c21c0 58 50 41 54 48 5d 20 3d 20 22 5c 30 22 3b 0a 20  XPATH] = "\0";. 
c21d0 20 63 68 61 72 20 2a 7a 46 75 6c 6c 55 54 46 3b   char *zFullUTF;
c21e0 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 44  .  APIRET rc = D
c21f0 6f 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f 28  osQueryPathInfo(
c2200 20 7a 52 65 6c 61 74 69 76 65 43 70 2c 20 46 49   zRelativeCp, FI
c2210 4c 5f 51 55 45 52 59 46 55 4c 4c 4e 41 4d 45 2c  L_QUERYFULLNAME,
c2220 20 7a 46 75 6c 6c 43 70 2c 0a 20 20 20 20 20 20   zFullCp,.      
c2230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2240 20 20 20 20 20 20 20 20 20 20 43 43 48 4d 41 58            CCHMAX
c2250 50 41 54 48 20 29 3b 0a 20 20 66 72 65 65 28 20  PATH );.  free( 
c2260 7a 52 65 6c 61 74 69 76 65 43 70 20 29 3b 0a 20  zRelativeCp );. 
c2270 20 7a 46 75 6c 6c 55 54 46 20 3d 20 63 6f 6e 76   zFullUTF = conv
c2280 65 72 74 43 70 50 61 74 68 54 6f 55 74 66 38 28  ertCpPathToUtf8(
c2290 20 7a 46 75 6c 6c 43 70 20 29 3b 0a 20 20 73 71   zFullCp );.  sq
c22a0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20  lite3_snprintf( 
c22b0 6e 46 75 6c 6c 2c 20 7a 46 75 6c 6c 2c 20 7a 46  nFull, zFull, zF
c22c0 75 6c 6c 55 54 46 20 29 3b 0a 20 20 66 72 65 65  ullUTF );.  free
c22d0 28 20 7a 46 75 6c 6c 55 54 46 20 29 3b 0a 20 20  ( zFullUTF );.  
c22e0 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f  return rc == NO_
c22f0 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f  ERROR ? SQLITE_O
c2300 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  K : SQLITE_IOERR
c2310 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e  ;.}.../*.** Open
c2320 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74   a file..*/.stat
c2330 69 63 20 69 6e 74 20 6f 73 32 4f 70 65 6e 28 0a  ic int os2Open(.
c2340 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
c2350 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  Vfs,            
c2360 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20  /* Not used */. 
c2370 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
c2380 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  me,            /
c2390 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69  * Name of the fi
c23a0 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  le */.  sqlite3_
c23b0 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20  file *id,       
c23c0 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
c23d0 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65 20 68  he SQLite file h
c23e0 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20  andle here */.  
c23f0 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20  int flags,      
c2400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c2410 20 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 73   Open mode flags
c2420 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46   */.  int *pOutF
c2430 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20  lags            
c2440 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 72 65      /* Status re
c2450 74 75 72 6e 20 66 6c 61 67 73 20 2a 2f 0a 29 7b  turn flags */.){
c2460 0a 20 20 48 46 49 4c 45 20 68 3b 0a 20 20 55 4c  .  HFILE h;.  UL
c2470 4f 4e 47 20 75 6c 46 69 6c 65 41 74 74 72 69 62  ONG ulFileAttrib
c2480 75 74 65 20 3d 20 46 49 4c 45 5f 4e 4f 52 4d 41  ute = FILE_NORMA
c2490 4c 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 4f 70 65  L;.  ULONG ulOpe
c24a0 6e 46 6c 61 67 73 20 3d 20 30 3b 0a 20 20 55 4c  nFlags = 0;.  UL
c24b0 4f 4e 47 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 3d  ONG ulOpenMode =
c24c0 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70   0;.  os2File *p
c24d0 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a  File = (os2File*
c24e0 29 69 64 3b 0a 20 20 41 50 49 52 45 54 20 72 63  )id;.  APIRET rc
c24f0 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 55   = NO_ERROR;.  U
c2500 4c 4f 4e 47 20 75 6c 41 63 74 69 6f 6e 3b 0a 20  LONG ulAction;. 
c2510 20 63 68 61 72 20 2a 7a 4e 61 6d 65 43 70 3b 0a   char *zNameCp;.
c2520 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 5b    char zTmpname[
c2530 43 43 48 4d 41 58 50 41 54 48 2b 31 5d 3b 20 20  CCHMAXPATH+1];  
c2540 20 20 2f 2a 20 42 75 66 66 65 72 20 74 6f 20 68    /* Buffer to h
c2550 6f 6c 64 20 6e 61 6d 65 20 6f 66 20 74 65 6d 70  old name of temp
c2560 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49   file */..  /* I
c2570 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  f the second arg
c2580 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75  ument to this fu
c2590 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20  nction is NULL, 
c25a0 67 65 6e 65 72 61 74 65 20 61 20 0a 20 20 2a 2a  generate a .  **
c25b0 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20   temporary file 
c25c0 6e 61 6d 65 20 74 6f 20 75 73 65 20 0a 20 20 2a  name to use .  *
c25d0 2f 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 29  /.  if( !zName )
c25e0 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 67  {.    int rc = g
c25f0 65 74 54 65 6d 70 6e 61 6d 65 28 43 43 48 4d 41  etTempname(CCHMA
c2600 58 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d  XPATH+1, zTmpnam
c2610 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  e);.    if( rc!=
c2620 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
c2630 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
c2640 20 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20    }.    zName = 
c2650 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 0a  zTmpname;.  }...
c2660 20 20 6d 65 6d 73 65 74 28 20 70 46 69 6c 65 2c    memset( pFile,
c2670 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69 6c   0, sizeof(*pFil
c2680 65 29 20 29 3b 0a 0a 20 20 4f 53 54 52 41 43 45  e) );..  OSTRACE
c2690 32 28 20 22 4f 50 45 4e 20 77 61 6e 74 20 25 64  2( "OPEN want %d
c26a0 5c 6e 22 2c 20 66 6c 61 67 73 20 29 3b 0a 0a 20  \n", flags );.. 
c26b0 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c   if( flags & SQL
c26c0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
c26d0 54 45 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e  TE ){.    ulOpen
c26e0 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 43 43  Mode |= OPEN_ACC
c26f0 45 53 53 5f 52 45 41 44 57 52 49 54 45 3b 0a 20  ESS_READWRITE;. 
c2700 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50     OSTRACE1( "OP
c2710 45 4e 20 72 65 61 64 2f 77 72 69 74 65 5c 6e 22  EN read/write\n"
c2720 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   );.  }else{.   
c2730 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f   ulOpenMode |= O
c2740 50 45 4e 5f 41 43 43 45 53 53 5f 52 45 41 44 4f  PEN_ACCESS_READO
c2750 4e 4c 59 3b 0a 20 20 20 20 4f 53 54 52 41 43 45  NLY;.    OSTRACE
c2760 31 28 20 22 4f 50 45 4e 20 72 65 61 64 20 6f 6e  1( "OPEN read on
c2770 6c 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20  ly\n" );.  }..  
c2780 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49  if( flags & SQLI
c2790 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 29  TE_OPEN_CREATE )
c27a0 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67  {.    ulOpenFlag
c27b0 73 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e  s |= OPEN_ACTION
c27c0 5f 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 53 20  _OPEN_IF_EXISTS 
c27d0 7c 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 43 52  | OPEN_ACTION_CR
c27e0 45 41 54 45 5f 49 46 5f 4e 45 57 3b 0a 20 20 20  EATE_IF_NEW;.   
c27f0 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e   OSTRACE1( "OPEN
c2800 20 6f 70 65 6e 20 6e 65 77 2f 63 72 65 61 74 65   open new/create
c2810 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  \n" );.  }else{.
c2820 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20      ulOpenFlags 
c2830 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f  |= OPEN_ACTION_O
c2840 50 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c 20  PEN_IF_EXISTS | 
c2850 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 46 41 49 4c  OPEN_ACTION_FAIL
c2860 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 20 4f 53 54  _IF_NEW;.    OST
c2870 52 41 43 45 31 28 20 22 4f 50 45 4e 20 6f 70 65  RACE1( "OPEN ope
c2880 6e 20 65 78 69 73 74 69 6e 67 5c 6e 22 20 29 3b  n existing\n" );
c2890 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67  .  }..  if( flag
c28a0 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
c28b0 4d 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 75  MAIN_DB ){.    u
c28c0 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45  lOpenMode |= OPE
c28d0 4e 5f 53 48 41 52 45 5f 44 45 4e 59 4e 4f 4e 45  N_SHARE_DENYNONE
c28e0 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20  ;.    OSTRACE1( 
c28f0 22 4f 50 45 4e 20 73 68 61 72 65 20 72 65 61 64  "OPEN share read
c2900 2f 77 72 69 74 65 5c 6e 22 20 29 3b 0a 20 20 7d  /write\n" );.  }
c2910 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65 6e  else{.    ulOpen
c2920 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 48 41  Mode |= OPEN_SHA
c2930 52 45 5f 44 45 4e 59 57 52 49 54 45 3b 0a 20 20  RE_DENYWRITE;.  
c2940 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45    OSTRACE1( "OPE
c2950 4e 20 73 68 61 72 65 20 72 65 61 64 20 6f 6e 6c  N share read onl
c2960 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69  y\n" );.  }..  i
c2970 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f( flags & SQLIT
c2980 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43  E_OPEN_DELETEONC
c2990 4c 4f 53 45 20 29 7b 0a 20 20 20 20 63 68 61 72  LOSE ){.    char
c29a0 20 70 61 74 68 55 74 66 38 5b 43 43 48 4d 41 58   pathUtf8[CCHMAX
c29b0 50 41 54 48 5d 3b 0a 23 69 66 64 65 66 20 4e 44  PATH];.#ifdef ND
c29c0 45 42 55 47 20 2f 2a 20 77 68 65 6e 20 64 65 62  EBUG /* when deb
c29d0 75 67 67 69 6e 67 20 77 65 20 77 61 6e 74 20 74  ugging we want t
c29e0 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69  o make sure it i
c29f0 73 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 20  s deleted */.   
c2a00 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65   ulFileAttribute
c2a10 20 3d 20 46 49 4c 45 5f 48 49 44 44 45 4e 3b 0a   = FILE_HIDDEN;.
c2a20 23 65 6e 64 69 66 0a 20 20 20 20 6f 73 32 46 75  #endif.    os2Fu
c2a30 6c 6c 50 61 74 68 6e 61 6d 65 28 20 70 56 66 73  llPathname( pVfs
c2a40 2c 20 7a 4e 61 6d 65 2c 20 43 43 48 4d 41 58 50  , zName, CCHMAXP
c2a50 41 54 48 2c 20 70 61 74 68 55 74 66 38 20 29 3b  ATH, pathUtf8 );
c2a60 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 68  .    pFile->path
c2a70 54 6f 44 65 6c 20 3d 20 63 6f 6e 76 65 72 74 55  ToDel = convertU
c2a80 74 66 38 50 61 74 68 54 6f 43 70 28 20 70 61 74  tf8PathToCp( pat
c2a90 68 55 74 66 38 20 29 3b 0a 20 20 20 20 4f 53 54  hUtf8 );.    OST
c2aa0 52 41 43 45 31 28 20 22 4f 50 45 4e 20 68 69 64  RACE1( "OPEN hid
c2ab0 64 65 6e 2f 64 65 6c 65 74 65 20 6f 6e 20 63 6c  den/delete on cl
c2ac0 6f 73 65 20 66 69 6c 65 20 61 74 74 72 69 62 75  ose file attribu
c2ad0 74 65 73 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73  tes\n" );.  }els
c2ae0 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61  e{.    pFile->pa
c2af0 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a  thToDel = NULL;.
c2b00 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f      OSTRACE1( "O
c2b10 50 45 4e 20 6e 6f 72 6d 61 6c 20 66 69 6c 65 20  PEN normal file 
c2b20 61 74 74 72 69 62 75 74 65 5c 6e 22 20 29 3b 0a  attribute\n" );.
c2b30 20 20 7d 0a 0a 20 20 2f 2a 20 61 6c 77 61 79 73    }..  /* always
c2b40 20 6f 70 65 6e 20 69 6e 20 72 61 6e 64 6f 6d 20   open in random 
c2b50 61 63 63 65 73 73 20 6d 6f 64 65 20 66 6f 72 20  access mode for 
c2b60 70 6f 73 73 69 62 6c 79 20 62 65 74 74 65 72 20  possibly better 
c2b70 73 70 65 65 64 20 2a 2f 0a 20 20 75 6c 4f 70 65  speed */.  ulOpe
c2b80 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c  nMode |= OPEN_FL
c2b90 41 47 53 5f 52 41 4e 44 4f 4d 3b 0a 20 20 75 6c  AGS_RANDOM;.  ul
c2ba0 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e  OpenMode |= OPEN
c2bb0 5f 46 4c 41 47 53 5f 46 41 49 4c 5f 4f 4e 5f 45  _FLAGS_FAIL_ON_E
c2bc0 52 52 4f 52 3b 0a 20 20 75 6c 4f 70 65 6e 4d 6f  RROR;.  ulOpenMo
c2bd0 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53  de |= OPEN_FLAGS
c2be0 5f 4e 4f 49 4e 48 45 52 49 54 3b 0a 0a 20 20 7a  _NOINHERIT;..  z
c2bf0 4e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 74  NameCp = convert
c2c00 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a 4e  Utf8PathToCp( zN
c2c10 61 6d 65 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f  ame );.  rc = Do
c2c20 73 4f 70 65 6e 28 20 28 50 53 5a 29 7a 4e 61 6d  sOpen( (PSZ)zNam
c2c30 65 43 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20  eCp,.           
c2c40 20 20 20 20 20 26 68 2c 0a 20 20 20 20 20 20 20       &h,.       
c2c50 20 20 20 20 20 20 20 20 20 26 75 6c 41 63 74 69           &ulActi
c2c60 6f 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  on,.            
c2c70 20 20 20 20 30 4c 2c 0a 20 20 20 20 20 20 20 20      0L,.        
c2c80 20 20 20 20 20 20 20 20 75 6c 46 69 6c 65 41 74          ulFileAt
c2c90 74 72 69 62 75 74 65 2c 0a 20 20 20 20 20 20 20  tribute,.       
c2ca0 20 20 20 20 20 20 20 20 20 75 6c 4f 70 65 6e 46           ulOpenF
c2cb0 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20  lags,.          
c2cc0 20 20 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65        ulOpenMode
c2cd0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
c2ce0 20 20 28 50 45 41 4f 50 32 29 4e 55 4c 4c 20 29    (PEAOP2)NULL )
c2cf0 3b 0a 20 20 66 72 65 65 28 20 7a 4e 61 6d 65 43  ;.  free( zNameC
c2d00 70 20 29 3b 0a 20 20 69 66 28 20 72 63 20 21 3d  p );.  if( rc !=
c2d10 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
c2d20 20 4f 53 54 52 41 43 45 37 28 20 22 4f 50 45 4e   OSTRACE7( "OPEN
c2d30 20 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c 65 20   Invalid handle 
c2d40 72 63 3d 25 64 3a 20 7a 4e 61 6d 65 3d 25 73 2c  rc=%d: zName=%s,
c2d50 20 75 6c 41 63 74 69 6f 6e 3d 25 23 6c 78 2c 20   ulAction=%#lx, 
c2d60 75 6c 41 74 74 72 3d 25 23 6c 78 2c 20 75 6c 46  ulAttr=%#lx, ulF
c2d70 6c 61 67 73 3d 25 23 6c 78 2c 20 75 6c 4d 6f 64  lags=%#lx, ulMod
c2d80 65 3d 25 23 6c 78 5c 6e 22 2c 0a 20 20 20 20 20  e=%#lx\n",.     
c2d90 20 20 20 20 20 20 20 20 20 72 63 2c 20 7a 4e 61           rc, zNa
c2da0 6d 65 2c 20 75 6c 41 63 74 69 6f 6e 2c 20 75 6c  me, ulAction, ul
c2db0 46 69 6c 65 41 74 74 72 69 62 75 74 65 2c 20 75  FileAttribute, u
c2dc0 6c 4f 70 65 6e 46 6c 61 67 73 2c 20 75 6c 4f 70  lOpenFlags, ulOp
c2dd0 65 6e 4d 6f 64 65 20 29 3b 0a 20 20 20 20 69 66  enMode );.    if
c2de0 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44  ( pFile->pathToD
c2df0 65 6c 20 29 0a 20 20 20 20 20 20 66 72 65 65 28  el ).      free(
c2e00 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65   pFile->pathToDe
c2e10 6c 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  l );.    pFile->
c2e20 70 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c  pathToDel = NULL
c2e30 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20  ;.    if( flags 
c2e40 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  & SQLITE_OPEN_RE
c2e50 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20  ADWRITE ){.     
c2e60 20 4f 53 54 52 41 43 45 32 28 20 22 4f 50 45 4e   OSTRACE2( "OPEN
c2e70 20 25 64 20 49 6e 76 61 6c 69 64 20 68 61 6e 64   %d Invalid hand
c2e80 6c 65 5c 6e 22 2c 20 28 28 66 6c 61 67 73 20 7c  le\n", ((flags |
c2e90 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
c2ea0 44 4f 4e 4c 59 29 20 26 20 7e 53 51 4c 49 54 45  DONLY) & ~SQLITE
c2eb0 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29  _OPEN_READWRITE)
c2ec0 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   );.      return
c2ed0 20 6f 73 32 4f 70 65 6e 28 20 70 56 66 73 2c 20   os2Open( pVfs, 
c2ee0 7a 4e 61 6d 65 2c 20 69 64 2c 0a 20 20 20 20 20  zName, id,.     
c2ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2f00 20 28 28 66 6c 61 67 73 20 7c 20 53 51 4c 49 54   ((flags | SQLIT
c2f10 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29  E_OPEN_READONLY)
c2f20 20 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f   & ~SQLITE_OPEN_
c2f30 52 45 41 44 57 52 49 54 45 29 2c 0a 20 20 20 20  READWRITE),.    
c2f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2f50 20 20 70 4f 75 74 46 6c 61 67 73 20 29 3b 0a 20    pOutFlags );. 
c2f60 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
c2f70 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41  return SQLITE_CA
c2f80 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20  NTOPEN;.    }.  
c2f90 7d 0a 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61  }..  if( pOutFla
c2fa0 67 73 20 29 7b 0a 20 20 20 20 2a 70 4f 75 74 46  gs ){.    *pOutF
c2fb0 6c 61 67 73 20 3d 20 66 6c 61 67 73 20 26 20 53  lags = flags & S
c2fc0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
c2fd0 52 49 54 45 20 3f 20 53 51 4c 49 54 45 5f 4f 50  RITE ? SQLITE_OP
c2fe0 45 4e 5f 52 45 41 44 57 52 49 54 45 20 3a 20 53  EN_READWRITE : S
c2ff0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
c3000 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 70 46 69 6c  NLY;.  }..  pFil
c3010 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 6f 73  e->pMethod = &os
c3020 32 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46 69  2IoMethod;.  pFi
c3030 6c 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 4f 70 65  le->h = h;.  Ope
c3040 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20  nCounter(+1);.  
c3050 4f 53 54 52 41 43 45 33 28 20 22 4f 50 45 4e 20  OSTRACE3( "OPEN 
c3060 25 64 20 70 4f 75 74 46 6c 61 67 73 3d 25 64 5c  %d pOutFlags=%d\
c3070 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 4f  n", pFile->h, pO
c3080 75 74 46 6c 61 67 73 20 29 3b 0a 20 20 72 65 74  utFlags );.  ret
c3090 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
c30a0 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74  ../*.** Delete t
c30b0 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a  he named file..*
c30c0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  /.static int os2
c30d0 44 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65  Delete(.  sqlite
c30e0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20  3_vfs *pVfs,    
c30f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3100 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20   /* Not used on 
c3110 6f 73 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  os2 */.  const c
c3120 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20  har *zFilename, 
c3130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3140 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20  /* Name of file 
c3150 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69  to delete */.  i
c3160 6e 74 20 73 79 6e 63 44 69 72 20 20 20 20 20 20  nt syncDir      
c3170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3180 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65        /* Not use
c3190 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a 29 7b 0a 20  d on os2 */.){. 
c31a0 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f   APIRET rc = NO_
c31b0 45 52 52 4f 52 3b 0a 20 20 63 68 61 72 20 2a 7a  ERROR;.  char *z
c31c0 46 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e  FilenameCp = con
c31d0 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70  vertUtf8PathToCp
c31e0 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20  ( zFilename );. 
c31f0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
c3200 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ( return SQLITE_
c3210 49 4f 45 52 52 5f 44 45 4c 45 54 45 20 29 3b 0a  IOERR_DELETE );.
c3220 20 20 72 63 20 3d 20 44 6f 73 44 65 6c 65 74 65    rc = DosDelete
c3230 28 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d 65  ( (PSZ)zFilename
c3240 43 70 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 46  Cp );.  free( zF
c3250 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20 4f  ilenameCp );.  O
c3260 53 54 52 41 43 45 32 28 20 22 44 45 4c 45 54 45  STRACE2( "DELETE
c3270 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69 6c   \"%s\"\n", zFil
c3280 65 6e 61 6d 65 20 29 3b 0a 20 20 72 65 74 75 72  ename );.  retur
c3290 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52  n rc == NO_ERROR
c32a0 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53   ? SQLITE_OK : S
c32b0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45  QLITE_IOERR_DELE
c32c0 54 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65  TE;.}../*.** Che
c32d0 63 6b 20 74 68 65 20 65 78 69 73 74 61 6e 63 65  ck the existance
c32e0 20 61 6e 64 20 73 74 61 74 75 73 20 6f 66 20 61   and status of a
c32f0 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
c3300 20 69 6e 74 20 6f 73 32 41 63 63 65 73 73 28 0a   int os2Access(.
c3310 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
c3320 56 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e  Vfs,        /* N
c3330 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 20 2a  ot used on os2 *
c3340 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
c3350 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 2f 2a  zFilename,    /*
c3360 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f   Name of file to
c3370 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20   check */.  int 
c3380 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20  flags,          
c3390 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66        /* Type of
c33a0 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f 6e   test to make on
c33b0 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20   this file */.  
c33c0 69 6e 74 20 2a 70 4f 75 74 20 20 20 20 20 20 20  int *pOut       
c33d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
c33e0 74 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20  te results here 
c33f0 2a 2f 0a 29 7b 0a 20 20 46 49 4c 45 53 54 41 54  */.){.  FILESTAT
c3400 55 53 33 20 66 73 74 73 33 43 6f 6e 66 69 67 49  US3 fsts3ConfigI
c3410 6e 66 6f 3b 0a 20 20 41 50 49 52 45 54 20 72 63  nfo;.  APIRET rc
c3420 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 63   = NO_ERROR;.  c
c3430 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 70  har *zFilenameCp
c3440 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61   = convertUtf8Pa
c3450 74 68 54 6f 43 70 28 20 7a 46 69 6c 65 6e 61 6d  thToCp( zFilenam
c3460 65 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 20  e );..  memset( 
c3470 26 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f  &fsts3ConfigInfo
c3480 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74 73  , 0, sizeof(fsts
c3490 33 43 6f 6e 66 69 67 49 6e 66 6f 29 20 29 3b 0a  3ConfigInfo) );.
c34a0 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50    rc = DosQueryP
c34b0 61 74 68 49 6e 66 6f 28 20 28 50 53 5a 29 7a 46  athInfo( (PSZ)zF
c34c0 69 6c 65 6e 61 6d 65 43 70 2c 20 46 49 4c 5f 53  ilenameCp, FIL_S
c34d0 54 41 4e 44 41 52 44 2c 0a 20 20 20 20 20 20 20  TANDARD,.       
c34e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c34f0 20 20 26 66 73 74 73 33 43 6f 6e 66 69 67 49 6e    &fsts3ConfigIn
c3500 66 6f 2c 20 73 69 7a 65 6f 66 28 46 49 4c 45 53  fo, sizeof(FILES
c3510 54 41 54 55 53 33 29 20 29 3b 0a 20 20 66 72 65  TATUS3) );.  fre
c3520 65 28 20 7a 46 69 6c 65 6e 61 6d 65 43 70 20 29  e( zFilenameCp )
c3530 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22 41  ;.  OSTRACE4( "A
c3540 43 43 45 53 53 20 66 73 74 73 33 43 6f 6e 66 69  CCESS fsts3Confi
c3550 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 3d 25  gInfo.attrFile=%
c3560 64 20 66 6c 61 67 73 3d 25 64 20 72 63 3d 25 64  d flags=%d rc=%d
c3570 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
c3580 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f   fsts3ConfigInfo
c3590 2e 61 74 74 72 46 69 6c 65 2c 20 66 6c 61 67 73  .attrFile, flags
c35a0 2c 20 72 63 20 29 3b 0a 20 20 73 77 69 74 63 68  , rc );.  switch
c35b0 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63  ( flags ){.    c
c35c0 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53  ase SQLITE_ACCES
c35d0 53 5f 52 45 41 44 3a 0a 20 20 20 20 63 61 73 65  S_READ:.    case
c35e0 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45   SQLITE_ACCESS_E
c35f0 58 49 53 54 53 3a 0a 20 20 20 20 20 20 72 63 20  XISTS:.      rc 
c3600 3d 20 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f  = (rc == NO_ERRO
c3610 52 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43  R);.      OSTRAC
c3620 45 33 28 20 22 41 43 43 45 53 53 20 25 73 20 61  E3( "ACCESS %s a
c3630 63 63 65 73 73 20 6f 66 20 72 65 61 64 20 61 6e  ccess of read an
c3640 64 20 65 78 69 73 74 73 20 20 72 63 3d 25 64 5c  d exists  rc=%d\
c3650 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72  n", zFilename, r
c3660 63 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  c );.      break
c3670 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
c3680 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49  E_ACCESS_READWRI
c3690 54 45 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 28  TE:.      rc = (
c36a0 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 20  rc == NO_ERROR) 
c36b0 26 26 20 28 20 28 66 73 74 73 33 43 6f 6e 66 69  && ( (fsts3Confi
c36c0 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 20 26  gInfo.attrFile &
c36d0 20 46 49 4c 45 5f 52 45 41 44 4f 4e 4c 59 29 20   FILE_READONLY) 
c36e0 3d 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 4f 53  == 0 );.      OS
c36f0 54 52 41 43 45 33 28 20 22 41 43 43 45 53 53 20  TRACE3( "ACCESS 
c3700 25 73 20 61 63 63 65 73 73 20 6f 66 20 72 65 61  %s access of rea
c3710 64 2f 77 72 69 74 65 20 20 72 63 3d 25 64 5c 6e  d/write  rc=%d\n
c3720 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 63  ", zFilename, rc
c3730 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   );.      break;
c3740 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20  .    default:.  
c3750 20 20 20 20 61 73 73 65 72 74 28 20 21 22 49 6e      assert( !"In
c3760 76 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75  valid flags argu
c3770 6d 65 6e 74 22 20 29 3b 0a 20 20 7d 0a 20 20 2a  ment" );.  }.  *
c3780 70 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74  pOut = rc;.  ret
c3790 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
c37a0 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
c37b0 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
c37c0 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  NSION./*.** Inte
c37d0 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69  rfaces for openi
c37e0 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72  ng a shared libr
c37f0 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74  ary, finding ent
c3800 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74  ry points.** wit
c3810 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c  hin the shared l
c3820 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73  ibrary, and clos
c3830 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c  ing the shared l
c3840 69 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a  ibrary..*/./*.**
c3850 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20   Interfaces for 
c3860 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64  opening a shared
c3870 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e   library, findin
c3880 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a  g entry points.*
c3890 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61  * within the sha
c38a0 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64  red library, and
c38b0 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61   closing the sha
c38c0 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a  red library..*/.
c38d0 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6f 73 32  static void *os2
c38e0 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76  DlOpen(sqlite3_v
c38f0 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20  fs *pVfs, const 
c3900 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29  char *zFilename)
c3910 7b 0a 20 20 55 43 48 41 52 20 6c 6f 61 64 45 72  {.  UCHAR loadEr
c3920 72 5b 32 35 36 5d 3b 0a 20 20 48 4d 4f 44 55 4c  r[256];.  HMODUL
c3930 45 20 68 6d 6f 64 3b 0a 20 20 41 50 49 52 45 54  E hmod;.  APIRET
c3940 20 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69   rc;.  char *zFi
c3950 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65  lenameCp = conve
c3960 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 7a  rtUtf8PathToCp(z
c3970 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 63 20  Filename);.  rc 
c3980 3d 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65 28  = DosLoadModule(
c3990 28 50 53 5a 29 6c 6f 61 64 45 72 72 2c 20 73 69  (PSZ)loadErr, si
c39a0 7a 65 6f 66 28 6c 6f 61 64 45 72 72 29 2c 20 7a  zeof(loadErr), z
c39b0 46 69 6c 65 6e 61 6d 65 43 70 2c 20 26 68 6d 6f  FilenameCp, &hmo
c39c0 64 29 3b 0a 20 20 66 72 65 65 28 7a 46 69 6c 65  d);.  free(zFile
c39d0 6e 61 6d 65 43 70 29 3b 0a 20 20 72 65 74 75 72  nameCp);.  retur
c39e0 6e 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52  n rc != NO_ERROR
c39f0 20 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29 68 6d   ? 0 : (void*)hm
c3a00 6f 64 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 41 20 6e 6f  od;.}./*.** A no
c3a10 2d 6f 70 20 73 69 6e 63 65 20 74 68 65 20 65 72  -op since the er
c3a20 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
c3a30 72 6e 65 64 20 6f 6e 20 74 68 65 20 44 6f 73 4c  rned on the DosL
c3a40 6f 61 64 4d 6f 64 75 6c 65 20 63 61 6c 6c 2e 0a  oadModule call..
c3a50 2a 2a 20 6f 73 32 44 6c 6f 70 65 6e 20 72 65 74  ** os2Dlopen ret
c3a60 75 72 6e 73 20 7a 65 72 6f 20 69 66 20 44 6f 73  urns zero if Dos
c3a70 4c 6f 61 64 4d 6f 64 75 6c 65 20 69 73 20 6e 6f  LoadModule is no
c3a80 74 20 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2f  t successful..*/
c3a90 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32  .static void os2
c3aa0 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f  DlError(sqlite3_
c3ab0 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e  vfs *pVfs, int n
c3ac0 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f  Buf, char *zBufO
c3ad0 75 74 29 7b 0a 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f  ut){./* no-op */
c3ae0 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  .}.static void *
c3af0 6f 73 32 44 6c 53 79 6d 28 73 71 6c 69 74 65 33  os2DlSym(sqlite3
c3b00 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64  _vfs *pVfs, void
c3b10 20 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74   *pHandle, const
c3b20 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b   char *zSymbol){
c3b30 0a 20 20 50 46 4e 20 70 66 6e 3b 0a 20 20 41 50  .  PFN pfn;.  AP
c3b40 49 52 45 54 20 72 63 3b 0a 20 20 72 63 20 3d 20  IRET rc;.  rc = 
c3b50 44 6f 73 51 75 65 72 79 50 72 6f 63 41 64 64 72  DosQueryProcAddr
c3b60 28 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e 64 6c  ((HMODULE)pHandl
c3b70 65 2c 20 30 4c 2c 20 7a 53 79 6d 62 6f 6c 2c 20  e, 0L, zSymbol, 
c3b80 26 70 66 6e 29 3b 0a 20 20 69 66 28 20 72 63 20  &pfn);.  if( rc 
c3b90 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  != NO_ERROR ){. 
c3ba0 20 20 20 2f 2a 20 69 66 20 74 68 65 20 73 79 6d     /* if the sym
c3bb0 62 6f 6c 20 69 74 73 65 6c 66 20 77 61 73 20 6e  bol itself was n
c3bc0 6f 74 20 66 6f 75 6e 64 2c 20 73 65 61 72 63 68  ot found, search
c3bd0 20 61 67 61 69 6e 20 66 6f 72 20 74 68 65 20 73   again for the s
c3be0 61 6d 65 0a 20 20 20 20 20 2a 20 73 79 6d 62 6f  ame.     * symbo
c3bf0 6c 20 77 69 74 68 20 61 6e 20 65 78 74 72 61 20  l with an extra 
c3c00 75 6e 64 65 72 73 63 6f 72 65 2c 20 74 68 61 74  underscore, that
c3c10 20 6d 69 67 68 74 20 62 65 20 6e 65 65 64 65 64   might be needed
c3c20 20 64 65 70 65 6e 64 69 6e 67 0a 20 20 20 20 20   depending.     
c3c30 2a 20 6f 6e 20 74 68 65 20 63 61 6c 6c 69 6e 67  * on the calling
c3c40 20 63 6f 6e 76 65 6e 74 69 6f 6e 20 2a 2f 0a 20   convention */. 
c3c50 20 20 20 63 68 61 72 20 5f 7a 53 79 6d 62 6f 6c     char _zSymbol
c3c60 5b 32 35 36 5d 20 3d 20 22 5f 22 3b 0a 20 20 20  [256] = "_";.   
c3c70 20 73 74 72 6e 63 61 74 28 5f 7a 53 79 6d 62 6f   strncat(_zSymbo
c3c80 6c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 32 35 35 29  l, zSymbol, 255)
c3c90 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 51 75  ;.    rc = DosQu
c3ca0 65 72 79 50 72 6f 63 41 64 64 72 28 28 48 4d 4f  eryProcAddr((HMO
c3cb0 44 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30 4c  DULE)pHandle, 0L
c3cc0 2c 20 5f 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 6e  , _zSymbol, &pfn
c3cd0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
c3ce0 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f  rc != NO_ERROR ?
c3cf0 20 30 20 3a 20 28 76 6f 69 64 2a 29 70 66 6e 3b   0 : (void*)pfn;
c3d00 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f  .}.static void o
c3d10 73 32 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65  s2DlClose(sqlite
c3d20 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69  3_vfs *pVfs, voi
c3d30 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 44  d *pHandle){.  D
c3d40 6f 73 46 72 65 65 4d 6f 64 75 6c 65 28 28 48 4d  osFreeModule((HM
c3d50 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a  ODULE)pHandle);.
c3d60 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 51  }.#else /* if SQ
c3d70 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45  LITE_OMIT_LOAD_E
c3d80 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 69  XTENSION is defi
c3d90 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e  ned: */.  #defin
c3da0 65 20 6f 73 32 44 6c 4f 70 65 6e 20 30 0a 20 20  e os2DlOpen 0.  
c3db0 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 45 72 72  #define os2DlErr
c3dc0 6f 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20 6f  or 0.  #define o
c3dd0 73 32 44 6c 53 79 6d 20 30 0a 20 20 23 64 65 66  s2DlSym 0.  #def
c3de0 69 6e 65 20 6f 73 32 44 6c 43 6c 6f 73 65 20 30  ine os2DlClose 0
c3df0 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
c3e00 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75 66  Write up to nBuf
c3e10 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d   bytes of random
c3e20 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e 0a  ness into zBuf..
c3e30 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  */.static int os
c3e40 32 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69  2Randomness(sqli
c3e50 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69  te3_vfs *pVfs, i
c3e60 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a  nt nBuf, char *z
c3e70 42 75 66 20 29 7b 0a 20 20 69 6e 74 20 6e 20 3d  Buf ){.  int n =
c3e80 20 30 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28   0;.#if defined(
c3e90 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 6e  SQLITE_TEST).  n
c3ea0 20 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d 73 65   = nBuf;.  memse
c3eb0 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66 29  t(zBuf, 0, nBuf)
c3ec0 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 73 69  ;.#else.  int si
c3ed0 7a 65 6f 66 55 4c 6f 6e 67 20 3d 20 73 69 7a 65  zeofULong = size
c3ee0 6f 66 28 55 4c 4f 4e 47 29 3b 0a 20 20 69 66 28  of(ULONG);.  if(
c3ef0 20 28 69 6e 74 29 73 69 7a 65 6f 66 28 44 41 54   (int)sizeof(DAT
c3f00 45 54 49 4d 45 29 20 3c 3d 20 6e 42 75 66 20 2d  ETIME) <= nBuf -
c3f10 20 6e 20 29 7b 0a 20 20 20 20 44 41 54 45 54 49   n ){.    DATETI
c3f20 4d 45 20 78 3b 0a 20 20 20 20 44 6f 73 47 65 74  ME x;.    DosGet
c3f30 44 61 74 65 54 69 6d 65 28 26 78 29 3b 0a 20 20  DateTime(&x);.  
c3f40 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e    memcpy(&zBuf[n
c3f50 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 29  ], &x, sizeof(x)
c3f60 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65  );.    n += size
c3f70 6f 66 28 78 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  of(x);.  }..  if
c3f80 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d  ( sizeofULong <=
c3f90 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20   nBuf - n ){.   
c3fa0 20 50 50 49 42 20 70 70 69 62 3b 0a 20 20 20 20   PPIB ppib;.    
c3fb0 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73  DosGetInfoBlocks
c3fc0 28 4e 55 4c 4c 2c 20 26 70 70 69 62 29 3b 0a 20  (NULL, &ppib);. 
c3fd0 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b     memcpy(&zBuf[
c3fe0 6e 5d 2c 20 26 70 70 69 62 2d 3e 70 69 62 5f 75  n], &ppib->pib_u
c3ff0 6c 70 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e  lpid, sizeofULon
c4000 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a  g);.    n += siz
c4010 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20  eofULong;.  }.. 
c4020 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67   if( sizeofULong
c4030 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a   <= nBuf - n ){.
c4040 20 20 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20      PTIB ptib;. 
c4050 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f     DosGetInfoBlo
c4060 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29  cks(&ptib, NULL)
c4070 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42  ;.    memcpy(&zB
c4080 75 66 5b 6e 5d 2c 20 26 70 74 69 62 2d 3e 74 69  uf[n], &ptib->ti
c4090 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c  b_ptib2->tib2_ul
c40a0 74 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67  tid, sizeofULong
c40b0 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65  );.    n += size
c40c0 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 20  ofULong;.  }..  
c40d0 2f 2a 20 69 66 20 77 65 20 73 74 69 6c 6c 20 68  /* if we still h
c40e0 61 76 65 6e 27 74 20 66 69 6c 6c 65 64 20 74 68  aven't filled th
c40f0 65 20 62 75 66 66 65 72 20 79 65 74 20 74 68 65  e buffer yet the
c4100 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 69 6c 6c 20   following will 
c4110 2a 2f 0a 20 20 2f 2a 20 67 72 61 62 20 65 76 65  */.  /* grab eve
c4120 72 79 74 68 69 6e 67 20 6f 6e 63 65 20 69 6e 73  rything once ins
c4130 74 65 61 64 20 6f 66 20 6d 61 6b 69 6e 67 20 73  tead of making s
c4140 65 76 65 72 61 6c 20 63 61 6c 6c 73 20 66 6f 72  everal calls for
c4150 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d 20 2a   a single item *
c4160 2f 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c  /.  if( sizeofUL
c4170 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20  ong <= nBuf - n 
c4180 29 7b 0a 20 20 20 20 55 4c 4f 4e 47 20 75 6c 53  ){.    ULONG ulS
c4190 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 41 58 5d 3b  ysInfo[QSV_MAX];
c41a0 0a 20 20 20 20 44 6f 73 51 75 65 72 79 53 79 73  .    DosQuerySys
c41b0 49 6e 66 6f 28 31 4c 2c 20 51 53 56 5f 4d 41 58  Info(1L, QSV_MAX
c41c0 2c 20 75 6c 53 79 73 49 6e 66 6f 2c 20 73 69 7a  , ulSysInfo, siz
c41d0 65 6f 66 55 4c 6f 6e 67 20 2a 20 51 53 56 5f 4d  eofULong * QSV_M
c41e0 41 58 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 79  AX);..    memcpy
c41f0 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79  (&zBuf[n], &ulSy
c4200 73 49 6e 66 6f 5b 51 53 56 5f 4d 53 5f 43 4f 55  sInfo[QSV_MS_COU
c4210 4e 54 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55  NT - 1], sizeofU
c4220 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20  Long);.    n += 
c4230 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 0a 20 20  sizeofULong;..  
c4240 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e    if( sizeofULon
c4250 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b  g <= nBuf - n ){
c4260 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a  .      memcpy(&z
c4270 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e  Buf[n], &ulSysIn
c4280 66 6f 5b 51 53 56 5f 54 49 4d 45 52 5f 49 4e 54  fo[QSV_TIMER_INT
c4290 45 52 56 41 4c 20 2d 20 31 5d 2c 20 73 69 7a 65  ERVAL - 1], size
c42a0 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20  ofULong);.      
c42b0 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67  n += sizeofULong
c42c0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
c42d0 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e  sizeofULong <= n
c42e0 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20  Buf - n ){.     
c42f0 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d   memcpy(&zBuf[n]
c4300 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56  , &ulSysInfo[QSV
c4310 5f 54 49 4d 45 5f 4c 4f 57 20 2d 20 31 5d 2c 20  _TIME_LOW - 1], 
c4320 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20  sizeofULong);.  
c4330 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55      n += sizeofU
c4340 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Long;.    }.    
c4350 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20  if( sizeofULong 
c4360 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20  <= nBuf - n ){. 
c4370 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75       memcpy(&zBu
c4380 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f  f[n], &ulSysInfo
c4390 5b 51 53 56 5f 54 49 4d 45 5f 48 49 47 48 20 2d  [QSV_TIME_HIGH -
c43a0 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67   1], sizeofULong
c43b0 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69  );.      n += si
c43c0 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d  zeofULong;.    }
c43d0 0a 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55  .    if( sizeofU
c43e0 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e  Long <= nBuf - n
c43f0 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   ){.      memcpy
c4400 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79  (&zBuf[n], &ulSy
c4410 73 49 6e 66 6f 5b 51 53 56 5f 54 4f 54 41 56 41  sInfo[QSV_TOTAVA
c4420 49 4c 4d 45 4d 20 2d 20 31 5d 2c 20 73 69 7a 65  ILMEM - 1], size
c4430 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20  ofULong);.      
c4440 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67  n += sizeofULong
c4450 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
c4460 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a  if..  return n;.
c4470 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66  }../*.** Sleep f
c4480 6f 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c  or a little whil
c4490 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61  e.  Return the a
c44a0 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c  mount of time sl
c44b0 65 70 74 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75  ept..** The argu
c44c0 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 75 6d 62  ment is the numb
c44d0 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e  er of microsecon
c44e0 64 73 20 77 65 20 77 61 6e 74 20 74 6f 20 73 6c  ds we want to sl
c44f0 65 65 70 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75  eep..** The retu
c4500 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20  rn value is the 
c4510 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73  number of micros
c4520 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 20  econds of sleep 
c4530 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75  actually.** requ
c4540 65 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 75  ested from the u
c4550 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74  nderlying operat
c4560 69 6e 67 20 73 79 73 74 65 6d 2c 20 61 20 6e 75  ing system, a nu
c4570 6d 62 65 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69  mber which.** mi
c4580 67 68 74 20 62 65 20 67 72 65 61 74 65 72 20 74  ght be greater t
c4590 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20  han or equal to 
c45a0 74 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 62 75  the argument, bu
c45b0 74 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68  t not less.** th
c45c0 61 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e  an the argument.
c45d0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
c45e0 73 32 53 6c 65 65 70 28 20 73 71 6c 69 74 65 33  s2Sleep( sqlite3
c45f0 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20  _vfs *pVfs, int 
c4600 6d 69 63 72 6f 73 65 63 20 29 7b 0a 20 20 44 6f  microsec ){.  Do
c4610 73 53 6c 65 65 70 28 20 28 6d 69 63 72 6f 73 65  sSleep( (microse
c4620 63 2f 31 30 30 30 29 20 29 3b 0a 20 20 72 65 74  c/1000) );.  ret
c4630 75 72 6e 20 6d 69 63 72 6f 73 65 63 3b 0a 7d 0a  urn microsec;.}.
c4640 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
c4650 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 69  wing variable, i
c4660 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a  f set to a non-z
c4670 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f 6d  ero value, becom
c4680 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a  es the result.**
c4690 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 73   returned from s
c46a0 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54  qlite3OsCurrentT
c46b0 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73 20  ime().  This is 
c46c0 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  used for testing
c46d0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
c46e0 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41  TE_TEST.SQLITE_A
c46f0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
c4700 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b  urrent_time = 0;
c4710 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46  .#endif../*.** F
c4720 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20  ind the current 
c4730 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 73  time (in Univers
c4740 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 54  al Coordinated T
c4750 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68 65  ime).  Write the
c4760 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d 65  .** current time
c4770 20 61 6e 64 20 64 61 74 65 20 61 73 20 61 20 4a   and date as a J
c4780 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72  ulian Day number
c4790 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64   into *prNow and
c47a0 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 52  .** return 0.  R
c47b0 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 74  eturn 1 if the t
c47c0 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61 6e  ime and date can
c47d0 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f  not be found..*/
c47e0 0a 69 6e 74 20 6f 73 32 43 75 72 72 65 6e 74 54  .int os2CurrentT
c47f0 69 6d 65 28 20 73 71 6c 69 74 65 33 5f 76 66 73  ime( sqlite3_vfs
c4800 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a   *pVfs, double *
c4810 70 72 4e 6f 77 20 29 7b 0a 20 20 64 6f 75 62 6c  prNow ){.  doubl
c4820 65 20 6e 6f 77 3b 0a 20 20 53 48 4f 52 54 20 6d  e now;.  SHORT m
c4830 69 6e 75 74 65 3b 20 2f 2a 20 6e 65 65 64 73 20  inute; /* needs 
c4840 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 63 6f  to be able to co
c4850 70 65 20 77 69 74 68 20 6e 65 67 61 74 69 76 65  pe with negative
c4860 20 74 69 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74   timezone offset
c4870 20 2a 2f 0a 20 20 55 53 48 4f 52 54 20 73 65 63   */.  USHORT sec
c4880 6f 6e 64 2c 20 68 6f 75 72 2c 0a 20 20 20 20 20  ond, hour,.     
c4890 20 20 20 20 64 61 79 2c 20 6d 6f 6e 74 68 2c 20      day, month, 
c48a0 79 65 61 72 3b 0a 20 20 44 41 54 45 54 49 4d 45  year;.  DATETIME
c48b0 20 64 74 3b 0a 20 20 44 6f 73 47 65 74 44 61 74   dt;.  DosGetDat
c48c0 65 54 69 6d 65 28 20 26 64 74 20 29 3b 0a 20 20  eTime( &dt );.  
c48d0 73 65 63 6f 6e 64 20 3d 20 28 55 53 48 4f 52 54  second = (USHORT
c48e0 29 64 74 2e 73 65 63 6f 6e 64 73 3b 0a 20 20 6d  )dt.seconds;.  m
c48f0 69 6e 75 74 65 20 3d 20 28 53 48 4f 52 54 29 64  inute = (SHORT)d
c4900 74 2e 6d 69 6e 75 74 65 73 20 2b 20 64 74 2e 74  t.minutes + dt.t
c4910 69 6d 65 7a 6f 6e 65 3b 0a 20 20 68 6f 75 72 20  imezone;.  hour 
c4920 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 68 6f 75  = (USHORT)dt.hou
c4930 72 73 3b 0a 20 20 64 61 79 20 3d 20 28 55 53 48  rs;.  day = (USH
c4940 4f 52 54 29 64 74 2e 64 61 79 3b 0a 20 20 6d 6f  ORT)dt.day;.  mo
c4950 6e 74 68 20 3d 20 28 55 53 48 4f 52 54 29 64 74  nth = (USHORT)dt
c4960 2e 6d 6f 6e 74 68 3b 0a 20 20 79 65 61 72 20 3d  .month;.  year =
c4970 20 28 55 53 48 4f 52 54 29 64 74 2e 79 65 61 72   (USHORT)dt.year
c4980 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74  ;..  /* Calculat
c4990 69 6f 6e 73 20 66 72 6f 6d 20 68 74 74 70 3a 2f  ions from http:/
c49a0 2f 77 77 77 2e 61 73 74 72 6f 2e 6b 65 65 6c 65  /www.astro.keele
c49b0 2e 61 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74 72  .ac.uk/~rno/Astr
c49c0 6f 6e 6f 6d 79 2f 68 6a 64 2e 68 74 6d 6c 0a 20  onomy/hjd.html. 
c49d0 20 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 61      http://www.a
c49e0 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 6b  stro.keele.ac.uk
c49f0 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 2f  /~rno/Astronomy/
c4a00 68 6a 64 2d 30 2e 31 2e 63 20 2a 2f 0a 20 20 2f  hjd-0.1.c */.  /
c4a10 2a 20 43 61 6c 63 75 6c 61 74 65 20 74 68 65 20  * Calculate the 
c4a20 4a 75 6c 69 61 6e 20 64 61 79 73 20 2a 2f 0a 20  Julian days */. 
c4a30 20 6e 6f 77 20 3d 20 64 61 79 20 2d 20 33 32 30   now = day - 320
c4a40 37 36 20 2b 0a 20 20 20 20 31 34 36 31 2a 28 79  76 +.    1461*(y
c4a50 65 61 72 20 2b 20 34 38 30 30 20 2b 20 28 6d 6f  ear + 4800 + (mo
c4a60 6e 74 68 20 2d 20 31 34 29 2f 31 32 29 2f 34 20  nth - 14)/12)/4 
c4a70 2b 0a 20 20 20 20 33 36 37 2a 28 6d 6f 6e 74 68  +.    367*(month
c4a80 20 2d 20 32 20 2d 20 28 6d 6f 6e 74 68 20 2d 20   - 2 - (month - 
c4a90 31 34 29 2f 31 32 2a 31 32 29 2f 31 32 20 2d 0a  14)/12*12)/12 -.
c4aa0 20 20 20 20 33 2a 28 28 79 65 61 72 20 2b 20 34      3*((year + 4
c4ab0 39 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 20 31  900 + (month - 1
c4ac0 34 29 2f 31 32 29 2f 31 30 30 29 2f 34 3b 0a 0a  4)/12)/100)/4;..
c4ad0 20 20 2f 2a 20 41 64 64 20 74 68 65 20 66 72 61    /* Add the fra
c4ae0 63 74 69 6f 6e 61 6c 20 68 6f 75 72 73 2c 20 6d  ctional hours, m
c4af0 69 6e 73 20 61 6e 64 20 73 65 63 6f 6e 64 73 20  ins and seconds 
c4b00 2a 2f 0a 20 20 6e 6f 77 20 2b 3d 20 28 68 6f 75  */.  now += (hou
c4b10 72 20 2b 20 31 32 2e 30 29 2f 32 34 2e 30 3b 0a  r + 12.0)/24.0;.
c4b20 20 20 6e 6f 77 20 2b 3d 20 6d 69 6e 75 74 65 2f    now += minute/
c4b30 31 34 34 30 2e 30 3b 0a 20 20 6e 6f 77 20 2b 3d  1440.0;.  now +=
c4b40 20 73 65 63 6f 6e 64 2f 38 36 34 30 30 2e 30 3b   second/86400.0;
c4b50 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 6e 6f 77 3b  .  *prNow = now;
c4b60 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
c4b70 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65  EST.  if( sqlite
c4b80 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29  3_current_time )
c4b90 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73  {.    *prNow = s
c4ba0 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74  qlite3_current_t
c4bb0 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32 34  ime/86400.0 + 24
c4bc0 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e  40587.5;.  }.#en
c4bd0 64 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  dif.  return 0;.
c4be0 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  }..static int os
c4bf0 32 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71  2GetLastError(sq
c4c00 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
c4c10 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20   int nBuf, char 
c4c20 2a 7a 42 75 66 29 7b 0a 20 20 72 65 74 75 72 6e  *zBuf){.  return
c4c30 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69   0;.}../*.** Ini
c4c40 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e  tialize and dein
c4c50 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65  itialize the ope
c4c60 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e  rating system in
c4c70 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49  terface..*/.SQLI
c4c80 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
c4c90 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29  e3_os_init(void)
c4ca0 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74  {.  static sqlit
c4cb0 65 33 5f 76 66 73 20 6f 73 32 56 66 73 20 3d 20  e3_vfs os2Vfs = 
c4cc0 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20 20  {.    1,        
c4cd0 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72           /* iVer
c4ce0 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a 65  sion */.    size
c4cf0 6f 66 28 6f 73 32 46 69 6c 65 29 2c 20 20 20 2f  of(os2File),   /
c4d00 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 20  * szOsFile */.  
c4d10 20 20 43 43 48 4d 41 58 50 41 54 48 2c 20 20 20    CCHMAXPATH,   
c4d20 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61       /* mxPathna
c4d30 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20  me */.    0,    
c4d40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c4d50 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 6f 73  pNext */.    "os
c4d60 32 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  2",             
c4d70 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20  /* zName */.    
c4d80 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
c4d90 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 2a     /* pAppData *
c4da0 2f 0a 0a 20 20 20 20 6f 73 32 4f 70 65 6e 2c 20  /..    os2Open, 
c4db0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70            /* xOp
c4dc0 65 6e 20 2a 2f 0a 20 20 20 20 6f 73 32 44 65 6c  en */.    os2Del
c4dd0 65 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ete,         /* 
c4de0 78 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 6f  xDelete */.    o
c4df0 73 32 41 63 63 65 73 73 2c 20 20 20 20 20 20 20  s2Access,       
c4e00 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a    /* xAccess */.
c4e10 20 20 20 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e      os2FullPathn
c4e20 61 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50  ame,   /* xFullP
c4e30 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6f  athname */.    o
c4e40 73 32 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20  s2DlOpen,       
c4e50 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a    /* xDlOpen */.
c4e60 20 20 20 20 6f 73 32 44 6c 45 72 72 6f 72 2c 20      os2DlError, 
c4e70 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 72         /* xDlErr
c4e80 6f 72 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 53  or */.    os2DlS
c4e90 79 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ym,          /* 
c4ea0 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 6f 73  xDlSym */.    os
c4eb0 32 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 20  2DlClose,       
c4ec0 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a   /* xDlClose */.
c4ed0 20 20 20 20 6f 73 32 52 61 6e 64 6f 6d 6e 65 73      os2Randomnes
c4ee0 73 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f  s,     /* xRando
c4ef0 6d 6e 65 73 73 20 2a 2f 0a 20 20 20 20 6f 73 32  mness */.    os2
c4f00 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20 20  Sleep,          
c4f10 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20  /* xSleep */.   
c4f20 20 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65 2c   os2CurrentTime,
c4f30 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 54      /* xCurrentT
c4f40 69 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 47 65  ime */.    os2Ge
c4f50 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20 2f 2a  tLastError    /*
c4f60 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a   xGetLastError *
c4f70 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33  /.  };.  sqlite3
c4f80 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 6f  _vfs_register(&o
c4f90 73 32 56 66 73 2c 20 31 29 3b 0a 20 20 69 6e 69  s2Vfs, 1);.  ini
c4fa0 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b  tUconvObjects();
c4fb0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
c4fc0 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  _OK;.}.SQLITE_AP
c4fd0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73  I int sqlite3_os
c4fe0 5f 65 6e 64 28 76 6f 69 64 29 7b 0a 20 20 66 72  _end(void){.  fr
c4ff0 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29  eeUconvObjects()
c5000 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
c5010 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  E_OK;.}..#endif 
c5020 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32  /* SQLITE_OS_OS2
c5030 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
c5040 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 6f  **** End of os_o
c5050 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s2.c ***********
c5060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5080 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
c5090 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
c50a0 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a  os_unix.c ******
c50b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c50c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c50d0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20  ***/./*.** 2004 
c50e0 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65  May 22.**.** The
c50f0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
c5100 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
c5110 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
c5120 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
c5130 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
c5140 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
c5150 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
c5160 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
c5170 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
c5180 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
c5190 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
c51a0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
c51b0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
c51c0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
c51d0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
c51e0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
c51f0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
c5200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
c5250 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
c5260 69 6e 73 20 74 68 65 20 56 46 53 20 69 6d 70 6c  ins the VFS impl
c5270 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 75  ementation for u
c5280 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61 74 69  nix-like operati
c5290 6e 67 20 73 79 73 74 65 6d 73 0a 2a 2a 20 69 6e  ng systems.** in
c52a0 63 6c 75 64 65 20 4c 69 6e 75 78 2c 20 4d 61 63  clude Linux, Mac
c52b0 4f 53 58 2c 20 2a 42 53 44 2c 20 51 4e 58 2c 20  OSX, *BSD, QNX, 
c52c0 56 78 57 6f 72 6b 73 2c 20 41 49 58 2c 20 48 50  VxWorks, AIX, HP
c52d0 55 58 2c 20 61 6e 64 20 6f 74 68 65 72 73 2e 0a  UX, and others..
c52e0 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20  **.** There are 
c52f0 61 63 74 75 61 6c 6c 79 20 73 65 76 65 72 61 6c  actually several
c5300 20 64 69 66 66 65 72 65 6e 74 20 56 46 53 20 69   different VFS i
c5310 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 69  mplementations i
c5320 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a 20  n this file..** 
c5330 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 73 20  The differences 
c5340 61 72 65 20 69 6e 20 74 68 65 20 77 61 79 20 74  are in the way t
c5350 68 61 74 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67  hat file locking
c5360 20 69 73 20 64 6f 6e 65 2e 20 20 54 68 65 20 64   is done.  The d
c5370 65 66 61 75 6c 74 0a 2a 2a 20 69 6d 70 6c 65 6d  efault.** implem
c5380 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 50 6f  entation uses Po
c5390 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f 63  six Advisory Loc
c53a0 6b 73 2e 20 20 41 6c 74 65 72 6e 61 74 69 76 65  ks.  Alternative
c53b0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
c53c0 0a 2a 2a 20 75 73 65 20 66 6c 6f 63 6b 28 29 2c  .** use flock(),
c53d0 20 64 6f 74 2d 66 69 6c 65 73 2c 20 76 61 72 69   dot-files, vari
c53e0 6f 75 73 20 70 72 6f 70 72 69 65 74 61 72 79 20  ous proprietary 
c53f0 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 61 73 2c  locking schemas,
c5400 20 6f 72 20 73 69 6d 70 6c 79 0a 2a 2a 20 73 6b   or simply.** sk
c5410 69 70 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20 74  ip locking all t
c5420 6f 67 65 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 54  ogether..**.** T
c5430 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 20  his source file 
c5440 69 73 20 6f 72 67 61 6e 69 7a 65 64 20 69 6e 74  is organized int
c5450 6f 20 64 69 76 69 73 69 6f 6e 73 20 77 68 65 72  o divisions wher
c5460 65 20 74 68 65 20 6c 6f 67 69 63 20 66 6f 72 20  e the logic for 
c5470 76 61 72 69 6f 75 73 0a 2a 2a 20 73 75 62 66 75  various.** subfu
c5480 6e 63 74 69 6f 6e 73 20 69 73 20 63 6f 6e 74 61  nctions is conta
c5490 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 20  ined within the 
c54a0 61 70 70 72 6f 70 72 69 61 74 65 20 64 69 76 69  appropriate divi
c54b0 73 69 6f 6e 2e 20 20 50 4c 45 41 53 45 0a 2a 2a  sion.  PLEASE.**
c54c0 20 4b 45 45 50 20 54 48 45 20 53 54 52 55 43 54   KEEP THE STRUCT
c54d0 55 52 45 20 4f 46 20 54 48 49 53 20 46 49 4c 45  URE OF THIS FILE
c54e0 20 49 4e 54 41 43 54 2e 20 20 4e 65 77 20 63 6f   INTACT.  New co
c54f0 64 65 20 73 68 6f 75 6c 64 20 62 65 20 70 6c 61  de should be pla
c5500 63 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f  ced.** in the co
c5510 72 72 65 63 74 20 64 69 76 69 73 69 6f 6e 20 61  rrect division a
c5520 6e 64 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 65  nd should be cle
c5530 61 72 6c 79 20 6c 61 62 65 6c 65 64 2e 0a 2a 2a  arly labeled..**
c5540 0a 2a 2a 20 54 68 65 20 6c 61 79 6f 75 74 20 6f  .** The layout o
c5550 66 20 64 69 76 69 73 69 6f 6e 73 20 69 73 20 61  f divisions is a
c5560 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
c5570 20 20 20 2a 20 20 47 65 6e 65 72 61 6c 2d 70 75     *  General-pu
c5580 72 70 6f 73 65 20 64 65 63 6c 61 72 61 74 69 6f  rpose declaratio
c5590 6e 73 20 61 6e 64 20 75 74 69 6c 69 74 79 20 66  ns and utility f
c55a0 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 2a  unctions..**   *
c55b0 20 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49 44    Unique file ID
c55c0 20 6c 6f 67 69 63 20 75 73 65 64 20 62 79 20 56   logic used by V
c55d0 78 57 6f 72 6b 73 2e 0a 2a 2a 20 20 20 2a 20 20  xWorks..**   *  
c55e0 56 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 20  Various locking 
c55f0 70 72 69 6d 69 74 69 76 65 20 69 6d 70 6c 65 6d  primitive implem
c5600 65 6e 74 61 74 69 6f 6e 73 20 28 61 6c 6c 20 65  entations (all e
c5610 78 63 65 70 74 20 70 72 6f 78 79 20 6c 6f 63 6b  xcept proxy lock
c5620 69 6e 67 29 3a 0a 2a 2a 20 20 20 20 20 20 2b 20  ing):.**      + 
c5630 66 6f 72 20 50 6f 73 69 78 20 41 64 76 69 73 6f  for Posix Adviso
c5640 72 79 20 4c 6f 63 6b 73 0a 2a 2a 20 20 20 20 20  ry Locks.**     
c5650 20 2b 20 66 6f 72 20 6e 6f 2d 6f 70 20 6c 6f 63   + for no-op loc
c5660 6b 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72  ks.**      + for
c5670 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73 0a   dot-file locks.
c5680 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 66 6c  **      + for fl
c5690 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 0a 2a 2a  ock() locking.**
c56a0 20 20 20 20 20 20 2b 20 66 6f 72 20 6e 61 6d 65        + for name
c56b0 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b  d semaphore lock
c56c0 73 20 28 56 78 57 6f 72 6b 73 20 6f 6e 6c 79 29  s (VxWorks only)
c56d0 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 41  .**      + for A
c56e0 46 50 20 66 69 6c 65 73 79 73 74 65 6d 20 6c 6f  FP filesystem lo
c56f0 63 6b 73 20 28 4d 61 63 4f 53 58 20 6f 6e 6c 79  cks (MacOSX only
c5700 29 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65  ).**   *  sqlite
c5710 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 6e  3_file methods n
c5720 6f 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ot associated wi
c5730 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20  th locking..**  
c5740 20 2a 20 20 44 65 66 69 6e 69 74 69 6f 6e 73 20   *  Definitions 
c5750 6f 66 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  of sqlite3_io_me
c5760 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66 6f  thods objects fo
c5770 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 0a 2a 2a  r all locking.**
c5780 20 20 20 20 20 20 6d 65 74 68 6f 64 73 20 70 6c        methods pl
c5790 75 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63  us "finder" func
c57a0 74 69 6f 6e 73 20 66 6f 72 20 65 61 63 68 20 6c  tions for each l
c57b0 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 2e 0a 2a  ocking method..*
c57c0 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 5f 76  *   *  sqlite3_v
c57d0 66 73 20 6d 65 74 68 6f 64 20 69 6d 70 6c 65 6d  fs method implem
c57e0 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20  entations..**   
c57f0 2a 20 20 4c 6f 63 6b 69 6e 67 20 70 72 69 6d 69  *  Locking primi
c5800 74 69 76 65 73 20 66 6f 72 20 74 68 65 20 70 72  tives for the pr
c5810 6f 78 79 20 75 62 65 72 2d 6c 6f 63 6b 69 6e 67  oxy uber-locking
c5820 2d 6d 65 74 68 6f 64 2e 20 28 4d 61 63 4f 53 58  -method. (MacOSX
c5830 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a 20 20 44   only).**   *  D
c5840 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 73 71  efinitions of sq
c5850 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74  lite3_vfs object
c5860 73 20 66 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e  s for all lockin
c5870 67 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 20 20 20  g methods.**    
c5880 20 20 70 6c 75 73 20 69 6d 70 6c 65 6d 65 6e 74    plus implement
c5890 61 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65  ations of sqlite
c58a0 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 20  3_os_init() and 
c58b0 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29  sqlite3_os_end()
c58c0 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  ..*/.#if SQLITE_
c58d0 4f 53 5f 55 4e 49 58 20 20 20 20 20 20 20 20 20  OS_UNIX         
c58e0 20 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c       /* This fil
c58f0 65 20 69 73 20 75 73 65 64 20 6f 6e 20 75 6e 69  e is used on uni
c5900 78 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  x only */../*.**
c5910 20 54 68 65 72 65 20 61 72 65 20 76 61 72 69 6f   There are vario
c5920 75 73 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 66  us methods for f
c5930 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 75 73 65 64  ile locking used
c5940 20 66 6f 72 20 63 6f 6e 63 75 72 72 65 6e 63 79   for concurrency
c5950 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 3a 0a 2a 2a 0a  .** control:.**.
c5960 2a 2a 20 20 20 31 2e 20 50 4f 53 49 58 20 6c 6f  **   1. POSIX lo
c5970 63 6b 69 6e 67 20 28 74 68 65 20 64 65 66 61 75  cking (the defau
c5980 6c 74 29 2c 0a 2a 2a 20 20 20 32 2e 20 4e 6f 20  lt),.**   2. No 
c5990 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 33 2e  locking,.**   3.
c59a0 20 44 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e   Dot-file lockin
c59b0 67 2c 0a 2a 2a 20 20 20 34 2e 20 66 6c 6f 63 6b  g,.**   4. flock
c59c0 28 29 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20  () locking,.**  
c59d0 20 35 2e 20 41 46 50 20 6c 6f 63 6b 69 6e 67 20   5. AFP locking 
c59e0 28 4f 53 58 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20  (OSX only),.**  
c59f0 20 36 2e 20 4e 61 6d 65 64 20 50 4f 53 49 58 20   6. Named POSIX 
c5a00 73 65 6d 61 70 68 6f 72 65 73 20 28 56 58 57 6f  semaphores (VXWo
c5a10 72 6b 73 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20 20  rks only),.**   
c5a20 37 2e 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67  7. proxy locking
c5a30 2e 20 28 4f 53 58 20 6f 6e 6c 79 29 0a 2a 2a 0a  . (OSX only).**.
c5a40 2a 2a 20 53 74 79 6c 65 73 20 34 2c 20 35 2c 20  ** Styles 4, 5, 
c5a50 61 6e 64 20 37 20 61 72 65 20 6f 6e 6c 79 20 61  and 7 are only a
c5a60 76 61 69 6c 61 62 6c 65 20 6f 66 20 53 51 4c 49  vailable of SQLI
c5a70 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
c5a80 47 5f 53 54 59 4c 45 0a 2a 2a 20 69 73 20 64 65  G_STYLE.** is de
c5a90 66 69 6e 65 64 20 74 6f 20 31 2e 20 20 54 68 65  fined to 1.  The
c5aa0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
c5ab0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 61 6c 73  OCKING_STYLE als
c5ac0 6f 20 65 6e 61 62 6c 65 73 20 61 75 74 6f 6d 61  o enables automa
c5ad0 74 69 63 0a 2a 2a 20 73 65 6c 65 63 74 69 6f 6e  tic.** selection
c5ae0 20 6f 66 20 74 68 65 20 61 70 70 72 6f 70 72 69   of the appropri
c5af0 61 74 65 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c  ate locking styl
c5b00 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 66  e based on the f
c5b10 69 6c 65 73 79 73 74 65 6d 0a 2a 2a 20 77 68 65  ilesystem.** whe
c5b20 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  re the database 
c5b30 69 73 20 6c 6f 63 61 74 65 64 2e 20 20 0a 2a 2f  is located.  .*/
c5b40 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
c5b50 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
c5b60 49 4e 47 5f 53 54 59 4c 45 29 0a 23 20 20 69 66  ING_STYLE).#  if
c5b70 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45   defined(__APPLE
c5b80 5f 5f 29 0a 23 20 20 20 20 64 65 66 69 6e 65 20  __).#    define 
c5b90 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
c5ba0 43 4b 49 4e 47 5f 53 54 59 4c 45 20 31 0a 23 20  CKING_STYLE 1.# 
c5bb0 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e   else.#    defin
c5bc0 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  e SQLITE_ENABLE_
c5bd0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 30 0a  LOCKING_STYLE 0.
c5be0 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  #  endif.#endif.
c5bf0 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 74 68  ./*.** Define th
c5c00 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 70 72 65  e OS_VXWORKS pre
c5c10 2d 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f  -processor macro
c5c20 20 74 6f 20 31 20 69 66 20 62 75 69 6c 64 69 6e   to 1 if buildin
c5c30 67 20 6f 6e 20 0a 2a 2a 20 76 78 77 6f 72 6b 73  g on .** vxworks
c5c40 2c 20 6f 72 20 30 20 6f 74 68 65 72 77 69 73 65  , or 0 otherwise
c5c50 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4f 53 5f  ..*/.#ifndef OS_
c5c60 56 58 57 4f 52 4b 53 0a 23 20 20 69 66 20 64 65  VXWORKS.#  if de
c5c70 66 69 6e 65 64 28 5f 5f 52 54 50 5f 5f 29 20 7c  fined(__RTP__) |
c5c80 7c 20 64 65 66 69 6e 65 64 28 5f 57 52 53 5f 4b  | defined(_WRS_K
c5c90 45 52 4e 45 4c 29 0a 23 20 20 20 20 64 65 66 69  ERNEL).#    defi
c5ca0 6e 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 31 0a  ne OS_VXWORKS 1.
c5cb0 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66  #  else.#    def
c5cc0 69 6e 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 30  ine OS_VXWORKS 0
c5cd0 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66  .#  endif.#endif
c5ce0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64  ../*.** These #d
c5cf0 65 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e  efines should en
c5d00 61 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73  able >2GB file s
c5d10 75 70 70 6f 72 74 20 6f 6e 20 50 6f 73 69 78 20  upport on Posix 
c5d20 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c  if the.** underl
c5d30 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73  ying operating s
c5d40 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69  ystem supports i
c5d50 74 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61  t.  If the OS la
c5d60 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c  cks.** large fil
c5d70 65 20 73 75 70 70 6f 72 74 2c 20 74 68 65 73 65  e support, these
c5d80 20 73 68 6f 75 6c 64 20 62 65 20 6e 6f 2d 6f 70   should be no-op
c5d90 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 67 65 20 66  s..**.** Large f
c5da0 69 6c 65 20 73 75 70 70 6f 72 74 20 63 61 6e 20  ile support can 
c5db0 62 65 20 64 69 73 61 62 6c 65 64 20 75 73 69 6e  be disabled usin
c5dc0 67 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 44  g the -DSQLITE_D
c5dd0 49 53 41 42 4c 45 5f 4c 46 53 20 73 77 69 74 63  ISABLE_LFS switc
c5de0 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 70  h.** on the comp
c5df0 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e  iler command lin
c5e00 65 2e 20 20 54 68 69 73 20 69 73 20 6e 65 63 65  e.  This is nece
c5e10 73 73 61 72 79 20 69 66 20 79 6f 75 20 61 72 65  ssary if you are
c5e20 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 6f 6e   compiling.** on
c5e30 20 61 20 72 65 63 65 6e 74 20 6d 61 63 68 69 6e   a recent machin
c5e40 65 20 28 65 78 3a 20 52 65 64 48 61 74 20 37 2e  e (ex: RedHat 7.
c5e50 32 29 20 62 75 74 20 79 6f 75 20 77 61 6e 74 20  2) but you want 
c5e60 79 6f 75 72 20 63 6f 64 65 20 74 6f 20 77 6f 72  your code to wor
c5e70 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f 6c 64 65 72  k.** on an older
c5e80 20 6d 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65   machine (ex: Re
c5e90 64 48 61 74 20 36 2e 30 29 2e 20 20 49 66 20 79  dHat 6.0).  If y
c5ea0 6f 75 20 63 6f 6d 70 69 6c 65 20 6f 6e 20 52 65  ou compile on Re
c5eb0 64 48 61 74 20 37 2e 32 0a 2a 2a 20 77 69 74 68  dHat 7.2.** with
c5ec0 6f 75 74 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c  out this option,
c5ed0 20 4c 46 53 20 69 73 20 65 6e 61 62 6c 65 2e 20   LFS is enable. 
c5ee0 20 42 75 74 20 4c 46 53 20 64 6f 65 73 20 6e 6f   But LFS does no
c5ef0 74 20 65 78 69 73 74 20 69 6e 20 74 68 65 20 6b  t exist in the k
c5f00 65 72 6e 65 6c 0a 2a 2a 20 69 6e 20 52 65 64 48  ernel.** in RedH
c5f10 61 74 20 36 2e 30 2c 20 73 6f 20 74 68 65 20 63  at 6.0, so the c
c5f20 6f 64 65 20 77 6f 6e 27 74 20 77 6f 72 6b 2e 20  ode won't work. 
c5f30 20 48 65 6e 63 65 2c 20 66 6f 72 20 6d 61 78 69   Hence, for maxi
c5f40 6d 75 6d 20 62 69 6e 61 72 79 0a 2a 2a 20 70 6f  mum binary.** po
c5f50 72 74 61 62 69 6c 69 74 79 20 79 6f 75 20 73 68  rtability you sh
c5f60 6f 75 6c 64 20 6f 6d 69 74 20 4c 46 53 2e 0a 2a  ould omit LFS..*
c5f70 2a 0a 2a 2a 20 54 68 65 20 70 72 65 76 69 6f 75  *.** The previou
c5f80 73 20 70 61 72 61 67 72 61 70 68 20 77 61 73 20  s paragraph was 
c5f90 77 72 69 74 74 65 6e 20 69 6e 20 32 30 30 35 2e  written in 2005.
c5fa0 20 20 28 54 68 69 73 20 70 61 72 61 67 72 61 70    (This paragrap
c5fb0 68 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20  h is written.** 
c5fc0 6f 6e 20 32 30 30 38 2d 31 31 2d 32 38 2e 29 20  on 2008-11-28.) 
c5fd0 54 68 65 73 65 20 64 61 79 73 2c 20 61 6c 6c 20  These days, all 
c5fe0 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20 73 75  Linux kernels su
c5ff0 70 70 6f 72 74 20 6c 61 72 67 65 20 66 69 6c 65  pport large file
c6000 73 2c 20 73 6f 0a 2a 2a 20 79 6f 75 20 73 68 6f  s, so.** you sho
c6010 75 6c 64 20 70 72 6f 62 61 62 6c 79 20 6c 65 61  uld probably lea
c6020 76 65 20 4c 46 53 20 65 6e 61 62 6c 65 64 2e 20  ve LFS enabled. 
c6030 20 42 75 74 20 73 6f 6d 65 20 65 6d 62 65 64 64   But some embedd
c6040 65 64 20 70 6c 61 74 66 6f 72 6d 73 20 6d 69 67  ed platforms mig
c6050 68 74 0a 2a 2a 20 6c 61 63 6b 20 4c 46 53 20 69  ht.** lack LFS i
c6060 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65  n which case the
c6070 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f   SQLITE_DISABLE_
c6080 4c 46 53 20 6d 61 63 72 6f 20 6d 69 67 68 74 20  LFS macro might 
c6090 73 74 69 6c 6c 20 62 65 20 75 73 65 66 75 6c 2e  still be useful.
c60a0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
c60b0 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 0a 23  TE_DISABLE_LFS.#
c60c0 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 5f 46   define _LARGE_F
c60d0 49 4c 45 20 20 20 20 20 20 20 31 0a 23 20 69 66  ILE       1.# if
c60e0 6e 64 65 66 20 5f 46 49 4c 45 5f 4f 46 46 53 45  ndef _FILE_OFFSE
c60f0 54 5f 42 49 54 53 0a 23 20 20 20 64 65 66 69 6e  T_BITS.#   defin
c6100 65 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42  e _FILE_OFFSET_B
c6110 49 54 53 20 36 34 0a 23 20 65 6e 64 69 66 0a 23  ITS 64.# endif.#
c6120 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 46 49   define _LARGEFI
c6130 4c 45 5f 53 4f 55 52 43 45 20 31 0a 23 65 6e 64  LE_SOURCE 1.#end
c6140 69 66 0a 0a 2f 2a 0a 2a 2a 20 73 74 61 6e 64 61  if../*.** standa
c6150 72 64 20 69 6e 63 6c 75 64 65 20 66 69 6c 65 73  rd include files
c6160 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 73  ..*/.#include <s
c6170 79 73 2f 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63  ys/types.h>.#inc
c6180 6c 75 64 65 20 3c 73 79 73 2f 73 74 61 74 2e 68  lude <sys/stat.h
c6190 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 66 63 6e 74  >.#include <fcnt
c61a0 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75  l.h>.#include <u
c61b0 6e 69 73 74 64 2e 68 3e 0a 23 69 6e 63 6c 75 64  nistd.h>.#includ
c61c0 65 20 3c 73 79 73 2f 74 69 6d 65 2e 68 3e 0a 23  e <sys/time.h>.#
c61d0 69 6e 63 6c 75 64 65 20 3c 65 72 72 6e 6f 2e 68  include <errno.h
c61e0 3e 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e  >..#if SQLITE_EN
c61f0 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
c6200 4c 45 0a 23 20 69 6e 63 6c 75 64 65 20 3c 73 79  LE.# include <sy
c6210 73 2f 69 6f 63 74 6c 2e 68 3e 0a 23 20 69 66 20  s/ioctl.h>.# if 
c6220 4f 53 5f 56 58 57 4f 52 4b 53 0a 23 20 20 69 6e  OS_VXWORKS.#  in
c6230 63 6c 75 64 65 20 3c 73 65 6d 61 70 68 6f 72 65  clude <semaphore
c6240 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c  .h>.#  include <
c6250 6c 69 6d 69 74 73 2e 68 3e 0a 23 20 65 6c 73 65  limits.h>.# else
c6260 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79 73  .#  include <sys
c6270 2f 66 69 6c 65 2e 68 3e 0a 23 20 20 69 6e 63 6c  /file.h>.#  incl
c6280 75 64 65 20 3c 73 79 73 2f 70 61 72 61 6d 2e 68  ude <sys/param.h
c6290 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79  >.#  include <sy
c62a0 73 2f 6d 6f 75 6e 74 2e 68 3e 0a 23 20 65 6e 64  s/mount.h>.# end
c62b0 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  if.#endif /* SQL
c62c0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
c62d0 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a  NG_STYLE */../*.
c62e0 2a 2a 20 49 66 20 77 65 20 61 72 65 20 74 6f 20  ** If we are to 
c62f0 62 65 20 74 68 72 65 61 64 2d 73 61 66 65 2c 20  be thread-safe, 
c6300 69 6e 63 6c 75 64 65 20 74 68 65 20 70 74 68 72  include the pthr
c6310 65 61 64 73 20 68 65 61 64 65 72 20 61 6e 64 20  eads header and 
c6320 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 53 51  define.** the SQ
c6330 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45 41 44  LITE_UNIX_THREAD
c6340 53 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 20  S macro..*/.#if 
c6350 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
c6360 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  E.# define SQLIT
c6370 45 5f 55 4e 49 58 5f 54 48 52 45 41 44 53 20 31  E_UNIX_THREADS 1
c6380 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44  .#endif../*.** D
c6390 65 66 61 75 6c 74 20 70 65 72 6d 69 73 73 69 6f  efault permissio
c63a0 6e 73 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67  ns when creating
c63b0 20 61 20 6e 65 77 20 66 69 6c 65 0a 2a 2f 0a 23   a new file.*/.#
c63c0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45  ifndef SQLITE_DE
c63d0 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49  FAULT_FILE_PERMI
c63e0 53 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65 20  SSIONS.# define 
c63f0 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46  SQLITE_DEFAULT_F
c6400 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 20  ILE_PERMISSIONS 
c6410 30 36 34 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  0644.#endif../*.
c6420 20 2a 2a 20 44 65 66 61 75 6c 74 20 70 65 72 6d   ** Default perm
c6430 69 73 73 69 6f 6e 73 20 77 68 65 6e 20 63 72 65  issions when cre
c6440 61 74 69 6e 67 20 61 75 74 6f 20 70 72 6f 78 79  ating auto proxy
c6450 20 64 69 72 0a 20 2a 2f 0a 23 69 66 6e 64 65 66   dir. */.#ifndef
c6460 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
c6470 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53  PROXYDIR_PERMISS
c6480 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65 20 53 51  IONS.# define SQ
c6490 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f  LITE_DEFAULT_PRO
c64a0 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e  XYDIR_PERMISSION
c64b0 53 20 30 37 35 35 0a 23 65 6e 64 69 66 0a 0a 2f  S 0755.#endif../
c64c0 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 75 70  *.** Maximum sup
c64d0 70 6f 72 74 65 64 20 70 61 74 68 2d 6c 65 6e 67  ported path-leng
c64e0 74 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  th..*/.#define M
c64f0 41 58 5f 50 41 54 48 4e 41 4d 45 20 35 31 32 0a  AX_PATHNAME 512.
c6500 0a 2f 2a 0a 2a 2a 20 4f 6e 6c 79 20 73 65 74 20  ./*.** Only set 
c6510 74 68 65 20 6c 61 73 74 45 72 72 6e 6f 20 69 66  the lastErrno if
c6520 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   the error code 
c6530 69 73 20 61 20 72 65 61 6c 20 65 72 72 6f 72 20  is a real error 
c6540 61 6e 64 20 6e 6f 74 20 0a 2a 2a 20 61 20 6e 6f  and not .** a no
c6550 72 6d 61 6c 20 65 78 70 65 63 74 65 64 20 72 65  rmal expected re
c6560 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 53 51 4c  turn code of SQL
c6570 49 54 45 5f 42 55 53 59 20 6f 72 20 53 51 4c 49  ITE_BUSY or SQLI
c6580 54 45 5f 4f 4b 0a 2a 2f 0a 23 64 65 66 69 6e 65  TE_OK.*/.#define
c6590 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 78   IS_LOCK_ERROR(x
c65a0 29 20 20 28 28 78 20 21 3d 20 53 51 4c 49 54 45  )  ((x != SQLITE
c65b0 5f 4f 4b 29 20 26 26 20 28 78 20 21 3d 20 53 51  _OK) && (x != SQ
c65c0 4c 49 54 45 5f 42 55 53 59 29 29 0a 0a 0a 2f 2a  LITE_BUSY)).../*
c65d0 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 2c 20 61  .** Sometimes, a
c65e0 66 74 65 72 20 61 20 66 69 6c 65 20 68 61 6e 64  fter a file hand
c65f0 6c 65 20 69 73 20 63 6c 6f 73 65 64 20 62 79 20  le is closed by 
c6600 53 51 4c 69 74 65 2c 20 74 68 65 20 66 69 6c 65  SQLite, the file
c6610 20 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a 20 63   descriptor.** c
c6620 61 6e 6e 6f 74 20 62 65 20 63 6c 6f 73 65 64 20  annot be closed 
c6630 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49 6e 20  immediately. In 
c6640 74 68 65 73 65 20 63 61 73 65 73 2c 20 69 6e 73  these cases, ins
c6650 74 61 6e 63 65 73 20 6f 66 20 74 68 65 20 66 6f  tances of the fo
c6660 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63  llowing.** struc
c6670 74 75 72 65 20 61 72 65 20 75 73 65 64 20 74 6f  ture are used to
c6680 20 73 74 6f 72 65 20 74 68 65 20 66 69 6c 65 20   store the file 
c6690 64 65 73 63 72 69 70 74 6f 72 20 77 68 69 6c 65  descriptor while
c66a0 20 77 61 69 74 69 6e 67 20 66 6f 72 20 61 6e 0a   waiting for an.
c66b0 2a 2a 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74  ** opportunity t
c66c0 6f 20 65 69 74 68 65 72 20 63 6c 6f 73 65 20 6f  o either close o
c66d0 72 20 72 65 75 73 65 20 69 74 2e 0a 2a 2f 0a 74  r reuse it..*/.t
c66e0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 55 6e  ypedef struct Un
c66f0 69 78 55 6e 75 73 65 64 46 64 20 55 6e 69 78 55  ixUnusedFd UnixU
c6700 6e 75 73 65 64 46 64 3b 0a 73 74 72 75 63 74 20  nusedFd;.struct 
c6710 55 6e 69 78 55 6e 75 73 65 64 46 64 20 7b 0a 20  UnixUnusedFd {. 
c6720 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20   int fd;        
c6730 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
c6740 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f  le descriptor to
c6750 20 63 6c 6f 73 65 20 2a 2f 0a 20 20 69 6e 74 20   close */.  int 
c6760 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  flags;          
c6770 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 74        /* Flags t
c6780 68 69 73 20 66 69 6c 65 20 64 65 73 63 72 69 70  his file descrip
c6790 74 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 77  tor was opened w
c67a0 69 74 68 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75  ith */.  UnixUnu
c67b0 73 65 64 46 64 20 2a 70 4e 65 78 74 3b 20 20 20  sedFd *pNext;   
c67c0 20 20 20 2f 2a 20 4e 65 78 74 20 75 6e 75 73 65     /* Next unuse
c67d0 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  d file descripto
c67e0 72 20 6f 6e 20 73 61 6d 65 20 66 69 6c 65 20 2a  r on same file *
c67f0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  /.};../*.** The 
c6800 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75  unixFile structu
c6810 72 65 20 69 73 20 73 75 62 63 6c 61 73 73 20 6f  re is subclass o
c6820 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73  f sqlite3_file s
c6830 70 65 63 69 66 69 63 20 74 6f 20 74 68 65 20 75  pecific to the u
c6840 6e 69 78 0a 2a 2a 20 56 46 53 20 69 6d 70 6c 65  nix.** VFS imple
c6850 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 74  mentations..*/.t
c6860 79 70 65 64 65 66 20 73 74 72 75 63 74 20 75 6e  ypedef struct un
c6870 69 78 46 69 6c 65 20 75 6e 69 78 46 69 6c 65 3b  ixFile unixFile;
c6880 0a 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65  .struct unixFile
c6890 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f   {.  sqlite3_io_
c68a0 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70  methods const *p
c68b0 4d 65 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61  Method;  /* Alwa
c68c0 79 73 20 74 68 65 20 66 69 72 73 74 20 65 6e 74  ys the first ent
c68d0 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75  ry */.  struct u
c68e0 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65  nixOpenCnt *pOpe
c68f0 6e 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f  n;       /* Info
c6900 20 61 62 6f 75 74 20 61 6c 6c 20 6f 70 65 6e 20   about all open 
c6910 66 64 27 73 20 6f 6e 20 74 68 69 73 20 69 6e 6f  fd's on this ino
c6920 64 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75  de */.  struct u
c6930 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f  nixLockInfo *pLo
c6940 63 6b 3b 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f  ck;      /* Info
c6950 20 61 62 6f 75 74 20 6c 6f 63 6b 73 20 6f 6e 20   about locks on 
c6960 74 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0a 20 20  this inode */.  
c6970 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20  int h;          
c6980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6990 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 73   /* The file des
c69a0 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e 74  criptor */.  int
c69b0 20 64 69 72 66 64 3b 20 20 20 20 20 20 20 20 20   dirfd;         
c69c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c69d0 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   File descriptor
c69e0 20 66 6f 72 20 74 68 65 20 64 69 72 65 63 74 6f   for the directo
c69f0 72 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  ry */.  unsigned
c6a00 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b 20   char locktype; 
c6a10 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
c6a20 74 79 70 65 20 6f 66 20 6c 6f 63 6b 20 68 65 6c  type of lock hel
c6a30 64 20 6f 6e 20 74 68 69 73 20 66 64 20 2a 2f 0a  d on this fd */.
c6a40 20 20 69 6e 74 20 6c 61 73 74 45 72 72 6e 6f 3b    int lastErrno;
c6a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6a60 20 20 20 2f 2a 20 54 68 65 20 75 6e 69 78 20 65     /* The unix e
c6a70 72 72 6e 6f 20 66 72 6f 6d 20 74 68 65 20 6c 61  rrno from the la
c6a80 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a  st I/O error */.
c6a90 20 20 76 6f 69 64 20 2a 6c 6f 63 6b 69 6e 67 43    void *lockingC
c6aa0 6f 6e 74 65 78 74 3b 20 20 20 20 20 20 20 20 20  ontext;         
c6ab0 20 20 20 2f 2a 20 4c 6f 63 6b 69 6e 67 20 73 74     /* Locking st
c6ac0 79 6c 65 20 73 70 65 63 69 66 69 63 20 73 74 61  yle specific sta
c6ad0 74 65 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 73  te */.  UnixUnus
c6ae0 65 64 46 64 20 2a 70 55 6e 75 73 65 64 3b 20 20  edFd *pUnused;  
c6af0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 2d           /* Pre-
c6b00 61 6c 6c 6f 63 61 74 65 64 20 55 6e 69 78 55 6e  allocated UnixUn
c6b10 75 73 65 64 46 64 20 2a 2f 0a 20 20 69 6e 74 20  usedFd */.  int 
c6b20 66 69 6c 65 46 6c 61 67 73 3b 20 20 20 20 20 20  fileFlags;      
c6b30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c6b40 4d 69 73 63 65 6c 6c 61 6e 6f 75 73 20 66 6c 61  Miscellanous fla
c6b50 67 73 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  gs */.#if SQLITE
c6b60 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
c6b70 53 54 59 4c 45 0a 20 20 69 6e 74 20 6f 70 65 6e  STYLE.  int open
c6b80 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  Flags;          
c6b90 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
c6ba0 66 6c 61 67 73 20 73 70 65 63 69 66 69 65 64 20  flags specified 
c6bb0 61 74 20 6f 70 65 6e 28 29 20 2a 2f 0a 23 65 6e  at open() */.#en
c6bc0 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  dif.#if SQLITE_T
c6bd0 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66  HREADSAFE && def
c6be0 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a  ined(__linux__).
c6bf0 20 20 70 74 68 72 65 61 64 5f 74 20 74 69 64 3b    pthread_t tid;
c6c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6c10 20 20 20 2f 2a 20 54 68 65 20 74 68 72 65 61 64     /* The thread
c6c20 20 74 68 61 74 20 22 6f 77 6e 73 22 20 74 68 69   that "owns" thi
c6c30 73 20 75 6e 69 78 46 69 6c 65 20 2a 2f 0a 23 65  s unixFile */.#e
c6c40 6e 64 69 66 0a 23 69 66 20 4f 53 5f 56 58 57 4f  ndif.#if OS_VXWO
c6c50 52 4b 53 0a 20 20 69 6e 74 20 69 73 44 65 6c 65  RKS.  int isDele
c6c60 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  te;             
c6c70 20 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 74 65         /* Delete
c6c80 20 6f 6e 20 63 6c 6f 73 65 20 69 66 20 74 72 75   on close if tru
c6c90 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 76 78  e */.  struct vx
c6ca0 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49 64  worksFileId *pId
c6cb0 3b 20 20 20 20 20 20 20 2f 2a 20 55 6e 69 71 75  ;       /* Uniqu
c6cc0 65 20 66 69 6c 65 20 49 44 20 2a 2f 0a 23 65 6e  e file ID */.#en
c6cd0 64 69 66 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  dif.#ifndef NDEB
c6ce0 55 47 0a 20 20 2f 2a 20 54 68 65 20 6e 65 78 74  UG.  /* The next
c6cf0 20 67 72 6f 75 70 20 6f 66 20 76 61 72 69 61 62   group of variab
c6d00 6c 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20  les are used to 
c6d10 74 72 61 63 6b 20 77 68 65 74 68 65 72 20 6f 72  track whether or
c6d20 20 6e 6f 74 20 74 68 65 0a 20 20 2a 2a 20 74 72   not the.  ** tr
c6d30 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65  ansaction counte
c6d40 72 20 69 6e 20 62 79 74 65 73 20 32 34 2d 32 37  r in bytes 24-27
c6d50 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c   of database fil
c6d60 65 73 20 61 72 65 20 75 70 64 61 74 65 64 0a 20  es are updated. 
c6d70 20 2a 2a 20 77 68 65 6e 65 76 65 72 20 61 6e 79   ** whenever any
c6d80 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74   part of the dat
c6d90 61 62 61 73 65 20 63 68 61 6e 67 65 73 2e 20 20  abase changes.  
c6da0 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75  An assertion fau
c6db0 6c 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 6f 63 63  lt will.  ** occ
c6dc0 75 72 20 69 66 20 61 20 66 69 6c 65 20 69 73 20  ur if a file is 
c6dd0 75 70 64 61 74 65 64 20 77 69 74 68 6f 75 74 20  updated without 
c6de0 61 6c 73 6f 20 75 70 64 61 74 69 6e 67 20 74 68  also updating th
c6df0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20  e transaction.  
c6e00 2a 2a 20 63 6f 75 6e 74 65 72 2e 20 20 54 68 69  ** counter.  Thi
c6e10 73 20 74 65 73 74 20 69 73 20 6d 61 64 65 20 74  s test is made t
c6e20 6f 20 61 76 6f 69 64 20 6e 65 77 20 70 72 6f 62  o avoid new prob
c6e30 6c 65 6d 73 20 73 69 6d 69 6c 61 72 20 74 6f 20  lems similar to 
c6e40 74 68 65 0a 20 20 2a 2a 20 6f 6e 65 20 64 65 73  the.  ** one des
c6e50 63 72 69 62 65 64 20 62 79 20 74 69 63 6b 65 74  cribed by ticket
c6e60 20 23 33 35 38 34 2e 20 0a 20 20 2a 2f 0a 20 20   #3584. .  */.  
c6e70 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74 72  unsigned char tr
c6e80 61 6e 73 43 6e 74 72 43 68 6e 67 3b 20 20 20 2f  ansCntrChng;   /
c6e90 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 74 72  * True if the tr
c6ea0 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65  ansaction counte
c6eb0 72 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75  r changed */.  u
c6ec0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 64 62 55  nsigned char dbU
c6ed0 70 64 61 74 65 3b 20 20 20 20 20 20 20 20 2f 2a  pdate;        /*
c6ee0 20 54 72 75 65 20 69 66 20 61 6e 79 20 70 61 72   True if any par
c6ef0 74 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69  t of database fi
c6f00 6c 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20  le changed */.  
c6f10 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 6e  unsigned char in
c6f20 4e 6f 72 6d 61 6c 57 72 69 74 65 3b 20 20 20 2f  NormalWrite;   /
c6f30 2a 20 54 72 75 65 20 69 66 20 69 6e 20 61 20 6e  * True if in a n
c6f40 6f 72 6d 61 6c 20 77 72 69 74 65 20 6f 70 65 72  ormal write oper
c6f50 61 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a  ation */.#endif.
c6f60 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
c6f70 53 54 0a 20 20 2f 2a 20 49 6e 20 74 65 73 74 20  ST.  /* In test 
c6f80 6d 6f 64 65 2c 20 69 6e 63 72 65 61 73 65 20 74  mode, increase t
c6f90 68 65 20 73 69 7a 65 20 6f 66 20 74 68 69 73 20  he size of this 
c6fa0 73 74 72 75 63 74 75 72 65 20 61 20 62 69 74 20  structure a bit 
c6fb0 73 6f 20 74 68 61 74 20 0a 20 20 2a 2a 20 69 74  so that .  ** it
c6fc0 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20   is larger than 
c6fd0 74 68 65 20 73 74 72 75 63 74 20 43 72 61 73 68  the struct Crash
c6fe0 46 69 6c 65 20 64 65 66 69 6e 65 64 20 69 6e 20  File defined in 
c6ff0 74 65 73 74 36 2e 63 2e 0a 20 20 2a 2f 0a 20 20  test6.c..  */.  
c7000 63 68 61 72 20 61 50 61 64 64 69 6e 67 5b 33 32  char aPadding[32
c7010 5d 3b 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a  ];.#endif.};../*
c7020 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
c7030 67 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20  g macros define 
c7040 62 69 74 73 20 69 6e 20 75 6e 69 78 46 69 6c 65  bits in unixFile
c7050 2e 66 69 6c 65 46 6c 61 67 73 0a 2a 2f 0a 23 64  .fileFlags.*/.#d
c7060 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 48 4f  efine SQLITE_WHO
c7070 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20  LE_FILE_LOCKING 
c7080 20 30 78 30 30 30 31 20 20 20 2f 2a 20 55 73 65   0x0001   /* Use
c7090 20 77 68 6f 6c 65 2d 66 69 6c 65 20 6c 6f 63 6b   whole-file lock
c70a0 69 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e  ing */../*.** In
c70b0 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 20  clude code that 
c70c0 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c  is common to all
c70d0 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f   os_*.c files.*/
c70e0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
c70f0 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d   Include os_comm
c7100 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  on.h in the midd
c7110 6c 65 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 20  le of os_unix.c 
c7120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
c7130 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
c7140 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63   Begin file os_c
c7150 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  ommon.h ********
c7160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
c7180 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20  ./*.** 2004 May 
c7190 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  22.**.** The aut
c71a0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
c71b0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
c71c0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
c71d0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
c71e0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
c71f0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
c7200 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
c7210 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
c7220 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
c7230 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
c7240 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
c7250 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
c7260 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
c7270 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
c7280 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
c7290 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
c72a0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
c72b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c72c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c72d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c72e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c72f0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
c7300 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
c7310 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 74  macros and a lit
c7320 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 20  tle bit of code 
c7330 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74  that is common t
c7340 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20  o.** all of the 
c7350 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 69  platform-specifi
c7360 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29  c files (os_*.c)
c7370 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 65   and is #include
c7380 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20  d into those.** 
c7390 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  files..**.** Thi
c73a0 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65  s file should be
c73b0 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 68   #included by th
c73c0 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f  e os_*.c files o
c73d0 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 20  nly.  It is not 
c73e0 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 72  a.** general pur
c73f0 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c 65  pose header file
c7400 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f  ..**.** $Id: os_
c7410 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e 33 38 20  common.h,v 1.38 
c7420 32 30 30 39 2f 30 32 2f 32 34 20 31 38 3a 34 30  2009/02/24 18:40
c7430 3a 35 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :50 danielk1977 
c7440 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66  Exp $.*/.#ifndef
c7450 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23   _OS_COMMON_H_.#
c7460 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f  define _OS_COMMO
c7470 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c  N_H_../*.** At l
c7480 65 61 73 74 20 74 77 6f 20 62 75 67 73 20 68 61  east two bugs ha
c7490 76 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62 65  ve slipped in be
c74a0 63 61 75 73 65 20 77 65 20 63 68 61 6e 67 65 64  cause we changed
c74b0 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55   the MEMORY_DEBU
c74c0 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51  G.** macro to SQ
c74d0 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 73  LITE_DEBUG and s
c74e0 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69  ome older makefi
c74f0 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65 74  les have not yet
c7500 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77 69   made the.** swi
c7510 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  tch.  The follow
c7520 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64 20  ing code should 
c7530 63 61 74 63 68 20 74 68 69 73 20 70 72 6f 62 6c  catch this probl
c7540 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69  em at compile-ti
c7550 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45  me..*/.#ifdef ME
c7560 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72 72  MORY_DEBUG.# err
c7570 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44  or "The MEMORY_D
c7580 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f 62  EBUG macro is ob
c7590 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51 4c  solete.  Use SQL
c75a0 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65 61  ITE_DEBUG instea
c75b0 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  d.".#endif..#ifd
c75c0 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
c75d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
c75e0 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63  nt sqlite3OSTrac
c75f0 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f  e = 0;.#define O
c7600 53 54 52 41 43 45 31 28 58 29 20 20 20 20 20 20  STRACE1(X)      
c7610 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53     if( sqlite3OS
c7620 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44  Trace ) sqlite3D
c7630 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23 64  ebugPrintf(X).#d
c7640 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58  efine OSTRACE2(X
c7650 2c 59 29 20 20 20 20 20 20 20 69 66 28 20 73 71  ,Y)       if( sq
c7660 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73  lite3OSTrace ) s
c7670 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
c7680 66 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f  f(X,Y).#define O
c7690 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20 20  STRACE3(X,Y,Z)  
c76a0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53     if( sqlite3OS
c76b0 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44  Trace ) sqlite3D
c76c0 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a  ebugPrintf(X,Y,Z
c76d0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
c76e0 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66  E4(X,Y,Z,A)   if
c76f0 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  ( sqlite3OSTrace
c7700 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50   ) sqlite3DebugP
c7710 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23  rintf(X,Y,Z,A).#
c7720 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28  define OSTRACE5(
c7730 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73  X,Y,Z,A,B) if( s
c7740 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20  qlite3OSTrace ) 
c7750 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
c7760 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64  tf(X,Y,Z,A,B).#d
c7770 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58  efine OSTRACE6(X
c7780 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20  ,Y,Z,A,B,C) \.  
c7790 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72    if(sqlite3OSTr
c77a0 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75  ace) sqlite3Debu
c77b0 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c  gPrintf(X,Y,Z,A,
c77c0 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  B,C).#define OST
c77d0 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c  RACE7(X,Y,Z,A,B,
c77e0 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73 71  C,D) \.    if(sq
c77f0 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 71  lite3OSTrace) sq
c7800 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
c7810 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a  (X,Y,Z,A,B,C,D).
c7820 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 53  #else.#define OS
c7830 54 52 41 43 45 31 28 58 29 0a 23 64 65 66 69 6e  TRACE1(X).#defin
c7840 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 0a  e OSTRACE2(X,Y).
c7850 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33  #define OSTRACE3
c7860 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20  (X,Y,Z).#define 
c7870 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41  OSTRACE4(X,Y,Z,A
c7880 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
c7890 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64  E5(X,Y,Z,A,B).#d
c78a0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58  efine OSTRACE6(X
c78b0 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66  ,Y,Z,A,B,C).#def
c78c0 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59  ine OSTRACE7(X,Y
c78d0 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64  ,Z,A,B,C,D).#end
c78e0 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73  if../*.** Macros
c78f0 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65   for performance
c7900 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61   tracing.  Norma
c7910 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 2e 20  lly turned off. 
c7920 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f   Only works.** o
c7930 6e 20 69 34 38 36 20 68 61 72 64 77 61 72 65 2e  n i486 hardware.
c7940 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
c7950 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52  E_PERFORMANCE_TR
c7960 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69  ACE../* .** hwti
c7970 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e  me.h contains in
c7980 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 63  line assembler c
c7990 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e  ode for implemen
c79a0 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65  ting .** high-pe
c79b0 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67  rformance timing
c79c0 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a   routines..*/./*
c79d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
c79e0 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20 69  clude hwtime.h i
c79f0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
c7a00 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a  os_common.h ****
c7a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
c7a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
c7a30 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 2e  gin file hwtime.
c7a40 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
c7a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
c7a70 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37 0a  .** 2008 May 27.
c7a80 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
c7a90 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
c7aa0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
c7ab0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
c7ac0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
c7ad0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
c7ae0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
c7af0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
c7b00 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
c7b10 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
c7b20 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
c7b30 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
c7b40 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
c7b50 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
c7b60 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
c7b70 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
c7b80 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
c7b90 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
c7ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7be0 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ***.**.** This f
c7bf0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c  ile contains inl
c7c00 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f 72  ine asm code for
c7c10 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 69 67   retrieving "hig
c7c20 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a  h-performance".*
c7c30 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 78  * counters for x
c7c40 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a  86 class CPUs..*
c7c50 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d 65  *.** $Id: hwtime
c7c60 2e 68 2c 76 20 31 2e 33 20 32 30 30 38 2f 30 38  .h,v 1.3 2008/08
c7c70 2f 30 31 20 31 34 3a 33 33 3a 31 35 20 73 68 61  /01 14:33:15 sha
c7c80 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e  ne Exp $.*/.#ifn
c7c90 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23  def _HWTIME_H_.#
c7ca0 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48  define _HWTIME_H
c7cb0 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  _../*.** The fol
c7cc0 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f  lowing routine o
c7cd0 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e  nly works on pen
c7ce0 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e  tium-class (or n
c7cf0 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72 73  ewer) processors
c7d00 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68 65  ..** It uses the
c7d10 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74 6f   RDTSC opcode to
c7d20 20 72 65 61 64 20 74 68 65 20 63 79 63 6c 65 20   read the cycle 
c7d30 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74 20  count value out 
c7d40 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73  of the.** proces
c7d50 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73 20  sor and returns 
c7d60 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68 69  that value.  Thi
c7d70 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f  s can be used fo
c7d80 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72  r high-res.** pr
c7d90 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20  ofiling..*/.#if 
c7da0 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f  (defined(__GNUC_
c7db0 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d  _) || defined(_M
c7dc0 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20 20  SC_VER)) && \.  
c7dd0 20 20 20 20 28 64 65 66 69 6e 65 64 28 69 33 38      (defined(i38
c7de0 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f  6) || defined(__
c7df0 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e  i386__) || defin
c7e00 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20  ed(_M_IX86))..  
c7e10 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e  #if defined(__GN
c7e20 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e  UC__)..  __inlin
c7e30 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  e__ sqlite_uint6
c7e40 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  4 sqlite3Hwtime(
c7e50 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73 69  void){.     unsi
c7e60 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b  gned int lo, hi;
c7e70 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f  .     __asm__ __
c7e80 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74  volatile__ ("rdt
c7e90 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c  sc" : "=a" (lo),
c7ea0 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20 20   "=d" (hi));.   
c7eb0 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65    return (sqlite
c7ec0 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 32  _uint64)hi << 32
c7ed0 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65   | lo;.  }..  #e
c7ee0 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43  lif defined(_MSC
c7ef0 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73  _VER)..  __decls
c7f00 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c  pec(naked) __inl
c7f10 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  ine sqlite_uint6
c7f20 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74 65  4 __cdecl sqlite
c7f30 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20  3Hwtime(void){. 
c7f40 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20      __asm {.    
c7f50 20 20 20 20 72 64 74 73 63 0a 20 20 20 20 20 20      rdtsc.      
c7f60 20 20 72 65 74 20 20 20 20 20 20 20 3b 20 72 65    ret       ; re
c7f70 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45 44  turn value at ED
c7f80 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d  X:EAX.     }.  }
c7f90 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69  ..  #endif..#eli
c7fa0 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55  f (defined(__GNU
c7fb0 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28  C__) && defined(
c7fc0 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20  __x86_64__))..  
c7fd0 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74  __inline__ sqlit
c7fe0 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e_uint64 sqlite3
c7ff0 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20  Hwtime(void){.  
c8000 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e      unsigned lon
c8010 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61  g val;.      __a
c8020 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f  sm__ __volatile_
c8030 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 41  _ ("rdtsc" : "=A
c8040 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20 20  " (val));.      
c8050 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a  return val;.  }.
c8060 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64   .#elif (defined
c8070 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65  (__GNUC__) && de
c8080 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a  fined(__ppc__)).
c8090 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71  .  __inline__ sq
c80a0 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69  lite_uint64 sqli
c80b0 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b  te3Hwtime(void){
c80c0 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  .      unsigned 
c80d0 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c  long long retval
c80e0 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64  ;.      unsigned
c80f0 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20   long junk;.    
c8100 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61    __asm__ __vola
c8110 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20  tile__ ("\n\.   
c8120 20 20 20 20 20 20 20 31 3a 20 20 20 20 20 20 6d         1:      m
c8130 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20  ftbu   %1\n\.   
c8140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d                 m
c8150 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20  ftb    %L0\n\.  
c8160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c8170 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20  mftbu   %0\n\.  
c8180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c8190 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c  cmpw    %0,%1\n\
c81a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
c81b0 20 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a 20     bne     1b". 
c81c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c81d0 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c 29   : "=r" (retval)
c81e0 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a  , "=r" (junk));.
c81f0 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 65 74        return ret
c8200 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a  val;.  }..#else.
c8210 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20 69  .  #error Need i
c8220 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
c8230 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29   sqlite3Hwtime()
c8240 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f   for your platfo
c8250 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54  rm...  /*.  ** T
c8260 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75  o compile withou
c8270 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73  t implementing s
c8280 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66  qlite3Hwtime() f
c8290 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d  or your platform
c82a0 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72  ,.  ** you can r
c82b0 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65 20  emove the above 
c82c0 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20 74  #error and use t
c82d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a  he following.  *
c82e0 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e  * stub function.
c82f0 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20    You will lose 
c8300 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20 66  timing support f
c8310 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20  or many.  ** of 
c8320 74 68 65 20 64 65 62 75 67 67 69 6e 67 20 61 6e  the debugging an
c8330 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69 74  d testing utilit
c8340 69 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f 75  ies, but it shou
c8350 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74  ld at.  ** least
c8360 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e   compile and run
c8370 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ..  */.SQLITE_PR
c8380 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f 75  IVATE   sqlite_u
c8390 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74  int64 sqlite3Hwt
c83a0 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72  ime(void){ retur
c83b0 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36  n ((sqlite_uint6
c83c0 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a  4)0); }..#endif.
c83d0 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
c83e0 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20  ned(_HWTIME_H_) 
c83f0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
c8400 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69 6d  *** End of hwtim
c8410 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
c8420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8440 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
c8450 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
c8460 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
c8470 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20   in os_common.h 
c8480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8490 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71 6c 69  **/..static sqli
c84a0 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 74 61 72  te_uint64 g_star
c84b0 74 3b 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65  t;.static sqlite
c84c0 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 70 73 65  _uint64 g_elapse
c84d0 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  d;.#define TIMER
c84e0 5f 53 54 41 52 54 20 20 20 20 20 20 20 67 5f 73  _START       g_s
c84f0 74 61 72 74 3d 73 71 6c 69 74 65 33 48 77 74 69  tart=sqlite3Hwti
c8500 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 54 49 4d  me().#define TIM
c8510 45 52 5f 45 4e 44 20 20 20 20 20 20 20 20 20 67  ER_END         g
c8520 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 74 65 33  _elapsed=sqlite3
c8530 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 72 74  Hwtime()-g_start
c8540 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45  .#define TIMER_E
c8550 4c 41 50 53 45 44 20 20 20 20 20 67 5f 65 6c 61  LAPSED     g_ela
c8560 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 65 66 69  psed.#else.#defi
c8570 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 0a 23  ne TIMER_START.#
c8580 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44  define TIMER_END
c8590 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45  .#define TIMER_E
c85a0 4c 41 50 53 45 44 20 20 20 20 20 28 28 73 71 6c  LAPSED     ((sql
c85b0 69 74 65 5f 75 69 6e 74 36 34 29 30 29 0a 23 65  ite_uint64)0).#e
c85c0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77  ndif../*.** If w
c85d0 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 74  e compile with t
c85e0 68 65 20 53 51 4c 49 54 45 5f 54 45 53 54 20 6d  he SQLITE_TEST m
c85f0 61 63 72 6f 20 73 65 74 2c 20 74 68 65 6e 20 74  acro set, then t
c8600 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f  he following blo
c8610 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69  ck.** of code wi
c8620 6c 6c 20 67 69 76 65 20 75 73 20 74 68 65 20 61  ll give us the a
c8630 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c 61  bility to simula
c8640 74 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72  te a disk I/O er
c8650 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73  ror.  This.** is
c8660 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
c8670 67 20 74 68 65 20 49 2f 4f 20 72 65 63 6f 76 65  g the I/O recove
c8680 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66  ry logic..*/.#if
c8690 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
c86a0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
c86b0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
c86c0 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  hit = 0;        
c86d0 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d      /* Total num
c86e0 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72  ber of I/O Error
c86f0 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  s */.SQLITE_API 
c8700 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
c8710 72 72 6f 72 5f 68 61 72 64 68 69 74 20 3d 20 30  rror_hardhit = 0
c8720 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ;        /* Numb
c8730 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e  er of non-benign
c8740 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54   errors */.SQLIT
c8750 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
c8760 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69  3_io_error_pendi
c8770 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  ng = 0;        /
c8780 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20  * Count down to 
c8790 66 69 72 73 74 20 49 2f 4f 20 65 72 72 6f 72 20  first I/O error 
c87a0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
c87b0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  t sqlite3_io_err
c87c0 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 30 3b 20  or_persist = 0; 
c87d0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
c87e0 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 70 65 72  f I/O errors per
c87f0 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41  sist */.SQLITE_A
c8800 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  PI int sqlite3_i
c8810 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d  o_error_benign =
c8820 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54   0;         /* T
c8830 72 75 65 20 69 66 20 65 72 72 6f 72 73 20 61 72  rue if errors ar
c8840 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49  e benign */.SQLI
c8850 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
c8860 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64  e3_diskfull_pend
c8870 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f  ing = 0;.SQLITE_
c8880 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
c8890 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64  diskfull = 0;.#d
c88a0 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f  efine SimulateIO
c88b0 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 20 73  ErrorBenign(X) s
c88c0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
c88d0 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65 66 69  benign=(X).#defi
c88e0 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  ne SimulateIOErr
c88f0 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69 66  or(CODE)  \.  if
c8900 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  ( (sqlite3_io_er
c8910 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26 20 73  ror_persist && s
c8920 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
c8930 68 69 74 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c  hit) \.       ||
c8940 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
c8950 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31  r_pending-- == 1
c8960 20 29 20 20 5c 0a 20 20 20 20 20 20 20 20 20 20   )  \.          
c8970 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72      { local_ioer
c8980 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61  r(); CODE; }.sta
c8990 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69  tic void local_i
c89a0 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 41 43  oerr(){.  IOTRAC
c89b0 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a  E(("IOERR\n"));.
c89c0 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72    sqlite3_io_err
c89d0 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20  or_hit++;.  if( 
c89e0 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f  !sqlite3_io_erro
c89f0 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c 69 74  r_benign ) sqlit
c8a00 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64  e3_io_error_hard
c8a10 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65  hit++;.}.#define
c8a20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c   SimulateDiskful
c8a30 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20  lError(CODE) \. 
c8a40 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69    if( sqlite3_di
c8a50 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29  skfull_pending )
c8a60 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 73 71 6c  { \.     if( sql
c8a70 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65  ite3_diskfull_pe
c8a80 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a  nding == 1 ){ \.
c8a90 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65         local_ioe
c8aa0 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 20 20 73  rr(); \.       s
c8ab0 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20  qlite3_diskfull 
c8ac0 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 73 71  = 1; \.       sq
c8ad0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68  lite3_io_error_h
c8ae0 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20  it = 1; \.      
c8af0 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65   CODE; \.     }e
c8b00 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 20 73 71  lse{ \.       sq
c8b10 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70  lite3_diskfull_p
c8b20 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20  ending--; \.    
c8b30 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a   } \.   }.#else.
c8b40 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65  #define Simulate
c8b50 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29  IOErrorBenign(X)
c8b60 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74  .#define Simulat
c8b70 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 64 65 66  eIOError(A).#def
c8b80 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b  ine SimulateDisk
c8b90 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e  fullError(A).#en
c8ba0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20  dif../*.** When 
c8bb0 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 20 61 20  testing, keep a 
c8bc0 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d  count of the num
c8bd0 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65  ber of open file
c8be0 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  s..*/.#ifdef SQL
c8bf0 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
c8c00 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
c8c10 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20  open_file_count 
c8c20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65  = 0;.#define Ope
c8c30 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 73 71 6c  nCounter(X)  sql
c8c40 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63  ite3_open_file_c
c8c50 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a  ount+=(X).#else.
c8c60 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e  #define OpenCoun
c8c70 74 65 72 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23  ter(X).#endif..#
c8c80 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
c8c90 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29  d(_OS_COMMON_H_)
c8ca0 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
c8cb0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63  **** End of os_c
c8cc0 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  ommon.h ********
c8cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8cf0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
c8d00 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
c8d10 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
c8d20 66 20 69 6e 20 6f 73 5f 75 6e 69 78 2e 63 20 2a  f in os_unix.c *
c8d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8d40 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69  ***/../*.** Defi
c8d50 6e 65 20 76 61 72 69 6f 75 73 20 6d 61 63 72 6f  ne various macro
c8d60 73 20 74 68 61 74 20 61 72 65 20 6d 69 73 73 69  s that are missi
c8d70 6e 67 20 66 72 6f 6d 20 73 6f 6d 65 20 73 79 73  ng from some sys
c8d80 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  tems..*/.#ifndef
c8d90 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 64   O_LARGEFILE.# d
c8da0 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c  efine O_LARGEFIL
c8db0 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  E 0.#endif.#ifde
c8dc0 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45  f SQLITE_DISABLE
c8dd0 5f 4c 46 53 0a 23 20 75 6e 64 65 66 20 4f 5f 4c  _LFS.# undef O_L
c8de0 41 52 47 45 46 49 4c 45 0a 23 20 64 65 66 69 6e  ARGEFILE.# defin
c8df0 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45 20 30 0a  e O_LARGEFILE 0.
c8e00 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4f  #endif.#ifndef O
c8e10 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 20 64 65 66 69  _NOFOLLOW.# defi
c8e20 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 20 30 0a  ne O_NOFOLLOW 0.
c8e30 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4f  #endif.#ifndef O
c8e40 5f 42 49 4e 41 52 59 0a 23 20 64 65 66 69 6e 65  _BINARY.# define
c8e50 20 4f 5f 42 49 4e 41 52 59 20 30 0a 23 65 6e 64   O_BINARY 0.#end
c8e60 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 44 4a  if../*.** The DJ
c8e70 47 50 50 20 63 6f 6d 70 69 6c 65 72 20 65 6e 76  GPP compiler env
c8e80 69 72 6f 6e 6d 65 6e 74 20 6c 6f 6f 6b 73 20 6d  ironment looks m
c8e90 6f 73 74 6c 79 20 6c 69 6b 65 20 55 6e 69 78 2c  ostly like Unix,
c8ea0 20 62 75 74 20 69 74 0a 2a 2a 20 6c 61 63 6b 73   but it.** lacks
c8eb0 20 74 68 65 20 66 63 6e 74 6c 28 29 20 73 79 73   the fcntl() sys
c8ec0 74 65 6d 20 63 61 6c 6c 2e 20 20 53 6f 20 72 65  tem call.  So re
c8ed0 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 29 20 74  define fcntl() t
c8ee0 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 0a 2a  o be something.*
c8ef0 2a 20 74 68 61 74 20 61 6c 77 61 79 73 20 73 75  * that always su
c8f00 63 63 65 65 64 73 2e 20 20 54 68 69 73 20 6d 65  cceeds.  This me
c8f10 61 6e 73 20 74 68 61 74 20 6c 6f 63 6b 69 6e 67  ans that locking
c8f20 20 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75 72 20   does not occur 
c8f30 75 6e 64 65 72 0a 2a 2a 20 44 4a 47 50 50 2e 20  under.** DJGPP. 
c8f40 20 42 75 74 20 69 74 20 69 73 20 44 4f 53 20 2d   But it is DOS -
c8f50 20 77 68 61 74 20 64 69 64 20 79 6f 75 20 65 78   what did you ex
c8f60 70 65 63 74 3f 0a 2a 2f 0a 23 69 66 64 65 66 20  pect?.*/.#ifdef 
c8f70 5f 5f 44 4a 47 50 50 5f 5f 0a 23 20 64 65 66 69  __DJGPP__.# defi
c8f80 6e 65 20 66 63 6e 74 6c 28 41 2c 42 2c 43 29 20  ne fcntl(A,B,C) 
c8f90 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
c8fa0 54 68 65 20 74 68 72 65 61 64 69 64 20 6d 61 63  The threadid mac
c8fb0 72 6f 20 72 65 73 6f 6c 76 65 73 20 74 6f 20 74  ro resolves to t
c8fc0 68 65 20 74 68 72 65 61 64 2d 69 64 20 6f 72 20  he thread-id or 
c8fd0 74 6f 20 30 2e 20 20 55 73 65 64 20 66 6f 72 0a  to 0.  Used for.
c8fe0 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64  ** testing and d
c8ff0 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a  ebugging only..*
c9000 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  /.#if SQLITE_THR
c9010 45 41 44 53 41 46 45 0a 23 64 65 66 69 6e 65 20  EADSAFE.#define 
c9020 74 68 72 65 61 64 69 64 20 70 74 68 72 65 61 64  threadid pthread
c9030 5f 73 65 6c 66 28 29 0a 23 65 6c 73 65 0a 23 64  _self().#else.#d
c9040 65 66 69 6e 65 20 74 68 72 65 61 64 69 64 20 30  efine threadid 0
c9050 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
c9060 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 73  Helper functions
c9070 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 72   to obtain and r
c9080 65 6c 69 6e 71 75 69 73 68 20 74 68 65 20 67 6c  elinquish the gl
c9090 6f 62 61 6c 20 6d 75 74 65 78 2e 20 54 68 65 0a  obal mutex. The.
c90a0 2a 2a 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20  ** global mutex 
c90b0 69 73 20 75 73 65 64 20 74 6f 20 70 72 6f 74 65  is used to prote
c90c0 63 74 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43  ct the unixOpenC
c90d0 6e 74 2c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f  nt, unixLockInfo
c90e0 20 61 6e 64 0a 2a 2a 20 76 78 77 6f 72 6b 73 46   and.** vxworksF
c90f0 69 6c 65 49 64 20 6f 62 6a 65 63 74 73 20 75 73  ileId objects us
c9100 65 64 20 62 79 20 74 68 69 73 20 66 69 6c 65 2c  ed by this file,
c9110 20 61 6c 6c 20 6f 66 20 77 68 69 63 68 20 6d 61   all of which ma
c9120 79 20 62 65 20 0a 2a 2a 20 73 68 61 72 65 64 20  y be .** shared 
c9130 62 79 20 6d 75 6c 74 69 70 6c 65 20 74 68 72 65  by multiple thre
c9140 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74  ads..**.** Funct
c9150 69 6f 6e 20 75 6e 69 78 4d 75 74 65 78 48 65 6c  ion unixMutexHel
c9160 64 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 61  d() is used to a
c9170 73 73 65 72 74 28 29 20 74 68 61 74 20 74 68 65  ssert() that the
c9180 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 0a 2a   global mutex .*
c9190 2a 20 69 73 20 68 65 6c 64 20 77 68 65 6e 20 72  * is held when r
c91a0 65 71 75 69 72 65 64 2e 20 54 68 69 73 20 66 75  equired. This fu
c91b0 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75  nction is only u
c91c0 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61  sed as part of a
c91d0 73 73 65 72 74 28 29 20 0a 2a 2a 20 73 74 61 74  ssert() .** stat
c91e0 65 6d 65 6e 74 73 2e 20 65 2e 67 2e 0a 2a 2a 0a  ements. e.g..**.
c91f0 2a 2a 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75  **   unixEnterMu
c9200 74 65 78 28 29 0a 2a 2a 20 20 20 20 20 61 73 73  tex().**     ass
c9210 65 72 74 28 20 75 6e 69 78 4d 75 74 65 78 48 65  ert( unixMutexHe
c9220 6c 64 28 29 20 29 3b 0a 2a 2a 20 20 20 75 6e 69  ld() );.**   uni
c9230 78 45 6e 74 65 72 4c 65 61 76 65 28 29 0a 2a 2f  xEnterLeave().*/
c9240 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69  .static void uni
c9250 78 45 6e 74 65 72 4d 75 74 65 78 28 76 6f 69 64  xEnterMutex(void
c9260 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ){.  sqlite3_mut
c9270 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33  ex_enter(sqlite3
c9280 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
c9290 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
c92a0 41 53 54 45 52 29 29 3b 0a 7d 0a 73 74 61 74 69  ASTER));.}.stati
c92b0 63 20 76 6f 69 64 20 75 6e 69 78 4c 65 61 76 65  c void unixLeave
c92c0 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73  Mutex(void){.  s
c92d0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
c92e0 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41  ve(sqlite3MutexA
c92f0 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
c9300 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
c9310 29 3b 0a 7d 0a 23 69 66 64 65 66 20 53 51 4c 49  );.}.#ifdef SQLI
c9320 54 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63 20  TE_DEBUG.static 
c9330 69 6e 74 20 75 6e 69 78 4d 75 74 65 78 48 65 6c  int unixMutexHel
c9340 64 28 76 6f 69 64 29 20 7b 0a 20 20 72 65 74 75  d(void) {.  retu
c9350 72 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  rn sqlite3_mutex
c9360 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 4d 75 74  _held(sqlite3Mut
c9370 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
c9380 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
c9390 45 52 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  ER));.}.#endif..
c93a0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
c93b0 45 42 55 47 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65  EBUG./*.** Helpe
c93c0 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 70  r function for p
c93d0 72 69 6e 74 69 6e 67 20 6f 75 74 20 74 72 61 63  rinting out trac
c93e0 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72  e information fr
c93f0 6f 6d 20 64 65 62 75 67 67 69 6e 67 0a 2a 2a 20  om debugging.** 
c9400 62 69 6e 61 72 69 65 73 2e 20 54 68 69 73 20 72  binaries. This r
c9410 65 74 75 72 6e 73 20 74 68 65 20 73 74 72 69 6e  eturns the strin
c9420 67 20 72 65 70 72 65 73 65 74 61 74 69 6f 6e 20  g represetation 
c9430 6f 66 20 74 68 65 20 73 75 70 70 6c 69 65 64 0a  of the supplied.
c9440 2a 2a 20 69 6e 74 65 67 65 72 20 6c 6f 63 6b 2d  ** integer lock-
c9450 74 79 70 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  type..*/.static 
c9460 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 6f 63 6b  const char *lock
c9470 74 79 70 65 4e 61 6d 65 28 69 6e 74 20 6c 6f 63  typeName(int loc
c9480 6b 74 79 70 65 29 7b 0a 20 20 73 77 69 74 63 68  ktype){.  switch
c9490 28 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20  ( locktype ){.  
c94a0 20 20 63 61 73 65 20 4e 4f 5f 4c 4f 43 4b 3a 20    case NO_LOCK: 
c94b0 72 65 74 75 72 6e 20 22 4e 4f 4e 45 22 3b 0a 20  return "NONE";. 
c94c0 20 20 20 63 61 73 65 20 53 48 41 52 45 44 5f 4c     case SHARED_L
c94d0 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 53 48 41  OCK: return "SHA
c94e0 52 45 44 22 3b 0a 20 20 20 20 63 61 73 65 20 52  RED";.    case R
c94f0 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 20 72 65  ESERVED_LOCK: re
c9500 74 75 72 6e 20 22 52 45 53 45 52 56 45 44 22 3b  turn "RESERVED";
c9510 0a 20 20 20 20 63 61 73 65 20 50 45 4e 44 49 4e  .    case PENDIN
c9520 47 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22  G_LOCK: return "
c9530 50 45 4e 44 49 4e 47 22 3b 0a 20 20 20 20 63 61  PENDING";.    ca
c9540 73 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  se EXCLUSIVE_LOC
c9550 4b 3a 20 72 65 74 75 72 6e 20 22 45 58 43 4c 55  K: return "EXCLU
c9560 53 49 56 45 22 3b 0a 20 20 7d 0a 20 20 72 65 74  SIVE";.  }.  ret
c9570 75 72 6e 20 22 45 52 52 4f 52 22 3b 0a 7d 0a 23  urn "ERROR";.}.#
c9580 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
c9590 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 0a  LITE_LOCK_TRACE.
c95a0 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 6f 75 74 20  /*.** Print out 
c95b0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
c95c0 74 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6f 70  t all locking op
c95d0 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20  erations..**.** 
c95e0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
c95f0 75 73 65 64 20 66 6f 72 20 74 72 6f 75 62 6c 65  used for trouble
c9600 73 68 6f 6f 74 69 6e 67 20 6c 6f 63 6b 73 20 6f  shooting locks o
c9610 6e 20 6d 75 6c 74 69 74 68 72 65 61 64 65 64 0a  n multithreaded.
c9620 2a 2a 20 70 6c 61 74 66 6f 72 6d 73 2e 20 20 45  ** platforms.  E
c9630 6e 61 62 6c 65 20 62 79 20 63 6f 6d 70 69 6c 69  nable by compili
c9640 6e 67 20 77 69 74 68 20 74 68 65 20 2d 44 53 51  ng with the -DSQ
c9650 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 0a  LITE_LOCK_TRACE.
c9660 2a 2a 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20  ** command-line 
c9670 6f 70 74 69 6f 6e 20 6f 6e 20 74 68 65 20 63 6f  option on the co
c9680 6d 70 69 6c 65 72 2e 20 20 54 68 69 73 20 63 6f  mpiler.  This co
c9690 64 65 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 0a 2a  de is normally.*
c96a0 2a 20 74 75 72 6e 65 64 20 6f 66 66 2e 0a 2a 2f  * turned off..*/
c96b0 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b  .static int lock
c96c0 54 72 61 63 65 28 69 6e 74 20 66 64 2c 20 69 6e  Trace(int fd, in
c96d0 74 20 6f 70 2c 20 73 74 72 75 63 74 20 66 6c 6f  t op, struct flo
c96e0 63 6b 20 2a 70 29 7b 0a 20 20 63 68 61 72 20 2a  ck *p){.  char *
c96f0 7a 4f 70 4e 61 6d 65 2c 20 2a 7a 54 79 70 65 3b  zOpName, *zType;
c9700 0a 20 20 69 6e 74 20 73 3b 0a 20 20 69 6e 74 20  .  int s;.  int 
c9710 73 61 76 65 64 45 72 72 6e 6f 3b 0a 20 20 69 66  savedErrno;.  if
c9720 28 20 6f 70 3d 3d 46 5f 47 45 54 4c 4b 20 29 7b  ( op==F_GETLK ){
c9730 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 22  .    zOpName = "
c9740 47 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 20  GETLK";.  }else 
c9750 69 66 28 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20  if( op==F_SETLK 
c9760 29 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20 3d  ){.    zOpName =
c9770 20 22 53 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73   "SETLK";.  }els
c9780 65 7b 0a 20 20 20 20 73 20 3d 20 66 63 6e 74 6c  e{.    s = fcntl
c9790 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a 20 20 20  (fd, op, p);.   
c97a0 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
c97b0 6e 74 66 28 22 66 63 6e 74 6c 20 75 6e 6b 6e 6f  ntf("fcntl unkno
c97c0 77 6e 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20  wn %d %d %d\n", 
c97d0 66 64 2c 20 6f 70 2c 20 73 29 3b 0a 20 20 20 20  fd, op, s);.    
c97e0 72 65 74 75 72 6e 20 73 3b 0a 20 20 7d 0a 20 20  return s;.  }.  
c97f0 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46  if( p->l_type==F
c9800 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54  _RDLCK ){.    zT
c9810 79 70 65 20 3d 20 22 52 44 4c 43 4b 22 3b 0a 20  ype = "RDLCK";. 
c9820 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6c 5f   }else if( p->l_
c9830 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b  type==F_WRLCK ){
c9840 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22 57 52  .    zType = "WR
c9850 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66  LCK";.  }else if
c9860 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 55  ( p->l_type==F_U
c9870 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 79 70  NLCK ){.    zTyp
c9880 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20 20 7d  e = "UNLCK";.  }
c9890 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
c98a0 28 20 30 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73  ( 0 );.  }.  ass
c98b0 65 72 74 28 20 70 2d 3e 6c 5f 77 68 65 6e 63 65  ert( p->l_whence
c98c0 3d 3d 53 45 45 4b 5f 53 45 54 20 29 3b 0a 20 20  ==SEEK_SET );.  
c98d0 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 6f 70  s = fcntl(fd, op
c98e0 2c 20 70 29 3b 0a 20 20 73 61 76 65 64 45 72 72  , p);.  savedErr
c98f0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 73 71  no = errno;.  sq
c9900 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
c9910 28 22 66 63 6e 74 6c 20 25 64 20 25 64 20 25 73  ("fcntl %d %d %s
c9920 20 25 73 20 25 64 20 25 64 20 25 64 20 25 64 5c   %s %d %d %d %d\
c9930 6e 22 2c 0a 20 20 20 20 20 74 68 72 65 61 64 69  n",.     threadi
c9940 64 2c 20 66 64 2c 20 7a 4f 70 4e 61 6d 65 2c 20  d, fd, zOpName, 
c9950 7a 54 79 70 65 2c 20 28 69 6e 74 29 70 2d 3e 6c  zType, (int)p->l
c9960 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 70 2d 3e  _start, (int)p->
c9970 6c 5f 6c 65 6e 2c 0a 20 20 20 20 20 28 69 6e 74  l_len,.     (int
c9980 29 70 2d 3e 6c 5f 70 69 64 2c 20 73 29 3b 0a 20  )p->l_pid, s);. 
c9990 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 26 26 20   if( s==(-1) && 
c99a0 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 26 26 20 28  op==F_SETLK && (
c99b0 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c  p->l_type==F_RDL
c99c0 43 4b 20 7c 7c 20 70 2d 3e 6c 5f 74 79 70 65 3d  CK || p->l_type=
c99d0 3d 46 5f 57 52 4c 43 4b 29 20 29 7b 0a 20 20 20  =F_WRLCK) ){.   
c99e0 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 32   struct flock l2
c99f0 3b 0a 20 20 20 20 6c 32 20 3d 20 2a 70 3b 0a 20  ;.    l2 = *p;. 
c9a00 20 20 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47     fcntl(fd, F_G
c9a10 45 54 4c 4b 2c 20 26 6c 32 29 3b 0a 20 20 20 20  ETLK, &l2);.    
c9a20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46  if( l2.l_type==F
c9a30 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20  _RDLCK ){.      
c9a40 7a 54 79 70 65 20 3d 20 22 52 44 4c 43 4b 22 3b  zType = "RDLCK";
c9a50 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c  .    }else if( l
c9a60 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43  2.l_type==F_WRLC
c9a70 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 65  K ){.      zType
c9a80 20 3d 20 22 57 52 4c 43 4b 22 3b 0a 20 20 20 20   = "WRLCK";.    
c9a90 7d 65 6c 73 65 20 69 66 28 20 6c 32 2e 6c 5f 74  }else if( l2.l_t
c9aa0 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a  ype==F_UNLCK ){.
c9ab0 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 55        zType = "U
c9ac0 4e 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65  NLCK";.    }else
c9ad0 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
c9ae0 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  0 );.    }.    s
c9af0 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
c9b00 66 28 22 66 63 6e 74 6c 2d 66 61 69 6c 75 72 65  f("fcntl-failure
c9b10 2d 72 65 61 73 6f 6e 3a 20 25 73 20 25 64 20 25  -reason: %s %d %
c9b20 64 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20  d %d\n",.       
c9b30 7a 54 79 70 65 2c 20 28 69 6e 74 29 6c 32 2e 6c  zType, (int)l2.l
c9b40 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 6c 32 2e  _start, (int)l2.
c9b50 6c 5f 6c 65 6e 2c 20 28 69 6e 74 29 6c 32 2e 6c  l_len, (int)l2.l
c9b60 5f 70 69 64 29 3b 0a 20 20 7d 0a 20 20 65 72 72  _pid);.  }.  err
c9b70 6e 6f 20 3d 20 73 61 76 65 64 45 72 72 6e 6f 3b  no = savedErrno;
c9b80 0a 20 20 72 65 74 75 72 6e 20 73 3b 0a 7d 0a 23  .  return s;.}.#
c9b90 64 65 66 69 6e 65 20 66 63 6e 74 6c 20 6c 6f 63  define fcntl loc
c9ba0 6b 54 72 61 63 65 0a 23 65 6e 64 69 66 20 2f 2a  kTrace.#endif /*
c9bb0 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41   SQLITE_LOCK_TRA
c9bc0 43 45 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54  CE */..../*.** T
c9bd0 68 69 73 20 72 6f 75 74 69 6e 65 20 74 72 61 6e  his routine tran
c9be0 73 6c 61 74 65 73 20 61 20 73 74 61 6e 64 61 72  slates a standar
c9bf0 64 20 50 4f 53 49 58 20 65 72 72 6e 6f 20 63 6f  d POSIX errno co
c9c00 64 65 20 69 6e 74 6f 20 73 6f 6d 65 74 68 69 6e  de into somethin
c9c10 67 0a 2a 2a 20 75 73 65 66 75 6c 20 74 6f 20 74  g.** useful to t
c9c20 68 65 20 63 6c 69 65 6e 74 73 20 6f 66 20 74 68  he clients of th
c9c30 65 20 73 71 6c 69 74 65 33 20 66 75 6e 63 74 69  e sqlite3 functi
c9c40 6f 6e 73 2e 20 20 53 70 65 63 69 66 69 63 61 6c  ons.  Specifical
c9c50 6c 79 2c 20 69 74 20 69 73 0a 2a 2a 20 69 6e 74  ly, it is.** int
c9c60 65 6e 64 65 64 20 74 6f 20 74 72 61 6e 73 6c 61  ended to transla
c9c70 74 65 20 61 20 76 61 72 69 65 74 79 20 6f 66 20  te a variety of 
c9c80 22 74 72 79 20 61 67 61 69 6e 22 20 65 72 72 6f  "try again" erro
c9c90 72 73 20 69 6e 74 6f 20 53 51 4c 49 54 45 5f 42  rs into SQLITE_B
c9ca0 55 53 59 0a 2a 2a 20 61 6e 64 20 61 20 76 61 72  USY.** and a var
c9cb0 69 65 74 79 20 6f 66 20 22 70 6c 65 61 73 65 20  iety of "please 
c9cc0 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 64  close the file d
c9cd0 65 73 63 72 69 70 74 6f 72 20 4e 4f 57 22 20 65  escriptor NOW" e
c9ce0 72 72 6f 72 73 20 69 6e 74 6f 20 0a 2a 2a 20 53  rrors into .** S
c9cf0 51 4c 49 54 45 5f 49 4f 45 52 52 0a 2a 2a 20 0a  QLITE_IOERR.** .
c9d00 2a 2a 20 45 72 72 6f 72 73 20 64 75 72 69 6e 67  ** Errors during
c9d10 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
c9d20 6f 66 20 6c 6f 63 6b 73 2c 20 6f 72 20 66 69 6c  of locks, or fil
c9d30 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74  e system support
c9d40 20 66 6f 72 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 73   for locks,.** s
c9d50 68 6f 75 6c 64 20 68 61 6e 64 6c 65 20 45 4e 4f  hould handle ENO
c9d60 4c 43 4b 2c 20 45 4e 4f 54 53 55 50 2c 20 45 4f  LCK, ENOTSUP, EO
c9d70 50 4e 4f 54 53 55 50 50 20 73 65 70 61 72 61 74  PNOTSUPP separat
c9d80 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ely..*/.static i
c9d90 6e 74 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72  nt sqliteErrorFr
c9da0 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 69 6e 74  omPosixError(int
c9db0 20 70 6f 73 69 78 45 72 72 6f 72 2c 20 69 6e 74   posixError, int
c9dc0 20 73 71 6c 69 74 65 49 4f 45 72 72 29 20 7b 0a   sqliteIOErr) {.
c9dd0 20 20 73 77 69 74 63 68 20 28 70 6f 73 69 78 45    switch (posixE
c9de0 72 72 6f 72 29 20 7b 0a 20 20 63 61 73 65 20 30  rror) {.  case 0
c9df0 3a 20 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  : .    return SQ
c9e00 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 0a 20 20  LITE_OK;.    .  
c9e10 63 61 73 65 20 45 41 47 41 49 4e 3a 0a 20 20 63  case EAGAIN:.  c
c9e20 61 73 65 20 45 54 49 4d 45 44 4f 55 54 3a 0a 20  ase ETIMEDOUT:. 
c9e30 20 63 61 73 65 20 45 42 55 53 59 3a 0a 20 20 63   case EBUSY:.  c
c9e40 61 73 65 20 45 49 4e 54 52 3a 0a 20 20 63 61 73  ase EINTR:.  cas
c9e50 65 20 45 4e 4f 4c 43 4b 3a 20 20 0a 20 20 20 20  e ENOLCK:  .    
c9e60 2f 2a 20 72 61 6e 64 6f 6d 20 4e 46 53 20 72 65  /* random NFS re
c9e70 74 72 79 20 65 72 72 6f 72 2c 20 75 6e 6c 65 73  try error, unles
c9e80 73 20 64 75 72 69 6e 67 20 66 69 6c 65 20 73 79  s during file sy
c9e90 73 74 65 6d 20 73 75 70 70 6f 72 74 20 0a 20 20  stem support .  
c9ea0 20 20 20 2a 20 69 6e 74 72 6f 73 70 65 63 74 69     * introspecti
c9eb0 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 69 74 20  on, in which it 
c9ec0 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77  actually means w
c9ed0 68 61 74 20 69 74 20 73 61 79 73 20 2a 2f 0a 20  hat it says */. 
c9ee0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
c9ef0 5f 42 55 53 59 3b 0a 20 20 20 20 0a 20 20 63 61  _BUSY;.    .  ca
c9f00 73 65 20 45 41 43 43 45 53 3a 20 0a 20 20 20 20  se EACCES: .    
c9f10 2f 2a 20 45 41 43 43 45 53 20 69 73 20 6c 69 6b  /* EACCES is lik
c9f20 65 20 45 41 47 41 49 4e 20 64 75 72 69 6e 67 20  e EAGAIN during 
c9f30 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f  locking operatio
c9f40 6e 73 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20  ns, but not any 
c9f50 6f 74 68 65 72 20 74 69 6d 65 2a 2f 0a 20 20 20  other time*/.   
c9f60 20 69 66 28 20 28 73 71 6c 69 74 65 49 4f 45 72   if( (sqliteIOEr
c9f70 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  r == SQLITE_IOER
c9f80 52 5f 4c 4f 43 4b 29 20 7c 7c 20 0a 09 28 73 71  R_LOCK) || ..(sq
c9f90 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c  liteIOErr == SQL
c9fa0 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b  ITE_IOERR_UNLOCK
c9fb0 29 20 7c 7c 20 0a 09 28 73 71 6c 69 74 65 49 4f  ) || ..(sqliteIO
c9fc0 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f  Err == SQLITE_IO
c9fd0 45 52 52 5f 52 44 4c 4f 43 4b 29 20 7c 7c 0a 09  ERR_RDLOCK) ||..
c9fe0 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20  (sqliteIOErr == 
c9ff0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45  SQLITE_IOERR_CHE
ca000 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 20  CKRESERVEDLOCK) 
ca010 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
ca020 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20  SQLITE_BUSY;.   
ca030 20 7d 0a 20 20 20 20 2f 2a 20 65 6c 73 65 20 66   }.    /* else f
ca040 61 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20  all through */. 
ca050 20 63 61 73 65 20 45 50 45 52 4d 3a 20 0a 20 20   case EPERM: .  
ca060 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
ca070 50 45 52 4d 3b 0a 20 20 20 20 0a 20 20 63 61 73  PERM;.    .  cas
ca080 65 20 45 44 45 41 44 4c 4b 3a 0a 20 20 20 20 72  e EDEADLK:.    r
ca090 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
ca0a0 52 52 5f 42 4c 4f 43 4b 45 44 3b 0a 20 20 20 20  RR_BLOCKED;.    
ca0b0 0a 23 69 66 20 45 4f 50 4e 4f 54 53 55 50 50 21  .#if EOPNOTSUPP!
ca0c0 3d 45 4e 4f 54 53 55 50 0a 20 20 63 61 73 65 20  =ENOTSUP.  case 
ca0d0 45 4f 50 4e 4f 54 53 55 50 50 3a 20 0a 20 20 20  EOPNOTSUPP: .   
ca0e0 20 2f 2a 20 73 6f 6d 65 74 68 69 6e 67 20 77 65   /* something we
ca0f0 6e 74 20 74 65 72 72 69 62 6c 79 20 61 77 72 79  nt terribly awry
ca100 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 20  , unless during 
ca110 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70  file system supp
ca120 6f 72 74 20 0a 20 20 20 20 20 2a 20 69 6e 74 72  ort .     * intr
ca130 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77 68  ospection, in wh
ca140 69 63 68 20 69 74 20 61 63 74 75 61 6c 6c 79 20  ich it actually 
ca150 6d 65 61 6e 73 20 77 68 61 74 20 69 74 20 73 61  means what it sa
ca160 79 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66  ys */.#endif.#if
ca170 64 65 66 20 45 4e 4f 54 53 55 50 0a 20 20 63 61  def ENOTSUP.  ca
ca180 73 65 20 45 4e 4f 54 53 55 50 3a 20 0a 20 20 20  se ENOTSUP: .   
ca190 20 2f 2a 20 69 6e 76 61 6c 69 64 20 66 64 2c 20   /* invalid fd, 
ca1a0 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 20 66 69  unless during fi
ca1b0 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72  le system suppor
ca1c0 74 20 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c  t introspection,
ca1d0 20 69 6e 20 77 68 69 63 68 20 0a 20 20 20 20 20   in which .     
ca1e0 2a 20 69 74 20 61 63 74 75 61 6c 6c 79 20 6d 65  * it actually me
ca1f0 61 6e 73 20 77 68 61 74 20 69 74 20 73 61 79 73  ans what it says
ca200 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 61 73   */.#endif.  cas
ca210 65 20 45 49 4f 3a 0a 20 20 63 61 73 65 20 45 42  e EIO:.  case EB
ca220 41 44 46 3a 0a 20 20 63 61 73 65 20 45 49 4e 56  ADF:.  case EINV
ca230 41 4c 3a 0a 20 20 63 61 73 65 20 45 4e 4f 54 43  AL:.  case ENOTC
ca240 4f 4e 4e 3a 0a 20 20 63 61 73 65 20 45 4e 4f 44  ONN:.  case ENOD
ca250 45 56 3a 0a 20 20 63 61 73 65 20 45 4e 58 49 4f  EV:.  case ENXIO
ca260 3a 0a 20 20 63 61 73 65 20 45 4e 4f 45 4e 54 3a  :.  case ENOENT:
ca270 0a 20 20 63 61 73 65 20 45 53 54 41 4c 45 3a 0a  .  case ESTALE:.
ca280 20 20 63 61 73 65 20 45 4e 4f 53 59 53 3a 0a 20    case ENOSYS:. 
ca290 20 20 20 2f 2a 20 74 68 65 73 65 20 73 68 6f 75     /* these shou
ca2a0 6c 64 20 66 6f 72 63 65 20 74 68 65 20 63 6c 69  ld force the cli
ca2b0 65 6e 74 20 74 6f 20 63 6c 6f 73 65 20 74 68 65  ent to close the
ca2c0 20 66 69 6c 65 20 61 6e 64 20 72 65 63 6f 6e 6e   file and reconn
ca2d0 65 63 74 20 2a 2f 0a 20 20 20 20 0a 20 20 64 65  ect */.    .  de
ca2e0 66 61 75 6c 74 3a 20 0a 20 20 20 20 72 65 74 75  fault: .    retu
ca2f0 72 6e 20 73 71 6c 69 74 65 49 4f 45 72 72 3b 0a  rn sqliteIOErr;.
ca300 20 20 7d 0a 7d 0a 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a    }.}..../******
ca310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ca320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ca330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ca340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ca350 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  ********.*******
ca360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
ca370 6e 20 55 6e 69 71 75 65 20 46 69 6c 65 20 49 44  n Unique File ID
ca380 20 55 74 69 6c 69 74 79 20 55 73 65 64 20 42 79   Utility Used By
ca390 20 56 78 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a   VxWorks *******
ca3a0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4f  ********.**.** O
ca3b0 6e 20 6d 6f 73 74 20 76 65 72 73 69 6f 6e 73 20  n most versions 
ca3c0 6f 66 20 75 6e 69 78 2c 20 77 65 20 63 61 6e 20  of unix, we can 
ca3d0 67 65 74 20 61 20 75 6e 69 71 75 65 20 49 44 20  get a unique ID 
ca3e0 66 6f 72 20 61 20 66 69 6c 65 20 62 79 20 63 6f  for a file by co
ca3f0 6e 63 61 74 65 6e 61 74 69 6e 67 0a 2a 2a 20 74  ncatenating.** t
ca400 68 65 20 64 65 76 69 63 65 20 6e 75 6d 62 65 72  he device number
ca410 20 61 6e 64 20 74 68 65 20 69 6e 6f 64 65 20 6e   and the inode n
ca420 75 6d 62 65 72 2e 20 20 42 75 74 20 74 68 69 73  umber.  But this
ca430 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f   does not work o
ca440 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 4f 6e  n VxWorks..** On
ca450 20 56 78 57 6f 72 6b 73 2c 20 61 20 75 6e 69 71   VxWorks, a uniq
ca460 75 65 20 66 69 6c 65 20 69 64 20 6d 75 73 74 20  ue file id must 
ca470 62 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20  be based on the 
ca480 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61  canonical filena
ca490 6d 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e  me..**.** A poin
ca4a0 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e  ter to an instan
ca4b0 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
ca4c0 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 61  ing structure ca
ca4d0 6e 20 62 65 20 75 73 65 64 20 61 73 20 61 0a 2a  n be used as a.*
ca4e0 2a 20 75 6e 69 71 75 65 20 66 69 6c 65 20 49 44  * unique file ID
ca4f0 20 69 6e 20 56 78 57 6f 72 6b 73 2e 20 20 45 61   in VxWorks.  Ea
ca500 63 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  ch instance of t
ca510 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63 6f  his structure co
ca520 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 63 6f 70 79  ntains.** a copy
ca530 20 6f 66 20 74 68 65 20 63 61 6e 6f 6e 69 63 61   of the canonica
ca540 6c 20 66 69 6c 65 6e 61 6d 65 2e 20 20 54 68 65  l filename.  The
ca550 72 65 20 69 73 20 61 6c 73 6f 20 61 20 72 65 66  re is also a ref
ca560 65 72 65 6e 63 65 20 63 6f 75 6e 74 2e 20 20 0a  erence count.  .
ca570 2a 2a 20 54 68 65 20 73 74 72 75 63 74 75 72 65  ** The structure
ca580 20 69 73 20 72 65 63 6c 61 69 6d 65 64 20 77 68   is reclaimed wh
ca590 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  en the number of
ca5a0 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 69 74 20   pointers to it 
ca5b0 64 72 6f 70 73 20 74 6f 0a 2a 2a 20 7a 65 72 6f  drops to.** zero
ca5c0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72  ..**.** There ar
ca5d0 65 20 6e 65 76 65 72 20 76 65 72 79 20 6d 61 6e  e never very man
ca5e0 79 20 66 69 6c 65 73 20 6f 70 65 6e 20 61 74 20  y files open at 
ca5f0 6f 6e 65 20 74 69 6d 65 20 61 6e 64 20 6c 6f 6f  one time and loo
ca600 6b 75 70 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20  kups are not.** 
ca610 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 2d 63 72  a performance-cr
ca620 69 74 69 63 61 6c 20 70 61 74 68 2c 20 73 6f 20  itical path, so 
ca630 69 74 20 69 73 20 73 75 66 66 69 63 69 65 6e 74  it is sufficient
ca640 20 74 6f 20 70 75 74 20 74 68 65 73 65 0a 2a 2a   to put these.**
ca650 20 73 74 72 75 63 74 75 72 65 73 20 6f 6e 20 61   structures on a
ca660 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2f   linked list..*/
ca670 0a 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46  .struct vxworksF
ca680 69 6c 65 49 64 20 7b 0a 20 20 73 74 72 75 63 74  ileId {.  struct
ca690 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a   vxworksFileId *
ca6a0 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20  pNext;  /* Next 
ca6b0 69 6e 20 61 20 6c 69 73 74 20 6f 66 20 74 68 65  in a list of the
ca6c0 6d 20 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e  m all */.  int n
ca6d0 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Ref;            
ca6e0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
ca6f0 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73  er of references
ca700 20 74 6f 20 74 68 69 73 20 6f 6e 65 20 2a 2f 0a   to this one */.
ca710 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 20 20 20 20    int nName;    
ca720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca730 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65  /* Length of the
ca740 20 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 5b   zCanonicalName[
ca750 5d 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63 68  ] string */.  ch
ca760 61 72 20 2a 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61  ar *zCanonicalNa
ca770 6d 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43  me;         /* C
ca780 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d  anonical filenam
ca790 65 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 20 4f 53 5f  e */.};..#if OS_
ca7a0 56 58 57 4f 52 4b 53 0a 2f 2a 20 0a 2a 2a 20 41  VXWORKS./* .** A
ca7b0 6c 6c 20 75 6e 69 71 75 65 20 66 69 6c 65 6e 61  ll unique filena
ca7c0 6d 65 73 20 61 72 65 20 68 65 6c 64 20 6f 6e 20  mes are held on 
ca7d0 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68 65  a linked list he
ca7e0 61 64 65 64 20 62 79 20 74 68 69 73 0a 2a 2a 20  aded by this.** 
ca7f0 76 61 72 69 61 62 6c 65 3a 0a 2a 2f 0a 73 74 61  variable:.*/.sta
ca800 74 69 63 20 73 74 72 75 63 74 20 76 78 77 6f 72  tic struct vxwor
ca810 6b 73 46 69 6c 65 49 64 20 2a 76 78 77 6f 72 6b  ksFileId *vxwork
ca820 73 46 69 6c 65 4c 69 73 74 20 3d 20 30 3b 0a 0a  sFileList = 0;..
ca830 2f 2a 0a 2a 2a 20 53 69 6d 70 6c 69 66 79 20 61  /*.** Simplify a
ca840 20 66 69 6c 65 6e 61 6d 65 20 69 6e 74 6f 20 69   filename into i
ca850 74 73 20 63 61 6e 6f 6e 69 63 61 6c 20 66 6f 72  ts canonical for
ca860 6d 0a 2a 2a 20 62 79 20 6d 61 6b 69 6e 67 20 74  m.** by making t
ca870 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61  he following cha
ca880 6e 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20 72  nges:.**.**  * r
ca890 65 6d 6f 76 69 6e 67 20 61 6e 79 20 74 72 61 69  emoving any trai
ca8a0 6c 69 6e 67 20 61 6e 64 20 64 75 70 6c 69 63 61  ling and duplica
ca8b0 74 65 20 2f 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65  te /.**  * conve
ca8c0 72 74 20 2f 2e 2f 20 69 6e 74 6f 20 6a 75 73 74  rt /./ into just
ca8d0 20 2f 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65 72 74   /.**  * convert
ca8e0 20 2f 41 2f 2e 2e 2f 20 77 68 65 72 65 20 41 20   /A/../ where A 
ca8f0 69 73 20 61 6e 79 20 73 69 6d 70 6c 65 20 6e 61  is any simple na
ca900 6d 65 20 69 6e 74 6f 20 6a 75 73 74 20 2f 0a 2a  me into just /.*
ca910 2a 0a 2a 2a 20 43 68 61 6e 67 65 73 20 61 72 65  *.** Changes are
ca920 20 6d 61 64 65 20 69 6e 2d 70 6c 61 63 65 2e 20   made in-place. 
ca930 20 52 65 74 75 72 6e 20 74 68 65 20 6e 65 77 20   Return the new 
ca940 6e 61 6d 65 20 6c 65 6e 67 74 68 2e 0a 2a 2a 0a  name length..**.
ca950 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20  ** The original 
ca960 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 20 7a  filename is in z
ca970 5b 30 2e 2e 6e 2d 31 5d 2e 20 20 52 65 74 75 72  [0..n-1].  Retur
ca980 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a  n the number of.
ca990 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  ** characters in
ca9a0 20 74 68 65 20 73 69 6d 70 6c 69 66 69 65 64 20   the simplified 
ca9b0 6e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  name..*/.static 
ca9c0 69 6e 74 20 76 78 77 6f 72 6b 73 53 69 6d 70 6c  int vxworksSimpl
ca9d0 69 66 79 4e 61 6d 65 28 63 68 61 72 20 2a 7a 2c  ifyName(char *z,
ca9e0 20 69 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69   int n){.  int i
ca9f0 2c 20 6a 3b 0a 20 20 77 68 69 6c 65 28 20 6e 3e  , j;.  while( n>
caa00 31 20 26 26 20 7a 5b 6e 2d 31 5d 3d 3d 27 2f 27  1 && z[n-1]=='/'
caa10 20 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 66 6f 72   ){ n--; }.  for
caa20 28 69 3d 6a 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b  (i=j=0; i<n; i++
caa30 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d  ){.    if( z[i]=
caa40 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 69 66  ='/' ){.      if
caa50 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2f 27 20 29 20  ( z[i+1]=='/' ) 
caa60 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
caa70 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2e 27 20  if( z[i+1]=='.' 
caa80 26 26 20 69 2b 32 3c 6e 20 26 26 20 7a 5b 69 2b  && i+2<n && z[i+
caa90 32 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20  2]=='/' ){.     
caaa0 20 20 20 69 20 2b 3d 20 31 3b 0a 20 20 20 20 20     i += 1;.     
caab0 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
caac0 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 7a     }.      if( z
caad0 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26 20 69 2b  [i+1]=='.' && i+
caae0 33 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d 3d 3d 27  3<n && z[i+2]=='
caaf0 2e 27 20 26 26 20 7a 5b 69 2b 33 5d 3d 3d 27 2f  .' && z[i+3]=='/
cab00 27 20 29 7b 0a 20 20 20 20 20 20 20 20 77 68 69  ' ){.        whi
cab10 6c 65 28 20 6a 3e 30 20 26 26 20 7a 5b 6a 2d 31  le( j>0 && z[j-1
cab20 5d 21 3d 27 2f 27 20 29 7b 20 6a 2d 2d 3b 20 7d  ]!='/' ){ j--; }
cab30 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a 3e 30  .        if( j>0
cab40 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20 20   ){ j--; }.     
cab50 20 20 20 69 20 2b 3d 20 32 3b 0a 20 20 20 20 20     i += 2;.     
cab60 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
cab70 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a     }.    }.    z
cab80 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20  [j++] = z[i];.  
cab90 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20  }.  z[j] = 0;.  
caba0 72 65 74 75 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 0a  return j;.}../*.
cabb0 2a 2a 20 46 69 6e 64 20 61 20 75 6e 69 71 75 65  ** Find a unique
cabc0 20 66 69 6c 65 20 49 44 20 66 6f 72 20 74 68 65   file ID for the
cabd0 20 67 69 76 65 6e 20 61 62 73 6f 6c 75 74 65 20   given absolute 
cabe0 70 61 74 68 6e 61 6d 65 2e 20 20 52 65 74 75 72  pathname.  Retur
cabf0 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74  n.** a pointer t
cac00 6f 20 74 68 65 20 76 78 77 6f 72 6b 73 46 69 6c  o the vxworksFil
cac10 65 49 64 20 6f 62 6a 65 63 74 2e 20 20 54 68 69  eId object.  Thi
cac20 73 20 70 6f 69 6e 74 65 72 20 69 73 20 74 68 65  s pointer is the
cac30 20 75 6e 69 71 75 65 0a 2a 2a 20 66 69 6c 65 20   unique.** file 
cac40 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 52  ID..**.** The nR
cac50 65 66 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20  ef field of the 
cac60 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62  vxworksFileId ob
cac70 6a 65 63 74 20 69 73 20 69 6e 63 72 65 6d 65 6e  ject is incremen
cac80 74 65 64 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68  ted before.** th
cac90 65 20 6f 62 6a 65 63 74 20 69 73 20 72 65 74 75  e object is retu
caca0 72 6e 65 64 2e 20 20 41 20 6e 65 77 20 76 78 77  rned.  A new vxw
cacb0 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 63  orksFileId objec
cacc0 74 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20  t is created.** 
cacd0 61 6e 64 20 61 64 64 65 64 20 74 6f 20 74 68 65  and added to the
cace0 20 67 6c 6f 62 61 6c 20 6c 69 73 74 20 69 66 20   global list if 
cacf0 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a  necessary..**.**
cad00 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c   If a memory all
cad10 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63  ocation error oc
cad20 63 75 72 73 2c 20 72 65 74 75 72 6e 20 4e 55 4c  curs, return NUL
cad30 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72  L..*/.static str
cad40 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49  uct vxworksFileI
cad50 64 20 2a 76 78 77 6f 72 6b 73 46 69 6e 64 46 69  d *vxworksFindFi
cad60 6c 65 49 64 28 63 6f 6e 73 74 20 63 68 61 72 20  leId(const char 
cad70 2a 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 29 7b  *zAbsoluteName){
cad80 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b  .  struct vxwork
cad90 73 46 69 6c 65 49 64 20 2a 70 4e 65 77 3b 20 20  sFileId *pNew;  
cada0 20 20 20 20 20 20 20 2f 2a 20 73 65 61 72 63 68         /* search
cadb0 20 6b 65 79 20 61 6e 64 20 6e 65 77 20 66 69 6c   key and new fil
cadc0 65 20 49 44 20 2a 2f 0a 20 20 73 74 72 75 63 74  e ID */.  struct
cadd0 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a   vxworksFileId *
cade0 70 43 61 6e 64 69 64 61 74 65 3b 20 20 20 2f 2a  pCandidate;   /*
cadf0 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   For looping ove
cae00 72 20 65 78 69 73 74 69 6e 67 20 66 69 6c 65 20  r existing file 
cae10 49 44 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20  IDs */.  int n; 
cae20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cae30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
cae40 4c 65 6e 67 74 68 20 6f 66 20 7a 41 62 73 6f 6c  Length of zAbsol
cae50 75 74 65 4e 61 6d 65 20 73 74 72 69 6e 67 20 2a  uteName string *
cae60 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 7a 41 62  /..  assert( zAb
cae70 73 6f 6c 75 74 65 4e 61 6d 65 5b 30 5d 3d 3d 27  soluteName[0]=='
cae80 2f 27 20 29 3b 0a 20 20 6e 20 3d 20 28 69 6e 74  /' );.  n = (int
cae90 29 73 74 72 6c 65 6e 28 7a 41 62 73 6f 6c 75 74  )strlen(zAbsolut
caea0 65 4e 61 6d 65 29 3b 0a 20 20 70 4e 65 77 20 3d  eName);.  pNew =
caeb0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
caec0 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 2b   sizeof(*pNew) +
caed0 20 28 6e 2b 31 29 20 29 3b 0a 20 20 69 66 28 20   (n+1) );.  if( 
caee0 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e  pNew==0 ) return
caef0 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 7a 43 61 6e   0;.  pNew->zCan
caf00 6f 6e 69 63 61 6c 4e 61 6d 65 20 3d 20 28 63 68  onicalName = (ch
caf10 61 72 2a 29 26 70 4e 65 77 5b 31 5d 3b 0a 20 20  ar*)&pNew[1];.  
caf20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e 7a 43 61  memcpy(pNew->zCa
caf30 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 7a 41 62  nonicalName, zAb
caf40 73 6f 6c 75 74 65 4e 61 6d 65 2c 20 6e 2b 31 29  soluteName, n+1)
caf50 3b 0a 20 20 6e 20 3d 20 76 78 77 6f 72 6b 73 53  ;.  n = vxworksS
caf60 69 6d 70 6c 69 66 79 4e 61 6d 65 28 70 4e 65 77  implifyName(pNew
caf70 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65  ->zCanonicalName
caf80 2c 20 6e 29 3b 0a 0a 20 20 2f 2a 20 53 65 61 72  , n);..  /* Sear
caf90 63 68 20 66 6f 72 20 61 6e 20 65 78 69 73 74 69  ch for an existi
cafa0 6e 67 20 65 6e 74 72 79 20 74 68 61 74 20 6d 61  ng entry that ma
cafb0 74 63 68 69 6e 67 20 74 68 65 20 63 61 6e 6f 6e  tching the canon
cafc0 69 63 61 6c 20 6e 61 6d 65 2e 0a 20 20 2a 2a 20  ical name..  ** 
cafd0 49 66 20 66 6f 75 6e 64 2c 20 69 6e 63 72 65 6d  If found, increm
cafe0 65 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e 63  ent the referenc
caff0 65 20 63 6f 75 6e 74 20 61 6e 64 20 72 65 74 75  e count and retu
cb000 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a  rn a pointer to.
cb010 20 20 2a 2a 20 74 68 65 20 65 78 69 73 74 69 6e    ** the existin
cb020 67 20 66 69 6c 65 20 49 44 2e 0a 20 20 2a 2f 0a  g file ID..  */.
cb030 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78    unixEnterMutex
cb040 28 29 3b 0a 20 20 66 6f 72 28 70 43 61 6e 64 69  ();.  for(pCandi
cb050 64 61 74 65 3d 76 78 77 6f 72 6b 73 46 69 6c 65  date=vxworksFile
cb060 4c 69 73 74 3b 20 70 43 61 6e 64 69 64 61 74 65  List; pCandidate
cb070 3b 20 70 43 61 6e 64 69 64 61 74 65 3d 70 43 61  ; pCandidate=pCa
cb080 6e 64 69 64 61 74 65 2d 3e 70 4e 65 78 74 29 7b  ndidate->pNext){
cb090 0a 20 20 20 20 69 66 28 20 70 43 61 6e 64 69 64  .    if( pCandid
cb0a0 61 74 65 2d 3e 6e 4e 61 6d 65 3d 3d 6e 20 0a 20  ate->nName==n . 
cb0b0 20 20 20 20 26 26 20 6d 65 6d 63 6d 70 28 70 43      && memcmp(pC
cb0c0 61 6e 64 69 64 61 74 65 2d 3e 7a 43 61 6e 6f 6e  andidate->zCanon
cb0d0 69 63 61 6c 4e 61 6d 65 2c 20 70 4e 65 77 2d 3e  icalName, pNew->
cb0e0 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20  zCanonicalName, 
cb0f0 6e 29 3d 3d 30 0a 20 20 20 20 29 7b 0a 20 20 20  n)==0.    ){.   
cb100 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
cb110 28 70 4e 65 77 29 3b 0a 20 20 20 20 20 20 20 70  (pNew);.       p
cb120 43 61 6e 64 69 64 61 74 65 2d 3e 6e 52 65 66 2b  Candidate->nRef+
cb130 2b 3b 0a 20 20 20 20 20 20 20 75 6e 69 78 4c 65  +;.       unixLe
cb140 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20  aveMutex();.    
cb150 20 20 20 72 65 74 75 72 6e 20 70 43 61 6e 64 69     return pCandi
cb160 64 61 74 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  date;.    }.  }.
cb170 0a 20 20 2f 2a 20 4e 6f 20 6d 61 74 63 68 20 77  .  /* No match w
cb180 61 73 20 66 6f 75 6e 64 2e 20 20 57 65 20 77 69  as found.  We wi
cb190 6c 6c 20 6d 61 6b 65 20 61 20 6e 65 77 20 66 69  ll make a new fi
cb1a0 6c 65 20 49 44 20 2a 2f 0a 20 20 70 4e 65 77 2d  le ID */.  pNew-
cb1b0 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70 4e 65  >nRef = 1;.  pNe
cb1c0 77 2d 3e 6e 4e 61 6d 65 20 3d 20 6e 3b 0a 20 20  w->nName = n;.  
cb1d0 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d 20 76 78  pNew->pNext = vx
cb1e0 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b 0a 20  worksFileList;. 
cb1f0 20 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74   vxworksFileList
cb200 20 3d 20 70 4e 65 77 3b 0a 20 20 75 6e 69 78 4c   = pNew;.  unixL
cb210 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72  eaveMutex();.  r
cb220 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f  eturn pNew;.}../
cb230 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74  *.** Decrement t
cb240 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  he reference cou
cb250 6e 74 20 6f 6e 20 61 20 76 78 77 6f 72 6b 73 46  nt on a vxworksF
cb260 69 6c 65 49 64 20 6f 62 6a 65 63 74 2e 20 20 46  ileId object.  F
cb270 72 65 65 0a 2a 2a 20 74 68 65 20 6f 62 6a 65 63  ree.** the objec
cb280 74 20 77 68 65 6e 20 74 68 65 20 72 65 66 65 72  t when the refer
cb290 65 6e 63 65 20 63 6f 75 6e 74 20 72 65 61 63 68  ence count reach
cb2a0 65 73 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74  es zero..*/.stat
cb2b0 69 63 20 76 6f 69 64 20 76 78 77 6f 72 6b 73 52  ic void vxworksR
cb2c0 65 6c 65 61 73 65 46 69 6c 65 49 64 28 73 74 72  eleaseFileId(str
cb2d0 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49  uct vxworksFileI
cb2e0 64 20 2a 70 49 64 29 7b 0a 20 20 75 6e 69 78 45  d *pId){.  unixE
cb2f0 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 61  nterMutex();.  a
cb300 73 73 65 72 74 28 20 70 49 64 2d 3e 6e 52 65 66  ssert( pId->nRef
cb310 3e 30 20 29 3b 0a 20 20 70 49 64 2d 3e 6e 52 65  >0 );.  pId->nRe
cb320 66 2d 2d 3b 0a 20 20 69 66 28 20 70 49 64 2d 3e  f--;.  if( pId->
cb330 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 73  nRef==0 ){.    s
cb340 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c  truct vxworksFil
cb350 65 49 64 20 2a 2a 70 70 3b 0a 20 20 20 20 66 6f  eId **pp;.    fo
cb360 72 28 70 70 3d 26 76 78 77 6f 72 6b 73 46 69 6c  r(pp=&vxworksFil
cb370 65 4c 69 73 74 3b 20 2a 70 70 20 26 26 20 2a 70  eList; *pp && *p
cb380 70 21 3d 70 49 64 3b 20 70 70 20 3d 20 26 28 28  p!=pId; pp = &((
cb390 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29 7b 7d 0a  *pp)->pNext)){}.
cb3a0 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 70 3d      assert( *pp=
cb3b0 3d 70 49 64 20 29 3b 0a 20 20 20 20 2a 70 70 20  =pId );.    *pp 
cb3c0 3d 20 70 49 64 2d 3e 70 4e 65 78 74 3b 0a 20 20  = pId->pNext;.  
cb3d0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
cb3e0 49 64 29 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 4c  Id);.  }.  unixL
cb3f0 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 23  eaveMutex();.}.#
cb400 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 57 4f  endif /* OS_VXWO
cb410 52 4b 53 20 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  RKS */./********
cb420 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 55  ******* End of U
cb430 6e 69 71 75 65 20 46 69 6c 65 20 49 44 20 55 74  nique File ID Ut
cb440 69 6c 69 74 79 20 55 73 65 64 20 42 79 20 56 78  ility Used By Vx
cb450 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Works **********
cb460 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ******.*********
cb470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb4b0 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a  *****/.../******
cb4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb500 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  ********.*******
cb510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb520 2a 2a 2a 2a 20 50 6f 73 69 78 20 41 64 76 69 73  **** Posix Advis
cb530 6f 72 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a  ory Locking ****
cb540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb550 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 50  ********.**.** P
cb560 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20 6c 6f  OSIX advisory lo
cb570 63 6b 73 20 61 72 65 20 62 72 6f 6b 65 6e 20 62  cks are broken b
cb580 79 20 64 65 73 69 67 6e 2e 20 20 41 4e 53 49 20  y design.  ANSI 
cb590 53 54 44 20 31 30 30 33 2e 31 20 28 31 39 39 36  STD 1003.1 (1996
cb5a0 29 0a 2a 2a 20 73 65 63 74 69 6f 6e 20 36 2e 35  ).** section 6.5
cb5b0 2e 32 2e 32 20 6c 69 6e 65 73 20 34 38 33 20 74  .2.2 lines 483 t
cb5c0 68 72 6f 75 67 68 20 34 39 30 20 73 70 65 63 69  hrough 490 speci
cb5d0 66 79 20 74 68 61 74 20 77 68 65 6e 20 61 20 70  fy that when a p
cb5e0 72 6f 63 65 73 73 0a 2a 2a 20 73 65 74 73 20 6f  rocess.** sets o
cb5f0 72 20 63 6c 65 61 72 73 20 61 20 6c 6f 63 6b 2c  r clears a lock,
cb600 20 74 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20   that operation 
cb610 6f 76 65 72 72 69 64 65 73 20 61 6e 79 20 70 72  overrides any pr
cb620 69 6f 72 20 6c 6f 63 6b 73 20 73 65 74 0a 2a 2a  ior locks set.**
cb630 20 62 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f   by the same pro
cb640 63 65 73 73 2e 20 20 49 74 20 64 6f 65 73 20 6e  cess.  It does n
cb650 6f 74 20 65 78 70 6c 69 63 69 74 6c 79 20 73 61  ot explicitly sa
cb660 79 20 73 6f 2c 20 62 75 74 20 74 68 69 73 20 69  y so, but this i
cb670 6d 70 6c 69 65 73 0a 2a 2a 20 74 68 61 74 20 69  mplies.** that i
cb680 74 20 6f 76 65 72 72 69 64 65 73 20 6c 6f 63 6b  t overrides lock
cb690 73 20 73 65 74 20 62 79 20 74 68 65 20 73 61 6d  s set by the sam
cb6a0 65 20 70 72 6f 63 65 73 73 20 75 73 69 6e 67 20  e process using 
cb6b0 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66  a different.** f
cb6c0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20  ile descriptor. 
cb6d0 20 43 6f 6e 73 69 64 65 72 20 74 68 69 73 20 74   Consider this t
cb6e0 65 73 74 20 63 61 73 65 3a 0a 2a 2a 0a 2a 2a 20  est case:.**.** 
cb6f0 20 20 20 20 20 20 69 6e 74 20 66 64 31 20 3d 20        int fd1 = 
cb700 6f 70 65 6e 28 22 2e 2f 66 69 6c 65 31 22 2c 20  open("./file1", 
cb710 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 41 54 2c 20  O_RDWR|O_CREAT, 
cb720 30 36 34 34 29 3b 0a 2a 2a 20 20 20 20 20 20 20  0644);.**       
cb730 69 6e 74 20 66 64 32 20 3d 20 6f 70 65 6e 28 22  int fd2 = open("
cb740 2e 2f 66 69 6c 65 32 22 2c 20 4f 5f 52 44 57 52  ./file2", O_RDWR
cb750 7c 4f 5f 43 52 45 41 54 2c 20 30 36 34 34 29 3b  |O_CREAT, 0644);
cb760 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 2e  .**.** Suppose .
cb770 2f 66 69 6c 65 31 20 61 6e 64 20 2e 2f 66 69 6c  /file1 and ./fil
cb780 65 32 20 61 72 65 20 72 65 61 6c 6c 79 20 74 68  e2 are really th
cb790 65 20 73 61 6d 65 20 66 69 6c 65 20 28 62 65 63  e same file (bec
cb7a0 61 75 73 65 0a 2a 2a 20 6f 6e 65 20 69 73 20 61  ause.** one is a
cb7b0 20 68 61 72 64 20 6f 72 20 73 79 6d 62 6f 6c 69   hard or symboli
cb7c0 63 20 6c 69 6e 6b 20 74 6f 20 74 68 65 20 6f 74  c link to the ot
cb7d0 68 65 72 29 20 74 68 65 6e 20 69 66 20 79 6f 75  her) then if you
cb7e0 20 73 65 74 0a 2a 2a 20 61 6e 20 65 78 63 6c 75   set.** an exclu
cb7f0 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 66 64 31  sive lock on fd1
cb800 2c 20 74 68 65 6e 20 74 72 79 20 74 6f 20 67 65  , then try to ge
cb810 74 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  t an exclusive l
cb820 6f 63 6b 0a 2a 2a 20 6f 6e 20 66 64 32 2c 20 69  ock.** on fd2, i
cb830 74 20 77 6f 72 6b 73 2e 20 20 49 20 77 6f 75 6c  t works.  I woul
cb840 64 20 68 61 76 65 20 65 78 70 65 63 74 65 64 20  d have expected 
cb850 74 68 65 20 73 65 63 6f 6e 64 20 6c 6f 63 6b 20  the second lock 
cb860 74 6f 0a 2a 2a 20 66 61 69 6c 20 73 69 6e 63 65  to.** fail since
cb870 20 74 68 65 72 65 20 77 61 73 20 61 6c 72 65 61   there was alrea
cb880 64 79 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  dy a lock on the
cb890 20 66 69 6c 65 20 64 75 65 20 74 6f 20 66 64 31   file due to fd1
cb8a0 2e 0a 2a 2a 20 42 75 74 20 6e 6f 74 20 73 6f 2e  ..** But not so.
cb8b0 20 20 53 69 6e 63 65 20 62 6f 74 68 20 6c 6f 63    Since both loc
cb8c0 6b 73 20 63 61 6d 65 20 66 72 6f 6d 20 74 68 65  ks came from the
cb8d0 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2c 20 74   same process, t
cb8e0 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 6f 76 65  he.** second ove
cb8f0 72 72 69 64 65 73 20 74 68 65 20 66 69 72 73 74  rrides the first
cb900 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68  , even though th
cb910 65 79 20 77 65 72 65 20 6f 6e 20 64 69 66 66 65  ey were on diffe
cb920 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 20 64 65 73  rent.** file des
cb930 63 72 69 70 74 6f 72 73 20 6f 70 65 6e 65 64 20  criptors opened 
cb940 6f 6e 20 64 69 66 66 65 72 65 6e 74 20 66 69 6c  on different fil
cb950 65 20 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20 54  e names..**.** T
cb960 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 77  his means that w
cb970 65 20 63 61 6e 6e 6f 74 20 75 73 65 20 50 4f 53  e cannot use POS
cb980 49 58 20 6c 6f 63 6b 73 20 74 6f 20 73 79 6e 63  IX locks to sync
cb990 68 72 6f 6e 69 7a 65 20 66 69 6c 65 20 61 63 63  hronize file acc
cb9a0 65 73 73 0a 2a 2a 20 61 6d 6f 6e 67 20 63 6f 6d  ess.** among com
cb9b0 70 65 74 69 6e 67 20 74 68 72 65 61 64 73 20 6f  peting threads o
cb9c0 66 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65  f the same proce
cb9d0 73 73 2e 20 20 50 4f 53 49 58 20 6c 6f 63 6b 73  ss.  POSIX locks
cb9e0 20 77 69 6c 6c 20 77 6f 72 6b 20 66 69 6e 65 0a   will work fine.
cb9f0 2a 2a 20 74 6f 20 73 79 6e 63 68 72 6f 6e 69 7a  ** to synchroniz
cba00 65 20 61 63 63 65 73 73 20 66 6f 72 20 74 68 72  e access for thr
cba10 65 61 64 73 20 69 6e 20 73 65 70 61 72 61 74 65  eads in separate
cba20 20 70 72 6f 63 65 73 73 65 73 2c 20 62 75 74 20   processes, but 
cba30 6e 6f 74 0a 2a 2a 20 74 68 72 65 61 64 73 20 77  not.** threads w
cba40 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20 70  ithin the same p
cba50 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f  rocess..**.** To
cba60 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 65   work around the
cba70 20 70 72 6f 62 6c 65 6d 2c 20 53 51 4c 69 74 65   problem, SQLite
cba80 20 68 61 73 20 74 6f 20 6d 61 6e 61 67 65 20 66   has to manage f
cba90 69 6c 65 20 6c 6f 63 6b 73 20 69 6e 74 65 72 6e  ile locks intern
cbaa0 61 6c 6c 79 0a 2a 2a 20 6f 6e 20 69 74 73 20 6f  ally.** on its o
cbab0 77 6e 2e 20 20 57 68 65 6e 65 76 65 72 20 61 20  wn.  Whenever a 
cbac0 6e 65 77 20 64 61 74 61 62 61 73 65 20 69 73 20  new database is 
cbad0 6f 70 65 6e 65 64 2c 20 77 65 20 68 61 76 65 20  opened, we have 
cbae0 74 6f 20 66 69 6e 64 20 74 68 65 0a 2a 2a 20 73  to find the.** s
cbaf0 70 65 63 69 66 69 63 20 69 6e 6f 64 65 20 6f 66  pecific inode of
cbb00 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
cbb10 6c 65 20 28 74 68 65 20 69 6e 6f 64 65 20 69 73  le (the inode is
cbb20 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74   determined by t
cbb30 68 65 0a 2a 2a 20 73 74 5f 64 65 76 20 61 6e 64  he.** st_dev and
cbb40 20 73 74 5f 69 6e 6f 20 66 69 65 6c 64 73 20 6f   st_ino fields o
cbb50 66 20 74 68 65 20 73 74 61 74 20 73 74 72 75 63  f the stat struc
cbb60 74 75 72 65 20 74 68 61 74 20 66 73 74 61 74 28  ture that fstat(
cbb70 29 20 66 69 6c 6c 73 20 69 6e 29 0a 2a 2a 20 61  ) fills in).** a
cbb80 6e 64 20 63 68 65 63 6b 20 66 6f 72 20 6c 6f 63  nd check for loc
cbb90 6b 73 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ks already exist
cbba0 69 6e 67 20 6f 6e 20 74 68 61 74 20 69 6e 6f 64  ing on that inod
cbbb0 65 2e 20 20 57 68 65 6e 20 6c 6f 63 6b 73 20 61  e.  When locks a
cbbc0 72 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 6f 72  re.** created or
cbbd0 20 72 65 6d 6f 76 65 64 2c 20 77 65 20 68 61 76   removed, we hav
cbbe0 65 20 74 6f 20 6c 6f 6f 6b 20 61 74 20 6f 75 72  e to look at our
cbbf0 20 6f 77 6e 20 69 6e 74 65 72 6e 61 6c 20 72 65   own internal re
cbc00 63 6f 72 64 20 6f 66 20 74 68 65 0a 2a 2a 20 6c  cord of the.** l
cbc10 6f 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 61  ocks to see if a
cbc20 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 68 61  nother thread ha
cbc30 73 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 74  s previously set
cbc40 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 61 74 20   a lock on that 
cbc50 73 61 6d 65 0a 2a 2a 20 69 6e 6f 64 65 2e 0a 2a  same.** inode..*
cbc60 2a 0a 2a 2a 20 28 41 73 69 64 65 3a 20 54 68 65  *.** (Aside: The
cbc70 20 75 73 65 20 6f 66 20 69 6e 6f 64 65 20 6e 75   use of inode nu
cbc80 6d 62 65 72 73 20 61 73 20 75 6e 69 71 75 65 20  mbers as unique 
cbc90 49 44 73 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72  IDs does not wor
cbca0 6b 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a  k on VxWorks..**
cbcb0 20 46 6f 72 20 56 78 57 6f 72 6b 73 2c 20 77 65   For VxWorks, we
cbcc0 20 68 61 76 65 20 74 6f 20 75 73 65 20 74 68 65   have to use the
cbcd0 20 61 6c 74 65 72 6e 61 74 69 76 65 20 75 6e 69   alternative uni
cbce0 71 75 65 20 49 44 20 73 79 73 74 65 6d 20 62 61  que ID system ba
cbcf0 73 65 64 20 6f 6e 0a 2a 2a 20 63 61 6e 6f 6e 69  sed on.** canoni
cbd00 63 61 6c 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64  cal filename and
cbd10 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20   implemented in 
cbd20 74 68 65 20 70 72 65 76 69 6f 75 73 20 64 69 76  the previous div
cbd30 69 73 69 6f 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68  ision.).**.** Th
cbd40 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73  e sqlite3_file s
cbd50 74 72 75 63 74 75 72 65 20 66 6f 72 20 50 4f 53  tructure for POS
cbd60 49 58 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20  IX is no longer 
cbd70 6a 75 73 74 20 61 6e 20 69 6e 74 65 67 65 72 20  just an integer 
cbd80 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 74  file.** descript
cbd90 6f 72 2e 20 20 49 74 20 69 73 20 6e 6f 77 20 61  or.  It is now a
cbda0 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20   structure that 
cbdb0 68 6f 6c 64 73 20 74 68 65 20 69 6e 74 65 67 65  holds the intege
cbdc0 72 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69  r file.** descri
cbdd0 70 74 6f 72 20 61 6e 64 20 61 20 70 6f 69 6e 74  ptor and a point
cbde0 65 72 20 74 6f 20 61 20 73 74 72 75 63 74 75 72  er to a structur
cbdf0 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73  e that describes
cbe00 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a   the internal.**
cbe10 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 63 6f   locks on the co
cbe20 72 72 65 73 70 6f 6e 64 69 6e 67 20 69 6e 6f 64  rresponding inod
cbe30 65 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65  e.  There is one
cbe40 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75   locking structu
cbe50 72 65 0a 2a 2a 20 70 65 72 20 69 6e 6f 64 65 2c  re.** per inode,
cbe60 20 73 6f 20 69 66 20 74 68 65 20 73 61 6d 65 20   so if the same 
cbe70 69 6e 6f 64 65 20 69 73 20 6f 70 65 6e 65 64 20  inode is opened 
cbe80 74 77 69 63 65 2c 20 62 6f 74 68 20 75 6e 69 78  twice, both unix
cbe90 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 73 0a  File structures.
cbea0 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  ** point to the 
cbeb0 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72  same locking str
cbec0 75 63 74 75 72 65 2e 20 20 54 68 65 20 6c 6f 63  ucture.  The loc
cbed0 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 6b  king structure k
cbee0 65 65 70 73 0a 2a 2a 20 61 20 72 65 66 65 72 65  eeps.** a refere
cbef0 6e 63 65 20 63 6f 75 6e 74 20 28 73 6f 20 77 65  nce count (so we
cbf00 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68 65 6e 20   will know when 
cbf10 74 6f 20 64 65 6c 65 74 65 20 69 74 29 20 61 6e  to delete it) an
cbf20 64 20 61 20 22 63 6e 74 22 0a 2a 2a 20 66 69 65  d a "cnt".** fie
cbf30 6c 64 20 74 68 61 74 20 74 65 6c 6c 73 20 75 73  ld that tells us
cbf40 20 69 74 73 20 69 6e 74 65 72 6e 61 6c 20 6c 6f   its internal lo
cbf50 63 6b 20 73 74 61 74 75 73 2e 20 20 63 6e 74 3d  ck status.  cnt=
cbf60 3d 30 20 6d 65 61 6e 73 20 74 68 65 0a 2a 2a 20  =0 means the.** 
cbf70 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64  file is unlocked
cbf80 2e 20 20 63 6e 74 3d 3d 2d 31 20 6d 65 61 6e 73  .  cnt==-1 means
cbf90 20 74 68 65 20 66 69 6c 65 20 68 61 73 20 61 6e   the file has an
cbfa0 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e   exclusive lock.
cbfb0 0a 2a 2a 20 63 6e 74 3e 30 20 6d 65 61 6e 73 20  .** cnt>0 means 
cbfc0 74 68 65 72 65 20 61 72 65 20 63 6e 74 20 73 68  there are cnt sh
cbfd0 61 72 65 64 20 6c 6f 63 6b 73 20 6f 6e 20 74 68  ared locks on th
cbfe0 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e  e file..**.** An
cbff0 79 20 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63  y attempt to loc
cc000 6b 20 6f 72 20 75 6e 6c 6f 63 6b 20 61 20 66 69  k or unlock a fi
cc010 6c 65 20 66 69 72 73 74 20 63 68 65 63 6b 73 20  le first checks 
cc020 74 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73  the locking.** s
cc030 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 66  tructure.  The f
cc040 63 6e 74 6c 28 29 20 73 79 73 74 65 6d 20 63 61  cntl() system ca
cc050 6c 6c 20 69 73 20 6f 6e 6c 79 20 69 6e 76 6f 6b  ll is only invok
cc060 65 64 20 74 6f 20 73 65 74 20 61 20 0a 2a 2a 20  ed to set a .** 
cc070 50 4f 53 49 58 20 6c 6f 63 6b 20 69 66 20 74 68  POSIX lock if th
cc080 65 20 69 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b 20  e internal lock 
cc090 73 74 72 75 63 74 75 72 65 20 74 72 61 6e 73 69  structure transi
cc0a0 74 69 6f 6e 73 20 62 65 74 77 65 65 6e 0a 2a 2a  tions between.**
cc0b0 20 61 20 6c 6f 63 6b 65 64 20 61 6e 64 20 61 6e   a locked and an
cc0c0 20 75 6e 6c 6f 63 6b 65 64 20 73 74 61 74 65 2e   unlocked state.
cc0d0 0a 2a 2a 0a 2a 2a 20 42 75 74 20 77 61 69 74 3a  .**.** But wait:
cc0e0 20 20 74 68 65 72 65 20 61 72 65 20 79 65 74 20    there are yet 
cc0f0 6d 6f 72 65 20 70 72 6f 62 6c 65 6d 73 20 77 69  more problems wi
cc100 74 68 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72  th POSIX advisor
cc110 79 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49  y locks..**.** I
cc120 66 20 79 6f 75 20 63 6c 6f 73 65 20 61 20 66 69  f you close a fi
cc130 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 68  le descriptor th
cc140 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 66  at points to a f
cc150 69 6c 65 20 74 68 61 74 20 68 61 73 20 6c 6f 63  ile that has loc
cc160 6b 73 2c 0a 2a 2a 20 61 6c 6c 20 6c 6f 63 6b 73  ks,.** all locks
cc170 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 20 74 68   on that file th
cc180 61 74 20 61 72 65 20 6f 77 6e 65 64 20 62 79 20  at are owned by 
cc190 74 68 65 20 63 75 72 72 65 6e 74 20 70 72 6f 63  the current proc
cc1a0 65 73 73 20 61 72 65 0a 2a 2a 20 72 65 6c 65 61  ess are.** relea
cc1b0 73 65 64 2e 20 20 54 6f 20 77 6f 72 6b 20 61 72  sed.  To work ar
cc1c0 6f 75 6e 64 20 74 68 69 73 20 70 72 6f 62 6c 65  ound this proble
cc1d0 6d 2c 20 65 61 63 68 20 75 6e 69 78 46 69 6c 65  m, each unixFile
cc1e0 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61   structure conta
cc1f0 69 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72  ins.** a pointer
cc200 20 74 6f 20 61 6e 20 75 6e 69 78 4f 70 65 6e 43   to an unixOpenC
cc210 6e 74 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  nt structure.  T
cc220 68 65 72 65 20 69 73 20 6f 6e 65 20 75 6e 69 78  here is one unix
cc230 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72  OpenCnt structur
cc240 65 0a 2a 2a 20 70 65 72 20 6f 70 65 6e 20 69 6e  e.** per open in
cc250 6f 64 65 2c 20 77 68 69 63 68 20 6d 65 61 6e 73  ode, which means
cc260 20 74 68 61 74 20 6d 75 6c 74 69 70 6c 65 20 75   that multiple u
cc270 6e 69 78 46 69 6c 65 20 63 61 6e 20 70 6f 69 6e  nixFile can poin
cc280 74 20 74 6f 20 61 20 73 69 6e 67 6c 65 0a 2a 2a  t to a single.**
cc290 20 75 6e 69 78 4f 70 65 6e 43 6e 74 2e 20 20 57   unixOpenCnt.  W
cc2a0 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69  hen an attempt i
cc2b0 73 20 6d 61 64 65 20 74 6f 20 63 6c 6f 73 65 20  s made to close 
cc2c0 61 6e 20 75 6e 69 78 46 69 6c 65 2c 20 69 66 20  an unixFile, if 
cc2d0 74 68 65 72 65 20 61 72 65 0a 2a 2a 20 6f 74 68  there are.** oth
cc2e0 65 72 20 75 6e 69 78 46 69 6c 65 20 6f 70 65 6e  er unixFile open
cc2f0 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 69 6e 6f   on the same ino
cc300 64 65 20 74 68 61 74 20 61 72 65 20 68 6f 6c 64  de that are hold
cc310 69 6e 67 20 6c 6f 63 6b 73 2c 20 74 68 65 20 63  ing locks, the c
cc320 61 6c 6c 0a 2a 2a 20 74 6f 20 63 6c 6f 73 65 28  all.** to close(
cc330 29 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72  ) the file descr
cc340 69 70 74 6f 72 20 69 73 20 64 65 66 65 72 72 65  iptor is deferre
cc350 64 20 75 6e 74 69 6c 20 61 6c 6c 20 6f 66 20 74  d until all of t
cc360 68 65 20 6c 6f 63 6b 73 20 63 6c 65 61 72 2e 0a  he locks clear..
cc370 2a 2a 20 54 68 65 20 75 6e 69 78 4f 70 65 6e 43  ** The unixOpenC
cc380 6e 74 20 73 74 72 75 63 74 75 72 65 20 6b 65 65  nt structure kee
cc390 70 73 20 61 20 6c 69 73 74 20 6f 66 20 66 69 6c  ps a list of fil
cc3a0 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 74 68  e descriptors th
cc3b0 61 74 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 62 65  at need to.** be
cc3c0 20 63 6c 6f 73 65 64 20 61 6e 64 20 74 68 61 74   closed and that
cc3d0 20 6c 69 73 74 20 69 73 20 77 61 6c 6b 65 64 20   list is walked 
cc3e0 28 61 6e 64 20 63 6c 65 61 72 65 64 29 20 77 68  (and cleared) wh
cc3f0 65 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b  en the last lock
cc400 0a 2a 2a 20 63 6c 65 61 72 73 2e 0a 2a 2a 0a 2a  .** clears..**.*
cc410 2a 20 59 65 74 20 61 6e 6f 74 68 65 72 20 70 72  * Yet another pr
cc420 6f 62 6c 65 6d 3a 20 20 4c 69 6e 75 78 54 68 72  oblem:  LinuxThr
cc430 65 61 64 73 20 64 6f 20 6e 6f 74 20 70 6c 61 79  eads do not play
cc440 20 77 65 6c 6c 20 77 69 74 68 20 70 6f 73 69 78   well with posix
cc450 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 4d 61   locks..**.** Ma
cc460 6e 79 20 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e  ny older version
cc470 73 20 6f 66 20 6c 69 6e 75 78 20 75 73 65 20 74  s of linux use t
cc480 68 65 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20  he LinuxThreads 
cc490 6c 69 62 72 61 72 79 20 77 68 69 63 68 20 69 73  library which is
cc4a0 0a 2a 2a 20 6e 6f 74 20 70 6f 73 69 78 20 63 6f  .** not posix co
cc4b0 6d 70 6c 69 61 6e 74 2e 20 20 55 6e 64 65 72 20  mpliant.  Under 
cc4c0 4c 69 6e 75 78 54 68 72 65 61 64 73 2c 20 61 20  LinuxThreads, a 
cc4d0 6c 6f 63 6b 20 63 72 65 61 74 65 64 20 62 79 20  lock created by 
cc4e0 74 68 72 65 61 64 0a 2a 2a 20 41 20 63 61 6e 6e  thread.** A cann
cc4f0 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 64 20 6f  ot be modified o
cc500 72 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20  r overridden by 
cc510 61 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65  a different thre
cc520 61 64 20 42 2e 0a 2a 2a 20 4f 6e 6c 79 20 74 68  ad B..** Only th
cc530 72 65 61 64 20 41 20 63 61 6e 20 6d 6f 64 69 66  read A can modif
cc540 79 20 74 68 65 20 6c 6f 63 6b 2e 20 20 4c 6f 63  y the lock.  Loc
cc550 6b 69 6e 67 20 62 65 68 61 76 69 6f 72 20 69 73  king behavior is
cc560 20 63 6f 72 72 65 63 74 0a 2a 2a 20 69 66 20 74   correct.** if t
cc570 68 65 20 61 70 70 6c 69 61 74 69 6f 6e 20 75 73  he appliation us
cc580 65 73 20 74 68 65 20 6e 65 77 65 72 20 4e 61 74  es the newer Nat
cc590 69 76 65 20 50 6f 73 69 78 20 54 68 72 65 61 64  ive Posix Thread
cc5a0 20 4c 69 62 72 61 72 79 20 28 4e 50 54 4c 29 0a   Library (NPTL).
cc5b0 2a 2a 20 6f 6e 20 6c 69 6e 75 78 20 2d 20 77 69  ** on linux - wi
cc5c0 74 68 20 4e 50 54 4c 20 61 20 6c 6f 63 6b 20 63  th NPTL a lock c
cc5d0 72 65 61 74 65 64 20 62 79 20 74 68 72 65 61 64  reated by thread
cc5e0 20 41 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20   A can override 
cc5f0 6c 6f 63 6b 73 0a 2a 2a 20 69 6e 20 74 68 72 65  locks.** in thre
cc600 61 64 20 42 2e 20 20 42 75 74 20 74 68 65 72 65  ad B.  But there
cc610 20 69 73 20 6e 6f 20 77 61 79 20 74 6f 20 6b 6e   is no way to kn
cc620 6f 77 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69  ow at compile-ti
cc630 6d 65 20 77 68 69 63 68 0a 2a 2a 20 74 68 72 65  me which.** thre
cc640 61 64 69 6e 67 20 6c 69 62 72 61 72 79 20 69 73  ading library is
cc650 20 62 65 69 6e 67 20 75 73 65 64 2e 20 20 53 6f   being used.  So
cc660 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79   there is no way
cc670 20 74 6f 20 6b 6e 6f 77 20 61 74 0a 2a 2a 20 63   to know at.** c
cc680 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 77 68 65 74  ompile-time whet
cc690 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61  her or not threa
cc6a0 64 20 41 20 63 61 6e 20 6f 76 65 72 72 69 64 65  d A can override
cc6b0 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 72 65 61 64   locks on thread
cc6c0 20 42 2e 0a 2a 2a 20 57 65 20 68 61 76 65 20 74   B..** We have t
cc6d0 6f 20 64 6f 20 61 20 72 75 6e 2d 74 69 6d 65 20  o do a run-time 
cc6e0 63 68 65 63 6b 20 74 6f 20 64 69 73 63 6f 76 65  check to discove
cc6f0 72 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f  r the behavior o
cc700 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74  f the.** current
cc710 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20   process..**.** 
cc720 4f 6e 20 73 79 73 74 65 6d 73 20 77 68 65 72 65  On systems where
cc730 20 74 68 72 65 61 64 20 41 20 69 73 20 75 6e 61   thread A is una
cc740 62 6c 65 20 74 6f 20 6d 6f 64 69 66 79 20 6c 6f  ble to modify lo
cc750 63 6b 73 20 63 72 65 61 74 65 64 20 62 79 0a 2a  cks created by.*
cc760 2a 20 74 68 72 65 61 64 20 42 2c 20 77 65 20 68  * thread B, we h
cc770 61 76 65 20 74 6f 20 6b 65 65 70 20 74 72 61 63  ave to keep trac
cc780 6b 20 6f 66 20 77 68 69 63 68 20 74 68 72 65 61  k of which threa
cc790 64 20 63 72 65 61 74 65 64 20 65 61 63 68 0a 2a  d created each.*
cc7a0 2a 20 6c 6f 63 6b 2e 20 20 48 65 6e 63 65 20 74  * lock.  Hence t
cc7b0 68 65 72 65 20 69 73 20 61 6e 20 65 78 74 72 61  here is an extra
cc7c0 20 66 69 65 6c 64 20 69 6e 20 74 68 65 20 6b 65   field in the ke
cc7d0 79 20 74 6f 20 74 68 65 20 75 6e 69 78 4c 6f 63  y to the unixLoc
cc7e0 6b 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75  kInfo.** structu
cc7f0 72 65 20 74 6f 20 72 65 63 6f 72 64 20 74 68 69  re to record thi
cc800 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20  s information.  
cc810 41 6e 64 20 6f 6e 20 74 68 6f 73 65 20 73 79 73  And on those sys
cc820 74 65 6d 73 20 69 74 0a 2a 2a 20 69 73 20 69 6c  tems it.** is il
cc830 6c 65 67 61 6c 20 74 6f 20 62 65 67 69 6e 20 61  legal to begin a
cc840 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20   transaction in 
cc850 6f 6e 65 20 74 68 72 65 61 64 20 61 6e 64 20 66  one thread and f
cc860 69 6e 69 73 68 20 69 74 0a 2a 2a 20 69 6e 20 61  inish it.** in a
cc870 6e 6f 74 68 65 72 2e 20 20 46 6f 72 20 74 68 69  nother.  For thi
cc880 73 20 6c 61 74 74 65 72 20 72 65 73 74 72 69 63  s latter restric
cc890 74 69 6f 6e 2c 20 74 68 65 72 65 20 69 73 20 6e  tion, there is n
cc8a0 6f 20 77 6f 72 6b 2d 61 72 6f 75 6e 64 2e 0a 2a  o work-around..*
cc8b0 2a 20 49 74 20 69 73 20 61 20 6c 69 6d 69 74 61  * It is a limita
cc8c0 74 69 6f 6e 20 6f 66 20 4c 69 6e 75 78 54 68 72  tion of LinuxThr
cc8d0 65 61 64 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  eads..*/../*.** 
cc8e0 53 65 74 20 6f 72 20 63 68 65 63 6b 20 74 68 65  Set or check the
cc8f0 20 75 6e 69 78 46 69 6c 65 2e 74 69 64 20 66 69   unixFile.tid fi
cc900 65 6c 64 2e 20 20 54 68 69 73 20 66 69 65 6c 64  eld.  This field
cc910 20 69 73 20 73 65 74 20 77 68 65 6e 20 61 6e 20   is set when an 
cc920 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 69 73 20 66  unixFile.** is f
cc930 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 20 41 6c  irst opened.  Al
cc940 6c 20 73 75 62 73 65 71 75 65 6e 74 20 75 73 65  l subsequent use
cc950 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c  s of the unixFil
cc960 65 20 76 65 72 69 66 79 20 74 68 61 74 20 74 68  e verify that th
cc970 65 0a 2a 2a 20 73 61 6d 65 20 74 68 72 65 61 64  e.** same thread
cc980 20 69 73 20 6f 70 65 72 61 74 69 6e 67 20 6f 6e   is operating on
cc990 20 74 68 65 20 75 6e 69 78 46 69 6c 65 2e 20 20   the unixFile.  
cc9a0 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e 67 20 73  Some operating s
cc9b0 79 73 74 65 6d 73 20 64 6f 0a 2a 2a 20 6e 6f 74  ystems do.** not
cc9c0 20 61 6c 6c 6f 77 20 6c 6f 63 6b 73 20 74 6f 20   allow locks to 
cc9d0 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79  be overridden by
cc9e0 20 6f 74 68 65 72 20 74 68 72 65 61 64 73 20 61   other threads a
cc9f0 6e 64 20 74 68 61 74 20 72 65 73 74 72 69 63 74  nd that restrict
cca00 69 6f 6e 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61  ion.** means tha
cca10 74 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62  t sqlite3* datab
cca20 61 73 65 20 68 61 6e 64 6c 65 73 20 63 61 6e 6e  ase handles cann
cca30 6f 74 20 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d  ot be moved from
cca40 20 6f 6e 65 20 74 68 72 65 61 64 0a 2a 2a 20 74   one thread.** t
cca50 6f 20 61 6e 6f 74 68 65 72 20 77 68 69 6c 65 20  o another while 
cca60 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 2e 0a  locks are held..
cca70 2a 2a 0a 2a 2a 20 56 65 72 73 69 6f 6e 20 33 2e  **.** Version 3.
cca80 33 2e 31 20 28 32 30 30 36 2d 30 31 2d 31 35 29  3.1 (2006-01-15)
cca90 3a 20 20 75 6e 69 78 46 69 6c 65 20 63 61 6e 20  :  unixFile can 
ccaa0 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d 20 6f 6e  be moved from on
ccab0 65 20 74 68 72 65 61 64 20 74 6f 0a 2a 2a 20 61  e thread to.** a
ccac0 6e 6f 74 68 65 72 20 61 73 20 6c 6f 6e 67 20 61  nother as long a
ccad0 73 20 77 65 20 61 72 65 20 72 75 6e 6e 69 6e 67  s we are running
ccae0 20 6f 6e 20 61 20 73 79 73 74 65 6d 20 74 68 61   on a system tha
ccaf0 74 20 73 75 70 70 6f 72 74 73 20 74 68 72 65 61  t supports threa
ccb00 64 73 0a 2a 2a 20 6f 76 65 72 72 69 64 69 6e 67  ds.** overriding
ccb10 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63   each others loc
ccb20 6b 73 20 28 77 68 69 63 68 20 69 73 20 6e 6f 77  ks (which is now
ccb30 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e   the most common
ccb40 20 62 65 68 61 76 69 6f 72 29 0a 2a 2a 20 6f 72   behavior).** or
ccb50 20 69 66 20 6e 6f 20 6c 6f 63 6b 73 20 61 72 65   if no locks are
ccb60 20 68 65 6c 64 2e 20 20 42 75 74 20 74 68 65 20   held.  But the 
ccb70 75 6e 69 78 46 69 6c 65 2e 70 4c 6f 63 6b 20 66  unixFile.pLock f
ccb80 69 65 6c 64 20 6e 65 65 64 73 20 74 6f 20 62 65  ield needs to be
ccb90 0a 2a 2a 20 72 65 63 6f 6d 70 75 74 65 64 20 62  .** recomputed b
ccba0 65 63 61 75 73 65 20 69 74 73 20 6b 65 79 20 69  ecause its key i
ccbb0 6e 63 6c 75 64 65 73 20 74 68 65 20 74 68 72 65  ncludes the thre
ccbc0 61 64 2d 69 64 2e 20 20 53 65 65 20 74 68 65 20  ad-id.  See the 
ccbd0 0a 2a 2a 20 74 72 61 6e 73 66 65 72 4f 77 6e 65  .** transferOwne
ccbe0 72 73 68 69 70 28 29 20 66 75 6e 63 74 69 6f 6e  rship() function
ccbf0 20 62 65 6c 6f 77 20 66 6f 72 20 61 64 64 69 74   below for addit
ccc00 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
ccc10 6e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  n.*/.#if SQLITE_
ccc20 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65  THREADSAFE && de
ccc30 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29  fined(__linux__)
ccc40 0a 23 20 64 65 66 69 6e 65 20 53 45 54 5f 54 48  .# define SET_TH
ccc50 52 45 41 44 49 44 28 58 29 20 20 20 28 58 29 2d  READID(X)   (X)-
ccc60 3e 74 69 64 20 3d 20 70 74 68 72 65 61 64 5f 73  >tid = pthread_s
ccc70 65 6c 66 28 29 0a 23 20 64 65 66 69 6e 65 20 43  elf().# define C
ccc80 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28 58 29  HECK_THREADID(X)
ccc90 20 28 74 68 72 65 61 64 73 4f 76 65 72 72 69 64   (threadsOverrid
ccca0 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73  eEachOthersLocks
cccb0 3d 3d 30 20 26 26 20 5c 0a 20 20 20 20 20 20 20  ==0 && \.       
cccc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cccd0 20 20 20 20 20 21 70 74 68 72 65 61 64 5f 65 71       !pthread_eq
ccce0 75 61 6c 28 28 58 29 2d 3e 74 69 64 2c 20 70 74  ual((X)->tid, pt
cccf0 68 72 65 61 64 5f 73 65 6c 66 28 29 29 29 0a 23  hread_self())).#
ccd00 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 45  else.# define SE
ccd10 54 5f 54 48 52 45 41 44 49 44 28 58 29 0a 23 20  T_THREADID(X).# 
ccd20 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 54 48 52  define CHECK_THR
ccd30 45 41 44 49 44 28 58 29 20 30 0a 23 65 6e 64 69  EADID(X) 0.#endi
ccd40 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  f../*.** An inst
ccd50 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
ccd60 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
ccd70 73 65 72 76 65 73 20 61 73 20 74 68 65 20 6b 65  serves as the ke
ccd80 79 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63  y used.** to loc
ccd90 61 74 65 20 61 20 70 61 72 74 69 63 75 6c 61 72  ate a particular
ccda0 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72   unixOpenCnt str
ccdb0 75 63 74 75 72 65 20 67 69 76 65 6e 20 69 74 73  ucture given its
ccdc0 20 69 6e 6f 64 65 2e 20 20 54 68 69 73 0a 2a 2a   inode.  This.**
ccdd0 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
ccde0 74 68 65 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20  the unixLockKey 
ccdf0 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20  except that the 
cce00 74 68 72 65 61 64 20 49 44 20 69 73 20 6f 6d 69  thread ID is omi
cce10 74 74 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  tted..*/.struct 
cce20 75 6e 69 78 46 69 6c 65 49 64 20 7b 0a 20 20 64  unixFileId {.  d
cce30 65 76 5f 74 20 64 65 76 3b 20 20 20 20 20 20 20  ev_t dev;       
cce40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65             /* De
cce50 76 69 63 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 23  vice number */.#
cce60 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20  if OS_VXWORKS.  
cce70 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69  struct vxworksFi
cce80 6c 65 49 64 20 2a 70 49 64 3b 20 20 2f 2a 20 55  leId *pId;  /* U
cce90 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20 66 6f  nique file ID fo
ccea0 72 20 76 78 77 6f 72 6b 73 2e 20 2a 2f 0a 23 65  r vxworks. */.#e
cceb0 6c 73 65 0a 20 20 69 6e 6f 5f 74 20 69 6e 6f 3b  lse.  ino_t ino;
ccec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cced0 20 20 2f 2a 20 49 6e 6f 64 65 20 6e 75 6d 62 65    /* Inode numbe
ccee0 72 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a  r */.#endif.};..
ccef0 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
ccf00 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
ccf10 6e 67 20 73 74 72 75 63 74 75 72 65 20 73 65 72  ng structure ser
ccf20 76 65 73 20 61 73 20 74 68 65 20 6b 65 79 20 75  ves as the key u
ccf30 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65  sed.** to locate
ccf40 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 75 6e   a particular un
ccf50 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63  ixLockInfo struc
ccf60 74 75 72 65 20 67 69 76 65 6e 20 69 74 73 20 69  ture given its i
ccf70 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  node..**.** If t
ccf80 68 72 65 61 64 73 20 63 61 6e 6e 6f 74 20 6f 76  hreads cannot ov
ccf90 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65  erride each othe
ccfa0 72 73 20 6c 6f 63 6b 73 20 28 4c 69 6e 75 78 54  rs locks (LinuxT
ccfb0 68 72 65 61 64 73 29 2c 20 74 68 65 6e 20 77 65  hreads), then we
ccfc0 0a 2a 2a 20 73 65 74 20 74 68 65 20 75 6e 69 78  .** set the unix
ccfd0 4c 6f 63 6b 4b 65 79 2e 74 69 64 20 66 69 65 6c  LockKey.tid fiel
ccfe0 64 20 74 6f 20 74 68 65 20 74 68 72 65 61 64 20  d to the thread 
ccff0 49 44 2e 20 20 49 66 20 74 68 72 65 61 64 73 20  ID.  If threads 
cd000 63 61 6e 20 6f 76 65 72 72 69 64 65 0a 2a 2a 20  can override.** 
cd010 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b  each others lock
cd020 73 20 28 50 6f 73 69 78 20 61 6e 64 20 4e 50 54  s (Posix and NPT
cd030 4c 29 20 74 68 65 6e 20 74 69 64 20 69 73 20 61  L) then tid is a
cd040 6c 77 61 79 73 20 73 65 74 20 74 6f 20 7a 65 72  lways set to zer
cd050 6f 2e 0a 2a 2a 20 74 69 64 20 69 73 20 6f 6d 69  o..** tid is omi
cd060 74 74 65 64 20 69 66 20 77 65 20 63 6f 6d 70 69  tted if we compi
cd070 6c 65 20 77 69 74 68 6f 75 74 20 74 68 72 65 61  le without threa
cd080 64 69 6e 67 20 73 75 70 70 6f 72 74 20 6f 72 20  ding support or 
cd090 6f 6e 20 61 6e 20 4f 53 0a 2a 2a 20 6f 74 68 65  on an OS.** othe
cd0a0 72 20 74 68 61 6e 20 6c 69 6e 75 78 2e 0a 2a 2f  r than linux..*/
cd0b0 0a 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b  .struct unixLock
cd0c0 4b 65 79 20 7b 0a 20 20 73 74 72 75 63 74 20 75  Key {.  struct u
cd0d0 6e 69 78 46 69 6c 65 49 64 20 66 69 64 3b 20 20  nixFileId fid;  
cd0e0 2f 2a 20 55 6e 69 71 75 65 20 69 64 65 6e 74 69  /* Unique identi
cd0f0 66 69 65 72 20 66 6f 72 20 74 68 65 20 66 69 6c  fier for the fil
cd100 65 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  e */.#if SQLITE_
cd110 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65  THREADSAFE && de
cd120 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29  fined(__linux__)
cd130 0a 20 20 70 74 68 72 65 61 64 5f 74 20 74 69 64  .  pthread_t tid
cd140 3b 20 20 2f 2a 20 54 68 72 65 61 64 20 49 44 20  ;  /* Thread ID 
cd150 6f 66 20 6c 6f 63 6b 20 6f 77 6e 65 72 2e 20 5a  of lock owner. Z
cd160 65 72 6f 20 69 66 20 6e 6f 74 20 75 73 69 6e 67  ero if not using
cd170 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20 2a 2f   LinuxThreads */
cd180 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a  .#endif.};../*.*
cd190 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
cd1a0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
cd1b0 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f  tructure is allo
cd1c0 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 6f  cated for each o
cd1d0 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 4f  pen.** inode.  O
cd1e0 72 2c 20 6f 6e 20 4c 69 6e 75 78 54 68 72 65 61  r, on LinuxThrea
cd1f0 64 73 2c 20 74 68 65 72 65 20 69 73 20 6f 6e 65  ds, there is one
cd200 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 74   of these struct
cd210 75 72 65 73 20 66 6f 72 0a 2a 2a 20 65 61 63 68  ures for.** each
cd220 20 69 6e 6f 64 65 20 6f 70 65 6e 65 64 20 62 79   inode opened by
cd230 20 65 61 63 68 20 74 68 72 65 61 64 2e 0a 2a 2a   each thread..**
cd240 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e 6f  .** A single ino
cd250 64 65 20 63 61 6e 20 68 61 76 65 20 6d 75 6c 74  de can have mult
cd260 69 70 6c 65 20 66 69 6c 65 20 64 65 73 63 72 69  iple file descri
cd270 70 74 6f 72 73 2c 20 73 6f 20 65 61 63 68 20 75  ptors, so each u
cd280 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72 75 63  nixFile.** struc
cd290 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 20  ture contains a 
cd2a0 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e  pointer to an in
cd2b0 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f  stance of this o
cd2c0 62 6a 65 63 74 20 61 6e 64 20 74 68 69 73 0a 2a  bject and this.*
cd2d0 2a 20 6f 62 6a 65 63 74 20 6b 65 65 70 73 20 61  * object keeps a
cd2e0 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75   count of the nu
cd2f0 6d 62 65 72 20 6f 66 20 75 6e 69 78 46 69 6c 65  mber of unixFile
cd300 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69 74 2e   pointing to it.
cd310 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 4c  .*/.struct unixL
cd320 6f 63 6b 49 6e 66 6f 20 7b 0a 20 20 73 74 72 75  ockInfo {.  stru
cd330 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 6c  ct unixLockKey l
cd340 6f 63 6b 4b 65 79 3b 20 20 20 20 20 2f 2a 20 54  ockKey;     /* T
cd350 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f  he lookup key */
cd360 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20  .  int cnt;     
cd370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd380 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
cd390 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 68 65 6c  SHARED locks hel
cd3a0 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 6f 63 6b 74  d */.  int lockt
cd3b0 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ype;            
cd3c0 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66         /* One of
cd3d0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2c 20 52 45   SHARED_LOCK, RE
cd3e0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 65 74 63 2e  SERVED_LOCK etc.
cd3f0 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20   */.  int nRef; 
cd400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd410 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
cd420 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74  of pointers to t
cd430 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f  his structure */
cd440 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f  .  struct unixLo
cd450 63 6b 49 6e 66 6f 20 2a 70 4e 65 78 74 3b 20 20  ckInfo *pNext;  
cd460 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c     /* List of al
cd470 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 6f  l unixLockInfo o
cd480 62 6a 65 63 74 73 20 2a 2f 0a 20 20 73 74 72 75  bjects */.  stru
cd490 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20  ct unixLockInfo 
cd4a0 2a 70 50 72 65 76 3b 20 20 20 20 20 2f 2a 20 20  *pPrev;     /*  
cd4b0 20 20 2e 2e 2e 2e 20 64 6f 75 62 6c 79 20 6c 69    .... doubly li
cd4c0 6e 6b 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  nked */.};../*.*
cd4d0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
cd4e0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
cd4f0 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f  tructure is allo
cd500 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 6f  cated for each o
cd510 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 54  pen.** inode.  T
cd520 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6b 65  his structure ke
cd530 65 70 73 20 74 72 61 63 6b 20 6f 66 20 74 68 65  eps track of the
cd540 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73   number of locks
cd550 20 6f 6e 20 74 68 61 74 0a 2a 2a 20 69 6e 6f 64   on that.** inod
cd560 65 2e 20 20 49 66 20 61 20 63 6c 6f 73 65 20 69  e.  If a close i
cd570 73 20 61 74 74 65 6d 70 74 65 64 20 61 67 61 69  s attempted agai
cd580 6e 73 74 20 61 6e 20 69 6e 6f 64 65 20 74 68 61  nst an inode tha
cd590 74 20 69 73 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20  t is holding.** 
cd5a0 6c 6f 63 6b 73 2c 20 74 68 65 20 63 6c 6f 73 65  locks, the close
cd5b0 20 69 73 20 64 65 66 65 72 72 65 64 20 75 6e 74   is deferred unt
cd5c0 69 6c 20 61 6c 6c 20 6c 6f 63 6b 73 20 63 6c 65  il all locks cle
cd5d0 61 72 20 62 79 20 61 64 64 69 6e 67 20 74 68 65  ar by adding the
cd5e0 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70  .** file descrip
cd5f0 74 6f 72 20 74 6f 20 62 65 20 63 6c 6f 73 65 64  tor to be closed
cd600 20 74 6f 20 74 68 65 20 70 65 6e 64 69 6e 67 20   to the pending 
cd610 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f  list..**.** TODO
cd620 3a 20 20 43 6f 6e 73 69 64 65 72 20 63 68 61 6e  :  Consider chan
cd630 67 69 6e 67 20 74 68 69 73 20 73 6f 20 74 68 61  ging this so tha
cd640 74 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20  t there is only 
cd650 61 20 73 69 6e 67 6c 65 20 66 69 6c 65 0a 2a 2a  a single file.**
cd660 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20   descriptor for 
cd670 65 61 63 68 20 6f 70 65 6e 20 66 69 6c 65 2c 20  each open file, 
cd680 65 76 65 6e 20 77 68 65 6e 20 69 74 20 69 73 20  even when it is 
cd690 6f 70 65 6e 65 64 20 6d 75 6c 74 69 70 6c 65 20  opened multiple 
cd6a0 74 69 6d 65 73 2e 0a 2a 2a 20 54 68 65 20 63 6c  times..** The cl
cd6b0 6f 73 65 28 29 20 73 79 73 74 65 6d 20 63 61 6c  ose() system cal
cd6c0 6c 20 77 6f 75 6c 64 20 6f 6e 6c 79 20 6f 63 63  l would only occ
cd6d0 75 72 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74  ur when the last
cd6e0 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 75 73 69   database.** usi
cd6f0 6e 67 20 74 68 65 20 66 69 6c 65 20 63 6c 6f 73  ng the file clos
cd700 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e  es..*/.struct un
cd710 69 78 4f 70 65 6e 43 6e 74 20 7b 0a 20 20 73 74  ixOpenCnt {.  st
cd720 72 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20  ruct unixFileId 
cd730 66 69 6c 65 49 64 3b 20 20 20 2f 2a 20 54 68 65  fileId;   /* The
cd740 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f 0a 20   lookup key */. 
cd750 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20   int nRef;      
cd760 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
cd770 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65  Number of pointe
cd780 72 73 20 74 6f 20 74 68 69 73 20 73 74 72 75 63  rs to this struc
cd790 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c  ture */.  int nL
cd7a0 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  ock;            
cd7b0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
cd7c0 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c  of outstanding l
cd7d0 6f 63 6b 73 20 2a 2f 0a 20 20 55 6e 69 78 55 6e  ocks */.  UnixUn
cd7e0 75 73 65 64 46 64 20 2a 70 55 6e 75 73 65 64 3b  usedFd *pUnused;
cd7f0 20 20 20 20 20 20 2f 2a 20 55 6e 75 73 65 64 20        /* Unused 
cd800 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73  file descriptors
cd810 20 74 6f 20 63 6c 6f 73 65 20 2a 2f 0a 23 69 66   to close */.#if
cd820 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 65   OS_VXWORKS.  se
cd830 6d 5f 74 20 2a 70 53 65 6d 3b 20 20 20 20 20 20  m_t *pSem;      
cd840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
cd850 2a 20 4e 61 6d 65 64 20 50 4f 53 49 58 20 73 65  * Named POSIX se
cd860 6d 61 70 68 6f 72 65 20 2a 2f 0a 20 20 63 68 61  maphore */.  cha
cd870 72 20 61 53 65 6d 4e 61 6d 65 5b 4d 41 58 5f 50  r aSemName[MAX_P
cd880 41 54 48 4e 41 4d 45 2b 32 5d 3b 20 20 20 2f 2a  ATHNAME+2];   /*
cd890 20 4e 61 6d 65 20 6f 66 20 74 68 61 74 20 73 65   Name of that se
cd8a0 6d 61 70 68 6f 72 65 20 2a 2f 0a 23 65 6e 64 69  maphore */.#endi
cd8b0 66 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f  f.  struct unixO
cd8c0 70 65 6e 43 6e 74 20 2a 70 4e 65 78 74 2c 20 2a  penCnt *pNext, *
cd8d0 70 50 72 65 76 3b 20 20 20 2f 2a 20 4c 69 73 74  pPrev;   /* List
cd8e0 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4f 70 65 6e   of all unixOpen
cd8f0 43 6e 74 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 7d  Cnt objects */.}
cd900 3b 0a 0a 2f 2a 0a 2a 2a 20 4c 69 73 74 73 20 6f  ;../*.** Lists o
cd910 66 20 61 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e  f all unixLockIn
cd920 66 6f 20 61 6e 64 20 75 6e 69 78 4f 70 65 6e 43  fo and unixOpenC
cd930 6e 74 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 65  nt objects.  The
cd940 73 65 20 75 73 65 64 20 74 6f 20 62 65 20 68 61  se used to be ha
cd950 73 68 0a 2a 2a 20 74 61 62 6c 65 73 2e 20 20 42  sh.** tables.  B
cd960 75 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ut the number of
cd970 20 6f 62 6a 65 63 74 73 20 69 73 20 72 61 72 65   objects is rare
cd980 6c 79 20 6d 6f 72 65 20 74 68 61 6e 20 61 20 64  ly more than a d
cd990 6f 7a 65 6e 20 61 6e 64 0a 2a 2a 20 6e 65 76 65  ozen and.** neve
cd9a0 72 20 65 78 63 65 65 64 73 20 61 20 66 65 77 20  r exceeds a few 
cd9b0 74 68 6f 75 73 61 6e 64 2e 20 20 41 6e 64 20 6c  thousand.  And l
cd9c0 6f 6f 6b 75 70 20 69 73 20 6e 6f 74 20 6f 6e 20  ookup is not on 
cd9d0 61 20 63 72 69 74 69 63 61 6c 0a 2a 2a 20 70 61  a critical.** pa
cd9e0 74 68 20 73 6f 20 61 20 73 69 6d 70 6c 65 20 6c  th so a simple l
cd9f0 69 6e 6b 65 64 20 6c 69 73 74 20 77 69 6c 6c 20  inked list will 
cda00 73 75 66 66 69 63 65 2e 0a 2a 2f 0a 73 74 61 74  suffice..*/.stat
cda10 69 63 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f  ic struct unixLo
cda20 63 6b 49 6e 66 6f 20 2a 6c 6f 63 6b 4c 69 73 74  ckInfo *lockList
cda30 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 73 74 72   = 0;.static str
cda40 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20  uct unixOpenCnt 
cda50 2a 6f 70 65 6e 4c 69 73 74 20 3d 20 30 3b 0a 0a  *openList = 0;..
cda60 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 61 72 69 61  /*.** This varia
cda70 62 6c 65 20 72 65 6d 65 6d 62 65 72 73 20 77 68  ble remembers wh
cda80 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72  ether or not thr
cda90 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64  eads can overrid
cdaa0 65 20 65 61 63 68 20 6f 74 68 65 72 73 0a 2a 2a  e each others.**
cdab0 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 20 20   locks..**.**   
cdac0 20 30 3a 20 20 4e 6f 2e 20 20 54 68 72 65 61 64   0:  No.  Thread
cdad0 73 20 63 61 6e 6e 6f 74 20 6f 76 65 72 72 69 64  s cannot overrid
cdae0 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f  e each others lo
cdaf0 63 6b 73 2e 20 20 28 4c 69 6e 75 78 54 68 72 65  cks.  (LinuxThre
cdb00 61 64 73 29 0a 2a 2a 20 20 20 20 31 3a 20 20 59  ads).**    1:  Y
cdb10 65 73 2e 20 20 54 68 72 65 61 64 73 20 63 61 6e  es.  Threads can
cdb20 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 6f   override each o
cdb30 74 68 65 72 73 20 6c 6f 63 6b 73 2e 20 20 28 50  thers locks.  (P
cdb40 6f 73 69 78 20 26 20 4e 4c 50 54 29 0a 2a 2a 20  osix & NLPT).** 
cdb50 20 20 2d 31 3a 20 20 57 65 20 64 6f 6e 27 74 20    -1:  We don't 
cdb60 6b 6e 6f 77 20 79 65 74 2e 0a 2a 2a 0a 2a 2a 20  know yet..**.** 
cdb70 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c  On some systems,
cdb80 20 77 65 20 6b 6e 6f 77 20 61 74 20 63 6f 6d 70   we know at comp
cdb90 69 6c 65 2d 74 69 6d 65 20 69 66 20 74 68 72 65  ile-time if thre
cdba0 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65  ads can override
cdbb0 20 65 61 63 68 0a 2a 2a 20 6f 74 68 65 72 73 20   each.** others 
cdbc0 6c 6f 63 6b 73 2e 20 20 4f 6e 20 74 68 6f 73 65  locks.  On those
cdbd0 20 73 79 73 74 65 6d 73 2c 20 74 68 65 20 53 51   systems, the SQ
cdbe0 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52  LITE_THREAD_OVER
cdbf0 52 49 44 45 5f 4c 4f 43 4b 20 6d 61 63 72 6f 0a  RIDE_LOCK macro.
cdc00 2a 2a 20 77 69 6c 6c 20 62 65 20 73 65 74 20 61  ** will be set a
cdc10 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 20 20 4f  ppropriately.  O
cdc20 6e 20 6f 74 68 65 72 20 73 79 73 74 65 6d 73 2c  n other systems,
cdc30 20 77 65 20 68 61 76 65 20 74 6f 20 63 68 65 63   we have to chec
cdc40 6b 20 61 74 0a 2a 2a 20 72 75 6e 74 69 6d 65 2e  k at.** runtime.
cdc50 20 20 4f 6e 20 74 68 65 73 65 20 6c 61 74 74 65    On these latte
cdc60 72 20 73 79 73 74 65 6d 73 2c 20 53 51 4c 54 49  r systems, SQLTI
cdc70 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44  E_THREAD_OVERRID
cdc80 45 5f 4c 4f 43 4b 20 69 73 0a 2a 2a 20 75 6e 64  E_LOCK is.** und
cdc90 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  efined..**.** Th
cdca0 69 73 20 76 61 72 69 61 62 6c 65 20 6e 6f 72 6d  is variable norm
cdcb0 61 6c 6c 79 20 68 61 73 20 66 69 6c 65 20 73 63  ally has file sc
cdcc0 6f 70 65 20 6f 6e 6c 79 2e 20 20 42 75 74 20 64  ope only.  But d
cdcd0 75 72 69 6e 67 20 74 65 73 74 69 6e 67 2c 20 77  uring testing, w
cdce0 65 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 61 20 67  e make.** it a g
cdcf0 6c 6f 62 61 6c 20 73 6f 20 74 68 61 74 20 74 68  lobal so that th
cdd00 65 20 74 65 73 74 20 63 6f 64 65 20 63 61 6e 20  e test code can 
cdd10 63 68 61 6e 67 65 20 69 74 73 20 76 61 6c 75 65  change its value
cdd20 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 76 65 72   in order to ver
cdd30 69 66 79 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  ify.** that the 
cdd40 72 69 67 68 74 20 73 74 75 66 66 20 68 61 70 70  right stuff happ
cdd50 65 6e 73 20 69 6e 20 65 69 74 68 65 72 20 63 61  ens in either ca
cdd60 73 65 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  se..*/.#if SQLIT
cdd70 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20  E_THREADSAFE && 
cdd80 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f  defined(__linux_
cdd90 5f 29 0a 23 20 20 69 66 6e 64 65 66 20 53 51 4c  _).#  ifndef SQL
cdda0 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52  ITE_THREAD_OVERR
cddb0 49 44 45 5f 4c 4f 43 4b 0a 23 20 20 20 20 64 65  IDE_LOCK.#    de
cddc0 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45  fine SQLITE_THRE
cddd0 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b  AD_OVERRIDE_LOCK
cdde0 20 2d 31 0a 23 20 20 65 6e 64 69 66 0a 23 20 20   -1.#  endif.#  
cddf0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
cde00 54 0a 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65  T.int threadsOve
cde10 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c  rrideEachOthersL
cde20 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48  ocks = SQLITE_TH
cde30 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f  READ_OVERRIDE_LO
cde40 43 4b 3b 0a 23 20 20 65 6c 73 65 0a 73 74 61 74  CK;.#  else.stat
cde50 69 63 20 69 6e 74 20 74 68 72 65 61 64 73 4f 76  ic int threadsOv
cde60 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73  errideEachOthers
cde70 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54  Locks = SQLITE_T
cde80 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c  HREAD_OVERRIDE_L
cde90 4f 43 4b 3b 0a 23 20 20 65 6e 64 69 66 0a 23 65  OCK;.#  endif.#e
cdea0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ndif../*.** This
cdeb0 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73   structure holds
cdec0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73   information pas
cded0 73 65 64 20 69 6e 74 6f 20 69 6e 64 69 76 69 64  sed into individ
cdee0 75 61 6c 20 74 65 73 74 0a 2a 2a 20 74 68 72 65  ual test.** thre
cdef0 61 64 73 20 62 79 20 74 68 65 20 74 65 73 74 54  ads by the testT
cdf00 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61  hreadLockingBeha
cdf10 76 69 6f 72 28 29 20 72 6f 75 74 69 6e 65 2e 0a  vior() routine..
cdf20 2a 2f 0a 73 74 72 75 63 74 20 74 68 72 65 61 64  */.struct thread
cdf30 54 65 73 74 44 61 74 61 20 7b 0a 20 20 69 6e 74  TestData {.  int
cdf40 20 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 20   fd;            
cdf50 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 62      /* File to b
cdf60 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 73 74  e locked */.  st
cdf70 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b  ruct flock lock;
cdf80 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 6b       /* The lock
cdf90 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f  ing operation */
cdfa0 0a 20 20 69 6e 74 20 72 65 73 75 6c 74 3b 20 20  .  int result;  
cdfb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
cdfc0 75 6c 74 20 6f 66 20 74 68 65 20 6c 6f 63 6b 69  ult of the locki
cdfd0 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a  ng operation */.
cdfe0 7d 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  };..#if SQLITE_T
cdff0 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66  HREADSAFE && def
ce000 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a  ined(__linux__).
ce010 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
ce020 69 6f 6e 20 69 73 20 75 73 65 64 20 61 73 20 74  ion is used as t
ce030 68 65 20 6d 61 69 6e 20 72 6f 75 74 69 6e 65 20  he main routine 
ce040 66 6f 72 20 61 20 74 68 72 65 61 64 20 6c 61 75  for a thread lau
ce050 6e 63 68 65 64 20 62 79 0a 2a 2a 20 74 65 73 74  nched by.** test
ce060 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68  ThreadLockingBeh
ce070 61 76 69 6f 72 28 29 2e 20 49 74 20 74 65 73 74  avior(). It test
ce080 73 20 77 68 65 74 68 65 72 20 74 68 65 20 73 68  s whether the sh
ce090 61 72 65 64 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e  ared-lock obtain
ce0a0 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 6d 61 69  ed.** by the mai
ce0b0 6e 20 74 68 72 65 61 64 20 69 6e 20 74 65 73 74  n thread in test
ce0c0 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68  ThreadLockingBeh
ce0d0 61 76 69 6f 72 28 29 20 63 6f 6e 66 6c 69 63 74  avior() conflict
ce0e0 73 20 77 69 74 68 20 61 0a 2a 2a 20 68 79 70 6f  s with a.** hypo
ce0f0 74 68 65 74 69 63 61 6c 20 77 72 69 74 65 2d 6c  thetical write-l
ce100 6f 63 6b 20 6f 62 74 61 69 6e 65 64 20 62 79 20  ock obtained by 
ce110 74 68 69 73 20 74 68 72 65 61 64 20 6f 6e 20 74  this thread on t
ce120 68 65 20 73 61 6d 65 20 66 69 6c 65 2e 0a 2a 2a  he same file..**
ce130 0a 2a 2a 20 54 68 65 20 77 72 69 74 65 2d 6c 6f  .** The write-lo
ce140 63 6b 20 69 73 20 6e 6f 74 20 61 63 74 75 61 6c  ck is not actual
ce150 6c 79 20 61 63 71 75 69 72 65 64 2c 20 61 73 20  ly acquired, as 
ce160 74 68 69 73 20 69 73 20 6e 6f 74 20 70 6f 73 73  this is not poss
ce170 69 62 6c 65 20 69 66 20 0a 2a 2a 20 74 68 65 20  ible if .** the 
ce180 66 69 6c 65 20 69 73 20 6f 70 65 6e 20 69 6e 20  file is open in 
ce190 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 28  read-only mode (
ce1a0 73 65 65 20 74 69 63 6b 65 74 20 23 33 34 37 32  see ticket #3472
ce1b0 29 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76 6f  )..*/ .static vo
ce1c0 69 64 20 2a 74 68 72 65 61 64 4c 6f 63 6b 69 6e  id *threadLockin
ce1d0 67 54 65 73 74 28 76 6f 69 64 20 2a 70 41 72 67  gTest(void *pArg
ce1e0 29 7b 0a 20 20 73 74 72 75 63 74 20 74 68 72 65  ){.  struct thre
ce1f0 61 64 54 65 73 74 44 61 74 61 20 2a 70 44 61 74  adTestData *pDat
ce200 61 20 3d 20 28 73 74 72 75 63 74 20 74 68 72 65  a = (struct thre
ce210 61 64 54 65 73 74 44 61 74 61 2a 29 70 41 72 67  adTestData*)pArg
ce220 3b 0a 20 20 70 44 61 74 61 2d 3e 72 65 73 75 6c  ;.  pData->resul
ce230 74 20 3d 20 66 63 6e 74 6c 28 70 44 61 74 61 2d  t = fcntl(pData-
ce240 3e 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 70  >fd, F_GETLK, &p
ce250 44 61 74 61 2d 3e 6c 6f 63 6b 29 3b 0a 20 20 72  Data->lock);.  r
ce260 65 74 75 72 6e 20 70 41 72 67 3b 0a 7d 0a 23 65  eturn pArg;.}.#e
ce270 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54  ndif /* SQLITE_T
ce280 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66  HREADSAFE && def
ce290 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 20  ined(__linux__) 
ce2a0 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f  */...#if SQLITE_
ce2b0 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65  THREADSAFE && de
ce2c0 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29  fined(__linux__)
ce2d0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63  ./*.** This proc
ce2e0 65 64 75 72 65 20 61 74 74 65 6d 70 74 73 20 74  edure attempts t
ce2f0 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74  o determine whet
ce300 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61  her or not threa
ce310 64 73 0a 2a 2a 20 63 61 6e 20 6f 76 65 72 72 69  ds.** can overri
ce320 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c  de each others l
ce330 6f 63 6b 73 20 74 68 65 6e 20 73 65 74 73 20 74  ocks then sets t
ce340 68 65 20 0a 2a 2a 20 74 68 72 65 61 64 73 4f 76  he .** threadsOv
ce350 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73  errideEachOthers
ce360 4c 6f 63 6b 73 20 76 61 72 69 61 62 6c 65 20 61  Locks variable a
ce370 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f  ppropriately..*/
ce380 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 65 73  .static void tes
ce390 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65  tThreadLockingBe
ce3a0 68 61 76 69 6f 72 28 69 6e 74 20 66 64 5f 6f 72  havior(int fd_or
ce3b0 69 67 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20  ig){.  int fd;. 
ce3c0 20 69 6e 74 20 72 63 3b 0a 20 20 73 74 72 75 63   int rc;.  struc
ce3d0 74 20 74 68 72 65 61 64 54 65 73 74 44 61 74 61  t threadTestData
ce3e0 20 64 3b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f   d;.  struct flo
ce3f0 63 6b 20 6c 3b 0a 20 20 70 74 68 72 65 61 64 5f  ck l;.  pthread_
ce400 74 20 74 3b 0a 0a 20 20 66 64 20 3d 20 64 75 70  t t;..  fd = dup
ce410 28 66 64 5f 6f 72 69 67 29 3b 0a 20 20 69 66 28  (fd_orig);.  if(
ce420 20 66 64 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a   fd<0 ) return;.
ce430 20 20 6d 65 6d 73 65 74 28 26 6c 2c 20 30 2c 20    memset(&l, 0, 
ce440 73 69 7a 65 6f 66 28 6c 29 29 3b 0a 20 20 6c 2e  sizeof(l));.  l.
ce450 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b  l_type = F_RDLCK
ce460 3b 0a 20 20 6c 2e 6c 5f 6c 65 6e 20 3d 20 31 3b  ;.  l.l_len = 1;
ce470 0a 20 20 6c 2e 6c 5f 73 74 61 72 74 20 3d 20 30  .  l.l_start = 0
ce480 3b 0a 20 20 6c 2e 6c 5f 77 68 65 6e 63 65 20 3d  ;.  l.l_whence =
ce490 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 72 63 20   SEEK_SET;.  rc 
ce4a0 3d 20 66 63 6e 74 6c 28 66 64 5f 6f 72 69 67 2c  = fcntl(fd_orig,
ce4b0 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 29 3b 0a 20   F_SETLK, &l);. 
ce4c0 20 69 66 28 20 72 63 21 3d 30 20 29 20 72 65 74   if( rc!=0 ) ret
ce4d0 75 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64  urn;.  memset(&d
ce4e0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 29 29 3b  , 0, sizeof(d));
ce4f0 0a 20 20 64 2e 66 64 20 3d 20 66 64 3b 0a 20 20  .  d.fd = fd;.  
ce500 64 2e 6c 6f 63 6b 20 3d 20 6c 3b 0a 20 20 64 2e  d.lock = l;.  d.
ce510 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f  lock.l_type = F_
ce520 57 52 4c 43 4b 3b 0a 20 20 69 66 28 20 70 74 68  WRLCK;.  if( pth
ce530 72 65 61 64 5f 63 72 65 61 74 65 28 26 74 2c 20  read_create(&t, 
ce540 30 2c 20 74 68 72 65 61 64 4c 6f 63 6b 69 6e 67  0, threadLocking
ce550 54 65 73 74 2c 20 26 64 29 3d 3d 30 20 29 7b 0a  Test, &d)==0 ){.
ce560 20 20 20 20 70 74 68 72 65 61 64 5f 6a 6f 69 6e      pthread_join
ce570 28 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 63 6c  (t, 0);.  }.  cl
ce580 6f 73 65 28 66 64 29 3b 0a 20 20 69 66 28 20 64  ose(fd);.  if( d
ce590 2e 72 65 73 75 6c 74 21 3d 30 20 29 20 72 65 74  .result!=0 ) ret
ce5a0 75 72 6e 3b 0a 20 20 74 68 72 65 61 64 73 4f 76  urn;.  threadsOv
ce5b0 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73  errideEachOthers
ce5c0 4c 6f 63 6b 73 20 3d 20 28 64 2e 6c 6f 63 6b 2e  Locks = (d.lock.
ce5d0 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 29  l_type==F_UNLCK)
ce5e0 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
ce5f0 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20  LITE_THREADSAFE 
ce600 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e  && defined(__lin
ce610 75 78 5f 5f 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  ux__) */../*.** 
ce620 52 65 6c 65 61 73 65 20 61 20 75 6e 69 78 4c 6f  Release a unixLo
ce630 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  ckInfo structure
ce640 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f   previously allo
ce650 63 61 74 65 64 20 62 79 20 66 69 6e 64 4c 6f 63  cated by findLoc
ce660 6b 49 6e 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a 20 54  kInfo()..**.** T
ce670 68 65 20 6d 75 74 65 78 20 65 6e 74 65 72 65 64  he mutex entered
ce680 20 75 73 69 6e 67 20 74 68 65 20 75 6e 69 78 45   using the unixE
ce690 6e 74 65 72 4d 75 74 65 78 28 29 20 66 75 6e 63  nterMutex() func
ce6a0 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 68 65 6c  tion must be hel
ce6b0 64 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 66  d.** when this f
ce6c0 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
ce6d0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
ce6e0 64 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66  d releaseLockInf
ce6f0 6f 28 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63  o(struct unixLoc
ce700 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 29 7b 0a 20  kInfo *pLock){. 
ce710 20 61 73 73 65 72 74 28 20 75 6e 69 78 4d 75 74   assert( unixMut
ce720 65 78 48 65 6c 64 28 29 20 29 3b 0a 20 20 69 66  exHeld() );.  if
ce730 28 20 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70  ( pLock ){.    p
ce740 4c 6f 63 6b 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20  Lock->nRef--;.  
ce750 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 6e 52 65    if( pLock->nRe
ce760 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66  f==0 ){.      if
ce770 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 20 29  ( pLock->pPrev )
ce780 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
ce790 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d 3e  ( pLock->pPrev->
ce7a0 70 4e 65 78 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a  pNext==pLock );.
ce7b0 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70          pLock->p
ce7c0 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4c  Prev->pNext = pL
ce7d0 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  ock->pNext;.    
ce7e0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
ce7f0 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 4c 69 73   assert( lockLis
ce800 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20  t==pLock );.    
ce810 20 20 20 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 70      lockList = p
ce820 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  Lock->pNext;.   
ce830 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
ce840 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 29 7b 0a 20  Lock->pNext ){. 
ce850 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
ce860 4c 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50 72  Lock->pNext->pPr
ce870 65 76 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20  ev==pLock );.   
ce880 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78       pLock->pNex
ce890 74 2d 3e 70 50 72 65 76 20 3d 20 70 4c 6f 63 6b  t->pPrev = pLock
ce8a0 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 7d  ->pPrev;.      }
ce8b0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
ce8c0 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20  ree(pLock);.    
ce8d0 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  }.  }.}../*.** R
ce8e0 65 6c 65 61 73 65 20 61 20 75 6e 69 78 4f 70 65  elease a unixOpe
ce8f0 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 70  nCnt structure p
ce900 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61  reviously alloca
ce910 74 65 64 20 62 79 20 66 69 6e 64 4c 6f 63 6b 49  ted by findLockI
ce920 6e 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  nfo()..**.** The
ce930 20 6d 75 74 65 78 20 65 6e 74 65 72 65 64 20 75   mutex entered u
ce940 73 69 6e 67 20 74 68 65 20 75 6e 69 78 45 6e 74  sing the unixEnt
ce950 65 72 4d 75 74 65 78 28 29 20 66 75 6e 63 74 69  erMutex() functi
ce960 6f 6e 20 6d 75 73 74 20 62 65 20 68 65 6c 64 0a  on must be held.
ce970 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e  ** when this fun
ce980 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e  ction is called.
ce990 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
ce9a0 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 73  releaseOpenCnt(s
ce9b0 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e  truct unixOpenCn
ce9c0 74 20 2a 70 4f 70 65 6e 29 7b 0a 20 20 61 73 73  t *pOpen){.  ass
ce9d0 65 72 74 28 20 75 6e 69 78 4d 75 74 65 78 48 65  ert( unixMutexHe
ce9e0 6c 64 28 29 20 29 3b 0a 20 20 69 66 28 20 70 4f  ld() );.  if( pO
ce9f0 70 65 6e 20 29 7b 0a 20 20 20 20 70 4f 70 65 6e  pen ){.    pOpen
cea00 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66  ->nRef--;.    if
cea10 28 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 3d 3d 30  ( pOpen->nRef==0
cea20 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f   ){.      if( pO
cea30 70 65 6e 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20  pen->pPrev ){.  
cea40 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
cea50 70 65 6e 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78  pen->pPrev->pNex
cea60 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20  t==pOpen );.    
cea70 20 20 20 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76      pOpen->pPrev
cea80 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 70 65 6e 2d  ->pNext = pOpen-
cea90 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 65  >pNext;.      }e
ceaa0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73  lse{.        ass
ceab0 65 72 74 28 20 6f 70 65 6e 4c 69 73 74 3d 3d 70  ert( openList==p
ceac0 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 20 20  Open );.        
cead0 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e  openList = pOpen
ceae0 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d  ->pNext;.      }
ceaf0 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 6e  .      if( pOpen
ceb00 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20  ->pNext ){.     
ceb10 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 65 6e     assert( pOpen
ceb20 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d  ->pNext->pPrev==
ceb30 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 20  pOpen );.       
ceb40 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 2d 3e 70   pOpen->pNext->p
ceb50 50 72 65 76 20 3d 20 70 4f 70 65 6e 2d 3e 70 50  Prev = pOpen->pP
ceb60 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rev;.      }.   
ceb70 20 20 20 61 73 73 65 72 74 28 20 21 70 4f 70 65     assert( !pOpe
ceb80 6e 2d 3e 70 55 6e 75 73 65 64 20 29 3b 0a 20 20  n->pUnused );.  
ceb90 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
ceba0 28 70 4f 70 65 6e 29 3b 0a 20 20 20 20 7d 0a 20  (pOpen);.    }. 
cebb0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65   }.}../*.** Give
cebc0 6e 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70  n a file descrip
cebd0 74 6f 72 2c 20 6c 6f 63 61 74 65 20 75 6e 69 78  tor, locate unix
cebe0 4c 6f 63 6b 49 6e 66 6f 20 61 6e 64 20 75 6e 69  LockInfo and uni
cebf0 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75  xOpenCnt structu
cec00 72 65 73 20 74 68 61 74 0a 2a 2a 20 64 65 73 63  res that.** desc
cec10 72 69 62 65 73 20 74 68 61 74 20 66 69 6c 65 20  ribes that file 
cec20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 43 72 65  descriptor.  Cre
cec30 61 74 65 20 6e 65 77 20 6f 6e 65 73 20 69 66 20  ate new ones if 
cec40 6e 65 63 65 73 73 61 72 79 2e 20 20 54 68 65 0a  necessary.  The.
cec50 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73  ** return values
cec60 20 6d 69 67 68 74 20 62 65 20 75 6e 69 6e 69 74   might be uninit
cec70 69 61 6c 69 7a 65 64 20 69 66 20 61 6e 20 65 72  ialized if an er
cec80 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a  ror occurs..**.*
cec90 2a 20 54 68 65 20 6d 75 74 65 78 20 65 6e 74 65  * The mutex ente
ceca0 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 75 6e  red using the un
cecb0 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20 66  ixEnterMutex() f
cecc0 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20  unction must be 
cecd0 68 65 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68 69  held.** when thi
cece0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
cecf0 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  lled..**.** Retu
ced00 72 6e 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74  rn an appropriat
ced10 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f  e error code..*/
ced20 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6e 64  .static int find
ced30 4c 6f 63 6b 49 6e 66 6f 28 0a 20 20 75 6e 69 78  LockInfo(.  unix
ced40 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20  File *pFile,    
ced50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e             /* Un
ced60 69 78 20 66 69 6c 65 20 77 69 74 68 20 66 69 6c  ix file with fil
ced70 65 20 64 65 73 63 20 75 73 65 64 20 69 6e 20 74  e desc used in t
ced80 68 65 20 6b 65 79 20 2a 2f 0a 20 20 73 74 72 75  he key */.  stru
ced90 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20  ct unixLockInfo 
ceda0 2a 2a 70 70 4c 6f 63 6b 2c 20 20 2f 2a 20 52 65  **ppLock,  /* Re
cedb0 74 75 72 6e 20 74 68 65 20 75 6e 69 78 4c 6f 63  turn the unixLoc
cedc0 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  kInfo structure 
cedd0 68 65 72 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  here */.  struct
cede0 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 2a 70   unixOpenCnt **p
cedf0 70 4f 70 65 6e 20 20 20 20 2f 2a 20 52 65 74 75  pOpen    /* Retu
cee00 72 6e 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43  rn the unixOpenC
cee10 6e 74 20 73 74 72 75 63 74 75 72 65 20 68 65 72  nt structure her
cee20 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
cee30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
cee40 20 20 20 20 20 20 20 20 20 2f 2a 20 53 79 73 74           /* Syst
cee50 65 6d 20 63 61 6c 6c 20 72 65 74 75 72 6e 20 63  em call return c
cee60 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 66 64 3b  ode */.  int fd;
cee70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cee80 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66          /* The f
cee90 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66  ile descriptor f
ceea0 6f 72 20 70 46 69 6c 65 20 2a 2f 0a 20 20 73 74  or pFile */.  st
ceeb0 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79  ruct unixLockKey
ceec0 20 6c 6f 63 6b 4b 65 79 3b 20 20 20 20 2f 2a 20   lockKey;    /* 
ceed0 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f 72 20 74  Lookup key for t
ceee0 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20  he unixLockInfo 
ceef0 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 73  structure */.  s
cef00 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64  truct unixFileId
cef10 20 66 69 6c 65 49 64 3b 20 20 20 20 20 20 2f 2a   fileId;      /*
cef20 20 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f 72 20   Lookup key for 
cef30 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20  the unixOpenCnt 
cef40 73 74 72 75 63 74 20 2a 2f 0a 20 20 73 74 72 75  struct */.  stru
cef50 63 74 20 73 74 61 74 20 73 74 61 74 62 75 66 3b  ct stat statbuf;
cef60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
cef70 77 2d 6c 65 76 65 6c 20 66 69 6c 65 20 69 6e 66  w-level file inf
cef80 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 74  ormation */.  st
cef90 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66  ruct unixLockInf
cefa0 6f 20 2a 70 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20  o *pLock = 0;/* 
cefb0 43 61 6e 64 69 64 61 74 65 20 75 6e 69 78 4c 6f  Candidate unixLo
cefc0 63 6b 49 6e 66 6f 20 6f 62 6a 65 63 74 20 2a 2f  ckInfo object */
cefd0 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70  .  struct unixOp
cefe0 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 20 20 20  enCnt *pOpen;   
ceff0 20 20 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 75    /* Candidate u
cf000 6e 69 78 4f 70 65 6e 43 6e 74 20 6f 62 6a 65 63  nixOpenCnt objec
cf010 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  t */..  assert( 
cf020 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28 29 20  unixMutexHeld() 
cf030 29 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 6c 6f 77  );..  /* Get low
cf040 2d 6c 65 76 65 6c 20 69 6e 66 6f 72 6d 61 74 69  -level informati
cf050 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66 69 6c  on about the fil
cf060 65 20 74 68 61 74 20 77 65 20 63 61 6e 20 75 73  e that we can us
cf070 65 64 20 74 6f 0a 20 20 2a 2a 20 63 72 65 61 74  ed to.  ** creat
cf080 65 20 61 20 75 6e 69 71 75 65 20 6e 61 6d 65 20  e a unique name 
cf090 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 0a 20 20  for the file..  
cf0a0 2a 2f 0a 20 20 66 64 20 3d 20 70 46 69 6c 65 2d  */.  fd = pFile-
cf0b0 3e 68 3b 0a 20 20 72 63 20 3d 20 66 73 74 61 74  >h;.  rc = fstat
cf0c0 28 66 64 2c 20 26 73 74 61 74 62 75 66 29 3b 0a  (fd, &statbuf);.
cf0d0 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20    if( rc!=0 ){. 
cf0e0 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
cf0f0 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 23 69 66  rno = errno;.#if
cf100 64 65 66 20 45 4f 56 45 52 46 4c 4f 57 0a 20 20  def EOVERFLOW.  
cf110 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 61 73    if( pFile->las
cf120 74 45 72 72 6e 6f 3d 3d 45 4f 56 45 52 46 4c 4f  tErrno==EOVERFLO
cf130 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  W ) return SQLIT
cf140 45 5f 4e 4f 4c 46 53 3b 0a 23 65 6e 64 69 66 0a  E_NOLFS;.#endif.
cf150 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
cf160 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 0a 23 69  E_IOERR;.  }..#i
cf170 66 64 65 66 20 5f 5f 41 50 50 4c 45 5f 5f 0a 20  fdef __APPLE__. 
cf180 20 2f 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e 20 61   /* On OS X on a
cf190 6e 20 6d 73 64 6f 73 20 66 69 6c 65 73 79 73 74  n msdos filesyst
cf1a0 65 6d 2c 20 74 68 65 20 69 6e 6f 64 65 20 6e 75  em, the inode nu
cf1b0 6d 62 65 72 20 69 73 20 72 65 70 6f 72 74 65 64  mber is reported
cf1c0 0a 20 20 2a 2a 20 69 6e 63 6f 72 72 65 63 74 6c  .  ** incorrectl
cf1d0 79 20 66 6f 72 20 7a 65 72 6f 2d 73 69 7a 65 20  y for zero-size 
cf1e0 66 69 6c 65 73 2e 20 20 53 65 65 20 74 69 63 6b  files.  See tick
cf1f0 65 74 20 23 33 32 36 30 2e 20 20 54 6f 20 77 6f  et #3260.  To wo
cf200 72 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 20 74  rk.  ** around t
cf210 68 69 73 20 70 72 6f 62 6c 65 6d 20 28 77 65 20  his problem (we 
cf220 63 6f 6e 73 69 64 65 72 20 69 74 20 61 20 62 75  consider it a bu
cf230 67 20 69 6e 20 4f 53 20 58 2c 20 6e 6f 74 20 53  g in OS X, not S
cf240 51 4c 69 74 65 29 0a 20 20 2a 2a 20 77 65 20 61  QLite).  ** we a
cf250 6c 77 61 79 73 20 69 6e 63 72 65 61 73 65 20 74  lways increase t
cf260 68 65 20 66 69 6c 65 20 73 69 7a 65 20 74 6f 20  he file size to 
cf270 31 20 62 79 20 77 72 69 74 69 6e 67 20 61 20 73  1 by writing a s
cf280 69 6e 67 6c 65 20 62 79 74 65 0a 20 20 2a 2a 20  ingle byte.  ** 
cf290 70 72 69 6f 72 20 74 6f 20 61 63 63 65 73 73 69  prior to accessi
cf2a0 6e 67 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d  ng the inode num
cf2b0 62 65 72 2e 20 20 54 68 65 20 6f 6e 65 20 62 79  ber.  The one by
cf2c0 74 65 20 77 72 69 74 74 65 6e 20 69 73 0a 20 20  te written is.  
cf2d0 2a 2a 20 61 6e 20 41 53 43 49 49 20 27 53 27 20  ** an ASCII 'S' 
cf2e0 63 68 61 72 61 63 74 65 72 20 77 68 69 63 68 20  character which 
cf2f0 61 6c 73 6f 20 68 61 70 70 65 6e 73 20 74 6f 20  also happens to 
cf300 62 65 20 74 68 65 20 66 69 72 73 74 20 62 79 74  be the first byt
cf310 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 68 65  e.  ** in the he
cf320 61 64 65 72 20 6f 66 20 65 76 65 72 79 20 53 51  ader of every SQ
cf330 4c 69 74 65 20 64 61 74 61 62 61 73 65 2e 20 20  Lite database.  
cf340 49 6e 20 74 68 69 73 20 77 61 79 2c 20 69 66 20  In this way, if 
cf350 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 61 20  there.  ** is a 
cf360 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 73  race condition s
cf370 75 63 68 20 74 68 61 74 20 61 6e 6f 74 68 65 72  uch that another
cf380 20 74 68 72 65 61 64 20 68 61 73 20 61 6c 72 65   thread has alre
cf390 61 64 79 20 70 6f 70 75 6c 61 74 65 64 0a 20 20  ady populated.  
cf3a0 2a 2a 20 74 68 65 20 66 69 72 73 74 20 70 61 67  ** the first pag
cf3b0 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
cf3c0 65 2c 20 6e 6f 20 64 61 6d 61 67 65 20 69 73 20  e, no damage is 
cf3d0 64 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  done..  */.  if(
cf3e0 20 73 74 61 74 62 75 66 2e 73 74 5f 73 69 7a 65   statbuf.st_size
cf3f0 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ==0 ){.    rc = 
cf400 77 72 69 74 65 28 66 64 2c 20 22 53 22 2c 20 31  write(fd, "S", 1
cf410 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 31  );.    if( rc!=1
cf420 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
cf430 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20   SQLITE_IOERR;. 
cf440 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 66 73     }.    rc = fs
cf450 74 61 74 28 66 64 2c 20 26 73 74 61 74 62 75 66  tat(fd, &statbuf
cf460 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 30  );.    if( rc!=0
cf470 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d   ){.      pFile-
cf480 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72  >lastErrno = err
cf490 6e 6f 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  no;.      return
cf4a0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20   SQLITE_IOERR;. 
cf4b0 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
cf4c0 0a 20 20 6d 65 6d 73 65 74 28 26 6c 6f 63 6b 4b  .  memset(&lockK
cf4d0 65 79 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6c 6f  ey, 0, sizeof(lo
cf4e0 63 6b 4b 65 79 29 29 3b 0a 20 20 6c 6f 63 6b 4b  ckKey));.  lockK
cf4f0 65 79 2e 66 69 64 2e 64 65 76 20 3d 20 73 74 61  ey.fid.dev = sta
cf500 74 62 75 66 2e 73 74 5f 64 65 76 3b 0a 23 69 66  tbuf.st_dev;.#if
cf510 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 6c 6f   OS_VXWORKS.  lo
cf520 63 6b 4b 65 79 2e 66 69 64 2e 70 49 64 20 3d 20  ckKey.fid.pId = 
cf530 70 46 69 6c 65 2d 3e 70 49 64 3b 0a 23 65 6c 73  pFile->pId;.#els
cf540 65 0a 20 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e  e.  lockKey.fid.
cf550 69 6e 6f 20 3d 20 73 74 61 74 62 75 66 2e 73 74  ino = statbuf.st
cf560 5f 69 6e 6f 3b 0a 23 65 6e 64 69 66 0a 23 69 66  _ino;.#endif.#if
cf570 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
cf580 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  FE && defined(__
cf590 6c 69 6e 75 78 5f 5f 29 0a 20 20 69 66 28 20 74  linux__).  if( t
cf5a0 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 61  hreadsOverrideEa
cf5b0 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 3c 30 20  chOthersLocks<0 
cf5c0 29 7b 0a 20 20 20 20 74 65 73 74 54 68 72 65 61  ){.    testThrea
cf5d0 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72  dLockingBehavior
cf5e0 28 66 64 29 3b 0a 20 20 7d 0a 20 20 6c 6f 63 6b  (fd);.  }.  lock
cf5f0 4b 65 79 2e 74 69 64 20 3d 20 74 68 72 65 61 64  Key.tid = thread
cf600 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68  sOverrideEachOth
cf610 65 72 73 4c 6f 63 6b 73 20 3f 20 30 20 3a 20 70  ersLocks ? 0 : p
cf620 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 23  thread_self();.#
cf630 65 6e 64 69 66 0a 20 20 66 69 6c 65 49 64 20 3d  endif.  fileId =
cf640 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 3b 0a 20 20   lockKey.fid;.  
cf650 69 66 28 20 70 70 4c 6f 63 6b 21 3d 30 20 29 7b  if( ppLock!=0 ){
cf660 0a 20 20 20 20 70 4c 6f 63 6b 20 3d 20 6c 6f 63  .    pLock = loc
cf670 6b 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65  kList;.    while
cf680 28 20 70 4c 6f 63 6b 20 26 26 20 6d 65 6d 63 6d  ( pLock && memcm
cf690 70 28 26 6c 6f 63 6b 4b 65 79 2c 20 26 70 4c 6f  p(&lockKey, &pLo
cf6a0 63 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c 20 73 69 7a  ck->lockKey, siz
cf6b0 65 6f 66 28 6c 6f 63 6b 4b 65 79 29 29 20 29 7b  eof(lockKey)) ){
cf6c0 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 70  .      pLock = p
cf6d0 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  Lock->pNext;.   
cf6e0 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b   }.    if( pLock
cf6f0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f  ==0 ){.      pLo
cf700 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  ck = sqlite3_mal
cf710 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 4c 6f  loc( sizeof(*pLo
cf720 63 6b 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28  ck) );.      if(
cf730 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20   pLock==0 ){.   
cf740 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
cf750 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20  _NOMEM;.        
cf760 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f  goto exit_findlo
cf770 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 20 7d 0a  ckinfo;.      }.
cf780 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63        pLock->loc
cf790 6b 4b 65 79 20 3d 20 6c 6f 63 6b 4b 65 79 3b 0a  kKey = lockKey;.
cf7a0 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65        pLock->nRe
cf7b0 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4c 6f  f = 1;.      pLo
cf7c0 63 6b 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 20 20  ck->cnt = 0;.   
cf7d0 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79     pLock->lockty
cf7e0 70 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c  pe = 0;.      pL
cf7f0 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d 20 6c 6f 63  ock->pNext = loc
cf800 6b 4c 69 73 74 3b 0a 20 20 20 20 20 20 70 4c 6f  kList;.      pLo
cf810 63 6b 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20  ck->pPrev = 0;. 
cf820 20 20 20 20 20 69 66 28 20 6c 6f 63 6b 4c 69 73       if( lockLis
cf830 74 20 29 20 6c 6f 63 6b 4c 69 73 74 2d 3e 70 50  t ) lockList->pP
cf840 72 65 76 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 20  rev = pLock;.   
cf850 20 20 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 70 4c     lockList = pL
cf860 6f 63 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ock;.    }else{.
cf870 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65        pLock->nRe
cf880 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a  f++;.    }.    *
cf890 70 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a  ppLock = pLock;.
cf8a0 20 20 7d 0a 20 20 69 66 28 20 70 70 4f 70 65 6e    }.  if( ppOpen
cf8b0 21 3d 30 20 29 7b 0a 20 20 20 20 70 4f 70 65 6e  !=0 ){.    pOpen
cf8c0 20 3d 20 6f 70 65 6e 4c 69 73 74 3b 0a 20 20 20   = openList;.   
cf8d0 20 77 68 69 6c 65 28 20 70 4f 70 65 6e 20 26 26   while( pOpen &&
cf8e0 20 6d 65 6d 63 6d 70 28 26 66 69 6c 65 49 64 2c   memcmp(&fileId,
cf8f0 20 26 70 4f 70 65 6e 2d 3e 66 69 6c 65 49 64 2c   &pOpen->fileId,
cf900 20 73 69 7a 65 6f 66 28 66 69 6c 65 49 64 29 29   sizeof(fileId))
cf910 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e 20   ){.      pOpen 
cf920 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a  = pOpen->pNext;.
cf930 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4f      }.    if( pO
cf940 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  pen==0 ){.      
cf950 70 4f 70 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f  pOpen = sqlite3_
cf960 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a  malloc( sizeof(*
cf970 70 4f 70 65 6e 29 20 29 3b 0a 20 20 20 20 20 20  pOpen) );.      
cf980 69 66 28 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a  if( pOpen==0 ){.
cf990 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 4c          releaseL
cf9a0 6f 63 6b 49 6e 66 6f 28 70 4c 6f 63 6b 29 3b 0a  ockInfo(pLock);.
cf9b0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
cf9c0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
cf9d0 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e     goto exit_fin
cf9e0 64 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20  dlockinfo;.     
cf9f0 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28   }.      memset(
cfa00 70 4f 70 65 6e 2c 20 30 2c 20 73 69 7a 65 6f 66  pOpen, 0, sizeof
cfa10 28 2a 70 4f 70 65 6e 29 29 3b 0a 20 20 20 20 20  (*pOpen));.     
cfa20 20 70 4f 70 65 6e 2d 3e 66 69 6c 65 49 64 20 3d   pOpen->fileId =
cfa30 20 66 69 6c 65 49 64 3b 0a 20 20 20 20 20 20 70   fileId;.      p
cfa40 4f 70 65 6e 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a  Open->nRef = 1;.
cfa50 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 4e 65        pOpen->pNe
cfa60 78 74 20 3d 20 6f 70 65 6e 4c 69 73 74 3b 0a 20  xt = openList;. 
cfa70 20 20 20 20 20 69 66 28 20 6f 70 65 6e 4c 69 73       if( openLis
cfa80 74 20 29 20 6f 70 65 6e 4c 69 73 74 2d 3e 70 50  t ) openList->pP
cfa90 72 65 76 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 20  rev = pOpen;.   
cfaa0 20 20 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f     openList = pO
cfab0 70 65 6e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  pen;.    }else{.
cfac0 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65        pOpen->nRe
cfad0 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a  f++;.    }.    *
cfae0 70 70 4f 70 65 6e 20 3d 20 70 4f 70 65 6e 3b 0a  ppOpen = pOpen;.
cfaf0 20 20 7d 0a 0a 65 78 69 74 5f 66 69 6e 64 6c 6f    }..exit_findlo
cfb00 63 6b 69 6e 66 6f 3a 0a 20 20 72 65 74 75 72 6e  ckinfo:.  return
cfb10 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66   rc;.}../*.** If
cfb20 20 77 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c   we are currentl
cfb30 79 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74  y in a different
cfb40 20 74 68 72 65 61 64 20 74 68 61 6e 20 74 68 65   thread than the
cfb50 20 74 68 72 65 61 64 20 74 68 61 74 20 74 68 65   thread that the
cfb60 0a 2a 2a 20 75 6e 69 78 46 69 6c 65 20 61 72 67  .** unixFile arg
cfb70 75 6d 65 6e 74 20 62 65 6c 6f 6e 67 73 20 74 6f  ument belongs to
cfb80 2c 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20  , then transfer 
cfb90 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 74 68 65  ownership of the
cfba0 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 6f 76 65   unixFile.** ove
cfbb0 72 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74  r to the current
cfbc0 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41   thread..**.** A
cfbd0 20 75 6e 69 78 46 69 6c 65 20 69 73 20 6f 6e 6c   unixFile is onl
cfbe0 79 20 6f 77 6e 65 64 20 62 79 20 61 20 74 68 72  y owned by a thr
cfbf0 65 61 64 20 6f 6e 20 73 79 73 74 65 6d 73 20 74  ead on systems t
cfc00 68 61 74 20 75 73 65 20 4c 69 6e 75 78 54 68 72  hat use LinuxThr
cfc10 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f 77 6e 65  eads..**.** Owne
cfc20 72 73 68 69 70 20 74 72 61 6e 73 66 65 72 20 69  rship transfer i
cfc30 73 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 69  s only allowed i
cfc40 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20 69  f the unixFile i
cfc50 73 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f  s currently unlo
cfc60 63 6b 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20  cked..** If the 
cfc70 75 6e 69 78 46 69 6c 65 20 69 73 20 6c 6f 63 6b  unixFile is lock
cfc80 65 64 20 61 6e 64 20 61 6e 20 6f 77 6e 65 72 73  ed and an owners
cfc90 68 69 70 20 69 73 20 77 72 6f 6e 67 2c 20 74 68  hip is wrong, th
cfca0 65 6e 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c  en return.** SQL
cfcb0 49 54 45 5f 4d 49 53 55 53 45 2e 20 20 53 51 4c  ITE_MISUSE.  SQL
cfcc0 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
cfcd0 65 64 20 69 66 20 65 76 65 72 79 74 68 69 6e 67  ed if everything
cfce0 20 77 6f 72 6b 73 2e 0a 2a 2f 0a 23 69 66 20 53   works..*/.#if S
cfcf0 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
cfd00 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69   && defined(__li
cfd10 6e 75 78 5f 5f 29 0a 73 74 61 74 69 63 20 69 6e  nux__).static in
cfd20 74 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73  t transferOwners
cfd30 68 69 70 28 75 6e 69 78 46 69 6c 65 20 2a 70 46  hip(unixFile *pF
cfd40 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  ile){.  int rc;.
cfd50 20 20 70 74 68 72 65 61 64 5f 74 20 68 53 65 6c    pthread_t hSel
cfd60 66 3b 0a 20 20 69 66 28 20 74 68 72 65 61 64 73  f;.  if( threads
cfd70 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65  OverrideEachOthe
cfd80 72 73 4c 6f 63 6b 73 20 29 7b 0a 20 20 20 20 2f  rsLocks ){.    /
cfd90 2a 20 4f 77 6e 65 72 73 68 69 70 20 74 72 61 6e  * Ownership tran
cfda0 73 66 65 72 73 20 6e 6f 74 20 6e 65 65 64 65 64  sfers not needed
cfdb0 20 6f 6e 20 74 68 69 73 20 73 79 73 74 65 6d 20   on this system 
cfdc0 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  */.    return SQ
cfdd0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68  LITE_OK;.  }.  h
cfde0 53 65 6c 66 20 3d 20 70 74 68 72 65 61 64 5f 73  Self = pthread_s
cfdf0 65 6c 66 28 29 3b 0a 20 20 69 66 28 20 70 74 68  elf();.  if( pth
cfe00 72 65 61 64 5f 65 71 75 61 6c 28 70 46 69 6c 65  read_equal(pFile
cfe10 2d 3e 74 69 64 2c 20 68 53 65 6c 66 29 20 29 7b  ->tid, hSelf) ){
cfe20 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 73  .    /* We are s
cfe30 74 69 6c 6c 20 69 6e 20 74 68 65 20 73 61 6d 65  till in the same
cfe40 20 74 68 72 65 61 64 20 2a 2f 0a 20 20 20 20 4f   thread */.    O
cfe50 53 54 52 41 43 45 31 28 22 4e 6f 2d 74 72 61 6e  STRACE1("No-tran
cfe60 73 66 65 72 2c 20 73 61 6d 65 20 74 68 72 65 61  sfer, same threa
cfe70 64 5c 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72  d\n");.    retur
cfe80 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
cfe90 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
cfea0 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20  cktype!=NO_LOCK 
cfeb0 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 63 61 6e  ){.    /* We can
cfec0 6e 6f 74 20 63 68 61 6e 67 65 20 6f 77 6e 65 72  not change owner
cfed0 73 68 69 70 20 77 68 69 6c 65 20 77 65 20 61 72  ship while we ar
cfee0 65 20 68 6f 6c 64 69 6e 67 20 61 20 6c 6f 63 6b  e holding a lock
cfef0 21 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  ! */.    return 
cff00 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
cff10 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54   }.  OSTRACE4("T
cff20 72 61 6e 73 66 65 72 20 6f 77 6e 65 72 73 68 69  ransfer ownershi
cff30 70 20 6f 66 20 25 64 20 66 72 6f 6d 20 25 64 20  p of %d from %d 
cff40 74 6f 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20  to %d\n",.      
cff50 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 2c 20        pFile->h, 
cff60 70 46 69 6c 65 2d 3e 74 69 64 2c 20 68 53 65 6c  pFile->tid, hSel
cff70 66 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 74 69 64  f);.  pFile->tid
cff80 20 3d 20 68 53 65 6c 66 3b 0a 20 20 69 66 20 28   = hSelf;.  if (
cff90 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 21 3d 20  pFile->pLock != 
cffa0 4e 55 4c 4c 29 20 7b 0a 20 20 20 20 72 65 6c 65  NULL) {.    rele
cffb0 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c  aseLockInfo(pFil
cffc0 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 72  e->pLock);.    r
cffd0 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f  c = findLockInfo
cffe0 28 70 46 69 6c 65 2c 20 26 70 46 69 6c 65 2d 3e  (pFile, &pFile->
cfff0 70 4c 6f 63 6b 2c 20 30 29 3b 0a 20 20 20 20 4f  pLock, 0);.    O
d0000 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 20 20  STRACE5("LOCK   
d0010 20 25 64 20 69 73 20 6e 6f 77 20 25 73 28 25 73   %d is now %s(%s
d0020 2c 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  ,%d)\n", pFile->
d0030 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f  h,.           lo
d0040 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65  cktypeName(pFile
d0050 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20  ->locktype),.   
d0060 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65          locktype
d0070 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 70 4c 6f 63  Name(pFile->pLoc
d0080 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 70 46  k->locktype), pF
d0090 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e 74 29  ile->pLock->cnt)
d00a0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  ;.    return rc;
d00b0 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
d00c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d00d0 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 20 20 2f  ;.  }.}.#else  /
d00e0 2a 20 69 66 20 6e 6f 74 20 53 51 4c 49 54 45 5f  * if not SQLITE_
d00f0 54 48 52 45 41 44 53 41 46 45 20 2a 2f 0a 20 20  THREADSAFE */.  
d0100 2f 2a 20 4f 6e 20 73 69 6e 67 6c 65 2d 74 68 72  /* On single-thr
d0110 65 61 64 65 64 20 62 75 69 6c 64 73 2c 20 6f 77  eaded builds, ow
d0120 6e 65 72 73 68 69 70 20 74 72 61 6e 73 66 65 72  nership transfer
d0130 20 69 73 20 61 20 6e 6f 2d 6f 70 20 2a 2f 0a 23   is a no-op */.#
d0140 20 64 65 66 69 6e 65 20 74 72 61 6e 73 66 65 72   define transfer
d0150 4f 77 6e 65 72 73 68 69 70 28 58 29 20 53 51 4c  Ownership(X) SQL
d0160 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 20 2f 2a  ITE_OK.#endif /*
d0170 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
d0180 46 45 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  FE */.../*.** Th
d0190 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b  is routine check
d01a0 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  s if there is a 
d01b0 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65  RESERVED lock he
d01c0 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66  ld on the specif
d01d0 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74  ied.** file by t
d01e0 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72  his or any other
d01f0 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63   process. If suc
d0200 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64  h a lock is held
d0210 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a  , set *pResOut.*
d0220 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20  * to a non-zero 
d0230 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20  value otherwise 
d0240 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20  *pResOut is set 
d0250 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65  to zero.  The re
d0260 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73  turn value.** is
d0270 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f   set to SQLITE_O
d0280 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20  K unless an I/O 
d0290 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72  error occurs dur
d02a0 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e  ing lock checkin
d02b0 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  g..*/.static int
d02c0 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76   unixCheckReserv
d02d0 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  edLock(sqlite3_f
d02e0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52  ile *id, int *pR
d02f0 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63  esOut){.  int rc
d0300 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
d0310 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 30  int reserved = 0
d0320 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  ;.  unixFile *pF
d0330 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
d0340 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65  )id;..  Simulate
d0350 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20  IOError( return 
d0360 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45  SQLITE_IOERR_CHE
d0370 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20  CKRESERVEDLOCK; 
d0380 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46  );..  assert( pF
d0390 69 6c 65 20 29 3b 0a 20 20 75 6e 69 78 45 6e 74  ile );.  unixEnt
d03a0 65 72 4d 75 74 65 78 28 29 3b 20 2f 2a 20 42 65  erMutex(); /* Be
d03b0 63 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f  cause pFile->pLo
d03c0 63 6b 20 69 73 20 73 68 61 72 65 64 20 61 63 72  ck is shared acr
d03d0 6f 73 73 20 74 68 72 65 61 64 73 20 2a 2f 0a 0a  oss threads */..
d03e0 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20    /* Check if a 
d03f0 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20 70  thread in this p
d0400 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63  rocess holds suc
d0410 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66  h a lock */.  if
d0420 28 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e  ( pFile->pLock->
d0430 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f  locktype>SHARED_
d0440 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65  LOCK ){.    rese
d0450 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20  rved = 1;.  }.. 
d0460 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65   /* Otherwise se
d0470 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20  e if some other 
d0480 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 74  process holds it
d0490 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 5f  ..  */.#ifndef _
d04a0 5f 44 4a 47 50 50 5f 5f 0a 20 20 69 66 28 20 21  _DJGPP__.  if( !
d04b0 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20  reserved ){.    
d04c0 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63  struct flock loc
d04d0 6b 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68  k;.    lock.l_wh
d04e0 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b  ence = SEEK_SET;
d04f0 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72  .    lock.l_star
d0500 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54  t = RESERVED_BYT
d0510 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65  E;.    lock.l_le
d0520 6e 20 3d 20 31 3b 0a 20 20 20 20 6c 6f 63 6b 2e  n = 1;.    lock.
d0530 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b  l_type = F_WRLCK
d0540 3b 0a 20 20 20 20 69 66 20 28 2d 31 20 3d 3d 20  ;.    if (-1 == 
d0550 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20  fcntl(pFile->h, 
d0560 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 29  F_GETLK, &lock))
d0570 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72   {.      int tEr
d0580 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  rno = errno;.   
d0590 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72     rc = sqliteEr
d05a0 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f  rorFromPosixErro
d05b0 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45  r(tErrno, SQLITE
d05c0 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45  _IOERR_CHECKRESE
d05d0 52 56 45 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20  RVEDLOCK);.     
d05e0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
d05f0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20  o = tErrno;.    
d0600 7d 20 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 2e  } else if( lock.
d0610 6c 5f 74 79 70 65 21 3d 46 5f 55 4e 4c 43 4b 20  l_type!=F_UNLCK 
d0620 29 7b 0a 20 20 20 20 20 20 72 65 73 65 72 76 65  ){.      reserve
d0630 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  d = 1;.    }.  }
d0640 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 75 6e 69  .#endif.  .  uni
d0650 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  xLeaveMutex();. 
d0660 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 54 20   OSTRACE4("TEST 
d0670 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64  WR-LOCK %d %d %d
d0680 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
d0690 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a 0a 20  c, reserved);.. 
d06a0 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65   *pResOut = rese
d06b0 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72  rved;.  return r
d06c0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66  c;.}../*.** Perf
d06d0 6f 72 6d 20 61 20 66 69 6c 65 20 6c 6f 63 6b 69  orm a file locki
d06e0 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 20  ng operation on 
d06f0 61 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73  a range of bytes
d0700 20 69 6e 20 61 20 66 69 6c 65 2e 0a 2a 2a 20 54   in a file..** T
d0710 68 65 20 22 6f 70 22 20 70 61 72 61 6d 65 74 65  he "op" paramete
d0720 72 20 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20  r should be one 
d0730 6f 66 20 46 5f 52 44 4c 43 4b 2c 20 46 5f 57 52  of F_RDLCK, F_WR
d0740 4c 43 4b 2c 20 6f 72 20 46 5f 55 4e 4c 43 4b 2e  LCK, or F_UNLCK.
d0750 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 6f 6e 20  .** Return 0 on 
d0760 73 75 63 63 65 73 73 20 6f 72 20 2d 31 20 66 6f  success or -1 fo
d0770 72 20 66 61 69 6c 75 72 65 2e 20 20 4f 6e 20 66  r failure.  On f
d0780 61 69 6c 75 72 65 2c 20 77 72 69 74 65 20 74 68  ailure, write th
d0790 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20  e error.** code 
d07a0 69 6e 74 6f 20 2a 70 45 72 72 63 6f 64 65 2e 0a  into *pErrcode..
d07b0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c  **.** If the SQL
d07c0 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c  ITE_WHOLE_FILE_L
d07d0 4f 43 4b 49 4e 47 20 62 69 74 20 69 73 20 63 6c  OCKING bit is cl
d07e0 65 61 72 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 6c  ear, then only l
d07f0 6f 63 6b 0a 2a 2a 20 74 68 65 20 72 61 6e 67 65  ock.** the range
d0800 20 6f 66 20 62 79 74 65 73 20 6f 6e 20 74 68 65   of bytes on the
d0810 20 6c 6f 63 6b 69 6e 67 20 70 61 67 65 20 62 65   locking page be
d0820 74 77 65 65 6e 20 53 48 41 52 45 44 5f 46 49 52  tween SHARED_FIR
d0830 53 54 20 61 6e 64 0a 2a 2a 20 53 48 41 52 45 44  ST and.** SHARED
d0840 5f 53 49 5a 45 2e 20 20 49 66 20 53 51 4c 49 54  _SIZE.  If SQLIT
d0850 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43  E_WHOLE_FILE_LOC
d0860 4b 49 4e 47 20 69 73 20 73 65 74 2c 20 74 68 65  KING is set, the
d0870 6e 20 6c 6f 63 6b 20 61 6c 6c 0a 2a 2a 20 62 79  n lock all.** by
d0880 74 65 73 20 66 72 6f 6d 20 30 20 75 70 20 74 6f  tes from 0 up to
d0890 20 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75 64 69   but not includi
d08a0 6e 67 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c  ng PENDING_BYTE,
d08b0 20 61 6e 64 20 61 6c 6c 20 62 79 74 65 73 0a 2a   and all bytes.*
d08c0 2a 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 53 48  * that follow SH
d08d0 41 52 45 44 5f 46 49 52 53 54 2e 0a 2a 2a 0a 2a  ARED_FIRST..**.*
d08e0 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  * In other words
d08f0 2c 20 6f 66 20 53 51 4c 49 54 45 5f 57 48 4f 4c  , of SQLITE_WHOL
d0900 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 69  E_FILE_LOCKING i
d0910 66 20 66 61 6c 73 65 20 28 74 68 65 20 68 69 73  f false (the his
d0920 74 6f 72 69 63 61 6c 0a 2a 2a 20 64 65 66 61 75  torical.** defau
d0930 6c 74 20 63 61 73 65 29 20 74 68 65 6e 20 6f 6e  lt case) then on
d0940 6c 79 20 6c 6f 63 6b 20 61 20 73 6d 61 6c 6c 20  ly lock a small 
d0950 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 20 66  range of bytes f
d0960 72 6f 6d 20 53 48 41 52 45 44 5f 46 49 52 53 54  rom SHARED_FIRST
d0970 0a 2a 2a 20 74 68 72 6f 75 67 68 20 53 48 41 52  .** through SHAR
d0980 45 44 5f 46 49 52 53 54 2b 53 48 41 52 45 44 5f  ED_FIRST+SHARED_
d0990 53 49 5a 45 2d 31 2e 20 20 42 75 74 20 69 66 20  SIZE-1.  But if 
d09a0 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c  SQLITE_WHOLE_FIL
d09b0 45 5f 4c 4f 43 4b 49 4e 47 20 69 73 0a 2a 2a 20  E_LOCKING is.** 
d09c0 74 72 75 65 20 74 68 65 6e 20 6c 6f 63 6b 20 65  true then lock e
d09d0 76 65 72 79 20 62 79 74 65 20 69 6e 20 74 68 65  very byte in the
d09e0 20 66 69 6c 65 20 65 78 63 65 70 74 20 66 6f 72   file except for
d09f0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 61 6e   PENDING_BYTE an
d0a00 64 0a 2a 2a 20 52 45 53 45 52 56 45 44 5f 42 59  d.** RESERVED_BY
d0a10 54 45 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45  TE..**.** SQLITE
d0a20 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b  _WHOLE_FILE_LOCK
d0a30 49 4e 47 3d 74 72 75 65 20 6f 76 65 72 6c 61 70  ING=true overlap
d0a40 73 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46  s SQLITE_WHOLE_F
d0a50 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3d 66 61 6c 73  ILE_LOCKING=fals
d0a60 65 0a 2a 2a 20 61 6e 64 20 73 6f 20 74 68 65 20  e.** and so the 
d0a70 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 65 73 20  locking schemes 
d0a80 61 72 65 20 63 6f 6d 70 61 74 69 62 6c 65 2e 20  are compatible. 
d0a90 20 4f 6e 65 20 74 79 70 65 20 6f 66 20 6c 6f 63   One type of loc
d0aa0 6b 20 77 69 6c 6c 0a 2a 2a 20 65 66 66 65 63 74  k will.** effect
d0ab0 69 76 65 6c 79 20 65 78 63 6c 75 64 65 20 74 68  ively exclude th
d0ac0 65 20 6f 74 68 65 72 20 74 79 70 65 2e 20 20 54  e other type.  T
d0ad0 68 65 20 72 65 61 73 6f 6e 20 66 6f 72 20 75 73  he reason for us
d0ae0 69 6e 67 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54  ing the.** SQLIT
d0af0 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43  E_WHOLE_FILE_LOC
d0b00 4b 49 4e 47 3d 74 72 75 65 20 69 73 20 74 68 61  KING=true is tha
d0b10 74 20 62 79 20 69 6e 64 69 63 61 74 69 6e 67 20  t by indicating 
d0b20 74 68 65 20 66 75 6c 6c 20 72 61 6e 67 65 0a 2a  the full range.*
d0b30 2a 20 6f 66 20 62 79 74 65 73 20 74 6f 20 62 65  * of bytes to be
d0b40 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e   read or written
d0b50 2c 20 77 65 20 67 69 76 65 20 68 69 6e 74 73 20  , we give hints 
d0b60 74 6f 20 4e 46 53 20 74 6f 20 68 65 6c 70 20 69  to NFS to help i
d0b70 74 0a 2a 2a 20 6d 61 69 6e 74 61 69 6e 20 63 61  t.** maintain ca
d0b80 63 68 65 20 63 6f 68 65 72 65 6e 63 79 2e 20 20  che coherency.  
d0b90 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e  On the other han
d0ba0 64 2c 20 77 68 6f 6c 65 20 66 69 6c 65 20 6c 6f  d, whole file lo
d0bb0 63 6b 69 6e 67 0a 2a 2a 20 69 73 20 73 6c 6f 77  cking.** is slow
d0bc0 65 72 2c 20 73 6f 20 77 65 20 64 6f 6e 27 74 20  er, so we don't 
d0bd0 77 61 6e 74 20 74 6f 20 75 73 65 20 69 74 20 65  want to use it e
d0be0 78 63 65 70 74 20 66 6f 72 20 4e 46 53 2e 0a 2a  xcept for NFS..*
d0bf0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 61 6e  /.static int ran
d0c00 67 65 4c 6f 63 6b 28 75 6e 69 78 46 69 6c 65 20  geLock(unixFile 
d0c10 2a 70 46 69 6c 65 2c 20 69 6e 74 20 6f 70 2c 20  *pFile, int op, 
d0c20 69 6e 74 20 2a 70 45 72 72 63 6f 64 65 29 7b 0a  int *pErrcode){.
d0c30 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c    struct flock l
d0c40 6f 63 6b 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ock;.  int rc;. 
d0c50 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 6f   lock.l_type = o
d0c60 70 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72  p;.  lock.l_star
d0c70 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54  t = SHARED_FIRST
d0c80 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63  ;.  lock.l_whenc
d0c90 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20  e = SEEK_SET;.  
d0ca0 69 66 28 20 28 70 46 69 6c 65 2d 3e 66 69 6c 65  if( (pFile->file
d0cb0 46 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 57  Flags & SQLITE_W
d0cc0 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e  HOLE_FILE_LOCKIN
d0cd0 47 29 3d 3d 30 20 29 7b 0a 20 20 20 20 6c 6f 63  G)==0 ){.    loc
d0ce0 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45 44  k.l_len = SHARED
d0cf0 5f 53 49 5a 45 3b 0a 20 20 20 20 72 63 20 3d 20  _SIZE;.    rc = 
d0d00 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20  fcntl(pFile->h, 
d0d10 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b  F_SETLK, &lock);
d0d20 0a 20 20 20 20 2a 70 45 72 72 63 6f 64 65 20 3d  .    *pErrcode =
d0d30 20 65 72 72 6e 6f 3b 0a 20 20 7d 65 6c 73 65 7b   errno;.  }else{
d0d40 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20  .    lock.l_len 
d0d50 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 66 63  = 0;.    rc = fc
d0d60 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f  ntl(pFile->h, F_
d0d70 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20  SETLK, &lock);. 
d0d80 20 20 20 2a 70 45 72 72 63 6f 64 65 20 3d 20 65     *pErrcode = e
d0d90 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 20 4e 45  rrno;.    if( NE
d0da0 56 45 52 28 6f 70 3d 3d 46 5f 55 4e 4c 43 4b 29  VER(op==F_UNLCK)
d0db0 20 7c 7c 20 72 63 21 3d 28 2d 31 29 20 29 7b 0a   || rc!=(-1) ){.
d0dc0 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61        lock.l_sta
d0dd0 72 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 6c 6f  rt = 0;.      lo
d0de0 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 50 45 4e 44 49  ck.l_len = PENDI
d0df0 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 20 20 72  NG_BYTE;.      r
d0e00 63 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d  c = fcntl(pFile-
d0e10 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f  >h, F_SETLK, &lo
d0e20 63 6b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 41  ck);.      if( A
d0e30 4c 57 41 59 53 28 6f 70 21 3d 46 5f 55 4e 4c 43  LWAYS(op!=F_UNLC
d0e40 4b 29 20 26 26 20 72 63 3d 3d 28 2d 31 29 20 29  K) && rc==(-1) )
d0e50 7b 0a 20 20 20 20 20 20 20 20 2a 70 45 72 72 63  {.        *pErrc
d0e60 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  ode = errno;.   
d0e70 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65       lock.l_type
d0e80 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20   = F_UNLCK;.    
d0e90 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74      lock.l_start
d0ea0 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b   = SHARED_FIRST;
d0eb0 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f  .        lock.l_
d0ec0 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  len = 0;.       
d0ed0 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c   fcntl(pFile->h,
d0ee0 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29   F_SETLK, &lock)
d0ef0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
d0f00 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
d0f10 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74  .}../*.** Lock t
d0f20 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65  he file with the
d0f30 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20   lock specified 
d0f40 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63  by parameter loc
d0f50 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f  ktype - one.** o
d0f60 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  f the following:
d0f70 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53  .**.**     (1) S
d0f80 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  HARED_LOCK.**   
d0f90 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c    (2) RESERVED_L
d0fa0 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50  OCK.**     (3) P
d0fb0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20  ENDING_LOCK.**  
d0fc0 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45     (4) EXCLUSIVE
d0fd0 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65  _LOCK.**.** Some
d0fe0 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65  times when reque
d0ff0 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73  sting one lock s
d1000 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c  tate, additional
d1010 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20   lock states.** 
d1020 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20  are inserted in 
d1030 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f  between.  The lo
d1040 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c  cking might fail
d1050 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c   on one of the l
d1060 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69  ater.** transiti
d1070 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20  ons leaving the 
d1080 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65  lock state diffe
d1090 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69  rent from what i
d10a0 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a  t started but.**
d10b0 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20   still short of 
d10c0 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66  its goal.  The f
d10d0 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73  ollowing chart s
d10e0 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64  hows the allowed
d10f0 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  .** transitions 
d1100 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64  and the inserted
d1110 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74   intermediate st
d1120 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55  ates:.**.**    U
d1130 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45  NLOCKED -> SHARE
d1140 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  D.**    SHARED -
d1150 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20  > RESERVED.**   
d1160 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44   SHARED -> (PEND
d1170 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56  ING) -> EXCLUSIV
d1180 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44  E.**    RESERVED
d1190 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
d11a0 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
d11b0 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c   PENDING -> EXCL
d11c0 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73  USIVE.**.** This
d11d0 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e   routine will on
d11e0 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f  ly increase a lo
d11f0 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c  ck.  Use the sql
d1200 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a  ite3OsUnlock().*
d1210 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77  * routine to low
d1220 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  er a locking lev
d1230 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  el..*/.static in
d1240 74 20 75 6e 69 78 4c 6f 63 6b 28 73 71 6c 69 74  t unixLock(sqlit
d1250 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
d1260 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 2f 2a   locktype){.  /*
d1270 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 64   The following d
d1280 65 73 63 72 69 62 65 73 20 74 68 65 20 69 6d 70  escribes the imp
d1290 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
d12a0 68 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73  he various locks
d12b0 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f 63 6b 20 74   and.  ** lock t
d12c0 72 61 6e 73 69 74 69 6f 6e 73 20 69 6e 20 74 65  ransitions in te
d12d0 72 6d 73 20 6f 66 20 74 68 65 20 50 4f 53 49 58  rms of the POSIX
d12e0 20 61 64 76 69 73 6f 72 79 20 73 68 61 72 65 64   advisory shared
d12f0 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 20   and exclusive. 
d1300 20 2a 2a 20 6c 6f 63 6b 20 70 72 69 6d 69 74 69   ** lock primiti
d1310 76 65 73 20 28 63 61 6c 6c 65 64 20 72 65 61 64  ves (called read
d1320 2d 6c 6f 63 6b 73 20 61 6e 64 20 77 72 69 74 65  -locks and write
d1330 2d 6c 6f 63 6b 73 20 62 65 6c 6f 77 2c 20 74 6f  -locks below, to
d1340 20 61 76 6f 69 64 0a 20 20 2a 2a 20 63 6f 6e 66   avoid.  ** conf
d1350 75 73 69 6f 6e 20 77 69 74 68 20 53 51 4c 69 74  usion with SQLit
d1360 65 20 6c 6f 63 6b 20 6e 61 6d 65 73 29 2e 20 54  e lock names). T
d1370 68 65 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72  he algorithms ar
d1380 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 0a 20 20  e complicated.  
d1390 2a 2a 20 73 6c 69 67 68 74 6c 79 20 69 6e 20 6f  ** slightly in o
d13a0 72 64 65 72 20 74 6f 20 62 65 20 63 6f 6d 70 61  rder to be compa
d13b0 74 69 62 6c 65 20 77 69 74 68 20 77 69 6e 64 6f  tible with windo
d13c0 77 73 20 73 79 73 74 65 6d 73 20 73 69 6d 75 6c  ws systems simul
d13d0 74 61 6e 65 6f 75 73 6c 79 0a 20 20 2a 2a 20 61  taneously.  ** a
d13e0 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d  ccessing the sam
d13f0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
d1400 20 69 6e 20 63 61 73 65 20 74 68 61 74 20 69 73   in case that is
d1410 20 65 76 65 72 20 72 65 71 75 69 72 65 64 2e 0a   ever required..
d1420 20 20 2a 2a 0a 20 20 2a 2a 20 53 79 6d 62 6f 6c    **.  ** Symbol
d1430 73 20 64 65 66 69 6e 65 64 20 69 6e 20 6f 73 2e  s defined in os.
d1440 68 20 69 6e 64 65 6e 74 69 66 79 20 74 68 65 20  h indentify the 
d1450 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27 20 61  'pending byte' a
d1460 6e 64 20 74 68 65 20 27 72 65 73 65 72 76 65 64  nd the 'reserved
d1470 0a 20 20 2a 2a 20 62 79 74 65 27 2c 20 65 61 63  .  ** byte', eac
d1480 68 20 73 69 6e 67 6c 65 20 62 79 74 65 73 20 61  h single bytes a
d1490 74 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66  t well known off
d14a0 73 65 74 73 2c 20 61 6e 64 20 74 68 65 20 27 73  sets, and the 's
d14b0 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20  hared byte.  ** 
d14c0 72 61 6e 67 65 27 2c 20 61 20 72 61 6e 67 65 20  range', a range 
d14d0 6f 66 20 35 31 30 20 62 79 74 65 73 20 61 74 20  of 510 bytes at 
d14e0 61 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66  a well known off
d14f0 73 65 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  set..  **.  ** T
d1500 6f 20 6f 62 74 61 69 6e 20 61 20 53 48 41 52 45  o obtain a SHARE
d1510 44 20 6c 6f 63 6b 2c 20 61 20 72 65 61 64 2d 6c  D lock, a read-l
d1520 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20  ock is obtained 
d1530 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 0a  on the 'pending.
d1540 20 20 2a 2a 20 62 79 74 65 27 2e 20 20 49 66 20    ** byte'.  If 
d1550 74 68 69 73 20 69 73 20 73 75 63 63 65 73 73 66  this is successf
d1560 75 6c 2c 20 61 20 72 61 6e 64 6f 6d 20 62 79 74  ul, a random byt
d1570 65 20 66 72 6f 6d 20 74 68 65 20 27 73 68 61 72  e from the 'shar
d1580 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e  ed byte.  ** ran
d1590 67 65 27 20 69 73 20 72 65 61 64 2d 6c 6f 63 6b  ge' is read-lock
d15a0 65 64 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20  ed and the lock 
d15b0 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20  on the 'pending 
d15c0 62 79 74 65 27 20 72 65 6c 65 61 73 65 64 2e 0a  byte' released..
d15d0 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f 63    **.  ** A proc
d15e0 65 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74  ess may only obt
d15f0 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c  ain a RESERVED l
d1600 6f 63 6b 20 61 66 74 65 72 20 69 74 20 68 61 73  ock after it has
d1610 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 0a   a SHARED lock..
d1620 20 20 2a 2a 20 41 20 52 45 53 45 52 56 45 44 20    ** A RESERVED 
d1630 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65 6e  lock is implemen
d1640 74 65 64 20 62 79 20 67 72 61 62 62 69 6e 67 20  ted by grabbing 
d1650 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20  a write-lock on 
d1660 74 68 65 0a 20 20 2a 2a 20 27 72 65 73 65 72 76  the.  ** 'reserv
d1670 65 64 20 62 79 74 65 27 2e 20 0a 20 20 2a 2a 0a  ed byte'. .  **.
d1680 20 20 2a 2a 20 41 20 70 72 6f 63 65 73 73 20 6d    ** A process m
d1690 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20 61  ay only obtain a
d16a0 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 61 66   PENDING lock af
d16b0 74 65 72 20 69 74 20 68 61 73 20 6f 62 74 61 69  ter it has obtai
d16c0 6e 65 64 20 61 0a 20 20 2a 2a 20 53 48 41 52 45  ned a.  ** SHARE
d16d0 44 20 6c 6f 63 6b 2e 20 41 20 50 45 4e 44 49 4e  D lock. A PENDIN
d16e0 47 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d  G lock is implem
d16f0 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e 69  ented by obtaini
d1700 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 0a  ng a write-lock.
d1710 20 20 2a 2a 20 6f 6e 20 74 68 65 20 27 70 65 6e    ** on the 'pen
d1720 64 69 6e 67 20 62 79 74 65 27 2e 20 54 68 69 73  ding byte'. This
d1730 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 6e 6f   ensures that no
d1740 20 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63 6b   new SHARED lock
d1750 73 20 63 61 6e 20 62 65 0a 20 20 2a 2a 20 6f 62  s can be.  ** ob
d1760 74 61 69 6e 65 64 2c 20 62 75 74 20 65 78 69 73  tained, but exis
d1770 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 6b  ting SHARED lock
d1780 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f  s are allowed to
d1790 20 70 65 72 73 69 73 74 2e 20 41 20 70 72 6f 63   persist. A proc
d17a0 65 73 73 0a 20 20 2a 2a 20 64 6f 65 73 20 6e 6f  ess.  ** does no
d17b0 74 20 68 61 76 65 20 74 6f 20 6f 62 74 61 69 6e  t have to obtain
d17c0 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   a RESERVED lock
d17d0 20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20 61   on the way to a
d17e0 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 0a 20   PENDING lock.. 
d17f0 20 2a 2a 20 54 68 69 73 20 70 72 6f 70 65 72 74   ** This propert
d1800 79 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65  y is used by the
d1810 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 72   algorithm for r
d1820 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a 6f  olling back a jo
d1830 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a 2a 20  urnal file.  ** 
d1840 61 66 74 65 72 20 61 20 63 72 61 73 68 2e 0a 20  after a crash.. 
d1850 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 45 58 43 4c   **.  ** An EXCL
d1860 55 53 49 56 45 20 6c 6f 63 6b 2c 20 6f 62 74 61  USIVE lock, obta
d1870 69 6e 65 64 20 61 66 74 65 72 20 61 20 50 45 4e  ined after a PEN
d1880 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 68 65 6c  DING lock is hel
d1890 64 2c 20 69 73 0a 20 20 2a 2a 20 69 6d 70 6c 65  d, is.  ** imple
d18a0 6d 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e  mented by obtain
d18b0 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b  ing a write-lock
d18c0 20 6f 6e 20 74 68 65 20 65 6e 74 69 72 65 20 27   on the entire '
d18d0 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a  shared byte.  **
d18e0 20 72 61 6e 67 65 27 2e 20 53 69 6e 63 65 20 61   range'. Since a
d18f0 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 72  ll other locks r
d1900 65 71 75 69 72 65 20 61 20 72 65 61 64 2d 6c 6f  equire a read-lo
d1910 63 6b 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65  ck on one of the
d1920 20 62 79 74 65 73 0a 20 20 2a 2a 20 77 69 74 68   bytes.  ** with
d1930 69 6e 20 74 68 69 73 20 72 61 6e 67 65 2c 20 74  in this range, t
d1940 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74  his ensures that
d1950 20 6e 6f 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20   no other locks 
d1960 61 72 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 0a  are held on the.
d1970 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 0a    ** database. .
d1980 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 72 65    **.  ** The re
d1990 61 73 6f 6e 20 61 20 73 69 6e 67 6c 65 20 62 79  ason a single by
d19a0 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65  te cannot be use
d19b0 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65  d instead of the
d19c0 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20   'shared byte.  
d19d0 2a 2a 20 72 61 6e 67 65 27 20 69 73 20 74 68 61  ** range' is tha
d19e0 74 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20  t some versions 
d19f0 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 20 6e 6f  of windows do no
d1a00 74 20 73 75 70 70 6f 72 74 20 72 65 61 64 2d 6c  t support read-l
d1a10 6f 63 6b 73 2e 20 42 79 0a 20 20 2a 2a 20 6c 6f  ocks. By.  ** lo
d1a20 63 6b 69 6e 67 20 61 20 72 61 6e 64 6f 6d 20 62  cking a random b
d1a30 79 74 65 20 66 72 6f 6d 20 61 20 72 61 6e 67 65  yte from a range
d1a40 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20 53 48 41  , concurrent SHA
d1a50 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 65 78  RED locks may ex
d1a60 69 73 74 0a 20 20 2a 2a 20 65 76 65 6e 20 69 66  ist.  ** even if
d1a70 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 70 72 69   the locking pri
d1a80 6d 69 74 69 76 65 20 75 73 65 64 20 69 73 20 61  mitive used is a
d1a90 6c 77 61 79 73 20 61 20 77 72 69 74 65 2d 6c 6f  lways a write-lo
d1aa0 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 72  ck..  */.  int r
d1ab0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
d1ac0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
d1ad0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
d1ae0 3b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c  ;.  struct unixL
d1af0 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 20 3d  ockInfo *pLock =
d1b00 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20   pFile->pLock;. 
d1b10 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f   struct flock lo
d1b20 63 6b 3b 0a 20 20 69 6e 74 20 73 20 3d 20 30 3b  ck;.  int s = 0;
d1b30 0a 20 20 69 6e 74 20 74 45 72 72 6e 6f 3b 0a 0a  .  int tErrno;..
d1b40 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
d1b50 29 3b 0a 20 20 4f 53 54 52 41 43 45 37 28 22 4c  );.  OSTRACE7("L
d1b60 4f 43 4b 20 20 20 20 25 64 20 25 73 20 77 61 73  OCK    %d %s was
d1b70 20 25 73 28 25 73 2c 25 64 29 20 70 69 64 3d 25   %s(%s,%d) pid=%
d1b80 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a  d\n", pFile->h,.
d1b90 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61        locktypeNa
d1ba0 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 6c 6f  me(locktype), lo
d1bb0 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65  cktypeName(pFile
d1bc0 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20  ->locktype),.   
d1bd0 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28     locktypeName(
d1be0 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29  pLock->locktype)
d1bf0 2c 20 70 4c 6f 63 6b 2d 3e 63 6e 74 20 2c 20 67  , pLock->cnt , g
d1c00 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 2f 2a 20  etpid());..  /* 
d1c10 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 65  If there is alre
d1c20 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68  ady a lock of th
d1c30 69 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20  is type or more 
d1c40 72 65 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74  restrictive on t
d1c50 68 65 0a 20 20 2a 2a 20 75 6e 69 78 46 69 6c 65  he.  ** unixFile
d1c60 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f  , do nothing. Do
d1c70 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e 64 5f  n't use the end_
d1c80 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c  lock: exit path,
d1c90 20 61 73 0a 20 20 2a 2a 20 75 6e 69 78 45 6e 74   as.  ** unixEnt
d1ca0 65 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74  erMutex() hasn't
d1cb0 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74   been called yet
d1cc0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69  ..  */.  if( pFi
d1cd0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f  le->locktype>=lo
d1ce0 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53  cktype ){.    OS
d1cf0 54 52 41 43 45 33 28 22 4c 4f 43 4b 20 20 20 20  TRACE3("LOCK    
d1d00 25 64 20 25 73 20 6f 6b 20 28 61 6c 72 65 61 64  %d %s ok (alread
d1d10 79 20 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c  y held)\n", pFil
d1d20 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20  e->h,.          
d1d30 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c    locktypeName(l
d1d40 6f 63 6b 74 79 70 65 29 29 3b 0a 20 20 20 20 72  ocktype));.    r
d1d50 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
d1d60 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20  .  }..  /* Make 
d1d70 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67  sure the locking
d1d80 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72   sequence is cor
d1d90 72 65 63 74 2e 0a 20 20 2a 2a 20 20 28 31 29 20  rect..  **  (1) 
d1da0 57 65 20 6e 65 76 65 72 20 6d 6f 76 65 20 66 72  We never move fr
d1db0 6f 6d 20 75 6e 6c 6f 63 6b 65 64 20 74 6f 20 61  om unlocked to a
d1dc0 6e 79 74 68 69 6e 67 20 68 69 67 68 65 72 20 74  nything higher t
d1dd0 68 61 6e 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e  han shared lock.
d1de0 0a 20 20 2a 2a 20 20 28 32 29 20 53 51 4c 69 74  .  **  (2) SQLit
d1df0 65 20 6e 65 76 65 72 20 65 78 70 6c 69 63 69 74  e never explicit
d1e00 6c 79 20 72 65 71 75 65 73 74 73 20 61 20 70 65  ly requests a pe
d1e10 6e 64 69 67 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20  ndig lock..  ** 
d1e20 20 28 33 29 20 41 20 73 68 61 72 65 64 20 6c 6f   (3) A shared lo
d1e30 63 6b 20 69 73 20 61 6c 77 61 79 73 20 68 65 6c  ck is always hel
d1e40 64 20 77 68 65 6e 20 61 20 72 65 73 65 72 76 65  d when a reserve
d1e50 20 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74   lock is request
d1e60 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ed..  */.  asser
d1e70 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  t( pFile->lockty
d1e80 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c  pe!=NO_LOCK || l
d1e90 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
d1ea0 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  LOCK );.  assert
d1eb0 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44  ( locktype!=PEND
d1ec0 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73  ING_LOCK );.  as
d1ed0 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d  sert( locktype!=
d1ee0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c  RESERVED_LOCK ||
d1ef0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d1f00 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b  ==SHARED_LOCK );
d1f10 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65  ..  /* This mute
d1f20 78 20 69 73 20 6e 65 65 64 65 64 20 62 65 63 61  x is needed beca
d1f30 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b  use pFile->pLock
d1f40 20 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73   is shared acros
d1f50 73 20 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a 20  s threads.  */. 
d1f60 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28   unixEnterMutex(
d1f70 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75  );..  /* Make su
d1f80 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  re the current t
d1f90 68 72 65 61 64 20 6f 77 6e 73 20 74 68 65 20 70  hread owns the p
d1fa0 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20  File..  */.  rc 
d1fb0 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73  = transferOwners
d1fc0 68 69 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 66  hip(pFile);.  if
d1fd0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
d1fe0 29 7b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65  ){.    unixLeave
d1ff0 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 74  Mutex();.    ret
d2000 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 4c  urn rc;.  }.  pL
d2010 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f  ock = pFile->pLo
d2020 63 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d  ck;..  /* If som
d2030 65 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74  e thread using t
d2040 68 69 73 20 50 49 44 20 68 61 73 20 61 20 6c 6f  his PID has a lo
d2050 63 6b 20 76 69 61 20 61 20 64 69 66 66 65 72 65  ck via a differe
d2060 6e 74 20 75 6e 69 78 46 69 6c 65 2a 0a 20 20 2a  nt unixFile*.  *
d2070 2a 20 68 61 6e 64 6c 65 20 74 68 61 74 20 70 72  * handle that pr
d2080 65 63 6c 75 64 65 73 20 74 68 65 20 72 65 71 75  ecludes the requ
d2090 65 73 74 65 64 20 6c 6f 63 6b 2c 20 72 65 74 75  ested lock, retu
d20a0 72 6e 20 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20  rn BUSY..  */.  
d20b0 69 66 28 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b  if( (pFile->lock
d20c0 74 79 70 65 21 3d 70 4c 6f 63 6b 2d 3e 6c 6f 63  type!=pLock->loc
d20d0 6b 74 79 70 65 20 26 26 20 0a 20 20 20 20 20 20  ktype && .      
d20e0 20 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b      (pLock->lock
d20f0 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f  type>=PENDING_LO
d2100 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3e 53  CK || locktype>S
d2110 48 41 52 45 44 5f 4c 4f 43 4b 29 29 0a 20 20 29  HARED_LOCK)).  )
d2120 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
d2130 45 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f 74 6f  E_BUSY;.    goto
d2140 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a   end_lock;.  }..
d2150 20 20 2f 2a 20 49 66 20 61 20 53 48 41 52 45 44    /* If a SHARED
d2160 20 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74   lock is request
d2170 65 64 2c 20 61 6e 64 20 73 6f 6d 65 20 74 68 72  ed, and some thr
d2180 65 61 64 20 75 73 69 6e 67 20 74 68 69 73 20 50  ead using this P
d2190 49 44 20 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20  ID already.  ** 
d21a0 68 61 73 20 61 20 53 48 41 52 45 44 20 6f 72 20  has a SHARED or 
d21b0 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20 74  RESERVED lock, t
d21c0 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 72 65  hen increment re
d21d0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 73 20 61  ference counts a
d21e0 6e 64 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 53  nd.  ** return S
d21f0 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2f 0a 20  QLITE_OK..  */. 
d2200 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53   if( locktype==S
d2210 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 0a 20  HARED_LOCK && . 
d2220 20 20 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63       (pLock->loc
d2230 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
d2240 43 4b 20 7c 7c 20 70 4c 6f 63 6b 2d 3e 6c 6f 63  CK || pLock->loc
d2250 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f  ktype==RESERVED_
d2260 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 20 61 73 73  LOCK) ){.    ass
d2270 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53  ert( locktype==S
d2280 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
d2290 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d    assert( pFile-
d22a0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a  >locktype==0 );.
d22b0 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63      assert( pLoc
d22c0 6b 2d 3e 63 6e 74 3e 30 20 29 3b 0a 20 20 20 20  k->cnt>0 );.    
d22d0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
d22e0 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20  = SHARED_LOCK;. 
d22f0 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2b 2b 3b     pLock->cnt++;
d2300 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65  .    pFile->pOpe
d2310 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20  n->nLock++;.    
d2320 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20  goto end_lock;. 
d2330 20 7d 0a 0a 0a 20 20 2f 2a 20 41 20 50 45 4e 44   }...  /* A PEND
d2340 49 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64  ING lock is need
d2350 65 64 20 62 65 66 6f 72 65 20 61 63 71 75 69 72  ed before acquir
d2360 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63  ing a SHARED loc
d2370 6b 20 61 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a  k and before.  *
d2380 2a 20 61 63 71 75 69 72 69 6e 67 20 61 6e 20 45  * acquiring an E
d2390 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20  XCLUSIVE lock.  
d23a0 46 6f 72 20 74 68 65 20 53 48 41 52 45 44 20 6c  For the SHARED l
d23b0 6f 63 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e 47  ock, the PENDING
d23c0 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65   will.  ** be re
d23d0 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 6c  leased..  */.  l
d23e0 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c 3b 0a  ock.l_len = 1L;.
d23f0 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20    lock.l_whence 
d2400 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 69 66  = SEEK_SET;.  if
d2410 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ( locktype==SHAR
d2420 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c  ED_LOCK .      |
d2430 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43  | (locktype==EXC
d2440 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70  LUSIVE_LOCK && p
d2450 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50  File->locktype<P
d2460 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29  ENDING_LOCK).  )
d2470 7b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70  {.    lock.l_typ
d2480 65 20 3d 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53  e = (locktype==S
d2490 48 41 52 45 44 5f 4c 4f 43 4b 3f 46 5f 52 44 4c  HARED_LOCK?F_RDL
d24a0 43 4b 3a 46 5f 57 52 4c 43 4b 29 3b 0a 20 20 20  CK:F_WRLCK);.   
d24b0 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20   lock.l_start = 
d24c0 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20  PENDING_BYTE;.  
d24d0 20 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c    s = fcntl(pFil
d24e0 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26  e->h, F_SETLK, &
d24f0 6c 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 73  lock);.    if( s
d2500 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  ==(-1) ){.      
d2510 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
d2520 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
d2530 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45  eErrorFromPosixE
d2540 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c  rror(tErrno, SQL
d2550 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b  ITE_IOERR_LOCK);
d2560 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f  .      if( IS_LO
d2570 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a  CK_ERROR(rc) ){.
d2580 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c          pFile->l
d2590 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e  astErrno = tErrn
d25a0 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  o;.      }.     
d25b0 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a   goto end_lock;.
d25c0 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a 20 20 2f 2a      }.  }...  /*
d25d0 20 49 66 20 63 6f 6e 74 72 6f 6c 20 67 65 74 73   If control gets
d25e0 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c 20   to this point, 
d25f0 74 68 65 6e 20 61 63 74 75 61 6c 6c 79 20 67 6f  then actually go
d2600 20 61 68 65 61 64 20 61 6e 64 20 6d 61 6b 65 0a   ahead and make.
d2610 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 73    ** operating s
d2620 79 73 74 65 6d 20 63 61 6c 6c 73 20 66 6f 72 20  ystem calls for 
d2630 74 68 65 20 73 70 65 63 69 66 69 65 64 20 6c 6f  the specified lo
d2640 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c  ck..  */.  if( l
d2650 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
d2660 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 61 73 73 65  LOCK ){.    asse
d2670 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d  rt( pLock->cnt==
d2680 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
d2690 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65   pLock->locktype
d26a0 3d 3d 30 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 4e  ==0 );..    /* N
d26b0 6f 77 20 67 65 74 20 74 68 65 20 72 65 61 64 2d  ow get the read-
d26c0 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 73 20 3d 20  lock */.    s = 
d26d0 72 61 6e 67 65 4c 6f 63 6b 28 70 46 69 6c 65 2c  rangeLock(pFile,
d26e0 20 46 5f 52 44 4c 43 4b 2c 20 26 74 45 72 72 6e   F_RDLCK, &tErrn
d26f0 6f 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70  o);..    /* Drop
d2700 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 50   the temporary P
d2710 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20  ENDING lock */. 
d2720 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20     lock.l_start 
d2730 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a  = PENDING_BYTE;.
d2740 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d      lock.l_len =
d2750 20 31 4c 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f   1L;.    lock.l_
d2760 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a  type = F_UNLCK;.
d2770 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 70 46      if( fcntl(pF
d2780 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c  ile->h, F_SETLK,
d2790 20 26 6c 6f 63 6b 29 21 3d 30 20 29 7b 0a 20 20   &lock)!=0 ){.  
d27a0 20 20 20 20 69 66 28 20 73 20 21 3d 20 2d 31 20      if( s != -1 
d27b0 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
d27c0 69 73 20 63 6f 75 6c 64 20 68 61 70 70 65 6e 20  is could happen 
d27d0 77 69 74 68 20 61 20 6e 65 74 77 6f 72 6b 20 6d  with a network m
d27e0 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20  ount */.        
d27f0 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20  tErrno = errno; 
d2800 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
d2810 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
d2820 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
d2830 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c  SQLITE_IOERR_UNL
d2840 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20 20 20 69  OCK); .        i
d2850 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52  f( IS_LOCK_ERROR
d2860 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20  (rc) ){.        
d2870 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
d2880 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
d2890 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 67       }.        g
d28a0 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20  oto end_lock;.  
d28b0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
d28c0 69 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20  if( s==(-1) ){. 
d28d0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
d28e0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
d28f0 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49  ror(tErrno, SQLI
d2900 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a  TE_IOERR_LOCK);.
d2910 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43        if( IS_LOC
d2920 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20  K_ERROR(rc) ){. 
d2930 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61         pFile->la
d2940 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
d2950 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
d2960 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 69 6c 65  lse{.      pFile
d2970 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41  ->locktype = SHA
d2980 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20  RED_LOCK;.      
d2990 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c  pFile->pOpen->nL
d29a0 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 70 4c 6f  ock++;.      pLo
d29b0 63 6b 2d 3e 63 6e 74 20 3d 20 31 3b 0a 20 20 20  ck->cnt = 1;.   
d29c0 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c   }.  }else if( l
d29d0 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49  ocktype==EXCLUSI
d29e0 56 45 5f 4c 4f 43 4b 20 26 26 20 70 4c 6f 63 6b  VE_LOCK && pLock
d29f0 2d 3e 63 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f  ->cnt>1 ){.    /
d2a00 2a 20 57 65 20 61 72 65 20 74 72 79 69 6e 67 20  * We are trying 
d2a10 66 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65  for an exclusive
d2a20 20 6c 6f 63 6b 20 62 75 74 20 61 6e 6f 74 68 65   lock but anothe
d2a30 72 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73  r thread in this
d2a40 0a 20 20 20 20 2a 2a 20 73 61 6d 65 20 70 72 6f  .    ** same pro
d2a50 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20 68 6f  cess is still ho
d2a60 6c 64 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c  lding a shared l
d2a70 6f 63 6b 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d  ock. */.    rc =
d2a80 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
d2a90 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68  }else{.    /* Th
d2aa0 65 20 72 65 71 75 65 73 74 20 77 61 73 20 66 6f  e request was fo
d2ab0 72 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20  r a RESERVED or 
d2ac0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20  EXCLUSIVE lock. 
d2ad0 20 49 74 20 69 73 0a 20 20 20 20 2a 2a 20 61 73   It is.    ** as
d2ae0 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 72 65  sumed that there
d2af0 20 69 73 20 61 20 53 48 41 52 45 44 20 6f 72 20   is a SHARED or 
d2b00 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20  greater lock on 
d2b10 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20  the file.    ** 
d2b20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a  already..    */.
d2b30 20 20 20 20 61 73 73 65 72 74 28 20 30 21 3d 70      assert( 0!=p
d2b40 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29  File->locktype )
d2b50 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70  ;.    lock.l_typ
d2b60 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20  e = F_WRLCK;.   
d2b70 20 73 77 69 74 63 68 28 20 6c 6f 63 6b 74 79 70   switch( locktyp
d2b80 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20  e ){.      case 
d2b90 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 0a 20  RESERVED_LOCK:. 
d2ba0 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74         lock.l_st
d2bb0 61 72 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42  art = RESERVED_B
d2bc0 59 54 45 3b 0a 20 20 20 20 20 20 20 20 73 20 3d  YTE;.        s =
d2bd0 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c   fcntl(pFile->h,
d2be0 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29   F_SETLK, &lock)
d2bf0 3b 0a 20 20 20 20 20 20 20 20 74 45 72 72 6e 6f  ;.        tErrno
d2c00 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20   = errno;.      
d2c10 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
d2c20 61 73 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f  ase EXCLUSIVE_LO
d2c30 43 4b 3a 0a 20 20 20 20 20 20 20 20 73 20 3d 20  CK:.        s = 
d2c40 72 61 6e 67 65 4c 6f 63 6b 28 70 46 69 6c 65 2c  rangeLock(pFile,
d2c50 20 46 5f 57 52 4c 43 4b 2c 20 26 74 45 72 72 6e   F_WRLCK, &tErrn
d2c60 6f 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  o);.        brea
d2c70 6b 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74  k;.      default
d2c80 3a 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  :.        assert
d2c90 28 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  (0);.    }.    i
d2ca0 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20  f( s==(-1) ){.  
d2cb0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45      rc = sqliteE
d2cc0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72  rrorFromPosixErr
d2cd0 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54  or(tErrno, SQLIT
d2ce0 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20  E_IOERR_LOCK);. 
d2cf0 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b       if( IS_LOCK
d2d00 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20  _ERROR(rc) ){.  
d2d10 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
d2d20 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b  tErrno = tErrno;
d2d30 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
d2d40 20 7d 0a 20 20 0a 0a 23 69 66 6e 64 65 66 20 4e   }.  ..#ifndef N
d2d50 44 45 42 55 47 0a 20 20 2f 2a 20 53 65 74 20 75  DEBUG.  /* Set u
d2d60 70 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  p the transactio
d2d70 6e 2d 63 6f 75 6e 74 65 72 20 63 68 61 6e 67 65  n-counter change
d2d80 20 63 68 65 63 6b 69 6e 67 20 66 6c 61 67 73 20   checking flags 
d2d90 77 68 65 6e 0a 20 20 2a 2a 20 74 72 61 6e 73 69  when.  ** transi
d2da0 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 61 20 53  tioning from a S
d2db0 48 41 52 45 44 20 74 6f 20 61 20 52 45 53 45 52  HARED to a RESER
d2dc0 56 45 44 20 6c 6f 63 6b 2e 20 20 54 68 65 20 63  VED lock.  The c
d2dd0 68 61 6e 67 65 0a 20 20 2a 2a 20 66 72 6f 6d 20  hange.  ** from 
d2de0 53 48 41 52 45 44 20 74 6f 20 52 45 53 45 52 56  SHARED to RESERV
d2df0 45 44 20 6d 61 72 6b 73 20 74 68 65 20 62 65 67  ED marks the beg
d2e00 69 6e 6e 69 6e 67 20 6f 66 20 61 20 6e 6f 72 6d  inning of a norm
d2e10 61 6c 0a 20 20 2a 2a 20 77 72 69 74 65 20 6f 70  al.  ** write op
d2e20 65 72 61 74 69 6f 6e 20 28 6e 6f 74 20 61 20 68  eration (not a h
d2e30 6f 74 20 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62  ot journal rollb
d2e40 61 63 6b 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ack)..  */.  if(
d2e50 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20   rc==SQLITE_OK. 
d2e60 20 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b    && pFile->lock
d2e70 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43  type<=SHARED_LOC
d2e80 4b 0a 20 20 20 26 26 20 6c 6f 63 6b 74 79 70 65  K.   && locktype
d2e90 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a  ==RESERVED_LOCK.
d2ea0 20 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e    ){.    pFile->
d2eb0 74 72 61 6e 73 43 6e 74 72 43 68 6e 67 20 3d 20  transCntrChng = 
d2ec0 30 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64 62  0;.    pFile->db
d2ed0 55 70 64 61 74 65 20 3d 20 30 3b 0a 20 20 20 20  Update = 0;.    
d2ee0 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57  pFile->inNormalW
d2ef0 72 69 74 65 20 3d 20 31 3b 0a 20 20 7d 0a 23 65  rite = 1;.  }.#e
d2f00 6e 64 69 66 0a 0a 0a 20 20 69 66 28 20 72 63 3d  ndif...  if( rc=
d2f10 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
d2f20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
d2f30 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  e = locktype;.  
d2f40 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70    pLock->locktyp
d2f50 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  e = locktype;.  
d2f60 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79  }else if( lockty
d2f70 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f  pe==EXCLUSIVE_LO
d2f80 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d  CK ){.    pFile-
d2f90 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44  >locktype = PEND
d2fa0 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 70 4c  ING_LOCK;.    pL
d2fb0 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ock->locktype = 
d2fc0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20  PENDING_LOCK;.  
d2fd0 7d 0a 0a 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75  }..end_lock:.  u
d2fe0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  nixLeaveMutex();
d2ff0 0a 20 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43  .  OSTRACE4("LOC
d3000 4b 20 20 20 20 25 64 20 25 73 20 25 73 5c 6e 22  K    %d %s %s\n"
d3010 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b  , pFile->h, lock
d3020 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70  typeName(locktyp
d3030 65 29 2c 20 0a 20 20 20 20 20 20 72 63 3d 3d 53  e), .      rc==S
d3040 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20  QLITE_OK ? "ok" 
d3050 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 72  : "failed");.  r
d3060 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
d3070 2a 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 66 69 6c  ** Close all fil
d3080 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 61 63  e descriptors ac
d3090 63 75 6d 75 61 74 65 64 20 69 6e 20 74 68 65 20  cumuated in the 
d30a0 75 6e 69 78 4f 70 65 6e 43 6e 74 2d 3e 70 55 6e  unixOpenCnt->pUn
d30b0 75 73 65 64 20 6c 69 73 74 2e 0a 2a 2a 20 49 66  used list..** If
d30c0 20 61 6c 6c 20 73 75 63 68 20 66 69 6c 65 20 64   all such file d
d30d0 65 73 63 72 69 70 74 6f 72 73 20 61 72 65 20 63  escriptors are c
d30e0 6c 6f 73 65 64 20 77 69 74 68 6f 75 74 20 65 72  losed without er
d30f0 72 6f 72 2c 20 74 68 65 20 6c 69 73 74 20 69 73  ror, the list is
d3100 0a 2a 2a 20 63 6c 65 61 72 65 64 20 61 6e 64 20  .** cleared and 
d3110 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e  SQLITE_OK return
d3120 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77  ed..**.** Otherw
d3130 69 73 65 2c 20 69 66 20 61 6e 20 65 72 72 6f 72  ise, if an error
d3140 20 6f 63 63 75 72 73 2c 20 74 68 65 6e 20 73 75   occurs, then su
d3150 63 63 65 73 73 66 75 6c 6c 79 20 63 6c 6f 73 65  ccessfully close
d3160 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  d file descripto
d3170 72 0a 2a 2a 20 65 6e 74 72 69 65 73 20 61 72 65  r.** entries are
d3180 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68   removed from th
d3190 65 20 6c 69 73 74 2c 20 61 6e 64 20 53 51 4c 49  e list, and SQLI
d31a0 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 20 72  TE_IOERR_CLOSE r
d31b0 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 20 6e 6f 74  eturned. .** not
d31c0 20 64 65 6c 65 74 65 64 20 61 6e 64 20 53 51 4c   deleted and SQL
d31d0 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 20  ITE_IOERR_CLOSE 
d31e0 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 20 0a 73 74  returned..*/ .st
d31f0 61 74 69 63 20 69 6e 74 20 63 6c 6f 73 65 50 65  atic int closePe
d3200 6e 64 69 6e 67 46 64 73 28 75 6e 69 78 46 69 6c  ndingFds(unixFil
d3210 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74  e *pFile){.  int
d3220 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
d3230 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70  .  struct unixOp
d3240 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70  enCnt *pOpen = p
d3250 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 55  File->pOpen;.  U
d3260 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 45 72  nixUnusedFd *pEr
d3270 72 6f 72 20 3d 20 30 3b 0a 20 20 55 6e 69 78 55  ror = 0;.  UnixU
d3280 6e 75 73 65 64 46 64 20 2a 70 3b 0a 20 20 55 6e  nusedFd *p;.  Un
d3290 69 78 55 6e 75 73 65 64 46 64 20 2a 70 4e 65 78  ixUnusedFd *pNex
d32a0 74 3b 0a 20 20 66 6f 72 28 70 3d 70 4f 70 65 6e  t;.  for(p=pOpen
d32b0 2d 3e 70 55 6e 75 73 65 64 3b 20 70 3b 20 70 3d  ->pUnused; p; p=
d32c0 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e 65 78  pNext){.    pNex
d32d0 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20  t = p->pNext;.  
d32e0 20 20 69 66 28 20 63 6c 6f 73 65 28 70 2d 3e 66    if( close(p->f
d32f0 64 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c  d) ){.      pFil
d3300 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65  e->lastErrno = e
d3310 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d  rrno;.      rc =
d3320 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c   SQLITE_IOERR_CL
d3330 4f 53 45 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4e  OSE;.      p->pN
d3340 65 78 74 20 3d 20 70 45 72 72 6f 72 3b 0a 20 20  ext = pError;.  
d3350 20 20 20 20 70 45 72 72 6f 72 20 3d 20 70 3b 0a      pError = p;.
d3360 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
d3370 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29   sqlite3_free(p)
d3380 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 4f  ;.    }.  }.  pO
d3390 70 65 6e 2d 3e 70 55 6e 75 73 65 64 20 3d 20 70  pen->pUnused = p
d33a0 45 72 72 6f 72 3b 0a 20 20 72 65 74 75 72 6e 20  Error;.  return 
d33b0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  rc;.}../*.** Add
d33c0 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
d33d0 70 74 6f 72 20 75 73 65 64 20 62 79 20 66 69 6c  ptor used by fil
d33e0 65 20 68 61 6e 64 6c 65 20 70 46 69 6c 65 20 74  e handle pFile t
d33f0 6f 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  o the correspond
d3400 69 6e 67 0a 2a 2a 20 70 55 6e 75 73 65 64 20 6c  ing.** pUnused l
d3410 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ist..*/.static v
d3420 6f 69 64 20 73 65 74 50 65 6e 64 69 6e 67 46 64  oid setPendingFd
d3430 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65  (unixFile *pFile
d3440 29 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78  ){.  struct unix
d3450 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 20 3d  OpenCnt *pOpen =
d3460 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20   pFile->pOpen;. 
d3470 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70   UnixUnusedFd *p
d3480 20 3d 20 70 46 69 6c 65 2d 3e 70 55 6e 75 73 65   = pFile->pUnuse
d3490 64 3b 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20  d;.  p->pNext = 
d34a0 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64 3b 0a  pOpen->pUnused;.
d34b0 20 20 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64    pOpen->pUnused
d34c0 20 3d 20 70 3b 0a 20 20 70 46 69 6c 65 2d 3e 68   = p;.  pFile->h
d34d0 20 3d 20 2d 31 3b 0a 20 20 70 46 69 6c 65 2d 3e   = -1;.  pFile->
d34e0 70 55 6e 75 73 65 64 20 3d 20 30 3b 0a 7d 0a 0a  pUnused = 0;.}..
d34f0 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20  /*.** Lower the 
d3500 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e  locking level on
d3510 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
d3520 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79   pFile to lockty
d3530 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a  pe.  locktype.**
d3540 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20   must be either 
d3550 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45  NO_LOCK or SHARE
d3560 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66  D_LOCK..**.** If
d3570 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76   the locking lev
d3580 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64  el of the file d
d3590 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72  escriptor is alr
d35a0 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77  eady at or below
d35b0 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65  .** the requeste
d35c0 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c  d locking level,
d35d0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
d35e0 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61   a no-op..*/.sta
d35f0 74 69 63 20 69 6e 74 20 75 6e 69 78 55 6e 6c 6f  tic int unixUnlo
d3600 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
d3610 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70  *id, int locktyp
d3620 65 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  e){.  unixFile *
d3630 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
d3640 65 2a 29 69 64 3b 20 2f 2a 20 54 68 65 20 6f 70  e*)id; /* The op
d3650 65 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 73 74 72  en file */.  str
d3660 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f  uct unixLockInfo
d3670 20 2a 70 4c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a   *pLock;      /*
d3680 20 53 74 72 75 63 74 75 72 65 20 64 65 73 63 72   Structure descr
d3690 69 62 69 6e 67 20 63 75 72 72 65 6e 74 20 6c 6f  ibing current lo
d36a0 63 6b 20 73 74 61 74 65 20 2a 2f 0a 20 20 73 74  ck state */.  st
d36b0 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b  ruct flock lock;
d36c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
d36d0 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61  * Information pa
d36e0 73 73 65 64 20 69 6e 74 6f 20 66 63 6e 74 6c 28  ssed into fcntl(
d36f0 29 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20  ) */.  int rc = 
d3700 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
d3710 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
d3720 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 69 73  n code from this
d3730 20 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20   interface */.  
d3740 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20  int h;          
d3750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d3760 20 2f 2a 20 54 68 65 20 75 6e 64 65 72 6c 79 69   /* The underlyi
d3770 6e 67 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ng file descript
d3780 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 74 45 72 72  or */.  int tErr
d3790 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  no;             
d37a0 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f           /* Erro
d37b0 72 20 63 6f 64 65 20 66 72 6f 6d 20 73 79 73 74  r code from syst
d37c0 65 6d 20 63 61 6c 6c 20 65 72 72 6f 72 73 20 2a  em call errors *
d37d0 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69  /..  assert( pFi
d37e0 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 37  le );.  OSTRACE7
d37f0 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20  ("UNLOCK  %d %d 
d3800 77 61 73 20 25 64 28 25 64 2c 25 64 29 20 70 69  was %d(%d,%d) pi
d3810 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  d=%d\n", pFile->
d3820 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20  h, locktype,.   
d3830 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
d3840 70 65 2c 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b  pe, pFile->pLock
d3850 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c  ->locktype, pFil
d3860 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e 74 2c 20 67  e->pLock->cnt, g
d3870 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 61 73 73  etpid());..  ass
d3880 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53  ert( locktype<=S
d3890 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
d38a0 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
d38b0 79 70 65 3c 3d 6c 6f 63 6b 74 79 70 65 20 29 7b  ype<=locktype ){
d38c0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
d38d0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28  TE_OK;.  }.  if(
d38e0 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28   CHECK_THREADID(
d38f0 70 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 72 65  pFile) ){.    re
d3900 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  turn SQLITE_MISU
d3910 53 45 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 45 6e  SE;.  }.  unixEn
d3920 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 68 20  terMutex();.  h 
d3930 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 70 4c  = pFile->h;.  pL
d3940 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f  ock = pFile->pLo
d3950 63 6b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c  ck;.  assert( pL
d3960 6f 63 6b 2d 3e 63 6e 74 21 3d 30 20 29 3b 0a 20  ock->cnt!=0 );. 
d3970 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
d3980 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b  type>SHARED_LOCK
d3990 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
d39a0 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pLock->locktype=
d39b0 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65  =pFile->locktype
d39c0 20 29 3b 0a 20 20 20 20 53 69 6d 75 6c 61 74 65   );.    Simulate
d39d0 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29  IOErrorBenign(1)
d39e0 3b 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f  ;.    SimulateIO
d39f0 45 72 72 6f 72 28 20 68 3d 28 2d 31 29 20 29 0a  Error( h=(-1) ).
d3a00 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72      SimulateIOEr
d3a10 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 0a 23  rorBenign(0);..#
d3a20 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
d3a30 20 20 2f 2a 20 57 68 65 6e 20 72 65 64 75 63 69    /* When reduci
d3a40 6e 67 20 61 20 6c 6f 63 6b 20 73 75 63 68 20 74  ng a lock such t
d3a50 68 61 74 20 6f 74 68 65 72 20 70 72 6f 63 65 73  hat other proces
d3a60 73 65 73 20 63 61 6e 20 73 74 61 72 74 0a 20 20  ses can start.  
d3a70 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 74 68 65    ** reading the
d3a80 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
d3a90 67 61 69 6e 2c 20 6d 61 6b 65 20 73 75 72 65 20  gain, make sure 
d3aa0 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a 20  that the.    ** 
d3ab0 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e  transaction coun
d3ac0 74 65 72 20 77 61 73 20 75 70 64 61 74 65 64 20  ter was updated 
d3ad0 69 66 20 61 6e 79 20 70 61 72 74 20 6f 66 20 74  if any part of t
d3ae0 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20  he database.    
d3af0 2a 2a 20 66 69 6c 65 20 63 68 61 6e 67 65 64 2e  ** file changed.
d3b00 20 20 49 66 20 74 68 65 20 74 72 61 6e 73 61 63    If the transac
d3b10 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 69 73 20  tion counter is 
d3b20 6e 6f 74 20 75 70 64 61 74 65 64 2c 0a 20 20 20  not updated,.   
d3b30 20 2a 2a 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63   ** other connec
d3b40 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 73 61 6d  tions to the sam
d3b50 65 20 66 69 6c 65 20 6d 69 67 68 74 20 6e 6f 74  e file might not
d3b60 20 72 65 61 6c 69 7a 65 20 74 68 61 74 0a 20 20   realize that.  
d3b70 20 20 2a 2a 20 74 68 65 20 66 69 6c 65 20 68 61    ** the file ha
d3b80 73 20 63 68 61 6e 67 65 64 20 61 6e 64 20 68 65  s changed and he
d3b90 6e 63 65 20 6d 69 67 68 74 20 6e 6f 74 20 6b 6e  nce might not kn
d3ba0 6f 77 20 74 6f 20 66 6c 75 73 68 20 74 68 65 69  ow to flush thei
d3bb0 72 0a 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 20  r.    ** cache. 
d3bc0 20 54 68 65 20 75 73 65 20 6f 66 20 61 20 73 74   The use of a st
d3bd0 61 6c 65 20 63 61 63 68 65 20 63 61 6e 20 6c 65  ale cache can le
d3be0 61 64 20 74 6f 20 64 61 74 61 62 61 73 65 20 63  ad to database c
d3bf0 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 20 20 2a  orruption..    *
d3c00 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46  /.    assert( pF
d3c10 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69  ile->inNormalWri
d3c20 74 65 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 7c  te==0.         |
d3c30 7c 20 70 46 69 6c 65 2d 3e 64 62 55 70 64 61 74  | pFile->dbUpdat
d3c40 65 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 7c 7c  e==0.         ||
d3c50 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74   pFile->transCnt
d3c60 72 43 68 6e 67 3d 3d 31 20 29 3b 0a 20 20 20 20  rChng==1 );.    
d3c70 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57  pFile->inNormalW
d3c80 72 69 74 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66  rite = 0;.#endif
d3c90 0a 0a 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 74  ...    if( lockt
d3ca0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
d3cb0 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72 61   ){.      if( ra
d3cc0 6e 67 65 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 46  ngeLock(pFile, F
d3cd0 5f 52 44 4c 43 4b 2c 20 26 74 45 72 72 6e 6f 29  _RDLCK, &tErrno)
d3ce0 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  ==(-1) ){.      
d3cf0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72    rc = sqliteErr
d3d00 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72  orFromPosixError
d3d10 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f  (tErrno, SQLITE_
d3d20 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 29 3b 0a 20  IOERR_RDLOCK);. 
d3d30 20 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f         if( IS_LO
d3d40 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a  CK_ERROR(rc) ){.
d3d50 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d            pFile-
d3d60 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72  >lastErrno = tEr
d3d70 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  rno;.        }. 
d3d80 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
d3d90 75 6e 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 7d 0a  unlock;.      }.
d3da0 20 20 20 20 7d 0a 20 20 20 20 6c 6f 63 6b 2e 6c      }.    lock.l
d3db0 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b  _type = F_UNLCK;
d3dc0 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e  .    lock.l_when
d3dd0 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20  ce = SEEK_SET;. 
d3de0 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20     lock.l_start 
d3df0 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a  = PENDING_BYTE;.
d3e00 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d      lock.l_len =
d3e10 20 32 4c 3b 20 20 61 73 73 65 72 74 28 20 50 45   2L;  assert( PE
d3e20 4e 44 49 4e 47 5f 42 59 54 45 2b 31 3d 3d 52 45  NDING_BYTE+1==RE
d3e30 53 45 52 56 45 44 5f 42 59 54 45 20 29 3b 0a 20  SERVED_BYTE );. 
d3e40 20 20 20 69 66 28 20 66 63 6e 74 6c 28 68 2c 20     if( fcntl(h, 
d3e50 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21  F_SETLK, &lock)!
d3e60 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 70  =(-1) ){.      p
d3e70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  Lock->locktype =
d3e80 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20   SHARED_LOCK;.  
d3e90 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 74    }else{.      t
d3ea0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
d3eb0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
d3ec0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
d3ed0 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49  ror(tErrno, SQLI
d3ee0 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29  TE_IOERR_UNLOCK)
d3ef0 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c  ;.      if( IS_L
d3f00 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b  OCK_ERROR(rc) ){
d3f10 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  .        pFile->
d3f20 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72  lastErrno = tErr
d3f30 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  no;.      }.    
d3f40 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63    goto end_unloc
d3f50 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  k;.    }.  }.  i
d3f60 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f  f( locktype==NO_
d3f70 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 73 74 72 75  LOCK ){.    stru
d3f80 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a  ct unixOpenCnt *
d3f90 70 4f 70 65 6e 3b 0a 0a 20 20 20 20 2f 2a 20 44  pOpen;..    /* D
d3fa0 65 63 72 65 6d 65 6e 74 20 74 68 65 20 73 68 61  ecrement the sha
d3fb0 72 65 64 20 6c 6f 63 6b 20 63 6f 75 6e 74 65 72  red lock counter
d3fc0 2e 20 20 52 65 6c 65 61 73 65 20 74 68 65 20 6c  .  Release the l
d3fd0 6f 63 6b 20 75 73 69 6e 67 20 61 6e 0a 20 20 20  ock using an.   
d3fe0 20 2a 2a 20 4f 53 20 63 61 6c 6c 20 6f 6e 6c 79   ** OS call only
d3ff0 20 77 68 65 6e 20 61 6c 6c 20 74 68 72 65 61 64   when all thread
d4000 73 20 69 6e 20 74 68 69 73 20 73 61 6d 65 20 70  s in this same p
d4010 72 6f 63 65 73 73 20 68 61 76 65 20 72 65 6c 65  rocess have rele
d4020 61 73 65 64 0a 20 20 20 20 2a 2a 20 74 68 65 20  ased.    ** the 
d4030 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  lock..    */.   
d4040 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2d 2d 3b 0a 20   pLock->cnt--;. 
d4050 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 63 6e     if( pLock->cn
d4060 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6c 6f  t==0 ){.      lo
d4070 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e  ck.l_type = F_UN
d4080 4c 43 4b 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e  LCK;.      lock.
d4090 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f  l_whence = SEEK_
d40a0 53 45 54 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e  SET;.      lock.
d40b0 6c 5f 73 74 61 72 74 20 3d 20 6c 6f 63 6b 2e 6c  l_start = lock.l
d40c0 5f 6c 65 6e 20 3d 20 30 4c 3b 0a 20 20 20 20 20  _len = 0L;.     
d40d0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
d40e0 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20 20 20 20  Benign(1);.     
d40f0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
d4100 28 20 68 3d 28 2d 31 29 20 29 0a 20 20 20 20 20  ( h=(-1) ).     
d4110 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
d4120 42 65 6e 69 67 6e 28 30 29 3b 0a 20 20 20 20 20  Benign(0);.     
d4130 20 69 66 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f   if( fcntl(h, F_
d4140 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 28  SETLK, &lock)!=(
d4150 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70  -1) ){.        p
d4160 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  Lock->locktype =
d4170 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20   NO_LOCK;.      
d4180 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 74  }else{.        t
d4190 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
d41a0 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
d41b0 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78  teErrorFromPosix
d41c0 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51  Error(tErrno, SQ
d41d0 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43  LITE_IOERR_UNLOC
d41e0 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  K);.        if( 
d41f0 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63  IS_LOCK_ERROR(rc
d4200 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  ) ){.          p
d4210 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
d4220 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20  = tErrno;.      
d4230 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4c 6f 63    }.        pLoc
d4240 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f  k->locktype = NO
d4250 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 70  _LOCK;.        p
d4260 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
d4270 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20   NO_LOCK;.      
d4280 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
d4290 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 63 6f  Decrement the co
d42a0 75 6e 74 20 6f 66 20 6c 6f 63 6b 73 20 61 67 61  unt of locks aga
d42b0 69 6e 73 74 20 74 68 69 73 20 73 61 6d 65 20 66  inst this same f
d42c0 69 6c 65 2e 20 20 57 68 65 6e 20 74 68 65 0a 20  ile.  When the. 
d42d0 20 20 20 2a 2a 20 63 6f 75 6e 74 20 72 65 61 63     ** count reac
d42e0 68 65 73 20 7a 65 72 6f 2c 20 63 6c 6f 73 65 20  hes zero, close 
d42f0 61 6e 79 20 6f 74 68 65 72 20 66 69 6c 65 20 64  any other file d
d4300 65 73 63 72 69 70 74 6f 72 73 20 77 68 6f 73 65  escriptors whose
d4310 20 63 6c 6f 73 65 0a 20 20 20 20 2a 2a 20 77 61   close.    ** wa
d4320 73 20 64 65 66 65 72 72 65 64 20 62 65 63 61 75  s deferred becau
d4330 73 65 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e  se of outstandin
d4340 67 20 6c 6f 63 6b 73 2e 0a 20 20 20 20 2a 2f 0a  g locks..    */.
d4350 20 20 20 20 70 4f 70 65 6e 20 3d 20 70 46 69 6c      pOpen = pFil
d4360 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20 20 70 4f  e->pOpen;.    pO
d4370 70 65 6e 2d 3e 6e 4c 6f 63 6b 2d 2d 3b 0a 20 20  pen->nLock--;.  
d4380 20 20 61 73 73 65 72 74 28 20 70 4f 70 65 6e 2d    assert( pOpen-
d4390 3e 6e 4c 6f 63 6b 3e 3d 30 20 29 3b 0a 20 20 20  >nLock>=0 );.   
d43a0 20 69 66 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63   if( pOpen->nLoc
d43b0 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e  k==0 ){.      in
d43c0 74 20 72 63 32 20 3d 20 63 6c 6f 73 65 50 65 6e  t rc2 = closePen
d43d0 64 69 6e 67 46 64 73 28 70 46 69 6c 65 29 3b 0a  dingFds(pFile);.
d43e0 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
d43f0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
d4400 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20     rc = rc2;.   
d4410 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 09     }.    }.  }..
d4420 0a 65 6e 64 5f 75 6e 6c 6f 63 6b 3a 0a 20 20 75  .end_unlock:.  u
d4430 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  nixLeaveMutex();
d4440 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
d4450 45 5f 4f 4b 20 29 20 70 46 69 6c 65 2d 3e 6c 6f  E_OK ) pFile->lo
d4460 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70  cktype = locktyp
d4470 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  e;.  return rc;.
d4480 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
d4490 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d 73 20  nction performs 
d44a0 74 68 65 20 70 61 72 74 73 20 6f 66 20 74 68 65  the parts of the
d44b0 20 22 63 6c 6f 73 65 20 66 69 6c 65 22 20 6f 70   "close file" op
d44c0 65 72 61 74 69 6f 6e 20 0a 2a 2a 20 63 6f 6d 6d  eration .** comm
d44d0 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69 6e  on to all lockin
d44e0 67 20 73 63 68 65 6d 65 73 2e 20 49 74 20 63 6c  g schemes. It cl
d44f0 6f 73 65 73 20 74 68 65 20 64 69 72 65 63 74 6f  oses the directo
d4500 72 79 20 61 6e 64 20 66 69 6c 65 0a 2a 2a 20 68  ry and file.** h
d4510 61 6e 64 6c 65 73 2c 20 69 66 20 74 68 65 79 20  andles, if they 
d4520 61 72 65 20 76 61 6c 69 64 2c 20 61 6e 64 20 73  are valid, and s
d4530 65 74 73 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f  ets all fields o
d4540 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 0a 2a  f the unixFile.*
d4550 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 30  * structure to 0
d4560 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 2a 6e  ..**.** It is *n
d4570 6f 74 2a 20 6e 65 63 65 73 73 61 72 79 20 74 6f  ot* necessary to
d4580 20 68 6f 6c 64 20 74 68 65 20 6d 75 74 65 78 20   hold the mutex 
d4590 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  when this routin
d45a0 65 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20  e is called,.** 
d45b0 65 76 65 6e 20 6f 6e 20 56 78 57 6f 72 6b 73 2e  even on VxWorks.
d45c0 20 20 41 20 6d 75 74 65 78 20 77 69 6c 6c 20 62    A mutex will b
d45d0 65 20 61 63 71 75 69 72 65 64 20 6f 6e 20 56 78  e acquired on Vx
d45e0 57 6f 72 6b 73 20 62 79 20 74 68 65 0a 2a 2a 20  Works by the.** 
d45f0 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65 46 69  vxworksReleaseFi
d4600 6c 65 49 64 28 29 20 72 6f 75 74 69 6e 65 2e 0a  leId() routine..
d4610 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6c  */.static int cl
d4620 6f 73 65 55 6e 69 78 46 69 6c 65 28 73 71 6c 69  oseUnixFile(sqli
d4630 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20  te3_file *id){. 
d4640 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
d4650 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
d4660 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 20 29 7b  ;.  if( pFile ){
d4670 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  .    if( pFile->
d4680 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20  dirfd>=0 ){.    
d4690 20 20 69 6e 74 20 65 72 72 20 3d 20 63 6c 6f 73    int err = clos
d46a0 65 28 70 46 69 6c 65 2d 3e 64 69 72 66 64 29 3b  e(pFile->dirfd);
d46b0 0a 20 20 20 20 20 20 69 66 28 20 65 72 72 20 29  .      if( err )
d46c0 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  {.        pFile-
d46d0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72  >lastErrno = err
d46e0 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  no;.        retu
d46f0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
d4700 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20  DIR_CLOSE;.     
d4710 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
d4720 70 46 69 6c 65 2d 3e 64 69 72 66 64 3d 2d 31 3b  pFile->dirfd=-1;
d4730 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
d4740 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e     if( pFile->h>
d4750 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  =0 ){.      int 
d4760 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c  err = close(pFil
d4770 65 2d 3e 68 29 3b 0a 20 20 20 20 20 20 69 66 28  e->h);.      if(
d4780 20 65 72 72 20 29 7b 0a 20 20 20 20 20 20 20 20   err ){.        
d4790 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
d47a0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20   = errno;.      
d47b0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d47c0 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 20  IOERR_CLOSE;.   
d47d0 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 20 4f     }.    }.#if O
d47e0 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 69 66  S_VXWORKS.    if
d47f0 28 20 70 46 69 6c 65 2d 3e 70 49 64 20 29 7b 0a  ( pFile->pId ){.
d4800 20 20 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d        if( pFile-
d4810 3e 69 73 44 65 6c 65 74 65 20 29 7b 0a 20 20 20  >isDelete ){.   
d4820 20 20 20 20 20 75 6e 6c 69 6e 6b 28 70 46 69 6c       unlink(pFil
d4830 65 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e 69 63  e->pId->zCanonic
d4840 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d  alName);.      }
d4850 0a 20 20 20 20 20 20 76 78 77 6f 72 6b 73 52 65  .      vxworksRe
d4860 6c 65 61 73 65 46 69 6c 65 49 64 28 70 46 69 6c  leaseFileId(pFil
d4870 65 2d 3e 70 49 64 29 3b 0a 20 20 20 20 20 20 70  e->pId);.      p
d4880 46 69 6c 65 2d 3e 70 49 64 20 3d 20 30 3b 0a 20  File->pId = 0;. 
d4890 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
d48a0 4f 53 54 52 41 43 45 32 28 22 43 4c 4f 53 45 20  OSTRACE2("CLOSE 
d48b0 20 20 25 2d 33 64 5c 6e 22 2c 20 70 46 69 6c 65    %-3d\n", pFile
d48c0 2d 3e 68 29 3b 0a 20 20 20 20 4f 70 65 6e 43 6f  ->h);.    OpenCo
d48d0 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 20 20 73  unter(-1);.    s
d48e0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c  qlite3_free(pFil
d48f0 65 2d 3e 70 55 6e 75 73 65 64 29 3b 0a 20 20 20  e->pUnused);.   
d4900 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c 20 30   memset(pFile, 0
d4910 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c  , sizeof(unixFil
d4920 65 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  e));.  }.  retur
d4930 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
d4940 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69  /*.** Close a fi
d4950 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
d4960 74 20 75 6e 69 78 43 6c 6f 73 65 28 73 71 6c 69  t unixClose(sqli
d4970 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20  te3_file *id){. 
d4980 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
d4990 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 64 20 29 7b  _OK;.  if( id ){
d49a0 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  .    unixFile *p
d49b0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
d49c0 20 2a 29 69 64 3b 0a 20 20 20 20 75 6e 69 78 55   *)id;.    unixU
d49d0 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43  nlock(id, NO_LOC
d49e0 4b 29 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65  K);.    unixEnte
d49f0 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 69 66  rMutex();.    if
d4a00 28 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 20 26  ( pFile->pOpen &
d4a10 26 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e  & pFile->pOpen->
d4a20 6e 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 2f  nLock ){.      /
d4a30 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6f  * If there are o
d4a40 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73  utstanding locks
d4a50 2c 20 64 6f 20 6e 6f 74 20 61 63 74 75 61 6c 6c  , do not actuall
d4a60 79 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65  y close the file
d4a70 20 6a 75 73 74 0a 20 20 20 20 20 20 2a 2a 20 79   just.      ** y
d4a80 65 74 20 62 65 63 61 75 73 65 20 74 68 61 74 20  et because that 
d4a90 77 6f 75 6c 64 20 63 6c 65 61 72 20 74 68 6f 73  would clear thos
d4aa0 65 20 6c 6f 63 6b 73 2e 20 20 49 6e 73 74 65 61  e locks.  Instea
d4ab0 64 2c 20 61 64 64 20 74 68 65 20 66 69 6c 65 0a  d, add the file.
d4ac0 20 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69 70        ** descrip
d4ad0 74 6f 72 20 74 6f 20 70 4f 70 65 6e 2d 3e 70 55  tor to pOpen->pU
d4ae0 6e 75 73 65 64 20 6c 69 73 74 2e 20 20 49 74 20  nused list.  It 
d4af0 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69  will be automati
d4b00 63 61 6c 6c 79 20 63 6c 6f 73 65 64 20 0a 20 20  cally closed .  
d4b10 20 20 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20      ** when the 
d4b20 6c 61 73 74 20 6c 6f 63 6b 20 69 73 20 63 6c 65  last lock is cle
d4b30 61 72 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ared..      */. 
d4b40 20 20 20 20 20 73 65 74 50 65 6e 64 69 6e 67 46       setPendingF
d4b50 64 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a  d(pFile);.    }.
d4b60 20 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49      releaseLockI
d4b70 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b  nfo(pFile->pLock
d4b80 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 4f 70  );.    releaseOp
d4b90 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70  enCnt(pFile->pOp
d4ba0 65 6e 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c  en);.    rc = cl
d4bb0 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b  oseUnixFile(id);
d4bc0 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75  .    unixLeaveMu
d4bd0 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74  tex();.  }.  ret
d4be0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  urn rc;.}../****
d4bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
d4c00 66 20 74 68 65 20 70 6f 73 69 78 20 61 64 76 69  f the posix advi
d4c10 73 6f 72 79 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d  sory lock implem
d4c20 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a  entation *******
d4c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a  **********.*****
d4c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d4c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d4c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d4c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d4c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a  *********/../***
d4c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d4ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d4cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d4cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d4cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a  ***********.****
d4ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d4cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 2d 6f 70  ********** No-op
d4d00 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a   Locking *******
d4d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d4d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
d4d30 2a 20 4f 66 20 74 68 65 20 76 61 72 69 6f 75 73  * Of the various
d4d40 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65   locking impleme
d4d50 6e 74 61 74 69 6f 6e 73 20 61 76 61 69 6c 61 62  ntations availab
d4d60 6c 65 2c 20 74 68 69 73 20 69 73 20 62 79 20 66  le, this is by f
d4d70 61 72 20 74 68 65 0a 2a 2a 20 73 69 6d 70 6c 65  ar the.** simple
d4d80 73 74 3a 20 20 6c 6f 63 6b 69 6e 67 20 69 73 20  st:  locking is 
d4d90 69 67 6e 6f 72 65 64 2e 20 20 4e 6f 20 61 74 74  ignored.  No att
d4da0 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20  empt is made to 
d4db0 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73  lock the databas
d4dc0 65 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 20 72 65  e.** file for re
d4dd0 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67  ading or writing
d4de0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f 63  ..**.** This loc
d4df0 6b 69 6e 67 20 6d 6f 64 65 20 69 73 20 61 70 70  king mode is app
d4e00 72 6f 70 72 69 61 74 65 20 66 6f 72 20 75 73 65  ropriate for use
d4e10 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61   on read-only da
d4e20 74 61 62 61 73 65 73 0a 2a 2a 20 28 65 78 3a 20  tabases.** (ex: 
d4e30 64 61 74 61 62 61 73 65 73 20 74 68 61 74 20 61  databases that a
d4e40 72 65 20 62 75 72 6e 65 64 20 69 6e 74 6f 20 43  re burned into C
d4e50 44 2d 52 4f 4d 2c 20 66 6f 72 20 65 78 61 6d 70  D-ROM, for examp
d4e60 6c 65 2e 29 20 20 49 74 20 63 61 6e 0a 2a 2a 20  le.)  It can.** 
d4e70 61 6c 73 6f 20 62 65 20 75 73 65 64 20 69 66 20  also be used if 
d4e80 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
d4e90 65 6d 70 6c 6f 79 73 20 73 6f 6d 65 20 65 78 74  employs some ext
d4ea0 65 72 6e 61 6c 20 6d 65 63 68 61 6e 69 73 6d 20  ernal mechanism 
d4eb0 74 6f 0a 2a 2a 20 70 72 65 76 65 6e 74 20 73 69  to.** prevent si
d4ec0 6d 75 6c 74 61 6e 65 6f 75 73 20 61 63 63 65 73  multaneous acces
d4ed0 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 64 61  s of the same da
d4ee0 74 61 62 61 73 65 20 62 79 20 74 77 6f 20 6f 72  tabase by two or
d4ef0 20 6d 6f 72 65 0a 2a 2a 20 64 61 74 61 62 61 73   more.** databas
d4f00 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20  e connections.  
d4f10 42 75 74 20 74 68 65 72 65 20 69 73 20 61 20 73  But there is a s
d4f20 65 72 69 6f 75 73 20 72 69 73 6b 20 6f 66 20 64  erious risk of d
d4f30 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 72 72 75  atabase.** corru
d4f40 70 74 69 6f 6e 20 69 66 20 74 68 69 73 20 6c 6f  ption if this lo
d4f50 63 6b 69 6e 67 20 6d 6f 64 65 20 69 73 20 75 73  cking mode is us
d4f60 65 64 20 69 6e 20 73 69 74 75 61 74 69 6f 6e 73  ed in situations
d4f70 20 77 68 65 72 65 20 6d 75 6c 74 69 70 6c 65 0a   where multiple.
d4f80 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ** database conn
d4f90 65 63 74 69 6f 6e 73 20 61 72 65 20 61 63 63 65  ections are acce
d4fa0 73 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64  ssing the same d
d4fb0 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 74 20  atabase file at 
d4fc0 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 69 6d 65  the same.** time
d4fd0 20 61 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65   and one or more
d4fe0 20 6f 66 20 74 68 6f 73 65 20 63 6f 6e 6e 65 63   of those connec
d4ff0 74 69 6f 6e 73 20 61 72 65 20 77 72 69 74 69 6e  tions are writin
d5000 67 2e 0a 2a 2f 0a 0a 73 74 61 74 69 63 20 69 6e  g..*/..static in
d5010 74 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52 65 73  t nolockCheckRes
d5020 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65  ervedLock(sqlite
d5030 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c  3_file *NotUsed,
d5040 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a   int *pResOut){.
d5050 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
d5060 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 2a  ER(NotUsed);.  *
d5070 70 52 65 73 4f 75 74 20 3d 20 30 3b 0a 20 20 72  pResOut = 0;.  r
d5080 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
d5090 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f  .}.static int no
d50a0 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33  lockLock(sqlite3
d50b0 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20  _file *NotUsed, 
d50c0 69 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20  int NotUsed2){. 
d50d0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
d50e0 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55  R2(NotUsed, NotU
d50f0 73 65 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20  sed2);.  return 
d5100 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74 61  SQLITE_OK;.}.sta
d5110 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 55 6e  tic int nolockUn
d5120 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  lock(sqlite3_fil
d5130 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20  e *NotUsed, int 
d5140 4e 6f 74 55 73 65 64 32 29 7b 0a 20 20 55 4e 55  NotUsed2){.  UNU
d5150 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e  SED_PARAMETER2(N
d5160 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32  otUsed, NotUsed2
d5170 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
d5180 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
d5190 43 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a  Close the file..
d51a0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f  */.static int no
d51b0 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65  lockClose(sqlite
d51c0 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20  3_file *id) {.  
d51d0 72 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e 69 78  return closeUnix
d51e0 46 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a 2f 2a 2a  File(id);.}../**
d51f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5200 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 6e 6f 2d  * End of the no-
d5210 6f 70 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e  op lock implemen
d5220 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  tation *********
d5230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a  ************.***
d5240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
d5290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d52a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d52b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d52c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d52d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
d52e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d52f0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 64 6f  ******* Begin do
d5300 74 2d 66 69 6c 65 20 4c 6f 63 6b 69 6e 67 20 2a  t-file Locking *
d5310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
d5330 0a 2a 2a 20 54 68 65 20 64 6f 74 66 69 6c 65 20  .** The dotfile 
d5340 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e  locking implemen
d5350 74 61 74 69 6f 6e 20 75 73 65 73 20 74 68 65 20  tation uses the 
d5360 65 78 69 73 74 61 6e 63 65 20 6f 66 20 73 65 70  existance of sep
d5370 61 72 61 74 65 20 6c 6f 63 6b 0a 2a 2a 20 66 69  arate lock.** fi
d5380 6c 65 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  les in order to 
d5390 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 74  control access t
d53a0 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  o the database. 
d53b0 20 54 68 69 73 20 77 6f 72 6b 73 20 6f 6e 20 6a   This works on j
d53c0 75 73 74 0a 2a 2a 20 61 62 6f 75 74 20 65 76 65  ust.** about eve
d53d0 72 79 20 66 69 6c 65 73 79 73 74 65 6d 20 69 6d  ry filesystem im
d53e0 61 67 69 6e 61 62 6c 65 2e 20 20 42 75 74 20 74  aginable.  But t
d53f0 68 65 72 65 20 61 72 65 20 73 65 72 69 6f 75 73  here are serious
d5400 20 64 6f 77 6e 73 69 64 65 73 3a 0a 2a 2a 0a 2a   downsides:.**.*
d5410 2a 20 20 20 20 28 31 29 20 20 54 68 65 72 65 20  *    (1)  There 
d5420 69 73 20 7a 65 72 6f 20 63 6f 6e 63 75 72 72 65  is zero concurre
d5430 6e 63 79 2e 20 20 41 20 73 69 6e 67 6c 65 20 72  ncy.  A single r
d5440 65 61 64 65 72 20 62 6c 6f 63 6b 73 20 61 6c 6c  eader blocks all
d5450 20 6f 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 20   other.**       
d5460 20 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 72    connections fr
d5470 6f 6d 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72  om reading or wr
d5480 69 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  iting the databa
d5490 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29  se..**.**    (2)
d54a0 20 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e    An application
d54b0 20 63 72 61 73 68 20 6f 72 20 70 6f 77 65 72 20   crash or power 
d54c0 6c 6f 73 73 20 63 61 6e 20 6c 65 61 76 65 20 73  loss can leave s
d54d0 74 61 6c 65 20 6c 6f 63 6b 20 66 69 6c 65 73 0a  tale lock files.
d54e0 2a 2a 20 20 20 20 20 20 20 20 20 73 69 74 74 69  **         sitti
d54f0 6e 67 20 61 72 6f 75 6e 64 20 74 68 61 74 20 6e  ng around that n
d5500 65 65 64 20 74 6f 20 62 65 20 63 6c 65 61 72 65  eed to be cleare
d5510 64 20 6d 61 6e 75 61 6c 6c 79 2e 0a 2a 2a 0a 2a  d manually..**.*
d5520 2a 20 4e 65 76 65 72 74 68 65 6c 65 73 73 2c 20  * Nevertheless, 
d5530 61 20 64 6f 74 6c 6f 63 6b 20 69 73 20 61 6e 20  a dotlock is an 
d5540 61 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b  appropriate lock
d5550 69 6e 67 20 6d 6f 64 65 20 66 6f 72 20 75 73 65  ing mode for use
d5560 20 69 66 20 6e 6f 0a 2a 2a 20 6f 74 68 65 72 20   if no.** other 
d5570 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79  locking strategy
d5580 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a   is available..*
d5590 2a 0a 2a 2a 20 44 6f 74 66 69 6c 65 20 6c 6f 63  *.** Dotfile loc
d55a0 6b 69 6e 67 20 77 6f 72 6b 73 20 62 79 20 63 72  king works by cr
d55b0 65 61 74 69 6e 67 20 61 20 66 69 6c 65 20 69 6e  eating a file in
d55c0 20 74 68 65 20 73 61 6d 65 20 64 69 72 65 63 74   the same direct
d55d0 6f 72 79 20 61 73 20 74 68 65 0a 2a 2a 20 64 61  ory as the.** da
d55e0 74 61 62 61 73 65 20 61 6e 64 20 77 69 74 68 20  tabase and with 
d55f0 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20 62 75  the same name bu
d5600 74 20 77 69 74 68 20 61 20 22 2e 6c 6f 63 6b 22  t with a ".lock"
d5610 20 65 78 74 65 6e 73 69 6f 6e 20 61 64 64 65 64   extension added
d5620 2e 0a 2a 2a 20 54 68 65 20 65 78 69 73 74 61 6e  ..** The existan
d5630 63 65 20 6f 66 20 61 20 6c 6f 63 6b 20 66 69 6c  ce of a lock fil
d5640 65 20 69 6d 70 6c 69 65 73 20 61 6e 20 45 58 43  e implies an EXC
d5650 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 41 6c  LUSIVE lock.  Al
d5660 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 0a 2a 2a 20  l other lock.** 
d5670 74 79 70 65 73 20 28 53 48 41 52 45 44 2c 20 52  types (SHARED, R
d5680 45 53 45 52 56 45 44 2c 20 50 45 4e 44 49 4e 47  ESERVED, PENDING
d5690 29 20 61 72 65 20 6d 61 70 70 65 64 20 69 6e 74  ) are mapped int
d56a0 6f 20 45 58 43 4c 55 53 49 56 45 2e 0a 2a 2f 0a  o EXCLUSIVE..*/.
d56b0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20  ./*.** The file 
d56c0 73 75 66 66 69 78 20 61 64 64 65 64 20 74 6f 20  suffix added to 
d56d0 74 68 65 20 64 61 74 61 20 62 61 73 65 20 66 69  the data base fi
d56e0 6c 65 6e 61 6d 65 20 69 6e 20 6f 72 64 65 72 20  lename in order 
d56f0 74 6f 20 63 72 65 61 74 65 20 74 68 65 0a 2a 2a  to create the.**
d5700 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a 2a 2f 0a 23   lock file..*/.#
d5710 64 65 66 69 6e 65 20 44 4f 54 4c 4f 43 4b 5f 53  define DOTLOCK_S
d5720 55 46 46 49 58 20 22 2e 6c 6f 63 6b 22 0a 0a 2f  UFFIX ".lock"../
d5730 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
d5740 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72  e checks if ther
d5750 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20  e is a RESERVED 
d5760 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65  lock held on the
d5770 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69   specified.** fi
d5780 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e  le by this or an
d5790 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e  y other process.
d57a0 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20   If such a lock 
d57b0 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52  is held, set *pR
d57c0 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f  esOut.** to a no
d57d0 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68  n-zero value oth
d57e0 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20  erwise *pResOut 
d57f0 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20  is set to zero. 
d5800 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
d5810 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53  e.** is set to S
d5820 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20  QLITE_OK unless 
d5830 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63  an I/O error occ
d5840 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20  urs during lock 
d5850 63 68 65 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  checking..**.** 
d5860 49 6e 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69  In dotfile locki
d5870 6e 67 2c 20 65 69 74 68 65 72 20 61 20 6c 6f 63  ng, either a loc
d5880 6b 20 65 78 69 73 74 73 20 6f 72 20 69 74 20 64  k exists or it d
d5890 6f 65 73 20 6e 6f 74 2e 20 20 53 6f 20 69 6e 20  oes not.  So in 
d58a0 74 68 69 73 0a 2a 2a 20 76 61 72 69 61 74 69 6f  this.** variatio
d58b0 6e 20 6f 66 20 43 68 65 63 6b 52 65 73 65 72 76  n of CheckReserv
d58c0 65 64 4c 6f 63 6b 28 29 2c 20 2a 70 52 65 73 4f  edLock(), *pResO
d58d0 75 74 20 69 73 20 73 65 74 20 74 6f 20 74 72 75  ut is set to tru
d58e0 65 20 69 66 20 61 6e 79 20 6c 6f 63 6b 0a 2a 2a  e if any lock.**
d58f0 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20   is held on the 
d5900 66 69 6c 65 20 61 6e 64 20 66 61 6c 73 65 20 69  file and false i
d5910 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 75 6e  f the file is un
d5920 6c 6f 63 6b 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  locked..*/.stati
d5930 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 43 68 65  c int dotlockChe
d5940 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73  ckReservedLock(s
d5950 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
d5960 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b   int *pResOut) {
d5970 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
d5980 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73  TE_OK;.  int res
d5990 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69  erved = 0;.  uni
d59a0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
d59b0 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20  unixFile*)id;.. 
d59c0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
d59d0 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ( return SQLITE_
d59e0 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52  IOERR_CHECKRESER
d59f0 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20  VEDLOCK; );.  . 
d5a00 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
d5a10 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66  ;..  /* Check if
d5a20 20 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69   a thread in thi
d5a30 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20  s process holds 
d5a40 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20  such a lock */. 
d5a50 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
d5a60 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b  type>SHARED_LOCK
d5a70 20 29 7b 0a 20 20 20 20 2f 2a 20 45 69 74 68 65   ){.    /* Eithe
d5a80 72 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f  r this connectio
d5a90 6e 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20  n or some other 
d5aa0 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 20 74 68  connection in th
d5ab0 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 0a 20  e same process. 
d5ac0 20 20 20 2a 2a 20 68 6f 6c 64 73 20 61 20 6c 6f     ** holds a lo
d5ad0 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20  ck on the file. 
d5ae0 20 4e 6f 20 6e 65 65 64 20 74 6f 20 63 68 65 63   No need to chec
d5af0 6b 20 66 75 72 74 68 65 72 2e 20 2a 2f 0a 20 20  k further. */.  
d5b00 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a    reserved = 1;.
d5b10 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
d5b20 54 68 65 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64  The lock is held
d5b30 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 20   if and only if 
d5b40 74 68 65 20 6c 6f 63 6b 66 69 6c 65 20 65 78 69  the lockfile exi
d5b50 73 74 73 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74  sts */.    const
d5b60 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65   char *zLockFile
d5b70 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29   = (const char*)
d5b80 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
d5b90 6e 74 65 78 74 3b 0a 20 20 20 20 72 65 73 65 72  ntext;.    reser
d5ba0 76 65 64 20 3d 20 61 63 63 65 73 73 28 7a 4c 6f  ved = access(zLo
d5bb0 63 6b 46 69 6c 65 2c 20 30 29 3d 3d 30 3b 0a 20  ckFile, 0)==0;. 
d5bc0 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54   }.  OSTRACE4("T
d5bd0 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25  EST WR-LOCK %d %
d5be0 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  d %d\n", pFile->
d5bf0 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29  h, rc, reserved)
d5c00 3b 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72  ;.  *pResOut = r
d5c10 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72  eserved;.  retur
d5c20 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  n rc;.}../*.** L
d5c30 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74  ock the file wit
d5c40 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69  h the lock speci
d5c50 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65  fied by paramete
d5c60 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65  r locktype - one
d5c70 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  .** of the follo
d5c80 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  wing:.**.**     
d5c90 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a  (1) SHARED_LOCK.
d5ca0 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52  **     (2) RESER
d5cb0 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20  VED_LOCK.**     
d5cc0 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  (3) PENDING_LOCK
d5cd0 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c  .**     (4) EXCL
d5ce0 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a  USIVE_LOCK.**.**
d5cf0 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20   Sometimes when 
d5d00 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c  requesting one l
d5d10 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74  ock state, addit
d5d20 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65  ional lock state
d5d30 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65  s.** are inserte
d5d40 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54  d in between.  T
d5d50 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74  he locking might
d5d60 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20   fail on one of 
d5d70 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61  the later.** tra
d5d80 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67  nsitions leaving
d5d90 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20   the lock state 
d5da0 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77  different from w
d5db0 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62  hat it started b
d5dc0 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72  ut.** still shor
d5dd0 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20  t of its goal.  
d5de0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68  The following ch
d5df0 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c  art shows the al
d5e00 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74  lowed.** transit
d5e10 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73  ions and the ins
d5e20 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61  erted intermedia
d5e30 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a  te states:.**.**
d5e40 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20      UNLOCKED -> 
d5e50 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41  SHARED.**    SHA
d5e60 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a  RED -> RESERVED.
d5e70 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20  **    SHARED -> 
d5e80 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43  (PENDING) -> EXC
d5e90 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53  LUSIVE.**    RES
d5ea0 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e  ERVED -> (PENDIN
d5eb0 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a  G) -> EXCLUSIVE.
d5ec0 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e  **    PENDING ->
d5ed0 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a   EXCLUSIVE.**.**
d5ee0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   This routine wi
d5ef0 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65  ll only increase
d5f00 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68   a lock.  Use th
d5f10 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63  e sqlite3OsUnloc
d5f20 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74  k().** routine t
d5f30 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e  o lower a lockin
d5f40 67 20 6c 65 76 65 6c 2e 0a 2a 2a 0a 2a 2a 20 57  g level..**.** W
d5f50 69 74 68 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b  ith dotfile lock
d5f60 69 6e 67 2c 20 77 65 20 72 65 61 6c 6c 79 20 6f  ing, we really o
d5f70 6e 6c 79 20 73 75 70 70 6f 72 74 20 73 74 61 74  nly support stat
d5f80 65 20 28 34 29 3a 20 45 58 43 4c 55 53 49 56 45  e (4): EXCLUSIVE
d5f90 2e 0a 2a 2a 20 42 75 74 20 77 65 20 74 72 61 63  ..** But we trac
d5fa0 6b 20 74 68 65 20 6f 74 68 65 72 20 6c 6f 63 6b  k the other lock
d5fb0 69 6e 67 20 6c 65 76 65 6c 73 20 69 6e 74 65 72  ing levels inter
d5fc0 6e 61 6c 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  nally..*/.static
d5fd0 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b   int dotlockLock
d5fe0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
d5ff0 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29  d, int locktype)
d6000 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70   {.  unixFile *p
d6010 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
d6020 2a 29 69 64 3b 0a 20 20 69 6e 74 20 66 64 3b 0a  *)id;.  int fd;.
d6030 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c    char *zLockFil
d6040 65 20 3d 20 28 63 68 61 72 20 2a 29 70 46 69 6c  e = (char *)pFil
d6050 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
d6060 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  t;.  int rc = SQ
d6070 4c 49 54 45 5f 4f 4b 3b 0a 0a 0a 20 20 2f 2a 20  LITE_OK;...  /* 
d6080 49 66 20 77 65 20 68 61 76 65 20 61 6e 79 20 6c  If we have any l
d6090 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 6c 6f  ock, then the lo
d60a0 63 6b 20 66 69 6c 65 20 61 6c 72 65 61 64 79 20  ck file already 
d60b0 65 78 69 73 74 73 2e 20 20 41 6c 6c 20 77 65 20  exists.  All we 
d60c0 68 61 76 65 0a 20 20 2a 2a 20 74 6f 20 64 6f 20  have.  ** to do 
d60d0 69 73 20 61 64 6a 75 73 74 20 6f 75 72 20 69 6e  is adjust our in
d60e0 74 65 72 6e 61 6c 20 72 65 63 6f 72 64 20 6f 66  ternal record of
d60f0 20 74 68 65 20 6c 6f 63 6b 20 6c 65 76 65 6c 2e   the lock level.
d6100 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c  .  */.  if( pFil
d6110 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f  e->locktype > NO
d6120 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 69  _LOCK ){.    pFi
d6130 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c  le->locktype = l
d6140 6f 63 6b 74 79 70 65 3b 0a 23 69 66 20 21 4f 53  ocktype;.#if !OS
d6150 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 2f 2a 20  _VXWORKS.    /* 
d6160 41 6c 77 61 79 73 20 75 70 64 61 74 65 20 74 68  Always update th
d6170 65 20 74 69 6d 65 73 74 61 6d 70 20 6f 6e 20 74  e timestamp on t
d6180 68 65 20 6f 6c 64 20 66 69 6c 65 20 2a 2f 0a 20  he old file */. 
d6190 20 20 20 75 74 69 6d 65 73 28 7a 4c 6f 63 6b 46     utimes(zLockF
d61a0 69 6c 65 2c 20 4e 55 4c 4c 29 3b 0a 23 65 6e 64  ile, NULL);.#end
d61b0 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  if.    return SQ
d61c0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a  LITE_OK;.  }.  .
d61d0 20 20 2f 2a 20 67 72 61 62 20 61 6e 20 65 78 63    /* grab an exc
d61e0 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20  lusive lock */. 
d61f0 20 66 64 20 3d 20 6f 70 65 6e 28 7a 4c 6f 63 6b   fd = open(zLock
d6200 46 69 6c 65 2c 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f  File,O_RDONLY|O_
d6210 43 52 45 41 54 7c 4f 5f 45 58 43 4c 2c 30 36 30  CREAT|O_EXCL,060
d6220 30 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 29  0);.  if( fd<0 )
d6230 7b 0a 20 20 20 20 2f 2a 20 66 61 69 6c 65 64 20  {.    /* failed 
d6240 74 6f 20 6f 70 65 6e 2f 63 72 65 61 74 65 20 74  to open/create t
d6250 68 65 20 66 69 6c 65 2c 20 73 6f 6d 65 6f 6e 65  he file, someone
d6260 20 65 6c 73 65 20 6d 61 79 20 68 61 76 65 20 73   else may have s
d6270 74 6f 6c 65 6e 20 74 68 65 20 6c 6f 63 6b 20 2a  tolen the lock *
d6280 2f 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f  /.    int tErrno
d6290 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66   = errno;.    if
d62a0 28 20 45 45 58 49 53 54 20 3d 3d 20 74 45 72 72  ( EEXIST == tErr
d62b0 6e 6f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  no ){.      rc =
d62c0 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
d62d0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20    } else {.     
d62e0 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f   rc = sqliteErro
d62f0 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28  rFromPosixError(
d6300 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49  tErrno, SQLITE_I
d6310 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  OERR_LOCK);.    
d6320 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
d6330 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20  ROR(rc) ){.     
d6340 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
d6350 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20  rno = tErrno;.  
d6360 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
d6370 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20 0a  return rc;.  } .
d6380 20 20 69 66 28 20 63 6c 6f 73 65 28 66 64 29 20    if( close(fd) 
d6390 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  ){.    pFile->la
d63a0 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b  stErrno = errno;
d63b0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
d63c0 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20  _IOERR_CLOSE;.  
d63d0 7d 0a 20 20 0a 20 20 2f 2a 20 67 6f 74 20 69 74  }.  .  /* got it
d63e0 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 20 61  , set the type a
d63f0 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a  nd return ok */.
d6400 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
d6410 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  e = locktype;.  
d6420 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
d6430 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f  .** Lower the lo
d6440 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66  cking level on f
d6450 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70  ile descriptor p
d6460 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65  File to locktype
d6470 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d  .  locktype.** m
d6480 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f  ust be either NO
d6490 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f  _LOCK or SHARED_
d64a0 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  LOCK..**.** If t
d64b0 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  he locking level
d64c0 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73   of the file des
d64d0 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61  criptor is alrea
d64e0 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a  dy at or below.*
d64f0 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  * the requested 
d6500 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74  locking level, t
d6510 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
d6520 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 57 68   no-op..**.** Wh
d6530 65 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  en the locking l
d6540 65 76 65 6c 20 72 65 61 63 68 65 73 20 4e 4f 5f  evel reaches NO_
d6550 4c 4f 43 4b 2c 20 64 65 6c 65 74 65 20 74 68 65  LOCK, delete the
d6560 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a 2a 2f 0a 73   lock file..*/.s
d6570 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63  tatic int dotloc
d6580 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  kUnlock(sqlite3_
d6590 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f  file *id, int lo
d65a0 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78  cktype) {.  unix
d65b0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
d65c0 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 63  nixFile*)id;.  c
d65d0 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d  har *zLockFile =
d65e0 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e   (char *)pFile->
d65f0 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a  lockingContext;.
d6600 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
d6610 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22   );.  OSTRACE5("
d6620 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61  UNLOCK  %d %d wa
d6630 73 20 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20  s %d pid=%d\n", 
d6640 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79  pFile->h, lockty
d6650 70 65 2c 0a 09 20 20 20 70 46 69 6c 65 2d 3e 6c  pe,..   pFile->l
d6660 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 64 28  ocktype, getpid(
d6670 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f  ));.  assert( lo
d6680 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c  cktype<=SHARED_L
d6690 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e  OCK );.  .  /* n
d66a0 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 6c 65  o-op if possible
d66b0 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d   */.  if( pFile-
d66c0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b 74  >locktype==lockt
d66d0 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ype ){.    retur
d66e0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
d66f0 0a 0a 20 20 2f 2a 20 54 6f 20 64 6f 77 6e 67 72  ..  /* To downgr
d6700 61 64 65 20 74 6f 20 73 68 61 72 65 64 2c 20 73  ade to shared, s
d6710 69 6d 70 6c 79 20 75 70 64 61 74 65 20 6f 75 72  imply update our
d6720 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 74 69 6f 6e   internal notion
d6730 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6c 6f 63   of the.  ** loc
d6740 6b 20 73 74 61 74 65 2e 20 20 4e 6f 20 6e 65 65  k state.  No nee
d6750 64 20 74 6f 20 6d 65 73 73 20 77 69 74 68 20 74  d to mess with t
d6760 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e  he file on disk.
d6770 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b  .  */.  if( lock
d6780 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
d6790 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  K ){.    pFile->
d67a0 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45  locktype = SHARE
d67b0 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 74 75  D_LOCK;.    retu
d67c0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
d67d0 7d 0a 20 20 0a 20 20 2f 2a 20 54 6f 20 66 75 6c  }.  .  /* To ful
d67e0 6c 79 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61  ly unlock the da
d67f0 74 61 62 61 73 65 2c 20 64 65 6c 65 74 65 20 74  tabase, delete t
d6800 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a  he lock file */.
d6810 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
d6820 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20  pe==NO_LOCK );. 
d6830 20 69 66 28 20 75 6e 6c 69 6e 6b 28 7a 4c 6f 63   if( unlink(zLoc
d6840 6b 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 69 6e  kFile) ){.    in
d6850 74 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 69 6e  t rc = 0;.    in
d6860 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  t tErrno = errno
d6870 3b 0a 20 20 20 20 69 66 28 20 45 4e 4f 45 4e 54  ;.    if( ENOENT
d6880 20 21 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20   != tErrno ){.  
d6890 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45      rc = sqliteE
d68a0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72  rrorFromPosixErr
d68b0 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54  or(tErrno, SQLIT
d68c0 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b  E_IOERR_UNLOCK);
d68d0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 49  .    }.    if( I
d68e0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29  S_LOCK_ERROR(rc)
d68f0 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d   ){.      pFile-
d6900 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72  >lastErrno = tEr
d6910 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  rno;.    }.    r
d6920 65 74 75 72 6e 20 72 63 3b 20 0a 20 20 7d 0a 20  eturn rc; .  }. 
d6930 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d6940 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 65   = NO_LOCK;.  re
d6950 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
d6960 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61  }../*.** Close a
d6970 20 66 69 6c 65 2e 20 20 4d 61 6b 65 20 73 75 72   file.  Make sur
d6980 65 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 62  e the lock has b
d6990 65 65 6e 20 72 65 6c 65 61 73 65 64 20 62 65 66  een released bef
d69a0 6f 72 65 20 63 6c 6f 73 69 6e 67 2e 0a 2a 2f 0a  ore closing..*/.
d69b0 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f  static int dotlo
d69c0 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  ckClose(sqlite3_
d69d0 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 6e  file *id) {.  in
d69e0 74 20 72 63 3b 0a 20 20 69 66 28 20 69 64 20 29  t rc;.  if( id )
d69f0 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a  {.    unixFile *
d6a00 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
d6a10 65 2a 29 69 64 3b 0a 20 20 20 20 64 6f 74 6c 6f  e*)id;.    dotlo
d6a20 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f  ckUnlock(id, NO_
d6a30 4c 4f 43 4b 29 3b 0a 20 20 20 20 73 71 6c 69 74  LOCK);.    sqlit
d6a40 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e 6c  e3_free(pFile->l
d6a50 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b 0a  ockingContext);.
d6a60 20 20 7d 0a 20 20 72 63 20 3d 20 63 6c 6f 73 65    }.  rc = close
d6a70 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 20 20  UnixFile(id);.  
d6a80 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 2f 2a 2a  return rc;.}./**
d6a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6aa0 20 45 6e 64 20 6f 66 20 74 68 65 20 64 6f 74 2d   End of the dot-
d6ab0 66 69 6c 65 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d  file lock implem
d6ac0 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a  entation *******
d6ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a  ************.***
d6ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
d6b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
d6b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6b90 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
d6ba0 6c 6f 63 6b 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a  lock Locking ***
d6bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
d6bd0 0a 2a 2a 20 55 73 65 20 74 68 65 20 66 6c 6f 63  .** Use the floc
d6be0 6b 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 20  k() system call 
d6bf0 74 6f 20 64 6f 20 66 69 6c 65 20 6c 6f 63 6b 69  to do file locki
d6c00 6e 67 2e 0a 2a 2a 0a 2a 2a 20 66 6c 6f 63 6b 28  ng..**.** flock(
d6c10 29 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 69 6b  ) locking is lik
d6c20 65 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69  e dot-file locki
d6c30 6e 67 20 69 6e 20 74 68 61 74 20 74 68 65 20 76  ng in that the v
d6c40 61 72 69 6f 75 73 0a 2a 2a 20 66 69 6e 65 2d 67  arious.** fine-g
d6c50 72 61 69 6e 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  rain locking lev
d6c60 65 6c 73 20 73 75 70 70 6f 72 74 65 64 20 62 79  els supported by
d6c70 20 53 51 4c 69 74 65 20 61 72 65 20 63 6f 6c 6c   SQLite are coll
d6c80 61 70 73 65 64 20 69 6e 74 6f 0a 2a 2a 20 61 20  apsed into.** a 
d6c90 73 69 6e 67 6c 65 20 65 78 63 6c 75 73 69 76 65  single exclusive
d6ca0 20 6c 6f 63 6b 2e 20 20 49 6e 20 6f 74 68 65 72   lock.  In other
d6cb0 20 77 6f 72 64 73 2c 20 53 48 41 52 45 44 2c 20   words, SHARED, 
d6cc0 52 45 53 45 52 56 45 44 2c 20 61 6e 64 0a 2a 2a  RESERVED, and.**
d6cd0 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 73 20 61   PENDING locks a
d6ce0 72 65 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e  re the same thin
d6cf0 67 20 61 73 20 61 6e 20 45 58 43 4c 55 53 49 56  g as an EXCLUSIV
d6d00 45 20 6c 6f 63 6b 2e 20 20 53 51 4c 69 74 65 0a  E lock.  SQLite.
d6d10 2a 2a 20 73 74 69 6c 6c 20 77 6f 72 6b 73 20 77  ** still works w
d6d20 68 65 6e 20 79 6f 75 20 64 6f 20 74 68 69 73 2c  hen you do this,
d6d30 20 62 75 74 20 63 6f 6e 63 75 72 72 65 6e 63 79   but concurrency
d6d40 20 69 73 20 72 65 64 75 63 65 64 20 73 69 6e 63   is reduced sinc
d6d50 65 0a 2a 2a 20 6f 6e 6c 79 20 61 20 73 69 6e 67  e.** only a sing
d6d60 6c 65 20 70 72 6f 63 65 73 73 20 63 61 6e 20 62  le process can b
d6d70 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61  e reading the da
d6d80 74 61 62 61 73 65 20 61 74 20 61 20 74 69 6d 65  tabase at a time
d6d90 2e 0a 2a 2a 0a 2a 2a 20 4f 6d 69 74 20 74 68 69  ..**.** Omit thi
d6da0 73 20 73 65 63 74 69 6f 6e 20 69 66 20 53 51 4c  s section if SQL
d6db0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
d6dc0 4e 47 5f 53 54 59 4c 45 20 69 73 20 74 75 72 6e  NG_STYLE is turn
d6dd0 65 64 20 6f 66 66 20 6f 72 20 69 66 0a 2a 2a 20  ed off or if.** 
d6de0 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 56 58  compiling for VX
d6df0 57 4f 52 4b 53 2e 0a 2a 2f 0a 23 69 66 20 53 51  WORKS..*/.#if SQ
d6e00 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
d6e10 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 21 4f 53  ING_STYLE && !OS
d6e20 5f 56 58 57 4f 52 4b 53 0a 0a 2f 2a 0a 2a 2a 20  _VXWORKS../*.** 
d6e30 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65  This routine che
d6e40 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20  cks if there is 
d6e50 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  a RESERVED lock 
d6e60 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63  held on the spec
d6e70 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79  ified.** file by
d6e80 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68   this or any oth
d6e90 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73  er process. If s
d6ea0 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65  uch a lock is he
d6eb0 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74  ld, set *pResOut
d6ec0 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72  .** to a non-zer
d6ed0 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73  o value otherwis
d6ee0 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65  e *pResOut is se
d6ef0 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20  t to zero.  The 
d6f00 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20  return value.** 
d6f10 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45  is set to SQLITE
d6f20 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f  _OK unless an I/
d6f30 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64  O error occurs d
d6f40 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b  uring lock check
d6f50 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ing..*/.static i
d6f60 6e 74 20 66 6c 6f 63 6b 43 68 65 63 6b 52 65 73  nt flockCheckRes
d6f70 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65  ervedLock(sqlite
d6f80 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
d6f90 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74  *pResOut){.  int
d6fa0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
d6fb0 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 20  .  int reserved 
d6fc0 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20  = 0;.  unixFile 
d6fd0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
d6fe0 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 53 69 6d  le*)id;.  .  Sim
d6ff0 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65  ulateIOError( re
d7000 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
d7010 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c  R_CHECKRESERVEDL
d7020 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73  OCK; );.  .  ass
d7030 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20  ert( pFile );.  
d7040 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61  .  /* Check if a
d7050 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20   thread in this 
d7060 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75  process holds su
d7070 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69  ch a lock */.  i
d7080 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
d7090 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  pe>SHARED_LOCK )
d70a0 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d  {.    reserved =
d70b0 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20   1;.  }.  .  /* 
d70c0 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69 66  Otherwise see if
d70d0 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63   some other proc
d70e0 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 20 2a 2f  ess holds it. */
d70f0 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65 64  .  if( !reserved
d7100 20 29 7b 0a 20 20 20 20 2f 2a 20 61 74 74 65 6d   ){.    /* attem
d7110 70 74 20 74 6f 20 67 65 74 20 74 68 65 20 6c 6f  pt to get the lo
d7120 63 6b 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72  ck */.    int lr
d7130 63 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65 2d  c = flock(pFile-
d7140 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20 7c 20 4c 4f  >h, LOCK_EX | LO
d7150 43 4b 5f 4e 42 29 3b 0a 20 20 20 20 69 66 28 20  CK_NB);.    if( 
d7160 21 6c 72 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a  !lrc ){.      /*
d7170 20 67 6f 74 20 74 68 65 20 6c 6f 63 6b 2c 20 75   got the lock, u
d7180 6e 6c 6f 63 6b 20 69 74 20 2a 2f 0a 20 20 20 20  nlock it */.    
d7190 20 20 6c 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46    lrc = flock(pF
d71a0 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 4e 29  ile->h, LOCK_UN)
d71b0 3b 0a 20 20 20 20 20 20 69 66 20 28 20 6c 72 63  ;.      if ( lrc
d71c0 20 29 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74   ) {.        int
d71d0 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b   tErrno = errno;
d71e0 0a 20 20 20 20 20 20 20 20 2f 2a 20 75 6e 6c 6f  .        /* unlo
d71f0 63 6b 20 66 61 69 6c 65 64 20 77 69 74 68 20 61  ck failed with a
d7200 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20  n error */.     
d7210 20 20 20 6c 72 63 20 3d 20 73 71 6c 69 74 65 45     lrc = sqliteE
d7220 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72  rrorFromPosixErr
d7230 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54  or(tErrno, SQLIT
d7240 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b  E_IOERR_UNLOCK);
d7250 20 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 53   .        if( IS
d7260 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29  _LOCK_ERROR(lrc)
d7270 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46   ){.          pF
d7280 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
d7290 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20   tErrno;.       
d72a0 20 20 20 72 63 20 3d 20 6c 72 63 3b 0a 20 20 20     rc = lrc;.   
d72b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
d72c0 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20     } else {.    
d72d0 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65    int tErrno = e
d72e0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 73 65  rrno;.      rese
d72f0 72 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  rved = 1;.      
d7300 2f 2a 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20  /* someone else 
d7310 6d 69 67 68 74 20 68 61 76 65 20 69 74 20 72 65  might have it re
d7320 73 65 72 76 65 64 20 2a 2f 0a 20 20 20 20 20 20  served */.      
d7330 6c 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f  lrc = sqliteErro
d7340 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28  rFromPosixError(
d7350 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49  tErrno, SQLITE_I
d7360 4f 45 52 52 5f 4c 4f 43 4b 29 3b 20 0a 20 20 20  OERR_LOCK); .   
d7370 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45     if( IS_LOCK_E
d7380 52 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20  RROR(lrc) ){.   
d7390 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74       pFile->last
d73a0 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a  Errno = tErrno;.
d73b0 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63          rc = lrc
d73c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
d73d0 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22    }.  OSTRACE4("
d73e0 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20  TEST WR-LOCK %d 
d73f0 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  %d %d\n", pFile-
d7400 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64  >h, rc, reserved
d7410 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  );..#ifdef SQLIT
d7420 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c  E_IGNORE_FLOCK_L
d7430 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 69 66 28  OCK_ERRORS.  if(
d7440 20 28 72 63 20 26 20 53 51 4c 49 54 45 5f 49 4f   (rc & SQLITE_IO
d7450 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49  ERR) == SQLITE_I
d7460 4f 45 52 52 20 29 7b 0a 20 20 20 20 72 63 20 3d  OERR ){.    rc =
d7470 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
d7480 72 65 73 65 72 76 65 64 3d 31 3b 0a 20 20 7d 0a  reserved=1;.  }.
d7490 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
d74a0 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f  _IGNORE_FLOCK_LO
d74b0 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 2a  CK_ERRORS */.  *
d74c0 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76  pResOut = reserv
d74d0 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ed;.  return rc;
d74e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74  .}../*.** Lock t
d74f0 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65  he file with the
d7500 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20   lock specified 
d7510 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63  by parameter loc
d7520 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f  ktype - one.** o
d7530 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  f the following:
d7540 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53  .**.**     (1) S
d7550 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  HARED_LOCK.**   
d7560 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c    (2) RESERVED_L
d7570 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50  OCK.**     (3) P
d7580 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20  ENDING_LOCK.**  
d7590 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45     (4) EXCLUSIVE
d75a0 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65  _LOCK.**.** Some
d75b0 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65  times when reque
d75c0 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73  sting one lock s
d75d0 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c  tate, additional
d75e0 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20   lock states.** 
d75f0 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20  are inserted in 
d7600 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f  between.  The lo
d7610 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c  cking might fail
d7620 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c   on one of the l
d7630 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69  ater.** transiti
d7640 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20  ons leaving the 
d7650 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65  lock state diffe
d7660 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69  rent from what i
d7670 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a  t started but.**
d7680 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20   still short of 
d7690 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66  its goal.  The f
d76a0 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73  ollowing chart s
d76b0 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64  hows the allowed
d76c0 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  .** transitions 
d76d0 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64  and the inserted
d76e0 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74   intermediate st
d76f0 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55  ates:.**.**    U
d7700 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45  NLOCKED -> SHARE
d7710 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  D.**    SHARED -
d7720 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20  > RESERVED.**   
d7730 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44   SHARED -> (PEND
d7740 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56  ING) -> EXCLUSIV
d7750 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44  E.**    RESERVED
d7760 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
d7770 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
d7780 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c   PENDING -> EXCL
d7790 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 66 6c 6f 63  USIVE.**.** floc
d77a0 6b 28 29 20 6f 6e 6c 79 20 72 65 61 6c 6c 79 20  k() only really 
d77b0 73 75 70 70 6f 72 74 20 45 58 43 4c 55 53 49 56  support EXCLUSIV
d77c0 45 20 6c 6f 63 6b 73 2e 20 20 57 65 20 74 72 61  E locks.  We tra
d77d0 63 6b 20 69 6e 74 65 72 6d 65 64 69 61 74 65 0a  ck intermediate.
d77e0 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 65 73 20 69  ** lock states i
d77f0 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69  n the sqlite3_fi
d7800 6c 65 20 73 74 72 75 63 74 75 72 65 2c 20 62 75  le structure, bu
d7810 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 53 48 41 52  t all locks SHAR
d7820 45 44 20 6f 72 0a 2a 2a 20 61 62 6f 76 65 20 61  ED or.** above a
d7830 72 65 20 72 65 61 6c 6c 79 20 45 58 43 4c 55 53  re really EXCLUS
d7840 49 56 45 20 6c 6f 63 6b 73 20 61 6e 64 20 65 78  IVE locks and ex
d7850 63 6c 75 64 65 20 61 6c 6c 20 6f 74 68 65 72 20  clude all other 
d7860 70 72 6f 63 65 73 73 65 73 20 66 72 6f 6d 0a 2a  processes from.*
d7870 2a 20 61 63 63 65 73 73 20 74 68 65 20 66 69 6c  * access the fil
d7880 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  e..**.** This ro
d7890 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20  utine will only 
d78a0 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e  increase a lock.
d78b0 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 65    Use the sqlite
d78c0 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72  3OsUnlock().** r
d78d0 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20  outine to lower 
d78e0 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e  a locking level.
d78f0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
d7900 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33  lockLock(sqlite3
d7910 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c  _file *id, int l
d7920 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 69 6e 74  ocktype) {.  int
d7930 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
d7940 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
d7950 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
d7960 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  id;..  assert( p
d7970 46 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 69 66  File );..  /* if
d7980 20 77 65 20 61 6c 72 65 61 64 79 20 68 61 76 65   we already have
d7990 20 61 20 6c 6f 63 6b 2c 20 69 74 20 69 73 20 65   a lock, it is e
d79a0 78 63 6c 75 73 69 76 65 2e 20 20 0a 20 20 2a 2a  xclusive.  .  **
d79b0 20 4a 75 73 74 20 61 64 6a 75 73 74 20 6c 65 76   Just adjust lev
d79c0 65 6c 20 61 6e 64 20 70 75 6e 74 20 6f 6e 20 6f  el and punt on o
d79d0 75 74 74 61 20 68 65 72 65 2e 20 2a 2f 0a 20 20  utta here. */.  
d79e0 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if (pFile->lockt
d79f0 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 29 20 7b  ype > NO_LOCK) {
d7a00 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  .    pFile->lock
d7a10 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b  type = locktype;
d7a20 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
d7a30 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20  TE_OK;.  }.  .  
d7a40 2f 2a 20 67 72 61 62 20 61 6e 20 65 78 63 6c 75  /* grab an exclu
d7a50 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 0a  sive lock */.  .
d7a60 20 20 69 66 20 28 66 6c 6f 63 6b 28 70 46 69 6c    if (flock(pFil
d7a70 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20 7c 20  e->h, LOCK_EX | 
d7a80 4c 4f 43 4b 5f 4e 42 29 29 20 7b 0a 20 20 20 20  LOCK_NB)) {.    
d7a90 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72  int tErrno = err
d7aa0 6e 6f 3b 0a 20 20 20 20 2f 2a 20 64 69 64 6e 27  no;.    /* didn'
d7ab0 74 20 67 65 74 2c 20 6d 75 73 74 20 62 65 20 62  t get, must be b
d7ac0 75 73 79 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  usy */.    rc = 
d7ad0 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50  sqliteErrorFromP
d7ae0 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f  osixError(tErrno
d7af0 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c  , SQLITE_IOERR_L
d7b00 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53  OCK);.    if( IS
d7b10 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20  _LOCK_ERROR(rc) 
d7b20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  ){.      pFile->
d7b30 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72  lastErrno = tErr
d7b40 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c  no;.    }.  } el
d7b50 73 65 20 7b 0a 20 20 20 20 2f 2a 20 67 6f 74 20  se {.    /* got 
d7b60 69 74 2c 20 73 65 74 20 74 68 65 20 74 79 70 65  it, set the type
d7b70 20 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a   and return ok *
d7b80 2f 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  /.    pFile->loc
d7b90 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65  ktype = locktype
d7ba0 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34  ;.  }.  OSTRACE4
d7bb0 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20  ("LOCK    %d %s 
d7bc0 25 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %s\n", pFile->h,
d7bd0 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f   locktypeName(lo
d7be0 63 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 20 20  cktype), .      
d7bf0 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f       rc==SQLITE_
d7c00 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69  OK ? "ok" : "fai
d7c10 6c 65 64 22 29 3b 0a 23 69 66 64 65 66 20 53 51  led");.#ifdef SQ
d7c20 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43  LITE_IGNORE_FLOC
d7c30 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20  K_LOCK_ERRORS.  
d7c40 69 66 28 20 28 72 63 20 26 20 53 51 4c 49 54 45  if( (rc & SQLITE
d7c50 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 54  _IOERR) == SQLIT
d7c60 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 72  E_IOERR ){.    r
d7c70 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  c = SQLITE_BUSY;
d7c80 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
d7c90 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f  QLITE_IGNORE_FLO
d7ca0 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a  CK_LOCK_ERRORS *
d7cb0 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  /.  return rc;.}
d7cc0 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74  .../*.** Lower t
d7cd0 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  he locking level
d7ce0 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70   on file descrip
d7cf0 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63  tor pFile to loc
d7d00 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65  ktype.  locktype
d7d10 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68  .** must be eith
d7d20 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48  er NO_LOCK or SH
d7d30 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a  ARED_LOCK..**.**
d7d40 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20   If the locking 
d7d50 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c  level of the fil
d7d60 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20  e descriptor is 
d7d70 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65  already at or be
d7d80 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65  low.** the reque
d7d90 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  sted locking lev
d7da0 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  el, this routine
d7db0 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a   is a no-op..*/.
d7dc0 73 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b  static int flock
d7dd0 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  Unlock(sqlite3_f
d7de0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63  ile *id, int loc
d7df0 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46  ktype) {.  unixF
d7e00 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
d7e10 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20  ixFile*)id;.  . 
d7e20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
d7e30 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e  ;.  OSTRACE5("UN
d7e40 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20  LOCK  %d %d was 
d7e50 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46  %d pid=%d\n", pF
d7e60 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65  ile->h, locktype
d7e70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 46 69  ,.           pFi
d7e80 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65  le->locktype, ge
d7e90 74 70 69 64 28 29 29 3b 0a 20 20 61 73 73 65 72  tpid());.  asser
d7ea0 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41  t( locktype<=SHA
d7eb0 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20  RED_LOCK );.  . 
d7ec0 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73   /* no-op if pos
d7ed0 73 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70  sible */.  if( p
d7ee0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  File->locktype==
d7ef0 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20  locktype ){.    
d7f00 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d7f10 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 68  ;.  }.  .  /* sh
d7f20 61 72 65 64 20 63 61 6e 20 6a 75 73 74 20 62 65  ared can just be
d7f30 20 73 65 74 20 62 65 63 61 75 73 65 20 77 65 20   set because we 
d7f40 61 6c 77 61 79 73 20 68 61 76 65 20 61 6e 20 65  always have an e
d7f50 78 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69 66  xclusive */.  if
d7f60 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52   (locktype==SHAR
d7f70 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70  ED_LOCK) {.    p
d7f80 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
d7f90 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72   locktype;.    r
d7fa0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
d7fb0 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2c  .  }.  .  /* no,
d7fc0 20 72 65 61 6c 6c 79 2c 20 75 6e 6c 6f 63 6b 2e   really, unlock.
d7fd0 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 66   */.  int rc = f
d7fe0 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c  lock(pFile->h, L
d7ff0 4f 43 4b 5f 55 4e 29 3b 0a 20 20 69 66 20 28 72  OCK_UN);.  if (r
d8000 63 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 2c 20  c) {.    int r, 
d8010 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
d8020 20 20 20 20 72 20 3d 20 73 71 6c 69 74 65 45 72      r = sqliteEr
d8030 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f  rorFromPosixErro
d8040 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45  r(tErrno, SQLITE
d8050 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a  _IOERR_UNLOCK);.
d8060 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f      if( IS_LOCK_
d8070 45 52 52 4f 52 28 72 29 20 29 7b 0a 20 20 20 20  ERROR(r) ){.    
d8080 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
d8090 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
d80a0 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45   }.#ifdef SQLITE
d80b0 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f  _IGNORE_FLOCK_LO
d80c0 43 4b 5f 45 52 52 4f 52 53 0a 20 20 20 20 69 66  CK_ERRORS.    if
d80d0 28 20 28 72 20 26 20 53 51 4c 49 54 45 5f 49 4f  ( (r & SQLITE_IO
d80e0 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49  ERR) == SQLITE_I
d80f0 4f 45 52 52 20 29 7b 0a 20 20 20 20 20 20 72 20  OERR ){.      r 
d8100 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  = SQLITE_BUSY;. 
d8110 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53     }.#endif /* S
d8120 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f  QLITE_IGNORE_FLO
d8130 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a  CK_LOCK_ERRORS *
d8140 2f 0a 20 20 20 20 0a 20 20 20 20 72 65 74 75 72  /.    .    retur
d8150 6e 20 72 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a  n r;.  } else {.
d8160 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
d8170 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20  ype = NO_LOCK;. 
d8180 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
d8190 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  _OK;.  }.}../*.*
d81a0 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a  * Close a file..
d81b0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c  */.static int fl
d81c0 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  ockClose(sqlite3
d81d0 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69  _file *id) {.  i
d81e0 66 28 20 69 64 20 29 7b 0a 20 20 20 20 66 6c 6f  f( id ){.    flo
d81f0 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f  ckUnlock(id, NO_
d8200 4c 4f 43 4b 29 3b 0a 20 20 7d 0a 20 20 72 65 74  LOCK);.  }.  ret
d8210 75 72 6e 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c  urn closeUnixFil
d8220 65 28 69 64 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66  e(id);.}..#endif
d8230 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   /* SQLITE_ENABL
d8240 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
d8250 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b 20 2a 2f  && !OS_VXWORK */
d8260 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
d8270 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68  ****** End of th
d8280 65 20 66 6c 6f 63 6b 20 6c 6f 63 6b 20 69 6d 70  e flock lock imp
d8290 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a  lementation ****
d82a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d82b0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
d82c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d82d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d82e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d82f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8300 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
d8310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8350 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
d8360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
d8370 6e 20 4e 61 6d 65 64 20 53 65 6d 61 70 68 6f 72  n Named Semaphor
d8380 65 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a  e Locking ******
d8390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d83a0 2a 2a 0a 2a 2a 0a 2a 2a 20 4e 61 6d 65 64 20 73  **.**.** Named s
d83b0 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67  emaphore locking
d83c0 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74   is only support
d83d0 65 64 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a  ed on VxWorks..*
d83e0 2a 0a 2a 2a 20 53 65 6d 61 70 68 6f 72 65 20 6c  *.** Semaphore l
d83f0 6f 63 6b 69 6e 67 20 69 73 20 6c 69 6b 65 20 64  ocking is like d
d8400 6f 74 2d 6c 6f 63 6b 20 61 6e 64 20 66 6c 6f 63  ot-lock and floc
d8410 6b 20 69 6e 20 74 68 61 74 20 69 74 20 72 65 61  k in that it rea
d8420 6c 6c 79 20 6f 6e 6c 79 0a 2a 2a 20 73 75 70 70  lly only.** supp
d8430 6f 72 74 73 20 45 58 43 4c 55 53 49 56 45 20 6c  orts EXCLUSIVE l
d8440 6f 63 6b 69 6e 67 2e 20 20 4f 6e 6c 79 20 61 20  ocking.  Only a 
d8450 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 63  single process c
d8460 61 6e 20 72 65 61 64 20 6f 72 20 77 72 69 74 65  an read or write
d8470 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
d8480 20 66 69 6c 65 20 61 74 20 61 20 74 69 6d 65 2e   file at a time.
d8490 20 20 54 68 69 73 20 72 65 64 75 63 65 73 20 70    This reduces p
d84a0 6f 74 65 6e 74 69 61 6c 20 63 6f 6e 63 75 72 72  otential concurr
d84b0 65 6e 63 79 2c 20 62 75 74 0a 2a 2a 20 6d 61 6b  ency, but.** mak
d84c0 65 73 20 74 68 65 20 6c 6f 63 6b 20 69 6d 70 6c  es the lock impl
d84d0 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 63 68 20  ementation much 
d84e0 65 61 73 69 65 72 2e 0a 2a 2f 0a 23 69 66 20 4f  easier..*/.#if O
d84f0 53 5f 56 58 57 4f 52 4b 53 0a 0a 2f 2a 0a 2a 2a  S_VXWORKS../*.**
d8500 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68   This routine ch
d8510 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73  ecks if there is
d8520 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   a RESERVED lock
d8530 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65   held on the spe
d8540 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62  cified.** file b
d8550 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74  y this or any ot
d8560 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20  her process. If 
d8570 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68  such a lock is h
d8580 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75  eld, set *pResOu
d8590 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65  t.** to a non-ze
d85a0 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69  ro value otherwi
d85b0 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73  se *pResOut is s
d85c0 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65  et to zero.  The
d85d0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a   return value.**
d85e0 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54   is set to SQLIT
d85f0 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49  E_OK unless an I
d8600 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  /O error occurs 
d8610 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63  during lock chec
d8620 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  king..*/.static 
d8630 69 6e 74 20 73 65 6d 43 68 65 63 6b 52 65 73 65  int semCheckRese
d8640 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33  rvedLock(sqlite3
d8650 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a  _file *id, int *
d8660 70 52 65 73 4f 75 74 29 20 7b 0a 20 20 69 6e 74  pResOut) {.  int
d8670 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
d8680 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 20  .  int reserved 
d8690 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20  = 0;.  unixFile 
d86a0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
d86b0 6c 65 2a 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c  le*)id;..  Simul
d86c0 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75  ateIOError( retu
d86d0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
d86e0 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43  CHECKRESERVEDLOC
d86f0 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 65 72  K; );.  .  asser
d8700 74 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 20 2f  t( pFile );..  /
d8710 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72  * Check if a thr
d8720 65 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63  ead in this proc
d8730 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61  ess holds such a
d8740 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70   lock */.  if( p
d8750 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53  File->locktype>S
d8760 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20  HARED_LOCK ){.  
d8770 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a    reserved = 1;.
d8780 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 65    }.  .  /* Othe
d8790 72 77 69 73 65 20 73 65 65 20 69 66 20 73 6f 6d  rwise see if som
d87a0 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20  e other process 
d87b0 68 6f 6c 64 73 20 69 74 2e 20 2a 2f 0a 20 20 69  holds it. */.  i
d87c0 66 28 20 21 72 65 73 65 72 76 65 64 20 29 7b 0a  f( !reserved ){.
d87d0 20 20 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20      sem_t *pSem 
d87e0 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e  = pFile->pOpen->
d87f0 70 53 65 6d 3b 0a 20 20 20 20 73 74 72 75 63 74  pSem;.    struct
d8800 20 73 74 61 74 20 73 74 61 74 42 75 66 3b 0a 0a   stat statBuf;..
d8810 20 20 20 20 69 66 28 20 73 65 6d 5f 74 72 79 77      if( sem_tryw
d8820 61 69 74 28 70 53 65 6d 29 3d 3d 2d 31 20 29 7b  ait(pSem)==-1 ){
d8830 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e  .      int tErrn
d8840 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20  o = errno;.     
d8850 20 69 66 28 20 45 41 47 41 49 4e 20 21 3d 20 74   if( EAGAIN != t
d8860 45 72 72 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20  Errno ){.       
d8870 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f   rc = sqliteErro
d8880 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28  rFromPosixError(
d8890 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49  tErrno, SQLITE_I
d88a0 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56  OERR_CHECKRESERV
d88b0 45 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20  EDLOCK);.       
d88c0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
d88d0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20  o = tErrno;.    
d88e0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20    } else {.     
d88f0 20 20 20 2f 2a 20 73 6f 6d 65 6f 6e 65 20 65 6c     /* someone el
d8900 73 65 20 68 61 73 20 74 68 65 20 6c 6f 63 6b 20  se has the lock 
d8910 77 68 65 6e 20 77 65 20 61 72 65 20 69 6e 20 4e  when we are in N
d8920 4f 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 20 20 20 20  O_LOCK */.      
d8930 20 20 72 65 73 65 72 76 65 64 20 3d 20 28 70 46    reserved = (pF
d8940 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c 20  ile->locktype < 
d8950 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20  SHARED_LOCK);.  
d8960 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
d8970 0a 20 20 20 20 20 20 2f 2a 20 77 65 20 63 6f 75  .      /* we cou
d8980 6c 64 20 68 61 76 65 20 69 74 20 69 66 20 77 65  ld have it if we
d8990 20 77 61 6e 74 20 69 74 20 2a 2f 0a 20 20 20 20   want it */.    
d89a0 20 20 73 65 6d 5f 70 6f 73 74 28 70 53 65 6d 29    sem_post(pSem)
d89b0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 4f 53  ;.    }.  }.  OS
d89c0 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d  TRACE4("TEST WR-
d89d0 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22  LOCK %d %d %d\n"
d89e0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20  , pFile->h, rc, 
d89f0 72 65 73 65 72 76 65 64 29 3b 0a 0a 20 20 2a 70  reserved);..  *p
d8a00 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65  ResOut = reserve
d8a10 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  d;.  return rc;.
d8a20 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68  }../*.** Lock th
d8a30 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20  e file with the 
d8a40 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62  lock specified b
d8a50 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b  y parameter lock
d8a60 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66  type - one.** of
d8a70 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
d8a80 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48  **.**     (1) SH
d8a90 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  ARED_LOCK.**    
d8aa0 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f   (2) RESERVED_LO
d8ab0 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45  CK.**     (3) PE
d8ac0 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  NDING_LOCK.**   
d8ad0 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f    (4) EXCLUSIVE_
d8ae0 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74  LOCK.**.** Somet
d8af0 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73  imes when reques
d8b00 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74  ting one lock st
d8b10 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20  ate, additional 
d8b20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61  lock states.** a
d8b30 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62  re inserted in b
d8b40 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63  etween.  The loc
d8b50 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20  king might fail 
d8b60 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61  on one of the la
d8b70 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  ter.** transitio
d8b80 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c  ns leaving the l
d8b90 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72  ock state differ
d8ba0 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74  ent from what it
d8bb0 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20   started but.** 
d8bc0 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69  still short of i
d8bd0 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f  ts goal.  The fo
d8be0 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68  llowing chart sh
d8bf0 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a  ows the allowed.
d8c00 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61  ** transitions a
d8c10 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20  nd the inserted 
d8c20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61  intermediate sta
d8c30 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e  tes:.**.**    UN
d8c40 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44  LOCKED -> SHARED
d8c50 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e  .**    SHARED ->
d8c60 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20   RESERVED.**    
d8c70 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49  SHARED -> (PENDI
d8c80 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  NG) -> EXCLUSIVE
d8c90 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20  .**    RESERVED 
d8ca0 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20  -> (PENDING) -> 
d8cb0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20  EXCLUSIVE.**    
d8cc0 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55  PENDING -> EXCLU
d8cd0 53 49 56 45 0a 2a 2a 0a 2a 2a 20 53 65 6d 61 70  SIVE.**.** Semap
d8ce0 68 6f 72 65 20 6c 6f 63 6b 73 20 6f 6e 6c 79 20  hore locks only 
d8cf0 72 65 61 6c 6c 79 20 73 75 70 70 6f 72 74 20 45  really support E
d8d00 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 2e 20  XCLUSIVE locks. 
d8d10 20 57 65 20 74 72 61 63 6b 20 69 6e 74 65 72 6d   We track interm
d8d20 65 64 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b 20 73  ediate.** lock s
d8d30 74 61 74 65 73 20 69 6e 20 74 68 65 20 73 71 6c  tates in the sql
d8d40 69 74 65 33 5f 66 69 6c 65 20 73 74 72 75 63 74  ite3_file struct
d8d50 75 72 65 2c 20 62 75 74 20 61 6c 6c 20 6c 6f 63  ure, but all loc
d8d60 6b 73 20 53 48 41 52 45 44 20 6f 72 0a 2a 2a 20  ks SHARED or.** 
d8d70 61 62 6f 76 65 20 61 72 65 20 72 65 61 6c 6c 79  above are really
d8d80 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73   EXCLUSIVE locks
d8d90 20 61 6e 64 20 65 78 63 6c 75 64 65 20 61 6c 6c   and exclude all
d8da0 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73   other processes
d8db0 20 66 72 6f 6d 0a 2a 2a 20 61 63 63 65 73 73 20   from.** access 
d8dc0 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  the file..**.** 
d8dd0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  This routine wil
d8de0 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20  l only increase 
d8df0 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65  a lock.  Use the
d8e00 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b   sqlite3OsUnlock
d8e10 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f  ().** routine to
d8e20 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67   lower a locking
d8e30 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69   level..*/.stati
d8e40 63 20 69 6e 74 20 73 65 6d 4c 6f 63 6b 28 73 71  c int semLock(sq
d8e50 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
d8e60 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a  int locktype) {.
d8e70 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
d8e80 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
d8e90 64 3b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 73  d;.  int fd;.  s
d8ea0 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69  em_t *pSem = pFi
d8eb0 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b  le->pOpen->pSem;
d8ec0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
d8ed0 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 69 66 20  TE_OK;..  /* if 
d8ee0 77 65 20 61 6c 72 65 61 64 79 20 68 61 76 65 20  we already have 
d8ef0 61 20 6c 6f 63 6b 2c 20 69 74 20 69 73 20 65 78  a lock, it is ex
d8f00 63 6c 75 73 69 76 65 2e 20 20 0a 20 20 2a 2a 20  clusive.  .  ** 
d8f10 4a 75 73 74 20 61 64 6a 75 73 74 20 6c 65 76 65  Just adjust leve
d8f20 6c 20 61 6e 64 20 70 75 6e 74 20 6f 6e 20 6f 75  l and punt on ou
d8f30 74 74 61 20 68 65 72 65 2e 20 2a 2f 0a 20 20 69  tta here. */.  i
d8f40 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f (pFile->lockty
d8f50 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a  pe > NO_LOCK) {.
d8f60 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
d8f70 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a  ype = locktype;.
d8f80 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
d8f90 4f 4b 3b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6d  OK;.    goto sem
d8fa0 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 20  _end_lock;.  }. 
d8fb0 20 0a 20 20 2f 2a 20 6c 6f 63 6b 20 73 65 6d 61   .  /* lock sema
d8fc0 70 68 6f 72 65 20 6e 6f 77 20 62 75 74 20 62 61  phore now but ba
d8fd0 69 6c 20 6f 75 74 20 77 68 65 6e 20 61 6c 72 65  il out when alre
d8fe0 61 64 79 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20  ady locked. */. 
d8ff0 20 69 66 28 20 73 65 6d 5f 74 72 79 77 61 69 74   if( sem_trywait
d9000 28 70 53 65 6d 29 3d 3d 2d 31 20 29 7b 0a 20 20  (pSem)==-1 ){.  
d9010 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55    rc = SQLITE_BU
d9020 53 59 3b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6d  SY;.    goto sem
d9030 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a  _end_lock;.  }..
d9040 20 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65 74    /* got it, set
d9050 20 74 68 65 20 74 79 70 65 20 61 6e 64 20 72 65   the type and re
d9060 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 70 46 69  turn ok */.  pFi
d9070 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c  le->locktype = l
d9080 6f 63 6b 74 79 70 65 3b 0a 0a 20 73 65 6d 5f 65  ocktype;.. sem_e
d9090 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 72 65 74 75 72  nd_lock:.  retur
d90a0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  n rc;.}../*.** L
d90b0 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67  ower the locking
d90c0 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64   level on file d
d90d0 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20  escriptor pFile 
d90e0 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f  to locktype.  lo
d90f0 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62  cktype.** must b
d9100 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b  e either NO_LOCK
d9110 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e   or SHARED_LOCK.
d9120 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f  .**.** If the lo
d9130 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74  cking level of t
d9140 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
d9150 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74  or is already at
d9160 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65   or below.** the
d9170 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69   requested locki
d9180 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72  ng level, this r
d9190 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
d91a0 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  p..*/.static int
d91b0 20 73 65 6d 55 6e 6c 6f 63 6b 28 73 71 6c 69 74   semUnlock(sqlit
d91c0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
d91d0 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75   locktype) {.  u
d91e0 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
d91f0 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
d9200 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20    sem_t *pSem = 
d9210 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53  pFile->pOpen->pS
d9220 65 6d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  em;..  assert( p
d9230 46 69 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 74  File );.  assert
d9240 28 20 70 53 65 6d 20 29 3b 0a 20 20 4f 53 54 52  ( pSem );.  OSTR
d9250 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64  ACE5("UNLOCK  %d
d9260 20 25 64 20 77 61 73 20 25 64 20 70 69 64 3d 25   %d was %d pid=%
d9270 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
d9280 6c 6f 63 6b 74 79 70 65 2c 0a 09 20 20 20 70 46  locktype,..   pF
d9290 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67  ile->locktype, g
d92a0 65 74 70 69 64 28 29 29 3b 0a 20 20 61 73 73 65  etpid());.  asse
d92b0 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48  rt( locktype<=SH
d92c0 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a  ARED_LOCK );.  .
d92d0 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f    /* no-op if po
d92e0 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20  ssible */.  if( 
d92f0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
d9300 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20  =locktype ){.   
d9310 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
d9320 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73  K;.  }.  .  /* s
d9330 68 61 72 65 64 20 63 61 6e 20 6a 75 73 74 20 62  hared can just b
d9340 65 20 73 65 74 20 62 65 63 61 75 73 65 20 77 65  e set because we
d9350 20 61 6c 77 61 79 73 20 68 61 76 65 20 61 6e 20   always have an 
d9360 65 78 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69  exclusive */.  i
d9370 66 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  f (locktype==SHA
d9380 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20  RED_LOCK) {.    
d9390 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
d93a0 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20  = locktype;.    
d93b0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d93c0 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f  ;.  }.  .  /* no
d93d0 2c 20 72 65 61 6c 6c 79 20 75 6e 6c 6f 63 6b 2e  , really unlock.
d93e0 20 2a 2f 0a 20 20 69 66 20 28 20 73 65 6d 5f 70   */.  if ( sem_p
d93f0 6f 73 74 28 70 53 65 6d 29 3d 3d 2d 31 20 29 20  ost(pSem)==-1 ) 
d9400 7b 0a 20 20 20 20 69 6e 74 20 72 63 2c 20 74 45  {.    int rc, tE
d9410 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
d9420 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72    rc = sqliteErr
d9430 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72  orFromPosixError
d9440 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f  (tErrno, SQLITE_
d9450 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20  IOERR_UNLOCK);. 
d9460 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45     if( IS_LOCK_E
d9470 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20  RROR(rc) ){.    
d9480 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
d9490 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
d94a0 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63   }.    return rc
d94b0 3b 20 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e  ; .  }.  pFile->
d94c0 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f  locktype = NO_LO
d94d0 43 4b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  CK;.  return SQL
d94e0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 20 2a  ITE_OK;.}../*. *
d94f0 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a  * Close a file..
d9500 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73   */.static int s
d9510 65 6d 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  emClose(sqlite3_
d9520 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 66  file *id) {.  if
d9530 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78  ( id ){.    unix
d9540 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
d9550 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20  nixFile*)id;.   
d9560 20 73 65 6d 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e   semUnlock(id, N
d9570 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 61 73 73  O_LOCK);.    ass
d9580 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20  ert( pFile );.  
d9590 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78    unixEnterMutex
d95a0 28 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 4c  ();.    releaseL
d95b0 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70  ockInfo(pFile->p
d95c0 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 65 6c 65 61  Lock);.    relea
d95d0 73 65 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d  seOpenCnt(pFile-
d95e0 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 75 6e 69  >pOpen);.    uni
d95f0 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  xLeaveMutex();. 
d9600 20 20 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65     closeUnixFile
d9610 28 69 64 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  (id);.  }.  retu
d9620 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
d9630 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58  .#endif /* OS_VX
d9640 57 4f 52 4b 53 20 2a 2f 0a 2f 2a 0a 2a 2a 20 4e  WORKS */./*.** N
d9650 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c  amed semaphore l
d9660 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20 61  ocking is only a
d9670 76 61 69 6c 61 62 6c 65 20 6f 6e 20 56 78 57 6f  vailable on VxWo
d9680 72 6b 73 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  rks..**.********
d9690 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74  ******* End of t
d96a0 68 65 20 6e 61 6d 65 64 20 73 65 6d 61 70 68 6f  he named semapho
d96b0 72 65 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e  re lock implemen
d96c0 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  tation *********
d96d0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  *******.********
d96e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d96f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9720 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a  ******/.../*****
d9730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9770 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a  *********.******
d9780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9790 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 41 46 50 20  ***** Begin AFP 
d97a0 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a  Locking ********
d97b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d97c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
d97d0 41 46 50 20 69 73 20 74 68 65 20 41 70 70 6c 65  AFP is the Apple
d97e0 20 46 69 6c 69 6e 67 20 50 72 6f 74 6f 63 6f 6c   Filing Protocol
d97f0 2e 20 20 41 46 50 20 69 73 20 61 20 6e 65 74 77  .  AFP is a netw
d9800 6f 72 6b 20 66 69 6c 65 73 79 73 74 65 6d 20 66  ork filesystem f
d9810 6f 75 6e 64 0a 2a 2a 20 6f 6e 20 41 70 70 6c 65  ound.** on Apple
d9820 20 4d 61 63 69 6e 74 6f 73 68 20 63 6f 6d 70 75   Macintosh compu
d9830 74 65 72 73 20 2d 20 62 6f 74 68 20 4f 53 39 20  ters - both OS9 
d9840 61 6e 64 20 4f 53 58 2e 0a 2a 2a 0a 2a 2a 20 54  and OSX..**.** T
d9850 68 69 72 64 2d 70 61 72 74 79 20 69 6d 70 6c 65  hird-party imple
d9860 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 41 46  mentations of AF
d9870 50 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2e  P are available.
d9880 20 20 42 75 74 20 74 68 69 73 20 63 6f 64 65 20    But this code 
d9890 68 65 72 65 0a 2a 2a 20 6f 6e 6c 79 20 77 6f 72  here.** only wor
d98a0 6b 73 20 6f 6e 20 4f 53 58 2e 0a 2a 2f 0a 0a 23  ks on OSX..*/..#
d98b0 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50  if defined(__APP
d98c0 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f  LE__) && SQLITE_
d98d0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
d98e0 54 59 4c 45 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61  TYLE./*.** The a
d98f0 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  fpLockingContext
d9900 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61   structure conta
d9910 69 6e 73 20 61 6c 6c 20 61 66 70 20 6c 6f 63 6b  ins all afp lock
d9920 20 73 70 65 63 69 66 69 63 20 73 74 61 74 65 0a   specific state.
d9930 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
d9940 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74  t afpLockingCont
d9950 65 78 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f  ext afpLockingCo
d9960 6e 74 65 78 74 3b 0a 73 74 72 75 63 74 20 61 66  ntext;.struct af
d9970 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  pLockingContext 
d9980 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  {.  unsigned lon
d9990 67 20 6c 6f 6e 67 20 73 68 61 72 65 64 42 79 74  g long sharedByt
d99a0 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  e;.  const char 
d99b0 2a 64 62 50 61 74 68 3b 20 20 20 20 20 20 20 20  *dbPath;        
d99c0 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
d99d0 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f  the open file */
d99e0 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 42 79 74 65  .};..struct Byte
d99f0 52 61 6e 67 65 4c 6f 63 6b 50 42 32 0a 7b 0a 20  RangeLockPB2.{. 
d9a00 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c   unsigned long l
d9a10 6f 6e 67 20 6f 66 66 73 65 74 3b 20 20 20 20 20  ong offset;     
d9a20 20 20 20 2f 2a 20 6f 66 66 73 65 74 20 74 6f 20     /* offset to 
d9a30 66 69 72 73 74 20 62 79 74 65 20 74 6f 20 6c 6f  first byte to lo
d9a40 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  ck */.  unsigned
d9a50 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6c 65 6e 67 74   long long lengt
d9a60 68 3b 20 20 20 20 20 20 20 20 2f 2a 20 6e 62 72  h;        /* nbr
d9a70 20 6f 66 20 62 79 74 65 73 20 74 6f 20 6c 6f 63   of bytes to loc
d9a80 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  k */.  unsigned 
d9a90 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 52 61 6e  long long retRan
d9aa0 67 65 53 74 61 72 74 3b 20 2f 2a 20 6e 62 72 20  geStart; /* nbr 
d9ab0 6f 66 20 31 73 74 20 62 79 74 65 20 6c 6f 63 6b  of 1st byte lock
d9ac0 65 64 20 69 66 20 73 75 63 63 65 73 73 66 75 6c  ed if successful
d9ad0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   */.  unsigned c
d9ae0 68 61 72 20 75 6e 4c 6f 63 6b 46 6c 61 67 3b 20  har unLockFlag; 
d9af0 20 20 20 20 20 20 20 20 2f 2a 20 31 20 3d 20 75          /* 1 = u
d9b00 6e 6c 6f 63 6b 2c 20 30 20 3d 20 6c 6f 63 6b 20  nlock, 0 = lock 
d9b10 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
d9b20 61 72 20 73 74 61 72 74 45 6e 64 46 6c 61 67 3b  ar startEndFlag;
d9b30 20 20 20 20 20 20 20 2f 2a 20 31 3d 72 65 6c 20         /* 1=rel 
d9b40 74 6f 20 65 6e 64 20 6f 66 20 66 6f 72 6b 2c 20  to end of fork, 
d9b50 30 3d 72 65 6c 20 74 6f 20 73 74 61 72 74 20 2a  0=rel to start *
d9b60 2f 0a 20 20 69 6e 74 20 66 64 3b 20 20 20 20 20  /.  int fd;     
d9b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9b80 20 20 20 20 20 20 2f 2a 20 66 69 6c 65 20 64 65        /* file de
d9b90 73 63 20 74 6f 20 61 73 73 6f 63 20 74 68 69 73  sc to assoc this
d9ba0 20 6c 6f 63 6b 20 77 69 74 68 20 2a 2f 0a 7d 3b   lock with */.};
d9bb0 0a 0a 23 64 65 66 69 6e 65 20 61 66 70 66 73 42  ..#define afpfsB
d9bc0 79 74 65 52 61 6e 67 65 4c 6f 63 6b 32 46 53 43  yteRangeLock2FSC
d9bd0 54 4c 20 20 20 20 20 20 20 20 5f 49 4f 57 52 28  TL        _IOWR(
d9be0 27 7a 27 2c 20 32 33 2c 20 73 74 72 75 63 74 20  'z', 23, struct 
d9bf0 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32  ByteRangeLockPB2
d9c00 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73  )../*.** This is
d9c10 20 61 20 75 74 69 6c 69 74 79 20 66 6f 72 20 73   a utility for s
d9c20 65 74 74 69 6e 67 20 6f 72 20 63 6c 65 61 72 69  etting or cleari
d9c30 6e 67 20 61 20 62 69 74 2d 72 61 6e 67 65 20 6c  ng a bit-range l
d9c40 6f 63 6b 20 6f 6e 20 61 6e 0a 2a 2a 20 41 46 50  ock on an.** AFP
d9c50 20 66 69 6c 65 73 79 73 74 65 6d 2e 0a 2a 2a 20   filesystem..** 
d9c60 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
d9c70 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2c  E_OK on success,
d9c80 20 53 51 4c 49 54 45 5f 42 55 53 59 20 6f 6e 20   SQLITE_BUSY on 
d9c90 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74  failure..*/.stat
d9ca0 69 63 20 69 6e 74 20 61 66 70 53 65 74 4c 6f 63  ic int afpSetLoc
d9cb0 6b 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  k(.  const char 
d9cc0 2a 70 61 74 68 2c 20 20 20 20 20 20 20 20 20 20  *path,          
d9cd0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
d9ce0 68 65 20 66 69 6c 65 20 74 6f 20 62 65 20 6c 6f  he file to be lo
d9cf0 63 6b 65 64 20 6f 72 20 75 6e 6c 6f 63 6b 65 64  cked or unlocked
d9d00 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a   */.  unixFile *
d9d10 70 46 69 6c 65 2c 20 20 20 20 20 20 20 20 20 20  pFile,          
d9d20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 66 69 6c       /* Open fil
d9d30 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f 6e 20  e descriptor on 
d9d40 70 61 74 68 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  path */.  unsign
d9d50 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66  ed long long off
d9d60 73 65 74 2c 20 20 20 20 20 2f 2a 20 46 69 72 73  set,     /* Firs
d9d70 74 20 62 79 74 65 20 74 6f 20 62 65 20 6c 6f 63  t byte to be loc
d9d80 6b 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ked */.  unsigne
d9d90 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6c 65 6e 67  d long long leng
d9da0 74 68 2c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  th,     /* Numbe
d9db0 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 6c 6f  r of bytes to lo
d9dc0 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 73 65 74 4c  ck */.  int setL
d9dd0 6f 63 6b 46 6c 61 67 20 20 20 20 20 20 20 20 20  ockFlag         
d9de0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
d9df0 6f 20 73 65 74 20 6c 6f 63 6b 2e 20 20 46 61 6c  o set lock.  Fal
d9e00 73 65 20 74 6f 20 63 6c 65 61 72 20 6c 6f 63 6b  se to clear lock
d9e10 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20   */.){.  struct 
d9e20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32  ByteRangeLockPB2
d9e30 20 70 62 3b 0a 20 20 69 6e 74 20 65 72 72 3b 0a   pb;.  int err;.
d9e40 20 20 0a 20 20 70 62 2e 75 6e 4c 6f 63 6b 46 6c    .  pb.unLockFl
d9e50 61 67 20 3d 20 73 65 74 4c 6f 63 6b 46 6c 61 67  ag = setLockFlag
d9e60 20 3f 20 30 20 3a 20 31 3b 0a 20 20 70 62 2e 73   ? 0 : 1;.  pb.s
d9e70 74 61 72 74 45 6e 64 46 6c 61 67 20 3d 20 30 3b  tartEndFlag = 0;
d9e80 0a 20 20 70 62 2e 6f 66 66 73 65 74 20 3d 20 6f  .  pb.offset = o
d9e90 66 66 73 65 74 3b 0a 20 20 70 62 2e 6c 65 6e 67  ffset;.  pb.leng
d9ea0 74 68 20 3d 20 6c 65 6e 67 74 68 3b 20 0a 20 20  th = length; .  
d9eb0 70 62 2e 66 64 20 3d 20 70 46 69 6c 65 2d 3e 68  pb.fd = pFile->h
d9ec0 3b 0a 20 20 0a 20 20 4f 53 54 52 41 43 45 36 28  ;.  .  OSTRACE6(
d9ed0 22 41 46 50 53 45 54 4c 4f 43 4b 20 5b 25 73 5d  "AFPSETLOCK [%s]
d9ee0 20 66 6f 72 20 25 64 25 73 20 69 6e 20 72 61 6e   for %d%s in ran
d9ef0 67 65 20 25 6c 6c 78 3a 25 6c 6c 78 5c 6e 22 2c  ge %llx:%llx\n",
d9f00 20 0a 20 20 20 20 28 73 65 74 4c 6f 63 6b 46 6c   .    (setLockFl
d9f10 61 67 3f 22 4f 4e 22 3a 22 4f 46 46 22 29 2c 20  ag?"ON":"OFF"), 
d9f20 70 46 69 6c 65 2d 3e 68 2c 20 28 70 62 2e 66 64  pFile->h, (pb.fd
d9f30 3d 3d 2d 31 3f 22 5b 74 65 73 74 76 61 6c 2d 31  ==-1?"[testval-1
d9f40 5d 22 3a 22 22 29 2c 0a 20 20 20 20 6f 66 66 73  ]":""),.    offs
d9f50 65 74 2c 20 6c 65 6e 67 74 68 29 3b 0a 20 20 65  et, length);.  e
d9f60 72 72 20 3d 20 66 73 63 74 6c 28 70 61 74 68 2c  rr = fsctl(path,
d9f70 20 61 66 70 66 73 42 79 74 65 52 61 6e 67 65 4c   afpfsByteRangeL
d9f80 6f 63 6b 32 46 53 43 54 4c 2c 20 26 70 62 2c 20  ock2FSCTL, &pb, 
d9f90 30 29 3b 0a 20 20 69 66 20 28 20 65 72 72 3d 3d  0);.  if ( err==
d9fa0 2d 31 20 29 20 7b 0a 20 20 20 20 69 6e 74 20 72  -1 ) {.    int r
d9fb0 63 3b 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e  c;.    int tErrn
d9fc0 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 4f  o = errno;.    O
d9fd0 53 54 52 41 43 45 34 28 22 41 46 50 53 45 54 4c  STRACE4("AFPSETL
d9fe0 4f 43 4b 20 66 61 69 6c 65 64 20 74 6f 20 66 73  OCK failed to fs
d9ff0 63 74 6c 28 29 20 27 25 73 27 20 25 64 20 25 73  ctl() '%s' %d %s
da000 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
da010 20 20 70 61 74 68 2c 20 74 45 72 72 6e 6f 2c 20    path, tErrno, 
da020 73 74 72 65 72 72 6f 72 28 74 45 72 72 6e 6f 29  strerror(tErrno)
da030 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  );.#ifdef SQLITE
da040 5f 49 47 4e 4f 52 45 5f 41 46 50 5f 4c 4f 43 4b  _IGNORE_AFP_LOCK
da050 5f 45 52 52 4f 52 53 0a 20 20 20 20 72 63 20 3d  _ERRORS.    rc =
da060 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 23 65   SQLITE_BUSY;.#e
da070 6c 73 65 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  lse.    rc = sql
da080 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69  iteErrorFromPosi
da090 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 0a 20  xError(tErrno,. 
da0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
da0b0 20 20 20 73 65 74 4c 6f 63 6b 46 6c 61 67 20 3f     setLockFlag ?
da0c0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f   SQLITE_IOERR_LO
da0d0 43 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52  CK : SQLITE_IOER
da0e0 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 23 65 6e 64 69  R_UNLOCK);.#endi
da0f0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f  f /* SQLITE_IGNO
da100 52 45 5f 41 46 50 5f 4c 4f 43 4b 5f 45 52 52 4f  RE_AFP_LOCK_ERRO
da110 52 53 20 2a 2f 0a 20 20 20 20 69 66 28 20 49 53  RS */.    if( IS
da120 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20  _LOCK_ERROR(rc) 
da130 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  ){.      pFile->
da140 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72  lastErrno = tErr
da150 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  no;.    }.    re
da160 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20 65 6c 73  turn rc;.  } els
da170 65 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  e {.    return S
da180 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a  QLITE_OK;.  }.}.
da190 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
da1a0 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 68  ine checks if th
da1b0 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56 45  ere is a RESERVE
da1c0 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74  D lock held on t
da1d0 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20  he specified.** 
da1e0 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20  file by this or 
da1f0 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73  any other proces
da200 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63  s. If such a loc
da210 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a  k is held, set *
da220 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20  pResOut.** to a 
da230 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f  non-zero value o
da240 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75  therwise *pResOu
da250 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f  t is set to zero
da260 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  .  The return va
da270 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f  lue.** is set to
da280 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73   SQLITE_OK unles
da290 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f  s an I/O error o
da2a0 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63  ccurs during loc
da2b0 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73  k checking..*/.s
da2c0 74 61 74 69 63 20 69 6e 74 20 61 66 70 43 68 65  tatic int afpChe
da2d0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73  ckReservedLock(s
da2e0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
da2f0 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a   int *pResOut){.
da300 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
da310 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65  E_OK;.  int rese
da320 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78  rved = 0;.  unix
da330 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
da340 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a  nixFile*)id;.  .
da350 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
da360 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
da370 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45  _IOERR_CHECKRESE
da380 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a  RVEDLOCK; );.  .
da390 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
da3a0 29 3b 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43  );.  afpLockingC
da3b0 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 20  ontext *context 
da3c0 3d 20 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e  = (afpLockingCon
da3d0 74 65 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c  text *) pFile->l
da3e0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20  ockingContext;. 
da3f0 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20   .  /* Check if 
da400 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73  a thread in this
da410 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73   process holds s
da420 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20  uch a lock */.  
da430 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
da440 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ype>SHARED_LOCK 
da450 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20  ){.    reserved 
da460 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a  = 1;.  }.  .  /*
da470 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69   Otherwise see i
da480 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f  f some other pro
da490 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 0a 20  cess holds it.. 
da4a0 20 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65    */.  if( !rese
da4b0 72 76 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 6c  rved ){.    /* l
da4c0 6f 63 6b 20 74 68 65 20 52 45 53 45 52 56 45 44  ock the RESERVED
da4d0 20 62 79 74 65 20 2a 2f 0a 20 20 20 20 69 6e 74   byte */.    int
da4e0 20 6c 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63   lrc = afpSetLoc
da4f0 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74  k(context->dbPat
da500 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56  h, pFile, RESERV
da510 45 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b 20 20  ED_BYTE, 1,1);  
da520 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f  .    if( SQLITE_
da530 4f 4b 3d 3d 6c 72 63 20 29 7b 0a 20 20 20 20 20  OK==lrc ){.     
da540 20 2f 2a 20 69 66 20 77 65 20 73 75 63 63 65 65   /* if we succee
da550 64 65 64 20 69 6e 20 74 61 6b 69 6e 67 20 74 68  ded in taking th
da560 65 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2c  e reserved lock,
da570 20 75 6e 6c 6f 63 6b 20 69 74 20 74 6f 20 72 65   unlock it to re
da580 73 74 6f 72 65 0a 20 20 20 20 20 20 2a 2a 20 74  store.      ** t
da590 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74  he original stat
da5a0 65 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d  e */.      lrc =
da5b0 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74   afpSetLock(cont
da5c0 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69  ext->dbPath, pFi
da5d0 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54  le, RESERVED_BYT
da5e0 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20  E, 1, 0);.    } 
da5f0 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 2f 2a 20  else {.      /* 
da600 69 66 20 77 65 20 66 61 69 6c 65 64 20 74 6f 20  if we failed to 
da610 67 65 74 20 74 68 65 20 6c 6f 63 6b 20 74 68 65  get the lock the
da620 6e 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d  n someone else m
da630 75 73 74 20 68 61 76 65 20 69 74 20 2a 2f 0a 20  ust have it */. 
da640 20 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20       reserved = 
da650 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  1;.    }.    if(
da660 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c   IS_LOCK_ERROR(l
da670 72 63 29 20 29 7b 0a 20 20 20 20 20 20 72 63 3d  rc) ){.      rc=
da680 6c 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  lrc;.    }.  }. 
da690 20 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 45   .  OSTRACE4("TE
da6a0 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64  ST WR-LOCK %d %d
da6b0 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68   %d\n", pFile->h
da6c0 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b  , rc, reserved);
da6d0 0a 20 20 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d  .  .  *pResOut =
da6e0 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74   reserved;.  ret
da6f0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
da700 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77   Lock the file w
da710 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65  ith the lock spe
da720 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65  cified by parame
da730 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f  ter locktype - o
da740 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c  ne.** of the fol
da750 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  lowing:.**.**   
da760 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43    (1) SHARED_LOC
da770 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53  K.**     (2) RES
da780 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  ERVED_LOCK.**   
da790 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f    (3) PENDING_LO
da7a0 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58  CK.**     (4) EX
da7b0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a  CLUSIVE_LOCK.**.
da7c0 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65  ** Sometimes whe
da7d0 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65  n requesting one
da7e0 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64   lock state, add
da7f0 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61  itional lock sta
da800 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72  tes.** are inser
da810 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20  ted in between. 
da820 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67   The locking mig
da830 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f  ht fail on one o
da840 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74  f the later.** t
da850 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69  ransitions leavi
da860 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74  ng the lock stat
da870 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  e different from
da880 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64   what it started
da890 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68   but.** still sh
da8a0 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e  ort of its goal.
da8b0 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
da8c0 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20  chart shows the 
da8d0 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73  allowed.** trans
da8e0 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69  itions and the i
da8f0 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64  nserted intermed
da900 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a  iate states:.**.
da910 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d  **    UNLOCKED -
da920 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53  > SHARED.**    S
da930 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45  HARED -> RESERVE
da940 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  D.**    SHARED -
da950 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45  > (PENDING) -> E
da960 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52  XCLUSIVE.**    R
da970 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44  ESERVED -> (PEND
da980 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56  ING) -> EXCLUSIV
da990 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20  E.**    PENDING 
da9a0 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a  -> EXCLUSIVE.**.
da9b0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
da9c0 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61  will only increa
da9d0 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20  se a lock.  Use 
da9e0 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c  the sqlite3OsUnl
da9f0 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65  ock().** routine
daa00 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b   to lower a lock
daa10 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74  ing level..*/.st
daa20 61 74 69 63 20 69 6e 74 20 61 66 70 4c 6f 63 6b  atic int afpLock
daa30 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
daa40 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29  d, int locktype)
daa50 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
daa60 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69  ITE_OK;.  unixFi
daa70 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
daa80 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70  xFile*)id;.  afp
daa90 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a  LockingContext *
daaa0 63 6f 6e 74 65 78 74 20 3d 20 28 61 66 70 4c 6f  context = (afpLo
daab0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20  ckingContext *) 
daac0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
daad0 6e 74 65 78 74 3b 0a 20 20 0a 20 20 61 73 73 65  ntext;.  .  asse
daae0 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f  rt( pFile );.  O
daaf0 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 20 20  STRACE5("LOCK   
dab00 20 25 64 20 25 73 20 77 61 73 20 25 73 20 70 69   %d %s was %s pi
dab10 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  d=%d\n", pFile->
dab20 68 2c 0a 20 20 20 20 20 20 20 20 20 6c 6f 63 6b  h,.         lock
dab30 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70  typeName(locktyp
dab40 65 29 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65  e), locktypeName
dab50 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65  (pFile->locktype
dab60 29 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20  ), getpid());.. 
dab70 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
dab80 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f  already a lock o
dab90 66 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d  f this type or m
daba0 6f 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20  ore restrictive 
dabb0 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 78  on the.  ** unix
dabc0 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67  File, do nothing
dabd0 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65 20  . Don't use the 
dabe0 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78  afp_end_lock: ex
dabf0 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a  it path, as.  **
dac00 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28   unixEnterMutex(
dac10 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61  ) hasn't been ca
dac20 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20  lled yet..  */. 
dac30 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
dac40 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29  type>=locktype )
dac50 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 22  {.    OSTRACE3("
dac60 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 6f 6b  LOCK    %d %s ok
dac70 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 29 5c   (already held)\
dac80 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20  n", pFile->h,.  
dac90 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70           locktyp
daca0 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 29  eName(locktype))
dacb0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
dacc0 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f  ITE_OK;.  }..  /
dacd0 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20  * Make sure the 
dace0 6c 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65  locking sequence
dacf0 20 69 73 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f   is correct.  */
dad00 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
dad10 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c  ->locktype!=NO_L
dad20 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d  OCK || locktype=
dad30 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
dad40 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
dad50 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  pe!=PENDING_LOCK
dad60 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f   );.  assert( lo
dad70 63 6b 74 79 70 65 21 3d 52 45 53 45 52 56 45 44  cktype!=RESERVED
dad80 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e  _LOCK || pFile->
dad90 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
dada0 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a  _LOCK );.  .  /*
dadb0 20 54 68 69 73 20 6d 75 74 65 78 20 69 73 20 6e   This mutex is n
dadc0 65 65 64 65 64 20 62 65 63 61 75 73 65 20 70 46  eeded because pF
dadd0 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73 20 73 68  ile->pLock is sh
dade0 61 72 65 64 20 61 63 72 6f 73 73 20 74 68 72 65  ared across thre
dadf0 61 64 73 0a 20 20 2a 2f 0a 20 20 75 6e 69 78 45  ads.  */.  unixE
dae00 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20  nterMutex();..  
dae10 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  /* Make sure the
dae20 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20   current thread 
dae30 6f 77 6e 73 20 74 68 65 20 70 46 69 6c 65 2e 0a  owns the pFile..
dae40 20 20 2a 2f 0a 20 20 72 63 20 3d 20 74 72 61 6e    */.  rc = tran
dae50 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28 70 46  sferOwnership(pF
dae60 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  ile);.  if( rc!=
dae70 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
dae80 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28   unixLeaveMutex(
dae90 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  );.    return rc
daea0 3b 0a 20 20 7d 0a 20 20 20 20 0a 20 20 2f 2a 20  ;.  }.    .  /* 
daeb0 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69  A PENDING lock i
daec0 73 20 6e 65 65 64 65 64 20 62 65 66 6f 72 65 20  s needed before 
daed0 61 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52  acquiring a SHAR
daee0 45 44 20 6c 6f 63 6b 20 61 6e 64 20 62 65 66 6f  ED lock and befo
daef0 72 65 0a 20 20 2a 2a 20 61 63 71 75 69 72 69 6e  re.  ** acquirin
daf00 67 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c  g an EXCLUSIVE l
daf10 6f 63 6b 2e 20 20 46 6f 72 20 74 68 65 20 53 48  ock.  For the SH
daf20 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 50  ARED lock, the P
daf30 45 4e 44 49 4e 47 20 77 69 6c 6c 0a 20 20 2a 2a  ENDING will.  **
daf40 20 62 65 20 72 65 6c 65 61 73 65 64 2e 0a 20 20   be released..  
daf50 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70  */.  if( locktyp
daf60 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 0a  e==SHARED_LOCK .
daf70 20 20 20 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79        || (lockty
daf80 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f  pe==EXCLUSIVE_LO
daf90 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63  CK && pFile->loc
dafa0 6b 74 79 70 65 3c 50 45 4e 44 49 4e 47 5f 4c 4f  ktype<PENDING_LO
dafb0 43 4b 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74  CK).  ){.    int
dafc0 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 66 61 69   failed;.    fai
dafd0 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b  led = afpSetLock
dafe0 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68  (context->dbPath
daff0 2c 20 70 46 69 6c 65 2c 20 50 45 4e 44 49 4e 47  , pFile, PENDING
db000 5f 42 59 54 45 2c 20 31 2c 20 31 29 3b 0a 20 20  _BYTE, 1, 1);.  
db010 20 20 69 66 20 28 66 61 69 6c 65 64 29 20 7b 0a    if (failed) {.
db020 20 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65        rc = faile
db030 64 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 66  d;.      goto af
db040 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20  p_end_lock;.    
db050 7d 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 49 66  }.  }.  .  /* If
db060 20 63 6f 6e 74 72 6f 6c 20 67 65 74 73 20 74 6f   control gets to
db070 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65   this point, the
db080 6e 20 61 63 74 75 61 6c 6c 79 20 67 6f 20 61 68  n actually go ah
db090 65 61 64 20 61 6e 64 20 6d 61 6b 65 0a 20 20 2a  ead and make.  *
db0a0 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  * operating syst
db0b0 65 6d 20 63 61 6c 6c 73 20 66 6f 72 20 74 68 65  em calls for the
db0c0 20 73 70 65 63 69 66 69 65 64 20 6c 6f 63 6b 2e   specified lock.
db0d0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b  .  */.  if( lock
db0e0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
db0f0 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 6c 6b 2c  K ){.    int lk,
db100 20 6c 72 63 31 2c 20 6c 72 63 32 2c 20 6c 72 63   lrc1, lrc2, lrc
db110 31 45 72 72 6e 6f 3b 0a 20 20 20 20 0a 20 20 20  1Errno;.    .   
db120 20 2f 2a 20 4e 6f 77 20 67 65 74 20 74 68 65 20   /* Now get the 
db130 72 65 61 64 2d 6c 6f 63 6b 20 53 48 41 52 45 44  read-lock SHARED
db140 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 20 20 2f 2a 20  _LOCK */.    /* 
db150 6e 6f 74 65 20 74 68 61 74 20 74 68 65 20 71 75  note that the qu
db160 61 6c 69 74 79 20 6f 66 20 74 68 65 20 72 61 6e  ality of the ran
db170 64 6f 6d 6e 65 73 73 20 64 6f 65 73 6e 27 74 20  domness doesn't 
db180 6d 61 74 74 65 72 20 74 68 61 74 20 6d 75 63 68  matter that much
db190 20 2a 2f 0a 20 20 20 20 6c 6b 20 3d 20 72 61 6e   */.    lk = ran
db1a0 64 6f 6d 28 29 3b 20 0a 20 20 20 20 63 6f 6e 74  dom(); .    cont
db1b0 65 78 74 2d 3e 73 68 61 72 65 64 42 79 74 65 20  ext->sharedByte 
db1c0 3d 20 28 6c 6b 20 26 20 30 78 37 66 66 66 66 66  = (lk & 0x7fffff
db1d0 66 66 29 25 28 53 48 41 52 45 44 5f 53 49 5a 45  ff)%(SHARED_SIZE
db1e0 20 2d 20 31 29 3b 0a 20 20 20 20 6c 72 63 31 20   - 1);.    lrc1 
db1f0 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e  = afpSetLock(con
db200 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46  text->dbPath, pF
db210 69 6c 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20  ile, .          
db220 53 48 41 52 45 44 5f 46 49 52 53 54 2b 63 6f 6e  SHARED_FIRST+con
db230 74 65 78 74 2d 3e 73 68 61 72 65 64 42 79 74 65  text->sharedByte
db240 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 69 66 28  , 1, 1);.    if(
db250 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c   IS_LOCK_ERROR(l
db260 72 63 31 29 20 29 7b 0a 20 20 20 20 20 20 6c 72  rc1) ){.      lr
db270 63 31 45 72 72 6e 6f 20 3d 20 70 46 69 6c 65 2d  c1Errno = pFile-
db280 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20  >lastErrno;.    
db290 7d 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68  }.    /* Drop th
db2a0 65 20 74 65 6d 70 6f 72 61 72 79 20 50 45 4e 44  e temporary PEND
db2b0 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20  ING lock */.    
db2c0 6c 72 63 32 20 3d 20 61 66 70 53 65 74 4c 6f 63  lrc2 = afpSetLoc
db2d0 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74  k(context->dbPat
db2e0 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e 44 49 4e  h, pFile, PENDIN
db2f0 47 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20  G_BYTE, 1, 0);. 
db300 20 20 20 0a 20 20 20 20 69 66 28 20 49 53 5f 4c     .    if( IS_L
db310 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 31 29 20  OCK_ERROR(lrc1) 
db320 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  ) {.      pFile-
db330 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c 72 63  >lastErrno = lrc
db340 31 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63  1Errno;.      rc
db350 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20 20 20 67   = lrc1;.      g
db360 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b  oto afp_end_lock
db370 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28  ;.    } else if(
db380 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c   IS_LOCK_ERROR(l
db390 72 63 32 29 20 29 7b 0a 20 20 20 20 20 20 72 63  rc2) ){.      rc
db3a0 20 3d 20 6c 72 63 32 3b 0a 20 20 20 20 20 20 67   = lrc2;.      g
db3b0 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b  oto afp_end_lock
db3c0 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28  ;.    } else if(
db3d0 20 6c 72 63 31 20 21 3d 20 53 51 4c 49 54 45 5f   lrc1 != SQLITE_
db3e0 4f 4b 20 29 20 7b 0a 20 20 20 20 20 20 72 63 20  OK ) {.      rc 
db3f0 3d 20 6c 72 63 31 3b 0a 20 20 20 20 7d 20 65 6c  = lrc1;.    } el
db400 73 65 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65  se {.      pFile
db410 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41  ->locktype = SHA
db420 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20  RED_LOCK;.      
db430 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c  pFile->pOpen->nL
db440 6f 63 6b 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  ock++;.    }.  }
db450 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65  else{.    /* The
db460 20 72 65 71 75 65 73 74 20 77 61 73 20 66 6f 72   request was for
db470 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 45   a RESERVED or E
db480 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20  XCLUSIVE lock.  
db490 49 74 20 69 73 0a 20 20 20 20 2a 2a 20 61 73 73  It is.    ** ass
db4a0 75 6d 65 64 20 74 68 61 74 20 74 68 65 72 65 20  umed that there 
db4b0 69 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 67  is a SHARED or g
db4c0 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74  reater lock on t
db4d0 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 61  he file.    ** a
db4e0 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20  lready..    */. 
db4f0 20 20 20 69 6e 74 20 66 61 69 6c 65 64 20 3d 20     int failed = 
db500 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 30  0;.    assert( 0
db510 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  !=pFile->locktyp
db520 65 20 29 3b 0a 20 20 20 20 69 66 20 28 6c 6f 63  e );.    if (loc
db530 6b 74 79 70 65 20 3e 3d 20 52 45 53 45 52 56 45  ktype >= RESERVE
db540 44 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d  D_LOCK && pFile-
db550 3e 6c 6f 63 6b 74 79 70 65 20 3c 20 52 45 53 45  >locktype < RESE
db560 52 56 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20  RVED_LOCK) {.   
db570 20 20 20 20 20 2f 2a 20 41 63 71 75 69 72 65 20       /* Acquire 
db580 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  a RESERVED lock 
db590 2a 2f 0a 20 20 20 20 20 20 20 20 66 61 69 6c 65  */.        faile
db5a0 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63  d = afpSetLock(c
db5b0 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20  ontext->dbPath, 
db5c0 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45 44 5f  pFile, RESERVED_
db5d0 42 59 54 45 2c 20 31 2c 31 29 3b 0a 20 20 20 20  BYTE, 1,1);.    
db5e0 7d 0a 20 20 20 20 69 66 20 28 21 66 61 69 6c 65  }.    if (!faile
db5f0 64 20 26 26 20 6c 6f 63 6b 74 79 70 65 20 3d 3d  d && locktype ==
db600 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29   EXCLUSIVE_LOCK)
db610 20 7b 0a 20 20 20 20 20 20 2f 2a 20 41 63 71 75   {.      /* Acqu
db620 69 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45  ire an EXCLUSIVE
db630 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20   lock */.       
db640 20 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76   .      /* Remov
db650 65 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63  e the shared loc
db660 6b 20 62 65 66 6f 72 65 20 74 72 79 69 6e 67 20  k before trying 
db670 74 68 65 20 72 61 6e 67 65 2e 20 20 77 65 27 6c  the range.  we'l
db680 6c 20 6e 65 65 64 20 74 6f 20 0a 20 20 20 20 20  l need to .     
db690 20 2a 2a 20 72 65 65 73 74 61 62 6c 69 73 68 20   ** reestablish 
db6a0 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20  the shared lock 
db6b0 69 66 20 77 65 20 63 61 6e 27 74 20 67 65 74 20  if we can't get 
db6c0 74 68 65 20 20 61 66 70 55 6e 6c 6f 63 6b 0a 20  the  afpUnlock. 
db6d0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
db6e0 28 20 21 28 66 61 69 6c 65 64 20 3d 20 61 66 70  ( !(failed = afp
db6f0 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d  SetLock(context-
db700 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20  >dbPath, pFile, 
db710 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b 0a 20  SHARED_FIRST +. 
db720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
db730 20 20 20 20 20 20 20 20 63 6f 6e 74 65 78 74 2d          context-
db740 3e 73 68 61 72 65 64 42 79 74 65 2c 20 31 2c 20  >sharedByte, 1, 
db750 30 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69  0)) ){.        i
db760 6e 74 20 66 61 69 6c 65 64 32 20 3d 20 53 51 4c  nt failed2 = SQL
db770 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20  ITE_OK;.        
db780 2f 2a 20 6e 6f 77 20 61 74 74 65 6d 6d 70 74 20  /* now attemmpt 
db790 74 6f 20 67 65 74 20 74 68 65 20 65 78 63 6c 75  to get the exclu
db7a0 73 69 76 65 20 6c 6f 63 6b 20 72 61 6e 67 65 20  sive lock range 
db7b0 2a 2f 0a 20 20 20 20 20 20 20 20 66 61 69 6c 65  */.        faile
db7c0 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63  d = afpSetLock(c
db7d0 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20  ontext->dbPath, 
db7e0 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49  pFile, SHARED_FI
db7f0 52 53 54 2c 20 0a 20 20 20 20 20 20 20 20 20 20  RST, .          
db800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
db810 20 20 20 20 20 53 48 41 52 45 44 5f 53 49 5a 45       SHARED_SIZE
db820 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66  , 1);.        if
db830 28 20 66 61 69 6c 65 64 20 26 26 20 28 66 61 69  ( failed && (fai
db840 6c 65 64 32 20 3d 20 61 66 70 53 65 74 4c 6f 63  led2 = afpSetLoc
db850 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74  k(context->dbPat
db860 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20 20 20 20  h, pFile, .     
db870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
db880 20 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b    SHARED_FIRST +
db890 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64   context->shared
db8a0 42 79 74 65 2c 20 31 2c 20 31 29 29 20 29 7b 0a  Byte, 1, 1)) ){.
db8b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6e            /* Can
db8c0 27 74 20 72 65 65 73 74 61 62 6c 69 73 68 20 74  't reestablish t
db8d0 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20  he shared lock. 
db8e0 20 53 71 6c 69 74 65 20 63 61 6e 27 74 20 64 65   Sqlite can't de
db8f0 61 6c 2c 20 74 68 69 73 20 69 73 0a 20 20 20 20  al, this is.    
db900 20 20 20 20 20 20 2a 2a 20 61 20 63 72 69 74 69        ** a criti
db910 63 61 6c 20 49 2f 4f 20 65 72 72 6f 72 0a 20 20  cal I/O error.  
db920 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
db930 20 20 20 20 20 72 63 20 3d 20 28 28 66 61 69 6c       rc = ((fail
db940 65 64 20 26 20 53 51 4c 49 54 45 5f 49 4f 45 52  ed & SQLITE_IOER
db950 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45  R) == SQLITE_IOE
db960 52 52 29 20 3f 20 66 61 69 6c 65 64 32 20 3a 20  RR) ? failed2 : 
db970 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
db980 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43  SQLITE_IOERR_LOC
db990 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  K;.          got
db9a0 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a  o afp_end_lock;.
db9b0 20 20 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20          } .     
db9c0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
db9d0 72 63 20 3d 20 66 61 69 6c 65 64 3b 20 0a 20 20  rc = failed; .  
db9e0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
db9f0 69 66 28 20 66 61 69 6c 65 64 20 29 7b 0a 20 20  if( failed ){.  
dba00 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b      rc = failed;
dba10 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20  .    }.  }.  .  
dba20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
dba30 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  K ){.    pFile->
dba40 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74  locktype = lockt
dba50 79 70 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ype;.  }else if(
dba60 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55   locktype==EXCLU
dba70 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  SIVE_LOCK ){.   
dba80 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
dba90 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b   = PENDING_LOCK;
dbaa0 0a 20 20 7d 0a 20 20 0a 61 66 70 5f 65 6e 64 5f  .  }.  .afp_end_
dbab0 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c 65 61 76  lock:.  unixLeav
dbac0 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53 54 52  eMutex();.  OSTR
dbad0 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20 25 64  ACE4("LOCK    %d
dbae0 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69 6c 65   %s %s\n", pFile
dbaf0 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d  ->h, locktypeNam
dbb00 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20  e(locktype), .  
dbb10 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54         rc==SQLIT
dbb20 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66  E_OK ? "ok" : "f
dbb30 61 69 6c 65 64 22 29 3b 0a 20 20 72 65 74 75 72  ailed");.  retur
dbb40 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  n rc;.}../*.** L
dbb50 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67  ower the locking
dbb60 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64   level on file d
dbb70 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20  escriptor pFile 
dbb80 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f  to locktype.  lo
dbb90 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62  cktype.** must b
dbba0 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b  e either NO_LOCK
dbbb0 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e   or SHARED_LOCK.
dbbc0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f  .**.** If the lo
dbbd0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74  cking level of t
dbbe0 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
dbbf0 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74  or is already at
dbc00 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65   or below.** the
dbc10 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69   requested locki
dbc20 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72  ng level, this r
dbc30 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
dbc40 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  p..*/.static int
dbc50 20 61 66 70 55 6e 6c 6f 63 6b 28 73 71 6c 69 74   afpUnlock(sqlit
dbc60 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
dbc70 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 69   locktype) {.  i
dbc80 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
dbc90 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  K;.  unixFile *p
dbca0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
dbcb0 2a 29 69 64 3b 0a 20 20 61 66 70 4c 6f 63 6b 69  *)id;.  afpLocki
dbcc0 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20  ngContext *pCtx 
dbcd0 3d 20 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e  = (afpLockingCon
dbce0 74 65 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c  text *) pFile->l
dbcf0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 0a  ockingContext;..
dbd00 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
dbd10 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55  );.  OSTRACE5("U
dbd20 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73  NLOCK  %d %d was
dbd30 20 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70   %d pid=%d\n", p
dbd40 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70  File->h, locktyp
dbd50 65 2c 0a 20 20 20 20 20 20 20 20 20 70 46 69 6c  e,.         pFil
dbd60 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74  e->locktype, get
dbd70 70 69 64 28 29 29 3b 0a 0a 20 20 61 73 73 65 72  pid());..  asser
dbd80 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41  t( locktype<=SHA
dbd90 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66  RED_LOCK );.  if
dbda0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
dbdb0 65 3c 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20  e<=locktype ){. 
dbdc0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
dbdd0 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 43  _OK;.  }.  if( C
dbde0 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28 70 46  HECK_THREADID(pF
dbdf0 69 6c 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75  ile) ){.    retu
dbe00 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
dbe10 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 45 6e 74 65  ;.  }.  unixEnte
dbe20 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20  rMutex();.  if( 
dbe30 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e  pFile->locktype>
dbe40 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SHARED_LOCK ){. 
dbe50 20 20 20 0a 20 20 20 20 69 66 28 20 70 46 69 6c     .    if( pFil
dbe60 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43  e->locktype==EXC
dbe70 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20  LUSIVE_LOCK ){. 
dbe80 20 20 20 20 20 72 63 20 3d 20 61 66 70 53 65 74       rc = afpSet
dbe90 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50 61 74  Lock(pCtx->dbPat
dbea0 68 2c 20 70 46 69 6c 65 2c 20 53 48 41 52 45 44  h, pFile, SHARED
dbeb0 5f 46 49 52 53 54 2c 20 53 48 41 52 45 44 5f 53  _FIRST, SHARED_S
dbec0 49 5a 45 2c 20 30 29 3b 0a 20 20 20 20 20 20 69  IZE, 0);.      i
dbed0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
dbee0 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48   && locktype==SH
dbef0 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  ARED_LOCK ){.   
dbf00 20 20 20 20 20 2f 2a 20 6f 6e 6c 79 20 72 65 2d       /* only re-
dbf10 65 73 74 61 62 6c 69 73 68 20 74 68 65 20 73 68  establish the sh
dbf20 61 72 65 64 20 6c 6f 63 6b 20 69 66 20 6e 65 63  ared lock if nec
dbf30 65 73 73 61 72 79 20 2a 2f 0a 20 20 20 20 20 20  essary */.      
dbf40 20 20 69 6e 74 20 73 68 61 72 65 64 4c 6f 63 6b    int sharedLock
dbf50 42 79 74 65 20 3d 20 53 48 41 52 45 44 5f 46 49  Byte = SHARED_FI
dbf60 52 53 54 2b 70 43 74 78 2d 3e 73 68 61 72 65 64  RST+pCtx->shared
dbf70 42 79 74 65 3b 0a 20 20 20 20 20 20 20 20 72 63  Byte;.        rc
dbf80 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43   = afpSetLock(pC
dbf90 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c  tx->dbPath, pFil
dbfa0 65 2c 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74  e, sharedLockByt
dbfb0 65 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 20 20  e, 1, 1);.      
dbfc0 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
dbfd0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
dbfe0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
dbff0 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29  >=PENDING_LOCK )
dc000 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 66 70  {.      rc = afp
dc010 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62  SetLock(pCtx->db
dc020 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e  Path, pFile, PEN
dc030 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 30 29  DING_BYTE, 1, 0)
dc040 3b 0a 20 20 20 20 7d 20 0a 20 20 20 20 69 66 28  ;.    } .    if(
dc050 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
dc060 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  & pFile->locktyp
dc070 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e>=RESERVED_LOCK
dc080 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61   ){.      rc = a
dc090 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e  fpSetLock(pCtx->
dc0a0 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52  dbPath, pFile, R
dc0b0 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c  ESERVED_BYTE, 1,
dc0c0 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c   0);.    }.  }el
dc0d0 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d  se if( locktype=
dc0e0 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  =NO_LOCK ){.    
dc0f0 2f 2a 20 63 6c 65 61 72 20 74 68 65 20 73 68 61  /* clear the sha
dc100 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20  red lock */.    
dc110 69 6e 74 20 73 68 61 72 65 64 4c 6f 63 6b 42 79  int sharedLockBy
dc120 74 65 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53  te = SHARED_FIRS
dc130 54 2b 70 43 74 78 2d 3e 73 68 61 72 65 64 42 79  T+pCtx->sharedBy
dc140 74 65 3b 0a 20 20 20 20 72 63 20 3d 20 61 66 70  te;.    rc = afp
dc150 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62  SetLock(pCtx->db
dc160 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 73 68 61  Path, pFile, sha
dc170 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20  redLockByte, 1, 
dc180 30 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72  0);.  }..  if( r
dc190 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
dc1a0 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65      if( locktype
dc1b0 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  ==NO_LOCK ){.   
dc1c0 20 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70     struct unixOp
dc1d0 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70  enCnt *pOpen = p
dc1e0 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20  File->pOpen;.   
dc1f0 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2d     pOpen->nLock-
dc200 2d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  -;.      assert(
dc210 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e 3d 30   pOpen->nLock>=0
dc220 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f   );.      if( pO
dc230 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 29 7b  pen->nLock==0 ){
dc240 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 63 6c  .        rc = cl
dc250 6f 73 65 50 65 6e 64 69 6e 67 46 64 73 28 70 46  osePendingFds(pF
dc260 69 6c 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ile);.      }.  
dc270 20 20 7d 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65    }.  }.  unixLe
dc280 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 66  aveMutex();.  if
dc290 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
dc2a0 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f  ){.    pFile->lo
dc2b0 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70  cktype = locktyp
dc2c0 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  e;.  }.  return 
dc2d0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f  rc;.}../*.** Clo
dc2e0 73 65 20 61 20 66 69 6c 65 20 26 20 63 6c 65 61  se a file & clea
dc2f0 6e 75 70 20 41 46 50 20 73 70 65 63 69 66 69 63  nup AFP specific
dc300 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74   locking context
dc310 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20   .*/.static int 
dc320 61 66 70 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  afpClose(sqlite3
dc330 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69  _file *id) {.  i
dc340 66 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69  f( id ){.    uni
dc350 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
dc360 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20  unixFile*)id;.  
dc370 20 20 61 66 70 55 6e 6c 6f 63 6b 28 69 64 2c 20    afpUnlock(id, 
dc380 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 75 6e  NO_LOCK);.    un
dc390 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a  ixEnterMutex();.
dc3a0 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70      if( pFile->p
dc3b0 4f 70 65 6e 20 26 26 20 70 46 69 6c 65 2d 3e 70  Open && pFile->p
dc3c0 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b 0a 20  Open->nLock ){. 
dc3d0 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65       /* If there
dc3e0 20 61 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67   are outstanding
dc3f0 20 6c 6f 63 6b 73 2c 20 64 6f 20 6e 6f 74 20 61   locks, do not a
dc400 63 74 75 61 6c 6c 79 20 63 6c 6f 73 65 20 74 68  ctually close th
dc410 65 20 66 69 6c 65 20 6a 75 73 74 0a 20 20 20 20  e file just.    
dc420 20 20 2a 2a 20 79 65 74 20 62 65 63 61 75 73 65    ** yet because
dc430 20 74 68 61 74 20 77 6f 75 6c 64 20 63 6c 65 61   that would clea
dc440 72 20 74 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 20  r those locks.  
dc450 49 6e 73 74 65 61 64 2c 20 61 64 64 20 74 68 65  Instead, add the
dc460 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 64   file.      ** d
dc470 65 73 63 72 69 70 74 6f 72 20 74 6f 20 70 4f 70  escriptor to pOp
dc480 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 2e 20 20 49  en->aPending.  I
dc490 74 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61  t will be automa
dc4a0 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65 64 20 77  tically closed w
dc4b0 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  hen.      ** the
dc4c0 20 6c 61 73 74 20 6c 6f 63 6b 20 69 73 20 63 6c   last lock is cl
dc4d0 65 61 72 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a  eared..      */.
dc4e0 20 20 20 20 20 20 73 65 74 50 65 6e 64 69 6e 67        setPending
dc4f0 46 64 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d  Fd(pFile);.    }
dc500 0a 20 20 20 20 72 65 6c 65 61 73 65 4f 70 65 6e  .    releaseOpen
dc510 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 65 6e  Cnt(pFile->pOpen
dc520 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
dc530 72 65 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69  ree(pFile->locki
dc540 6e 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  ngContext);.    
dc550 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64  closeUnixFile(id
dc560 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65  );.    unixLeave
dc570 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72  Mutex();.  }.  r
dc580 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
dc590 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65  .}..#endif /* de
dc5a0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
dc5b0 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   && SQLITE_ENABL
dc5c0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
dc5d0 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64  */./*.** The cod
dc5e0 65 20 61 62 6f 76 65 20 69 73 20 74 68 65 20 41  e above is the A
dc5f0 46 50 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e  FP lock implemen
dc600 74 61 74 69 6f 6e 2e 20 20 54 68 65 20 63 6f 64  tation.  The cod
dc610 65 20 69 73 20 73 70 65 63 69 66 69 63 0a 2a 2a  e is specific.**
dc620 20 74 6f 20 4d 61 63 4f 53 58 20 61 6e 64 20 64   to MacOSX and d
dc630 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20  oes not work on 
dc640 6f 74 68 65 72 20 75 6e 69 78 20 70 6c 61 74 66  other unix platf
dc650 6f 72 6d 73 2e 20 20 4e 6f 20 61 6c 74 65 72 6e  orms.  No altern
dc660 61 74 69 76 65 0a 2a 2a 20 69 73 20 61 76 61 69  ative.** is avai
dc670 6c 61 62 6c 65 2e 20 20 49 66 20 79 6f 75 20 64  lable.  If you d
dc680 6f 6e 27 74 20 63 6f 6d 70 69 6c 65 20 66 6f 72  on't compile for
dc690 20 61 20 6d 61 63 2c 20 74 68 65 6e 20 74 68 65   a mac, then the
dc6a0 20 22 75 6e 69 78 2d 61 66 70 22 0a 2a 2a 20 56   "unix-afp".** V
dc6b0 46 53 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61  FS is not availa
dc6c0 62 6c 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ble..**.********
dc6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
dc6e0 64 20 6f 66 20 74 68 65 20 41 46 50 20 6c 6f 63  d of the AFP loc
dc6f0 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  k implementation
dc700 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
dc710 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  *******.********
dc720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dc730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dc740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dc750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dc760 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a  ******/.../*****
dc770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dc780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dc790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dc7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dc7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a  *********.******
dc7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 6e 2d 6c  ********** Non-l
dc7d0 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 66  ocking sqlite3_f
dc7e0 69 6c 65 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a  ile methods ****
dc7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dc800 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
dc810 54 68 65 20 6e 65 78 74 20 64 69 76 69 73 69 6f  The next divisio
dc820 6e 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65  n contains imple
dc830 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 61  mentations for a
dc840 6c 6c 20 6d 65 74 68 6f 64 73 20 6f 66 20 74 68  ll methods of th
dc850 65 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69  e .** sqlite3_fi
dc860 6c 65 20 6f 62 6a 65 63 74 20 6f 74 68 65 72 20  le object other 
dc870 74 68 61 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67  than the locking
dc880 20 6d 65 74 68 6f 64 73 2e 20 20 54 68 65 20 6c   methods.  The l
dc890 6f 63 6b 69 6e 67 0a 2a 2a 20 6d 65 74 68 6f 64  ocking.** method
dc8a0 73 20 77 65 72 65 20 64 65 66 69 6e 65 64 20 69  s were defined i
dc8b0 6e 20 64 69 76 69 73 69 6f 6e 73 20 61 62 6f 76  n divisions abov
dc8c0 65 20 28 6f 6e 65 20 6c 6f 63 6b 69 6e 67 20 6d  e (one locking m
dc8d0 65 74 68 6f 64 20 70 65 72 0a 2a 2a 20 64 69 76  ethod per.** div
dc8e0 69 73 69 6f 6e 29 2e 20 20 54 68 6f 73 65 20 6d  ision).  Those m
dc8f0 65 74 68 6f 64 73 20 74 68 61 74 20 61 72 65 20  ethods that are 
dc900 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f  common to all lo
dc910 63 6b 69 6e 67 20 6d 6f 64 65 73 0a 2a 2a 20 61  cking modes.** a
dc920 72 65 20 67 61 74 68 65 72 20 74 6f 67 65 74 68  re gather togeth
dc930 65 72 20 69 6e 74 6f 20 74 68 69 73 20 64 69 76  er into this div
dc940 69 73 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ision..*/../*.**
dc950 20 53 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66   Seek to the off
dc960 73 65 74 20 70 61 73 73 65 64 20 61 73 20 74 68  set passed as th
dc970 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
dc980 74 2c 20 74 68 65 6e 20 72 65 61 64 20 63 6e 74  t, then read cnt
dc990 20 0a 2a 2a 20 62 79 74 65 73 20 69 6e 74 6f 20   .** bytes into 
dc9a0 70 42 75 66 2e 20 52 65 74 75 72 6e 20 74 68 65  pBuf. Return the
dc9b0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
dc9c0 20 61 63 74 75 61 6c 6c 79 20 72 65 61 64 2e 0a   actually read..
dc9d0 2a 2a 0a 2a 2a 20 4e 42 3a 20 20 49 66 20 79 6f  **.** NB:  If yo
dc9e0 75 20 64 65 66 69 6e 65 20 55 53 45 5f 50 52 45  u define USE_PRE
dc9f0 41 44 20 6f 72 20 55 53 45 5f 50 52 45 41 44 36  AD or USE_PREAD6
dca00 34 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74  4, then it might
dca10 20 61 6c 73 6f 0a 2a 2a 20 62 65 20 6e 65 63 65   also.** be nece
dca20 73 73 61 72 79 20 74 6f 20 64 65 66 69 6e 65 20  ssary to define 
dca30 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 74 6f  _XOPEN_SOURCE to
dca40 20 62 65 20 35 30 30 2e 20 20 54 68 69 73 20 76   be 500.  This v
dca50 61 72 69 65 73 20 66 72 6f 6d 0a 2a 2a 20 6f 6e  aries from.** on
dca60 65 20 73 79 73 74 65 6d 20 74 6f 20 61 6e 6f 74  e system to anot
dca70 68 65 72 2e 20 20 53 69 6e 63 65 20 53 51 4c 69  her.  Since SQLi
dca80 74 65 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69  te does not defi
dca90 6e 65 20 55 53 45 5f 50 52 45 41 44 0a 2a 2a 20  ne USE_PREAD.** 
dcaa0 61 6e 79 20 61 6e 79 20 66 6f 72 6d 20 62 79 20  any any form by 
dcab0 64 65 66 61 75 6c 74 2c 20 77 65 20 77 69 6c 6c  default, we will
dcac0 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20   not attempt to 
dcad0 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f  define _XOPEN_SO
dcae0 55 52 43 45 2e 0a 2a 2a 20 53 65 65 20 74 69 63  URCE..** See tic
dcaf0 6b 65 74 73 20 23 32 37 34 31 20 61 6e 64 20 23  kets #2741 and #
dcb00 32 36 38 31 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61  2681..**.** To a
dcb10 76 6f 69 64 20 73 74 6f 6d 70 69 6e 67 20 74 68  void stomping th
dcb20 65 20 65 72 72 6e 6f 20 76 61 6c 75 65 20 6f 6e  e errno value on
dcb30 20 61 20 66 61 69 6c 65 64 20 72 65 61 64 20 74   a failed read t
dcb40 68 65 20 6c 61 73 74 45 72 72 6e 6f 20 76 61 6c  he lastErrno val
dcb50 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 62 65 66  ue.** is set bef
dcb60 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
dcb70 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 65  /.static int see
dcb80 6b 41 6e 64 52 65 61 64 28 75 6e 69 78 46 69 6c  kAndRead(unixFil
dcb90 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69  e *id, sqlite3_i
dcba0 6e 74 36 34 20 6f 66 66 73 65 74 2c 20 76 6f 69  nt64 offset, voi
dcbb0 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 63 6e 74  d *pBuf, int cnt
dcbc0 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20  ){.  int got;.  
dcbd0 69 36 34 20 6e 65 77 4f 66 66 73 65 74 3b 0a 20  i64 newOffset;. 
dcbe0 20 54 49 4d 45 52 5f 53 54 41 52 54 3b 0a 23 69   TIMER_START;.#i
dcbf0 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52  f defined(USE_PR
dcc00 45 41 44 29 0a 20 20 67 6f 74 20 3d 20 70 72 65  EAD).  got = pre
dcc10 61 64 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20  ad(id->h, pBuf, 
dcc20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20  cnt, offset);.  
dcc30 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
dcc40 20 67 6f 74 20 3d 20 2d 31 20 29 3b 0a 23 65 6c   got = -1 );.#el
dcc50 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50  if defined(USE_P
dcc60 52 45 41 44 36 34 29 0a 20 20 67 6f 74 20 3d 20  READ64).  got = 
dcc70 70 72 65 61 64 36 34 28 69 64 2d 3e 68 2c 20 70  pread64(id->h, p
dcc80 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74  Buf, cnt, offset
dcc90 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
dcca0 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 29  rror( got = -1 )
dccb0 3b 0a 23 65 6c 73 65 0a 20 20 6e 65 77 4f 66 66  ;.#else.  newOff
dccc0 73 65 74 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e  set = lseek(id->
dccd0 68 2c 20 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f  h, offset, SEEK_
dcce0 53 45 54 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65  SET);.  Simulate
dccf0 49 4f 45 72 72 6f 72 28 20 6e 65 77 4f 66 66 73  IOError( newOffs
dcd00 65 74 2d 2d 20 29 3b 0a 20 20 69 66 28 20 6e 65  et-- );.  if( ne
dcd10 77 4f 66 66 73 65 74 21 3d 6f 66 66 73 65 74 20  wOffset!=offset 
dcd20 29 7b 0a 20 20 20 20 69 66 28 20 6e 65 77 4f 66  ){.    if( newOf
dcd30 66 73 65 74 20 3d 3d 20 2d 31 20 29 7b 0a 20 20  fset == -1 ){.  
dcd40 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29      ((unixFile*)
dcd50 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  id)->lastErrno =
dcd60 20 65 72 72 6e 6f 3b 0a 20 20 20 20 7d 65 6c 73   errno;.    }els
dcd70 65 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46  e{.      ((unixF
dcd80 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72  ile*)id)->lastEr
dcd90 72 6e 6f 20 3d 20 30 3b 09 09 09 0a 20 20 20 20  rno = 0;....    
dcda0 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b  }.    return -1;
dcdb0 0a 20 20 7d 0a 20 20 67 6f 74 20 3d 20 72 65 61  .  }.  got = rea
dcdc0 64 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63  d(id->h, pBuf, c
dcdd0 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 54 49  nt);.#endif.  TI
dcde0 4d 45 52 5f 45 4e 44 3b 0a 20 20 69 66 28 20 67  MER_END;.  if( g
dcdf0 6f 74 3c 30 20 29 7b 0a 20 20 20 20 28 28 75 6e  ot<0 ){.    ((un
dce00 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73  ixFile*)id)->las
dce10 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
dce20 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 35 28 22    }.  OSTRACE5("
dce30 52 45 41 44 20 20 20 20 25 2d 33 64 20 25 35 64  READ    %-3d %5d
dce40 20 25 37 6c 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20   %7lld %llu\n", 
dce50 69 64 2d 3e 68 2c 20 67 6f 74 2c 20 6f 66 66 73  id->h, got, offs
dce60 65 74 2c 20 54 49 4d 45 52 5f 45 4c 41 50 53 45  et, TIMER_ELAPSE
dce70 44 29 3b 0a 20 20 72 65 74 75 72 6e 20 67 6f 74  D);.  return got
dce80 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  ;.}../*.** Read 
dce90 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65  data from a file
dcea0 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20   into a buffer. 
dceb0 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
dcec0 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65  K if all.** byte
dced0 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63 63  s were read succ
dcee0 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c  essfully and SQL
dcef0 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79  ITE_IOERR if any
dcf00 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72  thing goes.** wr
dcf10 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ong..*/.static i
dcf20 6e 74 20 75 6e 69 78 52 65 61 64 28 0a 20 20 73  nt unixRead(.  s
dcf30 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
dcf40 20 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c 20   .  void *pBuf, 
dcf50 0a 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71  .  int amt,.  sq
dcf60 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73  lite3_int64 offs
dcf70 65 74 0a 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65  et.){.  unixFile
dcf80 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
dcf90 69 6c 65 20 2a 29 69 64 3b 0a 20 20 69 6e 74 20  ile *)id;.  int 
dcfa0 67 6f 74 3b 0a 20 20 61 73 73 65 72 74 28 20 69  got;.  assert( i
dcfb0 64 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  d );..  /* If th
dcfc0 69 73 20 69 73 20 61 20 64 61 74 61 62 61 73 65  is is a database
dcfd0 20 66 69 6c 65 20 28 6e 6f 74 20 61 20 6a 6f 75   file (not a jou
dcfe0 72 6e 61 6c 2c 20 6d 61 73 74 65 72 2d 6a 6f 75  rnal, master-jou
dcff0 72 6e 61 6c 20 6f 72 20 74 65 6d 70 0a 20 20 2a  rnal or temp.  *
dd000 2a 20 66 69 6c 65 29 2c 20 74 68 65 20 62 79 74  * file), the byt
dd010 65 73 20 69 6e 20 74 68 65 20 6c 6f 63 6b 69 6e  es in the lockin
dd020 67 20 72 61 6e 67 65 20 73 68 6f 75 6c 64 20 6e  g range should n
dd030 65 76 65 72 20 62 65 20 72 65 61 64 20 6f 72 20  ever be read or 
dd040 77 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 61 73  written. */.  as
dd050 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 70 55 6e  sert( pFile->pUn
dd060 75 73 65 64 3d 3d 30 0a 20 20 20 20 20 20 20 7c  used==0.       |
dd070 7c 20 6f 66 66 73 65 74 3e 3d 50 45 4e 44 49 4e  | offset>=PENDIN
dd080 47 5f 42 59 54 45 2b 35 31 32 0a 20 20 20 20 20  G_BYTE+512.     
dd090 20 20 7c 7c 20 6f 66 66 73 65 74 2b 61 6d 74 3c    || offset+amt<
dd0a0 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 0a 20  =PENDING_BYTE . 
dd0b0 20 29 3b 0a 0a 20 20 67 6f 74 20 3d 20 73 65 65   );..  got = see
dd0c0 6b 41 6e 64 52 65 61 64 28 70 46 69 6c 65 2c 20  kAndRead(pFile, 
dd0d0 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20 61 6d  offset, pBuf, am
dd0e0 74 29 3b 0a 20 20 69 66 28 20 67 6f 74 3d 3d 61  t);.  if( got==a
dd0f0 6d 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  mt ){.    return
dd100 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65   SQLITE_OK;.  }e
dd110 6c 73 65 20 69 66 28 20 67 6f 74 3c 30 20 29 7b  lse if( got<0 ){
dd120 0a 20 20 20 20 2f 2a 20 6c 61 73 74 45 72 72 6e  .    /* lastErrn
dd130 6f 20 73 65 74 20 62 79 20 73 65 65 6b 41 6e 64  o set by seekAnd
dd140 52 65 61 64 20 2a 2f 0a 20 20 20 20 72 65 74 75  Read */.    retu
dd150 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
dd160 52 45 41 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  READ;.  }else{. 
dd170 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
dd180 72 6e 6f 20 3d 20 30 3b 20 2f 2a 20 6e 6f 74 20  rno = 0; /* not 
dd190 61 20 73 79 73 74 65 6d 20 65 72 72 6f 72 20 2a  a system error *
dd1a0 2f 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 20  /.    /* Unread 
dd1b0 70 61 72 74 73 20 6f 66 20 74 68 65 20 62 75 66  parts of the buf
dd1c0 66 65 72 20 6d 75 73 74 20 62 65 20 7a 65 72 6f  fer must be zero
dd1d0 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 6d  -filled */.    m
dd1e0 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 70  emset(&((char*)p
dd1f0 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d  Buf)[got], 0, am
dd200 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 74 75  t-got);.    retu
dd210 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
dd220 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a  SHORT_READ;.  }.
dd230 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 65 6b 20 74 6f  }../*.** Seek to
dd240 20 74 68 65 20 6f 66 66 73 65 74 20 69 6e 20 69   the offset in i
dd250 64 2d 3e 6f 66 66 73 65 74 20 74 68 65 6e 20 72  d->offset then r
dd260 65 61 64 20 63 6e 74 20 62 79 74 65 73 20 69 6e  ead cnt bytes in
dd270 74 6f 20 70 42 75 66 2e 0a 2a 2a 20 52 65 74 75  to pBuf..** Retu
dd280 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
dd290 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20   bytes actually 
dd2a0 72 65 61 64 2e 20 20 55 70 64 61 74 65 20 74 68  read.  Update th
dd2b0 65 20 6f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20  e offset..**.** 
dd2c0 54 6f 20 61 76 6f 69 64 20 73 74 6f 6d 70 69 6e  To avoid stompin
dd2d0 67 20 74 68 65 20 65 72 72 6e 6f 20 76 61 6c 75  g the errno valu
dd2e0 65 20 6f 6e 20 61 20 66 61 69 6c 65 64 20 77 72  e on a failed wr
dd2f0 69 74 65 20 74 68 65 20 6c 61 73 74 45 72 72 6e  ite the lastErrn
dd300 6f 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65  o value.** is se
dd310 74 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  t before returni
dd320 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ng..*/.static in
dd330 74 20 73 65 65 6b 41 6e 64 57 72 69 74 65 28 75  t seekAndWrite(u
dd340 6e 69 78 46 69 6c 65 20 2a 69 64 2c 20 69 36 34  nixFile *id, i64
dd350 20 6f 66 66 73 65 74 2c 20 63 6f 6e 73 74 20 76   offset, const v
dd360 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 63  oid *pBuf, int c
dd370 6e 74 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a  nt){.  int got;.
dd380 20 20 69 36 34 20 6e 65 77 4f 66 66 73 65 74 3b    i64 newOffset;
dd390 0a 20 20 54 49 4d 45 52 5f 53 54 41 52 54 3b 0a  .  TIMER_START;.
dd3a0 23 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f  #if defined(USE_
dd3b0 50 52 45 41 44 29 0a 20 20 67 6f 74 20 3d 20 70  PREAD).  got = p
dd3c0 77 72 69 74 65 28 69 64 2d 3e 68 2c 20 70 42 75  write(id->h, pBu
dd3d0 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b  f, cnt, offset);
dd3e0 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 55  .#elif defined(U
dd3f0 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20 67 6f  SE_PREAD64).  go
dd400 74 20 3d 20 70 77 72 69 74 65 36 34 28 69 64 2d  t = pwrite64(id-
dd410 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f  >h, pBuf, cnt, o
dd420 66 66 73 65 74 29 3b 0a 23 65 6c 73 65 0a 20 20  ffset);.#else.  
dd430 6e 65 77 4f 66 66 73 65 74 20 3d 20 6c 73 65 65  newOffset = lsee
dd440 6b 28 69 64 2d 3e 68 2c 20 6f 66 66 73 65 74 2c  k(id->h, offset,
dd450 20 53 45 45 4b 5f 53 45 54 29 3b 0a 20 20 69 66   SEEK_SET);.  if
dd460 28 20 6e 65 77 4f 66 66 73 65 74 21 3d 6f 66 66  ( newOffset!=off
dd470 73 65 74 20 29 7b 0a 20 20 20 20 69 66 28 20 6e  set ){.    if( n
dd480 65 77 4f 66 66 73 65 74 20 3d 3d 20 2d 31 20 29  ewOffset == -1 )
dd490 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 69  {.      ((unixFi
dd4a0 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72  le*)id)->lastErr
dd4b0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
dd4c0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 28 75  }else{.      ((u
dd4d0 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61  nixFile*)id)->la
dd4e0 73 74 45 72 72 6e 6f 20 3d 20 30 3b 09 09 09 0a  stErrno = 0;....
dd4f0 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
dd500 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f 74 20 3d   -1;.  }.  got =
dd510 20 77 72 69 74 65 28 69 64 2d 3e 68 2c 20 70 42   write(id->h, pB
dd520 75 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 69 66  uf, cnt);.#endif
dd530 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20  .  TIMER_END;.  
dd540 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20  if( got<0 ){.   
dd550 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29   ((unixFile*)id)
dd560 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
dd570 72 6e 6f 3b 0a 20 20 7d 0a 0a 20 20 4f 53 54 52  rno;.  }..  OSTR
dd580 41 43 45 35 28 22 57 52 49 54 45 20 20 20 25 2d  ACE5("WRITE   %-
dd590 33 64 20 25 35 64 20 25 37 6c 6c 64 20 25 6c 6c  3d %5d %7lld %ll
dd5a0 75 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f 74  u\n", id->h, got
dd5b0 2c 20 6f 66 66 73 65 74 2c 20 54 49 4d 45 52 5f  , offset, TIMER_
dd5c0 45 4c 41 50 53 45 44 29 3b 0a 20 20 72 65 74 75  ELAPSED);.  retu
dd5d0 72 6e 20 67 6f 74 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  rn got;.}.../*.*
dd5e0 2a 20 57 72 69 74 65 20 64 61 74 61 20 66 72 6f  * Write data fro
dd5f0 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20  m a buffer into 
dd600 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20  a file.  Return 
dd610 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63  SQLITE_OK on suc
dd620 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20  cess.** or some 
dd630 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65  other error code
dd640 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a   on failure..*/.
dd650 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 57  static int unixW
dd660 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  rite(.  sqlite3_
dd670 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20 63 6f 6e  file *id, .  con
dd680 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 0a  st void *pBuf, .
dd690 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 6c    int amt,.  sql
dd6a0 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65  ite3_int64 offse
dd6b0 74 20 0a 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65  t .){.  unixFile
dd6c0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
dd6d0 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 77  ile*)id;.  int w
dd6e0 72 6f 74 65 20 3d 20 30 3b 0a 20 20 61 73 73 65  rote = 0;.  asse
dd6f0 72 74 28 20 69 64 20 29 3b 0a 20 20 61 73 73 65  rt( id );.  asse
dd700 72 74 28 20 61 6d 74 3e 30 20 29 3b 0a 0a 20 20  rt( amt>0 );..  
dd710 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20  /* If this is a 
dd720 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 6e  database file (n
dd730 6f 74 20 61 20 6a 6f 75 72 6e 61 6c 2c 20 6d 61  ot a journal, ma
dd740 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6f 72 20  ster-journal or 
dd750 74 65 6d 70 0a 20 20 2a 2a 20 66 69 6c 65 29 2c  temp.  ** file),
dd760 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 74 68   the bytes in th
dd770 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20  e locking range 
dd780 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20  should never be 
dd790 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e  read or written.
dd7a0 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46   */.  assert( pF
dd7b0 69 6c 65 2d 3e 70 55 6e 75 73 65 64 3d 3d 30 0a  ile->pUnused==0.
dd7c0 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 65 74         || offset
dd7d0 3e 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35  >=PENDING_BYTE+5
dd7e0 31 32 0a 20 20 20 20 20 20 20 7c 7c 20 6f 66 66  12.       || off
dd7f0 73 65 74 2b 61 6d 74 3c 3d 50 45 4e 44 49 4e 47  set+amt<=PENDING
dd800 5f 42 59 54 45 20 0a 20 20 29 3b 0a 0a 23 69 66  _BYTE .  );..#if
dd810 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a  ndef NDEBUG.  /*
dd820 20 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e 67   If we are doing
dd830 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74 65 20   a normal write 
dd840 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 66 69  to a database fi
dd850 6c 65 20 28 61 73 20 6f 70 70 6f 73 65 64 20 74  le (as opposed t
dd860 6f 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 61 20 68  o.  ** doing a h
dd870 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62  ot-journal rollb
dd880 61 63 6b 20 6f 72 20 61 20 77 72 69 74 65 20 74  ack or a write t
dd890 6f 20 73 6f 6d 65 20 66 69 6c 65 20 6f 74 68 65  o some file othe
dd8a0 72 20 74 68 61 6e 20 61 0a 20 20 2a 2a 20 6e 6f  r than a.  ** no
dd8b0 72 6d 61 6c 20 64 61 74 61 62 61 73 65 20 66 69  rmal database fi
dd8c0 6c 65 29 20 74 68 65 6e 20 72 65 63 6f 72 64 20  le) then record 
dd8d0 74 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68  the fact that th
dd8e0 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20  e database.  ** 
dd8f0 68 61 73 20 63 68 61 6e 67 65 64 2e 20 20 49 66  has changed.  If
dd900 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
dd910 20 63 6f 75 6e 74 65 72 20 69 73 20 6d 6f 64 69   counter is modi
dd920 66 69 65 64 2c 20 72 65 63 6f 72 64 20 74 68 61  fied, record tha
dd930 74 0a 20 20 2a 2a 20 66 61 63 74 20 74 6f 6f 2e  t.  ** fact too.
dd940 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c  .  */.  if( pFil
dd950 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65  e->inNormalWrite
dd960 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64   ){.    pFile->d
dd970 62 55 70 64 61 74 65 20 3d 20 31 3b 20 20 2f 2a  bUpdate = 1;  /*
dd980 20 54 68 65 20 64 61 74 61 62 61 73 65 20 68 61   The database ha
dd990 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20  s been modified 
dd9a0 2a 2f 0a 20 20 20 20 69 66 28 20 6f 66 66 73 65  */.    if( offse
dd9b0 74 3c 3d 32 34 20 26 26 20 6f 66 66 73 65 74 2b  t<=24 && offset+
dd9c0 61 6d 74 3e 3d 32 37 20 29 7b 0a 20 20 20 20 20  amt>=27 ){.     
dd9d0 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 63   int rc;.      c
dd9e0 68 61 72 20 6f 6c 64 43 6e 74 72 5b 34 5d 3b 0a  har oldCntr[4];.
dd9f0 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f        SimulateIO
dda00 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a  ErrorBenign(1);.
dda10 20 20 20 20 20 20 72 63 20 3d 20 73 65 65 6b 41        rc = seekA
dda20 6e 64 52 65 61 64 28 70 46 69 6c 65 2c 20 32 34  ndRead(pFile, 24
dda30 2c 20 6f 6c 64 43 6e 74 72 2c 20 34 29 3b 0a 20  , oldCntr, 4);. 
dda40 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45       SimulateIOE
dda50 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 20  rrorBenign(0);. 
dda60 20 20 20 20 20 69 66 28 20 72 63 21 3d 34 20 7c       if( rc!=4 |
dda70 7c 20 6d 65 6d 63 6d 70 28 6f 6c 64 43 6e 74 72  | memcmp(oldCntr
dda80 2c 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 29  , &((char*)pBuf)
dda90 5b 32 34 2d 6f 66 66 73 65 74 5d 2c 20 34 29 21  [24-offset], 4)!
ddaa0 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46  =0 ){.        pF
ddab0 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68  ile->transCntrCh
ddac0 6e 67 20 3d 20 31 3b 20 20 2f 2a 20 54 68 65 20  ng = 1;  /* The 
ddad0 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e  transaction coun
ddae0 74 65 72 20 68 61 73 20 63 68 61 6e 67 65 64 20  ter has changed 
ddaf0 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  */.      }.    }
ddb00 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 77  .  }.#endif..  w
ddb10 68 69 6c 65 28 20 61 6d 74 3e 30 20 26 26 20 28  hile( amt>0 && (
ddb20 77 72 6f 74 65 20 3d 20 73 65 65 6b 41 6e 64 57  wrote = seekAndW
ddb30 72 69 74 65 28 70 46 69 6c 65 2c 20 6f 66 66 73  rite(pFile, offs
ddb40 65 74 2c 20 70 42 75 66 2c 20 61 6d 74 29 29 3e  et, pBuf, amt))>
ddb50 30 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20  0 ){.    amt -= 
ddb60 77 72 6f 74 65 3b 0a 20 20 20 20 6f 66 66 73 65  wrote;.    offse
ddb70 74 20 2b 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20  t += wrote;.    
ddb80 70 42 75 66 20 3d 20 26 28 28 63 68 61 72 2a 29  pBuf = &((char*)
ddb90 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20  pBuf)[wrote];.  
ddba0 7d 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  }.  SimulateIOEr
ddbb0 72 6f 72 28 28 20 77 72 6f 74 65 3d 28 2d 31 29  ror(( wrote=(-1)
ddbc0 2c 20 61 6d 74 3d 31 20 29 29 3b 0a 20 20 53 69  , amt=1 ));.  Si
ddbd0 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72  mulateDiskfullEr
ddbe0 72 6f 72 28 28 20 77 72 6f 74 65 3d 30 2c 20 61  ror(( wrote=0, a
ddbf0 6d 74 3d 31 20 29 29 3b 0a 20 20 69 66 28 20 61  mt=1 ));.  if( a
ddc00 6d 74 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20  mt>0 ){.    if( 
ddc10 77 72 6f 74 65 3c 30 20 29 7b 0a 20 20 20 20 20  wrote<0 ){.     
ddc20 20 2f 2a 20 6c 61 73 74 45 72 72 6e 6f 20 73 65   /* lastErrno se
ddc30 74 20 62 79 20 73 65 65 6b 41 6e 64 57 72 69 74  t by seekAndWrit
ddc40 65 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72  e */.      retur
ddc50 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57  n SQLITE_IOERR_W
ddc60 52 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  RITE;.    }else{
ddc70 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  .      pFile->la
ddc80 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a 20  stErrno = 0; /* 
ddc90 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 65 72 72  not a system err
ddca0 6f 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75  or */.      retu
ddcb0 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a  rn SQLITE_FULL;.
ddcc0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
ddcd0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
ddce0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
ddcf0 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20  EST./*.** Count 
ddd00 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75  the number of fu
ddd10 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d  llsyncs and norm
ddd20 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20  al syncs.  This 
ddd30 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a  is used to test.
ddd40 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e  ** that syncs an
ddd50 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20  d fullsyncs are 
ddd60 6f 63 63 75 72 72 69 6e 67 20 61 74 20 74 68 65  occurring at the
ddd70 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f   right times..*/
ddd80 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
ddd90 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75  sqlite3_sync_cou
ddda0 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41  nt = 0;.SQLITE_A
dddb0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66  PI int sqlite3_f
dddc0 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20  ullsync_count = 
dddd0 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  0;.#endif../*.**
ddde0 20 57 65 20 64 6f 20 6e 6f 74 20 74 72 75 73 74   We do not trust
dddf0 20 73 79 73 74 65 6d 73 20 74 6f 20 70 72 6f 76   systems to prov
dde00 69 64 65 20 61 20 77 6f 72 6b 69 6e 67 20 66 64  ide a working fd
dde10 61 74 61 73 79 6e 63 28 29 2e 20 20 53 6f 6d 65  atasync().  Some
dde20 20 64 6f 2e 0a 2a 2a 20 4f 74 68 65 72 73 20 64   do..** Others d
dde30 6f 20 6e 6f 2e 20 20 54 6f 20 62 65 20 73 61 66  o no.  To be saf
dde40 65 2c 20 77 65 20 77 69 6c 6c 20 73 74 69 63 6b  e, we will stick
dde50 20 77 69 74 68 20 74 68 65 20 28 73 6c 6f 77 65   with the (slowe
dde60 72 29 20 66 73 79 6e 63 28 29 2e 0a 2a 2a 20 49  r) fsync()..** I
dde70 66 20 79 6f 75 20 6b 6e 6f 77 20 74 68 61 74 20  f you know that 
dde80 79 6f 75 72 20 73 79 73 74 65 6d 20 64 6f 65 73  your system does
dde90 20 73 75 70 70 6f 72 74 20 66 64 61 74 61 73 79   support fdatasy
ddea0 6e 63 28 29 20 63 6f 72 72 65 63 74 6c 79 2c 0a  nc() correctly,.
ddeb0 2a 2a 20 74 68 65 6e 20 73 69 6d 70 6c 79 20 63  ** then simply c
ddec0 6f 6d 70 69 6c 65 20 77 69 74 68 20 2d 44 66 64  ompile with -Dfd
dded0 61 74 61 73 79 6e 63 3d 66 64 61 74 61 73 79 6e  atasync=fdatasyn
ddee0 63 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65  c.*/.#if !define
ddef0 64 28 66 64 61 74 61 73 79 6e 63 29 20 26 26 20  d(fdatasync) && 
ddf00 21 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78  !defined(__linux
ddf10 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20 66 64 61  __).# define fda
ddf20 74 61 73 79 6e 63 20 66 73 79 6e 63 0a 23 65 6e  tasync fsync.#en
ddf30 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e  dif../*.** Defin
ddf40 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43  e HAVE_FULLFSYNC
ddf50 20 74 6f 20 30 20 6f 72 20 31 20 64 65 70 65 6e   to 0 or 1 depen
ddf60 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 20  ding on whether 
ddf70 6f 72 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 46 5f  or not.** the F_
ddf80 46 55 4c 4c 46 53 59 4e 43 20 6d 61 63 72 6f 20  FULLFSYNC macro 
ddf90 69 73 20 64 65 66 69 6e 65 64 2e 20 20 46 5f 46  is defined.  F_F
ddfa0 55 4c 4c 46 53 59 4e 43 20 69 73 20 63 75 72 72  ULLFSYNC is curr
ddfb0 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 6c 79 20 61 76  ently.** only av
ddfc0 61 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 20 4f  ailable on Mac O
ddfd0 53 20 58 2e 20 20 42 75 74 20 74 68 61 74 20 63  S X.  But that c
ddfe0 6f 75 6c 64 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a  ould change..*/.
ddff0 23 69 66 64 65 66 20 46 5f 46 55 4c 4c 46 53 59  #ifdef F_FULLFSY
de000 4e 43 0a 23 20 64 65 66 69 6e 65 20 48 41 56 45  NC.# define HAVE
de010 5f 46 55 4c 4c 46 53 59 4e 43 20 31 0a 23 65 6c  _FULLFSYNC 1.#el
de020 73 65 0a 23 20 64 65 66 69 6e 65 20 48 41 56 45  se.# define HAVE
de030 5f 46 55 4c 4c 46 53 59 4e 43 20 30 0a 23 65 6e  _FULLFSYNC 0.#en
de040 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  dif.../*.** The 
de050 66 73 79 6e 63 28 29 20 73 79 73 74 65 6d 20 63  fsync() system c
de060 61 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72  all does not wor
de070 6b 20 61 73 20 61 64 76 65 72 74 69 73 65 64 20  k as advertised 
de080 6f 6e 20 6d 61 6e 79 0a 2a 2a 20 75 6e 69 78 20  on many.** unix 
de090 73 79 73 74 65 6d 73 2e 20 20 54 68 65 20 66 6f  systems.  The fo
de0a0 6c 6c 6f 77 69 6e 67 20 70 72 6f 63 65 64 75 72  llowing procedur
de0b0 65 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74 20  e is an attempt 
de0c0 74 6f 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 77 6f  to make.** it wo
de0d0 72 6b 20 62 65 74 74 65 72 2e 0a 2a 2a 0a 2a 2a  rk better..**.**
de0e0 20 54 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53   The SQLITE_NO_S
de0f0 59 4e 43 20 6d 61 63 72 6f 20 64 69 73 61 62 6c  YNC macro disabl
de100 65 73 20 61 6c 6c 20 66 73 79 6e 63 28 29 73 2e  es all fsync()s.
de110 20 20 54 68 69 73 20 69 73 20 75 73 65 66 75 6c    This is useful
de120 0a 2a 2a 20 66 6f 72 20 74 65 73 74 69 6e 67 20  .** for testing 
de130 77 68 65 6e 20 77 65 20 77 61 6e 74 20 74 6f 20  when we want to 
de140 72 75 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20  run through the 
de150 74 65 73 74 20 73 75 69 74 65 20 71 75 69 63 6b  test suite quick
de160 6c 79 2e 0a 2a 2a 20 59 6f 75 20 61 72 65 20 73  ly..** You are s
de170 74 72 6f 6e 67 6c 79 20 61 64 76 69 73 65 64 20  trongly advised 
de180 2a 6e 6f 74 2a 20 74 6f 20 64 65 70 6c 6f 79 20  *not* to deploy 
de190 77 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53  with SQLITE_NO_S
de1a0 59 4e 43 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20  YNC.** enabled, 
de1b0 68 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20 77  however, since w
de1c0 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59  ith SQLITE_NO_SY
de1d0 4e 43 20 65 6e 61 62 6c 65 64 2c 20 61 6e 20 4f  NC enabled, an O
de1e0 53 20 63 72 61 73 68 0a 2a 2a 20 6f 72 20 70 6f  S crash.** or po
de1f0 77 65 72 20 66 61 69 6c 75 72 65 20 77 69 6c 6c  wer failure will
de200 20 6c 69 6b 65 6c 79 20 63 6f 72 72 75 70 74 20   likely corrupt 
de210 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
de220 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  e..**.** SQLite 
de230 73 65 74 73 20 74 68 65 20 64 61 74 61 4f 6e 6c  sets the dataOnl
de240 79 20 66 6c 61 67 20 69 66 20 74 68 65 20 73 69  y flag if the si
de250 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69  ze of the file i
de260 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 20  s unchanged..** 
de270 54 68 65 20 69 64 65 61 20 62 65 68 69 6e 64 20  The idea behind 
de280 64 61 74 61 4f 6e 6c 79 20 69 73 20 74 68 61 74  dataOnly is that
de290 20 69 74 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20   it should only 
de2a0 77 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 63  write the file c
de2b0 6f 6e 74 65 6e 74 0a 2a 2a 20 74 6f 20 64 69 73  ontent.** to dis
de2c0 6b 2c 20 6e 6f 74 20 74 68 65 20 69 6e 6f 64 65  k, not the inode
de2d0 2e 20 20 57 65 20 6f 6e 6c 79 20 73 65 74 20 64  .  We only set d
de2e0 61 74 61 4f 6e 6c 79 20 69 66 20 74 68 65 20 66  ataOnly if the f
de2f0 69 6c 65 20 73 69 7a 65 20 69 73 20 0a 2a 2a 20  ile size is .** 
de300 75 6e 63 68 61 6e 67 65 64 20 73 69 6e 63 65 20  unchanged since 
de310 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 69 73  the file size is
de320 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e 6f   part of the ino
de330 64 65 2e 20 20 48 6f 77 65 76 65 72 2c 20 0a 2a  de.  However, .*
de340 2a 20 54 65 64 20 54 73 27 6f 20 74 65 6c 6c 73  * Ted Ts'o tells
de350 20 75 73 20 74 68 61 74 20 66 64 61 74 61 73 79   us that fdatasy
de360 6e 63 28 29 20 77 69 6c 6c 20 61 6c 73 6f 20 77  nc() will also w
de370 72 69 74 65 20 74 68 65 20 69 6e 6f 64 65 20 69  rite the inode i
de380 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 73 69  f the.** file si
de390 7a 65 20 68 61 73 20 63 68 61 6e 67 65 64 2e 20  ze has changed. 
de3a0 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 6c 20 64   The only real d
de3b0 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65  ifference betwee
de3c0 6e 20 66 64 61 74 61 73 79 6e 63 28 29 0a 2a 2a  n fdatasync().**
de3d0 20 61 6e 64 20 66 73 79 6e 63 28 29 2c 20 54 65   and fsync(), Te
de3e0 64 20 74 65 6c 6c 73 20 75 73 2c 20 69 73 20 74  d tells us, is t
de3f0 68 61 74 20 66 64 61 74 61 73 79 6e 63 28 29 20  hat fdatasync() 
de400 77 69 6c 6c 20 6e 6f 74 20 66 6c 75 73 68 20 74  will not flush t
de410 68 65 0a 2a 2a 20 69 6e 6f 64 65 20 69 66 20 74  he.** inode if t
de420 68 65 20 6d 74 69 6d 65 20 6f 72 20 6f 77 6e 65  he mtime or owne
de430 72 20 6f 72 20 6f 74 68 65 72 20 69 6e 6f 64 65  r or other inode
de440 20 61 74 74 72 69 62 75 74 65 73 20 68 61 76 65   attributes have
de450 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 57 65 20   changed..** We 
de460 6f 6e 6c 79 20 63 61 72 65 20 61 62 6f 75 74 20  only care about 
de470 74 68 65 20 66 69 6c 65 20 73 69 7a 65 2c 20 6e  the file size, n
de480 6f 74 20 74 68 65 20 6f 74 68 65 72 20 66 69 6c  ot the other fil
de490 65 20 61 74 74 72 69 62 75 74 65 73 2c 20 73 6f  e attributes, so
de4a0 0a 2a 2a 20 61 73 20 66 61 72 20 61 73 20 53 51  .** as far as SQ
de4b0 4c 69 74 65 20 69 73 20 63 6f 6e 63 65 72 6e 65  Lite is concerne
de4c0 64 2c 20 61 6e 20 66 64 61 74 61 73 79 6e 63 28  d, an fdatasync(
de4d0 29 20 69 73 20 61 6c 77 61 79 73 20 61 64 65 71  ) is always adeq
de4e0 75 61 74 65 2e 0a 2a 2a 20 53 6f 2c 20 77 65 20  uate..** So, we 
de4f0 61 6c 77 61 79 73 20 75 73 65 20 66 64 61 74 61  always use fdata
de500 73 79 6e 63 28 29 20 69 66 20 69 74 20 69 73 20  sync() if it is 
de510 61 76 61 69 6c 61 62 6c 65 2c 20 72 65 67 61 72  available, regar
de520 64 6c 65 73 73 20 6f 66 0a 2a 2a 20 74 68 65 20  dless of.** the 
de530 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74  value of the dat
de540 61 4f 6e 6c 79 20 66 6c 61 67 2e 0a 2a 2f 0a 73  aOnly flag..*/.s
de550 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 5f 66  tatic int full_f
de560 73 79 6e 63 28 69 6e 74 20 66 64 2c 20 69 6e 74  sync(int fd, int
de570 20 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74 20 64   fullSync, int d
de580 61 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e 74 20  ataOnly){.  int 
de590 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f  rc;..  /* The fo
de5a0 6c 6c 6f 77 69 6e 67 20 22 69 66 64 65 66 2f 65  llowing "ifdef/e
de5b0 6c 69 66 2f 65 6c 73 65 2f 22 20 62 6c 6f 63 6b  lif/else/" block
de5c0 20 68 61 73 20 74 68 65 20 73 61 6d 65 20 73 74   has the same st
de5d0 72 75 63 74 75 72 65 20 61 73 0a 20 20 2a 2a 20  ructure as.  ** 
de5e0 74 68 65 20 6f 6e 65 20 62 65 6c 6f 77 2e 20 49  the one below. I
de5f0 74 20 69 73 20 72 65 70 6c 69 63 61 74 65 64 20  t is replicated 
de600 68 65 72 65 20 73 6f 6c 65 6c 79 20 74 6f 20 61  here solely to a
de610 76 6f 69 64 20 63 6c 75 74 74 65 72 69 6e 67 20  void cluttering 
de620 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 72 65 61  .  ** up the rea
de630 6c 20 63 6f 64 65 20 77 69 74 68 20 74 68 65 20  l code with the 
de640 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
de650 28 29 20 6d 61 63 72 6f 73 2e 0a 20 20 2a 2f 0a  () macros..  */.
de660 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f  #ifdef SQLITE_NO
de670 5f 53 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50  _SYNC.  UNUSED_P
de680 41 52 41 4d 45 54 45 52 28 66 64 29 3b 0a 20 20  ARAMETER(fd);.  
de690 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
de6a0 28 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 55 4e  (fullSync);.  UN
de6b0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64  USED_PARAMETER(d
de6c0 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c 69 66 20  ataOnly);.#elif 
de6d0 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 0a 20  HAVE_FULLFSYNC. 
de6e0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
de6f0 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c  R(dataOnly);.#el
de700 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  se.  UNUSED_PARA
de710 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b  METER(fullSync);
de720 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
de730 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23  TER(dataOnly);.#
de740 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 63 6f  endif..  /* Reco
de750 72 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rd the number of
de760 20 74 69 6d 65 73 20 74 68 61 74 20 77 65 20 64   times that we d
de770 6f 20 61 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63  o a normal fsync
de780 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 46 55 4c  () and .  ** FUL
de790 4c 53 59 4e 43 2e 20 20 54 68 69 73 20 69 73 20  LSYNC.  This is 
de7a0 75 73 65 64 20 64 75 72 69 6e 67 20 74 65 73 74  used during test
de7b0 69 6e 67 20 74 6f 20 76 65 72 69 66 79 20 74 68  ing to verify th
de7c0 61 74 20 74 68 69 73 20 70 72 6f 63 65 64 75 72  at this procedur
de7d0 65 0a 20 20 2a 2a 20 67 65 74 73 20 63 61 6c 6c  e.  ** gets call
de7e0 65 64 20 77 69 74 68 20 74 68 65 20 63 6f 72 72  ed with the corr
de7f0 65 63 74 20 61 72 67 75 6d 65 6e 74 73 2e 0a 20  ect arguments.. 
de800 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
de810 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 75 6c  E_TEST.  if( ful
de820 6c 53 79 6e 63 20 29 20 73 71 6c 69 74 65 33 5f  lSync ) sqlite3_
de830 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b  fullsync_count++
de840 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 6e 63  ;.  sqlite3_sync
de850 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66  _count++;.#endif
de860 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d  ..  /* If we com
de870 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 53  piled with the S
de880 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c  QLITE_NO_SYNC fl
de890 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69 6e 67  ag, then syncing
de8a0 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70   is a.  ** no-op
de8b0 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  .  */.#ifdef SQL
de8c0 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 72 63  ITE_NO_SYNC.  rc
de8d0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65   = SQLITE_OK;.#e
de8e0 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59  lif HAVE_FULLFSY
de8f0 4e 43 0a 20 20 69 66 28 20 66 75 6c 6c 53 79 6e  NC.  if( fullSyn
de900 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 63  c ){.    rc = fc
de910 6e 74 6c 28 66 64 2c 20 46 5f 46 55 4c 4c 46 53  ntl(fd, F_FULLFS
de920 59 4e 43 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65  YNC, 0);.  }else
de930 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20  {.    rc = 1;.  
de940 7d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 46 55  }.  /* If the FU
de950 4c 4c 46 53 59 4e 43 20 66 61 69 6c 65 64 2c 20  LLFSYNC failed, 
de960 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 61 74 74  fall back to att
de970 65 6d 70 74 69 6e 67 20 61 6e 20 66 73 79 6e 63  empting an fsync
de980 28 29 2e 0a 20 20 2a 2a 20 49 74 20 73 68 6f 75  ()..  ** It shou
de990 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69 62 6c  ldn't be possibl
de9a0 65 20 66 6f 72 20 66 75 6c 6c 66 73 79 6e 63 20  e for fullfsync 
de9b0 74 6f 20 66 61 69 6c 20 6f 6e 20 74 68 65 20 6c  to fail on the l
de9c0 6f 63 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20  ocal .  ** file 
de9d0 73 79 73 74 65 6d 20 28 6f 6e 20 4f 53 58 29 2c  system (on OSX),
de9e0 20 73 6f 20 66 61 69 6c 75 72 65 20 69 6e 64 69   so failure indi
de9f0 63 61 74 65 73 20 74 68 61 74 20 46 55 4c 4c 46  cates that FULLF
dea00 53 59 4e 43 0a 20 20 2a 2a 20 69 73 6e 27 74 20  SYNC.  ** isn't 
dea10 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20 74 68  supported for th
dea20 69 73 20 66 69 6c 65 20 73 79 73 74 65 6d 2e 20  is file system. 
dea30 53 6f 2c 20 61 74 74 65 6d 70 74 20 61 6e 20 66  So, attempt an f
dea40 73 79 6e 63 20 0a 20 20 2a 2a 20 61 6e 64 20 28  sync .  ** and (
dea50 66 6f 72 20 6e 6f 77 29 20 69 67 6e 6f 72 65 20  for now) ignore 
dea60 74 68 65 20 6f 76 65 72 68 65 61 64 20 6f 66 20  the overhead of 
dea70 61 20 73 75 70 65 72 66 6c 75 6f 75 73 20 66 63  a superfluous fc
dea80 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a 20 20 2a 2a  ntl call.  .  **
dea90 20 49 74 27 64 20 62 65 20 62 65 74 74 65 72 20   It'd be better 
deaa0 74 6f 20 64 65 74 65 63 74 20 66 75 6c 6c 66 73  to detect fullfs
deab0 79 6e 63 20 73 75 70 70 6f 72 74 20 6f 6e 63 65  ync support once
deac0 20 61 6e 64 20 61 76 6f 69 64 20 0a 20 20 2a 2a   and avoid .  **
dead0 20 74 68 65 20 66 63 6e 74 6c 20 63 61 6c 6c 20   the fcntl call 
deae0 65 76 65 72 79 20 74 69 6d 65 20 73 79 6e 63 20  every time sync 
deaf0 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20 2a 2f 0a  is called..  */.
deb00 20 20 69 66 28 20 72 63 20 29 20 72 63 20 3d 20    if( rc ) rc = 
deb10 66 73 79 6e 63 28 66 64 29 3b 0a 0a 23 65 6c 73  fsync(fd);..#els
deb20 65 20 0a 20 20 72 63 20 3d 20 66 64 61 74 61 73  e .  rc = fdatas
deb30 79 6e 63 28 66 64 29 3b 0a 23 69 66 20 4f 53 5f  ync(fd);.#if OS_
deb40 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72 63  VXWORKS.  if( rc
deb50 3d 3d 2d 31 20 26 26 20 65 72 72 6e 6f 3d 3d 45  ==-1 && errno==E
deb60 4e 4f 54 53 55 50 20 29 7b 0a 20 20 20 20 72 63  NOTSUP ){.    rc
deb70 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 20 20   = fsync(fd);.  
deb80 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56  }.#endif /* OS_V
deb90 58 57 4f 52 4b 53 20 2a 2f 0a 23 65 6e 64 69 66  XWORKS */.#endif
deba0 20 2f 2a 20 69 66 64 65 66 20 53 51 4c 49 54 45   /* ifdef SQLITE
debb0 5f 4e 4f 5f 53 59 4e 43 20 65 6c 69 66 20 48 41  _NO_SYNC elif HA
debc0 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 2a 2f 0a  VE_FULLFSYNC */.
debd0 0a 20 20 69 66 28 20 4f 53 5f 56 58 57 4f 52 4b  .  if( OS_VXWORK
debe0 53 20 26 26 20 72 63 21 3d 20 2d 31 20 29 7b 0a  S && rc!= -1 ){.
debf0 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 7d 0a      rc = 0;.  }.
dec00 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
dec10 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  /*.** Make sure 
dec20 61 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 61 20  all writes to a 
dec30 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20  particular file 
dec40 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f  are committed to
dec50 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   disk..**.** If 
dec60 64 61 74 61 4f 6e 6c 79 3d 3d 30 20 74 68 65 6e  dataOnly==0 then
dec70 20 62 6f 74 68 20 74 68 65 20 66 69 6c 65 20 69   both the file i
dec80 74 73 65 6c 66 20 61 6e 64 20 69 74 73 20 6d 65  tself and its me
dec90 74 61 64 61 74 61 20 28 66 69 6c 65 0a 2a 2a 20  tadata (file.** 
deca0 73 69 7a 65 2c 20 61 63 63 65 73 73 20 74 69 6d  size, access tim
decb0 65 2c 20 65 74 63 29 20 61 72 65 20 73 79 6e 63  e, etc) are sync
decc0 65 64 2e 20 20 49 66 20 64 61 74 61 4f 6e 6c 79  ed.  If dataOnly
decd0 21 3d 30 20 74 68 65 6e 20 6f 6e 6c 79 20 74 68  !=0 then only th
dece0 65 0a 2a 2a 20 66 69 6c 65 20 64 61 74 61 20 69  e.** file data i
decf0 73 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20  s synced..**.** 
ded00 55 6e 64 65 72 20 55 6e 69 78 2c 20 61 6c 73 6f  Under Unix, also
ded10 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
ded20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 65 6e  the directory en
ded30 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 6c 65  try for the file
ded40 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 72 65  .** has been cre
ded50 61 74 65 64 20 62 79 20 66 73 79 6e 63 2d 69 6e  ated by fsync-in
ded60 67 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  g the directory 
ded70 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
ded80 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 66 20 77 65  e file..** If we
ded90 20 64 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73 20   do not do this 
deda0 61 6e 64 20 77 65 20 65 6e 63 6f 75 6e 74 65 72  and we encounter
dedb0 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65   a power failure
dedc0 2c 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 0a  , the directory.
dedd0 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65  ** entry for the
dede0 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20 6e   journal might n
dedf0 6f 74 20 65 78 69 73 74 20 61 66 74 65 72 20 77  ot exist after w
dee00 65 20 72 65 62 6f 6f 74 2e 20 20 54 68 65 20 6e  e reboot.  The n
dee10 65 78 74 0a 2a 2a 20 53 51 4c 69 74 65 20 74 6f  ext.** SQLite to
dee20 20 61 63 63 65 73 73 20 74 68 65 20 66 69 6c 65   access the file
dee30 20 77 69 6c 6c 20 6e 6f 74 20 6b 6e 6f 77 20 74   will not know t
dee40 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  hat the journal 
dee50 65 78 69 73 74 73 20 28 62 65 63 61 75 73 65 0a  exists (because.
dee60 2a 2a 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  ** the directory
dee70 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6a   entry for the j
dee80 6f 75 72 6e 61 6c 20 77 61 73 20 6e 65 76 65 72  ournal was never
dee90 20 63 72 65 61 74 65 64 29 20 61 6e 64 20 74 68   created) and th
deea0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a  e transaction.**
deeb0 20 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c 6c 20 62   will not roll b
deec0 61 63 6b 20 2d 20 70 6f 73 73 69 62 6c 79 20 6c  ack - possibly l
deed0 65 61 64 69 6e 67 20 74 6f 20 64 61 74 61 62 61  eading to databa
deee0 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a  se corruption..*
deef0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
def00 78 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69  xSync(sqlite3_fi
def10 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67  le *id, int flag
def20 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  s){.  int rc;.  
def30 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
def40 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
def50 0a 0a 20 20 69 6e 74 20 69 73 44 61 74 61 4f 6e  ..  int isDataOn
def60 6c 79 20 3d 20 28 66 6c 61 67 73 26 53 51 4c 49  ly = (flags&SQLI
def70 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59  TE_SYNC_DATAONLY
def80 29 3b 0a 20 20 69 6e 74 20 69 73 46 75 6c 6c 73  );.  int isFulls
def90 79 6e 63 20 3d 20 28 66 6c 61 67 73 26 30 78 30  ync = (flags&0x0
defa0 46 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f  F)==SQLITE_SYNC_
defb0 46 55 4c 4c 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  FULL;..  /* Chec
defc0 6b 20 74 68 61 74 20 6f 6e 65 20 6f 66 20 53 51  k that one of SQ
defd0 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c  LITE_SYNC_NORMAL
defe0 20 6f 72 20 46 55 4c 4c 20 77 61 73 20 70 61 73   or FULL was pas
deff0 73 65 64 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  sed */.  assert(
df000 28 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51  (flags&0x0F)==SQ
df010 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c  LITE_SYNC_NORMAL
df020 0a 20 20 20 20 20 20 7c 7c 20 28 66 6c 61 67 73  .      || (flags
df030 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53  &0x0F)==SQLITE_S
df040 59 4e 43 5f 46 55 4c 4c 0a 20 20 29 3b 0a 0a 20  YNC_FULL.  );.. 
df050 20 2f 2a 20 55 6e 69 78 20 63 61 6e 6e 6f 74 2c   /* Unix cannot,
df060 20 62 75 74 20 73 6f 6d 65 20 73 79 73 74 65 6d   but some system
df070 73 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c  s may return SQL
df080 49 54 45 5f 46 55 4c 4c 20 66 72 6f 6d 20 68 65  ITE_FULL from he
df090 72 65 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6c 69  re. This.  ** li
df0a0 6e 65 20 69 73 20 74 6f 20 74 65 73 74 20 74 68  ne is to test th
df0b0 61 74 20 64 6f 69 6e 67 20 73 6f 20 64 6f 65 73  at doing so does
df0c0 20 6e 6f 74 20 63 61 75 73 65 20 61 6e 79 20 70   not cause any p
df0d0 72 6f 62 6c 65 6d 73 2e 0a 20 20 2a 2f 0a 20 20  roblems..  */.  
df0e0 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c  SimulateDiskfull
df0f0 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
df100 4c 49 54 45 5f 46 55 4c 4c 20 29 3b 0a 0a 20 20  LITE_FULL );..  
df110 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
df120 0a 20 20 4f 53 54 52 41 43 45 32 28 22 53 59 4e  .  OSTRACE2("SYN
df130 43 20 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70 46  C    %-3d\n", pF
df140 69 6c 65 2d 3e 68 29 3b 0a 20 20 72 63 20 3d 20  ile->h);.  rc = 
df150 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46 69 6c 65  full_fsync(pFile
df160 2d 3e 68 2c 20 69 73 46 75 6c 6c 73 79 6e 63 2c  ->h, isFullsync,
df170 20 69 73 44 61 74 61 4f 6e 6c 79 29 3b 0a 20 20   isDataOnly);.  
df180 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
df190 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20 72   rc=1 );.  if( r
df1a0 63 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  c ){.    pFile->
df1b0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  lastErrno = errn
df1c0 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  o;.    return SQ
df1d0 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43  LITE_IOERR_FSYNC
df1e0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 69 6c  ;.  }.  if( pFil
df1f0 65 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20  e->dirfd>=0 ){. 
df200 20 20 20 69 6e 74 20 65 72 72 3b 0a 20 20 20 20     int err;.    
df210 4f 53 54 52 41 43 45 34 28 22 44 49 52 53 59 4e  OSTRACE4("DIRSYN
df220 43 20 25 2d 33 64 20 28 68 61 76 65 5f 66 75 6c  C %-3d (have_ful
df230 6c 66 73 79 6e 63 3d 25 64 20 66 75 6c 6c 73 79  lfsync=%d fullsy
df240 6e 63 3d 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65  nc=%d)\n", pFile
df250 2d 3e 64 69 72 66 64 2c 0a 20 20 20 20 20 20 20  ->dirfd,.       
df260 20 20 20 20 20 48 41 56 45 5f 46 55 4c 4c 46 53       HAVE_FULLFS
df270 59 4e 43 2c 20 69 73 46 75 6c 6c 73 79 6e 63 29  YNC, isFullsync)
df280 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
df290 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43  _DISABLE_DIRSYNC
df2a0 0a 20 20 20 20 2f 2a 20 54 68 65 20 64 69 72 65  .    /* The dire
df2b0 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6f 6e  ctory sync is on
df2c0 6c 79 20 61 74 74 65 6d 70 74 65 64 20 69 66 20  ly attempted if 
df2d0 66 75 6c 6c 5f 66 73 79 6e 63 20 69 73 0a 20 20  full_fsync is.  
df2e0 20 20 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66 20    ** turned off 
df2f0 6f 72 20 75 6e 61 76 61 69 6c 61 62 6c 65 2e 20  or unavailable. 
df300 20 49 66 20 61 20 66 75 6c 6c 5f 66 73 79 6e 63   If a full_fsync
df310 20 6f 63 63 75 72 72 65 64 20 61 62 6f 76 65 2c   occurred above,
df320 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65  .    ** then the
df330 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20   directory sync 
df340 69 73 20 73 75 70 65 72 66 6c 75 6f 75 73 2e 0a  is superfluous..
df350 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28      */.    if( (
df360 21 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20  !HAVE_FULLFSYNC 
df370 7c 7c 20 21 69 73 46 75 6c 6c 73 79 6e 63 29 20  || !isFullsync) 
df380 26 26 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46  && full_fsync(pF
df390 69 6c 65 2d 3e 64 69 72 66 64 2c 30 2c 30 29 20  ile->dirfd,0,0) 
df3a0 29 7b 0a 20 20 20 20 20 20 20 2f 2a 0a 20 20 20  ){.       /*.   
df3b0 20 20 20 20 2a 2a 20 57 65 20 68 61 76 65 20 72      ** We have r
df3c0 65 63 65 69 76 65 64 20 6d 75 6c 74 69 70 6c 65  eceived multiple
df3d0 20 72 65 70 6f 72 74 73 20 6f 66 20 66 73 79 6e   reports of fsyn
df3e0 63 28 29 20 72 65 74 75 72 6e 69 6e 67 0a 20 20  c() returning.  
df3f0 20 20 20 20 20 2a 2a 20 65 72 72 6f 72 73 20 77       ** errors w
df400 68 65 6e 20 61 70 70 6c 69 65 64 20 74 6f 20 64  hen applied to d
df410 69 72 65 63 74 6f 72 69 65 73 20 6f 6e 20 63 65  irectories on ce
df420 72 74 61 69 6e 20 66 69 6c 65 20 73 79 73 74 65  rtain file syste
df430 6d 73 2e 0a 20 20 20 20 20 20 20 2a 2a 20 41 20  ms..       ** A 
df440 66 61 69 6c 65 64 20 64 69 72 65 63 74 6f 72 79  failed directory
df450 20 73 79 6e 63 20 69 73 20 6e 6f 74 20 61 20 62   sync is not a b
df460 69 67 20 64 65 61 6c 2e 20 20 53 6f 20 69 74 20  ig deal.  So it 
df470 73 65 65 6d 73 0a 20 20 20 20 20 20 20 2a 2a 20  seems.       ** 
df480 62 65 74 74 65 72 20 74 6f 20 69 67 6e 6f 72 65  better to ignore
df490 20 74 68 65 20 65 72 72 6f 72 2e 20 20 54 69 63   the error.  Tic
df4a0 6b 65 74 20 23 31 36 35 37 0a 20 20 20 20 20 20  ket #1657.      
df4b0 20 2a 2f 0a 20 20 20 20 20 20 20 2f 2a 20 70 46   */.       /* pF
df4c0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
df4d0 20 65 72 72 6e 6f 3b 20 2a 2f 0a 20 20 20 20 20   errno; */.     
df4e0 20 20 2f 2a 20 72 65 74 75 72 6e 20 53 51 4c 49    /* return SQLI
df4f0 54 45 5f 49 4f 45 52 52 3b 20 2a 2f 0a 20 20 20  TE_IOERR; */.   
df500 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 65 72   }.#endif.    er
df510 72 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d  r = close(pFile-
df520 3e 64 69 72 66 64 29 3b 20 2f 2a 20 4f 6e 6c 79  >dirfd); /* Only
df530 20 6e 65 65 64 20 74 6f 20 73 79 6e 63 20 6f 6e   need to sync on
df540 63 65 2c 20 73 6f 20 63 6c 6f 73 65 20 74 68 65  ce, so close the
df550 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 72 72 3d   */.    if( err=
df560 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20  =0 ){           
df570 20 20 20 2f 2a 20 64 69 72 65 63 74 6f 72 79 20     /* directory 
df580 77 68 65 6e 20 77 65 20 61 72 65 20 64 6f 6e 65  when we are done
df590 20 2a 2f 0a 20 20 20 20 20 20 70 46 69 6c 65 2d   */.      pFile-
df5a0 3e 64 69 72 66 64 20 3d 20 2d 31 3b 0a 20 20 20  >dirfd = -1;.   
df5b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 46   }else{.      pF
df5c0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
df5d0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63   errno;.      rc
df5e0 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
df5f0 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 7d  DIR_CLOSE;.    }
df600 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
df610 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63  ;.}../*.** Trunc
df620 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65  ate an open file
df630 20 74 6f 20 61 20 73 70 65 63 69 66 69 65 64 20   to a specified 
df640 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69  size.*/.static i
df650 6e 74 20 75 6e 69 78 54 72 75 6e 63 61 74 65 28  nt unixTruncate(
df660 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
df670 2c 20 69 36 34 20 6e 42 79 74 65 29 7b 0a 20 20  , i64 nByte){.  
df680 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74  int rc;.  assert
df690 28 20 69 64 20 29 3b 0a 20 20 53 69 6d 75 6c 61  ( id );.  Simula
df6a0 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72  teIOError( retur
df6b0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54  n SQLITE_IOERR_T
df6c0 52 55 4e 43 41 54 45 20 29 3b 0a 20 20 72 63 20  RUNCATE );.  rc 
df6d0 3d 20 66 74 72 75 6e 63 61 74 65 28 28 28 75 6e  = ftruncate(((un
df6e0 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20  ixFile*)id)->h, 
df6f0 28 6f 66 66 5f 74 29 6e 42 79 74 65 29 3b 0a 20  (off_t)nByte);. 
df700 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 28   if( rc ){.    (
df710 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e  (unixFile*)id)->
df720 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  lastErrno = errn
df730 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  o;.    return SQ
df740 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43  LITE_IOERR_TRUNC
df750 41 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ATE;.  }else{.  
df760 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
df770 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  OK;.  }.}../*.**
df780 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 63   Determine the c
df790 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 61  urrent size of a
df7a0 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a   file in bytes.*
df7b0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
df7c0 78 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65  xFileSize(sqlite
df7d0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20  3_file *id, i64 
df7e0 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72  *pSize){.  int r
df7f0 63 3b 0a 20 20 73 74 72 75 63 74 20 73 74 61 74  c;.  struct stat
df800 20 62 75 66 3b 0a 20 20 61 73 73 65 72 74 28 20   buf;.  assert( 
df810 69 64 20 29 3b 0a 20 20 72 63 20 3d 20 66 73 74  id );.  rc = fst
df820 61 74 28 28 28 75 6e 69 78 46 69 6c 65 2a 29 69  at(((unixFile*)i
df830 64 29 2d 3e 68 2c 20 26 62 75 66 29 3b 0a 20 20  d)->h, &buf);.  
df840 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
df850 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20 72   rc=1 );.  if( r
df860 63 21 3d 30 20 29 7b 0a 20 20 20 20 28 28 75 6e  c!=0 ){.    ((un
df870 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73  ixFile*)id)->las
df880 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
df890 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
df8a0 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20  E_IOERR_FSTAT;. 
df8b0 20 7d 0a 20 20 2a 70 53 69 7a 65 20 3d 20 62 75   }.  *pSize = bu
df8c0 66 2e 73 74 5f 73 69 7a 65 3b 0a 0a 20 20 2f 2a  f.st_size;..  /*
df8d0 20 57 68 65 6e 20 6f 70 65 6e 69 6e 67 20 61 20   When opening a 
df8e0 7a 65 72 6f 2d 73 69 7a 65 20 64 61 74 61 62 61  zero-size databa
df8f0 73 65 2c 20 74 68 65 20 66 69 6e 64 4c 6f 63 6b  se, the findLock
df900 49 6e 66 6f 28 29 20 70 72 6f 63 65 64 75 72 65  Info() procedure
df910 0a 20 20 2a 2a 20 77 72 69 74 65 73 20 61 20 73  .  ** writes a s
df920 69 6e 67 6c 65 20 62 79 74 65 20 69 6e 74 6f 20  ingle byte into 
df930 74 68 61 74 20 66 69 6c 65 20 69 6e 20 6f 72 64  that file in ord
df940 65 72 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e  er to work aroun
df950 64 20 61 20 62 75 67 0a 20 20 2a 2a 20 69 6e 20  d a bug.  ** in 
df960 74 68 65 20 4f 53 2d 58 20 6d 73 64 6f 73 20 66  the OS-X msdos f
df970 69 6c 65 73 79 73 74 65 6d 2e 20 20 49 6e 20 6f  ilesystem.  In o
df980 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20 70 72  rder to avoid pr
df990 6f 62 6c 65 6d 73 20 77 69 74 68 20 75 70 70 65  oblems with uppe
df9a0 72 0a 20 20 2a 2a 20 6c 61 79 65 72 73 2c 20 77  r.  ** layers, w
df9b0 65 20 6e 65 65 64 20 74 6f 20 72 65 70 6f 72 74  e need to report
df9c0 20 74 68 69 73 20 66 69 6c 65 20 73 69 7a 65 20   this file size 
df9d0 61 73 20 7a 65 72 6f 20 65 76 65 6e 20 74 68 6f  as zero even tho
df9e0 75 67 68 20 69 74 20 69 73 0a 20 20 2a 2a 20 72  ugh it is.  ** r
df9f0 65 61 6c 6c 79 20 31 2e 20 20 20 54 69 63 6b 65  eally 1.   Ticke
dfa00 74 20 23 33 32 36 30 2e 0a 20 20 2a 2f 0a 20 20  t #3260..  */.  
dfa10 69 66 28 20 2a 70 53 69 7a 65 3d 3d 31 20 29 20  if( *pSize==1 ) 
dfa20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 0a 0a 20 20  *pSize = 0;...  
dfa30 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
dfa40 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f  ;.}..#if SQLITE_
dfa50 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
dfa60 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28  TYLE && defined(
dfa70 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a  __APPLE__)./*.**
dfa80 20 48 61 6e 64 6c 65 72 20 66 6f 72 20 70 72 6f   Handler for pro
dfa90 78 79 2d 6c 6f 63 6b 69 6e 67 20 66 69 6c 65 2d  xy-locking file-
dfaa0 63 6f 6e 74 72 6f 6c 20 76 65 72 62 73 2e 20 20  control verbs.  
dfab0 44 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 6e  Defined below in
dfac0 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79 69 6e 67   the.** proxying
dfad0 20 6c 6f 63 6b 69 6e 67 20 64 69 76 69 73 69 6f   locking divisio
dfae0 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  n..*/.static int
dfaf0 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72 6f   proxyFileContro
dfb00 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  l(sqlite3_file*,
dfb10 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 65 6e 64  int,void*);.#end
dfb20 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 66 6f 72  if.../*.** Infor
dfb30 6d 61 74 69 6f 6e 20 61 6e 64 20 63 6f 6e 74 72  mation and contr
dfb40 6f 6c 20 6f 66 20 61 6e 20 6f 70 65 6e 20 66 69  ol of an open fi
dfb50 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74  le handle..*/.st
dfb60 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 69 6c  atic int unixFil
dfb70 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33  eControl(sqlite3
dfb80 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f  _file *id, int o
dfb90 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a  p, void *pArg){.
dfba0 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a    switch( op ){.
dfbb0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
dfbc0 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a  FCNTL_LOCKSTATE:
dfbd0 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29   {.      *(int*)
dfbe0 70 41 72 67 20 3d 20 28 28 75 6e 69 78 46 69 6c  pArg = ((unixFil
dfbf0 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65  e*)id)->locktype
dfc00 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
dfc10 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
dfc20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
dfc30 4c 41 53 54 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20  LAST_ERRNO: {.  
dfc40 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20      *(int*)pArg 
dfc50 3d 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64  = ((unixFile*)id
dfc60 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20  )->lastErrno;.  
dfc70 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
dfc80 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e  E_OK;.    }.#ifn
dfc90 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f  def NDEBUG.    /
dfca0 2a 20 54 68 65 20 70 61 67 65 72 20 63 61 6c 6c  * The pager call
dfcb0 73 20 74 68 69 73 20 6d 65 74 68 6f 64 20 74 6f  s this method to
dfcc0 20 73 69 67 6e 61 6c 20 74 68 61 74 20 69 74 20   signal that it 
dfcd0 68 61 73 20 64 6f 6e 65 0a 20 20 20 20 2a 2a 20  has done.    ** 
dfce0 61 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 74  a rollback and t
dfcf0 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65  hat the database
dfd00 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 75 6e   is therefore un
dfd10 63 68 61 6e 67 65 64 20 61 6e 64 0a 20 20 20 20  changed and.    
dfd20 2a 2a 20 69 74 20 68 65 6e 63 65 20 69 74 20 69  ** it hence it i
dfd30 73 20 4f 4b 20 66 6f 72 20 74 68 65 20 74 72 61  s OK for the tra
dfd40 6e 73 61 63 74 69 6f 6e 20 63 68 61 6e 67 65 20  nsaction change 
dfd50 63 6f 75 6e 74 65 72 20 74 6f 20 62 65 0a 20 20  counter to be.  
dfd60 20 20 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e 0a    ** unchanged..
dfd70 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
dfd80 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f  SQLITE_FCNTL_DB_
dfd90 55 4e 43 48 41 4e 47 45 44 3a 20 7b 0a 20 20 20  UNCHANGED: {.   
dfda0 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69     ((unixFile*)i
dfdb0 64 29 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 30  d)->dbUpdate = 0
dfdc0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
dfdd0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
dfde0 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
dfdf0 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
dfe00 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65  _STYLE && define
dfe10 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 20  d(__APPLE__).   
dfe20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45 54   case SQLITE_SET
dfe30 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a 0a  _LOCKPROXYFILE:.
dfe40 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
dfe50 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c  GET_LOCKPROXYFIL
dfe60 45 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72  E: {.      retur
dfe70 6e 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72  n proxyFileContr
dfe80 6f 6c 28 69 64 2c 6f 70 2c 70 41 72 67 29 3b 0a  ol(id,op,pArg);.
dfe90 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20      }.#endif /* 
dfea0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
dfeb0 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64  CKING_STYLE && d
dfec0 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f  efined(__APPLE__
dfed0 29 20 2a 2f 0a 20 20 7d 0a 20 20 72 65 74 75 72  ) */.  }.  retur
dfee0 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
dfef0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
dff00 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20  the sector size 
dff10 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20  in bytes of the 
dff20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b  underlying block
dff30 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74   device for.** t
dff40 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69 6c  he specified fil
dff50 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f 73  e. This is almos
dff60 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74  t always 512 byt
dff70 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a 2a  es, but may be.*
dff80 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d  * larger for som
dff90 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a  e devices..**.**
dffa0 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73   SQLite code ass
dffb0 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 69  umes this functi
dffc0 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20  on cannot fail. 
dffd0 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20  It also assumes 
dffe0 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66  that.** if two f
dfff0 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 64  iles are created
e0000 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c   in the same fil
e0010 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 6f  e-system directo
e0020 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61  ry (i.e..** a da
e0030 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 6a  tabase and its j
e0040 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 61  ournal file) tha
e0050 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a  t the sector siz
e0060 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a  e will be the.**
e0070 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a   same for both..
e0080 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
e0090 69 78 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c  ixSectorSize(sql
e00a0 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73  ite3_file *NotUs
e00b0 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  ed){.  UNUSED_PA
e00c0 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
e00d0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
e00e0 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52  E_DEFAULT_SECTOR
e00f0 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  _SIZE;.}../*.** 
e0100 52 65 74 75 72 6e 20 74 68 65 20 64 65 76 69 63  Return the devic
e0110 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63  e characteristic
e0120 73 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 20  s for the file. 
e0130 54 68 69 73 20 69 73 20 61 6c 77 61 79 73 20 30  This is always 0
e0140 20 66 6f 72 20 75 6e 69 78 2e 0a 2a 2f 0a 73 74   for unix..*/.st
e0150 61 74 69 63 20 69 6e 74 20 75 6e 69 78 44 65 76  atic int unixDev
e0160 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
e0170 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  cs(sqlite3_file 
e0180 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55  *NotUsed){.  UNU
e0190 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
e01a0 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e  tUsed);.  return
e01b0 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 72   0;.}../*.** Her
e01c0 65 20 65 6e 64 73 20 74 68 65 20 69 6d 70 6c 65  e ends the imple
e01d0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6c 6c  mentation of all
e01e0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65   sqlite3_file me
e01f0 74 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a  thods..**.******
e0200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0210 20 45 6e 64 20 73 71 6c 69 74 65 33 5f 66 69 6c   End sqlite3_fil
e0220 65 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a  e Methods ******
e0230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0240 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a  *********.******
e0250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0290 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ********/../*.**
e02a0 20 54 68 69 73 20 64 69 76 69 73 69 6f 6e 20 63   This division c
e02b0 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69  ontains definiti
e02c0 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 69  ons of sqlite3_i
e02d0 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  o_methods object
e02e0 73 20 74 68 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d  s that.** implem
e02f0 65 6e 74 20 76 61 72 69 6f 75 73 20 66 69 6c 65  ent various file
e0300 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67   locking strateg
e0310 69 65 73 2e 20 20 49 74 20 61 6c 73 6f 20 63 6f  ies.  It also co
e0320 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 6f  ntains definitio
e0330 6e 73 0a 2a 2a 20 6f 66 20 22 66 69 6e 64 65 72  ns.** of "finder
e0340 22 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 41 20  " functions.  A 
e0350 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20  finder-function 
e0360 69 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74  is used to locat
e0370 65 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  e the appropriat
e0380 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f  e.** sqlite3_io_
e0390 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 66  methods object f
e03a0 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  or a particular 
e03b0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
e03c0 54 68 65 20 70 41 70 70 44 61 74 61 0a 2a 2a 20  The pAppData.** 
e03d0 66 69 65 6c 64 20 6f 66 20 74 68 65 20 73 71 6c  field of the sql
e03e0 69 74 65 33 5f 76 66 73 20 56 46 53 20 6f 62 6a  ite3_vfs VFS obj
e03f0 65 63 74 73 20 61 72 65 20 69 6e 69 74 69 61 6c  ects are initial
e0400 69 7a 65 64 20 74 6f 20 62 65 20 70 6f 69 6e 74  ized to be point
e0410 65 72 73 20 74 6f 0a 2a 2a 20 74 68 65 20 63 6f  ers to.** the co
e0420 72 72 65 63 74 20 66 69 6e 64 65 72 2d 66 75 6e  rrect finder-fun
e0430 63 74 69 6f 6e 20 66 6f 72 20 74 68 61 74 20 56  ction for that V
e0440 46 53 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 66  FS..**.** Most f
e0450 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 73 20  inder functions 
e0460 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  return a pointer
e0470 20 74 6f 20 61 20 66 69 78 65 64 20 73 71 6c 69   to a fixed sqli
e0480 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a  te3_io_methods.*
e0490 2a 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 6f  * object.  The o
e04a0 6e 6c 79 20 69 6e 74 65 72 65 73 74 69 6e 67 20  nly interesting 
e04b0 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20  finder-function 
e04c0 69 73 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e  is autolockIoFin
e04d0 64 65 72 2c 20 77 68 69 63 68 0a 2a 2a 20 6c 6f  der, which.** lo
e04e0 6f 6b 73 20 61 74 20 74 68 65 20 66 69 6c 65 73  oks at the files
e04f0 79 73 74 65 6d 20 74 79 70 65 20 61 6e 64 20 74  ystem type and t
e0500 72 69 65 73 20 74 6f 20 67 75 65 73 73 20 74 68  ries to guess th
e0510 65 20 62 65 73 74 20 6c 6f 63 6b 69 6e 67 0a 2a  e best locking.*
e0520 2a 20 73 74 72 61 74 65 67 79 20 66 72 6f 6d 20  * strategy from 
e0530 74 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  that..**.** For 
e0540 66 69 6e 64 65 72 2d 66 75 6e 74 69 6f 6e 20 46  finder-funtion F
e0550 2c 20 74 77 6f 20 6f 62 6a 65 63 74 73 20 61 72  , two objects ar
e0560 65 20 63 72 65 61 74 65 64 3a 0a 2a 2a 0a 2a 2a  e created:.**.**
e0570 20 20 20 20 28 31 29 20 54 68 65 20 72 65 61 6c      (1) The real
e0580 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e   finder-function
e0590 20 6e 61 6d 65 64 20 22 46 49 6d 70 74 28 29 22   named "FImpt()"
e05a0 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 41  ..**.**    (2) A
e05b0 20 63 6f 6e 73 74 61 6e 74 20 70 6f 69 6e 74 65   constant pointe
e05c0 72 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  r to this functi
e05d0 6f 6e 20 6e 61 6d 65 64 20 6a 75 73 74 20 22 46  on named just "F
e05e0 22 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 20 70 6f  "..**.**.** A po
e05f0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46 20 70  inter to the F p
e0600 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61  ointer is used a
e0610 73 20 74 68 65 20 70 41 70 70 44 61 74 61 20 76  s the pAppData v
e0620 61 6c 75 65 20 66 6f 72 20 56 46 53 0a 2a 2a 20  alue for VFS.** 
e0630 6f 62 6a 65 63 74 73 2e 20 20 57 65 20 68 61 76  objects.  We hav
e0640 65 20 74 6f 20 64 6f 20 74 68 69 73 20 69 6e 73  e to do this ins
e0650 74 65 61 64 20 6f 66 20 6c 65 74 74 69 6e 67 20  tead of letting 
e0660 70 41 70 70 44 61 74 61 20 70 6f 69 6e 74 0a 2a  pAppData point.*
e0670 2a 20 64 69 72 65 63 74 6c 79 20 61 74 20 74 68  * directly at th
e0680 65 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f  e finder-functio
e0690 6e 20 73 69 6e 63 65 20 43 39 30 20 72 75 6c 65  n since C90 rule
e06a0 73 20 70 72 65 76 65 6e 74 20 61 20 76 6f 69 64  s prevent a void
e06b0 2a 0a 2a 2a 20 66 72 6f 6d 20 62 65 20 63 61 73  *.** from be cas
e06c0 74 20 69 6e 74 6f 20 61 20 66 75 6e 63 74 69 6f  t into a functio
e06d0 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a  n pointer..**.**
e06e0 0a 2a 2a 20 45 61 63 68 20 69 6e 73 74 61 6e 63  .** Each instanc
e06f0 65 20 6f 66 20 74 68 69 73 20 6d 61 63 72 6f 20  e of this macro 
e0700 67 65 6e 65 72 61 74 65 73 20 74 77 6f 20 6f 62  generates two ob
e0710 6a 65 63 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a  jects:.**.**   *
e0720 20 20 41 20 63 6f 6e 73 74 61 6e 74 20 73 71 6c    A constant sql
e0730 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
e0740 6f 62 6a 65 63 74 20 63 61 6c 6c 20 4d 45 54 48  object call METH
e0750 4f 44 20 74 68 61 74 20 68 61 73 20 6c 6f 63 6b  OD that has lock
e0760 69 6e 67 0a 2a 2a 20 20 20 20 20 20 6d 65 74 68  ing.**      meth
e0770 6f 64 73 20 43 4c 4f 53 45 2c 20 4c 4f 43 4b 2c  ods CLOSE, LOCK,
e0780 20 55 4e 4c 4f 43 4b 2c 20 43 4b 52 45 53 4c 4f   UNLOCK, CKRESLO
e0790 43 4b 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41  CK..**.**   *  A
e07a0 6e 20 49 2f 4f 20 6d 65 74 68 6f 64 20 66 69 6e  n I/O method fin
e07b0 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c  der function cal
e07c0 6c 65 64 20 46 49 4e 44 45 52 20 74 68 61 74 20  led FINDER that 
e07d0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
e07e0 72 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65  r.**      to the
e07f0 20 4d 45 54 48 4f 44 20 6f 62 6a 65 63 74 20 69   METHOD object i
e0800 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 62  n the previous b
e0810 75 6c 6c 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ullet..*/.#defin
e0820 65 20 49 4f 4d 45 54 48 4f 44 53 28 46 49 4e 44  e IOMETHODS(FIND
e0830 45 52 2c 20 4d 45 54 48 4f 44 2c 20 43 4c 4f 53  ER, METHOD, CLOS
e0840 45 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f 43 4b 2c  E, LOCK, UNLOCK,
e0850 20 43 4b 4c 4f 43 4b 29 20 20 20 20 20 20 20 20   CKLOCK)        
e0860 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20         \.static 
e0870 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f  const sqlite3_io
e0880 5f 6d 65 74 68 6f 64 73 20 4d 45 54 48 4f 44 20  _methods METHOD 
e0890 3d 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  = {             
e08a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e08b0 20 20 20 20 20 20 5c 0a 20 20 20 31 2c 20 20 20        \.   1,   
e08c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e08d0 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69         /* iVersi
e08e0 6f 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  on */           
e08f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0900 20 20 20 20 20 5c 0a 20 20 20 43 4c 4f 53 45 2c       \.   CLOSE,
e0910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0920 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20        /* xClose 
e0930 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
e0940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0950 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 52 65 61      \.   unixRea
e0960 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d,              
e0970 20 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f       /* xRead */
e0980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e09a0 20 20 20 5c 0a 20 20 20 75 6e 69 78 57 72 69 74     \.   unixWrit
e09b0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
e09c0 20 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f      /* xWrite */
e09d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e09e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e09f0 20 20 5c 0a 20 20 20 75 6e 69 78 54 72 75 6e 63    \.   unixTrunc
e0a00 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ate,            
e0a10 20 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20     /* xTruncate 
e0a20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
e0a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0a40 20 5c 0a 20 20 20 75 6e 69 78 53 79 6e 63 2c 20   \.   unixSync, 
e0a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0a60 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 20 20 20    /* xSync */   
e0a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0a90 5c 0a 20 20 20 75 6e 69 78 46 69 6c 65 53 69 7a  \.   unixFileSiz
e0aa0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
e0ab0 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f   /* xFileSize */
e0ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
e0ae0 0a 20 20 20 4c 4f 43 4b 2c 20 20 20 20 20 20 20  .   LOCK,       
e0af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0b00 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 20 20 20 20 20  /* xLock */     
e0b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
e0b30 20 20 20 55 4e 4c 4f 43 4b 2c 20 20 20 20 20 20     UNLOCK,      
e0b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
e0b50 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 20 20 20 20  * xUnlock */    
e0b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0b70 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
e0b80 20 20 43 4b 4c 4f 43 4b 2c 20 20 20 20 20 20 20    CKLOCK,       
e0b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e0ba0 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c   xCheckReservedL
e0bb0 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ock */          
e0bc0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
e0bd0 20 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c   unixFileControl
e0be0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
e0bf0 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 20  xFileControl */ 
e0c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0c10 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
e0c20 75 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 2c 20  unixSectorSize, 
e0c30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
e0c40 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f 20 20 20  SectorSize */   
e0c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0c60 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75            \.   u
e0c70 6e 69 78 44 65 76 69 63 65 43 68 61 72 61 63 74  nixDeviceCharact
e0c80 65 72 69 73 74 69 63 73 20 20 20 2f 2a 20 78 44  eristics   /* xD
e0c90 65 76 69 63 65 43 61 70 61 62 69 6c 69 74 69 65  eviceCapabilitie
e0ca0 73 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  s */            
e0cb0 20 20 20 20 20 20 20 20 20 5c 0a 7d 3b 20 20 20           \.};   
e0cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0d00 20 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63          \.static
e0d10 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69   const sqlite3_i
e0d20 6f 5f 6d 65 74 68 6f 64 73 20 2a 46 49 4e 44 45  o_methods *FINDE
e0d30 52 23 23 49 6d 70 6c 28 63 6f 6e 73 74 20 63 68  R##Impl(const ch
e0d40 61 72 20 2a 7a 2c 20 75 6e 69 78 46 69 6c 65 20  ar *z, unixFile 
e0d50 2a 70 29 7b 20 20 20 5c 0a 20 20 55 4e 55 53 45  *p){   \.  UNUSE
e0d60 44 5f 50 41 52 41 4d 45 54 45 52 28 7a 29 3b 20  D_PARAMETER(z); 
e0d70 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
e0d80 28 70 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  (p);            
e0d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0da0 20 20 20 20 20 20 5c 0a 20 20 72 65 74 75 72 6e        \.  return
e0db0 20 26 4d 45 54 48 4f 44 3b 20 20 20 20 20 20 20   &METHOD;       
e0dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0df0 20 20 20 20 20 5c 0a 7d 20 20 20 20 20 20 20 20       \.}        
e0e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0e40 20 20 20 20 5c 0a 73 74 61 74 69 63 20 63 6f 6e      \.static con
e0e50 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  st sqlite3_io_me
e0e60 74 68 6f 64 73 20 2a 28 2a 63 6f 6e 73 74 20 46  thods *(*const F
e0e70 49 4e 44 45 52 29 28 63 6f 6e 73 74 20 63 68 61  INDER)(const cha
e0e80 72 2a 2c 75 6e 69 78 46 69 6c 65 20 2a 70 29 20  r*,unixFile *p) 
e0e90 20 20 20 5c 0a 20 20 20 20 3d 20 46 49 4e 44 45     \.    = FINDE
e0ea0 52 23 23 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a 2a 20  R##Impl;../*.** 
e0eb0 48 65 72 65 20 61 72 65 20 61 6c 6c 20 6f 66 20  Here are all of 
e0ec0 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  the sqlite3_io_m
e0ed0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66  ethods objects f
e0ee0 6f 72 20 65 61 63 68 20 6f 66 20 74 68 65 0a 2a  or each of the.*
e0ef0 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65  * locking strate
e0f00 67 69 65 73 2e 20 20 46 75 6e 63 74 69 6f 6e 73  gies.  Functions
e0f10 20 74 68 61 74 20 72 65 74 75 72 6e 20 70 6f 69   that return poi
e0f20 6e 74 65 72 73 20 74 6f 20 74 68 65 73 65 20 6d  nters to these m
e0f30 65 74 68 6f 64 73 0a 2a 2a 20 61 72 65 20 61 6c  ethods.** are al
e0f40 73 6f 20 63 72 65 61 74 65 64 2e 0a 2a 2f 0a 49  so created..*/.I
e0f50 4f 4d 45 54 48 4f 44 53 28 0a 20 20 70 6f 73 69  OMETHODS(.  posi
e0f60 78 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20  xIoFinder,      
e0f70 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20        /* Finder 
e0f80 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f  function name */
e0f90 0a 20 20 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64  .  posixIoMethod
e0fa0 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  s,           /* 
e0fb0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
e0fc0 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a  ds object name *
e0fd0 2f 0a 20 20 75 6e 69 78 43 6c 6f 73 65 2c 20 20  /.  unixClose,  
e0fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e0ff0 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a   xClose method *
e1000 2f 0a 20 20 75 6e 69 78 4c 6f 63 6b 2c 20 20 20  /.  unixLock,   
e1010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e1020 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f   xLock method */
e1030 0a 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 2c 20 20  .  unixUnlock,  
e1040 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e1050 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  xUnlock method *
e1060 2f 0a 20 20 75 6e 69 78 43 68 65 63 6b 52 65 73  /.  unixCheckRes
e1070 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 2f 2a  ervedLock     /*
e1080 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c   xCheckReservedL
e1090 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a  ock method */.).
e10a0 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 6e 6f 6c  IOMETHODS(.  nol
e10b0 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 20  ockIoFinder,    
e10c0 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72         /* Finder
e10d0 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a   function name *
e10e0 2f 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68  /.  nolockIoMeth
e10f0 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ods,          /*
e1100 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
e1110 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20  ods object name 
e1120 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65  */.  nolockClose
e1130 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
e1140 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20  * xClose method 
e1150 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 2c  */.  nolockLock,
e1160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
e1170 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  * xLock method *
e1180 2f 0a 20 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b  /.  nolockUnlock
e1190 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
e11a0 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20   xUnlock method 
e11b0 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b  */.  nolockCheck
e11c0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 2f  ReservedLock   /
e11d0 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  * xCheckReserved
e11e0 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29  Lock method */.)
e11f0 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 64 6f  .IOMETHODS(.  do
e1200 74 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20  tlockIoFinder,  
e1210 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65          /* Finde
e1220 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20  r function name 
e1230 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f 4d 65  */.  dotlockIoMe
e1240 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 2f  thods,         /
e1250 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  * sqlite3_io_met
e1260 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65  hods object name
e1270 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 6c 6f   */.  dotlockClo
e1280 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
e1290 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64  /* xClose method
e12a0 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 4c 6f 63   */.  dotlockLoc
e12b0 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k,              
e12c0 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20  /* xLock method 
e12d0 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f  */.  dotlockUnlo
e12e0 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  ck,            /
e12f0 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64  * xUnlock method
e1300 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 68 65   */.  dotlockChe
e1310 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20  ckReservedLock  
e1320 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65  /* xCheckReserve
e1330 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  dLock method */.
e1340 29 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e  )..#if SQLITE_EN
e1350 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
e1360 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b  LE && !OS_VXWORK
e1370 53 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 66  S.IOMETHODS(.  f
e1380 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20  lockIoFinder,   
e1390 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64           /* Find
e13a0 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65  er function name
e13b0 20 2a 2f 0a 20 20 66 6c 6f 63 6b 49 6f 4d 65 74   */.  flockIoMet
e13c0 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20  hods,           
e13d0 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  /* sqlite3_io_me
e13e0 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d  thods object nam
e13f0 65 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 6c 6f 73  e */.  flockClos
e1400 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
e1410 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f   /* xClose metho
e1420 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 4c 6f 63 6b  d */.  flockLock
e1430 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e1440 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64   /* xLock method
e1450 20 2a 2f 0a 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63   */.  flockUnloc
e1460 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k,              
e1470 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f  /* xUnlock metho
e1480 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 68 65 63  d */.  flockChec
e1490 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20  kReservedLock   
e14a0 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76   /* xCheckReserv
e14b0 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f  edLock method */
e14c0 0a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 4f  .).#endif..#if O
e14d0 53 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d 45 54 48  S_VXWORKS.IOMETH
e14e0 4f 44 53 28 0a 20 20 73 65 6d 49 6f 46 69 6e 64  ODS(.  semIoFind
e14f0 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  er,             
e1500 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74   /* Finder funct
e1510 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 65  ion name */.  se
e1520 6d 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20  mIoMethods,     
e1530 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74          /* sqlit
e1540 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62  e3_io_methods ob
e1550 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 73  ject name */.  s
e1560 65 6d 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20  emClose,        
e1570 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f           /* xClo
e1580 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73  se method */.  s
e1590 65 6d 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20  emLock,         
e15a0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63           /* xLoc
e15b0 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65  k method */.  se
e15c0 6d 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20  mUnlock,        
e15d0 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f          /* xUnlo
e15e0 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73  ck method */.  s
e15f0 65 6d 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  emCheckReservedL
e1600 6f 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65  ock      /* xChe
e1610 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d  ckReservedLock m
e1620 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69  ethod */.).#endi
e1630 66 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f  f..#if defined(_
e1640 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c  _APPLE__) && SQL
e1650 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
e1660 4e 47 5f 53 54 59 4c 45 0a 49 4f 4d 45 54 48 4f  NG_STYLE.IOMETHO
e1670 44 53 28 0a 20 20 61 66 70 49 6f 46 69 6e 64 65  DS(.  afpIoFinde
e1680 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
e1690 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69  /* Finder functi
e16a0 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 70  on name */.  afp
e16b0 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20  IoMethods,      
e16c0 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65         /* sqlite
e16d0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a  3_io_methods obj
e16e0 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66  ect name */.  af
e16f0 70 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20  pClose,         
e1700 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73          /* xClos
e1710 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66  e method */.  af
e1720 70 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20  pLock,          
e1730 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b          /* xLock
e1740 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70   method */.  afp
e1750 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20  Unlock,         
e1760 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63         /* xUnloc
e1770 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66  k method */.  af
e1780 70 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  pCheckReservedLo
e1790 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63  ck      /* xChec
e17a0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65  kReservedLock me
e17b0 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66  thod */.).#endif
e17c0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 57 68 6f  ../*.** The "Who
e17d0 6c 65 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 22  le File Locking"
e17e0 20 66 69 6e 64 65 72 20 72 65 74 75 72 6e 73 20   finder returns 
e17f0 74 68 65 20 73 61 6d 65 20 73 65 74 20 6f 66 20  the same set of 
e1800 6d 65 74 68 6f 64 73 20 61 73 0a 2a 2a 20 74 68  methods as.** th
e1810 65 20 70 6f 73 69 78 20 6c 6f 63 6b 69 6e 67 20  e posix locking 
e1820 66 69 6e 64 65 72 2e 20 20 42 75 74 20 69 74 20  finder.  But it 
e1830 61 6c 73 6f 20 73 65 74 73 20 74 68 65 20 53 51  also sets the SQ
e1840 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f  LITE_WHOLE_FILE_
e1850 4c 4f 43 4b 49 4e 47 0a 2a 2a 20 66 6c 61 67 20  LOCKING.** flag 
e1860 74 6f 20 66 6f 72 63 65 20 74 68 65 20 70 6f 73  to force the pos
e1870 69 78 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b  ix advisory lock
e1880 73 20 74 6f 20 63 6f 76 65 72 20 74 68 65 20 77  s to cover the w
e1890 68 6f 6c 65 20 66 69 6c 65 20 69 6e 73 74 65 61  hole file instea
e18a0 64 0a 2a 2a 20 6f 66 20 6a 75 73 74 20 61 20 73  d.** of just a s
e18b0 6d 61 6c 6c 20 73 70 61 6e 20 6f 66 20 62 79 74  mall span of byt
e18c0 65 73 20 6e 65 61 72 20 74 68 65 20 31 47 69 42  es near the 1GiB
e18d0 20 62 6f 75 6e 64 61 72 79 2e 20 20 57 68 6f 6c   boundary.  Whol
e18e0 65 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 0a 2a  e File Locking.*
e18f0 2a 20 69 73 20 75 73 65 66 75 6c 20 6f 6e 20 4e  * is useful on N
e1900 46 53 2d 6d 6f 75 6e 74 65 64 20 66 69 6c 65 73  FS-mounted files
e1910 20 73 69 6e 63 65 20 69 74 20 68 65 6c 70 73 20   since it helps 
e1920 4e 46 53 20 74 6f 20 6d 61 69 6e 74 61 69 6e 20  NFS to maintain 
e1930 63 61 63 68 65 0a 2a 2a 20 63 6f 68 65 72 65 6e  cache.** coheren
e1940 63 79 2e 20 20 42 75 74 20 69 74 20 69 73 20 61  cy.  But it is a
e1950 20 64 65 74 72 69 6d 65 6e 74 20 74 6f 20 6f 74   detriment to ot
e1960 68 65 72 20 66 69 6c 65 73 79 73 74 65 6d 73 20  her filesystems 
e1970 73 69 6e 63 65 20 69 74 20 72 75 6e 73 0a 2a 2a  since it runs.**
e1980 20 73 6c 6f 77 65 72 2e 0a 2a 2f 0a 73 74 61 74   slower..*/.stat
e1990 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
e19a0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 6f 73  _io_methods *pos
e19b0 69 78 57 66 6c 49 6f 46 69 6e 64 65 72 49 6d 70  ixWflIoFinderImp
e19c0 6c 28 63 6f 6e 73 74 20 63 68 61 72 2a 7a 2c 20  l(const char*z, 
e19d0 75 6e 69 78 46 69 6c 65 2a 70 29 7b 0a 20 20 55  unixFile*p){.  U
e19e0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
e19f0 7a 29 3b 0a 20 20 70 2d 3e 66 69 6c 65 46 6c 61  z);.  p->fileFla
e1a00 67 73 20 3d 20 53 51 4c 49 54 45 5f 57 48 4f 4c  gs = SQLITE_WHOL
e1a10 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3b 0a  E_FILE_LOCKING;.
e1a20 20 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49    return &posixI
e1a30 6f 4d 65 74 68 6f 64 73 3b 0a 7d 0a 73 74 61 74  oMethods;.}.stat
e1a40 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
e1a50 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 0a 20 20 2a  _io_methods .  *
e1a60 28 2a 63 6f 6e 73 74 20 70 6f 73 69 78 57 66 6c  (*const posixWfl
e1a70 49 6f 46 69 6e 64 65 72 29 28 63 6f 6e 73 74 20  IoFinder)(const 
e1a80 63 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 20 2a  char*,unixFile *
e1a90 70 29 20 3d 20 70 6f 73 69 78 57 66 6c 49 6f 46  p) = posixWflIoF
e1aa0 69 6e 64 65 72 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a  inderImpl;../*.*
e1ab0 2a 20 54 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b  * The proxy lock
e1ac0 69 6e 67 20 6d 65 74 68 6f 64 20 69 73 20 61 20  ing method is a 
e1ad0 22 73 75 70 65 72 2d 6d 65 74 68 6f 64 22 20 69  "super-method" i
e1ae0 6e 20 74 68 65 20 73 65 6e 73 65 20 74 68 61 74  n the sense that
e1af0 20 69 74 0a 2a 2a 20 6f 70 65 6e 73 20 73 65 63   it.** opens sec
e1b00 6f 6e 64 61 72 79 20 66 69 6c 65 20 64 65 73 63  ondary file desc
e1b10 72 69 70 74 6f 72 73 20 66 6f 72 20 74 68 65 20  riptors for the 
e1b20 63 6f 6e 63 68 20 61 6e 64 20 6c 6f 63 6b 20 66  conch and lock f
e1b30 69 6c 65 73 20 61 6e 64 0a 2a 2a 20 69 74 20 75  iles and.** it u
e1b40 73 65 73 20 70 72 6f 78 79 2c 20 64 6f 74 2d 66  ses proxy, dot-f
e1b50 69 6c 65 2c 20 41 46 50 2c 20 61 6e 64 20 66 6c  ile, AFP, and fl
e1b60 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 20 6d 65  ock() locking me
e1b70 74 68 6f 64 73 20 6f 6e 20 74 68 6f 73 65 0a 2a  thods on those.*
e1b80 2a 20 73 65 63 6f 6e 64 61 72 79 20 66 69 6c 65  * secondary file
e1b90 73 2e 20 20 46 6f 72 20 74 68 69 73 20 72 65 61  s.  For this rea
e1ba0 73 6f 6e 2c 20 74 68 65 20 64 69 76 69 73 69 6f  son, the divisio
e1bb0 6e 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  n that implement
e1bc0 73 0a 2a 2a 20 70 72 6f 78 79 20 6c 6f 63 6b 69  s.** proxy locki
e1bd0 6e 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6d 75  ng is located mu
e1be0 63 68 20 66 75 72 74 68 65 72 20 64 6f 77 6e 20  ch further down 
e1bf0 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 42 75  in the file.  Bu
e1c00 74 20 77 65 20 6e 65 65 64 0a 2a 2a 20 74 6f 20  t we need.** to 
e1c10 67 6f 20 61 68 65 61 64 20 61 6e 64 20 64 65 66  go ahead and def
e1c20 69 6e 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ine the sqlite3_
e1c30 69 6f 5f 6d 65 74 68 6f 64 73 20 61 6e 64 20 66  io_methods and f
e1c40 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 0a 2a  inder function.*
e1c50 2a 20 66 6f 72 20 70 72 6f 78 79 20 6c 6f 63 6b  * for proxy lock
e1c60 69 6e 67 20 68 65 72 65 2e 20 20 53 6f 20 77 65  ing here.  So we
e1c70 20 66 6f 72 77 61 72 64 20 64 65 63 6c 61 72 65   forward declare
e1c80 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73   the I/O methods
e1c90 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64  ..*/.#if defined
e1ca0 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53  (__APPLE__) && S
e1cb0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
e1cc0 4b 49 4e 47 5f 53 54 59 4c 45 0a 73 74 61 74 69  KING_STYLE.stati
e1cd0 63 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73 65  c int proxyClose
e1ce0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b  (sqlite3_file*);
e1cf0 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78  .static int prox
e1d00 79 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  yLock(sqlite3_fi
e1d10 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 61 74 69  le*, int);.stati
e1d20 63 20 69 6e 74 20 70 72 6f 78 79 55 6e 6c 6f 63  c int proxyUnloc
e1d30 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  k(sqlite3_file*,
e1d40 20 69 6e 74 29 3b 0a 73 74 61 74 69 63 20 69 6e   int);.static in
e1d50 74 20 70 72 6f 78 79 43 68 65 63 6b 52 65 73 65  t proxyCheckRese
e1d60 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33  rvedLock(sqlite3
e1d70 5f 66 69 6c 65 2a 2c 20 69 6e 74 2a 29 3b 0a 49  _file*, int*);.I
e1d80 4f 4d 45 54 48 4f 44 53 28 0a 20 20 70 72 6f 78  OMETHODS(.  prox
e1d90 79 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20  yIoFinder,      
e1da0 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20        /* Finder 
e1db0 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f  function name */
e1dc0 0a 20 20 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64  .  proxyIoMethod
e1dd0 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  s,           /* 
e1de0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
e1df0 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a  ds object name *
e1e00 2f 0a 20 20 70 72 6f 78 79 43 6c 6f 73 65 2c 20  /.  proxyClose, 
e1e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e1e20 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a   xClose method *
e1e30 2f 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 2c 20 20  /.  proxyLock,  
e1e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e1e50 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f   xLock method */
e1e60 0a 20 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 2c 20  .  proxyUnlock, 
e1e70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e1e80 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  xUnlock method *
e1e90 2f 0a 20 20 70 72 6f 78 79 43 68 65 63 6b 52 65  /.  proxyCheckRe
e1ea0 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 2f 2a  servedLock    /*
e1eb0 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c   xCheckReservedL
e1ec0 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a  ock method */.).
e1ed0 23 65 6e 64 69 66 0a 0a 0a 23 69 66 20 64 65 66  #endif...#if def
e1ee0 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20  ined(__APPLE__) 
e1ef0 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  && SQLITE_ENABLE
e1f00 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2f  _LOCKING_STYLE./
e1f10 2a 20 0a 2a 2a 20 54 68 69 73 20 22 66 69 6e 64  * .** This "find
e1f20 65 72 22 20 66 75 6e 63 74 69 6f 6e 20 61 74 74  er" function att
e1f30 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72 6d 69  empts to determi
e1f40 6e 65 20 74 68 65 20 62 65 73 74 20 6c 6f 63 6b  ne the best lock
e1f50 69 6e 67 20 73 74 72 61 74 65 67 79 20 0a 2a 2a  ing strategy .**
e1f60 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73   for the databas
e1f70 65 20 66 69 6c 65 20 22 66 69 6c 65 50 61 74 68  e file "filePath
e1f80 22 2e 20 20 49 74 20 74 68 65 6e 20 72 65 74 75  ".  It then retu
e1f90 72 6e 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f  rns the sqlite3_
e1fa0 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 62  io_methods.** ob
e1fb0 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c 65 6d  ject that implem
e1fc0 65 6e 74 73 20 74 68 61 74 20 73 74 72 61 74 65  ents that strate
e1fd0 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  gy..**.** This i
e1fe0 73 20 66 6f 72 20 4d 61 63 4f 53 58 20 6f 6e 6c  s for MacOSX onl
e1ff0 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  y..*/.static con
e2000 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  st sqlite3_io_me
e2010 74 68 6f 64 73 20 2a 61 75 74 6f 6c 6f 63 6b 49  thods *autolockI
e2020 6f 46 69 6e 64 65 72 49 6d 70 6c 28 0a 20 20 63  oFinderImpl(.  c
e2030 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 50  onst char *fileP
e2040 61 74 68 2c 20 20 20 20 2f 2a 20 6e 61 6d 65 20  ath,    /* name 
e2050 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
e2060 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 69 78 46 69  file */.  unixFi
e2070 6c 65 20 2a 70 4e 65 77 20 20 20 20 20 20 20 20  le *pNew        
e2080 20 20 20 2f 2a 20 6f 70 65 6e 20 66 69 6c 65 20     /* open file 
e2090 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20 64  object for the d
e20a0 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a  atabase file */.
e20b0 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ){.  static cons
e20c0 74 20 73 74 72 75 63 74 20 4d 61 70 70 69 6e 67  t struct Mapping
e20d0 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61   {.    const cha
e20e0 72 20 2a 7a 46 69 6c 65 73 79 73 74 65 6d 3b 20  r *zFilesystem; 
e20f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e2100 46 69 6c 65 73 79 73 74 65 6d 20 74 79 70 65 20  Filesystem type 
e2110 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 63 6f 6e 73  name */.    cons
e2120 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
e2130 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 73 3b 20  hods *pMethods; 
e2140 20 20 2f 2a 20 41 70 70 72 6f 70 72 69 61 74 65    /* Appropriate
e2150 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20   locking method 
e2160 2a 2f 0a 20 20 7d 20 61 4d 61 70 5b 5d 20 3d 20  */.  } aMap[] = 
e2170 7b 0a 20 20 20 20 7b 20 22 68 66 73 22 2c 20 20  {.    { "hfs",  
e2180 20 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64    &posixIoMethod
e2190 73 20 7d 2c 0a 20 20 20 20 7b 20 22 75 66 73 22  s },.    { "ufs"
e21a0 2c 20 20 20 20 26 70 6f 73 69 78 49 6f 4d 65 74  ,    &posixIoMet
e21b0 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 22 61  hods },.    { "a
e21c0 66 70 66 73 22 2c 20 20 26 61 66 70 49 6f 4d 65  fpfs",  &afpIoMe
e21d0 74 68 6f 64 73 20 7d 2c 0a 23 69 66 64 65 66 20  thods },.#ifdef 
e21e0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 46  SQLITE_ENABLE_AF
e21f0 50 5f 4c 4f 43 4b 49 4e 47 5f 53 4d 42 0a 20 20  P_LOCKING_SMB.  
e2200 20 20 7b 20 22 73 6d 62 66 73 22 2c 20 20 26 61    { "smbfs",  &a
e2210 66 70 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 23  fpIoMethods },.#
e2220 65 6c 73 65 0a 20 20 20 20 7b 20 22 73 6d 62 66  else.    { "smbf
e2230 73 22 2c 20 20 26 66 6c 6f 63 6b 49 6f 4d 65 74  s",  &flockIoMet
e2240 68 6f 64 73 20 7d 2c 0a 23 65 6e 64 69 66 0a 20  hods },.#endif. 
e2250 20 20 20 7b 20 22 77 65 62 64 61 76 22 2c 20 26     { "webdav", &
e2260 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20  nolockIoMethods 
e2270 7d 2c 0a 20 20 20 20 7b 20 30 2c 20 30 20 7d 0a  },.    { 0, 0 }.
e2280 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20    };.  int i;.  
e2290 73 74 72 75 63 74 20 73 74 61 74 66 73 20 66 73  struct statfs fs
e22a0 49 6e 66 6f 3b 0a 20 20 73 74 72 75 63 74 20 66  Info;.  struct f
e22b0 6c 6f 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a  lock lockInfo;..
e22c0 20 20 69 66 28 20 21 66 69 6c 65 50 61 74 68 20    if( !filePath 
e22d0 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 66 69 6c  ){.    /* If fil
e22e0 65 50 61 74 68 3d 3d 4e 55 4c 4c 20 74 68 61 74  ePath==NULL that
e22f0 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20 64 65   means we are de
e2300 61 6c 69 6e 67 20 77 69 74 68 20 61 20 74 72 61  aling with a tra
e2310 6e 73 69 65 6e 74 20 66 69 6c 65 0a 20 20 20 20  nsient file.    
e2320 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74  ** that does not
e2330 20 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b   need to be lock
e2340 65 64 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  ed. */.    retur
e2350 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f  n &nolockIoMetho
e2360 64 73 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 74  ds;.  }.  if( st
e2370 61 74 66 73 28 66 69 6c 65 50 61 74 68 2c 20 26  atfs(filePath, &
e2380 66 73 49 6e 66 6f 29 20 21 3d 20 2d 31 20 29 7b  fsInfo) != -1 ){
e2390 0a 20 20 20 20 69 66 28 20 66 73 49 6e 66 6f 2e  .    if( fsInfo.
e23a0 66 5f 66 6c 61 67 73 20 26 20 4d 4e 54 5f 52 44  f_flags & MNT_RD
e23b0 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 72 65  ONLY ){.      re
e23c0 74 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65  turn &nolockIoMe
e23d0 74 68 6f 64 73 3b 0a 20 20 20 20 7d 0a 20 20 20  thods;.    }.   
e23e0 20 66 6f 72 28 69 3d 30 3b 20 61 4d 61 70 5b 69   for(i=0; aMap[i
e23f0 5d 2e 7a 46 69 6c 65 73 79 73 74 65 6d 3b 20 69  ].zFilesystem; i
e2400 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73  ++){.      if( s
e2410 74 72 63 6d 70 28 66 73 49 6e 66 6f 2e 66 5f 66  trcmp(fsInfo.f_f
e2420 73 74 79 70 65 6e 61 6d 65 2c 20 61 4d 61 70 5b  stypename, aMap[
e2430 69 5d 2e 7a 46 69 6c 65 73 79 73 74 65 6d 29 3d  i].zFilesystem)=
e2440 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  =0 ){.        re
e2450 74 75 72 6e 20 61 4d 61 70 5b 69 5d 2e 70 4d 65  turn aMap[i].pMe
e2460 74 68 6f 64 73 3b 0a 20 20 20 20 20 20 7d 0a 20  thods;.      }. 
e2470 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44     }.  }..  /* D
e2480 65 66 61 75 6c 74 20 63 61 73 65 2e 20 48 61 6e  efault case. Han
e2490 64 6c 65 73 2c 20 61 6d 6f 6e 67 73 74 20 6f 74  dles, amongst ot
e24a0 68 65 72 73 2c 20 22 6e 66 73 22 2e 0a 20 20 2a  hers, "nfs"..  *
e24b0 2a 20 54 65 73 74 20 62 79 74 65 2d 72 61 6e 67  * Test byte-rang
e24c0 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 66 63 6e  e lock using fcn
e24d0 74 6c 28 29 2e 20 49 66 20 74 68 65 20 63 61 6c  tl(). If the cal
e24e0 6c 20 73 75 63 63 65 65 64 73 2c 20 0a 20 20 2a  l succeeds, .  *
e24f0 2a 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68  * assume that th
e2500 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 75  e file-system su
e2510 70 70 6f 72 74 73 20 50 4f 53 49 58 20 73 74 79  pports POSIX sty
e2520 6c 65 20 6c 6f 63 6b 73 2e 20 0a 20 20 2a 2f 0a  le locks. .  */.
e2530 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c 65 6e    lockInfo.l_len
e2540 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f   = 1;.  lockInfo
e2550 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20 20  .l_start = 0;.  
e2560 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 65 6e 63  lockInfo.l_whenc
e2570 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20  e = SEEK_SET;.  
e2580 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 70 65 20  lockInfo.l_type 
e2590 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 69 66 28  = F_RDLCK;.  if(
e25a0 20 66 63 6e 74 6c 28 70 4e 65 77 2d 3e 68 2c 20   fcntl(pNew->h, 
e25b0 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 49 6e  F_GETLK, &lockIn
e25c0 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20 20 20  fo)!=-1 ) {.    
e25d0 70 4e 65 77 2d 3e 66 69 6c 65 46 6c 61 67 73 20  pNew->fileFlags 
e25e0 3d 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46  = SQLITE_WHOLE_F
e25f0 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3b 0a 20 20 20  ILE_LOCKING;.   
e2600 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 6f   return &posixIo
e2610 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65  Methods;.  }else
e2620 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 64 6f  {.    return &do
e2630 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a  tlockIoMethods;.
e2640 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e    }.}.static con
e2650 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  st sqlite3_io_me
e2660 74 68 6f 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73  thods .  *(*cons
e2670 74 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64  t autolockIoFind
e2680 65 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  er)(const char*,
e2690 75 6e 69 78 46 69 6c 65 2a 29 20 3d 20 61 75 74  unixFile*) = aut
e26a0 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70  olockIoFinderImp
e26b0 6c 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65  l;..#endif /* de
e26c0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
e26d0 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   && SQLITE_ENABL
e26e0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
e26f0 2a 2f 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52  */..#if OS_VXWOR
e2700 4b 53 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41  KS && SQLITE_ENA
e2710 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
e2720 45 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 22 66  E./* .** This "f
e2730 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 20  inder" function 
e2740 61 74 74 65 6d 70 74 73 20 74 6f 20 64 65 74 65  attempts to dete
e2750 72 6d 69 6e 65 20 74 68 65 20 62 65 73 74 20 6c  rmine the best l
e2760 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20  ocking strategy 
e2770 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 61 74 61  .** for the data
e2780 62 61 73 65 20 66 69 6c 65 20 22 66 69 6c 65 50  base file "fileP
e2790 61 74 68 22 2e 20 20 49 74 20 74 68 65 6e 20 72  ath".  It then r
e27a0 65 74 75 72 6e 73 20 74 68 65 20 73 71 6c 69 74  eturns the sqlit
e27b0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a  e3_io_methods.**
e27c0 20 6f 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70   object that imp
e27d0 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 73 74 72  lements that str
e27e0 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ategy..**.** Thi
e27f0 73 20 69 73 20 66 6f 72 20 56 58 57 6f 72 6b 73  s is for VXWorks
e2800 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63   only..*/.static
e2810 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69   const sqlite3_i
e2820 6f 5f 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f 6c  o_methods *autol
e2830 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 28  ockIoFinderImpl(
e2840 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66  .  const char *f
e2850 69 6c 65 50 61 74 68 2c 20 20 20 20 2f 2a 20 6e  ilePath,    /* n
e2860 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
e2870 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e  ase file */.  un
e2880 69 78 46 69 6c 65 20 2a 70 4e 65 77 20 20 20 20  ixFile *pNew    
e2890 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 6f 70         /* the op
e28a0 65 6e 20 66 69 6c 65 20 6f 62 6a 65 63 74 20 2a  en file object *
e28b0 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 66 6c  /.){.  struct fl
e28c0 6f 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a 20  ock lockInfo;.. 
e28d0 20 69 66 28 20 21 66 69 6c 65 50 61 74 68 20 29   if( !filePath )
e28e0 7b 0a 20 20 20 20 2f 2a 20 49 66 20 66 69 6c 65  {.    /* If file
e28f0 50 61 74 68 3d 3d 4e 55 4c 4c 20 74 68 61 74 20  Path==NULL that 
e2900 6d 65 61 6e 73 20 77 65 20 61 72 65 20 64 65 61  means we are dea
e2910 6c 69 6e 67 20 77 69 74 68 20 61 20 74 72 61 6e  ling with a tran
e2920 73 69 65 6e 74 20 66 69 6c 65 0a 20 20 20 20 2a  sient file.    *
e2930 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20  * that does not 
e2940 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65  need to be locke
e2950 64 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  d. */.    return
e2960 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64   &nolockIoMethod
e2970 73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73  s;.  }..  /* Tes
e2980 74 20 69 66 20 66 63 6e 74 6c 28 29 20 69 73 20  t if fcntl() is 
e2990 73 75 70 70 6f 72 74 65 64 20 61 6e 64 20 75 73  supported and us
e29a0 65 20 50 4f 53 49 58 20 73 74 79 6c 65 20 6c 6f  e POSIX style lo
e29b0 63 6b 73 2e 0a 20 20 2a 2a 20 4f 74 68 65 72 77  cks..  ** Otherw
e29c0 69 73 65 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f  ise fall back to
e29d0 20 74 68 65 20 6e 61 6d 65 64 20 73 65 6d 61 70   the named semap
e29e0 68 6f 72 65 20 6d 65 74 68 6f 64 2e 0a 20 20 2a  hore method..  *
e29f0 2f 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c  /.  lockInfo.l_l
e2a00 65 6e 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 6e  en = 1;.  lockIn
e2a10 66 6f 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a  fo.l_start = 0;.
e2a20 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 65    lockInfo.l_whe
e2a30 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a  nce = SEEK_SET;.
e2a40 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 70    lockInfo.l_typ
e2a50 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 69  e = F_RDLCK;.  i
e2a60 66 28 20 66 63 6e 74 6c 28 70 4e 65 77 2d 3e 68  f( fcntl(pNew->h
e2a70 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b  , F_GETLK, &lock
e2a80 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20  Info)!=-1 ) {.  
e2a90 20 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49    return &posixI
e2aa0 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73  oMethods;.  }els
e2ab0 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 73  e{.    return &s
e2ac0 65 6d 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d  emIoMethods;.  }
e2ad0 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .}.static const 
e2ae0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
e2af0 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74 20 61  ds .  *(*const a
e2b00 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 29  utolockIoFinder)
e2b10 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69  (const char*,uni
e2b20 78 46 69 6c 65 2a 29 20 3d 20 61 75 74 6f 6c 6f  xFile*) = autolo
e2b30 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 3b 0a  ckIoFinderImpl;.
e2b40 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58  .#endif /* OS_VX
e2b50 57 4f 52 4b 53 20 26 26 20 53 51 4c 49 54 45 5f  WORKS && SQLITE_
e2b60 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
e2b70 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  TYLE */../*.** A
e2b80 6e 20 61 62 73 74 72 61 63 74 20 74 79 70 65 20  n abstract type 
e2b90 66 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  for a pointer to
e2ba0 20 61 20 49 4f 20 6d 65 74 68 6f 64 20 66 69 6e   a IO method fin
e2bb0 64 65 72 20 66 75 6e 63 74 69 6f 6e 3a 0a 2a 2f  der function:.*/
e2bc0 0a 74 79 70 65 64 65 66 20 63 6f 6e 73 74 20 73  .typedef const s
e2bd0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
e2be0 73 20 2a 28 2a 66 69 6e 64 65 72 5f 74 79 70 65  s *(*finder_type
e2bf0 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e  )(const char*,un
e2c00 69 78 46 69 6c 65 2a 29 3b 0a 0a 0a 2f 2a 2a 2a  ixFile*);.../***
e2c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e2c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e2c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e2c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e2c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a  *********.******
e2c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e2c70 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 76  ****** sqlite3_v
e2c80 66 73 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a  fs methods *****
e2c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e2ca0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
e2cb0 69 73 20 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74  is division cont
e2cc0 61 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65  ains the impleme
e2cd0 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 65 74 68 6f  ntation of metho
e2ce0 64 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 71 6c  ds on the.** sql
e2cf0 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e  ite3_vfs object.
e2d00 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  .*/../*.** Initi
e2d10 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e 74 65 6e  alize the conten
e2d20 74 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69  ts of the unixFi
e2d30 6c 65 20 73 74 72 75 63 74 75 72 65 20 70 6f 69  le structure poi
e2d40 6e 74 65 64 20 74 6f 20 62 79 20 70 49 64 2e 0a  nted to by pId..
e2d50 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69  */.static int fi
e2d60 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 0a 20 20  llInUnixFile(.  
e2d70 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
e2d80 73 2c 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74  s,      /* Point
e2d90 65 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74  er to vfs object
e2da0 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 20 20 20   */.  int h,    
e2db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e2dc0 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72   Open file descr
e2dd0 69 70 74 6f 72 20 6f 66 20 66 69 6c 65 20 62 65  iptor of file be
e2de0 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20  ing opened */.  
e2df0 69 6e 74 20 64 69 72 66 64 2c 20 20 20 20 20 20  int dirfd,      
e2e00 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63          /* Direc
e2e10 74 6f 72 79 20 66 69 6c 65 20 64 65 73 63 72 69  tory file descri
e2e20 70 74 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ptor */.  sqlite
e2e30 33 5f 66 69 6c 65 20 2a 70 49 64 2c 20 20 20 20  3_file *pId,    
e2e40 20 20 2f 2a 20 57 72 69 74 65 20 74 6f 20 74 68    /* Write to th
e2e50 65 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63  e unixFile struc
e2e60 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63  ture here */.  c
e2e70 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
e2e80 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f  name,  /* Name o
e2e90 66 20 74 68 65 20 66 69 6c 65 20 62 65 69 6e 67  f the file being
e2ea0 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e 74   opened */.  int
e2eb0 20 6e 6f 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20   noLock,        
e2ec0 20 20 20 20 20 2f 2a 20 4f 6d 69 74 20 6c 6f 63       /* Omit loc
e2ed0 6b 69 6e 67 20 69 66 20 74 72 75 65 20 2a 2f 0a  king if true */.
e2ee0 20 20 69 6e 74 20 69 73 44 65 6c 65 74 65 20 20    int isDelete  
e2ef0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 6c            /* Del
e2f00 65 74 65 20 6f 6e 20 63 6c 6f 73 65 20 69 66 20  ete on close if 
e2f10 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e  true */.){.  con
e2f20 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  st sqlite3_io_me
e2f30 74 68 6f 64 73 20 2a 70 4c 6f 63 6b 69 6e 67 53  thods *pLockingS
e2f40 74 79 6c 65 3b 0a 20 20 75 6e 69 78 46 69 6c 65  tyle;.  unixFile
e2f50 20 2a 70 4e 65 77 20 3d 20 28 75 6e 69 78 46 69   *pNew = (unixFi
e2f60 6c 65 20 2a 29 70 49 64 3b 0a 20 20 69 6e 74 20  le *)pId;.  int 
e2f70 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
e2f80 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d  .  assert( pNew-
e2f90 3e 70 4c 6f 63 6b 3d 3d 4e 55 4c 4c 20 29 3b 0a  >pLock==NULL );.
e2fa0 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e    assert( pNew->
e2fb0 70 4f 70 65 6e 3d 3d 4e 55 4c 4c 20 29 3b 0a 0a  pOpen==NULL );..
e2fc0 20 20 2f 2a 20 50 61 72 61 6d 65 74 65 72 20 69    /* Parameter i
e2fd0 73 44 65 6c 65 74 65 20 69 73 20 6f 6e 6c 79 20  sDelete is only 
e2fe0 75 73 65 64 20 6f 6e 20 76 78 77 6f 72 6b 73 2e  used on vxworks.
e2ff0 20 45 78 70 72 65 73 73 20 74 68 69 73 20 65 78   Express this ex
e3000 70 6c 69 63 69 74 6c 79 20 0a 20 20 2a 2a 20 68  plicitly .  ** h
e3010 65 72 65 20 74 6f 20 70 72 65 76 65 6e 74 20 63  ere to prevent c
e3020 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 73  ompiler warnings
e3030 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20 70 61   about unused pa
e3040 72 61 6d 65 74 65 72 73 2e 0a 20 20 2a 2f 0a 20  rameters..  */. 
e3050 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
e3060 52 28 69 73 44 65 6c 65 74 65 29 3b 0a 0a 20 20  R(isDelete);..  
e3070 4f 53 54 52 41 43 45 33 28 22 4f 50 45 4e 20 20  OSTRACE3("OPEN  
e3080 20 20 25 2d 33 64 20 25 73 5c 6e 22 2c 20 68 2c    %-3d %s\n", h,
e3090 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 20 20 20 20   zFilename);    
e30a0 0a 20 20 70 4e 65 77 2d 3e 68 20 3d 20 68 3b 0a  .  pNew->h = h;.
e30b0 20 20 70 4e 65 77 2d 3e 64 69 72 66 64 20 3d 20    pNew->dirfd = 
e30c0 64 69 72 66 64 3b 0a 20 20 53 45 54 5f 54 48 52  dirfd;.  SET_THR
e30d0 45 41 44 49 44 28 70 4e 65 77 29 3b 0a 20 20 70  EADID(pNew);.  p
e30e0 4e 65 77 2d 3e 66 69 6c 65 46 6c 61 67 73 20 3d  New->fileFlags =
e30f0 20 30 3b 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f   0;..#if OS_VXWO
e3100 52 4b 53 0a 20 20 70 4e 65 77 2d 3e 70 49 64 20  RKS.  pNew->pId 
e3110 3d 20 76 78 77 6f 72 6b 73 46 69 6e 64 46 69 6c  = vxworksFindFil
e3120 65 49 64 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  eId(zFilename);.
e3130 20 20 69 66 28 20 70 4e 65 77 2d 3e 70 49 64 3d    if( pNew->pId=
e3140 3d 30 20 29 7b 0a 20 20 20 20 6e 6f 4c 6f 63 6b  =0 ){.    noLock
e3150 20 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 53   = 1;.    rc = S
e3160 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
e3170 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6e  .#endif..  if( n
e3180 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f  oLock ){.    pLo
e3190 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20 26 6e 6f  ckingStyle = &no
e31a0 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20  lockIoMethods;. 
e31b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4c 6f 63   }else{.    pLoc
e31c0 6b 69 6e 67 53 74 79 6c 65 20 3d 20 28 2a 2a 28  kingStyle = (**(
e31d0 66 69 6e 64 65 72 5f 74 79 70 65 2a 29 70 56 66  finder_type*)pVf
e31e0 73 2d 3e 70 41 70 70 44 61 74 61 29 28 7a 46 69  s->pAppData)(zFi
e31f0 6c 65 6e 61 6d 65 2c 20 70 4e 65 77 29 3b 0a 23  lename, pNew);.#
e3200 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  if SQLITE_ENABLE
e3210 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20  _LOCKING_STYLE. 
e3220 20 20 20 2f 2a 20 43 61 63 68 65 20 7a 46 69 6c     /* Cache zFil
e3230 65 6e 61 6d 65 20 69 6e 20 74 68 65 20 6c 6f 63  ename in the loc
e3240 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 28 41 46  king context (AF
e3250 50 20 61 6e 64 20 64 6f 74 6c 6f 63 6b 20 6f 76  P and dotlock ov
e3260 65 72 72 69 64 65 29 20 66 6f 72 0a 20 20 20 20  erride) for.    
e3270 2a 2a 20 70 72 6f 78 79 4c 6f 63 6b 20 61 63 74  ** proxyLock act
e3280 69 76 61 74 69 6f 6e 20 69 73 20 70 6f 73 73 69  ivation is possi
e3290 62 6c 65 20 28 72 65 6d 6f 74 65 20 70 72 6f 78  ble (remote prox
e32a0 79 20 69 73 20 62 61 73 65 64 20 6f 6e 20 64 62  y is based on db
e32b0 20 6e 61 6d 65 29 0a 20 20 20 20 2a 2a 20 7a 46   name).    ** zF
e32c0 69 6c 65 6e 61 6d 65 20 72 65 6d 61 69 6e 73 20  ilename remains 
e32d0 76 61 6c 69 64 20 75 6e 74 69 6c 20 66 69 6c 65  valid until file
e32e0 20 69 73 20 63 6c 6f 73 65 64 2c 20 74 6f 20 73   is closed, to s
e32f0 75 70 70 6f 72 74 20 2a 2f 0a 20 20 20 20 70 4e  upport */.    pN
e3300 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  ew->lockingConte
e3310 78 74 20 3d 20 28 76 6f 69 64 2a 29 7a 46 69 6c  xt = (void*)zFil
e3320 65 6e 61 6d 65 3b 0a 23 65 6e 64 69 66 0a 20 20  ename;.#endif.  
e3330 7d 0a 0a 20 20 69 66 28 20 70 4c 6f 63 6b 69 6e  }..  if( pLockin
e3340 67 53 74 79 6c 65 20 3d 3d 20 26 70 6f 73 69 78  gStyle == &posix
e3350 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20  IoMethods ){.   
e3360 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28   unixEnterMutex(
e3370 29 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64  );.    rc = find
e3380 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26  LockInfo(pNew, &
e3390 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e  pNew->pLock, &pN
e33a0 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20  ew->pOpen);.    
e33b0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
e33c0 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66  K ){.      /* If
e33d0 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 65   an error occure
e33e0 64 20 69 6e 20 66 69 6e 64 4c 6f 63 6b 49 6e 66  d in findLockInf
e33f0 6f 28 29 2c 20 63 6c 6f 73 65 20 74 68 65 20 66  o(), close the f
e3400 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a 20  ile descriptor. 
e3410 20 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74       ** immediat
e3420 65 6c 79 2c 20 62 65 66 6f 72 65 20 72 65 6c 65  ely, before rele
e3430 61 73 69 6e 67 20 74 68 65 20 6d 75 74 65 78 2e  asing the mutex.
e3440 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 20   findLockInfo() 
e3450 6d 61 79 20 66 61 69 6c 0a 20 20 20 20 20 20 2a  may fail.      *
e3460 2a 20 69 6e 20 74 77 6f 20 73 63 65 6e 61 72 69  * in two scenari
e3470 6f 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  os:.      **.   
e3480 20 20 20 2a 2a 20 20 20 28 61 29 20 41 20 63 61     **   (a) A ca
e3490 6c 6c 20 74 6f 20 66 73 74 61 74 28 29 20 66 61  ll to fstat() fa
e34a0 69 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20 20  iled..      **  
e34b0 20 28 62 29 20 41 20 6d 61 6c 6c 6f 63 20 66 61   (b) A malloc fa
e34c0 69 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20  iled..      **. 
e34d0 20 20 20 20 20 2a 2a 20 53 63 65 6e 61 72 69 6f       ** Scenario
e34e0 20 28 62 29 20 6d 61 79 20 6f 6e 6c 79 20 6f 63   (b) may only oc
e34f0 63 75 72 20 69 66 20 74 68 65 20 70 72 6f 63 65  cur if the proce
e3500 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 6e 6f  ss is holding no
e3510 20 6f 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20   other.      ** 
e3520 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73  file descriptors
e3530 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d   open on the sam
e3540 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65  e file. If there
e3550 20 77 65 72 65 20 6f 74 68 65 72 20 66 69 6c 65   were other file
e3560 0a 20 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69  .      ** descri
e3570 70 74 6f 72 73 20 6f 6e 20 74 68 69 73 20 66 69  ptors on this fi
e3580 6c 65 2c 20 74 68 65 6e 20 6e 6f 20 6d 61 6c 6c  le, then no mall
e3590 6f 63 20 77 6f 75 6c 64 20 62 65 20 72 65 71 75  oc would be requ
e35a0 69 72 65 64 20 62 79 0a 20 20 20 20 20 20 2a 2a  ired by.      **
e35b0 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e   findLockInfo().
e35c0 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
e35d0 63 61 73 65 2c 20 69 74 20 69 73 20 71 75 69 74  case, it is quit
e35e0 65 20 73 61 66 65 20 74 6f 20 63 6c 6f 73 65 0a  e safe to close.
e35f0 20 20 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 20        ** handle 
e3600 68 20 2d 20 61 73 20 69 74 20 69 73 20 67 75 61  h - as it is gua
e3610 72 61 6e 74 65 65 64 20 74 68 61 74 20 6e 6f 20  ranteed that no 
e3620 70 6f 73 69 78 20 6c 6f 63 6b 73 20 77 69 6c 6c  posix locks will
e3630 20 62 65 20 72 65 6c 65 61 73 65 64 0a 20 20 20   be released.   
e3640 20 20 20 2a 2a 20 62 79 20 64 6f 69 6e 67 20 73     ** by doing s
e3650 6f 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  o..      **.    
e3660 20 20 2a 2a 20 49 66 20 73 63 65 6e 61 72 69 6f    ** If scenario
e3670 20 28 61 29 20 63 61 75 73 65 64 20 74 68 65 20   (a) caused the 
e3680 65 72 72 6f 72 20 74 68 65 6e 20 74 68 69 6e 67  error then thing
e3690 73 20 61 72 65 20 6e 6f 74 20 73 6f 20 73 61 66  s are not so saf
e36a0 65 2e 20 54 68 65 0a 20 20 20 20 20 20 2a 2a 20  e. The.      ** 
e36b0 69 6d 70 6c 69 63 69 74 20 61 73 73 75 6d 70 74  implicit assumpt
e36c0 69 6f 6e 20 68 65 72 65 20 69 73 20 74 68 61 74  ion here is that
e36d0 20 69 66 20 66 73 74 61 74 28 29 20 66 61 69 6c   if fstat() fail
e36e0 73 2c 20 74 68 69 6e 67 73 20 61 72 65 20 69 6e  s, things are in
e36f0 0a 20 20 20 20 20 20 2a 2a 20 73 75 63 68 20 62  .      ** such b
e3700 61 64 20 73 68 61 70 65 20 74 68 61 74 20 64 72  ad shape that dr
e3710 6f 70 70 69 6e 67 20 61 20 6c 6f 63 6b 20 6f 72  opping a lock or
e3720 20 74 77 6f 20 64 6f 65 73 6e 27 74 20 6d 61 74   two doesn't mat
e3730 74 65 72 20 6d 75 63 68 2e 0a 20 20 20 20 20 20  ter much..      
e3740 2a 2f 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 68  */.      close(h
e3750 29 3b 0a 20 20 20 20 20 20 68 20 3d 20 2d 31 3b  );.      h = -1;
e3760 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c  .    }.    unixL
e3770 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d  eaveMutex();.  }
e3780 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  ..#if SQLITE_ENA
e3790 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
e37a0 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41  E && defined(__A
e37b0 50 50 4c 45 5f 5f 29 0a 20 20 65 6c 73 65 20 69  PPLE__).  else i
e37c0 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65  f( pLockingStyle
e37d0 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f 64   == &afpIoMethod
e37e0 73 20 29 7b 0a 20 20 20 20 2f 2a 20 41 46 50 20  s ){.    /* AFP 
e37f0 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65  locking uses the
e3800 20 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74   file path so it
e3810 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e 63   needs to be inc
e3820 6c 75 64 65 64 20 69 6e 0a 20 20 20 20 2a 2a 20  luded in.    ** 
e3830 74 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f  the afpLockingCo
e3840 6e 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  ntext..    */.  
e3850 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74    afpLockingCont
e3860 65 78 74 20 2a 70 43 74 78 3b 0a 20 20 20 20 70  ext *pCtx;.    p
e3870 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  New->lockingCont
e3880 65 78 74 20 3d 20 70 43 74 78 20 3d 20 73 71 6c  ext = pCtx = sql
e3890 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a  ite3_malloc( siz
e38a0 65 6f 66 28 2a 70 43 74 78 29 20 29 3b 0a 20 20  eof(*pCtx) );.  
e38b0 20 20 69 66 28 20 70 43 74 78 3d 3d 30 20 29 7b    if( pCtx==0 ){
e38c0 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
e38d0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65  TE_NOMEM;.    }e
e38e0 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 42  lse{.      /* NB
e38f0 3a 20 7a 46 69 6c 65 6e 61 6d 65 20 65 78 69 73  : zFilename exis
e3900 74 73 20 61 6e 64 20 72 65 6d 61 69 6e 73 20 76  ts and remains v
e3910 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 66  alid until the f
e3920 69 6c 65 20 69 73 20 63 6c 6f 73 65 64 0a 20 20  ile is closed.  
e3930 20 20 20 20 2a 2a 20 61 63 63 6f 72 64 69 6e 67      ** according
e3940 20 74 6f 20 72 65 71 75 69 72 65 6d 65 6e 74 20   to requirement 
e3950 46 31 31 31 34 31 2e 20 20 53 6f 20 77 65 20 64  F11141.  So we d
e3960 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61  o not need to ma
e3970 6b 65 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f  ke a.      ** co
e3980 70 79 20 6f 66 20 74 68 65 20 66 69 6c 65 6e 61  py of the filena
e3990 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 70 43 74  me. */.      pCt
e39a0 78 2d 3e 64 62 50 61 74 68 20 3d 20 7a 46 69 6c  x->dbPath = zFil
e39b0 65 6e 61 6d 65 3b 0a 20 20 20 20 20 20 73 72 61  ename;.      sra
e39c0 6e 64 6f 6d 64 65 76 28 29 3b 0a 20 20 20 20 20  ndomdev();.     
e39d0 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28   unixEnterMutex(
e39e0 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69  );.      rc = fi
e39f0 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c  ndLockInfo(pNew,
e3a00 20 4e 55 4c 4c 2c 20 26 70 4e 65 77 2d 3e 70 4f   NULL, &pNew->pO
e3a10 70 65 6e 29 3b 0a 20 20 20 20 20 20 75 6e 69 78  pen);.      unix
e3a20 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 20 20 20  LeaveMutex();   
e3a30 20 20 20 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a       .    }.  }.
e3a40 23 65 6e 64 69 66 0a 0a 20 20 65 6c 73 65 20 69  #endif..  else i
e3a50 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65  f( pLockingStyle
e3a60 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65   == &dotlockIoMe
e3a70 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20  thods ){.    /* 
e3a80 44 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20  Dotfile locking 
e3a90 75 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61  uses the file pa
e3aa0 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74  th so it needs t
e3ab0 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 69 6e  o be included in
e3ac0 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 6f 74 6c  .    ** the dotl
e3ad0 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  ockLockingContex
e3ae0 74 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 68  t .    */.    ch
e3af0 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20  ar *zLockFile;. 
e3b00 20 20 20 69 6e 74 20 6e 46 69 6c 65 6e 61 6d 65     int nFilename
e3b10 3b 0a 20 20 20 20 6e 46 69 6c 65 6e 61 6d 65 20  ;.    nFilename 
e3b20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 46  = (int)strlen(zF
e3b30 69 6c 65 6e 61 6d 65 29 20 2b 20 36 3b 0a 20 20  ilename) + 6;.  
e3b40 20 20 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63    zLockFile = (c
e3b50 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61  har *)sqlite3_ma
e3b60 6c 6c 6f 63 28 6e 46 69 6c 65 6e 61 6d 65 29 3b  lloc(nFilename);
e3b70 0a 20 20 20 20 69 66 28 20 7a 4c 6f 63 6b 46 69  .    if( zLockFi
e3b80 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  le==0 ){.      r
e3b90 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
e3ba0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
e3bb0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
e3bc0 6e 74 66 28 6e 46 69 6c 65 6e 61 6d 65 2c 20 7a  ntf(nFilename, z
e3bd0 4c 6f 63 6b 46 69 6c 65 2c 20 22 25 73 22 20 44  LockFile, "%s" D
e3be0 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 2c 20 7a  OTLOCK_SUFFIX, z
e3bf0 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7d  Filename);.    }
e3c00 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69  .    pNew->locki
e3c10 6e 67 43 6f 6e 74 65 78 74 20 3d 20 7a 4c 6f 63  ngContext = zLoc
e3c20 6b 46 69 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 20  kFile;.  }..#if 
e3c30 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 65 6c 73  OS_VXWORKS.  els
e3c40 65 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74  e if( pLockingSt
e3c50 79 6c 65 20 3d 3d 20 26 73 65 6d 49 6f 4d 65 74  yle == &semIoMet
e3c60 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4e  hods ){.    /* N
e3c70 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c  amed semaphore l
e3c80 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20  ocking uses the 
e3c90 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20  file path so it 
e3ca0 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20  needs to be.    
e3cb0 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74  ** included in t
e3cc0 68 65 20 73 65 6d 4c 6f 63 6b 69 6e 67 43 6f 6e  he semLockingCon
e3cd0 74 65 78 74 0a 20 20 20 20 2a 2f 0a 20 20 20 20  text.    */.    
e3ce0 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29  unixEnterMutex()
e3cf0 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c  ;.    rc = findL
e3d00 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26 70  ockInfo(pNew, &p
e3d10 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65  New->pLock, &pNe
e3d20 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 69  w->pOpen);.    i
e3d30 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  f( (rc==SQLITE_O
e3d40 4b 29 20 26 26 20 28 70 4e 65 77 2d 3e 70 4f 70  K) && (pNew->pOp
e3d50 65 6e 2d 3e 70 53 65 6d 3d 3d 4e 55 4c 4c 29 20  en->pSem==NULL) 
e3d60 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  ){.      char *z
e3d70 53 65 6d 4e 61 6d 65 20 3d 20 70 4e 65 77 2d 3e  SemName = pNew->
e3d80 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 3b  pOpen->aSemName;
e3d90 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20  .      int n;.  
e3da0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
e3db0 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e 41 4d  intf(MAX_PATHNAM
e3dc0 45 2c 20 7a 53 65 6d 4e 61 6d 65 2c 20 22 2f 25  E, zSemName, "/%
e3dd0 73 2e 73 65 6d 22 2c 0a 20 20 20 20 20 20 20 20  s.sem",.        
e3de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
e3df0 4e 65 77 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e  New->pId->zCanon
e3e00 69 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20  icalName);.     
e3e10 20 66 6f 72 28 20 6e 3d 31 3b 20 7a 53 65 6d 4e   for( n=1; zSemN
e3e20 61 6d 65 5b 6e 5d 3b 20 6e 2b 2b 20 29 0a 20 20  ame[n]; n++ ).  
e3e30 20 20 20 20 20 20 69 66 28 20 7a 53 65 6d 4e 61        if( zSemNa
e3e40 6d 65 5b 6e 5d 3d 3d 27 2f 27 20 29 20 7a 53 65  me[n]=='/' ) zSe
e3e50 6d 4e 61 6d 65 5b 6e 5d 20 3d 20 27 5f 27 3b 0a  mName[n] = '_';.
e3e60 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4f 70 65        pNew->pOpe
e3e70 6e 2d 3e 70 53 65 6d 20 3d 20 73 65 6d 5f 6f 70  n->pSem = sem_op
e3e80 65 6e 28 7a 53 65 6d 4e 61 6d 65 2c 20 4f 5f 43  en(zSemName, O_C
e3e90 52 45 41 54 2c 20 30 36 36 36 2c 20 31 29 3b 0a  REAT, 0666, 1);.
e3ea0 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 2d 3e        if( pNew->
e3eb0 70 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d 3d 20 53  pOpen->pSem == S
e3ec0 45 4d 5f 46 41 49 4c 45 44 20 29 7b 0a 20 20 20  EM_FAILED ){.   
e3ed0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
e3ee0 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20  _NOMEM;.        
e3ef0 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 61 53 65  pNew->pOpen->aSe
e3f00 6d 4e 61 6d 65 5b 30 5d 20 3d 20 27 5c 30 27 3b  mName[0] = '\0';
e3f10 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
e3f20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65     unixLeaveMute
e3f30 78 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  x();.  }.#endif.
e3f40 20 20 0a 20 20 70 4e 65 77 2d 3e 6c 61 73 74 45    .  pNew->lastE
e3f50 72 72 6e 6f 20 3d 20 30 3b 0a 23 69 66 20 4f 53  rrno = 0;.#if OS
e3f60 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72  _VXWORKS.  if( r
e3f70 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
e3f80 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 46 69 6c 65      unlink(zFile
e3f90 6e 61 6d 65 29 3b 0a 20 20 20 20 69 73 44 65 6c  name);.    isDel
e3fa0 65 74 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70  ete = 0;.  }.  p
e3fb0 4e 65 77 2d 3e 69 73 44 65 6c 65 74 65 20 3d 20  New->isDelete = 
e3fc0 69 73 44 65 6c 65 74 65 3b 0a 23 65 6e 64 69 66  isDelete;.#endif
e3fd0 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
e3fe0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20  E_OK ){.    if( 
e3ff0 64 69 72 66 64 3e 3d 30 20 29 20 63 6c 6f 73 65  dirfd>=0 ) close
e4000 28 64 69 72 66 64 29 3b 20 2f 2a 20 73 69 6c 65  (dirfd); /* sile
e4010 6e 74 20 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c  nt leak if fail,
e4020 20 61 6c 72 65 61 64 79 20 69 6e 20 65 72 72 6f   already in erro
e4030 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 68 3e 3d  r */.    if( h>=
e4040 30 20 29 20 63 6c 6f 73 65 28 68 29 3b 0a 20 20  0 ) close(h);.  
e4050 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d  }else{.    pNew-
e4060 3e 70 4d 65 74 68 6f 64 20 3d 20 70 4c 6f 63 6b  >pMethod = pLock
e4070 69 6e 67 53 74 79 6c 65 3b 0a 20 20 20 20 4f 70  ingStyle;.    Op
e4080 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20  enCounter(+1);. 
e4090 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
e40a0 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20  }../*.** Open a 
e40b0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
e40c0 74 6f 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  to the directory
e40d0 20 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69 6c 65   containing file
e40e0 20 7a 46 69 6c 65 6e 61 6d 65 2e 0a 2a 2a 20 49   zFilename..** I
e40f0 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 2a 70  f successful, *p
e4100 46 64 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  Fd is set to the
e4110 20 6f 70 65 6e 65 64 20 66 69 6c 65 20 64 65 73   opened file des
e4120 63 72 69 70 74 6f 72 20 61 6e 64 0a 2a 2a 20 53  criptor and.** S
e4130 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
e4140 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72 6f  rned. If an erro
e4150 72 20 6f 63 63 75 72 73 2c 20 65 69 74 68 65 72  r occurs, either
e4160 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a   SQLITE_NOMEM.**
e4170 20 6f 72 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f   or SQLITE_CANTO
e4180 50 45 4e 20 69 73 20 72 65 74 75 72 6e 65 64 20  PEN is returned 
e4190 61 6e 64 20 2a 70 46 64 20 69 73 20 73 65 74 20  and *pFd is set 
e41a0 74 6f 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 0a  to an undefined.
e41b0 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20  ** value..**.** 
e41c0 49 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  If SQLITE_OK is 
e41d0 72 65 74 75 72 6e 65 64 2c 20 74 68 65 20 63 61  returned, the ca
e41e0 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69  ller is responsi
e41f0 62 6c 65 20 66 6f 72 20 63 6c 6f 73 69 6e 67 0a  ble for closing.
e4200 2a 2a 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  ** the file desc
e4210 72 69 70 74 6f 72 20 2a 70 46 64 20 75 73 69 6e  riptor *pFd usin
e4220 67 20 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a 73 74  g close()..*/.st
e4230 61 74 69 63 20 69 6e 74 20 6f 70 65 6e 44 69 72  atic int openDir
e4240 65 63 74 6f 72 79 28 63 6f 6e 73 74 20 63 68 61  ectory(const cha
e4250 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 69 6e  r *zFilename, in
e4260 74 20 2a 70 46 64 29 7b 0a 20 20 69 6e 74 20 69  t *pFd){.  int i
e4270 69 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20 2d 31  i;.  int fd = -1
e4280 3b 0a 20 20 63 68 61 72 20 7a 44 69 72 6e 61 6d  ;.  char zDirnam
e4290 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31  e[MAX_PATHNAME+1
e42a0 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e  ];..  sqlite3_sn
e42b0 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e  printf(MAX_PATHN
e42c0 41 4d 45 2c 20 7a 44 69 72 6e 61 6d 65 2c 20 22  AME, zDirname, "
e42d0 25 73 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b  %s", zFilename);
e42e0 0a 20 20 66 6f 72 28 69 69 3d 28 69 6e 74 29 73  .  for(ii=(int)s
e42f0 74 72 6c 65 6e 28 7a 44 69 72 6e 61 6d 65 29 3b  trlen(zDirname);
e4300 20 69 69 3e 31 20 26 26 20 7a 44 69 72 6e 61 6d   ii>1 && zDirnam
e4310 65 5b 69 69 5d 21 3d 27 2f 27 3b 20 69 69 2d 2d  e[ii]!='/'; ii--
e4320 29 3b 0a 20 20 69 66 28 20 69 69 3e 30 20 29 7b  );.  if( ii>0 ){
e4330 0a 20 20 20 20 7a 44 69 72 6e 61 6d 65 5b 69 69  .    zDirname[ii
e4340 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 66 64  ] = '\0';.    fd
e4350 20 3d 20 6f 70 65 6e 28 7a 44 69 72 6e 61 6d 65   = open(zDirname
e4360 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 42 49 4e  , O_RDONLY|O_BIN
e4370 41 52 59 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  ARY, 0);.    if(
e4380 20 66 64 3e 3d 30 20 29 7b 0a 23 69 66 64 65 66   fd>=0 ){.#ifdef
e4390 20 46 44 5f 43 4c 4f 45 58 45 43 0a 20 20 20 20   FD_CLOEXEC.    
e43a0 20 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45    fcntl(fd, F_SE
e43b0 54 46 44 2c 20 66 63 6e 74 6c 28 66 64 2c 20 46  TFD, fcntl(fd, F
e43c0 5f 47 45 54 46 44 2c 20 30 29 20 7c 20 46 44 5f  _GETFD, 0) | FD_
e43d0 43 4c 4f 45 58 45 43 29 3b 0a 23 65 6e 64 69 66  CLOEXEC);.#endif
e43e0 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28  .      OSTRACE3(
e43f0 22 4f 50 45 4e 44 49 52 20 25 2d 33 64 20 25 73  "OPENDIR %-3d %s
e4400 5c 6e 22 2c 20 66 64 2c 20 7a 44 69 72 6e 61 6d  \n", fd, zDirnam
e4410 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e);.    }.  }.  
e4420 2a 70 46 64 20 3d 20 66 64 3b 0a 20 20 72 65 74  *pFd = fd;.  ret
e4430 75 72 6e 20 28 66 64 3e 3d 30 3f 53 51 4c 49 54  urn (fd>=0?SQLIT
e4440 45 5f 4f 4b 3a 53 51 4c 49 54 45 5f 43 41 4e 54  E_OK:SQLITE_CANT
e4450 4f 50 45 4e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  OPEN);.}../*.** 
e4460 43 72 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61  Create a tempora
e4470 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20  ry file name in 
e4480 7a 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74  zBuf.  zBuf must
e4490 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a   be allocated.**
e44a0 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20   by the calling 
e44b0 70 72 6f 63 65 73 73 20 61 6e 64 20 6d 75 73 74  process and must
e44c0 20 62 65 20 62 69 67 20 65 6e 6f 75 67 68 20 74   be big enough t
e44d0 6f 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 0a  o hold at least.
e44e0 2a 2a 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e  ** pVfs->mxPathn
e44f0 61 6d 65 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74  ame bytes..*/.st
e4500 61 74 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70  atic int getTemp
e4510 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63  name(int nBuf, c
e4520 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74  har *zBuf){.  st
e4530 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
e4540 2a 61 7a 44 69 72 73 5b 5d 20 3d 20 7b 0a 20 20  *azDirs[] = {.  
e4550 20 20 20 30 2c 0a 20 20 20 20 20 30 2c 0a 20 20     0,.     0,.  
e4560 20 20 20 22 2f 76 61 72 2f 74 6d 70 22 2c 0a 20     "/var/tmp",. 
e4570 20 20 20 20 22 2f 75 73 72 2f 74 6d 70 22 2c 0a      "/usr/tmp",.
e4580 20 20 20 20 20 22 2f 74 6d 70 22 2c 0a 20 20 20       "/tmp",.   
e4590 20 20 22 2e 22 2c 0a 20 20 7d 3b 0a 20 20 73 74    ".",.  };.  st
e45a0 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
e45b0 6e 65 64 20 63 68 61 72 20 7a 43 68 61 72 73 5b  ned char zChars[
e45c0 5d 20 3d 0a 20 20 20 20 22 61 62 63 64 65 66 67  ] =.    "abcdefg
e45d0 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77  hijklmnopqrstuvw
e45e0 78 79 7a 22 0a 20 20 20 20 22 41 42 43 44 45 46  xyz".    "ABCDEF
e45f0 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56  GHIJKLMNOPQRSTUV
e4600 57 58 59 5a 22 0a 20 20 20 20 22 30 31 32 33 34  WXYZ".    "01234
e4610 35 36 37 38 39 22 3b 0a 20 20 75 6e 73 69 67 6e  56789";.  unsign
e4620 65 64 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73  ed int i, j;.  s
e4630 74 72 75 63 74 20 73 74 61 74 20 62 75 66 3b 0a  truct stat buf;.
e4640 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
e4650 69 72 20 3d 20 22 2e 22 3b 0a 0a 20 20 2f 2a 20  ir = ".";..  /* 
e4660 49 74 27 73 20 6f 64 64 20 74 6f 20 73 69 6d 75  It's odd to simu
e4670 6c 61 74 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72  late an io-error
e4680 20 68 65 72 65 2c 20 62 75 74 20 72 65 61 6c 6c   here, but reall
e4690 79 20 74 68 69 73 20 69 73 20 6a 75 73 74 0a 20  y this is just. 
e46a0 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 69 6f   ** using the io
e46b0 2d 65 72 72 6f 72 20 69 6e 66 72 61 73 74 72 75  -error infrastru
e46c0 63 74 75 72 65 20 74 6f 20 74 65 73 74 20 74 68  cture to test th
e46d0 61 74 20 53 51 4c 69 74 65 20 68 61 6e 64 6c 65  at SQLite handle
e46e0 73 20 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63  s this.  ** func
e46f0 74 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 0a 20  tion failing. . 
e4700 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f   */.  SimulateIO
e4710 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
e4720 4c 49 54 45 5f 49 4f 45 52 52 20 29 3b 0a 0a 20  LITE_IOERR );.. 
e4730 20 61 7a 44 69 72 73 5b 30 5d 20 3d 20 73 71 6c   azDirs[0] = sql
e4740 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74  ite3_temp_direct
e4750 6f 72 79 3b 0a 20 20 69 66 20 28 4e 55 4c 4c 20  ory;.  if (NULL 
e4760 3d 3d 20 61 7a 44 69 72 73 5b 31 5d 29 20 7b 0a  == azDirs[1]) {.
e4770 20 20 20 20 61 7a 44 69 72 73 5b 31 5d 20 3d 20      azDirs[1] = 
e4780 67 65 74 65 6e 76 28 22 54 4d 50 44 49 52 22 29  getenv("TMPDIR")
e4790 3b 0a 20 20 7d 0a 20 20 0a 20 20 66 6f 72 28 69  ;.  }.  .  for(i
e47a0 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 7a 44  =0; i<sizeof(azD
e47b0 69 72 73 29 2f 73 69 7a 65 6f 66 28 61 7a 44 69  irs)/sizeof(azDi
e47c0 72 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20  rs[0]); i++){.  
e47d0 20 20 69 66 28 20 61 7a 44 69 72 73 5b 69 5d 3d    if( azDirs[i]=
e47e0 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =0 ) continue;. 
e47f0 20 20 20 69 66 28 20 73 74 61 74 28 61 7a 44 69     if( stat(azDi
e4800 72 73 5b 69 5d 2c 20 26 62 75 66 29 20 29 20 63  rs[i], &buf) ) c
e4810 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28  ontinue;.    if(
e4820 20 21 53 5f 49 53 44 49 52 28 62 75 66 2e 73 74   !S_ISDIR(buf.st
e4830 5f 6d 6f 64 65 29 20 29 20 63 6f 6e 74 69 6e 75  _mode) ) continu
e4840 65 3b 0a 20 20 20 20 69 66 28 20 61 63 63 65 73  e;.    if( acces
e4850 73 28 61 7a 44 69 72 73 5b 69 5d 2c 20 30 37 29  s(azDirs[i], 07)
e4860 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
e4870 20 7a 44 69 72 20 3d 20 61 7a 44 69 72 73 5b 69   zDir = azDirs[i
e4880 5d 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  ];.    break;.  
e4890 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  }..  /* Check th
e48a0 61 74 20 74 68 65 20 6f 75 74 70 75 74 20 62 75  at the output bu
e48b0 66 66 65 72 20 69 73 20 6c 61 72 67 65 20 65 6e  ffer is large en
e48c0 6f 75 67 68 20 66 6f 72 20 74 68 65 20 74 65 6d  ough for the tem
e48d0 70 6f 72 61 72 79 20 66 69 6c 65 20 0a 20 20 2a  porary file .  *
e48e0 2a 20 6e 61 6d 65 2e 20 49 66 20 69 74 20 69 73  * name. If it is
e48f0 20 6e 6f 74 2c 20 72 65 74 75 72 6e 20 53 51 4c   not, return SQL
e4900 49 54 45 5f 45 52 52 4f 52 2e 0a 20 20 2a 2f 0a  ITE_ERROR..  */.
e4910 20 20 69 66 28 20 28 73 74 72 6c 65 6e 28 7a 44    if( (strlen(zD
e4920 69 72 29 20 2b 20 73 74 72 6c 65 6e 28 53 51 4c  ir) + strlen(SQL
e4930 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52  ITE_TEMP_FILE_PR
e4940 45 46 49 58 29 20 2b 20 31 37 29 20 3e 3d 20 28  EFIX) + 17) >= (
e4950 73 69 7a 65 5f 74 29 6e 42 75 66 20 29 7b 0a 20  size_t)nBuf ){. 
e4960 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
e4970 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 64  _ERROR;.  }..  d
e4980 6f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  o{.    sqlite3_s
e4990 6e 70 72 69 6e 74 66 28 6e 42 75 66 2d 31 37 2c  nprintf(nBuf-17,
e49a0 20 7a 42 75 66 2c 20 22 25 73 2f 22 53 51 4c 49   zBuf, "%s/"SQLI
e49b0 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45  TE_TEMP_FILE_PRE
e49c0 46 49 58 2c 20 7a 44 69 72 29 3b 0a 20 20 20 20  FIX, zDir);.    
e49d0 6a 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28  j = (int)strlen(
e49e0 7a 42 75 66 29 3b 0a 20 20 20 20 73 71 6c 69 74  zBuf);.    sqlit
e49f0 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 31 35  e3_randomness(15
e4a00 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 20  , &zBuf[j]);.   
e4a10 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 35 3b 20   for(i=0; i<15; 
e4a20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  i++, j++){.     
e4a30 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72   zBuf[j] = (char
e4a40 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69 67  )zChars[ ((unsig
e4a50 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d  ned char)zBuf[j]
e4a60 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 73  )%(sizeof(zChars
e4a70 29 2d 31 29 20 5d 3b 0a 20 20 20 20 7d 0a 20 20  )-1) ];.    }.  
e4a80 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20    zBuf[j] = 0;. 
e4a90 20 7d 77 68 69 6c 65 28 20 61 63 63 65 73 73 28   }while( access(
e4aa0 7a 42 75 66 2c 30 29 3d 3d 30 20 29 3b 0a 20 20  zBuf,0)==0 );.  
e4ab0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
e4ac0 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f  ;.}..#if SQLITE_
e4ad0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
e4ae0 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28  TYLE && defined(
e4af0 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a  __APPLE__)./*.**
e4b00 20 52 6f 75 74 69 6e 65 20 74 6f 20 74 72 61 6e   Routine to tran
e4b10 73 66 6f 72 6d 20 61 20 75 6e 69 78 46 69 6c 65  sform a unixFile
e4b20 20 69 6e 74 6f 20 61 20 70 72 6f 78 79 2d 6c 6f   into a proxy-lo
e4b30 63 6b 69 6e 67 20 75 6e 69 78 46 69 6c 65 2e 0a  cking unixFile..
e4b40 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
e4b50 6e 20 69 6e 20 74 68 65 20 70 72 6f 78 79 2d 6c  n in the proxy-l
e4b60 6f 63 6b 20 64 69 76 69 73 69 6f 6e 2c 20 62 75  ock division, bu
e4b70 74 20 75 73 65 64 20 62 79 20 75 6e 69 78 4f 70  t used by unixOp
e4b80 65 6e 28 29 0a 2a 2a 20 69 66 20 53 51 4c 49 54  en().** if SQLIT
e4b90 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c  E_PREFER_PROXY_L
e4ba0 4f 43 4b 49 4e 47 20 69 73 20 64 65 66 69 6e 65  OCKING is define
e4bb0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
e4bc0 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55   proxyTransformU
e4bd0 6e 69 78 46 69 6c 65 28 75 6e 69 78 46 69 6c 65  nixFile(unixFile
e4be0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  *, const char*);
e4bf0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53  .#endif../*.** S
e4c00 65 61 72 63 68 20 66 6f 72 20 61 6e 20 75 6e 75  earch for an unu
e4c10 73 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70  sed file descrip
e4c20 74 6f 72 20 74 68 61 74 20 77 61 73 20 6f 70 65  tor that was ope
e4c30 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ned on the datab
e4c40 61 73 65 20 0a 2a 2a 20 66 69 6c 65 20 28 6e 6f  ase .** file (no
e4c50 74 20 61 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 6d  t a journal or m
e4c60 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69  aster-journal fi
e4c70 6c 65 29 20 69 64 65 6e 74 69 66 69 65 64 20 62  le) identified b
e4c80 79 20 70 61 74 68 6e 61 6d 65 0a 2a 2a 20 7a 50  y pathname.** zP
e4c90 61 74 68 20 77 69 74 68 20 53 51 4c 49 54 45 5f  ath with SQLITE_
e4ca0 4f 50 45 4e 5f 58 58 58 20 66 6c 61 67 73 20 6d  OPEN_XXX flags m
e4cb0 61 74 63 68 69 6e 67 20 74 68 6f 73 65 20 70 61  atching those pa
e4cc0 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f  ssed as the seco
e4cd0 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74  nd.** argument t
e4ce0 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  o this function.
e4cf0 0a 2a 2a 0a 2a 2a 20 53 75 63 68 20 61 20 66 69  .**.** Such a fi
e4d00 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6d 61  le descriptor ma
e4d10 79 20 65 78 69 73 74 20 69 66 20 61 20 64 61 74  y exist if a dat
e4d20 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
e4d30 20 77 61 73 20 63 6c 6f 73 65 64 0a 2a 2a 20 62   was closed.** b
e4d40 75 74 20 74 68 65 20 61 73 73 6f 63 69 61 74 65  ut the associate
e4d50 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  d file descripto
e4d60 72 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63  r could not be c
e4d70 6c 6f 73 65 64 20 62 65 63 61 75 73 65 20 73 6f  losed because so
e4d80 6d 65 0a 2a 2a 20 6f 74 68 65 72 20 66 69 6c 65  me.** other file
e4d90 20 64 65 73 63 72 69 70 74 6f 72 20 6f 70 65 6e   descriptor open
e4da0 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c   on the same fil
e4db0 65 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 66  e is holding a f
e4dc0 69 6c 65 2d 6c 6f 63 6b 2e 0a 2a 2a 20 52 65 66  ile-lock..** Ref
e4dd0 65 72 20 74 6f 20 63 6f 6d 6d 65 6e 74 73 20 69  er to comments i
e4de0 6e 20 74 68 65 20 75 6e 69 78 43 6c 6f 73 65 28  n the unixClose(
e4df0 29 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 74  ) function and t
e4e00 68 65 20 6c 65 6e 67 74 68 79 20 63 6f 6d 6d 65  he lengthy comme
e4e10 6e 74 0a 2a 2a 20 64 65 73 63 72 69 62 69 6e 67  nt.** describing
e4e20 20 22 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79   "Posix Advisory
e4e30 20 4c 6f 63 6b 69 6e 67 22 20 61 74 20 74 68 65   Locking" at the
e4e40 20 73 74 61 72 74 20 6f 66 20 74 68 69 73 20 66   start of this f
e4e50 69 6c 65 20 66 6f 72 20 0a 2a 2a 20 66 75 72 74  ile for .** furt
e4e60 68 65 72 20 64 65 74 61 69 6c 73 2e 20 41 6c 73  her details. Als
e4e70 6f 2c 20 74 69 63 6b 65 74 20 23 34 30 31 38 2e  o, ticket #4018.
e4e80 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 75 69 74  .**.** If a suit
e4e90 61 62 6c 65 20 66 69 6c 65 20 64 65 73 63 72 69  able file descri
e4ea0 70 74 6f 72 20 69 73 20 66 6f 75 6e 64 2c 20 74  ptor is found, t
e4eb0 68 65 6e 20 69 74 20 69 73 20 72 65 74 75 72 6e  hen it is return
e4ec0 65 64 2e 20 49 66 20 6e 6f 0a 2a 2a 20 73 75 63  ed. If no.** suc
e4ed0 68 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  h file descripto
e4ee0 72 20 69 73 20 6c 6f 63 61 74 65 64 2c 20 2d 31  r is located, -1
e4ef0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
e4f00 0a 73 74 61 74 69 63 20 55 6e 69 78 55 6e 75 73  .static UnixUnus
e4f10 65 64 46 64 20 2a 66 69 6e 64 52 65 75 73 61 62  edFd *findReusab
e4f20 6c 65 46 64 28 63 6f 6e 73 74 20 63 68 61 72 20  leFd(const char 
e4f30 2a 7a 50 61 74 68 2c 20 69 6e 74 20 66 6c 61 67  *zPath, int flag
e4f40 73 29 7b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64  s){.  UnixUnused
e4f50 46 64 20 2a 70 55 6e 75 73 65 64 20 3d 20 30 3b  Fd *pUnused = 0;
e4f60 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 65  ..  /* Do not se
e4f70 61 72 63 68 20 66 6f 72 20 61 6e 20 75 6e 75 73  arch for an unus
e4f80 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ed file descript
e4f90 6f 72 20 6f 6e 20 76 78 77 6f 72 6b 73 2e 20 4e  or on vxworks. N
e4fa0 6f 74 20 62 65 63 61 75 73 65 0a 20 20 2a 2a 20  ot because.  ** 
e4fb0 76 78 77 6f 72 6b 73 20 77 6f 75 6c 64 20 6e 6f  vxworks would no
e4fc0 74 20 62 65 6e 65 66 69 74 20 66 72 6f 6d 20 74  t benefit from t
e4fd0 68 65 20 63 68 61 6e 67 65 20 28 69 74 20 6d 69  he change (it mi
e4fe0 67 68 74 2c 20 77 65 27 72 65 20 6e 6f 74 20 73  ght, we're not s
e4ff0 75 72 65 29 2c 0a 20 20 2a 2a 20 62 75 74 20 62  ure),.  ** but b
e5000 65 63 61 75 73 65 20 6e 6f 20 77 61 79 20 74 6f  ecause no way to
e5010 20 74 65 73 74 20 69 74 20 69 73 20 63 75 72 72   test it is curr
e5020 65 6e 74 6c 79 20 61 76 61 69 6c 61 62 6c 65 2e  ently available.
e5030 20 49 74 20 69 73 20 62 65 74 74 65 72 20 0a 20   It is better . 
e5040 20 2a 2a 20 6e 6f 74 20 74 6f 20 72 69 73 6b 20   ** not to risk 
e5050 62 72 65 61 6b 69 6e 67 20 76 78 77 6f 72 6b 73  breaking vxworks
e5060 20 73 75 70 70 6f 72 74 20 66 6f 72 20 74 68 65   support for the
e5070 20 73 61 6b 65 20 6f 66 20 73 75 63 68 20 61 6e   sake of such an
e5080 20 6f 62 73 63 75 72 65 20 0a 20 20 2a 2a 20 66   obscure .  ** f
e5090 65 61 74 75 72 65 2e 20 20 2a 2f 0a 23 69 66 20  eature.  */.#if 
e50a0 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74  !OS_VXWORKS.  st
e50b0 72 75 63 74 20 73 74 61 74 20 73 53 74 61 74 3b  ruct stat sStat;
e50c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e50d0 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66     /* Results of
e50e0 20 73 74 61 74 28 29 20 63 61 6c 6c 20 2a 2f 0a   stat() call */.
e50f0 0a 20 20 2f 2a 20 41 20 73 74 61 74 28 29 20 63  .  /* A stat() c
e5100 61 6c 6c 20 6d 61 79 20 66 61 69 6c 20 66 6f 72  all may fail for
e5110 20 76 61 72 69 6f 75 73 20 72 65 61 73 6f 6e 73   various reasons
e5120 2e 20 49 66 20 74 68 69 73 20 68 61 70 70 65 6e  . If this happen
e5130 73 2c 20 69 74 20 69 73 0a 20 20 2a 2a 20 61 6c  s, it is.  ** al
e5140 6d 6f 73 74 20 63 65 72 74 61 69 6e 20 74 68 61  most certain tha
e5150 74 20 61 6e 20 6f 70 65 6e 28 29 20 63 61 6c 6c  t an open() call
e5160 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 70 61 74   on the same pat
e5170 68 20 77 69 6c 6c 20 61 6c 73 6f 20 66 61 69 6c  h will also fail
e5180 2e 0a 20 20 2a 2a 20 46 6f 72 20 74 68 69 73 20  ..  ** For this 
e5190 72 65 61 73 6f 6e 2c 20 69 66 20 61 6e 20 65 72  reason, if an er
e51a0 72 6f 72 20 6f 63 63 75 72 73 20 69 6e 20 74 68  ror occurs in th
e51b0 65 20 73 74 61 74 28 29 20 63 61 6c 6c 20 68 65  e stat() call he
e51c0 72 65 2c 20 69 74 20 69 73 0a 20 20 2a 2a 20 69  re, it is.  ** i
e51d0 67 6e 6f 72 65 64 20 61 6e 64 20 2d 31 20 69 73  gnored and -1 is
e51e0 20 72 65 74 75 72 6e 65 64 2e 20 54 68 65 20 63   returned. The c
e51f0 61 6c 6c 65 72 20 77 69 6c 6c 20 74 72 79 20 74  aller will try t
e5200 6f 20 6f 70 65 6e 20 61 20 6e 65 77 20 66 69 6c  o open a new fil
e5210 65 0a 20 20 2a 2a 20 64 65 73 63 72 69 70 74 6f  e.  ** descripto
e5220 72 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 70 61  r on the same pa
e5230 74 68 2c 20 66 61 69 6c 2c 20 61 6e 64 20 72 65  th, fail, and re
e5240 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 74 6f  turn an error to
e5250 20 53 51 4c 69 74 65 2e 0a 20 20 2a 2a 0a 20 20   SQLite..  **.  
e5260 2a 2a 20 45 76 65 6e 20 69 66 20 61 20 73 75 62  ** Even if a sub
e5270 73 65 71 75 65 6e 74 20 6f 70 65 6e 28 29 20 63  sequent open() c
e5280 61 6c 6c 20 64 6f 65 73 20 73 75 63 63 65 65 64  all does succeed
e5290 2c 20 74 68 65 20 63 6f 6e 73 65 71 75 65 6e 63  , the consequenc
e52a0 65 73 20 6f 66 0a 20 20 2a 2a 20 6e 6f 74 20 73  es of.  ** not s
e52b0 65 61 72 63 68 69 6e 67 20 66 6f 72 20 61 20 72  earching for a r
e52c0 65 73 75 73 61 62 6c 65 20 66 69 6c 65 20 64 65  esusable file de
e52d0 73 63 72 69 70 74 6f 72 20 61 72 65 20 6e 6f 74  scriptor are not
e52e0 20 64 69 72 65 2e 20 20 2a 2f 0a 20 20 69 66 28   dire.  */.  if(
e52f0 20 30 3d 3d 73 74 61 74 28 7a 50 61 74 68 2c 20   0==stat(zPath, 
e5300 26 73 53 74 61 74 29 20 29 7b 0a 20 20 20 20 73  &sStat) ){.    s
e5310 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e  truct unixOpenCn
e5320 74 20 2a 70 4f 3b 0a 20 20 20 20 73 74 72 75 63  t *pO;.    struc
e5330 74 20 75 6e 69 78 46 69 6c 65 49 64 20 69 64 3b  t unixFileId id;
e5340 0a 20 20 20 20 69 64 2e 64 65 76 20 3d 20 73 53  .    id.dev = sS
e5350 74 61 74 2e 73 74 5f 64 65 76 3b 0a 20 20 20 20  tat.st_dev;.    
e5360 69 64 2e 69 6e 6f 20 3d 20 73 53 74 61 74 2e 73  id.ino = sStat.s
e5370 74 5f 69 6e 6f 3b 0a 0a 20 20 20 20 75 6e 69 78  t_ino;..    unix
e5380 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20  EnterMutex();.  
e5390 20 20 66 6f 72 28 70 4f 3d 6f 70 65 6e 4c 69 73    for(pO=openLis
e53a0 74 3b 20 70 4f 20 26 26 20 6d 65 6d 63 6d 70 28  t; pO && memcmp(
e53b0 26 69 64 2c 20 26 70 4f 2d 3e 66 69 6c 65 49 64  &id, &pO->fileId
e53c0 2c 20 73 69 7a 65 6f 66 28 69 64 29 29 3b 20 70  , sizeof(id)); p
e53d0 4f 3d 70 4f 2d 3e 70 4e 65 78 74 29 3b 0a 20 20  O=pO->pNext);.  
e53e0 20 20 69 66 28 20 70 4f 20 29 7b 0a 20 20 20 20    if( pO ){.    
e53f0 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a    UnixUnusedFd *
e5400 2a 70 70 3b 0a 20 20 20 20 20 20 66 6f 72 28 70  *pp;.      for(p
e5410 70 3d 26 70 4f 2d 3e 70 55 6e 75 73 65 64 3b 20  p=&pO->pUnused; 
e5420 2a 70 70 20 26 26 20 28 2a 70 70 29 2d 3e 66 6c  *pp && (*pp)->fl
e5430 61 67 73 21 3d 66 6c 61 67 73 3b 20 70 70 3d 26  ags!=flags; pp=&
e5440 28 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29 3b  ((*pp)->pNext));
e5450 0a 20 20 20 20 20 20 70 55 6e 75 73 65 64 20 3d  .      pUnused =
e5460 20 2a 70 70 3b 0a 20 20 20 20 20 20 69 66 28 20   *pp;.      if( 
e5470 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20 20  pUnused ){.     
e5480 20 20 20 2a 70 70 20 3d 20 70 55 6e 75 73 65 64     *pp = pUnused
e5490 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d  ->pNext;.      }
e54a0 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c  .    }.    unixL
e54b0 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d  eaveMutex();.  }
e54c0 0a 23 65 6e 64 69 66 20 20 20 20 2f 2a 20 69 66  .#endif    /* if
e54d0 20 21 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a   !OS_VXWORKS */.
e54e0 20 20 72 65 74 75 72 6e 20 70 55 6e 75 73 65 64    return pUnused
e54f0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20  ;.}../*.** Open 
e5500 74 68 65 20 66 69 6c 65 20 7a 50 61 74 68 2e 0a  the file zPath..
e5510 2a 2a 20 0a 2a 2a 20 50 72 65 76 69 6f 75 73 6c  ** .** Previousl
e5520 79 2c 20 74 68 65 20 53 51 4c 69 74 65 20 4f 53  y, the SQLite OS
e5530 20 6c 61 79 65 72 20 75 73 65 64 20 74 68 72 65   layer used thre
e5540 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 70  e functions in p
e5550 6c 61 63 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20  lace of this.** 
e5560 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73  one:.**.**     s
e5570 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64  qlite3OsOpenRead
e5580 57 72 69 74 65 28 29 3b 0a 2a 2a 20 20 20 20 20  Write();.**     
e5590 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61  sqlite3OsOpenRea
e55a0 64 4f 6e 6c 79 28 29 3b 0a 2a 2a 20 20 20 20 20  dOnly();.**     
e55b0 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 45 78 63  sqlite3OsOpenExc
e55c0 6c 75 73 69 76 65 28 29 3b 0a 2a 2a 0a 2a 2a 20  lusive();.**.** 
e55d0 54 68 65 73 65 20 63 61 6c 6c 73 20 63 6f 72 72  These calls corr
e55e0 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 20 66 6f  espond to the fo
e55f0 6c 6c 6f 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74  llowing combinat
e5600 69 6f 6e 73 20 6f 66 20 66 6c 61 67 73 3a 0a 2a  ions of flags:.*
e5610 2a 0a 2a 2a 20 20 20 20 20 52 65 61 64 57 72 69  *.**     ReadWri
e5620 74 65 28 29 20 2d 3e 20 20 20 20 20 28 52 45 41  te() ->     (REA
e5630 44 57 52 49 54 45 20 7c 20 43 52 45 41 54 45 29  DWRITE | CREATE)
e5640 0a 2a 2a 20 20 20 20 20 52 65 61 64 4f 6e 6c 79  .**     ReadOnly
e5650 28 29 20 20 2d 3e 20 20 20 20 20 28 52 45 41 44  ()  ->     (READ
e5660 4f 4e 4c 59 29 20 0a 2a 2a 20 20 20 20 20 4f 70  ONLY) .**     Op
e5670 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20 2d 3e  enExclusive() ->
e5680 20 28 52 45 41 44 57 52 49 54 45 20 7c 20 43 52   (READWRITE | CR
e5690 45 41 54 45 20 7c 20 45 58 43 4c 55 53 49 56 45  EATE | EXCLUSIVE
e56a0 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6c 64 20  ).**.** The old 
e56b0 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20  OpenExclusive() 
e56c0 61 63 63 65 70 74 65 64 20 61 20 62 6f 6f 6c 65  accepted a boole
e56d0 61 6e 20 61 72 67 75 6d 65 6e 74 20 2d 20 22 64  an argument - "d
e56e0 65 6c 46 6c 61 67 22 2e 20 49 66 0a 2a 2a 20 74  elFlag". If.** t
e56f0 72 75 65 2c 20 74 68 65 20 66 69 6c 65 20 77 61  rue, the file wa
e5700 73 20 63 6f 6e 66 69 67 75 72 65 64 20 74 6f 20  s configured to 
e5710 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  be automatically
e5720 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68   deleted when th
e5730 65 0a 2a 2a 20 66 69 6c 65 20 68 61 6e 64 6c 65  e.** file handle
e5740 20 63 6c 6f 73 65 64 2e 20 54 6f 20 61 63 68 69   closed. To achi
e5750 65 76 65 20 74 68 65 20 73 61 6d 65 20 65 66 66  eve the same eff
e5760 65 63 74 20 75 73 69 6e 67 20 74 68 69 73 20 6e  ect using this n
e5770 65 77 20 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  ew .** interface
e5780 2c 20 61 64 64 20 74 68 65 20 44 45 4c 45 54 45  , add the DELETE
e5790 4f 4e 43 4c 4f 53 45 20 66 6c 61 67 20 74 6f 20  ONCLOSE flag to 
e57a0 74 68 6f 73 65 20 73 70 65 63 69 66 69 65 64 20  those specified 
e57b0 61 62 6f 76 65 20 66 6f 72 20 0a 2a 2a 20 4f 70  above for .** Op
e57c0 65 6e 45 78 63 6c 75 73 69 76 65 28 29 2e 0a 2a  enExclusive()..*
e57d0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
e57e0 78 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33  xOpen(.  sqlite3
e57f0 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
e5800 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53        /* The VFS
e5810 20 66 6f 72 20 77 68 69 63 68 20 74 68 69 73 20   for which this 
e5820 69 73 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74  is the xOpen met
e5830 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  hod */.  const c
e5840 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20  har *zPath,     
e5850 20 20 20 20 20 20 2f 2a 20 50 61 74 68 6e 61 6d        /* Pathnam
e5860 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 65 20  e of file to be 
e5870 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 73 71 6c 69  opened */.  sqli
e5880 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c  te3_file *pFile,
e5890 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
e58a0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
e58b0 74 6f 20 62 65 20 66 69 6c 6c 65 64 20 69 6e 20  to be filled in 
e58c0 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20  */.  int flags, 
e58d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e58e0 20 20 2f 2a 20 49 6e 70 75 74 20 66 6c 61 67 73    /* Input flags
e58f0 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20   to control the 
e5900 6f 70 65 6e 69 6e 67 20 2a 2f 0a 20 20 69 6e 74  opening */.  int
e5910 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 20 20   *pOutFlags     
e5920 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74            /* Out
e5930 70 75 74 20 66 6c 61 67 73 20 72 65 74 75 72 6e  put flags return
e5940 65 64 20 74 6f 20 53 51 4c 69 74 65 20 63 6f 72  ed to SQLite cor
e5950 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 69 78 46 69  e */.){.  unixFi
e5960 6c 65 20 2a 70 20 3d 20 28 75 6e 69 78 46 69 6c  le *p = (unixFil
e5970 65 20 2a 29 70 46 69 6c 65 3b 0a 20 20 69 6e 74  e *)pFile;.  int
e5980 20 66 64 20 3d 20 2d 31 3b 20 20 20 20 20 20 20   fd = -1;       
e5990 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
e59a0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 72  ile descriptor r
e59b0 65 74 75 72 6e 65 64 20 62 79 20 6f 70 65 6e 28  eturned by open(
e59c0 29 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 66 64  ) */.  int dirfd
e59d0 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 20   = -1;          
e59e0 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63 74 6f        /* Directo
e59f0 72 79 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ry file descript
e5a00 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e  or */.  int open
e5a10 46 6c 61 67 73 20 3d 20 30 3b 20 20 20 20 20 20  Flags = 0;      
e5a20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
e5a30 74 6f 20 70 61 73 73 20 74 6f 20 6f 70 65 6e 28  to pass to open(
e5a40 29 20 2a 2f 0a 20 20 69 6e 74 20 65 54 79 70 65  ) */.  int eType
e5a50 20 3d 20 66 6c 61 67 73 26 30 78 46 46 46 46 46   = flags&0xFFFFF
e5a60 46 30 30 3b 20 20 2f 2a 20 54 79 70 65 20 6f 66  F00;  /* Type of
e5a70 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f   file to open */
e5a80 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b 3b 20 20  .  int noLock;  
e5a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e5aa0 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69    /* True to omi
e5ab0 74 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74  t locking primit
e5ac0 69 76 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63  ives */.  int rc
e5ad0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
e5ae0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 75 6e 63           /* Func
e5af0 74 69 6f 6e 20 52 65 74 75 72 6e 20 43 6f 64 65  tion Return Code
e5b00 20 2a 2f 0a 0a 20 20 69 6e 74 20 69 73 45 78 63   */..  int isExc
e5b10 6c 75 73 69 76 65 20 20 3d 20 28 66 6c 61 67 73  lusive  = (flags
e5b20 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45   & SQLITE_OPEN_E
e5b30 58 43 4c 55 53 49 56 45 29 3b 0a 20 20 69 6e 74  XCLUSIVE);.  int
e5b40 20 69 73 44 65 6c 65 74 65 20 20 20 20 20 3d 20   isDelete     = 
e5b50 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  (flags & SQLITE_
e5b60 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f  OPEN_DELETEONCLO
e5b70 53 45 29 3b 0a 20 20 69 6e 74 20 69 73 43 72 65  SE);.  int isCre
e5b80 61 74 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73  ate     = (flags
e5b90 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43   & SQLITE_OPEN_C
e5ba0 52 45 41 54 45 29 3b 0a 20 20 69 6e 74 20 69 73  REATE);.  int is
e5bb0 52 65 61 64 6f 6e 6c 79 20 20 20 3d 20 28 66 6c  Readonly   = (fl
e5bc0 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
e5bd0 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 20 20 69  N_READONLY);.  i
e5be0 6e 74 20 69 73 52 65 61 64 57 72 69 74 65 20 20  nt isReadWrite  
e5bf0 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54  = (flags & SQLIT
e5c00 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
e5c10 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 63 72 65 61  );..  /* If crea
e5c20 74 69 6e 67 20 61 20 6d 61 73 74 65 72 20 6f 72  ting a master or
e5c30 20 6d 61 69 6e 2d 66 69 6c 65 20 6a 6f 75 72 6e   main-file journ
e5c40 61 6c 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  al, this functio
e5c50 6e 20 77 69 6c 6c 20 6f 70 65 6e 0a 20 20 2a 2a  n will open.  **
e5c60 20 61 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74   a file-descript
e5c70 6f 72 20 6f 6e 20 74 68 65 20 64 69 72 65 63 74  or on the direct
e5c80 6f 72 79 20 74 6f 6f 2e 20 54 68 65 20 66 69 72  ory too. The fir
e5c90 73 74 20 74 69 6d 65 20 75 6e 69 78 53 79 6e 63  st time unixSync
e5ca0 28 29 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65  ().  ** is calle
e5cb0 64 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  d the directory 
e5cc0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
e5cd0 77 69 6c 6c 20 62 65 20 66 73 79 6e 63 28 29 65  will be fsync()e
e5ce0 64 20 61 6e 64 20 63 6c 6f 73 65 28 29 64 2e 0a  d and close()d..
e5cf0 20 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4f 70 65    */.  int isOpe
e5d00 6e 44 69 72 65 63 74 6f 72 79 20 3d 20 28 69 73  nDirectory = (is
e5d10 43 72 65 61 74 65 20 26 26 20 0a 20 20 20 20 20  Create && .     
e5d20 20 28 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f   (eType==SQLITE_
e5d30 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52  OPEN_MASTER_JOUR
e5d40 4e 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51  NAL || eType==SQ
e5d50 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a  LITE_OPEN_MAIN_J
e5d60 4f 55 52 4e 41 4c 29 0a 20 20 29 3b 0a 0a 20 20  OURNAL).  );..  
e5d70 2f 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 7a  /* If argument z
e5d80 50 61 74 68 20 69 73 20 61 20 4e 55 4c 4c 20 70  Path is a NULL p
e5d90 6f 69 6e 74 65 72 2c 20 74 68 69 73 20 66 75 6e  ointer, this fun
e5da0 63 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65  ction is require
e5db0 64 20 74 6f 20 6f 70 65 6e 0a 20 20 2a 2a 20 61  d to open.  ** a
e5dc0 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e   temporary file.
e5dd0 20 55 73 65 20 74 68 69 73 20 62 75 66 66 65 72   Use this buffer
e5de0 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 66 69   to store the fi
e5df0 6c 65 20 6e 61 6d 65 20 69 6e 2e 0a 20 20 2a 2f  le name in..  */
e5e00 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65  .  char zTmpname
e5e10 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d  [MAX_PATHNAME+1]
e5e20 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
e5e30 7a 4e 61 6d 65 20 3d 20 7a 50 61 74 68 3b 0a 0a  zName = zPath;..
e5e40 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 66    /* Check the f
e5e50 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65  ollowing stateme
e5e60 6e 74 73 20 61 72 65 20 74 72 75 65 3a 20 0a 20  nts are true: . 
e5e70 20 2a 2a 0a 20 20 2a 2a 20 20 20 28 61 29 20 45   **.  **   (a) E
e5e80 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68  xactly one of th
e5e90 65 20 52 45 41 44 57 52 49 54 45 20 61 6e 64 20  e READWRITE and 
e5ea0 52 45 41 44 4f 4e 4c 59 20 66 6c 61 67 73 20 6d  READONLY flags m
e5eb0 75 73 74 20 62 65 20 73 65 74 2c 20 61 6e 64 20  ust be set, and 
e5ec0 0a 20 20 2a 2a 20 20 20 28 62 29 20 69 66 20 43  .  **   (b) if C
e5ed0 52 45 41 54 45 20 69 73 20 73 65 74 2c 20 74 68  REATE is set, th
e5ee0 65 6e 20 52 45 41 44 57 52 49 54 45 20 6d 75 73  en READWRITE mus
e5ef0 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2c 20 61  t also be set, a
e5f00 6e 64 0a 20 20 2a 2a 20 20 20 28 63 29 20 69 66  nd.  **   (c) if
e5f10 20 45 58 43 4c 55 53 49 56 45 20 69 73 20 73 65   EXCLUSIVE is se
e5f20 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20 6d  t, then CREATE m
e5f30 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2e  ust also be set.
e5f40 0a 20 20 2a 2a 20 20 20 28 64 29 20 69 66 20 44  .  **   (d) if D
e5f50 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 69 73 20  ELETEONCLOSE is 
e5f60 73 65 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45  set, then CREATE
e5f70 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65   must also be se
e5f80 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  t..  */.  assert
e5f90 28 28 69 73 52 65 61 64 6f 6e 6c 79 3d 3d 30 20  ((isReadonly==0 
e5fa0 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65 3d 3d  || isReadWrite==
e5fb0 30 29 20 26 26 20 28 69 73 52 65 61 64 57 72 69  0) && (isReadWri
e5fc0 74 65 20 7c 7c 20 69 73 52 65 61 64 6f 6e 6c 79  te || isReadonly
e5fd0 29 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 43  ));.  assert(isC
e5fe0 72 65 61 74 65 3d 3d 30 20 7c 7c 20 69 73 52 65  reate==0 || isRe
e5ff0 61 64 57 72 69 74 65 29 3b 0a 20 20 61 73 73 65  adWrite);.  asse
e6000 72 74 28 69 73 45 78 63 6c 75 73 69 76 65 3d 3d  rt(isExclusive==
e6010 30 20 7c 7c 20 69 73 43 72 65 61 74 65 29 3b 0a  0 || isCreate);.
e6020 20 20 61 73 73 65 72 74 28 69 73 44 65 6c 65 74    assert(isDelet
e6030 65 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61 74 65  e==0 || isCreate
e6040 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 61 69  );..  /* The mai
e6050 6e 20 44 42 2c 20 6d 61 69 6e 20 6a 6f 75 72 6e  n DB, main journ
e6060 61 6c 2c 20 61 6e 64 20 6d 61 73 74 65 72 20 6a  al, and master j
e6070 6f 75 72 6e 61 6c 20 61 72 65 20 6e 65 76 65 72  ournal are never
e6080 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 20   automatically. 
e6090 20 2a 2a 20 64 65 6c 65 74 65 64 2e 20 4e 6f 72   ** deleted. Nor
e60a0 20 61 72 65 20 74 68 65 79 20 65 76 65 72 20 74   are they ever t
e60b0 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 20  emporary files. 
e60c0 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 21   */.  assert( (!
e60d0 69 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d  isDelete && zNam
e60e0 65 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c  e) || eType!=SQL
e60f0 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42  ITE_OPEN_MAIN_DB
e6100 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 21   );.  assert( (!
e6110 69 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d  isDelete && zNam
e6120 65 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c  e) || eType!=SQL
e6130 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f  ITE_OPEN_MAIN_JO
e6140 55 52 4e 41 4c 20 29 3b 0a 20 20 61 73 73 65 72  URNAL );.  asser
e6150 74 28 20 28 21 69 73 44 65 6c 65 74 65 20 26 26  t( (!isDelete &&
e6160 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70 65   zName) || eType
e6170 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  !=SQLITE_OPEN_MA
e6180 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 29 3b 0a  STER_JOURNAL );.
e6190 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61  .  /* Assert tha
e61a0 74 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65  t the upper laye
e61b0 72 20 68 61 73 20 73 65 74 20 6f 6e 65 20 6f 66  r has set one of
e61c0 20 74 68 65 20 22 66 69 6c 65 2d 74 79 70 65 22   the "file-type"
e61d0 20 66 6c 61 67 73 2e 20 2a 2f 0a 20 20 61 73 73   flags. */.  ass
e61e0 65 72 74 28 20 65 54 79 70 65 3d 3d 53 51 4c 49  ert( eType==SQLI
e61f0 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20  TE_OPEN_MAIN_DB 
e6200 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53       || eType==S
e6210 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f  QLITE_OPEN_TEMP_
e6220 44 42 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54  DB .       || eT
e6230 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype==SQLITE_OPEN
e6240 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c  _MAIN_JOURNAL ||
e6250 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f   eType==SQLITE_O
e6260 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c  PEN_TEMP_JOURNAL
e6270 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70   .       || eTyp
e6280 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53  e==SQLITE_OPEN_S
e6290 55 42 4a 4f 55 52 4e 41 4c 20 20 20 7c 7c 20 65  UBJOURNAL   || e
e62a0 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45  Type==SQLITE_OPE
e62b0 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c  N_MASTER_JOURNAL
e62c0 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70   .       || eTyp
e62d0 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54  e==SQLITE_OPEN_T
e62e0 52 41 4e 53 49 45 4e 54 5f 44 42 0a 20 20 29 3b  RANSIENT_DB.  );
e62f0 0a 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c  ..  memset(p, 0,
e6300 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65   sizeof(unixFile
e6310 29 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 70 65  ));..  if( eType
e6320 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  ==SQLITE_OPEN_MA
e6330 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 55 6e 69  IN_DB ){.    Uni
e6340 78 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 73  xUnusedFd *pUnus
e6350 65 64 3b 0a 20 20 20 20 70 55 6e 75 73 65 64 20  ed;.    pUnused 
e6360 3d 20 66 69 6e 64 52 65 75 73 61 62 6c 65 46 64  = findReusableFd
e6370 28 7a 4e 61 6d 65 2c 20 66 6c 61 67 73 29 3b 0a  (zName, flags);.
e6380 20 20 20 20 69 66 28 20 70 55 6e 75 73 65 64 20      if( pUnused 
e6390 29 7b 0a 20 20 20 20 20 20 66 64 20 3d 20 70 55  ){.      fd = pU
e63a0 6e 75 73 65 64 2d 3e 66 64 3b 0a 20 20 20 20 7d  nused->fd;.    }
e63b0 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 55 6e 75  else{.      pUnu
e63c0 73 65 64 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  sed = sqlite3_ma
e63d0 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 70 55 6e  lloc(sizeof(*pUn
e63e0 75 73 65 64 29 29 3b 0a 20 20 20 20 20 20 69 66  used));.      if
e63f0 28 20 21 70 55 6e 75 73 65 64 20 29 7b 0a 20 20  ( !pUnused ){.  
e6400 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
e6410 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
e6420 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e   }.    }.    p->
e6430 70 55 6e 75 73 65 64 20 3d 20 70 55 6e 75 73 65  pUnused = pUnuse
e6440 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21  d;.  }else if( !
e6450 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 2f 2a 20  zName ){.    /* 
e6460 49 66 20 7a 4e 61 6d 65 20 69 73 20 4e 55 4c 4c  If zName is NULL
e6470 2c 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65  , the upper laye
e6480 72 20 69 73 20 72 65 71 75 65 73 74 69 6e 67 20  r is requesting 
e6490 61 20 74 65 6d 70 20 66 69 6c 65 2e 20 2a 2f 0a  a temp file. */.
e64a0 20 20 20 20 61 73 73 65 72 74 28 69 73 44 65 6c      assert(isDel
e64b0 65 74 65 20 26 26 20 21 69 73 4f 70 65 6e 44 69  ete && !isOpenDi
e64c0 72 65 63 74 6f 72 79 29 3b 0a 20 20 20 20 72 63  rectory);.    rc
e64d0 20 3d 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 4d   = getTempname(M
e64e0 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 2c 20 7a  AX_PATHNAME+1, z
e64f0 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66  Tmpname);.    if
e6500 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
e6510 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
e6520 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 4e  rc;.    }.    zN
e6530 61 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a  ame = zTmpname;.
e6540 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 65 72 6d    }..  /* Determ
e6550 69 6e 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66  ine the value of
e6560 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d   the flags param
e6570 65 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 50  eter passed to P
e6580 4f 53 49 58 20 66 75 6e 63 74 69 6f 6e 0a 20 20  OSIX function.  
e6590 2a 2a 20 6f 70 65 6e 28 29 2e 20 54 68 65 73 65  ** open(). These
e65a0 20 6d 75 73 74 20 62 65 20 63 61 6c 63 75 6c 61   must be calcula
e65b0 74 65 64 20 65 76 65 6e 20 69 66 20 6f 70 65 6e  ted even if open
e65c0 28 29 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65 64  () is not called
e65d0 2c 20 61 73 0a 20 20 2a 2a 20 74 68 65 79 20 6d  , as.  ** they m
e65e0 61 79 20 62 65 20 73 74 6f 72 65 64 20 61 73 20  ay be stored as 
e65f0 70 61 72 74 20 6f 66 20 74 68 65 20 66 69 6c 65  part of the file
e6600 20 68 61 6e 64 6c 65 20 61 6e 64 20 75 73 65 64   handle and used
e6610 20 62 79 20 74 68 65 20 0a 20 20 2a 2a 20 27 63   by the .  ** 'c
e6620 6f 6e 63 68 20 66 69 6c 65 27 20 6c 6f 63 6b 69  onch file' locki
e6630 6e 67 20 66 75 6e 63 74 69 6f 6e 73 20 6c 61 74  ng functions lat
e6640 65 72 20 6f 6e 2e 20 20 2a 2f 0a 20 20 69 66 28  er on.  */.  if(
e6650 20 69 73 52 65 61 64 6f 6e 6c 79 20 29 20 20 6f   isReadonly )  o
e6660 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44  penFlags |= O_RD
e6670 4f 4e 4c 59 3b 0a 20 20 69 66 28 20 69 73 52 65  ONLY;.  if( isRe
e6680 61 64 57 72 69 74 65 20 29 20 6f 70 65 6e 46 6c  adWrite ) openFl
e6690 61 67 73 20 7c 3d 20 4f 5f 52 44 57 52 3b 0a 20  ags |= O_RDWR;. 
e66a0 20 69 66 28 20 69 73 43 72 65 61 74 65 20 29 20   if( isCreate ) 
e66b0 20 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20     openFlags |= 
e66c0 4f 5f 43 52 45 41 54 3b 0a 20 20 69 66 28 20 69  O_CREAT;.  if( i
e66d0 73 45 78 63 6c 75 73 69 76 65 20 29 20 6f 70 65  sExclusive ) ope
e66e0 6e 46 6c 61 67 73 20 7c 3d 20 28 4f 5f 45 58 43  nFlags |= (O_EXC
e66f0 4c 7c 4f 5f 4e 4f 46 4f 4c 4c 4f 57 29 3b 0a 20  L|O_NOFOLLOW);. 
e6700 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 28 4f   openFlags |= (O
e6710 5f 4c 41 52 47 45 46 49 4c 45 7c 4f 5f 42 49 4e  _LARGEFILE|O_BIN
e6720 41 52 59 29 3b 0a 0a 20 20 69 66 28 20 66 64 3c  ARY);..  if( fd<
e6730 30 20 29 7b 0a 20 20 20 20 6d 6f 64 65 5f 74 20  0 ){.    mode_t 
e6740 6f 70 65 6e 4d 6f 64 65 20 3d 20 28 69 73 44 65  openMode = (isDe
e6750 6c 65 74 65 3f 30 36 30 30 3a 53 51 4c 49 54 45  lete?0600:SQLITE
e6760 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45  _DEFAULT_FILE_PE
e6770 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 20 20  RMISSIONS);.    
e6780 66 64 20 3d 20 6f 70 65 6e 28 7a 4e 61 6d 65 2c  fd = open(zName,
e6790 20 6f 70 65 6e 46 6c 61 67 73 2c 20 6f 70 65 6e   openFlags, open
e67a0 4d 6f 64 65 29 3b 0a 20 20 20 20 4f 53 54 52 41  Mode);.    OSTRA
e67b0 43 45 34 28 22 4f 50 45 4e 58 20 20 20 25 2d 33  CE4("OPENX   %-3
e67c0 64 20 25 73 20 30 25 6f 5c 6e 22 2c 20 66 64 2c  d %s 0%o\n", fd,
e67d0 20 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67   zName, openFlag
e67e0 73 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3c 30  s);.    if( fd<0
e67f0 20 26 26 20 65 72 72 6e 6f 21 3d 45 49 53 44 49   && errno!=EISDI
e6800 52 20 26 26 20 69 73 52 65 61 64 57 72 69 74 65  R && isReadWrite
e6810 20 26 26 20 21 69 73 45 78 63 6c 75 73 69 76 65   && !isExclusive
e6820 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 46 61 69   ){.      /* Fai
e6830 6c 65 64 20 74 6f 20 6f 70 65 6e 20 74 68 65 20  led to open the 
e6840 66 69 6c 65 20 66 6f 72 20 72 65 61 64 2f 77 72  file for read/wr
e6850 69 74 65 20 61 63 63 65 73 73 2e 20 54 72 79 20  ite access. Try 
e6860 72 65 61 64 2d 6f 6e 6c 79 2e 20 2a 2f 0a 20 20  read-only. */.  
e6870 20 20 20 20 66 6c 61 67 73 20 26 3d 20 7e 28 53      flags &= ~(S
e6880 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
e6890 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e  RITE|SQLITE_OPEN
e68a0 5f 43 52 45 41 54 45 29 3b 0a 20 20 20 20 20 20  _CREATE);.      
e68b0 6f 70 65 6e 46 6c 61 67 73 20 26 3d 20 7e 28 4f  openFlags &= ~(O
e68c0 5f 52 44 57 52 7c 4f 5f 43 52 45 41 54 29 3b 0a  _RDWR|O_CREAT);.
e68d0 20 20 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 53        flags |= S
e68e0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
e68f0 4e 4c 59 3b 0a 20 20 20 20 20 20 6f 70 65 6e 46  NLY;.      openF
e6900 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 4f 4e 4c 59  lags |= O_RDONLY
e6910 3b 0a 20 20 20 20 20 20 66 64 20 3d 20 6f 70 65  ;.      fd = ope
e6920 6e 28 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61  n(zName, openFla
e6930 67 73 2c 20 6f 70 65 6e 4d 6f 64 65 29 3b 0a 20  gs, openMode);. 
e6940 20 20 20 7d 0a 20 20 20 20 69 66 28 20 66 64 3c     }.    if( fd<
e6950 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
e6960 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b  SQLITE_CANTOPEN;
e6970 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e  .      goto open
e6980 5f 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 20 7d  _finished;.    }
e6990 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 66  .  }.  assert( f
e69a0 64 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f  d>=0 );.  if( pO
e69b0 75 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 2a  utFlags ){.    *
e69c0 70 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c 61 67  pOutFlags = flag
e69d0 73 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d  s;.  }..  if( p-
e69e0 3e 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20  >pUnused ){.    
e69f0 70 2d 3e 70 55 6e 75 73 65 64 2d 3e 66 64 20 3d  p->pUnused->fd =
e6a00 20 66 64 3b 0a 20 20 20 20 70 2d 3e 70 55 6e 75   fd;.    p->pUnu
e6a10 73 65 64 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61  sed->flags = fla
e6a20 67 73 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69  gs;.  }..  if( i
e6a30 73 44 65 6c 65 74 65 20 29 7b 0a 23 69 66 20 4f  sDelete ){.#if O
e6a40 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 7a 50  S_VXWORKS.    zP
e6a50 61 74 68 20 3d 20 7a 4e 61 6d 65 3b 0a 23 65 6c  ath = zName;.#el
e6a60 73 65 0a 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 4e  se.    unlink(zN
e6a70 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  ame);.#endif.  }
e6a80 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  .#if SQLITE_ENAB
e6a90 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
e6aa0 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 70 2d 3e  .  else{.    p->
e6ab0 6f 70 65 6e 46 6c 61 67 73 20 3d 20 6f 70 65 6e  openFlags = open
e6ac0 46 6c 61 67 73 3b 0a 20 20 7d 0a 23 65 6e 64 69  Flags;.  }.#endi
e6ad0 66 0a 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 44  f..  if( isOpenD
e6ae0 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20  irectory ){.    
e6af0 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 74 6f  rc = openDirecto
e6b00 72 79 28 7a 50 61 74 68 2c 20 26 64 69 72 66 64  ry(zPath, &dirfd
e6b10 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
e6b20 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
e6b30 20 20 2f 2a 20 49 74 20 69 73 20 73 61 66 65 20    /* It is safe 
e6b40 74 6f 20 63 6c 6f 73 65 20 66 64 20 61 74 20 74  to close fd at t
e6b50 68 69 73 20 70 6f 69 6e 74 2c 20 62 65 63 61 75  his point, becau
e6b60 73 65 20 69 74 20 69 73 20 67 75 61 72 61 6e 74  se it is guarant
e6b70 65 65 64 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a  eed not.      **
e6b80 20 74 6f 20 62 65 20 6f 70 65 6e 20 6f 6e 20 61   to be open on a
e6b90 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
e6ba0 49 66 20 69 74 20 77 65 72 65 20 6f 70 65 6e 20  If it were open 
e6bb0 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69  on a database fi
e6bc0 6c 65 2c 0a 20 20 20 20 20 20 2a 2a 20 69 74 20  le,.      ** it 
e6bd0 77 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 61 66  would not be saf
e6be0 65 20 74 6f 20 63 6c 6f 73 65 20 61 73 20 74 68  e to close as th
e6bf0 69 73 20 77 6f 75 6c 64 20 72 65 6c 65 61 73 65  is would release
e6c00 20 61 6e 79 20 6c 6f 63 6b 73 20 68 65 6c 64 0a   any locks held.
e6c10 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20        ** on the 
e6c20 66 69 6c 65 20 62 79 20 74 68 69 73 20 70 72 6f  file by this pro
e6c30 63 65 73 73 2e 20 20 2a 2f 0a 20 20 20 20 20 20  cess.  */.      
e6c40 61 73 73 65 72 74 28 20 65 54 79 70 65 21 3d 53  assert( eType!=S
e6c50 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
e6c60 44 42 20 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73  DB );.      clos
e6c70 65 28 66 64 29 3b 20 20 20 20 20 20 20 20 20 20  e(fd);          
e6c80 20 20 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c     /* silently l
e6c90 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 61 6c 72  eak if fail, alr
e6ca0 65 61 64 79 20 69 6e 20 65 72 72 6f 72 20 2a 2f  eady in error */
e6cb0 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e  .      goto open
e6cc0 5f 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 20 7d  _finished;.    }
e6cd0 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 46 44 5f  .  }..#ifdef FD_
e6ce0 43 4c 4f 45 58 45 43 0a 20 20 66 63 6e 74 6c 28  CLOEXEC.  fcntl(
e6cf0 66 64 2c 20 46 5f 53 45 54 46 44 2c 20 66 63 6e  fd, F_SETFD, fcn
e6d00 74 6c 28 66 64 2c 20 46 5f 47 45 54 46 44 2c 20  tl(fd, F_GETFD, 
e6d10 30 29 20 7c 20 46 44 5f 43 4c 4f 45 58 45 43 29  0) | FD_CLOEXEC)
e6d20 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 6e 6f 4c 6f  ;.#endif..  noLo
e6d30 63 6b 20 3d 20 65 54 79 70 65 21 3d 53 51 4c 49  ck = eType!=SQLI
e6d40 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 3b  TE_OPEN_MAIN_DB;
e6d50 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 50 52 45  ..#if SQLITE_PRE
e6d60 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e  FER_PROXY_LOCKIN
e6d70 47 0a 20 20 69 66 28 20 7a 50 61 74 68 21 3d 4e  G.  if( zPath!=N
e6d80 55 4c 4c 20 26 26 20 21 6e 6f 4c 6f 63 6b 20 26  ULL && !noLock &
e6d90 26 20 70 56 66 73 2d 3e 78 4f 70 65 6e 20 29 7b  & pVfs->xOpen ){
e6da0 0a 20 20 20 20 63 68 61 72 20 2a 65 6e 76 66 6f  .    char *envfo
e6db0 72 63 65 20 3d 20 67 65 74 65 6e 76 28 22 53 51  rce = getenv("SQ
e6dc0 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59  LITE_FORCE_PROXY
e6dd0 5f 4c 4f 43 4b 49 4e 47 22 29 3b 0a 20 20 20 20  _LOCKING");.    
e6de0 69 6e 74 20 75 73 65 50 72 6f 78 79 20 3d 20 30  int useProxy = 0
e6df0 3b 0a 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45  ;..    /* SQLITE
e6e00 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43  _FORCE_PROXY_LOC
e6e10 4b 49 4e 47 3d 3d 31 20 6d 65 61 6e 73 20 66 6f  KING==1 means fo
e6e20 72 63 65 20 61 6c 77 61 79 73 20 75 73 65 20 70  rce always use p
e6e30 72 6f 78 79 2c 20 30 20 6d 65 61 6e 73 20 0a 20  roxy, 0 means . 
e6e40 20 20 20 2a 2a 20 6e 65 76 65 72 20 75 73 65 20     ** never use 
e6e50 70 72 6f 78 79 2c 20 4e 55 4c 4c 20 6d 65 61 6e  proxy, NULL mean
e6e60 73 20 75 73 65 20 70 72 6f 78 79 20 66 6f 72 20  s use proxy for 
e6e70 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 73 20  non-local files 
e6e80 6f 6e 6c 79 2e 20 20 2a 2f 0a 20 20 20 20 69 66  only.  */.    if
e6e90 28 20 65 6e 76 66 6f 72 63 65 21 3d 4e 55 4c 4c  ( envforce!=NULL
e6ea0 20 29 7b 0a 20 20 20 20 20 20 75 73 65 50 72 6f   ){.      usePro
e6eb0 78 79 20 3d 20 61 74 6f 69 28 65 6e 76 66 6f 72  xy = atoi(envfor
e6ec0 63 65 29 3e 30 3b 0a 20 20 20 20 7d 65 6c 73 65  ce)>0;.    }else
e6ed0 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 73  {.      struct s
e6ee0 74 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a 20 20  tatfs fsInfo;.  
e6ef0 20 20 20 20 69 66 28 20 73 74 61 74 66 73 28 7a      if( statfs(z
e6f00 50 61 74 68 2c 20 26 66 73 49 6e 66 6f 29 20 3d  Path, &fsInfo) =
e6f10 3d 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  = -1 ){.        
e6f20 2f 2a 20 49 6e 20 74 68 65 6f 72 79 2c 20 74 68  /* In theory, th
e6f30 65 20 63 6c 6f 73 65 28 66 64 29 20 63 61 6c 6c  e close(fd) call
e6f40 20 69 73 20 73 75 62 2d 6f 70 74 69 6d 61 6c 2e   is sub-optimal.
e6f50 20 49 66 20 74 68 65 20 66 69 6c 65 20 6f 70 65   If the file ope
e6f60 6e 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 77  ned.        ** w
e6f70 69 74 68 20 66 64 20 69 73 20 61 20 64 61 74 61  ith fd is a data
e6f80 62 61 73 65 20 66 69 6c 65 2c 20 61 6e 64 20 74  base file, and t
e6f90 68 65 72 65 20 61 72 65 20 6f 74 68 65 72 20 63  here are other c
e6fa0 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f 70 65 6e 0a  onnections open.
e6fb0 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68          ** on th
e6fc0 61 74 20 66 69 6c 65 20 74 68 61 74 20 61 72 65  at file that are
e6fd0 20 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 69   currently holdi
e6fe0 6e 67 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b  ng advisory lock
e6ff0 73 20 6f 6e 20 69 74 2c 0a 20 20 20 20 20 20 20  s on it,.       
e7000 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 61 6c   ** then the cal
e7010 6c 20 74 6f 20 63 6c 6f 73 65 28 29 20 77 69 6c  l to close() wil
e7020 6c 20 63 61 6e 63 65 6c 20 74 68 6f 73 65 20 6c  l cancel those l
e7030 6f 63 6b 73 2e 20 49 6e 20 70 72 61 63 74 69 63  ocks. In practic
e7040 65 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 65  e,.        ** we
e7050 27 72 65 20 61 73 73 75 6d 69 6e 67 20 74 68 61  're assuming tha
e7060 74 20 73 74 61 74 66 73 28 29 20 64 6f 65 73 6e  t statfs() doesn
e7070 27 74 20 66 61 69 6c 20 76 65 72 79 20 6f 66 74  't fail very oft
e7080 65 6e 2e 20 41 74 20 6c 65 61 73 74 0a 20 20 20  en. At least.   
e7090 20 20 20 20 20 2a 2a 20 6e 6f 74 20 77 68 69 6c       ** not whil
e70a0 65 20 6f 74 68 65 72 20 66 69 6c 65 20 64 65 73  e other file des
e70b0 63 72 69 70 74 6f 72 73 20 6f 70 65 6e 65 64 20  criptors opened 
e70c0 62 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63  by the same proc
e70d0 65 73 73 20 6f 6e 0a 20 20 20 20 20 20 20 20 2a  ess on.        *
e70e0 2a 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20  * the same file 
e70f0 61 72 65 20 77 6f 72 6b 69 6e 67 2e 20 20 2a 2f  are working.  */
e7100 0a 20 20 20 20 20 20 20 20 70 2d 3e 6c 61 73 74  .        p->last
e7110 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
e7120 20 20 20 20 20 20 20 69 66 28 20 64 69 72 66 64         if( dirfd
e7130 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  >=0 ){.         
e7140 20 63 6c 6f 73 65 28 64 69 72 66 64 29 3b 20 2f   close(dirfd); /
e7150 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20  * silently leak 
e7160 69 66 20 66 61 69 6c 2c 20 69 6e 20 65 72 72 6f  if fail, in erro
e7170 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20  r */.        }. 
e7180 20 20 20 20 20 20 20 63 6c 6f 73 65 28 66 64 29         close(fd)
e7190 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65  ; /* silently le
e71a0 61 6b 20 69 66 20 66 61 69 6c 2c 20 69 6e 20 65  ak if fail, in e
e71b0 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20  rror */.        
e71c0 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  rc = SQLITE_IOER
e71d0 52 5f 41 43 43 45 53 53 3b 0a 20 20 20 20 20 20  R_ACCESS;.      
e71e0 20 20 67 6f 74 6f 20 6f 70 65 6e 5f 66 69 6e 69    goto open_fini
e71f0 73 68 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20  shed;.      }.  
e7200 20 20 20 20 75 73 65 50 72 6f 78 79 20 3d 20 21      useProxy = !
e7210 28 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67 73 26  (fsInfo.f_flags&
e7220 4d 4e 54 5f 4c 4f 43 41 4c 29 3b 0a 20 20 20 20  MNT_LOCAL);.    
e7230 7d 0a 20 20 20 20 69 66 28 20 75 73 65 50 72 6f  }.    if( usePro
e7240 78 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  xy ){.      rc =
e7250 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28   fillInUnixFile(
e7260 70 56 66 73 2c 20 66 64 2c 20 64 69 72 66 64 2c  pVfs, fd, dirfd,
e7270 20 70 46 69 6c 65 2c 20 7a 50 61 74 68 2c 20 6e   pFile, zPath, n
e7280 6f 4c 6f 63 6b 2c 20 69 73 44 65 6c 65 74 65 29  oLock, isDelete)
e7290 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
e72a0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
e72b0 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 54       rc = proxyT
e72c0 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65  ransformUnixFile
e72d0 28 28 75 6e 69 78 46 69 6c 65 2a 29 70 46 69 6c  ((unixFile*)pFil
e72e0 65 2c 20 22 3a 61 75 74 6f 3a 22 29 3b 0a 20 20  e, ":auto:");.  
e72f0 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f      }.      goto
e7300 20 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a   open_finished;.
e7310 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
e7320 0a 20 20 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49  .  .  rc = fillI
e7330 6e 55 6e 69 78 46 69 6c 65 28 70 56 66 73 2c 20  nUnixFile(pVfs, 
e7340 66 64 2c 20 64 69 72 66 64 2c 20 70 46 69 6c 65  fd, dirfd, pFile
e7350 2c 20 7a 50 61 74 68 2c 20 6e 6f 4c 6f 63 6b 2c  , zPath, noLock,
e7360 20 69 73 44 65 6c 65 74 65 29 3b 0a 6f 70 65 6e   isDelete);.open
e7370 5f 66 69 6e 69 73 68 65 64 3a 0a 20 20 69 66 28  _finished:.  if(
e7380 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
e7390 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
e73a0 65 65 28 70 2d 3e 70 55 6e 75 73 65 64 29 3b 0a  ee(p->pUnused);.
e73b0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
e73c0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  .}.../*.** Delet
e73d0 65 20 74 68 65 20 66 69 6c 65 20 61 74 20 7a 50  e the file at zP
e73e0 61 74 68 2e 20 49 66 20 74 68 65 20 64 69 72 53  ath. If the dirS
e73f0 79 6e 63 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ync argument is 
e7400 74 72 75 65 2c 20 66 73 79 6e 63 28 29 0a 2a 2a  true, fsync().**
e7410 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 61   the directory a
e7420 66 74 65 72 20 64 65 6c 65 74 69 6e 67 20 74 68  fter deleting th
e7430 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  e file..*/.stati
e7440 63 20 69 6e 74 20 75 6e 69 78 44 65 6c 65 74 65  c int unixDelete
e7450 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  (.  sqlite3_vfs 
e7460 2a 4e 6f 74 55 73 65 64 2c 20 20 20 20 20 2f 2a  *NotUsed,     /*
e7470 20 56 46 53 20 63 6f 6e 74 61 69 6e 69 6e 67 20   VFS containing 
e7480 74 68 69 73 20 61 73 20 74 68 65 20 78 44 65 6c  this as the xDel
e7490 65 74 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  ete method */.  
e74a0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74  const char *zPat
e74b0 68 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  h,        /* Nam
e74c0 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 65 20  e of file to be 
e74d0 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74  deleted */.  int
e74e0 20 64 69 72 53 79 6e 63 20 20 20 20 20 20 20 20   dirSync        
e74f0 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75         /* If tru
e7500 65 2c 20 66 73 79 6e 63 28 29 20 64 69 72 65 63  e, fsync() direc
e7510 74 6f 72 79 20 61 66 74 65 72 20 64 65 6c 65 74  tory after delet
e7520 69 6e 67 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20  ing file */.){. 
e7530 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
e7540 5f 4f 4b 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  _OK;.  UNUSED_PA
e7550 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
e7560 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
e7570 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54  ror(return SQLIT
e7580 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b  E_IOERR_DELETE);
e7590 0a 20 20 75 6e 6c 69 6e 6b 28 7a 50 61 74 68 29  .  unlink(zPath)
e75a0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
e75b0 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43  _DISABLE_DIRSYNC
e75c0 0a 20 20 69 66 28 20 64 69 72 53 79 6e 63 20 29  .  if( dirSync )
e75d0 7b 0a 20 20 20 20 69 6e 74 20 66 64 3b 0a 20 20  {.    int fd;.  
e75e0 20 20 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63    rc = openDirec
e75f0 74 6f 72 79 28 7a 50 61 74 68 2c 20 26 66 64 29  tory(zPath, &fd)
e7600 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
e7610 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 66 20 4f  LITE_OK ){.#if O
e7620 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 20 20  S_VXWORKS.      
e7630 69 66 28 20 66 73 79 6e 63 28 66 64 29 3d 3d 2d  if( fsync(fd)==-
e7640 31 20 29 0a 23 65 6c 73 65 0a 20 20 20 20 20 20  1 ).#else.      
e7650 69 66 28 20 66 73 79 6e 63 28 66 64 29 20 29 0a  if( fsync(fd) ).
e7660 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20  #endif.      {. 
e7670 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
e7680 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 59  TE_IOERR_DIR_FSY
e7690 4e 43 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  NC;.      }.    
e76a0 20 20 69 66 28 20 63 6c 6f 73 65 28 66 64 29 26    if( close(fd)&
e76b0 26 21 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20  &!rc ){.        
e76c0 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  rc = SQLITE_IOER
e76d0 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20  R_DIR_CLOSE;.   
e76e0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23     }.    }.  }.#
e76f0 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72  endif.  return r
e7700 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74  c;.}../*.** Test
e7710 20 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f   the existance o
e7720 66 20 6f 72 20 61 63 63 65 73 73 20 70 65 72 6d  f or access perm
e7730 69 73 73 69 6f 6e 73 20 6f 66 20 66 69 6c 65 20  issions of file 
e7740 7a 50 61 74 68 2e 20 54 68 65 0a 2a 2a 20 74 65  zPath. The.** te
e7750 73 74 20 70 65 72 66 6f 72 6d 65 64 20 64 65 70  st performed dep
e7760 65 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75  ends on the valu
e7770 65 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a  e of flags:.**.*
e7780 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43 43  *     SQLITE_ACC
e7790 45 53 53 5f 45 58 49 53 54 53 3a 20 52 65 74 75  ESS_EXISTS: Retu
e77a0 72 6e 20 31 20 69 66 20 74 68 65 20 66 69 6c 65  rn 1 if the file
e77b0 20 65 78 69 73 74 73 0a 2a 2a 20 20 20 20 20 53   exists.**     S
e77c0 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41  QLITE_ACCESS_REA
e77d0 44 57 52 49 54 45 3a 20 52 65 74 75 72 6e 20 31  DWRITE: Return 1
e77e0 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20   if the file is 
e77f0 72 65 61 64 20 61 6e 64 20 77 72 69 74 61 62 6c  read and writabl
e7800 65 2e 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  e..**     SQLITE
e7810 5f 41 43 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59  _ACCESS_READONLY
e7820 3a 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68  : Return 1 if th
e7830 65 20 66 69 6c 65 20 69 73 20 72 65 61 64 61 62  e file is readab
e7840 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77  le..**.** Otherw
e7850 69 73 65 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f  ise return 0..*/
e7860 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
e7870 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65  Access(.  sqlite
e7880 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20  3_vfs *NotUsed, 
e7890 20 20 2f 2a 20 54 68 65 20 56 46 53 20 63 6f 6e    /* The VFS con
e78a0 74 61 69 6e 69 6e 67 20 74 68 69 73 20 78 41 63  taining this xAc
e78b0 63 65 73 73 20 6d 65 74 68 6f 64 20 2a 2f 0a 20  cess method */. 
e78c0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61   const char *zPa
e78d0 74 68 2c 20 20 20 20 20 20 2f 2a 20 50 61 74 68  th,      /* Path
e78e0 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20   of the file to 
e78f0 65 78 61 6d 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  examine */.  int
e7900 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
e7910 20 20 20 20 20 2f 2a 20 57 68 61 74 20 64 6f 20       /* What do 
e7920 77 65 20 77 61 6e 74 20 74 6f 20 6c 65 61 72 6e  we want to learn
e7930 20 61 62 6f 75 74 20 74 68 65 20 7a 50 61 74 68   about the zPath
e7940 20 66 69 6c 65 3f 20 2a 2f 0a 20 20 69 6e 74 20   file? */.  int 
e7950 2a 70 52 65 73 4f 75 74 20 20 20 20 20 20 20 20  *pResOut        
e7960 20 20 20 20 2f 2a 20 57 72 69 74 65 20 72 65 73      /* Write res
e7970 75 6c 74 20 62 6f 6f 6c 65 61 6e 20 68 65 72 65  ult boolean here
e7980 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 6d 6f   */.){.  int amo
e7990 64 65 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45 44  de = 0;.  UNUSED
e79a0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
e79b0 65 64 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49  ed);.  SimulateI
e79c0 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53  OError( return S
e79d0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45  QLITE_IOERR_ACCE
e79e0 53 53 3b 20 29 3b 0a 20 20 73 77 69 74 63 68 28  SS; );.  switch(
e79f0 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61   flags ){.    ca
e7a00 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  se SQLITE_ACCESS
e7a10 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 61  _EXISTS:.      a
e7a20 6d 6f 64 65 20 3d 20 46 5f 4f 4b 3b 0a 20 20 20  mode = F_OK;.   
e7a30 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
e7a40 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  se SQLITE_ACCESS
e7a50 5f 52 45 41 44 57 52 49 54 45 3a 0a 20 20 20 20  _READWRITE:.    
e7a60 20 20 61 6d 6f 64 65 20 3d 20 57 5f 4f 4b 7c 52    amode = W_OK|R
e7a70 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  _OK;.      break
e7a80 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
e7a90 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20  E_ACCESS_READ:. 
e7aa0 20 20 20 20 20 61 6d 6f 64 65 20 3d 20 52 5f 4f       amode = R_O
e7ab0 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  K;.      break;.
e7ac0 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20  .    default:.  
e7ad0 20 20 20 20 61 73 73 65 72 74 28 21 22 49 6e 76      assert(!"Inv
e7ae0 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d  alid flags argum
e7af0 65 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52  ent");.  }.  *pR
e7b00 65 73 4f 75 74 20 3d 20 28 61 63 63 65 73 73 28  esOut = (access(
e7b10 7a 50 61 74 68 2c 20 61 6d 6f 64 65 29 3d 3d 30  zPath, amode)==0
e7b20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
e7b30 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  TE_OK;.}.../*.**
e7b40 20 54 75 72 6e 20 61 20 72 65 6c 61 74 69 76 65   Turn a relative
e7b50 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61   pathname into a
e7b60 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20   full pathname. 
e7b70 54 68 65 20 72 65 6c 61 74 69 76 65 20 70 61 74  The relative pat
e7b80 68 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 20 61  h.** is stored a
e7b90 73 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74  s a nul-terminat
e7ba0 65 64 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65  ed string in the
e7bb0 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20   buffer pointed 
e7bc0 74 6f 20 62 79 0a 2a 2a 20 7a 50 61 74 68 2e 20  to by.** zPath. 
e7bd0 0a 2a 2a 0a 2a 2a 20 7a 4f 75 74 20 70 6f 69 6e  .**.** zOut poin
e7be0 74 73 20 74 6f 20 61 20 62 75 66 66 65 72 20 6f  ts to a buffer o
e7bf0 66 20 61 74 20 6c 65 61 73 74 20 73 71 6c 69 74  f at least sqlit
e7c00 65 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d  e3_vfs.mxPathnam
e7c10 65 20 62 79 74 65 73 20 0a 2a 2a 20 28 69 6e 20  e bytes .** (in 
e7c20 74 68 69 73 20 63 61 73 65 2c 20 4d 41 58 5f 50  this case, MAX_P
e7c30 41 54 48 4e 41 4d 45 20 62 79 74 65 73 29 2e 20  ATHNAME bytes). 
e7c40 54 68 65 20 66 75 6c 6c 2d 70 61 74 68 20 69 73  The full-path is
e7c50 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 74   written to.** t
e7c60 68 69 73 20 62 75 66 66 65 72 20 62 65 66 6f 72  his buffer befor
e7c70 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a  e returning..*/.
e7c80 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46  static int unixF
e7c90 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73  ullPathname(.  s
e7ca0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
e7cb0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
e7cc0 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f  Pointer to vfs o
e7cd0 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74  bject */.  const
e7ce0 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20   char *zPath,   
e7cf0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 73           /* Poss
e7d00 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e  ibly relative in
e7d10 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e  put path */.  in
e7d20 74 20 6e 4f 75 74 2c 20 20 20 20 20 20 20 20 20  t nOut,         
e7d30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
e7d40 69 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75  ize of output bu
e7d50 66 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f  ffer in bytes */
e7d60 0a 20 20 63 68 61 72 20 2a 7a 4f 75 74 20 20 20  .  char *zOut   
e7d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e7d80 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65   /* Output buffe
e7d90 72 20 2a 2f 0a 29 7b 0a 0a 20 20 2f 2a 20 49 74  r */.){..  /* It
e7da0 27 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61  's odd to simula
e7db0 74 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68  te an io-error h
e7dc0 65 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20  ere, but really 
e7dd0 74 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a  this is just.  *
e7de0 2a 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65  * using the io-e
e7df0 72 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74  rror infrastruct
e7e00 75 72 65 20 74 6f 20 74 65 73 74 20 74 68 61 74  ure to test that
e7e10 20 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20   SQLite handles 
e7e20 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69  this.  ** functi
e7e30 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 54 68 69 73  on failing. This
e7e40 20 66 75 6e 63 74 69 6f 6e 20 63 6f 75 6c 64 20   function could 
e7e50 66 61 69 6c 20 69 66 2c 20 66 6f 72 20 65 78 61  fail if, for exa
e7e60 6d 70 6c 65 2c 20 74 68 65 0a 20 20 2a 2a 20 63  mple, the.  ** c
e7e70 75 72 72 65 6e 74 20 77 6f 72 6b 69 6e 67 20 64  urrent working d
e7e80 69 72 65 63 74 6f 72 79 20 68 61 73 20 62 65 65  irectory has bee
e7e90 6e 20 75 6e 6c 69 6e 6b 65 64 2e 0a 20 20 2a 2f  n unlinked..  */
e7ea0 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
e7eb0 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54  or( return SQLIT
e7ec0 45 5f 45 52 52 4f 52 20 29 3b 0a 0a 20 20 61 73  E_ERROR );..  as
e7ed0 73 65 72 74 28 20 70 56 66 73 2d 3e 6d 78 50 61  sert( pVfs->mxPa
e7ee0 74 68 6e 61 6d 65 3d 3d 4d 41 58 5f 50 41 54 48  thname==MAX_PATH
e7ef0 4e 41 4d 45 20 29 3b 0a 20 20 55 4e 55 53 45 44  NAME );.  UNUSED
e7f00 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29  _PARAMETER(pVfs)
e7f10 3b 0a 0a 20 20 7a 4f 75 74 5b 6e 4f 75 74 2d 31  ;..  zOut[nOut-1
e7f20 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 20  ] = '\0';.  if( 
e7f30 7a 50 61 74 68 5b 30 5d 3d 3d 27 2f 27 20 29 7b  zPath[0]=='/' ){
e7f40 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
e7f50 72 69 6e 74 66 28 6e 4f 75 74 2c 20 7a 4f 75 74  rintf(nOut, zOut
e7f60 2c 20 22 25 73 22 2c 20 7a 50 61 74 68 29 3b 0a  , "%s", zPath);.
e7f70 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74    }else{.    int
e7f80 20 6e 43 77 64 3b 0a 20 20 20 20 69 66 28 20 67   nCwd;.    if( g
e7f90 65 74 63 77 64 28 7a 4f 75 74 2c 20 6e 4f 75 74  etcwd(zOut, nOut
e7fa0 2d 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  -1)==0 ){.      
e7fb0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41  return SQLITE_CA
e7fc0 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20  NTOPEN;.    }.  
e7fd0 20 20 6e 43 77 64 20 3d 20 28 69 6e 74 29 73 74    nCwd = (int)st
e7fe0 72 6c 65 6e 28 7a 4f 75 74 29 3b 0a 20 20 20 20  rlen(zOut);.    
e7ff0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
e8000 28 6e 4f 75 74 2d 6e 43 77 64 2c 20 26 7a 4f 75  (nOut-nCwd, &zOu
e8010 74 5b 6e 43 77 64 5d 2c 20 22 2f 25 73 22 2c 20  t[nCwd], "/%s", 
e8020 7a 50 61 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65  zPath);.  }.  re
e8030 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
e8040 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  }...#ifndef SQLI
e8050 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
e8060 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74  ENSION./*.** Int
e8070 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e  erfaces for open
e8080 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62  ing a shared lib
e8090 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e  rary, finding en
e80a0 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69  try points.** wi
e80b0 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20  thin the shared 
e80c0 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f  library, and clo
e80d0 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20  sing the shared 
e80e0 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 6e 63  library..*/.#inc
e80f0 6c 75 64 65 20 3c 64 6c 66 63 6e 2e 68 3e 0a 73  lude <dlfcn.h>.s
e8100 74 61 74 69 63 20 76 6f 69 64 20 2a 75 6e 69 78  tatic void *unix
e8110 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76  DlOpen(sqlite3_v
e8120 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 63 6f 6e  fs *NotUsed, con
e8130 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
e8140 6d 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  me){.  UNUSED_PA
e8150 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
e8160 3b 0a 20 20 72 65 74 75 72 6e 20 64 6c 6f 70 65  ;.  return dlope
e8170 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 52 54 4c  n(zFilename, RTL
e8180 44 5f 4e 4f 57 20 7c 20 52 54 4c 44 5f 47 4c 4f  D_NOW | RTLD_GLO
e8190 42 41 4c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  BAL);.}../*.** S
e81a0 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 69 73  QLite calls this
e81b0 20 66 75 6e 63 74 69 6f 6e 20 69 6d 6d 65 64 69   function immedi
e81c0 61 74 65 6c 79 20 61 66 74 65 72 20 61 20 63 61  ately after a ca
e81d0 6c 6c 20 74 6f 20 75 6e 69 78 44 6c 53 79 6d 28  ll to unixDlSym(
e81e0 29 20 6f 72 0a 2a 2a 20 75 6e 69 78 44 6c 4f 70  ) or.** unixDlOp
e81f0 65 6e 28 29 20 66 61 69 6c 73 20 28 72 65 74 75  en() fails (retu
e8200 72 6e 73 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74  rns a null point
e8210 65 72 29 2e 20 49 66 20 61 20 6d 6f 72 65 20 64  er). If a more d
e8220 65 74 61 69 6c 65 64 20 65 72 72 6f 72 0a 2a 2a  etailed error.**
e8230 20 6d 65 73 73 61 67 65 20 69 73 20 61 76 61 69   message is avai
e8240 6c 61 62 6c 65 2c 20 69 74 20 69 73 20 77 72 69  lable, it is wri
e8250 74 74 65 6e 20 74 6f 20 7a 42 75 66 4f 75 74 2e  tten to zBufOut.
e8260 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 6d 65 73   If no error mes
e8270 73 61 67 65 0a 2a 2a 20 69 73 20 61 76 61 69 6c  sage.** is avail
e8280 61 62 6c 65 2c 20 7a 42 75 66 4f 75 74 20 69 73  able, zBufOut is
e8290 20 6c 65 66 74 20 75 6e 6d 6f 64 69 66 69 65 64   left unmodified
e82a0 20 61 6e 64 20 53 51 4c 69 74 65 20 75 73 65 73   and SQLite uses
e82b0 20 61 20 64 65 66 61 75 6c 74 0a 2a 2a 20 65 72   a default.** er
e82c0 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f 0a  ror message..*/.
e82d0 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78  static void unix
e82e0 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f  DlError(sqlite3_
e82f0 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e  vfs *NotUsed, in
e8300 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42  t nBuf, char *zB
e8310 75 66 4f 75 74 29 7b 0a 20 20 63 68 61 72 20 2a  ufOut){.  char *
e8320 7a 45 72 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50  zErr;.  UNUSED_P
e8330 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
e8340 29 3b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75  );.  unixEnterMu
e8350 74 65 78 28 29 3b 0a 20 20 7a 45 72 72 20 3d 20  tex();.  zErr = 
e8360 64 6c 65 72 72 6f 72 28 29 3b 0a 20 20 69 66 28  dlerror();.  if(
e8370 20 7a 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c   zErr ){.    sql
e8380 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42  ite3_snprintf(nB
e8390 75 66 2c 20 7a 42 75 66 4f 75 74 2c 20 22 25 73  uf, zBufOut, "%s
e83a0 22 2c 20 7a 45 72 72 29 3b 0a 20 20 7d 0a 20 20  ", zErr);.  }.  
e83b0 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29  unixLeaveMutex()
e83c0 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ;.}.static void 
e83d0 28 2a 75 6e 69 78 44 6c 53 79 6d 28 73 71 6c 69  (*unixDlSym(sqli
e83e0 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64  te3_vfs *NotUsed
e83f0 2c 20 76 6f 69 64 20 2a 70 2c 20 63 6f 6e 73 74  , void *p, const
e8400 20 63 68 61 72 2a 7a 53 79 6d 29 29 28 76 6f 69   char*zSym))(voi
e8410 64 29 7b 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 47  d){.  /* .  ** G
e8420 43 43 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69  CC with -pedanti
e8430 63 2d 65 72 72 6f 72 73 20 73 61 79 73 20 74 68  c-errors says th
e8440 61 74 20 43 39 30 20 64 6f 65 73 20 6e 6f 74 20  at C90 does not 
e8450 61 6c 6c 6f 77 20 61 20 76 6f 69 64 2a 20 74 6f  allow a void* to
e8460 20 62 65 0a 20 20 2a 2a 20 63 61 73 74 20 69 6e   be.  ** cast in
e8470 74 6f 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  to a pointer to 
e8480 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 6e 64  a function.  And
e8490 20 79 65 74 20 74 68 65 20 6c 69 62 72 61 72 79   yet the library
e84a0 20 64 6c 73 79 6d 28 29 20 72 6f 75 74 69 6e 65   dlsym() routine
e84b0 0a 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 61 20  .  ** returns a 
e84c0 76 6f 69 64 2a 20 77 68 69 63 68 20 69 73 20 72  void* which is r
e84d0 65 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20  eally a pointer 
e84e0 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20  to a function.  
e84f0 53 6f 20 68 6f 77 20 64 6f 20 77 65 0a 20 20 2a  So how do we.  *
e8500 2a 20 75 73 65 20 64 6c 73 79 6d 28 29 20 77 69  * use dlsym() wi
e8510 74 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 72  th -pedantic-err
e8520 6f 72 73 3f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56  ors?.  **.  ** V
e8530 61 72 69 61 62 6c 65 20 78 20 62 65 6c 6f 77 20  ariable x below 
e8540 69 73 20 64 65 66 69 6e 65 64 20 74 6f 20 62 65  is defined to be
e8550 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
e8560 66 75 6e 63 74 69 6f 6e 20 74 61 6b 69 6e 67 0a  function taking.
e8570 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20    ** parameters 
e8580 76 6f 69 64 2a 20 61 6e 64 20 63 6f 6e 73 74 20  void* and const 
e8590 63 68 61 72 2a 20 61 6e 64 20 72 65 74 75 72 6e  char* and return
e85a0 69 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ing a pointer to
e85b0 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a   a function..  *
e85c0 2a 20 57 65 20 69 6e 69 74 69 61 6c 69 7a 65 20  * We initialize 
e85d0 78 20 62 79 20 61 73 73 69 67 6e 69 6e 67 20 69  x by assigning i
e85e0 74 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  t a pointer to t
e85f0 68 65 20 64 6c 73 79 6d 28 29 20 66 75 6e 63 74  he dlsym() funct
e8600 69 6f 6e 2e 0a 20 20 2a 2a 20 28 54 68 61 74 20  ion..  ** (That 
e8610 61 73 73 69 67 6e 6d 65 6e 74 20 72 65 71 75 69  assignment requi
e8620 72 65 73 20 61 20 63 61 73 74 2e 29 20 20 54 68  res a cast.)  Th
e8630 65 6e 20 77 65 20 63 61 6c 6c 20 74 68 65 20 66  en we call the f
e8640 75 6e 63 74 69 6f 6e 20 74 68 61 74 0a 20 20 2a  unction that.  *
e8650 2a 20 78 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20  * x points to.  
e8660 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20  .  **.  ** This 
e8670 77 6f 72 6b 2d 61 72 6f 75 6e 64 20 69 73 20 75  work-around is u
e8680 6e 6c 69 6b 65 6c 79 20 74 6f 20 77 6f 72 6b 20  nlikely to work 
e8690 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 61 6e 79  correctly on any
e86a0 20 73 79 73 74 65 6d 20 77 68 65 72 65 0a 20 20   system where.  
e86b0 2a 2a 20 79 6f 75 20 72 65 61 6c 6c 79 20 63 61  ** you really ca
e86c0 6e 6e 6f 74 20 63 61 73 74 20 61 20 66 75 6e 63  nnot cast a func
e86d0 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20 69 6e 74  tion pointer int
e86e0 6f 20 76 6f 69 64 2a 2e 20 20 42 75 74 20 74 68  o void*.  But th
e86f0 65 6e 2c 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20  en, on the.  ** 
e8700 6f 74 68 65 72 20 68 61 6e 64 2c 20 64 6c 73 79  other hand, dlsy
e8710 6d 28 29 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72  m() will not wor
e8720 6b 20 6f 6e 20 73 75 63 68 20 61 20 73 79 73 74  k on such a syst
e8730 65 6d 20 65 69 74 68 65 72 2c 20 73 6f 20 77 65  em either, so we
e8740 20 68 61 76 65 0a 20 20 2a 2a 20 6e 6f 74 20 72   have.  ** not r
e8750 65 61 6c 6c 79 20 6c 6f 73 74 20 61 6e 79 74 68  eally lost anyth
e8760 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 76 6f 69 64  ing..  */.  void
e8770 20 28 2a 28 2a 78 29 28 76 6f 69 64 2a 2c 63 6f   (*(*x)(void*,co
e8780 6e 73 74 20 63 68 61 72 2a 29 29 28 76 6f 69 64  nst char*))(void
e8790 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
e87a0 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
e87b0 20 20 78 20 3d 20 28 76 6f 69 64 28 2a 28 2a 29    x = (void(*(*)
e87c0 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61  (void*,const cha
e87d0 72 2a 29 29 28 76 6f 69 64 29 29 64 6c 73 79 6d  r*))(void))dlsym
e87e0 3b 0a 20 20 72 65 74 75 72 6e 20 28 2a 78 29 28  ;.  return (*x)(
e87f0 70 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 73 74 61 74  p, zSym);.}.stat
e8800 69 63 20 76 6f 69 64 20 75 6e 69 78 44 6c 43 6c  ic void unixDlCl
e8810 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ose(sqlite3_vfs 
e8820 2a 4e 6f 74 55 73 65 64 2c 20 76 6f 69 64 20 2a  *NotUsed, void *
e8830 70 48 61 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53  pHandle){.  UNUS
e8840 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
e8850 55 73 65 64 29 3b 0a 20 20 64 6c 63 6c 6f 73 65  Used);.  dlclose
e8860 28 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c  (pHandle);.}.#el
e8870 73 65 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f  se /* if SQLITE_
e8880 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53  OMIT_LOAD_EXTENS
e8890 49 4f 4e 20 69 73 20 64 65 66 69 6e 65 64 3a 20  ION is defined: 
e88a0 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69  */.  #define uni
e88b0 78 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23 64 65  xDlOpen  0.  #de
e88c0 66 69 6e 65 20 75 6e 69 78 44 6c 45 72 72 6f 72  fine unixDlError
e88d0 20 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69   0.  #define uni
e88e0 78 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65  xDlSym   0.  #de
e88f0 66 69 6e 65 20 75 6e 69 78 44 6c 43 6c 6f 73 65  fine unixDlClose
e8900 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   0.#endif../*.**
e8910 20 57 72 69 74 65 20 6e 42 75 66 20 62 79 74 65   Write nBuf byte
e8920 73 20 6f 66 20 72 61 6e 64 6f 6d 20 64 61 74 61  s of random data
e8930 20 74 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64   to the supplied
e8940 20 62 75 66 66 65 72 20 7a 42 75 66 2e 0a 2a 2f   buffer zBuf..*/
e8950 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
e8960 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74  Randomness(sqlit
e8970 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c  e3_vfs *NotUsed,
e8980 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20   int nBuf, char 
e8990 2a 7a 42 75 66 29 7b 0a 20 20 55 4e 55 53 45 44  *zBuf){.  UNUSED
e89a0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
e89b0 65 64 29 3b 0a 20 20 61 73 73 65 72 74 28 28 73  ed);.  assert((s
e89c0 69 7a 65 5f 74 29 6e 42 75 66 3e 3d 28 73 69 7a  ize_t)nBuf>=(siz
e89d0 65 6f 66 28 74 69 6d 65 5f 74 29 2b 73 69 7a 65  eof(time_t)+size
e89e0 6f 66 28 69 6e 74 29 29 29 3b 0a 0a 20 20 2f 2a  of(int)));..  /*
e89f0 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e 69 74   We have to init
e8a00 69 61 6c 69 7a 65 20 7a 42 75 66 20 74 6f 20 70  ialize zBuf to p
e8a10 72 65 76 65 6e 74 20 76 61 6c 67 72 69 6e 64 20  revent valgrind 
e8a20 66 72 6f 6d 20 72 65 70 6f 72 74 69 6e 67 0a 20  from reporting. 
e8a30 20 2a 2a 20 65 72 72 6f 72 73 2e 20 20 54 68 65   ** errors.  The
e8a40 20 72 65 70 6f 72 74 73 20 69 73 73 75 65 64 20   reports issued 
e8a50 62 79 20 76 61 6c 67 72 69 6e 64 20 61 72 65 20  by valgrind are 
e8a60 69 6e 63 6f 72 72 65 63 74 20 2d 20 77 65 20 77  incorrect - we w
e8a70 6f 75 6c 64 0a 20 20 2a 2a 20 70 72 65 66 65 72  ould.  ** prefer
e8a80 20 74 68 61 74 20 74 68 65 20 72 61 6e 64 6f 6d   that the random
e8a90 6e 65 73 73 20 62 65 20 69 6e 63 72 65 61 73 65  ness be increase
e8aa0 64 20 62 79 20 6d 61 6b 69 6e 67 20 75 73 65 20  d by making use 
e8ab0 6f 66 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 6e  of the.  ** unin
e8ac0 69 74 69 61 6c 69 7a 65 64 20 73 70 61 63 65 20  itialized space 
e8ad0 69 6e 20 7a 42 75 66 20 2d 20 62 75 74 20 76 61  in zBuf - but va
e8ae0 6c 67 72 69 6e 64 20 65 72 72 6f 72 73 20 74 65  lgrind errors te
e8af0 6e 64 20 74 6f 20 77 6f 72 72 79 0a 20 20 2a 2a  nd to worry.  **
e8b00 20 73 6f 6d 65 20 75 73 65 72 73 2e 20 20 52 61   some users.  Ra
e8b10 74 68 65 72 20 74 68 61 6e 20 61 72 67 75 65 2c  ther than argue,
e8b20 20 69 74 20 73 65 65 6d 73 20 65 61 73 69 65 72   it seems easier
e8b30 20 6a 75 73 74 20 74 6f 20 69 6e 69 74 69 61 6c   just to initial
e8b40 69 7a 65 0a 20 20 2a 2a 20 74 68 65 20 77 68 6f  ize.  ** the who
e8b50 6c 65 20 61 72 72 61 79 20 61 6e 64 20 73 69 6c  le array and sil
e8b60 65 6e 63 65 20 76 61 6c 67 72 69 6e 64 2c 20 65  ence valgrind, e
e8b70 76 65 6e 20 69 66 20 74 68 61 74 20 6d 65 61 6e  ven if that mean
e8b80 73 20 6c 65 73 73 20 72 61 6e 64 6f 6d 6e 65 73  s less randomnes
e8b90 73 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 61  s.  ** in the ra
e8ba0 6e 64 6f 6d 20 73 65 65 64 2e 0a 20 20 2a 2a 0a  ndom seed..  **.
e8bb0 20 20 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e    ** When testin
e8bc0 67 2c 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20  g, initializing 
e8bd0 7a 42 75 66 5b 5d 20 74 6f 20 7a 65 72 6f 20 69  zBuf[] to zero i
e8be0 73 20 61 6c 6c 20 77 65 20 64 6f 2e 20 20 54 68  s all we do.  Th
e8bf0 61 74 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 74 68  at means.  ** th
e8c00 61 74 20 77 65 20 61 6c 77 61 79 73 20 75 73 65  at we always use
e8c10 20 74 68 65 20 73 61 6d 65 20 72 61 6e 64 6f 6d   the same random
e8c20 20 6e 75 6d 62 65 72 20 73 65 71 75 65 6e 63 65   number sequence
e8c30 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68  .  This makes th
e8c40 65 0a 20 20 2a 2a 20 74 65 73 74 73 20 72 65 70  e.  ** tests rep
e8c50 65 61 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20  eatable..  */.  
e8c60 6d 65 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20  memset(zBuf, 0, 
e8c70 6e 42 75 66 29 3b 0a 23 69 66 20 21 64 65 66 69  nBuf);.#if !defi
e8c80 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29  ned(SQLITE_TEST)
e8c90 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 70 69 64  .  {.    int pid
e8ca0 2c 20 66 64 3b 0a 20 20 20 20 66 64 20 3d 20 6f  , fd;.    fd = o
e8cb0 70 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e 64 6f  pen("/dev/urando
e8cc0 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20  m", O_RDONLY);. 
e8cd0 20 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20     if( fd<0 ){. 
e8ce0 20 20 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20       time_t t;. 
e8cf0 20 20 20 20 20 74 69 6d 65 28 26 74 29 3b 0a 20       time(&t);. 
e8d00 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 42 75 66       memcpy(zBuf
e8d10 2c 20 26 74 2c 20 73 69 7a 65 6f 66 28 74 29 29  , &t, sizeof(t))
e8d20 3b 0a 20 20 20 20 20 20 70 69 64 20 3d 20 67 65  ;.      pid = ge
e8d30 74 70 69 64 28 29 3b 0a 20 20 20 20 20 20 6d 65  tpid();.      me
e8d40 6d 63 70 79 28 26 7a 42 75 66 5b 73 69 7a 65 6f  mcpy(&zBuf[sizeo
e8d50 66 28 74 29 5d 2c 20 26 70 69 64 2c 20 73 69 7a  f(t)], &pid, siz
e8d60 65 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20 20  eof(pid));.     
e8d70 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28   assert( sizeof(
e8d80 74 29 2b 73 69 7a 65 6f 66 28 70 69 64 29 3c 3d  t)+sizeof(pid)<=
e8d90 28 73 69 7a 65 5f 74 29 6e 42 75 66 20 29 3b 0a  (size_t)nBuf );.
e8da0 20 20 20 20 20 20 6e 42 75 66 20 3d 20 73 69 7a        nBuf = siz
e8db0 65 6f 66 28 74 29 20 2b 20 73 69 7a 65 6f 66 28  eof(t) + sizeof(
e8dc0 70 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  pid);.    }else{
e8dd0 0a 20 20 20 20 20 20 6e 42 75 66 20 3d 20 72 65  .      nBuf = re
e8de0 61 64 28 66 64 2c 20 7a 42 75 66 2c 20 6e 42 75  ad(fd, zBuf, nBu
e8df0 66 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73 65 28  f);.      close(
e8e00 66 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  fd);.    }.  }.#
e8e10 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 6e  endif.  return n
e8e20 42 75 66 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53  Buf;.}.../*.** S
e8e30 6c 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c  leep for a littl
e8e40 65 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e  e while.  Return
e8e50 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74   the amount of t
e8e60 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2a 20 54 68  ime slept..** Th
e8e70 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  e argument is th
e8e80 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72  e number of micr
e8e90 6f 73 65 63 6f 6e 64 73 20 77 65 20 77 61 6e 74  oseconds we want
e8ea0 20 74 6f 20 73 6c 65 65 70 2e 0a 2a 2a 20 54 68   to sleep..** Th
e8eb0 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69  e return value i
e8ec0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
e8ed0 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 6f 66 20  microseconds of 
e8ee0 73 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a  sleep actually.*
e8ef0 2a 20 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d  * requested from
e8f00 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   the underlying 
e8f10 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
e8f20 2c 20 61 20 6e 75 6d 62 65 72 20 77 68 69 63 68  , a number which
e8f30 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 67 72 65  .** might be gre
e8f40 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75  ater than or equ
e8f50 61 6c 20 74 6f 20 74 68 65 20 61 72 67 75 6d 65  al to the argume
e8f60 6e 74 2c 20 62 75 74 20 6e 6f 74 20 6c 65 73 73  nt, but not less
e8f70 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 61 72 67  .** than the arg
e8f80 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  ument..*/.static
e8f90 20 69 6e 74 20 75 6e 69 78 53 6c 65 65 70 28 73   int unixSleep(s
e8fa0 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55  qlite3_vfs *NotU
e8fb0 73 65 64 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65  sed, int microse
e8fc0 63 6f 6e 64 73 29 7b 0a 23 69 66 20 4f 53 5f 56  conds){.#if OS_V
e8fd0 58 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 74 20  XWORKS.  struct 
e8fe0 74 69 6d 65 73 70 65 63 20 73 70 3b 0a 0a 20 20  timespec sp;..  
e8ff0 73 70 2e 74 76 5f 73 65 63 20 3d 20 6d 69 63 72  sp.tv_sec = micr
e9000 6f 73 65 63 6f 6e 64 73 20 2f 20 31 30 30 30 30  oseconds / 10000
e9010 30 30 3b 0a 20 20 73 70 2e 74 76 5f 6e 73 65 63  00;.  sp.tv_nsec
e9020 20 3d 20 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73   = (microseconds
e9030 20 25 20 31 30 30 30 30 30 30 29 20 2a 20 31 30   % 1000000) * 10
e9040 30 30 3b 0a 20 20 6e 61 6e 6f 73 6c 65 65 70 28  00;.  nanosleep(
e9050 26 73 70 2c 20 4e 55 4c 4c 29 3b 0a 20 20 55 4e  &sp, NULL);.  UN
e9060 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
e9070 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72  otUsed);.  retur
e9080 6e 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a  n microseconds;.
e9090 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41  #elif defined(HA
e90a0 56 45 5f 55 53 4c 45 45 50 29 20 26 26 20 48 41  VE_USLEEP) && HA
e90b0 56 45 5f 55 53 4c 45 45 50 0a 20 20 75 73 6c 65  VE_USLEEP.  usle
e90c0 65 70 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29  ep(microseconds)
e90d0 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
e90e0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
e90f0 20 72 65 74 75 72 6e 20 6d 69 63 72 6f 73 65 63   return microsec
e9100 6f 6e 64 73 3b 0a 23 65 6c 73 65 0a 20 20 69 6e  onds;.#else.  in
e9110 74 20 73 65 63 6f 6e 64 73 20 3d 20 28 6d 69 63  t seconds = (mic
e9120 72 6f 73 65 63 6f 6e 64 73 2b 39 39 39 39 39 39  roseconds+999999
e9130 29 2f 31 30 30 30 30 30 30 3b 0a 20 20 73 6c 65  )/1000000;.  sle
e9140 65 70 28 73 65 63 6f 6e 64 73 29 3b 0a 20 20 55  ep(seconds);.  U
e9150 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
e9160 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75  NotUsed);.  retu
e9170 72 6e 20 73 65 63 6f 6e 64 73 2a 31 30 30 30 30  rn seconds*10000
e9180 30 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a  00;.#endif.}../*
e9190 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
e91a0 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73  g variable, if s
e91b0 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f  et to a non-zero
e91c0 20 76 61 6c 75 65 2c 20 69 73 20 69 6e 74 65 72   value, is inter
e91d0 70 72 65 74 65 64 20 61 73 0a 2a 2a 20 74 68 65  preted as.** the
e91e0 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 63 6f 6e   number of secon
e91f0 64 73 20 73 69 6e 63 65 20 31 39 37 30 20 61 6e  ds since 1970 an
e9200 64 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 74  d is used to set
e9210 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a   the result of.*
e9220 2a 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65  * sqlite3OsCurre
e9230 6e 74 54 69 6d 65 28 29 20 64 75 72 69 6e 67 20  ntTime() during 
e9240 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64  testing..*/.#ifd
e9250 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
e9260 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
e9270 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69  lite3_current_ti
e9280 6d 65 20 3d 20 30 3b 20 20 2f 2a 20 46 61 6b 65  me = 0;  /* Fake
e9290 20 73 79 73 74 65 6d 20 74 69 6d 65 20 69 6e 20   system time in 
e92a0 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39  seconds since 19
e92b0 37 30 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f  70. */.#endif../
e92c0 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 75  *.** Find the cu
e92d0 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 55  rrent time (in U
e92e0 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e  niversal Coordin
e92f0 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72 69  ated Time).  Wri
e9300 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e  te the.** curren
e9310 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20  t time and date 
e9320 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20  as a Julian Day 
e9330 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 4e  number into *prN
e9340 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e  ow and.** return
e9350 20 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66   0.  Return 1 if
e9360 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64 61   the time and da
e9370 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75  te cannot be fou
e9380 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  nd..*/.static in
e9390 74 20 75 6e 69 78 43 75 72 72 65 6e 74 54 69 6d  t unixCurrentTim
e93a0 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e  e(sqlite3_vfs *N
e93b0 6f 74 55 73 65 64 2c 20 64 6f 75 62 6c 65 20 2a  otUsed, double *
e93c0 70 72 4e 6f 77 29 7b 0a 23 69 66 20 64 65 66 69  prNow){.#if defi
e93d0 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
e93e0 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a  FLOATING_POINT).
e93f0 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 74 69    time_t t;.  ti
e9400 6d 65 28 26 74 29 3b 0a 20 20 2a 70 72 4e 6f 77  me(&t);.  *prNow
e9410 20 3d 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e   = (((sqlite3_in
e9420 74 36 34 29 74 29 2f 38 36 34 30 20 2b 20 32 34  t64)t)/8640 + 24
e9430 34 30 35 38 37 35 29 2f 31 30 3b 0a 23 65 6c 69  405875)/10;.#eli
e9440 66 20 64 65 66 69 6e 65 64 28 4e 4f 5f 47 45 54  f defined(NO_GET
e9450 54 4f 44 29 0a 20 20 74 69 6d 65 5f 74 20 74 3b  TOD).  time_t t;
e9460 0a 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 2a  .  time(&t);.  *
e9470 70 72 4e 6f 77 20 3d 20 74 2f 38 36 34 30 30 2e  prNow = t/86400.
e9480 30 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a 23  0 + 2440587.5;.#
e9490 65 6c 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a  elif OS_VXWORKS.
e94a0 20 20 73 74 72 75 63 74 20 74 69 6d 65 73 70 65    struct timespe
e94b0 63 20 73 4e 6f 77 3b 0a 20 20 63 6c 6f 63 6b 5f  c sNow;.  clock_
e94c0 67 65 74 74 69 6d 65 28 43 4c 4f 43 4b 5f 52 45  gettime(CLOCK_RE
e94d0 41 4c 54 49 4d 45 2c 20 26 73 4e 6f 77 29 3b 0a  ALTIME, &sNow);.
e94e0 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34 30 35    *prNow = 24405
e94f0 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 5f 73  87.5 + sNow.tv_s
e9500 65 63 2f 38 36 34 30 30 2e 30 20 2b 20 73 4e 6f  ec/86400.0 + sNo
e9510 77 2e 74 76 5f 6e 73 65 63 2f 38 36 34 30 30 30  w.tv_nsec/864000
e9520 30 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 6c 73  00000000.0;.#els
e9530 65 0a 20 20 73 74 72 75 63 74 20 74 69 6d 65 76  e.  struct timev
e9540 61 6c 20 73 4e 6f 77 3b 0a 20 20 67 65 74 74 69  al sNow;.  getti
e9550 6d 65 6f 66 64 61 79 28 26 73 4e 6f 77 2c 20 30  meofday(&sNow, 0
e9560 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34  );.  *prNow = 24
e9570 34 30 35 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74  40587.5 + sNow.t
e9580 76 5f 73 65 63 2f 38 36 34 30 30 2e 30 20 2b 20  v_sec/86400.0 + 
e9590 73 4e 6f 77 2e 74 76 5f 75 73 65 63 2f 38 36 34  sNow.tv_usec/864
e95a0 30 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 6e 64  00000000.0;.#end
e95b0 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
e95c0 45 5f 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c  E_TEST.  if( sql
e95d0 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d  ite3_current_tim
e95e0 65 20 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20  e ){.    *prNow 
e95f0 3d 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e  = sqlite3_curren
e9600 74 5f 74 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b  t_time/86400.0 +
e9610 20 32 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a   2440587.5;.  }.
e9620 23 65 6e 64 69 66 0a 20 20 55 4e 55 53 45 44 5f  #endif.  UNUSED_
e9630 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
e9640 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  d);.  return 0;.
e9650 7d 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 61 64 64 65  }../*.** We adde
e9660 64 20 74 68 65 20 78 47 65 74 4c 61 73 74 45 72  d the xGetLastEr
e9670 72 6f 72 28 29 20 6d 65 74 68 6f 64 20 77 69 74  ror() method wit
e9680 68 20 74 68 65 20 69 6e 74 65 6e 74 69 6f 6e 20  h the intention 
e9690 6f 66 20 70 72 6f 76 69 64 69 6e 67 0a 2a 2a 20  of providing.** 
e96a0 62 65 74 74 65 72 20 6c 6f 77 2d 6c 65 76 65 6c  better low-level
e96b0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20   error messages 
e96c0 77 68 65 6e 20 6f 70 65 72 61 74 69 6e 67 2d 73  when operating-s
e96d0 79 73 74 65 6d 20 70 72 6f 62 6c 65 6d 73 20 63  ystem problems c
e96e0 6f 6d 65 20 75 70 0a 2a 2a 20 64 75 72 69 6e 67  ome up.** during
e96f0 20 53 51 4c 69 74 65 20 6f 70 65 72 61 74 69 6f   SQLite operatio
e9700 6e 2e 20 20 42 75 74 20 73 6f 20 66 61 72 2c 20  n.  But so far, 
e9710 6e 6f 6e 65 20 6f 66 20 74 68 61 74 20 68 61 73  none of that has
e9720 20 62 65 65 6e 20 69 6d 70 6c 65 6d 65 6e 74 65   been implemente
e9730 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 72 65  d.** in the core
e9740 2e 20 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69  .  So this routi
e9750 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c  ne is never call
e9760 65 64 2e 20 20 46 6f 72 20 6e 6f 77 2c 20 69 74  ed.  For now, it
e9770 20 69 73 20 6d 65 72 65 6c 79 0a 2a 2a 20 61 20   is merely.** a 
e9780 70 6c 61 63 65 2d 68 6f 6c 64 65 72 2e 0a 2a 2f  place-holder..*/
e9790 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
e97a0 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c  GetLastError(sql
e97b0 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65  ite3_vfs *NotUse
e97c0 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 2c  d, int NotUsed2,
e97d0 20 63 68 61 72 20 2a 4e 6f 74 55 73 65 64 33 29   char *NotUsed3)
e97e0 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
e97f0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
e9800 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
e9810 52 28 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 55  R(NotUsed2);.  U
e9820 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
e9830 4e 6f 74 55 73 65 64 33 29 3b 0a 20 20 72 65 74  NotUsed3);.  ret
e9840 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 2a  urn 0;.}../*.***
e9850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9860 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c  ***** End of sql
e9870 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f 64 73  ite3_vfs methods
e9880 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
e9890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a  ************.***
e98a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e98b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e98c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e98d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e98e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
e98f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
e9940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9950 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 50  ******** Begin P
e9960 72 6f 78 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a  roxy Locking ***
e9970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
e9990 0a 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e  .** Proxy lockin
e99a0 67 20 69 73 20 61 20 22 75 62 65 72 2d 6c 6f 63  g is a "uber-loc
e99b0 6b 69 6e 67 2d 6d 65 74 68 6f 64 22 20 69 6e 20  king-method" in 
e99c0 74 68 69 73 20 73 65 6e 73 65 3a 20 20 49 74 20  this sense:  It 
e99d0 75 73 65 73 20 74 68 65 0a 2a 2a 20 6f 74 68 65  uses the.** othe
e99e0 72 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64  r locking method
e99f0 73 20 6f 6e 20 73 65 63 6f 6e 64 61 72 79 20 6c  s on secondary l
e9a00 6f 63 6b 20 66 69 6c 65 73 2e 20 20 50 72 6f 78  ock files.  Prox
e9a10 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 0a 2a  y locking is a.*
e9a20 2a 20 6d 65 74 61 2d 6c 61 79 65 72 20 6f 76 65  * meta-layer ove
e9a30 72 20 74 6f 70 20 6f 66 20 74 68 65 20 70 72 69  r top of the pri
e9a40 6d 69 74 69 76 65 20 6c 6f 63 6b 69 6e 67 20 69  mitive locking i
e9a50 6d 70 6c 65 6d 65 6e 74 65 64 20 61 62 6f 76 65  mplemented above
e9a60 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 69 73 20 72  .  For.** this r
e9a70 65 61 73 6f 6e 2c 20 74 68 65 20 64 69 76 69 73  eason, the divis
e9a80 69 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ion that impleme
e9a90 6e 74 73 20 6f 66 20 70 72 6f 78 79 20 6c 6f 63  nts of proxy loc
e9aa0 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72 65 64  king is deferred
e9ab0 0a 2a 2a 20 75 6e 74 69 6c 20 6c 61 74 65 20 69  .** until late i
e9ac0 6e 20 74 68 65 20 66 69 6c 65 20 28 68 65 72 65  n the file (here
e9ad0 29 20 61 66 74 65 72 20 61 6c 6c 20 6f 66 20 74  ) after all of t
e9ae0 68 65 20 6f 74 68 65 72 20 49 2f 4f 20 6d 65 74  he other I/O met
e9af0 68 6f 64 73 20 68 61 76 65 0a 2a 2a 20 62 65 65  hods have.** bee
e9b00 6e 20 64 65 66 69 6e 65 64 20 2d 20 73 6f 20 74  n defined - so t
e9b10 68 61 74 20 74 68 65 20 70 72 69 6d 69 74 69 76  hat the primitiv
e9b20 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64  e locking method
e9b30 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 0a  s are available.
e9b40 2a 2a 20 61 73 20 73 65 72 76 69 63 65 73 20 74  ** as services t
e9b50 6f 20 68 65 6c 70 20 77 69 74 68 20 74 68 65 20  o help with the 
e9b60 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
e9b70 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e  f proxy locking.
e9b80 0a 2a 2a 0a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  .**.****.**.** T
e9b90 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69  he default locki
e9ba0 6e 67 20 73 63 68 65 6d 65 73 20 69 6e 20 53 51  ng schemes in SQ
e9bb0 4c 69 74 65 20 75 73 65 20 62 79 74 65 2d 72 61  Lite use byte-ra
e9bc0 6e 67 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65  nge locks on the
e9bd0 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c  .** database fil
e9be0 65 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 65 20  e to coordinate 
e9bf0 73 61 66 65 2c 20 63 6f 6e 63 75 72 72 65 6e 74  safe, concurrent
e9c00 20 61 63 63 65 73 73 20 62 79 20 6d 75 6c 74 69   access by multi
e9c10 70 6c 65 20 72 65 61 64 65 72 73 0a 2a 2a 20 61  ple readers.** a
e9c20 6e 64 20 77 72 69 74 65 72 73 20 5b 68 74 74 70  nd writers [http
e9c30 3a 2f 2f 73 71 6c 69 74 65 2e 6f 72 67 2f 6c 6f  ://sqlite.org/lo
e9c40 63 6b 69 6e 67 76 33 2e 68 74 6d 6c 5d 2e 20 20  ckingv3.html].  
e9c50 54 68 65 20 66 69 76 65 20 66 69 6c 65 20 6c 6f  The five file lo
e9c60 63 6b 69 6e 67 0a 2a 2a 20 73 74 61 74 65 73 20  cking.** states 
e9c70 28 55 4e 4c 4f 43 4b 45 44 2c 20 50 45 4e 44 49  (UNLOCKED, PENDI
e9c80 4e 47 2c 20 53 48 41 52 45 44 2c 20 52 45 53 45  NG, SHARED, RESE
e9c90 52 56 45 44 2c 20 45 58 43 4c 55 53 49 56 45 29  RVED, EXCLUSIVE)
e9ca0 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64   are implemented
e9cb0 0a 2a 2a 20 61 73 20 50 4f 53 49 58 20 72 65 61  .** as POSIX rea
e9cc0 64 20 26 20 77 72 69 74 65 20 6c 6f 63 6b 73 20  d & write locks 
e9cd0 6f 76 65 72 20 66 69 78 65 64 20 73 65 74 20 6f  over fixed set o
e9ce0 66 20 6c 6f 63 61 74 69 6f 6e 73 20 28 76 69 61  f locations (via
e9cf0 20 66 73 63 74 6c 29 2c 0a 2a 2a 20 6f 6e 20 41   fsctl),.** on A
e9d00 46 50 20 61 6e 64 20 53 4d 42 20 6f 6e 6c 79 20  FP and SMB only 
e9d10 65 78 63 6c 75 73 69 76 65 20 62 79 74 65 2d 72  exclusive byte-r
e9d20 61 6e 67 65 20 6c 6f 63 6b 73 20 61 72 65 20 61  ange locks are a
e9d30 76 61 69 6c 61 62 6c 65 20 76 69 61 20 66 73 63  vailable via fsc
e9d40 74 6c 0a 2a 2a 20 77 69 74 68 20 5f 49 4f 57 52  tl.** with _IOWR
e9d50 28 27 7a 27 2c 20 32 33 2c 20 73 74 72 75 63 74  ('z', 23, struct
e9d60 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42   ByteRangeLockPB
e9d70 32 29 20 74 6f 20 74 72 61 63 6b 20 74 68 65 20  2) to track the 
e9d80 73 61 6d 65 20 35 20 73 74 61 74 65 73 2e 0a 2a  same 5 states..*
e9d90 2a 20 54 6f 20 73 69 6d 75 6c 61 74 65 20 61 20  * To simulate a 
e9da0 46 5f 52 44 4c 43 4b 20 6f 6e 20 74 68 65 20 73  F_RDLCK on the s
e9db0 68 61 72 65 64 20 72 61 6e 67 65 2c 20 6f 6e 20  hared range, on 
e9dc0 41 46 50 20 61 20 72 61 6e 64 6f 6d 6c 79 20 73  AFP a randomly s
e9dd0 65 6c 65 63 74 65 64 0a 2a 2a 20 61 64 64 72 65  elected.** addre
e9de0 73 73 20 69 6e 20 74 68 65 20 73 68 61 72 65 64  ss in the shared
e9df0 20 72 61 6e 67 65 20 69 73 20 74 61 6b 65 6e 20   range is taken 
e9e00 66 6f 72 20 61 20 53 48 41 52 45 44 20 6c 6f 63  for a SHARED loc
e9e10 6b 2c 20 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a  k, the entire.**
e9e20 20 73 68 61 72 65 64 20 72 61 6e 67 65 20 69 73   shared range is
e9e30 20 74 61 6b 65 6e 20 66 6f 72 20 61 6e 20 45 58   taken for an EX
e9e40 43 4c 55 53 49 56 45 20 6c 6f 63 6b 29 3a 0a 2a  CLUSIVE lock):.*
e9e50 2a 0a 2a 2a 20 20 20 20 20 20 50 45 4e 44 49 4e  *.**      PENDIN
e9e60 47 5f 42 59 54 45 20 20 20 20 20 20 20 20 30 78  G_BYTE        0x
e9e70 34 30 30 30 30 30 30 30 09 09 20 20 20 09 0a 2a  40000000..   ..*
e9e80 2a 20 20 20 20 20 20 52 45 53 45 52 56 45 44 5f  *      RESERVED_
e9e90 42 59 54 45 20 20 20 20 20 20 20 30 78 34 30 30  BYTE       0x400
e9ea0 30 30 30 30 31 0a 2a 2a 20 20 20 20 20 20 53 48  00001.**      SH
e9eb0 41 52 45 44 5f 52 41 4e 47 45 20 20 20 20 20 20  ARED_RANGE      
e9ec0 20 20 30 78 34 30 30 30 30 30 30 32 20 2d 3e 20    0x40000002 -> 
e9ed0 30 78 34 30 30 30 30 32 30 30 0a 2a 2a 0a 2a 2a  0x40000200.**.**
e9ee0 20 54 68 69 73 20 77 6f 72 6b 73 20 77 65 6c 6c   This works well
e9ef0 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 66 69   on the local fi
e9f00 6c 65 20 73 79 73 74 65 6d 2c 20 62 75 74 20 73  le system, but s
e9f10 68 6f 77 73 20 61 20 6e 65 61 72 6c 79 20 31 30  hows a nearly 10
e9f20 30 78 0a 2a 2a 20 73 6c 6f 77 64 6f 77 6e 20 69  0x.** slowdown i
e9f30 6e 20 72 65 61 64 20 70 65 72 66 6f 72 6d 61 6e  n read performan
e9f40 63 65 20 6f 6e 20 41 46 50 20 62 65 63 61 75 73  ce on AFP becaus
e9f50 65 20 74 68 65 20 41 46 50 20 63 6c 69 65 6e 74  e the AFP client
e9f60 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 74 68 65   disables.** the
e9f70 20 72 65 61 64 20 63 61 63 68 65 20 77 68 65 6e   read cache when
e9f80 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b   byte-range lock
e9f90 73 20 61 72 65 20 70 72 65 73 65 6e 74 2e 20 20  s are present.  
e9fa0 45 6e 61 62 6c 69 6e 67 20 74 68 65 20 72 65 61  Enabling the rea
e9fb0 64 0a 2a 2a 20 63 61 63 68 65 20 65 78 70 6f 73  d.** cache expos
e9fc0 65 73 20 61 20 63 61 63 68 65 20 63 6f 68 65 72  es a cache coher
e9fd0 65 6e 63 79 20 70 72 6f 62 6c 65 6d 20 74 68 61  ency problem tha
e9fe0 74 20 69 73 20 70 72 65 73 65 6e 74 20 6f 6e 20  t is present on 
e9ff0 61 6c 6c 20 4f 53 20 58 0a 2a 2a 20 73 75 70 70  all OS X.** supp
ea000 6f 72 74 65 64 20 6e 65 74 77 6f 72 6b 20 66 69  orted network fi
ea010 6c 65 20 73 79 73 74 65 6d 73 2e 20 20 4e 46 53  le systems.  NFS
ea020 20 61 6e 64 20 41 46 50 20 62 6f 74 68 20 6f 62   and AFP both ob
ea030 73 65 72 76 65 20 74 68 65 0a 2a 2a 20 63 6c 6f  serve the.** clo
ea040 73 65 2d 74 6f 2d 6f 70 65 6e 20 73 65 6d 61 6e  se-to-open seman
ea050 74 69 63 73 20 66 6f 72 20 65 6e 73 75 72 69 6e  tics for ensurin
ea060 67 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63  g cache coherenc
ea070 79 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 6e 66 73  y.** [http://nfs
ea080 2e 73 6f 75 72 63 65 66 6f 72 67 65 2e 6e 65 74  .sourceforge.net
ea090 2f 23 66 61 71 5f 61 38 5d 2c 20 77 68 69 63 68  /#faq_a8], which
ea0a0 20 64 6f 65 73 20 6e 6f 74 20 65 66 66 65 63 74   does not effect
ea0b0 69 76 65 6c 79 0a 2a 2a 20 61 64 64 72 65 73 73  ively.** address
ea0c0 20 74 68 65 20 72 65 71 75 69 72 65 6d 65 6e 74   the requirement
ea0d0 73 20 66 6f 72 20 63 6f 6e 63 75 72 72 65 6e 74  s for concurrent
ea0e0 20 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73   database access
ea0f0 20 62 79 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20   by multiple.** 
ea100 72 65 61 64 65 72 73 20 61 6e 64 20 77 72 69 74  readers and writ
ea110 65 72 73 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 77  ers.** [http://w
ea120 77 77 2e 6e 61 62 62 6c 65 2e 63 6f 6d 2f 53 51  ww.nabble.com/SQ
ea130 4c 69 74 65 2d 6f 6e 2d 4e 46 53 2d 63 61 63 68  Lite-on-NFS-cach
ea140 65 2d 63 6f 68 65 72 65 6e 63 79 2d 74 64 31 35  e-coherency-td15
ea150 36 35 35 37 30 31 2e 68 74 6d 6c 5d 2e 0a 2a 2a  655701.html]..**
ea160 0a 2a 2a 20 54 6f 20 61 64 64 72 65 73 73 20 74  .** To address t
ea170 68 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 61  he performance a
ea180 6e 64 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e  nd cache coheren
ea190 63 79 20 69 73 73 75 65 73 2c 20 70 72 6f 78 79  cy issues, proxy
ea1a0 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a   file locking.**
ea1b0 20 63 68 61 6e 67 65 73 20 74 68 65 20 77 61 79   changes the way
ea1c0 20 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73   database access
ea1d0 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62   is controlled b
ea1e0 79 20 6c 69 6d 69 74 69 6e 67 20 61 63 63 65 73  y limiting acces
ea1f0 73 20 74 6f 20 61 0a 2a 2a 20 73 69 6e 67 6c 65  s to a.** single
ea200 20 68 6f 73 74 20 61 74 20 61 20 74 69 6d 65 20   host at a time 
ea210 61 6e 64 20 6d 6f 76 69 6e 67 20 66 69 6c 65 20  and moving file 
ea220 6c 6f 63 6b 73 20 6f 66 66 20 6f 66 20 74 68 65  locks off of the
ea230 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a   database file.*
ea240 2a 20 61 6e 64 20 6f 6e 74 6f 20 61 20 70 72 6f  * and onto a pro
ea250 78 79 20 66 69 6c 65 20 6f 6e 20 74 68 65 20 6c  xy file on the l
ea260 6f 63 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d  ocal file system
ea270 2e 20 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 69  .  .**.**.** Usi
ea280 6e 67 20 70 72 6f 78 79 20 6c 6f 63 6b 73 0a 2a  ng proxy locks.*
ea290 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  * --------------
ea2a0 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 43 20 41 50 49 73  ---.**.** C APIs
ea2b0 0a 2a 2a 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f  .**.**  sqlite3_
ea2c0 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c  file_control(db,
ea2d0 20 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f   dbname, SQLITE_
ea2e0 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c  SET_LOCKPROXYFIL
ea2f0 45 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  E,.**           
ea300 20 20 20 20 20 20 20 20 20 20 20 20 3c 70 72 6f              <pro
ea310 78 79 5f 70 61 74 68 3e 20 7c 20 22 3a 61 75 74  xy_path> | ":aut
ea320 6f 3a 22 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65  o:");.**  sqlite
ea330 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64  3_file_control(d
ea340 62 2c 20 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54  b, dbname, SQLIT
ea350 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46  E_GET_LOCKPROXYF
ea360 49 4c 45 2c 20 26 3c 70 72 6f 78 79 5f 70 61 74  ILE, &<proxy_pat
ea370 68 3e 29 3b 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 53 51  h>);.**.**.** SQ
ea380 4c 20 70 72 61 67 6d 61 73 0a 2a 2a 0a 2a 2a 20  L pragmas.**.** 
ea390 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
ea3a0 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69  e.]lock_proxy_fi
ea3b0 6c 65 3d 3c 70 72 6f 78 79 5f 70 61 74 68 3e 20  le=<proxy_path> 
ea3c0 7c 20 3a 61 75 74 6f 3a 0a 2a 2a 20 20 50 52 41  | :auto:.**  PRA
ea3d0 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c  GMA [database.]l
ea3e0 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 0a 2a  ock_proxy_file.*
ea3f0 2a 0a 2a 2a 20 53 70 65 63 69 66 79 69 6e 67 20  *.** Specifying 
ea400 22 3a 61 75 74 6f 3a 22 20 6d 65 61 6e 73 20 74  ":auto:" means t
ea410 68 61 74 20 69 66 20 74 68 65 72 65 20 69 73 20  hat if there is 
ea420 61 20 63 6f 6e 63 68 20 66 69 6c 65 20 77 69 74  a conch file wit
ea430 68 20 61 20 6d 61 74 63 68 69 6e 67 0a 2a 2a 20  h a matching.** 
ea440 68 6f 73 74 20 49 44 20 69 6e 20 69 74 2c 20 74  host ID in it, t
ea450 68 65 20 70 72 6f 78 79 20 70 61 74 68 20 69 6e  he proxy path in
ea460 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20   the conch file 
ea470 77 69 6c 6c 20 62 65 20 75 73 65 64 2c 20 6f 74  will be used, ot
ea480 68 65 72 77 69 73 65 0a 2a 2a 20 61 20 70 72 6f  herwise.** a pro
ea490 78 79 20 70 61 74 68 20 62 61 73 65 64 20 6f 6e  xy path based on
ea4a0 20 74 68 65 20 75 73 65 72 27 73 20 74 65 6d 70   the user's temp
ea4b0 20 64 69 72 0a 2a 2a 20 28 76 69 61 20 63 6f 6e   dir.** (via con
ea4c0 66 73 74 72 28 5f 43 53 5f 44 41 52 57 49 4e 5f  fstr(_CS_DARWIN_
ea4d0 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 2c 2e 2e  USER_TEMP_DIR,..
ea4e0 2e 29 29 20 77 69 6c 6c 20 62 65 20 75 73 65 64  .)) will be used
ea4f0 20 61 6e 64 20 74 68 65 0a 2a 2a 20 61 63 74 75   and the.** actu
ea500 61 6c 20 70 72 6f 78 79 20 66 69 6c 65 20 6e 61  al proxy file na
ea510 6d 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20  me is generated 
ea520 66 72 6f 6d 20 74 68 65 20 6e 61 6d 65 20 61 6e  from the name an
ea530 64 20 70 61 74 68 20 6f 66 20 74 68 65 0a 2a 2a  d path of the.**
ea540 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
ea550 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a   For example:.**
ea560 0a 2a 2a 20 20 20 20 20 20 20 46 6f 72 20 64 61  .**       For da
ea570 74 61 62 61 73 65 20 70 61 74 68 20 22 2f 55 73  tabase path "/Us
ea580 65 72 73 2f 6d 65 2f 66 6f 6f 2e 64 62 22 20 0a  ers/me/foo.db" .
ea590 2a 2a 20 20 20 20 20 20 20 54 68 65 20 6c 6f 63  **       The loc
ea5a0 6b 20 70 61 74 68 20 77 69 6c 6c 20 62 65 20 22  k path will be "
ea5b0 3c 74 6d 70 64 69 72 3e 2f 73 71 6c 69 74 65 70  <tmpdir>/sqlitep
ea5c0 6c 6f 63 6b 73 2f 5f 55 73 65 72 73 5f 6d 65 5f  locks/_Users_me_
ea5d0 66 6f 6f 2e 64 62 3a 61 75 74 6f 3a 22 29 0a 2a  foo.db:auto:").*
ea5e0 2a 0a 2a 2a 20 4f 6e 63 65 20 61 20 6c 6f 63 6b  *.** Once a lock
ea5f0 20 70 72 6f 78 79 20 69 73 20 63 6f 6e 66 69 67   proxy is config
ea600 75 72 65 64 20 66 6f 72 20 61 20 64 61 74 61 62  ured for a datab
ea610 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20  ase connection, 
ea620 69 74 20 63 61 6e 20 6e 6f 74 0a 2a 2a 20 62 65  it can not.** be
ea630 20 72 65 6d 6f 76 65 64 2c 20 68 6f 77 65 76 65   removed, howeve
ea640 72 20 69 74 20 6d 61 79 20 62 65 20 73 77 69 74  r it may be swit
ea650 63 68 65 64 20 74 6f 20 61 20 64 69 66 66 65 72  ched to a differ
ea660 65 6e 74 20 70 72 6f 78 79 20 70 61 74 68 20 76  ent proxy path v
ea670 69 61 0a 2a 2a 20 74 68 65 20 61 62 6f 76 65 20  ia.** the above 
ea680 41 50 49 73 20 28 61 73 73 75 6d 69 6e 67 20 74  APIs (assuming t
ea690 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 69 73  he conch file is
ea6a0 20 6e 6f 74 20 62 65 69 6e 67 20 68 65 6c 64 20   not being held 
ea6b0 62 79 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 63 6f  by another.** co
ea6c0 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 70 72 6f 63  nnection or proc
ea6d0 65 73 73 29 2e 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20  ess). .**.**.** 
ea6e0 48 6f 77 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e  How proxy lockin
ea6f0 67 20 77 6f 72 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d  g works.** -----
ea700 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ea710 2d 2d 0a 2a 2a 0a 2a 2a 20 50 72 6f 78 79 20 66  --.**.** Proxy f
ea720 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 72 65 6c 69  ile locking reli
ea730 65 73 20 70 72 69 6d 61 72 69 6c 79 20 6f 6e 20  es primarily on 
ea740 74 77 6f 20 6e 65 77 20 73 75 70 70 6f 72 74 69  two new supporti
ea750 6e 67 20 66 69 6c 65 73 3a 20 0a 2a 2a 0a 2a 2a  ng files: .**.**
ea760 20 20 20 2a 20 20 63 6f 6e 63 68 20 66 69 6c 65     *  conch file
ea770 20 74 6f 20 6c 69 6d 69 74 20 61 63 63 65 73 73   to limit access
ea780 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
ea790 20 66 69 6c 65 20 74 6f 20 61 20 73 69 6e 67 6c   file to a singl
ea7a0 65 20 68 6f 73 74 0a 2a 2a 20 20 20 20 20 20 61  e host.**      a
ea7b0 74 20 61 20 74 69 6d 65 0a 2a 2a 0a 2a 2a 20 20  t a time.**.**  
ea7c0 20 2a 20 20 70 72 6f 78 79 20 66 69 6c 65 20 74   *  proxy file t
ea7d0 6f 20 61 63 74 20 61 73 20 61 20 70 72 6f 78 79  o act as a proxy
ea7e0 20 66 6f 72 20 74 68 65 20 61 64 76 69 73 6f 72   for the advisor
ea7f0 79 20 6c 6f 63 6b 73 20 6e 6f 72 6d 61 6c 6c 79  y locks normally
ea800 0a 2a 2a 20 20 20 20 20 20 74 61 6b 65 6e 20 6f  .**      taken o
ea810 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a  n the database.*
ea820 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 63 68 20 66  *.** The conch f
ea830 69 6c 65 20 2d 20 74 6f 20 75 73 65 20 61 20 70  ile - to use a p
ea840 72 6f 78 79 20 66 69 6c 65 2c 20 73 71 6c 69 74  roxy file, sqlit
ea850 65 20 6d 75 73 74 20 66 69 72 73 74 20 22 68 6f  e must first "ho
ea860 6c 64 20 74 68 65 20 63 6f 6e 63 68 22 0a 2a 2a  ld the conch".**
ea870 20 62 79 20 74 61 6b 69 6e 67 20 61 6e 20 73 71   by taking an sq
ea880 6c 69 74 65 2d 73 74 79 6c 65 20 73 68 61 72 65  lite-style share
ea890 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 63 6f  d lock on the co
ea8a0 6e 63 68 20 66 69 6c 65 2c 20 72 65 61 64 69 6e  nch file, readin
ea8b0 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74  g the.** content
ea8c0 73 20 61 6e 64 20 63 6f 6d 70 61 72 69 6e 67 20  s and comparing 
ea8d0 74 68 65 20 68 6f 73 74 27 73 20 75 6e 69 71 75  the host's uniqu
ea8e0 65 20 68 6f 73 74 20 49 44 20 28 73 65 65 20 62  e host ID (see b
ea8f0 65 6c 6f 77 29 20 61 6e 64 20 6c 6f 63 6b 0a 2a  elow) and lock.*
ea900 2a 20 70 72 6f 78 79 20 70 61 74 68 20 61 67 61  * proxy path aga
ea910 69 6e 73 74 20 74 68 65 20 76 61 6c 75 65 73 20  inst the values 
ea920 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 6f  stored in the co
ea930 6e 63 68 2e 20 20 54 68 65 20 63 6f 6e 63 68 20  nch.  The conch 
ea940 66 69 6c 65 20 69 73 0a 2a 2a 20 73 74 6f 72 65  file is.** store
ea950 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 69  d in the same di
ea960 72 65 63 74 6f 72 79 20 61 73 20 74 68 65 20 64  rectory as the d
ea970 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
ea980 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 0a 2a   the file name.*
ea990 2a 20 69 73 20 70 61 74 74 65 72 6e 65 64 20 61  * is patterned a
ea9a0 66 74 65 72 20 74 68 65 20 64 61 74 61 62 61 73  fter the databas
ea9b0 65 20 66 69 6c 65 20 6e 61 6d 65 20 61 73 20 22  e file name as "
ea9c0 2e 3c 64 61 74 61 62 61 73 65 6e 61 6d 65 3e 2d  .<databasename>-
ea9d0 63 6f 6e 63 68 22 2e 0a 2a 2a 20 49 66 20 74 68  conch"..** If th
ea9e0 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 64 6f 65  e conch file doe
ea9f0 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 6f 72 20  s not exist, or 
eaa00 69 74 27 73 20 63 6f 6e 74 65 6e 74 73 20 64 6f  it's contents do
eaa10 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 0a 2a   not match the.*
eaa20 2a 20 68 6f 73 74 20 49 44 20 61 6e 64 2f 6f 72  * host ID and/or
eaa30 20 70 72 6f 78 79 20 70 61 74 68 2c 20 74 68 65   proxy path, the
eaa40 6e 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 65 73  n the lock is es
eaa50 63 61 6c 61 74 65 64 20 74 6f 20 61 6e 20 65 78  calated to an ex
eaa60 63 6c 75 73 69 76 65 0a 2a 2a 20 6c 6f 63 6b 20  clusive.** lock 
eaa70 61 6e 64 20 74 68 65 20 63 6f 6e 63 68 20 66 69  and the conch fi
eaa80 6c 65 20 63 6f 6e 74 65 6e 74 73 20 69 73 20 75  le contents is u
eaa90 70 64 61 74 65 64 20 77 69 74 68 20 74 68 65 20  pdated with the 
eaaa0 68 6f 73 74 20 49 44 20 61 6e 64 20 70 72 6f 78  host ID and prox
eaab0 79 0a 2a 2a 20 70 61 74 68 20 61 6e 64 20 74 68  y.** path and th
eaac0 65 20 6c 6f 63 6b 20 69 73 20 64 6f 77 6e 67 72  e lock is downgr
eaad0 61 64 65 64 20 74 6f 20 61 20 73 68 61 72 65 64  aded to a shared
eaae0 20 6c 6f 63 6b 20 61 67 61 69 6e 2e 20 20 49 66   lock again.  If
eaaf0 20 74 68 65 20 63 6f 6e 63 68 0a 2a 2a 20 69 73   the conch.** is
eab00 20 68 65 6c 64 20 62 79 20 61 6e 6f 74 68 65 72   held by another
eab10 20 70 72 6f 63 65 73 73 20 28 77 69 74 68 20 61   process (with a
eab20 20 73 68 61 72 65 64 20 6c 6f 63 6b 29 2c 20 74   shared lock), t
eab30 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  he exclusive loc
eab40 6b 0a 2a 2a 20 77 69 6c 6c 20 66 61 69 6c 20 61  k.** will fail a
eab50 6e 64 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69  nd SQLITE_BUSY i
eab60 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
eab70 2a 20 54 68 65 20 70 72 6f 78 79 20 66 69 6c 65  * The proxy file
eab80 20 2d 20 61 20 73 69 6e 67 6c 65 2d 62 79 74 65   - a single-byte
eab90 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20 61   file used for a
eaba0 6c 6c 20 61 64 76 69 73 6f 72 79 20 66 69 6c 65  ll advisory file
eabb0 20 6c 6f 63 6b 73 0a 2a 2a 20 6e 6f 72 6d 61 6c   locks.** normal
eabc0 6c 79 20 74 61 6b 65 6e 20 6f 6e 20 74 68 65 20  ly taken on the 
eabd0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
eabe0 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 66 6f 72   This allows for
eabf0 20 73 61 66 65 20 73 68 61 72 69 6e 67 0a 2a 2a   safe sharing.**
eac00 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
eac10 20 66 69 6c 65 20 66 6f 72 20 6d 75 6c 74 69 70   file for multip
eac20 6c 65 20 72 65 61 64 65 72 73 20 61 6e 64 20 77  le readers and w
eac30 72 69 74 65 72 73 20 6f 6e 20 74 68 65 20 73 61  riters on the sa
eac40 6d 65 0a 2a 2a 20 68 6f 73 74 20 28 74 68 65 20  me.** host (the 
eac50 63 6f 6e 63 68 20 65 6e 73 75 72 65 73 20 74 68  conch ensures th
eac60 61 74 20 74 68 65 79 20 61 6c 6c 20 75 73 65 20  at they all use 
eac70 74 68 65 20 73 61 6d 65 20 6c 6f 63 61 6c 20 6c  the same local l
eac80 6f 63 6b 20 66 69 6c 65 29 2e 0a 2a 2a 0a 2a 2a  ock file)..**.**
eac90 20 54 68 65 72 65 20 69 73 20 61 20 74 68 69 72   There is a thir
eaca0 64 20 66 69 6c 65 20 2d 20 74 68 65 20 68 6f 73  d file - the hos
eacb0 74 20 49 44 20 66 69 6c 65 20 2d 20 75 73 65 64  t ID file - used
eacc0 20 61 73 20 61 20 70 65 72 73 69 73 74 65 6e 74   as a persistent
eacd0 20 72 65 63 6f 72 64 0a 2a 2a 20 6f 66 20 61 20   record.** of a 
eace0 75 6e 69 71 75 65 20 69 64 65 6e 74 69 66 69 65  unique identifie
eacf0 72 20 66 6f 72 20 74 68 65 20 68 6f 73 74 2c 20  r for the host, 
ead00 61 20 31 32 38 2d 62 79 74 65 20 75 6e 69 71 75  a 128-byte uniqu
ead10 65 20 68 6f 73 74 20 69 64 20 66 69 6c 65 0a 2a  e host id file.*
ead20 2a 20 69 6e 20 74 68 65 20 70 61 74 68 20 64 65  * in the path de
ead30 66 69 6e 65 64 20 62 79 20 74 68 65 20 48 4f 53  fined by the HOS
ead40 54 49 44 50 41 54 48 20 6d 61 63 72 6f 20 28 64  TIDPATH macro (d
ead50 65 66 61 75 6c 74 20 76 61 6c 75 65 20 69 73 0a  efault value is.
ead60 2a 2a 20 2f 4c 69 62 72 61 72 79 2f 43 61 63 68  ** /Library/Cach
ead70 65 73 2f 2e 63 6f 6d 2e 61 70 70 6c 65 2e 73 71  es/.com.apple.sq
ead80 6c 69 74 65 43 6f 6e 63 68 48 6f 73 74 49 64 29  liteConchHostId)
ead90 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 65 73 74 69  ..**.** Requesti
eada0 6e 67 20 74 68 65 20 6c 6f 63 6b 20 70 72 6f 78  ng the lock prox
eadb0 79 20 64 6f 65 73 20 6e 6f 74 20 69 6d 6d 65 64  y does not immed
eadc0 69 61 74 65 6c 79 20 74 61 6b 65 20 74 68 65 20  iately take the 
eadd0 63 6f 6e 63 68 2c 20 69 74 20 69 73 0a 2a 2a 20  conch, it is.** 
eade0 6f 6e 6c 79 20 74 61 6b 65 6e 20 77 68 65 6e 20  only taken when 
eadf0 74 68 65 20 66 69 72 73 74 20 72 65 71 75 65 73  the first reques
eae00 74 20 74 6f 20 6c 6f 63 6b 20 64 61 74 61 62 61  t to lock databa
eae10 73 65 20 66 69 6c 65 20 69 73 20 6d 61 64 65 2e  se file is made.
eae20 20 20 0a 2a 2a 20 54 68 69 73 20 6d 61 74 63 68    .** This match
eae30 65 73 20 74 68 65 20 73 65 6d 61 6e 74 69 63 73  es the semantics
eae40 20 6f 66 20 74 68 65 20 74 72 61 64 69 74 69 6f   of the traditio
eae50 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20 62 65 68 61  nal locking beha
eae60 76 69 6f 72 2c 20 77 68 65 72 65 0a 2a 2a 20 6f  vior, where.** o
eae70 70 65 6e 69 6e 67 20 61 20 63 6f 6e 6e 65 63 74  pening a connect
eae80 69 6f 6e 20 74 6f 20 61 20 64 61 74 61 62 61 73  ion to a databas
eae90 65 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20  e file does not 
eaea0 74 61 6b 65 20 61 20 6c 6f 63 6b 20 6f 6e 20 69  take a lock on i
eaeb0 74 2e 0a 2a 2a 20 54 68 65 20 73 68 61 72 65 64  t..** The shared
eaec0 20 6c 6f 63 6b 20 61 6e 64 20 61 6e 20 6f 70 65   lock and an ope
eaed0 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
eaee0 72 20 61 72 65 20 6d 61 69 6e 74 61 69 6e 65 64  r are maintained
eaef0 20 75 6e 74 69 6c 20 0a 2a 2a 20 74 68 65 20 63   until .** the c
eaf00 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 74 68 65  onnection to the
eaf10 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6c 6f   database is clo
eaf20 73 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20  sed. .**.** The 
eaf30 70 72 6f 78 79 20 66 69 6c 65 20 61 6e 64 20 74  proxy file and t
eaf40 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61 72 65  he lock file are
eaf50 20 6e 65 76 65 72 20 64 65 6c 65 74 65 64 20 73   never deleted s
eaf60 6f 20 74 68 65 79 20 6f 6e 6c 79 20 6e 65 65 64  o they only need
eaf70 0a 2a 2a 20 74 6f 20 62 65 20 63 72 65 61 74 65  .** to be create
eaf80 64 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65  d the first time
eaf90 20 74 68 65 79 20 61 72 65 20 75 73 65 64 2e 0a   they are used..
eafa0 2a 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 61 74  **.** Configurat
eafb0 69 6f 6e 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 2d  ion options.** -
eafc0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
eafd0 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20 53 51 4c 49  ----.**.**  SQLI
eafe0 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f  TE_PREFER_PROXY_
eaff0 4c 4f 43 4b 49 4e 47 0a 2a 2a 0a 2a 2a 20 20 20  LOCKING.**.**   
eb000 20 20 20 20 44 61 74 61 62 61 73 65 20 66 69 6c      Database fil
eb010 65 73 20 61 63 63 65 73 73 65 64 20 6f 6e 20 6e  es accessed on n
eb020 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79  on-local file sy
eb030 73 74 65 6d 73 20 61 72 65 0a 2a 2a 20 20 20 20  stems are.**    
eb040 20 20 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79     automatically
eb050 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20   configured for 
eb060 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2c 20 6c  proxy locking, l
eb070 6f 63 6b 20 66 69 6c 65 73 20 61 72 65 0a 2a 2a  ock files are.**
eb080 20 20 20 20 20 20 20 6e 61 6d 65 64 20 61 75 74         named aut
eb090 6f 6d 61 74 69 63 61 6c 6c 79 20 75 73 69 6e 67  omatically using
eb0a0 20 74 68 65 20 73 61 6d 65 20 6c 6f 67 69 63 20   the same logic 
eb0b0 61 73 0a 2a 2a 20 20 20 20 20 20 20 50 52 41 47  as.**       PRAG
eb0c0 4d 41 20 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69  MA lock_proxy_fi
eb0d0 6c 65 3d 22 3a 61 75 74 6f 3a 22 0a 2a 2a 20 20  le=":auto:".**  
eb0e0 20 20 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 50 52    .**  SQLITE_PR
eb0f0 4f 58 59 5f 44 45 42 55 47 0a 2a 2a 0a 2a 2a 20  OXY_DEBUG.**.** 
eb100 20 20 20 20 20 20 45 6e 61 62 6c 65 73 20 74 68        Enables th
eb110 65 20 6c 6f 67 67 69 6e 67 20 6f 66 20 65 72 72  e logging of err
eb120 6f 72 20 6d 65 73 73 61 67 65 73 20 64 75 72 69  or messages duri
eb130 6e 67 20 68 6f 73 74 20 69 64 20 66 69 6c 65 0a  ng host id file.
eb140 2a 2a 20 20 20 20 20 20 20 72 65 74 72 69 65 76  **       retriev
eb150 61 6c 20 61 6e 64 20 63 72 65 61 74 69 6f 6e 0a  al and creation.
eb160 2a 2a 0a 2a 2a 20 20 48 4f 53 54 49 44 50 41 54  **.**  HOSTIDPAT
eb170 48 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f 76  H.**.**       Ov
eb180 65 72 72 69 64 65 73 20 74 68 65 20 64 65 66 61  errides the defa
eb190 75 6c 74 20 68 6f 73 74 20 49 44 20 66 69 6c 65  ult host ID file
eb1a0 20 70 61 74 68 20 6c 6f 63 61 74 69 6f 6e 0a 2a   path location.*
eb1b0 2a 0a 2a 2a 20 20 4c 4f 43 4b 50 52 4f 58 59 44  *.**  LOCKPROXYD
eb1c0 49 52 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f  IR.**.**       O
eb1d0 76 65 72 72 69 64 65 73 20 74 68 65 20 64 65 66  verrides the def
eb1e0 61 75 6c 74 20 64 69 72 65 63 74 6f 72 79 20 75  ault directory u
eb1f0 73 65 64 20 66 6f 72 20 6c 6f 63 6b 20 70 72 6f  sed for lock pro
eb200 78 79 20 66 69 6c 65 73 20 74 68 61 74 0a 2a 2a  xy files that.**
eb210 20 20 20 20 20 20 20 61 72 65 20 6e 61 6d 65 64         are named
eb220 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 76   automatically v
eb230 69 61 20 74 68 65 20 22 3a 61 75 74 6f 3a 22 20  ia the ":auto:" 
eb240 73 65 74 74 69 6e 67 0a 2a 2a 0a 2a 2a 20 20 53  setting.**.**  S
eb250 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52  QLITE_DEFAULT_PR
eb260 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f  OXYDIR_PERMISSIO
eb270 4e 53 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 50  NS.**.**       P
eb280 65 72 6d 69 73 73 69 6f 6e 73 20 74 6f 20 75 73  ermissions to us
eb290 65 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67 20  e when creating 
eb2a0 61 20 64 69 72 65 63 74 6f 72 79 20 66 6f 72 20  a directory for 
eb2b0 73 74 6f 72 69 6e 67 20 74 68 65 0a 2a 2a 20 20  storing the.**  
eb2c0 20 20 20 20 20 6c 6f 63 6b 20 70 72 6f 78 79 20       lock proxy 
eb2d0 66 69 6c 65 73 2c 20 6f 6e 6c 79 20 75 73 65 64  files, only used
eb2e0 20 77 68 65 6e 20 4c 4f 43 4b 50 52 4f 58 59 44   when LOCKPROXYD
eb2f0 49 52 20 69 73 20 6e 6f 74 20 73 65 74 2e 0a 2a  IR is not set..*
eb300 2a 20 20 20 20 0a 2a 2a 20 20 20 20 0a 2a 2a 20  *    .**    .** 
eb310 41 73 20 6d 65 6e 74 69 6f 6e 65 64 20 61 62 6f  As mentioned abo
eb320 76 65 2c 20 77 68 65 6e 20 63 6f 6d 70 69 6c 65  ve, when compile
eb330 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 50 52  d with SQLITE_PR
eb340 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49  EFER_PROXY_LOCKI
eb350 4e 47 2c 0a 2a 2a 20 73 65 74 74 69 6e 67 20 74  NG,.** setting t
eb360 68 65 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 76  he environment v
eb370 61 72 69 61 62 6c 65 20 53 51 4c 49 54 45 5f 46  ariable SQLITE_F
eb380 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49  ORCE_PROXY_LOCKI
eb390 4e 47 20 74 6f 20 31 20 77 69 6c 6c 0a 2a 2a 20  NG to 1 will.** 
eb3a0 66 6f 72 63 65 20 70 72 6f 78 79 20 6c 6f 63 6b  force proxy lock
eb3b0 69 6e 67 20 74 6f 20 62 65 20 75 73 65 64 20 66  ing to be used f
eb3c0 6f 72 20 65 76 65 72 79 20 64 61 74 61 62 61 73  or every databas
eb3d0 65 20 66 69 6c 65 20 6f 70 65 6e 65 64 2c 20 61  e file opened, a
eb3e0 6e 64 20 30 0a 2a 2a 20 77 69 6c 6c 20 66 6f 72  nd 0.** will for
eb3f0 63 65 20 61 75 74 6f 6d 61 74 69 63 20 70 72 6f  ce automatic pro
eb400 78 79 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 62 65  xy locking to be
eb410 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 61 6c   disabled for al
eb420 6c 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69  l database.** fi
eb430 6c 65 73 20 28 65 78 70 6c 69 63 69 74 79 20 63  les (explicity c
eb440 61 6c 6c 69 6e 67 20 74 68 65 20 53 51 4c 49 54  alling the SQLIT
eb450 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46  E_SET_LOCKPROXYF
eb460 49 4c 45 20 70 72 61 67 6d 61 20 6f 72 0a 2a 2a  ILE pragma or.**
eb470 20 73 71 6c 69 74 65 5f 66 69 6c 65 5f 63 6f 6e   sqlite_file_con
eb480 74 72 6f 6c 20 41 50 49 20 69 73 20 6e 6f 74 20  trol API is not 
eb490 61 66 66 65 63 74 65 64 20 62 79 20 53 51 4c 49  affected by SQLI
eb4a0 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c  TE_FORCE_PROXY_L
eb4b0 4f 43 4b 49 4e 47 29 2e 0a 2a 2f 0a 0a 2f 2a 0a  OCKING)..*/../*.
eb4c0 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67  ** Proxy locking
eb4d0 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62   is only availab
eb4e0 6c 65 20 6f 6e 20 4d 61 63 4f 53 58 20 0a 2a 2f  le on MacOSX .*/
eb4f0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41  .#if defined(__A
eb500 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54  PPLE__) && SQLIT
eb510 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
eb520 5f 53 54 59 4c 45 0a 0a 23 69 66 64 65 66 20 53  _STYLE..#ifdef S
eb530 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 20 73 69  QLITE_TEST./* si
eb540 6d 75 6c 61 74 65 20 6d 75 6c 74 69 70 6c 65 20  mulate multiple 
eb550 68 6f 73 74 73 20 62 79 20 63 72 65 61 74 69 6e  hosts by creatin
eb560 67 20 75 6e 69 71 75 65 20 68 6f 73 74 69 64 20  g unique hostid 
eb570 66 69 6c 65 20 70 61 74 68 73 20 2a 2f 0a 53 51  file paths */.SQ
eb580 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
eb590 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 20  ite3_hostid_num 
eb5a0 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  = 0;.#endif../*.
eb5b0 2a 2a 20 54 68 65 20 70 72 6f 78 79 4c 6f 63 6b  ** The proxyLock
eb5c0 69 6e 67 43 6f 6e 74 65 78 74 20 68 61 73 20 74  ingContext has t
eb5d0 68 65 20 70 61 74 68 20 61 6e 64 20 66 69 6c 65  he path and file
eb5e0 20 73 74 72 75 63 74 75 72 65 73 20 66 6f 72 20   structures for 
eb5f0 74 68 65 20 72 65 6d 6f 74 65 20 0a 2a 2a 20 61  the remote .** a
eb600 6e 64 20 6c 6f 63 61 6c 20 70 72 6f 78 79 20 66  nd local proxy f
eb610 69 6c 65 73 20 69 6e 20 69 74 0a 2a 2f 0a 74 79  iles in it.*/.ty
eb620 70 65 64 65 66 20 73 74 72 75 63 74 20 70 72 6f  pedef struct pro
eb630 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
eb640 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e   proxyLockingCon
eb650 74 65 78 74 3b 0a 73 74 72 75 63 74 20 70 72 6f  text;.struct pro
eb660 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
eb670 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 63   {.  unixFile *c
eb680 6f 6e 63 68 46 69 6c 65 3b 20 20 20 20 20 20 20  onchFile;       
eb690 20 20 2f 2a 20 4f 70 65 6e 20 63 6f 6e 63 68 20    /* Open conch 
eb6a0 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  file */.  char *
eb6b0 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 3b 20 20  conchFilePath;  
eb6c0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
eb6d0 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65  f the conch file
eb6e0 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a   */.  unixFile *
eb6f0 6c 6f 63 6b 50 72 6f 78 79 3b 20 20 20 20 20 20  lockProxy;      
eb700 20 20 20 2f 2a 20 4f 70 65 6e 20 70 72 6f 78 79     /* Open proxy
eb710 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20   lock file */.  
eb720 63 68 61 72 20 2a 6c 6f 63 6b 50 72 6f 78 79 50  char *lockProxyP
eb730 61 74 68 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ath;         /* 
eb740 4e 61 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 78  Name of the prox
eb750 79 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20  y lock file */. 
eb760 20 63 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20   char *dbPath;  
eb770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
eb780 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f 70 65   Name of the ope
eb790 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20  n file */.  int 
eb7a0 63 6f 6e 63 68 48 65 6c 64 3b 20 20 20 20 20 20  conchHeld;      
eb7b0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
eb7c0 20 69 66 20 74 68 65 20 63 6f 6e 63 68 20 69 73   if the conch is
eb7d0 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20   currently held 
eb7e0 2a 2f 0a 20 20 76 6f 69 64 20 2a 6f 6c 64 4c 6f  */.  void *oldLo
eb7f0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 20 20 20  ckingContext;   
eb800 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6c 6f    /* Original lo
eb810 63 6b 69 6e 67 63 6f 6e 74 65 78 74 20 74 6f 20  ckingcontext to 
eb820 72 65 73 74 6f 72 65 20 6f 6e 20 63 6c 6f 73 65  restore on close
eb830 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f   */.  sqlite3_io
eb840 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a  _methods const *
eb850 70 4f 6c 64 4d 65 74 68 6f 64 3b 20 20 20 20 20  pOldMethod;     
eb860 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 49 2f 4f 20  /* Original I/O 
eb870 6d 65 74 68 6f 64 73 20 66 6f 72 20 63 6c 6f 73  methods for clos
eb880 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 48 4f 53 54  e */.};../* HOST
eb890 49 44 4c 45 4e 20 61 6e 64 20 43 4f 4e 43 48 4c  IDLEN and CONCHL
eb8a0 45 4e 20 62 6f 74 68 20 69 6e 63 6c 75 64 65 20  EN both include 
eb8b0 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 73 74  space for the st
eb8c0 72 69 6e 67 20 0a 2a 2a 20 74 65 72 6d 69 6e 61  ring .** termina
eb8d0 74 69 6e 67 20 6e 75 6c 20 0a 2a 2f 0a 23 64 65  ting nul .*/.#de
eb8e0 66 69 6e 65 20 48 4f 53 54 49 44 4c 45 4e 20 20  fine HOSTIDLEN  
eb8f0 20 20 20 20 20 20 20 31 32 38 0a 23 64 65 66 69         128.#defi
eb900 6e 65 20 43 4f 4e 43 48 4c 45 4e 20 20 20 20 20  ne CONCHLEN     
eb910 20 20 20 20 20 28 4d 41 58 50 41 54 48 4c 45 4e       (MAXPATHLEN
eb920 2b 48 4f 53 54 49 44 4c 45 4e 2b 31 29 0a 23 69  +HOSTIDLEN+1).#i
eb930 66 6e 64 65 66 20 48 4f 53 54 49 44 50 41 54 48  fndef HOSTIDPATH
eb940 0a 23 20 64 65 66 69 6e 65 20 48 4f 53 54 49 44  .# define HOSTID
eb950 50 41 54 48 20 20 20 20 20 20 20 22 2f 4c 69 62  PATH       "/Lib
eb960 72 61 72 79 2f 43 61 63 68 65 73 2f 2e 63 6f 6d  rary/Caches/.com
eb970 2e 61 70 70 6c 65 2e 73 71 6c 69 74 65 43 6f 6e  .apple.sqliteCon
eb980 63 68 48 6f 73 74 49 64 22 0a 23 65 6e 64 69 66  chHostId".#endif
eb990 0a 0a 2f 2a 20 62 61 73 69 63 61 6c 6c 79 20 61  ../* basically a
eb9a0 20 63 6f 70 79 20 6f 66 20 75 6e 69 78 52 61 6e   copy of unixRan
eb9b0 64 6f 6d 6e 65 73 73 20 77 69 74 68 20 64 69 66  domness with dif
eb9c0 66 65 72 65 6e 74 0a 2a 2a 20 74 65 73 74 20 62  ferent.** test b
eb9d0 65 68 61 76 69 6f 72 20 62 75 69 6c 74 20 69 6e  ehavior built in
eb9e0 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70   */.static int p
eb9f0 72 6f 78 79 47 65 6e 65 72 61 74 65 48 6f 73 74  roxyGenerateHost
eba00 49 44 28 63 68 61 72 20 2a 70 48 6f 73 74 49 44  ID(char *pHostID
eba10 29 7b 0a 20 20 69 6e 74 20 70 69 64 2c 20 66 64  ){.  int pid, fd
eba20 2c 20 6c 65 6e 3b 0a 20 20 75 6e 73 69 67 6e 65  , len;.  unsigne
eba30 64 20 63 68 61 72 20 2a 6b 65 79 20 3d 20 28 75  d char *key = (u
eba40 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70  nsigned char *)p
eba50 48 6f 73 74 49 44 3b 0a 20 20 0a 20 20 6d 65 6d  HostID;.  .  mem
eba60 73 65 74 28 6b 65 79 2c 20 30 2c 20 48 4f 53 54  set(key, 0, HOST
eba70 49 44 4c 45 4e 29 3b 0a 20 20 6c 65 6e 20 3d 20  IDLEN);.  len = 
eba80 30 3b 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 22  0;.  fd = open("
eba90 2f 64 65 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f  /dev/urandom", O
ebaa0 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20 69 66 28 20  _RDONLY);.  if( 
ebab0 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 6c 65 6e  fd>=0 ){.    len
ebac0 20 3d 20 72 65 61 64 28 66 64 2c 20 6b 65 79 2c   = read(fd, key,
ebad0 20 48 4f 53 54 49 44 4c 45 4e 29 3b 0a 20 20 20   HOSTIDLEN);.   
ebae0 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73   close(fd); /* s
ebaf0 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65  ilently leak the
ebb00 20 66 64 20 69 66 20 69 74 20 66 61 69 6c 73 20   fd if it fails 
ebb10 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 6c 65 6e  */.  }.  if( len
ebb20 20 3c 20 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a   < HOSTIDLEN ){.
ebb30 20 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20      time_t t;.  
ebb40 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20    time(&t);.    
ebb50 6d 65 6d 63 70 79 28 6b 65 79 2c 20 26 74 2c 20  memcpy(key, &t, 
ebb60 73 69 7a 65 6f 66 28 74 29 29 3b 0a 20 20 20 20  sizeof(t));.    
ebb70 70 69 64 20 3d 20 67 65 74 70 69 64 28 29 3b 0a  pid = getpid();.
ebb80 20 20 20 20 6d 65 6d 63 70 79 28 26 6b 65 79 5b      memcpy(&key[
ebb90 73 69 7a 65 6f 66 28 74 29 5d 2c 20 26 70 69 64  sizeof(t)], &pid
ebba0 2c 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a  , sizeof(pid));.
ebbb0 20 20 7d 0a 20 20 0a 23 69 66 64 65 66 20 4d 41    }.  .#ifdef MA
ebbc0 4b 45 5f 50 52 45 54 54 59 5f 48 4f 53 54 49 44  KE_PRETTY_HOSTID
ebbd0 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  .  {.    int i;.
ebbe0 20 20 20 20 2f 2a 20 66 69 6c 74 65 72 20 74 68      /* filter th
ebbf0 65 20 62 79 74 65 73 20 69 6e 74 6f 20 70 72 69  e bytes into pri
ebc00 6e 74 61 62 6c 65 20 61 73 63 69 69 20 63 68 61  ntable ascii cha
ebc10 72 61 63 74 65 72 73 20 61 6e 64 20 4e 55 4c 20  racters and NUL 
ebc20 74 65 72 6d 69 6e 61 74 65 20 2a 2f 0a 20 20 20  terminate */.   
ebc30 20 6b 65 79 5b 28 48 4f 53 54 49 44 4c 45 4e 2d   key[(HOSTIDLEN-
ebc40 31 29 5d 20 3d 20 30 78 30 30 3b 0a 20 20 20 20  1)] = 0x00;.    
ebc50 66 6f 72 28 20 69 3d 30 3b 20 69 3c 28 48 4f 53  for( i=0; i<(HOS
ebc60 54 49 44 4c 45 4e 2d 31 29 3b 20 69 2b 2b 20 29  TIDLEN-1); i++ )
ebc70 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64  {.      unsigned
ebc80 20 63 68 61 72 20 70 61 20 3d 20 6b 65 79 5b 69   char pa = key[i
ebc90 5d 26 30 78 37 46 3b 0a 20 20 20 20 20 20 69 66  ]&0x7F;.      if
ebca0 28 20 70 61 3c 30 78 32 30 20 29 7b 0a 20 20 20  ( pa<0x20 ){.   
ebcb0 20 20 20 20 20 6b 65 79 5b 69 5d 20 3d 20 28 6b       key[i] = (k
ebcc0 65 79 5b 69 5d 26 30 78 38 30 20 3d 3d 20 30 78  ey[i]&0x80 == 0x
ebcd0 38 30 29 20 3f 20 70 61 2b 30 78 34 30 20 3a 20  80) ? pa+0x40 : 
ebce0 70 61 2b 30 78 32 30 3b 0a 20 20 20 20 20 20 7d  pa+0x20;.      }
ebcf0 65 6c 73 65 20 69 66 28 20 70 61 3d 3d 30 78 37  else if( pa==0x7
ebd00 46 20 29 7b 0a 20 20 20 20 20 20 20 20 6b 65 79  F ){.        key
ebd10 5b 69 5d 20 3d 20 28 6b 65 79 5b 69 5d 26 30 78  [i] = (key[i]&0x
ebd20 38 30 20 3d 3d 20 30 78 38 30 29 20 3f 20 70 61  80 == 0x80) ? pa
ebd30 3d 30 78 32 30 20 3a 20 70 61 2b 30 78 37 45 3b  =0x20 : pa+0x7E;
ebd40 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
ebd50 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75   }.#endif.  retu
ebd60 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
ebd70 0a 2f 2a 20 77 72 69 74 65 73 20 74 68 65 20 68  ./* writes the h
ebd80 6f 73 74 20 69 64 20 70 61 74 68 20 74 6f 20 70  ost id path to p
ebd90 61 74 68 2c 20 70 61 74 68 20 73 68 6f 75 6c 64  ath, path should
ebda0 20 62 65 20 61 6e 20 70 72 65 2d 61 6c 6c 6f 63   be an pre-alloc
ebdb0 61 74 65 64 20 62 75 66 66 65 72 0a 2a 2a 20 77  ated buffer.** w
ebdc0 69 74 68 20 65 6e 6f 75 67 68 20 73 70 61 63 65  ith enough space
ebdd0 20 66 6f 72 20 61 20 70 61 74 68 20 0a 2a 2f 0a   for a path .*/.
ebde0 73 74 61 74 69 63 20 76 6f 69 64 20 70 72 6f 78  static void prox
ebdf0 79 47 65 74 48 6f 73 74 49 44 50 61 74 68 28 63  yGetHostIDPath(c
ebe00 68 61 72 20 2a 70 61 74 68 2c 20 73 69 7a 65 5f  har *path, size_
ebe10 74 20 6c 65 6e 29 7b 0a 20 20 73 74 72 6c 63 70  t len){.  strlcp
ebe20 79 28 70 61 74 68 2c 20 48 4f 53 54 49 44 50 41  y(path, HOSTIDPA
ebe30 54 48 2c 20 6c 65 6e 29 3b 0a 23 69 66 64 65 66  TH, len);.#ifdef
ebe40 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69   SQLITE_TEST.  i
ebe50 66 28 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69  f( sqlite3_hosti
ebe60 64 5f 6e 75 6d 3e 30 20 29 7b 0a 20 20 20 20 63  d_num>0 ){.    c
ebe70 68 61 72 20 73 75 66 66 69 78 5b 32 5d 20 3d 20  har suffix[2] = 
ebe80 22 31 22 3b 0a 20 20 20 20 73 75 66 66 69 78 5b  "1";.    suffix[
ebe90 30 5d 20 3d 20 73 75 66 66 69 78 5b 30 5d 20 2b  0] = suffix[0] +
ebea0 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f   sqlite3_hostid_
ebeb0 6e 75 6d 3b 0a 20 20 20 20 73 74 72 6c 63 61 74  num;.    strlcat
ebec0 28 70 61 74 68 2c 20 73 75 66 66 69 78 2c 20 6c  (path, suffix, l
ebed0 65 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  en);.  }.#endif.
ebee0 20 20 4f 53 54 52 41 43 45 33 28 22 47 45 54 48    OSTRACE3("GETH
ebef0 4f 53 54 49 44 50 41 54 48 20 20 25 73 20 70 69  OSTIDPATH  %s pi
ebf00 64 3d 25 64 5c 6e 22 2c 20 70 61 74 68 2c 20 67  d=%d\n", path, g
ebf10 65 74 70 69 64 28 29 29 3b 0a 7d 0a 0a 2f 2a 20  etpid());.}../* 
ebf20 67 65 74 20 74 68 65 20 68 6f 73 74 20 49 44 20  get the host ID 
ebf30 66 72 6f 6d 20 61 20 73 71 6c 69 74 65 20 68 6f  from a sqlite ho
ebf40 73 74 69 64 20 66 69 6c 65 20 73 74 6f 72 65 64  stid file stored
ebf50 20 69 6e 20 74 68 65 20 0a 2a 2a 20 75 73 65 72   in the .** user
ebf60 2d 73 70 65 63 69 66 69 63 20 74 6d 70 20 64 69  -specific tmp di
ebf70 72 65 63 74 6f 72 79 2c 20 63 72 65 61 74 65 20  rectory, create 
ebf80 74 68 65 20 49 44 20 69 66 20 69 74 27 73 20 6e  the ID if it's n
ebf90 6f 74 20 74 68 65 72 65 20 61 6c 72 65 61 64 79  ot there already
ebfa0 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20   .*/.static int 
ebfb0 70 72 6f 78 79 47 65 74 48 6f 73 74 49 44 28 63  proxyGetHostID(c
ebfc0 68 61 72 20 2a 70 48 6f 73 74 49 44 2c 20 69 6e  har *pHostID, in
ebfd0 74 20 2a 70 45 72 72 6f 72 29 7b 0a 20 20 69 6e  t *pError){.  in
ebfe0 74 20 66 64 3b 0a 20 20 63 68 61 72 20 70 61 74  t fd;.  char pat
ebff0 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 20 0a  h[MAXPATHLEN]; .
ec000 20 20 73 69 7a 65 5f 74 20 6c 65 6e 3b 0a 20 20    size_t len;.  
ec010 69 6e 74 20 72 63 3d 53 51 4c 49 54 45 5f 4f 4b  int rc=SQLITE_OK
ec020 3b 0a 0a 20 20 70 72 6f 78 79 47 65 74 48 6f 73  ;..  proxyGetHos
ec030 74 49 44 50 61 74 68 28 70 61 74 68 2c 20 4d 41  tIDPath(path, MA
ec040 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 2f 2a 20  XPATHLEN);.  /* 
ec050 74 72 79 20 74 6f 20 63 72 65 61 74 65 20 74 68  try to create th
ec060 65 20 68 6f 73 74 20 49 44 20 66 69 6c 65 2c 20  e host ID file, 
ec070 69 66 20 69 74 20 61 6c 72 65 61 64 79 20 65 78  if it already ex
ec080 69 73 74 73 20 72 65 61 64 20 74 68 65 20 63 6f  ists read the co
ec090 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 66 64 20 3d  ntents */.  fd =
ec0a0 20 6f 70 65 6e 28 70 61 74 68 2c 20 4f 5f 43 52   open(path, O_CR
ec0b0 45 41 54 7c 4f 5f 57 52 4f 4e 4c 59 7c 4f 5f 45  EAT|O_WRONLY|O_E
ec0c0 58 43 4c 2c 20 30 36 34 34 29 3b 0a 20 20 69 66  XCL, 0644);.  if
ec0d0 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 69 6e  ( fd<0 ){.    in
ec0e0 74 20 65 72 72 3d 65 72 72 6e 6f 3b 0a 09 09 0a  t err=errno;....
ec0f0 20 20 20 20 69 66 28 20 65 72 72 21 3d 45 45 58      if( err!=EEX
ec100 49 53 54 20 29 7b 0a 23 69 66 64 65 66 20 53 51  IST ){.#ifdef SQ
ec110 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47  LITE_PROXY_DEBUG
ec120 20 2f 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69   /* set the sqli
ec130 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  te error message
ec140 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20 20   instead */.    
ec150 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72    fprintf(stderr
ec160 2c 20 22 73 71 6c 69 74 65 20 65 72 72 6f 72 20  , "sqlite error 
ec170 63 72 65 61 74 69 6e 67 20 68 6f 73 74 20 49 44  creating host ID
ec180 20 66 69 6c 65 20 25 73 3a 20 25 73 5c 6e 22 2c   file %s: %s\n",
ec190 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
ec1a0 61 74 68 2c 20 73 74 72 65 72 72 6f 72 28 65 72  ath, strerror(er
ec1b0 72 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  r));.#endif.    
ec1c0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
ec1d0 50 45 52 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  PERM;.    }.    
ec1e0 2f 2a 20 63 6f 75 6c 64 6e 27 74 20 63 72 65 61  /* couldn't crea
ec1f0 74 65 20 74 68 65 20 66 69 6c 65 2c 20 72 65 61  te the file, rea
ec200 64 20 69 74 20 69 6e 73 74 65 61 64 20 2a 2f 0a  d it instead */.
ec210 20 20 20 20 66 64 20 3d 20 6f 70 65 6e 28 70 61      fd = open(pa
ec220 74 68 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 45  th, O_RDONLY|O_E
ec230 58 43 4c 29 3b 0a 20 20 20 20 69 66 28 20 66 64  XCL);.    if( fd
ec240 3c 30 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c  <0 ){.#ifdef SQL
ec250 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 20  ITE_PROXY_DEBUG 
ec260 2f 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69 74  /* set the sqlit
ec270 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  e error message 
ec280 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 20  instead */.     
ec290 20 69 6e 74 20 65 72 72 20 3d 20 65 72 72 6e 6f   int err = errno
ec2a0 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28  ;.      fprintf(
ec2b0 73 74 64 65 72 72 2c 20 22 73 71 6c 69 74 65 20  stderr, "sqlite 
ec2c0 65 72 72 6f 72 20 6f 70 65 6e 69 6e 67 20 68 6f  error opening ho
ec2d0 73 74 20 49 44 20 66 69 6c 65 20 25 73 3a 20 25  st ID file %s: %
ec2e0 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  s\n",.          
ec2f0 20 20 20 20 70 61 74 68 2c 20 73 74 72 65 72 72      path, strerr
ec300 6f 72 28 65 72 72 29 29 3b 0a 23 65 6e 64 69 66  or(err));.#endif
ec310 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
ec320 4c 49 54 45 5f 50 45 52 4d 3b 0a 20 20 20 20 7d  LITE_PERM;.    }
ec330 0a 20 20 20 20 6c 65 6e 20 3d 20 70 72 65 61 64  .    len = pread
ec340 28 66 64 2c 20 70 48 6f 73 74 49 44 2c 20 48 4f  (fd, pHostID, HO
ec350 53 54 49 44 4c 45 4e 2c 20 30 29 3b 0a 20 20 20  STIDLEN, 0);.   
ec360 20 69 66 28 20 6c 65 6e 3c 30 20 29 7b 0a 20 20   if( len<0 ){.  
ec370 20 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 65 72      *pError = er
ec380 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  rno;.      rc = 
ec390 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41  SQLITE_IOERR_REA
ec3a0 44 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  D;.    }else if(
ec3b0 20 6c 65 6e 3c 48 4f 53 54 49 44 4c 45 4e 20 29   len<HOSTIDLEN )
ec3c0 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 6f 72 20  {.      *pError 
ec3d0 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  = 0;.      rc = 
ec3e0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f  SQLITE_IOERR_SHO
ec3f0 52 54 5f 52 45 41 44 3b 0a 20 20 20 20 7d 0a 20  RT_READ;.    }. 
ec400 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a     close(fd); /*
ec410 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74   silently leak t
ec420 68 65 20 66 64 20 69 66 20 69 74 20 66 61 69 6c  he fd if it fail
ec430 73 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41 43 45  s */.    OSTRACE
ec440 33 28 22 47 45 54 48 4f 53 54 49 44 20 20 72 65  3("GETHOSTID  re
ec450 61 64 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c  ad %s pid=%d\n",
ec460 20 70 48 6f 73 74 49 44 2c 20 67 65 74 70 69 64   pHostID, getpid
ec470 28 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ());.    return 
ec480 72 63 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  rc;.  }else{.   
ec490 20 2f 2a 20 77 65 27 72 65 20 63 72 65 61 74 69   /* we're creati
ec4a0 6e 67 20 74 68 65 20 68 6f 73 74 20 49 44 20 66  ng the host ID f
ec4b0 69 6c 65 20 28 75 73 65 20 61 20 72 61 6e 64 6f  ile (use a rando
ec4c0 6d 20 73 74 72 69 6e 67 20 6f 66 20 62 79 74 65  m string of byte
ec4d0 73 29 20 2a 2f 0a 20 20 20 20 70 72 6f 78 79 47  s) */.    proxyG
ec4e0 65 6e 65 72 61 74 65 48 6f 73 74 49 44 28 70 48  enerateHostID(pH
ec4f0 6f 73 74 49 44 29 3b 0a 20 20 20 20 6c 65 6e 20  ostID);.    len 
ec500 3d 20 70 77 72 69 74 65 28 66 64 2c 20 70 48 6f  = pwrite(fd, pHo
ec510 73 74 49 44 2c 20 48 4f 53 54 49 44 4c 45 4e 2c  stID, HOSTIDLEN,
ec520 20 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 65 6e   0);.    if( len
ec530 3c 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72  <0 ){.      *pEr
ec540 72 6f 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  ror = errno;.   
ec550 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49     rc = SQLITE_I
ec560 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20  OERR_WRITE;.    
ec570 7d 65 6c 73 65 20 69 66 28 20 6c 65 6e 3c 48 4f  }else if( len<HO
ec580 53 54 49 44 4c 45 4e 20 29 7b 0a 20 20 20 20 20  STIDLEN ){.     
ec590 20 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20   *pError = 0;.  
ec5a0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
ec5b0 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20  IOERR_WRITE;.   
ec5c0 20 7d 0a 20 20 20 20 63 6c 6f 73 65 28 66 64 29   }.    close(fd)
ec5d0 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65  ; /* silently le
ec5e0 61 6b 20 74 68 65 20 66 64 20 69 66 20 69 74 20  ak the fd if it 
ec5f0 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 4f 53 54  fails */.    OST
ec600 52 41 43 45 33 28 22 47 45 54 48 4f 53 54 49 44  RACE3("GETHOSTID
ec610 20 20 77 72 6f 74 65 20 25 73 20 70 69 64 3d 25    wrote %s pid=%
ec620 64 5c 6e 22 2c 20 70 48 6f 73 74 49 44 2c 20 67  d\n", pHostID, g
ec630 65 74 70 69 64 28 29 29 3b 0a 20 20 20 20 72 65  etpid());.    re
ec640 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a  turn rc;.  }.}..
ec650 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79  static int proxy
ec660 47 65 74 4c 6f 63 6b 50 61 74 68 28 63 6f 6e 73  GetLockPath(cons
ec670 74 20 63 68 61 72 20 2a 64 62 50 61 74 68 2c 20  t char *dbPath, 
ec680 63 68 61 72 20 2a 6c 50 61 74 68 2c 20 73 69 7a  char *lPath, siz
ec690 65 5f 74 20 6d 61 78 4c 65 6e 29 7b 0a 20 20 69  e_t maxLen){.  i
ec6a0 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 64 62  nt len;.  int db
ec6b0 4c 65 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 23  Len;.  int i;..#
ec6c0 69 66 64 65 66 20 4c 4f 43 4b 50 52 4f 58 59 44  ifdef LOCKPROXYD
ec6d0 49 52 0a 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63  IR.  len = strlc
ec6e0 70 79 28 6c 50 61 74 68 2c 20 4c 4f 43 4b 50 52  py(lPath, LOCKPR
ec6f0 4f 58 59 44 49 52 2c 20 6d 61 78 4c 65 6e 29 3b  OXYDIR, maxLen);
ec700 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20 5f  .#else.# ifdef _
ec710 43 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f 54  CS_DARWIN_USER_T
ec720 45 4d 50 5f 44 49 52 0a 20 20 7b 0a 20 20 20 20  EMP_DIR.  {.    
ec730 63 6f 6e 66 73 74 72 28 5f 43 53 5f 44 41 52 57  confstr(_CS_DARW
ec740 49 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52  IN_USER_TEMP_DIR
ec750 2c 20 6c 50 61 74 68 2c 20 6d 61 78 4c 65 6e 29  , lPath, maxLen)
ec760 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 74 72 6c  ;.    len = strl
ec770 63 61 74 28 6c 50 61 74 68 2c 20 22 73 71 6c 69  cat(lPath, "sqli
ec780 74 65 70 6c 6f 63 6b 73 22 2c 20 6d 61 78 4c 65  teplocks", maxLe
ec790 6e 29 3b 0a 20 20 20 20 69 66 28 20 6d 6b 64 69  n);.    if( mkdi
ec7a0 72 28 6c 50 61 74 68 2c 20 53 51 4c 49 54 45 5f  r(lPath, SQLITE_
ec7b0 44 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52  DEFAULT_PROXYDIR
ec7c0 5f 50 45 52 4d 49 53 53 49 4f 4e 53 29 20 29 7b  _PERMISSIONS) ){
ec7d0 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 6d 6b 64  .      /* if mkd
ec7e0 69 72 20 66 61 69 6c 73 2c 20 68 61 6e 64 6c 65  ir fails, handle
ec7f0 20 61 73 20 6c 6f 63 6b 20 66 69 6c 65 20 63 72   as lock file cr
ec800 65 61 74 69 6f 6e 20 66 61 69 6c 75 72 65 20 2a  eation failure *
ec810 2f 0a 23 20 20 69 66 64 65 66 20 53 51 4c 49 54  /.#  ifdef SQLIT
ec820 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 69 6e  E_DEBUG.      in
ec830 74 20 65 72 72 20 3d 20 65 72 72 6e 6f 3b 0a 20  t err = errno;. 
ec840 20 20 20 20 20 69 66 28 20 65 72 72 21 3d 45 45       if( err!=EE
ec850 58 49 53 54 20 29 7b 0a 20 20 20 20 20 20 20 20  XIST ){.        
ec860 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20  fprintf(stderr, 
ec870 22 70 72 6f 78 79 47 65 74 4c 6f 63 6b 50 61 74  "proxyGetLockPat
ec880 68 3a 20 6d 6b 64 69 72 28 25 73 2c 30 25 6f 29  h: mkdir(%s,0%o)
ec890 20 65 72 72 6f 72 20 25 64 20 25 73 5c 6e 22 2c   error %d %s\n",
ec8a0 20 6c 50 61 74 68 2c 0a 20 20 20 20 20 20 20 20   lPath,.        
ec8b0 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44          SQLITE_D
ec8c0 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f  EFAULT_PROXYDIR_
ec8d0 50 45 52 4d 49 53 53 49 4f 4e 53 2c 20 65 72 72  PERMISSIONS, err
ec8e0 2c 20 73 74 72 65 72 72 6f 72 28 65 72 72 29 29  , strerror(err))
ec8f0 3b 0a 20 20 20 20 20 20 7d 0a 23 20 20 65 6e 64  ;.      }.#  end
ec900 69 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  if.    }else{.  
ec910 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 47 45      OSTRACE3("GE
ec920 54 4c 4f 43 4b 50 41 54 48 20 20 6d 6b 64 69 72  TLOCKPATH  mkdir
ec930 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 6c   %s pid=%d\n", l
ec940 50 61 74 68 2c 20 67 65 74 70 69 64 28 29 29 3b  Path, getpid());
ec950 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 7d 0a  .    }.    .  }.
ec960 23 20 65 6c 73 65 0a 20 20 6c 65 6e 20 3d 20 73  # else.  len = s
ec970 74 72 6c 63 70 79 28 6c 50 61 74 68 2c 20 22 2f  trlcpy(lPath, "/
ec980 74 6d 70 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a  tmp/", maxLen);.
ec990 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a  # endif.#endif..
ec9a0 20 20 69 66 28 20 6c 50 61 74 68 5b 6c 65 6e 2d    if( lPath[len-
ec9b0 31 5d 21 3d 27 2f 27 20 29 7b 0a 20 20 20 20 6c  1]!='/' ){.    l
ec9c0 65 6e 20 3d 20 73 74 72 6c 63 61 74 28 6c 50 61  en = strlcat(lPa
ec9d0 74 68 2c 20 22 2f 22 2c 20 6d 61 78 4c 65 6e 29  th, "/", maxLen)
ec9e0 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 74 72  ;.  }.  .  /* tr
ec9f0 61 6e 73 66 6f 72 6d 20 74 68 65 20 64 62 20 70  ansform the db p
eca00 61 74 68 20 74 6f 20 61 20 75 6e 69 71 75 65 20  ath to a unique 
eca10 63 61 63 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  cache name */.  
eca20 64 62 4c 65 6e 20 3d 20 28 69 6e 74 29 73 74 72  dbLen = (int)str
eca30 6c 65 6e 28 64 62 50 61 74 68 29 3b 0a 20 20 66  len(dbPath);.  f
eca40 6f 72 28 20 69 3d 30 3b 20 69 3c 64 62 4c 65 6e  or( i=0; i<dbLen
eca50 20 26 26 20 28 69 2b 6c 65 6e 2b 37 29 3c 6d 61   && (i+len+7)<ma
eca60 78 4c 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  xLen; i++){.    
eca70 63 68 61 72 20 63 20 3d 20 64 62 50 61 74 68 5b  char c = dbPath[
eca80 69 5d 3b 0a 20 20 20 20 6c 50 61 74 68 5b 69 2b  i];.    lPath[i+
eca90 6c 65 6e 5d 20 3d 20 28 63 3d 3d 27 2f 27 29 3f  len] = (c=='/')?
ecaa0 27 5f 27 3a 63 3b 0a 20 20 7d 0a 20 20 6c 50 61  '_':c;.  }.  lPa
ecab0 74 68 5b 69 2b 6c 65 6e 5d 3d 27 5c 30 27 3b 0a  th[i+len]='\0';.
ecac0 20 20 73 74 72 6c 63 61 74 28 6c 50 61 74 68 2c    strlcat(lPath,
ecad0 20 22 3a 61 75 74 6f 3a 22 2c 20 6d 61 78 4c 65   ":auto:", maxLe
ecae0 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  n);.  return SQL
ecaf0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
ecb00 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 56 46   Create a new VF
ecb10 53 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  S file descripto
ecb20 72 20 28 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d  r (stored in mem
ecb30 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
ecb40 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c  m.** sqlite3_mal
ecb50 6c 6f 63 29 20 61 6e 64 20 6f 70 65 6e 20 74 68  loc) and open th
ecb60 65 20 66 69 6c 65 20 6e 61 6d 65 64 20 22 70 61  e file named "pa
ecb70 74 68 22 20 69 6e 20 74 68 65 20 66 69 6c 65 20  th" in the file 
ecb80 64 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2a 0a 2a  descriptor..**.*
ecb90 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20  * The caller is 
ecba0 72 65 73 70 6f 6e 73 69 62 6c 65 20 6e 6f 74 20  responsible not 
ecbb0 6f 6e 6c 79 20 66 6f 72 20 63 6c 6f 73 69 6e 67  only for closing
ecbc0 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
ecbd0 70 74 6f 72 0a 2a 2a 20 62 75 74 20 61 6c 73 6f  ptor.** but also
ecbe0 20 66 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65   for freeing the
ecbf0 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74   memory associat
ecc00 65 64 20 77 69 74 68 20 74 68 65 20 66 69 6c 65  ed with the file
ecc10 20 64 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2f 0a   descriptor..*/.
ecc20 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79  static int proxy
ecc30 43 72 65 61 74 65 55 6e 69 78 46 69 6c 65 28 63  CreateUnixFile(c
ecc40 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c  onst char *path,
ecc50 20 75 6e 69 78 46 69 6c 65 20 2a 2a 70 70 46 69   unixFile **ppFi
ecc60 6c 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65  le) {.  unixFile
ecc70 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 66 6c   *pNew;.  int fl
ecc80 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45  ags = SQLITE_OPE
ecc90 4e 5f 4d 41 49 4e 5f 44 42 7c 53 51 4c 49 54 45  N_MAIN_DB|SQLITE
ecca0 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53 51 4c  _OPEN_CREATE|SQL
eccb0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
eccc0 54 45 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  TE;.  int rc = S
eccd0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69  QLITE_OK;.  sqli
ecce0 74 65 33 5f 76 66 73 20 64 75 6d 6d 79 56 66 73  te3_vfs dummyVfs
eccf0 3b 0a 0a 20 20 70 4e 65 77 20 3d 20 28 75 6e 69  ;..  pNew = (uni
ecd00 78 46 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 5f  xFile *)sqlite3_
ecd10 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 75 6e  malloc(sizeof(un
ecd20 69 78 46 69 6c 65 29 29 3b 0a 20 20 69 66 28 20  ixFile));.  if( 
ecd30 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 72 65 74  !pNew ){.    ret
ecd40 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
ecd50 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70  ;.  }.  memset(p
ecd60 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75  New, 0, sizeof(u
ecd70 6e 69 78 46 69 6c 65 29 29 3b 0a 0a 20 20 2f 2a  nixFile));..  /*
ecd80 20 43 61 6c 6c 20 75 6e 69 78 4f 70 65 6e 28 29   Call unixOpen()
ecd90 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 70 72 6f   to open the pro
ecda0 78 79 20 66 69 6c 65 2e 20 54 68 65 20 66 6c 61  xy file. The fla
ecdb0 67 73 20 70 61 73 73 65 64 20 74 6f 20 75 6e 69  gs passed to uni
ecdc0 78 4f 70 65 6e 28 29 0a 20 20 2a 2a 20 73 75 67  xOpen().  ** sug
ecdd0 67 65 73 74 20 74 68 61 74 20 74 68 65 20 66 69  gest that the fi
ecde0 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20  le being opened 
ecdf0 69 73 20 61 20 22 6d 61 69 6e 20 64 61 74 61 62  is a "main datab
ece00 61 73 65 22 2e 20 54 68 69 73 20 69 73 0a 20 20  ase". This is.  
ece10 2a 2a 20 6e 65 63 65 73 73 61 72 79 20 61 73 20  ** necessary as 
ece20 6f 74 68 65 72 20 66 69 6c 65 20 74 79 70 65 73  other file types
ece30 20 64 6f 20 6e 6f 74 20 6e 65 63 65 73 73 61 72   do not necessar
ece40 69 6c 79 20 73 75 70 70 6f 72 74 20 6c 6f 63 6b  ily support lock
ece50 69 6e 67 2e 20 49 74 0a 20 20 2a 2a 20 69 73 20  ing. It.  ** is 
ece60 62 65 74 74 65 72 20 74 6f 20 75 73 65 20 75 6e  better to use un
ece70 69 78 4f 70 65 6e 28 29 20 69 6e 73 74 65 61 64  ixOpen() instead
ece80 20 6f 66 20 6f 70 65 6e 69 6e 67 20 74 68 65 20   of opening the 
ece90 66 69 6c 65 20 64 69 72 65 63 74 6c 79 20 77 69  file directly wi
ecea0 74 68 0a 20 20 2a 2a 20 6f 70 65 6e 28 29 2c 20  th.  ** open(), 
eceb0 61 73 20 75 6e 69 78 4f 70 65 6e 28 29 20 73 65  as unixOpen() se
ecec0 74 73 20 75 70 20 74 68 65 20 76 61 72 69 6f 75  ts up the variou
eced0 73 20 6d 65 63 68 61 6e 69 73 6d 73 20 72 65 71  s mechanisms req
ecee0 75 69 72 65 64 20 74 6f 0a 20 20 2a 2a 20 6d 61  uired to.  ** ma
ecef0 6b 65 20 73 75 72 65 20 61 20 63 61 6c 6c 20 74  ke sure a call t
ecf00 6f 20 63 6c 6f 73 65 28 29 20 64 6f 65 73 20 6e  o close() does n
ecf10 6f 74 20 63 61 75 73 65 20 74 68 65 20 73 79 73  ot cause the sys
ecf20 74 65 6d 20 74 6f 20 64 69 73 63 61 72 64 0a 20  tem to discard. 
ecf30 20 2a 2a 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20   ** POSIX locks 
ecf40 70 72 65 6d 61 74 75 72 65 6c 79 2e 0a 20 20 2a  prematurely..  *
ecf50 2a 0a 20 20 2a 2a 20 49 74 20 69 73 20 69 6d 70  *.  ** It is imp
ecf60 6f 72 74 61 6e 74 20 74 68 61 74 20 74 68 65 20  ortant that the 
ecf70 78 4f 70 65 6e 20 6d 65 6d 62 65 72 20 6f 66 20  xOpen member of 
ecf80 74 68 65 20 56 46 53 20 6f 62 6a 65 63 74 20 70  the VFS object p
ecf90 61 73 73 65 64 20 74 6f 20 0a 20 20 2a 2a 20 75  assed to .  ** u
ecfa0 6e 69 78 4f 70 65 6e 28 29 20 69 73 20 4e 55 4c  nixOpen() is NUL
ecfb0 4c 2e 20 54 68 69 73 20 74 65 6c 6c 73 20 75 6e  L. This tells un
ecfc0 69 78 4f 70 65 6e 28 29 20 6d 61 79 20 74 72 79  ixOpen() may try
ecfd0 20 74 6f 20 6f 70 65 6e 20 61 20 70 72 6f 78 79   to open a proxy
ecfe0 2d 66 69 6c 65 20 0a 20 20 2a 2a 20 66 6f 72 20  -file .  ** for 
ecff0 74 68 65 20 70 72 6f 78 79 2d 66 69 6c 65 20 28  the proxy-file (
ed000 63 72 65 61 74 69 6e 67 20 61 20 70 6f 74 65 6e  creating a poten
ed010 74 69 61 6c 20 69 6e 66 69 6e 69 74 65 20 6c 6f  tial infinite lo
ed020 6f 70 29 2e 0a 20 20 2a 2f 0a 20 20 64 75 6d 6d  op)..  */.  dumm
ed030 79 56 66 73 2e 70 41 70 70 44 61 74 61 20 3d 20  yVfs.pAppData = 
ed040 28 76 6f 69 64 2a 29 26 61 75 74 6f 6c 6f 63 6b  (void*)&autolock
ed050 49 6f 46 69 6e 64 65 72 3b 0a 20 20 64 75 6d 6d  IoFinder;.  dumm
ed060 79 56 66 73 2e 78 4f 70 65 6e 20 3d 20 30 3b 0a  yVfs.xOpen = 0;.
ed070 20 20 72 63 20 3d 20 75 6e 69 78 4f 70 65 6e 28    rc = unixOpen(
ed080 26 64 75 6d 6d 79 56 66 73 2c 20 70 61 74 68 2c  &dummyVfs, path,
ed090 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a   (sqlite3_file *
ed0a0 29 70 4e 65 77 2c 20 66 6c 61 67 73 2c 20 26 66  )pNew, flags, &f
ed0b0 6c 61 67 73 29 3b 0a 20 20 69 66 28 20 72 63 3d  lags);.  if( rc=
ed0c0 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 66  =SQLITE_OK && (f
ed0d0 6c 61 67 73 26 53 51 4c 49 54 45 5f 4f 50 45 4e  lags&SQLITE_OPEN
ed0e0 5f 52 45 41 44 4f 4e 4c 59 29 20 29 7b 0a 20 20  _READONLY) ){.  
ed0f0 20 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 2d    pNew->pMethod-
ed100 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33  >xClose((sqlite3
ed110 5f 66 69 6c 65 20 2a 29 70 4e 65 77 29 3b 0a 20  _file *)pNew);. 
ed120 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43     rc = SQLITE_C
ed130 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 0a 20 20  ANTOPEN;.  }..  
ed140 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
ed150 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  K ){.    sqlite3
ed160 5f 66 72 65 65 28 70 4e 65 77 29 3b 0a 20 20 20  _free(pNew);.   
ed170 20 70 4e 65 77 20 3d 20 30 3b 0a 20 20 7d 0a 0a   pNew = 0;.  }..
ed180 20 20 2a 70 70 46 69 6c 65 20 3d 20 70 4e 65 77    *ppFile = pNew
ed190 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
ed1a0 0a 0a 2f 2a 20 74 61 6b 65 73 20 74 68 65 20 63  ../* takes the c
ed1b0 6f 6e 63 68 20 62 79 20 74 61 6b 69 6e 67 20 61  onch by taking a
ed1c0 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6e 64   shared lock and
ed1d0 20 72 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e   read the conten
ed1e0 74 73 20 63 6f 6e 63 68 2c 20 69 66 20 0a 2a 2a  ts conch, if .**
ed1f0 20 6c 6f 63 6b 50 61 74 68 20 69 73 20 6e 6f 6e   lockPath is non
ed200 2d 4e 55 4c 4c 2c 20 74 68 65 20 68 6f 73 74 20  -NULL, the host 
ed210 49 44 20 61 6e 64 20 6c 6f 63 6b 20 66 69 6c 65  ID and lock file
ed220 20 70 61 74 68 20 6d 75 73 74 20 6d 61 74 63 68   path must match
ed230 2e 20 20 41 20 4e 55 4c 4c 20 0a 2a 2a 20 6c 6f  .  A NULL .** lo
ed240 63 6b 50 61 74 68 20 6d 65 61 6e 73 20 74 68 61  ckPath means tha
ed250 74 20 74 68 65 20 6c 6f 63 6b 50 61 74 68 20 69  t the lockPath i
ed260 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65  n the conch file
ed270 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 69 66   will be used if
ed280 20 74 68 65 20 0a 2a 2a 20 68 6f 73 74 20 49 44   the .** host ID
ed290 73 20 6d 61 74 63 68 2c 20 6f 72 20 61 20 6e 65  s match, or a ne
ed2a0 77 20 6c 6f 63 6b 20 70 61 74 68 20 77 69 6c 6c  w lock path will
ed2b0 20 62 65 20 67 65 6e 65 72 61 74 65 64 20 61 75   be generated au
ed2c0 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20  tomatically .** 
ed2d0 61 6e 64 20 77 72 69 74 74 65 6e 20 74 6f 20 74  and written to t
ed2e0 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a  he conch file..*
ed2f0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f  /.static int pro
ed300 78 79 54 61 6b 65 43 6f 6e 63 68 28 75 6e 69 78  xyTakeConch(unix
ed310 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20  File *pFile){.  
ed320 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
ed330 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f  ext *pCtx = (pro
ed340 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
ed350 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e   *)pFile->lockin
ed360 67 43 6f 6e 74 65 78 74 3b 20 0a 20 20 0a 20 20  gContext; .  .  
ed370 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48  if( pCtx->conchH
ed380 65 6c 64 3e 30 20 29 7b 0a 20 20 20 20 72 65 74  eld>0 ){.    ret
ed390 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
ed3a0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 69 78   }else{.    unix
ed3b0 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 20  File *conchFile 
ed3c0 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c  = pCtx->conchFil
ed3d0 65 3b 0a 20 20 20 20 63 68 61 72 20 74 65 73 74  e;.    char test
ed3e0 56 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b  Value[CONCHLEN];
ed3f0 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 63 68 56  .    char conchV
ed400 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b 0a  alue[CONCHLEN];.
ed410 20 20 20 20 63 68 61 72 20 6c 6f 63 6b 50 61 74      char lockPat
ed420 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 0a 20  h[MAXPATHLEN];. 
ed430 20 20 20 63 68 61 72 20 2a 74 4c 6f 63 6b 50 61     char *tLockPa
ed440 74 68 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69  th = NULL;.    i
ed450 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
ed460 4b 3b 0a 20 20 20 20 69 6e 74 20 72 65 61 64 52  K;.    int readR
ed470 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
ed480 20 20 20 69 6e 74 20 73 79 6e 63 50 65 72 6d 73     int syncPerms
ed490 20 3d 20 30 3b 0a 0a 20 20 20 20 4f 53 54 52 41   = 0;..    OSTRA
ed4a0 43 45 34 28 22 54 41 4b 45 43 4f 4e 43 48 20 20  CE4("TAKECONCH  
ed4b0 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d 25 64  %d for %s pid=%d
ed4c0 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e  \n", conchFile->
ed4d0 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  h,.             
ed4e0 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79  (pCtx->lockProxy
ed4f0 50 61 74 68 20 3f 20 70 43 74 78 2d 3e 6c 6f 63  Path ? pCtx->loc
ed500 6b 50 72 6f 78 79 50 61 74 68 20 3a 20 22 3a 61  kProxyPath : ":a
ed510 75 74 6f 3a 22 29 2c 20 67 65 74 70 69 64 28 29  uto:"), getpid()
ed520 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 63 6f 6e  );..    rc = con
ed530 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d  chFile->pMethod-
ed540 3e 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f  >xLock((sqlite3_
ed550 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c  file*)conchFile,
ed560 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20   SHARED_LOCK);. 
ed570 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
ed580 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e  E_OK ){.      in
ed590 74 20 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20  t pError = 0;.  
ed5a0 20 20 20 20 6d 65 6d 73 65 74 28 74 65 73 74 56      memset(testV
ed5b0 61 6c 75 65 2c 20 30 2c 20 43 4f 4e 43 48 4c 45  alue, 0, CONCHLE
ed5c0 4e 29 3b 20 2f 2a 20 63 6f 6e 63 68 20 69 73 20  N); /* conch is 
ed5d0 66 69 78 65 64 20 73 69 7a 65 20 2a 2f 0a 20 20  fixed size */.  
ed5e0 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 47 65      rc = proxyGe
ed5f0 74 48 6f 73 74 49 44 28 74 65 73 74 56 61 6c 75  tHostID(testValu
ed600 65 2c 20 26 70 45 72 72 6f 72 29 3b 0a 20 20 20  e, &pError);.   
ed610 20 20 20 69 66 28 20 28 72 63 26 30 78 66 66 29     if( (rc&0xff)
ed620 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29  ==SQLITE_IOERR )
ed630 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  {.        pFile-
ed640 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 70 45 72  >lastErrno = pEr
ed650 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ror;.      }.   
ed660 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63     if( pCtx->loc
ed670 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20  kProxyPath ){.  
ed680 20 20 20 20 20 20 73 74 72 6c 63 70 79 28 26 74        strlcpy(&t
ed690 65 73 74 56 61 6c 75 65 5b 48 4f 53 54 49 44 4c  estValue[HOSTIDL
ed6a0 45 4e 5d 2c 20 70 43 74 78 2d 3e 6c 6f 63 6b 50  EN], pCtx->lockP
ed6b0 72 6f 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54  roxyPath, MAXPAT
ed6c0 48 4c 45 4e 29 3b 0a 20 20 20 20 20 20 7d 0a 20  HLEN);.      }. 
ed6d0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21     }.    if( rc!
ed6e0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
ed6f0 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b      goto end_tak
ed700 65 63 6f 6e 63 68 3b 0a 20 20 20 20 7d 0a 20 20  econch;.    }.  
ed710 20 20 0a 20 20 20 20 72 65 61 64 52 63 20 3d 20    .    readRc = 
ed720 75 6e 69 78 52 65 61 64 28 28 73 71 6c 69 74 65  unixRead((sqlite
ed730 33 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69  3_file *)conchFi
ed740 6c 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c 20  le, conchValue, 
ed750 43 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20  CONCHLEN, 0);.  
ed760 20 20 69 66 28 20 72 65 61 64 52 63 21 3d 53 51    if( readRc!=SQ
ed770 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54  LITE_IOERR_SHORT
ed780 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 69  _READ ){.      i
ed790 66 28 20 72 65 61 64 52 63 21 3d 53 51 4c 49 54  f( readRc!=SQLIT
ed7a0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
ed7b0 69 66 28 20 28 72 63 26 30 78 66 66 29 3d 3d 53  if( (rc&0xff)==S
ed7c0 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20  QLITE_IOERR ){. 
ed7d0 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e           pFile->
ed7e0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 63 6f 6e 63  lastErrno = conc
ed7f0 68 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  hFile->lastErrno
ed800 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
ed810 20 20 20 20 72 63 20 3d 20 72 65 61 64 52 63 3b      rc = readRc;
ed820 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e  .        goto en
ed830 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20  d_takeconch;.   
ed840 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 69 66     }.      /* if
ed850 20 74 68 65 20 63 6f 6e 63 68 20 68 61 73 20 64   the conch has d
ed860 61 74 61 20 63 6f 6d 70 61 72 65 20 74 68 65 20  ata compare the 
ed870 63 6f 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 20 20  contents */.    
ed880 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f 63    if( !pCtx->loc
ed890 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20  kProxyPath ){.  
ed8a0 20 20 20 20 20 20 2f 2a 20 66 6f 72 20 61 75 74        /* for aut
ed8b0 6f 2d 6e 61 6d 65 64 20 6c 6f 63 61 6c 20 6c 6f  o-named local lo
ed8c0 63 6b 20 66 69 6c 65 2c 20 6a 75 73 74 20 63 68  ck file, just ch
ed8d0 65 63 6b 20 74 68 65 20 68 6f 73 74 20 49 44 20  eck the host ID 
ed8e0 61 6e 64 20 77 65 27 6c 6c 0a 20 20 20 20 20 20  and we'll.      
ed8f0 20 20 20 2a 2a 20 75 73 65 20 74 68 65 20 6c 6f     ** use the lo
ed900 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61  cal lock file pa
ed910 74 68 20 74 68 61 74 27 73 20 61 6c 72 65 61 64  th that's alread
ed920 79 20 69 6e 20 74 68 65 72 65 20 2a 2f 0a 20 20  y in there */.  
ed930 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63 6d        if( !memcm
ed940 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f 6e  p(testValue, con
ed950 63 68 56 61 6c 75 65 2c 20 48 4f 53 54 49 44 4c  chValue, HOSTIDL
ed960 45 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  EN) ){.         
ed970 20 74 4c 6f 63 6b 50 61 74 68 20 3d 20 28 63 68   tLockPath = (ch
ed980 61 72 20 2a 29 26 63 6f 6e 63 68 56 61 6c 75 65  ar *)&conchValue
ed990 5b 48 4f 53 54 49 44 4c 45 4e 5d 3b 0a 20 20 20  [HOSTIDLEN];.   
ed9a0 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
ed9b0 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 20  takeconch;.     
ed9c0 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
ed9d0 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 77 65 27  {.        /* we'
ed9e0 76 65 20 67 6f 74 20 74 68 65 20 63 6f 6e 63 68  ve got the conch
ed9f0 20 69 66 20 63 6f 6e 63 68 56 61 6c 75 65 20 6d   if conchValue m
eda00 61 74 63 68 65 73 20 6f 75 72 20 70 61 74 68 20  atches our path 
eda10 61 6e 64 20 68 6f 73 74 20 49 44 20 2a 2f 0a 20  and host ID */. 
eda20 20 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63         if( !memc
eda30 6d 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f  mp(testValue, co
eda40 6e 63 68 56 61 6c 75 65 2c 20 43 4f 4e 43 48 4c  nchValue, CONCHL
eda50 45 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  EN) ){.         
eda60 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f   goto end_takeco
eda70 6e 63 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  nch;.        }. 
eda80 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
eda90 7b 0a 20 20 20 20 20 20 2f 2a 20 61 20 73 68 6f  {.      /* a sho
edaa0 72 74 20 72 65 61 64 20 6d 65 61 6e 73 20 77 65  rt read means we
edab0 27 72 65 20 22 63 72 65 61 74 69 6e 67 22 20 74  're "creating" t
edac0 68 65 20 63 6f 6e 63 68 20 28 65 76 65 6e 20 74  he conch (even t
edad0 68 6f 75 67 68 20 69 74 20 63 6f 75 6c 64 20 0a  hough it could .
edae0 20 20 20 20 20 20 2a 2a 20 68 61 76 65 20 62 65        ** have be
edaf0 65 6e 20 75 73 65 72 2d 69 6e 74 65 72 76 65 6e  en user-interven
edb00 74 69 6f 6e 29 2c 20 69 66 20 77 65 20 61 63 71  tion), if we acq
edb10 75 69 72 65 20 74 68 65 20 65 78 63 6c 75 73 69  uire the exclusi
edb20 76 65 20 6c 6f 63 6b 2c 0a 20 20 20 20 20 20 2a  ve lock,.      *
edb30 2a 20 77 65 27 6c 6c 20 74 72 79 20 74 6f 20 6d  * we'll try to m
edb40 61 74 63 68 20 74 68 65 20 63 75 72 72 65 6e 74  atch the current
edb50 20 6f 6e 2d 64 69 73 6b 20 70 65 72 6d 69 73 73   on-disk permiss
edb60 69 6f 6e 73 20 6f 66 20 74 68 65 20 64 61 74 61  ions of the data
edb70 62 61 73 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20  base.      */.  
edb80 20 20 20 20 73 79 6e 63 50 65 72 6d 73 20 3d 20      syncPerms = 
edb90 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20  1;.    }.    .  
edba0 20 20 2f 2a 20 65 69 74 68 65 72 20 63 6f 6e 63    /* either conc
edbb0 68 20 77 61 73 20 65 6d 74 70 79 20 6f 72 20 64  h was emtpy or d
edbc0 69 64 6e 27 74 20 6d 61 74 63 68 20 2a 2f 0a 20  idn't match */. 
edbd0 20 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f     if( !pCtx->lo
edbe0 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20  ckProxyPath ){. 
edbf0 20 20 20 20 20 70 72 6f 78 79 47 65 74 4c 6f 63       proxyGetLoc
edc00 6b 50 61 74 68 28 70 43 74 78 2d 3e 64 62 50 61  kPath(pCtx->dbPa
edc10 74 68 2c 20 6c 6f 63 6b 50 61 74 68 2c 20 4d 41  th, lockPath, MA
edc20 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20 20  XPATHLEN);.     
edc30 20 74 4c 6f 63 6b 50 61 74 68 20 3d 20 6c 6f 63   tLockPath = loc
edc40 6b 50 61 74 68 3b 0a 20 20 20 20 20 20 73 74 72  kPath;.      str
edc50 6c 63 70 79 28 26 74 65 73 74 56 61 6c 75 65 5b  lcpy(&testValue[
edc60 48 4f 53 54 49 44 4c 45 4e 5d 2c 20 6c 6f 63 6b  HOSTIDLEN], lock
edc70 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e  Path, MAXPATHLEN
edc80 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20  );.    }.    .  
edc90 20 20 2f 2a 20 75 70 64 61 74 65 20 63 6f 6e 63    /* update conc
edca0 68 20 77 69 74 68 20 68 6f 73 74 20 61 6e 64 20  h with host and 
edcb0 70 61 74 68 20 28 74 68 69 73 20 77 69 6c 6c 20  path (this will 
edcc0 66 61 69 6c 20 69 66 20 6f 74 68 65 72 20 70 72  fail if other pr
edcd0 6f 63 65 73 73 0a 20 20 20 20 20 2a 2a 20 68 61  ocess.     ** ha
edce0 73 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20  s a shared lock 
edcf0 61 6c 72 65 61 64 79 29 20 2a 2f 0a 20 20 20 20  already) */.    
edd00 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e  rc = conchFile->
edd10 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28  pMethod->xLock((
edd20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f  sqlite3_file*)co
edd30 6e 63 68 46 69 6c 65 2c 20 45 58 43 4c 55 53 49  nchFile, EXCLUSI
edd40 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66  VE_LOCK);.    if
edd50 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
edd60 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 75 6e  ){.      rc = un
edd70 69 78 57 72 69 74 65 28 28 73 71 6c 69 74 65 33  ixWrite((sqlite3
edd80 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69 6c  _file *)conchFil
edd90 65 2c 20 74 65 73 74 56 61 6c 75 65 2c 20 43 4f  e, testValue, CO
edda0 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20  NCHLEN, 0);.    
eddb0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
eddc0 5f 4f 4b 20 26 26 20 73 79 6e 63 50 65 72 6d 73  _OK && syncPerms
eddd0 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72 75   ){.        stru
edde0 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 20  ct stat buf;.   
eddf0 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 66       int err = f
ede00 73 74 61 74 28 70 46 69 6c 65 2d 3e 68 2c 20 26  stat(pFile->h, &
ede10 62 75 66 29 3b 0a 20 20 20 20 20 20 20 20 69 66  buf);.        if
ede20 28 20 65 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20  ( err==0 ){.    
ede30 20 20 20 20 20 20 2f 2a 20 74 72 79 20 74 6f 20        /* try to 
ede40 6d 61 74 63 68 20 74 68 65 20 64 61 74 61 62 61  match the databa
ede50 73 65 20 66 69 6c 65 20 70 65 72 6d 69 73 73 69  se file permissi
ede60 6f 6e 73 2c 20 69 67 6e 6f 72 65 20 66 61 69 6c  ons, ignore fail
ede70 75 72 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ure */.#ifndef S
ede80 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55  QLITE_PROXY_DEBU
ede90 47 0a 20 20 20 20 20 20 20 20 20 20 66 63 68 6d  G.          fchm
edea0 6f 64 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c  od(conchFile->h,
edeb0 20 62 75 66 2e 73 74 5f 6d 6f 64 65 29 3b 0a 23   buf.st_mode);.#
edec0 65 6c 73 65 0a 20 20 20 20 20 20 20 20 20 20 69  else.          i
eded0 66 28 20 66 63 68 6d 6f 64 28 63 6f 6e 63 68 46  f( fchmod(conchF
edee0 69 6c 65 2d 3e 68 2c 20 62 75 66 2e 73 74 5f 6d  ile->h, buf.st_m
edef0 6f 64 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ode)!=0 ){.     
edf00 20 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65 20         int code 
edf10 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20  = errno;.       
edf20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64       fprintf(std
edf30 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f 20  err, "fchmod %o 
edf40 46 41 49 4c 45 44 20 77 69 74 68 20 25 64 20 25  FAILED with %d %
edf50 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  s\n",.          
edf60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
edf70 20 20 20 62 75 66 2e 73 74 5f 6d 6f 64 65 2c 20     buf.st_mode, 
edf80 63 6f 64 65 2c 20 73 74 72 65 72 72 6f 72 28 63  code, strerror(c
edf90 6f 64 65 29 29 3b 0a 20 20 20 20 20 20 20 20 20  ode));.         
edfa0 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20   } else {.      
edfb0 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74        fprintf(st
edfc0 64 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f  derr, "fchmod %o
edfd0 20 53 55 43 43 45 44 45 44 5c 6e 22 2c 62 75 66   SUCCEDED\n",buf
edfe0 2e 73 74 5f 6d 6f 64 65 29 3b 0a 20 20 20 20 20  .st_mode);.     
edff0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
ee000 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
ee010 69 6e 74 20 63 6f 64 65 20 3d 20 65 72 72 6e 6f  int code = errno
ee020 3b 0a 20 20 20 20 20 20 20 20 20 20 66 70 72 69  ;.          fpri
ee030 6e 74 66 28 73 74 64 65 72 72 2c 20 22 53 54 41  ntf(stderr, "STA
ee040 54 20 46 41 49 4c 45 44 5b 25 64 5d 20 77 69 74  T FAILED[%d] wit
ee050 68 20 25 64 20 25 73 5c 6e 22 2c 20 0a 20 20 20  h %d %s\n", .   
ee060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ee070 20 20 20 20 20 20 20 65 72 72 2c 20 63 6f 64 65         err, code
ee080 2c 20 73 74 72 65 72 72 6f 72 28 63 6f 64 65 29  , strerror(code)
ee090 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  );.#endif.      
ee0a0 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
ee0b0 7d 0a 20 20 20 20 63 6f 6e 63 68 46 69 6c 65 2d  }.    conchFile-
ee0c0 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63  >pMethod->xUnloc
ee0d0 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  k((sqlite3_file*
ee0e0 29 63 6f 6e 63 68 46 69 6c 65 2c 20 53 48 41 52  )conchFile, SHAR
ee0f0 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 0a 65 6e 64  ED_LOCK);.  .end
ee100 5f 74 61 6b 65 63 6f 6e 63 68 3a 0a 20 20 20 20  _takeconch:.    
ee110 4f 53 54 52 41 43 45 32 28 22 54 52 41 4e 53 50  OSTRACE2("TRANSP
ee120 52 4f 58 59 3a 20 43 4c 4f 53 45 20 20 25 64 5c  ROXY: CLOSE  %d\
ee130 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20  n", pFile->h);. 
ee140 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
ee150 45 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6f  E_OK && pFile->o
ee160 70 65 6e 46 6c 61 67 73 20 29 7b 0a 20 20 20 20  penFlags ){.    
ee170 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e 3d    if( pFile->h>=
ee180 30 20 29 7b 0a 23 69 66 64 65 66 20 53 54 52 49  0 ){.#ifdef STRI
ee190 43 54 5f 43 4c 4f 53 45 5f 45 52 52 4f 52 0a 20  CT_CLOSE_ERROR. 
ee1a0 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65         if( close
ee1b0 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 20  (pFile->h) ){.  
ee1c0 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c          pFile->l
ee1d0 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  astErrno = errno
ee1e0 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  ;.          retu
ee1f0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
ee200 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20 20 20 7d  CLOSE;.        }
ee210 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 63  .#else.        c
ee220 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 3b 20  lose(pFile->h); 
ee230 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b  /* silently leak
ee240 20 66 64 20 69 66 20 66 61 69 6c 20 2a 2f 0a 23   fd if fail */.#
ee250 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 0a 20 20  endif.      }.  
ee260 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d      pFile->h = -
ee270 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 66 64 20  1;.      int fd 
ee280 3d 20 6f 70 65 6e 28 70 43 74 78 2d 3e 64 62 50  = open(pCtx->dbP
ee290 61 74 68 2c 20 70 46 69 6c 65 2d 3e 6f 70 65 6e  ath, pFile->open
ee2a0 46 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20  Flags,.         
ee2b0 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
ee2c0 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50  E_DEFAULT_FILE_P
ee2d0 45 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 20  ERMISSIONS);.   
ee2e0 20 20 20 4f 53 54 52 41 43 45 32 28 22 54 52 41     OSTRACE2("TRA
ee2f0 4e 53 50 52 4f 58 59 3a 20 4f 50 45 4e 20 20 25  NSPROXY: OPEN  %
ee300 64 5c 6e 22 2c 20 66 64 29 3b 0a 20 20 20 20 20  d\n", fd);.     
ee310 20 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a 20 20   if( fd>=0 ){.  
ee320 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d        pFile->h =
ee330 20 66 64 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65   fd;.      }else
ee340 7b 0a 20 20 20 20 20 20 20 20 72 63 3d 53 51 4c  {.        rc=SQL
ee350 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 20 2f 2a  ITE_CANTOPEN; /*
ee360 20 53 51 4c 49 54 45 5f 42 55 53 59 3f 20 70 72   SQLITE_BUSY? pr
ee370 6f 78 79 54 61 6b 65 43 6f 6e 63 68 20 63 61 6c  oxyTakeConch cal
ee380 6c 65 64 0a 20 20 20 20 20 20 20 20 20 20 20 20  led.            
ee390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ee3a0 20 20 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69 6e     during lockin
ee3b0 67 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20  g */.      }.   
ee3c0 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53   }.    if( rc==S
ee3d0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 43 74  QLITE_OK && !pCt
ee3e0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20 29 7b 0a  x->lockProxy ){.
ee3f0 20 20 20 20 20 20 63 68 61 72 20 2a 70 61 74 68        char *path
ee400 20 3d 20 74 4c 6f 63 6b 50 61 74 68 20 3f 20 74   = tLockPath ? t
ee410 4c 6f 63 6b 50 61 74 68 20 3a 20 70 43 74 78 2d  LockPath : pCtx-
ee420 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a  >lockProxyPath;.
ee430 20 20 20 20 20 20 2f 2a 20 41 43 53 3a 20 4e 65        /* ACS: Ne
ee440 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 63 6f 70  ed to make a cop
ee450 79 20 6f 66 20 70 61 74 68 20 73 6f 6d 65 74 69  y of path someti
ee460 6d 65 73 20 2a 2f 0a 20 20 20 20 20 20 72 63 20  mes */.      rc 
ee470 3d 20 70 72 6f 78 79 43 72 65 61 74 65 55 6e 69  = proxyCreateUni
ee480 78 46 69 6c 65 28 70 61 74 68 2c 20 26 70 43 74  xFile(path, &pCt
ee490 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20  x->lockProxy);. 
ee4a0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d     }.    if( rc=
ee4b0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
ee4c0 20 20 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48      pCtx->conchH
ee4d0 65 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20  eld = 1;..      
ee4e0 69 66 28 20 74 4c 6f 63 6b 50 61 74 68 20 29 7b  if( tLockPath ){
ee4f0 0a 20 20 20 20 20 20 20 20 70 43 74 78 2d 3e 6c  .        pCtx->l
ee500 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3d 20 73  ockProxyPath = s
ee510 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30  qlite3DbStrDup(0
ee520 2c 20 74 4c 6f 63 6b 50 61 74 68 29 3b 0a 20 20  , tLockPath);.  
ee530 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e        if( pCtx->
ee540 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68  lockProxy->pMeth
ee550 6f 64 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68  od == &afpIoMeth
ee560 6f 64 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ods ){.         
ee570 20 28 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e   ((afpLockingCon
ee580 74 65 78 74 20 2a 29 70 43 74 78 2d 3e 6c 6f 63  text *)pCtx->loc
ee590 6b 50 72 6f 78 79 2d 3e 6c 6f 63 6b 69 6e 67 43  kProxy->lockingC
ee5a0 6f 6e 74 65 78 74 29 2d 3e 64 62 50 61 74 68 20  ontext)->dbPath 
ee5b0 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  =.              
ee5c0 20 20 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63         pCtx->loc
ee5d0 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 20 20  kProxyPath;.    
ee5e0 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
ee5f0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20    } else {.     
ee600 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74   conchFile->pMet
ee610 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71  hod->xUnlock((sq
ee620 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63  lite3_file*)conc
ee630 68 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b  hFile, NO_LOCK);
ee640 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 41  .    }.    OSTRA
ee650 43 45 33 28 22 54 41 4b 45 43 4f 4e 43 48 20 20  CE3("TAKECONCH  
ee660 25 64 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 68 46  %d %s\n", conchF
ee670 69 6c 65 2d 3e 68 2c 20 72 63 3d 3d 53 51 4c 49  ile->h, rc==SQLI
ee680 54 45 5f 4f 4b 3f 22 6f 6b 22 3a 22 66 61 69 6c  TE_OK?"ok":"fail
ee690 65 64 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ed");.    return
ee6a0 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a   rc;.  }.}../*.*
ee6b0 2a 20 49 66 20 70 46 69 6c 65 20 68 6f 6c 64 73  * If pFile holds
ee6c0 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 63 6f 6e   a lock on a con
ee6d0 63 68 20 66 69 6c 65 2c 20 74 68 65 6e 20 72 65  ch file, then re
ee6e0 6c 65 61 73 65 20 74 68 61 74 20 6c 6f 63 6b 2e  lease that lock.
ee6f0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
ee700 72 6f 78 79 52 65 6c 65 61 73 65 43 6f 6e 63 68  roxyReleaseConch
ee710 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65  (unixFile *pFile
ee720 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  ){.  int rc;    
ee730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ee740 20 2f 2a 20 53 75 62 72 6f 75 74 69 6e 65 20 72   /* Subroutine r
ee750 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
ee760 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
ee770 65 78 74 20 2a 70 43 74 78 3b 20 20 2f 2a 20 54  ext *pCtx;  /* T
ee780 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65  he locking conte
ee790 78 74 20 66 6f 72 20 74 68 65 20 70 72 6f 78 79  xt for the proxy
ee7a0 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 69 78 46   lock */.  unixF
ee7b0 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 3b 20  ile *conchFile; 
ee7c0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
ee7d0 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65  f the conch file
ee7e0 20 2a 2f 0a 0a 20 20 70 43 74 78 20 3d 20 28 70   */..  pCtx = (p
ee7f0 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
ee800 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b  xt *)pFile->lock
ee810 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 63 6f  ingContext;.  co
ee820 6e 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e  nchFile = pCtx->
ee830 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 4f 53 54  conchFile;.  OST
ee840 52 41 43 45 34 28 22 52 45 4c 45 41 53 45 43 4f  RACE4("RELEASECO
ee850 4e 43 48 20 20 25 64 20 66 6f 72 20 25 73 20 70  NCH  %d for %s p
ee860 69 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63 68 46  id=%d\n", conchF
ee870 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20  ile->h,.        
ee880 20 20 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72     (pCtx->lockPr
ee890 6f 78 79 50 61 74 68 20 3f 20 70 43 74 78 2d 3e  oxyPath ? pCtx->
ee8a0 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3a 20  lockProxyPath : 
ee8b0 22 3a 61 75 74 6f 3a 22 29 2c 20 0a 20 20 20 20  ":auto:"), .    
ee8c0 20 20 20 20 20 20 20 67 65 74 70 69 64 28 29 29         getpid())
ee8d0 3b 0a 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48  ;.  pCtx->conchH
ee8e0 65 6c 64 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20  eld = 0;.  rc = 
ee8f0 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68  conchFile->pMeth
ee900 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c  od->xUnlock((sql
ee910 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68  ite3_file*)conch
ee920 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a  File, NO_LOCK);.
ee930 20 20 4f 53 54 52 41 43 45 33 28 22 52 45 4c 45    OSTRACE3("RELE
ee940 41 53 45 43 4f 4e 43 48 20 20 25 64 20 25 73 5c  ASECONCH  %d %s\
ee950 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68  n", conchFile->h
ee960 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28 72 63  ,.           (rc
ee970 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f  ==SQLITE_OK ? "o
ee980 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29 3b  k" : "failed"));
ee990 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
ee9a0 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65  ./*.** Given the
ee9b0 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61 62   name of a datab
ee9c0 61 73 65 20 66 69 6c 65 2c 20 63 6f 6d 70 75 74  ase file, comput
ee9d0 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 69 74  e the name of it
ee9e0 73 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a 2a  s conch file..**
ee9f0 20 53 74 6f 72 65 20 74 68 65 20 63 6f 6e 63 68   Store the conch
eea00 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 6d 65 6d   filename in mem
eea10 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
eea20 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  m sqlite3_malloc
eea30 28 29 2e 0a 2a 2a 20 4d 61 6b 65 20 2a 70 43 6f  ()..** Make *pCo
eea40 6e 63 68 50 61 74 68 20 70 6f 69 6e 74 20 74 6f  nchPath point to
eea50 20 74 68 65 20 6e 65 77 20 6e 61 6d 65 2e 20 20   the new name.  
eea60 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
eea70 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f   on success.** o
eea80 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69  r SQLITE_NOMEM i
eea90 66 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61  f unable to obta
eeaa0 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a  in memory..**.**
eeab0 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72   The caller is r
eeac0 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 65  esponsible for e
eead0 6e 73 75 72 69 6e 67 20 74 68 61 74 20 74 68 65  nsuring that the
eeae0 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72   allocated memor
eeaf0 79 0a 2a 2a 20 73 70 61 63 65 20 69 73 20 65 76  y.** space is ev
eeb00 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 2e 0a  entually freed..
eeb10 2a 2a 0a 2a 2a 20 2a 70 43 6f 6e 63 68 50 61 74  **.** *pConchPat
eeb20 68 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c  h is set to NULL
eeb30 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c   if a memory all
eeb40 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63  ocation error oc
eeb50 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  curs..*/.static 
eeb60 69 6e 74 20 70 72 6f 78 79 43 72 65 61 74 65 43  int proxyCreateC
eeb70 6f 6e 63 68 50 61 74 68 6e 61 6d 65 28 63 68 61  onchPathname(cha
eeb80 72 20 2a 64 62 50 61 74 68 2c 20 63 68 61 72 20  r *dbPath, char 
eeb90 2a 2a 70 43 6f 6e 63 68 50 61 74 68 29 7b 0a 20  **pConchPath){. 
eeba0 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
eebb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
eebc0 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
eebd0 2f 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 28 69  /.  int len = (i
eebe0 6e 74 29 73 74 72 6c 65 6e 28 64 62 50 61 74 68  nt)strlen(dbPath
eebf0 29 3b 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20  ); /* Length of 
eec00 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d  database filenam
eec10 65 20 2d 20 64 62 50 61 74 68 20 2a 2f 0a 20 20  e - dbPath */.  
eec20 63 68 61 72 20 2a 63 6f 6e 63 68 50 61 74 68 3b  char *conchPath;
eec30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
eec40 20 62 75 66 66 65 72 20 69 6e 20 77 68 69 63 68   buffer in which
eec50 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 63 6f   to construct co
eec60 6e 63 68 20 6e 61 6d 65 20 2a 2f 0a 0a 20 20 2f  nch name */..  /
eec70 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65  * Allocate space
eec80 20 66 6f 72 20 74 68 65 20 63 6f 6e 63 68 20 66   for the conch f
eec90 69 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6e 69 74  ilename and init
eeca0 69 61 6c 69 7a 65 20 74 68 65 20 6e 61 6d 65 20  ialize the name 
eecb0 74 6f 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65  to.  ** the name
eecc0 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   of the original
eecd0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
eece0 2a 2f 20 20 0a 20 20 2a 70 43 6f 6e 63 68 50 61  */  .  *pConchPa
eecf0 74 68 20 3d 20 63 6f 6e 63 68 50 61 74 68 20 3d  th = conchPath =
eed00 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33   (char *)sqlite3
eed10 5f 6d 61 6c 6c 6f 63 28 6c 65 6e 20 2b 20 38 29  _malloc(len + 8)
eed20 3b 0a 20 20 69 66 28 20 63 6f 6e 63 68 50 61 74  ;.  if( conchPat
eed30 68 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  h==0 ){.    retu
eed40 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
eed50 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 63 6f  .  }.  memcpy(co
eed60 6e 63 68 50 61 74 68 2c 20 64 62 50 61 74 68 2c  nchPath, dbPath,
eed70 20 6c 65 6e 2b 31 29 3b 0a 20 20 0a 20 20 2f 2a   len+1);.  .  /*
eed80 20 6e 6f 77 20 69 6e 73 65 72 74 20 61 20 22 2e   now insert a ".
eed90 22 20 62 65 66 6f 72 65 20 74 68 65 20 6c 61 73  " before the las
eeda0 74 20 2f 20 63 68 61 72 61 63 74 65 72 20 2a 2f  t / character */
eedb0 0a 20 20 66 6f 72 28 20 69 3d 28 6c 65 6e 2d 31  .  for( i=(len-1
eedc0 29 3b 20 69 3e 3d 30 3b 20 69 2d 2d 20 29 7b 0a  ); i>=0; i-- ){.
eedd0 20 20 20 20 69 66 28 20 63 6f 6e 63 68 50 61 74      if( conchPat
eede0 68 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20  h[i]=='/' ){.   
eedf0 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 62 72     i++;.      br
eee00 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
eee10 20 63 6f 6e 63 68 50 61 74 68 5b 69 5d 3d 27 2e   conchPath[i]='.
eee20 27 3b 0a 20 20 77 68 69 6c 65 20 28 20 69 3c 6c  ';.  while ( i<l
eee30 65 6e 20 29 7b 0a 20 20 20 20 63 6f 6e 63 68 50  en ){.    conchP
eee40 61 74 68 5b 69 2b 31 5d 3d 64 62 50 61 74 68 5b  ath[i+1]=dbPath[
eee50 69 5d 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d  i];.    i++;.  }
eee60 0a 0a 20 20 2f 2a 20 61 70 70 65 6e 64 20 74 68  ..  /* append th
eee70 65 20 22 2d 63 6f 6e 63 68 22 20 73 75 66 66 69  e "-conch" suffi
eee80 78 20 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f  x to the file */
eee90 0a 20 20 6d 65 6d 63 70 79 28 26 63 6f 6e 63 68  .  memcpy(&conch
eeea0 50 61 74 68 5b 69 2b 31 5d 2c 20 22 2d 63 6f 6e  Path[i+1], "-con
eeeb0 63 68 22 2c 20 37 29 3b 0a 20 20 61 73 73 65 72  ch", 7);.  asser
eeec0 74 28 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 63  t( (int)strlen(c
eeed0 6f 6e 63 68 50 61 74 68 29 20 3d 3d 20 6c 65 6e  onchPath) == len
eeee0 2b 37 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  +7 );..  return 
eeef0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f  SQLITE_OK;.}.../
eef00 2a 20 54 61 6b 65 73 20 61 20 66 75 6c 6c 79 20  * Takes a fully 
eef10 63 6f 6e 66 69 67 75 72 65 64 20 70 72 6f 78 79  configured proxy
eef20 20 6c 6f 63 6b 69 6e 67 2d 73 74 79 6c 65 20 75   locking-style u
eef30 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 73 77 69  nix file and swi
eef40 74 63 68 65 73 0a 2a 2a 20 74 68 65 20 6c 6f 63  tches.** the loc
eef50 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74  al lock file pat
eef60 68 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  h .*/.static int
eef70 20 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79   switchLockProxy
eef80 50 61 74 68 28 75 6e 69 78 46 69 6c 65 20 2a 70  Path(unixFile *p
eef90 46 69 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  File, const char
eefa0 20 2a 70 61 74 68 29 20 7b 0a 20 20 70 72 6f 78   *path) {.  prox
eefb0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
eefc0 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f  *pCtx = (proxyLo
eefd0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46  ckingContext*)pF
eefe0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
eeff0 65 78 74 3b 0a 20 20 63 68 61 72 20 2a 6f 6c 64  ext;.  char *old
ef000 50 61 74 68 20 3d 20 70 43 74 78 2d 3e 6c 6f 63  Path = pCtx->loc
ef010 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 69 6e  kProxyPath;.  in
ef020 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
ef030 3b 0a 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  ;..  if( pFile->
ef040 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43  locktype!=NO_LOC
ef050 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
ef060 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d  SQLITE_BUSY;.  }
ef070 20 20 0a 0a 20 20 2f 2a 20 6e 6f 74 68 69 6e 67    ..  /* nothing
ef080 20 74 6f 20 64 6f 20 69 66 20 74 68 65 20 70 61   to do if the pa
ef090 74 68 20 69 73 20 4e 55 4c 4c 2c 20 3a 61 75 74  th is NULL, :aut
ef0a0 6f 3a 20 6f 72 20 6d 61 74 63 68 65 73 20 74 68  o: or matches th
ef0b0 65 20 65 78 69 73 74 69 6e 67 20 70 61 74 68 20  e existing path 
ef0c0 2a 2f 0a 20 20 69 66 28 20 21 70 61 74 68 20 7c  */.  if( !path |
ef0d0 7c 20 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20  | path[0]=='\0' 
ef0e0 7c 7c 20 21 73 74 72 63 6d 70 28 70 61 74 68 2c  || !strcmp(path,
ef0f0 20 22 3a 61 75 74 6f 3a 22 29 20 7c 7c 0a 20 20   ":auto:") ||.  
ef100 20 20 28 6f 6c 64 50 61 74 68 20 26 26 20 21 73    (oldPath && !s
ef110 74 72 6e 63 6d 70 28 6f 6c 64 50 61 74 68 2c 20  trncmp(oldPath, 
ef120 70 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e  path, MAXPATHLEN
ef130 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  )) ){.    return
ef140 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65   SQLITE_OK;.  }e
ef150 6c 73 65 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c  lse{.    unixFil
ef160 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70  e *lockProxy = p
ef170 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a  Ctx->lockProxy;.
ef180 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72      pCtx->lockPr
ef190 6f 78 79 3d 4e 55 4c 4c 3b 0a 20 20 20 20 70 43  oxy=NULL;.    pC
ef1a0 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 3d 20  tx->conchHeld = 
ef1b0 30 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 50  0;.    if( lockP
ef1c0 72 6f 78 79 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20  roxy!=NULL ){.  
ef1d0 20 20 20 20 72 63 3d 6c 6f 63 6b 50 72 6f 78 79      rc=lockProxy
ef1e0 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73  ->pMethod->xClos
ef1f0 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  e((sqlite3_file 
ef200 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20  *)lockProxy);.  
ef210 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
ef220 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 71  urn rc;.      sq
ef230 6c 69 74 65 33 5f 66 72 65 65 28 6c 6f 63 6b 50  lite3_free(lockP
ef240 72 6f 78 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20  roxy);.    }.   
ef250 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6f 6c   sqlite3_free(ol
ef260 64 50 61 74 68 29 3b 0a 20 20 20 20 70 43 74 78  dPath);.    pCtx
ef270 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20  ->lockProxyPath 
ef280 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
ef290 70 28 30 2c 20 70 61 74 68 29 3b 0a 20 20 7d 0a  p(0, path);.  }.
ef2a0 20 20 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a    .  return rc;.
ef2b0 7d 0a 0a 2f 2a 0a 2a 2a 20 70 46 69 6c 65 20 69  }../*.** pFile i
ef2c0 73 20 61 20 66 69 6c 65 20 74 68 61 74 20 68 61  s a file that ha
ef2d0 73 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62 79  s been opened by
ef2e0 20 61 20 70 72 69 6f 72 20 78 4f 70 65 6e 20 63   a prior xOpen c
ef2f0 61 6c 6c 2e 20 20 64 62 50 61 74 68 0a 2a 2a 20  all.  dbPath.** 
ef300 69 73 20 61 20 73 74 72 69 6e 67 20 62 75 66 66  is a string buff
ef310 65 72 20 61 74 20 6c 65 61 73 74 20 4d 41 58 50  er at least MAXP
ef320 41 54 48 4c 45 4e 2b 31 20 63 68 61 72 61 63 74  ATHLEN+1 charact
ef330 65 72 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a  ers in size..**.
ef340 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
ef350 66 69 6e 64 20 74 68 65 20 66 69 6c 65 6e 61 6d  find the filenam
ef360 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
ef370 68 20 70 46 69 6c 65 20 61 6e 64 20 77 72 69 74  h pFile and writ
ef380 65 73 20 69 74 0a 2a 2a 20 69 6e 74 20 64 62 50  es it.** int dbP
ef390 61 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ath..*/.static i
ef3a0 6e 74 20 70 72 6f 78 79 47 65 74 44 62 50 61 74  nt proxyGetDbPat
ef3b0 68 46 6f 72 55 6e 69 78 46 69 6c 65 28 75 6e 69  hForUnixFile(uni
ef3c0 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 63 68  xFile *pFile, ch
ef3d0 61 72 20 2a 64 62 50 61 74 68 29 7b 0a 23 69 66  ar *dbPath){.#if
ef3e0 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45   defined(__APPLE
ef3f0 5f 5f 29 0a 20 20 69 66 28 20 70 46 69 6c 65 2d  __).  if( pFile-
ef400 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 61 66 70  >pMethod == &afp
ef410 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20  IoMethods ){.   
ef420 20 2f 2a 20 61 66 70 20 73 74 79 6c 65 20 6b 65   /* afp style ke
ef430 65 70 73 20 61 20 72 65 66 65 72 65 6e 63 65 20  eps a reference 
ef440 74 6f 20 74 68 65 20 64 62 20 70 61 74 68 20 69  to the db path i
ef450 6e 20 74 68 65 20 66 69 6c 65 50 61 74 68 20 66  n the filePath f
ef460 69 65 6c 64 20 0a 20 20 20 20 2a 2a 20 6f 66 20  ield .    ** of 
ef470 74 68 65 20 73 74 72 75 63 74 20 2a 2f 0a 20 20  the struct */.  
ef480 20 20 61 73 73 65 72 74 28 20 28 69 6e 74 29 73    assert( (int)s
ef490 74 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69  trlen((char*)pFi
ef4a0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
ef4b0 78 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20  xt)<=MAXPATHLEN 
ef4c0 29 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64 62  );.    strcpy(db
ef4d0 50 61 74 68 2c 20 28 28 61 66 70 4c 6f 63 6b 69  Path, ((afpLocki
ef4e0 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c  ngContext *)pFil
ef4f0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
ef500 74 29 2d 3e 64 62 50 61 74 68 29 3b 0a 20 20 7d  t)->dbPath);.  }
ef510 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 69 66  else.#endif.  if
ef520 28 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64  ( pFile->pMethod
ef530 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65   == &dotlockIoMe
ef540 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20  thods ){.    /* 
ef550 64 6f 74 20 6c 6f 63 6b 20 73 74 79 6c 65 20 75  dot lock style u
ef560 73 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  ses the locking 
ef570 63 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f 72 65  context to store
ef580 20 74 68 65 20 64 6f 74 20 6c 6f 63 6b 0a 20 20   the dot lock.  
ef590 20 20 2a 2a 20 66 69 6c 65 20 70 61 74 68 20 2a    ** file path *
ef5a0 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20  /.    int len = 
ef5b0 73 74 72 6c 65 6e 28 28 63 68 61 72 20 2a 29 70  strlen((char *)p
ef5c0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
ef5d0 74 65 78 74 29 20 2d 20 73 74 72 6c 65 6e 28 44  text) - strlen(D
ef5e0 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 29 3b 0a  OTLOCK_SUFFIX);.
ef5f0 20 20 20 20 6d 65 6d 63 70 79 28 64 62 50 61 74      memcpy(dbPat
ef600 68 2c 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65  h, (char *)pFile
ef610 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
ef620 2c 20 6c 65 6e 20 2b 20 31 29 3b 0a 20 20 7d 65  , len + 1);.  }e
ef630 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 61 6c 6c 20  lse{.    /* all 
ef640 6f 74 68 65 72 20 73 74 79 6c 65 73 20 75 73 65  other styles use
ef650 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e   the locking con
ef660 74 65 78 74 20 74 6f 20 73 74 6f 72 65 20 74 68  text to store th
ef670 65 20 64 62 20 66 69 6c 65 20 70 61 74 68 20 2a  e db file path *
ef680 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 74  /.    assert( st
ef690 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69 6c  rlen((char*)pFil
ef6a0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
ef6b0 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20 29  t)<=MAXPATHLEN )
ef6c0 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64 62 50  ;.    strcpy(dbP
ef6d0 61 74 68 2c 20 28 63 68 61 72 20 2a 29 70 46 69  ath, (char *)pFi
ef6e0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
ef6f0 78 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  xt);.  }.  retur
ef700 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
ef710 2f 2a 0a 2a 2a 20 54 61 6b 65 73 20 61 6e 20 61  /*.** Takes an a
ef720 6c 72 65 61 64 79 20 66 69 6c 6c 65 64 20 69 6e  lready filled in
ef730 20 75 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 61   unix file and a
ef740 6c 74 65 72 73 20 69 74 20 73 6f 20 61 6c 6c 20  lters it so all 
ef750 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a  file locking .**
ef760 20 77 69 6c 6c 20 62 65 20 70 65 72 66 6f 72 6d   will be perform
ef770 65 64 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20  ed on the local 
ef780 70 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 2e  proxy lock file.
ef790 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
ef7a0 66 69 65 6c 64 73 0a 2a 2a 20 61 72 65 20 70 72  fields.** are pr
ef7b0 65 73 65 72 76 65 64 20 69 6e 20 74 68 65 20 6c  eserved in the l
ef7c0 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 73  ocking context s
ef7d0 6f 20 74 68 61 74 20 74 68 65 79 20 63 61 6e 20  o that they can 
ef7e0 62 65 20 72 65 73 74 6f 72 65 64 20 61 6e 64 20  be restored and 
ef7f0 0a 2a 2a 20 74 68 65 20 75 6e 69 78 20 73 74 72  .** the unix str
ef800 75 63 74 75 72 65 20 70 72 6f 70 65 72 6c 79 20  ucture properly 
ef810 63 6c 65 61 6e 65 64 20 75 70 20 61 74 20 63 6c  cleaned up at cl
ef820 6f 73 65 20 74 69 6d 65 3a 0a 2a 2a 20 20 2d 3e  ose time:.**  ->
ef830 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 0a 2a  lockingContext.*
ef840 2a 20 20 2d 3e 70 4d 65 74 68 6f 64 0a 2a 2f 0a  *  ->pMethod.*/.
ef850 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79  static int proxy
ef860 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c  TransformUnixFil
ef870 65 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c  e(unixFile *pFil
ef880 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  e, const char *p
ef890 61 74 68 29 20 7b 0a 20 20 70 72 6f 78 79 4c 6f  ath) {.  proxyLo
ef8a0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43  ckingContext *pC
ef8b0 74 78 3b 0a 20 20 63 68 61 72 20 64 62 50 61 74  tx;.  char dbPat
ef8c0 68 5b 4d 41 58 50 41 54 48 4c 45 4e 2b 31 5d 3b  h[MAXPATHLEN+1];
ef8d0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
ef8e0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
ef8f0 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 6c  ile */.  char *l
ef900 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20 20  ockPath=NULL;.  
ef910 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
ef920 4f 4b 3b 0a 20 20 0a 20 20 69 66 28 20 70 46 69  OK;.  .  if( pFi
ef930 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f  le->locktype!=NO
ef940 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 74  _LOCK ){.    ret
ef950 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  urn SQLITE_BUSY;
ef960 0a 20 20 7d 0a 20 20 70 72 6f 78 79 47 65 74 44  .  }.  proxyGetD
ef970 62 50 61 74 68 46 6f 72 55 6e 69 78 46 69 6c 65  bPathForUnixFile
ef980 28 70 46 69 6c 65 2c 20 64 62 50 61 74 68 29 3b  (pFile, dbPath);
ef990 0a 20 20 69 66 28 20 21 70 61 74 68 20 7c 7c 20  .  if( !path || 
ef9a0 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 7c 7c  path[0]=='\0' ||
ef9b0 20 21 73 74 72 63 6d 70 28 70 61 74 68 2c 20 22   !strcmp(path, "
ef9c0 3a 61 75 74 6f 3a 22 29 20 29 7b 0a 20 20 20 20  :auto:") ){.    
ef9d0 6c 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20  lockPath=NULL;. 
ef9e0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f 63 6b   }else{.    lock
ef9f0 50 61 74 68 3d 28 63 68 61 72 20 2a 29 70 61 74  Path=(char *)pat
efa00 68 3b 0a 20 20 7d 0a 20 20 0a 20 20 4f 53 54 52  h;.  }.  .  OSTR
efa10 41 43 45 34 28 22 54 52 41 4e 53 50 52 4f 58 59  ACE4("TRANSPROXY
efa20 20 20 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d    %d for %s pid=
efa30 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
efa40 0a 20 20 20 20 20 20 20 20 20 20 20 28 6c 6f 63  .           (loc
efa50 6b 50 61 74 68 20 3f 20 6c 6f 63 6b 50 61 74 68  kPath ? lockPath
efa60 20 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 67 65   : ":auto:"), ge
efa70 74 70 69 64 28 29 29 3b 0a 0a 20 20 70 43 74 78  tpid());..  pCtx
efa80 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
efa90 63 28 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29  c( sizeof(*pCtx)
efaa0 20 29 3b 0a 20 20 69 66 28 20 70 43 74 78 3d 3d   );.  if( pCtx==
efab0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
efac0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
efad0 7d 0a 20 20 6d 65 6d 73 65 74 28 70 43 74 78 2c  }.  memset(pCtx,
efae0 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43 74 78   0, sizeof(*pCtx
efaf0 29 29 3b 0a 0a 20 20 72 63 20 3d 20 70 72 6f 78  ));..  rc = prox
efb00 79 43 72 65 61 74 65 43 6f 6e 63 68 50 61 74 68  yCreateConchPath
efb10 6e 61 6d 65 28 64 62 50 61 74 68 2c 20 26 70 43  name(dbPath, &pC
efb20 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74  tx->conchFilePat
efb30 68 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  h);.  if( rc==SQ
efb40 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
efb50 63 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65 55  c = proxyCreateU
efb60 6e 69 78 46 69 6c 65 28 70 43 74 78 2d 3e 63 6f  nixFile(pCtx->co
efb70 6e 63 68 46 69 6c 65 50 61 74 68 2c 20 26 70 43  nchFilePath, &pC
efb80 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29 3b 0a  tx->conchFile);.
efb90 20 20 7d 20 20 0a 20 20 69 66 28 20 72 63 3d 3d    }  .  if( rc==
efba0 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6c 6f 63  SQLITE_OK && loc
efbb0 6b 50 61 74 68 20 29 7b 0a 20 20 20 20 70 43 74  kPath ){.    pCt
efbc0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68  x->lockProxyPath
efbd0 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
efbe0 75 70 28 30 2c 20 6c 6f 63 6b 50 61 74 68 29 3b  up(0, lockPath);
efbf0 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d  .  }..  if( rc==
efc00 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
efc10 20 2f 2a 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 69   /* all memory i
efc20 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 70 72 6f  s allocated, pro
efc30 78 79 73 20 61 72 65 20 63 72 65 61 74 65 64 20  xys are created 
efc40 61 6e 64 20 61 73 73 69 67 6e 65 64 2c 20 0a 20  and assigned, . 
efc50 20 20 20 2a 2a 20 73 77 69 74 63 68 20 74 68 65     ** switch the
efc60 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74   locking context
efc70 20 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 68 65   and pMethod the
efc80 6e 20 72 65 74 75 72 6e 2e 0a 20 20 20 20 2a 2f  n return..    */
efc90 0a 20 20 20 20 70 43 74 78 2d 3e 64 62 50 61 74  .    pCtx->dbPat
efca0 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  h = sqlite3DbStr
efcb0 44 75 70 28 30 2c 20 64 62 50 61 74 68 29 3b 0a  Dup(0, dbPath);.
efcc0 20 20 20 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63      pCtx->oldLoc
efcd0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 46  kingContext = pF
efce0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
efcf0 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  ext;.    pFile->
efd00 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d  lockingContext =
efd10 20 70 43 74 78 3b 0a 20 20 20 20 70 43 74 78 2d   pCtx;.    pCtx-
efd20 3e 70 4f 6c 64 4d 65 74 68 6f 64 20 3d 20 70 46  >pOldMethod = pF
efd30 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 3b 0a 20 20  ile->pMethod;.  
efd40 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64    pFile->pMethod
efd50 20 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f   = &proxyIoMetho
efd60 64 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ds;.  }else{.   
efd70 20 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68   if( pCtx->conch
efd80 46 69 6c 65 20 29 7b 20 0a 20 20 20 20 20 20 72  File ){ .      r
efd90 63 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46  c = pCtx->conchF
efda0 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43  ile->pMethod->xC
efdb0 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69  lose((sqlite3_fi
efdc0 6c 65 20 2a 29 70 43 74 78 2d 3e 63 6f 6e 63 68  le *)pCtx->conch
efdd0 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 66 28  File);.      if(
efde0 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
efdf0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
efe00 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46  ree(pCtx->conchF
efe10 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ile);.    }.    
efe20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74  sqlite3_free(pCt
efe30 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68  x->conchFilePath
efe40 29 3b 20 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ); .    sqlite3_
efe50 66 72 65 65 28 70 43 74 78 29 3b 0a 20 20 7d 0a  free(pCtx);.  }.
efe60 20 20 4f 53 54 52 41 43 45 33 28 22 54 52 41 4e    OSTRACE3("TRAN
efe70 53 50 52 4f 58 59 20 20 25 64 20 25 73 5c 6e 22  SPROXY  %d %s\n"
efe80 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20  , pFile->h,.    
efe90 20 20 20 20 20 20 20 28 72 63 3d 3d 53 51 4c 49         (rc==SQLI
efea0 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22  TE_OK ? "ok" : "
efeb0 66 61 69 6c 65 64 22 29 29 3b 0a 20 20 72 65 74  failed"));.  ret
efec0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  urn rc;.}.../*.*
efed0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68  * This routine h
efee0 61 6e 64 6c 65 73 20 73 71 6c 69 74 65 33 5f 66  andles sqlite3_f
efef0 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 20 63 61  ile_control() ca
eff00 6c 6c 73 20 74 68 61 74 20 61 72 65 20 73 70 65  lls that are spe
eff10 63 69 66 69 63 0a 2a 2a 20 74 6f 20 70 72 6f 78  cific.** to prox
eff20 79 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74  y locking..*/.st
eff30 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 46 69  atic int proxyFi
eff40 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65  leControl(sqlite
eff50 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
eff60 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b  op, void *pArg){
eff70 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b  .  switch( op ){
eff80 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
eff90 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49  _GET_LOCKPROXYFI
effa0 4c 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e 69 78  LE: {.      unix
effb0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
effc0 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20  nixFile*)id;.   
effd0 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d     if( pFile->pM
effe0 65 74 68 6f 64 20 3d 3d 20 26 70 72 6f 78 79 49  ethod == &proxyI
efff0 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20  oMethods ){.    
f0000 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67      proxyLocking
f0010 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20  Context *pCtx = 
f0020 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e  (proxyLockingCon
f0030 74 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63  text*)pFile->loc
f0040 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20  kingContext;.   
f0050 20 20 20 20 20 70 72 6f 78 79 54 61 6b 65 43 6f       proxyTakeCo
f0060 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 20 20  nch(pFile);.    
f0070 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f      if( pCtx->lo
f0080 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20  ckProxyPath ){. 
f0090 20 20 20 20 20 20 20 20 20 2a 28 63 6f 6e 73 74           *(const
f00a0 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20 3d 20   char **)pArg = 
f00b0 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50  pCtx->lockProxyP
f00c0 61 74 68 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  ath;.        }el
f00d0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28  se{.          *(
f00e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 70 41  const char **)pA
f00f0 72 67 20 3d 20 22 3a 61 75 74 6f 3a 20 28 6e 6f  rg = ":auto: (no
f0100 74 20 68 65 6c 64 29 22 3b 0a 20 20 20 20 20 20  t held)";.      
f0110 20 20 7d 0a 20 20 20 20 20 20 7d 20 65 6c 73 65    }.      } else
f0120 20 7b 0a 20 20 20 20 20 20 20 20 2a 28 63 6f 6e   {.        *(con
f0130 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20  st char **)pArg 
f0140 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a  = NULL;.      }.
f0150 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
f0160 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20  ITE_OK;.    }.  
f0170 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45    case SQLITE_SE
f0180 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a  T_LOCKPROXYFILE:
f0190 20 7b 0a 20 20 20 20 20 20 75 6e 69 78 46 69 6c   {.      unixFil
f01a0 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
f01b0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 20 20  File*)id;.      
f01c0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
f01d0 4f 4b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 73  OK;.      int is
f01e0 50 72 6f 78 79 53 74 79 6c 65 20 3d 20 28 70 46  ProxyStyle = (pF
f01f0 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20  ile->pMethod == 
f0200 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73 29  &proxyIoMethods)
f0210 3b 0a 20 20 20 20 20 20 69 66 28 20 70 41 72 67  ;.      if( pArg
f0220 3d 3d 4e 55 4c 4c 20 7c 7c 20 28 63 6f 6e 73 74  ==NULL || (const
f0230 20 63 68 61 72 20 2a 29 70 41 72 67 3d 3d 30 20   char *)pArg==0 
f0240 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  ){.        if( i
f0250 73 50 72 6f 78 79 53 74 79 6c 65 20 29 7b 0a 20  sProxyStyle ){. 
f0260 20 20 20 20 20 20 20 20 20 2f 2a 20 74 75 72 6e           /* turn
f0270 20 6f 66 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69   off proxy locki
f0280 6e 67 20 2d 20 6e 6f 74 20 73 75 70 70 6f 72 74  ng - not support
f0290 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  ed */.          
f02a0 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
f02b0 52 20 2f 2a 53 51 4c 49 54 45 5f 50 52 4f 54 4f  R /*SQLITE_PROTO
f02c0 43 4f 4c 3f 20 53 51 4c 49 54 45 5f 4d 49 53 55  COL? SQLITE_MISU
f02d0 53 45 3f 2a 2f 3b 0a 20 20 20 20 20 20 20 20 7d  SE?*/;.        }
f02e0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
f02f0 2f 2a 20 74 75 72 6e 20 6f 66 66 20 70 72 6f 78  /* turn off prox
f0300 79 20 6c 6f 63 6b 69 6e 67 20 2d 20 61 6c 72 65  y locking - alre
f0310 61 64 79 20 6f 66 66 20 2d 20 4e 4f 4f 50 20 2a  ady off - NOOP *
f0320 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  /.          rc =
f0330 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
f0340 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
f0350 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74  e{.        const
f0360 20 63 68 61 72 20 2a 70 72 6f 78 79 50 61 74 68   char *proxyPath
f0370 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
f0380 29 70 41 72 67 3b 0a 20 20 20 20 20 20 20 20 69  )pArg;.        i
f0390 66 28 20 69 73 50 72 6f 78 79 53 74 79 6c 65 20  f( isProxyStyle 
f03a0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 6f  ){.          pro
f03b0 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
f03c0 20 2a 70 43 74 78 20 3d 20 0a 20 20 20 20 20 20   *pCtx = .      
f03d0 20 20 20 20 20 20 28 70 72 6f 78 79 4c 6f 63 6b        (proxyLock
f03e0 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 69 6c  ingContext*)pFil
f03f0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
f0400 74 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  t;.          if(
f0410 20 21 73 74 72 63 6d 70 28 70 41 72 67 2c 20 22   !strcmp(pArg, "
f0420 3a 61 75 74 6f 3a 22 29 20 0a 20 20 20 20 20 20  :auto:") .      
f0430 20 20 20 20 20 7c 7c 20 28 70 43 74 78 2d 3e 6c       || (pCtx->l
f0440 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 26 26 0a  ockProxyPath &&.
f0450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 21                 !
f0460 73 74 72 6e 63 6d 70 28 70 43 74 78 2d 3e 6c 6f  strncmp(pCtx->lo
f0470 63 6b 50 72 6f 78 79 50 61 74 68 2c 20 70 72 6f  ckProxyPath, pro
f0480 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c  xyPath, MAXPATHL
f0490 45 4e 29 29 0a 20 20 20 20 20 20 20 20 20 20 29  EN)).          )
f04a0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  {.            rc
f04b0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
f04c0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
f04d0 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
f04e0 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79 50  switchLockProxyP
f04f0 61 74 68 28 70 46 69 6c 65 2c 20 70 72 6f 78 79  ath(pFile, proxy
f0500 50 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 20  Path);.         
f0510 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65   }.        }else
f0520 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74  {.          /* t
f0530 75 72 6e 20 6f 6e 20 70 72 6f 78 79 20 66 69 6c  urn on proxy fil
f0540 65 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20  e locking */.   
f0550 20 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78         rc = prox
f0560 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69  yTransformUnixFi
f0570 6c 65 28 70 46 69 6c 65 2c 20 70 72 6f 78 79 50  le(pFile, proxyP
f0580 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ath);.        }.
f0590 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
f05a0 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
f05b0 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
f05c0 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b      assert( 0 );
f05d0 20 20 2f 2a 20 54 68 65 20 63 61 6c 6c 20 61 73    /* The call as
f05e0 73 75 72 65 73 20 74 68 61 74 20 6f 6e 6c 79 20  sures that only 
f05f0 76 61 6c 69 64 20 6f 70 63 6f 64 65 73 20 61 72  valid opcodes ar
f0600 65 20 73 65 6e 74 20 2a 2f 0a 20 20 20 20 7d 0a  e sent */.    }.
f0610 20 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48    }.  /*NOTREACH
f0620 45 44 2a 2f 0a 20 20 72 65 74 75 72 6e 20 53 51  ED*/.  return SQ
f0630 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f  LITE_ERROR;.}../
f0640 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 69 73  *.** Within this
f0650 20 64 69 76 69 73 69 6f 6e 20 28 74 68 65 20 70   division (the p
f0660 72 6f 78 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20  roxying locking 
f0670 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 20  implementation) 
f0680 74 68 65 20 70 72 6f 63 65 64 75 72 65 73 0a 2a  the procedures.*
f0690 2a 20 61 62 6f 76 65 20 74 68 69 73 20 70 6f 69  * above this poi
f06a0 6e 74 20 61 72 65 20 61 6c 6c 20 75 74 69 6c 69  nt are all utili
f06b0 74 69 65 73 2e 20 20 54 68 65 20 6c 6f 63 6b 2d  ties.  The lock-
f06c0 72 65 6c 61 74 65 64 20 6d 65 74 68 6f 64 73 20  related methods 
f06d0 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79 2d  of the.** proxy-
f06e0 6c 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f  locking sqlite3_
f06f0 69 6f 5f 6d 65 74 68 6f 64 20 6f 62 6a 65 63 74  io_method object
f0700 20 66 6f 6c 6c 6f 77 2e 0a 2a 2f 0a 0a 0a 2f 2a   follow..*/.../*
f0710 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
f0720 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65   checks if there
f0730 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c   is a RESERVED l
f0740 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20  ock held on the 
f0750 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c  specified.** fil
f0760 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79  e by this or any
f0770 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20   other process. 
f0780 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69  If such a lock i
f0790 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65  s held, set *pRe
f07a0 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e  sOut.** to a non
f07b0 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65  -zero value othe
f07c0 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69  rwise *pResOut i
f07d0 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20  s set to zero.  
f07e0 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
f07f0 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51  .** is set to SQ
f0800 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61  LITE_OK unless a
f0810 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75  n I/O error occu
f0820 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63  rs during lock c
f0830 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74  hecking..*/.stat
f0840 69 63 20 69 6e 74 20 70 72 6f 78 79 43 68 65 63  ic int proxyChec
f0850 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71  kReservedLock(sq
f0860 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
f0870 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a  int *pResOut) {.
f0880 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
f0890 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
f08a0 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72  d;.  int rc = pr
f08b0 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69  oxyTakeConch(pFi
f08c0 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  le);.  if( rc==S
f08d0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
f08e0 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
f08f0 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f  ext *pCtx = (pro
f0900 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
f0910 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e   *)pFile->lockin
f0920 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e  gContext;.    un
f0930 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20  ixFile *proxy = 
f0940 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b  pCtx->lockProxy;
f0950 0a 20 20 20 20 72 65 74 75 72 6e 20 70 72 6f 78  .    return prox
f0960 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 68 65  y->pMethod->xChe
f0970 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 28  ckReservedLock((
f0980 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72  sqlite3_file*)pr
f0990 6f 78 79 2c 20 70 52 65 73 4f 75 74 29 3b 0a 20  oxy, pResOut);. 
f09a0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
f09b0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68  }../*.** Lock th
f09c0 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20  e file with the 
f09d0 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62  lock specified b
f09e0 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b  y parameter lock
f09f0 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66  type - one.** of
f0a00 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
f0a10 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48  **.**     (1) SH
f0a20 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  ARED_LOCK.**    
f0a30 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f   (2) RESERVED_LO
f0a40 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45  CK.**     (3) PE
f0a50 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  NDING_LOCK.**   
f0a60 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f    (4) EXCLUSIVE_
f0a70 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74  LOCK.**.** Somet
f0a80 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73  imes when reques
f0a90 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74  ting one lock st
f0aa0 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20  ate, additional 
f0ab0 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61  lock states.** a
f0ac0 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62  re inserted in b
f0ad0 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63  etween.  The loc
f0ae0 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20  king might fail 
f0af0 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61  on one of the la
f0b00 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  ter.** transitio
f0b10 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c  ns leaving the l
f0b20 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72  ock state differ
f0b30 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74  ent from what it
f0b40 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20   started but.** 
f0b50 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69  still short of i
f0b60 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f  ts goal.  The fo
f0b70 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68  llowing chart sh
f0b80 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a  ows the allowed.
f0b90 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61  ** transitions a
f0ba0 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20  nd the inserted 
f0bb0 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61  intermediate sta
f0bc0 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e  tes:.**.**    UN
f0bd0 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44  LOCKED -> SHARED
f0be0 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e  .**    SHARED ->
f0bf0 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20   RESERVED.**    
f0c00 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49  SHARED -> (PENDI
f0c10 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  NG) -> EXCLUSIVE
f0c20 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20  .**    RESERVED 
f0c30 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20  -> (PENDING) -> 
f0c40 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20  EXCLUSIVE.**    
f0c50 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55  PENDING -> EXCLU
f0c60 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  SIVE.**.** This 
f0c70 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c  routine will onl
f0c80 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63  y increase a loc
f0c90 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69  k.  Use the sqli
f0ca0 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a  te3OsUnlock().**
f0cb0 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65   routine to lowe
f0cc0 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  r a locking leve
f0cd0 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  l..*/.static int
f0ce0 20 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 74   proxyLock(sqlit
f0cf0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
f0d00 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75   locktype) {.  u
f0d10 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
f0d20 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
f0d30 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f 78 79    int rc = proxy
f0d40 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 29  TakeConch(pFile)
f0d50 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
f0d60 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 72 6f  TE_OK ){.    pro
f0d70 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
f0d80 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c   *pCtx = (proxyL
f0d90 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29  ockingContext *)
f0da0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
f0db0 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 78 46  ntext;.    unixF
f0dc0 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 43 74  ile *proxy = pCt
f0dd0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20  x->lockProxy;.  
f0de0 20 20 72 63 20 3d 20 70 72 6f 78 79 2d 3e 70 4d    rc = proxy->pM
f0df0 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73 71  ethod->xLock((sq
f0e00 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f 78  lite3_file*)prox
f0e10 79 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20  y, locktype);.  
f0e20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
f0e30 65 20 3d 20 70 72 6f 78 79 2d 3e 6c 6f 63 6b 74  e = proxy->lockt
f0e40 79 70 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ype;.  }.  retur
f0e50 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  n rc;.}.../*.** 
f0e60 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e  Lower the lockin
f0e70 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20  g level on file 
f0e80 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65  descriptor pFile
f0e90 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c   to locktype.  l
f0ea0 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20  ocktype.** must 
f0eb0 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43  be either NO_LOC
f0ec0 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b  K or SHARED_LOCK
f0ed0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c  ..**.** If the l
f0ee0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20  ocking level of 
f0ef0 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
f0f00 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61  tor is already a
f0f10 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68  t or below.** th
f0f20 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b  e requested lock
f0f30 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20  ing level, this 
f0f40 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
f0f50 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  op..*/.static in
f0f60 74 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 28 73 71  t proxyUnlock(sq
f0f70 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
f0f80 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a  int locktype) {.
f0f90 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
f0fa0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
f0fb0 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72  d;.  int rc = pr
f0fc0 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69  oxyTakeConch(pFi
f0fd0 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  le);.  if( rc==S
f0fe0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
f0ff0 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
f1000 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f  ext *pCtx = (pro
f1010 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
f1020 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e   *)pFile->lockin
f1030 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e  gContext;.    un
f1040 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20  ixFile *proxy = 
f1050 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b  pCtx->lockProxy;
f1060 0a 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 2d  .    rc = proxy-
f1070 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63  >pMethod->xUnloc
f1080 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  k((sqlite3_file*
f1090 29 70 72 6f 78 79 2c 20 6c 6f 63 6b 74 79 70 65  )proxy, locktype
f10a0 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f  );.    pFile->lo
f10b0 63 6b 74 79 70 65 20 3d 20 70 72 6f 78 79 2d 3e  cktype = proxy->
f10c0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20  locktype;.  }.  
f10d0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
f10e0 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65  .** Close a file
f10f0 20 74 68 61 74 20 75 73 65 73 20 70 72 6f 78 79   that uses proxy
f1100 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69   locks..*/.stati
f1110 63 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73 65  c int proxyClose
f1120 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
f1130 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b  d) {.  if( id ){
f1140 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  .    unixFile *p
f1150 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
f1160 2a 29 69 64 3b 0a 20 20 20 20 70 72 6f 78 79 4c  *)id;.    proxyL
f1170 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70  ockingContext *p
f1180 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b  Ctx = (proxyLock
f1190 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69  ingContext *)pFi
f11a0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
f11b0 78 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65  xt;.    unixFile
f11c0 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70 43   *lockProxy = pC
f11d0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20  tx->lockProxy;. 
f11e0 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e     unixFile *con
f11f0 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63  chFile = pCtx->c
f1200 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 20 20 69 6e  onchFile;.    in
f1210 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
f1220 3b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 6c  ;.    .    if( l
f1230 6f 63 6b 50 72 6f 78 79 20 29 7b 0a 20 20 20 20  ockProxy ){.    
f1240 20 20 72 63 20 3d 20 6c 6f 63 6b 50 72 6f 78 79    rc = lockProxy
f1250 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f  ->pMethod->xUnlo
f1260 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ck((sqlite3_file
f1270 2a 29 6c 6f 63 6b 50 72 6f 78 79 2c 20 4e 4f 5f  *)lockProxy, NO_
f1280 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28  LOCK);.      if(
f1290 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
f12a0 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b  .      rc = lock
f12b0 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e  Proxy->pMethod->
f12c0 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f  xClose((sqlite3_
f12d0 66 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78 79 29  file*)lockProxy)
f12e0 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
f12f0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
f1300 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6c    sqlite3_free(l
f1310 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 20  ockProxy);.     
f1320 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79   pCtx->lockProxy
f1330 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
f1340 69 66 28 20 63 6f 6e 63 68 46 69 6c 65 20 29 7b  if( conchFile ){
f1350 0a 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d  .      if( pCtx-
f1360 3e 63 6f 6e 63 68 48 65 6c 64 20 29 7b 0a 20 20  >conchHeld ){.  
f1370 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79        rc = proxy
f1380 52 65 6c 65 61 73 65 43 6f 6e 63 68 28 70 46 69  ReleaseConch(pFi
f1390 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  le);.        if(
f13a0 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
f13b0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
f13c0 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70  c = conchFile->p
f13d0 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28  Method->xClose((
f13e0 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f  sqlite3_file*)co
f13f0 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 20 20  nchFile);.      
f1400 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
f1410 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  rc;.      sqlite
f1420 33 5f 66 72 65 65 28 63 6f 6e 63 68 46 69 6c 65  3_free(conchFile
f1430 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
f1440 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e  ite3_free(pCtx->
f1450 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 29 3b 0a  lockProxyPath);.
f1460 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
f1470 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65  (pCtx->conchFile
f1480 50 61 74 68 29 3b 0a 20 20 20 20 73 71 6c 69 74  Path);.    sqlit
f1490 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 64 62  e3_free(pCtx->db
f14a0 50 61 74 68 29 3b 0a 20 20 20 20 2f 2a 20 72 65  Path);.    /* re
f14b0 73 74 6f 72 65 20 74 68 65 20 6f 72 69 67 69 6e  store the origin
f14c0 61 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65  al locking conte
f14d0 78 74 20 61 6e 64 20 70 4d 65 74 68 6f 64 20 74  xt and pMethod t
f14e0 68 65 6e 20 63 6c 6f 73 65 20 69 74 20 2a 2f 0a  hen close it */.
f14f0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69      pFile->locki
f1500 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 78  ngContext = pCtx
f1510 2d 3e 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74  ->oldLockingCont
f1520 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  ext;.    pFile->
f1530 70 4d 65 74 68 6f 64 20 3d 20 70 43 74 78 2d 3e  pMethod = pCtx->
f1540 70 4f 6c 64 4d 65 74 68 6f 64 3b 0a 20 20 20 20  pOldMethod;.    
f1550 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74  sqlite3_free(pCt
f1560 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70  x);.    return p
f1570 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78  File->pMethod->x
f1580 43 6c 6f 73 65 28 69 64 29 3b 0a 20 20 7d 0a 20  Close(id);.  }. 
f1590 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
f15a0 4b 3b 0a 7d 0a 0a 0a 0a 23 65 6e 64 69 66 20 2f  K;.}....#endif /
f15b0 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c  * defined(__APPL
f15c0 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45  E__) && SQLITE_E
f15d0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
f15e0 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65  YLE */./*.** The
f15f0 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 73   proxy locking s
f1600 74 79 6c 65 20 69 73 20 69 6e 74 65 6e 64 65 64  tyle is intended
f1610 20 66 6f 72 20 75 73 65 20 77 69 74 68 20 41 46   for use with AF
f1620 50 20 66 69 6c 65 73 79 73 74 65 6d 73 2e 0a 2a  P filesystems..*
f1630 2a 20 41 6e 64 20 73 69 6e 63 65 20 41 46 50 20  * And since AFP 
f1640 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 65  is only supporte
f1650 64 20 6f 6e 20 4d 61 63 4f 53 58 2c 20 74 68 65  d on MacOSX, the
f1660 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69   proxy locking i
f1670 73 20 61 6c 73 6f 0a 2a 2a 20 72 65 73 74 72 69  s also.** restri
f1680 63 74 65 64 20 74 6f 20 4d 61 63 4f 53 58 2e 0a  cted to MacOSX..
f1690 2a 2a 20 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ** .**.*********
f16a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
f16b0 66 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b  f the proxy lock
f16c0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
f16d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f16e0 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ******.*********
f16f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f1700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f1710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f1720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f1730 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e  *****/../*.** In
f1740 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65  itialize the ope
f1750 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e  rating system in
f1760 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54  terface..**.** T
f1770 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 67 69  his routine regi
f1780 73 74 65 72 73 20 61 6c 6c 20 56 46 53 20 69 6d  sters all VFS im
f1790 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f  plementations fo
f17a0 72 20 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72  r unix-like oper
f17b0 61 74 69 6e 67 0a 2a 2a 20 73 79 73 74 65 6d 73  ating.** systems
f17c0 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 2c  .  This routine,
f17d0 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33   and the sqlite3
f17e0 5f 6f 73 5f 65 6e 64 28 29 20 72 6f 75 74 69 6e  _os_end() routin
f17f0 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2c 0a  e that follows,.
f1800 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65  ** should be the
f1810 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 73 20 69   only routines i
f1820 6e 20 74 68 69 73 20 66 69 6c 65 20 74 68 61 74  n this file that
f1830 20 61 72 65 20 76 69 73 69 62 6c 65 20 66 72 6f   are visible fro
f1840 6d 20 6f 74 68 65 72 0a 2a 2a 20 66 69 6c 65 73  m other.** files
f1850 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
f1860 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f  tine is called o
f1870 6e 63 65 20 64 75 72 69 6e 67 20 53 51 4c 69 74  nce during SQLit
f1880 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  e initialization
f1890 20 61 6e 64 20 62 79 20 61 0a 2a 2a 20 73 69 6e   and by a.** sin
f18a0 67 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65  gle thread.  The
f18b0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
f18c0 6f 6e 20 61 6e 64 20 6d 75 74 65 78 20 73 75 62  on and mutex sub
f18d0 73 79 73 74 65 6d 73 20 68 61 76 65 20 6e 6f 74  systems have not
f18e0 0a 2a 2a 20 6e 65 63 65 73 73 61 72 69 6c 79 20  .** necessarily 
f18f0 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64  been initialized
f1900 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   when this routi
f1910 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 61 6e  ne is called, an
f1920 64 20 73 6f 20 74 68 65 79 0a 2a 2a 20 73 68 6f  d so they.** sho
f1930 75 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64 2e  uld not be used.
f1940 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
f1950 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  nt sqlite3_os_in
f1960 69 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a 20  it(void){ .  /* 
f1970 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .  ** The follow
f1980 69 6e 67 20 6d 61 63 72 6f 20 64 65 66 69 6e 65  ing macro define
f1990 73 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a 65 72  s an initializer
f19a0 20 66 6f 72 20 61 6e 20 73 71 6c 69 74 65 33 5f   for an sqlite3_
f19b0 76 66 73 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2a  vfs object..  **
f19c0 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   The name of the
f19d0 20 56 46 53 20 69 73 20 4e 41 4d 45 2e 20 20 54   VFS is NAME.  T
f19e0 68 65 20 70 41 70 70 44 61 74 61 20 69 73 20 61  he pAppData is a
f19f0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 6f   pointer to a po
f1a00 69 6e 74 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68  inter.  ** to th
f1a10 65 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74  e "finder" funct
f1a20 69 6f 6e 2e 20 20 28 70 41 70 70 44 61 74 61 20  ion.  (pAppData 
f1a30 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
f1a40 61 20 70 6f 69 6e 74 65 72 20 62 65 63 61 75 73  a pointer becaus
f1a50 65 0a 20 20 2a 2a 20 73 69 6c 6c 79 20 43 39 30  e.  ** silly C90
f1a60 20 72 75 6c 65 73 20 70 72 6f 68 69 62 69 74 20   rules prohibit 
f1a70 61 20 76 6f 69 64 2a 20 66 72 6f 6d 20 62 65 69  a void* from bei
f1a80 6e 67 20 63 61 73 74 20 74 6f 20 61 20 66 75 6e  ng cast to a fun
f1a90 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 0a 20 20  ction pointer.  
f1aa0 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20 68 61 76  ** and so we hav
f1ab0 65 20 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 20  e to go through 
f1ac0 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65  the intermediate
f1ad0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 76 6f 69   pointer to avoi
f1ae0 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20  d problems.  ** 
f1af0 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 77  when compiling w
f1b00 69 74 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 72  ith -pedantic-er
f1b10 72 6f 72 73 20 6f 6e 20 47 43 43 2e 29 0a 20 20  rors on GCC.).  
f1b20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 46 49 4e 44  **.  ** The FIND
f1b30 45 52 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  ER parameter to 
f1b40 74 68 69 73 20 6d 61 63 72 6f 20 69 73 20 74 68  this macro is th
f1b50 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 70 6f  e name of the po
f1b60 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 20 20 2a  inter to the.  *
f1b70 2a 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f  * finder-functio
f1b80 6e 2e 20 20 54 68 65 20 66 69 6e 64 65 72 2d 66  n.  The finder-f
f1b90 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
f1ba0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
f1bb0 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 69 6f 5f  .  ** sqlite_io_
f1bc0 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 74  methods object t
f1bd0 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  hat implements t
f1be0 68 65 20 64 65 73 69 72 65 64 20 6c 6f 63 6b 69  he desired locki
f1bf0 6e 67 0a 20 20 2a 2a 20 62 65 68 61 76 69 6f 72  ng.  ** behavior
f1c00 73 2e 20 20 53 65 65 20 74 68 65 20 64 69 76 69  s.  See the divi
f1c10 73 69 6f 6e 20 61 62 6f 76 65 20 74 68 61 74 20  sion above that 
f1c20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 49 4f 4d  contains the IOM
f1c30 45 54 48 4f 44 53 0a 20 20 2a 2a 20 6d 61 63 72  ETHODS.  ** macr
f1c40 6f 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 20 69  o for addition i
f1c50 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 66 69  nformation on fi
f1c60 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 73 2e 0a  nder-functions..
f1c70 20 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 73 74 20 66    **.  ** Most f
f1c80 69 6e 64 65 72 73 20 73 69 6d 70 6c 79 20 72 65  inders simply re
f1c90 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
f1ca0 6f 20 61 20 66 69 78 65 64 20 73 71 6c 69 74 65  o a fixed sqlite
f1cb0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 20 20 2a  3_io_methods.  *
f1cc0 2a 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 20 74  * object.  But t
f1cd0 68 65 20 22 61 75 74 6f 6c 6f 63 6b 49 6f 46 69  he "autolockIoFi
f1ce0 6e 64 65 72 22 20 61 76 61 69 6c 61 62 6c 65 20  nder" available 
f1cf0 6f 6e 20 4d 61 63 4f 53 58 20 64 6f 65 73 20 61  on MacOSX does a
f1d00 20 6c 69 74 74 6c 65 0a 20 20 2a 2a 20 6d 6f 72   little.  ** mor
f1d10 65 20 74 68 61 6e 20 74 68 61 74 3b 20 69 74 20  e than that; it 
f1d20 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 66 69 6c  looks at the fil
f1d30 65 73 79 73 74 65 6d 20 74 79 70 65 20 74 68 61  esystem type tha
f1d40 74 20 68 6f 73 74 73 20 74 68 65 20 0a 20 20 2a  t hosts the .  *
f1d50 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  * database file 
f1d60 61 6e 64 20 74 72 69 65 73 20 74 6f 20 63 68 6f  and tries to cho
f1d70 6f 73 65 20 61 6e 20 6c 6f 63 6b 69 6e 67 20 6d  ose an locking m
f1d80 65 74 68 6f 64 20 61 70 70 72 6f 70 72 69 61 74  ethod appropriat
f1d90 65 20 66 6f 72 0a 20 20 2a 2a 20 74 68 61 74 20  e for.  ** that 
f1da0 66 69 6c 65 73 79 73 74 65 6d 20 74 69 6d 65 2e  filesystem time.
f1db0 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20  .  */.  #define 
f1dc0 55 4e 49 58 56 46 53 28 56 46 53 4e 41 4d 45 2c  UNIXVFS(VFSNAME,
f1dd0 20 46 49 4e 44 45 52 29 20 7b 20 20 20 20 20 20   FINDER) {      
f1de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f1df0 20 20 5c 0a 20 20 20 20 31 2c 20 20 20 20 20 20    \.    1,      
f1e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
f1e10 20 69 56 65 72 73 69 6f 6e 20 2a 2f 20 20 20 20   iVersion */    
f1e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f1e30 5c 0a 20 20 20 20 73 69 7a 65 6f 66 28 75 6e 69  \.    sizeof(uni
f1e40 78 46 69 6c 65 29 2c 20 20 20 20 20 2f 2a 20 73  xFile),     /* s
f1e50 7a 4f 73 46 69 6c 65 20 2a 2f 20 20 20 20 20 20  zOsFile */      
f1e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
f1e70 20 20 20 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45      MAX_PATHNAME
f1e80 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 50  ,         /* mxP
f1e90 61 74 68 6e 61 6d 65 20 2a 2f 20 20 20 20 20 20  athname */      
f1ea0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
f1eb0 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
f1ec0 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74          /* pNext
f1ed0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
f1ee0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
f1ef0 56 46 53 4e 41 4d 45 2c 20 20 20 20 20 20 20 20  VFSNAME,        
f1f00 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a        /* zName *
f1f10 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
f1f20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 28 76          \.    (v
f1f30 6f 69 64 2a 29 26 46 49 4e 44 45 52 2c 20 20 20  oid*)&FINDER,   
f1f40 20 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20      /* pAppData 
f1f50 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
f1f60 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78        \.    unix
f1f70 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20  Open,           
f1f80 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 20 20 20    /* xOpen */   
f1f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f1fa0 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 65      \.    unixDe
f1fb0 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  lete,           
f1fc0 2f 2a 20 78 44 65 6c 65 74 65 20 2a 2f 20 20 20  /* xDelete */   
f1fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f1fe0 20 20 5c 0a 20 20 20 20 75 6e 69 78 41 63 63 65    \.    unixAcce
f1ff0 73 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ss,           /*
f2000 20 78 41 63 63 65 73 73 20 2a 2f 20 20 20 20 20   xAccess */     
f2010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2020 5c 0a 20 20 20 20 75 6e 69 78 46 75 6c 6c 50 61  \.    unixFullPa
f2030 74 68 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 78  thname,     /* x
f2040 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a 2f 20  FullPathname */ 
f2050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
f2060 20 20 20 20 75 6e 69 78 44 6c 4f 70 65 6e 2c 20      unixDlOpen, 
f2070 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c            /* xDl
f2080 4f 70 65 6e 20 2a 2f 20 20 20 20 20 20 20 20 20  Open */         
f2090 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
f20a0 20 20 75 6e 69 78 44 6c 45 72 72 6f 72 2c 20 20    unixDlError,  
f20b0 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72          /* xDlEr
f20c0 72 6f 72 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ror */          
f20d0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
f20e0 75 6e 69 78 44 6c 53 79 6d 2c 20 20 20 20 20 20  unixDlSym,      
f20f0 20 20 20 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20        /* xDlSym 
f2100 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
f2110 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e          \.    un
f2120 69 78 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20  ixDlClose,      
f2130 20 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20      /* xDlClose 
f2140 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
f2150 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78        \.    unix
f2160 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20  Randomness,     
f2170 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73    /* xRandomness
f2180 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
f2190 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 53 6c      \.    unixSl
f21a0 65 65 70 2c 20 20 20 20 20 20 20 20 20 20 20 20  eep,            
f21b0 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 20 20 20 20  /* xSleep */    
f21c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f21d0 20 20 5c 0a 20 20 20 20 75 6e 69 78 43 75 72 72    \.    unixCurr
f21e0 65 6e 74 54 69 6d 65 2c 20 20 20 20 20 20 2f 2a  entTime,      /*
f21f0 20 78 43 75 72 72 65 6e 74 54 69 6d 65 20 2a 2f   xCurrentTime */
f2200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2210 5c 0a 20 20 20 20 75 6e 69 78 47 65 74 4c 61 73  \.    unixGetLas
f2220 74 45 72 72 6f 72 20 20 20 20 20 20 2f 2a 20 78  tError      /* x
f2230 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 20  GetLastError */ 
f2240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
f2250 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41    }..  /*.  ** A
f2260 6c 6c 20 64 65 66 61 75 6c 74 20 56 46 53 65 73  ll default VFSes
f2270 20 66 6f 72 20 75 6e 69 78 20 61 72 65 20 63 6f   for unix are co
f2280 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 66  ntained in the f
f2290 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 2e 0a  ollowing array..
f22a0 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74    **.  ** Note t
f22b0 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f  hat the sqlite3_
f22c0 76 66 73 2e 70 4e 65 78 74 20 66 69 65 6c 64 20  vfs.pNext field 
f22d0 6f 66 20 74 68 65 20 56 46 53 20 6f 62 6a 65 63  of the VFS objec
f22e0 74 20 69 73 20 6d 6f 64 69 66 69 65 64 0a 20 20  t is modified.  
f22f0 2a 2a 20 62 79 20 74 68 65 20 53 51 4c 69 74 65  ** by the SQLite
f2300 20 63 6f 72 65 20 77 68 65 6e 20 74 68 65 20 56   core when the V
f2310 46 53 20 69 73 20 72 65 67 69 73 74 65 72 65 64  FS is registered
f2320 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f 77  .  So the follow
f2330 69 6e 67 0a 20 20 2a 2a 20 61 72 72 61 79 20 63  ing.  ** array c
f2340 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74 2e 0a  annot be const..
f2350 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 73 71    */.  static sq
f2360 6c 69 74 65 33 5f 76 66 73 20 61 56 66 73 5b 5d  lite3_vfs aVfs[]
f2370 20 3d 20 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f   = {.#if SQLITE_
f2380 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
f2390 54 59 4c 45 20 26 26 20 28 4f 53 5f 56 58 57 4f  TYLE && (OS_VXWO
f23a0 52 4b 53 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  RKS || defined(_
f23b0 5f 41 50 50 4c 45 5f 5f 29 29 0a 20 20 20 20 55  _APPLE__)).    U
f23c0 4e 49 58 56 46 53 28 22 75 6e 69 78 22 2c 20 20  NIXVFS("unix",  
f23d0 20 20 20 20 20 20 20 20 61 75 74 6f 6c 6f 63 6b          autolock
f23e0 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6c 73  IoFinder ),.#els
f23f0 65 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75  e.    UNIXVFS("u
f2400 6e 69 78 22 2c 20 20 20 20 20 20 20 20 20 20 70  nix",          p
f2410 6f 73 69 78 49 6f 46 69 6e 64 65 72 20 29 2c 0a  osixIoFinder ),.
f2420 23 65 6e 64 69 66 0a 20 20 20 20 55 4e 49 58 56  #endif.    UNIXV
f2430 46 53 28 22 75 6e 69 78 2d 6e 6f 6e 65 22 2c 20  FS("unix-none", 
f2440 20 20 20 20 6e 6f 6c 6f 63 6b 49 6f 46 69 6e 64      nolockIoFind
f2450 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46  er ),.    UNIXVF
f2460 53 28 22 75 6e 69 78 2d 64 6f 74 66 69 6c 65 22  S("unix-dotfile"
f2470 2c 20 20 64 6f 74 6c 6f 63 6b 49 6f 46 69 6e 64  ,  dotlockIoFind
f2480 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46  er ),.    UNIXVF
f2490 53 28 22 75 6e 69 78 2d 77 66 6c 22 2c 20 20 20  S("unix-wfl",   
f24a0 20 20 20 70 6f 73 69 78 57 66 6c 49 6f 46 69 6e     posixWflIoFin
f24b0 64 65 72 20 29 2c 0a 23 69 66 20 4f 53 5f 56 58  der ),.#if OS_VX
f24c0 57 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56 46  WORKS.    UNIXVF
f24d0 53 28 22 75 6e 69 78 2d 6e 61 6d 65 64 73 65 6d  S("unix-namedsem
f24e0 22 2c 20 73 65 6d 49 6f 46 69 6e 64 65 72 20 29  ", semIoFinder )
f24f0 2c 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c  ,.#endif.#if SQL
f2500 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
f2510 4e 47 5f 53 54 59 4c 45 0a 20 20 20 20 55 4e 49  NG_STYLE.    UNI
f2520 58 56 46 53 28 22 75 6e 69 78 2d 70 6f 73 69 78  XVFS("unix-posix
f2530 22 2c 20 20 20 20 70 6f 73 69 78 49 6f 46 69 6e  ",    posixIoFin
f2540 64 65 72 20 29 2c 0a 23 69 66 20 21 4f 53 5f 56  der ),.#if !OS_V
f2550 58 57 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56  XWORKS.    UNIXV
f2560 46 53 28 22 75 6e 69 78 2d 66 6c 6f 63 6b 22 2c  FS("unix-flock",
f2570 20 20 20 20 66 6c 6f 63 6b 49 6f 46 69 6e 64 65      flockIoFinde
f2580 72 20 29 2c 0a 23 65 6e 64 69 66 0a 23 65 6e 64  r ),.#endif.#end
f2590 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e  if.#if SQLITE_EN
f25a0 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
f25b0 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  LE && defined(__
f25c0 41 50 50 4c 45 5f 5f 29 0a 20 20 20 20 55 4e 49  APPLE__).    UNI
f25d0 58 56 46 53 28 22 75 6e 69 78 2d 61 66 70 22 2c  XVFS("unix-afp",
f25e0 20 20 20 20 20 20 61 66 70 49 6f 46 69 6e 64 65        afpIoFinde
f25f0 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53  r ),.    UNIXVFS
f2600 28 22 75 6e 69 78 2d 70 72 6f 78 79 22 2c 20 20  ("unix-proxy",  
f2610 20 20 70 72 6f 78 79 49 6f 46 69 6e 64 65 72 20    proxyIoFinder 
f2620 29 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 20  ),.#endif.  };. 
f2630 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b   unsigned int i;
f2640 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
f2650 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20  p counter */..  
f2660 2f 2a 20 52 65 67 69 73 74 65 72 20 61 6c 6c 20  /* Register all 
f2670 56 46 53 65 73 20 64 65 66 69 6e 65 64 20 69 6e  VFSes defined in
f2680 20 74 68 65 20 61 56 66 73 5b 5d 20 61 72 72 61   the aVfs[] arra
f2690 79 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20  y */.  for(i=0; 
f26a0 69 3c 28 73 69 7a 65 6f 66 28 61 56 66 73 29 2f  i<(sizeof(aVfs)/
f26b0 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 76  sizeof(sqlite3_v
f26c0 66 73 29 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  fs)); i++){.    
f26d0 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69  sqlite3_vfs_regi
f26e0 73 74 65 72 28 26 61 56 66 73 5b 69 5d 2c 20 69  ster(&aVfs[i], i
f26f0 3d 3d 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ==0);.  }.  retu
f2700 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d  rn SQLITE_OK; .}
f2710 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e  ../*.** Shutdown
f2720 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73   the operating s
f2730 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e  ystem interface.
f2740 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 70 65 72  .**.** Some oper
f2750 61 74 69 6e 67 20 73 79 73 74 65 6d 73 20 6d 69  ating systems mi
f2760 67 68 74 20 6e 65 65 64 20 74 6f 20 64 6f 20 73  ght need to do s
f2770 6f 6d 65 20 63 6c 65 61 6e 75 70 20 69 6e 20 74  ome cleanup in t
f2780 68 69 73 20 72 6f 75 74 69 6e 65 2c 0a 2a 2a 20  his routine,.** 
f2790 74 6f 20 72 65 6c 65 61 73 65 20 64 79 6e 61 6d  to release dynam
f27a0 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64  ically allocated
f27b0 20 6f 62 6a 65 63 74 73 2e 20 20 42 75 74 20 6e   objects.  But n
f27c0 6f 74 20 6f 6e 20 75 6e 69 78 2e 0a 2a 2a 20 54  ot on unix..** T
f27d0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
f27e0 20 6e 6f 2d 6f 70 20 66 6f 72 20 75 6e 69 78 2e   no-op for unix.
f27f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
f2800 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e  nt sqlite3_os_en
f2810 64 28 76 6f 69 64 29 7b 20 0a 20 20 72 65 74 75  d(void){ .  retu
f2820 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d  rn SQLITE_OK; .}
f2830 0a 20 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  . .#endif /* SQL
f2840 49 54 45 5f 4f 53 5f 55 4e 49 58 20 2a 2f 0a 0a  ITE_OS_UNIX */..
f2850 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
f2860 45 6e 64 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63  End of os_unix.c
f2870 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
f2880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
f28a0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
f28b0 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 77 69  Begin file os_wi
f28c0 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
f28d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f28e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
f28f0 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32  /*.** 2004 May 2
f2900 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  2.**.** The auth
f2910 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
f2920 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
f2930 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
f2940 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
f2950 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
f2960 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
f2970 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
f2980 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
f2990 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
f29a0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
f29b0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
f29c0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
f29d0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
f29e0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
f29f0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
f2a00 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
f2a10 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
f2a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2a60 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
f2a70 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
f2a80 6f 64 65 20 74 68 61 74 20 69 73 20 73 70 65 63  ode that is spec
f2a90 69 66 69 63 20 74 6f 20 77 69 6e 64 6f 77 73 2e  ific to windows.
f2aa0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  .*/.#if SQLITE_O
f2ab0 53 5f 57 49 4e 20 20 20 20 20 20 20 20 20 20 20  S_WIN           
f2ac0 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c 65      /* This file
f2ad0 20 69 73 20 75 73 65 64 20 66 6f 72 20 77 69 6e   is used for win
f2ae0 64 6f 77 73 20 6f 6e 6c 79 20 2a 2f 0a 0a 0a 2f  dows only */.../
f2af0 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 62 6f 75  *.** A Note Abou
f2b00 74 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74  t Memory Allocat
f2b10 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ion:.**.** This 
f2b20 64 72 69 76 65 72 20 75 73 65 73 20 6d 61 6c 6c  driver uses mall
f2b30 6f 63 28 29 2f 66 72 65 65 28 29 20 64 69 72 65  oc()/free() dire
f2b40 63 74 6c 79 20 72 61 74 68 65 72 20 74 68 61 6e  ctly rather than
f2b50 20 67 6f 69 6e 67 20 74 68 72 6f 75 67 68 0a 2a   going through.*
f2b60 2a 20 74 68 65 20 53 51 4c 69 74 65 2d 77 72 61  * the SQLite-wra
f2b70 70 70 65 72 73 20 73 71 6c 69 74 65 33 5f 6d 61  ppers sqlite3_ma
f2b80 6c 6c 6f 63 28 29 2f 73 71 6c 69 74 65 33 5f 66  lloc()/sqlite3_f
f2b90 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20 77 72  ree().  Those wr
f2ba0 61 70 70 65 72 73 0a 2a 2a 20 61 72 65 20 64 65  appers.** are de
f2bb0 73 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20 6f  signed for use o
f2bc0 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65  n embedded syste
f2bd0 6d 73 20 77 68 65 72 65 20 6d 65 6d 6f 72 79 20  ms where memory 
f2be0 69 73 20 73 63 61 72 63 65 20 61 6e 64 0a 2a 2a  is scarce and.**
f2bf0 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73   malloc failures
f2c00 20 68 61 70 70 65 6e 20 66 72 65 71 75 65 6e 74   happen frequent
f2c10 6c 79 2e 20 20 57 69 6e 33 32 20 64 6f 65 73 20  ly.  Win32 does 
f2c20 6e 6f 74 20 74 79 70 69 63 61 6c 6c 79 20 72 75  not typically ru
f2c30 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64  n on.** embedded
f2c40 20 73 79 73 74 65 6d 73 2c 20 61 6e 64 20 77 68   systems, and wh
f2c50 65 6e 20 69 74 20 64 6f 65 73 20 74 68 65 20 64  en it does the d
f2c60 65 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d 61 6c  evelopers normal
f2c70 6c 79 20 68 61 76 65 20 62 69 67 67 65 72 0a 2a  ly have bigger.*
f2c80 2a 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20 77 6f  * problems to wo
f2c90 72 72 79 20 61 62 6f 75 74 20 74 68 61 6e 20 72  rry about than r
f2ca0 75 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20 6d 65  unning out of me
f2cb0 6d 6f 72 79 2e 20 20 53 6f 20 74 68 65 72 65 20  mory.  So there 
f2cc0 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f 6d 70  is not.** a comp
f2cd0 65 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f 20 75  elling need to u
f2ce0 73 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e  se the wrappers.
f2cf0 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65 72 65  .**.** But there
f2d00 20 69 73 20 61 20 67 6f 6f 64 20 72 65 61 73 6f   is a good reaso
f2d10 6e 20 74 6f 20 6e 6f 74 20 75 73 65 20 74 68 65  n to not use the
f2d20 20 77 72 61 70 70 65 72 73 2e 20 20 49 66 20 77   wrappers.  If w
f2d30 65 20 75 73 65 20 74 68 65 0a 2a 2a 20 77 72 61  e use the.** wra
f2d40 70 70 65 72 73 20 74 68 65 6e 20 77 65 20 77 69  ppers then we wi
f2d50 6c 6c 20 67 65 74 20 73 69 6d 75 6c 61 74 65 64  ll get simulated
f2d60 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72   malloc() failur
f2d70 65 73 20 77 69 74 68 69 6e 20 74 68 69 73 0a 2a  es within this.*
f2d80 2a 20 64 72 69 76 65 72 2e 20 20 41 6e 64 20 74  * driver.  And t
f2d90 68 61 74 20 63 61 75 73 65 73 20 61 6c 6c 20 6b  hat causes all k
f2da0 69 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65 6d 73  inds of problems
f2db0 20 66 6f 72 20 6f 75 72 20 74 65 73 74 73 2e 20   for our tests. 
f2dc0 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65 6e 68   We.** could enh
f2dd0 61 6e 63 65 20 53 51 4c 69 74 65 20 74 6f 20 64  ance SQLite to d
f2de0 65 61 6c 20 77 69 74 68 20 73 69 6d 75 6c 61 74  eal with simulat
f2df0 65 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72  ed malloc failur
f2e00 65 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65  es within.** the
f2e10 20 4f 53 20 64 72 69 76 65 72 2c 20 62 75 74 20   OS driver, but 
f2e20 74 68 65 20 63 6f 64 65 20 74 6f 20 64 65 61 6c  the code to deal
f2e30 20 77 69 74 68 20 74 68 6f 73 65 20 66 61 69 6c   with those fail
f2e40 75 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a  ure would not.**
f2e50 20 62 65 20 65 78 65 72 63 69 73 65 64 20 6f 6e   be exercised on
f2e60 20 4c 69 6e 75 78 20 28 77 68 69 63 68 20 64 6f   Linux (which do
f2e70 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d  es not need to m
f2e80 61 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65 20 64  alloc() in the d
f2e90 72 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20 73 6f  river).** and so
f2ea0 20 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20 64   we would have d
f2eb0 69 66 66 69 63 75 6c 74 79 20 77 72 69 74 69 6e  ifficulty writin
f2ec0 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 73  g coverage tests
f2ed0 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f 64   for that.** cod
f2ee0 65 2e 20 20 42 65 74 74 65 72 20 74 6f 20 6c 65  e.  Better to le
f2ef0 61 76 65 20 74 68 65 20 63 6f 64 65 20 6f 75 74  ave the code out
f2f00 2c 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a  , we think..**.*
f2f10 2a 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74  * The point of t
f2f20 68 69 73 20 64 69 73 63 75 73 73 69 6f 6e 20 69  his discussion i
f2f30 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 20 57  s as follows:  W
f2f40 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e  hen creating a n
f2f50 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72 20 66  ew.** OS layer f
f2f60 6f 72 20 61 6e 20 65 6d 62 65 64 64 65 64 20 73  or an embedded s
f2f70 79 73 74 65 6d 2c 20 69 66 20 79 6f 75 20 75 73  ystem, if you us
f2f80 65 20 74 68 69 73 20 66 69 6c 65 20 61 73 20 61  e this file as a
f2f90 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 61 76  n example,.** av
f2fa0 6f 69 64 20 74 68 65 20 75 73 65 20 6f 66 20 6d  oid the use of m
f2fb0 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 2e 20  alloc()/free(). 
f2fc0 20 54 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20   Those routines 
f2fd0 77 6f 72 6b 20 6f 6b 20 6f 6e 20 77 69 6e 64 6f  work ok on windo
f2fe0 77 73 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62  ws.** desktops b
f2ff0 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69  ut not so well i
f3000 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65  n embedded syste
f3010 6d 73 2e 0a 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65  ms..*/..#include
f3020 20 3c 77 69 6e 62 61 73 65 2e 68 3e 0a 0a 23 69   <winbase.h>..#i
f3030 66 64 65 66 20 5f 5f 43 59 47 57 49 4e 5f 5f 0a  fdef __CYGWIN__.
f3040 23 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 63  # include <sys/c
f3050 79 67 77 69 6e 2e 68 3e 0a 23 65 6e 64 69 66 0a  ygwin.h>.#endif.
f3060 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73  ./*.** Macros us
f3070 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ed to determine 
f3080 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
f3090 6f 20 75 73 65 20 74 68 72 65 61 64 73 2e 0a 2a  o use threads..*
f30a0 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48  /.#if defined(TH
f30b0 52 45 41 44 53 41 46 45 29 20 26 26 20 54 48 52  READSAFE) && THR
f30c0 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65  EADSAFE.# define
f30d0 20 53 51 4c 49 54 45 5f 57 33 32 5f 54 48 52 45   SQLITE_W32_THRE
f30e0 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ADS 1.#endif../*
f30f0 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65  .** Include code
f3100 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20   that is common 
f3110 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69  to all os_*.c fi
f3120 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  les.*/./********
f3130 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f  ****** Include o
f3140 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68  s_common.h in th
f3150 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 77  e middle of os_w
f3160 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  in.c ***********
f3170 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
f3180 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
f3190 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a  e os_common.h **
f31a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f31b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f31c0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
f31d0 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54  4 May 22.**.** T
f31e0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
f31f0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
f3200 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
f3210 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
f3220 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
f3230 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
f3240 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
f3250 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
f3260 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
f3270 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
f3280 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
f3290 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
f32a0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
f32b0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
f32c0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
f32d0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
f32e0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
f32f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
f3340 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
f3350 74 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64  tains macros and
f3360 20 61 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66   a little bit of
f3370 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f   code that is co
f3380 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f  mmon to.** all o
f3390 66 20 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73  f the platform-s
f33a0 70 65 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f  pecific files (o
f33b0 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69  s_*.c) and is #i
f33c0 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f  ncluded into tho
f33d0 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a  se.** files..**.
f33e0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f  ** This file sho
f33f0 75 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64  uld be #included
f3400 20 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66   by the os_*.c f
f3410 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69  iles only.  It i
f3420 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72  s not a.** gener
f3430 61 6c 20 70 75 72 70 6f 73 65 20 68 65 61 64 65  al purpose heade
f3440 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49  r file..**.** $I
f3450 64 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76  d: os_common.h,v
f3460 20 31 2e 33 38 20 32 30 30 39 2f 30 32 2f 32 34   1.38 2009/02/24
f3470 20 31 38 3a 34 30 3a 35 30 20 64 61 6e 69 65 6c   18:40:50 daniel
f3480 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23  k1977 Exp $.*/.#
f3490 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f  ifndef _OS_COMMO
f34a0 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53  N_H_.#define _OS
f34b0 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a  _COMMON_H_../*.*
f34c0 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62  * At least two b
f34d0 75 67 73 20 68 61 76 65 20 73 6c 69 70 70 65 64  ugs have slipped
f34e0 20 69 6e 20 62 65 63 61 75 73 65 20 77 65 20 63   in because we c
f34f0 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52  hanged the MEMOR
f3500 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f  Y_DEBUG.** macro
f3510 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47   to SQLITE_DEBUG
f3520 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20   and some older 
f3530 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e  makefiles have n
f3540 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a  ot yet made the.
f3550 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68 65 20  ** switch.  The 
f3560 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73  following code s
f3570 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68 69 73  hould catch this
f3580 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70   problem at comp
f3590 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66  ile-time..*/.#if
f35a0 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47  def MEMORY_DEBUG
f35b0 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45  .# error "The ME
f35c0 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f  MORY_DEBUG macro
f35d0 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55   is obsolete.  U
f35e0 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  se SQLITE_DEBUG 
f35f0 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66  instead.".#endif
f3600 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
f3610 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49  DEBUG.SQLITE_PRI
f3620 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
f3630 4f 53 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65  OSTrace = 0;.#de
f3640 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29  fine OSTRACE1(X)
f3650 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c           if( sql
f3660 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71  ite3OSTrace ) sq
f3670 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
f3680 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  (X).#define OSTR
f3690 41 43 45 32 28 58 2c 59 29 20 20 20 20 20 20 20  ACE2(X,Y)       
f36a0 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61  if( sqlite3OSTra
f36b0 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
f36c0 67 50 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65  gPrintf(X,Y).#de
f36d0 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c  fine OSTRACE3(X,
f36e0 59 2c 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c  Y,Z)     if( sql
f36f0 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71  ite3OSTrace ) sq
f3700 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
f3710 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20  (X,Y,Z).#define 
f3720 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41  OSTRACE4(X,Y,Z,A
f3730 29 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f  )   if( sqlite3O
f3740 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33  STrace ) sqlite3
f3750 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
f3760 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  Z,A).#define OST
f3770 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29  RACE5(X,Y,Z,A,B)
f3780 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72   if( sqlite3OSTr
f3790 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62  ace ) sqlite3Deb
f37a0 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41  ugPrintf(X,Y,Z,A
f37b0 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,B).#define OSTR
f37c0 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  ACE6(X,Y,Z,A,B,C
f37d0 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74  ) \.    if(sqlit
f37e0 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74  e3OSTrace) sqlit
f37f0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
f3800 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69  Y,Z,A,B,C).#defi
f3810 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c  ne OSTRACE7(X,Y,
f3820 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20  Z,A,B,C,D) \.   
f3830 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61   if(sqlite3OSTra
f3840 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  ce) sqlite3Debug
f3850 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42  Printf(X,Y,Z,A,B
f3860 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66  ,C,D).#else.#def
f3870 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a  ine OSTRACE1(X).
f3880 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32  #define OSTRACE2
f3890 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53  (X,Y).#define OS
f38a0 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64  TRACE3(X,Y,Z).#d
f38b0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58  efine OSTRACE4(X
f38c0 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20  ,Y,Z,A).#define 
f38d0 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41  OSTRACE5(X,Y,Z,A
f38e0 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,B).#define OSTR
f38f0 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  ACE6(X,Y,Z,A,B,C
f3900 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
f3910 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44  E7(X,Y,Z,A,B,C,D
f3920 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
f3930 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f  Macros for perfo
f3940 72 6d 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20  rmance tracing. 
f3950 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64   Normally turned
f3960 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b   off.  Only work
f3970 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72  s.** on i486 har
f3980 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66  dware..*/.#ifdef
f3990 20 53 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41   SQLITE_PERFORMA
f39a0 4e 43 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a  NCE_TRACE../* .*
f39b0 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61  * hwtime.h conta
f39c0 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d  ins inline assem
f39d0 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d  bler code for im
f39e0 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68  plementing .** h
f39f0 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20  igh-performance 
f3a00 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e  timing routines.
f3a10 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .*/./***********
f3a20 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69  *** Include hwti
f3a30 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  me.h in the midd
f3a40 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e  le of os_common.
f3a50 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
f3a60 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
f3a70 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68  *** Begin file h
f3a80 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  wtime.h ********
f3a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3ab0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d  **/./*.** 2008 M
f3ac0 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ay 27.**.** The 
f3ad0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
f3ae0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
f3af0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
f3b00 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
f3b10 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
f3b20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
f3b30 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
f3b40 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
f3b50 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
f3b60 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
f3b70 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
f3b80 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
f3b90 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
f3ba0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
f3bb0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
f3bc0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
f3bd0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
f3be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
f3c30 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
f3c40 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f  ns inline asm co
f3c50 64 65 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e  de for retrievin
f3c60 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61  g "high-performa
f3c70 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73  nce".** counters
f3c80 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43   for x86 class C
f3c90 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  PUs..**.** $Id: 
f3ca0 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32  hwtime.h,v 1.3 2
f3cb0 30 30 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a  008/08/01 14:33:
f3cc0 31 35 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a  15 shane Exp $.*
f3cd0 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d  /.#ifndef _HWTIM
f3ce0 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57  E_H_.#define _HW
f3cf0 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54  TIME_H_../*.** T
f3d00 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75  he following rou
f3d10 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20  tine only works 
f3d20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73  on pentium-class
f3d30 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63   (or newer) proc
f3d40 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73  essors..** It us
f3d50 65 73 20 74 68 65 20 52 44 54 53 43 20 6f 70 63  es the RDTSC opc
f3d60 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 65 20  ode to read the 
f3d70 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75  cycle count valu
f3d80 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20  e out of the.** 
f3d90 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65  processor and re
f3da0 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65  turns that value
f3db0 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75  .  This can be u
f3dc0 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73  sed for high-res
f3dd0 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a  .** profiling..*
f3de0 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f  /.#if (defined(_
f3df0 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69  _GNUC__) || defi
f3e00 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26  ned(_MSC_VER)) &
f3e10 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e  & \.      (defin
f3e20 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69  ed(i386) || defi
f3e30 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c  ned(__i386__) ||
f3e40 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36   defined(_M_IX86
f3e50 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65  ))..  #if define
f3e60 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f  d(__GNUC__)..  _
f3e70 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65  _inline__ sqlite
f3e80 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48  _uint64 sqlite3H
f3e90 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20  wtime(void){.   
f3ea0 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c    unsigned int l
f3eb0 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73  o, hi;.     __as
f3ec0 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f  m__ __volatile__
f3ed0 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22   ("rdtsc" : "=a"
f3ee0 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29   (lo), "=d" (hi)
f3ef0 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28  );.     return (
f3f00 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69  sqlite_uint64)hi
f3f10 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d   << 32 | lo;.  }
f3f20 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65  ..  #elif define
f3f30 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f  d(_MSC_VER)..  _
f3f40 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29  _declspec(naked)
f3f50 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65   __inline sqlite
f3f60 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20  _uint64 __cdecl 
f3f70 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f  sqlite3Hwtime(vo
f3f80 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20  id){.     __asm 
f3f90 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 0a  {.        rdtsc.
f3fa0 20 20 20 20 20 20 20 20 72 65 74 20 20 20 20 20          ret     
f3fb0 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65    ; return value
f3fc0 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20   at EDX:EAX.    
f3fd0 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66   }.  }..  #endif
f3fe0 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64  ..#elif (defined
f3ff0 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65  (__GNUC__) && de
f4000 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f  fined(__x86_64__
f4010 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f  ))..  __inline__
f4020 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
f4030 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
f4040 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e  d){.      unsign
f4050 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20  ed long val;.   
f4060 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c     __asm__ __vol
f4070 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22  atile__ ("rdtsc"
f4080 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a   : "=A" (val));.
f4090 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c        return val
f40a0 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64  ;.  }. .#elif (d
f40b0 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29  efined(__GNUC__)
f40c0 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70   && defined(__pp
f40d0 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e  c__))..  __inlin
f40e0 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  e__ sqlite_uint6
f40f0 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  4 sqlite3Hwtime(
f4100 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73  void){.      uns
f4110 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
f4120 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e  retval;.      un
f4130 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b  signed long junk
f4140 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20  ;.      __asm__ 
f4150 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c  __volatile__ ("\
f4160 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20  n\.          1: 
f4170 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c       mftbu   %1\
f4180 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n\.             
f4190 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30       mftb    %L0
f41a0 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  \n\.            
f41b0 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 30        mftbu   %0
f41c0 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  \n\.            
f41d0 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 25 30        cmpw    %0
f41e0 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20  ,%1\n\.         
f41f0 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 20 20           bne    
f4200 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 20 20   1b".           
f4210 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72         : "=r" (r
f4220 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75  etval), "=r" (ju
f4230 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75  nk));.      retu
f4240 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a  rn retval;.  }..
f4250 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20  #else..  #error 
f4260 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74  Need implementat
f4270 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77  ion of sqlite3Hw
f4280 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20  time() for your 
f4290 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a  platform...  /*.
f42a0 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20    ** To compile 
f42b0 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e  without implemen
f42c0 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69  ting sqlite3Hwti
f42d0 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c  me() for your pl
f42e0 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75  atform,.  ** you
f42f0 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20   can remove the 
f4300 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64  above #error and
f4310 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69   use the followi
f4320 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e  ng.  ** stub fun
f4330 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c  ction.  You will
f4340 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70   lose timing sup
f4350 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20  port for many.  
f4360 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67 67  ** of the debugg
f4370 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20  ing and testing 
f4380 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69  utilities, but i
f4390 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a  t should at.  **
f43a0 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61   least compile a
f43b0 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c  nd run..  */.SQL
f43c0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 71  ITE_PRIVATE   sq
f43d0 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69  lite_uint64 sqli
f43e0 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b  te3Hwtime(void){
f43f0 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65   return ((sqlite
f4400 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23  _uint64)0); }..#
f4410 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a  endif..#endif /*
f4420 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d   !defined(_HWTIM
f4430 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  E_H_) */../*****
f4440 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
f4450 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a   hwtime.h ******
f4460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4480 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
f4490 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
f44a0 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
f44b0 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d  ft off in os_com
f44c0 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  mon.h **********
f44d0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69  ********/..stati
f44e0 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  c sqlite_uint64 
f44f0 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20  g_start;.static 
f4500 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f  sqlite_uint64 g_
f4510 65 6c 61 70 73 65 64 3b 0a 23 64 65 66 69 6e 65  elapsed;.#define
f4520 20 54 49 4d 45 52 5f 53 54 41 52 54 20 20 20 20   TIMER_START    
f4530 20 20 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 74     g_start=sqlit
f4540 65 33 48 77 74 69 6d 65 28 29 0a 23 64 65 66 69  e3Hwtime().#defi
f4550 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20  ne TIMER_END    
f4560 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 3d 73       g_elapsed=s
f4570 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 2d 67  qlite3Hwtime()-g
f4580 5f 73 74 61 72 74 0a 23 64 65 66 69 6e 65 20 54  _start.#define T
f4590 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20  IMER_ELAPSED    
f45a0 20 67 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 65   g_elapsed.#else
f45b0 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53  .#define TIMER_S
f45c0 54 41 52 54 0a 23 64 65 66 69 6e 65 20 54 49 4d  TART.#define TIM
f45d0 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 54  ER_END.#define T
f45e0 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20  IMER_ELAPSED    
f45f0 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34   ((sqlite_uint64
f4600 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  )0).#endif../*.*
f4610 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20  * If we compile 
f4620 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f  with the SQLITE_
f4630 54 45 53 54 20 6d 61 63 72 6f 20 73 65 74 2c 20  TEST macro set, 
f4640 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69  then the followi
f4650 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63  ng block.** of c
f4660 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73  ode will give us
f4670 20 74 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20   the ability to 
f4680 73 69 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20  simulate a disk 
f4690 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73  I/O error.  This
f46a0 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20  .** is used for 
f46b0 74 65 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20  testing the I/O 
f46c0 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a  recovery logic..
f46d0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
f46e0 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
f46f0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f   int sqlite3_io_
f4700 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20  error_hit = 0;  
f4710 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74            /* Tot
f4720 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f  al number of I/O
f4730 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54   Errors */.SQLIT
f4740 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
f4750 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68  3_io_error_hardh
f4760 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  it = 0;        /
f4770 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d  * Number of non-
f4780 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f  benign errors */
f4790 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
f47a0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
f47b0 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20  _pending = 0;   
f47c0 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f       /* Count do
f47d0 77 6e 20 74 6f 20 66 69 72 73 74 20 49 2f 4f 20  wn to first I/O 
f47e0 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f  error */.SQLITE_
f47f0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
f4800 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74  io_error_persist
f4810 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
f4820 54 72 75 65 20 69 66 20 49 2f 4f 20 65 72 72 6f  True if I/O erro
f4830 72 73 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 51  rs persist */.SQ
f4840 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
f4850 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65  ite3_io_error_be
f4860 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 20  nign = 0;       
f4870 20 20 2f 2a 20 54 72 75 65 20 69 66 20 65 72 72    /* True if err
f4880 6f 72 73 20 61 72 65 20 62 65 6e 69 67 6e 20 2a  ors are benign *
f4890 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
f48a0 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
f48b0 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53  l_pending = 0;.S
f48c0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
f48d0 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d  lite3_diskfull =
f48e0 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d 75   0;.#define Simu
f48f0 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67  lateIOErrorBenig
f4900 6e 28 58 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f  n(X) sqlite3_io_
f4910 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29  error_benign=(X)
f4920 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74  .#define Simulat
f4930 65 49 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 20  eIOError(CODE)  
f4940 5c 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65 33  \.  if( (sqlite3
f4950 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73  _io_error_persis
f4960 74 20 26 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f  t && sqlite3_io_
f4970 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20  error_hit) \.   
f4980 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69      || sqlite3_i
f4990 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d  o_error_pending-
f49a0 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 20  - == 1 )  \.    
f49b0 20 20 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 61            { loca
f49c0 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b  l_ioerr(); CODE;
f49d0 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c   }.static void l
f49e0 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20  ocal_ioerr(){.  
f49f0 49 4f 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c  IOTRACE(("IOERR\
f4a00 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  n"));.  sqlite3_
f4a10 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a  io_error_hit++;.
f4a20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f 69    if( !sqlite3_i
f4a30 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29  o_error_benign )
f4a40 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
f4a50 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23  r_hardhit++;.}.#
f4a60 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44  define SimulateD
f4a70 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44  iskfullError(COD
f4a80 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69  E) \.   if( sqli
f4a90 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e  te3_diskfull_pen
f4aa0 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69  ding ){ \.     i
f4ab0 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66  f( sqlite3_diskf
f4ac0 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31  ull_pending == 1
f4ad0 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63   ){ \.       loc
f4ae0 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20  al_ioerr(); \.  
f4af0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73       sqlite3_dis
f4b00 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20  kfull = 1; \.   
f4b10 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65      sqlite3_io_e
f4b20 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a  rror_hit = 1; \.
f4b30 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20         CODE; \. 
f4b40 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20      }else{ \.   
f4b50 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b      sqlite3_disk
f4b60 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20  full_pending--; 
f4b70 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a  \.     } \.   }.
f4b80 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69  #else.#define Si
f4b90 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e  mulateIOErrorBen
f4ba0 69 67 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 53  ign(X).#define S
f4bb0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41  imulateIOError(A
f4bc0 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  ).#define Simula
f4bd0 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28  teDiskfullError(
f4be0 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  A).#endif../*.**
f4bf0 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b   When testing, k
f4c00 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74  eep a count of t
f4c10 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65  he number of ope
f4c20 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64  n files..*/.#ifd
f4c30 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
f4c40 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
f4c50 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f  lite3_open_file_
f4c60 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69  count = 0;.#defi
f4c70 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58  ne OpenCounter(X
f4c80 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  )  sqlite3_open_
f4c90 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a  file_count+=(X).
f4ca0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 70  #else.#define Op
f4cb0 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e  enCounter(X).#en
f4cc0 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21  dif..#endif /* !
f4cd0 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d  defined(_OS_COMM
f4ce0 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  ON_H_) */../****
f4cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
f4d00 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a  f os_common.h **
f4d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
f4d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
f4d50 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
f4d60 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 77 69  eft off in os_wi
f4d70 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
f4d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a  *********/../*.*
f4d90 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 74  * Some microsoft
f4da0 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b 20   compilers lack 
f4db0 74 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e  this definition.
f4dc0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56 41  .*/.#ifndef INVA
f4dd0 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55  LID_FILE_ATTRIBU
f4de0 54 45 53 0a 23 20 64 65 66 69 6e 65 20 49 4e 56  TES.# define INV
f4df0 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42  ALID_FILE_ATTRIB
f4e00 55 54 45 53 20 28 28 44 57 4f 52 44 29 2d 31 29  UTES ((DWORD)-1)
f4e10 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20   .#endif../*.** 
f4e20 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65 20  Determine if we 
f4e30 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68  are dealing with
f4e40 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 69   WindowsCE - whi
f4e50 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a 2a 2a  ch has a much.**
f4e60 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a 2a 2f   reduced API..*/
f4e70 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
f4e80 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 41 72  INCE.# define Ar
f4e90 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20  eFileApisANSI() 
f4ea0 31 0a 23 20 64 65 66 69 6e 65 20 47 65 74 44 69  1.# define GetDi
f4eb0 73 6b 46 72 65 65 53 70 61 63 65 57 28 29 20 30  skFreeSpaceW() 0
f4ec0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57  .#endif../*.** W
f4ed0 69 6e 43 45 20 6c 61 63 6b 73 20 6e 61 74 69 76  inCE lacks nativ
f4ee0 65 20 73 75 70 70 6f 72 74 20 66 6f 72 20 66 69  e support for fi
f4ef0 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 6f 20 77 65  le locking so we
f4f00 20 68 61 76 65 20 74 6f 20 66 61 6b 65 20 69 74   have to fake it
f4f10 0a 2a 2a 20 77 69 74 68 20 73 6f 6d 65 20 63 6f  .** with some co
f4f20 64 65 20 6f 66 20 6f 75 72 20 6f 77 6e 2e 0a 2a  de of our own..*
f4f30 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  /.#if SQLITE_OS_
f4f40 57 49 4e 43 45 0a 74 79 70 65 64 65 66 20 73 74  WINCE.typedef st
f4f50 72 75 63 74 20 77 69 6e 63 65 4c 6f 63 6b 20 7b  ruct winceLock {
f4f60 0a 20 20 69 6e 74 20 6e 52 65 61 64 65 72 73 3b  .  int nReaders;
f4f70 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
f4f80 20 6f 66 20 72 65 61 64 65 72 20 6c 6f 63 6b 73   of reader locks
f4f90 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42   obtained */.  B
f4fa0 4f 4f 4c 20 62 50 65 6e 64 69 6e 67 3b 20 20 20  OOL bPending;   
f4fb0 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20     /* Indicates 
f4fc0 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 68  a pending lock h
f4fd0 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64  as been obtained
f4fe0 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 52 65 73 65   */.  BOOL bRese
f4ff0 72 76 65 64 3b 20 20 20 20 20 2f 2a 20 49 6e 64  rved;     /* Ind
f5000 69 63 61 74 65 73 20 61 20 72 65 73 65 72 76 65  icates a reserve
f5010 64 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20  d lock has been 
f5020 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42 4f  obtained */.  BO
f5030 4f 4c 20 62 45 78 63 6c 75 73 69 76 65 3b 20 20  OL bExclusive;  
f5040 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20 61    /* Indicates a
f5050 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
f5060 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e   has been obtain
f5070 65 64 20 2a 2f 0a 7d 20 77 69 6e 63 65 4c 6f 63  ed */.} winceLoc
f5080 6b 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  k;.#endif../*.**
f5090 20 54 68 65 20 77 69 6e 46 69 6c 65 20 73 74 72   The winFile str
f50a0 75 63 74 75 72 65 20 69 73 20 61 20 73 75 62 63  ucture is a subc
f50b0 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f  lass of sqlite3_
f50c0 66 69 6c 65 2a 20 73 70 65 63 69 66 69 63 20 74  file* specific t
f50d0 6f 20 74 68 65 20 77 69 6e 33 32 0a 2a 2a 20 70  o the win32.** p
f50e0 6f 72 74 61 62 69 6c 69 74 79 20 6c 61 79 65 72  ortability layer
f50f0 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
f5100 75 63 74 20 77 69 6e 46 69 6c 65 20 77 69 6e 46  uct winFile winF
f5110 69 6c 65 3b 0a 73 74 72 75 63 74 20 77 69 6e 46  ile;.struct winF
f5120 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71  ile {.  const sq
f5130 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
f5140 20 2a 70 4d 65 74 68 6f 64 3b 2f 2a 20 4d 75 73   *pMethod;/* Mus
f5150 74 20 62 65 20 66 69 72 73 74 20 2a 2f 0a 20 20  t be first */.  
f5160 48 41 4e 44 4c 45 20 68 3b 20 20 20 20 20 20 20  HANDLE h;       
f5170 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c          /* Handl
f5180 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20  e for accessing 
f5190 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e  the file */.  un
f51a0 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b  signed char lock
f51b0 74 79 70 65 3b 20 2f 2a 20 54 79 70 65 20 6f 66  type; /* Type of
f51c0 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 20   lock currently 
f51d0 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 6c  held on this fil
f51e0 65 20 2a 2f 0a 20 20 73 68 6f 72 74 20 73 68 61  e */.  short sha
f51f0 72 65 64 4c 6f 63 6b 42 79 74 65 3b 20 20 20 2f  redLockByte;   /
f5200 2a 20 52 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65  * Randomly chose
f5210 6e 20 62 79 74 65 20 75 73 65 64 20 61 73 20 61  n byte used as a
f5220 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a   shared lock */.
f5230 20 20 44 57 4f 52 44 20 6c 61 73 74 45 72 72 6e    DWORD lastErrn
f5240 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  o;        /* The
f5250 20 57 69 6e 64 6f 77 73 20 65 72 72 6e 6f 20 66   Windows errno f
f5260 72 6f 6d 20 74 68 65 20 6c 61 73 74 20 49 2f 4f  rom the last I/O
f5270 20 65 72 72 6f 72 20 2a 2f 0a 20 20 44 57 4f 52   error */.  DWOR
f5280 44 20 73 65 63 74 6f 72 53 69 7a 65 3b 20 20 20  D sectorSize;   
f5290 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 69      /* Sector si
f52a0 7a 65 20 6f 66 20 74 68 65 20 64 65 76 69 63 65  ze of the device
f52b0 20 66 69 6c 65 20 69 73 20 6f 6e 20 2a 2f 0a 23   file is on */.#
f52c0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
f52d0 43 45 0a 20 20 57 43 48 41 52 20 2a 7a 44 65 6c  CE.  WCHAR *zDel
f52e0 65 74 65 4f 6e 43 6c 6f 73 65 3b 20 20 2f 2a 20  eteOnClose;  /* 
f52f0 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20  Name of file to 
f5300 64 65 6c 65 74 65 20 77 68 65 6e 20 63 6c 6f 73  delete when clos
f5310 69 6e 67 20 2a 2f 0a 20 20 48 41 4e 44 4c 45 20  ing */.  HANDLE 
f5320 68 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20  hMutex;         
f5330 20 2f 2a 20 4d 75 74 65 78 20 75 73 65 64 20 74   /* Mutex used t
f5340 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73  o control access
f5350 20 74 6f 20 73 68 61 72 65 64 20 6c 6f 63 6b 20   to shared lock 
f5360 2a 2f 20 20 0a 20 20 48 41 4e 44 4c 45 20 68 53  */  .  HANDLE hS
f5370 68 61 72 65 64 3b 20 20 20 20 20 20 20 20 20 2f  hared;         /
f5380 2a 20 53 68 61 72 65 64 20 6d 65 6d 6f 72 79 20  * Shared memory 
f5390 73 65 67 6d 65 6e 74 20 75 73 65 64 20 66 6f 72  segment used for
f53a0 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 77 69   locking */.  wi
f53b0 6e 63 65 4c 6f 63 6b 20 6c 6f 63 61 6c 3b 20 20  nceLock local;  
f53c0 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 73 20 6f        /* Locks o
f53d0 62 74 61 69 6e 65 64 20 62 79 20 74 68 69 73 20  btained by this 
f53e0 69 6e 73 74 61 6e 63 65 20 6f 66 20 77 69 6e 46  instance of winF
f53f0 69 6c 65 20 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f  ile */.  winceLo
f5400 63 6b 20 2a 73 68 61 72 65 64 3b 20 20 20 20 20  ck *shared;     
f5410 20 2f 2a 20 47 6c 6f 62 61 6c 20 73 68 61 72 65   /* Global share
f5420 64 20 6c 6f 63 6b 20 6d 65 6d 6f 72 79 20 66 6f  d lock memory fo
f5430 72 20 74 68 65 20 66 69 6c 65 20 20 2a 2f 0a 23  r the file  */.#
f5440 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  endif.};../*.** 
f5450 46 6f 72 77 61 72 64 20 70 72 6f 74 6f 74 79 70  Forward prototyp
f5460 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  es..*/.static in
f5470 74 20 67 65 74 53 65 63 74 6f 72 53 69 7a 65 28  t getSectorSize(
f5480 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73  .    sqlite3_vfs
f5490 20 2a 70 56 66 73 2c 0a 20 20 20 20 63 6f 6e 73   *pVfs,.    cons
f54a0 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76  t char *zRelativ
f54b0 65 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 66  e     /* UTF-8 f
f54c0 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 29 3b 0a 0a  ile name */.);..
f54d0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
f54e0 69 6e 67 20 76 61 72 69 61 62 6c 65 20 69 73 20  ing variable is 
f54f0 28 6e 6f 72 6d 61 6c 6c 79 29 20 73 65 74 20 6f  (normally) set o
f5500 6e 63 65 20 61 6e 64 20 6e 65 76 65 72 20 63 68  nce and never ch
f5510 61 6e 67 65 73 0a 2a 2a 20 74 68 65 72 65 61 66  anges.** thereaf
f5520 74 65 72 2e 20 20 49 74 20 72 65 63 6f 72 64 73  ter.  It records
f5530 20 77 68 65 74 68 65 72 20 74 68 65 20 6f 70 65   whether the ope
f5540 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73  rating system is
f5550 20 57 69 6e 39 35 0a 2a 2a 20 6f 72 20 57 69 6e   Win95.** or Win
f5560 4e 54 2e 0a 2a 2a 0a 2a 2a 20 30 3a 20 20 20 4f  NT..**.** 0:   O
f5570 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
f5580 75 6e 6b 6e 6f 77 6e 2e 0a 2a 2a 20 31 3a 20 20  unknown..** 1:  
f5590 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   Operating syste
f55a0 6d 20 69 73 20 57 69 6e 39 35 2e 0a 2a 2a 20 32  m is Win95..** 2
f55b0 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79  :   Operating sy
f55c0 73 74 65 6d 20 69 73 20 57 69 6e 4e 54 2e 0a 2a  stem is WinNT..*
f55d0 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 74 6f  *.** In order to
f55e0 20 66 61 63 69 6c 69 74 61 74 65 20 74 65 73 74   facilitate test
f55f0 69 6e 67 20 6f 6e 20 61 20 57 69 6e 4e 54 20 73  ing on a WinNT s
f5600 79 73 74 65 6d 2c 20 74 68 65 20 74 65 73 74 20  ystem, the test 
f5610 66 69 78 74 75 72 65 0a 2a 2a 20 63 61 6e 20 6d  fixture.** can m
f5620 61 6e 75 61 6c 6c 79 20 73 65 74 20 74 68 69 73  anually set this
f5630 20 76 61 6c 75 65 20 74 6f 20 31 20 74 6f 20 65   value to 1 to e
f5640 6d 75 6c 61 74 65 20 57 69 6e 39 38 20 62 65 68  mulate Win98 beh
f5650 61 76 69 6f 72 2e 0a 2a 2f 0a 23 69 66 64 65 66  avior..*/.#ifdef
f5660 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
f5670 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
f5680 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30 3b  te3_os_type = 0;
f5690 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 69 6e  .#else.static in
f56a0 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70  t sqlite3_os_typ
f56b0 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f  e = 0;.#endif../
f56c0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
f56d0 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77   (non-zero) if w
f56e0 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e  e are running un
f56f0 64 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b  der WinNT, Win2K
f5700 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57  , WinXP,.** or W
f5710 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61  inCE.  Return fa
f5720 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57  lse (zero) for W
f5730 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20  in95, Win98, or 
f5740 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72  WinME..**.** Her
f5750 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74  e is an interest
f5760 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a  ing observation:
f5770 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20    Win95, Win98, 
f5780 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a  and WinME lack.*
f5790 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78  * the LockFileEx
f57a0 28 29 20 41 50 49 2e 20 20 42 75 74 20 77 65 20  () API.  But we 
f57b0 63 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63  can still static
f57c0 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73  ally link agains
f57d0 74 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73  t that.** API as
f57e0 20 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27   long as we don'
f57f0 74 20 63 61 6c 6c 20 69 74 20 77 68 65 6e 20 72  t call it when r
f5800 75 6e 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f  unning Win95/98/
f5810 4d 45 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a  ME.  A call to.*
f5820 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * this routine i
f5830 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  s used to determ
f5840 69 6e 65 20 69 66 20 74 68 65 20 68 6f 73 74 20  ine if the host 
f5850 69 73 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f  is Win95/98/ME o
f5860 72 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50  r.** WinNT/2K/XP
f5870 20 73 6f 20 74 68 61 74 20 77 65 20 77 69 6c 6c   so that we will
f5880 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72   know whether or
f5890 20 6e 6f 74 20 77 65 20 63 61 6e 20 73 61 66 65   not we can safe
f58a0 6c 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c  ly call.** the L
f58b0 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e  ockFileEx() API.
f58c0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  .*/.#if SQLITE_O
f58d0 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65  S_WINCE.# define
f58e0 20 69 73 4e 54 28 29 20 20 28 31 29 0a 23 65 6c   isNT()  (1).#el
f58f0 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20  se.  static int 
f5900 69 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20 20  isNT(void){.    
f5910 69 66 28 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74  if( sqlite3_os_t
f5920 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ype==0 ){.      
f5930 4f 53 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73 49  OSVERSIONINFO sI
f5940 6e 66 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66 6f  nfo;.      sInfo
f5950 2e 64 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66 6f  .dwOSVersionInfo
f5960 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73 49  Size = sizeof(sI
f5970 6e 66 6f 29 3b 0a 20 20 20 20 20 20 47 65 74 56  nfo);.      GetV
f5980 65 72 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f 29  ersionEx(&sInfo)
f5990 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
f59a0 6f 73 5f 74 79 70 65 20 3d 20 73 49 6e 66 6f 2e  os_type = sInfo.
f59b0 64 77 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45  dwPlatformId==VE
f59c0 52 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32  R_PLATFORM_WIN32
f59d0 5f 4e 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20  _NT ? 2 : 1;.   
f59e0 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71   }.    return sq
f59f0 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d 32  lite3_os_type==2
f5a00 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;.  }.#endif /* 
f5a10 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20  SQLITE_OS_WINCE 
f5a20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  */../*.** Conver
f5a30 74 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67  t a UTF-8 string
f5a40 20 74 6f 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e   to microsoft un
f5a50 69 63 6f 64 65 20 28 55 54 46 2d 31 36 3f 29 2e  icode (UTF-16?).
f5a60 20 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f   .**.** Space to
f5a70 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e   hold the return
f5a80 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74  ed string is obt
f5a90 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f  ained from mallo
f5aa0 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43 48  c..*/.static WCH
f5ab0 41 52 20 2a 75 74 66 38 54 6f 55 6e 69 63 6f 64  AR *utf8ToUnicod
f5ac0 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  e(const char *zF
f5ad0 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20  ilename){.  int 
f5ae0 6e 43 68 61 72 3b 0a 20 20 57 43 48 41 52 20 2a  nChar;.  WCHAR *
f5af0 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a 0a  zWideFilename;..
f5b00 20 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74 69 42    nChar = MultiB
f5b10 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 43 50  yteToWideChar(CP
f5b20 5f 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c 65 6e  _UTF8, 0, zFilen
f5b30 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 20 30  ame, -1, NULL, 0
f5b40 29 3b 0a 20 20 7a 57 69 64 65 46 69 6c 65 6e 61  );.  zWideFilena
f5b50 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 43 68  me = malloc( nCh
f5b60 61 72 2a 73 69 7a 65 6f 66 28 7a 57 69 64 65 46  ar*sizeof(zWideF
f5b70 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20  ilename[0]) );. 
f5b80 20 69 66 28 20 7a 57 69 64 65 46 69 6c 65 6e 61   if( zWideFilena
f5b90 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  me==0 ){.    ret
f5ba0 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 43 68  urn 0;.  }.  nCh
f5bb0 61 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f  ar = MultiByteTo
f5bc0 57 69 64 65 43 68 61 72 28 43 50 5f 55 54 46 38  WideChar(CP_UTF8
f5bd0 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20  , 0, zFilename, 
f5be0 2d 31 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d  -1, zWideFilenam
f5bf0 65 2c 20 6e 43 68 61 72 29 3b 0a 20 20 69 66 28  e, nChar);.  if(
f5c00 20 6e 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20 20   nChar==0 ){.   
f5c10 20 66 72 65 65 28 7a 57 69 64 65 46 69 6c 65 6e   free(zWideFilen
f5c20 61 6d 65 29 3b 0a 20 20 20 20 7a 57 69 64 65 46  ame);.    zWideF
f5c30 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d  ilename = 0;.  }
f5c40 0a 20 20 72 65 74 75 72 6e 20 7a 57 69 64 65 46  .  return zWideF
f5c50 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  ilename;.}../*.*
f5c60 2a 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f 73  * Convert micros
f5c70 6f 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 55  oft unicode to U
f5c80 54 46 2d 38 2e 20 20 53 70 61 63 65 20 74 6f 20  TF-8.  Space to 
f5c90 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65  hold the returne
f5ca0 64 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 6f  d string is.** o
f5cb0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c  btained from mal
f5cc0 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  loc()..*/.static
f5cd0 20 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f   char *unicodeTo
f5ce0 55 74 66 38 28 63 6f 6e 73 74 20 57 43 48 41 52  Utf8(const WCHAR
f5cf0 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29   *zWideFilename)
f5d00 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20  {.  int nByte;. 
f5d10 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
f5d20 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64  ;..  nByte = Wid
f5d30 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65  eCharToMultiByte
f5d40 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 69  (CP_UTF8, 0, zWi
f5d50 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20  deFilename, -1, 
f5d60 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a  0, 0, 0, 0);.  z
f5d70 46 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f  Filename = mallo
f5d80 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66  c( nByte );.  if
f5d90 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29  ( zFilename==0 )
f5da0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
f5db0 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57 69    }.  nByte = Wi
f5dc0 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74  deCharToMultiByt
f5dd0 65 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57  e(CP_UTF8, 0, zW
f5de0 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c  ideFilename, -1,
f5df0 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74   zFilename, nByt
f5e00 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
f5e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f5e20 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42   0, 0);.  if( nB
f5e30 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20  yte == 0 ){.    
f5e40 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b  free(zFilename);
f5e50 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d  .    zFilename =
f5e60 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   0;.  }.  return
f5e70 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f   zFilename;.}../
f5e80 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20  *.** Convert an 
f5e90 61 6e 73 69 20 73 74 72 69 6e 67 20 74 6f 20 6d  ansi string to m
f5ea0 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65  icrosoft unicode
f5eb0 2c 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a  , based on the.*
f5ec0 2a 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61  * current codepa
f5ed0 67 65 20 73 65 74 74 69 6e 67 73 20 66 6f 72 20  ge settings for 
f5ee0 66 69 6c 65 20 61 70 69 73 2e 0a 2a 2a 20 0a 2a  file apis..** .*
f5ef0 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20  * Space to hold 
f5f00 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72  the returned str
f5f10 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 0a  ing is obtained.
f5f20 2a 2a 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a  ** from malloc..
f5f30 2a 2f 0a 73 74 61 74 69 63 20 57 43 48 41 52 20  */.static WCHAR 
f5f40 2a 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 63  *mbcsToUnicode(c
f5f50 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
f5f60 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79  name){.  int nBy
f5f70 74 65 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4d 62  te;.  WCHAR *zMb
f5f80 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e  csFilename;.  in
f5f90 74 20 63 6f 64 65 70 61 67 65 20 3d 20 41 72 65  t codepage = Are
f5fa0 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 3f  FileApisANSI() ?
f5fb0 20 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45 4d   CP_ACP : CP_OEM
f5fc0 43 50 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 4d  CP;..  nByte = M
f5fd0 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68  ultiByteToWideCh
f5fe0 61 72 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20  ar(codepage, 0, 
f5ff0 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 4e  zFilename, -1, N
f6000 55 4c 4c 2c 30 29 2a 73 69 7a 65 6f 66 28 57 43  ULL,0)*sizeof(WC
f6010 48 41 52 29 3b 0a 20 20 7a 4d 62 63 73 46 69 6c  HAR);.  zMbcsFil
f6020 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20  ename = malloc( 
f6030 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 4d 62  nByte*sizeof(zMb
f6040 63 73 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29  csFilename[0]) )
f6050 3b 0a 20 20 69 66 28 20 7a 4d 62 63 73 46 69 6c  ;.  if( zMbcsFil
f6060 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20  ename==0 ){.    
f6070 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
f6080 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 42 79 74  nByte = MultiByt
f6090 65 54 6f 57 69 64 65 43 68 61 72 28 63 6f 64 65  eToWideChar(code
f60a0 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65 6e 61  page, 0, zFilena
f60b0 6d 65 2c 20 2d 31 2c 20 7a 4d 62 63 73 46 69 6c  me, -1, zMbcsFil
f60c0 65 6e 61 6d 65 2c 20 6e 42 79 74 65 29 3b 0a 20  ename, nByte);. 
f60d0 20 69 66 28 20 6e 42 79 74 65 3d 3d 30 20 29 7b   if( nByte==0 ){
f60e0 0a 20 20 20 20 66 72 65 65 28 7a 4d 62 63 73 46  .    free(zMbcsF
f60f0 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 4d  ilename);.    zM
f6100 62 63 73 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b  bcsFilename = 0;
f6110 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4d  .  }.  return zM
f6120 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a  bcsFilename;.}..
f6130 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 69  /*.** Convert mi
f6140 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 20  crosoft unicode 
f6150 74 6f 20 6d 75 6c 74 69 62 79 74 65 20 63 68 61  to multibyte cha
f6160 72 61 63 74 65 72 20 73 74 72 69 6e 67 2c 20 62  racter string, b
f6170 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 75  ased on the.** u
f6180 73 65 72 27 73 20 41 6e 73 69 20 63 6f 64 65 70  ser's Ansi codep
f6190 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65  age..**.** Space
f61a0 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74   to hold the ret
f61b0 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20  urned string is 
f61c0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a  obtained from.**
f61d0 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74   malloc()..*/.st
f61e0 61 74 69 63 20 63 68 61 72 20 2a 75 6e 69 63 6f  atic char *unico
f61f0 64 65 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 57  deToMbcs(const W
f6200 43 48 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e  CHAR *zWideFilen
f6210 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74  ame){.  int nByt
f6220 65 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65  e;.  char *zFile
f6230 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64 65  name;.  int code
f6240 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65 41 70  page = AreFileAp
f6250 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f 41 43  isANSI() ? CP_AC
f6260 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a 0a 20  P : CP_OEMCP;.. 
f6270 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61   nByte = WideCha
f6280 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f 64  rToMultiByte(cod
f6290 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 65 46  epage, 0, zWideF
f62a0 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20  ilename, -1, 0, 
f62b0 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69 6c  0, 0, 0);.  zFil
f62c0 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20  ename = malloc( 
f62d0 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 7a  nByte );.  if( z
f62e0 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20  Filename==0 ){. 
f62f0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
f6300 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43  .  nByte = WideC
f6310 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63  harToMultiByte(c
f6320 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64  odepage, 0, zWid
f6330 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a  eFilename, -1, z
f6340 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 2c  Filename, nByte,
f6350 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
f6360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
f6370 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 79 74  , 0);.  if( nByt
f6380 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66 72  e == 0 ){.    fr
f6390 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  ee(zFilename);. 
f63a0 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30     zFilename = 0
f63b0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a  ;.  }.  return z
f63c0 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a  Filename;.}../*.
f63d0 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 75 6c 74 69  ** Convert multi
f63e0 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 73  byte character s
f63f0 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 2e 20  tring to UTF-8. 
f6400 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   Space to hold t
f6410 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 73  he.** returned s
f6420 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65  tring is obtaine
f6430 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e  d from malloc().
f6440 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
f6450 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 77 69 6e  har *sqlite3_win
f6460 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28  32_mbcs_to_utf8(
f6470 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
f6480 65 6e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a  ename){.  char *
f6490 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b 0a 20  zFilenameUtf8;. 
f64a0 20 57 43 48 41 52 20 2a 7a 54 6d 70 57 69 64 65   WCHAR *zTmpWide
f64b0 3b 0a 0a 20 20 7a 54 6d 70 57 69 64 65 20 3d 20  ;..  zTmpWide = 
f64c0 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 7a 46  mbcsToUnicode(zF
f64d0 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20  ilename);.  if( 
f64e0 7a 54 6d 70 57 69 64 65 3d 3d 30 20 29 7b 0a 20  zTmpWide==0 ){. 
f64f0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
f6500 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38  .  zFilenameUtf8
f6510 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38   = unicodeToUtf8
f6520 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 66 72  (zTmpWide);.  fr
f6530 65 65 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20  ee(zTmpWide);.  
f6540 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65  return zFilename
f6550 55 74 66 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  Utf8;.}../*.** C
f6560 6f 6e 76 65 72 74 20 55 54 46 2d 38 20 74 6f 20  onvert UTF-8 to 
f6570 6d 75 6c 74 69 62 79 74 65 20 63 68 61 72 61 63  multibyte charac
f6580 74 65 72 20 73 74 72 69 6e 67 2e 20 20 53 70 61  ter string.  Spa
f6590 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 0a  ce to hold the .
f65a0 2a 2a 20 72 65 74 75 72 6e 65 64 20 73 74 72 69  ** returned stri
f65b0 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66  ng is obtained f
f65c0 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f  rom malloc()..*/
f65d0 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 75 74  .static char *ut
f65e0 66 38 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 63  f8ToMbcs(const c
f65f0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b  har *zFilename){
f6600 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  .  char *zFilena
f6610 6d 65 4d 62 63 73 3b 0a 20 20 57 43 48 41 52 20  meMbcs;.  WCHAR 
f6620 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20 7a 54  *zTmpWide;..  zT
f6630 6d 70 57 69 64 65 20 3d 20 75 74 66 38 54 6f 55  mpWide = utf8ToU
f6640 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65  nicode(zFilename
f6650 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57 69 64  );.  if( zTmpWid
f6660 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  e==0 ){.    retu
f6670 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c  rn 0;.  }.  zFil
f6680 65 6e 61 6d 65 4d 62 63 73 20 3d 20 75 6e 69 63  enameMbcs = unic
f6690 6f 64 65 54 6f 4d 62 63 73 28 7a 54 6d 70 57 69  odeToMbcs(zTmpWi
f66a0 64 65 29 3b 0a 20 20 66 72 65 65 28 7a 54 6d 70  de);.  free(zTmp
f66b0 57 69 64 65 29 3b 0a 20 20 72 65 74 75 72 6e 20  Wide);.  return 
f66c0 7a 46 69 6c 65 6e 61 6d 65 4d 62 63 73 3b 0a 7d  zFilenameMbcs;.}
f66d0 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  ..#if SQLITE_OS_
f66e0 57 49 4e 43 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  WINCE./*********
f66f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6730 0a 2a 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e  .** This section
f6740 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 66   contains code f
f6750 6f 72 20 57 69 6e 43 45 20 6f 6e 6c 79 2e 0a 2a  or WinCE only..*
f6760 2f 0a 2f 2a 0a 2a 2a 20 57 69 6e 64 6f 77 73 43  /./*.** WindowsC
f6770 45 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20  E does not have 
f6780 61 20 6c 6f 63 61 6c 74 69 6d 65 28 29 20 66 75  a localtime() fu
f6790 6e 63 74 69 6f 6e 2e 20 20 53 6f 20 63 72 65 61  nction.  So crea
f67a0 74 65 20 61 0a 2a 2a 20 73 75 62 73 74 69 74 75  te a.** substitu
f67b0 74 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74 6d  te..*/.struct tm
f67c0 20 2a 5f 5f 63 64 65 63 6c 20 6c 6f 63 61 6c 74   *__cdecl localt
f67d0 69 6d 65 28 63 6f 6e 73 74 20 74 69 6d 65 5f 74  ime(const time_t
f67e0 20 2a 74 29 0a 7b 0a 20 20 73 74 61 74 69 63 20   *t).{.  static 
f67f0 73 74 72 75 63 74 20 74 6d 20 79 3b 0a 20 20 46  struct tm y;.  F
f6800 49 4c 45 54 49 4d 45 20 75 54 6d 2c 20 6c 54 6d  ILETIME uTm, lTm
f6810 3b 0a 20 20 53 59 53 54 45 4d 54 49 4d 45 20 70  ;.  SYSTEMTIME p
f6820 54 6d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  Tm;.  sqlite3_in
f6830 74 36 34 20 74 36 34 3b 0a 20 20 74 36 34 20 3d  t64 t64;.  t64 =
f6840 20 2a 74 3b 0a 20 20 74 36 34 20 3d 20 28 74 36   *t;.  t64 = (t6
f6850 34 20 2b 20 31 31 36 34 34 34 37 33 36 30 30 29  4 + 11644473600)
f6860 2a 31 30 30 30 30 30 30 30 3b 0a 20 20 75 54 6d  *10000000;.  uTm
f6870 2e 64 77 4c 6f 77 44 61 74 65 54 69 6d 65 20 3d  .dwLowDateTime =
f6880 20 28 44 57 4f 52 44 29 28 74 36 34 20 26 20 30   (DWORD)(t64 & 0
f6890 78 46 46 46 46 46 46 46 46 29 3b 0a 20 20 75 54  xFFFFFFFF);.  uT
f68a0 6d 2e 64 77 48 69 67 68 44 61 74 65 54 69 6d 65  m.dwHighDateTime
f68b0 3d 20 28 44 57 4f 52 44 29 28 74 36 34 20 3e 3e  = (DWORD)(t64 >>
f68c0 20 33 32 29 3b 0a 20 20 46 69 6c 65 54 69 6d 65   32);.  FileTime
f68d0 54 6f 4c 6f 63 61 6c 46 69 6c 65 54 69 6d 65 28  ToLocalFileTime(
f68e0 26 75 54 6d 2c 26 6c 54 6d 29 3b 0a 20 20 46 69  &uTm,&lTm);.  Fi
f68f0 6c 65 54 69 6d 65 54 6f 53 79 73 74 65 6d 54 69  leTimeToSystemTi
f6900 6d 65 28 26 6c 54 6d 2c 26 70 54 6d 29 3b 0a 20  me(&lTm,&pTm);. 
f6910 20 79 2e 74 6d 5f 79 65 61 72 20 3d 20 70 54 6d   y.tm_year = pTm
f6920 2e 77 59 65 61 72 20 2d 20 31 39 30 30 3b 0a 20  .wYear - 1900;. 
f6930 20 79 2e 74 6d 5f 6d 6f 6e 20 3d 20 70 54 6d 2e   y.tm_mon = pTm.
f6940 77 4d 6f 6e 74 68 20 2d 20 31 3b 0a 20 20 79 2e  wMonth - 1;.  y.
f6950 74 6d 5f 77 64 61 79 20 3d 20 70 54 6d 2e 77 44  tm_wday = pTm.wD
f6960 61 79 4f 66 57 65 65 6b 3b 0a 20 20 79 2e 74 6d  ayOfWeek;.  y.tm
f6970 5f 6d 64 61 79 20 3d 20 70 54 6d 2e 77 44 61 79  _mday = pTm.wDay
f6980 3b 0a 20 20 79 2e 74 6d 5f 68 6f 75 72 20 3d 20  ;.  y.tm_hour = 
f6990 70 54 6d 2e 77 48 6f 75 72 3b 0a 20 20 79 2e 74  pTm.wHour;.  y.t
f69a0 6d 5f 6d 69 6e 20 3d 20 70 54 6d 2e 77 4d 69 6e  m_min = pTm.wMin
f69b0 75 74 65 3b 0a 20 20 79 2e 74 6d 5f 73 65 63 20  ute;.  y.tm_sec 
f69c0 3d 20 70 54 6d 2e 77 53 65 63 6f 6e 64 3b 0a 20  = pTm.wSecond;. 
f69d0 20 72 65 74 75 72 6e 20 26 79 3b 0a 7d 0a 0a 2f   return &y;.}../
f69e0 2a 20 54 68 69 73 20 77 69 6c 6c 20 6e 65 76 65  * This will neve
f69f0 72 20 62 65 20 63 61 6c 6c 65 64 2c 20 62 75 74  r be called, but
f6a00 20 64 65 66 69 6e 65 64 20 74 6f 20 6d 61 6b 65   defined to make
f6a10 20 74 68 65 20 63 6f 64 65 20 63 6f 6d 70 69 6c   the code compil
f6a20 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 47 65 74  e */.#define Get
f6a30 54 65 6d 70 50 61 74 68 41 28 61 2c 62 29 0a 0a  TempPathA(a,b)..
f6a40 23 64 65 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65  #define LockFile
f6a50 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20 20  (a,b,c,d,e)     
f6a60 20 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28    winceLockFile(
f6a70 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a  &a, b, c, d, e).
f6a80 23 64 65 66 69 6e 65 20 55 6e 6c 6f 63 6b 46 69  #define UnlockFi
f6a90 6c 65 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20 20  le(a,b,c,d,e)   
f6aa0 20 20 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c    winceUnlockFil
f6ab0 65 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65  e(&a, b, c, d, e
f6ac0 29 0a 23 64 65 66 69 6e 65 20 4c 6f 63 6b 46 69  ).#define LockFi
f6ad0 6c 65 45 78 28 61 2c 62 2c 63 2c 64 2c 65 2c 66  leEx(a,b,c,d,e,f
f6ae0 29 20 20 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c  )   winceLockFil
f6af0 65 45 78 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c  eEx(&a, b, c, d,
f6b00 20 65 2c 20 66 29 0a 0a 23 64 65 66 69 6e 65 20   e, f)..#define 
f6b10 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c  HANDLE_TO_WINFIL
f6b20 45 28 61 29 20 28 77 69 6e 46 69 6c 65 2a 29 26  E(a) (winFile*)&
f6b30 28 28 63 68 61 72 2a 29 61 29 5b 2d 28 69 6e 74  ((char*)a)[-(int
f6b40 29 6f 66 66 73 65 74 6f 66 28 77 69 6e 46 69 6c  )offsetof(winFil
f6b50 65 2c 68 29 5d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71  e,h)]../*.** Acq
f6b60 75 69 72 65 20 61 20 6c 6f 63 6b 20 6f 6e 20 74  uire a lock on t
f6b70 68 65 20 68 61 6e 64 6c 65 20 68 0a 2a 2f 0a 73  he handle h.*/.s
f6b80 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 63 65  tatic void wince
f6b90 4d 75 74 65 78 41 63 71 75 69 72 65 28 48 41 4e  MutexAcquire(HAN
f6ba0 44 4c 45 20 68 29 7b 0a 20 20 20 44 57 4f 52 44  DLE h){.   DWORD
f6bb0 20 64 77 45 72 72 3b 0a 20 20 20 64 6f 20 7b 0a   dwErr;.   do {.
f6bc0 20 20 20 20 20 64 77 45 72 72 20 3d 20 57 61 69       dwErr = Wai
f6bd0 74 46 6f 72 53 69 6e 67 6c 65 4f 62 6a 65 63 74  tForSingleObject
f6be0 28 68 2c 20 49 4e 46 49 4e 49 54 45 29 3b 0a 20  (h, INFINITE);. 
f6bf0 20 20 7d 20 77 68 69 6c 65 20 28 64 77 45 72 72    } while (dwErr
f6c00 20 21 3d 20 57 41 49 54 5f 4f 42 4a 45 43 54 5f   != WAIT_OBJECT_
f6c10 30 20 26 26 20 64 77 45 72 72 20 21 3d 20 57 41  0 && dwErr != WA
f6c20 49 54 5f 41 42 41 4e 44 4f 4e 45 44 29 3b 0a 7d  IT_ABANDONED);.}
f6c30 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61  ./*.** Release a
f6c40 20 6c 6f 63 6b 20 61 63 71 75 69 72 65 64 20 62   lock acquired b
f6c50 79 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75  y winceMutexAcqu
f6c60 69 72 65 28 29 0a 2a 2f 0a 23 64 65 66 69 6e 65  ire().*/.#define
f6c70 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61   winceMutexRelea
f6c80 73 65 28 68 29 20 52 65 6c 65 61 73 65 4d 75 74  se(h) ReleaseMut
f6c90 65 78 28 68 29 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  ex(h)../*.** Cre
f6ca0 61 74 65 20 74 68 65 20 6d 75 74 65 78 20 61 6e  ate the mutex an
f6cb0 64 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20  d shared memory 
f6cc0 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67  used for locking
f6cd0 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20   in the file.** 
f6ce0 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65  descriptor pFile
f6cf0 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20  .*/.static BOOL 
f6d00 77 69 6e 63 65 43 72 65 61 74 65 4c 6f 63 6b 28  winceCreateLock(
f6d10 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
f6d20 65 6e 61 6d 65 2c 20 77 69 6e 46 69 6c 65 20 2a  ename, winFile *
f6d30 70 46 69 6c 65 29 7b 0a 20 20 57 43 48 41 52 20  pFile){.  WCHAR 
f6d40 2a 7a 54 6f 6b 3b 0a 20 20 57 43 48 41 52 20 2a  *zTok;.  WCHAR *
f6d50 7a 4e 61 6d 65 20 3d 20 75 74 66 38 54 6f 55 6e  zName = utf8ToUn
f6d60 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29  icode(zFilename)
f6d70 3b 0a 20 20 42 4f 4f 4c 20 62 49 6e 69 74 20 3d  ;.  BOOL bInit =
f6d80 20 54 52 55 45 3b 0a 0a 20 20 2f 2a 20 49 6e 69   TRUE;..  /* Ini
f6d90 74 69 61 6c 69 7a 65 20 74 68 65 20 6c 6f 63 61  tialize the loca
f6da0 6c 20 6c 6f 63 6b 64 61 74 61 20 2a 2f 0a 20 20  l lockdata */.  
f6db0 5a 65 72 6f 4d 65 6d 6f 72 79 28 26 70 46 69 6c  ZeroMemory(&pFil
f6dc0 65 2d 3e 6c 6f 63 61 6c 2c 20 73 69 7a 65 6f 66  e->local, sizeof
f6dd0 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 29 29 3b  (pFile->local));
f6de0 0a 0a 20 20 2f 2a 20 52 65 70 6c 61 63 65 20 74  ..  /* Replace t
f6df0 68 65 20 62 61 63 6b 73 6c 61 73 68 65 73 20 66  he backslashes f
f6e00 72 6f 6d 20 74 68 65 20 66 69 6c 65 6e 61 6d 65  rom the filename
f6e10 20 61 6e 64 20 6c 6f 77 65 72 63 61 73 65 20 69   and lowercase i
f6e20 74 0a 20 20 2a 2a 20 74 6f 20 64 65 72 69 76 65  t.  ** to derive
f6e30 20 61 20 6d 75 74 65 78 20 6e 61 6d 65 2e 20 2a   a mutex name. *
f6e40 2f 0a 20 20 7a 54 6f 6b 20 3d 20 43 68 61 72 4c  /.  zTok = CharL
f6e50 6f 77 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20 20  owerW(zName);.  
f6e60 66 6f 72 20 28 3b 2a 7a 54 6f 6b 3b 7a 54 6f 6b  for (;*zTok;zTok
f6e70 2b 2b 29 7b 0a 20 20 20 20 69 66 20 28 2a 7a 54  ++){.    if (*zT
f6e80 6f 6b 20 3d 3d 20 27 5c 5c 27 29 20 2a 7a 54 6f  ok == '\\') *zTo
f6e90 6b 20 3d 20 27 5f 27 3b 0a 20 20 7d 0a 0a 20 20  k = '_';.  }..  
f6ea0 2f 2a 20 43 72 65 61 74 65 2f 6f 70 65 6e 20 74  /* Create/open t
f6eb0 68 65 20 6e 61 6d 65 64 20 6d 75 74 65 78 20 2a  he named mutex *
f6ec0 2f 0a 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65  /.  pFile->hMute
f6ed0 78 20 3d 20 43 72 65 61 74 65 4d 75 74 65 78 57  x = CreateMutexW
f6ee0 28 4e 55 4c 4c 2c 20 46 41 4c 53 45 2c 20 7a 4e  (NULL, FALSE, zN
f6ef0 61 6d 65 29 3b 0a 20 20 69 66 20 28 21 70 46 69  ame);.  if (!pFi
f6f00 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a 20 20 20  le->hMutex){.   
f6f10 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
f6f20 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72  o = GetLastError
f6f30 28 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 4e 61  ();.    free(zNa
f6f40 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  me);.    return 
f6f50 46 41 4c 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  FALSE;.  }..  /*
f6f60 20 41 63 71 75 69 72 65 20 74 68 65 20 6d 75 74   Acquire the mut
f6f70 65 78 20 62 65 66 6f 72 65 20 63 6f 6e 74 69 6e  ex before contin
f6f80 75 69 6e 67 20 2a 2f 0a 20 20 77 69 6e 63 65 4d  uing */.  winceM
f6f90 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c  utexAcquire(pFil
f6fa0 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 0a 20  e->hMutex);.  . 
f6fb0 20 2f 2a 20 53 69 6e 63 65 20 74 68 65 20 6e 61   /* Since the na
f6fc0 6d 65 73 20 6f 66 20 6e 61 6d 65 64 20 6d 75 74  mes of named mut
f6fd0 65 78 65 73 2c 20 73 65 6d 61 70 68 6f 72 65 73  exes, semaphores
f6fe0 2c 20 66 69 6c 65 20 6d 61 70 70 69 6e 67 73 20  , file mappings 
f6ff0 65 74 63 20 61 72 65 20 0a 20 20 2a 2a 20 63 61  etc are .  ** ca
f7000 73 65 2d 73 65 6e 73 69 74 69 76 65 2c 20 74 61  se-sensitive, ta
f7010 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f 66 20  ke advantage of 
f7020 74 68 61 74 20 62 79 20 75 70 70 65 72 63 61 73  that by uppercas
f7030 69 6e 67 20 74 68 65 20 6d 75 74 65 78 20 6e 61  ing the mutex na
f7040 6d 65 0a 20 20 2a 2a 20 61 6e 64 20 75 73 69 6e  me.  ** and usin
f7050 67 20 74 68 61 74 20 61 73 20 74 68 65 20 73 68  g that as the sh
f7060 61 72 65 64 20 66 69 6c 65 6d 61 70 70 69 6e 67  ared filemapping
f7070 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 43 68   name..  */.  Ch
f7080 61 72 55 70 70 65 72 57 28 7a 4e 61 6d 65 29 3b  arUpperW(zName);
f7090 0a 20 20 70 46 69 6c 65 2d 3e 68 53 68 61 72 65  .  pFile->hShare
f70a0 64 20 3d 20 43 72 65 61 74 65 46 69 6c 65 4d 61  d = CreateFileMa
f70b0 70 70 69 6e 67 57 28 49 4e 56 41 4c 49 44 5f 48  ppingW(INVALID_H
f70c0 41 4e 44 4c 45 5f 56 41 4c 55 45 2c 20 4e 55 4c  ANDLE_VALUE, NUL
f70d0 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  L,.             
f70e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f70f0 20 20 20 20 20 20 20 20 20 20 50 41 47 45 5f 52            PAGE_R
f7100 45 41 44 57 52 49 54 45 2c 20 30 2c 20 73 69 7a  EADWRITE, 0, siz
f7110 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 2c 0a  eof(winceLock),.
f7120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f7130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f7140 20 20 20 20 20 20 20 7a 4e 61 6d 65 29 3b 20 20         zName);  
f7150 0a 0a 20 20 2f 2a 20 53 65 74 20 61 20 66 6c 61  ..  /* Set a fla
f7160 67 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 73  g that indicates
f7170 20 77 65 27 72 65 20 74 68 65 20 66 69 72 73 74   we're the first
f7180 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 6d   to create the m
f7190 65 6d 6f 72 79 20 73 6f 20 69 74 20 0a 20 20 2a  emory so it .  *
f71a0 2a 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d 69  * must be zero-i
f71b0 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20  nitialized */.  
f71c0 69 66 20 28 47 65 74 4c 61 73 74 45 72 72 6f 72  if (GetLastError
f71d0 28 29 20 3d 3d 20 45 52 52 4f 52 5f 41 4c 52 45  () == ERROR_ALRE
f71e0 41 44 59 5f 45 58 49 53 54 53 29 7b 0a 20 20 20  ADY_EXISTS){.   
f71f0 20 62 49 6e 69 74 20 3d 20 46 41 4c 53 45 3b 0a   bInit = FALSE;.
f7200 20 20 7d 0a 0a 20 20 66 72 65 65 28 7a 4e 61 6d    }..  free(zNam
f7210 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20  e);..  /* If we 
f7220 73 75 63 63 65 65 64 65 64 20 69 6e 20 6d 61 6b  succeeded in mak
f7230 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6d  ing the shared m
f7240 65 6d 6f 72 79 20 68 61 6e 64 6c 65 2c 20 6d 61  emory handle, ma
f7250 70 20 69 74 2e 20 2a 2f 0a 20 20 69 66 20 28 70  p it. */.  if (p
f7260 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 29 7b 0a  File->hShared){.
f7270 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65      pFile->share
f7280 64 20 3d 20 28 77 69 6e 63 65 4c 6f 63 6b 2a 29  d = (winceLock*)
f7290 4d 61 70 56 69 65 77 4f 66 46 69 6c 65 28 70 46  MapViewOfFile(pF
f72a0 69 6c 65 2d 3e 68 53 68 61 72 65 64 2c 20 0a 20  ile->hShared, . 
f72b0 20 20 20 20 20 20 20 20 20 20 20 20 46 49 4c 45              FILE
f72c0 5f 4d 41 50 5f 52 45 41 44 7c 46 49 4c 45 5f 4d  _MAP_READ|FILE_M
f72d0 41 50 5f 57 52 49 54 45 2c 20 30 2c 20 30 2c 20  AP_WRITE, 0, 0, 
f72e0 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b  sizeof(winceLock
f72f0 29 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 6d 61  ));.    /* If ma
f7300 70 70 69 6e 67 20 66 61 69 6c 65 64 2c 20 63 6c  pping failed, cl
f7310 6f 73 65 20 74 68 65 20 73 68 61 72 65 64 20 6d  ose the shared m
f7320 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20 61 6e 64  emory handle and
f7330 20 65 72 61 73 65 20 69 74 20 2a 2f 0a 20 20 20   erase it */.   
f7340 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 73 68 61   if (!pFile->sha
f7350 72 65 64 29 7b 0a 20 20 20 20 20 20 70 46 69 6c  red){.      pFil
f7360 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47  e->lastErrno = G
f7370 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20  etLastError();. 
f7380 20 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65       CloseHandle
f7390 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 29  (pFile->hShared)
f73a0 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68  ;.      pFile->h
f73b0 53 68 61 72 65 64 20 3d 20 4e 55 4c 4c 3b 0a 20  Shared = NULL;. 
f73c0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
f73d0 66 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20  f shared memory 
f73e0 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 72 65  could not be cre
f73f0 61 74 65 64 2c 20 74 68 65 6e 20 63 6c 6f 73 65  ated, then close
f7400 20 74 68 65 20 6d 75 74 65 78 20 61 6e 64 20 66   the mutex and f
f7410 61 69 6c 20 2a 2f 0a 20 20 69 66 20 28 70 46 69  ail */.  if (pFi
f7420 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 3d 20 4e  le->hShared == N
f7430 55 4c 4c 29 7b 0a 20 20 20 20 77 69 6e 63 65 4d  ULL){.    winceM
f7440 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c  utexRelease(pFil
f7450 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 20 20  e->hMutex);.    
f7460 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c  CloseHandle(pFil
f7470 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 20 20  e->hMutex);.    
f7480 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20  pFile->hMutex = 
f7490 4e 55 4c 4c 3b 0a 20 20 20 20 72 65 74 75 72 6e  NULL;.    return
f74a0 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 20 20 0a 20   FALSE;.  }.  . 
f74b0 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
f74c0 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79  he shared memory
f74d0 20 69 66 20 77 65 27 72 65 20 73 75 70 70 6f 73   if we're suppos
f74e0 65 64 20 74 6f 20 2a 2f 0a 20 20 69 66 20 28 62  ed to */.  if (b
f74f0 49 6e 69 74 29 20 7b 0a 20 20 20 20 5a 65 72 6f  Init) {.    Zero
f7500 4d 65 6d 6f 72 79 28 70 46 69 6c 65 2d 3e 73 68  Memory(pFile->sh
f7510 61 72 65 64 2c 20 73 69 7a 65 6f 66 28 77 69 6e  ared, sizeof(win
f7520 63 65 4c 6f 63 6b 29 29 3b 0a 20 20 7d 0a 0a 20  ceLock));.  }.. 
f7530 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61   winceMutexRelea
f7540 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78  se(pFile->hMutex
f7550 29 3b 0a 20 20 72 65 74 75 72 6e 20 54 52 55 45  );.  return TRUE
f7560 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72  ;.}../*.** Destr
f7570 6f 79 20 74 68 65 20 70 61 72 74 20 6f 66 20 77  oy the part of w
f7580 69 6e 46 69 6c 65 20 74 68 61 74 20 64 65 61 6c  inFile that deal
f7590 73 20 77 69 74 68 20 77 69 6e 63 65 20 6c 6f 63  s with wince loc
f75a0 6b 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ks.*/.static voi
f75b0 64 20 77 69 6e 63 65 44 65 73 74 72 6f 79 4c 6f  d winceDestroyLo
f75c0 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c  ck(winFile *pFil
f75d0 65 29 7b 0a 20 20 69 66 20 28 70 46 69 6c 65 2d  e){.  if (pFile-
f75e0 3e 68 4d 75 74 65 78 29 7b 0a 20 20 20 20 2f 2a  >hMutex){.    /*
f75f0 20 41 63 71 75 69 72 65 20 74 68 65 20 6d 75 74   Acquire the mut
f7600 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e 63 65 4d  ex */.    winceM
f7610 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c  utexAcquire(pFil
f7620 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 20  e->hMutex);..   
f7630 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   /* The followin
f7640 67 20 62 6c 6f 63 6b 73 20 73 68 6f 75 6c 64 20  g blocks should 
f7650 70 72 6f 62 61 62 6c 79 20 61 73 73 65 72 74 20  probably assert 
f7660 69 6e 20 64 65 62 75 67 20 6d 6f 64 65 2c 20 62  in debug mode, b
f7670 75 74 20 74 68 65 79 0a 20 20 20 20 20 20 20 61  ut they.       a
f7680 72 65 20 74 6f 20 63 6c 65 61 6e 75 70 20 69 6e  re to cleanup in
f7690 20 63 61 73 65 20 61 6e 79 20 6c 6f 63 6b 73 20   case any locks 
f76a0 72 65 6d 61 69 6e 65 64 20 6f 70 65 6e 20 2a 2f  remained open */
f76b0 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  .    if (pFile->
f76c0 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 29 7b  local.nReaders){
f76d0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68  .      pFile->sh
f76e0 61 72 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 2d  ared->nReaders -
f76f0 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20  -;.    }.    if 
f7700 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52  (pFile->local.bR
f7710 65 73 65 72 76 65 64 29 7b 0a 20 20 20 20 20 20  eserved){.      
f7720 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62  pFile->shared->b
f7730 52 65 73 65 72 76 65 64 20 3d 20 46 41 4c 53 45  Reserved = FALSE
f7740 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28  ;.    }.    if (
f7750 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65  pFile->local.bPe
f7760 6e 64 69 6e 67 29 7b 0a 20 20 20 20 20 20 70 46  nding){.      pF
f7770 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65  ile->shared->bPe
f7780 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20  nding = FALSE;. 
f7790 20 20 20 7d 0a 20 20 20 20 69 66 20 28 70 46 69     }.    if (pFi
f77a0 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c 75  le->local.bExclu
f77b0 73 69 76 65 29 7b 0a 20 20 20 20 20 20 70 46 69  sive){.      pFi
f77c0 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63  le->shared->bExc
f77d0 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a  lusive = FALSE;.
f77e0 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65      }..    /* De
f77f0 2d 72 65 66 65 72 65 6e 63 65 20 61 6e 64 20 63  -reference and c
f7800 6c 6f 73 65 20 6f 75 72 20 63 6f 70 79 20 6f 66  lose our copy of
f7810 20 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f   the shared memo
f7820 72 79 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 20  ry handle */.   
f7830 20 55 6e 6d 61 70 56 69 65 77 4f 66 46 69 6c 65   UnmapViewOfFile
f7840 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 29 3b  (pFile->shared);
f7850 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65  .    CloseHandle
f7860 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 29  (pFile->hShared)
f7870 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 6e 65 20 77  ;..    /* Done w
f7880 69 74 68 20 74 68 65 20 6d 75 74 65 78 20 2a 2f  ith the mutex */
f7890 0a 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78 52  .    winceMutexR
f78a0 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d  elease(pFile->hM
f78b0 75 74 65 78 29 3b 20 20 20 20 0a 20 20 20 20 43  utex);    .    C
f78c0 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65  loseHandle(pFile
f78d0 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 20 20 70  ->hMutex);.    p
f78e0 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20 4e  File->hMutex = N
f78f0 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a  ULL;.  }.}../* .
f7900 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61  ** An implementa
f7910 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c 6f 63 6b  tion of the Lock
f7920 46 69 6c 65 28 29 20 41 50 49 20 6f 66 20 77 69  File() API of wi
f7930 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 0a  ndows for wince.
f7940 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77  */.static BOOL w
f7950 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 0a 20 20  inceLockFile(.  
f7960 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65 2c 0a  HANDLE *phFile,.
f7970 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66    DWORD dwFileOf
f7980 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57 4f 52 44  fsetLow,.  DWORD
f7990 20 64 77 46 69 6c 65 4f 66 66 73 65 74 48 69 67   dwFileOffsetHig
f79a0 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62  h,.  DWORD nNumb
f79b0 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c  erOfBytesToLockL
f79c0 6f 77 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d  ow,.  DWORD nNum
f79d0 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b  berOfBytesToLock
f79e0 48 69 67 68 0a 29 7b 0a 20 20 77 69 6e 46 69 6c  High.){.  winFil
f79f0 65 20 2a 70 46 69 6c 65 20 3d 20 48 41 4e 44 4c  e *pFile = HANDL
f7a00 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 70 68 46  E_TO_WINFILE(phF
f7a10 69 6c 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 52 65  ile);.  BOOL bRe
f7a20 74 75 72 6e 20 3d 20 46 41 4c 53 45 3b 0a 0a 20  turn = FALSE;.. 
f7a30 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
f7a40 52 28 64 77 46 69 6c 65 4f 66 66 73 65 74 48 69  R(dwFileOffsetHi
f7a50 67 68 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  gh);.  UNUSED_PA
f7a60 52 41 4d 45 54 45 52 28 6e 4e 75 6d 62 65 72 4f  RAMETER(nNumberO
f7a70 66 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68  fBytesToLockHigh
f7a80 29 3b 0a 0a 20 20 69 66 20 28 21 70 46 69 6c 65  );..  if (!pFile
f7a90 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75 72 6e  ->hMutex) return
f7aa0 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65 4d 75   TRUE;.  winceMu
f7ab0 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c 65  texAcquire(pFile
f7ac0 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a  ->hMutex);..  /*
f7ad0 20 57 61 6e 74 69 6e 67 20 61 6e 20 65 78 63 6c   Wanting an excl
f7ae0 75 73 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20  usive lock? */. 
f7af0 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65   if (dwFileOffse
f7b00 74 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44 5f 46  tLow == SHARED_F
f7b10 49 52 53 54 0a 20 20 20 20 20 20 20 26 26 20 6e  IRST.       && n
f7b20 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c  NumberOfBytesToL
f7b30 6f 63 6b 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44  ockLow == SHARED
f7b40 5f 53 49 5a 45 29 7b 0a 20 20 20 20 69 66 20 28  _SIZE){.    if (
f7b50 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e  pFile->shared->n
f7b60 52 65 61 64 65 72 73 20 3d 3d 20 30 20 26 26 20  Readers == 0 && 
f7b70 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62  pFile->shared->b
f7b80 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 30 29 7b  Exclusive == 0){
f7b90 0a 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73  .       pFile->s
f7ba0 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76  hared->bExclusiv
f7bb0 65 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20  e = TRUE;.      
f7bc0 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45   pFile->local.bE
f7bd0 78 63 6c 75 73 69 76 65 20 3d 20 54 52 55 45 3b  xclusive = TRUE;
f7be0 0a 20 20 20 20 20 20 20 62 52 65 74 75 72 6e 20  .       bReturn 
f7bf0 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20  = TRUE;.    }.  
f7c00 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 72  }..  /* Want a r
f7c10 65 61 64 2d 6f 6e 6c 79 20 6c 6f 63 6b 3f 20 2a  ead-only lock? *
f7c20 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46  /.  else if (dwF
f7c30 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20  ileOffsetLow == 
f7c40 53 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a  SHARED_FIRST &&.
f7c50 20 20 20 20 20 20 20 20 20 20 20 6e 4e 75 6d 62             nNumb
f7c60 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c  erOfBytesToLockL
f7c70 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66  ow == 1){.    if
f7c80 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d   (pFile->shared-
f7c90 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 30  >bExclusive == 0
f7ca0 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  ){.      pFile->
f7cb0 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 2b  local.nReaders +
f7cc0 2b 3b 0a 20 20 20 20 20 20 69 66 20 28 70 46 69  +;.      if (pFi
f7cd0 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65  le->local.nReade
f7ce0 72 73 20 3d 3d 20 31 29 7b 0a 20 20 20 20 20 20  rs == 1){.      
f7cf0 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
f7d00 3e 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a 20 20  >nReaders ++;.  
f7d10 20 20 20 20 7d 0a 20 20 20 20 20 20 62 52 65 74      }.      bRet
f7d20 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  urn = TRUE;.    
f7d30 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74  }.  }..  /* Want
f7d40 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 3f   a pending lock?
f7d50 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64   */.  else if (d
f7d60 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d  wFileOffsetLow =
f7d70 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26  = PENDING_BYTE &
f7d80 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73  & nNumberOfBytes
f7d90 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b  ToLockLow == 1){
f7da0 0a 20 20 20 20 2f 2a 20 49 66 20 6e 6f 20 70 65  .    /* If no pe
f7db0 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62  nding lock has b
f7dc0 65 65 6e 20 61 63 71 75 69 72 65 64 2c 20 74 68  een acquired, th
f7dd0 65 6e 20 61 63 71 75 69 72 65 20 69 74 20 2a 2f  en acquire it */
f7de0 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  .    if (pFile->
f7df0 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67  shared->bPending
f7e00 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70   == 0) {.      p
f7e10 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50  File->shared->bP
f7e20 65 6e 64 69 6e 67 20 3d 20 54 52 55 45 3b 0a 20  ending = TRUE;. 
f7e30 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61       pFile->loca
f7e40 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 54 52 55  l.bPending = TRU
f7e50 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e  E;.      bReturn
f7e60 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20   = TRUE;.    }. 
f7e70 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20   }..  /* Want a 
f7e80 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 3f 20 2a  reserved lock? *
f7e90 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46  /.  else if (dwF
f7ea0 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20  ileOffsetLow == 
f7eb0 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 26 26  RESERVED_BYTE &&
f7ec0 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54   nNumberOfBytesT
f7ed0 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a  oLockLow == 1){.
f7ee0 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73      if (pFile->s
f7ef0 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64  hared->bReserved
f7f00 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70   == 0) {.      p
f7f10 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52  File->shared->bR
f7f20 65 73 65 72 76 65 64 20 3d 20 54 52 55 45 3b 0a  eserved = TRUE;.
f7f30 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63        pFile->loc
f7f40 61 6c 2e 62 52 65 73 65 72 76 65 64 20 3d 20 54  al.bReserved = T
f7f50 52 55 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75  RUE;.      bRetu
f7f60 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d  rn = TRUE;.    }
f7f70 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74  .  }..  winceMut
f7f80 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d  exRelease(pFile-
f7f90 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  >hMutex);.  retu
f7fa0 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f  rn bReturn;.}../
f7fb0 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e  *.** An implemen
f7fc0 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 55 6e  tation of the Un
f7fd0 6c 6f 63 6b 46 69 6c 65 20 41 50 49 20 6f 66 20  lockFile API of 
f7fe0 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63  windows for winc
f7ff0 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c  e.*/.static BOOL
f8000 20 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c 65   winceUnlockFile
f8010 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69  (.  HANDLE *phFi
f8020 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69  le,.  DWORD dwFi
f8030 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20 44  leOffsetLow,.  D
f8040 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65  WORD dwFileOffse
f8050 74 48 69 67 68 2c 0a 20 20 44 57 4f 52 44 20 6e  tHigh,.  DWORD n
f8060 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55  NumberOfBytesToU
f8070 6e 6c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52  nlockLow,.  DWOR
f8080 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73  D nNumberOfBytes
f8090 54 6f 55 6e 6c 6f 63 6b 48 69 67 68 0a 29 7b 0a  ToUnlockHigh.){.
f80a0 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65    winFile *pFile
f80b0 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e   = HANDLE_TO_WIN
f80c0 46 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 20  FILE(phFile);.  
f80d0 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 46  BOOL bReturn = F
f80e0 41 4c 53 45 3b 0a 0a 20 20 55 4e 55 53 45 44 5f  ALSE;..  UNUSED_
f80f0 50 41 52 41 4d 45 54 45 52 28 64 77 46 69 6c 65  PARAMETER(dwFile
f8100 4f 66 66 73 65 74 48 69 67 68 29 3b 0a 20 20 55  OffsetHigh);.  U
f8110 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
f8120 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
f8130 55 6e 6c 6f 63 6b 48 69 67 68 29 3b 0a 0a 20 20  UnlockHigh);..  
f8140 69 66 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75 74  if (!pFile->hMut
f8150 65 78 29 20 72 65 74 75 72 6e 20 54 52 55 45 3b  ex) return TRUE;
f8160 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71  .  winceMutexAcq
f8170 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74  uire(pFile->hMut
f8180 65 78 29 3b 0a 0a 20 20 2f 2a 20 52 65 6c 65 61  ex);..  /* Relea
f8190 73 69 6e 67 20 61 20 72 65 61 64 65 72 20 6c 6f  sing a reader lo
f81a0 63 6b 20 6f 72 20 61 6e 20 65 78 63 6c 75 73 69  ck or an exclusi
f81b0 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 20  ve lock */.  if 
f81c0 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77  (dwFileOffsetLow
f81d0 20 3d 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54   == SHARED_FIRST
f81e0 29 7b 0a 20 20 20 20 2f 2a 20 44 69 64 20 77 65  ){.    /* Did we
f81f0 20 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69   have an exclusi
f8200 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20 20  ve lock? */.    
f8210 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c  if (pFile->local
f8220 2e 62 45 78 63 6c 75 73 69 76 65 29 7b 0a 20 20  .bExclusive){.  
f8230 20 20 20 20 61 73 73 65 72 74 28 6e 4e 75 6d 62      assert(nNumb
f8240 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63  erOfBytesToUnloc
f8250 6b 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44 5f 53  kLow == SHARED_S
f8260 49 5a 45 29 3b 0a 20 20 20 20 20 20 70 46 69 6c  IZE);.      pFil
f8270 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 73  e->local.bExclus
f8280 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20  ive = FALSE;.   
f8290 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64     pFile->shared
f82a0 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 46  ->bExclusive = F
f82b0 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65 74  ALSE;.      bRet
f82c0 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  urn = TRUE;.    
f82d0 7d 0a 0a 20 20 20 20 2f 2a 20 44 69 64 20 77 65  }..    /* Did we
f82e0 20 6a 75 73 74 20 68 61 76 65 20 61 20 72 65 61   just have a rea
f82f0 64 65 72 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20  der lock? */.   
f8300 20 65 6c 73 65 20 69 66 20 28 70 46 69 6c 65 2d   else if (pFile-
f8310 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 29  >local.nReaders)
f8320 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 6e  {.      assert(n
f8330 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55  NumberOfBytesToU
f8340 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 3b 0a  nlockLow == 1);.
f8350 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63        pFile->loc
f8360 61 6c 2e 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a  al.nReaders --;.
f8370 20 20 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d        if (pFile-
f8380 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20  >local.nReaders 
f8390 3d 3d 20 30 29 0a 20 20 20 20 20 20 7b 0a 20 20  == 0).      {.  
f83a0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61        pFile->sha
f83b0 72 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 2d 2d  red->nReaders --
f83c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
f83d0 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a  bReturn = TRUE;.
f83e0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
f83f0 52 65 6c 65 61 73 69 6e 67 20 61 20 70 65 6e 64  Releasing a pend
f8400 69 6e 67 20 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c  ing lock */.  el
f8410 73 65 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66  se if (dwFileOff
f8420 73 65 74 4c 6f 77 20 3d 3d 20 50 45 4e 44 49 4e  setLow == PENDIN
f8430 47 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65  G_BYTE && nNumbe
f8440 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b  rOfBytesToUnlock
f8450 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69  Low == 1){.    i
f8460 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e  f (pFile->local.
f8470 62 50 65 6e 64 69 6e 67 29 7b 0a 20 20 20 20 20  bPending){.     
f8480 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50   pFile->local.bP
f8490 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a  ending = FALSE;.
f84a0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61        pFile->sha
f84b0 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 20  red->bPending = 
f84c0 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65  FALSE;.      bRe
f84d0 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20  turn = TRUE;.   
f84e0 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 52 65 6c 65   }.  }.  /* Rele
f84f0 61 73 69 6e 67 20 61 20 72 65 73 65 72 76 65 64  asing a reserved
f8500 20 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c 73 65 20   lock */.  else 
f8510 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74  if (dwFileOffset
f8520 4c 6f 77 20 3d 3d 20 52 45 53 45 52 56 45 44 5f  Low == RESERVED_
f8530 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f  BYTE && nNumberO
f8540 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f  fBytesToUnlockLo
f8550 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 20  w == 1){.    if 
f8560 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52  (pFile->local.bR
f8570 65 73 65 72 76 65 64 29 20 7b 0a 20 20 20 20 20  eserved) {.     
f8580 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52   pFile->local.bR
f8590 65 73 65 72 76 65 64 20 3d 20 46 41 4c 53 45 3b  eserved = FALSE;
f85a0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68  .      pFile->sh
f85b0 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20  ared->bReserved 
f85c0 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 62  = FALSE;.      b
f85d0 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20  Return = TRUE;. 
f85e0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 77 69 6e 63     }.  }..  winc
f85f0 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 46  eMutexRelease(pF
f8600 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20  ile->hMutex);.  
f8610 72 65 74 75 72 6e 20 62 52 65 74 75 72 6e 3b 0a  return bReturn;.
f8620 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c  }../*.** An impl
f8630 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
f8640 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41  e LockFileEx() A
f8650 50 49 20 6f 66 20 77 69 6e 64 6f 77 73 20 66 6f  PI of windows fo
f8660 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 69  r wince.*/.stati
f8670 63 20 42 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b  c BOOL winceLock
f8680 46 69 6c 65 45 78 28 0a 20 20 48 41 4e 44 4c 45  FileEx(.  HANDLE
f8690 20 2a 70 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52   *phFile,.  DWOR
f86a0 44 20 64 77 46 6c 61 67 73 2c 0a 20 20 44 57 4f  D dwFlags,.  DWO
f86b0 52 44 20 64 77 52 65 73 65 72 76 65 64 2c 0a 20  RD dwReserved,. 
f86c0 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66   DWORD nNumberOf
f86d0 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a  BytesToLockLow,.
f86e0 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f    DWORD nNumberO
f86f0 66 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68  fBytesToLockHigh
f8700 2c 0a 20 20 4c 50 4f 56 45 52 4c 41 50 50 45 44  ,.  LPOVERLAPPED
f8710 20 6c 70 4f 76 65 72 6c 61 70 70 65 64 0a 29 7b   lpOverlapped.){
f8720 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
f8730 54 45 52 28 64 77 52 65 73 65 72 76 65 64 29 3b  TER(dwReserved);
f8740 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
f8750 54 45 52 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74  TER(nNumberOfByt
f8760 65 73 54 6f 4c 6f 63 6b 48 69 67 68 29 3b 0a 0a  esToLockHigh);..
f8770 20 20 2f 2a 20 49 66 20 74 68 65 20 63 61 6c 6c    /* If the call
f8780 65 72 20 77 61 6e 74 73 20 61 20 73 68 61 72 65  er wants a share
f8790 64 20 72 65 61 64 20 6c 6f 63 6b 2c 20 66 6f 72  d read lock, for
f87a0 77 61 72 64 20 74 68 69 73 20 63 61 6c 6c 0a 20  ward this call. 
f87b0 20 2a 2a 20 74 6f 20 77 69 6e 63 65 4c 6f 63 6b   ** to winceLock
f87c0 46 69 6c 65 20 2a 2f 0a 20 20 69 66 20 28 6c 70  File */.  if (lp
f87d0 4f 76 65 72 6c 61 70 70 65 64 2d 3e 4f 66 66 73  Overlapped->Offs
f87e0 65 74 20 3d 3d 20 53 48 41 52 45 44 5f 46 49 52  et == SHARED_FIR
f87f0 53 54 20 26 26 0a 20 20 20 20 20 20 64 77 46 6c  ST &&.      dwFl
f8800 61 67 73 20 3d 3d 20 31 20 26 26 0a 20 20 20 20  ags == 1 &&.    
f8810 20 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73    nNumberOfBytes
f8820 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 53 48 41  ToLockLow == SHA
f8830 52 45 44 5f 53 49 5a 45 29 7b 0a 20 20 20 20 72  RED_SIZE){.    r
f8840 65 74 75 72 6e 20 77 69 6e 63 65 4c 6f 63 6b 46  eturn winceLockF
f8850 69 6c 65 28 70 68 46 69 6c 65 2c 20 53 48 41 52  ile(phFile, SHAR
f8860 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 31 2c 20  ED_FIRST, 0, 1, 
f8870 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  0);.  }.  return
f8880 20 46 41 4c 53 45 3b 0a 7d 0a 2f 2a 0a 2a 2a 20   FALSE;.}./*.** 
f8890 45 6e 64 20 6f 66 20 74 68 65 20 73 70 65 63 69  End of the speci
f88a0 61 6c 20 63 6f 64 65 20 66 6f 72 20 77 69 6e 63  al code for winc
f88b0 65 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.**************
f88c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f88d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f88e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f88f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
f8900 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
f8910 45 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f  E_OS_WINCE */../
f8920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
f8970 20 54 68 65 20 6e 65 78 74 20 67 72 6f 75 70 20   The next group 
f8980 6f 66 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c  of routines impl
f8990 65 6d 65 6e 74 20 74 68 65 20 49 2f 4f 20 6d 65  ement the I/O me
f89a0 74 68 6f 64 73 20 73 70 65 63 69 66 69 65 64 0a  thods specified.
f89b0 2a 2a 20 62 79 20 74 68 65 20 73 71 6c 69 74 65  ** by the sqlite
f89c0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a  3_io_methods obj
f89d0 65 63 74 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ect..***********
f89e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f89f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8a20 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73  ***/../*.** Clos
f8a30 65 20 61 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  e a file..**.** 
f8a40 49 74 20 69 73 20 72 65 70 6f 72 74 65 64 20 74  It is reported t
f8a50 68 61 74 20 61 6e 20 61 74 74 65 6d 70 74 20 74  hat an attempt t
f8a60 6f 20 63 6c 6f 73 65 20 61 20 68 61 6e 64 6c 65  o close a handle
f8a70 20 6d 69 67 68 74 20 73 6f 6d 65 74 69 6d 65 73   might sometimes
f8a80 0a 2a 2a 20 66 61 69 6c 2e 20 20 54 68 69 73 20  .** fail.  This 
f8a90 69 73 20 61 20 76 65 72 79 20 75 6e 72 65 61 73  is a very unreas
f8aa0 6f 6e 61 62 6c 65 20 72 65 73 75 6c 74 2c 20 62  onable result, b
f8ab0 75 74 20 77 69 6e 64 6f 77 73 20 69 73 20 6e 6f  ut windows is no
f8ac0 74 6f 72 69 6f 75 73 0a 2a 2a 20 66 6f 72 20 62  torious.** for b
f8ad0 65 69 6e 67 20 75 6e 72 65 61 73 6f 6e 61 62 6c  eing unreasonabl
f8ae0 65 20 73 6f 20 49 20 64 6f 20 6e 6f 74 20 64 6f  e so I do not do
f8af0 75 62 74 20 74 68 61 74 20 69 74 20 6d 69 67 68  ubt that it migh
f8b00 74 20 68 61 70 70 65 6e 2e 20 20 49 66 0a 2a 2a  t happen.  If.**
f8b10 20 74 68 65 20 63 6c 6f 73 65 20 66 61 69 6c 73   the close fails
f8b20 2c 20 77 65 20 70 61 75 73 65 20 66 6f 72 20 31  , we pause for 1
f8b30 30 30 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20  00 milliseconds 
f8b40 61 6e 64 20 74 72 79 20 61 67 61 69 6e 2e 20 20  and try again.  
f8b50 41 73 0a 2a 2a 20 6d 61 6e 79 20 61 73 20 4d 58  As.** many as MX
f8b60 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20 61  _CLOSE_ATTEMPT a
f8b70 74 74 65 6d 70 74 73 20 74 6f 20 63 6c 6f 73 65  ttempts to close
f8b80 20 74 68 65 20 68 61 6e 64 6c 65 20 61 72 65 20   the handle are 
f8b90 6d 61 64 65 20 62 65 66 6f 72 65 0a 2a 2a 20 67  made before.** g
f8ba0 69 76 69 6e 67 20 75 70 20 61 6e 64 20 72 65 74  iving up and ret
f8bb0 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 2e  urning an error.
f8bc0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43  .*/.#define MX_C
f8bd0 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20 33 0a 73  LOSE_ATTEMPT 3.s
f8be0 74 61 74 69 63 20 69 6e 74 20 77 69 6e 43 6c 6f  tatic int winClo
f8bf0 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  se(sqlite3_file 
f8c00 2a 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 2c 20  *id){.  int rc, 
f8c10 63 6e 74 20 3d 20 30 3b 0a 20 20 77 69 6e 46 69  cnt = 0;.  winFi
f8c20 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e  le *pFile = (win
f8c30 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73  File*)id;..  ass
f8c40 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20  ert( id!=0 );.  
f8c50 4f 53 54 52 41 43 45 32 28 22 43 4c 4f 53 45 20  OSTRACE2("CLOSE 
f8c60 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29  %d\n", pFile->h)
f8c70 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 72 63 20 3d  ;.  do{.    rc =
f8c80 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69   CloseHandle(pFi
f8c90 6c 65 2d 3e 68 29 3b 0a 20 20 7d 77 68 69 6c 65  le->h);.  }while
f8ca0 28 20 72 63 3d 3d 30 20 26 26 20 2b 2b 63 6e 74  ( rc==0 && ++cnt
f8cb0 20 3c 20 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 45   < MX_CLOSE_ATTE
f8cc0 4d 50 54 20 26 26 20 28 53 6c 65 65 70 28 31 30  MPT && (Sleep(10
f8cd0 30 29 2c 20 31 29 20 29 3b 0a 23 69 66 20 53 51  0), 1) );.#if SQ
f8ce0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 23 64  LITE_OS_WINCE.#d
f8cf0 65 66 69 6e 65 20 57 49 4e 43 45 5f 44 45 4c 45  efine WINCE_DELE
f8d00 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 20 33 0a  TION_ATTEMPTS 3.
f8d10 20 20 77 69 6e 63 65 44 65 73 74 72 6f 79 4c 6f    winceDestroyLo
f8d20 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28  ck(pFile);.  if(
f8d30 20 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f   pFile->zDeleteO
f8d40 6e 43 6c 6f 73 65 20 29 7b 0a 20 20 20 20 69 6e  nClose ){.    in
f8d50 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 77  t cnt = 0;.    w
f8d60 68 69 6c 65 28 0a 20 20 20 20 20 20 20 20 20 20  hile(.          
f8d70 20 44 65 6c 65 74 65 46 69 6c 65 57 28 70 46 69   DeleteFileW(pFi
f8d80 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f  le->zDeleteOnClo
f8d90 73 65 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 26  se)==0.        &
f8da0 26 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 75  & GetFileAttribu
f8db0 74 65 73 57 28 70 46 69 6c 65 2d 3e 7a 44 65 6c  tesW(pFile->zDel
f8dc0 65 74 65 4f 6e 43 6c 6f 73 65 29 21 3d 30 78 66  eteOnClose)!=0xf
f8dd0 66 66 66 66 66 66 66 20 0a 20 20 20 20 20 20 20  fffffff .       
f8de0 20 26 26 20 63 6e 74 2b 2b 20 3c 20 57 49 4e 43   && cnt++ < WINC
f8df0 45 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d  E_DELETION_ATTEM
f8e00 50 54 53 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  PTS.    ){.     
f8e10 20 20 53 6c 65 65 70 28 31 30 30 29 3b 20 20 2f    Sleep(100);  /
f8e20 2a 20 57 61 69 74 20 61 20 6c 69 74 74 6c 65 20  * Wait a little 
f8e30 62 65 66 6f 72 65 20 74 72 79 69 6e 67 20 61 67  before trying ag
f8e40 61 69 6e 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20  ain */.    }.   
f8e50 20 66 72 65 65 28 70 46 69 6c 65 2d 3e 7a 44 65   free(pFile->zDe
f8e60 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 3b 0a 20 20  leteOnClose);.  
f8e70 7d 0a 23 65 6e 64 69 66 0a 20 20 4f 70 65 6e 43  }.#endif.  OpenC
f8e80 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 72 65  ounter(-1);.  re
f8e90 74 75 72 6e 20 72 63 20 3f 20 53 51 4c 49 54 45  turn rc ? SQLITE
f8ea0 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45  _OK : SQLITE_IOE
f8eb0 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d  RR;.}../*.** Som
f8ec0 65 20 6d 69 63 72 6f 73 6f 66 74 20 63 6f 6d 70  e microsoft comp
f8ed0 69 6c 65 72 73 20 6c 61 63 6b 20 74 68 69 73 20  ilers lack this 
f8ee0 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 23  definition..*/.#
f8ef0 69 66 6e 64 65 66 20 49 4e 56 41 4c 49 44 5f 53  ifndef INVALID_S
f8f00 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 0a  ET_FILE_POINTER.
f8f10 23 20 64 65 66 69 6e 65 20 49 4e 56 41 4c 49 44  # define INVALID
f8f20 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45  _SET_FILE_POINTE
f8f30 52 20 28 28 44 57 4f 52 44 29 2d 31 29 0a 23 65  R ((DWORD)-1).#e
f8f40 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  ndif../*.** Read
f8f50 20 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c   data from a fil
f8f60 65 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e  e into a buffer.
f8f70 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    Return SQLITE_
f8f80 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74  OK if all.** byt
f8f90 65 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63  es were read suc
f8fa0 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51  cessfully and SQ
f8fb0 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e  LITE_IOERR if an
f8fc0 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77  ything goes.** w
f8fd0 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rong..*/.static 
f8fe0 69 6e 74 20 77 69 6e 52 65 61 64 28 0a 20 20 73  int winRead(.  s
f8ff0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
f9000 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c            /* Fil
f9010 65 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a  e to read from *
f9020 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c 20  /.  void *pBuf, 
f9030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
f9040 2a 20 57 72 69 74 65 20 63 6f 6e 74 65 6e 74 20  * Write content 
f9050 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66 65 72  into this buffer
f9060 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20   */.  int amt,  
f9070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f9080 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
f9090 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20  tes to read */. 
f90a0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f   sqlite3_int64 o
f90b0 66 66 73 65 74 20 20 20 20 20 20 20 2f 2a 20 42  ffset       /* B
f90c0 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 74 20  egin reading at 
f90d0 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29  this offset */.)
f90e0 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 72 42 69  {.  LONG upperBi
f90f0 74 73 20 3d 20 28 4c 4f 4e 47 29 28 28 6f 66 66  ts = (LONG)((off
f9100 73 65 74 3e 3e 33 32 29 20 26 20 30 78 37 66 66  set>>32) & 0x7ff
f9110 66 66 66 66 66 29 3b 0a 20 20 4c 4f 4e 47 20 6c  fffff);.  LONG l
f9120 6f 77 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47  owerBits = (LONG
f9130 29 28 6f 66 66 73 65 74 20 26 20 30 78 66 66 66  )(offset & 0xfff
f9140 66 66 66 66 66 29 3b 0a 20 20 44 57 4f 52 44 20  fffff);.  DWORD 
f9150 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70  rc;.  winFile *p
f9160 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a  File = (winFile*
f9170 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 65 72 72  )id;.  DWORD err
f9180 6f 72 3b 0a 20 20 44 57 4f 52 44 20 67 6f 74 3b  or;.  DWORD got;
f9190 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d  ..  assert( id!=
f91a0 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49  0 );.  SimulateI
f91b0 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51  OError(return SQ
f91c0 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 29  LITE_IOERR_READ)
f91d0 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 52 45  ;.  OSTRACE3("RE
f91e0 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22  AD %d lock=%d\n"
f91f0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c  , pFile->h, pFil
f9200 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20  e->locktype);.  
f9210 72 63 20 3d 20 53 65 74 46 69 6c 65 50 6f 69 6e  rc = SetFilePoin
f9220 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f  ter(pFile->h, lo
f9230 77 65 72 42 69 74 73 2c 20 26 75 70 70 65 72 42  werBits, &upperB
f9240 69 74 73 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29  its, FILE_BEGIN)
f9250 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49 4e 56 41  ;.  if( rc==INVA
f9260 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49  LID_SET_FILE_POI
f9270 4e 54 45 52 20 26 26 20 28 65 72 72 6f 72 3d 47  NTER && (error=G
f9280 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 21 3d  etLastError())!=
f9290 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
f92a0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
f92b0 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 65   = error;.    re
f92c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c  turn SQLITE_FULL
f92d0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 52 65 61  ;.  }.  if( !Rea
f92e0 64 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20  dFile(pFile->h, 
f92f0 70 42 75 66 2c 20 61 6d 74 2c 20 26 67 6f 74 2c  pBuf, amt, &got,
f9300 20 30 29 20 29 7b 0a 20 20 20 20 70 46 69 6c 65   0) ){.    pFile
f9310 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65  ->lastErrno = Ge
f9320 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20  tLastError();.  
f9330 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
f9340 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d 0a  IOERR_READ;.  }.
f9350 20 20 69 66 28 20 67 6f 74 3d 3d 28 44 57 4f 52    if( got==(DWOR
f9360 44 29 61 6d 74 20 29 7b 0a 20 20 20 20 72 65 74  D)amt ){.    ret
f9370 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
f9380 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 55   }else{.    /* U
f9390 6e 72 65 61 64 20 70 61 72 74 73 20 6f 66 20 74  nread parts of t
f93a0 68 65 20 62 75 66 66 65 72 20 6d 75 73 74 20 62  he buffer must b
f93b0 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f  e zero-filled */
f93c0 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63  .    memset(&((c
f93d0 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c  har*)pBuf)[got],
f93e0 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20   0, amt-got);.  
f93f0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
f9400 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44  IOERR_SHORT_READ
f9410 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  ;.  }.}../*.** W
f9420 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d 20 61  rite data from a
f9430 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61 20 66   buffer into a f
f9440 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c  ile.  Return SQL
f9450 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73  ITE_OK on succes
f9460 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68  s.** or some oth
f9470 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e  er error code on
f9480 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61   failure..*/.sta
f9490 74 69 63 20 69 6e 74 20 77 69 6e 57 72 69 74 65  tic int winWrite
f94a0 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  (.  sqlite3_file
f94b0 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 2f 2a   *id,         /*
f94c0 20 46 69 6c 65 20 74 6f 20 77 72 69 74 65 20 69   File to write i
f94d0 6e 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  nto */.  const v
f94e0 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20  oid *pBuf,      
f94f0 20 20 20 2f 2a 20 54 68 65 20 62 79 74 65 73 20     /* The bytes 
f9500 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 2a 2f  to be written */
f9510 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20  .  int amt,     
f9520 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f9530 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
f9540 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71  to write */.  sq
f9550 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73  lite3_int64 offs
f9560 65 74 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65  et      /* Offse
f9570 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 20  t into the file 
f9580 74 6f 20 62 65 67 69 6e 20 77 72 69 74 69 6e 67  to begin writing
f9590 20 61 74 20 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e 47   at */.){.  LONG
f95a0 20 75 70 70 65 72 42 69 74 73 20 3d 20 28 4c 4f   upperBits = (LO
f95b0 4e 47 29 28 28 6f 66 66 73 65 74 3e 3e 33 32 29  NG)((offset>>32)
f95c0 20 26 20 30 78 37 66 66 66 66 66 66 66 29 3b 0a   & 0x7fffffff);.
f95d0 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73    LONG lowerBits
f95e0 20 3d 20 28 4c 4f 4e 47 29 28 6f 66 66 73 65 74   = (LONG)(offset
f95f0 20 26 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a   & 0xffffffff);.
f9600 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 77 69    DWORD rc;.  wi
f9610 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  nFile *pFile = (
f9620 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44  winFile*)id;.  D
f9630 57 4f 52 44 20 65 72 72 6f 72 3b 0a 20 20 44 57  WORD error;.  DW
f9640 4f 52 44 20 77 72 6f 74 65 20 3d 20 30 3b 0a 0a  ORD wrote = 0;..
f9650 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20    assert( id!=0 
f9660 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
f9670 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49  rror(return SQLI
f9680 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 29 3b  TE_IOERR_WRITE);
f9690 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66  .  SimulateDiskf
f96a0 75 6c 6c 45 72 72 6f 72 28 72 65 74 75 72 6e 20  ullError(return 
f96b0 53 51 4c 49 54 45 5f 46 55 4c 4c 29 3b 0a 20 20  SQLITE_FULL);.  
f96c0 4f 53 54 52 41 43 45 33 28 22 57 52 49 54 45 20  OSTRACE3("WRITE 
f96d0 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70  %d lock=%d\n", p
f96e0 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e  File->h, pFile->
f96f0 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 72 63 20  locktype);.  rc 
f9700 3d 20 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72  = SetFilePointer
f9710 28 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72  (pFile->h, lower
f9720 42 69 74 73 2c 20 26 75 70 70 65 72 42 69 74 73  Bits, &upperBits
f9730 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20  , FILE_BEGIN);. 
f9740 20 69 66 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44   if( rc==INVALID
f9750 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45  _SET_FILE_POINTE
f9760 52 20 26 26 20 28 65 72 72 6f 72 3d 47 65 74 4c  R && (error=GetL
f9770 61 73 74 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f  astError())!=NO_
f9780 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 70 46 69  ERROR ){.    pFi
f9790 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
f97a0 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 72  error;.    retur
f97b0 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20  n SQLITE_FULL;. 
f97c0 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61 6d 74   }.  assert( amt
f97d0 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 0a 20  >0 );.  while(. 
f97e0 20 20 20 20 61 6d 74 3e 30 0a 20 20 20 20 20 26      amt>0.     &
f97f0 26 20 28 72 63 20 3d 20 57 72 69 74 65 46 69 6c  & (rc = WriteFil
f9800 65 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66  e(pFile->h, pBuf
f9810 2c 20 61 6d 74 2c 20 26 77 72 6f 74 65 2c 20 30  , amt, &wrote, 0
f9820 29 29 21 3d 30 0a 20 20 20 20 20 26 26 20 77 72  ))!=0.     && wr
f9830 6f 74 65 3e 30 0a 20 20 29 7b 0a 20 20 20 20 61  ote>0.  ){.    a
f9840 6d 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20  mt -= wrote;.   
f9850 20 70 42 75 66 20 3d 20 26 28 28 63 68 61 72 2a   pBuf = &((char*
f9860 29 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20  )pBuf)[wrote];. 
f9870 20 7d 0a 20 20 69 66 28 20 21 72 63 20 7c 7c 20   }.  if( !rc || 
f9880 61 6d 74 3e 28 69 6e 74 29 77 72 6f 74 65 20 29  amt>(int)wrote )
f9890 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73  {.    pFile->las
f98a0 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74  tErrno = GetLast
f98b0 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 65 74  Error();.    ret
f98c0 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b  urn SQLITE_FULL;
f98d0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
f98e0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
f98f0 2a 20 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70  * Truncate an op
f9900 65 6e 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65  en file to a spe
f9910 63 69 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73  cified size.*/.s
f9920 74 61 74 69 63 20 69 6e 74 20 77 69 6e 54 72 75  tatic int winTru
f9930 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69  ncate(sqlite3_fi
f9940 6c 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f  le *id, sqlite3_
f9950 69 6e 74 36 34 20 6e 42 79 74 65 29 7b 0a 20 20  int64 nByte){.  
f9960 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20 3d  LONG upperBits =
f9970 20 28 4c 4f 4e 47 29 28 28 6e 42 79 74 65 3e 3e   (LONG)((nByte>>
f9980 33 32 29 20 26 20 30 78 37 66 66 66 66 66 66 66  32) & 0x7fffffff
f9990 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42  );.  LONG lowerB
f99a0 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 6e 42 79  its = (LONG)(nBy
f99b0 74 65 20 26 20 30 78 66 66 66 66 66 66 66 66 29  te & 0xffffffff)
f99c0 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20  ;.  DWORD rc;.  
f99d0 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  winFile *pFile =
f99e0 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20   (winFile*)id;. 
f99f0 20 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a 0a 20   DWORD error;.. 
f9a00 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29   assert( id!=0 )
f9a10 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 54 52  ;.  OSTRACE3("TR
f9a20 55 4e 43 41 54 45 20 25 64 20 25 6c 6c 64 5c 6e  UNCATE %d %lld\n
f9a30 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79  ", pFile->h, nBy
f9a40 74 65 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49  te);.  SimulateI
f9a50 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51  OError(return SQ
f9a60 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43  LITE_IOERR_TRUNC
f9a70 41 54 45 29 3b 0a 20 20 72 63 20 3d 20 53 65 74  ATE);.  rc = Set
f9a80 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 69 6c  FilePointer(pFil
f9a90 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c  e->h, lowerBits,
f9aa0 20 26 75 70 70 65 72 42 69 74 73 2c 20 46 49 4c   &upperBits, FIL
f9ab0 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66 28 20  E_BEGIN);.  if( 
f9ac0 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f  rc==INVALID_SET_
f9ad0 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26 20  FILE_POINTER && 
f9ae0 28 65 72 72 6f 72 3d 47 65 74 4c 61 73 74 45 72  (error=GetLastEr
f9af0 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 52 4f 52  ror())!=NO_ERROR
f9b00 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c   ){.    pFile->l
f9b10 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72  astErrno = error
f9b20 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
f9b30 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41  ITE_IOERR_TRUNCA
f9b40 54 45 3b 0a 20 20 7d 0a 20 20 2f 2a 20 53 65 74  TE;.  }.  /* Set
f9b50 45 6e 64 4f 66 46 69 6c 65 20 77 69 6c 6c 20 66  EndOfFile will f
f9b60 61 69 6c 20 69 66 20 6e 42 79 74 65 20 69 73 20  ail if nByte is 
f9b70 6e 65 67 61 74 69 76 65 20 2a 2f 0a 20 20 69 66  negative */.  if
f9b80 28 20 21 53 65 74 45 6e 64 4f 66 46 69 6c 65 28  ( !SetEndOfFile(
f9b90 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 20  pFile->h) ){.   
f9ba0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
f9bb0 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72  o = GetLastError
f9bc0 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  ();.    return S
f9bd0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e  QLITE_IOERR_TRUN
f9be0 43 41 54 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75  CATE;.  }.  retu
f9bf0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
f9c00 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
f9c10 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20  EST./*.** Count 
f9c20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75  the number of fu
f9c30 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d  llsyncs and norm
f9c40 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20  al syncs.  This 
f9c50 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a  is used to test.
f9c60 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e  ** that syncs an
f9c70 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20  d fullsyncs are 
f9c80 6f 63 63 75 72 69 6e 67 20 61 74 20 74 68 65 20  occuring at the 
f9c90 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a  right times..*/.
f9ca0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
f9cb0 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e  qlite3_sync_coun
f9cc0 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50  t = 0;.SQLITE_AP
f9cd0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 75  I int sqlite3_fu
f9ce0 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30  llsync_count = 0
f9cf0 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
f9d00 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 77 72  Make sure all wr
f9d10 69 74 65 73 20 74 6f 20 61 20 70 61 72 74 69 63  ites to a partic
f9d20 75 6c 61 72 20 66 69 6c 65 20 61 72 65 20 63 6f  ular file are co
f9d30 6d 6d 69 74 74 65 64 20 74 6f 20 64 69 73 6b 2e  mmitted to disk.
f9d40 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
f9d50 69 6e 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66  inSync(sqlite3_f
f9d60 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61  ile *id, int fla
f9d70 67 73 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  gs){.#ifndef SQL
f9d80 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 77 69  ITE_NO_SYNC.  wi
f9d90 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  nFile *pFile = (
f9da0 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20  winFile*)id;..  
f9db0 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b  assert( id!=0 );
f9dc0 0a 20 20 4f 53 54 52 41 43 45 33 28 22 53 59 4e  .  OSTRACE3("SYN
f9dd0 43 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c  C %d lock=%d\n",
f9de0 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65   pFile->h, pFile
f9df0 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a 23 65 6c  ->locktype);.#el
f9e00 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  se.  UNUSED_PARA
f9e10 4d 45 54 45 52 28 69 64 29 3b 0a 23 65 6e 64 69  METER(id);.#endi
f9e20 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  f.#ifndef SQLITE
f9e30 5f 54 45 53 54 0a 20 20 55 4e 55 53 45 44 5f 50  _TEST.  UNUSED_P
f9e40 41 52 41 4d 45 54 45 52 28 66 6c 61 67 73 29 3b  ARAMETER(flags);
f9e50 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 66 6c 61  .#else.  if( fla
f9e60 67 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e 43  gs & SQLITE_SYNC
f9e70 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 73 71 6c  _FULL ){.    sql
f9e80 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f  ite3_fullsync_co
f9e90 75 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c  unt++;.  }.  sql
f9ea0 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b  ite3_sync_count+
f9eb0 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49  +;.#endif.  /* I
f9ec0 66 20 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69  f we compiled wi
f9ed0 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f  th the SQLITE_NO
f9ee0 5f 53 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e  _SYNC flag, then
f9ef0 20 73 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20   syncing is a.  
f9f00 2a 2a 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69  ** no-op.  */.#i
f9f10 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53  fdef SQLITE_NO_S
f9f20 59 4e 43 0a 20 20 20 20 72 65 74 75 72 6e 20 53  YNC.    return S
f9f30 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6c 73 65 0a  QLITE_OK;.#else.
f9f40 20 20 69 66 28 20 46 6c 75 73 68 46 69 6c 65 42    if( FlushFileB
f9f50 75 66 66 65 72 73 28 70 46 69 6c 65 2d 3e 68 29  uffers(pFile->h)
f9f60 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
f9f70 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73  QLITE_OK;.  }els
f9f80 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  e{.    pFile->la
f9f90 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73  stErrno = GetLas
f9fa0 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 65  tError();.    re
f9fb0 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
f9fc0 52 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a  R;.  }.#endif.}.
f9fd0 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65  ./*.** Determine
f9fe0 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a   the current siz
f9ff0 65 20 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 62  e of a file in b
fa000 79 74 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ytes.*/.static i
fa010 6e 74 20 77 69 6e 46 69 6c 65 53 69 7a 65 28 73  nt winFileSize(s
fa020 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
fa030 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a   sqlite3_int64 *
fa040 70 53 69 7a 65 29 7b 0a 20 20 44 57 4f 52 44 20  pSize){.  DWORD 
fa050 75 70 70 65 72 42 69 74 73 3b 0a 20 20 44 57 4f  upperBits;.  DWO
fa060 52 44 20 6c 6f 77 65 72 42 69 74 73 3b 0a 20 20  RD lowerBits;.  
fa070 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  winFile *pFile =
fa080 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20   (winFile*)id;. 
fa090 20 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a 0a 20   DWORD error;.. 
fa0a0 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29   assert( id!=0 )
fa0b0 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
fa0c0 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54  ror(return SQLIT
fa0d0 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 29 3b 0a  E_IOERR_FSTAT);.
fa0e0 20 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 47 65    lowerBits = Ge
fa0f0 74 46 69 6c 65 53 69 7a 65 28 70 46 69 6c 65 2d  tFileSize(pFile-
fa100 3e 68 2c 20 26 75 70 70 65 72 42 69 74 73 29 3b  >h, &upperBits);
fa110 0a 20 20 69 66 28 20 20 20 28 6c 6f 77 65 72 42  .  if(   (lowerB
fa120 69 74 73 20 3d 3d 20 49 4e 56 41 4c 49 44 5f 46  its == INVALID_F
fa130 49 4c 45 5f 53 49 5a 45 29 0a 20 20 20 20 20 26  ILE_SIZE).     &
fa140 26 20 28 28 65 72 72 6f 72 20 3d 20 47 65 74 4c  & ((error = GetL
fa150 61 73 74 45 72 72 6f 72 28 29 29 20 21 3d 20 4e  astError()) != N
fa160 4f 5f 45 52 52 4f 52 29 20 29 0a 20 20 7b 0a 20  O_ERROR) ).  {. 
fa170 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
fa180 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20  rno = error;.   
fa190 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
fa1a0 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a  OERR_FSTAT;.  }.
fa1b0 20 20 2a 70 53 69 7a 65 20 3d 20 28 28 28 73 71    *pSize = (((sq
fa1c0 6c 69 74 65 33 5f 69 6e 74 36 34 29 75 70 70 65  lite3_int64)uppe
fa1d0 72 42 69 74 73 29 3c 3c 33 32 29 20 2b 20 6c 6f  rBits)<<32) + lo
fa1e0 77 65 72 42 69 74 73 3b 0a 20 20 72 65 74 75 72  werBits;.  retur
fa1f0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
fa200 2f 2a 0a 2a 2a 20 4c 4f 43 4b 46 49 4c 45 5f 46  /*.** LOCKFILE_F
fa210 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 20  AIL_IMMEDIATELY 
fa220 69 73 20 75 6e 64 65 66 69 6e 65 64 20 6f 6e 20  is undefined on 
fa230 73 6f 6d 65 20 57 69 6e 64 6f 77 73 20 73 79 73  some Windows sys
fa240 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  tems..*/.#ifndef
fa250 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49   LOCKFILE_FAIL_I
fa260 4d 4d 45 44 49 41 54 45 4c 59 0a 23 20 64 65 66  MMEDIATELY.# def
fa270 69 6e 65 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49  ine LOCKFILE_FAI
fa280 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 20 31 0a  L_IMMEDIATELY 1.
fa290 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 63  #endif../*.** Ac
fa2a0 71 75 69 72 65 20 61 20 72 65 61 64 65 72 20 6c  quire a reader l
fa2b0 6f 63 6b 2e 0a 2a 2a 20 44 69 66 66 65 72 65 6e  ock..** Differen
fa2c0 74 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20 61  t API routines a
fa2d0 72 65 20 63 61 6c 6c 65 64 20 64 65 70 65 6e 64  re called depend
fa2e0 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 20 6f  ing on whether o
fa2f0 72 20 6e 6f 74 20 74 68 69 73 0a 2a 2a 20 69 73  r not this.** is
fa300 20 57 69 6e 39 35 20 6f 72 20 57 69 6e 4e 54 2e   Win95 or WinNT.
fa310 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67  .*/.static int g
fa320 65 74 52 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69  etReadLock(winFi
fa330 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e  le *pFile){.  in
fa340 74 20 72 65 73 3b 0a 20 20 69 66 28 20 69 73 4e  t res;.  if( isN
fa350 54 28 29 20 29 7b 0a 20 20 20 20 4f 56 45 52 4c  T() ){.    OVERL
fa360 41 50 50 45 44 20 6f 76 6c 70 3b 0a 20 20 20 20  APPED ovlp;.    
fa370 6f 76 6c 70 2e 4f 66 66 73 65 74 20 3d 20 53 48  ovlp.Offset = SH
fa380 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20  ARED_FIRST;.    
fa390 6f 76 6c 70 2e 4f 66 66 73 65 74 48 69 67 68 20  ovlp.OffsetHigh 
fa3a0 3d 20 30 3b 0a 20 20 20 20 6f 76 6c 70 2e 68 45  = 0;.    ovlp.hE
fa3b0 76 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 72 65  vent = 0;.    re
fa3c0 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 45 78 28 70  s = LockFileEx(p
fa3d0 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 46 49 4c  File->h, LOCKFIL
fa3e0 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45  E_FAIL_IMMEDIATE
fa3f0 4c 59 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  LY,.            
fa400 20 20 20 20 20 20 20 20 20 30 2c 20 53 48 41 52           0, SHAR
fa410 45 44 5f 53 49 5a 45 2c 20 30 2c 20 26 6f 76 6c  ED_SIZE, 0, &ovl
fa420 70 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73  p);./* isNT() is
fa430 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f   1 if SQLITE_OS_
fa440 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69  WINCE==1, so thi
fa450 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20  s else is never 
fa460 65 78 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69  executed. .*/.#i
fa470 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
fa480 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  E==0.  }else{.  
fa490 20 20 69 6e 74 20 6c 6b 3b 0a 20 20 20 20 73 71    int lk;.    sq
fa4a0 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73  lite3_randomness
fa4b0 28 73 69 7a 65 6f 66 28 6c 6b 29 2c 20 26 6c 6b  (sizeof(lk), &lk
fa4c0 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 73 68  );.    pFile->sh
fa4d0 61 72 65 64 4c 6f 63 6b 42 79 74 65 20 3d 20 28  aredLockByte = (
fa4e0 73 68 6f 72 74 29 28 28 6c 6b 20 26 20 30 78 37  short)((lk & 0x7
fa4f0 66 66 66 66 66 66 66 29 25 28 53 48 41 52 45 44  fffffff)%(SHARED
fa500 5f 53 49 5a 45 20 2d 20 31 29 29 3b 0a 20 20 20  _SIZE - 1));.   
fa510 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28   res = LockFile(
fa520 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44  pFile->h, SHARED
fa530 5f 46 49 52 53 54 2b 70 46 69 6c 65 2d 3e 73 68  _FIRST+pFile->sh
fa540 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c  aredLockByte, 0,
fa550 20 31 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20   1, 0);.#endif. 
fa560 20 7d 0a 20 20 69 66 28 20 72 65 73 20 3d 3d 20   }.  if( res == 
fa570 30 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  0 ){.    pFile->
fa580 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c  lastErrno = GetL
fa590 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 7d 0a  astError();.  }.
fa5a0 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a    return res;.}.
fa5b0 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 65  ./*.** Undo a re
fa5c0 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69 63  adlock.*/.static
fa5d0 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 61 64 4c   int unlockReadL
fa5e0 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70 46 69  ock(winFile *pFi
fa5f0 6c 65 29 7b 0a 20 20 69 6e 74 20 72 65 73 3b 0a  le){.  int res;.
fa600 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a    if( isNT() ){.
fa610 20 20 20 20 72 65 73 20 3d 20 55 6e 6c 6f 63 6b      res = Unlock
fa620 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53  File(pFile->h, S
fa630 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c 20  HARED_FIRST, 0, 
fa640 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b  SHARED_SIZE, 0);
fa650 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20  ./* isNT() is 1 
fa660 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
fa670 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65  CE==1, so this e
fa680 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65  lse is never exe
fa690 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53  cuted. .*/.#if S
fa6a0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d  QLITE_OS_WINCE==
fa6b0 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  0.  }else{.    r
fa6c0 65 73 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c 65 28  es = UnlockFile(
fa6d0 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44  pFile->h, SHARED
fa6e0 5f 46 49 52 53 54 20 2b 20 70 46 69 6c 65 2d 3e  _FIRST + pFile->
fa6f0 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20  sharedLockByte, 
fa700 30 2c 20 31 2c 20 30 29 3b 0a 23 65 6e 64 69 66  0, 1, 0);.#endif
fa710 0a 20 20 7d 0a 20 20 69 66 28 20 72 65 73 20 3d  .  }.  if( res =
fa720 3d 20 30 20 29 7b 0a 20 20 20 20 70 46 69 6c 65  = 0 ){.    pFile
fa730 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65  ->lastErrno = Ge
fa740 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20  tLastError();.  
fa750 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a  }.  return res;.
fa760 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68  }../*.** Lock th
fa770 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20  e file with the 
fa780 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62  lock specified b
fa790 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b  y parameter lock
fa7a0 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66  type - one.** of
fa7b0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
fa7c0 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48  **.**     (1) SH
fa7d0 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  ARED_LOCK.**    
fa7e0 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f   (2) RESERVED_LO
fa7f0 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45  CK.**     (3) PE
fa800 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  NDING_LOCK.**   
fa810 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f    (4) EXCLUSIVE_
fa820 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74  LOCK.**.** Somet
fa830 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73  imes when reques
fa840 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74  ting one lock st
fa850 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20  ate, additional 
fa860 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61  lock states.** a
fa870 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62  re inserted in b
fa880 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63  etween.  The loc
fa890 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20  king might fail 
fa8a0 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61  on one of the la
fa8b0 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  ter.** transitio
fa8c0 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c  ns leaving the l
fa8d0 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72  ock state differ
fa8e0 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74  ent from what it
fa8f0 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20   started but.** 
fa900 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69  still short of i
fa910 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f  ts goal.  The fo
fa920 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68  llowing chart sh
fa930 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a  ows the allowed.
fa940 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61  ** transitions a
fa950 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20  nd the inserted 
fa960 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61  intermediate sta
fa970 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e  tes:.**.**    UN
fa980 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44  LOCKED -> SHARED
fa990 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e  .**    SHARED ->
fa9a0 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20   RESERVED.**    
fa9b0 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49  SHARED -> (PENDI
fa9c0 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  NG) -> EXCLUSIVE
fa9d0 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20  .**    RESERVED 
fa9e0 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20  -> (PENDING) -> 
fa9f0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20  EXCLUSIVE.**    
faa00 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55  PENDING -> EXCLU
faa10 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  SIVE.**.** This 
faa20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c  routine will onl
faa30 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63  y increase a loc
faa40 6b 2e 20 20 54 68 65 20 77 69 6e 55 6e 6c 6f 63  k.  The winUnloc
faa50 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65  k() routine.** e
faa60 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 20  rases all locks 
faa70 61 74 20 6f 6e 63 65 20 61 6e 64 20 72 65 74 75  at once and retu
faa80 72 6e 73 20 75 73 20 69 6d 6d 65 64 69 61 74 65  rns us immediate
faa90 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65  ly to locking le
faaa0 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 69 73 20  vel 0..** It is 
faab0 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20  not possible to 
faac0 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e  lower the lockin
faad0 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73 74 65 70  g level one step
faae0 20 61 74 20 61 20 74 69 6d 65 2e 20 20 59 6f 75   at a time.  You
faaf0 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73 74 72 61  .** must go stra
fab00 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 20  ight to locking 
fab10 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61 74  level 0..*/.stat
fab20 69 63 20 69 6e 74 20 77 69 6e 4c 6f 63 6b 28 73  ic int winLock(s
fab30 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
fab40 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a   int locktype){.
fab50 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
fab60 45 5f 4f 4b 3b 20 20 20 20 2f 2a 20 52 65 74 75  E_OK;    /* Retu
fab70 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 62  rn code from sub
fab80 72 6f 75 74 69 6e 65 73 20 2a 2f 0a 20 20 69 6e  routines */.  in
fab90 74 20 72 65 73 20 3d 20 31 3b 20 20 20 20 20 20  t res = 1;      
faba0 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f       /* Result o
fabb0 66 20 61 20 77 69 6e 64 6f 77 73 20 6c 6f 63 6b  f a windows lock
fabc0 20 63 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e   call */.  int n
fabd0 65 77 4c 6f 63 6b 74 79 70 65 3b 20 20 20 20 20  ewLocktype;     
fabe0 20 20 2f 2a 20 53 65 74 20 70 46 69 6c 65 2d 3e    /* Set pFile->
fabf0 6c 6f 63 6b 74 79 70 65 20 74 6f 20 74 68 69 73  locktype to this
fac00 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 65 78   value before ex
fac10 69 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 67  iting */.  int g
fac20 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20  otPendingLock = 
fac30 30 3b 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20  0;/* True if we 
fac40 61 63 71 75 69 72 65 64 20 61 20 50 45 4e 44 49  acquired a PENDI
fac50 4e 47 20 6c 6f 63 6b 20 74 68 69 73 20 74 69 6d  NG lock this tim
fac60 65 20 2a 2f 0a 20 20 77 69 6e 46 69 6c 65 20 2a  e */.  winFile *
fac70 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65  pFile = (winFile
fac80 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 65 72  *)id;.  DWORD er
fac90 72 6f 72 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a  ror = NO_ERROR;.
faca0 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30  .  assert( id!=0
facb0 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22   );.  OSTRACE5("
facc0 4c 4f 43 4b 20 25 64 20 25 64 20 77 61 73 20 25  LOCK %d %d was %
facd0 64 28 25 64 29 5c 6e 22 2c 0a 20 20 20 20 20 20  d(%d)\n",.      
face0 20 20 20 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f      pFile->h, lo
facf0 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 6c  cktype, pFile->l
fad00 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e  ocktype, pFile->
fad10 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 29 3b  sharedLockByte);
fad20 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
fad30 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f 63  is already a loc
fad40 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 6f  k of this type o
fad50 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 69  r more restricti
fad60 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 4f  ve on the.  ** O
fad70 73 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e  sFile, do nothin
fad80 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65  g. Don't use the
fad90 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20   end_lock: exit 
fada0 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20 73 71  path, as.  ** sq
fadb0 6c 69 74 65 33 4f 73 45 6e 74 65 72 4d 75 74 65  lite3OsEnterMute
fadc0 78 28 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20  x() hasn't been 
fadd0 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f  called yet..  */
fade0 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
fadf0 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65  cktype>=locktype
fae00 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
fae10 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20  QLITE_OK;.  }.. 
fae20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68   /* Make sure th
fae30 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e  e locking sequen
fae40 63 65 20 69 73 20 63 6f 72 72 65 63 74 0a 20 20  ce is correct.  
fae50 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46 69  */.  assert( pFi
fae60 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f  le->locktype!=NO
fae70 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70  _LOCK || locktyp
fae80 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e==SHARED_LOCK )
fae90 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b  ;.  assert( lock
faea0 74 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f  type!=PENDING_LO
faeb0 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  CK );.  assert( 
faec0 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 52 56  locktype!=RESERV
faed0 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65  ED_LOCK || pFile
faee0 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ->locktype==SHAR
faef0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a  ED_LOCK );..  /*
faf00 20 4c 6f 63 6b 20 74 68 65 20 50 45 4e 44 49 4e   Lock the PENDIN
faf10 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 66 20 77  G_LOCK byte if w
faf20 65 20 6e 65 65 64 20 74 6f 20 61 63 71 75 69 72  e need to acquir
faf30 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b  e a PENDING lock
faf40 20 6f 72 0a 20 20 2a 2a 20 61 20 53 48 41 52 45   or.  ** a SHARE
faf50 44 20 6c 6f 63 6b 2e 20 20 49 66 20 77 65 20 61  D lock.  If we a
faf60 72 65 20 61 63 71 75 69 72 69 6e 67 20 61 20 53  re acquiring a S
faf70 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20  HARED lock, the 
faf80 61 63 71 75 69 73 69 74 69 6f 6e 20 6f 66 0a 20  acquisition of. 
faf90 20 2a 2a 20 74 68 65 20 50 45 4e 44 49 4e 47 5f   ** the PENDING_
fafa0 4c 4f 43 4b 20 62 79 74 65 20 69 73 20 74 65 6d  LOCK byte is tem
fafb0 70 6f 72 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6e  porary..  */.  n
fafc0 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 70 46 69  ewLocktype = pFi
fafd0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  le->locktype;.  
fafe0 69 66 28 20 20 20 28 70 46 69 6c 65 2d 3e 6c 6f  if(   (pFile->lo
faff0 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 29  cktype==NO_LOCK)
fb000 0a 20 20 20 20 20 7c 7c 20 28 20 20 20 28 6c 6f  .     || (   (lo
fb010 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56  cktype==EXCLUSIV
fb020 45 5f 4c 4f 43 4b 29 0a 20 20 20 20 20 20 20 20  E_LOCK).        
fb030 20 26 26 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b   && (pFile->lock
fb040 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c  type==RESERVED_L
fb050 4f 43 4b 29 29 0a 20 20 29 7b 0a 20 20 20 20 69  OCK)).  ){.    i
fb060 6e 74 20 63 6e 74 20 3d 20 33 3b 0a 20 20 20 20  nt cnt = 3;.    
fb070 77 68 69 6c 65 28 20 63 6e 74 2d 2d 3e 30 20 26  while( cnt-->0 &
fb080 26 20 28 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c  & (res = LockFil
fb090 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44  e(pFile->h, PEND
fb0a0 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20  ING_BYTE, 0, 1, 
fb0b0 30 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  0))==0 ){.      
fb0c0 2f 2a 20 54 72 79 20 33 20 74 69 6d 65 73 20 74  /* Try 3 times t
fb0d0 6f 20 67 65 74 20 74 68 65 20 70 65 6e 64 69 6e  o get the pendin
fb0e0 67 20 6c 6f 63 6b 2e 20 20 54 68 65 20 70 65 6e  g lock.  The pen
fb0f0 64 69 6e 67 20 6c 6f 63 6b 20 6d 69 67 68 74 20  ding lock might 
fb100 62 65 0a 20 20 20 20 20 20 2a 2a 20 68 65 6c 64  be.      ** held
fb110 20 62 79 20 61 6e 6f 74 68 65 72 20 72 65 61 64   by another read
fb120 65 72 20 70 72 6f 63 65 73 73 20 77 68 6f 20 77  er process who w
fb130 69 6c 6c 20 72 65 6c 65 61 73 65 20 69 74 20 6d  ill release it m
fb140 6f 6d 65 6e 74 61 72 69 6c 79 2e 0a 20 20 20 20  omentarily..    
fb150 20 20 2a 2f 0a 20 20 20 20 20 20 4f 53 54 52 41    */.      OSTRA
fb160 43 45 32 28 22 63 6f 75 6c 64 20 6e 6f 74 20 67  CE2("could not g
fb170 65 74 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  et a PENDING loc
fb180 6b 2e 20 63 6e 74 3d 25 64 5c 6e 22 2c 20 63 6e  k. cnt=%d\n", cn
fb190 74 29 3b 0a 20 20 20 20 20 20 53 6c 65 65 70 28  t);.      Sleep(
fb1a0 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f  1);.    }.    go
fb1b0 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 72  tPendingLock = r
fb1c0 65 73 3b 0a 20 20 20 20 69 66 28 20 21 72 65 73  es;.    if( !res
fb1d0 20 29 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 20   ){.      error 
fb1e0 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29  = GetLastError()
fb1f0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
fb200 2a 20 41 63 71 75 69 72 65 20 61 20 73 68 61 72  * Acquire a shar
fb210 65 64 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69  ed lock.  */.  i
fb220 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  f( locktype==SHA
fb230 52 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20  RED_LOCK && res 
fb240 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
fb250 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  File->locktype==
fb260 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72  NO_LOCK );.    r
fb270 65 73 20 3d 20 67 65 74 52 65 61 64 4c 6f 63 6b  es = getReadLock
fb280 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 69 66 28  (pFile);.    if(
fb290 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65   res ){.      ne
fb2a0 77 4c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52  wLocktype = SHAR
fb2b0 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c  ED_LOCK;.    }el
fb2c0 73 65 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 20  se{.      error 
fb2d0 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29  = GetLastError()
fb2e0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
fb2f0 2a 20 41 63 71 75 69 72 65 20 61 20 52 45 53 45  * Acquire a RESE
fb300 52 56 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20  RVED lock.  */. 
fb310 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52   if( locktype==R
fb320 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20  ESERVED_LOCK && 
fb330 72 65 73 20 29 7b 0a 20 20 20 20 61 73 73 65 72  res ){.    asser
fb340 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  t( pFile->lockty
fb350 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe==SHARED_LOCK 
fb360 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63  );.    res = Loc
fb370 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20  kFile(pFile->h, 
fb380 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30  RESERVED_BYTE, 0
fb390 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  , 1, 0);.    if(
fb3a0 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65   res ){.      ne
fb3b0 77 4c 6f 63 6b 74 79 70 65 20 3d 20 52 45 53 45  wLocktype = RESE
fb3c0 52 56 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d  RVED_LOCK;.    }
fb3d0 65 6c 73 65 7b 0a 20 20 20 20 20 20 65 72 72 6f  else{.      erro
fb3e0 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72  r = GetLastError
fb3f0 28 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ();.    }.  }.. 
fb400 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 50 45   /* Acquire a PE
fb410 4e 44 49 4e 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a  NDING lock.  */.
fb420 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d    if( locktype==
fb430 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26  EXCLUSIVE_LOCK &
fb440 26 20 72 65 73 20 29 7b 0a 20 20 20 20 6e 65 77  & res ){.    new
fb450 4c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49  Locktype = PENDI
fb460 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 67 6f 74  NG_LOCK;.    got
fb470 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b  PendingLock = 0;
fb480 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69  .  }..  /* Acqui
fb490 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20  re an EXCLUSIVE 
fb4a0 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20  lock.  */.  if( 
fb4b0 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53  locktype==EXCLUS
fb4c0 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20  IVE_LOCK && res 
fb4d0 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
fb4e0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d  File->locktype>=
fb4f0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20  SHARED_LOCK );. 
fb500 20 20 20 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52     res = unlockR
fb510 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a  eadLock(pFile);.
fb520 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 75 6e      OSTRACE2("un
fb530 72 65 61 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22  readlock = %d\n"
fb540 2c 20 72 65 73 29 3b 0a 20 20 20 20 72 65 73 20  , res);.    res 
fb550 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65  = LockFile(pFile
fb560 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53  ->h, SHARED_FIRS
fb570 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53 49 5a  T, 0, SHARED_SIZ
fb580 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72  E, 0);.    if( r
fb590 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c  es ){.      newL
fb5a0 6f 63 6b 74 79 70 65 20 3d 20 45 58 43 4c 55 53  ocktype = EXCLUS
fb5b0 49 56 45 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65  IVE_LOCK;.    }e
fb5c0 6c 73 65 7b 0a 20 20 20 20 20 20 65 72 72 6f 72  lse{.      error
fb5d0 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28   = GetLastError(
fb5e0 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45  );.      OSTRACE
fb5f0 32 28 22 65 72 72 6f 72 2d 63 6f 64 65 20 3d 20  2("error-code = 
fb600 25 64 5c 6e 22 2c 20 65 72 72 6f 72 29 3b 0a 20  %d\n", error);. 
fb610 20 20 20 20 20 67 65 74 52 65 61 64 4c 6f 63 6b       getReadLock
fb620 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20  (pFile);.    }. 
fb630 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 61   }..  /* If we a
fb640 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e  re holding a PEN
fb650 44 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f  DING lock that o
fb660 75 67 68 74 20 74 6f 20 62 65 20 72 65 6c 65 61  ught to be relea
fb670 73 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72  sed, then.  ** r
fb680 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20  elease it now.. 
fb690 20 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 65 6e   */.  if( gotPen
fb6a0 64 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b  dingLock && lock
fb6b0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
fb6c0 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46  K ){.    UnlockF
fb6d0 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45  ile(pFile->h, PE
fb6e0 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31  NDING_BYTE, 0, 1
fb6f0 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  , 0);.  }..  /* 
fb700 55 70 64 61 74 65 20 74 68 65 20 73 74 61 74 65  Update the state
fb710 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 68 61 73   of the lock has
fb720 20 68 65 6c 64 20 69 6e 20 74 68 65 20 66 69 6c   held in the fil
fb730 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 68 65  e descriptor the
fb740 6e 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 68  n.  ** return th
fb750 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 72 65  e appropriate re
fb760 73 75 6c 74 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a  sult code..  */.
fb770 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20    if( res ){.   
fb780 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
fb790 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4f 53  .  }else{.    OS
fb7a0 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 46 41 49  TRACE4("LOCK FAI
fb7b0 4c 45 44 20 25 64 20 74 72 79 69 6e 67 20 66 6f  LED %d trying fo
fb7c0 72 20 25 64 20 62 75 74 20 67 6f 74 20 25 64 5c  r %d but got %d\
fb7d0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20  n", pFile->h,.  
fb7e0 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70           locktyp
fb7f0 65 2c 20 6e 65 77 4c 6f 63 6b 74 79 70 65 29 3b  e, newLocktype);
fb800 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74  .    pFile->last
fb810 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20  Errno = error;. 
fb820 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
fb830 55 53 59 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65  USY;.  }.  pFile
fb840 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 28 75 38  ->locktype = (u8
fb850 29 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 0a 20 20  )newLocktype;.  
fb860 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
fb870 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
fb880 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65   checks if there
fb890 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c   is a RESERVED l
fb8a0 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20  ock held on the 
fb8b0 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c  specified.** fil
fb8c0 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79  e by this or any
fb8d0 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20   other process. 
fb8e0 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69  If such a lock i
fb8f0 73 20 68 65 6c 64 2c 20 72 65 74 75 72 6e 0a 2a  s held, return.*
fb900 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 6f 74 68 65  * non-zero, othe
fb910 72 77 69 73 65 20 7a 65 72 6f 2e 0a 2a 2f 0a 73  rwise zero..*/.s
fb920 74 61 74 69 63 20 69 6e 74 20 77 69 6e 43 68 65  tatic int winChe
fb930 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73  ckReservedLock(s
fb940 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
fb950 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a   int *pResOut){.
fb960 20 20 69 6e 74 20 72 63 3b 0a 20 20 77 69 6e 46    int rc;.  winF
fb970 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69  ile *pFile = (wi
fb980 6e 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73  nFile*)id;..  as
fb990 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20  sert( id!=0 );. 
fb9a0 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
fb9b0 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c  type>=RESERVED_L
fb9c0 4f 43 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  OCK ){.    rc = 
fb9d0 31 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28  1;.    OSTRACE3(
fb9e0 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64  "TEST WR-LOCK %d
fb9f0 20 25 64 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20   %d (local)\n", 
fba00 70 46 69 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a 20  pFile->h, rc);. 
fba10 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d   }else{.    rc =
fba20 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d   LockFile(pFile-
fba30 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54  >h, RESERVED_BYT
fba40 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20  E, 0, 1, 0);.   
fba50 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
fba60 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c   UnlockFile(pFil
fba70 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42  e->h, RESERVED_B
fba80 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20  YTE, 0, 1, 0);. 
fba90 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 21 72     }.    rc = !r
fbaa0 63 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28  c;.    OSTRACE3(
fbab0 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64  "TEST WR-LOCK %d
fbac0 20 25 64 20 28 72 65 6d 6f 74 65 29 5c 6e 22 2c   %d (remote)\n",
fbad0 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a   pFile->h, rc);.
fbae0 20 20 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d    }.  *pResOut =
fbaf0 20 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 53 51   rc;.  return SQ
fbb00 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
fbb10 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b  * Lower the lock
fbb20 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c  ing level on fil
fbb30 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 64 20  e descriptor id 
fbb40 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f  to locktype.  lo
fbb50 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62  cktype.** must b
fbb60 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b  e either NO_LOCK
fbb70 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e   or SHARED_LOCK.
fbb80 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f  .**.** If the lo
fbb90 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74  cking level of t
fbba0 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
fbbb0 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74  or is already at
fbbc0 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65   or below.** the
fbbd0 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69   requested locki
fbbe0 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72  ng level, this r
fbbf0 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
fbc00 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 6e  p..**.** It is n
fbc10 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20  ot possible for 
fbc20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20  this routine to 
fbc30 66 61 69 6c 20 69 66 20 74 68 65 20 73 65 63 6f  fail if the seco
fbc40 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69  nd argument.** i
fbc50 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 20 49 66 20 74  s NO_LOCK.  If t
fbc60 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
fbc70 6e 74 20 69 73 20 53 48 41 52 45 44 5f 4c 4f 43  nt is SHARED_LOC
fbc80 4b 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  K then this rout
fbc90 69 6e 65 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74  ine.** might ret
fbca0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
fbcb0 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.*/.static int 
fbcc0 77 69 6e 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65  winUnlock(sqlite
fbcd0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
fbce0 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74  locktype){.  int
fbcf0 20 74 79 70 65 3b 0a 20 20 77 69 6e 46 69 6c 65   type;.  winFile
fbd00 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69   *pFile = (winFi
fbd10 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 72 63  le*)id;.  int rc
fbd20 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
fbd30 61 73 73 65 72 74 28 20 70 46 69 6c 65 21 3d 30  assert( pFile!=0
fbd40 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f   );.  assert( lo
fbd50 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c  cktype<=SHARED_L
fbd60 4f 43 4b 20 29 3b 0a 20 20 4f 53 54 52 41 43 45  OCK );.  OSTRACE
fbd70 35 28 22 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20  5("UNLOCK %d to 
fbd80 25 64 20 77 61 73 20 25 64 28 25 64 29 5c 6e 22  %d was %d(%d)\n"
fbd90 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b  , pFile->h, lock
fbda0 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20 20  type,.          
fbdb0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c  pFile->locktype,
fbdc0 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f   pFile->sharedLo
fbdd0 63 6b 42 79 74 65 29 3b 0a 20 20 74 79 70 65 20  ckByte);.  type 
fbde0 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  = pFile->locktyp
fbdf0 65 3b 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 45  e;.  if( type>=E
fbe00 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b  XCLUSIVE_LOCK ){
fbe10 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28  .    UnlockFile(
fbe20 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44  pFile->h, SHARED
fbe30 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 52 45  _FIRST, 0, SHARE
fbe40 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 20 20  D_SIZE, 0);.    
fbe50 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  if( locktype==SH
fbe60 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 21 67 65  ARED_LOCK && !ge
fbe70 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29  tReadLock(pFile)
fbe80 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69   ){.      /* Thi
fbe90 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68  s should never h
fbea0 61 70 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c  appen.  We shoul
fbeb0 64 20 61 6c 77 61 79 73 20 62 65 20 61 62 6c 65  d always be able
fbec0 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61   to.      ** rea
fbed0 63 71 75 69 72 65 20 74 68 65 20 72 65 61 64 20  cquire the read 
fbee0 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 72 63  lock */.      rc
fbef0 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
fbf00 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20  UNLOCK;.    }.  
fbf10 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45  }.  if( type>=RE
fbf20 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SERVED_LOCK ){. 
fbf30 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46     UnlockFile(pF
fbf40 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44  ile->h, RESERVED
fbf50 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b  _BYTE, 0, 1, 0);
fbf60 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63 6b 74  .  }.  if( lockt
fbf70 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 20  ype==NO_LOCK && 
fbf80 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43  type>=SHARED_LOC
fbf90 4b 20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b 52  K ){.    unlockR
fbfa0 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a  eadLock(pFile);.
fbfb0 20 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d    }.  if( type>=
fbfc0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a  PENDING_LOCK ){.
fbfd0 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70      UnlockFile(p
fbfe0 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47  File->h, PENDING
fbff0 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b  _BYTE, 0, 1, 0);
fc000 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f  .  }.  pFile->lo
fc010 63 6b 74 79 70 65 20 3d 20 28 75 38 29 6c 6f 63  cktype = (u8)loc
fc020 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20  ktype;.  return 
fc030 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  rc;.}../*.** Con
fc040 74 72 6f 6c 20 61 6e 64 20 71 75 65 72 79 20 6f  trol and query o
fc050 66 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20  f the open file 
fc060 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  handle..*/.stati
fc070 63 20 69 6e 74 20 77 69 6e 46 69 6c 65 43 6f 6e  c int winFileCon
fc080 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c  trol(sqlite3_fil
fc090 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76  e *id, int op, v
fc0a0 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 77  oid *pArg){.  sw
fc0b0 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20  itch( op ){.    
fc0c0 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54  case SQLITE_FCNT
fc0d0 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20  L_LOCKSTATE: {. 
fc0e0 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67       *(int*)pArg
fc0f0 20 3d 20 28 28 77 69 6e 46 69 6c 65 2a 29 69 64   = ((winFile*)id
fc100 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20  )->locktype;.   
fc110 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
fc120 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  _OK;.    }.    c
fc130 61 73 65 20 53 51 4c 49 54 45 5f 4c 41 53 54 5f  ase SQLITE_LAST_
fc140 45 52 52 4e 4f 3a 20 7b 0a 20 20 20 20 20 20 2a  ERRNO: {.      *
fc150 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 69 6e  (int*)pArg = (in
fc160 74 29 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 29  t)((winFile*)id)
fc170 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20  ->lastErrno;.   
fc180 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
fc190 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  _OK;.    }.  }. 
fc1a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
fc1b0 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  RROR;.}../*.** R
fc1c0 65 74 75 72 6e 20 74 68 65 20 73 65 63 74 6f 72  eturn the sector
fc1d0 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f   size in bytes o
fc1e0 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  f the underlying
fc1f0 20 62 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 6f   block device fo
fc200 72 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69  r.** the specifi
fc210 65 64 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73  ed file. This is
fc220 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 35   almost always 5
fc230 31 32 20 62 79 74 65 73 2c 20 62 75 74 20 6d 61  12 bytes, but ma
fc240 79 20 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66  y be.** larger f
fc250 6f 72 20 73 6f 6d 65 20 64 65 76 69 63 65 73 2e  or some devices.
fc260 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f  .**.** SQLite co
fc270 64 65 20 61 73 73 75 6d 65 73 20 74 68 69 73 20  de assumes this 
fc280 66 75 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20  function cannot 
fc290 66 61 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73  fail. It also as
fc2a0 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69 66  sumes that.** if
fc2b0 20 74 77 6f 20 66 69 6c 65 73 20 61 72 65 20 63   two files are c
fc2c0 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 73 61  reated in the sa
fc2d0 6d 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 64  me file-system d
fc2e0 69 72 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a  irectory (i.e..*
fc2f0 2a 20 61 20 64 61 74 61 62 61 73 65 20 61 6e 64  * a database and
fc300 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   its journal fil
fc310 65 29 20 74 68 61 74 20 74 68 65 20 73 65 63 74  e) that the sect
fc320 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 20  or size will be 
fc330 74 68 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20  the.** same for 
fc340 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  both..*/.static 
fc350 69 6e 74 20 77 69 6e 53 65 63 74 6f 72 53 69 7a  int winSectorSiz
fc360 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
fc370 69 64 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69  id){.  assert( i
fc380 64 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e  d!=0 );.  return
fc390 20 28 69 6e 74 29 28 28 28 77 69 6e 46 69 6c 65   (int)(((winFile
fc3a0 2a 29 69 64 29 2d 3e 73 65 63 74 6f 72 53 69 7a  *)id)->sectorSiz
fc3b0 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  e);.}../*.** Ret
fc3c0 75 72 6e 20 61 20 76 65 63 74 6f 72 20 6f 66 20  urn a vector of 
fc3d0 64 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72  device character
fc3e0 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 74 61 74 69  istics..*/.stati
fc3f0 63 20 69 6e 74 20 77 69 6e 44 65 76 69 63 65 43  c int winDeviceC
fc400 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 73  haracteristics(s
fc410 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
fc420 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
fc430 45 54 45 52 28 69 64 29 3b 0a 20 20 72 65 74 75  ETER(id);.  retu
fc440 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  rn 0;.}../*.** T
fc450 68 69 73 20 76 65 63 74 6f 72 20 64 65 66 69 6e  his vector defin
fc460 65 73 20 61 6c 6c 20 74 68 65 20 6d 65 74 68 6f  es all the metho
fc470 64 73 20 74 68 61 74 20 63 61 6e 20 6f 70 65 72  ds that can oper
fc480 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c  ate on an.** sql
fc490 69 74 65 33 5f 66 69 6c 65 20 66 6f 72 20 77 69  ite3_file for wi
fc4a0 6e 33 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  n32..*/.static c
fc4b0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f  onst sqlite3_io_
fc4c0 6d 65 74 68 6f 64 73 20 77 69 6e 49 6f 4d 65 74  methods winIoMet
fc4d0 68 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20  hod = {.  1,    
fc4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fc4f0 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20      /* iVersion 
fc500 2a 2f 0a 20 20 77 69 6e 43 6c 6f 73 65 2c 0a 20  */.  winClose,. 
fc510 20 77 69 6e 52 65 61 64 2c 0a 20 20 77 69 6e 57   winRead,.  winW
fc520 72 69 74 65 2c 0a 20 20 77 69 6e 54 72 75 6e 63  rite,.  winTrunc
fc530 61 74 65 2c 0a 20 20 77 69 6e 53 79 6e 63 2c 0a  ate,.  winSync,.
fc540 20 20 77 69 6e 46 69 6c 65 53 69 7a 65 2c 0a 20    winFileSize,. 
fc550 20 77 69 6e 4c 6f 63 6b 2c 0a 20 20 77 69 6e 55   winLock,.  winU
fc560 6e 6c 6f 63 6b 2c 0a 20 20 77 69 6e 43 68 65 63  nlock,.  winChec
fc570 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c 0a 20  kReservedLock,. 
fc580 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c   winFileControl,
fc590 0a 20 20 77 69 6e 53 65 63 74 6f 72 53 69 7a 65  .  winSectorSize
fc5a0 2c 0a 20 20 77 69 6e 44 65 76 69 63 65 43 68 61  ,.  winDeviceCha
fc5b0 72 61 63 74 65 72 69 73 74 69 63 73 0a 7d 3b 0a  racteristics.};.
fc5c0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
fc5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fc5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fc5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fc600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
fc610 20 48 65 72 65 20 65 6e 64 73 20 74 68 65 20 49   Here ends the I
fc620 2f 4f 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20  /O methods that 
fc630 66 6f 72 6d 20 74 68 65 20 73 71 6c 69 74 65 33  form the sqlite3
fc640 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65  _io_methods obje
fc650 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65  ct..**.** The ne
fc660 78 74 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65  xt block of code
fc670 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20   implements the 
fc680 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 2a  VFS methods..***
fc690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fc6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fc6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fc6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fc6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a  *********/../*.*
fc6e0 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d  * Convert a UTF-
fc6f0 38 20 66 69 6c 65 6e 61 6d 65 20 69 6e 74 6f 20  8 filename into 
fc700 77 68 61 74 65 76 65 72 20 66 6f 72 6d 20 74 68  whatever form th
fc710 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20  e underlying.** 
fc720 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
fc730 20 77 61 6e 74 73 20 66 69 6c 65 6e 61 6d 65 73   wants filenames
fc740 20 69 6e 2e 20 20 53 70 61 63 65 20 74 6f 20 68   in.  Space to h
fc750 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 0a 2a  old the result.*
fc760 2a 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72  * is obtained fr
fc770 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75  om malloc and mu
fc780 73 74 20 62 65 20 66 72 65 65 64 20 62 79 20 74  st be freed by t
fc790 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75  he calling.** fu
fc7a0 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  nction..*/.stati
fc7b0 63 20 76 6f 69 64 20 2a 63 6f 6e 76 65 72 74 55  c void *convertU
fc7c0 74 66 38 46 69 6c 65 6e 61 6d 65 28 63 6f 6e 73  tf8Filename(cons
fc7d0 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
fc7e0 65 29 7b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e  e){.  void *zCon
fc7f0 76 65 72 74 65 64 20 3d 20 30 3b 0a 20 20 69 66  verted = 0;.  if
fc800 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20  ( isNT() ){.    
fc810 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75 74 66  zConverted = utf
fc820 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65  8ToUnicode(zFile
fc830 6e 61 6d 65 29 3b 0a 2f 2a 20 69 73 4e 54 28 29  name);./* isNT()
fc840 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f   is 1 if SQLITE_
fc850 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20  OS_WINCE==1, so 
fc860 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76  this else is nev
fc870 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2f  er executed. .*/
fc880 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
fc890 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b  INCE==0.  }else{
fc8a0 0a 20 20 20 20 7a 43 6f 6e 76 65 72 74 65 64 20  .    zConverted 
fc8b0 3d 20 75 74 66 38 54 6f 4d 62 63 73 28 7a 46 69  = utf8ToMbcs(zFi
fc8c0 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a  lename);.#endif.
fc8d0 20 20 7d 0a 20 20 2f 2a 20 63 61 6c 6c 65 72 20    }.  /* caller 
fc8e0 77 69 6c 6c 20 68 61 6e 64 6c 65 20 6f 75 74 20  will handle out 
fc8f0 6f 66 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 72  of memory */.  r
fc900 65 74 75 72 6e 20 7a 43 6f 6e 76 65 72 74 65 64  eturn zConverted
fc910 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  ;.}../*.** Creat
fc920 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69  e a temporary fi
fc930 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e  le name in zBuf.
fc940 20 20 7a 42 75 66 20 6d 75 73 74 20 62 65 20 62    zBuf must be b
fc950 69 67 20 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20  ig enough to.** 
fc960 68 6f 6c 64 20 61 74 20 70 56 66 73 2d 3e 6d 78  hold at pVfs->mx
fc970 50 61 74 68 6e 61 6d 65 20 63 68 61 72 61 63 74  Pathname charact
fc980 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ers..*/.static i
fc990 6e 74 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 69  nt getTempname(i
fc9a0 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a  nt nBuf, char *z
fc9b0 42 75 66 29 7b 0a 20 20 73 74 61 74 69 63 20 63  Buf){.  static c
fc9c0 68 61 72 20 7a 43 68 61 72 73 5b 5d 20 3d 0a 20  har zChars[] =. 
fc9d0 20 20 20 22 61 62 63 64 65 66 67 68 69 6a 6b 6c     "abcdefghijkl
fc9e0 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 22 0a  mnopqrstuvwxyz".
fc9f0 20 20 20 20 22 41 42 43 44 45 46 47 48 49 4a 4b      "ABCDEFGHIJK
fca00 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 22  LMNOPQRSTUVWXYZ"
fca10 0a 20 20 20 20 22 30 31 32 33 34 35 36 37 38 39  .    "0123456789
fca20 22 3b 0a 20 20 73 69 7a 65 5f 74 20 69 2c 20 6a  ";.  size_t i, j
fca30 3b 0a 20 20 63 68 61 72 20 7a 54 65 6d 70 50 61  ;.  char zTempPa
fca40 74 68 5b 4d 41 58 5f 50 41 54 48 2b 31 5d 3b 0a  th[MAX_PATH+1];.
fca50 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 74 65    if( sqlite3_te
fca60 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 29 7b 0a  mp_directory ){.
fca70 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
fca80 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 2d 33 30  intf(MAX_PATH-30
fca90 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20 22 25 73  , zTempPath, "%s
fcaa0 22 2c 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f  ", sqlite3_temp_
fcab0 64 69 72 65 63 74 6f 72 79 29 3b 0a 20 20 7d 65  directory);.  }e
fcac0 6c 73 65 20 69 66 28 20 69 73 4e 54 28 29 20 29  lse if( isNT() )
fcad0 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 75 6c  {.    char *zMul
fcae0 74 69 3b 0a 20 20 20 20 57 43 48 41 52 20 7a 57  ti;.    WCHAR zW
fcaf0 69 64 65 50 61 74 68 5b 4d 41 58 5f 50 41 54 48  idePath[MAX_PATH
fcb00 5d 3b 0a 20 20 20 20 47 65 74 54 65 6d 70 50 61  ];.    GetTempPa
fcb10 74 68 57 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c  thW(MAX_PATH-30,
fcb20 20 7a 57 69 64 65 50 61 74 68 29 3b 0a 20 20 20   zWidePath);.   
fcb30 20 7a 4d 75 6c 74 69 20 3d 20 75 6e 69 63 6f 64   zMulti = unicod
fcb40 65 54 6f 55 74 66 38 28 7a 57 69 64 65 50 61 74  eToUtf8(zWidePat
fcb50 68 29 3b 0a 20 20 20 20 69 66 28 20 7a 4d 75 6c  h);.    if( zMul
fcb60 74 69 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ti ){.      sqli
fcb70 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58  te3_snprintf(MAX
fcb80 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d 70 50  _PATH-30, zTempP
fcb90 61 74 68 2c 20 22 25 73 22 2c 20 7a 4d 75 6c 74  ath, "%s", zMult
fcba0 69 29 3b 0a 20 20 20 20 20 20 66 72 65 65 28 7a  i);.      free(z
fcbb0 4d 75 6c 74 69 29 3b 0a 20 20 20 20 7d 65 6c 73  Multi);.    }els
fcbc0 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  e{.      return 
fcbd0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
fcbe0 20 20 7d 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73    }./* isNT() is
fcbf0 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f   1 if SQLITE_OS_
fcc00 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69  WINCE==1, so thi
fcc10 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20  s else is never 
fcc20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69  executed. .** Si
fcc30 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65  nce the ASCII ve
fcc40 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57  rsion of these W
fcc50 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f  indows API do no
fcc60 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43  t exist for WINC
fcc70 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72  E,.** it's impor
fcc80 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65  tant to not refe
fcc90 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57  rence them for W
fcca0 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a  INCE builds..*/.
fccb0 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
fccc0 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a  NCE==0.  }else{.
fccd0 20 20 20 20 63 68 61 72 20 2a 7a 55 74 66 38 3b      char *zUtf8;
fcce0 0a 20 20 20 20 63 68 61 72 20 7a 4d 62 63 73 50  .    char zMbcsP
fccf0 61 74 68 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a 20  ath[MAX_PATH];. 
fcd00 20 20 20 47 65 74 54 65 6d 70 50 61 74 68 41 28     GetTempPathA(
fcd10 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 4d 62  MAX_PATH-30, zMb
fcd20 63 73 50 61 74 68 29 3b 0a 20 20 20 20 7a 55 74  csPath);.    zUt
fcd30 66 38 20 3d 20 73 71 6c 69 74 65 33 5f 77 69 6e  f8 = sqlite3_win
fcd40 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28  32_mbcs_to_utf8(
fcd50 7a 4d 62 63 73 50 61 74 68 29 3b 0a 20 20 20 20  zMbcsPath);.    
fcd60 69 66 28 20 7a 55 74 66 38 20 29 7b 0a 20 20 20  if( zUtf8 ){.   
fcd70 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
fcd80 6e 74 66 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c  ntf(MAX_PATH-30,
fcd90 20 7a 54 65 6d 70 50 61 74 68 2c 20 22 25 73 22   zTempPath, "%s"
fcda0 2c 20 7a 55 74 66 38 29 3b 0a 20 20 20 20 20 20  , zUtf8);.      
fcdb0 66 72 65 65 28 7a 55 74 66 38 29 3b 0a 20 20 20  free(zUtf8);.   
fcdc0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65   }else{.      re
fcdd0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
fcde0 4d 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  M;.    }.#endif.
fcdf0 20 20 7d 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69    }.  for(i=sqli
fce00 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d  te3Strlen30(zTem
fce10 70 50 61 74 68 29 3b 20 69 3e 30 20 26 26 20 7a  pPath); i>0 && z
fce20 54 65 6d 70 50 61 74 68 5b 69 2d 31 5d 3d 3d 27  TempPath[i-1]=='
fce30 5c 5c 27 3b 20 69 2d 2d 29 7b 7d 0a 20 20 7a 54  \\'; i--){}.  zT
fce40 65 6d 70 50 61 74 68 5b 69 5d 20 3d 20 30 3b 0a  empPath[i] = 0;.
fce50 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
fce60 74 66 28 6e 42 75 66 2d 33 30 2c 20 7a 42 75 66  tf(nBuf-30, zBuf
fce70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
fce80 20 20 20 20 20 22 25 73 5c 5c 22 53 51 4c 49 54       "%s\\"SQLIT
fce90 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46  E_TEMP_FILE_PREF
fcea0 49 58 2c 20 7a 54 65 6d 70 50 61 74 68 29 3b 0a  IX, zTempPath);.
fceb0 20 20 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72    j = sqlite3Str
fcec0 6c 65 6e 33 30 28 7a 42 75 66 29 3b 0a 20 20 73  len30(zBuf);.  s
fced0 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73  qlite3_randomnes
fcee0 73 28 32 30 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b  s(20, &zBuf[j]);
fcef0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 30  .  for(i=0; i<20
fcf00 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20  ; i++, j++){.   
fcf10 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72   zBuf[j] = (char
fcf20 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69 67  )zChars[ ((unsig
fcf30 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d  ned char)zBuf[j]
fcf40 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 73  )%(sizeof(zChars
fcf50 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a 20 20 7a 42  )-1) ];.  }.  zB
fcf60 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 4f 53 54  uf[j] = 0;.  OST
fcf70 52 41 43 45 32 28 22 54 45 4d 50 20 46 49 4c 45  RACE2("TEMP FILE
fcf80 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c 20 7a 42 75  NAME: %s\n", zBu
fcf90 66 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  f);.  return SQL
fcfa0 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a  ITE_OK; .}../*.*
fcfb0 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c  * The return val
fcfc0 75 65 20 6f 66 20 67 65 74 4c 61 73 74 45 72 72  ue of getLastErr
fcfd0 6f 72 4d 73 67 0a 2a 2a 20 69 73 20 7a 65 72 6f  orMsg.** is zero
fcfe0 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65   if the error me
fcff0 73 73 61 67 65 20 66 69 74 73 20 69 6e 20 74 68  ssage fits in th
fd000 65 20 62 75 66 66 65 72 2c 20 6f 72 20 6e 6f 6e  e buffer, or non
fd010 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68 65 72 77 69  -zero.** otherwi
fd020 73 65 20 28 69 66 20 74 68 65 20 6d 65 73 73 61  se (if the messa
fd030 67 65 20 77 61 73 20 74 72 75 6e 63 61 74 65 64  ge was truncated
fd040 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
fd050 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67   getLastErrorMsg
fd060 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20  (int nBuf, char 
fd070 2a 7a 42 75 66 29 7b 0a 20 20 44 57 4f 52 44 20  *zBuf){.  DWORD 
fd080 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45  error = GetLastE
fd090 72 72 6f 72 28 29 3b 0a 0a 23 69 66 20 53 51 4c  rror();..#if SQL
fd0a0 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 73  ITE_OS_WINCE.  s
fd0b0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
fd0c0 6e 42 75 66 2c 20 7a 42 75 66 2c 20 22 4f 73 45  nBuf, zBuf, "OsE
fd0d0 72 72 6f 72 20 30 78 25 78 20 28 25 75 29 22 2c  rror 0x%x (%u)",
fd0e0 20 65 72 72 6f 72 2c 20 65 72 72 6f 72 29 3b 0a   error, error);.
fd0f0 23 65 6c 73 65 0a 20 20 2f 2a 20 46 6f 72 6d 61  #else.  /* Forma
fd100 74 4d 65 73 73 61 67 65 20 72 65 74 75 72 6e 73  tMessage returns
fd110 20 30 20 6f 6e 20 66 61 69 6c 75 72 65 2e 20 20   0 on failure.  
fd120 4f 74 68 65 72 77 69 73 65 20 69 74 0a 20 20 2a  Otherwise it.  *
fd130 2a 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  * returns the nu
fd140 6d 62 65 72 20 6f 66 20 54 43 48 41 52 73 20 77  mber of TCHARs w
fd150 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6f 75  ritten to the ou
fd160 74 70 75 74 0a 20 20 2a 2a 20 62 75 66 66 65 72  tput.  ** buffer
fd170 2c 20 65 78 63 6c 75 64 69 6e 67 20 74 68 65 20  , excluding the 
fd180 74 65 72 6d 69 6e 61 74 69 6e 67 20 6e 75 6c 6c  terminating null
fd190 20 63 68 61 72 2e 0a 20 20 2a 2f 0a 20 20 69 66   char..  */.  if
fd1a0 20 28 21 46 6f 72 6d 61 74 4d 65 73 73 61 67 65   (!FormatMessage
fd1b0 41 28 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45  A(FORMAT_MESSAGE
fd1c0 5f 46 52 4f 4d 5f 53 59 53 54 45 4d 2c 0a 20 20  _FROM_SYSTEM,.  
fd1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fd1e0 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20      NULL,.      
fd1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fd200 65 72 72 6f 72 2c 0a 20 20 20 20 20 20 20 20 20  error,.         
fd210 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 0a               0,.
fd220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fd230 20 20 20 20 20 20 7a 42 75 66 2c 0a 20 20 20 20        zBuf,.    
fd240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fd250 20 20 6e 42 75 66 2d 31 2c 0a 20 20 20 20 20 20    nBuf-1,.      
fd260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fd270 30 29 29 0a 20 20 7b 0a 20 20 20 20 73 71 6c 69  0)).  {.    sqli
fd280 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75  te3_snprintf(nBu
fd290 66 2c 20 7a 42 75 66 2c 20 22 4f 73 45 72 72 6f  f, zBuf, "OsErro
fd2a0 72 20 30 78 25 78 20 28 25 75 29 22 2c 20 65 72  r 0x%x (%u)", er
fd2b0 72 6f 72 2c 20 65 72 72 6f 72 29 3b 0a 20 20 7d  ror, error);.  }
fd2c0 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72  .#endif..  retur
fd2d0 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70  n 0;.}../*.** Op
fd2e0 65 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74  en a file..*/.st
fd2f0 61 74 69 63 20 69 6e 74 20 77 69 6e 4f 70 65 6e  atic int winOpen
fd300 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  (.  sqlite3_vfs 
fd310 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 2f 2a  *pVfs,        /*
fd320 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 63   Not used */.  c
fd330 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
fd340 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ,        /* Name
fd350 20 6f 66 20 74 68 65 20 66 69 6c 65 20 28 55 54   of the file (UT
fd360 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65  F-8) */.  sqlite
fd370 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20  3_file *id,     
fd380 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
fd390 20 53 51 4c 69 74 65 20 66 69 6c 65 20 68 61 6e   SQLite file han
fd3a0 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e  dle here */.  in
fd3b0 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20  t flags,        
fd3c0 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20          /* Open 
fd3d0 6d 6f 64 65 20 66 6c 61 67 73 20 2a 2f 0a 20 20  mode flags */.  
fd3e0 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 20 20  int *pOutFlags  
fd3f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61            /* Sta
fd400 74 75 73 20 72 65 74 75 72 6e 20 66 6c 61 67 73  tus return flags
fd410 20 2a 2f 0a 29 7b 0a 20 20 48 41 4e 44 4c 45 20   */.){.  HANDLE 
fd420 68 3b 0a 20 20 44 57 4f 52 44 20 64 77 44 65 73  h;.  DWORD dwDes
fd430 69 72 65 64 41 63 63 65 73 73 3b 0a 20 20 44 57  iredAccess;.  DW
fd440 4f 52 44 20 64 77 53 68 61 72 65 4d 6f 64 65 3b  ORD dwShareMode;
fd450 0a 20 20 44 57 4f 52 44 20 64 77 43 72 65 61 74  .  DWORD dwCreat
fd460 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 3b 0a  ionDisposition;.
fd470 20 20 44 57 4f 52 44 20 64 77 46 6c 61 67 73 41    DWORD dwFlagsA
fd480 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d 20 30  ndAttributes = 0
fd490 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  ;.#if SQLITE_OS_
fd4a0 57 49 4e 43 45 0a 20 20 69 6e 74 20 69 73 54 65  WINCE.  int isTe
fd4b0 6d 70 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20  mp = 0;.#endif. 
fd4c0 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20   winFile *pFile 
fd4d0 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a  = (winFile*)id;.
fd4e0 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74    void *zConvert
fd4f0 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ed;             
fd500 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20      /* Filename 
fd510 69 6e 20 4f 53 20 65 6e 63 6f 64 69 6e 67 20 2a  in OS encoding *
fd520 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
fd530 7a 55 74 66 38 4e 61 6d 65 20 3d 20 7a 4e 61 6d  zUtf8Name = zNam
fd540 65 3b 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d  e;    /* Filenam
fd550 65 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64  e in UTF-8 encod
fd560 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 7a 54  ing */.  char zT
fd570 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 48 2b  mpname[MAX_PATH+
fd580 31 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 75  1];        /* Bu
fd590 66 66 65 72 20 75 73 65 64 20 74 6f 20 63 72 65  ffer used to cre
fd5a0 61 74 65 20 74 65 6d 70 20 66 69 6c 65 6e 61 6d  ate temp filenam
fd5b0 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  e */..  assert( 
fd5c0 69 64 21 3d 30 20 29 3b 0a 20 20 55 4e 55 53 45  id!=0 );.  UNUSE
fd5d0 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73  D_PARAMETER(pVfs
fd5e0 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
fd5f0 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
fd600 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
fd610 20 69 73 20 4e 55 4c 4c 2c 20 67 65 6e 65 72 61   is NULL, genera
fd620 74 65 20 61 20 0a 20 20 2a 2a 20 74 65 6d 70 6f  te a .  ** tempo
fd630 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 74  rary file name t
fd640 6f 20 75 73 65 20 0a 20 20 2a 2f 0a 20 20 69 66  o use .  */.  if
fd650 28 20 21 7a 55 74 66 38 4e 61 6d 65 20 29 7b 0a  ( !zUtf8Name ){.
fd660 20 20 20 20 69 6e 74 20 72 63 20 3d 20 67 65 74      int rc = get
fd670 54 65 6d 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54  Tempname(MAX_PAT
fd680 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a  H+1, zTmpname);.
fd690 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
fd6a0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
fd6b0 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
fd6c0 20 20 20 20 7a 55 74 66 38 4e 61 6d 65 20 3d 20      zUtf8Name = 
fd6d0 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20  zTmpname;.  }.. 
fd6e0 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20   /* Convert the 
fd6f0 66 69 6c 65 6e 61 6d 65 20 74 6f 20 74 68 65 20  filename to the 
fd700 73 79 73 74 65 6d 20 65 6e 63 6f 64 69 6e 67 2e  system encoding.
fd710 20 2a 2f 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64   */.  zConverted
fd720 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69   = convertUtf8Fi
fd730 6c 65 6e 61 6d 65 28 7a 55 74 66 38 4e 61 6d 65  lename(zUtf8Name
fd740 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72  );.  if( zConver
fd750 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ted==0 ){.    re
fd760 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
fd770 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c  M;.  }..  if( fl
fd780 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
fd790 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20  N_READWRITE ){. 
fd7a0 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65     dwDesiredAcce
fd7b0 73 73 20 3d 20 47 45 4e 45 52 49 43 5f 52 45 41  ss = GENERIC_REA
fd7c0 44 20 7c 20 47 45 4e 45 52 49 43 5f 57 52 49 54  D | GENERIC_WRIT
fd7d0 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  E;.  }else{.    
fd7e0 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 20  dwDesiredAccess 
fd7f0 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 44 3b 0a  = GENERIC_READ;.
fd800 20 20 7d 0a 20 20 2f 2a 20 53 51 4c 49 54 45 5f    }.  /* SQLITE_
fd810 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 69  OPEN_EXCLUSIVE i
fd820 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73  s used to make s
fd830 75 72 65 20 74 68 61 74 20 61 20 6e 65 77 20 66  ure that a new f
fd840 69 6c 65 20 69 73 20 0a 20 20 2a 2a 20 63 72 65  ile is .  ** cre
fd850 61 74 65 64 2e 20 53 51 4c 69 74 65 20 64 6f 65  ated. SQLite doe
fd860 73 6e 27 74 20 75 73 65 20 69 74 20 74 6f 20 69  sn't use it to i
fd870 6e 64 69 63 61 74 65 20 22 65 78 63 6c 75 73 69  ndicate "exclusi
fd880 76 65 20 61 63 63 65 73 73 22 20 0a 20 20 2a 2a  ve access" .  **
fd890 20 61 73 20 69 74 20 69 73 20 75 73 75 61 6c 6c   as it is usuall
fd8a0 79 20 75 6e 64 65 72 73 74 6f 6f 64 2e 0a 20 20  y understood..  
fd8b0 2a 2f 0a 20 20 61 73 73 65 72 74 28 21 28 66 6c  */.  assert(!(fl
fd8c0 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
fd8d0 4e 5f 45 58 43 4c 55 53 49 56 45 29 20 7c 7c 20  N_EXCLUSIVE) || 
fd8e0 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  (flags & SQLITE_
fd8f0 4f 50 45 4e 5f 43 52 45 41 54 45 29 29 3b 0a 20  OPEN_CREATE));. 
fd900 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c   if( flags & SQL
fd910 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49  ITE_OPEN_EXCLUSI
fd920 56 45 20 29 7b 0a 20 20 20 20 2f 2a 20 43 72 65  VE ){.    /* Cre
fd930 61 74 65 73 20 61 20 6e 65 77 20 66 69 6c 65 2c  ates a new file,
fd940 20 6f 6e 6c 79 20 69 66 20 69 74 20 64 6f 65 73   only if it does
fd950 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69   not already exi
fd960 73 74 2e 20 2a 2f 0a 20 20 20 20 2f 2a 20 49 66  st. */.    /* If
fd970 20 74 68 65 20 66 69 6c 65 20 65 78 69 73 74 73   the file exists
fd980 2c 20 69 74 20 66 61 69 6c 73 2e 20 2a 2f 0a 20  , it fails. */. 
fd990 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73     dwCreationDis
fd9a0 70 6f 73 69 74 69 6f 6e 20 3d 20 43 52 45 41 54  position = CREAT
fd9b0 45 5f 4e 45 57 3b 0a 20 20 7d 65 6c 73 65 20 69  E_NEW;.  }else i
fd9c0 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f( flags & SQLIT
fd9d0 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 29 7b  E_OPEN_CREATE ){
fd9e0 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 65 78 69  .    /* Open exi
fd9f0 73 74 69 6e 67 20 66 69 6c 65 2c 20 6f 72 20 63  sting file, or c
fda00 72 65 61 74 65 20 69 66 20 69 74 20 64 6f 65 73  reate if it does
fda10 6e 27 74 20 65 78 69 73 74 20 2a 2f 0a 20 20 20  n't exist */.   
fda20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f   dwCreationDispo
fda30 73 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f 41 4c  sition = OPEN_AL
fda40 57 41 59 53 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  WAYS;.  }else{. 
fda50 20 20 20 2f 2a 20 4f 70 65 6e 73 20 61 20 66 69     /* Opens a fi
fda60 6c 65 2c 20 6f 6e 6c 79 20 69 66 20 69 74 20 65  le, only if it e
fda70 78 69 73 74 73 2e 20 2a 2f 0a 20 20 20 20 64 77  xists. */.    dw
fda80 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74  CreationDisposit
fda90 69 6f 6e 20 3d 20 4f 50 45 4e 5f 45 58 49 53 54  ion = OPEN_EXIST
fdaa0 49 4e 47 3b 0a 20 20 7d 0a 20 20 64 77 53 68 61  ING;.  }.  dwSha
fdab0 72 65 4d 6f 64 65 20 3d 20 46 49 4c 45 5f 53 48  reMode = FILE_SH
fdac0 41 52 45 5f 52 45 41 44 20 7c 20 46 49 4c 45 5f  ARE_READ | FILE_
fdad0 53 48 41 52 45 5f 57 52 49 54 45 3b 0a 20 20 69  SHARE_WRITE;.  i
fdae0 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f( flags & SQLIT
fdaf0 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43  E_OPEN_DELETEONC
fdb00 4c 4f 53 45 20 29 7b 0a 23 69 66 20 53 51 4c 49  LOSE ){.#if SQLI
fdb10 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 20 20  TE_OS_WINCE.    
fdb20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62  dwFlagsAndAttrib
fdb30 75 74 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 52  utes = FILE_ATTR
fdb40 49 42 55 54 45 5f 48 49 44 44 45 4e 3b 0a 20 20  IBUTE_HIDDEN;.  
fdb50 20 20 69 73 54 65 6d 70 20 3d 20 31 3b 0a 23 65    isTemp = 1;.#e
fdb60 6c 73 65 0a 20 20 20 20 64 77 46 6c 61 67 73 41  lse.    dwFlagsA
fdb70 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d 20 46  ndAttributes = F
fdb80 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 54 45  ILE_ATTRIBUTE_TE
fdb90 4d 50 4f 52 41 52 59 0a 20 20 20 20 20 20 20 20  MPORARY.        
fdba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fdbb0 20 20 20 20 20 20 20 7c 20 46 49 4c 45 5f 41 54         | FILE_AT
fdbc0 54 52 49 42 55 54 45 5f 48 49 44 44 45 4e 0a 20  TRIBUTE_HIDDEN. 
fdbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fdbe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20                | 
fdbf0 46 49 4c 45 5f 46 4c 41 47 5f 44 45 4c 45 54 45  FILE_FLAG_DELETE
fdc00 5f 4f 4e 5f 43 4c 4f 53 45 3b 0a 23 65 6e 64 69  _ON_CLOSE;.#endi
fdc10 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64  f.  }else{.    d
fdc20 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75  wFlagsAndAttribu
fdc30 74 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49  tes = FILE_ATTRI
fdc40 42 55 54 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 7d  BUTE_NORMAL;.  }
fdc50 0a 20 20 2f 2a 20 52 65 70 6f 72 74 73 20 66 72  .  /* Reports fr
fdc60 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 65 74 20  om the internet 
fdc70 61 72 65 20 74 68 61 74 20 70 65 72 66 6f 72 6d  are that perform
fdc80 61 6e 63 65 20 69 73 20 61 6c 77 61 79 73 0a 20  ance is always. 
fdc90 20 2a 2a 20 62 65 74 74 65 72 20 69 66 20 46 49   ** better if FI
fdca0 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41  LE_FLAG_RANDOM_A
fdcb0 43 43 45 53 53 20 69 73 20 75 73 65 64 2e 20 20  CCESS is used.  
fdcc0 54 69 63 6b 65 74 20 23 32 36 39 39 2e 20 2a 2f  Ticket #2699. */
fdcd0 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
fdce0 49 4e 43 45 0a 20 20 64 77 46 6c 61 67 73 41 6e  INCE.  dwFlagsAn
fdcf0 64 41 74 74 72 69 62 75 74 65 73 20 7c 3d 20 46  dAttributes |= F
fdd00 49 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f  ILE_FLAG_RANDOM_
fdd10 41 43 43 45 53 53 3b 0a 23 65 6e 64 69 66 0a 20  ACCESS;.#endif. 
fdd20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20   if( isNT() ){. 
fdd30 20 20 20 68 20 3d 20 43 72 65 61 74 65 46 69 6c     h = CreateFil
fdd40 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76  eW((WCHAR*)zConv
fdd50 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20 64 77  erted,.       dw
fdd60 44 65 73 69 72 65 64 41 63 63 65 73 73 2c 0a 20  DesiredAccess,. 
fdd70 20 20 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64        dwShareMod
fdd80 65 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a  e,.       NULL,.
fdd90 20 20 20 20 20 20 20 64 77 43 72 65 61 74 69 6f         dwCreatio
fdda0 6e 44 69 73 70 6f 73 69 74 69 6f 6e 2c 0a 20 20  nDisposition,.  
fddb0 20 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41       dwFlagsAndA
fddc0 74 74 72 69 62 75 74 65 73 2c 0a 20 20 20 20 20  ttributes,.     
fddd0 20 20 4e 55 4c 4c 0a 20 20 20 20 29 3b 0a 2f 2a    NULL.    );./*
fdde0 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20   isNT() is 1 if 
fddf0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d  SQLITE_OS_WINCE=
fde00 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65  =1, so this else
fde10 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74   is never execut
fde20 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68  ed. .** Since th
fde30 65 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20  e ASCII version 
fde40 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73  of these Windows
fde50 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73   API do not exis
fde60 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20  t for WINCE,.** 
fde70 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74  it's important t
fde80 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20  o not reference 
fde90 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62  them for WINCE b
fdea0 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51  uilds..*/.#if SQ
fdeb0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30  LITE_OS_WINCE==0
fdec0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20  .  }else{.    h 
fded0 3d 20 43 72 65 61 74 65 46 69 6c 65 41 28 28 63  = CreateFileA((c
fdee0 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c  har*)zConverted,
fdef0 0a 20 20 20 20 20 20 20 64 77 44 65 73 69 72 65  .       dwDesire
fdf00 64 41 63 63 65 73 73 2c 0a 20 20 20 20 20 20 20  dAccess,.       
fdf10 64 77 53 68 61 72 65 4d 6f 64 65 2c 0a 20 20 20  dwShareMode,.   
fdf20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20      NULL,.      
fdf30 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f   dwCreationDispo
fdf40 73 69 74 69 6f 6e 2c 0a 20 20 20 20 20 20 20 64  sition,.       d
fdf50 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75  wFlagsAndAttribu
fdf60 74 65 73 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c  tes,.       NULL
fdf70 0a 20 20 20 20 29 3b 0a 23 65 6e 64 69 66 0a 20  .    );.#endif. 
fdf80 20 7d 0a 20 20 69 66 28 20 68 3d 3d 49 4e 56 41   }.  if( h==INVA
fdf90 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45  LID_HANDLE_VALUE
fdfa0 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f   ){.    free(zCo
fdfb0 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 69 66  nverted);.    if
fdfc0 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ( flags & SQLITE
fdfd0 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20  _OPEN_READWRITE 
fdfe0 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
fdff0 77 69 6e 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4e  winOpen(pVfs, zN
fe000 61 6d 65 2c 20 69 64 2c 20 0a 20 20 20 20 20 20  ame, id, .      
fe010 20 20 20 20 20 20 20 28 28 66 6c 61 67 73 7c 53         ((flags|S
fe020 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
fe030 4e 4c 59 29 26 7e 53 51 4c 49 54 45 5f 4f 50 45  NLY)&~SQLITE_OPE
fe040 4e 5f 52 45 41 44 57 52 49 54 45 29 2c 20 70 4f  N_READWRITE), pO
fe050 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 20 7d 65  utFlags);.    }e
fe060 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72  lse{.      retur
fe070 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45  n SQLITE_CANTOPE
fe080 4e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  N;.    }.  }.  i
fe090 66 28 20 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a  f( pOutFlags ){.
fe0a0 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20      if( flags & 
fe0b0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
fe0c0 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20 2a  WRITE ){.      *
fe0d0 70 4f 75 74 46 6c 61 67 73 20 3d 20 53 51 4c 49  pOutFlags = SQLI
fe0e0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
fe0f0 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  E;.    }else{.  
fe100 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d      *pOutFlags =
fe110 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
fe120 44 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 7d  DONLY;.    }.  }
fe130 0a 20 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c  .  memset(pFile,
fe140 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69 6c   0, sizeof(*pFil
fe150 65 29 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 70 4d  e));.  pFile->pM
fe160 65 74 68 6f 64 20 3d 20 26 77 69 6e 49 6f 4d 65  ethod = &winIoMe
fe170 74 68 6f 64 3b 0a 20 20 70 46 69 6c 65 2d 3e 68  thod;.  pFile->h
fe180 20 3d 20 68 3b 0a 20 20 70 46 69 6c 65 2d 3e 6c   = h;.  pFile->l
fe190 61 73 74 45 72 72 6e 6f 20 3d 20 4e 4f 5f 45 52  astErrno = NO_ER
fe1a0 52 4f 52 3b 0a 20 20 70 46 69 6c 65 2d 3e 73 65  ROR;.  pFile->se
fe1b0 63 74 6f 72 53 69 7a 65 20 3d 20 67 65 74 53 65  ctorSize = getSe
fe1c0 63 74 6f 72 53 69 7a 65 28 70 56 66 73 2c 20 7a  ctorSize(pVfs, z
fe1d0 55 74 66 38 4e 61 6d 65 29 3b 0a 23 69 66 20 53  Utf8Name);.#if S
fe1e0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20  QLITE_OS_WINCE. 
fe1f0 20 69 66 28 20 28 66 6c 61 67 73 20 26 20 28 53   if( (flags & (S
fe200 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
fe210 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e  RITE|SQLITE_OPEN
fe220 5f 4d 41 49 4e 5f 44 42 29 29 20 3d 3d 0a 20 20  _MAIN_DB)) ==.  
fe230 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51               (SQ
fe240 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
fe250 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ITE|SQLITE_OPEN_
fe260 4d 41 49 4e 5f 44 42 29 0a 20 20 20 20 20 20 20  MAIN_DB).       
fe270 26 26 20 21 77 69 6e 63 65 43 72 65 61 74 65 4c  && !winceCreateL
fe280 6f 63 6b 28 7a 4e 61 6d 65 2c 20 70 46 69 6c 65  ock(zName, pFile
fe290 29 0a 20 20 29 7b 0a 20 20 20 20 43 6c 6f 73 65  ).  ){.    Close
fe2a0 48 61 6e 64 6c 65 28 68 29 3b 0a 20 20 20 20 66  Handle(h);.    f
fe2b0 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ree(zConverted);
fe2c0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
fe2d0 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d  TE_CANTOPEN;.  }
fe2e0 0a 20 20 69 66 28 20 69 73 54 65 6d 70 20 29 7b  .  if( isTemp ){
fe2f0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 7a 44 65 6c  .    pFile->zDel
fe300 65 74 65 4f 6e 43 6c 6f 73 65 20 3d 20 7a 43 6f  eteOnClose = zCo
fe310 6e 76 65 72 74 65 64 3b 0a 20 20 7d 65 6c 73 65  nverted;.  }else
fe320 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20  .#endif.  {.    
fe330 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29  free(zConverted)
fe340 3b 0a 20 20 7d 0a 20 20 4f 70 65 6e 43 6f 75 6e  ;.  }.  OpenCoun
fe350 74 65 72 28 2b 31 29 3b 0a 20 20 72 65 74 75 72  ter(+1);.  retur
fe360 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
fe370 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65  /*.** Delete the
fe380 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a   named file..**.
fe390 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 69 6e  ** Note that win
fe3a0 64 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 61 6c  dows does not al
fe3b0 6c 6f 77 20 61 20 66 69 6c 65 20 74 6f 20 62 65  low a file to be
fe3c0 20 64 65 6c 65 74 65 64 20 69 66 20 73 6f 6d 65   deleted if some
fe3d0 20 6f 74 68 65 72 0a 2a 2a 20 70 72 6f 63 65 73   other.** proces
fe3e0 73 20 68 61 73 20 69 74 20 6f 70 65 6e 2e 20 20  s has it open.  
fe3f0 53 6f 6d 65 74 69 6d 65 73 20 61 20 76 69 72 75  Sometimes a viru
fe400 73 20 73 63 61 6e 6e 65 72 20 6f 72 20 69 6e 64  s scanner or ind
fe410 65 78 69 6e 67 20 70 72 6f 67 72 61 6d 0a 2a 2a  exing program.**
fe420 20 77 69 6c 6c 20 6f 70 65 6e 20 61 20 6a 6f 75   will open a jou
fe430 72 6e 61 6c 20 66 69 6c 65 20 73 68 6f 72 74 6c  rnal file shortl
fe440 79 20 61 66 74 65 72 20 69 74 20 69 73 20 63 72  y after it is cr
fe450 65 61 74 65 64 20 69 6e 20 6f 72 64 65 72 20 74  eated in order t
fe460 6f 20 64 6f 0a 2a 2a 20 77 68 61 74 65 76 65 72  o do.** whatever
fe470 20 69 74 20 64 6f 65 73 2e 20 20 57 68 69 6c 65   it does.  While
fe480 20 74 68 69 73 20 6f 74 68 65 72 20 70 72 6f 63   this other proc
fe490 65 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 74  ess is holding t
fe4a0 68 65 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 6e 2c  he.** file open,
fe4b0 20 77 65 20 77 69 6c 6c 20 62 65 20 75 6e 61 62   we will be unab
fe4c0 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 69 74 2e  le to delete it.
fe4d0 20 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64    To work around
fe4e0 20 74 68 69 73 0a 2a 2a 20 70 72 6f 62 6c 65 6d   this.** problem
fe4f0 2c 20 77 65 20 64 65 6c 61 79 20 31 30 30 20 6d  , we delay 100 m
fe500 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20  illiseconds and 
fe510 74 72 79 20 74 6f 20 64 65 6c 65 74 65 20 61 67  try to delete ag
fe520 61 69 6e 2e 20 20 55 70 0a 2a 2a 20 74 6f 20 4d  ain.  Up.** to M
fe530 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d  X_DELETION_ATTEM
fe540 50 54 73 20 64 65 6c 65 74 69 6f 6e 20 61 74 74  PTs deletion att
fe550 65 6d 70 74 73 20 61 72 65 20 72 75 6e 20 62 65  empts are run be
fe560 66 6f 72 65 20 67 69 76 69 6e 67 0a 2a 2a 20 75  fore giving.** u
fe570 70 20 61 6e 64 20 72 65 74 75 72 6e 69 6e 67 20  p and returning 
fe580 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23 64 65  an error..*/.#de
fe590 66 69 6e 65 20 4d 58 5f 44 45 4c 45 54 49 4f 4e  fine MX_DELETION
fe5a0 5f 41 54 54 45 4d 50 54 53 20 35 0a 73 74 61 74  _ATTEMPTS 5.stat
fe5b0 69 63 20 69 6e 74 20 77 69 6e 44 65 6c 65 74 65  ic int winDelete
fe5c0 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  (.  sqlite3_vfs 
fe5d0 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20  *pVfs,          
fe5e0 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77  /* Not used on w
fe5f0 69 6e 33 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  in32 */.  const 
fe600 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  char *zFilename,
fe610 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
fe620 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 65 20   file to delete 
fe630 2a 2f 0a 20 20 69 6e 74 20 73 79 6e 63 44 69 72  */.  int syncDir
fe640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe650 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20   /* Not used on 
fe660 77 69 6e 33 32 20 2a 2f 0a 29 7b 0a 20 20 69 6e  win32 */.){.  in
fe670 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 44 57 4f  t cnt = 0;.  DWO
fe680 52 44 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 65  RD rc;.  DWORD e
fe690 72 72 6f 72 20 3d 20 30 3b 0a 20 20 76 6f 69 64  rror = 0;.  void
fe6a0 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63   *zConverted = c
fe6b0 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61  onvertUtf8Filena
fe6c0 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  me(zFilename);. 
fe6d0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
fe6e0 52 28 70 56 66 73 29 3b 0a 20 20 55 4e 55 53 45  R(pVfs);.  UNUSE
fe6f0 44 5f 50 41 52 41 4d 45 54 45 52 28 73 79 6e 63  D_PARAMETER(sync
fe700 44 69 72 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e  Dir);.  if( zCon
fe710 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20  verted==0 ){.   
fe720 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
fe730 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75  OMEM;.  }.  Simu
fe740 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75  lateIOError(retu
fe750 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
fe760 44 45 4c 45 54 45 29 3b 0a 20 20 69 66 28 20 69  DELETE);.  if( i
fe770 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 64 6f 7b  sNT() ){.    do{
fe780 0a 20 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c  .      DeleteFil
fe790 65 57 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  eW(zConverted);.
fe7a0 20 20 20 20 7d 77 68 69 6c 65 28 20 20 20 28 20      }while(   ( 
fe7b0 20 20 28 28 72 63 20 3d 20 47 65 74 46 69 6c 65    ((rc = GetFile
fe7c0 41 74 74 72 69 62 75 74 65 73 57 28 7a 43 6f 6e  AttributesW(zCon
fe7d0 76 65 72 74 65 64 29 29 20 21 3d 20 49 4e 56 41  verted)) != INVA
fe7e0 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55  LID_FILE_ATTRIBU
fe7f0 54 45 53 29 0a 20 20 20 20 20 20 20 20 20 20 20  TES).           
fe800 20 20 20 20 7c 7c 20 28 28 65 72 72 6f 72 20 3d      || ((error =
fe810 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29   GetLastError())
fe820 20 3d 3d 20 45 52 52 4f 52 5f 41 43 43 45 53 53   == ERROR_ACCESS
fe830 5f 44 45 4e 49 45 44 29 29 0a 20 20 20 20 20 20  _DENIED)).      
fe840 20 20 20 20 20 26 26 20 28 2b 2b 63 6e 74 20 3c       && (++cnt <
fe850 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54   MX_DELETION_ATT
fe860 45 4d 50 54 53 29 0a 20 20 20 20 20 20 20 20 20  EMPTS).         
fe870 20 20 26 26 20 28 53 6c 65 65 70 28 31 30 30 29    && (Sleep(100)
fe880 2c 20 31 29 20 29 3b 0a 2f 2a 20 69 73 4e 54 28  , 1) );./* isNT(
fe890 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45  ) is 1 if SQLITE
fe8a0 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f  _OS_WINCE==1, so
fe8b0 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65   this else is ne
fe8c0 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a  ver executed. .*
fe8d0 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49  * Since the ASCI
fe8e0 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  I version of the
fe8f0 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64  se Windows API d
fe900 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20  o not exist for 
fe910 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69  WINCE,.** it's i
fe920 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20  mportant to not 
fe930 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66  reference them f
fe940 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e  or WINCE builds.
fe950 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  .*/.#if SQLITE_O
fe960 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c  S_WINCE==0.  }el
fe970 73 65 7b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20  se{.    do{.    
fe980 20 20 44 65 6c 65 74 65 46 69 6c 65 41 28 7a 43    DeleteFileA(zC
fe990 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7d  onverted);.    }
fe9a0 77 68 69 6c 65 28 20 20 20 28 20 20 20 28 28 72  while(   (   ((r
fe9b0 63 20 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69  c = GetFileAttri
fe9c0 62 75 74 65 73 41 28 7a 43 6f 6e 76 65 72 74 65  butesA(zConverte
fe9d0 64 29 29 20 21 3d 20 49 4e 56 41 4c 49 44 5f 46  d)) != INVALID_F
fe9e0 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 29 0a  ILE_ATTRIBUTES).
fe9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c                 |
fea00 7c 20 28 28 65 72 72 6f 72 20 3d 20 47 65 74 4c  | ((error = GetL
fea10 61 73 74 45 72 72 6f 72 28 29 29 20 3d 3d 20 45  astError()) == E
fea20 52 52 4f 52 5f 41 43 43 45 53 53 5f 44 45 4e 49  RROR_ACCESS_DENI
fea30 45 44 29 29 0a 20 20 20 20 20 20 20 20 20 20 20  ED)).           
fea40 26 26 20 28 2b 2b 63 6e 74 20 3c 20 4d 58 5f 44  && (++cnt < MX_D
fea50 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53  ELETION_ATTEMPTS
fea60 29 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20  ).           && 
fea70 28 53 6c 65 65 70 28 31 30 30 29 2c 20 31 29 20  (Sleep(100), 1) 
fea80 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20  );.#endif.  }.  
fea90 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29  free(zConverted)
feaa0 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22 44 45  ;.  OSTRACE2("DE
feab0 4c 45 54 45 20 5c 22 25 73 5c 22 5c 6e 22 2c 20  LETE \"%s\"\n", 
feac0 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 65  zFilename);.  re
fead0 74 75 72 6e 20 28 20 20 20 28 72 63 20 3d 3d 20  turn (   (rc == 
feae0 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54  INVALID_FILE_ATT
feaf0 52 49 42 55 54 45 53 29 20 0a 20 20 20 20 20 20  RIBUTES) .      
feb00 20 20 20 20 26 26 20 28 65 72 72 6f 72 20 3d 3d      && (error ==
feb10 20 45 52 52 4f 52 5f 46 49 4c 45 5f 4e 4f 54 5f   ERROR_FILE_NOT_
feb20 46 4f 55 4e 44 29 29 20 3f 20 53 51 4c 49 54 45  FOUND)) ? SQLITE
feb30 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45  _OK : SQLITE_IOE
feb40 52 52 5f 44 45 4c 45 54 45 3b 0a 7d 0a 0a 2f 2a  RR_DELETE;.}../*
feb50 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 65 78  .** Check the ex
feb60 69 73 74 61 6e 63 65 20 61 6e 64 20 73 74 61 74  istance and stat
feb70 75 73 20 6f 66 20 61 20 66 69 6c 65 2e 0a 2a 2f  us of a file..*/
feb80 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 41  .static int winA
feb90 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33  ccess(.  sqlite3
feba0 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
febb0 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20      /* Not used 
febc0 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 20 63 6f  on win32 */.  co
febd0 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
febe0 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ame,     /* Name
febf0 20 6f 66 20 66 69 6c 65 20 74 6f 20 63 68 65 63   of file to chec
fec00 6b 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  k */.  int flags
fec10 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
fec20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 74 65 73    /* Type of tes
fec30 74 20 74 6f 20 6d 61 6b 65 20 6f 6e 20 74 68 69  t to make on thi
fec40 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20  s file */.  int 
fec50 2a 70 52 65 73 4f 75 74 20 20 20 20 20 20 20 20  *pResOut        
fec60 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52         /* OUT: R
fec70 65 73 75 6c 74 20 2a 2f 0a 29 7b 0a 20 20 44 57  esult */.){.  DW
fec80 4f 52 44 20 61 74 74 72 3b 0a 20 20 69 6e 74 20  ORD attr;.  int 
fec90 72 63 20 3d 20 30 3b 0a 20 20 76 6f 69 64 20 2a  rc = 0;.  void *
feca0 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e  zConverted = con
fecb0 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65  vertUtf8Filename
fecc0 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55  (zFilename);.  U
fecd0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
fece0 70 56 66 73 29 3b 0a 20 20 69 66 28 20 7a 43 6f  pVfs);.  if( zCo
fecf0 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20  nverted==0 ){.  
fed00 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
fed10 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28  NOMEM;.  }.  if(
fed20 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 61   isNT() ){.    a
fed30 74 74 72 20 3d 20 47 65 74 46 69 6c 65 41 74 74  ttr = GetFileAtt
fed40 72 69 62 75 74 65 73 57 28 28 57 43 48 41 52 2a  ributesW((WCHAR*
fed50 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 2f 2a  )zConverted);./*
fed60 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20   isNT() is 1 if 
fed70 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d  SQLITE_OS_WINCE=
fed80 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65  =1, so this else
fed90 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74   is never execut
feda0 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68  ed. .** Since th
fedb0 65 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20  e ASCII version 
fedc0 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73  of these Windows
fedd0 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73   API do not exis
fede0 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20  t for WINCE,.** 
fedf0 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74  it's important t
fee00 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20  o not reference 
fee10 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62  them for WINCE b
fee20 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51  uilds..*/.#if SQ
fee30 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30  LITE_OS_WINCE==0
fee40 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 74  .  }else{.    at
fee50 74 72 20 3d 20 47 65 74 46 69 6c 65 41 74 74 72  tr = GetFileAttr
fee60 69 62 75 74 65 73 41 28 28 63 68 61 72 2a 29 7a  ibutesA((char*)z
fee70 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23 65 6e 64  Converted);.#end
fee80 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a 43  if.  }.  free(zC
fee90 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 73 77 69  onverted);.  swi
feea0 74 63 68 28 20 66 6c 61 67 73 20 29 7b 0a 20 20  tch( flags ){.  
feeb0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43    case SQLITE_AC
feec0 43 45 53 53 5f 52 45 41 44 3a 0a 20 20 20 20 63  CESS_READ:.    c
feed0 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53  ase SQLITE_ACCES
feee0 53 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20  S_EXISTS:.      
feef0 72 63 20 3d 20 61 74 74 72 21 3d 49 4e 56 41 4c  rc = attr!=INVAL
fef00 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54  ID_FILE_ATTRIBUT
fef10 45 53 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ES;.      break;
fef20 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
fef30 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54  _ACCESS_READWRIT
fef40 45 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 28 61  E:.      rc = (a
fef50 74 74 72 20 26 20 46 49 4c 45 5f 41 54 54 52 49  ttr & FILE_ATTRI
fef60 42 55 54 45 5f 52 45 41 44 4f 4e 4c 59 29 3d 3d  BUTE_READONLY)==
fef70 30 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  0;.      break;.
fef80 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20      default:.   
fef90 20 20 20 61 73 73 65 72 74 28 21 22 49 6e 76 61     assert(!"Inva
fefa0 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d 65  lid flags argume
fefb0 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65  nt");.  }.  *pRe
fefc0 73 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74  sOut = rc;.  ret
fefd0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
fefe0 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20  .../*.** Turn a 
feff0 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d  relative pathnam
ff000 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61  e into a full pa
ff010 74 68 6e 61 6d 65 2e 20 20 57 72 69 74 65 20 74  thname.  Write t
ff020 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 61 74 68 6e  he full.** pathn
ff030 61 6d 65 20 69 6e 74 6f 20 7a 4f 75 74 5b 5d 2e  ame into zOut[].
ff040 20 20 7a 4f 75 74 5b 5d 20 77 69 6c 6c 20 62 65    zOut[] will be
ff050 20 61 74 20 6c 65 61 73 74 20 70 56 66 73 2d 3e   at least pVfs->
ff060 6d 78 50 61 74 68 6e 61 6d 65 0a 2a 2a 20 62 79  mxPathname.** by
ff070 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a  tes in size..*/.
ff080 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 46 75  static int winFu
ff090 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71  llPathname(.  sq
ff0a0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
ff0b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
ff0c0 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62  ointer to vfs ob
ff0d0 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ject */.  const 
ff0e0 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 2c  char *zRelative,
ff0f0 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69          /* Possi
ff100 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70  bly relative inp
ff110 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74  ut path */.  int
ff120 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20 20 20   nFull,         
ff130 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
ff140 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66  ze of output buf
ff150 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  fer in bytes */.
ff160 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20    char *zFull   
ff170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff180 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65 72  /* Output buffer
ff190 20 2a 2f 0a 29 7b 0a 20 20 0a 23 69 66 20 64 65   */.){.  .#if de
ff1a0 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f  fined(__CYGWIN__
ff1b0 29 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ).  UNUSED_PARAM
ff1c0 45 54 45 52 28 6e 46 75 6c 6c 29 3b 0a 20 20 63  ETER(nFull);.  c
ff1d0 79 67 77 69 6e 5f 63 6f 6e 76 5f 74 6f 5f 66 75  ygwin_conv_to_fu
ff1e0 6c 6c 5f 77 69 6e 33 32 5f 70 61 74 68 28 7a 52  ll_win32_path(zR
ff1f0 65 6c 61 74 69 76 65 2c 20 7a 46 75 6c 6c 29 3b  elative, zFull);
ff200 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
ff210 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66  _OK;.#endif..#if
ff220 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
ff230 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
ff240 54 45 52 28 6e 46 75 6c 6c 29 3b 0a 20 20 2f 2a  TER(nFull);.  /*
ff250 20 57 69 6e 43 45 20 68 61 73 20 6e 6f 20 63 6f   WinCE has no co
ff260 6e 63 65 70 74 20 6f 66 20 61 20 72 65 6c 61 74  ncept of a relat
ff270 69 76 65 20 70 61 74 68 6e 61 6d 65 2c 20 6f 72  ive pathname, or
ff280 20 73 6f 20 49 20 61 6d 20 74 6f 6c 64 2e 20 2a   so I am told. *
ff290 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72  /.  sqlite3_snpr
ff2a0 69 6e 74 66 28 70 56 66 73 2d 3e 6d 78 50 61 74  intf(pVfs->mxPat
ff2b0 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22 25  hname, zFull, "%
ff2c0 73 22 2c 20 7a 52 65 6c 61 74 69 76 65 29 3b 0a  s", zRelative);.
ff2d0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
ff2e0 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20  OK;.#endif..#if 
ff2f0 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45  !SQLITE_OS_WINCE
ff300 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f 43   && !defined(__C
ff310 59 47 57 49 4e 5f 5f 29 0a 20 20 69 6e 74 20 6e  YGWIN__).  int n
ff320 42 79 74 65 3b 0a 20 20 76 6f 69 64 20 2a 7a 43  Byte;.  void *zC
ff330 6f 6e 76 65 72 74 65 64 3b 0a 20 20 63 68 61 72  onverted;.  char
ff340 20 2a 7a 4f 75 74 3b 0a 20 20 55 4e 55 53 45 44   *zOut;.  UNUSED
ff350 5f 50 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c  _PARAMETER(nFull
ff360 29 3b 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 20  );.  zConverted 
ff370 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c  = convertUtf8Fil
ff380 65 6e 61 6d 65 28 7a 52 65 6c 61 74 69 76 65 29  ename(zRelative)
ff390 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29  ;.  if( isNT() )
ff3a0 7b 0a 20 20 20 20 57 43 48 41 52 20 2a 7a 54 65  {.    WCHAR *zTe
ff3b0 6d 70 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20  mp;.    nByte = 
ff3c0 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 57  GetFullPathNameW
ff3d0 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72  ((WCHAR*)zConver
ff3e0 74 65 64 2c 20 30 2c 20 30 2c 20 30 29 20 2b 20  ted, 0, 0, 0) + 
ff3f0 33 3b 0a 20 20 20 20 7a 54 65 6d 70 20 3d 20 6d  3;.    zTemp = m
ff400 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a  alloc( nByte*siz
ff410 65 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20 29 3b  eof(zTemp[0]) );
ff420 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70 3d 3d  .    if( zTemp==
ff430 30 20 29 7b 0a 20 20 20 20 20 20 66 72 65 65 28  0 ){.      free(
ff440 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20  zConverted);.   
ff450 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
ff460 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
ff470 20 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d    GetFullPathNam
ff480 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76  eW((WCHAR*)zConv
ff490 65 72 74 65 64 2c 20 6e 42 79 74 65 2c 20 7a 54  erted, nByte, zT
ff4a0 65 6d 70 2c 20 30 29 3b 0a 20 20 20 20 66 72 65  emp, 0);.    fre
ff4b0 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  e(zConverted);. 
ff4c0 20 20 20 7a 4f 75 74 20 3d 20 75 6e 69 63 6f 64     zOut = unicod
ff4d0 65 54 6f 55 74 66 38 28 7a 54 65 6d 70 29 3b 0a  eToUtf8(zTemp);.
ff4e0 20 20 20 20 66 72 65 65 28 7a 54 65 6d 70 29 3b      free(zTemp);
ff4f0 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20  ./* isNT() is 1 
ff500 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
ff510 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65  CE==1, so this e
ff520 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65  lse is never exe
ff530 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65  cuted. .** Since
ff540 20 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69   the ASCII versi
ff550 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64  on of these Wind
ff560 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65  ows API do not e
ff570 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a  xist for WINCE,.
ff580 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e  ** it's importan
ff590 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e  t to not referen
ff5a0 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43  ce them for WINC
ff5b0 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66  E builds..*/.#if
ff5c0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
ff5d0 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ==0.  }else{.   
ff5e0 20 63 68 61 72 20 2a 7a 54 65 6d 70 3b 0a 20 20   char *zTemp;.  
ff5f0 20 20 6e 42 79 74 65 20 3d 20 47 65 74 46 75 6c    nByte = GetFul
ff600 6c 50 61 74 68 4e 61 6d 65 41 28 28 63 68 61 72  lPathNameA((char
ff610 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 30 2c  *)zConverted, 0,
ff620 20 30 2c 20 30 29 20 2b 20 33 3b 0a 20 20 20 20   0, 0) + 3;.    
ff630 7a 54 65 6d 70 20 3d 20 6d 61 6c 6c 6f 63 28 20  zTemp = malloc( 
ff640 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 54 65  nByte*sizeof(zTe
ff650 6d 70 5b 30 5d 29 20 29 3b 0a 20 20 20 20 69 66  mp[0]) );.    if
ff660 28 20 7a 54 65 6d 70 3d 3d 30 20 29 7b 0a 20 20  ( zTemp==0 ){.  
ff670 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72      free(zConver
ff680 74 65 64 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ted);.      retu
ff690 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
ff6a0 0a 20 20 20 20 7d 0a 20 20 20 20 47 65 74 46 75  .    }.    GetFu
ff6b0 6c 6c 50 61 74 68 4e 61 6d 65 41 28 28 63 68 61  llPathNameA((cha
ff6c0 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 6e  r*)zConverted, n
ff6d0 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20 30 29 3b  Byte, zTemp, 0);
ff6e0 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65  .    free(zConve
ff6f0 72 74 65 64 29 3b 0a 20 20 20 20 7a 4f 75 74 20  rted);.    zOut 
ff700 3d 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f  = sqlite3_win32_
ff710 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a 54 65  mbcs_to_utf8(zTe
ff720 6d 70 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 54  mp);.    free(zT
ff730 65 6d 70 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  emp);.#endif.  }
ff740 0a 20 20 69 66 28 20 7a 4f 75 74 20 29 7b 0a 20  .  if( zOut ){. 
ff750 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
ff760 6e 74 66 28 70 56 66 73 2d 3e 6d 78 50 61 74 68  ntf(pVfs->mxPath
ff770 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22 25 73  name, zFull, "%s
ff780 22 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 20 66 72  ", zOut);.    fr
ff790 65 65 28 7a 4f 75 74 29 3b 0a 20 20 20 20 72 65  ee(zOut);.    re
ff7a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
ff7b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
ff7c0 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
ff7d0 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a  ;.  }.#endif.}..
ff7e0 2f 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20 73 65  /*.** Get the se
ff7f0 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 74 68 65  ctor size of the
ff800 20 64 65 76 69 63 65 20 75 73 65 64 20 74 6f 20   device used to 
ff810 73 74 6f 72 65 0a 2a 2a 20 66 69 6c 65 2e 0a 2a  store.** file..*
ff820 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
ff830 53 65 63 74 6f 72 53 69 7a 65 28 0a 20 20 20 20  SectorSize(.    
ff840 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
ff850 73 2c 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  s,.    const cha
ff860 72 20 2a 7a 52 65 6c 61 74 69 76 65 20 20 20 20  r *zRelative    
ff870 20 2f 2a 20 55 54 46 2d 38 20 66 69 6c 65 20 6e   /* UTF-8 file n
ff880 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20 44 57 4f 52  ame */.){.  DWOR
ff890 44 20 62 79 74 65 73 50 65 72 53 65 63 74 6f 72  D bytesPerSector
ff8a0 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c   = SQLITE_DEFAUL
ff8b0 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20  T_SECTOR_SIZE;. 
ff8c0 20 2f 2a 20 47 65 74 44 69 73 6b 46 72 65 65 53   /* GetDiskFreeS
ff8d0 70 61 63 65 20 69 73 20 6e 6f 74 20 73 75 70 70  pace is not supp
ff8e0 6f 72 74 65 64 20 75 6e 64 65 72 20 57 49 4e 43  orted under WINC
ff8f0 45 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  E */.#if SQLITE_
ff900 4f 53 5f 57 49 4e 43 45 0a 20 20 55 4e 55 53 45  OS_WINCE.  UNUSE
ff910 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73  D_PARAMETER(pVfs
ff920 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
ff930 4d 45 54 45 52 28 7a 52 65 6c 61 74 69 76 65 29  METER(zRelative)
ff940 3b 0a 23 65 6c 73 65 0a 20 20 63 68 61 72 20 7a  ;.#else.  char z
ff950 46 75 6c 6c 70 61 74 68 5b 4d 41 58 5f 50 41 54  Fullpath[MAX_PAT
ff960 48 2b 31 5d 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  H+1];.  int rc;.
ff970 20 20 44 57 4f 52 44 20 64 77 52 65 74 20 3d 20    DWORD dwRet = 
ff980 30 3b 0a 20 20 44 57 4f 52 44 20 64 77 44 75 6d  0;.  DWORD dwDum
ff990 6d 79 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 57  my;..  /*.  ** W
ff9a0 65 20 6e 65 65 64 20 74 6f 20 67 65 74 20 74 68  e need to get th
ff9b0 65 20 66 75 6c 6c 20 70 61 74 68 20 6e 61 6d 65  e full path name
ff9c0 20 6f 66 20 74 68 65 20 66 69 6c 65 0a 20 20 2a   of the file.  *
ff9d0 2a 20 74 6f 20 67 65 74 20 74 68 65 20 64 72 69  * to get the dri
ff9e0 76 65 20 6c 65 74 74 65 72 20 74 6f 20 6c 6f 6f  ve letter to loo
ff9f0 6b 20 75 70 20 74 68 65 20 73 65 63 74 6f 72 0a  k up the sector.
ffa00 20 20 2a 2a 20 73 69 7a 65 2e 0a 20 20 2a 2f 0a    ** size..  */.
ffa10 20 20 72 63 20 3d 20 77 69 6e 46 75 6c 6c 50 61    rc = winFullPa
ffa20 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 52 65  thname(pVfs, zRe
ffa30 6c 61 74 69 76 65 2c 20 4d 41 58 5f 50 41 54 48  lative, MAX_PATH
ffa40 2c 20 7a 46 75 6c 6c 70 61 74 68 29 3b 0a 20 20  , zFullpath);.  
ffa50 69 66 28 20 72 63 20 3d 3d 20 53 51 4c 49 54 45  if( rc == SQLITE
ffa60 5f 4f 4b 20 29 0a 20 20 7b 0a 20 20 20 20 76 6f  _OK ).  {.    vo
ffa70 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d  id *zConverted =
ffa80 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65   convertUtf8File
ffa90 6e 61 6d 65 28 7a 46 75 6c 6c 70 61 74 68 29 3b  name(zFullpath);
ffaa0 0a 20 20 20 20 69 66 28 20 7a 43 6f 6e 76 65 72  .    if( zConver
ffab0 74 65 64 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ted ){.      if(
ffac0 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 20   isNT() ){.     
ffad0 20 20 20 2f 2a 20 74 72 69 6d 20 70 61 74 68 20     /* trim path 
ffae0 74 6f 20 6a 75 73 74 20 64 72 69 76 65 20 72 65  to just drive re
ffaf0 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 20 20 20  ference */.     
ffb00 20 20 20 57 43 48 41 52 20 2a 70 20 3d 20 7a 43     WCHAR *p = zC
ffb10 6f 6e 76 65 72 74 65 64 3b 0a 20 20 20 20 20 20  onverted;.      
ffb20 20 20 66 6f 72 28 3b 2a 70 3b 70 2b 2b 29 7b 0a    for(;*p;p++){.
ffb30 20 20 20 20 20 20 20 20 20 20 69 66 28 20 2a 70            if( *p
ffb40 20 3d 3d 20 27 5c 5c 27 20 29 7b 0a 20 20 20 20   == '\\' ){.    
ffb50 20 20 20 20 20 20 20 20 2a 70 20 3d 20 27 5c 30          *p = '\0
ffb60 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  ';.            b
ffb70 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
ffb80 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
ffb90 20 20 20 20 64 77 52 65 74 20 3d 20 47 65 74 44      dwRet = GetD
ffba0 69 73 6b 46 72 65 65 53 70 61 63 65 57 28 28 57  iskFreeSpaceW((W
ffbb0 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64  CHAR*)zConverted
ffbc0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
ffbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ffbe0 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20 20      &dwDummy,.  
ffbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ffc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ffc10 26 62 79 74 65 73 50 65 72 53 65 63 74 6f 72 2c  &bytesPerSector,
ffc20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
ffc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ffc40 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20 20 20     &dwDummy,.   
ffc50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ffc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
ffc70 64 77 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20  dwDummy);.      
ffc80 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
ffc90 2a 20 74 72 69 6d 20 70 61 74 68 20 74 6f 20 6a  * trim path to j
ffca0 75 73 74 20 64 72 69 76 65 20 72 65 66 65 72 65  ust drive refere
ffcb0 6e 63 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 43  nce */.        C
ffcc0 48 41 52 20 2a 70 20 3d 20 28 43 48 41 52 20 2a  HAR *p = (CHAR *
ffcd0 29 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 20  )zConverted;.   
ffce0 20 20 20 20 20 66 6f 72 28 3b 2a 70 3b 70 2b 2b       for(;*p;p++
ffcf0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
ffd00 20 2a 70 20 3d 3d 20 27 5c 5c 27 20 29 7b 0a 20   *p == '\\' ){. 
ffd10 20 20 20 20 20 20 20 20 20 20 20 2a 70 20 3d 20             *p = 
ffd20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20  '\0';.          
ffd30 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
ffd40 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
ffd50 20 20 20 20 20 20 20 64 77 52 65 74 20 3d 20 47         dwRet = G
ffd60 65 74 44 69 73 6b 46 72 65 65 53 70 61 63 65 41  etDiskFreeSpaceA
ffd70 28 28 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74  ((CHAR*)zConvert
ffd80 65 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ed,.            
ffd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ffda0 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a        &dwDummy,.
ffdb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ffdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ffdd0 20 20 26 62 79 74 65 73 50 65 72 53 65 63 74 6f    &bytesPerSecto
ffde0 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
ffdf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ffe00 20 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20       &dwDummy,. 
ffe10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ffe20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ffe30 20 26 64 77 44 75 6d 6d 79 29 3b 0a 20 20 20 20   &dwDummy);.    
ffe40 20 20 7d 0a 20 20 20 20 20 20 66 72 65 65 28 7a    }.      free(z
ffe50 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20  Converted);.    
ffe60 7d 0a 20 20 20 20 69 66 28 20 21 64 77 52 65 74  }.    if( !dwRet
ffe70 20 29 7b 0a 20 20 20 20 20 20 62 79 74 65 73 50   ){.      bytesP
ffe80 65 72 53 65 63 74 6f 72 20 3d 20 53 51 4c 49 54  erSector = SQLIT
ffe90 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52  E_DEFAULT_SECTOR
ffea0 5f 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 20 20 7d  _SIZE;.    }.  }
ffeb0 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
ffec0 20 28 69 6e 74 29 20 62 79 74 65 73 50 65 72 53   (int) bytesPerS
ffed0 65 63 74 6f 72 3b 20 0a 7d 0a 0a 23 69 66 6e 64  ector; .}..#ifnd
ffee0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  ef SQLITE_OMIT_L
ffef0 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a  OAD_EXTENSION./*
fff00 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66  .** Interfaces f
fff10 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61  or opening a sha
fff20 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e  red library, fin
fff30 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74  ding entry point
fff40 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20  s.** within the 
fff50 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20  shared library, 
fff60 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20  and closing the 
fff70 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a  shared library..
fff80 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61  */./*.** Interfa
fff90 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20  ces for opening 
fffa0 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79  a shared library
fffb0 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20  , finding entry 
fffc0 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e  points.** within
fffd0 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
fffe0 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67  ary, and closing
ffff0 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
10000 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ary..*/.static v
10001 6f 69 64 20 2a 77 69 6e 44 6c 4f 70 65 6e 28 73  oid *winDlOpen(s
10002 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
10003 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
10004 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 48 41 4e 44  ilename){.  HAND
10005 4c 45 20 68 3b 0a 20 20 76 6f 69 64 20 2a 7a 43  LE h;.  void *zC
10006 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65  onverted = conve
10007 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a  rtUtf8Filename(z
10008 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55 4e 55  Filename);.  UNU
10009 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56  SED_PARAMETER(pV
1000a 66 73 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76  fs);.  if( zConv
1000b 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20  erted==0 ){.    
1000c 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
1000d 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20  if( isNT() ){.  
1000e 20 20 68 20 3d 20 4c 6f 61 64 4c 69 62 72 61 72    h = LoadLibrar
1000f 79 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76  yW((WCHAR*)zConv
10010 65 72 74 65 64 29 3b 0a 2f 2a 20 69 73 4e 54 28  erted);./* isNT(
10011 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45  ) is 1 if SQLITE
10012 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f  _OS_WINCE==1, so
10013 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65   this else is ne
10014 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a  ver executed. .*
10015 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49  * Since the ASCI
10016 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  I version of the
10017 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64  se Windows API d
10018 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20  o not exist for 
10019 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69  WINCE,.** it's i
1001a 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20  mportant to not 
1001b 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66  reference them f
1001c 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e  or WINCE builds.
1001d 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  .*/.#if SQLITE_O
1001e 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c  S_WINCE==0.  }el
1001f 73 65 7b 0a 20 20 20 20 68 20 3d 20 4c 6f 61 64  se{.    h = Load
10020 4c 69 62 72 61 72 79 41 28 28 63 68 61 72 2a 29  LibraryA((char*)
10021 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23 65 6e  zConverted);.#en
10022 64 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a  dif.  }.  free(z
10023 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 72 65  Converted);.  re
10024 74 75 72 6e 20 28 76 6f 69 64 2a 29 68 3b 0a 7d  turn (void*)h;.}
10025 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e  .static void win
10026 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f  DlError(sqlite3_
10027 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e  vfs *pVfs, int n
10028 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f  Buf, char *zBufO
10029 75 74 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  ut){.  UNUSED_PA
1002a 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20  RAMETER(pVfs);. 
1002b 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67   getLastErrorMsg
1002c 28 6e 42 75 66 2c 20 7a 42 75 66 4f 75 74 29 3b  (nBuf, zBufOut);
1002d 0a 7d 0a 76 6f 69 64 20 28 2a 77 69 6e 44 6c 53  .}.void (*winDlS
1002e 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  ym(sqlite3_vfs *
1002f 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e  pVfs, void *pHan
10030 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  dle, const char 
10031 2a 7a 53 79 6d 62 6f 6c 29 29 28 76 6f 69 64 29  *zSymbol))(void)
10032 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
10033 45 54 45 52 28 70 56 66 73 29 3b 0a 23 69 66 20  ETER(pVfs);.#if 
10034 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a  SQLITE_OS_WINCE.
10035 20 20 2f 2a 20 54 68 65 20 47 65 74 50 72 6f 63    /* The GetProc
10036 41 64 64 72 65 73 73 41 28 29 20 72 6f 75 74 69  AddressA() routi
10037 6e 65 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c  ne is only avail
10038 61 62 6c 65 20 6f 6e 20 77 69 6e 63 65 2e 20 2a  able on wince. *
10039 2f 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64  /.  return (void
1003a 28 2a 29 28 76 6f 69 64 29 29 47 65 74 50 72 6f  (*)(void))GetPro
1003b 63 41 64 64 72 65 73 73 41 28 28 48 41 4e 44 4c  cAddressA((HANDL
1003c 45 29 70 48 61 6e 64 6c 65 2c 20 7a 53 79 6d 62  E)pHandle, zSymb
1003d 6f 6c 29 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20  ol);.#else.  /* 
1003e 41 6c 6c 20 6f 74 68 65 72 20 77 69 6e 64 6f 77  All other window
1003f 73 20 70 6c 61 74 66 6f 72 6d 73 20 65 78 70 65  s platforms expe
10040 63 74 20 47 65 74 50 72 6f 63 41 64 64 72 65 73  ct GetProcAddres
10041 73 28 29 20 74 6f 20 74 61 6b 65 0a 20 20 2a 2a  s() to take.  **
10042 20 61 6e 20 41 6e 73 69 20 73 74 72 69 6e 67 20   an Ansi string 
10043 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68  regardless of th
10044 65 20 5f 55 4e 49 43 4f 44 45 20 73 65 74 74 69  e _UNICODE setti
10045 6e 67 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28  ng */.  return (
10046 76 6f 69 64 28 2a 29 28 76 6f 69 64 29 29 47 65  void(*)(void))Ge
10047 74 50 72 6f 63 41 64 64 72 65 73 73 28 28 48 41  tProcAddress((HA
10048 4e 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20 7a 53  NDLE)pHandle, zS
10049 79 6d 62 6f 6c 29 3b 0a 23 65 6e 64 69 66 0a 7d  ymbol);.#endif.}
1004a 0a 76 6f 69 64 20 77 69 6e 44 6c 43 6c 6f 73 65  .void winDlClose
1004b 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
1004c 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c  fs, void *pHandl
1004d 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  e){.  UNUSED_PAR
1004e 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20  AMETER(pVfs);.  
1004f 46 72 65 65 4c 69 62 72 61 72 79 28 28 48 41 4e  FreeLibrary((HAN
10050 44 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a  DLE)pHandle);.}.
10051 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c 49  #else /* if SQLI
10052 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
10053 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e 65  ENSION is define
10054 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20  d: */.  #define 
10055 77 69 6e 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23  winDlOpen  0.  #
10056 64 65 66 69 6e 65 20 77 69 6e 44 6c 45 72 72 6f  define winDlErro
10057 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69  r 0.  #define wi
10058 6e 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65  nDlSym   0.  #de
10059 66 69 6e 65 20 77 69 6e 44 6c 43 6c 6f 73 65 20  fine winDlClose 
1005a 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  0.#endif.../*.**
1005b 20 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75   Write up to nBu
1005c 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f  f bytes of rando
1005d 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e  mness into zBuf.
1005e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
1005f 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c  inRandomness(sql
10060 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
10061 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a  int nBuf, char *
10062 7a 42 75 66 29 7b 0a 20 20 69 6e 74 20 6e 20 3d  zBuf){.  int n =
10063 20 30 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52   0;.  UNUSED_PAR
10064 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 23 69  AMETER(pVfs);.#i
10065 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
10066 5f 54 45 53 54 29 0a 20 20 6e 20 3d 20 6e 42 75  _TEST).  n = nBu
10067 66 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 42 75 66  f;.  memset(zBuf
10068 2c 20 30 2c 20 6e 42 75 66 29 3b 0a 23 65 6c 73  , 0, nBuf);.#els
10069 65 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 28 53  e.  if( sizeof(S
1006a 59 53 54 45 4d 54 49 4d 45 29 3c 3d 6e 42 75 66  YSTEMTIME)<=nBuf
1006b 2d 6e 20 29 7b 0a 20 20 20 20 53 59 53 54 45 4d  -n ){.    SYSTEM
1006c 54 49 4d 45 20 78 3b 0a 20 20 20 20 47 65 74 53  TIME x;.    GetS
1006d 79 73 74 65 6d 54 69 6d 65 28 26 78 29 3b 0a 20  ystemTime(&x);. 
1006e 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b     memcpy(&zBuf[
1006f 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78  n], &x, sizeof(x
10070 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a  ));.    n += siz
10071 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 20 20 69 66  eof(x);.  }.  if
10072 28 20 73 69 7a 65 6f 66 28 44 57 4f 52 44 29 3c  ( sizeof(DWORD)<
10073 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 44  =nBuf-n ){.    D
10074 57 4f 52 44 20 70 69 64 20 3d 20 47 65 74 43 75  WORD pid = GetCu
10075 72 72 65 6e 74 50 72 6f 63 65 73 73 49 64 28 29  rrentProcessId()
10076 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42  ;.    memcpy(&zB
10077 75 66 5b 6e 5d 2c 20 26 70 69 64 2c 20 73 69 7a  uf[n], &pid, siz
10078 65 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20 6e  eof(pid));.    n
10079 20 2b 3d 20 73 69 7a 65 6f 66 28 70 69 64 29 3b   += sizeof(pid);
1007a 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65 6f  .  }.  if( sizeo
1007b 66 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66 2d 6e  f(DWORD)<=nBuf-n
1007c 20 29 7b 0a 20 20 20 20 44 57 4f 52 44 20 63 6e   ){.    DWORD cn
1007d 74 20 3d 20 47 65 74 54 69 63 6b 43 6f 75 6e 74  t = GetTickCount
1007e 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26  ();.    memcpy(&
1007f 7a 42 75 66 5b 6e 5d 2c 20 26 63 6e 74 2c 20 73  zBuf[n], &cnt, s
10080 69 7a 65 6f 66 28 63 6e 74 29 29 3b 0a 20 20 20  izeof(cnt));.   
10081 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 63 6e 74   n += sizeof(cnt
10082 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a  );.  }.  if( siz
10083 65 6f 66 28 4c 41 52 47 45 5f 49 4e 54 45 47 45  eof(LARGE_INTEGE
10084 52 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20  R)<=nBuf-n ){.  
10085 20 20 4c 41 52 47 45 5f 49 4e 54 45 47 45 52 20    LARGE_INTEGER 
10086 69 3b 0a 20 20 20 20 51 75 65 72 79 50 65 72 66  i;.    QueryPerf
10087 6f 72 6d 61 6e 63 65 43 6f 75 6e 74 65 72 28 26  ormanceCounter(&
10088 69 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26  i);.    memcpy(&
10089 7a 42 75 66 5b 6e 5d 2c 20 26 69 2c 20 73 69 7a  zBuf[n], &i, siz
1008a 65 6f 66 28 69 29 29 3b 0a 20 20 20 20 6e 20 2b  eof(i));.    n +
1008b 3d 20 73 69 7a 65 6f 66 28 69 29 3b 0a 20 20 7d  = sizeof(i);.  }
1008c 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
1008d 20 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c   n;.}.../*.** Sl
1008e 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65  eep for a little
1008f 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20   while.  Return 
10090 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69  the amount of ti
10091 6d 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a 73 74 61  me slept..*/.sta
10092 74 69 63 20 69 6e 74 20 77 69 6e 53 6c 65 65 70  tic int winSleep
10093 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
10094 66 73 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63  fs, int microsec
10095 29 7b 0a 20 20 53 6c 65 65 70 28 28 6d 69 63 72  ){.  Sleep((micr
10096 6f 73 65 63 2b 39 39 39 29 2f 31 30 30 30 29 3b  osec+999)/1000);
10097 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
10098 54 45 52 28 70 56 66 73 29 3b 0a 20 20 72 65 74  TER(pVfs);.  ret
10099 75 72 6e 20 28 28 6d 69 63 72 6f 73 65 63 2b 39  urn ((microsec+9
1009a 39 39 29 2f 31 30 30 30 29 2a 31 30 30 30 3b 0a  99)/1000)*1000;.
1009b 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  }../*.** The fol
1009c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c  lowing variable,
1009d 20 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e   if set to a non
1009e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63  -zero value, bec
1009f 6f 6d 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a  omes the result.
100a0 2a 2a 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d  ** returned from
100a1 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e   sqlite3OsCurren
100a2 74 54 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69  tTime().  This i
100a3 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69  s used for testi
100a4 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ng..*/.#ifdef SQ
100a5 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
100a6 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
100a7 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20  _current_time = 
100a8 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  0;.#endif../*.**
100a9 20 46 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e   Find the curren
100aa 74 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65  t time (in Unive
100ab 72 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64  rsal Coordinated
100ac 20 54 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74   Time).  Write t
100ad 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69  he.** current ti
100ae 6d 65 20 61 6e 64 20 64 61 74 65 20 61 73 20 61  me and date as a
100af 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62   Julian Day numb
100b0 65 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61  er into *prNow a
100b1 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20  nd.** return 0. 
100b2 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65   Return 1 if the
100b3 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63   time and date c
100b4 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a  annot be found..
100b5 2a 2f 0a 69 6e 74 20 77 69 6e 43 75 72 72 65 6e  */.int winCurren
100b6 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66  tTime(sqlite3_vf
100b7 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20  s *pVfs, double 
100b8 2a 70 72 4e 6f 77 29 7b 0a 20 20 46 49 4c 45 54  *prNow){.  FILET
100b9 49 4d 45 20 66 74 3b 0a 20 20 2f 2a 20 46 49 4c  IME ft;.  /* FIL
100ba 45 54 49 4d 45 20 73 74 72 75 63 74 75 72 65 20  ETIME structure 
100bb 69 73 20 61 20 36 34 2d 62 69 74 20 76 61 6c 75  is a 64-bit valu
100bc 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74  e representing t
100bd 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 0a 20 20  he number of .  
100be 20 20 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e     100-nanosecon
100bf 64 20 69 6e 74 65 72 76 61 6c 73 20 73 69 6e 63  d intervals sinc
100c0 65 20 4a 61 6e 75 61 72 79 20 31 2c 20 31 36 30  e January 1, 160
100c1 31 20 28 3d 20 4a 44 20 32 33 30 35 38 31 33 2e  1 (= JD 2305813.
100c2 35 29 2e 20 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  5). .  */.  sqli
100c3 74 65 33 5f 69 6e 74 36 34 20 74 69 6d 65 57 3b  te3_int64 timeW;
100c4 20 20 20 2f 2a 20 57 68 6f 6c 65 20 64 61 79 73     /* Whole days
100c5 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e   */.  sqlite3_in
100c6 74 36 34 20 74 69 6d 65 46 3b 20 20 20 2f 2a 20  t64 timeF;   /* 
100c7 46 72 61 63 74 69 6f 6e 61 6c 20 44 61 79 73 20  Fractional Days 
100c8 2a 2f 0a 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20  */..  /* Number 
100c9 6f 66 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e  of 100-nanosecon
100ca 64 20 69 6e 74 65 72 76 61 6c 73 20 69 6e 20 61  d intervals in a
100cb 20 73 69 6e 67 6c 65 20 64 61 79 20 2a 2f 0a 20   single day */. 
100cc 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71   static const sq
100cd 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 74 75 50  lite3_int64 ntuP
100ce 65 72 44 61 79 20 3d 20 0a 20 20 20 20 20 20 31  erDay = .      1
100cf 30 30 30 30 30 30 30 2a 28 73 71 6c 69 74 65 33  0000000*(sqlite3
100d0 5f 69 6e 74 36 34 29 38 36 34 30 30 3b 0a 0a 20  _int64)86400;.. 
100d1 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 31 30   /* Number of 10
100d2 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 20 69 6e 74  0-nanosecond int
100d3 65 72 76 61 6c 73 20 69 6e 20 68 61 6c 66 20 6f  ervals in half o
100d4 66 20 61 20 64 61 79 20 2a 2f 0a 20 20 73 74 61  f a day */.  sta
100d5 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
100d6 33 5f 69 6e 74 36 34 20 6e 74 75 50 65 72 48 61  3_int64 ntuPerHa
100d7 6c 66 44 61 79 20 3d 20 0a 20 20 20 20 20 20 31  lfDay = .      1
100d8 30 30 30 30 30 30 30 2a 28 73 71 6c 69 74 65 33  0000000*(sqlite3
100d9 5f 69 6e 74 36 34 29 34 33 32 30 30 3b 0a 0a 20  _int64)43200;.. 
100da 20 2f 2a 20 32 5e 33 32 20 2d 20 74 6f 20 61 76   /* 2^32 - to av
100db 6f 69 64 20 75 73 65 20 6f 66 20 4c 4c 20 61 6e  oid use of LL an
100dc 64 20 77 61 72 6e 69 6e 67 73 20 69 6e 20 67 63  d warnings in gc
100dd 63 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f  c */.  static co
100de 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  nst sqlite3_int6
100df 34 20 6d 61 78 33 32 42 69 74 56 61 6c 75 65 20  4 max32BitValue 
100e0 3d 20 0a 20 20 20 20 20 20 28 73 71 6c 69 74 65  = .      (sqlite
100e1 33 5f 69 6e 74 36 34 29 32 30 30 30 30 30 30 30  3_int64)20000000
100e2 30 30 20 2b 20 28 73 71 6c 69 74 65 33 5f 69 6e  00 + (sqlite3_in
100e3 74 36 34 29 32 30 30 30 30 30 30 30 30 30 20 2b  t64)2000000000 +
100e4 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29   (sqlite3_int64)
100e5 32 39 34 39 36 37 32 39 36 3b 0a 0a 23 69 66 20  294967296;..#if 
100e6 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a  SQLITE_OS_WINCE.
100e7 20 20 53 59 53 54 45 4d 54 49 4d 45 20 74 69 6d    SYSTEMTIME tim
100e8 65 3b 0a 20 20 47 65 74 53 79 73 74 65 6d 54 69  e;.  GetSystemTi
100e9 6d 65 28 26 74 69 6d 65 29 3b 0a 20 20 2f 2a 20  me(&time);.  /* 
100ea 69 66 20 53 79 73 74 65 6d 54 69 6d 65 54 6f 46  if SystemTimeToF
100eb 69 6c 65 54 69 6d 65 28 29 20 66 61 69 6c 73 2c  ileTime() fails,
100ec 20 69 74 20 72 65 74 75 72 6e 73 20 7a 65 72 6f   it returns zero
100ed 2e 20 2a 2f 0a 20 20 69 66 20 28 21 53 79 73 74  . */.  if (!Syst
100ee 65 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69 6d 65  emTimeToFileTime
100ef 28 26 74 69 6d 65 2c 26 66 74 29 29 7b 0a 20 20  (&time,&ft)){.  
100f0 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
100f1 23 65 6c 73 65 0a 20 20 47 65 74 53 79 73 74 65  #else.  GetSyste
100f2 6d 54 69 6d 65 41 73 46 69 6c 65 54 69 6d 65 28  mTimeAsFileTime(
100f3 20 26 66 74 20 29 3b 0a 23 65 6e 64 69 66 0a 20   &ft );.#endif. 
100f4 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
100f5 52 28 70 56 66 73 29 3b 0a 20 20 74 69 6d 65 57  R(pVfs);.  timeW
100f6 20 3d 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e   = (((sqlite3_in
100f7 74 36 34 29 66 74 2e 64 77 48 69 67 68 44 61 74  t64)ft.dwHighDat
100f8 65 54 69 6d 65 29 2a 6d 61 78 33 32 42 69 74 56  eTime)*max32BitV
100f9 61 6c 75 65 29 20 2b 20 28 73 71 6c 69 74 65 33  alue) + (sqlite3
100fa 5f 69 6e 74 36 34 29 66 74 2e 64 77 4c 6f 77 44  _int64)ft.dwLowD
100fb 61 74 65 54 69 6d 65 3b 0a 20 20 74 69 6d 65 46  ateTime;.  timeF
100fc 20 3d 20 74 69 6d 65 57 20 25 20 6e 74 75 50 65   = timeW % ntuPe
100fd 72 44 61 79 3b 20 20 20 20 20 20 20 20 20 20 2f  rDay;          /
100fe 2a 20 66 72 61 63 74 69 6f 6e 61 6c 20 64 61 79  * fractional day
100ff 73 20 28 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e  s (100-nanosecon
10100 64 73 29 20 2a 2f 0a 20 20 74 69 6d 65 57 20 3d  ds) */.  timeW =
10101 20 74 69 6d 65 57 20 2f 20 6e 74 75 50 65 72 44   timeW / ntuPerD
10102 61 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ay;          /* 
10103 77 68 6f 6c 65 20 64 61 79 73 20 2a 2f 0a 20 20  whole days */.  
10104 74 69 6d 65 57 20 3d 20 74 69 6d 65 57 20 2b 20  timeW = timeW + 
10105 32 33 30 35 38 31 33 3b 20 20 20 20 20 20 20 20  2305813;        
10106 20 20 20 20 2f 2a 20 61 64 64 20 77 68 6f 6c 65      /* add whole
10107 20 64 61 79 73 20 28 66 72 6f 6d 20 32 33 30 35   days (from 2305
10108 38 31 33 2e 35 29 20 2a 2f 0a 20 20 74 69 6d 65  813.5) */.  time
10109 46 20 3d 20 74 69 6d 65 46 20 2b 20 6e 74 75 50  F = timeF + ntuP
1010a 65 72 48 61 6c 66 44 61 79 3b 20 20 20 20 20 20  erHalfDay;      
1010b 2f 2a 20 61 64 64 20 68 61 6c 66 20 61 20 64 61  /* add half a da
1010c 79 20 28 66 72 6f 6d 20 32 33 30 35 38 31 33 2e  y (from 2305813.
1010d 35 29 20 2a 2f 0a 20 20 74 69 6d 65 57 20 3d 20  5) */.  timeW = 
1010e 74 69 6d 65 57 20 2b 20 28 74 69 6d 65 46 2f 6e  timeW + (timeF/n
1010f 74 75 50 65 72 44 61 79 29 3b 20 20 2f 2a 20 61  tuPerDay);  /* a
10110 64 64 20 77 68 6f 6c 65 20 64 61 79 20 69 66 20  dd whole day if 
10111 68 61 6c 66 20 64 61 79 20 6d 61 64 65 20 6f 6e  half day made on
10112 65 20 2a 2f 0a 20 20 74 69 6d 65 46 20 3d 20 74  e */.  timeF = t
10113 69 6d 65 46 20 25 20 6e 74 75 50 65 72 44 61 79  imeF % ntuPerDay
10114 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f  ;          /* co
10115 6d 70 75 74 65 20 6e 65 77 20 66 72 61 63 74 69  mpute new fracti
10116 6f 6e 61 6c 20 64 61 79 73 20 2a 2f 0a 20 20 2a  onal days */.  *
10117 70 72 4e 6f 77 20 3d 20 28 64 6f 75 62 6c 65 29  prNow = (double)
10118 74 69 6d 65 57 20 2b 20 28 28 64 6f 75 62 6c 65  timeW + ((double
10119 29 74 69 6d 65 46 20 2f 20 28 64 6f 75 62 6c 65  )timeF / (double
1011a 29 6e 74 75 50 65 72 44 61 79 29 3b 0a 23 69 66  )ntuPerDay);.#if
1011b 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
1011c 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75    if( sqlite3_cu
1011d 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20  rrent_time ){.  
1011e 20 20 2a 70 72 4e 6f 77 20 3d 20 28 28 64 6f 75    *prNow = ((dou
1011f 62 6c 65 29 73 71 6c 69 74 65 33 5f 63 75 72 72  ble)sqlite3_curr
10120 65 6e 74 5f 74 69 6d 65 20 2b 20 28 64 6f 75 62  ent_time + (doub
10121 6c 65 29 34 33 32 30 30 29 20 2f 20 28 64 6f 75  le)43200) / (dou
10122 62 6c 65 29 38 36 34 30 30 20 2b 20 28 64 6f 75  ble)86400 + (dou
10123 62 6c 65 29 32 34 34 30 35 38 37 3b 0a 20 20 7d  ble)2440587;.  }
10124 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
10125 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65   0;.}../*.** The
10126 20 69 64 65 61 20 69 73 20 74 68 61 74 20 74 68   idea is that th
10127 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 6f 72 6b  is function work
10128 73 20 6c 69 6b 65 20 61 20 63 6f 6d 62 69 6e 61  s like a combina
10129 74 69 6f 6e 20 6f 66 0a 2a 2a 20 47 65 74 4c 61  tion of.** GetLa
1012a 73 74 45 72 72 6f 72 28 29 20 61 6e 64 20 46 6f  stError() and Fo
1012b 72 6d 61 74 4d 65 73 73 61 67 65 28 29 20 6f 6e  rmatMessage() on
1012c 20 77 69 6e 64 6f 77 73 20 28 6f 72 20 65 72 72   windows (or err
1012d 6e 6f 20 61 6e 64 0a 2a 2a 20 73 74 72 65 72 72  no and.** strerr
1012e 6f 72 5f 72 28 29 20 6f 6e 20 75 6e 69 78 29 2e  or_r() on unix).
1012f 20 41 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20   After an error 
10130 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 61  is returned by a
10131 6e 20 4f 53 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  n OS.** function
10132 2c 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74  , SQLite calls t
10133 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  his function wit
10134 68 20 7a 42 75 66 20 70 6f 69 6e 74 69 6e 67 20  h zBuf pointing 
10135 74 6f 0a 2a 2a 20 61 20 62 75 66 66 65 72 20 6f  to.** a buffer o
10136 66 20 6e 42 75 66 20 62 79 74 65 73 2e 20 54 68  f nBuf bytes. Th
10137 65 20 4f 53 20 6c 61 79 65 72 20 73 68 6f 75 6c  e OS layer shoul
10138 64 20 70 6f 70 75 6c 61 74 65 20 74 68 65 0a 2a  d populate the.*
10139 2a 20 62 75 66 66 65 72 20 77 69 74 68 20 61 20  * buffer with a 
1013a 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55  nul-terminated U
1013b 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 65 72 72  TF-8 encoded err
1013c 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 64 65  or message.** de
1013d 73 63 72 69 62 69 6e 67 20 74 68 65 20 6c 61 73  scribing the las
1013e 74 20 49 4f 20 65 72 72 6f 72 20 74 6f 20 68 61  t IO error to ha
1013f 76 65 20 6f 63 63 75 72 72 65 64 20 77 69 74 68  ve occurred with
10140 69 6e 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a  in the calling.*
10141 2a 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20  * thread..**.** 
10142 49 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73  If the error mes
10143 73 61 67 65 20 69 73 20 74 6f 6f 20 6c 61 72 67  sage is too larg
10144 65 20 66 6f 72 20 74 68 65 20 73 75 70 70 6c 69  e for the suppli
10145 65 64 20 62 75 66 66 65 72 2c 0a 2a 2a 20 69 74  ed buffer,.** it
10146 20 73 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63   should be trunc
10147 61 74 65 64 2e 20 54 68 65 20 72 65 74 75 72 6e  ated. The return
10148 20 76 61 6c 75 65 20 6f 66 20 78 47 65 74 4c 61   value of xGetLa
10149 73 74 45 72 72 6f 72 0a 2a 2a 20 69 73 20 7a 65  stError.** is ze
1014a 72 6f 20 69 66 20 74 68 65 20 65 72 72 6f 72 20  ro if the error 
1014b 6d 65 73 73 61 67 65 20 66 69 74 73 20 69 6e 20  message fits in 
1014c 74 68 65 20 62 75 66 66 65 72 2c 20 6f 72 20 6e  the buffer, or n
1014d 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68 65 72  on-zero.** other
1014e 77 69 73 65 20 28 69 66 20 74 68 65 20 6d 65 73  wise (if the mes
1014f 73 61 67 65 20 77 61 73 20 74 72 75 6e 63 61 74  sage was truncat
10150 65 64 29 2e 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f  ed). If non-zero
10151 20 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a 2a   is returned,.**
10152 20 74 68 65 6e 20 69 74 20 69 73 20 6e 6f 74 20   then it is not 
10153 6e 65 63 65 73 73 61 72 79 20 74 6f 20 69 6e 63  necessary to inc
10154 6c 75 64 65 20 74 68 65 20 6e 75 6c 2d 74 65 72  lude the nul-ter
10155 6d 69 6e 61 74 6f 72 20 63 68 61 72 61 63 74 65  minator characte
10156 72 0a 2a 2a 20 69 6e 20 74 68 65 20 6f 75 74 70  r.** in the outp
10157 75 74 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a  ut buffer..**.**
10158 20 4e 6f 74 20 73 75 70 70 6c 79 69 6e 67 20 61   Not supplying a
10159 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
1015a 77 69 6c 6c 20 68 61 76 65 20 6e 6f 20 61 64 76  will have no adv
1015b 65 72 73 65 20 65 66 66 65 63 74 0a 2a 2a 20 6f  erse effect.** o
1015c 6e 20 53 51 4c 69 74 65 2e 20 49 74 20 69 73 20  n SQLite. It is 
1015d 66 69 6e 65 20 74 6f 20 68 61 76 65 20 61 6e 20  fine to have an 
1015e 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74  implementation t
1015f 68 61 74 20 6e 65 76 65 72 0a 2a 2a 20 72 65 74  hat never.** ret
10160 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 6d 65  urns an error me
10161 73 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 69  ssage:.**.**   i
10162 6e 74 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72  nt xGetLastError
10163 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
10164 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68  fs, int nBuf, ch
10165 61 72 20 2a 7a 42 75 66 29 7b 0a 2a 2a 20 20 20  ar *zBuf){.**   
10166 20 20 61 73 73 65 72 74 28 7a 42 75 66 5b 30 5d    assert(zBuf[0]
10167 3d 3d 27 5c 30 27 29 3b 0a 2a 2a 20 20 20 20 20  =='\0');.**     
10168 72 65 74 75 72 6e 20 30 3b 0a 2a 2a 20 20 20 7d  return 0;.**   }
10169 0a 2a 2a 0a 2a 2a 20 48 6f 77 65 76 65 72 20 69  .**.** However i
1016a 66 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  f an error messa
1016b 67 65 20 69 73 20 73 75 70 70 6c 69 65 64 2c 20  ge is supplied, 
1016c 69 74 20 77 69 6c 6c 20 62 65 20 69 6e 63 6f 72  it will be incor
1016d 70 6f 72 61 74 65 64 0a 2a 2a 20 62 79 20 73 71  porated.** by sq
1016e 6c 69 74 65 20 69 6e 74 6f 20 74 68 65 20 65 72  lite into the er
1016f 72 6f 72 20 6d 65 73 73 61 67 65 20 61 76 61 69  ror message avai
10170 6c 61 62 6c 65 20 74 6f 20 74 68 65 20 75 73 65  lable to the use
10171 72 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74  r using.** sqlit
10172 65 33 5f 65 72 72 6d 73 67 28 29 2c 20 70 6f 73  e3_errmsg(), pos
10173 73 69 62 6c 79 20 6d 61 6b 69 6e 67 20 49 4f 20  sibly making IO 
10174 65 72 72 6f 72 73 20 65 61 73 69 65 72 20 74 6f  errors easier to
10175 20 64 65 62 75 67 2e 0a 2a 2f 0a 73 74 61 74 69   debug..*/.stati
10176 63 20 69 6e 74 20 77 69 6e 47 65 74 4c 61 73 74  c int winGetLast
10177 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66  Error(sqlite3_vf
10178 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75  s *pVfs, int nBu
10179 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a  f, char *zBuf){.
1017a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
1017b 45 52 28 70 56 66 73 29 3b 0a 20 20 72 65 74 75  ER(pVfs);.  retu
1017c 72 6e 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d  rn getLastErrorM
1017d 73 67 28 6e 42 75 66 2c 20 7a 42 75 66 29 3b 0a  sg(nBuf, zBuf);.
1017e 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c  }../*.** Initial
1017f 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61  ize and deinitia
10180 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 69  lize the operati
10181 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66  ng system interf
10182 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ace..*/.SQLITE_A
10183 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
10184 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20  s_init(void){.  
10185 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76  static sqlite3_v
10186 66 73 20 77 69 6e 56 66 73 20 3d 20 7b 0a 20 20  fs winVfs = {.  
10187 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20    1,            
10188 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e       /* iVersion
10189 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 77   */.    sizeof(w
1018a 69 6e 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73 7a  inFile),   /* sz
1018b 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 4d 41  OsFile */.    MA
1018c 58 5f 50 41 54 48 2c 20 20 20 20 20 20 20 20 20  X_PATH,         
1018d 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a   /* mxPathname *
1018e 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  /.    0,        
1018f 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78           /* pNex
10190 74 20 2a 2f 0a 20 20 20 20 22 77 69 6e 33 32 22  t */.    "win32"
10191 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a  ,           /* z
10192 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20  Name */.    0,  
10193 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10194 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 20 0a  * pAppData */. .
10195 20 20 20 20 77 69 6e 4f 70 65 6e 2c 20 20 20 20      winOpen,    
10196 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20         /* xOpen 
10197 2a 2f 0a 20 20 20 20 77 69 6e 44 65 6c 65 74 65  */.    winDelete
10198 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65  ,         /* xDe
10199 6c 65 74 65 20 2a 2f 0a 20 20 20 20 77 69 6e 41  lete */.    winA
1019a 63 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 2f  ccess,         /
1019b 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20  * xAccess */.   
1019c 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65   winFullPathname
1019d 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68  ,   /* xFullPath
1019e 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e 44  name */.    winD
1019f 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 2f  lOpen,         /
101a0 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20  * xDlOpen */.   
101a1 20 77 69 6e 44 6c 45 72 72 6f 72 2c 20 20 20 20   winDlError,    
101a2 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20      /* xDlError 
101a3 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 53 79 6d 2c  */.    winDlSym,
101a4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c            /* xDl
101a5 53 79 6d 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c  Sym */.    winDl
101a6 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 2f 2a  Close,        /*
101a7 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 20 20   xDlClose */.   
101a8 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 2c 20   winRandomness, 
101a9 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65      /* xRandomne
101aa 73 73 20 2a 2f 0a 20 20 20 20 77 69 6e 53 6c 65  ss */.    winSle
101ab 65 70 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ep,          /* 
101ac 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 77 69  xSleep */.    wi
101ad 6e 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20  nCurrentTime,   
101ae 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65   /* xCurrentTime
101af 20 2a 2f 0a 20 20 20 20 77 69 6e 47 65 74 4c 61   */.    winGetLa
101b0 73 74 45 72 72 6f 72 20 20 20 20 2f 2a 20 78 47  stError    /* xG
101b1 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 0a 20  etLastError */. 
101b2 20 7d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 76   };..  sqlite3_v
101b3 66 73 5f 72 65 67 69 73 74 65 72 28 26 77 69 6e  fs_register(&win
101b4 56 66 73 2c 20 31 29 3b 0a 20 20 72 65 74 75 72  Vfs, 1);.  retur
101b5 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a  n SQLITE_OK; .}.
101b6 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
101b7 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f  qlite3_os_end(vo
101b8 69 64 29 7b 20 0a 20 20 72 65 74 75 72 6e 20 53  id){ .  return S
101b9 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e  QLITE_OK;.}..#en
101ba 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53  dif /* SQLITE_OS
101bb 5f 57 49 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  _WIN */../******
101bc 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
101bd 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a  os_win.c *******
101be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101c0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
101c1 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
101c2 69 6c 65 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a  ile bitvec.c ***
101c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101c5 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
101c6 30 30 38 20 46 65 62 72 75 61 72 79 20 31 36 0a  008 February 16.
101c7 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
101c8 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
101c9 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
101ca 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
101cb 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
101cc 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
101cd 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
101ce 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
101cf 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
101d0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
101d1 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
101d2 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
101d3 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
101d4 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
101d5 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
101d6 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
101d7 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
101d8 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
101d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
101dd 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c  * This file impl
101de 65 6d 65 6e 74 73 20 61 6e 20 6f 62 6a 65 63 74  ements an object
101df 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 73   that represents
101e0 20 61 20 66 69 78 65 64 2d 6c 65 6e 67 74 68 0a   a fixed-length.
101e1 2a 2a 20 62 69 74 6d 61 70 2e 20 20 42 69 74 73  ** bitmap.  Bits
101e2 20 61 72 65 20 6e 75 6d 62 65 72 65 64 20 73 74   are numbered st
101e3 61 72 74 69 6e 67 20 77 69 74 68 20 31 2e 0a 2a  arting with 1..*
101e4 2a 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20 69 73  *.** A bitmap is
101e5 20 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20   used to record 
101e6 77 68 69 63 68 20 70 61 67 65 73 20 6f 66 20 61  which pages of a
101e7 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68   database file h
101e8 61 76 65 20 62 65 65 6e 0a 2a 2a 20 6a 6f 75 72  ave been.** jour
101e9 6e 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 61 20  nalled during a 
101ea 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20  transaction, or 
101eb 77 68 69 63 68 20 70 61 67 65 73 20 68 61 76 65  which pages have
101ec 20 74 68 65 20 22 64 6f 6e 74 2d 77 72 69 74 65   the "dont-write
101ed 22 0a 2a 2a 20 70 72 6f 70 65 72 74 79 2e 20 20  ".** property.  
101ee 55 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 61 20 66  Usually only a f
101ef 65 77 20 70 61 67 65 73 20 61 72 65 20 6d 65 65  ew pages are mee
101f0 74 20 65 69 74 68 65 72 20 63 6f 6e 64 69 74 69  t either conditi
101f1 6f 6e 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 62 69  on..** So the bi
101f2 74 6d 61 70 20 69 73 20 75 73 75 61 6c 6c 79 20  tmap is usually 
101f3 73 70 61 72 73 65 20 61 6e 64 20 68 61 73 20 6c  sparse and has l
101f4 6f 77 20 63 61 72 64 69 6e 61 6c 69 74 79 2e 0a  ow cardinality..
101f5 2a 2a 20 42 75 74 20 73 6f 6d 65 74 69 6d 65 73  ** But sometimes
101f6 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 77 68   (for example wh
101f7 65 6e 20 64 75 72 69 6e 67 20 61 20 44 52 4f 50  en during a DROP
101f8 20 6f 66 20 61 20 6c 61 72 67 65 20 74 61 62 6c   of a large tabl
101f9 65 29 20 6d 6f 73 74 0a 2a 2a 20 6f 72 20 61 6c  e) most.** or al
101fa 6c 20 6f 66 20 74 68 65 20 70 61 67 65 73 20 69  l of the pages i
101fb 6e 20 61 20 64 61 74 61 62 61 73 65 20 63 61 6e  n a database can
101fc 20 67 65 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 2e   get journalled.
101fd 20 20 49 6e 20 74 68 6f 73 65 20 63 61 73 65 73    In those cases
101fe 2c 20 0a 2a 2a 20 74 68 65 20 62 69 74 6d 61 70  , .** the bitmap
101ff 20 62 65 63 6f 6d 65 73 20 64 65 6e 73 65 20 77   becomes dense w
10200 69 74 68 20 68 69 67 68 20 63 61 72 64 69 6e 61  ith high cardina
10201 6c 69 74 79 2e 20 20 54 68 65 20 61 6c 67 6f 72  lity.  The algor
10202 69 74 68 6d 20 6e 65 65 64 73 20 0a 2a 2a 20 74  ithm needs .** t
10203 6f 20 68 61 6e 64 6c 65 20 62 6f 74 68 20 63 61  o handle both ca
10204 73 65 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20  ses well..**.** 
10205 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  The size of the 
10206 62 69 74 6d 61 70 20 69 73 20 66 69 78 65 64 20  bitmap is fixed 
10207 77 68 65 6e 20 74 68 65 20 6f 62 6a 65 63 74 20  when the object 
10208 69 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a  is created..**.*
10209 2a 20 41 6c 6c 20 62 69 74 73 20 61 72 65 20 63  * All bits are c
1020a 6c 65 61 72 20 77 68 65 6e 20 74 68 65 20 62 69  lear when the bi
1020b 74 6d 61 70 20 69 73 20 63 72 65 61 74 65 64 2e  tmap is created.
1020c 20 20 49 6e 64 69 76 69 64 75 61 6c 20 62 69 74    Individual bit
1020d 73 0a 2a 2a 20 6d 61 79 20 62 65 20 73 65 74 20  s.** may be set 
1020e 6f 72 20 63 6c 65 61 72 65 64 20 6f 6e 65 20 61  or cleared one a
1020f 74 20 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20  t a time..**.** 
10210 54 65 73 74 20 6f 70 65 72 61 74 69 6f 6e 73 20  Test operations 
10211 61 72 65 20 61 62 6f 75 74 20 31 30 30 20 74 69  are about 100 ti
10212 6d 65 73 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 20  mes more common 
10213 74 68 61 74 20 73 65 74 20 6f 70 65 72 61 74 69  that set operati
10214 6f 6e 73 2e 0a 2a 2a 20 43 6c 65 61 72 20 6f 70  ons..** Clear op
10215 65 72 61 74 69 6f 6e 73 20 61 72 65 20 65 78 63  erations are exc
10216 65 65 64 69 6e 67 6c 79 20 72 61 72 65 2e 20 20  eedingly rare.  
10217 54 68 65 72 65 20 61 72 65 20 75 73 75 61 6c 6c  There are usuall
10218 79 20 62 65 74 77 65 65 6e 0a 2a 2a 20 35 20 61  y between.** 5 a
10219 6e 64 20 35 30 30 20 73 65 74 20 6f 70 65 72 61  nd 500 set opera
1021a 74 69 6f 6e 73 20 70 65 72 20 42 69 74 76 65 63  tions per Bitvec
1021b 20 6f 62 6a 65 63 74 2c 20 74 68 6f 75 67 68 20   object, though 
1021c 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65  the number of se
1021d 74 73 20 63 61 6e 0a 2a 2a 20 73 6f 6d 65 74 69  ts can.** someti
1021e 6d 65 73 20 67 72 6f 77 20 69 6e 74 6f 20 74 65  mes grow into te
1021f 6e 73 20 6f 66 20 74 68 6f 75 73 61 6e 64 73 20  ns of thousands 
10220 6f 72 20 6c 61 72 67 65 72 2e 20 20 54 68 65 20  or larger.  The 
10221 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 42  size of the.** B
10222 69 74 76 65 63 20 6f 62 6a 65 63 74 20 69 73 20  itvec object is 
10223 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
10224 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62  ges in the datab
10225 61 73 65 20 66 69 6c 65 20 61 74 20 74 68 65 0a  ase file at the.
10226 2a 2a 20 73 74 61 72 74 20 6f 66 20 61 20 74 72  ** start of a tr
10227 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64 20 69  ansaction, and i
10228 73 20 74 68 75 73 20 75 73 75 61 6c 6c 79 20 6c  s thus usually l
10229 65 73 73 20 74 68 61 6e 20 61 20 66 65 77 20 74  ess than a few t
1022a 68 6f 75 73 61 6e 64 2c 0a 2a 2a 20 62 75 74 20  housand,.** but 
1022b 63 61 6e 20 62 65 20 61 73 20 6c 61 72 67 65 20  can be as large 
1022c 61 73 20 32 20 62 69 6c 6c 69 6f 6e 20 66 6f 72  as 2 billion for
1022d 20 61 20 72 65 61 6c 6c 79 20 62 69 67 20 64 61   a really big da
1022e 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 40 28  tabase..**.** @(
1022f 23 29 20 24 49 64 3a 20 62 69 74 76 65 63 2e 63  #) $Id: bitvec.c
10230 2c 76 20 31 2e 31 37 20 32 30 30 39 2f 30 37 2f  ,v 1.17 2009/07/
10231 32 35 20 31 37 3a 33 33 3a 32 36 20 64 72 68 20  25 17:33:26 drh 
10232 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 53 69 7a  Exp $.*/../* Siz
10233 65 20 6f 66 20 74 68 65 20 42 69 74 76 65 63 20  e of the Bitvec 
10234 73 74 72 75 63 74 75 72 65 20 69 6e 20 62 79 74  structure in byt
10235 65 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42  es. */.#define B
10236 49 54 56 45 43 5f 53 5a 20 20 20 20 20 20 20 20  ITVEC_SZ        
10237 28 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 2a 31  (sizeof(void*)*1
10238 32 38 29 20 20 2f 2a 20 35 31 32 20 6f 6e 20 33  28)  /* 512 on 3
10239 32 62 69 74 2e 20 20 31 30 32 34 20 6f 6e 20 36  2bit.  1024 on 6
1023a 34 62 69 74 20 2a 2f 0a 0a 2f 2a 20 52 6f 75 6e  4bit */../* Roun
1023b 64 20 74 68 65 20 75 6e 69 6f 6e 20 73 69 7a 65  d the union size
1023c 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 6e 65 61   down to the nea
1023d 72 65 73 74 20 70 6f 69 6e 74 65 72 20 62 6f 75  rest pointer bou
1023e 6e 64 61 72 79 2c 20 73 69 6e 63 65 20 74 68 61  ndary, since tha
1023f 74 27 73 20 68 6f 77 20 0a 2a 2a 20 69 74 20 77  t's how .** it w
10240 69 6c 6c 20 62 65 20 61 6c 69 67 6e 65 64 20 77  ill be aligned w
10241 69 74 68 69 6e 20 74 68 65 20 42 69 74 76 65 63  ithin the Bitvec
10242 20 73 74 72 75 63 74 2e 20 2a 2f 0a 23 64 65 66   struct. */.#def
10243 69 6e 65 20 42 49 54 56 45 43 5f 55 53 49 5a 45  ine BITVEC_USIZE
10244 20 20 20 20 20 28 28 28 42 49 54 56 45 43 5f 53       (((BITVEC_S
10245 5a 2d 28 33 2a 73 69 7a 65 6f 66 28 75 33 32 29  Z-(3*sizeof(u32)
10246 29 29 2f 73 69 7a 65 6f 66 28 42 69 74 76 65 63  ))/sizeof(Bitvec
10247 2a 29 29 2a 73 69 7a 65 6f 66 28 42 69 74 76 65  *))*sizeof(Bitve
10248 63 2a 29 29 0a 0a 2f 2a 20 54 79 70 65 20 6f 66  c*))../* Type of
10249 20 74 68 65 20 61 72 72 61 79 20 22 65 6c 65 6d   the array "elem
1024a 65 6e 74 22 20 66 6f 72 20 74 68 65 20 62 69 74  ent" for the bit
1024b 6d 61 70 20 72 65 70 72 65 73 65 6e 74 61 74 69  map representati
1024c 6f 6e 2e 20 0a 2a 2a 20 53 68 6f 75 6c 64 20 62  on. .** Should b
1024d 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2c 20  e a power of 2, 
1024e 61 6e 64 20 69 64 65 61 6c 6c 79 2c 20 65 76 65  and ideally, eve
1024f 6e 6c 79 20 64 69 76 69 64 65 20 69 6e 74 6f 20  nly divide into 
10250 42 49 54 56 45 43 5f 55 53 49 5a 45 2e 20 0a 2a  BITVEC_USIZE. .*
10251 2a 20 53 65 74 74 69 6e 67 20 74 68 69 73 20 74  * Setting this t
10252 6f 20 74 68 65 20 22 6e 61 74 75 72 61 6c 20 77  o the "natural w
10253 6f 72 64 22 20 73 69 7a 65 20 6f 66 20 79 6f 75  ord" size of you
10254 72 20 43 50 55 20 6d 61 79 20 69 6d 70 72 6f 76  r CPU may improv
10255 65 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65  e.** performance
10256 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54  . */.#define BIT
10257 56 45 43 5f 54 45 4c 45 4d 20 20 20 20 20 75 38  VEC_TELEM     u8
10258 0a 2f 2a 20 53 69 7a 65 2c 20 69 6e 20 62 69 74  ./* Size, in bit
10259 73 2c 20 6f 66 20 74 68 65 20 62 69 74 6d 61 70  s, of the bitmap
1025a 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 23 64 65   element. */.#de
1025b 66 69 6e 65 20 42 49 54 56 45 43 5f 53 5a 45 4c  fine BITVEC_SZEL
1025c 45 4d 20 20 20 20 38 0a 2f 2a 20 4e 75 6d 62 65  EM    8./* Numbe
1025d 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e  r of elements in
1025e 20 61 20 62 69 74 6d 61 70 20 61 72 72 61 79 2e   a bitmap array.
1025f 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56   */.#define BITV
10260 45 43 5f 4e 45 4c 45 4d 20 20 20 20 20 28 42 49  EC_NELEM     (BI
10261 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f  TVEC_USIZE/sizeo
10262 66 28 42 49 54 56 45 43 5f 54 45 4c 45 4d 29 29  f(BITVEC_TELEM))
10263 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 69  ./* Number of bi
10264 74 73 20 69 6e 20 74 68 65 20 62 69 74 6d 61 70  ts in the bitmap
10265 20 61 72 72 61 79 2e 20 2a 2f 0a 23 64 65 66 69   array. */.#defi
10266 6e 65 20 42 49 54 56 45 43 5f 4e 42 49 54 20 20  ne BITVEC_NBIT  
10267 20 20 20 20 28 42 49 54 56 45 43 5f 4e 45 4c 45      (BITVEC_NELE
10268 4d 2a 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 29  M*BITVEC_SZELEM)
10269 0a 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75  ../* Number of u
1026a 33 32 20 76 61 6c 75 65 73 20 69 6e 20 68 61 73  32 values in has
1026b 68 20 74 61 62 6c 65 2e 20 2a 2f 0a 23 64 65 66  h table. */.#def
1026c 69 6e 65 20 42 49 54 56 45 43 5f 4e 49 4e 54 20  ine BITVEC_NINT 
1026d 20 20 20 20 20 28 42 49 54 56 45 43 5f 55 53 49       (BITVEC_USI
1026e 5a 45 2f 73 69 7a 65 6f 66 28 75 33 32 29 29 0a  ZE/sizeof(u32)).
1026f 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  /* Maximum numbe
10270 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
10271 68 61 73 68 20 74 61 62 6c 65 20 62 65 66 6f 72  hash table befor
10272 65 20 0a 2a 2a 20 73 75 62 2d 64 69 76 69 64 69  e .** sub-dividi
10273 6e 67 20 61 6e 64 20 72 65 2d 68 61 73 68 69 6e  ng and re-hashin
10274 67 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49  g. */.#define BI
10275 54 56 45 43 5f 4d 58 48 41 53 48 20 20 20 20 28  TVEC_MXHASH    (
10276 42 49 54 56 45 43 5f 4e 49 4e 54 2f 32 29 0a 2f  BITVEC_NINT/2)./
10277 2a 20 48 61 73 68 69 6e 67 20 66 75 6e 63 74 69  * Hashing functi
10278 6f 6e 20 66 6f 72 20 74 68 65 20 61 48 61 73 68  on for the aHash
10279 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e   representation.
1027a 0a 2a 2a 20 45 6d 70 69 72 69 63 61 6c 20 74 65  .** Empirical te
1027b 73 74 69 6e 67 20 73 68 6f 77 65 64 20 74 68 61  sting showed tha
1027c 74 20 74 68 65 20 2a 33 37 20 6d 75 6c 74 69 70  t the *37 multip
1027d 6c 69 65 72 20 0a 2a 2a 20 28 61 6e 20 61 72 62  lier .** (an arb
1027e 69 74 72 61 72 79 20 70 72 69 6d 65 29 69 6e 20  itrary prime)in 
1027f 74 68 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f  the hash functio
10280 6e 20 70 72 6f 76 69 64 65 64 20 0a 2a 2a 20 6e  n provided .** n
10281 6f 20 66 65 77 65 72 20 63 6f 6c 6c 69 73 69 6f  o fewer collisio
10282 6e 73 20 74 68 61 6e 20 74 68 65 20 6e 6f 2d 6f  ns than the no-o
10283 70 20 2a 31 2e 20 2a 2f 0a 23 64 65 66 69 6e 65  p *1. */.#define
10284 20 42 49 54 56 45 43 5f 48 41 53 48 28 58 29 20   BITVEC_HASH(X) 
10285 20 20 28 28 28 58 29 2a 31 29 25 42 49 54 56 45    (((X)*1)%BITVE
10286 43 5f 4e 49 4e 54 29 0a 0a 23 64 65 66 69 6e 65  C_NINT)..#define
10287 20 42 49 54 56 45 43 5f 4e 50 54 52 20 20 20 20   BITVEC_NPTR    
10288 20 20 28 42 49 54 56 45 43 5f 55 53 49 5a 45 2f    (BITVEC_USIZE/
10289 73 69 7a 65 6f 66 28 42 69 74 76 65 63 20 2a 29  sizeof(Bitvec *)
1028a 29 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 62 69 74 6d  ).../*.** A bitm
1028b 61 70 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  ap is an instanc
1028c 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
1028d 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ng structure..**
1028e 0a 2a 2a 20 54 68 69 73 20 62 69 74 6d 61 70 20  .** This bitmap 
1028f 72 65 63 6f 72 64 73 20 74 68 65 20 65 78 69 73  records the exis
10290 74 61 6e 63 65 20 6f 66 20 7a 65 72 6f 20 6f 72  tance of zero or
10291 20 6d 6f 72 65 20 62 69 74 73 0a 2a 2a 20 77 69   more bits.** wi
10292 74 68 20 76 61 6c 75 65 73 20 62 65 74 77 65 65  th values betwee
10293 6e 20 31 20 61 6e 64 20 69 53 69 7a 65 2c 20 69  n 1 and iSize, i
10294 6e 63 6c 75 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20  nclusive..**.** 
10295 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65 20  There are three 
10296 70 6f 73 73 69 62 6c 65 20 72 65 70 72 65 73 65  possible represe
10297 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20  ntations of the 
10298 62 69 74 6d 61 70 2e 0a 2a 2a 20 49 66 20 69 53  bitmap..** If iS
10299 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54  ize<=BITVEC_NBIT
1029a 2c 20 74 68 65 6e 20 42 69 74 76 65 63 2e 75 2e  , then Bitvec.u.
1029b 61 42 69 74 6d 61 70 5b 5d 20 69 73 20 61 20 73  aBitmap[] is a s
1029c 74 72 61 69 67 68 74 0a 2a 2a 20 62 69 74 6d 61  traight.** bitma
1029d 70 2e 20 20 54 68 65 20 6c 65 61 73 74 20 73 69  p.  The least si
1029e 67 6e 69 66 69 63 61 6e 74 20 62 69 74 20 69 73  gnificant bit is
1029f 20 62 69 74 20 31 2e 0a 2a 2a 0a 2a 2a 20 49 66   bit 1..**.** If
102a0 20 69 53 69 7a 65 3e 42 49 54 56 45 43 5f 4e 42   iSize>BITVEC_NB
102a1 49 54 20 61 6e 64 20 69 44 69 76 69 73 6f 72 3d  IT and iDivisor=
102a2 3d 30 20 74 68 65 6e 20 42 69 74 76 65 63 2e 75  =0 then Bitvec.u
102a3 2e 61 48 61 73 68 5b 5d 20 69 73 0a 2a 2a 20 61  .aHash[] is.** a
102a4 20 68 61 73 68 20 74 61 62 6c 65 20 74 68 61 74   hash table that
102a5 20 77 69 6c 6c 20 68 6f 6c 64 20 75 70 20 74 6f   will hold up to
102a6 20 42 49 54 56 45 43 5f 4d 58 48 41 53 48 20 64   BITVEC_MXHASH d
102a7 69 73 74 69 6e 63 74 20 76 61 6c 75 65 73 2e 0a  istinct values..
102a8 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  **.** Otherwise,
102a9 20 74 68 65 20 76 61 6c 75 65 20 69 20 69 73 20   the value i is 
102aa 72 65 64 69 72 65 63 74 65 64 20 69 6e 74 6f 20  redirected into 
102ab 6f 6e 65 20 6f 66 20 42 49 54 56 45 43 5f 4e 50  one of BITVEC_NP
102ac 54 52 0a 2a 2a 20 73 75 62 2d 62 69 74 6d 61 70  TR.** sub-bitmap
102ad 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  s pointed to by 
102ae 42 69 74 76 65 63 2e 75 2e 61 70 53 75 62 5b 5d  Bitvec.u.apSub[]
102af 2e 20 20 45 61 63 68 20 73 75 62 62 69 74 6d 61  .  Each subbitma
102b0 70 0a 2a 2a 20 68 61 6e 64 6c 65 73 20 75 70 20  p.** handles up 
102b1 74 6f 20 69 44 69 76 69 73 6f 72 20 73 65 70 61  to iDivisor sepa
102b2 72 61 74 65 20 76 61 6c 75 65 73 20 6f 66 20 69  rate values of i
102b3 2e 20 20 61 70 53 75 62 5b 30 5d 20 68 6f 6c 64  .  apSub[0] hold
102b4 73 0a 2a 2a 20 76 61 6c 75 65 73 20 62 65 74 77  s.** values betw
102b5 65 65 6e 20 31 20 61 6e 64 20 69 44 69 76 69 73  een 1 and iDivis
102b6 6f 72 2e 20 20 61 70 53 75 62 5b 31 5d 20 68 6f  or.  apSub[1] ho
102b7 6c 64 73 20 76 61 6c 75 65 73 20 62 65 74 77 65  lds values betwe
102b8 65 6e 0a 2a 2a 20 69 44 69 76 69 73 6f 72 2b 31  en.** iDivisor+1
102b9 20 61 6e 64 20 32 2a 69 44 69 76 69 73 6f 72 2e   and 2*iDivisor.
102ba 20 20 61 70 53 75 62 5b 4e 5d 20 68 6f 6c 64 73    apSub[N] holds
102bb 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e 0a   values between.
102bc 2a 2a 20 4e 2a 69 44 69 76 69 73 6f 72 2b 31 20  ** N*iDivisor+1 
102bd 61 6e 64 20 28 4e 2b 31 29 2a 69 44 69 76 69 73  and (N+1)*iDivis
102be 6f 72 2e 20 20 45 61 63 68 20 73 75 62 62 69 74  or.  Each subbit
102bf 6d 61 70 20 69 73 20 6e 6f 72 6d 61 6c 69 7a 65  map is normalize
102c0 64 0a 2a 2a 20 74 6f 20 68 6f 6c 64 20 64 65 61  d.** to hold dea
102c1 6c 20 77 69 74 68 20 76 61 6c 75 65 73 20 62 65  l with values be
102c2 74 77 65 65 6e 20 31 20 61 6e 64 20 69 44 69 76  tween 1 and iDiv
102c3 69 73 6f 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  isor..*/.struct 
102c4 42 69 74 76 65 63 20 7b 0a 20 20 75 33 32 20 69  Bitvec {.  u32 i
102c5 53 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4d 61  Size;      /* Ma
102c6 78 69 6d 75 6d 20 62 69 74 20 69 6e 64 65 78 2e  ximum bit index.
102c7 20 20 4d 61 78 20 69 53 69 7a 65 20 69 73 20 34    Max iSize is 4
102c8 2c 32 39 34 2c 39 36 37 2c 32 39 36 2e 20 2a 2f  ,294,967,296. */
102c9 0a 20 20 75 33 32 20 6e 53 65 74 3b 20 20 20 20  .  u32 nSet;    
102ca 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
102cb 62 69 74 73 20 74 68 61 74 20 61 72 65 20 73 65  bits that are se
102cc 74 20 2d 20 6f 6e 6c 79 20 76 61 6c 69 64 20 66  t - only valid f
102cd 6f 72 20 61 48 61 73 68 0a 20 20 20 20 20 20 20  or aHash.       
102ce 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 65 6c             ** el
102cf 65 6d 65 6e 74 2e 20 20 4d 61 78 20 69 73 20 42  ement.  Max is B
102d0 49 54 56 45 43 5f 4e 49 4e 54 2e 20 20 46 6f 72  ITVEC_NINT.  For
102d1 20 42 49 54 56 45 43 5f 53 5a 20 6f 66 20 35 31   BITVEC_SZ of 51
102d2 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  2,.             
102d3 20 20 20 20 20 2a 2a 20 74 68 69 73 20 77 6f 75       ** this wou
102d4 6c 64 20 62 65 20 31 32 35 2e 20 2a 2f 0a 20 20  ld be 125. */.  
102d5 75 33 32 20 69 44 69 76 69 73 6f 72 3b 20 20 20  u32 iDivisor;   
102d6 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 69 74  /* Number of bit
102d7 73 20 68 61 6e 64 6c 65 64 20 62 79 20 65 61 63  s handled by eac
102d8 68 20 61 70 53 75 62 5b 5d 20 65 6e 74 72 79 2e  h apSub[] entry.
102d9 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
102da 20 20 20 20 20 20 2f 2a 20 53 68 6f 75 6c 64 20        /* Should 
102db 3e 3d 30 20 66 6f 72 20 61 70 53 75 62 20 65 6c  >=0 for apSub el
102dc 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 20 20  ement. */.      
102dd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
102de 61 78 20 69 44 69 76 69 73 6f 72 20 69 73 20 6d  ax iDivisor is m
102df 61 78 28 75 33 32 29 20 2f 20 42 49 54 56 45 43  ax(u32) / BITVEC
102e0 5f 4e 50 54 52 20 2b 20 31 2e 20 20 2a 2f 0a 20  _NPTR + 1.  */. 
102e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
102e2 20 2f 2a 20 46 6f 72 20 61 20 42 49 54 56 45 43   /* For a BITVEC
102e3 5f 53 5a 20 6f 66 20 35 31 32 2c 20 74 68 69 73  _SZ of 512, this
102e4 20 77 6f 75 6c 64 20 62 65 20 33 34 2c 33 35 39   would be 34,359
102e5 2c 37 33 39 2e 20 2a 2f 0a 20 20 75 6e 69 6f 6e  ,739. */.  union
102e6 20 7b 0a 20 20 20 20 42 49 54 56 45 43 5f 54 45   {.    BITVEC_TE
102e7 4c 45 4d 20 61 42 69 74 6d 61 70 5b 42 49 54 56  LEM aBitmap[BITV
102e8 45 43 5f 4e 45 4c 45 4d 5d 3b 20 20 20 20 2f 2a  EC_NELEM];    /*
102e9 20 42 69 74 6d 61 70 20 72 65 70 72 65 73 65 6e   Bitmap represen
102ea 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 75 33  tation */.    u3
102eb 32 20 61 48 61 73 68 5b 42 49 54 56 45 43 5f 4e  2 aHash[BITVEC_N
102ec 49 4e 54 5d 3b 20 20 20 20 20 20 2f 2a 20 48 61  INT];      /* Ha
102ed 73 68 20 74 61 62 6c 65 20 72 65 70 72 65 73 65  sh table represe
102ee 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 42  ntation */.    B
102ef 69 74 76 65 63 20 2a 61 70 53 75 62 5b 42 49 54  itvec *apSub[BIT
102f0 56 45 43 5f 4e 50 54 52 5d 3b 20 20 2f 2a 20 52  VEC_NPTR];  /* R
102f1 65 63 75 72 73 69 76 65 20 72 65 70 72 65 73 65  ecursive represe
102f2 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 7d 20 75  ntation */.  } u
102f3 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61  ;.};../*.** Crea
102f4 74 65 20 61 20 6e 65 77 20 62 69 74 6d 61 70 20  te a new bitmap 
102f5 6f 62 6a 65 63 74 20 61 62 6c 65 20 74 6f 20 68  object able to h
102f6 61 6e 64 6c 65 20 62 69 74 73 20 62 65 74 77 65  andle bits betwe
102f7 65 6e 20 30 20 61 6e 64 20 69 53 69 7a 65 2c 0a  en 0 and iSize,.
102f8 2a 2a 20 69 6e 63 6c 75 73 69 76 65 2e 20 20 52  ** inclusive.  R
102f9 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
102fa 74 6f 20 74 68 65 20 6e 65 77 20 6f 62 6a 65 63  to the new objec
102fb 74 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20  t.  Return NULL 
102fc 69 66 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61  if .** malloc fa
102fd 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ils..*/.SQLITE_P
102fe 52 49 56 41 54 45 20 42 69 74 76 65 63 20 2a 73  RIVATE Bitvec *s
102ff 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61  qlite3BitvecCrea
10300 74 65 28 75 33 32 20 69 53 69 7a 65 29 7b 0a 20  te(u32 iSize){. 
10301 20 42 69 74 76 65 63 20 2a 70 3b 0a 20 20 61 73   Bitvec *p;.  as
10302 73 65 72 74 28 20 73 69 7a 65 6f 66 28 2a 70 29  sert( sizeof(*p)
10303 3d 3d 42 49 54 56 45 43 5f 53 5a 20 29 3b 0a 20  ==BITVEC_SZ );. 
10304 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c   p = sqlite3Mall
10305 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a  ocZero( sizeof(*
10306 70 29 20 29 3b 0a 20 20 69 66 28 20 70 20 29 7b  p) );.  if( p ){
10307 0a 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20  .    p->iSize = 
10308 69 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74  iSize;.  }.  ret
10309 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
1030a 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
1030b 74 68 65 20 69 2d 74 68 20 62 69 74 20 69 73 20  the i-th bit is 
1030c 73 65 74 2e 20 20 52 65 74 75 72 6e 20 74 72 75  set.  Return tru
1030d 65 20 6f 72 20 66 61 6c 73 65 2e 0a 2a 2a 20 49  e or false..** I
1030e 66 20 70 20 69 73 20 4e 55 4c 4c 20 28 69 66 20  f p is NULL (if 
1030f 74 68 65 20 62 69 74 6d 61 70 20 68 61 73 20 6e  the bitmap has n
10310 6f 74 20 62 65 65 6e 20 63 72 65 61 74 65 64 29  ot been created)
10311 20 6f 72 20 69 66 0a 2a 2a 20 69 20 69 73 20 6f   or if.** i is o
10312 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 65  ut of range, the
10313 6e 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a  n return false..
10314 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
10315 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74  E int sqlite3Bit
10316 76 65 63 54 65 73 74 28 42 69 74 76 65 63 20 2a  vecTest(Bitvec *
10317 70 2c 20 75 33 32 20 69 29 7b 0a 20 20 69 66 28  p, u32 i){.  if(
10318 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   p==0 ) return 0
10319 3b 0a 20 20 69 66 28 20 69 3e 70 2d 3e 69 53 69  ;.  if( i>p->iSi
1031a 7a 65 20 7c 7c 20 69 3d 3d 30 20 29 20 72 65 74  ze || i==0 ) ret
1031b 75 72 6e 20 30 3b 0a 20 20 69 2d 2d 3b 0a 20 20  urn 0;.  i--;.  
1031c 77 68 69 6c 65 28 20 70 2d 3e 69 44 69 76 69 73  while( p->iDivis
1031d 6f 72 20 29 7b 0a 20 20 20 20 75 33 32 20 62 69  or ){.    u32 bi
1031e 6e 20 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f  n = i/p->iDiviso
1031f 72 3b 0a 20 20 20 20 69 20 3d 20 69 25 70 2d 3e  r;.    i = i%p->
10320 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 70 20  iDivisor;.    p 
10321 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e  = p->u.apSub[bin
10322 5d 3b 0a 20 20 20 20 69 66 20 28 21 70 29 20 7b  ];.    if (!p) {
10323 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
10324 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
10325 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45   p->iSize<=BITVE
10326 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20 20 72 65  C_NBIT ){.    re
10327 74 75 72 6e 20 28 70 2d 3e 75 2e 61 42 69 74 6d  turn (p->u.aBitm
10328 61 70 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45 4c  ap[i/BITVEC_SZEL
10329 45 4d 5d 20 26 20 28 31 3c 3c 28 69 26 28 42 49  EM] & (1<<(i&(BI
1032a 54 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 29  TVEC_SZELEM-1)))
1032b 29 21 3d 30 3b 0a 20 20 7d 20 65 6c 73 65 7b 0a  )!=0;.  } else{.
1032c 20 20 20 20 75 33 32 20 68 20 3d 20 42 49 54 56      u32 h = BITV
1032d 45 43 5f 48 41 53 48 28 69 2b 2b 29 3b 0a 20 20  EC_HASH(i++);.  
1032e 20 20 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48    while( p->u.aH
1032f 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 20 20  ash[h] ){.      
10330 69 66 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68  if( p->u.aHash[h
10331 5d 3d 3d 69 20 29 20 72 65 74 75 72 6e 20 31 3b  ]==i ) return 1;
10332 0a 20 20 20 20 20 20 68 20 3d 20 28 68 2b 31 29  .      h = (h+1)
10333 20 25 20 42 49 54 56 45 43 5f 4e 49 4e 54 3b 0a   % BITVEC_NINT;.
10334 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
10335 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   0;.  }.}../*.**
10336 20 53 65 74 20 74 68 65 20 69 2d 74 68 20 62 69   Set the i-th bi
10337 74 2e 20 20 52 65 74 75 72 6e 20 30 20 6f 6e 20  t.  Return 0 on 
10338 73 75 63 63 65 73 73 20 61 6e 64 20 61 6e 20 65  success and an e
10339 72 72 6f 72 20 63 6f 64 65 20 69 66 0a 2a 2a 20  rror code if.** 
1033a 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72  anything goes wr
1033b 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ong..**.** This 
1033c 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 63 61  routine might ca
1033d 75 73 65 20 73 75 62 2d 62 69 74 6d 61 70 73 20  use sub-bitmaps 
1033e 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e  to be allocated.
1033f 20 20 46 61 69 6c 69 6e 67 0a 2a 2a 20 74 6f 20    Failing.** to 
10340 67 65 74 20 74 68 65 20 6d 65 6d 6f 72 79 20 6e  get the memory n
10341 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 74 68  eeded to hold th
10342 65 20 73 75 62 2d 62 69 74 6d 61 70 20 69 73 20  e sub-bitmap is 
10343 74 68 65 20 6f 6e 6c 79 0a 2a 2a 20 74 68 61 74  the only.** that
10344 20 63 61 6e 20 67 6f 20 77 72 6f 6e 67 20 77 69   can go wrong wi
10345 74 68 20 61 6e 20 69 6e 73 65 72 74 2c 20 61 73  th an insert, as
10346 73 75 6d 69 6e 67 20 70 20 61 6e 64 20 69 20 61  suming p and i a
10347 72 65 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20  re valid..**.** 
10348 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63  The calling func
10349 74 69 6f 6e 20 6d 75 73 74 20 65 6e 73 75 72 65  tion must ensure
1034a 20 74 68 61 74 20 70 20 69 73 20 61 20 76 61 6c   that p is a val
1034b 69 64 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74  id Bitvec object
1034c 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74 68 65  .** and that the
1034d 20 76 61 6c 75 65 20 66 6f 72 20 22 69 22 20 69   value for "i" i
1034e 73 20 77 69 74 68 69 6e 20 72 61 6e 67 65 20 6f  s within range o
1034f 66 20 74 68 65 20 42 69 74 76 65 63 20 6f 62 6a  f the Bitvec obj
10350 65 63 74 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ect..** Otherwis
10351 65 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69  e the behavior i
10352 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a  s undefined..*/.
10353 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
10354 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  nt sqlite3Bitvec
10355 53 65 74 28 42 69 74 76 65 63 20 2a 70 2c 20 75  Set(Bitvec *p, u
10356 33 32 20 69 29 7b 0a 20 20 75 33 32 20 68 3b 0a  32 i){.  u32 h;.
10357 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
10358 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
10359 20 61 73 73 65 72 74 28 20 69 3e 30 20 29 3b 0a   assert( i>0 );.
1035a 20 20 61 73 73 65 72 74 28 20 69 3c 3d 70 2d 3e    assert( i<=p->
1035b 69 53 69 7a 65 20 29 3b 0a 20 20 69 2d 2d 3b 0a  iSize );.  i--;.
1035c 20 20 77 68 69 6c 65 28 28 70 2d 3e 69 53 69 7a    while((p->iSiz
1035d 65 20 3e 20 42 49 54 56 45 43 5f 4e 42 49 54 29  e > BITVEC_NBIT)
1035e 20 26 26 20 70 2d 3e 69 44 69 76 69 73 6f 72 29   && p->iDivisor)
1035f 20 7b 0a 20 20 20 20 75 33 32 20 62 69 6e 20 3d   {.    u32 bin =
10360 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a   i/p->iDivisor;.
10361 20 20 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 69      i = i%p->iDi
10362 76 69 73 6f 72 3b 0a 20 20 20 20 69 66 28 20 70  visor;.    if( p
10363 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3d 3d  ->u.apSub[bin]==
10364 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 75 2e  0 ){.      p->u.
10365 61 70 53 75 62 5b 62 69 6e 5d 20 3d 20 73 71 6c  apSub[bin] = sql
10366 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65  ite3BitvecCreate
10367 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 3b  ( p->iDivisor );
10368 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 75 2e  .      if( p->u.
10369 61 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 20  apSub[bin]==0 ) 
1036a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
1036b 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  MEM;.    }.    p
1036c 20 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69   = p->u.apSub[bi
1036d 6e 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  n];.  }.  if( p-
1036e 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e  >iSize<=BITVEC_N
1036f 42 49 54 20 29 7b 0a 20 20 20 20 70 2d 3e 75 2e  BIT ){.    p->u.
10370 61 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45 43  aBitmap[i/BITVEC
10371 5f 53 5a 45 4c 45 4d 5d 20 7c 3d 20 31 20 3c 3c  _SZELEM] |= 1 <<
10372 20 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45 4c   (i&(BITVEC_SZEL
10373 45 4d 2d 31 29 29 3b 0a 20 20 20 20 72 65 74 75  EM-1));.    retu
10374 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
10375 7d 0a 20 20 68 20 3d 20 42 49 54 56 45 43 5f 48  }.  h = BITVEC_H
10376 41 53 48 28 69 2b 2b 29 3b 0a 20 20 2f 2a 20 69  ASH(i++);.  /* i
10377 66 20 74 68 65 72 65 20 77 61 73 6e 27 74 20 61  f there wasn't a
10378 20 68 61 73 68 20 63 6f 6c 6c 69 73 69 6f 6e 2c   hash collision,
10379 20 61 6e 64 20 74 68 69 73 20 64 6f 65 73 6e 27   and this doesn'
1037a 74 20 2a 2f 0a 20 20 2f 2a 20 63 6f 6d 70 6c 65  t */.  /* comple
1037b 74 65 6c 79 20 66 69 6c 6c 20 74 68 65 20 68 61  tely fill the ha
1037c 73 68 2c 20 74 68 65 6e 20 6a 75 73 74 20 61 64  sh, then just ad
1037d 64 20 69 74 20 77 69 74 68 6f 75 74 20 2a 2f 0a  d it without */.
1037e 20 20 2f 2a 20 77 6f 72 72 69 6e 67 20 61 62 6f    /* worring abo
1037f 75 74 20 73 75 62 2d 64 69 76 69 64 69 6e 67 20  ut sub-dividing 
10380 61 6e 64 20 72 65 2d 68 61 73 68 69 6e 67 2e 20  and re-hashing. 
10381 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e 75 2e 61  */.  if( !p->u.a
10382 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 69  Hash[h] ){.    i
10383 66 20 28 70 2d 3e 6e 53 65 74 3c 28 42 49 54 56  f (p->nSet<(BITV
10384 45 43 5f 4e 49 4e 54 2d 31 29 29 20 7b 0a 20 20  EC_NINT-1)) {.  
10385 20 20 20 20 67 6f 74 6f 20 62 69 74 76 65 63 5f      goto bitvec_
10386 73 65 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 20 65  set_end;.    } e
10387 6c 73 65 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f  lse {.      goto
10388 20 62 69 74 76 65 63 5f 73 65 74 5f 72 65 68 61   bitvec_set_reha
10389 73 68 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  sh;.    }.  }.  
1038a 2f 2a 20 74 68 65 72 65 20 77 61 73 20 61 20 63  /* there was a c
1038b 6f 6c 6c 69 73 69 6f 6e 2c 20 63 68 65 63 6b 20  ollision, check 
1038c 74 6f 20 73 65 65 20 69 66 20 69 74 27 73 20 61  to see if it's a
1038d 6c 72 65 61 64 79 20 2a 2f 0a 20 20 2f 2a 20 69  lready */.  /* i
1038e 6e 20 68 61 73 68 2c 20 69 66 20 6e 6f 74 2c 20  n hash, if not, 
1038f 74 72 79 20 74 6f 20 66 69 6e 64 20 61 20 73 70  try to find a sp
10390 6f 74 20 66 6f 72 20 69 74 20 2a 2f 0a 20 20 64  ot for it */.  d
10391 6f 20 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75  o {.    if( p->u
10392 2e 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20 72  .aHash[h]==i ) r
10393 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
10394 0a 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20 69 66  .    h++;.    if
10395 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54  ( h>=BITVEC_NINT
10396 20 29 20 68 20 3d 20 30 3b 0a 20 20 7d 20 77 68   ) h = 0;.  } wh
10397 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b  ile( p->u.aHash[
10398 68 5d 20 29 3b 0a 20 20 2f 2a 20 77 65 20 64 69  h] );.  /* we di
10399 64 6e 27 74 20 66 69 6e 64 20 69 74 20 69 6e 20  dn't find it in 
1039a 74 68 65 20 68 61 73 68 2e 20 20 68 20 70 6f 69  the hash.  h poi
1039b 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 74  nts to the first
1039c 20 2a 2f 0a 20 20 2f 2a 20 61 76 61 69 6c 61 62   */.  /* availab
1039d 6c 65 20 66 72 65 65 20 73 70 6f 74 2e 20 63 68  le free spot. ch
1039e 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68  eck to see if th
1039f 69 73 20 69 73 20 67 6f 69 6e 67 20 74 6f 20 2a  is is going to *
103a0 2f 0a 20 20 2f 2a 20 6d 61 6b 65 20 6f 75 72 20  /.  /* make our 
103a1 68 61 73 68 20 74 6f 6f 20 22 66 75 6c 6c 22 2e  hash too "full".
103a2 20 20 2a 2f 0a 62 69 74 76 65 63 5f 73 65 74 5f    */.bitvec_set_
103a3 72 65 68 61 73 68 3a 0a 20 20 69 66 28 20 70 2d  rehash:.  if( p-
103a4 3e 6e 53 65 74 3e 3d 42 49 54 56 45 43 5f 4d 58  >nSet>=BITVEC_MX
103a5 48 41 53 48 20 29 7b 0a 20 20 20 20 75 6e 73 69  HASH ){.    unsi
103a6 67 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20 20 20  gned int j;.    
103a7 69 6e 74 20 72 63 3b 0a 20 20 20 20 75 33 32 20  int rc;.    u32 
103a8 2a 61 69 56 61 6c 75 65 73 20 3d 20 73 71 6c 69  *aiValues = sqli
103a9 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77  te3StackAllocRaw
103aa 28 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e  (0, sizeof(p->u.
103ab 61 48 61 73 68 29 29 3b 0a 20 20 20 20 69 66 28  aHash));.    if(
103ac 20 61 69 56 61 6c 75 65 73 3d 3d 30 20 29 7b 0a   aiValues==0 ){.
103ad 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
103ae 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
103af 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63  else{.      memc
103b0 70 79 28 61 69 56 61 6c 75 65 73 2c 20 70 2d 3e  py(aiValues, p->
103b1 75 2e 61 48 61 73 68 2c 20 73 69 7a 65 6f 66 28  u.aHash, sizeof(
103b2 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20 20  p->u.aHash));.  
103b3 20 20 20 20 6d 65 6d 73 65 74 28 70 2d 3e 75 2e      memset(p->u.
103b4 61 70 53 75 62 2c 20 30 2c 20 73 69 7a 65 6f 66  apSub, 0, sizeof
103b5 28 70 2d 3e 75 2e 61 70 53 75 62 29 29 3b 0a 20  (p->u.apSub));. 
103b6 20 20 20 20 20 70 2d 3e 69 44 69 76 69 73 6f 72       p->iDivisor
103b7 20 3d 20 28 70 2d 3e 69 53 69 7a 65 20 2b 20 42   = (p->iSize + B
103b8 49 54 56 45 43 5f 4e 50 54 52 20 2d 20 31 29 2f  ITVEC_NPTR - 1)/
103b9 42 49 54 56 45 43 5f 4e 50 54 52 3b 0a 20 20 20  BITVEC_NPTR;.   
103ba 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
103bb 69 74 76 65 63 53 65 74 28 70 2c 20 69 29 3b 0a  itvecSet(p, i);.
103bc 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
103bd 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b  <BITVEC_NINT; j+
103be 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
103bf 61 69 56 61 6c 75 65 73 5b 6a 5d 20 29 20 72 63  aiValues[j] ) rc
103c0 20 7c 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65   |= sqlite3Bitve
103c1 63 53 65 74 28 70 2c 20 61 69 56 61 6c 75 65 73  cSet(p, aiValues
103c2 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  [j]);.      }.  
103c3 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b      sqlite3Stack
103c4 46 72 65 65 28 30 2c 20 61 69 56 61 6c 75 65 73  Free(0, aiValues
103c5 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
103c6 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 62 69  rc;.    }.  }.bi
103c7 74 76 65 63 5f 73 65 74 5f 65 6e 64 3a 0a 20 20  tvec_set_end:.  
103c8 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20 70 2d 3e  p->nSet++;.  p->
103c9 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20 69 3b 0a  u.aHash[h] = i;.
103ca 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
103cb 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65  OK;.}../*.** Cle
103cc 61 72 20 74 68 65 20 69 2d 74 68 20 62 69 74 2e  ar the i-th bit.
103cd 0a 2a 2a 0a 2a 2a 20 70 42 75 66 20 6d 75 73 74  .**.** pBuf must
103ce 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   be a pointer to
103cf 20 61 74 20 6c 65 61 73 74 20 42 49 54 56 45 43   at least BITVEC
103d0 5f 53 5a 20 62 79 74 65 73 20 6f 66 20 74 65 6d  _SZ bytes of tem
103d1 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65 0a 2a  porary storage.*
103d2 2a 20 74 68 61 74 20 42 69 74 76 65 63 43 6c 65  * that BitvecCle
103d3 61 72 20 63 61 6e 20 75 73 65 20 74 6f 20 72 65  ar can use to re
103d4 62 75 69 6c 74 20 69 74 73 20 68 61 73 68 20 74  built its hash t
103d5 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  able..*/.SQLITE_
103d6 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
103d7 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 72 28  ite3BitvecClear(
103d8 42 69 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69  Bitvec *p, u32 i
103d9 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20  , void *pBuf){. 
103da 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
103db 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e  rn;.  assert( i>
103dc 30 20 29 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68  0 );.  i--;.  wh
103dd 69 6c 65 28 20 70 2d 3e 69 44 69 76 69 73 6f 72  ile( p->iDivisor
103de 20 29 7b 0a 20 20 20 20 75 33 32 20 62 69 6e 20   ){.    u32 bin 
103df 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b  = i/p->iDivisor;
103e0 0a 20 20 20 20 69 20 3d 20 69 25 70 2d 3e 69 44  .    i = i%p->iD
103e1 69 76 69 73 6f 72 3b 0a 20 20 20 20 70 20 3d 20  ivisor;.    p = 
103e2 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3b  p->u.apSub[bin];
103e3 0a 20 20 20 20 69 66 20 28 21 70 29 20 7b 0a 20  .    if (!p) {. 
103e4 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
103e5 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e   }.  }.  if( p->
103e6 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42  iSize<=BITVEC_NB
103e7 49 54 20 29 7b 0a 20 20 20 20 70 2d 3e 75 2e 61  IT ){.    p->u.a
103e8 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45 43 5f  Bitmap[i/BITVEC_
103e9 53 5a 45 4c 45 4d 5d 20 26 3d 20 7e 28 31 20 3c  SZELEM] &= ~(1 <
103ea 3c 20 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45  < (i&(BITVEC_SZE
103eb 4c 45 4d 2d 31 29 29 29 3b 0a 20 20 7d 65 6c 73  LEM-1)));.  }els
103ec 65 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  e{.    unsigned 
103ed 69 6e 74 20 6a 3b 0a 20 20 20 20 75 33 32 20 2a  int j;.    u32 *
103ee 61 69 56 61 6c 75 65 73 20 3d 20 70 42 75 66 3b  aiValues = pBuf;
103ef 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 69 56 61  .    memcpy(aiVa
103f0 6c 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73 68  lues, p->u.aHash
103f1 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 48  , sizeof(p->u.aH
103f2 61 73 68 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65  ash));.    memse
103f3 74 28 70 2d 3e 75 2e 61 48 61 73 68 2c 20 30 2c  t(p->u.aHash, 0,
103f4 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 48 61   sizeof(p->u.aHa
103f5 73 68 29 29 3b 0a 20 20 20 20 70 2d 3e 6e 53 65  sh));.    p->nSe
103f6 74 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 6a  t = 0;.    for(j
103f7 3d 30 3b 20 6a 3c 42 49 54 56 45 43 5f 4e 49 4e  =0; j<BITVEC_NIN
103f8 54 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69  T; j++){.      i
103f9 66 28 20 61 69 56 61 6c 75 65 73 5b 6a 5d 20 26  f( aiValues[j] &
103fa 26 20 61 69 56 61 6c 75 65 73 5b 6a 5d 21 3d 28  & aiValues[j]!=(
103fb 69 2b 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20  i+1) ){.        
103fc 75 33 32 20 68 20 3d 20 42 49 54 56 45 43 5f 48  u32 h = BITVEC_H
103fd 41 53 48 28 61 69 56 61 6c 75 65 73 5b 6a 5d 2d  ASH(aiValues[j]-
103fe 31 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e  1);.        p->n
103ff 53 65 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77  Set++;.        w
10400 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68  hile( p->u.aHash
10401 5b 68 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20  [h] ){.         
10402 20 68 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20   h++;.          
10403 69 66 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49  if( h>=BITVEC_NI
10404 4e 54 20 29 20 68 20 3d 20 30 3b 0a 20 20 20 20  NT ) h = 0;.    
10405 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 2d      }.        p-
10406 3e 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20 61 69  >u.aHash[h] = ai
10407 56 61 6c 75 65 73 5b 6a 5d 3b 0a 20 20 20 20 20  Values[j];.     
10408 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a   }.    }.  }.}..
10409 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20  /*.** Destroy a 
1040a 62 69 74 6d 61 70 20 6f 62 6a 65 63 74 2e 20 20  bitmap object.  
1040b 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f  Reclaim all memo
1040c 72 79 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49  ry used..*/.SQLI
1040d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1040e 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73  sqlite3BitvecDes
1040f 74 72 6f 79 28 42 69 74 76 65 63 20 2a 70 29 7b  troy(Bitvec *p){
10410 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
10411 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 69  turn;.  if( p->i
10412 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 75  Divisor ){.    u
10413 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20  nsigned int i;. 
10414 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 42 49     for(i=0; i<BI
10415 54 56 45 43 5f 4e 50 54 52 3b 20 69 2b 2b 29 7b  TVEC_NPTR; i++){
10416 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69  .      sqlite3Bi
10417 74 76 65 63 44 65 73 74 72 6f 79 28 70 2d 3e 75  tvecDestroy(p->u
10418 2e 61 70 53 75 62 5b 69 5d 29 3b 0a 20 20 20 20  .apSub[i]);.    
10419 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  }.  }.  sqlite3_
1041a 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  free(p);.}../*.*
1041b 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c  * Return the val
1041c 75 65 20 6f 66 20 74 68 65 20 69 53 69 7a 65 20  ue of the iSize 
1041d 70 61 72 61 6d 65 74 65 72 20 73 70 65 63 69 66  parameter specif
1041e 69 65 64 20 77 68 65 6e 20 42 69 74 76 65 63 20  ied when Bitvec 
1041f 2a 70 0a 2a 2a 20 77 61 73 20 63 72 65 61 74 65  *p.** was create
10420 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
10421 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33  VATE u32 sqlite3
10422 42 69 74 76 65 63 53 69 7a 65 28 42 69 74 76 65  BitvecSize(Bitve
10423 63 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20  c *p){.  return 
10424 70 2d 3e 69 53 69 7a 65 3b 0a 7d 0a 0a 23 69 66  p->iSize;.}..#if
10425 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
10426 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a  _BUILTIN_TEST./*
10427 0a 2a 2a 20 4c 65 74 20 56 5b 5d 20 62 65 20 61  .** Let V[] be a
10428 6e 20 61 72 72 61 79 20 6f 66 20 75 6e 73 69 67  n array of unsig
10429 6e 65 64 20 63 68 61 72 61 63 74 65 72 73 20 73  ned characters s
1042a 75 66 66 69 63 69 65 6e 74 20 74 6f 20 68 6f 6c  ufficient to hol
1042b 64 0a 2a 2a 20 75 70 20 74 6f 20 4e 20 62 69 74  d.** up to N bit
1042c 73 2e 20 20 4c 65 74 20 49 20 62 65 20 61 6e 20  s.  Let I be an 
1042d 69 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e 20  integer between 
1042e 30 20 61 6e 64 20 4e 2e 20 20 30 3c 3d 49 3c 4e  0 and N.  0<=I<N
1042f 2e 0a 2a 2a 20 54 68 65 6e 20 74 68 65 20 66 6f  ..** Then the fo
10430 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 63  llowing macros c
10431 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65  an be used to se
10432 74 2c 20 63 6c 65 61 72 2c 20 6f 72 20 74 65 73  t, clear, or tes
10433 74 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20  t.** individual 
10434 62 69 74 73 20 77 69 74 68 69 6e 20 56 2e 0a 2a  bits within V..*
10435 2f 0a 23 64 65 66 69 6e 65 20 53 45 54 42 49 54  /.#define SETBIT
10436 28 56 2c 49 29 20 20 20 20 20 20 56 5b 49 3e 3e  (V,I)      V[I>>
10437 33 5d 20 7c 3d 20 28 31 3c 3c 28 49 26 37 29 29  3] |= (1<<(I&7))
10438 0a 23 64 65 66 69 6e 65 20 43 4c 45 41 52 42 49  .#define CLEARBI
10439 54 28 56 2c 49 29 20 20 20 20 56 5b 49 3e 3e 33  T(V,I)    V[I>>3
1043a 5d 20 26 3d 20 7e 28 31 3c 3c 28 49 26 37 29 29  ] &= ~(1<<(I&7))
1043b 0a 23 64 65 66 69 6e 65 20 54 45 53 54 42 49 54  .#define TESTBIT
1043c 28 56 2c 49 29 20 20 20 20 20 28 56 5b 49 3e 3e  (V,I)     (V[I>>
1043d 33 5d 26 28 31 3c 3c 28 49 26 37 29 29 29 21 3d  3]&(1<<(I&7)))!=
1043e 30 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  0../*.** This ro
1043f 75 74 69 6e 65 20 72 75 6e 73 20 61 6e 20 65 78  utine runs an ex
10440 74 65 6e 73 69 76 65 20 74 65 73 74 20 6f 66 20  tensive test of 
10441 74 68 65 20 42 69 74 76 65 63 20 63 6f 64 65 2e  the Bitvec code.
10442 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74  .**.** The input
10443 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20   is an array of 
10444 69 6e 74 65 67 65 72 73 20 74 68 61 74 20 61 63  integers that ac
10445 74 73 20 61 73 20 61 20 70 72 6f 67 72 61 6d 0a  ts as a program.
10446 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 65 20 42  ** to test the B
10447 69 74 76 65 63 2e 20 20 54 68 65 20 69 6e 74 65  itvec.  The inte
10448 67 65 72 73 20 61 72 65 20 6f 70 63 6f 64 65 73  gers are opcodes
10449 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79 20   followed.** by 
1044a 30 2c 20 31 2c 20 6f 72 20 33 20 6f 70 65 72 61  0, 1, or 3 opera
1044b 6e 64 73 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f  nds, depending o
1044c 6e 20 74 68 65 20 6f 70 63 6f 64 65 2e 20 20 41  n the opcode.  A
1044d 6e 6f 74 68 65 72 0a 2a 2a 20 6f 70 63 6f 64 65  nother.** opcode
1044e 20 66 6f 6c 6c 6f 77 73 20 69 6d 6d 65 64 69 61   follows immedia
1044f 74 65 6c 79 20 61 66 74 65 72 20 74 68 65 20 6c  tely after the l
10450 61 73 74 20 6f 70 65 72 61 6e 64 2e 0a 2a 2a 0a  ast operand..**.
10451 2a 2a 20 54 68 65 72 65 20 61 72 65 20 36 20 6f  ** There are 6 o
10452 70 63 6f 64 65 73 20 6e 75 6d 62 65 72 65 64 20  pcodes numbered 
10453 66 72 6f 6d 20 30 20 74 68 72 6f 75 67 68 20 35  from 0 through 5
10454 2e 20 20 30 20 69 73 20 74 68 65 0a 2a 2a 20 22  .  0 is the.** "
10455 68 61 6c 74 22 20 6f 70 63 6f 64 65 20 61 6e 64  halt" opcode and
10456 20 63 61 75 73 65 73 20 74 68 65 20 74 65 73 74   causes the test
10457 20 74 6f 20 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20   to end..**.**  
10458 20 20 30 20 20 20 20 20 20 20 20 20 20 48 61 6c    0          Hal
10459 74 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65  t and return the
1045a 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72   number of error
1045b 73 0a 2a 2a 20 20 20 20 31 20 4e 20 53 20 58 20  s.**    1 N S X 
1045c 20 20 20 53 65 74 20 4e 20 62 69 74 73 20 62 65     Set N bits be
1045d 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53 20 61  ginning with S a
1045e 6e 64 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20  nd incrementing 
1045f 62 79 20 58 0a 2a 2a 20 20 20 20 32 20 4e 20 53  by X.**    2 N S
10460 20 58 20 20 20 20 43 6c 65 61 72 20 4e 20 62 69   X    Clear N bi
10461 74 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74  ts beginning wit
10462 68 20 53 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e  h S and incremen
10463 74 69 6e 67 20 62 79 20 58 0a 2a 2a 20 20 20 20  ting by X.**    
10464 33 20 4e 20 20 20 20 20 20 20 20 53 65 74 20 4e  3 N        Set N
10465 20 72 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e   randomly chosen
10466 20 62 69 74 73 0a 2a 2a 20 20 20 20 34 20 4e 20   bits.**    4 N 
10467 20 20 20 20 20 20 20 43 6c 65 61 72 20 4e 20 72         Clear N r
10468 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62  andomly chosen b
10469 69 74 73 0a 2a 2a 20 20 20 20 35 20 4e 20 53 20  its.**    5 N S 
1046a 58 20 20 20 20 53 65 74 20 4e 20 62 69 74 73 20  X    Set N bits 
1046b 66 72 6f 6d 20 53 20 69 6e 63 72 65 6d 65 6e 74  from S increment
1046c 20 58 20 69 6e 20 61 72 72 61 79 20 6f 6e 6c 79   X in array only
1046d 2c 20 6e 6f 74 20 69 6e 20 62 69 74 76 65 63 0a  , not in bitvec.
1046e 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 63 6f 64 65  **.** The opcode
1046f 73 20 31 20 74 68 72 6f 75 67 68 20 34 20 70 65  s 1 through 4 pe
10470 72 66 6f 72 6d 20 73 65 74 20 61 6e 64 20 63 6c  rform set and cl
10471 65 61 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 61  ear operations a
10472 72 65 20 70 65 72 66 6f 72 6d 65 64 0a 2a 2a 20  re performed.** 
10473 6f 6e 20 62 6f 74 68 20 61 20 42 69 74 76 65 63  on both a Bitvec
10474 20 6f 62 6a 65 63 74 20 61 6e 64 20 6f 6e 20 61   object and on a
10475 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 6f 66   linear array of
10476 20 62 69 74 73 20 6f 62 74 61 69 6e 65 64 20 66   bits obtained f
10477 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 20 4f  rom malloc..** O
10478 70 63 6f 64 65 20 35 20 77 6f 72 6b 73 20 6f 6e  pcode 5 works on
10479 20 74 68 65 20 6c 69 6e 65 61 72 20 61 72 72 61   the linear arra
1047a 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20 6f 6e 20 74  y only, not on t
1047b 68 65 20 42 69 74 76 65 63 2e 0a 2a 2a 20 4f 70  he Bitvec..** Op
1047c 63 6f 64 65 20 35 20 69 73 20 75 73 65 64 20 74  code 5 is used t
1047d 6f 20 64 65 6c 69 62 65 72 61 74 65 6c 79 20 69  o deliberately i
1047e 6e 64 75 63 65 20 61 20 66 61 75 6c 74 20 69 6e  nduce a fault in
1047f 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 6f 6e   order to.** con
10480 66 69 72 6d 20 74 68 61 74 20 65 72 72 6f 72 20  firm that error 
10481 64 65 74 65 63 74 69 6f 6e 20 77 6f 72 6b 73 2e  detection works.
10482 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68 65 20 63 6f  .**.** At the co
10483 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 74 68 65 20  nclusion of the 
10484 74 65 73 74 20 74 68 65 20 6c 69 6e 65 61 72 20  test the linear 
10485 61 72 72 61 79 20 69 73 20 63 6f 6d 70 61 72 65  array is compare
10486 64 0a 2a 2a 20 61 67 61 69 6e 73 74 20 74 68 65  d.** against the
10487 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2e 20   Bitvec object. 
10488 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61 6e   If there are an
10489 79 20 64 69 66 66 65 72 65 6e 63 65 73 2c 0a 2a  y differences,.*
1048a 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65  * an error is re
1048b 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68 65 79  turned.  If they
1048c 20 61 72 65 20 74 68 65 20 73 61 6d 65 2c 20 7a   are the same, z
1048d 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ero is returned.
1048e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f  .**.** If a memo
1048f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72  ry allocation er
10490 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75  ror occurs, retu
10491 72 6e 20 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45  rn -1..*/.SQLITE
10492 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
10493 69 74 65 33 42 69 74 76 65 63 42 75 69 6c 74 69  ite3BitvecBuilti
10494 6e 54 65 73 74 28 69 6e 74 20 73 7a 2c 20 69 6e  nTest(int sz, in
10495 74 20 2a 61 4f 70 29 7b 0a 20 20 42 69 74 76 65  t *aOp){.  Bitve
10496 63 20 2a 70 42 69 74 76 65 63 20 3d 20 30 3b 0a  c *pBitvec = 0;.
10497 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
10498 2a 70 56 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  *pV = 0;.  int r
10499 63 20 3d 20 2d 31 3b 0a 20 20 69 6e 74 20 69 2c  c = -1;.  int i,
1049a 20 6e 78 2c 20 70 63 2c 20 6f 70 3b 0a 20 20 76   nx, pc, op;.  v
1049b 6f 69 64 20 2a 70 54 6d 70 53 70 61 63 65 3b 0a  oid *pTmpSpace;.
1049c 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74  .  /* Allocate t
1049d 68 65 20 42 69 74 76 65 63 20 74 6f 20 62 65 20  he Bitvec to be 
1049e 74 65 73 74 65 64 20 61 6e 64 20 61 20 6c 69 6e  tested and a lin
1049f 65 61 72 20 61 72 72 61 79 20 6f 66 0a 20 20 2a  ear array of.  *
104a0 2a 20 62 69 74 73 20 74 6f 20 61 63 74 20 61 73  * bits to act as
104a1 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 2a   the reference *
104a2 2f 0a 20 20 70 42 69 74 76 65 63 20 3d 20 73 71  /.  pBitvec = sq
104a3 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74  lite3BitvecCreat
104a4 65 28 20 73 7a 20 29 3b 0a 20 20 70 56 20 3d 20  e( sz );.  pV = 
104a5 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20  sqlite3_malloc( 
104a6 28 73 7a 2b 37 29 2f 38 20 2b 20 31 20 29 3b 0a  (sz+7)/8 + 1 );.
104a7 20 20 70 54 6d 70 53 70 61 63 65 20 3d 20 73 71    pTmpSpace = sq
104a8 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 42 49 54  lite3_malloc(BIT
104a9 56 45 43 5f 53 5a 29 3b 0a 20 20 69 66 28 20 70  VEC_SZ);.  if( p
104aa 42 69 74 76 65 63 3d 3d 30 20 7c 7c 20 70 56 3d  Bitvec==0 || pV=
104ab 3d 30 20 7c 7c 20 70 54 6d 70 53 70 61 63 65 3d  =0 || pTmpSpace=
104ac 3d 30 20 20 29 20 67 6f 74 6f 20 62 69 74 76 65  =0  ) goto bitve
104ad 63 5f 65 6e 64 3b 0a 20 20 6d 65 6d 73 65 74 28  c_end;.  memset(
104ae 70 56 2c 20 30 2c 20 28 73 7a 2b 37 29 2f 38 20  pV, 0, (sz+7)/8 
104af 2b 20 31 29 3b 0a 0a 20 20 2f 2a 20 4e 55 4c 4c  + 1);..  /* NULL
104b0 20 70 42 69 74 76 65 63 20 74 65 73 74 73 20 2a   pBitvec tests *
104b1 2f 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65  /.  sqlite3Bitve
104b2 63 53 65 74 28 30 2c 20 31 29 3b 0a 20 20 73 71  cSet(0, 1);.  sq
104b3 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 72  lite3BitvecClear
104b4 28 30 2c 20 31 2c 20 70 54 6d 70 53 70 61 63 65  (0, 1, pTmpSpace
104b5 29 3b 0a 0a 20 20 2f 2a 20 52 75 6e 20 74 68 65  );..  /* Run the
104b6 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 70 63   program */.  pc
104b7 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 28   = 0;.  while( (
104b8 6f 70 20 3d 20 61 4f 70 5b 70 63 5d 29 21 3d 30  op = aOp[pc])!=0
104b9 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68 28 20   ){.    switch( 
104ba 6f 70 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65  op ){.      case
104bb 20 31 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32   1:.      case 2
104bc 3a 0a 20 20 20 20 20 20 63 61 73 65 20 35 3a 20  :.      case 5: 
104bd 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 3d 20 34  {.        nx = 4
104be 3b 0a 20 20 20 20 20 20 20 20 69 20 3d 20 61 4f  ;.        i = aO
104bf 70 5b 70 63 2b 32 5d 20 2d 20 31 3b 0a 20 20 20  p[pc+2] - 1;.   
104c0 20 20 20 20 20 61 4f 70 5b 70 63 2b 32 5d 20 2b       aOp[pc+2] +
104c1 3d 20 61 4f 70 5b 70 63 2b 33 5d 3b 0a 20 20 20  = aOp[pc+3];.   
104c2 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
104c3 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 33    }.      case 3
104c4 3a 0a 20 20 20 20 20 20 63 61 73 65 20 34 3a 20  :.      case 4: 
104c5 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20  .      default: 
104c6 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 3d 20 32  {.        nx = 2
104c7 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
104c8 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a  3_randomness(siz
104c9 65 6f 66 28 69 29 2c 20 26 69 29 3b 0a 20 20 20  eof(i), &i);.   
104ca 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
104cb 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
104cc 28 20 28 2d 2d 61 4f 70 5b 70 63 2b 31 5d 29 20  ( (--aOp[pc+1]) 
104cd 3e 20 30 20 29 20 6e 78 20 3d 20 30 3b 0a 20 20  > 0 ) nx = 0;.  
104ce 20 20 70 63 20 2b 3d 20 6e 78 3b 0a 20 20 20 20    pc += nx;.    
104cf 69 20 3d 20 28 69 20 26 20 30 78 37 66 66 66 66  i = (i & 0x7ffff
104d0 66 66 66 29 25 73 7a 3b 0a 20 20 20 20 69 66 28  fff)%sz;.    if(
104d1 20 28 6f 70 20 26 20 31 29 21 3d 30 20 29 7b 0a   (op & 1)!=0 ){.
104d2 20 20 20 20 20 20 53 45 54 42 49 54 28 70 56 2c        SETBIT(pV,
104d3 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 20 20 69   (i+1));.      i
104d4 66 28 20 6f 70 21 3d 35 20 29 7b 0a 20 20 20 20  f( op!=5 ){.    
104d5 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42      if( sqlite3B
104d6 69 74 76 65 63 53 65 74 28 70 42 69 74 76 65 63  itvecSet(pBitvec
104d7 2c 20 69 2b 31 29 20 29 20 67 6f 74 6f 20 62 69  , i+1) ) goto bi
104d8 74 76 65 63 5f 65 6e 64 3b 0a 20 20 20 20 20 20  tvec_end;.      
104d9 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
104da 20 20 20 43 4c 45 41 52 42 49 54 28 70 56 2c 20     CLEARBIT(pV, 
104db 28 69 2b 31 29 29 3b 0a 20 20 20 20 20 20 73 71  (i+1));.      sq
104dc 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 72  lite3BitvecClear
104dd 28 70 42 69 74 76 65 63 2c 20 69 2b 31 2c 20 70  (pBitvec, i+1, p
104de 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 20 20 7d  TmpSpace);.    }
104df 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 20  .  }..  /* Test 
104e0 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65  to make sure the
104e1 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 65 78   linear array ex
104e2 61 63 74 6c 79 20 6d 61 74 63 68 65 73 20 74 68  actly matches th
104e3 65 0a 20 20 2a 2a 20 42 69 74 76 65 63 20 6f 62  e.  ** Bitvec ob
104e4 6a 65 63 74 2e 20 20 53 74 61 72 74 20 77 69 74  ject.  Start wit
104e5 68 20 74 68 65 20 61 73 73 75 6d 70 74 69 6f 6e  h the assumption
104e6 20 74 68 61 74 20 74 68 65 79 20 64 6f 0a 20 20   that they do.  
104e7 2a 2a 20 6d 61 74 63 68 20 28 72 63 3d 3d 30 29  ** match (rc==0)
104e8 2e 20 20 43 68 61 6e 67 65 20 72 63 20 74 6f 20  .  Change rc to 
104e9 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 64 69  non-zero if a di
104ea 73 63 72 65 70 61 6e 63 79 0a 20 20 2a 2a 20 69  screpancy.  ** i
104eb 73 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20 20  s found..  */.  
104ec 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76  rc = sqlite3Bitv
104ed 65 63 54 65 73 74 28 30 2c 30 29 20 2b 20 73 71  ecTest(0,0) + sq
104ee 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28  lite3BitvecTest(
104ef 70 42 69 74 76 65 63 2c 20 73 7a 2b 31 29 0a 20  pBitvec, sz+1). 
104f0 20 20 20 20 20 20 20 20 20 2b 20 73 71 6c 69 74           + sqlit
104f1 65 33 42 69 74 76 65 63 54 65 73 74 28 70 42 69  e3BitvecTest(pBi
104f2 74 76 65 63 2c 20 30 29 0a 20 20 20 20 20 20 20  tvec, 0).       
104f3 20 20 20 2b 20 28 73 71 6c 69 74 65 33 42 69 74     + (sqlite3Bit
104f4 76 65 63 53 69 7a 65 28 70 42 69 74 76 65 63 29  vecSize(pBitvec)
104f5 20 2d 20 73 7a 29 3b 0a 20 20 66 6f 72 28 69 3d   - sz);.  for(i=
104f6 31 3b 20 69 3c 3d 73 7a 3b 20 69 2b 2b 29 7b 0a  1; i<=sz; i++){.
104f7 20 20 20 20 69 66 28 20 20 28 54 45 53 54 42 49      if(  (TESTBI
104f8 54 28 70 56 2c 69 29 29 21 3d 73 71 6c 69 74 65  T(pV,i))!=sqlite
104f9 33 42 69 74 76 65 63 54 65 73 74 28 70 42 69 74  3BitvecTest(pBit
104fa 76 65 63 2c 69 29 20 29 7b 0a 20 20 20 20 20 20  vec,i) ){.      
104fb 72 63 20 3d 20 69 3b 0a 20 20 20 20 20 20 62 72  rc = i;.      br
104fc 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  eak;.    }.  }..
104fd 20 20 2f 2a 20 46 72 65 65 20 61 6c 6c 6f 63 61    /* Free alloca
104fe 74 65 64 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ted structure */
104ff 0a 62 69 74 76 65 63 5f 65 6e 64 3a 0a 20 20 73  .bitvec_end:.  s
10500 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6d 70  qlite3_free(pTmp
10501 53 70 61 63 65 29 3b 0a 20 20 73 71 6c 69 74 65  Space);.  sqlite
10502 33 5f 66 72 65 65 28 70 56 29 3b 0a 20 20 73 71  3_free(pV);.  sq
10503 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72  lite3BitvecDestr
10504 6f 79 28 70 42 69 74 76 65 63 29 3b 0a 20 20 72  oy(pBitvec);.  r
10505 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64  eturn rc;.}.#end
10506 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
10507 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a  T_BUILTIN_TEST *
10508 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
10509 2a 2a 20 45 6e 64 20 6f 66 20 62 69 74 76 65 63  ** End of bitvec
1050a 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1050b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1050c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1050d 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
1050e 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 63  ** Begin file pc
1050f 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ache.c *********
10510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10511 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10512 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75  */./*.** 2008 Au
10513 67 75 73 74 20 30 35 0a 2a 2a 0a 2a 2a 20 54 68  gust 05.**.** Th
10514 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
10515 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
10516 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
10517 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
10518 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
10519 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
1051a 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
1051b 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
1051c 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
1051d 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
1051e 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
1051f 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
10520 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
10521 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
10522 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
10523 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
10524 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
10525 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10526 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10527 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10528 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10529 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
1052a 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  ile implements t
1052b 68 61 74 20 70 61 67 65 20 63 61 63 68 65 2e 0a  hat page cache..
1052c 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20  **.** @(#) $Id: 
1052d 70 63 61 63 68 65 2e 63 2c 76 20 31 2e 34 37 20  pcache.c,v 1.47 
1052e 32 30 30 39 2f 30 37 2f 32 35 20 31 31 3a 34 36  2009/07/25 11:46
1052f 3a 34 39 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :49 danielk1977 
10530 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
10531 41 20 63 6f 6d 70 6c 65 74 65 20 70 61 67 65 20  A complete page 
10532 63 61 63 68 65 20 69 73 20 61 6e 20 69 6e 73 74  cache is an inst
10533 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
10534 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63  ucture..*/.struc
10535 74 20 50 43 61 63 68 65 20 7b 0a 20 20 50 67 48  t PCache {.  PgH
10536 64 72 20 2a 70 44 69 72 74 79 2c 20 2a 70 44 69  dr *pDirty, *pDi
10537 72 74 79 54 61 69 6c 3b 20 20 20 20 20 20 20 20  rtyTail;        
10538 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69 72 74   /* List of dirt
10539 79 20 70 61 67 65 73 20 69 6e 20 4c 52 55 20 6f  y pages in LRU o
1053a 72 64 65 72 20 2a 2f 0a 20 20 50 67 48 64 72 20  rder */.  PgHdr 
1053b 2a 70 53 79 6e 63 65 64 3b 20 20 20 20 20 20 20  *pSynced;       
1053c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1053d 20 4c 61 73 74 20 73 79 6e 63 65 64 20 70 61 67   Last synced pag
1053e 65 20 69 6e 20 64 69 72 74 79 20 70 61 67 65 20  e in dirty page 
1053f 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  list */.  int nR
10540 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ef;             
10541 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10542 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72   Number of refer
10543 65 6e 63 65 64 20 70 61 67 65 73 20 2a 2f 0a 20  enced pages */. 
10544 20 69 6e 74 20 6e 4d 61 78 3b 20 20 20 20 20 20   int nMax;      
10545 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10546 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72       /* Configur
10547 65 64 20 63 61 63 68 65 20 73 69 7a 65 20 2a 2f  ed cache size */
10548 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 20  .  int szPage;  
10549 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1054a 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
1054b 66 20 65 76 65 72 79 20 70 61 67 65 20 69 6e 20  f every page in 
1054c 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a 20 20  this cache */.  
1054d 69 6e 74 20 73 7a 45 78 74 72 61 3b 20 20 20 20  int szExtra;    
1054e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1054f 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65      /* Size of e
10550 78 74 72 61 20 73 70 61 63 65 20 66 6f 72 20 65  xtra space for e
10551 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e  ach page */.  in
10552 74 20 62 50 75 72 67 65 61 62 6c 65 3b 20 20 20  t bPurgeable;   
10553 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10554 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61 67    /* True if pag
10555 65 73 20 61 72 65 20 6f 6e 20 62 61 63 6b 69 6e  es are on backin
10556 67 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 6e 74  g store */.  int
10557 20 28 2a 78 53 74 72 65 73 73 29 28 76 6f 69 64   (*xStress)(void
10558 2a 2c 50 67 48 64 72 2a 29 3b 20 20 20 20 20 20  *,PgHdr*);      
10559 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 79 20   /* Call to try 
1055a 6d 61 6b 65 20 61 20 70 61 67 65 20 63 6c 65 61  make a page clea
1055b 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74  n */.  void *pSt
1055c 72 65 73 73 3b 20 20 20 20 20 20 20 20 20 20 20  ress;           
1055d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
1055e 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 73  gument to xStres
1055f 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 70  s */.  sqlite3_p
10560 63 61 63 68 65 20 2a 70 43 61 63 68 65 3b 20 20  cache *pCache;  
10561 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6c             /* Pl
10562 75 67 67 61 62 6c 65 20 63 61 63 68 65 20 6d 6f  uggable cache mo
10563 64 75 6c 65 20 2a 2f 0a 20 20 50 67 48 64 72 20  dule */.  PgHdr 
10564 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20 20 20  *pPage1;        
10565 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10566 20 52 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61   Reference to pa
10567 67 65 20 31 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  ge 1 */.};../*.*
10568 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 61 73  * Some of the as
10569 73 65 72 74 28 29 20 6d 61 63 72 6f 73 20 69 6e  sert() macros in
1056a 20 74 68 69 73 20 63 6f 64 65 20 61 72 65 20 74   this code are t
1056b 6f 6f 20 65 78 70 65 6e 73 69 76 65 20 74 6f 20  oo expensive to 
1056c 72 75 6e 0a 2a 2a 20 65 76 65 6e 20 64 75 72 69  run.** even duri
1056d 6e 67 20 6e 6f 72 6d 61 6c 20 64 65 62 75 67 67  ng normal debugg
1056e 69 6e 67 2e 20 20 55 73 65 20 74 68 65 6d 20 6f  ing.  Use them o
1056f 6e 6c 79 20 72 61 72 65 6c 79 20 6f 6e 20 6c 6f  nly rarely on lo
10570 6e 67 2d 72 75 6e 6e 69 6e 67 0a 2a 2a 20 74 65  ng-running.** te
10571 73 74 73 2e 20 20 45 6e 61 62 6c 65 20 74 68 65  sts.  Enable the
10572 20 65 78 70 65 6e 73 69 76 65 20 61 73 73 65 72   expensive asser
10573 74 73 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  ts using the.** 
10574 2d 44 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  -DSQLITE_ENABLE_
10575 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54  EXPENSIVE_ASSERT
10576 3d 31 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20  =1 compile-time 
10577 6f 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 64 65  option..*/.#ifde
10578 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
10579 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54  EXPENSIVE_ASSERT
1057a 0a 23 20 64 65 66 69 6e 65 20 65 78 70 65 6e 73  .# define expens
1057b 69 76 65 5f 61 73 73 65 72 74 28 58 29 20 20 61  ive_assert(X)  a
1057c 73 73 65 72 74 28 58 29 0a 23 65 6c 73 65 0a 23  ssert(X).#else.#
1057d 20 64 65 66 69 6e 65 20 65 78 70 65 6e 73 69 76   define expensiv
1057e 65 5f 61 73 73 65 72 74 28 58 29 0a 23 65 6e 64  e_assert(X).#end
1057f 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  if../***********
10580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10581 2a 2a 2a 2a 2a 2a 2a 20 4c 69 6e 6b 65 64 20 4c  ******* Linked L
10582 69 73 74 20 4d 61 6e 61 67 65 6d 65 6e 74 20 2a  ist Management *
10583 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10584 2a 2a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e  ***/..#if !defin
10585 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 64 65  ed(NDEBUG) && de
10586 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
10587 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53  BLE_EXPENSIVE_AS
10588 53 45 52 54 29 0a 2f 2a 0a 2a 2a 20 43 68 65 63  SERT)./*.** Chec
10589 6b 20 74 68 61 74 20 74 68 65 20 70 43 61 63 68  k that the pCach
1058a 65 2d 3e 70 53 79 6e 63 65 64 20 76 61 72 69 61  e->pSynced varia
1058b 62 6c 65 20 69 73 20 73 65 74 20 63 6f 72 72 65  ble is set corre
1058c 63 74 6c 79 2e 20 49 66 20 69 74 0a 2a 2a 20 69  ctly. If it.** i
1058d 73 20 6e 6f 74 2c 20 65 69 74 68 65 72 20 66 61  s not, either fa
1058e 69 6c 20 61 6e 20 61 73 73 65 72 74 20 6f 72 20  il an assert or 
1058f 72 65 74 75 72 6e 20 7a 65 72 6f 2e 20 4f 74 68  return zero. Oth
10590 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 0a 2a  erwise, return.*
10591 2a 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 54 68 69 73  * non-zero. This
10592 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 6e   is only used in
10593 20 64 65 62 75 67 67 69 6e 67 20 62 75 69 6c 64   debugging build
10594 73 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  s, as follows:.*
10595 2a 0a 2a 2a 20 20 20 65 78 70 65 6e 73 69 76 65  *.**   expensive
10596 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 65 43  _assert( pcacheC
10597 68 65 63 6b 53 79 6e 63 65 64 28 70 43 61 63 68  heckSynced(pCach
10598 65 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20  e) );.*/.static 
10599 69 6e 74 20 70 63 61 63 68 65 43 68 65 63 6b 53  int pcacheCheckS
1059a 79 6e 63 65 64 28 50 43 61 63 68 65 20 2a 70 43  ynced(PCache *pC
1059b 61 63 68 65 29 7b 0a 20 20 50 67 48 64 72 20 2a  ache){.  PgHdr *
1059c 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 68  p;.  for(p=pCach
1059d 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b 20 70  e->pDirtyTail; p
1059e 21 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65  !=pCache->pSynce
1059f 64 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 50 72  d; p=p->pDirtyPr
105a0 65 76 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ev){.    assert(
105a1 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 2d 3e   p->nRef || (p->
105a2 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44  flags&PGHDR_NEED
105a3 5f 53 59 4e 43 29 20 29 3b 0a 20 20 7d 0a 20 20  _SYNC) );.  }.  
105a4 72 65 74 75 72 6e 20 28 70 3d 3d 30 20 7c 7c 20  return (p==0 || 
105a5 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 2d 3e 66  p->nRef || (p->f
105a6 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f  lags&PGHDR_NEED_
105a7 53 59 4e 43 29 3d 3d 30 29 3b 0a 7d 0a 23 65 6e  SYNC)==0);.}.#en
105a8 64 69 66 20 2f 2a 20 21 4e 44 45 42 55 47 20 26  dif /* !NDEBUG &
105a9 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  & SQLITE_ENABLE_
105aa 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54  EXPENSIVE_ASSERT
105ab 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76   */../*.** Remov
105ac 65 20 70 61 67 65 20 70 50 61 67 65 20 66 72 6f  e page pPage fro
105ad 6d 20 74 68 65 20 6c 69 73 74 20 6f 66 20 64 69  m the list of di
105ae 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 73 74  rty pages..*/.st
105af 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65  atic void pcache
105b0 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c  RemoveFromDirtyL
105b1 69 73 74 28 50 67 48 64 72 20 2a 70 50 61 67 65  ist(PgHdr *pPage
105b2 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 20 3d  ){.  PCache *p =
105b3 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a   pPage->pCache;.
105b4 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
105b5 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 7c 7c 20  ->pDirtyNext || 
105b6 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79  pPage==p->pDirty
105b7 54 61 69 6c 20 29 3b 0a 20 20 61 73 73 65 72 74  Tail );.  assert
105b8 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50  ( pPage->pDirtyP
105b9 72 65 76 20 7c 7c 20 70 50 61 67 65 3d 3d 70 2d  rev || pPage==p-
105ba 3e 70 44 69 72 74 79 20 29 3b 0a 0a 20 20 2f 2a  >pDirty );..  /*
105bb 20 55 70 64 61 74 65 20 74 68 65 20 50 43 61 63   Update the PCac
105bc 68 65 31 2e 70 53 79 6e 63 65 64 20 76 61 72 69  he1.pSynced vari
105bd 61 62 6c 65 20 69 66 20 6e 65 63 65 73 73 61 72  able if necessar
105be 79 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70  y. */.  if( p->p
105bf 53 79 6e 63 65 64 3d 3d 70 50 61 67 65 20 29 7b  Synced==pPage ){
105c0 0a 20 20 20 20 50 67 48 64 72 20 2a 70 53 79 6e  .    PgHdr *pSyn
105c1 63 65 64 20 3d 20 70 50 61 67 65 2d 3e 70 44 69  ced = pPage->pDi
105c2 72 74 79 50 72 65 76 3b 0a 20 20 20 20 77 68 69  rtyPrev;.    whi
105c3 6c 65 28 20 70 53 79 6e 63 65 64 20 26 26 20 28  le( pSynced && (
105c4 70 53 79 6e 63 65 64 2d 3e 66 6c 61 67 73 26 50  pSynced->flags&P
105c5 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20  GHDR_NEED_SYNC) 
105c6 29 7b 0a 20 20 20 20 20 20 70 53 79 6e 63 65 64  ){.      pSynced
105c7 20 3d 20 70 53 79 6e 63 65 64 2d 3e 70 44 69 72   = pSynced->pDir
105c8 74 79 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20  tyPrev;.    }.  
105c9 20 20 70 2d 3e 70 53 79 6e 63 65 64 20 3d 20 70    p->pSynced = p
105ca 53 79 6e 63 65 64 3b 0a 20 20 7d 0a 0a 20 20 69  Synced;.  }..  i
105cb 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79  f( pPage->pDirty
105cc 4e 65 78 74 20 29 7b 0a 20 20 20 20 70 50 61 67  Next ){.    pPag
105cd 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e 70  e->pDirtyNext->p
105ce 44 69 72 74 79 50 72 65 76 20 3d 20 70 50 61 67  DirtyPrev = pPag
105cf 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20  e->pDirtyPrev;. 
105d0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
105d1 72 74 28 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44  rt( pPage==p->pD
105d2 69 72 74 79 54 61 69 6c 20 29 3b 0a 20 20 20 20  irtyTail );.    
105d3 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 3d 20  p->pDirtyTail = 
105d4 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65  pPage->pDirtyPre
105d5 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  v;.  }.  if( pPa
105d6 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 20 29  ge->pDirtyPrev )
105d7 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44 69  {.    pPage->pDi
105d8 72 74 79 50 72 65 76 2d 3e 70 44 69 72 74 79 4e  rtyPrev->pDirtyN
105d9 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 44 69  ext = pPage->pDi
105da 72 74 79 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65  rtyNext;.  }else
105db 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  {.    assert( pP
105dc 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 20 29  age==p->pDirty )
105dd 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 20  ;.    p->pDirty 
105de 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e  = pPage->pDirtyN
105df 65 78 74 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65  ext;.  }.  pPage
105e0 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d 20 30  ->pDirtyNext = 0
105e1 3b 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74  ;.  pPage->pDirt
105e2 79 50 72 65 76 20 3d 20 30 3b 0a 0a 20 20 65 78  yPrev = 0;..  ex
105e3 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20  pensive_assert( 
105e4 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65  pcacheCheckSynce
105e5 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  d(p) );.}../*.**
105e6 20 41 64 64 20 70 61 67 65 20 70 50 61 67 65 20   Add page pPage 
105e7 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 74  to the head of t
105e8 68 65 20 64 69 72 74 79 20 6c 69 73 74 20 28 50  he dirty list (P
105e9 43 61 63 68 65 31 2e 70 44 69 72 74 79 20 69 73  Cache1.pDirty is
105ea 20 73 65 74 20 74 6f 0a 2a 2a 20 70 50 61 67 65   set to.** pPage
105eb 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
105ec 64 20 70 63 61 63 68 65 41 64 64 54 6f 44 69 72  d pcacheAddToDir
105ed 74 79 4c 69 73 74 28 50 67 48 64 72 20 2a 70 50  tyList(PgHdr *pP
105ee 61 67 65 29 7b 0a 20 20 50 43 61 63 68 65 20 2a  age){.  PCache *
105ef 70 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 63 68  p = pPage->pCach
105f0 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50  e;..  assert( pP
105f1 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 3d  age->pDirtyNext=
105f2 3d 30 20 26 26 20 70 50 61 67 65 2d 3e 70 44 69  =0 && pPage->pDi
105f3 72 74 79 50 72 65 76 3d 3d 30 20 26 26 20 70 2d  rtyPrev==0 && p-
105f4 3e 70 44 69 72 74 79 21 3d 70 50 61 67 65 20 29  >pDirty!=pPage )
105f5 3b 0a 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 72  ;..  pPage->pDir
105f6 74 79 4e 65 78 74 20 3d 20 70 2d 3e 70 44 69 72  tyNext = p->pDir
105f7 74 79 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d  ty;.  if( pPage-
105f8 3e 70 44 69 72 74 79 4e 65 78 74 20 29 7b 0a 20  >pDirtyNext ){. 
105f9 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
105fa 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e 70 44  ->pDirtyNext->pD
105fb 69 72 74 79 50 72 65 76 3d 3d 30 20 29 3b 0a 20  irtyPrev==0 );. 
105fc 20 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79     pPage->pDirty
105fd 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 72 65 76  Next->pDirtyPrev
105fe 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20   = pPage;.  }.  
105ff 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 50 61 67  p->pDirty = pPag
10600 65 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 44 69  e;.  if( !p->pDi
10601 72 74 79 54 61 69 6c 20 29 7b 0a 20 20 20 20 70  rtyTail ){.    p
10602 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 3d 20 70  ->pDirtyTail = p
10603 50 61 67 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Page;.  }.  if( 
10604 21 70 2d 3e 70 53 79 6e 63 65 64 20 26 26 20 30  !p->pSynced && 0
10605 3d 3d 28 70 50 61 67 65 2d 3e 66 6c 61 67 73 26  ==(pPage->flags&
10606 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29  PGHDR_NEED_SYNC)
10607 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53 79 6e 63   ){.    p->pSync
10608 65 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a  ed = pPage;.  }.
10609 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65    expensive_asse
1060a 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b 53  rt( pcacheCheckS
1060b 79 6e 63 65 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f  ynced(p) );.}../
1060c 2a 0a 2a 2a 20 57 72 61 70 70 65 72 20 61 72 6f  *.** Wrapper aro
1060d 75 6e 64 20 74 68 65 20 70 6c 75 67 67 61 62 6c  und the pluggabl
1060e 65 20 63 61 63 68 65 73 20 78 55 6e 70 69 6e 20  e caches xUnpin 
1060f 6d 65 74 68 6f 64 2e 20 49 66 20 74 68 65 20 63  method. If the c
10610 61 63 68 65 20 69 73 0a 2a 2a 20 62 65 69 6e 67  ache is.** being
10611 20 75 73 65 64 20 66 6f 72 20 61 6e 20 69 6e 2d   used for an in-
10612 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c  memory database,
10613 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
10614 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74  s a no-op..*/.st
10615 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65  atic void pcache
10616 55 6e 70 69 6e 28 50 67 48 64 72 20 2a 70 29 7b  Unpin(PgHdr *p){
10617 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68  .  PCache *pCach
10618 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20  e = p->pCache;. 
10619 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75   if( pCache->bPu
1061a 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 69  rgeable ){.    i
1061b 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b  f( p->pgno==1 ){
1061c 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e 70  .      pCache->p
1061d 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 20 20 7d  Page1 = 0;.    }
1061e 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
1061f 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e  alConfig.pcache.
10620 78 55 6e 70 69 6e 28 70 43 61 63 68 65 2d 3e 70  xUnpin(pCache->p
10621 43 61 63 68 65 2c 20 70 2c 20 30 29 3b 0a 20 20  Cache, p, 0);.  
10622 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }.}../**********
10623 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10624 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10625 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61  ********* Genera
10626 6c 20 49 6e 74 65 72 66 61 63 65 73 20 2a 2a 2a  l Interfaces ***
10627 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6e 69 74 69 61  ***.**.** Initia
10628 6c 69 7a 65 20 61 6e 64 20 73 68 75 74 64 6f 77  lize and shutdow
10629 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  n the page cache
1062a 20 73 75 62 73 79 73 74 65 6d 2e 20 4e 65 69 74   subsystem. Neit
1062b 68 65 72 20 6f 66 20 74 68 65 73 65 20 0a 2a 2a  her of these .**
1062c 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 74   functions are t
1062d 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f 0a 53 51  hreadsafe..*/.SQ
1062e 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1062f 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 6e   sqlite3PcacheIn
10630 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 7b 0a  itialize(void){.
10631 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
10632 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65  balConfig.pcache
10633 2e 78 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20  .xInit==0 ){.   
10634 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 53 65   sqlite3PCacheSe
10635 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 7d 0a  tDefault();.  }.
10636 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
10637 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61  GlobalConfig.pca
10638 63 68 65 2e 78 49 6e 69 74 28 73 71 6c 69 74 65  che.xInit(sqlite
10639 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
1063a 61 63 68 65 2e 70 41 72 67 29 3b 0a 7d 0a 53 51  ache.pArg);.}.SQ
1063b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1063c 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53  d sqlite3PcacheS
1063d 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 7b 0a 20  hutdown(void){. 
1063e 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
1063f 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e  alConfig.pcache.
10640 78 53 68 75 74 64 6f 77 6e 20 29 7b 0a 20 20 20  xShutdown ){.   
10641 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
10642 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 53 68 75  nfig.pcache.xShu
10643 74 64 6f 77 6e 28 73 71 6c 69 74 65 33 47 6c 6f  tdown(sqlite3Glo
10644 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65  balConfig.pcache
10645 2e 70 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  .pArg);.  }.}../
10646 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
10647 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66  size in bytes of
10648 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74   a PCache object
10649 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1064a 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
1064b 63 61 63 68 65 53 69 7a 65 28 76 6f 69 64 29 7b  cacheSize(void){
1064c 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 28 50   return sizeof(P
1064d 43 61 63 68 65 29 3b 20 7d 0a 0a 2f 2a 0a 2a 2a  Cache); }../*.**
1064e 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 50 43   Create a new PC
1064f 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 53 74 6f  ache object. Sto
10650 72 61 67 65 20 73 70 61 63 65 20 74 6f 20 68 6f  rage space to ho
10651 6c 64 20 74 68 65 20 6f 62 6a 65 63 74 0a 2a 2a  ld the object.**
10652 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
10653 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20  n allocated and 
10654 69 73 20 70 61 73 73 65 64 20 69 6e 20 61 73 20  is passed in as 
10655 74 68 65 20 70 20 70 6f 69 6e 74 65 72 2e 20 0a  the p pointer. .
10656 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 64 69  ** The caller di
10657 73 63 6f 76 65 72 73 20 68 6f 77 20 6d 75 63 68  scovers how much
10658 20 73 70 61 63 65 20 6e 65 65 64 73 20 74 6f 20   space needs to 
10659 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  be allocated by 
1065a 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69  .** calling sqli
1065b 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 29 2e  te3PcacheSize().
1065c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1065d 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
1065e 63 61 63 68 65 4f 70 65 6e 28 0a 20 20 69 6e 74  cacheOpen(.  int
1065f 20 73 7a 50 61 67 65 2c 20 20 20 20 20 20 20 20   szPage,        
10660 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
10661 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 20  e of every page 
10662 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 61  */.  int szExtra
10663 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
10664 20 20 2f 2a 20 45 78 74 72 61 20 73 70 61 63 65    /* Extra space
10665 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
10666 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20   each page */.  
10667 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 2c 20  int bPurgeable, 
10668 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10669 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72  True if pages ar
1066a 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f  e on backing sto
1066b 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53  re */.  int (*xS
1066c 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 67 48  tress)(void*,PgH
1066d 64 72 2a 29 2c 2f 2a 20 43 61 6c 6c 20 74 6f 20  dr*),/* Call to 
1066e 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65  try to make page
1066f 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f 69  s clean */.  voi
10670 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20 20  d *pStress,     
10671 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67            /* Arg
10672 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 73 73  ument to xStress
10673 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 20   */.  PCache *p 
10674 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10675 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 74     /* Preallocat
10676 65 64 20 73 70 61 63 65 20 66 6f 72 20 74 68 65  ed space for the
10677 20 50 43 61 63 68 65 20 2a 2f 0a 29 7b 0a 20 20   PCache */.){.  
10678 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a  memset(p, 0, siz
10679 65 6f 66 28 50 43 61 63 68 65 29 29 3b 0a 20 20  eof(PCache));.  
1067a 70 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61  p->szPage = szPa
1067b 67 65 3b 0a 20 20 70 2d 3e 73 7a 45 78 74 72 61  ge;.  p->szExtra
1067c 20 3d 20 73 7a 45 78 74 72 61 3b 0a 20 20 70 2d   = szExtra;.  p-
1067d 3e 62 50 75 72 67 65 61 62 6c 65 20 3d 20 62 50  >bPurgeable = bP
1067e 75 72 67 65 61 62 6c 65 3b 0a 20 20 70 2d 3e 78  urgeable;.  p->x
1067f 53 74 72 65 73 73 20 3d 20 78 53 74 72 65 73 73  Stress = xStress
10680 3b 0a 20 20 70 2d 3e 70 53 74 72 65 73 73 20 3d  ;.  p->pStress =
10681 20 70 53 74 72 65 73 73 3b 0a 20 20 70 2d 3e 6e   pStress;.  p->n
10682 4d 61 78 20 3d 20 31 30 30 3b 0a 7d 0a 0a 2f 2a  Max = 100;.}../*
10683 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 70  .** Change the p
10684 61 67 65 20 73 69 7a 65 20 66 6f 72 20 50 43 61  age size for PCa
10685 63 68 65 20 6f 62 6a 65 63 74 2e 20 54 68 65 20  che object. The 
10686 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75  caller must ensu
10687 72 65 20 74 68 61 74 20 74 68 65 72 65 0a 2a 2a  re that there.**
10688 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64   are no outstand
10689 69 6e 67 20 70 61 67 65 20 72 65 66 65 72 65 6e  ing page referen
1068a 63 65 73 20 77 68 65 6e 20 74 68 69 73 20 66 75  ces when this fu
1068b 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
1068c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1068d 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1068e 50 63 61 63 68 65 53 65 74 50 61 67 65 53 69 7a  PcacheSetPageSiz
1068f 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65  e(PCache *pCache
10690 2c 20 69 6e 74 20 73 7a 50 61 67 65 29 7b 0a 20  , int szPage){. 
10691 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65 2d   assert( pCache-
10692 3e 6e 52 65 66 3d 3d 30 20 26 26 20 70 43 61 63  >nRef==0 && pCac
10693 68 65 2d 3e 70 44 69 72 74 79 3d 3d 30 20 29 3b  he->pDirty==0 );
10694 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70  .  if( pCache->p
10695 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c  Cache ){.    sql
10696 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
10697 2e 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79  .pcache.xDestroy
10698 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29  (pCache->pCache)
10699 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70 43  ;.    pCache->pC
1069a 61 63 68 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  ache = 0;.  }.  
1069b 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 3d  pCache->szPage =
1069c 20 73 7a 50 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a   szPage;.}../*.*
1069d 2a 20 54 72 79 20 74 6f 20 6f 62 74 61 69 6e 20  * Try to obtain 
1069e 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20  a page from the 
1069f 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cache..*/.SQLITE
106a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
106a1 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 28  ite3PcacheFetch(
106a2 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68  .  PCache *pCach
106a3 65 2c 20 20 20 20 20 20 20 2f 2a 20 4f 62 74 61  e,       /* Obta
106a4 69 6e 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d  in the page from
106a5 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a 20   this cache */. 
106a6 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20   Pgno pgno,     
106a7 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e         /* Page n
106a8 75 6d 62 65 72 20 74 6f 20 6f 62 74 61 69 6e 20  umber to obtain 
106a9 2a 2f 0a 20 20 69 6e 74 20 63 72 65 61 74 65 46  */.  int createF
106aa 6c 61 67 2c 20 20 20 20 20 20 20 2f 2a 20 49 66  lag,       /* If
106ab 20 74 72 75 65 2c 20 63 72 65 61 74 65 20 70 61   true, create pa
106ac 67 65 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f  ge if it does no
106ad 74 20 65 78 69 73 74 20 61 6c 72 65 61 64 79 20  t exist already 
106ae 2a 2f 0a 20 20 50 67 48 64 72 20 2a 2a 70 70 50  */.  PgHdr **ppP
106af 61 67 65 20 20 20 20 20 20 20 20 2f 2a 20 57 72  age        /* Wr
106b0 69 74 65 20 74 68 65 20 70 61 67 65 20 68 65 72  ite the page her
106b1 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 72 20  e */.){.  PgHdr 
106b2 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 6e  *pPage = 0;.  in
106b3 74 20 65 43 72 65 61 74 65 3b 0a 0a 20 20 61 73  t eCreate;..  as
106b4 73 65 72 74 28 20 70 43 61 63 68 65 21 3d 30 20  sert( pCache!=0 
106b5 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 72 65  );.  assert( cre
106b6 61 74 65 46 6c 61 67 3d 3d 31 20 7c 7c 20 63 72  ateFlag==1 || cr
106b7 65 61 74 65 46 6c 61 67 3d 3d 30 20 29 3b 0a 20  eateFlag==0 );. 
106b8 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3e 30 20   assert( pgno>0 
106b9 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
106ba 70 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65 20  pluggable cache 
106bb 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a  (sqlite3_pcache*
106bc 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 61  ) has not been a
106bd 6c 6c 6f 63 61 74 65 64 2c 0a 20 20 2a 2a 20 61  llocated,.  ** a
106be 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77 2e 0a  llocate it now..
106bf 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 43 61 63    */.  if( !pCac
106c0 68 65 2d 3e 70 43 61 63 68 65 20 26 26 20 63 72  he->pCache && cr
106c1 65 61 74 65 46 6c 61 67 20 29 7b 0a 20 20 20 20  eateFlag ){.    
106c2 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a  sqlite3_pcache *
106c3 70 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65  p;.    int nByte
106c4 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 70 43  ;.    nByte = pC
106c5 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 2b 20 70  ache->szPage + p
106c6 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 20 2b  Cache->szExtra +
106c7 20 73 69 7a 65 6f 66 28 50 67 48 64 72 29 3b 0a   sizeof(PgHdr);.
106c8 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47      p = sqlite3G
106c9 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63  lobalConfig.pcac
106ca 68 65 2e 78 43 72 65 61 74 65 28 6e 42 79 74 65  he.xCreate(nByte
106cb 2c 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65  , pCache->bPurge
106cc 61 62 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 21  able);.    if( !
106cd 70 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  p ){.      retur
106ce 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
106cf 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
106d0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
106d1 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65 28  ache.xCachesize(
106d2 70 2c 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29  p, pCache->nMax)
106d3 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70 43  ;.    pCache->pC
106d4 61 63 68 65 20 3d 20 70 3b 0a 20 20 7d 0a 0a 20  ache = p;.  }.. 
106d5 20 65 43 72 65 61 74 65 20 3d 20 63 72 65 61 74   eCreate = creat
106d6 65 46 6c 61 67 20 2a 20 28 31 20 2b 20 28 21 70  eFlag * (1 + (!p
106d7 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c  Cache->bPurgeabl
106d8 65 20 7c 7c 20 21 70 43 61 63 68 65 2d 3e 70 44  e || !pCache->pD
106d9 69 72 74 79 29 29 3b 0a 20 20 69 66 28 20 70 43  irty));.  if( pC
106da 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a  ache->pCache ){.
106db 20 20 20 20 70 50 61 67 65 20 3d 20 73 71 6c 69      pPage = sqli
106dc 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
106dd 70 63 61 63 68 65 2e 78 46 65 74 63 68 28 70 43  pcache.xFetch(pC
106de 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 67  ache->pCache, pg
106df 6e 6f 2c 20 65 43 72 65 61 74 65 29 3b 0a 20 20  no, eCreate);.  
106e0 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 20  }..  if( !pPage 
106e1 26 26 20 65 43 72 65 61 74 65 3d 3d 31 20 29 7b  && eCreate==1 ){
106e2 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 3b  .    PgHdr *pPg;
106e3 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61 20  ..    /* Find a 
106e4 64 69 72 74 79 20 70 61 67 65 20 74 6f 20 77 72  dirty page to wr
106e5 69 74 65 2d 6f 75 74 20 61 6e 64 20 72 65 63 79  ite-out and recy
106e6 63 6c 65 2e 20 46 69 72 73 74 20 74 72 79 20 74  cle. First try t
106e7 6f 20 66 69 6e 64 20 61 20 0a 20 20 20 20 2a 2a  o find a .    **
106e8 20 70 61 67 65 20 74 68 61 74 20 64 6f 65 73 20   page that does 
106e9 6e 6f 74 20 72 65 71 75 69 72 65 20 61 20 6a 6f  not require a jo
106ea 75 72 6e 61 6c 2d 73 79 6e 63 20 28 6f 6e 65 20  urnal-sync (one 
106eb 77 69 74 68 20 50 47 48 44 52 5f 4e 45 45 44 5f  with PGHDR_NEED_
106ec 53 59 4e 43 0a 20 20 20 20 2a 2a 20 63 6c 65 61  SYNC.    ** clea
106ed 72 65 64 29 2c 20 62 75 74 20 69 66 20 74 68 61  red), but if tha
106ee 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
106ef 65 20 73 65 74 74 6c 65 20 66 6f 72 20 61 6e 79  e settle for any
106f0 20 6f 74 68 65 72 20 0a 20 20 20 20 2a 2a 20 75   other .    ** u
106f1 6e 72 65 66 65 72 65 6e 63 65 64 20 64 69 72 74  nreferenced dirt
106f2 79 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20  y page..    */. 
106f3 20 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73     expensive_ass
106f4 65 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b  ert( pcacheCheck
106f5 53 79 6e 63 65 64 28 70 43 61 63 68 65 29 20 29  Synced(pCache) )
106f6 3b 0a 20 20 20 20 66 6f 72 28 70 50 67 3d 70 43  ;.    for(pPg=pC
106f7 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 3b 20 0a  ache->pSynced; .
106f8 20 20 20 20 20 20 20 20 70 50 67 20 26 26 20 28          pPg && (
106f9 70 50 67 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 50  pPg->nRef || (pP
106fa 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e  g->flags&PGHDR_N
106fb 45 45 44 5f 53 59 4e 43 29 29 3b 20 0a 20 20 20  EED_SYNC)); .   
106fc 20 20 20 20 20 70 50 67 3d 70 50 67 2d 3e 70 44       pPg=pPg->pD
106fd 69 72 74 79 50 72 65 76 0a 20 20 20 20 29 3b 0a  irtyPrev.    );.
106fe 20 20 20 20 69 66 28 20 21 70 50 67 20 29 7b 0a      if( !pPg ){.
106ff 20 20 20 20 20 20 66 6f 72 28 70 50 67 3d 70 43        for(pPg=pC
10700 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c  ache->pDirtyTail
10701 3b 20 70 50 67 20 26 26 20 70 50 67 2d 3e 6e 52  ; pPg && pPg->nR
10702 65 66 3b 20 70 50 67 3d 70 50 67 2d 3e 70 44 69  ef; pPg=pPg->pDi
10703 72 74 79 50 72 65 76 29 3b 0a 20 20 20 20 7d 0a  rtyPrev);.    }.
10704 20 20 20 20 69 66 28 20 70 50 67 20 29 7b 0a 20      if( pPg ){. 
10705 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20       int rc;.   
10706 20 20 20 72 63 20 3d 20 70 43 61 63 68 65 2d 3e     rc = pCache->
10707 78 53 74 72 65 73 73 28 70 43 61 63 68 65 2d 3e  xStress(pCache->
10708 70 53 74 72 65 73 73 2c 20 70 50 67 29 3b 0a 20  pStress, pPg);. 
10709 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
1070a 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51  ITE_OK && rc!=SQ
1070b 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20  LITE_BUSY ){.   
1070c 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1070d 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
1070e 20 20 20 70 50 61 67 65 20 3d 20 73 71 6c 69 74     pPage = sqlit
1070f 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
10710 63 61 63 68 65 2e 78 46 65 74 63 68 28 70 43 61  cache.xFetch(pCa
10711 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 67 6e  che->pCache, pgn
10712 6f 2c 20 32 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  o, 2);.  }..  if
10713 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 69  ( pPage ){.    i
10714 66 28 20 21 70 50 61 67 65 2d 3e 70 44 61 74 61  f( !pPage->pData
10715 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74   ){.      memset
10716 28 70 50 61 67 65 2c 20 30 2c 20 73 69 7a 65 6f  (pPage, 0, sizeo
10717 66 28 50 67 48 64 72 29 20 2b 20 70 43 61 63 68  f(PgHdr) + pCach
10718 65 2d 3e 73 7a 45 78 74 72 61 29 3b 0a 20 20 20  e->szExtra);.   
10719 20 20 20 70 50 61 67 65 2d 3e 70 45 78 74 72 61     pPage->pExtra
1071a 20 3d 20 28 76 6f 69 64 2a 29 26 70 50 61 67 65   = (void*)&pPage
1071b 5b 31 5d 3b 0a 20 20 20 20 20 20 70 50 61 67 65  [1];.      pPage
1071c 2d 3e 70 44 61 74 61 20 3d 20 28 76 6f 69 64 20  ->pData = (void 
1071d 2a 29 26 28 28 63 68 61 72 20 2a 29 70 50 61 67  *)&((char *)pPag
1071e 65 29 5b 73 69 7a 65 6f 66 28 50 67 48 64 72 29  e)[sizeof(PgHdr)
1071f 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 45 78 74   + pCache->szExt
10720 72 61 5d 3b 0a 20 20 20 20 20 20 70 50 61 67 65  ra];.      pPage
10721 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61 63 68  ->pCache = pCach
10722 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e  e;.      pPage->
10723 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20 20 20  pgno = pgno;.   
10724 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
10725 50 61 67 65 2d 3e 70 43 61 63 68 65 3d 3d 70 43  Page->pCache==pC
10726 61 63 68 65 20 29 3b 0a 20 20 20 20 61 73 73 65  ache );.    asse
10727 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3d  rt( pPage->pgno=
10728 3d 70 67 6e 6f 20 29 3b 0a 20 20 20 20 61 73 73  =pgno );.    ass
10729 65 72 74 28 20 70 50 61 67 65 2d 3e 70 45 78 74  ert( pPage->pExt
1072a 72 61 3d 3d 28 76 6f 69 64 20 2a 29 26 70 50 61  ra==(void *)&pPa
1072b 67 65 5b 31 5d 20 29 3b 0a 0a 20 20 20 20 69 66  ge[1] );..    if
1072c 28 20 30 3d 3d 70 50 61 67 65 2d 3e 6e 52 65 66  ( 0==pPage->nRef
1072d 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68 65   ){.      pCache
1072e 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a  ->nRef++;.    }.
1072f 20 20 20 20 70 50 61 67 65 2d 3e 6e 52 65 66 2b      pPage->nRef+
10730 2b 3b 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3d  +;.    if( pgno=
10731 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63  =1 ){.      pCac
10732 68 65 2d 3e 70 50 61 67 65 31 20 3d 20 70 50 61  he->pPage1 = pPa
10733 67 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ge;.    }.  }.  
10734 2a 70 70 50 61 67 65 20 3d 20 70 50 61 67 65 3b  *ppPage = pPage;
10735 0a 20 20 72 65 74 75 72 6e 20 28 70 50 61 67 65  .  return (pPage
10736 3d 3d 30 20 26 26 20 65 43 72 65 61 74 65 29 20  ==0 && eCreate) 
10737 3f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 3a  ? SQLITE_NOMEM :
10738 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
10739 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74  *.** Decrement t
1073a 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  he reference cou
1073b 6e 74 20 6f 6e 20 61 20 70 61 67 65 2e 20 49 66  nt on a page. If
1073c 20 74 68 65 20 70 61 67 65 20 69 73 20 63 6c 65   the page is cle
1073d 61 6e 20 61 6e 64 20 74 68 65 0a 2a 2a 20 72 65  an and the.** re
1073e 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 64 72  ference count dr
1073f 6f 70 73 20 74 6f 20 30 2c 20 74 68 65 6e 20 69  ops to 0, then i
10740 74 20 69 73 20 6d 61 64 65 20 65 6c 69 62 6c 65  t is made elible
10741 20 66 6f 72 20 72 65 63 79 63 6c 69 6e 67 2e 0a   for recycling..
10742 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
10743 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
10744 61 63 68 65 52 65 6c 65 61 73 65 28 50 67 48 64  acheRelease(PgHd
10745 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  r *p){.  assert(
10746 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20   p->nRef>0 );.  
10747 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28  p->nRef--;.  if(
10748 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20   p->nRef==0 ){. 
10749 20 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68     PCache *pCach
1074a 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20  e = p->pCache;. 
1074b 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 2d     pCache->nRef-
1074c 2d 3b 0a 20 20 20 20 69 66 28 20 28 70 2d 3e 66  -;.    if( (p->f
1074d 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59  lags&PGHDR_DIRTY
1074e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 63  )==0 ){.      pc
1074f 61 63 68 65 55 6e 70 69 6e 28 70 29 3b 0a 20 20  acheUnpin(p);.  
10750 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f    }else{.      /
10751 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65 20  * Move the page 
10752 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 74  to the head of t
10753 68 65 20 64 69 72 74 79 20 6c 69 73 74 2e 20 2a  he dirty list. *
10754 2f 0a 20 20 20 20 20 20 70 63 61 63 68 65 52 65  /.      pcacheRe
10755 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 73  moveFromDirtyLis
10756 74 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 63  t(p);.      pcac
10757 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 73 74  heAddToDirtyList
10758 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  (p);.    }.  }.}
10759 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 61 73 65  ../*.** Increase
1075a 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63   the reference c
1075b 6f 75 6e 74 20 6f 66 20 61 20 73 75 70 70 6c 69  ount of a suppli
1075c 65 64 20 70 61 67 65 20 62 79 20 31 2e 0a 2a 2f  ed page by 1..*/
1075d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1075e 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
1075f 68 65 52 65 66 28 50 67 48 64 72 20 2a 70 29 7b  heRef(PgHdr *p){
10760 0a 20 20 61 73 73 65 72 74 28 70 2d 3e 6e 52 65  .  assert(p->nRe
10761 66 3e 30 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b  f>0);.  p->nRef+
10762 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70  +;.}../*.** Drop
10763 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65   a page from the
10764 20 63 61 63 68 65 2e 20 54 68 65 72 65 20 6d 75   cache. There mu
10765 73 74 20 62 65 20 65 78 61 63 74 6c 79 20 6f 6e  st be exactly on
10766 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74  e reference to t
10767 68 65 0a 2a 2a 20 70 61 67 65 2e 20 54 68 69 73  he.** page. This
10768 20 66 75 6e 63 74 69 6f 6e 20 64 65 6c 65 74 65   function delete
10769 73 20 74 68 61 74 20 72 65 66 65 72 65 6e 63 65  s that reference
1076a 2c 20 73 6f 20 61 66 74 65 72 20 69 74 20 72 65  , so after it re
1076b 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 70 61 67  turns the.** pag
1076c 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  e pointed to by 
1076d 70 20 69 73 20 69 6e 76 61 6c 69 64 2e 0a 2a 2f  p is invalid..*/
1076e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1076f 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
10770 68 65 44 72 6f 70 28 50 67 48 64 72 20 2a 70 29  heDrop(PgHdr *p)
10771 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63  {.  PCache *pCac
10772 68 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  he;.  assert( p-
10773 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 20 69 66  >nRef==1 );.  if
10774 28 20 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  ( p->flags&PGHDR
10775 5f 44 49 52 54 59 20 29 7b 0a 20 20 20 20 70 63  _DIRTY ){.    pc
10776 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69  acheRemoveFromDi
10777 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 7d 0a  rtyList(p);.  }.
10778 20 20 70 43 61 63 68 65 20 3d 20 70 2d 3e 70 43    pCache = p->pC
10779 61 63 68 65 3b 0a 20 20 70 43 61 63 68 65 2d 3e  ache;.  pCache->
1077a 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 2d  nRef--;.  if( p-
1077b 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20  >pgno==1 ){.    
1077c 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 20 3d  pCache->pPage1 =
1077d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   0;.  }.  sqlite
1077e 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
1077f 61 63 68 65 2e 78 55 6e 70 69 6e 28 70 43 61 63  ache.xUnpin(pCac
10780 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c 20 31  he->pCache, p, 1
10781 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65  );.}../*.** Make
10782 20 73 75 72 65 20 74 68 65 20 70 61 67 65 20 69   sure the page i
10783 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74  s marked as dirt
10784 79 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 64  y. If it isn't d
10785 69 72 74 79 20 61 6c 72 65 61 64 79 2c 0a 2a 2a  irty already,.**
10786 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a   make it so..*/.
10787 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
10788 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
10789 65 4d 61 6b 65 44 69 72 74 79 28 50 67 48 64 72  eMakeDirty(PgHdr
1078a 20 2a 70 29 7b 0a 20 20 70 2d 3e 66 6c 61 67 73   *p){.  p->flags
1078b 20 26 3d 20 7e 50 47 48 44 52 5f 44 4f 4e 54 5f   &= ~PGHDR_DONT_
1078c 57 52 49 54 45 3b 0a 20 20 61 73 73 65 72 74 28  WRITE;.  assert(
1078d 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20   p->nRef>0 );.  
1078e 69 66 28 20 30 3d 3d 28 70 2d 3e 66 6c 61 67 73  if( 0==(p->flags
1078f 20 26 20 50 47 48 44 52 5f 44 49 52 54 59 29 20   & PGHDR_DIRTY) 
10790 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20  ){.    p->flags 
10791 7c 3d 20 50 47 48 44 52 5f 44 49 52 54 59 3b 0a  |= PGHDR_DIRTY;.
10792 20 20 20 20 70 63 61 63 68 65 41 64 64 54 6f 44      pcacheAddToD
10793 69 72 74 79 4c 69 73 74 28 20 70 29 3b 0a 20 20  irtyList( p);.  
10794 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  }.}../*.** Make 
10795 73 75 72 65 20 74 68 65 20 70 61 67 65 20 69 73  sure the page is
10796 20 6d 61 72 6b 65 64 20 61 73 20 63 6c 65 61 6e   marked as clean
10797 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 63 6c  . If it isn't cl
10798 65 61 6e 20 61 6c 72 65 61 64 79 2c 0a 2a 2a 20  ean already,.** 
10799 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a 53  make it so..*/.S
1079a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1079b 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
1079c 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64 72 20  MakeClean(PgHdr 
1079d 2a 70 29 7b 0a 20 20 69 66 28 20 28 70 2d 3e 66  *p){.  if( (p->f
1079e 6c 61 67 73 20 26 20 50 47 48 44 52 5f 44 49 52  lags & PGHDR_DIR
1079f 54 59 29 20 29 7b 0a 20 20 20 20 70 63 61 63 68  TY) ){.    pcach
107a0 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79  eRemoveFromDirty
107a1 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 2d 3e  List(p);.    p->
107a2 66 6c 61 67 73 20 26 3d 20 7e 28 50 47 48 44 52  flags &= ~(PGHDR
107a3 5f 44 49 52 54 59 7c 50 47 48 44 52 5f 4e 45 45  _DIRTY|PGHDR_NEE
107a4 44 5f 53 59 4e 43 29 3b 0a 20 20 20 20 69 66 28  D_SYNC);.    if(
107a5 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20   p->nRef==0 ){. 
107a6 20 20 20 20 20 70 63 61 63 68 65 55 6e 70 69 6e       pcacheUnpin
107a7 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  (p);.    }.  }.}
107a8 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 65 76 65  ../*.** Make eve
107a9 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 63  ry page in the c
107aa 61 63 68 65 20 63 6c 65 61 6e 2e 0a 2a 2f 0a 53  ache clean..*/.S
107ab 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
107ac 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
107ad 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 20  CleanAll(PCache 
107ae 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64  *pCache){.  PgHd
107af 72 20 2a 70 3b 0a 20 20 77 68 69 6c 65 28 20 28  r *p;.  while( (
107b0 70 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72  p = pCache->pDir
107b1 74 79 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71  ty)!=0 ){.    sq
107b2 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43  lite3PcacheMakeC
107b3 6c 65 61 6e 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a  lean(p);.  }.}..
107b4 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20  /*.** Clear the 
107b5 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20  PGHDR_NEED_SYNC 
107b6 66 6c 61 67 20 66 72 6f 6d 20 61 6c 6c 20 64 69  flag from all di
107b7 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 53 51  rty pages..*/.SQ
107b8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
107b9 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43  d sqlite3PcacheC
107ba 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28 50 43  learSyncFlags(PC
107bb 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20  ache *pCache){. 
107bc 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72   PgHdr *p;.  for
107bd 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74  (p=pCache->pDirt
107be 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74  y; p; p=p->pDirt
107bf 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 66  yNext){.    p->f
107c0 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e  lags &= ~PGHDR_N
107c1 45 45 44 5f 53 59 4e 43 3b 0a 20 20 7d 0a 20 20  EED_SYNC;.  }.  
107c2 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 20  pCache->pSynced 
107c3 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79  = pCache->pDirty
107c4 54 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  Tail;.}../*.** C
107c5 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 6e  hange the page n
107c6 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 70 20  umber of page p 
107c7 74 6f 20 6e 65 77 50 67 6e 6f 2e 20 0a 2a 2f 0a  to newPgno. .*/.
107c8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
107c9 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
107ca 65 4d 6f 76 65 28 50 67 48 64 72 20 2a 70 2c 20  eMove(PgHdr *p, 
107cb 50 67 6e 6f 20 6e 65 77 50 67 6e 6f 29 7b 0a 20  Pgno newPgno){. 
107cc 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 20   PCache *pCache 
107cd 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20 61  = p->pCache;.  a
107ce 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30  ssert( p->nRef>0
107cf 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 65   );.  assert( ne
107d0 77 50 67 6e 6f 3e 30 20 29 3b 0a 20 20 73 71 6c  wPgno>0 );.  sql
107d1 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
107d2 2e 70 63 61 63 68 65 2e 78 52 65 6b 65 79 28 70  .pcache.xRekey(p
107d3 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70  Cache->pCache, p
107d4 2c 20 70 2d 3e 70 67 6e 6f 2c 20 6e 65 77 50 67  , p->pgno, newPg
107d5 6e 6f 29 3b 0a 20 20 70 2d 3e 70 67 6e 6f 20 3d  no);.  p->pgno =
107d6 20 6e 65 77 50 67 6e 6f 3b 0a 20 20 69 66 28 20   newPgno;.  if( 
107d7 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  (p->flags&PGHDR_
107d8 44 49 52 54 59 29 20 26 26 20 28 70 2d 3e 66 6c  DIRTY) && (p->fl
107d9 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53  ags&PGHDR_NEED_S
107da 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 63 61 63  YNC) ){.    pcac
107db 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74  heRemoveFromDirt
107dc 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 63  yList(p);.    pc
107dd 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69  acheAddToDirtyLi
107de 73 74 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  st(p);.  }.}../*
107df 0a 2a 2a 20 44 72 6f 70 20 65 76 65 72 79 20 63  .** Drop every c
107e0 61 63 68 65 20 65 6e 74 72 79 20 77 68 6f 73 65  ache entry whose
107e1 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20   page number is 
107e2 67 72 65 61 74 65 72 20 74 68 61 6e 20 22 70 67  greater than "pg
107e3 6e 6f 22 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c  no". The.** call
107e4 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74  er must ensure t
107e5 68 61 74 20 74 68 65 72 65 20 61 72 65 20 6e 6f  hat there are no
107e6 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66   outstanding ref
107e7 65 72 65 6e 63 65 73 20 74 6f 20 61 6e 79 20 70  erences to any p
107e8 61 67 65 73 0a 2a 2a 20 6f 74 68 65 72 20 74 68  ages.** other th
107e9 61 6e 20 70 61 67 65 20 31 20 77 69 74 68 20 61  an page 1 with a
107ea 20 70 61 67 65 20 6e 75 6d 62 65 72 20 67 72 65   page number gre
107eb 61 74 65 72 20 74 68 61 6e 20 70 67 6e 6f 2e 0a  ater than pgno..
107ec 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69  **.** If there i
107ed 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  s a reference to
107ee 20 70 61 67 65 20 31 20 61 6e 64 20 74 68 65 20   page 1 and the 
107ef 70 67 6e 6f 20 70 61 72 61 6d 65 74 65 72 20 70  pgno parameter p
107f0 61 73 73 65 64 20 74 6f 20 74 68 69 73 0a 2a 2a  assed to this.**
107f1 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 30 2c 20   function is 0, 
107f2 74 68 65 6e 20 74 68 65 20 64 61 74 61 20 61 72  then the data ar
107f3 65 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ea associated wi
107f4 74 68 20 70 61 67 65 20 31 20 69 73 20 7a 65 72  th page 1 is zer
107f5 6f 65 64 2c 20 62 75 74 0a 2a 2a 20 74 68 65 20  oed, but.** the 
107f6 70 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20 6e  page object is n
107f7 6f 74 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a 53  ot dropped..*/.S
107f8 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
107f9 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
107fa 54 72 75 6e 63 61 74 65 28 50 43 61 63 68 65 20  Truncate(PCache 
107fb 2a 70 43 61 63 68 65 2c 20 50 67 6e 6f 20 70 67  *pCache, Pgno pg
107fc 6e 6f 29 7b 0a 20 20 69 66 28 20 70 43 61 63 68  no){.  if( pCach
107fd 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20  e->pCache ){.   
107fe 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 20 20 50   PgHdr *p;.    P
107ff 67 48 64 72 20 2a 70 4e 65 78 74 3b 0a 20 20 20  gHdr *pNext;.   
10800 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70   for(p=pCache->p
10801 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 4e 65 78  Dirty; p; p=pNex
10802 74 29 7b 0a 20 20 20 20 20 20 70 4e 65 78 74 20  t){.      pNext 
10803 3d 20 70 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b  = p->pDirtyNext;
10804 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 67  .      if( p->pg
10805 6e 6f 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20  no>pgno ){.     
10806 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 66 6c     assert( p->fl
10807 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 20  ags&PGHDR_DIRTY 
10808 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
10809 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61  e3PcacheMakeClea
1080a 6e 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n(p);.      }.  
1080b 20 20 7d 0a 20 20 20 20 69 66 28 20 70 67 6e 6f    }.    if( pgno
1080c 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e 70  ==0 && pCache->p
1080d 50 61 67 65 31 20 29 7b 0a 20 20 20 20 20 20 6d  Page1 ){.      m
1080e 65 6d 73 65 74 28 70 43 61 63 68 65 2d 3e 70 50  emset(pCache->pP
1080f 61 67 65 31 2d 3e 70 44 61 74 61 2c 20 30 2c 20  age1->pData, 0, 
10810 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 29 3b  pCache->szPage);
10811 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 31 3b  .      pgno = 1;
10812 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
10813 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
10814 63 61 63 68 65 2e 78 54 72 75 6e 63 61 74 65 28  cache.xTruncate(
10815 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20  pCache->pCache, 
10816 70 67 6e 6f 2b 31 29 3b 0a 20 20 7d 0a 7d 0a 0a  pgno+1);.  }.}..
10817 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 61  /*.** Close a ca
10818 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  che..*/.SQLITE_P
10819 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1081a 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 50  te3PcacheClose(P
1081b 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a  Cache *pCache){.
1081c 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43    if( pCache->pC
1081d 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  ache ){.    sqli
1081e 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
1081f 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79 28  pcache.xDestroy(
10820 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 3b  pCache->pCache);
10821 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 44  .  }.}../* .** D
10822 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65  iscard the conte
10823 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63 68 65  nts of the cache
10824 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
10825 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
10826 50 63 61 63 68 65 43 6c 65 61 72 28 50 43 61 63  PcacheClear(PCac
10827 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 73  he *pCache){.  s
10828 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e  qlite3PcacheTrun
10829 63 61 74 65 28 70 43 61 63 68 65 2c 20 30 29 3b  cate(pCache, 0);
1082a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 20  .}../*.** Merge 
1082b 74 77 6f 20 6c 69 73 74 73 20 6f 66 20 70 61 67  two lists of pag
1082c 65 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20  es connected by 
1082d 70 44 69 72 74 79 20 61 6e 64 20 69 6e 20 70 67  pDirty and in pg
1082e 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a 20 44 6f 20  no order..** Do 
1082f 6e 6f 74 20 62 6f 74 68 20 66 69 78 69 6e 67 20  not both fixing 
10830 74 68 65 20 70 44 69 72 74 79 50 72 65 76 20 70  the pDirtyPrev p
10831 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74  ointers..*/.stat
10832 69 63 20 50 67 48 64 72 20 2a 70 63 61 63 68 65  ic PgHdr *pcache
10833 4d 65 72 67 65 44 69 72 74 79 4c 69 73 74 28 50  MergeDirtyList(P
10834 67 48 64 72 20 2a 70 41 2c 20 50 67 48 64 72 20  gHdr *pA, PgHdr 
10835 2a 70 42 29 7b 0a 20 20 50 67 48 64 72 20 72 65  *pB){.  PgHdr re
10836 73 75 6c 74 2c 20 2a 70 54 61 69 6c 3b 0a 20 20  sult, *pTail;.  
10837 70 54 61 69 6c 20 3d 20 26 72 65 73 75 6c 74 3b  pTail = &result;
10838 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26 26 20  .  while( pA && 
10839 70 42 20 29 7b 0a 20 20 20 20 69 66 28 20 70 41  pB ){.    if( pA
1083a 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67 6e 6f 20  ->pgno<pB->pgno 
1083b 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e  ){.      pTail->
1083c 70 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 20 20  pDirty = pA;.   
1083d 20 20 20 70 54 61 69 6c 20 3d 20 70 41 3b 0a 20     pTail = pA;. 
1083e 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 44       pA = pA->pD
1083f 69 72 74 79 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  irty;.    }else{
10840 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 44  .      pTail->pD
10841 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 20 20 20  irty = pB;.     
10842 20 70 54 61 69 6c 20 3d 20 70 42 3b 0a 20 20 20   pTail = pB;.   
10843 20 20 20 70 42 20 3d 20 70 42 2d 3e 70 44 69 72     pB = pB->pDir
10844 74 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ty;.    }.  }.  
10845 69 66 28 20 70 41 20 29 7b 0a 20 20 20 20 70 54  if( pA ){.    pT
10846 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 41  ail->pDirty = pA
10847 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42  ;.  }else if( pB
10848 20 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e 70   ){.    pTail->p
10849 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 7d 65  Dirty = pB;.  }e
1084a 6c 73 65 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e  lse{.    pTail->
1084b 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 7d 0a  pDirty = 0;.  }.
1084c 20 20 72 65 74 75 72 6e 20 72 65 73 75 6c 74 2e    return result.
1084d 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pDirty;.}../*.**
1084e 20 53 6f 72 74 20 74 68 65 20 6c 69 73 74 20 6f   Sort the list o
1084f 66 20 70 61 67 65 73 20 69 6e 20 61 63 63 65 6e  f pages in accen
10850 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70 67  ding order by pg
10851 6e 6f 2e 20 20 50 61 67 65 73 20 61 72 65 0a 2a  no.  Pages are.*
10852 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70  * connected by p
10853 44 69 72 74 79 20 70 6f 69 6e 74 65 72 73 2e 20  Dirty pointers. 
10854 20 54 68 65 20 70 44 69 72 74 79 50 72 65 76 20   The pDirtyPrev 
10855 70 6f 69 6e 74 65 72 73 20 61 72 65 0a 2a 2a 20  pointers are.** 
10856 63 6f 72 72 75 70 74 65 64 20 62 79 20 74 68 69  corrupted by thi
10857 73 20 73 6f 72 74 2e 0a 2a 2a 0a 2a 2a 20 53 69  s sort..**.** Si
10858 6e 63 65 20 74 68 65 72 65 20 63 61 6e 6e 6f 74  nce there cannot
10859 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 32 5e   be more than 2^
1085a 33 31 20 64 69 73 74 69 6e 63 74 20 70 61 67 65  31 distinct page
1085b 73 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 2c  s in a database,
1085c 0a 2a 2a 20 74 68 65 72 65 20 63 61 6e 6e 6f 74  .** there cannot
1085d 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 33 31   be more than 31
1085e 20 62 75 63 6b 65 74 73 20 72 65 71 75 69 72 65   buckets require
1085f 64 20 62 79 20 74 68 65 20 6d 65 72 67 65 20 73  d by the merge s
10860 6f 72 74 65 72 2e 0a 2a 2a 20 4f 6e 65 20 65 78  orter..** One ex
10861 74 72 61 20 62 75 63 6b 65 74 20 69 73 20 61 64  tra bucket is ad
10862 64 65 64 20 74 6f 20 63 61 74 63 68 20 6f 76 65  ded to catch ove
10863 72 66 6c 6f 77 20 69 6e 20 63 61 73 65 20 73 6f  rflow in case so
10864 6d 65 74 68 69 6e 67 0a 2a 2a 20 65 76 65 72 20  mething.** ever 
10865 63 68 61 6e 67 65 73 20 74 6f 20 6d 61 6b 65 20  changes to make 
10866 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e  the previous sen
10867 74 65 6e 63 65 20 69 6e 63 6f 72 72 65 63 74 2e  tence incorrect.
10868 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 53 4f  .*/.#define N_SO
10869 52 54 5f 42 55 43 4b 45 54 20 20 33 32 0a 73 74  RT_BUCKET  32.st
1086a 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 63  atic PgHdr *pcac
1086b 68 65 53 6f 72 74 44 69 72 74 79 4c 69 73 74 28  heSortDirtyList(
1086c 50 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20 20 50  PgHdr *pIn){.  P
1086d 67 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54 5f 42  gHdr *a[N_SORT_B
1086e 55 43 4b 45 54 5d 2c 20 2a 70 3b 0a 20 20 69 6e  UCKET], *p;.  in
1086f 74 20 69 3b 0a 20 20 6d 65 6d 73 65 74 28 61 2c  t i;.  memset(a,
10870 20 30 2c 20 73 69 7a 65 6f 66 28 61 29 29 3b 0a   0, sizeof(a));.
10871 20 20 77 68 69 6c 65 28 20 70 49 6e 20 29 7b 0a    while( pIn ){.
10872 20 20 20 20 70 20 3d 20 70 49 6e 3b 0a 20 20 20      p = pIn;.   
10873 20 70 49 6e 20 3d 20 70 2d 3e 70 44 69 72 74 79   pIn = p->pDirty
10874 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 20  ;.    p->pDirty 
10875 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  = 0;.    for(i=0
10876 3b 20 41 4c 57 41 59 53 28 69 3c 4e 5f 53 4f 52  ; ALWAYS(i<N_SOR
10877 54 5f 42 55 43 4b 45 54 2d 31 29 3b 20 69 2b 2b  T_BUCKET-1); i++
10878 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 5b 69  ){.      if( a[i
10879 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ]==0 ){.        
1087a 61 5b 69 5d 20 3d 20 70 3b 0a 20 20 20 20 20 20  a[i] = p;.      
1087b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
1087c 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 20  else{.        p 
1087d 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72  = pcacheMergeDir
1087e 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b  tyList(a[i], p);
1087f 0a 20 20 20 20 20 20 20 20 61 5b 69 5d 20 3d 20  .        a[i] = 
10880 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  0;.      }.    }
10881 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 69  .    if( NEVER(i
10882 3d 3d 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 2d  ==N_SORT_BUCKET-
10883 31 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  1) ){.      /* T
10884 6f 20 67 65 74 20 68 65 72 65 2c 20 74 68 65 72  o get here, ther
10885 65 20 6e 65 65 64 20 74 6f 20 62 65 20 32 5e 28  e need to be 2^(
10886 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 29 20 65  N_SORT_BUCKET) e
10887 6c 65 6d 65 6e 74 73 20 69 6e 0a 20 20 20 20 20  lements in.     
10888 20 2a 2a 20 74 68 65 20 69 6e 70 75 74 20 6c 69   ** the input li
10889 73 74 2e 20 20 42 75 74 20 74 68 61 74 20 69 73  st.  But that is
1088a 20 69 6d 70 6f 73 73 69 62 6c 65 2e 0a 20 20 20   impossible..   
1088b 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 5b 69 5d     */.      a[i]
1088c 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69   = pcacheMergeDi
1088d 72 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29  rtyList(a[i], p)
1088e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 20  ;.    }.  }.  p 
1088f 3d 20 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d  = a[0];.  for(i=
10890 31 3b 20 69 3c 4e 5f 53 4f 52 54 5f 42 55 43 4b  1; i<N_SORT_BUCK
10891 45 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20  ET; i++){.    p 
10892 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72  = pcacheMergeDir
10893 74 79 4c 69 73 74 28 70 2c 20 61 5b 69 5d 29 3b  tyList(p, a[i]);
10894 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
10895 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
10896 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 64   a list of all d
10897 69 72 74 79 20 70 61 67 65 73 20 69 6e 20 74 68  irty pages in th
10898 65 20 63 61 63 68 65 2c 20 73 6f 72 74 65 64 20  e cache, sorted 
10899 62 79 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a  by page number..
1089a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1089b 45 20 50 67 48 64 72 20 2a 73 71 6c 69 74 65 33  E PgHdr *sqlite3
1089c 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 74 28  PcacheDirtyList(
1089d 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b  PCache *pCache){
1089e 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66  .  PgHdr *p;.  f
1089f 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69  or(p=pCache->pDi
108a0 72 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69  rty; p; p=p->pDi
108a1 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d  rtyNext){.    p-
108a2 3e 70 44 69 72 74 79 20 3d 20 70 2d 3e 70 44 69  >pDirty = p->pDi
108a3 72 74 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72  rtyNext;.  }.  r
108a4 65 74 75 72 6e 20 70 63 61 63 68 65 53 6f 72 74  eturn pcacheSort
108a5 44 69 72 74 79 4c 69 73 74 28 70 43 61 63 68 65  DirtyList(pCache
108a6 2d 3e 70 44 69 72 74 79 29 3b 0a 7d 0a 0a 2f 2a  ->pDirty);.}../*
108a7 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20   .** Return the 
108a8 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
108a9 72 65 66 65 72 65 6e 63 65 64 20 70 61 67 65 73  referenced pages
108aa 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 63   held by the cac
108ab 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  he..*/.SQLITE_PR
108ac 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
108ad 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28  3PcacheRefCount(
108ae 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b  PCache *pCache){
108af 0a 20 20 72 65 74 75 72 6e 20 70 43 61 63 68 65  .  return pCache
108b0 2d 3e 6e 52 65 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ->nRef;.}../*.**
108b1 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
108b2 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73  er of references
108b3 20 74 6f 20 74 68 65 20 70 61 67 65 20 73 75 70   to the page sup
108b4 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 75  plied as an argu
108b5 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ment..*/.SQLITE_
108b6 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
108b7 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65 66  te3PcachePageRef
108b8 63 6f 75 6e 74 28 50 67 48 64 72 20 2a 70 29 7b  count(PgHdr *p){
108b9 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65  .  return p->nRe
108ba 66 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74  f;.}../* .** Ret
108bb 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75  urn the total nu
108bc 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
108bd 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53   the cache..*/.S
108be 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
108bf 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50  t sqlite3PcacheP
108c0 61 67 65 63 6f 75 6e 74 28 50 43 61 63 68 65 20  agecount(PCache 
108c1 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e 74 20  *pCache){.  int 
108c2 6e 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 66 28  nPage = 0;.  if(
108c3 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20   pCache->pCache 
108c4 29 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 73  ){.    nPage = s
108c5 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
108c6 69 67 2e 70 63 61 63 68 65 2e 78 50 61 67 65 63  ig.pcache.xPagec
108c7 6f 75 6e 74 28 70 43 61 63 68 65 2d 3e 70 43 61  ount(pCache->pCa
108c8 63 68 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  che);.  }.  retu
108c9 72 6e 20 6e 50 61 67 65 3b 0a 7d 0a 0a 23 69 66  rn nPage;.}..#if
108ca 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
108cb 2f 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20 73 75  /*.** Get the su
108cc 67 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 69  ggested cache-si
108cd 7a 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c  ze value..*/.SQL
108ce 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
108cf 73 71 6c 69 74 65 33 50 63 61 63 68 65 47 65 74  sqlite3PcacheGet
108d0 43 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 65  Cachesize(PCache
108d1 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 65 74   *pCache){.  ret
108d2 75 72 6e 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78  urn pCache->nMax
108d3 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
108d4 2a 20 53 65 74 20 74 68 65 20 73 75 67 67 65 73  * Set the sugges
108d5 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20 76  ted cache-size v
108d6 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  alue..*/.SQLITE_
108d7 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
108d8 69 74 65 33 50 63 61 63 68 65 53 65 74 43 61 63  ite3PcacheSetCac
108d9 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 2a 70  hesize(PCache *p
108da 43 61 63 68 65 2c 20 69 6e 74 20 6d 78 50 61 67  Cache, int mxPag
108db 65 29 7b 0a 20 20 70 43 61 63 68 65 2d 3e 6e 4d  e){.  pCache->nM
108dc 61 78 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20 69  ax = mxPage;.  i
108dd 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68  f( pCache->pCach
108de 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  e ){.    sqlite3
108df 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61  GlobalConfig.pca
108e0 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65 28 70  che.xCachesize(p
108e1 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 6d  Cache->pCache, m
108e2 78 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 23  xPage);.  }.}..#
108e3 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
108e4 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 29 20 7c  E_CHECK_PAGES) |
108e5 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
108e6 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 46 6f  _DEBUG)./*.** Fo
108e7 72 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65  r all dirty page
108e8 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74  s currently in t
108e9 68 65 20 63 61 63 68 65 2c 20 69 6e 76 6f 6b 65  he cache, invoke
108ea 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a   the specified.*
108eb 2a 20 63 61 6c 6c 62 61 63 6b 2e 20 54 68 69 73  * callback. This
108ec 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66   is only used if
108ed 20 74 68 65 20 53 51 4c 49 54 45 5f 43 48 45 43   the SQLITE_CHEC
108ee 4b 5f 50 41 47 45 53 20 6d 61 63 72 6f 20 69 73  K_PAGES macro is
108ef 0a 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a  .** defined..*/.
108f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
108f1 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
108f2 65 49 74 65 72 61 74 65 44 69 72 74 79 28 50 43  eIterateDirty(PC
108f3 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76 6f  ache *pCache, vo
108f4 69 64 20 28 2a 78 49 74 65 72 29 28 50 67 48 64  id (*xIter)(PgHd
108f5 72 20 2a 29 29 7b 0a 20 20 50 67 48 64 72 20 2a  r *)){.  PgHdr *
108f6 70 44 69 72 74 79 3b 0a 20 20 66 6f 72 28 70 44  pDirty;.  for(pD
108f7 69 72 74 79 3d 70 43 61 63 68 65 2d 3e 70 44 69  irty=pCache->pDi
108f8 72 74 79 3b 20 70 44 69 72 74 79 3b 20 70 44 69  rty; pDirty; pDi
108f9 72 74 79 3d 70 44 69 72 74 79 2d 3e 70 44 69 72  rty=pDirty->pDir
108fa 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 78 49 74  tyNext){.    xIt
108fb 65 72 28 70 44 69 72 74 79 29 3b 0a 20 20 7d 0a  er(pDirty);.  }.
108fc 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  }.#endif../*****
108fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
108fe 20 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a   pcache.c ******
108ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10901 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
10902 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
10903 66 69 6c 65 20 70 63 61 63 68 65 31 2e 63 20 2a  file pcache1.c *
10904 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10905 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10906 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
10907 32 30 30 38 20 4e 6f 76 65 6d 62 65 72 20 30 35  2008 November 05
10908 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
10909 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
1090a 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
1090b 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
1090c 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
1090d 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
1090e 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
1090f 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
10910 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
10911 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
10912 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
10913 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
10914 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
10915 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
10916 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
10917 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
10918 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
10919 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
1091a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1091b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1091c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1091d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1091e 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
1091f 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 64  implements the d
10920 65 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68  efault page cach
10921 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
10922 20 28 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33   (the.** sqlite3
10923 5f 70 63 61 63 68 65 20 69 6e 74 65 72 66 61 63  _pcache interfac
10924 65 29 2e 20 49 74 20 61 6c 73 6f 20 63 6f 6e 74  e). It also cont
10925 61 69 6e 73 20 70 61 72 74 20 6f 66 20 74 68 65  ains part of the
10926 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a   implementation.
10927 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45  ** of the SQLITE
10928 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48  _CONFIG_PAGECACH
10929 45 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65  E and sqlite3_re
1092a 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 66  lease_memory() f
1092b 65 61 74 75 72 65 73 2e 0a 2a 2a 20 49 66 20 74  eatures..** If t
1092c 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 20  he default page 
1092d 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  cache implementa
1092e 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 65  tion is override
1092f 6e 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20  n, then neither 
10930 6f 66 0a 2a 2a 20 74 68 65 73 65 20 74 77 6f 20  of.** these two 
10931 66 65 61 74 75 72 65 73 20 61 72 65 20 61 76 61  features are ava
10932 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 40 28  ilable..**.** @(
10933 23 29 20 24 49 64 3a 20 70 63 61 63 68 65 31 2e  #) $Id: pcache1.
10934 63 2c 76 20 31 2e 31 39 20 32 30 30 39 2f 30 37  c,v 1.19 2009/07
10935 2f 31 37 20 31 31 3a 34 34 3a 30 37 20 64 72 68  /17 11:44:07 drh
10936 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 74 79 70 65   Exp $.*/...type
10937 64 65 66 20 73 74 72 75 63 74 20 50 43 61 63 68  def struct PCach
10938 65 31 20 50 43 61 63 68 65 31 3b 0a 74 79 70 65  e1 PCache1;.type
10939 64 65 66 20 73 74 72 75 63 74 20 50 67 48 64 72  def struct PgHdr
1093a 31 20 50 67 48 64 72 31 3b 0a 74 79 70 65 64 65  1 PgHdr1;.typede
1093b 66 20 73 74 72 75 63 74 20 50 67 46 72 65 65 73  f struct PgFrees
1093c 6c 6f 74 20 50 67 46 72 65 65 73 6c 6f 74 3b 0a  lot PgFreeslot;.
1093d 0a 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20  ./* Pointers to 
1093e 73 74 72 75 63 74 75 72 65 73 20 6f 66 20 74 68  structures of th
1093f 69 73 20 74 79 70 65 20 61 72 65 20 63 61 73 74  is type are cast
10940 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 61 73   and returned as
10941 20 0a 2a 2a 20 6f 70 61 71 75 65 20 73 71 6c 69   .** opaque sqli
10942 74 65 33 5f 70 63 61 63 68 65 2a 20 68 61 6e 64  te3_pcache* hand
10943 6c 65 73 0a 2a 2f 0a 73 74 72 75 63 74 20 50 43  les.*/.struct PC
10944 61 63 68 65 31 20 7b 0a 20 20 2f 2a 20 43 61 63  ache1 {.  /* Cac
10945 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  he configuration
10946 20 70 61 72 61 6d 65 74 65 72 73 2e 20 50 61 67   parameters. Pag
10947 65 20 73 69 7a 65 20 28 73 7a 50 61 67 65 29 20  e size (szPage) 
10948 61 6e 64 20 74 68 65 20 70 75 72 67 65 61 62 6c  and the purgeabl
10949 65 0a 20 20 2a 2a 20 66 6c 61 67 20 28 62 50 75  e.  ** flag (bPu
1094a 72 67 65 61 62 6c 65 29 20 61 72 65 20 73 65 74  rgeable) are set
1094b 20 77 68 65 6e 20 74 68 65 20 63 61 63 68 65 20   when the cache 
1094c 69 73 20 63 72 65 61 74 65 64 2e 20 6e 4d 61 78  is created. nMax
1094d 20 6d 61 79 20 62 65 20 0a 20 20 2a 2a 20 6d 6f   may be .  ** mo
1094e 64 69 66 69 65 64 20 61 74 20 61 6e 79 20 74 69  dified at any ti
1094f 6d 65 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20  me by a call to 
10950 74 68 65 20 70 63 61 63 68 65 31 43 61 63 68 65  the pcache1Cache
10951 53 69 7a 65 28 29 20 6d 65 74 68 6f 64 2e 0a 20  Size() method.. 
10952 20 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20 6d   ** The global m
10953 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c  utex must be hel
10954 64 20 77 68 65 6e 20 61 63 63 65 73 73 69 6e 67  d when accessing
10955 20 6e 4d 61 78 2e 0a 20 20 2a 2f 0a 20 20 69 6e   nMax..  */.  in
10956 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20  t szPage;       
10957 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10958 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 6c 6c    /* Size of all
10959 6f 63 61 74 65 64 20 70 61 67 65 73 20 69 6e 20  ocated pages in 
1095a 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 62  bytes */.  int b
1095b 50 75 72 67 65 61 62 6c 65 3b 20 20 20 20 20 20  Purgeable;      
1095c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1095d 2a 20 54 72 75 65 20 69 66 20 63 61 63 68 65 20  * True if cache 
1095e 69 73 20 70 75 72 67 65 61 62 6c 65 20 2a 2f 0a  is purgeable */.
1095f 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e    unsigned int n
10960 4d 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  Min;            
10961 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d        /* Minimum
10962 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
10963 20 72 65 73 65 72 76 65 64 20 2a 2f 0a 20 20 75   reserved */.  u
10964 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 4d 61 78  nsigned int nMax
10965 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
10966 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 64     /* Configured
10967 20 22 63 61 63 68 65 5f 73 69 7a 65 22 20 76 61   "cache_size" va
10968 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a 20 48 61 73  lue */..  /* Has
10969 68 20 74 61 62 6c 65 20 6f 66 20 61 6c 6c 20 70  h table of all p
1096a 61 67 65 73 2e 20 54 68 65 20 66 6f 6c 6c 6f 77  ages. The follow
1096b 69 6e 67 20 76 61 72 69 61 62 6c 65 73 20 6d 61  ing variables ma
1096c 79 20 6f 6e 6c 79 20 62 65 20 61 63 63 65 73 73  y only be access
1096d 65 64 0a 20 20 2a 2a 20 77 68 65 6e 20 74 68 65  ed.  ** when the
1096e 20 61 63 63 65 73 73 6f 72 20 69 73 20 68 6f 6c   accessor is hol
1096f 64 69 6e 67 20 74 68 65 20 67 6c 6f 62 61 6c 20  ding the global 
10970 6d 75 74 65 78 20 28 73 65 65 20 70 63 61 63 68  mutex (see pcach
10971 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 20 0a  e1EnterMutex() .
10972 20 20 2a 2a 20 61 6e 64 20 70 63 61 63 68 65 31    ** and pcache1
10973 4c 65 61 76 65 4d 75 74 65 78 28 29 29 2e 0a 20  LeaveMutex()).. 
10974 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69   */.  unsigned i
10975 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c 65 3b 20  nt nRecyclable; 
10976 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
10977 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
10978 74 68 65 20 4c 52 55 20 6c 69 73 74 20 2a 2f 0a  the LRU list */.
10979 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e    unsigned int n
1097a 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  Page;           
1097b 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e        /* Total n
1097c 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
1097d 6e 20 61 70 48 61 73 68 20 2a 2f 0a 20 20 75 6e  n apHash */.  un
1097e 73 69 67 6e 65 64 20 69 6e 74 20 6e 48 61 73 68  signed int nHash
1097f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
10980 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73    /* Number of s
10981 6c 6f 74 73 20 69 6e 20 61 70 48 61 73 68 5b 5d  lots in apHash[]
10982 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a 2a 61   */.  PgHdr1 **a
10983 70 48 61 73 68 3b 20 20 20 20 20 20 20 20 20 20  pHash;          
10984 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73            /* Has
10985 68 20 74 61 62 6c 65 20 66 6f 72 20 66 61 73 74  h table for fast
10986 20 6c 6f 6f 6b 75 70 20 62 79 20 6b 65 79 20 2a   lookup by key *
10987 2f 0a 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  /..  unsigned in
10988 74 20 69 4d 61 78 4b 65 79 3b 20 20 20 20 20 20  t iMaxKey;      
10989 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 72 67           /* Larg
1098a 65 73 74 20 6b 65 79 20 73 65 65 6e 20 73 69 6e  est key seen sin
1098b 63 65 20 78 54 72 75 6e 63 61 74 65 28 29 20 2a  ce xTruncate() *
1098c 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68  /.};../*.** Each
1098d 20 63 61 63 68 65 20 65 6e 74 72 79 20 69 73 20   cache entry is 
1098e 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 61  represented by a
1098f 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
10990 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20  e following .** 
10991 73 74 72 75 63 74 75 72 65 2e 20 41 20 62 75 66  structure. A buf
10992 66 65 72 20 6f 66 20 50 67 48 64 72 31 2e 70 43  fer of PgHdr1.pC
10993 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 62 79 74  ache->szPage byt
10994 65 73 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  es is allocated 
10995 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 62 65 66  .** directly bef
10996 6f 72 65 20 74 68 69 73 20 73 74 72 75 63 74 75  ore this structu
10997 72 65 20 69 6e 20 6d 65 6d 6f 72 79 20 28 73 65  re in memory (se
10998 65 20 74 68 65 20 50 47 48 44 52 31 5f 54 4f 5f  e the PGHDR1_TO_
10999 50 41 47 45 28 29 20 0a 2a 2a 20 6d 61 63 72 6f  PAGE() .** macro
1099a 20 62 65 6c 6f 77 29 2e 0a 2a 2f 0a 73 74 72 75   below)..*/.stru
1099b 63 74 20 50 67 48 64 72 31 20 7b 0a 20 20 75 6e  ct PgHdr1 {.  un
1099c 73 69 67 6e 65 64 20 69 6e 74 20 69 4b 65 79 3b  signed int iKey;
1099d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1099e 4b 65 79 20 76 61 6c 75 65 20 28 70 61 67 65 20  Key value (page 
1099f 6e 75 6d 62 65 72 29 20 2a 2f 0a 20 20 50 67 48  number) */.  PgH
109a0 64 72 31 20 2a 70 4e 65 78 74 3b 20 20 20 20 20  dr1 *pNext;     
109a1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
109a2 65 78 74 20 69 6e 20 68 61 73 68 20 74 61 62 6c  ext in hash tabl
109a3 65 20 63 68 61 69 6e 20 2a 2f 0a 20 20 50 43 61  e chain */.  PCa
109a4 63 68 65 31 20 2a 70 43 61 63 68 65 3b 20 20 20  che1 *pCache;   
109a5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
109a6 61 63 68 65 20 74 68 61 74 20 63 75 72 72 65 6e  ache that curren
109a7 74 6c 79 20 6f 77 6e 73 20 74 68 69 73 20 70 61  tly owns this pa
109a8 67 65 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a  ge */.  PgHdr1 *
109a9 70 4c 72 75 4e 65 78 74 3b 20 20 20 20 20 20 20  pLruNext;       
109aa 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69         /* Next i
109ab 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 6e  n LRU list of un
109ac 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f 0a  pinned pages */.
109ad 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 50 72    PgHdr1 *pLruPr
109ae 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ev;             
109af 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 69 6e 20   /* Previous in 
109b0 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 6e 70 69  LRU list of unpi
109b1 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f 0a 7d 3b  nned pages */.};
109b2 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 73 6c 6f  ../*.** Free slo
109b3 74 73 20 69 6e 20 74 68 65 20 61 6c 6c 6f 63 61  ts in the alloca
109b4 74 6f 72 20 75 73 65 64 20 74 6f 20 64 69 76 69  tor used to divi
109b5 64 65 20 75 70 20 74 68 65 20 62 75 66 66 65 72  de up the buffer
109b6 20 70 72 6f 76 69 64 65 64 20 75 73 69 6e 67 0a   provided using.
109b7 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f  ** the SQLITE_CO
109b8 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 20 6d  NFIG_PAGECACHE m
109b9 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a 73 74 72  echanism..*/.str
109ba 75 63 74 20 50 67 46 72 65 65 73 6c 6f 74 20 7b  uct PgFreeslot {
109bb 0a 20 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70  .  PgFreeslot *p
109bc 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 66  Next;  /* Next f
109bd 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 7d 3b 0a 0a  ree slot */.};..
109be 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 64 61 74  /*.** Global dat
109bf 61 20 75 73 65 64 20 62 79 20 74 68 69 73 20 63  a used by this c
109c0 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ache..*/.static 
109c1 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63  SQLITE_WSD struc
109c2 74 20 50 43 61 63 68 65 47 6c 6f 62 61 6c 20 7b  t PCacheGlobal {
109c3 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
109c4 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20   *mutex;        
109c5 20 20 20 20 20 20 20 2f 2a 20 73 74 61 74 69 63         /* static
109c6 20 6d 75 74 65 78 20 4d 55 54 45 58 5f 53 54 41   mutex MUTEX_STA
109c7 54 49 43 5f 4c 52 55 20 2a 2f 0a 0a 20 20 69 6e  TIC_LRU */..  in
109c8 74 20 6e 4d 61 78 50 61 67 65 3b 20 20 20 20 20  t nMaxPage;     
109c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
109ca 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e 4d 61 78    /* Sum of nMax
109cb 50 61 67 65 20 66 6f 72 20 70 75 72 67 65 61 62  Page for purgeab
109cc 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20 69  le caches */.  i
109cd 6e 74 20 6e 4d 69 6e 50 61 67 65 3b 20 20 20 20  nt nMinPage;    
109ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
109cf 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e 4d 69     /* Sum of nMi
109d0 6e 50 61 67 65 20 66 6f 72 20 70 75 72 67 65 61  nPage for purgea
109d1 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20  ble caches */.  
109d2 69 6e 74 20 6e 43 75 72 72 65 6e 74 50 61 67 65  int nCurrentPage
109d3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
109d4 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
109d5 20 70 75 72 67 65 61 62 6c 65 20 70 61 67 65 73   purgeable pages
109d6 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20   allocated */.  
109d7 50 67 48 64 72 31 20 2a 70 4c 72 75 48 65 61 64  PgHdr1 *pLruHead
109d8 2c 20 2a 70 4c 72 75 54 61 69 6c 3b 20 20 20 20  , *pLruTail;    
109d9 20 20 20 20 2f 2a 20 4c 52 55 20 6c 69 73 74 20      /* LRU list 
109da 6f 66 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65  of unpinned page
109db 73 20 2a 2f 0a 0a 20 20 2f 2a 20 56 61 72 69 61  s */..  /* Varia
109dc 62 6c 65 73 20 72 65 6c 61 74 65 64 20 74 6f 20  bles related to 
109dd 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41  SQLITE_CONFIG_PA
109de 47 45 43 41 43 48 45 20 73 65 74 74 69 6e 67 73  GECACHE settings
109df 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 6c 6f  . */.  int szSlo
109e0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
109e1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
109e2 7a 65 20 6f 66 20 65 61 63 68 20 66 72 65 65 20  ze of each free 
109e3 73 6c 6f 74 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  slot */.  void *
109e4 70 53 74 61 72 74 2c 20 2a 70 45 6e 64 3b 20 20  pStart, *pEnd;  
109e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
109e6 20 42 6f 75 6e 64 73 20 6f 66 20 70 61 67 65 63   Bounds of pagec
109e7 61 63 68 65 20 6d 61 6c 6c 6f 63 20 72 61 6e 67  ache malloc rang
109e8 65 20 2a 2f 0a 20 20 50 67 46 72 65 65 73 6c 6f  e */.  PgFreeslo
109e9 74 20 2a 70 46 72 65 65 3b 20 20 20 20 20 20 20  t *pFree;       
109ea 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72             /* Fr
109eb 65 65 20 70 61 67 65 20 62 6c 6f 63 6b 73 20 2a  ee page blocks *
109ec 2f 0a 20 20 69 6e 74 20 69 73 49 6e 69 74 3b 20  /.  int isInit; 
109ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
109ee 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
109ef 69 66 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a  if initialized *
109f0 2f 0a 7d 20 70 63 61 63 68 65 31 5f 67 3b 0a 0a  /.} pcache1_g;..
109f1 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 6f 64 65 20 69  /*.** All code i
109f2 6e 20 74 68 69 73 20 66 69 6c 65 20 73 68 6f 75  n this file shou
109f3 6c 64 20 61 63 63 65 73 73 20 74 68 65 20 67 6c  ld access the gl
109f4 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65 20 61  obal structure a
109f5 62 6f 76 65 20 76 69 61 20 74 68 65 0a 2a 2a 20  bove via the.** 
109f6 61 6c 69 61 73 20 22 70 63 61 63 68 65 31 22 2e  alias "pcache1".
109f7 20 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 68   This ensures th
109f8 61 74 20 74 68 65 20 57 53 44 20 65 6d 75 6c 61  at the WSD emula
109f9 74 69 6f 6e 20 69 73 20 75 73 65 64 20 77 68 65  tion is used whe
109fa 6e 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 66  n.** compiling f
109fb 6f 72 20 73 79 73 74 65 6d 73 20 74 68 61 74 20  or systems that 
109fc 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 72  do not support r
109fd 65 61 6c 20 57 53 44 2e 0a 2a 2f 0a 23 64 65 66  eal WSD..*/.#def
109fe 69 6e 65 20 70 63 61 63 68 65 31 20 28 47 4c 4f  ine pcache1 (GLO
109ff 42 41 4c 28 73 74 72 75 63 74 20 50 43 61 63 68  BAL(struct PCach
10a00 65 47 6c 6f 62 61 6c 2c 20 70 63 61 63 68 65 31  eGlobal, pcache1
10a01 5f 67 29 29 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e  _g))../*.** When
10a02 20 61 20 50 67 48 64 72 31 20 73 74 72 75 63 74   a PgHdr1 struct
10a03 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  ure is allocated
10a04 2c 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64  , the associated
10a05 20 50 43 61 63 68 65 31 2e 73 7a 50 61 67 65 0a   PCache1.szPage.
10a06 2a 2a 20 62 79 74 65 73 20 6f 66 20 64 61 74 61  ** bytes of data
10a07 20 61 72 65 20 6c 6f 63 61 74 65 64 20 64 69 72   are located dir
10a08 65 63 74 6c 79 20 62 65 66 6f 72 65 20 69 74 20  ectly before it 
10a09 69 6e 20 6d 65 6d 6f 72 79 20 28 69 2e 65 2e 20  in memory (i.e. 
10a0a 74 68 65 20 74 6f 74 61 6c 0a 2a 2a 20 73 69 7a  the total.** siz
10a0b 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74  e of the allocat
10a0c 69 6f 6e 20 69 73 20 73 69 7a 65 6f 66 28 50 67  ion is sizeof(Pg
10a0d 48 64 72 31 29 2b 50 43 61 63 68 65 31 2e 73 7a  Hdr1)+PCache1.sz
10a0e 50 61 67 65 20 62 79 74 65 29 2e 20 54 68 65 0a  Page byte). The.
10a0f 2a 2a 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47  ** PGHDR1_TO_PAG
10a10 45 28 29 20 6d 61 63 72 6f 20 74 61 6b 65 73 20  E() macro takes 
10a11 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 50  a pointer to a P
10a12 67 48 64 72 31 20 73 74 72 75 63 74 75 72 65 20  gHdr1 structure 
10a13 61 73 0a 2a 2a 20 61 6e 20 61 72 67 75 6d 65 6e  as.** an argumen
10a14 74 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20  t and returns a 
10a15 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61  pointer to the a
10a16 73 73 6f 63 69 61 74 65 64 20 62 6c 6f 63 6b 20  ssociated block 
10a17 6f 66 20 73 7a 50 61 67 65 0a 2a 2a 20 62 79 74  of szPage.** byt
10a18 65 73 2e 20 54 68 65 20 50 41 47 45 5f 54 4f 5f  es. The PAGE_TO_
10a19 50 47 48 44 52 31 28 29 20 6d 61 63 72 6f 20 64  PGHDR1() macro d
10a1a 6f 65 73 20 74 68 65 20 6f 70 70 6f 73 69 74 65  oes the opposite
10a1b 3a 20 69 74 73 20 61 72 67 75 6d 65 6e 74 20 69  : its argument i
10a1c 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74  s.** a pointer t
10a1d 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 73 7a 50  o a block of szP
10a1e 61 67 65 20 62 79 74 65 73 20 6f 66 20 64 61 74  age bytes of dat
10a1f 61 20 61 6e 64 20 74 68 65 20 72 65 74 75 72 6e  a and the return
10a20 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61 20 70   value is.** a p
10a21 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 73  ointer to the as
10a22 73 6f 63 69 61 74 65 64 20 50 67 48 64 72 31 20  sociated PgHdr1 
10a23 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a  structure..**.**
10a24 20 20 20 61 73 73 65 72 74 28 20 50 47 48 44 52     assert( PGHDR
10a25 31 5f 54 4f 5f 50 41 47 45 28 50 41 47 45 5f 54  1_TO_PAGE(PAGE_T
10a26 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68 65 2c  O_PGHDR1(pCache,
10a27 20 58 29 29 3d 3d 58 20 29 3b 0a 2a 2f 0a 23 64   X))==X );.*/.#d
10a28 65 66 69 6e 65 20 50 47 48 44 52 31 5f 54 4f 5f  efine PGHDR1_TO_
10a29 50 41 47 45 28 70 29 20 20 20 20 28 76 6f 69 64  PAGE(p)    (void
10a2a 2a 29 28 28 28 63 68 61 72 2a 29 70 29 20 2d 20  *)(((char*)p) - 
10a2b 70 2d 3e 70 43 61 63 68 65 2d 3e 73 7a 50 61 67  p->pCache->szPag
10a2c 65 29 0a 23 64 65 66 69 6e 65 20 50 41 47 45 5f  e).#define PAGE_
10a2d 54 4f 5f 50 47 48 44 52 31 28 63 2c 20 70 29 20  TO_PGHDR1(c, p) 
10a2e 28 50 67 48 64 72 31 2a 29 28 28 28 63 68 61 72  (PgHdr1*)(((char
10a2f 2a 29 70 29 20 2b 20 63 2d 3e 73 7a 50 61 67 65  *)p) + c->szPage
10a30 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20  )../*.** Macros 
10a31 74 6f 20 65 6e 74 65 72 20 61 6e 64 20 6c 65 61  to enter and lea
10a32 76 65 20 74 68 65 20 67 6c 6f 62 61 6c 20 4c 52  ve the global LR
10a33 55 20 6d 75 74 65 78 2e 0a 2a 2f 0a 23 64 65 66  U mutex..*/.#def
10a34 69 6e 65 20 70 63 61 63 68 65 31 45 6e 74 65 72  ine pcache1Enter
10a35 4d 75 74 65 78 28 29 20 73 71 6c 69 74 65 33 5f  Mutex() sqlite3_
10a36 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 63 61 63  mutex_enter(pcac
10a37 68 65 31 2e 6d 75 74 65 78 29 0a 23 64 65 66 69  he1.mutex).#defi
10a38 6e 65 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d  ne pcache1LeaveM
10a39 75 74 65 78 28 29 20 73 71 6c 69 74 65 33 5f 6d  utex() sqlite3_m
10a3a 75 74 65 78 5f 6c 65 61 76 65 28 70 63 61 63 68  utex_leave(pcach
10a3b 65 31 2e 6d 75 74 65 78 29 0a 0a 2f 2a 2a 2a 2a  e1.mutex)../****
10a3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a3e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a3f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
10a41 2a 2a 2a 2a 2a 20 50 61 67 65 20 41 6c 6c 6f 63  ***** Page Alloc
10a42 61 74 69 6f 6e 2f 53 51 4c 49 54 45 5f 43 4f 4e  ation/SQLITE_CON
10a43 46 49 47 5f 50 43 41 43 48 45 20 52 65 6c 61 74  FIG_PCACHE Relat
10a44 65 64 20 46 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a  ed Functions ***
10a45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
10a46 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
10a47 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 75 72 69  n is called duri
10a48 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  ng initializatio
10a49 6e 20 69 66 20 61 20 73 74 61 74 69 63 20 62 75  n if a static bu
10a4a 66 66 65 72 20 69 73 20 0a 2a 2a 20 73 75 70 70  ffer is .** supp
10a4b 6c 69 65 64 20 74 6f 20 75 73 65 20 66 6f 72 20  lied to use for 
10a4c 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 62  the page-cache b
10a4d 79 20 70 61 73 73 69 6e 67 20 74 68 65 20 53 51  y passing the SQ
10a4e 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45  LITE_CONFIG_PAGE
10a4f 43 41 43 48 45 0a 2a 2a 20 76 65 72 62 20 74 6f  CACHE.** verb to
10a50 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28   sqlite3_config(
10a51 29 2e 20 50 61 72 61 6d 65 74 65 72 20 70 42 75  ). Parameter pBu
10a52 66 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61  f points to an a
10a53 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67 65 0a  llocation large.
10a54 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20 63 6f 6e  ** enough to con
10a55 74 61 69 6e 20 27 6e 27 20 62 75 66 66 65 72 73  tain 'n' buffers
10a56 20 6f 66 20 27 73 7a 27 20 62 79 74 65 73 20 65   of 'sz' bytes e
10a57 61 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ach..*/.SQLITE_P
10a58 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
10a59 74 65 33 50 43 61 63 68 65 42 75 66 66 65 72 53  te3PCacheBufferS
10a5a 65 74 75 70 28 76 6f 69 64 20 2a 70 42 75 66 2c  etup(void *pBuf,
10a5b 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e 29 7b   int sz, int n){
10a5c 0a 20 20 69 66 28 20 70 63 61 63 68 65 31 2e 69  .  if( pcache1.i
10a5d 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 50 67 46  sInit ){.    PgF
10a5e 72 65 65 73 6c 6f 74 20 2a 70 3b 0a 20 20 20 20  reeslot *p;.    
10a5f 73 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 28  sz = ROUNDDOWN8(
10a60 73 7a 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31  sz);.    pcache1
10a61 2e 73 7a 53 6c 6f 74 20 3d 20 73 7a 3b 0a 20 20  .szSlot = sz;.  
10a62 20 20 70 63 61 63 68 65 31 2e 70 53 74 61 72 74    pcache1.pStart
10a63 20 3d 20 70 42 75 66 3b 0a 20 20 20 20 70 63 61   = pBuf;.    pca
10a64 63 68 65 31 2e 70 46 72 65 65 20 3d 20 30 3b 0a  che1.pFree = 0;.
10a65 20 20 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 29      while( n-- )
10a66 7b 0a 20 20 20 20 20 20 70 20 3d 20 28 50 67 46  {.      p = (PgF
10a67 72 65 65 73 6c 6f 74 2a 29 70 42 75 66 3b 0a 20  reeslot*)pBuf;. 
10a68 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20       p->pNext = 
10a69 70 63 61 63 68 65 31 2e 70 46 72 65 65 3b 0a 20  pcache1.pFree;. 
10a6a 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 46 72       pcache1.pFr
10a6b 65 65 20 3d 20 70 3b 0a 20 20 20 20 20 20 70 42  ee = p;.      pB
10a6c 75 66 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 63  uf = (void*)&((c
10a6d 68 61 72 2a 29 70 42 75 66 29 5b 73 7a 5d 3b 0a  har*)pBuf)[sz];.
10a6e 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65      }.    pcache
10a6f 31 2e 70 45 6e 64 20 3d 20 70 42 75 66 3b 0a 20  1.pEnd = pBuf;. 
10a70 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c   }.}../*.** Mall
10a71 6f 63 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64  oc function used
10a72 20 77 69 74 68 69 6e 20 74 68 69 73 20 66 69 6c   within this fil
10a73 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 73 70  e to allocate sp
10a74 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62 75 66  ace from the buf
10a75 66 65 72 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 65  fer.** configure
10a76 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f  d using sqlite3_
10a77 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f  config(SQLITE_CO
10a78 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 29 20  NFIG_PAGECACHE) 
10a79 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 0a 2a  option. If no .*
10a7a 2a 20 73 75 63 68 20 62 75 66 66 65 72 20 65 78  * such buffer ex
10a7b 69 73 74 73 20 6f 72 20 74 68 65 72 65 20 69 73  ists or there is
10a7c 20 6e 6f 20 73 70 61 63 65 20 6c 65 66 74 20 69   no space left i
10a7d 6e 20 69 74 2c 20 74 68 69 73 20 66 75 6e 63 74  n it, this funct
10a7e 69 6f 6e 20 66 61 6c 6c 73 20 0a 2a 2a 20 62 61  ion falls .** ba
10a7f 63 6b 20 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c  ck to sqlite3Mal
10a80 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  loc()..*/.static
10a81 20 76 6f 69 64 20 2a 70 63 61 63 68 65 31 41 6c   void *pcache1Al
10a82 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a  loc(int nByte){.
10a83 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73    void *p;.  ass
10a84 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
10a85 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e  ex_held(pcache1.
10a86 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
10a87 6e 42 79 74 65 3c 3d 70 63 61 63 68 65 31 2e 73  nByte<=pcache1.s
10a88 7a 53 6c 6f 74 20 26 26 20 70 63 61 63 68 65 31  zSlot && pcache1
10a89 2e 70 46 72 65 65 20 29 7b 0a 20 20 20 20 61 73  .pFree ){.    as
10a8a 73 65 72 74 28 20 70 63 61 63 68 65 31 2e 69 73  sert( pcache1.is
10a8b 49 6e 69 74 20 29 3b 0a 20 20 20 20 70 20 3d 20  Init );.    p = 
10a8c 28 50 67 48 64 72 31 20 2a 29 70 63 61 63 68 65  (PgHdr1 *)pcache
10a8d 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 63 61  1.pFree;.    pca
10a8e 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70 63 61  che1.pFree = pca
10a8f 63 68 65 31 2e 70 46 72 65 65 2d 3e 70 4e 65 78  che1.pFree->pNex
10a90 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74  t;.    sqlite3St
10a91 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53  atusSet(SQLITE_S
10a92 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f  TATUS_PAGECACHE_
10a93 53 49 5a 45 2c 20 6e 42 79 74 65 29 3b 0a 20 20  SIZE, nByte);.  
10a94 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41    sqlite3StatusA
10a95 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  dd(SQLITE_STATUS
10a96 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 44 2c  _PAGECACHE_USED,
10a97 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 0a 20   1);.  }else{.. 
10a98 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61     /* Allocate a
10a99 20 6e 65 77 20 62 75 66 66 65 72 20 75 73 69 6e   new buffer usin
10a9a 67 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 2e  g sqlite3Malloc.
10a9b 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 73 6f   Before doing so
10a9c 2c 20 65 78 69 74 20 74 68 65 0a 20 20 20 20 2a  , exit the.    *
10a9d 2a 20 67 6c 6f 62 61 6c 20 70 63 61 63 68 65 20  * global pcache 
10a9e 6d 75 74 65 78 20 61 6e 64 20 75 6e 6c 6f 63 6b  mutex and unlock
10a9f 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65   the pager-cache
10aa0 20 6f 62 6a 65 63 74 20 70 43 61 63 68 65 2e 20   object pCache. 
10aa1 54 68 69 73 20 69 73 20 0a 20 20 20 20 2a 2a 20  This is .    ** 
10aa2 73 6f 20 74 68 61 74 20 69 66 20 74 68 65 20 61  so that if the a
10aa3 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61  ttempt to alloca
10aa4 74 65 20 61 20 6e 65 77 20 62 75 66 66 65 72 20  te a new buffer 
10aa5 63 61 75 73 65 73 20 74 68 65 20 74 68 65 20 0a  causes the the .
10aa6 20 20 20 20 2a 2a 20 63 6f 6e 66 69 67 75 72 65      ** configure
10aa7 64 20 73 6f 66 74 2d 68 65 61 70 2d 6c 69 6d 69  d soft-heap-limi
10aa8 74 20 74 6f 20 62 65 20 62 72 65 61 63 68 65 64  t to be breached
10aa9 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 70 6f 73  , it will be pos
10aaa 73 69 62 6c 65 20 74 6f 0a 20 20 20 20 2a 2a 20  sible to.    ** 
10aab 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 20 66  reclaim memory f
10aac 72 6f 6d 20 74 68 69 73 20 70 61 67 65 72 2d 63  rom this pager-c
10aad 61 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ache..    */.   
10aae 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74   pcache1LeaveMut
10aaf 65 78 28 29 3b 0a 20 20 20 20 70 20 3d 20 73 71  ex();.    p = sq
10ab0 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 79 74  lite3Malloc(nByt
10ab1 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 45  e);.    pcache1E
10ab2 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20  nterMutex();.   
10ab3 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20   if( p ){.      
10ab4 69 6e 74 20 73 7a 20 3d 20 73 71 6c 69 74 65 33  int sz = sqlite3
10ab5 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20  MallocSize(p);. 
10ab6 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74       sqlite3Stat
10ab7 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41  usAdd(SQLITE_STA
10ab8 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56  TUS_PAGECACHE_OV
10ab9 45 52 46 4c 4f 57 2c 20 73 7a 29 3b 0a 20 20 20  ERFLOW, sz);.   
10aba 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
10abb 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65  p;.}../*.** Free
10abc 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 75   an allocated bu
10abd 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66 72  ffer obtained fr
10abe 6f 6d 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 28  om pcache1Alloc(
10abf 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
10ac0 64 20 70 63 61 63 68 65 31 46 72 65 65 28 76 6f  d pcache1Free(vo
10ac1 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  id *p){.  assert
10ac2 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
10ac3 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74  held(pcache1.mut
10ac4 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d  ex) );.  if( p==
10ac5 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  0 ) return;.  if
10ac6 28 20 70 3e 3d 70 63 61 63 68 65 31 2e 70 53 74  ( p>=pcache1.pSt
10ac7 61 72 74 20 26 26 20 70 3c 70 63 61 63 68 65 31  art && p<pcache1
10ac8 2e 70 45 6e 64 20 29 7b 0a 20 20 20 20 50 67 46  .pEnd ){.    PgF
10ac9 72 65 65 73 6c 6f 74 20 2a 70 53 6c 6f 74 3b 0a  reeslot *pSlot;.
10aca 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75      sqlite3Statu
10acb 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54  sAdd(SQLITE_STAT
10acc 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45  US_PAGECACHE_USE
10acd 44 2c 20 2d 31 29 3b 0a 20 20 20 20 70 53 6c 6f  D, -1);.    pSlo
10ace 74 20 3d 20 28 50 67 46 72 65 65 73 6c 6f 74 2a  t = (PgFreeslot*
10acf 29 70 3b 0a 20 20 20 20 70 53 6c 6f 74 2d 3e 70  )p;.    pSlot->p
10ad0 4e 65 78 74 20 3d 20 70 63 61 63 68 65 31 2e 70  Next = pcache1.p
10ad1 46 72 65 65 3b 0a 20 20 20 20 70 63 61 63 68 65  Free;.    pcache
10ad2 31 2e 70 46 72 65 65 20 3d 20 70 53 6c 6f 74 3b  1.pFree = pSlot;
10ad3 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
10ad4 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69 74 65  t iSize = sqlite
10ad5 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a  3MallocSize(p);.
10ad6 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75      sqlite3Statu
10ad7 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54  sAdd(SQLITE_STAT
10ad8 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 45  US_PAGECACHE_OVE
10ad9 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29 3b 0a  RFLOW, -iSize);.
10ada 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
10adb 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  (p);.  }.}../*.*
10adc 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  * Allocate a new
10add 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 6e 69   page object ini
10ade 74 69 61 6c 6c 79 20 61 73 73 6f 63 69 61 74 65  tially associate
10adf 64 20 77 69 74 68 20 63 61 63 68 65 20 70 43 61  d with cache pCa
10ae0 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50  che..*/.static P
10ae1 67 48 64 72 31 20 2a 70 63 61 63 68 65 31 41 6c  gHdr1 *pcache1Al
10ae2 6c 6f 63 50 61 67 65 28 50 43 61 63 68 65 31 20  locPage(PCache1 
10ae3 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e 74 20  *pCache){.  int 
10ae4 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 50  nByte = sizeof(P
10ae5 67 48 64 72 31 29 20 2b 20 70 43 61 63 68 65 2d  gHdr1) + pCache-
10ae6 3e 73 7a 50 61 67 65 3b 0a 20 20 76 6f 69 64 20  >szPage;.  void 
10ae7 2a 70 50 67 20 3d 20 70 63 61 63 68 65 31 41 6c  *pPg = pcache1Al
10ae8 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20 50 67  loc(nByte);.  Pg
10ae9 48 64 72 31 20 2a 70 3b 0a 20 20 69 66 28 20 70  Hdr1 *p;.  if( p
10aea 50 67 20 29 7b 0a 20 20 20 20 70 20 3d 20 50 41  Pg ){.    p = PA
10aeb 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70 43 61  GE_TO_PGHDR1(pCa
10aec 63 68 65 2c 20 70 50 67 29 3b 0a 20 20 20 20 69  che, pPg);.    i
10aed 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67  f( pCache->bPurg
10aee 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70  eable ){.      p
10aef 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50  cache1.nCurrentP
10af0 61 67 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  age++;.    }.  }
10af1 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 30 3b  else{.    p = 0;
10af2 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
10af3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61  .}../*.** Free a
10af4 20 70 61 67 65 20 6f 62 6a 65 63 74 20 61 6c 6c   page object all
10af5 6f 63 61 74 65 64 20 62 79 20 70 63 61 63 68 65  ocated by pcache
10af6 31 41 6c 6c 6f 63 50 61 67 65 28 29 2e 0a 2a 2a  1AllocPage()..**
10af7 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20  .** The pointer 
10af8 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65  is allowed to be
10af9 20 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69 73 20   NULL, which is 
10afa 70 72 75 64 65 6e 74 2e 20 20 42 75 74 20 69 74  prudent.  But it
10afb 20 74 75 72 6e 73 20 6f 75 74 0a 2a 2a 20 74 68   turns out.** th
10afc 61 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 69  at the current i
10afd 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 68 61  mplementation ha
10afe 70 70 65 6e 73 20 74 6f 20 6e 65 76 65 72 20 63  ppens to never c
10aff 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  all this routine
10b00 0a 2a 2a 20 77 69 74 68 20 61 20 4e 55 4c 4c 20  .** with a NULL 
10b01 70 6f 69 6e 74 65 72 2c 20 73 6f 20 77 65 20 6d  pointer, so we m
10b02 61 72 6b 20 74 68 65 20 4e 55 4c 4c 20 74 65 73  ark the NULL tes
10b03 74 20 77 69 74 68 20 41 4c 57 41 59 53 28 29 2e  t with ALWAYS().
10b04 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
10b05 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28  pcache1FreePage(
10b06 50 67 48 64 72 31 20 2a 70 29 7b 0a 20 20 69 66  PgHdr1 *p){.  if
10b07 28 20 41 4c 57 41 59 53 28 70 29 20 29 7b 0a 20  ( ALWAYS(p) ){. 
10b08 20 20 20 69 66 28 20 70 2d 3e 70 43 61 63 68 65     if( p->pCache
10b09 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a  ->bPurgeable ){.
10b0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 6e 43        pcache1.nC
10b0b 75 72 72 65 6e 74 50 61 67 65 2d 2d 3b 0a 20 20  urrentPage--;.  
10b0c 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65 31 46    }.    pcache1F
10b0d 72 65 65 28 50 47 48 44 52 31 5f 54 4f 5f 50 41  ree(PGHDR1_TO_PA
10b0e 47 45 28 70 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  GE(p));.  }.}../
10b0f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 75 6e 63  *.** Malloc func
10b10 74 69 6f 6e 20 75 73 65 64 20 62 79 20 53 51 4c  tion used by SQL
10b11 69 74 65 20 74 6f 20 6f 62 74 61 69 6e 20 73 70  ite to obtain sp
10b12 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62 75 66  ace from the buf
10b13 66 65 72 20 63 6f 6e 66 69 67 75 72 65 64 0a 2a  fer configured.*
10b14 2a 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f  * using sqlite3_
10b15 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f  config(SQLITE_CO
10b16 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 29 20  NFIG_PAGECACHE) 
10b17 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 73 75  option. If no su
10b18 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 65 78 69  ch buffer.** exi
10b19 73 74 73 2c 20 74 68 69 73 20 66 75 6e 63 74 69  sts, this functi
10b1a 6f 6e 20 66 61 6c 6c 73 20 62 61 63 6b 20 74 6f  on falls back to
10b1b 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29   sqlite3Malloc()
10b1c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
10b1d 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
10b1e 33 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e 74 20  3PageMalloc(int 
10b1f 73 7a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a  sz){.  void *p;.
10b20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75    pcache1EnterMu
10b21 74 65 78 28 29 3b 0a 20 20 70 20 3d 20 70 63 61  tex();.  p = pca
10b22 63 68 65 31 41 6c 6c 6f 63 28 73 7a 29 3b 0a 20  che1Alloc(sz);. 
10b23 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74   pcache1LeaveMut
10b24 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  ex();.  return p
10b25 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20  ;.}../*.** Free 
10b26 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66  an allocated buf
10b27 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  fer obtained fro
10b28 6d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c  m sqlite3PageMal
10b29 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  loc()..*/.SQLITE
10b2a 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
10b2b 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 76 6f  lite3PageFree(vo
10b2c 69 64 20 2a 70 29 7b 0a 20 20 70 63 61 63 68 65  id *p){.  pcache
10b2d 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  1EnterMutex();. 
10b2e 20 70 63 61 63 68 65 31 46 72 65 65 28 70 29 3b   pcache1Free(p);
10b2f 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d  .  pcache1LeaveM
10b30 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  utex();.}../****
10b31 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10b32 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10b33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10b34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10b35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
10b36 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20 49 6d  ***** General Im
10b37 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 46 75 6e  plementation Fun
10b38 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ctions *********
10b39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10b3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
10b3b 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
10b3c 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 73  n is used to res
10b3d 69 7a 65 20 74 68 65 20 68 61 73 68 20 74 61 62  ize the hash tab
10b3e 6c 65 20 75 73 65 64 20 62 79 20 74 68 65 20 63  le used by the c
10b3f 61 63 68 65 20 70 61 73 73 65 64 0a 2a 2a 20 61  ache passed.** a
10b40 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
10b41 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ment..**.** The 
10b42 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d 75 73  global mutex mus
10b43 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20 74  t be held when t
10b44 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
10b45 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  called..*/.stati
10b46 63 20 69 6e 74 20 70 63 61 63 68 65 31 52 65 73  c int pcache1Res
10b47 69 7a 65 48 61 73 68 28 50 43 61 63 68 65 31 20  izeHash(PCache1 
10b48 2a 70 29 7b 0a 20 20 50 67 48 64 72 31 20 2a 2a  *p){.  PgHdr1 **
10b49 61 70 4e 65 77 3b 0a 20 20 75 6e 73 69 67 6e 65  apNew;.  unsigne
10b4a 64 20 69 6e 74 20 6e 4e 65 77 3b 0a 20 20 75 6e  d int nNew;.  un
10b4b 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 0a 20  signed int i;.. 
10b4c 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
10b4d 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63  _mutex_held(pcac
10b4e 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  he1.mutex) );.. 
10b4f 20 6e 4e 65 77 20 3d 20 70 2d 3e 6e 48 61 73 68   nNew = p->nHash
10b50 2a 32 3b 0a 20 20 69 66 28 20 6e 4e 65 77 3c 32  *2;.  if( nNew<2
10b51 35 36 20 29 7b 0a 20 20 20 20 6e 4e 65 77 20 3d  56 ){.    nNew =
10b52 20 32 35 36 3b 0a 20 20 7d 0a 0a 20 20 70 63 61   256;.  }..  pca
10b53 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29  che1LeaveMutex()
10b54 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 48 61 73 68  ;.  if( p->nHash
10b55 20 29 7b 20 73 71 6c 69 74 65 33 42 65 67 69 6e   ){ sqlite3Begin
10b56 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 20  BenignMalloc(); 
10b57 7d 0a 20 20 61 70 4e 65 77 20 3d 20 28 50 67 48  }.  apNew = (PgH
10b58 64 72 31 20 2a 2a 29 73 71 6c 69 74 65 33 5f 6d  dr1 **)sqlite3_m
10b59 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 50 67 48  alloc(sizeof(PgH
10b5a 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b 0a 20 20  dr1 *)*nNew);.  
10b5b 69 66 28 20 70 2d 3e 6e 48 61 73 68 20 29 7b 20  if( p->nHash ){ 
10b5c 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e  sqlite3EndBenign
10b5d 4d 61 6c 6c 6f 63 28 29 3b 20 7d 0a 20 20 70 63  Malloc(); }.  pc
10b5e 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28  ache1EnterMutex(
10b5f 29 3b 0a 20 20 69 66 28 20 61 70 4e 65 77 20 29  );.  if( apNew )
10b60 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 61 70 4e  {.    memset(apN
10b61 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50 67  ew, 0, sizeof(Pg
10b62 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b 0a 20  Hdr1 *)*nNew);. 
10b63 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d     for(i=0; i<p-
10b64 3e 6e 48 61 73 68 3b 20 69 2b 2b 29 7b 0a 20 20  >nHash; i++){.  
10b65 20 20 20 20 50 67 48 64 72 31 20 2a 70 50 61 67      PgHdr1 *pPag
10b66 65 3b 0a 20 20 20 20 20 20 50 67 48 64 72 31 20  e;.      PgHdr1 
10b67 2a 70 4e 65 78 74 20 3d 20 70 2d 3e 61 70 48 61  *pNext = p->apHa
10b68 73 68 5b 69 5d 3b 0a 20 20 20 20 20 20 77 68 69  sh[i];.      whi
10b69 6c 65 28 20 28 70 50 61 67 65 20 3d 20 70 4e 65  le( (pPage = pNe
10b6a 78 74 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  xt)!=0 ){.      
10b6b 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68    unsigned int h
10b6c 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 25   = pPage->iKey %
10b6d 20 6e 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 70   nNew;.        p
10b6e 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 4e  Next = pPage->pN
10b6f 65 78 74 3b 0a 20 20 20 20 20 20 20 20 70 50 61  ext;.        pPa
10b70 67 65 2d 3e 70 4e 65 78 74 20 3d 20 61 70 4e 65  ge->pNext = apNe
10b71 77 5b 68 5d 3b 0a 20 20 20 20 20 20 20 20 61 70  w[h];.        ap
10b72 4e 65 77 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a  New[h] = pPage;.
10b73 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
10b74 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
10b75 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20 20 20 70  ->apHash);.    p
10b76 2d 3e 61 70 48 61 73 68 20 3d 20 61 70 4e 65 77  ->apHash = apNew
10b77 3b 0a 20 20 20 20 70 2d 3e 6e 48 61 73 68 20 3d  ;.    p->nHash =
10b78 20 6e 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 72 65   nNew;.  }..  re
10b79 74 75 72 6e 20 28 70 2d 3e 61 70 48 61 73 68 20  turn (p->apHash 
10b7a 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51  ? SQLITE_OK : SQ
10b7b 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a  LITE_NOMEM);.}..
10b7c 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
10b7d 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e 74 65  ion is used inte
10b7e 72 6e 61 6c 6c 79 20 74 6f 20 72 65 6d 6f 76 65  rnally to remove
10b7f 20 74 68 65 20 70 61 67 65 20 70 50 61 67 65 20   the page pPage 
10b80 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 67 6c 6f  from the .** glo
10b81 62 61 6c 20 4c 52 55 20 6c 69 73 74 2c 20 69 66  bal LRU list, if
10b82 20 69 73 20 70 61 72 74 20 6f 66 20 69 74 2e 20   is part of it. 
10b83 49 66 20 70 50 61 67 65 20 69 73 20 6e 6f 74 20  If pPage is not 
10b84 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c 6f 62  part of the glob
10b85 61 6c 0a 2a 2a 20 4c 52 55 20 6c 69 73 74 2c 20  al.** LRU list, 
10b86 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  then this functi
10b87 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  on is a no-op..*
10b88 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20  *.** The global 
10b89 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65  mutex must be he
10b8a 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e  ld when this fun
10b8b 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e  ction is called.
10b8c 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
10b8d 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 50  pcache1PinPage(P
10b8e 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b 0a 20  gHdr1 *pPage){. 
10b8f 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
10b90 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63  _mutex_held(pcac
10b91 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20  he1.mutex) );.  
10b92 69 66 28 20 70 50 61 67 65 20 26 26 20 28 70 50  if( pPage && (pP
10b93 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 7c 7c  age->pLruNext ||
10b94 20 70 50 61 67 65 3d 3d 70 63 61 63 68 65 31 2e   pPage==pcache1.
10b95 70 4c 72 75 54 61 69 6c 29 20 29 7b 0a 20 20 20  pLruTail) ){.   
10b96 20 69 66 28 20 70 50 61 67 65 2d 3e 70 4c 72 75   if( pPage->pLru
10b97 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20 70 50  Prev ){.      pP
10b98 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 2d 3e 70  age->pLruPrev->p
10b99 4c 72 75 4e 65 78 74 20 3d 20 70 50 61 67 65 2d  LruNext = pPage-
10b9a 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 20 7d  >pLruNext;.    }
10b9b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e  .    if( pPage->
10b9c 70 4c 72 75 4e 65 78 74 20 29 7b 0a 20 20 20 20  pLruNext ){.    
10b9d 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78    pPage->pLruNex
10b9e 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 70 50  t->pLruPrev = pP
10b9f 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b 0a 20  age->pLruPrev;. 
10ba0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 63 61     }.    if( pca
10ba1 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3d 3d 70  che1.pLruHead==p
10ba2 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 70 63  Page ){.      pc
10ba3 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20 3d  ache1.pLruHead =
10ba4 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74   pPage->pLruNext
10ba5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
10ba6 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c  pcache1.pLruTail
10ba7 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20  ==pPage ){.     
10ba8 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69   pcache1.pLruTai
10ba9 6c 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 50  l = pPage->pLruP
10baa 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  rev;.    }.    p
10bab 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 3d  Page->pLruNext =
10bac 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70   0;.    pPage->p
10bad 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20 20  LruPrev = 0;.   
10bae 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e   pPage->pCache->
10baf 6e 52 65 63 79 63 6c 61 62 6c 65 2d 2d 3b 0a 20  nRecyclable--;. 
10bb0 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d   }.}.../*.** Rem
10bb1 6f 76 65 20 74 68 65 20 70 61 67 65 20 73 75 70  ove the page sup
10bb2 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 75  plied as an argu
10bb3 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 68 61  ment from the ha
10bb4 73 68 20 74 61 62 6c 65 20 0a 2a 2a 20 28 50 43  sh table .** (PC
10bb5 61 63 68 65 31 2e 61 70 48 61 73 68 20 73 74 72  ache1.apHash str
10bb6 75 63 74 75 72 65 29 20 74 68 61 74 20 69 74 20  ucture) that it 
10bb7 69 73 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f  is currently sto
10bb8 72 65 64 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  red in..**.** Th
10bb9 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d  e global mutex m
10bba 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e  ust be held when
10bbb 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
10bbc 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61  s called..*/.sta
10bbd 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31  tic void pcache1
10bbe 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 50  RemoveFromHash(P
10bbf 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b 0a 20  gHdr1 *pPage){. 
10bc0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 3b   unsigned int h;
10bc1 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63  .  PCache1 *pCac
10bc2 68 65 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 63  he = pPage->pCac
10bc3 68 65 3b 0a 20 20 50 67 48 64 72 31 20 2a 2a 70  he;.  PgHdr1 **p
10bc4 70 3b 0a 0a 20 20 68 20 3d 20 70 50 61 67 65 2d  p;..  h = pPage-
10bc5 3e 69 4b 65 79 20 25 20 70 43 61 63 68 65 2d 3e  >iKey % pCache->
10bc6 6e 48 61 73 68 3b 0a 20 20 66 6f 72 28 70 70 3d  nHash;.  for(pp=
10bc7 26 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b  &pCache->apHash[
10bc8 68 5d 3b 20 28 2a 70 70 29 21 3d 70 50 61 67 65  h]; (*pp)!=pPage
10bc9 3b 20 70 70 3d 26 28 2a 70 70 29 2d 3e 70 4e 65  ; pp=&(*pp)->pNe
10bca 78 74 29 3b 0a 20 20 2a 70 70 20 3d 20 28 2a 70  xt);.  *pp = (*p
10bcb 70 29 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 70 43  p)->pNext;..  pC
10bcc 61 63 68 65 2d 3e 6e 50 61 67 65 2d 2d 3b 0a 7d  ache->nPage--;.}
10bcd 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65  ../*.** If there
10bce 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 6d   are currently m
10bcf 6f 72 65 20 74 68 61 6e 20 70 63 61 63 68 65 2e  ore than pcache.
10bd0 6e 4d 61 78 50 61 67 65 20 70 61 67 65 73 20 61  nMaxPage pages a
10bd1 6c 6c 6f 63 61 74 65 64 2c 20 74 72 79 0a 2a 2a  llocated, try.**
10bd2 20 74 6f 20 72 65 63 79 63 6c 65 20 70 61 67 65   to recycle page
10bd3 73 20 74 6f 20 72 65 64 75 63 65 20 74 68 65 20  s to reduce the 
10bd4 6e 75 6d 62 65 72 20 61 6c 6c 6f 63 61 74 65 64  number allocated
10bd5 20 74 6f 20 70 63 61 63 68 65 2e 6e 4d 61 78 50   to pcache.nMaxP
10bd6 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  age..*/.static v
10bd7 6f 69 64 20 70 63 61 63 68 65 31 45 6e 66 6f 72  oid pcache1Enfor
10bd8 63 65 4d 61 78 50 61 67 65 28 76 6f 69 64 29 7b  ceMaxPage(void){
10bd9 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
10bda 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63  e3_mutex_held(pc
10bdb 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a  ache1.mutex) );.
10bdc 20 20 77 68 69 6c 65 28 20 70 63 61 63 68 65 31    while( pcache1
10bdd 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e 70 63  .nCurrentPage>pc
10bde 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 20 26  ache1.nMaxPage &
10bdf 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61  & pcache1.pLruTa
10be0 69 6c 20 29 7b 0a 20 20 20 20 50 67 48 64 72 31  il ){.    PgHdr1
10be1 20 2a 70 20 3d 20 70 63 61 63 68 65 31 2e 70 4c   *p = pcache1.pL
10be2 72 75 54 61 69 6c 3b 0a 20 20 20 20 70 63 61 63  ruTail;.    pcac
10be3 68 65 31 50 69 6e 50 61 67 65 28 70 29 3b 0a 20  he1PinPage(p);. 
10be4 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f 76 65     pcache1Remove
10be5 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20 20 20  FromHash(p);.   
10be6 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65   pcache1FreePage
10be7 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  (p);.  }.}../*.*
10be8 2a 20 44 69 73 63 61 72 64 20 61 6c 6c 20 70 61  * Discard all pa
10be9 67 65 73 20 66 72 6f 6d 20 63 61 63 68 65 20 70  ges from cache p
10bea 43 61 63 68 65 20 77 69 74 68 20 61 20 70 61 67  Cache with a pag
10beb 65 20 6e 75 6d 62 65 72 20 28 6b 65 79 20 76 61  e number (key va
10bec 6c 75 65 29 20 0a 2a 2a 20 67 72 65 61 74 65 72  lue) .** greater
10bed 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
10bee 6f 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20 70 69  o iLimit. Any pi
10bef 6e 6e 65 64 20 70 61 67 65 73 20 74 68 61 74 20  nned pages that 
10bf0 6d 65 65 74 20 74 68 69 73 20 0a 2a 2a 20 63 72  meet this .** cr
10bf1 69 74 65 72 69 61 20 61 72 65 20 75 6e 70 69 6e  iteria are unpin
10bf2 6e 65 64 20 62 65 66 6f 72 65 20 74 68 65 79 20  ned before they 
10bf3 61 72 65 20 64 69 73 63 61 72 64 65 64 2e 0a 2a  are discarded..*
10bf4 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20  *.** The global 
10bf5 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65  mutex must be he
10bf6 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e  ld when this fun
10bf7 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e  ction is called.
10bf8 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
10bf9 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 65 55  pcache1TruncateU
10bfa 6e 73 61 66 65 28 0a 20 20 50 43 61 63 68 65 31  nsafe(.  PCache1
10bfb 20 2a 70 43 61 63 68 65 2c 20 0a 20 20 75 6e 73   *pCache, .  uns
10bfc 69 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d 69 74  igned int iLimit
10bfd 20 0a 29 7b 0a 20 20 54 45 53 54 4f 4e 4c 59 28   .){.  TESTONLY(
10bfe 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 50   unsigned int nP
10bff 61 67 65 20 3d 20 30 3b 20 29 20 20 20 20 20 20  age = 0; )      
10c00 2f 2a 20 55 73 65 64 20 74 6f 20 61 73 73 65 72  /* Used to asser
10c01 74 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 20  t pCache->nPage 
10c02 69 73 20 63 6f 72 72 65 63 74 20 2a 2f 0a 20 20  is correct */.  
10c03 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 3b 0a  unsigned int h;.
10c04 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
10c05 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61  3_mutex_held(pca
10c06 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20  che1.mutex) );. 
10c07 20 66 6f 72 28 68 3d 30 3b 20 68 3c 70 43 61 63   for(h=0; h<pCac
10c08 68 65 2d 3e 6e 48 61 73 68 3b 20 68 2b 2b 29 7b  he->nHash; h++){
10c09 0a 20 20 20 20 50 67 48 64 72 31 20 2a 2a 70 70  .    PgHdr1 **pp
10c0a 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70 48 61   = &pCache->apHa
10c0b 73 68 5b 68 5d 3b 20 0a 20 20 20 20 50 67 48 64  sh[h]; .    PgHd
10c0c 72 31 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 77  r1 *pPage;.    w
10c0d 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d 20 2a  hile( (pPage = *
10c0e 70 70 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  pp)!=0 ){.      
10c0f 69 66 28 20 70 50 61 67 65 2d 3e 69 4b 65 79 3e  if( pPage->iKey>
10c10 3d 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20  =iLimit ){.     
10c11 20 20 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65     pCache->nPage
10c12 2d 2d 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 20  --;.        *pp 
10c13 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a  = pPage->pNext;.
10c14 20 20 20 20 20 20 20 20 70 63 61 63 68 65 31 50          pcache1P
10c15 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  inPage(pPage);. 
10c16 20 20 20 20 20 20 20 70 63 61 63 68 65 31 46 72         pcache1Fr
10c17 65 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  eePage(pPage);. 
10c18 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
10c19 20 20 20 20 70 70 20 3d 20 26 70 50 61 67 65 2d      pp = &pPage-
10c1a 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20  >pNext;.        
10c1b 54 45 53 54 4f 4e 4c 59 28 20 6e 50 61 67 65 2b  TESTONLY( nPage+
10c1c 2b 3b 20 29 0a 20 20 20 20 20 20 7d 0a 20 20 20  +; ).      }.   
10c1d 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28   }.  }.  assert(
10c1e 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 3d 3d   pCache->nPage==
10c1f 6e 50 61 67 65 20 29 3b 0a 7d 0a 0a 2f 2a 2a 2a  nPage );.}../***
10c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c22 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c24 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
10c25 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 70  ****** sqlite3_p
10c26 63 61 63 68 65 20 4d 65 74 68 6f 64 73 20 2a 2a  cache Methods **
10c27 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c28 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c29 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f  ************/../
10c2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
10c2b 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
10c2c 65 33 5f 70 63 61 63 68 65 2e 78 49 6e 69 74 20  e3_pcache.xInit 
10c2d 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69  method..*/.stati
10c2e 63 20 69 6e 74 20 70 63 61 63 68 65 31 49 6e 69  c int pcache1Ini
10c2f 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29  t(void *NotUsed)
10c30 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
10c31 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
10c32 20 61 73 73 65 72 74 28 20 70 63 61 63 68 65 31   assert( pcache1
10c33 2e 69 73 49 6e 69 74 3d 3d 30 20 29 3b 0a 20 20  .isInit==0 );.  
10c34 6d 65 6d 73 65 74 28 26 70 63 61 63 68 65 31 2c  memset(&pcache1,
10c35 20 30 2c 20 73 69 7a 65 6f 66 28 70 63 61 63 68   0, sizeof(pcach
10c36 65 31 29 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  e1));.  if( sqli
10c37 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
10c38 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20  bCoreMutex ){.  
10c39 20 20 70 63 61 63 68 65 31 2e 6d 75 74 65 78 20    pcache1.mutex 
10c3a 3d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  = sqlite3_mutex_
10c3b 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  alloc(SQLITE_MUT
10c3c 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 29 3b 0a  EX_STATIC_LRU);.
10c3d 20 20 7d 0a 20 20 70 63 61 63 68 65 31 2e 69 73    }.  pcache1.is
10c3e 49 6e 69 74 20 3d 20 31 3b 0a 20 20 72 65 74 75  Init = 1;.  retu
10c3f 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
10c40 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
10c41 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c  ation of the sql
10c42 69 74 65 33 5f 70 63 61 63 68 65 2e 78 53 68 75  ite3_pcache.xShu
10c43 74 64 6f 77 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2a  tdown method..**
10c44 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 73   Note that the s
10c45 74 61 74 69 63 20 6d 75 74 65 78 20 61 6c 6c 6f  tatic mutex allo
10c46 63 61 74 65 64 20 69 6e 20 78 49 6e 69 74 20 64  cated in xInit d
10c47 6f 65 73 20 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64  oes .** not need
10c48 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2f   to be freed..*/
10c49 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61  .static void pca
10c4a 63 68 65 31 53 68 75 74 64 6f 77 6e 28 76 6f 69  che1Shutdown(voi
10c4b 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55  d *NotUsed){.  U
10c4c 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
10c4d 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61 73 73 65  NotUsed);.  asse
10c4e 72 74 28 20 70 63 61 63 68 65 31 2e 69 73 49 6e  rt( pcache1.isIn
10c4f 69 74 21 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 65  it!=0 );.  memse
10c50 74 28 26 70 63 61 63 68 65 31 2c 20 30 2c 20 73  t(&pcache1, 0, s
10c51 69 7a 65 6f 66 28 70 63 61 63 68 65 31 29 29 3b  izeof(pcache1));
10c52 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
10c53 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
10c54 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78  sqlite3_pcache.x
10c55 43 72 65 61 74 65 20 6d 65 74 68 6f 64 2e 0a 2a  Create method..*
10c56 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20  *.** Allocate a 
10c57 6e 65 77 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74  new cache..*/.st
10c58 61 74 69 63 20 73 71 6c 69 74 65 33 5f 70 63 61  atic sqlite3_pca
10c59 63 68 65 20 2a 70 63 61 63 68 65 31 43 72 65 61  che *pcache1Crea
10c5a 74 65 28 69 6e 74 20 73 7a 50 61 67 65 2c 20 69  te(int szPage, i
10c5b 6e 74 20 62 50 75 72 67 65 61 62 6c 65 29 7b 0a  nt bPurgeable){.
10c5c 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68    PCache1 *pCach
10c5d 65 3b 0a 0a 20 20 70 43 61 63 68 65 20 3d 20 28  e;..  pCache = (
10c5e 50 43 61 63 68 65 31 20 2a 29 73 71 6c 69 74 65  PCache1 *)sqlite
10c5f 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
10c60 50 43 61 63 68 65 31 29 29 3b 0a 20 20 69 66 28  PCache1));.  if(
10c61 20 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 6d   pCache ){.    m
10c62 65 6d 73 65 74 28 70 43 61 63 68 65 2c 20 30 2c  emset(pCache, 0,
10c63 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65 31 29   sizeof(PCache1)
10c64 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 73  );.    pCache->s
10c65 7a 50 61 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a  zPage = szPage;.
10c66 20 20 20 20 70 43 61 63 68 65 2d 3e 62 50 75 72      pCache->bPur
10c67 67 65 61 62 6c 65 20 3d 20 28 62 50 75 72 67 65  geable = (bPurge
10c68 61 62 6c 65 20 3f 20 31 20 3a 20 30 29 3b 0a 20  able ? 1 : 0);. 
10c69 20 20 20 69 66 28 20 62 50 75 72 67 65 61 62 6c     if( bPurgeabl
10c6a 65 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68  e ){.      pCach
10c6b 65 2d 3e 6e 4d 69 6e 20 3d 20 31 30 3b 0a 20 20  e->nMin = 10;.  
10c6c 20 20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72      pcache1Enter
10c6d 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 70  Mutex();.      p
10c6e 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65 20  cache1.nMinPage 
10c6f 2b 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b  += pCache->nMin;
10c70 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 4c 65  .      pcache1Le
10c71 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20  aveMutex();.    
10c72 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  }.  }.  return (
10c73 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a  sqlite3_pcache *
10c74 29 70 43 61 63 68 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  )pCache;.}../*.*
10c75 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
10c76 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
10c77 70 63 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a  pcache.xCachesiz
10c78 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a  e method. .**.**
10c79 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 63   Configure the c
10c7a 61 63 68 65 5f 73 69 7a 65 20 6c 69 6d 69 74 20  ache_size limit 
10c7b 66 6f 72 20 61 20 63 61 63 68 65 2e 0a 2a 2f 0a  for a cache..*/.
10c7c 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63  static void pcac
10c7d 68 65 31 43 61 63 68 65 73 69 7a 65 28 73 71 6c  he1Cachesize(sql
10c7e 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20  ite3_pcache *p, 
10c7f 69 6e 74 20 6e 4d 61 78 29 7b 0a 20 20 50 43 61  int nMax){.  PCa
10c80 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28  che1 *pCache = (
10c81 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 69  PCache1 *)p;.  i
10c82 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67  f( pCache->bPurg
10c83 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 70 63 61  eable ){.    pca
10c84 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29  che1EnterMutex()
10c85 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e 6e 4d  ;.    pcache1.nM
10c86 61 78 50 61 67 65 20 2b 3d 20 28 6e 4d 61 78 20  axPage += (nMax 
10c87 2d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29 3b  - pCache->nMax);
10c88 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 4d 61  .    pCache->nMa
10c89 78 20 3d 20 6e 4d 61 78 3b 0a 20 20 20 20 70 63  x = nMax;.    pc
10c8a 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 78 50  ache1EnforceMaxP
10c8b 61 67 65 28 29 3b 0a 20 20 20 20 70 63 61 63 68  age();.    pcach
10c8c 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a  e1LeaveMutex();.
10c8d 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70    }.}../*.** Imp
10c8e 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
10c8f 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  he sqlite3_pcach
10c90 65 2e 78 50 61 67 65 63 6f 75 6e 74 20 6d 65 74  e.xPagecount met
10c91 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20  hod. .*/.static 
10c92 69 6e 74 20 70 63 61 63 68 65 31 50 61 67 65 63  int pcache1Pagec
10c93 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 70 63 61  ount(sqlite3_pca
10c94 63 68 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e  che *p){.  int n
10c95 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72  ;.  pcache1Enter
10c96 4d 75 74 65 78 28 29 3b 0a 20 20 6e 20 3d 20 28  Mutex();.  n = (
10c97 28 50 43 61 63 68 65 31 20 2a 29 70 29 2d 3e 6e  (PCache1 *)p)->n
10c98 50 61 67 65 3b 0a 20 20 70 63 61 63 68 65 31 4c  Page;.  pcache1L
10c99 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72  eaveMutex();.  r
10c9a 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn n;.}../*.*
10c9b 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
10c9c 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
10c9d 70 63 61 63 68 65 2e 78 46 65 74 63 68 20 6d 65  pcache.xFetch me
10c9e 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 46 65 74  thod. .**.** Fet
10c9f 63 68 20 61 20 70 61 67 65 20 62 79 20 6b 65 79  ch a page by key
10ca0 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 57 68   value..**.** Wh
10ca1 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 6e  ether or not a n
10ca2 65 77 20 70 61 67 65 20 6d 61 79 20 62 65 20 61  ew page may be a
10ca3 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69 73  llocated by this
10ca4 20 66 75 6e 63 74 69 6f 6e 20 64 65 70 65 6e 64   function depend
10ca5 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 76 61 6c 75  s on.** the valu
10ca6 65 20 6f 66 20 74 68 65 20 63 72 65 61 74 65 46  e of the createF
10ca7 6c 61 67 20 61 72 67 75 6d 65 6e 74 2e 20 20 30  lag argument.  0
10ca8 20 6d 65 61 6e 73 20 64 6f 20 6e 6f 74 20 61 6c   means do not al
10ca9 6c 6f 63 61 74 65 20 61 20 6e 65 77 0a 2a 2a 20  locate a new.** 
10caa 70 61 67 65 2e 20 20 31 20 6d 65 61 6e 73 20 61  page.  1 means a
10cab 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61  llocate a new pa
10cac 67 65 20 69 66 20 73 70 61 63 65 20 69 73 20 65  ge if space is e
10cad 61 73 69 6c 79 20 61 76 61 69 6c 61 62 6c 65 2e  asily available.
10cae 20 20 32 20 0a 2a 2a 20 6d 65 61 6e 73 20 74 6f    2 .** means to
10caf 20 74 72 79 20 72 65 61 6c 6c 79 20 68 61 72 64   try really hard
10cb0 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e   to allocate a n
10cb1 65 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 46  ew page..**.** F
10cb2 6f 72 20 61 20 6e 6f 6e 2d 70 75 72 67 65 61 62  or a non-purgeab
10cb3 6c 65 20 63 61 63 68 65 20 28 61 20 63 61 63 68  le cache (a cach
10cb4 65 20 75 73 65 64 20 61 73 20 74 68 65 20 73 74  e used as the st
10cb5 6f 72 61 67 65 20 66 6f 72 20 61 6e 20 69 6e 2d  orage for an in-
10cb6 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 62 61  memory.** databa
10cb7 73 65 29 20 74 68 65 72 65 20 69 73 20 72 65 61  se) there is rea
10cb8 6c 6c 79 20 6e 6f 20 64 69 66 66 65 72 65 6e 63  lly no differenc
10cb9 65 20 62 65 74 77 65 65 6e 20 63 72 65 61 74 65  e between create
10cba 46 6c 61 67 20 31 20 61 6e 64 20 32 2e 20 20 53  Flag 1 and 2.  S
10cbb 6f 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69 6e 67  o.** the calling
10cbc 20 66 75 6e 63 74 69 6f 6e 20 28 70 63 61 63 68   function (pcach
10cbd 65 2e 63 29 20 77 69 6c 6c 20 6e 65 76 65 72 20  e.c) will never 
10cbe 68 61 76 65 20 61 20 63 72 65 61 74 65 46 6c 61  have a createFla
10cbf 67 20 6f 66 20 31 20 6f 6e 0a 2a 2a 20 61 20 6e  g of 1 on.** a n
10cc0 6f 6e 2d 70 75 72 67 61 62 6c 65 20 63 61 63 68  on-purgable cach
10cc1 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61  e..**.** There a
10cc2 72 65 20 74 68 72 65 65 20 64 69 66 66 65 72 65  re three differe
10cc3 6e 74 20 61 70 70 72 6f 61 63 68 65 73 20 74 6f  nt approaches to
10cc4 20 6f 62 74 61 69 6e 69 6e 67 20 73 70 61 63 65   obtaining space
10cc5 20 66 6f 72 20 61 20 70 61 67 65 2c 0a 2a 2a 20   for a page,.** 
10cc6 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65  depending on the
10cc7 20 76 61 6c 75 65 20 6f 66 20 70 61 72 61 6d 65   value of parame
10cc8 74 65 72 20 63 72 65 61 74 65 46 6c 61 67 20 28  ter createFlag (
10cc9 77 68 69 63 68 20 6d 61 79 20 62 65 20 30 2c 20  which may be 0, 
10cca 31 20 6f 72 20 32 29 2e 0a 2a 2a 0a 2a 2a 20 20  1 or 2)..**.**  
10ccb 20 31 2e 20 52 65 67 61 72 64 6c 65 73 73 20 6f   1. Regardless o
10ccc 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 63  f the value of c
10ccd 72 65 61 74 65 46 6c 61 67 2c 20 74 68 65 20 63  reateFlag, the c
10cce 61 63 68 65 20 69 73 20 73 65 61 72 63 68 65 64  ache is searched
10ccf 20 66 6f 72 20 61 20 0a 2a 2a 20 20 20 20 20 20   for a .**      
10cd0 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 71 75  copy of the requ
10cd1 65 73 74 65 64 20 70 61 67 65 2e 20 49 66 20 6f  ested page. If o
10cd2 6e 65 20 69 73 20 66 6f 75 6e 64 2c 20 69 74 20  ne is found, it 
10cd3 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
10cd4 2a 2a 20 20 20 32 2e 20 49 66 20 63 72 65 61 74  **   2. If creat
10cd5 65 46 6c 61 67 3d 3d 30 20 61 6e 64 20 74 68 65  eFlag==0 and the
10cd6 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72   page is not alr
10cd7 65 61 64 79 20 69 6e 20 74 68 65 20 63 61 63 68  eady in the cach
10cd8 65 2c 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20 20 20  e, NULL is.**   
10cd9 20 20 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a     returned..**.
10cda 2a 2a 20 20 20 33 2e 20 49 66 20 63 72 65 61 74  **   3. If creat
10cdb 65 46 6c 61 67 20 69 73 20 31 2c 20 61 6e 64 20  eFlag is 1, and 
10cdc 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20  the page is not 
10cdd 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63  already in the c
10cde 61 63 68 65 2c 0a 2a 2a 20 20 20 20 20 20 61 6e  ache,.**      an
10cdf 64 20 69 66 20 65 69 74 68 65 72 20 6f 66 20 74  d if either of t
10ce0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65  he following are
10ce1 20 74 72 75 65 2c 20 72 65 74 75 72 6e 20 4e 55   true, return NU
10ce2 4c 4c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  LL:.**.**       
10ce3 28 61 29 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  (a) the number o
10ce4 66 20 70 61 67 65 73 20 70 69 6e 6e 65 64 20 62  f pages pinned b
10ce5 79 20 74 68 65 20 63 61 63 68 65 20 69 73 20 67  y the cache is g
10ce6 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 20  reater than.**  
10ce7 20 20 20 20 20 20 20 20 20 50 43 61 63 68 65 31           PCache1
10ce8 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 20 20 20 20  .nMax, or.**    
10ce9 20 20 20 28 62 29 20 74 68 65 20 6e 75 6d 62 65     (b) the numbe
10cea 72 20 6f 66 20 70 61 67 65 73 20 70 69 6e 6e 65  r of pages pinne
10ceb 64 20 62 79 20 74 68 65 20 63 61 63 68 65 20 69  d by the cache i
10cec 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a  s greater than.*
10ced 2a 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20  *           the 
10cee 73 75 6d 20 6f 66 20 6e 4d 61 78 20 66 6f 72 20  sum of nMax for 
10cef 61 6c 6c 20 70 75 72 67 65 61 62 6c 65 20 63 61  all purgeable ca
10cf0 63 68 65 73 2c 20 6c 65 73 73 20 74 68 65 20 73  ches, less the s
10cf1 75 6d 20 6f 66 20 0a 2a 2a 20 20 20 20 20 20 20  um of .**       
10cf2 20 20 20 20 6e 4d 69 6e 20 66 6f 72 20 61 6c 6c      nMin for all
10cf3 20 6f 74 68 65 72 20 70 75 72 67 65 61 62 6c 65   other purgeable
10cf4 20 63 61 63 68 65 73 2e 20 0a 2a 2a 0a 2a 2a 20   caches. .**.** 
10cf5 20 20 34 2e 20 49 66 20 6e 6f 6e 65 20 6f 66 20    4. If none of 
10cf6 74 68 65 20 66 69 72 73 74 20 74 68 72 65 65 20  the first three 
10cf7 63 6f 6e 64 69 74 69 6f 6e 73 20 61 70 70 6c 79  conditions apply
10cf8 20 61 6e 64 20 74 68 65 20 63 61 63 68 65 20 69   and the cache i
10cf9 73 20 6d 61 72 6b 65 64 0a 2a 2a 20 20 20 20 20  s marked.**     
10cfa 20 61 73 20 70 75 72 67 65 61 62 6c 65 2c 20 61   as purgeable, a
10cfb 6e 64 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65  nd if one of the
10cfc 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72   following is tr
10cfd 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  ue:.**.**       
10cfe 28 61 29 20 54 68 65 20 6e 75 6d 62 65 72 20 6f  (a) The number o
10cff 66 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 74 65  f pages allocate
10d00 64 20 66 6f 72 20 74 68 65 20 63 61 63 68 65 20  d for the cache 
10d01 69 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a 20 20  is already .**  
10d02 20 20 20 20 20 20 20 20 20 50 43 61 63 68 65 31           PCache1
10d03 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20  .nMax, or.**.** 
10d04 20 20 20 20 20 20 28 62 29 20 54 68 65 20 6e 75        (b) The nu
10d05 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 61 6c  mber of pages al
10d06 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 6c 6c 20  located for all 
10d07 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73  purgeable caches
10d08 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   is.**          
10d09 20 61 6c 72 65 61 64 79 20 65 71 75 61 6c 20 74   already equal t
10d0a 6f 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61  o or greater tha
10d0b 6e 20 74 68 65 20 73 75 6d 20 6f 66 20 6e 4d 61  n the sum of nMa
10d0c 78 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20 20 20 20  x for all.**    
10d0d 20 20 20 20 20 20 20 70 75 72 67 65 61 62 6c 65         purgeable
10d0e 20 63 61 63 68 65 73 2c 0a 2a 2a 0a 2a 2a 20 20   caches,.**.**  
10d0f 20 20 20 20 74 68 65 6e 20 61 74 74 65 6d 70 74      then attempt
10d10 20 74 6f 20 72 65 63 79 63 6c 65 20 61 20 70 61   to recycle a pa
10d11 67 65 20 66 72 6f 6d 20 74 68 65 20 4c 52 55 20  ge from the LRU 
10d12 6c 69 73 74 2e 20 49 66 20 69 74 20 69 73 20 74  list. If it is t
10d13 68 65 20 72 69 67 68 74 0a 2a 2a 20 20 20 20 20  he right.**     
10d14 20 73 69 7a 65 2c 20 72 65 74 75 72 6e 20 74 68   size, return th
10d15 65 20 72 65 63 79 63 6c 65 64 20 62 75 66 66 65  e recycled buffe
10d16 72 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 72  r. Otherwise, fr
10d17 65 65 20 74 68 65 20 62 75 66 66 65 72 20 61 6e  ee the buffer an
10d18 64 0a 2a 2a 20 20 20 20 20 20 70 72 6f 63 65 65  d.**      procee
10d19 64 20 74 6f 20 73 74 65 70 20 35 2e 20 0a 2a 2a  d to step 5. .**
10d1a 0a 2a 2a 20 20 20 35 2e 20 4f 74 68 65 72 77 69  .**   5. Otherwi
10d1b 73 65 2c 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64  se, allocate and
10d1c 20 72 65 74 75 72 6e 20 61 20 6e 65 77 20 70 61   return a new pa
10d1d 67 65 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74  ge buffer..*/.st
10d1e 61 74 69 63 20 76 6f 69 64 20 2a 70 63 61 63 68  atic void *pcach
10d1f 65 31 46 65 74 63 68 28 73 71 6c 69 74 65 33 5f  e1Fetch(sqlite3_
10d20 70 63 61 63 68 65 20 2a 70 2c 20 75 6e 73 69 67  pcache *p, unsig
10d21 6e 65 64 20 69 6e 74 20 69 4b 65 79 2c 20 69 6e  ned int iKey, in
10d22 74 20 63 72 65 61 74 65 46 6c 61 67 29 7b 0a 20  t createFlag){. 
10d23 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 50   unsigned int nP
10d24 69 6e 6e 65 64 3b 0a 20 20 50 43 61 63 68 65 31  inned;.  PCache1
10d25 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63   *pCache = (PCac
10d26 68 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 72  he1 *)p;.  PgHdr
10d27 31 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 0a 20  1 *pPage = 0;.. 
10d28 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65 2d   assert( pCache-
10d29 3e 62 50 75 72 67 65 61 62 6c 65 20 7c 7c 20 63  >bPurgeable || c
10d2a 72 65 61 74 65 46 6c 61 67 21 3d 31 20 29 3b 0a  reateFlag!=1 );.
10d2b 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75    pcache1EnterMu
10d2c 74 65 78 28 29 3b 0a 20 20 69 66 28 20 63 72 65  tex();.  if( cre
10d2d 61 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 71 6c  ateFlag==1 ) sql
10d2e 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d  ite3BeginBenignM
10d2f 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 2f 2a 20 53  alloc();..  /* S
10d30 65 61 72 63 68 20 74 68 65 20 68 61 73 68 20 74  earch the hash t
10d31 61 62 6c 65 20 66 6f 72 20 61 6e 20 65 78 69 73  able for an exis
10d32 74 69 6e 67 20 65 6e 74 72 79 2e 20 2a 2f 0a 20  ting entry. */. 
10d33 20 69 66 28 20 70 43 61 63 68 65 2d 3e 6e 48 61   if( pCache->nHa
10d34 73 68 3e 30 20 29 7b 0a 20 20 20 20 75 6e 73 69  sh>0 ){.    unsi
10d35 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 69 4b 65  gned int h = iKe
10d36 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73  y % pCache->nHas
10d37 68 3b 0a 20 20 20 20 66 6f 72 28 70 50 61 67 65  h;.    for(pPage
10d38 3d 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b  =pCache->apHash[
10d39 68 5d 3b 20 70 50 61 67 65 26 26 70 50 61 67 65  h]; pPage&&pPage
10d3a 2d 3e 69 4b 65 79 21 3d 69 4b 65 79 3b 20 70 50  ->iKey!=iKey; pP
10d3b 61 67 65 3d 70 50 61 67 65 2d 3e 70 4e 65 78 74  age=pPage->pNext
10d3c 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50  );.  }..  if( pP
10d3d 61 67 65 20 7c 7c 20 63 72 65 61 74 65 46 6c 61  age || createFla
10d3e 67 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 61 63  g==0 ){.    pcac
10d3f 68 65 31 50 69 6e 50 61 67 65 28 70 50 61 67 65  he1PinPage(pPage
10d40 29 3b 0a 20 20 20 20 67 6f 74 6f 20 66 65 74 63  );.    goto fetc
10d41 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  h_out;.  }..  /*
10d42 20 53 74 65 70 20 33 20 6f 66 20 68 65 61 64 65   Step 3 of heade
10d43 72 20 63 6f 6d 6d 65 6e 74 2e 20 2a 2f 0a 20 20  r comment. */.  
10d44 6e 50 69 6e 6e 65 64 20 3d 20 70 43 61 63 68 65  nPinned = pCache
10d45 2d 3e 6e 50 61 67 65 20 2d 20 70 43 61 63 68 65  ->nPage - pCache
10d46 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 3b 0a 20  ->nRecyclable;. 
10d47 20 69 66 28 20 63 72 65 61 74 65 46 6c 61 67 3d   if( createFlag=
10d48 3d 31 20 26 26 20 28 0a 20 20 20 20 20 20 20 20  =1 && (.        
10d49 6e 50 69 6e 6e 65 64 3e 3d 28 70 63 61 63 68 65  nPinned>=(pcache
10d4a 31 2e 6e 4d 61 78 50 61 67 65 2b 70 43 61 63 68  1.nMaxPage+pCach
10d4b 65 2d 3e 6e 4d 69 6e 2d 70 63 61 63 68 65 31 2e  e->nMin-pcache1.
10d4c 6e 4d 69 6e 50 61 67 65 29 0a 20 20 20 20 20 7c  nMinPage).     |
10d4d 7c 20 6e 50 69 6e 6e 65 64 3e 3d 28 70 43 61 63  | nPinned>=(pCac
10d4e 68 65 2d 3e 6e 4d 61 78 20 2a 20 39 20 2f 20 31  he->nMax * 9 / 1
10d4f 30 29 0a 20 20 29 29 7b 0a 20 20 20 20 67 6f 74  0).  )){.    got
10d50 6f 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d  o fetch_out;.  }
10d51 0a 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e  ..  if( pCache->
10d52 6e 50 61 67 65 3e 3d 70 43 61 63 68 65 2d 3e 6e  nPage>=pCache->n
10d53 48 61 73 68 20 26 26 20 70 63 61 63 68 65 31 52  Hash && pcache1R
10d54 65 73 69 7a 65 48 61 73 68 28 70 43 61 63 68 65  esizeHash(pCache
10d55 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 65  ) ){.    goto fe
10d56 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20  tch_out;.  }..  
10d57 2f 2a 20 53 74 65 70 20 34 2e 20 54 72 79 20 74  /* Step 4. Try t
10d58 6f 20 72 65 63 79 63 6c 65 20 61 20 70 61 67 65  o recycle a page
10d59 20 62 75 66 66 65 72 20 69 66 20 61 70 70 72 6f   buffer if appro
10d5a 70 72 69 61 74 65 2e 20 2a 2f 0a 20 20 69 66 28  priate. */.  if(
10d5b 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61   pCache->bPurgea
10d5c 62 6c 65 20 26 26 20 70 63 61 63 68 65 31 2e 70  ble && pcache1.p
10d5d 4c 72 75 54 61 69 6c 20 26 26 20 28 0a 20 20 20  LruTail && (.   
10d5e 20 20 28 70 43 61 63 68 65 2d 3e 6e 50 61 67 65    (pCache->nPage
10d5f 2b 31 3e 3d 70 43 61 63 68 65 2d 3e 6e 4d 61 78  +1>=pCache->nMax
10d60 29 20 7c 7c 20 70 63 61 63 68 65 31 2e 6e 43 75  ) || pcache1.nCu
10d61 72 72 65 6e 74 50 61 67 65 3e 3d 70 63 61 63 68  rrentPage>=pcach
10d62 65 31 2e 6e 4d 61 78 50 61 67 65 0a 20 20 29 29  e1.nMaxPage.  ))
10d63 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 63  {.    pPage = pc
10d64 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 3b 0a  ache1.pLruTail;.
10d65 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f 76      pcache1Remov
10d66 65 46 72 6f 6d 48 61 73 68 28 70 50 61 67 65 29  eFromHash(pPage)
10d67 3b 0a 20 20 20 20 70 63 61 63 68 65 31 50 69 6e  ;.    pcache1Pin
10d68 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
10d69 20 69 66 28 20 70 50 61 67 65 2d 3e 70 43 61 63   if( pPage->pCac
10d6a 68 65 2d 3e 73 7a 50 61 67 65 21 3d 70 43 61 63  he->szPage!=pCac
10d6b 68 65 2d 3e 73 7a 50 61 67 65 20 29 7b 0a 20 20  he->szPage ){.  
10d6c 20 20 20 20 70 63 61 63 68 65 31 46 72 65 65 50      pcache1FreeP
10d6d 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20  age(pPage);.    
10d6e 20 20 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 20    pPage = 0;.   
10d6f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 63   }else{.      pc
10d70 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 61  ache1.nCurrentPa
10d71 67 65 20 2d 3d 20 28 70 50 61 67 65 2d 3e 70 43  ge -= (pPage->pC
10d72 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65  ache->bPurgeable
10d73 20 2d 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67   - pCache->bPurg
10d74 65 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20  eable);.    }.  
10d75 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 35 2e 20  }..  /* Step 5. 
10d76 49 66 20 61 20 75 73 61 62 6c 65 20 70 61 67 65  If a usable page
10d77 20 62 75 66 66 65 72 20 68 61 73 20 73 74 69 6c   buffer has stil
10d78 6c 20 6e 6f 74 20 62 65 65 6e 20 66 6f 75 6e 64  l not been found
10d79 2c 20 0a 20 20 2a 2a 20 61 74 74 65 6d 70 74 20  , .  ** attempt 
10d7a 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65  to allocate a ne
10d7b 77 20 6f 6e 65 2e 20 0a 20 20 2a 2f 0a 20 20 69  w one. .  */.  i
10d7c 66 28 20 21 70 50 61 67 65 20 29 7b 0a 20 20 20  f( !pPage ){.   
10d7d 20 70 50 61 67 65 20 3d 20 70 63 61 63 68 65 31   pPage = pcache1
10d7e 41 6c 6c 6f 63 50 61 67 65 28 70 43 61 63 68 65  AllocPage(pCache
10d7f 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50  );.  }..  if( pP
10d80 61 67 65 20 29 7b 0a 20 20 20 20 75 6e 73 69 67  age ){.    unsig
10d81 6e 65 64 20 69 6e 74 20 68 20 3d 20 69 4b 65 79  ned int h = iKey
10d82 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68   % pCache->nHash
10d83 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 50  ;.    pCache->nP
10d84 61 67 65 2b 2b 3b 0a 20 20 20 20 70 50 61 67 65  age++;.    pPage
10d85 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 79 3b 0a 20  ->iKey = iKey;. 
10d86 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 20     pPage->pNext 
10d87 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68  = pCache->apHash
10d88 5b 68 5d 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  [h];.    pPage->
10d89 70 43 61 63 68 65 20 3d 20 70 43 61 63 68 65 3b  pCache = pCache;
10d8a 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75  .    pPage->pLru
10d8b 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 50  Prev = 0;.    pP
10d8c 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20  age->pLruNext = 
10d8d 30 3b 0a 20 20 20 20 2a 28 76 6f 69 64 20 2a 2a  0;.    *(void **
10d8e 29 28 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45  )(PGHDR1_TO_PAGE
10d8f 28 70 50 61 67 65 29 29 20 3d 20 30 3b 0a 20 20  (pPage)) = 0;.  
10d90 20 20 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68    pCache->apHash
10d91 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d  [h] = pPage;.  }
10d92 0a 0a 66 65 74 63 68 5f 6f 75 74 3a 0a 20 20 69  ..fetch_out:.  i
10d93 66 28 20 70 50 61 67 65 20 26 26 20 69 4b 65 79  f( pPage && iKey
10d94 3e 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79  >pCache->iMaxKey
10d95 20 29 7b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e   ){.    pCache->
10d96 69 4d 61 78 4b 65 79 20 3d 20 69 4b 65 79 3b 0a  iMaxKey = iKey;.
10d97 20 20 7d 0a 20 20 69 66 28 20 63 72 65 61 74 65    }.  if( create
10d98 46 6c 61 67 3d 3d 31 20 29 20 73 71 6c 69 74 65  Flag==1 ) sqlite
10d99 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  3EndBenignMalloc
10d9a 28 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61  ();.  pcache1Lea
10d9b 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74  veMutex();.  ret
10d9c 75 72 6e 20 28 70 50 61 67 65 20 3f 20 50 47 48  urn (pPage ? PGH
10d9d 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 50 61 67  DR1_TO_PAGE(pPag
10d9e 65 29 20 3a 20 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a  e) : 0);.}.../*.
10d9f 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
10da0 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  n of the sqlite3
10da1 5f 70 63 61 63 68 65 2e 78 55 6e 70 69 6e 20 6d  _pcache.xUnpin m
10da2 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 4d 61 72  ethod..**.** Mar
10da3 6b 20 61 20 70 61 67 65 20 61 73 20 75 6e 70 69  k a page as unpi
10da4 6e 6e 65 64 20 28 65 6c 69 67 69 62 6c 65 20 66  nned (eligible f
10da5 6f 72 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73 20  or asynchronous 
10da6 72 65 63 79 63 6c 69 6e 67 29 2e 0a 2a 2f 0a 73  recycling)..*/.s
10da7 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68  tatic void pcach
10da8 65 31 55 6e 70 69 6e 28 73 71 6c 69 74 65 33 5f  e1Unpin(sqlite3_
10da9 70 63 61 63 68 65 20 2a 70 2c 20 76 6f 69 64 20  pcache *p, void 
10daa 2a 70 50 67 2c 20 69 6e 74 20 72 65 75 73 65 55  *pPg, int reuseU
10dab 6e 6c 69 6b 65 6c 79 29 7b 0a 20 20 50 43 61 63  nlikely){.  PCac
10dac 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50  he1 *pCache = (P
10dad 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 50 67  Cache1 *)p;.  Pg
10dae 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20 50 41  Hdr1 *pPage = PA
10daf 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70 43 61  GE_TO_PGHDR1(pCa
10db0 63 68 65 2c 20 70 50 67 29 3b 0a 20 0a 20 20 61  che, pPg);. .  a
10db1 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 43  ssert( pPage->pC
10db2 61 63 68 65 3d 3d 70 43 61 63 68 65 20 29 3b 0a  ache==pCache );.
10db3 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75    pcache1EnterMu
10db4 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 49 74 20  tex();..  /* It 
10db5 69 73 20 61 6e 20 65 72 72 6f 72 20 74 6f 20 63  is an error to c
10db6 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  all this functio
10db7 6e 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73  n if the page is
10db8 20 61 6c 72 65 61 64 79 20 0a 20 20 2a 2a 20 70   already .  ** p
10db9 61 72 74 20 6f 66 20 74 68 65 20 67 6c 6f 62 61  art of the globa
10dba 6c 20 4c 52 55 20 6c 69 73 74 2e 0a 20 20 2a 2f  l LRU list..  */
10dbb 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
10dbc 2d 3e 70 4c 72 75 50 72 65 76 3d 3d 30 20 26 26  ->pLruPrev==0 &&
10dbd 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74   pPage->pLruNext
10dbe 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
10dbf 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61   pcache1.pLruHea
10dc0 64 21 3d 70 50 61 67 65 20 26 26 20 70 63 61 63  d!=pPage && pcac
10dc1 68 65 31 2e 70 4c 72 75 54 61 69 6c 21 3d 70 50  he1.pLruTail!=pP
10dc2 61 67 65 20 29 3b 0a 0a 20 20 69 66 28 20 72 65  age );..  if( re
10dc3 75 73 65 55 6e 6c 69 6b 65 6c 79 20 7c 7c 20 70  useUnlikely || p
10dc4 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50  cache1.nCurrentP
10dc5 61 67 65 3e 70 63 61 63 68 65 31 2e 6e 4d 61 78  age>pcache1.nMax
10dc6 50 61 67 65 20 29 7b 0a 20 20 20 20 70 63 61 63  Page ){.    pcac
10dc7 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73  he1RemoveFromHas
10dc8 68 28 70 50 61 67 65 29 3b 0a 20 20 20 20 70 63  h(pPage);.    pc
10dc9 61 63 68 65 31 46 72 65 65 50 61 67 65 28 70 50  ache1FreePage(pP
10dca 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  age);.  }else{. 
10dcb 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 70 61     /* Add the pa
10dcc 67 65 20 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c  ge to the global
10dcd 20 4c 52 55 20 6c 69 73 74 2e 20 4e 6f 72 6d 61   LRU list. Norma
10dce 6c 6c 79 2c 20 74 68 65 20 70 61 67 65 20 69 73  lly, the page is
10dcf 20 61 64 64 65 64 20 74 6f 0a 20 20 20 20 2a 2a   added to.    **
10dd0 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 68 65   the head of the
10dd1 20 6c 69 73 74 20 28 6c 61 73 74 20 70 61 67 65   list (last page
10dd2 20 74 6f 20 62 65 20 72 65 63 79 63 6c 65 64 29   to be recycled)
10dd3 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74 68  . However, if th
10dd4 65 20 0a 20 20 20 20 2a 2a 20 72 65 75 73 65 55  e .    ** reuseU
10dd5 6e 6c 69 6b 65 6c 79 20 66 6c 61 67 20 70 61 73  nlikely flag pas
10dd6 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63  sed to this func
10dd7 74 69 6f 6e 20 69 73 20 74 72 75 65 2c 20 74 68  tion is true, th
10dd8 65 20 70 61 67 65 20 69 73 20 61 64 64 65 64 0a  e page is added.
10dd9 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 74 61      ** to the ta
10dda 69 6c 20 6f 66 20 74 68 65 20 6c 69 73 74 20 28  il of the list (
10ddb 66 69 72 73 74 20 70 61 67 65 20 74 6f 20 62 65  first page to be
10ddc 20 72 65 63 79 63 6c 65 64 29 2e 0a 20 20 20 20   recycled)..    
10ddd 2a 2f 0a 20 20 20 20 69 66 28 20 70 63 61 63 68  */.    if( pcach
10dde 65 31 2e 70 4c 72 75 48 65 61 64 20 29 7b 0a 20  e1.pLruHead ){. 
10ddf 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72       pcache1.pLr
10de0 75 48 65 61 64 2d 3e 70 4c 72 75 50 72 65 76 20  uHead->pLruPrev 
10de1 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20 20 70  = pPage;.      p
10de2 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 3d  Page->pLruNext =
10de3 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61   pcache1.pLruHea
10de4 64 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31  d;.      pcache1
10de5 2e 70 4c 72 75 48 65 61 64 20 3d 20 70 50 61 67  .pLruHead = pPag
10de6 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  e;.    }else{.  
10de7 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75      pcache1.pLru
10de8 54 61 69 6c 20 3d 20 70 50 61 67 65 3b 0a 20 20  Tail = pPage;.  
10de9 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75      pcache1.pLru
10dea 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a 20 20  Head = pPage;.  
10deb 20 20 7d 0a 20 20 20 20 70 43 61 63 68 65 2d 3e    }.    pCache->
10dec 6e 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b 0a 20  nRecyclable++;. 
10ded 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c 65 61   }..  pcache1Lea
10dee 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a  veMutex();.}../*
10def 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
10df0 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  on of the sqlite
10df1 33 5f 70 63 61 63 68 65 2e 78 52 65 6b 65 79 20  3_pcache.xRekey 
10df2 6d 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74  method. .*/.stat
10df3 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 52  ic void pcache1R
10df4 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 33 5f  ekey(.  sqlite3_
10df5 70 63 61 63 68 65 20 2a 70 2c 0a 20 20 76 6f 69  pcache *p,.  voi
10df6 64 20 2a 70 50 67 2c 0a 20 20 75 6e 73 69 67 6e  d *pPg,.  unsign
10df7 65 64 20 69 6e 74 20 69 4f 6c 64 2c 0a 20 20 75  ed int iOld,.  u
10df8 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4e 65 77  nsigned int iNew
10df9 0a 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70  .){.  PCache1 *p
10dfa 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31  Cache = (PCache1
10dfb 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 31 20 2a   *)p;.  PgHdr1 *
10dfc 70 50 61 67 65 20 3d 20 50 41 47 45 5f 54 4f 5f  pPage = PAGE_TO_
10dfd 50 47 48 44 52 31 28 70 43 61 63 68 65 2c 20 70  PGHDR1(pCache, p
10dfe 50 67 29 3b 0a 20 20 50 67 48 64 72 31 20 2a 2a  Pg);.  PgHdr1 **
10dff 70 70 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69  pp;.  unsigned i
10e00 6e 74 20 68 3b 20 0a 20 20 61 73 73 65 72 74 28  nt h; .  assert(
10e01 20 70 50 61 67 65 2d 3e 69 4b 65 79 3d 3d 69 4f   pPage->iKey==iO
10e02 6c 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ld );.  assert( 
10e03 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3d 3d 70  pPage->pCache==p
10e04 43 61 63 68 65 20 29 3b 0a 0a 20 20 70 63 61 63  Cache );..  pcac
10e05 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  he1EnterMutex();
10e06 0a 0a 20 20 68 20 3d 20 69 4f 6c 64 25 70 43 61  ..  h = iOld%pCa
10e07 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 70 70  che->nHash;.  pp
10e08 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70 48 61   = &pCache->apHa
10e09 73 68 5b 68 5d 3b 0a 20 20 77 68 69 6c 65 28 20  sh[h];.  while( 
10e0a 28 2a 70 70 29 21 3d 70 50 61 67 65 20 29 7b 0a  (*pp)!=pPage ){.
10e0b 20 20 20 20 70 70 20 3d 20 26 28 2a 70 70 29 2d      pp = &(*pp)-
10e0c 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 2a 70  >pNext;.  }.  *p
10e0d 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74  p = pPage->pNext
10e0e 3b 0a 0a 20 20 68 20 3d 20 69 4e 65 77 25 70 43  ;..  h = iNew%pC
10e0f 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 70  ache->nHash;.  p
10e10 50 61 67 65 2d 3e 69 4b 65 79 20 3d 20 69 4e 65  Page->iKey = iNe
10e11 77 3b 0a 20 20 70 50 61 67 65 2d 3e 70 4e 65 78  w;.  pPage->pNex
10e12 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61  t = pCache->apHa
10e13 73 68 5b 68 5d 3b 0a 20 20 70 43 61 63 68 65 2d  sh[h];.  pCache-
10e14 3e 61 70 48 61 73 68 5b 68 5d 20 3d 20 70 50 61  >apHash[h] = pPa
10e15 67 65 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 78 52  ge;..  /* The xR
10e16 65 6b 65 79 28 29 20 69 6e 74 65 72 66 61 63 65  ekey() interface
10e17 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 74 6f   is only used to
10e18 20 6d 6f 76 65 20 70 61 67 65 73 20 65 61 72 6c   move pages earl
10e19 69 65 72 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20  ier in the.  ** 
10e1a 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 69  database file (i
10e1b 6e 20 6f 72 64 65 72 20 74 6f 20 6d 6f 76 65 20  n order to move 
10e1c 61 6c 6c 20 66 72 65 65 20 70 61 67 65 73 20 74  all free pages t
10e1d 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  o the end of the
10e1e 0a 20 20 2a 2a 20 66 69 6c 65 20 77 68 65 72 65  .  ** file where
10e1f 20 74 68 65 79 20 63 61 6e 20 62 65 20 74 72 75   they can be tru
10e20 6e 63 61 74 65 64 20 6f 66 66 2e 29 20 20 48 65  ncated off.)  He
10e21 6e 63 65 2c 20 69 74 20 69 73 20 6e 6f 74 20 70  nce, it is not p
10e22 6f 73 73 69 62 6c 65 0a 20 20 2a 2a 20 66 6f 72  ossible.  ** for
10e23 20 74 68 65 20 6e 65 77 20 70 61 67 65 20 6e 75   the new page nu
10e24 6d 62 65 72 20 74 6f 20 62 65 20 67 72 65 61 74  mber to be great
10e25 65 72 20 74 68 61 6e 20 74 68 65 20 6c 61 72 67  er than the larg
10e26 65 73 74 20 70 72 65 76 69 6f 75 73 6c 79 0a 20  est previously. 
10e27 20 2a 2a 20 66 65 74 63 68 65 64 20 70 61 67 65   ** fetched page
10e28 2e 20 20 42 75 74 20 77 65 20 72 65 74 61 69 6e  .  But we retain
10e29 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   the following t
10e2a 65 73 74 20 69 6e 20 63 61 73 65 20 78 52 65 6b  est in case xRek
10e2b 65 79 28 29 0a 20 20 2a 2a 20 62 65 67 69 6e 73  ey().  ** begins
10e2c 20 74 6f 20 62 65 20 75 73 65 64 20 69 6e 20 64   to be used in d
10e2d 69 66 66 65 72 65 6e 74 20 77 61 79 73 20 69 6e  ifferent ways in
10e2e 20 74 68 65 20 66 75 74 75 72 65 2e 0a 20 20 2a   the future..  *
10e2f 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 69 4e  /.  if( NEVER(iN
10e30 65 77 3e 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b  ew>pCache->iMaxK
10e31 65 79 29 20 29 7b 0a 20 20 20 20 70 43 61 63 68  ey) ){.    pCach
10e32 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4e 65  e->iMaxKey = iNe
10e33 77 3b 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65  w;.  }..  pcache
10e34 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d  1LeaveMutex();.}
10e35 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
10e36 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71  tation of the sq
10e37 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 54 72  lite3_pcache.xTr
10e38 75 6e 63 61 74 65 20 6d 65 74 68 6f 64 2e 20 0a  uncate method. .
10e39 2a 2a 0a 2a 2a 20 44 69 73 63 61 72 64 20 61 6c  **.** Discard al
10e3a 6c 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73  l unpinned pages
10e3b 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 77 69   in the cache wi
10e3c 74 68 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72  th a page number
10e3d 20 65 71 75 61 6c 20 74 6f 0a 2a 2a 20 6f 72 20   equal to.** or 
10e3e 67 72 65 61 74 65 72 20 74 68 61 6e 20 70 61 72  greater than par
10e3f 61 6d 65 74 65 72 20 69 4c 69 6d 69 74 2e 20 41  ameter iLimit. A
10e40 6e 79 20 70 69 6e 6e 65 64 20 70 61 67 65 73 20  ny pinned pages 
10e41 77 69 74 68 20 61 20 70 61 67 65 20 6e 75 6d 62  with a page numb
10e42 65 72 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 20 6f  er.** equal to o
10e43 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 69  r greater than i
10e44 4c 69 6d 69 74 20 61 72 65 20 69 6d 70 6c 69 63  Limit are implic
10e45 69 74 6c 79 20 75 6e 70 69 6e 6e 65 64 2e 0a 2a  itly unpinned..*
10e46 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63  /.static void pc
10e47 61 63 68 65 31 54 72 75 6e 63 61 74 65 28 73 71  ache1Truncate(sq
10e48 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c  lite3_pcache *p,
10e49 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4c   unsigned int iL
10e4a 69 6d 69 74 29 7b 0a 20 20 50 43 61 63 68 65 31  imit){.  PCache1
10e4b 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63   *pCache = (PCac
10e4c 68 65 31 20 2a 29 70 3b 0a 20 20 70 63 61 63 68  he1 *)p;.  pcach
10e4d 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a  e1EnterMutex();.
10e4e 20 20 69 66 28 20 69 4c 69 6d 69 74 3c 3d 70 43    if( iLimit<=pC
10e4f 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 29 7b  ache->iMaxKey ){
10e50 0a 20 20 20 20 70 63 61 63 68 65 31 54 72 75 6e  .    pcache1Trun
10e51 63 61 74 65 55 6e 73 61 66 65 28 70 43 61 63 68  cateUnsafe(pCach
10e52 65 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20  e, iLimit);.    
10e53 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20  pCache->iMaxKey 
10e54 3d 20 69 4c 69 6d 69 74 2d 31 3b 0a 20 20 7d 0a  = iLimit-1;.  }.
10e55 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75    pcache1LeaveMu
10e56 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  tex();.}../*.** 
10e57 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
10e58 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63  f the sqlite3_pc
10e59 61 63 68 65 2e 78 44 65 73 74 72 6f 79 20 6d 65  ache.xDestroy me
10e5a 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 44 65 73  thod. .**.** Des
10e5b 74 72 6f 79 20 61 20 63 61 63 68 65 20 61 6c 6c  troy a cache all
10e5c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 70 63 61  ocated using pca
10e5d 63 68 65 31 43 72 65 61 74 65 28 29 2e 0a 2a 2f  che1Create()..*/
10e5e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61  .static void pca
10e5f 63 68 65 31 44 65 73 74 72 6f 79 28 73 71 6c 69  che1Destroy(sqli
10e60 74 65 33 5f 70 63 61 63 68 65 20 2a 70 29 7b 0a  te3_pcache *p){.
10e61 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68    PCache1 *pCach
10e62 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70  e = (PCache1 *)p
10e63 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72  ;.  pcache1Enter
10e64 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61 63 68  Mutex();.  pcach
10e65 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 66 65  e1TruncateUnsafe
10e66 28 70 43 61 63 68 65 2c 20 30 29 3b 0a 20 20 70  (pCache, 0);.  p
10e67 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 20  cache1.nMaxPage 
10e68 2d 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b  -= pCache->nMax;
10e69 0a 20 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50  .  pcache1.nMinP
10e6a 61 67 65 20 2d 3d 20 70 43 61 63 68 65 2d 3e 6e  age -= pCache->n
10e6b 4d 69 6e 3b 0a 20 20 70 63 61 63 68 65 31 45 6e  Min;.  pcache1En
10e6c 66 6f 72 63 65 4d 61 78 50 61 67 65 28 29 3b 0a  forceMaxPage();.
10e6d 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75    pcache1LeaveMu
10e6e 74 65 78 28 29 3b 0a 20 20 73 71 6c 69 74 65 33  tex();.  sqlite3
10e6f 5f 66 72 65 65 28 70 43 61 63 68 65 2d 3e 61 70  _free(pCache->ap
10e70 48 61 73 68 29 3b 0a 20 20 73 71 6c 69 74 65 33  Hash);.  sqlite3
10e71 5f 66 72 65 65 28 70 43 61 63 68 65 29 3b 0a 7d  _free(pCache);.}
10e72 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
10e73 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
10e74 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a  during initializ
10e75 61 74 69 6f 6e 20 28 73 71 6c 69 74 65 33 5f 69  ation (sqlite3_i
10e76 6e 69 74 69 61 6c 69 7a 65 28 29 29 20 74 6f 0a  nitialize()) to.
10e77 2a 2a 20 69 6e 73 74 61 6c 6c 20 74 68 65 20 64  ** install the d
10e78 65 66 61 75 6c 74 20 70 6c 75 67 67 61 62 6c 65  efault pluggable
10e79 20 63 61 63 68 65 20 6d 6f 64 75 6c 65 2c 20 61   cache module, a
10e7a 73 73 75 6d 69 6e 67 20 74 68 65 20 75 73 65 72  ssuming the user
10e7b 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65   has not.** alre
10e7c 61 64 79 20 70 72 6f 76 69 64 65 64 20 61 6e 20  ady provided an 
10e7d 61 6c 74 65 72 6e 61 74 69 76 65 2e 0a 2a 2f 0a  alternative..*/.
10e7e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
10e7f 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68  oid sqlite3PCach
10e80 65 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64  eSetDefault(void
10e81 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69  ){.  static sqli
10e82 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f  te3_pcache_metho
10e83 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64  ds defaultMethod
10e84 73 20 3d 20 7b 0a 20 20 20 20 30 2c 20 20 20 20  s = {.    0,    
10e85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10e86 20 20 20 2f 2a 20 70 41 72 67 20 2a 2f 0a 20 20     /* pArg */.  
10e87 20 20 70 63 61 63 68 65 31 49 6e 69 74 2c 20 20    pcache1Init,  
10e88 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 49             /* xI
10e89 6e 69 74 20 2a 2f 0a 20 20 20 20 70 63 61 63 68  nit */.    pcach
10e8a 65 31 53 68 75 74 64 6f 77 6e 2c 20 20 20 20 20  e1Shutdown,     
10e8b 20 20 20 20 2f 2a 20 78 53 68 75 74 64 6f 77 6e      /* xShutdown
10e8c 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 43   */.    pcache1C
10e8d 72 65 61 74 65 2c 20 20 20 20 20 20 20 20 20 20  reate,          
10e8e 20 2f 2a 20 78 43 72 65 61 74 65 20 2a 2f 0a 20   /* xCreate */. 
10e8f 20 20 20 70 63 61 63 68 65 31 43 61 63 68 65 73     pcache1Caches
10e90 69 7a 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 78  ize,        /* x
10e91 43 61 63 68 65 73 69 7a 65 20 2a 2f 0a 20 20 20  Cachesize */.   
10e92 20 70 63 61 63 68 65 31 50 61 67 65 63 6f 75 6e   pcache1Pagecoun
10e93 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 50 61  t,        /* xPa
10e94 67 65 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 70  gecount */.    p
10e95 63 61 63 68 65 31 46 65 74 63 68 2c 20 20 20 20  cache1Fetch,    
10e96 20 20 20 20 20 20 20 20 2f 2a 20 78 46 65 74 63          /* xFetc
10e97 68 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31  h */.    pcache1
10e98 55 6e 70 69 6e 2c 20 20 20 20 20 20 20 20 20 20  Unpin,          
10e99 20 20 2f 2a 20 78 55 6e 70 69 6e 20 2a 2f 0a 20    /* xUnpin */. 
10e9a 20 20 20 70 63 61 63 68 65 31 52 65 6b 65 79 2c     pcache1Rekey,
10e9b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
10e9c 52 65 6b 65 79 20 2a 2f 0a 20 20 20 20 70 63 61  Rekey */.    pca
10e9d 63 68 65 31 54 72 75 6e 63 61 74 65 2c 20 20 20  che1Truncate,   
10e9e 20 20 20 20 20 20 2f 2a 20 78 54 72 75 6e 63 61        /* xTrunca
10e9f 74 65 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65  te */.    pcache
10ea0 31 44 65 73 74 72 6f 79 20 20 20 20 20 20 20 20  1Destroy        
10ea1 20 20 20 2f 2a 20 78 44 65 73 74 72 6f 79 20 2a     /* xDestroy *
10ea2 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33  /.  };.  sqlite3
10ea3 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43  _config(SQLITE_C
10ea4 4f 4e 46 49 47 5f 50 43 41 43 48 45 2c 20 26 64  ONFIG_PCACHE, &d
10ea5 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b 0a  efaultMethods);.
10ea6 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
10ea7 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d  _ENABLE_MEMORY_M
10ea8 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20  ANAGEMENT./*.** 
10ea9 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
10eaa 20 63 61 6c 6c 65 64 20 74 6f 20 66 72 65 65 20   called to free 
10eab 73 75 70 65 72 66 6c 75 6f 75 73 20 64 79 6e 61  superfluous dyna
10eac 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65  mically allocate
10ead 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 65 6c 64  d memory.** held
10eae 20 62 79 20 74 68 65 20 70 61 67 65 72 20 73 79   by the pager sy
10eaf 73 74 65 6d 2e 20 4d 65 6d 6f 72 79 20 69 6e 20  stem. Memory in 
10eb0 75 73 65 20 62 79 20 61 6e 79 20 53 51 4c 69 74  use by any SQLit
10eb1 65 20 70 61 67 65 72 20 61 6c 6c 6f 63 61 74 65  e pager allocate
10eb2 64 0a 2a 2a 20 62 79 20 74 68 65 20 63 75 72 72  d.** by the curr
10eb3 65 6e 74 20 74 68 72 65 61 64 20 6d 61 79 20 62  ent thread may b
10eb4 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  e sqlite3_free()
10eb5 65 64 2e 0a 2a 2a 0a 2a 2a 20 6e 52 65 71 20 69  ed..**.** nReq i
10eb6 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
10eb7 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20  bytes of memory 
10eb8 72 65 71 75 69 72 65 64 2e 20 4f 6e 63 65 20 74  required. Once t
10eb9 68 69 73 20 6d 75 63 68 20 68 61 73 0a 2a 2a 20  his much has.** 
10eba 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2c 20 74  been released, t
10ebb 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  he function retu
10ebc 72 6e 73 2e 20 54 68 65 20 72 65 74 75 72 6e 20  rns. The return 
10ebd 76 61 6c 75 65 20 69 73 20 74 68 65 20 74 6f 74  value is the tot
10ebe 61 6c 20 6e 75 6d 62 65 72 20 0a 2a 2a 20 6f 66  al number .** of
10ebf 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79   bytes of memory
10ec0 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2f 0a 53 51   released..*/.SQ
10ec1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
10ec2 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65   sqlite3PcacheRe
10ec3 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74 20  leaseMemory(int 
10ec4 6e 52 65 71 29 7b 0a 20 20 69 6e 74 20 6e 46 72  nReq){.  int nFr
10ec5 65 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 63  ee = 0;.  if( pc
10ec6 61 63 68 65 31 2e 70 53 74 61 72 74 3d 3d 30 20  ache1.pStart==0 
10ec7 29 7b 0a 20 20 20 20 50 67 48 64 72 31 20 2a 70  ){.    PgHdr1 *p
10ec8 3b 0a 20 20 20 20 70 63 61 63 68 65 31 45 6e 74  ;.    pcache1Ent
10ec9 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 77  erMutex();.    w
10eca 68 69 6c 65 28 20 28 6e 52 65 71 3c 30 20 7c 7c  hile( (nReq<0 ||
10ecb 20 6e 46 72 65 65 3c 6e 52 65 71 29 20 26 26 20   nFree<nReq) && 
10ecc 28 70 3d 70 63 61 63 68 65 31 2e 70 4c 72 75 54  (p=pcache1.pLruT
10ecd 61 69 6c 29 20 29 7b 0a 20 20 20 20 20 20 6e 46  ail) ){.      nF
10ece 72 65 65 20 2b 3d 20 73 71 6c 69 74 65 33 4d 61  ree += sqlite3Ma
10ecf 6c 6c 6f 63 53 69 7a 65 28 50 47 48 44 52 31 5f  llocSize(PGHDR1_
10ed0 54 4f 5f 50 41 47 45 28 70 29 29 3b 0a 20 20 20  TO_PAGE(p));.   
10ed1 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 61 67     pcache1PinPag
10ed2 65 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 63  e(p);.      pcac
10ed3 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73  he1RemoveFromHas
10ed4 68 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 63  h(p);.      pcac
10ed5 68 65 31 46 72 65 65 50 61 67 65 28 70 29 3b 0a  he1FreePage(p);.
10ed6 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65      }.    pcache
10ed7 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  1LeaveMutex();. 
10ed8 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 46 72 65   }.  return nFre
10ed9 65 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  e;.}.#endif /* S
10eda 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
10edb 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 20 2a  ORY_MANAGEMENT *
10edc 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
10edd 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73  _TEST./*.** This
10ede 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
10edf 64 20 62 79 20 74 65 73 74 20 70 72 6f 63 65 64  d by test proced
10ee0 75 72 65 73 20 74 6f 20 69 6e 73 70 65 63 74 20  ures to inspect 
10ee1 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 61  the internal sta
10ee2 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 67 6c 6f  te.** of the glo
10ee3 62 61 6c 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51  bal cache..*/.SQ
10ee4 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
10ee5 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53  d sqlite3PcacheS
10ee6 74 61 74 73 28 0a 20 20 69 6e 74 20 2a 70 6e 43  tats(.  int *pnC
10ee7 75 72 72 65 6e 74 2c 20 20 20 20 20 20 2f 2a 20  urrent,      /* 
10ee8 4f 55 54 3a 20 54 6f 74 61 6c 20 6e 75 6d 62 65  OUT: Total numbe
10ee9 72 20 6f 66 20 70 61 67 65 73 20 63 61 63 68 65  r of pages cache
10eea 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4d 61  d */.  int *pnMa
10eeb 78 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  x,          /* O
10eec 55 54 3a 20 47 6c 6f 62 61 6c 20 6d 61 78 69 6d  UT: Global maxim
10eed 75 6d 20 63 61 63 68 65 20 73 69 7a 65 20 2a 2f  um cache size */
10eee 0a 20 20 69 6e 74 20 2a 70 6e 4d 69 6e 2c 20 20  .  int *pnMin,  
10eef 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
10ef0 53 75 6d 20 6f 66 20 50 43 61 63 68 65 31 2e 6e  Sum of PCache1.n
10ef1 4d 69 6e 20 66 6f 72 20 70 75 72 67 65 61 62 6c  Min for purgeabl
10ef2 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20 69 6e  e caches */.  in
10ef3 74 20 2a 70 6e 52 65 63 79 63 6c 61 62 6c 65 20  t *pnRecyclable 
10ef4 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 74 61 6c     /* OUT: Total
10ef5 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
10ef6 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 72   available for r
10ef7 65 63 79 63 6c 69 6e 67 20 2a 2f 0a 29 7b 0a 20  ecycling */.){. 
10ef8 20 50 67 48 64 72 31 20 2a 70 3b 0a 20 20 69 6e   PgHdr1 *p;.  in
10ef9 74 20 6e 52 65 63 79 63 6c 61 62 6c 65 20 3d 20  t nRecyclable = 
10efa 30 3b 0a 20 20 66 6f 72 28 70 3d 70 63 61 63 68  0;.  for(p=pcach
10efb 65 31 2e 70 4c 72 75 48 65 61 64 3b 20 70 3b 20  e1.pLruHead; p; 
10efc 70 3d 70 2d 3e 70 4c 72 75 4e 65 78 74 29 7b 0a  p=p->pLruNext){.
10efd 20 20 20 20 6e 52 65 63 79 63 6c 61 62 6c 65 2b      nRecyclable+
10efe 2b 3b 0a 20 20 7d 0a 20 20 2a 70 6e 43 75 72 72  +;.  }.  *pnCurr
10eff 65 6e 74 20 3d 20 70 63 61 63 68 65 31 2e 6e 43  ent = pcache1.nC
10f00 75 72 72 65 6e 74 50 61 67 65 3b 0a 20 20 2a 70  urrentPage;.  *p
10f01 6e 4d 61 78 20 3d 20 70 63 61 63 68 65 31 2e 6e  nMax = pcache1.n
10f02 4d 61 78 50 61 67 65 3b 0a 20 20 2a 70 6e 4d 69  MaxPage;.  *pnMi
10f03 6e 20 3d 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e  n = pcache1.nMin
10f04 50 61 67 65 3b 0a 20 20 2a 70 6e 52 65 63 79 63  Page;.  *pnRecyc
10f05 6c 61 62 6c 65 20 3d 20 6e 52 65 63 79 63 6c 61  lable = nRecycla
10f06 62 6c 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  ble;.}.#endif../
10f07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
10f08 6e 64 20 6f 66 20 70 63 61 63 68 65 31 2e 63 20  nd of pcache1.c 
10f09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10f0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10f0b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
10f0c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
10f0d 65 67 69 6e 20 66 69 6c 65 20 72 6f 77 73 65 74  egin file rowset
10f0e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
10f0f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
10f11 2a 0a 2a 2a 20 32 30 30 38 20 44 65 63 65 6d 62  *.** 2008 Decemb
10f12 65 72 20 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  er 3.**.** The a
10f13 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
10f14 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
10f15 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
10f16 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
10f17 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
10f18 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
10f19 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
10f1a 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
10f1b 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
10f1c 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
10f1d 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
10f1e 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
10f1f 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
10f20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
10f21 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
10f22 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
10f23 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
10f24 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10f25 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10f26 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10f27 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10f28 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d  ***.**.** This m
10f29 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73  odule implements
10f2a 20 61 6e 20 6f 62 6a 65 63 74 20 77 65 20 63 61   an object we ca
10f2b 6c 6c 20 61 20 22 52 6f 77 53 65 74 22 2e 0a 2a  ll a "RowSet"..*
10f2c 2a 0a 2a 2a 20 54 68 65 20 52 6f 77 53 65 74 20  *.** The RowSet 
10f2d 6f 62 6a 65 63 74 20 69 73 20 61 20 63 6f 6c 6c  object is a coll
10f2e 65 63 74 69 6f 6e 20 6f 66 20 72 6f 77 69 64 73  ection of rowids
10f2f 2e 20 20 52 6f 77 69 64 73 0a 2a 2a 20 61 72 65  .  Rowids.** are
10f30 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74   inserted into t
10f31 68 65 20 52 6f 77 53 65 74 20 69 6e 20 61 6e 20  he RowSet in an 
10f32 61 72 62 69 74 72 61 72 79 20 6f 72 64 65 72 2e  arbitrary order.
10f33 20 20 49 6e 73 65 72 74 73 0a 2a 2a 20 63 61 6e    Inserts.** can
10f34 20 62 65 20 69 6e 74 65 72 6d 69 78 65 64 20 77   be intermixed w
10f35 69 74 68 20 74 65 73 74 73 20 74 6f 20 73 65 65  ith tests to see
10f36 20 69 66 20 61 20 67 69 76 65 6e 20 72 6f 77 69   if a given rowi
10f37 64 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72  d has been.** pr
10f38 65 76 69 6f 75 73 6c 79 20 69 6e 73 65 72 74 65  eviously inserte
10f39 64 20 69 6e 74 6f 20 74 68 65 20 52 6f 77 53 65  d into the RowSe
10f3a 74 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61  t..**.** After a
10f3b 6c 6c 20 69 6e 73 65 72 74 73 20 61 72 65 20 66  ll inserts are f
10f3c 69 6e 69 73 68 65 64 2c 20 69 74 20 69 73 20 70  inished, it is p
10f3d 6f 73 73 69 62 6c 65 20 74 6f 20 65 78 74 72 61  ossible to extra
10f3e 63 74 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e  ct the.** elemen
10f3f 74 73 20 6f 66 20 74 68 65 20 52 6f 77 53 65 74  ts of the RowSet
10f40 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72   in sorted order
10f41 2e 20 20 4f 6e 63 65 20 74 68 69 73 20 65 78 74  .  Once this ext
10f42 72 61 63 74 69 6f 6e 0a 2a 2a 20 70 72 6f 63 65  raction.** proce
10f43 73 73 20 68 61 73 20 73 74 61 72 74 65 64 2c 20  ss has started, 
10f44 6e 6f 20 6e 65 77 20 65 6c 65 6d 65 6e 74 73 20  no new elements 
10f45 6d 61 79 20 62 65 20 69 6e 73 65 72 74 65 64 2e  may be inserted.
10f46 0a 2a 2a 0a 2a 2a 20 48 65 6e 63 65 2c 20 74 68  .**.** Hence, th
10f47 65 20 70 72 69 6d 69 74 69 76 65 20 6f 70 65 72  e primitive oper
10f48 61 74 69 6f 6e 73 20 66 6f 72 20 61 20 52 6f 77  ations for a Row
10f49 53 65 74 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20  Set are:.**.**  
10f4a 20 20 43 52 45 41 54 45 0a 2a 2a 20 20 20 20 49    CREATE.**    I
10f4b 4e 53 45 52 54 0a 2a 2a 20 20 20 20 54 45 53 54  NSERT.**    TEST
10f4c 0a 2a 2a 20 20 20 20 53 4d 41 4c 4c 45 53 54 0a  .**    SMALLEST.
10f4d 2a 2a 20 20 20 20 44 45 53 54 52 4f 59 0a 2a 2a  **    DESTROY.**
10f4e 0a 2a 2a 20 54 68 65 20 43 52 45 41 54 45 20 61  .** The CREATE a
10f4f 6e 64 20 44 45 53 54 52 4f 59 20 70 72 69 6d 69  nd DESTROY primi
10f50 74 69 76 65 73 20 61 72 65 20 74 68 65 20 63 6f  tives are the co
10f51 6e 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64 65  nstructor and de
10f52 73 74 72 75 63 74 6f 72 2c 0a 2a 2a 20 6f 62 76  structor,.** obv
10f53 69 6f 75 73 6c 79 2e 20 20 54 68 65 20 49 4e 53  iously.  The INS
10f54 45 52 54 20 70 72 69 6d 69 74 69 76 65 20 61 64  ERT primitive ad
10f55 64 73 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74  ds a new element
10f56 20 74 6f 20 74 68 65 20 52 6f 77 53 65 74 2e 0a   to the RowSet..
10f57 2a 2a 20 54 45 53 54 20 63 68 65 63 6b 73 20 74  ** TEST checks t
10f58 6f 20 73 65 65 20 69 66 20 61 6e 20 65 6c 65 6d  o see if an elem
10f59 65 6e 74 20 69 73 20 61 6c 72 65 61 64 79 20 69  ent is already i
10f5a 6e 20 74 68 65 20 52 6f 77 53 65 74 2e 20 20 53  n the RowSet.  S
10f5b 4d 41 4c 4c 45 53 54 0a 2a 2a 20 65 78 74 72 61  MALLEST.** extra
10f5c 63 74 73 20 74 68 65 20 6c 65 61 73 74 20 76 61  cts the least va
10f5d 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 52 6f 77  lue from the Row
10f5e 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49  Set..**.** The I
10f5f 4e 53 45 52 54 20 70 72 69 6d 69 74 69 76 65 20  NSERT primitive 
10f60 6d 69 67 68 74 20 61 6c 6c 6f 63 61 74 65 20 61  might allocate a
10f61 64 64 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79  dditional memory
10f62 2e 20 20 4d 65 6d 6f 72 79 20 69 73 0a 2a 2a 20  .  Memory is.** 
10f63 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 63 68 75  allocated in chu
10f64 6e 6b 73 20 73 6f 20 6d 6f 73 74 20 49 4e 53 45  nks so most INSE
10f65 52 54 73 20 64 6f 20 6e 6f 20 61 6c 6c 6f 63 61  RTs do no alloca
10f66 74 69 6f 6e 2e 20 20 54 68 65 72 65 20 69 73 20  tion.  There is 
10f67 61 6e 20 0a 2a 2a 20 75 70 70 65 72 20 62 6f 75  an .** upper bou
10f68 6e 64 20 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f  nd on the size o
10f69 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f  f allocated memo
10f6a 72 79 2e 20 20 4e 6f 20 6d 65 6d 6f 72 79 20 69  ry.  No memory i
10f6b 73 20 66 72 65 65 64 0a 2a 2a 20 75 6e 74 69 6c  s freed.** until
10f6c 20 44 45 53 54 52 4f 59 2e 0a 2a 2a 0a 2a 2a 20   DESTROY..**.** 
10f6d 54 68 65 20 54 45 53 54 20 70 72 69 6d 69 74 69  The TEST primiti
10f6e 76 65 20 69 6e 63 6c 75 64 65 73 20 61 20 22 62  ve includes a "b
10f6f 61 74 63 68 22 20 6e 75 6d 62 65 72 2e 20 20 54  atch" number.  T
10f70 68 65 20 54 45 53 54 20 70 72 69 6d 69 74 69 76  he TEST primitiv
10f71 65 0a 2a 2a 20 77 69 6c 6c 20 6f 6e 6c 79 20 73  e.** will only s
10f72 65 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 61 74  ee elements that
10f73 20 77 65 72 65 20 69 6e 73 65 72 74 65 64 20 62   were inserted b
10f74 65 66 6f 72 65 20 74 68 65 20 6c 61 73 74 20 63  efore the last c
10f75 68 61 6e 67 65 0a 2a 2a 20 69 6e 20 74 68 65 20  hange.** in the 
10f76 62 61 74 63 68 20 6e 75 6d 62 65 72 2e 20 20 49  batch number.  I
10f77 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69  n other words, i
10f78 66 20 61 6e 20 49 4e 53 45 52 54 20 6f 63 63 75  f an INSERT occu
10f79 72 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 77  rs between.** tw
10f7a 6f 20 54 45 53 54 73 20 77 68 65 72 65 20 74 68  o TESTs where th
10f7b 65 20 54 45 53 54 73 20 68 61 76 65 20 74 68 65  e TESTs have the
10f7c 20 73 61 6d 65 20 62 61 74 63 68 20 6e 75 62 6d   same batch nubm
10f7d 65 72 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20  er, then the.** 
10f7e 76 61 6c 75 65 20 61 64 64 65 64 20 62 79 20 74  value added by t
10f7f 68 65 20 49 4e 53 45 52 54 20 77 69 6c 6c 20 6e  he INSERT will n
10f80 6f 74 20 62 65 20 76 69 73 69 62 6c 65 20 74 6f  ot be visible to
10f81 20 74 68 65 20 73 65 63 6f 6e 64 20 54 45 53 54   the second TEST
10f82 2e 0a 2a 2a 20 54 68 65 20 69 6e 69 74 69 61 6c  ..** The initial
10f83 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69 73   batch number is
10f84 20 7a 65 72 6f 2c 20 73 6f 20 69 66 20 74 68 65   zero, so if the
10f85 20 76 65 72 79 20 66 69 72 73 74 20 54 45 53 54   very first TEST
10f86 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 6e   contains.** a n
10f87 6f 6e 2d 7a 65 72 6f 20 62 61 74 63 68 20 6e 75  on-zero batch nu
10f88 6d 62 65 72 2c 20 69 74 20 77 69 6c 6c 20 73 65  mber, it will se
10f89 65 20 61 6c 6c 20 70 72 69 6f 72 20 49 4e 53 45  e all prior INSE
10f8a 52 54 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 49 4e  RTs..**.** No IN
10f8b 53 45 52 54 73 20 6d 61 79 20 6f 63 63 75 72 73  SERTs may occurs
10f8c 20 61 66 74 65 72 20 61 20 53 4d 41 4c 4c 45 53   after a SMALLES
10f8d 54 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e  T.  An assertion
10f8e 20 77 69 6c 6c 20 66 61 69 6c 20 69 66 0a 2a 2a   will fail if.**
10f8f 20 74 68 61 74 20 69 73 20 61 74 74 65 6d 70 74   that is attempt
10f90 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  ed..**.** The co
10f91 73 74 20 6f 66 20 61 6e 20 49 4e 53 45 52 54 20  st of an INSERT 
10f92 69 73 20 72 6f 75 67 68 6c 79 20 63 6f 6e 73 74  is roughly const
10f93 61 6e 74 2e 20 20 28 53 6f 6d 65 74 69 6d 65 20  ant.  (Sometime 
10f94 6e 65 77 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 61  new memory.** ha
10f95 73 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65  s to be allocate
10f96 64 20 6f 6e 20 61 6e 20 49 4e 53 45 52 54 2e 29  d on an INSERT.)
10f97 20 20 54 68 65 20 63 6f 73 74 20 6f 66 20 61 20    The cost of a 
10f98 54 45 53 54 20 77 69 74 68 20 61 20 6e 65 77 0a  TEST with a new.
10f99 2a 2a 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20  ** batch number 
10f9a 69 73 20 4f 28 4e 6c 6f 67 4e 29 20 77 68 65 72  is O(NlogN) wher
10f9b 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65  e N is the numbe
10f9c 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e  r of elements in
10f9d 20 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20   the RowSet..** 
10f9e 54 68 65 20 63 6f 73 74 20 6f 66 20 61 20 54 45  The cost of a TE
10f9f 53 54 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d  ST using the sam
10fa0 65 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69  e batch number i
10fa1 73 20 4f 28 6c 6f 67 4e 29 2e 20 20 54 68 65 20  s O(logN).  The 
10fa2 63 6f 73 74 0a 2a 2a 20 6f 66 20 74 68 65 20 66  cost.** of the f
10fa3 69 72 73 74 20 53 4d 41 4c 4c 45 53 54 20 69 73  irst SMALLEST is
10fa4 20 4f 28 4e 6c 6f 67 4e 29 2e 20 20 53 65 63 6f   O(NlogN).  Seco
10fa5 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e  nd and subsequen
10fa6 74 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20 70 72  t SMALLEST.** pr
10fa7 69 6d 69 74 69 76 65 73 20 61 72 65 20 63 6f 6e  imitives are con
10fa8 73 74 61 6e 74 20 74 69 6d 65 2e 20 20 54 68 65  stant time.  The
10fa9 20 63 6f 73 74 20 6f 66 20 44 45 53 54 52 4f 59   cost of DESTROY
10faa 20 69 73 20 4f 28 4e 29 2e 0a 2a 2a 0a 2a 2a 20   is O(N)..**.** 
10fab 54 68 65 72 65 20 69 73 20 61 6e 20 61 64 64 65  There is an adde
10fac 64 20 63 6f 73 74 20 6f 66 20 4f 28 4e 29 20 77  d cost of O(N) w
10fad 68 65 6e 20 73 77 69 74 63 68 69 6e 67 20 62 65  hen switching be
10fae 74 77 65 65 6e 20 54 45 53 54 20 61 6e 64 0a 2a  tween TEST and.*
10faf 2a 20 53 4d 41 4c 4c 45 53 54 20 70 72 69 6d 69  * SMALLEST primi
10fb0 74 69 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  tives..**.** $Id
10fb1 3a 20 72 6f 77 73 65 74 2e 63 2c 76 20 31 2e 37  : rowset.c,v 1.7
10fb2 20 32 30 30 39 2f 30 35 2f 32 32 20 30 31 3a 30   2009/05/22 01:0
10fb3 30 3a 31 33 20 64 72 68 20 45 78 70 20 24 0a 2a  0:13 drh Exp $.*
10fb4 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 61 72 67 65 74  /.../*.** Target
10fb5 20 73 69 7a 65 20 66 6f 72 20 61 6c 6c 6f 63 61   size for alloca
10fb6 74 69 6f 6e 20 63 68 75 6e 6b 73 2e 0a 2a 2f 0a  tion chunks..*/.
10fb7 23 64 65 66 69 6e 65 20 52 4f 57 53 45 54 5f 41  #define ROWSET_A
10fb8 4c 4c 4f 43 41 54 49 4f 4e 5f 53 49 5a 45 20 31  LLOCATION_SIZE 1
10fb9 30 32 34 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e  024../*.** The n
10fba 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 65 74 20  umber of rowset 
10fbb 65 6e 74 72 69 65 73 20 70 65 72 20 61 6c 6c 6f  entries per allo
10fbc 63 61 74 69 6f 6e 20 63 68 75 6e 6b 2e 0a 2a 2f  cation chunk..*/
10fbd 0a 23 64 65 66 69 6e 65 20 52 4f 57 53 45 54 5f  .#define ROWSET_
10fbe 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 20  ENTRY_PER_CHUNK 
10fbf 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   \.             
10fc0 20 20 20 20 20 20 20 20 20 20 28 28 52 4f 57 53            ((ROWS
10fc1 45 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e 5f 53 49  ET_ALLOCATION_SI
10fc2 5a 45 2d 38 29 2f 73 69 7a 65 6f 66 28 73 74 72  ZE-8)/sizeof(str
10fc3 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 29  uct RowSetEntry)
10fc4 29 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 65 6e  )../*.** Each en
10fc5 74 72 79 20 69 6e 20 61 20 52 6f 77 53 65 74 20  try in a RowSet 
10fc6 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
10fc7 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
10fc8 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 72 75 63  object..*/.struc
10fc9 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 7b 20  t RowSetEntry { 
10fca 20 20 20 20 20 20 20 20 20 20 20 0a 20 20 69 36             .  i6
10fcb 34 20 76 3b 20 20 20 20 20 20 20 20 20 20 20 20  4 v;            
10fcc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
10fcd 4f 57 49 44 20 76 61 6c 75 65 20 66 6f 72 20 74  OWID value for t
10fce 68 69 73 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73  his entry */.  s
10fcf 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
10fd0 79 20 2a 70 52 69 67 68 74 3b 20 20 20 2f 2a 20  y *pRight;   /* 
10fd1 52 69 67 68 74 20 73 75 62 74 72 65 65 20 28 6c  Right subtree (l
10fd2 61 72 67 65 72 20 65 6e 74 72 69 65 73 29 20 6f  arger entries) o
10fd3 72 20 6c 69 73 74 20 2a 2f 0a 20 20 73 74 72 75  r list */.  stru
10fd4 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
10fd5 70 4c 65 66 74 3b 20 20 20 20 2f 2a 20 4c 65 66  pLeft;    /* Lef
10fd6 74 20 73 75 62 74 72 65 65 20 28 73 6d 61 6c 6c  t subtree (small
10fd7 65 72 20 65 6e 74 72 69 65 73 29 20 2a 2f 0a 7d  er entries) */.}
10fd8 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 77 53 65 74 45  ;../*.** RowSetE
10fd9 6e 74 72 79 20 6f 62 6a 65 63 74 73 20 61 72 65  ntry objects are
10fda 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 6c 61   allocated in la
10fdb 72 67 65 20 63 68 75 6e 6b 73 20 28 69 6e 73 74  rge chunks (inst
10fdc 61 6e 63 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20  ances of the.** 
10fdd 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
10fde 75 72 65 29 20 74 6f 20 72 65 64 75 63 65 20 6d  ure) to reduce m
10fdf 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
10fe0 20 6f 76 65 72 68 65 61 64 2e 20 20 54 68 65 0a   overhead.  The.
10fe1 2a 2a 20 63 68 75 6e 6b 73 20 61 72 65 20 6b 65  ** chunks are ke
10fe2 70 74 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c  pt on a linked l
10fe3 69 73 74 20 73 6f 20 74 68 61 74 20 74 68 65 79  ist so that they
10fe4 20 63 61 6e 20 62 65 20 64 65 61 6c 6c 6f 63 61   can be dealloca
10fe5 74 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20  ted.** when the 
10fe6 52 6f 77 53 65 74 20 69 73 20 64 65 73 74 72 6f  RowSet is destro
10fe7 79 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52  yed..*/.struct R
10fe8 6f 77 53 65 74 43 68 75 6e 6b 20 7b 0a 20 20 73  owSetChunk {.  s
10fe9 74 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e  truct RowSetChun
10fea 6b 20 2a 70 4e 65 78 74 43 68 75 6e 6b 3b 20 20  k *pNextChunk;  
10feb 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68        /* Next ch
10fec 75 6e 6b 20 6f 6e 20 6c 69 73 74 20 6f 66 20 74  unk on list of t
10fed 68 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20 73 74 72  hem all */.  str
10fee 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
10fef 61 45 6e 74 72 79 5b 52 4f 57 53 45 54 5f 45 4e  aEntry[ROWSET_EN
10ff0 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 5d 3b 20  TRY_PER_CHUNK]; 
10ff1 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 65 6e 74  /* Allocated ent
10ff2 72 69 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  ries */.};../*.*
10ff3 2a 20 41 20 52 6f 77 53 65 74 20 69 6e 20 61 6e  * A RowSet in an
10ff4 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
10ff5 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
10ff6 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 79  ture..**.** A ty
10ff7 70 65 64 65 66 20 6f 66 20 74 68 69 73 20 73 74  pedef of this st
10ff8 72 75 63 74 75 72 65 20 69 66 20 66 6f 75 6e 64  ructure if found
10ff9 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e   in sqliteInt.h.
10ffa 0a 2a 2f 0a 73 74 72 75 63 74 20 52 6f 77 53 65  .*/.struct RowSe
10ffb 74 20 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77  t {.  struct Row
10ffc 53 65 74 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b  SetChunk *pChunk
10ffd 3b 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20  ;    /* List of 
10ffe 61 6c 6c 20 63 68 75 6e 6b 20 61 6c 6c 6f 63 61  all chunk alloca
10fff 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 71 6c 69 74  tions */.  sqlit
11000 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20  e3 *db;         
11001 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
11002 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
11003 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74  tion */.  struct
11004 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 45   RowSetEntry *pE
11005 6e 74 72 79 3b 20 20 20 20 2f 2a 20 4c 69 73 74  ntry;    /* List
11006 20 6f 66 20 65 6e 74 72 69 65 73 20 75 73 69 6e   of entries usin
11007 67 20 70 52 69 67 68 74 20 2a 2f 0a 20 20 73 74  g pRight */.  st
11008 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
11009 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20   *pLast;     /* 
1100a 4c 61 73 74 20 65 6e 74 72 79 20 6f 6e 20 74 68  Last entry on th
1100b 65 20 70 45 6e 74 72 79 20 6c 69 73 74 20 2a 2f  e pEntry list */
1100c 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
1100d 45 6e 74 72 79 20 2a 70 46 72 65 73 68 3b 20 20  Entry *pFresh;  
1100e 20 20 2f 2a 20 53 6f 75 72 63 65 20 6f 66 20 6e    /* Source of n
1100f 65 77 20 65 6e 74 72 79 20 6f 62 6a 65 63 74 73  ew entry objects
11010 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77   */.  struct Row
11011 53 65 74 45 6e 74 72 79 20 2a 70 54 72 65 65 3b  SetEntry *pTree;
11012 20 20 20 20 20 2f 2a 20 42 69 6e 61 72 79 20 74       /* Binary t
11013 72 65 65 20 6f 66 20 65 6e 74 72 69 65 73 20 2a  ree of entries *
11014 2f 0a 20 20 75 31 36 20 6e 46 72 65 73 68 3b 20  /.  u16 nFresh; 
11015 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11016 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
11017 6f 62 6a 65 63 74 73 20 6f 6e 20 70 46 72 65 73  objects on pFres
11018 68 20 2a 2f 0a 20 20 75 38 20 69 73 53 6f 72 74  h */.  u8 isSort
11019 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ed;             
1101a 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
1101b 20 70 45 6e 74 72 79 20 69 73 20 73 6f 72 74 65   pEntry is sorte
1101c 64 20 2a 2f 0a 20 20 75 38 20 69 42 61 74 63 68  d */.  u8 iBatch
1101d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1101e 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
1101f 20 69 6e 73 65 72 74 20 62 61 74 63 68 20 2a 2f   insert batch */
11020 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20  .};../*.** Turn 
11021 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f  bulk memory into
11022 20 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74   a RowSet object
11023 2e 20 20 4e 20 62 79 74 65 73 20 6f 66 20 6d 65  .  N bytes of me
11024 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61 76 61 69  mory.** are avai
11025 6c 61 62 6c 65 20 61 74 20 70 53 70 61 63 65 2e  lable at pSpace.
11026 20 20 54 68 65 20 64 62 20 70 6f 69 6e 74 65 72    The db pointer
11027 20 69 73 20 75 73 65 64 20 61 73 20 61 20 6d 65   is used as a me
11028 6d 6f 72 79 20 63 6f 6e 74 65 78 74 0a 2a 2a 20  mory context.** 
11029 66 6f 72 20 61 6e 79 20 73 75 62 73 65 71 75 65  for any subseque
1102a 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74  nt allocations t
1102b 68 61 74 20 6e 65 65 64 20 74 6f 20 6f 63 63 75  hat need to occu
1102c 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70  r..** Return a p
1102d 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65  ointer to the ne
1102e 77 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 2e  w RowSet object.
1102f 0a 2a 2a 0a 2a 2a 20 49 74 20 6d 75 73 74 20 62  .**.** It must b
11030 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20  e the case that 
11031 4e 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 20  N is sufficient 
11032 74 6f 20 6d 61 6b 65 20 61 20 52 6f 77 73 65 74  to make a Rowset
11033 2e 20 20 49 66 20 6e 6f 74 0a 2a 2a 20 61 6e 20  .  If not.** an 
11034 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20  assertion fault 
11035 6f 63 63 75 72 73 2e 0a 2a 2a 20 0a 2a 2a 20 49  occurs..** .** I
11036 66 20 4e 20 69 73 20 6c 61 72 67 65 72 20 74 68  f N is larger th
11037 61 6e 20 74 68 65 20 6d 69 6e 69 6d 75 6d 2c 20  an the minimum, 
11038 75 73 65 20 74 68 65 20 73 75 72 70 6c 75 73 20  use the surplus 
11039 61 73 20 61 6e 20 69 6e 69 74 69 61 6c 0a 2a 2a  as an initial.**
1103a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 65   allocation of e
1103b 6e 74 72 69 65 73 20 61 76 61 69 6c 61 62 6c 65  ntries available
1103c 20 74 6f 20 62 65 20 66 69 6c 6c 65 64 2e 0a 2a   to be filled..*
1103d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1103e 20 52 6f 77 53 65 74 20 2a 73 71 6c 69 74 65 33   RowSet *sqlite3
1103f 52 6f 77 53 65 74 49 6e 69 74 28 73 71 6c 69 74  RowSetInit(sqlit
11040 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 53  e3 *db, void *pS
11041 70 61 63 65 2c 20 75 6e 73 69 67 6e 65 64 20 69  pace, unsigned i
11042 6e 74 20 4e 29 7b 0a 20 20 52 6f 77 53 65 74 20  nt N){.  RowSet 
11043 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 4e 20  *p;.  assert( N 
11044 3e 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66  >= ROUND8(sizeof
11045 28 2a 70 29 29 20 29 3b 0a 20 20 70 20 3d 20 70  (*p)) );.  p = p
11046 53 70 61 63 65 3b 0a 20 20 70 2d 3e 70 43 68 75  Space;.  p->pChu
11047 6e 6b 20 3d 20 30 3b 0a 20 20 70 2d 3e 64 62 20  nk = 0;.  p->db 
11048 3d 20 64 62 3b 0a 20 20 70 2d 3e 70 45 6e 74 72  = db;.  p->pEntr
11049 79 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 4c 61 73  y = 0;.  p->pLas
1104a 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 54 72 65  t = 0;.  p->pTre
1104b 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 46 72 65  e = 0;.  p->pFre
1104c 73 68 20 3d 20 28 73 74 72 75 63 74 20 52 6f 77  sh = (struct Row
1104d 53 65 74 45 6e 74 72 79 2a 29 28 52 4f 55 4e 44  SetEntry*)(ROUND
1104e 38 28 73 69 7a 65 6f 66 28 2a 70 29 29 20 2b 20  8(sizeof(*p)) + 
1104f 28 63 68 61 72 2a 29 70 29 3b 0a 20 20 70 2d 3e  (char*)p);.  p->
11050 6e 46 72 65 73 68 20 3d 20 28 75 31 36 29 28 28  nFresh = (u16)((
11051 4e 20 2d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f  N - ROUND8(sizeo
11052 66 28 2a 70 29 29 29 2f 73 69 7a 65 6f 66 28 73  f(*p)))/sizeof(s
11053 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
11054 79 29 29 3b 0a 20 20 70 2d 3e 69 73 53 6f 72 74  y));.  p->isSort
11055 65 64 20 3d 20 31 3b 0a 20 20 70 2d 3e 69 42 61  ed = 1;.  p->iBa
11056 74 63 68 20 3d 20 30 3b 0a 20 20 72 65 74 75 72  tch = 0;.  retur
11057 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  n p;.}../*.** De
11058 61 6c 6c 6f 63 61 74 65 20 61 6c 6c 20 63 68 75  allocate all chu
11059 6e 6b 73 20 66 72 6f 6d 20 61 20 52 6f 77 53 65  nks from a RowSe
1105a 74 2e 20 20 54 68 69 73 20 66 72 65 65 73 20 61  t.  This frees a
1105b 6c 6c 20 6d 65 6d 6f 72 79 20 74 68 61 74 0a 2a  ll memory that.*
1105c 2a 20 74 68 65 20 52 6f 77 53 65 74 20 68 61 73  * the RowSet has
1105d 20 61 6c 6c 6f 63 61 74 65 64 20 6f 76 65 72 20   allocated over 
1105e 69 74 73 20 6c 69 66 65 74 69 6d 65 2e 20 20 54  its lifetime.  T
1105f 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a  his routine is.*
11060 2a 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72  * the destructor
11061 20 66 6f 72 20 74 68 65 20 52 6f 77 53 65 74 2e   for the RowSet.
11062 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
11063 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
11064 6f 77 53 65 74 43 6c 65 61 72 28 52 6f 77 53 65  owSetClear(RowSe
11065 74 20 2a 70 29 7b 0a 20 20 73 74 72 75 63 74 20  t *p){.  struct 
11066 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 43 68  RowSetChunk *pCh
11067 75 6e 6b 2c 20 2a 70 4e 65 78 74 43 68 75 6e 6b  unk, *pNextChunk
11068 3b 0a 20 20 66 6f 72 28 70 43 68 75 6e 6b 3d 70  ;.  for(pChunk=p
11069 2d 3e 70 43 68 75 6e 6b 3b 20 70 43 68 75 6e 6b  ->pChunk; pChunk
1106a 3b 20 70 43 68 75 6e 6b 20 3d 20 70 4e 65 78 74  ; pChunk = pNext
1106b 43 68 75 6e 6b 29 7b 0a 20 20 20 20 70 4e 65 78  Chunk){.    pNex
1106c 74 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 2d  tChunk = pChunk-
1106d 3e 70 4e 65 78 74 43 68 75 6e 6b 3b 0a 20 20 20  >pNextChunk;.   
1106e 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
1106f 2d 3e 64 62 2c 20 70 43 68 75 6e 6b 29 3b 0a 20  ->db, pChunk);. 
11070 20 7d 0a 20 20 70 2d 3e 70 43 68 75 6e 6b 20 3d   }.  p->pChunk =
11071 20 30 3b 0a 20 20 70 2d 3e 6e 46 72 65 73 68 20   0;.  p->nFresh 
11072 3d 20 30 3b 0a 20 20 70 2d 3e 70 45 6e 74 72 79  = 0;.  p->pEntry
11073 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 4c 61 73 74   = 0;.  p->pLast
11074 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 54 72 65 65   = 0;.  p->pTree
11075 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 53 6f 72   = 0;.  p->isSor
11076 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  ted = 1;.}../*.*
11077 2a 20 49 6e 73 65 72 74 20 61 20 6e 65 77 20 76  * Insert a new v
11078 61 6c 75 65 20 69 6e 74 6f 20 61 20 52 6f 77 53  alue into a RowS
11079 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61  et..**.** The ma
1107a 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20  llocFailed flag 
1107b 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1107c 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 73 65  connection is se
1107d 74 20 69 66 20 61 0a 2a 2a 20 6d 65 6d 6f 72 79  t if a.** memory
1107e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c   allocation fail
1107f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
11080 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
11081 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 52 6f  3RowSetInsert(Ro
11082 77 53 65 74 20 2a 70 2c 20 69 36 34 20 72 6f 77  wSet *p, i64 row
11083 69 64 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f  id){.  struct Ro
11084 77 53 65 74 45 6e 74 72 79 20 2a 70 45 6e 74 72  wSetEntry *pEntr
11085 79 3b 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 65  y;  /* The new e
11086 6e 74 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74  ntry */.  struct
11087 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c   RowSetEntry *pL
11088 61 73 74 3b 20 20 20 2f 2a 20 54 68 65 20 6c 61  ast;   /* The la
11089 73 74 20 70 72 69 6f 72 20 65 6e 74 72 79 20 2a  st prior entry *
1108a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30  /.  assert( p!=0
1108b 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 46 72   );.  if( p->nFr
1108c 65 73 68 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74  esh==0 ){.    st
1108d 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b  ruct RowSetChunk
1108e 20 2a 70 4e 65 77 3b 0a 20 20 20 20 70 4e 65 77   *pNew;.    pNew
1108f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
11090 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20 73 69 7a  ocRaw(p->db, siz
11091 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 20 20  eof(*pNew));.   
11092 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a   if( pNew==0 ){.
11093 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
11094 20 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4e    }.    pNew->pN
11095 65 78 74 43 68 75 6e 6b 20 3d 20 70 2d 3e 70 43  extChunk = p->pC
11096 68 75 6e 6b 3b 0a 20 20 20 20 70 2d 3e 70 43 68  hunk;.    p->pCh
11097 75 6e 6b 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20  unk = pNew;.    
11098 70 2d 3e 70 46 72 65 73 68 20 3d 20 70 4e 65 77  p->pFresh = pNew
11099 2d 3e 61 45 6e 74 72 79 3b 0a 20 20 20 20 70 2d  ->aEntry;.    p-
1109a 3e 6e 46 72 65 73 68 20 3d 20 52 4f 57 53 45 54  >nFresh = ROWSET
1109b 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b  _ENTRY_PER_CHUNK
1109c 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d  ;.  }.  pEntry =
1109d 20 70 2d 3e 70 46 72 65 73 68 2b 2b 3b 0a 20 20   p->pFresh++;.  
1109e 70 2d 3e 6e 46 72 65 73 68 2d 2d 3b 0a 20 20 70  p->nFresh--;.  p
1109f 45 6e 74 72 79 2d 3e 76 20 3d 20 72 6f 77 69 64  Entry->v = rowid
110a0 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 70 52 69 67  ;.  pEntry->pRig
110a1 68 74 20 3d 20 30 3b 0a 20 20 70 4c 61 73 74 20  ht = 0;.  pLast 
110a2 3d 20 70 2d 3e 70 4c 61 73 74 3b 0a 20 20 69 66  = p->pLast;.  if
110a3 28 20 70 4c 61 73 74 20 29 7b 0a 20 20 20 20 69  ( pLast ){.    i
110a4 66 28 20 70 2d 3e 69 73 53 6f 72 74 65 64 20 26  f( p->isSorted &
110a5 26 20 72 6f 77 69 64 3c 3d 70 4c 61 73 74 2d 3e  & rowid<=pLast->
110a6 76 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 73  v ){.      p->is
110a7 53 6f 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20  Sorted = 0;.    
110a8 7d 0a 20 20 20 20 70 4c 61 73 74 2d 3e 70 52 69  }.    pLast->pRi
110a9 67 68 74 20 3d 20 70 45 6e 74 72 79 3b 0a 20 20  ght = pEntry;.  
110aa 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
110ab 74 28 20 70 2d 3e 70 45 6e 74 72 79 3d 3d 30 20  t( p->pEntry==0 
110ac 29 3b 20 2f 2a 20 46 69 72 65 73 20 69 66 20 49  ); /* Fires if I
110ad 4e 53 45 52 54 20 61 66 74 65 72 20 53 4d 41 4c  NSERT after SMAL
110ae 4c 45 53 54 20 2a 2f 0a 20 20 20 20 70 2d 3e 70  LEST */.    p->p
110af 45 6e 74 72 79 20 3d 20 70 45 6e 74 72 79 3b 0a  Entry = pEntry;.
110b0 20 20 7d 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d    }.  p->pLast =
110b1 20 70 45 6e 74 72 79 3b 0a 7d 0a 0a 2f 2a 0a 2a   pEntry;.}../*.*
110b2 2a 20 4d 65 72 67 65 20 74 77 6f 20 6c 69 73 74  * Merge two list
110b3 73 20 6f 66 20 52 6f 77 53 65 74 45 6e 74 72 79  s of RowSetEntry
110b4 20 6f 62 6a 65 63 74 73 2e 20 20 52 65 6d 6f 76   objects.  Remov
110b5 65 20 64 75 70 6c 69 63 61 74 65 73 2e 0a 2a 2a  e duplicates..**
110b6 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 6c 69  .** The input li
110b7 73 74 73 20 61 72 65 20 63 6f 6e 6e 65 63 74 65  sts are connecte
110b8 64 20 76 69 61 20 70 52 69 67 68 74 20 70 6f 69  d via pRight poi
110b9 6e 74 65 72 73 20 61 6e 64 20 61 72 65 20 0a 2a  nters and are .*
110ba 2a 20 61 73 73 75 6d 65 64 20 74 6f 20 65 61 63  * assumed to eac
110bb 68 20 61 6c 72 65 61 64 79 20 62 65 20 69 6e 20  h already be in 
110bc 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 0a 2a 2f  sorted order..*/
110bd 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 52  .static struct R
110be 6f 77 53 65 74 45 6e 74 72 79 20 2a 72 6f 77 53  owSetEntry *rowS
110bf 65 74 4d 65 72 67 65 28 0a 20 20 73 74 72 75 63  etMerge(.  struc
110c0 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70  t RowSetEntry *p
110c1 41 2c 20 20 20 20 2f 2a 20 46 69 72 73 74 20 73  A,    /* First s
110c2 6f 72 74 65 64 20 6c 69 73 74 20 74 6f 20 62 65  orted list to be
110c3 20 6d 65 72 67 65 64 20 2a 2f 0a 20 20 73 74 72   merged */.  str
110c4 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
110c5 2a 70 42 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e  *pB     /* Secon
110c6 64 20 73 6f 72 74 65 64 20 6c 69 73 74 20 74 6f  d sorted list to
110c7 20 62 65 20 6d 65 72 67 65 64 20 2a 2f 0a 29 7b   be merged */.){
110c8 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
110c9 45 6e 74 72 79 20 68 65 61 64 3b 0a 20 20 73 74  Entry head;.  st
110ca 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
110cb 20 2a 70 54 61 69 6c 3b 0a 0a 20 20 70 54 61 69   *pTail;..  pTai
110cc 6c 20 3d 20 26 68 65 61 64 3b 0a 20 20 77 68 69  l = &head;.  whi
110cd 6c 65 28 20 70 41 20 26 26 20 70 42 20 29 7b 0a  le( pA && pB ){.
110ce 20 20 20 20 61 73 73 65 72 74 28 20 70 41 2d 3e      assert( pA->
110cf 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 41 2d  pRight==0 || pA-
110d0 3e 76 3c 3d 70 41 2d 3e 70 52 69 67 68 74 2d 3e  >v<=pA->pRight->
110d1 76 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  v );.    assert(
110d2 20 70 42 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c   pB->pRight==0 |
110d3 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70 52 69  | pB->v<=pB->pRi
110d4 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 69 66  ght->v );.    if
110d5 28 20 70 41 2d 3e 76 3c 70 42 2d 3e 76 20 29 7b  ( pA->v<pB->v ){
110d6 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 52  .      pTail->pR
110d7 69 67 68 74 20 3d 20 70 41 3b 0a 20 20 20 20 20  ight = pA;.     
110d8 20 70 41 20 3d 20 70 41 2d 3e 70 52 69 67 68 74   pA = pA->pRight
110d9 3b 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20  ;.      pTail = 
110da 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 3b 0a 20  pTail->pRight;. 
110db 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 2d     }else if( pB-
110dc 3e 76 3c 70 41 2d 3e 76 20 29 7b 0a 20 20 20 20  >v<pA->v ){.    
110dd 20 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20    pTail->pRight 
110de 3d 20 70 42 3b 0a 20 20 20 20 20 20 70 42 20 3d  = pB;.      pB =
110df 20 70 42 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20   pB->pRight;.   
110e0 20 20 20 70 54 61 69 6c 20 3d 20 70 54 61 69 6c     pTail = pTail
110e1 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 7d 65  ->pRight;.    }e
110e2 6c 73 65 7b 0a 20 20 20 20 20 20 70 41 20 3d 20  lse{.      pA = 
110e3 70 41 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20  pA->pRight;.    
110e4 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 41 20 29  }.  }.  if( pA )
110e5 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 41  {.    assert( pA
110e6 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70  ->pRight==0 || p
110e7 41 2d 3e 76 3c 3d 70 41 2d 3e 70 52 69 67 68 74  A->v<=pA->pRight
110e8 2d 3e 76 20 29 3b 0a 20 20 20 20 70 54 61 69 6c  ->v );.    pTail
110e9 2d 3e 70 52 69 67 68 74 20 3d 20 70 41 3b 0a 20  ->pRight = pA;. 
110ea 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
110eb 72 74 28 20 70 42 3d 3d 30 20 7c 7c 20 70 42 2d  rt( pB==0 || pB-
110ec 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 42  >pRight==0 || pB
110ed 2d 3e 76 3c 3d 70 42 2d 3e 70 52 69 67 68 74 2d  ->v<=pB->pRight-
110ee 3e 76 20 29 3b 0a 20 20 20 20 70 54 61 69 6c 2d  >v );.    pTail-
110ef 3e 70 52 69 67 68 74 20 3d 20 70 42 3b 0a 20 20  >pRight = pB;.  
110f0 7d 0a 20 20 72 65 74 75 72 6e 20 68 65 61 64 2e  }.  return head.
110f1 70 52 69 67 68 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pRight;.}../*.**
110f2 20 53 6f 72 74 20 61 6c 6c 20 65 6c 65 6d 65 6e   Sort all elemen
110f3 74 73 20 6f 6e 20 74 68 65 20 70 45 6e 74 72 79  ts on the pEntry
110f4 20 6c 69 73 74 20 6f 66 20 74 68 65 20 52 6f 77   list of the Row
110f5 53 65 74 20 69 6e 74 6f 20 61 73 63 65 6e 64 69  Set into ascendi
110f6 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 20 0a 73 74  ng order..*/ .st
110f7 61 74 69 63 20 76 6f 69 64 20 72 6f 77 53 65 74  atic void rowSet
110f8 53 6f 72 74 28 52 6f 77 53 65 74 20 2a 70 29 7b  Sort(RowSet *p){
110f9 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
110fa 69 3b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53  i;.  struct RowS
110fb 65 74 45 6e 74 72 79 20 2a 70 45 6e 74 72 79 3b  etEntry *pEntry;
110fc 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
110fd 45 6e 74 72 79 20 2a 61 42 75 63 6b 65 74 5b 34  Entry *aBucket[4
110fe 30 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  0];..  assert( p
110ff 2d 3e 69 73 53 6f 72 74 65 64 3d 3d 30 20 29 3b  ->isSorted==0 );
11100 0a 20 20 6d 65 6d 73 65 74 28 61 42 75 63 6b 65  .  memset(aBucke
11101 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 42 75  t, 0, sizeof(aBu
11102 63 6b 65 74 29 29 3b 0a 20 20 77 68 69 6c 65 28  cket));.  while(
11103 20 70 2d 3e 70 45 6e 74 72 79 20 29 7b 0a 20 20   p->pEntry ){.  
11104 20 20 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 45    pEntry = p->pE
11105 6e 74 72 79 3b 0a 20 20 20 20 70 2d 3e 70 45 6e  ntry;.    p->pEn
11106 74 72 79 20 3d 20 70 45 6e 74 72 79 2d 3e 70 52  try = pEntry->pR
11107 69 67 68 74 3b 0a 20 20 20 20 70 45 6e 74 72 79  ight;.    pEntry
11108 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20  ->pRight = 0;.  
11109 20 20 66 6f 72 28 69 3d 30 3b 20 61 42 75 63 6b    for(i=0; aBuck
1110a 65 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20  et[i]; i++){.   
1110b 20 20 20 70 45 6e 74 72 79 20 3d 20 72 6f 77 53     pEntry = rowS
1110c 65 74 4d 65 72 67 65 28 61 42 75 63 6b 65 74 5b  etMerge(aBucket[
1110d 69 5d 2c 20 70 45 6e 74 72 79 29 3b 0a 20 20 20  i], pEntry);.   
1110e 20 20 20 61 42 75 63 6b 65 74 5b 69 5d 20 3d 20     aBucket[i] = 
1110f 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 42 75  0;.    }.    aBu
11110 63 6b 65 74 5b 69 5d 20 3d 20 70 45 6e 74 72 79  cket[i] = pEntry
11111 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d  ;.  }.  pEntry =
11112 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   0;.  for(i=0; i
11113 3c 73 69 7a 65 6f 66 28 61 42 75 63 6b 65 74 29  <sizeof(aBucket)
11114 2f 73 69 7a 65 6f 66 28 61 42 75 63 6b 65 74 5b  /sizeof(aBucket[
11115 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70  0]); i++){.    p
11116 45 6e 74 72 79 20 3d 20 72 6f 77 53 65 74 4d 65  Entry = rowSetMe
11117 72 67 65 28 70 45 6e 74 72 79 2c 20 61 42 75 63  rge(pEntry, aBuc
11118 6b 65 74 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 70  ket[i]);.  }.  p
11119 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 72  ->pEntry = pEntr
1111a 79 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d 20  y;.  p->pLast = 
1111b 30 3b 0a 20 20 70 2d 3e 69 73 53 6f 72 74 65 64  0;.  p->isSorted
1111c 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20   = 1;.}.../*.** 
1111d 54 68 65 20 69 6e 70 75 74 2c 20 70 49 6e 2c 20  The input, pIn, 
1111e 69 73 20 61 20 62 69 6e 61 72 79 20 74 72 65 65  is a binary tree
1111f 20 28 6f 72 20 73 75 62 74 72 65 65 29 20 6f 66   (or subtree) of
11120 20 52 6f 77 53 65 74 45 6e 74 72 79 20 6f 62 6a   RowSetEntry obj
11121 65 63 74 73 2e 0a 2a 2a 20 43 6f 6e 76 65 72 74  ects..** Convert
11122 20 74 68 69 73 20 74 72 65 65 20 69 6e 74 6f 20   this tree into 
11123 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 63 6f  a linked list co
11124 6e 6e 65 63 74 65 64 20 62 79 20 74 68 65 20 70  nnected by the p
11125 52 69 67 68 74 20 70 6f 69 6e 74 65 72 73 0a 2a  Right pointers.*
11126 2a 20 61 6e 64 20 72 65 74 75 72 6e 20 70 6f 69  * and return poi
11127 6e 74 65 72 73 20 74 6f 20 74 68 65 20 66 69 72  nters to the fir
11128 73 74 20 61 6e 64 20 6c 61 73 74 20 65 6c 65 6d  st and last elem
11129 65 6e 74 73 20 6f 66 20 74 68 65 20 6e 65 77 20  ents of the new 
1112a 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  list..*/.static 
1112b 76 6f 69 64 20 72 6f 77 53 65 74 54 72 65 65 54  void rowSetTreeT
1112c 6f 4c 69 73 74 28 0a 20 20 73 74 72 75 63 74 20  oList(.  struct 
1112d 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 49 6e  RowSetEntry *pIn
1112e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f  ,         /* Roo
1112f 74 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 74  t of the input t
11130 72 65 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ree */.  struct 
11131 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a 70 70  RowSetEntry **pp
11132 46 69 72 73 74 2c 20 20 20 20 2f 2a 20 57 72 69  First,    /* Wri
11133 74 65 20 68 65 61 64 20 6f 66 20 74 68 65 20 6f  te head of the o
11134 75 74 70 75 74 20 6c 69 73 74 20 68 65 72 65 20  utput list here 
11135 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53  */.  struct RowS
11136 65 74 45 6e 74 72 79 20 2a 2a 70 70 4c 61 73 74  etEntry **ppLast
11137 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
11138 61 69 6c 20 6f 66 20 74 68 65 20 6f 75 74 70 75  ail of the outpu
11139 74 20 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a 29  t list here */.)
1113a 7b 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 21  {.  assert( pIn!
1113b 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 2d  =0 );.  if( pIn-
1113c 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 73 74  >pLeft ){.    st
1113d 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
1113e 20 2a 70 3b 0a 20 20 20 20 72 6f 77 53 65 74 54   *p;.    rowSetT
1113f 72 65 65 54 6f 4c 69 73 74 28 70 49 6e 2d 3e 70  reeToList(pIn->p
11140 4c 65 66 74 2c 20 70 70 46 69 72 73 74 2c 20 26  Left, ppFirst, &
11141 70 29 3b 0a 20 20 20 20 70 2d 3e 70 52 69 67 68  p);.    p->pRigh
11142 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 65 6c 73 65  t = pIn;.  }else
11143 7b 0a 20 20 20 20 2a 70 70 46 69 72 73 74 20 3d  {.    *ppFirst =
11144 20 70 49 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20   pIn;.  }.  if( 
11145 70 49 6e 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20  pIn->pRight ){. 
11146 20 20 20 72 6f 77 53 65 74 54 72 65 65 54 6f 4c     rowSetTreeToL
11147 69 73 74 28 70 49 6e 2d 3e 70 52 69 67 68 74 2c  ist(pIn->pRight,
11148 20 26 70 49 6e 2d 3e 70 52 69 67 68 74 2c 20 70   &pIn->pRight, p
11149 70 4c 61 73 74 29 3b 0a 20 20 7d 65 6c 73 65 7b  pLast);.  }else{
1114a 0a 20 20 20 20 2a 70 70 4c 61 73 74 20 3d 20 70  .    *ppLast = p
1114b 49 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  In;.  }.  assert
1114c 28 20 28 2a 70 70 4c 61 73 74 29 2d 3e 70 52 69  ( (*ppLast)->pRi
1114d 67 68 74 3d 3d 30 20 29 3b 0a 7d 0a 0a 0a 2f 2a  ght==0 );.}.../*
1114e 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 73 6f  .** Convert a so
1114f 72 74 65 64 20 6c 69 73 74 20 6f 66 20 65 6c 65  rted list of ele
11150 6d 65 6e 74 73 20 28 63 6f 6e 6e 65 63 74 65 64  ments (connected
11151 20 62 79 20 70 52 69 67 68 74 29 20 69 6e 74 6f   by pRight) into
11152 20 61 20 62 69 6e 61 72 79 0a 2a 2a 20 74 72 65   a binary.** tre
11153 65 20 77 69 74 68 20 64 65 70 74 68 20 6f 66 20  e with depth of 
11154 69 44 65 70 74 68 2e 20 20 41 20 64 65 70 74 68  iDepth.  A depth
11155 20 6f 66 20 31 20 6d 65 61 6e 73 20 74 68 65 20   of 1 means the 
11156 74 72 65 65 20 63 6f 6e 74 61 69 6e 73 20 61 20  tree contains a 
11157 73 69 6e 67 6c 65 0a 2a 2a 20 6e 6f 64 65 20 74  single.** node t
11158 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 68 65  aken from the he
11159 61 64 20 6f 66 20 2a 70 70 4c 69 73 74 2e 20 20  ad of *ppList.  
1115a 41 20 64 65 70 74 68 20 6f 66 20 32 20 6d 65 61  A depth of 2 mea
1115b 6e 73 20 61 20 74 72 65 65 20 77 69 74 68 0a 2a  ns a tree with.*
1115c 2a 20 74 68 72 65 65 20 6e 6f 64 65 73 2e 20 20  * three nodes.  
1115d 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a  And so forth..**
1115e 0a 2a 2a 20 55 73 65 20 61 73 20 6d 61 6e 79 20  .** Use as many 
1115f 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 68 65  entries from the
11160 20 69 6e 70 75 74 20 6c 69 73 74 20 61 73 20 72   input list as r
11161 65 71 75 69 72 65 64 20 61 6e 64 20 75 70 64 61  equired and upda
11162 74 65 20 74 68 65 0a 2a 2a 20 2a 70 70 4c 69 73  te the.** *ppLis
11163 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  t to point to th
11164 65 20 75 6e 75 73 65 64 20 65 6c 65 6d 65 6e 74  e unused element
11165 73 20 6f 66 20 74 68 65 20 6c 69 73 74 2e 20 20  s of the list.  
11166 49 66 20 74 68 65 20 69 6e 70 75 74 0a 2a 2a 20  If the input.** 
11167 6c 69 73 74 20 63 6f 6e 74 61 69 6e 73 20 74 6f  list contains to
11168 6f 20 66 65 77 20 65 6c 65 6d 65 6e 74 73 2c 20  o few elements, 
11169 74 68 65 6e 20 63 6f 6e 73 74 72 75 63 74 20 61  then construct a
1116a 6e 20 69 6e 63 6f 6d 70 6c 65 74 65 20 74 72 65  n incomplete tre
1116b 65 0a 2a 2a 20 61 6e 64 20 6c 65 61 76 65 20 2a  e.** and leave *
1116c 70 70 4c 69 73 74 20 73 65 74 20 74 6f 20 4e 55  ppList set to NU
1116d 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  LL..**.** Return
1116e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
1116f 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 63 6f  e root of the co
11170 6e 73 74 72 75 63 74 65 64 20 62 69 6e 61 72 79  nstructed binary
11171 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   tree..*/.static
11172 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e   struct RowSetEn
11173 74 72 79 20 2a 72 6f 77 53 65 74 4e 44 65 65 70  try *rowSetNDeep
11174 54 72 65 65 28 0a 20 20 73 74 72 75 63 74 20 52  Tree(.  struct R
11175 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a 70 70 4c  owSetEntry **ppL
11176 69 73 74 2c 0a 20 20 69 6e 74 20 69 44 65 70 74  ist,.  int iDept
11177 68 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f  h.){.  struct Ro
11178 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 20 20 20  wSetEntry *p;   
11179 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 6f 66        /* Root of
1117a 20 74 68 65 20 6e 65 77 20 74 72 65 65 20 2a 2f   the new tree */
1117b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
1117c 45 6e 74 72 79 20 2a 70 4c 65 66 74 3b 20 20 20  Entry *pLeft;   
1117d 20 20 2f 2a 20 4c 65 66 74 20 73 75 62 74 72 65    /* Left subtre
1117e 65 20 2a 2f 0a 20 20 69 66 28 20 2a 70 70 4c 69  e */.  if( *ppLi
1117f 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  st==0 ){.    ret
11180 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  urn 0;.  }.  if(
11181 20 69 44 65 70 74 68 3d 3d 31 20 29 7b 0a 20 20   iDepth==1 ){.  
11182 20 20 70 20 3d 20 2a 70 70 4c 69 73 74 3b 0a 20    p = *ppList;. 
11183 20 20 20 2a 70 70 4c 69 73 74 20 3d 20 70 2d 3e     *ppList = p->
11184 70 52 69 67 68 74 3b 0a 20 20 20 20 70 2d 3e 70  pRight;.    p->p
11185 4c 65 66 74 20 3d 20 70 2d 3e 70 52 69 67 68 74  Left = p->pRight
11186 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 0;.    return
11187 20 70 3b 0a 20 20 7d 0a 20 20 70 4c 65 66 74 20   p;.  }.  pLeft 
11188 3d 20 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65  = rowSetNDeepTre
11189 65 28 70 70 4c 69 73 74 2c 20 69 44 65 70 74 68  e(ppList, iDepth
1118a 2d 31 29 3b 0a 20 20 70 20 3d 20 2a 70 70 4c 69  -1);.  p = *ppLi
1118b 73 74 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29  st;.  if( p==0 )
1118c 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 4c 65  {.    return pLe
1118d 66 74 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4c 65  ft;.  }.  p->pLe
1118e 66 74 20 3d 20 70 4c 65 66 74 3b 0a 20 20 2a 70  ft = pLeft;.  *p
1118f 70 4c 69 73 74 20 3d 20 70 2d 3e 70 52 69 67 68  pList = p->pRigh
11190 74 3b 0a 20 20 70 2d 3e 70 52 69 67 68 74 20 3d  t;.  p->pRight =
11191 20 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65 65   rowSetNDeepTree
11192 28 70 70 4c 69 73 74 2c 20 69 44 65 70 74 68 2d  (ppList, iDepth-
11193 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a  1);.  return p;.
11194 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  }../*.** Convert
11195 20 61 20 73 6f 72 74 65 64 20 6c 69 73 74 20 6f   a sorted list o
11196 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 74 6f 20  f elements into 
11197 61 20 62 69 6e 61 72 79 20 74 72 65 65 2e 20 4d  a binary tree. M
11198 61 6b 65 20 74 68 65 20 74 72 65 65 0a 2a 2a 20  ake the tree.** 
11199 61 73 20 64 65 65 70 20 61 73 20 69 74 20 6e 65  as deep as it ne
1119a 65 64 73 20 74 6f 20 62 65 20 69 6e 20 6f 72 64  eds to be in ord
1119b 65 72 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68  er to contain th
1119c 65 20 65 6e 74 69 72 65 20 6c 69 73 74 2e 0a 2a  e entire list..*
1119d 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20  /.static struct 
1119e 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 72 6f 77  RowSetEntry *row
1119f 53 65 74 4c 69 73 74 54 6f 54 72 65 65 28 73 74  SetListToTree(st
111a0 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
111a1 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20   *pList){.  int 
111a2 69 44 65 70 74 68 3b 20 20 20 20 20 20 20 20 20  iDepth;         
111a3 20 20 2f 2a 20 44 65 70 74 68 20 6f 66 20 74 68    /* Depth of th
111a4 65 20 74 72 65 65 20 73 6f 20 66 61 72 20 2a 2f  e tree so far */
111a5 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
111a6 45 6e 74 72 79 20 2a 70 3b 20 20 20 20 20 20 20  Entry *p;       
111a7 2f 2a 20 43 75 72 72 65 6e 74 20 74 72 65 65 20  /* Current tree 
111a8 72 6f 6f 74 20 2a 2f 0a 20 20 73 74 72 75 63 74  root */.  struct
111a9 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c   RowSetEntry *pL
111aa 65 66 74 3b 20 20 20 2f 2a 20 4c 65 66 74 20 73  eft;   /* Left s
111ab 75 62 74 72 65 65 20 2a 2f 0a 0a 20 20 61 73 73  ubtree */..  ass
111ac 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29 3b  ert( pList!=0 );
111ad 0a 20 20 70 20 3d 20 70 4c 69 73 74 3b 0a 20 20  .  p = pList;.  
111ae 70 4c 69 73 74 20 3d 20 70 2d 3e 70 52 69 67 68  pList = p->pRigh
111af 74 3b 0a 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20  t;.  p->pLeft = 
111b0 70 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20  p->pRight = 0;. 
111b1 20 66 6f 72 28 69 44 65 70 74 68 3d 31 3b 20 70   for(iDepth=1; p
111b2 4c 69 73 74 3b 20 69 44 65 70 74 68 2b 2b 29 7b  List; iDepth++){
111b3 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 70 3b 0a  .    pLeft = p;.
111b4 20 20 20 20 70 20 3d 20 70 4c 69 73 74 3b 0a 20      p = pList;. 
111b5 20 20 20 70 4c 69 73 74 20 3d 20 70 2d 3e 70 52     pList = p->pR
111b6 69 67 68 74 3b 0a 20 20 20 20 70 2d 3e 70 4c 65  ight;.    p->pLe
111b7 66 74 20 3d 20 70 4c 65 66 74 3b 0a 20 20 20 20  ft = pLeft;.    
111b8 70 2d 3e 70 52 69 67 68 74 20 3d 20 72 6f 77 53  p->pRight = rowS
111b9 65 74 4e 44 65 65 70 54 72 65 65 28 26 70 4c 69  etNDeepTree(&pLi
111ba 73 74 2c 20 69 44 65 70 74 68 29 3b 0a 20 20 7d  st, iDepth);.  }
111bb 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
111bc 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68  /*.** Convert th
111bd 65 20 6c 69 73 74 20 69 6e 20 70 2d 3e 70 45 6e  e list in p->pEn
111be 74 72 79 20 69 6e 74 6f 20 61 20 73 6f 72 74 65  try into a sorte
111bf 64 20 6c 69 73 74 20 69 66 20 69 74 20 69 73 20  d list if it is 
111c0 6e 6f 74 0a 2a 2a 20 73 6f 72 74 65 64 20 61 6c  not.** sorted al
111c1 72 65 61 64 79 2e 20 20 49 66 20 74 68 65 72 65  ready.  If there
111c2 20 69 73 20 61 20 62 69 6e 61 72 79 20 74 72 65   is a binary tre
111c3 65 20 6f 6e 20 70 2d 3e 70 54 72 65 65 2c 20 74  e on p->pTree, t
111c4 68 65 6e 0a 2a 2a 20 63 6f 6e 76 65 72 74 20 69  hen.** convert i
111c5 74 20 69 6e 74 6f 20 61 20 6c 69 73 74 20 74 6f  t into a list to
111c6 6f 20 61 6e 64 20 6d 65 72 67 65 20 69 74 20 69  o and merge it i
111c7 6e 74 6f 20 74 68 65 20 70 2d 3e 70 45 6e 74 72  nto the p->pEntr
111c8 79 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69  y list..*/.stati
111c9 63 20 76 6f 69 64 20 72 6f 77 53 65 74 54 6f 4c  c void rowSetToL
111ca 69 73 74 28 52 6f 77 53 65 74 20 2a 70 29 7b 0a  ist(RowSet *p){.
111cb 20 20 69 66 28 20 21 70 2d 3e 69 73 53 6f 72 74    if( !p->isSort
111cc 65 64 20 29 7b 0a 20 20 20 20 72 6f 77 53 65 74  ed ){.    rowSet
111cd 53 6f 72 74 28 70 29 3b 0a 20 20 7d 0a 20 20 69  Sort(p);.  }.  i
111ce 66 28 20 70 2d 3e 70 54 72 65 65 20 29 7b 0a 20  f( p->pTree ){. 
111cf 20 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74     struct RowSet
111d0 45 6e 74 72 79 20 2a 70 48 65 61 64 2c 20 2a 70  Entry *pHead, *p
111d1 54 61 69 6c 3b 0a 20 20 20 20 72 6f 77 53 65 74  Tail;.    rowSet
111d2 54 72 65 65 54 6f 4c 69 73 74 28 70 2d 3e 70 54  TreeToList(p->pT
111d3 72 65 65 2c 20 26 70 48 65 61 64 2c 20 26 70 54  ree, &pHead, &pT
111d4 61 69 6c 29 3b 0a 20 20 20 20 70 2d 3e 70 54 72  ail);.    p->pTr
111d5 65 65 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70  ee = 0;.    p->p
111d6 45 6e 74 72 79 20 3d 20 72 6f 77 53 65 74 4d 65  Entry = rowSetMe
111d7 72 67 65 28 70 2d 3e 70 45 6e 74 72 79 2c 20 70  rge(p->pEntry, p
111d8 48 65 61 64 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  Head);.  }.}../*
111d9 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20  .** Extract the 
111da 73 6d 61 6c 6c 65 73 74 20 65 6c 65 6d 65 6e 74  smallest element
111db 20 66 72 6f 6d 20 74 68 65 20 52 6f 77 53 65 74   from the RowSet
111dc 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 65  ..** Write the e
111dd 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 2a 70 52 6f  lement into *pRo
111de 77 69 64 2e 20 20 52 65 74 75 72 6e 20 31 20 6f  wid.  Return 1 o
111df 6e 20 73 75 63 63 65 73 73 2e 20 20 52 65 74 75  n success.  Retu
111e0 72 6e 0a 2a 2a 20 30 20 69 66 20 74 68 65 20 52  rn.** 0 if the R
111e1 6f 77 53 65 74 20 69 73 20 61 6c 72 65 61 64 79  owSet is already
111e2 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 41 66   empty..**.** Af
111e3 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ter this routine
111e4 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64   has been called
111e5 2c 20 74 68 65 20 73 71 6c 69 74 65 33 52 6f 77  , the sqlite3Row
111e6 53 65 74 49 6e 73 65 72 74 28 29 0a 2a 2a 20 72  SetInsert().** r
111e7 6f 75 74 69 6e 65 20 6d 61 79 20 6e 6f 74 20 62  outine may not b
111e8 65 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 2e 20  e called again. 
111e9 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
111ea 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52  ATE int sqlite3R
111eb 6f 77 53 65 74 4e 65 78 74 28 52 6f 77 53 65 74  owSetNext(RowSet
111ec 20 2a 70 2c 20 69 36 34 20 2a 70 52 6f 77 69 64   *p, i64 *pRowid
111ed 29 7b 0a 20 20 72 6f 77 53 65 74 54 6f 4c 69 73  ){.  rowSetToLis
111ee 74 28 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70  t(p);.  if( p->p
111ef 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 2a 70 52  Entry ){.    *pR
111f0 6f 77 69 64 20 3d 20 70 2d 3e 70 45 6e 74 72 79  owid = p->pEntry
111f1 2d 3e 76 3b 0a 20 20 20 20 70 2d 3e 70 45 6e 74  ->v;.    p->pEnt
111f2 72 79 20 3d 20 70 2d 3e 70 45 6e 74 72 79 2d 3e  ry = p->pEntry->
111f3 70 52 69 67 68 74 3b 0a 20 20 20 20 69 66 28 20  pRight;.    if( 
111f4 70 2d 3e 70 45 6e 74 72 79 3d 3d 30 20 29 7b 0a  p->pEntry==0 ){.
111f5 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77        sqlite3Row
111f6 53 65 74 43 6c 65 61 72 28 70 29 3b 0a 20 20 20  SetClear(p);.   
111f7 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b   }.    return 1;
111f8 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
111f9 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f  turn 0;.  }.}../
111fa 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65  *.** Check to se
111fb 65 20 69 66 20 65 6c 65 6d 65 6e 74 20 69 52 6f  e if element iRo
111fc 77 69 64 20 77 61 73 20 69 6e 73 65 72 74 65 64  wid was inserted
111fd 20 69 6e 74 6f 20 74 68 65 20 74 68 65 20 72 6f   into the the ro
111fe 77 73 65 74 20 61 73 0a 2a 2a 20 70 61 72 74 20  wset as.** part 
111ff 6f 66 20 61 6e 79 20 69 6e 73 65 72 74 20 62 61  of any insert ba
11200 74 63 68 20 70 72 69 6f 72 20 74 6f 20 69 42 61  tch prior to iBa
11201 74 63 68 2e 20 20 52 65 74 75 72 6e 20 31 20 6f  tch.  Return 1 o
11202 72 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  r 0..*/.SQLITE_P
11203 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
11204 65 33 52 6f 77 53 65 74 54 65 73 74 28 52 6f 77  e3RowSetTest(Row
11205 53 65 74 20 2a 70 52 6f 77 53 65 74 2c 20 75 38  Set *pRowSet, u8
11206 20 69 42 61 74 63 68 2c 20 73 71 6c 69 74 65 33   iBatch, sqlite3
11207 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 29 7b 0a  _int64 iRowid){.
11208 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
11209 6e 74 72 79 20 2a 70 3b 0a 20 20 69 66 28 20 69  ntry *p;.  if( i
1120a 42 61 74 63 68 21 3d 70 52 6f 77 53 65 74 2d 3e  Batch!=pRowSet->
1120b 69 42 61 74 63 68 20 29 7b 0a 20 20 20 20 69 66  iBatch ){.    if
1120c 28 20 70 52 6f 77 53 65 74 2d 3e 70 45 6e 74 72  ( pRowSet->pEntr
1120d 79 20 29 7b 0a 20 20 20 20 20 20 72 6f 77 53 65  y ){.      rowSe
1120e 74 54 6f 4c 69 73 74 28 70 52 6f 77 53 65 74 29  tToList(pRowSet)
1120f 3b 0a 20 20 20 20 20 20 70 52 6f 77 53 65 74 2d  ;.      pRowSet-
11210 3e 70 54 72 65 65 20 3d 20 72 6f 77 53 65 74 4c  >pTree = rowSetL
11211 69 73 74 54 6f 54 72 65 65 28 70 52 6f 77 53 65  istToTree(pRowSe
11212 74 2d 3e 70 45 6e 74 72 79 29 3b 0a 20 20 20 20  t->pEntry);.    
11213 20 20 70 52 6f 77 53 65 74 2d 3e 70 45 6e 74 72    pRowSet->pEntr
11214 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 52 6f  y = 0;.      pRo
11215 77 53 65 74 2d 3e 70 4c 61 73 74 20 3d 20 30 3b  wSet->pLast = 0;
11216 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 6f 77 53  .    }.    pRowS
11217 65 74 2d 3e 69 42 61 74 63 68 20 3d 20 69 42 61  et->iBatch = iBa
11218 74 63 68 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 70  tch;.  }.  p = p
11219 52 6f 77 53 65 74 2d 3e 70 54 72 65 65 3b 0a 20  RowSet->pTree;. 
1121a 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20   while( p ){.   
1121b 20 69 66 28 20 70 2d 3e 76 3c 69 52 6f 77 69 64   if( p->v<iRowid
1121c 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 2d   ){.      p = p-
1121d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 7d 65 6c  >pRight;.    }el
1121e 73 65 20 69 66 28 20 70 2d 3e 76 3e 69 52 6f 77  se if( p->v>iRow
1121f 69 64 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20  id ){.      p = 
11220 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 7d 65  p->pLeft;.    }e
11221 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72  lse{.      retur
11222 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n 1;.    }.  }. 
11223 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
11224 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
11225 64 20 6f 66 20 72 6f 77 73 65 74 2e 63 20 2a 2a  d of rowset.c **
11226 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11227 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11228 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
11229 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
1122a 67 69 6e 20 66 69 6c 65 20 70 61 67 65 72 2e 63  gin file pager.c
1122b 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1122c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1122d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1122e 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
1122f 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
11230 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
11231 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
11232 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
11233 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
11234 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
11235 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
11236 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
11237 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
11238 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
11239 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
1123a 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
1123b 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
1123c 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
1123d 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
1123e 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
1123f 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
11240 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
11241 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11242 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11243 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11244 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11245 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ****.** This is 
11246 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
11247 6f 6e 20 6f 66 20 74 68 65 20 70 61 67 65 20 63  on of the page c
11248 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 6f  ache subsystem o
11249 72 20 22 70 61 67 65 72 22 2e 0a 2a 2a 20 0a 2a  r "pager"..** .*
1124a 2a 20 54 68 65 20 70 61 67 65 72 20 69 73 20 75  * The pager is u
1124b 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 61 20  sed to access a 
1124c 64 61 74 61 62 61 73 65 20 64 69 73 6b 20 66 69  database disk fi
1124d 6c 65 2e 20 20 49 74 20 69 6d 70 6c 65 6d 65 6e  le.  It implemen
1124e 74 73 0a 2a 2a 20 61 74 6f 6d 69 63 20 63 6f 6d  ts.** atomic com
1124f 6d 69 74 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b  mit and rollback
11250 20 74 68 72 6f 75 67 68 20 74 68 65 20 75 73 65   through the use
11251 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69   of a journal fi
11252 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 73 65  le that.** is se
11253 70 61 72 61 74 65 20 66 72 6f 6d 20 74 68 65 20  parate from the 
11254 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
11255 54 68 65 20 70 61 67 65 72 20 61 6c 73 6f 20 69  The pager also i
11256 6d 70 6c 65 6d 65 6e 74 73 20 66 69 6c 65 0a 2a  mplements file.*
11257 2a 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 70 72 65  * locking to pre
11258 76 65 6e 74 20 74 77 6f 20 70 72 6f 63 65 73 73  vent two process
11259 65 73 20 66 72 6f 6d 20 77 72 69 74 69 6e 67 20  es from writing 
1125a 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
1125b 65 0a 2a 2a 20 66 69 6c 65 20 73 69 6d 75 6c 74  e.** file simult
1125c 61 6e 65 6f 75 73 6c 79 2c 20 6f 72 20 6f 6e 65  aneously, or one
1125d 20 70 72 6f 63 65 73 73 20 66 72 6f 6d 20 72 65   process from re
1125e 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  ading the databa
1125f 73 65 20 77 68 69 6c 65 0a 2a 2a 20 61 6e 6f 74  se while.** anot
11260 68 65 72 20 69 73 20 77 72 69 74 69 6e 67 2e 0a  her is writing..
11261 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20  **.** @(#) $Id: 
11262 70 61 67 65 72 2e 63 2c 76 20 31 2e 36 32 39 20  pager.c,v 1.629 
11263 32 30 30 39 2f 30 38 2f 31 30 20 31 37 3a 34 38  2009/08/10 17:48
11264 3a 35 37 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :57 drh Exp $.*/
11265 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
11266 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 0a 2f 2a 0a  OMIT_DISKIO../*.
11267 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 74 72  ** Macros for tr
11268 6f 75 62 6c 65 73 68 6f 6f 74 69 6e 67 2e 20 20  oubleshooting.  
11269 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20  Normally turned 
1126a 6f 66 66 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e 74  off.*/.#if 0.int
1126b 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 61   sqlite3PagerTra
1126c 63 65 3d 31 3b 20 20 2f 2a 20 54 72 75 65 20 74  ce=1;  /* True t
1126d 6f 20 65 6e 61 62 6c 65 20 74 72 61 63 69 6e 67  o enable tracing
1126e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69   */.#define sqli
1126f 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 20 70  te3DebugPrintf p
11270 72 69 6e 74 66 0a 23 64 65 66 69 6e 65 20 50 41  rintf.#define PA
11271 47 45 52 54 52 41 43 45 28 58 29 20 20 20 20 20  GERTRACE(X)     
11272 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  if( sqlite3Pager
11273 54 72 61 63 65 20 29 7b 20 73 71 6c 69 74 65 33  Trace ){ sqlite3
11274 44 65 62 75 67 50 72 69 6e 74 66 20 58 3b 20 7d  DebugPrintf X; }
11275 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 50  .#else.#define P
11276 41 47 45 52 54 52 41 43 45 28 58 29 0a 23 65 6e  AGERTRACE(X).#en
11277 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  dif../*.** The f
11278 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 6d 61 63  ollowing two mac
11279 72 6f 73 20 61 72 65 20 75 73 65 64 20 77 69 74  ros are used wit
1127a 68 69 6e 20 74 68 65 20 50 41 47 45 52 54 52 41  hin the PAGERTRA
1127b 43 45 28 29 20 6d 61 63 72 6f 73 20 61 62 6f 76  CE() macros abov
1127c 65 0a 2a 2a 20 74 6f 20 70 72 69 6e 74 20 6f 75  e.** to print ou
1127d 74 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f  t file-descripto
1127e 72 73 2e 20 0a 2a 2a 0a 2a 2a 20 50 41 47 45 52  rs. .**.** PAGER
1127f 49 44 28 29 20 74 61 6b 65 73 20 61 20 70 6f 69  ID() takes a poi
11280 6e 74 65 72 20 74 6f 20 61 20 50 61 67 65 72 20  nter to a Pager 
11281 73 74 72 75 63 74 20 61 73 20 69 74 73 20 61 72  struct as its ar
11282 67 75 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20 61  gument. The.** a
11283 73 73 6f 63 69 61 74 65 64 20 66 69 6c 65 2d 64  ssociated file-d
11284 65 73 63 72 69 70 74 6f 72 20 69 73 20 72 65 74  escriptor is ret
11285 75 72 6e 65 64 2e 20 46 49 4c 45 48 41 4e 44 4c  urned. FILEHANDL
11286 45 49 44 28 29 20 74 61 6b 65 73 20 61 6e 20 73  EID() takes an s
11287 71 6c 69 74 65 33 5f 66 69 6c 65 0a 2a 2a 20 73  qlite3_file.** s
11288 74 72 75 63 74 20 61 73 20 69 74 73 20 61 72 67  truct as its arg
11289 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ument..*/.#defin
1128a 65 20 50 41 47 45 52 49 44 28 70 29 20 28 28 69  e PAGERID(p) ((i
1128b 6e 74 29 28 70 2d 3e 66 64 29 29 0a 23 64 65 66  nt)(p->fd)).#def
1128c 69 6e 65 20 46 49 4c 45 48 41 4e 44 4c 45 49 44  ine FILEHANDLEID
1128d 28 66 64 29 20 28 28 69 6e 74 29 66 64 29 0a 0a  (fd) ((int)fd)..
1128e 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 63  /*.** The page c
1128f 61 63 68 65 20 61 73 20 61 20 77 68 6f 6c 65 20  ache as a whole 
11290 69 73 20 61 6c 77 61 79 73 20 69 6e 20 6f 6e 65  is always in one
11291 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
11292 67 0a 2a 2a 20 73 74 61 74 65 73 3a 0a 2a 2a 0a  g.** states:.**.
11293 2a 2a 20 20 20 50 41 47 45 52 5f 55 4e 4c 4f 43  **   PAGER_UNLOC
11294 4b 20 20 20 20 20 20 20 20 54 68 65 20 70 61 67  K        The pag
11295 65 20 63 61 63 68 65 20 69 73 20 6e 6f 74 20 63  e cache is not c
11296 75 72 72 65 6e 74 6c 79 20 72 65 61 64 69 6e 67  urrently reading
11297 20 6f 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 20   or .**         
11298 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 72                wr
11299 69 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  iting the databa
1129a 73 65 20 66 69 6c 65 2e 20 20 54 68 65 72 65 20  se file.  There 
1129b 69 73 20 6e 6f 0a 2a 2a 20 20 20 20 20 20 20 20  is no.**        
1129c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64                 d
1129d 61 74 61 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f  ata held in memo
1129e 72 79 2e 20 20 54 68 69 73 20 69 73 20 74 68 65  ry.  This is the
1129f 20 69 6e 69 74 69 61 6c 0a 2a 2a 20 20 20 20 20   initial.**     
112a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
112a1 20 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20    state..**.**  
112a2 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 20 20   PAGER_SHARED   
112a3 20 20 20 20 20 54 68 65 20 70 61 67 65 20 63 61       The page ca
112a4 63 68 65 20 69 73 20 72 65 61 64 69 6e 67 20 74  che is reading t
112a5 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20  he database..** 
112a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
112a7 20 20 20 20 20 20 57 72 69 74 69 6e 67 20 69 73        Writing is
112a8 20 6e 6f 74 20 70 65 72 6d 69 74 74 65 64 2e 20   not permitted. 
112a9 20 54 68 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a   There can be.**
112aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
112ab 20 20 20 20 20 20 20 6d 75 6c 74 69 70 6c 65 20         multiple 
112ac 72 65 61 64 65 72 73 20 61 63 63 65 73 73 69 6e  readers accessin
112ad 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  g the same datab
112ae 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ase.**          
112af 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69 6c               fil
112b0 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69  e at the same ti
112b1 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45  me..**.**   PAGE
112b2 52 5f 52 45 53 45 52 56 45 44 20 20 20 20 20 20  R_RESERVED      
112b3 54 68 69 73 20 70 72 6f 63 65 73 73 20 68 61 73  This process has
112b4 20 72 65 73 65 72 76 65 64 20 74 68 65 20 64 61   reserved the da
112b5 74 61 62 61 73 65 20 66 6f 72 20 77 72 69 74 69  tabase for writi
112b6 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ng.**           
112b7 20 20 20 20 20 20 20 20 20 20 20 20 62 75 74 20              but 
112b8 68 61 73 20 6e 6f 74 20 79 65 74 20 6d 61 64 65  has not yet made
112b9 20 61 6e 79 20 63 68 61 6e 67 65 73 2e 20 20 4f   any changes.  O
112ba 6e 6c 79 20 6f 6e 65 20 70 72 6f 63 65 73 73 0a  nly one process.
112bb 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
112bc 20 20 20 20 20 20 20 20 20 61 74 20 61 20 74 69           at a ti
112bd 6d 65 20 63 61 6e 20 72 65 73 65 72 76 65 20 74  me can reserve t
112be 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68  he database.  Th
112bf 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 20 20  e original.**   
112c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
112c1 20 20 20 20 64 61 74 61 62 61 73 65 20 66 69 6c      database fil
112c2 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 6d  e has not been m
112c3 6f 64 69 66 69 65 64 20 73 6f 20 6f 74 68 65 72  odified so other
112c4 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
112c5 20 20 20 20 20 20 20 20 20 20 70 72 6f 63 65 73            proces
112c6 73 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62 65  ses may still be
112c7 20 72 65 61 64 69 6e 67 20 74 68 65 20 6f 6e 2d   reading the on-
112c8 64 69 73 6b 0a 2a 2a 20 20 20 20 20 20 20 20 20  disk.**         
112c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61                da
112ca 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a  tabase file..**.
112cb 2a 2a 20 20 20 50 41 47 45 52 5f 45 58 43 4c 55  **   PAGER_EXCLU
112cc 53 49 56 45 20 20 20 20 20 54 68 65 20 70 61 67  SIVE     The pag
112cd 65 20 63 61 63 68 65 20 69 73 20 77 72 69 74 69  e cache is writi
112ce 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  ng the database.
112cf 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
112d0 20 20 20 20 20 20 20 20 20 20 41 63 63 65 73 73            Access
112d1 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20   is exclusive.  
112d2 4e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  No other process
112d3 65 73 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20  es or.**        
112d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
112d5 68 72 65 61 64 73 20 63 61 6e 20 62 65 20 72 65  hreads can be re
112d6 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67  ading or writing
112d7 20 77 68 69 6c 65 20 6f 6e 65 0a 2a 2a 20 20 20   while one.**   
112d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
112d9 20 20 20 20 70 72 6f 63 65 73 73 20 69 73 20 77      process is w
112da 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20  riting..**.**   
112db 50 41 47 45 52 5f 53 59 4e 43 45 44 20 20 20 20  PAGER_SYNCED    
112dc 20 20 20 20 54 68 65 20 70 61 67 65 72 20 6d 6f      The pager mo
112dd 76 65 73 20 74 6f 20 74 68 69 73 20 73 74 61 74  ves to this stat
112de 65 20 66 72 6f 6d 20 50 41 47 45 52 5f 45 58 43  e from PAGER_EXC
112df 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 20 20 20  LUSIVE.**       
112e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
112e1 61 66 74 65 72 20 61 6c 6c 20 64 69 72 74 79 20  after all dirty 
112e2 70 61 67 65 73 20 68 61 76 65 20 62 65 65 6e 20  pages have been 
112e3 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a 2a  written to the.*
112e4 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
112e5 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65          database
112e6 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 66 69   file and the fi
112e7 6c 65 20 68 61 73 20 62 65 65 6e 20 73 79 6e 63  le has been sync
112e8 65 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20  ed to.**        
112e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64                 d
112ea 69 73 6b 2e 20 41 6c 6c 20 74 68 61 74 20 72 65  isk. All that re
112eb 6d 61 69 6e 73 20 74 6f 20 64 6f 20 69 73 20 74  mains to do is t
112ec 6f 20 72 65 6d 6f 76 65 20 6f 72 0a 2a 2a 20 20  o remove or.**  
112ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
112ee 20 20 20 20 20 74 72 75 6e 63 61 74 65 20 74 68       truncate th
112ef 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61  e journal file a
112f0 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  nd the transacti
112f1 6f 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  on .**          
112f2 20 20 20 20 20 20 20 20 20 20 20 20 20 77 69 6c               wil
112f3 6c 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 2e 0a  l be committed..
112f4 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 63  **.** The page c
112f5 61 63 68 65 20 63 6f 6d 65 73 20 75 70 20 69 6e  ache comes up in
112f6 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20 20   PAGER_UNLOCK.  
112f7 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 61  The first time a
112f8 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72  .** sqlite3Pager
112f9 47 65 74 28 29 20 6f 63 63 75 72 73 2c 20 74 68  Get() occurs, th
112fa 65 20 73 74 61 74 65 20 74 72 61 6e 73 69 74 69  e state transiti
112fb 6f 6e 73 20 74 6f 20 50 41 47 45 52 5f 53 48 41  ons to PAGER_SHA
112fc 52 45 44 2e 0a 2a 2a 20 41 66 74 65 72 20 61 6c  RED..** After al
112fd 6c 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65  l pages have bee
112fe 6e 20 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67  n released using
112ff 20 73 71 6c 69 74 65 5f 70 61 67 65 5f 75 6e 72   sqlite_page_unr
11300 65 66 28 29 2c 0a 2a 2a 20 74 68 65 20 73 74 61  ef(),.** the sta
11301 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 62  te transitions b
11302 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 55 4e 4c  ack to PAGER_UNL
11303 4f 43 4b 2e 20 20 54 68 65 20 66 69 72 73 74 20  OCK.  The first 
11304 74 69 6d 65 0a 2a 2a 20 74 68 61 74 20 73 71 6c  time.** that sql
11305 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29  ite3PagerWrite()
11306 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20   is called, the 
11307 73 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e  state transition
11308 73 20 74 6f 0a 2a 2a 20 50 41 47 45 52 5f 52 45  s to.** PAGER_RE
11309 53 45 52 56 45 44 2e 20 20 28 4e 6f 74 65 20 74  SERVED.  (Note t
1130a 68 61 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  hat sqlite3Pager
1130b 57 72 69 74 65 28 29 20 63 61 6e 20 6f 6e 6c 79  Write() can only
1130c 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 6f 6e   be.** called on
1130d 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   an outstanding 
1130e 70 61 67 65 20 77 68 69 63 68 20 6d 65 61 6e 73  page which means
1130f 20 74 68 61 74 20 74 68 65 20 70 61 67 65 72 20   that the pager 
11310 6d 75 73 74 0a 2a 2a 20 62 65 20 69 6e 20 50 41  must.** be in PA
11311 47 45 52 5f 53 48 41 52 45 44 20 62 65 66 6f 72  GER_SHARED befor
11312 65 20 69 74 20 74 72 61 6e 73 69 74 69 6f 6e 73  e it transitions
11313 20 74 6f 20 50 41 47 45 52 5f 52 45 53 45 52 56   to PAGER_RESERV
11314 45 44 2e 29 0a 2a 2a 20 50 41 47 45 52 5f 52 45  ED.).** PAGER_RE
11315 53 45 52 56 45 44 20 6d 65 61 6e 73 20 74 68 61  SERVED means tha
11316 74 20 74 68 65 72 65 20 69 73 20 61 6e 20 6f 70  t there is an op
11317 65 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  en rollback jour
11318 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 74 72 61 6e  nal..** The tran
11319 73 69 74 69 6f 6e 20 74 6f 20 50 41 47 45 52 5f  sition to PAGER_
1131a 45 58 43 4c 55 53 49 56 45 20 6f 63 63 75 72 73  EXCLUSIVE occurs
1131b 20 62 65 66 6f 72 65 20 61 6e 79 20 63 68 61 6e   before any chan
1131c 67 65 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20  ges.** are made 
1131d 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
1131e 66 69 6c 65 2c 20 74 68 6f 75 67 68 20 77 72 69  file, though wri
1131f 74 65 73 20 74 6f 20 74 68 65 20 72 6f 6c 6c 62  tes to the rollb
11320 61 63 6b 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6f  ack.** journal o
11321 63 63 75 72 73 20 77 69 74 68 20 6a 75 73 74 20  ccurs with just 
11322 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e 20  PAGER_RESERVED. 
11323 20 41 66 74 65 72 20 61 6e 20 73 71 6c 69 74 65   After an sqlite
11324 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29  3PagerRollback()
11325 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 50 61  .** or sqlite3Pa
11326 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77  gerCommitPhaseTw
11327 6f 28 29 2c 20 74 68 65 20 73 74 61 74 65 20 63  o(), the state c
11328 61 6e 20 67 6f 20 62 61 63 6b 20 74 6f 20 50 41  an go back to PA
11329 47 45 52 5f 53 48 41 52 45 44 2c 0a 2a 2a 20 6f  GER_SHARED,.** o
1132a 72 20 69 74 20 63 61 6e 20 73 74 61 79 20 61 74  r it can stay at
1132b 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45   PAGER_EXCLUSIVE
1132c 20 69 66 20 77 65 20 61 72 65 20 69 6e 20 65 78   if we are in ex
1132d 63 6c 75 73 69 76 65 20 61 63 63 65 73 73 20 6d  clusive access m
1132e 6f 64 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ode..*/.#define 
1132f 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20 20  PAGER_UNLOCK    
11330 20 20 30 0a 23 64 65 66 69 6e 65 20 50 41 47 45    0.#define PAGE
11331 52 5f 53 48 41 52 45 44 20 20 20 20 20 20 31 20  R_SHARED      1 
11332 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 53 48 41    /* same as SHA
11333 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66  RED_LOCK */.#def
11334 69 6e 65 20 50 41 47 45 52 5f 52 45 53 45 52 56  ine PAGER_RESERV
11335 45 44 20 20 20 20 32 20 20 20 2f 2a 20 73 61 6d  ED    2   /* sam
11336 65 20 61 73 20 52 45 53 45 52 56 45 44 5f 4c 4f  e as RESERVED_LO
11337 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41  CK */.#define PA
11338 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 20 20  GER_EXCLUSIVE   
11339 34 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 45  4   /* same as E
1133a 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 2a 2f  XCLUSIVE_LOCK */
1133b 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 53  .#define PAGER_S
1133c 59 4e 43 45 44 20 20 20 20 20 20 35 0a 0a 2f 2a  YNCED      5../*
1133d 0a 2a 2a 20 41 20 6d 61 63 72 6f 20 75 73 65 64  .** A macro used
1133e 20 66 6f 72 20 69 6e 76 6f 6b 69 6e 67 20 74 68   for invoking th
1133f 65 20 63 6f 64 65 63 20 69 66 20 74 68 65 72 65  e codec if there
11340 20 69 73 20 6f 6e 65 0a 2a 2f 0a 23 69 66 64 65   is one.*/.#ifde
11341 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44  f SQLITE_HAS_COD
11342 45 43 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45  EC.# define CODE
11343 43 31 28 50 2c 44 2c 4e 2c 58 2c 45 29 20 5c 0a  C1(P,D,N,X,E) \.
11344 20 20 20 20 69 66 28 20 50 2d 3e 78 43 6f 64 65      if( P->xCode
11345 63 20 26 26 20 50 2d 3e 78 43 6f 64 65 63 28 50  c && P->xCodec(P
11346 2d 3e 70 43 6f 64 65 63 2c 44 2c 4e 2c 58 29 3d  ->pCodec,D,N,X)=
11347 3d 30 20 29 7b 20 45 3b 20 7d 0a 23 20 64 65 66  =0 ){ E; }.# def
11348 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 2c 4e  ine CODEC2(P,D,N
11349 2c 58 2c 45 2c 4f 29 20 5c 0a 20 20 20 20 69 66  ,X,E,O) \.    if
1134a 28 20 50 2d 3e 78 43 6f 64 65 63 3d 3d 30 20 29  ( P->xCodec==0 )
1134b 7b 20 4f 3d 28 63 68 61 72 2a 29 44 3b 20 7d 65  { O=(char*)D; }e
1134c 6c 73 65 20 5c 0a 20 20 20 20 69 66 28 20 28 4f  lse \.    if( (O
1134d 3d 28 63 68 61 72 2a 29 28 50 2d 3e 78 43 6f 64  =(char*)(P->xCod
1134e 65 63 28 50 2d 3e 70 43 6f 64 65 63 2c 44 2c 4e  ec(P->pCodec,D,N
1134f 2c 58 29 29 29 3d 3d 30 20 29 7b 20 45 3b 20 7d  ,X)))==0 ){ E; }
11350 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
11351 43 4f 44 45 43 31 28 50 2c 44 2c 4e 2c 58 2c 45  CODEC1(P,D,N,X,E
11352 29 20 20 20 2f 2a 20 4e 4f 2d 4f 50 20 2a 2f 0a  )   /* NO-OP */.
11353 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 32 28  # define CODEC2(
11354 50 2c 44 2c 4e 2c 58 2c 45 2c 4f 29 20 4f 3d 28  P,D,N,X,E,O) O=(
11355 63 68 61 72 2a 29 44 0a 23 65 6e 64 69 66 0a 0a  char*)D.#endif..
11356 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75  /*.** The maximu
11357 6d 20 61 6c 6c 6f 77 65 64 20 73 65 63 74 6f 72  m allowed sector
11358 20 73 69 7a 65 2e 20 36 34 4b 69 42 2e 20 49 66   size. 64KiB. If
11359 20 74 68 65 20 78 53 65 63 74 6f 72 73 69 7a 65   the xSectorsize
1135a 28 29 20 6d 65 74 68 6f 64 20 0a 2a 2a 20 72 65  () method .** re
1135b 74 75 72 6e 73 20 61 20 76 61 6c 75 65 20 6c 61  turns a value la
1135c 72 67 65 72 20 74 68 61 6e 20 74 68 69 73 2c 20  rger than this, 
1135d 74 68 65 6e 20 4d 41 58 5f 53 45 43 54 4f 52 5f  then MAX_SECTOR_
1135e 53 49 5a 45 20 69 73 20 75 73 65 64 20 69 6e 73  SIZE is used ins
1135f 74 65 61 64 2e 0a 2a 2a 20 54 68 69 73 20 63 6f  tead..** This co
11360 75 6c 64 20 63 6f 6e 63 65 69 76 61 62 6c 79 20  uld conceivably 
11361 63 61 75 73 65 20 63 6f 72 72 75 70 74 69 6f 6e  cause corruption
11362 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 70 6f 77   following a pow
11363 65 72 20 66 61 69 6c 75 72 65 20 6f 6e 0a 2a 2a  er failure on.**
11364 20 73 75 63 68 20 61 20 73 79 73 74 65 6d 2e 20   such a system. 
11365 54 68 69 73 20 69 73 20 63 75 72 72 65 6e 74 6c  This is currentl
11366 79 20 61 6e 20 75 6e 64 6f 63 75 6d 65 6e 74 65  y an undocumente
11367 64 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 64 65 66  d limit..*/.#def
11368 69 6e 65 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53  ine MAX_SECTOR_S
11369 49 5a 45 20 30 78 31 30 30 30 30 0a 0a 2f 2a 0a  IZE 0x10000../*.
1136a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
1136b 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
1136c 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c  structure is all
1136d 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20  ocated for each 
1136e 61 63 74 69 76 65 0a 2a 2a 20 73 61 76 65 70 6f  active.** savepo
1136f 69 6e 74 20 61 6e 64 20 73 74 61 74 65 6d 65 6e  int and statemen
11370 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e  t transaction in
11371 20 74 68 65 20 73 79 73 74 65 6d 2e 20 41 6c 6c   the system. All
11372 20 73 75 63 68 20 73 74 72 75 63 74 75 72 65 73   such structures
11373 0a 2a 2a 20 61 72 65 20 73 74 6f 72 65 64 20 69  .** are stored i
11374 6e 20 74 68 65 20 50 61 67 65 72 2e 61 53 61 76  n the Pager.aSav
11375 65 70 6f 69 6e 74 5b 5d 20 61 72 72 61 79 2c 20  epoint[] array, 
11376 77 68 69 63 68 20 69 73 20 61 6c 6c 6f 63 61 74  which is allocat
11377 65 64 20 61 6e 64 0a 2a 2a 20 72 65 73 69 7a 65  ed and.** resize
11378 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 52  d using sqlite3R
11379 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20  ealloc()..**.** 
1137a 57 68 65 6e 20 61 20 73 61 76 65 70 6f 69 6e 74  When a savepoint
1137b 20 69 73 20 63 72 65 61 74 65 64 2c 20 74 68 65   is created, the
1137c 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e   PagerSavepoint.
1137d 69 48 64 72 4f 66 66 73 65 74 20 66 69 65 6c 64  iHdrOffset field
1137e 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 2e   is.** set to 0.
1137f 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 2d 68 65   If a journal-he
11380 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e 20  ader is written 
11381 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 6a 6f  into the main jo
11382 75 72 6e 61 6c 20 77 68 69 6c 65 0a 2a 2a 20 74  urnal while.** t
11383 68 65 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20  he savepoint is 
11384 61 63 74 69 76 65 2c 20 74 68 65 6e 20 69 48 64  active, then iHd
11385 72 4f 66 66 73 65 74 20 69 73 20 73 65 74 20 74  rOffset is set t
11386 6f 20 74 68 65 20 62 79 74 65 20 6f 66 66 73 65  o the byte offse
11387 74 20 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c  t .** immediatel
11388 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20  y following the 
11389 6c 61 73 74 20 6a 6f 75 72 6e 61 6c 20 72 65 63  last journal rec
1138a 6f 72 64 20 77 72 69 74 74 65 6e 20 69 6e 74 6f  ord written into
1138b 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f 75   the main.** jou
1138c 72 6e 61 6c 20 62 65 66 6f 72 65 20 74 68 65 20  rnal before the 
1138d 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 2e 20  journal-header. 
1138e 54 68 69 73 20 69 73 20 72 65 71 75 69 72 65 64  This is required
1138f 20 64 75 72 69 6e 67 20 73 61 76 65 70 6f 69 6e   during savepoin
11390 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 28 73  t.** rollback (s
11391 65 65 20 70 61 67 65 72 50 6c 61 79 62 61 63 6b  ee pagerPlayback
11392 53 61 76 65 70 6f 69 6e 74 28 29 29 2e 0a 2a 2f  Savepoint())..*/
11393 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
11394 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 50  PagerSavepoint P
11395 61 67 65 72 53 61 76 65 70 6f 69 6e 74 3b 0a 73  agerSavepoint;.s
11396 74 72 75 63 74 20 50 61 67 65 72 53 61 76 65 70  truct PagerSavep
11397 6f 69 6e 74 20 7b 0a 20 20 69 36 34 20 69 4f 66  oint {.  i64 iOf
11398 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20  fset;           
11399 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 69 6e        /* Startin
1139a 67 20 6f 66 66 73 65 74 20 69 6e 20 6d 61 69 6e  g offset in main
1139b 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36   journal */.  i6
1139c 34 20 69 48 64 72 4f 66 66 73 65 74 3b 20 20 20  4 iHdrOffset;   
1139d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65             /* Se
1139e 65 20 61 62 6f 76 65 20 2a 2f 0a 20 20 42 69 74  e above */.  Bit
1139f 76 65 63 20 2a 70 49 6e 53 61 76 65 70 6f 69 6e  vec *pInSavepoin
113a0 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74  t;        /* Set
113a1 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 69   of pages in thi
113a2 73 20 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 20  s savepoint */. 
113a3 20 50 67 6e 6f 20 6e 4f 72 69 67 3b 20 20 20 20   Pgno nOrig;    
113a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
113a5 20 4f 72 69 67 69 6e 61 6c 20 6e 75 6d 62 65 72   Original number
113a6 20 6f 66 20 70 61 67 65 73 20 69 6e 20 66 69 6c   of pages in fil
113a7 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 53 75 62  e */.  Pgno iSub
113a8 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  Rec;            
113a9 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
113aa 66 69 72 73 74 20 72 65 63 6f 72 64 20 69 6e 20  first record in 
113ab 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 7d  sub-journal */.}
113ac 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6f 70 65 6e 20  ;../*.** A open 
113ad 70 61 67 65 20 63 61 63 68 65 20 69 73 20 61 6e  page cache is an
113ae 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
113af 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
113b0 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 65 72 72 43  ture..**.** errC
113b1 6f 64 65 0a 2a 2a 0a 2a 2a 20 20 20 50 61 67 65  ode.**.**   Page
113b2 72 2e 65 72 72 43 6f 64 65 20 6d 61 79 20 62 65  r.errCode may be
113b3 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 49   set to SQLITE_I
113b4 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f 52  OERR, SQLITE_COR
113b5 52 55 50 54 2c 20 6f 72 0a 2a 2a 20 20 20 6f 72  RUPT, or.**   or
113b6 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e 20 4f 6e   SQLITE_FULL. On
113b7 63 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 69  ce one of the fi
113b8 72 73 74 20 74 68 72 65 65 20 65 72 72 6f 72 73  rst three errors
113b9 20 6f 63 63 75 72 73 2c 20 69 74 20 70 65 72 73   occurs, it pers
113ba 69 73 74 73 0a 2a 2a 20 20 20 61 6e 64 20 69 73  ists.**   and is
113bb 20 72 65 74 75 72 6e 65 64 20 61 73 20 74 68 65   returned as the
113bc 20 72 65 73 75 6c 74 20 6f 66 20 65 76 65 72 79   result of every
113bd 20 6d 61 6a 6f 72 20 70 61 67 65 72 20 41 50 49   major pager API
113be 20 63 61 6c 6c 2e 20 20 54 68 65 0a 2a 2a 20 20   call.  The.**  
113bf 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 72 65 74   SQLITE_FULL ret
113c0 75 72 6e 20 63 6f 64 65 20 69 73 20 73 6c 69 67  urn code is slig
113c1 68 74 6c 79 20 64 69 66 66 65 72 65 6e 74 2e 20  htly different. 
113c2 49 74 20 70 65 72 73 69 73 74 73 20 6f 6e 6c 79  It persists only
113c3 20 75 6e 74 69 6c 20 74 68 65 0a 2a 2a 20 20 20   until the.**   
113c4 6e 65 78 74 20 73 75 63 63 65 73 73 66 75 6c 20  next successful 
113c5 72 6f 6c 6c 62 61 63 6b 20 69 73 20 70 65 72 66  rollback is perf
113c6 6f 72 6d 65 64 20 6f 6e 20 74 68 65 20 70 61 67  ormed on the pag
113c7 65 72 20 63 61 63 68 65 2e 20 41 6c 73 6f 2c 0a  er cache. Also,.
113c8 2a 2a 20 20 20 53 51 4c 49 54 45 5f 46 55 4c 4c  **   SQLITE_FULL
113c9 20 64 6f 65 73 20 6e 6f 74 20 61 66 66 65 63 74   does not affect
113ca 20 74 68 65 20 73 71 6c 69 74 65 33 50 61 67 65   the sqlite3Page
113cb 72 47 65 74 28 29 20 61 6e 64 20 73 71 6c 69 74  rGet() and sqlit
113cc 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 29 0a  e3PagerLookup().
113cd 2a 2a 20 20 20 41 50 49 73 2c 20 74 68 65 79 20  **   APIs, they 
113ce 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 75 73 65  may still be use
113cf 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 0a  d successfully..
113d0 2a 2a 0a 2a 2a 20 64 62 53 69 7a 65 56 61 6c 69  **.** dbSizeVali
113d1 64 2c 20 64 62 53 69 7a 65 2c 20 64 62 4f 72 69  d, dbSize, dbOri
113d2 67 53 69 7a 65 2c 20 64 62 46 69 6c 65 53 69 7a  gSize, dbFileSiz
113d3 65 0a 2a 2a 0a 2a 2a 20 20 20 4d 61 6e 61 67 69  e.**.**   Managi
113d4 6e 67 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  ng the size of t
113d5 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
113d6 20 69 6e 20 70 61 67 65 73 20 69 73 20 61 20 6c   in pages is a l
113d7 69 74 74 6c 65 20 63 6f 6d 70 6c 69 63 61 74 65  ittle complicate
113d8 64 2e 0a 2a 2a 20 20 20 54 68 65 20 76 61 72 69  d..**   The vari
113d9 61 62 6c 65 20 50 61 67 65 72 2e 64 62 53 69 7a  able Pager.dbSiz
113da 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e  e contains the n
113db 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74  umber of pages t
113dc 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65  hat the database
113dd 0a 2a 2a 20 20 20 69 6d 61 67 65 20 63 75 72 72  .**   image curr
113de 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73 2e 20  ently contains. 
113df 41 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  As the database 
113e0 69 6d 61 67 65 20 67 72 6f 77 73 20 6f 72 20 73  image grows or s
113e1 68 72 69 6e 6b 73 20 74 68 69 73 0a 2a 2a 20 20  hrinks this.**  
113e2 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 70 64   variable is upd
113e3 61 74 65 64 2e 20 54 68 65 20 76 61 72 69 61 62  ated. The variab
113e4 6c 65 20 50 61 67 65 72 2e 64 62 46 69 6c 65 53  le Pager.dbFileS
113e5 69 7a 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ize contains the
113e6 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 6f 66 20   number.**   of 
113e7 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74  pages in the dat
113e8 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69 73  abase file. This
113e9 20 6d 61 79 20 62 65 20 64 69 66 66 65 72 65 6e   may be differen
113ea 74 20 66 72 6f 6d 20 50 61 67 65 72 2e 64 62 53  t from Pager.dbS
113eb 69 7a 65 0a 2a 2a 20 20 20 69 66 20 73 6f 6d 65  ize.**   if some
113ec 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65 6e   pages have been
113ed 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65   appended to the
113ee 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 20   database image 
113ef 62 75 74 20 6e 6f 74 20 79 65 74 20 77 72 69 74  but not yet writ
113f0 74 65 6e 0a 2a 2a 20 20 20 6f 75 74 20 66 72 6f  ten.**   out fro
113f1 6d 20 74 68 65 20 63 61 63 68 65 20 74 6f 20 74  m the cache to t
113f2 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 20 6f  he actual file o
113f3 6e 20 64 69 73 6b 2e 20 4f 72 20 69 66 20 74 68  n disk. Or if th
113f4 65 20 69 6d 61 67 65 20 68 61 73 20 62 65 65 6e  e image has been
113f5 0a 2a 2a 20 20 20 74 72 75 6e 63 61 74 65 64 20  .**   truncated 
113f6 62 79 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61  by an incrementa
113f7 6c 2d 76 61 63 75 75 6d 20 6f 70 65 72 61 74 69  l-vacuum operati
113f8 6f 6e 2e 20 54 68 65 20 50 61 67 65 72 2e 64 62  on. The Pager.db
113f9 4f 72 69 67 53 69 7a 65 20 76 61 72 69 61 62 6c  OrigSize variabl
113fa 65 0a 2a 2a 20 20 20 63 6f 6e 74 61 69 6e 73 20  e.**   contains 
113fb 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
113fc 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62  ges in the datab
113fd 61 73 65 20 69 6d 61 67 65 20 77 68 65 6e 20 74  ase image when t
113fe 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 20 20  he current.**   
113ff 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20  transaction was 
11400 6f 70 65 6e 65 64 2e 20 54 68 65 20 63 6f 6e 74  opened. The cont
11401 65 6e 74 73 20 6f 66 20 61 6c 6c 20 74 68 72 65  ents of all thre
11402 65 20 6f 66 20 74 68 65 73 65 20 76 61 72 69 61  e of these varia
11403 62 6c 65 73 20 69 73 0a 2a 2a 20 20 20 6f 6e 6c  bles is.**   onl
11404 79 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20  y guaranteed to 
11405 62 65 20 63 6f 72 72 65 63 74 20 69 66 20 74 68  be correct if th
11406 65 20 62 6f 6f 6c 65 61 6e 20 50 61 67 65 72 2e  e boolean Pager.
11407 64 62 53 69 7a 65 56 61 6c 69 64 20 69 73 20 74  dbSizeValid is t
11408 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 4f 44  rue..**.**   TOD
11409 4f 3a 20 55 6e 64 65 72 20 77 68 61 74 20 63 6f  O: Under what co
1140a 6e 64 69 74 69 6f 6e 73 20 69 73 20 64 62 53 69  nditions is dbSi
1140b 7a 65 56 61 6c 69 64 20 73 65 74 3f 20 43 6c 65  zeValid set? Cle
1140c 61 72 65 64 3f 0a 2a 2a 0a 2a 2a 20 63 68 61 6e  ared?.**.** chan
1140d 67 65 43 6f 75 6e 74 44 6f 6e 65 0a 2a 2a 0a 2a  geCountDone.**.*
1140e 2a 20 20 20 54 68 69 73 20 62 6f 6f 6c 65 61 6e  *   This boolean
1140f 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 65   variable is use
11410 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  d to make sure t
11411 68 61 74 20 74 68 65 20 63 68 61 6e 67 65 2d 63  hat the change-c
11412 6f 75 6e 74 65 72 20 0a 2a 2a 20 20 20 28 74 68  ounter .**   (th
11413 65 20 34 2d 62 79 74 65 20 68 65 61 64 65 72 20  e 4-byte header 
11414 66 69 65 6c 64 20 61 74 20 62 79 74 65 20 6f 66  field at byte of
11415 66 73 65 74 20 32 34 20 6f 66 20 74 68 65 20 64  fset 24 of the d
11416 61 74 61 62 61 73 65 20 66 69 6c 65 29 20 69 73  atabase file) is
11417 20 0a 2a 2a 20 20 20 6e 6f 74 20 75 70 64 61 74   .**   not updat
11418 65 64 20 6d 6f 72 65 20 6f 66 74 65 6e 20 74 68  ed more often th
11419 61 6e 20 6e 65 63 65 73 73 61 72 79 2e 20 0a 2a  an necessary. .*
1141a 2a 0a 2a 2a 20 20 20 49 74 20 69 73 20 73 65 74  *.**   It is set
1141b 20 74 6f 20 74 72 75 65 20 77 68 65 6e 20 74 68   to true when th
1141c 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72  e change-counter
1141d 20 66 69 65 6c 64 20 69 73 20 75 70 64 61 74 65   field is update
1141e 64 2c 20 77 68 69 63 68 20 0a 2a 2a 20 20 20 63  d, which .**   c
1141f 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69  an only happen i
11420 66 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  f an exclusive l
11421 6f 63 6b 20 69 73 20 68 65 6c 64 20 6f 6e 20 74  ock is held on t
11422 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
11423 2e 0a 2a 2a 20 20 20 49 74 20 69 73 20 63 6c 65  ..**   It is cle
11424 61 72 65 64 20 28 73 65 74 20 74 6f 20 66 61 6c  ared (set to fal
11425 73 65 29 20 77 68 65 6e 65 76 65 72 20 61 6e 20  se) whenever an 
11426 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69  exclusive lock i
11427 73 20 0a 2a 2a 20 20 20 72 65 6c 69 6e 71 75 69  s .**   relinqui
11428 73 68 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61  shed on the data
11429 62 61 73 65 20 66 69 6c 65 2e 20 45 61 63 68 20  base file. Each 
1142a 74 69 6d 65 20 61 20 74 72 61 6e 73 61 63 74 69  time a transacti
1142b 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 2c  on is committed,
1142c 0a 2a 2a 20 20 20 54 68 65 20 63 68 61 6e 67 65  .**   The change
1142d 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 20 69  CountDone flag i
1142e 73 20 69 6e 73 70 65 63 74 65 64 2e 20 49 66 20  s inspected. If 
1142f 69 74 20 69 73 20 74 72 75 65 2c 20 74 68 65 20  it is true, the 
11430 77 6f 72 6b 20 6f 66 0a 2a 2a 20 20 20 75 70 64  work of.**   upd
11431 61 74 69 6e 67 20 74 68 65 20 63 68 61 6e 67 65  ating the change
11432 2d 63 6f 75 6e 74 65 72 20 69 73 20 6f 6d 69 74  -counter is omit
11433 74 65 64 20 66 6f 72 20 74 68 65 20 63 75 72 72  ted for the curr
11434 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ent transaction.
11435 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 6d 65  .**.**   This me
11436 63 68 61 6e 69 73 6d 20 6d 65 61 6e 73 20 74 68  chanism means th
11437 61 74 20 77 68 65 6e 20 72 75 6e 6e 69 6e 67 20  at when running 
11438 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64  in exclusive mod
11439 65 2c 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  e, a connection 
1143a 0a 2a 2a 20 20 20 6e 65 65 64 20 6f 6e 6c 79 20  .**   need only 
1143b 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67  update the chang
1143c 65 2d 63 6f 75 6e 74 65 72 20 6f 6e 63 65 2c 20  e-counter once, 
1143d 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 72  for the first tr
1143e 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 63  ansaction.**   c
1143f 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  ommitted..**.** 
11440 64 62 4d 6f 64 69 66 69 65 64 0a 2a 2a 0a 2a 2a  dbModified.**.**
11441 20 20 20 54 68 65 20 64 62 4d 6f 64 69 66 69 65     The dbModifie
11442 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 77 68  d flag is set wh
11443 65 6e 65 76 65 72 20 61 20 64 61 74 61 62 61 73  enever a databas
11444 65 20 70 61 67 65 20 69 73 20 64 69 72 74 69 65  e page is dirtie
11445 64 2e 0a 2a 2a 20 20 20 49 74 20 69 73 20 63 6c  d..**   It is cl
11446 65 61 72 65 64 20 61 74 20 74 68 65 20 65 6e 64  eared at the end
11447 20 6f 66 20 65 61 63 68 20 74 72 61 6e 73 61 63   of each transac
11448 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 74  tion..**.**   It
11449 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 63 6f   is used when co
1144a 6d 6d 69 74 74 69 6e 67 20 6f 72 20 6f 74 68 65  mmitting or othe
1144b 72 77 69 73 65 20 65 6e 64 69 6e 67 20 61 20 74  rwise ending a t
1144c 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 0a 2a  ransaction. If.*
1144d 2a 20 20 20 74 68 65 20 64 62 4d 6f 64 69 66 69  *   the dbModifi
1144e 65 64 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72  ed flag is clear
1144f 20 74 68 65 6e 20 6c 65 73 73 20 77 6f 72 6b 20   then less work 
11450 68 61 73 20 74 6f 20 62 65 20 64 6f 6e 65 2e 0a  has to be done..
11451 2a 2a 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 53 74 61  **.** journalSta
11452 72 74 65 64 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69  rted.**.**   Thi
11453 73 20 66 6c 61 67 20 69 73 20 73 65 74 20 77 68  s flag is set wh
11454 65 6e 65 76 65 72 20 74 68 65 20 74 68 65 20 6d  enever the the m
11455 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73  ain journal is s
11456 79 6e 63 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 20 20  ynced. .**.**   
11457 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74 68 69  The point of thi
11458 73 20 66 6c 61 67 20 69 73 20 74 68 61 74 20 69  s flag is that i
11459 74 20 6d 75 73 74 20 62 65 20 73 65 74 20 61 66  t must be set af
1145a 74 65 72 20 74 68 65 20 0a 2a 2a 20 20 20 66 69  ter the .**   fi
1145b 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  rst journal head
1145c 65 72 20 69 6e 20 61 20 6a 6f 75 72 6e 61 6c 20  er in a journal 
1145d 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 73 79  file has been sy
1145e 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a  nced to disk..**
1145f 20 20 20 41 66 74 65 72 20 74 68 69 73 20 68 61     After this ha
11460 73 20 68 61 70 70 65 6e 65 64 2c 20 6e 65 77 20  s happened, new 
11461 70 61 67 65 73 20 61 70 70 65 6e 64 65 64 20 74  pages appended t
11462 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 0a  o the database .
11463 2a 2a 20 20 20 64 6f 20 6e 6f 74 20 6e 65 65 64  **   do not need
11464 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f   the PGHDR_NEED_
11465 53 59 4e 43 20 66 6c 61 67 20 73 65 74 2c 20 61  SYNC flag set, a
11466 73 20 74 68 65 79 20 64 6f 20 6e 6f 74 20 6e 65  s they do not ne
11467 65 64 0a 2a 2a 20 20 20 74 6f 20 77 61 69 74 20  ed.**   to wait 
11468 66 6f 72 20 61 20 6a 6f 75 72 6e 61 6c 20 73 79  for a journal sy
11469 6e 63 20 62 65 66 6f 72 65 20 74 68 65 79 20 63  nc before they c
1146a 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 6f 75  an be written ou
1146b 74 20 74 6f 0a 2a 2a 20 20 20 74 68 65 20 64 61  t to.**   the da
1146c 74 61 62 61 73 65 20 66 69 6c 65 20 28 73 65 65  tabase file (see
1146d 20 66 75 6e 63 74 69 6f 6e 20 70 61 67 65 72 5f   function pager_
1146e 77 72 69 74 65 28 29 29 2e 0a 2a 2a 20 20 20 0a  write())..**   .
1146f 2a 2a 20 73 65 74 4d 61 73 74 65 72 0a 2a 2a 0a  ** setMaster.**.
11470 2a 2a 20 20 20 54 68 69 73 20 76 61 72 69 61 62  **   This variab
11471 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 65 6e  le is used to en
11472 73 75 72 65 20 74 68 61 74 20 74 68 65 20 6d 61  sure that the ma
11473 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
11474 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 28 69 66 20  e name.**   (if 
11475 61 6e 79 29 20 69 73 20 6f 6e 6c 79 20 77 72 69  any) is only wri
11476 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f  tten into the jo
11477 75 72 6e 61 6c 20 66 69 6c 65 20 6f 6e 63 65 2e  urnal file once.
11478 0a 2a 2a 0a 2a 2a 20 20 20 57 68 65 6e 20 63 6f  .**.**   When co
11479 6d 6d 69 74 74 69 6e 67 20 61 20 74 72 61 6e 73  mmitting a trans
1147a 61 63 74 69 6f 6e 2c 20 74 68 65 20 6d 61 73 74  action, the mast
1147b 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
1147c 6e 61 6d 65 20 28 69 66 20 61 6e 79 29 0a 2a 2a  name (if any).**
1147d 20 20 20 6d 61 79 20 62 65 20 77 72 69 74 74 65     may be writte
1147e 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e  n into the journ
1147f 61 6c 20 66 69 6c 65 20 77 68 69 6c 65 20 74 68  al file while th
11480 65 20 70 61 67 65 72 20 69 73 20 73 74 69 6c 6c  e pager is still
11481 20 69 6e 0a 2a 2a 20 20 20 50 41 47 45 52 5f 52   in.**   PAGER_R
11482 45 53 45 52 56 45 44 20 73 74 61 74 65 20 28 73  ESERVED state (s
11483 65 65 20 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e  ee CommitPhaseOn
11484 65 28 29 20 66 6f 72 20 74 68 65 20 61 63 74 69  e() for the acti
11485 6f 6e 29 2e 20 49 74 0a 2a 2a 20 20 20 74 68 65  on). It.**   the
11486 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 75 70  n attempts to up
11487 67 72 61 64 65 20 74 6f 20 61 6e 20 65 78 63 6c  grade to an excl
11488 75 73 69 76 65 20 6c 6f 63 6b 2e 20 49 66 20 74  usive lock. If t
11489 68 69 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 20  his attempt.**  
1148a 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 53 51 4c   fails, then SQL
1148b 49 54 45 5f 42 55 53 59 20 6d 61 79 20 62 65 20  ITE_BUSY may be 
1148c 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20  returned to the 
1148d 75 73 65 72 20 61 6e 64 20 74 68 65 20 75 73 65  user and the use
1148e 72 0a 2a 2a 20 20 20 6d 61 79 20 61 74 74 65 6d  r.**   may attem
1148f 70 74 20 74 6f 20 63 6f 6d 6d 69 74 20 74 68 65  pt to commit the
11490 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 67 61   transaction aga
11491 69 6e 20 6c 61 74 65 72 20 28 63 61 6c 6c 69 6e  in later (callin
11492 67 0a 2a 2a 20 20 20 43 6f 6d 6d 69 74 50 68 61  g.**   CommitPha
11493 73 65 4f 6e 65 28 29 20 61 67 61 69 6e 29 2e 20  seOne() again). 
11494 54 68 69 73 20 66 6c 61 67 20 69 73 20 75 73 65  This flag is use
11495 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74  d to ensure that
11496 20 74 68 65 20 0a 2a 2a 20 20 20 6d 61 73 74 65   the .**   maste
11497 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69  r journal name i
11498 73 20 6f 6e 6c 79 20 77 72 69 74 74 65 6e 20 74  s only written t
11499 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  o the journal fi
1149a 6c 65 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20  le the first.** 
1149b 20 20 74 69 6d 65 20 43 6f 6d 6d 69 74 50 68 61    time CommitPha
1149c 73 65 4f 6e 65 28 29 20 69 73 20 63 61 6c 6c 65  seOne() is calle
1149d 64 2e 0a 2a 2a 0a 2a 2a 20 64 6f 4e 6f 74 53 79  d..**.** doNotSy
1149e 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20  nc.**.**   This 
1149f 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20  variable is set 
114a0 61 6e 64 20 63 6c 65 61 72 65 64 20 62 79 20 73  and cleared by s
114a1 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
114a2 28 29 2e 0a 2a 2a 0a 2a 2a 20 6e 65 65 64 53 79  ()..**.** needSy
114a3 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54 4f 44 4f 3a  nc.**.**   TODO:
114a4 20 49 74 20 6d 69 67 68 74 20 62 65 20 65 61 73   It might be eas
114a5 69 65 72 20 74 6f 20 73 65 74 20 74 68 69 73 20  ier to set this 
114a6 76 61 72 69 61 62 6c 65 20 69 6e 20 77 72 69 74  variable in writ
114a7 65 4a 6f 75 72 6e 61 6c 48 64 72 28 29 0a 2a 2a  eJournalHdr().**
114a8 20 20 20 61 6e 64 20 77 72 69 74 65 4d 61 73 74     and writeMast
114a9 65 72 4a 6f 75 72 6e 61 6c 28 29 20 6f 6e 6c 79  erJournal() only
114aa 2e 20 43 68 61 6e 67 65 20 69 74 73 20 6d 65 61  . Change its mea
114ab 6e 69 6e 67 20 74 6f 20 22 75 6e 73 79 6e 63 65  ning to "unsynce
114ac 64 20 64 61 74 61 0a 2a 2a 20 20 20 68 61 73 20  d data.**   has 
114ad 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20  been written to 
114ae 74 68 65 20 6a 6f 75 72 6e 61 6c 22 2e 0a 2a 2a  the journal"..**
114af 0a 2a 2a 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79  .** subjInMemory
114b0 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 69 73  .**.**   This is
114b1 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61 72 69 61   a boolean varia
114b2 62 6c 65 2e 20 49 66 20 74 72 75 65 2c 20 74 68  ble. If true, th
114b3 65 6e 20 61 6e 79 20 72 65 71 75 69 72 65 64 20  en any required 
114b4 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20  sub-journal.**  
114b5 20 69 73 20 6f 70 65 6e 65 64 20 61 73 20 61 6e   is opened as an
114b6 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e   in-memory journ
114b7 61 6c 20 66 69 6c 65 2e 20 49 66 20 66 61 6c 73  al file. If fals
114b8 65 2c 20 74 68 65 6e 20 69 6e 2d 6d 65 6d 6f 72  e, then in-memor
114b9 79 0a 2a 2a 20 20 20 73 75 62 2d 6a 6f 75 72 6e  y.**   sub-journ
114ba 61 6c 73 20 61 72 65 20 6f 6e 6c 79 20 75 73 65  als are only use
114bb 64 20 66 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20  d for in-memory 
114bc 70 61 67 65 72 20 66 69 6c 65 73 2e 0a 2a 2f 0a  pager files..*/.
114bd 73 74 72 75 63 74 20 50 61 67 65 72 20 7b 0a 20  struct Pager {. 
114be 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
114bf 66 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  fs;          /* 
114c0 4f 53 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20  OS functions to 
114c1 75 73 65 20 66 6f 72 20 49 4f 20 2a 2f 0a 20 20  use for IO */.  
114c2 75 38 20 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  u8 exclusiveMode
114c3 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42  ;           /* B
114c4 6f 6f 6c 65 61 6e 2e 20 54 72 75 65 20 69 66 20  oolean. True if 
114c5 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 3d 45 58  locking_mode==EX
114c6 43 4c 55 53 49 56 45 20 2a 2f 0a 20 20 75 38 20  CLUSIVE */.  u8 
114c7 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 20 20 20 20  journalMode;    
114c8 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 20 6f           /* On o
114c9 66 20 74 68 65 20 50 41 47 45 52 5f 4a 4f 55 52  f the PAGER_JOUR
114ca 4e 41 4c 4d 4f 44 45 5f 2a 20 76 61 6c 75 65 73  NALMODE_* values
114cb 20 2a 2f 0a 20 20 75 38 20 75 73 65 4a 6f 75 72   */.  u8 useJour
114cc 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  nal;            
114cd 20 20 2f 2a 20 55 73 65 20 61 20 72 6f 6c 6c 62    /* Use a rollb
114ce 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 6f 6e 20 74  ack journal on t
114cf 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38  his file */.  u8
114d0 20 6e 6f 52 65 61 64 6c 6f 63 6b 3b 20 20 20 20   noReadlock;    
114d1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20            /* Do 
114d2 6e 6f 74 20 62 6f 74 68 65 72 20 74 6f 20 6f 62  not bother to ob
114d3 74 61 69 6e 20 72 65 61 64 6c 6f 63 6b 73 20 2a  tain readlocks *
114d4 2f 0a 20 20 75 38 20 6e 6f 53 79 6e 63 3b 20 20  /.  u8 noSync;  
114d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
114d6 2f 2a 20 44 6f 20 6e 6f 74 20 73 79 6e 63 20 74  /* Do not sync t
114d7 68 65 20 6a 6f 75 72 6e 61 6c 20 69 66 20 74 72  he journal if tr
114d8 75 65 20 2a 2f 0a 20 20 75 38 20 66 75 6c 6c 53  ue */.  u8 fullS
114d9 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  ync;            
114da 20 20 20 20 2f 2a 20 44 6f 20 65 78 74 72 61 20      /* Do extra 
114db 73 79 6e 63 73 20 6f 66 20 74 68 65 20 6a 6f 75  syncs of the jou
114dc 72 6e 61 6c 20 66 6f 72 20 72 6f 62 75 73 74 6e  rnal for robustn
114dd 65 73 73 20 2a 2f 0a 20 20 75 38 20 73 79 6e 63  ess */.  u8 sync
114de 5f 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20  _flags;         
114df 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53       /* One of S
114e0 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f 72 20 53 59  YNC_NORMAL or SY
114e1 4e 43 5f 46 55 4c 4c 20 2a 2f 0a 20 20 75 38 20  NC_FULL */.  u8 
114e2 74 65 6d 70 46 69 6c 65 3b 20 20 20 20 20 20 20  tempFile;       
114e3 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 6c           /* zFil
114e4 65 6e 61 6d 65 20 69 73 20 61 20 74 65 6d 70 6f  ename is a tempo
114e5 72 61 72 79 20 66 69 6c 65 20 2a 2f 0a 20 20 75  rary file */.  u
114e6 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20  8 readOnly;     
114e7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
114e8 75 65 20 66 6f 72 20 61 20 72 65 61 64 2d 6f 6e  ue for a read-on
114e9 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  ly database */. 
114ea 20 75 38 20 6d 65 6d 44 62 3b 20 20 20 20 20 20   u8 memDb;      
114eb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
114ec 54 72 75 65 20 74 6f 20 69 6e 68 69 62 69 74 20  True to inhibit 
114ed 61 6c 6c 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a  all file I/O */.
114ee 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .  /* The follow
114ef 69 6e 67 20 62 6c 6f 63 6b 20 63 6f 6e 74 61 69  ing block contai
114f0 6e 73 20 74 68 6f 73 65 20 63 6c 61 73 73 20 6d  ns those class m
114f1 65 6d 62 65 72 73 20 74 68 61 74 20 61 72 65 20  embers that are 
114f2 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 20 20 2a 2a  dynamically.  **
114f3 20 6d 6f 64 69 66 69 65 64 20 64 75 72 69 6e 67   modified during
114f4 20 6e 6f 72 6d 61 6c 20 6f 70 65 72 61 74 69 6f   normal operatio
114f5 6e 73 2e 20 54 68 65 20 6f 74 68 65 72 20 76 61  ns. The other va
114f6 72 69 61 62 6c 65 73 20 69 6e 20 74 68 69 73 20  riables in this 
114f7 73 74 72 75 63 74 75 72 65 0a 20 20 2a 2a 20 61  structure.  ** a
114f8 72 65 20 65 69 74 68 65 72 20 63 6f 6e 73 74 61  re either consta
114f9 6e 74 20 74 68 72 6f 75 67 68 6f 75 74 20 74 68  nt throughout th
114fa 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68  e lifetime of th
114fb 65 20 70 61 67 65 72 2c 20 6f 72 20 65 6c 73 65  e pager, or else
114fc 0a 20 20 2a 2a 20 75 73 65 64 20 74 6f 20 73 74  .  ** used to st
114fd 6f 72 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  ore configuratio
114fe 6e 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 61  n parameters tha
114ff 74 20 61 66 66 65 63 74 20 74 68 65 20 77 61 79  t affect the way
11500 20 74 68 65 20 70 61 67 65 72 20 0a 20 20 2a 2a   the pager .  **
11501 20 6f 70 65 72 61 74 65 73 2e 0a 20 20 2a 2a 0a   operates..  **.
11502 20 20 2a 2a 20 54 68 65 20 27 73 74 61 74 65 27    ** The 'state'
11503 20 76 61 72 69 61 62 6c 65 20 69 73 20 64 65 73   variable is des
11504 63 72 69 62 65 64 20 69 6e 20 6d 6f 72 65 20 64  cribed in more d
11505 65 74 61 69 6c 20 61 6c 6f 6e 67 20 77 69 74 68  etail along with
11506 20 74 68 65 0a 20 20 2a 2a 20 64 65 73 63 72 69   the.  ** descri
11507 70 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 76 61  ptions of the va
11508 6c 75 65 73 20 69 74 20 6d 61 79 20 74 61 6b 65  lues it may take
11509 20 2d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20   - PAGER_UNLOCK 
1150a 65 74 63 2e 20 4d 61 6e 79 20 6f 66 20 74 68 65  etc. Many of the
1150b 0a 20 20 2a 2a 20 6f 74 68 65 72 20 76 61 72 69  .  ** other vari
1150c 61 62 6c 65 73 20 69 6e 20 74 68 69 73 20 62 6c  ables in this bl
1150d 6f 63 6b 20 61 72 65 20 64 65 73 63 72 69 62 65  ock are describe
1150e 64 20 69 6e 20 74 68 65 20 63 6f 6d 6d 65 6e 74  d in the comment
1150f 20 64 69 72 65 63 74 6c 79 20 0a 20 20 2a 2a 20   directly .  ** 
11510 61 62 6f 76 65 20 74 68 69 73 20 63 6c 61 73 73  above this class
11511 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 20 20 2a   definition..  *
11512 2f 0a 20 20 75 38 20 73 74 61 74 65 3b 20 20 20  /.  u8 state;   
11513 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11514 2f 2a 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2c  /* PAGER_UNLOCK,
11515 20 5f 53 48 41 52 45 44 2c 20 5f 52 45 53 45 52   _SHARED, _RESER
11516 56 45 44 2c 20 65 74 63 2e 20 2a 2f 0a 20 20 75  VED, etc. */.  u
11517 38 20 64 62 4d 6f 64 69 66 69 65 64 3b 20 20 20  8 dbModified;   
11518 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
11519 75 65 20 69 66 20 74 68 65 72 65 20 61 72 65 20  ue if there are 
1151a 61 6e 79 20 63 68 61 6e 67 65 73 20 74 6f 20 74  any changes to t
1151b 68 65 20 44 62 20 2a 2f 0a 20 20 75 38 20 6e 65  he Db */.  u8 ne
1151c 65 64 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20  edSync;         
1151d 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
1151e 66 20 61 6e 20 66 73 79 6e 63 28 29 20 69 73 20  f an fsync() is 
1151f 6e 65 65 64 65 64 20 6f 6e 20 74 68 65 20 6a 6f  needed on the jo
11520 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 6a 6f  urnal */.  u8 jo
11521 75 72 6e 61 6c 53 74 61 72 74 65 64 3b 20 20 20  urnalStarted;   
11522 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
11523 66 20 68 65 61 64 65 72 20 6f 66 20 6a 6f 75 72  f header of jour
11524 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 2a 2f  nal is synced */
11525 0a 20 20 75 38 20 63 68 61 6e 67 65 43 6f 75 6e  .  u8 changeCoun
11526 74 44 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 2f  tDone;         /
11527 2a 20 53 65 74 20 61 66 74 65 72 20 69 6e 63 72  * Set after incr
11528 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 63 68 61  ementing the cha
11529 6e 67 65 2d 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  nge-counter */. 
1152a 20 75 38 20 73 65 74 4d 61 73 74 65 72 3b 20 20   u8 setMaster;  
1152b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1152c 54 72 75 65 20 69 66 20 61 20 6d 2d 6a 20 6e 61  True if a m-j na
1152d 6d 65 20 68 61 73 20 62 65 65 6e 20 77 72 69 74  me has been writ
1152e 74 65 6e 20 74 6f 20 6a 72 6e 6c 20 2a 2f 0a 20  ten to jrnl */. 
1152f 20 75 38 20 64 6f 4e 6f 74 53 79 6e 63 3b 20 20   u8 doNotSync;  
11530 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11531 42 6f 6f 6c 65 61 6e 2e 20 57 68 69 6c 65 20 74  Boolean. While t
11532 72 75 65 2c 20 64 6f 20 6e 6f 74 20 73 70 69 6c  rue, do not spil
11533 6c 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 20  l the cache */. 
11534 20 75 38 20 64 62 53 69 7a 65 56 61 6c 69 64 3b   u8 dbSizeValid;
11535 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11536 53 65 74 20 77 68 65 6e 20 64 62 53 69 7a 65 20  Set when dbSize 
11537 69 73 20 63 6f 72 72 65 63 74 20 2a 2f 0a 20 20  is correct */.  
11538 75 38 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 3b  u8 subjInMemory;
11539 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1153a 72 75 65 20 74 6f 20 75 73 65 20 69 6e 2d 6d 65  rue to use in-me
1153b 6d 6f 72 79 20 73 75 62 2d 6a 6f 75 72 6e 61 6c  mory sub-journal
1153c 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 53 69  s */.  Pgno dbSi
1153d 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
1153e 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1153f 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74  pages in the dat
11540 61 62 61 73 65 20 2a 2f 0a 20 20 50 67 6e 6f 20  abase */.  Pgno 
11541 64 62 4f 72 69 67 53 69 7a 65 3b 20 20 20 20 20  dbOrigSize;     
11542 20 20 20 20 20 20 20 2f 2a 20 64 62 53 69 7a 65         /* dbSize
11543 20 62 65 66 6f 72 65 20 74 68 65 20 63 75 72 72   before the curr
11544 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent transaction 
11545 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 46 69 6c 65  */.  Pgno dbFile
11546 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20  Size;           
11547 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
11548 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62  ges in the datab
11549 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e  ase file */.  in
1154a 74 20 65 72 72 43 6f 64 65 3b 20 20 20 20 20 20  t errCode;      
1154b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
1154c 20 6f 66 20 73 65 76 65 72 61 6c 20 6b 69 6e 64   of several kind
1154d 73 20 6f 66 20 65 72 72 6f 72 73 20 2a 2f 0a 20  s of errors */. 
1154e 20 69 6e 74 20 6e 52 65 63 3b 20 20 20 20 20 20   int nRec;      
1154f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11550 50 61 67 65 73 20 6a 6f 75 72 6e 61 6c 6c 65 64  Pages journalled
11551 20 73 69 6e 63 65 20 6c 61 73 74 20 6a 2d 68 65   since last j-he
11552 61 64 65 72 20 77 72 69 74 74 65 6e 20 2a 2f 0a  ader written */.
11553 20 20 75 33 32 20 63 6b 73 75 6d 49 6e 69 74 3b    u32 cksumInit;
11554 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11555 20 51 75 61 73 69 2d 72 61 6e 64 6f 6d 20 76 61   Quasi-random va
11556 6c 75 65 20 61 64 64 65 64 20 74 6f 20 65 76 65  lue added to eve
11557 72 79 20 63 68 65 63 6b 73 75 6d 20 2a 2f 0a 20  ry checksum */. 
11558 20 75 33 32 20 6e 53 75 62 52 65 63 3b 20 20 20   u32 nSubRec;   
11559 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1155a 4e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 64  Number of record
1155b 73 20 77 72 69 74 74 65 6e 20 74 6f 20 73 75 62  s written to sub
1155c 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 42 69  -journal */.  Bi
1155d 74 76 65 63 20 2a 70 49 6e 4a 6f 75 72 6e 61 6c  tvec *pInJournal
1155e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65  ;         /* One
1155f 20 62 69 74 20 66 6f 72 20 65 61 63 68 20 70 61   bit for each pa
11560 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ge in the databa
11561 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c  se file */.  sql
11562 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 3b 20 20  ite3_file *fd;  
11563 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65           /* File
11564 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20   descriptor for 
11565 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 73 71  database */.  sq
11566 6c 69 74 65 33 5f 66 69 6c 65 20 2a 6a 66 64 3b  lite3_file *jfd;
11567 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c            /* Fil
11568 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72  e descriptor for
11569 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f   main journal */
1156a 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
1156b 2a 73 6a 66 64 3b 20 20 20 20 20 20 20 20 20 2f  *sjfd;         /
1156c 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f  * File descripto
1156d 72 20 66 6f 72 20 73 75 62 2d 6a 6f 75 72 6e 61  r for sub-journa
1156e 6c 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e  l */.  i64 journ
1156f 61 6c 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20  alOff;          
11570 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 77 72     /* Current wr
11571 69 74 65 20 6f 66 66 73 65 74 20 69 6e 20 74 68  ite offset in th
11572 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a  e journal file *
11573 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c 48  /.  i64 journalH
11574 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dr;             
11575 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20 74  /* Byte offset t
11576 6f 20 70 72 65 76 69 6f 75 73 20 6a 6f 75 72 6e  o previous journ
11577 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 50  al header */.  P
11578 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 61  agerSavepoint *a
11579 53 61 76 65 70 6f 69 6e 74 3b 20 2f 2a 20 41 72  Savepoint; /* Ar
1157a 72 61 79 20 6f 66 20 61 63 74 69 76 65 20 73 61  ray of active sa
1157b 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e  vepoints */.  in
1157c 74 20 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20 20  t nSavepoint;   
1157d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
1157e 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
1157f 69 6e 20 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20  in aSavepoint[] 
11580 2a 2f 0a 20 20 63 68 61 72 20 64 62 46 69 6c 65  */.  char dbFile
11581 56 65 72 73 5b 31 36 5d 3b 20 20 20 20 20 20 20  Vers[16];       
11582 20 2f 2a 20 43 68 61 6e 67 65 73 20 77 68 65 6e   /* Changes when
11583 65 76 65 72 20 64 61 74 61 62 61 73 65 20 66 69  ever database fi
11584 6c 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 20 20  le changes */.  
11585 75 33 32 20 73 65 63 74 6f 72 53 69 7a 65 3b 20  u32 sectorSize; 
11586 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
11587 73 73 75 6d 65 64 20 73 65 63 74 6f 72 20 73 69  ssumed sector si
11588 7a 65 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62 61  ze during rollba
11589 63 6b 20 2a 2f 0a 0a 20 20 75 31 36 20 6e 45 78  ck */..  u16 nEx
1158a 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 20  tra;            
1158b 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 69 73       /* Add this
1158c 20 6d 61 6e 79 20 62 79 74 65 73 20 74 6f 20 65   many bytes to e
1158d 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61  ach in-memory pa
1158e 67 65 20 2a 2f 0a 20 20 69 31 36 20 6e 52 65 73  ge */.  i16 nRes
1158f 65 72 76 65 3b 20 20 20 20 20 20 20 20 20 20 20  erve;           
11590 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
11591 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 61 74   unused bytes at
11592 20 65 6e 64 20 6f 66 20 65 61 63 68 20 70 61 67   end of each pag
11593 65 20 2a 2f 0a 20 20 75 33 32 20 76 66 73 46 6c  e */.  u32 vfsFl
11594 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
11595 20 20 20 2f 2a 20 46 6c 61 67 73 20 66 6f 72 20     /* Flags for 
11596 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65  sqlite3_vfs.xOpe
11597 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 70 61 67  n() */.  int pag
11598 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20  eSize;          
11599 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1159a 66 20 62 79 74 65 73 20 69 6e 20 61 20 70 61 67  f bytes in a pag
1159b 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 50 67  e */.  Pgno mxPg
1159c 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  no;             
1159d 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6c     /* Maximum al
1159e 6c 6f 77 65 64 20 73 69 7a 65 20 6f 66 20 74 68  lowed size of th
1159f 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  e database */.  
115a0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b  char *zFilename;
115a1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
115a2 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
115a3 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68  ase file */.  ch
115a4 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 20 20  ar *zJournal;   
115a5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
115a6 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e of the journal
115a7 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 28   file */.  int (
115a8 2a 78 42 75 73 79 48 61 6e 64 6c 65 72 29 28 76  *xBusyHandler)(v
115a9 6f 69 64 2a 29 3b 20 2f 2a 20 46 75 6e 63 74 69  oid*); /* Functi
115aa 6f 6e 20 74 6f 20 63 61 6c 6c 20 77 68 65 6e 20  on to call when 
115ab 62 75 73 79 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  busy */.  void *
115ac 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 3b  pBusyHandlerArg;
115ad 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74        /* Context
115ae 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 20 78 42   argument for xB
115af 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f 0a 23 69  usyHandler */.#i
115b0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
115b1 0a 20 20 69 6e 74 20 6e 48 69 74 2c 20 6e 4d 69  .  int nHit, nMi
115b2 73 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ss;            /
115b3 2a 20 43 61 63 68 65 20 68 69 74 73 20 61 6e 64  * Cache hits and
115b4 20 6d 69 73 73 69 6e 67 20 2a 2f 0a 20 20 69 6e   missing */.  in
115b5 74 20 6e 52 65 61 64 2c 20 6e 57 72 69 74 65 3b  t nRead, nWrite;
115b6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
115b7 61 62 61 73 65 20 70 61 67 65 73 20 72 65 61 64  abase pages read
115b8 2f 77 72 69 74 74 65 6e 20 2a 2f 0a 23 65 6e 64  /written */.#end
115b9 69 66 0a 20 20 76 6f 69 64 20 28 2a 78 52 65 69  if.  void (*xRei
115ba 6e 69 74 65 72 29 28 44 62 50 61 67 65 2a 29 3b  niter)(DbPage*);
115bb 20 2f 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f   /* Call this ro
115bc 75 74 69 6e 65 20 77 68 65 6e 20 72 65 6c 6f 61  utine when reloa
115bd 64 69 6e 67 20 70 61 67 65 73 20 2a 2f 0a 23 69  ding pages */.#i
115be 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f  fdef SQLITE_HAS_
115bf 43 4f 44 45 43 0a 20 20 76 6f 69 64 20 2a 28 2a  CODEC.  void *(*
115c0 78 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76 6f  xCodec)(void*,vo
115c1 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b 20 2f  id*,Pgno,int); /
115c2 2a 20 52 6f 75 74 69 6e 65 20 66 6f 72 20 65 6e  * Routine for en
115c3 2f 64 65 63 6f 64 69 6e 67 20 64 61 74 61 20 2a  /decoding data *
115c4 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f 64 65  /.  void (*xCode
115c5 63 53 69 7a 65 43 68 6e 67 29 28 76 6f 69 64 2a  cSizeChng)(void*
115c6 2c 69 6e 74 2c 69 6e 74 29 3b 20 2f 2a 20 4e 6f  ,int,int); /* No
115c7 74 69 66 79 20 6f 66 20 70 61 67 65 20 73 69 7a  tify of page siz
115c8 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 20 20 76  e changes */.  v
115c9 6f 69 64 20 28 2a 78 43 6f 64 65 63 46 72 65 65  oid (*xCodecFree
115ca 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 20  )(void*);       
115cb 20 20 20 20 20 20 2f 2a 20 44 65 73 74 72 75 63        /* Destruc
115cc 74 6f 72 20 66 6f 72 20 74 68 65 20 63 6f 64 65  tor for the code
115cd 63 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6f  c */.  void *pCo
115ce 64 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  dec;            
115cf 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75     /* First argu
115d0 6d 65 6e 74 20 74 6f 20 78 43 6f 64 65 63 2e 2e  ment to xCodec..
115d1 2e 20 6d 65 74 68 6f 64 73 20 2a 2f 0a 23 65 6e  . methods */.#en
115d2 64 69 66 0a 20 20 63 68 61 72 20 2a 70 54 6d 70  dif.  char *pTmp
115d3 53 70 61 63 65 3b 20 20 20 20 20 20 20 20 20 20  Space;          
115d4 20 20 2f 2a 20 50 61 67 65 72 2e 70 61 67 65 53    /* Pager.pageS
115d5 69 7a 65 20 62 79 74 65 73 20 6f 66 20 73 70 61  ize bytes of spa
115d6 63 65 20 66 6f 72 20 74 6d 70 20 75 73 65 20 2a  ce for tmp use *
115d7 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c 53  /.  i64 journalS
115d8 69 7a 65 4c 69 6d 69 74 3b 20 20 20 20 20 20 20  izeLimit;       
115d9 2f 2a 20 53 69 7a 65 20 6c 69 6d 69 74 20 66 6f  /* Size limit fo
115da 72 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75  r persistent jou
115db 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 20  rnal files */.  
115dc 50 43 61 63 68 65 20 2a 70 50 43 61 63 68 65 3b  PCache *pPCache;
115dd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
115de 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 65 20 63  ointer to page c
115df 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20  ache object */. 
115e0 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20   sqlite3_backup 
115e1 2a 70 42 61 63 6b 75 70 3b 20 20 20 20 2f 2a 20  *pBackup;    /* 
115e2 50 6f 69 6e 74 65 72 20 74 6f 20 6c 69 73 74 20  Pointer to list 
115e3 6f 66 20 6f 6e 67 6f 69 6e 67 20 62 61 63 6b 75  of ongoing backu
115e4 70 20 70 72 6f 63 65 73 73 65 73 20 2a 2f 0a 7d  p processes */.}
115e5 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  ;../*.** The fol
115e6 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61  lowing global va
115e7 72 69 61 62 6c 65 73 20 68 6f 6c 64 20 63 6f 75  riables hold cou
115e8 6e 74 65 72 73 20 75 73 65 64 20 66 6f 72 0a 2a  nters used for.*
115e9 2a 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73  * testing purpos
115ea 65 73 20 6f 6e 6c 79 2e 20 20 54 68 65 73 65 20  es only.  These 
115eb 76 61 72 69 61 62 6c 65 73 20 64 6f 20 6e 6f 74  variables do not
115ec 20 65 78 69 73 74 20 69 6e 0a 2a 2a 20 61 20 6e   exist in.** a n
115ed 6f 6e 2d 74 65 73 74 69 6e 67 20 62 75 69 6c 64  on-testing build
115ee 2e 20 20 54 68 65 73 65 20 76 61 72 69 61 62 6c  .  These variabl
115ef 65 73 20 61 72 65 20 6e 6f 74 20 74 68 72 65 61  es are not threa
115f0 64 2d 73 61 66 65 2e 0a 2a 2f 0a 23 69 66 64 65  d-safe..*/.#ifde
115f1 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
115f2 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
115f3 69 74 65 33 5f 70 61 67 65 72 5f 72 65 61 64 64  ite3_pager_readd
115f4 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 20  b_count = 0;    
115f5 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 6c  /* Number of ful
115f6 6c 20 70 61 67 65 73 20 72 65 61 64 20 66 72 6f  l pages read fro
115f7 6d 20 44 42 20 2a 2f 0a 53 51 4c 49 54 45 5f 41  m DB */.SQLITE_A
115f8 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70  PI int sqlite3_p
115f9 61 67 65 72 5f 77 72 69 74 65 64 62 5f 63 6f 75  ager_writedb_cou
115fa 6e 74 20 3d 20 30 3b 20 20 20 2f 2a 20 4e 75 6d  nt = 0;   /* Num
115fb 62 65 72 20 6f 66 20 66 75 6c 6c 20 70 61 67 65  ber of full page
115fc 73 20 77 72 69 74 74 65 6e 20 74 6f 20 44 42 20  s written to DB 
115fd 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
115fe 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f  t sqlite3_pager_
115ff 77 72 69 74 65 6a 5f 63 6f 75 6e 74 20 3d 20 30  writej_count = 0
11600 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f  ;    /* Number o
11601 66 20 70 61 67 65 73 20 77 72 69 74 74 65 6e 20  f pages written 
11602 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 20  to journal */.# 
11603 64 65 66 69 6e 65 20 50 41 47 45 52 5f 49 4e 43  define PAGER_INC
11604 52 28 76 29 20 20 76 2b 2b 0a 23 65 6c 73 65 0a  R(v)  v++.#else.
11605 23 20 64 65 66 69 6e 65 20 50 41 47 45 52 5f 49  # define PAGER_I
11606 4e 43 52 28 76 29 0a 23 65 6e 64 69 66 0a 0a 0a  NCR(v).#endif...
11607 0a 2f 2a 0a 2a 2a 20 4a 6f 75 72 6e 61 6c 20 66  ./*.** Journal f
11608 69 6c 65 73 20 62 65 67 69 6e 20 77 69 74 68 20  iles begin with 
11609 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61  the following ma
1160a 67 69 63 20 73 74 72 69 6e 67 2e 20 20 54 68 65  gic string.  The
1160b 20 64 61 74 61 0a 2a 2a 20 77 61 73 20 6f 62 74   data.** was obt
1160c 61 69 6e 65 64 20 66 72 6f 6d 20 2f 64 65 76 2f  ained from /dev/
1160d 72 61 6e 64 6f 6d 2e 20 20 49 74 20 69 73 20 75  random.  It is u
1160e 73 65 64 20 6f 6e 6c 79 20 61 73 20 61 20 73 61  sed only as a sa
1160f 6e 69 74 79 20 63 68 65 63 6b 2e 0a 2a 2a 0a 2a  nity check..**.*
11610 2a 20 53 69 6e 63 65 20 76 65 72 73 69 6f 6e 20  * Since version 
11611 32 2e 38 2e 30 2c 20 74 68 65 20 6a 6f 75 72 6e  2.8.0, the journ
11612 61 6c 20 66 6f 72 6d 61 74 20 63 6f 6e 74 61 69  al format contai
11613 6e 73 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 61  ns additional sa
11614 6e 69 74 79 0a 2a 2a 20 63 68 65 63 6b 69 6e 67  nity.** checking
11615 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 49   information.  I
11616 66 20 74 68 65 20 70 6f 77 65 72 20 66 61 69 6c  f the power fail
11617 73 20 77 68 69 6c 65 20 74 68 65 20 6a 6f 75 72  s while the jour
11618 6e 61 6c 20 69 73 20 62 65 69 6e 67 0a 2a 2a 20  nal is being.** 
11619 77 72 69 74 74 65 6e 2c 20 73 65 6d 69 2d 72 61  written, semi-ra
1161a 6e 64 6f 6d 20 67 61 72 62 61 67 65 20 64 61 74  ndom garbage dat
1161b 61 20 6d 69 67 68 74 20 61 70 70 65 61 72 20 69  a might appear i
1161c 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a  n the journal.**
1161d 20 66 69 6c 65 20 61 66 74 65 72 20 70 6f 77 65   file after powe
1161e 72 20 69 73 20 72 65 73 74 6f 72 65 64 2e 20 20  r is restored.  
1161f 49 66 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73  If an attempt is
11620 20 74 68 65 6e 20 6d 61 64 65 0a 2a 2a 20 74 6f   then made.** to
11621 20 72 6f 6c 6c 20 74 68 65 20 6a 6f 75 72 6e 61   roll the journa
11622 6c 20 62 61 63 6b 2c 20 74 68 65 20 64 61 74 61  l back, the data
11623 62 61 73 65 20 63 6f 75 6c 64 20 62 65 20 63 6f  base could be co
11624 72 72 75 70 74 65 64 2e 20 20 54 68 65 20 61 64  rrupted.  The ad
11625 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 73 61 6e 69  ditional.** sani
11626 74 79 20 63 68 65 63 6b 69 6e 67 20 64 61 74 61  ty checking data
11627 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74 20 74   is an attempt t
11628 6f 20 64 69 73 63 6f 76 65 72 20 74 68 65 20 67  o discover the g
11629 61 72 62 61 67 65 20 69 6e 20 74 68 65 0a 2a 2a  arbage in the.**
1162a 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 69 67 6e   journal and ign
1162b 6f 72 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  ore it..**.** Th
1162c 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e  e sanity checkin
1162d 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f  g information fo
1162e 72 20 74 68 65 20 6e 65 77 20 6a 6f 75 72 6e 61  r the new journa
1162f 6c 20 66 6f 72 6d 61 74 20 63 6f 6e 73 69 73 74  l format consist
11630 73 0a 2a 2a 20 6f 66 20 61 20 33 32 2d 62 69 74  s.** of a 32-bit
11631 20 63 68 65 63 6b 73 75 6d 20 6f 6e 20 65 61 63   checksum on eac
11632 68 20 70 61 67 65 20 6f 66 20 64 61 74 61 2e 20  h page of data. 
11633 20 54 68 65 20 63 68 65 63 6b 73 75 6d 20 63 6f   The checksum co
11634 76 65 72 73 20 62 6f 74 68 0a 2a 2a 20 74 68 65  vers both.** the
11635 20 70 61 67 65 20 6e 75 6d 62 65 72 20 61 6e 64   page number and
11636 20 74 68 65 20 70 50 61 67 65 72 2d 3e 70 61 67   the pPager->pag
11637 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 64  eSize bytes of d
11638 61 74 61 20 66 6f 72 20 74 68 65 20 70 61 67 65  ata for the page
11639 2e 0a 2a 2a 20 54 68 69 73 20 63 6b 73 75 6d 20  ..** This cksum 
1163a 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74  is initialized t
1163b 6f 20 61 20 33 32 2d 62 69 74 20 72 61 6e 64 6f  o a 32-bit rando
1163c 6d 20 76 61 6c 75 65 20 74 68 61 74 20 61 70 70  m value that app
1163d 65 61 72 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6a  ears in the.** j
1163e 6f 75 72 6e 61 6c 20 66 69 6c 65 20 72 69 67 68  ournal file righ
1163f 74 20 61 66 74 65 72 20 74 68 65 20 68 65 61 64  t after the head
11640 65 72 2e 20 20 54 68 65 20 72 61 6e 64 6f 6d 20  er.  The random 
11641 69 6e 69 74 69 61 6c 69 7a 65 72 20 69 73 20 69  initializer is i
11642 6d 70 6f 72 74 61 6e 74 2c 0a 2a 2a 20 62 65 63  mportant,.** bec
11643 61 75 73 65 20 67 61 72 62 61 67 65 20 64 61 74  ause garbage dat
11644 61 20 74 68 61 74 20 61 70 70 65 61 72 73 20 61  a that appears a
11645 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 6a  t the end of a j
11646 6f 75 72 6e 61 6c 20 69 73 20 6c 69 6b 65 6c 79  ournal is likely
11647 0a 2a 2a 20 64 61 74 61 20 74 68 61 74 20 77 61  .** data that wa
11648 73 20 6f 6e 63 65 20 69 6e 20 6f 74 68 65 72 20  s once in other 
11649 66 69 6c 65 73 20 74 68 61 74 20 68 61 76 65 20  files that have 
1164a 6e 6f 77 20 62 65 65 6e 20 64 65 6c 65 74 65 64  now been deleted
1164b 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 67 61 72  .  If the.** gar
1164c 62 61 67 65 20 64 61 74 61 20 63 61 6d 65 20 66  bage data came f
1164d 72 6f 6d 20 61 6e 20 6f 62 73 6f 6c 65 74 65 20  rom an obsolete 
1164e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68  journal file, th
1164f 65 20 63 68 65 63 6b 73 75 6d 73 20 6d 69 67 68  e checksums migh
11650 74 0a 2a 2a 20 62 65 20 63 6f 72 72 65 63 74 2e  t.** be correct.
11651 20 20 42 75 74 20 62 79 20 69 6e 69 74 69 61 6c    But by initial
11652 69 7a 69 6e 67 20 74 68 65 20 63 68 65 63 6b 73  izing the checks
11653 75 6d 20 74 6f 20 72 61 6e 64 6f 6d 20 76 61 6c  um to random val
11654 75 65 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 64  ue which.** is d
11655 69 66 66 65 72 65 6e 74 20 66 6f 72 20 65 76 65  ifferent for eve
11656 72 79 20 6a 6f 75 72 6e 61 6c 2c 20 77 65 20 6d  ry journal, we m
11657 69 6e 69 6d 69 7a 65 20 74 68 61 74 20 72 69 73  inimize that ris
11658 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  k..*/.static con
11659 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
1165a 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b 5d   aJournalMagic[]
1165b 20 3d 20 7b 0a 20 20 30 78 64 39 2c 20 30 78 64   = {.  0xd9, 0xd
1165c 35 2c 20 30 78 30 35 2c 20 30 78 66 39 2c 20 30  5, 0x05, 0xf9, 0
1165d 78 32 30 2c 20 30 78 61 31 2c 20 30 78 36 33 2c  x20, 0xa1, 0x63,
1165e 20 30 78 64 37 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a   0xd7,.};../*.**
1165f 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   The size of the
11660 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 72 65   of each page re
11661 63 6f 72 64 20 69 6e 20 74 68 65 20 6a 6f 75 72  cord in the jour
11662 6e 61 6c 20 69 73 20 67 69 76 65 6e 20 62 79 0a  nal is given by.
11663 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** the following
11664 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 64 65 66 69   macro..*/.#defi
11665 6e 65 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a  ne JOURNAL_PG_SZ
11666 28 70 50 61 67 65 72 29 20 20 28 28 70 50 61 67  (pPager)  ((pPag
11667 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 20 2b 20  er->pageSize) + 
11668 38 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f  8)../*.** The jo
11669 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73 69 7a  urnal header siz
1166a 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72  e for this pager
1166b 2e 20 54 68 69 73 20 69 73 20 75 73 75 61 6c 6c  . This is usuall
1166c 79 20 74 68 65 20 73 61 6d 65 20 0a 2a 2a 20 73  y the same .** s
1166d 69 7a 65 20 61 73 20 61 20 73 69 6e 67 6c 65 20  ize as a single 
1166e 64 69 73 6b 20 73 65 63 74 6f 72 2e 20 53 65 65  disk sector. See
1166f 20 61 6c 73 6f 20 73 65 74 53 65 63 74 6f 72 53   also setSectorS
11670 69 7a 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ize()..*/.#defin
11671 65 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  e JOURNAL_HDR_SZ
11672 28 70 50 61 67 65 72 29 20 28 70 50 61 67 65 72  (pPager) (pPager
11673 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 0a 0a 2f  ->sectorSize)../
11674 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f 20 4d  *.** The macro M
11675 45 4d 44 42 20 69 73 20 74 72 75 65 20 69 66 20  EMDB is true if 
11676 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77  we are dealing w
11677 69 74 68 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  ith an in-memory
11678 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 57 65   database..** We
11679 20 64 6f 20 74 68 69 73 20 61 73 20 61 20 6d 61   do this as a ma
1167a 63 72 6f 20 73 6f 20 74 68 61 74 20 69 66 20 74  cro so that if t
1167b 68 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d  he SQLITE_OMIT_M
1167c 45 4d 4f 52 59 44 42 20 6d 61 63 72 6f 20 69 73  EMORYDB macro is
1167d 20 73 65 74 2c 0a 2a 2a 20 74 68 65 20 76 61 6c   set,.** the val
1167e 75 65 20 6f 66 20 4d 45 4d 44 42 20 77 69 6c 6c  ue of MEMDB will
1167f 20 62 65 20 61 20 63 6f 6e 73 74 61 6e 74 20 61   be a constant a
11680 6e 64 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20  nd the compiler 
11681 77 69 6c 6c 20 6f 70 74 69 6d 69 7a 65 0a 2a 2a  will optimize.**
11682 20 6f 75 74 20 63 6f 64 65 20 74 68 61 74 20 77   out code that w
11683 6f 75 6c 64 20 6e 65 76 65 72 20 65 78 65 63 75  ould never execu
11684 74 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  te..*/.#ifdef SQ
11685 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59  LITE_OMIT_MEMORY
11686 44 42 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d 44  DB.# define MEMD
11687 42 20 30 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  B 0.#else.# defi
11688 6e 65 20 4d 45 4d 44 42 20 70 50 61 67 65 72 2d  ne MEMDB pPager-
11689 3e 6d 65 6d 44 62 0a 23 65 6e 64 69 66 0a 0a 2f  >memDb.#endif../
1168a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d  *.** The maximum
1168b 20 6c 65 67 61 6c 20 70 61 67 65 20 6e 75 6d 62   legal page numb
1168c 65 72 20 69 73 20 28 32 5e 33 31 20 2d 20 31 29  er is (2^31 - 1)
1168d 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47  ..*/.#define PAG
1168e 45 52 5f 4d 41 58 5f 50 47 4e 4f 20 32 31 34 37  ER_MAX_PGNO 2147
1168f 34 38 33 36 34 37 0a 0a 23 69 66 6e 64 65 66 20  483647..#ifndef 
11690 4e 44 45 42 55 47 20 0a 2f 2a 0a 2a 2a 20 55 73  NDEBUG ./*.** Us
11691 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 73 73  age:.**.**   ass
11692 65 72 74 28 20 61 73 73 65 72 74 5f 70 61 67 65  ert( assert_page
11693 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 29 20  r_state(pPager) 
11694 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  );.*/.static int
11695 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74   assert_pager_st
11696 61 74 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  ate(Pager *pPage
11697 72 29 7b 0a 0a 20 20 2f 2a 20 41 20 74 65 6d 70  r){..  /* A temp
11698 2d 66 69 6c 65 20 69 73 20 61 6c 77 61 79 73 20  -file is always 
11699 69 6e 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49  in PAGER_EXCLUSI
1169a 56 45 20 6f 72 20 50 41 47 45 52 5f 53 59 4e 43  VE or PAGER_SYNC
1169b 45 44 20 73 74 61 74 65 2e 20 2a 2f 0a 20 20 61  ED state. */.  a
1169c 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 74  ssert( pPager->t
1169d 65 6d 70 46 69 6c 65 3d 3d 30 20 7c 7c 20 70 50  empFile==0 || pP
1169e 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47  ager->state>=PAG
1169f 45 52 5f 45 58 43 4c 55 53 49 56 45 20 29 3b 0a  ER_EXCLUSIVE );.
116a0 0a 20 20 2f 2a 20 54 68 65 20 63 68 61 6e 67 65  .  /* The change
116a1 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 20 69  CountDone flag i
116a2 73 20 61 6c 77 61 79 73 20 73 65 74 20 66 6f 72  s always set for
116a3 20 74 65 6d 70 2d 66 69 6c 65 73 20 2a 2f 0a 20   temp-files */. 
116a4 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
116a5 3e 74 65 6d 70 46 69 6c 65 3d 3d 30 20 7c 7c 20  >tempFile==0 || 
116a6 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f  pPager->changeCo
116a7 75 6e 74 44 6f 6e 65 20 29 3b 0a 0a 20 20 72 65  untDone );..  re
116a8 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66  turn 1;.}.#endif
116a9 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
116aa 72 75 65 20 69 66 20 69 74 20 69 73 20 6e 65 63  rue if it is nec
116ab 65 73 73 61 72 79 20 74 6f 20 77 72 69 74 65 20  essary to write 
116ac 70 61 67 65 20 2a 70 50 67 20 69 6e 74 6f 20 74  page *pPg into t
116ad 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 0a  he sub-journal..
116ae 2a 2a 20 41 20 70 61 67 65 20 6e 65 65 64 73 20  ** A page needs 
116af 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e  to be written in
116b0 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e  to the sub-journ
116b1 61 6c 20 69 66 20 74 68 65 72 65 20 65 78 69 73  al if there exis
116b2 74 73 20 6f 6e 65 0a 2a 2a 20 6f 72 20 6d 6f 72  ts one.** or mor
116b3 65 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74  e open savepoint
116b4 73 20 66 6f 72 20 77 68 69 63 68 3a 0a 2a 2a 0a  s for which:.**.
116b5 2a 2a 20 20 20 2a 20 54 68 65 20 70 61 67 65 2d  **   * The page-
116b6 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73 20 74  number is less t
116b7 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20  han or equal to 
116b8 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 6e  PagerSavepoint.n
116b9 4f 72 69 67 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a  Orig, and.**   *
116ba 20 54 68 65 20 62 69 74 20 63 6f 72 72 65 73 70   The bit corresp
116bb 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 70 61  onding to the pa
116bc 67 65 2d 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74  ge-number is not
116bd 20 73 65 74 20 69 6e 0a 2a 2a 20 20 20 20 20 50   set in.**     P
116be 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49  agerSavepoint.pI
116bf 6e 53 61 76 65 70 6f 69 6e 74 2e 0a 2a 2f 0a 73  nSavepoint..*/.s
116c0 74 61 74 69 63 20 69 6e 74 20 73 75 62 6a 52 65  tatic int subjRe
116c1 71 75 69 72 65 73 50 61 67 65 28 50 67 48 64 72  quiresPage(PgHdr
116c2 20 2a 70 50 67 29 7b 0a 20 20 50 67 6e 6f 20 70   *pPg){.  Pgno p
116c3 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b  gno = pPg->pgno;
116c4 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
116c5 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a   = pPg->pPager;.
116c6 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69    int i;.  for(i
116c7 3d 30 3b 20 69 3c 70 50 61 67 65 72 2d 3e 6e 53  =0; i<pPager->nS
116c8 61 76 65 70 6f 69 6e 74 3b 20 69 2b 2b 29 7b 0a  avepoint; i++){.
116c9 20 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69      PagerSavepoi
116ca 6e 74 20 2a 70 20 3d 20 26 70 50 61 67 65 72 2d  nt *p = &pPager-
116cb 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 5d 3b 0a  >aSavepoint[i];.
116cc 20 20 20 20 69 66 28 20 70 2d 3e 6e 4f 72 69 67      if( p->nOrig
116cd 3e 3d 70 67 6e 6f 20 26 26 20 30 3d 3d 73 71 6c  >=pgno && 0==sql
116ce 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70  ite3BitvecTest(p
116cf 2d 3e 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c 20  ->pInSavepoint, 
116d0 70 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 72  pgno) ){.      r
116d1 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
116d2 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
116d3 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
116d4 72 75 65 20 69 66 20 74 68 65 20 70 61 67 65 20  rue if the page 
116d5 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68  is already in th
116d6 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a  e journal file..
116d7 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
116d8 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 50 67 48 64  geInJournal(PgHd
116d9 72 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 72  r *pPg){.  retur
116da 6e 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54  n sqlite3BitvecT
116db 65 73 74 28 70 50 67 2d 3e 70 50 61 67 65 72 2d  est(pPg->pPager-
116dc 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67  >pInJournal, pPg
116dd 2d 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  ->pgno);.}../*.*
116de 2a 20 52 65 61 64 20 61 20 33 32 2d 62 69 74 20  * Read a 32-bit 
116df 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 74 68 65  integer from the
116e0 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65 73 63   given file desc
116e1 72 69 70 74 6f 72 2e 20 20 53 74 6f 72 65 20 74  riptor.  Store t
116e2 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 74 68  he integer.** th
116e3 61 74 20 69 73 20 72 65 61 64 20 69 6e 20 2a 70  at is read in *p
116e4 52 65 73 2e 20 20 52 65 74 75 72 6e 20 53 51 4c  Res.  Return SQL
116e5 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74  ITE_OK if everyt
116e6 68 69 6e 67 20 77 6f 72 6b 65 64 2c 20 6f 72 20  hing worked, or 
116e7 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65  an.** error code
116e8 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f   is something go
116e9 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20  es wrong..**.** 
116ea 41 6c 6c 20 76 61 6c 75 65 73 20 61 72 65 20 73  All values are s
116eb 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b 20 61 73  tored on disk as
116ec 20 62 69 67 2d 65 6e 64 69 61 6e 2e 0a 2a 2f 0a   big-endian..*/.
116ed 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 33  static int read3
116ee 32 62 69 74 73 28 73 71 6c 69 74 65 33 5f 66 69  2bits(sqlite3_fi
116ef 6c 65 20 2a 66 64 2c 20 69 36 34 20 6f 66 66 73  le *fd, i64 offs
116f0 65 74 2c 20 75 33 32 20 2a 70 52 65 73 29 7b 0a  et, u32 *pRes){.
116f1 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
116f2 61 63 5b 34 5d 3b 0a 20 20 69 6e 74 20 72 63 20  ac[4];.  int rc 
116f3 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28  = sqlite3OsRead(
116f4 66 64 2c 20 61 63 2c 20 73 69 7a 65 6f 66 28 61  fd, ac, sizeof(a
116f5 63 29 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 69  c), offset);.  i
116f6 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
116f7 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20   ){.    *pRes = 
116f8 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28  sqlite3Get4byte(
116f9 61 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ac);.  }.  retur
116fa 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  n rc;.}../*.** W
116fb 72 69 74 65 20 61 20 33 32 2d 62 69 74 20 69 6e  rite a 32-bit in
116fc 74 65 67 65 72 20 69 6e 74 6f 20 61 20 73 74 72  teger into a str
116fd 69 6e 67 20 62 75 66 66 65 72 20 69 6e 20 62 69  ing buffer in bi
116fe 67 2d 65 6e 64 69 61 6e 20 62 79 74 65 20 6f 72  g-endian byte or
116ff 64 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  der..*/.#define 
11700 70 75 74 33 32 62 69 74 73 28 41 2c 42 29 20 20  put32bits(A,B)  
11701 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28  sqlite3Put4byte(
11702 28 75 38 2a 29 41 2c 42 29 0a 0a 2f 2a 0a 2a 2a  (u8*)A,B)../*.**
11703 20 57 72 69 74 65 20 61 20 33 32 2d 62 69 74 20   Write a 32-bit 
11704 69 6e 74 65 67 65 72 20 69 6e 74 6f 20 74 68 65  integer into the
11705 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65 73 63   given file desc
11706 72 69 70 74 6f 72 2e 20 20 52 65 74 75 72 6e 20  riptor.  Return 
11707 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20  SQLITE_OK.** on 
11708 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72  success or an er
11709 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d 65  ror code is some
1170a 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67  thing goes wrong
1170b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1170c 77 72 69 74 65 33 32 62 69 74 73 28 73 71 6c 69  write32bits(sqli
1170d 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 69 36  te3_file *fd, i6
1170e 34 20 6f 66 66 73 65 74 2c 20 75 33 32 20 76 61  4 offset, u32 va
1170f 6c 29 7b 0a 20 20 63 68 61 72 20 61 63 5b 34 5d  l){.  char ac[4]
11710 3b 0a 20 20 70 75 74 33 32 62 69 74 73 28 61 63  ;.  put32bits(ac
11711 2c 20 76 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e  , val);.  return
11712 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
11713 66 64 2c 20 61 63 2c 20 34 2c 20 6f 66 66 73 65  fd, ac, 4, offse
11714 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  t);.}../*.** The
11715 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69   argument to thi
11716 73 20 6d 61 63 72 6f 20 69 73 20 61 20 66 69 6c  s macro is a fil
11717 65 20 64 65 73 63 72 69 70 74 6f 72 20 28 74 79  e descriptor (ty
11718 70 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  pe sqlite3_file*
11719 29 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 69  )..** Return 0 i
1171a 66 20 69 74 20 69 73 20 6e 6f 74 20 6f 70 65 6e  f it is not open
1171b 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 20 28 62  , or non-zero (b
1171c 75 74 20 6e 6f 74 20 31 29 20 69 66 20 69 74 20  ut not 1) if it 
1171d 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  is..**.** This i
1171e 73 20 73 6f 20 74 68 61 74 20 65 78 70 72 65 73  s so that expres
1171f 73 69 6f 6e 73 20 63 61 6e 20 62 65 20 77 72 69  sions can be wri
11720 74 74 65 6e 20 61 73 3a 0a 2a 2a 0a 2a 2a 20 20  tten as:.**.**  
11721 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67   if( isOpen(pPag
11722 65 72 2d 3e 6a 66 64 29 20 29 7b 20 2e 2e 2e 0a  er->jfd) ){ ....
11723 2a 2a 0a 2a 2a 20 69 6e 73 74 65 61 64 20 6f 66  **.** instead of
11724 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 70 50 61  .**.**   if( pPa
11725 67 65 72 2d 3e 6a 66 64 2d 3e 70 4d 65 74 68 6f  ger->jfd->pMetho
11726 64 73 20 29 7b 20 2e 2e 2e 0a 2a 2f 0a 23 64 65  ds ){ ....*/.#de
11727 66 69 6e 65 20 69 73 4f 70 65 6e 28 70 46 64 29  fine isOpen(pFd)
11728 20 28 28 70 46 64 29 2d 3e 70 4d 65 74 68 6f 64   ((pFd)->pMethod
11729 73 29 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 66 69 6c  s)../*.** If fil
1172a 65 20 70 46 64 20 69 73 20 6f 70 65 6e 2c 20 63  e pFd is open, c
1172b 61 6c 6c 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c  all sqlite3OsUnl
1172c 6f 63 6b 28 29 20 6f 6e 20 69 74 2e 0a 2a 2f 0a  ock() on it..*/.
1172d 73 74 61 74 69 63 20 69 6e 74 20 6f 73 55 6e 6c  static int osUnl
1172e 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
1172f 20 2a 70 46 64 2c 20 69 6e 74 20 65 4c 6f 63 6b   *pFd, int eLock
11730 29 7b 0a 20 20 69 66 28 20 21 69 73 4f 70 65 6e  ){.  if( !isOpen
11731 28 70 46 64 29 20 29 7b 0a 20 20 20 20 72 65 74  (pFd) ){.    ret
11732 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
11733 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69   }.  return sqli
11734 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 70 46 64 2c  te3OsUnlock(pFd,
11735 20 65 4c 6f 63 6b 29 3b 0a 7d 0a 0a 2f 2a 0a 2a   eLock);.}../*.*
11736 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
11737 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68  determines wheth
11738 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 74  er or not the at
11739 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d  omic-write optim
1173a 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6e 20 62  ization.** can b
1173b 65 20 75 73 65 64 20 77 69 74 68 20 74 68 69 73  e used with this
1173c 20 70 61 67 65 72 2e 20 54 68 65 20 6f 70 74 69   pager. The opti
1173d 6d 69 7a 61 74 69 6f 6e 20 63 61 6e 20 62 65 20  mization can be 
1173e 75 73 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20  used if:.**.**  
1173f 28 61 29 20 74 68 65 20 76 61 6c 75 65 20 72 65  (a) the value re
11740 74 75 72 6e 65 64 20 62 79 20 4f 73 44 65 76 69  turned by OsDevi
11741 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
11742 73 28 29 20 69 6e 64 69 63 61 74 65 73 20 74 68  s() indicates th
11743 61 74 0a 2a 2a 20 20 20 20 20 20 61 20 64 61 74  at.**      a dat
11744 61 62 61 73 65 20 70 61 67 65 20 6d 61 79 20 62  abase page may b
11745 65 20 77 72 69 74 74 65 6e 20 61 74 6f 6d 69 63  e written atomic
11746 61 6c 6c 79 2c 20 61 6e 64 0a 2a 2a 20 20 28 62  ally, and.**  (b
11747 29 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75  ) the value retu
11748 72 6e 65 64 20 62 79 20 4f 73 53 65 63 74 6f 72  rned by OsSector
11749 53 69 7a 65 28 29 20 69 73 20 6c 65 73 73 20 74  Size() is less t
1174a 68 61 6e 20 6f 72 20 65 71 75 61 6c 0a 2a 2a 20  han or equal.** 
1174b 20 20 20 20 20 74 6f 20 74 68 65 20 70 61 67 65       to the page
1174c 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   size..**.** The
1174d 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73   optimization is
1174e 20 61 6c 73 6f 20 61 6c 77 61 79 73 20 65 6e 61   also always ena
1174f 62 6c 65 64 20 66 6f 72 20 74 65 6d 70 6f 72 61  bled for tempora
11750 72 79 20 66 69 6c 65 73 2e 20 49 74 20 69 73 0a  ry files. It is.
11751 2a 2a 20 61 6e 20 65 72 72 6f 72 20 74 6f 20 63  ** an error to c
11752 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  all this functio
11753 6e 20 69 66 20 70 50 61 67 65 72 20 69 73 20 6f  n if pPager is o
11754 70 65 6e 65 64 20 6f 6e 20 61 6e 20 69 6e 2d 6d  pened on an in-m
11755 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 62 61 73  emory.** databas
11756 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
11757 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 63 61 6e  optimization can
11758 6e 6f 74 20 62 65 20 75 73 65 64 2c 20 30 20 69  not be used, 0 i
11759 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 69  s returned. If i
1175a 74 20 63 61 6e 20 62 65 20 75 73 65 64 2c 0a 2a  t can be used,.*
1175b 2a 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65  * then the value
1175c 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65   returned is the
1175d 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75   size of the jou
1175e 72 6e 61 6c 20 66 69 6c 65 20 77 68 65 6e 20 69  rnal file when i
1175f 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 72 6f  t.** contains ro
11760 6c 6c 62 61 63 6b 20 64 61 74 61 20 66 6f 72 20  llback data for 
11761 65 78 61 63 74 6c 79 20 6f 6e 65 20 70 61 67 65  exactly one page
11762 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
11763 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43  TE_ENABLE_ATOMIC
11764 5f 57 52 49 54 45 0a 73 74 61 74 69 63 20 69 6e  _WRITE.static in
11765 74 20 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a 65  t jrnlBufferSize
11766 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
11767 0a 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44  .  assert( !MEMD
11768 42 20 29 3b 0a 20 20 69 66 28 20 21 70 50 61 67  B );.  if( !pPag
11769 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a  er->tempFile ){.
1176a 20 20 20 20 69 6e 74 20 64 63 3b 20 20 20 20 20      int dc;     
1176b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1176c 20 20 20 20 20 20 2f 2a 20 44 65 76 69 63 65 20        /* Device 
1176d 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20  characteristics 
1176e 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 53 65 63 74  */.    int nSect
1176f 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  or;             
11770 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 74           /* Sect
11771 6f 72 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 69  or size */.    i
11772 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20  nt szPage;      
11773 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11774 20 2f 2a 20 50 61 67 65 20 73 69 7a 65 20 2a 2f   /* Page size */
11775 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73  ..    assert( is
11776 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29  Open(pPager->fd)
11777 20 29 3b 0a 20 20 20 20 64 63 20 3d 20 73 71 6c   );.    dc = sql
11778 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72  ite3OsDeviceChar
11779 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61 67  acteristics(pPag
1177a 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 6e 53 65  er->fd);.    nSe
1177b 63 74 6f 72 20 3d 20 70 50 61 67 65 72 2d 3e 73  ctor = pPager->s
1177c 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 20 20 73  ectorSize;.    s
1177d 7a 50 61 67 65 20 3d 20 70 50 61 67 65 72 2d 3e  zPage = pPager->
1177e 70 61 67 65 53 69 7a 65 3b 0a 0a 20 20 20 20 61  pageSize;..    a
1177f 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43  ssert(SQLITE_IOC
11780 41 50 5f 41 54 4f 4d 49 43 35 31 32 3d 3d 28 35  AP_ATOMIC512==(5
11781 31 32 3e 3e 38 29 29 3b 0a 20 20 20 20 61 73 73  12>>8));.    ass
11782 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 50  ert(SQLITE_IOCAP
11783 5f 41 54 4f 4d 49 43 36 34 4b 3d 3d 28 36 35 35  _ATOMIC64K==(655
11784 33 36 3e 3e 38 29 29 3b 0a 20 20 20 20 69 66 28  36>>8));.    if(
11785 20 30 3d 3d 28 64 63 26 28 53 51 4c 49 54 45 5f   0==(dc&(SQLITE_
11786 49 4f 43 41 50 5f 41 54 4f 4d 49 43 7c 28 73 7a  IOCAP_ATOMIC|(sz
11787 50 61 67 65 3e 3e 38 29 29 20 7c 7c 20 6e 53 65  Page>>8)) || nSe
11788 63 74 6f 72 3e 73 7a 50 61 67 65 29 20 29 7b 0a  ctor>szPage) ){.
11789 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
1178a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
1178b 75 72 6e 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  urn JOURNAL_HDR_
1178c 53 5a 28 70 50 61 67 65 72 29 20 2b 20 4a 4f 55  SZ(pPager) + JOU
1178d 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65  RNAL_PG_SZ(pPage
1178e 72 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  r);.}.#endif../*
1178f 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 43 48  .** If SQLITE_CH
11790 45 43 4b 5f 50 41 47 45 53 20 69 73 20 64 65 66  ECK_PAGES is def
11791 69 6e 65 64 20 74 68 65 6e 20 77 65 20 64 6f 20  ined then we do 
11792 73 6f 6d 65 20 73 61 6e 69 74 79 20 63 68 65 63  some sanity chec
11793 6b 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68 65 20 63  king.** on the c
11794 61 63 68 65 20 75 73 69 6e 67 20 61 20 68 61 73  ache using a has
11795 68 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69  h function.  Thi
11796 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65  s is used for te
11797 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 65 62  sting.** and deb
11798 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a  ugging only..*/.
11799 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48  #ifdef SQLITE_CH
1179a 45 43 4b 5f 50 41 47 45 53 0a 2f 2a 0a 2a 2a 20  ECK_PAGES./*.** 
1179b 52 65 74 75 72 6e 20 61 20 33 32 2d 62 69 74 20  Return a 32-bit 
1179c 68 61 73 68 20 6f 66 20 74 68 65 20 70 61 67 65  hash of the page
1179d 20 64 61 74 61 20 66 6f 72 20 70 50 61 67 65 2e   data for pPage.
1179e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 20 70  .*/.static u32 p
1179f 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 69 6e  ager_datahash(in
117a0 74 20 6e 42 79 74 65 2c 20 75 6e 73 69 67 6e 65  t nByte, unsigne
117a1 64 20 63 68 61 72 20 2a 70 44 61 74 61 29 7b 0a  d char *pData){.
117a2 20 20 75 33 32 20 68 61 73 68 20 3d 20 30 3b 0a    u32 hash = 0;.
117a3 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69    int i;.  for(i
117a4 3d 30 3b 20 69 3c 6e 42 79 74 65 3b 20 69 2b 2b  =0; i<nByte; i++
117a5 29 7b 0a 20 20 20 20 68 61 73 68 20 3d 20 28 68  ){.    hash = (h
117a6 61 73 68 2a 31 30 33 39 29 20 2b 20 70 44 61 74  ash*1039) + pDat
117a7 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75  a[i];.  }.  retu
117a8 72 6e 20 68 61 73 68 3b 0a 7d 0a 73 74 61 74 69  rn hash;.}.stati
117a9 63 20 75 33 32 20 70 61 67 65 72 5f 70 61 67 65  c u32 pager_page
117aa 68 61 73 68 28 50 67 48 64 72 20 2a 70 50 61 67  hash(PgHdr *pPag
117ab 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 61 67  e){.  return pag
117ac 65 72 5f 64 61 74 61 68 61 73 68 28 70 50 61 67  er_datahash(pPag
117ad 65 2d 3e 70 50 61 67 65 72 2d 3e 70 61 67 65 53  e->pPager->pageS
117ae 69 7a 65 2c 20 28 75 6e 73 69 67 6e 65 64 20 63  ize, (unsigned c
117af 68 61 72 20 2a 29 70 50 61 67 65 2d 3e 70 44 61  har *)pPage->pDa
117b0 74 61 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  ta);.}.static vo
117b1 69 64 20 70 61 67 65 72 5f 73 65 74 5f 70 61 67  id pager_set_pag
117b2 65 68 61 73 68 28 50 67 48 64 72 20 2a 70 50 61  ehash(PgHdr *pPa
117b3 67 65 29 7b 0a 20 20 70 50 61 67 65 2d 3e 70 61  ge){.  pPage->pa
117b4 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70  geHash = pager_p
117b5 61 67 65 68 61 73 68 28 70 50 61 67 65 29 3b 0a  agehash(pPage);.
117b6 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 48 45  }../*.** The CHE
117b7 43 4b 5f 50 41 47 45 20 6d 61 63 72 6f 20 74 61  CK_PAGE macro ta
117b8 6b 65 73 20 61 20 50 67 48 64 72 2a 20 61 73 20  kes a PgHdr* as 
117b9 61 6e 20 61 72 67 75 6d 65 6e 74 2e 20 49 66 20  an argument. If 
117ba 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47  SQLITE_CHECK_PAG
117bb 45 53 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64  ES.** is defined
117bc 2c 20 61 6e 64 20 4e 44 45 42 55 47 20 69 73 20  , and NDEBUG is 
117bd 6e 6f 74 20 64 65 66 69 6e 65 64 2c 20 61 6e 20  not defined, an 
117be 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
117bf 6e 74 20 63 68 65 63 6b 73 0a 2a 2a 20 74 68 61  nt checks.** tha
117c0 74 20 74 68 65 20 70 61 67 65 20 69 73 20 65 69  t the page is ei
117c1 74 68 65 72 20 64 69 72 74 79 20 6f 72 20 73 74  ther dirty or st
117c2 69 6c 6c 20 6d 61 74 63 68 65 73 20 74 68 65 20  ill matches the 
117c3 63 61 6c 63 75 6c 61 74 65 64 20 70 61 67 65 2d  calculated page-
117c4 68 61 73 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  hash..*/.#define
117c5 20 43 48 45 43 4b 5f 50 41 47 45 28 78 29 20 63   CHECK_PAGE(x) c
117c6 68 65 63 6b 50 61 67 65 28 78 29 0a 73 74 61 74  heckPage(x).stat
117c7 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 61 67  ic void checkPag
117c8 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20  e(PgHdr *pPg){. 
117c9 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d   Pager *pPager =
117ca 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20   pPg->pPager;.  
117cb 61 73 73 65 72 74 28 20 21 70 50 67 2d 3e 70 61  assert( !pPg->pa
117cc 67 65 48 61 73 68 20 7c 7c 20 70 50 61 67 65 72  geHash || pPager
117cd 2d 3e 65 72 72 43 6f 64 65 0a 20 20 20 20 20 20  ->errCode.      
117ce 7c 7c 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 50  || (pPg->flags&P
117cf 47 48 44 52 5f 44 49 52 54 59 29 20 7c 7c 20 70  GHDR_DIRTY) || p
117d0 50 67 2d 3e 70 61 67 65 48 61 73 68 3d 3d 70 61  Pg->pageHash==pa
117d1 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67  ger_pagehash(pPg
117d2 29 20 29 3b 0a 7d 0a 0a 23 65 6c 73 65 0a 23 64  ) );.}..#else.#d
117d3 65 66 69 6e 65 20 70 61 67 65 72 5f 64 61 74 61  efine pager_data
117d4 68 61 73 68 28 58 2c 59 29 20 20 30 0a 23 64 65  hash(X,Y)  0.#de
117d5 66 69 6e 65 20 70 61 67 65 72 5f 70 61 67 65 68  fine pager_pageh
117d6 61 73 68 28 58 29 20 20 30 0a 23 64 65 66 69 6e  ash(X)  0.#defin
117d7 65 20 43 48 45 43 4b 5f 50 41 47 45 28 78 29 0a  e CHECK_PAGE(x).
117d8 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54  #endif  /* SQLIT
117d9 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 20 2a 2f  E_CHECK_PAGES */
117da 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69  ../*.** When thi
117db 73 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65 20  s is called the 
117dc 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72  journal file for
117dd 20 70 61 67 65 72 20 70 50 61 67 65 72 20 6d 75   pager pPager mu
117de 73 74 20 62 65 20 6f 70 65 6e 2e 0a 2a 2a 20 54  st be open..** T
117df 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 74  his function att
117e0 65 6d 70 74 73 20 74 6f 20 72 65 61 64 20 61 20  empts to read a 
117e1 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
117e2 69 6c 65 20 6e 61 6d 65 20 66 72 6f 6d 20 74 68  ile name from th
117e3 65 20 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68 65  e .** end of the
117e4 20 66 69 6c 65 20 61 6e 64 2c 20 69 66 20 73 75   file and, if su
117e5 63 63 65 73 73 66 75 6c 2c 20 63 6f 70 69 65 73  ccessful, copies
117e6 20 69 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20   it into memory 
117e7 73 75 70 70 6c 69 65 64 20 0a 2a 2a 20 62 79 20  supplied .** by 
117e8 74 68 65 20 63 61 6c 6c 65 72 2e 20 53 65 65 20  the caller. See 
117e9 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 77  comments above w
117ea 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61  riteMasterJourna
117eb 6c 28 29 20 66 6f 72 20 74 68 65 20 66 6f 72 6d  l() for the form
117ec 61 74 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74  at.** used to st
117ed 6f 72 65 20 61 20 6d 61 73 74 65 72 20 6a 6f 75  ore a master jou
117ee 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 61  rnal file name a
117ef 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 6a  t the end of a j
117f0 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a  ournal file..**.
117f1 2a 2a 20 7a 4d 61 73 74 65 72 20 6d 75 73 74 20  ** zMaster must 
117f2 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65  point to a buffe
117f3 72 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 4d  r of at least nM
117f4 61 73 74 65 72 20 62 79 74 65 73 20 61 6c 6c 6f  aster bytes allo
117f5 63 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20  cated by.** the 
117f6 63 61 6c 6c 65 72 2e 20 54 68 69 73 20 73 68 6f  caller. This sho
117f7 75 6c 64 20 62 65 20 73 71 6c 69 74 65 33 5f 76  uld be sqlite3_v
117f8 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 20  fs.mxPathname+1 
117f9 28 74 6f 20 65 6e 73 75 72 65 20 74 68 65 72 65  (to ensure there
117fa 20 69 73 0a 2a 2a 20 65 6e 6f 75 67 68 20 73 70   is.** enough sp
117fb 61 63 65 20 74 6f 20 77 72 69 74 65 20 74 68 65  ace to write the
117fc 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
117fd 6e 61 6d 65 29 2e 20 49 66 20 74 68 65 20 6d 61  name). If the ma
117fe 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20  ster journal.** 
117ff 6e 61 6d 65 20 69 6e 20 74 68 65 20 6a 6f 75 72  name in the jour
11800 6e 61 6c 20 69 73 20 6c 6f 6e 67 65 72 20 74 68  nal is longer th
11801 61 6e 20 6e 4d 61 73 74 65 72 20 62 79 74 65 73  an nMaster bytes
11802 20 28 69 6e 63 6c 75 64 69 6e 67 20 61 0a 2a 2a   (including a.**
11803 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 29   nul-terminator)
11804 2c 20 74 68 65 6e 20 74 68 69 73 20 69 73 20 68  , then this is h
11805 61 6e 64 6c 65 64 20 61 73 20 69 66 20 6e 6f 20  andled as if no 
11806 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e  master journal n
11807 61 6d 65 0a 2a 2a 20 77 65 72 65 20 70 72 65 73  ame.** were pres
11808 65 6e 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  ent in the journ
11809 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d  al..**.** If a m
1180a 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
1180b 6c 65 20 6e 61 6d 65 20 69 73 20 70 72 65 73 65  le name is prese
1180c 6e 74 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  nt at the end of
1180d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20   the journal.** 
1180e 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73  file, then it is
1180f 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65   copied into the
11810 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20   buffer pointed 
11811 74 6f 20 62 79 20 7a 4d 61 73 74 65 72 2e 20 41  to by zMaster. A
11812 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74  .** nul-terminat
11813 6f 72 20 62 79 74 65 20 69 73 20 61 70 70 65 6e  or byte is appen
11814 64 65 64 20 74 6f 20 74 68 65 20 62 75 66 66 65  ded to the buffe
11815 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20  r following the 
11816 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61  master.** journa
11817 6c 20 66 69 6c 65 20 6e 61 6d 65 2e 0a 2a 2a 0a  l file name..**.
11818 2a 2a 20 49 66 20 69 74 20 69 73 20 64 65 74 65  ** If it is dete
11819 72 6d 69 6e 65 64 20 74 68 61 74 20 6e 6f 20 6d  rmined that no m
1181a 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
1181b 6c 65 20 6e 61 6d 65 20 69 73 20 70 72 65 73 65  le name is prese
1181c 6e 74 20 0a 2a 2a 20 7a 4d 61 73 74 65 72 5b 30  nt .** zMaster[0
1181d 5d 20 69 73 20 73 65 74 20 74 6f 20 30 20 61 6e  ] is set to 0 an
1181e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75  d SQLITE_OK retu
1181f 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  rned..**.** If a
11820 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77  n error occurs w
11821 68 69 6c 65 20 72 65 61 64 69 6e 67 20 66 72 6f  hile reading fro
11822 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  m the journal fi
11823 6c 65 2c 20 61 6e 20 53 51 4c 69 74 65 0a 2a 2a  le, an SQLite.**
11824 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
11825 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74  eturned..*/.stat
11826 69 63 20 69 6e 74 20 72 65 61 64 4d 61 73 74 65  ic int readMaste
11827 72 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 33  rJournal(sqlite3
11828 5f 66 69 6c 65 20 2a 70 4a 72 6e 6c 2c 20 63 68  _file *pJrnl, ch
11829 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20 75 33 32  ar *zMaster, u32
1182a 20 6e 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74   nMaster){.  int
1182b 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
1182c 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
1182d 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75 33 32 20  n code */.  u32 
1182e 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  len;            
1182f 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
11830 20 69 6e 20 62 79 74 65 73 20 6f 66 20 6d 61 73   in bytes of mas
11831 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65  ter journal name
11832 20 2a 2f 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20   */.  i64 szJ;  
11833 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11834 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a 65 20 69   /* Total size i
11835 6e 20 62 79 74 65 73 20 6f 66 20 6a 6f 75 72 6e  n bytes of journ
11836 61 6c 20 66 69 6c 65 20 70 4a 72 6e 6c 20 2a 2f  al file pJrnl */
11837 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b 20 20 20  .  u32 cksum;   
11838 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11839 20 4d 4a 20 63 68 65 63 6b 73 75 6d 20 76 61 6c   MJ checksum val
1183a 75 65 20 72 65 61 64 20 66 72 6f 6d 20 6a 6f 75  ue read from jou
1183b 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20 75 3b  rnal */.  u32 u;
1183c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1183d 20 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64       /* Unsigned
1183e 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   loop counter */
1183f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
11840 20 61 4d 61 67 69 63 5b 38 5d 3b 20 20 20 2f 2a   aMagic[8];   /*
11841 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c   A buffer to hol
11842 64 20 74 68 65 20 6d 61 67 69 63 20 68 65 61 64  d the magic head
11843 65 72 20 2a 2f 0a 20 20 7a 4d 61 73 74 65 72 5b  er */.  zMaster[
11844 30 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 69 66  0] = '\0';..  if
11845 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  ( SQLITE_OK!=(rc
11846 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65   = sqlite3OsFile
11847 53 69 7a 65 28 70 4a 72 6e 6c 2c 20 26 73 7a 4a  Size(pJrnl, &szJ
11848 29 29 0a 20 20 20 7c 7c 20 73 7a 4a 3c 31 36 0a  )).   || szJ<16.
11849 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21     || SQLITE_OK!
1184a 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 74  =(rc = read32bit
1184b 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2d 31 36 2c  s(pJrnl, szJ-16,
1184c 20 26 6c 65 6e 29 29 0a 20 20 20 7c 7c 20 6c 65   &len)).   || le
1184d 6e 3e 3d 6e 4d 61 73 74 65 72 20 0a 20 20 20 7c  n>=nMaster .   |
1184e 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  | SQLITE_OK!=(rc
1184f 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 4a   = read32bits(pJ
11850 72 6e 6c 2c 20 73 7a 4a 2d 31 32 2c 20 26 63 6b  rnl, szJ-12, &ck
11851 73 75 6d 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49  sum)).   || SQLI
11852 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c  TE_OK!=(rc = sql
11853 69 74 65 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c  ite3OsRead(pJrnl
11854 2c 20 61 4d 61 67 69 63 2c 20 38 2c 20 73 7a 4a  , aMagic, 8, szJ
11855 2d 38 29 29 0a 20 20 20 7c 7c 20 6d 65 6d 63 6d  -8)).   || memcm
11856 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75 72 6e  p(aMagic, aJourn
11857 61 6c 4d 61 67 69 63 2c 20 38 29 0a 20 20 20 7c  alMagic, 8).   |
11858 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  | SQLITE_OK!=(rc
11859 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64   = sqlite3OsRead
1185a 28 70 4a 72 6e 6c 2c 20 7a 4d 61 73 74 65 72 2c  (pJrnl, zMaster,
1185b 20 6c 65 6e 2c 20 73 7a 4a 2d 31 36 2d 6c 65 6e   len, szJ-16-len
1185c 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75  )).  ){.    retu
1185d 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  rn rc;.  }..  /*
1185e 20 53 65 65 20 69 66 20 74 68 65 20 63 68 65 63   See if the chec
1185f 6b 73 75 6d 20 6d 61 74 63 68 65 73 20 74 68 65  ksum matches the
11860 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
11861 6e 61 6d 65 20 2a 2f 0a 20 20 66 6f 72 28 75 3d  name */.  for(u=
11862 30 3b 20 75 3c 6c 65 6e 3b 20 75 2b 2b 29 7b 0a  0; u<len; u++){.
11863 20 20 20 20 63 6b 73 75 6d 20 2d 3d 20 7a 4d 61      cksum -= zMa
11864 73 74 65 72 5b 75 5d 3b 0a 20 20 7d 0a 20 20 69  ster[u];.  }.  i
11865 66 28 20 63 6b 73 75 6d 20 29 7b 0a 20 20 20 20  f( cksum ){.    
11866 2f 2a 20 49 66 20 74 68 65 20 63 68 65 63 6b 73  /* If the checks
11867 75 6d 20 64 6f 65 73 6e 27 74 20 61 64 64 20 75  um doesn't add u
11868 70 2c 20 74 68 65 6e 20 6f 6e 65 20 6f 72 20 6d  p, then one or m
11869 6f 72 65 20 6f 66 20 74 68 65 20 64 69 73 6b 20  ore of the disk 
1186a 73 65 63 74 6f 72 73 0a 20 20 20 20 2a 2a 20 63  sectors.    ** c
1186b 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6d 61  ontaining the ma
1186c 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
1186d 65 6e 61 6d 65 20 69 73 20 63 6f 72 72 75 70 74  ename is corrupt
1186e 65 64 2e 20 54 68 69 73 20 6d 65 61 6e 73 0a 20  ed. This means. 
1186f 20 20 20 2a 2a 20 64 65 66 69 6e 69 74 65 6c 79     ** definitely
11870 20 72 6f 6c 6c 20 62 61 63 6b 2c 20 73 6f 20 6a   roll back, so j
11871 75 73 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ust return SQLIT
11872 45 5f 4f 4b 20 61 6e 64 20 72 65 70 6f 72 74 20  E_OK and report 
11873 61 20 28 6e 75 6c 29 0a 20 20 20 20 2a 2a 20 6d  a (nul).    ** m
11874 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69  aster-journal fi
11875 6c 65 6e 61 6d 65 2e 0a 20 20 20 20 2a 2f 0a 20  lename..    */. 
11876 20 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 7d 0a     len = 0;.  }.
11877 20 20 7a 4d 61 73 74 65 72 5b 6c 65 6e 5d 20 3d    zMaster[len] =
11878 20 27 5c 30 27 3b 0a 20 20 20 0a 20 20 72 65 74   '\0';.   .  ret
11879 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1187a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
1187b 68 65 20 6f 66 66 73 65 74 20 6f 66 20 74 68 65  he offset of the
1187c 20 73 65 63 74 6f 72 20 62 6f 75 6e 64 61 72 79   sector boundary
1187d 20 61 74 20 6f 72 20 69 6d 6d 65 64 69 61 74 65   at or immediate
1187e 6c 79 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67  ly .** following
1187f 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 70 50   the value in pP
11880 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
11881 2c 20 61 73 73 75 6d 69 6e 67 20 61 20 73 65 63  , assuming a sec
11882 74 6f 72 20 0a 2a 2a 20 73 69 7a 65 20 6f 66 20  tor .** size of 
11883 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69  pPager->sectorSi
11884 7a 65 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20  ze bytes..**.** 
11885 69 2e 65 20 66 6f 72 20 61 20 73 65 63 74 6f 72  i.e for a sector
11886 20 73 69 7a 65 20 6f 66 20 35 31 32 3a 0a 2a 2a   size of 512:.**
11887 0a 2a 2a 20 20 20 50 61 67 65 72 2e 6a 6f 75 72  .**   Pager.jour
11888 6e 61 6c 4f 66 66 20 20 20 20 20 20 20 20 20 20  nalOff          
11889 52 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20  Return value.** 
1188a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
1188b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1188c 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 30  ---------.**   0
1188d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1188e 20 20 20 20 20 20 20 20 20 30 0a 2a 2a 20 20 20           0.**   
1188f 35 31 32 20 20 20 20 20 20 20 20 20 20 20 20 20  512             
11890 20 20 20 20 20 20 20 20 20 20 35 31 32 0a 2a 2a            512.**
11891 20 20 20 31 30 30 20 20 20 20 20 20 20 20 20 20     100          
11892 20 20 20 20 20 20 20 20 20 20 20 20 20 35 31 32               512
11893 0a 2a 2a 20 20 20 32 30 30 30 20 20 20 20 20 20  .**   2000      
11894 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11895 32 30 34 38 0a 2a 2a 20 0a 2a 2f 0a 73 74 61 74  2048.** .*/.stat
11896 69 63 20 69 36 34 20 6a 6f 75 72 6e 61 6c 48 64  ic i64 journalHd
11897 72 4f 66 66 73 65 74 28 50 61 67 65 72 20 2a 70  rOffset(Pager *p
11898 50 61 67 65 72 29 7b 0a 20 20 69 36 34 20 6f 66  Pager){.  i64 of
11899 66 73 65 74 20 3d 20 30 3b 0a 20 20 69 36 34 20  fset = 0;.  i64 
1189a 63 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  c = pPager->jour
1189b 6e 61 6c 4f 66 66 3b 0a 20 20 69 66 28 20 63 20  nalOff;.  if( c 
1189c 29 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 3d 20  ){.    offset = 
1189d 28 28 63 2d 31 29 2f 4a 4f 55 52 4e 41 4c 5f 48  ((c-1)/JOURNAL_H
1189e 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 2b 20  DR_SZ(pPager) + 
1189f 31 29 20 2a 20 4a 4f 55 52 4e 41 4c 5f 48 44 52  1) * JOURNAL_HDR
118a0 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  _SZ(pPager);.  }
118a1 0a 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 65  .  assert( offse
118a2 74 25 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  t%JOURNAL_HDR_SZ
118a3 28 70 50 61 67 65 72 29 3d 3d 30 20 29 3b 0a 20  (pPager)==0 );. 
118a4 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 3e   assert( offset>
118a5 3d 63 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =c );.  assert( 
118a6 28 6f 66 66 73 65 74 2d 63 29 3c 4a 4f 55 52 4e  (offset-c)<JOURN
118a7 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
118a8 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 6f 66  ) );.  return of
118a9 66 73 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  fset;.}../*.** T
118aa 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
118ab 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65  must be open whe
118ac 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
118ad 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a  is called..**.**
118ae 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
118af 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65  s a no-op if the
118b0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61   journal file ha
118b1 73 20 6e 6f 74 20 62 65 65 6e 20 77 72 69 74 74  s not been writt
118b2 65 6e 20 74 6f 0a 2a 2a 20 77 69 74 68 69 6e 20  en to.** within 
118b3 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e  the current tran
118b4 73 61 63 74 69 6f 6e 20 28 69 2e 65 2e 20 69 66  saction (i.e. if
118b5 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66   Pager.journalOf
118b6 66 3d 3d 30 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  f==0)..**.** If 
118b7 64 6f 54 72 75 6e 63 61 74 65 20 69 73 20 6e 6f  doTruncate is no
118b8 6e 2d 7a 65 72 6f 20 6f 72 20 74 68 65 20 50 61  n-zero or the Pa
118b9 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c  ger.journalSizeL
118ba 69 6d 69 74 20 76 61 72 69 61 62 6c 65 20 69 73  imit variable is
118bb 0a 2a 2a 20 73 65 74 20 74 6f 20 30 2c 20 74 68  .** set to 0, th
118bc 65 6e 20 74 72 75 6e 63 61 74 65 20 74 68 65 20  en truncate the 
118bd 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20  journal file to 
118be 7a 65 72 6f 20 62 79 74 65 73 20 69 6e 20 73 69  zero bytes in si
118bf 7a 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a 2a  ze. Otherwise,.*
118c0 2a 20 7a 65 72 6f 20 74 68 65 20 32 38 2d 62 79  * zero the 28-by
118c1 74 65 20 68 65 61 64 65 72 20 61 74 20 74 68 65  te header at the
118c2 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6a 6f   start of the jo
118c3 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 6e 20 65  urnal file. In e
118c4 69 74 68 65 72 20 63 61 73 65 2c 20 0a 2a 2a 20  ither case, .** 
118c5 69 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20  if the pager is 
118c6 6e 6f 74 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d  not in no-sync m
118c7 6f 64 65 2c 20 73 79 6e 63 20 74 68 65 20 6a 6f  ode, sync the jo
118c8 75 72 6e 61 6c 20 66 69 6c 65 20 69 6d 6d 65 64  urnal file immed
118c9 69 61 74 65 6c 79 20 0a 2a 2a 20 61 66 74 65 72  iately .** after
118ca 20 77 72 69 74 69 6e 67 20 6f 72 20 74 72 75 6e   writing or trun
118cb 63 61 74 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a  cating it..**.**
118cc 20 49 66 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61   If Pager.journa
118cd 6c 53 69 7a 65 4c 69 6d 69 74 20 69 73 20 73 65  lSizeLimit is se
118ce 74 20 74 6f 20 61 20 70 6f 73 69 74 69 76 65 2c  t to a positive,
118cf 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c   non-zero value,
118d0 20 61 6e 64 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e   and.** followin
118d1 67 20 74 68 65 20 74 72 75 6e 63 61 74 69 6f 6e  g the truncation
118d2 20 6f 72 20 7a 65 72 6f 69 6e 67 20 64 65 73 63   or zeroing desc
118d3 72 69 62 65 64 20 61 62 6f 76 65 20 74 68 65 20  ribed above the 
118d4 73 69 7a 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20  size of the .** 
118d5 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20  journal file in 
118d6 62 79 74 65 73 20 69 73 20 6c 61 72 67 65 72 20  bytes is larger 
118d7 74 68 61 6e 20 74 68 69 73 20 76 61 6c 75 65 2c  than this value,
118d8 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65 20 74   then truncate t
118d9 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69  he.** journal fi
118da 6c 65 20 74 6f 20 50 61 67 65 72 2e 6a 6f 75 72  le to Pager.jour
118db 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 62 79 74  nalSizeLimit byt
118dc 65 73 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20  es. The journal 
118dd 66 69 6c 65 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74  file does.** not
118de 20 6e 65 65 64 20 74 6f 20 62 65 20 73 79 6e 63   need to be sync
118df 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69  ed following thi
118e0 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a  s operation..**.
118e1 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f  ** If an IO erro
118e2 72 20 6f 63 63 75 72 73 2c 20 61 62 61 6e 64 6f  r occurs, abando
118e3 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61 6e 64  n processing and
118e4 20 72 65 74 75 72 6e 20 74 68 65 20 49 4f 20 65   return the IO e
118e5 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 20 4f 74  rror code..** Ot
118e6 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 20  herwise, return 
118e7 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74  SQLITE_OK..*/.st
118e8 61 74 69 63 20 69 6e 74 20 7a 65 72 6f 4a 6f 75  atic int zeroJou
118e9 72 6e 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70  rnalHdr(Pager *p
118ea 50 61 67 65 72 2c 20 69 6e 74 20 64 6f 54 72 75  Pager, int doTru
118eb 6e 63 61 74 65 29 7b 0a 20 20 69 6e 74 20 72 63  ncate){.  int rc
118ec 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
118ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
118ee 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
118ef 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
118f0 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70  assert( isOpen(p
118f1 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20  Pager->jfd) );. 
118f2 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75   if( pPager->jou
118f3 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20 20 63  rnalOff ){.    c
118f4 6f 6e 73 74 20 69 36 34 20 69 4c 69 6d 69 74 20  onst i64 iLimit 
118f5 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  = pPager->journa
118f6 6c 53 69 7a 65 4c 69 6d 69 74 3b 20 20 20 20 2f  lSizeLimit;    /
118f7 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66  * Local cache of
118f8 20 6a 73 6c 20 2a 2f 0a 0a 20 20 20 20 49 4f 54   jsl */..    IOT
118f9 52 41 43 45 28 28 22 4a 5a 45 52 4f 48 44 52 20  RACE(("JZEROHDR 
118fa 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a  %p\n", pPager)).
118fb 20 20 20 20 69 66 28 20 64 6f 54 72 75 6e 63 61      if( doTrunca
118fc 74 65 20 7c 7c 20 69 4c 69 6d 69 74 3d 3d 30 20  te || iLimit==0 
118fd 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
118fe 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28  lite3OsTruncate(
118ff 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 30 29 3b  pPager->jfd, 0);
11900 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
11901 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
11902 68 61 72 20 7a 65 72 6f 48 64 72 5b 32 38 5d 20  har zeroHdr[28] 
11903 3d 20 7b 30 7d 3b 0a 20 20 20 20 20 20 72 63 20  = {0};.      rc 
11904 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65  = sqlite3OsWrite
11905 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 65  (pPager->jfd, ze
11906 72 6f 48 64 72 2c 20 73 69 7a 65 6f 66 28 7a 65  roHdr, sizeof(ze
11907 72 6f 48 64 72 29 2c 20 30 29 3b 0a 20 20 20 20  roHdr), 0);.    
11908 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  }.    if( rc==SQ
11909 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 50 61 67  LITE_OK && !pPag
1190a 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20  er->noSync ){.  
1190b 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1190c 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a  OsSync(pPager->j
1190d 66 64 2c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f  fd, SQLITE_SYNC_
1190e 44 41 54 41 4f 4e 4c 59 7c 70 50 61 67 65 72 2d  DATAONLY|pPager-
1190f 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20  >sync_flags);.  
11910 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20 74    }..    /* At t
11911 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20 74 72  his point the tr
11912 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d  ansaction is com
11913 6d 69 74 74 65 64 20 62 75 74 20 74 68 65 20 77  mitted but the w
11914 72 69 74 65 20 6c 6f 63 6b 20 0a 20 20 20 20 2a  rite lock .    *
11915 2a 20 69 73 20 73 74 69 6c 6c 20 68 65 6c 64 20  * is still held 
11916 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20 49 66 20  on the file. If 
11917 74 68 65 72 65 20 69 73 20 61 20 73 69 7a 65 20  there is a size 
11918 6c 69 6d 69 74 20 63 6f 6e 66 69 67 75 72 65 64  limit configured
11919 20 66 6f 72 20 0a 20 20 20 20 2a 2a 20 74 68 65   for .    ** the
1191a 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75 72   persistent jour
1191b 6e 61 6c 20 61 6e 64 20 74 68 65 20 6a 6f 75 72  nal and the jour
1191c 6e 61 6c 20 66 69 6c 65 20 63 75 72 72 65 6e 74  nal file current
1191d 6c 79 20 63 6f 6e 73 75 6d 65 73 20 6d 6f 72 65  ly consumes more
1191e 0a 20 20 20 20 2a 2a 20 73 70 61 63 65 20 74 68  .    ** space th
1191f 61 6e 20 74 68 61 74 20 6c 69 6d 69 74 20 61 6c  an that limit al
11920 6c 6f 77 73 20 66 6f 72 2c 20 74 72 75 6e 63 61  lows for, trunca
11921 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65 72 65  te it now. There
11922 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 20 20   is no need.    
11923 2a 2a 20 74 6f 20 73 79 6e 63 20 74 68 65 20 66  ** to sync the f
11924 69 6c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  ile following th
11925 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 20  is operation..  
11926 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d    */.    if( rc=
11927 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4c  =SQLITE_OK && iL
11928 69 6d 69 74 3e 30 20 29 7b 0a 20 20 20 20 20 20  imit>0 ){.      
11929 69 36 34 20 73 7a 3b 0a 20 20 20 20 20 20 72 63  i64 sz;.      rc
1192a 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65   = sqlite3OsFile
1192b 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  Size(pPager->jfd
1192c 2c 20 26 73 7a 29 3b 0a 20 20 20 20 20 20 69 66  , &sz);.      if
1192d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1192e 26 26 20 73 7a 3e 69 4c 69 6d 69 74 20 29 7b 0a  && sz>iLimit ){.
1192f 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
11930 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70  ite3OsTruncate(p
11931 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 4c 69 6d  Pager->jfd, iLim
11932 69 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  it);.      }.   
11933 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
11934 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  rc;.}../*.** The
11935 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75   journal file mu
11936 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65 6e 20  st be open when 
11937 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
11938 63 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72 6e 61  called. A journa
11939 6c 0a 2a 2a 20 68 65 61 64 65 72 20 28 4a 4f 55  l.** header (JOU
1193a 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65  RNAL_HDR_SZ byte
1193b 73 29 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e  s) is written in
1193c 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
1193d 69 6c 65 20 61 74 20 74 68 65 0a 2a 2a 20 63 75  ile at the.** cu
1193e 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e 0a  rrent location..
1193f 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74  **.** The format
11940 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c   for the journal
11941 20 68 65 61 64 65 72 20 69 73 20 61 73 20 66 6f   header is as fo
11942 6c 6c 6f 77 73 3a 0a 2a 2a 20 2d 20 38 20 62 79  llows:.** - 8 by
11943 74 65 73 3a 20 4d 61 67 69 63 20 69 64 65 6e 74  tes: Magic ident
11944 69 66 79 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 66  ifying journal f
11945 6f 72 6d 61 74 2e 0a 2a 2a 20 2d 20 34 20 62 79  ormat..** - 4 by
11946 74 65 73 3a 20 4e 75 6d 62 65 72 20 6f 66 20 72  tes: Number of r
11947 65 63 6f 72 64 73 20 69 6e 20 6a 6f 75 72 6e 61  ecords in journa
11948 6c 2c 20 6f 72 20 2d 31 20 6e 6f 2d 73 79 6e 63  l, or -1 no-sync
11949 20 6d 6f 64 65 20 69 73 20 6f 6e 2e 0a 2a 2a 20   mode is on..** 
1194a 2d 20 34 20 62 79 74 65 73 3a 20 52 61 6e 64 6f  - 4 bytes: Rando
1194b 6d 20 6e 75 6d 62 65 72 20 75 73 65 64 20 66 6f  m number used fo
1194c 72 20 70 61 67 65 20 68 61 73 68 2e 0a 2a 2a 20  r page hash..** 
1194d 2d 20 34 20 62 79 74 65 73 3a 20 49 6e 69 74 69  - 4 bytes: Initi
1194e 61 6c 20 64 61 74 61 62 61 73 65 20 70 61 67 65  al database page
1194f 20 63 6f 75 6e 74 2e 0a 2a 2a 20 2d 20 34 20 62   count..** - 4 b
11950 79 74 65 73 3a 20 53 65 63 74 6f 72 20 73 69 7a  ytes: Sector siz
11951 65 20 75 73 65 64 20 62 79 20 74 68 65 20 70 72  e used by the pr
11952 6f 63 65 73 73 20 74 68 61 74 20 77 72 6f 74 65  ocess that wrote
11953 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 0a 2a   this journal..*
11954 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 44 61 74  * - 4 bytes: Dat
11955 61 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 2e  abase page size.
11956 0a 2a 2a 20 0a 2a 2a 20 46 6f 6c 6c 6f 77 65 64  .** .** Followed
11957 20 62 79 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52   by (JOURNAL_HDR
11958 5f 53 5a 20 2d 20 32 38 29 20 62 79 74 65 73 20  _SZ - 28) bytes 
11959 6f 66 20 75 6e 75 73 65 64 20 73 70 61 63 65 2e  of unused space.
1195a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
1195b 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 50  riteJournalHdr(P
1195c 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
1195d 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1195e 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
1195f 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
11960 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ode */.  char *z
11961 48 65 61 64 65 72 20 3d 20 70 50 61 67 65 72 2d  Header = pPager-
11962 3e 70 54 6d 70 53 70 61 63 65 3b 20 20 2f 2a 20  >pTmpSpace;  /* 
11963 54 65 6d 70 6f 72 61 72 79 20 73 70 61 63 65 20  Temporary space 
11964 75 73 65 64 20 74 6f 20 62 75 69 6c 64 20 68 65  used to build he
11965 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e 48  ader */.  u32 nH
11966 65 61 64 65 72 20 3d 20 70 50 61 67 65 72 2d 3e  eader = pPager->
11967 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20 2f 2a  pageSize;     /*
11968 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72 20   Size of buffer 
11969 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a 48  pointed to by zH
1196a 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e  eader */.  u32 n
1196b 57 72 69 74 65 3b 20 20 20 20 20 20 20 20 20 20  Write;          
1196c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1196d 2a 20 42 79 74 65 73 20 6f 66 20 68 65 61 64 65  * Bytes of heade
1196e 72 20 73 65 63 74 6f 72 20 77 72 69 74 74 65 6e  r sector written
1196f 20 2a 2f 0a 20 20 69 6e 74 20 69 69 3b 20 20 20   */.  int ii;   
11970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11971 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
11972 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20  p counter */..  
11973 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70  assert( isOpen(p
11974 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 20 20  Pager->jfd) );  
11975 20 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 66      /* Journal f
11976 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e  ile must be open
11977 2e 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 48 65 61  . */..  if( nHea
11978 64 65 72 3e 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  der>JOURNAL_HDR_
11979 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20  SZ(pPager) ){.  
1197a 20 20 6e 48 65 61 64 65 72 20 3d 20 4a 4f 55 52    nHeader = JOUR
1197b 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
1197c 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  r);.  }..  /* If
1197d 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76   there are activ
1197e 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 6e 64  e savepoints and
1197f 20 61 6e 79 20 6f 66 20 74 68 65 6d 20 77 65 72   any of them wer
11980 65 20 63 72 65 61 74 65 64 20 0a 20 20 2a 2a 20  e created .  ** 
11981 73 69 6e 63 65 20 74 68 65 20 6d 6f 73 74 20 72  since the most r
11982 65 63 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 68 65  ecent journal he
11983 61 64 65 72 20 77 61 73 20 77 72 69 74 74 65 6e  ader was written
11984 2c 20 75 70 64 61 74 65 20 74 68 65 20 0a 20 20  , update the .  
11985 2a 2a 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e  ** PagerSavepoin
11986 74 2e 69 48 64 72 4f 66 66 73 65 74 20 66 69 65  t.iHdrOffset fie
11987 6c 64 73 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20  lds now..  */.  
11988 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61  for(ii=0; ii<pPa
11989 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b  ger->nSavepoint;
1198a 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20   ii++){.    if( 
1198b 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69  pPager->aSavepoi
1198c 6e 74 5b 69 69 5d 2e 69 48 64 72 4f 66 66 73 65  nt[ii].iHdrOffse
1198d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 50  t==0 ){.      pP
1198e 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74  ager->aSavepoint
1198f 5b 69 69 5d 2e 69 48 64 72 4f 66 66 73 65 74 20  [ii].iHdrOffset 
11990 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  = pPager->journa
11991 6c 4f 66 66 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  lOff;.    }.  }.
11992 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  .  pPager->journ
11993 61 6c 48 64 72 20 3d 20 70 50 61 67 65 72 2d 3e  alHdr = pPager->
11994 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f 75  journalOff = jou
11995 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 70 50  rnalHdrOffset(pP
11996 61 67 65 72 29 3b 0a 0a 20 20 2f 2a 20 0a 20 20  ager);..  /* .  
11997 2a 2a 20 57 72 69 74 65 20 74 68 65 20 6e 52 65  ** Write the nRe
11998 63 20 46 69 65 6c 64 20 2d 20 74 68 65 20 6e 75  c Field - the nu
11999 6d 62 65 72 20 6f 66 20 70 61 67 65 20 72 65 63  mber of page rec
1199a 6f 72 64 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77  ords that follow
1199b 20 74 68 69 73 0a 20 20 2a 2a 20 6a 6f 75 72 6e   this.  ** journ
1199c 61 6c 20 68 65 61 64 65 72 2e 20 4e 6f 72 6d 61  al header. Norma
1199d 6c 6c 79 2c 20 7a 65 72 6f 20 69 73 20 77 72 69  lly, zero is wri
1199e 74 74 65 6e 20 74 6f 20 74 68 69 73 20 76 61 6c  tten to this val
1199f 75 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e  ue at this time.
119a0 0a 20 20 2a 2a 20 41 66 74 65 72 20 74 68 65 20  .  ** After the 
119a1 72 65 63 6f 72 64 73 20 61 72 65 20 61 64 64 65  records are adde
119a2 64 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  d to the journal
119a3 20 28 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61   (and the journa
119a4 6c 20 73 79 6e 63 65 64 2c 20 0a 20 20 2a 2a 20  l synced, .  ** 
119a5 69 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20  if in full-sync 
119a6 6d 6f 64 65 29 2c 20 74 68 65 20 7a 65 72 6f 20  mode), the zero 
119a7 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77  is overwritten w
119a8 69 74 68 20 74 68 65 20 74 72 75 65 20 6e 75 6d  ith the true num
119a9 62 65 72 0a 20 20 2a 2a 20 6f 66 20 72 65 63 6f  ber.  ** of reco
119aa 72 64 73 20 28 73 65 65 20 73 79 6e 63 4a 6f 75  rds (see syncJou
119ab 72 6e 61 6c 28 29 29 2e 0a 20 20 2a 2a 0a 20 20  rnal())..  **.  
119ac 2a 2a 20 41 20 66 61 73 74 65 72 20 61 6c 74 65  ** A faster alte
119ad 72 6e 61 74 69 76 65 20 69 73 20 74 6f 20 77 72  rnative is to wr
119ae 69 74 65 20 30 78 46 46 46 46 46 46 46 46 20 74  ite 0xFFFFFFFF t
119af 6f 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64  o the nRec field
119b0 2e 20 57 68 65 6e 0a 20 20 2a 2a 20 72 65 61 64  . When.  ** read
119b1 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ing the journal 
119b2 74 68 69 73 20 76 61 6c 75 65 20 74 65 6c 6c 73  this value tells
119b3 20 53 51 4c 69 74 65 20 74 6f 20 61 73 73 75 6d   SQLite to assum
119b4 65 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20  e that the.  ** 
119b5 72 65 73 74 20 6f 66 20 74 68 65 20 6a 6f 75 72  rest of the jour
119b6 6e 61 6c 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  nal file contain
119b7 73 20 76 61 6c 69 64 20 70 61 67 65 20 72 65 63  s valid page rec
119b8 6f 72 64 73 2e 20 54 68 69 73 20 61 73 73 75 6d  ords. This assum
119b9 70 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 64 61  ption.  ** is da
119ba 6e 67 65 72 6f 75 73 2c 20 61 73 20 69 66 20 61  ngerous, as if a
119bb 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 72 65   failure occurre
119bc 64 20 77 68 69 6c 73 74 20 77 72 69 74 69 6e 67  d whilst writing
119bd 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a   to the journal.
119be 20 20 2a 2a 20 66 69 6c 65 20 69 74 20 6d 61 79    ** file it may
119bf 20 63 6f 6e 74 61 69 6e 20 73 6f 6d 65 20 67 61   contain some ga
119c0 72 62 61 67 65 20 64 61 74 61 2e 20 54 68 65 72  rbage data. Ther
119c1 65 20 61 72 65 20 74 77 6f 20 73 63 65 6e 61 72  e are two scenar
119c2 69 6f 73 0a 20 20 2a 2a 20 77 68 65 72 65 20 74  ios.  ** where t
119c3 68 69 73 20 72 69 73 6b 20 63 61 6e 20 62 65 20  his risk can be 
119c4 69 67 6e 6f 72 65 64 3a 0a 20 20 2a 2a 0a 20 20  ignored:.  **.  
119c5 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74 68 65 20  **   * When the 
119c6 70 61 67 65 72 20 69 73 20 69 6e 20 6e 6f 2d 73  pager is in no-s
119c7 79 6e 63 20 6d 6f 64 65 2e 20 43 6f 72 72 75 70  ync mode. Corrup
119c8 74 69 6f 6e 20 63 61 6e 20 66 6f 6c 6c 6f 77 20  tion can follow 
119c9 61 0a 20 20 2a 2a 20 20 20 20 20 70 6f 77 65 72  a.  **     power
119ca 20 66 61 69 6c 75 72 65 20 69 6e 20 74 68 69 73   failure in this
119cb 20 63 61 73 65 20 61 6e 79 77 61 79 2e 0a 20 20   case anyway..  
119cc 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e  **.  **   * When
119cd 20 74 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41   the SQLITE_IOCA
119ce 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 66 6c  P_SAFE_APPEND fl
119cf 61 67 20 69 73 20 73 65 74 2e 20 54 68 69 73 20  ag is set. This 
119d0 67 75 61 72 61 6e 74 65 65 73 0a 20 20 2a 2a 20  guarantees.  ** 
119d1 20 20 20 20 74 68 61 74 20 67 61 72 62 61 67 65      that garbage
119d2 20 64 61 74 61 20 69 73 20 6e 65 76 65 72 20 61   data is never a
119d3 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 6a  ppended to the j
119d4 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 2a  ournal file..  *
119d5 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70  /.  assert( isOp
119d6 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c  en(pPager->fd) |
119d7 7c 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63  | pPager->noSync
119d8 20 29 3b 0a 20 20 69 66 28 20 28 70 50 61 67 65   );.  if( (pPage
119d9 72 2d 3e 6e 6f 53 79 6e 63 29 20 7c 7c 20 28 70  r->noSync) || (p
119da 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
119db 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
119dc 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 0a 20 20  LMODE_MEMORY).  
119dd 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f 73 44 65   || (sqlite3OsDe
119de 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
119df 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 26  ics(pPager->fd)&
119e0 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46  SQLITE_IOCAP_SAF
119e1 45 5f 41 50 50 45 4e 44 29 20 0a 20 20 29 7b 0a  E_APPEND) .  ){.
119e2 20 20 20 20 6d 65 6d 63 70 79 28 7a 48 65 61 64      memcpy(zHead
119e3 65 72 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  er, aJournalMagi
119e4 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e  c, sizeof(aJourn
119e5 61 6c 4d 61 67 69 63 29 29 3b 0a 20 20 20 20 70  alMagic));.    p
119e6 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65  ut32bits(&zHeade
119e7 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61  r[sizeof(aJourna
119e8 6c 4d 61 67 69 63 29 5d 2c 20 30 78 66 66 66 66  lMagic)], 0xffff
119e9 66 66 66 66 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ffff);.  }else{.
119ea 20 20 20 20 6d 65 6d 73 65 74 28 7a 48 65 61 64      memset(zHead
119eb 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 4a  er, 0, sizeof(aJ
119ec 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 34 29 3b  ournalMagic)+4);
119ed 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 72  .  }..  /* The r
119ee 61 6e 64 6f 6d 20 63 68 65 63 6b 2d 68 61 73 68  andom check-hash
119ef 20 69 6e 69 74 69 61 6c 69 73 65 72 20 2a 2f 20   initialiser */ 
119f0 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f  .  sqlite3_rando
119f1 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 70 50 61  mness(sizeof(pPa
119f2 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 2c  ger->cksumInit),
119f3 20 26 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49   &pPager->cksumI
119f4 6e 69 74 29 3b 0a 20 20 70 75 74 33 32 62 69 74  nit);.  put32bit
119f5 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f  s(&zHeader[sizeo
119f6 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29  f(aJournalMagic)
119f7 2b 34 5d 2c 20 70 50 61 67 65 72 2d 3e 63 6b 73  +4], pPager->cks
119f8 75 6d 49 6e 69 74 29 3b 0a 20 20 2f 2a 20 54 68  umInit);.  /* Th
119f9 65 20 69 6e 69 74 69 61 6c 20 64 61 74 61 62 61  e initial databa
119fa 73 65 20 73 69 7a 65 20 2a 2f 0a 20 20 70 75 74  se size */.  put
119fb 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b  32bits(&zHeader[
119fc 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d  sizeof(aJournalM
119fd 61 67 69 63 29 2b 38 5d 2c 20 70 50 61 67 65 72  agic)+8], pPager
119fe 2d 3e 64 62 4f 72 69 67 53 69 7a 65 29 3b 0a 20  ->dbOrigSize);. 
119ff 20 2f 2a 20 54 68 65 20 61 73 73 75 6d 65 64 20   /* The assumed 
11a00 73 65 63 74 6f 72 20 73 69 7a 65 20 66 6f 72 20  sector size for 
11a01 74 68 69 73 20 70 72 6f 63 65 73 73 20 2a 2f 0a  this process */.
11a02 20 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65    put32bits(&zHe
11a03 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75  ader[sizeof(aJou
11a04 72 6e 61 6c 4d 61 67 69 63 29 2b 31 32 5d 2c 20  rnalMagic)+12], 
11a05 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69  pPager->sectorSi
11a06 7a 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 70  ze);..  /* The p
11a07 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 70 75  age size */.  pu
11a08 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 72  t32bits(&zHeader
11a09 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c  [sizeof(aJournal
11a0a 4d 61 67 69 63 29 2b 31 36 5d 2c 20 70 50 61 67  Magic)+16], pPag
11a0b 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 0a  er->pageSize);..
11a0c 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 69 6e    /* Initializin
11a0d 67 20 74 68 65 20 74 61 69 6c 20 6f 66 20 74 68  g the tail of th
11a0e 65 20 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20  e buffer is not 
11a0f 6e 65 63 65 73 73 61 72 79 2e 20 20 45 76 65 72  necessary.  Ever
11a10 79 74 68 69 6e 67 0a 20 20 2a 2a 20 77 6f 72 6b  ything.  ** work
11a11 73 20 66 69 6e 64 20 69 66 20 74 68 65 20 66 6f  s find if the fo
11a12 6c 6c 6f 77 69 6e 67 20 6d 65 6d 73 65 74 28 29  llowing memset()
11a13 20 69 73 20 6f 6d 69 74 74 65 64 2e 20 20 42 75   is omitted.  Bu
11a14 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 0a 20  t initializing. 
11a15 20 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 70   ** the memory p
11a16 72 65 76 65 6e 74 73 20 76 61 6c 67 72 69 6e 64  revents valgrind
11a17 20 66 72 6f 6d 20 63 6f 6d 70 6c 61 69 6e 69 6e   from complainin
11a18 67 2c 20 73 6f 20 77 65 20 61 72 65 20 77 69 6c  g, so we are wil
11a19 6c 69 6e 67 20 74 6f 0a 20 20 2a 2a 20 74 61 6b  ling to.  ** tak
11a1a 65 20 74 68 65 20 70 65 72 66 6f 72 6d 61 6e 63  e the performanc
11a1b 65 20 68 69 74 2e 0a 20 20 2a 2f 0a 20 20 6d 65  e hit..  */.  me
11a1c 6d 73 65 74 28 26 7a 48 65 61 64 65 72 5b 73 69  mset(&zHeader[si
11a1d 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67  zeof(aJournalMag
11a1e 69 63 29 2b 32 30 5d 2c 20 30 2c 0a 20 20 20 20  ic)+20], 0,.    
11a1f 20 20 20 20 20 6e 48 65 61 64 65 72 2d 28 73 69       nHeader-(si
11a20 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67  zeof(aJournalMag
11a21 69 63 29 2b 32 30 29 29 3b 0a 0a 20 20 2f 2a 20  ic)+20));..  /* 
11a22 49 6e 20 74 68 65 6f 72 79 2c 20 69 74 20 69 73  In theory, it is
11a23 20 6f 6e 6c 79 20 6e 65 63 65 73 73 61 72 79 20   only necessary 
11a24 74 6f 20 77 72 69 74 65 20 74 68 65 20 32 38 20  to write the 28 
11a25 62 79 74 65 73 20 74 68 61 74 20 74 68 65 20 0a  bytes that the .
11a26 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 68 65 61    ** journal hea
11a27 64 65 72 20 63 6f 6e 73 75 6d 65 73 20 74 6f 20  der consumes to 
11a28 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
11a29 20 68 65 72 65 2e 20 54 68 65 6e 20 69 6e 63 72   here. Then incr
11a2a 65 6d 65 6e 74 20 74 68 65 20 0a 20 20 2a 2a 20  ement the .  ** 
11a2b 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66  Pager.journalOff
11a2c 20 76 61 72 69 61 62 6c 65 20 62 79 20 4a 4f 55   variable by JOU
11a2d 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 73 6f 20 74  RNAL_HDR_SZ so t
11a2e 68 61 74 20 74 68 65 20 6e 65 78 74 20 0a 20 20  hat the next .  
11a2f 2a 2a 20 72 65 63 6f 72 64 20 69 73 20 77 72 69  ** record is wri
11a30 74 74 65 6e 20 74 6f 20 74 68 65 20 66 6f 6c 6c  tten to the foll
11a31 6f 77 69 6e 67 20 73 65 63 74 6f 72 20 28 6c 65  owing sector (le
11a32 61 76 69 6e 67 20 61 20 67 61 70 20 69 6e 20 74  aving a gap in t
11a33 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 74 68 61  he file.  ** tha
11a34 74 20 77 69 6c 6c 20 62 65 20 69 6d 70 6c 69 63  t will be implic
11a35 69 74 6c 79 20 66 69 6c 6c 65 64 20 69 6e 20 62  itly filled in b
11a36 79 20 74 68 65 20 4f 53 29 2e 0a 20 20 2a 2a 0a  y the OS)..  **.
11a37 20 20 2a 2a 20 48 6f 77 65 76 65 72 20 69 74 20    ** However it 
11a38 68 61 73 20 62 65 65 6e 20 64 69 73 63 6f 76 65  has been discove
11a39 72 65 64 20 74 68 61 74 20 6f 6e 20 73 6f 6d 65  red that on some
11a3a 20 73 79 73 74 65 6d 73 20 74 68 69 73 20 70 61   systems this pa
11a3b 74 74 65 72 6e 20 63 61 6e 20 0a 20 20 2a 2a 20  ttern can .  ** 
11a3c 62 65 20 73 69 67 6e 69 66 69 63 61 6e 74 6c 79  be significantly
11a3d 20 73 6c 6f 77 65 72 20 74 68 61 6e 20 63 6f 6e   slower than con
11a3e 74 69 67 75 6f 75 73 6c 79 20 77 72 69 74 69 6e  tiguously writin
11a3f 67 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 69  g data to the fi
11a40 6c 65 2c 0a 20 20 2a 2a 20 65 76 65 6e 20 69 66  le,.  ** even if
11a41 20 74 68 61 74 20 6d 65 61 6e 73 20 65 78 70 6c   that means expl
11a42 69 63 69 74 6c 79 20 77 72 69 74 69 6e 67 20 64  icitly writing d
11a43 61 74 61 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b  ata to the block
11a44 20 6f 66 20 0a 20 20 2a 2a 20 28 4a 4f 55 52 4e   of .  ** (JOURN
11a45 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 38 29 20  AL_HDR_SZ - 28) 
11a46 62 79 74 65 73 20 74 68 61 74 20 77 69 6c 6c 20  bytes that will 
11a47 6e 6f 74 20 62 65 20 75 73 65 64 2e 20 53 6f 20  not be used. So 
11a48 74 68 61 74 20 69 73 20 77 68 61 74 0a 20 20 2a  that is what.  *
11a49 2a 20 69 73 20 64 6f 6e 65 2e 20 0a 20 20 2a 2a  * is done. .  **
11a4a 0a 20 20 2a 2a 20 54 68 65 20 6c 6f 6f 70 20 69  .  ** The loop i
11a4b 73 20 72 65 71 75 69 72 65 64 20 68 65 72 65 20  s required here 
11a4c 69 6e 20 63 61 73 65 20 74 68 65 20 73 65 63 74  in case the sect
11a4d 6f 72 2d 73 69 7a 65 20 69 73 20 6c 61 72 67 65  or-size is large
11a4e 72 20 74 68 61 6e 20 74 68 65 20 0a 20 20 2a 2a  r than the .  **
11a4f 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 73   database page s
11a50 69 7a 65 2e 20 53 69 6e 63 65 20 74 68 65 20 7a  ize. Since the z
11a51 48 65 61 64 65 72 20 62 75 66 66 65 72 20 69 73  Header buffer is
11a52 20 6f 6e 6c 79 20 50 61 67 65 72 2e 70 61 67 65   only Pager.page
11a53 53 69 7a 65 0a 20 20 2a 2a 20 62 79 74 65 73 20  Size.  ** bytes 
11a54 69 6e 20 73 69 7a 65 2c 20 6d 6f 72 65 20 74 68  in size, more th
11a55 61 6e 20 6f 6e 65 20 63 61 6c 6c 20 74 6f 20 73  an one call to s
11a56 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 29 20  qlite3OsWrite() 
11a57 6d 61 79 20 62 65 20 72 65 71 75 69 72 65 64 0a  may be required.
11a58 20 20 2a 2a 20 74 6f 20 70 6f 70 75 6c 61 74 65    ** to populate
11a59 20 74 68 65 20 65 6e 74 69 72 65 20 6a 6f 75 72   the entire jour
11a5a 6e 61 6c 20 68 65 61 64 65 72 20 73 65 63 74 6f  nal header secto
11a5b 72 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 28 6e  r..  */ .  for(n
11a5c 57 72 69 74 65 3d 30 3b 20 72 63 3d 3d 53 51 4c  Write=0; rc==SQL
11a5d 49 54 45 5f 4f 4b 26 26 6e 57 72 69 74 65 3c 4a  ITE_OK&&nWrite<J
11a5e 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
11a5f 61 67 65 72 29 3b 20 6e 57 72 69 74 65 2b 3d 6e  ager); nWrite+=n
11a60 48 65 61 64 65 72 29 7b 0a 20 20 20 20 49 4f 54  Header){.    IOT
11a61 52 41 43 45 28 28 22 4a 48 44 52 20 25 70 20 25  RACE(("JHDR %p %
11a62 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61 67 65  lld %d\n", pPage
11a63 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  r, pPager->journ
11a64 61 6c 48 64 72 2c 20 6e 48 65 61 64 65 72 29 29  alHdr, nHeader))
11a65 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
11a66 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d  3OsWrite(pPager-
11a67 3e 6a 66 64 2c 20 7a 48 65 61 64 65 72 2c 20 6e  >jfd, zHeader, n
11a68 48 65 61 64 65 72 2c 20 70 50 61 67 65 72 2d 3e  Header, pPager->
11a69 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 20 20 20  journalOff);.   
11a6a 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
11a6b 4f 66 66 20 2b 3d 20 6e 48 65 61 64 65 72 3b 0a  Off += nHeader;.
11a6c 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
11a6d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a  ;.}../*.** The j
11a6e 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74  ournal file must
11a6f 20 62 65 20 6f 70 65 6e 20 77 68 65 6e 20 74 68   be open when th
11a70 69 73 20 69 73 20 63 61 6c 6c 65 64 2e 20 41 20  is is called. A 
11a71 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66  journal header f
11a72 69 6c 65 0a 2a 2a 20 28 4a 4f 55 52 4e 41 4c 5f  ile.** (JOURNAL_
11a73 48 44 52 5f 53 5a 20 62 79 74 65 73 29 20 69 73  HDR_SZ bytes) is
11a74 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 63   read from the c
11a75 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20  urrent location 
11a76 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a  in the journal.*
11a77 2a 20 66 69 6c 65 2e 20 54 68 65 20 63 75 72 72  * file. The curr
11a78 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 69 6e 20  ent location in 
11a79 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
11a7a 20 69 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a 20   is given by.** 
11a7b 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
11a7c 66 66 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73  ff. See comments
11a7d 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f 6e 20   above function 
11a7e 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28  writeJournalHdr(
11a7f 29 20 66 6f 72 0a 2a 2a 20 61 20 64 65 73 63 72  ) for.** a descr
11a80 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 6a 6f  iption of the jo
11a81 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 6f 72  urnal header for
11a82 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  mat..**.** If th
11a83 65 20 68 65 61 64 65 72 20 69 73 20 72 65 61 64  e header is read
11a84 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 2a   successfully, *
11a85 70 4e 52 65 63 20 69 73 20 73 65 74 20 74 6f 20  pNRec is set to 
11a86 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  the number of.**
11a87 20 70 61 67 65 20 72 65 63 6f 72 64 73 20 66 6f   page records fo
11a88 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 68 65 61  llowing this hea
11a89 64 65 72 20 61 6e 64 20 2a 70 44 62 53 69 7a 65  der and *pDbSize
11a8a 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 73   is set to the s
11a8b 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61  ize of the.** da
11a8c 74 61 62 61 73 65 20 62 65 66 6f 72 65 20 74 68  tabase before th
11a8d 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65  e transaction be
11a8e 67 61 6e 2c 20 69 6e 20 70 61 67 65 73 2e 20 41  gan, in pages. A
11a8f 6c 73 6f 2c 20 70 50 61 67 65 72 2d 3e 63 6b 73  lso, pPager->cks
11a90 75 6d 49 6e 69 74 0a 2a 2a 20 69 73 20 73 65 74  umInit.** is set
11a91 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 72 65   to the value re
11a92 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72  ad from the jour
11a93 6e 61 6c 20 68 65 61 64 65 72 2e 20 53 51 4c 49  nal header. SQLI
11a94 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
11a95 64 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 73  d.** in this cas
11a96 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
11a97 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66  journal header f
11a98 69 6c 65 20 61 70 70 65 61 72 73 20 74 6f 20 62  ile appears to b
11a99 65 20 63 6f 72 72 75 70 74 65 64 2c 20 53 51 4c  e corrupted, SQL
11a9a 49 54 45 5f 44 4f 4e 45 20 69 73 0a 2a 2a 20 72  ITE_DONE is.** r
11a9b 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 4e 52  eturned and *pNR
11a9c 65 63 20 61 6e 64 20 2a 50 44 62 53 69 7a 65 20  ec and *PDbSize 
11a9d 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 20 20  are undefined.  
11a9e 49 66 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53  If JOURNAL_HDR_S
11a9f 5a 20 62 79 74 65 73 0a 2a 2a 20 63 61 6e 6e 6f  Z bytes.** canno
11aa0 74 20 62 65 20 72 65 61 64 20 66 72 6f 6d 20 74  t be read from t
11aa1 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11aa2 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  an error code is
11aa3 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74   returned..*/.st
11aa4 61 74 69 63 20 69 6e 74 20 72 65 61 64 4a 6f 75  atic int readJou
11aa5 72 6e 61 6c 48 64 72 28 0a 20 20 50 61 67 65 72  rnalHdr(.  Pager
11aa6 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 20   *pPager,       
11aa7 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72          /* Pager
11aa8 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74   object */.  int
11aa9 20 69 73 48 6f 74 2c 0a 20 20 69 36 34 20 6a 6f   isHot,.  i64 jo
11aaa 75 72 6e 61 6c 53 69 7a 65 2c 20 20 20 20 20 20  urnalSize,      
11aab 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
11aac 66 20 74 68 65 20 6f 70 65 6e 20 6a 6f 75 72 6e  f the open journ
11aad 61 6c 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73  al file in bytes
11aae 20 2a 2f 0a 20 20 75 33 32 20 2a 70 4e 52 65 63   */.  u32 *pNRec
11aaf 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
11ab0 20 20 20 2f 2a 20 4f 55 54 3a 20 56 61 6c 75 65     /* OUT: Value
11ab1 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6e   read from the n
11ab2 52 65 63 20 66 69 65 6c 64 20 2a 2f 0a 20 20 75  Rec field */.  u
11ab3 33 32 20 2a 70 44 62 53 69 7a 65 20 20 20 20 20  32 *pDbSize     
11ab4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
11ab5 55 54 3a 20 56 61 6c 75 65 20 6f 66 20 6f 72 69  UT: Value of ori
11ab6 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 73  ginal database s
11ab7 69 7a 65 20 66 69 65 6c 64 20 2a 2f 0a 29 7b 0a  ize field */.){.
11ab8 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
11ab9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11aba 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
11abb 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
11abc 20 61 4d 61 67 69 63 5b 38 5d 3b 20 20 20 20 20   aMagic[8];     
11abd 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f 20 68  /* A buffer to h
11abe 6f 6c 64 20 74 68 65 20 6d 61 67 69 63 20 68 65  old the magic he
11abf 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20 69 48  ader */.  i64 iH
11ac0 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20  drOff;          
11ac1 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
11ac2 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64   of journal head
11ac3 65 72 20 62 65 69 6e 67 20 72 65 61 64 20 2a 2f  er being read */
11ac4 0a 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70  ..  assert( isOp
11ac5 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
11ac6 29 3b 20 20 20 20 20 20 2f 2a 20 4a 6f 75 72 6e  );      /* Journ
11ac7 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20  al file must be 
11ac8 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 2f 2a 20 41  open. */..  /* A
11ac9 64 76 61 6e 63 65 20 50 61 67 65 72 2e 6a 6f 75  dvance Pager.jou
11aca 72 6e 61 6c 4f 66 66 20 74 6f 20 74 68 65 20 73  rnalOff to the s
11acb 74 61 72 74 20 6f 66 20 74 68 65 20 6e 65 78 74  tart of the next
11acc 20 73 65 63 74 6f 72 2e 20 49 66 20 74 68 65 0a   sector. If the.
11acd 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c    ** journal fil
11ace 65 20 69 73 20 74 6f 6f 20 73 6d 61 6c 6c 20 66  e is too small f
11acf 6f 72 20 74 68 65 72 65 20 74 6f 20 62 65 20 61  or there to be a
11ad0 20 68 65 61 64 65 72 20 73 74 6f 72 65 64 20 61   header stored a
11ad1 74 20 74 68 69 73 0a 20 20 2a 2a 20 70 6f 69 6e  t this.  ** poin
11ad2 74 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  t, return SQLITE
11ad3 5f 44 4f 4e 45 2e 0a 20 20 2a 2f 0a 20 20 70 50  _DONE..  */.  pP
11ad4 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
11ad5 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66   = journalHdrOff
11ad6 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 69  set(pPager);.  i
11ad7 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  f( pPager->journ
11ad8 61 6c 4f 66 66 2b 4a 4f 55 52 4e 41 4c 5f 48 44  alOff+JOURNAL_HD
11ad9 52 5f 53 5a 28 70 50 61 67 65 72 29 20 3e 20 6a  R_SZ(pPager) > j
11ada 6f 75 72 6e 61 6c 53 69 7a 65 20 29 7b 0a 20 20  ournalSize ){.  
11adb 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
11adc 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 48 64 72  DONE;.  }.  iHdr
11add 4f 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f  Off = pPager->jo
11ade 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20  urnalOff;..  /* 
11adf 52 65 61 64 20 69 6e 20 74 68 65 20 66 69 72 73  Read in the firs
11ae0 74 20 38 20 62 79 74 65 73 20 6f 66 20 74 68 65  t 8 bytes of the
11ae1 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e   journal header.
11ae2 20 49 66 20 74 68 65 79 20 64 6f 20 6e 6f 74 20   If they do not 
11ae3 6d 61 74 63 68 0a 20 20 2a 2a 20 74 68 65 20 20  match.  ** the  
11ae4 6d 61 67 69 63 20 73 74 72 69 6e 67 20 66 6f 75  magic string fou
11ae5 6e 64 20 61 74 20 74 68 65 20 73 74 61 72 74 20  nd at the start 
11ae6 6f 66 20 65 61 63 68 20 6a 6f 75 72 6e 61 6c 20  of each journal 
11ae7 68 65 61 64 65 72 2c 20 72 65 74 75 72 6e 0a 20  header, return. 
11ae8 20 2a 2a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2e   ** SQLITE_DONE.
11ae9 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20   If an IO error 
11aea 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 61  occurs, return a
11aeb 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 4f 74  n error code. Ot
11aec 68 65 72 77 69 73 65 2c 0a 20 20 2a 2a 20 70 72  herwise,.  ** pr
11aed 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  oceed..  */.  if
11aee 28 20 69 73 48 6f 74 20 7c 7c 20 69 48 64 72 4f  ( isHot || iHdrO
11aef 66 66 21 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72  ff!=pPager->jour
11af0 6e 61 6c 48 64 72 20 29 7b 0a 20 20 20 20 72 63  nalHdr ){.    rc
11af1 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64   = sqlite3OsRead
11af2 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 61 4d  (pPager->jfd, aM
11af3 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4d 61  agic, sizeof(aMa
11af4 67 69 63 29 2c 20 69 48 64 72 4f 66 66 29 3b 0a  gic), iHdrOff);.
11af5 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
11af6 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
11af7 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65 6d     }.    if( mem
11af8 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75  cmp(aMagic, aJou
11af9 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 6f  rnalMagic, sizeo
11afa 66 28 61 4d 61 67 69 63 29 29 21 3d 30 20 29 7b  f(aMagic))!=0 ){
11afb 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
11afc 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d  LITE_DONE;.    }
11afd 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20  .  }..  /* Read 
11afe 74 68 65 20 66 69 72 73 74 20 74 68 72 65 65 20  the first three 
11aff 33 32 2d 62 69 74 20 66 69 65 6c 64 73 20 6f 66  32-bit fields of
11b00 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61   the journal hea
11b01 64 65 72 3a 20 54 68 65 20 6e 52 65 63 0a 20 20  der: The nRec.  
11b02 2a 2a 20 66 69 65 6c 64 2c 20 74 68 65 20 63 68  ** field, the ch
11b03 65 63 6b 73 75 6d 2d 69 6e 69 74 69 61 6c 69 7a  ecksum-initializ
11b04 65 72 20 61 6e 64 20 74 68 65 20 64 61 74 61 62  er and the datab
11b05 61 73 65 20 73 69 7a 65 20 61 74 20 74 68 65 20  ase size at the 
11b06 73 74 61 72 74 0a 20 20 2a 2a 20 6f 66 20 74 68  start.  ** of th
11b07 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 52  e transaction. R
11b08 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63  eturn an error c
11b09 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20  ode if anything 
11b0a 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 20 20 2a 2f  goes wrong..  */
11b0b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
11b0c 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 69  !=(rc = read32bi
11b0d 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  ts(pPager->jfd, 
11b0e 69 48 64 72 4f 66 66 2b 38 2c 20 70 4e 52 65 63  iHdrOff+8, pNRec
11b0f 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f  )).   || SQLITE_
11b10 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32  OK!=(rc = read32
11b11 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64  bits(pPager->jfd
11b12 2c 20 69 48 64 72 4f 66 66 2b 31 32 2c 20 26 70  , iHdrOff+12, &p
11b13 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74  Pager->cksumInit
11b14 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f  )).   || SQLITE_
11b15 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32  OK!=(rc = read32
11b16 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64  bits(pPager->jfd
11b17 2c 20 69 48 64 72 4f 66 66 2b 31 36 2c 20 70 44  , iHdrOff+16, pD
11b18 62 53 69 7a 65 29 29 0a 20 20 29 7b 0a 20 20 20  bSize)).  ){.   
11b19 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
11b1a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a  .  if( pPager->j
11b1b 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a  ournalOff==0 ){.
11b1c 20 20 20 20 75 33 32 20 69 50 61 67 65 53 69 7a      u32 iPageSiz
11b1d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
11b1e 20 2f 2a 20 50 61 67 65 2d 73 69 7a 65 20 66 69   /* Page-size fi
11b1f 65 6c 64 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68  eld of journal h
11b20 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 75 33 32  eader */.    u32
11b21 20 69 53 65 63 74 6f 72 53 69 7a 65 3b 20 20 20   iSectorSize;   
11b22 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63            /* Sec
11b23 74 6f 72 2d 73 69 7a 65 20 66 69 65 6c 64 20 6f  tor-size field o
11b24 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  f journal header
11b25 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 50 61 67   */.    u16 iPag
11b26 65 53 69 7a 65 31 36 3b 20 20 20 20 20 20 20 20  eSize16;        
11b27 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20       /* Copy of 
11b28 69 50 61 67 65 53 69 7a 65 20 69 6e 20 31 36 2d  iPageSize in 16-
11b29 62 69 74 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a  bit variable */.
11b2a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65  .    /* Read the
11b2b 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 73   page-size and s
11b2c 65 63 74 6f 72 2d 73 69 7a 65 20 6a 6f 75 72 6e  ector-size journ
11b2d 61 6c 20 68 65 61 64 65 72 20 66 69 65 6c 64 73  al header fields
11b2e 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 53 51 4c  . */.    if( SQL
11b2f 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65  ITE_OK!=(rc = re
11b30 61 64 33 32 62 69 74 73 28 70 50 61 67 65 72 2d  ad32bits(pPager-
11b31 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 32 30  >jfd, iHdrOff+20
11b32 2c 20 26 69 53 65 63 74 6f 72 53 69 7a 65 29 29  , &iSectorSize))
11b33 0a 20 20 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f  .     || SQLITE_
11b34 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32  OK!=(rc = read32
11b35 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64  bits(pPager->jfd
11b36 2c 20 69 48 64 72 4f 66 66 2b 32 34 2c 20 26 69  , iHdrOff+24, &i
11b37 50 61 67 65 53 69 7a 65 29 29 0a 20 20 20 20 29  PageSize)).    )
11b38 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
11b39 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  c;.    }..    /*
11b3a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20   Check that the 
11b3b 76 61 6c 75 65 73 20 72 65 61 64 20 66 72 6f 6d  values read from
11b3c 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61   the page-size a
11b3d 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 66  nd sector-size f
11b3e 69 65 6c 64 73 0a 20 20 20 20 2a 2a 20 61 72 65  ields.    ** are
11b3f 20 77 69 74 68 69 6e 20 72 61 6e 67 65 2e 20 54   within range. T
11b40 6f 20 62 65 20 27 69 6e 20 72 61 6e 67 65 27 2c  o be 'in range',
11b41 20 62 6f 74 68 20 76 61 6c 75 65 73 20 6e 65 65   both values nee
11b42 64 20 74 6f 20 62 65 20 61 20 70 6f 77 65 72 0a  d to be a power.
11b43 20 20 20 20 2a 2a 20 6f 66 20 74 77 6f 20 67 72      ** of two gr
11b44 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71  eater than or eq
11b45 75 61 6c 20 74 6f 20 35 31 32 2c 20 61 6e 64 20  ual to 512, and 
11b46 6e 6f 74 20 67 72 65 61 74 65 72 20 74 68 61 6e  not greater than
11b47 20 74 68 65 69 72 20 0a 20 20 20 20 2a 2a 20 72   their .    ** r
11b48 65 73 70 65 63 74 69 76 65 20 63 6f 6d 70 69 6c  espective compil
11b49 65 20 74 69 6d 65 20 6d 61 78 69 6d 75 6d 20 6c  e time maximum l
11b4a 69 6d 69 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  imits..    */.  
11b4b 20 20 69 66 28 20 69 50 61 67 65 53 69 7a 65 3c    if( iPageSize<
11b4c 35 31 32 20 20 20 20 20 20 20 20 20 20 20 20 20  512             
11b4d 20 20 20 20 20 7c 7c 20 69 53 65 63 74 6f 72 53       || iSectorS
11b4e 69 7a 65 3c 35 31 32 0a 20 20 20 20 20 7c 7c 20  ize<512.     || 
11b4f 69 50 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45  iPageSize>SQLITE
11b50 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 7c  _MAX_PAGE_SIZE |
11b51 7c 20 69 53 65 63 74 6f 72 53 69 7a 65 3e 4d 41  | iSectorSize>MA
11b52 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 0a 20 20  X_SECTOR_SIZE.  
11b53 20 20 20 7c 7c 20 28 28 69 50 61 67 65 53 69 7a     || ((iPageSiz
11b54 65 2d 31 29 26 69 50 61 67 65 53 69 7a 65 29 21  e-1)&iPageSize)!
11b55 3d 30 20 20 20 7c 7c 20 28 28 69 53 65 63 74 6f  =0   || ((iSecto
11b56 72 53 69 7a 65 2d 31 29 26 69 53 65 63 74 6f 72  rSize-1)&iSector
11b57 53 69 7a 65 29 21 3d 30 20 0a 20 20 20 20 29 7b  Size)!=0 .    ){
11b58 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
11b59 20 65 69 74 68 65 72 20 74 68 65 20 70 61 67 65   either the page
11b5a 2d 73 69 7a 65 20 6f 72 20 73 65 63 74 6f 72 2d  -size or sector-
11b5b 73 69 7a 65 20 69 6e 20 74 68 65 20 6a 6f 75 72  size in the jour
11b5c 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20 0a 20  nal-header is . 
11b5d 20 20 20 20 20 2a 2a 20 69 6e 76 61 6c 69 64 2c       ** invalid,
11b5e 20 74 68 65 6e 20 74 68 65 20 70 72 6f 63 65 73   then the proces
11b5f 73 20 74 68 61 74 20 77 72 6f 74 65 20 74 68 65  s that wrote the
11b60 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20   journal-header 
11b61 6d 75 73 74 20 68 61 76 65 20 0a 20 20 20 20 20  must have .     
11b62 20 2a 2a 20 63 72 61 73 68 65 64 20 62 65 66 6f   ** crashed befo
11b63 72 65 20 74 68 65 20 68 65 61 64 65 72 20 77 61  re the header wa
11b64 73 20 73 79 6e 63 65 64 2e 20 49 6e 20 74 68 69  s synced. In thi
11b65 73 20 63 61 73 65 20 73 74 6f 70 20 72 65 61 64  s case stop read
11b66 69 6e 67 20 0a 20 20 20 20 20 20 2a 2a 20 74 68  ing .      ** th
11b67 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68  e journal file h
11b68 65 72 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ere..      */.  
11b69 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
11b6a 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20  E_DONE;.    }.. 
11b6b 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65     /* Update the
11b6c 20 70 61 67 65 2d 73 69 7a 65 20 74 6f 20 6d 61   page-size to ma
11b6d 74 63 68 20 74 68 65 20 76 61 6c 75 65 20 72 65  tch the value re
11b6e 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72  ad from the jour
11b6f 6e 61 6c 2e 20 0a 20 20 20 20 2a 2a 20 55 73 65  nal. .    ** Use
11b70 20 61 20 74 65 73 74 63 61 73 65 28 29 20 6d 61   a testcase() ma
11b71 63 72 6f 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  cro to make sure
11b72 20 74 68 61 74 20 6d 61 6c 6c 6f 63 20 66 61 69   that malloc fai
11b73 6c 75 72 65 20 77 69 74 68 69 6e 20 0a 20 20 20  lure within .   
11b74 20 2a 2a 20 50 61 67 65 72 53 65 74 50 61 67 65   ** PagerSetPage
11b75 73 69 7a 65 28 29 20 69 73 20 74 65 73 74 65 64  size() is tested
11b76 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 50 61  ..    */.    iPa
11b77 67 65 53 69 7a 65 31 36 20 3d 20 28 75 31 36 29  geSize16 = (u16)
11b78 69 50 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 72  iPageSize;.    r
11b79 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
11b7a 53 65 74 50 61 67 65 73 69 7a 65 28 70 50 61 67  SetPagesize(pPag
11b7b 65 72 2c 20 26 69 50 61 67 65 53 69 7a 65 31 36  er, &iPageSize16
11b7c 2c 20 2d 31 29 3b 0a 20 20 20 20 74 65 73 74 63  , -1);.    testc
11b7d 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ase( rc!=SQLITE_
11b7e 4f 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  OK );.    assert
11b7f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
11b80 7c 7c 20 69 50 61 67 65 53 69 7a 65 31 36 3d 3d  || iPageSize16==
11b81 28 75 31 36 29 69 50 61 67 65 53 69 7a 65 20 29  (u16)iPageSize )
11b82 3b 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 65  ;..    /* Update
11b83 20 74 68 65 20 61 73 73 75 6d 65 64 20 73 65 63   the assumed sec
11b84 74 6f 72 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63  tor-size to matc
11b85 68 20 74 68 65 20 76 61 6c 75 65 20 75 73 65 64  h the value used
11b86 20 62 79 20 0a 20 20 20 20 2a 2a 20 74 68 65 20   by .    ** the 
11b87 70 72 6f 63 65 73 73 20 74 68 61 74 20 63 72 65  process that cre
11b88 61 74 65 64 20 74 68 69 73 20 6a 6f 75 72 6e 61  ated this journa
11b89 6c 2e 20 49 66 20 74 68 69 73 20 6a 6f 75 72 6e  l. If this journ
11b8a 61 6c 20 77 61 73 0a 20 20 20 20 2a 2a 20 63 72  al was.    ** cr
11b8b 65 61 74 65 64 20 62 79 20 61 20 70 72 6f 63 65  eated by a proce
11b8c 73 73 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68  ss other than th
11b8d 69 73 20 6f 6e 65 2c 20 74 68 65 6e 20 74 68 69  is one, then thi
11b8e 73 20 72 6f 75 74 69 6e 65 0a 20 20 20 20 2a 2a  s routine.    **
11b8f 20 69 73 20 62 65 69 6e 67 20 63 61 6c 6c 65 64   is being called
11b90 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 70 61 67   from within pag
11b91 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 2e 20 54  er_playback(). T
11b92 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65 0a 20  he local value. 
11b93 20 20 20 2a 2a 20 6f 66 20 50 61 67 65 72 2e 73     ** of Pager.s
11b94 65 63 74 6f 72 53 69 7a 65 20 69 73 20 72 65 73  ectorSize is res
11b95 74 6f 72 65 64 20 61 74 20 74 68 65 20 65 6e 64  tored at the end
11b96 20 6f 66 20 74 68 61 74 20 72 6f 75 74 69 6e 65   of that routine
11b97 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61  ..    */.    pPa
11b98 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20  ger->sectorSize 
11b99 3d 20 69 53 65 63 74 6f 72 53 69 7a 65 3b 0a 20  = iSectorSize;. 
11b9a 20 7d 0a 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f   }..  pPager->jo
11b9b 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 4a 4f 55 52  urnalOff += JOUR
11b9c 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
11b9d 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  r);.  return rc;
11b9e 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  .}.../*.** Write
11b9f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 6d 61   the supplied ma
11ba0 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d  ster journal nam
11ba1 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e  e into the journ
11ba2 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65  al file for page
11ba3 72 0a 2a 2a 20 70 50 61 67 65 72 20 61 74 20 74  r.** pPager at t
11ba4 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74  he current locat
11ba5 69 6f 6e 2e 20 54 68 65 20 6d 61 73 74 65 72 20  ion. The master 
11ba6 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 6d 75 73  journal name mus
11ba7 74 20 62 65 20 74 68 65 20 6c 61 73 74 0a 2a 2a  t be the last.**
11ba8 20 74 68 69 6e 67 20 77 72 69 74 74 65 6e 20 74   thing written t
11ba9 6f 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  o a journal file
11baa 2e 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69  . If the pager i
11bab 73 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d  s in full-sync m
11bac 6f 64 65 2c 20 74 68 65 0a 2a 2a 20 6a 6f 75 72  ode, the.** jour
11bad 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70  nal file descrip
11bae 74 6f 72 20 69 73 20 61 64 76 61 6e 63 65 64 20  tor is advanced 
11baf 74 6f 20 74 68 65 20 6e 65 78 74 20 73 65 63 74  to the next sect
11bb0 6f 72 20 62 6f 75 6e 64 61 72 79 20 62 65 66 6f  or boundary befo
11bb1 72 65 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 69  re.** anything i
11bb2 73 20 77 72 69 74 74 65 6e 2e 20 54 68 65 20 66  s written. The f
11bb3 6f 72 6d 61 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20  ormat is:.**.** 
11bb4 20 20 2b 20 34 20 62 79 74 65 73 3a 20 50 41 47    + 4 bytes: PAG
11bb5 45 52 5f 4d 4a 5f 50 47 4e 4f 2e 0a 2a 2a 20 20  ER_MJ_PGNO..**  
11bb6 20 2b 20 4e 20 62 79 74 65 73 3a 20 4d 61 73 74   + N bytes: Mast
11bb7 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e  er journal filen
11bb8 61 6d 65 20 69 6e 20 75 74 66 2d 38 2e 0a 2a 2a  ame in utf-8..**
11bb9 20 20 20 2b 20 34 20 62 79 74 65 73 3a 20 4e 20     + 4 bytes: N 
11bba 28 6c 65 6e 67 74 68 20 6f 66 20 6d 61 73 74 65  (length of maste
11bbb 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69  r journal name i
11bbc 6e 20 62 79 74 65 73 2c 20 6e 6f 20 6e 75 6c 2d  n bytes, no nul-
11bbd 74 65 72 6d 69 6e 61 74 6f 72 29 2e 0a 2a 2a 20  terminator)..** 
11bbe 20 20 2b 20 34 20 62 79 74 65 73 3a 20 4d 61 73    + 4 bytes: Mas
11bbf 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65  ter journal name
11bc0 20 63 68 65 63 6b 73 75 6d 2e 0a 2a 2a 20 20 20   checksum..**   
11bc1 2b 20 38 20 62 79 74 65 73 3a 20 61 4a 6f 75 72  + 8 bytes: aJour
11bc2 6e 61 6c 4d 61 67 69 63 5b 5d 2e 0a 2a 2a 0a 2a  nalMagic[]..**.*
11bc3 2a 20 54 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  * The master jou
11bc4 72 6e 61 6c 20 70 61 67 65 20 63 68 65 63 6b 73  rnal page checks
11bc5 75 6d 20 69 73 20 74 68 65 20 73 75 6d 20 6f 66  um is the sum of
11bc6 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 74 68   the bytes in th
11bc7 65 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72  e master.** jour
11bc8 6e 61 6c 20 6e 61 6d 65 2c 20 77 68 65 72 65 20  nal name, where 
11bc9 65 61 63 68 20 62 79 74 65 20 69 73 20 69 6e 74  each byte is int
11bca 65 72 70 72 65 74 65 64 20 61 73 20 61 20 73 69  erpreted as a si
11bcb 67 6e 65 64 20 38 2d 62 69 74 20 69 6e 74 65 67  gned 8-bit integ
11bcc 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 4d 61  er..**.** If zMa
11bcd 73 74 65 72 20 69 73 20 61 20 4e 55 4c 4c 20 70  ster is a NULL p
11bce 6f 69 6e 74 65 72 20 28 6f 63 63 75 72 73 20 66  ointer (occurs f
11bcf 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61  or a single data
11bd0 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  base transaction
11bd1 29 2c 20 0a 2a 2a 20 74 68 69 73 20 63 61 6c 6c  ), .** this call
11bd2 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a   is a no-op..*/.
11bd3 73 74 61 74 69 63 20 69 6e 74 20 77 72 69 74 65  static int write
11bd4 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 50 61  MasterJournal(Pa
11bd5 67 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e  ger *pPager, con
11bd6 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72  st char *zMaster
11bd7 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  ){.  int rc;    
11bd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11bd9 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
11bda 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d  code */.  int nM
11bdb 61 73 74 65 72 3b 20 20 20 20 20 20 20 20 20 20  aster;          
11bdc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
11bdd 6e 67 74 68 20 6f 66 20 73 74 72 69 6e 67 20 7a  ngth of string z
11bde 4d 61 73 74 65 72 20 2a 2f 0a 20 20 69 36 34 20  Master */.  i64 
11bdf 69 48 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20  iHdrOff;        
11be0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11be1 4f 66 66 73 65 74 20 6f 66 20 68 65 61 64 65 72  Offset of header
11be2 20 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65   in journal file
11be3 20 2a 2f 0a 20 20 69 36 34 20 6a 72 6e 6c 53 69   */.  i64 jrnlSi
11be4 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
11be5 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
11be6 66 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f  f journal file o
11be7 6e 20 64 69 73 6b 20 2a 2f 0a 20 20 75 33 32 20  n disk */.  u32 
11be8 63 6b 73 75 6d 20 3d 20 30 3b 20 20 20 20 20 20  cksum = 0;      
11be9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11bea 43 68 65 63 6b 73 75 6d 20 6f 66 20 73 74 72 69  Checksum of stri
11beb 6e 67 20 7a 4d 61 73 74 65 72 20 2a 2f 0a 0a 20  ng zMaster */.. 
11bec 20 69 66 28 20 21 7a 4d 61 73 74 65 72 20 7c 7c   if( !zMaster ||
11bed 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74   pPager->setMast
11bee 65 72 0a 20 20 20 7c 7c 20 70 50 61 67 65 72 2d  er.   || pPager-
11bef 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41  >journalMode==PA
11bf0 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
11bf1 4d 45 4d 4f 52 59 20 0a 20 20 20 7c 7c 20 70 50  MEMORY .   || pP
11bf2 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
11bf3 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
11bf4 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 29 7b 0a 20  MODE_OFF .  ){. 
11bf5 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
11bf6 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65  _OK;.  }.  pPage
11bf7 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 31  r->setMaster = 1
11bf8 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70  ;.  assert( isOp
11bf9 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
11bfa 29 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61  );..  /* Calcula
11bfb 74 65 20 74 68 65 20 6c 65 6e 67 74 68 20 69 6e  te the length in
11bfc 20 62 79 74 65 73 20 61 6e 64 20 74 68 65 20 63   bytes and the c
11bfd 68 65 63 6b 73 75 6d 20 6f 66 20 7a 4d 61 73 74  hecksum of zMast
11bfe 65 72 20 2a 2f 0a 20 20 66 6f 72 28 6e 4d 61 73  er */.  for(nMas
11bff 74 65 72 3d 30 3b 20 7a 4d 61 73 74 65 72 5b 6e  ter=0; zMaster[n
11c00 4d 61 73 74 65 72 5d 3b 20 6e 4d 61 73 74 65 72  Master]; nMaster
11c01 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d 20 2b  ++){.    cksum +
11c02 3d 20 7a 4d 61 73 74 65 72 5b 6e 4d 61 73 74 65  = zMaster[nMaste
11c03 72 5d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  r];.  }..  /* If
11c04 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f   in full-sync mo
11c05 64 65 2c 20 61 64 76 61 6e 63 65 20 74 6f 20 74  de, advance to t
11c06 68 65 20 6e 65 78 74 20 64 69 73 6b 20 73 65 63  he next disk sec
11c07 74 6f 72 20 62 65 66 6f 72 65 20 77 72 69 74 69  tor before writi
11c08 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6d 61 73 74  ng.  ** the mast
11c09 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e  er journal name.
11c0a 20 54 68 69 73 20 69 73 20 69 6e 20 63 61 73 65   This is in case
11c0b 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 61   the previous pa
11c0c 67 65 20 77 72 69 74 74 65 6e 20 74 6f 0a 20 20  ge written to.  
11c0d 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68  ** the journal h
11c0e 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
11c0f 73 79 6e 63 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  synced..  */.  i
11c10 66 28 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53  f( pPager->fullS
11c11 79 6e 63 20 29 7b 0a 20 20 20 20 70 50 61 67 65  ync ){.    pPage
11c12 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20  r->journalOff = 
11c13 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74  journalHdrOffset
11c14 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20  (pPager);.  }.  
11c15 69 48 64 72 4f 66 66 20 3d 20 70 50 61 67 65 72  iHdrOff = pPager
11c16 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20  ->journalOff;.. 
11c17 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6d 61   /* Write the ma
11c18 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 64 61 74  ster journal dat
11c19 61 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  a to the end of 
11c1a 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
11c1b 2e 20 49 66 0a 20 20 2a 2a 20 61 6e 20 65 72 72  . If.  ** an err
11c1c 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72  or occurs, retur
11c1d 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  n the error code
11c1e 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a   to the caller..
11c1f 20 20 2a 2f 0a 20 20 69 66 28 20 28 30 20 21 3d    */.  if( (0 !=
11c20 20 28 72 63 20 3d 20 77 72 69 74 65 33 32 62 69   (rc = write32bi
11c21 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  ts(pPager->jfd, 
11c22 69 48 64 72 4f 66 66 2c 20 50 41 47 45 52 5f 4d  iHdrOff, PAGER_M
11c23 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 29 29  J_PGNO(pPager)))
11c24 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 72  ).   || (0 != (r
11c25 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69  c = sqlite3OsWri
11c26 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  te(pPager->jfd, 
11c27 7a 4d 61 73 74 65 72 2c 20 6e 4d 61 73 74 65 72  zMaster, nMaster
11c28 2c 20 69 48 64 72 4f 66 66 2b 34 29 29 29 0a 20  , iHdrOff+4))). 
11c29 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d    || (0 != (rc =
11c2a 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61   write32bits(pPa
11c2b 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66  ger->jfd, iHdrOf
11c2c 66 2b 34 2b 6e 4d 61 73 74 65 72 2c 20 6e 4d 61  f+4+nMaster, nMa
11c2d 73 74 65 72 29 29 29 0a 20 20 20 7c 7c 20 28 30  ster))).   || (0
11c2e 20 21 3d 20 28 72 63 20 3d 20 77 72 69 74 65 33   != (rc = write3
11c2f 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66  2bits(pPager->jf
11c30 64 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61  d, iHdrOff+4+nMa
11c31 73 74 65 72 2b 34 2c 20 63 6b 73 75 6d 29 29 29  ster+4, cksum)))
11c32 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 63  .   || (0 != (rc
11c33 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
11c34 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 61  e(pPager->jfd, a
11c35 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 38 2c  JournalMagic, 8,
11c36 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61 73 74   iHdrOff+4+nMast
11c37 65 72 2b 38 29 29 29 0a 20 20 29 7b 0a 20 20 20  er+8))).  ){.   
11c38 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
11c39 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
11c3a 6c 4f 66 66 20 2b 3d 20 28 6e 4d 61 73 74 65 72  lOff += (nMaster
11c3b 2b 32 30 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e  +20);.  pPager->
11c3c 6e 65 65 64 53 79 6e 63 20 3d 20 21 70 50 61 67  needSync = !pPag
11c3d 65 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 0a 20 20 2f  er->noSync;..  /
11c3e 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69  * If the pager i
11c3f 73 20 69 6e 20 70 65 72 69 73 74 65 6e 74 2d 6a  s in peristent-j
11c40 6f 75 72 6e 61 6c 20 6d 6f 64 65 2c 20 74 68 65  ournal mode, the
11c41 6e 20 74 68 65 20 70 68 79 73 69 63 61 6c 20 0a  n the physical .
11c42 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c    ** journal-fil
11c43 65 20 6d 61 79 20 65 78 74 65 6e 64 20 70 61 73  e may extend pas
11c44 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
11c45 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20   master-journal 
11c46 6e 61 6d 65 0a 20 20 2a 2a 20 61 6e 64 20 38 20  name.  ** and 8 
11c47 62 79 74 65 73 20 6f 66 20 6d 61 67 69 63 20 64  bytes of magic d
11c48 61 74 61 20 6a 75 73 74 20 77 72 69 74 74 65 6e  ata just written
11c49 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 54 68   to the file. Th
11c4a 69 73 20 69 73 20 0a 20 20 2a 2a 20 64 61 6e 67  is is .  ** dang
11c4b 65 72 6f 75 73 20 62 65 63 61 75 73 65 20 74 68  erous because th
11c4c 65 20 63 6f 64 65 20 74 6f 20 72 6f 6c 6c 62 61  e code to rollba
11c4d 63 6b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c  ck a hot-journal
11c4e 20 66 69 6c 65 0a 20 20 2a 2a 20 77 69 6c 6c 20   file.  ** will 
11c4f 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 66  not be able to f
11c50 69 6e 64 20 74 68 65 20 6d 61 73 74 65 72 2d 6a  ind the master-j
11c51 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 74 6f 20 64  ournal name to d
11c52 65 74 65 72 6d 69 6e 65 20 0a 20 20 2a 2a 20 77  etermine .  ** w
11c53 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
11c54 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 68 6f 74  e journal is hot
11c55 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 61 73  . .  **.  ** Eas
11c56 69 65 73 74 20 74 68 69 6e 67 20 74 6f 20 64 6f  iest thing to do
11c57 20 69 6e 20 74 68 69 73 20 73 63 65 6e 61 72 69   in this scenari
11c58 6f 20 69 73 20 74 6f 20 74 72 75 6e 63 61 74 65  o is to truncate
11c59 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20   the journal .  
11c5a 2a 2a 20 66 69 6c 65 20 74 6f 20 74 68 65 20 72  ** file to the r
11c5b 65 71 75 69 72 65 64 20 73 69 7a 65 2e 0a 20 20  equired size..  
11c5c 2a 2f 20 0a 20 20 69 66 28 20 53 51 4c 49 54 45  */ .  if( SQLITE
11c5d 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74  _OK==(rc = sqlit
11c5e 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61  e3OsFileSize(pPa
11c5f 67 65 72 2d 3e 6a 66 64 2c 20 26 6a 72 6e 6c 53  ger->jfd, &jrnlS
11c60 69 7a 65 29 29 0a 20 20 20 26 26 20 6a 72 6e 6c  ize)).   && jrnl
11c61 53 69 7a 65 3e 70 50 61 67 65 72 2d 3e 6a 6f 75  Size>pPager->jou
11c62 72 6e 61 6c 4f 66 66 0a 20 20 29 7b 0a 20 20 20  rnalOff.  ){.   
11c63 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54   rc = sqlite3OsT
11c64 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e  runcate(pPager->
11c65 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75  jfd, pPager->jou
11c66 72 6e 61 6c 4f 66 66 29 3b 0a 20 20 7d 0a 20 20  rnalOff);.  }.  
11c67 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
11c68 0a 2a 2a 20 46 69 6e 64 20 61 20 70 61 67 65 20  .** Find a page 
11c69 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  in the hash tabl
11c6a 65 20 67 69 76 65 6e 20 69 74 73 20 70 61 67 65  e given its page
11c6b 20 6e 75 6d 62 65 72 2e 20 52 65 74 75 72 6e 0a   number. Return.
11c6c 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
11c6d 74 68 65 20 70 61 67 65 20 6f 72 20 4e 55 4c 4c  the page or NULL
11c6e 20 69 66 20 74 68 65 20 72 65 71 75 65 73 74 65   if the requeste
11c6f 64 20 70 61 67 65 20 69 73 20 6e 6f 74 20 0a 2a  d page is not .*
11c70 2a 20 61 6c 72 65 61 64 79 20 69 6e 20 6d 65 6d  * already in mem
11c71 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50  ory..*/.static P
11c72 67 48 64 72 20 2a 70 61 67 65 72 5f 6c 6f 6f 6b  gHdr *pager_look
11c73 75 70 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  up(Pager *pPager
11c74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20  , Pgno pgno){.  
11c75 50 67 48 64 72 20 2a 70 3b 20 20 20 20 20 20 20  PgHdr *p;       
11c76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11c77 20 20 2f 2a 20 52 65 74 75 72 6e 20 76 61 6c 75    /* Return valu
11c78 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 74 20 69 73  e */..  /* It is
11c79 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f   not possible fo
11c7a 72 20 61 20 63 61 6c 6c 20 74 6f 20 50 63 61 63  r a call to Pcac
11c7b 68 65 46 65 74 63 68 28 29 20 77 69 74 68 20 63  heFetch() with c
11c7c 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 74 6f 0a  reateFlag==0 to.
11c7d 20 20 2a 2a 20 66 61 69 6c 2c 20 73 69 6e 63 65    ** fail, since
11c7e 20 6e 6f 20 61 74 74 65 6d 70 74 20 74 6f 20 61   no attempt to a
11c7f 6c 6c 6f 63 61 74 65 20 64 79 6e 61 6d 69 63 20  llocate dynamic 
11c80 6d 65 6d 6f 72 79 20 77 69 6c 6c 20 62 65 20 6d  memory will be m
11c81 61 64 65 2e 0a 20 20 2a 2f 0a 20 20 28 76 6f 69  ade..  */.  (voi
11c82 64 29 73 71 6c 69 74 65 33 50 63 61 63 68 65 46  d)sqlite3PcacheF
11c83 65 74 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43  etch(pPager->pPC
11c84 61 63 68 65 2c 20 70 67 6e 6f 2c 20 30 2c 20 26  ache, pgno, 0, &
11c85 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a  p);.  return p;.
11c86 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20  }../*.** Unless 
11c87 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20  the pager is in 
11c88 65 72 72 6f 72 2d 73 74 61 74 65 2c 20 64 69 73  error-state, dis
11c89 63 61 72 64 20 61 6c 6c 20 69 6e 2d 6d 65 6d 6f  card all in-memo
11c8a 72 79 20 70 61 67 65 73 2e 20 49 66 0a 2a 2a 20  ry pages. If.** 
11c8b 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20  the pager is in 
11c8c 65 72 72 6f 72 2d 73 74 61 74 65 2c 20 74 68 65  error-state, the
11c8d 6e 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 61  n this call is a
11c8e 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 4f   no-op..**.** TO
11c8f 44 4f 3a 20 57 68 79 20 63 61 6e 20 77 65 20 6e  DO: Why can we n
11c90 6f 74 20 72 65 73 65 74 20 74 68 65 20 70 61 67  ot reset the pag
11c91 65 72 20 77 68 69 6c 65 20 69 6e 20 65 72 72 6f  er while in erro
11c92 72 20 73 74 61 74 65 3f 0a 2a 2f 0a 73 74 61 74  r state?.*/.stat
11c93 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f 72 65  ic void pager_re
11c94 73 65 74 28 50 61 67 65 72 20 2a 70 50 61 67 65  set(Pager *pPage
11c95 72 29 7b 0a 20 20 69 66 28 20 53 51 4c 49 54 45  r){.  if( SQLITE
11c96 5f 4f 4b 3d 3d 70 50 61 67 65 72 2d 3e 65 72 72  _OK==pPager->err
11c97 43 6f 64 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  Code ){.    sqli
11c98 74 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74  te3BackupRestart
11c99 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70  (pPager->pBackup
11c9a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63  );.    sqlite3Pc
11c9b 61 63 68 65 43 6c 65 61 72 28 70 50 61 67 65 72  acheClear(pPager
11c9c 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 20 20  ->pPCache);.    
11c9d 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61  pPager->dbSizeVa
11c9e 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a  lid = 0;.  }.}..
11c9f 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6c 6c 20 73  /*.** Free all s
11ca0 74 72 75 63 74 75 72 65 73 20 69 6e 20 74 68 65  tructures in the
11ca1 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e   Pager.aSavepoin
11ca2 74 5b 5d 20 61 72 72 61 79 20 61 6e 64 20 73 65  t[] array and se
11ca3 74 20 62 6f 74 68 0a 2a 2a 20 50 61 67 65 72 2e  t both.** Pager.
11ca4 61 53 61 76 65 70 6f 69 6e 74 20 61 6e 64 20 50  aSavepoint and P
11ca5 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 6e 74 20  ager.nSavepoint 
11ca6 74 6f 20 7a 65 72 6f 2e 20 43 6c 6f 73 65 20 74  to zero. Close t
11ca7 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a  he sub-journal.*
11ca8 2a 20 69 66 20 69 74 20 69 73 20 6f 70 65 6e 20  * if it is open 
11ca9 61 6e 64 20 74 68 65 20 70 61 67 65 72 20 69 73  and the pager is
11caa 20 6e 6f 74 20 69 6e 20 65 78 63 6c 75 73 69 76   not in exclusiv
11cab 65 20 6d 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69  e mode..*/.stati
11cac 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 41 6c  c void releaseAl
11cad 6c 53 61 76 65 70 6f 69 6e 74 73 28 50 61 67 65  lSavepoints(Page
11cae 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e  r *pPager){.  in
11caf 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20  t ii;           
11cb0 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20      /* Iterator 
11cb1 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f  for looping thro
11cb2 75 67 68 20 50 61 67 65 72 2e 61 53 61 76 65 70  ugh Pager.aSavep
11cb3 6f 69 6e 74 20 2a 2f 0a 20 20 66 6f 72 28 69 69  oint */.  for(ii
11cb4 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e  =0; ii<pPager->n
11cb5 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29  Savepoint; ii++)
11cb6 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69 74  {.    sqlite3Bit
11cb7 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65  vecDestroy(pPage
11cb8 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69  r->aSavepoint[ii
11cb9 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 29 3b  ].pInSavepoint);
11cba 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67  .  }.  if( !pPag
11cbb 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64  er->exclusiveMod
11cbc 65 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 4d 65  e || sqlite3IsMe
11cbd 6d 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2d  mJournal(pPager-
11cbe 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 73 71  >sjfd) ){.    sq
11cbf 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61  lite3OsClose(pPa
11cc0 67 65 72 2d 3e 73 6a 66 64 29 3b 0a 20 20 7d 0a  ger->sjfd);.  }.
11cc1 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
11cc2 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e  Pager->aSavepoin
11cc3 74 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 61 53  t);.  pPager->aS
11cc4 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20  avepoint = 0;.  
11cc5 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69  pPager->nSavepoi
11cc6 6e 74 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72  nt = 0;.  pPager
11cc7 2d 3e 6e 53 75 62 52 65 63 20 3d 20 30 3b 0a 7d  ->nSubRec = 0;.}
11cc8 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
11cc9 62 69 74 20 6e 75 6d 62 65 72 20 70 67 6e 6f 20  bit number pgno 
11cca 69 6e 20 74 68 65 20 50 61 67 65 72 53 61 76 65  in the PagerSave
11ccb 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69  point.pInSavepoi
11ccc 6e 74 20 0a 2a 2a 20 62 69 74 76 65 63 73 20 6f  nt .** bitvecs o
11ccd 66 20 61 6c 6c 20 6f 70 65 6e 20 73 61 76 65 70  f all open savep
11cce 6f 69 6e 74 73 2e 20 52 65 74 75 72 6e 20 53 51  oints. Return SQ
11ccf 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75 63 63 65  LITE_OK if succe
11cd0 73 73 66 75 6c 0a 2a 2a 20 6f 72 20 53 51 4c 49  ssful.** or SQLI
11cd1 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 61  TE_NOMEM if a ma
11cd2 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 63  lloc failure occ
11cd3 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  urs..*/.static i
11cd4 6e 74 20 61 64 64 54 6f 53 61 76 65 70 6f 69 6e  nt addToSavepoin
11cd5 74 42 69 74 76 65 63 73 28 50 61 67 65 72 20 2a  tBitvecs(Pager *
11cd6 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e  pPager, Pgno pgn
11cd7 6f 29 7b 0a 20 20 69 6e 74 20 69 69 3b 20 20 20  o){.  int ii;   
11cd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11cd9 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
11cda 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  */.  int rc = SQ
11cdb 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 2f  LITE_OK;       /
11cdc 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 2a 2f  * Result code */
11cdd 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ..  for(ii=0; ii
11cde 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f  <pPager->nSavepo
11cdf 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  int; ii++){.    
11ce0 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a  PagerSavepoint *
11ce1 70 20 3d 20 26 70 50 61 67 65 72 2d 3e 61 53 61  p = &pPager->aSa
11ce2 76 65 70 6f 69 6e 74 5b 69 69 5d 3b 0a 20 20 20  vepoint[ii];.   
11ce3 20 69 66 28 20 70 67 6e 6f 3c 3d 70 2d 3e 6e 4f   if( pgno<=p->nO
11ce4 72 69 67 20 29 7b 0a 20 20 20 20 20 20 72 63 20  rig ){.      rc 
11ce5 7c 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  |= sqlite3Bitvec
11ce6 53 65 74 28 70 2d 3e 70 49 6e 53 61 76 65 70 6f  Set(p->pInSavepo
11ce7 69 6e 74 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20  int, pgno);.    
11ce8 20 20 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d    testcase( rc==
11ce9 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a  SQLITE_NOMEM );.
11cea 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
11ceb 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72  ==SQLITE_OK || r
11cec 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
11ced 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  );.    }.  }.  r
11cee 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
11cef 2a 2a 20 55 6e 6c 6f 63 6b 20 74 68 65 20 64 61  ** Unlock the da
11cf0 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69  tabase file. Thi
11cf1 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20  s function is a 
11cf2 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 70 61 67  no-op if the pag
11cf3 65 72 0a 2a 2a 20 69 73 20 69 6e 20 65 78 63 6c  er.** is in excl
11cf4 75 73 69 76 65 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a  usive mode..**.*
11cf5 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69  * If the pager i
11cf6 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 65  s currently in e
11cf7 72 72 6f 72 20 73 74 61 74 65 2c 20 64 69 73 63  rror state, disc
11cf8 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ard the contents
11cf9 20 6f 66 20 0a 2a 2a 20 74 68 65 20 63 61 63 68   of .** the cach
11cfa 65 20 61 6e 64 20 72 65 73 65 74 20 74 68 65 20  e and reset the 
11cfb 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65 20  Pager structure 
11cfc 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 2e 20  internal state. 
11cfd 49 66 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 61  If there is.** a
11cfe 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 2d 66  n open journal-f
11cff 69 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 6e 65  ile, then the ne
11d00 78 74 20 74 69 6d 65 20 61 20 73 68 61 72 65 64  xt time a shared
11d01 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65  -lock is obtaine
11d02 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 70 61 67 65  d.** on the page
11d03 72 20 66 69 6c 65 20 28 62 79 20 74 68 69 73 20  r file (by this 
11d04 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f  or any other pro
11d05 63 65 73 73 29 2c 20 69 74 20 77 69 6c 6c 20 62  cess), it will b
11d06 65 0a 2a 2a 20 74 72 65 61 74 65 64 20 61 73 20  e.** treated as 
11d07 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e  a hot-journal an
11d08 64 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a  d rolled back..*
11d09 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61  /.static void pa
11d0a 67 65 72 5f 75 6e 6c 6f 63 6b 28 50 61 67 65 72  ger_unlock(Pager
11d0b 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28   *pPager){.  if(
11d0c 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73   !pPager->exclus
11d0d 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 69  iveMode ){.    i
11d0e 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
11d0f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
11d10 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20  eturn code */.. 
11d11 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 63 6c 6f     /* Always clo
11d12 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  se the journal f
11d13 69 6c 65 20 77 68 65 6e 20 64 72 6f 70 70 69 6e  ile when droppin
11d14 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c  g the database l
11d15 6f 63 6b 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65  ock..    ** Othe
11d16 72 77 69 73 65 2c 20 61 6e 6f 74 68 65 72 20 63  rwise, another c
11d17 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 20 6a  onnection with j
11d18 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 64 65 6c 65  ournal_mode=dele
11d19 74 65 20 6d 69 67 68 74 0a 20 20 20 20 2a 2a 20  te might.    ** 
11d1a 64 65 6c 65 74 65 20 74 68 65 20 66 69 6c 65 20  delete the file 
11d1b 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 75  out from under u
11d1c 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  s..    */.    sq
11d1d 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61  lite3OsClose(pPa
11d1e 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 73  ger->jfd);.    s
11d1f 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74  qlite3BitvecDest
11d20 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a  roy(pPager->pInJ
11d21 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 70 50 61  ournal);.    pPa
11d22 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20  ger->pInJournal 
11d23 3d 20 30 3b 0a 20 20 20 20 72 65 6c 65 61 73 65  = 0;.    release
11d24 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 70 50  AllSavepoints(pP
11d25 61 67 65 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 49  ager);..    /* I
11d26 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 75 6e  f the file is un
11d27 6c 6f 63 6b 65 64 2c 20 73 6f 6d 65 62 6f 64 79  locked, somebody
11d28 20 65 6c 73 65 20 6d 69 67 68 74 20 63 68 61 6e   else might chan
11d29 67 65 20 69 74 2e 20 54 68 65 0a 20 20 20 20 2a  ge it. The.    *
11d2a 2a 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20  * values stored 
11d2b 69 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65 20  in Pager.dbSize 
11d2c 65 74 63 2e 20 6d 69 67 68 74 20 62 65 63 6f 6d  etc. might becom
11d2d 65 20 69 6e 76 61 6c 69 64 20 69 66 0a 20 20 20  e invalid if.   
11d2e 20 2a 2a 20 74 68 69 73 20 68 61 70 70 65 6e 73   ** this happens
11d2f 2e 20 54 4f 44 4f 3a 20 52 65 61 6c 6c 79 2c 20  . TODO: Really, 
11d30 74 68 69 73 20 64 6f 65 73 6e 27 74 20 6e 65 65  this doesn't nee
11d31 64 20 74 6f 20 62 65 20 63 6c 65 61 72 65 64 0a  d to be cleared.
11d32 20 20 20 20 2a 2a 20 75 6e 74 69 6c 20 74 68 65      ** until the
11d33 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20   change-counter 
11d34 63 68 65 63 6b 20 66 61 69 6c 73 20 69 6e 20 50  check fails in P
11d35 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 29  agerSharedLock()
11d36 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61  ..    */.    pPa
11d37 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64  ger->dbSizeValid
11d38 20 3d 20 30 3b 0a 0a 20 20 20 20 72 63 20 3d 20   = 0;..    rc = 
11d39 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 72 2d  osUnlock(pPager-
11d3a 3e 66 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20  >fd, NO_LOCK);. 
11d3b 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
11d3c 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f     pPager->errCo
11d3d 64 65 20 3d 20 72 63 3b 0a 20 20 20 20 7d 0a 20  de = rc;.    }. 
11d3e 20 20 20 49 4f 54 52 41 43 45 28 28 22 55 4e 4c     IOTRACE(("UNL
11d3f 4f 43 4b 20 25 70 5c 6e 22 2c 20 70 50 61 67 65  OCK %p\n", pPage
11d40 72 29 29 0a 0a 20 20 20 20 2f 2a 20 49 66 20 50  r))..    /* If P
11d41 61 67 65 72 2e 65 72 72 43 6f 64 65 20 69 73 20  ager.errCode is 
11d42 73 65 74 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74  set, the content
11d43 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 63  s of the pager c
11d44 61 63 68 65 20 63 61 6e 6e 6f 74 20 62 65 0a 20  ache cannot be. 
11d45 20 20 20 2a 2a 20 74 72 75 73 74 65 64 2e 20 4e     ** trusted. N
11d46 6f 77 20 74 68 61 74 20 74 68 65 20 70 61 67 65  ow that the page
11d47 72 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b  r file is unlock
11d48 65 64 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ed, the contents
11d49 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 63   of the.    ** c
11d4a 61 63 68 65 20 63 61 6e 20 62 65 20 64 69 73 63  ache can be disc
11d4b 61 72 64 65 64 20 61 6e 64 20 74 68 65 20 65 72  arded and the er
11d4c 72 6f 72 20 63 6f 64 65 20 73 61 66 65 6c 79 20  ror code safely 
11d4d 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 2a 2f 0a  cleared..    */.
11d4e 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
11d4f 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 20  errCode ){.     
11d50 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
11d51 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50  OK ){.        pP
11d52 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20  ager->errCode = 
11d53 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
11d54 20 7d 0a 20 20 20 20 20 20 70 61 67 65 72 5f 72   }.      pager_r
11d55 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20  eset(pPager);.  
11d56 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65 72 2d    }..    pPager-
11d57 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65  >changeCountDone
11d58 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72   = 0;.    pPager
11d59 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f  ->state = PAGER_
11d5a 55 4e 4c 4f 43 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f  UNLOCK;.  }.}../
11d5b 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
11d5c 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c  on should be cal
11d5d 6c 65 64 20 77 68 65 6e 20 61 6e 20 49 4f 45 52  led when an IOER
11d5e 52 2c 20 43 4f 52 52 55 50 54 20 6f 72 20 46 55  R, CORRUPT or FU
11d5f 4c 4c 20 65 72 72 6f 72 0a 2a 2a 20 6d 61 79 20  LL error.** may 
11d60 68 61 76 65 20 6f 63 63 75 72 72 65 64 2e 20 54  have occurred. T
11d61 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
11d62 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  t is a pointer t
11d63 6f 20 74 68 65 20 70 61 67 65 72 20 0a 2a 2a 20  o the pager .** 
11d64 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 20 73  structure, the s
11d65 65 63 6f 6e 64 20 74 68 65 20 65 72 72 6f 72 2d  econd the error-
11d66 63 6f 64 65 20 61 62 6f 75 74 20 74 6f 20 62 65  code about to be
11d67 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 20 70   returned by a p
11d68 61 67 65 72 20 0a 2a 2a 20 41 50 49 20 66 75 6e  ager .** API fun
11d69 63 74 69 6f 6e 2e 20 54 68 65 20 76 61 6c 75 65  ction. The value
11d6a 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 20 63   returned is a c
11d6b 6f 70 79 20 6f 66 20 74 68 65 20 73 65 63 6f 6e  opy of the secon
11d6c 64 20 61 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 74  d argument .** t
11d6d 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  o this function.
11d6e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73   .**.** If the s
11d6f 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69  econd argument i
11d70 73 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20  s SQLITE_IOERR, 
11d71 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2c 20  SQLITE_CORRUPT, 
11d72 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c 0a 2a  or SQLITE_FULL.*
11d73 2a 20 74 68 65 20 65 72 72 6f 72 20 62 65 63 6f  * the error beco
11d74 6d 65 73 20 70 65 72 73 69 73 74 65 6e 74 2e 20  mes persistent. 
11d75 55 6e 74 69 6c 20 74 68 65 20 70 65 72 73 69 73  Until the persis
11d76 74 65 6e 20 65 72 72 6f 72 20 69 73 20 63 6c 65  ten error is cle
11d77 61 72 65 64 2c 0a 2a 2a 20 73 75 62 73 65 71 75  ared,.** subsequ
11d78 65 6e 74 20 41 50 49 20 63 61 6c 6c 73 20 6f 6e  ent API calls on
11d79 20 74 68 69 73 20 50 61 67 65 72 20 77 69 6c 6c   this Pager will
11d7a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 72 65 74   immediately ret
11d7b 75 72 6e 20 74 68 65 20 73 61 6d 65 20 0a 2a 2a  urn the same .**
11d7c 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a   error code..**.
11d7d 2a 2a 20 41 20 70 65 72 73 69 73 74 65 6e 74 20  ** A persistent 
11d7e 65 72 72 6f 72 20 69 6e 64 69 63 61 74 65 73 20  error indicates 
11d7f 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74  that the content
11d80 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63  s of the pager-c
11d81 61 63 68 65 20 0a 2a 2a 20 63 61 6e 6e 6f 74 20  ache .** cannot 
11d82 62 65 20 74 72 75 73 74 65 64 2e 20 54 68 69 73  be trusted. This
11d83 20 73 74 61 74 65 20 63 61 6e 20 62 65 20 63 6c   state can be cl
11d84 65 61 72 65 64 20 62 79 20 63 6f 6d 70 6c 65 74  eared by complet
11d85 65 6c 79 20 64 69 73 63 61 72 64 69 6e 67 20 0a  ely discarding .
11d86 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  ** the contents 
11d87 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63 61 63  of the pager-cac
11d88 68 65 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63  he. If a transac
11d89 74 69 6f 6e 20 77 61 73 20 61 63 74 69 76 65 20  tion was active 
11d8a 77 68 65 6e 0a 2a 2a 20 74 68 65 20 70 65 72 73  when.** the pers
11d8b 69 73 74 65 6e 74 20 65 72 72 6f 72 20 6f 63 63  istent error occ
11d8c 75 72 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20  urred, then the 
11d8d 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
11d8e 20 6d 61 79 20 6e 65 65 64 0a 2a 2a 20 74 6f 20   may need.** to 
11d8f 62 65 20 72 65 70 6c 61 79 65 64 20 74 6f 20 72  be replayed to r
11d90 65 73 74 6f 72 65 20 74 68 65 20 63 6f 6e 74 65  estore the conte
11d91 6e 74 73 20 6f 66 20 74 68 65 20 64 61 74 61 62  nts of the datab
11d92 61 73 65 20 66 69 6c 65 20 28 61 73 20 69 66 0a  ase file (as if.
11d93 2a 2a 20 69 74 20 77 65 72 65 20 61 20 68 6f 74  ** it were a hot
11d94 2d 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2f 0a 73 74  -journal)..*/.st
11d95 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 65  atic int pager_e
11d96 72 72 6f 72 28 50 61 67 65 72 20 2a 70 50 61 67  rror(Pager *pPag
11d97 65 72 2c 20 69 6e 74 20 72 63 29 7b 0a 20 20 69  er, int rc){.  i
11d98 6e 74 20 72 63 32 20 3d 20 72 63 20 26 20 30 78  nt rc2 = rc & 0x
11d99 66 66 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63  ff;.  assert( rc
11d9a 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21  ==SQLITE_OK || !
11d9b 4d 45 4d 44 42 20 29 3b 0a 20 20 61 73 73 65 72  MEMDB );.  asser
11d9c 74 28 0a 20 20 20 20 20 20 20 70 50 61 67 65 72  t(.       pPager
11d9d 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54  ->errCode==SQLIT
11d9e 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20 20 20 20 20  E_FULL ||.      
11d9f 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
11da0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 0a 20  ==SQLITE_OK ||. 
11da1 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e 65        (pPager->e
11da2 72 72 43 6f 64 65 20 26 20 30 78 66 66 29 3d 3d  rrCode & 0xff)==
11da3 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 29  SQLITE_IOERR.  )
11da4 3b 0a 20 20 69 66 28 20 72 63 32 3d 3d 53 51 4c  ;.  if( rc2==SQL
11da5 49 54 45 5f 46 55 4c 4c 20 7c 7c 20 72 63 32 3d  ITE_FULL || rc2=
11da6 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b  =SQLITE_IOERR ){
11da7 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72  .    pPager->err
11da8 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 7d 0a 20  Code = rc;.  }. 
11da9 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
11daa 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 61 20 72  *.** Execute a r
11dab 6f 6c 6c 62 61 63 6b 20 69 66 20 61 20 74 72 61  ollback if a tra
11dac 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69  nsaction is acti
11dad 76 65 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68  ve and unlock th
11dae 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66  e .** database f
11daf 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ile. .**.** If t
11db0 68 65 20 70 61 67 65 72 20 68 61 73 20 61 6c 72  he pager has alr
11db1 65 61 64 79 20 65 6e 74 65 72 65 64 20 74 68 65  eady entered the
11db2 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 64 6f   error state, do
11db3 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 0a 2a 2a   not attempt .**
11db4 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 61 74   the rollback at
11db5 20 74 68 69 73 20 74 69 6d 65 2e 20 49 6e 73 74   this time. Inst
11db6 65 61 64 2c 20 70 61 67 65 72 5f 75 6e 6c 6f 63  ead, pager_unloc
11db7 6b 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 54  k() is called. T
11db8 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 70 61  he.** call to pa
11db9 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 77 69 6c  ger_unlock() wil
11dba 6c 20 64 69 73 63 61 72 64 20 61 6c 6c 20 69 6e  l discard all in
11dbb 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 2c 20 75  -memory pages, u
11dbc 6e 6c 6f 63 6b 0a 2a 2a 20 74 68 65 20 64 61 74  nlock.** the dat
11dbd 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 63  abase file and c
11dbe 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 20 73  lear the error s
11dbf 74 61 74 65 2e 20 49 66 20 74 68 69 73 20 6d 65  tate. If this me
11dc0 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 72  ans that.** ther
11dc1 65 20 69 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e  e is a hot-journ
11dc2 61 6c 20 6c 65 66 74 20 69 6e 20 74 68 65 20 66  al left in the f
11dc3 69 6c 65 2d 73 79 73 74 65 6d 2c 20 74 68 65 20  ile-system, the 
11dc4 6e 65 78 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a  next connection.
11dc5 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 73  ** to obtain a s
11dc6 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 74 68  hared lock on th
11dc7 65 20 70 61 67 65 72 20 28 77 68 69 63 68 20 6d  e pager (which m
11dc8 61 79 20 62 65 20 74 68 69 73 20 6f 6e 65 29 20  ay be this one) 
11dc9 77 69 6c 6c 0a 2a 2a 20 72 6f 6c 6c 20 69 74 20  will.** roll it 
11dca 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  back..**.** If t
11dcb 68 65 20 70 61 67 65 72 20 68 61 73 20 6e 6f 74  he pager has not
11dcc 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72 65 64   already entered
11dcd 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65   the error state
11dce 2c 20 62 75 74 20 61 6e 20 49 4f 20 6f 72 0a 2a  , but an IO or.*
11dcf 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 20 6f  * malloc error o
11dd0 63 63 75 72 73 20 64 75 72 69 6e 67 20 61 20 72  ccurs during a r
11dd1 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 6e 20 74 68  ollback, then th
11dd2 69 73 20 77 69 6c 6c 20 69 74 73 65 6c 66 20 63  is will itself c
11dd3 61 75 73 65 20 0a 2a 2a 20 74 68 65 20 70 61 67  ause .** the pag
11dd4 65 72 20 74 6f 20 65 6e 74 65 72 20 74 68 65 20  er to enter the 
11dd5 65 72 72 6f 72 20 73 74 61 74 65 2e 20 57 68 69  error state. Whi
11dd6 63 68 20 77 69 6c 6c 20 62 65 20 63 6c 65 61 72  ch will be clear
11dd7 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63 61 6c  ed by the.** cal
11dd8 6c 20 74 6f 20 70 61 67 65 72 5f 75 6e 6c 6f 63  l to pager_unloc
11dd9 6b 28 29 2c 20 61 73 20 64 65 73 63 72 69 62 65  k(), as describe
11dda 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74 61 74  d above..*/.stat
11ddb 69 63 20 76 6f 69 64 20 70 61 67 65 72 55 6e 6c  ic void pagerUnl
11ddc 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 50  ockAndRollback(P
11ddd 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
11dde 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72   if( pPager->err
11ddf 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  Code==SQLITE_OK 
11de0 26 26 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  && pPager->state
11de1 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44  >=PAGER_RESERVED
11de2 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42   ){.    sqlite3B
11de3 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  eginBenignMalloc
11de4 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50  ();.    sqlite3P
11de5 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 50 61  agerRollback(pPa
11de6 67 65 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ger);.    sqlite
11de7 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  3EndBenignMalloc
11de8 28 29 3b 0a 20 20 7d 0a 20 20 70 61 67 65 72 5f  ();.  }.  pager_
11de9 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a  unlock(pPager);.
11dea 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
11deb 75 74 69 6e 65 20 65 6e 64 73 20 61 20 74 72 61  utine ends a tra
11dec 6e 73 61 63 74 69 6f 6e 2e 20 41 20 74 72 61 6e  nsaction. A tran
11ded 73 61 63 74 69 6f 6e 20 69 73 20 75 73 75 61 6c  saction is usual
11dee 6c 79 20 65 6e 64 65 64 20 62 79 20 0a 2a 2a 20  ly ended by .** 
11def 65 69 74 68 65 72 20 61 20 43 4f 4d 4d 49 54 20  either a COMMIT 
11df0 6f 72 20 61 20 52 4f 4c 4c 42 41 43 4b 20 6f 70  or a ROLLBACK op
11df1 65 72 61 74 69 6f 6e 2e 20 54 68 69 73 20 72 6f  eration. This ro
11df2 75 74 69 6e 65 20 6d 61 79 20 62 65 20 63 61 6c  utine may be cal
11df3 6c 65 64 20 0a 2a 2a 20 61 66 74 65 72 20 72 6f  led .** after ro
11df4 6c 6c 62 61 63 6b 20 6f 66 20 61 20 68 6f 74 2d  llback of a hot-
11df5 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 69 66 20 61  journal, or if a
11df6 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77  n error occurs w
11df7 68 69 6c 65 20 6f 70 65 6e 69 6e 67 0a 2a 2a 20  hile opening.** 
11df8 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
11df9 20 6f 72 20 77 72 69 74 69 6e 67 20 74 68 65 20   or writing the 
11dfa 76 65 72 79 20 66 69 72 73 74 20 6a 6f 75 72 6e  very first journ
11dfb 61 6c 2d 68 65 61 64 65 72 20 6f 66 20 61 0a 2a  al-header of a.*
11dfc 2a 20 64 61 74 61 62 61 73 65 20 74 72 61 6e 73  * database trans
11dfd 61 63 74 69 6f 6e 2e 0a 2a 2a 20 0a 2a 2a 20 49  action..** .** I
11dfe 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69  f the pager is i
11dff 6e 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 6f  n PAGER_SHARED o
11e00 72 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 73  r PAGER_UNLOCK s
11e01 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 0a 2a  tate when this.*
11e02 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  * routine is cal
11e03 6c 65 64 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d  led, it is a no-
11e04 6f 70 20 28 72 65 74 75 72 6e 73 20 53 51 4c 49  op (returns SQLI
11e05 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74  TE_OK)..**.** Ot
11e06 68 65 72 77 69 73 65 2c 20 61 6e 79 20 61 63 74  herwise, any act
11e07 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61  ive savepoints a
11e08 72 65 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a  re released..**.
11e09 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61  ** If the journa
11e0a 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 2c 20  l file is open, 
11e0b 74 68 65 6e 20 69 74 20 69 73 20 22 66 69 6e 61  then it is "fina
11e0c 6c 69 7a 65 64 22 2e 20 4f 6e 63 65 20 61 20 6a  lized". Once a j
11e0d 6f 75 72 6e 61 6c 20 0a 2a 2a 20 66 69 6c 65 20  ournal .** file 
11e0e 68 61 73 20 62 65 65 6e 20 66 69 6e 61 6c 69 7a  has been finaliz
11e0f 65 64 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73  ed it is not pos
11e10 73 69 62 6c 65 20 74 6f 20 75 73 65 20 69 74 20  sible to use it 
11e11 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 61 20 0a  to roll back a .
11e12 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  ** transaction. 
11e13 4e 6f 72 20 77 69 6c 6c 20 69 74 20 62 65 20 63  Nor will it be c
11e14 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 65 20  onsidered to be 
11e15 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 79  a hot-journal by
11e16 20 74 68 69 73 0a 2a 2a 20 6f 72 20 61 6e 79 20   this.** or any 
11e17 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63  other database c
11e18 6f 6e 6e 65 63 74 69 6f 6e 2e 20 45 78 61 63 74  onnection. Exact
11e19 6c 79 20 68 6f 77 20 61 20 6a 6f 75 72 6e 61 6c  ly how a journal
11e1a 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 0a 2a 2a   is finalized.**
11e1b 20 64 65 70 65 6e 64 73 20 6f 6e 20 77 68 65 74   depends on whet
11e1c 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 70  her or not the p
11e1d 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20  ager is running 
11e1e 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64  in exclusive mod
11e1f 65 20 61 6e 64 0a 2a 2a 20 74 68 65 20 63 75 72  e and.** the cur
11e20 72 65 6e 74 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64  rent journal-mod
11e21 65 20 28 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c  e (Pager.journal
11e22 4d 6f 64 65 20 76 61 6c 75 65 29 2c 20 61 73 20  Mode value), as 
11e23 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20  follows:.**.**  
11e24 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45   journalMode==ME
11e25 4d 4f 52 59 0a 2a 2a 20 20 20 20 20 4a 6f 75 72  MORY.**     Jour
11e26 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70  nal file descrip
11e27 74 6f 72 20 69 73 20 73 69 6d 70 6c 79 20 63 6c  tor is simply cl
11e28 6f 73 65 64 2e 20 54 68 69 73 20 64 65 73 74 72  osed. This destr
11e29 6f 79 73 20 61 6e 20 0a 2a 2a 20 20 20 20 20 69  oys an .**     i
11e2a 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c  n-memory journal
11e2b 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61  ..**.**   journa
11e2c 6c 4d 6f 64 65 3d 3d 54 52 55 4e 43 41 54 45 0a  lMode==TRUNCATE.
11e2d 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c 20 66  **     Journal f
11e2e 69 6c 65 20 69 73 20 74 72 75 6e 63 61 74 65 64  ile is truncated
11e2f 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 20 69   to zero bytes i
11e30 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  n size..**.**   
11e31 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 45 52  journalMode==PER
11e32 53 49 53 54 0a 2a 2a 20 20 20 20 20 54 68 65 20  SIST.**     The 
11e33 66 69 72 73 74 20 32 38 20 62 79 74 65 73 20 6f  first 28 bytes o
11e34 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
11e35 6c 65 20 61 72 65 20 7a 65 72 6f 65 64 2e 20 54  le are zeroed. T
11e36 68 69 73 20 69 6e 76 61 6c 69 64 61 74 65 73 0a  his invalidates.
11e37 2a 2a 20 20 20 20 20 74 68 65 20 66 69 72 73 74  **     the first
11e38 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
11e39 69 6e 20 74 68 65 20 66 69 6c 65 2c 20 61 6e 64  in the file, and
11e3a 20 68 65 6e 63 65 20 74 68 65 20 65 6e 74 69 72   hence the entir
11e3b 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20  e journal.**    
11e3c 20 66 69 6c 65 2e 20 41 6e 20 69 6e 76 61 6c 69   file. An invali
11e3d 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63  d journal file c
11e3e 61 6e 6e 6f 74 20 62 65 20 72 6f 6c 6c 65 64 20  annot be rolled 
11e3f 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f  back..**.**   jo
11e40 75 72 6e 61 6c 4d 6f 64 65 3d 3d 44 45 4c 45 54  urnalMode==DELET
11e41 45 0a 2a 2a 20 20 20 20 20 54 68 65 20 6a 6f 75  E.**     The jou
11e42 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 63 6c 6f  rnal file is clo
11e43 73 65 64 20 61 6e 64 20 64 65 6c 65 74 65 64 20  sed and deleted 
11e44 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 44  using sqlite3OsD
11e45 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 20  elete()..**.**  
11e46 20 20 20 49 66 20 74 68 65 20 70 61 67 65 72 20     If the pager 
11e47 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78  is running in ex
11e48 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 74 68  clusive mode, th
11e49 69 73 20 6d 65 74 68 6f 64 20 6f 66 20 66 69 6e  is method of fin
11e4a 61 6c 69 7a 69 6e 67 0a 2a 2a 20 20 20 20 20 74  alizing.**     t
11e4b 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11e4c 69 73 20 6e 65 76 65 72 20 75 73 65 64 2e 20 49  is never used. I
11e4d 6e 73 74 65 61 64 2c 20 69 66 20 74 68 65 20 6a  nstead, if the j
11e4e 6f 75 72 6e 61 6c 4d 6f 64 65 20 69 73 0a 2a 2a  ournalMode is.**
11e4f 20 20 20 20 20 44 45 4c 45 54 45 20 61 6e 64 20       DELETE and 
11e50 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20  the pager is in 
11e51 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20  exclusive mode, 
11e52 74 68 65 20 6d 65 74 68 6f 64 20 64 65 73 63 72  the method descr
11e53 69 62 65 64 20 75 6e 64 65 72 0a 2a 2a 20 20 20  ibed under.**   
11e54 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50    journalMode==P
11e55 45 52 53 49 53 54 20 69 73 20 75 73 65 64 20 69  ERSIST is used i
11e56 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 66  nstead..**.** Af
11e57 74 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ter the journal 
11e58 69 73 20 66 69 6e 61 6c 69 7a 65 64 2c 20 69 66  is finalized, if
11e59 20 72 75 6e 6e 69 6e 67 20 69 6e 20 6e 6f 6e 2d   running in non-
11e5a 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20  exclusive mode, 
11e5b 74 68 65 0a 2a 2a 20 70 61 67 65 72 20 6d 6f 76  the.** pager mov
11e5c 65 73 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52  es to PAGER_SHAR
11e5d 45 44 20 73 74 61 74 65 20 28 61 6e 64 20 64 6f  ED state (and do
11e5e 77 6e 67 72 61 64 65 73 20 74 68 65 20 6c 6f 63  wngrades the loc
11e5f 6b 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 61 74 61  k on the.** data
11e60 62 61 73 65 20 66 69 6c 65 20 61 63 63 6f 72 64  base file accord
11e61 69 6e 67 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20 49 66  ingly)..**.** If
11e62 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72 75   the pager is ru
11e63 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69  nning in exclusi
11e64 76 65 20 6d 6f 64 65 20 61 6e 64 20 69 73 20 69  ve mode and is i
11e65 6e 20 50 41 47 45 52 5f 53 59 4e 43 45 44 20 73  n PAGER_SYNCED s
11e66 74 61 74 65 2c 0a 2a 2a 20 69 74 20 6d 6f 76 65  tate,.** it move
11e67 73 20 74 6f 20 50 41 47 45 52 5f 45 58 43 4c 55  s to PAGER_EXCLU
11e68 53 49 56 45 2e 20 4e 6f 20 6c 6f 63 6b 73 20 61  SIVE. No locks a
11e69 72 65 20 64 6f 77 6e 67 72 61 64 65 64 20 77 68  re downgraded wh
11e6a 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 0a 2a 2a  en running in.**
11e6b 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e   exclusive mode.
11e6c 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  .**.** SQLITE_OK
11e6d 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
11e6e 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e  no error occurs.
11e6f 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
11e70 75 72 73 20 64 75 72 69 6e 67 0a 2a 2a 20 61 6e  urs during.** an
11e71 79 20 6f 66 20 74 68 65 20 49 4f 20 6f 70 65 72  y of the IO oper
11e72 61 74 69 6f 6e 73 20 74 6f 20 66 69 6e 61 6c 69  ations to finali
11e73 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ze the journal f
11e74 69 6c 65 20 6f 72 20 75 6e 6c 6f 63 6b 20 74 68  ile or unlock th
11e75 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 74 68  e.** database th
11e76 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20  en the IO error 
11e77 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
11e78 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 49 66   to the user. If
11e79 20 74 68 65 20 0a 2a 2a 20 6f 70 65 72 61 74 69   the .** operati
11e7a 6f 6e 20 74 6f 20 66 69 6e 61 6c 69 7a 65 20 74  on to finalize t
11e7b 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11e7c 66 61 69 6c 73 2c 20 74 68 65 6e 20 74 68 65 20  fails, then the 
11e7d 63 6f 64 65 20 73 74 69 6c 6c 0a 2a 2a 20 74 72  code still.** tr
11e7e 69 65 73 20 74 6f 20 75 6e 6c 6f 63 6b 20 74 68  ies to unlock th
11e7f 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
11e80 69 66 20 6e 6f 74 20 69 6e 20 65 78 63 6c 75 73  if not in exclus
11e81 69 76 65 20 6d 6f 64 65 2e 20 49 66 20 74 68 65  ive mode. If the
11e82 0a 2a 2a 20 75 6e 6c 6f 63 6b 20 6f 70 65 72 61  .** unlock opera
11e83 74 69 6f 6e 20 66 61 69 6c 73 20 61 73 20 77 65  tion fails as we
11e84 6c 6c 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72  ll, then the fir
11e85 73 74 20 65 72 72 6f 72 20 63 6f 64 65 20 72 65  st error code re
11e86 6c 61 74 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20  lated.** to the 
11e87 66 69 72 73 74 20 65 72 72 6f 72 20 65 6e 63 6f  first error enco
11e88 75 6e 74 65 72 65 64 20 28 74 68 65 20 6a 6f 75  untered (the jou
11e89 72 6e 61 6c 20 66 69 6e 61 6c 69 7a 61 74 69 6f  rnal finalizatio
11e8a 6e 20 6f 6e 65 29 20 69 73 0a 2a 2a 20 72 65 74  n one) is.** ret
11e8b 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  urned..*/.static
11e8c 20 69 6e 74 20 70 61 67 65 72 5f 65 6e 64 5f 74   int pager_end_t
11e8d 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 67 65 72  ransaction(Pager
11e8e 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 68 61   *pPager, int ha
11e8f 73 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74 20  sMaster){.  int 
11e90 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
11e91 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f       /* Error co
11e92 64 65 20 66 72 6f 6d 20 6a 6f 75 72 6e 61 6c 20  de from journal 
11e93 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 6f 70 65  finalization ope
11e94 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ration */.  int 
11e95 72 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  rc2 = SQLITE_OK;
11e96 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f       /* Error co
11e97 64 65 20 66 72 6f 6d 20 64 62 20 66 69 6c 65 20  de from db file 
11e98 75 6e 6c 6f 63 6b 20 6f 70 65 72 61 74 69 6f 6e  unlock operation
11e99 20 2a 2f 0a 0a 20 20 69 66 28 20 70 50 61 67 65   */..  if( pPage
11e9a 72 2d 3e 73 74 61 74 65 3c 50 41 47 45 52 5f 52  r->state<PAGER_R
11e9b 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20 72  ESERVED ){.    r
11e9c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
11e9d 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 41 6c  .  }.  releaseAl
11e9e 6c 53 61 76 65 70 6f 69 6e 74 73 28 70 50 61 67  lSavepoints(pPag
11e9f 65 72 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  er);..  assert( 
11ea0 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
11ea1 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 70  fd) || pPager->p
11ea2 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a  InJournal==0 );.
11ea3 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61    if( isOpen(pPa
11ea4 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 0a 20 20  ger->jfd) ){..  
11ea5 20 20 2f 2a 20 46 69 6e 61 6c 69 7a 65 20 74 68    /* Finalize th
11ea6 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  e journal file. 
11ea7 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  */.    if( sqlit
11ea8 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70  e3IsMemJournal(p
11ea9 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20  Pager->jfd) ){. 
11eaa 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
11eab 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
11eac 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
11ead 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a 20 20  ODE_MEMORY );.  
11eae 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f      sqlite3OsClo
11eaf 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b  se(pPager->jfd);
11eb0 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70  .    }else if( p
11eb1 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
11eb2 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
11eb3 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 29  LMODE_TRUNCATE )
11eb4 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67  {.      if( pPag
11eb5 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d  er->journalOff==
11eb6 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  0 ){.        rc 
11eb7 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
11eb8 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
11eb9 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
11eba 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d  Truncate(pPager-
11ebb 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 20 20  >jfd, 0);.      
11ebc 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  }.      pPager->
11ebd 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a  journalOff = 0;.
11ebe 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f        pPager->jo
11ebf 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 30  urnalStarted = 0
11ec0 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
11ec1 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
11ec2 65 4d 6f 64 65 20 0a 20 20 20 20 20 7c 7c 20 70  eMode .     || p
11ec3 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
11ec4 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
11ec5 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20 20  LMODE_PERSIST.  
11ec6 20 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20    ){.      rc = 
11ec7 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 28 70  zeroJournalHdr(p
11ec8 50 61 67 65 72 2c 20 68 61 73 4d 61 73 74 65 72  Pager, hasMaster
11ec9 29 3b 0a 20 20 20 20 20 20 70 61 67 65 72 5f 65  );.      pager_e
11eca 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29  rror(pPager, rc)
11ecb 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  ;.      pPager->
11ecc 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a  journalOff = 0;.
11ecd 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f        pPager->jo
11ece 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 30  urnalStarted = 0
11ecf 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
11ed0 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63     /* This branc
11ed1 68 20 6d 61 79 20 62 65 20 65 78 65 63 75 74 65  h may be execute
11ed2 64 20 77 69 74 68 20 50 61 67 65 72 2e 6a 6f 75  d with Pager.jou
11ed3 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45 4d 4f 52 59  rnalMode==MEMORY
11ed4 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 61 20 68   if.      ** a h
11ed5 6f 74 2d 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6a  ot-journal was j
11ed6 75 73 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e  ust rolled back.
11ed7 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
11ed8 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20  e journal.      
11ed9 2a 2a 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62  ** file should b
11eda 65 20 63 6c 6f 73 65 64 20 61 6e 64 20 64 65 6c  e closed and del
11edb 65 74 65 64 2e 20 49 66 20 74 68 69 73 20 63 6f  eted. If this co
11edc 6e 6e 65 63 74 69 6f 6e 20 77 72 69 74 65 73 20  nnection writes 
11edd 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  to.      ** the 
11ede 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 69  database file, i
11edf 74 20 77 69 6c 6c 20 64 6f 20 73 6f 20 75 73 69  t will do so usi
11ee0 6e 67 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  ng an in-memory 
11ee1 6a 6f 75 72 6e 61 6c 2e 20 20 2a 2f 0a 20 20 20  journal.  */.   
11ee2 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
11ee3 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d  r->journalMode==
11ee4 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
11ee5 45 5f 44 45 4c 45 54 45 20 0a 20 20 20 20 20 20  E_DELETE .      
11ee6 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e       || pPager->
11ee7 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47  journalMode==PAG
11ee8 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d  ER_JOURNALMODE_M
11ee9 45 4d 4f 52 59 20 0a 20 20 20 20 20 20 29 3b 0a  EMORY .      );.
11eea 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43        sqlite3OsC
11eeb 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  lose(pPager->jfd
11eec 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 50  );.      if( !pP
11eed 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29  ager->tempFile )
11eee 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
11eef 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70  qlite3OsDelete(p
11ef0 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 50 61  Pager->pVfs, pPa
11ef1 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30  ger->zJournal, 0
11ef2 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
11ef3 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
11ef4 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20  CHECK_PAGES.    
11ef5 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65  sqlite3PcacheIte
11ef6 72 61 74 65 44 69 72 74 79 28 70 50 61 67 65 72  rateDirty(pPager
11ef7 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 67 65 72  ->pPCache, pager
11ef8 5f 73 65 74 5f 70 61 67 65 68 61 73 68 29 3b 0a  _set_pagehash);.
11ef9 23 65 6e 64 69 66 0a 0a 20 20 20 20 73 71 6c 69  #endif..    sqli
11efa 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e 41 6c  te3PcacheCleanAl
11efb 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  l(pPager->pPCach
11efc 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  e);.    sqlite3B
11efd 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61  itvecDestroy(pPa
11efe 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29  ger->pInJournal)
11eff 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49  ;.    pPager->pI
11f00 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20  nJournal = 0;.  
11f01 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d    pPager->nRec =
11f02 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21   0;.  }..  if( !
11f03 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
11f04 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 72 63 32  eMode ){.    rc2
11f05 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 67   = osUnlock(pPag
11f06 65 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f 4c  er->fd, SHARED_L
11f07 4f 43 4b 29 3b 0a 20 20 20 20 70 50 61 67 65 72  OCK);.    pPager
11f08 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f  ->state = PAGER_
11f09 53 48 41 52 45 44 3b 0a 20 20 20 20 70 50 61 67  SHARED;.    pPag
11f0a 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44  er->changeCountD
11f0b 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  one = 0;.  }else
11f0c 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61   if( pPager->sta
11f0d 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45 44  te==PAGER_SYNCED
11f0e 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   ){.    pPager->
11f0f 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58  state = PAGER_EX
11f10 43 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 20 20 70  CLUSIVE;.  }.  p
11f11 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72  Pager->setMaster
11f12 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e   = 0;.  pPager->
11f13 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20  needSync = 0;.  
11f14 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69  pPager->dbModifi
11f15 65 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 4f  ed = 0;..  /* TO
11f16 44 4f 3a 20 49 73 20 74 68 69 73 20 6f 70 74 69  DO: Is this opti
11f17 6d 61 6c 3f 20 57 68 79 20 69 73 20 74 68 65 20  mal? Why is the 
11f18 64 62 20 73 69 7a 65 20 69 6e 76 61 6c 69 64 61  db size invalida
11f19 74 65 64 20 68 65 72 65 20 0a 20 20 2a 2a 20 77  ted here .  ** w
11f1a 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
11f1b 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 75 6e 6c   file is not unl
11f1c 6f 63 6b 65 64 3f 20 2a 2f 0a 20 20 70 50 61 67  ocked? */.  pPag
11f1d 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 3d  er->dbOrigSize =
11f1e 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61   0;.  sqlite3Pca
11f1f 63 68 65 54 72 75 6e 63 61 74 65 28 70 50 61 67  cheTruncate(pPag
11f20 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 50 61  er->pPCache, pPa
11f21 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20  ger->dbSize);.  
11f22 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20  if( !MEMDB ){.  
11f23 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
11f24 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a  Valid = 0;.  }..
11f25 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 51    return (rc==SQ
11f26 4c 49 54 45 5f 4f 4b 3f 72 63 32 3a 72 63 29 3b  LITE_OK?rc2:rc);
11f27 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65  .}../*.** Parame
11f28 74 65 72 20 61 44 61 74 61 20 6d 75 73 74 20 70  ter aData must p
11f29 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72  oint to a buffer
11f2a 20 6f 66 20 70 50 61 67 65 72 2d 3e 70 61 67 65   of pPager->page
11f2b 53 69 7a 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66  Size bytes.** of
11f2c 20 64 61 74 61 2e 20 43 6f 6d 70 75 74 65 20 61   data. Compute a
11f2d 6e 64 20 72 65 74 75 72 6e 20 61 20 63 68 65 63  nd return a chec
11f2e 6b 73 75 6d 20 62 61 73 65 64 20 6f 6e 74 20 74  ksum based ont t
11f2f 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
11f30 68 65 20 0a 2a 2a 20 70 61 67 65 20 6f 66 20 64  he .** page of d
11f31 61 74 61 20 61 6e 64 20 74 68 65 20 63 75 72 72  ata and the curr
11f32 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 70 50 61  ent value of pPa
11f33 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 2e 0a  ger->cksumInit..
11f34 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f  **.** This is no
11f35 74 20 61 20 72 65 61 6c 20 63 68 65 63 6b 73 75  t a real checksu
11f36 6d 2e 20 49 74 20 69 73 20 72 65 61 6c 6c 79 20  m. It is really 
11f37 6a 75 73 74 20 74 68 65 20 73 75 6d 20 6f 66 20  just the sum of 
11f38 74 68 65 20 0a 2a 2a 20 72 61 6e 64 6f 6d 20 69  the .** random i
11f39 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 28 70 50  nitial value (pP
11f3a 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29  ager->cksumInit)
11f3b 20 61 6e 64 20 65 76 65 72 79 20 32 30 30 74 68   and every 200th
11f3c 20 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20   byte.** of the 
11f3d 70 61 67 65 20 64 61 74 61 2c 20 73 74 61 72 74  page data, start
11f3e 69 6e 67 20 77 69 74 68 20 62 79 74 65 20 6f 66  ing with byte of
11f3f 66 73 65 74 20 28 70 50 61 67 65 72 2d 3e 70 61  fset (pPager->pa
11f40 67 65 53 69 7a 65 25 32 30 30 29 2e 0a 2a 2a 20  geSize%200)..** 
11f41 45 61 63 68 20 62 79 74 65 20 69 73 20 69 6e 74  Each byte is int
11f42 65 72 70 72 65 74 65 64 20 61 73 20 61 6e 20 38  erpreted as an 8
11f43 2d 62 69 74 20 75 6e 73 69 67 6e 65 64 20 69 6e  -bit unsigned in
11f44 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 68 61  teger..**.** Cha
11f45 6e 67 69 6e 67 20 74 68 65 20 66 6f 72 6d 75 6c  nging the formul
11f46 61 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75 74  a used to comput
11f47 65 20 74 68 69 73 20 63 68 65 63 6b 73 75 6d 20  e this checksum 
11f48 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 0a 2a 2a  results in an.**
11f49 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 6a 6f   incompatible jo
11f4a 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d 61  urnal file forma
11f4b 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6a 6f 75 72  t..**.** If jour
11f4c 6e 61 6c 20 63 6f 72 72 75 70 74 69 6f 6e 20 6f  nal corruption o
11f4d 63 63 75 72 73 20 64 75 65 20 74 6f 20 61 20 70  ccurs due to a p
11f4e 6f 77 65 72 20 66 61 69 6c 75 72 65 2c 20 74 68  ower failure, th
11f4f 65 20 6d 6f 73 74 20 6c 69 6b 65 6c 79 20 0a 2a  e most likely .*
11f50 2a 20 73 63 65 6e 61 72 69 6f 20 69 73 20 74 68  * scenario is th
11f51 61 74 20 6f 6e 65 20 65 6e 64 20 6f 72 20 74 68  at one end or th
11f52 65 20 6f 74 68 65 72 20 6f 66 20 74 68 65 20 72  e other of the r
11f53 65 63 6f 72 64 20 77 69 6c 6c 20 62 65 20 63 68  ecord will be ch
11f54 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 74 20 69 73  anged. .** It is
11f55 20 6d 75 63 68 20 6c 65 73 73 20 6c 69 6b 65 6c   much less likel
11f56 79 20 74 68 61 74 20 74 68 65 20 74 77 6f 20 65  y that the two e
11f57 6e 64 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  nds of the journ
11f58 61 6c 20 72 65 63 6f 72 64 20 77 69 6c 6c 20 62  al record will b
11f59 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 6e 64  e.** correct and
11f5a 20 74 68 65 20 6d 69 64 64 6c 65 20 62 65 20 63   the middle be c
11f5b 6f 72 72 75 70 74 2e 20 20 54 68 75 73 2c 20 74  orrupt.  Thus, t
11f5c 68 69 73 20 22 63 68 65 63 6b 73 75 6d 22 20 73  his "checksum" s
11f5d 63 68 65 6d 65 2c 0a 2a 2a 20 74 68 6f 75 67 68  cheme,.** though
11f5e 20 66 61 73 74 20 61 6e 64 20 73 69 6d 70 6c 65   fast and simple
11f5f 2c 20 63 61 74 63 68 65 73 20 74 68 65 20 6d 6f  , catches the mo
11f60 73 74 6c 79 20 6c 69 6b 65 6c 79 20 6b 69 6e 64  stly likely kind
11f61 20 6f 66 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a   of corruption..
11f62 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 20 70 61  */.static u32 pa
11f63 67 65 72 5f 63 6b 73 75 6d 28 50 61 67 65 72 20  ger_cksum(Pager 
11f64 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 75  *pPager, const u
11f65 38 20 2a 61 44 61 74 61 29 7b 0a 20 20 75 33 32  8 *aData){.  u32
11f66 20 63 6b 73 75 6d 20 3d 20 70 50 61 67 65 72 2d   cksum = pPager-
11f67 3e 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20 20 20  >cksumInit;     
11f68 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20      /* Checksum 
11f69 76 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20  value to return 
11f6a 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 50 61  */.  int i = pPa
11f6b 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2d 32 30  ger->pageSize-20
11f6c 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  0;          /* L
11f6d 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
11f6e 20 77 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a 20   while( i>0 ){. 
11f6f 20 20 20 63 6b 73 75 6d 20 2b 3d 20 61 44 61 74     cksum += aDat
11f70 61 5b 69 5d 3b 0a 20 20 20 20 69 20 2d 3d 20 32  a[i];.    i -= 2
11f71 30 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  00;.  }.  return
11f72 20 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   cksum;.}../*.**
11f73 20 52 65 61 64 20 61 20 73 69 6e 67 6c 65 20 70   Read a single p
11f74 61 67 65 20 66 72 6f 6d 20 65 69 74 68 65 72 20  age from either 
11f75 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
11f76 20 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d   (if isMainJrnl=
11f77 3d 31 29 20 6f 72 0a 2a 2a 20 66 72 6f 6d 20 74  =1) or.** from t
11f78 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 28  he sub-journal (
11f79 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d 3d 30  if isMainJrnl==0
11f7a 29 20 61 6e 64 20 70 6c 61 79 62 61 63 6b 20 74  ) and playback t
11f7b 68 61 74 20 70 61 67 65 2e 0a 2a 2a 20 54 68 65  hat page..** The
11f7c 20 70 61 67 65 20 62 65 67 69 6e 73 20 61 74 20   page begins at 
11f7d 6f 66 66 73 65 74 20 2a 70 4f 66 66 73 65 74 20  offset *pOffset 
11f7e 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 54  into the file. T
11f7f 68 65 20 2a 70 4f 66 66 73 65 74 0a 2a 2a 20 76  he *pOffset.** v
11f80 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61 73 65  alue is increase
11f81 64 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f  d to the start o
11f82 66 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20  f the next page 
11f83 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a  in the journal..
11f84 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 4d 61 69 6e  **.** The isMain
11f85 4a 72 6e 6c 20 66 6c 61 67 20 69 73 20 74 72 75  Jrnl flag is tru
11f86 65 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65  e if this is the
11f87 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a   main rollback j
11f88 6f 75 72 6e 61 6c 20 61 6e 64 0a 2a 2a 20 66 61  ournal and.** fa
11f89 6c 73 65 20 66 6f 72 20 74 68 65 20 73 74 61 74  lse for the stat
11f8a 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 20  ement journal.  
11f8b 54 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63  The main rollbac
11f8c 6b 20 6a 6f 75 72 6e 61 6c 20 75 73 65 73 0a 2a  k journal uses.*
11f8d 2a 20 63 68 65 63 6b 73 75 6d 73 20 2d 20 74 68  * checksums - th
11f8e 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72  e statement jour
11f8f 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a  nal does not..**
11f90 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20  .** If the page 
11f91 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61  number of the pa
11f92 67 65 20 72 65 63 6f 72 64 20 72 65 61 64 20 66  ge record read f
11f93 72 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f  rom the (sub-)jo
11f94 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 69 73  urnal file.** is
11f95 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68   greater than th
11f96 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20  e current value 
11f97 6f 66 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c  of Pager.dbSize,
11f98 20 74 68 65 6e 20 70 6c 61 79 62 61 63 6b 20 69   then playback i
11f99 73 0a 2a 2a 20 73 6b 69 70 70 65 64 20 61 6e 64  s.** skipped and
11f9a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
11f9b 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  turned..**.** If
11f9c 20 70 44 6f 6e 65 20 69 73 20 6e 6f 74 20 4e 55   pDone is not NU
11f9d 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 20 61  LL, then it is a
11f9e 20 72 65 63 6f 72 64 20 6f 66 20 70 61 67 65 73   record of pages
11f9f 20 74 68 61 74 20 68 61 76 65 20 61 6c 72 65 61   that have alrea
11fa0 64 79 0a 2a 2a 20 62 65 65 6e 20 70 6c 61 79 65  dy.** been playe
11fa1 64 20 62 61 63 6b 2e 20 20 49 66 20 74 68 65 20  d back.  If the 
11fa2 70 61 67 65 20 61 74 20 2a 70 4f 66 66 73 65 74  page at *pOffset
11fa3 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
11fa4 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 0a 2a 2a  n played back.**
11fa5 20 28 69 66 20 74 68 65 20 63 6f 72 72 65 73 70   (if the corresp
11fa6 6f 6e 64 69 6e 67 20 70 44 6f 6e 65 20 62 69 74  onding pDone bit
11fa7 20 69 73 20 73 65 74 29 20 74 68 65 6e 20 73 6b   is set) then sk
11fa8 69 70 20 74 68 65 20 70 6c 61 79 62 61 63 6b 2e  ip the playback.
11fa9 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68  .** Make sure th
11faa 65 20 70 44 6f 6e 65 20 62 69 74 20 63 6f 72 72  e pDone bit corr
11fab 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65  esponding to the
11fac 20 2a 70 4f 66 66 73 65 74 20 70 61 67 65 20 69   *pOffset page i
11fad 73 20 73 65 74 0a 2a 2a 20 70 72 69 6f 72 20 74  s set.** prior t
11fae 6f 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a  o returning..**.
11faf 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 72  ** If the page r
11fb0 65 63 6f 72 64 20 69 73 20 73 75 63 63 65 73 73  ecord is success
11fb1 66 75 6c 6c 79 20 72 65 61 64 20 66 72 6f 6d 20  fully read from 
11fb2 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61  the (sub-)journa
11fb3 6c 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 70 6c  l file.** and pl
11fb4 61 79 65 64 20 62 61 63 6b 2c 20 74 68 65 6e 20  ayed back, then 
11fb5 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
11fb6 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20  urned. If an IO 
11fb7 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 2a 2a 20  error occurs.** 
11fb8 77 68 69 6c 65 20 72 65 61 64 69 6e 67 20 74 68  while reading th
11fb9 65 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 74 68  e record from th
11fba 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20  e (sub-)journal 
11fbb 66 69 6c 65 20 6f 72 20 77 68 69 6c 65 20 77 72  file or while wr
11fbc 69 74 69 6e 67 0a 2a 2a 20 74 6f 20 74 68 65 20  iting.** to the 
11fbd 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74  database file, t
11fbe 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72  hen the IO error
11fbf 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
11fc0 64 2e 20 49 66 20 64 61 74 61 0a 2a 2a 20 69 73  d. If data.** is
11fc1 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 72 65   successfully re
11fc2 61 64 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62  ad from the (sub
11fc3 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62  -)journal file b
11fc4 75 74 20 61 70 70 65 61 72 73 20 74 6f 20 62 65  ut appears to be
11fc5 0a 2a 2a 20 63 6f 72 72 75 70 74 65 64 2c 20 53  .** corrupted, S
11fc6 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 65  QLITE_DONE is re
11fc7 74 75 72 6e 65 64 2e 20 44 61 74 61 20 69 73 20  turned. Data is 
11fc8 63 6f 6e 73 69 64 65 72 65 64 20 63 6f 72 72 75  considered corru
11fc9 70 74 65 64 20 69 6e 0a 2a 2a 20 74 77 6f 20 63  pted in.** two c
11fca 69 72 63 75 6d 73 74 61 6e 63 65 73 3a 0a 2a 2a  ircumstances:.**
11fcb 20 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20   .**   * If the 
11fcc 72 65 63 6f 72 64 20 70 61 67 65 2d 6e 75 6d 62  record page-numb
11fcd 65 72 20 69 73 20 69 6c 6c 65 67 61 6c 20 28 30  er is illegal (0
11fce 20 6f 72 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e   or PAGER_MJ_PGN
11fcf 4f 29 2c 20 6f 72 0a 2a 2a 20 20 20 2a 20 49 66  O), or.**   * If
11fd0 20 74 68 65 20 72 65 63 6f 72 64 20 69 73 20 62   the record is b
11fd1 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b  eing rolled back
11fd2 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a   from the main j
11fd3 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 20  ournal file.**  
11fd4 20 20 20 61 6e 64 20 74 68 65 20 63 68 65 63 6b     and the check
11fd5 73 75 6d 20 66 69 65 6c 64 20 64 6f 65 73 20 6e  sum field does n
11fd6 6f 74 20 6d 61 74 63 68 20 74 68 65 20 72 65 63  ot match the rec
11fd7 6f 72 64 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a  ord content..**.
11fd8 2a 2a 20 4e 65 69 74 68 65 72 20 6f 66 20 74 68  ** Neither of th
11fd9 65 73 65 20 74 77 6f 20 73 63 65 6e 61 72 69 6f  ese two scenario
11fda 73 20 61 72 65 20 70 6f 73 73 69 62 6c 65 20 64  s are possible d
11fdb 75 72 69 6e 67 20 61 20 73 61 76 65 70 6f 69 6e  uring a savepoin
11fdc 74 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a  t rollback..**.*
11fdd 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 73  * If this is a s
11fde 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63  avepoint rollbac
11fdf 6b 2c 20 74 68 65 6e 20 6d 65 6d 6f 72 79 20 6d  k, then memory m
11fe0 61 79 20 68 61 76 65 20 74 6f 20 62 65 20 64 79  ay have to be dy
11fe1 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 6c 6c  namically.** all
11fe2 6f 63 61 74 65 64 20 62 79 20 74 68 69 73 20 66  ocated by this f
11fe3 75 6e 63 74 69 6f 6e 2e 20 49 66 20 74 68 69 73  unction. If this
11fe4 20 69 73 20 74 68 65 20 63 61 73 65 20 61 6e 64   is the case and
11fe5 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66   an allocation f
11fe6 61 69 6c 73 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f  ails,.** SQLITE_
11fe7 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65  NOMEM is returne
11fe8 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
11fe9 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f   pager_playback_
11fea 6f 6e 65 5f 70 61 67 65 28 0a 20 20 50 61 67 65  one_page(.  Page
11feb 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20  r *pPager,      
11fec 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
11fed 20 70 61 67 65 72 20 62 65 69 6e 67 20 70 6c 61   pager being pla
11fee 79 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20 69 6e  yed back */.  in
11fef 74 20 69 73 4d 61 69 6e 4a 72 6e 6c 2c 20 20 20  t isMainJrnl,   
11ff0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31              /* 1
11ff1 20 2d 3e 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c   -> main journal
11ff2 2e 20 30 20 2d 3e 20 73 75 62 2d 6a 6f 75 72 6e  . 0 -> sub-journ
11ff3 61 6c 2e 20 2a 2f 0a 20 20 69 6e 74 20 69 73 55  al. */.  int isU
11ff4 6e 73 79 6e 63 2c 20 20 20 20 20 20 20 20 20 20  nsync,          
11ff5 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
11ff6 66 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 75  f reading from u
11ff7 6e 73 79 6e 63 65 64 20 6d 61 69 6e 20 6a 6f 75  nsynced main jou
11ff8 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 2a 70  rnal */.  i64 *p
11ff9 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20  Offset,         
11ffa 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
11ffb 74 20 6f 66 20 72 65 63 6f 72 64 20 74 6f 20 70  t of record to p
11ffc 6c 61 79 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74  layback */.  int
11ffd 20 69 73 53 61 76 65 70 6e 74 2c 20 20 20 20 20   isSavepnt,     
11ffe 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
11fff 75 65 20 66 6f 72 20 61 20 73 61 76 65 70 6f 69  ue for a savepoi
12000 6e 74 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 20  nt rollback */. 
12001 20 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 20 20   Bitvec *pDone  
12002 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
12003 2a 20 42 69 74 76 65 63 20 6f 66 20 70 61 67 65  * Bitvec of page
12004 73 20 61 6c 72 65 61 64 79 20 70 6c 61 79 65 64  s already played
12005 20 62 61 63 6b 20 2a 2f 0a 29 7b 0a 20 20 69 6e   back */.){.  in
12006 74 20 72 63 3b 0a 20 20 50 67 48 64 72 20 2a 70  t rc;.  PgHdr *p
12007 50 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Pg;             
12008 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 78 69 73        /* An exis
12009 74 69 6e 67 20 70 61 67 65 20 69 6e 20 74 68 65  ting page in the
1200a 20 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f   cache */.  Pgno
1200b 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20   pgno;          
1200c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1200d 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   page number of 
1200e 61 20 70 61 67 65 20 69 6e 20 6a 6f 75 72 6e 61  a page in journa
1200f 6c 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d  l */.  u32 cksum
12010 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
12011 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d       /* Checksum
12012 20 75 73 65 64 20 66 6f 72 20 73 61 6e 69 74 79   used for sanity
12013 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 75   checking */.  u
12014 38 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20  8 *aData;       
12015 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
12016 54 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67  Temporary storag
12017 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 20 2a  e for the page *
12018 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  /.  sqlite3_file
12019 20 2a 6a 66 64 3b 20 20 20 20 20 20 20 20 20 20   *jfd;          
1201a 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65    /* The file de
1201b 73 63 72 69 70 74 6f 72 20 66 6f 72 20 74 68 65  scriptor for the
1201c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f   journal file */
1201d 0a 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 4d  ..  assert( (isM
1201e 61 69 6e 4a 72 6e 6c 26 7e 31 29 3d 3d 30 20 29  ainJrnl&~1)==0 )
1201f 3b 20 20 20 20 20 20 2f 2a 20 69 73 4d 61 69 6e  ;      /* isMain
12020 4a 72 6e 6c 20 69 73 20 30 20 6f 72 20 31 20 2a  Jrnl is 0 or 1 *
12021 2f 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 53  /.  assert( (isS
12022 61 76 65 70 6e 74 26 7e 31 29 3d 3d 30 20 29 3b  avepnt&~1)==0 );
12023 20 20 20 20 20 20 20 2f 2a 20 69 73 53 61 76 65         /* isSave
12024 70 6e 74 20 69 73 20 30 20 6f 72 20 31 20 2a 2f  pnt is 0 or 1 */
12025 0a 20 20 61 73 73 65 72 74 28 20 69 73 4d 61 69  .  assert( isMai
12026 6e 4a 72 6e 6c 20 7c 7c 20 70 44 6f 6e 65 20 29  nJrnl || pDone )
12027 3b 20 20 20 20 20 2f 2a 20 70 44 6f 6e 65 20 61  ;     /* pDone a
12028 6c 77 61 79 73 20 75 73 65 64 20 6f 6e 20 73 75  lways used on su
12029 62 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a 20 20  b-journals */.  
1202a 61 73 73 65 72 74 28 20 69 73 53 61 76 65 70 6e  assert( isSavepn
1202b 74 20 7c 7c 20 70 44 6f 6e 65 3d 3d 30 20 29 3b  t || pDone==0 );
1202c 20 20 20 2f 2a 20 70 44 6f 6e 65 20 6e 65 76 65     /* pDone neve
1202d 72 20 75 73 65 64 20 6f 6e 20 6e 6f 6e 2d 73 61  r used on non-sa
1202e 76 65 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20 61 44  vepoint */..  aD
1202f 61 74 61 20 3d 20 28 75 38 2a 29 70 50 61 67 65  ata = (u8*)pPage
12030 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20  r->pTmpSpace;.  
12031 61 73 73 65 72 74 28 20 61 44 61 74 61 20 29 3b  assert( aData );
12032 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70           /* Temp
12033 20 73 74 6f 72 61 67 65 20 6d 75 73 74 20 68 61   storage must ha
12034 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ve already been 
12035 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a 20 20  allocated */..  
12036 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 65  /* Read the page
12037 20 6e 75 6d 62 65 72 20 61 6e 64 20 70 61 67 65   number and page
12038 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 6a   data from the j
12039 6f 75 72 6e 61 6c 20 6f 72 20 73 75 62 2d 6a 6f  ournal or sub-jo
1203a 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 2e  urnal.  ** file.
1203b 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   Return an error
1203c 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c   code to the cal
1203d 6c 65 72 20 69 66 20 61 6e 20 49 4f 20 65 72 72  ler if an IO err
1203e 6f 72 20 6f 63 63 75 72 73 2e 0a 20 20 2a 2f 0a  or occurs..  */.
1203f 20 20 6a 66 64 20 3d 20 69 73 4d 61 69 6e 4a 72    jfd = isMainJr
12040 6e 6c 20 3f 20 70 50 61 67 65 72 2d 3e 6a 66 64  nl ? pPager->jfd
12041 20 3a 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 3b   : pPager->sjfd;
12042 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69  .  rc = read32bi
12043 74 73 28 6a 66 64 2c 20 2a 70 4f 66 66 73 65 74  ts(jfd, *pOffset
12044 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20  , &pgno);.  if( 
12045 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
12046 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20  return rc;.  rc 
12047 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28  = sqlite3OsRead(
12048 6a 66 64 2c 20 61 44 61 74 61 2c 20 70 50 61 67  jfd, aData, pPag
12049 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 2a  er->pageSize, (*
1204a 70 4f 66 66 73 65 74 29 2b 34 29 3b 0a 20 20 69  pOffset)+4);.  i
1204b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1204c 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
1204d 2a 70 4f 66 66 73 65 74 20 2b 3d 20 70 50 61 67  *pOffset += pPag
1204e 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 34  er->pageSize + 4
1204f 20 2b 20 69 73 4d 61 69 6e 4a 72 6e 6c 2a 34 3b   + isMainJrnl*4;
12050 0a 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68  ..  /* Sanity ch
12051 65 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 70 61  ecking on the pa
12052 67 65 2e 20 20 54 68 69 73 20 69 73 20 6d 6f 72  ge.  This is mor
12053 65 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74  e important that
12054 20 49 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20   I originally.  
12055 2a 2a 20 74 68 6f 75 67 68 74 2e 20 20 49 66 20  ** thought.  If 
12056 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20  a power failure 
12057 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 68 65  occurs while the
12058 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 69 6e   journal is bein
12059 67 20 77 72 69 74 74 65 6e 2c 0a 20 20 2a 2a 20  g written,.  ** 
1205a 69 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20 69  it could cause i
1205b 6e 76 61 6c 69 64 20 64 61 74 61 20 74 6f 20 62  nvalid data to b
1205c 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  e written into t
1205d 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 57 65 20  he journal.  We 
1205e 6e 65 65 64 20 74 6f 0a 20 20 2a 2a 20 64 65 74  need to.  ** det
1205f 65 63 74 20 74 68 69 73 20 69 6e 76 61 6c 69 64  ect this invalid
12060 20 64 61 74 61 20 28 77 69 74 68 20 68 69 67 68   data (with high
12061 20 70 72 6f 62 61 62 69 6c 69 74 79 29 20 61 6e   probability) an
12062 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a 20 20 2a  d ignore it..  *
12063 2f 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20  /.  if( pgno==0 
12064 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 5f 4d  || pgno==PAGER_M
12065 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29  J_PGNO(pPager) )
12066 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 69  {.    assert( !i
12067 73 53 61 76 65 70 6e 74 20 29 3b 0a 20 20 20 20  sSavepnt );.    
12068 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f  return SQLITE_DO
12069 4e 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 67  NE;.  }.  if( pg
1206a 6e 6f 3e 28 50 67 6e 6f 29 70 50 61 67 65 72 2d  no>(Pgno)pPager-
1206b 3e 64 62 53 69 7a 65 20 7c 7c 20 73 71 6c 69 74  >dbSize || sqlit
1206c 65 33 42 69 74 76 65 63 54 65 73 74 28 70 44 6f  e3BitvecTest(pDo
1206d 6e 65 2c 20 70 67 6e 6f 29 20 29 7b 0a 20 20 20  ne, pgno) ){.   
1206e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1206f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 4d  K;.  }.  if( isM
12070 61 69 6e 4a 72 6e 6c 20 29 7b 0a 20 20 20 20 72  ainJrnl ){.    r
12071 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 6a  c = read32bits(j
12072 66 64 2c 20 28 2a 70 4f 66 66 73 65 74 29 2d 34  fd, (*pOffset)-4
12073 2c 20 26 63 6b 73 75 6d 29 3b 0a 20 20 20 20 69  , &cksum);.    i
12074 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
12075 63 3b 0a 20 20 20 20 69 66 28 20 21 69 73 53 61  c;.    if( !isSa
12076 76 65 70 6e 74 20 26 26 20 70 61 67 65 72 5f 63  vepnt && pager_c
12077 6b 73 75 6d 28 70 50 61 67 65 72 2c 20 61 44 61  ksum(pPager, aDa
12078 74 61 29 21 3d 63 6b 73 75 6d 20 29 7b 0a 20 20  ta)!=cksum ){.  
12079 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1207a 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20  E_DONE;.    }.  
1207b 7d 0a 0a 20 20 69 66 28 20 70 44 6f 6e 65 20 26  }..  if( pDone &
1207c 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42  & (rc = sqlite3B
1207d 69 74 76 65 63 53 65 74 28 70 44 6f 6e 65 2c 20  itvecSet(pDone, 
1207e 70 67 6e 6f 29 29 21 3d 53 51 4c 49 54 45 5f 4f  pgno))!=SQLITE_O
1207f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
12080 72 63 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  rc;.  }..  asser
12081 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  t( pPager->state
12082 3d 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44  ==PAGER_RESERVED
12083 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74   || pPager->stat
12084 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49  e>=PAGER_EXCLUSI
12085 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  VE );..  /* If t
12086 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 52  he pager is in R
12087 45 53 45 52 56 45 44 20 73 74 61 74 65 2c 20 74  ESERVED state, t
12088 68 65 6e 20 74 68 65 72 65 20 6d 75 73 74 20 62  hen there must b
12089 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 69 73  e a copy of this
1208a 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68  .  ** page in th
1208b 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 49  e pager cache. I
1208c 6e 20 74 68 69 73 20 63 61 73 65 20 6a 75 73 74  n this case just
1208d 20 75 70 64 61 74 65 20 74 68 65 20 70 61 67 65   update the page
1208e 72 20 63 61 63 68 65 2c 0a 20 20 2a 2a 20 6e 6f  r cache,.  ** no
1208f 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  t the database f
12090 69 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69 73  ile. The page is
12091 20 6c 65 66 74 20 6d 61 72 6b 65 64 20 64 69 72   left marked dir
12092 74 79 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e  ty in this case.
12093 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 65 78  .  **.  ** An ex
12094 63 65 70 74 69 6f 6e 20 74 6f 20 74 68 65 20 61  ception to the a
12095 62 6f 76 65 20 72 75 6c 65 3a 20 49 66 20 74 68  bove rule: If th
12096 65 20 64 61 74 61 62 61 73 65 20 69 73 20 69 6e  e database is in
12097 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 0a 20 20   no-sync mode.  
12098 2a 2a 20 61 6e 64 20 61 20 70 61 67 65 20 69 73  ** and a page is
12099 20 6d 6f 76 65 64 20 64 75 72 69 6e 67 20 61 6e   moved during an
1209a 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63   incremental vac
1209b 75 75 6d 20 74 68 65 6e 20 74 68 65 20 70 61 67  uum then the pag
1209c 65 20 6d 61 79 0a 20 20 2a 2a 20 6e 6f 74 20 62  e may.  ** not b
1209d 65 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 63  e in the pager c
1209e 61 63 68 65 2e 20 4c 61 74 65 72 3a 20 69 66 20  ache. Later: if 
1209f 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 49 4f  a malloc() or IO
120a0 20 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 20 20   error occurs.  
120a1 2a 2a 20 64 75 72 69 6e 67 20 61 20 4d 6f 76 65  ** during a Move
120a2 70 61 67 65 28 29 20 63 61 6c 6c 2c 20 74 68 65  page() call, the
120a3 6e 20 74 68 65 20 70 61 67 65 20 6d 61 79 20 6e  n the page may n
120a4 6f 74 20 62 65 20 69 6e 20 74 68 65 20 63 61 63  ot be in the cac
120a5 68 65 0a 20 20 2a 2a 20 65 69 74 68 65 72 2e 20  he.  ** either. 
120a6 53 6f 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e  So the condition
120a7 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 74 68   described in th
120a8 65 20 61 62 6f 76 65 20 70 61 72 61 67 72 61 70  e above paragrap
120a9 68 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 61 73  h is not.  ** as
120aa 73 65 72 74 28 29 61 62 6c 65 2e 0a 20 20 2a 2a  sert()able..  **
120ab 0a 20 20 2a 2a 20 49 66 20 69 6e 20 45 58 43 4c  .  ** If in EXCL
120ac 55 53 49 56 45 20 73 74 61 74 65 2c 20 74 68 65  USIVE state, the
120ad 6e 20 77 65 20 75 70 64 61 74 65 20 74 68 65 20  n we update the 
120ae 70 61 67 65 72 20 63 61 63 68 65 20 69 66 20 69  pager cache if i
120af 74 20 65 78 69 73 74 73 0a 20 20 2a 2a 20 61 6e  t exists.  ** an
120b0 64 20 74 68 65 20 6d 61 69 6e 20 66 69 6c 65 2e  d the main file.
120b1 20 54 68 65 20 70 61 67 65 20 69 73 20 74 68 65   The page is the
120b2 6e 20 6d 61 72 6b 65 64 20 6e 6f 74 20 64 69 72  n marked not dir
120b3 74 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 69  ty..  **.  ** Ti
120b4 63 6b 65 74 20 23 31 31 37 31 3a 20 20 54 68 65  cket #1171:  The
120b5 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e   statement journ
120b6 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e  al might contain
120b7 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 74 68   page content th
120b8 61 74 20 69 73 0a 20 20 2a 2a 20 64 69 66 66 65  at is.  ** diffe
120b9 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 61  rent from the pa
120ba 67 65 20 63 6f 6e 74 65 6e 74 20 61 74 20 74 68  ge content at th
120bb 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 74  e start of the t
120bc 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2a  ransaction..  **
120bd 20 54 68 69 73 20 6f 63 63 75 72 73 20 77 68 65   This occurs whe
120be 6e 20 61 20 70 61 67 65 20 69 73 20 63 68 61 6e  n a page is chan
120bf 67 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 65  ged prior to the
120c0 20 73 74 61 72 74 20 6f 66 20 61 20 73 74 61 74   start of a stat
120c1 65 6d 65 6e 74 0a 20 20 2a 2a 20 74 68 65 6e 20  ement.  ** then 
120c2 63 68 61 6e 67 65 64 20 61 67 61 69 6e 20 77 69  changed again wi
120c3 74 68 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65  thin the stateme
120c4 6e 74 2e 20 20 57 68 65 6e 20 72 6f 6c 6c 69 6e  nt.  When rollin
120c5 67 20 62 61 63 6b 20 73 75 63 68 20 61 0a 20 20  g back such a.  
120c6 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 65 20  ** statement we 
120c7 6d 75 73 74 20 6e 6f 74 20 77 72 69 74 65 20 74  must not write t
120c8 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64  o the original d
120c9 61 74 61 62 61 73 65 20 75 6e 6c 65 73 73 20 77  atabase unless w
120ca 65 20 6b 6e 6f 77 0a 20 20 2a 2a 20 66 6f 72 20  e know.  ** for 
120cb 63 65 72 74 61 69 6e 20 74 68 61 74 20 6f 72 69  certain that ori
120cc 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 65  ginal page conte
120cd 6e 74 73 20 61 72 65 20 73 79 6e 63 65 64 20 69  nts are synced i
120ce 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c  nto the main rol
120cf 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e  lback.  ** journ
120d0 61 6c 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20  al.  Otherwise, 
120d1 61 20 70 6f 77 65 72 20 6c 6f 73 73 20 6d 69 67  a power loss mig
120d2 68 74 20 6c 65 61 76 65 20 6d 6f 64 69 66 69 65  ht leave modifie
120d3 64 20 64 61 74 61 20 69 6e 20 74 68 65 0a 20 20  d data in the.  
120d4 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
120d5 20 77 69 74 68 6f 75 74 20 61 6e 20 65 6e 74 72   without an entr
120d6 79 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63  y in the rollbac
120d7 6b 20 6a 6f 75 72 6e 61 6c 20 74 68 61 74 20 63  k journal that c
120d8 61 6e 0a 20 20 2a 2a 20 72 65 73 74 6f 72 65 20  an.  ** restore 
120d9 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  the database to 
120da 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 66 6f 72  its original for
120db 6d 2e 20 20 54 77 6f 20 63 6f 6e 64 69 74 69 6f  m.  Two conditio
120dc 6e 73 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20  ns must be.  ** 
120dd 6d 65 74 20 62 65 66 6f 72 65 20 77 72 69 74 69  met before writi
120de 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ng to the databa
120df 73 65 20 66 69 6c 65 73 2e 20 28 31 29 20 74 68  se files. (1) th
120e0 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20  e database must 
120e1 62 65 0a 20 20 2a 2a 20 6c 6f 63 6b 65 64 2e 20  be.  ** locked. 
120e2 20 28 32 29 20 77 65 20 6b 6e 6f 77 20 74 68 61   (2) we know tha
120e3 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70  t the original p
120e4 61 67 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 66  age content is f
120e5 75 6c 6c 79 20 73 79 6e 63 65 64 0a 20 20 2a 2a  ully synced.  **
120e6 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75   in the main jou
120e7 72 6e 61 6c 20 65 69 74 68 65 72 20 62 65 63 61  rnal either beca
120e8 75 73 65 20 74 68 65 20 70 61 67 65 20 69 73 20  use the page is 
120e9 6e 6f 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20  not in cache or 
120ea 65 6c 73 65 0a 20 20 2a 2a 20 74 68 65 20 70 61  else.  ** the pa
120eb 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20  ge is marked as 
120ec 6e 65 65 64 53 79 6e 63 3d 3d 30 2e 0a 20 20 2a  needSync==0..  *
120ed 2a 0a 20 20 2a 2a 20 32 30 30 38 2d 30 34 2d 31  *.  ** 2008-04-1
120ee 34 3a 20 20 57 68 65 6e 20 61 74 74 65 6d 70 74  4:  When attempt
120ef 69 6e 67 20 74 6f 20 76 61 63 75 75 6d 20 61 20  ing to vacuum a 
120f0 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65  corrupt database
120f1 20 66 69 6c 65 2c 20 69 74 0a 20 20 2a 2a 20 69   file, it.  ** i
120f2 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 66 61  s possible to fa
120f3 69 6c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6f  il a statement o
120f4 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 68 61  n a database tha
120f5 74 20 64 6f 65 73 20 6e 6f 74 20 79 65 74 20 65  t does not yet e
120f6 78 69 73 74 2e 0a 20 20 2a 2a 20 44 6f 20 6e 6f  xist..  ** Do no
120f7 74 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69  t attempt to wri
120f8 74 65 20 69 66 20 64 61 74 61 62 61 73 65 20 66  te if database f
120f9 69 6c 65 20 68 61 73 20 6e 65 76 65 72 20 62 65  ile has never be
120fa 65 6e 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a  en opened..  */.
120fb 20 20 70 50 67 20 3d 20 70 61 67 65 72 5f 6c 6f    pPg = pager_lo
120fc 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67 6e  okup(pPager, pgn
120fd 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50  o);.  assert( pP
120fe 67 20 7c 7c 20 21 4d 45 4d 44 42 20 29 3b 0a 20  g || !MEMDB );. 
120ff 20 50 41 47 45 52 54 52 41 43 45 28 28 22 50 4c   PAGERTRACE(("PL
12100 41 59 42 41 43 4b 20 25 64 20 70 61 67 65 20 25  AYBACK %d page %
12101 64 20 68 61 73 68 28 25 30 38 78 29 20 25 73 5c  d hash(%08x) %s\
12102 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
12103 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65     PAGERID(pPage
12104 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f  r), pgno, pager_
12105 64 61 74 61 68 61 73 68 28 70 50 61 67 65 72 2d  datahash(pPager-
12106 3e 70 61 67 65 53 69 7a 65 2c 20 61 44 61 74 61  >pageSize, aData
12107 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
12108 20 20 28 69 73 4d 61 69 6e 4a 72 6e 6c 3f 22 6d    (isMainJrnl?"m
12109 61 69 6e 2d 6a 6f 75 72 6e 61 6c 22 3a 22 73 75  ain-journal":"su
1210a 62 2d 6a 6f 75 72 6e 61 6c 22 29 0a 20 20 29 29  b-journal").  ))
1210b 3b 0a 20 20 69 66 28 20 28 70 50 61 67 65 72 2d  ;.  if( (pPager-
1210c 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58  >state>=PAGER_EX
1210d 43 4c 55 53 49 56 45 29 0a 20 20 20 26 26 20 28  CLUSIVE).   && (
1210e 70 50 67 3d 3d 30 20 7c 7c 20 30 3d 3d 28 70 50  pPg==0 || 0==(pP
1210f 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e  g->flags&PGHDR_N
12110 45 45 44 5f 53 59 4e 43 29 29 0a 20 20 20 26 26  EED_SYNC)).   &&
12111 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
12112 66 64 29 0a 20 20 20 26 26 20 21 69 73 55 6e 73  fd).   && !isUns
12113 79 6e 63 0a 20 20 29 7b 0a 20 20 20 20 69 36 34  ync.  ){.    i64
12114 20 6f 66 73 74 20 3d 20 28 70 67 6e 6f 2d 31 29   ofst = (pgno-1)
12115 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61  *(i64)pPager->pa
12116 67 65 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d  geSize;.    rc =
12117 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
12118 70 50 61 67 65 72 2d 3e 66 64 2c 20 61 44 61 74  pPager->fd, aDat
12119 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  a, pPager->pageS
1211a 69 7a 65 2c 20 6f 66 73 74 29 3b 0a 20 20 20 20  ize, ofst);.    
1211b 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d  if( pgno>pPager-
1211c 3e 64 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20  >dbFileSize ){. 
1211d 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46       pPager->dbF
1211e 69 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a  ileSize = pgno;.
1211f 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 50      }.    if( pP
12120 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 29 7b  ager->pBackup ){
12121 0a 20 20 20 20 20 20 43 4f 44 45 43 31 28 70 50  .      CODEC1(pP
12122 61 67 65 72 2c 20 61 44 61 74 61 2c 20 70 67 6e  ager, aData, pgn
12123 6f 2c 20 33 2c 20 72 63 3d 53 51 4c 49 54 45 5f  o, 3, rc=SQLITE_
12124 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 20 20 73 71  NOMEM);.      sq
12125 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 74  lite3BackupUpdat
12126 65 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75  e(pPager->pBacku
12127 70 2c 20 70 67 6e 6f 2c 20 61 44 61 74 61 29 3b  p, pgno, aData);
12128 0a 20 20 20 20 20 20 43 4f 44 45 43 31 28 70 50  .      CODEC1(pP
12129 61 67 65 72 2c 20 61 44 61 74 61 2c 20 70 67 6e  ager, aData, pgn
1212a 6f 2c 20 30 2c 20 72 63 3d 53 51 4c 49 54 45 5f  o, 0, rc=SQLITE_
1212b 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 7d 0a 20 20  NOMEM);.    }.  
1212c 7d 65 6c 73 65 20 69 66 28 20 21 69 73 4d 61 69  }else if( !isMai
1212d 6e 4a 72 6e 6c 20 26 26 20 70 50 67 3d 3d 30 20  nJrnl && pPg==0 
1212e 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69  ){.    /* If thi
1212f 73 20 69 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20  s is a rollback 
12130 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 61  of a savepoint a
12131 6e 64 20 64 61 74 61 20 77 61 73 20 6e 6f 74 20  nd data was not 
12132 77 72 69 74 74 65 6e 20 74 6f 0a 20 20 20 20 2a  written to.    *
12133 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61  * the database a
12134 6e 64 20 74 68 65 20 70 61 67 65 20 69 73 20 6e  nd the page is n
12135 6f 74 20 69 6e 2d 6d 65 6d 6f 72 79 2c 20 74 68  ot in-memory, th
12136 65 72 65 20 69 73 20 61 20 70 6f 74 65 6e 74 69  ere is a potenti
12137 61 6c 0a 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65  al.    ** proble
12138 6d 2e 20 57 68 65 6e 20 74 68 65 20 70 61 67 65  m. When the page
12139 20 69 73 20 6e 65 78 74 20 66 65 74 63 68 65 64   is next fetched
1213a 20 62 79 20 74 68 65 20 62 2d 74 72 65 65 20 6c   by the b-tree l
1213b 61 79 65 72 2c 20 69 74 20 0a 20 20 20 20 2a 2a  ayer, it .    **
1213c 20 77 69 6c 6c 20 62 65 20 72 65 61 64 20 66 72   will be read fr
1213d 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20  om the database 
1213e 66 69 6c 65 2c 20 77 68 69 63 68 20 6d 61 79 20  file, which may 
1213f 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 0a 20  or may not be . 
12140 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 2e 20 0a     ** current. .
12141 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
12142 65 72 65 20 61 72 65 20 61 20 63 6f 75 70 6c 65  ere are a couple
12143 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 77 61   of different wa
12144 79 73 20 74 68 69 73 20 63 61 6e 20 68 61 70 70  ys this can happ
12145 65 6e 2e 20 41 6c 6c 20 61 72 65 20 71 75 69 74  en. All are quit
12146 65 0a 20 20 20 20 2a 2a 20 6f 62 73 63 75 72 65  e.    ** obscure
12147 2e 20 57 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69  . When running i
12148 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f  n synchronous mo
12149 64 65 2c 20 74 68 69 73 20 63 61 6e 20 6f 6e 6c  de, this can onl
1214a 79 20 68 61 70 70 65 6e 20 0a 20 20 20 20 2a 2a  y happen .    **
1214b 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20   if the page is 
1214c 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  on the free-list
1214d 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66   at the start of
1214e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
1214f 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 70 6f  , then.    ** po
12150 70 75 6c 61 74 65 64 2c 20 74 68 65 6e 20 6d 6f  pulated, then mo
12151 76 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65  ved using sqlite
12152 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 29  3PagerMovepage()
12153 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
12154 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20  The solution is 
12155 74 6f 20 61 64 64 20 61 6e 20 69 6e 2d 6d 65 6d  to add an in-mem
12156 6f 72 79 20 70 61 67 65 20 74 6f 20 74 68 65 20  ory page to the 
12157 63 61 63 68 65 20 63 6f 6e 74 61 69 6e 69 6e 67  cache containing
12158 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61  .    ** the data
12159 20 6a 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20   just read from 
1215a 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e  the sub-journal.
1215b 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61   Mark the page a
1215c 73 20 64 69 72 74 79 20 0a 20 20 20 20 2a 2a 20  s dirty .    ** 
1215d 61 6e 64 20 69 66 20 74 68 65 20 70 61 67 65 72  and if the pager
1215e 20 72 65 71 75 69 72 65 73 20 61 20 6a 6f 75 72   requires a jour
1215f 6e 61 6c 2d 73 79 6e 63 2c 20 74 68 65 6e 20 6d  nal-sync, then m
12160 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 20  ark the page as 
12161 0a 20 20 20 20 2a 2a 20 72 65 71 75 69 72 69 6e  .    ** requirin
12162 67 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63  g a journal-sync
12163 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 77 72   before it is wr
12164 69 74 74 65 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20  itten..    */.  
12165 20 20 61 73 73 65 72 74 28 20 69 73 53 61 76 65    assert( isSave
12166 70 6e 74 20 29 3b 0a 20 20 20 20 69 66 28 20 28  pnt );.    if( (
12167 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
12168 72 41 63 71 75 69 72 65 28 70 50 61 67 65 72 2c  rAcquire(pPager,
12169 20 70 67 6e 6f 2c 20 26 70 50 67 2c 20 31 29 29   pgno, &pPg, 1))
1216a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1216b 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1216c 20 20 20 20 7d 0a 20 20 20 20 70 50 67 2d 3e 66      }.    pPg->f
1216d 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e  lags &= ~PGHDR_N
1216e 45 45 44 5f 52 45 41 44 3b 0a 20 20 20 20 73 71  EED_READ;.    sq
1216f 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44  lite3PcacheMakeD
12170 69 72 74 79 28 70 50 67 29 3b 0a 20 20 7d 0a 20  irty(pPg);.  }. 
12171 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20   if( pPg ){.    
12172 2f 2a 20 4e 6f 20 70 61 67 65 20 73 68 6f 75 6c  /* No page shoul
12173 64 20 65 76 65 72 20 62 65 20 65 78 70 6c 69 63  d ever be explic
12174 69 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b  itly rolled back
12175 20 74 68 61 74 20 69 73 20 69 6e 20 75 73 65 2c   that is in use,
12176 20 65 78 63 65 70 74 0a 20 20 20 20 2a 2a 20 66   except.    ** f
12177 6f 72 20 70 61 67 65 20 31 20 77 68 69 63 68 20  or page 1 which 
12178 69 73 20 68 65 6c 64 20 69 6e 20 75 73 65 20 69  is held in use i
12179 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20  n order to keep 
1217a 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a  the lock on the.
1217b 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20      ** database 
1217c 61 63 74 69 76 65 2e 20 48 6f 77 65 76 65 72 20  active. However 
1217d 73 75 63 68 20 61 20 70 61 67 65 20 6d 61 79 20  such a page may 
1217e 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61  be rolled back a
1217f 73 20 61 20 72 65 73 75 6c 74 0a 20 20 20 20 2a  s a result.    *
12180 2a 20 6f 66 20 61 6e 20 69 6e 74 65 72 6e 61 6c  * of an internal
12181 20 65 72 72 6f 72 20 72 65 73 75 6c 74 69 6e 67   error resulting
12182 20 69 6e 20 61 6e 20 61 75 74 6f 6d 61 74 69 63   in an automatic
12183 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20   call to.    ** 
12184 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c  sqlite3PagerRoll
12185 62 61 63 6b 28 29 2e 0a 20 20 20 20 2a 2f 0a 20  back()..    */. 
12186 20 20 20 76 6f 69 64 20 2a 70 44 61 74 61 3b 0a     void *pData;.
12187 20 20 20 20 70 44 61 74 61 20 3d 20 70 50 67 2d      pData = pPg-
12188 3e 70 44 61 74 61 3b 0a 20 20 20 20 6d 65 6d 63  >pData;.    memc
12189 70 79 28 70 44 61 74 61 2c 20 61 44 61 74 61 2c  py(pData, aData,
1218a 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
1218b 65 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  e);.    pPager->
1218c 78 52 65 69 6e 69 74 65 72 28 70 50 67 29 3b 0a  xReiniter(pPg);.
1218d 20 20 20 20 69 66 28 20 69 73 4d 61 69 6e 4a 72      if( isMainJr
1218e 6e 6c 20 26 26 20 28 21 69 73 53 61 76 65 70 6e  nl && (!isSavepn
1218f 74 20 7c 7c 20 2a 70 4f 66 66 73 65 74 3c 3d 70  t || *pOffset<=p
12190 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64  Pager->journalHd
12191 72 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49  r) ){.      /* I
12192 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  f the contents o
12193 66 20 74 68 69 73 20 70 61 67 65 20 77 65 72 65  f this page were
12194 20 6a 75 73 74 20 72 65 73 74 6f 72 65 64 20 66   just restored f
12195 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 0a 20 20  rom the main .  
12196 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66      ** journal f
12197 69 6c 65 2c 20 74 68 65 6e 20 69 74 73 20 63 6f  ile, then its co
12198 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 20 61 73  ntent must be as
12199 20 74 68 65 79 20 77 65 72 65 20 77 68 65 6e 20   they were when 
1219a 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 72  the .      ** tr
1219b 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 66 69  ansaction was fi
1219c 72 73 74 20 6f 70 65 6e 65 64 2e 20 49 6e 20 74  rst opened. In t
1219d 68 69 73 20 63 61 73 65 20 77 65 20 63 61 6e 20  his case we can 
1219e 6d 61 72 6b 20 74 68 65 20 70 61 67 65 0a 20 20  mark the page.  
1219f 20 20 20 20 2a 2a 20 61 73 20 63 6c 65 61 6e 2c      ** as clean,
121a0 20 73 69 6e 63 65 20 74 68 65 72 65 20 77 69 6c   since there wil
121a1 6c 20 62 65 20 6e 6f 20 6e 65 65 64 20 74 6f 20  l be no need to 
121a2 77 72 69 74 65 20 69 74 20 6f 75 74 20 74 6f 20  write it out to 
121a3 74 68 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20  the..      **.  
121a4 20 20 20 20 2a 2a 20 54 68 65 72 65 20 69 73 20      ** There is 
121a5 6f 6e 65 20 65 78 63 65 70 74 69 6f 6e 20 74 6f  one exception to
121a6 20 74 68 69 73 20 72 75 6c 65 2e 20 49 66 20 74   this rule. If t
121a7 68 65 20 70 61 67 65 20 69 73 20 62 65 69 6e 67  he page is being
121a8 20 72 6f 6c 6c 65 64 0a 20 20 20 20 20 20 2a 2a   rolled.      **
121a9 20 62 61 63 6b 20 61 73 20 70 61 72 74 20 6f 66   back as part of
121aa 20 61 20 73 61 76 65 70 6f 69 6e 74 20 28 6f 72   a savepoint (or
121ab 20 73 74 61 74 65 6d 65 6e 74 29 20 72 6f 6c 6c   statement) roll
121ac 62 61 63 6b 20 66 72 6f 6d 20 61 6e 20 0a 20 20  back from an .  
121ad 20 20 20 20 2a 2a 20 75 6e 73 79 6e 63 65 64 20      ** unsynced 
121ae 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d  portion of the m
121af 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ain journal file
121b0 2c 20 74 68 65 6e 20 69 74 20 69 73 20 6e 6f 74  , then it is not
121b1 20 73 61 66 65 0a 20 20 20 20 20 20 2a 2a 20 74   safe.      ** t
121b2 6f 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65 20  o mark the page 
121b3 61 73 20 63 6c 65 61 6e 2e 20 54 68 69 73 20 69  as clean. This i
121b4 73 20 62 65 63 61 75 73 65 20 6d 61 72 6b 69 6e  s because markin
121b5 67 20 74 68 65 20 70 61 67 65 20 61 73 0a 20 20  g the page as.  
121b6 20 20 20 20 2a 2a 20 63 6c 65 61 6e 20 77 69 6c      ** clean wil
121b7 6c 20 63 6c 65 61 72 20 74 68 65 20 50 47 48 44  l clear the PGHD
121b8 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67  R_NEED_SYNC flag
121b9 2e 20 53 69 6e 63 65 20 74 68 65 20 70 61 67 65  . Since the page
121ba 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72   is.      ** alr
121bb 65 61 64 79 20 69 6e 20 74 68 65 20 6a 6f 75 72  eady in the jour
121bc 6e 61 6c 20 66 69 6c 65 20 28 72 65 63 6f 72 64  nal file (record
121bd 65 64 20 69 6e 20 50 61 67 65 72 2e 70 49 6e 4a  ed in Pager.pInJ
121be 6f 75 72 6e 61 6c 29 20 61 6e 64 0a 20 20 20 20  ournal) and.    
121bf 20 20 2a 2a 20 74 68 65 20 50 47 48 44 52 5f 4e    ** the PGHDR_N
121c0 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 69 73  EED_SYNC flag is
121c1 20 63 6c 65 61 72 65 64 2c 20 69 66 20 74 68 65   cleared, if the
121c2 20 70 61 67 65 20 69 73 20 77 72 69 74 74 65 6e   page is written
121c3 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 61 67 61   to.      ** aga
121c4 69 6e 20 77 69 74 68 69 6e 20 74 68 69 73 20 74  in within this t
121c5 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20 77  ransaction, it w
121c6 69 6c 6c 20 62 65 20 6d 61 72 6b 65 64 20 61 73  ill be marked as
121c7 20 64 69 72 74 79 20 62 75 74 0a 20 20 20 20 20   dirty but.     
121c8 20 2a 2a 20 74 68 65 20 50 47 48 44 52 5f 4e 45   ** the PGHDR_NE
121c9 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 77 69 6c  ED_SYNC flag wil
121ca 6c 20 6e 6f 74 20 62 65 20 73 65 74 2e 20 49 74  l not be set. It
121cb 20 63 6f 75 6c 64 20 74 68 65 6e 20 70 6f 74 65   could then pote
121cc 6e 74 69 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a  ntially.      **
121cd 20 62 65 20 77 72 69 74 74 65 6e 20 6f 75 74 20   be written out 
121ce 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  into the databas
121cf 65 20 66 69 6c 65 20 62 65 66 6f 72 65 20 69 74  e file before it
121d0 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20  s journal file. 
121d1 20 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74 20       ** segment 
121d2 69 73 20 73 79 6e 63 65 64 2e 20 49 66 20 61 20  is synced. If a 
121d3 63 72 61 73 68 20 6f 63 63 75 72 73 20 64 75 72  crash occurs dur
121d4 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f 77 69 6e 67  ing or following
121d5 20 74 68 69 73 2c 0a 20 20 20 20 20 20 2a 2a 20   this,.      ** 
121d6 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74  database corrupt
121d7 69 6f 6e 20 6d 61 79 20 65 6e 73 75 65 2e 0a 20  ion may ensue.. 
121d8 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
121d9 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43  lite3PcacheMakeC
121da 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 20 20 7d  lean(pPg);.    }
121db 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43  .#ifdef SQLITE_C
121dc 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70  HECK_PAGES.    p
121dd 50 67 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70  Pg->pageHash = p
121de 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50  ager_pagehash(pP
121df 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f  g);.#endif.    /
121e0 2a 20 49 66 20 74 68 69 73 20 77 61 73 20 70 61  * If this was pa
121e1 67 65 20 31 2c 20 74 68 65 6e 20 72 65 73 74 6f  ge 1, then resto
121e2 72 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  re the value of 
121e3 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73  Pager.dbFileVers
121e4 2e 0a 20 20 20 20 2a 2a 20 44 6f 20 74 68 69 73  ..    ** Do this
121e5 20 62 65 66 6f 72 65 20 61 6e 79 20 64 65 63 6f   before any deco
121e6 64 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 69 66 28  ding. */.    if(
121e7 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20   pgno==1 ){.    
121e8 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72    memcpy(&pPager
121e9 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 26 28  ->dbFileVers, &(
121ea 28 75 38 2a 29 70 44 61 74 61 29 5b 32 34 5d 2c  (u8*)pData)[24],
121eb 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64  sizeof(pPager->d
121ec 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 20  bFileVers));.   
121ed 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 63 6f 64   }..    /* Decod
121ee 65 20 74 68 65 20 70 61 67 65 20 6a 75 73 74 20  e the page just 
121ef 72 65 61 64 20 66 72 6f 6d 20 64 69 73 6b 20 2a  read from disk *
121f0 2f 0a 20 20 20 20 43 4f 44 45 43 31 28 70 50 61  /.    CODEC1(pPa
121f1 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d  ger, pData, pPg-
121f2 3e 70 67 6e 6f 2c 20 33 2c 20 72 63 3d 53 51 4c  >pgno, 3, rc=SQL
121f3 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20  ITE_NOMEM);.    
121f4 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c  sqlite3PcacheRel
121f5 65 61 73 65 28 70 50 67 29 3b 0a 20 20 7d 0a 20  ease(pPg);.  }. 
121f6 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
121f7 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a  *.** Parameter z
121f8 4d 61 73 74 65 72 20 69 73 20 74 68 65 20 6e 61  Master is the na
121f9 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a  me of a master j
121fa 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 20 73  ournal file. A s
121fb 69 6e 67 6c 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a  ingle journal.**
121fc 20 66 69 6c 65 20 74 68 61 74 20 72 65 66 65 72   file that refer
121fd 72 65 64 20 74 6f 20 74 68 65 20 6d 61 73 74 65  red to the maste
121fe 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68  r journal file h
121ff 61 73 20 6a 75 73 74 20 62 65 65 6e 20 72 6f 6c  as just been rol
12200 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 20 54 68 69  led back..** Thi
12201 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73  s routine checks
12202 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62   if it is possib
12203 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65  le to delete the
12204 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
12205 66 69 6c 65 2c 0a 2a 2a 20 61 6e 64 20 64 6f 65  file,.** and doe
12206 73 20 73 6f 20 69 66 20 69 74 20 69 73 2e 0a 2a  s so if it is..*
12207 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 7a 4d  *.** Argument zM
12208 61 73 74 65 72 20 6d 61 79 20 70 6f 69 6e 74 20  aster may point 
12209 74 6f 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61  to Pager.pTmpSpa
1220a 63 65 2e 20 53 6f 20 74 68 61 74 20 62 75 66 66  ce. So that buff
1220b 65 72 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 76  er is not .** av
1220c 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 20  ailable for use 
1220d 77 69 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63  within this func
1220e 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  tion..**.** When
1220f 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61   a master journa
12210 6c 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65  l file is create
12211 64 2c 20 69 74 20 69 73 20 70 6f 70 75 6c 61 74  d, it is populat
12212 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65  ed with the name
12213 73 20 0a 2a 2a 20 6f 66 20 61 6c 6c 20 6f 66 20  s .** of all of 
12214 69 74 73 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61  its child journa
12215 6c 73 2c 20 6f 6e 65 20 61 66 74 65 72 20 61 6e  ls, one after an
12216 6f 74 68 65 72 2c 20 66 6f 72 6d 61 74 74 65 64  other, formatted
12217 20 61 73 20 75 74 66 2d 38 20 0a 2a 2a 20 65 6e   as utf-8 .** en
12218 63 6f 64 65 64 20 74 65 78 74 2e 20 54 68 65 20  coded text. The 
12219 65 6e 64 20 6f 66 20 65 61 63 68 20 63 68 69 6c  end of each chil
1221a 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  d journal file i
1221b 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20 61 20  s marked with a 
1221c 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74  .** nul-terminat
1221d 6f 72 20 62 79 74 65 20 28 30 78 30 30 29 2e 20  or byte (0x00). 
1221e 69 2e 65 2e 20 74 68 65 20 65 6e 74 69 72 65 20  i.e. the entire 
1221f 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 20 6d 61  contents of a ma
12220 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20  ster journal.** 
12221 66 69 6c 65 20 66 6f 72 20 61 20 74 72 61 6e 73  file for a trans
12222 61 63 74 69 6f 6e 20 69 6e 76 6f 6c 76 69 6e 67  action involving
12223 20 74 77 6f 20 64 61 74 61 62 61 73 65 73 20 6d   two databases m
12224 69 67 68 74 20 62 65 3a 0a 2a 2a 0a 2a 2a 20 20  ight be:.**.**  
12225 20 22 2f 68 6f 6d 65 2f 62 69 6c 6c 2f 61 2e 64   "/home/bill/a.d
12226 62 2d 6a 6f 75 72 6e 61 6c 5c 78 30 30 2f 68 6f  b-journal\x00/ho
12227 6d 65 2f 62 69 6c 6c 2f 62 2e 64 62 2d 6a 6f 75  me/bill/b.db-jou
12228 72 6e 61 6c 5c 78 30 30 22 0a 2a 2a 0a 2a 2a 20  rnal\x00".**.** 
12229 41 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  A master journal
1222a 20 66 69 6c 65 20 6d 61 79 20 6f 6e 6c 79 20 62   file may only b
1222b 65 20 64 65 6c 65 74 65 64 20 6f 6e 63 65 20 61  e deleted once a
1222c 6c 6c 20 6f 66 20 69 74 73 20 63 68 69 6c 64 20  ll of its child 
1222d 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 73 20 68 61 76  .** journals hav
1222e 65 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61  e been rolled ba
1222f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ck..**.** This f
12230 75 6e 63 74 69 6f 6e 20 72 65 61 64 73 20 74 68  unction reads th
12231 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
12232 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c  e master-journal
12233 20 66 69 6c 65 20 69 6e 74 6f 20 0a 2a 2a 20 6d   file into .** m
12234 65 6d 6f 72 79 20 61 6e 64 20 6c 6f 6f 70 73 20  emory and loops 
12235 74 68 72 6f 75 67 68 20 65 61 63 68 20 6f 66 20  through each of 
12236 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61  the child journa
12237 6c 20 6e 61 6d 65 73 2e 20 46 6f 72 0a 2a 2a 20  l names. For.** 
12238 65 61 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e  each child journ
12239 61 6c 2c 20 69 74 20 63 68 65 63 6b 73 20 69 66  al, it checks if
1223a 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 69 66 20 74  :.**.**   * if t
1223b 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c  he child journal
1223c 20 65 78 69 73 74 73 2c 20 61 6e 64 20 69 66 20   exists, and if 
1223d 73 6f 0a 2a 2a 20 20 20 2a 20 69 66 20 74 68 65  so.**   * if the
1223e 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63   child journal c
1223f 6f 6e 74 61 69 6e 73 20 61 20 72 65 66 65 72 65  ontains a refere
12240 6e 63 65 20 74 6f 20 6d 61 73 74 65 72 20 6a 6f  nce to master jo
12241 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20 66 69  urnal .**     fi
12242 6c 65 20 7a 4d 61 73 74 65 72 0a 2a 2a 0a 2a 2a  le zMaster.**.**
12243 20 49 66 20 61 20 63 68 69 6c 64 20 6a 6f 75 72   If a child jour
12244 6e 61 6c 20 63 61 6e 20 62 65 20 66 6f 75 6e 64  nal can be found
12245 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 62 6f   that matches bo
12246 74 68 20 6f 66 20 74 68 65 20 63 72 69 74 65 72  th of the criter
12247 69 61 0a 2a 2a 20 61 62 6f 76 65 2c 20 74 68 69  ia.** above, thi
12248 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
12249 6e 73 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67  ns without doing
1224a 20 61 6e 79 74 68 69 6e 67 2e 20 4f 74 68 65 72   anything. Other
1224b 77 69 73 65 2c 20 69 66 0a 2a 2a 20 6e 6f 20 73  wise, if.** no s
1224c 75 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61  uch child journa
1224d 6c 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20  l can be found, 
1224e 66 69 6c 65 20 7a 4d 61 73 74 65 72 20 69 73 20  file zMaster is 
1224f 64 65 6c 65 74 65 64 20 66 72 6f 6d 0a 2a 2a 20  deleted from.** 
12250 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  the file-system 
12251 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 44  using sqlite3OsD
12252 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49  elete()..**.** I
12253 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 77 69  f an IO error wi
12254 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63 74 69  thin this functi
12255 6f 6e 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  on, an error cod
12256 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 54  e is returned. T
12257 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  his.** function 
12258 61 6c 6c 6f 63 61 74 65 73 20 6d 65 6d 6f 72 79  allocates memory
12259 20 62 79 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69   by calling sqli
1225a 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 20 49 66 20  te3Malloc(). If 
1225b 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a  an allocation.**
1225c 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e   fails, SQLITE_N
1225d 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64  OMEM is returned
1225e 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20  . Otherwise, if 
1225f 6e 6f 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f 63 20  no IO or malloc 
12260 65 72 72 6f 72 73 20 0a 2a 2a 20 6f 63 63 75 72  errors .** occur
12261 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  , SQLITE_OK is r
12262 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  eturned..**.** T
12263 4f 44 4f 3a 20 54 68 69 73 20 66 75 6e 63 74 69  ODO: This functi
12264 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 73  on allocates a s
12265 69 6e 67 6c 65 20 62 6c 6f 63 6b 20 6f 66 20 6d  ingle block of m
12266 65 6d 6f 72 79 20 74 6f 20 6c 6f 61 64 0a 2a 2a  emory to load.**
12267 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f 6e 74   the entire cont
12268 65 6e 74 73 20 6f 66 20 74 68 65 20 6d 61 73 74  ents of the mast
12269 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  er journal file.
1226a 20 54 68 69 73 20 63 6f 75 6c 64 20 62 65 0a 2a   This could be.*
1226b 2a 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 6b 69  * a couple of ki
1226c 6c 6f 62 79 74 65 73 20 6f 72 20 73 6f 20 2d 20  lobytes or so - 
1226d 70 6f 74 65 6e 74 69 61 6c 6c 79 20 6c 61 72 67  potentially larg
1226e 65 72 20 74 68 61 6e 20 74 68 65 20 70 61 67 65  er than the page
1226f 20 0a 2a 2a 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74   .** size..*/.st
12270 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 64  atic int pager_d
12271 65 6c 6d 61 73 74 65 72 28 50 61 67 65 72 20 2a  elmaster(Pager *
12272 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 68  pPager, const ch
12273 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20  ar *zMaster){.  
12274 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
12275 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73  s = pPager->pVfs
12276 3b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  ;.  int rc;     
12277 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
12278 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
12279 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
1227a 70 4d 61 73 74 65 72 3b 20 20 20 20 2f 2a 20 4d  pMaster;    /* M
1227b 61 6c 6c 6f 63 27 64 20 6d 61 73 74 65 72 2d 6a  alloc'd master-j
1227c 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63  ournal file desc
1227d 72 69 70 74 6f 72 20 2a 2f 0a 20 20 73 71 6c 69  riptor */.  sqli
1227e 74 65 33 5f 66 69 6c 65 20 2a 70 4a 6f 75 72 6e  te3_file *pJourn
1227f 61 6c 3b 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27  al;   /* Malloc'
12280 64 20 63 68 69 6c 64 2d 6a 6f 75 72 6e 61 6c 20  d child-journal 
12281 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
12282 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73 74  */.  char *zMast
12283 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 20 2f  erJournal = 0; /
12284 2a 20 43 6f 6e 74 65 6e 74 73 20 6f 66 20 6d 61  * Contents of ma
12285 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
12286 65 20 2a 2f 0a 20 20 69 36 34 20 6e 4d 61 73 74  e */.  i64 nMast
12287 65 72 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20  erJournal;      
12288 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6d 61 73 74   /* Size of mast
12289 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
1228a 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  */..  /* Allocat
1228b 65 20 73 70 61 63 65 20 66 6f 72 20 62 6f 74 68  e space for both
1228c 20 74 68 65 20 70 4a 6f 75 72 6e 61 6c 20 61 6e   the pJournal an
1228d 64 20 70 4d 61 73 74 65 72 20 66 69 6c 65 20 64  d pMaster file d
1228e 65 73 63 72 69 70 74 6f 72 73 2e 0a 20 20 2a 2a  escriptors..  **
1228f 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
12290 6f 70 65 6e 20 74 68 65 20 6d 61 73 74 65 72 20  open the master 
12291 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72  journal file for
12292 20 72 65 61 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20   reading..  */. 
12293 20 70 4d 61 73 74 65 72 20 3d 20 28 73 71 6c 69   pMaster = (sqli
12294 74 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74  te3_file *)sqlit
12295 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 56 66  e3MallocZero(pVf
12296 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 2a 20 32 29  s->szOsFile * 2)
12297 3b 0a 20 20 70 4a 6f 75 72 6e 61 6c 20 3d 20 28  ;.  pJournal = (
12298 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 28  sqlite3_file *)(
12299 28 28 75 38 20 2a 29 70 4d 61 73 74 65 72 29 20  ((u8 *)pMaster) 
1229a 2b 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65  + pVfs->szOsFile
1229b 29 3b 0a 20 20 69 66 28 20 21 70 4d 61 73 74 65  );.  if( !pMaste
1229c 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  r ){.    rc = SQ
1229d 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65  LITE_NOMEM;.  }e
1229e 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69  lse{.    const i
1229f 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 51 4c 49  nt flags = (SQLI
122a0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
122a1 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53  |SQLITE_OPEN_MAS
122a2 54 45 52 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20  TER_JOURNAL);.  
122a3 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
122a4 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4d 61 73 74  Open(pVfs, zMast
122a5 65 72 2c 20 70 4d 61 73 74 65 72 2c 20 66 6c 61  er, pMaster, fla
122a6 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66  gs, 0);.  }.  if
122a7 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
122a8 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72  ) goto delmaster
122a9 5f 6f 75 74 3b 0a 0a 20 20 72 63 20 3d 20 73 71  _out;..  rc = sq
122aa 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
122ab 70 4d 61 73 74 65 72 2c 20 26 6e 4d 61 73 74 65  pMaster, &nMaste
122ac 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 69 66 28  rJournal);.  if(
122ad 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
122ae 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f   goto delmaster_
122af 6f 75 74 3b 0a 0a 20 20 69 66 28 20 6e 4d 61 73  out;..  if( nMas
122b0 74 65 72 4a 6f 75 72 6e 61 6c 3e 30 20 29 7b 0a  terJournal>0 ){.
122b1 20 20 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e      char *zJourn
122b2 61 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d  al;.    char *zM
122b3 61 73 74 65 72 50 74 72 20 3d 20 30 3b 0a 20 20  asterPtr = 0;.  
122b4 20 20 69 6e 74 20 6e 4d 61 73 74 65 72 50 74 72    int nMasterPtr
122b5 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e   = pVfs->mxPathn
122b6 61 6d 65 2b 31 3b 0a 0a 20 20 20 20 2f 2a 20 4c  ame+1;..    /* L
122b7 6f 61 64 20 74 68 65 20 65 6e 74 69 72 65 20 6d  oad the entire m
122b8 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
122b9 6c 65 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62  le into space ob
122ba 74 61 69 6e 65 64 20 66 72 6f 6d 0a 20 20 20 20  tained from.    
122bb 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  ** sqlite3_mallo
122bc 63 28 29 20 61 6e 64 20 70 6f 69 6e 74 65 64 20  c() and pointed 
122bd 74 6f 20 62 79 20 7a 4d 61 73 74 65 72 4a 6f 75  to by zMasterJou
122be 72 6e 61 6c 2e 20 0a 20 20 20 20 2a 2f 0a 20 20  rnal. .    */.  
122bf 20 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c    zMasterJournal
122c0 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
122c1 28 28 69 6e 74 29 6e 4d 61 73 74 65 72 4a 6f 75  ((int)nMasterJou
122c2 72 6e 61 6c 20 2b 20 6e 4d 61 73 74 65 72 50 74  rnal + nMasterPt
122c3 72 20 2b 20 31 29 3b 0a 20 20 20 20 69 66 28 20  r + 1);.    if( 
122c4 21 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20  !zMasterJournal 
122c5 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
122c6 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
122c7 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72    goto delmaster
122c8 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _out;.    }.    
122c9 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 26 7a 4d  zMasterPtr = &zM
122ca 61 73 74 65 72 4a 6f 75 72 6e 61 6c 5b 6e 4d 61  asterJournal[nMa
122cb 73 74 65 72 4a 6f 75 72 6e 61 6c 2b 31 5d 3b 0a  sterJournal+1];.
122cc 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
122cd 4f 73 52 65 61 64 28 70 4d 61 73 74 65 72 2c 20  OsRead(pMaster, 
122ce 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2c 20  zMasterJournal, 
122cf 28 69 6e 74 29 6e 4d 61 73 74 65 72 4a 6f 75 72  (int)nMasterJour
122d0 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  nal, 0);.    if(
122d1 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
122d2 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f   goto delmaster_
122d3 6f 75 74 3b 0a 20 20 20 20 7a 4d 61 73 74 65 72  out;.    zMaster
122d4 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 72 4a  Journal[nMasterJ
122d5 6f 75 72 6e 61 6c 5d 20 3d 20 30 3b 0a 0a 20 20  ournal] = 0;..  
122d6 20 20 7a 4a 6f 75 72 6e 61 6c 20 3d 20 7a 4d 61    zJournal = zMa
122d7 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20  sterJournal;.   
122d8 20 77 68 69 6c 65 28 20 28 7a 4a 6f 75 72 6e 61   while( (zJourna
122d9 6c 2d 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  l-zMasterJournal
122da 29 3c 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  )<nMasterJournal
122db 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 78   ){.      int ex
122dc 69 73 74 73 3b 0a 20 20 20 20 20 20 72 63 20 3d  ists;.      rc =
122dd 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73   sqlite3OsAccess
122de 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c  (pVfs, zJournal,
122df 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45   SQLITE_ACCESS_E
122e0 58 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 3b  XISTS, &exists);
122e1 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
122e2 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
122e3 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74      goto delmast
122e4 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a  er_out;.      }.
122e5 20 20 20 20 20 20 69 66 28 20 65 78 69 73 74 73        if( exists
122e6 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4f   ){.        /* O
122e7 6e 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  ne of the journa
122e8 6c 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ls pointed to by
122e9 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
122ea 6e 61 6c 20 65 78 69 73 74 73 2e 0a 20 20 20 20  nal exists..    
122eb 20 20 20 20 2a 2a 20 4f 70 65 6e 20 69 74 20 61      ** Open it a
122ec 6e 64 20 63 68 65 63 6b 20 69 66 20 69 74 20 70  nd check if it p
122ed 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6d 61 73  oints at the mas
122ee 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 0a  ter journal. If.
122ef 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 2c 20 72          ** so, r
122f0 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 65  eturn without de
122f1 6c 65 74 69 6e 67 20 74 68 65 20 6d 61 73 74 65  leting the maste
122f2 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a  r journal file..
122f3 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
122f4 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 20     int c;.      
122f5 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 53    int flags = (S
122f6 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
122f7 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  NLY|SQLITE_OPEN_
122f8 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20  MAIN_JOURNAL);. 
122f9 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
122fa 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20  te3OsOpen(pVfs, 
122fb 7a 4a 6f 75 72 6e 61 6c 2c 20 70 4a 6f 75 72 6e  zJournal, pJourn
122fc 61 6c 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20  al, flags, 0);. 
122fd 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
122fe 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
122ff 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61        goto delma
12300 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20  ster_out;.      
12301 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 72 63 20    }..        rc 
12302 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72  = readMasterJour
12303 6e 61 6c 28 70 4a 6f 75 72 6e 61 6c 2c 20 7a 4d  nal(pJournal, zM
12304 61 73 74 65 72 50 74 72 2c 20 6e 4d 61 73 74 65  asterPtr, nMaste
12305 72 50 74 72 29 3b 0a 20 20 20 20 20 20 20 20 73  rPtr);.        s
12306 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 4a  qlite3OsClose(pJ
12307 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20 20 20  ournal);.       
12308 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
12309 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
1230a 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f  goto delmaster_o
1230b 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20  ut;.        }.. 
1230c 20 20 20 20 20 20 20 63 20 3d 20 7a 4d 61 73 74         c = zMast
1230d 65 72 50 74 72 5b 30 5d 21 3d 30 20 26 26 20 73  erPtr[0]!=0 && s
1230e 74 72 63 6d 70 28 7a 4d 61 73 74 65 72 50 74 72  trcmp(zMasterPtr
1230f 2c 20 7a 4d 61 73 74 65 72 29 3d 3d 30 3b 0a 20  , zMaster)==0;. 
12310 20 20 20 20 20 20 20 69 66 28 20 63 20 29 7b 0a         if( c ){.
12311 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20            /* We 
12312 68 61 76 65 20 61 20 6d 61 74 63 68 2e 20 44 6f  have a match. Do
12313 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65 20   not delete the 
12314 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
12315 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ile. */.        
12316 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72    goto delmaster
12317 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  _out;.        }.
12318 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 4a        }.      zJ
12319 6f 75 72 6e 61 6c 20 2b 3d 20 28 73 71 6c 69 74  ournal += (sqlit
1231a 65 33 53 74 72 6c 65 6e 33 30 28 7a 4a 6f 75 72  e3Strlen30(zJour
1231b 6e 61 6c 29 2b 31 29 3b 0a 20 20 20 20 7d 0a 20  nal)+1);.    }. 
1231c 20 7d 0a 20 20 0a 20 20 72 63 20 3d 20 73 71 6c   }.  .  rc = sql
1231d 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66  ite3OsDelete(pVf
1231e 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a  s, zMaster, 0);.
1231f 0a 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3a 0a  .delmaster_out:.
12320 20 20 69 66 28 20 7a 4d 61 73 74 65 72 4a 6f 75    if( zMasterJou
12321 72 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69  rnal ){.    sqli
12322 74 65 33 5f 66 72 65 65 28 7a 4d 61 73 74 65 72  te3_free(zMaster
12323 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 7d 20 20 0a  Journal);.  }  .
12324 20 20 69 66 28 20 70 4d 61 73 74 65 72 20 29 7b    if( pMaster ){
12325 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c  .    sqlite3OsCl
12326 6f 73 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20  ose(pMaster);.  
12327 20 20 61 73 73 65 72 74 28 20 21 69 73 4f 70 65    assert( !isOpe
12328 6e 28 70 4a 6f 75 72 6e 61 6c 29 20 29 3b 0a 20  n(pJournal) );. 
12329 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65   }.  sqlite3_fre
1232a 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72 65  e(pMaster);.  re
1232b 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
1232c 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
1232d 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 61 6e   is used to chan
1232e 67 65 20 74 68 65 20 61 63 74 75 61 6c 20 73 69  ge the actual si
1232f 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ze of the databa
12330 73 65 20 0a 2a 2a 20 66 69 6c 65 20 69 6e 20 74  se .** file in t
12331 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2e 20  he file-system. 
12332 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e  This only happen
12333 73 20 77 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e  s when committin
12334 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  g a transaction,
12335 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 62  .** or rolling b
12336 61 63 6b 20 61 20 74 72 61 6e 73 61 63 74 69 6f  ack a transactio
12337 6e 20 28 69 6e 63 6c 75 64 69 6e 67 20 72 6f 6c  n (including rol
12338 6c 69 6e 67 20 62 61 63 6b 20 61 20 68 6f 74 2d  ling back a hot-
12339 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2a 0a 2a 2a 20  journal)..**.** 
1233a 49 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  If the main data
1233b 62 61 73 65 20 66 69 6c 65 20 69 73 20 6e 6f 74  base file is not
1233c 20 6f 70 65 6e 2c 20 6f 72 20 61 6e 20 65 78 63   open, or an exc
1233d 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 6e  lusive lock is n
1233e 6f 74 0a 2a 2a 20 68 65 6c 64 2c 20 74 68 69 73  ot.** held, this
1233f 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
12340 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 65 2c  o-op. Otherwise,
12341 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
12342 20 66 69 6c 65 20 69 73 0a 2a 2a 20 63 68 61 6e   file is.** chan
12343 67 65 64 20 74 6f 20 6e 50 61 67 65 20 70 61 67  ged to nPage pag
12344 65 73 20 28 6e 50 61 67 65 2a 70 50 61 67 65 72  es (nPage*pPager
12345 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73  ->pageSize bytes
12346 29 2e 20 49 66 20 74 68 65 20 66 69 6c 65 0a 2a  ). If the file.*
12347 2a 20 6f 6e 20 64 69 73 6b 20 69 73 20 63 75 72  * on disk is cur
12348 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 20 74 68  rently larger th
12349 61 6e 20 6e 50 61 67 65 20 70 61 67 65 73 2c 20  an nPage pages, 
1234a 74 68 65 6e 20 75 73 65 20 74 68 65 20 56 46 53  then use the VFS
1234b 0a 2a 2a 20 78 54 72 75 6e 63 61 74 65 28 29 20  .** xTruncate() 
1234c 6d 65 74 68 6f 64 20 74 6f 20 74 72 75 6e 63 61  method to trunca
1234d 74 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c  te it..**.** Or,
1234e 20 69 74 20 6d 69 67 68 74 20 6d 69 67 68 74 20   it might might 
1234f 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74  be the case that
12350 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73   the file on dis
12351 6b 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61  k is smaller tha
12352 6e 20 0a 2a 2a 20 6e 50 61 67 65 20 70 61 67 65  n .** nPage page
12353 73 2e 20 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e  s. Some operatin
12354 67 20 73 79 73 74 65 6d 20 69 6d 70 6c 65 6d 65  g system impleme
12355 6e 74 61 74 69 6f 6e 73 20 63 61 6e 20 67 65 74  ntations can get
12356 20 63 6f 6e 66 75 73 65 64 20 69 66 20 0a 2a 2a   confused if .**
12357 20 79 6f 75 20 74 72 79 20 74 6f 20 74 72 75 6e   you try to trun
12358 63 61 74 65 20 61 20 66 69 6c 65 20 74 6f 20 73  cate a file to s
12359 6f 6d 65 20 73 69 7a 65 20 74 68 61 74 20 69 73  ome size that is
1235a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69 74 20   larger than it 
1235b 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 69 73  .** currently is
1235c 2c 20 73 6f 20 64 65 74 65 63 74 20 74 68 69 73  , so detect this
1235d 20 63 61 73 65 20 61 6e 64 20 77 72 69 74 65 20   case and write 
1235e 61 20 73 69 6e 67 6c 65 20 7a 65 72 6f 20 62 79  a single zero by
1235f 74 65 20 74 6f 20 0a 2a 2a 20 74 68 65 20 65 6e  te to .** the en
12360 64 20 6f 66 20 74 68 65 20 6e 65 77 20 66 69 6c  d of the new fil
12361 65 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a  e instead..**.**
12362 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
12363 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
12364 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72  . If an IO error
12365 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6d 6f   occurs while mo
12366 64 69 66 79 69 6e 67 0a 2a 2a 20 74 68 65 20 64  difying.** the d
12367 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 72 65  atabase file, re
12368 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63  turn the error c
12369 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65  ode to the calle
1236a 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
1236b 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28   pager_truncate(
1236c 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50  Pager *pPager, P
1236d 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e  gno nPage){.  in
1236e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
1236f 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ;.  if( pPager->
12370 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43  state>=PAGER_EXC
12371 4c 55 53 49 56 45 20 26 26 20 69 73 4f 70 65 6e  LUSIVE && isOpen
12372 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a  (pPager->fd) ){.
12373 20 20 20 20 69 36 34 20 63 75 72 72 65 6e 74 53      i64 currentS
12374 69 7a 65 2c 20 6e 65 77 53 69 7a 65 3b 0a 20 20  ize, newSize;.  
12375 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73 20 69 74    /* TODO: Is it
12376 20 73 61 66 65 20 74 6f 20 75 73 65 20 50 61 67   safe to use Pag
12377 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 20 68 65  er.dbFileSize he
12378 72 65 3f 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  re? */.    rc = 
12379 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a  sqlite3OsFileSiz
1237a 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 63  e(pPager->fd, &c
1237b 75 72 72 65 6e 74 53 69 7a 65 29 3b 0a 20 20 20  urrentSize);.   
1237c 20 6e 65 77 53 69 7a 65 20 3d 20 70 50 61 67 65   newSize = pPage
1237d 72 2d 3e 70 61 67 65 53 69 7a 65 2a 28 69 36 34  r->pageSize*(i64
1237e 29 6e 50 61 67 65 3b 0a 20 20 20 20 69 66 28 20  )nPage;.    if( 
1237f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
12380 20 63 75 72 72 65 6e 74 53 69 7a 65 21 3d 6e 65   currentSize!=ne
12381 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 69  wSize ){.      i
12382 66 28 20 63 75 72 72 65 6e 74 53 69 7a 65 3e 6e  f( currentSize>n
12383 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  ewSize ){.      
12384 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
12385 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d  Truncate(pPager-
12386 3e 66 64 2c 20 6e 65 77 53 69 7a 65 29 3b 0a 20  >fd, newSize);. 
12387 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
12388 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
12389 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e  OsWrite(pPager->
1238a 66 64 2c 20 22 22 2c 20 31 2c 20 6e 65 77 53 69  fd, "", 1, newSi
1238b 7a 65 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ze-1);.      }. 
1238c 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
1238d 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1238e 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65    pPager->dbFile
1238f 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20  Size = nPage;.  
12390 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
12391 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
12392 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61  /*.** Set the va
12393 6c 75 65 20 6f 66 20 74 68 65 20 50 61 67 65 72  lue of the Pager
12394 2e 73 65 63 74 6f 72 53 69 7a 65 20 76 61 72 69  .sectorSize vari
12395 61 62 6c 65 20 66 6f 72 20 74 68 65 20 67 69 76  able for the giv
12396 65 6e 0a 2a 2a 20 70 61 67 65 72 20 62 61 73 65  en.** pager base
12397 64 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 72  d on the value r
12398 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 78  eturned by the x
12399 53 65 63 74 6f 72 53 69 7a 65 20 6d 65 74 68 6f  SectorSize metho
1239a 64 0a 2a 2a 20 6f 66 20 74 68 65 20 6f 70 65 6e  d.** of the open
1239b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
1239c 54 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20  The sector size 
1239d 77 69 6c 6c 20 62 65 20 75 73 65 64 20 75 73 65  will be used use
1239e 64 20 0a 2a 2a 20 74 6f 20 64 65 74 65 72 6d 69  d .** to determi
1239f 6e 65 20 74 68 65 20 73 69 7a 65 20 61 6e 64 20  ne the size and 
123a0 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 6a 6f 75  alignment of jou
123a1 72 6e 61 6c 20 68 65 61 64 65 72 20 61 6e 64 20  rnal header and 
123a2 0a 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  .** master journ
123a3 61 6c 20 70 6f 69 6e 74 65 72 73 20 77 69 74 68  al pointers with
123a4 69 6e 20 63 72 65 61 74 65 64 20 6a 6f 75 72 6e  in created journ
123a5 61 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  al files..**.** 
123a6 46 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69  For temporary fi
123a7 6c 65 73 20 74 68 65 20 65 66 66 65 63 74 69 76  les the effectiv
123a8 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 73  e sector size is
123a9 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65   always 512 byte
123aa 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69  s..**.** Otherwi
123ab 73 65 2c 20 66 6f 72 20 6e 6f 6e 2d 74 65 6d 70  se, for non-temp
123ac 6f 72 61 72 79 20 66 69 6c 65 73 2c 20 74 68 65  orary files, the
123ad 20 65 66 66 65 63 74 69 76 65 20 73 65 63 74 6f   effective secto
123ae 72 20 73 69 7a 65 20 69 73 0a 2a 2a 20 74 68 65  r size is.** the
123af 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
123b0 62 79 20 74 68 65 20 78 53 65 63 74 6f 72 53 69  by the xSectorSi
123b1 7a 65 28 29 20 6d 65 74 68 6f 64 20 72 6f 75 6e  ze() method roun
123b2 64 65 64 20 75 70 20 74 6f 20 35 31 32 20 69 66  ded up to 512 if
123b3 0a 2a 2a 20 69 74 20 69 73 20 6c 65 73 73 20 74  .** it is less t
123b4 68 61 6e 20 35 31 32 2c 20 6f 72 20 72 6f 75 6e  han 512, or roun
123b5 64 65 64 20 64 6f 77 6e 20 74 6f 20 4d 41 58 5f  ded down to MAX_
123b6 53 45 43 54 4f 52 5f 53 49 5a 45 20 69 66 20 69  SECTOR_SIZE if i
123b7 74 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72 20  t.** is greater 
123b8 74 68 61 6e 20 4d 41 58 5f 53 45 43 54 4f 52 5f  than MAX_SECTOR_
123b9 53 49 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  SIZE..*/.static 
123ba 76 6f 69 64 20 73 65 74 53 65 63 74 6f 72 53 69  void setSectorSi
123bb 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ze(Pager *pPager
123bc 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f  ){.  assert( isO
123bd 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20  pen(pPager->fd) 
123be 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  || pPager->tempF
123bf 69 6c 65 20 29 3b 0a 0a 20 20 69 66 28 20 21 70  ile );..  if( !p
123c0 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20  Pager->tempFile 
123c1 29 7b 0a 20 20 20 20 2f 2a 20 53 65 63 74 6f 72  ){.    /* Sector
123c2 20 73 69 7a 65 20 64 6f 65 73 6e 27 74 20 6d 61   size doesn't ma
123c3 74 74 65 72 20 66 6f 72 20 74 65 6d 70 6f 72 61  tter for tempora
123c4 72 79 20 66 69 6c 65 73 2e 20 41 6c 73 6f 2c 20  ry files. Also, 
123c5 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20  the file.    ** 
123c6 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 62 65 65  may not have bee
123c7 6e 20 6f 70 65 6e 65 64 20 79 65 74 2c 20 69 6e  n opened yet, in
123c8 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20   which case the 
123c9 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 0a 20  OsSectorSize(). 
123ca 20 20 20 2a 2a 20 63 61 6c 6c 20 77 69 6c 6c 20     ** call will 
123cb 73 65 67 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2f  segfault..    */
123cc 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63  .    pPager->sec
123cd 74 6f 72 53 69 7a 65 20 3d 20 73 71 6c 69 74 65  torSize = sqlite
123ce 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 70 50  3OsSectorSize(pP
123cf 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 7d 0a 20  ager->fd);.  }. 
123d0 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 65 63   if( pPager->sec
123d1 74 6f 72 53 69 7a 65 3c 35 31 32 20 29 7b 0a 20  torSize<512 ){. 
123d2 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f     pPager->secto
123d3 72 53 69 7a 65 20 3d 20 35 31 32 3b 0a 20 20 7d  rSize = 512;.  }
123d4 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  .  if( pPager->s
123d5 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45  ectorSize>MAX_SE
123d6 43 54 4f 52 5f 53 49 5a 45 20 29 7b 0a 20 20 20  CTOR_SIZE ){.   
123d7 20 61 73 73 65 72 74 28 20 4d 41 58 5f 53 45 43   assert( MAX_SEC
123d8 54 4f 52 5f 53 49 5a 45 3e 3d 35 31 32 20 29 3b  TOR_SIZE>=512 );
123d9 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63  .    pPager->sec
123da 74 6f 72 53 69 7a 65 20 3d 20 4d 41 58 5f 53 45  torSize = MAX_SE
123db 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 7d 0a 7d  CTOR_SIZE;.  }.}
123dc 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b  ../*.** Playback
123dd 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64   the journal and
123de 20 74 68 75 73 20 72 65 73 74 6f 72 65 20 74 68   thus restore th
123df 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
123e0 74 6f 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 20  to.** the state 
123e1 69 74 20 77 61 73 20 69 6e 20 62 65 66 6f 72 65  it was in before
123e2 20 77 65 20 73 74 61 72 74 65 64 20 6d 61 6b 69   we started maki
123e3 6e 67 20 63 68 61 6e 67 65 73 2e 20 20 0a 2a 2a  ng changes.  .**
123e4 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20  .** The journal 
123e5 66 69 6c 65 20 66 6f 72 6d 61 74 20 69 73 20 61  file format is a
123e6 73 20 66 6f 6c 6c 6f 77 73 3a 20 0a 2a 2a 0a 2a  s follows: .**.*
123e7 2a 20 20 28 31 29 20 20 38 20 62 79 74 65 20 70  *  (1)  8 byte p
123e8 72 65 66 69 78 2e 20 20 41 20 63 6f 70 79 20 6f  refix.  A copy o
123e9 66 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b  f aJournalMagic[
123ea 5d 2e 0a 2a 2a 20 20 28 32 29 20 20 34 20 62 79  ]..**  (2)  4 by
123eb 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e  te big-endian in
123ec 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74  teger which is t
123ed 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c  he number of val
123ee 69 64 20 70 61 67 65 20 72 65 63 6f 72 64 73 0a  id page records.
123ef 2a 2a 20 20 20 20 20 20 20 69 6e 20 74 68 65 20  **       in the 
123f0 6a 6f 75 72 6e 61 6c 2e 20 20 49 66 20 74 68 69  journal.  If thi
123f1 73 20 76 61 6c 75 65 20 69 73 20 30 78 66 66 66  s value is 0xfff
123f2 66 66 66 66 66 2c 20 74 68 65 6e 20 63 6f 6d 70  fffff, then comp
123f3 75 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  ute the.**      
123f4 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20   number of page 
123f5 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 65  records from the
123f6 20 6a 6f 75 72 6e 61 6c 20 73 69 7a 65 2e 0a 2a   journal size..*
123f7 2a 20 20 28 33 29 20 20 34 20 62 79 74 65 20 62  *  (3)  4 byte b
123f8 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65  ig-endian intege
123f9 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 69  r which is the i
123fa 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 66 6f 72  nitial value for
123fb 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 20 73   the .**       s
123fc 61 6e 69 74 79 20 63 68 65 63 6b 73 75 6d 2e 0a  anity checksum..
123fd 2a 2a 20 20 28 34 29 20 20 34 20 62 79 74 65 20  **  (4)  4 byte 
123fe 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73  integer which is
123ff 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
12400 61 67 65 73 20 74 6f 20 74 72 75 6e 63 61 74 65  ages to truncate
12401 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 64 61   the.**       da
12402 74 61 62 61 73 65 20 74 6f 20 64 75 72 69 6e 67  tabase to during
12403 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 20   a rollback..** 
12404 20 28 35 29 20 20 34 20 62 79 74 65 20 62 69 67   (5)  4 byte big
12405 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20  -endian integer 
12406 77 68 69 63 68 20 69 73 20 74 68 65 20 73 65 63  which is the sec
12407 74 6f 72 20 73 69 7a 65 2e 20 20 54 68 65 20 68  tor size.  The h
12408 65 61 64 65 72 0a 2a 2a 20 20 20 20 20 20 20 69  eader.**       i
12409 73 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65  s this many byte
1240a 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 20 28  s in size..**  (
1240b 36 29 20 20 34 20 62 79 74 65 20 62 69 67 2d 65  6)  4 byte big-e
1240c 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68  ndian integer wh
1240d 69 63 68 20 69 73 20 74 68 65 20 70 61 67 65 20  ich is the page 
1240e 63 61 73 65 2e 0a 2a 2a 20 20 28 37 29 20 20 34  case..**  (7)  4
1240f 20 62 79 74 65 20 69 6e 74 65 67 65 72 20 77 68   byte integer wh
12410 69 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65  ich is the numbe
12411 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
12412 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
12413 0a 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65 2e 20  .**       name. 
12414 20 54 68 65 20 76 61 6c 75 65 20 6d 61 79 20 62   The value may b
12415 65 20 7a 65 72 6f 20 28 69 6e 64 69 63 61 74 65  e zero (indicate
12416 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6e   that there is n
12417 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 20 20 20 20  o master.**     
12418 20 20 6a 6f 75 72 6e 61 6c 2e 29 0a 2a 2a 20 20    journal.).**  
12419 28 38 29 20 20 4e 20 62 79 74 65 73 20 6f 66 20  (8)  N bytes of 
1241a 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
1241b 61 6c 20 6e 61 6d 65 2e 20 20 54 68 65 20 6e 61  al name.  The na
1241c 6d 65 20 77 69 6c 6c 20 62 65 20 6e 75 6c 2d 74  me will be nul-t
1241d 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 20 20 20  erminated.**    
1241e 20 20 20 61 6e 64 20 6d 69 67 68 74 20 62 65 20     and might be 
1241f 73 68 6f 72 74 65 72 20 74 68 61 6e 20 74 68 65  shorter than the
12420 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d   value read from
12421 20 28 35 29 2e 20 20 49 66 20 74 68 65 20 66 69   (5).  If the fi
12422 72 73 74 20 62 79 74 65 0a 2a 2a 20 20 20 20 20  rst byte.**     
12423 20 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 69 73    of the name is
12424 20 5c 30 30 30 20 74 68 65 6e 20 74 68 65 72 65   \000 then there
12425 20 69 73 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f   is no master jo
12426 75 72 6e 61 6c 2e 20 20 54 68 65 20 6d 61 73 74  urnal.  The mast
12427 65 72 0a 2a 2a 20 20 20 20 20 20 20 6a 6f 75 72  er.**       jour
12428 6e 61 6c 20 6e 61 6d 65 20 69 73 20 73 74 6f 72  nal name is stor
12429 65 64 20 69 6e 20 55 54 46 2d 38 2e 0a 2a 2a 20  ed in UTF-8..** 
1242a 20 28 39 29 20 20 5a 65 72 6f 20 6f 72 20 6d 6f   (9)  Zero or mo
1242b 72 65 20 70 61 67 65 73 20 69 6e 73 74 61 6e 63  re pages instanc
1242c 65 73 2c 20 65 61 63 68 20 61 73 20 66 6f 6c 6c  es, each as foll
1242d 6f 77 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 2b  ows:.**        +
1242e 20 20 34 20 62 79 74 65 20 70 61 67 65 20 6e 75    4 byte page nu
1242f 6d 62 65 72 2e 0a 2a 2a 20 20 20 20 20 20 20 20  mber..**        
12430 2b 20 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  +  pPager->pageS
12431 69 7a 65 20 62 79 74 65 73 20 6f 66 20 64 61 74  ize bytes of dat
12432 61 2e 0a 2a 2a 20 20 20 20 20 20 20 20 2b 20 20  a..**        +  
12433 34 20 62 79 74 65 20 63 68 65 63 6b 73 75 6d 0a  4 byte checksum.
12434 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 65 20 73 70  **.** When we sp
12435 65 61 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  eak of the journ
12436 61 6c 20 68 65 61 64 65 72 2c 20 77 65 20 6d 65  al header, we me
12437 61 6e 20 74 68 65 20 66 69 72 73 74 20 38 20 69  an the first 8 i
12438 74 65 6d 73 20 61 62 6f 76 65 2e 0a 2a 2a 20 45  tems above..** E
12439 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65  ach entry in the
1243a 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6e 20 69   journal is an i
1243b 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 39  nstance of the 9
1243c 74 68 20 69 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 43  th item..**.** C
1243d 61 6c 6c 20 74 68 65 20 76 61 6c 75 65 20 66 72  all the value fr
1243e 6f 6d 20 74 68 65 20 73 65 63 6f 6e 64 20 62 75  om the second bu
1243f 6c 6c 65 74 20 22 6e 52 65 63 22 2e 20 20 6e 52  llet "nRec".  nR
12440 65 63 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ec is the number
12441 20 6f 66 0a 2a 2a 20 76 61 6c 69 64 20 70 61 67   of.** valid pag
12442 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65  e entries in the
12443 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e 20 6d 6f   journal.  In mo
12444 73 74 20 63 61 73 65 73 2c 20 79 6f 75 20 63 61  st cases, you ca
12445 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a  n compute the.**
12446 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 20 66   value of nRec f
12447 72 6f 6d 20 74 68 65 20 73 69 7a 65 20 6f 66 20  rom the size of 
12448 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
12449 2e 20 20 42 75 74 20 69 66 20 61 20 70 6f 77 65  .  But if a powe
1244a 72 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f 63 63  r.** failure occ
1244b 75 72 72 65 64 20 77 68 69 6c 65 20 74 68 65 20  urred while the 
1244c 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 65 69 6e  journal was bein
1244d 67 20 77 72 69 74 74 65 6e 2c 20 69 74 20 63 6f  g written, it co
1244e 75 6c 64 20 62 65 20 74 68 65 0a 2a 2a 20 63 61  uld be the.** ca
1244f 73 65 20 74 68 61 74 20 74 68 65 20 73 69 7a 65  se that the size
12450 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
12451 66 69 6c 65 20 68 61 64 20 61 6c 72 65 61 64 79  file had already
12452 20 62 65 65 6e 20 69 6e 63 72 65 61 73 65 64 20   been increased 
12453 62 75 74 0a 2a 2a 20 74 68 65 20 65 78 74 72 61  but.** the extra
12454 20 65 6e 74 72 69 65 73 20 68 61 64 20 6e 6f 74   entries had not
12455 20 79 65 74 20 6d 61 64 65 20 69 74 20 73 61 66   yet made it saf
12456 65 6c 79 20 74 6f 20 64 69 73 6b 2e 20 20 49 6e  ely to disk.  In
12457 20 73 75 63 68 20 61 20 63 61 73 65 2c 0a 2a 2a   such a case,.**
12458 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6e 52   the value of nR
12459 65 63 20 63 6f 6d 70 75 74 65 64 20 66 72 6f 6d  ec computed from
1245a 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 77   the file size w
1245b 6f 75 6c 64 20 62 65 20 74 6f 6f 20 6c 61 72 67  ould be too larg
1245c 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 61 74 20  e.  For.** that 
1245d 72 65 61 73 6f 6e 2c 20 77 65 20 61 6c 77 61 79  reason, we alway
1245e 73 20 75 73 65 20 74 68 65 20 6e 52 65 63 20 76  s use the nRec v
1245f 61 6c 75 65 20 69 6e 20 74 68 65 20 68 65 61 64  alue in the head
12460 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  er..**.** If the
12461 20 6e 52 65 63 20 76 61 6c 75 65 20 69 73 20 30   nRec value is 0
12462 78 66 66 66 66 66 66 66 66 20 69 74 20 6d 65 61  xffffffff it mea
12463 6e 73 20 74 68 61 74 20 6e 52 65 63 20 73 68 6f  ns that nRec sho
12464 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 64 0a  uld be computed.
12465 2a 2a 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65  ** from the file
12466 20 73 69 7a 65 2e 20 20 54 68 69 73 20 76 61 6c   size.  This val
12467 75 65 20 69 73 20 75 73 65 64 20 77 68 65 6e 20  ue is used when 
12468 74 68 65 20 75 73 65 72 20 73 65 6c 65 63 74 73  the user selects
12469 20 74 68 65 0a 2a 2a 20 6e 6f 2d 73 79 6e 63 20   the.** no-sync 
1246a 6f 70 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6a  option for the j
1246b 6f 75 72 6e 61 6c 2e 20 20 41 20 70 6f 77 65 72  ournal.  A power
1246c 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c   failure could l
1246d 65 61 64 20 74 6f 20 63 6f 72 72 75 70 74 69 6f  ead to corruptio
1246e 6e 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 73  n.** in this cas
1246f 65 2e 20 20 42 75 74 20 66 6f 72 20 74 68 69 6e  e.  But for thin
12470 67 73 20 6c 69 6b 65 20 74 65 6d 70 6f 72 61 72  gs like temporar
12471 79 20 74 61 62 6c 65 20 28 77 68 69 63 68 20 77  y table (which w
12472 69 6c 6c 20 62 65 0a 2a 2a 20 64 65 6c 65 74 65  ill be.** delete
12473 64 20 77 68 65 6e 20 74 68 65 20 70 6f 77 65 72  d when the power
12474 20 69 73 20 72 65 73 74 6f 72 65 64 29 20 77 65   is restored) we
12475 20 64 6f 6e 27 74 20 63 61 72 65 2e 20 20 0a 2a   don't care.  .*
12476 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6c 65  *.** If the file
12477 20 6f 70 65 6e 65 64 20 61 73 20 74 68 65 20 6a   opened as the j
12478 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e  ournal file is n
12479 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64  ot a well-formed
1247a 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  .** journal file
1247b 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 20   then all pages 
1247c 75 70 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  up to the first 
1247d 63 6f 72 72 75 70 74 65 64 20 70 61 67 65 20 61  corrupted page a
1247e 72 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63  re rolled.** bac
1247f 6b 20 28 6f 72 20 6e 6f 20 70 61 67 65 73 20 69  k (or no pages i
12480 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65  f the journal he
12481 61 64 65 72 20 69 73 20 63 6f 72 72 75 70 74 65  ader is corrupte
12482 64 29 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20  d). The journal 
12483 66 69 6c 65 0a 2a 2a 20 69 73 20 74 68 65 6e 20  file.** is then 
12484 64 65 6c 65 74 65 64 20 61 6e 64 20 53 51 4c 49  deleted and SQLI
12485 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2c 20  TE_OK returned, 
12486 6a 75 73 74 20 61 73 20 69 66 20 6e 6f 20 63 6f  just as if no co
12487 72 72 75 70 74 69 6f 6e 20 68 61 64 0a 2a 2a 20  rruption had.** 
12488 62 65 65 6e 20 65 6e 63 6f 75 6e 74 65 72 65 64  been encountered
12489 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 2f  ..**.** If an I/
1248a 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 65 72  O or malloc() er
1248b 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 20  ror occurs, the 
1248c 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 69 73 20  journal-file is 
1248d 6e 6f 74 20 64 65 6c 65 74 65 64 0a 2a 2a 20 61  not deleted.** a
1248e 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  nd an error code
1248f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
12490 0a 2a 2a 20 54 68 65 20 69 73 48 6f 74 20 70 61  .** The isHot pa
12491 72 61 6d 65 74 65 72 20 69 6e 64 69 63 61 74 65  rameter indicate
12492 73 20 74 68 61 74 20 77 65 20 61 72 65 20 74 72  s that we are tr
12493 79 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b  ying to rollback
12494 20 61 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 74 68   a journal.** th
12495 61 74 20 6d 69 67 68 74 20 62 65 20 61 20 68 6f  at might be a ho
12496 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f 72 2c 20  t journal.  Or, 
12497 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68 61 74  it could be that
12498 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20   the journal is 
12499 0a 2a 2a 20 70 72 65 73 65 72 76 65 64 20 62 65  .** preserved be
1249a 63 61 75 73 65 20 6f 66 20 4a 4f 55 52 4e 41 4c  cause of JOURNAL
1249b 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 6f 72 20  MODE_PERSIST or 
1249c 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e  JOURNALMODE_TRUN
1249d 43 41 54 45 2e 0a 2a 2a 20 49 66 20 74 68 65 20  CATE..** If the 
1249e 6a 6f 75 72 6e 61 6c 20 72 65 61 6c 6c 79 20 69  journal really i
1249f 73 20 68 6f 74 2c 20 72 65 73 65 74 20 74 68 65  s hot, reset the
124a0 20 70 61 67 65 72 20 63 61 63 68 65 20 70 72 69   pager cache pri
124a1 6f 72 20 72 6f 6c 6c 69 6e 67 0a 2a 2a 20 62 61  or rolling.** ba
124a2 63 6b 20 61 6e 79 20 63 6f 6e 74 65 6e 74 2e 20  ck any content. 
124a3 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   If the journal 
124a4 69 73 20 6d 65 72 65 6c 79 20 70 65 72 73 69 73  is merely persis
124a5 74 65 6e 74 2c 20 6e 6f 20 72 65 73 65 74 20 69  tent, no reset i
124a6 73 0a 2a 2a 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a  s.** needed..*/.
124a7 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72  static int pager
124a8 5f 70 6c 61 79 62 61 63 6b 28 50 61 67 65 72 20  _playback(Pager 
124a9 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 69 73 48  *pPager, int isH
124aa 6f 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76  ot){.  sqlite3_v
124ab 66 73 20 2a 70 56 66 73 20 3d 20 70 50 61 67 65  fs *pVfs = pPage
124ac 72 2d 3e 70 56 66 73 3b 0a 20 20 69 36 34 20 73  r->pVfs;.  i64 s
124ad 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  zJ;             
124ae 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74      /* Size of t
124af 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
124b0 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 33  in bytes */.  u3
124b1 32 20 6e 52 65 63 3b 20 20 20 20 20 20 20 20 20  2 nRec;         
124b2 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
124b3 20 6f 66 20 52 65 63 6f 72 64 73 20 69 6e 20 74   of Records in t
124b4 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20  he journal */.  
124b5 75 33 32 20 75 3b 20 20 20 20 20 20 20 20 20 20  u32 u;          
124b6 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69           /* Unsi
124b7 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65  gned loop counte
124b8 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 50 67  r */.  Pgno mxPg
124b9 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
124ba 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6f  /* Size of the o
124bb 72 69 67 69 6e 61 6c 20 66 69 6c 65 20 69 6e 20  riginal file in 
124bc 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72  pages */.  int r
124bd 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
124be 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f      /* Result co
124bf 64 65 20 6f 66 20 61 20 73 75 62 72 6f 75 74 69  de of a subrouti
124c0 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 20  ne */.  int res 
124c1 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 1;            
124c2 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e   /* Value return
124c3 65 64 20 62 79 20 73 71 6c 69 74 65 33 4f 73 41  ed by sqlite3OsA
124c4 63 63 65 73 73 28 29 20 2a 2f 0a 20 20 63 68 61  ccess() */.  cha
124c5 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20 30 3b 20  r *zMaster = 0; 
124c6 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
124c7 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
124c8 66 69 6c 65 20 69 66 20 61 6e 79 20 2a 2f 0a 20  file if any */. 
124c9 20 69 6e 74 20 6e 65 65 64 50 61 67 65 72 52 65   int needPagerRe
124ca 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 54 72 75  set;      /* Tru
124cb 65 20 74 6f 20 72 65 73 65 74 20 70 61 67 65 20  e to reset page 
124cc 70 72 69 6f 72 20 74 6f 20 66 69 72 73 74 20 70  prior to first p
124cd 61 67 65 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a  age rollback */.
124ce 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74  .  /* Figure out
124cf 20 68 6f 77 20 6d 61 6e 79 20 72 65 63 6f 72 64   how many record
124d0 73 20 61 72 65 20 69 6e 20 74 68 65 20 6a 6f 75  s are in the jou
124d1 72 6e 61 6c 2e 20 20 41 62 6f 72 74 20 65 61 72  rnal.  Abort ear
124d2 6c 79 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 6a  ly if.  ** the j
124d3 6f 75 72 6e 61 6c 20 69 73 20 65 6d 70 74 79 2e  ournal is empty.
124d4 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
124d5 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
124d6 66 64 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  fd) );.  rc = sq
124d7 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
124d8 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a  pPager->jfd, &sz
124d9 4a 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  J);.  if( rc!=SQ
124da 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 7a 4a 3d 3d  LITE_OK || szJ==
124db 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e  0 ){.    goto en
124dc 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 7d 0a  d_playback;.  }.
124dd 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6d  .  /* Read the m
124de 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61  aster journal na
124df 6d 65 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72  me from the jour
124e0 6e 61 6c 2c 20 69 66 20 69 74 20 69 73 20 70 72  nal, if it is pr
124e1 65 73 65 6e 74 2e 0a 20 20 2a 2a 20 49 66 20 61  esent..  ** If a
124e2 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
124e3 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 73 70 65  file name is spe
124e4 63 69 66 69 65 64 2c 20 62 75 74 20 74 68 65 20  cified, but the 
124e5 66 69 6c 65 20 69 73 20 6e 6f 74 0a 20 20 2a 2a  file is not.  **
124e6 20 70 72 65 73 65 6e 74 20 6f 6e 20 64 69 73 6b   present on disk
124e7 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e  , then the journ
124e8 61 6c 20 69 73 20 6e 6f 74 20 68 6f 74 20 61 6e  al is not hot an
124e9 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  d does not need 
124ea 74 6f 20 62 65 0a 20 20 2a 2a 20 70 6c 61 79 65  to be.  ** playe
124eb 64 20 62 61 63 6b 2e 0a 20 20 2a 2a 0a 20 20 2a  d back..  **.  *
124ec 2a 20 54 4f 44 4f 3a 20 54 65 63 68 6e 69 63 61  * TODO: Technica
124ed 6c 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  lly the followin
124ee 67 20 69 73 20 61 6e 20 65 72 72 6f 72 20 62 65  g is an error be
124ef 63 61 75 73 65 20 69 74 20 61 73 73 75 6d 65 73  cause it assumes
124f0 20 74 68 61 74 0a 20 20 2a 2a 20 62 75 66 66 65   that.  ** buffe
124f1 72 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63  r Pager.pTmpSpac
124f2 65 20 69 73 20 28 6d 78 50 61 74 68 6e 61 6d 65  e is (mxPathname
124f3 2b 31 29 20 62 79 74 65 73 20 6f 72 20 6c 61 72  +1) bytes or lar
124f4 67 65 72 2e 20 69 2e 65 2e 20 74 68 61 74 0a 20  ger. i.e. that. 
124f5 20 2a 2a 20 28 70 50 61 67 65 72 2d 3e 70 61 67   ** (pPager->pag
124f6 65 53 69 7a 65 20 3e 3d 20 70 50 61 67 65 72 2d  eSize >= pPager-
124f7 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d  >pVfs->mxPathnam
124f8 65 2b 31 29 2e 20 55 73 69 6e 67 20 6f 73 5f 75  e+1). Using os_u
124f9 6e 69 78 2e 63 2c 0a 20 20 2a 2a 20 20 6d 78 50  nix.c,.  **  mxP
124fa 61 74 68 6e 61 6d 65 20 69 73 20 35 31 32 2c 20  athname is 512, 
124fb 77 68 69 63 68 20 69 73 20 74 68 65 20 73 61 6d  which is the sam
124fc 65 20 61 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d  e as the minimum
124fd 20 61 6c 6c 6f 77 61 62 6c 65 20 76 61 6c 75 65   allowable value
124fe 0a 20 20 2a 2a 20 66 6f 72 20 70 61 67 65 53 69  .  ** for pageSi
124ff 7a 65 2e 0a 20 20 2a 2f 0a 20 20 7a 4d 61 73 74  ze..  */.  zMast
12500 65 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d  er = pPager->pTm
12501 70 53 70 61 63 65 3b 0a 20 20 72 63 20 3d 20 72  pSpace;.  rc = r
12502 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  eadMasterJournal
12503 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d  (pPager->jfd, zM
12504 61 73 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 70  aster, pPager->p
12505 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b  Vfs->mxPathname+
12506 31 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  1);.  if( rc==SQ
12507 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 4d 61 73 74  LITE_OK && zMast
12508 65 72 5b 30 5d 20 29 7b 0a 20 20 20 20 72 63 20  er[0] ){.    rc 
12509 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73  = sqlite3OsAcces
1250a 73 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c  s(pVfs, zMaster,
1250b 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45   SQLITE_ACCESS_E
1250c 58 49 53 54 53 2c 20 26 72 65 73 29 3b 0a 20 20  XISTS, &res);.  
1250d 7d 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b  }.  zMaster = 0;
1250e 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
1250f 45 5f 4f 4b 20 7c 7c 20 21 72 65 73 20 29 7b 0a  E_OK || !res ){.
12510 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61      goto end_pla
12511 79 62 61 63 6b 3b 0a 20 20 7d 0a 20 20 70 50 61  yback;.  }.  pPa
12512 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
12513 3d 20 30 3b 0a 20 20 6e 65 65 64 50 61 67 65 72  = 0;.  needPager
12514 52 65 73 65 74 20 3d 20 69 73 48 6f 74 3b 0a 0a  Reset = isHot;..
12515 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 74    /* This loop t
12516 65 72 6d 69 6e 61 74 65 73 20 65 69 74 68 65 72  erminates either
12517 20 77 68 65 6e 20 61 20 72 65 61 64 4a 6f 75 72   when a readJour
12518 6e 61 6c 48 64 72 28 29 20 6f 72 20 0a 20 20 2a  nalHdr() or .  *
12519 2a 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b  * pager_playback
1251a 5f 6f 6e 65 5f 70 61 67 65 28 29 20 63 61 6c 6c  _one_page() call
1251b 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
1251c 44 4f 4e 45 20 6f 72 20 61 6e 20 49 4f 20 65 72  DONE or an IO er
1251d 72 6f 72 20 0a 20 20 2a 2a 20 6f 63 63 75 72 73  ror .  ** occurs
1251e 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28  . .  */.  while(
1251f 20 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 73   1 ){.    int is
12520 55 6e 73 79 6e 63 20 3d 20 30 3b 0a 0a 20 20 20  Unsync = 0;..   
12521 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6e 65 78   /* Read the nex
12522 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  t journal header
12523 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61   from the journa
12524 6c 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72  l file.  If ther
12525 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e 6f 74  e are.    ** not
12526 20 65 6e 6f 75 67 68 20 62 79 74 65 73 20 6c 65   enough bytes le
12527 66 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61  ft in the journa
12528 6c 20 66 69 6c 65 20 66 6f 72 20 61 20 63 6f 6d  l file for a com
12529 70 6c 65 74 65 20 68 65 61 64 65 72 2c 20 6f 72  plete header, or
1252a 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 63 6f  .    ** it is co
1252b 72 72 75 70 74 65 64 2c 20 74 68 65 6e 20 61 20  rrupted, then a 
1252c 70 72 6f 63 65 73 73 20 6d 75 73 74 20 6f 66 20  process must of 
1252d 66 61 69 6c 65 64 20 77 68 69 6c 65 20 77 72 69  failed while wri
1252e 74 69 6e 67 20 69 74 2e 0a 20 20 20 20 2a 2a 20  ting it..    ** 
1252f 54 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 6e  This indicates n
12530 6f 74 68 69 6e 67 20 6d 6f 72 65 20 6e 65 65 64  othing more need
12531 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62  s to be rolled b
12532 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ack..    */.    
12533 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e 61 6c  rc = readJournal
12534 48 64 72 28 70 50 61 67 65 72 2c 20 69 73 48 6f  Hdr(pPager, isHo
12535 74 2c 20 73 7a 4a 2c 20 26 6e 52 65 63 2c 20 26  t, szJ, &nRec, &
12536 6d 78 50 67 29 3b 0a 20 20 20 20 69 66 28 20 72  mxPg);.    if( r
12537 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20  c!=SQLITE_OK ){ 
12538 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
12539 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20  QLITE_DONE ){.  
1253a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1253b 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  E_OK;.      }.  
1253c 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61      goto end_pla
1253d 79 62 61 63 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  yback;.    }..  
1253e 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 20    /* If nRec is 
1253f 30 78 66 66 66 66 66 66 66 66 2c 20 74 68 65 6e  0xffffffff, then
12540 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61   this journal wa
12541 73 20 63 72 65 61 74 65 64 20 62 79 20 61 20 70  s created by a p
12542 72 6f 63 65 73 73 0a 20 20 20 20 2a 2a 20 77 6f  rocess.    ** wo
12543 72 6b 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63  rking in no-sync
12544 20 6d 6f 64 65 2e 20 54 68 69 73 20 6d 65 61 6e   mode. This mean
12545 73 20 74 68 61 74 20 74 68 65 20 72 65 73 74 20  s that the rest 
12546 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20  of the journal. 
12547 20 20 20 2a 2a 20 66 69 6c 65 20 63 6f 6e 73 69     ** file consi
12548 73 74 73 20 6f 66 20 70 61 67 65 73 2c 20 74 68  sts of pages, th
12549 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20  ere are no more 
1254a 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 73 2e  journal headers.
1254b 20 43 6f 6d 70 75 74 65 0a 20 20 20 20 2a 2a 20   Compute.    ** 
1254c 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6e 52 65  the value of nRe
1254d 63 20 62 61 73 65 64 20 6f 6e 20 74 68 69 73 20  c based on this 
1254e 61 73 73 75 6d 70 74 69 6f 6e 2e 0a 20 20 20 20  assumption..    
1254f 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52 65 63 3d  */.    if( nRec=
12550 3d 30 78 66 66 66 66 66 66 66 66 20 29 7b 0a 20  =0xffffffff ){. 
12551 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
12552 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d  ger->journalOff=
12553 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28  =JOURNAL_HDR_SZ(
12554 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 20  pPager) );.     
12555 20 6e 52 65 63 20 3d 20 28 69 6e 74 29 28 28 73   nRec = (int)((s
12556 7a 4a 20 2d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52  zJ - JOURNAL_HDR
12557 5f 53 5a 28 70 50 61 67 65 72 29 29 2f 4a 4f 55  _SZ(pPager))/JOU
12558 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65  RNAL_PG_SZ(pPage
12559 72 29 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  r));.    }..    
1255a 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 20 30 20  /* If nRec is 0 
1255b 61 6e 64 20 74 68 69 73 20 72 6f 6c 6c 62 61 63  and this rollbac
1255c 6b 20 69 73 20 6f 66 20 61 20 74 72 61 6e 73 61  k is of a transa
1255d 63 74 69 6f 6e 20 63 72 65 61 74 65 64 20 62 79  ction created by
1255e 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 70 72 6f   this.    ** pro
1255f 63 65 73 73 20 61 6e 64 20 69 66 20 74 68 69 73  cess and if this
12560 20 69 73 20 74 68 65 20 66 69 6e 61 6c 20 68 65   is the final he
12561 61 64 65 72 20 69 6e 20 74 68 65 20 6a 6f 75 72  ader in the jour
12562 6e 61 6c 2c 20 74 68 65 6e 20 69 74 20 6d 65 61  nal, then it mea
12563 6e 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 74  ns.    ** that t
12564 68 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20  his part of the 
12565 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 65 69 6e  journal was bein
12566 67 20 66 69 6c 6c 65 64 20 62 75 74 20 68 61 73  g filled but has
12567 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 0a 20 20   not yet been.  
12568 20 20 2a 2a 20 73 79 6e 63 65 64 20 74 6f 20 64    ** synced to d
12569 69 73 6b 2e 20 20 43 6f 6d 70 75 74 65 20 74 68  isk.  Compute th
1256a 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
1256b 73 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 72  s based on the r
1256c 65 6d 61 69 6e 69 6e 67 0a 20 20 20 20 2a 2a 20  emaining.    ** 
1256d 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65  size of the file
1256e 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
1256f 54 68 65 20 74 68 69 72 64 20 74 65 72 6d 20 6f  The third term o
12570 66 20 74 68 65 20 74 65 73 74 20 77 61 73 20 61  f the test was a
12571 64 64 65 64 20 74 6f 20 66 69 78 20 74 69 63 6b  dded to fix tick
12572 65 74 20 23 32 35 36 35 2e 0a 20 20 20 20 2a 2a  et #2565..    **
12573 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 61   When rolling ba
12574 63 6b 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c  ck a hot journal
12575 2c 20 6e 52 65 63 3d 3d 30 20 61 6c 77 61 79 73  , nRec==0 always
12576 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
12577 6e 65 78 74 0a 20 20 20 20 2a 2a 20 63 68 75 6e  next.    ** chun
12578 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  k of the journal
12579 20 63 6f 6e 74 61 69 6e 73 20 7a 65 72 6f 20 70   contains zero p
1257a 61 67 65 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65  ages to be rolle
1257b 64 20 62 61 63 6b 2e 20 20 42 75 74 0a 20 20 20  d back.  But.   
1257c 20 2a 2a 20 77 68 65 6e 20 64 6f 69 6e 67 20 61   ** when doing a
1257d 20 52 4f 4c 4c 42 41 43 4b 20 61 6e 64 20 74 68   ROLLBACK and th
1257e 65 20 6e 52 65 63 3d 3d 30 20 63 68 75 6e 6b 20  e nRec==0 chunk 
1257f 69 73 20 74 68 65 20 6c 61 73 74 20 63 68 75 6e  is the last chun
12580 6b 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20  k in.    ** the 
12581 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 6d 65 61 6e  journal, it mean
12582 73 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e  s that the journ
12583 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e  al might contain
12584 20 61 64 64 69 74 69 6f 6e 61 6c 0a 20 20 20 20   additional.    
12585 2a 2a 20 70 61 67 65 73 20 74 68 61 74 20 6e 65  ** pages that ne
12586 65 64 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20  ed to be rolled 
12587 62 61 63 6b 20 61 6e 64 20 74 68 61 74 20 74 68  back and that th
12588 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
12589 73 20 0a 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64  s .    ** should
1258a 20 62 65 20 63 6f 6d 70 75 74 65 64 20 62 61 73   be computed bas
1258b 65 64 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61  ed on the journa
1258c 6c 20 66 69 6c 65 20 73 69 7a 65 2e 0a 20 20 20  l file size..   
1258d 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52 65 63   */.    if( nRec
1258e 3d 3d 30 20 26 26 20 21 69 73 48 6f 74 20 26 26  ==0 && !isHot &&
1258f 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d  .        pPager-
12590 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52  >journalHdr+JOUR
12591 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
12592 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72  r)==pPager->jour
12593 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20 20 20 20  nalOff ){.      
12594 6e 52 65 63 20 3d 20 28 69 6e 74 29 28 28 73 7a  nRec = (int)((sz
12595 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  J - pPager->jour
12596 6e 61 6c 4f 66 66 29 20 2f 20 4a 4f 55 52 4e 41  nalOff) / JOURNA
12597 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 29  L_PG_SZ(pPager))
12598 3b 0a 20 20 20 20 20 20 69 73 55 6e 73 79 6e 63  ;.      isUnsync
12599 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20   = 1;.    }..   
1259a 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 74   /* If this is t
1259b 68 65 20 66 69 72 73 74 20 68 65 61 64 65 72 20  he first header 
1259c 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f  read from the jo
1259d 75 72 6e 61 6c 2c 20 74 72 75 6e 63 61 74 65 20  urnal, truncate 
1259e 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62  the.    ** datab
1259f 61 73 65 20 66 69 6c 65 20 62 61 63 6b 20 74 6f  ase file back to
125a0 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 69   its original si
125a1 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ze..    */.    i
125a2 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  f( pPager->journ
125a3 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48  alOff==JOURNAL_H
125a4 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29 7b  DR_SZ(pPager) ){
125a5 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
125a6 72 5f 74 72 75 6e 63 61 74 65 28 70 50 61 67 65  r_truncate(pPage
125a7 72 2c 20 6d 78 50 67 29 3b 0a 20 20 20 20 20 20  r, mxPg);.      
125a8 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
125a9 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74  K ){.        got
125aa 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a  o end_playback;.
125ab 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50        }.      pP
125ac 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6d  ager->dbSize = m
125ad 78 50 67 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  xPg;.    }..    
125ae 2f 2a 20 43 6f 70 79 20 6f 72 69 67 69 6e 61 6c  /* Copy original
125af 20 70 61 67 65 73 20 6f 75 74 20 6f 66 20 74 68   pages out of th
125b0 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 62 61  e journal and ba
125b1 63 6b 20 69 6e 74 6f 20 74 68 65 20 0a 20 20 20  ck into the .   
125b2 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c   ** database fil
125b3 65 20 61 6e 64 2f 6f 72 20 70 61 67 65 20 63 61  e and/or page ca
125b4 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  che..    */.    
125b5 66 6f 72 28 75 3d 30 3b 20 75 3c 6e 52 65 63 3b  for(u=0; u<nRec;
125b6 20 75 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   u++){.      if(
125b7 20 6e 65 65 64 50 61 67 65 72 52 65 73 65 74 20   needPagerReset 
125b8 29 7b 0a 20 20 20 20 20 20 20 20 70 61 67 65 72  ){.        pager
125b9 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a  _reset(pPager);.
125ba 20 20 20 20 20 20 20 20 6e 65 65 64 50 61 67 65          needPage
125bb 72 52 65 73 65 74 20 3d 20 30 3b 0a 20 20 20 20  rReset = 0;.    
125bc 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 70    }.      rc = p
125bd 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e  ager_playback_on
125be 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c 31 2c  e_page(pPager,1,
125bf 69 73 55 6e 73 79 6e 63 2c 26 70 50 61 67 65 72  isUnsync,&pPager
125c0 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 30 2c 30  ->journalOff,0,0
125c1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
125c2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
125c3 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
125c4 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20  LITE_DONE ){.   
125c5 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
125c6 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20  TE_OK;.         
125c7 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
125c8 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 20 20 20  Off = szJ;.     
125c9 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
125ca 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
125cb 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61 72       /* If we ar
125cc 65 20 75 6e 61 62 6c 65 20 74 6f 20 72 6f 6c 6c  e unable to roll
125cd 62 61 63 6b 2c 20 71 75 69 74 20 61 6e 64 20 72  back, quit and r
125ce 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 0a  eturn the error.
125cf 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 64            ** cod
125d0 65 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 63 61  e.  This will ca
125d1 75 73 65 20 74 68 65 20 70 61 67 65 72 20 74 6f  use the pager to
125d2 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72   enter the error
125d3 20 73 74 61 74 65 0a 20 20 20 20 20 20 20 20 20   state.         
125d4 20 2a 2a 20 73 6f 20 74 68 61 74 20 6e 6f 20 66   ** so that no f
125d5 75 72 74 68 65 72 20 68 61 72 6d 20 77 69 6c 6c  urther harm will
125d6 20 62 65 20 64 6f 6e 65 2e 20 20 50 65 72 68 61   be done.  Perha
125d7 70 73 20 74 68 65 20 6e 65 78 74 0a 20 20 20 20  ps the next.    
125d8 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73        ** process
125d9 20 74 6f 20 63 6f 6d 65 20 61 6c 6f 6e 67 20 77   to come along w
125da 69 6c 6c 20 62 65 20 61 62 6c 65 20 74 6f 20 72  ill be able to r
125db 6f 6c 6c 62 61 63 6b 20 74 68 65 20 64 61 74 61  ollback the data
125dc 62 61 73 65 2e 0a 20 20 20 20 20 20 20 20 20 20  base..          
125dd 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  */.          got
125de 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a  o end_playback;.
125df 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
125e0 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a  }.    }.  }.  /*
125e1 4e 4f 54 52 45 41 43 48 45 44 2a 2f 0a 20 20 61  NOTREACHED*/.  a
125e2 73 73 65 72 74 28 20 30 20 29 3b 0a 0a 65 6e 64  ssert( 0 );..end
125e3 5f 70 6c 61 79 62 61 63 6b 3a 0a 20 20 2f 2a 20  _playback:.  /* 
125e4 46 6f 6c 6c 6f 77 69 6e 67 20 61 20 72 6f 6c 6c  Following a roll
125e5 62 61 63 6b 2c 20 74 68 65 20 64 61 74 61 62 61  back, the databa
125e6 73 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62  se file should b
125e7 65 20 62 61 63 6b 20 69 6e 20 69 74 73 20 6f 72  e back in its or
125e8 69 67 69 6e 61 6c 0a 20 20 2a 2a 20 73 74 61 74  iginal.  ** stat
125e9 65 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 73  e prior to the s
125ea 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e  tart of the tran
125eb 73 61 63 74 69 6f 6e 2c 20 73 6f 20 69 6e 76 6f  saction, so invo
125ec 6b 65 20 74 68 65 0a 20 20 2a 2a 20 53 51 4c 49  ke the.  ** SQLI
125ed 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48  TE_FCNTL_DB_UNCH
125ee 41 4e 47 45 44 20 66 69 6c 65 2d 63 6f 6e 74 72  ANGED file-contr
125ef 6f 6c 20 6d 65 74 68 6f 64 20 74 6f 20 64 69 73  ol method to dis
125f0 61 62 6c 65 20 74 68 65 0a 20 20 2a 2a 20 61 73  able the.  ** as
125f1 73 65 72 74 69 6f 6e 20 74 68 61 74 20 74 68 65  sertion that the
125f2 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75   transaction cou
125f3 6e 74 65 72 20 77 61 73 20 6d 6f 64 69 66 69 65  nter was modifie
125f4 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  d..  */.  assert
125f5 28 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 66 64  (.    pPager->fd
125f6 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 30 20 7c 7c  ->pMethods==0 ||
125f7 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 69  .    sqlite3OsFi
125f8 6c 65 43 6f 6e 74 72 6f 6c 28 70 50 61 67 65 72  leControl(pPager
125f9 2d 3e 66 64 2c 53 51 4c 49 54 45 5f 46 43 4e 54  ->fd,SQLITE_FCNT
125fa 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 2c 30  L_DB_UNCHANGED,0
125fb 29 3e 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 29  )>=SQLITE_OK.  )
125fc 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20  ;..  /* If this 
125fd 70 6c 61 79 62 61 63 6b 20 69 73 20 68 61 70 70  playback is happ
125fe 65 6e 69 6e 67 20 61 75 74 6f 6d 61 74 69 63 61  ening automatica
125ff 6c 6c 79 20 61 73 20 61 20 72 65 73 75 6c 74 20  lly as a result 
12600 6f 66 20 61 6e 20 49 4f 20 6f 72 20 0a 20 20 2a  of an IO or .  *
12601 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 20 74  * malloc error t
12602 68 61 74 20 6f 63 63 75 72 72 65 64 20 61 66 74  hat occurred aft
12603 65 72 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f  er the change-co
12604 75 6e 74 65 72 20 77 61 73 20 75 70 64 61 74 65  unter was update
12605 64 20 62 75 74 20 0a 20 20 2a 2a 20 62 65 66 6f  d but .  ** befo
12606 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  re the transacti
12607 6f 6e 20 77 61 73 20 63 6f 6d 6d 69 74 74 65 64  on was committed
12608 2c 20 74 68 65 6e 20 74 68 65 20 63 68 61 6e 67  , then the chang
12609 65 2d 63 6f 75 6e 74 65 72 20 0a 20 20 2a 2a 20  e-counter .  ** 
1260a 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 6d 61 79  modification may
1260b 20 6a 75 73 74 20 68 61 76 65 20 62 65 65 6e 20   just have been 
1260c 72 65 76 65 72 74 65 64 2e 20 49 66 20 74 68 69  reverted. If thi
1260d 73 20 68 61 70 70 65 6e 73 20 69 6e 20 65 78 63  s happens in exc
1260e 6c 75 73 69 76 65 20 0a 20 20 2a 2a 20 6d 6f 64  lusive .  ** mod
1260f 65 2c 20 74 68 65 6e 20 73 75 62 73 65 71 75 65  e, then subseque
12610 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  nt transactions 
12611 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 68 65  performed by the
12612 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 6c 6c   connection will
12613 20 6e 6f 74 0a 20 20 2a 2a 20 75 70 64 61 74 65   not.  ** update
12614 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e   the change-coun
12615 74 65 72 20 61 74 20 61 6c 6c 2e 20 54 68 69 73  ter at all. This
12616 20 6d 61 79 20 6c 65 61 64 20 74 6f 20 63 61 63   may lead to cac
12617 68 65 20 69 6e 63 6f 6e 73 69 73 74 65 6e 63 79  he inconsistency
12618 0a 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 66  .  ** problems f
12619 6f 72 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  or other process
1261a 65 73 20 61 74 20 73 6f 6d 65 20 70 6f 69 6e 74  es at some point
1261b 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 2e 20   in the future. 
1261c 53 6f 2c 20 6a 75 73 74 0a 20 20 2a 2a 20 69 6e  So, just.  ** in
1261d 20 63 61 73 65 20 74 68 69 73 20 68 61 73 20 68   case this has h
1261e 61 70 70 65 6e 65 64 2c 20 63 6c 65 61 72 20 74  appened, clear t
1261f 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f  he changeCountDo
12620 6e 65 20 66 6c 61 67 20 6e 6f 77 2e 0a 20 20 2a  ne flag now..  *
12621 2f 0a 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e  /.  pPager->chan
12622 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 70 50  geCountDone = pP
12623 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a  ager->tempFile;.
12624 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
12625 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 7a 4d 61 73  E_OK ){.    zMas
12626 74 65 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 54  ter = pPager->pT
12627 6d 70 53 70 61 63 65 3b 0a 20 20 20 20 72 63 20  mpSpace;.    rc 
12628 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72  = readMasterJour
12629 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  nal(pPager->jfd,
1262a 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 72   zMaster, pPager
1262b 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61  ->pVfs->mxPathna
1262c 6d 65 2b 31 29 3b 0a 20 20 20 20 74 65 73 74 63  me+1);.    testc
1262d 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ase( rc!=SQLITE_
1262e 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  OK );.  }.  if( 
1262f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
12630 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f  .    rc = pager_
12631 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28  end_transaction(
12632 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 5b  pPager, zMaster[
12633 30 5d 21 3d 27 5c 30 27 29 3b 0a 20 20 20 20 74  0]!='\0');.    t
12634 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c  estcase( rc!=SQL
12635 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20  ITE_OK );.  }.  
12636 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
12637 4b 20 26 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20  K && zMaster[0] 
12638 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 2f 2a  && res ){.    /*
12639 20 49 66 20 74 68 65 72 65 20 77 61 73 20 61 20   If there was a 
1263a 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 61  master journal a
1263b 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  nd this routine 
1263c 77 69 6c 6c 20 72 65 74 75 72 6e 20 73 75 63 63  will return succ
1263d 65 73 73 2c 0a 20 20 20 20 2a 2a 20 73 65 65 20  ess,.    ** see 
1263e 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c  if it is possibl
1263f 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20  e to delete the 
12640 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a  master journal..
12641 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20      */.    rc = 
12642 70 61 67 65 72 5f 64 65 6c 6d 61 73 74 65 72 28  pager_delmaster(
12643 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 29  pPager, zMaster)
12644 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
12645 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc!=SQLITE_OK );
12646 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 50  .  }..  /* The P
12647 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20  ager.sectorSize 
12648 76 61 72 69 61 62 6c 65 20 6d 61 79 20 68 61 76  variable may hav
12649 65 20 62 65 65 6e 20 75 70 64 61 74 65 64 20 77  e been updated w
1264a 68 69 6c 65 20 72 6f 6c 6c 69 6e 67 0a 20 20 2a  hile rolling.  *
1264b 2a 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c  * back a journal
1264c 20 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72   created by a pr
1264d 6f 63 65 73 73 20 77 69 74 68 20 61 20 64 69 66  ocess with a dif
1264e 66 65 72 65 6e 74 20 73 65 63 74 6f 72 20 73 69  ferent sector si
1264f 7a 65 0a 20 20 2a 2a 20 76 61 6c 75 65 2e 20 52  ze.  ** value. R
12650 65 73 65 74 20 69 74 20 74 6f 20 74 68 65 20 63  eset it to the c
12651 6f 72 72 65 63 74 20 76 61 6c 75 65 20 66 6f 72  orrect value for
12652 20 74 68 69 73 20 70 72 6f 63 65 73 73 2e 0a 20   this process.. 
12653 20 2a 2f 0a 20 20 73 65 74 53 65 63 74 6f 72 53   */.  setSectorS
12654 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 72  ize(pPager);.  r
12655 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
12656 2a 2a 20 50 6c 61 79 62 61 63 6b 20 73 61 76 65  ** Playback save
12657 70 6f 69 6e 74 20 70 53 61 76 65 70 6f 69 6e 74  point pSavepoint
12658 2e 20 4f 72 2c 20 69 66 20 70 53 61 76 65 70 6f  . Or, if pSavepo
12659 69 6e 74 3d 3d 4e 55 4c 4c 2c 20 74 68 65 6e 20  int==NULL, then 
1265a 70 6c 61 79 62 61 63 6b 0a 2a 2a 20 74 68 65 20  playback.** the 
1265b 65 6e 74 69 72 65 20 6d 61 73 74 65 72 20 6a 6f  entire master jo
1265c 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 20  urnal file. The 
1265d 63 61 73 65 20 70 53 61 76 65 70 6f 69 6e 74 3d  case pSavepoint=
1265e 3d 4e 55 4c 4c 20 6f 63 63 75 72 73 20 77 68 65  =NULL occurs whe
1265f 6e 20 0a 2a 2a 20 61 20 52 4f 4c 4c 42 41 43 4b  n .** a ROLLBACK
12660 20 54 4f 20 63 6f 6d 6d 61 6e 64 20 69 73 20 69   TO command is i
12661 6e 76 6f 6b 65 64 20 6f 6e 20 61 20 53 41 56 45  nvoked on a SAVE
12662 50 4f 49 4e 54 20 74 68 61 74 20 69 73 20 61 20  POINT that is a 
12663 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20  transaction .** 
12664 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a  savepoint..**.**
12665 20 57 68 65 6e 20 70 53 61 76 65 70 6f 69 6e 74   When pSavepoint
12666 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 28 6d 65   is not NULL (me
12667 61 6e 69 6e 67 20 61 20 6e 6f 6e 2d 74 72 61 6e  aning a non-tran
12668 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e  saction savepoin
12669 74 20 69 73 20 0a 2a 2a 20 62 65 69 6e 67 20 72  t is .** being r
1266a 6f 6c 6c 65 64 20 62 61 63 6b 29 2c 20 74 68 65  olled back), the
1266b 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63  n the rollback c
1266c 6f 6e 73 69 73 74 73 20 6f 66 20 75 70 20 74 6f  onsists of up to
1266d 20 74 68 72 65 65 20 73 74 61 67 65 73 2c 0a 2a   three stages,.*
1266e 2a 20 70 65 72 66 6f 72 6d 65 64 20 69 6e 20 74  * performed in t
1266f 68 65 20 6f 72 64 65 72 20 73 70 65 63 69 66 69  he order specifi
12670 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61  ed:.**.**   * Pa
12671 67 65 73 20 61 72 65 20 70 6c 61 79 65 64 20 62  ges are played b
12672 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 6d 61 69  ack from the mai
12673 6e 20 6a 6f 75 72 6e 61 6c 20 73 74 61 72 74 69  n journal starti
12674 6e 67 20 61 74 20 62 79 74 65 0a 2a 2a 20 20 20  ng at byte.**   
12675 20 20 6f 66 66 73 65 74 20 50 61 67 65 72 53 61    offset PagerSa
12676 76 65 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20  vepoint.iOffset 
12677 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74  and continuing t
12678 6f 20 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 53  o .**     PagerS
12679 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66  avepoint.iHdrOff
1267a 73 65 74 2c 20 6f 72 20 74 6f 20 74 68 65 20 65  set, or to the e
1267b 6e 64 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a  nd of the main j
1267c 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 66 69  ournal.**     fi
1267d 6c 65 20 69 66 20 50 61 67 65 72 53 61 76 65 70  le if PagerSavep
1267e 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20  oint.iHdrOffset 
1267f 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20  is zero..**.**  
12680 20 2a 20 49 66 20 50 61 67 65 72 53 61 76 65 70   * If PagerSavep
12681 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20  oint.iHdrOffset 
12682 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65  is not zero, the
12683 6e 20 70 61 67 65 73 20 61 72 65 20 70 6c 61 79  n pages are play
12684 65 64 0a 2a 2a 20 20 20 20 20 62 61 63 6b 20 73  ed.**     back s
12685 74 61 72 74 69 6e 67 20 66 72 6f 6d 20 74 68 65  tarting from the
12686 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
12687 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c  immediately foll
12688 6f 77 69 6e 67 20 0a 2a 2a 20 20 20 20 20 50 61  owing .**     Pa
12689 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64  gerSavepoint.iHd
1268a 72 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 65  rOffset to the e
1268b 6e 64 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a  nd of the main j
1268c 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a  ournal file..**.
1268d 2a 2a 20 20 20 2a 20 50 61 67 65 73 20 61 72 65  **   * Pages are
1268e 20 74 68 65 6e 20 70 6c 61 79 65 64 20 62 61 63   then played bac
1268f 6b 20 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a  k from the sub-j
12690 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 73 74 61  ournal file, sta
12691 72 74 69 6e 67 0a 2a 2a 20 20 20 20 20 77 69 74  rting.**     wit
12692 68 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70  h the PagerSavep
12693 6f 69 6e 74 2e 69 53 75 62 52 65 63 20 61 6e 64  oint.iSubRec and
12694 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74   continuing to t
12695 68 65 20 65 6e 64 20 6f 66 0a 2a 2a 20 20 20 20  he end of.**    
12696 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
12697 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 72 6f 75 67 68  e..**.** Through
12698 6f 75 74 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b  out the rollback
12699 20 70 72 6f 63 65 73 73 2c 20 65 61 63 68 20 74   process, each t
1269a 69 6d 65 20 61 20 70 61 67 65 20 69 73 20 72 6f  ime a page is ro
1269b 6c 6c 65 64 20 62 61 63 6b 2c 20 74 68 65 0a 2a  lled back, the.*
1269c 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  * corresponding 
1269d 62 69 74 20 69 73 20 73 65 74 20 69 6e 20 61 20  bit is set in a 
1269e 62 69 74 76 65 63 20 73 74 72 75 63 74 75 72 65  bitvec structure
1269f 20 28 76 61 72 69 61 62 6c 65 20 70 44 6f 6e 65   (variable pDone
126a0 20 69 6e 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65   in the.** imple
126a1 6d 65 6e 74 61 74 69 6f 6e 20 62 65 6c 6f 77 29  mentation below)
126a2 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74  . This is used t
126a3 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 61 20  o ensure that a 
126a4 70 61 67 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20  page is only.** 
126a5 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 65 20  rolled back the 
126a6 66 69 72 73 74 20 74 69 6d 65 20 69 74 20 69 73  first time it is
126a7 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 69 6e 20   encountered in 
126a8 65 69 74 68 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a  either journal..
126a9 2a 2a 0a 2a 2a 20 49 66 20 70 53 61 76 65 70 6f  **.** If pSavepo
126aa 69 6e 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  int is NULL, the
126ab 6e 20 70 61 67 65 73 20 61 72 65 20 6f 6e 6c 79  n pages are only
126ac 20 70 6c 61 79 65 64 20 62 61 63 6b 20 66 72 6f   played back fro
126ad 6d 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f  m the main.** jo
126ae 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 72  urnal file. Ther
126af 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72  e is no need for
126b0 20 61 20 62 69 74 76 65 63 20 69 6e 20 74 68 69   a bitvec in thi
126b1 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e  s case..**.** In
126b2 20 65 69 74 68 65 72 20 63 61 73 65 2c 20 62 65   either case, be
126b3 66 6f 72 65 20 70 6c 61 79 62 61 63 6b 20 63 6f  fore playback co
126b4 6d 6d 65 6e 63 65 73 20 74 68 65 20 50 61 67 65  mmences the Page
126b5 72 2e 64 62 53 69 7a 65 20 76 61 72 69 61 62 6c  r.dbSize variabl
126b6 65 0a 2a 2a 20 69 73 20 72 65 73 65 74 20 74 6f  e.** is reset to
126b7 20 74 68 65 20 76 61 6c 75 65 20 74 68 61 74 20   the value that 
126b8 69 74 20 68 65 6c 64 20 61 74 20 74 68 65 20 73  it held at the s
126b9 74 61 72 74 20 6f 66 20 74 68 65 20 73 61 76 65  tart of the save
126ba 70 6f 69 6e 74 20 0a 2a 2a 20 28 6f 72 20 74 72  point .** (or tr
126bb 61 6e 73 61 63 74 69 6f 6e 29 2e 20 4e 6f 20 70  ansaction). No p
126bc 61 67 65 20 77 69 74 68 20 61 20 70 61 67 65 2d  age with a page-
126bd 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72 20 74  number greater t
126be 68 61 6e 20 74 68 69 73 20 76 61 6c 75 65 0a 2a  han this value.*
126bf 2a 20 69 73 20 70 6c 61 79 65 64 20 62 61 63 6b  * is played back
126c0 2e 20 49 66 20 6f 6e 65 20 69 73 20 65 6e 63 6f  . If one is enco
126c1 75 6e 74 65 72 65 64 20 69 74 20 69 73 20 73 69  untered it is si
126c2 6d 70 6c 79 20 73 6b 69 70 70 65 64 2e 0a 2a 2f  mply skipped..*/
126c3 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65  .static int page
126c4 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69  rPlaybackSavepoi
126c5 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  nt(Pager *pPager
126c6 2c 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74  , PagerSavepoint
126c7 20 2a 70 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20   *pSavepoint){. 
126c8 20 69 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 20   i64 szJ;       
126c9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 66 66            /* Eff
126ca 65 63 74 69 76 65 20 73 69 7a 65 20 6f 66 20 74  ective size of t
126cb 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  he main journal 
126cc 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66  */.  i64 iHdrOff
126cd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
126ce 20 45 6e 64 20 6f 66 20 66 69 72 73 74 20 73 65   End of first se
126cf 67 6d 65 6e 74 20 6f 66 20 6d 61 69 6e 2d 6a 6f  gment of main-jo
126d0 75 72 6e 61 6c 20 72 65 63 6f 72 64 73 20 2a 2f  urnal records */
126d1 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
126d2 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 52  TE_OK;      /* R
126d3 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
126d4 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 20 3d 20  Bitvec *pDone = 
126d5 30 3b 20 20 20 20 20 20 20 2f 2a 20 42 69 74 76  0;       /* Bitv
126d6 65 63 20 74 6f 20 65 6e 73 75 72 65 20 70 61 67  ec to ensure pag
126d7 65 73 20 70 6c 61 79 65 64 20 62 61 63 6b 20 6f  es played back o
126d8 6e 6c 79 20 6f 6e 63 65 20 2a 2f 0a 0a 20 20 61  nly once */..  a
126d9 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
126da 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52  tate>=PAGER_SHAR
126db 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  ED );..  /* Allo
126dc 63 61 74 65 20 61 20 62 69 74 76 65 63 20 74 6f  cate a bitvec to
126dd 20 75 73 65 20 74 6f 20 73 74 6f 72 65 20 74 68   use to store th
126de 65 20 73 65 74 20 6f 66 20 70 61 67 65 73 20 72  e set of pages r
126df 6f 6c 6c 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20  olled back */.  
126e0 69 66 28 20 70 53 61 76 65 70 6f 69 6e 74 20 29  if( pSavepoint )
126e1 7b 0a 20 20 20 20 70 44 6f 6e 65 20 3d 20 73 71  {.    pDone = sq
126e2 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74  lite3BitvecCreat
126e3 65 28 70 53 61 76 65 70 6f 69 6e 74 2d 3e 6e 4f  e(pSavepoint->nO
126e4 72 69 67 29 3b 0a 20 20 20 20 69 66 28 20 21 70  rig);.    if( !p
126e5 44 6f 6e 65 20 29 7b 0a 20 20 20 20 20 20 72 65  Done ){.      re
126e6 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
126e7 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  M;.    }.  }..  
126e8 2f 2a 20 53 65 74 20 74 68 65 20 64 61 74 61 62  /* Set the datab
126e9 61 73 65 20 73 69 7a 65 20 62 61 63 6b 20 74 6f  ase size back to
126ea 20 74 68 65 20 76 61 6c 75 65 20 69 74 20 77 61   the value it wa
126eb 73 20 62 65 66 6f 72 65 20 74 68 65 20 73 61 76  s before the sav
126ec 65 70 6f 69 6e 74 20 0a 20 20 2a 2a 20 62 65 69  epoint .  ** bei
126ed 6e 67 20 72 65 76 65 72 74 65 64 20 77 61 73 20  ng reverted was 
126ee 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 70  opened..  */.  p
126ef 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20  Pager->dbSize = 
126f0 70 53 61 76 65 70 6f 69 6e 74 20 3f 20 70 53 61  pSavepoint ? pSa
126f1 76 65 70 6f 69 6e 74 2d 3e 6e 4f 72 69 67 20 3a  vepoint->nOrig :
126f2 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53   pPager->dbOrigS
126f3 69 7a 65 3b 0a 0a 20 20 2f 2a 20 55 73 65 20 70  ize;..  /* Use p
126f4 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
126f5 66 20 61 73 20 74 68 65 20 65 66 66 65 63 74 69  f as the effecti
126f6 76 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6d  ve size of the m
126f7 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a  ain rollback.  *
126f8 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20  * journal.  The 
126f9 61 63 74 75 61 6c 20 66 69 6c 65 20 6d 69 67 68  actual file migh
126fa 74 20 62 65 20 6c 61 72 67 65 72 20 74 68 61 6e  t be larger than
126fb 20 74 68 69 73 20 69 6e 0a 20 20 2a 2a 20 50 41   this in.  ** PA
126fc 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
126fd 54 52 55 4e 43 41 54 45 20 6f 72 20 50 41 47 45  TRUNCATE or PAGE
126fe 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45  R_JOURNALMODE_PE
126ff 52 53 49 53 54 2e 20 20 42 75 74 20 61 6e 79 74  RSIST.  But anyt
12700 68 69 6e 67 0a 20 20 2a 2a 20 70 61 73 74 20 70  hing.  ** past p
12701 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
12702 66 20 69 73 20 6f 66 66 2d 6c 69 6d 69 74 73 20  f is off-limits 
12703 74 6f 20 75 73 2e 0a 20 20 2a 2f 0a 20 20 73 7a  to us..  */.  sz
12704 4a 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  J = pPager->jour
12705 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 42 65  nalOff;..  /* Be
12706 67 69 6e 20 62 79 20 72 6f 6c 6c 69 6e 67 20 62  gin by rolling b
12707 61 63 6b 20 72 65 63 6f 72 64 73 20 66 72 6f 6d  ack records from
12708 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61   the main journa
12709 6c 20 73 74 61 72 74 69 6e 67 20 61 74 0a 20 20  l starting at.  
1270a 2a 2a 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e  ** PagerSavepoin
1270b 74 2e 69 4f 66 66 73 65 74 20 61 6e 64 20 63 6f  t.iOffset and co
1270c 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 68 65 20  ntinuing to the 
1270d 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61  next journal hea
1270e 64 65 72 2e 0a 20 20 2a 2a 20 54 68 65 72 65 20  der..  ** There 
1270f 6d 69 67 68 74 20 62 65 20 72 65 63 6f 72 64 73  might be records
12710 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75   in the main jou
12711 72 6e 61 6c 20 74 68 61 74 20 68 61 76 65 20 61  rnal that have a
12712 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a   page number.  *
12713 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74  * greater than t
12714 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 62  he current datab
12715 61 73 65 20 73 69 7a 65 20 28 70 50 61 67 65 72  ase size (pPager
12716 2d 3e 64 62 53 69 7a 65 29 20 62 75 74 20 74 68  ->dbSize) but th
12717 6f 73 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65  ose.  ** will be
12718 20 73 6b 69 70 70 65 64 20 61 75 74 6f 6d 61 74   skipped automat
12719 69 63 61 6c 6c 79 2e 20 20 50 61 67 65 73 20 61  ically.  Pages a
1271a 72 65 20 61 64 64 65 64 20 74 6f 20 70 44 6f 6e  re added to pDon
1271b 65 20 61 73 20 74 68 65 79 0a 20 20 2a 2a 20 61  e as they.  ** a
1271c 72 65 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a  re played back..
1271d 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 65    */.  if( pSave
1271e 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 69 48 64  point ){.    iHd
1271f 72 4f 66 66 20 3d 20 70 53 61 76 65 70 6f 69 6e  rOff = pSavepoin
12720 74 2d 3e 69 48 64 72 4f 66 66 73 65 74 20 3f 20  t->iHdrOffset ? 
12721 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 48 64 72  pSavepoint->iHdr
12722 4f 66 66 73 65 74 20 3a 20 73 7a 4a 3b 0a 20 20  Offset : szJ;.  
12723 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
12724 6c 4f 66 66 20 3d 20 70 53 61 76 65 70 6f 69 6e  lOff = pSavepoin
12725 74 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20  t->iOffset;.    
12726 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54  while( rc==SQLIT
12727 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e  E_OK && pPager->
12728 6a 6f 75 72 6e 61 6c 4f 66 66 3c 69 48 64 72 4f  journalOff<iHdrO
12729 66 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  ff ){.      rc =
1272a 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f   pager_playback_
1272b 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c  one_page(pPager,
1272c 20 31 2c 20 30 2c 20 26 70 50 61 67 65 72 2d 3e   1, 0, &pPager->
1272d 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 70  journalOff, 1, p
1272e 44 6f 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Done);.    }.   
1272f 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
12730 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 65  ITE_DONE );.  }e
12731 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 65 72 2d  lse{.    pPager-
12732 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b  >journalOff = 0;
12733 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 74 69  .  }..  /* Conti
12734 6e 75 65 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b  nue rolling back
12735 20 72 65 63 6f 72 64 73 20 6f 75 74 20 6f 66 20   records out of 
12736 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c  the main journal
12737 20 73 74 61 72 74 69 6e 67 20 61 74 0a 20 20 2a   starting at.  *
12738 2a 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75 72  * the first jour
12739 6e 61 6c 20 68 65 61 64 65 72 20 73 65 65 6e 20  nal header seen 
1273a 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 75  and continuing u
1273b 6e 74 69 6c 20 74 68 65 20 65 66 66 65 63 74 69  ntil the effecti
1273c 76 65 20 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 74  ve end.  ** of t
1273d 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  he main journal 
1273e 66 69 6c 65 2e 20 20 43 6f 6e 74 69 6e 75 65 20  file.  Continue 
1273f 74 6f 20 73 6b 69 70 20 6f 75 74 2d 6f 66 2d 72  to skip out-of-r
12740 61 6e 67 65 20 70 61 67 65 73 20 61 6e 64 0a 20  ange pages and. 
12741 20 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 61 64 64   ** continue add
12742 69 6e 67 20 70 61 67 65 73 20 72 6f 6c 6c 65 64  ing pages rolled
12743 20 62 61 63 6b 20 74 6f 20 70 44 6f 6e 65 2e 0a   back to pDone..
12744 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 72 63    */.  while( rc
12745 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
12746 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
12747 66 3c 73 7a 4a 20 29 7b 0a 20 20 20 20 75 33 32  f<szJ ){.    u32
12748 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20   ii;            
12749 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
1274a 2a 2f 0a 20 20 20 20 75 33 32 20 6e 4a 52 65 63  */.    u32 nJRec
1274b 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d   = 0;     /* Num
1274c 62 65 72 20 6f 66 20 4a 6f 75 72 6e 61 6c 20 52  ber of Journal R
1274d 65 63 6f 72 64 73 20 2a 2f 0a 20 20 20 20 75 33  ecords */.    u3
1274e 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20 72 63 20  2 dummy;.    rc 
1274f 3d 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72  = readJournalHdr
12750 28 70 50 61 67 65 72 2c 20 30 2c 20 73 7a 4a 2c  (pPager, 0, szJ,
12751 20 26 6e 4a 52 65 63 2c 20 26 64 75 6d 6d 79 29   &nJRec, &dummy)
12752 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63  ;.    assert( rc
12753 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b  !=SQLITE_DONE );
12754 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20  ..    /*.    ** 
12755 54 68 65 20 22 70 50 61 67 65 72 2d 3e 6a 6f 75  The "pPager->jou
12756 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f  rnalHdr+JOURNAL_
12757 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d  HDR_SZ(pPager)==
12758 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
12759 66 66 22 0a 20 20 20 20 2a 2a 20 74 65 73 74 20  ff".    ** test 
1275a 69 73 20 72 65 6c 61 74 65 64 20 74 6f 20 74 69  is related to ti
1275b 63 6b 65 74 20 23 32 35 36 35 2e 20 20 53 65 65  cket #2565.  See
1275c 20 74 68 65 20 64 69 73 63 75 73 73 69 6f 6e 20   the discussion 
1275d 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 61  in the.    ** pa
1275e 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 20 66  ger_playback() f
1275f 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 64 64 69  unction for addi
12760 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
12761 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  on..    */.    i
12762 66 28 20 6e 4a 52 65 63 3d 3d 30 20 0a 20 20 20  f( nJRec==0 .   
12763 20 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75    && pPager->jou
12764 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f  rnalHdr+JOURNAL_
12765 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d  HDR_SZ(pPager)==
12766 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
12767 66 66 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  ff.    ){.      
12768 6e 4a 52 65 63 20 3d 20 28 75 33 32 29 28 28 73  nJRec = (u32)((s
12769 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75  zJ - pPager->jou
1276a 72 6e 61 6c 4f 66 66 29 2f 4a 4f 55 52 4e 41 4c  rnalOff)/JOURNAL
1276b 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 29 3b  _PG_SZ(pPager));
1276c 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69  .    }.    for(i
1276d 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  i=0; rc==SQLITE_
1276e 4f 4b 20 26 26 20 69 69 3c 6e 4a 52 65 63 20 26  OK && ii<nJRec &
1276f 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  & pPager->journa
12770 6c 4f 66 66 3c 73 7a 4a 3b 20 69 69 2b 2b 29 7b  lOff<szJ; ii++){
12771 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
12772 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70  r_playback_one_p
12773 61 67 65 28 70 50 61 67 65 72 2c 20 31 2c 20 30  age(pPager, 1, 0
12774 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  , &pPager->journ
12775 61 6c 4f 66 66 2c 20 31 2c 20 70 44 6f 6e 65 29  alOff, 1, pDone)
12776 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
12777 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44  rt( rc!=SQLITE_D
12778 4f 4e 45 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73  ONE );.  }.  ass
12779 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
1277a 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f  OK || pPager->jo
1277b 75 72 6e 61 6c 4f 66 66 3d 3d 73 7a 4a 20 29 3b  urnalOff==szJ );
1277c 0a 0a 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20  ..  /* Finally, 
1277d 20 72 6f 6c 6c 62 61 63 6b 20 70 61 67 65 73 20   rollback pages 
1277e 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75  from the sub-jou
1277f 72 6e 61 6c 2e 20 20 50 61 67 65 20 74 68 61 74  rnal.  Page that
12780 20 77 65 72 65 0a 20 20 2a 2a 20 70 72 65 76 69   were.  ** previ
12781 6f 75 73 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63  ously rolled bac
12782 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6d 61 69  k out of the mai
12783 6e 20 6a 6f 75 72 6e 61 6c 20 28 61 6e 64 20 61  n journal (and a
12784 72 65 20 68 65 6e 63 65 20 69 6e 20 70 44 6f 6e  re hence in pDon
12785 65 29 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20  e).  ** will be 
12786 73 6b 69 70 70 65 64 2e 20 20 4f 75 74 2d 6f 66  skipped.  Out-of
12787 2d 72 61 6e 67 65 20 70 61 67 65 73 20 61 72 65  -range pages are
12788 20 61 6c 73 6f 20 73 6b 69 70 70 65 64 2e 0a 20   also skipped.. 
12789 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 65 70   */.  if( pSavep
1278a 6f 69 6e 74 20 29 7b 0a 20 20 20 20 75 33 32 20  oint ){.    u32 
1278b 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ii;            /
1278c 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
1278d 2f 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74  /.    i64 offset
1278e 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69   = pSavepoint->i
1278f 53 75 62 52 65 63 2a 28 34 2b 70 50 61 67 65 72  SubRec*(4+pPager
12790 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20  ->pageSize);.   
12791 20 66 6f 72 28 69 69 3d 70 53 61 76 65 70 6f 69   for(ii=pSavepoi
12792 6e 74 2d 3e 69 53 75 62 52 65 63 3b 20 72 63 3d  nt->iSubRec; rc=
12793 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69  =SQLITE_OK && ii
12794 3c 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63  <pPager->nSubRec
12795 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61  ; ii++){.      a
12796 73 73 65 72 74 28 20 6f 66 66 73 65 74 3d 3d 69  ssert( offset==i
12797 69 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70 61 67  i*(4+pPager->pag
12798 65 53 69 7a 65 29 20 29 3b 0a 20 20 20 20 20 20  eSize) );.      
12799 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62  rc = pager_playb
1279a 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61  ack_one_page(pPa
1279b 67 65 72 2c 20 30 2c 20 30 2c 20 26 6f 66 66 73  ger, 0, 0, &offs
1279c 65 74 2c 20 31 2c 20 70 44 6f 6e 65 29 3b 0a 20  et, 1, pDone);. 
1279d 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
1279e 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc!=SQLITE_DONE
1279f 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74   );.  }..  sqlit
127a0 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28  e3BitvecDestroy(
127a1 70 44 6f 6e 65 29 3b 0a 20 20 69 66 28 20 72 63  pDone);.  if( rc
127a2 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
127a3 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
127a4 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 7d  alOff = szJ;.  }
127a5 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
127a6 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
127a7 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
127a8 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61   of in-memory pa
127a9 67 65 73 20 74 68 61 74 20 61 72 65 20 61 6c 6c  ges that are all
127aa 6f 77 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  owed..*/.SQLITE_
127ab 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
127ac 69 74 65 33 50 61 67 65 72 53 65 74 43 61 63 68  ite3PagerSetCach
127ad 65 73 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61  esize(Pager *pPa
127ae 67 65 72 2c 20 69 6e 74 20 6d 78 50 61 67 65 29  ger, int mxPage)
127af 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68  {.  sqlite3Pcach
127b0 65 53 65 74 43 61 63 68 65 73 69 7a 65 28 70 50  eSetCachesize(pP
127b1 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 6d  ager->pPCache, m
127b2 78 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  xPage);.}../*.**
127b3 20 41 64 6a 75 73 74 20 74 68 65 20 72 6f 62 75   Adjust the robu
127b4 73 74 6e 65 73 73 20 6f 66 20 74 68 65 20 64 61  stness of the da
127b5 74 61 62 61 73 65 20 74 6f 20 64 61 6d 61 67 65  tabase to damage
127b6 20 64 75 65 20 74 6f 20 4f 53 20 63 72 61 73 68   due to OS crash
127b7 65 73 0a 2a 2a 20 6f 72 20 70 6f 77 65 72 20 66  es.** or power f
127b8 61 69 6c 75 72 65 73 20 62 79 20 63 68 61 6e 67  ailures by chang
127b9 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ing the number o
127ba 66 20 73 79 6e 63 73 28 29 73 20 77 68 65 6e 20  f syncs()s when 
127bb 77 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 72  writing.** the r
127bc 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e  ollback journal.
127bd 20 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65    There are thre
127be 65 20 6c 65 76 65 6c 73 3a 0a 2a 2a 0a 2a 2a 20  e levels:.**.** 
127bf 20 20 20 4f 46 46 20 20 20 20 20 20 20 73 71 6c     OFF       sql
127c0 69 74 65 33 4f 73 53 79 6e 63 28 29 20 69 73 20  ite3OsSync() is 
127c1 6e 65 76 65 72 20 63 61 6c 6c 65 64 2e 20 20 54  never called.  T
127c2 68 69 73 20 69 73 20 74 68 65 20 64 65 66 61 75  his is the defau
127c3 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  lt.**           
127c4 20 20 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79     for temporary
127c5 20 61 6e 64 20 74 72 61 6e 73 69 65 6e 74 20 66   and transient f
127c6 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 4e  iles..**.**    N
127c7 4f 52 4d 41 4c 20 20 20 20 54 68 65 20 6a 6f 75  ORMAL    The jou
127c8 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 6f  rnal is synced o
127c9 6e 63 65 20 62 65 66 6f 72 65 20 77 72 69 74 65  nce before write
127ca 73 20 62 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a  s begin on the.*
127cb 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64  *              d
127cc 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 69  atabase.  This i
127cd 73 20 6e 6f 72 6d 61 6c 6c 79 20 61 64 65 71 75  s normally adequ
127ce 61 74 65 20 70 72 6f 74 65 63 74 69 6f 6e 2c 20  ate protection, 
127cf 62 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  but.**          
127d0 20 20 20 20 69 74 20 69 73 20 74 68 65 6f 72 65      it is theore
127d1 74 69 63 61 6c 6c 79 20 70 6f 73 73 69 62 6c 65  tically possible
127d2 2c 20 74 68 6f 75 67 68 20 76 65 72 79 20 75 6e  , though very un
127d3 6c 69 6b 65 6c 79 2c 0a 2a 2a 20 20 20 20 20 20  likely,.**      
127d4 20 20 20 20 20 20 20 20 74 68 61 74 20 61 6e 20          that an 
127d5 69 6e 6f 70 65 72 74 75 6e 65 20 70 6f 77 65 72  inopertune power
127d6 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c   failure could l
127d7 65 61 76 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  eave the journal
127d8 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
127d9 20 69 6e 20 61 20 73 74 61 74 65 20 77 68 69 63   in a state whic
127da 68 20 77 6f 75 6c 64 20 63 61 75 73 65 20 64 61  h would cause da
127db 6d 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61  mage to the data
127dc 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  base.**         
127dd 20 20 20 20 20 77 68 65 6e 20 69 74 20 69 73 20       when it is 
127de 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a  rolled back..**.
127df 2a 2a 20 20 20 20 46 55 4c 4c 20 20 20 20 20 20  **    FULL      
127e0 54 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73  The journal is s
127e1 79 6e 63 65 64 20 74 77 69 63 65 20 62 65 66 6f  ynced twice befo
127e2 72 65 20 77 72 69 74 65 73 20 62 65 67 69 6e 20  re writes begin 
127e3 6f 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  on the.**       
127e4 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20         database 
127e5 28 77 69 74 68 20 73 6f 6d 65 20 61 64 64 69 74  (with some addit
127e6 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
127e7 6e 20 2d 20 74 68 65 20 6e 52 65 63 20 66 69 65  n - the nRec fie
127e8 6c 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ld.**           
127e9 20 20 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61     of the journa
127ea 6c 20 68 65 61 64 65 72 20 2d 20 62 65 69 6e 67  l header - being
127eb 20 77 72 69 74 74 65 6e 20 69 6e 20 62 65 74 77   written in betw
127ec 65 65 6e 20 74 68 65 20 74 77 6f 0a 2a 2a 20 20  een the two.**  
127ed 20 20 20 20 20 20 20 20 20 20 20 20 73 79 6e 63              sync
127ee 73 29 2e 20 20 49 66 20 77 65 20 61 73 73 75 6d  s).  If we assum
127ef 65 20 74 68 61 74 20 77 72 69 74 69 6e 67 20 61  e that writing a
127f0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
127f1 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63   single disk sec
127f2 74 6f 72 20 69 73 20 61 74 6f 6d 69 63 2c 20 74  tor is atomic, t
127f3 68 65 6e 20 74 68 69 73 20 6d 6f 64 65 20 70 72  hen this mode pr
127f4 6f 76 69 64 65 73 0a 2a 2a 20 20 20 20 20 20 20  ovides.**       
127f5 20 20 20 20 20 20 20 61 73 73 75 72 61 6e 63 65         assurance
127f6 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61   that the journa
127f7 6c 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 63 6f  l will not be co
127f8 72 72 75 70 74 65 64 20 74 6f 20 74 68 65 0a 2a  rrupted to the.*
127f9 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  *              p
127fa 6f 69 6e 74 20 6f 66 20 63 61 75 73 69 6e 67 20  oint of causing 
127fb 64 61 6d 61 67 65 20 74 6f 20 74 68 65 20 64 61  damage to the da
127fc 74 61 62 61 73 65 20 64 75 72 69 6e 67 20 72 6f  tabase during ro
127fd 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 4e 75  llback..**.** Nu
127fe 6d 65 72 69 63 20 76 61 6c 75 65 73 20 61 73 73  meric values ass
127ff 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
12800 73 65 20 73 74 61 74 65 73 20 61 72 65 20 4f 46  se states are OF
12801 46 3d 3d 31 2c 20 4e 4f 52 4d 41 4c 3d 32 2c 0a  F==1, NORMAL=2,.
12802 2a 2a 20 61 6e 64 20 46 55 4c 4c 3d 33 2e 0a 2a  ** and FULL=3..*
12803 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
12804 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47  _OMIT_PAGER_PRAG
12805 4d 41 53 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  MAS.SQLITE_PRIVA
12806 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
12807 61 67 65 72 53 65 74 53 61 66 65 74 79 4c 65 76  agerSetSafetyLev
12808 65 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  el(Pager *pPager
12809 2c 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74  , int level, int
1280a 20 62 46 75 6c 6c 46 73 79 6e 63 29 7b 0a 20 20   bFullFsync){.  
1280b 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d  pPager->noSync =
1280c 20 20 28 6c 65 76 65 6c 3d 3d 31 20 7c 7c 20 70    (level==1 || p
1280d 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29  Pager->tempFile)
1280e 20 3f 31 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d   ?1:0;.  pPager-
1280f 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 28 6c 65 76  >fullSync = (lev
12810 65 6c 3d 3d 33 20 26 26 20 21 70 50 61 67 65 72  el==3 && !pPager
12811 2d 3e 74 65 6d 70 46 69 6c 65 29 20 3f 31 3a 30  ->tempFile) ?1:0
12812 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 79 6e 63  ;.  pPager->sync
12813 5f 66 6c 61 67 73 20 3d 20 28 62 46 75 6c 6c 46  _flags = (bFullF
12814 73 79 6e 63 3f 53 51 4c 49 54 45 5f 53 59 4e 43  sync?SQLITE_SYNC
12815 5f 46 55 4c 4c 3a 53 51 4c 49 54 45 5f 53 59 4e  _FULL:SQLITE_SYN
12816 43 5f 4e 4f 52 4d 41 4c 29 3b 0a 20 20 69 66 28  C_NORMAL);.  if(
12817 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20   pPager->noSync 
12818 29 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79  ) pPager->needSy
12819 6e 63 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69 66  nc = 0;.}.#endif
1281a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
1281b 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72  owing global var
1281c 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65  iable is increme
1281d 6e 74 65 64 20 77 68 65 6e 65 76 65 72 20 74 68  nted whenever th
1281e 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 61 74 74  e library.** att
1281f 65 6d 70 74 73 20 74 6f 20 6f 70 65 6e 20 61 20  empts to open a 
12820 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20  temporary file. 
12821 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f   This informatio
12822 6e 20 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a  n is used for.**
12823 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e 61   testing and ana
12824 6c 79 73 69 73 20 6f 6e 6c 79 2e 20 20 0a 2a 2f  lysis only.  .*/
12825 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
12826 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  EST.SQLITE_API i
12827 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74  nt sqlite3_opent
12828 65 6d 70 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23  emp_count = 0;.#
12829 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65  endif../*.** Ope
1282a 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69  n a temporary fi
1282b 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20  le..**.** Write 
1282c 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
1282d 74 6f 72 20 69 6e 74 6f 20 2a 70 46 69 6c 65 2e  tor into *pFile.
1282e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
1282f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 0a 2a 2a  K on success .**
12830 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65   or some other e
12831 72 72 6f 72 20 63 6f 64 65 20 69 66 20 77 65 20  rror code if we 
12832 66 61 69 6c 2e 20 54 68 65 20 4f 53 20 77 69 6c  fail. The OS wil
12833 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  l automatically 
12834 0a 2a 2a 20 64 65 6c 65 74 65 20 74 68 65 20 74  .** delete the t
12835 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 77 68  emporary file wh
12836 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e  en it is closed.
12837 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73  .**.** The flags
12838 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 56   passed to the V
12839 46 53 20 6c 61 79 65 72 20 78 4f 70 65 6e 28 29  FS layer xOpen()
1283a 20 63 61 6c 6c 20 61 72 65 20 74 68 6f 73 65 20   call are those 
1283b 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20  specified.** by 
1283c 70 61 72 61 6d 65 74 65 72 20 76 66 73 46 6c 61  parameter vfsFla
1283d 67 73 20 4f 52 65 64 20 77 69 74 68 20 74 68 65  gs ORed with the
1283e 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a   following:.**.*
1283f 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45  *     SQLITE_OPE
12840 4e 5f 52 45 41 44 57 52 49 54 45 0a 2a 2a 20 20  N_READWRITE.**  
12841 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43     SQLITE_OPEN_C
12842 52 45 41 54 45 0a 2a 2a 20 20 20 20 20 53 51 4c  REATE.**     SQL
12843 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49  ITE_OPEN_EXCLUSI
12844 56 45 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  VE.**     SQLITE
12845 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c  _OPEN_DELETEONCL
12846 4f 53 45 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  OSE.*/.static in
12847 74 20 70 61 67 65 72 4f 70 65 6e 74 65 6d 70 28  t pagerOpentemp(
12848 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
12849 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ,        /* The 
1284a 70 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a  pager object */.
1284b 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
1284c 70 46 69 6c 65 2c 20 20 2f 2a 20 57 72 69 74 65  pFile,  /* Write
1284d 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
1284e 70 74 6f 72 20 68 65 72 65 20 2a 2f 0a 20 20 69  ptor here */.  i
1284f 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20 20 20  nt vfsFlags     
12850 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61       /* Flags pa
12851 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20  ssed through to 
12852 74 68 65 20 56 46 53 20 2a 2f 0a 29 7b 0a 20 20  the VFS */.){.  
12853 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
12854 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
12855 63 6f 64 65 20 2a 2f 0a 0a 23 69 66 64 65 66 20  code */..#ifdef 
12856 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71  SQLITE_TEST.  sq
12857 6c 69 74 65 33 5f 6f 70 65 6e 74 65 6d 70 5f 63  lite3_opentemp_c
12858 6f 75 6e 74 2b 2b 3b 20 20 2f 2a 20 55 73 65 64  ount++;  /* Used
12859 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64   for testing and
1285a 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 20 2a   analysis only *
1285b 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 76 66 73 46  /.#endif..  vfsF
1285c 6c 61 67 73 20 7c 3d 20 20 53 51 4c 49 54 45 5f  lags |=  SQLITE_
1285d 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c  OPEN_READWRITE |
1285e 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45   SQLITE_OPEN_CRE
1285f 41 54 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20  ATE |.          
12860 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58    SQLITE_OPEN_EX
12861 43 4c 55 53 49 56 45 20 7c 20 53 51 4c 49 54 45  CLUSIVE | SQLITE
12862 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c  _OPEN_DELETEONCL
12863 4f 53 45 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  OSE;.  rc = sqli
12864 74 65 33 4f 73 4f 70 65 6e 28 70 50 61 67 65 72  te3OsOpen(pPager
12865 2d 3e 70 56 66 73 2c 20 30 2c 20 70 46 69 6c 65  ->pVfs, 0, pFile
12866 2c 20 76 66 73 46 6c 61 67 73 2c 20 30 29 3b 0a  , vfsFlags, 0);.
12867 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51    assert( rc!=SQ
12868 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65  LITE_OK || isOpe
12869 6e 28 70 46 69 6c 65 29 20 29 3b 0a 20 20 72 65  n(pFile) );.  re
1286a 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1286b 2a 20 53 65 74 20 74 68 65 20 62 75 73 79 20 68  * Set the busy h
1286c 61 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e 2e  andler function.
1286d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72  .**.** The pager
1286e 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 62 75 73   invokes the bus
1286f 79 2d 68 61 6e 64 6c 65 72 20 69 66 20 73 71 6c  y-handler if sql
12870 69 74 65 33 4f 73 4c 6f 63 6b 28 29 20 72 65 74  ite3OsLock() ret
12871 75 72 6e 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f  urns .** SQLITE_
12872 42 55 53 59 20 77 68 65 6e 20 74 72 79 69 6e 67  BUSY when trying
12873 20 74 6f 20 75 70 67 72 61 64 65 20 66 72 6f 6d   to upgrade from
12874 20 6e 6f 2d 6c 6f 63 6b 20 74 6f 20 61 20 53 48   no-lock to a SH
12875 41 52 45 44 20 6c 6f 63 6b 2c 0a 2a 2a 20 6f 72  ARED lock,.** or
12876 20 77 68 65 6e 20 74 72 79 69 6e 67 20 74 6f 20   when trying to 
12877 75 70 67 72 61 64 65 20 66 72 6f 6d 20 61 20 52  upgrade from a R
12878 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 74 6f 20  ESERVED lock to 
12879 61 6e 20 45 58 43 4c 55 53 49 56 45 20 0a 2a 2a  an EXCLUSIVE .**
1287a 20 6c 6f 63 6b 2e 20 49 74 20 64 6f 65 73 20 2a   lock. It does *
1287b 6e 6f 74 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20  not* invoke the 
1287c 62 75 73 79 20 68 61 6e 64 6c 65 72 20 77 68 65  busy handler whe
1287d 6e 20 75 70 67 72 61 64 69 6e 67 20 66 72 6f 6d  n upgrading from
1287e 0a 2a 2a 20 53 48 41 52 45 44 20 74 6f 20 52 45  .** SHARED to RE
1287f 53 45 52 56 45 44 2c 20 6f 72 20 77 68 65 6e 20  SERVED, or when 
12880 75 70 67 72 61 64 69 6e 67 20 66 72 6f 6d 20 53  upgrading from S
12881 48 41 52 45 44 20 74 6f 20 45 58 43 4c 55 53 49  HARED to EXCLUSI
12882 56 45 0a 2a 2a 20 28 77 68 69 63 68 20 6f 63 63  VE.** (which occ
12883 75 72 73 20 64 75 72 69 6e 67 20 68 6f 74 2d 6a  urs during hot-j
12884 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 29  ournal rollback)
12885 2e 20 53 75 6d 6d 61 72 79 3a 0a 2a 2a 0a 2a 2a  . Summary:.**.**
12886 20 20 20 54 72 61 6e 73 69 74 69 6f 6e 20 20 20     Transition   
12887 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12888 20 20 20 20 20 7c 20 49 6e 76 6f 6b 65 73 20 78       | Invokes x
12889 42 75 73 79 48 61 6e 64 6c 65 72 0a 2a 2a 20 20  BusyHandler.**  
1288a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
1288b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1288c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1288d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 4e  ---------.**   N
1288e 4f 5f 4c 4f 43 4b 20 20 20 20 20 20 20 2d 3e 20  O_LOCK       -> 
1288f 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20  SHARED_LOCK     
12890 20 7c 20 59 65 73 0a 2a 2a 20 20 20 53 48 41 52   | Yes.**   SHAR
12891 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20 52 45 53  ED_LOCK   -> RES
12892 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20 20 7c 20  ERVED_LOCK    | 
12893 4e 6f 0a 2a 2a 20 20 20 53 48 41 52 45 44 5f 4c  No.**   SHARED_L
12894 4f 43 4b 20 20 20 2d 3e 20 45 58 43 4c 55 53 49  OCK   -> EXCLUSI
12895 56 45 5f 4c 4f 43 4b 20 20 20 7c 20 4e 6f 0a 2a  VE_LOCK   | No.*
12896 2a 20 20 20 52 45 53 45 52 56 45 44 5f 4c 4f 43  *   RESERVED_LOC
12897 4b 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 5f 4c  K -> EXCLUSIVE_L
12898 4f 43 4b 20 20 20 7c 20 59 65 73 0a 2a 2a 0a 2a  OCK   | Yes.**.*
12899 2a 20 49 66 20 74 68 65 20 62 75 73 79 2d 68 61  * If the busy-ha
1289a 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 72  ndler callback r
1289b 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  eturns non-zero,
1289c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 0a 2a 2a   the lock is .**
1289d 20 72 65 74 72 69 65 64 2e 20 49 66 20 69 74 20   retried. If it 
1289e 72 65 74 75 72 6e 73 20 7a 65 72 6f 2c 20 74 68  returns zero, th
1289f 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 42 55  en the SQLITE_BU
128a0 53 59 20 65 72 72 6f 72 20 69 73 0a 2a 2a 20 72  SY error is.** r
128a1 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63  eturned to the c
128a2 61 6c 6c 65 72 20 6f 66 20 74 68 65 20 70 61 67  aller of the pag
128a3 65 72 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e  er API function.
128a4 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
128a5 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
128a6 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c  agerSetBusyhandl
128a7 65 72 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61  er(.  Pager *pPa
128a8 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  ger,            
128a9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
128aa 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  ger object */.  
128ab 69 6e 74 20 28 2a 78 42 75 73 79 48 61 6e 64 6c  int (*xBusyHandl
128ac 65 72 29 28 76 6f 69 64 20 2a 29 2c 20 20 20 20  er)(void *),    
128ad 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
128ae 74 6f 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20  to busy-handler 
128af 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f  function */.  vo
128b0 69 64 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72  id *pBusyHandler
128b1 41 72 67 20 20 20 20 20 20 20 20 20 20 20 20 20  Arg             
128b2 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74     /* Argument t
128b3 6f 20 70 61 73 73 20 74 6f 20 78 42 75 73 79 48  o pass to xBusyH
128b4 61 6e 64 6c 65 72 20 2a 2f 0a 29 7b 20 20 0a 20  andler */.){  . 
128b5 20 70 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61   pPager->xBusyHa
128b6 6e 64 6c 65 72 20 3d 20 78 42 75 73 79 48 61 6e  ndler = xBusyHan
128b7 64 6c 65 72 3b 0a 20 20 70 50 61 67 65 72 2d 3e  dler;.  pPager->
128b8 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 20  pBusyHandlerArg 
128b9 3d 20 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72  = pBusyHandlerAr
128ba 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 70 6f  g;.}../*.** Repo
128bb 72 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 70  rt the current p
128bc 61 67 65 20 73 69 7a 65 20 61 6e 64 20 6e 75 6d  age size and num
128bd 62 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 20  ber of reserved 
128be 62 79 74 65 73 20 62 61 63 6b 0a 2a 2a 20 74 6f  bytes back.** to
128bf 20 74 68 65 20 63 6f 64 65 63 2e 0a 2a 2f 0a 23   the codec..*/.#
128c0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53  ifdef SQLITE_HAS
128c1 5f 43 4f 44 45 43 0a 73 74 61 74 69 63 20 76 6f  _CODEC.static vo
128c2 69 64 20 70 61 67 65 72 52 65 70 6f 72 74 53 69  id pagerReportSi
128c3 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ze(Pager *pPager
128c4 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  ){.  if( pPager-
128c5 3e 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 20  >xCodecSizeChng 
128c6 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 78  ){.    pPager->x
128c7 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 28 70 50  CodecSizeChng(pP
128c8 61 67 65 72 2d 3e 70 43 6f 64 65 63 2c 20 70 50  ager->pCodec, pP
128c9 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 0a  ager->pageSize,.
128ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
128cb 20 20 20 20 20 20 20 20 20 20 20 28 69 6e 74 29             (int)
128cc 70 50 61 67 65 72 2d 3e 6e 52 65 73 65 72 76 65  pPager->nReserve
128cd 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23  );.  }.}.#else.#
128ce 20 64 65 66 69 6e 65 20 70 61 67 65 72 52 65 70   define pagerRep
128cf 6f 72 74 53 69 7a 65 28 58 29 20 20 20 20 20 2f  ortSize(X)     /
128d0 2a 20 4e 6f 2d 6f 70 20 69 66 20 77 65 20 64 6f  * No-op if we do
128d1 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 61 20 63   not support a c
128d2 6f 64 65 63 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a  odec */.#endif..
128d3 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
128d4 20 70 61 67 65 20 73 69 7a 65 20 75 73 65 64 20   page size used 
128d5 62 79 20 74 68 65 20 50 61 67 65 72 20 6f 62 6a  by the Pager obj
128d6 65 63 74 2e 20 54 68 65 20 6e 65 77 20 70 61 67  ect. The new pag
128d7 65 20 73 69 7a 65 20 0a 2a 2a 20 69 73 20 70 61  e size .** is pa
128d8 73 73 65 64 20 69 6e 20 2a 70 50 61 67 65 53 69  ssed in *pPageSi
128d9 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ze..**.** If the
128da 20 70 61 67 65 72 20 69 73 20 69 6e 20 74 68 65   pager is in the
128db 20 65 72 72 6f 72 20 73 74 61 74 65 20 77 68 65   error state whe
128dc 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
128dd 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 0a 2a 2a  is called, it.**
128de 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 54 68 65   is a no-op. The
128df 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
128e0 69 73 20 74 68 65 20 65 72 72 6f 72 20 73 74 61  is the error sta
128e1 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 28 69  te error code (i
128e2 2e 65 2e 20 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53  .e. .** one of S
128e3 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c  QLITE_IOERR, SQL
128e4 49 54 45 5f 43 4f 52 52 55 50 54 20 6f 72 20 53  ITE_CORRUPT or S
128e5 51 4c 49 54 45 5f 46 55 4c 4c 29 2e 0a 2a 2a 0a  QLITE_FULL)..**.
128e6 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66  ** Otherwise, if
128e7 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c   all of the foll
128e8 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65 3a 0a  owing are true:.
128e9 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 20 6e 65  **.**   * the ne
128ea 77 20 70 61 67 65 20 73 69 7a 65 20 28 76 61 6c  w page size (val
128eb 75 65 20 6f 66 20 2a 70 50 61 67 65 53 69 7a 65  ue of *pPageSize
128ec 29 20 69 73 20 76 61 6c 69 64 20 28 61 20 70 6f  ) is valid (a po
128ed 77 65 72 20 0a 2a 2a 20 20 20 20 20 6f 66 20 74  wer .**     of t
128ee 77 6f 20 62 65 74 77 65 65 6e 20 35 31 32 20 61  wo between 512 a
128ef 6e 64 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41  nd SQLITE_MAX_PA
128f0 47 45 5f 53 49 5a 45 2c 20 69 6e 63 6c 75 73 69  GE_SIZE, inclusi
128f1 76 65 29 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20  ve), and.**.**  
128f2 20 2a 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20   * there are no 
128f3 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65  outstanding page
128f4 20 72 65 66 65 72 65 6e 63 65 73 2c 20 61 6e 64   references, and
128f5 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64  .**.**   * the d
128f6 61 74 61 62 61 73 65 20 69 73 20 65 69 74 68 65  atabase is eithe
128f7 72 20 6e 6f 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f  r not an in-memo
128f8 72 79 20 64 61 74 61 62 61 73 65 20 6f 72 20 69  ry database or i
128f9 74 20 69 73 0a 2a 2a 20 20 20 20 20 61 6e 20 69  t is.**     an i
128fa 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
128fb 65 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79  e that currently
128fc 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 7a 65 72   consists of zer
128fd 6f 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 74  o pages..**.** t
128fe 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 6f 62  hen the pager ob
128ff 6a 65 63 74 20 70 61 67 65 20 73 69 7a 65 20 69  ject page size i
12900 73 20 73 65 74 20 74 6f 20 2a 70 50 61 67 65 53  s set to *pPageS
12901 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ize..**.** If th
12902 65 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 63  e page size is c
12903 68 61 6e 67 65 64 2c 20 74 68 65 6e 20 74 68 69  hanged, then thi
12904 73 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 73 20  s function uses 
12905 73 71 6c 69 74 65 33 50 61 67 65 72 4d 61 6c 6c  sqlite3PagerMall
12906 6f 63 28 29 20 0a 2a 2a 20 74 6f 20 6f 62 74 61  oc() .** to obta
12907 69 6e 20 61 20 6e 65 77 20 50 61 67 65 72 2e 70  in a new Pager.p
12908 54 6d 70 53 70 61 63 65 20 62 75 66 66 65 72 2e  TmpSpace buffer.
12909 20 49 66 20 74 68 69 73 20 61 6c 6c 6f 63 61 74   If this allocat
1290a 69 6f 6e 20 61 74 74 65 6d 70 74 20 0a 2a 2a 20  ion attempt .** 
1290b 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e 4f  fails, SQLITE_NO
1290c 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 20  MEM is returned 
1290d 61 6e 64 20 74 68 65 20 70 61 67 65 20 73 69 7a  and the page siz
1290e 65 20 72 65 6d 61 69 6e 73 20 75 6e 63 68 61 6e  e remains unchan
1290f 67 65 64 2e 20 0a 2a 2a 20 49 6e 20 61 6c 6c 20  ged. .** In all 
12910 6f 74 68 65 72 20 63 61 73 65 73 2c 20 53 51 4c  other cases, SQL
12911 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
12912 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ed..**.** If the
12913 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 6e 6f   page size is no
12914 74 20 63 68 61 6e 67 65 64 2c 20 65 69 74 68 65  t changed, eithe
12915 72 20 62 65 63 61 75 73 65 20 6f 6e 65 20 6f 66  r because one of
12916 20 74 68 65 20 65 6e 75 6d 65 72 61 74 65 64 0a   the enumerated.
12917 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 62  ** conditions ab
12918 6f 76 65 20 69 73 20 6e 6f 74 20 74 72 75 65 2c  ove is not true,
12919 20 74 68 65 20 70 61 67 65 72 20 77 61 73 20 69   the pager was i
1291a 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 77 68  n error state wh
1291b 65 6e 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74  en this.** funct
1291c 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64 2c 20  ion was called, 
1291d 6f 72 20 62 65 63 61 75 73 65 20 74 68 65 20 6d  or because the m
1291e 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
1291f 20 61 74 74 65 6d 70 74 20 66 61 69 6c 65 64 2c   attempt failed,
12920 20 0a 2a 2a 20 74 68 65 6e 20 2a 70 50 61 67 65   .** then *pPage
12921 53 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20 74  Size is set to t
12922 68 65 20 6f 6c 64 2c 20 72 65 74 61 69 6e 65 64  he old, retained
12923 20 70 61 67 65 20 73 69 7a 65 20 62 65 66 6f 72   page size befor
12924 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a  e returning..*/.
12925 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
12926 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  nt sqlite3PagerS
12927 65 74 50 61 67 65 73 69 7a 65 28 50 61 67 65 72  etPagesize(Pager
12928 20 2a 70 50 61 67 65 72 2c 20 75 31 36 20 2a 70   *pPager, u16 *p
12929 50 61 67 65 53 69 7a 65 2c 20 69 6e 74 20 6e 52  PageSize, int nR
1292a 65 73 65 72 76 65 29 7b 0a 20 20 69 6e 74 20 72  eserve){.  int r
1292b 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43  c = pPager->errC
1292c 6f 64 65 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d  ode;..  if( rc==
1292d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1292e 20 75 31 36 20 70 61 67 65 53 69 7a 65 20 3d 20   u16 pageSize = 
1292f 2a 70 50 61 67 65 53 69 7a 65 3b 0a 20 20 20 20  *pPageSize;.    
12930 61 73 73 65 72 74 28 20 70 61 67 65 53 69 7a 65  assert( pageSize
12931 3d 3d 30 20 7c 7c 20 28 70 61 67 65 53 69 7a 65  ==0 || (pageSize
12932 3e 3d 35 31 32 20 26 26 20 70 61 67 65 53 69 7a  >=512 && pageSiz
12933 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41  e<=SQLITE_MAX_PA
12934 47 45 5f 53 49 5a 45 29 20 29 3b 0a 20 20 20 20  GE_SIZE) );.    
12935 69 66 28 20 28 70 50 61 67 65 72 2d 3e 6d 65 6d  if( (pPager->mem
12936 44 62 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d  Db==0 || pPager-
12937 3e 64 62 53 69 7a 65 3d 3d 30 29 0a 20 20 20 20  >dbSize==0).    
12938 20 26 26 20 73 71 6c 69 74 65 33 50 63 61 63 68   && sqlite3Pcach
12939 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72  eRefCount(pPager
1293a 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 0a 20  ->pPCache)==0 . 
1293b 20 20 20 20 26 26 20 70 61 67 65 53 69 7a 65 20      && pageSize 
1293c 26 26 20 70 61 67 65 53 69 7a 65 21 3d 70 50 61  && pageSize!=pPa
1293d 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 0a 20  ger->pageSize . 
1293e 20 20 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72     ){.      char
1293f 20 2a 70 4e 65 77 20 3d 20 28 63 68 61 72 20 2a   *pNew = (char *
12940 29 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c  )sqlite3PageMall
12941 6f 63 28 70 61 67 65 53 69 7a 65 29 3b 0a 20 20  oc(pageSize);.  
12942 20 20 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b      if( !pNew ){
12943 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
12944 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
12945 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
12946 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61   pager_reset(pPa
12947 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 70 50  ger);.        pP
12948 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 3d  ager->pageSize =
12949 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20   pageSize;.     
1294a 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 46 72     sqlite3PageFr
1294b 65 65 28 70 50 61 67 65 72 2d 3e 70 54 6d 70 53  ee(pPager->pTmpS
1294c 70 61 63 65 29 3b 0a 20 20 20 20 20 20 20 20 70  pace);.        p
1294d 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65  Pager->pTmpSpace
1294e 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 20   = pNew;.       
1294f 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 65   sqlite3PcacheSe
12950 74 50 61 67 65 53 69 7a 65 28 70 50 61 67 65 72  tPageSize(pPager
12951 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 67 65 53  ->pPCache, pageS
12952 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ize);.      }.  
12953 20 20 7d 0a 20 20 20 20 2a 70 50 61 67 65 53 69    }.    *pPageSi
12954 7a 65 20 3d 20 28 75 31 36 29 70 50 61 67 65 72  ze = (u16)pPager
12955 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20  ->pageSize;.    
12956 69 66 28 20 6e 52 65 73 65 72 76 65 3c 30 20 29  if( nReserve<0 )
12957 20 6e 52 65 73 65 72 76 65 20 3d 20 70 50 61 67   nReserve = pPag
12958 65 72 2d 3e 6e 52 65 73 65 72 76 65 3b 0a 20 20  er->nReserve;.  
12959 20 20 61 73 73 65 72 74 28 20 6e 52 65 73 65 72    assert( nReser
1295a 76 65 3e 3d 30 20 26 26 20 6e 52 65 73 65 72 76  ve>=0 && nReserv
1295b 65 3c 31 30 30 30 20 29 3b 0a 20 20 20 20 70 50  e<1000 );.    pP
1295c 61 67 65 72 2d 3e 6e 52 65 73 65 72 76 65 20 3d  ager->nReserve =
1295d 20 28 69 31 36 29 6e 52 65 73 65 72 76 65 3b 0a   (i16)nReserve;.
1295e 20 20 20 20 70 61 67 65 72 52 65 70 6f 72 74 53      pagerReportS
1295f 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  ize(pPager);.  }
12960 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
12961 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
12962 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 22  pointer to the "
12963 74 65 6d 70 6f 72 61 72 79 20 70 61 67 65 22 20  temporary page" 
12964 62 75 66 66 65 72 20 68 65 6c 64 20 69 6e 74 65  buffer held inte
12965 72 6e 61 6c 6c 79 0a 2a 2a 20 62 79 20 74 68 65  rnally.** by the
12966 20 70 61 67 65 72 2e 20 20 54 68 69 73 20 69 73   pager.  This is
12967 20 61 20 62 75 66 66 65 72 20 74 68 61 74 20 69   a buffer that i
12968 73 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20  s big enough to 
12969 68 6f 6c 64 20 74 68 65 0a 2a 2a 20 65 6e 74 69  hold the.** enti
1296a 72 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20  re content of a 
1296b 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 20  database page.  
1296c 54 68 69 73 20 62 75 66 66 65 72 20 69 73 20 75  This buffer is u
1296d 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a  sed internally.*
1296e 2a 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62 61 63  * during rollbac
1296f 6b 20 61 6e 64 20 77 69 6c 6c 20 62 65 20 6f 76  k and will be ov
12970 65 72 77 72 69 74 74 65 6e 20 77 68 65 6e 65 76  erwritten whenev
12971 65 72 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a  er a rollback.**
12972 20 6f 63 63 75 72 73 2e 20 20 42 75 74 20 6f 74   occurs.  But ot
12973 68 65 72 20 6d 6f 64 75 6c 65 73 20 61 72 65 20  her modules are 
12974 66 72 65 65 20 74 6f 20 75 73 65 20 69 74 20 74  free to use it t
12975 6f 6f 2c 20 61 73 20 6c 6f 6e 67 20 61 73 0a 2a  oo, as long as.*
12976 2a 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 61  * no rollbacks a
12977 72 65 20 68 61 70 70 65 6e 69 6e 67 2e 0a 2a 2f  re happening..*/
12978 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
12979 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67  void *sqlite3Pag
1297a 65 72 54 65 6d 70 53 70 61 63 65 28 50 61 67 65  erTempSpace(Page
1297b 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65  r *pPager){.  re
1297c 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 70 54 6d  turn pPager->pTm
1297d 70 53 70 61 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pSpace;.}../*.**
1297e 20 41 74 74 65 6d 70 74 20 74 6f 20 73 65 74 20   Attempt to set 
1297f 74 68 65 20 6d 61 78 69 6d 75 6d 20 64 61 74 61  the maximum data
12980 62 61 73 65 20 70 61 67 65 20 63 6f 75 6e 74 20  base page count 
12981 69 66 20 6d 78 50 61 67 65 20 69 73 20 70 6f 73  if mxPage is pos
12982 69 74 69 76 65 2e 20 0a 2a 2a 20 4d 61 6b 65 20  itive. .** Make 
12983 6e 6f 20 63 68 61 6e 67 65 73 20 69 66 20 6d 78  no changes if mx
12984 50 61 67 65 20 69 73 20 7a 65 72 6f 20 6f 72 20  Page is zero or 
12985 6e 65 67 61 74 69 76 65 2e 20 20 41 6e 64 20 6e  negative.  And n
12986 65 76 65 72 20 72 65 64 75 63 65 20 74 68 65 0a  ever reduce the.
12987 2a 2a 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20  ** maximum page 
12988 63 6f 75 6e 74 20 62 65 6c 6f 77 20 74 68 65 20  count below the 
12989 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20  current size of 
1298a 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a  the database..**
1298b 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f  .** Regardless o
1298c 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75 72 6e  f mxPage, return
1298d 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61 78   the current max
1298e 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 2e  imum page count.
1298f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
12990 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
12991 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28  gerMaxPageCount(
12992 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
12993 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 69 66  nt mxPage){.  if
12994 28 20 6d 78 50 61 67 65 3e 30 20 29 7b 0a 20 20  ( mxPage>0 ){.  
12995 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f    pPager->mxPgno
12996 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20 7d 0a 20   = mxPage;.  }. 
12997 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
12998 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 30  ecount(pPager, 0
12999 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67  );.  return pPag
1299a 65 72 2d 3e 6d 78 50 67 6e 6f 3b 0a 7d 0a 0a 2f  er->mxPgno;.}../
1299b 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
1299c 6e 67 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e  ng set of routin
1299d 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 64  es are used to d
1299e 69 73 61 62 6c 65 20 74 68 65 20 73 69 6d 75 6c  isable the simul
1299f 61 74 65 64 0a 2a 2a 20 49 2f 4f 20 65 72 72 6f  ated.** I/O erro
129a0 72 20 6d 65 63 68 61 6e 69 73 6d 2e 20 20 54 68  r mechanism.  Th
129a1 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ese routines are
129a2 20 75 73 65 64 20 74 6f 20 61 76 6f 69 64 20 73   used to avoid s
129a3 69 6d 75 6c 61 74 65 64 0a 2a 2a 20 65 72 72 6f  imulated.** erro
129a4 72 73 20 69 6e 20 70 6c 61 63 65 73 20 77 68 65  rs in places whe
129a5 72 65 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72  re we do not car
129a6 65 20 61 62 6f 75 74 20 65 72 72 6f 72 73 2e 0a  e about errors..
129a7 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 2d 44 53  **.** Unless -DS
129a8 51 4c 49 54 45 5f 54 45 53 54 3d 31 20 69 73 20  QLITE_TEST=1 is 
129a9 75 73 65 64 2c 20 74 68 65 73 65 20 72 6f 75 74  used, these rout
129aa 69 6e 65 73 20 61 72 65 20 61 6c 6c 20 6e 6f 2d  ines are all no-
129ab 6f 70 73 0a 2a 2a 20 61 6e 64 20 67 65 6e 65 72  ops.** and gener
129ac 61 74 65 20 6e 6f 20 63 6f 64 65 2e 0a 2a 2f 0a  ate no code..*/.
129ad 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
129ae 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 65 78  ST.SQLITE_API ex
129af 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33  tern int sqlite3
129b0 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e  _io_error_pendin
129b1 67 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 65 78  g;.SQLITE_API ex
129b2 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33  tern int sqlite3
129b3 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 3b 0a 73  _io_error_hit;.s
129b4 74 61 74 69 63 20 69 6e 74 20 73 61 76 65 64 5f  tatic int saved_
129b5 63 6e 74 3b 0a 76 6f 69 64 20 64 69 73 61 62 6c  cnt;.void disabl
129b6 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65  e_simulated_io_e
129b7 72 72 6f 72 73 28 76 6f 69 64 29 7b 0a 20 20 73  rrors(void){.  s
129b8 61 76 65 64 5f 63 6e 74 20 3d 20 73 71 6c 69 74  aved_cnt = sqlit
129b9 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64  e3_io_error_pend
129ba 69 6e 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69  ing;.  sqlite3_i
129bb 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20  o_error_pending 
129bc 3d 20 2d 31 3b 0a 7d 0a 76 6f 69 64 20 65 6e 61  = -1;.}.void ena
129bd 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f  ble_simulated_io
129be 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 7b 0a 20  _errors(void){. 
129bf 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
129c0 72 5f 70 65 6e 64 69 6e 67 20 3d 20 73 61 76 65  r_pending = save
129c1 64 5f 63 6e 74 3b 0a 7d 0a 23 65 6c 73 65 0a 23  d_cnt;.}.#else.#
129c2 20 64 65 66 69 6e 65 20 64 69 73 61 62 6c 65 5f   define disable_
129c3 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72  simulated_io_err
129c4 6f 72 73 28 29 0a 23 20 64 65 66 69 6e 65 20 65  ors().# define e
129c5 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f  nable_simulated_
129c6 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 65 6e 64  io_errors().#end
129c7 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74  if../*.** Read t
129c8 68 65 20 66 69 72 73 74 20 4e 20 62 79 74 65 73  he first N bytes
129c9 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e   from the beginn
129ca 69 6e 67 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ing of the file 
129cb 69 6e 74 6f 20 6d 65 6d 6f 72 79 0a 2a 2a 20 74  into memory.** t
129cc 68 61 74 20 70 44 65 73 74 20 70 6f 69 6e 74 73  hat pDest points
129cd 20 74 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74   to. .**.** If t
129ce 68 65 20 70 61 67 65 72 20 77 61 73 20 6f 70 65  he pager was ope
129cf 6e 65 64 20 6f 6e 20 61 20 74 72 61 6e 73 69 65  ned on a transie
129d0 6e 74 20 66 69 6c 65 20 28 7a 46 69 6c 65 6e 61  nt file (zFilena
129d1 6d 65 3d 3d 22 22 29 2c 20 6f 72 0a 2a 2a 20 6f  me==""), or.** o
129d2 70 65 6e 65 64 20 6f 6e 20 61 20 66 69 6c 65 20  pened on a file 
129d3 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 79 74 65  less than N byte
129d4 73 20 69 6e 20 73 69 7a 65 2c 20 74 68 65 20 6f  s in size, the o
129d5 75 74 70 75 74 20 62 75 66 66 65 72 20 69 73 0a  utput buffer is.
129d6 2a 2a 20 7a 65 72 6f 65 64 20 61 6e 64 20 53 51  ** zeroed and SQ
129d7 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64  LITE_OK returned
129d8 2e 20 54 68 65 20 72 61 74 69 6f 6e 61 6c 65 20  . The rationale 
129d9 66 6f 72 20 74 68 69 73 20 69 73 20 74 68 61 74  for this is that
129da 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69   this .** functi
129db 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  on is used to re
129dc 61 64 20 64 61 74 61 62 61 73 65 20 68 65 61 64  ad database head
129dd 65 72 73 2c 20 61 6e 64 20 61 20 6e 65 77 20 74  ers, and a new t
129de 72 61 6e 73 69 65 6e 74 20 6f 72 0a 2a 2a 20 7a  ransient or.** z
129df 65 72 6f 20 73 69 7a 65 64 20 64 61 74 61 62 61  ero sized databa
129e0 73 65 20 68 61 73 20 61 20 68 65 61 64 65 72 20  se has a header 
129e1 74 68 61 6e 20 63 6f 6e 73 69 73 74 73 20 65 6e  than consists en
129e2 74 69 72 65 6c 79 20 6f 66 20 7a 65 72 6f 65 73  tirely of zeroes
129e3 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 49  ..**.** If any I
129e4 4f 20 65 72 72 6f 72 20 61 70 61 72 74 20 66 72  O error apart fr
129e5 6f 6d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  om SQLITE_IOERR_
129e6 53 48 4f 52 54 5f 52 45 41 44 20 69 73 20 65 6e  SHORT_READ is en
129e7 63 6f 75 6e 74 65 72 65 64 2c 0a 2a 2a 20 74 68  countered,.** th
129e8 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  e error code is 
129e9 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20  returned to the 
129ea 63 61 6c 6c 65 72 20 61 6e 64 20 74 68 65 20 63  caller and the c
129eb 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a  ontents of the.*
129ec 2a 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20  * output buffer 
129ed 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51  undefined..*/.SQ
129ee 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
129ef 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 61   sqlite3PagerRea
129f0 64 46 69 6c 65 68 65 61 64 65 72 28 50 61 67 65  dFileheader(Page
129f1 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 4e  r *pPager, int N
129f2 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  , unsigned char 
129f3 2a 70 44 65 73 74 29 7b 0a 20 20 69 6e 74 20 72  *pDest){.  int r
129f4 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
129f5 20 6d 65 6d 73 65 74 28 70 44 65 73 74 2c 20 30   memset(pDest, 0
129f6 2c 20 4e 29 3b 0a 20 20 61 73 73 65 72 74 28 20  , N);.  assert( 
129f7 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66  isOpen(pPager->f
129f8 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65  d) || pPager->te
129f9 6d 70 46 69 6c 65 20 29 3b 0a 20 20 69 66 28 20  mpFile );.  if( 
129fa 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66  isOpen(pPager->f
129fb 64 29 20 29 7b 0a 20 20 20 20 49 4f 54 52 41 43  d) ){.    IOTRAC
129fc 45 28 28 22 44 42 48 44 52 20 25 70 20 30 20 25  E(("DBHDR %p 0 %
129fd 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 4e 29  d\n", pPager, N)
129fe 29 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ).    rc = sqlit
129ff 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d  e3OsRead(pPager-
12a00 3e 66 64 2c 20 70 44 65 73 74 2c 20 4e 2c 20 30  >fd, pDest, N, 0
12a01 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
12a02 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52  QLITE_IOERR_SHOR
12a03 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20  T_READ ){.      
12a04 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
12a05 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
12a06 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
12a07 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c  Return the total
12a08 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
12a09 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
12a0a 20 66 69 6c 65 20 61 73 73 6f 63 69 61 74 65 64   file associated
12a0b 20 0a 2a 2a 20 77 69 74 68 20 70 50 61 67 65 72   .** with pPager
12a0c 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 74 68 69 73  . Normally, this
12a0d 20 69 73 20 63 61 6c 63 75 6c 61 74 65 64 20 61   is calculated a
12a0e 73 20 28 3c 64 62 20 66 69 6c 65 20 73 69 7a 65  s (<db file size
12a0f 3e 2f 3c 70 61 67 65 2d 73 69 7a 65 3e 29 2e 0a  >/<page-size>)..
12a10 2a 2a 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74  ** However, if t
12a11 68 65 20 66 69 6c 65 20 69 73 20 62 65 74 77 65  he file is betwe
12a12 65 6e 20 31 20 61 6e 64 20 3c 70 61 67 65 2d 73  en 1 and <page-s
12a13 69 7a 65 3e 20 62 79 74 65 73 20 69 6e 20 73 69  ize> bytes in si
12a14 7a 65 2c 20 74 68 65 6e 20 0a 2a 2a 20 74 68 69  ze, then .** thi
12a15 73 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  s is considered 
12a16 61 20 31 20 70 61 67 65 20 66 69 6c 65 2e 0a 2a  a 1 page file..*
12a17 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65  *.** If the page
12a18 72 20 69 73 20 69 6e 20 65 72 72 6f 72 20 73 74  r is in error st
12a19 61 74 65 20 77 68 65 6e 20 74 68 69 73 20 66 75  ate when this fu
12a1a 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
12a1b 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 65 72  , then the.** er
12a1c 72 6f 72 20 73 74 61 74 65 20 65 72 72 6f 72 20  ror state error 
12a1d 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
12a1e 20 61 6e 64 20 2a 70 6e 50 61 67 65 20 6c 65 66   and *pnPage lef
12a1f 74 20 75 6e 63 68 61 6e 67 65 64 2e 20 4f 72 2c  t unchanged. Or,
12a20 0a 2a 2a 20 69 66 20 74 68 65 20 66 69 6c 65 20  .** if the file 
12a21 73 79 73 74 65 6d 20 68 61 73 20 74 6f 20 62 65  system has to be
12a22 20 71 75 65 72 69 65 64 20 66 6f 72 20 74 68 65   queried for the
12a23 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c   size of the fil
12a24 65 20 61 6e 64 0a 2a 2a 20 74 68 65 20 71 75 65  e and.** the que
12a25 72 79 20 61 74 74 65 6d 70 74 20 72 65 74 75 72  ry attempt retur
12a26 6e 73 20 61 6e 20 49 4f 20 65 72 72 6f 72 2c 20  ns an IO error, 
12a27 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64  the IO error cod
12a28 65 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a  e is returned.**
12a29 20 61 6e 64 20 2a 70 6e 50 61 67 65 20 69 73 20   and *pnPage is 
12a2a 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a  left unchanged..
12a2b 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  **.** Otherwise,
12a2c 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69   if everything i
12a2d 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68  s successful, th
12a2e 65 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  en SQLITE_OK is 
12a2f 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20  returned.** and 
12a30 2a 70 6e 50 61 67 65 20 69 73 20 73 65 74 20 74  *pnPage is set t
12a31 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  o the number of 
12a32 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74  pages in the dat
12a33 61 62 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  abase..*/.SQLITE
12a34 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
12a35 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75  ite3PagerPagecou
12a36 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  nt(Pager *pPager
12a37 2c 20 69 6e 74 20 2a 70 6e 50 61 67 65 29 7b 0a  , int *pnPage){.
12a38 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 20 20 20    Pgno nPage;   
12a39 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
12a3a 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 76  alue to return v
12a3b 69 61 20 2a 70 6e 50 61 67 65 20 2a 2f 0a 0a 20  ia *pnPage */.. 
12a3c 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 72   /* If the pager
12a3d 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74   is already in t
12a3e 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20  he error state, 
12a3f 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72  return the error
12a40 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 69 66 28 20   code. */.  if( 
12a41 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20  pPager->errCode 
12a42 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50  ){.    return pP
12a43 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20  ager->errCode;. 
12a44 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 65 72 6d 69   }..  /* Determi
12a45 6e 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ne the number of
12a46 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 66 69   pages in the fi
12a47 6c 65 2e 20 53 74 6f 72 65 20 74 68 69 73 20 69  le. Store this i
12a48 6e 20 6e 50 61 67 65 2e 20 2a 2f 0a 20 20 69 66  n nPage. */.  if
12a49 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  ( pPager->dbSize
12a4a 56 61 6c 69 64 20 29 7b 0a 20 20 20 20 6e 50 61  Valid ){.    nPa
12a4b 67 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53  ge = pPager->dbS
12a4c 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ize;.  }else{.  
12a4d 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
12a4e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72            /* Err
12a4f 6f 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 4f  or returned by O
12a50 73 46 69 6c 65 53 69 7a 65 28 29 20 2a 2f 0a 20  sFileSize() */. 
12a51 20 20 20 69 36 34 20 6e 20 3d 20 30 3b 20 20 20     i64 n = 0;   
12a52 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
12a53 6c 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73  le size in bytes
12a54 20 72 65 74 75 72 6e 65 64 20 62 79 20 4f 73 46   returned by OsF
12a55 69 6c 65 53 69 7a 65 28 29 20 2a 2f 0a 0a 20 20  ileSize() */..  
12a56 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e    assert( isOpen
12a57 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20  (pPager->fd) || 
12a58 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65  pPager->tempFile
12a59 20 29 3b 0a 20 20 20 20 69 66 28 20 69 73 4f 70   );.    if( isOp
12a5a 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 26  en(pPager->fd) &
12a5b 26 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71  & (0 != (rc = sq
12a5c 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
12a5d 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 6e 29 29  pPager->fd, &n))
12a5e 29 20 29 7b 0a 20 20 20 20 20 20 70 61 67 65 72  ) ){.      pager
12a5f 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72  _error(pPager, r
12a60 63 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  c);.      return
12a61 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69   rc;.    }.    i
12a62 66 28 20 6e 3e 30 20 26 26 20 6e 3c 70 50 61 67  f( n>0 && n<pPag
12a63 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a  er->pageSize ){.
12a64 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 31 3b        nPage = 1;
12a65 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
12a66 20 20 6e 50 61 67 65 20 3d 20 28 50 67 6e 6f 29    nPage = (Pgno)
12a67 28 6e 20 2f 20 70 50 61 67 65 72 2d 3e 70 61 67  (n / pPager->pag
12a68 65 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20  eSize);.    }.  
12a69 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74    if( pPager->st
12a6a 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f 43  ate!=PAGER_UNLOC
12a6b 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65  K ){.      pPage
12a6c 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6e 50 61 67  r->dbSize = nPag
12a6d 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  e;.      pPager-
12a6e 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d 20 6e 50  >dbFileSize = nP
12a6f 61 67 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65  age;.      pPage
12a70 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d  r->dbSizeValid =
12a71 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   1;.    }.  }.. 
12a72 20 2f 2a 20 49 66 20 74 68 65 20 63 75 72 72 65   /* If the curre
12a73 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  nt number of pag
12a74 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 20 69  es in the file i
12a75 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74  s greater than t
12a76 68 65 20 0a 20 20 2a 2a 20 63 6f 6e 66 69 67 75  he .  ** configu
12a77 72 65 64 20 6d 61 78 69 6d 75 6d 20 70 61 67 65  red maximum page
12a78 72 20 6e 75 6d 62 65 72 2c 20 69 6e 63 72 65 61  r number, increa
12a79 73 65 20 74 68 65 20 61 6c 6c 6f 77 65 64 20 6c  se the allowed l
12a7a 69 6d 69 74 20 73 6f 0a 20 20 2a 2a 20 74 68 61  imit so.  ** tha
12a7b 74 20 74 68 65 20 66 69 6c 65 20 63 61 6e 20 62  t the file can b
12a7c 65 20 72 65 61 64 2e 0a 20 20 2a 2f 0a 20 20 69  e read..  */.  i
12a7d 66 28 20 6e 50 61 67 65 3e 70 50 61 67 65 72 2d  f( nPage>pPager-
12a7e 3e 6d 78 50 67 6e 6f 20 29 7b 0a 20 20 20 20 70  >mxPgno ){.    p
12a7f 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20  Pager->mxPgno = 
12a80 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0a 20 20 7d  (Pgno)nPage;.  }
12a81 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6f  ..  /* Set the o
12a82 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 61  utput variable a
12a83 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  nd return SQLITE
12a84 5f 4f 4b 20 2a 2f 0a 20 20 69 66 28 20 70 6e 50  _OK */.  if( pnP
12a85 61 67 65 20 29 7b 0a 20 20 20 20 2a 70 6e 50 61  age ){.    *pnPa
12a86 67 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 7d 0a  ge = nPage;.  }.
12a87 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
12a88 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 72  OK;.}.../*.** Tr
12a89 79 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 6c 6f  y to obtain a lo
12a8a 63 6b 20 6f 66 20 74 79 70 65 20 6c 6f 63 6b 74  ck of type lockt
12a8b 79 70 65 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ype on the datab
12a8c 61 73 65 20 66 69 6c 65 2e 20 49 66 0a 2a 2a 20  ase file. If.** 
12a8d 61 20 73 69 6d 69 6c 61 72 20 6f 72 20 67 72 65  a similar or gre
12a8e 61 74 65 72 20 6c 6f 63 6b 20 69 73 20 61 6c 72  ater lock is alr
12a8f 65 61 64 79 20 68 65 6c 64 2c 20 74 68 69 73 20  eady held, this 
12a90 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f  function is a no
12a91 2d 6f 70 0a 2a 2a 20 28 72 65 74 75 72 6e 69 6e  -op.** (returnin
12a92 67 20 53 51 4c 49 54 45 5f 4f 4b 20 69 6d 6d 65  g SQLITE_OK imme
12a93 64 69 61 74 65 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20  diately)..**.** 
12a94 4f 74 68 65 72 77 69 73 65 2c 20 61 74 74 65 6d  Otherwise, attem
12a95 70 74 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65  pt to obtain the
12a96 20 6c 6f 63 6b 20 75 73 69 6e 67 20 73 71 6c 69   lock using sqli
12a97 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 49 6e 76  te3OsLock(). Inv
12a98 6f 6b 65 20 0a 2a 2a 20 74 68 65 20 62 75 73 79  oke .** the busy
12a99 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 74 68 65   callback if the
12a9a 20 6c 6f 63 6b 20 69 73 20 63 75 72 72 65 6e 74   lock is current
12a9b 6c 79 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65  ly not available
12a9c 2e 20 52 65 70 65 61 74 20 0a 2a 2a 20 75 6e 74  . Repeat .** unt
12a9d 69 6c 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c  il the busy call
12a9e 62 61 63 6b 20 72 65 74 75 72 6e 73 20 66 61 6c  back returns fal
12a9f 73 65 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 20  se or until the 
12aa0 61 74 74 65 6d 70 74 20 74 6f 20 0a 2a 2a 20 6f  attempt to .** o
12aa1 62 74 61 69 6e 20 74 68 65 20 6c 6f 63 6b 20 73  btain the lock s
12aa2 75 63 63 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 52  ucceeds..**.** R
12aa3 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
12aa4 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 61  on success and a
12aa5 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20  n error code if 
12aa6 77 65 20 63 61 6e 6e 6f 74 20 6f 62 74 61 69 6e  we cannot obtain
12aa7 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e 20 49 66  .** the lock. If
12aa8 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 6f 62 74   the lock is obt
12aa9 61 69 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c  ained successful
12aaa 6c 79 2c 20 73 65 74 20 74 68 65 20 50 61 67 65  ly, set the Page
12aab 72 2e 73 74 61 74 65 20 0a 2a 2a 20 76 61 72 69  r.state .** vari
12aac 61 62 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65  able to locktype
12aad 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
12aae 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  g..*/.static int
12aaf 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c   pager_wait_on_l
12ab0 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65  ock(Pager *pPage
12ab1 72 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29  r, int locktype)
12ab2 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  {.  int rc;     
12ab3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12ab4 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
12ab5 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a  rn code */..  /*
12ab6 20 54 68 65 20 4f 53 20 6c 6f 63 6b 20 76 61 6c   The OS lock val
12ab7 75 65 73 20 6d 75 73 74 20 62 65 20 74 68 65 20  ues must be the 
12ab8 73 61 6d 65 20 61 73 20 74 68 65 20 50 61 67 65  same as the Page
12ab9 72 20 6c 6f 63 6b 20 76 61 6c 75 65 73 20 2a 2f  r lock values */
12aba 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52  .  assert( PAGER
12abb 5f 53 48 41 52 45 44 3d 3d 53 48 41 52 45 44 5f  _SHARED==SHARED_
12abc 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  LOCK );.  assert
12abd 28 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44  ( PAGER_RESERVED
12abe 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  ==RESERVED_LOCK 
12abf 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47  );.  assert( PAG
12ac0 45 52 5f 45 58 43 4c 55 53 49 56 45 3d 3d 45 58  ER_EXCLUSIVE==EX
12ac1 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a  CLUSIVE_LOCK );.
12ac2 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c  .  /* If the fil
12ac3 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 75  e is currently u
12ac4 6e 6c 6f 63 6b 65 64 20 74 68 65 6e 20 74 68 65  nlocked then the
12ac5 20 73 69 7a 65 20 6d 75 73 74 20 62 65 20 75 6e   size must be un
12ac6 6b 6e 6f 77 6e 20 2a 2f 0a 20 20 61 73 73 65 72  known */.  asser
12ac7 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  t( pPager->state
12ac8 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 7c  >=PAGER_SHARED |
12ac9 7c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  | pPager->dbSize
12aca 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 0a 20 20 2f  Valid==0 );..  /
12acb 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 69  * Check that thi
12acc 73 20 69 73 20 65 69 74 68 65 72 20 61 20 6e 6f  s is either a no
12acd 2d 6f 70 20 28 62 65 63 61 75 73 65 20 74 68 65  -op (because the
12ace 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 20   requested lock 
12acf 69 73 20 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79  is .  ** already
12ad0 20 68 65 6c 64 2c 20 6f 72 20 6f 6e 65 20 6f 66   held, or one of
12ad1 20 74 68 65 20 74 72 61 6e 73 69 73 74 69 6f 6e   the transistion
12ad2 73 20 74 68 61 74 20 74 68 65 20 62 75 73 79 2d  s that the busy-
12ad3 68 61 6e 64 6c 65 72 0a 20 20 2a 2a 20 6d 61 79  handler.  ** may
12ad4 20 62 65 20 69 6e 76 6f 6b 65 64 20 64 75 72 69   be invoked duri
12ad5 6e 67 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  ng, according to
12ad6 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 61 62 6f   the comment abo
12ad7 76 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 50  ve.  ** sqlite3P
12ad8 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c  agerSetBusyhandl
12ad9 65 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  er()..  */.  ass
12ada 65 72 74 28 20 28 70 50 61 67 65 72 2d 3e 73 74  ert( (pPager->st
12adb 61 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 29 0a 20  ate>=locktype). 
12adc 20 20 20 20 20 20 7c 7c 20 28 70 50 61 67 65 72        || (pPager
12add 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55  ->state==PAGER_U
12ade 4e 4c 4f 43 4b 20 26 26 20 6c 6f 63 6b 74 79 70  NLOCK && locktyp
12adf 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 29  e==PAGER_SHARED)
12ae0 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 50 61 67  .       || (pPag
12ae1 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52  er->state==PAGER
12ae2 5f 52 45 53 45 52 56 45 44 20 26 26 20 6c 6f 63  _RESERVED && loc
12ae3 6b 74 79 70 65 3d 3d 50 41 47 45 52 5f 45 58 43  ktype==PAGER_EXC
12ae4 4c 55 53 49 56 45 29 0a 20 20 29 3b 0a 0a 20 20  LUSIVE).  );..  
12ae5 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  if( pPager->stat
12ae6 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20  e>=locktype ){. 
12ae7 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
12ae8 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  K;.  }else{.    
12ae9 64 6f 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  do {.      rc = 
12aea 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50  sqlite3OsLock(pP
12aeb 61 67 65 72 2d 3e 66 64 2c 20 6c 6f 63 6b 74 79  ager->fd, lockty
12aec 70 65 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28  pe);.    }while(
12aed 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59   rc==SQLITE_BUSY
12aee 20 26 26 20 70 50 61 67 65 72 2d 3e 78 42 75 73   && pPager->xBus
12aef 79 48 61 6e 64 6c 65 72 28 70 50 61 67 65 72 2d  yHandler(pPager-
12af0 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67  >pBusyHandlerArg
12af1 29 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  ) );.    if( rc=
12af2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
12af3 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74      pPager->stat
12af4 65 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79 70 65  e = (u8)locktype
12af5 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45 28  ;.      IOTRACE(
12af6 28 22 4c 4f 43 4b 20 25 70 20 25 64 5c 6e 22 2c  ("LOCK %p %d\n",
12af7 20 70 50 61 67 65 72 2c 20 6c 6f 63 6b 74 79 70   pPager, locktyp
12af8 65 29 29 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e)).    }.  }.  
12af9 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
12afa 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 61 73 73  .** Function ass
12afb 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74  ertTruncateConst
12afc 72 61 69 6e 74 28 70 50 61 67 65 72 29 20 63 68  raint(pPager) ch
12afd 65 63 6b 73 20 74 68 61 74 20 6f 6e 65 20 6f 66  ecks that one of
12afe 20 74 68 65 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 69   the .** followi
12aff 6e 67 20 69 73 20 74 72 75 65 20 66 6f 72 20 61  ng is true for a
12b00 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 63  ll dirty pages c
12b01 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20  urrently in the 
12b02 70 61 67 65 2d 63 61 63 68 65 3a 0a 2a 2a 0a 2a  page-cache:.**.*
12b03 2a 20 20 20 61 29 20 54 68 65 20 70 61 67 65 20  *   a) The page 
12b04 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73 20 74  number is less t
12b05 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20  han or equal to 
12b06 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
12b07 0a 2a 2a 20 20 20 20 20 20 63 75 72 72 65 6e 74  .**      current
12b08 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 2c   database image,
12b09 20 69 6e 20 70 61 67 65 73 2c 20 4f 52 0a 2a 2a   in pages, OR.**
12b0a 0a 2a 2a 20 20 20 62 29 20 69 66 20 74 68 65 20  .**   b) if the 
12b0b 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 77 65 72  page content wer
12b0c 65 20 77 72 69 74 74 65 6e 20 61 74 20 74 68 69  e written at thi
12b0d 73 20 74 69 6d 65 2c 20 69 74 20 77 6f 75 6c 64  s time, it would
12b0e 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 62 65 20   not.**      be 
12b0f 6e 65 63 65 73 73 61 72 79 20 74 6f 20 77 72 69  necessary to wri
12b10 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63  te the current c
12b11 6f 6e 74 65 6e 74 20 6f 75 74 20 74 6f 20 74 68  ontent out to th
12b12 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a  e sub-journal.**
12b13 20 20 20 20 20 20 28 61 73 20 64 65 74 65 72 6d        (as determ
12b14 69 6e 65 64 20 62 79 20 66 75 6e 63 74 69 6f 6e  ined by function
12b15 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67   subjRequiresPag
12b16 65 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  e())..**.** If t
12b17 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 61 73 73  he condition ass
12b18 65 72 74 65 64 20 62 79 20 74 68 69 73 20 66 75  erted by this fu
12b19 6e 63 74 69 6f 6e 20 77 65 72 65 20 6e 6f 74 20  nction were not 
12b1a 74 72 75 65 2c 20 61 6e 64 20 74 68 65 0a 2a 2a  true, and the.**
12b1b 20 64 69 72 74 79 20 70 61 67 65 20 77 65 72 65   dirty page were
12b1c 20 74 6f 20 62 65 20 64 69 73 63 61 72 64 65 64   to be discarded
12b1d 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20   from the cache 
12b1e 76 69 61 20 74 68 65 20 70 61 67 65 72 53 74 72  via the pagerStr
12b1f 65 73 73 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65  ess().** routine
12b20 2c 20 70 61 67 65 72 53 74 72 65 73 73 28 29 20  , pagerStress() 
12b21 77 6f 75 6c 64 20 6e 6f 74 20 77 72 69 74 65 20  would not write 
12b22 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65  the current page
12b23 20 63 6f 6e 74 65 6e 74 20 74 6f 0a 2a 2a 20 74   content to.** t
12b24 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12b25 2e 20 49 66 20 61 20 73 61 76 65 70 6f 69 6e 74  . If a savepoint
12b26 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 65 72   transaction wer
12b27 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 66  e rolled back af
12b28 74 65 72 0a 2a 2a 20 74 68 69 73 20 68 61 70 70  ter.** this happ
12b29 65 6e 65 64 2c 20 74 68 65 20 63 6f 72 72 65 63  ened, the correc
12b2a 74 20 62 65 68 61 76 69 6f 75 72 20 77 6f 75 6c  t behaviour woul
12b2b 64 20 62 65 20 74 6f 20 72 65 73 74 6f 72 65 20  d be to restore 
12b2c 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 63  the current.** c
12b2d 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61  ontent of the pa
12b2e 67 65 2e 20 48 6f 77 65 76 65 72 2c 20 73 69 6e  ge. However, sin
12b2f 63 65 20 74 68 69 73 20 63 6f 6e 74 65 6e 74 20  ce this content 
12b30 69 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 69  is not present i
12b31 6e 20 65 69 74 68 65 72 0a 2a 2a 20 74 68 65 20  n either.** the 
12b32 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 72  database file or
12b33 20 74 68 65 20 70 6f 72 74 69 6f 6e 20 6f 66 20   the portion of 
12b34 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  the rollback jou
12b35 72 6e 61 6c 20 61 6e 64 20 0a 2a 2a 20 73 75 62  rnal and .** sub
12b36 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 65 64 20  -journal rolled 
12b37 62 61 63 6b 20 74 68 65 20 63 6f 6e 74 65 6e 74  back the content
12b38 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 72 65   could not be re
12b39 73 74 6f 72 65 64 20 61 6e 64 20 74 68 65 0a 2a  stored and the.*
12b3a 2a 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65  * database image
12b3b 20 77 6f 75 6c 64 20 62 65 63 6f 6d 65 20 63 6f   would become co
12b3c 72 72 75 70 74 2e 20 49 74 20 69 73 20 74 68 65  rrupt. It is the
12b3d 72 65 66 6f 72 65 20 66 6f 72 74 75 6e 61 74 65  refore fortunate
12b3e 20 74 68 61 74 20 0a 2a 2a 20 74 68 69 73 20 63   that .** this c
12b3f 69 72 63 75 6d 73 74 61 6e 63 65 20 63 61 6e 6e  ircumstance cann
12b40 6f 74 20 61 72 69 73 65 2e 0a 2a 2f 0a 23 69 66  ot arise..*/.#if
12b41 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
12b42 44 45 42 55 47 29 0a 73 74 61 74 69 63 20 76 6f  DEBUG).static vo
12b43 69 64 20 61 73 73 65 72 74 54 72 75 6e 63 61 74  id assertTruncat
12b44 65 43 6f 6e 73 74 72 61 69 6e 74 43 62 28 50 67  eConstraintCb(Pg
12b45 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 61 73 73  Hdr *pPg){.  ass
12b46 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67 73 26  ert( pPg->flags&
12b47 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b 0a 20  PGHDR_DIRTY );. 
12b48 20 61 73 73 65 72 74 28 20 21 73 75 62 6a 52 65   assert( !subjRe
12b49 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 20  quiresPage(pPg) 
12b4a 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50  || pPg->pgno<=pP
12b4b 67 2d 3e 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  g->pPager->dbSiz
12b4c 65 20 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  e );.}.static vo
12b4d 69 64 20 61 73 73 65 72 74 54 72 75 6e 63 61 74  id assertTruncat
12b4e 65 43 6f 6e 73 74 72 61 69 6e 74 28 50 61 67 65  eConstraint(Page
12b4f 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 73 71  r *pPager){.  sq
12b50 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 72 61  lite3PcacheItera
12b51 74 65 44 69 72 74 79 28 70 50 61 67 65 72 2d 3e  teDirty(pPager->
12b52 70 50 43 61 63 68 65 2c 20 61 73 73 65 72 74 54  pPCache, assertT
12b53 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e  runcateConstrain
12b54 74 43 62 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20  tCb);.}.#else.# 
12b55 64 65 66 69 6e 65 20 61 73 73 65 72 74 54 72 75  define assertTru
12b56 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 28  ncateConstraint(
12b57 70 50 61 67 65 72 29 0a 23 65 6e 64 69 66 0a 0a  pPager).#endif..
12b58 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 74  /*.** Truncate t
12b59 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  he in-memory dat
12b5a 61 62 61 73 65 20 66 69 6c 65 20 69 6d 61 67 65  abase file image
12b5b 20 74 6f 20 6e 50 61 67 65 20 70 61 67 65 73 2e   to nPage pages.
12b5c 20 54 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69   This .** functi
12b5d 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 61 63 74 75  on does not actu
12b5e 61 6c 6c 79 20 6d 6f 64 69 66 79 20 74 68 65 20  ally modify the 
12b5f 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 6e  database file on
12b60 20 64 69 73 6b 2e 20 49 74 20 0a 2a 2a 20 6a 75   disk. It .** ju
12b61 73 74 20 73 65 74 73 20 74 68 65 20 69 6e 74 65  st sets the inte
12b62 72 6e 61 6c 20 73 74 61 74 65 20 6f 66 20 74 68  rnal state of th
12b63 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20 73  e pager object s
12b64 6f 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 74  o that the .** t
12b65 72 75 6e 63 61 74 69 6f 6e 20 77 69 6c 6c 20 62  runcation will b
12b66 65 20 64 6f 6e 65 20 77 68 65 6e 20 74 68 65 20  e done when the 
12b67 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
12b68 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64  ion is committed
12b69 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
12b6a 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
12b6b 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61  PagerTruncateIma
12b6c 67 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ge(Pager *pPager
12b6d 2c 20 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20  , Pgno nPage){. 
12b6e 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
12b6f 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 29 3b 0a  >dbSizeValid );.
12b70 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
12b71 2d 3e 64 62 53 69 7a 65 3e 3d 6e 50 61 67 65 20  ->dbSize>=nPage 
12b72 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
12b73 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45  ger->state>=PAGE
12b74 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20  R_RESERVED );.  
12b75 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d  pPager->dbSize =
12b76 20 6e 50 61 67 65 3b 0a 20 20 61 73 73 65 72 74   nPage;.  assert
12b77 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69  TruncateConstrai
12b78 6e 74 28 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f  nt(pPager);.}../
12b79 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20 74 68  *.** Shutdown th
12b7a 65 20 70 61 67 65 20 63 61 63 68 65 2e 20 20 46  e page cache.  F
12b7b 72 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61  ree all memory a
12b7c 6e 64 20 63 6c 6f 73 65 20 61 6c 6c 20 66 69 6c  nd close all fil
12b7d 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74  es..**.** If a t
12b7e 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 69  ransaction was i
12b7f 6e 20 70 72 6f 67 72 65 73 73 20 77 68 65 6e 20  n progress when 
12b80 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
12b81 63 61 6c 6c 65 64 2c 20 74 68 61 74 0a 2a 2a 20  called, that.** 
12b82 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72  transaction is r
12b83 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 41 6c 6c  olled back.  All
12b84 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67   outstanding pag
12b85 65 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61 74  es are invalidat
12b86 65 64 0a 2a 2a 20 61 6e 64 20 74 68 65 69 72 20  ed.** and their 
12b87 6d 65 6d 6f 72 79 20 69 73 20 66 72 65 65 64 2e  memory is freed.
12b88 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f    Any attempt to
12b89 20 75 73 65 20 61 20 70 61 67 65 20 61 73 73 6f   use a page asso
12b8a 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74  ciated.** with t
12b8b 68 69 73 20 70 61 67 65 20 63 61 63 68 65 20 61  his page cache a
12b8c 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 69  fter this functi
12b8d 6f 6e 20 72 65 74 75 72 6e 73 20 77 69 6c 6c 20  on returns will 
12b8e 6c 69 6b 65 6c 79 0a 2a 2a 20 72 65 73 75 6c 74  likely.** result
12b8f 20 69 6e 20 61 20 63 6f 72 65 64 75 6d 70 2e 0a   in a coredump..
12b90 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
12b91 69 6f 6e 20 61 6c 77 61 79 73 20 73 75 63 63 65  ion always succe
12b92 65 64 73 2e 20 49 66 20 61 20 74 72 61 6e 73 61  eds. If a transa
12b93 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20  ction is active 
12b94 61 6e 20 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73  an attempt.** is
12b95 20 6d 61 64 65 20 74 6f 20 72 6f 6c 6c 20 69 74   made to roll it
12b96 20 62 61 63 6b 2e 20 49 66 20 61 6e 20 65 72 72   back. If an err
12b97 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67  or occurs during
12b98 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 0a 2a   the rollback .*
12b99 2a 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20  * a hot journal 
12b9a 6d 61 79 20 62 65 20 6c 65 66 74 20 69 6e 20 74  may be left in t
12b9b 68 65 20 66 69 6c 65 73 79 73 74 65 6d 20 62 75  he filesystem bu
12b9c 74 20 6e 6f 20 65 72 72 6f 72 20 69 73 20 72 65  t no error is re
12b9d 74 75 72 6e 65 64 0a 2a 2a 20 74 6f 20 74 68 65  turned.** to the
12b9e 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49   caller..*/.SQLI
12b9f 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
12ba0 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65  qlite3PagerClose
12ba1 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
12ba2 0a 20 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c  .  disable_simul
12ba3 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29  ated_io_errors()
12ba4 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e  ;.  sqlite3Begin
12ba5 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
12ba6 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64    pPager->errCod
12ba7 65 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d  e = 0;.  pPager-
12ba8 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d  >exclusiveMode =
12ba9 20 30 3b 0a 20 20 70 61 67 65 72 5f 72 65 73 65   0;.  pager_rese
12baa 74 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28  t(pPager);.  if(
12bab 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 61   MEMDB ){.    pa
12bac 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65  ger_unlock(pPage
12bad 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  r);.  }else{.   
12bae 20 2f 2a 20 53 65 74 20 50 61 67 65 72 2e 6a 6f   /* Set Pager.jo
12baf 75 72 6e 61 6c 48 64 72 20 74 6f 20 2d 31 20 66  urnalHdr to -1 f
12bb0 6f 72 20 74 68 65 20 62 65 6e 65 66 69 74 20 6f  or the benefit o
12bb1 66 20 74 68 65 20 70 61 67 65 72 5f 70 6c 61 79  f the pager_play
12bb2 62 61 63 6b 28 29 20 0a 20 20 20 20 2a 2a 20 63  back() .    ** c
12bb3 61 6c 6c 20 77 68 69 63 68 20 6d 61 79 20 62 65  all which may be
12bb4 20 6d 61 64 65 20 66 72 6f 6d 20 77 69 74 68 69   made from withi
12bb5 6e 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64  n pagerUnlockAnd
12bb6 52 6f 6c 6c 62 61 63 6b 28 29 2e 20 49 66 20 69  Rollback(). If i
12bb7 74 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20  t.    ** is not 
12bb8 2d 31 2c 20 74 68 65 6e 20 74 68 65 20 75 6e 73  -1, then the uns
12bb9 79 6e 63 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66  ynced portion of
12bba 20 61 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c   an open journal
12bbb 20 66 69 6c 65 20 6d 61 79 0a 20 20 20 20 2a 2a   file may.    **
12bbc 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b 20   be played back 
12bbd 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  into the databas
12bbe 65 2e 20 49 66 20 61 20 70 6f 77 65 72 20 66 61  e. If a power fa
12bbf 69 6c 75 72 65 20 6f 63 63 75 72 73 20 77 68 69  ilure occurs whi
12bc0 6c 65 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 69  le.    ** this i
12bc1 73 20 68 61 70 70 65 6e 69 6e 67 2c 20 74 68 65  s happening, the
12bc2 20 64 61 74 61 62 61 73 65 20 6d 61 79 20 62 65   database may be
12bc3 63 6f 6d 65 20 63 6f 72 72 75 70 74 2e 0a 20 20  come corrupt..  
12bc4 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d    */.    pPager-
12bc5 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 2d 31  >journalHdr = -1
12bc6 3b 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63  ;.    pagerUnloc
12bc7 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 70 50 61  kAndRollback(pPa
12bc8 67 65 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  ger);.  }.  sqli
12bc9 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
12bca 6f 63 28 29 3b 0a 20 20 65 6e 61 62 6c 65 5f 73  oc();.  enable_s
12bcb 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f  imulated_io_erro
12bcc 72 73 28 29 3b 0a 20 20 50 41 47 45 52 54 52 41  rs();.  PAGERTRA
12bcd 43 45 28 28 22 43 4c 4f 53 45 20 25 64 5c 6e 22  CE(("CLOSE %d\n"
12bce 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72  , PAGERID(pPager
12bcf 29 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28  )));.  IOTRACE((
12bd0 22 43 4c 4f 53 45 20 25 70 5c 6e 22 2c 20 70 50  "CLOSE %p\n", pP
12bd1 61 67 65 72 29 29 0a 20 20 73 71 6c 69 74 65 33  ager)).  sqlite3
12bd2 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e  OsClose(pPager->
12bd3 66 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61  fd);.  sqlite3Pa
12bd4 67 65 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70  geFree(pPager->p
12bd5 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 73 71 6c  TmpSpace);.  sql
12bd6 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28  ite3PcacheClose(
12bd7 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29  pPager->pPCache)
12bd8 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
12bd9 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 69 66 28  _HAS_CODEC.  if(
12bda 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 46   pPager->xCodecF
12bdb 72 65 65 20 29 20 70 50 61 67 65 72 2d 3e 78 43  ree ) pPager->xC
12bdc 6f 64 65 63 46 72 65 65 28 70 50 61 67 65 72 2d  odecFree(pPager-
12bdd 3e 70 43 6f 64 65 63 29 3b 0a 23 65 6e 64 69 66  >pCodec);.#endif
12bde 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 61  ..  assert( !pPa
12bdf 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20  ger->aSavepoint 
12be0 26 26 20 21 70 50 61 67 65 72 2d 3e 70 49 6e 4a  && !pPager->pInJ
12be1 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65  ournal );.  asse
12be2 72 74 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67  rt( !isOpen(pPag
12be3 65 72 2d 3e 6a 66 64 29 20 26 26 20 21 69 73 4f  er->jfd) && !isO
12be4 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64  pen(pPager->sjfd
12be5 29 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f  ) );..  sqlite3_
12be6 66 72 65 65 28 70 50 61 67 65 72 29 3b 0a 20 20  free(pPager);.  
12be7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
12be8 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65  ;.}..#if !define
12be9 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66  d(NDEBUG) || def
12bea 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
12beb 29 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  )./*.** Return t
12bec 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66  he page number f
12bed 6f 72 20 70 61 67 65 20 70 50 67 2e 0a 2a 2f 0a  or page pPg..*/.
12bee 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50  SQLITE_PRIVATE P
12bef 67 6e 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72  gno sqlite3Pager
12bf0 50 61 67 65 6e 75 6d 62 65 72 28 44 62 50 61 67  Pagenumber(DbPag
12bf1 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 72  e *pPg){.  retur
12bf2 6e 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 7d 0a 23  n pPg->pgno;.}.#
12bf3 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63  endif../*.** Inc
12bf4 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72  rement the refer
12bf5 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72 20 70  ence count for p
12bf6 61 67 65 20 70 50 67 2e 0a 2a 2f 0a 53 51 4c 49  age pPg..*/.SQLI
12bf7 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
12bf8 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28  sqlite3PagerRef(
12bf9 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20  DbPage *pPg){.  
12bfa 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66  sqlite3PcacheRef
12bfb 28 70 50 67 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  (pPg);.}../*.** 
12bfc 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c  Sync the journal
12bfd 2e 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  . In other words
12bfe 2c 20 6d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20  , make sure all 
12bff 74 68 65 20 70 61 67 65 73 20 74 68 61 74 20 68  the pages that h
12c00 61 76 65 0a 2a 2a 20 62 65 65 6e 20 77 72 69 74  ave.** been writ
12c01 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ten to the journ
12c02 61 6c 20 68 61 76 65 20 61 63 74 75 61 6c 6c 79  al have actually
12c03 20 72 65 61 63 68 65 64 20 74 68 65 20 73 75 72   reached the sur
12c04 66 61 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64  face of the.** d
12c05 69 73 6b 20 61 6e 64 20 63 61 6e 20 62 65 20 72  isk and can be r
12c06 65 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 65  estored in the e
12c07 76 65 6e 74 20 6f 66 20 61 20 68 6f 74 2d 6a 6f  vent of a hot-jo
12c08 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2e 0a  urnal rollback..
12c09 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 61 67  **.** If the Pag
12c0a 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67  er.needSync flag
12c0b 20 69 73 20 6e 6f 74 20 73 65 74 2c 20 74 68 65   is not set, the
12c0c 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
12c0d 69 73 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 2e 20 4f  is a.** no-op. O
12c0e 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 61 63  therwise, the ac
12c0f 74 69 6f 6e 73 20 72 65 71 75 69 72 65 64 20 64  tions required d
12c10 65 70 65 6e 64 20 6f 6e 20 74 68 65 20 6a 6f 75  epend on the jou
12c11 72 6e 61 6c 2d 6d 6f 64 65 0a 2a 2a 20 61 6e 64  rnal-mode.** and
12c12 20 74 68 65 20 64 65 76 69 63 65 20 63 68 61 72   the device char
12c13 61 63 74 65 72 69 73 74 69 63 73 20 6f 66 20 74  acteristics of t
12c14 68 65 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74  he the file-syst
12c15 65 6d 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  em, as follows:.
12c16 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65  **.**   * If the
12c17 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
12c18 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f   an in-memory jo
12c19 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6e 6f 20 61  urnal file, no a
12c1a 63 74 69 6f 6e 20 6e 65 65 64 0a 2a 2a 20 20 20  ction need.**   
12c1b 20 20 62 65 20 74 61 6b 65 6e 2e 0a 2a 2a 0a 2a    be taken..**.*
12c1c 2a 20 20 20 2a 20 4f 74 68 65 72 77 69 73 65 2c  *   * Otherwise,
12c1d 20 69 66 20 74 68 65 20 64 65 76 69 63 65 20 64   if the device d
12c1e 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20  oes not support 
12c1f 74 68 65 20 53 41 46 45 5f 41 50 50 45 4e 44 20  the SAFE_APPEND 
12c20 70 72 6f 70 65 72 74 79 2c 0a 2a 2a 20 20 20 20  property,.**    
12c21 20 74 68 65 6e 20 74 68 65 20 6e 52 65 63 20 66   then the nRec f
12c22 69 65 6c 64 20 6f 66 20 74 68 65 20 6d 6f 73 74  ield of the most
12c23 20 72 65 63 65 6e 74 6c 79 20 77 72 69 74 74 65   recently writte
12c24 6e 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  n journal header
12c25 0a 2a 2a 20 20 20 20 20 69 73 20 75 70 64 61 74  .**     is updat
12c26 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68  ed to contain th
12c27 65 20 6e 75 6d 62 65 72 20 6f 66 20 6a 6f 75 72  e number of jour
12c28 6e 61 6c 20 72 65 63 6f 72 64 73 20 74 68 61 74  nal records that
12c29 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 62 65 65   have.**     bee
12c2a 6e 20 77 72 69 74 74 65 6e 20 66 6f 6c 6c 6f 77  n written follow
12c2b 69 6e 67 20 69 74 2e 20 49 66 20 74 68 65 20 70  ing it. If the p
12c2c 61 67 65 72 20 69 73 20 6f 70 65 72 61 74 69 6e  ager is operatin
12c2d 67 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 0a 2a  g in full-sync.*
12c2e 2a 20 20 20 20 20 6d 6f 64 65 2c 20 74 68 65 6e  *     mode, then
12c2f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
12c30 65 20 69 73 20 73 79 6e 63 65 64 20 62 65 66 6f  e is synced befo
12c31 72 65 20 74 68 69 73 20 66 69 65 6c 64 20 69 73  re this field is
12c32 20 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20   updated..**.** 
12c33 20 20 2a 20 49 66 20 74 68 65 20 64 65 76 69 63    * If the devic
12c34 65 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f  e does not suppo
12c35 72 74 20 74 68 65 20 53 45 51 55 45 4e 54 49 41  rt the SEQUENTIA
12c36 4c 20 70 72 6f 70 65 72 74 79 2c 20 74 68 65 6e  L property, then
12c37 20 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61 6c   .**     journal
12c38 20 66 69 6c 65 20 69 73 20 73 79 6e 63 65 64 2e   file is synced.
12c39 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 6e 20 70 73  .**.** Or, in ps
12c3a 65 75 64 6f 2d 63 6f 64 65 3a 0a 2a 2a 0a 2a 2a  eudo-code:.**.**
12c3b 20 20 20 69 66 28 20 4e 4f 54 20 3c 69 6e 2d 6d     if( NOT <in-m
12c3c 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 3e 20 29  emory journal> )
12c3d 7b 0a 2a 2a 20 20 20 20 20 69 66 28 20 4e 4f 54  {.**     if( NOT
12c3e 20 53 41 46 45 5f 41 50 50 45 4e 44 20 29 7b 0a   SAFE_APPEND ){.
12c3f 2a 2a 20 20 20 20 20 20 20 69 66 28 20 3c 66 75  **       if( <fu
12c40 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 3e 20 29 20  ll-sync mode> ) 
12c41 78 53 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20 66  xSync(<journal f
12c42 69 6c 65 3e 29 3b 0a 2a 2a 20 20 20 20 20 20 20  ile>);.**       
12c43 3c 75 70 64 61 74 65 20 6e 52 65 63 20 66 69 65  <update nRec fie
12c44 6c 64 3e 0a 2a 2a 20 20 20 20 20 7d 20 0a 2a 2a  ld>.**     } .**
12c45 20 20 20 20 20 69 66 28 20 4e 4f 54 20 53 45 51       if( NOT SEQ
12c46 55 45 4e 54 49 41 4c 20 29 20 78 53 79 6e 63 28  UENTIAL ) xSync(
12c47 3c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 3e 29 3b  <journal file>);
12c48 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 54 68  .**   }.**.** Th
12c49 65 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63  e Pager.needSync
12c4a 20 66 6c 61 67 20 69 73 20 6e 65 76 65 72 20 62   flag is never b
12c4b 65 20 73 65 74 20 66 6f 72 20 74 65 6d 70 6f 72  e set for tempor
12c4c 61 72 79 20 66 69 6c 65 73 2c 20 6f 72 20 61 6e  ary files, or an
12c4d 79 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 72 61 74  y.** file operat
12c4e 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d  ing in no-sync m
12c4f 6f 64 65 20 28 50 61 67 65 72 2e 6e 6f 53 79 6e  ode (Pager.noSyn
12c50 63 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72  c set to non-zer
12c51 6f 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63  o)..**.** If suc
12c52 63 65 73 73 66 75 6c 2c 20 74 68 69 73 20 72 6f  cessful, this ro
12c53 75 74 69 6e 65 20 63 6c 65 61 72 73 20 74 68 65  utine clears the
12c54 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43   PGHDR_NEED_SYNC
12c55 20 66 6c 61 67 20 6f 66 20 65 76 65 72 79 20 0a   flag of every .
12c56 2a 2a 20 70 61 67 65 20 63 75 72 72 65 6e 74 6c  ** page currentl
12c57 79 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79  y held in memory
12c58 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
12c59 67 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20  g SQLITE_OK. If 
12c5a 61 6e 20 49 4f 0a 2a 2a 20 65 72 72 6f 72 20 69  an IO.** error i
12c5b 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74  s encountered, t
12c5c 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72  hen the IO error
12c5d 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
12c5e 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e  d to the caller.
12c5f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
12c60 79 6e 63 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72  yncJournal(Pager
12c61 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28   *pPager){.  if(
12c62 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
12c63 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  c ){.    assert(
12c64 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69   !pPager->tempFi
12c65 6c 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50  le );.    if( pP
12c66 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
12c67 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e!=PAGER_JOURNAL
12c68 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20  MODE_MEMORY ){. 
12c69 20 20 20 20 20 69 6e 74 20 72 63 3b 20 20 20 20       int rc;    
12c6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12c6b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
12c6c 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 20 20  urn code */.    
12c6d 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 44 63 20    const int iDc 
12c6e 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63  = sqlite3OsDevic
12c6f 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73  eCharacteristics
12c70 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20  (pPager->fd);.  
12c71 20 20 20 20 61 73 73 65 72 74 28 20 69 73 4f 70      assert( isOp
12c72 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
12c73 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 30 3d  );..      if( 0=
12c74 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49 4f 43  =(iDc&SQLITE_IOC
12c75 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 29 20  AP_SAFE_APPEND) 
12c76 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
12c77 69 73 20 62 6c 6f 63 6b 20 64 65 61 6c 73 20 77  is block deals w
12c78 69 74 68 20 61 6e 20 6f 62 73 63 75 72 65 20 70  ith an obscure p
12c79 72 6f 62 6c 65 6d 2e 20 49 66 20 74 68 65 20 6c  roblem. If the l
12c7a 61 73 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20  ast connection. 
12c7b 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 77         ** that w
12c7c 72 6f 74 65 20 74 6f 20 74 68 69 73 20 64 61 74  rote to this dat
12c7d 61 62 61 73 65 20 77 61 73 20 6f 70 65 72 61 74  abase was operat
12c7e 69 6e 67 20 69 6e 20 70 65 72 73 69 73 74 65 6e  ing in persisten
12c7f 74 2d 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20  t-journal.      
12c80 20 20 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 6e 20    ** mode, then 
12c81 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
12c82 20 6d 61 79 20 61 74 20 74 68 69 73 20 70 6f 69   may at this poi
12c83 6e 74 20 61 63 74 75 61 6c 6c 79 20 62 65 20 6c  nt actually be l
12c84 61 72 67 65 72 0a 20 20 20 20 20 20 20 20 2a 2a  arger.        **
12c85 20 74 68 61 6e 20 50 61 67 65 72 2e 6a 6f 75 72   than Pager.jour
12c86 6e 61 6c 4f 66 66 20 62 79 74 65 73 2e 20 49 66  nalOff bytes. If
12c87 20 74 68 65 20 6e 65 78 74 20 74 68 69 6e 67 20   the next thing 
12c88 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20  in the journal. 
12c89 20 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 20 68         ** file h
12c8a 61 70 70 65 6e 73 20 74 6f 20 62 65 20 61 20 6a  appens to be a j
12c8b 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 28 77  ournal-header (w
12c8c 72 69 74 74 65 6e 20 61 73 20 70 61 72 74 20 6f  ritten as part o
12c8d 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  f the.        **
12c8e 20 70 72 65 76 69 6f 75 73 20 63 6f 6e 6e 65 63   previous connec
12c8f 74 69 6f 6e 73 20 74 72 61 6e 73 61 63 74 69 6f  tions transactio
12c90 6e 29 2c 20 61 6e 64 20 61 20 63 72 61 73 68 20  n), and a crash 
12c91 6f 72 20 70 6f 77 65 72 2d 66 61 69 6c 75 72 65  or power-failure
12c92 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 63 63   .        ** occ
12c93 75 72 73 20 61 66 74 65 72 20 6e 52 65 63 20 69  urs after nRec i
12c94 73 20 75 70 64 61 74 65 64 20 62 75 74 20 62 65  s updated but be
12c95 66 6f 72 65 20 74 68 69 73 20 63 6f 6e 6e 65 63  fore this connec
12c96 74 69 6f 6e 20 77 72 69 74 65 73 20 0a 20 20 20  tion writes .   
12c97 20 20 20 20 20 2a 2a 20 61 6e 79 74 68 69 6e 67       ** anything
12c98 20 65 6c 73 65 20 74 6f 20 74 68 65 20 6a 6f 75   else to the jou
12c99 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 20 63 6f  rnal file (or co
12c9a 6d 6d 69 74 73 2f 72 6f 6c 6c 73 20 62 61 63 6b  mmits/rolls back
12c9b 20 69 74 73 20 0a 20 20 20 20 20 20 20 20 2a 2a   its .        **
12c9c 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 74   transaction), t
12c9d 68 65 6e 20 53 51 4c 69 74 65 20 6d 61 79 20 62  hen SQLite may b
12c9e 65 63 6f 6d 65 20 63 6f 6e 66 75 73 65 64 20 77  ecome confused w
12c9f 68 65 6e 20 64 6f 69 6e 67 20 74 68 65 20 0a 20  hen doing the . 
12ca0 20 20 20 20 20 20 20 2a 2a 20 68 6f 74 2d 6a 6f         ** hot-jo
12ca1 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 20 66  urnal rollback f
12ca2 6f 6c 6c 6f 77 69 6e 67 20 72 65 63 6f 76 65 72  ollowing recover
12ca3 79 2e 20 49 74 20 6d 61 79 20 72 6f 6c 6c 20 62  y. It may roll b
12ca4 61 63 6b 20 61 6c 6c 0a 20 20 20 20 20 20 20 20  ack all.        
12ca5 2a 2a 20 6f 66 20 74 68 69 73 20 63 6f 6e 6e 65  ** of this conne
12ca6 63 74 69 6f 6e 73 20 64 61 74 61 2c 20 74 68 65  ctions data, the
12ca7 6e 20 70 72 6f 63 65 65 64 20 74 6f 20 72 6f 6c  n proceed to rol
12ca8 6c 69 6e 67 20 62 61 63 6b 20 74 68 65 20 6f 6c  ling back the ol
12ca9 64 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 75  d,.        ** ou
12caa 74 2d 6f 66 2d 64 61 74 65 20 64 61 74 61 20 74  t-of-date data t
12cab 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 74 2e 20  hat follows it. 
12cac 44 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74  Database corrupt
12cad 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a  ion..        **.
12cae 20 20 20 20 20 20 20 20 2a 2a 20 54 6f 20 77 6f          ** To wo
12caf 72 6b 20 61 72 6f 75 6e 64 20 74 68 69 73 2c 20  rk around this, 
12cb0 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  if the journal f
12cb1 69 6c 65 20 64 6f 65 73 20 61 70 70 65 61 72 20  ile does appear 
12cb2 74 6f 20 63 6f 6e 74 61 69 6e 0a 20 20 20 20 20  to contain.     
12cb3 20 20 20 2a 2a 20 61 20 76 61 6c 69 64 20 68 65     ** a valid he
12cb4 61 64 65 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 50  ader following P
12cb5 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 2c  ager.journalOff,
12cb6 20 74 68 65 6e 20 77 72 69 74 65 20 61 20 30 78   then write a 0x
12cb7 30 30 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 79  00.        ** by
12cb8 74 65 20 74 6f 20 74 68 65 20 73 74 61 72 74 20  te to the start 
12cb9 6f 66 20 69 74 20 74 6f 20 70 72 65 76 65 6e 74  of it to prevent
12cba 20 69 74 20 66 72 6f 6d 20 62 65 69 6e 67 20 72   it from being r
12cbb 65 63 6f 67 6e 69 7a 65 64 2e 0a 20 20 20 20 20  ecognized..     
12cbc 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
12cbd 20 56 61 72 69 61 62 6c 65 20 69 4e 65 78 74 48   Variable iNextH
12cbe 64 72 4f 66 66 73 65 74 20 69 73 20 73 65 74 20  drOffset is set 
12cbf 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20 61 74  to the offset at
12cc0 20 77 68 69 63 68 20 74 68 69 73 0a 20 20 20 20   which this.    
12cc1 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 61 74      ** problemat
12cc2 69 63 20 68 65 61 64 65 72 20 77 69 6c 6c 20 6f  ic header will o
12cc3 63 63 75 72 2c 20 69 66 20 69 74 20 65 78 69 73  ccur, if it exis
12cc4 74 73 2e 20 61 4d 61 67 69 63 20 69 73 20 75 73  ts. aMagic is us
12cc5 65 64 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  ed .        ** a
12cc6 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 62 75  s a temporary bu
12cc7 66 66 65 72 20 74 6f 20 69 6e 73 70 65 63 74 20  ffer to inspect 
12cc8 74 68 65 20 66 69 72 73 74 20 63 6f 75 70 6c 65  the first couple
12cc9 20 6f 66 20 62 79 74 65 73 20 6f 66 0a 20 20 20   of bytes of.   
12cca 20 20 20 20 20 2a 2a 20 74 68 65 20 70 6f 74 65       ** the pote
12ccb 6e 74 69 61 6c 20 6a 6f 75 72 6e 61 6c 20 68 65  ntial journal he
12ccc 61 64 65 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f  ader..        */
12ccd 0a 20 20 20 20 20 20 20 20 69 36 34 20 69 4e 65  .        i64 iNe
12cce 78 74 48 64 72 4f 66 66 73 65 74 3b 0a 20 20 20  xtHdrOffset;.   
12ccf 20 20 20 20 20 75 38 20 61 4d 61 67 69 63 5b 38       u8 aMagic[8
12cd0 5d 3b 0a 09 75 38 20 7a 48 65 61 64 65 72 5b 73  ];..u8 zHeader[s
12cd1 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61  izeof(aJournalMa
12cd2 67 69 63 29 2b 34 5d 3b 0a 0a 09 6d 65 6d 63 70  gic)+4];...memcp
12cd3 79 28 7a 48 65 61 64 65 72 2c 20 61 4a 6f 75 72  y(zHeader, aJour
12cd4 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66  nalMagic, sizeof
12cd5 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 29  (aJournalMagic))
12cd6 3b 0a 09 70 75 74 33 32 62 69 74 73 28 26 7a 48  ;..put32bits(&zH
12cd7 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f  eader[sizeof(aJo
12cd8 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c 20 70 50  urnalMagic)], pP
12cd9 61 67 65 72 2d 3e 6e 52 65 63 29 3b 0a 0a 20 20  ager->nRec);..  
12cda 20 20 20 20 20 20 69 4e 65 78 74 48 64 72 4f 66        iNextHdrOf
12cdb 66 73 65 74 20 3d 20 6a 6f 75 72 6e 61 6c 48 64  fset = journalHd
12cdc 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 3b  rOffset(pPager);
12cdd 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
12cde 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67  lite3OsRead(pPag
12cdf 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 63 2c  er->jfd, aMagic,
12ce0 20 38 2c 20 69 4e 65 78 74 48 64 72 4f 66 66 73   8, iNextHdrOffs
12ce1 65 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  et);.        if(
12ce2 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
12ce3 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 61 4d 61 67  & 0==memcmp(aMag
12ce4 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  ic, aJournalMagi
12ce5 63 2c 20 38 29 20 29 7b 0a 20 20 20 20 20 20 20  c, 8) ){.       
12ce6 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
12ce7 75 38 20 7a 65 72 6f 62 79 74 65 20 3d 20 30 3b  u8 zerobyte = 0;
12ce8 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
12ce9 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70  sqlite3OsWrite(p
12cea 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 7a 65 72  Pager->jfd, &zer
12ceb 6f 62 79 74 65 2c 20 31 2c 20 69 4e 65 78 74 48  obyte, 1, iNextH
12cec 64 72 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20  drOffset);.     
12ced 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
12cee 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc!=SQLITE_OK &
12cef 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45  & rc!=SQLITE_IOE
12cf0 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b  RR_SHORT_READ ){
12cf1 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
12cf2 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  n rc;.        }.
12cf3 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74  .        /* Writ
12cf4 65 20 74 68 65 20 6e 52 65 63 20 76 61 6c 75 65  e the nRec value
12cf5 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61   into the journa
12cf6 6c 20 66 69 6c 65 20 68 65 61 64 65 72 2e 20 49  l file header. I
12cf7 66 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  f in.        ** 
12cf8 66 75 6c 6c 2d 73 79 6e 63 68 72 6f 6e 6f 75 73  full-synchronous
12cf9 20 6d 6f 64 65 2c 20 73 79 6e 63 20 74 68 65 20   mode, sync the 
12cfa 6a 6f 75 72 6e 61 6c 20 66 69 72 73 74 2e 20 54  journal first. T
12cfb 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74  his ensures that
12cfc 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c 20  .        ** all 
12cfd 64 61 74 61 20 68 61 73 20 72 65 61 6c 6c 79 20  data has really 
12cfe 68 69 74 20 74 68 65 20 64 69 73 6b 20 62 65 66  hit the disk bef
12cff 6f 72 65 20 6e 52 65 63 20 69 73 20 75 70 64 61  ore nRec is upda
12d00 74 65 64 20 74 6f 20 6d 61 72 6b 0a 20 20 20 20  ted to mark.    
12d01 20 20 20 20 2a 2a 20 69 74 20 61 73 20 61 20 63      ** it as a c
12d02 61 6e 64 69 64 61 74 65 20 66 6f 72 20 72 6f 6c  andidate for rol
12d03 6c 62 61 63 6b 2e 0a 20 20 20 20 20 20 20 20 2a  lback..        *
12d04 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 68 69  *.        ** Thi
12d05 73 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65  s is not require
12d06 64 20 69 66 20 74 68 65 20 70 65 72 73 69 73 74  d if the persist
12d07 65 6e 74 20 6d 65 64 69 61 20 73 75 70 70 6f 72  ent media suppor
12d08 74 73 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  ts the.        *
12d09 2a 20 53 41 46 45 5f 41 50 50 45 4e 44 20 70 72  * SAFE_APPEND pr
12d0a 6f 70 65 72 74 79 2e 20 42 65 63 61 75 73 65 20  operty. Because 
12d0b 69 6e 20 74 68 69 73 20 63 61 73 65 20 69 74 20  in this case it 
12d0c 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
12d0d 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 20  .        ** for 
12d0e 67 61 72 62 61 67 65 20 64 61 74 61 20 74 6f 20  garbage data to 
12d0f 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74  be appended to t
12d10 68 65 20 66 69 6c 65 2c 20 74 68 65 20 6e 52 65  he file, the nRe
12d11 63 20 66 69 65 6c 64 0a 20 20 20 20 20 20 20 20  c field.        
12d12 2a 2a 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20  ** is populated 
12d13 77 69 74 68 20 30 78 46 46 46 46 46 46 46 46 20  with 0xFFFFFFFF 
12d14 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  when the journal
12d15 20 68 65 61 64 65 72 20 69 73 20 77 72 69 74 74   header is writt
12d16 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e  en.        ** an
12d17 64 20 6e 65 76 65 72 20 6e 65 65 64 73 20 74 6f  d never needs to
12d18 20 62 65 20 75 70 64 61 74 65 64 2e 0a 20 20 20   be updated..   
12d19 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
12d1a 69 66 28 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c  if( pPager->full
12d1b 53 79 6e 63 20 26 26 20 30 3d 3d 28 69 44 63 26  Sync && 0==(iDc&
12d1c 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51  SQLITE_IOCAP_SEQ
12d1d 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20 20 20  UENTIAL) ){.    
12d1e 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45        PAGERTRACE
12d1f 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20  (("SYNC journal 
12d20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49  of %d\n", PAGERI
12d21 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20  D(pPager)));.   
12d22 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28         IOTRACE((
12d23 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50  "JSYNC %p\n", pP
12d24 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 20  ager)).         
12d25 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53   rc = sqlite3OsS
12d26 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ync(pPager->jfd,
12d27 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c   pPager->sync_fl
12d28 61 67 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ags);.          
12d29 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
12d2a 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
12d2b 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
12d2c 20 49 4f 54 52 41 43 45 28 28 22 4a 48 44 52 20   IOTRACE(("JHDR 
12d2d 25 70 20 25 6c 6c 64 5c 6e 22 2c 20 70 50 61 67  %p %lld\n", pPag
12d2e 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  er, pPager->jour
12d2f 6e 61 6c 48 64 72 29 29 3b 0a 20 20 20 20 20 20  nalHdr));.      
12d30 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
12d31 57 72 69 74 65 28 0a 20 20 20 20 20 20 20 20 20  Write(.         
12d32 20 20 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20     pPager->jfd, 
12d33 7a 48 65 61 64 65 72 2c 20 73 69 7a 65 6f 66 28  zHeader, sizeof(
12d34 7a 48 65 61 64 65 72 29 2c 20 70 50 61 67 65 72  zHeader), pPager
12d35 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 0a 09 29 3b  ->journalHdr..);
12d36 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
12d37 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
12d38 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
12d39 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 44        if( 0==(iD
12d3a 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53  c&SQLITE_IOCAP_S
12d3b 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20  EQUENTIAL) ){.  
12d3c 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45        PAGERTRACE
12d3d 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20  (("SYNC journal 
12d3e 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49  of %d\n", PAGERI
12d3f 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20  D(pPager)));.   
12d40 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a       IOTRACE(("J
12d41 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67  SYNC %p\n", pPag
12d42 65 72 29 29 0a 20 20 20 20 20 20 20 20 72 63 20  er)).        rc 
12d43 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28  = sqlite3OsSync(
12d44 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61  pPager->jfd, pPa
12d45 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 7c  ger->sync_flags|
12d46 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 50 61   .          (pPa
12d47 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 3d  ger->sync_flags=
12d48 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c  =SQLITE_SYNC_FUL
12d49 4c 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41  L?SQLITE_SYNC_DA
12d4a 54 41 4f 4e 4c 59 3a 30 29 0a 20 20 20 20 20 20  TAONLY:0).      
12d4b 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28    );.        if(
12d4c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
12d4d 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
12d4e 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
12d4f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  * The journal fi
12d50 6c 65 20 77 61 73 20 6a 75 73 74 20 73 75 63 63  le was just succ
12d51 65 73 73 66 75 6c 6c 79 20 73 79 6e 63 65 64 2e  essfully synced.
12d52 20 53 65 74 20 50 61 67 65 72 2e 6e 65 65 64 53   Set Pager.needS
12d53 79 6e 63 20 0a 20 20 20 20 2a 2a 20 74 6f 20 7a  ync .    ** to z
12d54 65 72 6f 20 61 6e 64 20 63 6c 65 61 72 20 74 68  ero and clear th
12d55 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  e PGHDR_NEED_SYN
12d56 43 20 66 6c 61 67 20 6f 6e 20 61 6c 6c 20 70 61  C flag on all pa
12d57 67 65 73 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  gess..    */.   
12d58 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
12d59 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65  c = 0;.    pPage
12d5a 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65  r->journalStarte
12d5b 64 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74  d = 1;.    sqlit
12d5c 65 33 50 63 61 63 68 65 43 6c 65 61 72 53 79 6e  e3PcacheClearSyn
12d5d 63 46 6c 61 67 73 28 70 50 61 67 65 72 2d 3e 70  cFlags(pPager->p
12d5e 50 43 61 63 68 65 29 3b 0a 20 20 7d 0a 0a 20 20  PCache);.  }..  
12d5f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
12d60 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61  ;.}../*.** The a
12d61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 66  rgument is the f
12d62 69 72 73 74 20 69 6e 20 61 20 6c 69 6e 6b 65 64  irst in a linked
12d63 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70   list of dirty p
12d64 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 0a 2a  ages connected.*
12d65 2a 20 62 79 20 74 68 65 20 50 67 48 64 72 2e 70  * by the PgHdr.p
12d66 44 69 72 74 79 20 70 6f 69 6e 74 65 72 2e 20 54  Dirty pointer. T
12d67 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 72 69  his function wri
12d68 74 65 73 20 65 61 63 68 20 6f 6e 65 20 6f 66 20  tes each one of 
12d69 74 68 65 0a 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79  the.** in-memory
12d6a 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 6c 69   pages in the li
12d6b 73 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  st to the databa
12d6c 73 65 20 66 69 6c 65 2e 20 54 68 65 20 61 72 67  se file. The arg
12d6d 75 6d 65 6e 74 20 6d 61 79 0a 2a 2a 20 62 65 20  ument may.** be 
12d6e 4e 55 4c 4c 2c 20 72 65 70 72 65 73 65 6e 74 69  NULL, representi
12d6f 6e 67 20 61 6e 20 65 6d 70 74 79 20 6c 69 73 74  ng an empty list
12d70 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
12d71 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a  his function is.
12d72 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  ** a no-op..**.*
12d73 2a 20 54 68 65 20 70 61 67 65 72 20 6d 75 73 74  * The pager must
12d74 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 20 61   hold at least a
12d75 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 77   RESERVED lock w
12d76 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
12d77 6e 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e 20  n.** is called. 
12d78 42 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 61  Before writing a
12d79 6e 79 74 68 69 6e 67 20 74 6f 20 74 68 65 20 64  nything to the d
12d7a 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68  atabase file, th
12d7b 69 73 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20 75 70  is lock.** is up
12d7c 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45 58 43  graded to an EXC
12d7d 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20  LUSIVE lock. If 
12d7e 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20  the lock cannot 
12d7f 62 65 20 6f 62 74 61 69 6e 65 64 2c 0a 2a 2a 20  be obtained,.** 
12d80 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72  SQLITE_BUSY is r
12d81 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64  eturned and no d
12d82 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 74  ata is written t
12d83 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
12d84 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74  ile..** .** If t
12d85 68 65 20 70 61 67 65 72 20 69 73 20 61 20 74 65  he pager is a te
12d86 6d 70 2d 66 69 6c 65 20 70 61 67 65 72 20 61 6e  mp-file pager an
12d87 64 20 74 68 65 20 61 63 74 75 61 6c 20 66 69 6c  d the actual fil
12d88 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 0a 2a 2a  e-system file.**
12d89 20 69 73 20 6e 6f 74 20 79 65 74 20 6f 70 65 6e   is not yet open
12d8a 2c 20 69 74 20 69 73 20 63 72 65 61 74 65 64 20  , it is created 
12d8b 61 6e 64 20 6f 70 65 6e 65 64 20 62 65 66 6f 72  and opened befor
12d8c 65 20 61 6e 79 20 64 61 74 61 20 69 73 20 0a 2a  e any data is .*
12d8d 2a 20 77 72 69 74 74 65 6e 20 6f 75 74 2e 0a 2a  * written out..*
12d8e 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68 65 20 6c 6f  *.** Once the lo
12d8f 63 6b 20 68 61 73 20 62 65 65 6e 20 75 70 67 72  ck has been upgr
12d90 61 64 65 64 20 61 6e 64 2c 20 69 66 20 6e 65 63  aded and, if nec
12d91 65 73 73 61 72 79 2c 20 74 68 65 20 66 69 6c 65  essary, the file
12d92 20 6f 70 65 6e 65 64 2c 0a 2a 2a 20 74 68 65 20   opened,.** the 
12d93 70 61 67 65 73 20 61 72 65 20 77 72 69 74 74 65  pages are writte
12d94 6e 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74  n out to the dat
12d95 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 6c 69  abase file in li
12d96 73 74 20 6f 72 64 65 72 2e 20 57 72 69 74 69 6e  st order. Writin
12d97 67 0a 2a 2a 20 61 20 70 61 67 65 20 69 73 20 73  g.** a page is s
12d98 6b 69 70 70 65 64 20 69 66 20 69 74 20 6d 65 65  kipped if it mee
12d99 74 73 20 65 69 74 68 65 72 20 6f 66 20 74 68 65  ts either of the
12d9a 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 74 65   following crite
12d9b 72 69 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54  ria:.**.**   * T
12d9c 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69  he page number i
12d9d 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 50  s greater than P
12d9e 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 6f 72 0a  ager.dbSize, or.
12d9f 2a 2a 20 20 20 2a 20 54 68 65 20 50 47 48 44 52  **   * The PGHDR
12da0 5f 44 4f 4e 54 5f 57 52 49 54 45 20 66 6c 61 67  _DONT_WRITE flag
12da1 20 69 73 20 73 65 74 20 6f 6e 20 74 68 65 20 70   is set on the p
12da2 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72  age..**.** If wr
12da3 69 74 69 6e 67 20 6f 75 74 20 61 20 70 61 67 65  iting out a page
12da4 20 63 61 75 73 65 73 20 74 68 65 20 64 61 74 61   causes the data
12da5 62 61 73 65 20 66 69 6c 65 20 74 6f 20 67 72 6f  base file to gro
12da6 77 2c 20 50 61 67 65 72 2e 64 62 46 69 6c 65 53  w, Pager.dbFileS
12da7 69 7a 65 0a 2a 2a 20 69 73 20 75 70 64 61 74 65  ize.** is update
12da8 64 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 20 49  d accordingly. I
12da9 66 20 70 61 67 65 20 31 20 69 73 20 77 72 69 74  f page 1 is writ
12daa 74 65 6e 20 6f 75 74 2c 20 74 68 65 6e 20 74 68  ten out, then th
12dab 65 20 76 61 6c 75 65 20 63 61 63 68 65 64 0a 2a  e value cached.*
12dac 2a 20 69 6e 20 50 61 67 65 72 2e 64 62 46 69 6c  * in Pager.dbFil
12dad 65 56 65 72 73 5b 5d 20 69 73 20 75 70 64 61 74  eVers[] is updat
12dae 65 64 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20  ed to match the 
12daf 6e 65 77 20 76 61 6c 75 65 20 73 74 6f 72 65 64  new value stored
12db0 20 69 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61 62   in.** the datab
12db1 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ase file..**.** 
12db2 49 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73  If everything is
12db3 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c   successful, SQL
12db4 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
12db5 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72  ed. If an IO err
12db6 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 2c 20 61  or .** occurs, a
12db7 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20  n IO error code 
12db8 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 72 2c  is returned. Or,
12db9 20 69 66 20 74 68 65 20 45 58 43 4c 55 53 49 56   if the EXCLUSIV
12dba 45 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 0a 2a 2a  E lock cannot.**
12dbb 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 53 51   be obtained, SQ
12dbc 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74  LITE_BUSY is ret
12dbd 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  urned..*/.static
12dbe 20 69 6e 74 20 70 61 67 65 72 5f 77 72 69 74 65   int pager_write
12dbf 5f 70 61 67 65 6c 69 73 74 28 50 67 48 64 72 20  _pagelist(PgHdr 
12dc0 2a 70 4c 69 73 74 29 7b 0a 20 20 50 61 67 65 72  *pList){.  Pager
12dc1 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 20   *pPager;       
12dc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12dc3 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20  /* Pager object 
12dc4 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
12dc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12dc6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
12dc7 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 69  urn code */..  i
12dc8 66 28 20 4e 45 56 45 52 28 70 4c 69 73 74 3d 3d  f( NEVER(pList==
12dc9 30 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  0) ) return SQLI
12dca 54 45 5f 4f 4b 3b 0a 20 20 70 50 61 67 65 72 20  TE_OK;.  pPager 
12dcb 3d 20 70 4c 69 73 74 2d 3e 70 50 61 67 65 72 3b  = pList->pPager;
12dcc 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70  ..  /* At this p
12dcd 6f 69 6e 74 20 74 68 65 72 65 20 6d 61 79 20 62  oint there may b
12dce 65 20 65 69 74 68 65 72 20 61 20 52 45 53 45 52  e either a RESER
12dcf 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45  VED or EXCLUSIVE
12dd0 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 2a   lock on the.  *
12dd1 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  * database file.
12dd2 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72   If there is alr
12dd3 65 61 64 79 20 61 6e 20 45 58 43 4c 55 53 49 56  eady an EXCLUSIV
12dd4 45 20 6c 6f 63 6b 2c 20 74 68 65 20 66 6f 6c 6c  E lock, the foll
12dd5 6f 77 69 6e 67 0a 20 20 2a 2a 20 63 61 6c 6c 20  owing.  ** call 
12dd6 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a  is a no-op..  **
12dd7 0a 20 20 2a 2a 20 4d 6f 76 69 6e 67 20 74 68 65  .  ** Moving the
12dd8 20 6c 6f 63 6b 20 66 72 6f 6d 20 52 45 53 45 52   lock from RESER
12dd9 56 45 44 20 74 6f 20 45 58 43 4c 55 53 49 56 45  VED to EXCLUSIVE
12dda 20 61 63 74 75 61 6c 6c 79 20 69 6e 76 6f 6c 76   actually involv
12ddb 65 73 20 67 6f 69 6e 67 0a 20 20 2a 2a 20 74 68  es going.  ** th
12ddc 72 6f 75 67 68 20 61 6e 20 69 6e 74 65 72 6d 65  rough an interme
12ddd 64 69 61 74 65 20 73 74 61 74 65 20 50 45 4e 44  diate state PEND
12dde 49 4e 47 2e 20 20 20 41 20 50 45 4e 44 49 4e 47  ING.   A PENDING
12ddf 20 6c 6f 63 6b 20 70 72 65 76 65 6e 74 73 20 6e   lock prevents n
12de0 65 77 0a 20 20 2a 2a 20 72 65 61 64 65 72 73 20  ew.  ** readers 
12de1 66 72 6f 6d 20 61 74 74 61 63 68 69 6e 67 20 74  from attaching t
12de2 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62  o the database b
12de3 75 74 20 69 73 20 75 6e 73 75 66 66 69 63 69 65  ut is unsufficie
12de4 6e 74 20 66 6f 72 20 75 73 20 74 6f 0a 20 20 2a  nt for us to.  *
12de5 2a 20 77 72 69 74 65 2e 20 20 54 68 65 20 69 64  * write.  The id
12de6 65 61 20 6f 66 20 61 20 50 45 4e 44 49 4e 47 20  ea of a PENDING 
12de7 6c 6f 63 6b 20 69 73 20 74 6f 20 70 72 65 76 65  lock is to preve
12de8 6e 74 20 6e 65 77 20 72 65 61 64 65 72 73 20 66  nt new readers f
12de9 72 6f 6d 0a 20 20 2a 2a 20 63 6f 6d 69 6e 67 20  rom.  ** coming 
12dea 69 6e 20 77 68 69 6c 65 20 77 65 20 77 61 69 74  in while we wait
12deb 20 66 6f 72 20 65 78 69 73 74 69 6e 67 20 72 65   for existing re
12dec 61 64 65 72 73 20 74 6f 20 63 6c 65 61 72 2e 0a  aders to clear..
12ded 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 69 6c 65 20    **.  ** While 
12dee 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20  the pager is in 
12def 74 68 65 20 52 45 53 45 52 56 45 44 20 73 74 61  the RESERVED sta
12df0 74 65 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  te, the original
12df1 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20   database file. 
12df2 20 2a 2a 20 69 73 20 75 6e 63 68 61 6e 67 65 64   ** is unchanged
12df3 20 61 6e 64 20 77 65 20 63 61 6e 20 72 6f 6c 6c   and we can roll
12df4 62 61 63 6b 20 77 69 74 68 6f 75 74 20 68 61 76  back without hav
12df5 69 6e 67 20 74 6f 20 70 6c 61 79 62 61 63 6b 20  ing to playback 
12df6 74 68 65 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c  the.  ** journal
12df7 20 69 6e 74 6f 20 74 68 65 20 6f 72 69 67 69 6e   into the origin
12df8 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  al database file
12df9 2e 20 20 4f 6e 63 65 20 77 65 20 74 72 61 6e 73  .  Once we trans
12dfa 69 74 69 6f 6e 20 74 6f 0a 20 20 2a 2a 20 45 58  ition to.  ** EX
12dfb 43 4c 55 53 49 56 45 2c 20 69 74 20 6d 65 61 6e  CLUSIVE, it mean
12dfc 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  s the database f
12dfd 69 6c 65 20 68 61 73 20 62 65 65 6e 20 63 68 61  ile has been cha
12dfe 6e 67 65 64 20 61 6e 64 20 61 6e 79 20 72 6f 6c  nged and any rol
12dff 6c 62 61 63 6b 0a 20 20 2a 2a 20 77 69 6c 6c 20  lback.  ** will 
12e00 72 65 71 75 69 72 65 20 61 20 6a 6f 75 72 6e 61  require a journa
12e01 6c 20 70 6c 61 79 62 61 63 6b 2e 0a 20 20 2a 2f  l playback..  */
12e02 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
12e03 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f  r->state>=PAGER_
12e04 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20 72 63  RESERVED );.  rc
12e05 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e   = pager_wait_on
12e06 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20 45 58  _lock(pPager, EX
12e07 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 0a  CLUSIVE_LOCK);..
12e08 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65    /* If the file
12e09 20 69 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 20   is a temp-file 
12e0a 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e  has not yet been
12e0b 20 6f 70 65 6e 65 64 2c 20 6f 70 65 6e 20 69 74   opened, open it
12e0c 20 6e 6f 77 2e 20 49 74 0a 20 20 2a 2a 20 69 73   now. It.  ** is
12e0d 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f   not possible fo
12e0e 72 20 72 63 20 74 6f 20 62 65 20 6f 74 68 65 72  r rc to be other
12e0f 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4f 4b 20   than SQLITE_OK 
12e10 69 66 20 74 68 69 73 20 62 72 61 6e 63 68 0a 20  if this branch. 
12e11 20 2a 2a 20 69 73 20 74 61 6b 65 6e 2c 20 61 73   ** is taken, as
12e12 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c   pager_wait_on_l
12e13 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70  ock() is a no-op
12e14 20 66 6f 72 20 74 65 6d 70 2d 66 69 6c 65 73 2e   for temp-files.
12e15 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 69 73 4f  .  */.  if( !isO
12e16 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20  pen(pPager->fd) 
12e17 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
12e18 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20  Pager->tempFile 
12e19 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
12e1a 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67   );.    rc = pag
12e1b 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 67 65  erOpentemp(pPage
12e1c 72 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c 20 70  r, pPager->fd, p
12e1d 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 67 73 29  Pager->vfsFlags)
12e1e 3b 0a 20 20 7d 0a 0a 20 20 77 68 69 6c 65 28 20  ;.  }..  while( 
12e1f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
12e20 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 50 67   pList ){.    Pg
12e21 6e 6f 20 70 67 6e 6f 20 3d 20 70 4c 69 73 74 2d  no pgno = pList-
12e22 3e 70 67 6e 6f 3b 0a 0a 20 20 20 20 2f 2a 20 49  >pgno;..    /* I
12e23 66 20 74 68 65 72 65 20 61 72 65 20 64 69 72 74  f there are dirt
12e24 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 70  y pages in the p
12e25 61 67 65 20 63 61 63 68 65 20 77 69 74 68 20 70  age cache with p
12e26 61 67 65 20 6e 75 6d 62 65 72 73 20 67 72 65 61  age numbers grea
12e27 74 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 6e 20  ter.    ** than 
12e28 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 74 68  Pager.dbSize, th
12e29 69 73 20 6d 65 61 6e 73 20 73 71 6c 69 74 65 33  is means sqlite3
12e2a 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61  PagerTruncateIma
12e2b 67 65 28 29 20 77 61 73 20 63 61 6c 6c 65 64 20  ge() was called 
12e2c 74 6f 0a 20 20 20 20 2a 2a 20 6d 61 6b 65 20 74  to.    ** make t
12e2d 68 65 20 66 69 6c 65 20 73 6d 61 6c 6c 65 72 20  he file smaller 
12e2e 28 70 72 65 73 75 6d 61 62 6c 79 20 62 79 20 61  (presumably by a
12e2f 75 74 6f 2d 76 61 63 75 75 6d 20 63 6f 64 65 29  uto-vacuum code)
12e30 2e 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 0a 20  . Do not write. 
12e31 20 20 20 2a 2a 20 61 6e 79 20 73 75 63 68 20 70     ** any such p
12e32 61 67 65 73 20 74 6f 20 74 68 65 20 66 69 6c 65  ages to the file
12e33 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
12e34 41 6c 73 6f 2c 20 64 6f 20 6e 6f 74 20 77 72 69  Also, do not wri
12e35 74 65 20 6f 75 74 20 61 6e 79 20 70 61 67 65 20  te out any page 
12e36 74 68 61 74 20 68 61 73 20 74 68 65 20 50 47 48  that has the PGH
12e37 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20 66 6c  DR_DONT_WRITE fl
12e38 61 67 0a 20 20 20 20 2a 2a 20 73 65 74 20 28 73  ag.    ** set (s
12e39 65 74 20 62 79 20 73 71 6c 69 74 65 33 50 61 67  et by sqlite3Pag
12e3a 65 72 44 6f 6e 74 57 72 69 74 65 28 29 29 2e 0a  erDontWrite())..
12e3b 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
12e3c 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 53  gno<=pPager->dbS
12e3d 69 7a 65 20 26 26 20 30 3d 3d 28 70 4c 69 73 74  ize && 0==(pList
12e3e 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 4f  ->flags&PGHDR_DO
12e3f 4e 54 5f 57 52 49 54 45 29 20 29 7b 0a 20 20 20  NT_WRITE) ){.   
12e40 20 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20     i64 offset = 
12e41 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34 29 70 50  (pgno-1)*(i64)pP
12e42 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 20  ager->pageSize; 
12e43 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 77    /* Offset to w
12e44 72 69 74 65 20 2a 2f 0a 20 20 20 20 20 20 63 68  rite */.      ch
12e45 61 72 20 2a 70 44 61 74 61 3b 20 20 20 20 20 20  ar *pData;      
12e46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12e47 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
12e48 44 61 74 61 20 74 6f 20 77 72 69 74 65 20 2a 2f  Data to write */
12e49 20 20 20 20 0a 0a 20 20 20 20 20 20 2f 2a 20 45      ..      /* E
12e4a 6e 63 6f 64 65 20 74 68 65 20 64 61 74 61 62 61  ncode the databa
12e4b 73 65 20 2a 2f 0a 20 20 20 20 20 20 43 4f 44 45  se */.      CODE
12e4c 43 32 28 70 50 61 67 65 72 2c 20 70 4c 69 73 74  C2(pPager, pList
12e4d 2d 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c 20 36  ->pData, pgno, 6
12e4e 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
12e4f 4e 4f 4d 45 4d 2c 20 70 44 61 74 61 29 3b 0a 0a  NOMEM, pData);..
12e50 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 6f        /* Write o
12e51 75 74 20 74 68 65 20 70 61 67 65 20 64 61 74 61  ut the page data
12e52 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20  . */.      rc = 
12e53 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70  sqlite3OsWrite(p
12e54 50 61 67 65 72 2d 3e 66 64 2c 20 70 44 61 74 61  Pager->fd, pData
12e55 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  , pPager->pageSi
12e56 7a 65 2c 20 6f 66 66 73 65 74 29 3b 0a 0a 20 20  ze, offset);..  
12e57 20 20 20 20 2f 2a 20 49 66 20 70 61 67 65 20 31      /* If page 1
12e58 20 77 61 73 20 6a 75 73 74 20 77 72 69 74 74 65   was just writte
12e59 6e 2c 20 75 70 64 61 74 65 20 50 61 67 65 72 2e  n, update Pager.
12e5a 64 62 46 69 6c 65 56 65 72 73 20 74 6f 20 6d 61  dbFileVers to ma
12e5b 74 63 68 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  tch.      ** the
12e5c 20 76 61 6c 75 65 20 6e 6f 77 20 73 74 6f 72 65   value now store
12e5d 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  d in the databas
12e5e 65 20 66 69 6c 65 2e 20 49 66 20 77 72 69 74 69  e file. If writi
12e5f 6e 67 20 74 68 69 73 20 0a 20 20 20 20 20 20 2a  ng this .      *
12e60 2a 20 70 61 67 65 20 63 61 75 73 65 64 20 74 68  * page caused th
12e61 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
12e62 74 6f 20 67 72 6f 77 2c 20 75 70 64 61 74 65 20  to grow, update 
12e63 64 62 46 69 6c 65 53 69 7a 65 2e 20 0a 20 20 20  dbFileSize. .   
12e64 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
12e65 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20  pgno==1 ){.     
12e66 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65     memcpy(&pPage
12e67 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 26  r->dbFileVers, &
12e68 70 44 61 74 61 5b 32 34 5d 2c 20 73 69 7a 65 6f  pData[24], sizeo
12e69 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  f(pPager->dbFile
12e6a 56 65 72 73 29 29 3b 0a 20 20 20 20 20 20 7d 0a  Vers));.      }.
12e6b 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 70        if( pgno>p
12e6c 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a  Pager->dbFileSiz
12e6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61  e ){.        pPa
12e6e 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20  ger->dbFileSize 
12e6f 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a  = pgno;.      }.
12e70 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 61 74 65  .      /* Update
12e71 20 61 6e 79 20 62 61 63 6b 75 70 20 6f 62 6a 65   any backup obje
12e72 63 74 73 20 63 6f 70 79 69 6e 67 20 74 68 65 20  cts copying the 
12e73 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 69 73  contents of this
12e74 20 70 61 67 65 72 2e 20 2a 2f 0a 20 20 20 20 20   pager. */.     
12e75 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70   sqlite3BackupUp
12e76 64 61 74 65 28 70 50 61 67 65 72 2d 3e 70 42 61  date(pPager->pBa
12e77 63 6b 75 70 2c 20 70 67 6e 6f 2c 20 28 75 38 2a  ckup, pgno, (u8*
12e78 29 70 4c 69 73 74 2d 3e 70 44 61 74 61 29 3b 0a  )pList->pData);.
12e79 0a 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43  .      PAGERTRAC
12e7a 45 28 28 22 53 54 4f 52 45 20 25 64 20 70 61 67  E(("STORE %d pag
12e7b 65 20 25 64 20 68 61 73 68 28 25 30 38 78 29 5c  e %d hash(%08x)\
12e7c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
12e7d 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70         PAGERID(p
12e7e 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61  Pager), pgno, pa
12e7f 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 4c 69  ger_pagehash(pLi
12e80 73 74 29 29 29 3b 0a 20 20 20 20 20 20 49 4f 54  st)));.      IOT
12e81 52 41 43 45 28 28 22 50 47 4f 55 54 20 25 70 20  RACE(("PGOUT %p 
12e82 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70  %d\n", pPager, p
12e83 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 50 41 47  gno));.      PAG
12e84 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65 33 5f  ER_INCR(sqlite3_
12e85 70 61 67 65 72 5f 77 72 69 74 65 64 62 5f 63 6f  pager_writedb_co
12e86 75 6e 74 29 3b 0a 20 20 20 20 20 20 50 41 47 45  unt);.      PAGE
12e87 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e  R_INCR(pPager->n
12e88 57 72 69 74 65 29 3b 0a 20 20 20 20 7d 65 6c 73  Write);.    }els
12e89 65 7b 0a 20 20 20 20 20 20 50 41 47 45 52 54 52  e{.      PAGERTR
12e8a 41 43 45 28 28 22 4e 4f 53 54 4f 52 45 20 25 64  ACE(("NOSTORE %d
12e8b 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47   page %d\n", PAG
12e8c 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 67  ERID(pPager), pg
12e8d 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64  no));.    }.#ifd
12e8e 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f  ef SQLITE_CHECK_
12e8f 50 41 47 45 53 0a 20 20 20 20 70 4c 69 73 74 2d  PAGES.    pList-
12e90 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65  >pageHash = page
12e91 72 5f 70 61 67 65 68 61 73 68 28 70 4c 69 73 74  r_pagehash(pList
12e92 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 4c  );.#endif.    pL
12e93 69 73 74 20 3d 20 70 4c 69 73 74 2d 3e 70 44 69  ist = pList->pDi
12e94 72 74 79 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  rty;.  }..  retu
12e95 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
12e96 41 70 70 65 6e 64 20 61 20 72 65 63 6f 72 64 20  Append a record 
12e97 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  of the current s
12e98 74 61 74 65 20 6f 66 20 70 61 67 65 20 70 50 67  tate of page pPg
12e99 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72   to the sub-jour
12e9a 6e 61 6c 2e 20 0a 2a 2a 20 49 74 20 69 73 20 74  nal. .** It is t
12e9b 68 65 20 63 61 6c 6c 65 72 73 20 72 65 73 70 6f  he callers respo
12e9c 6e 73 69 62 69 6c 69 74 79 20 74 6f 20 75 73 65  nsibility to use
12e9d 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67   subjRequiresPag
12e9e 65 28 29 20 74 6f 20 63 68 65 63 6b 20 0a 2a 2a  e() to check .**
12e9f 20 74 68 61 74 20 69 74 20 69 73 20 72 65 61 6c   that it is real
12ea0 6c 79 20 72 65 71 75 69 72 65 64 20 62 65 66 6f  ly required befo
12ea1 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20  re calling this 
12ea2 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
12ea3 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73  If successful, s
12ea4 65 74 20 74 68 65 20 62 69 74 20 63 6f 72 72 65  et the bit corre
12ea5 73 70 6f 6e 64 69 6e 67 20 74 6f 20 70 50 67 2d  sponding to pPg-
12ea6 3e 70 67 6e 6f 20 69 6e 20 74 68 65 20 62 69 74  >pgno in the bit
12ea7 76 65 63 73 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20  vecs.** for all 
12ea8 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 20  open savepoints 
12ea9 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
12eaa 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
12eab 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51  ction returns SQ
12eac 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79  LITE_OK if every
12ead 74 68 69 6e 67 20 69 73 20 73 75 63 63 65 73 73  thing is success
12eae 66 75 6c 2c 20 61 6e 20 49 4f 0a 2a 2a 20 65 72  ful, an IO.** er
12eaf 72 6f 72 20 63 6f 64 65 20 69 66 20 74 68 65 20  ror code if the 
12eb0 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65  attempt to write
12eb1 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72   to the sub-jour
12eb2 6e 61 6c 20 66 61 69 6c 73 2c 20 6f 72 20 0a 2a  nal fails, or .*
12eb3 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69  * SQLITE_NOMEM i
12eb4 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73  f a malloc fails
12eb5 20 77 68 69 6c 65 20 73 65 74 74 69 6e 67 20 61   while setting a
12eb6 20 62 69 74 20 69 6e 20 61 20 73 61 76 65 70 6f   bit in a savepo
12eb7 69 6e 74 0a 2a 2a 20 62 69 74 76 65 63 2e 0a 2a  int.** bitvec..*
12eb8 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 75 62  /.static int sub
12eb9 6a 6f 75 72 6e 61 6c 50 61 67 65 28 50 67 48 64  journalPage(PgHd
12eba 72 20 2a 70 50 67 29 7b 0a 20 20 69 6e 74 20 72  r *pPg){.  int r
12ebb 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
12ebc 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d   Pager *pPager =
12ebd 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20   pPg->pPager;.  
12ebe 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65  if( isOpen(pPage
12ebf 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20  r->sjfd) ){.    
12ec0 76 6f 69 64 20 2a 70 44 61 74 61 20 3d 20 70 50  void *pData = pP
12ec1 67 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20 69 36  g->pData;.    i6
12ec2 34 20 6f 66 66 73 65 74 20 3d 20 70 50 61 67 65  4 offset = pPage
12ec3 72 2d 3e 6e 53 75 62 52 65 63 2a 28 34 2b 70 50  r->nSubRec*(4+pP
12ec4 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b  ager->pageSize);
12ec5 0a 20 20 20 20 63 68 61 72 20 2a 70 44 61 74 61  .    char *pData
12ec6 32 3b 0a 0a 20 20 20 20 43 4f 44 45 43 32 28 70  2;..    CODEC2(p
12ec7 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50  Pager, pData, pP
12ec8 67 2d 3e 70 67 6e 6f 2c 20 37 2c 20 72 65 74 75  g->pgno, 7, retu
12ec9 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c  rn SQLITE_NOMEM,
12eca 20 70 44 61 74 61 32 29 3b 0a 20 20 20 20 50 41   pData2);.    PA
12ecb 47 45 52 54 52 41 43 45 28 28 22 53 54 4d 54 2d  GERTRACE(("STMT-
12ecc 4a 4f 55 52 4e 41 4c 20 25 64 20 70 61 67 65 20  JOURNAL %d page 
12ecd 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70  %d\n", PAGERID(p
12ece 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e  Pager), pPg->pgn
12ecf 6f 29 29 3b 0a 20 20 0a 20 20 20 20 61 73 73 65  o));.  .    asse
12ed0 72 74 28 20 70 61 67 65 49 6e 4a 6f 75 72 6e 61  rt( pageInJourna
12ed1 6c 28 70 50 67 29 20 7c 7c 20 70 50 67 2d 3e 70  l(pPg) || pPg->p
12ed2 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 4f 72  gno>pPager->dbOr
12ed3 69 67 53 69 7a 65 20 29 3b 0a 20 20 20 20 72 63  igSize );.    rc
12ed4 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70   = write32bits(p
12ed5 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 6f 66 66  Pager->sjfd, off
12ed6 73 65 74 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b  set, pPg->pgno);
12ed7 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
12ed8 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
12ed9 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72  rc = sqlite3OsWr
12eda 69 74 65 28 70 50 61 67 65 72 2d 3e 73 6a 66 64  ite(pPager->sjfd
12edb 2c 20 70 44 61 74 61 32 2c 20 70 50 61 67 65 72  , pData2, pPager
12edc 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73  ->pageSize, offs
12edd 65 74 2b 34 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  et+4);.    }.  }
12ede 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
12edf 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 67  E_OK ){.    pPag
12ee0 65 72 2d 3e 6e 53 75 62 52 65 63 2b 2b 3b 0a 20  er->nSubRec++;. 
12ee1 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
12ee2 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3e 30 20  r->nSavepoint>0 
12ee3 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 64 64 54  );.    rc = addT
12ee4 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76 65 63  oSavepointBitvec
12ee5 73 28 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70  s(pPager, pPg->p
12ee6 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  gno);.  }.  retu
12ee7 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  rn rc;.}.../*.**
12ee8 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
12ee9 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  s called by the 
12eea 70 63 61 63 68 65 20 6c 61 79 65 72 20 77 68 65  pcache layer whe
12eeb 6e 20 69 74 20 68 61 73 20 72 65 61 63 68 65 64  n it has reached
12eec 20 73 6f 6d 65 0a 2a 2a 20 73 6f 66 74 20 6d 65   some.** soft me
12eed 6d 6f 72 79 20 6c 69 6d 69 74 2e 20 54 68 65 20  mory limit. The 
12eee 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69  first argument i
12eef 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
12ef0 20 50 61 67 65 72 20 6f 62 6a 65 63 74 0a 2a 2a   Pager object.**
12ef1 20 28 63 61 73 74 20 61 73 20 61 20 76 6f 69 64   (cast as a void
12ef2 2a 29 2e 20 54 68 65 20 70 61 67 65 72 20 69 73  *). The pager is
12ef3 20 61 6c 77 61 79 73 20 27 70 75 72 67 65 61 62   always 'purgeab
12ef4 6c 65 27 20 28 6e 6f 74 20 61 6e 20 69 6e 2d 6d  le' (not an in-m
12ef5 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 62 61 73  emory.** databas
12ef6 65 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 61  e). The second a
12ef7 72 67 75 6d 65 6e 74 20 69 73 20 61 20 72 65 66  rgument is a ref
12ef8 65 72 65 6e 63 65 20 74 6f 20 61 20 70 61 67 65  erence to a page
12ef9 20 74 68 61 74 20 69 73 20 0a 2a 2a 20 63 75 72   that is .** cur
12efa 72 65 6e 74 6c 79 20 64 69 72 74 79 20 62 75 74  rently dirty but
12efb 20 68 61 73 20 6e 6f 20 6f 75 74 73 74 61 6e 64   has no outstand
12efc 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 2e 20  ing references. 
12efd 54 68 65 20 70 61 67 65 0a 2a 2a 20 69 73 20 61  The page.** is a
12efe 6c 77 61 79 73 20 61 73 73 6f 63 69 61 74 65 64  lways associated
12eff 20 77 69 74 68 20 74 68 65 20 50 61 67 65 72 20   with the Pager 
12f00 6f 62 6a 65 63 74 20 70 61 73 73 65 64 20 61 73  object passed as
12f01 20 74 68 65 20 66 69 72 73 74 20 0a 2a 2a 20 61   the first .** a
12f02 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  rgument..**.** T
12f03 68 65 20 6a 6f 62 20 6f 66 20 74 68 69 73 20 66  he job of this f
12f04 75 6e 63 74 69 6f 6e 20 69 73 20 74 6f 20 6d 61  unction is to ma
12f05 6b 65 20 70 50 67 20 63 6c 65 61 6e 20 62 79 20  ke pPg clean by 
12f06 77 72 69 74 69 6e 67 20 69 74 73 20 63 6f 6e 74  writing its cont
12f07 65 6e 74 73 0a 2a 2a 20 6f 75 74 20 74 6f 20 74  ents.** out to t
12f08 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12f09 2c 20 69 66 20 70 6f 73 73 69 62 6c 65 2e 20 54  , if possible. T
12f0a 68 69 73 20 6d 61 79 20 69 6e 76 6f 6c 76 65 20  his may involve 
12f0b 73 79 6e 63 69 6e 67 20 74 68 65 0a 2a 2a 20 6a  syncing the.** j
12f0c 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 0a 2a 2a  ournal file. .**
12f0d 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75  .** If successfu
12f0e 6c 2c 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  l, sqlite3Pcache
12f0f 4d 61 6b 65 43 6c 65 61 6e 28 29 20 69 73 20 63  MakeClean() is c
12f10 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 70 61 67  alled on the pag
12f11 65 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f  e and.** SQLITE_
12f12 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20  OK returned. If 
12f13 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75  an IO error occu
12f14 72 73 20 77 68 69 6c 65 20 74 72 79 69 6e 67 20  rs while trying 
12f15 74 6f 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 70  to make the.** p
12f16 61 67 65 20 63 6c 65 61 6e 2c 20 74 68 65 20 49  age clean, the I
12f17 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  O error code is 
12f18 72 65 74 75 72 6e 65 64 2e 20 49 66 20 74 68 65  returned. If the
12f19 20 70 61 67 65 20 63 61 6e 6e 6f 74 20 62 65 0a   page cannot be.
12f1a 2a 2a 20 6d 61 64 65 20 63 6c 65 61 6e 20 66 6f  ** made clean fo
12f1b 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 72 65 61  r some other rea
12f1c 73 6f 6e 2c 20 62 75 74 20 6e 6f 20 65 72 72 6f  son, but no erro
12f1d 72 20 6f 63 63 75 72 73 2c 20 74 68 65 6e 20 53  r occurs, then S
12f1e 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20 72  QLITE_OK.** is r
12f1f 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74  eturned by sqlit
12f20 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61  e3PcacheMakeClea
12f21 6e 28 29 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65  n() is not calle
12f22 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
12f23 20 70 61 67 65 72 53 74 72 65 73 73 28 76 6f 69   pagerStress(voi
12f24 64 20 2a 70 2c 20 50 67 48 64 72 20 2a 70 50 67  d *p, PgHdr *pPg
12f25 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  ){.  Pager *pPag
12f26 65 72 20 3d 20 28 50 61 67 65 72 20 2a 29 70 3b  er = (Pager *)p;
12f27 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
12f28 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74  TE_OK;..  assert
12f29 28 20 70 50 67 2d 3e 70 50 61 67 65 72 3d 3d 70  ( pPg->pPager==p
12f2a 50 61 67 65 72 20 29 3b 0a 20 20 61 73 73 65 72  Pager );.  asser
12f2b 74 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47  t( pPg->flags&PG
12f2c 48 44 52 5f 44 49 52 54 59 20 29 3b 0a 0a 20 20  HDR_DIRTY );..  
12f2d 2f 2a 20 54 68 65 20 64 6f 4e 6f 74 53 79 6e 63  /* The doNotSync
12f2e 20 66 6c 61 67 20 69 73 20 73 65 74 20 62 79 20   flag is set by 
12f2f 74 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72  the sqlite3Pager
12f30 57 72 69 74 65 28 29 20 66 75 6e 63 74 69 6f 6e  Write() function
12f31 20 77 68 69 6c 65 20 69 74 0a 20 20 2a 2a 20 69   while it.  ** i
12f32 73 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 61 20  s journalling a 
12f33 73 65 74 20 6f 66 20 74 77 6f 20 6f 72 20 6d 6f  set of two or mo
12f34 72 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  re database page
12f35 73 20 74 68 61 74 20 61 72 65 20 73 74 6f 72 65  s that are store
12f36 64 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 73 61  d.  ** on the sa
12f37 6d 65 20 64 69 73 6b 20 73 65 63 74 6f 72 2e 20  me disk sector. 
12f38 53 79 6e 63 69 6e 67 20 74 68 65 20 6a 6f 75 72  Syncing the jour
12f39 6e 61 6c 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77  nal is not allow
12f3a 65 64 20 77 68 69 6c 65 0a 20 20 2a 2a 20 74 68  ed while.  ** th
12f3b 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e 67 20  is is happening 
12f3c 61 73 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61  as it is importa
12f3d 6e 74 20 74 68 61 74 20 61 6c 6c 20 6d 65 6d 62  nt that all memb
12f3e 65 72 73 20 6f 66 20 73 75 63 68 20 61 0a 20 20  ers of such a.  
12f3f 2a 2a 20 73 65 74 20 6f 66 20 70 61 67 65 73 20  ** set of pages 
12f40 61 72 65 20 73 79 6e 63 65 64 20 74 6f 20 64 69  are synced to di
12f41 73 6b 20 74 6f 67 65 74 68 65 72 2e 20 53 6f 2c  sk together. So,
12f42 20 69 66 20 74 68 65 20 70 61 67 65 20 74 68 69   if the page thi
12f43 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20  s function.  ** 
12f44 69 73 20 74 72 79 69 6e 67 20 74 6f 20 6d 61 6b  is trying to mak
12f45 65 20 63 6c 65 61 6e 20 77 69 6c 6c 20 72 65 71  e clean will req
12f46 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c 20 73  uire a journal s
12f47 79 6e 63 20 61 6e 64 20 74 68 65 20 64 6f 4e 6f  ync and the doNo
12f48 74 53 79 6e 63 0a 20 20 2a 2a 20 66 6c 61 67 20  tSync.  ** flag 
12f49 69 73 20 73 65 74 2c 20 72 65 74 75 72 6e 20 77  is set, return w
12f4a 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79  ithout doing any
12f4b 74 68 69 6e 67 2e 20 54 68 65 20 70 63 61 63 68  thing. The pcach
12f4c 65 20 6c 61 79 65 72 20 77 69 6c 6c 0a 20 20 2a  e layer will.  *
12f4d 2a 20 6a 75 73 74 20 68 61 76 65 20 74 6f 20 67  * just have to g
12f4e 6f 20 61 68 65 61 64 20 61 6e 64 20 61 6c 6c 6f  o ahead and allo
12f4f 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20  cate a new page 
12f50 62 75 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f  buffer instead o
12f51 66 0a 20 20 2a 2a 20 72 65 75 73 69 6e 67 20 70  f.  ** reusing p
12f52 50 67 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 69  Pg..  **.  ** Si
12f53 6d 69 6c 61 72 6c 79 2c 20 69 66 20 74 68 65 20  milarly, if the 
12f54 70 61 67 65 72 20 68 61 73 20 61 6c 72 65 61 64  pager has alread
12f55 79 20 65 6e 74 65 72 65 64 20 74 68 65 20 65 72  y entered the er
12f56 72 6f 72 20 73 74 61 74 65 2c 20 64 6f 20 6e 6f  ror state, do no
12f57 74 0a 20 20 2a 2a 20 74 72 79 20 74 6f 20 77 72  t.  ** try to wr
12f58 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ite the contents
12f59 20 6f 66 20 70 50 67 20 74 6f 20 64 69 73 6b 2e   of pPg to disk.
12f5a 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45  .  */.  if( NEVE
12f5b 52 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  R(pPager->errCod
12f5c 65 29 0a 20 20 20 7c 7c 20 28 70 50 61 67 65 72  e).   || (pPager
12f5d 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 26 26 20 70  ->doNotSync && p
12f5e 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  Pg->flags&PGHDR_
12f5f 4e 45 45 44 5f 53 59 4e 43 29 0a 20 20 29 7b 0a  NEED_SYNC).  ){.
12f60 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
12f61 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  E_OK;.  }..  /* 
12f62 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c  Sync the journal
12f63 20 66 69 6c 65 20 69 66 20 72 65 71 75 69 72 65   file if require
12f64 64 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 67 2d  d. */.  if( pPg-
12f65 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45  >flags&PGHDR_NEE
12f66 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 72 63  D_SYNC ){.    rc
12f67 20 3d 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70   = syncJournal(p
12f68 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20  Pager);.    if( 
12f69 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
12f6a 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e   pPager->fullSyn
12f6b 63 20 26 26 20 0a 20 20 20 20 20 20 21 28 70 50  c && .      !(pP
12f6c 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
12f6d 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
12f6e 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 20 26 26 0a  MODE_MEMORY) &&.
12f6f 20 20 20 20 20 20 21 28 73 71 6c 69 74 65 33 4f        !(sqlite3O
12f70 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  sDeviceCharacter
12f71 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66  istics(pPager->f
12f72 64 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  d)&SQLITE_IOCAP_
12f73 53 41 46 45 5f 41 50 50 45 4e 44 29 0a 20 20 20  SAFE_APPEND).   
12f74 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72   ){.      pPager
12f75 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 20 20  ->nRec = 0;.    
12f76 20 20 72 63 20 3d 20 77 72 69 74 65 4a 6f 75 72    rc = writeJour
12f77 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29 3b 0a  nalHdr(pPager);.
12f78 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
12f79 49 66 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62  If the page numb
12f7a 65 72 20 6f 66 20 74 68 69 73 20 70 61 67 65 20  er of this page 
12f7b 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74  is larger than t
12f7c 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20  he current size 
12f7d 6f 66 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61  of.  ** the data
12f7e 62 61 73 65 20 69 6d 61 67 65 2c 20 69 74 20 6d  base image, it m
12f7f 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20 77 72  ay need to be wr
12f80 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 75 62  itten to the sub
12f81 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a 20 54  -journal..  ** T
12f82 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20 74  his is because t
12f83 68 65 20 63 61 6c 6c 20 74 6f 20 70 61 67 65 72  he call to pager
12f84 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73 74 28  _write_pagelist(
12f85 29 20 62 65 6c 6f 77 20 77 69 6c 6c 20 6e 6f 74  ) below will not
12f86 0a 20 20 2a 2a 20 61 63 74 75 61 6c 6c 79 20 77  .  ** actually w
12f87 72 69 74 65 20 64 61 74 61 20 74 6f 20 74 68 65  rite data to the
12f88 20 66 69 6c 65 20 69 6e 20 74 68 69 73 20 63 61   file in this ca
12f89 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 6f  se..  **.  ** Co
12f8a 6e 73 69 64 65 72 20 74 68 65 20 66 6f 6c 6c 6f  nsider the follo
12f8b 77 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 66  wing sequence of
12f8c 20 65 76 65 6e 74 73 3a 0a 20 20 2a 2a 0a 20 20   events:.  **.  
12f8d 2a 2a 20 20 20 42 45 47 49 4e 3b 0a 20 20 2a 2a  **   BEGIN;.  **
12f8e 20 20 20 20 20 3c 6a 6f 75 72 6e 61 6c 20 70 61       <journal pa
12f8f 67 65 20 58 3e 0a 20 20 2a 2a 20 20 20 20 20 3c  ge X>.  **     <
12f90 6d 6f 64 69 66 79 20 70 61 67 65 20 58 3e 0a 20  modify page X>. 
12f91 20 2a 2a 20 20 20 20 20 53 41 56 45 50 4f 49 4e   **     SAVEPOIN
12f92 54 20 73 70 3b 0a 20 20 2a 2a 20 20 20 20 20 20  T sp;.  **      
12f93 20 3c 73 68 72 69 6e 6b 20 64 61 74 61 62 61 73   <shrink databas
12f94 65 20 66 69 6c 65 20 74 6f 20 59 20 70 61 67 65  e file to Y page
12f95 73 3e 0a 20 20 2a 2a 20 20 20 20 20 20 20 70 61  s>.  **       pa
12f96 67 65 72 53 74 72 65 73 73 28 70 61 67 65 20 58  gerStress(page X
12f97 29 0a 20 20 2a 2a 20 20 20 20 20 52 4f 4c 4c 42  ).  **     ROLLB
12f98 41 43 4b 20 54 4f 20 73 70 3b 0a 20 20 2a 2a 0a  ACK TO sp;.  **.
12f99 20 20 2a 2a 20 49 66 20 28 58 3e 59 29 2c 20 74    ** If (X>Y), t
12f9a 68 65 6e 20 77 68 65 6e 20 70 61 67 65 72 53 74  hen when pagerSt
12f9b 72 65 73 73 20 69 73 20 63 61 6c 6c 65 64 20 70  ress is called p
12f9c 61 67 65 20 58 20 77 69 6c 6c 20 6e 6f 74 20 62  age X will not b
12f9d 65 20 77 72 69 74 74 65 6e 0a 20 20 2a 2a 20 6f  e written.  ** o
12f9e 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ut to the databa
12f9f 73 65 20 66 69 6c 65 2c 20 62 75 74 20 77 69 6c  se file, but wil
12fa0 6c 20 62 65 20 64 72 6f 70 70 65 64 20 66 72 6f  l be dropped fro
12fa1 6d 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 65  m the cache. The
12fa2 6e 2c 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e  n,.  ** followin
12fa3 67 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20  g the "ROLLBACK 
12fa4 54 4f 20 73 70 22 20 73 74 61 74 65 6d 65 6e 74  TO sp" statement
12fa5 2c 20 72 65 61 64 69 6e 67 20 70 61 67 65 20 58  , reading page X
12fa6 20 77 69 6c 6c 20 72 65 61 64 0a 20 20 2a 2a 20   will read.  ** 
12fa7 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 64 61  data from the da
12fa8 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69  tabase file. Thi
12fa9 73 20 77 69 6c 6c 20 62 65 20 74 68 65 20 63 6f  s will be the co
12faa 70 79 20 6f 66 20 70 61 67 65 20 58 20 61 73 20  py of page X as 
12fab 69 74 0a 20 20 2a 2a 20 77 61 73 20 77 68 65 6e  it.  ** was when
12fac 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
12fad 20 73 74 61 72 74 65 64 2c 20 6e 6f 74 20 61 73   started, not as
12fae 20 69 74 20 77 61 73 20 77 68 65 6e 20 22 53 41   it was when "SA
12faf 56 45 50 4f 49 4e 54 20 73 70 22 0a 20 20 2a 2a  VEPOINT sp".  **
12fb0 20 77 61 73 20 65 78 65 63 75 74 65 64 2e 0a 20   was executed.. 
12fb1 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 6f 6c   **.  ** The sol
12fb2 75 74 69 6f 6e 20 69 73 20 74 6f 20 77 72 69 74  ution is to writ
12fb3 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61  e the current da
12fb4 74 61 20 66 6f 72 20 70 61 67 65 20 58 20 69 6e  ta for page X in
12fb5 74 6f 20 74 68 65 20 0a 20 20 2a 2a 20 73 75 62  to the .  ** sub
12fb6 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 6f  -journal file no
12fb7 77 20 28 69 66 20 69 74 20 69 73 20 6e 6f 74 20  w (if it is not 
12fb8 61 6c 72 65 61 64 79 20 74 68 65 72 65 29 2c 20  already there), 
12fb9 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 0a  so that it will.
12fba 20 20 2a 2a 20 62 65 20 72 65 73 74 6f 72 65 64    ** be restored
12fbb 20 74 6f 20 69 74 73 20 63 75 72 72 65 6e 74 20   to its current 
12fbc 76 61 6c 75 65 20 77 68 65 6e 20 74 68 65 20 22  value when the "
12fbd 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 22 20  ROLLBACK TO sp" 
12fbe 69 73 20 0a 20 20 2a 2a 20 65 78 65 63 75 74 65  is .  ** execute
12fbf 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45  d..  */.  if( NE
12fc0 56 45 52 28 0a 20 20 20 20 20 20 72 63 3d 3d 53  VER(.      rc==S
12fc1 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 67 2d  QLITE_OK && pPg-
12fc2 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62  >pgno>pPager->db
12fc3 53 69 7a 65 20 26 26 20 73 75 62 6a 52 65 71 75  Size && subjRequ
12fc4 69 72 65 73 50 61 67 65 28 70 50 67 29 0a 20 20  iresPage(pPg).  
12fc5 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 75  ) ){.    rc = su
12fc6 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 50 67  bjournalPage(pPg
12fc7 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 69  );.  }..  /* Wri
12fc8 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  te the contents 
12fc9 6f 66 20 74 68 65 20 70 61 67 65 20 6f 75 74 20  of the page out 
12fca 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
12fcb 66 69 6c 65 2e 20 2a 2f 0a 20 20 69 66 28 20 72  file. */.  if( r
12fcc 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
12fcd 20 20 20 20 70 50 67 2d 3e 70 44 69 72 74 79 20      pPg->pDirty 
12fce 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 70 61  = 0;.    rc = pa
12fcf 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69  ger_write_pageli
12fd0 73 74 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20  st(pPg);.  }..  
12fd1 2f 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65  /* Mark the page
12fd2 20 61 73 20 63 6c 65 61 6e 2e 20 2a 2f 0a 20 20   as clean. */.  
12fd3 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
12fd4 4b 20 29 7b 0a 20 20 20 20 50 41 47 45 52 54 52  K ){.    PAGERTR
12fd5 41 43 45 28 28 22 53 54 52 45 53 53 20 25 64 20  ACE(("STRESS %d 
12fd6 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45  page %d\n", PAGE
12fd7 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67  RID(pPager), pPg
12fd8 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 73 71  ->pgno));.    sq
12fd9 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43  lite3PcacheMakeC
12fda 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 7d 0a 0a  lean(pPg);.  }..
12fdb 20 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65    return pager_e
12fdc 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29  rror(pPager, rc)
12fdd 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  ;.}.../*.** Allo
12fde 63 61 74 65 20 61 6e 64 20 69 6e 69 74 69 61 6c  cate and initial
12fdf 69 7a 65 20 61 20 6e 65 77 20 50 61 67 65 72 20  ize a new Pager 
12fe0 6f 62 6a 65 63 74 20 61 6e 64 20 70 75 74 20 61  object and put a
12fe1 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 0a 2a   pointer to it.*
12fe2 2a 20 69 6e 20 2a 70 70 50 61 67 65 72 2e 20 54  * in *ppPager. T
12fe3 68 65 20 70 61 67 65 72 20 73 68 6f 75 6c 64 20  he pager should 
12fe4 65 76 65 6e 74 75 61 6c 6c 79 20 62 65 20 66 72  eventually be fr
12fe5 65 65 64 20 62 79 20 70 61 73 73 69 6e 67 20 69  eed by passing i
12fe6 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 50  t.** to sqlite3P
12fe7 61 67 65 72 43 6c 6f 73 65 28 29 2e 0a 2a 2a 0a  agerClose()..**.
12fe8 2a 2a 20 54 68 65 20 7a 46 69 6c 65 6e 61 6d 65  ** The zFilename
12fe9 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
12fea 20 70 61 74 68 20 74 6f 20 74 68 65 20 64 61 74   path to the dat
12feb 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6f 70  abase file to op
12fec 65 6e 2e 0a 2a 2a 20 49 66 20 7a 46 69 6c 65 6e  en..** If zFilen
12fed 61 6d 65 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e  ame is NULL then
12fee 20 61 20 72 61 6e 64 6f 6d 6c 79 2d 6e 61 6d 65   a randomly-name
12fef 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  d temporary file
12ff0 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 61   is created.** a
12ff1 6e 64 20 75 73 65 64 20 61 73 20 74 68 65 20 66  nd used as the f
12ff2 69 6c 65 20 74 6f 20 62 65 20 63 61 63 68 65 64  ile to be cached
12ff3 2e 20 54 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  . Temporary file
12ff4 73 20 61 72 65 20 62 65 20 64 65 6c 65 74 65 64  s are be deleted
12ff5 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  .** automaticall
12ff6 79 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 20  y when they are 
12ff7 63 6c 6f 73 65 64 2e 20 49 66 20 7a 46 69 6c 65  closed. If zFile
12ff8 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79  name is ":memory
12ff9 3a 22 20 74 68 65 6e 20 0a 2a 2a 20 61 6c 6c 20  :" then .** all 
12ffa 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 68  information is h
12ffb 65 6c 64 20 69 6e 20 63 61 63 68 65 2e 20 49 74  eld in cache. It
12ffc 20 69 73 20 6e 65 76 65 72 20 77 72 69 74 74 65   is never writte
12ffd 6e 20 74 6f 20 64 69 73 6b 2e 20 0a 2a 2a 20 54  n to disk. .** T
12ffe 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20  his can be used 
12fff 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20  to implement an 
13000 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
13001 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 45  se..**.** The nE
13002 78 74 72 61 20 70 61 72 61 6d 65 74 65 72 20 73  xtra parameter s
13003 70 65 63 69 66 69 65 73 20 74 68 65 20 6e 75 6d  pecifies the num
13004 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
13005 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 0a  space allocated.
13006 2a 2a 20 61 6c 6f 6e 67 20 77 69 74 68 20 65 61  ** along with ea
13007 63 68 20 70 61 67 65 20 72 65 66 65 72 65 6e 63  ch page referenc
13008 65 2e 20 54 68 69 73 20 73 70 61 63 65 20 69 73  e. This space is
13009 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 68   available to th
1300a 65 20 75 73 65 72 0a 2a 2a 20 76 69 61 20 74 68  e user.** via th
1300b 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  e sqlite3PagerGe
1300c 74 45 78 74 72 61 28 29 20 41 50 49 2e 0a 2a 2a  tExtra() API..**
1300d 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72  .** The flags ar
1300e 67 75 6d 65 6e 74 20 69 73 20 75 73 65 64 20 74  gument is used t
1300f 6f 20 73 70 65 63 69 66 79 20 70 72 6f 70 65 72  o specify proper
13010 74 69 65 73 20 74 68 61 74 20 61 66 66 65 63 74  ties that affect
13011 20 74 68 65 0a 2a 2a 20 6f 70 65 72 61 74 69 6f   the.** operatio
13012 6e 20 6f 66 20 74 68 65 20 70 61 67 65 72 2e 20  n of the pager. 
13013 49 74 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73  It should be pas
13014 73 65 64 20 73 6f 6d 65 20 62 69 74 77 69 73 65  sed some bitwise
13015 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 0a 2a 2a 20   combination.** 
13016 6f 66 20 74 68 65 20 50 41 47 45 52 5f 4f 4d 49  of the PAGER_OMI
13017 54 5f 4a 4f 55 52 4e 41 4c 20 61 6e 64 20 50 41  T_JOURNAL and PA
13018 47 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20  GER_NO_READLOCK 
13019 66 6c 61 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  flags..**.** The
1301a 20 76 66 73 46 6c 61 67 73 20 70 61 72 61 6d 65   vfsFlags parame
1301b 74 65 72 20 69 73 20 61 20 62 69 74 6d 61 73 6b  ter is a bitmask
1301c 20 74 6f 20 70 61 73 73 20 74 6f 20 74 68 65 20   to pass to the 
1301d 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 0a  flags parameter.
1301e 2a 2a 20 6f 66 20 74 68 65 20 78 4f 70 65 6e 28  ** of the xOpen(
1301f 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20  ) method of the 
13020 73 75 70 70 6c 69 65 64 20 56 46 53 20 77 68 65  supplied VFS whe
13021 6e 20 6f 70 65 6e 69 6e 67 20 66 69 6c 65 73 2e  n opening files.
13022 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70   .**.** If the p
13023 61 67 65 72 20 6f 62 6a 65 63 74 20 69 73 20 61  ager object is a
13024 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65  llocated and the
13025 20 73 70 65 63 69 66 69 65 64 20 66 69 6c 65 20   specified file 
13026 6f 70 65 6e 65 64 20 0a 2a 2a 20 73 75 63 63 65  opened .** succe
13027 73 73 66 75 6c 6c 79 2c 20 53 51 4c 49 54 45 5f  ssfully, SQLITE_
13028 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  OK is returned a
13029 6e 64 20 2a 70 70 50 61 67 65 72 20 73 65 74 20  nd *ppPager set 
1302a 74 6f 20 70 6f 69 6e 74 20 74 6f 0a 2a 2a 20 74  to point to.** t
1302b 68 65 20 6e 65 77 20 70 61 67 65 72 20 6f 62 6a  he new pager obj
1302c 65 63 74 2e 20 49 66 20 61 6e 20 65 72 72 6f 72  ect. If an error
1302d 20 6f 63 63 75 72 73 2c 20 2a 70 70 50 61 67 65   occurs, *ppPage
1302e 72 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c  r is set to NULL
1302f 0a 2a 2a 20 61 6e 64 20 65 72 72 6f 72 20 63 6f  .** and error co
13030 64 65 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69  de returned. Thi
13031 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72  s function may r
13032 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
13033 45 4d 0a 2a 2a 20 28 73 71 6c 69 74 65 33 4d 61  EM.** (sqlite3Ma
13034 6c 6c 6f 63 28 29 20 69 73 20 75 73 65 64 20 74  lloc() is used t
13035 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72  o allocate memor
13036 79 29 2c 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f  y), SQLITE_CANTO
13037 50 45 4e 20 6f 72 20 0a 2a 2a 20 76 61 72 69 6f  PEN or .** vario
13038 75 73 20 53 51 4c 49 54 45 5f 49 4f 5f 58 58 58  us SQLITE_IO_XXX
13039 20 65 72 72 6f 72 73 2e 0a 2a 2f 0a 53 51 4c 49   errors..*/.SQLI
1303a 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1303b 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28  qlite3PagerOpen(
1303c 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
1303d 70 56 66 73 2c 20 20 20 20 20 20 20 2f 2a 20 54  pVfs,       /* T
1303e 68 65 20 76 69 72 74 75 61 6c 20 66 69 6c 65 20  he virtual file 
1303f 73 79 73 74 65 6d 20 74 6f 20 75 73 65 20 2a 2f  system to use */
13040 0a 20 20 50 61 67 65 72 20 2a 2a 70 70 50 61 67  .  Pager **ppPag
13041 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  er,         /* O
13042 55 54 3a 20 52 65 74 75 72 6e 20 74 68 65 20 50  UT: Return the P
13043 61 67 65 72 20 73 74 72 75 63 74 75 72 65 20 68  ager structure h
13044 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ere */.  const c
13045 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20  har *zFilename, 
13046 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
13047 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74   database file t
13048 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20  o open */.  int 
13049 6e 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 20  nExtra,         
1304a 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 62 79       /* Extra by
1304b 74 65 73 20 61 70 70 65 6e 64 20 74 6f 20 65 61  tes append to ea
1304c 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67  ch in-memory pag
1304d 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  e */.  int flags
1304e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1304f 2f 2a 20 66 6c 61 67 73 20 63 6f 6e 74 72 6f 6c  /* flags control
13050 6c 69 6e 67 20 74 68 69 73 20 66 69 6c 65 20 2a  ling this file *
13051 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73  /.  int vfsFlags
13052 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
13053 66 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72  flags passed thr
13054 6f 75 67 68 20 74 6f 20 73 71 6c 69 74 65 33 5f  ough to sqlite3_
13055 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20  vfs.xOpen() */. 
13056 20 76 6f 69 64 20 28 2a 78 52 65 69 6e 69 74 29   void (*xReinit)
13057 28 44 62 50 61 67 65 2a 29 20 2f 2a 20 46 75 6e  (DbPage*) /* Fun
13058 63 74 69 6f 6e 20 74 6f 20 72 65 69 6e 69 74 69  ction to reiniti
13059 61 6c 69 7a 65 20 70 61 67 65 73 20 2a 2f 0a 29  alize pages */.)
1305a 7b 0a 20 20 75 38 20 2a 70 50 74 72 3b 0a 20 20  {.  u8 *pPtr;.  
1305b 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20  Pager *pPager = 
1305c 30 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65  0;       /* Page
1305d 72 20 6f 62 6a 65 63 74 20 74 6f 20 61 6c 6c 6f  r object to allo
1305e 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20  cate and return 
1305f 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  */.  int rc = SQ
13060 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a  LITE_OK;      /*
13061 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
13062 20 20 69 6e 74 20 74 65 6d 70 46 69 6c 65 20 3d    int tempFile =
13063 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72   0;        /* Tr
13064 75 65 20 66 6f 72 20 74 65 6d 70 20 66 69 6c 65  ue for temp file
13065 73 20 28 69 6e 63 6c 2e 20 69 6e 2d 6d 65 6d 6f  s (incl. in-memo
13066 72 79 20 66 69 6c 65 73 29 20 2a 2f 0a 20 20 69  ry files) */.  i
13067 6e 74 20 6d 65 6d 44 62 20 3d 20 30 3b 20 20 20  nt memDb = 0;   
13068 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
13069 69 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e  if this is an in
1306a 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 20 2a 2f 0a  -memory file */.
1306b 20 20 69 6e 74 20 72 65 61 64 4f 6e 6c 79 20 3d    int readOnly =
1306c 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72   0;        /* Tr
1306d 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20  ue if this is a 
1306e 72 65 61 64 2d 6f 6e 6c 79 20 66 69 6c 65 20 2a  read-only file *
1306f 2f 0a 20 20 69 6e 74 20 6a 6f 75 72 6e 61 6c 46  /.  int journalF
13070 69 6c 65 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20  ileSize;     /* 
13071 42 79 74 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74  Bytes to allocat
13072 65 20 66 6f 72 20 65 61 63 68 20 6a 6f 75 72 6e  e for each journ
13073 61 6c 20 66 64 20 2a 2f 0a 20 20 63 68 61 72 20  al fd */.  char 
13074 2a 7a 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b 20  *zPathname = 0; 
13075 20 20 20 20 2f 2a 20 46 75 6c 6c 20 70 61 74 68      /* Full path
13076 20 74 6f 20 64 61 74 61 62 61 73 65 20 66 69 6c   to database fil
13077 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 74 68  e */.  int nPath
13078 6e 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20  name = 0;       
13079 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
1307a 65 73 20 69 6e 20 7a 50 61 74 68 6e 61 6d 65 20  es in zPathname 
1307b 2a 2f 0a 20 20 69 6e 74 20 75 73 65 4a 6f 75 72  */.  int useJour
1307c 6e 61 6c 20 3d 20 28 66 6c 61 67 73 20 26 20 50  nal = (flags & P
1307d 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41  AGER_OMIT_JOURNA
1307e 4c 29 3d 3d 30 3b 20 2f 2a 20 46 61 6c 73 65 20  L)==0; /* False 
1307f 74 6f 20 6f 6d 69 74 20 6a 6f 75 72 6e 61 6c 20  to omit journal 
13080 2a 2f 0a 20 20 69 6e 74 20 6e 6f 52 65 61 64 6c  */.  int noReadl
13081 6f 63 6b 20 3d 20 28 66 6c 61 67 73 20 26 20 50  ock = (flags & P
13082 41 47 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b  AGER_NO_READLOCK
13083 29 21 3d 30 3b 20 20 2f 2a 20 54 72 75 65 20 74  )!=0;  /* True t
13084 6f 20 6f 6d 69 74 20 72 65 61 64 2d 6c 6f 63 6b  o omit read-lock
13085 20 2a 2f 0a 20 20 69 6e 74 20 70 63 61 63 68 65   */.  int pcache
13086 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 50 63  Size = sqlite3Pc
13087 61 63 68 65 53 69 7a 65 28 29 3b 20 20 20 20 20  acheSize();     
13088 20 20 2f 2a 20 42 79 74 65 73 20 74 6f 20 61 6c    /* Bytes to al
13089 6c 6f 63 61 74 65 20 66 6f 72 20 50 43 61 63 68  locate for PCach
1308a 65 20 2a 2f 0a 20 20 75 31 36 20 73 7a 50 61 67  e */.  u16 szPag
1308b 65 44 66 6c 74 20 3d 20 53 51 4c 49 54 45 5f 44  eDflt = SQLITE_D
1308c 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
1308d 3b 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 70 61  ;  /* Default pa
1308e 67 65 20 73 69 7a 65 20 2a 2f 0a 0a 20 20 2f 2a  ge size */..  /*
1308f 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20   Figure out how 
13090 6d 75 63 68 20 73 70 61 63 65 20 69 73 20 72 65  much space is re
13091 71 75 69 72 65 64 20 66 6f 72 20 65 61 63 68 20  quired for each 
13092 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e  journal file-han
13093 64 6c 65 0a 20 20 2a 2a 20 28 74 68 65 72 65 20  dle.  ** (there 
13094 61 72 65 20 74 77 6f 20 6f 66 20 74 68 65 6d 2c  are two of them,
13095 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61   the main journa
13096 6c 20 61 6e 64 20 74 68 65 20 73 75 62 2d 6a 6f  l and the sub-jo
13097 75 72 6e 61 6c 29 2e 20 54 68 69 73 0a 20 20 2a  urnal). This.  *
13098 2a 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d  * is the maximum
13099 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20   space required 
1309a 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  for an in-memory
1309b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61   journal file ha
1309c 6e 64 6c 65 20 0a 20 20 2a 2a 20 61 6e 64 20 61  ndle .  ** and a
1309d 20 72 65 67 75 6c 61 72 20 6a 6f 75 72 6e 61 6c   regular journal
1309e 20 66 69 6c 65 2d 68 61 6e 64 6c 65 2e 20 4e 6f   file-handle. No
1309f 74 65 20 74 68 61 74 20 61 20 22 72 65 67 75 6c  te that a "regul
130a0 61 72 20 6a 6f 75 72 6e 61 6c 2d 68 61 6e 64 6c  ar journal-handl
130a1 65 22 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20 61  e".  ** may be a
130a2 20 77 72 61 70 70 65 72 20 63 61 70 61 62 6c 65   wrapper capable
130a3 20 6f 66 20 63 61 63 68 69 6e 67 20 74 68 65 20   of caching the 
130a4 66 69 72 73 74 20 70 6f 72 74 69 6f 6e 20 6f 66  first portion of
130a5 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a   the journal.  *
130a6 2a 20 66 69 6c 65 20 69 6e 20 6d 65 6d 6f 72 79  * file in memory
130a7 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
130a8 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f  e atomic-write o
130a9 70 74 69 6d 69 7a 61 74 69 6f 6e 20 28 73 65 65  ptimization (see
130aa 20 0a 20 20 2a 2a 20 73 6f 75 72 63 65 20 66 69   .  ** source fi
130ab 6c 65 20 6a 6f 75 72 6e 61 6c 2e 63 29 2e 0a 20  le journal.c).. 
130ac 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
130ad 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56 66  3JournalSize(pVf
130ae 73 29 3e 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75  s)>sqlite3MemJou
130af 72 6e 61 6c 53 69 7a 65 28 29 20 29 7b 0a 20 20  rnalSize() ){.  
130b0 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a    journalFileSiz
130b1 65 20 3d 20 52 4f 55 4e 44 38 28 73 71 6c 69 74  e = ROUND8(sqlit
130b2 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56  e3JournalSize(pV
130b3 66 73 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  fs));.  }else{. 
130b4 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69     journalFileSi
130b5 7a 65 20 3d 20 52 4f 55 4e 44 38 28 73 71 6c 69  ze = ROUND8(sqli
130b6 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a  te3MemJournalSiz
130b7 65 28 29 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  e());.  }..  /* 
130b8 53 65 74 20 74 68 65 20 6f 75 74 70 75 74 20 76  Set the output v
130b9 61 72 69 61 62 6c 65 20 74 6f 20 4e 55 4c 4c 20  ariable to NULL 
130ba 69 6e 20 63 61 73 65 20 61 6e 20 65 72 72 6f 72  in case an error
130bb 20 6f 63 63 75 72 73 2e 20 2a 2f 0a 20 20 2a 70   occurs. */.  *p
130bc 70 50 61 67 65 72 20 3d 20 30 3b 0a 0a 20 20 2f  pPager = 0;..  /
130bd 2a 20 43 6f 6d 70 75 74 65 20 61 6e 64 20 73 74  * Compute and st
130be 6f 72 65 20 74 68 65 20 66 75 6c 6c 20 70 61 74  ore the full pat
130bf 68 6e 61 6d 65 20 69 6e 20 61 6e 20 61 6c 6c 6f  hname in an allo
130c0 63 61 74 65 64 20 62 75 66 66 65 72 20 70 6f 69  cated buffer poi
130c1 6e 74 65 64 0a 20 20 2a 2a 20 74 6f 20 62 79 20  nted.  ** to by 
130c2 7a 50 61 74 68 6e 61 6d 65 2c 20 6c 65 6e 67 74  zPathname, lengt
130c3 68 20 6e 50 61 74 68 6e 61 6d 65 2e 20 4f 72 2c  h nPathname. Or,
130c4 20 69 66 20 74 68 69 73 20 69 73 20 61 20 74 65   if this is a te
130c5 6d 70 6f 72 61 72 79 20 66 69 6c 65 2c 0a 20 20  mporary file,.  
130c6 2a 2a 20 6c 65 61 76 65 20 62 6f 74 68 20 6e 50  ** leave both nP
130c7 61 74 68 6e 61 6d 65 20 61 6e 64 20 7a 50 61 74  athname and zPat
130c8 68 6e 61 6d 65 20 73 65 74 20 74 6f 20 30 2e 0a  hname set to 0..
130c9 20 20 2a 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65    */.  if( zFile
130ca 6e 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d  name && zFilenam
130cb 65 5b 30 5d 20 29 7b 0a 20 20 20 20 6e 50 61 74  e[0] ){.    nPat
130cc 68 6e 61 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78  hname = pVfs->mx
130cd 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20  Pathname+1;.    
130ce 7a 50 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69  zPathname = sqli
130cf 74 65 33 4d 61 6c 6c 6f 63 28 6e 50 61 74 68 6e  te3Malloc(nPathn
130d0 61 6d 65 2a 32 29 3b 0a 20 20 20 20 69 66 28 20  ame*2);.    if( 
130d1 7a 50 61 74 68 6e 61 6d 65 3d 3d 30 20 29 7b 0a  zPathname==0 ){.
130d2 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
130d3 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
130d4 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
130d5 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20  OMIT_MEMORYDB.  
130d6 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a 46 69    if( strcmp(zFi
130d7 6c 65 6e 61 6d 65 2c 22 3a 6d 65 6d 6f 72 79 3a  lename,":memory:
130d8 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d  ")==0 ){.      m
130d9 65 6d 44 62 20 3d 20 31 3b 0a 20 20 20 20 20 20  emDb = 1;.      
130da 7a 50 61 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30  zPathname[0] = 0
130db 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64  ;.    }else.#end
130dc 69 66 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 7a  if.    {.      z
130dd 50 61 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b  Pathname[0] = 0;
130de 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 69 6e   /* Make sure in
130df 69 74 69 61 6c 69 7a 65 64 20 65 76 65 6e 20 69  itialized even i
130e0 66 20 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 29  f FullPathname()
130e1 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 20 20   fails */.      
130e2 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 75  rc = sqlite3OsFu
130e3 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c  llPathname(pVfs,
130e4 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 50 61 74   zFilename, nPat
130e5 68 6e 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65  hname, zPathname
130e6 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 50  );.    }..    nP
130e7 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65  athname = sqlite
130e8 33 53 74 72 6c 65 6e 33 30 28 7a 50 61 74 68 6e  3Strlen30(zPathn
130e9 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ame);.    if( rc
130ea 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e  ==SQLITE_OK && n
130eb 50 61 74 68 6e 61 6d 65 2b 38 3e 70 56 66 73 2d  Pathname+8>pVfs-
130ec 3e 6d 78 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20  >mxPathname ){. 
130ed 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61       /* This bra
130ee 6e 63 68 20 69 73 20 74 61 6b 65 6e 20 77 68 65  nch is taken whe
130ef 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 70 61  n the journal pa
130f0 74 68 20 72 65 71 75 69 72 65 64 20 62 79 0a 20  th required by. 
130f1 20 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61       ** the data
130f2 62 61 73 65 20 62 65 69 6e 67 20 6f 70 65 6e 65  base being opene
130f3 64 20 77 69 6c 6c 20 62 65 20 6d 6f 72 65 20 74  d will be more t
130f4 68 61 6e 20 70 56 66 73 2d 3e 6d 78 50 61 74 68  han pVfs->mxPath
130f5 6e 61 6d 65 0a 20 20 20 20 20 20 2a 2a 20 62 79  name.      ** by
130f6 74 65 73 20 69 6e 20 6c 65 6e 67 74 68 2e 20 54  tes in length. T
130f7 68 69 73 20 6d 65 61 6e 73 20 74 68 65 20 64 61  his means the da
130f8 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 65  tabase cannot be
130f9 20 6f 70 65 6e 65 64 2c 0a 20 20 20 20 20 20 2a   opened,.      *
130fa 2a 20 61 73 20 69 74 20 77 69 6c 6c 20 6e 6f 74  * as it will not
130fb 20 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20   be possible to 
130fc 6f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  open the journal
130fd 20 66 69 6c 65 20 6f 72 20 65 76 65 6e 0a 20 20   file or even.  
130fe 20 20 20 20 2a 2a 20 63 68 65 63 6b 20 66 6f 72      ** check for
130ff 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62   a hot-journal b
13100 65 66 6f 72 65 20 72 65 61 64 69 6e 67 2e 0a 20  efore reading.. 
13101 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63       */.      rc
13102 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50   = SQLITE_CANTOP
13103 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  EN;.    }.    if
13104 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
13105 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
13106 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 29  _free(zPathname)
13107 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
13108 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  c;.    }.  }..  
13109 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f  /* Allocate memo
1310a 72 79 20 66 6f 72 20 74 68 65 20 50 61 67 65 72  ry for the Pager
1310b 20 73 74 72 75 63 74 75 72 65 2c 20 50 43 61 63   structure, PCac
1310c 68 65 20 6f 62 6a 65 63 74 2c 20 74 68 65 0a 20  he object, the. 
1310d 20 2a 2a 20 74 68 72 65 65 20 66 69 6c 65 20 64   ** three file d
1310e 65 73 63 72 69 70 74 6f 72 73 2c 20 74 68 65 20  escriptors, the 
1310f 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6e 61  database file na
13110 6d 65 20 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e  me and the journ
13111 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20 6e 61  al .  ** file na
13112 6d 65 2e 20 54 68 65 20 6c 61 79 6f 75 74 20 69  me. The layout i
13113 6e 20 6d 65 6d 6f 72 79 20 69 73 20 61 73 20 66  n memory is as f
13114 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a 20 20 2a  ollows:.  **.  *
13115 2a 20 20 20 20 20 50 61 67 65 72 20 6f 62 6a 65  *     Pager obje
13116 63 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ct              
13117 20 20 20 20 20 20 28 73 69 7a 65 6f 66 28 50 61        (sizeof(Pa
13118 67 65 72 29 20 62 79 74 65 73 29 0a 20 20 2a 2a  ger) bytes).  **
13119 20 20 20 20 20 50 43 61 63 68 65 20 6f 62 6a 65       PCache obje
1311a 63 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ct              
1311b 20 20 20 20 20 28 73 71 6c 69 74 65 33 50 63 61       (sqlite3Pca
1311c 63 68 65 53 69 7a 65 28 29 20 62 79 74 65 73 29  cheSize() bytes)
1311d 0a 20 20 2a 2a 20 20 20 20 20 44 61 74 61 62 61  .  **     Databa
1311e 73 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 20  se file handle  
1311f 20 20 20 20 20 20 20 20 20 20 28 70 56 66 73 2d            (pVfs-
13120 3e 73 7a 4f 73 46 69 6c 65 20 62 79 74 65 73 29  >szOsFile bytes)
13121 0a 20 20 2a 2a 20 20 20 20 20 53 75 62 2d 6a 6f  .  **     Sub-jo
13122 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e 64 6c  urnal file handl
13123 65 20 20 20 20 20 20 20 20 20 28 6a 6f 75 72 6e  e         (journ
13124 61 6c 46 69 6c 65 53 69 7a 65 20 62 79 74 65 73  alFileSize bytes
13125 29 0a 20 20 2a 2a 20 20 20 20 20 4d 61 69 6e 20  ).  **     Main 
13126 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e  journal file han
13127 64 6c 65 20 20 20 20 20 20 20 20 28 6a 6f 75 72  dle        (jour
13128 6e 61 6c 46 69 6c 65 53 69 7a 65 20 62 79 74 65  nalFileSize byte
13129 73 29 0a 20 20 2a 2a 20 20 20 20 20 44 61 74 61  s).  **     Data
1312a 62 61 73 65 20 66 69 6c 65 20 6e 61 6d 65 20 20  base file name  
1312b 20 20 20 20 20 20 20 20 20 20 20 20 28 6e 50 61              (nPa
1312c 74 68 6e 61 6d 65 2b 31 20 62 79 74 65 73 29 0a  thname+1 bytes).
1312d 20 20 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c    **     Journal
1312e 20 66 69 6c 65 20 6e 61 6d 65 20 20 20 20 20 20   file name      
1312f 20 20 20 20 20 20 20 20 20 28 6e 50 61 74 68 6e           (nPathn
13130 61 6d 65 2b 38 2b 31 20 62 79 74 65 73 29 0a 20  ame+8+1 bytes). 
13131 20 2a 2f 0a 20 20 70 50 74 72 20 3d 20 28 75 38   */.  pPtr = (u8
13132 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   *)sqlite3Malloc
13133 5a 65 72 6f 28 0a 20 20 20 20 52 4f 55 4e 44 38  Zero(.    ROUND8
13134 28 73 69 7a 65 6f 66 28 2a 70 50 61 67 65 72 29  (sizeof(*pPager)
13135 29 20 2b 20 20 20 20 20 20 2f 2a 20 50 61 67 65  ) +      /* Page
13136 72 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  r structure */. 
13137 20 20 20 52 4f 55 4e 44 38 28 70 63 61 63 68 65     ROUND8(pcache
13138 53 69 7a 65 29 20 2b 20 20 20 20 20 20 20 20 20  Size) +         
13139 20 20 2f 2a 20 50 43 61 63 68 65 20 6f 62 6a 65    /* PCache obje
1313a 63 74 20 2a 2f 0a 20 20 20 20 52 4f 55 4e 44 38  ct */.    ROUND8
1313b 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29  (pVfs->szOsFile)
1313c 20 2b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20   +       /* The 
1313d 6d 61 69 6e 20 64 62 20 66 69 6c 65 20 2a 2f 0a  main db file */.
1313e 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53      journalFileS
1313f 69 7a 65 20 2a 20 32 20 2b 20 20 20 20 20 20 20  ize * 2 +       
13140 20 20 20 2f 2a 20 54 68 65 20 74 77 6f 20 6a 6f     /* The two jo
13141 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 20 0a  urnal files */ .
13142 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 2b 20      nPathname + 
13143 31 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 20  1 +             
13144 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20     /* zFilename 
13145 2a 2f 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65  */.    nPathname
13146 20 2b 20 38 20 2b 20 31 20 20 20 20 20 20 20 20   + 8 + 1        
13147 20 20 20 20 20 20 2f 2a 20 7a 4a 6f 75 72 6e 61        /* zJourna
13148 6c 20 2a 2f 0a 20 20 29 3b 0a 20 20 61 73 73 65  l */.  );.  asse
13149 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41  rt( EIGHT_BYTE_A
1314a 4c 49 47 4e 4d 45 4e 54 28 53 51 4c 49 54 45 5f  LIGNMENT(SQLITE_
1314b 49 4e 54 5f 54 4f 5f 50 54 52 28 6a 6f 75 72 6e  INT_TO_PTR(journ
1314c 61 6c 46 69 6c 65 53 69 7a 65 29 29 20 29 3b 0a  alFileSize)) );.
1314d 20 20 69 66 28 20 21 70 50 74 72 20 29 7b 0a 20    if( !pPtr ){. 
1314e 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
1314f 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20  zPathname);.    
13150 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
13151 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65  MEM;.  }.  pPage
13152 72 20 3d 20 20 20 20 20 20 20 20 20 20 20 20 20  r =             
13153 20 28 50 61 67 65 72 2a 29 28 70 50 74 72 29 3b   (Pager*)(pPtr);
13154 0a 20 20 70 50 61 67 65 72 2d 3e 70 50 43 61 63  .  pPager->pPCac
13155 68 65 20 3d 20 20 20 20 28 50 43 61 63 68 65 2a  he =    (PCache*
13156 29 28 70 50 74 72 20 2b 3d 20 52 4f 55 4e 44 38  )(pPtr += ROUND8
13157 28 73 69 7a 65 6f 66 28 2a 70 50 61 67 65 72 29  (sizeof(*pPager)
13158 29 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 66 64  ));.  pPager->fd
13159 20 3d 20 20 20 28 73 71 6c 69 74 65 33 5f 66 69   =   (sqlite3_fi
1315a 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 52 4f 55  le*)(pPtr += ROU
1315b 4e 44 38 28 70 63 61 63 68 65 53 69 7a 65 29 29  ND8(pcacheSize))
1315c 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 6a 66 64  ;.  pPager->sjfd
1315d 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65   = (sqlite3_file
1315e 2a 29 28 70 50 74 72 20 2b 3d 20 52 4f 55 4e 44  *)(pPtr += ROUND
1315f 38 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65  8(pVfs->szOsFile
13160 29 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 66  ));.  pPager->jf
13161 64 20 3d 20 20 28 73 71 6c 69 74 65 33 5f 66 69  d =  (sqlite3_fi
13162 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 6a 6f 75  le*)(pPtr += jou
13163 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 3b 0a 20  rnalFileSize);. 
13164 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61   pPager->zFilena
13165 6d 65 20 3d 20 20 20 20 28 63 68 61 72 2a 29 28  me =    (char*)(
13166 70 50 74 72 20 2b 3d 20 6a 6f 75 72 6e 61 6c 46  pPtr += journalF
13167 69 6c 65 53 69 7a 65 29 3b 0a 20 20 61 73 73 65  ileSize);.  asse
13168 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41  rt( EIGHT_BYTE_A
13169 4c 49 47 4e 4d 45 4e 54 28 70 50 61 67 65 72 2d  LIGNMENT(pPager-
1316a 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 2f 2a 20 46  >jfd) );..  /* F
1316b 69 6c 6c 20 69 6e 20 74 68 65 20 50 61 67 65 72  ill in the Pager
1316c 2e 7a 46 69 6c 65 6e 61 6d 65 20 61 6e 64 20 50  .zFilename and P
1316d 61 67 65 72 2e 7a 4a 6f 75 72 6e 61 6c 20 62 75  ager.zJournal bu
1316e 66 66 65 72 73 2c 20 69 66 20 72 65 71 75 69 72  ffers, if requir
1316f 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 7a 50 61  ed. */.  if( zPa
13170 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20 70 50  thname ){.    pP
13171 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 3d  ager->zJournal =
13172 20 20 20 28 63 68 61 72 2a 29 28 70 50 74 72 20     (char*)(pPtr 
13173 2b 3d 20 6e 50 61 74 68 6e 61 6d 65 20 2b 20 31  += nPathname + 1
13174 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50  );.    memcpy(pP
13175 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c  ager->zFilename,
13176 20 7a 50 61 74 68 6e 61 6d 65 2c 20 6e 50 61 74   zPathname, nPat
13177 68 6e 61 6d 65 29 3b 0a 20 20 20 20 6d 65 6d 63  hname);.    memc
13178 70 79 28 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72  py(pPager->zJour
13179 6e 61 6c 2c 20 7a 50 61 74 68 6e 61 6d 65 2c 20  nal, zPathname, 
1317a 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20  nPathname);.    
1317b 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e  memcpy(&pPager->
1317c 7a 4a 6f 75 72 6e 61 6c 5b 6e 50 61 74 68 6e 61  zJournal[nPathna
1317d 6d 65 5d 2c 20 22 2d 6a 6f 75 72 6e 61 6c 22 2c  me], "-journal",
1317e 20 38 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61   8);.    if( pPa
1317f 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 5b 30  ger->zFilename[0
13180 5d 3d 3d 30 20 29 20 70 50 61 67 65 72 2d 3e 7a  ]==0 ) pPager->z
13181 4a 6f 75 72 6e 61 6c 5b 30 5d 20 3d 20 30 3b 0a  Journal[0] = 0;.
13182 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
13183 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 7d  (zPathname);.  }
13184 0a 20 20 70 50 61 67 65 72 2d 3e 70 56 66 73 20  .  pPager->pVfs 
13185 3d 20 70 56 66 73 3b 0a 20 20 70 50 61 67 65 72  = pVfs;.  pPager
13186 2d 3e 76 66 73 46 6c 61 67 73 20 3d 20 76 66 73  ->vfsFlags = vfs
13187 46 6c 61 67 73 3b 0a 0a 20 20 2f 2a 20 4f 70 65  Flags;..  /* Ope
13188 6e 20 74 68 65 20 70 61 67 65 72 20 66 69 6c 65  n the pager file
13189 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 46 69  ..  */.  if( zFi
1318a 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e  lename && zFilen
1318b 61 6d 65 5b 30 5d 20 26 26 20 21 6d 65 6d 44 62  ame[0] && !memDb
1318c 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 6f 75 74   ){.    int fout
1318d 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
1318e 20 20 20 20 20 20 20 20 20 2f 2a 20 56 46 53 20           /* VFS 
1318f 66 6c 61 67 73 20 72 65 74 75 72 6e 65 64 20 62  flags returned b
13190 79 20 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 20  y xOpen() */.   
13191 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f   rc = sqlite3OsO
13192 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72  pen(pVfs, pPager
13193 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 70 50 61  ->zFilename, pPa
13194 67 65 72 2d 3e 66 64 2c 20 76 66 73 46 6c 61 67  ger->fd, vfsFlag
13195 73 2c 20 26 66 6f 75 74 29 3b 0a 20 20 20 20 72  s, &fout);.    r
13196 65 61 64 4f 6e 6c 79 20 3d 20 28 66 6f 75 74 26  eadOnly = (fout&
13197 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
13198 4f 4e 4c 59 29 3b 0a 0a 20 20 20 20 2f 2a 20 49  ONLY);..    /* I
13199 66 20 74 68 65 20 66 69 6c 65 20 77 61 73 20 73  f the file was s
1319a 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e  uccessfully open
1319b 65 64 20 66 6f 72 20 72 65 61 64 2f 77 72 69 74  ed for read/writ
1319c 65 20 61 63 63 65 73 73 2c 0a 20 20 20 20 2a 2a  e access,.    **
1319d 20 63 68 6f 6f 73 65 20 61 20 64 65 66 61 75 6c   choose a defaul
1319e 74 20 70 61 67 65 20 73 69 7a 65 20 69 6e 20 63  t page size in c
1319f 61 73 65 20 77 65 20 68 61 76 65 20 74 6f 20 63  ase we have to c
131a0 72 65 61 74 65 20 74 68 65 0a 20 20 20 20 2a 2a  reate the.    **
131a1 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
131a2 54 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65  The default page
131a3 20 73 69 7a 65 20 69 73 20 74 68 65 20 6d 61 78   size is the max
131a4 69 6d 75 6d 20 6f 66 3a 0a 20 20 20 20 2a 2a 0a  imum of:.    **.
131a5 20 20 20 20 2a 2a 20 20 20 20 2b 20 53 51 4c 49      **    + SQLI
131a6 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  TE_DEFAULT_PAGE_
131a7 53 49 5a 45 2c 0a 20 20 20 20 2a 2a 20 20 20 20  SIZE,.    **    
131a8 2b 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75  + The value retu
131a9 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 4f  rned by sqlite3O
131aa 73 53 65 63 74 6f 72 53 69 7a 65 28 29 0a 20 20  sSectorSize().  
131ab 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 6c 61    **    + The la
131ac 72 67 65 73 74 20 70 61 67 65 20 73 69 7a 65 20  rgest page size 
131ad 74 68 61 74 20 63 61 6e 20 62 65 20 77 72 69 74  that can be writ
131ae 74 65 6e 20 61 74 6f 6d 69 63 61 6c 6c 79 2e 0a  ten atomically..
131af 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72      */.    if( r
131b0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
131b1 21 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20  !readOnly ){.   
131b2 20 20 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65     setSectorSize
131b3 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20  (pPager);.      
131b4 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 44 45  assert(SQLITE_DE
131b5 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3c  FAULT_PAGE_SIZE<
131b6 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41  =SQLITE_MAX_DEFA
131b7 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 29 3b 0a  ULT_PAGE_SIZE);.
131b8 20 20 20 20 20 20 69 66 28 20 73 7a 50 61 67 65        if( szPage
131b9 44 66 6c 74 3c 70 50 61 67 65 72 2d 3e 73 65 63  Dflt<pPager->sec
131ba 74 6f 72 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  torSize ){.     
131bb 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73     if( pPager->s
131bc 65 63 74 6f 72 53 69 7a 65 3e 53 51 4c 49 54 45  ectorSize>SQLITE
131bd 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47  _MAX_DEFAULT_PAG
131be 45 5f 53 49 5a 45 20 29 7b 0a 20 20 20 20 20 20  E_SIZE ){.      
131bf 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74 20 3d      szPageDflt =
131c0 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41   SQLITE_MAX_DEFA
131c1 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b 0a 20  ULT_PAGE_SIZE;. 
131c2 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
131c3 20 20 20 20 20 20 20 20 73 7a 50 61 67 65 44 66          szPageDf
131c4 6c 74 20 3d 20 28 75 31 36 29 70 50 61 67 65 72  lt = (u16)pPager
131c5 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20  ->sectorSize;.  
131c6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
131c7 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
131c8 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54  ABLE_ATOMIC_WRIT
131c9 45 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20  E.      {.      
131ca 20 20 69 6e 74 20 69 44 63 20 3d 20 73 71 6c 69    int iDc = sqli
131cb 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61  te3OsDeviceChara
131cc 63 74 65 72 69 73 74 69 63 73 28 70 50 61 67 65  cteristics(pPage
131cd 72 2d 3e 66 64 29 3b 0a 20 20 20 20 20 20 20 20  r->fd);.        
131ce 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 20 20  int ii;.        
131cf 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49 4f  assert(SQLITE_IO
131d0 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 3d 3d 28  CAP_ATOMIC512==(
131d1 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20 20 20 20  512>>8));.      
131d2 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f    assert(SQLITE_
131d3 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b 3d  IOCAP_ATOMIC64K=
131d4 3d 28 36 35 35 33 36 3e 3e 38 29 29 3b 0a 20 20  =(65536>>8));.  
131d5 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51 4c        assert(SQL
131d6 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f  ITE_MAX_DEFAULT_
131d7 50 41 47 45 5f 53 49 5a 45 3c 3d 36 35 35 33 36  PAGE_SIZE<=65536
131d8 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  );.        for(i
131d9 69 3d 73 7a 50 61 67 65 44 66 6c 74 3b 20 69 69  i=szPageDflt; ii
131da 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46  <=SQLITE_MAX_DEF
131db 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b 20  AULT_PAGE_SIZE; 
131dc 69 69 3d 69 69 2a 32 29 7b 0a 20 20 20 20 20 20  ii=ii*2){.      
131dd 20 20 20 20 69 66 28 20 69 44 63 26 28 53 51 4c      if( iDc&(SQL
131de 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
131df 7c 28 69 69 3e 3e 38 29 29 20 29 7b 0a 20 20 20  |(ii>>8)) ){.   
131e0 20 20 20 20 20 20 20 20 20 73 7a 50 61 67 65 44           szPageD
131e1 66 6c 74 20 3d 20 69 69 3b 0a 20 20 20 20 20 20  flt = ii;.      
131e2 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
131e3 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
131e4 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
131e5 20 20 2f 2a 20 49 66 20 61 20 74 65 6d 70 6f 72    /* If a tempor
131e6 61 72 79 20 66 69 6c 65 20 69 73 20 72 65 71 75  ary file is requ
131e7 65 73 74 65 64 2c 20 69 74 20 69 73 20 6e 6f 74  ested, it is not
131e8 20 6f 70 65 6e 65 64 20 69 6d 6d 65 64 69 61 74   opened immediat
131e9 65 6c 79 2e 0a 20 20 20 20 2a 2a 20 49 6e 20 74  ely..    ** In t
131ea 68 69 73 20 63 61 73 65 20 77 65 20 61 63 63 65  his case we acce
131eb 70 74 20 74 68 65 20 64 65 66 61 75 6c 74 20 70  pt the default p
131ec 61 67 65 20 73 69 7a 65 20 61 6e 64 20 64 65 6c  age size and del
131ed 61 79 20 61 63 74 75 61 6c 6c 79 0a 20 20 20 20  ay actually.    
131ee 2a 2a 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66  ** opening the f
131ef 69 6c 65 20 75 6e 74 69 6c 20 74 68 65 20 66 69  ile until the fi
131f0 72 73 74 20 63 61 6c 6c 20 74 6f 20 4f 73 57 72  rst call to OsWr
131f1 69 74 65 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20  ite()..    **.  
131f2 20 20 2a 2a 20 54 68 69 73 20 62 72 61 6e 63 68    ** This branch
131f3 20 69 73 20 61 6c 73 6f 20 72 75 6e 20 66 6f 72   is also run for
131f4 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
131f5 74 61 62 61 73 65 2e 20 41 6e 20 69 6e 2d 6d 65  tabase. An in-me
131f6 6d 6f 72 79 0a 20 20 20 20 2a 2a 20 64 61 74 61  mory.    ** data
131f7 62 61 73 65 20 69 73 20 74 68 65 20 73 61 6d 65  base is the same
131f8 20 61 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 20   as a temp-file 
131f9 74 68 61 74 20 69 73 20 6e 65 76 65 72 20 77 72  that is never wr
131fa 69 74 74 65 6e 20 6f 75 74 20 74 6f 0a 20 20 20  itten out to.   
131fb 20 2a 2a 20 64 69 73 6b 20 61 6e 64 20 75 73 65   ** disk and use
131fc 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 72  s an in-memory r
131fd 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e  ollback journal.
131fe 0a 20 20 20 20 2a 2f 20 0a 20 20 20 20 74 65 6d  .    */ .    tem
131ff 70 46 69 6c 65 20 3d 20 31 3b 0a 20 20 20 20 70  pFile = 1;.    p
13200 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50  Pager->state = P
13201 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 3b 0a  AGER_EXCLUSIVE;.
13202 20 20 20 20 72 65 61 64 4f 6e 6c 79 20 3d 20 28      readOnly = (
13203 76 66 73 46 6c 61 67 73 26 53 51 4c 49 54 45 5f  vfsFlags&SQLITE_
13204 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a  OPEN_READONLY);.
13205 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f    }..  /* The fo
13206 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 74 6f 20  llowing call to 
13207 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65  PagerSetPagesize
13208 28 29 20 73 65 72 76 65 73 20 74 6f 20 73 65 74  () serves to set
13209 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 0a 20   the value of . 
1320a 20 2a 2a 20 50 61 67 65 72 2e 70 61 67 65 53 69   ** Pager.pageSi
1320b 7a 65 20 61 6e 64 20 74 6f 20 61 6c 6c 6f 63 61  ze and to alloca
1320c 74 65 20 74 68 65 20 50 61 67 65 72 2e 70 54 6d  te the Pager.pTm
1320d 70 53 70 61 63 65 20 62 75 66 66 65 72 2e 0a 20  pSpace buffer.. 
1320e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   */.  if( rc==SQ
1320f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61  LITE_OK ){.    a
13210 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6d  ssert( pPager->m
13211 65 6d 44 62 3d 3d 30 20 29 3b 0a 20 20 20 20 72  emDb==0 );.    r
13212 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
13213 53 65 74 50 61 67 65 73 69 7a 65 28 70 50 61 67  SetPagesize(pPag
13214 65 72 2c 20 26 73 7a 50 61 67 65 44 66 6c 74 2c  er, &szPageDflt,
13215 20 2d 31 29 3b 0a 20 20 20 20 74 65 73 74 63 61   -1);.    testca
13216 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  se( rc!=SQLITE_O
13217 4b 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  K );.  }..  /* I
13218 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
13219 72 65 64 20 69 6e 20 65 69 74 68 65 72 20 6f 66  red in either of
1321a 20 74 68 65 20 62 6c 6f 63 6b 73 20 61 62 6f 76   the blocks abov
1321b 65 2c 20 66 72 65 65 20 74 68 65 20 0a 20 20 2a  e, free the .  *
1321c 2a 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72  * Pager structur
1321d 65 20 61 6e 64 20 63 6c 6f 73 65 20 74 68 65 20  e and close the 
1321e 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  file..  */.  if(
1321f 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
13220 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70  {.    assert( !p
13221 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65  Pager->pTmpSpace
13222 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f   );.    sqlite3O
13223 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 66  sClose(pPager->f
13224 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  d);.    sqlite3_
13225 66 72 65 65 28 70 50 61 67 65 72 29 3b 0a 20 20  free(pPager);.  
13226 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
13227 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a  ..  /* Initializ
13228 65 20 74 68 65 20 50 43 61 63 68 65 20 6f 62 6a  e the PCache obj
13229 65 63 74 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  ect. */.  assert
1322a 28 20 6e 45 78 74 72 61 3c 31 30 30 30 20 29 3b  ( nExtra<1000 );
1322b 0a 20 20 6e 45 78 74 72 61 20 3d 20 52 4f 55 4e  .  nExtra = ROUN
1322c 44 38 28 6e 45 78 74 72 61 29 3b 0a 20 20 73 71  D8(nExtra);.  sq
1322d 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 6e 28  lite3PcacheOpen(
1322e 73 7a 50 61 67 65 44 66 6c 74 2c 20 6e 45 78 74  szPageDflt, nExt
1322f 72 61 2c 20 21 6d 65 6d 44 62 2c 0a 20 20 20 20  ra, !memDb,.    
13230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13231 21 6d 65 6d 44 62 3f 70 61 67 65 72 53 74 72 65  !memDb?pagerStre
13232 73 73 3a 30 2c 20 28 76 6f 69 64 20 2a 29 70 50  ss:0, (void *)pP
13233 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 50  ager, pPager->pP
13234 43 61 63 68 65 29 3b 0a 0a 20 20 50 41 47 45 52  Cache);..  PAGER
13235 54 52 41 43 45 28 28 22 4f 50 45 4e 20 25 64 20  TRACE(("OPEN %d 
13236 25 73 5c 6e 22 2c 20 46 49 4c 45 48 41 4e 44 4c  %s\n", FILEHANDL
13237 45 49 44 28 70 50 61 67 65 72 2d 3e 66 64 29 2c  EID(pPager->fd),
13238 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61   pPager->zFilena
13239 6d 65 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28  me));.  IOTRACE(
1323a 28 22 4f 50 45 4e 20 25 70 20 25 73 5c 6e 22 2c  ("OPEN %p %s\n",
1323b 20 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d   pPager, pPager-
1323c 3e 7a 46 69 6c 65 6e 61 6d 65 29 29 0a 0a 20 20  >zFilename))..  
1323d 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e  pPager->useJourn
1323e 61 6c 20 3d 20 28 75 38 29 75 73 65 4a 6f 75 72  al = (u8)useJour
1323f 6e 61 6c 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e  nal;.  pPager->n
13240 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 28 6e 6f 52  oReadlock = (noR
13241 65 61 64 6c 6f 63 6b 20 26 26 20 72 65 61 64 4f  eadlock && readO
13242 6e 6c 79 29 20 3f 31 3a 30 3b 0a 20 20 2f 2a 20  nly) ?1:0;.  /* 
13243 70 50 61 67 65 72 2d 3e 73 74 6d 74 4f 70 65 6e  pPager->stmtOpen
13244 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50   = 0; */.  /* pP
13245 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20  ager->stmtInUse 
13246 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61  = 0; */.  /* pPa
13247 67 65 72 2d 3e 6e 52 65 66 20 3d 20 30 3b 20 2a  ger->nRef = 0; *
13248 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69  /.  pPager->dbSi
13249 7a 65 56 61 6c 69 64 20 3d 20 28 75 38 29 6d 65  zeValid = (u8)me
1324a 6d 44 62 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72  mDb;.  /* pPager
1324b 2d 3e 73 74 6d 74 53 69 7a 65 20 3d 20 30 3b 20  ->stmtSize = 0; 
1324c 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e  */.  /* pPager->
1324d 73 74 6d 74 4a 53 69 7a 65 20 3d 20 30 3b 20 2a  stmtJSize = 0; *
1324e 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e  /.  /* pPager->n
1324f 50 61 67 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 70  Page = 0; */.  p
13250 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20  Pager->mxPgno = 
13251 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f  SQLITE_MAX_PAGE_
13252 43 4f 55 4e 54 3b 0a 20 20 2f 2a 20 70 50 61 67  COUNT;.  /* pPag
13253 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45  er->state = PAGE
13254 52 5f 55 4e 4c 4f 43 4b 3b 20 2a 2f 0a 20 20 61  R_UNLOCK; */.  a
13255 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
13256 74 61 74 65 20 3d 3d 20 28 74 65 6d 70 46 69 6c  tate == (tempFil
13257 65 20 3f 20 50 41 47 45 52 5f 45 58 43 4c 55 53  e ? PAGER_EXCLUS
13258 49 56 45 20 3a 20 50 41 47 45 52 5f 55 4e 4c 4f  IVE : PAGER_UNLO
13259 43 4b 29 20 29 3b 0a 20 20 2f 2a 20 70 50 61 67  CK) );.  /* pPag
1325a 65 72 2d 3e 65 72 72 4d 61 73 6b 20 3d 20 30 3b  er->errMask = 0;
1325b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 74 65   */.  pPager->te
1325c 6d 70 46 69 6c 65 20 3d 20 28 75 38 29 74 65 6d  mpFile = (u8)tem
1325d 70 46 69 6c 65 3b 0a 20 20 61 73 73 65 72 74 28  pFile;.  assert(
1325e 20 74 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52   tempFile==PAGER
1325f 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52  _LOCKINGMODE_NOR
13260 4d 41 4c 20 0a 20 20 20 20 20 20 20 20 20 20 7c  MAL .          |
13261 7c 20 74 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45  | tempFile==PAGE
13262 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58  R_LOCKINGMODE_EX
13263 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 61 73 73  CLUSIVE );.  ass
13264 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49  ert( PAGER_LOCKI
13265 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45  NGMODE_EXCLUSIVE
13266 3d 3d 31 20 29 3b 0a 20 20 70 50 61 67 65 72 2d  ==1 );.  pPager-
13267 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d  >exclusiveMode =
13268 20 28 75 38 29 74 65 6d 70 46 69 6c 65 3b 20 0a   (u8)tempFile; .
13269 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65    pPager->change
1326a 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 70 50 61 67  CountDone = pPag
1326b 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20  er->tempFile;.  
1326c 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 3d 20  pPager->memDb = 
1326d 28 75 38 29 6d 65 6d 44 62 3b 0a 20 20 70 50 61  (u8)memDb;.  pPa
1326e 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20  ger->readOnly = 
1326f 28 75 38 29 72 65 61 64 4f 6e 6c 79 3b 0a 20 20  (u8)readOnly;.  
13270 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53  /* pPager->needS
13271 79 6e 63 20 3d 20 30 3b 20 2a 2f 0a 20 20 61 73  ync = 0; */.  as
13272 73 65 72 74 28 20 75 73 65 4a 6f 75 72 6e 61 6c  sert( useJournal
13273 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70   || pPager->temp
13274 46 69 6c 65 20 29 3b 0a 20 20 70 50 61 67 65 72  File );.  pPager
13275 2d 3e 6e 6f 53 79 6e 63 20 3d 20 70 50 61 67 65  ->noSync = pPage
13276 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 70  r->tempFile;.  p
13277 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20  Pager->fullSync 
13278 3d 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63  = pPager->noSync
13279 20 3f 30 3a 31 3b 0a 20 20 70 50 61 67 65 72 2d   ?0:1;.  pPager-
1327a 3e 73 79 6e 63 5f 66 6c 61 67 73 20 3d 20 53 51  >sync_flags = SQ
1327b 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c  LITE_SYNC_NORMAL
1327c 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70  ;.  /* pPager->p
1327d 46 69 72 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20  First = 0; */.  
1327e 2f 2a 20 70 50 61 67 65 72 2d 3e 70 46 69 72 73  /* pPager->pFirs
1327f 74 53 79 6e 63 65 64 20 3d 20 30 3b 20 2a 2f 0a  tSynced = 0; */.
13280 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 4c 61    /* pPager->pLa
13281 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61  st = 0; */.  pPa
13282 67 65 72 2d 3e 6e 45 78 74 72 61 20 3d 20 28 75  ger->nExtra = (u
13283 31 36 29 6e 45 78 74 72 61 3b 0a 20 20 70 50 61  16)nExtra;.  pPa
13284 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65  ger->journalSize
13285 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 45 5f 44  Limit = SQLITE_D
13286 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53  EFAULT_JOURNAL_S
13287 49 5a 45 5f 4c 49 4d 49 54 3b 0a 20 20 61 73 73  IZE_LIMIT;.  ass
13288 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67  ert( isOpen(pPag
13289 65 72 2d 3e 66 64 29 20 7c 7c 20 74 65 6d 70 46  er->fd) || tempF
1328a 69 6c 65 20 29 3b 0a 20 20 73 65 74 53 65 63 74  ile );.  setSect
1328b 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a  orSize(pPager);.
1328c 20 20 69 66 28 20 21 75 73 65 4a 6f 75 72 6e 61    if( !useJourna
1328d 6c 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d  l ){.    pPager-
1328e 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 50  >journalMode = P
1328f 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
13290 5f 4f 46 46 3b 0a 20 20 7d 65 6c 73 65 20 69 66  _OFF;.  }else if
13291 28 20 6d 65 6d 44 62 20 29 7b 0a 20 20 20 20 70  ( memDb ){.    p
13292 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
13293 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52 4e  de = PAGER_JOURN
13294 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 3b 0a 20  ALMODE_MEMORY;. 
13295 20 7d 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e   }.  /* pPager->
13296 78 42 75 73 79 48 61 6e 64 6c 65 72 20 3d 20 30  xBusyHandler = 0
13297 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72  ; */.  /* pPager
13298 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72  ->pBusyHandlerAr
13299 67 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67  g = 0; */.  pPag
1329a 65 72 2d 3e 78 52 65 69 6e 69 74 65 72 20 3d 20  er->xReiniter = 
1329b 78 52 65 69 6e 69 74 3b 0a 20 20 2f 2a 20 6d 65  xReinit;.  /* me
1329c 6d 73 65 74 28 70 50 61 67 65 72 2d 3e 61 48 61  mset(pPager->aHa
1329d 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 50  sh, 0, sizeof(pP
1329e 61 67 65 72 2d 3e 61 48 61 73 68 29 29 3b 20 2a  ager->aHash)); *
1329f 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d 20 70  /.  *ppPager = p
132a0 50 61 67 65 72 3b 0a 20 20 72 65 74 75 72 6e 20  Pager;.  return 
132a1 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a  SQLITE_OK;.}....
132a2 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
132a3 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66  ion is called af
132a4 74 65 72 20 74 72 61 6e 73 69 74 69 6f 6e 69 6e  ter transitionin
132a5 67 20 66 72 6f 6d 20 50 41 47 45 52 5f 55 4e 4c  g from PAGER_UNL
132a6 4f 43 4b 20 74 6f 0a 2a 2a 20 50 41 47 45 52 5f  OCK to.** PAGER_
132a7 53 48 41 52 45 44 20 73 74 61 74 65 2e 20 49 74  SHARED state. It
132a8 20 74 65 73 74 73 20 69 66 20 74 68 65 72 65 20   tests if there 
132a9 69 73 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c  is a hot journal
132aa 20 70 72 65 73 65 6e 74 20 69 6e 0a 2a 2a 20 74   present in.** t
132ab 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 66  he file-system f
132ac 6f 72 20 74 68 65 20 67 69 76 65 6e 20 70 61 67  or the given pag
132ad 65 72 2e 20 41 20 68 6f 74 20 6a 6f 75 72 6e 61  er. A hot journa
132ae 6c 20 69 73 20 6f 6e 65 20 74 68 61 74 20 0a 2a  l is one that .*
132af 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 70 6c  * needs to be pl
132b0 61 79 65 64 20 62 61 63 6b 2e 20 41 63 63 6f 72  ayed back. Accor
132b1 64 69 6e 67 20 74 6f 20 74 68 69 73 20 66 75 6e  ding to this fun
132b2 63 74 69 6f 6e 2c 20 61 20 68 6f 74 2d 6a 6f 75  ction, a hot-jou
132b3 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 65 78 69  rnal.** file exi
132b4 73 74 73 20 69 66 20 74 68 65 20 66 6f 6c 6c 6f  sts if the follo
132b5 77 69 6e 67 20 63 72 69 74 65 72 69 61 20 61 72  wing criteria ar
132b6 65 20 6d 65 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a  e met:.**.**   *
132b7 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   The journal fil
132b8 65 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20  e exists in the 
132b9 66 69 6c 65 20 73 79 73 74 65 6d 2c 20 61 6e 64  file system, and
132ba 0a 2a 2a 20 20 20 2a 20 4e 6f 20 70 72 6f 63 65  .**   * No proce
132bb 73 73 20 68 6f 6c 64 73 20 61 20 52 45 53 45 52  ss holds a RESER
132bc 56 45 44 20 6f 72 20 67 72 65 61 74 65 72 20 6c  VED or greater l
132bd 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
132be 61 73 65 20 66 69 6c 65 2c 20 61 6e 64 0a 2a 2a  ase file, and.**
132bf 20 20 20 2a 20 54 68 65 20 64 61 74 61 62 61 73     * The databas
132c0 65 20 66 69 6c 65 20 69 74 73 65 6c 66 20 69 73  e file itself is
132c1 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 30 20   greater than 0 
132c2 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 61  bytes in size, a
132c3 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 66 69  nd.**   * The fi
132c4 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 20  rst byte of the 
132c5 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69  journal file exi
132c6 73 74 73 20 61 6e 64 20 69 73 20 6e 6f 74 20 30  sts and is not 0
132c7 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  x00..**.** If th
132c8 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f  e current size o
132c9 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
132ca 69 6c 65 20 69 73 20 30 20 62 75 74 20 61 20 6a  ile is 0 but a j
132cb 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 65  ournal file.** e
132cc 78 69 73 74 73 2c 20 74 68 61 74 20 69 73 20 70  xists, that is p
132cd 72 6f 62 61 62 6c 79 20 61 6e 20 6f 6c 64 20 6a  robably an old j
132ce 6f 75 72 6e 61 6c 20 6c 65 66 74 20 6f 76 65 72  ournal left over
132cf 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 0a 2a 2a   from a prior.**
132d0 20 64 61 74 61 62 61 73 65 20 77 69 74 68 20 74   database with t
132d1 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 20 49 6e  he same name. In
132d2 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 6a   this case the j
132d3 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 0a 2a  ournal file is.*
132d4 2a 20 6a 75 73 74 20 64 65 6c 65 74 65 64 20 75  * just deleted u
132d5 73 69 6e 67 20 4f 73 44 65 6c 65 74 65 2c 20 2a  sing OsDelete, *
132d6 70 45 78 69 73 74 73 20 69 73 20 73 65 74 20 74  pExists is set t
132d7 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f  o 0 and SQLITE_O
132d8 4b 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  K.** is returned
132d9 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
132da 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 63 68  tine does not ch
132db 65 63 6b 20 69 66 20 74 68 65 72 65 20 69 73 20  eck if there is 
132dc 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  a master journal
132dd 20 66 69 6c 65 6e 61 6d 65 0a 2a 2a 20 61 74 20   filename.** at 
132de 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66  the end of the f
132df 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69 73  ile. If there is
132e0 2c 20 61 6e 64 20 74 68 61 74 20 6d 61 73 74 65  , and that maste
132e1 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a  r journal file.*
132e2 2a 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74  * does not exist
132e3 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e  , then the journ
132e4 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 72  al file is not r
132e5 65 61 6c 6c 79 20 68 6f 74 2e 20 49 6e 20 74 68  eally hot. In th
132e6 69 73 0a 2a 2a 20 63 61 73 65 20 74 68 69 73 20  is.** case this 
132e7 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 74  routine will ret
132e8 75 72 6e 20 61 20 66 61 6c 73 65 2d 70 6f 73 69  urn a false-posi
132e9 74 69 76 65 2e 20 54 68 65 20 70 61 67 65 72 5f  tive. The pager_
132ea 70 6c 61 79 62 61 63 6b 28 29 0a 2a 2a 20 72 6f  playback().** ro
132eb 75 74 69 6e 65 20 77 69 6c 6c 20 64 69 73 63 6f  utine will disco
132ec 76 65 72 20 74 68 61 74 20 74 68 65 20 6a 6f 75  ver that the jou
132ed 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74  rnal file is not
132ee 20 72 65 61 6c 6c 79 20 68 6f 74 20 61 6e 64 20   really hot and 
132ef 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c  .** will not rol
132f0 6c 20 69 74 20 62 61 63 6b 2e 20 0a 2a 2a 0a 2a  l it back. .**.*
132f1 2a 20 49 66 20 61 20 68 6f 74 2d 6a 6f 75 72 6e  * If a hot-journ
132f2 61 6c 20 66 69 6c 65 20 69 73 20 66 6f 75 6e 64  al file is found
132f3 20 74 6f 20 65 78 69 73 74 2c 20 2a 70 45 78 69   to exist, *pExi
132f4 73 74 73 20 69 73 20 73 65 74 20 74 6f 20 31 20  sts is set to 1 
132f5 61 6e 64 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f  and .** SQLITE_O
132f6 4b 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 6e  K returned. If n
132f7 6f 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69  o hot-journal fi
132f8 6c 65 20 69 73 20 70 72 65 73 65 6e 74 2c 20 2a  le is present, *
132f9 70 45 78 69 73 74 73 20 69 73 0a 2a 2a 20 73 65  pExists is.** se
132fa 74 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 54  t to 0 and SQLIT
132fb 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 49  E_OK returned. I
132fc 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63  f an IO error oc
132fd 63 75 72 73 20 77 68 69 6c 65 20 74 72 79 69 6e  curs while tryin
132fe 67 0a 2a 2a 20 74 6f 20 64 65 74 65 72 6d 69 6e  g.** to determin
132ff 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  e whether or not
13300 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66   a hot-journal f
13301 69 6c 65 20 65 78 69 73 74 73 2c 20 74 68 65 20  ile exists, the 
13302 49 4f 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65  IO error.** code
13303 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
13304 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 2a 70   the value of *p
13305 45 78 69 73 74 73 20 69 73 20 75 6e 64 65 66 69  Exists is undefi
13306 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ned..*/.static i
13307 6e 74 20 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c  nt hasHotJournal
13308 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
13309 69 6e 74 20 2a 70 45 78 69 73 74 73 29 7b 0a 20  int *pExists){. 
1330a 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 63   sqlite3_vfs * c
1330b 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 50 61 67  onst pVfs = pPag
1330c 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 6e 74 20  er->pVfs;.  int 
1330d 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
1330e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
1330f 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e  urn code */.  in
13310 74 20 65 78 69 73 74 73 3b 20 20 20 20 20 20 20  t exists;       
13311 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
13312 72 75 65 20 69 66 20 61 20 6a 6f 75 72 6e 61 6c  rue if a journal
13313 20 66 69 6c 65 20 69 73 20 70 72 65 73 65 6e 74   file is present
13314 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
13315 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 61 73  Pager!=0 );.  as
13316 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 75 73  sert( pPager->us
13317 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73  eJournal );.  as
13318 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61  sert( isOpen(pPa
13319 67 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20 61 73  ger->fd) );.  as
1331a 73 65 72 74 28 20 21 69 73 4f 70 65 6e 28 70 50  sert( !isOpen(pP
1331b 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20  ager->jfd) );.  
1331c 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
1331d 73 74 61 74 65 20 3c 3d 20 50 41 47 45 52 5f 53  state <= PAGER_S
1331e 48 41 52 45 44 20 29 3b 0a 0a 20 20 2a 70 45 78  HARED );..  *pEx
1331f 69 73 74 73 20 3d 20 30 3b 0a 20 20 72 63 20 3d  ists = 0;.  rc =
13320 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73   sqlite3OsAccess
13321 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a  (pVfs, pPager->z
13322 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c 49 54 45 5f  Journal, SQLITE_
13323 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26  ACCESS_EXISTS, &
13324 65 78 69 73 74 73 29 3b 0a 20 20 69 66 28 20 72  exists);.  if( r
13325 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
13326 65 78 69 73 74 73 20 29 7b 0a 20 20 20 20 69 6e  exists ){.    in
13327 74 20 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20  t locked;       
13328 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
13329 65 20 69 66 20 73 6f 6d 65 20 70 72 6f 63 65 73  e if some proces
1332a 73 20 68 6f 6c 64 73 20 61 20 52 45 53 45 52 56  s holds a RESERV
1332b 45 44 20 6c 6f 63 6b 20 2a 2f 0a 0a 20 20 20 20  ED lock */..    
1332c 2f 2a 20 52 61 63 65 20 63 6f 6e 64 69 74 69 6f  /* Race conditio
1332d 6e 20 68 65 72 65 3a 20 20 41 6e 6f 74 68 65 72  n here:  Another
1332e 20 70 72 6f 63 65 73 73 20 6d 69 67 68 74 20 68   process might h
1332f 61 76 65 20 62 65 65 6e 20 68 6f 6c 64 69 6e 67  ave been holding
13330 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 68 65 20   the.    ** the 
13331 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 61 6e  RESERVED lock an
13332 64 20 68 61 76 65 20 61 20 6a 6f 75 72 6e 61 6c  d have a journal
13333 20 6f 70 65 6e 20 61 74 20 74 68 65 20 73 71 6c   open at the sql
13334 69 74 65 33 4f 73 41 63 63 65 73 73 28 29 20 0a  ite3OsAccess() .
13335 20 20 20 20 2a 2a 20 63 61 6c 6c 20 61 62 6f 76      ** call abov
13336 65 2c 20 62 75 74 20 74 68 65 6e 20 64 65 6c 65  e, but then dele
13337 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61  te the journal a
13338 6e 64 20 64 72 6f 70 20 74 68 65 20 6c 6f 63 6b  nd drop the lock
13339 20 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20 77   before.    ** w
1333a 65 20 67 65 74 20 74 6f 20 74 68 65 20 66 6f 6c  e get to the fol
1333b 6c 6f 77 69 6e 67 20 73 71 6c 69 74 65 33 4f 73  lowing sqlite3Os
1333c 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
1333d 6b 28 29 20 63 61 6c 6c 2e 20 20 49 66 20 74 68  k() call.  If th
1333e 61 74 0a 20 20 20 20 2a 2a 20 69 73 20 74 68 65  at.    ** is the
1333f 20 63 61 73 65 2c 20 74 68 69 73 20 72 6f 75 74   case, this rout
13340 69 6e 65 20 6d 69 67 68 74 20 74 68 69 6e 6b 20  ine might think 
13341 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 20 6a  there is a hot j
13342 6f 75 72 6e 61 6c 20 77 68 65 6e 0a 20 20 20 20  ournal when.    
13343 2a 2a 20 69 6e 20 66 61 63 74 20 74 68 65 72 65  ** in fact there
13344 20 69 73 20 6e 6f 6e 65 2e 20 20 54 68 69 73 20   is none.  This 
13345 72 65 73 75 6c 74 73 20 69 6e 20 61 20 66 61 6c  results in a fal
13346 73 65 2d 70 6f 73 69 74 69 76 65 20 77 68 69 63  se-positive whic
13347 68 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 62 65  h will.    ** be
13348 20 64 65 61 6c 74 20 77 69 74 68 20 62 79 20 74   dealt with by t
13349 68 65 20 70 6c 61 79 62 61 63 6b 20 72 6f 75 74  he playback rout
1334a 69 6e 65 2e 20 20 54 69 63 6b 65 74 20 23 33 38  ine.  Ticket #38
1334b 38 33 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  83..    */.    r
1334c 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 43 68 65  c = sqlite3OsChe
1334d 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 70  ckReservedLock(p
1334e 50 61 67 65 72 2d 3e 66 64 2c 20 26 6c 6f 63 6b  Pager->fd, &lock
1334f 65 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  ed);.    if( rc=
13350 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 6c  =SQLITE_OK && !l
13351 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20 69  ocked ){.      i
13352 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 20 20 20  nt nPage;..     
13353 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 73 69   /* Check the si
13354 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ze of the databa
13355 73 65 20 66 69 6c 65 2e 20 49 66 20 69 74 20 63  se file. If it c
13356 6f 6e 73 69 73 74 73 20 6f 66 20 30 20 70 61 67  onsists of 0 pag
13357 65 73 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  es,.      ** the
13358 6e 20 64 65 6c 65 74 65 20 74 68 65 20 6a 6f 75  n delete the jou
13359 72 6e 61 6c 20 66 69 6c 65 2e 20 53 65 65 20 74  rnal file. See t
1335a 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e  he header commen
1335b 74 20 61 62 6f 76 65 20 66 6f 72 20 0a 20 20 20  t above for .   
1335c 20 20 20 2a 2a 20 74 68 65 20 72 65 61 73 6f 6e     ** the reason
1335d 69 6e 67 20 68 65 72 65 2e 20 20 44 65 6c 65 74  ing here.  Delet
1335e 65 20 74 68 65 20 6f 62 73 6f 6c 65 74 65 20 6a  e the obsolete j
1335f 6f 75 72 6e 61 6c 20 66 69 6c 65 20 75 6e 64 65  ournal file unde
13360 72 0a 20 20 20 20 20 20 2a 2a 20 61 20 52 45 53  r.      ** a RES
13361 45 52 56 45 44 20 6c 6f 63 6b 20 74 6f 20 61 76  ERVED lock to av
13362 6f 69 64 20 72 61 63 65 20 63 6f 6e 64 69 74 69  oid race conditi
13363 6f 6e 73 20 61 6e 64 20 74 6f 20 61 76 6f 69 64  ons and to avoid
13364 20 76 69 6f 6c 61 74 69 6e 67 0a 20 20 20 20 20   violating.     
13365 20 2a 2a 20 5b 48 33 33 30 32 30 5d 2e 0a 20 20   ** [H33020]..  
13366 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20      */.      rc 
13367 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61  = sqlite3PagerPa
13368 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20  gecount(pPager, 
13369 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69  &nPage);.      i
1336a 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1336b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
1336c 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20  nPage==0 ){.    
1336d 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67        sqlite3Beg
1336e 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  inBenignMalloc()
1336f 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
13370 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50  sqlite3OsLock(pP
13371 61 67 65 72 2d 3e 66 64 2c 20 52 45 53 45 52 56  ager->fd, RESERV
13372 45 44 5f 4c 4f 43 4b 29 3d 3d 53 51 4c 49 54 45  ED_LOCK)==SQLITE
13373 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
13374 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65     sqlite3OsDele
13375 74 65 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d  te(pVfs, pPager-
13376 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20  >zJournal, 0);. 
13377 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
13378 65 33 4f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65  e3OsUnlock(pPage
13379 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f 4c 4f  r->fd, SHARED_LO
1337a 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  CK);.          }
1337b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1337c 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f  e3EndBenignMallo
1337d 63 28 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  c();.        }el
1337e 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  se{.          /*
1337f 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   The journal fil
13380 65 20 65 78 69 73 74 73 20 61 6e 64 20 6e 6f 20  e exists and no 
13381 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e  other connection
13382 20 68 61 73 20 61 20 72 65 73 65 72 76 65 64 0a   has a reserved.
13383 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20            ** or 
13384 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20  greater lock on 
13385 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
13386 65 2e 20 4e 6f 77 20 63 68 65 63 6b 20 74 68 61  e. Now check tha
13387 74 20 74 68 65 72 65 20 69 73 0a 20 20 20 20 20  t there is.     
13388 20 20 20 20 20 2a 2a 20 61 74 20 6c 65 61 73 74       ** at least
13389 20 6f 6e 65 20 6e 6f 6e 2d 7a 65 72 6f 20 62 79   one non-zero by
1338a 74 65 73 20 61 74 20 74 68 65 20 73 74 61 72 74  tes at the start
1338b 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
1338c 66 69 6c 65 2e 0a 20 20 20 20 20 20 20 20 20 20  file..          
1338d 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 2c 20  ** If there is, 
1338e 74 68 65 6e 20 77 65 20 63 6f 6e 73 69 64 65 72  then we consider
1338f 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 74 6f   this journal to
13390 20 62 65 20 68 6f 74 2e 20 49 66 20 6e 6f 74 2c   be hot. If not,
13391 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69   .          ** i
13392 74 20 63 61 6e 20 62 65 20 69 67 6e 6f 72 65 64  t can be ignored
13393 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
13394 20 20 20 20 20 20 20 20 20 69 6e 74 20 66 20 3d           int f =
13395 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
13396 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45  DONLY|SQLITE_OPE
13397 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a  N_MAIN_JOURNAL;.
13398 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
13399 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66  qlite3OsOpen(pVf
1339a 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72  s, pPager->zJour
1339b 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64  nal, pPager->jfd
1339c 2c 20 66 2c 20 26 66 29 3b 0a 20 20 20 20 20 20  , f, &f);.      
1339d 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1339e 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1339f 20 20 20 20 20 75 38 20 66 69 72 73 74 20 3d 20       u8 first = 
133a0 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  0;.            r
133a1 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61  c = sqlite3OsRea
133a2 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 28  d(pPager->jfd, (
133a3 76 6f 69 64 20 2a 29 26 66 69 72 73 74 2c 20 31  void *)&first, 1
133a4 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
133a5 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
133a6 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41  _IOERR_SHORT_REA
133a7 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  D ){.           
133a8 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
133a9 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  K;.            }
133aa 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
133ab 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67  ite3OsClose(pPag
133ac 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20  er->jfd);.      
133ad 20 20 20 20 20 20 2a 70 45 78 69 73 74 73 20 3d        *pExists =
133ae 20 28 66 69 72 73 74 21 3d 30 29 3b 0a 20 20 20   (first!=0);.   
133af 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
133b0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 43 41 4e 54   rc==SQLITE_CANT
133b1 4f 50 45 4e 20 29 7b 0a 20 20 20 20 20 20 20 20  OPEN ){.        
133b2 20 20 20 20 2f 2a 20 49 66 20 77 65 20 63 61 6e      /* If we can
133b3 6e 6f 74 20 6f 70 65 6e 20 74 68 65 20 72 6f 6c  not open the rol
133b4 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69  lback journal fi
133b5 6c 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73  le in order to s
133b6 65 65 20 69 66 0a 20 20 20 20 20 20 20 20 20 20  ee if.          
133b7 20 20 2a 2a 20 69 74 73 20 68 61 73 20 61 20 7a    ** its has a z
133b8 65 72 6f 20 68 65 61 64 65 72 2c 20 74 68 61 74  ero header, that
133b9 20 6d 69 67 68 74 20 62 65 20 64 75 65 20 74 6f   might be due to
133ba 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 2c 20 6f   an I/O error, o
133bb 72 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  r.            **
133bc 20 69 74 20 6d 69 67 68 74 20 62 65 20 64 75 65   it might be due
133bd 20 74 6f 20 74 68 65 20 72 61 63 65 20 63 6f 6e   to the race con
133be 64 69 74 69 6f 6e 20 64 65 73 63 72 69 62 65 64  dition described
133bf 20 61 62 6f 76 65 20 61 6e 64 20 69 6e 0a 20 20   above and in.  
133c0 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 69 63            ** tic
133c1 6b 65 74 20 23 33 38 38 33 2e 20 20 45 69 74 68  ket #3883.  Eith
133c2 65 72 20 77 61 79 2c 20 61 73 73 75 6d 65 20 74  er way, assume t
133c3 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  hat the journal 
133c4 69 73 20 68 6f 74 2e 0a 20 20 20 20 20 20 20 20  is hot..        
133c5 20 20 20 20 2a 2a 20 54 68 69 73 20 6d 69 67 68      ** This migh
133c6 74 20 62 65 20 61 20 66 61 6c 73 65 20 70 6f 73  t be a false pos
133c7 69 74 69 76 65 2e 20 20 42 75 74 20 69 66 20 69  itive.  But if i
133c8 74 20 69 73 2c 20 74 68 65 6e 20 74 68 65 0a 20  t is, then the. 
133c9 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 75             ** au
133ca 74 6f 6d 61 74 69 63 20 6a 6f 75 72 6e 61 6c 20  tomatic journal 
133cb 70 6c 61 79 62 61 63 6b 20 61 6e 64 20 72 65 63  playback and rec
133cc 6f 76 65 72 79 20 6d 65 63 68 61 6e 69 73 6d 20  overy mechanism 
133cd 77 69 6c 6c 20 64 65 61 6c 0a 20 20 20 20 20 20  will deal.      
133ce 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20 69 74        ** with it
133cf 20 75 6e 64 65 72 20 61 6e 20 45 58 43 4c 55 53   under an EXCLUS
133d0 49 56 45 20 6c 6f 63 6b 20 77 68 65 72 65 20 77  IVE lock where w
133d1 65 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f  e do not need to
133d2 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
133d3 77 6f 72 72 79 20 73 6f 20 6d 75 63 68 20 77 69  worry so much wi
133d4 74 68 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f  th race conditio
133d5 6e 73 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20  ns..            
133d6 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  */.            *
133d7 70 45 78 69 73 74 73 20 3d 20 31 3b 0a 20 20 20  pExists = 1;.   
133d8 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
133d9 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20  LITE_OK;.       
133da 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
133db 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
133dc 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
133dd 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65  ../*.** Read the
133de 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 70 61 67   content for pag
133df 65 20 70 50 67 20 6f 75 74 20 6f 66 20 74 68 65  e pPg out of the
133e0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
133e1 6e 64 20 69 6e 74 6f 20 0a 2a 2a 20 70 50 67 2d  nd into .** pPg-
133e2 3e 70 44 61 74 61 2e 20 41 20 73 68 61 72 65 64  >pData. A shared
133e3 20 6c 6f 63 6b 20 6f 72 20 67 72 65 61 74 65 72   lock or greater
133e4 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 6f 6e   must be held on
133e5 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a   the database.**
133e6 20 66 69 6c 65 20 62 65 66 6f 72 65 20 74 68 69   file before thi
133e7 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
133e8 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70  lled..**.** If p
133e9 61 67 65 20 31 20 69 73 20 72 65 61 64 2c 20 74  age 1 is read, t
133ea 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66  hen the value of
133eb 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72   Pager.dbFileVer
133ec 73 5b 5d 20 69 73 20 73 65 74 20 74 6f 0a 2a 2a  s[] is set to.**
133ed 20 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 20   the value read 
133ee 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
133ef 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  e file..**.** If
133f0 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63   an IO error occ
133f1 75 72 73 2c 20 74 68 65 6e 20 74 68 65 20 49 4f  urs, then the IO
133f2 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e   error is return
133f3 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  ed to the caller
133f4 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20  ..** Otherwise, 
133f5 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
133f6 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  urned..*/.static
133f7 20 69 6e 74 20 72 65 61 64 44 62 50 61 67 65 28   int readDbPage(
133f8 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50  PgHdr *pPg){.  P
133f9 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70  ager *pPager = p
133fa 50 67 2d 3e 70 50 61 67 65 72 3b 20 2f 2a 20 50  Pg->pPager; /* P
133fb 61 67 65 72 20 6f 62 6a 65 63 74 20 61 73 73 6f  ager object asso
133fc 63 69 61 74 65 64 20 77 69 74 68 20 70 61 67 65  ciated with page
133fd 20 70 50 67 20 2a 2f 0a 20 20 50 67 6e 6f 20 70   pPg */.  Pgno p
133fe 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b  gno = pPg->pgno;
133ff 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e         /* Page n
13400 75 6d 62 65 72 20 74 6f 20 72 65 61 64 20 2a 2f  umber to read */
13401 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
13402 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13403 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
13404 2f 0a 20 20 69 36 34 20 69 4f 66 66 73 65 74 3b  /.  i64 iOffset;
13405 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13406 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20   /* Byte offset 
13407 6f 66 20 66 69 6c 65 20 74 6f 20 72 65 61 64 20  of file to read 
13408 66 72 6f 6d 20 2a 2f 0a 0a 20 20 61 73 73 65 72  from */..  asser
13409 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  t( pPager->state
1340a 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 26  >=PAGER_SHARED &
1340b 26 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 61 73  & !MEMDB );.  as
1340c 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61  sert( isOpen(pPa
1340d 67 65 72 2d 3e 66 64 29 20 29 3b 0a 0a 20 20 69  ger->fd) );..  i
1340e 66 28 20 4e 45 56 45 52 28 21 69 73 4f 70 65 6e  f( NEVER(!isOpen
1340f 28 70 50 61 67 65 72 2d 3e 66 64 29 29 20 29 7b  (pPager->fd)) ){
13410 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
13411 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b  ger->tempFile );
13412 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50 67 2d  .    memset(pPg-
13413 3e 70 44 61 74 61 2c 20 30 2c 20 70 50 61 67 65  >pData, 0, pPage
13414 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20  r->pageSize);.  
13415 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
13416 4f 4b 3b 0a 20 20 7d 0a 20 20 69 4f 66 66 73 65  OK;.  }.  iOffse
13417 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36  t = (pgno-1)*(i6
13418 34 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  4)pPager->pageSi
13419 7a 65 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  ze;.  rc = sqlit
1341a 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d  e3OsRead(pPager-
1341b 3e 66 64 2c 20 70 50 67 2d 3e 70 44 61 74 61 2c  >fd, pPg->pData,
1341c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
1341d 65 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 20 69  e, iOffset);.  i
1341e 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f  f( rc==SQLITE_IO
1341f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29  ERR_SHORT_READ )
13420 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
13421 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20  E_OK;.  }.  if( 
13422 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 75  pgno==1 ){.    u
13423 38 20 2a 64 62 46 69 6c 65 56 65 72 73 20 3d 20  8 *dbFileVers = 
13424 26 28 28 75 38 2a 29 70 50 67 2d 3e 70 44 61 74  &((u8*)pPg->pDat
13425 61 29 5b 32 34 5d 3b 0a 20 20 20 20 6d 65 6d 63  a)[24];.    memc
13426 70 79 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69  py(&pPager->dbFi
13427 6c 65 56 65 72 73 2c 20 64 62 46 69 6c 65 56 65  leVers, dbFileVe
13428 72 73 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 65  rs, sizeof(pPage
13429 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b  r->dbFileVers));
1342a 0a 20 20 7d 0a 20 20 43 4f 44 45 43 31 28 70 50  .  }.  CODEC1(pP
1342b 61 67 65 72 2c 20 70 50 67 2d 3e 70 44 61 74 61  ager, pPg->pData
1342c 2c 20 70 67 6e 6f 2c 20 33 2c 20 72 63 20 3d 20  , pgno, 3, rc = 
1342d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 0a  SQLITE_NOMEM);..
1342e 20 20 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c    PAGER_INCR(sql
1342f 69 74 65 33 5f 70 61 67 65 72 5f 72 65 61 64 64  ite3_pager_readd
13430 62 5f 63 6f 75 6e 74 29 3b 0a 20 20 50 41 47 45  b_count);.  PAGE
13431 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e  R_INCR(pPager->n
13432 52 65 61 64 29 3b 0a 20 20 49 4f 54 52 41 43 45  Read);.  IOTRACE
13433 28 28 22 50 47 49 4e 20 25 70 20 25 64 5c 6e 22  (("PGIN %p %d\n"
13434 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 29  , pPager, pgno))
13435 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28  ;.  PAGERTRACE((
13436 22 46 45 54 43 48 20 25 64 20 70 61 67 65 20 25  "FETCH %d page %
13437 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22 2c  d hash(%08x)\n",
13438 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
13439 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c  PAGERID(pPager),
1343a 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f 70 61 67   pgno, pager_pag
1343b 65 68 61 73 68 28 70 50 67 29 29 29 3b 0a 0a 20  ehash(pPg)));.. 
1343c 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1343d 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
1343e 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  on is called to 
1343f 6f 62 74 61 69 6e 20 61 20 73 68 61 72 65 64 20  obtain a shared 
13440 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
13441 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 74  base file..** It
13442 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 63   is illegal to c
13443 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65 72  all sqlite3Pager
13444 41 63 71 75 69 72 65 28 29 20 75 6e 74 69 6c 20  Acquire() until 
13445 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74  after this funct
13446 69 6f 6e 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20  ion.** has been 
13447 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 61 6c  successfully cal
13448 6c 65 64 2e 20 49 66 20 61 20 73 68 61 72 65 64  led. If a shared
13449 2d 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79  -lock is already
1344a 20 68 65 6c 64 20 77 68 65 6e 0a 2a 2a 20 74 68   held when.** th
1344b 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
1344c 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 61 20 6e  alled, it is a n
1344d 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  o-op..**.** The 
1344e 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 65 72 61 74  following operat
1344f 69 6f 6e 73 20 61 72 65 20 61 6c 73 6f 20 70 65  ions are also pe
13450 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69 73 20  rformed by this 
13451 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
13452 20 20 31 29 20 49 66 20 74 68 65 20 70 61 67 65    1) If the page
13453 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69  r is currently i
13454 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 73  n PAGER_UNLOCK s
13455 74 61 74 65 20 28 6e 6f 20 6c 6f 63 6b 20 68 65  tate (no lock he
13456 6c 64 0a 2a 2a 20 20 20 20 20 20 6f 6e 20 74 68  ld.**      on th
13457 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 29  e database file)
13458 2c 20 74 68 65 6e 20 61 6e 20 61 74 74 65 6d 70  , then an attemp
13459 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 62 74  t is made to obt
1345a 61 69 6e 20 61 0a 2a 2a 20 20 20 20 20 20 53 48  ain a.**      SH
1345b 41 52 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  ARED lock on the
1345c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
1345d 49 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65  Immediately afte
1345e 72 20 6f 62 74 61 69 6e 69 6e 67 0a 2a 2a 20 20  r obtaining.**  
1345f 20 20 20 20 74 68 65 20 53 48 41 52 45 44 20 6c      the SHARED l
13460 6f 63 6b 2c 20 74 68 65 20 66 69 6c 65 2d 73 79  ock, the file-sy
13461 73 74 65 6d 20 69 73 20 63 68 65 63 6b 65 64 20  stem is checked 
13462 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61  for a hot-journa
13463 6c 2c 0a 2a 2a 20 20 20 20 20 20 77 68 69 63 68  l,.**      which
13464 20 69 73 20 70 6c 61 79 65 64 20 62 61 63 6b 20   is played back 
13465 69 66 20 70 72 65 73 65 6e 74 2e 20 46 6f 6c 6c  if present. Foll
13466 6f 77 69 6e 67 20 61 6e 79 20 68 6f 74 2d 6a 6f  owing any hot-jo
13467 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20 20 72  urnal .**      r
13468 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 20 63 6f 6e  ollback, the con
13469 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63  tents of the cac
1346a 68 65 20 61 72 65 20 76 61 6c 69 64 61 74 65 64  he are validated
1346b 20 62 79 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20   by checking.** 
1346c 20 20 20 20 20 74 68 65 20 27 63 68 61 6e 67 65       the 'change
1346d 2d 63 6f 75 6e 74 65 72 27 20 66 69 65 6c 64 20  -counter' field 
1346e 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1346f 66 69 6c 65 20 68 65 61 64 65 72 20 61 6e 64 0a  file header and.
13470 2a 2a 20 20 20 20 20 20 64 69 73 63 61 72 64 65  **      discarde
13471 64 20 69 66 20 74 68 65 79 20 61 72 65 20 66 6f  d if they are fo
13472 75 6e 64 20 74 6f 20 62 65 20 69 6e 76 61 6c 69  und to be invali
13473 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 49 66  d..**.**   2) If
13474 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72 75   the pager is ru
13475 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69  nning in exclusi
13476 76 65 2d 6d 6f 64 65 2c 20 61 6e 64 20 74 68 65  ve-mode, and the
13477 72 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79  re are currently
13478 0a 2a 2a 20 20 20 20 20 20 6e 6f 20 6f 75 74 73  .**      no outs
13479 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63  tanding referenc
1347a 65 73 20 74 6f 20 61 6e 79 20 70 61 67 65 73 2c  es to any pages,
1347b 20 61 6e 64 20 69 73 20 69 6e 20 74 68 65 20 65   and is in the e
1347c 72 72 6f 72 20 73 74 61 74 65 2c 0a 2a 2a 20 20  rror state,.**  
1347d 20 20 20 20 74 68 65 6e 20 61 6e 20 61 74 74 65      then an atte
1347e 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 63  mpt is made to c
1347f 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 20 73  lear the error s
13480 74 61 74 65 20 62 79 20 64 69 73 63 61 72 64 69  tate by discardi
13481 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 63  ng.**      the c
13482 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70  ontents of the p
13483 61 67 65 20 63 61 63 68 65 20 61 6e 64 20 72 6f  age cache and ro
13484 6c 6c 69 6e 67 20 62 61 63 6b 20 61 6e 79 20 6f  lling back any o
13485 70 65 6e 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20  pen journal.**  
13486 20 20 20 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20      file..**.** 
13487 49 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  If the operation
13488 20 64 65 73 63 72 69 62 65 64 20 62 79 20 28 32   described by (2
13489 29 20 61 62 6f 76 65 20 69 73 20 6e 6f 74 20 61  ) above is not a
1348a 74 74 65 6d 70 74 65 64 2c 20 61 6e 64 20 69 66  ttempted, and if
1348b 20 74 68 65 0a 2a 2a 20 70 61 67 65 72 20 69 73   the.** pager is
1348c 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 61   in an error sta
1348d 74 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51  te other than SQ
1348e 4c 49 54 45 5f 46 55 4c 4c 20 77 68 65 6e 20 74  LITE_FULL when t
1348f 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a  his is called,.*
13490 2a 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74  * the error stat
13491 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  e error code is 
13492 72 65 74 75 72 6e 65 64 2e 20 49 74 20 69 73 20  returned. It is 
13493 70 65 72 6d 69 74 74 65 64 20 74 6f 20 72 65 61  permitted to rea
13494 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  d the.** databas
13495 65 20 77 68 65 6e 20 69 6e 20 53 51 4c 49 54 45  e when in SQLITE
13496 5f 46 55 4c 4c 20 65 72 72 6f 72 20 73 74 61 74  _FULL error stat
13497 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69  e..**.** Otherwi
13498 73 65 2c 20 69 66 20 65 76 65 72 79 74 68 69 6e  se, if everythin
13499 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c  g is successful,
1349a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
1349b 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 0a 2a 2a  turned. If an.**
1349c 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73   IO error occurs
1349d 20 77 68 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 74   while locking t
1349e 68 65 20 64 61 74 61 62 61 73 65 2c 20 63 68 65  he database, che
1349f 63 6b 69 6e 67 20 66 6f 72 20 61 20 68 6f 74 2d  cking for a hot-
134a0 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20  journal.** file 
134a1 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20  or rolling back 
134a2 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20  a journal file, 
134a3 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64  the IO error cod
134a4 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  e is returned..*
134a5 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
134a6 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
134a7 72 53 68 61 72 65 64 4c 6f 63 6b 28 50 61 67 65  rSharedLock(Page
134a8 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e  r *pPager){.  in
134a9 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
134aa 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
134ab 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
134ac 2a 2f 0a 20 20 69 6e 74 20 69 73 45 72 72 6f 72  */.  int isError
134ad 52 65 73 65 74 20 3d 20 30 3b 20 20 20 20 20 20  Reset = 0;      
134ae 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
134af 69 66 20 72 65 63 6f 76 65 72 69 6e 67 20 66 72  if recovering fr
134b0 6f 6d 20 65 72 72 6f 72 20 73 74 61 74 65 20 2a  om error state *
134b1 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75  /..  /* This rou
134b2 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 63 61 6c  tine is only cal
134b3 6c 65 64 20 66 72 6f 6d 20 62 2d 74 72 65 65 20  led from b-tree 
134b4 61 6e 64 20 6f 6e 6c 79 20 77 68 65 6e 20 74 68  and only when th
134b5 65 72 65 20 61 72 65 20 6e 6f 0a 20 20 2a 2a 20  ere are no.  ** 
134b6 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65  outstanding page
134b7 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73  s */.  assert( s
134b8 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43  qlite3PcacheRefC
134b9 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43  ount(pPager->pPC
134ba 61 63 68 65 29 3d 3d 30 20 29 3b 0a 20 20 69 66  ache)==0 );.  if
134bb 28 20 4e 45 56 45 52 28 4d 45 4d 44 42 20 26 26  ( NEVER(MEMDB &&
134bc 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
134bd 29 20 29 7b 20 72 65 74 75 72 6e 20 70 50 61 67  ) ){ return pPag
134be 65 72 2d 3e 65 72 72 43 6f 64 65 3b 20 7d 0a 0a  er->errCode; }..
134bf 20 20 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74    /* If this dat
134c0 61 62 61 73 65 20 69 73 20 69 6e 20 61 6e 20 65  abase is in an e
134c1 72 72 6f 72 2d 73 74 61 74 65 2c 20 6e 6f 77 20  rror-state, now 
134c2 69 73 20 61 20 63 68 61 6e 63 65 20 74 6f 20 63  is a chance to c
134c3 6c 65 61 72 0a 20 20 2a 2a 20 74 68 65 20 65 72  lear.  ** the er
134c4 72 6f 72 2e 20 44 69 73 63 61 72 64 20 74 68 65  ror. Discard the
134c5 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
134c6 20 70 61 67 65 72 2d 63 61 63 68 65 20 61 6e 64   pager-cache and
134c7 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 61   rollback.  ** a
134c8 6e 79 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 69  ny hot journal i
134c9 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65  n the file-syste
134ca 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50  m..  */.  if( pP
134cb 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b  ager->errCode ){
134cc 0a 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28  .    if( isOpen(
134cd 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20  pPager->jfd) || 
134ce 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
134cf 20 29 7b 0a 20 20 20 20 20 20 69 73 45 72 72 6f   ){.      isErro
134d0 72 52 65 73 65 74 20 3d 20 31 3b 0a 20 20 20 20  rReset = 1;.    
134d1 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72  }.    pPager->er
134d2 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4f  rCode = SQLITE_O
134d3 4b 3b 0a 20 20 20 20 70 61 67 65 72 5f 72 65 73  K;.    pager_res
134d4 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a  et(pPager);.  }.
134d5 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  .  if( pPager->s
134d6 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f  tate==PAGER_UNLO
134d7 43 4b 20 7c 7c 20 69 73 45 72 72 6f 72 52 65 73  CK || isErrorRes
134d8 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  et ){.    sqlite
134d9 33 5f 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 56  3_vfs * const pV
134da 66 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66  fs = pPager->pVf
134db 73 3b 0a 20 20 20 20 69 6e 74 20 69 73 48 6f 74  s;.    int isHot
134dc 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20  Journal = 0;.   
134dd 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20   assert( !MEMDB 
134de 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  );.    assert( s
134df 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43  qlite3PcacheRefC
134e0 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43  ount(pPager->pPC
134e1 61 63 68 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20  ache)==0 );.    
134e2 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 6f 52 65  if( pPager->noRe
134e3 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20  adlock ){.      
134e4 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
134e5 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 20 20  readOnly );.    
134e6 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20    pPager->state 
134e7 3d 20 50 41 47 45 52 5f 53 48 41 52 45 44 3b 0a  = PAGER_SHARED;.
134e8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
134e9 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74   rc = pager_wait
134ea 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c  _on_lock(pPager,
134eb 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20   SHARED_LOCK);. 
134ec 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
134ed 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
134ee 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
134ef 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55  ->state==PAGER_U
134f0 4e 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20 20  NLOCK );.       
134f1 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72   return pager_er
134f2 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b  ror(pPager, rc);
134f3 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
134f4 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
134f5 72 2d 3e 73 74 61 74 65 3e 3d 53 48 41 52 45 44  r->state>=SHARED
134f6 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a  _LOCK );..    /*
134f7 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69   If a journal fi
134f8 6c 65 20 65 78 69 73 74 73 2c 20 61 6e 64 20 74  le exists, and t
134f9 68 65 72 65 20 69 73 20 6e 6f 20 52 45 53 45 52  here is no RESER
134fa 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a  VED lock on the.
134fb 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20      ** database 
134fc 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 65 69  file, then it ei
134fd 74 68 65 72 20 6e 65 65 64 73 20 74 6f 20 62 65  ther needs to be
134fe 20 70 6c 61 79 65 64 20 62 61 63 6b 20 6f 72 20   played back or 
134ff 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a  deleted..    */.
13500 20 20 20 20 69 66 28 20 21 69 73 45 72 72 6f 72      if( !isError
13501 52 65 73 65 74 20 29 7b 0a 20 20 20 20 20 20 61  Reset ){.      a
13502 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
13503 74 61 74 65 20 3c 3d 20 50 41 47 45 52 5f 53 48  tate <= PAGER_SH
13504 41 52 45 44 20 29 3b 0a 20 20 20 20 20 20 72 63  ARED );.      rc
13505 20 3d 20 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c   = hasHotJournal
13506 28 70 50 61 67 65 72 2c 20 26 69 73 48 6f 74 4a  (pPager, &isHotJ
13507 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20 20 69  ournal);.      i
13508 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
13509 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   ){.        goto
1350a 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d   failed;.      }
1350b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69  .    }.    if( i
1350c 73 45 72 72 6f 72 52 65 73 65 74 20 7c 7c 20 69  sErrorReset || i
1350d 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20  sHotJournal ){. 
1350e 20 20 20 20 20 2f 2a 20 47 65 74 20 61 6e 20 45       /* Get an E
1350f 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e  XCLUSIVE lock on
13510 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
13511 6c 65 2e 20 41 74 20 74 68 69 73 20 70 6f 69 6e  le. At this poin
13512 74 20 69 74 20 69 73 0a 20 20 20 20 20 20 2a 2a  t it is.      **
13513 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20   important that 
13514 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  a RESERVED lock 
13515 69 73 20 6e 6f 74 20 6f 62 74 61 69 6e 65 64 20  is not obtained 
13516 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20 74 68  on the way to th
13517 65 0a 20 20 20 20 20 20 2a 2a 20 45 58 43 4c 55  e.      ** EXCLU
13518 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20 69 74  SIVE lock. If it
13519 20 77 65 72 65 2c 20 61 6e 6f 74 68 65 72 20 70   were, another p
1351a 72 6f 63 65 73 73 20 6d 69 67 68 74 20 6f 70 65  rocess might ope
1351b 6e 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64  n the.      ** d
1351c 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 64 65  atabase file, de
1351d 74 65 63 74 20 74 68 65 20 52 45 53 45 52 56 45  tect the RESERVE
1351e 44 20 6c 6f 63 6b 2c 20 61 6e 64 20 63 6f 6e 63  D lock, and conc
1351f 6c 75 64 65 20 74 68 61 74 20 74 68 65 0a 20 20  lude that the.  
13520 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20      ** database 
13521 69 73 20 73 61 66 65 20 74 6f 20 72 65 61 64 20  is safe to read 
13522 77 68 69 6c 65 20 74 68 69 73 20 70 72 6f 63 65  while this proce
13523 73 73 20 69 73 20 73 74 69 6c 6c 20 72 6f 6c 6c  ss is still roll
13524 69 6e 67 20 74 68 65 20 0a 20 20 20 20 20 20 2a  ing the .      *
13525 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 61  * hot-journal ba
13526 63 6b 2e 0a 20 20 20 20 20 20 2a 2a 20 0a 20 20  ck..      ** .  
13527 20 20 20 20 2a 2a 20 42 65 63 61 75 73 65 20 74      ** Because t
13528 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  he intermediate 
13529 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73  RESERVED lock is
1352a 20 6e 6f 74 20 72 65 71 75 65 73 74 65 64 2c 20   not requested, 
1352b 61 6e 79 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68  any.      ** oth
1352c 65 72 20 70 72 6f 63 65 73 73 20 61 74 74 65 6d  er process attem
1352d 70 74 69 6e 67 20 74 6f 20 61 63 63 65 73 73 20  pting to access 
1352e 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1352f 65 20 77 69 6c 6c 20 67 65 74 20 74 6f 20 0a 20  e will get to . 
13530 20 20 20 20 20 2a 2a 20 74 68 69 73 20 70 6f 69       ** this poi
13531 6e 74 20 69 6e 20 74 68 65 20 63 6f 64 65 20 61  nt in the code a
13532 6e 64 20 66 61 69 6c 20 74 6f 20 6f 62 74 61 69  nd fail to obtai
13533 6e 20 69 74 73 20 6f 77 6e 20 45 58 43 4c 55 53  n its own EXCLUS
13534 49 56 45 20 6c 6f 63 6b 20 0a 20 20 20 20 20 20  IVE lock .      
13535 2a 2a 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ** on the databa
13536 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 2a  se file..      *
13537 2f 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67  /.      if( pPag
13538 65 72 2d 3e 73 74 61 74 65 3c 45 58 43 4c 55 53  er->state<EXCLUS
13539 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  IVE_LOCK ){.    
1353a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1353b 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66  OsLock(pPager->f
1353c 64 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  d, EXCLUSIVE_LOC
1353d 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  K);.        if( 
1353e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1353f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
13540 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67  pager_error(pPag
13541 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 20  er, rc);.       
13542 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a     goto failed;.
13543 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
13544 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20    pPager->state 
13545 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  = PAGER_EXCLUSIV
13546 45 3b 0a 20 20 20 20 20 20 7d 0a 20 0a 20 20 20  E;.      }. .   
13547 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a     /* Open the j
13548 6f 75 72 6e 61 6c 20 66 6f 72 20 72 65 61 64 2f  ournal for read/
13549 77 72 69 74 65 20 61 63 63 65 73 73 2e 20 54 68  write access. Th
1354a 69 73 20 69 73 20 62 65 63 61 75 73 65 20 69 6e  is is because in
1354b 20 0a 20 20 20 20 20 20 2a 2a 20 65 78 63 6c 75   .      ** exclu
1354c 73 69 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65  sive-access mode
1354d 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
1354e 70 74 6f 72 20 77 69 6c 6c 20 62 65 20 6b 65 70  ptor will be kep
1354f 74 20 6f 70 65 6e 20 61 6e 64 0a 20 20 20 20 20  t open and.     
13550 20 2a 2a 20 70 6f 73 73 69 62 6c 79 20 75 73 65   ** possibly use
13551 64 20 66 6f 72 20 61 20 74 72 61 6e 73 61 63 74  d for a transact
13552 69 6f 6e 20 6c 61 74 65 72 20 6f 6e 2e 20 4f 6e  ion later on. On
13553 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c 20 74   some systems, t
13554 68 65 0a 20 20 20 20 20 20 2a 2a 20 4f 73 54 72  he.      ** OsTr
13555 75 6e 63 61 74 65 28 29 20 63 61 6c 6c 20 75 73  uncate() call us
13556 65 64 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d  ed in exclusive-
13557 61 63 63 65 73 73 20 6d 6f 64 65 20 61 6c 73 6f  access mode also
13558 20 72 65 71 75 69 72 65 73 0a 20 20 20 20 20 20   requires.      
13559 2a 2a 20 61 20 72 65 61 64 2f 77 72 69 74 65 20  ** a read/write 
1355a 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 20 20 20  file handle..   
1355b 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
1355c 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e  !isOpen(pPager->
1355d 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20  jfd) ){.        
1355e 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 20  int res;.       
1355f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41   rc = sqlite3OsA
13560 63 63 65 73 73 28 70 56 66 73 2c 70 50 61 67 65  ccess(pVfs,pPage
13561 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 53 51 4c 49  r->zJournal,SQLI
13562 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53  TE_ACCESS_EXISTS
13563 2c 26 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20  ,&res);.        
13564 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
13565 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  K ){.          i
13566 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20  f( res ){.      
13567 20 20 20 20 20 20 69 6e 74 20 66 6f 75 74 20 3d        int fout =
13568 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   0;.            
13569 69 6e 74 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f  int f = SQLITE_O
1356a 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51  PEN_READWRITE|SQ
1356b 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a  LITE_OPEN_MAIN_J
1356c 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20 20 20  OURNAL;.        
1356d 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61      assert( !pPa
1356e 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b  ger->tempFile );
1356f 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20  .            rc 
13570 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28  = sqlite3OsOpen(
13571 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a  pVfs, pPager->zJ
13572 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e  ournal, pPager->
13573 6a 66 64 2c 20 66 2c 20 26 66 6f 75 74 29 3b 0a  jfd, f, &fout);.
13574 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65              asse
13575 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  rt( rc!=SQLITE_O
13576 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 70 50 61 67  K || isOpen(pPag
13577 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 20 20  er->jfd) );.    
13578 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
13579 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 66 6f 75  SQLITE_OK && fou
1357a 74 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  t&SQLITE_OPEN_RE
1357b 41 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20  ADONLY ){.      
1357c 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
1357d 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20  ITE_CANTOPEN;.  
1357e 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1357f 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65  te3OsClose(pPage
13580 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 20  r->jfd);.       
13581 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
13582 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
13583 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f      /* If the jo
13584 75 72 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 20 65  urnal does not e
13585 78 69 73 74 2c 20 69 74 20 75 73 75 61 6c 6c 79  xist, it usually
13586 20 6d 65 61 6e 73 20 74 68 61 74 20 73 6f 6d 65   means that some
13587 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a   .            **
13588 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f   other connectio
13589 6e 20 6d 61 6e 61 67 65 64 20 74 6f 20 67 65 74  n managed to get
1358a 20 69 6e 20 61 6e 64 20 72 6f 6c 6c 20 69 74 20   in and roll it 
1358b 62 61 63 6b 20 62 65 66 6f 72 65 20 0a 20 20 20  back before .   
1358c 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69 73           ** this
1358d 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 74 61   connection obta
1358e 69 6e 65 64 20 74 68 65 20 65 78 63 6c 75 73 69  ined the exclusi
1358f 76 65 20 6c 6f 63 6b 20 61 62 6f 76 65 2e 20 4f  ve lock above. O
13590 72 2c 20 69 74 20 0a 20 20 20 20 20 20 20 20 20  r, it .         
13591 20 20 20 2a 2a 20 6d 61 79 20 6d 65 61 6e 20 74     ** may mean t
13592 68 61 74 20 74 68 65 20 70 61 67 65 72 20 77 61  hat the pager wa
13593 73 20 69 6e 20 74 68 65 20 65 72 72 6f 72 2d 73  s in the error-s
13594 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 0a 20  tate when this. 
13595 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 75             ** fu
13596 6e 63 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65  nction was calle
13597 64 20 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61  d and the journa
13598 6c 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20  l file does not 
13599 65 78 69 73 74 2e 20 20 2a 2f 0a 20 20 20 20 20  exist.  */.     
1359a 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65         rc = page
1359b 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  r_end_transactio
1359c 6e 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20  n(pPager, 0);.  
1359d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1359e 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1359f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
135a0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67  _OK ){.        g
135a1 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20  oto failed;.    
135a2 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 54 4f    }..      /* TO
135a3 44 4f 3a 20 57 68 79 20 61 72 65 20 74 68 65 73  DO: Why are thes
135a4 65 20 63 6c 65 61 72 65 64 20 68 65 72 65 3f 20  e cleared here? 
135a5 49 73 20 69 74 20 6e 65 63 65 73 73 61 72 79 3f  Is it necessary?
135a6 20 2a 2f 0a 20 20 20 20 20 20 70 50 61 67 65 72   */.      pPager
135a7 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64  ->journalStarted
135a8 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67   = 0;.      pPag
135a9 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d  er->journalOff =
135aa 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72   0;.      pPager
135ab 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 30 3b  ->setMaster = 0;
135ac 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a  .      pPager->j
135ad 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30 3b 0a 20  ournalHdr = 0;. 
135ae 0a 20 20 20 20 20 20 2f 2a 20 50 6c 61 79 62 61  .      /* Playba
135af 63 6b 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68  ck and delete th
135b0 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 44 72 6f 70  e journal.  Drop
135b1 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 72   the database wr
135b2 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 63  ite.      ** loc
135b3 6b 20 61 6e 64 20 72 65 61 63 71 75 69 72 65 20  k and reacquire 
135b4 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 20 50  the read lock. P
135b5 75 72 67 65 20 74 68 65 20 63 61 63 68 65 20 62  urge the cache b
135b6 65 66 6f 72 65 0a 20 20 20 20 20 20 2a 2a 20 70  efore.      ** p
135b7 6c 61 79 69 6e 67 20 62 61 63 6b 20 74 68 65 20  laying back the 
135b8 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 73 6f 20 74  hot-journal so t
135b9 68 61 74 20 77 65 20 64 6f 6e 27 74 20 65 6e 64  hat we don't end
135ba 20 75 70 20 77 69 74 68 0a 20 20 20 20 20 20 2a   up with.      *
135bb 2a 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e  * an inconsisten
135bc 74 20 63 61 63 68 65 2e 0a 20 20 20 20 20 20 2a  t cache..      *
135bd 2f 0a 20 20 20 20 20 20 69 66 28 20 69 73 4f 70  /.      if( isOp
135be 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
135bf 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
135c0 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70  pager_playback(p
135c1 50 61 67 65 72 2c 20 31 29 3b 0a 20 20 20 20 20  Pager, 1);.     
135c2 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
135c3 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
135c4 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 72 72    rc = pager_err
135c5 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a  or(pPager, rc);.
135c6 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66            goto f
135c7 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 7d  ailed;.        }
135c8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61  .      }.      a
135c9 73 73 65 72 74 28 20 28 70 50 61 67 65 72 2d 3e  ssert( (pPager->
135ca 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 48 41  state==PAGER_SHA
135cb 52 45 44 29 0a 20 20 20 20 20 20 20 20 20 20 20  RED).           
135cc 7c 7c 20 28 70 50 61 67 65 72 2d 3e 65 78 63 6c  || (pPager->excl
135cd 75 73 69 76 65 4d 6f 64 65 20 26 26 20 70 50 61  usiveMode && pPa
135ce 67 65 72 2d 3e 73 74 61 74 65 3e 50 41 47 45 52  ger->state>PAGER
135cf 5f 53 48 41 52 45 44 29 0a 20 20 20 20 20 20 29  _SHARED).      )
135d0 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
135d1 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70   pPager->pBackup
135d2 20 7c 7c 20 73 71 6c 69 74 65 33 50 63 61 63 68   || sqlite3Pcach
135d3 65 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65  ePagecount(pPage
135d4 72 2d 3e 70 50 43 61 63 68 65 29 3e 30 20 29 7b  r->pPCache)>0 ){
135d5 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 68  .      /* The sh
135d6 61 72 65 64 2d 6c 6f 63 6b 20 68 61 73 20 6a 75  ared-lock has ju
135d7 73 74 20 62 65 65 6e 20 61 63 71 75 69 72 65 64  st been acquired
135d8 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
135d9 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61   file.      ** a
135da 6e 64 20 74 68 65 72 65 20 61 72 65 20 61 6c 72  nd there are alr
135db 65 61 64 79 20 70 61 67 65 73 20 69 6e 20 74 68  eady pages in th
135dc 65 20 63 61 63 68 65 20 28 66 72 6f 6d 20 61 20  e cache (from a 
135dd 70 72 65 76 69 6f 75 73 0a 20 20 20 20 20 20 2a  previous.      *
135de 2a 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20  * read or write 
135df 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 20 20 43  transaction).  C
135e0 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
135e1 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20  he database.    
135e2 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f    ** has been mo
135e3 64 69 66 69 65 64 2e 20 20 49 66 20 74 68 65 20  dified.  If the 
135e4 64 61 74 61 62 61 73 65 20 68 61 73 20 63 68 61  database has cha
135e5 6e 67 65 64 2c 20 66 6c 75 73 68 20 74 68 65 0a  nged, flush the.
135e6 20 20 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 0a        ** cache..
135e7 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
135e8 2a 20 44 61 74 61 62 61 73 65 20 63 68 61 6e 67  * Database chang
135e9 65 73 20 69 73 20 64 65 74 65 63 74 65 64 20 62  es is detected b
135ea 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 31 35 20  y looking at 15 
135eb 62 79 74 65 73 20 62 65 67 69 6e 6e 69 6e 67 0a  bytes beginning.
135ec 20 20 20 20 20 20 2a 2a 20 61 74 20 6f 66 66 73        ** at offs
135ed 65 74 20 32 34 20 69 6e 74 6f 20 74 68 65 20 66  et 24 into the f
135ee 69 6c 65 2e 20 20 54 68 65 20 66 69 72 73 74 20  ile.  The first 
135ef 34 20 6f 66 20 74 68 65 73 65 20 31 36 20 62 79  4 of these 16 by
135f0 74 65 73 20 61 72 65 0a 20 20 20 20 20 20 2a 2a  tes are.      **
135f1 20 61 20 33 32 2d 62 69 74 20 63 6f 75 6e 74 65   a 32-bit counte
135f2 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 65 6d  r that is increm
135f3 65 6e 74 65 64 20 77 69 74 68 20 65 61 63 68 20  ented with each 
135f4 63 68 61 6e 67 65 2e 20 20 54 68 65 0a 20 20 20  change.  The.   
135f5 20 20 20 2a 2a 20 6f 74 68 65 72 20 62 79 74 65     ** other byte
135f6 73 20 63 68 61 6e 67 65 20 72 61 6e 64 6f 6d 6c  s change randoml
135f7 79 20 77 69 74 68 20 65 61 63 68 20 66 69 6c 65  y with each file
135f8 20 63 68 61 6e 67 65 20 77 68 65 6e 0a 20 20 20   change when.   
135f9 20 20 20 2a 2a 20 61 20 63 6f 64 65 63 20 69 73     ** a codec is
135fa 20 69 6e 20 75 73 65 2e 0a 20 20 20 20 20 20 2a   in use..      *
135fb 2a 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72  * .      ** Ther
135fc 65 20 69 73 20 61 20 76 61 6e 69 73 68 69 6e 67  e is a vanishing
135fd 6c 79 20 73 6d 61 6c 6c 20 63 68 61 6e 63 65 20  ly small chance 
135fe 74 68 61 74 20 61 20 63 68 61 6e 67 65 20 77 69  that a change wi
135ff 6c 6c 20 6e 6f 74 20 62 65 20 0a 20 20 20 20 20  ll not be .     
13600 20 2a 2a 20 64 65 74 65 63 74 65 64 2e 20 20 54   ** detected.  T
13601 68 65 20 63 68 61 6e 63 65 20 6f 66 20 61 6e 20  he chance of an 
13602 75 6e 64 65 74 65 63 74 65 64 20 63 68 61 6e 67  undetected chang
13603 65 20 69 73 20 73 6f 20 73 6d 61 6c 6c 20 74 68  e is so small th
13604 61 74 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 63  at.      ** it c
13605 61 6e 20 62 65 20 6e 65 67 6c 65 63 74 65 64 2e  an be neglected.
13606 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
13607 63 68 61 72 20 64 62 46 69 6c 65 56 65 72 73 5b  char dbFileVers[
13608 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64  sizeof(pPager->d
13609 62 46 69 6c 65 56 65 72 73 29 5d 3b 0a 20 20 20  bFileVers)];.   
1360a 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50     sqlite3PagerP
1360b 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c  agecount(pPager,
1360c 20 30 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20   0);..      if( 
1360d 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20  pPager->errCode 
1360e 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
1360f 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b  pPager->errCode;
13610 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61  .        goto fa
13611 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  iled;.      }.. 
13612 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
13613 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64  ger->dbSizeValid
13614 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50   );.      if( pP
13615 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e 30 20 29  ager->dbSize>0 )
13616 7b 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41 43  {.        IOTRAC
13617 45 28 28 22 43 4b 56 45 52 53 20 25 70 20 25 64  E(("CKVERS %p %d
13618 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 73 69 7a  \n", pPager, siz
13619 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29 29  eof(dbFileVers))
1361a 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
1361b 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50  sqlite3OsRead(pP
1361c 61 67 65 72 2d 3e 66 64 2c 20 26 64 62 46 69 6c  ager->fd, &dbFil
1361d 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 64 62  eVers, sizeof(db
1361e 46 69 6c 65 56 65 72 73 29 2c 20 32 34 29 3b 0a  FileVers), 24);.
1361f 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
13620 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
13621 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c         goto fail
13622 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ed;.        }.  
13623 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
13624 20 20 20 6d 65 6d 73 65 74 28 64 62 46 69 6c 65     memset(dbFile
13625 56 65 72 73 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Vers, 0, sizeof(
13626 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20  dbFileVers));.  
13627 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28      }..      if(
13628 20 6d 65 6d 63 6d 70 28 70 50 61 67 65 72 2d 3e   memcmp(pPager->
13629 64 62 46 69 6c 65 56 65 72 73 2c 20 64 62 46 69  dbFileVers, dbFi
1362a 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 64  leVers, sizeof(d
1362b 62 46 69 6c 65 56 65 72 73 29 29 21 3d 30 20 29  bFileVers))!=0 )
1362c 7b 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 5f  {.        pager_
1362d 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20  reset(pPager);. 
1362e 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1362f 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
13630 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c  >exclusiveMode |
13631 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d  | pPager->state=
13632 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b  =PAGER_SHARED );
13633 0a 20 20 7d 0a 0a 20 66 61 69 6c 65 64 3a 0a 20  .  }.. failed:. 
13634 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
13635 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 70 61 67  OK ){.    /* pag
13636 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 69 73 20 61  er_unlock() is a
13637 20 6e 6f 2d 6f 70 20 66 6f 72 20 65 78 63 6c 75   no-op for exclu
13638 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 20 69 6e  sive mode and in
13639 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
1363a 73 2e 20 2a 2f 0a 20 20 20 20 70 61 67 65 72 5f  s. */.    pager_
1363b 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a  unlock(pPager);.
1363c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1363d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  .}../*.** If the
1363e 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
1363f 20 68 61 73 20 72 65 61 63 68 65 64 20 7a 65 72   has reached zer
13640 6f 2c 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 79 20  o, rollback any 
13641 61 63 74 69 76 65 0a 2a 2a 20 74 72 61 6e 73 61  active.** transa
13642 63 74 69 6f 6e 20 61 6e 64 20 75 6e 6c 6f 63 6b  ction and unlock
13643 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2a 0a 2a   the pager..**.*
13644 2a 20 45 78 63 65 70 74 2c 20 69 6e 20 6c 6f 63  * Except, in loc
13645 6b 69 6e 67 5f 6d 6f 64 65 3d 45 58 43 4c 55 53  king_mode=EXCLUS
13646 49 56 45 20 77 68 65 6e 20 74 68 65 72 65 20 69  IVE when there i
13647 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 69 6e 0a  s nothing to in.
13648 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  ** the rollback 
13649 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 20 75 6e 6c  journal, the unl
1364a 6f 63 6b 20 69 73 20 6e 6f 74 20 70 65 72 66 6f  ock is not perfo
1364b 72 6d 65 64 20 61 6e 64 20 74 68 65 72 65 20 69  rmed and there i
1364c 73 0a 2a 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20  s.** nothing to 
1364d 72 6f 6c 6c 62 61 63 6b 2c 20 73 6f 20 74 68 69  rollback, so thi
1364e 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
1364f 6f 2d 6f 70 2e 0a 2a 2f 20 0a 73 74 61 74 69 63  o-op..*/ .static
13650 20 76 6f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63   void pagerUnloc
13651 6b 49 66 55 6e 75 73 65 64 28 50 61 67 65 72 20  kIfUnused(Pager 
13652 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20  *pPager){.  if( 
13653 28 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65  (sqlite3PcacheRe
13654 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70  fCount(pPager->p
13655 50 43 61 63 68 65 29 3d 3d 30 29 0a 20 20 20 26  PCache)==0).   &
13656 26 20 28 21 70 50 61 67 65 72 2d 3e 65 78 63 6c  & (!pPager->excl
13657 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 70 50 61  usiveMode || pPa
13658 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e  ger->journalOff>
13659 30 29 20 0a 20 20 29 7b 0a 20 20 20 20 70 61 67  0) .  ){.    pag
1365a 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62  erUnlockAndRollb
1365b 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  ack(pPager);.  }
1365c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72  .}../*.** Acquir
1365d 65 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  e a reference to
1365e 20 70 61 67 65 20 6e 75 6d 62 65 72 20 70 67 6e   page number pgn
1365f 6f 20 69 6e 20 70 61 67 65 72 20 70 50 61 67 65  o in pager pPage
13660 72 20 28 61 20 70 61 67 65 0a 2a 2a 20 72 65 66  r (a page.** ref
13661 65 72 65 6e 63 65 20 68 61 73 20 74 79 70 65 20  erence has type 
13662 44 62 50 61 67 65 2a 29 2e 20 49 66 20 74 68 65  DbPage*). If the
13663 20 72 65 71 75 65 73 74 65 64 20 72 65 66 65 72   requested refer
13664 65 6e 63 65 20 69 73 20 0a 2a 2a 20 73 75 63 63  ence is .** succ
13665 65 73 73 66 75 6c 6c 79 20 6f 62 74 61 69 6e 65  essfully obtaine
13666 64 2c 20 69 74 20 69 73 20 63 6f 70 69 65 64 20  d, it is copied 
13667 74 6f 20 2a 70 70 50 61 67 65 20 61 6e 64 20 53  to *ppPage and S
13668 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65  QLITE_OK returne
13669 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
1366a 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69  requested page i
1366b 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  s already in the
1366c 20 63 61 63 68 65 2c 20 69 74 20 69 73 20 72 65   cache, it is re
1366d 74 75 72 6e 65 64 2e 20 0a 2a 2a 20 4f 74 68 65  turned. .** Othe
1366e 72 77 69 73 65 2c 20 61 20 6e 65 77 20 70 61 67  rwise, a new pag
1366f 65 20 6f 62 6a 65 63 74 20 69 73 20 61 6c 6c 6f  e object is allo
13670 63 61 74 65 64 20 61 6e 64 20 70 6f 70 75 6c 61  cated and popula
13671 74 65 64 20 77 69 74 68 20 64 61 74 61 0a 2a 2a  ted with data.**
13672 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64   read from the d
13673 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 6e  atabase file. In
13674 20 73 6f 6d 65 20 63 61 73 65 73 2c 20 74 68 65   some cases, the
13675 20 70 63 61 63 68 65 20 6d 6f 64 75 6c 65 20 6d   pcache module m
13676 61 79 0a 2a 2a 20 63 68 6f 6f 73 65 20 6e 6f 74  ay.** choose not
13677 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e   to allocate a n
13678 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20 61  ew page object a
13679 6e 64 20 6d 61 79 20 72 65 75 73 65 20 61 6e 20  nd may reuse an 
1367a 65 78 69 73 74 69 6e 67 0a 2a 2a 20 6f 62 6a 65  existing.** obje
1367b 63 74 20 77 69 74 68 20 6e 6f 20 6f 75 74 73 74  ct with no outst
1367c 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65  anding reference
1367d 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 78 74  s..**.** The ext
1367e 72 61 20 64 61 74 61 20 61 70 70 65 6e 64 65 64  ra data appended
1367f 20 74 6f 20 61 20 70 61 67 65 20 69 73 20 61 6c   to a page is al
13680 77 61 79 73 20 69 6e 69 74 69 61 6c 69 7a 65 64  ways initialized
13681 20 74 6f 20 7a 65 72 6f 73 20 74 68 65 20 0a 2a   to zeros the .*
13682 2a 20 66 69 72 73 74 20 74 69 6d 65 20 61 20 70  * first time a p
13683 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20 69 6e  age is loaded in
13684 74 6f 20 6d 65 6d 6f 72 79 2e 20 49 66 20 74 68  to memory. If th
13685 65 20 70 61 67 65 20 72 65 71 75 65 73 74 65 64  e page requested
13686 20 69 73 20 0a 2a 2a 20 61 6c 72 65 61 64 79 20   is .** already 
13687 69 6e 20 74 68 65 20 63 61 63 68 65 20 77 68 65  in the cache whe
13688 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
13689 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20  is called, then 
1368a 74 68 65 20 65 78 74 72 61 0a 2a 2a 20 64 61 74  the extra.** dat
1368b 61 20 69 73 20 6c 65 66 74 20 61 73 20 69 74 20  a is left as it 
1368c 77 61 73 20 77 68 65 6e 20 74 68 65 20 70 61 67  was when the pag
1368d 65 20 6f 62 6a 65 63 74 20 77 61 73 20 6c 61 73  e object was las
1368e 74 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  t used..**.** If
1368f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d   the database im
13690 61 67 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 74  age is smaller t
13691 68 61 6e 20 74 68 65 20 72 65 71 75 65 73 74 65  han the requeste
13692 64 20 70 61 67 65 20 6f 72 20 69 66 20 61 20 0a  d page or if a .
13693 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75  ** non-zero valu
13694 65 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74  e is passed as t
13695 68 65 20 6e 6f 43 6f 6e 74 65 6e 74 20 70 61 72  he noContent par
13696 61 6d 65 74 65 72 20 61 6e 64 20 74 68 65 20 0a  ameter and the .
13697 2a 2a 20 72 65 71 75 65 73 74 65 64 20 70 61 67  ** requested pag
13698 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  e is not already
13699 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63   stored in the c
1369a 61 63 68 65 2c 20 74 68 65 6e 20 6e 6f 20 0a 2a  ache, then no .*
1369b 2a 20 61 63 74 75 61 6c 20 64 69 73 6b 20 72 65  * actual disk re
1369c 61 64 20 6f 63 63 75 72 73 2e 20 49 6e 20 74 68  ad occurs. In th
1369d 69 73 20 63 61 73 65 20 74 68 65 20 6d 65 6d 6f  is case the memo
1369e 72 79 20 69 6d 61 67 65 20 6f 66 20 74 68 65 20  ry image of the 
1369f 0a 2a 2a 20 70 61 67 65 20 69 73 20 69 6e 69 74  .** page is init
136a0 69 61 6c 69 7a 65 64 20 74 6f 20 61 6c 6c 20 7a  ialized to all z
136a1 65 72 6f 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20  eros. .**.** If 
136a2 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 20 74 72 75  noContent is tru
136a3 65 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  e, it means that
136a4 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20   we do not care 
136a5 61 62 6f 75 74 20 74 68 65 20 63 6f 6e 74 65 6e  about the conten
136a6 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 67  ts.** of the pag
136a7 65 2e 20 54 68 69 73 20 6f 63 63 75 72 73 20 69  e. This occurs i
136a8 6e 20 74 77 6f 20 73 65 70 65 72 61 74 65 20 73  n two seperate s
136a9 63 65 6e 61 72 69 6f 73 3a 0a 2a 2a 0a 2a 2a 20  cenarios:.**.** 
136aa 20 20 61 29 20 57 68 65 6e 20 72 65 61 64 69 6e    a) When readin
136ab 67 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65  g a free-list le
136ac 61 66 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65  af page from the
136ad 20 64 61 74 61 62 61 73 65 2c 20 61 6e 64 0a 2a   database, and.*
136ae 2a 0a 2a 2a 20 20 20 62 29 20 57 68 65 6e 20 61  *.**   b) When a
136af 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 62 65   savepoint is be
136b0 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20  ing rolled back 
136b1 61 6e 64 20 77 65 20 6e 65 65 64 20 74 6f 20 6c  and we need to l
136b2 6f 61 64 0a 2a 2a 20 20 20 20 20 20 61 20 6e 65  oad.**      a ne
136b3 77 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20  w page into the 
136b4 63 61 63 68 65 20 74 6f 20 70 6f 70 75 6c 61 74  cache to populat
136b5 65 20 77 69 74 68 20 74 68 65 20 64 61 74 61 20  e with the data 
136b6 72 65 61 64 0a 2a 2a 20 20 20 20 20 20 66 72 6f  read.**      fro
136b7 6d 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20  m the savepoint 
136b8 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49  journal..**.** I
136b9 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 20 74  f noContent is t
136ba 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 64 61  rue, then the da
136bb 74 61 20 72 65 74 75 72 6e 65 64 20 69 73 20 7a  ta returned is z
136bc 65 72 6f 65 64 20 69 6e 73 74 65 61 64 20 6f 66  eroed instead of
136bd 0a 2a 2a 20 62 65 69 6e 67 20 72 65 61 64 20 66  .** being read f
136be 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
136bf 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20  . Additionally, 
136c0 74 68 65 20 62 69 74 73 20 63 6f 72 72 65 73 70  the bits corresp
136c1 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 70 67 6e  onding.** to pgn
136c2 6f 20 69 6e 20 50 61 67 65 72 2e 70 49 6e 4a 6f  o in Pager.pInJo
136c3 75 72 6e 61 6c 20 28 62 69 74 76 65 63 20 6f 66  urnal (bitvec of
136c4 20 70 61 67 65 73 20 61 6c 72 65 61 64 79 20 77   pages already w
136c5 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a 2a 2a  ritten to the.**
136c6 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 61   journal file) a
136c7 6e 64 20 74 68 65 20 50 61 67 65 72 53 61 76 65  nd the PagerSave
136c8 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69  point.pInSavepoi
136c9 6e 74 20 62 69 74 76 65 63 73 20 6f 66 20 61 6e  nt bitvecs of an
136ca 79 20 6f 70 65 6e 0a 2a 2a 20 73 61 76 65 70 6f  y open.** savepo
136cb 69 6e 74 73 20 61 72 65 20 73 65 74 2e 20 54 68  ints are set. Th
136cc 69 73 20 6d 65 61 6e 73 20 69 66 20 74 68 65 20  is means if the 
136cd 70 61 67 65 20 69 73 20 6d 61 64 65 20 77 72 69  page is made wri
136ce 74 61 62 6c 65 20 61 74 20 61 6e 79 0a 2a 2a 20  table at any.** 
136cf 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74  point in the fut
136d0 75 72 65 2c 20 75 73 69 6e 67 20 61 20 63 61 6c  ure, using a cal
136d1 6c 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65  l to sqlite3Page
136d2 72 57 72 69 74 65 28 29 2c 20 69 74 73 20 63 6f  rWrite(), its co
136d3 6e 74 65 6e 74 73 0a 2a 2a 20 77 69 6c 6c 20 6e  ntents.** will n
136d4 6f 74 20 62 65 20 6a 6f 75 72 6e 61 6c 65 64 2e  ot be journaled.
136d5 20 54 68 69 73 20 73 61 76 65 73 20 49 4f 2e 0a   This saves IO..
136d6 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 71 75 69 73  **.** The acquis
136d7 69 74 69 6f 6e 20 6d 69 67 68 74 20 66 61 69 6c  ition might fail
136d8 20 66 6f 72 20 73 65 76 65 72 61 6c 20 72 65 61   for several rea
136d9 73 6f 6e 73 2e 20 20 49 6e 20 61 6c 6c 20 63 61  sons.  In all ca
136da 73 65 73 2c 0a 2a 2a 20 61 6e 20 61 70 70 72 6f  ses,.** an appro
136db 70 72 69 61 74 65 20 65 72 72 6f 72 20 63 6f 64  priate error cod
136dc 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  e is returned an
136dd 64 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 74  d *ppPage is set
136de 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20   to NULL..**.** 
136df 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 33  See also sqlite3
136e0 50 61 67 65 72 4c 6f 6f 6b 75 70 28 29 2e 20 20  PagerLookup().  
136e1 42 6f 74 68 20 74 68 69 73 20 72 6f 75 74 69 6e  Both this routin
136e2 65 20 61 6e 64 20 4c 6f 6f 6b 75 70 28 29 20 61  e and Lookup() a
136e3 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 66 69 6e  ttempt.** to fin
136e4 64 20 61 20 70 61 67 65 20 69 6e 20 74 68 65 20  d a page in the 
136e5 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 20  in-memory cache 
136e6 66 69 72 73 74 2e 20 20 49 66 20 74 68 65 20 70  first.  If the p
136e7 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61  age is not alrea
136e8 64 79 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 2c  dy.** in memory,
136e9 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 67 6f   this routine go
136ea 65 73 20 74 6f 20 64 69 73 6b 20 74 6f 20 72 65  es to disk to re
136eb 61 64 20 69 74 20 69 6e 20 77 68 65 72 65 61 73  ad it in whereas
136ec 20 4c 6f 6f 6b 75 70 28 29 0a 2a 2a 20 6a 75 73   Lookup().** jus
136ed 74 20 72 65 74 75 72 6e 73 20 30 2e 20 20 54 68  t returns 0.  Th
136ee 69 73 20 72 6f 75 74 69 6e 65 20 61 63 71 75 69  is routine acqui
136ef 72 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20  res a read-lock 
136f0 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 69  the first time i
136f1 74 0a 2a 2a 20 68 61 73 20 74 6f 20 67 6f 20 74  t.** has to go t
136f2 6f 20 64 69 73 6b 2c 20 61 6e 64 20 63 6f 75 6c  o disk, and coul
136f3 64 20 61 6c 73 6f 20 70 6c 61 79 62 61 63 6b 20  d also playback 
136f4 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 69  an old journal i
136f5 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20  f necessary..** 
136f6 53 69 6e 63 65 20 4c 6f 6f 6b 75 70 28 29 20 6e  Since Lookup() n
136f7 65 76 65 72 20 67 6f 65 73 20 74 6f 20 64 69 73  ever goes to dis
136f8 6b 2c 20 69 74 20 6e 65 76 65 72 20 68 61 73 20  k, it never has 
136f9 74 6f 20 64 65 61 6c 20 77 69 74 68 20 6c 6f 63  to deal with loc
136fa 6b 73 0a 2a 2a 20 6f 72 20 6a 6f 75 72 6e 61 6c  ks.** or journal
136fb 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54   files..*/.SQLIT
136fc 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
136fd 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72  lite3PagerAcquir
136fe 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  e(.  Pager *pPag
136ff 65 72 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  er,      /* The 
13700 70 61 67 65 72 20 6f 70 65 6e 20 6f 6e 20 74 68  pager open on th
13701 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
13702 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20  */.  Pgno pgno, 
13703 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
13704 20 6e 75 6d 62 65 72 20 74 6f 20 66 65 74 63 68   number to fetch
13705 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 2a 70   */.  DbPage **p
13706 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 57 72 69  pPage,    /* Wri
13707 74 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  te a pointer to 
13708 74 68 65 20 70 61 67 65 20 68 65 72 65 20 2a 2f  the page here */
13709 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74  .  int noContent
1370a 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74         /* Do not
1370b 20 62 6f 74 68 65 72 20 72 65 61 64 69 6e 67 20   bother reading 
1370c 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 64 69 73  content from dis
1370d 6b 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a  k if true */.){.
1370e 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 67 48 64    int rc;.  PgHd
1370f 72 20 2a 70 50 67 3b 0a 0a 20 20 61 73 73 65 72  r *pPg;..  asser
13710 74 28 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f  t( assert_pager_
13711 73 74 61 74 65 28 70 50 61 67 65 72 29 20 29 3b  state(pPager) );
13712 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
13713 72 2d 3e 73 74 61 74 65 3e 50 41 47 45 52 5f 55  r->state>PAGER_U
13714 4e 4c 4f 43 4b 20 29 3b 0a 0a 20 20 69 66 28 20  NLOCK );..  if( 
13715 70 67 6e 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 72  pgno==0 ){.    r
13716 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
13717 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a  RUPT_BKPT;.  }..
13718 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65    /* If the page
13719 72 20 69 73 20 69 6e 20 74 68 65 20 65 72 72 6f  r is in the erro
1371a 72 20 73 74 61 74 65 2c 20 72 65 74 75 72 6e 20  r state, return 
1371b 61 6e 20 65 72 72 6f 72 20 69 6d 6d 65 64 69 61  an error immedia
1371c 74 65 6c 79 2e 20 0a 20 20 2a 2a 20 4f 74 68 65  tely. .  ** Othe
1371d 72 77 69 73 65 2c 20 72 65 71 75 65 73 74 20 74  rwise, request t
1371e 68 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65  he page from the
1371f 20 50 43 61 63 68 65 20 6c 61 79 65 72 2e 20 2a   PCache layer. *
13720 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  /.  if( pPager->
13721 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f  errCode!=SQLITE_
13722 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72  OK && pPager->er
13723 72 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 46 55  rCode!=SQLITE_FU
13724 4c 4c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70  LL ){.    rc = p
13725 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a  Pager->errCode;.
13726 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
13727 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 46  = sqlite3PcacheF
13728 65 74 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43  etch(pPager->pPC
13729 61 63 68 65 2c 20 70 67 6e 6f 2c 20 31 2c 20 70  ache, pgno, 1, p
1372a 70 50 61 67 65 29 3b 0a 20 20 7d 0a 0a 20 20 69  pPage);.  }..  i
1372b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1372c 20 29 7b 0a 20 20 20 20 2f 2a 20 45 69 74 68 65   ){.    /* Eithe
1372d 72 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71  r the call to sq
1372e 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68  lite3PcacheFetch
1372f 28 29 20 72 65 74 75 72 6e 65 64 20 61 6e 20 65  () returned an e
13730 72 72 6f 72 20 6f 72 20 74 68 65 0a 20 20 20 20  rror or the.    
13731 2a 2a 20 70 61 67 65 72 20 77 61 73 20 61 6c 72  ** pager was alr
13732 65 61 64 79 20 69 6e 20 74 68 65 20 65 72 72 6f  eady in the erro
13733 72 2d 73 74 61 74 65 20 77 68 65 6e 20 74 68 69  r-state when thi
13734 73 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 63  s function was c
13735 61 6c 6c 65 64 2e 0a 20 20 20 20 2a 2a 20 53 65  alled..    ** Se
13736 74 20 70 50 67 20 74 6f 20 30 20 61 6e 64 20 6a  t pPg to 0 and j
13737 75 6d 70 20 74 6f 20 74 68 65 20 65 78 63 65 70  ump to the excep
13738 74 69 6f 6e 20 68 61 6e 64 6c 65 72 2e 20 20 2a  tion handler.  *
13739 2f 0a 20 20 20 20 70 50 67 20 3d 20 30 3b 0a 20  /.    pPg = 0;. 
1373a 20 20 20 67 6f 74 6f 20 70 61 67 65 72 5f 61 63     goto pager_ac
1373b 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 7d 0a 20  quire_err;.  }. 
1373c 20 61 73 73 65 72 74 28 20 28 2a 70 70 50 61 67   assert( (*ppPag
1373d 65 29 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f 20 29  e)->pgno==pgno )
1373e 3b 0a 20 20 61 73 73 65 72 74 28 20 28 2a 70 70  ;.  assert( (*pp
1373f 50 61 67 65 29 2d 3e 70 50 61 67 65 72 3d 3d 70  Page)->pPager==p
13740 50 61 67 65 72 20 7c 7c 20 28 2a 70 70 50 61 67  Pager || (*ppPag
13741 65 29 2d 3e 70 50 61 67 65 72 3d 3d 30 20 29 3b  e)->pPager==0 );
13742 0a 0a 20 20 69 66 28 20 28 2a 70 70 50 61 67 65  ..  if( (*ppPage
13743 29 2d 3e 70 50 61 67 65 72 20 29 7b 0a 20 20 20  )->pPager ){.   
13744 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65   /* In this case
13745 20 74 68 65 20 70 63 61 63 68 65 20 61 6c 72 65   the pcache alre
13746 61 64 79 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20  ady contains an 
13747 69 6e 69 74 69 61 6c 69 7a 65 64 20 63 6f 70 79  initialized copy
13748 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 20 70   of.    ** the p
13749 61 67 65 2e 20 52 65 74 75 72 6e 20 77 69 74 68  age. Return with
1374a 6f 75 74 20 66 75 72 74 68 65 72 20 61 64 6f 2e  out further ado.
1374b 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
1374c 20 70 67 6e 6f 3c 3d 50 41 47 45 52 5f 4d 41 58   pgno<=PAGER_MAX
1374d 5f 50 47 4e 4f 20 26 26 20 70 67 6e 6f 21 3d 50  _PGNO && pgno!=P
1374e 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61  AGER_MJ_PGNO(pPa
1374f 67 65 72 29 20 29 3b 0a 20 20 20 20 50 41 47 45  ger) );.    PAGE
13750 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e  R_INCR(pPager->n
13751 48 69 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  Hit);.    return
13752 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 7d   SQLITE_OK;..  }
13753 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65  else{.    /* The
13754 20 70 61 67 65 72 20 63 61 63 68 65 20 68 61 73   pager cache has
13755 20 63 72 65 61 74 65 64 20 61 20 6e 65 77 20 70   created a new p
13756 61 67 65 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74  age. Its content
13757 20 6e 65 65 64 73 20 74 6f 20 0a 20 20 20 20 2a   needs to .    *
13758 2a 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64  * be initialized
13759 2e 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 4d  .  */.    int nM
1375a 61 78 3b 0a 0a 20 20 20 20 50 41 47 45 52 5f 49  ax;..    PAGER_I
1375b 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 4d 69 73  NCR(pPager->nMis
1375c 73 29 3b 0a 20 20 20 20 70 50 67 20 3d 20 2a 70  s);.    pPg = *p
1375d 70 50 61 67 65 3b 0a 20 20 20 20 70 50 67 2d 3e  pPage;.    pPg->
1375e 70 50 61 67 65 72 20 3d 20 70 50 61 67 65 72 3b  pPager = pPager;
1375f 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 78  ..    /* The max
13760 69 6d 75 6d 20 70 61 67 65 20 6e 75 6d 62 65 72  imum page number
13761 20 69 73 20 32 5e 33 31 2e 20 52 65 74 75 72 6e   is 2^31. Return
13762 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20   SQLITE_CORRUPT 
13763 69 66 20 61 20 70 61 67 65 0a 20 20 20 20 2a 2a  if a page.    **
13764 20 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72 20   number greater 
13765 74 68 61 6e 20 74 68 69 73 2c 20 6f 72 20 74 68  than this, or th
13766 65 20 75 6e 75 73 65 64 20 6c 6f 63 6b 69 6e 67  e unused locking
13767 2d 70 61 67 65 2c 20 69 73 20 72 65 71 75 65 73  -page, is reques
13768 74 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ted. */.    if( 
13769 70 67 6e 6f 3e 50 41 47 45 52 5f 4d 41 58 5f 50  pgno>PAGER_MAX_P
1376a 47 4e 4f 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47  GNO || pgno==PAG
1376b 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65  ER_MJ_PGNO(pPage
1376c 72 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  r) ){.      rc =
1376d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1376e 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f  BKPT;.      goto
1376f 20 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65   pager_acquire_e
13770 72 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72  rr;.    }..    r
13771 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
13772 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72  Pagecount(pPager
13773 2c 20 26 6e 4d 61 78 29 3b 0a 20 20 20 20 69 66  , &nMax);.    if
13774 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
13775 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61  ){.      goto pa
13776 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3b  ger_acquire_err;
13777 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
13778 6e 4d 61 78 3c 28 69 6e 74 29 70 67 6e 6f 20 7c  nMax<(int)pgno |
13779 7c 20 4d 45 4d 44 42 20 7c 7c 20 6e 6f 43 6f 6e  | MEMDB || noCon
1377a 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 69 66  tent ){.      if
1377b 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 6d  ( pgno>pPager->m
1377c 78 50 67 6e 6f 20 29 7b 0a 09 72 63 20 3d 20 53  xPgno ){..rc = S
1377d 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 09 67 6f 74  QLITE_FULL;..got
1377e 6f 20 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f  o pager_acquire_
1377f 65 72 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  err;.      }.   
13780 20 20 20 69 66 28 20 6e 6f 43 6f 6e 74 65 6e 74     if( noContent
13781 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46   ){.        /* F
13782 61 69 6c 75 72 65 20 74 6f 20 73 65 74 20 74 68  ailure to set th
13783 65 20 62 69 74 73 20 69 6e 20 74 68 65 20 49 6e  e bits in the In
13784 4a 6f 75 72 6e 61 6c 20 62 69 74 2d 76 65 63 74  Journal bit-vect
13785 6f 72 73 20 69 73 20 62 65 6e 69 67 6e 2e 0a 20  ors is benign.. 
13786 20 20 20 20 20 20 20 2a 2a 20 49 74 20 6d 65 72         ** It mer
13787 65 6c 79 20 6d 65 61 6e 73 20 74 68 61 74 20 77  ely means that w
13788 65 20 6d 69 67 68 74 20 64 6f 20 73 6f 6d 65 20  e might do some 
13789 65 78 74 72 61 20 77 6f 72 6b 20 74 6f 20 6a 6f  extra work to jo
1378a 75 72 6e 61 6c 20 61 20 0a 20 20 20 20 20 20 20  urnal a .       
1378b 20 2a 2a 20 70 61 67 65 20 74 68 61 74 20 64 6f   ** page that do
1378c 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62  es not need to b
1378d 65 20 6a 6f 75 72 6e 61 6c 65 64 2e 20 20 4e 65  e journaled.  Ne
1378e 76 65 72 74 68 65 6c 65 73 73 2c 20 62 65 20 73  vertheless, be s
1378f 75 72 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  ure .        ** 
13790 74 6f 20 74 65 73 74 20 74 68 65 20 63 61 73 65  to test the case
13791 20 77 68 65 72 65 20 61 20 6d 61 6c 6c 6f 63 20   where a malloc 
13792 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69  error occurs whi
13793 6c 65 20 74 72 79 69 6e 67 20 74 6f 20 73 65 74  le trying to set
13794 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 62   .        ** a b
13795 69 74 20 69 6e 20 61 20 62 69 74 20 76 65 63 74  it in a bit vect
13796 6f 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  or..        */. 
13797 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65         sqlite3Be
13798 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
13799 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
1379a 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f  gno<=pPager->dbO
1379b 72 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  rigSize ){.     
1379c 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72       TESTONLY( r
1379d 63 20 3d 20 29 20 73 71 6c 69 74 65 33 42 69 74  c = ) sqlite3Bit
1379e 76 65 63 53 65 74 28 70 50 61 67 65 72 2d 3e 70  vecSet(pPager->p
1379f 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 6e 6f 29  InJournal, pgno)
137a0 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65 73 74  ;.          test
137a1 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45  case( rc==SQLITE
137a2 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20  _NOMEM );.      
137a3 20 20 7d 0a 20 20 20 20 20 20 20 20 54 45 53 54    }.        TEST
137a4 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 61 64 64  ONLY( rc = ) add
137a5 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76 65  ToSavepointBitve
137a6 63 73 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 29  cs(pPager, pgno)
137a7 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  ;.        testca
137a8 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  se( rc==SQLITE_N
137a9 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20  OMEM );.        
137aa 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e  sqlite3EndBenign
137ab 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20  Malloc();.      
137ac 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6d  }else{.        m
137ad 65 6d 73 65 74 28 70 50 67 2d 3e 70 44 61 74 61  emset(pPg->pData
137ae 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e 70 61 67  , 0, pPager->pag
137af 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a  eSize);.      }.
137b0 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22        IOTRACE(("
137b1 5a 45 52 4f 20 25 70 20 25 64 5c 6e 22 2c 20 70  ZERO %p %d\n", p
137b2 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20  Pager, pgno));. 
137b3 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
137b4 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 50 61  assert( pPg->pPa
137b5 67 65 72 3d 3d 70 50 61 67 65 72 20 29 3b 0a 20  ger==pPager );. 
137b6 20 20 20 20 20 72 63 20 3d 20 72 65 61 64 44 62       rc = readDb
137b7 50 61 67 65 28 70 50 67 29 3b 0a 20 20 20 20 20  Page(pPg);.     
137b8 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
137b9 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  OK ){.        go
137ba 74 6f 20 70 61 67 65 72 5f 61 63 71 75 69 72 65  to pager_acquire
137bb 5f 65 72 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20  _err;.      }.  
137bc 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54    }.#ifdef SQLIT
137bd 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20  E_CHECK_PAGES.  
137be 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20    pPg->pageHash 
137bf 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68  = pager_pagehash
137c0 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20  (pPg);.#endif.  
137c1 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  }..  return SQLI
137c2 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65 72 5f 61 63  TE_OK;..pager_ac
137c3 71 75 69 72 65 5f 65 72 72 3a 0a 20 20 61 73 73  quire_err:.  ass
137c4 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
137c5 4f 4b 20 29 3b 0a 20 20 69 66 28 20 70 50 67 20  OK );.  if( pPg 
137c6 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63  ){.    sqlite3Pc
137c7 61 63 68 65 44 72 6f 70 28 70 50 67 29 3b 0a 20  acheDrop(pPg);. 
137c8 20 7d 0a 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b   }.  pagerUnlock
137c9 49 66 55 6e 75 73 65 64 28 70 50 61 67 65 72 29  IfUnused(pPager)
137ca 3b 0a 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 30  ;..  *ppPage = 0
137cb 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
137cc 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20  ../*.** Acquire 
137cd 61 20 70 61 67 65 20 69 66 20 69 74 20 69 73 20  a page if it is 
137ce 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 69  already in the i
137cf 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e 20  n-memory cache. 
137d0 20 44 6f 0a 2a 2a 20 6e 6f 74 20 72 65 61 64 20   Do.** not read 
137d1 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 64 69  the page from di
137d2 73 6b 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f  sk.  Return a po
137d3 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67  inter to the pag
137d4 65 2c 0a 2a 2a 20 6f 72 20 30 20 69 66 20 74 68  e,.** or 0 if th
137d5 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e  e page is not in
137d6 20 63 61 63 68 65 2e 20 41 6c 73 6f 2c 20 72 65   cache. Also, re
137d7 74 75 72 6e 20 30 20 69 66 20 74 68 65 20 0a 2a  turn 0 if the .*
137d8 2a 20 70 61 67 65 72 20 69 73 20 69 6e 20 50 41  * pager is in PA
137d9 47 45 52 5f 55 4e 4c 4f 43 4b 20 73 74 61 74 65  GER_UNLOCK state
137da 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   when this funct
137db 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a  ion is called,.*
137dc 2a 20 6f 72 20 69 66 20 74 68 65 20 70 61 67 65  * or if the page
137dd 72 20 69 73 20 69 6e 20 61 6e 20 65 72 72 6f 72  r is in an error
137de 20 73 74 61 74 65 20 6f 74 68 65 72 20 74 68 61   state other tha
137df 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e 0a 2a  n SQLITE_FULL..*
137e0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71  *.** See also sq
137e1 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 2e  lite3PagerGet().
137e2 20 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65    The difference
137e3 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 72 6f   between this ro
137e4 75 74 69 6e 65 0a 2a 2a 20 61 6e 64 20 73 71 6c  utine.** and sql
137e5 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 69  ite3PagerGet() i
137e6 73 20 74 68 61 74 20 5f 67 65 74 28 29 20 77 69  s that _get() wi
137e7 6c 6c 20 67 6f 20 74 6f 20 74 68 65 20 64 69 73  ll go to the dis
137e8 6b 20 61 6e 64 20 72 65 61 64 0a 2a 2a 20 69 6e  k and read.** in
137e9 20 74 68 65 20 70 61 67 65 20 69 66 20 74 68 65   the page if the
137ea 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72   page is not alr
137eb 65 61 64 79 20 69 6e 20 63 61 63 68 65 2e 20 20  eady in cache.  
137ec 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  This routine.** 
137ed 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 66 20  returns NULL if 
137ee 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20  the page is not 
137ef 69 6e 20 63 61 63 68 65 20 6f 72 20 69 66 20 61  in cache or if a
137f0 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 20   disk I/O error 
137f1 0a 2a 2a 20 68 61 73 20 65 76 65 72 20 68 61 70  .** has ever hap
137f2 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  pened..*/.SQLITE
137f3 5f 50 52 49 56 41 54 45 20 44 62 50 61 67 65 20  _PRIVATE DbPage 
137f4 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f  *sqlite3PagerLoo
137f5 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61 67 65  kup(Pager *pPage
137f6 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20  r, Pgno pgno){. 
137f7 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 30 3b   PgHdr *pPg = 0;
137f8 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
137f9 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  r!=0 );.  assert
137fa 28 20 70 67 6e 6f 21 3d 30 20 29 3b 0a 20 20 61  ( pgno!=0 );.  a
137fb 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70  ssert( pPager->p
137fc 50 43 61 63 68 65 21 3d 30 20 29 3b 0a 20 20 61  PCache!=0 );.  a
137fd 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
137fe 74 61 74 65 20 3e 20 50 41 47 45 52 5f 55 4e 4c  tate > PAGER_UNL
137ff 4f 43 4b 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  OCK );.  sqlite3
13800 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61 67  PcacheFetch(pPag
13801 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e  er->pPCache, pgn
13802 6f 2c 20 30 2c 20 26 70 50 67 29 3b 0a 20 20 72  o, 0, &pPg);.  r
13803 65 74 75 72 6e 20 70 50 67 3b 0a 7d 0a 0a 2f 2a  eturn pPg;.}../*
13804 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 70 61  .** Release a pa
13805 67 65 20 72 65 66 65 72 65 6e 63 65 2e 0a 2a 2a  ge reference..**
13806 0a 2a 2a 20 49 66 20 74 68 65 20 6e 75 6d 62 65  .** If the numbe
13807 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20  r of references 
13808 74 6f 20 74 68 65 20 70 61 67 65 20 64 72 6f 70  to the page drop
13809 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74   to zero, then t
1380a 68 65 0a 2a 2a 20 70 61 67 65 20 69 73 20 61 64  he.** page is ad
1380b 64 65 64 20 74 6f 20 74 68 65 20 4c 52 55 20 6c  ded to the LRU l
1380c 69 73 74 2e 20 20 57 68 65 6e 20 61 6c 6c 20 72  ist.  When all r
1380d 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6c 6c  eferences to all
1380e 20 70 61 67 65 73 0a 2a 2a 20 61 72 65 20 72 65   pages.** are re
1380f 6c 65 61 73 65 64 2c 20 61 20 72 6f 6c 6c 62 61  leased, a rollba
13810 63 6b 20 6f 63 63 75 72 73 20 61 6e 64 20 74 68  ck occurs and th
13811 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61  e lock on the da
13812 74 61 62 61 73 65 20 69 73 0a 2a 2a 20 72 65 6d  tabase is.** rem
13813 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  oved..*/.SQLITE_
13814 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
13815 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 44  ite3PagerUnref(D
13816 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 69  bPage *pPg){.  i
13817 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 50 61  f( pPg ){.    Pa
13818 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50  ger *pPager = pP
13819 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 20 20 73  g->pPager;.    s
1381a 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65  qlite3PcacheRele
1381b 61 73 65 28 70 50 67 29 3b 0a 20 20 20 20 70 61  ase(pPg);.    pa
1381c 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65  gerUnlockIfUnuse
1381d 64 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 7d  d(pPager);.  }.}
1381e 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d  ../*.** If the m
1381f 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ain journal file
13820 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
13821 6e 20 6f 70 65 6e 65 64 2c 20 65 6e 73 75 72 65  n opened, ensure
13822 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 73 75 62   that the.** sub
13823 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73  -journal file is
13824 20 6f 70 65 6e 20 74 6f 6f 2e 20 49 66 20 74 68   open too. If th
13825 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 69  e main journal i
13826 73 20 6e 6f 74 20 6f 70 65 6e 2c 0a 2a 2a 20 74  s not open,.** t
13827 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
13828 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53  a no-op..**.** S
13829 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
1382a 72 6e 65 64 20 69 66 20 65 76 65 72 79 74 68 69  rned if everythi
1382b 6e 67 20 67 6f 65 73 20 61 63 63 6f 72 64 69 6e  ng goes accordin
1382c 67 20 74 6f 20 70 6c 61 6e 2e 20 0a 2a 2a 20 41  g to plan. .** A
1382d 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58  n SQLITE_IOERR_X
1382e 58 58 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  XX error code is
1382f 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 20 63   returned if a c
13830 61 6c 6c 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74  all to .** sqlit
13831 65 33 4f 73 4f 70 65 6e 28 29 20 66 61 69 6c 73  e3OsOpen() fails
13832 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
13833 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 50  openSubJournal(P
13834 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
13835 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
13836 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 73 4f 70 65  _OK;.  if( isOpe
13837 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26  n(pPager->jfd) &
13838 26 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72  & !isOpen(pPager
13839 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 69  ->sjfd) ){.    i
1383a 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  f( pPager->journ
1383b 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f  alMode==PAGER_JO
1383c 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59  URNALMODE_MEMORY
1383d 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 75 62 6a   || pPager->subj
1383e 49 6e 4d 65 6d 6f 72 79 20 29 7b 0a 20 20 20 20  InMemory ){.    
1383f 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72    sqlite3MemJour
13840 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e  nalOpen(pPager->
13841 73 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65  sjfd);.    }else
13842 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67  {.      rc = pag
13843 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 67 65  erOpentemp(pPage
13844 72 2c 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c  r, pPager->sjfd,
13845 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42   SQLITE_OPEN_SUB
13846 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 7d 0a  JOURNAL);.    }.
13847 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
13848 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
13849 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
1384a 64 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f  d at the start o
1384b 66 20 65 76 65 72 79 20 77 72 69 74 65 20 74 72  f every write tr
1384c 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68  ansaction..** Th
1384d 65 72 65 20 6d 75 73 74 20 61 6c 72 65 61 64 79  ere must already
1384e 20 62 65 20 61 20 52 45 53 45 52 56 45 44 20 6f   be a RESERVED o
1384f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  r EXCLUSIVE lock
13850 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
13851 20 0a 2a 2a 20 66 69 6c 65 20 77 68 65 6e 20 74   .** file when t
13852 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
13853 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 70 65  alled..**.** Ope
13854 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  n the journal fi
13855 6c 65 20 66 6f 72 20 70 61 67 65 72 20 70 50 61  le for pager pPa
13856 67 65 72 20 61 6e 64 20 77 72 69 74 65 20 61 20  ger and write a 
13857 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 0a 2a  journal header.*
13858 2a 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f  * to the start o
13859 66 20 69 74 2e 20 49 66 20 74 68 65 72 65 20 61  f it. If there a
1385a 72 65 20 61 63 74 69 76 65 20 73 61 76 65 70 6f  re active savepo
1385b 69 6e 74 73 2c 20 6f 70 65 6e 20 74 68 65 20 73  ints, open the s
1385c 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 61 73  ub-journal.** as
1385d 20 77 65 6c 6c 2e 20 54 68 69 73 20 66 75 6e 63   well. This func
1385e 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65  tion is only use
1385f 64 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e  d when the journ
13860 61 6c 20 66 69 6c 65 20 69 73 20 62 65 69 6e 67  al file is being
13861 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 74 6f 20 77   .** opened to w
13862 72 69 74 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20  rite a rollback 
13863 6c 6f 67 20 66 6f 72 20 61 20 74 72 61 6e 73 61  log for a transa
13864 63 74 69 6f 6e 2e 20 49 74 20 69 73 20 6e 6f 74  ction. It is not
13865 20 75 73 65 64 20 0a 2a 2a 20 77 68 65 6e 20 6f   used .** when o
13866 70 65 6e 69 6e 67 20 61 20 68 6f 74 20 6a 6f 75  pening a hot jou
13867 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 72 6f 6c  rnal file to rol
13868 6c 20 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a  l it back..**.**
13869 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   If the journal 
1386a 66 69 6c 65 20 69 73 20 61 6c 72 65 61 64 79 20  file is already 
1386b 6f 70 65 6e 20 28 61 73 20 69 74 20 6d 61 79 20  open (as it may 
1386c 62 65 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20  be in exclusive 
1386d 6d 6f 64 65 29 2c 0a 2a 2a 20 74 68 65 6e 20 74  mode),.** then t
1386e 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6a 75 73  his function jus
1386f 74 20 77 72 69 74 65 73 20 61 20 6a 6f 75 72 6e  t writes a journ
13870 61 6c 20 68 65 61 64 65 72 20 74 6f 20 74 68 65  al header to the
13871 20 73 74 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a   start of the.**
13872 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 66 69   already open fi
13873 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68  le. .**.** Wheth
13874 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f  er or not the jo
13875 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70  urnal file is op
13876 65 6e 65 64 20 62 79 20 74 68 69 73 20 66 75 6e  ened by this fun
13877 63 74 69 6f 6e 2c 20 74 68 65 0a 2a 2a 20 50 61  ction, the.** Pa
13878 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 62  ger.pInJournal b
13879 69 74 76 65 63 20 73 74 72 75 63 74 75 72 65 20  itvec structure 
1387a 69 73 20 61 6c 6c 6f 63 61 74 65 64 2e 0a 2a 2a  is allocated..**
1387b 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
1387c 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69  E_OK if everythi
1387d 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c  ng is successful
1387e 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74  . Otherwise, ret
1387f 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e  urn .** SQLITE_N
13880 4f 4d 45 4d 20 69 66 20 74 68 65 20 61 74 74 65  OMEM if the atte
13881 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  mpt to allocate 
13882 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c  Pager.pInJournal
13883 20 66 61 69 6c 73 2c 20 6f 72 20 0a 2a 2a 20 61   fails, or .** a
13884 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20  n IO error code 
13885 69 66 20 6f 70 65 6e 69 6e 67 20 6f 72 20 77 72  if opening or wr
13886 69 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61  iting the journa
13887 6c 20 66 69 6c 65 20 66 61 69 6c 73 2e 0a 2a 2f  l file fails..*/
13888 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65  .static int page
13889 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 50  r_open_journal(P
1388a 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
1388b 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1388c 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
1388d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
1388e 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
1388f 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 63 6f  sqlite3_vfs * co
13890 6e 73 74 20 70 56 66 73 20 3d 20 70 50 61 67 65  nst pVfs = pPage
13891 72 2d 3e 70 56 66 73 3b 20 20 20 2f 2a 20 4c 6f  r->pVfs;   /* Lo
13892 63 61 6c 20 63 61 63 68 65 20 6f 66 20 76 66 73  cal cache of vfs
13893 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 0a 20 20 61   pointer */..  a
13894 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
13895 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45  tate>=PAGER_RESE
13896 52 56 45 44 20 29 3b 0a 20 20 61 73 73 65 72 74  RVED );.  assert
13897 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75  ( pPager->useJou
13898 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 72 74  rnal );.  assert
13899 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
1389a 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55  lMode!=PAGER_JOU
1389b 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a  RNALMODE_OFF );.
1389c 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
1389d 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20  ->pInJournal==0 
1389e 29 3b 0a 20 20 0a 20 20 2f 2a 20 49 66 20 61 6c  );.  .  /* If al
1389f 72 65 61 64 79 20 69 6e 20 74 68 65 20 65 72 72  ready in the err
138a0 6f 72 20 73 74 61 74 65 2c 20 74 68 69 73 20 66  or state, this f
138a1 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d  unction is a no-
138a2 6f 70 2e 20 20 42 75 74 20 6f 6e 0a 20 20 2a 2a  op.  But on.  **
138a3 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c   the other hand,
138a4 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
138a5 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 69 66   never called if
138a6 20 77 65 20 61 72 65 20 61 6c 72 65 61 64 79 20   we are already 
138a7 69 6e 0a 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72  in.  ** an error
138a8 20 73 74 61 74 65 2e 20 2a 2f 0a 20 20 69 66 28   state. */.  if(
138a9 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65   NEVER(pPager->e
138aa 72 72 43 6f 64 65 29 20 29 20 72 65 74 75 72 6e  rrCode) ) return
138ab 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
138ac 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73  ;..  /* TODO: Is
138ad 20 69 74 20 72 65 61 6c 6c 79 20 70 6f 73 73 69   it really possi
138ae 62 6c 65 20 74 6f 20 67 65 74 20 68 65 72 65 20  ble to get here 
138af 77 69 74 68 20 64 62 53 69 7a 65 56 61 6c 69 64  with dbSizeValid
138b0 3d 3d 30 3f 20 49 66 20 6e 6f 74 2c 0a 20 20 2a  ==0? If not,.  *
138b1 2a 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 50 61  * the call to Pa
138b2 67 65 72 50 61 67 65 63 6f 75 6e 74 28 29 20 63  gerPagecount() c
138b3 61 6e 20 62 65 20 72 65 6d 6f 76 65 64 2e 0a 20  an be removed.. 
138b4 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 28 20   */.  testcase( 
138b5 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61  pPager->dbSizeVa
138b6 6c 69 64 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69  lid==0 );.  sqli
138b7 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e  te3PagerPagecoun
138b8 74 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 0a 20  t(pPager, 0);.. 
138b9 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72   pPager->pInJour
138ba 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 42 69 74  nal = sqlite3Bit
138bb 76 65 63 43 72 65 61 74 65 28 70 50 61 67 65 72  vecCreate(pPager
138bc 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 69 66 28  ->dbSize);.  if(
138bd 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72   pPager->pInJour
138be 6e 61 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  nal==0 ){.    re
138bf 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
138c0 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65  M;.  }..  /* Ope
138c1 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  n the journal fi
138c2 6c 65 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20  le if it is not 
138c3 61 6c 72 65 61 64 79 20 6f 70 65 6e 2e 20 2a 2f  already open. */
138c4 0a 20 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70  .  if( !isOpen(p
138c5 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20  Pager->jfd) ){. 
138c6 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a     if( pPager->j
138c7 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45  ournalMode==PAGE
138c8 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45  R_JOURNALMODE_ME
138c9 4d 4f 52 59 20 29 7b 0a 20 20 20 20 20 20 73 71  MORY ){.      sq
138ca 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f  lite3MemJournalO
138cb 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
138cc 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
138cd 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 66 6c 61     const int fla
138ce 67 73 20 3d 20 20 20 20 20 20 20 20 20 20 20 20  gs =            
138cf 20 20 20 20 20 20 20 2f 2a 20 56 46 53 20 66 6c         /* VFS fl
138d0 61 67 73 20 74 6f 20 6f 70 65 6e 20 6a 6f 75 72  ags to open jour
138d1 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20  nal file */.    
138d2 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
138d3 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45  READWRITE|SQLITE
138d4 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 0a 20 20  _OPEN_CREATE|.  
138d5 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e 74        (pPager->t
138d6 65 6d 70 46 69 6c 65 20 3f 20 0a 20 20 20 20 20  empFile ? .     
138d7 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f 50 45       (SQLITE_OPE
138d8 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 7c  N_DELETEONCLOSE|
138d9 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50  SQLITE_OPEN_TEMP
138da 5f 4a 4f 55 52 4e 41 4c 29 3a 0a 20 20 20 20 20  _JOURNAL):.     
138db 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f 50 45       (SQLITE_OPE
138dc 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 0a  N_MAIN_JOURNAL).
138dd 20 20 20 20 20 20 20 20 29 3b 0a 23 69 66 64 65          );.#ifde
138de 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
138df 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20  ATOMIC_WRITE.   
138e0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4a     rc = sqlite3J
138e1 6f 75 72 6e 61 6c 4f 70 65 6e 28 0a 20 20 20 20  ournalOpen(.    
138e2 20 20 20 20 20 20 70 56 66 73 2c 20 70 50 61 67        pVfs, pPag
138e3 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50  er->zJournal, pP
138e4 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 67 73  ager->jfd, flags
138e5 2c 20 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a 65  , jrnlBufferSize
138e6 28 70 50 61 67 65 72 29 0a 20 20 20 20 20 20 29  (pPager).      )
138e7 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 72 63  ;.#else.      rc
138e8 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e   = sqlite3OsOpen
138e9 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a  (pVfs, pPager->z
138ea 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d  Journal, pPager-
138eb 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20 30 29 3b  >jfd, flags, 0);
138ec 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20  .#endif.    }.  
138ed 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51    assert( rc!=SQ
138ee 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65  LITE_OK || isOpe
138ef 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29  n(pPager->jfd) )
138f0 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 57 72 69  ;.  }...  /* Wri
138f1 74 65 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75  te the first jou
138f2 72 6e 61 6c 20 68 65 61 64 65 72 20 74 6f 20 74  rnal header to t
138f3 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
138f4 61 6e 64 20 6f 70 65 6e 20 0a 20 20 2a 2a 20 74  and open .  ** t
138f5 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69  he sub-journal i
138f6 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 20 20 2a  f necessary..  *
138f7 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
138f8 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20  TE_OK ){.    /* 
138f9 54 4f 44 4f 3a 20 43 68 65 63 6b 20 69 66 20 61  TODO: Check if a
138fa 6c 6c 20 6f 66 20 74 68 65 73 65 20 61 72 65 20  ll of these are 
138fb 72 65 61 6c 6c 79 20 72 65 71 75 69 72 65 64 2e  really required.
138fc 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   */.    pPager->
138fd 64 62 4f 72 69 67 53 69 7a 65 20 3d 20 70 50 61  dbOrigSize = pPa
138fe 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 20  ger->dbSize;.   
138ff 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
13900 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 20  Started = 0;.   
13901 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
13902 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65  c = 0;.    pPage
13903 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 20  r->nRec = 0;.   
13904 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
13905 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 70 50 61  Off = 0;.    pPa
13906 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d  ger->setMaster =
13907 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   0;.    pPager->
13908 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30 3b 0a  journalHdr = 0;.
13909 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 4a 6f      rc = writeJo
1390a 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29  urnalHdr(pPager)
1390b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d  ;.  }.  if( rc==
1390c 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61  SQLITE_OK && pPa
1390d 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20  ger->nSavepoint 
1390e 29 7b 0a 20 20 20 20 72 63 20 3d 20 6f 70 65 6e  ){.    rc = open
1390f 53 75 62 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65  SubJournal(pPage
13910 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72  r);.  }..  if( r
13911 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
13912 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65      sqlite3Bitve
13913 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d  cDestroy(pPager-
13914 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20  >pInJournal);.  
13915 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75    pPager->pInJou
13916 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  rnal = 0;.  }.  
13917 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
13918 0a 2a 2a 20 42 65 67 69 6e 20 61 20 77 72 69 74  .** Begin a writ
13919 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e  e-transaction on
1391a 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 70   the specified p
1391b 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 49 66 20  ager object. If 
1391c 61 20 0a 2a 2a 20 77 72 69 74 65 2d 74 72 61 6e  a .** write-tran
1391d 73 61 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65  saction has alre
1391e 61 64 79 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c  ady been opened,
1391f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
13920 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a  s a no-op..**.**
13921 20 49 66 20 74 68 65 20 65 78 46 6c 61 67 20 61   If the exFlag a
13922 72 67 75 6d 65 6e 74 20 69 73 20 66 61 6c 73 65  rgument is false
13923 2c 20 74 68 65 6e 20 61 63 71 75 69 72 65 20 61  , then acquire a
13924 74 20 6c 65 61 73 74 20 61 20 52 45 53 45 52 56  t least a RESERV
13925 45 44 0a 2a 2a 20 6c 6f 63 6b 20 6f 6e 20 74 68  ED.** lock on th
13926 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
13927 20 49 66 20 65 78 46 6c 61 67 20 69 73 20 74 72   If exFlag is tr
13928 75 65 2c 20 74 68 65 6e 20 61 63 71 75 69 72 65  ue, then acquire
13929 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 61 6e 20   at least.** an 
1392a 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20  EXCLUSIVE lock. 
1392b 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69  If such a lock i
1392c 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20  s already held, 
1392d 6e 6f 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a 20 66  no locking .** f
1392e 75 6e 63 74 69 6f 6e 73 20 6e 65 65 64 20 62 65  unctions need be
1392f 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 49   called..**.** I
13930 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 20  f this is not a 
13931 74 65 6d 70 6f 72 61 72 79 20 6f 72 20 69 6e 2d  temporary or in-
13932 6d 65 6d 6f 72 79 20 66 69 6c 65 20 61 6e 64 2c  memory file and,
13933 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
13934 65 20 69 73 20 0a 2a 2a 20 6f 70 65 6e 65 64 20  e is .** opened 
13935 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20 62 65  if it has not be
13936 65 6e 20 61 6c 72 65 61 64 79 2e 20 46 6f 72 20  en already. For 
13937 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  a temporary file
13938 2c 20 74 68 65 20 6f 70 65 6e 69 6e 67 20 0a 2a  , the opening .*
13939 2a 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  * of the journal
1393a 20 66 69 6c 65 20 69 73 20 64 65 66 65 72 72 65   file is deferre
1393b 64 20 75 6e 74 69 6c 20 74 68 65 72 65 20 69 73  d until there is
1393c 20 61 6e 20 61 63 74 75 61 6c 20 6e 65 65 64 20   an actual need 
1393d 74 6f 20 0a 2a 2a 20 77 72 69 74 65 20 74 6f 20  to .** write to 
1393e 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 54 4f 44  the journal. TOD
1393f 4f 3a 20 57 68 79 20 68 61 6e 64 6c 65 20 74 65  O: Why handle te
13940 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 64 69  mporary files di
13941 66 66 65 72 65 6e 74 6c 79 3f 0a 2a 2a 0a 2a 2a  fferently?.**.**
13942 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   If the journal 
13943 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 28  file is opened (
13944 6f 72 20 69 66 20 69 74 20 69 73 20 61 6c 72 65  or if it is alre
13945 61 64 79 20 6f 70 65 6e 29 2c 20 74 68 65 6e 20  ady open), then 
13946 61 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 68 65 61  a.** journal-hea
13947 64 65 72 20 69 73 20 77 72 69 74 74 65 6e 20 74  der is written t
13948 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 69  o the start of i
13949 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  t..**.** If the 
1394a 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 61 72 67  subjInMemory arg
1394b 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 2d 7a 65 72  ument is non-zer
1394c 6f 2c 20 74 68 65 6e 20 61 6e 79 20 73 75 62 2d  o, then any sub-
1394d 6a 6f 75 72 6e 61 6c 20 6f 70 65 6e 65 64 0a 2a  journal opened.*
1394e 2a 20 77 69 74 68 69 6e 20 74 68 69 73 20 74 72  * within this tr
1394f 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62  ansaction will b
13950 65 20 6f 70 65 6e 65 64 20 61 73 20 61 6e 20 69  e opened as an i
13951 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 2e 20 54  n-memory file. T
13952 68 69 73 0a 2a 2a 20 68 61 73 20 6e 6f 20 65 66  his.** has no ef
13953 66 65 63 74 20 69 66 20 74 68 65 20 73 75 62 2d  fect if the sub-
13954 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c 72 65 61  journal is alrea
13955 64 79 20 6f 70 65 6e 65 64 20 28 61 73 20 69 74  dy opened (as it
13956 20 6d 61 79 20 62 65 20 77 68 65 6e 0a 2a 2a 20   may be when.** 
13957 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75  running in exclu
13958 73 69 76 65 20 6d 6f 64 65 29 20 6f 72 20 69 66  sive mode) or if
13959 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
1395a 20 64 6f 65 73 20 6e 6f 74 20 72 65 71 75 69 72   does not requir
1395b 65 20 61 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e  e a.** sub-journ
1395c 61 6c 2e 20 49 66 20 74 68 65 20 73 75 62 6a 49  al. If the subjI
1395d 6e 4d 65 6d 6f 72 79 20 61 72 67 75 6d 65 6e 74  nMemory argument
1395e 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 61   is zero, then a
1395f 6e 79 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 73  ny required.** s
13960 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 69 6d  ub-journal is im
13961 70 6c 65 6d 65 6e 74 65 64 20 69 6e 2d 6d 65 6d  plemented in-mem
13962 6f 72 79 20 69 66 20 70 50 61 67 65 72 20 69 73  ory if pPager is
13963 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
13964 74 61 62 61 73 65 2c 20 0a 2a 2a 20 6f 72 20 75  tabase, .** or u
13965 73 69 6e 67 20 61 20 74 65 6d 70 6f 72 61 72 79  sing a temporary
13966 20 66 69 6c 65 20 6f 74 68 65 72 77 69 73 65 2e   file otherwise.
13967 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
13968 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
13969 67 65 72 42 65 67 69 6e 28 50 61 67 65 72 20 2a  gerBegin(Pager *
1396a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 78 46 6c  pPager, int exFl
1396b 61 67 2c 20 69 6e 74 20 73 75 62 6a 49 6e 4d 65  ag, int subjInMe
1396c 6d 6f 72 79 29 7b 0a 20 20 69 6e 74 20 72 63 20  mory){.  int rc 
1396d 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61  = SQLITE_OK;.  a
1396e 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
1396f 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f  tate!=PAGER_UNLO
13970 43 4b 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e  CK );.  pPager->
13971 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 3d 20 28  subjInMemory = (
13972 75 38 29 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 3b  u8)subjInMemory;
13973 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  .  if( pPager->s
13974 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 48 41 52  tate==PAGER_SHAR
13975 45 44 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  ED ){.    assert
13976 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75  ( pPager->pInJou
13977 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 61  rnal==0 );.    a
13978 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 26 26  ssert( !MEMDB &&
13979 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69   !pPager->tempFi
1397a 6c 65 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 4f 62  le );..    /* Ob
1397b 74 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 20  tain a RESERVED 
1397c 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
1397d 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68  base file. If th
1397e 65 20 65 78 46 6c 61 67 20 70 61 72 61 6d 65 74  e exFlag paramet
1397f 65 72 0a 20 20 20 20 2a 2a 20 69 73 20 74 72 75  er.    ** is tru
13980 65 2c 20 74 68 65 6e 20 69 6d 6d 65 64 69 61 74  e, then immediat
13981 65 6c 79 20 75 70 67 72 61 64 65 20 74 68 69 73  ely upgrade this
13982 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 45   to an EXCLUSIVE
13983 20 6c 6f 63 6b 2e 20 54 68 65 0a 20 20 20 20 2a   lock. The.    *
13984 2a 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 63  * busy-handler c
13985 61 6c 6c 62 61 63 6b 20 63 61 6e 20 62 65 20 75  allback can be u
13986 73 65 64 20 77 68 65 6e 20 75 70 67 72 61 64 69  sed when upgradi
13987 6e 67 20 74 6f 20 74 68 65 20 45 58 43 4c 55 53  ng to the EXCLUS
13988 49 56 45 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 2c  IVE.    ** lock,
13989 20 62 75 74 20 6e 6f 74 20 77 68 65 6e 20 6f 62   but not when ob
1398a 74 61 69 6e 69 6e 67 20 74 68 65 20 52 45 53 45  taining the RESE
1398b 52 56 45 44 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a  RVED lock..    *
1398c 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
1398d 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d  e3OsLock(pPager-
1398e 3e 66 64 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f  >fd, RESERVED_LO
1398f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  CK);.    if( rc=
13990 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
13991 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74      pPager->stat
13992 65 20 3d 20 50 41 47 45 52 5f 52 45 53 45 52 56  e = PAGER_RESERV
13993 45 44 3b 0a 20 20 20 20 20 20 69 66 28 20 65 78  ED;.      if( ex
13994 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20  Flag ){.        
13995 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f  rc = pager_wait_
13996 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20  on_lock(pPager, 
13997 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b  EXCLUSIVE_LOCK);
13998 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
13999 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 72 65      /* If the re
1399a 71 75 69 72 65 64 20 6c 6f 63 6b 73 20 77 65 72  quired locks wer
1399b 65 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f  e successfully o
1399c 62 74 61 69 6e 65 64 2c 20 6f 70 65 6e 20 74 68  btained, open th
1399d 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a  e journal.    **
1399e 20 66 69 6c 65 20 61 6e 64 20 77 72 69 74 65 20   file and write 
1399f 74 68 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61  the first journa
139a0 6c 2d 68 65 61 64 65 72 20 74 6f 20 69 74 2e 0a  l-header to it..
139a1 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72      */.    if( r
139a2 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
139a3 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
139a4 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode!=PAGER_JOURN
139a5 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b 0a 20 20  ALMODE_OFF ){.  
139a6 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 6f      rc = pager_o
139a7 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 61 67  pen_journal(pPag
139a8 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  er);.    }.  }el
139a9 73 65 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50  se if( isOpen(pP
139aa 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 70 50  ager->jfd) && pP
139ab 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
139ac 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  ==0 ){.    /* Th
139ad 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20  is happens when 
139ae 74 68 65 20 70 61 67 65 72 20 77 61 73 20 69 6e  the pager was in
139af 20 65 78 63 6c 75 73 69 76 65 2d 61 63 63 65 73   exclusive-acces
139b0 73 20 6d 6f 64 65 20 74 68 65 20 6c 61 73 74 0a  s mode the last.
139b1 20 20 20 20 2a 2a 20 74 69 6d 65 20 61 20 28 72      ** time a (r
139b2 65 61 64 20 6f 72 20 77 72 69 74 65 29 20 74 72  ead or write) tr
139b3 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 73 75  ansaction was su
139b4 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 6e 63 6c  ccessfully concl
139b5 75 64 65 64 0a 20 20 20 20 2a 2a 20 62 79 20 74  uded.    ** by t
139b6 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  his connection. 
139b7 49 6e 73 74 65 61 64 20 6f 66 20 64 65 6c 65 74  Instead of delet
139b8 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ing the journal 
139b9 66 69 6c 65 20 69 74 20 77 61 73 20 0a 20 20 20  file it was .   
139ba 20 2a 2a 20 6b 65 70 74 20 6f 70 65 6e 20 61 6e   ** kept open an
139bb 64 20 65 69 74 68 65 72 20 77 61 73 20 74 72 75  d either was tru
139bc 6e 63 61 74 65 64 20 74 6f 20 30 20 62 79 74 65  ncated to 0 byte
139bd 73 20 6f 72 20 69 74 73 20 68 65 61 64 65 72 20  s or its header 
139be 77 61 73 0a 20 20 20 20 2a 2a 20 6f 76 65 72 77  was.    ** overw
139bf 72 69 74 74 65 6e 20 77 69 74 68 20 7a 65 72 6f  ritten with zero
139c0 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  s..    */.    as
139c1 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 52  sert( pPager->nR
139c2 65 63 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  ec==0 );.    ass
139c3 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 4f  ert( pPager->dbO
139c4 72 69 67 53 69 7a 65 3d 3d 30 20 29 3b 0a 20 20  rigSize==0 );.  
139c5 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
139c6 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20  ->pInJournal==0 
139c7 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65  );.    rc = page
139c8 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70  r_open_journal(p
139c9 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 50  Pager);.  }..  P
139ca 41 47 45 52 54 52 41 43 45 28 28 22 54 52 41 4e  AGERTRACE(("TRAN
139cb 53 41 43 54 49 4f 4e 20 25 64 5c 6e 22 2c 20 50  SACTION %d\n", P
139cc 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29  AGERID(pPager)))
139cd 3b 0a 20 20 61 73 73 65 72 74 28 20 21 69 73 4f  ;.  assert( !isO
139ce 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
139cf 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72   || pPager->jour
139d0 6e 61 6c 4f 66 66 3e 30 20 7c 7c 20 72 63 21 3d  nalOff>0 || rc!=
139d1 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69  SQLITE_OK );.  i
139d2 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
139d3 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
139d4 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66  !pPager->dbModif
139d5 69 65 64 20 29 3b 0a 20 20 20 20 2f 2a 20 49 67  ied );.    /* Ig
139d6 6e 6f 72 65 20 61 6e 79 20 49 4f 20 65 72 72 6f  nore any IO erro
139d7 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 77 69  r that occurs wi
139d8 74 68 69 6e 20 70 61 67 65 72 5f 65 6e 64 5f 74  thin pager_end_t
139d9 72 61 6e 73 61 63 74 69 6f 6e 28 29 2e 20 54 68  ransaction(). Th
139da 65 0a 20 20 20 20 2a 2a 20 70 75 72 70 6f 73 65  e.    ** purpose
139db 20 6f 66 20 74 68 69 73 20 63 61 6c 6c 20 69 73   of this call is
139dc 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 69 6e   to reset the in
139dd 74 65 72 6e 61 6c 20 73 74 61 74 65 20 6f 66 20  ternal state of 
139de 74 68 65 20 70 61 67 65 72 0a 20 20 20 20 2a 2a  the pager.    **
139df 20 73 75 62 2d 73 79 73 74 65 6d 2e 20 49 74 20   sub-system. It 
139e0 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 69  doesn't matter i
139e1 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 66 69  f the journal-fi
139e2 6c 65 20 69 73 20 6e 6f 74 20 70 72 6f 70 65 72  le is not proper
139e3 6c 79 0a 20 20 20 20 2a 2a 20 66 69 6e 61 6c 69  ly.    ** finali
139e4 7a 65 64 20 61 74 20 74 68 69 73 20 70 6f 69 6e  zed at this poin
139e5 74 20 28 73 69 6e 63 65 20 69 74 20 69 73 20 6e  t (since it is n
139e6 6f 74 20 61 20 76 61 6c 69 64 20 6a 6f 75 72 6e  ot a valid journ
139e7 61 6c 20 66 69 6c 65 20 61 6e 79 77 61 79 29 2e  al file anyway).
139e8 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 61 67 65  .    */.    page
139e9 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  r_end_transactio
139ea 6e 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20  n(pPager, 0);.  
139eb 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
139ec 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 73  ../*.** Mark a s
139ed 69 6e 67 6c 65 20 64 61 74 61 20 70 61 67 65 20  ingle data page 
139ee 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20 54 68  as writeable. Th
139ef 65 20 70 61 67 65 20 69 73 20 77 72 69 74 74 65  e page is writte
139f0 6e 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 6d  n into the .** m
139f1 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 73  ain journal or s
139f2 75 62 2d 6a 6f 75 72 6e 61 6c 20 61 73 20 72 65  ub-journal as re
139f3 71 75 69 72 65 64 2e 20 49 66 20 74 68 65 20 70  quired. If the p
139f4 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 69  age is written i
139f5 6e 74 6f 0a 2a 2a 20 6f 6e 65 20 6f 66 20 74 68  nto.** one of th
139f6 65 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65 20  e journals, the 
139f7 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69  corresponding bi
139f8 74 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 20  t is set in the 
139f9 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75  .** Pager.pInJou
139fa 72 6e 61 6c 20 62 69 74 76 65 63 20 61 6e 64 20  rnal bitvec and 
139fb 74 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69  the PagerSavepoi
139fc 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20  nt.pInSavepoint 
139fd 62 69 74 76 65 63 73 0a 2a 2a 20 6f 66 20 61 6e  bitvecs.** of an
139fe 79 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74  y open savepoint
139ff 73 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65  s as appropriate
13a00 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
13a01 70 61 67 65 72 5f 77 72 69 74 65 28 50 67 48 64  pager_write(PgHd
13a02 72 20 2a 70 50 67 29 7b 0a 20 20 76 6f 69 64 20  r *pPg){.  void 
13a03 2a 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44  *pData = pPg->pD
13a04 61 74 61 3b 0a 20 20 50 61 67 65 72 20 2a 70 50  ata;.  Pager *pP
13a05 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67  ager = pPg->pPag
13a06 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  er;.  int rc = S
13a07 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20  QLITE_OK;..  /* 
13a08 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
13a09 6e 6f 74 20 63 61 6c 6c 65 64 20 75 6e 6c 65 73  not called unles
13a0a 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  s a transaction 
13a0b 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
13a0c 0a 20 20 2a 2a 20 73 74 61 72 74 65 64 2e 0a 20  .  ** started.. 
13a0d 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50   */.  assert( pP
13a0e 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47  ager->state>=PAG
13a0f 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 0a  ER_RESERVED );..
13a10 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72    /* If an error
13a11 20 68 61 73 20 62 65 65 6e 20 70 72 65 76 69 6f   has been previo
13a12 75 73 6c 79 20 64 65 74 65 63 74 65 64 2c 20 77  usly detected, w
13a13 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a  e should not be.
13a14 20 20 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 69    ** calling thi
13a15 73 20 72 6f 75 74 69 6e 65 2e 20 20 52 65 70 65  s routine.  Repe
13a16 61 74 20 74 68 65 20 65 72 72 6f 72 20 66 6f 72  at the error for
13a17 20 72 6f 62 75 73 74 6e 65 73 73 2e 0a 20 20 2a   robustness..  *
13a18 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50  /.  if( NEVER(pP
13a19 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29  ager->errCode) )
13a1a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
13a1b 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 20 2f 2a 20  >errCode;..  /* 
13a1c 48 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f 75  Higher-level rou
13a1d 74 69 6e 65 73 20 6e 65 76 65 72 20 63 61 6c 6c  tines never call
13a1e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
13a1f 66 20 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f  f database is no
13a20 74 0a 20 20 2a 2a 20 77 72 69 74 61 62 6c 65 2e  t.  ** writable.
13a21 20 20 42 75 74 20 63 68 65 63 6b 20 61 6e 79 77    But check anyw
13a22 61 79 2c 20 6a 75 73 74 20 66 6f 72 20 72 6f 62  ay, just for rob
13a23 75 73 74 6e 65 73 73 2e 20 2a 2f 0a 20 20 69 66  ustness. */.  if
13a24 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e  ( NEVER(pPager->
13a25 72 65 61 64 4f 6e 6c 79 29 20 29 20 72 65 74 75  readOnly) ) retu
13a26 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a  rn SQLITE_PERM;.
13a27 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 61 67  .  assert( !pPag
13a28 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 29 3b  er->setMaster );
13a29 0a 0a 20 20 43 48 45 43 4b 5f 50 41 47 45 28 70  ..  CHECK_PAGE(p
13a2a 50 67 29 3b 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20  Pg);..  /* Mark 
13a2b 74 68 65 20 70 61 67 65 20 61 73 20 64 69 72 74  the page as dirt
13a2c 79 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20  y.  If the page 
13a2d 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
13a2e 20 77 72 69 74 74 65 6e 0a 20 20 2a 2a 20 74 6f   written.  ** to
13a2f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74 68 65   the journal the
13a30 6e 20 77 65 20 63 61 6e 20 72 65 74 75 72 6e 20  n we can return 
13a31 72 69 67 68 74 20 61 77 61 79 2e 0a 20 20 2a 2f  right away..  */
13a32 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  .  sqlite3Pcache
13a33 4d 61 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0a  MakeDirty(pPg);.
13a34 20 20 69 66 28 20 70 61 67 65 49 6e 4a 6f 75 72    if( pageInJour
13a35 6e 61 6c 28 70 50 67 29 20 26 26 20 21 73 75 62  nal(pPg) && !sub
13a36 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 70 50  jRequiresPage(pP
13a37 67 29 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72  g) ){.    pPager
13a38 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 3d 20 31  ->dbModified = 1
13a39 3b 0a 20 20 7d 65 6c 73 65 7b 0a 0a 20 20 20 20  ;.  }else{..    
13a3a 2f 2a 20 49 66 20 77 65 20 67 65 74 20 74 68 69  /* If we get thi
13a3b 73 20 66 61 72 2c 20 69 74 20 6d 65 61 6e 73 20  s far, it means 
13a3c 74 68 61 74 20 74 68 65 20 70 61 67 65 20 6e 65  that the page ne
13a3d 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20 2a 2a  eds to be.    **
13a3e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
13a3f 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72  transaction jour
13a40 6e 61 6c 20 6f 72 20 74 68 65 20 63 6b 65 63 6b  nal or the ckeck
13a41 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c 0a 20 20  point journal.  
13a42 20 20 2a 2a 20 6f 72 20 62 6f 74 68 2e 0a 20 20    ** or both..  
13a43 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 48 69 67 68    **.    ** High
13a44 65 72 20 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65  er level routine
13a45 73 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61 6c  s should have al
13a46 72 65 61 64 79 20 73 74 61 72 74 65 64 20 61 20  ready started a 
13a47 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 20 20  transaction,.   
13a48 20 2a 2a 20 77 68 69 63 68 20 6d 65 61 6e 73 20   ** which means 
13a49 74 68 65 79 20 68 61 76 65 20 61 63 71 75 69 72  they have acquir
13a4a 65 64 20 74 68 65 20 6e 65 63 65 73 73 61 72 79  ed the necessary
13a4b 20 6c 6f 63 6b 73 20 61 6e 64 20 6f 70 65 6e 65   locks and opene
13a4c 64 0a 20 20 20 20 2a 2a 20 61 20 72 6f 6c 6c 62  d.    ** a rollb
13a4d 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20 44 6f  ack journal.  Do
13a4e 75 62 6c 65 2d 63 68 65 63 6b 20 74 6f 20 6d 61  uble-check to ma
13a4f 6b 65 73 20 73 75 72 65 20 74 68 69 73 20 69 73  kes sure this is
13a50 20 74 68 65 20 63 61 73 65 2e 0a 20 20 20 20 2a   the case..    *
13a51 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
13a52 65 33 50 61 67 65 72 42 65 67 69 6e 28 70 50 61  e3PagerBegin(pPa
13a53 67 65 72 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e  ger, 0, pPager->
13a54 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 29 3b 0a 20  subjInMemory);. 
13a55 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 21     if( NEVER(rc!
13a56 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20  =SQLITE_OK) ){. 
13a57 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
13a58 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 69      }.    if( !i
13a59 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
13a5a 64 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f  d) && pPager->jo
13a5b 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52  urnalMode!=PAGER
13a5c 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46  _JOURNALMODE_OFF
13a5d 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
13a5e 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75  ( pPager->useJou
13a5f 72 6e 61 6c 20 29 3b 0a 20 20 20 20 20 20 72 63  rnal );.      rc
13a60 20 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f   = pager_open_jo
13a61 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20  urnal(pPager);. 
13a62 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
13a63 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
13a64 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50  rc;.    }.    pP
13a65 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64  ager->dbModified
13a66 20 3d 20 31 3b 0a 20 20 0a 20 20 20 20 2f 2a 20   = 1;.  .    /* 
13a67 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  The transaction 
13a68 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 65 78 69 73  journal now exis
13a69 74 73 20 61 6e 64 20 77 65 20 68 61 76 65 20 61  ts and we have a
13a6a 20 52 45 53 45 52 56 45 44 20 6f 72 20 61 6e 0a   RESERVED or an.
13a6b 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 56 45      ** EXCLUSIVE
13a6c 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 6d 61 69   lock on the mai
13a6d 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  n database file.
13a6e 20 20 57 72 69 74 65 20 74 68 65 20 63 75 72 72    Write the curr
13a6f 65 6e 74 20 70 61 67 65 20 74 6f 0a 20 20 20 20  ent page to.    
13a70 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ** the transacti
13a71 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69 66 20 69 74  on journal if it
13a72 20 69 73 20 6e 6f 74 20 74 68 65 72 65 20 61 6c   is not there al
13a73 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20  ready..    */.  
13a74 20 20 69 66 28 20 21 70 61 67 65 49 6e 4a 6f 75    if( !pageInJou
13a75 72 6e 61 6c 28 70 50 67 29 20 26 26 20 69 73 4f  rnal(pPg) && isO
13a76 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
13a77 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50   ){.      if( pP
13a78 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d  g->pgno<=pPager-
13a79 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 7b 0a 20  >dbOrigSize ){. 
13a7a 20 20 20 20 20 20 20 75 33 32 20 63 6b 73 75 6d         u32 cksum
13a7b 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  ;.        char *
13a7c 70 44 61 74 61 32 3b 0a 0a 20 20 20 20 20 20 20  pData2;..       
13a7d 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 6e 65   /* We should ne
13a7e 76 65 72 20 77 72 69 74 65 20 74 6f 20 74 68 65  ver write to the
13a7f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68   journal file th
13a80 65 20 70 61 67 65 20 74 68 61 74 0a 20 20 20 20  e page that.    
13a81 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20      ** contains 
13a82 74 68 65 20 64 61 74 61 62 61 73 65 20 6c 6f 63  the database loc
13a83 6b 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ks.  The followi
13a84 6e 67 20 61 73 73 65 72 74 20 76 65 72 69 66 69  ng assert verifi
13a85 65 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  es.        ** th
13a86 61 74 20 77 65 20 64 6f 20 6e 6f 74 2e 20 2a 2f  at we do not. */
13a87 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
13a88 20 70 50 67 2d 3e 70 67 6e 6f 21 3d 50 41 47 45   pPg->pgno!=PAGE
13a89 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72  R_MJ_PGNO(pPager
13a8a 29 20 29 3b 0a 20 20 20 20 20 20 20 20 43 4f 44  ) );.        COD
13a8b 45 43 32 28 70 50 61 67 65 72 2c 20 70 44 61 74  EC2(pPager, pDat
13a8c 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 37 2c  a, pPg->pgno, 7,
13a8d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
13a8e 4f 4d 45 4d 2c 20 70 44 61 74 61 32 29 3b 0a 20  OMEM, pData2);. 
13a8f 20 20 20 20 20 20 20 63 6b 73 75 6d 20 3d 20 70         cksum = p
13a90 61 67 65 72 5f 63 6b 73 75 6d 28 70 50 61 67 65  ager_cksum(pPage
13a91 72 2c 20 28 75 38 2a 29 70 44 61 74 61 32 29 3b  r, (u8*)pData2);
13a92 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 77 72  .        rc = wr
13a93 69 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72  ite32bits(pPager
13a94 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a  ->jfd, pPager->j
13a95 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50 67 2d 3e  ournalOff, pPg->
13a96 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 69  pgno);.        i
13a97 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
13a98 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
13a99 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
13a9a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70  e(pPager->jfd, p
13a9b 44 61 74 61 32 2c 20 70 50 61 67 65 72 2d 3e 70  Data2, pPager->p
13a9c 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 20  ageSize,.       
13a9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13a9e 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a         pPager->j
13a9f 6f 75 72 6e 61 6c 4f 66 66 20 2b 20 34 29 3b 0a  ournalOff + 4);.
13aa0 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72            pPager
13aa1 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20  ->journalOff += 
13aa2 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
13aa3 2b 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  +4;.        }.  
13aa4 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
13aa5 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
13aa6 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 33       rc = write3
13aa7 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66  2bits(pPager->jf
13aa8 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  d, pPager->journ
13aa9 61 6c 4f 66 66 2c 20 63 6b 73 75 6d 29 3b 0a 20  alOff, cksum);. 
13aaa 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d           pPager-
13aab 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 34  >journalOff += 4
13aac 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
13aad 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 4f      IOTRACE(("JO
13aae 55 54 20 25 70 20 25 64 20 25 6c 6c 64 20 25 64  UT %p %d %lld %d
13aaf 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67  \n", pPager, pPg
13ab0 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20  ->pgno, .       
13ab1 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72            pPager
13ab2 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50  ->journalOff, pP
13ab3 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 29  ager->pageSize))
13ab4 3b 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 5f  ;.        PAGER_
13ab5 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67  INCR(sqlite3_pag
13ab6 65 72 5f 77 72 69 74 65 6a 5f 63 6f 75 6e 74 29  er_writej_count)
13ab7 3b 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 54  ;.        PAGERT
13ab8 52 41 43 45 28 28 22 4a 4f 55 52 4e 41 4c 20 25  RACE(("JOURNAL %
13ab9 64 20 70 61 67 65 20 25 64 20 6e 65 65 64 53 79  d page %d needSy
13aba 6e 63 3d 25 64 20 68 61 73 68 28 25 30 38 78 29  nc=%d hash(%08x)
13abb 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
13abc 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72    PAGERID(pPager
13abd 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20  ), pPg->pgno, . 
13abe 20 20 20 20 20 20 20 20 20 20 20 20 28 28 70 50              ((pP
13abf 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e  g->flags&PGHDR_N
13ac0 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 29 2c 20  EED_SYNC)?1:0), 
13ac1 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70  pager_pagehash(p
13ac2 50 67 29 29 29 3b 0a 0a 20 20 20 20 20 20 20 20  Pg)));..        
13ac3 2f 2a 20 45 76 65 6e 20 69 66 20 61 6e 20 49 4f  /* Even if an IO
13ac4 20 6f 72 20 64 69 73 6b 66 75 6c 6c 20 65 72 72   or diskfull err
13ac5 6f 72 20 6f 63 63 75 72 72 65 64 20 77 68 69 6c  or occurred whil
13ac6 65 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 74 68  e journalling th
13ac7 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67  e.        ** pag
13ac8 65 20 69 6e 20 74 68 65 20 62 6c 6f 63 6b 20 61  e in the block a
13ac9 62 6f 76 65 2c 20 73 65 74 20 74 68 65 20 6e 65  bove, set the ne
13aca 65 64 2d 73 79 6e 63 20 66 6c 61 67 20 66 6f 72  ed-sync flag for
13acb 20 74 68 65 20 70 61 67 65 2e 0a 20 20 20 20 20   the page..     
13acc 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c     ** Otherwise,
13acd 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61   when the transa
13ace 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20  ction is rolled 
13acf 62 61 63 6b 2c 20 74 68 65 20 6c 6f 67 69 63 20  back, the logic 
13ad0 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6c  in.        ** pl
13ad1 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28  ayback_one_page(
13ad2 29 20 77 69 6c 6c 20 74 68 69 6e 6b 20 74 68 61  ) will think tha
13ad3 74 20 74 68 65 20 70 61 67 65 20 6e 65 65 64 73  t the page needs
13ad4 20 74 6f 20 62 65 20 72 65 73 74 6f 72 65 64 0a   to be restored.
13ad5 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 68          ** in th
13ad6 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
13ad7 20 41 6e 64 20 69 66 20 61 6e 20 49 4f 20 65 72   And if an IO er
13ad8 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65  ror occurs while
13ad9 20 64 6f 69 6e 67 20 73 6f 2c 0a 20 20 20 20 20   doing so,.     
13ada 20 20 20 2a 2a 20 74 68 65 6e 20 63 6f 72 72 75     ** then corru
13adb 70 74 69 6f 6e 20 6d 61 79 20 66 6f 6c 6c 6f 77  ption may follow
13adc 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
13add 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72       if( !pPager
13ade 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20  ->noSync ){.    
13adf 20 20 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73        pPg->flags
13ae0 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53   |= PGHDR_NEED_S
13ae1 59 4e 43 3b 0a 20 20 20 20 20 20 20 20 20 20 70  YNC;.          p
13ae2 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20  Pager->needSync 
13ae3 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a  = 1;.        }..
13ae4 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 72          /* An er
13ae5 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64  ror has occurred
13ae6 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20   writing to the 
13ae7 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68  journal file. Th
13ae8 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72  e .        ** tr
13ae9 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62  ansaction will b
13aea 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 62 79  e rolled back by
13aeb 20 74 68 65 20 6c 61 79 65 72 20 61 62 6f 76 65   the layer above
13aec 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
13aed 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
13aee 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
13aef 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
13af0 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
13af1 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 2b 2b    pPager->nRec++
13af2 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
13af3 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75  ( pPager->pInJou
13af4 72 6e 61 6c 21 3d 30 20 29 3b 0a 20 20 20 20 20  rnal!=0 );.     
13af5 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
13af6 69 74 76 65 63 53 65 74 28 70 50 61 67 65 72 2d  itvecSet(pPager-
13af7 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67  >pInJournal, pPg
13af8 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20  ->pgno);.       
13af9 20 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53   testcase( rc==S
13afa 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20  QLITE_NOMEM );. 
13afb 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72         assert( r
13afc 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
13afd 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
13afe 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 7c   );.        rc |
13aff 3d 20 61 64 64 54 6f 53 61 76 65 70 6f 69 6e 74  = addToSavepoint
13b00 42 69 74 76 65 63 73 28 70 50 61 67 65 72 2c 20  Bitvecs(pPager, 
13b01 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20  pPg->pgno);.    
13b02 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
13b03 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
13b04 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53     assert( rc==S
13b05 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20  QLITE_NOMEM );. 
13b06 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
13b07 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  rc;.        }.  
13b08 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
13b09 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e     if( !pPager->
13b0a 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 26  journalStarted &
13b0b 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  & !pPager->noSyn
13b0c 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  c ){.          p
13b0d 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48  Pg->flags |= PGH
13b0e 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20  DR_NEED_SYNC;.  
13b0f 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
13b10 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20  needSync = 1;.  
13b11 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
13b12 50 41 47 45 52 54 52 41 43 45 28 28 22 41 50 50  PAGERTRACE(("APP
13b13 45 4e 44 20 25 64 20 70 61 67 65 20 25 64 20 6e  END %d page %d n
13b14 65 65 64 53 79 6e 63 3d 25 64 5c 6e 22 2c 0a 20  eedSync=%d\n",. 
13b15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50                 P
13b16 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20  AGERID(pPager), 
13b17 70 50 67 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20  pPg->pgno,.     
13b18 20 20 20 20 20 20 20 20 20 20 28 28 70 50 67 2d            ((pPg-
13b19 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45  >flags&PGHDR_NEE
13b1a 44 5f 53 59 4e 43 29 3f 31 3a 30 29 29 29 3b 0a  D_SYNC)?1:0)));.
13b1b 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
13b1c 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73  .    /* If the s
13b1d 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c  tatement journal
13b1e 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 68 65   is open and the
13b1f 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20   page is not in 
13b20 69 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20  it,.    ** then 
13b21 77 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e  write the curren
13b22 74 20 70 61 67 65 20 74 6f 20 74 68 65 20 73 74  t page to the st
13b23 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e  atement journal.
13b24 20 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 20 20    Note that.    
13b25 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  ** the statement
13b26 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20   journal format 
13b27 64 69 66 66 65 72 73 20 66 72 6f 6d 20 74 68 65  differs from the
13b28 20 73 74 61 6e 64 61 72 64 20 6a 6f 75 72 6e 61   standard journa
13b29 6c 20 66 6f 72 6d 61 74 0a 20 20 20 20 2a 2a 20  l format.    ** 
13b2a 69 6e 20 74 68 61 74 20 69 74 20 6f 6d 69 74 73  in that it omits
13b2b 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73 20 61   the checksums a
13b2c 6e 64 20 74 68 65 20 68 65 61 64 65 72 2e 0a 20  nd the header.. 
13b2d 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 75     */.    if( su
13b2e 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 70  bjRequiresPage(p
13b2f 50 67 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20  Pg) ){.      rc 
13b30 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65  = subjournalPage
13b31 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  (pPg);.    }.  }
13b32 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68  ..  /* Update th
13b33 65 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20  e database size 
13b34 61 6e 64 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f  and return..  */
13b35 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
13b36 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f  r->state>=PAGER_
13b37 53 48 41 52 45 44 20 29 3b 0a 20 20 69 66 28 20  SHARED );.  if( 
13b38 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3c 70  pPager->dbSize<p
13b39 50 67 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20  Pg->pgno ){.    
13b3a 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d  pPager->dbSize =
13b3b 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 7d 0a   pPg->pgno;.  }.
13b3c 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
13b3d 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 64 61 74  /*.** Mark a dat
13b3e 61 20 70 61 67 65 20 61 73 20 77 72 69 74 65 61  a page as writea
13b3f 62 6c 65 2e 20 54 68 69 73 20 72 6f 75 74 69 6e  ble. This routin
13b40 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64  e must be called
13b41 20 62 65 66 6f 72 65 20 0a 2a 2a 20 6d 61 6b 69   before .** maki
13b42 6e 67 20 63 68 61 6e 67 65 73 20 74 6f 20 61 20  ng changes to a 
13b43 70 61 67 65 2e 20 54 68 65 20 63 61 6c 6c 65 72  page. The caller
13b44 20 6d 75 73 74 20 63 68 65 63 6b 20 74 68 65 20   must check the 
13b45 72 65 74 75 72 6e 20 76 61 6c 75 65 20 0a 2a 2a  return value .**
13b46 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   of this functio
13b47 6e 20 61 6e 64 20 62 65 20 63 61 72 65 66 75 6c  n and be careful
13b48 20 6e 6f 74 20 74 6f 20 63 68 61 6e 67 65 20 61   not to change a
13b49 6e 79 20 70 61 67 65 20 64 61 74 61 20 75 6e 6c  ny page data unl
13b4a 65 73 73 20 0a 2a 2a 20 74 68 69 73 20 72 6f 75  ess .** this rou
13b4b 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c  tine returns SQL
13b4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68  ITE_OK..**.** Th
13b4d 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74  e difference bet
13b4e 77 65 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  ween this functi
13b4f 6f 6e 20 61 6e 64 20 70 61 67 65 72 5f 77 72 69  on and pager_wri
13b50 74 65 28 29 20 69 73 20 74 68 61 74 20 74 68 69  te() is that thi
13b51 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 6c  s.** function al
13b52 73 6f 20 64 65 61 6c 73 20 77 69 74 68 20 74 68  so deals with th
13b53 65 20 73 70 65 63 69 61 6c 20 63 61 73 65 20 77  e special case w
13b54 68 65 72 65 20 32 20 6f 72 20 6d 6f 72 65 20 70  here 2 or more p
13b55 61 67 65 73 0a 2a 2a 20 66 69 74 20 6f 6e 20 61  ages.** fit on a
13b56 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63   single disk sec
13b57 74 6f 72 2e 20 49 6e 20 74 68 69 73 20 63 61 73  tor. In this cas
13b58 65 20 61 6c 6c 20 63 6f 2d 72 65 73 69 64 65 6e  e all co-residen
13b59 74 20 70 61 67 65 73 0a 2a 2a 20 6d 75 73 74 20  t pages.** must 
13b5a 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 65  have been writte
13b5b 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  n to the journal
13b5c 20 66 69 6c 65 20 62 65 66 6f 72 65 20 72 65 74   file before ret
13b5d 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66  urning..**.** If
13b5e 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
13b5f 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f  , SQLITE_NOMEM o
13b60 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f  r an IO error co
13b61 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a  de is returned.*
13b62 2a 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65  * as appropriate
13b63 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c  . Otherwise, SQL
13b64 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54  ITE_OK..*/.SQLIT
13b65 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
13b66 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
13b67 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 29  DbPage *pDbPage)
13b68 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
13b69 49 54 45 5f 4f 4b 3b 0a 0a 20 20 50 67 48 64 72  ITE_OK;..  PgHdr
13b6a 20 2a 70 50 67 20 3d 20 70 44 62 50 61 67 65 3b   *pPg = pDbPage;
13b6b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
13b6c 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a   = pPg->pPager;.
13b6d 20 20 50 67 6e 6f 20 6e 50 61 67 65 50 65 72 53    Pgno nPagePerS
13b6e 65 63 74 6f 72 20 3d 20 28 70 50 61 67 65 72 2d  ector = (pPager-
13b6f 3e 73 65 63 74 6f 72 53 69 7a 65 2f 70 50 61 67  >sectorSize/pPag
13b70 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 0a  er->pageSize);..
13b71 20 20 69 66 28 20 6e 50 61 67 65 50 65 72 53 65    if( nPagePerSe
13b72 63 74 6f 72 3e 31 20 29 7b 0a 20 20 20 20 50 67  ctor>1 ){.    Pg
13b73 6e 6f 20 6e 50 61 67 65 43 6f 75 6e 74 3b 20 20  no nPageCount;  
13b74 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c          /* Total
13b75 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
13b76 20 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c   in database fil
13b77 65 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 70 67  e */.    Pgno pg
13b78 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
13b79 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65     /* First page
13b7a 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 70   of the sector p
13b7b 50 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e  Pg is located on
13b7c 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 50 61  . */.    int nPa
13b7d 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
13b7e 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
13b7f 70 61 67 65 73 20 73 74 61 72 74 69 6e 67 20 61  pages starting a
13b80 74 20 70 67 31 20 74 6f 20 6a 6f 75 72 6e 61 6c  t pg1 to journal
13b81 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 20   */.    int ii; 
13b82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13b83 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
13b84 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 65 65  r */.    int nee
13b85 64 53 79 6e 63 20 3d 20 30 3b 20 20 20 20 20 20  dSync = 0;      
13b86 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e     /* True if an
13b87 79 20 70 61 67 65 20 68 61 73 20 50 47 48 44 52  y page has PGHDR
13b88 5f 4e 45 45 44 5f 53 59 4e 43 20 2a 2f 0a 0a 20  _NEED_SYNC */.. 
13b89 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 64 6f     /* Set the do
13b8a 4e 6f 74 53 79 6e 63 20 66 6c 61 67 20 74 6f 20  NotSync flag to 
13b8b 31 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75  1. This is becau
13b8c 73 65 20 77 65 20 63 61 6e 6e 6f 74 20 61 6c 6c  se we cannot all
13b8d 6f 77 20 61 20 6a 6f 75 72 6e 61 6c 0a 20 20 20  ow a journal.   
13b8e 20 2a 2a 20 68 65 61 64 65 72 20 74 6f 20 62 65   ** header to be
13b8f 20 77 72 69 74 74 65 6e 20 62 65 74 77 65 65 6e   written between
13b90 20 74 68 65 20 70 61 67 65 73 20 6a 6f 75 72 6e   the pages journ
13b91 61 6c 65 64 20 62 79 20 74 68 69 73 20 66 75 6e  aled by this fun
13b92 63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20  ction..    */.  
13b93 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42    assert( !MEMDB
13b94 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
13b95 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e  pPager->doNotSyn
13b96 63 3d 3d 30 20 29 3b 0a 20 20 20 20 70 50 61 67  c==0 );.    pPag
13b97 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 3d 20  er->doNotSync = 
13b98 31 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20  1;..    /* This 
13b99 74 72 69 63 6b 20 61 73 73 75 6d 65 73 20 74 68  trick assumes th
13b9a 61 74 20 62 6f 74 68 20 74 68 65 20 70 61 67 65  at both the page
13b9b 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74 6f 72  -size and sector
13b9c 2d 73 69 7a 65 20 61 72 65 0a 20 20 20 20 2a 2a  -size are.    **
13b9d 20 61 6e 20 69 6e 74 65 67 65 72 20 70 6f 77 65   an integer powe
13b9e 72 20 6f 66 20 32 2e 20 49 74 20 73 65 74 73 20  r of 2. It sets 
13b9f 76 61 72 69 61 62 6c 65 20 70 67 31 20 74 6f 20  variable pg1 to 
13ba0 74 68 65 20 69 64 65 6e 74 69 66 69 65 72 0a 20  the identifier. 
13ba1 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 66 69 72     ** of the fir
13ba2 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 73  st page of the s
13ba3 65 63 74 6f 72 20 70 50 67 20 69 73 20 6c 6f 63  ector pPg is loc
13ba4 61 74 65 64 20 6f 6e 2e 0a 20 20 20 20 2a 2f 0a  ated on..    */.
13ba5 20 20 20 20 70 67 31 20 3d 20 28 28 70 50 67 2d      pg1 = ((pPg-
13ba6 3e 70 67 6e 6f 2d 31 29 20 26 20 7e 28 6e 50 61  >pgno-1) & ~(nPa
13ba7 67 65 50 65 72 53 65 63 74 6f 72 2d 31 29 29 20  gePerSector-1)) 
13ba8 2b 20 31 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65  + 1;..    sqlite
13ba9 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
13baa 70 50 61 67 65 72 2c 20 28 69 6e 74 20 2a 29 26  pPager, (int *)&
13bab 6e 50 61 67 65 43 6f 75 6e 74 29 3b 0a 20 20 20  nPageCount);.   
13bac 20 69 66 28 20 70 50 67 2d 3e 70 67 6e 6f 3e 6e   if( pPg->pgno>n
13bad 50 61 67 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20  PageCount ){.   
13bae 20 20 20 6e 50 61 67 65 20 3d 20 28 70 50 67 2d     nPage = (pPg-
13baf 3e 70 67 6e 6f 20 2d 20 70 67 31 29 2b 31 3b 0a  >pgno - pg1)+1;.
13bb0 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70      }else if( (p
13bb1 67 31 2b 6e 50 61 67 65 50 65 72 53 65 63 74 6f  g1+nPagePerSecto
13bb2 72 2d 31 29 3e 6e 50 61 67 65 43 6f 75 6e 74 20  r-1)>nPageCount 
13bb3 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d  ){.      nPage =
13bb4 20 6e 50 61 67 65 43 6f 75 6e 74 2b 31 2d 70 67   nPageCount+1-pg
13bb5 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  1;.    }else{.  
13bb6 20 20 20 20 6e 50 61 67 65 20 3d 20 6e 50 61 67      nPage = nPag
13bb7 65 50 65 72 53 65 63 74 6f 72 3b 0a 20 20 20 20  ePerSector;.    
13bb8 7d 0a 20 20 20 20 61 73 73 65 72 74 28 6e 50 61  }.    assert(nPa
13bb9 67 65 3e 30 29 3b 0a 20 20 20 20 61 73 73 65 72  ge>0);.    asser
13bba 74 28 70 67 31 3c 3d 70 50 67 2d 3e 70 67 6e 6f  t(pg1<=pPg->pgno
13bbb 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 28 70  );.    assert((p
13bbc 67 31 2b 6e 50 61 67 65 29 3e 70 50 67 2d 3e 70  g1+nPage)>pPg->p
13bbd 67 6e 6f 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69  gno);..    for(i
13bbe 69 3d 30 3b 20 69 69 3c 6e 50 61 67 65 20 26 26  i=0; ii<nPage &&
13bbf 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20   rc==SQLITE_OK; 
13bc0 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 50 67 6e  ii++){.      Pgn
13bc1 6f 20 70 67 20 3d 20 70 67 31 2b 69 69 3b 0a 20  o pg = pg1+ii;. 
13bc2 20 20 20 20 20 50 67 48 64 72 20 2a 70 50 61 67       PgHdr *pPag
13bc3 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 67 3d  e;.      if( pg=
13bc4 3d 70 50 67 2d 3e 70 67 6e 6f 20 7c 7c 20 21 73  =pPg->pgno || !s
13bc5 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74  qlite3BitvecTest
13bc6 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72  (pPager->pInJour
13bc7 6e 61 6c 2c 20 70 67 29 20 29 7b 0a 20 20 20 20  nal, pg) ){.    
13bc8 20 20 20 20 69 66 28 20 70 67 21 3d 50 41 47 45      if( pg!=PAGE
13bc9 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72  R_MJ_PGNO(pPager
13bca 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  ) ){.          r
13bcb 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
13bcc 47 65 74 28 70 50 61 67 65 72 2c 20 70 67 2c 20  Get(pPager, pg, 
13bcd 26 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  &pPage);.       
13bce 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
13bcf 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
13bd0 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77      rc = pager_w
13bd1 72 69 74 65 28 70 50 61 67 65 29 3b 0a 20 20 20  rite(pPage);.   
13bd2 20 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61           if( pPa
13bd3 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  ge->flags&PGHDR_
13bd4 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20  NEED_SYNC ){.   
13bd5 20 20 20 20 20 20 20 20 20 20 20 6e 65 65 64 53             needS
13bd6 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ync = 1;.       
13bd7 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70 50         assert(pP
13bd8 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 29 3b  ager->needSync);
13bd9 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
13bda 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
13bdb 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61  e3PagerUnref(pPa
13bdc 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  ge);.          }
13bdd 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
13bde 20 7d 65 6c 73 65 20 69 66 28 20 28 70 50 61 67   }else if( (pPag
13bdf 65 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70  e = pager_lookup
13be0 28 70 50 61 67 65 72 2c 20 70 67 29 29 21 3d 30  (pPager, pg))!=0
13be1 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
13be2 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48  pPage->flags&PGH
13be3 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a  DR_NEED_SYNC ){.
13be4 20 20 20 20 20 20 20 20 20 20 6e 65 65 64 53 79            needSy
13be5 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  nc = 1;.        
13be6 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
13be7 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 67  3PagerUnref(pPag
13be8 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
13be9 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  }..    /* If the
13bea 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43   PGHDR_NEED_SYNC
13beb 20 66 6c 61 67 20 69 73 20 73 65 74 20 66 6f 72   flag is set for
13bec 20 61 6e 79 20 6f 66 20 74 68 65 20 6e 50 61 67   any of the nPag
13bed 65 20 70 61 67 65 73 20 0a 20 20 20 20 2a 2a 20  e pages .    ** 
13bee 73 74 61 72 74 69 6e 67 20 61 74 20 70 67 31 2c  starting at pg1,
13bef 20 74 68 65 6e 20 69 74 20 6e 65 65 64 73 20 74   then it needs t
13bf0 6f 20 62 65 20 73 65 74 20 66 6f 72 20 61 6c 6c  o be set for all
13bf1 20 6f 66 20 74 68 65 6d 2e 20 42 65 63 61 75 73   of them. Becaus
13bf2 65 0a 20 20 20 20 2a 2a 20 77 72 69 74 69 6e 67  e.    ** writing
13bf3 20 74 6f 20 61 6e 79 20 6f 66 20 74 68 65 73 65   to any of these
13bf4 20 6e 50 61 67 65 20 70 61 67 65 73 20 6d 61 79   nPage pages may
13bf5 20 64 61 6d 61 67 65 20 74 68 65 20 6f 74 68 65   damage the othe
13bf6 72 73 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 6a  rs, the.    ** j
13bf7 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74  ournal file must
13bf8 20 63 6f 6e 74 61 69 6e 20 73 79 6e 63 28 29 65   contain sync()e
13bf9 64 20 63 6f 70 69 65 73 20 6f 66 20 61 6c 6c 20  d copies of all 
13bfa 6f 66 20 74 68 65 6d 0a 20 20 20 20 2a 2a 20 62  of them.    ** b
13bfb 65 66 6f 72 65 20 61 6e 79 20 6f 66 20 74 68 65  efore any of the
13bfc 6d 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e  m can be written
13bfd 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61   out to the data
13bfe 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a  base file..    *
13bff 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  /.    if( rc==SQ
13c00 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 65 65 64 53  LITE_OK && needS
13c01 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ync ){.      ass
13c02 65 72 74 28 20 21 4d 45 4d 44 42 20 26 26 20 70  ert( !MEMDB && p
13c03 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30  Pager->noSync==0
13c04 20 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 69   );.      for(ii
13c05 3d 30 3b 20 69 69 3c 6e 50 61 67 65 3b 20 69 69  =0; ii<nPage; ii
13c06 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 50 67 48  ++){.        PgH
13c07 64 72 20 2a 70 50 61 67 65 20 3d 20 70 61 67 65  dr *pPage = page
13c08 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c  r_lookup(pPager,
13c09 20 70 67 31 2b 69 69 29 3b 0a 20 20 20 20 20 20   pg1+ii);.      
13c0a 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20    if( pPage ){. 
13c0b 20 20 20 20 20 20 20 20 20 70 50 61 67 65 2d 3e           pPage->
13c0c 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e  flags |= PGHDR_N
13c0d 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20  EED_SYNC;.      
13c0e 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
13c0f 55 6e 72 65 66 28 70 50 61 67 65 29 3b 0a 20 20  Unref(pPage);.  
13c10 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
13c11 20 20 20 20 20 20 61 73 73 65 72 74 28 70 50 61        assert(pPa
13c12 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 29 3b 0a  ger->needSync);.
13c13 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 72      }..    asser
13c14 74 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74  t( pPager->doNot
13c15 53 79 6e 63 3d 3d 31 20 29 3b 0a 20 20 20 20 70  Sync==1 );.    p
13c16 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63  Pager->doNotSync
13c17 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
13c18 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72     rc = pager_wr
13c19 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a 20 20  ite(pDbPage);.  
13c1a 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
13c1b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
13c1c 52 55 45 20 69 66 20 74 68 65 20 70 61 67 65 20  RUE if the page 
13c1d 67 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67  given in the arg
13c1e 75 6d 65 6e 74 20 77 61 73 20 70 72 65 76 69 6f  ument was previo
13c1f 75 73 6c 79 20 70 61 73 73 65 64 0a 2a 2a 20 74  usly passed.** t
13c20 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  o sqlite3PagerWr
13c21 69 74 65 28 29 2e 20 20 49 6e 20 6f 74 68 65 72  ite().  In other
13c22 20 77 6f 72 64 73 2c 20 72 65 74 75 72 6e 20 54   words, return T
13c23 52 55 45 20 69 66 20 69 74 20 69 73 20 6f 6b 0a  RUE if it is ok.
13c24 2a 2a 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65  ** to change the
13c25 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20   content of the 
13c26 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  page..*/.#ifndef
13c27 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50   NDEBUG.SQLITE_P
13c28 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
13c29 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
13c2a 6c 65 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b  le(DbPage *pPg){
13c2b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 66  .  return pPg->f
13c2c 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59  lags&PGHDR_DIRTY
13c2d 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
13c2e 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74 68 69 73  * A call to this
13c2f 20 72 6f 75 74 69 6e 65 20 74 65 6c 6c 73 20 74   routine tells t
13c30 68 65 20 70 61 67 65 72 20 74 68 61 74 20 69 74  he pager that it
13c31 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72   is not necessar
13c32 79 20 74 6f 0a 2a 2a 20 77 72 69 74 65 20 74 68  y to.** write th
13c33 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e  e information on
13c34 20 70 61 67 65 20 70 50 67 20 62 61 63 6b 20 74   page pPg back t
13c35 6f 20 74 68 65 20 64 69 73 6b 2c 20 65 76 65 6e  o the disk, even
13c36 20 74 68 6f 75 67 68 0a 2a 2a 20 74 68 61 74 20   though.** that 
13c37 70 61 67 65 20 6d 69 67 68 74 20 62 65 20 6d 61  page might be ma
13c38 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 20  rked as dirty.  
13c39 54 68 69 73 20 68 61 70 70 65 6e 73 2c 20 66 6f  This happens, fo
13c3a 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 0a  r example, when.
13c3b 2a 2a 20 74 68 65 20 70 61 67 65 20 68 61 73 20  ** the page has 
13c3c 62 65 65 6e 20 61 64 64 65 64 20 61 73 20 61 20  been added as a 
13c3d 6c 65 61 66 20 6f 66 20 74 68 65 20 66 72 65 65  leaf of the free
13c3e 6c 69 73 74 20 61 6e 64 20 73 6f 20 69 74 73 0a  list and so its.
13c3f 2a 2a 20 63 6f 6e 74 65 6e 74 20 6e 6f 20 6c 6f  ** content no lo
13c40 6e 67 65 72 20 6d 61 74 74 65 72 73 2e 0a 2a 2a  nger matters..**
13c41 0a 2a 2a 20 54 68 65 20 6f 76 65 72 6c 79 69 6e  .** The overlyin
13c42 67 20 73 6f 66 74 77 61 72 65 20 6c 61 79 65 72  g software layer
13c43 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74   calls this rout
13c44 69 6e 65 20 77 68 65 6e 20 61 6c 6c 20 6f 66 20  ine when all of 
13c45 74 68 65 20 64 61 74 61 0a 2a 2a 20 6f 6e 20 74  the data.** on t
13c46 68 65 20 67 69 76 65 6e 20 70 61 67 65 20 69 73  he given page is
13c47 20 75 6e 75 73 65 64 2e 20 54 68 65 20 70 61 67   unused. The pag
13c48 65 72 20 6d 61 72 6b 73 20 74 68 65 20 70 61 67  er marks the pag
13c49 65 20 61 73 20 63 6c 65 61 6e 20 73 6f 0a 2a 2a  e as clean so.**
13c4a 20 74 68 61 74 20 69 74 20 64 6f 65 73 20 6e 6f   that it does no
13c4b 74 20 67 65 74 20 77 72 69 74 74 65 6e 20 74 6f  t get written to
13c4c 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 54 65 73   disk..**.** Tes
13c4d 74 73 20 73 68 6f 77 20 74 68 61 74 20 74 68 69  ts show that thi
13c4e 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 63  s optimization c
13c4f 61 6e 20 71 75 61 64 72 75 70 6c 65 20 74 68 65  an quadruple the
13c50 20 73 70 65 65 64 20 6f 66 20 6c 61 72 67 65 20   speed of large 
13c51 0a 2a 2a 20 44 45 4c 45 54 45 20 6f 70 65 72 61  .** DELETE opera
13c52 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  tions..*/.SQLITE
13c53 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
13c54 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72  lite3PagerDontWr
13c55 69 74 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b  ite(PgHdr *pPg){
13c56 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
13c57 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a   = pPg->pPager;.
13c58 20 20 69 66 28 20 28 70 50 67 2d 3e 66 6c 61 67    if( (pPg->flag
13c59 73 26 50 47 48 44 52 5f 44 49 52 54 59 29 20 26  s&PGHDR_DIRTY) &
13c5a 26 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70  & pPager->nSavep
13c5b 6f 69 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 50  oint==0 ){.    P
13c5c 41 47 45 52 54 52 41 43 45 28 28 22 44 4f 4e 54  AGERTRACE(("DONT
13c5d 5f 57 52 49 54 45 20 70 61 67 65 20 25 64 20 6f  _WRITE page %d o
13c5e 66 20 25 64 5c 6e 22 2c 20 70 50 67 2d 3e 70 67  f %d\n", pPg->pg
13c5f 6e 6f 2c 20 50 41 47 45 52 49 44 28 70 50 61 67  no, PAGERID(pPag
13c60 65 72 29 29 29 3b 0a 20 20 20 20 49 4f 54 52 41  er)));.    IOTRA
13c61 43 45 28 28 22 43 4c 45 41 4e 20 25 70 20 25 64  CE(("CLEAN %p %d
13c62 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67  \n", pPager, pPg
13c63 2d 3e 70 67 6e 6f 29 29 0a 20 20 20 20 70 50 67  ->pgno)).    pPg
13c64 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52  ->flags |= PGHDR
13c65 5f 44 4f 4e 54 5f 57 52 49 54 45 3b 0a 23 69 66  _DONT_WRITE;.#if
13c66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b  def SQLITE_CHECK
13c67 5f 50 41 47 45 53 0a 20 20 20 20 70 50 67 2d 3e  _PAGES.    pPg->
13c68 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65 72  pageHash = pager
13c69 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 3b 0a  _pagehash(pPg);.
13c6a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 2f 2a  #endif.  }.}../*
13c6b 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
13c6c 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 69 6e   is called to in
13c6d 63 72 65 6d 65 6e 74 20 74 68 65 20 76 61 6c 75  crement the valu
13c6e 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
13c6f 65 20 66 69 6c 65 20 0a 2a 2a 20 63 68 61 6e 67  e file .** chang
13c70 65 2d 63 6f 75 6e 74 65 72 2c 20 73 74 6f 72 65  e-counter, store
13c71 64 20 61 73 20 61 20 34 2d 62 79 74 65 20 62 69  d as a 4-byte bi
13c72 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72  g-endian integer
13c73 20 73 74 61 72 74 69 6e 67 20 61 74 20 0a 2a 2a   starting at .**
13c74 20 62 79 74 65 20 6f 66 66 73 65 74 20 32 34 20   byte offset 24 
13c75 6f 66 20 74 68 65 20 70 61 67 65 72 20 66 69 6c  of the pager fil
13c76 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
13c77 69 73 44 69 72 65 63 74 4d 6f 64 65 20 66 6c 61  isDirectMode fla
13c78 67 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20  g is zero, then 
13c79 74 68 69 73 20 69 73 20 64 6f 6e 65 20 62 79 20  this is done by 
13c7a 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c 69  calling .** sqli
13c7b 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20  te3PagerWrite() 
13c7c 6f 6e 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20  on page 1, then 
13c7d 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20 63 6f  modifying the co
13c7e 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a  ntents of the.**
13c7f 20 70 61 67 65 20 64 61 74 61 2e 20 49 6e 20 74   page data. In t
13c80 68 69 73 20 63 61 73 65 20 74 68 65 20 66 69 6c  his case the fil
13c81 65 20 77 69 6c 6c 20 62 65 20 75 70 64 61 74 65  e will be update
13c82 64 20 77 68 65 6e 20 74 68 65 20 63 75 72 72 65  d when the curre
13c83 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  nt.** transactio
13c84 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a  n is committed..
13c85 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 44 69 72 65  **.** The isDire
13c86 63 74 4d 6f 64 65 20 66 6c 61 67 20 6d 61 79 20  ctMode flag may 
13c87 6f 6e 6c 79 20 62 65 20 6e 6f 6e 2d 7a 65 72 6f  only be non-zero
13c88 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 79 20   if the library 
13c89 77 61 73 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20  was compiled.** 
13c8a 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f  with the SQLITE_
13c8b 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52  ENABLE_ATOMIC_WR
13c8c 49 54 45 20 6d 61 63 72 6f 20 64 65 66 69 6e 65  ITE macro define
13c8d 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c  d. In this case,
13c8e 0a 2a 2a 20 69 66 20 69 73 44 69 72 65 63 74 20  .** if isDirect 
13c8f 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
13c90 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
13c91 69 6c 65 20 69 73 20 75 70 64 61 74 65 64 20 64  ile is updated d
13c92 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 77 72  irectly.** by wr
13c93 69 74 69 6e 67 20 61 6e 20 75 70 64 61 74 65 64  iting an updated
13c94 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 61 67 65   version of page
13c95 20 31 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 20   1 using a call 
13c96 74 6f 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74  to the .** sqlit
13c97 65 33 4f 73 57 72 69 74 65 28 29 20 66 75 6e 63  e3OsWrite() func
13c98 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
13c99 69 6e 74 20 70 61 67 65 72 5f 69 6e 63 72 5f 63  int pager_incr_c
13c9a 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 50 61 67  hangecounter(Pag
13c9b 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
13c9c 69 73 44 69 72 65 63 74 4d 6f 64 65 29 7b 0a 20  isDirectMode){. 
13c9d 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
13c9e 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 44 65 63 6c 61  _OK;..  /* Decla
13c9f 72 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a  re and initializ
13ca0 65 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67  e constant integ
13ca1 65 72 20 27 69 73 44 69 72 65 63 74 27 2e 20 49  er 'isDirect'. I
13ca2 66 20 74 68 65 0a 20 20 2a 2a 20 61 74 6f 6d 69  f the.  ** atomi
13ca3 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61  c-write optimiza
13ca4 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 20  tion is enabled 
13ca5 69 6e 20 74 68 69 73 20 62 75 69 6c 64 2c 20 74  in this build, t
13ca6 68 65 6e 20 69 73 44 69 72 65 63 74 0a 20 20 2a  hen isDirect.  *
13ca7 2a 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64  * is initialized
13ca8 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 70 61   to the value pa
13ca9 73 73 65 64 20 61 73 20 74 68 65 20 69 73 44 69  ssed as the isDi
13caa 72 65 63 74 4d 6f 64 65 20 70 61 72 61 6d 65 74  rectMode paramet
13cab 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68 69 73 20  er.  ** to this 
13cac 66 75 6e 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77  function. Otherw
13cad 69 73 65 2c 20 69 74 20 69 73 20 61 6c 77 61 79  ise, it is alway
13cae 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 20  s set to zero.. 
13caf 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 69 64 65   **.  ** The ide
13cb0 61 20 69 73 20 74 68 61 74 20 69 66 20 74 68 65  a is that if the
13cb1 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70   atomic-write op
13cb2 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 6e 6f  timization is no
13cb3 74 0a 20 20 2a 2a 20 65 6e 61 62 6c 65 64 20 61  t.  ** enabled a
13cb4 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20  t compile time, 
13cb5 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 61 6e  the compiler can
13cb6 20 6f 6d 69 74 20 74 68 65 20 74 65 73 74 73 20   omit the tests 
13cb7 6f 66 0a 20 20 2a 2a 20 27 69 73 44 69 72 65 63  of.  ** 'isDirec
13cb8 74 27 20 62 65 6c 6f 77 2c 20 61 73 20 77 65 6c  t' below, as wel
13cb9 6c 20 61 73 20 74 68 65 20 62 6c 6f 63 6b 20 65  l as the block e
13cba 6e 63 6c 6f 73 65 64 20 69 6e 20 74 68 65 0a 20  nclosed in the. 
13cbb 20 2a 2a 20 22 69 66 28 20 69 73 44 69 72 65 63   ** "if( isDirec
13cbc 74 20 29 22 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a  t )" condition..
13cbd 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
13cbe 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49  ITE_ENABLE_ATOMI
13cbf 43 5f 57 52 49 54 45 0a 23 20 64 65 66 69 6e 65  C_WRITE.# define
13cc0 20 44 49 52 45 43 54 5f 4d 4f 44 45 20 30 0a 20   DIRECT_MODE 0. 
13cc1 20 61 73 73 65 72 74 28 20 69 73 44 69 72 65 63   assert( isDirec
13cc2 74 4d 6f 64 65 3d 3d 30 20 29 3b 0a 20 20 55 4e  tMode==0 );.  UN
13cc3 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69  USED_PARAMETER(i
13cc4 73 44 69 72 65 63 74 4d 6f 64 65 29 3b 0a 23 65  sDirectMode);.#e
13cc5 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 44 49 52  lse.# define DIR
13cc6 45 43 54 5f 4d 4f 44 45 20 69 73 44 69 72 65 63  ECT_MODE isDirec
13cc7 74 4d 6f 64 65 0a 23 65 6e 64 69 66 0a 0a 20 20  tMode.#endif..  
13cc8 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
13cc9 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53  state>=PAGER_RES
13cca 45 52 56 45 44 20 29 3b 0a 20 20 69 66 28 20 21  ERVED );.  if( !
13ccb 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f  pPager->changeCo
13ccc 75 6e 74 44 6f 6e 65 20 26 26 20 41 4c 57 41 59  untDone && ALWAY
13ccd 53 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  S(pPager->dbSize
13cce 3e 30 29 20 29 7b 0a 20 20 20 20 50 67 48 64 72  >0) ){.    PgHdr
13ccf 20 2a 70 50 67 48 64 72 3b 20 20 20 20 20 20 20   *pPgHdr;       
13cd0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 66 65           /* Refe
13cd1 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20  rence to page 1 
13cd2 2a 2f 0a 20 20 20 20 75 33 32 20 63 68 61 6e 67  */.    u32 chang
13cd3 65 5f 63 6f 75 6e 74 65 72 3b 20 20 20 20 20 20  e_counter;      
13cd4 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20       /* Initial 
13cd5 76 61 6c 75 65 20 6f 66 20 63 68 61 6e 67 65 2d  value of change-
13cd6 63 6f 75 6e 74 65 72 20 66 69 65 6c 64 20 2a 2f  counter field */
13cd7 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70  ..    assert( !p
13cd8 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20  Pager->tempFile 
13cd9 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  && isOpen(pPager
13cda 2d 3e 66 64 29 20 29 3b 0a 0a 20 20 20 20 2f 2a  ->fd) );..    /*
13cdb 20 4f 70 65 6e 20 70 61 67 65 20 31 20 6f 66 20   Open page 1 of 
13cdc 74 68 65 20 66 69 6c 65 20 66 6f 72 20 77 72 69  the file for wri
13cdd 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 63 20  ting. */.    rc 
13cde 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
13cdf 74 28 70 50 61 67 65 72 2c 20 31 2c 20 26 70 50  t(pPager, 1, &pP
13ce0 67 48 64 72 29 3b 0a 20 20 20 20 61 73 73 65 72  gHdr);.    asser
13ce1 74 28 20 70 50 67 48 64 72 3d 3d 30 20 7c 7c 20  t( pPgHdr==0 || 
13ce2 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
13ce3 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 61 67 65  ..    /* If page
13ce4 20 6f 6e 65 20 77 61 73 20 66 65 74 63 68 65 64   one was fetched
13ce5 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 61   successfully, a
13ce6 6e 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  nd this function
13ce7 20 69 73 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 6f   is not.    ** o
13ce8 70 65 72 61 74 69 6e 67 20 69 6e 20 64 69 72 65  perating in dire
13ce9 63 74 2d 6d 6f 64 65 2c 20 6d 61 6b 65 20 70 61  ct-mode, make pa
13cea 67 65 20 31 20 77 72 69 74 61 62 6c 65 2e 20 20  ge 1 writable.  
13ceb 57 68 65 6e 20 6e 6f 74 20 69 6e 20 0a 20 20 20  When not in .   
13cec 20 2a 2a 20 64 69 72 65 63 74 20 6d 6f 64 65 2c   ** direct mode,
13ced 20 70 61 67 65 20 31 20 69 73 20 61 6c 77 61 79   page 1 is alway
13cee 73 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 20  s held in cache 
13cef 61 6e 64 20 68 65 6e 63 65 20 74 68 65 20 50 61  and hence the Pa
13cf0 67 65 72 47 65 74 28 29 0a 20 20 20 20 2a 2a 20  gerGet().    ** 
13cf1 61 62 6f 76 65 20 69 73 20 61 6c 77 61 79 73 20  above is always 
13cf2 73 75 63 63 65 73 73 66 75 6c 20 2d 20 68 65 6e  successful - hen
13cf3 63 65 20 74 68 65 20 41 4c 57 41 59 53 20 6f 6e  ce the ALWAYS on
13cf4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 2e 0a   rc==SQLITE_OK..
13cf5 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21      */.    if( !
13cf6 44 49 52 45 43 54 5f 4d 4f 44 45 20 26 26 20 41  DIRECT_MODE && A
13cf7 4c 57 41 59 53 28 72 63 3d 3d 53 51 4c 49 54 45  LWAYS(rc==SQLITE
13cf8 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 20 20 72 63  _OK) ){.      rc
13cf9 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
13cfa 72 69 74 65 28 70 50 67 48 64 72 29 3b 0a 20 20  rite(pPgHdr);.  
13cfb 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 3d    }..    if( rc=
13cfc 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
13cfd 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74      /* Increment
13cfe 20 74 68 65 20 76 61 6c 75 65 20 6a 75 73 74 20   the value just 
13cff 72 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 69  read and write i
13d00 74 20 62 61 63 6b 20 74 6f 20 62 79 74 65 20 32  t back to byte 2
13d01 34 2e 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 6e  4. */.      chan
13d02 67 65 5f 63 6f 75 6e 74 65 72 20 3d 20 73 71 6c  ge_counter = sql
13d03 69 74 65 33 47 65 74 34 62 79 74 65 28 28 75 38  ite3Get4byte((u8
13d04 2a 29 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  *)pPager->dbFile
13d05 56 65 72 73 29 3b 0a 20 20 20 20 20 20 63 68 61  Vers);.      cha
13d06 6e 67 65 5f 63 6f 75 6e 74 65 72 2b 2b 3b 0a 20  nge_counter++;. 
13d07 20 20 20 20 20 70 75 74 33 32 62 69 74 73 28 28       put32bits((
13d08 28 63 68 61 72 2a 29 70 50 67 48 64 72 2d 3e 70  (char*)pPgHdr->p
13d09 44 61 74 61 29 2b 32 34 2c 20 63 68 61 6e 67 65  Data)+24, change
13d0a 5f 63 6f 75 6e 74 65 72 29 3b 0a 0a 20 20 20 20  _counter);..    
13d0b 20 20 2f 2a 20 49 66 20 72 75 6e 6e 69 6e 67 20    /* If running 
13d0c 69 6e 20 64 69 72 65 63 74 20 6d 6f 64 65 2c 20  in direct mode, 
13d0d 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e  write the conten
13d0e 74 73 20 6f 66 20 70 61 67 65 20 31 20 74 6f 20  ts of page 1 to 
13d0f 74 68 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20  the file. */.   
13d10 20 20 20 69 66 28 20 44 49 52 45 43 54 5f 4d 4f     if( DIRECT_MO
13d11 44 45 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f  DE ){.        co
13d12 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 20 3d  nst void *zBuf =
13d13 20 70 50 67 48 64 72 2d 3e 70 44 61 74 61 3b 0a   pPgHdr->pData;.
13d14 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
13d15 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69  pPager->dbFileSi
13d16 7a 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20  ze>0 );.        
13d17 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72  rc = sqlite3OsWr
13d18 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ite(pPager->fd, 
13d19 7a 42 75 66 2c 20 70 50 61 67 65 72 2d 3e 70 61  zBuf, pPager->pa
13d1a 67 65 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20  geSize, 0);.    
13d1b 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
13d1c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
13d1d 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67     pPager->chang
13d1e 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a  eCountDone = 1;.
13d1f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
13d20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
13d21 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75  Pager->changeCou
13d22 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 20  ntDone = 1;.    
13d23 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
13d24 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 70 61  * Release the pa
13d25 67 65 20 72 65 66 65 72 65 6e 63 65 2e 20 2a 2f  ge reference. */
13d26 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65  .    sqlite3Page
13d27 72 55 6e 72 65 66 28 70 50 67 48 64 72 29 3b 0a  rUnref(pPgHdr);.
13d28 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
13d29 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74  .}../*.** Sync t
13d2a 68 65 20 70 61 67 65 72 20 66 69 6c 65 20 74 6f  he pager file to
13d2b 20 64 69 73 6b 2e 20 54 68 69 73 20 69 73 20 61   disk. This is a
13d2c 20 6e 6f 2d 6f 70 20 66 6f 72 20 69 6e 2d 6d 65   no-op for in-me
13d2d 6d 6f 72 79 20 66 69 6c 65 73 0a 2a 2a 20 6f 72  mory files.** or
13d2e 20 70 61 67 65 73 20 77 69 74 68 20 74 68 65 20   pages with the 
13d2f 50 61 67 65 72 2e 6e 6f 53 79 6e 63 20 66 6c 61  Pager.noSync fla
13d30 67 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  g set..**.** If 
13d31 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 20 63  successful, or c
13d32 61 6c 6c 65 64 20 6f 6e 20 61 20 70 61 67 65 72  alled on a pager
13d33 20 66 6f 72 20 77 68 69 63 68 20 69 74 20 69 73   for which it is
13d34 20 61 20 6e 6f 2d 6f 70 2c 20 74 68 69 73 0a 2a   a no-op, this.*
13d35 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  * function retur
13d36 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74  ns SQLITE_OK. Ot
13d37 68 65 72 77 69 73 65 2c 20 61 6e 20 49 4f 20 65  herwise, an IO e
13d38 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
13d39 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  urned..*/.SQLITE
13d3a 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
13d3b 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 50 61  ite3PagerSync(Pa
13d3c 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
13d3d 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
13d3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13d3f 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
13d40 6f 64 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ode */.  assert(
13d41 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 69 66 28   !MEMDB );.  if(
13d42 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20   pPager->noSync 
13d43 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
13d44 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a  TE_OK;.  }else{.
13d45 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
13d46 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 66  OsSync(pPager->f
13d47 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f  d, pPager->sync_
13d48 66 6c 61 67 73 29 3b 0a 20 20 7d 0a 20 20 72 65  flags);.  }.  re
13d49 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
13d4a 2a 20 53 79 6e 63 20 74 68 65 20 64 61 74 61 62  * Sync the datab
13d4b 61 73 65 20 66 69 6c 65 20 66 6f 72 20 74 68 65  ase file for the
13d4c 20 70 61 67 65 72 20 70 50 61 67 65 72 2e 20 7a   pager pPager. z
13d4d 4d 61 73 74 65 72 20 70 6f 69 6e 74 73 20 74 6f  Master points to
13d4e 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20   the name.** of 
13d4f 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  a master journal
13d50 20 66 69 6c 65 20 74 68 61 74 20 73 68 6f 75 6c   file that shoul
13d51 64 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74  d be written int
13d52 6f 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c  o the individual
13d53 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  .** journal file
13d54 2e 20 7a 4d 61 73 74 65 72 20 6d 61 79 20 62 65  . zMaster may be
13d55 20 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69 73 20   NULL, which is 
13d56 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 6e  interpreted as n
13d57 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72  o master.** jour
13d58 6e 61 6c 20 28 61 20 73 69 6e 67 6c 65 20 64 61  nal (a single da
13d59 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 69  tabase transacti
13d5a 6f 6e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  on)..**.** This 
13d5b 72 6f 75 74 69 6e 65 20 65 6e 73 75 72 65 73 20  routine ensures 
13d5c 74 68 61 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  that:.**.**   * 
13d5d 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  The database fil
13d5e 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72  e change-counter
13d5f 20 69 73 20 75 70 64 61 74 65 64 2c 0a 2a 2a 20   is updated,.** 
13d60 20 20 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20    * the journal 
13d61 69 73 20 73 79 6e 63 65 64 20 28 75 6e 6c 65 73  is synced (unles
13d62 73 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69  s the atomic-wri
13d63 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  te optimization 
13d64 69 73 20 75 73 65 64 29 2c 0a 2a 2a 20 20 20 2a  is used),.**   *
13d65 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73   all dirty pages
13d66 20 61 72 65 20 77 72 69 74 74 65 6e 20 74 6f 20   are written to 
13d67 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
13d68 65 2c 20 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64  e, .**   * the d
13d69 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20  atabase file is 
13d6a 74 72 75 6e 63 61 74 65 64 20 28 69 66 20 72 65  truncated (if re
13d6b 71 75 69 72 65 64 29 2c 20 61 6e 64 0a 2a 2a 20  quired), and.** 
13d6c 20 20 2a 20 74 68 65 20 64 61 74 61 62 61 73 65    * the database
13d6d 20 66 69 6c 65 20 73 79 6e 63 65 64 2e 20 0a 2a   file synced. .*
13d6e 2a 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 74 68  *.** The only th
13d6f 69 6e 67 20 74 68 61 74 20 72 65 6d 61 69 6e 73  ing that remains
13d70 20 74 6f 20 63 6f 6d 6d 69 74 20 74 68 65 20 74   to commit the t
13d71 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 74 6f  ransaction is to
13d72 20 66 69 6e 61 6c 69 7a 65 20 0a 2a 2a 20 28 64   finalize .** (d
13d73 65 6c 65 74 65 2c 20 74 72 75 6e 63 61 74 65 20  elete, truncate 
13d74 6f 72 20 7a 65 72 6f 20 74 68 65 20 66 69 72 73  or zero the firs
13d75 74 20 70 61 72 74 20 6f 66 29 20 74 68 65 20 6a  t part of) the j
13d76 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 20  ournal file (or 
13d77 0a 2a 2a 20 64 65 6c 65 74 65 20 74 68 65 20 6d  .** delete the m
13d78 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
13d79 6c 65 20 69 66 20 73 70 65 63 69 66 69 65 64 29  le if specified)
13d7a 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
13d7b 74 20 69 66 20 7a 4d 61 73 74 65 72 3d 3d 4e 55  t if zMaster==NU
13d7c 4c 4c 2c 20 74 68 69 73 20 64 6f 65 73 20 6e 6f  LL, this does no
13d7d 74 20 6f 76 65 72 77 72 69 74 65 20 61 20 70 72  t overwrite a pr
13d7e 65 76 69 6f 75 73 20 76 61 6c 75 65 0a 2a 2a 20  evious value.** 
13d7f 70 61 73 73 65 64 20 74 6f 20 61 6e 20 73 71 6c  passed to an sql
13d80 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50  ite3PagerCommitP
13d81 68 61 73 65 4f 6e 65 28 29 20 63 61 6c 6c 2e 0a  haseOne() call..
13d82 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6e  **.** If the fin
13d83 61 6c 20 70 61 72 61 6d 65 74 65 72 20 2d 20 6e  al parameter - n
13d84 6f 53 79 6e 63 20 2d 20 69 73 20 74 72 75 65 2c  oSync - is true,
13d85 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 62 61   then the databa
13d86 73 65 20 66 69 6c 65 20 69 74 73 65 6c 66 0a 2a  se file itself.*
13d87 2a 20 69 73 20 6e 6f 74 20 73 79 6e 63 65 64 2e  * is not synced.
13d88 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74   The caller must
13d89 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67   call sqlite3Pag
13d8a 65 72 53 79 6e 63 28 29 20 64 69 72 65 63 74 6c  erSync() directl
13d8b 79 20 74 6f 0a 2a 2a 20 73 79 6e 63 20 74 68 65  y to.** sync the
13d8c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 62   database file b
13d8d 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 43 6f  efore calling Co
13d8e 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 74  mmitPhaseTwo() t
13d8f 6f 20 64 65 6c 65 74 65 20 74 68 65 0a 2a 2a 20  o delete the.** 
13d90 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20  journal file in 
13d91 74 68 69 73 20 63 61 73 65 2e 0a 2a 2f 0a 53 51  this case..*/.SQ
13d92 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
13d93 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d   sqlite3PagerCom
13d94 6d 69 74 50 68 61 73 65 4f 6e 65 28 0a 20 20 50  mitPhaseOne(.  P
13d95 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20  ager *pPager,   
13d96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
13d97 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a  * Pager object *
13d98 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
13d99 7a 4d 61 73 74 65 72 2c 20 20 20 20 20 20 20 20  zMaster,        
13d9a 20 20 20 20 2f 2a 20 49 66 20 6e 6f 74 20 4e 55      /* If not NU
13d9b 4c 4c 2c 20 74 68 65 20 6d 61 73 74 65 72 20 6a  LL, the master j
13d9c 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20  ournal name */. 
13d9d 20 69 6e 74 20 6e 6f 53 79 6e 63 20 20 20 20 20   int noSync     
13d9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13d9f 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 74   /* True to omit
13da0 20 74 68 65 20 78 53 79 6e 63 20 6f 6e 20 74 68   the xSync on th
13da1 65 20 64 62 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a  e db file */.){.
13da2 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
13da3 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20  E_OK;           
13da4 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
13da5 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 64 62   */..  /* The db
13da6 4f 72 69 67 53 69 7a 65 20 69 73 20 6e 65 76 65  OrigSize is neve
13da7 72 20 73 65 74 20 69 66 20 6a 6f 75 72 6e 61 6c  r set if journal
13da8 5f 6d 6f 64 65 3d 4f 46 46 20 2a 2f 0a 20 20 61  _mode=OFF */.  a
13da9 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a  ssert( pPager->j
13daa 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45  ournalMode!=PAGE
13dab 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46  R_JOURNALMODE_OF
13dac 46 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62 4f  F || pPager->dbO
13dad 72 69 67 53 69 7a 65 3d 3d 30 20 29 3b 0a 0a 20  rigSize==0 );.. 
13dae 20 2f 2a 20 49 66 20 61 20 70 72 69 6f 72 20 65   /* If a prior e
13daf 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 74  rror occurred, t
13db0 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75  his routine shou
13db1 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64  ld not be called
13db2 2e 20 20 52 4f 4c 4c 42 41 43 4b 0a 20 20 2a 2a  .  ROLLBACK.  **
13db3 20 69 73 20 74 68 65 20 61 70 70 72 6f 70 72 69   is the appropri
13db4 61 74 65 20 72 65 73 70 6f 6e 73 65 20 74 6f 20  ate response to 
13db5 61 6e 20 65 72 72 6f 72 2c 20 6e 6f 74 20 43 4f  an error, not CO
13db6 4d 4d 49 54 2e 20 20 47 75 61 72 64 20 61 67 61  MMIT.  Guard aga
13db7 69 6e 73 74 0a 20 20 2a 2a 20 63 6f 64 69 6e 67  inst.  ** coding
13db8 20 65 72 72 6f 72 73 20 62 79 20 72 65 70 65 61   errors by repea
13db9 74 69 6e 67 20 74 68 65 20 70 72 69 6f 72 20 65  ting the prior e
13dba 72 72 6f 72 2e 20 2a 2f 0a 20 20 69 66 28 20 4e  rror. */.  if( N
13dbb 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65 72 72  EVER(pPager->err
13dbc 43 6f 64 65 29 20 29 20 72 65 74 75 72 6e 20 70  Code) ) return p
13dbd 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a  Pager->errCode;.
13dbe 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22  .  PAGERTRACE(("
13dbf 44 41 54 41 42 41 53 45 20 53 59 4e 43 3a 20 46  DATABASE SYNC: F
13dc0 69 6c 65 3d 25 73 20 7a 4d 61 73 74 65 72 3d 25  ile=%s zMaster=%
13dc1 73 20 6e 53 69 7a 65 3d 25 64 5c 6e 22 2c 20 0a  s nSize=%d\n", .
13dc2 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 7a 46        pPager->zF
13dc3 69 6c 65 6e 61 6d 65 2c 20 7a 4d 61 73 74 65 72  ilename, zMaster
13dc4 2c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  , pPager->dbSize
13dc5 29 29 3b 0a 0a 20 20 69 66 28 20 4d 45 4d 44 42  ));..  if( MEMDB
13dc6 20 26 26 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f   && pPager->dbMo
13dc7 64 69 66 69 65 64 20 29 7b 0a 20 20 20 20 2f 2a  dified ){.    /*
13dc8 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 69   If this is an i
13dc9 6e 2d 6d 65 6d 6f 72 79 20 64 62 2c 20 6f 72 20  n-memory db, or 
13dca 6e 6f 20 70 61 67 65 73 20 68 61 76 65 20 62 65  no pages have be
13dcb 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 6f  en written to, o
13dcc 72 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 66 75  r this.    ** fu
13dcd 6e 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 61  nction has alrea
13dce 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 20  dy been called, 
13dcf 69 74 20 69 73 20 6d 6f 73 74 6c 79 20 61 20 6e  it is mostly a n
13dd0 6f 2d 6f 70 2e 20 20 48 6f 77 65 76 65 72 2c 20  o-op.  However, 
13dd1 61 6e 79 0a 20 20 20 20 2a 2a 20 62 61 63 6b 75  any.    ** backu
13dd2 70 20 69 6e 20 70 72 6f 67 72 65 73 73 20 6e 65  p in progress ne
13dd3 65 64 73 20 74 6f 20 62 65 20 72 65 73 74 61 72  eds to be restar
13dd4 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ted..    */.    
13dd5 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73  sqlite3BackupRes
13dd6 74 61 72 74 28 70 50 61 67 65 72 2d 3e 70 42 61  tart(pPager->pBa
13dd7 63 6b 75 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69  ckup);.  }else i
13dd8 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  f( pPager->state
13dd9 21 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 20 26  !=PAGER_SYNCED &
13dda 26 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69  & pPager->dbModi
13ddb 66 69 65 64 20 29 7b 0a 0a 20 20 20 20 2f 2a 20  fied ){..    /* 
13ddc 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c  The following bl
13ddd 6f 63 6b 20 75 70 64 61 74 65 73 20 74 68 65 20  ock updates the 
13dde 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 2e 20  change-counter. 
13ddf 45 78 61 63 74 6c 79 20 68 6f 77 20 69 74 0a 20  Exactly how it. 
13de0 20 20 20 2a 2a 20 64 6f 65 73 20 74 68 69 73 20     ** does this 
13de1 64 65 70 65 6e 64 73 20 6f 6e 20 77 68 65 74 68  depends on wheth
13de2 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 74  er or not the at
13de3 6f 6d 69 63 2d 75 70 64 61 74 65 20 6f 70 74 69  omic-update opti
13de4 6d 69 7a 61 74 69 6f 6e 0a 20 20 20 20 2a 2a 20  mization.    ** 
13de5 77 61 73 20 65 6e 61 62 6c 65 64 20 61 74 20 63  was enabled at c
13de6 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 61 6e 64  ompile time, and
13de7 20 69 66 20 74 68 69 73 20 74 72 61 6e 73 61 63   if this transac
13de8 74 69 6f 6e 20 6d 65 65 74 73 20 74 68 65 20 0a  tion meets the .
13de9 20 20 20 20 2a 2a 20 72 75 6e 74 69 6d 65 20 63      ** runtime c
13dea 72 69 74 65 72 69 61 20 74 6f 20 75 73 65 20 74  riteria to use t
13deb 68 65 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a 20  he operation: . 
13dec 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20     **.    **    
13ded 2a 20 54 68 65 20 66 69 6c 65 2d 73 79 73 74 65  * The file-syste
13dee 6d 20 73 75 70 70 6f 72 74 73 20 74 68 65 20 61  m supports the a
13def 74 6f 6d 69 63 2d 77 72 69 74 65 20 70 72 6f 70  tomic-write prop
13df0 65 72 74 79 20 66 6f 72 0a 20 20 20 20 2a 2a 20  erty for.    ** 
13df1 20 20 20 20 20 62 6c 6f 63 6b 73 20 6f 66 20 73       blocks of s
13df2 69 7a 65 20 70 61 67 65 2d 73 69 7a 65 2c 20 61  ize page-size, a
13df3 6e 64 20 0a 20 20 20 20 2a 2a 20 20 20 20 2a 20  nd .    **    * 
13df4 54 68 69 73 20 63 6f 6d 6d 69 74 20 69 73 20 6e  This commit is n
13df5 6f 74 20 70 61 72 74 20 6f 66 20 61 20 6d 75 6c  ot part of a mul
13df6 74 69 2d 66 69 6c 65 20 74 72 61 6e 73 61 63 74  ti-file transact
13df7 69 6f 6e 2c 20 61 6e 64 0a 20 20 20 20 2a 2a 20  ion, and.    ** 
13df8 20 20 20 2a 20 45 78 61 63 74 6c 79 20 6f 6e 65     * Exactly one
13df9 20 70 61 67 65 20 68 61 73 20 62 65 65 6e 20 6d   page has been m
13dfa 6f 64 69 66 69 65 64 20 61 6e 64 20 73 74 6f 72  odified and stor
13dfb 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e in the journal
13dfc 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20   file..    **.  
13dfd 20 20 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69    ** If the opti
13dfe 6d 69 7a 61 74 69 6f 6e 20 77 61 73 20 6e 6f 74  mization was not
13dff 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70   enabled at comp
13e00 69 6c 65 20 74 69 6d 65 2c 20 74 68 65 6e 20 74  ile time, then t
13e01 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 5f  he.    ** pager_
13e02 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74  incr_changecount
13e03 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73  er() function is
13e04 20 63 61 6c 6c 65 64 20 74 6f 20 75 70 64 61 74   called to updat
13e05 65 20 74 68 65 20 63 68 61 6e 67 65 0a 20 20 20  e the change.   
13e06 20 2a 2a 20 63 6f 75 6e 74 65 72 20 69 6e 20 27   ** counter in '
13e07 69 6e 64 69 72 65 63 74 2d 6d 6f 64 65 27 2e 20  indirect-mode'. 
13e08 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74  If the optimizat
13e09 69 6f 6e 20 69 73 20 63 6f 6d 70 69 6c 65 64 20  ion is compiled 
13e0a 69 6e 20 62 75 74 0a 20 20 20 20 2a 2a 20 69 73  in but.    ** is
13e0b 20 6e 6f 74 20 61 70 70 6c 69 63 61 62 6c 65 20   not applicable 
13e0c 74 6f 20 74 68 69 73 20 74 72 61 6e 73 61 63 74  to this transact
13e0d 69 6f 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65  ion, call sqlite
13e0e 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 29  3JournalCreate()
13e0f 0a 20 20 20 20 2a 2a 20 74 6f 20 6d 61 6b 65 20  .    ** to make 
13e10 73 75 72 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  sure the journal
13e11 20 66 69 6c 65 20 68 61 73 20 61 63 74 75 61 6c   file has actual
13e12 6c 79 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c  ly been created,
13e13 20 74 68 65 6e 20 63 61 6c 6c 0a 20 20 20 20 2a   then call.    *
13e14 2a 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61  * pager_incr_cha
13e15 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20  ngecounter() to 
13e16 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67  update the chang
13e17 65 2d 63 6f 75 6e 74 65 72 20 69 6e 20 69 6e 64  e-counter in ind
13e18 69 72 65 63 74 0a 20 20 20 20 2a 2a 20 6d 6f 64  irect.    ** mod
13e19 65 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  e. .    **.    *
13e1a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20  * Otherwise, if 
13e1b 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  the optimization
13e1c 20 69 73 20 62 6f 74 68 20 65 6e 61 62 6c 65 64   is both enabled
13e1d 20 61 6e 64 20 61 70 70 6c 69 63 61 62 6c 65 2c   and applicable,
13e1e 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 63 61 6c  .    ** then cal
13e1f 6c 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61  l pager_incr_cha
13e20 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20  ngecounter() to 
13e21 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67  update the chang
13e22 65 2d 63 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a  e-counter.    **
13e23 20 69 6e 20 27 64 69 72 65 63 74 27 20 6d 6f 64   in 'direct' mod
13e24 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  e. In this case 
13e25 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
13e26 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 0a 20   will never be. 
13e27 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20 66 6f     ** created fo
13e28 72 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69  r this transacti
13e29 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65  on..    */.#ifde
13e2a 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
13e2b 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20  ATOMIC_WRITE.   
13e2c 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 20 20 20   PgHdr *pPg;.   
13e2d 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28   assert( isOpen(
13e2e 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20  pPager->jfd) || 
13e2f 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
13e30 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
13e31 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a 20 20  ALMODE_OFF );.  
13e32 20 20 69 66 28 20 21 7a 4d 61 73 74 65 72 20 26    if( !zMaster &
13e33 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  & isOpen(pPager-
13e34 3e 6a 66 64 29 20 0a 20 20 20 20 20 26 26 20 70  >jfd) .     && p
13e35 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
13e36 66 3d 3d 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a  f==jrnlBufferSiz
13e37 65 28 70 50 61 67 65 72 29 20 0a 20 20 20 20 20  e(pPager) .     
13e38 26 26 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  && pPager->dbSiz
13e39 65 3e 3d 70 50 61 67 65 72 2d 3e 64 62 46 69 6c  e>=pPager->dbFil
13e3a 65 53 69 7a 65 0a 20 20 20 20 20 26 26 20 28 30  eSize.     && (0
13e3b 3d 3d 28 70 50 67 20 3d 20 73 71 6c 69 74 65 33  ==(pPg = sqlite3
13e3c 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 74 28  PcacheDirtyList(
13e3d 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29  pPager->pPCache)
13e3e 29 20 7c 7c 20 30 3d 3d 70 50 67 2d 3e 70 44 69  ) || 0==pPg->pDi
13e3f 72 74 79 29 0a 20 20 20 20 29 7b 0a 20 20 20 20  rty).    ){.    
13e40 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20    /* Update the 
13e41 64 62 20 66 69 6c 65 20 63 68 61 6e 67 65 20 63  db file change c
13e42 6f 75 6e 74 65 72 20 76 69 61 20 74 68 65 20 64  ounter via the d
13e43 69 72 65 63 74 2d 77 72 69 74 65 20 6d 65 74 68  irect-write meth
13e44 6f 64 2e 20 54 68 65 20 0a 20 20 20 20 20 20 2a  od. The .      *
13e45 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c  * following call
13e46 20 77 69 6c 6c 20 6d 6f 64 69 66 79 20 74 68 65   will modify the
13e47 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65   in-memory repre
13e48 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 70 61 67  sentation of pag
13e49 65 20 31 20 0a 20 20 20 20 20 20 2a 2a 20 74 6f  e 1 .      ** to
13e4a 20 69 6e 63 6c 75 64 65 20 74 68 65 20 75 70 64   include the upd
13e4b 61 74 65 64 20 63 68 61 6e 67 65 20 63 6f 75 6e  ated change coun
13e4c 74 65 72 20 61 6e 64 20 74 68 65 6e 20 77 72 69  ter and then wri
13e4d 74 65 20 70 61 67 65 20 31 20 0a 20 20 20 20 20  te page 1 .     
13e4e 20 2a 2a 20 64 69 72 65 63 74 6c 79 20 74 6f 20   ** directly to 
13e4f 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
13e50 65 2e 20 42 65 63 61 75 73 65 20 6f 66 20 74 68  e. Because of th
13e51 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 0a  e atomic-write .
13e52 20 20 20 20 20 20 2a 2a 20 70 72 6f 70 65 72 74        ** propert
13e53 79 20 6f 66 20 74 68 65 20 68 6f 73 74 20 66 69  y of the host fi
13e54 6c 65 2d 73 79 73 74 65 6d 2c 20 74 68 69 73 20  le-system, this 
13e55 69 73 20 73 61 66 65 2e 0a 20 20 20 20 20 20 2a  is safe..      *
13e56 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67  /.      rc = pag
13e57 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f  er_incr_changeco
13e58 75 6e 74 65 72 28 70 50 61 67 65 72 2c 20 31 29  unter(pPager, 1)
13e59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
13e5a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4a     rc = sqlite3J
13e5b 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 70 50 61  ournalCreate(pPa
13e5c 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20  ger->jfd);.     
13e5d 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
13e5e 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
13e5f 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68   = pager_incr_ch
13e60 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67  angecounter(pPag
13e61 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  er, 0);.      }.
13e62 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20      }.#else.    
13e63 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f  rc = pager_incr_
13e64 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50  changecounter(pP
13e65 61 67 65 72 2c 20 30 29 3b 0a 23 65 6e 64 69 66  ager, 0);.#endif
13e66 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
13e67 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f  ITE_OK ) goto co
13e68 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65  mmit_phase_one_e
13e69 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  xit;..    /* If 
13e6a 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e  this transaction
13e6b 20 68 61 73 20 6d 61 64 65 20 74 68 65 20 64 61   has made the da
13e6c 74 61 62 61 73 65 20 73 6d 61 6c 6c 65 72 2c 20  tabase smaller, 
13e6d 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 0a 20  then all pages. 
13e6e 20 20 20 2a 2a 20 62 65 69 6e 67 20 64 69 73 63     ** being disc
13e6f 61 72 64 65 64 20 62 79 20 74 68 65 20 74 72 75  arded by the tru
13e70 6e 63 61 74 69 6f 6e 20 6d 75 73 74 20 62 65 20  ncation must be 
13e71 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a  written to the j
13e72 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69  ournal.    ** fi
13e73 6c 65 2e 20 54 68 69 73 20 63 61 6e 20 6f 6e 6c  le. This can onl
13e74 79 20 68 61 70 70 65 6e 20 69 6e 20 61 75 74 6f  y happen in auto
13e75 2d 76 61 63 75 75 6d 20 6d 6f 64 65 2e 0a 20 20  -vacuum mode..  
13e76 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 42 65 66 6f    **.    ** Befo
13e77 72 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 70  re reading the p
13e78 61 67 65 73 20 77 69 74 68 20 70 61 67 65 20 6e  ages with page n
13e79 75 6d 62 65 72 73 20 6c 61 72 67 65 72 20 74 68  umbers larger th
13e7a 61 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63  an the .    ** c
13e7b 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20  urrent value of 
13e7c 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 73 65  Pager.dbSize, se
13e7d 74 20 64 62 53 69 7a 65 20 62 61 63 6b 20 74 6f  t dbSize back to
13e7e 20 74 68 65 20 76 61 6c 75 65 0a 20 20 20 20 2a   the value.    *
13e7f 2a 20 74 68 61 74 20 69 74 20 74 6f 6f 6b 20 61  * that it took a
13e80 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  t the start of t
13e81 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  he transaction. 
13e82 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 0a 20  Otherwise, the. 
13e83 20 20 20 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73     ** calls to s
13e84 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29  qlite3PagerGet()
13e85 20 72 65 74 75 72 6e 20 7a 65 72 6f 65 64 20 70   return zeroed p
13e86 61 67 65 73 20 69 6e 73 74 65 61 64 20 6f 66 20  ages instead of 
13e87 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20  .    ** reading 
13e88 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 64 61  data from the da
13e89 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20  tabase file..   
13e8a 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 68 65 6e 20   **.    ** When 
13e8b 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 3d 4f 46  journal_mode==OF
13e8c 46 20 74 68 65 20 64 62 4f 72 69 67 53 69 7a 65  F the dbOrigSize
13e8d 20 69 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2c   is always zero,
13e8e 20 73 6f 20 74 68 69 73 0a 20 20 20 20 2a 2a 20   so this.    ** 
13e8f 62 6c 6f 63 6b 20 6e 65 76 65 72 20 72 75 6e 73  block never runs
13e90 20 69 66 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65   if journal_mode
13e91 3d 4f 46 46 2e 0a 20 20 20 20 2a 2f 0a 23 69 66  =OFF..    */.#if
13e92 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
13e93 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
13e94 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69  if( pPager->dbSi
13e95 7a 65 3c 70 50 61 67 65 72 2d 3e 64 62 4f 72 69  ze<pPager->dbOri
13e96 67 53 69 7a 65 20 0a 20 20 20 20 20 26 26 20 41  gSize .     && A
13e97 4c 57 41 59 53 28 70 50 61 67 65 72 2d 3e 6a 6f  LWAYS(pPager->jo
13e98 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52  urnalMode!=PAGER
13e99 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46  _JOURNALMODE_OFF
13e9a 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 50  ).    ){.      P
13e9b 67 6e 6f 20 69 3b 20 20 20 20 20 20 20 20 20 20  gno i;          
13e9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13e9d 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72           /* Iter
13e9e 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f  ator variable */
13e9f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e  .      const Pgn
13ea0 6f 20 69 53 6b 69 70 20 3d 20 50 41 47 45 52 5f  o iSkip = PAGER_
13ea1 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 3b  MJ_PGNO(pPager);
13ea2 20 2f 2a 20 50 65 6e 64 69 6e 67 20 6c 6f 63 6b   /* Pending lock
13ea3 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 63   page */.      c
13ea4 6f 6e 73 74 20 50 67 6e 6f 20 64 62 53 69 7a 65  onst Pgno dbSize
13ea5 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a   = pPager->dbSiz
13ea6 65 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61  e;       /* Data
13ea7 62 61 73 65 20 69 6d 61 67 65 20 73 69 7a 65 20  base image size 
13ea8 2a 2f 20 0a 20 20 20 20 20 20 70 50 61 67 65 72  */ .      pPager
13ea9 2d 3e 64 62 53 69 7a 65 20 3d 20 70 50 61 67 65  ->dbSize = pPage
13eaa 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3b 0a 20  r->dbOrigSize;. 
13eab 20 20 20 20 20 66 6f 72 28 20 69 3d 64 62 53 69       for( i=dbSi
13eac 7a 65 2b 31 3b 20 69 3c 3d 70 50 61 67 65 72 2d  ze+1; i<=pPager-
13ead 3e 64 62 4f 72 69 67 53 69 7a 65 3b 20 69 2b 2b  >dbOrigSize; i++
13eae 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
13eaf 21 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65  !sqlite3BitvecTe
13eb0 73 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f  st(pPager->pInJo
13eb1 75 72 6e 61 6c 2c 20 69 29 20 26 26 20 69 21 3d  urnal, i) && i!=
13eb2 69 53 6b 69 70 20 29 7b 0a 20 20 20 20 20 20 20  iSkip ){.       
13eb3 20 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 3b     PgHdr *pPage;
13eb4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
13eb5 50 61 67 65 20 74 6f 20 6a 6f 75 72 6e 61 6c 20  Page to journal 
13eb6 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  */.          rc 
13eb7 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
13eb8 74 28 70 50 61 67 65 72 2c 20 69 2c 20 26 70 50  t(pPager, i, &pP
13eb9 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  age);.          
13eba 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
13ebb 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f  K ) goto commit_
13ebc 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a  phase_one_exit;.
13ebd 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
13ebe 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
13ebf 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  (pPage);.       
13ec0 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55     sqlite3PagerU
13ec1 6e 72 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20  nref(pPage);.   
13ec2 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
13ec3 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
13ec4 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65  commit_phase_one
13ec5 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 20 20 7d  _exit;.        }
13ec6 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20 20  .      } .      
13ec7 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d  pPager->dbSize =
13ec8 20 64 62 53 69 7a 65 3b 0a 20 20 20 20 7d 0a 23   dbSize;.    }.#
13ec9 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 57 72  endif..    /* Wr
13eca 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a  ite the master j
13ecb 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 74 6f  ournal name into
13ecc 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
13ecd 65 2e 20 49 66 20 61 20 6d 61 73 74 65 72 20 0a  e. If a master .
13ece 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66      ** journal f
13ecf 69 6c 65 20 6e 61 6d 65 20 68 61 73 20 61 6c 72  ile name has alr
13ed0 65 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65  eady been writte
13ed1 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  n to the journal
13ed2 20 66 69 6c 65 2c 20 0a 20 20 20 20 2a 2a 20 6f   file, .    ** o
13ed3 72 20 69 66 20 7a 4d 61 73 74 65 72 20 69 73 20  r if zMaster is 
13ed4 4e 55 4c 4c 20 28 6e 6f 20 6d 61 73 74 65 72 20  NULL (no master 
13ed5 6a 6f 75 72 6e 61 6c 29 2c 20 74 68 65 6e 20 74  journal), then t
13ed6 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f  his call is a no
13ed7 2d 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  -op..    */.    
13ed8 72 63 20 3d 20 77 72 69 74 65 4d 61 73 74 65 72  rc = writeMaster
13ed9 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2c 20  Journal(pPager, 
13eda 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 69 66  zMaster);.    if
13edb 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
13edc 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68  ) goto commit_ph
13edd 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a 20  ase_one_exit;.. 
13ede 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 6a     /* Sync the j
13edf 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20  ournal file. If 
13ee0 74 68 65 20 61 74 6f 6d 69 63 2d 75 70 64 61 74  the atomic-updat
13ee1 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69  e optimization i
13ee2 73 20 62 65 69 6e 67 0a 20 20 20 20 2a 2a 20 75  s being.    ** u
13ee3 73 65 64 2c 20 74 68 69 73 20 63 61 6c 6c 20 77  sed, this call w
13ee4 69 6c 6c 20 6e 6f 74 20 63 72 65 61 74 65 20 74  ill not create t
13ee5 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
13ee6 6f 72 20 70 65 72 66 6f 72 6d 20 61 6e 79 0a 20  or perform any. 
13ee7 20 20 20 2a 2a 20 72 65 61 6c 20 49 4f 2e 0a 20     ** real IO.. 
13ee8 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73     */.    rc = s
13ee9 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65  yncJournal(pPage
13eea 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  r);.    if( rc!=
13eeb 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
13eec 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e   commit_phase_on
13eed 65 5f 65 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20  e_exit;..    /* 
13eee 57 72 69 74 65 20 61 6c 6c 20 64 69 72 74 79 20  Write all dirty 
13eef 70 61 67 65 73 20 74 6f 20 74 68 65 20 64 61 74  pages to the dat
13ef0 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20  abase file. */. 
13ef1 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72     rc = pager_wr
13ef2 69 74 65 5f 70 61 67 65 6c 69 73 74 28 73 71 6c  ite_pagelist(sql
13ef3 69 74 65 33 50 63 61 63 68 65 44 69 72 74 79 4c  ite3PcacheDirtyL
13ef4 69 73 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61  ist(pPager->pPCa
13ef5 63 68 65 29 29 3b 0a 20 20 20 20 69 66 28 20 72  che));.    if( r
13ef6 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
13ef7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
13ef8 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42  !=SQLITE_IOERR_B
13ef9 4c 4f 43 4b 45 44 20 29 3b 0a 20 20 20 20 20 20  LOCKED );.      
13efa 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73  goto commit_phas
13efb 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 20 20  e_one_exit;.    
13efc 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61  }.    sqlite3Pca
13efd 63 68 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61 67  cheCleanAll(pPag
13efe 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a 20  er->pPCache);.. 
13eff 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c     /* If the fil
13f00 65 20 6f 6e 20 64 69 73 6b 20 69 73 20 6e 6f 74  e on disk is not
13f01 20 74 68 65 20 73 61 6d 65 20 73 69 7a 65 20 61   the same size a
13f02 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  s the database i
13f03 6d 61 67 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65  mage,.    ** the
13f04 6e 20 75 73 65 20 70 61 67 65 72 5f 74 72 75 6e  n use pager_trun
13f05 63 61 74 65 20 74 6f 20 67 72 6f 77 20 6f 72 20  cate to grow or 
13f06 73 68 72 69 6e 6b 20 74 68 65 20 66 69 6c 65 20  shrink the file 
13f07 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  here..    */.   
13f08 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53   if( pPager->dbS
13f09 69 7a 65 21 3d 70 50 61 67 65 72 2d 3e 64 62 46  ize!=pPager->dbF
13f0a 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  ileSize ){.     
13f0b 20 50 67 6e 6f 20 6e 4e 65 77 20 3d 20 70 50 61   Pgno nNew = pPa
13f0c 67 65 72 2d 3e 64 62 53 69 7a 65 20 2d 20 28 70  ger->dbSize - (p
13f0d 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 50  Pager->dbSize==P
13f0e 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61  AGER_MJ_PGNO(pPa
13f0f 67 65 72 29 29 3b 0a 20 20 20 20 20 20 61 73 73  ger));.      ass
13f10 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61  ert( pPager->sta
13f11 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53  te>=PAGER_EXCLUS
13f12 49 56 45 20 29 3b 0a 20 20 20 20 20 20 72 63 20  IVE );.      rc 
13f13 3d 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65  = pager_truncate
13f14 28 70 50 61 67 65 72 2c 20 6e 4e 65 77 29 3b 0a  (pPager, nNew);.
13f15 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
13f16 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63  LITE_OK ) goto c
13f17 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f  ommit_phase_one_
13f18 65 78 69 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  exit;.    }..   
13f19 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20 73 79 6e   /* Finally, syn
13f1a 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  c the database f
13f1b 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ile. */.    if( 
13f1c 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20  !pPager->noSync 
13f1d 26 26 20 21 6e 6f 53 79 6e 63 20 29 7b 0a 20 20  && !noSync ){.  
13f1e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
13f1f 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 66  OsSync(pPager->f
13f20 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f  d, pPager->sync_
13f21 66 6c 61 67 73 29 3b 0a 20 20 20 20 7d 0a 20 20  flags);.    }.  
13f22 20 20 49 4f 54 52 41 43 45 28 28 22 44 42 53 59    IOTRACE(("DBSY
13f23 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72  NC %p\n", pPager
13f24 29 29 0a 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  ))..    pPager->
13f25 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 53 59  state = PAGER_SY
13f26 4e 43 45 44 3b 0a 20 20 7d 0a 0a 63 6f 6d 6d 69  NCED;.  }..commi
13f27 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74  t_phase_one_exit
13f28 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  :.  return rc;.}
13f29 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68  .../*.** When th
13f2a 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
13f2b 61 6c 6c 65 64 2c 20 74 68 65 20 64 61 74 61 62  alled, the datab
13f2c 61 73 65 20 66 69 6c 65 20 68 61 73 20 62 65 65  ase file has bee
13f2d 6e 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20  n completely.** 
13f2e 75 70 64 61 74 65 64 20 74 6f 20 72 65 66 6c 65  updated to refle
13f2f 63 74 20 74 68 65 20 63 68 61 6e 67 65 73 20 6d  ct the changes m
13f30 61 64 65 20 62 79 20 74 68 65 20 63 75 72 72 65  ade by the curre
13f31 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61  nt transaction a
13f32 6e 64 0a 2a 2a 20 73 79 6e 63 65 64 20 74 6f 20  nd.** synced to 
13f33 64 69 73 6b 2e 20 54 68 65 20 6a 6f 75 72 6e 61  disk. The journa
13f34 6c 20 66 69 6c 65 20 73 74 69 6c 6c 20 65 78 69  l file still exi
13f35 73 74 73 20 69 6e 20 74 68 65 20 66 69 6c 65 2d  sts in the file-
13f36 73 79 73 74 65 6d 20 0a 2a 2a 20 74 68 6f 75 67  system .** thoug
13f37 68 2c 20 61 6e 64 20 69 66 20 61 20 66 61 69 6c  h, and if a fail
13f38 75 72 65 20 6f 63 63 75 72 73 20 61 74 20 74 68  ure occurs at th
13f39 69 73 20 70 6f 69 6e 74 20 69 74 20 77 69 6c 6c  is point it will
13f3a 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20 62   eventually.** b
13f3b 65 20 75 73 65 64 20 61 73 20 61 20 68 6f 74 2d  e used as a hot-
13f3c 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 65 20  journal and the 
13f3d 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
13f3e 69 6f 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e  ion rolled back.
13f3f 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
13f40 74 69 6f 6e 20 66 69 6e 61 6c 69 7a 65 73 20 74  tion finalizes t
13f41 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c  he journal file,
13f42 20 65 69 74 68 65 72 20 62 79 20 64 65 6c 65 74   either by delet
13f43 69 6e 67 2c 20 0a 2a 2a 20 74 72 75 6e 63 61 74  ing, .** truncat
13f44 69 6e 67 20 6f 72 20 70 61 72 74 69 61 6c 6c 79  ing or partially
13f45 20 7a 65 72 6f 69 6e 67 20 69 74 2c 20 73 6f 20   zeroing it, so 
13f46 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74 20 62  that it cannot b
13f47 65 20 75 73 65 64 20 0a 2a 2a 20 66 6f 72 20 68  e used .** for h
13f48 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62  ot-journal rollb
13f49 61 63 6b 2e 20 4f 6e 63 65 20 74 68 69 73 20 69  ack. Once this i
13f4a 73 20 64 6f 6e 65 20 74 68 65 20 74 72 61 6e 73  s done the trans
13f4b 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 69 72 72  action is.** irr
13f4c 65 76 6f 63 61 62 6c 79 20 63 6f 6d 6d 69 74 74  evocably committ
13f4d 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  ed..**.** If an 
13f4e 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e  error occurs, an
13f4f 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69   IO error code i
13f50 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74  s returned and t
13f51 68 65 20 70 61 67 65 72 0a 2a 2a 20 6d 6f 76 65  he pager.** move
13f52 73 20 69 6e 74 6f 20 74 68 65 20 65 72 72 6f 72  s into the error
13f53 20 73 74 61 74 65 2e 20 4f 74 68 65 72 77 69 73   state. Otherwis
13f54 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  e, SQLITE_OK is 
13f55 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c  returned..*/.SQL
13f56 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
13f57 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d  sqlite3PagerComm
13f58 69 74 50 68 61 73 65 54 77 6f 28 50 61 67 65 72  itPhaseTwo(Pager
13f59 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74   *pPager){.  int
13f5a 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
13f5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13f5c 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
13f5d 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72   */..  /* This r
13f5e 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6e 6f  outine should no
13f5f 74 20 62 65 20 63 61 6c 6c 65 64 20 69 66 20 61  t be called if a
13f60 20 70 72 69 6f 72 20 65 72 72 6f 72 20 68 61 73   prior error has
13f61 20 6f 63 63 75 72 72 65 64 2e 0a 20 20 2a 2a 20   occurred..  ** 
13f62 42 75 74 20 69 66 20 28 64 75 65 20 74 6f 20 61  But if (due to a
13f63 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 20 65 6c   coding error el
13f64 73 65 77 68 65 72 65 20 69 6e 20 74 68 65 20 73  sewhere in the s
13f65 79 73 74 65 6d 29 20 69 74 20 64 6f 65 73 20 67  ystem) it does g
13f66 65 74 0a 20 20 2a 2a 20 63 61 6c 6c 65 64 2c 20  et.  ** called, 
13f67 6a 75 73 74 20 72 65 74 75 72 6e 20 74 68 65 20  just return the 
13f68 73 61 6d 65 20 65 72 72 6f 72 20 63 6f 64 65 20  same error code 
13f69 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e  without doing an
13f6a 79 74 68 69 6e 67 2e 20 2a 2f 0a 20 20 69 66 28  ything. */.  if(
13f6b 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65   NEVER(pPager->e
13f6c 72 72 43 6f 64 65 29 20 29 20 72 65 74 75 72 6e  rrCode) ) return
13f6d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
13f6e 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66 75 6e  ;..  /* This fun
13f6f 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74  ction should not
13f70 20 62 65 20 63 61 6c 6c 65 64 20 69 66 20 74 68   be called if th
13f71 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74 20 69  e pager is not i
13f72 6e 20 61 74 20 6c 65 61 73 74 0a 20 20 2a 2a 20  n at least.  ** 
13f73 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 73  PAGER_RESERVED s
13f74 74 61 74 65 2e 20 41 6e 64 20 69 6e 64 65 65 64  tate. And indeed
13f75 20 53 51 4c 69 74 65 20 6e 65 76 65 72 20 64 6f   SQLite never do
13f76 65 73 20 74 68 69 73 2e 20 42 75 74 20 69 74 20  es this. But it 
13f77 69 73 0a 20 20 2a 2a 20 6e 69 63 65 20 74 6f 20  is.  ** nice to 
13f78 68 61 76 65 20 74 68 69 73 20 64 65 66 65 6e 73  have this defens
13f79 69 76 65 20 74 65 73 74 20 68 65 72 65 20 61 6e  ive test here an
13f7a 79 77 61 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  yway..  */.  if(
13f7b 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e 73   NEVER(pPager->s
13f7c 74 61 74 65 3c 50 41 47 45 52 5f 52 45 53 45 52  tate<PAGER_RESER
13f7d 56 45 44 29 20 29 20 72 65 74 75 72 6e 20 53 51  VED) ) return SQ
13f7e 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 0a 20 20 2f  LITE_ERROR;..  /
13f7f 2a 20 41 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  * An optimizatio
13f80 6e 2e 20 49 66 20 74 68 65 20 64 61 74 61 62 61  n. If the databa
13f81 73 65 20 77 61 73 20 6e 6f 74 20 61 63 74 75 61  se was not actua
13f82 6c 6c 79 20 6d 6f 64 69 66 69 65 64 20 64 75 72  lly modified dur
13f83 69 6e 67 0a 20 20 2a 2a 20 74 68 69 73 20 74 72  ing.  ** this tr
13f84 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 20 70  ansaction, the p
13f85 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20  ager is running 
13f86 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 6d 6f 64  in exclusive-mod
13f87 65 20 61 6e 64 20 69 73 0a 20 20 2a 2a 20 75 73  e and is.  ** us
13f88 69 6e 67 20 70 65 72 73 69 73 74 65 6e 74 20 6a  ing persistent j
13f89 6f 75 72 6e 61 6c 73 2c 20 74 68 65 6e 20 74 68  ournals, then th
13f8a 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  is function is a
13f8b 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20 20 2a   no-op..  **.  *
13f8c 2a 20 54 68 65 20 73 74 61 72 74 20 6f 66 20 74  * The start of t
13f8d 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
13f8e 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69  currently contai
13f8f 6e 73 20 61 20 73 69 6e 67 6c 65 20 6a 6f 75 72  ns a single jour
13f90 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 61 64 65 72  nal .  ** header
13f91 20 77 69 74 68 20 74 68 65 20 6e 52 65 63 20 66   with the nRec f
13f92 69 65 6c 64 20 73 65 74 20 74 6f 20 30 2e 20 49  ield set to 0. I
13f93 66 20 73 75 63 68 20 61 20 6a 6f 75 72 6e 61 6c  f such a journal
13f94 20 69 73 20 75 73 65 64 20 61 73 0a 20 20 2a 2a   is used as.  **
13f95 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 64   a hot-journal d
13f96 75 72 69 6e 67 20 68 6f 74 2d 6a 6f 75 72 6e 61  uring hot-journa
13f97 6c 20 72 6f 6c 6c 62 61 63 6b 2c 20 30 20 63 68  l rollback, 0 ch
13f98 61 6e 67 65 73 20 77 69 6c 6c 20 62 65 20 6d 61  anges will be ma
13f99 64 65 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 64  de.  ** to the d
13f9a 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 53 6f  atabase file. So
13f9b 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65   there is no nee
13f9c 64 20 74 6f 20 7a 65 72 6f 20 74 68 65 20 6a 6f  d to zero the jo
13f9d 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 61 64  urnal .  ** head
13f9e 65 72 2e 20 53 69 6e 63 65 20 74 68 65 20 70 61  er. Since the pa
13f9f 67 65 72 20 69 73 20 69 6e 20 65 78 63 6c 75 73  ger is in exclus
13fa0 69 76 65 20 6d 6f 64 65 2c 20 74 68 65 72 65 20  ive mode, there 
13fa1 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 2a 2a 20  is no need.  ** 
13fa2 74 6f 20 64 72 6f 70 20 61 6e 79 20 6c 6f 63 6b  to drop any lock
13fa3 73 20 65 69 74 68 65 72 2e 0a 20 20 2a 2f 0a 20  s either..  */. 
13fa4 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 4d   if( pPager->dbM
13fa5 6f 64 69 66 69 65 64 3d 3d 30 20 26 26 20 70 50  odified==0 && pP
13fa6 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d  ager->exclusiveM
13fa7 6f 64 65 20 0a 20 20 20 26 26 20 70 50 61 67 65  ode .   && pPage
13fa8 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d  r->journalMode==
13fa9 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
13faa 45 5f 50 45 52 53 49 53 54 0a 20 20 29 7b 0a 20  E_PERSIST.  ){. 
13fab 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
13fac 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a  r->journalOff==J
13fad 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
13fae 61 67 65 72 29 20 29 3b 0a 20 20 20 20 72 65 74  ager) );.    ret
13faf 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
13fb0 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45   }..  PAGERTRACE
13fb1 28 28 22 43 4f 4d 4d 49 54 20 25 64 5c 6e 22 2c  (("COMMIT %d\n",
13fb2 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29   PAGERID(pPager)
13fb3 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50  ));.  assert( pP
13fb4 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47  ager->state==PAG
13fb5 45 52 5f 53 59 4e 43 45 44 20 7c 7c 20 4d 45 4d  ER_SYNCED || MEM
13fb6 44 42 20 7c 7c 20 21 70 50 61 67 65 72 2d 3e 64  DB || !pPager->d
13fb7 62 4d 6f 64 69 66 69 65 64 20 29 3b 0a 20 20 72  bModified );.  r
13fb8 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72  c = pager_end_tr
13fb9 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72  ansaction(pPager
13fba 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73  , pPager->setMas
13fbb 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  ter);.  return p
13fbc 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65  ager_error(pPage
13fbd 72 2c 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  r, rc);.}../*.**
13fbe 20 52 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 63 68   Rollback all ch
13fbf 61 6e 67 65 73 2e 20 54 68 65 20 64 61 74 61 62  anges. The datab
13fc0 61 73 65 20 66 61 6c 6c 73 20 62 61 63 6b 20 74  ase falls back t
13fc1 6f 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 6d  o PAGER_SHARED m
13fc2 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ode..**.** This 
13fc3 66 75 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d  function perform
13fc4 73 20 74 77 6f 20 74 61 73 6b 73 3a 0a 2a 2a 0a  s two tasks:.**.
13fc5 2a 2a 20 20 20 31 29 20 49 74 20 72 6f 6c 6c 73  **   1) It rolls
13fc6 20 62 61 63 6b 20 74 68 65 20 6a 6f 75 72 6e 61   back the journa
13fc7 6c 20 66 69 6c 65 2c 20 72 65 73 74 6f 72 69 6e  l file, restorin
13fc8 67 20 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66  g all database f
13fc9 69 6c 65 20 61 6e 64 20 0a 2a 2a 20 20 20 20 20  ile and .**     
13fca 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65   in-memory cache
13fcb 20 70 61 67 65 73 20 74 6f 20 74 68 65 20 73 74   pages to the st
13fcc 61 74 65 20 74 68 65 79 20 77 65 72 65 20 69 6e  ate they were in
13fcd 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61   when the transa
13fce 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 77 61  ction.**      wa
13fcf 73 20 6f 70 65 6e 65 64 2c 20 61 6e 64 0a 2a 2a  s opened, and.**
13fd0 20 20 20 32 29 20 49 74 20 66 69 6e 61 6c 69 7a     2) It finaliz
13fd1 65 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  es the journal f
13fd2 69 6c 65 2c 20 73 6f 20 74 68 61 74 20 69 74 20  ile, so that it 
13fd3 69 73 20 6e 6f 74 20 75 73 65 64 20 66 6f 72 20  is not used for 
13fd4 68 6f 74 0a 2a 2a 20 20 20 20 20 20 72 6f 6c 6c  hot.**      roll
13fd5 62 61 63 6b 20 61 74 20 61 6e 79 20 70 6f 69 6e  back at any poin
13fd6 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 2e  t in the future.
13fd7 0a 2a 2a 0a 2a 2a 20 73 75 62 6a 65 63 74 20 74  .**.** subject t
13fd8 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  o the following 
13fd9 71 75 61 6c 69 66 69 63 61 74 69 6f 6e 73 3a 0a  qualifications:.
13fda 2a 2a 0a 2a 2a 20 2a 20 49 66 20 74 68 65 20 6a  **.** * If the j
13fdb 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e  ournal file is n
13fdc 6f 74 20 79 65 74 20 6f 70 65 6e 20 77 68 65 6e  ot yet open when
13fdd 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
13fde 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 20 20 74  s called,.**   t
13fdf 68 65 6e 20 6f 6e 6c 79 20 28 32 29 20 69 73 20  hen only (2) is 
13fe0 70 65 72 66 6f 72 6d 65 64 2e 20 49 6e 20 74 68  performed. In th
13fe1 69 73 20 63 61 73 65 20 74 68 65 72 65 20 69 73  is case there is
13fe2 20 6e 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65   no journal file
13fe3 0a 2a 2a 20 20 20 74 6f 20 72 6f 6c 6c 20 62 61  .**   to roll ba
13fe4 63 6b 2e 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 20 69  ck..**.** * If i
13fe5 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65  n an error state
13fe6 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49   other than SQLI
13fe7 54 45 5f 46 55 4c 4c 2c 20 74 68 65 6e 20 74 61  TE_FULL, then ta
13fe8 73 6b 20 28 31 29 20 69 73 20 0a 2a 2a 20 20 20  sk (1) is .**   
13fe9 70 65 72 66 6f 72 6d 65 64 2e 20 49 66 20 73 75  performed. If su
13fea 63 63 65 73 73 66 75 6c 2c 20 74 61 73 6b 20 28  ccessful, task (
13feb 32 29 2e 20 52 65 67 61 72 64 6c 65 73 73 20 6f  2). Regardless o
13fec 66 20 74 68 65 20 6f 75 74 63 6f 6d 65 0a 2a 2a  f the outcome.**
13fed 20 20 20 6f 66 20 65 69 74 68 65 72 2c 20 74 68     of either, th
13fee 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 65 72  e error state er
13fef 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
13ff0 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c  rned to the call
13ff1 65 72 0a 2a 2a 20 20 20 28 69 2e 65 2e 20 65 69  er.**   (i.e. ei
13ff2 74 68 65 72 20 53 51 4c 49 54 45 5f 49 4f 45 52  ther SQLITE_IOER
13ff3 52 20 6f 72 20 53 51 4c 49 54 45 5f 43 4f 52 52  R or SQLITE_CORR
13ff4 55 50 54 29 2e 0a 2a 2a 0a 2a 2a 20 2a 20 49 66  UPT)..**.** * If
13ff5 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e   the pager is in
13ff6 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20   PAGER_RESERVED 
13ff7 73 74 61 74 65 2c 20 74 68 65 6e 20 61 74 74 65  state, then atte
13ff8 6d 70 74 20 28 31 29 2e 20 57 68 65 74 68 65 72  mpt (1). Whether
13ff9 0a 2a 2a 20 20 20 6f 72 20 6e 6f 74 20 28 31 29  .**   or not (1)
13ffa 20 69 73 20 73 75 63 63 75 73 73 66 75 6c 2c 20   is succussful, 
13ffb 61 6c 73 6f 20 61 74 74 65 6d 70 74 20 28 32 29  also attempt (2)
13ffc 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  . If successful,
13ffd 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 53 51 4c   return.**   SQL
13ffe 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73  ITE_OK. Otherwis
13fff 65 2c 20 65 6e 74 65 72 20 74 68 65 20 65 72 72  e, enter the err
14000 6f 72 20 73 74 61 74 65 20 61 6e 64 20 72 65 74  or state and ret
14001 75 72 6e 20 74 68 65 20 66 69 72 73 74 20 0a 2a  urn the first .*
14002 2a 20 20 20 65 72 72 6f 72 20 63 6f 64 65 20 65  *   error code e
14003 6e 63 6f 75 6e 74 65 72 65 64 2e 20 0a 2a 2a 0a  ncountered. .**.
14004 2a 2a 20 20 20 49 6e 20 74 68 69 73 20 63 61 73  **   In this cas
14005 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 68  e there is no ch
14006 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 64 61  ance that the da
14007 74 61 62 61 73 65 20 77 61 73 20 77 72 69 74 74  tabase was writt
14008 65 6e 20 74 6f 2e 20 0a 2a 2a 20 20 20 53 6f 20  en to. .**   So 
14009 69 73 20 73 61 66 65 20 74 6f 20 66 69 6e 61 6c  is safe to final
1400a 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ize the journal 
1400b 66 69 6c 65 20 65 76 65 6e 20 69 66 20 74 68 65  file even if the
1400c 20 70 6c 61 79 62 61 63 6b 20 0a 2a 2a 20 20 20   playback .**   
1400d 28 6f 70 65 72 61 74 69 6f 6e 20 31 29 20 66 61  (operation 1) fa
1400e 69 6c 65 64 2e 20 48 6f 77 65 76 65 72 20 74 68  iled. However th
1400f 65 20 70 61 67 65 72 20 6d 75 73 74 20 65 6e 74  e pager must ent
14010 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61  er the error sta
14011 74 65 0a 2a 2a 20 20 20 61 73 20 74 68 65 20 63  te.**   as the c
14012 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 69  ontents of the i
14013 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 61  n-memory cache a
14014 72 65 20 6e 6f 77 20 73 75 73 70 65 63 74 2e 0a  re now suspect..
14015 2a 2a 0a 2a 2a 20 2a 20 46 69 6e 61 6c 6c 79 2c  **.** * Finally,
14016 20 69 66 20 69 6e 20 50 41 47 45 52 5f 45 58 43   if in PAGER_EXC
14017 4c 55 53 49 56 45 20 73 74 61 74 65 2c 20 74 68  LUSIVE state, th
14018 65 6e 20 61 74 74 65 6d 70 74 20 28 31 29 2e 20  en attempt (1). 
14019 4f 6e 6c 79 0a 2a 2a 20 20 20 61 74 74 65 6d 70  Only.**   attemp
1401a 74 20 28 32 29 20 69 66 20 28 31 29 20 69 73 20  t (2) if (1) is 
1401b 73 75 63 63 65 73 73 66 75 6c 2e 20 52 65 74 75  successful. Retu
1401c 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20  rn SQLITE_OK if 
1401d 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 20  successful,.**  
1401e 20 6f 74 68 65 72 77 69 73 65 20 65 6e 74 65 72   otherwise enter
1401f 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65   the error state
14020 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20   and return the 
14021 65 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20  error code from 
14022 74 68 65 20 0a 2a 2a 20 20 20 66 61 69 6c 69 6e  the .**   failin
14023 67 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a  g operation..**.
14024 2a 2a 20 20 20 49 6e 20 74 68 69 73 20 63 61 73  **   In this cas
14025 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  e the database f
14026 69 6c 65 20 6d 61 79 20 68 61 76 65 20 62 65 65  ile may have bee
14027 6e 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 53 6f  n written to. So
14028 20 69 66 20 74 68 65 0a 2a 2a 20 20 20 70 6c 61   if the.**   pla
14029 79 62 61 63 6b 20 6f 70 65 72 61 74 69 6f 6e 20  yback operation 
1402a 64 69 64 20 6e 6f 74 20 73 75 63 63 65 65 64 20  did not succeed 
1402b 69 74 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 20  it would not be 
1402c 73 61 66 65 20 74 6f 20 66 69 6e 61 6c 69 7a 65  safe to finalize
1402d 0a 2a 2a 20 20 20 74 68 65 20 6a 6f 75 72 6e 61  .**   the journa
1402e 6c 20 66 69 6c 65 2e 20 49 74 20 6e 65 65 64 73  l file. It needs
1402f 20 74 6f 20 62 65 20 6c 65 66 74 20 69 6e 20 74   to be left in t
14030 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 73  he file-system s
14031 6f 20 74 68 61 74 0a 2a 2a 20 20 20 73 6f 6d 65  o that.**   some
14032 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 63   other process c
14033 61 6e 20 75 73 65 20 69 74 20 74 6f 20 72 65 73  an use it to res
14034 74 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73  tore the databas
14035 65 20 73 74 61 74 65 20 28 62 79 0a 2a 2a 20 20  e state (by.**  
14036 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c   hot-journal rol
14037 6c 62 61 63 6b 29 2e 0a 2a 2f 0a 53 51 4c 49 54  lback)..*/.SQLIT
14038 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
14039 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61  lite3PagerRollba
1403a 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ck(Pager *pPager
1403b 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
1403c 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20  LITE_OK;        
1403d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
1403e 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 50 41  urn code */.  PA
1403f 47 45 52 54 52 41 43 45 28 28 22 52 4f 4c 4c 42  GERTRACE(("ROLLB
14040 41 43 4b 20 25 64 5c 6e 22 2c 20 50 41 47 45 52  ACK %d\n", PAGER
14041 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20  ID(pPager)));.  
14042 69 66 28 20 21 70 50 61 67 65 72 2d 3e 64 62 4d  if( !pPager->dbM
14043 6f 64 69 66 69 65 64 20 7c 7c 20 21 69 73 4f 70  odified || !isOp
14044 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
14045 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65  ){.    rc = page
14046 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  r_end_transactio
14047 6e 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72  n(pPager, pPager
14048 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a 20 20  ->setMaster);.  
14049 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 72  }else if( pPager
1404a 2d 3e 65 72 72 43 6f 64 65 20 26 26 20 70 50 61  ->errCode && pPa
1404b 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51  ger->errCode!=SQ
1404c 4c 49 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20  LITE_FULL ){.   
1404d 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61   if( pPager->sta
1404e 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53  te>=PAGER_EXCLUS
1404f 49 56 45 20 29 7b 0a 20 20 20 20 20 20 70 61 67  IVE ){.      pag
14050 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67  er_playback(pPag
14051 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  er, 0);.    }.  
14052 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65    rc = pPager->e
14053 72 72 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 7b  rrCode;.  }else{
14054 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d  .    if( pPager-
14055 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45  >state==PAGER_RE
14056 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20 20 20  SERVED ){.      
14057 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 20 20 72  int rc2;.      r
14058 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61  c = pager_playba
14059 63 6b 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20  ck(pPager, 0);. 
1405a 20 20 20 20 20 72 63 32 20 3d 20 70 61 67 65 72       rc2 = pager
1405b 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e  _end_transaction
1405c 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d  (pPager, pPager-
1405d 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a 20 20 20  >setMaster);.   
1405e 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1405f 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
14060 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 20 20  rc = rc2;.      
14061 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
14062 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c     rc = pager_pl
14063 61 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 30  ayback(pPager, 0
14064 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  );.    }..    if
14065 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20  ( !MEMDB ){.    
14066 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
14067 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 7d  Valid = 0;.    }
14068 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 65  ..    /* If an e
14069 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69  rror occurs duri
1406a 6e 67 20 61 20 52 4f 4c 4c 42 41 43 4b 2c 20 77  ng a ROLLBACK, w
1406b 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20  e can no longer 
1406c 74 72 75 73 74 20 74 68 65 20 70 61 67 65 72 0a  trust the pager.
1406d 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 20 53 6f      ** cache. So
1406e 20 63 61 6c 6c 20 70 61 67 65 72 5f 65 72 72 6f   call pager_erro
1406f 72 28 29 20 6f 6e 20 74 68 65 20 77 61 79 20 6f  r() on the way o
14070 75 74 20 74 6f 20 6d 61 6b 65 20 61 6e 79 20 65  ut to make any e
14071 72 72 6f 72 20 0a 20 20 20 20 2a 2a 20 70 65 72  rror .    ** per
14072 73 69 73 74 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a  sistent..    */.
14073 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65      rc = pager_e
14074 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29  rror(pPager, rc)
14075 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
14076 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  c;.}../*.** Retu
14077 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 64  rn TRUE if the d
14078 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20  atabase file is 
14079 6f 70 65 6e 65 64 20 72 65 61 64 2d 6f 6e 6c 79  opened read-only
1407a 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45 0a  .  Return FALSE.
1407b 2a 2a 20 69 66 20 74 68 65 20 64 61 74 61 62 61  ** if the databa
1407c 73 65 20 69 73 20 28 69 6e 20 74 68 65 6f 72 79  se is (in theory
1407d 29 20 77 72 69 74 61 62 6c 65 2e 0a 2a 2f 0a 53  ) writable..*/.S
1407e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 38  QLITE_PRIVATE u8
1407f 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 72   sqlite3PagerIsr
14080 65 61 64 6f 6e 6c 79 28 50 61 67 65 72 20 2a 70  eadonly(Pager *p
14081 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e  Pager){.  return
14082 20 70 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c   pPager->readOnl
14083 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  y;.}../*.** Retu
14084 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
14085 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74   references to t
14086 68 65 20 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c  he pager..*/.SQL
14087 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
14088 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63  sqlite3PagerRefc
14089 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67  ount(Pager *pPag
1408a 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  er){.  return sq
1408b 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f  lite3PcacheRefCo
1408c 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61  unt(pPager->pPCa
1408d 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  che);.}../*.** R
1408e 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
1408f 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74   of references t
14090 6f 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  o the specified 
14091 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  page..*/.SQLITE_
14092 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
14093 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63  te3PagerPageRefc
14094 6f 75 6e 74 28 44 62 50 61 67 65 20 2a 70 50 61  ount(DbPage *pPa
14095 67 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  ge){.  return sq
14096 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52  lite3PcachePageR
14097 65 66 63 6f 75 6e 74 28 70 50 61 67 65 29 3b 0a  efcount(pPage);.
14098 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
14099 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73  _TEST./*.** This
1409a 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64   routine is used
1409b 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64   for testing and
1409c 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 0a   analysis only..
1409d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1409e 45 20 69 6e 74 20 2a 73 71 6c 69 74 65 33 50 61  E int *sqlite3Pa
1409f 67 65 72 53 74 61 74 73 28 50 61 67 65 72 20 2a  gerStats(Pager *
140a0 70 50 61 67 65 72 29 7b 0a 20 20 73 74 61 74 69  pPager){.  stati
140a1 63 20 69 6e 74 20 61 5b 31 31 5d 3b 0a 20 20 61  c int a[11];.  a
140a2 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 61  [0] = sqlite3Pca
140a3 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67  cheRefCount(pPag
140a4 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20  er->pPCache);.  
140a5 61 5b 31 5d 20 3d 20 73 71 6c 69 74 65 33 50 63  a[1] = sqlite3Pc
140a6 61 63 68 65 50 61 67 65 63 6f 75 6e 74 28 70 50  achePagecount(pP
140a7 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a  ager->pPCache);.
140a8 20 20 61 5b 32 5d 20 3d 20 73 71 6c 69 74 65 33    a[2] = sqlite3
140a9 50 63 61 63 68 65 47 65 74 43 61 63 68 65 73 69  PcacheGetCachesi
140aa 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  ze(pPager->pPCac
140ab 68 65 29 3b 0a 20 20 61 5b 33 5d 20 3d 20 70 50  he);.  a[3] = pP
140ac 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69  ager->dbSizeVali
140ad 64 20 3f 20 28 69 6e 74 29 20 70 50 61 67 65 72  d ? (int) pPager
140ae 2d 3e 64 62 53 69 7a 65 20 3a 20 2d 31 3b 0a 20  ->dbSize : -1;. 
140af 20 61 5b 34 5d 20 3d 20 70 50 61 67 65 72 2d 3e   a[4] = pPager->
140b0 73 74 61 74 65 3b 0a 20 20 61 5b 35 5d 20 3d 20  state;.  a[5] = 
140b1 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b  pPager->errCode;
140b2 0a 20 20 61 5b 36 5d 20 3d 20 70 50 61 67 65 72  .  a[6] = pPager
140b3 2d 3e 6e 48 69 74 3b 0a 20 20 61 5b 37 5d 20 3d  ->nHit;.  a[7] =
140b4 20 70 50 61 67 65 72 2d 3e 6e 4d 69 73 73 3b 0a   pPager->nMiss;.
140b5 20 20 61 5b 38 5d 20 3d 20 30 3b 20 20 2f 2a 20    a[8] = 0;  /* 
140b6 55 73 65 64 20 74 6f 20 62 65 20 70 50 61 67 65  Used to be pPage
140b7 72 2d 3e 6e 4f 76 66 6c 20 2a 2f 0a 20 20 61 5b  r->nOvfl */.  a[
140b8 39 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 52 65  9] = pPager->nRe
140b9 61 64 3b 0a 20 20 61 5b 31 30 5d 20 3d 20 70 50  ad;.  a[10] = pP
140ba 61 67 65 72 2d 3e 6e 57 72 69 74 65 3b 0a 20 20  ager->nWrite;.  
140bb 72 65 74 75 72 6e 20 61 3b 0a 7d 0a 23 65 6e 64  return a;.}.#end
140bc 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  if../*.** Return
140bd 20 74 72 75 65 20 69 66 20 74 68 69 73 20 69 73   true if this is
140be 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61   an in-memory pa
140bf 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ger..*/.SQLITE_P
140c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
140c1 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28 50  e3PagerIsMemdb(P
140c2 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
140c3 20 72 65 74 75 72 6e 20 4d 45 4d 44 42 3b 0a 7d   return MEMDB;.}
140c4 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68  ../*.** Check th
140c5 61 74 20 74 68 65 72 65 20 61 72 65 20 61 74 20  at there are at 
140c6 6c 65 61 73 74 20 6e 53 61 76 65 70 6f 69 6e 74  least nSavepoint
140c7 20 73 61 76 65 70 6f 69 6e 74 73 20 6f 70 65 6e   savepoints open
140c8 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 0a 2a  . If there are.*
140c9 2a 20 63 75 72 72 65 6e 74 6c 79 20 6c 65 73 73  * currently less
140ca 20 74 68 61 6e 20 6e 53 61 76 65 70 6f 69 6e 74   than nSavepoint
140cb 73 20 6f 70 65 6e 2c 20 74 68 65 6e 20 6f 70 65  s open, then ope
140cc 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 61  n one or more sa
140cd 76 65 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 6d  vepoints.** to m
140ce 61 6b 65 20 75 70 20 74 68 65 20 64 69 66 66 65  ake up the diffe
140cf 72 65 6e 63 65 2e 20 49 66 20 74 68 65 20 6e 75  rence. If the nu
140d0 6d 62 65 72 20 6f 66 20 73 61 76 65 70 6f 69 6e  mber of savepoin
140d1 74 73 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a  ts is already.**
140d2 20 65 71 75 61 6c 20 74 6f 20 6e 53 61 76 65 70   equal to nSavep
140d3 6f 69 6e 74 2c 20 74 68 65 6e 20 74 68 69 73 20  oint, then this 
140d4 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f  function is a no
140d5 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  -op..**.** If a 
140d6 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
140d7 6e 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f  n fails, SQLITE_
140d8 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65  NOMEM is returne
140d9 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 0a  d. If an error .
140da 2a 2a 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20  ** occurs while 
140db 6f 70 65 6e 69 6e 67 20 74 68 65 20 73 75 62 2d  opening the sub-
140dc 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68  journal file, th
140dd 65 6e 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63  en an IO error c
140de 6f 64 65 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e  ode is.** return
140df 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53  ed. Otherwise, S
140e0 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c  QLITE_OK..*/.SQL
140e1 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
140e2 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e  sqlite3PagerOpen
140e3 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20  Savepoint(Pager 
140e4 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6e 53 61  *pPager, int nSa
140e5 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20  vepoint){.  int 
140e6 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
140e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140e8 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
140e9 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  code */.  int nC
140ea 75 72 72 65 6e 74 20 3d 20 70 50 61 67 65 72 2d  urrent = pPager-
140eb 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20  >nSavepoint;    
140ec 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e      /* Current n
140ed 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70 6f 69  umber of savepoi
140ee 6e 74 73 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 53  nts */..  if( nS
140ef 61 76 65 70 6f 69 6e 74 3e 6e 43 75 72 72 65 6e  avepoint>nCurren
140f0 74 20 26 26 20 70 50 61 67 65 72 2d 3e 75 73 65  t && pPager->use
140f1 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 69  Journal ){.    i
140f2 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20  nt ii;          
140f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140f4 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74         /* Iterat
140f5 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20  or variable */. 
140f6 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e     PagerSavepoin
140f7 74 20 2a 61 4e 65 77 3b 20 20 20 20 20 20 20 20  t *aNew;        
140f8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
140f9 77 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69  w Pager.aSavepoi
140fa 6e 74 20 61 72 72 61 79 20 2a 2f 0a 0a 20 20 20  nt array */..   
140fb 20 2f 2a 20 45 69 74 68 65 72 20 74 68 65 72 65   /* Either there
140fc 20 69 73 20 6e 6f 20 61 63 74 69 76 65 20 6a 6f   is no active jo
140fd 75 72 6e 61 6c 20 6f 72 20 74 68 65 20 73 75 62  urnal or the sub
140fe 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65 6e  -journal is open
140ff 20 6f 72 20 0a 20 20 20 20 2a 2a 20 74 68 65 20   or .    ** the 
14100 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c 77 61 79  journal is alway
14101 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f  s stored in memo
14102 72 79 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  ry */.    assert
14103 28 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70  ( pPager->nSavep
14104 6f 69 6e 74 3d 3d 30 20 7c 7c 20 69 73 4f 70 65  oint==0 || isOpe
14105 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20  n(pPager->sjfd) 
14106 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ||.            p
14107 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
14108 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
14109 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a  LMODE_MEMORY );.
1410a 0a 20 20 20 20 2f 2a 20 47 72 6f 77 20 74 68 65  .    /* Grow the
1410b 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e   Pager.aSavepoin
1410c 74 20 61 72 72 61 79 20 75 73 69 6e 67 20 72 65  t array using re
1410d 61 6c 6c 6f 63 28 29 2e 20 52 65 74 75 72 6e 20  alloc(). Return 
1410e 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 20  SQLITE_NOMEM.   
1410f 20 2a 2a 20 69 66 20 74 68 65 20 61 6c 6c 6f 63   ** if the alloc
14110 61 74 69 6f 6e 20 66 61 69 6c 73 2e 20 4f 74 68  ation fails. Oth
14111 65 72 77 69 73 65 2c 20 7a 65 72 6f 20 74 68 65  erwise, zero the
14112 20 6e 65 77 20 70 6f 72 74 69 6f 6e 20 69 6e 20   new portion in 
14113 63 61 73 65 20 61 20 0a 20 20 20 20 2a 2a 20 6d  case a .    ** m
14114 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63  alloc failure oc
14115 63 75 72 73 20 77 68 69 6c 65 20 70 6f 70 75 6c  curs while popul
14116 61 74 69 6e 67 20 69 74 20 69 6e 20 74 68 65 20  ating it in the 
14117 66 6f 72 28 2e 2e 2e 29 20 6c 6f 6f 70 20 62 65  for(...) loop be
14118 6c 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  low..    */.    
14119 61 4e 65 77 20 3d 20 28 50 61 67 65 72 53 61 76  aNew = (PagerSav
1411a 65 70 6f 69 6e 74 20 2a 29 73 71 6c 69 74 65 33  epoint *)sqlite3
1411b 52 65 61 6c 6c 6f 63 28 0a 20 20 20 20 20 20 20  Realloc(.       
1411c 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f   pPager->aSavepo
1411d 69 6e 74 2c 20 73 69 7a 65 6f 66 28 50 61 67 65  int, sizeof(Page
1411e 72 53 61 76 65 70 6f 69 6e 74 29 2a 6e 53 61 76  rSavepoint)*nSav
1411f 65 70 6f 69 6e 74 0a 20 20 20 20 29 3b 0a 20 20  epoint.    );.  
14120 20 20 69 66 28 20 21 61 4e 65 77 20 29 7b 0a 20    if( !aNew ){. 
14121 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
14122 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
14123 20 20 20 20 6d 65 6d 73 65 74 28 26 61 4e 65 77      memset(&aNew
14124 5b 6e 43 75 72 72 65 6e 74 5d 2c 20 30 2c 20 28  [nCurrent], 0, (
14125 6e 53 61 76 65 70 6f 69 6e 74 2d 6e 43 75 72 72  nSavepoint-nCurr
14126 65 6e 74 29 20 2a 20 73 69 7a 65 6f 66 28 50 61  ent) * sizeof(Pa
14127 67 65 72 53 61 76 65 70 6f 69 6e 74 29 29 3b 0a  gerSavepoint));.
14128 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76      pPager->aSav
14129 65 70 6f 69 6e 74 20 3d 20 61 4e 65 77 3b 0a 20  epoint = aNew;. 
1412a 20 20 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65     pPager->nSave
1412b 70 6f 69 6e 74 20 3d 20 6e 53 61 76 65 70 6f 69  point = nSavepoi
1412c 6e 74 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75  nt;..    /* Popu
1412d 6c 61 74 65 20 74 68 65 20 50 61 67 65 72 53 61  late the PagerSa
1412e 76 65 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72  vepoint structur
1412f 65 73 20 6a 75 73 74 20 61 6c 6c 6f 63 61 74 65  es just allocate
14130 64 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 69  d. */.    for(ii
14131 3d 6e 43 75 72 72 65 6e 74 3b 20 69 69 3c 6e 53  =nCurrent; ii<nS
14132 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b  avepoint; ii++){
14133 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
14134 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c  Pager->dbSizeVal
14135 69 64 20 29 3b 0a 20 20 20 20 20 20 61 4e 65 77  id );.      aNew
14136 5b 69 69 5d 2e 6e 4f 72 69 67 20 3d 20 70 50 61  [ii].nOrig = pPa
14137 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 20  ger->dbSize;.   
14138 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50     if( isOpen(pP
14139 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 41 4c  ager->jfd) && AL
1413a 57 41 59 53 28 70 50 61 67 65 72 2d 3e 6a 6f 75  WAYS(pPager->jou
1413b 72 6e 61 6c 4f 66 66 3e 30 29 20 29 7b 0a 20 20  rnalOff>0) ){.  
1413c 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69        aNew[ii].i
1413d 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 72 2d  Offset = pPager-
1413e 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 20  >journalOff;.   
1413f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
14140 20 20 61 4e 65 77 5b 69 69 5d 2e 69 4f 66 66 73    aNew[ii].iOffs
14141 65 74 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52  et = JOURNAL_HDR
14142 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 20  _SZ(pPager);.   
14143 20 20 20 7d 0a 20 20 20 20 20 20 61 4e 65 77 5b     }.      aNew[
14144 69 69 5d 2e 69 53 75 62 52 65 63 20 3d 20 70 50  ii].iSubRec = pP
14145 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 3b 0a 20  ager->nSubRec;. 
14146 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 70 49       aNew[ii].pI
14147 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 73 71 6c  nSavepoint = sql
14148 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65  ite3BitvecCreate
14149 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29  (pPager->dbSize)
1414a 3b 0a 20 20 20 20 20 20 69 66 28 20 21 61 4e 65  ;.      if( !aNe
1414b 77 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69  w[ii].pInSavepoi
1414c 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  nt ){.        re
1414d 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
1414e 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  M;.      }.    }
1414f 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68  ..    /* Open th
14150 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2c 20 69  e sub-journal, i
14151 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65  f it is not alre
14152 61 64 79 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20  ady opened. */. 
14153 20 20 20 72 63 20 3d 20 6f 70 65 6e 53 75 62 4a     rc = openSubJ
14154 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a  ournal(pPager);.
14155 20 20 20 20 61 73 73 65 72 74 54 72 75 6e 63 61      assertTrunca
14156 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61  teConstraint(pPa
14157 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  ger);.  }..  ret
14158 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
14159 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
1415a 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 6f 6c 6c  s called to roll
1415b 62 61 63 6b 20 6f 72 20 72 65 6c 65 61 73 65 20  back or release 
1415c 28 63 6f 6d 6d 69 74 29 20 61 20 73 61 76 65 70  (commit) a savep
1415d 6f 69 6e 74 2e 0a 2a 2a 20 54 68 65 20 73 61 76  oint..** The sav
1415e 65 70 6f 69 6e 74 20 74 6f 20 72 65 6c 65 61 73  epoint to releas
1415f 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6e 65  e or rollback ne
14160 65 64 20 6e 6f 74 20 62 65 20 74 68 65 20 6d 6f  ed not be the mo
14161 73 74 20 72 65 63 65 6e 74 6c 79 20 0a 2a 2a 20  st recently .** 
14162 63 72 65 61 74 65 64 20 73 61 76 65 70 6f 69 6e  created savepoin
14163 74 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74  t..**.** Paramet
14164 65 72 20 6f 70 20 69 73 20 61 6c 77 61 79 73 20  er op is always 
14165 65 69 74 68 65 72 20 53 41 56 45 50 4f 49 4e 54  either SAVEPOINT
14166 5f 52 4f 4c 4c 42 41 43 4b 20 6f 72 20 53 41 56  _ROLLBACK or SAV
14167 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 0a  EPOINT_RELEASE..
14168 2a 2a 20 49 66 20 69 74 20 69 73 20 53 41 56 45  ** If it is SAVE
14169 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 74  POINT_RELEASE, t
1416a 68 65 6e 20 72 65 6c 65 61 73 65 20 61 6e 64 20  hen release and 
1416b 64 65 73 74 72 6f 79 20 74 68 65 20 73 61 76 65  destroy the save
1416c 70 6f 69 6e 74 20 77 69 74 68 0a 2a 2a 20 69 6e  point with.** in
1416d 64 65 78 20 69 53 61 76 65 70 6f 69 6e 74 2e 20  dex iSavepoint. 
1416e 49 66 20 69 74 20 69 73 20 53 41 56 45 50 4f 49  If it is SAVEPOI
1416f 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 65  NT_ROLLBACK, the
14170 6e 20 72 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 63  n rollback all c
14171 68 61 6e 67 65 73 0a 2a 2a 20 74 68 61 74 20 68  hanges.** that h
14172 61 76 65 20 6f 63 63 75 72 72 65 64 20 73 69 6e  ave occurred sin
14173 63 65 20 74 68 65 20 73 70 65 63 69 66 69 65 64  ce the specified
14174 20 73 61 76 65 70 6f 69 6e 74 20 77 61 73 20 63   savepoint was c
14175 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  reated..**.** Th
14176 65 20 73 61 76 65 70 6f 69 6e 74 20 74 6f 20 72  e savepoint to r
14177 6f 6c 6c 62 61 63 6b 20 6f 72 20 72 65 6c 65 61  ollback or relea
14178 73 65 20 69 73 20 69 64 65 6e 74 69 66 69 65 64  se is identified
14179 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 0a 2a   by parameter .*
1417a 2a 20 69 53 61 76 65 70 6f 69 6e 74 2e 20 41 20  * iSavepoint. A 
1417b 76 61 6c 75 65 20 6f 66 20 30 20 6d 65 61 6e 73  value of 0 means
1417c 20 74 6f 20 6f 70 65 72 61 74 65 20 6f 6e 20 74   to operate on t
1417d 68 65 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76  he outermost sav
1417e 65 70 6f 69 6e 74 0a 2a 2a 20 28 74 68 65 20 66  epoint.** (the f
1417f 69 72 73 74 20 63 72 65 61 74 65 64 29 2e 20 41  irst created). A
14180 20 76 61 6c 75 65 20 6f 66 20 28 50 61 67 65 72   value of (Pager
14181 2e 6e 53 61 76 65 70 6f 69 6e 74 2d 31 29 20 6d  .nSavepoint-1) m
14182 65 61 6e 73 20 6f 70 65 72 61 74 65 0a 2a 2a 20  eans operate.** 
14183 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  on the most rece
14184 6e 74 6c 79 20 63 72 65 61 74 65 64 20 73 61 76  ntly created sav
14185 65 70 6f 69 6e 74 2e 20 49 66 20 69 53 61 76 65  epoint. If iSave
14186 70 6f 69 6e 74 20 69 73 20 67 72 65 61 74 65 72  point is greater
14187 20 74 68 61 6e 0a 2a 2a 20 28 50 61 67 65 72 2e   than.** (Pager.
14188 6e 53 61 76 65 70 6f 69 6e 74 2d 31 29 2c 20 74  nSavepoint-1), t
14189 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
1418a 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  n is a no-op..**
1418b 0a 2a 2a 20 49 66 20 61 20 6e 65 67 61 74 69 76  .** If a negativ
1418c 65 20 76 61 6c 75 65 20 69 73 20 70 61 73 73 65  e value is passe
1418d 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  d to this functi
1418e 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72  on, then the cur
1418f 72 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74  rent.** transact
14190 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61  ion is rolled ba
14191 63 6b 2e 20 54 68 69 73 20 69 73 20 64 69 66 66  ck. This is diff
14192 65 72 65 6e 74 20 74 6f 20 63 61 6c 6c 69 6e 67  erent to calling
14193 20 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65   .** sqlite3Page
14194 72 52 6f 6c 6c 62 61 63 6b 28 29 20 62 65 63 61  rRollback() beca
14195 75 73 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f  use this functio
14196 6e 20 64 6f 65 73 20 6e 6f 74 20 74 65 72 6d 69  n does not termi
14197 6e 61 74 65 0a 2a 2a 20 74 68 65 20 74 72 61 6e  nate.** the tran
14198 73 61 63 74 69 6f 6e 20 6f 72 20 75 6e 6c 6f 63  saction or unloc
14199 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20  k the database, 
1419a 69 74 20 6a 75 73 74 20 72 65 73 74 6f 72 65 73  it just restores
1419b 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e 74   the .** content
1419c 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  s of the databas
1419d 65 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61  e to its origina
1419e 6c 20 73 74 61 74 65 2e 20 0a 2a 2a 0a 2a 2a 20  l state. .**.** 
1419f 49 6e 20 61 6e 79 20 63 61 73 65 2c 20 61 6c 6c  In any case, all
141a0 20 73 61 76 65 70 6f 69 6e 74 73 20 77 69 74 68   savepoints with
141a1 20 61 6e 20 69 6e 64 65 78 20 67 72 65 61 74 65   an index greate
141a2 72 20 74 68 61 6e 20 69 53 61 76 65 70 6f 69 6e  r than iSavepoin
141a3 74 20 0a 2a 2a 20 61 72 65 20 64 65 73 74 72 6f  t .** are destro
141a4 79 65 64 2e 20 49 66 20 74 68 69 73 20 69 73 20  yed. If this is 
141a5 61 20 72 65 6c 65 61 73 65 20 6f 70 65 72 61 74  a release operat
141a6 69 6f 6e 20 28 6f 70 3d 3d 53 41 56 45 50 4f 49  ion (op==SAVEPOI
141a7 4e 54 5f 52 45 4c 45 41 53 45 29 2c 0a 2a 2a 20  NT_RELEASE),.** 
141a8 74 68 65 6e 20 73 61 76 65 70 6f 69 6e 74 20 69  then savepoint i
141a9 53 61 76 65 70 6f 69 6e 74 20 69 73 20 61 6c 73  Savepoint is als
141aa 6f 20 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2a 0a  o destroyed..**.
141ab 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
141ac 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49   may return SQLI
141ad 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 65  TE_NOMEM if a me
141ae 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
141af 66 61 69 6c 73 2c 0a 2a 2a 20 6f 72 20 61 6e 20  fails,.** or an 
141b0 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66  IO error code if
141b1 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63   an IO error occ
141b2 75 72 73 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e  urs while rollin
141b3 67 20 62 61 63 6b 20 61 20 0a 2a 2a 20 73 61 76  g back a .** sav
141b4 65 70 6f 69 6e 74 2e 20 49 66 20 6e 6f 20 65 72  epoint. If no er
141b5 72 6f 72 73 20 6f 63 63 75 72 2c 20 53 51 4c 49  rors occur, SQLI
141b6 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
141b7 64 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52  d..*/ .SQLITE_PR
141b8 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
141b9 33 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 28  3PagerSavepoint(
141ba 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
141bb 6e 74 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65  nt op, int iSave
141bc 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63  point){.  int rc
141bd 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
141be 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 56   assert( op==SAV
141bf 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 7c  EPOINT_RELEASE |
141c0 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f  | op==SAVEPOINT_
141c1 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 61 73  ROLLBACK );.  as
141c2 73 65 72 74 28 20 69 53 61 76 65 70 6f 69 6e 74  sert( iSavepoint
141c3 3e 3d 30 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50  >=0 || op==SAVEP
141c4 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b  OINT_ROLLBACK );
141c5 0a 0a 20 20 69 66 28 20 69 53 61 76 65 70 6f 69  ..  if( iSavepoi
141c6 6e 74 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65  nt<pPager->nSave
141c7 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74  point ){.    int
141c8 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20   ii;            
141c9 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69  /* Iterator vari
141ca 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20  able */.    int 
141cb 6e 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 2f  nNew;          /
141cc 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 6d 61  * Number of rema
141cd 69 6e 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 73  ining savepoints
141ce 20 61 66 74 65 72 20 74 68 69 73 20 6f 70 2e 20   after this op. 
141cf 2a 2f 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 72  */..    /* Figur
141d0 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 73  e out how many s
141d1 61 76 65 70 6f 69 6e 74 73 20 77 69 6c 6c 20 73  avepoints will s
141d2 74 69 6c 6c 20 62 65 20 61 63 74 69 76 65 20 61  till be active a
141d3 66 74 65 72 20 74 68 69 73 0a 20 20 20 20 2a 2a  fter this.    **
141d4 20 6f 70 65 72 61 74 69 6f 6e 2e 20 53 74 6f 72   operation. Stor
141d5 65 20 74 68 69 73 20 76 61 6c 75 65 20 69 6e 20  e this value in 
141d6 6e 4e 65 77 2e 20 54 68 65 6e 20 66 72 65 65 20  nNew. Then free 
141d7 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69  resources associ
141d8 61 74 65 64 20 0a 20 20 20 20 2a 2a 20 77 69 74  ated .    ** wit
141d9 68 20 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 73  h any savepoints
141da 20 74 68 61 74 20 61 72 65 20 64 65 73 74 72 6f   that are destro
141db 79 65 64 20 62 79 20 74 68 69 73 20 6f 70 65 72  yed by this oper
141dc 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20  ation..    */.  
141dd 20 20 6e 4e 65 77 20 3d 20 69 53 61 76 65 70 6f    nNew = iSavepo
141de 69 6e 74 20 2b 20 28 6f 70 3d 3d 53 41 56 45 50  int + (op==SAVEP
141df 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 29 3b 0a  OINT_ROLLBACK);.
141e0 20 20 20 20 66 6f 72 28 69 69 3d 6e 4e 65 77 3b      for(ii=nNew;
141e1 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76   ii<pPager->nSav
141e2 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20  epoint; ii++){. 
141e3 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76       sqlite3Bitv
141e4 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72  ecDestroy(pPager
141e5 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d  ->aSavepoint[ii]
141e6 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a  .pInSavepoint);.
141e7 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72      }.    pPager
141e8 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 6e  ->nSavepoint = n
141e9 4e 65 77 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  New;..    /* If 
141ea 74 68 69 73 20 69 73 20 61 20 72 6f 6c 6c 62 61  this is a rollba
141eb 63 6b 20 6f 70 65 72 61 74 69 6f 6e 2c 20 70 6c  ck operation, pl
141ec 61 79 62 61 63 6b 20 74 68 65 20 73 70 65 63 69  ayback the speci
141ed 66 69 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 0a  fied savepoint..
141ee 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69      ** If this i
141ef 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 2c 20 69  s a temp-file, i
141f0 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 68  t is possible th
141f1 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  at the journal f
141f2 69 6c 65 20 68 61 73 0a 20 20 20 20 2a 2a 20 6e  ile has.    ** n
141f3 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e  ot yet been open
141f4 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ed. In this case
141f5 20 74 68 65 72 65 20 68 61 76 65 20 62 65 65 6e   there have been
141f6 20 6e 6f 20 63 68 61 6e 67 65 73 20 74 6f 0a 20   no changes to. 
141f7 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61     ** the databa
141f8 73 65 20 66 69 6c 65 2c 20 73 6f 20 74 68 65 20  se file, so the 
141f9 70 6c 61 79 62 61 63 6b 20 6f 70 65 72 61 74 69  playback operati
141fa 6f 6e 20 63 61 6e 20 62 65 20 73 6b 69 70 70 65  on can be skippe
141fb 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  d..    */.    if
141fc 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f  ( op==SAVEPOINT_
141fd 52 4f 4c 4c 42 41 43 4b 20 26 26 20 69 73 4f 70  ROLLBACK && isOp
141fe 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
141ff 29 7b 0a 20 20 20 20 20 20 50 61 67 65 72 53 61  ){.      PagerSa
14200 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f  vepoint *pSavepo
14201 69 6e 74 20 3d 20 28 6e 4e 65 77 3d 3d 30 29 3f  int = (nNew==0)?
14202 30 3a 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65  0:&pPager->aSave
14203 70 6f 69 6e 74 5b 6e 4e 65 77 2d 31 5d 3b 0a 20  point[nNew-1];. 
14204 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 50       rc = pagerP
14205 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74  laybackSavepoint
14206 28 70 50 61 67 65 72 2c 20 70 53 61 76 65 70 6f  (pPager, pSavepo
14207 69 6e 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65  int);.      asse
14208 72 74 28 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f  rt(rc!=SQLITE_DO
14209 4e 45 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20  NE);.    }.  .  
1420a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
1420b 61 20 72 65 6c 65 61 73 65 20 6f 66 20 74 68 65  a release of the
1420c 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76 65 70   outermost savep
1420d 6f 69 6e 74 2c 20 74 72 75 6e 63 61 74 65 20 0a  oint, truncate .
1420e 20 20 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a      ** the sub-j
1420f 6f 75 72 6e 61 6c 20 74 6f 20 7a 65 72 6f 20 62  ournal to zero b
14210 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 2a 2f  ytes in size. */
14211 0a 20 20 20 20 69 66 28 20 6e 4e 65 77 3d 3d 30  .    if( nNew==0
14212 20 26 26 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e   && op==SAVEPOIN
14213 54 5f 52 45 4c 45 41 53 45 20 26 26 20 69 73 4f  T_RELEASE && isO
14214 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64  pen(pPager->sjfd
14215 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  ) ){.      asser
14216 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
14217 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   );.      rc = s
14218 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65  qlite3OsTruncate
14219 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 30  (pPager->sjfd, 0
1421a 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  );.      pPager-
1421b 3e 6e 53 75 62 52 65 63 20 3d 20 30 3b 0a 20 20  >nSubRec = 0;.  
1421c 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
1421d 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
1421e 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61  turn the full pa
1421f 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  thname of the da
14220 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a  tabase file..*/.
14221 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
14222 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
14223 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28  e3PagerFilename(
14224 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
14225 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
14226 3e 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f  >zFilename;.}../
14227 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
14228 56 46 53 20 73 74 72 75 63 74 75 72 65 20 66 6f  VFS structure fo
14229 72 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2f 0a  r the pager..*/.
1422a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
1422b 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 66 73  onst sqlite3_vfs
1422c 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 56 66   *sqlite3PagerVf
1422d 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  s(Pager *pPager)
1422e 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65  {.  return pPage
1422f 72 2d 3e 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a  r->pVfs;.}../*.*
14230 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 69 6c  * Return the fil
14231 65 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65  e handle for the
14232 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
14233 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74  ssociated.** wit
14234 68 20 74 68 65 20 70 61 67 65 72 2e 20 20 54 68  h the pager.  Th
14235 69 73 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20  is might return 
14236 4e 55 4c 4c 20 69 66 20 74 68 65 20 66 69 6c 65  NULL if the file
14237 20 68 61 73 0a 2a 2a 20 6e 6f 74 20 79 65 74 20   has.** not yet 
14238 62 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 2a 2f 0a  been opened..*/.
14239 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73  SQLITE_PRIVATE s
1423a 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c  qlite3_file *sql
1423b 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 50 61  ite3PagerFile(Pa
1423c 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
1423d 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 66  return pPager->f
1423e 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  d;.}../*.** Retu
1423f 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68  rn the full path
14240 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72  name of the jour
14241 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c  nal file..*/.SQL
14242 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
14243 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50  t char *sqlite3P
14244 61 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28  agerJournalname(
14245 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
14246 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
14247 3e 7a 4a 6f 75 72 6e 61 6c 3b 0a 7d 0a 0a 2f 2a  >zJournal;.}../*
14248 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
14249 69 66 20 66 73 79 6e 63 28 29 20 63 61 6c 6c 73  if fsync() calls
1424a 20 61 72 65 20 64 69 73 61 62 6c 65 64 20 66 6f   are disabled fo
1424b 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 20 52  r this pager.  R
1424c 65 74 75 72 6e 20 46 41 4c 53 45 0a 2a 2a 20 69  eturn FALSE.** i
1424d 66 20 66 73 79 6e 63 28 29 73 20 61 72 65 20 65  f fsync()s are e
1424e 78 65 63 75 74 65 64 20 6e 6f 72 6d 61 6c 6c 79  xecuted normally
1424f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
14250 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
14251 61 67 65 72 4e 6f 73 79 6e 63 28 50 61 67 65 72  agerNosync(Pager
14252 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74   *pPager){.  ret
14253 75 72 6e 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79  urn pPager->noSy
14254 6e 63 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51  nc;.}..#ifdef SQ
14255 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 2f  LITE_HAS_CODEC./
14256 2a 0a 2a 2a 20 53 65 74 20 6f 72 20 72 65 74 72  *.** Set or retr
14257 69 65 76 65 20 74 68 65 20 63 6f 64 65 63 20 66  ieve the codec f
14258 6f 72 20 74 68 69 73 20 70 61 67 65 72 0a 2a 2f  or this pager.*/
14259 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c  .static void sql
1425a 69 74 65 33 50 61 67 65 72 53 65 74 43 6f 64 65  ite3PagerSetCode
1425b 63 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  c(.  Pager *pPag
1425c 65 72 2c 0a 20 20 76 6f 69 64 20 2a 28 2a 78 43  er,.  void *(*xC
1425d 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76 6f 69 64  odec)(void*,void
1425e 2a 2c 50 67 6e 6f 2c 69 6e 74 29 2c 0a 20 20 76  *,Pgno,int),.  v
1425f 6f 69 64 20 28 2a 78 43 6f 64 65 63 53 69 7a 65  oid (*xCodecSize
14260 43 68 6e 67 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  Chng)(void*,int,
14261 69 6e 74 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78  int),.  void (*x
14262 43 6f 64 65 63 46 72 65 65 29 28 76 6f 69 64 2a  CodecFree)(void*
14263 29 2c 0a 20 20 76 6f 69 64 20 2a 70 43 6f 64 65  ),.  void *pCode
14264 63 0a 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65  c.){.  if( pPage
14265 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 20 29 20  r->xCodecFree ) 
14266 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 72  pPager->xCodecFr
14267 65 65 28 70 50 61 67 65 72 2d 3e 70 43 6f 64 65  ee(pPager->pCode
14268 63 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 78 43  c);.  pPager->xC
14269 6f 64 65 63 20 3d 20 78 43 6f 64 65 63 3b 0a 20  odec = xCodec;. 
1426a 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 53   pPager->xCodecS
1426b 69 7a 65 43 68 6e 67 20 3d 20 78 43 6f 64 65 63  izeChng = xCodec
1426c 53 69 7a 65 43 68 6e 67 3b 0a 20 20 70 50 61 67  SizeChng;.  pPag
1426d 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 20 3d  er->xCodecFree =
1426e 20 78 43 6f 64 65 63 46 72 65 65 3b 0a 20 20 70   xCodecFree;.  p
1426f 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 20 3d 20  Pager->pCodec = 
14270 70 43 6f 64 65 63 3b 0a 20 20 70 61 67 65 72 52  pCodec;.  pagerR
14271 65 70 6f 72 74 53 69 7a 65 28 70 50 61 67 65 72  eportSize(pPager
14272 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  );.}.static void
14273 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65   *sqlite3PagerGe
14274 74 43 6f 64 65 63 28 50 61 67 65 72 20 2a 70 50  tCodec(Pager *pP
14275 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  ager){.  return 
14276 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 3b 0a  pPager->pCodec;.
14277 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  }.#endif..#ifnde
14278 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
14279 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 4d  TOVACUUM./*.** M
1427a 6f 76 65 20 74 68 65 20 70 61 67 65 20 70 50 67  ove the page pPg
1427b 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 70 67 6e   to location pgn
1427c 6f 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a  o in the file..*
1427d 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20  *.** There must 
1427e 62 65 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 73  be no references
1427f 20 74 6f 20 74 68 65 20 70 61 67 65 20 70 72 65   to the page pre
14280 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65 64 20  viously located 
14281 61 74 0a 2a 2a 20 70 67 6e 6f 20 28 77 68 69 63  at.** pgno (whic
14282 68 20 77 65 20 63 61 6c 6c 20 70 50 67 4f 6c 64  h we call pPgOld
14283 29 20 74 68 6f 75 67 68 20 74 68 61 74 20 70 61  ) though that pa
14284 67 65 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f  ge is allowed to
14285 20 62 65 0a 2a 2a 20 69 6e 20 63 61 63 68 65 2e   be.** in cache.
14286 20 20 49 66 20 74 68 65 20 70 61 67 65 20 70 72    If the page pr
14287 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65 64  eviously located
14288 20 61 74 20 70 67 6e 6f 20 69 73 20 6e 6f 74 20   at pgno is not 
14289 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 74 68  already.** in th
1428a 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  e rollback journ
1428b 61 6c 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 75  al, it is not pu
1428c 74 20 74 68 65 72 65 20 62 79 20 62 79 20 74 68  t there by by th
1428d 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a  is routine..**.*
1428e 2a 20 52 65 66 65 72 65 6e 63 65 73 20 74 6f 20  * References to 
1428f 74 68 65 20 70 61 67 65 20 70 50 67 20 72 65 6d  the page pPg rem
14290 61 69 6e 20 76 61 6c 69 64 2e 20 55 70 64 61 74  ain valid. Updat
14291 69 6e 67 20 61 6e 79 0a 2a 2a 20 6d 65 74 61 2d  ing any.** meta-
14292 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20  data associated 
14293 77 69 74 68 20 70 50 67 20 28 69 2e 65 2e 20 64  with pPg (i.e. d
14294 61 74 61 20 73 74 6f 72 65 64 20 69 6e 20 74 68  ata stored in th
14295 65 20 6e 45 78 74 72 61 20 62 79 74 65 73 0a 2a  e nExtra bytes.*
14296 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e  * allocated alon
14297 67 20 77 69 74 68 20 74 68 65 20 70 61 67 65 29  g with the page)
14298 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73 69   is the responsi
14299 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61  bility of the ca
1429a 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72  ller..**.** A tr
1429b 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62  ansaction must b
1429c 65 20 61 63 74 69 76 65 20 77 68 65 6e 20 74 68  e active when th
1429d 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
1429e 6c 6c 65 64 2e 20 49 74 20 75 73 65 64 20 74 6f  lled. It used to
1429f 20 62 65 0a 2a 2a 20 72 65 71 75 69 72 65 64 20   be.** required 
142a0 74 68 61 74 20 61 20 73 74 61 74 65 6d 65 6e 74  that a statement
142a1 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73   transaction was
142a2 20 6e 6f 74 20 61 63 74 69 76 65 2c 20 62 75 74   not active, but
142a3 20 74 68 69 73 20 72 65 73 74 72 69 63 74 69 6f   this restrictio
142a4 6e 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 72 65  n.** has been re
142a5 6d 6f 76 65 64 20 28 43 52 45 41 54 45 20 49 4e  moved (CREATE IN
142a6 44 45 58 20 6e 65 65 64 73 20 74 6f 20 6d 6f 76  DEX needs to mov
142a7 65 20 61 20 70 61 67 65 20 77 68 65 6e 20 61 20  e a page when a 
142a8 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61  statement.** tra
142a9 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69  nsaction is acti
142aa 76 65 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ve)..**.** If th
142ab 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e  e fourth argumen
142ac 74 2c 20 69 73 43 6f 6d 6d 69 74 2c 20 69 73 20  t, isCommit, is 
142ad 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74  non-zero, then t
142ae 68 69 73 20 70 61 67 65 20 69 73 20 62 65 69 6e  his page is bein
142af 67 0a 2a 2a 20 6d 6f 76 65 64 20 61 73 20 70 61  g.** moved as pa
142b0 72 74 20 6f 66 20 61 20 64 61 74 61 62 61 73 65  rt of a database
142b1 20 72 65 6f 72 67 61 6e 69 7a 61 74 69 6f 6e 20   reorganization 
142b2 6a 75 73 74 20 62 65 66 6f 72 65 20 74 68 65 20  just before the 
142b3 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20  transaction .** 
142b4 69 73 20 62 65 69 6e 67 20 63 6f 6d 6d 69 74 74  is being committ
142b5 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ed. In this case
142b6 2c 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65  , it is guarante
142b7 65 64 20 74 68 61 74 20 74 68 65 20 64 61 74 61  ed that the data
142b8 62 61 73 65 20 70 61 67 65 20 0a 2a 2a 20 70 50  base page .** pP
142b9 67 20 72 65 66 65 72 73 20 74 6f 20 77 69 6c 6c  g refers to will
142ba 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e 20   not be written 
142bb 74 6f 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20  to again within 
142bc 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e  this transaction
142bd 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
142be 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 75 72 6e  ction may return
142bf 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f 72   SQLITE_NOMEM or
142c0 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64   an IO error cod
142c1 65 20 69 66 20 61 6e 20 65 72 72 6f 72 0a 2a 2a  e if an error.**
142c2 20 6f 63 63 75 72 73 2e 20 4f 74 68 65 72 77 69   occurs. Otherwi
142c3 73 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20 53  se, it returns S
142c4 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c  QLITE_OK..*/.SQL
142c5 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
142c6 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65  sqlite3PagerMove
142c7 70 61 67 65 28 50 61 67 65 72 20 2a 70 50 61 67  page(Pager *pPag
142c8 65 72 2c 20 44 62 50 61 67 65 20 2a 70 50 67 2c  er, DbPage *pPg,
142c9 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 69 6e 74 20   Pgno pgno, int 
142ca 69 73 43 6f 6d 6d 69 74 29 7b 0a 20 20 50 67 48  isCommit){.  PgH
142cb 64 72 20 2a 70 50 67 4f 6c 64 3b 20 20 20 20 20  dr *pPgOld;     
142cc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
142cd 20 70 61 67 65 20 62 65 69 6e 67 20 6f 76 65 72   page being over
142ce 77 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 50 67  written. */.  Pg
142cf 6e 6f 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20  no needSyncPgno 
142d0 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4f 6c  = 0;       /* Ol
142d1 64 20 76 61 6c 75 65 20 6f 66 20 70 50 67 2d 3e  d value of pPg->
142d2 70 67 6e 6f 2c 20 69 66 20 73 79 6e 63 20 69 73  pgno, if sync is
142d3 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69   required */.  i
142d4 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
142d5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
142d6 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
142d7 50 67 6e 6f 20 6f 72 69 67 50 67 6e 6f 3b 20 20  Pgno origPgno;  
142d8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
142d9 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67  The original pag
142da 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 20 61  e number */..  a
142db 73 73 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 66  ssert( pPg->nRef
142dc 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  >0 );..  /* If t
142dd 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 6d 6f  he page being mo
142de 76 65 64 20 69 73 20 64 69 72 74 79 20 61 6e 64  ved is dirty and
142df 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 73 61   has not been sa
142e0 76 65 64 20 62 79 20 74 68 65 20 6c 61 74 65 73  ved by the lates
142e1 74 0a 20 20 2a 2a 20 73 61 76 65 70 6f 69 6e 74  t.  ** savepoint
142e2 2c 20 74 68 65 6e 20 73 61 76 65 20 74 68 65 20  , then save the 
142e3 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73  current contents
142e4 20 6f 66 20 74 68 65 20 70 61 67 65 20 69 6e 74   of the page int
142e5 6f 20 74 68 65 20 0a 20 20 2a 2a 20 73 75 62 2d  o the .  ** sub-
142e6 6a 6f 75 72 6e 61 6c 20 6e 6f 77 2e 20 54 68 69  journal now. Thi
142e7 73 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f  s is required to
142e8 20 68 61 6e 64 6c 65 20 74 68 65 20 66 6f 6c 6c   handle the foll
142e9 6f 77 69 6e 67 20 73 63 65 6e 61 72 69 6f 3a 0a  owing scenario:.
142ea 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 47 49    **.  **   BEGI
142eb 4e 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75  N;.  **     <jou
142ec 72 6e 61 6c 20 70 61 67 65 20 58 2c 20 74 68 65  rnal page X, the
142ed 6e 20 6d 6f 64 69 66 79 20 69 74 20 69 6e 20 6d  n modify it in m
142ee 65 6d 6f 72 79 3e 0a 20 20 2a 2a 20 20 20 20 20  emory>.  **     
142ef 53 41 56 45 50 4f 49 4e 54 20 6f 6e 65 3b 0a 20  SAVEPOINT one;. 
142f0 20 2a 2a 20 20 20 20 20 20 20 3c 4d 6f 76 65 20   **       <Move 
142f1 70 61 67 65 20 58 20 74 6f 20 6c 6f 63 61 74 69  page X to locati
142f2 6f 6e 20 59 3e 0a 20 20 2a 2a 20 20 20 20 20 52  on Y>.  **     R
142f3 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 3b 0a  OLLBACK TO one;.
142f4 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 61 67    **.  ** If pag
142f5 65 20 58 20 77 65 72 65 20 6e 6f 74 20 77 72 69  e X were not wri
142f6 74 74 65 6e 20 74 6f 20 74 68 65 20 73 75 62 2d  tten to the sub-
142f7 6a 6f 75 72 6e 61 6c 20 68 65 72 65 2c 20 69 74  journal here, it
142f8 20 77 6f 75 6c 64 20 6e 6f 74 0a 20 20 2a 2a 20   would not.  ** 
142f9 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72  be possible to r
142fa 65 73 74 6f 72 65 20 69 74 73 20 63 6f 6e 74 65  estore its conte
142fb 6e 74 73 20 77 68 65 6e 20 74 68 65 20 22 52 4f  nts when the "RO
142fc 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 22 0a 20  LLBACK TO one". 
142fd 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 65   ** statement we
142fe 72 65 20 69 73 20 70 72 6f 63 65 73 73 65 64 2e  re is processed.
142ff 0a 20 20 2a 2a 0a 20 20 2a 2a 20 73 75 62 6a 6f  .  **.  ** subjo
14300 75 72 6e 61 6c 50 61 67 65 28 29 20 6d 61 79 20  urnalPage() may 
14301 6e 65 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65  need to allocate
14302 20 73 70 61 63 65 20 74 6f 20 73 74 6f 72 65 20   space to store 
14303 70 50 67 2d 3e 70 67 6e 6f 20 69 6e 74 6f 0a 20  pPg->pgno into. 
14304 20 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20   ** one or more 
14305 73 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63  savepoint bitvec
14306 73 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 72  s. This is the r
14307 65 61 73 6f 6e 20 74 68 69 73 20 66 75 6e 63 74  eason this funct
14308 69 6f 6e 0a 20 20 2a 2a 20 6d 61 79 20 72 65 74  ion.  ** may ret
14309 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
1430a 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 67  ..  */.  if( pPg
1430b 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49  ->flags&PGHDR_DI
1430c 52 54 59 20 0a 20 20 20 26 26 20 73 75 62 6a 52  RTY .   && subjR
1430d 65 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29  equiresPage(pPg)
1430e 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b  .   && SQLITE_OK
1430f 21 3d 28 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e  !=(rc = subjourn
14310 61 6c 50 61 67 65 28 70 50 67 29 29 0a 20 20 29  alPage(pPg)).  )
14311 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
14312 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41  .  }..  PAGERTRA
14313 43 45 28 28 22 4d 4f 56 45 20 25 64 20 70 61 67  CE(("MOVE %d pag
14314 65 20 25 64 20 28 6e 65 65 64 53 79 6e 63 3d 25  e %d (needSync=%
14315 64 29 20 6d 6f 76 65 73 20 74 6f 20 25 64 5c 6e  d) moves to %d\n
14316 22 2c 20 0a 20 20 20 20 20 20 50 41 47 45 52 49  ", .      PAGERI
14317 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e  D(pPager), pPg->
14318 70 67 6e 6f 2c 20 28 70 50 67 2d 3e 66 6c 61 67  pgno, (pPg->flag
14319 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  s&PGHDR_NEED_SYN
1431a 43 29 3f 31 3a 30 2c 20 70 67 6e 6f 29 29 3b 0a  C)?1:0, pgno));.
1431b 20 20 49 4f 54 52 41 43 45 28 28 22 4d 4f 56 45    IOTRACE(("MOVE
1431c 20 25 70 20 25 64 20 25 64 5c 6e 22 2c 20 70 50   %p %d %d\n", pP
1431d 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c  ager, pPg->pgno,
1431e 20 70 67 6e 6f 29 29 0a 0a 20 20 2f 2a 20 49 66   pgno))..  /* If
1431f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65   the journal nee
14320 64 73 20 74 6f 20 62 65 20 73 79 6e 63 28 29 65  ds to be sync()e
14321 64 20 62 65 66 6f 72 65 20 70 61 67 65 20 70 50  d before page pP
14322 67 2d 3e 70 67 6e 6f 20 63 61 6e 0a 20 20 2a 2a  g->pgno can.  **
14323 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2c 20   be written to, 
14324 73 74 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f 20  store pPg->pgno 
14325 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  in local variabl
14326 65 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2e 0a  e needSyncPgno..
14327 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65    **.  ** If the
14328 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20 69   isCommit flag i
14329 73 20 73 65 74 2c 20 74 68 65 72 65 20 69 73 20  s set, there is 
1432a 6e 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d  no need to remem
1432b 62 65 72 20 74 68 61 74 0a 20 20 2a 2a 20 74 68  ber that.  ** th
1432c 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73 20  e journal needs 
1432d 74 6f 20 62 65 20 73 79 6e 63 28 29 65 64 20 62  to be sync()ed b
1432e 65 66 6f 72 65 20 64 61 74 61 62 61 73 65 20 70  efore database p
1432f 61 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 0a 20  age pPg->pgno . 
14330 20 2a 2a 20 63 61 6e 20 62 65 20 77 72 69 74 74   ** can be writt
14331 65 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c 65  en to. The calle
14332 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 72  r has already pr
14333 6f 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20 77 72  omised not to wr
14334 69 74 65 20 74 6f 20 69 74 2e 0a 20 20 2a 2f 0a  ite to it..  */.
14335 20 20 69 66 28 20 28 70 50 67 2d 3e 66 6c 61 67    if( (pPg->flag
14336 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  s&PGHDR_NEED_SYN
14337 43 29 20 26 26 20 21 69 73 43 6f 6d 6d 69 74 20  C) && !isCommit 
14338 29 7b 0a 20 20 20 20 6e 65 65 64 53 79 6e 63 50  ){.    needSyncP
14339 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b  gno = pPg->pgno;
1433a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61 67  .    assert( pag
1433b 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20  eInJournal(pPg) 
1433c 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61  || pPg->pgno>pPa
1433d 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20  ger->dbOrigSize 
1433e 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
1433f 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  Pg->flags&PGHDR_
14340 44 49 52 54 59 20 29 3b 0a 20 20 20 20 61 73 73  DIRTY );.    ass
14341 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 65 65  ert( pPager->nee
14342 64 53 79 6e 63 20 29 3b 0a 20 20 7d 0a 0a 20 20  dSync );.  }..  
14343 2f 2a 20 49 66 20 74 68 65 20 63 61 63 68 65 20  /* If the cache 
14344 63 6f 6e 74 61 69 6e 73 20 61 20 70 61 67 65 20  contains a page 
14345 77 69 74 68 20 70 61 67 65 2d 6e 75 6d 62 65 72  with page-number
14346 20 70 67 6e 6f 2c 20 72 65 6d 6f 76 65 20 69 74   pgno, remove it
14347 0a 20 20 2a 2a 20 66 72 6f 6d 20 69 74 73 20 68  .  ** from its h
14348 61 73 68 20 63 68 61 69 6e 2e 20 41 6c 73 6f 2c  ash chain. Also,
14349 20 69 66 20 74 68 65 20 50 67 48 64 72 2e 6e 65   if the PgHdr.ne
1434a 65 64 53 79 6e 63 20 77 61 73 20 73 65 74 20 66  edSync was set f
1434b 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65 20 70 67  or .  ** page pg
1434c 6e 6f 20 62 65 66 6f 72 65 20 74 68 65 20 27 6d  no before the 'm
1434d 6f 76 65 27 20 6f 70 65 72 61 74 69 6f 6e 2c 20  ove' operation, 
1434e 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72  it needs to be r
1434f 65 74 61 69 6e 65 64 20 0a 20 20 2a 2a 20 66 6f  etained .  ** fo
14350 72 20 74 68 65 20 70 61 67 65 20 6d 6f 76 65 64  r the page moved
14351 20 74 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 70   there..  */.  p
14352 50 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47  Pg->flags &= ~PG
14353 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20  HDR_NEED_SYNC;. 
14354 20 70 50 67 4f 6c 64 20 3d 20 70 61 67 65 72 5f   pPgOld = pager_
14355 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70  lookup(pPager, p
14356 67 6e 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20  gno);.  assert( 
14357 21 70 50 67 4f 6c 64 20 7c 7c 20 70 50 67 4f 6c  !pPgOld || pPgOl
14358 64 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 20  d->nRef==1 );.  
14359 69 66 28 20 70 50 67 4f 6c 64 20 29 7b 0a 20 20  if( pPgOld ){.  
1435a 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20    pPg->flags |= 
1435b 28 70 50 67 4f 6c 64 2d 3e 66 6c 61 67 73 26 50  (pPgOld->flags&P
1435c 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3b  GHDR_NEED_SYNC);
1435d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63  .    sqlite3Pcac
1435e 68 65 44 72 6f 70 28 70 50 67 4f 6c 64 29 3b 0a  heDrop(pPgOld);.
1435f 20 20 7d 0a 0a 20 20 6f 72 69 67 50 67 6e 6f 20    }..  origPgno 
14360 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 73  = pPg->pgno;.  s
14361 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65  qlite3PcacheMove
14362 28 70 50 67 2c 20 70 67 6e 6f 29 3b 0a 20 20 73  (pPg, pgno);.  s
14363 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65  qlite3PcacheMake
14364 44 69 72 74 79 28 70 50 67 29 3b 0a 20 20 70 50  Dirty(pPg);.  pP
14365 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64  ager->dbModified
14366 20 3d 20 31 3b 0a 0a 20 20 69 66 28 20 6e 65 65   = 1;..  if( nee
14367 64 53 79 6e 63 50 67 6e 6f 20 29 7b 0a 20 20 20  dSyncPgno ){.   
14368 20 2f 2a 20 49 66 20 6e 65 65 64 53 79 6e 63 50   /* If needSyncP
14369 67 6e 6f 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  gno is non-zero,
1436a 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61   then the journa
1436b 6c 20 66 69 6c 65 20 6e 65 65 64 73 20 74 6f 20  l file needs to 
1436c 62 65 20 0a 20 20 20 20 2a 2a 20 73 79 6e 63 28  be .    ** sync(
1436d 29 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 64  )ed before any d
1436e 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 74  ata is written t
1436f 6f 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  o database file 
14370 70 61 67 65 20 6e 65 65 64 53 79 6e 63 50 67 6e  page needSyncPgn
14371 6f 2e 0a 20 20 20 20 2a 2a 20 43 75 72 72 65 6e  o..    ** Curren
14372 74 6c 79 2c 20 6e 6f 20 73 75 63 68 20 70 61 67  tly, no such pag
14373 65 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20  e exists in the 
14374 70 61 67 65 2d 63 61 63 68 65 20 61 6e 64 20 74  page-cache and t
14375 68 65 20 0a 20 20 20 20 2a 2a 20 22 69 73 20 6a  he .    ** "is j
14376 6f 75 72 6e 61 6c 65 64 22 20 62 69 74 76 65 63  ournaled" bitvec
14377 20 66 6c 61 67 20 68 61 73 20 62 65 65 6e 20 73   flag has been s
14378 65 74 2e 20 54 68 69 73 20 6e 65 65 64 73 20 74  et. This needs t
14379 6f 20 62 65 20 72 65 6d 65 64 69 65 64 20 62 79  o be remedied by
1437a 0a 20 20 20 20 2a 2a 20 6c 6f 61 64 69 6e 67 20  .    ** loading 
1437b 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68  the page into th
1437c 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 61 6e  e pager-cache an
1437d 64 20 73 65 74 74 69 6e 67 20 74 68 65 20 50 67  d setting the Pg
1437e 48 64 72 2e 6e 65 65 64 53 79 6e 63 20 0a 20 20  Hdr.needSync .  
1437f 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 20 20 2a    ** flag..    *
14380 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20  *.    ** If the 
14381 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61 64 20  attempt to load 
14382 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68  the page into th
14383 65 20 70 61 67 65 2d 63 61 63 68 65 20 66 61 69  e page-cache fai
14384 6c 73 2c 20 28 64 75 65 0a 20 20 20 20 2a 2a 20  ls, (due.    ** 
14385 74 6f 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72  to a malloc() or
14386 20 49 4f 20 66 61 69 6c 75 72 65 29 2c 20 63 6c   IO failure), cl
14387 65 61 72 20 74 68 65 20 62 69 74 20 69 6e 20 74  ear the bit in t
14388 68 65 20 70 49 6e 4a 6f 75 72 6e 61 6c 5b 5d 0a  he pInJournal[].
14389 20 20 20 20 2a 2a 20 61 72 72 61 79 2e 20 4f 74      ** array. Ot
1438a 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 20  herwise, if the 
1438b 70 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20 61  page is loaded a
1438c 6e 64 20 77 72 69 74 74 65 6e 20 61 67 61 69 6e  nd written again
1438d 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 69 73 20   in.    ** this 
1438e 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20  transaction, it 
1438f 6d 61 79 20 62 65 20 77 72 69 74 74 65 6e 20 74  may be written t
14390 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
14391 69 6c 65 20 62 65 66 6f 72 65 0a 20 20 20 20 2a  ile before.    *
14392 2a 20 69 74 20 69 73 20 73 79 6e 63 65 64 20 69  * it is synced i
14393 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  nto the journal 
14394 66 69 6c 65 2e 20 54 68 69 73 20 77 61 79 2c 20  file. This way, 
14395 69 74 20 6d 61 79 20 65 6e 64 20 75 70 20 69 6e  it may end up in
14396 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72  .    ** the jour
14397 6e 61 6c 20 66 69 6c 65 20 74 77 69 63 65 2c 20  nal file twice, 
14398 62 75 74 20 74 68 61 74 20 69 73 20 6e 6f 74 20  but that is not 
14399 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 20 20 2a  a problem..    *
1439a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 71 6c  *.    ** The sql
1439b 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 63  ite3PagerGet() c
1439c 61 6c 6c 20 6d 61 79 20 63 61 75 73 65 20 74 68  all may cause th
1439d 65 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 73 79 6e  e journal to syn
1439e 63 2e 20 53 6f 20 6d 61 6b 65 0a 20 20 20 20 2a  c. So make.    *
1439f 2a 20 73 75 72 65 20 74 68 65 20 50 61 67 65 72  * sure the Pager
143a0 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 20 69  .needSync flag i
143a1 73 20 73 65 74 20 74 6f 6f 2e 0a 20 20 20 20 2a  s set too..    *
143a2 2f 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67  /.    PgHdr *pPg
143a3 48 64 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28  Hdr;.    assert(
143a4 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
143a5 63 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  c );.    rc = sq
143a6 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 50  lite3PagerGet(pP
143a7 61 67 65 72 2c 20 6e 65 65 64 53 79 6e 63 50 67  ager, needSyncPg
143a8 6e 6f 2c 20 26 70 50 67 48 64 72 29 3b 0a 20 20  no, &pPgHdr);.  
143a9 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
143aa 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28  _OK ){.      if(
143ab 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 3c 3d 70   needSyncPgno<=p
143ac 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a  Pager->dbOrigSiz
143ad 65 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  e ){.        ass
143ae 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 54 6d  ert( pPager->pTm
143af 70 53 70 61 63 65 21 3d 30 20 29 3b 0a 20 20 20  pSpace!=0 );.   
143b0 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76       sqlite3Bitv
143b1 65 63 43 6c 65 61 72 28 70 50 61 67 65 72 2d 3e  ecClear(pPager->
143b2 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 6e 65 65 64  pInJournal, need
143b3 53 79 6e 63 50 67 6e 6f 2c 20 70 50 61 67 65 72  SyncPgno, pPager
143b4 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20  ->pTmpSpace);.  
143b5 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
143b6 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
143b7 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
143b8 63 20 3d 20 31 3b 0a 20 20 20 20 61 73 73 65 72  c = 1;.    asser
143b9 74 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  t( pPager->noSyn
143ba 63 3d 3d 30 20 26 26 20 21 4d 45 4d 44 42 20 29  c==0 && !MEMDB )
143bb 3b 0a 20 20 20 20 70 50 67 48 64 72 2d 3e 66 6c  ;.    pPgHdr->fl
143bc 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45  ags |= PGHDR_NEE
143bd 44 5f 53 59 4e 43 3b 0a 20 20 20 20 73 71 6c 69  D_SYNC;.    sqli
143be 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72  te3PcacheMakeDir
143bf 74 79 28 70 50 67 48 64 72 29 3b 0a 20 20 20 20  ty(pPgHdr);.    
143c0 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
143c1 66 28 70 50 67 48 64 72 29 3b 0a 20 20 7d 0a 0a  f(pPgHdr);.  }..
143c2 20 20 2f 2a 0a 20 20 2a 2a 20 46 6f 72 20 61 6e    /*.  ** For an
143c3 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62   in-memory datab
143c4 61 73 65 2c 20 6d 61 6b 65 20 73 75 72 65 20 74  ase, make sure t
143c5 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65  he original page
143c6 20 63 6f 6e 74 69 6e 75 65 73 0a 20 20 2a 2a 20   continues.  ** 
143c7 74 6f 20 65 78 69 73 74 2c 20 69 6e 20 63 61 73  to exist, in cas
143c8 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  e the transactio
143c9 6e 20 6e 65 65 64 73 20 74 6f 20 72 6f 6c 6c 20  n needs to roll 
143ca 62 61 63 6b 2e 20 20 57 65 20 61 6c 6c 6f 63 61  back.  We alloca
143cb 74 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65  te.  ** the page
143cc 20 6e 6f 77 2c 20 69 6e 73 74 65 61 64 20 6f 66   now, instead of
143cd 20 61 74 20 72 6f 6c 6c 62 61 63 6b 2c 20 62 65   at rollback, be
143ce 63 61 75 73 65 20 77 65 20 63 61 6e 20 62 65 74  cause we can bet
143cf 74 65 72 20 64 65 61 6c 0a 20 20 2a 2a 20 77 69  ter deal.  ** wi
143d0 74 68 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d  th an out-of-mem
143d1 6f 72 79 20 65 72 72 6f 72 20 6e 6f 77 2e 20 20  ory error now.  
143d2 54 69 63 6b 65 74 20 23 33 37 36 31 2e 0a 20 20  Ticket #3761..  
143d3 2a 2f 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29  */.  if( MEMDB )
143d4 7b 0a 20 20 20 20 44 62 50 61 67 65 20 2a 70 4e  {.    DbPage *pN
143d5 65 77 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  ew;.    rc = sql
143d6 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65  ite3PagerAcquire
143d7 28 70 50 61 67 65 72 2c 20 6f 72 69 67 50 67 6e  (pPager, origPgn
143d8 6f 2c 20 26 70 4e 65 77 2c 20 31 29 3b 0a 20 20  o, &pNew, 1);.  
143d9 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
143da 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
143db 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65 28 70  ite3PcacheMove(p
143dc 50 67 2c 20 6f 72 69 67 50 67 6e 6f 29 3b 0a 20  Pg, origPgno);. 
143dd 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
143de 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
143df 33 50 61 67 65 72 55 6e 72 65 66 28 70 4e 65 77  3PagerUnref(pNew
143e0 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  );.  }..  return
143e1 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65   SQLITE_OK;.}.#e
143e2 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ndif../*.** Retu
143e3 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
143e4 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65  the data for the
143e5 20 73 70 65 63 69 66 69 65 64 20 70 61 67 65 2e   specified page.
143e6 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
143e7 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
143e8 50 61 67 65 72 47 65 74 44 61 74 61 28 44 62 50  PagerGetData(DbP
143e9 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 61 73 73  age *pPg){.  ass
143ea 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30  ert( pPg->nRef>0
143eb 20 7c 7c 20 70 50 67 2d 3e 70 50 61 67 65 72 2d   || pPg->pPager-
143ec 3e 6d 65 6d 44 62 20 29 3b 0a 20 20 72 65 74 75  >memDb );.  retu
143ed 72 6e 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 7d  rn pPg->pData;.}
143ee 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
143ef 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
143f0 50 61 67 65 72 2e 6e 45 78 74 72 61 20 62 79 74  Pager.nExtra byt
143f1 65 73 20 6f 66 20 22 65 78 74 72 61 22 20 73 70  es of "extra" sp
143f2 61 63 65 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65  ace .** allocate
143f3 64 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65  d along with the
143f4 20 73 70 65 63 69 66 69 65 64 20 70 61 67 65 2e   specified page.
143f5 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
143f6 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
143f7 50 61 67 65 72 47 65 74 45 78 74 72 61 28 44 62  PagerGetExtra(Db
143f8 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 72 65  Page *pPg){.  re
143f9 74 75 72 6e 20 70 50 67 2d 3e 70 45 78 74 72 61  turn pPg->pExtra
143fa 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73  ;.}../*.** Get/s
143fb 65 74 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d  et the locking-m
143fc 6f 64 65 20 66 6f 72 20 74 68 69 73 20 70 61 67  ode for this pag
143fd 65 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d  er. Parameter eM
143fe 6f 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 0a  ode must be one.
143ff 2a 2a 20 6f 66 20 50 41 47 45 52 5f 4c 4f 43 4b  ** of PAGER_LOCK
14400 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 2c 20 50  INGMODE_QUERY, P
14401 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
14402 5f 4e 4f 52 4d 41 4c 20 6f 72 20 0a 2a 2a 20 50  _NORMAL or .** P
14403 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
14404 5f 45 58 43 4c 55 53 49 56 45 2e 20 49 66 20 74  _EXCLUSIVE. If t
14405 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  he parameter is 
14406 6e 6f 74 20 5f 51 55 45 52 59 2c 20 74 68 65 6e  not _QUERY, then
14407 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d  .** the locking-
14408 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 74  mode is set to t
14409 68 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 69  he value specifi
1440a 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  ed..**.** The re
1440b 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20  turned value is 
1440c 65 69 74 68 65 72 20 50 41 47 45 52 5f 4c 4f 43  either PAGER_LOC
1440d 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20  KINGMODE_NORMAL 
1440e 6f 72 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b  or.** PAGER_LOCK
1440f 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56  INGMODE_EXCLUSIV
14410 45 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68  E, indicating th
14411 65 20 63 75 72 72 65 6e 74 20 28 70 6f 73 73 69  e current (possi
14412 62 6c 79 20 75 70 64 61 74 65 64 29 0a 2a 2a 20  bly updated).** 
14413 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 2e 0a 2a 2f  locking-mode..*/
14414 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14415 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
14416 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67 65  LockingMode(Page
14417 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65  r *pPager, int e
14418 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65 72 74 28  Mode){.  assert(
14419 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f   eMode==PAGER_LO
1441a 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 0a  CKINGMODE_QUERY.
1441b 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65              || e
1441c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b  Mode==PAGER_LOCK
1441d 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 0a 20  INGMODE_NORMAL. 
1441e 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d             || eM
1441f 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49  ode==PAGER_LOCKI
14420 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45  NGMODE_EXCLUSIVE
14421 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41   );.  assert( PA
14422 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
14423 51 55 45 52 59 3c 30 20 29 3b 0a 20 20 61 73 73  QUERY<0 );.  ass
14424 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49  ert( PAGER_LOCKI
14425 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3e 3d 30  NGMODE_NORMAL>=0
14426 20 26 26 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e   && PAGER_LOCKIN
14427 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3e  GMODE_EXCLUSIVE>
14428 3d 30 20 29 3b 0a 20 20 69 66 28 20 65 4d 6f 64  =0 );.  if( eMod
14429 65 3e 3d 30 20 26 26 20 21 70 50 61 67 65 72 2d  e>=0 && !pPager-
1442a 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20  >tempFile ){.   
1442b 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69   pPager->exclusi
1442c 76 65 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d 6f  veMode = (u8)eMo
1442d 64 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  de;.  }.  return
1442e 20 28 69 6e 74 29 70 50 61 67 65 72 2d 3e 65 78   (int)pPager->ex
1442f 63 6c 75 73 69 76 65 4d 6f 64 65 3b 0a 7d 0a 0a  clusiveMode;.}..
14430 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 74 20 74 68  /*.** Get/set th
14431 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 66  e journal-mode f
14432 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 50  or this pager. P
14433 61 72 61 6d 65 74 65 72 20 65 4d 6f 64 65 20 6d  arameter eMode m
14434 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 3a 0a 2a  ust be one of:.*
14435 2a 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f  *.**    PAGER_JO
14436 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 0a  URNALMODE_QUERY.
14437 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52  **    PAGER_JOUR
14438 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 2a  NALMODE_DELETE.*
14439 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e  *    PAGER_JOURN
1443a 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a  ALMODE_TRUNCATE.
1443b 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52  **    PAGER_JOUR
1443c 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a  NALMODE_PERSIST.
1443d 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52  **    PAGER_JOUR
1443e 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a 2a 2a 20 20  NALMODE_OFF.**  
1443f 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d    PAGER_JOURNALM
14440 4f 44 45 5f 4d 45 4d 4f 52 59 0a 2a 2a 0a 2a 2a  ODE_MEMORY.**.**
14441 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65   If the paramete
14442 72 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c  r is not _QUERY,
14443 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61   then the journa
14444 6c 5f 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f  l_mode is set to
14445 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 73 70   the.** value sp
14446 65 63 69 66 69 65 64 20 69 66 20 74 68 65 20 63  ecified if the c
14447 68 61 6e 67 65 20 69 73 20 61 6c 6c 6f 77 65 64  hange is allowed
14448 2e 20 20 54 68 65 20 63 68 61 6e 67 65 20 69 73  .  The change is
14449 20 64 69 73 61 6c 6c 6f 77 65 64 0a 2a 2a 20 66   disallowed.** f
1444a 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  or the following
1444b 20 72 65 61 73 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20   reasons:.**.** 
1444c 20 20 2a 20 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72    *  An in-memor
1444d 79 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 6f  y database can o
1444e 6e 6c 79 20 68 61 76 65 20 69 74 73 20 6a 6f 75  nly have its jou
1444f 72 6e 61 6c 5f 6d 6f 64 65 20 73 65 74 20 74 6f  rnal_mode set to
14450 20 5f 4f 46 46 0a 2a 2a 20 20 20 20 20 20 6f 72   _OFF.**      or
14451 20 5f 4d 45 4d 4f 52 59 2e 0a 2a 2a 0a 2a 2a 20   _MEMORY..**.** 
14452 20 20 2a 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c    *  The journal
14453 20 6d 6f 64 65 20 6d 61 79 20 6e 6f 74 20 62 65   mode may not be
14454 20 63 68 61 6e 67 65 64 20 77 68 69 6c 65 20 61   changed while a
14455 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
14456 61 63 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  active..**.** Th
14457 65 20 72 65 74 75 72 6e 65 64 20 69 6e 64 69 63  e returned indic
14458 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ate the current 
14459 28 70 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65  (possibly update
1445a 64 29 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 2e  d) journal-mode.
1445b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1445c 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
1445d 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50  gerJournalMode(P
1445e 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e  ager *pPager, in
1445f 74 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65  t eMode){.  asse
14460 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52  rt( eMode==PAGER
14461 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45  _JOURNALMODE_QUE
14462 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c  RY.            |
14463 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  | eMode==PAGER_J
14464 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54  OURNALMODE_DELET
14465 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c  E.            ||
14466 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f   eMode==PAGER_JO
14467 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41  URNALMODE_TRUNCA
14468 54 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c  TE.            |
14469 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  | eMode==PAGER_J
1446a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49  OURNALMODE_PERSI
1446b 53 54 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c  ST.            |
1446c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  | eMode==PAGER_J
1446d 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a  OURNALMODE_OFF .
1446e 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65              || e
1446f 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
14470 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29  NALMODE_MEMORY )
14471 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45  ;.  assert( PAGE
14472 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55  R_JOURNALMODE_QU
14473 45 52 59 3c 30 20 29 3b 0a 20 20 69 66 28 20 65  ERY<0 );.  if( e
14474 4d 6f 64 65 3e 3d 30 0a 20 20 20 26 26 20 28 21  Mode>=0.   && (!
14475 4d 45 4d 44 42 20 7c 7c 20 65 4d 6f 64 65 3d 3d  MEMDB || eMode==
14476 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
14477 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20 20 20  E_MEMORY .      
14478 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65          || eMode
14479 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
1447a 4f 44 45 5f 4f 46 46 29 0a 20 20 20 26 26 20 21  ODE_OFF).   && !
1447b 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69  pPager->dbModifi
1447c 65 64 0a 20 20 20 26 26 20 28 21 69 73 4f 70 65  ed.   && (!isOpe
1447d 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c  n(pPager->jfd) |
1447e 7c 20 30 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75  | 0==pPager->jou
1447f 72 6e 61 6c 4f 66 66 29 0a 20 20 29 7b 0a 20 20  rnalOff).  ){.  
14480 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61    if( isOpen(pPa
14481 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20  ger->jfd) ){.   
14482 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73     sqlite3OsClos
14483 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a  e(pPager->jfd);.
14484 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72      }.    pPager
14485 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20  ->journalMode = 
14486 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20 7d 0a 20  (u8)eMode;.  }. 
14487 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70 50 61   return (int)pPa
14488 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
14489 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73  ;.}../*.** Get/s
1448a 65 74 20 74 68 65 20 73 69 7a 65 2d 6c 69 6d 69  et the size-limi
1448b 74 20 75 73 65 64 20 66 6f 72 20 70 65 72 73 69  t used for persi
1448c 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69  stent journal fi
1448d 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69  les..**.** Setti
1448e 6e 67 20 74 68 65 20 73 69 7a 65 20 6c 69 6d 69  ng the size limi
1448f 74 20 74 6f 20 2d 31 20 6d 65 61 6e 73 20 6e 6f  t to -1 means no
14490 20 6c 69 6d 69 74 20 69 73 20 65 6e 66 6f 72 63   limit is enforc
14491 65 64 2e 0a 2a 2a 20 41 6e 20 61 74 74 65 6d 70  ed..** An attemp
14492 74 20 74 6f 20 73 65 74 20 61 20 6c 69 6d 69 74  t to set a limit
14493 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 2d 31   smaller than -1
14494 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a   is a no-op..*/.
14495 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
14496 36 34 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a  64 sqlite3PagerJ
14497 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28  ournalSizeLimit(
14498 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
14499 36 34 20 69 4c 69 6d 69 74 29 7b 0a 20 20 69 66  64 iLimit){.  if
1449a 28 20 69 4c 69 6d 69 74 3e 3d 2d 31 20 29 7b 0a  ( iLimit>=-1 ){.
1449b 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
1449c 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 69  nalSizeLimit = i
1449d 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 65 74  Limit;.  }.  ret
1449e 75 72 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  urn pPager->jour
1449f 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a 7d 0a  nalSizeLimit;.}.
144a0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
144a1 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70  pointer to the p
144a2 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 76  Pager->pBackup v
144a3 61 72 69 61 62 6c 65 2e 20 54 68 65 20 62 61 63  ariable. The bac
144a4 6b 75 70 20 6d 6f 64 75 6c 65 0a 2a 2a 20 69 6e  kup module.** in
144a5 20 62 61 63 6b 75 70 2e 63 20 6d 61 69 6e 74 61   backup.c mainta
144a6 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ins the content 
144a7 6f 66 20 74 68 69 73 20 76 61 72 69 61 62 6c 65  of this variable
144a8 2e 20 54 68 69 73 20 6d 6f 64 75 6c 65 0a 2a 2a  . This module.**
144a9 20 75 73 65 73 20 69 74 20 6f 70 61 71 75 65 6c   uses it opaquel
144aa 79 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74  y as an argument
144ab 20 74 6f 20 73 71 6c 69 74 65 33 42 61 63 6b 75   to sqlite3Backu
144ac 70 52 65 73 74 61 72 74 28 29 20 61 6e 64 0a 2a  pRestart() and.*
144ad 2a 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55  * sqlite3BackupU
144ae 70 64 61 74 65 28 29 20 6f 6e 6c 79 2e 0a 2a 2f  pdate() only..*/
144af 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
144b0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
144b1 2a 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 63  *sqlite3PagerBac
144b2 6b 75 70 50 74 72 28 50 61 67 65 72 20 2a 70 50  kupPtr(Pager *pP
144b3 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  ager){.  return 
144b4 26 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70  &pPager->pBackup
144b5 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ;.}..#endif /* S
144b6 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49  QLITE_OMIT_DISKI
144b7 4f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  O */../*********
144b8 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67  ***** End of pag
144b9 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.c ***********
144ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
144bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
144bc 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
144bd 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
144be 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a   btmutex.c *****
144bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
144c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
144c1 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
144c2 20 41 75 67 75 73 74 20 32 37 0a 2a 2a 0a 2a 2a   August 27.**.**
144c3 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
144c4 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
144c5 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
144c6 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
144c7 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
144c8 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
144c9 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
144ca 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
144cb 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
144cc 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
144cd 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
144ce 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
144cf 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
144d0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
144d1 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
144d2 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
144d3 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
144d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
144d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
144d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
144d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
144d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
144d9 24 49 64 3a 20 62 74 6d 75 74 65 78 2e 63 2c 76  $Id: btmutex.c,v
144da 20 31 2e 31 37 20 32 30 30 39 2f 30 37 2f 32 30   1.17 2009/07/20
144db 20 31 32 3a 33 33 3a 33 33 20 64 72 68 20 45 78   12:33:33 drh Ex
144dc 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  p $.**.** This f
144dd 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64  ile contains cod
144de 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d  e used to implem
144df 65 6e 74 20 6d 75 74 65 78 65 73 20 6f 6e 20 42  ent mutexes on B
144e0 74 72 65 65 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a  tree objects..**
144e1 20 54 68 69 73 20 63 6f 64 65 20 72 65 61 6c 6c   This code reall
144e2 79 20 62 65 6c 6f 6e 67 73 20 69 6e 20 62 74 72  y belongs in btr
144e3 65 65 2e 63 2e 20 20 42 75 74 20 62 74 72 65 65  ee.c.  But btree
144e4 2e 63 20 69 73 20 67 65 74 74 69 6e 67 20 74 6f  .c is getting to
144e5 6f 0a 2a 2a 20 62 69 67 20 61 6e 64 20 77 65 20  o.** big and we 
144e6 77 61 6e 74 20 74 6f 20 62 72 65 61 6b 20 69 74  want to break it
144e7 20 64 6f 77 6e 20 73 6f 6d 65 2e 20 20 54 68 69   down some.  Thi
144e8 73 20 70 61 63 6b 61 67 65 64 20 73 65 65 6d 65  s packaged seeme
144e9 64 20 6c 69 6b 65 0a 2a 2a 20 61 20 67 6f 6f 64  d like.** a good
144ea 20 62 72 65 61 6b 6f 75 74 2e 0a 2a 2f 0a 2f 2a   breakout..*/./*
144eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
144ec 63 6c 75 64 65 20 62 74 72 65 65 49 6e 74 2e 68  clude btreeInt.h
144ed 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
144ee 66 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a  f btmutex.c ****
144ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
144f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
144f1 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 49 6e  gin file btreeIn
144f2 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
144f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
144f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
144f5 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20 36  .** 2004 April 6
144f6 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
144f7 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
144f8 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
144f9 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
144fa 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
144fb 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
144fc 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
144fd 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
144fe 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
144ff 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
14500 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
14501 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
14502 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
14503 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
14504 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
14505 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
14506 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
14507 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
14508 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14509 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1450a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1450b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1450c 2a 2a 20 24 49 64 3a 20 62 74 72 65 65 49 6e 74  ** $Id: btreeInt
1450d 2e 68 2c 76 20 31 2e 35 32 20 32 30 30 39 2f 30  .h,v 1.52 2009/0
1450e 37 2f 31 35 20 31 37 3a 32 35 3a 34 36 20 64 72  7/15 17:25:46 dr
1450f 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68  h Exp $.**.** Th
14510 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e  is file implemen
14511 74 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28 64  ts a external (d
14512 69 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61 62  isk-based) datab
14513 61 73 65 20 75 73 69 6e 67 20 42 54 72 65 65 73  ase using BTrees
14514 2e 0a 2a 2a 20 46 6f 72 20 61 20 64 65 74 61 69  ..** For a detai
14515 6c 65 64 20 64 69 73 63 75 73 73 69 6f 6e 20 6f  led discussion o
14516 66 20 42 54 72 65 65 73 2c 20 72 65 66 65 72 20  f BTrees, refer 
14517 74 6f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 6f 6e  to.**.**     Don
14518 61 6c 64 20 45 2e 20 4b 6e 75 74 68 2c 20 54 48  ald E. Knuth, TH
14519 45 20 41 52 54 20 4f 46 20 43 4f 4d 50 55 54 45  E ART OF COMPUTE
1451a 52 20 50 52 4f 47 52 41 4d 4d 49 4e 47 2c 20 56  R PROGRAMMING, V
1451b 6f 6c 75 6d 65 20 33 3a 0a 2a 2a 20 20 20 20 20  olume 3:.**     
1451c 22 53 6f 72 74 69 6e 67 20 41 6e 64 20 53 65 61  "Sorting And Sea
1451d 72 63 68 69 6e 67 22 2c 20 70 61 67 65 73 20 34  rching", pages 4
1451e 37 33 2d 34 38 30 2e 20 41 64 64 69 73 6f 6e 2d  73-480. Addison-
1451f 57 65 73 6c 65 79 0a 2a 2a 20 20 20 20 20 50 75  Wesley.**     Pu
14520 62 6c 69 73 68 69 6e 67 20 43 6f 6d 70 61 6e 79  blishing Company
14521 2c 20 52 65 61 64 69 6e 67 2c 20 4d 61 73 73 61  , Reading, Massa
14522 63 68 75 73 65 74 74 73 2e 0a 2a 2a 0a 2a 2a 20  chusetts..**.** 
14523 54 68 65 20 62 61 73 69 63 20 69 64 65 61 20 69  The basic idea i
14524 73 20 74 68 61 74 20 65 61 63 68 20 70 61 67 65  s that each page
14525 20 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f 6e   of the file con
14526 74 61 69 6e 73 20 4e 20 64 61 74 61 62 61 73 65  tains N database
14527 0a 2a 2a 20 65 6e 74 72 69 65 73 20 61 6e 64 20  .** entries and 
14528 4e 2b 31 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  N+1 pointers to 
14529 73 75 62 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20  subpages..**.** 
1452a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
1452b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1452c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1452d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1452e 2d 2d 0a 2a 2a 20 20 20 7c 20 20 50 74 72 28 30  --.**   |  Ptr(0
1452f 29 20 7c 20 4b 65 79 28 30 29 20 7c 20 50 74 72  ) | Key(0) | Ptr
14530 28 31 29 20 7c 20 4b 65 79 28 31 29 20 7c 20 2e  (1) | Key(1) | .
14531 2e 2e 20 7c 20 4b 65 79 28 4e 2d 31 29 20 7c 20  .. | Key(N-1) | 
14532 50 74 72 28 4e 29 20 7c 0a 2a 2a 20 20 20 2d 2d  Ptr(N) |.**   --
14533 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14534 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14535 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14536 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
14537 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20  *.** All of the 
14538 6b 65 79 73 20 6f 6e 20 74 68 65 20 70 61 67 65  keys on the page
14539 20 74 68 61 74 20 50 74 72 28 30 29 20 70 6f 69   that Ptr(0) poi
1453a 6e 74 73 20 74 6f 20 68 61 76 65 20 76 61 6c 75  nts to have valu
1453b 65 73 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20  es less.** than 
1453c 4b 65 79 28 30 29 2e 20 20 41 6c 6c 20 6f 66 20  Key(0).  All of 
1453d 74 68 65 20 6b 65 79 73 20 6f 6e 20 70 61 67 65  the keys on page
1453e 20 50 74 72 28 31 29 20 61 6e 64 20 69 74 73 20   Ptr(1) and its 
1453f 73 75 62 70 61 67 65 73 20 68 61 76 65 0a 2a 2a  subpages have.**
14540 20 76 61 6c 75 65 73 20 67 72 65 61 74 65 72 20   values greater 
14541 74 68 61 6e 20 4b 65 79 28 30 29 20 61 6e 64 20  than Key(0) and 
14542 6c 65 73 73 20 74 68 61 6e 20 4b 65 79 28 31 29  less than Key(1)
14543 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65  .  All of the ke
14544 79 73 0a 2a 2a 20 6f 6e 20 50 74 72 28 4e 29 20  ys.** on Ptr(N) 
14545 61 6e 64 20 69 74 73 20 73 75 62 70 61 67 65 73  and its subpages
14546 20 68 61 76 65 20 76 61 6c 75 65 73 20 67 72 65   have values gre
14547 61 74 65 72 20 74 68 61 6e 20 4b 65 79 28 4e 2d  ater than Key(N-
14548 31 29 2e 20 20 41 6e 64 0a 2a 2a 20 73 6f 20 66  1).  And.** so f
14549 6f 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 46 69 6e 64  orth..**.** Find
1454a 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 72  ing a particular
1454b 20 6b 65 79 20 72 65 71 75 69 72 65 73 20 72 65   key requires re
1454c 61 64 69 6e 67 20 4f 28 6c 6f 67 28 4d 29 29 20  ading O(log(M)) 
1454d 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 0a  pages from the .
1454e 2a 2a 20 64 69 73 6b 20 77 68 65 72 65 20 4d 20  ** disk where M 
1454f 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
14550 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20   entries in the 
14551 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74  tree..**.** In t
14552 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  his implementati
14553 6f 6e 2c 20 61 20 73 69 6e 67 6c 65 20 66 69 6c  on, a single fil
14554 65 20 63 61 6e 20 68 6f 6c 64 20 6f 6e 65 20 6f  e can hold one o
14555 72 20 6d 6f 72 65 20 73 65 70 61 72 61 74 65 20  r more separate 
14556 0a 2a 2a 20 42 54 72 65 65 73 2e 20 20 45 61 63  .** BTrees.  Eac
14557 68 20 42 54 72 65 65 20 69 73 20 69 64 65 6e 74  h BTree is ident
14558 69 66 69 65 64 20 62 79 20 74 68 65 20 69 6e 64  ified by the ind
14559 65 78 20 6f 66 20 69 74 73 20 72 6f 6f 74 20 70  ex of its root p
1455a 61 67 65 2e 20 20 54 68 65 0a 2a 2a 20 6b 65 79  age.  The.** key
1455b 20 61 6e 64 20 64 61 74 61 20 66 6f 72 20 61 6e   and data for an
1455c 79 20 65 6e 74 72 79 20 61 72 65 20 63 6f 6d 62  y entry are comb
1455d 69 6e 65 64 20 74 6f 20 66 6f 72 6d 20 74 68 65  ined to form the
1455e 20 22 70 61 79 6c 6f 61 64 22 2e 20 20 41 0a 2a   "payload".  A.*
1455f 2a 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f  * fixed amount o
14560 66 20 70 61 79 6c 6f 61 64 20 63 61 6e 20 62 65  f payload can be
14561 20 63 61 72 72 69 65 64 20 64 69 72 65 63 74 6c   carried directl
14562 79 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  y on the databas
14563 65 0a 2a 2a 20 70 61 67 65 2e 20 20 49 66 20 74  e.** page.  If t
14564 68 65 20 70 61 79 6c 6f 61 64 20 69 73 20 6c 61  he payload is la
14565 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 70 72  rger than the pr
14566 65 73 65 74 20 61 6d 6f 75 6e 74 20 74 68 65 6e  eset amount then
14567 20 73 75 72 70 6c 75 73 0a 2a 2a 20 62 79 74 65   surplus.** byte
14568 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20  s are stored on 
14569 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20  overflow pages. 
1456a 20 54 68 65 20 70 61 79 6c 6f 61 64 20 66 6f 72   The payload for
1456b 20 61 6e 20 65 6e 74 72 79 0a 2a 2a 20 61 6e 64   an entry.** and
1456c 20 74 68 65 20 70 72 65 63 65 64 69 6e 67 20 70   the preceding p
1456d 6f 69 6e 74 65 72 20 61 72 65 20 63 6f 6d 62 69  ointer are combi
1456e 6e 65 64 20 74 6f 20 66 6f 72 6d 20 61 20 22 43  ned to form a "C
1456f 65 6c 6c 22 2e 20 20 45 61 63 68 20 0a 2a 2a 20  ell".  Each .** 
14570 70 61 67 65 20 68 61 73 20 61 20 73 6d 61 6c 6c  page has a small
14571 20 68 65 61 64 65 72 20 77 68 69 63 68 20 63 6f   header which co
14572 6e 74 61 69 6e 73 20 74 68 65 20 50 74 72 28 4e  ntains the Ptr(N
14573 29 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 6f 74  ) pointer and ot
14574 68 65 72 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  her.** informati
14575 6f 6e 20 73 75 63 68 20 61 73 20 74 68 65 20 73  on such as the s
14576 69 7a 65 20 6f 66 20 6b 65 79 20 61 6e 64 20 64  ize of key and d
14577 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 46 4f 52 4d 41  ata..**.** FORMA
14578 54 20 44 45 54 41 49 4c 53 0a 2a 2a 0a 2a 2a 20  T DETAILS.**.** 
14579 54 68 65 20 66 69 6c 65 20 69 73 20 64 69 76 69  The file is divi
1457a 64 65 64 20 69 6e 74 6f 20 70 61 67 65 73 2e 20  ded into pages. 
1457b 20 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20   The first page 
1457c 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20 31  is called page 1
1457d 2c 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20  ,.** the second 
1457e 69 73 20 70 61 67 65 20 32 2c 20 61 6e 64 20 73  is page 2, and s
1457f 6f 20 66 6f 72 74 68 2e 20 20 41 20 70 61 67 65  o forth.  A page
14580 20 6e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 20   number of zero 
14581 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 22 6e 6f  indicates.** "no
14582 20 73 75 63 68 20 70 61 67 65 22 2e 20 20 54 68   such page".  Th
14583 65 20 70 61 67 65 20 73 69 7a 65 20 63 61 6e 20  e page size can 
14584 62 65 20 61 6e 79 74 68 69 6e 67 20 62 65 74 77  be anything betw
14585 65 65 6e 20 35 31 32 20 61 6e 64 20 36 35 35 33  een 512 and 6553
14586 36 2e 0a 2a 2a 20 45 61 63 68 20 70 61 67 65 20  6..** Each page 
14587 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 20  can be either a 
14588 62 74 72 65 65 20 70 61 67 65 2c 20 61 20 66 72  btree page, a fr
14589 65 65 6c 69 73 74 20 70 61 67 65 20 6f 72 20 61  eelist page or a
1458a 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 61  n overflow.** pa
1458b 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  ge..**.** The fi
1458c 72 73 74 20 70 61 67 65 20 69 73 20 61 6c 77 61  rst page is alwa
1458d 79 73 20 61 20 62 74 72 65 65 20 70 61 67 65 2e  ys a btree page.
1458e 20 20 54 68 65 20 66 69 72 73 74 20 31 30 30 20    The first 100 
1458f 62 79 74 65 73 20 6f 66 20 74 68 65 20 66 69 72  bytes of the fir
14590 73 74 0a 2a 2a 20 70 61 67 65 20 63 6f 6e 74 61  st.** page conta
14591 69 6e 20 61 20 73 70 65 63 69 61 6c 20 68 65 61  in a special hea
14592 64 65 72 20 28 74 68 65 20 22 66 69 6c 65 20 68  der (the "file h
14593 65 61 64 65 72 22 29 20 74 68 61 74 20 64 65 73  eader") that des
14594 63 72 69 62 65 73 20 74 68 65 20 66 69 6c 65 2e  cribes the file.
14595 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 6f  .** The format o
14596 66 20 74 68 65 20 66 69 6c 65 20 68 65 61 64 65  f the file heade
14597 72 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  r is as follows:
14598 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 54 20  .**.**   OFFSET 
14599 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 49    SIZE    DESCRI
1459a 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 20  PTION.**      0 
1459b 20 20 20 20 20 31 36 20 20 20 20 20 48 65 61 64       16     Head
1459c 65 72 20 73 74 72 69 6e 67 3a 20 22 53 51 4c 69  er string: "SQLi
1459d 74 65 20 66 6f 72 6d 61 74 20 33 5c 30 30 30 22  te format 3\000"
1459e 0a 2a 2a 20 20 20 20 20 31 36 20 20 20 20 20 20  .**     16      
1459f 20 32 20 20 20 20 20 50 61 67 65 20 73 69 7a 65   2     Page size
145a0 20 69 6e 20 62 79 74 65 73 2e 20 20 0a 2a 2a 20   in bytes.  .** 
145a1 20 20 20 20 31 38 20 20 20 20 20 20 20 31 20 20      18       1  
145a2 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 77     File format w
145a3 72 69 74 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20  rite version.** 
145a4 20 20 20 20 31 39 20 20 20 20 20 20 20 31 20 20      19       1  
145a5 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 72     File format r
145a6 65 61 64 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20  ead version.**  
145a7 20 20 20 32 30 20 20 20 20 20 20 20 31 20 20 20     20       1   
145a8 20 20 42 79 74 65 73 20 6f 66 20 75 6e 75 73 65    Bytes of unuse
145a9 64 20 73 70 61 63 65 20 61 74 20 74 68 65 20 65  d space at the e
145aa 6e 64 20 6f 66 20 65 61 63 68 20 70 61 67 65 0a  nd of each page.
145ab 2a 2a 20 20 20 20 20 32 31 20 20 20 20 20 20 20  **     21       
145ac 31 20 20 20 20 20 4d 61 78 20 65 6d 62 65 64 64  1     Max embedd
145ad 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74  ed payload fract
145ae 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 32 20 20 20  ion.**     22   
145af 20 20 20 20 31 20 20 20 20 20 4d 69 6e 20 65 6d      1     Min em
145b0 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66  bedded payload f
145b1 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32  raction.**     2
145b2 33 20 20 20 20 20 20 20 31 20 20 20 20 20 4d 69  3       1     Mi
145b3 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20 66  n leaf payload f
145b4 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32  raction.**     2
145b5 34 20 20 20 20 20 20 20 34 20 20 20 20 20 46 69  4       4     Fi
145b6 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65  le change counte
145b7 72 0a 2a 2a 20 20 20 20 20 32 38 20 20 20 20 20  r.**     28     
145b8 20 20 34 20 20 20 20 20 52 65 73 65 72 76 65 64    4     Reserved
145b9 20 66 6f 72 20 66 75 74 75 72 65 20 75 73 65 0a   for future use.
145ba 2a 2a 20 20 20 20 20 33 32 20 20 20 20 20 20 20  **     32       
145bb 34 20 20 20 20 20 46 69 72 73 74 20 66 72 65 65  4     First free
145bc 6c 69 73 74 20 70 61 67 65 0a 2a 2a 20 20 20 20  list page.**    
145bd 20 33 36 20 20 20 20 20 20 20 34 20 20 20 20 20   36       4     
145be 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c 69  Number of freeli
145bf 73 74 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  st pages in the 
145c0 66 69 6c 65 0a 2a 2a 20 20 20 20 20 34 30 20 20  file.**     40  
145c1 20 20 20 20 36 30 20 20 20 20 20 31 35 20 34 2d      60     15 4-
145c2 62 79 74 65 20 6d 65 74 61 20 76 61 6c 75 65 73  byte meta values
145c3 20 70 61 73 73 65 64 20 74 6f 20 68 69 67 68 65   passed to highe
145c4 72 20 6c 61 79 65 72 73 0a 2a 2a 0a 2a 2a 20 20  r layers.**.**  
145c5 20 20 20 34 30 20 20 20 20 20 20 20 34 20 20 20     40       4   
145c6 20 20 53 63 68 65 6d 61 20 63 6f 6f 6b 69 65 0a    Schema cookie.
145c7 2a 2a 20 20 20 20 20 34 34 20 20 20 20 20 20 20  **     44       
145c8 34 20 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 61  4     File forma
145c9 74 20 6f 66 20 73 63 68 65 6d 61 20 6c 61 79 65  t of schema laye
145ca 72 0a 2a 2a 20 20 20 20 20 34 38 20 20 20 20 20  r.**     48     
145cb 20 20 34 20 20 20 20 20 53 69 7a 65 20 6f 66 20    4     Size of 
145cc 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 20 20  page cache.**   
145cd 20 20 35 32 20 20 20 20 20 20 20 34 20 20 20 20    52       4    
145ce 20 4c 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61   Largest root-pa
145cf 67 65 20 28 61 75 74 6f 2f 69 6e 63 72 5f 76 61  ge (auto/incr_va
145d0 63 75 75 6d 29 0a 2a 2a 20 20 20 20 20 35 36 20  cuum).**     56 
145d1 20 20 20 20 20 20 34 20 20 20 20 20 31 3d 55 54        4     1=UT
145d2 46 2d 38 20 32 3d 55 54 46 31 36 6c 65 20 33 3d  F-8 2=UTF16le 3=
145d3 55 54 46 31 36 62 65 0a 2a 2a 20 20 20 20 20 36  UTF16be.**     6
145d4 30 20 20 20 20 20 20 20 34 20 20 20 20 20 55 73  0       4     Us
145d5 65 72 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 20  er version.**   
145d6 20 20 36 34 20 20 20 20 20 20 20 34 20 20 20 20    64       4    
145d7 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63   Incremental vac
145d8 75 75 6d 20 6d 6f 64 65 0a 2a 2a 20 20 20 20 20  uum mode.**     
145d9 36 38 20 20 20 20 20 20 20 34 20 20 20 20 20 75  68       4     u
145da 6e 75 73 65 64 0a 2a 2a 20 20 20 20 20 37 32 20  nused.**     72 
145db 20 20 20 20 20 20 34 20 20 20 20 20 75 6e 75 73        4     unus
145dc 65 64 0a 2a 2a 20 20 20 20 20 37 36 20 20 20 20  ed.**     76    
145dd 20 20 20 34 20 20 20 20 20 75 6e 75 73 65 64 0a     4     unused.
145de 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65  **.** All of the
145df 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20   integer values 
145e0 61 72 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 28  are big-endian (
145e1 6d 6f 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74  most significant
145e2 20 62 79 74 65 20 66 69 72 73 74 29 2e 0a 2a 2a   byte first)..**
145e3 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 63 68 61  .** The file cha
145e4 6e 67 65 20 63 6f 75 6e 74 65 72 20 69 73 20 69  nge counter is i
145e5 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e 20  ncremented when 
145e6 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
145e7 63 68 61 6e 67 65 64 0a 2a 2a 20 54 68 69 73 20  changed.** This 
145e8 63 6f 75 6e 74 65 72 20 61 6c 6c 6f 77 73 20 6f  counter allows o
145e9 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 74  ther processes t
145ea 6f 20 6b 6e 6f 77 20 77 68 65 6e 20 74 68 65 20  o know when the 
145eb 66 69 6c 65 20 68 61 73 20 63 68 61 6e 67 65 64  file has changed
145ec 0a 2a 2a 20 61 6e 64 20 74 68 75 73 20 77 68 65  .** and thus whe
145ed 6e 20 74 68 65 79 20 6e 65 65 64 20 74 6f 20 66  n they need to f
145ee 6c 75 73 68 20 74 68 65 69 72 20 63 61 63 68 65  lush their cache
145ef 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 20  ..**.** The max 
145f0 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64  embedded payload
145f1 20 66 72 61 63 74 69 6f 6e 20 69 73 20 74 68 65   fraction is the
145f2 20 61 6d 6f 75 6e 74 20 6f 66 20 74 68 65 20 74   amount of the t
145f3 6f 74 61 6c 20 75 73 61 62 6c 65 0a 2a 2a 20 73  otal usable.** s
145f4 70 61 63 65 20 69 6e 20 61 20 70 61 67 65 20 74  pace in a page t
145f5 68 61 74 20 63 61 6e 20 62 65 20 63 6f 6e 73 75  hat can be consu
145f6 6d 65 64 20 62 79 20 61 20 73 69 6e 67 6c 65 20  med by a single 
145f7 63 65 6c 6c 20 66 6f 72 20 73 74 61 6e 64 61 72  cell for standar
145f8 64 0a 2a 2a 20 42 2d 74 72 65 65 20 28 6e 6f 6e  d.** B-tree (non
145f9 2d 4c 45 41 46 44 41 54 41 29 20 74 61 62 6c 65  -LEAFDATA) table
145fa 73 2e 20 20 41 20 76 61 6c 75 65 20 6f 66 20 32  s.  A value of 2
145fb 35 35 20 6d 65 61 6e 73 20 31 30 30 25 2e 20 20  55 means 100%.  
145fc 54 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 69  The default.** i
145fd 73 20 74 6f 20 6c 69 6d 69 74 20 74 68 65 20 6d  s to limit the m
145fe 61 78 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65  aximum cell size
145ff 20 73 6f 20 74 68 61 74 20 61 74 20 6c 65 61 73   so that at leas
14600 74 20 34 20 63 65 6c 6c 73 20 77 69 6c 6c 20 66  t 4 cells will f
14601 69 74 0a 2a 2a 20 6f 6e 20 6f 6e 65 20 70 61 67  it.** on one pag
14602 65 2e 20 20 54 68 75 73 20 74 68 65 20 64 65 66  e.  Thus the def
14603 61 75 6c 74 20 6d 61 78 20 65 6d 62 65 64 64 65  ault max embedde
14604 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69  d payload fracti
14605 6f 6e 20 69 73 20 36 34 2e 0a 2a 2a 0a 2a 2a 20  on is 64..**.** 
14606 49 66 20 74 68 65 20 70 61 79 6c 6f 61 64 20 66  If the payload f
14607 6f 72 20 61 20 63 65 6c 6c 20 69 73 20 6c 61 72  or a cell is lar
14608 67 65 72 20 74 68 61 6e 20 74 68 65 20 6d 61 78  ger than the max
14609 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 6e 20 65   payload, then e
1460a 78 74 72 61 0a 2a 2a 20 70 61 79 6c 6f 61 64 20  xtra.** payload 
1460b 69 73 20 73 70 69 6c 6c 65 64 20 74 6f 20 6f 76  is spilled to ov
1460c 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 20 4f  erflow pages.  O
1460d 6e 63 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20  nce an overflow 
1460e 70 61 67 65 20 69 73 20 61 6c 6c 6f 63 61 74 65  page is allocate
1460f 64 2c 0a 2a 2a 20 61 73 20 6d 61 6e 79 20 62 79  d,.** as many by
14610 74 65 73 20 61 73 20 70 6f 73 73 69 62 6c 65 20  tes as possible 
14611 61 72 65 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74  are moved into t
14612 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  he overflow page
14613 73 20 77 69 74 68 6f 75 74 20 6c 65 74 74 69 6e  s without lettin
14614 67 0a 2a 2a 20 74 68 65 20 63 65 6c 6c 20 73 69  g.** the cell si
14615 7a 65 20 64 72 6f 70 20 62 65 6c 6f 77 20 74 68  ze drop below th
14616 65 20 6d 69 6e 20 65 6d 62 65 64 64 65 64 20 70  e min embedded p
14617 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 2e  ayload fraction.
14618 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 69 6e 20 6c  .**.** The min l
14619 65 61 66 20 70 61 79 6c 6f 61 64 20 66 72 61 63  eaf payload frac
1461a 74 69 6f 6e 20 69 73 20 6c 69 6b 65 20 74 68 65  tion is like the
1461b 20 6d 69 6e 20 65 6d 62 65 64 64 65 64 20 70 61   min embedded pa
1461c 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a  yload fraction.*
1461d 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74  * except that it
1461e 20 61 70 70 6c 69 65 73 20 74 6f 20 6c 65 61 66   applies to leaf
1461f 20 6e 6f 64 65 73 20 69 6e 20 61 20 4c 45 41 46   nodes in a LEAF
14620 44 41 54 41 20 74 72 65 65 2e 20 20 54 68 65 20  DATA tree.  The 
14621 6d 61 78 69 6d 75 6d 0a 2a 2a 20 70 61 79 6c 6f  maximum.** paylo
14622 61 64 20 66 72 61 63 74 69 6f 6e 20 66 6f 72 20  ad fraction for 
14623 61 20 4c 45 41 46 44 41 54 41 20 74 72 65 65 20  a LEAFDATA tree 
14624 69 73 20 61 6c 77 61 79 73 20 31 30 30 25 20 28  is always 100% (
14625 6f 72 20 32 35 35 29 20 61 6e 64 20 69 74 0a 2a  or 255) and it.*
14626 2a 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20  * not specified 
14627 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e 0a 2a  in the header..*
14628 2a 0a 2a 2a 20 45 61 63 68 20 62 74 72 65 65 20  *.** Each btree 
14629 70 61 67 65 73 20 69 73 20 64 69 76 69 64 65 64  pages is divided
1462a 20 69 6e 74 6f 20 74 68 72 65 65 20 73 65 63 74   into three sect
1462b 69 6f 6e 73 3a 20 20 54 68 65 20 68 65 61 64 65  ions:  The heade
1462c 72 2c 20 74 68 65 0a 2a 2a 20 63 65 6c 6c 20 70  r, the.** cell p
1462d 6f 69 6e 74 65 72 20 61 72 72 61 79 2c 20 61 6e  ointer array, an
1462e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65  d the cell conte
1462f 6e 74 20 61 72 65 61 2e 20 20 50 61 67 65 20 31  nt area.  Page 1
14630 20 61 6c 73 6f 20 68 61 73 20 61 20 31 30 30 2d   also has a 100-
14631 62 79 74 65 0a 2a 2a 20 66 69 6c 65 20 68 65 61  byte.** file hea
14632 64 65 72 20 74 68 61 74 20 6f 63 63 75 72 73 20  der that occurs 
14633 62 65 66 6f 72 65 20 74 68 65 20 70 61 67 65 20  before the page 
14634 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20  header..**.**   
14635 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     |------------
14636 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20  ----|.**      | 
14637 66 69 6c 65 20 68 65 61 64 65 72 20 20 20 20 7c  file header    |
14638 20 20 20 31 30 30 20 62 79 74 65 73 2e 20 20 50     100 bytes.  P
14639 61 67 65 20 31 20 6f 6e 6c 79 2e 0a 2a 2a 20 20  age 1 only..**  
1463a 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d      |-----------
1463b 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c  -----|.**      |
1463c 20 70 61 67 65 20 68 65 61 64 65 72 20 20 20 20   page header    
1463d 7c 20 20 20 38 20 62 79 74 65 73 20 66 6f 72 20  |   8 bytes for 
1463e 6c 65 61 76 65 73 2e 20 20 31 32 20 62 79 74 65  leaves.  12 byte
1463f 73 20 66 6f 72 20 69 6e 74 65 72 69 6f 72 20 6e  s for interior n
14640 6f 64 65 73 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d  odes.**      |--
14641 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a  --------------|.
14642 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c 6c 20 70  **      | cell p
14643 6f 69 6e 74 65 72 20 20 20 7c 20 20 20 7c 20 20  ointer   |   |  
14644 32 20 62 79 74 65 73 20 70 65 72 20 63 65 6c 6c  2 bytes per cell
14645 2e 20 20 53 6f 72 74 65 64 20 6f 72 64 65 72 2e  .  Sorted order.
14646 0a 2a 2a 20 20 20 20 20 20 7c 20 61 72 72 61 79  .**      | array
14647 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 7c 20            |   | 
14648 20 47 72 6f 77 73 20 64 6f 77 6e 77 61 72 64 0a   Grows downward.
14649 2a 2a 20 20 20 20 20 20 7c 20 20 20 20 20 20 20  **      |       
1464a 20 20 20 20 20 20 20 20 20 7c 20 20 20 76 0a 2a           |   v.*
1464b 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d  *      |--------
1464c 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20  --------|.**    
1464d 20 20 7c 20 75 6e 61 6c 6c 6f 63 61 74 65 64 20    | unallocated 
1464e 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 73     |.**      | s
1464f 70 61 63 65 20 20 20 20 20 20 20 20 20 20 7c 0a  pace          |.
14650 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d  **      |-------
14651 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 20 20 20 5e 20 20  ---------|   ^  
14652 47 72 6f 77 73 20 75 70 77 61 72 64 73 0a 2a 2a  Grows upwards.**
14653 20 20 20 20 20 20 7c 20 63 65 6c 6c 20 63 6f 6e        | cell con
14654 74 65 6e 74 20 20 20 7c 20 20 20 7c 20 20 41 72  tent   |   |  Ar
14655 62 69 74 72 61 72 79 20 6f 72 64 65 72 20 69 6e  bitrary order in
14656 74 65 72 73 70 65 72 73 65 64 20 77 69 74 68 20  terspersed with 
14657 66 72 65 65 62 6c 6f 63 6b 73 2e 0a 2a 2a 20 20  freeblocks..**  
14658 20 20 20 20 7c 20 61 72 65 61 20 20 20 20 20 20      | area      
14659 20 20 20 20 20 7c 20 20 20 7c 20 20 61 6e 64 20       |   |  and 
1465a 66 72 65 65 20 73 70 61 63 65 20 66 72 61 67 6d  free space fragm
1465b 65 6e 74 73 2e 0a 2a 2a 20 20 20 20 20 20 7c 2d  ents..**      |-
1465c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c  ---------------|
1465d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20  .**.** The page 
1465e 68 65 61 64 65 72 73 20 6c 6f 6f 6b 73 20 6c 69  headers looks li
1465f 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  ke this:.**.**  
14660 20 4f 46 46 53 45 54 20 20 20 53 49 5a 45 20 20   OFFSET   SIZE  
14661 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a     DESCRIPTION.*
14662 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 20 31  *      0       1
14663 20 20 20 20 20 20 46 6c 61 67 73 2e 20 31 3a 20        Flags. 1: 
14664 69 6e 74 6b 65 79 2c 20 32 3a 20 7a 65 72 6f 64  intkey, 2: zerod
14665 61 74 61 2c 20 34 3a 20 6c 65 61 66 64 61 74 61  ata, 4: leafdata
14666 2c 20 38 3a 20 6c 65 61 66 0a 2a 2a 20 20 20 20  , 8: leaf.**    
14667 20 20 31 20 20 20 20 20 20 20 32 20 20 20 20 20    1       2     
14668 20 62 79 74 65 20 6f 66 66 73 65 74 20 74 6f 20   byte offset to 
14669 74 68 65 20 66 69 72 73 74 20 66 72 65 65 62 6c  the first freebl
1466a 6f 63 6b 0a 2a 2a 20 20 20 20 20 20 33 20 20 20  ock.**      3   
1466b 20 20 20 20 32 20 20 20 20 20 20 6e 75 6d 62 65      2      numbe
1466c 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68  r of cells on th
1466d 69 73 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20  is page.**      
1466e 35 20 20 20 20 20 20 20 32 20 20 20 20 20 20 66  5       2      f
1466f 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65  irst byte of the
14670 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72   cell content ar
14671 65 61 0a 2a 2a 20 20 20 20 20 20 37 20 20 20 20  ea.**      7    
14672 20 20 20 31 20 20 20 20 20 20 6e 75 6d 62 65 72     1      number
14673 20 6f 66 20 66 72 61 67 6d 65 6e 74 65 64 20 66   of fragmented f
14674 72 65 65 20 62 79 74 65 73 0a 2a 2a 20 20 20 20  ree bytes.**    
14675 20 20 38 20 20 20 20 20 20 20 34 20 20 20 20 20    8       4     
14676 20 52 69 67 68 74 20 63 68 69 6c 64 20 28 74 68   Right child (th
14677 65 20 50 74 72 28 4e 29 20 76 61 6c 75 65 29 2e  e Ptr(N) value).
14678 20 20 4f 6d 69 74 74 65 64 20 6f 6e 20 6c 65 61    Omitted on lea
14679 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ves..**.** The f
1467a 6c 61 67 73 20 64 65 66 69 6e 65 20 74 68 65 20  lags define the 
1467b 66 6f 72 6d 61 74 20 6f 66 20 74 68 69 73 20 62  format of this b
1467c 74 72 65 65 20 70 61 67 65 2e 20 20 54 68 65 20  tree page.  The 
1467d 6c 65 61 66 20 66 6c 61 67 20 6d 65 61 6e 73 20  leaf flag means 
1467e 74 68 61 74 0a 2a 2a 20 74 68 69 73 20 70 61 67  that.** this pag
1467f 65 20 68 61 73 20 6e 6f 20 63 68 69 6c 64 72 65  e has no childre
14680 6e 2e 20 20 54 68 65 20 7a 65 72 6f 64 61 74 61  n.  The zerodata
14681 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 61 74   flag means that
14682 20 74 68 69 73 20 70 61 67 65 20 63 61 72 72 69   this page carri
14683 65 73 0a 2a 2a 20 6f 6e 6c 79 20 6b 65 79 73 20  es.** only keys 
14684 61 6e 64 20 6e 6f 20 64 61 74 61 2e 20 20 54 68  and no data.  Th
14685 65 20 69 6e 74 6b 65 79 20 66 6c 61 67 20 6d 65  e intkey flag me
14686 61 6e 73 20 74 68 61 74 20 74 68 65 20 6b 65 79  ans that the key
14687 20 69 73 20 61 20 69 6e 74 65 67 65 72 0a 2a 2a   is a integer.**
14688 20 77 68 69 63 68 20 69 73 20 73 74 6f 72 65 64   which is stored
14689 20 69 6e 20 74 68 65 20 6b 65 79 20 73 69 7a 65   in the key size
1468a 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 63 65   entry of the ce
1468b 6c 6c 20 68 65 61 64 65 72 20 72 61 74 68 65 72  ll header rather
1468c 20 74 68 61 6e 20 69 6e 0a 2a 2a 20 74 68 65 20   than in.** the 
1468d 70 61 79 6c 6f 61 64 20 61 72 65 61 2e 0a 2a 2a  payload area..**
1468e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 70 6f 69  .** The cell poi
1468f 6e 74 65 72 20 61 72 72 61 79 20 62 65 67 69 6e  nter array begin
14690 73 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 62  s on the first b
14691 79 74 65 20 61 66 74 65 72 20 74 68 65 20 70 61  yte after the pa
14692 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a 20 54 68  ge header..** Th
14693 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61  e cell pointer a
14694 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20 7a 65  rray contains ze
14695 72 6f 20 6f 72 20 6d 6f 72 65 20 32 2d 62 79 74  ro or more 2-byt
14696 65 20 6e 75 6d 62 65 72 73 20 77 68 69 63 68 20  e numbers which 
14697 61 72 65 0a 2a 2a 20 6f 66 66 73 65 74 73 20 66  are.** offsets f
14698 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  rom the beginnin
14699 67 20 6f 66 20 74 68 65 20 70 61 67 65 20 74 6f  g of the page to
1469a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
1469b 74 20 69 6e 20 74 68 65 20 63 65 6c 6c 0a 2a 2a  t in the cell.**
1469c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20   content area.  
1469d 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72  The cell pointer
1469e 73 20 6f 63 63 75 72 20 69 6e 20 73 6f 72 74 65  s occur in sorte
1469f 64 20 6f 72 64 65 72 2e 20 20 54 68 65 20 73 79  d order.  The sy
146a0 73 74 65 6d 20 73 74 72 69 76 65 73 0a 2a 2a 20  stem strives.** 
146a1 74 6f 20 6b 65 65 70 20 66 72 65 65 20 73 70 61  to keep free spa
146a2 63 65 20 61 66 74 65 72 20 74 68 65 20 6c 61 73  ce after the las
146a3 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 73  t cell pointer s
146a4 6f 20 74 68 61 74 20 6e 65 77 20 63 65 6c 6c 73  o that new cells
146a5 20 63 61 6e 0a 2a 2a 20 62 65 20 65 61 73 69 6c   can.** be easil
146a6 79 20 61 64 64 65 64 20 77 69 74 68 6f 75 74 20  y added without 
146a7 68 61 76 69 6e 67 20 74 6f 20 64 65 66 72 61 67  having to defrag
146a8 6d 65 6e 74 20 74 68 65 20 70 61 67 65 2e 0a 2a  ment the page..*
146a9 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74 65 6e  *.** Cell conten
146aa 74 20 69 73 20 73 74 6f 72 65 64 20 61 74 20 74  t is stored at t
146ab 68 65 20 76 65 72 79 20 65 6e 64 20 6f 66 20 74  he very end of t
146ac 68 65 20 70 61 67 65 20 61 6e 64 20 67 72 6f 77  he page and grow
146ad 73 20 74 6f 77 61 72 64 20 74 68 65 0a 2a 2a 20  s toward the.** 
146ae 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65  beginning of the
146af 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 55 6e 75   page..**.** Unu
146b0 73 65 64 20 73 70 61 63 65 20 77 69 74 68 69 6e  sed space within
146b1 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
146b2 74 20 61 72 65 61 20 69 73 20 63 6f 6c 6c 65 63  t area is collec
146b3 74 65 64 20 69 6e 74 6f 20 61 20 6c 69 6e 6b 65  ted into a linke
146b4 64 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 66 72 65  d list of.** fre
146b5 65 62 6c 6f 63 6b 73 2e 20 20 45 61 63 68 20 66  eblocks.  Each f
146b6 72 65 65 62 6c 6f 63 6b 20 69 73 20 61 74 20 6c  reeblock is at l
146b7 65 61 73 74 20 34 20 62 79 74 65 73 20 69 6e 20  east 4 bytes in 
146b8 73 69 7a 65 2e 20 20 54 68 65 20 62 79 74 65 20  size.  The byte 
146b9 6f 66 66 73 65 74 0a 2a 2a 20 74 6f 20 74 68 65  offset.** to the
146ba 20 66 69 72 73 74 20 66 72 65 65 62 6c 6f 63 6b   first freeblock
146bb 20 69 73 20 67 69 76 65 6e 20 69 6e 20 74 68 65   is given in the
146bc 20 68 65 61 64 65 72 2e 20 20 46 72 65 65 62 6c   header.  Freebl
146bd 6f 63 6b 73 20 6f 63 63 75 72 20 69 6e 0a 2a 2a  ocks occur in.**
146be 20 69 6e 63 72 65 61 73 69 6e 67 20 6f 72 64 65   increasing orde
146bf 72 2e 20 20 42 65 63 61 75 73 65 20 61 20 66 72  r.  Because a fr
146c0 65 65 62 6c 6f 63 6b 20 6d 75 73 74 20 62 65 20  eeblock must be 
146c1 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73  at least 4 bytes
146c2 20 69 6e 20 73 69 7a 65 2c 0a 2a 2a 20 61 6e 79   in size,.** any
146c3 20 67 72 6f 75 70 20 6f 66 20 33 20 6f 72 20 66   group of 3 or f
146c4 65 77 65 72 20 75 6e 75 73 65 64 20 62 79 74 65  ewer unused byte
146c5 73 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f  s in the cell co
146c6 6e 74 65 6e 74 20 61 72 65 61 20 63 61 6e 6e 6f  ntent area canno
146c7 74 0a 2a 2a 20 65 78 69 73 74 20 6f 6e 20 74 68  t.** exist on th
146c8 65 20 66 72 65 65 62 6c 6f 63 6b 20 63 68 61 69  e freeblock chai
146c9 6e 2e 20 20 41 20 67 72 6f 75 70 20 6f 66 20 33  n.  A group of 3
146ca 20 6f 72 20 66 65 77 65 72 20 66 72 65 65 20 62   or fewer free b
146cb 79 74 65 73 20 69 73 20 63 61 6c 6c 65 64 0a 2a  ytes is called.*
146cc 2a 20 61 20 66 72 61 67 6d 65 6e 74 2e 20 20 54  * a fragment.  T
146cd 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
146ce 6f 66 20 62 79 74 65 73 20 69 6e 20 61 6c 6c 20  of bytes in all 
146cf 66 72 61 67 6d 65 6e 74 73 20 69 73 20 72 65 63  fragments is rec
146d0 6f 72 64 65 64 2e 0a 2a 2a 20 69 6e 20 74 68 65  orded..** in the
146d1 20 70 61 67 65 20 68 65 61 64 65 72 20 61 74 20   page header at 
146d2 6f 66 66 73 65 74 20 37 2e 0a 2a 2a 0a 2a 2a 20  offset 7..**.** 
146d3 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52     SIZE    DESCR
146d4 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 32  IPTION.**      2
146d5 20 20 20 20 20 42 79 74 65 20 6f 66 66 73 65 74       Byte offset
146d6 20 6f 66 20 74 68 65 20 6e 65 78 74 20 66 72 65   of the next fre
146d7 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 20 20 32  eblock.**      2
146d8 20 20 20 20 20 42 79 74 65 73 20 69 6e 20 74 68       Bytes in th
146d9 69 73 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 0a  is freeblock.**.
146da 2a 2a 20 43 65 6c 6c 73 20 61 72 65 20 6f 66 20  ** Cells are of 
146db 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 2e  variable length.
146dc 20 20 43 65 6c 6c 73 20 61 72 65 20 73 74 6f 72    Cells are stor
146dd 65 64 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 63  ed in the cell c
146de 6f 6e 74 65 6e 74 20 61 72 65 61 20 61 74 0a 2a  ontent area at.*
146df 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  * the end of the
146e0 20 70 61 67 65 2e 20 20 50 6f 69 6e 74 65 72 73   page.  Pointers
146e1 20 74 6f 20 74 68 65 20 63 65 6c 6c 73 20 61 72   to the cells ar
146e2 65 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 70 6f  e in the cell po
146e3 69 6e 74 65 72 20 61 72 72 61 79 0a 2a 2a 20 74  inter array.** t
146e4 68 61 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  hat immediately 
146e5 66 6f 6c 6c 6f 77 73 20 74 68 65 20 70 61 67 65  follows the page
146e6 20 68 65 61 64 65 72 2e 20 20 43 65 6c 6c 73 20   header.  Cells 
146e7 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69  is not necessari
146e8 6c 79 0a 2a 2a 20 63 6f 6e 74 69 67 75 6f 75 73  ly.** contiguous
146e9 20 6f 72 20 69 6e 20 6f 72 64 65 72 2c 20 62 75   or in order, bu
146ea 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 73 20  t cell pointers 
146eb 61 72 65 20 63 6f 6e 74 69 67 75 6f 75 73 20 61  are contiguous a
146ec 6e 64 20 69 6e 20 6f 72 64 65 72 2e 0a 2a 2a 0a  nd in order..**.
146ed 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  ** Cell content 
146ee 6d 61 6b 65 73 20 75 73 65 20 6f 66 20 76 61 72  makes use of var
146ef 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74  iable length int
146f0 65 67 65 72 73 2e 20 20 41 20 76 61 72 69 61 62  egers.  A variab
146f1 6c 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 69 6e 74  le.** length int
146f2 65 67 65 72 20 69 73 20 31 20 74 6f 20 39 20 62  eger is 1 to 9 b
146f3 79 74 65 73 20 77 68 65 72 65 20 74 68 65 20 6c  ytes where the l
146f4 6f 77 65 72 20 37 20 62 69 74 73 20 6f 66 20 65  ower 7 bits of e
146f5 61 63 68 20 0a 2a 2a 20 62 79 74 65 20 61 72 65  ach .** byte are
146f6 20 75 73 65 64 2e 20 20 54 68 65 20 69 6e 74 65   used.  The inte
146f7 67 65 72 20 63 6f 6e 73 69 73 74 73 20 6f 66 20  ger consists of 
146f8 61 6c 6c 20 62 79 74 65 73 20 74 68 61 74 20 68  all bytes that h
146f9 61 76 65 20 62 69 74 20 38 20 73 65 74 20 61 6e  ave bit 8 set an
146fa 64 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 62  d.** the first b
146fb 79 74 65 20 77 69 74 68 20 62 69 74 20 38 20 63  yte with bit 8 c
146fc 6c 65 61 72 2e 20 20 54 68 65 20 6d 6f 73 74 20  lear.  The most 
146fd 73 69 67 6e 69 66 69 63 61 6e 74 20 62 79 74 65  significant byte
146fe 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a   of the integer.
146ff 2a 2a 20 61 70 70 65 61 72 73 20 66 69 72 73 74  ** appears first
14700 2e 20 20 41 20 76 61 72 69 61 62 6c 65 2d 6c 65  .  A variable-le
14701 6e 67 74 68 20 69 6e 74 65 67 65 72 20 6d 61 79  ngth integer may
14702 20 6e 6f 74 20 62 65 20 6d 6f 72 65 20 74 68 61   not be more tha
14703 6e 20 39 20 62 79 74 65 73 20 6c 6f 6e 67 2e 0a  n 9 bytes long..
14704 2a 2a 20 41 73 20 61 20 73 70 65 63 69 61 6c 20  ** As a special 
14705 63 61 73 65 2c 20 61 6c 6c 20 38 20 62 79 74 65  case, all 8 byte
14706 73 20 6f 66 20 74 68 65 20 39 74 68 20 62 79 74  s of the 9th byt
14707 65 20 61 72 65 20 75 73 65 64 20 61 73 20 64 61  e are used as da
14708 74 61 2e 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c  ta.  This.** all
14709 6f 77 73 20 61 20 36 34 2d 62 69 74 20 69 6e 74  ows a 64-bit int
1470a 65 67 65 72 20 74 6f 20 62 65 20 65 6e 63 6f 64  eger to be encod
1470b 65 64 20 69 6e 20 39 20 62 79 74 65 73 2e 0a 2a  ed in 9 bytes..*
1470c 2a 0a 2a 2a 20 20 20 20 30 78 30 30 20 20 20 20  *.**    0x00    
1470d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1470e 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30    becomes  0x000
1470f 30 30 30 30 30 0a 2a 2a 20 20 20 20 30 78 37 66  00000.**    0x7f
14710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14711 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30        becomes  0
14712 78 30 30 30 30 30 30 37 66 0a 2a 2a 20 20 20 20  x0000007f.**    
14713 30 78 38 31 20 30 78 30 30 20 20 20 20 20 20 20  0x81 0x00       
14714 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65            become
14715 73 20 20 30 78 30 30 30 30 30 30 38 30 0a 2a 2a  s  0x00000080.**
14716 20 20 20 20 30 78 38 32 20 30 78 30 30 20 20 20      0x82 0x00   
14717 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65                be
14718 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 31 30  comes  0x0000010
14719 30 0a 2a 2a 20 20 20 20 30 78 38 30 20 30 78 37  0.**    0x80 0x7
1471a 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f               
1471b 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30    becomes  0x000
1471c 30 30 30 37 66 0a 2a 2a 20 20 20 20 30 78 38 61  0007f.**    0x8a
1471d 20 30 78 39 31 20 30 78 64 31 20 30 78 61 63 20   0x91 0xd1 0xac 
1471e 30 78 37 38 20 20 62 65 63 6f 6d 65 73 20 20 30  0x78  becomes  0
1471f 78 31 32 33 34 35 36 37 38 0a 2a 2a 20 20 20 20  x12345678.**    
14720 30 78 38 31 20 30 78 38 31 20 30 78 38 31 20 30  0x81 0x81 0x81 0
14721 78 38 31 20 30 78 30 31 20 20 62 65 63 6f 6d 65  x81 0x01  become
14722 73 20 20 30 78 31 30 32 30 34 30 38 31 0a 2a 2a  s  0x10204081.**
14723 0a 2a 2a 20 56 61 72 69 61 62 6c 65 20 6c 65 6e  .** Variable len
14724 67 74 68 20 69 6e 74 65 67 65 72 73 20 61 72 65  gth integers are
14725 20 75 73 65 64 20 66 6f 72 20 72 6f 77 69 64 73   used for rowids
14726 20 61 6e 64 20 74 6f 20 68 6f 6c 64 20 74 68 65   and to hold the
14727 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 79   number of.** by
14728 74 65 73 20 6f 66 20 6b 65 79 20 61 6e 64 20 64  tes of key and d
14729 61 74 61 20 69 6e 20 61 20 62 74 72 65 65 20 63  ata in a btree c
1472a 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ell..**.** The c
1472b 6f 6e 74 65 6e 74 20 6f 66 20 61 20 63 65 6c 6c  ontent of a cell
1472c 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73   looks like this
1472d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20  :.**.**    SIZE 
1472e 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a     DESCRIPTION.*
1472f 2a 20 20 20 20 20 20 34 20 20 20 20 20 50 61 67  *      4     Pag
14730 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
14731 6c 65 66 74 20 63 68 69 6c 64 2e 20 4f 6d 69 74  left child. Omit
14732 74 65 64 20 69 66 20 6c 65 61 66 20 66 6c 61 67  ted if leaf flag
14733 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20   is set..**     
14734 76 61 72 20 20 20 20 4e 75 6d 62 65 72 20 6f 66  var    Number of
14735 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 2e 20   bytes of data. 
14736 4f 6d 69 74 74 65 64 20 69 66 20 74 68 65 20 7a  Omitted if the z
14737 65 72 6f 64 61 74 61 20 66 6c 61 67 20 69 73 20  erodata flag is 
14738 73 65 74 2e 0a 2a 2a 20 20 20 20 20 76 61 72 20  set..**     var 
14739 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74     Number of byt
1473a 65 73 20 6f 66 20 6b 65 79 2e 20 4f 72 20 74 68  es of key. Or th
1473b 65 20 6b 65 79 20 69 74 73 65 6c 66 20 69 66 20  e key itself if 
1473c 69 6e 74 6b 65 79 20 66 6c 61 67 20 69 73 20 73  intkey flag is s
1473d 65 74 2e 0a 2a 2a 20 20 20 20 20 20 2a 20 20 20  et..**      *   
1473e 20 20 50 61 79 6c 6f 61 64 0a 2a 2a 20 20 20 20    Payload.**    
1473f 20 20 34 20 20 20 20 20 46 69 72 73 74 20 70 61    4     First pa
14740 67 65 20 6f 66 20 74 68 65 20 6f 76 65 72 66 6c  ge of the overfl
14741 6f 77 20 63 68 61 69 6e 2e 20 20 4f 6d 69 74 74  ow chain.  Omitt
14742 65 64 20 69 66 20 6e 6f 20 6f 76 65 72 66 6c 6f  ed if no overflo
14743 77 0a 2a 2a 0a 2a 2a 20 4f 76 65 72 66 6c 6f 77  w.**.** Overflow
14744 20 70 61 67 65 73 20 66 6f 72 6d 20 61 20 6c 69   pages form a li
14745 6e 6b 65 64 20 6c 69 73 74 2e 20 20 45 61 63 68  nked list.  Each
14746 20 70 61 67 65 20 65 78 63 65 70 74 20 74 68 65   page except the
14747 20 6c 61 73 74 20 69 73 20 63 6f 6d 70 6c 65 74   last is complet
14748 65 6c 79 0a 2a 2a 20 66 69 6c 6c 65 64 20 77 69  ely.** filled wi
14749 74 68 20 64 61 74 61 20 28 70 61 67 65 73 69 7a  th data (pagesiz
1474a 65 20 2d 20 34 20 62 79 74 65 73 29 2e 20 20 54  e - 4 bytes).  T
1474b 68 65 20 6c 61 73 74 20 70 61 67 65 20 63 61 6e  he last page can
1474c 20 68 61 76 65 20 61 73 20 6c 69 74 74 6c 65 0a   have as little.
1474d 2a 2a 20 61 73 20 31 20 62 79 74 65 20 6f 66 20  ** as 1 byte of 
1474e 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 53  data..**.**    S
1474f 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49  IZE    DESCRIPTI
14750 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20  ON.**      4    
14751 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   Page number of 
14752 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70 61  next overflow pa
14753 67 65 0a 2a 2a 20 20 20 20 20 20 2a 20 20 20 20  ge.**      *    
14754 20 44 61 74 61 0a 2a 2a 0a 2a 2a 20 46 72 65 65   Data.**.** Free
14755 6c 69 73 74 20 70 61 67 65 73 20 63 6f 6d 65 20  list pages come 
14756 69 6e 20 74 77 6f 20 73 75 62 74 79 70 65 73 3a  in two subtypes:
14757 20 74 72 75 6e 6b 20 70 61 67 65 73 20 61 6e 64   trunk pages and
14758 20 6c 65 61 66 20 70 61 67 65 73 2e 20 20 54 68   leaf pages.  Th
14759 65 0a 2a 2a 20 66 69 6c 65 20 68 65 61 64 65 72  e.** file header
1475a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66   points to the f
1475b 69 72 73 74 20 69 6e 20 61 20 6c 69 6e 6b 65 64  irst in a linked
1475c 20 6c 69 73 74 20 6f 66 20 74 72 75 6e 6b 20 70   list of trunk p
1475d 61 67 65 2e 20 20 45 61 63 68 20 74 72 75 6e 6b  age.  Each trunk
1475e 0a 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 73 20  .** page points 
1475f 74 6f 20 6d 75 6c 74 69 70 6c 65 20 6c 65 61 66  to multiple leaf
14760 20 70 61 67 65 73 2e 20 20 54 68 65 20 63 6f 6e   pages.  The con
14761 74 65 6e 74 20 6f 66 20 61 20 6c 65 61 66 20 70  tent of a leaf p
14762 61 67 65 20 69 73 0a 2a 2a 20 75 6e 73 70 65 63  age is.** unspec
14763 69 66 69 65 64 2e 20 20 41 20 74 72 75 6e 6b 20  ified.  A trunk 
14764 70 61 67 65 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20  page looks like 
14765 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53  this:.**.**    S
14766 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49  IZE    DESCRIPTI
14767 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20  ON.**      4    
14768 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   Page number of 
14769 6e 65 78 74 20 74 72 75 6e 6b 20 70 61 67 65 0a  next trunk page.
1476a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 4e 75  **      4     Nu
1476b 6d 62 65 72 20 6f 66 20 6c 65 61 66 20 70 6f 69  mber of leaf poi
1476c 6e 74 65 72 73 20 6f 6e 20 74 68 69 73 20 70 61  nters on this pa
1476d 67 65 0a 2a 2a 20 20 20 20 20 20 2a 20 20 20 20  ge.**      *    
1476e 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 70 61   zero or more pa
1476f 67 65 73 20 6e 75 6d 62 65 72 73 20 6f 66 20 6c  ges numbers of l
14770 65 61 76 65 73 0a 2a 2f 0a 0a 0a 2f 2a 20 54 68  eaves.*/.../* Th
14771 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75  e following valu
14772 65 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d  e is the maximum
14773 20 63 65 6c 6c 20 73 69 7a 65 20 61 73 73 75 6d   cell size assum
14774 69 6e 67 20 61 20 6d 61 78 69 6d 75 6d 20 70 61  ing a maximum pa
14775 67 65 0a 2a 2a 20 73 69 7a 65 20 67 69 76 65 20  ge.** size give 
14776 61 62 6f 76 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e  above..*/.#defin
14777 65 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70  e MX_CELL_SIZE(p
14778 42 74 29 20 20 28 70 42 74 2d 3e 70 61 67 65 53  Bt)  (pBt->pageS
14779 69 7a 65 2d 38 29 0a 0a 2f 2a 20 54 68 65 20 6d  ize-8)../* The m
1477a 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  aximum number of
1477b 20 63 65 6c 6c 73 20 6f 6e 20 61 20 73 69 6e 67   cells on a sing
1477c 6c 65 20 70 61 67 65 20 6f 66 20 74 68 65 20 64  le page of the d
1477d 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 0a 2a  atabase.  This.*
1477e 2a 20 61 73 73 75 6d 65 73 20 61 20 6d 69 6e 69  * assumes a mini
1477f 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 6f 66  mum cell size of
14780 20 36 20 62 79 74 65 73 20 20 28 34 20 62 79 74   6 bytes  (4 byt
14781 65 73 20 66 6f 72 20 74 68 65 20 63 65 6c 6c 20  es for the cell 
14782 69 74 73 65 6c 66 0a 2a 2a 20 70 6c 75 73 20 32  itself.** plus 2
14783 20 62 79 74 65 73 20 66 6f 72 20 74 68 65 20 69   bytes for the i
14784 6e 64 65 78 20 74 6f 20 74 68 65 20 63 65 6c 6c  ndex to the cell
14785 20 69 6e 20 74 68 65 20 70 61 67 65 20 68 65 61   in the page hea
14786 64 65 72 29 2e 20 20 53 75 63 68 0a 2a 2a 20 73  der).  Such.** s
14787 6d 61 6c 6c 20 63 65 6c 6c 73 20 77 69 6c 6c 20  mall cells will 
14788 62 65 20 72 61 72 65 2c 20 62 75 74 20 74 68 65  be rare, but the
14789 79 20 61 72 65 20 70 6f 73 73 69 62 6c 65 2e 0a  y are possible..
1478a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43 45  */.#define MX_CE
1478b 4c 4c 28 70 42 74 29 20 28 28 70 42 74 2d 3e 70  LL(pBt) ((pBt->p
1478c 61 67 65 53 69 7a 65 2d 38 29 2f 36 29 0a 0a 2f  ageSize-8)/6)../
1478d 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72  * Forward declar
1478e 61 74 69 6f 6e 73 20 2a 2f 0a 74 79 70 65 64 65  ations */.typede
1478f 66 20 73 74 72 75 63 74 20 4d 65 6d 50 61 67 65  f struct MemPage
14790 20 4d 65 6d 50 61 67 65 3b 0a 74 79 70 65 64 65   MemPage;.typede
14791 66 20 73 74 72 75 63 74 20 42 74 4c 6f 63 6b 20  f struct BtLock 
14792 42 74 4c 6f 63 6b 3b 0a 0a 2f 2a 0a 2a 2a 20 54  BtLock;../*.** T
14793 68 69 73 20 69 73 20 61 20 6d 61 67 69 63 20 73  his is a magic s
14794 74 72 69 6e 67 20 74 68 61 74 20 61 70 70 65 61  tring that appea
14795 72 73 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  rs at the beginn
14796 69 6e 67 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20  ing of every.** 
14797 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20  SQLite database 
14798 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 64 65 6e  in order to iden
14799 74 69 66 79 20 74 68 65 20 66 69 6c 65 20 61 73  tify the file as
1479a 20 61 20 72 65 61 6c 20 64 61 74 61 62 61 73 65   a real database
1479b 2e 0a 2a 2a 0a 2a 2a 20 59 6f 75 20 63 61 6e 20  ..**.** You can 
1479c 63 68 61 6e 67 65 20 74 68 69 73 20 76 61 6c 75  change this valu
1479d 65 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  e at compile-tim
1479e 65 20 62 79 20 73 70 65 63 69 66 79 69 6e 67 20  e by specifying 
1479f 61 0a 2a 2a 20 2d 44 53 51 4c 49 54 45 5f 46 49  a.** -DSQLITE_FI
147a0 4c 45 5f 48 45 41 44 45 52 3d 22 2e 2e 2e 22 20  LE_HEADER="..." 
147a1 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20  on the compiler 
147a2 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 2e 20 20 54  command-line.  T
147a3 68 65 0a 2a 2a 20 68 65 61 64 65 72 20 6d 75 73  he.** header mus
147a4 74 20 62 65 20 65 78 61 63 74 6c 79 20 31 36 20  t be exactly 16 
147a5 62 79 74 65 73 20 69 6e 63 6c 75 64 69 6e 67 20  bytes including 
147a6 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61  the zero-termina
147a7 74 6f 72 20 73 6f 0a 2a 2a 20 74 68 65 20 73 74  tor so.** the st
147a8 72 69 6e 67 20 69 74 73 65 6c 66 20 73 68 6f 75  ring itself shou
147a9 6c 64 20 62 65 20 31 35 20 63 68 61 72 61 63 74  ld be 15 charact
147aa 65 72 73 20 6c 6f 6e 67 2e 20 20 49 66 20 79 6f  ers long.  If yo
147ab 75 20 63 68 61 6e 67 65 0a 2a 2a 20 74 68 65 20  u change.** the 
147ac 68 65 61 64 65 72 2c 20 74 68 65 6e 20 79 6f 75  header, then you
147ad 72 20 63 75 73 74 6f 6d 20 6c 69 62 72 61 72 79  r custom library
147ae 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c   will not be abl
147af 65 20 74 6f 20 72 65 61 64 20 0a 2a 2a 20 64 61  e to read .** da
147b0 74 61 62 61 73 65 73 20 67 65 6e 65 72 61 74 65  tabases generate
147b1 64 20 62 79 20 74 68 65 20 73 74 61 6e 64 61 72  d by the standar
147b2 64 20 74 6f 6f 6c 73 20 61 6e 64 20 74 68 65 20  d tools and the 
147b3 73 74 61 6e 64 61 72 64 20 74 6f 6f 6c 73 0a 2a  standard tools.*
147b4 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62  * will not be ab
147b5 6c 65 20 74 6f 20 72 65 61 64 20 64 61 74 61 62  le to read datab
147b6 61 73 65 73 20 63 72 65 61 74 65 64 20 62 79 20  ases created by 
147b7 79 6f 75 72 20 63 75 73 74 6f 6d 20 6c 69 62 72  your custom libr
147b8 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ary..*/.#ifndef 
147b9 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44  SQLITE_FILE_HEAD
147ba 45 52 20 2f 2a 20 31 32 33 34 35 36 37 38 39 20  ER /* 123456789 
147bb 31 32 33 34 35 36 20 2a 2f 0a 23 20 20 64 65 66  123456 */.#  def
147bc 69 6e 65 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f  ine SQLITE_FILE_
147bd 48 45 41 44 45 52 20 22 53 51 4c 69 74 65 20 66  HEADER "SQLite f
147be 6f 72 6d 61 74 20 33 22 0a 23 65 6e 64 69 66 0a  ormat 3".#endif.
147bf 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 74 79 70 65  ./*.** Page type
147c0 20 66 6c 61 67 73 2e 20 20 41 6e 20 4f 52 65 64   flags.  An ORed
147c1 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20   combination of 
147c2 74 68 65 73 65 20 66 6c 61 67 73 20 61 70 70 65  these flags appe
147c3 61 72 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 72  ar as the.** fir
147c4 73 74 20 62 79 74 65 20 6f 66 20 6f 6e 2d 64 69  st byte of on-di
147c5 73 6b 20 69 6d 61 67 65 20 6f 66 20 65 76 65 72  sk image of ever
147c6 79 20 42 54 72 65 65 20 70 61 67 65 2e 0a 2a 2f  y BTree page..*/
147c7 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 49 4e 54  .#define PTF_INT
147c8 4b 45 59 20 20 20 20 30 78 30 31 0a 23 64 65 66  KEY    0x01.#def
147c9 69 6e 65 20 50 54 46 5f 5a 45 52 4f 44 41 54 41  ine PTF_ZERODATA
147ca 20 20 30 78 30 32 0a 23 64 65 66 69 6e 65 20 50    0x02.#define P
147cb 54 46 5f 4c 45 41 46 44 41 54 41 20 20 30 78 30  TF_LEAFDATA  0x0
147cc 34 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 4c 45  4.#define PTF_LE
147cd 41 46 20 20 20 20 20 20 30 78 30 38 0a 0a 2f 2a  AF      0x08../*
147ce 0a 2a 2a 20 41 73 20 65 61 63 68 20 70 61 67 65  .** As each page
147cf 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 20   of the file is 
147d0 6c 6f 61 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f  loaded into memo
147d1 72 79 2c 20 61 6e 20 69 6e 73 74 61 6e 63 65 20  ry, an instance 
147d2 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
147d3 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69 73  .** structure is
147d4 20 61 70 70 65 6e 64 65 64 20 61 6e 64 20 69 6e   appended and in
147d5 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72  itialized to zer
147d6 6f 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75  o.  This structu
147d7 72 65 20 73 74 6f 72 65 73 0a 2a 2a 20 69 6e 66  re stores.** inf
147d8 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
147d9 68 65 20 70 61 67 65 20 74 68 61 74 20 69 73 20  he page that is 
147da 64 65 63 6f 64 65 64 20 66 72 6f 6d 20 74 68 65  decoded from the
147db 20 72 61 77 20 66 69 6c 65 20 70 61 67 65 2e 0a   raw file page..
147dc 2a 2a 0a 2a 2a 20 54 68 65 20 70 50 61 72 65 6e  **.** The pParen
147dd 74 20 66 69 65 6c 64 20 70 6f 69 6e 74 73 20 62  t field points b
147de 61 63 6b 20 74 6f 20 74 68 65 20 70 61 72 65 6e  ack to the paren
147df 74 20 70 61 67 65 2e 20 20 54 68 69 73 20 61 6c  t page.  This al
147e0 6c 6f 77 73 20 75 73 20 74 6f 0a 2a 2a 20 77 61  lows us to.** wa
147e1 6c 6b 20 75 70 20 74 68 65 20 42 54 72 65 65 20  lk up the BTree 
147e2 66 72 6f 6d 20 61 6e 79 20 6c 65 61 66 20 74 6f  from any leaf to
147e3 20 74 68 65 20 72 6f 6f 74 2e 20 20 43 61 72 65   the root.  Care
147e4 20 6d 75 73 74 20 62 65 20 74 61 6b 65 6e 20 74   must be taken t
147e5 6f 0a 2a 2a 20 75 6e 72 65 66 28 29 20 74 68 65  o.** unref() the
147e6 20 70 61 72 65 6e 74 20 70 61 67 65 20 70 6f 69   parent page poi
147e7 6e 74 65 72 20 77 68 65 6e 20 74 68 69 73 20 70  nter when this p
147e8 61 67 65 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72  age is no longer
147e9 20 72 65 66 65 72 65 6e 63 65 64 2e 0a 2a 2a 20   referenced..** 
147ea 54 68 65 20 70 61 67 65 44 65 73 74 72 75 63 74  The pageDestruct
147eb 6f 72 28 29 20 72 6f 75 74 69 6e 65 20 68 61 6e  or() routine han
147ec 64 6c 65 73 20 74 68 61 74 20 63 68 6f 72 65 2e  dles that chore.
147ed 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74 6f  .**.** Access to
147ee 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f 66 20 74   all fields of t
147ef 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73  his structure is
147f0 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 74   controlled by t
147f1 68 65 20 6d 75 74 65 78 0a 2a 2a 20 73 74 6f 72  he mutex.** stor
147f2 65 64 20 69 6e 20 4d 65 6d 50 61 67 65 2e 70 42  ed in MemPage.pB
147f3 74 2d 3e 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 72  t->mutex..*/.str
147f4 75 63 74 20 4d 65 6d 50 61 67 65 20 7b 0a 20 20  uct MemPage {.  
147f5 75 38 20 69 73 49 6e 69 74 3b 20 20 20 20 20 20  u8 isInit;      
147f6 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
147f7 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 69 74 69  previously initi
147f8 61 6c 69 7a 65 64 2e 20 4d 55 53 54 20 42 45 20  alized. MUST BE 
147f9 46 49 52 53 54 21 20 2a 2f 0a 20 20 75 38 20 6e  FIRST! */.  u8 n
147fa 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20 20 20 20  Overflow;       
147fb 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 76   /* Number of ov
147fc 65 72 66 6c 6f 77 20 63 65 6c 6c 20 62 6f 64 69  erflow cell bodi
147fd 65 73 20 69 6e 20 61 43 65 6c 6c 5b 5d 20 2a 2f  es in aCell[] */
147fe 0a 20 20 75 38 20 69 6e 74 4b 65 79 3b 20 20 20  .  u8 intKey;   
147ff 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
14800 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 67 20 69  if intkey flag i
14801 73 20 73 65 74 20 2a 2f 0a 20 20 75 38 20 6c 65  s set */.  u8 le
14802 61 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  af;             
14803 2f 2a 20 54 72 75 65 20 69 66 20 6c 65 61 66 20  /* True if leaf 
14804 66 6c 61 67 20 69 73 20 73 65 74 20 2a 2f 0a 20  flag is set */. 
14805 20 75 38 20 68 61 73 44 61 74 61 3b 20 20 20 20   u8 hasData;    
14806 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
14807 20 74 68 69 73 20 70 61 67 65 20 73 74 6f 72 65   this page store
14808 73 20 64 61 74 61 20 2a 2f 0a 20 20 75 38 20 68  s data */.  u8 h
14809 64 72 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20  drOffset;       
1480a 20 2f 2a 20 31 30 30 20 66 6f 72 20 70 61 67 65   /* 100 for page
1480b 20 31 2e 20 20 30 20 6f 74 68 65 72 77 69 73 65   1.  0 otherwise
1480c 20 2a 2f 0a 20 20 75 38 20 63 68 69 6c 64 50 74   */.  u8 childPt
1480d 72 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 30 20  rSize;     /* 0 
1480e 69 66 20 6c 65 61 66 3d 3d 31 2e 20 20 34 20 69  if leaf==1.  4 i
1480f 66 20 6c 65 61 66 3d 3d 30 20 2a 2f 0a 20 20 75  f leaf==0 */.  u
14810 31 36 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 20 20  16 maxLocal;    
14811 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42      /* Copy of B
14812 74 53 68 61 72 65 64 2e 6d 61 78 4c 6f 63 61 6c  tShared.maxLocal
14813 20 6f 72 20 42 74 53 68 61 72 65 64 2e 6d 61 78   or BtShared.max
14814 4c 65 61 66 20 2a 2f 0a 20 20 75 31 36 20 6d 69  Leaf */.  u16 mi
14815 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 2f  nLocal;        /
14816 2a 20 43 6f 70 79 20 6f 66 20 42 74 53 68 61 72  * Copy of BtShar
14817 65 64 2e 6d 69 6e 4c 6f 63 61 6c 20 6f 72 20 42  ed.minLocal or B
14818 74 53 68 61 72 65 64 2e 6d 69 6e 4c 65 61 66 20  tShared.minLeaf 
14819 2a 2f 0a 20 20 75 31 36 20 63 65 6c 6c 4f 66 66  */.  u16 cellOff
1481a 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 49 6e 64  set;      /* Ind
1481b 65 78 20 69 6e 20 61 44 61 74 61 20 6f 66 20 66  ex in aData of f
1481c 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65  irst cell pointe
1481d 72 20 2a 2f 0a 20 20 75 31 36 20 6e 46 72 65 65  r */.  u16 nFree
1481e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
1481f 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 62 79  umber of free by
14820 74 65 73 20 6f 6e 20 74 68 65 20 70 61 67 65 20  tes on the page 
14821 2a 2f 0a 20 20 75 31 36 20 6e 43 65 6c 6c 3b 20  */.  u16 nCell; 
14822 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
14823 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20  ber of cells on 
14824 74 68 69 73 20 70 61 67 65 2c 20 6c 6f 63 61 6c  this page, local
14825 20 61 6e 64 20 6f 76 66 6c 20 2a 2f 0a 20 20 75   and ovfl */.  u
14826 31 36 20 6d 61 73 6b 50 61 67 65 3b 20 20 20 20  16 maskPage;    
14827 20 20 20 20 2f 2a 20 4d 61 73 6b 20 66 6f 72 20      /* Mask for 
14828 70 61 67 65 20 6f 66 66 73 65 74 20 2a 2f 0a 20  page offset */. 
14829 20 73 74 72 75 63 74 20 5f 4f 76 66 6c 43 65 6c   struct _OvflCel
1482a 6c 20 7b 20 20 20 2f 2a 20 43 65 6c 6c 73 20 74  l {   /* Cells t
1482b 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74  hat will not fit
1482c 20 6f 6e 20 61 44 61 74 61 5b 5d 20 2a 2f 0a 20   on aData[] */. 
1482d 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20     u8 *pCell;   
1482e 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
1482f 72 73 20 74 6f 20 74 68 65 20 62 6f 64 79 20 6f  rs to the body o
14830 66 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63  f the overflow c
14831 65 6c 6c 20 2a 2f 0a 20 20 20 20 75 31 36 20 69  ell */.    u16 i
14832 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  dx;            /
14833 2a 20 49 6e 73 65 72 74 20 74 68 69 73 20 63 65  * Insert this ce
14834 6c 6c 20 62 65 66 6f 72 65 20 69 64 78 2d 74 68  ll before idx-th
14835 20 6e 6f 6e 2d 6f 76 65 72 66 6c 6f 77 20 63 65   non-overflow ce
14836 6c 6c 20 2a 2f 0a 20 20 7d 20 61 4f 76 66 6c 5b  ll */.  } aOvfl[
14837 35 5d 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  5];.  BtShared *
14838 70 42 74 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f  pBt;       /* Po
14839 69 6e 74 65 72 20 74 6f 20 42 74 53 68 61 72 65  inter to BtShare
1483a 64 20 74 68 61 74 20 74 68 69 73 20 70 61 67 65  d that this page
1483b 20 69 73 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20   is part of */. 
1483c 20 75 38 20 2a 61 44 61 74 61 3b 20 20 20 20 20   u8 *aData;     
1483d 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
1483e 20 74 6f 20 64 69 73 6b 20 69 6d 61 67 65 20 6f   to disk image o
1483f 66 20 74 68 65 20 70 61 67 65 20 64 61 74 61 20  f the page data 
14840 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62  */.  DbPage *pDb
14841 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 50 61 67  Page;     /* Pag
14842 65 72 20 70 61 67 65 20 68 61 6e 64 6c 65 20 2a  er page handle *
14843 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20  /.  Pgno pgno;  
14844 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
14845 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 69 73   number for this
14846 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a   page */.};../*.
14847 2a 2a 20 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  ** The in-memory
14848 20 69 6d 61 67 65 20 6f 66 20 61 20 64 69 73 6b   image of a disk
14849 20 70 61 67 65 20 68 61 73 20 74 68 65 20 61 75   page has the au
1484a 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 74  xiliary informat
1484b 69 6f 6e 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20  ion appended.** 
1484c 74 6f 20 74 68 65 20 65 6e 64 2e 20 20 45 58 54  to the end.  EXT
1484d 52 41 5f 53 49 5a 45 20 69 73 20 74 68 65 20 6e  RA_SIZE is the n
1484e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
1484f 66 20 73 70 61 63 65 20 6e 65 65 64 65 64 20 74  f space needed t
14850 6f 20 68 6f 6c 64 0a 2a 2a 20 74 68 61 74 20 65  o hold.** that e
14851 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  xtra information
14852 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 54  ..*/.#define EXT
14853 52 41 5f 53 49 5a 45 20 73 69 7a 65 6f 66 28 4d  RA_SIZE sizeof(M
14854 65 6d 50 61 67 65 29 0a 0a 2f 2a 0a 2a 2a 20 41  emPage)../*.** A
14855 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20   linked list of 
14856 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
14857 72 75 63 74 75 72 65 73 20 69 73 20 73 74 6f 72  ructures is stor
14858 65 64 20 61 74 20 42 74 53 68 61 72 65 64 2e 70  ed at BtShared.p
14859 4c 6f 63 6b 2e 0a 2a 2a 20 4c 6f 63 6b 73 20 61  Lock..** Locks a
1485a 72 65 20 61 64 64 65 64 20 28 6f 72 20 75 70 67  re added (or upg
1485b 72 61 64 65 64 20 66 72 6f 6d 20 52 45 41 44 5f  raded from READ_
1485c 4c 4f 43 4b 20 74 6f 20 57 52 49 54 45 5f 4c 4f  LOCK to WRITE_LO
1485d 43 4b 29 20 77 68 65 6e 20 61 20 63 75 72 73 6f  CK) when a curso
1485e 72 20 0a 2a 2a 20 69 73 20 6f 70 65 6e 65 64 20  r .** is opened 
1485f 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 74  on the table wit
14860 68 20 72 6f 6f 74 20 70 61 67 65 20 42 74 53 68  h root page BtSh
14861 61 72 65 64 2e 69 54 61 62 6c 65 2e 20 4c 6f 63  ared.iTable. Loc
14862 6b 73 20 61 72 65 20 72 65 6d 6f 76 65 64 0a 2a  ks are removed.*
14863 2a 20 66 72 6f 6d 20 74 68 69 73 20 6c 69 73 74  * from this list
14864 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74   when a transact
14865 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64  ion is committed
14866 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c   or rolled back,
14867 20 6f 72 20 77 68 65 6e 0a 2a 2a 20 61 20 62 74   or when.** a bt
14868 72 65 65 20 68 61 6e 64 6c 65 20 69 73 20 63 6c  ree handle is cl
14869 6f 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  osed..*/.struct 
1486a 42 74 4c 6f 63 6b 20 7b 0a 20 20 42 74 72 65 65  BtLock {.  Btree
1486b 20 2a 70 42 74 72 65 65 3b 20 20 20 20 20 20 20   *pBtree;       
1486c 20 2f 2a 20 42 74 72 65 65 20 68 61 6e 64 6c 65   /* Btree handle
1486d 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 6c 6f   holding this lo
1486e 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 61  ck */.  Pgno iTa
1486f 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ble;          /*
14870 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 61   Root page of ta
14871 62 6c 65 20 2a 2f 0a 20 20 75 38 20 65 4c 6f 63  ble */.  u8 eLoc
14872 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  k;             /
14873 2a 20 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57  * READ_LOCK or W
14874 52 49 54 45 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 42  RITE_LOCK */.  B
14875 74 4c 6f 63 6b 20 2a 70 4e 65 78 74 3b 20 20 20  tLock *pNext;   
14876 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20       /* Next in 
14877 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c  BtShared.pLock l
14878 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 43 61  ist */.};../* Ca
14879 6e 64 69 64 61 74 65 20 76 61 6c 75 65 73 20 66  ndidate values f
1487a 6f 72 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20  or BtLock.eLock 
1487b 2a 2f 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f  */.#define READ_
1487c 4c 4f 43 4b 20 20 20 20 20 31 0a 23 64 65 66 69  LOCK     1.#defi
1487d 6e 65 20 57 52 49 54 45 5f 4c 4f 43 4b 20 20 20  ne WRITE_LOCK   
1487e 20 32 0a 0a 2f 2a 20 41 20 42 74 72 65 65 20 68   2../* A Btree h
1487f 61 6e 64 6c 65 0a 2a 2a 0a 2a 2a 20 41 20 64 61  andle.**.** A da
14880 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
14881 6e 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69  n contains a poi
14882 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61  nter to an insta
14883 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 6f  nce of.** this o
14884 62 6a 65 63 74 20 66 6f 72 20 65 76 65 72 79 20  bject for every 
14885 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 68  database file th
14886 61 74 20 69 74 20 68 61 73 20 6f 70 65 6e 2e 20  at it has open. 
14887 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 0a   This structure.
14888 2a 2a 20 69 73 20 6f 70 61 71 75 65 20 74 6f 20  ** is opaque to 
14889 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
1488a 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 64 61  nection.  The da
1488b 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1488c 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 73 65 65 20  n cannot.** see 
1488d 74 68 65 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66  the internals of
1488e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
1488f 61 6e 64 20 6f 6e 6c 79 20 64 65 61 6c 73 20 77  and only deals w
14890 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a  ith pointers to.
14891 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 75 72  ** this structur
14892 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 73 6f 6d  e..**.** For som
14893 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73  e database files
14894 2c 20 74 68 65 20 73 61 6d 65 20 75 6e 64 65 72  , the same under
14895 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 20 63  lying database c
14896 61 63 68 65 20 6d 69 67 68 74 20 62 65 20 0a 2a  ache might be .*
14897 2a 20 73 68 61 72 65 64 20 62 65 74 77 65 65 6e  * shared between
14898 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 6e 65 63   multiple connec
14899 74 69 6f 6e 73 2e 20 20 49 6e 20 74 68 61 74 20  tions.  In that 
1489a 63 61 73 65 2c 20 65 61 63 68 20 63 6f 6e 74 65  case, each conte
1489b 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 69 74 20  ction.** has it 
1489c 6f 77 6e 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  own pointer to t
1489d 68 69 73 20 6f 62 6a 65 63 74 2e 20 20 42 75 74  his object.  But
1489e 20 65 61 63 68 20 69 6e 73 74 61 6e 63 65 20 6f   each instance o
1489f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 0a 2a 2a  f this object.**
148a0 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73   points to the s
148a1 61 6d 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a  ame BtShared obj
148a2 65 63 74 2e 20 20 54 68 65 20 64 61 74 61 62 61  ect.  The databa
148a3 73 65 20 63 61 63 68 65 20 61 6e 64 20 74 68 65  se cache and the
148a4 0a 2a 2a 20 73 63 68 65 6d 61 20 61 73 73 6f 63  .** schema assoc
148a5 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 64  iated with the d
148a6 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 72 65  atabase file are
148a7 20 61 6c 6c 20 63 6f 6e 74 61 69 6e 65 64 20 77   all contained w
148a8 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 42 74 53  ithin.** the BtS
148a9 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 0a 2a 2a  hared object..**
148aa 0a 2a 2a 20 41 6c 6c 20 66 69 65 6c 64 73 20 69  .** All fields i
148ab 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  n this structure
148ac 20 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e   are accessed un
148ad 64 65 72 20 73 71 6c 69 74 65 33 2e 6d 75 74 65  der sqlite3.mute
148ae 78 2e 0a 2a 2a 20 54 68 65 20 70 42 74 20 70 6f  x..** The pBt po
148af 69 6e 74 65 72 20 69 74 73 65 6c 66 20 6d 61 79  inter itself may
148b0 20 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64 20   not be changed 
148b1 77 68 69 6c 65 20 74 68 65 72 65 20 65 78 69 73  while there exis
148b2 74 73 20 63 75 72 73 6f 72 73 20 0a 2a 2a 20 69  ts cursors .** i
148b3 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 64  n the referenced
148b4 20 42 74 53 68 61 72 65 64 20 74 68 61 74 20 70   BtShared that p
148b5 6f 69 6e 74 20 62 61 63 6b 20 74 6f 20 74 68 69  oint back to thi
148b6 73 20 42 74 72 65 65 20 73 69 6e 63 65 20 74 68  s Btree since th
148b7 6f 73 65 0a 2a 2a 20 63 75 72 73 6f 72 73 20 68  ose.** cursors h
148b8 61 76 65 20 74 6f 20 64 6f 20 67 6f 20 74 68 72  ave to do go thr
148b9 6f 75 67 68 20 74 68 69 73 20 42 74 72 65 65 20  ough this Btree 
148ba 74 6f 20 66 69 6e 64 20 74 68 65 69 72 20 42 74  to find their Bt
148bb 53 68 61 72 65 64 20 61 6e 64 0a 2a 2a 20 74 68  Shared and.** th
148bc 65 79 20 6f 66 74 65 6e 20 64 6f 20 73 6f 20 77  ey often do so w
148bd 69 74 68 6f 75 74 20 68 6f 6c 64 69 6e 67 20 73  ithout holding s
148be 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e 0a 2a 2f  qlite3.mutex..*/
148bf 0a 73 74 72 75 63 74 20 42 74 72 65 65 20 7b 0a  .struct Btree {.
148c0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
148c1 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
148c2 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
148c3 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 62 74 72  holding this btr
148c4 65 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64  ee */.  BtShared
148c5 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 53 68   *pBt;     /* Sh
148c6 61 72 61 62 6c 65 20 63 6f 6e 74 65 6e 74 20 6f  arable content o
148c7 66 20 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a  f this btree */.
148c8 20 20 75 38 20 69 6e 54 72 61 6e 73 3b 20 20 20    u8 inTrans;   
148c9 20 20 20 20 20 2f 2a 20 54 52 41 4e 53 5f 4e 4f       /* TRANS_NO
148ca 4e 45 2c 20 54 52 41 4e 53 5f 52 45 41 44 20 6f  NE, TRANS_READ o
148cb 72 20 54 52 41 4e 53 5f 57 52 49 54 45 20 2a 2f  r TRANS_WRITE */
148cc 0a 20 20 75 38 20 73 68 61 72 61 62 6c 65 3b 20  .  u8 sharable; 
148cd 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
148ce 20 77 65 20 63 61 6e 20 73 68 61 72 65 20 70 42   we can share pB
148cf 74 20 77 69 74 68 20 61 6e 6f 74 68 65 72 20 64  t with another d
148d0 62 20 2a 2f 0a 20 20 75 38 20 6c 6f 63 6b 65 64  b */.  u8 locked
148d1 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ;         /* Tru
148d2 65 20 69 66 20 64 62 20 63 75 72 72 65 6e 74 6c  e if db currentl
148d3 79 20 68 61 73 20 70 42 74 20 6c 6f 63 6b 65 64  y has pBt locked
148d4 20 2a 2f 0a 20 20 69 6e 74 20 77 61 6e 74 54 6f   */.  int wantTo
148d5 4c 6f 63 6b 3b 20 20 20 20 2f 2a 20 4e 75 6d 62  Lock;    /* Numb
148d6 65 72 20 6f 66 20 6e 65 73 74 65 64 20 63 61 6c  er of nested cal
148d7 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72  ls to sqlite3Btr
148d8 65 65 45 6e 74 65 72 28 29 20 2a 2f 0a 20 20 69  eeEnter() */.  i
148d9 6e 74 20 6e 42 61 63 6b 75 70 3b 20 20 20 20 20  nt nBackup;     
148da 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
148db 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 73  ackup operations
148dc 20 72 65 61 64 69 6e 67 20 74 68 69 73 20 62 74   reading this bt
148dd 72 65 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a  ree */.  Btree *
148de 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4c  pNext;      /* L
148df 69 73 74 20 6f 66 20 6f 74 68 65 72 20 73 68 61  ist of other sha
148e0 72 61 62 6c 65 20 42 74 72 65 65 73 20 66 72 6f  rable Btrees fro
148e1 6d 20 74 68 65 20 73 61 6d 65 20 64 62 20 2a 2f  m the same db */
148e2 0a 20 20 42 74 72 65 65 20 2a 70 50 72 65 76 3b  .  Btree *pPrev;
148e3 20 20 20 20 20 20 2f 2a 20 42 61 63 6b 20 70 6f        /* Back po
148e4 69 6e 74 65 72 20 6f 66 20 74 68 65 20 73 61 6d  inter of the sam
148e5 65 20 6c 69 73 74 20 2a 2f 0a 23 69 66 6e 64 65  e list */.#ifnde
148e6 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
148e7 41 52 45 44 5f 43 41 43 48 45 0a 20 20 42 74 4c  ARED_CACHE.  BtL
148e8 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20 20 20 20 20  ock lock;       
148e9 2f 2a 20 4f 62 6a 65 63 74 20 75 73 65 64 20 74  /* Object used t
148ea 6f 20 6c 6f 63 6b 20 70 61 67 65 20 31 20 2a 2f  o lock page 1 */
148eb 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a  .#endif.};../*.*
148ec 2a 20 42 74 72 65 65 2e 69 6e 54 72 61 6e 73 20  * Btree.inTrans 
148ed 6d 61 79 20 74 61 6b 65 20 6f 6e 65 20 6f 66 20  may take one of 
148ee 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61  the following va
148ef 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  lues..**.** If t
148f0 68 65 20 73 68 61 72 65 64 2d 64 61 74 61 20 65  he shared-data e
148f1 78 74 65 6e 73 69 6f 6e 20 69 73 20 65 6e 61 62  xtension is enab
148f2 6c 65 64 2c 20 74 68 65 72 65 20 6d 61 79 20 62  led, there may b
148f3 65 20 6d 75 6c 74 69 70 6c 65 20 75 73 65 72 73  e multiple users
148f4 0a 2a 2a 20 6f 66 20 74 68 65 20 42 74 72 65 65  .** of the Btree
148f5 20 73 74 72 75 63 74 75 72 65 2e 20 41 74 20 6d   structure. At m
148f6 6f 73 74 20 6f 6e 65 20 6f 66 20 74 68 65 73 65  ost one of these
148f7 20 6d 61 79 20 6f 70 65 6e 20 61 20 77 72 69 74   may open a writ
148f8 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 2a  e transaction,.*
148f9 2a 20 62 75 74 20 61 6e 79 20 6e 75 6d 62 65 72  * but any number
148fa 20 6d 61 79 20 68 61 76 65 20 61 63 74 69 76 65   may have active
148fb 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f   read transactio
148fc 6e 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54  ns..*/.#define T
148fd 52 41 4e 53 5f 4e 4f 4e 45 20 20 30 0a 23 64 65  RANS_NONE  0.#de
148fe 66 69 6e 65 20 54 52 41 4e 53 5f 52 45 41 44 20  fine TRANS_READ 
148ff 20 31 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53   1.#define TRANS
14900 5f 57 52 49 54 45 20 32 0a 0a 2f 2a 0a 2a 2a 20  _WRITE 2../*.** 
14901 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
14902 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72 65  his object repre
14903 73 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20 64  sents a single d
14904 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a  atabase file..**
14905 20 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 64 61   .** A single da
14906 74 61 62 61 73 65 20 66 69 6c 65 20 63 61 6e 20  tabase file can 
14907 62 65 20 69 6e 20 75 73 65 20 61 73 20 74 68 65  be in use as the
14908 20 73 61 6d 65 20 74 69 6d 65 20 62 79 20 74 77   same time by tw
14909 6f 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20 64 61 74  o.** or more dat
1490a 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1490b 73 2e 20 20 57 68 65 6e 20 74 77 6f 20 6f 72 20  s.  When two or 
1490c 6d 6f 72 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  more connections
1490d 20 61 72 65 0a 2a 2a 20 73 68 61 72 69 6e 67 20   are.** sharing 
1490e 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
1490f 65 20 66 69 6c 65 2c 20 65 61 63 68 20 63 6f 6e  e file, each con
14910 6e 65 63 74 69 6f 6e 20 68 61 73 20 69 74 20 6f  nection has it o
14911 77 6e 0a 2a 2a 20 70 72 69 76 61 74 65 20 42 74  wn.** private Bt
14912 72 65 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 74  ree object for t
14913 68 65 20 66 69 6c 65 20 61 6e 64 20 65 61 63 68  he file and each
14914 20 6f 66 20 74 68 6f 73 65 20 42 74 72 65 65 73   of those Btrees
14915 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 74 68   points.** to th
14916 69 73 20 6f 6e 65 20 42 74 53 68 61 72 65 64 20  is one BtShared 
14917 6f 62 6a 65 63 74 2e 20 20 42 74 53 68 61 72 65  object.  BtShare
14918 64 2e 6e 52 65 66 20 69 73 20 74 68 65 20 6e 75  d.nRef is the nu
14919 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 6f 6e 6e 65  mber of.** conne
1491a 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 6c 79  ctions currently
1491b 20 73 68 61 72 69 6e 67 20 74 68 69 73 20 64 61   sharing this da
1491c 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a  tabase file..**.
1491d 2a 2a 20 46 69 65 6c 64 73 20 69 6e 20 74 68 69  ** Fields in thi
1491e 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20  s structure are 
1491f 61 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 74  accessed under t
14920 68 65 20 42 74 53 68 61 72 65 64 2e 6d 75 74 65  he BtShared.mute
14921 78 0a 2a 2a 20 6d 75 74 65 78 2c 20 65 78 63 65  x.** mutex, exce
14922 70 74 20 66 6f 72 20 6e 52 65 66 20 61 6e 64 20  pt for nRef and 
14923 70 4e 65 78 74 20 77 68 69 63 68 20 61 72 65 20  pNext which are 
14924 61 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 74  accessed under t
14925 68 65 0a 2a 2a 20 67 6c 6f 62 61 6c 20 53 51 4c  he.** global SQL
14926 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
14927 5f 4d 41 53 54 45 52 20 6d 75 74 65 78 2e 20 20  _MASTER mutex.  
14928 54 68 65 20 70 50 61 67 65 72 20 66 69 65 6c 64  The pPager field
14929 0a 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d  .** may not be m
1492a 6f 64 69 66 69 65 64 20 6f 6e 63 65 20 69 74 20  odified once it 
1492b 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 73 65 74  is initially set
1492c 20 61 73 20 6c 6f 6e 67 20 61 73 20 6e 52 65 66   as long as nRef
1492d 3e 30 2e 0a 2a 2a 20 54 68 65 20 70 53 63 68 65  >0..** The pSche
1492e 6d 61 20 66 69 65 6c 64 20 6d 61 79 20 62 65 20  ma field may be 
1492f 73 65 74 20 6f 6e 63 65 20 75 6e 64 65 72 20 42  set once under B
14930 74 53 68 61 72 65 64 2e 6d 75 74 65 78 20 61 6e  tShared.mutex an
14931 64 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 20  d.** thereafter 
14932 69 73 20 75 6e 63 68 61 6e 67 65 64 20 61 73 20  is unchanged as 
14933 6c 6f 6e 67 20 61 73 20 6e 52 65 66 3e 30 2e 0a  long as nRef>0..
14934 2a 2a 0a 2a 2a 20 69 73 50 65 6e 64 69 6e 67 3a  **.** isPending:
14935 0a 2a 2a 0a 2a 2a 20 20 20 49 66 20 61 20 42 74  .**.**   If a Bt
14936 53 68 61 72 65 64 20 63 6c 69 65 6e 74 20 66 61  Shared client fa
14937 69 6c 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 20  ils to obtain a 
14938 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 61 20  write-lock on a 
14939 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 74 61  database.**   ta
1493a 62 6c 65 20 28 62 65 63 61 75 73 65 20 74 68 65  ble (because the
1493b 72 65 20 65 78 69 73 74 73 20 6f 6e 65 20 6f 72  re exists one or
1493c 20 6d 6f 72 65 20 72 65 61 64 2d 6c 6f 63 6b 73   more read-locks
1493d 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 29 2c 0a   on the table),.
1493e 2a 2a 20 20 20 74 68 65 20 73 68 61 72 65 64 2d  **   the shared-
1493f 63 61 63 68 65 20 65 6e 74 65 72 73 20 27 70 65  cache enters 'pe
14940 6e 64 69 6e 67 2d 6c 6f 63 6b 27 20 73 74 61 74  nding-lock' stat
14941 65 20 61 6e 64 20 69 73 50 65 6e 64 69 6e 67 20  e and isPending 
14942 69 73 0a 2a 2a 20 20 20 73 65 74 20 74 6f 20 74  is.**   set to t
14943 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65  rue..**.**   The
14944 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c 65   shared-cache le
14945 61 76 65 73 20 74 68 65 20 27 70 65 6e 64 69 6e  aves the 'pendin
14946 67 20 6c 6f 63 6b 27 20 73 74 61 74 65 20 77 68  g lock' state wh
14947 65 6e 20 65 69 74 68 65 72 20 6f 66 0a 2a 2a 20  en either of.** 
14948 20 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    the following 
14949 6f 63 63 75 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  occur:.**.**    
1494a 20 31 29 20 54 68 65 20 63 75 72 72 65 6e 74 20   1) The current 
1494b 77 72 69 74 65 72 20 28 42 74 53 68 61 72 65 64  writer (BtShared
1494c 2e 70 57 72 69 74 65 72 29 20 63 6f 6e 63 6c 75  .pWriter) conclu
1494d 64 65 73 20 69 74 73 20 74 72 61 6e 73 61 63 74  des its transact
1494e 69 6f 6e 2c 20 4f 52 0a 2a 2a 20 20 20 20 20 32  ion, OR.**     2
1494f 29 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  ) The number of 
14950 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 6f 74  locks held by ot
14951 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  her connections 
14952 64 72 6f 70 73 20 74 6f 20 7a 65 72 6f 2e 0a 2a  drops to zero..*
14953 2a 0a 2a 2a 20 20 20 77 68 69 6c 65 20 69 6e 20  *.**   while in 
14954 74 68 65 20 27 70 65 6e 64 69 6e 67 2d 6c 6f 63  the 'pending-loc
14955 6b 27 20 73 74 61 74 65 2c 20 6e 6f 20 63 6f 6e  k' state, no con
14956 6e 65 63 74 69 6f 6e 20 6d 61 79 20 73 74 61 72  nection may star
14957 74 20 61 20 6e 65 77 0a 2a 2a 20 20 20 74 72 61  t a new.**   tra
14958 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  nsaction..**.** 
14959 20 20 54 68 69 73 20 66 65 61 74 75 72 65 20 69    This feature i
1495a 73 20 69 6e 63 6c 75 64 65 64 20 74 6f 20 68 65  s included to he
1495b 6c 70 20 70 72 65 76 65 6e 74 20 77 72 69 74 65  lp prevent write
1495c 72 2d 73 74 61 72 76 61 74 69 6f 6e 2e 0a 2a 2f  r-starvation..*/
1495d 0a 73 74 72 75 63 74 20 42 74 53 68 61 72 65 64  .struct BtShared
1495e 20 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67   {.  Pager *pPag
1495f 65 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68  er;        /* Th
14960 65 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f 0a  e page cache */.
14961 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
14962 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
14963 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63  ase connection c
14964 75 72 72 65 6e 74 6c 79 20 75 73 69 6e 67 20 74  urrently using t
14965 68 69 73 20 42 74 72 65 65 20 2a 2f 0a 20 20 42  his Btree */.  B
14966 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  tCursor *pCursor
14967 3b 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f  ;    /* A list o
14968 66 20 61 6c 6c 20 6f 70 65 6e 20 63 75 72 73 6f  f all open curso
14969 72 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  rs */.  MemPage 
1496a 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20 2f 2a  *pPage1;      /*
1496b 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20 74   First page of t
1496c 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  he database */. 
1496d 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20   u8 readOnly;   
1496e 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
1496f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  f the underlying
14970 20 66 69 6c 65 20 69 73 20 72 65 61 64 6f 6e 6c   file is readonl
14971 79 20 2a 2f 0a 20 20 75 38 20 70 61 67 65 53 69  y */.  u8 pageSi
14972 7a 65 46 69 78 65 64 3b 20 20 20 20 20 2f 2a 20  zeFixed;     /* 
14973 54 72 75 65 20 69 66 20 74 68 65 20 70 61 67 65  True if the page
14974 20 73 69 7a 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e   size can no lon
14975 67 65 72 20 62 65 20 63 68 61 6e 67 65 64 20 2a  ger be changed *
14976 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
14977 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
14978 0a 20 20 75 38 20 61 75 74 6f 56 61 63 75 75 6d  .  u8 autoVacuum
14979 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ;        /* True
1497a 20 69 66 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   if auto-vacuum 
1497b 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20  is enabled */.  
1497c 75 38 20 69 6e 63 72 56 61 63 75 75 6d 3b 20 20  u8 incrVacuum;  
1497d 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
1497e 20 69 6e 63 72 2d 76 61 63 75 75 6d 20 69 73 20   incr-vacuum is 
1497f 65 6e 61 62 6c 65 64 20 2a 2f 0a 23 65 6e 64 69  enabled */.#endi
14980 66 0a 20 20 75 31 36 20 70 61 67 65 53 69 7a 65  f.  u16 pageSize
14981 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74  ;         /* Tot
14982 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  al number of byt
14983 65 73 20 6f 6e 20 61 20 70 61 67 65 20 2a 2f 0a  es on a page */.
14984 20 20 75 31 36 20 75 73 61 62 6c 65 53 69 7a 65    u16 usableSize
14985 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ;       /* Numbe
14986 72 20 6f 66 20 75 73 61 62 6c 65 20 62 79 74 65  r of usable byte
14987 73 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a  s on each page *
14988 2f 0a 20 20 75 31 36 20 6d 61 78 4c 6f 63 61 6c  /.  u16 maxLocal
14989 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78  ;         /* Max
1498a 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f  imum local paylo
1498b 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 44 41  ad in non-LEAFDA
1498c 54 41 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 75  TA tables */.  u
1498d 31 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20  16 minLocal;    
1498e 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20       /* Minimum 
1498f 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e  local payload in
14990 20 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 20 74 61   non-LEAFDATA ta
14991 62 6c 65 73 20 2a 2f 0a 20 20 75 31 36 20 6d 61  bles */.  u16 ma
14992 78 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20  xLeaf;          
14993 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 6c  /* Maximum local
14994 20 70 61 79 6c 6f 61 64 20 69 6e 20 61 20 4c 45   payload in a LE
14995 41 46 44 41 54 41 20 74 61 62 6c 65 20 2a 2f 0a  AFDATA table */.
14996 20 20 75 31 36 20 6d 69 6e 4c 65 61 66 3b 20 20    u16 minLeaf;  
14997 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d          /* Minim
14998 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64  um local payload
14999 20 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74   in a LEAFDATA t
1499a 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54  able */.  u8 inT
1499b 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20 20  ransaction;     
1499c 2f 2a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 73  /* Transaction s
1499d 74 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 54  tate */.  int nT
1499e 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20 20  ransaction;     
1499f 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 70 65  /* Number of ope
149a0 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 28  n transactions (
149a1 72 65 61 64 20 2b 20 77 72 69 74 65 29 20 2a 2f  read + write) */
149a2 0a 20 20 76 6f 69 64 20 2a 70 53 63 68 65 6d 61  .  void *pSchema
149a3 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  ;        /* Poin
149a4 74 65 72 20 74 6f 20 73 70 61 63 65 20 61 6c 6c  ter to space all
149a5 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65  ocated by sqlite
149a6 33 42 74 72 65 65 53 63 68 65 6d 61 28 29 20 2a  3BtreeSchema() *
149a7 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 72 65 65  /.  void (*xFree
149a8 53 63 68 65 6d 61 29 28 76 6f 69 64 2a 29 3b 20  Schema)(void*); 
149a9 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66   /* Destructor f
149aa 6f 72 20 42 74 53 68 61 72 65 64 2e 70 53 63 68  or BtShared.pSch
149ab 65 6d 61 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ema */.  sqlite3
149ac 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 2f  _mutex *mutex; /
149ad 2a 20 4e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20  * Non-recursive 
149ae 6d 75 74 65 78 20 72 65 71 75 69 72 65 64 20 74  mutex required t
149af 6f 20 61 63 63 65 73 73 20 74 68 69 73 20 73 74  o access this st
149b0 72 75 63 74 20 2a 2f 0a 20 20 42 69 74 76 65 63  ruct */.  Bitvec
149b1 20 2a 70 48 61 73 43 6f 6e 74 65 6e 74 3b 20 20   *pHasContent;  
149b2 2f 2a 20 53 65 74 20 6f 66 20 70 61 67 65 73 20  /* Set of pages 
149b3 6d 6f 76 65 64 20 74 6f 20 66 72 65 65 2d 6c 69  moved to free-li
149b4 73 74 20 74 68 69 73 20 74 72 61 6e 73 61 63 74  st this transact
149b5 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ion */.#ifndef S
149b6 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
149b7 44 5f 43 41 43 48 45 0a 20 20 69 6e 74 20 6e 52  D_CACHE.  int nR
149b8 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ef;             
149b9 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66  /* Number of ref
149ba 65 72 65 6e 63 65 73 20 74 6f 20 74 68 69 73 20  erences to this 
149bb 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 42  structure */.  B
149bc 74 53 68 61 72 65 64 20 2a 70 4e 65 78 74 3b 20  tShared *pNext; 
149bd 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 6f 6e 20       /* Next on 
149be 61 20 6c 69 73 74 20 6f 66 20 73 68 61 72 61 62  a list of sharab
149bf 6c 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75  le BtShared stru
149c0 63 74 73 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b 20  cts */.  BtLock 
149c1 2a 70 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f  *pLock;        /
149c2 2a 20 4c 69 73 74 20 6f 66 20 6c 6f 63 6b 73 20  * List of locks 
149c3 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 73 68 61  held on this sha
149c4 72 65 64 2d 62 74 72 65 65 20 73 74 72 75 63 74  red-btree struct
149c5 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 57 72   */.  Btree *pWr
149c6 69 74 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 42  iter;       /* B
149c7 74 72 65 65 20 77 69 74 68 20 63 75 72 72 65 6e  tree with curren
149c8 74 6c 79 20 6f 70 65 6e 20 77 72 69 74 65 20 74  tly open write t
149c9 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20  ransaction */.  
149ca 75 38 20 69 73 45 78 63 6c 75 73 69 76 65 3b 20  u8 isExclusive; 
149cb 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
149cc 20 70 57 72 69 74 65 72 20 68 61 73 20 61 6e 20   pWriter has an 
149cd 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f  EXCLUSIVE lock o
149ce 6e 20 74 68 65 20 64 62 20 2a 2f 0a 20 20 75 38  n the db */.  u8
149cf 20 69 73 50 65 6e 64 69 6e 67 3b 20 20 20 20 20   isPending;     
149d0 20 20 20 20 2f 2a 20 49 66 20 77 61 69 74 69 6e      /* If waitin
149d1 67 20 66 6f 72 20 72 65 61 64 2d 6c 6f 63 6b 73  g for read-locks
149d2 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 23 65 6e   to clear */.#en
149d3 64 69 66 0a 20 20 75 38 20 2a 70 54 6d 70 53 70  dif.  u8 *pTmpSp
149d4 61 63 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42  ace;        /* B
149d5 74 53 68 61 72 65 64 2e 70 61 67 65 53 69 7a 65  tShared.pageSize
149d6 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20   bytes of space 
149d7 66 6f 72 20 74 6d 70 20 75 73 65 20 2a 2f 0a 7d  for tmp use */.}
149d8 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  ;../*.** An inst
149d9 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
149da 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
149db 69 73 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20  is used to hold 
149dc 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61  information.** a
149dd 62 6f 75 74 20 61 20 63 65 6c 6c 2e 20 20 54 68  bout a cell.  Th
149de 65 20 70 61 72 73 65 43 65 6c 6c 50 74 72 28 29  e parseCellPtr()
149df 20 66 75 6e 63 74 69 6f 6e 20 66 69 6c 6c 73 20   function fills 
149e0 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72  in this structur
149e1 65 0a 2a 2a 20 62 61 73 65 64 20 6f 6e 20 69 6e  e.** based on in
149e2 66 6f 72 6d 61 74 69 6f 6e 20 65 78 74 72 61 63  formation extrac
149e3 74 20 66 72 6f 6d 20 74 68 65 20 72 61 77 20 64  t from the raw d
149e4 69 73 6b 20 70 61 67 65 2e 0a 2a 2f 0a 74 79 70  isk page..*/.typ
149e5 65 64 65 66 20 73 74 72 75 63 74 20 43 65 6c 6c  edef struct Cell
149e6 49 6e 66 6f 20 43 65 6c 6c 49 6e 66 6f 3b 0a 73  Info CellInfo;.s
149e7 74 72 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20 7b  truct CellInfo {
149e8 0a 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20  .  u8 *pCell;   
149e9 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
149ea 74 68 65 20 73 74 61 72 74 20 6f 66 20 63 65 6c  the start of cel
149eb 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69  l content */.  i
149ec 36 34 20 6e 4b 65 79 3b 20 20 20 20 20 20 2f 2a  64 nKey;      /*
149ed 20 54 68 65 20 6b 65 79 20 66 6f 72 20 49 4e 54   The key for INT
149ee 4b 45 59 20 74 61 62 6c 65 73 2c 20 6f 72 20 6e  KEY tables, or n
149ef 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
149f0 6e 20 6b 65 79 20 2a 2f 0a 20 20 75 33 32 20 6e  n key */.  u32 n
149f1 44 61 74 61 3b 20 20 20 20 20 2f 2a 20 4e 75 6d  Data;     /* Num
149f2 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
149f3 64 61 74 61 20 2a 2f 0a 20 20 75 33 32 20 6e 50  data */.  u32 nP
149f4 61 79 6c 6f 61 64 3b 20 20 2f 2a 20 54 6f 74 61  ayload;  /* Tota
149f5 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c  l amount of payl
149f6 6f 61 64 20 2a 2f 0a 20 20 75 31 36 20 6e 48 65  oad */.  u16 nHe
149f7 61 64 65 72 3b 20 20 20 2f 2a 20 53 69 7a 65 20  ader;   /* Size 
149f8 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  of the cell cont
149f9 65 6e 74 20 68 65 61 64 65 72 20 69 6e 20 62 79  ent header in by
149fa 74 65 73 20 2a 2f 0a 20 20 75 31 36 20 6e 4c 6f  tes */.  u16 nLo
149fb 63 61 6c 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e  cal;    /* Amoun
149fc 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c  t of payload hel
149fd 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 75  d locally */.  u
149fe 31 36 20 69 4f 76 65 72 66 6c 6f 77 3b 20 2f 2a  16 iOverflow; /*
149ff 20 4f 66 66 73 65 74 20 74 6f 20 6f 76 65 72 66   Offset to overf
14a00 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72 2e  low page number.
14a01 20 20 5a 65 72 6f 20 69 66 20 6e 6f 20 6f 76 65    Zero if no ove
14a02 72 66 6c 6f 77 20 2a 2f 0a 20 20 75 31 36 20 6e  rflow */.  u16 n
14a03 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 53 69 7a  Size;     /* Siz
14a04 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f  e of the cell co
14a05 6e 74 65 6e 74 20 6f 6e 20 74 68 65 20 6d 61 69  ntent on the mai
14a06 6e 20 62 2d 74 72 65 65 20 70 61 67 65 20 2a 2f  n b-tree page */
14a07 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d  .};../*.** Maxim
14a08 75 6d 20 64 65 70 74 68 20 6f 66 20 61 6e 20 53  um depth of an S
14a09 51 4c 69 74 65 20 42 2d 54 72 65 65 20 73 74 72  QLite B-Tree str
14a0a 75 63 74 75 72 65 2e 20 41 6e 79 20 42 2d 54 72  ucture. Any B-Tr
14a0b 65 65 20 64 65 65 70 65 72 20 74 68 61 6e 0a 2a  ee deeper than.*
14a0c 2a 20 74 68 69 73 20 77 69 6c 6c 20 62 65 20 64  * this will be d
14a0d 65 63 6c 61 72 65 64 20 63 6f 72 72 75 70 74 2e  eclared corrupt.
14a0e 20 54 68 69 73 20 76 61 6c 75 65 20 69 73 20 63   This value is c
14a0f 61 6c 63 75 6c 61 74 65 64 20 62 61 73 65 64 20  alculated based 
14a10 6f 6e 20 61 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20  on a.** maximum 
14a11 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 6f 66  database size of
14a12 20 32 5e 33 31 20 70 61 67 65 73 20 61 20 6d 69   2^31 pages a mi
14a13 6e 69 6d 75 6d 20 66 61 6e 6f 75 74 20 6f 66 20  nimum fanout of 
14a14 32 20 66 6f 72 20 61 0a 2a 2a 20 72 6f 6f 74 2d  2 for a.** root-
14a15 6e 6f 64 65 20 61 6e 64 20 33 20 66 6f 72 20 61  node and 3 for a
14a16 6c 6c 20 6f 74 68 65 72 20 69 6e 74 65 72 6e 61  ll other interna
14a17 6c 20 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 49  l nodes..**.** I
14a18 66 20 61 20 74 72 65 65 20 74 68 61 74 20 61 70  f a tree that ap
14a19 70 65 61 72 73 20 74 6f 20 62 65 20 74 61 6c 6c  pears to be tall
14a1a 65 72 20 74 68 61 6e 20 74 68 69 73 20 69 73 20  er than this is 
14a1b 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 69 74 20  encountered, it 
14a1c 69 73 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74 68  is.** assumed th
14a1d 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20  at the database 
14a1e 69 73 20 63 6f 72 72 75 70 74 2e 0a 2a 2f 0a 23  is corrupt..*/.#
14a1f 64 65 66 69 6e 65 20 42 54 43 55 52 53 4f 52 5f  define BTCURSOR_
14a20 4d 41 58 5f 44 45 50 54 48 20 32 30 0a 0a 2f 2a  MAX_DEPTH 20../*
14a21 0a 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20  .** A cursor is 
14a22 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70  a pointer to a p
14a23 61 72 74 69 63 75 6c 61 72 20 65 6e 74 72 79 20  articular entry 
14a24 77 69 74 68 69 6e 20 61 20 70 61 72 74 69 63 75  within a particu
14a25 6c 61 72 0a 2a 2a 20 62 2d 74 72 65 65 20 77 69  lar.** b-tree wi
14a26 74 68 69 6e 20 61 20 64 61 74 61 62 61 73 65 20  thin a database 
14a27 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  file..**.** The 
14a28 65 6e 74 72 79 20 69 73 20 69 64 65 6e 74 69 66  entry is identif
14a29 69 65 64 20 62 79 20 69 74 73 20 4d 65 6d 50 61  ied by its MemPa
14a2a 67 65 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78  ge and the index
14a2b 20 69 6e 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61   in.** MemPage.a
14a2c 43 65 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 65 6e  Cell[] of the en
14a2d 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  try..**.** When 
14a2e 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73  a single databas
14a2f 65 20 66 69 6c 65 20 63 61 6e 20 73 68 61 72 65  e file can share
14a30 64 20 62 79 20 74 77 6f 20 6d 6f 72 65 20 64 61  d by two more da
14a31 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
14a32 6e 73 2c 0a 2a 2a 20 62 75 74 20 63 75 72 73 6f  ns,.** but curso
14a33 72 73 20 63 61 6e 6e 6f 74 20 62 65 20 73 68 61  rs cannot be sha
14a34 72 65 64 2e 20 20 45 61 63 68 20 63 75 72 73 6f  red.  Each curso
14a35 72 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20  r is associated 
14a36 77 69 74 68 20 61 0a 2a 2a 20 70 61 72 74 69 63  with a.** partic
14a37 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f  ular database co
14a38 6e 6e 65 63 74 69 6f 6e 20 69 64 65 6e 74 69 66  nnection identif
14a39 69 65 64 20 42 74 43 75 72 73 6f 72 2e 70 42 74  ied BtCursor.pBt
14a3a 72 65 65 2e 64 62 2e 0a 2a 2a 0a 2a 2a 20 46 69  ree.db..**.** Fi
14a3b 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 72  elds in this str
14a3c 75 63 74 75 72 65 20 61 72 65 20 61 63 63 65 73  ucture are acces
14a3d 73 65 64 20 75 6e 64 65 72 20 74 68 65 20 42 74  sed under the Bt
14a3e 53 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20  Shared.mutex.** 
14a3f 66 6f 75 6e 64 20 61 74 20 73 65 6c 66 2d 3e 70  found at self->p
14a40 42 74 2d 3e 6d 75 74 65 78 2e 20 0a 2a 2f 0a 73  Bt->mutex. .*/.s
14a41 74 72 75 63 74 20 42 74 43 75 72 73 6f 72 20 7b  truct BtCursor {
14a42 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65  .  Btree *pBtree
14a43 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
14a44 54 68 65 20 42 74 72 65 65 20 74 6f 20 77 68 69  The Btree to whi
14a45 63 68 20 74 68 69 73 20 63 75 72 73 6f 72 20 62  ch this cursor b
14a46 65 6c 6f 6e 67 73 20 2a 2f 0a 20 20 42 74 53 68  elongs */.  BtSh
14a47 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20  ared *pBt;      
14a48 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74 53        /* The BtS
14a49 68 61 72 65 64 20 74 68 69 73 20 63 75 72 73 6f  hared this curso
14a4a 72 20 70 6f 69 6e 74 73 20 74 6f 20 2a 2f 0a 20  r points to */. 
14a4b 20 42 74 43 75 72 73 6f 72 20 2a 70 4e 65 78 74   BtCursor *pNext
14a4c 2c 20 2a 70 50 72 65 76 3b 20 20 2f 2a 20 46 6f  , *pPrev;  /* Fo
14a4d 72 6d 73 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73  rms a linked lis
14a4e 74 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72 73  t of all cursors
14a4f 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79   */.  struct Key
14a50 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20  Info *pKeyInfo; 
14a51 2f 2a 20 41 72 67 75 6d 65 6e 74 20 70 61 73 73  /* Argument pass
14a52 65 64 20 74 6f 20 63 6f 6d 70 61 72 69 73 6f 6e  ed to comparison
14a53 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 50   function */.  P
14a54 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 20 20 20  gno pgnoRoot;   
14a55 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
14a56 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 69  root page of thi
14a57 73 20 74 72 65 65 20 2a 2f 0a 20 20 73 71 6c 69  s tree */.  sqli
14a58 74 65 33 5f 69 6e 74 36 34 20 63 61 63 68 65 64  te3_int64 cached
14a59 52 6f 77 69 64 3b 20 2f 2a 20 4e 65 78 74 20 72  Rowid; /* Next r
14a5a 6f 77 69 64 20 63 61 63 68 65 2e 20 20 30 20 6d  owid cache.  0 m
14a5b 65 61 6e 73 20 6e 6f 74 20 76 61 6c 69 64 20 2a  eans not valid *
14a5c 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66  /.  CellInfo inf
14a5d 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  o;            /*
14a5e 20 41 20 70 61 72 73 65 20 6f 66 20 74 68 65 20   A parse of the 
14a5f 63 65 6c 6c 20 77 65 20 61 72 65 20 70 6f 69 6e  cell we are poin
14a60 74 69 6e 67 20 61 74 20 2a 2f 0a 20 20 75 38 20  ting at */.  u8 
14a61 77 72 46 6c 61 67 3b 20 20 20 20 20 20 20 20 20  wrFlag;         
14a62 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
14a63 66 20 77 72 69 74 61 62 6c 65 20 2a 2f 0a 20 20  f writable */.  
14a64 75 38 20 61 74 4c 61 73 74 3b 20 20 20 20 20 20  u8 atLast;      
14a65 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
14a66 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20  sor pointing to 
14a67 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 2a  the last entry *
14a68 2f 0a 20 20 75 38 20 76 61 6c 69 64 4e 4b 65 79  /.  u8 validNKey
14a69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
14a6a 20 54 72 75 65 20 69 66 20 69 6e 66 6f 2e 6e 4b   True if info.nK
14a6b 65 79 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20  ey is valid */. 
14a6c 20 75 38 20 65 53 74 61 74 65 3b 20 20 20 20 20   u8 eState;     
14a6d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
14a6e 65 20 6f 66 20 74 68 65 20 43 55 52 53 4f 52 5f  e of the CURSOR_
14a6f 58 58 58 20 63 6f 6e 73 74 61 6e 74 73 20 28 73  XXX constants (s
14a70 65 65 20 62 65 6c 6f 77 29 20 2a 2f 0a 20 20 76  ee below) */.  v
14a71 6f 69 64 20 2a 70 4b 65 79 3b 20 20 20 20 20 20  oid *pKey;      
14a72 2f 2a 20 53 61 76 65 64 20 6b 65 79 20 74 68 61  /* Saved key tha
14a73 74 20 77 61 73 20 63 75 72 73 6f 72 27 73 20 6c  t was cursor's l
14a74 61 73 74 20 6b 6e 6f 77 6e 20 70 6f 73 69 74 69  ast known positi
14a75 6f 6e 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 79  on */.  i64 nKey
14a76 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65  ;        /* Size
14a77 20 6f 66 20 70 4b 65 79 2c 20 6f 72 20 6c 61 73   of pKey, or las
14a78 74 20 69 6e 74 65 67 65 72 20 6b 65 79 20 2a 2f  t integer key */
14a79 0a 20 20 69 6e 74 20 73 6b 69 70 4e 65 78 74 3b  .  int skipNext;
14a7a 20 20 20 20 2f 2a 20 50 72 65 76 28 29 20 69 73      /* Prev() is
14a7b 20 6e 6f 6f 70 20 69 66 20 6e 65 67 61 74 69 76   noop if negativ
14a7c 65 2e 20 4e 65 78 74 28 29 20 69 73 20 6e 6f 6f  e. Next() is noo
14a7d 70 20 69 66 20 70 6f 73 69 74 69 76 65 20 2a 2f  p if positive */
14a7e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
14a7f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20  OMIT_INCRBLOB.  
14a80 75 38 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e  u8 isIncrblobHan
14a81 64 6c 65 3b 20 20 20 20 20 20 2f 2a 20 54 72 75  dle;      /* Tru
14a82 65 20 69 66 20 74 68 69 73 20 63 75 72 73 6f 72  e if this cursor
14a83 20 69 73 20 61 6e 20 69 6e 63 72 2e 20 69 6f 20   is an incr. io 
14a84 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f  handle */.  Pgno
14a85 20 2a 61 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20   *aOverflow;    
14a86 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 6f        /* Cache o
14a87 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  f overflow page 
14a88 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e  locations */.#en
14a89 64 69 66 0a 20 20 69 31 36 20 69 50 61 67 65 3b  dif.  i16 iPage;
14a8a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14a8b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
14a8c 6e 64 65 78 20 6f 66 20 63 75 72 72 65 6e 74 20  ndex of current 
14a8d 70 61 67 65 20 69 6e 20 61 70 50 61 67 65 20 2a  page in apPage *
14a8e 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 50  /.  MemPage *apP
14a8f 61 67 65 5b 42 54 43 55 52 53 4f 52 5f 4d 41 58  age[BTCURSOR_MAX
14a90 5f 44 45 50 54 48 5d 3b 20 20 2f 2a 20 50 61 67  _DEPTH];  /* Pag
14a91 65 73 20 66 72 6f 6d 20 72 6f 6f 74 20 74 6f 20  es from root to 
14a92 63 75 72 72 65 6e 74 20 70 61 67 65 20 2a 2f 0a  current page */.
14a93 20 20 75 31 36 20 61 69 49 64 78 5b 42 54 43 55    u16 aiIdx[BTCU
14a94 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b  RSOR_MAX_DEPTH];
14a95 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65          /* Curre
14a96 6e 74 20 69 6e 64 65 78 20 69 6e 20 61 70 50 61  nt index in apPa
14a97 67 65 5b 69 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  ge[i] */.};../*.
14a98 2a 2a 20 50 6f 74 65 6e 74 69 61 6c 20 76 61 6c  ** Potential val
14a99 75 65 73 20 66 6f 72 20 42 74 43 75 72 73 6f 72  ues for BtCursor
14a9a 2e 65 53 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 43  .eState..**.** C
14a9b 55 52 53 4f 52 5f 56 41 4c 49 44 3a 0a 2a 2a 20  URSOR_VALID:.** 
14a9c 20 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 73 20    Cursor points 
14a9d 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72 79  to a valid entry
14a9e 2e 20 67 65 74 50 61 79 6c 6f 61 64 28 29 20 65  . getPayload() e
14a9f 74 63 2e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65  tc. may be calle
14aa0 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f  d..**.** CURSOR_
14aa1 49 4e 56 41 4c 49 44 3a 0a 2a 2a 20 20 20 43 75  INVALID:.**   Cu
14aa2 72 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20 70 6f  rsor does not po
14aa3 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 20 65  int to a valid e
14aa4 6e 74 72 79 2e 20 54 68 69 73 20 63 61 6e 20 68  ntry. This can h
14aa5 61 70 70 65 6e 20 28 66 6f 72 20 65 78 61 6d 70  appen (for examp
14aa6 6c 65 29 20 0a 2a 2a 20 20 20 62 65 63 61 75 73  le) .**   becaus
14aa7 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65  e the table is e
14aa8 6d 70 74 79 20 6f 72 20 62 65 63 61 75 73 65 20  mpty or because 
14aa9 42 74 72 65 65 43 75 72 73 6f 72 46 69 72 73 74  BtreeCursorFirst
14aaa 28 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 0a  () has not been.
14aab 2a 2a 20 20 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a  **   called..**.
14aac 2a 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52  ** CURSOR_REQUIR
14aad 45 53 45 45 4b 3a 0a 2a 2a 20 20 20 54 68 65 20  ESEEK:.**   The 
14aae 74 61 62 6c 65 20 74 68 61 74 20 74 68 69 73 20  table that this 
14aaf 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65  cursor was opene
14ab0 64 20 6f 6e 20 73 74 69 6c 6c 20 65 78 69 73 74  d on still exist
14ab1 73 2c 20 62 75 74 20 68 61 73 20 62 65 65 6e 20  s, but has been 
14ab2 0a 2a 2a 20 20 20 6d 6f 64 69 66 69 65 64 20 73  .**   modified s
14ab3 69 6e 63 65 20 74 68 65 20 63 75 72 73 6f 72 20  ince the cursor 
14ab4 77 61 73 20 6c 61 73 74 20 75 73 65 64 2e 20 54  was last used. T
14ab5 68 65 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69  he cursor positi
14ab6 6f 6e 20 69 73 20 73 61 76 65 64 0a 2a 2a 20 20  on is saved.**  
14ab7 20 69 6e 20 76 61 72 69 61 62 6c 65 73 20 42 74   in variables Bt
14ab8 43 75 72 73 6f 72 2e 70 4b 65 79 20 61 6e 64 20  Cursor.pKey and 
14ab9 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 2e 20 57  BtCursor.nKey. W
14aba 68 65 6e 20 61 20 63 75 72 73 6f 72 20 69 73 20  hen a cursor is 
14abb 69 6e 20 0a 2a 2a 20 20 20 74 68 69 73 20 73 74  in .**   this st
14abc 61 74 65 2c 20 72 65 73 74 6f 72 65 43 75 72 73  ate, restoreCurs
14abd 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 63 61 6e  orPosition() can
14abe 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 61 74   be called to at
14abf 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 73 65  tempt to.**   se
14ac0 65 6b 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f  ek the cursor to
14ac1 20 74 68 65 20 73 61 76 65 64 20 70 6f 73 69 74   the saved posit
14ac2 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f  ion..**.** CURSO
14ac3 52 5f 46 41 55 4c 54 3a 0a 2a 2a 20 20 20 41 20  R_FAULT:.**   A 
14ac4 75 6e 72 65 63 6f 76 65 72 61 62 6c 65 20 65 72  unrecoverable er
14ac5 72 6f 72 20 28 61 6e 20 49 2f 4f 20 65 72 72 6f  ror (an I/O erro
14ac6 72 20 6f 72 20 61 20 6d 61 6c 6c 6f 63 20 66 61  r or a malloc fa
14ac7 69 6c 75 72 65 29 20 68 61 73 20 6f 63 63 75 72  ilure) has occur
14ac8 72 65 64 0a 2a 2a 20 20 20 6f 6e 20 61 20 64 69  red.**   on a di
14ac9 66 66 65 72 65 6e 74 20 63 6f 6e 6e 65 63 74 69  fferent connecti
14aca 6f 6e 20 74 68 61 74 20 73 68 61 72 65 73 20 74  on that shares t
14acb 68 65 20 42 74 53 68 61 72 65 64 20 63 61 63 68  he BtShared cach
14acc 65 20 77 69 74 68 20 74 68 69 73 0a 2a 2a 20 20  e with this.**  
14acd 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 65 72   cursor.  The er
14ace 72 6f 72 20 68 61 73 20 6c 65 66 74 20 74 68 65  ror has left the
14acf 20 63 61 63 68 65 20 69 6e 20 61 6e 20 69 6e 63   cache in an inc
14ad0 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e  onsistent state.
14ad1 0a 2a 2a 20 20 20 44 6f 20 6e 6f 74 68 69 6e 67  .**   Do nothing
14ad2 20 65 6c 73 65 20 77 69 74 68 20 74 68 69 73 20   else with this 
14ad3 63 75 72 73 6f 72 2e 20 20 41 6e 79 20 61 74 74  cursor.  Any att
14ad4 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65 20  empt to use the 
14ad5 63 75 72 73 6f 72 0a 2a 2a 20 20 20 73 68 6f 75  cursor.**   shou
14ad6 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72  ld return the er
14ad7 72 6f 72 20 63 6f 64 65 20 73 74 6f 72 65 64 20  ror code stored 
14ad8 69 6e 20 42 74 43 75 72 73 6f 72 2e 73 6b 69 70  in BtCursor.skip
14ad9 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 55 52 53  .*/.#define CURS
14ada 4f 52 5f 49 4e 56 41 4c 49 44 20 20 20 20 20 20  OR_INVALID      
14adb 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 43       0.#define C
14adc 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 20 20  URSOR_VALID     
14add 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e          1.#defin
14ade 65 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  e CURSOR_REQUIRE
14adf 53 45 45 4b 20 20 20 20 20 20 20 32 0a 23 64 65  SEEK       2.#de
14ae0 66 69 6e 65 20 43 55 52 53 4f 52 5f 46 41 55 4c  fine CURSOR_FAUL
14ae1 54 20 20 20 20 20 20 20 20 20 20 20 20 20 33 0a  T             3.
14ae2 0a 2f 2a 20 0a 2a 2a 20 54 68 65 20 64 61 74 61  ./* .** The data
14ae3 62 61 73 65 20 70 61 67 65 20 74 68 65 20 50 45  base page the PE
14ae4 4e 44 49 4e 47 5f 42 59 54 45 20 6f 63 63 75 70  NDING_BYTE occup
14ae5 69 65 73 2e 20 54 68 69 73 20 70 61 67 65 20 69  ies. This page i
14ae6 73 20 6e 65 76 65 72 20 75 73 65 64 2e 0a 2a 2f  s never used..*/
14ae7 0a 23 20 64 65 66 69 6e 65 20 50 45 4e 44 49 4e  .# define PENDIN
14ae8 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29  G_BYTE_PAGE(pBt)
14ae9 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70   PAGER_MJ_PGNO(p
14aea 42 74 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65  Bt)../*.** These
14aeb 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 74   macros define t
14aec 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74  he location of t
14aed 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65  he pointer-map e
14aee 6e 74 72 79 20 66 6f 72 20 61 20 0a 2a 2a 20 64  ntry for a .** d
14aef 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 54 68  atabase page. Th
14af0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
14af1 20 74 6f 20 65 61 63 68 20 69 73 20 74 68 65 20   to each is the 
14af2 6e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65  number of usable
14af3 0a 2a 2a 20 62 79 74 65 73 20 6f 6e 20 65 61 63  .** bytes on eac
14af4 68 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61  h page of the da
14af5 74 61 62 61 73 65 20 28 6f 66 74 65 6e 20 31 30  tabase (often 10
14af6 32 34 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 20  24). The second 
14af7 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 6e  is the.** page n
14af8 75 6d 62 65 72 20 74 6f 20 6c 6f 6f 6b 20 75 70  umber to look up
14af9 20 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20   in the pointer 
14afa 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41  map..**.** PTRMA
14afb 50 5f 50 41 47 45 4e 4f 20 72 65 74 75 72 6e 73  P_PAGENO returns
14afc 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61   the database pa
14afd 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
14afe 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20   pointer-map.** 
14aff 70 61 67 65 20 74 68 61 74 20 73 74 6f 72 65 73  page that stores
14b00 20 74 68 65 20 72 65 71 75 69 72 65 64 20 70 6f   the required po
14b01 69 6e 74 65 72 2e 20 50 54 52 4d 41 50 5f 50 54  inter. PTRMAP_PT
14b02 52 4f 46 46 53 45 54 20 72 65 74 75 72 6e 73 0a  ROFFSET returns.
14b03 2a 2a 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66  ** the offset of
14b04 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6d   the requested m
14b05 61 70 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20  ap entry..**.** 
14b06 49 66 20 74 68 65 20 70 67 6e 6f 20 61 72 67 75  If the pgno argu
14b07 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 50  ment passed to P
14b08 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20 69 73 20  TRMAP_PAGENO is 
14b09 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61  a pointer-map pa
14b0a 67 65 2c 0a 2a 2a 20 74 68 65 6e 20 70 67 6e 6f  ge,.** then pgno
14b0b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 53 6f   is returned. So
14b0c 20 28 70 67 6e 6f 3d 3d 50 54 52 4d 41 50 5f 50   (pgno==PTRMAP_P
14b0d 41 47 45 4e 4f 28 70 67 73 7a 2c 20 70 67 6e 6f  AGENO(pgsz, pgno
14b0e 29 29 20 63 61 6e 20 62 65 0a 2a 2a 20 75 73 65  )) can be.** use
14b0f 64 20 74 6f 20 74 65 73 74 20 69 66 20 70 67 6e  d to test if pgn
14b10 6f 20 69 73 20 61 20 70 6f 69 6e 74 65 72 2d 6d  o is a pointer-m
14b11 61 70 20 70 61 67 65 2e 20 50 54 52 4d 41 50 5f  ap page. PTRMAP_
14b12 49 53 50 41 47 45 20 69 6d 70 6c 65 6d 65 6e 74  ISPAGE implement
14b13 73 0a 2a 2a 20 74 68 69 73 20 74 65 73 74 2e 0a  s.** this test..
14b14 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41  */.#define PTRMA
14b15 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 70 67  P_PAGENO(pBt, pg
14b16 6e 6f 29 20 70 74 72 6d 61 70 50 61 67 65 6e 6f  no) ptrmapPageno
14b17 28 70 42 74 2c 20 70 67 6e 6f 29 0a 23 64 65 66  (pBt, pgno).#def
14b18 69 6e 65 20 50 54 52 4d 41 50 5f 50 54 52 4f 46  ine PTRMAP_PTROF
14b19 46 53 45 54 28 70 67 70 74 72 6d 61 70 2c 20 70  FSET(pgptrmap, p
14b1a 67 6e 6f 29 20 28 35 2a 28 70 67 6e 6f 2d 70 67  gno) (5*(pgno-pg
14b1b 70 74 72 6d 61 70 2d 31 29 29 0a 23 64 65 66 69  ptrmap-1)).#defi
14b1c 6e 65 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45  ne PTRMAP_ISPAGE
14b1d 28 70 42 74 2c 20 70 67 6e 6f 29 20 28 50 54 52  (pBt, pgno) (PTR
14b1e 4d 41 50 5f 50 41 47 45 4e 4f 28 28 70 42 74 29  MAP_PAGENO((pBt)
14b1f 2c 28 70 67 6e 6f 29 29 3d 3d 28 70 67 6e 6f 29  ,(pgno))==(pgno)
14b20 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 6f 69  )../*.** The poi
14b21 6e 74 65 72 20 6d 61 70 20 69 73 20 61 20 6c 6f  nter map is a lo
14b22 6f 6b 75 70 20 74 61 62 6c 65 20 74 68 61 74 20  okup table that 
14b23 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70  identifies the p
14b24 61 72 65 6e 74 20 70 61 67 65 20 66 6f 72 0a 2a  arent page for.*
14b25 2a 20 65 61 63 68 20 63 68 69 6c 64 20 70 61 67  * each child pag
14b26 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  e in the databas
14b27 65 20 66 69 6c 65 2e 20 20 54 68 65 20 70 61 72  e file.  The par
14b28 65 6e 74 20 70 61 67 65 20 69 73 20 74 68 65 20  ent page is the 
14b29 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 63 6f 6e  page that.** con
14b2a 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  tains a pointer 
14b2b 74 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 20 45  to the child.  E
14b2c 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65  very page in the
14b2d 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
14b2e 6e 73 0a 2a 2a 20 30 20 6f 72 20 31 20 70 61 72  ns.** 0 or 1 par
14b2f 65 6e 74 20 70 61 67 65 73 2e 20 20 28 49 6e 20  ent pages.  (In 
14b30 74 68 69 73 20 63 6f 6e 74 65 78 74 20 27 64 61  this context 'da
14b31 74 61 62 61 73 65 20 70 61 67 65 27 20 72 65 66  tabase page' ref
14b32 65 72 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 70 61  ers.** to any pa
14b33 67 65 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70  ge that is not p
14b34 61 72 74 20 6f 66 20 74 68 65 20 70 6f 69 6e 74  art of the point
14b35 65 72 20 6d 61 70 20 69 74 73 65 6c 66 2e 29 20  er map itself.) 
14b36 20 45 61 63 68 20 70 6f 69 6e 74 65 72 20 6d 61   Each pointer ma
14b37 70 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 6e 73 69  p.** entry consi
14b38 73 74 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20  sts of a single 
14b39 62 79 74 65 20 27 74 79 70 65 27 20 61 6e 64 20  byte 'type' and 
14b3a 61 20 34 20 62 79 74 65 20 70 61 72 65 6e 74 20  a 4 byte parent 
14b3b 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20  page number..** 
14b3c 54 68 65 20 50 54 52 4d 41 50 5f 58 58 58 20 69  The PTRMAP_XXX i
14b3d 64 65 6e 74 69 66 69 65 72 73 20 62 65 6c 6f 77  dentifiers below
14b3e 20 61 72 65 20 74 68 65 20 76 61 6c 69 64 20 74   are the valid t
14b3f 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ypes..**.** The 
14b40 70 75 72 70 6f 73 65 20 6f 66 20 74 68 65 20 70  purpose of the p
14b41 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 74 6f  ointer map is to
14b42 20 66 61 63 69 6c 69 74 79 20 6d 6f 76 69 6e 67   facility moving
14b43 20 70 61 67 65 73 20 66 72 6f 6d 20 6f 6e 65 0a   pages from one.
14b44 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74  ** position in t
14b45 68 65 20 66 69 6c 65 20 74 6f 20 61 6e 6f 74 68  he file to anoth
14b46 65 72 20 61 73 20 70 61 72 74 20 6f 66 20 61 75  er as part of au
14b47 74 6f 76 61 63 75 75 6d 2e 20 20 57 68 65 6e 20  tovacuum.  When 
14b48 61 20 70 61 67 65 0a 2a 2a 20 69 73 20 6d 6f 76  a page.** is mov
14b49 65 64 2c 20 74 68 65 20 70 6f 69 6e 74 65 72 20  ed, the pointer 
14b4a 69 6e 20 69 74 73 20 70 61 72 65 6e 74 20 6d 75  in its parent mu
14b4b 73 74 20 62 65 20 75 70 64 61 74 65 64 20 74 6f  st be updated to
14b4c 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a   point to the.**
14b4d 20 6e 65 77 20 6c 6f 63 61 74 69 6f 6e 2e 20 20   new location.  
14b4e 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20  The pointer map 
14b4f 69 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74  is used to locat
14b50 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67  e the parent pag
14b51 65 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 0a 2a 2a  e quickly..**.**
14b52 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45   PTRMAP_ROOTPAGE
14b53 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70  : The database p
14b54 61 67 65 20 69 73 20 61 20 72 6f 6f 74 2d 70 61  age is a root-pa
14b55 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d  ge. The page-num
14b56 62 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 20 20  ber is not.**   
14b57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75                 u
14b58 73 65 64 20 69 6e 20 74 68 69 73 20 63 61 73 65  sed in this case
14b59 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 46  ..**.** PTRMAP_F
14b5a 52 45 45 50 41 47 45 3a 20 54 68 65 20 64 61 74  REEPAGE: The dat
14b5b 61 62 61 73 65 20 70 61 67 65 20 69 73 20 61 6e  abase page is an
14b5c 20 75 6e 75 73 65 64 20 28 66 72 65 65 29 20 70   unused (free) p
14b5d 61 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75  age. The page-nu
14b5e 6d 62 65 72 20 0a 2a 2a 20 20 20 20 20 20 20 20  mber .**        
14b5f 20 20 20 20 20 20 20 20 20 20 69 73 20 6e 6f 74            is not
14b60 20 75 73 65 64 20 69 6e 20 74 68 69 73 20 63 61   used in this ca
14b61 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50  se..**.** PTRMAP
14b62 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 54 68 65 20  _OVERFLOW1: The 
14b63 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73  database page is
14b64 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20   the first page 
14b65 69 6e 20 61 20 6c 69 73 74 20 6f 66 20 0a 2a 2a  in a list of .**
14b66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14b67 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65     overflow page
14b68 73 2e 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62  s. The page numb
14b69 65 72 20 69 64 65 6e 74 69 66 69 65 73 20 74 68  er identifies th
14b6a 65 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 20  e page that.**  
14b6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14b6c 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 65   contains the ce
14b6d 6c 6c 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65  ll with a pointe
14b6e 72 20 74 6f 20 74 68 69 73 20 6f 76 65 72 66 6c  r to this overfl
14b6f 6f 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50  ow page..**.** P
14b70 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a  TRMAP_OVERFLOW2:
14b71 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61   The database pa
14b72 67 65 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64  ge is the second
14b73 20 6f 72 20 6c 61 74 65 72 20 70 61 67 65 20 69   or later page i
14b74 6e 20 61 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 20  n a list of.**  
14b75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14b76 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e   overflow pages.
14b77 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72   The page-number
14b78 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20   identifies the 
14b79 70 72 65 76 69 6f 75 73 0a 2a 2a 20 20 20 20 20  previous.**     
14b7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61                pa
14b7b 67 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c  ge in the overfl
14b7c 6f 77 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a 2a  ow page list..**
14b7d 0a 2a 2a 20 50 54 52 4d 41 50 5f 42 54 52 45 45  .** PTRMAP_BTREE
14b7e 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70  : The database p
14b7f 61 67 65 20 69 73 20 61 20 6e 6f 6e 2d 72 6f 6f  age is a non-roo
14b80 74 20 62 74 72 65 65 20 70 61 67 65 2e 20 54 68  t btree page. Th
14b81 65 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a 2a  e page number.**
14b82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
14b83 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 61  dentifies the pa
14b84 72 65 6e 74 20 70 61 67 65 20 69 6e 20 74 68 65  rent page in the
14b85 20 62 74 72 65 65 2e 0a 2a 2f 0a 23 64 65 66 69   btree..*/.#defi
14b86 6e 65 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41  ne PTRMAP_ROOTPA
14b87 47 45 20 31 0a 23 64 65 66 69 6e 65 20 50 54 52  GE 1.#define PTR
14b88 4d 41 50 5f 46 52 45 45 50 41 47 45 20 32 0a 23  MAP_FREEPAGE 2.#
14b89 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f 56  define PTRMAP_OV
14b8a 45 52 46 4c 4f 57 31 20 33 0a 23 64 65 66 69 6e  ERFLOW1 3.#defin
14b8b 65 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f  e PTRMAP_OVERFLO
14b8c 57 32 20 34 0a 23 64 65 66 69 6e 65 20 50 54 52  W2 4.#define PTR
14b8d 4d 41 50 5f 42 54 52 45 45 20 35 0a 0a 2f 2a 20  MAP_BTREE 5../* 
14b8e 41 20 62 75 6e 63 68 20 6f 66 20 61 73 73 65 72  A bunch of asser
14b8f 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74  t() statements t
14b90 6f 20 63 68 65 63 6b 20 74 68 65 20 74 72 61 6e  o check the tran
14b91 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20 76 61  saction state va
14b92 72 69 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 68 61  riables.** of ha
14b93 6e 64 6c 65 20 70 20 28 74 79 70 65 20 42 74 72  ndle p (type Btr
14b94 65 65 2a 29 20 61 72 65 20 69 6e 74 65 72 6e 61  ee*) are interna
14b95 6c 6c 79 20 63 6f 6e 73 69 73 74 65 6e 74 2e 0a  lly consistent..
14b96 2a 2f 0a 23 64 65 66 69 6e 65 20 62 74 72 65 65  */.#define btree
14b97 49 6e 74 65 67 72 69 74 79 28 70 29 20 5c 0a 20  Integrity(p) \. 
14b98 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d   assert( p->pBt-
14b99 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d  >inTransaction!=
14b9a 54 52 41 4e 53 5f 4e 4f 4e 45 20 7c 7c 20 70 2d  TRANS_NONE || p-
14b9b 3e 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69  >pBt->nTransacti
14b9c 6f 6e 3d 3d 30 20 29 3b 20 5c 0a 20 20 61 73 73  on==0 ); \.  ass
14b9d 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54  ert( p->pBt->inT
14b9e 72 61 6e 73 61 63 74 69 6f 6e 3e 3d 70 2d 3e 69  ransaction>=p->i
14b9f 6e 54 72 61 6e 73 20 29 3b 20 0a 0a 0a 2f 2a 0a  nTrans ); .../*.
14ba0 2a 2a 20 54 68 65 20 49 53 41 55 54 4f 56 41 43  ** The ISAUTOVAC
14ba1 55 55 4d 20 6d 61 63 72 6f 20 69 73 20 75 73 65  UUM macro is use
14ba2 64 20 77 69 74 68 69 6e 20 62 61 6c 61 6e 63 65  d within balance
14ba3 5f 6e 6f 6e 72 6f 6f 74 28 29 20 74 6f 20 64 65  _nonroot() to de
14ba4 74 65 72 6d 69 6e 65 0a 2a 2a 20 69 66 20 74 68  termine.** if th
14ba5 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f  e database suppo
14ba6 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 20  rts auto-vacuum 
14ba7 6f 72 20 6e 6f 74 2e 20 42 65 63 61 75 73 65 20  or not. Because 
14ba8 69 74 20 69 73 20 75 73 65 64 0a 2a 2a 20 77 69  it is used.** wi
14ba9 74 68 69 6e 20 61 6e 20 65 78 70 72 65 73 73 69  thin an expressi
14baa 6f 6e 20 74 68 61 74 20 69 73 20 61 6e 20 61 72  on that is an ar
14bab 67 75 6d 65 6e 74 20 74 6f 20 61 6e 6f 74 68 65  gument to anothe
14bac 72 20 6d 61 63 72 6f 20 0a 2a 2a 20 28 73 71 6c  r macro .** (sql
14bad 69 74 65 4d 61 6c 6c 6f 63 52 61 77 29 2c 20 69  iteMallocRaw), i
14bae 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
14baf 65 20 74 6f 20 75 73 65 20 63 6f 6e 64 69 74 69  e to use conditi
14bb0 6f 6e 61 6c 20 63 6f 6d 70 69 6c 61 74 69 6f 6e  onal compilation
14bb1 2e 0a 2a 2a 20 53 6f 2c 20 74 68 69 73 20 6d 61  ..** So, this ma
14bb2 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 20 69  cro is defined i
14bb3 6e 73 74 65 61 64 2e 0a 2a 2f 0a 23 69 66 6e 64  nstead..*/.#ifnd
14bb4 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
14bb5 55 54 4f 56 41 43 55 55 4d 0a 23 64 65 66 69 6e  UTOVACUUM.#defin
14bb6 65 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 28  e ISAUTOVACUUM (
14bb7 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29  pBt->autoVacuum)
14bb8 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 49  .#else.#define I
14bb9 53 41 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65  SAUTOVACUUM 0.#e
14bba 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ndif.../*.** Thi
14bbb 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 70  s structure is p
14bbc 61 73 73 65 64 20 61 72 6f 75 6e 64 20 74 68 72  assed around thr
14bbd 6f 75 67 68 20 61 6c 6c 20 74 68 65 20 73 61 6e  ough all the san
14bbe 69 74 79 20 63 68 65 63 6b 69 6e 67 20 72 6f 75  ity checking rou
14bbf 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 72 64 65  tines.** in orde
14bc0 72 20 74 6f 20 6b 65 65 70 20 74 72 61 63 6b 20  r to keep track 
14bc1 6f 66 20 73 6f 6d 65 20 67 6c 6f 62 61 6c 20 73  of some global s
14bc2 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  tate information
14bc3 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
14bc4 75 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20  uct IntegrityCk 
14bc5 49 6e 74 65 67 72 69 74 79 43 6b 3b 0a 73 74 72  IntegrityCk;.str
14bc6 75 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20  uct IntegrityCk 
14bc7 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  {.  BtShared *pB
14bc8 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 74 72 65  t;    /* The tre
14bc9 65 20 62 65 69 6e 67 20 63 68 65 63 6b 65 64 20  e being checked 
14bca 6f 75 74 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a  out */.  Pager *
14bcb 70 50 61 67 65 72 3b 20 20 20 20 2f 2a 20 54 68  pPager;    /* Th
14bcc 65 20 61 73 73 6f 63 69 61 74 65 64 20 70 61 67  e associated pag
14bcd 65 72 2e 20 20 41 6c 73 6f 20 61 63 63 65 73 73  er.  Also access
14bce 69 62 6c 65 20 62 79 20 70 42 74 2d 3e 70 50 61  ible by pBt->pPa
14bcf 67 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 50  ger */.  Pgno nP
14bd0 61 67 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75  age;       /* Nu
14bd1 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
14bd2 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f   the database */
14bd3 0a 20 20 69 6e 74 20 2a 61 6e 52 65 66 3b 20 20  .  int *anRef;  
14bd4 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
14bd5 66 20 74 69 6d 65 73 20 65 61 63 68 20 70 61 67  f times each pag
14bd6 65 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 20  e is referenced 
14bd7 2a 2f 0a 20 20 69 6e 74 20 6d 78 45 72 72 3b 20  */.  int mxErr; 
14bd8 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 70 20 61         /* Stop a
14bd9 63 63 75 6d 75 6c 61 74 69 6e 67 20 65 72 72 6f  ccumulating erro
14bda 72 73 20 77 68 65 6e 20 74 68 69 73 20 72 65 61  rs when this rea
14bdb 63 68 65 73 20 7a 65 72 6f 20 2a 2f 0a 20 20 69  ches zero */.  i
14bdc 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20  nt nErr;        
14bdd 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65   /* Number of me
14bde 73 73 61 67 65 73 20 77 72 69 74 74 65 6e 20 74  ssages written t
14bdf 6f 20 7a 45 72 72 4d 73 67 20 73 6f 20 66 61 72  o zErrMsg so far
14be0 20 2a 2f 0a 20 20 69 6e 74 20 6d 61 6c 6c 6f 63   */.  int malloc
14be1 46 61 69 6c 65 64 3b 20 2f 2a 20 41 20 6d 65 6d  Failed; /* A mem
14be2 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65  ory allocation e
14be3 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65  rror has occurre
14be4 64 20 2a 2f 0a 20 20 53 74 72 41 63 63 75 6d 20  d */.  StrAccum 
14be5 65 72 72 4d 73 67 3b 20 20 2f 2a 20 41 63 63 75  errMsg;  /* Accu
14be6 6d 75 6c 61 74 65 20 74 68 65 20 65 72 72 6f 72  mulate the error
14be7 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 68 65   message text he
14be8 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  re */.};../*.** 
14be9 52 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 20  Read or write a 
14bea 74 77 6f 2d 20 61 6e 64 20 66 6f 75 72 2d 62 79  two- and four-by
14beb 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e  te big-endian in
14bec 74 65 67 65 72 20 76 61 6c 75 65 73 2e 0a 2a 2f  teger values..*/
14bed 0a 23 64 65 66 69 6e 65 20 67 65 74 32 62 79 74  .#define get2byt
14bee 65 28 78 29 20 20 20 28 28 78 29 5b 30 5d 3c 3c  e(x)   ((x)[0]<<
14bef 38 20 7c 20 28 78 29 5b 31 5d 29 0a 23 64 65 66  8 | (x)[1]).#def
14bf0 69 6e 65 20 70 75 74 32 62 79 74 65 28 70 2c 76  ine put2byte(p,v
14bf1 29 20 28 28 70 29 5b 30 5d 20 3d 20 28 75 38 29  ) ((p)[0] = (u8)
14bf2 28 28 76 29 3e 3e 38 29 2c 20 28 70 29 5b 31 5d  ((v)>>8), (p)[1]
14bf3 20 3d 20 28 75 38 29 28 76 29 29 0a 23 64 65 66   = (u8)(v)).#def
14bf4 69 6e 65 20 67 65 74 34 62 79 74 65 20 73 71 6c  ine get4byte sql
14bf5 69 74 65 33 47 65 74 34 62 79 74 65 0a 23 64 65  ite3Get4byte.#de
14bf6 66 69 6e 65 20 70 75 74 34 62 79 74 65 20 73 71  fine put4byte sq
14bf7 6c 69 74 65 33 50 75 74 34 62 79 74 65 0a 0a 2f  lite3Put4byte../
14bf8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
14bf9 6e 64 20 6f 66 20 62 74 72 65 65 49 6e 74 2e 68  nd of btreeInt.h
14bfa 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
14bfb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14bfc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
14bfd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
14bfe 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
14bff 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 62  we left off in b
14c00 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a  tmutex.c *******
14c01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23  *************/.#
14c02 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
14c03 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
14c04 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
14c05 44 53 41 46 45 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74  DSAFE../*.** Obt
14c06 61 69 6e 20 74 68 65 20 42 74 53 68 61 72 65 64  ain the BtShared
14c07 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 65   mutex associate
14c08 64 20 77 69 74 68 20 42 2d 54 72 65 65 20 68 61  d with B-Tree ha
14c09 6e 64 6c 65 20 70 2e 20 41 6c 73 6f 2c 0a 2a 2a  ndle p. Also,.**
14c0a 20 73 65 74 20 42 74 53 68 61 72 65 64 2e 64 62   set BtShared.db
14c0b 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
14c0c 20 68 61 6e 64 6c 65 20 61 73 73 6f 63 69 61 74   handle associat
14c0d 65 64 20 77 69 74 68 20 70 20 61 6e 64 20 74 68  ed with p and th
14c0e 65 0a 2a 2a 20 70 2d 3e 6c 6f 63 6b 65 64 20 62  e.** p->locked b
14c0f 6f 6f 6c 65 61 6e 20 74 6f 20 74 72 75 65 2e 0a  oolean to true..
14c10 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c  */.static void l
14c11 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 42 74  ockBtreeMutex(Bt
14c12 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  ree *p){.  asser
14c13 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20  t( p->locked==0 
14c14 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
14c15 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65  ite3_mutex_nothe
14c16 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  ld(p->pBt->mutex
14c17 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
14c18 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
14c19 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  d(p->db->mutex) 
14c1a 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  );..  sqlite3_mu
14c1b 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 42 74  tex_enter(p->pBt
14c1c 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 70  ->mutex);.  p->p
14c1d 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  Bt->db = p->db;.
14c1e 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b    p->locked = 1;
14c1f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73  .}../*.** Releas
14c20 65 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6d  e the BtShared m
14c21 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 20  utex associated 
14c22 77 69 74 68 20 42 2d 54 72 65 65 20 68 61 6e 64  with B-Tree hand
14c23 6c 65 20 70 20 61 6e 64 0a 2a 2a 20 63 6c 65 61  le p and.** clea
14c24 72 20 74 68 65 20 70 2d 3e 6c 6f 63 6b 65 64 20  r the p->locked 
14c25 62 6f 6f 6c 65 61 6e 2e 0a 2a 2f 0a 73 74 61 74  boolean..*/.stat
14c26 69 63 20 76 6f 69 64 20 75 6e 6c 6f 63 6b 42 74  ic void unlockBt
14c27 72 65 65 4d 75 74 65 78 28 42 74 72 65 65 20 2a  reeMutex(Btree *
14c28 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  p){.  assert( p-
14c29 3e 6c 6f 63 6b 65 64 3d 3d 31 20 29 3b 0a 20 20  >locked==1 );.  
14c2a 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
14c2b 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42  mutex_held(p->pB
14c2c 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  t->mutex) );.  a
14c2d 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
14c2e 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d  utex_held(p->db-
14c2f 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
14c30 65 72 74 28 20 70 2d 3e 64 62 3d 3d 70 2d 3e 70  ert( p->db==p->p
14c31 42 74 2d 3e 64 62 20 29 3b 0a 0a 20 20 73 71 6c  Bt->db );..  sql
14c32 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
14c33 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b  (p->pBt->mutex);
14c34 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30  .  p->locked = 0
14c35 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72  ;.}../*.** Enter
14c36 20 61 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20   a mutex on the 
14c37 67 69 76 65 6e 20 42 54 72 65 65 20 6f 62 6a 65  given BTree obje
14c38 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ct..**.** If the
14c39 20 6f 62 6a 65 63 74 20 69 73 20 6e 6f 74 20 73   object is not s
14c3a 68 61 72 61 62 6c 65 2c 20 74 68 65 6e 20 6e 6f  harable, then no
14c3b 20 6d 75 74 65 78 20 69 73 20 65 76 65 72 20 72   mutex is ever r
14c3c 65 71 75 69 72 65 64 0a 2a 2a 20 61 6e 64 20 74  equired.** and t
14c3d 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
14c3e 20 6e 6f 2d 6f 70 2e 20 20 54 68 65 20 75 6e 64   no-op.  The und
14c3f 65 72 6c 79 69 6e 67 20 6d 75 74 65 78 20 69 73  erlying mutex is
14c40 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 2e 0a   non-recursive..
14c41 2a 2a 20 42 75 74 20 77 65 20 6b 65 65 70 20 61  ** But we keep a
14c42 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
14c43 20 69 6e 20 42 74 72 65 65 2e 77 61 6e 74 54 6f   in Btree.wantTo
14c44 4c 6f 63 6b 20 73 6f 20 74 68 65 20 62 65 68 61  Lock so the beha
14c45 76 69 6f 72 0a 2a 2a 20 6f 66 20 74 68 69 73 20  vior.** of this 
14c46 69 6e 74 65 72 66 61 63 65 20 69 73 20 72 65 63  interface is rec
14c47 75 72 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 6f  ursive..**.** To
14c48 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 73   avoid deadlocks
14c49 2c 20 6d 75 6c 74 69 70 6c 65 20 42 74 72 65 65  , multiple Btree
14c4a 73 20 61 72 65 20 6c 6f 63 6b 65 64 20 69 6e 20  s are locked in 
14c4b 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72 0a 2a  the same order.*
14c4c 2a 20 62 79 20 61 6c 6c 20 64 61 74 61 62 61 73  * by all databas
14c4d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20  e connections.  
14c4e 54 68 65 20 70 2d 3e 70 4e 65 78 74 20 69 73 20  The p->pNext is 
14c4f 61 20 6c 69 73 74 20 6f 66 20 6f 74 68 65 72 0a  a list of other.
14c50 2a 2a 20 42 74 72 65 65 73 20 62 65 6c 6f 6e 67  ** Btrees belong
14c51 69 6e 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20  ing to the same 
14c52 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
14c53 69 6f 6e 20 61 73 20 74 68 65 20 70 20 42 74 72  ion as the p Btr
14c54 65 65 0a 2a 2a 20 77 68 69 63 68 20 6e 65 65 64  ee.** which need
14c55 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 61 66   to be locked af
14c56 74 65 72 20 70 2e 20 20 49 66 20 77 65 20 63 61  ter p.  If we ca
14c57 6e 6e 6f 74 20 67 65 74 20 61 20 6c 6f 63 6b 20  nnot get a lock 
14c58 6f 6e 0a 2a 2a 20 70 2c 20 74 68 65 6e 20 66 69  on.** p, then fi
14c59 72 73 74 20 75 6e 6c 6f 63 6b 20 61 6c 6c 20 6f  rst unlock all o
14c5a 66 20 74 68 65 20 6f 74 68 65 72 73 20 6f 6e 20  f the others on 
14c5b 70 2d 3e 70 4e 65 78 74 2c 20 74 68 65 6e 20 77  p->pNext, then w
14c5c 61 69 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6c  ait.** for the l
14c5d 6f 63 6b 20 74 6f 20 62 65 63 6f 6d 65 20 61 76  ock to become av
14c5e 61 69 6c 61 62 6c 65 20 6f 6e 20 70 2c 20 74 68  ailable on p, th
14c5f 65 6e 20 72 65 6c 6f 63 6b 20 61 6c 6c 20 6f 66  en relock all of
14c60 20 74 68 65 0a 2a 2a 20 73 75 62 73 65 71 75 65   the.** subseque
14c61 6e 74 20 42 74 72 65 65 73 20 74 68 61 74 20 64  nt Btrees that d
14c62 65 73 69 72 65 20 61 20 6c 6f 63 6b 2e 0a 2a 2f  esire a lock..*/
14c63 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14c64 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
14c65 65 45 6e 74 65 72 28 42 74 72 65 65 20 2a 70 29  eEnter(Btree *p)
14c66 7b 0a 20 20 42 74 72 65 65 20 2a 70 4c 61 74 65  {.  Btree *pLate
14c67 72 3b 0a 0a 20 20 2f 2a 20 53 6f 6d 65 20 62 61  r;..  /* Some ba
14c68 73 69 63 20 73 61 6e 69 74 79 20 63 68 65 63 6b  sic sanity check
14c69 69 6e 67 20 6f 6e 20 74 68 65 20 42 74 72 65 65  ing on the Btree
14c6a 2e 20 20 54 68 65 20 6c 69 73 74 20 6f 66 20 42  .  The list of B
14c6b 74 72 65 65 73 0a 20 20 2a 2a 20 63 6f 6e 6e 65  trees.  ** conne
14c6c 63 74 65 64 20 62 79 20 70 4e 65 78 74 20 61 6e  cted by pNext an
14c6d 64 20 70 50 72 65 76 20 73 68 6f 75 6c 64 20 62  d pPrev should b
14c6e 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65  e in sorted orde
14c6f 72 20 62 79 0a 20 20 2a 2a 20 42 74 72 65 65 2e  r by.  ** Btree.
14c70 70 42 74 20 76 61 6c 75 65 2e 20 41 6c 6c 20 65  pBt value. All e
14c71 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 6c  lements of the l
14c72 69 73 74 20 73 68 6f 75 6c 64 20 62 65 6c 6f 6e  ist should belon
14c73 67 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 73 61  g to.  ** the sa
14c74 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f  me connection. O
14c75 6e 6c 79 20 73 68 61 72 65 64 20 42 74 72 65 65  nly shared Btree
14c76 73 20 61 72 65 20 6f 6e 20 74 68 65 20 6c 69 73  s are on the lis
14c77 74 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  t. */.  assert( 
14c78 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70  p->pNext==0 || p
14c79 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3e 70 2d 3e  ->pNext->pBt>p->
14c7a 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28  pBt );.  assert(
14c7b 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c 20   p->pPrev==0 || 
14c7c 70 2d 3e 70 50 72 65 76 2d 3e 70 42 74 3c 70 2d  p->pPrev->pBt<p-
14c7d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74  >pBt );.  assert
14c7e 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c  ( p->pNext==0 ||
14c7f 20 70 2d 3e 70 4e 65 78 74 2d 3e 64 62 3d 3d 70   p->pNext->db==p
14c80 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74  ->db );.  assert
14c81 28 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c  ( p->pPrev==0 ||
14c82 20 70 2d 3e 70 50 72 65 76 2d 3e 64 62 3d 3d 70   p->pPrev->db==p
14c83 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74  ->db );.  assert
14c84 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c  ( p->sharable ||
14c85 20 28 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 26 26   (p->pNext==0 &&
14c86 20 70 2d 3e 70 50 72 65 76 3d 3d 30 29 20 29 3b   p->pPrev==0) );
14c87 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72  ..  /* Check for
14c88 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 73 69 73 74   locking consist
14c89 65 6e 63 79 20 2a 2f 0a 20 20 61 73 73 65 72 74  ency */.  assert
14c8a 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20  ( !p->locked || 
14c8b 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20  p->wantToLock>0 
14c8c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
14c8d 73 68 61 72 61 62 6c 65 20 7c 7c 20 70 2d 3e 77  sharable || p->w
14c8e 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a  antToLock==0 );.
14c8f 0a 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20  .  /* We should 
14c90 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 6c  already hold a l
14c91 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
14c92 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
14c93 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  /.  assert( sqli
14c94 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
14c95 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
14c96 0a 20 20 2f 2a 20 55 6e 6c 65 73 73 20 74 68 65  .  /* Unless the
14c97 20 64 61 74 61 62 61 73 65 20 69 73 20 73 68 61   database is sha
14c98 72 61 62 6c 65 20 61 6e 64 20 75 6e 6c 6f 63 6b  rable and unlock
14c99 65 64 2c 20 74 68 65 6e 20 42 74 53 68 61 72 65  ed, then BtShare
14c9a 64 2e 64 62 0a 20 20 2a 2a 20 73 68 6f 75 6c 64  d.db.  ** should
14c9b 20 61 6c 72 65 61 64 79 20 62 65 20 73 65 74 20   already be set 
14c9c 63 6f 72 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20  correctly. */.  
14c9d 61 73 73 65 72 74 28 20 28 70 2d 3e 6c 6f 63 6b  assert( (p->lock
14c9e 65 64 3d 3d 30 20 26 26 20 70 2d 3e 73 68 61 72  ed==0 && p->shar
14c9f 61 62 6c 65 29 20 7c 7c 20 70 2d 3e 70 42 74 2d  able) || p->pBt-
14ca0 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 0a 20  >db==p->db );.. 
14ca1 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c   if( !p->sharabl
14ca2 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 2d  e ) return;.  p-
14ca3 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20  >wantToLock++;. 
14ca4 20 69 66 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 29   if( p->locked )
14ca5 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 49   return;..  /* I
14ca6 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c 20 77 65  n most cases, we
14ca7 20 73 68 6f 75 6c 64 20 62 65 20 61 62 6c 65 20   should be able 
14ca8 74 6f 20 61 63 71 75 69 72 65 20 74 68 65 20 6c  to acquire the l
14ca9 6f 63 6b 20 77 65 0a 20 20 2a 2a 20 77 61 6e 74  ock we.  ** want
14caa 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20   without having 
14cab 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 74 20 74  to go throught t
14cac 68 65 20 61 73 63 65 6e 64 69 6e 67 20 6c 6f 63  he ascending loc
14cad 6b 0a 20 20 2a 2a 20 70 72 6f 63 65 64 75 72 65  k.  ** procedure
14cae 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 20 20   that follows.  
14caf 4a 75 73 74 20 62 65 20 73 75 72 65 20 6e 6f 74  Just be sure not
14cb0 20 74 6f 20 62 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a   to block..  */.
14cb1 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 6d 75    if( sqlite3_mu
14cb2 74 65 78 5f 74 72 79 28 70 2d 3e 70 42 74 2d 3e  tex_try(p->pBt->
14cb3 6d 75 74 65 78 29 3d 3d 53 51 4c 49 54 45 5f 4f  mutex)==SQLITE_O
14cb4 4b 20 29 7b 0a 20 20 20 20 70 2d 3e 70 42 74 2d  K ){.    p->pBt-
14cb5 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20  >db = p->db;.   
14cb6 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b 0a   p->locked = 1;.
14cb7 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
14cb8 0a 20 20 2f 2a 20 54 6f 20 61 76 6f 69 64 20 64  .  /* To avoid d
14cb9 65 61 64 6c 6f 63 6b 2c 20 66 69 72 73 74 20 72  eadlock, first r
14cba 65 6c 65 61 73 65 20 61 6c 6c 20 6c 6f 63 6b 73  elease all locks
14cbb 20 77 69 74 68 20 61 20 6c 61 72 67 65 72 0a 20   with a larger. 
14cbc 20 2a 2a 20 42 74 53 68 61 72 65 64 20 61 64 64   ** BtShared add
14cbd 72 65 73 73 2e 20 20 54 68 65 6e 20 61 63 71 75  ress.  Then acqu
14cbe 69 72 65 20 6f 75 72 20 6c 6f 63 6b 2e 20 20 54  ire our lock.  T
14cbf 68 65 6e 20 72 65 61 63 71 75 69 72 65 0a 20 20  hen reacquire.  
14cc0 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 42 74 53  ** the other BtS
14cc1 68 61 72 65 64 20 6c 6f 63 6b 73 20 74 68 61 74  hared locks that
14cc2 20 77 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64   we used to hold
14cc3 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 0a 20 20   in ascending.  
14cc4 2a 2a 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20  ** order..  */. 
14cc5 20 66 6f 72 28 70 4c 61 74 65 72 3d 70 2d 3e 70   for(pLater=p->p
14cc6 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c  Next; pLater; pL
14cc7 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65  ater=pLater->pNe
14cc8 78 74 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  xt){.    assert(
14cc9 20 70 4c 61 74 65 72 2d 3e 73 68 61 72 61 62 6c   pLater->sharabl
14cca 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  e );.    assert(
14ccb 20 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 3d 3d   pLater->pNext==
14ccc 30 20 7c 7c 20 70 4c 61 74 65 72 2d 3e 70 4e 65  0 || pLater->pNe
14ccd 78 74 2d 3e 70 42 74 3e 70 4c 61 74 65 72 2d 3e  xt->pBt>pLater->
14cce 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72  pBt );.    asser
14ccf 74 28 20 21 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b  t( !pLater->lock
14cd0 65 64 20 7c 7c 20 70 4c 61 74 65 72 2d 3e 77 61  ed || pLater->wa
14cd1 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20  ntToLock>0 );.  
14cd2 20 20 69 66 28 20 70 4c 61 74 65 72 2d 3e 6c 6f    if( pLater->lo
14cd3 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20 75 6e  cked ){.      un
14cd4 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70  lockBtreeMutex(p
14cd5 4c 61 74 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20  Later);.    }.  
14cd6 7d 0a 20 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74  }.  lockBtreeMut
14cd7 65 78 28 70 29 3b 0a 20 20 66 6f 72 28 70 4c 61  ex(p);.  for(pLa
14cd8 74 65 72 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c  ter=p->pNext; pL
14cd9 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61  ater; pLater=pLa
14cda 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  ter->pNext){.   
14cdb 20 69 66 28 20 70 4c 61 74 65 72 2d 3e 77 61 6e   if( pLater->wan
14cdc 74 54 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20  tToLock ){.     
14cdd 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28   lockBtreeMutex(
14cde 70 4c 61 74 65 72 29 3b 0a 20 20 20 20 7d 0a 20  pLater);.    }. 
14cdf 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 69 74   }.}../*.** Exit
14ce0 20 74 68 65 20 72 65 63 75 72 73 69 76 65 20 6d   the recursive m
14ce1 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65 65 2e  utex on a Btree.
14ce2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
14ce3 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
14ce4 74 72 65 65 4c 65 61 76 65 28 42 74 72 65 65 20  treeLeave(Btree 
14ce5 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 73 68  *p){.  if( p->sh
14ce6 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 61 73  arable ){.    as
14ce7 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c  sert( p->wantToL
14ce8 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 70 2d 3e  ock>0 );.    p->
14ce9 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20  wantToLock--;.  
14cea 20 20 69 66 28 20 70 2d 3e 77 61 6e 74 54 6f 4c    if( p->wantToL
14ceb 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ock==0 ){.      
14cec 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78  unlockBtreeMutex
14ced 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  (p);.    }.  }.}
14cee 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
14cef 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  ./*.** Return tr
14cf0 75 65 20 69 66 20 74 68 65 20 42 74 53 68 61 72  ue if the BtShar
14cf1 65 64 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64  ed mutex is held
14cf2 20 6f 6e 20 74 68 65 20 62 74 72 65 65 2c 20 6f   on the btree, o
14cf3 72 20 69 66 20 74 68 65 0a 2a 2a 20 42 2d 54 72  r if the.** B-Tr
14cf4 65 65 20 69 73 20 6e 6f 74 20 6d 61 72 6b 65 64  ee is not marked
14cf5 20 61 73 20 73 68 61 72 61 62 6c 65 2e 0a 2a 2a   as sharable..**
14cf6 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
14cf7 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 66 72   is used only fr
14cf8 6f 6d 20 77 69 74 68 69 6e 20 61 73 73 65 72 74  om within assert
14cf9 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  () statements..*
14cfa 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
14cfb 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
14cfc 65 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 72 65  eHoldsMutex(Btre
14cfd 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  e *p){.  assert(
14cfe 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20   p->sharable==0 
14cff 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20  || p->locked==0 
14d00 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b  || p->wantToLock
14d01 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
14d02 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 7c  p->sharable==0 |
14d03 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 7c  | p->locked==0 |
14d04 7c 20 70 2d 3e 64 62 3d 3d 70 2d 3e 70 42 74 2d  | p->db==p->pBt-
14d05 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28  >db );.  assert(
14d06 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20   p->sharable==0 
14d07 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20  || p->locked==0 
14d08 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
14d09 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75  _held(p->pBt->mu
14d0a 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
14d0b 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30  ( p->sharable==0
14d0c 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30   || p->locked==0
14d0d 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   || sqlite3_mute
14d0e 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
14d0f 74 65 78 29 20 29 3b 0a 0a 20 20 72 65 74 75 72  tex) );..  retur
14d10 6e 20 28 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d  n (p->sharable==
14d11 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 29 3b  0 || p->locked);
14d12 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e  .}.#endif...#ifn
14d13 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
14d14 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 45  INCRBLOB./*.** E
14d15 6e 74 65 72 20 61 6e 64 20 6c 65 61 76 65 20 61  nter and leave a
14d16 20 6d 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65   mutex on a Btre
14d17 65 20 67 69 76 65 6e 20 61 20 63 75 72 73 6f 72  e given a cursor
14d18 20 6f 77 6e 65 64 20 62 79 20 74 68 61 74 0a 2a   owned by that.*
14d19 2a 20 42 74 72 65 65 2e 20 20 54 68 65 73 65 20  * Btree.  These 
14d1a 65 6e 74 72 79 20 70 6f 69 6e 74 73 20 61 72 65  entry points are
14d1b 20 75 73 65 64 20 62 79 20 69 6e 63 72 65 6d 65   used by increme
14d1c 6e 74 61 6c 20 49 2f 4f 20 61 6e 64 20 63 61 6e  ntal I/O and can
14d1d 20 62 65 0a 2a 2a 20 6f 6d 69 74 74 65 64 20 69   be.** omitted i
14d1e 66 20 74 68 61 74 20 6d 6f 64 75 6c 65 20 69 73  f that module is
14d1f 20 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 53 51   not used..*/.SQ
14d20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
14d21 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e  d sqlite3BtreeEn
14d22 74 65 72 43 75 72 73 6f 72 28 42 74 43 75 72 73  terCursor(BtCurs
14d23 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73 71 6c  or *pCur){.  sql
14d24 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
14d25 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a 7d 0a  Cur->pBtree);.}.
14d26 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
14d27 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
14d28 4c 65 61 76 65 43 75 72 73 6f 72 28 42 74 43 75  LeaveCursor(BtCu
14d29 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73  rsor *pCur){.  s
14d2a 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
14d2b 28 70 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a  (pCur->pBtree);.
14d2c 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
14d2d 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42  TE_OMIT_INCRBLOB
14d2e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65   */.../*.** Ente
14d2f 72 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 65  r the mutex on e
14d30 76 65 72 79 20 42 74 72 65 65 20 61 73 73 6f 63  very Btree assoc
14d31 69 61 74 65 64 20 77 69 74 68 20 61 20 64 61 74  iated with a dat
14d32 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74  abase.** connect
14d33 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 6e 65  ion.  This is ne
14d34 65 64 65 64 20 28 66 6f 72 20 65 78 61 6d 70 6c  eded (for exampl
14d35 65 29 20 70 72 69 6f 72 20 74 6f 20 70 61 72 73  e) prior to pars
14d36 69 6e 67 0a 2a 2a 20 61 20 73 74 61 74 65 6d 65  ing.** a stateme
14d37 6e 74 20 73 69 6e 63 65 20 77 65 20 77 69 6c 6c  nt since we will
14d38 20 62 65 20 63 6f 6d 70 61 72 69 6e 67 20 74 61   be comparing ta
14d39 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e  ble and column n
14d3a 61 6d 65 73 0a 2a 2a 20 61 67 61 69 6e 73 74 20  ames.** against 
14d3b 61 6c 6c 20 73 63 68 65 6d 61 73 20 61 6e 64 20  all schemas and 
14d3c 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74  we do not want t
14d3d 68 6f 73 65 20 73 63 68 65 6d 61 73 20 62 65 69  hose schemas bei
14d3e 6e 67 0a 2a 2a 20 72 65 73 65 74 20 6f 75 74 20  ng.** reset out 
14d3f 66 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 2a  from under us..*
14d40 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20  *.** There is a 
14d41 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6c 65  corresponding le
14d42 61 76 65 2d 61 6c 6c 20 70 72 6f 63 65 64 75 72  ave-all procedur
14d43 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 6e 74 65 72 20  es..**.** Enter 
14d44 74 68 65 20 6d 75 74 65 78 65 73 20 69 6e 20 61  the mutexes in a
14d45 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62  ccending order b
14d46 79 20 42 74 53 68 61 72 65 64 20 70 6f 69 6e 74  y BtShared point
14d47 65 72 20 61 64 64 72 65 73 73 0a 2a 2a 20 74 6f  er address.** to
14d48 20 61 76 6f 69 64 20 74 68 65 20 70 6f 73 73 69   avoid the possi
14d49 62 69 6c 69 74 79 20 6f 66 20 64 65 61 64 6c 6f  bility of deadlo
14d4a 63 6b 20 77 68 65 6e 20 74 77 6f 20 74 68 72 65  ck when two thre
14d4b 61 64 73 20 77 69 74 68 0a 2a 2a 20 74 77 6f 20  ads with.** two 
14d4c 6f 72 20 6d 6f 72 65 20 62 74 72 65 65 73 20 69  or more btrees i
14d4d 6e 20 63 6f 6d 6d 6f 6e 20 62 6f 74 68 20 74 72  n common both tr
14d4e 79 20 74 6f 20 6c 6f 63 6b 20 61 6c 6c 20 74 68  y to lock all th
14d4f 65 69 72 20 62 74 72 65 65 73 0a 2a 2a 20 61 74  eir btrees.** at
14d50 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 61 6e   the same instan
14d51 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
14d52 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
14d53 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 73  3BtreeEnterAll(s
14d54 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69  qlite3 *db){.  i
14d55 6e 74 20 69 3b 0a 20 20 42 74 72 65 65 20 2a 70  nt i;.  Btree *p
14d56 2c 20 2a 70 4c 61 74 65 72 3b 0a 20 20 61 73 73  , *pLater;.  ass
14d57 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
14d58 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
14d59 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  x) );.  for(i=0;
14d5a 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
14d5b 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 44  {.    p = db->aD
14d5c 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 61 73  b[i].pBt;.    as
14d5d 73 65 72 74 28 20 21 70 20 7c 7c 20 28 70 2d 3e  sert( !p || (p->
14d5e 6c 6f 63 6b 65 64 3d 3d 30 20 26 26 20 70 2d 3e  locked==0 && p->
14d5f 73 68 61 72 61 62 6c 65 29 20 7c 7c 20 70 2d 3e  sharable) || p->
14d60 70 42 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29  pBt->db==p->db )
14d61 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 70  ;.    if( p && p
14d62 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20  ->sharable ){.  
14d63 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63      p->wantToLoc
14d64 6b 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 21  k++;.      if( !
14d65 70 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20  p->locked ){.   
14d66 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
14d67 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 31 20 29 3b  wantToLock==1 );
14d68 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
14d69 70 2d 3e 70 50 72 65 76 20 29 20 70 20 3d 20 70  p->pPrev ) p = p
14d6a 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 20  ->pPrev;.       
14d6b 20 2f 2a 20 52 65 61 73 6f 6e 20 66 6f 72 20 41   /* Reason for A
14d6c 4c 57 41 59 53 3a 20 20 54 68 65 72 65 20 6d 75  LWAYS:  There mu
14d6d 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 6f  st be at least o
14d6e 6e 20 75 6e 6c 6f 63 6b 65 64 20 42 74 72 65 65  n unlocked Btree
14d6f 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74   in.        ** t
14d70 68 65 20 63 68 61 69 6e 2e 20 20 4f 74 68 65 72  he chain.  Other
14d71 77 69 73 65 20 74 68 65 20 21 70 2d 3e 6c 6f 63  wise the !p->loc
14d72 6b 65 64 20 74 65 73 74 20 61 62 6f 76 65 20 77  ked test above w
14d73 6f 75 6c 64 20 68 61 76 65 20 66 61 69 6c 65 64  ould have failed
14d74 20 2a 2f 0a 20 20 20 20 20 20 20 20 77 68 69 6c   */.        whil
14d75 65 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 26 26 20  e( p->locked && 
14d76 41 4c 57 41 59 53 28 70 2d 3e 70 4e 65 78 74 29  ALWAYS(p->pNext)
14d77 20 29 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b   ) p = p->pNext;
14d78 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 4c 61  .        for(pLa
14d79 74 65 72 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 20  ter = p->pNext; 
14d7a 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70  pLater; pLater=p
14d7b 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20  Later->pNext){. 
14d7c 20 20 20 20 20 20 20 20 20 69 66 28 20 70 4c 61           if( pLa
14d7d 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20  ter->locked ){. 
14d7e 20 20 20 20 20 20 20 20 20 20 20 75 6e 6c 6f 63             unloc
14d7f 6b 42 74 72 65 65 4d 75 74 65 78 28 70 4c 61 74  kBtreeMutex(pLat
14d80 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  er);.          }
14d81 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
14d82 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20     while( p ){. 
14d83 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 42 74 72           lockBtr
14d84 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20  eeMutex(p);.    
14d85 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65        p = p->pNe
14d86 78 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  xt;.        }.  
14d87 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
14d88 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
14d89 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
14d8a 65 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c 69 74  eeLeaveAll(sqlit
14d8b 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69  e3 *db){.  int i
14d8c 3b 0a 20 20 42 74 72 65 65 20 2a 70 3b 0a 20 20  ;.  Btree *p;.  
14d8d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
14d8e 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d  mutex_held(db->m
14d8f 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69  utex) );.  for(i
14d90 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
14d91 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d  ++){.    p = db-
14d92 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20  >aDb[i].pBt;.   
14d93 20 69 66 28 20 70 20 26 26 20 70 2d 3e 73 68 61   if( p && p->sha
14d94 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 61  rable ){.      a
14d95 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f  ssert( p->wantTo
14d96 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 20 20  Lock>0 );.      
14d97 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b  p->wantToLock--;
14d98 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 77 61  .      if( p->wa
14d99 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20  ntToLock==0 ){. 
14d9a 20 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72         unlockBtr
14d9b 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20  eeMutex(p);.    
14d9c 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a    }.    }.  }.}.
14d9d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
14d9e 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  /*.** Return tru
14d9f 65 20 69 66 20 74 68 65 20 63 75 72 72 65 6e 74  e if the current
14da0 20 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68   thread holds th
14da1 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
14da2 63 74 69 6f 6e 0a 2a 2a 20 6d 75 74 65 78 20 61  ction.** mutex a
14da3 6e 64 20 61 6c 6c 20 72 65 71 75 69 72 65 64 20  nd all required 
14da4 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 65 73  BtShared mutexes
14da5 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
14da6 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73  tine is used ins
14da7 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61  ide assert() sta
14da8 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 0a 2a 2f  tements only..*/
14da9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14daa 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
14dab 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28  HoldsAllMutexes(
14dac 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
14dad 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21 73 71  int i;.  if( !sq
14dae 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
14daf 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 7b 0a 20  (db->mutex) ){. 
14db0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
14db1 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62  .  for(i=0; i<db
14db2 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
14db3 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 20 20 70   Btree *p;.    p
14db4 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
14db5 74 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20  t;.    if( p && 
14db6 70 2d 3e 73 68 61 72 61 62 6c 65 20 26 26 0a 20  p->sharable &&. 
14db7 20 20 20 20 20 20 20 20 28 70 2d 3e 77 61 6e 74          (p->want
14db8 54 6f 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 21 73 71  ToLock==0 || !sq
14db9 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
14dba 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 29  (p->pBt->mutex))
14dbb 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
14dbc 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   0;.    }.  }.  
14dbd 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64  return 1;.}.#end
14dbe 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a  if /* NDEBUG */.
14dbf 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77  ./*.** Add a new
14dc0 20 42 74 72 65 65 20 70 6f 69 6e 74 65 72 20 74   Btree pointer t
14dc1 6f 20 61 20 42 74 72 65 65 4d 75 74 65 78 41 72  o a BtreeMutexAr
14dc2 72 61 79 2e 20 0a 2a 2a 20 69 66 20 74 68 65 20  ray. .** if the 
14dc3 70 6f 69 6e 74 65 72 20 63 61 6e 20 70 6f 73 73  pointer can poss
14dc4 69 62 6c 79 20 62 65 20 73 68 61 72 65 64 20 77  ibly be shared w
14dc5 69 74 68 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 64  ith.** another d
14dc6 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
14dc7 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f  on..**.** The po
14dc8 69 6e 74 65 72 73 20 61 72 65 20 6b 65 70 74 20  inters are kept 
14dc9 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 20  in sorted order 
14dca 62 79 20 70 42 74 72 65 65 2d 3e 70 42 74 2e 20  by pBtree->pBt. 
14dcb 20 54 68 61 74 0a 2a 2a 20 77 61 79 20 77 68 65   That.** way whe
14dcc 6e 20 77 65 20 67 6f 20 74 6f 20 65 6e 74 65 72  n we go to enter
14dcd 20 61 6c 6c 20 74 68 65 20 6d 75 74 65 78 65 73   all the mutexes
14dce 2c 20 77 65 20 63 61 6e 20 65 6e 74 65 72 20 74  , we can enter t
14dcf 68 65 6d 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20  hem.** in order 
14dd0 77 69 74 68 6f 75 74 20 65 76 65 72 79 20 68 61  without every ha
14dd1 76 69 6e 67 20 74 6f 20 62 61 63 6b 75 70 20 61  ving to backup a
14dd2 6e 64 20 72 65 74 72 79 20 61 6e 64 20 77 69 74  nd retry and wit
14dd3 68 6f 75 74 0a 2a 2a 20 77 6f 72 72 79 69 6e 67  hout.** worrying
14dd4 20 61 62 6f 75 74 20 64 65 61 64 6c 6f 63 6b 2e   about deadlock.
14dd5 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65  .**.** The numbe
14dd6 72 20 6f 66 20 73 68 61 72 65 64 20 62 74 72 65  r of shared btre
14dd7 65 73 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62  es will always b
14dd8 65 20 73 6d 61 6c 6c 20 28 75 73 75 61 6c 6c 79  e small (usually
14dd9 20 30 20 6f 72 20 31 29 0a 2a 2a 20 73 6f 20 61   0 or 1).** so a
14dda 6e 20 69 6e 73 65 72 74 69 6f 6e 20 73 6f 72 74  n insertion sort
14ddb 20 69 73 20 61 6e 20 61 64 65 71 75 61 74 65 20   is an adequate 
14ddc 61 6c 67 6f 72 69 74 68 6d 20 68 65 72 65 2e 0a  algorithm here..
14ddd 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14dde 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
14ddf 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73  reeMutexArrayIns
14de0 65 72 74 28 42 74 72 65 65 4d 75 74 65 78 41 72  ert(BtreeMutexAr
14de1 72 61 79 20 2a 70 41 72 72 61 79 2c 20 42 74 72  ray *pArray, Btr
14de2 65 65 20 2a 70 42 74 72 65 65 29 7b 0a 20 20 69  ee *pBtree){.  i
14de3 6e 74 20 69 2c 20 6a 3b 0a 20 20 42 74 53 68 61  nt i, j;.  BtSha
14de4 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 66 28 20  red *pBt;.  if( 
14de5 70 42 74 72 65 65 3d 3d 30 20 7c 7c 20 70 42 74  pBtree==0 || pBt
14de6 72 65 65 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30  ree->sharable==0
14de7 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66 6e 64   ) return;.#ifnd
14de8 65 66 20 4e 44 45 42 55 47 0a 20 20 7b 0a 20 20  ef NDEBUG.  {.  
14de9 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72    for(i=0; i<pAr
14dea 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b  ray->nMutex; i++
14deb 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
14dec 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b   pArray->aBtree[
14ded 69 5d 21 3d 70 42 74 72 65 65 20 29 3b 0a 20 20  i]!=pBtree );.  
14dee 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20    }.  }.#endif. 
14def 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d   assert( pArray-
14df0 3e 6e 4d 75 74 65 78 3e 3d 30 20 29 3b 0a 20 20  >nMutex>=0 );.  
14df1 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d 3e  assert( pArray->
14df2 6e 4d 75 74 65 78 3c 41 72 72 61 79 53 69 7a 65  nMutex<ArraySize
14df3 28 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 29  (pArray->aBtree)
14df4 2d 31 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 42  -1 );.  pBt = pB
14df5 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 66 6f 72  tree->pBt;.  for
14df6 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e  (i=0; i<pArray->
14df7 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20  nMutex; i++){.  
14df8 20 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79    assert( pArray
14df9 2d 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70 42 74  ->aBtree[i]!=pBt
14dfa 72 65 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70  ree );.    if( p
14dfb 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d  Array->aBtree[i]
14dfc 2d 3e 70 42 74 3e 70 42 74 20 29 7b 0a 20 20 20  ->pBt>pBt ){.   
14dfd 20 20 20 66 6f 72 28 6a 3d 70 41 72 72 61 79 2d     for(j=pArray-
14dfe 3e 6e 4d 75 74 65 78 3b 20 6a 3e 69 3b 20 6a 2d  >nMutex; j>i; j-
14dff 2d 29 7b 0a 20 20 20 20 20 20 20 20 70 41 72 72  -){.        pArr
14e00 61 79 2d 3e 61 42 74 72 65 65 5b 6a 5d 20 3d 20  ay->aBtree[j] = 
14e01 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a  pArray->aBtree[j
14e02 2d 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  -1];.      }.   
14e03 20 20 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65     pArray->aBtre
14e04 65 5b 69 5d 20 3d 20 70 42 74 72 65 65 3b 0a 20  e[i] = pBtree;. 
14e05 20 20 20 20 20 70 41 72 72 61 79 2d 3e 6e 4d 75       pArray->nMu
14e06 74 65 78 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74  tex++;.      ret
14e07 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  urn;.    }.  }. 
14e08 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b   pArray->aBtree[
14e09 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 2b 2b  pArray->nMutex++
14e0a 5d 20 3d 20 70 42 74 72 65 65 3b 0a 7d 0a 0a 2f  ] = pBtree;.}../
14e0b 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d  *.** Enter the m
14e0c 75 74 65 78 20 6f 66 20 65 76 65 72 79 20 62 74  utex of every bt
14e0d 72 65 65 20 69 6e 20 74 68 65 20 61 72 72 61 79  ree in the array
14e0e 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
14e0f 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 74 20  is.** called at 
14e10 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
14e11 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63   sqlite3VdbeExec
14e12 28 29 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73  ().  The mutexes
14e13 20 61 72 65 0a 2a 2a 20 65 78 69 74 65 64 20 61   are.** exited a
14e14 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
14e15 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 2e 0a   same function..
14e16 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14e17 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
14e18 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74  reeMutexArrayEnt
14e19 65 72 28 42 74 72 65 65 4d 75 74 65 78 41 72 72  er(BtreeMutexArr
14e1a 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69  ay *pArray){.  i
14e1b 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
14e1c 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65   i<pArray->nMute
14e1d 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72  x; i++){.    Btr
14e1e 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e  ee *p = pArray->
14e1f 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f  aBtree[i];.    /
14e20 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e  * Some basic san
14e21 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a  ity checking */.
14e22 20 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30      assert( i==0
14e23 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72   || pArray->aBtr
14e24 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e  ee[i-1]->pBt<p->
14e25 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72  pBt );.    asser
14e26 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c  t( !p->locked ||
14e27 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30   p->wantToLock>0
14e28 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 57 65 20 73   );..    /* We s
14e29 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f  hould already ho
14e2a 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  ld a lock on the
14e2b 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
14e2c 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65  tion */.    asse
14e2d 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
14e2e 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
14e2f 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  tex) );..    /* 
14e30 54 68 65 20 42 74 72 65 65 20 69 73 20 73 68 61  The Btree is sha
14e31 72 61 62 6c 65 20 62 65 63 61 75 73 65 20 6f 6e  rable because on
14e32 6c 79 20 73 68 61 72 61 62 6c 65 20 42 74 72 65  ly sharable Btre
14e33 65 73 20 61 72 65 20 65 6e 74 65 72 65 64 0a 20  es are entered. 
14e34 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 61     ** into the a
14e35 72 72 61 79 20 69 6e 20 74 68 65 20 66 69 72 73  rray in the firs
14e36 74 20 70 6c 61 63 65 2e 20 2a 2f 0a 20 20 20 20  t place. */.    
14e37 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61  assert( p->shara
14e38 62 6c 65 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 77  ble );..    p->w
14e39 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20  antToLock++;.   
14e3a 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20   if( !p->locked 
14e3b 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 42 74 72  ){.      lockBtr
14e3c 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20  eeMutex(p);.    
14e3d 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  }.  }.}../*.** L
14e3e 65 61 76 65 20 74 68 65 20 6d 75 74 65 78 20 6f  eave the mutex o
14e3f 66 20 65 76 65 72 79 20 62 74 72 65 65 20 69 6e  f every btree in
14e40 20 74 68 65 20 67 72 6f 75 70 2e 0a 2a 2f 0a 53   the group..*/.S
14e41 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
14e42 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  id sqlite3BtreeM
14e43 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 42  utexArrayLeave(B
14e44 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 2a  treeMutexArray *
14e45 70 41 72 72 61 79 29 7b 0a 20 20 69 6e 74 20 69  pArray){.  int i
14e46 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
14e47 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69  Array->nMutex; i
14e48 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a  ++){.    Btree *
14e49 70 20 3d 20 70 41 72 72 61 79 2d 3e 61 42 74 72  p = pArray->aBtr
14e4a 65 65 5b 69 5d 3b 0a 20 20 20 20 2f 2a 20 53 6f  ee[i];.    /* So
14e4b 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74 79 20  me basic sanity 
14e4c 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20  checking */.    
14e4d 61 73 73 65 72 74 28 20 69 3d 3d 30 20 7c 7c 20  assert( i==0 || 
14e4e 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69  pArray->aBtree[i
14e4f 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20  -1]->pBt<p->pBt 
14e50 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
14e51 2d 3e 6c 6f 63 6b 65 64 20 29 3b 0a 20 20 20 20  ->locked );.    
14e52 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54  assert( p->wantT
14e53 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 0a 20 20 20 20  oLock>0 );..    
14e54 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 72  /* We should alr
14e55 65 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f 63 6b  eady hold a lock
14e56 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
14e57 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
14e58 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
14e59 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
14e5a 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a  >db->mutex) );..
14e5b 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63      p->wantToLoc
14e5c 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  k--;.    if( p->
14e5d 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b  wantToLock==0 ){
14e5e 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72  .      unlockBtr
14e5f 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20  eeMutex(p);.    
14e60 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a 53  }.  }.}..#else.S
14e61 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
14e62 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  id sqlite3BtreeE
14e63 6e 74 65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a  nter(Btree *p){.
14e64 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70    p->pBt->db = p
14e65 2d 3e 64 62 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  ->db;.}.SQLITE_P
14e66 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
14e67 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c  te3BtreeEnterAll
14e68 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
14e69 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
14e6a 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
14e6b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70  +){.    Btree *p
14e6c 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
14e6d 74 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a  t;.    if( p ){.
14e6e 20 20 20 20 20 20 70 2d 3e 70 42 74 2d 3e 64 62        p->pBt->db
14e6f 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 7d 0a   = p->db;.    }.
14e70 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20    }.}.#endif /* 
14e71 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
14e72 53 41 46 45 20 2a 2f 0a 23 65 6e 64 69 66 20 2f  SAFE */.#endif /
14e73 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  * ifndef SQLITE_
14e74 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
14e75 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  E */../*********
14e76 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 6d  ***** End of btm
14e77 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  utex.c *********
14e78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14e79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14e7a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
14e7b 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
14e7c 20 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a   btree.c *******
14e7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14e7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14e7f 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34  ****/./*.** 2004
14e80 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54   April 6.**.** T
14e81 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
14e82 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
14e83 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
14e84 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
14e85 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
14e86 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
14e87 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
14e88 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
14e89 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
14e8a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
14e8b 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
14e8c 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
14e8d 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
14e8e 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
14e8f 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
14e90 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
14e91 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
14e92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14e93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14e94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14e95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14e96 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  *******.** $Id: 
14e97 62 74 72 65 65 2e 63 2c 76 20 31 2e 37 30 35 20  btree.c,v 1.705 
14e98 32 30 30 39 2f 30 38 2f 31 30 20 30 33 3a 35 37  2009/08/10 03:57
14e99 3a 35 38 20 73 68 61 6e 65 20 45 78 70 20 24 0a  :58 shane Exp $.
14e9a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
14e9b 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 65 78 74  implements a ext
14e9c 65 72 6e 61 6c 20 28 64 69 73 6b 2d 62 61 73 65  ernal (disk-base
14e9d 64 29 20 64 61 74 61 62 61 73 65 20 75 73 69 6e  d) database usin
14e9e 67 20 42 54 72 65 65 73 2e 0a 2a 2a 20 53 65 65  g BTrees..** See
14e9f 20 74 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d   the header comm
14ea0 65 6e 74 20 6f 6e 20 22 62 74 72 65 65 49 6e 74  ent on "btreeInt
14ea1 2e 68 22 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  .h" for addition
14ea2 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  al information..
14ea3 2a 2a 20 49 6e 63 6c 75 64 69 6e 67 20 61 20 64  ** Including a d
14ea4 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 66 69  escription of fi
14ea5 6c 65 20 66 6f 72 6d 61 74 20 61 6e 64 20 61 6e  le format and an
14ea6 20 6f 76 65 72 76 69 65 77 20 6f 66 20 6f 70 65   overview of ope
14ea7 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  ration..*/../*.*
14ea8 2a 20 54 68 65 20 68 65 61 64 65 72 20 73 74 72  * The header str
14ea9 69 6e 67 20 74 68 61 74 20 61 70 70 65 61 72 73  ing that appears
14eaa 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   at the beginnin
14eab 67 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20 53 51  g of every.** SQ
14eac 4c 69 74 65 20 64 61 74 61 62 61 73 65 2e 0a 2a  Lite database..*
14ead 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  /.static const c
14eae 68 61 72 20 7a 4d 61 67 69 63 48 65 61 64 65 72  har zMagicHeader
14eaf 5b 5d 20 3d 20 53 51 4c 49 54 45 5f 46 49 4c 45  [] = SQLITE_FILE
14eb0 5f 48 45 41 44 45 52 3b 0a 0a 2f 2a 0a 2a 2a 20  _HEADER;../*.** 
14eb1 53 65 74 20 74 68 69 73 20 67 6c 6f 62 61 6c 20  Set this global 
14eb2 76 61 72 69 61 62 6c 65 20 74 6f 20 31 20 74 6f  variable to 1 to
14eb3 20 65 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20   enable tracing 
14eb4 75 73 69 6e 67 20 74 68 65 20 54 52 41 43 45 0a  using the TRACE.
14eb5 2a 2a 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66  ** macro..*/.#if
14eb6 20 30 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74   0.int sqlite3Bt
14eb7 72 65 65 54 72 61 63 65 3d 31 3b 20 20 2f 2a 20  reeTrace=1;  /* 
14eb8 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 74  True to enable t
14eb9 72 61 63 69 6e 67 20 2a 2f 0a 23 20 64 65 66 69  racing */.# defi
14eba 6e 65 20 54 52 41 43 45 28 58 29 20 20 69 66 28  ne TRACE(X)  if(
14ebb 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 61 63  sqlite3BtreeTrac
14ebc 65 29 7b 70 72 69 6e 74 66 20 58 3b 66 66 6c 75  e){printf X;fflu
14ebd 73 68 28 73 74 64 6f 75 74 29 3b 7d 0a 23 65 6c  sh(stdout);}.#el
14ebe 73 65 0a 23 20 64 65 66 69 6e 65 20 54 52 41 43  se.# define TRAC
14ebf 45 28 58 29 0a 23 65 6e 64 69 66 0a 0a 0a 0a 23  E(X).#endif....#
14ec0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
14ec1 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
14ec2 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 20  /*.** A list of 
14ec3 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 73  BtShared objects
14ec4 20 74 68 61 74 20 61 72 65 20 65 6c 69 67 69 62   that are eligib
14ec5 6c 65 20 66 6f 72 20 70 61 72 74 69 63 69 70 61  le for participa
14ec6 74 69 6f 6e 0a 2a 2a 20 69 6e 20 73 68 61 72 65  tion.** in share
14ec7 64 20 63 61 63 68 65 2e 20 20 54 68 69 73 20 76  d cache.  This v
14ec8 61 72 69 61 62 6c 65 20 68 61 73 20 66 69 6c 65  ariable has file
14ec9 20 73 63 6f 70 65 20 64 75 72 69 6e 67 20 6e 6f   scope during no
14eca 72 6d 61 6c 20 62 75 69 6c 64 73 2c 0a 2a 2a 20  rmal builds,.** 
14ecb 62 75 74 20 74 68 65 20 74 65 73 74 20 68 61 72  but the test har
14ecc 6e 65 73 73 20 6e 65 65 64 73 20 74 6f 20 61 63  ness needs to ac
14ecd 63 65 73 73 20 69 74 20 73 6f 20 77 65 20 6d 61  cess it so we ma
14ece 6b 65 20 69 74 20 67 6c 6f 62 61 6c 20 66 6f 72  ke it global for
14ecf 20 0a 2a 2a 20 74 65 73 74 20 62 75 69 6c 64 73   .** test builds
14ed0 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74  ..**.** Access t
14ed1 6f 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20  o this variable 
14ed2 69 73 20 70 72 6f 74 65 63 74 65 64 20 62 79 20  is protected by 
14ed3 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
14ed4 54 49 43 5f 4d 41 53 54 45 52 2e 0a 2a 2f 0a 23  TIC_MASTER..*/.#
14ed5 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
14ed6 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
14ed7 20 42 74 53 68 61 72 65 64 20 2a 53 51 4c 49 54   BtShared *SQLIT
14ed8 45 5f 57 53 44 20 73 71 6c 69 74 65 33 53 68 61  E_WSD sqlite3Sha
14ed9 72 65 64 43 61 63 68 65 4c 69 73 74 20 3d 20 30  redCacheList = 0
14eda 3b 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 42  ;.#else.static B
14edb 74 53 68 61 72 65 64 20 2a 53 51 4c 49 54 45 5f  tShared *SQLITE_
14edc 57 53 44 20 73 71 6c 69 74 65 33 53 68 61 72 65  WSD sqlite3Share
14edd 64 43 61 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a  dCacheList = 0;.
14ede 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a  #endif.#endif /*
14edf 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
14ee0 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69  RED_CACHE */..#i
14ee1 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
14ee2 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f  T_SHARED_CACHE./
14ee3 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64  *.** Enable or d
14ee4 69 73 61 62 6c 65 20 74 68 65 20 73 68 61 72 65  isable the share
14ee5 64 20 70 61 67 65 72 20 61 6e 64 20 73 63 68 65  d pager and sche
14ee6 6d 61 20 66 65 61 74 75 72 65 73 2e 0a 2a 2a 0a  ma features..**.
14ee7 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
14ee8 68 61 73 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e  has no effect on
14ee9 20 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 61   existing databa
14eea 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a  se connections..
14eeb 2a 2a 20 54 68 65 20 73 68 61 72 65 64 20 63 61  ** The shared ca
14eec 63 68 65 20 73 65 74 74 69 6e 67 20 65 66 66 65  che setting effe
14eed 63 74 73 20 6f 6e 6c 79 20 66 75 74 75 72 65 20  cts only future 
14eee 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69  calls to.** sqli
14eef 74 65 33 5f 6f 70 65 6e 28 29 2c 20 73 71 6c 69  te3_open(), sqli
14ef0 74 65 33 5f 6f 70 65 6e 31 36 28 29 2c 20 6f 72  te3_open16(), or
14ef1 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32   sqlite3_open_v2
14ef2 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ()..*/.SQLITE_AP
14ef3 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e  I int sqlite3_en
14ef4 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68  able_shared_cach
14ef5 65 28 69 6e 74 20 65 6e 61 62 6c 65 29 7b 0a 20  e(int enable){. 
14ef6 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
14ef7 6e 66 69 67 2e 73 68 61 72 65 64 43 61 63 68 65  nfig.sharedCache
14ef8 45 6e 61 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65  Enabled = enable
14ef9 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
14efa 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  E_OK;.}.#endif..
14efb 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
14efc 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
14efd 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20  E.  /*.  ** The 
14efe 66 75 6e 63 74 69 6f 6e 73 20 71 75 65 72 79 53  functions queryS
14eff 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
14f00 6f 63 6b 28 29 2c 20 73 65 74 53 68 61 72 65 64  ock(), setShared
14f01 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29  CacheTableLock()
14f02 2c 0a 20 20 2a 2a 20 61 6e 64 20 63 6c 65 61 72  ,.  ** and clear
14f03 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61  AllSharedCacheTa
14f04 62 6c 65 4c 6f 63 6b 73 28 29 0a 20 20 2a 2a 20  bleLocks().  ** 
14f05 6d 61 6e 69 70 75 6c 61 74 65 20 65 6e 74 72 69  manipulate entri
14f06 65 73 20 69 6e 20 74 68 65 20 42 74 53 68 61 72  es in the BtShar
14f07 65 64 2e 70 4c 6f 63 6b 20 6c 69 6e 6b 65 64 20  ed.pLock linked 
14f08 6c 69 73 74 20 75 73 65 64 20 74 6f 20 73 74 6f  list used to sto
14f09 72 65 0a 20 20 2a 2a 20 73 68 61 72 65 64 2d 63  re.  ** shared-c
14f0a 61 63 68 65 20 74 61 62 6c 65 20 6c 65 76 65 6c  ache table level
14f0b 20 6c 6f 63 6b 73 2e 20 49 66 20 74 68 65 20 6c   locks. If the l
14f0c 69 62 72 61 72 79 20 69 73 20 63 6f 6d 70 69 6c  ibrary is compil
14f0d 65 64 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a  ed with the.  **
14f0e 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 66 65   shared-cache fe
14f0f 61 74 75 72 65 20 64 69 73 61 62 6c 65 64 2c 20  ature disabled, 
14f10 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6f 6e  then there is on
14f11 6c 79 20 65 76 65 72 20 6f 6e 65 20 75 73 65 72  ly ever one user
14f12 0a 20 20 2a 2a 20 6f 66 20 65 61 63 68 20 42 74  .  ** of each Bt
14f13 53 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65  Shared structure
14f14 20 61 6e 64 20 73 6f 20 74 68 69 73 20 6c 6f 63   and so this loc
14f15 6b 69 6e 67 20 69 73 20 6e 6f 74 20 6e 65 63 65  king is not nece
14f16 73 73 61 72 79 2e 20 0a 20 20 2a 2a 20 53 6f 20  ssary. .  ** So 
14f17 64 65 66 69 6e 65 20 74 68 65 20 6c 6f 63 6b 20  define the lock 
14f18 72 65 6c 61 74 65 64 20 66 75 6e 63 74 69 6f 6e  related function
14f19 73 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 20 20 2a  s as no-ops..  *
14f1a 2f 0a 20 20 23 64 65 66 69 6e 65 20 71 75 65 72  /.  #define quer
14f1b 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  ySharedCacheTabl
14f1c 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20 53 51 4c  eLock(a,b,c) SQL
14f1d 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65  ITE_OK.  #define
14f1e 20 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54   setSharedCacheT
14f1f 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20  ableLock(a,b,c) 
14f20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66  SQLITE_OK.  #def
14f21 69 6e 65 20 63 6c 65 61 72 41 6c 6c 53 68 61 72  ine clearAllShar
14f22 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b  edCacheTableLock
14f23 73 28 61 29 0a 20 20 23 64 65 66 69 6e 65 20 64  s(a).  #define d
14f24 6f 77 6e 67 72 61 64 65 41 6c 6c 53 68 61 72 65  owngradeAllShare
14f25 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73  dCacheTableLocks
14f26 28 61 29 0a 20 20 23 64 65 66 69 6e 65 20 68 61  (a).  #define ha
14f27 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  sSharedCacheTabl
14f28 65 4c 6f 63 6b 28 61 2c 62 2c 63 2c 64 29 20 31  eLock(a,b,c,d) 1
14f29 0a 20 20 23 64 65 66 69 6e 65 20 68 61 73 52 65  .  #define hasRe
14f2a 61 64 43 6f 6e 66 6c 69 63 74 73 28 61 2c 20 62  adConflicts(a, b
14f2b 29 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  ) 0.#endif..#ifn
14f2c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
14f2d 53 48 41 52 45 44 5f 43 41 43 48 45 0a 0a 23 69  SHARED_CACHE..#i
14f2e 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
14f2f 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  G./*.** This fun
14f30 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73  ction is only us
14f31 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 6e  ed as part of an
14f32 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
14f33 65 6e 74 2e 20 49 74 20 63 68 65 63 6b 73 0a 2a  ent. It checks.*
14f34 2a 20 74 68 61 74 20 63 6f 6e 6e 65 63 74 69 6f  * that connectio
14f35 6e 20 70 20 68 6f 6c 64 73 20 74 68 65 20 72 65  n p holds the re
14f36 71 75 69 72 65 64 20 6c 6f 63 6b 73 20 74 6f 20  quired locks to 
14f37 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 6f  read or write to
14f38 20 74 68 65 20 0a 2a 2a 20 62 2d 74 72 65 65 20   the .** b-tree 
14f39 77 69 74 68 20 72 6f 6f 74 20 70 61 67 65 20 69  with root page i
14f3a 52 6f 6f 74 2e 20 49 66 20 73 6f 2c 20 74 72 75  Root. If so, tru
14f3b 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f  e is returned. O
14f3c 74 68 65 72 77 69 73 65 2c 20 66 61 6c 73 65 2e  therwise, false.
14f3d 20 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65   .** For example
14f3e 2c 20 77 68 65 6e 20 77 72 69 74 69 6e 67 20 74  , when writing t
14f3f 6f 20 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65  o a table b-tree
14f40 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20   with root-page 
14f41 69 52 6f 6f 74 20 76 69 61 20 0a 2a 2a 20 42 74  iRoot via .** Bt
14f42 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70  ree connection p
14f43 42 74 72 65 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  Btree:.**.**    
14f44 61 73 73 65 72 74 28 20 68 61 73 53 68 61 72 65  assert( hasShare
14f45 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28  dCacheTableLock(
14f46 70 42 74 72 65 65 2c 20 69 52 6f 6f 74 2c 20 30  pBtree, iRoot, 0
14f47 2c 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 29 3b  , WRITE_LOCK) );
14f48 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 72 69 74  .**.** When writ
14f49 69 6e 67 20 74 6f 20 61 6e 20 69 6e 64 65 78 20  ing to an index 
14f4a 62 2d 74 72 65 65 20 74 68 61 74 20 72 65 73 69  b-tree that resi
14f4b 64 65 73 20 69 6e 20 61 20 73 68 61 72 61 62 6c  des in a sharabl
14f4c 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20  e database, the 
14f4d 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 68 6f 75 6c  .** caller shoul
14f4e 64 20 68 61 76 65 20 66 69 72 73 74 20 6f 62 74  d have first obt
14f4f 61 69 6e 65 64 20 61 20 6c 6f 63 6b 20 73 70 65  ained a lock spe
14f50 63 69 66 79 69 6e 67 20 74 68 65 20 72 6f 6f 74  cifying the root
14f51 20 70 61 67 65 20 6f 66 0a 2a 2a 20 74 68 65 20   page of.** the 
14f52 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 61  corresponding ta
14f53 62 6c 65 20 62 2d 74 72 65 65 2e 20 54 68 69 73  ble b-tree. This
14f54 20 6d 61 6b 65 73 20 74 68 69 6e 67 73 20 61 20   makes things a 
14f55 62 69 74 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63  bit more complic
14f56 61 74 65 64 2c 0a 2a 2a 20 61 73 20 74 68 69 73  ated,.** as this
14f57 20 6d 6f 64 75 6c 65 20 74 72 65 61 74 73 20 65   module treats e
14f58 61 63 68 20 62 2d 74 72 65 65 20 61 73 20 61 20  ach b-tree as a 
14f59 73 65 70 61 72 61 74 65 20 73 74 72 75 63 74 75  separate structu
14f5a 72 65 2e 20 54 6f 20 64 65 74 65 72 6d 69 6e 65  re. To determine
14f5b 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 62 2d  .** the table b-
14f5c 74 72 65 65 20 63 6f 72 72 65 73 70 6f 6e 64 69  tree correspondi
14f5d 6e 67 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20  ng to the index 
14f5e 62 2d 74 72 65 65 20 62 65 69 6e 67 20 77 72 69  b-tree being wri
14f5f 74 74 65 6e 2c 20 74 68 69 73 0a 2a 2a 20 66 75  tten, this.** fu
14f60 6e 63 74 69 6f 6e 20 68 61 73 20 74 6f 20 73 65  nction has to se
14f61 61 72 63 68 20 74 68 72 6f 75 67 68 20 74 68 65  arch through the
14f62 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
14f63 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61 64 20  ..**.** Instead 
14f64 6f 66 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  of a lock on the
14f65 20 62 2d 74 72 65 65 20 72 6f 6f 74 65 64 20 61   b-tree rooted a
14f66 74 20 70 61 67 65 20 69 52 6f 6f 74 2c 20 74 68  t page iRoot, th
14f67 65 20 63 61 6c 6c 65 72 20 6d 61 79 0a 2a 2a 20  e caller may.** 
14f68 68 6f 6c 64 20 61 20 77 72 69 74 65 2d 6c 6f 63  hold a write-loc
14f69 6b 20 6f 6e 20 74 68 65 20 73 63 68 65 6d 61 20  k on the schema 
14f6a 74 61 62 6c 65 20 28 72 6f 6f 74 20 70 61 67 65  table (root page
14f6b 20 31 29 2e 20 54 68 69 73 20 69 73 20 61 6c 73   1). This is als
14f6c 6f 0a 2a 2a 20 61 63 63 65 70 74 61 62 6c 65 2e  o.** acceptable.
14f6d 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68  .*/.static int h
14f6e 61 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62  asSharedCacheTab
14f6f 6c 65 4c 6f 63 6b 28 0a 20 20 42 74 72 65 65 20  leLock(.  Btree 
14f70 2a 70 42 74 72 65 65 2c 20 20 20 20 20 20 20 20  *pBtree,        
14f71 20 2f 2a 20 48 61 6e 64 6c 65 20 74 68 61 74 20   /* Handle that 
14f72 6d 75 73 74 20 68 6f 6c 64 20 6c 6f 63 6b 20 2a  must hold lock *
14f73 2f 0a 20 20 50 67 6e 6f 20 69 52 6f 6f 74 2c 20  /.  Pgno iRoot, 
14f74 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f             /* Ro
14f75 6f 74 20 70 61 67 65 20 6f 66 20 62 2d 74 72 65  ot page of b-tre
14f76 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 6e 64  e */.  int isInd
14f77 65 78 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ex,           /*
14f78 20 54 72 75 65 20 69 66 20 69 52 6f 6f 74 20 69   True if iRoot i
14f79 73 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 61 6e  s the root of an
14f7a 20 69 6e 64 65 78 20 62 2d 74 72 65 65 20 2a 2f   index b-tree */
14f7b 0a 20 20 69 6e 74 20 65 4c 6f 63 6b 54 79 70 65  .  int eLockType
14f7c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 71            /* Req
14f7d 75 69 72 65 64 20 6c 6f 63 6b 20 74 79 70 65 20  uired lock type 
14f7e 28 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 52  (READ_LOCK or WR
14f7f 49 54 45 5f 4c 4f 43 4b 29 20 2a 2f 0a 29 7b 0a  ITE_LOCK) */.){.
14f80 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
14f81 61 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 70 42  a = (Schema *)pB
14f82 74 72 65 65 2d 3e 70 42 74 2d 3e 70 53 63 68 65  tree->pBt->pSche
14f83 6d 61 3b 0a 20 20 50 67 6e 6f 20 69 54 61 62 20  ma;.  Pgno iTab 
14f84 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70  = 0;.  BtLock *p
14f85 4c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  Lock;..  /* If t
14f86 68 69 73 20 62 2d 74 72 65 65 20 64 61 74 61 62  his b-tree datab
14f87 61 73 65 20 69 73 20 6e 6f 74 20 73 68 61 72 65  ase is not share
14f88 61 62 6c 65 2c 20 6f 72 20 69 66 20 74 68 65 20  able, or if the 
14f89 63 6c 69 65 6e 74 20 69 73 20 72 65 61 64 69 6e  client is readin
14f8a 67 0a 20 20 2a 2a 20 61 6e 64 20 68 61 73 20 74  g.  ** and has t
14f8b 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74  he read-uncommit
14f8c 74 65 64 20 66 6c 61 67 20 73 65 74 2c 20 74 68  ted flag set, th
14f8d 65 6e 20 6e 6f 20 6c 6f 63 6b 20 69 73 20 72 65  en no lock is re
14f8e 71 75 69 72 65 64 2e 20 0a 20 20 2a 2a 20 49 6e  quired. .  ** In
14f8f 20 74 68 65 73 65 20 63 61 73 65 73 20 72 65 74   these cases ret
14f90 75 72 6e 20 74 72 75 65 20 69 6d 6d 65 64 69 61  urn true immedia
14f91 74 65 6c 79 2e 20 20 49 66 20 74 68 65 20 63 6c  tely.  If the cl
14f92 69 65 6e 74 20 69 73 20 72 65 61 64 69 6e 67 20  ient is reading 
14f93 0a 20 20 2a 2a 20 6f 72 20 77 72 69 74 69 6e 67  .  ** or writing
14f94 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 65   an index b-tree
14f95 2c 20 62 75 74 20 74 68 65 20 73 63 68 65 6d 61  , but the schema
14f96 20 69 73 20 6e 6f 74 20 6c 6f 61 64 65 64 2c 20   is not loaded, 
14f97 74 68 65 6e 20 72 65 74 75 72 6e 0a 20 20 2a 2a  then return.  **
14f98 20 74 72 75 65 20 61 6c 73 6f 2e 20 49 6e 20 74   true also. In t
14f99 68 69 73 20 63 61 73 65 20 74 68 65 20 6c 6f 63  his case the loc
14f9a 6b 20 69 73 20 72 65 71 75 69 72 65 64 2c 20 62  k is required, b
14f9b 75 74 20 69 74 20 69 73 20 74 6f 6f 20 64 69 66  ut it is too dif
14f9c 66 69 63 75 6c 74 0a 20 20 2a 2a 20 74 6f 20 63  ficult.  ** to c
14f9d 68 65 63 6b 20 69 66 20 74 68 65 20 63 6c 69 65  heck if the clie
14f9e 6e 74 20 61 63 74 75 61 6c 6c 79 20 68 6f 6c 64  nt actually hold
14f9f 73 20 69 74 2e 20 54 68 69 73 20 64 6f 65 73 6e  s it. This doesn
14fa0 27 74 20 68 61 70 70 65 6e 20 76 65 72 79 0a 20  't happen very. 
14fa1 20 2a 2a 20 6f 66 74 65 6e 2e 20 20 2a 2f 0a 20   ** often.  */. 
14fa2 20 69 66 28 20 28 70 42 74 72 65 65 2d 3e 73 68   if( (pBtree->sh
14fa3 61 72 61 62 6c 65 3d 3d 30 29 0a 20 20 20 7c 7c  arable==0).   ||
14fa4 20 28 65 4c 6f 63 6b 54 79 70 65 3d 3d 52 45 41   (eLockType==REA
14fa5 44 5f 4c 4f 43 4b 20 26 26 20 28 70 42 74 72 65  D_LOCK && (pBtre
14fa6 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  e->db->flags & S
14fa7 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d  QLITE_ReadUncomm
14fa8 69 74 74 65 64 29 29 0a 20 20 20 7c 7c 20 28 69  itted)).   || (i
14fa9 73 49 6e 64 65 78 20 26 26 20 28 21 70 53 63 68  sIndex && (!pSch
14faa 65 6d 61 20 7c 7c 20 28 70 53 63 68 65 6d 61 2d  ema || (pSchema-
14fab 3e 66 6c 61 67 73 26 44 42 5f 53 63 68 65 6d 61  >flags&DB_Schema
14fac 4c 6f 61 64 65 64 29 3d 3d 30 20 29 29 0a 20 20  Loaded)==0 )).  
14fad 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  ){.    return 1;
14fae 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72  .  }..  /* Figur
14faf 65 20 6f 75 74 20 74 68 65 20 72 6f 6f 74 2d 70  e out the root-p
14fb0 61 67 65 20 74 68 61 74 20 74 68 65 20 6c 6f 63  age that the loc
14fb1 6b 20 73 68 6f 75 6c 64 20 62 65 20 68 65 6c 64  k should be held
14fb2 20 6f 6e 2e 20 46 6f 72 20 74 61 62 6c 65 0a 20   on. For table. 
14fb3 20 2a 2a 20 62 2d 74 72 65 65 73 2c 20 74 68 69   ** b-trees, thi
14fb4 73 20 69 73 20 6a 75 73 74 20 74 68 65 20 72 6f  s is just the ro
14fb5 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 62  ot page of the b
14fb6 2d 74 72 65 65 20 62 65 69 6e 67 20 72 65 61 64  -tree being read
14fb7 20 6f 72 0a 20 20 2a 2a 20 77 72 69 74 74 65 6e   or.  ** written
14fb8 2e 20 46 6f 72 20 69 6e 64 65 78 20 62 2d 74 72  . For index b-tr
14fb9 65 65 73 2c 20 69 74 20 69 73 20 74 68 65 20 72  ees, it is the r
14fba 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20  oot page of the 
14fbb 61 73 73 6f 63 69 61 74 65 64 0a 20 20 2a 2a 20  associated.  ** 
14fbc 74 61 62 6c 65 2e 20 20 2a 2f 0a 20 20 69 66 28  table.  */.  if(
14fbd 20 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20   isIndex ){.    
14fbe 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 20 20 20  HashElem *p;.   
14fbf 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 48 61 73   for(p=sqliteHas
14fc0 68 46 69 72 73 74 28 26 70 53 63 68 65 6d 61 2d  hFirst(&pSchema-
14fc1 3e 69 64 78 48 61 73 68 29 3b 20 70 3b 20 70 3d  >idxHash); p; p=
14fc2 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70  sqliteHashNext(p
14fc3 29 29 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20  )){.      Index 
14fc4 2a 70 49 64 78 20 3d 20 28 49 6e 64 65 78 20 2a  *pIdx = (Index *
14fc5 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28  )sqliteHashData(
14fc6 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49  p);.      if( pI
14fc7 64 78 2d 3e 74 6e 75 6d 3d 3d 28 69 6e 74 29 69  dx->tnum==(int)i
14fc8 52 6f 6f 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Root ){.        
14fc9 69 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54 61  iTab = pIdx->pTa
14fca 62 6c 65 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 20  ble->tnum;.     
14fcb 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
14fcc 7b 0a 20 20 20 20 69 54 61 62 20 3d 20 69 52 6f  {.    iTab = iRo
14fcd 6f 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65  ot;.  }..  /* Se
14fce 61 72 63 68 20 66 6f 72 20 74 68 65 20 72 65 71  arch for the req
14fcf 75 69 72 65 64 20 6c 6f 63 6b 2e 20 45 69 74 68  uired lock. Eith
14fd0 65 72 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20  er a write-lock 
14fd1 6f 6e 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61  on root-page iTa
14fd2 62 2c 20 61 20 0a 20 20 2a 2a 20 77 72 69 74 65  b, a .  ** write
14fd3 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 63 68  -lock on the sch
14fd4 65 6d 61 20 74 61 62 6c 65 2c 20 6f 72 20 28 69  ema table, or (i
14fd5 66 20 74 68 65 20 63 6c 69 65 6e 74 20 69 73 20  f the client is 
14fd6 72 65 61 64 69 6e 67 29 20 61 0a 20 20 2a 2a 20  reading) a.  ** 
14fd7 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 69 54 61  read-lock on iTa
14fd8 62 20 77 69 6c 6c 20 73 75 66 66 69 63 65 2e 20  b will suffice. 
14fd9 52 65 74 75 72 6e 20 31 20 69 66 20 61 6e 79 20  Return 1 if any 
14fda 6f 66 20 74 68 65 73 65 20 61 72 65 20 66 6f 75  of these are fou
14fdb 6e 64 2e 20 20 2a 2f 0a 20 20 66 6f 72 28 70 4c  nd.  */.  for(pL
14fdc 6f 63 6b 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d  ock=pBtree->pBt-
14fdd 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3b 20 70  >pLock; pLock; p
14fde 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e 70 4e 65 78  Lock=pLock->pNex
14fdf 74 29 7b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63  t){.    if( pLoc
14fe0 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 42 74 72 65  k->pBtree==pBtre
14fe1 65 20 0a 20 20 20 20 20 26 26 20 28 70 4c 6f 63  e .     && (pLoc
14fe2 6b 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20  k->iTable==iTab 
14fe3 7c 7c 20 28 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b  || (pLock->eLock
14fe4 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 26 26 20  ==WRITE_LOCK && 
14fe5 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 3d 3d 31  pLock->iTable==1
14fe6 29 29 0a 20 20 20 20 20 26 26 20 70 4c 6f 63 6b  )).     && pLock
14fe7 2d 3e 65 4c 6f 63 6b 3e 3d 65 4c 6f 63 6b 54 79  ->eLock>=eLockTy
14fe8 70 65 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  pe .    ){.     
14fe9 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
14fea 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 61 69 6c 65  .  }..  /* Faile
14feb 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 72 65  d to find the re
14fec 71 75 69 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0a  quired lock. */.
14fed 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
14fee 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
14fef 6f 6e 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20  on is also used 
14ff0 61 73 20 70 61 72 74 20 6f 66 20 61 73 73 65 72  as part of asser
14ff1 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f  t() statements o
14ff2 6e 6c 79 2e 20 49 74 20 0a 2a 2a 20 72 65 74 75  nly. It .** retu
14ff3 72 6e 73 20 74 72 75 65 20 69 66 20 74 68 65 72  rns true if ther
14ff4 65 20 65 78 69 73 74 20 6f 6e 65 20 6f 72 20 6d  e exist one or m
14ff5 6f 72 65 20 63 75 72 73 6f 72 73 20 6f 70 65 6e  ore cursors open
14ff6 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 0a 2a   on the table .*
14ff7 2a 20 77 69 74 68 20 72 6f 6f 74 20 70 61 67 65  * with root page
14ff8 20 69 52 6f 6f 74 20 74 68 61 74 20 64 6f 20 6e   iRoot that do n
14ff9 6f 74 20 62 65 6c 6f 6e 67 20 74 6f 20 65 69 74  ot belong to eit
14ffa 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70  her connection p
14ffb 42 74 72 65 65 20 0a 2a 2a 20 6f 72 20 73 6f 6d  Btree .** or som
14ffc 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69  e other connecti
14ffd 6f 6e 20 74 68 61 74 20 68 61 73 20 74 68 65 20  on that has the 
14ffe 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64  read-uncommitted
14fff 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2a 0a 2a 2a   flag set..**.**
15000 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 62 65   For example, be
15001 66 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f 20  fore writing to 
15002 70 61 67 65 20 69 52 6f 6f 74 3a 0a 2a 2a 0a 2a  page iRoot:.**.*
15003 2a 20 20 20 20 61 73 73 65 72 74 28 20 21 68 61  *    assert( !ha
15004 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70  sReadConflicts(p
15005 42 74 72 65 65 2c 20 69 52 6f 6f 74 29 20 29 3b  Btree, iRoot) );
15006 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68  .*/.static int h
15007 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28  asReadConflicts(
15008 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 50  Btree *pBtree, P
15009 67 6e 6f 20 69 52 6f 6f 74 29 7b 0a 20 20 42 74  gno iRoot){.  Bt
1500a 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 66 6f 72  Cursor *p;.  for
1500b 28 70 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e  (p=pBtree->pBt->
1500c 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d  pCursor; p; p=p-
1500d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28  >pNext){.    if(
1500e 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52   p->pgnoRoot==iR
1500f 6f 6f 74 20 0a 20 20 20 20 20 26 26 20 70 2d 3e  oot .     && p->
15010 70 42 74 72 65 65 21 3d 70 42 74 72 65 65 0a 20  pBtree!=pBtree. 
15011 20 20 20 20 26 26 20 30 3d 3d 28 70 2d 3e 70 42      && 0==(p->pB
15012 74 72 65 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20  tree->db->flags 
15013 26 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63  & SQLITE_ReadUnc
15014 6f 6d 6d 69 74 74 65 64 29 0a 20 20 20 20 29 7b  ommitted).    ){
15015 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
15016 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
15017 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20  urn 0;.}.#endif 
15018 20 20 20 2f 2a 20 23 69 66 64 65 66 20 53 51 4c     /* #ifdef SQL
15019 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a  ITE_DEBUG */../*
1501a 0a 2a 2a 20 51 75 65 72 79 20 74 6f 20 73 65 65  .** Query to see
1501b 20 69 66 20 62 74 72 65 65 20 68 61 6e 64 6c 65   if btree handle
1501c 20 70 20 6d 61 79 20 6f 62 74 61 69 6e 20 61 20   p may obtain a 
1501d 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20 65 4c 6f  lock of type eLo
1501e 63 6b 20 0a 2a 2a 20 28 52 45 41 44 5f 4c 4f 43  ck .** (READ_LOC
1501f 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29  K or WRITE_LOCK)
15020 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69   on the table wi
15021 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61  th root-page iTa
15022 62 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 53 51 4c  b. Return.** SQL
15023 49 54 45 5f 4f 4b 20 69 66 20 74 68 65 20 6c 6f  ITE_OK if the lo
15024 63 6b 20 6d 61 79 20 62 65 20 6f 62 74 61 69 6e  ck may be obtain
15025 65 64 20 28 62 79 20 63 61 6c 6c 69 6e 67 0a 2a  ed (by calling.*
15026 2a 20 73 65 74 53 68 61 72 65 64 43 61 63 68 65  * setSharedCache
15027 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2c 20 6f 72  TableLock()), or
15028 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69   SQLITE_LOCKED i
15029 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  f not..*/.static
1502a 20 69 6e 74 20 71 75 65 72 79 53 68 61 72 65 64   int queryShared
1502b 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 42  CacheTableLock(B
1502c 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54  tree *p, Pgno iT
1502d 61 62 2c 20 75 38 20 65 4c 6f 63 6b 29 7b 0a 20  ab, u8 eLock){. 
1502e 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
1502f 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 4c 6f 63   p->pBt;.  BtLoc
15030 6b 20 2a 70 49 74 65 72 3b 0a 0a 20 20 61 73 73  k *pIter;..  ass
15031 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
15032 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29  eHoldsMutex(p) )
15033 3b 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63  ;.  assert( eLoc
15034 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20  k==READ_LOCK || 
15035 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43  eLock==WRITE_LOC
15036 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  K );.  assert( p
15037 2d 3e 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73  ->db!=0 );.  ass
15038 65 72 74 28 20 21 28 70 2d 3e 64 62 2d 3e 66 6c  ert( !(p->db->fl
15039 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55  ags&SQLITE_ReadU
1503a 6e 63 6f 6d 6d 69 74 74 65 64 29 7c 7c 65 4c 6f  ncommitted)||eLo
1503b 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 7c 7c  ck==WRITE_LOCK||
1503c 69 54 61 62 3d 3d 31 20 29 3b 0a 20 20 0a 20 20  iTab==1 );.  .  
1503d 2f 2a 20 49 66 20 72 65 71 75 65 73 74 69 6e 67  /* If requesting
1503e 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 2c 20 74   a write-lock, t
1503f 68 65 6e 20 74 68 65 20 42 74 72 65 65 20 6d 75  hen the Btree mu
15040 73 74 20 68 61 76 65 20 61 6e 20 6f 70 65 6e 20  st have an open 
15041 77 72 69 74 65 0a 20 20 2a 2a 20 74 72 61 6e 73  write.  ** trans
15042 61 63 74 69 6f 6e 20 6f 6e 20 74 68 69 73 20 66  action on this f
15043 69 6c 65 2e 20 41 6e 64 2c 20 6f 62 76 69 6f 75  ile. And, obviou
15044 73 6c 79 2c 20 66 6f 72 20 74 68 69 73 20 74 6f  sly, for this to
15045 20 62 65 20 73 6f 20 74 68 65 72 65 20 0a 20 20   be so there .  
15046 2a 2a 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70  ** must be an op
15047 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 63  en write transac
15048 74 69 6f 6e 20 6f 6e 20 74 68 65 20 66 69 6c 65  tion on the file
15049 20 69 74 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20   itself..  */.  
1504a 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52  assert( eLock==R
1504b 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 28 70 3d 3d  EAD_LOCK || (p==
1504c 70 42 74 2d 3e 70 57 72 69 74 65 72 20 26 26 20  pBt->pWriter && 
1504d 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e  p->inTrans==TRAN
1504e 53 5f 57 52 49 54 45 29 20 29 3b 0a 20 20 61 73  S_WRITE) );.  as
1504f 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41  sert( eLock==REA
15050 44 5f 4c 4f 43 4b 20 7c 7c 20 70 42 74 2d 3e 69  D_LOCK || pBt->i
15051 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52  nTransaction==TR
15052 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 0a  ANS_WRITE );.  .
15053 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20 6e    /* This is a n
15054 6f 2d 6f 70 20 69 66 20 74 68 65 20 73 68 61 72  o-op if the shar
15055 65 64 2d 63 61 63 68 65 20 69 73 20 6e 6f 74 20  ed-cache is not 
15056 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 69 66 28  enabled */.  if(
15057 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b   !p->sharable ){
15058 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
15059 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  TE_OK;.  }..  /*
1505a 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 63   If some other c
1505b 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c  onnection is hol
1505c 64 69 6e 67 20 61 6e 20 65 78 63 6c 75 73 69 76  ding an exclusiv
1505d 65 20 6c 6f 63 6b 2c 20 74 68 65 0a 20 20 2a 2a  e lock, the.  **
1505e 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 20   requested lock 
1505f 6d 61 79 20 6e 6f 74 20 62 65 20 6f 62 74 61 69  may not be obtai
15060 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ned..  */.  if( 
15061 70 42 74 2d 3e 70 57 72 69 74 65 72 21 3d 70 20  pBt->pWriter!=p 
15062 26 26 20 70 42 74 2d 3e 69 73 45 78 63 6c 75 73  && pBt->isExclus
15063 69 76 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ive ){.    sqlit
15064 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63  e3ConnectionBloc
15065 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e  ked(p->db, pBt->
15066 70 57 72 69 74 65 72 2d 3e 64 62 29 3b 0a 20 20  pWriter->db);.  
15067 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
15068 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43  LOCKED_SHAREDCAC
15069 48 45 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 70  HE;.  }..  for(p
1506a 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b  Iter=pBt->pLock;
1506b 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49   pIter; pIter=pI
1506c 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  ter->pNext){.   
1506d 20 2f 2a 20 54 68 65 20 63 6f 6e 64 69 74 69 6f   /* The conditio
1506e 6e 20 28 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21  n (pIter->eLock!
1506f 3d 65 4c 6f 63 6b 29 20 69 6e 20 74 68 65 20 66  =eLock) in the f
15070 6f 6c 6c 6f 77 69 6e 67 20 69 66 28 2e 2e 2e 29  ollowing if(...)
15071 20 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65   .    ** stateme
15072 6e 74 20 69 73 20 61 20 73 69 6d 70 6c 69 66 69  nt is a simplifi
15073 63 61 74 69 6f 6e 20 6f 66 3a 0a 20 20 20 20 2a  cation of:.    *
15074 2a 0a 20 20 20 20 2a 2a 20 20 20 28 65 4c 6f 63  *.    **   (eLoc
15075 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 7c 7c  k==WRITE_LOCK ||
15076 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57   pIter->eLock==W
15077 52 49 54 45 5f 4c 4f 43 4b 29 0a 20 20 20 20 2a  RITE_LOCK).    *
15078 2a 0a 20 20 20 20 2a 2a 20 73 69 6e 63 65 20 77  *.    ** since w
15079 65 20 6b 6e 6f 77 20 74 68 61 74 20 69 66 20 65  e know that if e
1507a 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b  Lock==WRITE_LOCK
1507b 2c 20 74 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20  , then no other 
1507c 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 20 20 20 2a  connection.    *
1507d 2a 20 6d 61 79 20 68 6f 6c 64 20 61 20 57 52 49  * may hold a WRI
1507e 54 45 5f 4c 4f 43 4b 20 6f 6e 20 61 6e 79 20 74  TE_LOCK on any t
1507f 61 62 6c 65 20 69 6e 20 74 68 69 73 20 66 69 6c  able in this fil
15080 65 20 28 73 69 6e 63 65 20 74 68 65 72 65 20 63  e (since there c
15081 61 6e 0a 20 20 20 20 2a 2a 20 6f 6e 6c 79 20 62  an.    ** only b
15082 65 20 61 20 73 69 6e 67 6c 65 20 77 72 69 74 65  e a single write
15083 72 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  r)..    */.    a
15084 73 73 65 72 74 28 20 70 49 74 65 72 2d 3e 65 4c  ssert( pIter->eL
15085 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c  ock==READ_LOCK |
15086 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d  | pIter->eLock==
15087 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20  WRITE_LOCK );.  
15088 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d    assert( eLock=
15089 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49  =READ_LOCK || pI
1508a 74 65 72 2d 3e 70 42 74 72 65 65 3d 3d 70 20 7c  ter->pBtree==p |
1508b 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d  | pIter->eLock==
1508c 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  READ_LOCK);.    
1508d 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74 72 65  if( pIter->pBtre
1508e 65 21 3d 70 20 26 26 20 70 49 74 65 72 2d 3e 69  e!=p && pIter->i
1508f 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 20 70  Table==iTab && p
15090 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f  Iter->eLock!=eLo
15091 63 6b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ck ){.      sqli
15092 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f  te3ConnectionBlo
15093 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 49 74 65  cked(p->db, pIte
15094 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 29 3b 0a  r->pBtree->db);.
15095 20 20 20 20 20 20 69 66 28 20 65 4c 6f 63 6b 3d        if( eLock=
15096 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 7b 0a 20  =WRITE_LOCK ){. 
15097 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
15098 3d 3d 70 42 74 2d 3e 70 57 72 69 74 65 72 20 29  ==pBt->pWriter )
15099 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 69  ;.        pBt->i
1509a 73 50 65 6e 64 69 6e 67 20 3d 20 31 3b 0a 20 20  sPending = 1;.  
1509b 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
1509c 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  rn SQLITE_LOCKED
1509d 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20  _SHAREDCACHE;.  
1509e 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
1509f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65   SQLITE_OK;.}.#e
150a0 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f  ndif /* !SQLITE_
150a1 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
150a2 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  E */..#ifndef SQ
150a3 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
150a4 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 64 64  _CACHE./*.** Add
150a5 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74   a lock on the t
150a6 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70  able with root-p
150a7 61 67 65 20 69 54 61 62 6c 65 20 74 6f 20 74 68  age iTable to th
150a8 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 75  e shared-btree u
150a9 73 65 64 0a 2a 2a 20 62 79 20 42 74 72 65 65 20  sed.** by Btree 
150aa 68 61 6e 64 6c 65 20 70 2e 20 50 61 72 61 6d 65  handle p. Parame
150ab 74 65 72 20 65 4c 6f 63 6b 20 6d 75 73 74 20 62  ter eLock must b
150ac 65 20 65 69 74 68 65 72 20 52 45 41 44 5f 4c 4f  e either READ_LO
150ad 43 4b 20 6f 72 20 0a 2a 2a 20 57 52 49 54 45 5f  CK or .** WRITE_
150ae 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  LOCK..**.** This
150af 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65   function assume
150b0 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  s the following:
150b1 0a 2a 2a 0a 2a 2a 20 20 20 28 61 29 20 54 68 65  .**.**   (a) The
150b2 20 73 70 65 63 69 66 69 65 64 20 62 2d 74 72 65   specified b-tre
150b3 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e  e connection han
150b4 64 6c 65 20 69 73 20 63 6f 6e 6e 65 63 74 65 64  dle is connected
150b5 20 74 6f 20 61 20 73 68 61 72 61 62 6c 65 0a 2a   to a sharable.*
150b6 2a 20 20 20 20 20 20 20 62 2d 74 72 65 65 20 64  *       b-tree d
150b7 61 74 61 62 61 73 65 20 28 6f 6e 65 20 77 69 74  atabase (one wit
150b8 68 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 73  h the BtShared.s
150b9 68 61 72 61 62 6c 65 29 20 66 6c 61 67 20 73 65  harable) flag se
150ba 74 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 28  t, and.**.**   (
150bb 62 29 20 4e 6f 20 6f 74 68 65 72 20 62 2d 74 72  b) No other b-tr
150bc 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61  ee connection ha
150bd 6e 64 6c 65 20 68 6f 6c 64 73 20 61 20 6c 6f 63  ndle holds a loc
150be 6b 20 74 68 61 74 20 63 6f 6e 66 6c 69 63 74 73  k that conflicts
150bf 0a 2a 2a 20 20 20 20 20 20 20 77 69 74 68 20 74  .**       with t
150c0 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63  he requested loc
150c1 6b 20 28 69 2e 65 2e 20 71 75 65 72 79 53 68 61  k (i.e. querySha
150c2 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
150c3 6b 28 29 20 68 61 73 0a 2a 2a 20 20 20 20 20 20  k() has.**      
150c4 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 61   already been ca
150c5 6c 6c 65 64 20 61 6e 64 20 72 65 74 75 72 6e 65  lled and returne
150c6 64 20 53 51 4c 49 54 45 5f 4f 4b 29 2e 0a 2a 2a  d SQLITE_OK)..**
150c7 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  .** SQLITE_OK is
150c8 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65   returned if the
150c9 20 6c 6f 63 6b 20 69 73 20 61 64 64 65 64 20 73   lock is added s
150ca 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 53 51 4c  uccessfully. SQL
150cb 49 54 45 5f 4e 4f 4d 45 4d 20 0a 2a 2a 20 69 73  ITE_NOMEM .** is
150cc 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 20 6d   returned if a m
150cd 61 6c 6c 6f 63 20 61 74 74 65 6d 70 74 20 66 61  alloc attempt fa
150ce 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ils..*/.static i
150cf 6e 74 20 73 65 74 53 68 61 72 65 64 43 61 63 68  nt setSharedCach
150d0 65 54 61 62 6c 65 4c 6f 63 6b 28 42 74 72 65 65  eTableLock(Btree
150d1 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61 62 6c 65   *p, Pgno iTable
150d2 2c 20 75 38 20 65 4c 6f 63 6b 29 7b 0a 20 20 42  , u8 eLock){.  B
150d3 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
150d4 2d 3e 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20  ->pBt;.  BtLock 
150d5 2a 70 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 42 74  *pLock = 0;.  Bt
150d6 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 0a 20 20  Lock *pIter;..  
150d7 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
150d8 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70  treeHoldsMutex(p
150d9 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65  ) );.  assert( e
150da 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20  Lock==READ_LOCK 
150db 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f  || eLock==WRITE_
150dc 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  LOCK );.  assert
150dd 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b 0a 0a 20  ( p->db!=0 );.. 
150de 20 2f 2a 20 41 20 63 6f 6e 6e 65 63 74 69 6f 6e   /* A connection
150df 20 77 69 74 68 20 74 68 65 20 72 65 61 64 2d 75   with the read-u
150e0 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20  ncommitted flag 
150e1 73 65 74 20 77 69 6c 6c 20 6e 65 76 65 72 20 74  set will never t
150e2 72 79 20 74 6f 0a 20 20 2a 2a 20 6f 62 74 61 69  ry to.  ** obtai
150e3 6e 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 75 73  n a read-lock us
150e4 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ing this functio
150e5 6e 2e 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 64  n. The only read
150e6 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 64 0a 20  -lock obtained. 
150e7 20 2a 2a 20 62 79 20 61 20 63 6f 6e 6e 65 63 74   ** by a connect
150e8 69 6f 6e 20 69 6e 20 72 65 61 64 2d 75 6e 63 6f  ion in read-unco
150e9 6d 6d 69 74 74 65 64 20 6d 6f 64 65 20 69 73 20  mmitted mode is 
150ea 6f 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  on the sqlite_ma
150eb 73 74 65 72 20 0a 20 20 2a 2a 20 74 61 62 6c 65  ster .  ** table
150ec 2c 20 61 6e 64 20 74 68 61 74 20 6c 6f 63 6b 20  , and that lock 
150ed 69 73 20 6f 62 74 61 69 6e 65 64 20 69 6e 20 42  is obtained in B
150ee 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 29  treeBeginTrans()
150ef 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
150f0 30 3d 3d 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73  0==(p->db->flags
150f1 26 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f  &SQLITE_ReadUnco
150f2 6d 6d 69 74 74 65 64 29 20 7c 7c 20 65 4c 6f 63  mmitted) || eLoc
150f3 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b  k==WRITE_LOCK );
150f4 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63  ..  /* This func
150f5 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f 6e 6c 79  tion should only
150f6 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20   be called on a 
150f7 73 68 61 72 61 62 6c 65 20 62 2d 74 72 65 65 20  sharable b-tree 
150f8 61 66 74 65 72 20 69 74 20 0a 20 20 2a 2a 20 68  after it .  ** h
150f9 61 73 20 62 65 65 6e 20 64 65 74 65 72 6d 69 6e  as been determin
150fa 65 64 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72  ed that no other
150fb 20 62 2d 74 72 65 65 20 68 6f 6c 64 73 20 61 20   b-tree holds a 
150fc 63 6f 6e 66 6c 69 63 74 69 6e 67 20 6c 6f 63 6b  conflicting lock
150fd 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
150fe 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 3b 0a 20  p->sharable );. 
150ff 20 61 73 73 65 72 74 28 20 53 51 4c 49 54 45 5f   assert( SQLITE_
15100 4f 4b 3d 3d 71 75 65 72 79 53 68 61 72 65 64 43  OK==querySharedC
15101 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c  acheTableLock(p,
15102 20 69 54 61 62 6c 65 2c 20 65 4c 6f 63 6b 29 20   iTable, eLock) 
15103 29 3b 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 73  );..  /* First s
15104 65 61 72 63 68 20 74 68 65 20 6c 69 73 74 20 66  earch the list f
15105 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6c  or an existing l
15106 6f 63 6b 20 6f 6e 20 74 68 69 73 20 74 61 62 6c  ock on this tabl
15107 65 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 49 74 65  e. */.  for(pIte
15108 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49  r=pBt->pLock; pI
15109 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72  ter; pIter=pIter
1510a 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66  ->pNext){.    if
1510b 28 20 70 49 74 65 72 2d 3e 69 54 61 62 6c 65 3d  ( pIter->iTable=
1510c 3d 69 54 61 62 6c 65 20 26 26 20 70 49 74 65 72  =iTable && pIter
1510d 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20  ->pBtree==p ){. 
1510e 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 70 49 74       pLock = pIt
1510f 65 72 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  er;.      break;
15110 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
15111 20 49 66 20 74 68 65 20 61 62 6f 76 65 20 73 65   If the above se
15112 61 72 63 68 20 64 69 64 20 6e 6f 74 20 66 69 6e  arch did not fin
15113 64 20 61 20 42 74 4c 6f 63 6b 20 73 74 72 75 63  d a BtLock struc
15114 74 20 61 73 73 6f 63 69 61 74 69 6e 67 20 42 74  t associating Bt
15115 72 65 65 20 70 0a 20 20 2a 2a 20 77 69 74 68 20  ree p.  ** with 
15116 74 61 62 6c 65 20 69 54 61 62 6c 65 2c 20 61 6c  table iTable, al
15117 6c 6f 63 61 74 65 20 6f 6e 65 20 61 6e 64 20 6c  locate one and l
15118 69 6e 6b 20 69 74 20 69 6e 74 6f 20 74 68 65 20  ink it into the 
15119 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  list..  */.  if(
1511a 20 21 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70   !pLock ){.    p
1511b 4c 6f 63 6b 20 3d 20 28 42 74 4c 6f 63 6b 20 2a  Lock = (BtLock *
1511c 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65  )sqlite3MallocZe
1511d 72 6f 28 73 69 7a 65 6f 66 28 42 74 4c 6f 63 6b  ro(sizeof(BtLock
1511e 29 29 3b 0a 20 20 20 20 69 66 28 20 21 70 4c 6f  ));.    if( !pLo
1511f 63 6b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  ck ){.      retu
15120 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
15121 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 6f 63 6b  .    }.    pLock
15122 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 6c  ->iTable = iTabl
15123 65 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 42  e;.    pLock->pB
15124 74 72 65 65 20 3d 20 70 3b 0a 20 20 20 20 70 4c  tree = p;.    pL
15125 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74  ock->pNext = pBt
15126 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 74  ->pLock;.    pBt
15127 2d 3e 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b  ->pLock = pLock;
15128 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74  .  }..  /* Set t
15129 68 65 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20  he BtLock.eLock 
1512a 76 61 72 69 61 62 6c 65 20 74 6f 20 74 68 65 20  variable to the 
1512b 6d 61 78 69 6d 75 6d 20 6f 66 20 74 68 65 20 63  maximum of the c
1512c 75 72 72 65 6e 74 20 6c 6f 63 6b 0a 20 20 2a 2a  urrent lock.  **
1512d 20 61 6e 64 20 74 68 65 20 72 65 71 75 65 73 74   and the request
1512e 65 64 20 6c 6f 63 6b 2e 20 54 68 69 73 20 6d 65  ed lock. This me
1512f 61 6e 73 20 69 66 20 61 20 77 72 69 74 65 2d 6c  ans if a write-l
15130 6f 63 6b 20 77 61 73 20 61 6c 72 65 61 64 79 20  ock was already 
15131 68 65 6c 64 0a 20 20 2a 2a 20 61 6e 64 20 61 20  held.  ** and a 
15132 72 65 61 64 2d 6c 6f 63 6b 20 72 65 71 75 65 73  read-lock reques
15133 74 65 64 2c 20 77 65 20 64 6f 6e 27 74 20 69 6e  ted, we don't in
15134 63 6f 72 72 65 63 74 6c 79 20 64 6f 77 6e 67 72  correctly downgr
15135 61 64 65 20 74 68 65 20 6c 6f 63 6b 2e 0a 20 20  ade the lock..  
15136 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 57 52 49  */.  assert( WRI
15137 54 45 5f 4c 4f 43 4b 3e 52 45 41 44 5f 4c 4f 43  TE_LOCK>READ_LOC
15138 4b 20 29 3b 0a 20 20 69 66 28 20 65 4c 6f 63 6b  K );.  if( eLock
15139 3e 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 7b  >pLock->eLock ){
1513a 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63  .    pLock->eLoc
1513b 6b 20 3d 20 65 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a  k = eLock;.  }..
1513c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1513d 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  OK;.}.#endif /* 
1513e 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41  !SQLITE_OMIT_SHA
1513f 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69  RED_CACHE */..#i
15140 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
15141 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f  T_SHARED_CACHE./
15142 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c 6c  *.** Release all
15143 20 74 68 65 20 74 61 62 6c 65 20 6c 6f 63 6b 73   the table locks
15144 20 28 6c 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64   (locks obtained
15145 20 76 69 61 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a   via calls to.**
15146 20 74 68 65 20 73 65 74 53 68 61 72 65 64 43 61   the setSharedCa
15147 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 70  cheTableLock() p
15148 72 6f 63 65 64 75 72 65 29 20 68 65 6c 64 20 62  rocedure) held b
15149 79 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70  y Btree handle p
1514a 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
1514b 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68  ction assumes th
1514c 61 74 20 68 61 6e 64 6c 65 20 70 20 68 61 73 20  at handle p has 
1514d 61 6e 20 6f 70 65 6e 20 72 65 61 64 20 6f 72 20  an open read or 
1514e 77 72 69 74 65 20 0a 2a 2a 20 74 72 61 6e 73 61  write .** transa
1514f 63 74 69 6f 6e 2e 20 49 66 20 69 74 20 64 6f 65  ction. If it doe
15150 73 20 6e 6f 74 2c 20 74 68 65 6e 20 74 68 65 20  s not, then the 
15151 42 74 53 68 61 72 65 64 2e 69 73 50 65 6e 64 69  BtShared.isPendi
15152 6e 67 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d  ng variable.** m
15153 61 79 20 62 65 20 69 6e 63 6f 72 72 65 63 74 6c  ay be incorrectl
15154 79 20 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a 73 74  y cleared..*/.st
15155 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 41  atic void clearA
15156 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62  llSharedCacheTab
15157 6c 65 4c 6f 63 6b 73 28 42 74 72 65 65 20 2a 70  leLocks(Btree *p
15158 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  ){.  BtShared *p
15159 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42  Bt = p->pBt;.  B
1515a 74 4c 6f 63 6b 20 2a 2a 70 70 49 74 65 72 20 3d  tLock **ppIter =
1515b 20 26 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20   &pBt->pLock;.. 
1515c 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1515d 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28  BtreeHoldsMutex(
1515e 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  p) );.  assert( 
1515f 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 30  p->sharable || 0
15160 3d 3d 2a 70 70 49 74 65 72 20 29 3b 0a 20 20 61  ==*ppIter );.  a
15161 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e  ssert( p->inTran
15162 73 3e 30 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28  s>0 );..  while(
15163 20 2a 70 70 49 74 65 72 20 29 7b 0a 20 20 20 20   *ppIter ){.    
15164 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20  BtLock *pLock = 
15165 2a 70 70 49 74 65 72 3b 0a 20 20 20 20 61 73 73  *ppIter;.    ass
15166 65 72 74 28 20 70 42 74 2d 3e 69 73 45 78 63 6c  ert( pBt->isExcl
15167 75 73 69 76 65 3d 3d 30 20 7c 7c 20 70 42 74 2d  usive==0 || pBt-
15168 3e 70 57 72 69 74 65 72 3d 3d 70 4c 6f 63 6b 2d  >pWriter==pLock-
15169 3e 70 42 74 72 65 65 20 29 3b 0a 20 20 20 20 61  >pBtree );.    a
1516a 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 42  ssert( pLock->pB
1516b 74 72 65 65 2d 3e 69 6e 54 72 61 6e 73 3e 3d 70  tree->inTrans>=p
1516c 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 3b 0a 20  Lock->eLock );. 
1516d 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42     if( pLock->pB
1516e 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20  tree==p ){.     
1516f 20 2a 70 70 49 74 65 72 20 3d 20 70 4c 6f 63 6b   *ppIter = pLock
15170 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 61  ->pNext;.      a
15171 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 69 54  ssert( pLock->iT
15172 61 62 6c 65 21 3d 31 20 7c 7c 20 70 4c 6f 63 6b  able!=1 || pLock
15173 3d 3d 26 70 2d 3e 6c 6f 63 6b 20 29 3b 0a 20 20  ==&p->lock );.  
15174 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 69      if( pLock->i
15175 54 61 62 6c 65 21 3d 31 20 29 7b 0a 20 20 20 20  Table!=1 ){.    
15176 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
15177 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d  (pLock);.      }
15178 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
15179 20 20 70 70 49 74 65 72 20 3d 20 26 70 4c 6f 63    ppIter = &pLoc
1517a 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a  k->pNext;.    }.
1517b 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70    }..  assert( p
1517c 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 3d 3d 30  Bt->isPending==0
1517d 20 7c 7c 20 70 42 74 2d 3e 70 57 72 69 74 65 72   || pBt->pWriter
1517e 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 70   );.  if( pBt->p
1517f 57 72 69 74 65 72 3d 3d 70 20 29 7b 0a 20 20 20  Writer==p ){.   
15180 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20   pBt->pWriter = 
15181 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 73 45 78  0;.    pBt->isEx
15182 63 6c 75 73 69 76 65 20 3d 20 30 3b 0a 20 20 20  clusive = 0;.   
15183 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20   pBt->isPending 
15184 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  = 0;.  }else if(
15185 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69   pBt->nTransacti
15186 6f 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 2f 2a 20  on==2 ){.    /* 
15187 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
15188 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 63 6f 6e   called when con
15189 6e 65 63 74 69 6f 6e 20 70 20 69 73 20 63 6f 6e  nection p is con
1518a 63 6c 75 64 69 6e 67 20 69 74 73 20 0a 20 20 20  cluding its .   
1518b 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e   ** transaction.
1518c 20 49 66 20 74 68 65 72 65 20 63 75 72 72 65 6e   If there curren
1518d 74 6c 79 20 65 78 69 73 74 73 20 61 20 77 72 69  tly exists a wri
1518e 74 65 72 2c 20 61 6e 64 20 70 20 69 73 20 6e 6f  ter, and p is no
1518f 74 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 77 72  t.    ** that wr
15190 69 74 65 72 2c 20 74 68 65 6e 20 74 68 65 20 6e  iter, then the n
15191 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 68  umber of locks h
15192 65 6c 64 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f  eld by connectio
15193 6e 73 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20  ns other.    ** 
15194 74 68 61 6e 20 74 68 65 20 77 72 69 74 65 72 20  than the writer 
15195 6d 75 73 74 20 62 65 20 61 62 6f 75 74 20 74 6f  must be about to
15196 20 64 72 6f 70 20 74 6f 20 7a 65 72 6f 2e 20 49   drop to zero. I
15197 6e 20 74 68 69 73 20 63 61 73 65 0a 20 20 20 20  n this case.    
15198 2a 2a 20 73 65 74 20 74 68 65 20 69 73 50 65 6e  ** set the isPen
15199 64 69 6e 67 20 66 6c 61 67 20 74 6f 20 30 2e 0a  ding flag to 0..
1519a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66      **.    ** If
1519b 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 63 75   there is not cu
1519c 72 72 65 6e 74 6c 79 20 61 20 77 72 69 74 65 72  rrently a writer
1519d 2c 20 74 68 65 6e 20 42 74 53 68 61 72 65 64 2e  , then BtShared.
1519e 69 73 50 65 6e 64 69 6e 67 20 6d 75 73 74 0a 20  isPending must. 
1519f 20 20 20 2a 2a 20 62 65 20 7a 65 72 6f 20 61 6c     ** be zero al
151a0 72 65 61 64 79 2e 20 53 6f 20 74 68 69 73 20 6e  ready. So this n
151a1 65 78 74 20 6c 69 6e 65 20 69 73 20 68 61 72 6d  ext line is harm
151a2 6c 65 73 73 20 69 6e 20 74 68 61 74 20 63 61 73  less in that cas
151a3 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 42  e..    */.    pB
151a4 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20 30  t->isPending = 0
151a5 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
151a6 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 68 61  his function cha
151a7 6e 67 65 73 20 61 6c 6c 20 77 72 69 74 65 2d 6c  nges all write-l
151a8 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 63 6f 6e  ocks held by con
151a9 6e 65 63 74 69 6f 6e 20 70 20 74 6f 20 72 65 61  nection p to rea
151aa 64 2d 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74  d-locks..*/.stat
151ab 69 63 20 76 6f 69 64 20 64 6f 77 6e 67 72 61 64  ic void downgrad
151ac 65 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54  eAllSharedCacheT
151ad 61 62 6c 65 4c 6f 63 6b 73 28 42 74 72 65 65 20  ableLocks(Btree 
151ae 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  *p){.  BtShared 
151af 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
151b0 20 69 66 28 20 70 42 74 2d 3e 70 57 72 69 74 65   if( pBt->pWrite
151b1 72 3d 3d 70 20 29 7b 0a 20 20 20 20 42 74 4c 6f  r==p ){.    BtLo
151b2 63 6b 20 2a 70 4c 6f 63 6b 3b 0a 20 20 20 20 70  ck *pLock;.    p
151b3 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20 30 3b  Bt->pWriter = 0;
151b4 0a 20 20 20 20 70 42 74 2d 3e 69 73 45 78 63 6c  .    pBt->isExcl
151b5 75 73 69 76 65 20 3d 20 30 3b 0a 20 20 20 20 70  usive = 0;.    p
151b6 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20  Bt->isPending = 
151b7 30 3b 0a 20 20 20 20 66 6f 72 28 70 4c 6f 63 6b  0;.    for(pLock
151b8 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f  =pBt->pLock; pLo
151b9 63 6b 3b 20 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d  ck; pLock=pLock-
151ba 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61  >pNext){.      a
151bb 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 65 4c  ssert( pLock->eL
151bc 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c  ock==READ_LOCK |
151bd 7c 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d  | pLock->pBtree=
151be 3d 70 20 29 3b 0a 20 20 20 20 20 20 70 4c 6f 63  =p );.      pLoc
151bf 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 52 45 41 44 5f  k->eLock = READ_
151c0 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  LOCK;.    }.  }.
151c1 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
151c2 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
151c3 43 41 43 48 45 20 2a 2f 0a 0a 73 74 61 74 69 63  CACHE */..static
151c4 20 76 6f 69 64 20 72 65 6c 65 61 73 65 50 61 67   void releasePag
151c5 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  e(MemPage *pPage
151c6 29 3b 20 20 2f 2a 20 46 6f 72 77 61 72 64 20 72  );  /* Forward r
151c7 65 66 65 72 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a  eference */../*.
151c8 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74  ** Verify that t
151c9 68 65 20 63 75 72 73 6f 72 20 68 6f 6c 64 73 20  he cursor holds 
151ca 61 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 42  a mutex on the B
151cb 74 53 68 61 72 65 64 0a 2a 2f 0a 23 69 66 6e 64  tShared.*/.#ifnd
151cc 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69 63  ef NDEBUG.static
151cd 20 69 6e 74 20 63 75 72 73 6f 72 48 6f 6c 64 73   int cursorHolds
151ce 4d 75 74 65 78 28 42 74 43 75 72 73 6f 72 20 2a  Mutex(BtCursor *
151cf 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  p){.  return sql
151d0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
151d1 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a  p->pBt->mutex);.
151d2 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64  }.#endif...#ifnd
151d3 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
151d4 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 49 6e  NCRBLOB./*.** In
151d5 76 61 6c 69 64 61 74 65 20 74 68 65 20 6f 76 65  validate the ove
151d6 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20  rflow page-list 
151d7 63 61 63 68 65 20 66 6f 72 20 63 75 72 73 6f 72  cache for cursor
151d8 20 70 43 75 72 2c 20 69 66 20 61 6e 79 2e 0a 2a   pCur, if any..*
151d9 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e  /.static void in
151da 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77  validateOverflow
151db 43 61 63 68 65 28 42 74 43 75 72 73 6f 72 20 2a  Cache(BtCursor *
151dc 70 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28  pCur){.  assert(
151dd 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
151de 78 28 70 43 75 72 29 20 29 3b 0a 20 20 73 71 6c  x(pCur) );.  sql
151df 69 74 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e  ite3_free(pCur->
151e0 61 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43  aOverflow);.  pC
151e1 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20  ur->aOverflow = 
151e2 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 61  0;.}../*.** Inva
151e3 6c 69 64 61 74 65 20 74 68 65 20 6f 76 65 72 66  lidate the overf
151e4 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61  low page-list ca
151e5 63 68 65 20 66 6f 72 20 61 6c 6c 20 63 75 72 73  che for all curs
151e6 6f 72 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 6f 6e  ors opened.** on
151e7 20 74 68 65 20 73 68 61 72 65 64 20 62 74 72 65   the shared btre
151e8 65 20 73 74 72 75 63 74 75 72 65 20 70 42 74 2e  e structure pBt.
151e9 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
151ea 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65  invalidateAllOve
151eb 72 66 6c 6f 77 43 61 63 68 65 28 42 74 53 68 61  rflowCache(BtSha
151ec 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 42 74 43  red *pBt){.  BtC
151ed 75 72 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65  ursor *p;.  asse
151ee 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
151ef 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
151f0 78 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42  x) );.  for(p=pB
151f1 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70  t->pCursor; p; p
151f2 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  =p->pNext){.    
151f3 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c  invalidateOverfl
151f4 6f 77 43 61 63 68 65 28 70 29 3b 0a 20 20 7d 0a  owCache(p);.  }.
151f5 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
151f6 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
151f7 20 62 65 66 6f 72 65 20 6d 6f 64 69 66 79 69 6e   before modifyin
151f8 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  g the contents o
151f9 66 20 61 20 74 61 62 6c 65 0a 2a 2a 20 62 2d 74  f a table.** b-t
151fa 72 65 65 20 74 6f 20 69 6e 76 61 6c 69 64 61 74  ree to invalidat
151fb 65 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 63  e any incrblob c
151fc 75 72 73 6f 72 73 20 74 68 61 74 20 61 72 65 20  ursors that are 
151fd 6f 70 65 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 72  open on the.** r
151fe 6f 77 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65  ow or one of the
151ff 20 72 6f 77 73 20 62 65 69 6e 67 20 6d 6f 64 69   rows being modi
15200 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  fied..**.** If a
15201 72 67 75 6d 65 6e 74 20 69 73 43 6c 65 61 72 54  rgument isClearT
15202 61 62 6c 65 20 69 73 20 74 72 75 65 2c 20 74 68  able is true, th
15203 65 6e 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f  en the entire co
15204 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a  ntents of the.**
15205 20 74 61 62 6c 65 20 69 73 20 61 62 6f 75 74 20   table is about 
15206 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e 20 49  to be deleted. I
15207 6e 20 74 68 69 73 20 63 61 73 65 20 69 6e 76 61  n this case inva
15208 6c 69 64 61 74 65 20 61 6c 6c 20 69 6e 63 72 62  lidate all incrb
15209 6c 6f 62 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f  lob.** cursors o
1520a 70 65 6e 20 6f 6e 20 61 6e 79 20 72 6f 77 20 77  pen on any row w
1520b 69 74 68 69 6e 20 74 68 65 20 74 61 62 6c 65 20  ithin the table 
1520c 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 70  with root-page p
1520d 67 6e 6f 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 4f  gnoRoot..**.** O
1520e 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 72 67  therwise, if arg
1520f 75 6d 65 6e 74 20 69 73 43 6c 65 61 72 54 61 62  ument isClearTab
15210 6c 65 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65  le is false, the
15211 6e 20 74 68 65 20 72 6f 77 20 77 69 74 68 0a 2a  n the row with.*
15212 2a 20 72 6f 77 69 64 20 69 52 6f 77 20 69 73 20  * rowid iRow is 
15213 62 65 69 6e 67 20 72 65 70 6c 61 63 65 64 20 6f  being replaced o
15214 72 20 64 65 6c 65 74 65 64 2e 20 49 6e 20 74 68  r deleted. In th
15215 69 73 20 63 61 73 65 20 69 6e 76 61 6c 69 64 61  is case invalida
15216 74 65 0a 2a 2a 20 6f 6e 6c 79 20 74 68 6f 73 65  te.** only those
15217 20 69 6e 63 72 62 6c 6f 62 20 63 75 72 73 6f 72   incrblob cursor
15218 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20 73  s open on this s
15219 70 65 63 69 66 69 63 20 72 6f 77 2e 0a 2a 2f 0a  pecific row..*/.
1521a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61  static void inva
1521b 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75  lidateIncrblobCu
1521c 72 73 6f 72 73 28 0a 20 20 42 74 72 65 65 20 2a  rsors(.  Btree *
1521d 70 42 74 72 65 65 2c 20 20 20 20 20 20 20 20 20  pBtree,         
1521e 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
1521f 20 66 69 6c 65 20 74 6f 20 63 68 65 63 6b 20 2a   file to check *
15220 2f 0a 20 20 69 36 34 20 69 52 6f 77 2c 20 20 20  /.  i64 iRow,   
15221 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
15222 68 65 20 72 6f 77 69 64 20 74 68 61 74 20 6d 69  he rowid that mi
15223 67 68 74 20 62 65 20 63 68 61 6e 67 69 6e 67 20  ght be changing 
15224 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6c 65 61 72  */.  int isClear
15225 54 61 62 6c 65 20 20 20 20 20 20 20 20 2f 2a 20  Table        /* 
15226 54 72 75 65 20 69 66 20 61 6c 6c 20 72 6f 77 73  True if all rows
15227 20 61 72 65 20 62 65 69 6e 67 20 64 65 6c 65 74   are being delet
15228 65 64 20 2a 2f 0a 29 7b 0a 20 20 42 74 43 75 72  ed */.){.  BtCur
15229 73 6f 72 20 2a 70 3b 0a 20 20 42 74 53 68 61 72  sor *p;.  BtShar
1522a 65 64 20 2a 70 42 74 20 3d 20 70 42 74 72 65 65  ed *pBt = pBtree
1522b 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28  ->pBt;.  assert(
1522c 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
1522d 64 73 4d 75 74 65 78 28 70 42 74 72 65 65 29 20  dsMutex(pBtree) 
1522e 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e  );.  for(p=pBt->
1522f 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d  pCursor; p; p=p-
15230 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28  >pNext){.    if(
15231 20 70 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61   p->isIncrblobHa
15232 6e 64 6c 65 20 26 26 20 28 69 73 43 6c 65 61 72  ndle && (isClear
15233 54 61 62 6c 65 20 7c 7c 20 70 2d 3e 69 6e 66 6f  Table || p->info
15234 2e 6e 4b 65 79 3d 3d 69 52 6f 77 29 20 29 7b 0a  .nKey==iRow) ){.
15235 20 20 20 20 20 20 70 2d 3e 65 53 74 61 74 65 20        p->eState 
15236 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  = CURSOR_INVALID
15237 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23  ;.    }.  }.}..#
15238 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 69  else.  #define i
15239 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f  nvalidateOverflo
1523a 77 43 61 63 68 65 28 78 29 0a 20 20 23 64 65 66  wCache(x).  #def
1523b 69 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 41 6c  ine invalidateAl
1523c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 78  lOverflowCache(x
1523d 29 0a 20 20 23 64 65 66 69 6e 65 20 69 6e 76 61  ).  #define inva
1523e 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75  lidateIncrblobCu
1523f 72 73 6f 72 73 28 78 2c 79 2c 7a 29 0a 23 65 6e  rsors(x,y,z).#en
15240 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 62  dif../*.** Set b
15241 69 74 20 70 67 6e 6f 20 6f 66 20 74 68 65 20 42  it pgno of the B
15242 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74  tShared.pHasCont
15243 65 6e 74 20 62 69 74 76 65 63 2e 20 54 68 69 73  ent bitvec. This
15244 20 69 73 20 63 61 6c 6c 65 64 20 0a 2a 2a 20 77   is called .** w
15245 68 65 6e 20 61 20 70 61 67 65 20 74 68 61 74 20  hen a page that 
15246 70 72 65 76 69 6f 75 73 6c 79 20 63 6f 6e 74 61  previously conta
15247 69 6e 65 64 20 64 61 74 61 20 62 65 63 6f 6d 65  ined data become
15248 73 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65  s a free-list le
15249 61 66 20 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2a 0a  af .** page..**.
1524a 2a 2a 20 54 68 65 20 42 74 53 68 61 72 65 64 2e  ** The BtShared.
1524b 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76  pHasContent bitv
1524c 65 63 20 65 78 69 73 74 73 20 74 6f 20 77 6f 72  ec exists to wor
1524d 6b 20 61 72 6f 75 6e 64 20 61 6e 20 6f 62 73 63  k around an obsc
1524e 75 72 65 0a 2a 2a 20 62 75 67 20 63 61 75 73 65  ure.** bug cause
1524f 64 20 62 79 20 74 68 65 20 69 6e 74 65 72 61 63  d by the interac
15250 74 69 6f 6e 20 6f 66 20 74 77 6f 20 75 73 65 66  tion of two usef
15251 75 6c 20 49 4f 20 6f 70 74 69 6d 69 7a 61 74 69  ul IO optimizati
15252 6f 6e 73 20 73 75 72 72 6f 75 6e 64 69 6e 67 0a  ons surrounding.
15253 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61  ** free-list lea
15254 66 20 70 61 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 20  f pages:.**.**  
15255 20 31 29 20 57 68 65 6e 20 61 6c 6c 20 64 61 74   1) When all dat
15256 61 20 69 73 20 64 65 6c 65 74 65 64 20 66 72 6f  a is deleted fro
15257 6d 20 61 20 70 61 67 65 20 61 6e 64 20 74 68 65  m a page and the
15258 20 70 61 67 65 20 62 65 63 6f 6d 65 73 0a 2a 2a   page becomes.**
15259 20 20 20 20 20 20 61 20 66 72 65 65 2d 6c 69 73        a free-lis
1525a 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74 68 65  t leaf page, the
1525b 20 70 61 67 65 20 69 73 20 6e 6f 74 20 77 72 69   page is not wri
1525c 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61  tten to the data
1525d 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 28 61 73  base.**      (as
1525e 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20   free-list leaf 
1525f 70 61 67 65 73 20 63 6f 6e 74 61 69 6e 20 6e 6f  pages contain no
15260 20 6d 65 61 6e 69 6e 67 66 75 6c 20 64 61 74 61   meaningful data
15261 29 2e 20 53 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20  ). Sometimes.** 
15262 20 20 20 20 20 73 75 63 68 20 61 20 70 61 67 65       such a page
15263 20 69 73 20 6e 6f 74 20 65 76 65 6e 20 6a 6f 75   is not even jou
15264 72 6e 61 6c 6c 65 64 20 28 61 73 20 69 74 20 77  rnalled (as it w
15265 69 6c 6c 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66  ill not be modif
15266 69 65 64 2c 0a 2a 2a 20 20 20 20 20 20 77 68 79  ied,.**      why
15267 20 62 6f 74 68 65 72 20 6a 6f 75 72 6e 61 6c 6c   bother journall
15268 69 6e 67 20 69 74 3f 29 2e 0a 2a 2a 0a 2a 2a 20  ing it?)..**.** 
15269 20 20 32 29 20 57 68 65 6e 20 61 20 66 72 65 65    2) When a free
1526a 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20  -list leaf page 
1526b 69 73 20 72 65 75 73 65 64 2c 20 69 74 73 20 63  is reused, its c
1526c 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 72 65  ontent is not re
1526d 61 64 0a 2a 2a 20 20 20 20 20 20 66 72 6f 6d 20  ad.**      from 
1526e 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20  the database or 
1526f 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a  written to the j
15270 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 77 68 79  ournal file (why
15271 20 73 68 6f 75 6c 64 20 69 74 0a 2a 2a 20 20 20   should it.**   
15272 20 20 20 62 65 2c 20 69 66 20 69 74 20 69 73 20     be, if it is 
15273 6e 6f 74 20 61 74 20 61 6c 6c 20 6d 65 61 6e 69  not at all meani
15274 6e 67 66 75 6c 3f 29 2e 0a 2a 2a 0a 2a 2a 20 42  ngful?)..**.** B
15275 79 20 74 68 65 6d 73 65 6c 76 65 73 2c 20 74 68  y themselves, th
15276 65 73 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ese optimization
15277 73 20 77 6f 72 6b 20 66 69 6e 65 20 61 6e 64 20  s work fine and 
15278 70 72 6f 76 69 64 65 20 61 20 68 61 6e 64 79 0a  provide a handy.
15279 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 62  ** performance b
1527a 6f 6f 73 74 20 74 6f 20 62 75 6c 6b 20 64 65 6c  oost to bulk del
1527b 65 74 65 20 6f 72 20 69 6e 73 65 72 74 20 6f 70  ete or insert op
1527c 65 72 61 74 69 6f 6e 73 2e 20 48 6f 77 65 76 65  erations. Howeve
1527d 72 2c 20 69 66 0a 2a 2a 20 61 20 70 61 67 65 20  r, if.** a page 
1527e 69 73 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20  is moved to the 
1527f 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 74 68  free-list and th
15280 65 6e 20 72 65 75 73 65 64 20 77 69 74 68 69 6e  en reused within
15281 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 72 61   the same.** tra
15282 6e 73 61 63 74 69 6f 6e 2c 20 61 20 70 72 6f 62  nsaction, a prob
15283 6c 65 6d 20 63 6f 6d 65 73 20 75 70 2e 20 49 66  lem comes up. If
15284 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
15285 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77 68 65 6e   journalled when
15286 0a 2a 2a 20 69 74 20 69 73 20 6d 6f 76 65 64 20  .** it is moved 
15287 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  to the free-list
15288 20 61 6e 64 20 69 74 20 69 73 20 61 6c 73 6f 20   and it is also 
15289 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77  not journalled w
1528a 68 65 6e 20 69 74 0a 2a 2a 20 69 73 20 65 78 74  hen it.** is ext
1528b 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20  racted from the 
1528c 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 72 65  free-list and re
1528d 75 73 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6f  used, then the o
1528e 72 69 67 69 6e 61 6c 20 64 61 74 61 0a 2a 2a 20  riginal data.** 
1528f 6d 61 79 20 62 65 20 6c 6f 73 74 2e 20 49 6e 20  may be lost. In 
15290 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 20 72  the event of a r
15291 6f 6c 6c 62 61 63 6b 2c 20 69 74 20 6d 61 79 20  ollback, it may 
15292 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 6c 65 0a  not be possible.
15293 2a 2a 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68  ** to restore th
15294 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74  e database to it
15295 73 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 66 69  s original confi
15296 67 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  guration..**.** 
15297 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20  The solution is 
15298 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 61  the BtShared.pHa
15299 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 2e  sContent bitvec.
1529a 20 57 68 65 6e 65 76 65 72 20 61 20 70 61 67 65   Whenever a page
1529b 20 69 73 20 0a 2a 2a 20 6d 6f 76 65 64 20 74 6f   is .** moved to
1529c 20 62 65 63 6f 6d 65 20 61 20 66 72 65 65 2d 6c   become a free-l
1529d 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74  ist leaf page, t
1529e 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
1529f 20 62 69 74 20 69 73 0a 2a 2a 20 73 65 74 20 69   bit is.** set i
152a0 6e 20 74 68 65 20 62 69 74 76 65 63 2e 20 57 68  n the bitvec. Wh
152a1 65 6e 65 76 65 72 20 61 20 6c 65 61 66 20 70 61  enever a leaf pa
152a2 67 65 20 69 73 20 65 78 74 72 61 63 74 65 64 20  ge is extracted 
152a3 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69  from the free-li
152a4 73 74 2c 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74  st,.** optimizat
152a5 69 6f 6e 20 32 20 61 62 6f 76 65 20 69 73 20 6f  ion 2 above is o
152a6 6d 6d 69 74 74 65 64 20 69 66 20 74 68 65 20 63  mmitted if the c
152a7 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74  orresponding bit
152a8 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 73   is already.** s
152a9 65 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e 70  et in BtShared.p
152aa 48 61 73 43 6f 6e 74 65 6e 74 2e 20 54 68 65 20  HasContent. The 
152ab 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
152ac 62 69 74 76 65 63 20 61 72 65 20 63 6c 65 61 72  bitvec are clear
152ad 65 64 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64  ed.** at the end
152ae 20 6f 66 20 65 76 65 72 79 20 74 72 61 6e 73 61   of every transa
152af 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ction..*/.static
152b0 20 69 6e 74 20 62 74 72 65 65 53 65 74 48 61 73   int btreeSetHas
152b1 43 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64  Content(BtShared
152b2 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f   *pBt, Pgno pgno
152b3 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
152b4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 21  LITE_OK;.  if( !
152b5 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74  pBt->pHasContent
152b6 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 50 61 67   ){.    int nPag
152b7 65 20 3d 20 31 30 30 3b 0a 20 20 20 20 73 71 6c  e = 100;.    sql
152b8 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75  ite3PagerPagecou
152b9 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  nt(pBt->pPager, 
152ba 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 2f 2a 20  &nPage);.    /* 
152bb 49 66 20 73 71 6c 69 74 65 33 50 61 67 65 72 50  If sqlite3PagerP
152bc 61 67 65 63 6f 75 6e 74 28 29 20 66 61 69 6c 73  agecount() fails
152bd 20 74 68 65 72 65 20 69 73 20 6e 6f 20 68 61 72   there is no har
152be 6d 20 62 65 63 61 75 73 65 20 74 68 65 0a 20 20  m because the.  
152bf 20 20 2a 2a 20 6e 50 61 67 65 20 76 61 72 69 61    ** nPage varia
152c0 62 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64  ble is unchanged
152c1 20 66 72 6f 6d 20 69 74 73 20 64 65 66 61 75 6c   from its defaul
152c2 74 20 76 61 6c 75 65 20 6f 66 20 31 30 30 20 2a  t value of 100 *
152c3 2f 0a 20 20 20 20 70 42 74 2d 3e 70 48 61 73 43  /.    pBt->pHasC
152c4 6f 6e 74 65 6e 74 20 3d 20 73 71 6c 69 74 65 33  ontent = sqlite3
152c5 42 69 74 76 65 63 43 72 65 61 74 65 28 28 75 33  BitvecCreate((u3
152c6 32 29 6e 50 61 67 65 29 3b 0a 20 20 20 20 69 66  2)nPage);.    if
152c7 28 20 21 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74  ( !pBt->pHasCont
152c8 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ent ){.      rc 
152c9 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
152ca 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
152cb 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
152cc 20 70 67 6e 6f 3c 3d 73 71 6c 69 74 65 33 42 69   pgno<=sqlite3Bi
152cd 74 76 65 63 53 69 7a 65 28 70 42 74 2d 3e 70 48  tvecSize(pBt->pH
152ce 61 73 43 6f 6e 74 65 6e 74 29 20 29 7b 0a 20 20  asContent) ){.  
152cf 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69    rc = sqlite3Bi
152d0 74 76 65 63 53 65 74 28 70 42 74 2d 3e 70 48 61  tvecSet(pBt->pHa
152d1 73 43 6f 6e 74 65 6e 74 2c 20 70 67 6e 6f 29 3b  sContent, pgno);
152d2 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
152d3 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79  ;.}../*.** Query
152d4 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48   the BtShared.pH
152d5 61 73 43 6f 6e 74 65 6e 74 20 76 65 63 74 6f 72  asContent vector
152d6 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
152d7 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
152d8 77 68 65 6e 20 61 20 66 72 65 65 2d 6c 69 73 74  when a free-list
152d9 20 6c 65 61 66 20 70 61 67 65 20 69 73 20 72 65   leaf page is re
152da 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a  moved from the.*
152db 2a 20 66 72 65 65 2d 6c 69 73 74 20 66 6f 72 20  * free-list for 
152dc 72 65 75 73 65 2e 20 49 74 20 72 65 74 75 72 6e  reuse. It return
152dd 73 20 66 61 6c 73 65 20 69 66 20 69 74 20 69 73  s false if it is
152de 20 73 61 66 65 20 74 6f 20 72 65 74 72 69 65 76   safe to retriev
152df 65 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 66 72  e the.** page fr
152e0 6f 6d 20 74 68 65 20 70 61 67 65 72 20 6c 61 79  om the pager lay
152e1 65 72 20 77 69 74 68 20 74 68 65 20 27 6e 6f 2d  er with the 'no-
152e2 63 6f 6e 74 65 6e 74 27 20 66 6c 61 67 20 73 65  content' flag se
152e3 74 2e 20 54 72 75 65 20 6f 74 68 65 72 77 69 73  t. True otherwis
152e4 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
152e5 20 62 74 72 65 65 47 65 74 48 61 73 43 6f 6e 74   btreeGetHasCont
152e6 65 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70 42  ent(BtShared *pB
152e7 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20  t, Pgno pgno){. 
152e8 20 42 69 74 76 65 63 20 2a 70 20 3d 20 70 42 74   Bitvec *p = pBt
152e9 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 3b 0a 20  ->pHasContent;. 
152ea 20 72 65 74 75 72 6e 20 28 70 20 26 26 20 28 70   return (p && (p
152eb 67 6e 6f 3e 73 71 6c 69 74 65 33 42 69 74 76 65  gno>sqlite3Bitve
152ec 63 53 69 7a 65 28 70 29 20 7c 7c 20 73 71 6c 69  cSize(p) || sqli
152ed 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 2c  te3BitvecTest(p,
152ee 20 70 67 6e 6f 29 29 29 3b 0a 7d 0a 0a 2f 2a 0a   pgno)));.}../*.
152ef 2a 2a 20 43 6c 65 61 72 20 28 64 65 73 74 72 6f  ** Clear (destro
152f0 79 29 20 74 68 65 20 42 74 53 68 61 72 65 64 2e  y) the BtShared.
152f1 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76  pHasContent bitv
152f2 65 63 2e 20 54 68 69 73 20 73 68 6f 75 6c 64 20  ec. This should 
152f3 62 65 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 61 74  be.** invoked at
152f4 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20   the conclusion 
152f5 6f 66 20 65 61 63 68 20 77 72 69 74 65 2d 74 72  of each write-tr
152f6 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74  ansaction..*/.st
152f7 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 65 43  atic void btreeC
152f8 6c 65 61 72 48 61 73 43 6f 6e 74 65 6e 74 28 42  learHasContent(B
152f9 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20  tShared *pBt){. 
152fa 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65   sqlite3BitvecDe
152fb 73 74 72 6f 79 28 70 42 74 2d 3e 70 48 61 73 43  stroy(pBt->pHasC
152fc 6f 6e 74 65 6e 74 29 3b 0a 20 20 70 42 74 2d 3e  ontent);.  pBt->
152fd 70 48 61 73 43 6f 6e 74 65 6e 74 20 3d 20 30 3b  pHasContent = 0;
152fe 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 74  .}../*.** Save t
152ff 68 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f  he current curso
15300 72 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68  r position in th
15301 65 20 76 61 72 69 61 62 6c 65 73 20 42 74 43 75  e variables BtCu
15302 72 73 6f 72 2e 6e 4b 65 79 20 0a 2a 2a 20 61 6e  rsor.nKey .** an
15303 64 20 42 74 43 75 72 73 6f 72 2e 70 4b 65 79 2e  d BtCursor.pKey.
15304 20 54 68 65 20 63 75 72 73 6f 72 27 73 20 73 74   The cursor's st
15305 61 74 65 20 69 73 20 73 65 74 20 74 6f 20 43 55  ate is set to CU
15306 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b  RSOR_REQUIRESEEK
15307 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c  ..**.** The call
15308 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74  er must ensure t
15309 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69  hat the cursor i
1530a 73 20 76 61 6c 69 64 20 28 68 61 73 20 65 53 74  s valid (has eSt
1530b 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
1530c 44 29 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 63  D).** prior to c
1530d 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74  alling this rout
1530e 69 6e 65 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63  ine.  .*/.static
1530f 20 69 6e 74 20 73 61 76 65 43 75 72 73 6f 72 50   int saveCursorP
15310 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f 72  osition(BtCursor
15311 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72   *pCur){.  int r
15312 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 43 55  c;..  assert( CU
15313 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72  RSOR_VALID==pCur
15314 2d 3e 65 53 74 61 74 65 20 29 3b 0a 20 20 61 73  ->eState );.  as
15315 73 65 72 74 28 20 30 3d 3d 70 43 75 72 2d 3e 70  sert( 0==pCur->p
15316 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28  Key );.  assert(
15317 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
15318 78 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 72 63  x(pCur) );..  rc
15319 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b   = sqlite3BtreeK
1531a 65 79 53 69 7a 65 28 70 43 75 72 2c 20 26 70 43  eySize(pCur, &pC
1531b 75 72 2d 3e 6e 4b 65 79 29 3b 0a 20 20 61 73 73  ur->nKey);.  ass
1531c 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
1531d 4f 4b 20 29 3b 20 20 2f 2a 20 4b 65 79 53 69 7a  OK );  /* KeySiz
1531e 65 28 29 20 63 61 6e 6e 6f 74 20 66 61 69 6c 20  e() cannot fail 
1531f 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73  */..  /* If this
15320 20 69 73 20 61 6e 20 69 6e 74 4b 65 79 20 74 61   is an intKey ta
15321 62 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 61 62  ble, then the ab
15322 6f 76 65 20 63 61 6c 6c 20 74 6f 20 42 74 72 65  ove call to Btre
15323 65 4b 65 79 53 69 7a 65 28 29 0a 20 20 2a 2a 20  eKeySize().  ** 
15324 73 74 6f 72 65 73 20 74 68 65 20 69 6e 74 65 67  stores the integ
15325 65 72 20 6b 65 79 20 69 6e 20 70 43 75 72 2d 3e  er key in pCur->
15326 6e 4b 65 79 2e 20 49 6e 20 74 68 69 73 20 63 61  nKey. In this ca
15327 73 65 20 74 68 69 73 20 76 61 6c 75 65 20 69 73  se this value is
15328 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 69  .  ** all that i
15329 73 20 72 65 71 75 69 72 65 64 2e 20 4f 74 68 65  s required. Othe
1532a 72 77 69 73 65 2c 20 69 66 20 70 43 75 72 20 69  rwise, if pCur i
1532b 73 20 6e 6f 74 20 6f 70 65 6e 20 6f 6e 20 61 6e  s not open on an
1532c 20 69 6e 74 4b 65 79 0a 20 20 2a 2a 20 74 61 62   intKey.  ** tab
1532d 6c 65 2c 20 74 68 65 6e 20 6d 61 6c 6c 6f 63 20  le, then malloc 
1532e 73 70 61 63 65 20 66 6f 72 20 61 6e 64 20 73 74  space for and st
1532f 6f 72 65 20 74 68 65 20 70 43 75 72 2d 3e 6e 4b  ore the pCur->nK
15330 65 79 20 62 79 74 65 73 20 6f 66 20 6b 65 79 20  ey bytes of key 
15331 0a 20 20 2a 2a 20 64 61 74 61 2e 0a 20 20 2a 2f  .  ** data..  */
15332 0a 20 20 69 66 28 20 30 3d 3d 70 43 75 72 2d 3e  .  if( 0==pCur->
15333 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65  apPage[0]->intKe
15334 79 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70  y ){.    void *p
15335 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  Key = sqlite3Mal
15336 6c 6f 63 28 20 28 69 6e 74 29 70 43 75 72 2d 3e  loc( (int)pCur->
15337 6e 4b 65 79 20 29 3b 0a 20 20 20 20 69 66 28 20  nKey );.    if( 
15338 70 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63  pKey ){.      rc
15339 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b   = sqlite3BtreeK
1533a 65 79 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74  ey(pCur, 0, (int
1533b 29 70 43 75 72 2d 3e 6e 4b 65 79 2c 20 70 4b 65  )pCur->nKey, pKe
1533c 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  y);.      if( rc
1533d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1533e 20 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4b 65         pCur->pKe
1533f 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20 20  y = pKey;.      
15340 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
15341 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4b 65 79  qlite3_free(pKey
15342 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
15343 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
15344 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
15345 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72     }.  }.  asser
15346 74 28 20 21 70 43 75 72 2d 3e 61 70 50 61 67 65  t( !pCur->apPage
15347 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 21  [0]->intKey || !
15348 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 0a 20  pCur->pKey );.. 
15349 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1534a 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  OK ){.    int i;
1534b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
1534c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b  =pCur->iPage; i+
1534d 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73  +){.      releas
1534e 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61  ePage(pCur->apPa
1534f 67 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70 43  ge[i]);.      pC
15350 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d 20  ur->apPage[i] = 
15351 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75  0;.    }.    pCu
15352 72 2d 3e 69 50 61 67 65 20 3d 20 2d 31 3b 0a 20  r->iPage = -1;. 
15353 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20     pCur->eState 
15354 3d 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  = CURSOR_REQUIRE
15355 53 45 45 4b 3b 0a 20 20 7d 0a 0a 20 20 69 6e 76  SEEK;.  }..  inv
15356 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43  alidateOverflowC
15357 61 63 68 65 28 70 43 75 72 29 3b 0a 20 20 72 65  ache(pCur);.  re
15358 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
15359 2a 20 53 61 76 65 20 74 68 65 20 70 6f 73 69 74  * Save the posit
1535a 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 63 75 72 73  ions of all curs
1535b 6f 72 73 20 65 78 63 65 70 74 20 70 45 78 63 65  ors except pExce
1535c 70 74 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 74  pt open on the t
1535d 61 62 6c 65 20 0a 2a 2a 20 77 69 74 68 20 72 6f  able .** with ro
1535e 6f 74 2d 70 61 67 65 20 69 52 6f 6f 74 2e 20 55  ot-page iRoot. U
1535f 73 75 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20  sually, this is 
15360 63 61 6c 6c 65 64 20 6a 75 73 74 20 62 65 66 6f  called just befo
15361 72 65 20 63 75 72 73 6f 72 0a 2a 2a 20 70 45 78  re cursor.** pEx
15362 63 65 70 74 20 69 73 20 75 73 65 64 20 74 6f 20  cept is used to 
15363 6d 6f 64 69 66 79 20 74 68 65 20 74 61 62 6c 65  modify the table
15364 20 28 42 74 72 65 65 44 65 6c 65 74 65 28 29 20   (BtreeDelete() 
15365 6f 72 20 42 74 72 65 65 49 6e 73 65 72 74 28 29  or BtreeInsert()
15366 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
15367 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28   saveAllCursors(
15368 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50  BtShared *pBt, P
15369 67 6e 6f 20 69 52 6f 6f 74 2c 20 42 74 43 75 72  gno iRoot, BtCur
1536a 73 6f 72 20 2a 70 45 78 63 65 70 74 29 7b 0a 20  sor *pExcept){. 
1536b 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20   BtCursor *p;.  
1536c 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1536d 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
1536e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
1536f 72 74 28 20 70 45 78 63 65 70 74 3d 3d 30 20 7c  rt( pExcept==0 |
15370 7c 20 70 45 78 63 65 70 74 2d 3e 70 42 74 3d 3d  | pExcept->pBt==
15371 70 42 74 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70  pBt );.  for(p=p
15372 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20  Bt->pCursor; p; 
15373 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  p=p->pNext){.   
15374 20 69 66 28 20 70 21 3d 70 45 78 63 65 70 74 20   if( p!=pExcept 
15375 26 26 20 28 30 3d 3d 69 52 6f 6f 74 20 7c 7c 20  && (0==iRoot || 
15376 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f  p->pgnoRoot==iRo
15377 6f 74 29 20 26 26 20 0a 20 20 20 20 20 20 20 20  ot) && .        
15378 70 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  p->eState==CURSO
15379 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20  R_VALID ){.     
1537a 20 69 6e 74 20 72 63 20 3d 20 73 61 76 65 43 75   int rc = saveCu
1537b 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 3b  rsorPosition(p);
1537c 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54  .      if( SQLIT
1537d 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20  E_OK!=rc ){.    
1537e 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1537f 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
15380 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
15381 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  _OK;.}../*.** Cl
15382 65 61 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  ear the current 
15383 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 2e  cursor position.
15384 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
15385 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
15386 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28  treeClearCursor(
15387 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
15388 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
15389 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
1538a 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ) );.  sqlite3_f
1538b 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b  ree(pCur->pKey);
1538c 0a 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20  .  pCur->pKey = 
1538d 30 3b 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74  0;.  pCur->eStat
1538e 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c  e = CURSOR_INVAL
1538f 49 44 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 20  ID;.}../*.** In 
15390 74 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20  this version of 
15391 42 74 72 65 65 4d 6f 76 65 74 6f 2c 20 70 4b 65  BtreeMoveto, pKe
15392 79 20 69 73 20 61 20 70 61 63 6b 65 64 20 69 6e  y is a packed in
15393 64 65 78 20 72 65 63 6f 72 64 0a 2a 2a 20 73 75  dex record.** su
15394 63 68 20 61 73 20 69 73 20 67 65 6e 65 72 61 74  ch as is generat
15395 65 64 20 62 79 20 74 68 65 20 4f 50 5f 4d 61 6b  ed by the OP_Mak
15396 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e 20  eRecord opcode. 
15397 20 55 6e 70 61 63 6b 20 74 68 65 0a 2a 2a 20 72   Unpack the.** r
15398 65 63 6f 72 64 20 61 6e 64 20 74 68 65 6e 20 63  ecord and then c
15399 61 6c 6c 20 42 74 72 65 65 4d 6f 76 65 74 6f 55  all BtreeMovetoU
1539a 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 64 6f 20  npacked() to do 
1539b 74 68 65 20 77 6f 72 6b 2e 0a 2a 2f 0a 73 74 61  the work..*/.sta
1539c 74 69 63 20 69 6e 74 20 62 74 72 65 65 4d 6f 76  tic int btreeMov
1539d 65 74 6f 28 0a 20 20 42 74 43 75 72 73 6f 72 20  eto(.  BtCursor 
1539e 2a 70 43 75 72 2c 20 20 20 20 20 2f 2a 20 43 75  *pCur,     /* Cu
1539f 72 73 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65  rsor open on the
153a0 20 62 74 72 65 65 20 74 6f 20 62 65 20 73 65 61   btree to be sea
153a1 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  rched */.  const
153a2 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f   void *pKey,   /
153a3 2a 20 50 61 63 6b 65 64 20 6b 65 79 20 69 66 20  * Packed key if 
153a4 74 68 65 20 62 74 72 65 65 20 69 73 20 61 6e 20  the btree is an 
153a5 69 6e 64 65 78 20 2a 2f 0a 20 20 69 36 34 20 6e  index */.  i64 n
153a6 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f  Key,           /
153a7 2a 20 49 6e 74 65 67 65 72 20 6b 65 79 20 66 6f  * Integer key fo
153a8 72 20 74 61 62 6c 65 73 2e 20 20 53 69 7a 65 20  r tables.  Size 
153a9 6f 66 20 70 4b 65 79 20 66 6f 72 20 69 6e 64 69  of pKey for indi
153aa 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61  ces */.  int bia
153ab 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  s,           /* 
153ac 42 69 61 73 20 73 65 61 72 63 68 20 74 6f 20 74  Bias search to t
153ad 68 65 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20  he high end */. 
153ae 20 69 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20   int *pRes      
153af 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 73 65       /* Write se
153b0 61 72 63 68 20 72 65 73 75 6c 74 73 20 68 65 72  arch results her
153b1 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
153b2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
153b3 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 63       /* Status c
153b4 6f 64 65 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65  ode */.  Unpacke
153b5 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79  dRecord *pIdxKey
153b6 3b 20 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20  ;   /* Unpacked 
153b7 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 63  index key */.  c
153b8 68 61 72 20 61 53 70 61 63 65 5b 31 35 30 5d 3b  har aSpace[150];
153b9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d            /* Tem
153ba 70 20 73 70 61 63 65 20 66 6f 72 20 70 49 64 78  p space for pIdx
153bb 4b 65 79 20 2d 20 74 6f 20 61 76 6f 69 64 20 61  Key - to avoid a
153bc 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 0a 20 20 69 66   malloc */..  if
153bd 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 73  ( pKey ){.    as
153be 73 65 72 74 28 20 6e 4b 65 79 3d 3d 28 69 36 34  sert( nKey==(i64
153bf 29 28 69 6e 74 29 6e 4b 65 79 20 29 3b 0a 20 20  )(int)nKey );.  
153c0 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c 69    pIdxKey = sqli
153c1 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70  te3VdbeRecordUnp
153c2 61 63 6b 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e  ack(pCur->pKeyIn
153c3 66 6f 2c 20 28 69 6e 74 29 6e 4b 65 79 2c 20 70  fo, (int)nKey, p
153c4 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Key,.           
153c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
153c6 20 20 20 20 20 20 20 20 20 20 20 61 53 70 61 63             aSpac
153c7 65 2c 20 73 69 7a 65 6f 66 28 61 53 70 61 63 65  e, sizeof(aSpace
153c8 29 29 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78  ));.    if( pIdx
153c9 4b 65 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  Key==0 ) return 
153ca 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
153cb 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 64 78 4b  }else{.    pIdxK
153cc 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 63  ey = 0;.  }.  rc
153cd 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d   = sqlite3BtreeM
153ce 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43  ovetoUnpacked(pC
153cf 75 72 2c 20 70 49 64 78 4b 65 79 2c 20 6e 4b 65  ur, pIdxKey, nKe
153d0 79 2c 20 62 69 61 73 2c 20 70 52 65 73 29 3b 0a  y, bias, pRes);.
153d1 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20    if( pKey ){.  
153d2 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c    sqlite3VdbeDel
153d3 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  eteUnpackedRecor
153d4 64 28 70 49 64 78 4b 65 79 29 3b 0a 20 20 7d 0a  d(pIdxKey);.  }.
153d5 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
153d6 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 20 74 68  /*.** Restore th
153d7 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20  e cursor to the 
153d8 70 6f 73 69 74 69 6f 6e 20 69 74 20 77 61 73 20  position it was 
153d9 69 6e 20 28 6f 72 20 61 73 20 63 6c 6f 73 65 20  in (or as close 
153da 74 6f 20 61 73 20 70 6f 73 73 69 62 6c 65 29 0a  to as possible).
153db 2a 2a 20 77 68 65 6e 20 73 61 76 65 43 75 72 73  ** when saveCurs
153dc 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 77 61 73  orPosition() was
153dd 20 63 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 74 68   called. Note th
153de 61 74 20 74 68 69 73 20 63 61 6c 6c 20 64 65 6c  at this call del
153df 65 74 65 73 20 74 68 65 20 0a 2a 2a 20 73 61 76  etes the .** sav
153e0 65 64 20 70 6f 73 69 74 69 6f 6e 20 69 6e 66 6f  ed position info
153e1 20 73 74 6f 72 65 64 20 62 79 20 73 61 76 65 43   stored by saveC
153e2 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2c  ursorPosition(),
153e3 20 73 6f 20 74 68 65 72 65 20 63 61 6e 20 62 65   so there can be
153e4 0a 2a 2a 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20  .** at most one 
153e5 65 66 66 65 63 74 69 76 65 20 72 65 73 74 6f 72  effective restor
153e6 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  eCursorPosition(
153e7 29 20 63 61 6c 6c 20 61 66 74 65 72 20 65 61 63  ) call after eac
153e8 68 20 0a 2a 2a 20 73 61 76 65 43 75 72 73 6f 72  h .** saveCursor
153e9 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a 73  Position()..*/.s
153ea 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 52  tatic int btreeR
153eb 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69  estoreCursorPosi
153ec 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70  tion(BtCursor *p
153ed 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Cur){.  int rc;.
153ee 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
153ef 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
153f0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
153f1 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53  ur->eState>=CURS
153f2 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29  OR_REQUIRESEEK )
153f3 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53  ;.  if( pCur->eS
153f4 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55  tate==CURSOR_FAU
153f5 4c 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  LT ){.    return
153f6 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b   pCur->skipNext;
153f7 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 65 53 74  .  }.  pCur->eSt
153f8 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56  ate = CURSOR_INV
153f9 41 4c 49 44 3b 0a 20 20 72 63 20 3d 20 62 74 72  ALID;.  rc = btr
153fa 65 65 4d 6f 76 65 74 6f 28 70 43 75 72 2c 20 70  eeMoveto(pCur, p
153fb 43 75 72 2d 3e 70 4b 65 79 2c 20 70 43 75 72 2d  Cur->pKey, pCur-
153fc 3e 6e 4b 65 79 2c 20 30 2c 20 26 70 43 75 72 2d  >nKey, 0, &pCur-
153fd 3e 73 6b 69 70 4e 65 78 74 29 3b 0a 20 20 69 66  >skipNext);.  if
153fe 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
153ff 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
15400 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b  ree(pCur->pKey);
15401 0a 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20  .    pCur->pKey 
15402 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28  = 0;.    assert(
15403 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
15404 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70  URSOR_VALID || p
15405 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
15406 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20  SOR_INVALID );. 
15407 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
15408 7d 0a 0a 23 64 65 66 69 6e 65 20 72 65 73 74 6f  }..#define resto
15409 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  reCursorPosition
1540a 28 70 29 20 5c 0a 20 20 28 70 2d 3e 65 53 74 61  (p) \.  (p->eSta
1540b 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49  te>=CURSOR_REQUI
1540c 52 45 53 45 45 4b 20 3f 20 5c 0a 20 20 20 20 20  RESEEK ? \.     
1540d 20 20 20 20 62 74 72 65 65 52 65 73 74 6f 72 65      btreeRestore
1540e 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70  CursorPosition(p
1540f 29 20 3a 20 5c 0a 20 20 20 20 20 20 20 20 20 53  ) : \.         S
15410 51 4c 49 54 45 5f 4f 4b 29 0a 0a 2f 2a 0a 2a 2a  QLITE_OK)../*.**
15411 20 44 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68   Determine wheth
15412 65 72 20 6f 72 20 6e 6f 74 20 61 20 63 75 72 73  er or not a curs
15413 6f 72 20 68 61 73 20 6d 6f 76 65 64 20 66 72 6f  or has moved fro
15414 6d 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 69  m the position i
15415 74 0a 2a 2a 20 77 61 73 20 6c 61 73 74 20 70 6c  t.** was last pl
15416 61 63 65 64 20 61 74 2e 20 20 43 75 72 73 6f 72  aced at.  Cursor
15417 73 20 63 61 6e 20 6d 6f 76 65 20 77 68 65 6e 20  s can move when 
15418 74 68 65 20 72 6f 77 20 74 68 65 79 20 61 72 65  the row they are
15419 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 61 74 20   pointing.** at 
1541a 69 73 20 64 65 6c 65 74 65 64 20 6f 75 74 20 66  is deleted out f
1541b 72 6f 6d 20 75 6e 64 65 72 20 74 68 65 6d 2e 0a  rom under them..
1541c 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
1541d 6e 65 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72  ne returns an er
1541e 72 6f 72 20 63 6f 64 65 20 69 66 20 73 6f 6d 65  ror code if some
1541f 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67  thing goes wrong
15420 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 74 65 67 65  .  The.** intege
15421 72 20 2a 70 48 61 73 4d 6f 76 65 64 20 69 73 20  r *pHasMoved is 
15422 73 65 74 20 74 6f 20 6f 6e 65 20 69 66 20 74 68  set to one if th
15423 65 20 63 75 72 73 6f 72 20 68 61 73 20 6d 6f 76  e cursor has mov
15424 65 64 20 61 6e 64 20 30 20 69 66 20 6e 6f 74 2e  ed and 0 if not.
15425 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
15426 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
15427 72 65 65 43 75 72 73 6f 72 48 61 73 4d 6f 76 65  reeCursorHasMove
15428 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  d(BtCursor *pCur
15429 2c 20 69 6e 74 20 2a 70 48 61 73 4d 6f 76 65 64  , int *pHasMoved
1542a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  ){.  int rc;..  
1542b 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73  rc = restoreCurs
1542c 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29  orPosition(pCur)
1542d 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  ;.  if( rc ){.  
1542e 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31    *pHasMoved = 1
1542f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  ;.    return rc;
15430 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d  .  }.  if( pCur-
15431 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f  >eState!=CURSOR_
15432 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e 73  VALID || pCur->s
15433 6b 69 70 4e 65 78 74 21 3d 30 20 29 7b 0a 20 20  kipNext!=0 ){.  
15434 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31    *pHasMoved = 1
15435 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a  ;.  }else{.    *
15436 70 48 61 73 4d 6f 76 65 64 20 3d 20 30 3b 0a 20  pHasMoved = 0;. 
15437 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
15438 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65  TE_OK;.}..#ifnde
15439 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
1543a 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 47  TOVACUUM./*.** G
1543b 69 76 65 6e 20 61 20 70 61 67 65 20 6e 75 6d 62  iven a page numb
1543c 65 72 20 6f 66 20 61 20 72 65 67 75 6c 61 72 20  er of a regular 
1543d 64 61 74 61 62 61 73 65 20 70 61 67 65 2c 20 72  database page, r
1543e 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 0a 2a  eturn the page.*
1543f 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  * number for the
15440 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67   pointer-map pag
15441 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  e that contains 
15442 74 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68  the entry for th
15443 65 0a 2a 2a 20 69 6e 70 75 74 20 70 61 67 65 20  e.** input page 
15444 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  number..*/.stati
15445 63 20 50 67 6e 6f 20 70 74 72 6d 61 70 50 61 67  c Pgno ptrmapPag
15446 65 6e 6f 28 42 74 53 68 61 72 65 64 20 2a 70 42  eno(BtShared *pB
15447 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20  t, Pgno pgno){. 
15448 20 69 6e 74 20 6e 50 61 67 65 73 50 65 72 4d 61   int nPagesPerMa
15449 70 50 61 67 65 3b 0a 20 20 50 67 6e 6f 20 69 50  pPage;.  Pgno iP
1544a 74 72 4d 61 70 2c 20 72 65 74 3b 0a 20 20 61 73  trMap, ret;.  as
1544b 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1544c 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
1544d 74 65 78 29 20 29 3b 0a 20 20 6e 50 61 67 65 73  tex) );.  nPages
1544e 50 65 72 4d 61 70 50 61 67 65 20 3d 20 28 70 42  PerMapPage = (pB
1544f 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35 29  t->usableSize/5)
15450 2b 31 3b 0a 20 20 69 50 74 72 4d 61 70 20 3d 20  +1;.  iPtrMap = 
15451 28 70 67 6e 6f 2d 32 29 2f 6e 50 61 67 65 73 50  (pgno-2)/nPagesP
15452 65 72 4d 61 70 50 61 67 65 3b 0a 20 20 72 65 74  erMapPage;.  ret
15453 20 3d 20 28 69 50 74 72 4d 61 70 2a 6e 50 61 67   = (iPtrMap*nPag
15454 65 73 50 65 72 4d 61 70 50 61 67 65 29 20 2b 20  esPerMapPage) + 
15455 32 3b 20 0a 20 20 69 66 28 20 72 65 74 3d 3d 50  2; .  if( ret==P
15456 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
15457 28 70 42 74 29 20 29 7b 0a 20 20 20 20 72 65 74  (pBt) ){.    ret
15458 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ++;.  }.  return
15459 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57   ret;.}../*.** W
1545a 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e  rite an entry in
1545b 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d  to the pointer m
1545c 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ap..**.** This r
1545d 6f 75 74 69 6e 65 20 75 70 64 61 74 65 73 20 74  outine updates t
1545e 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65  he pointer map e
1545f 6e 74 72 79 20 66 6f 72 20 70 61 67 65 20 6e 75  ntry for page nu
15460 6d 62 65 72 20 27 6b 65 79 27 0a 2a 2a 20 73 6f  mber 'key'.** so
15461 20 74 68 61 74 20 69 74 20 6d 61 70 73 20 74 6f   that it maps to
15462 20 74 79 70 65 20 27 65 54 79 70 65 27 20 61 6e   type 'eType' an
15463 64 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75  d parent page nu
15464 6d 62 65 72 20 27 70 67 6e 6f 27 2e 0a 2a 2a 0a  mber 'pgno'..**.
15465 2a 2a 20 49 66 20 2a 70 52 43 20 69 73 20 69 6e  ** If *pRC is in
15466 69 74 69 61 6c 6c 79 20 6e 6f 6e 2d 7a 65 72 6f  itially non-zero
15467 20 28 6e 6f 6e 2d 53 51 4c 49 54 45 5f 4f 4b 29   (non-SQLITE_OK)
15468 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
15469 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70  ne is.** a no-op
1546a 2e 20 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  .  If an error o
1546b 63 63 75 72 73 2c 20 74 68 65 20 61 70 70 72 6f  ccurs, the appro
1546c 70 72 69 61 74 65 20 65 72 72 6f 72 20 63 6f 64  priate error cod
1546d 65 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20  e is written.** 
1546e 69 6e 74 6f 20 2a 70 52 43 2e 0a 2a 2f 0a 73 74  into *pRC..*/.st
1546f 61 74 69 63 20 76 6f 69 64 20 70 74 72 6d 61 70  atic void ptrmap
15470 50 75 74 28 42 74 53 68 61 72 65 64 20 2a 70 42  Put(BtShared *pB
15471 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20  t, Pgno key, u8 
15472 65 54 79 70 65 2c 20 50 67 6e 6f 20 70 61 72 65  eType, Pgno pare
15473 6e 74 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20  nt, int *pRC){. 
15474 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65   DbPage *pDbPage
15475 3b 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65  ;  /* The pointe
15476 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20  r map page */.  
15477 75 38 20 2a 70 50 74 72 6d 61 70 3b 20 20 20 20  u8 *pPtrmap;    
15478 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72    /* The pointer
15479 20 6d 61 70 20 64 61 74 61 20 2a 2f 0a 20 20 50   map data */.  P
1547a 67 6e 6f 20 69 50 74 72 6d 61 70 3b 20 20 20 20  gno iPtrmap;    
1547b 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20   /* The pointer 
1547c 6d 61 70 20 70 61 67 65 20 6e 75 6d 62 65 72 20  map page number 
1547d 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 3b  */.  int offset;
1547e 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
1547f 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 20   in pointer map 
15480 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63  page */.  int rc
15481 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52  ;           /* R
15482 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20  eturn code from 
15483 73 75 62 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a  subfunctions */.
15484 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 72 65  .  if( *pRC ) re
15485 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28  turn;..  assert(
15486 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
15487 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
15488 29 3b 0a 20 20 2f 2a 20 54 68 65 20 6d 61 73 74  );.  /* The mast
15489 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20  er-journal page 
1548a 6e 75 6d 62 65 72 20 6d 75 73 74 20 6e 65 76 65  number must neve
1548b 72 20 62 65 20 75 73 65 64 20 61 73 20 61 20 70  r be used as a p
1548c 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20  ointer map page 
1548d 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d  */.  assert( 0==
1548e 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42  PTRMAP_ISPAGE(pB
1548f 74 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  t, PENDING_BYTE_
15490 50 41 47 45 28 70 42 74 29 29 20 29 3b 0a 0a 20  PAGE(pBt)) );.. 
15491 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75   assert( pBt->au
15492 74 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 69 66  toVacuum );.  if
15493 28 20 6b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20  ( key==0 ){.    
15494 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 4f  *pRC = SQLITE_CO
15495 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
15496 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 50  return;.  }.  iP
15497 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f 50  trmap = PTRMAP_P
15498 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b  AGENO(pBt, key);
15499 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50  .  rc = sqlite3P
1549a 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61  agerGet(pBt->pPa
1549b 67 65 72 2c 20 69 50 74 72 6d 61 70 2c 20 26 70  ger, iPtrmap, &p
1549c 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72  DbPage);.  if( r
1549d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1549e 20 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20      *pRC = rc;. 
1549f 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
154a0 20 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50   offset = PTRMAP
154a1 5f 50 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d  _PTROFFSET(iPtrm
154a2 61 70 2c 20 6b 65 79 29 3b 0a 20 20 69 66 28 20  ap, key);.  if( 
154a3 6f 66 66 73 65 74 3c 30 20 29 7b 0a 20 20 20 20  offset<0 ){.    
154a4 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 4f  *pRC = SQLITE_CO
154a5 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
154a6 67 6f 74 6f 20 70 74 72 6d 61 70 5f 65 78 69 74  goto ptrmap_exit
154a7 3b 0a 20 20 7d 0a 20 20 70 50 74 72 6d 61 70 20  ;.  }.  pPtrmap 
154a8 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50  = (u8 *)sqlite3P
154a9 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50  agerGetData(pDbP
154aa 61 67 65 29 3b 0a 0a 20 20 69 66 28 20 65 54 79  age);..  if( eTy
154ab 70 65 21 3d 70 50 74 72 6d 61 70 5b 6f 66 66 73  pe!=pPtrmap[offs
154ac 65 74 5d 20 7c 7c 20 67 65 74 34 62 79 74 65 28  et] || get4byte(
154ad 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b  &pPtrmap[offset+
154ae 31 5d 29 21 3d 70 61 72 65 6e 74 20 29 7b 0a 20  1])!=parent ){. 
154af 20 20 20 54 52 41 43 45 28 28 22 50 54 52 4d 41     TRACE(("PTRMA
154b0 50 5f 55 50 44 41 54 45 3a 20 25 64 2d 3e 28 25  P_UPDATE: %d->(%
154b1 64 2c 25 64 29 5c 6e 22 2c 20 6b 65 79 2c 20 65  d,%d)\n", key, e
154b2 54 79 70 65 2c 20 70 61 72 65 6e 74 29 29 3b 0a  Type, parent));.
154b3 20 20 20 20 2a 70 52 43 3d 20 72 63 20 3d 20 73      *pRC= rc = s
154b4 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
154b5 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69  (pDbPage);.    i
154b6 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
154b7 20 29 7b 0a 20 20 20 20 20 20 70 50 74 72 6d 61   ){.      pPtrma
154b8 70 5b 6f 66 66 73 65 74 5d 20 3d 20 65 54 79 70  p[offset] = eTyp
154b9 65 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74  e;.      put4byt
154ba 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65  e(&pPtrmap[offse
154bb 74 2b 31 5d 2c 20 70 61 72 65 6e 74 29 3b 0a 20  t+1], parent);. 
154bc 20 20 20 7d 0a 20 20 7d 0a 0a 70 74 72 6d 61 70     }.  }..ptrmap
154bd 5f 65 78 69 74 3a 0a 20 20 73 71 6c 69 74 65 33  _exit:.  sqlite3
154be 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61  PagerUnref(pDbPa
154bf 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ge);.}../*.** Re
154c0 61 64 20 61 6e 20 65 6e 74 72 79 20 66 72 6f 6d  ad an entry from
154c1 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   the pointer map
154c2 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
154c3 74 69 6e 65 20 72 65 74 72 69 65 76 65 73 20 74  tine retrieves t
154c4 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65  he pointer map e
154c5 6e 74 72 79 20 66 6f 72 20 70 61 67 65 20 27 6b  ntry for page 'k
154c6 65 79 27 2c 20 77 72 69 74 69 6e 67 0a 2a 2a 20  ey', writing.** 
154c7 74 68 65 20 74 79 70 65 20 61 6e 64 20 70 61 72  the type and par
154c8 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20  ent page number 
154c9 74 6f 20 2a 70 45 54 79 70 65 20 61 6e 64 20 2a  to *pEType and *
154ca 70 50 67 6e 6f 20 72 65 73 70 65 63 74 69 76 65  pPgno respective
154cb 6c 79 2e 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20  ly..** An error 
154cc 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
154cd 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f   if something go
154ce 65 73 20 77 72 6f 6e 67 2c 20 6f 74 68 65 72 77  es wrong, otherw
154cf 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a  ise SQLITE_OK..*
154d0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 72  /.static int ptr
154d1 6d 61 70 47 65 74 28 42 74 53 68 61 72 65 64 20  mapGet(BtShared 
154d2 2a 70 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20  *pBt, Pgno key, 
154d3 75 38 20 2a 70 45 54 79 70 65 2c 20 50 67 6e 6f  u8 *pEType, Pgno
154d4 20 2a 70 50 67 6e 6f 29 7b 0a 20 20 44 62 50 61   *pPgno){.  DbPa
154d5 67 65 20 2a 70 44 62 50 61 67 65 3b 20 20 20 2f  ge *pDbPage;   /
154d6 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61  * The pointer ma
154d7 70 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  p page */.  int 
154d8 69 50 74 72 6d 61 70 3b 20 20 20 20 20 20 20 2f  iPtrmap;       /
154d9 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61  * Pointer map pa
154da 67 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38  ge index */.  u8
154db 20 2a 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20   *pPtrmap;      
154dc 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20   /* Pointer map 
154dd 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 69  page data */.  i
154de 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20  nt offset;      
154df 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 65    /* Offset of e
154e0 6e 74 72 79 20 69 6e 20 70 6f 69 6e 74 65 72 20  ntry in pointer 
154e1 6d 61 70 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  map */.  int rc;
154e2 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
154e3 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
154e4 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  Bt->mutex) );.. 
154e5 20 69 50 74 72 6d 61 70 20 3d 20 50 54 52 4d 41   iPtrmap = PTRMA
154e6 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65  P_PAGENO(pBt, ke
154e7 79 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  y);.  rc = sqlit
154e8 65 33 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e  e3PagerGet(pBt->
154e9 70 50 61 67 65 72 2c 20 69 50 74 72 6d 61 70 2c  pPager, iPtrmap,
154ea 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66   &pDbPage);.  if
154eb 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 72  ( rc!=0 ){.    r
154ec 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
154ed 70 50 74 72 6d 61 70 20 3d 20 28 75 38 20 2a 29  pPtrmap = (u8 *)
154ee 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44  sqlite3PagerGetD
154ef 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 0a 20  ata(pDbPage);.. 
154f0 20 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50   offset = PTRMAP
154f1 5f 50 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d  _PTROFFSET(iPtrm
154f2 61 70 2c 20 6b 65 79 29 3b 0a 20 20 61 73 73 65  ap, key);.  asse
154f3 72 74 28 20 70 45 54 79 70 65 21 3d 30 20 29 3b  rt( pEType!=0 );
154f4 0a 20 20 2a 70 45 54 79 70 65 20 3d 20 70 50 74  .  *pEType = pPt
154f5 72 6d 61 70 5b 6f 66 66 73 65 74 5d 3b 0a 20 20  rmap[offset];.  
154f6 69 66 28 20 70 50 67 6e 6f 20 29 20 2a 70 50 67  if( pPgno ) *pPg
154f7 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  no = get4byte(&p
154f8 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d  Ptrmap[offset+1]
154f9 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 50 61 67  );..  sqlite3Pag
154fa 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 65 29  erUnref(pDbPage)
154fb 3b 0a 20 20 69 66 28 20 2a 70 45 54 79 70 65 3c  ;.  if( *pEType<
154fc 31 20 7c 7c 20 2a 70 45 54 79 70 65 3e 35 20 29  1 || *pEType>5 )
154fd 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
154fe 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 72  ORRUPT_BKPT;.  r
154ff 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
15500 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20  .}..#else /* if 
15501 64 65 66 69 6e 65 64 20 53 51 4c 49 54 45 5f 4f  defined SQLITE_O
15502 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a  MIT_AUTOVACUUM *
15503 2f 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d  /.  #define ptrm
15504 61 70 50 75 74 28 77 2c 78 2c 79 2c 7a 2c 72 63  apPut(w,x,y,z,rc
15505 29 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d  ).  #define ptrm
15506 61 70 47 65 74 28 77 2c 78 2c 79 2c 7a 29 20 53  apGet(w,x,y,z) S
15507 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69  QLITE_OK.  #defi
15508 6e 65 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c  ne ptrmapPutOvfl
15509 50 74 72 28 78 2c 20 79 2c 20 72 63 29 0a 23 65  Ptr(x, y, rc).#e
1550a 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65  ndif../*.** Give
1550b 6e 20 61 20 62 74 72 65 65 20 70 61 67 65 20 61  n a btree page a
1550c 6e 64 20 61 20 63 65 6c 6c 20 69 6e 64 65 78 20  nd a cell index 
1550d 28 30 20 6d 65 61 6e 73 20 74 68 65 20 66 69 72  (0 means the fir
1550e 73 74 20 63 65 6c 6c 20 6f 6e 0a 2a 2a 20 74 68  st cell on.** th
1550f 65 20 70 61 67 65 2c 20 31 20 6d 65 61 6e 73 20  e page, 1 means 
15510 74 68 65 20 73 65 63 6f 6e 64 20 63 65 6c 6c 2c  the second cell,
15511 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 29 20 72   and so forth) r
15512 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a  eturn a pointer.
15513 2a 2a 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63  ** to the cell c
15514 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  ontent..**.** Th
15515 69 73 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73  is routine works
15516 20 6f 6e 6c 79 20 66 6f 72 20 70 61 67 65 73 20   only for pages 
15517 74 68 61 74 20 64 6f 20 6e 6f 74 20 63 6f 6e 74  that do not cont
15518 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c  ain overflow cel
15519 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66  ls..*/.#define f
1551a 69 6e 64 43 65 6c 6c 28 50 2c 49 29 20 5c 0a 20  indCell(P,I) \. 
1551b 20 28 28 50 29 2d 3e 61 44 61 74 61 20 2b 20 28   ((P)->aData + (
1551c 28 50 29 2d 3e 6d 61 73 6b 50 61 67 65 20 26 20  (P)->maskPage & 
1551d 67 65 74 32 62 79 74 65 28 26 28 50 29 2d 3e 61  get2byte(&(P)->a
1551e 44 61 74 61 5b 28 50 29 2d 3e 63 65 6c 6c 4f 66  Data[(P)->cellOf
1551f 66 73 65 74 2b 32 2a 28 49 29 5d 29 29 29 0a 0a  fset+2*(I)])))..
15520 2f 2a 0a 2a 2a 20 54 68 69 73 20 61 20 6d 6f 72  /*.** This a mor
15521 65 20 63 6f 6d 70 6c 65 78 20 76 65 72 73 69 6f  e complex versio
15522 6e 20 6f 66 20 66 69 6e 64 43 65 6c 6c 28 29 20  n of findCell() 
15523 74 68 61 74 20 77 6f 72 6b 73 20 66 6f 72 0a 2a  that works for.*
15524 2a 20 70 61 67 65 73 20 74 68 61 74 20 64 6f 20  * pages that do 
15525 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77  contain overflow
15526 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69   cells..*/.stati
15527 63 20 75 38 20 2a 66 69 6e 64 4f 76 65 72 66 6c  c u8 *findOverfl
15528 6f 77 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a  owCell(MemPage *
15529 70 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c  pPage, int iCell
1552a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73  ){.  int i;.  as
1552b 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1552c 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
1552d 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
1552e 20 66 6f 72 28 69 3d 70 50 61 67 65 2d 3e 6e 4f   for(i=pPage->nO
1552f 76 65 72 66 6c 6f 77 2d 31 3b 20 69 3e 3d 30 3b  verflow-1; i>=0;
15530 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6b   i--){.    int k
15531 3b 0a 20 20 20 20 73 74 72 75 63 74 20 5f 4f 76  ;.    struct _Ov
15532 66 6c 43 65 6c 6c 20 2a 70 4f 76 66 6c 3b 0a 20  flCell *pOvfl;. 
15533 20 20 20 70 4f 76 66 6c 20 3d 20 26 70 50 61 67     pOvfl = &pPag
15534 65 2d 3e 61 4f 76 66 6c 5b 69 5d 3b 0a 20 20 20  e->aOvfl[i];.   
15535 20 6b 20 3d 20 70 4f 76 66 6c 2d 3e 69 64 78 3b   k = pOvfl->idx;
15536 0a 20 20 20 20 69 66 28 20 6b 3c 3d 69 43 65 6c  .    if( k<=iCel
15537 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6b  l ){.      if( k
15538 3d 3d 69 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20  ==iCell ){.     
15539 20 20 20 72 65 74 75 72 6e 20 70 4f 76 66 6c 2d     return pOvfl-
1553a 3e 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a  >pCell;.      }.
1553b 20 20 20 20 20 20 69 43 65 6c 6c 2d 2d 3b 0a 20        iCell--;. 
1553c 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
1553d 6e 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65  n findCell(pPage
1553e 2c 20 69 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a  , iCell);.}../*.
1553f 2a 2a 20 50 61 72 73 65 20 61 20 63 65 6c 6c 20  ** Parse a cell 
15540 63 6f 6e 74 65 6e 74 20 62 6c 6f 63 6b 20 61 6e  content block an
15541 64 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 43 65  d fill in the Ce
15542 6c 6c 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  llInfo structure
15543 2e 20 20 54 68 65 72 65 0a 2a 2a 20 61 72 65 20  .  There.** are 
15544 74 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  two versions of 
15545 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20  this function.  
15546 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29  btreeParseCell()
15547 20 74 61 6b 65 73 20 61 20 0a 2a 2a 20 63 65 6c   takes a .** cel
15548 6c 20 69 6e 64 65 78 20 61 73 20 74 68 65 20 73  l index as the s
15549 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 61  econd argument a
1554a 6e 64 20 62 74 72 65 65 50 61 72 73 65 43 65 6c  nd btreeParseCel
1554b 6c 50 74 72 28 29 20 0a 2a 2a 20 74 61 6b 65 73  lPtr() .** takes
1554c 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
1554d 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 63 65  e body of the ce
1554e 6c 6c 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64  ll as its second
1554f 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a   argument..**.**
15550 20 57 69 74 68 69 6e 20 74 68 69 73 20 66 69 6c   Within this fil
15551 65 2c 20 74 68 65 20 70 61 72 73 65 43 65 6c 6c  e, the parseCell
15552 28 29 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20  () macro can be 
15553 63 61 6c 6c 65 64 20 69 6e 73 74 65 61 64 20 6f  called instead o
15554 66 0a 2a 2a 20 62 74 72 65 65 50 61 72 73 65 43  f.** btreeParseC
15555 65 6c 6c 50 74 72 28 29 2e 20 55 73 69 6e 67 20  ellPtr(). Using 
15556 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 2c 20  some compilers, 
15557 74 68 69 73 20 77 69 6c 6c 20 62 65 20 66 61 73  this will be fas
15558 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ter..*/.static v
15559 6f 69 64 20 62 74 72 65 65 50 61 72 73 65 43 65  oid btreeParseCe
1555a 6c 6c 50 74 72 28 0a 20 20 4d 65 6d 50 61 67 65  llPtr(.  MemPage
1555b 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20   *pPage,        
1555c 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e   /* Page contain
1555d 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a  ing the cell */.
1555e 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20    u8 *pCell,    
1555f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
15560 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c  nter to the cell
15561 20 74 65 78 74 2e 20 2a 2f 0a 20 20 43 65 6c 6c   text. */.  Cell
15562 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20  Info *pInfo     
15563 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74      /* Fill in t
15564 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f  his structure */
15565 0a 29 7b 0a 20 20 75 31 36 20 6e 3b 20 20 20 20  .){.  u16 n;    
15566 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
15567 20 4e 75 6d 62 65 72 20 62 79 74 65 73 20 69 6e   Number bytes in
15568 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65   cell content he
15569 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e 50  ader */.  u32 nP
1556a 61 79 6c 6f 61 64 3b 20 20 20 20 20 20 20 20 20  ayload;         
1556b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
1556c 79 74 65 73 20 6f 66 20 63 65 6c 6c 20 70 61 79  ytes of cell pay
1556d 6c 6f 61 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72  load */..  asser
1556e 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1556f 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
15570 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70  ->mutex) );..  p
15571 49 6e 66 6f 2d 3e 70 43 65 6c 6c 20 3d 20 70 43  Info->pCell = pC
15572 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ell;.  assert( p
15573 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c  Page->leaf==0 ||
15574 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 31 20   pPage->leaf==1 
15575 29 3b 0a 20 20 6e 20 3d 20 70 50 61 67 65 2d 3e  );.  n = pPage->
15576 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20  childPtrSize;.  
15577 61 73 73 65 72 74 28 20 6e 3d 3d 34 2d 34 2a 70  assert( n==4-4*p
15578 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20  Page->leaf );.  
15579 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65  if( pPage->intKe
1557a 79 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61  y ){.    if( pPa
1557b 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a 20  ge->hasData ){. 
1557c 20 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72       n += getVar
1557d 69 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c  int32(&pCell[n],
1557e 20 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20   nPayload);.    
1557f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61  }else{.      nPa
15580 79 6c 6f 61 64 20 3d 20 30 3b 0a 20 20 20 20 7d  yload = 0;.    }
15581 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72  .    n += getVar
15582 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 28  int(&pCell[n], (
15583 75 36 34 2a 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65  u64*)&pInfo->nKe
15584 79 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e  y);.    pInfo->n
15585 44 61 74 61 20 3d 20 6e 50 61 79 6c 6f 61 64 3b  Data = nPayload;
15586 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49  .  }else{.    pI
15587 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a  nfo->nData = 0;.
15588 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69      n += getVari
15589 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20  nt32(&pCell[n], 
1558a 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 70  nPayload);.    p
1558b 49 6e 66 6f 2d 3e 6e 4b 65 79 20 3d 20 6e 50 61  Info->nKey = nPa
1558c 79 6c 6f 61 64 3b 0a 20 20 7d 0a 20 20 70 49 6e  yload;.  }.  pIn
1558d 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20 3d 20 6e  fo->nPayload = n
1558e 50 61 79 6c 6f 61 64 3b 0a 20 20 70 49 6e 66 6f  Payload;.  pInfo
1558f 2d 3e 6e 48 65 61 64 65 72 20 3d 20 6e 3b 0a 20  ->nHeader = n;. 
15590 20 74 65 73 74 63 61 73 65 28 20 6e 50 61 79 6c   testcase( nPayl
15591 6f 61 64 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c  oad==pPage->maxL
15592 6f 63 61 6c 20 29 3b 0a 20 20 74 65 73 74 63 61  ocal );.  testca
15593 73 65 28 20 6e 50 61 79 6c 6f 61 64 3d 3d 70 50  se( nPayload==pP
15594 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20  age->maxLocal+1 
15595 29 3b 0a 20 20 69 66 28 20 6c 69 6b 65 6c 79 28  );.  if( likely(
15596 6e 50 61 79 6c 6f 61 64 3c 3d 70 50 61 67 65 2d  nPayload<=pPage-
15597 3e 6d 61 78 4c 6f 63 61 6c 29 20 29 7b 0a 20 20  >maxLocal) ){.  
15598 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65    /* This is the
15599 20 28 65 61 73 79 29 20 63 6f 6d 6d 6f 6e 20 63   (easy) common c
1559a 61 73 65 20 77 68 65 72 65 20 74 68 65 20 65 6e  ase where the en
1559b 74 69 72 65 20 70 61 79 6c 6f 61 64 20 66 69 74  tire payload fit
1559c 73 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20  s.    ** on the 
1559d 6c 6f 63 61 6c 20 70 61 67 65 2e 20 20 4e 6f 20  local page.  No 
1559e 6f 76 65 72 66 6c 6f 77 20 69 73 20 72 65 71 75  overflow is requ
1559f 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ired..    */.   
155a0 20 69 6e 74 20 6e 53 69 7a 65 3b 20 20 20 20 20   int nSize;     
155a1 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69       /* Total si
155a2 7a 65 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 65  ze of cell conte
155a3 6e 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  nt in bytes */. 
155a4 20 20 20 6e 53 69 7a 65 20 3d 20 6e 50 61 79 6c     nSize = nPayl
155a5 6f 61 64 20 2b 20 6e 3b 0a 20 20 20 20 70 49 6e  oad + n;.    pIn
155a6 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31  fo->nLocal = (u1
155a7 36 29 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20 20  6)nPayload;.    
155a8 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77  pInfo->iOverflow
155a9 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 28 6e   = 0;.    if( (n
155aa 53 69 7a 65 20 26 20 7e 33 29 3d 3d 30 20 29 7b  Size & ~3)==0 ){
155ab 0a 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 34  .      nSize = 4
155ac 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69  ;        /* Mini
155ad 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 73  mum cell size is
155ae 20 34 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20   4 */.    }.    
155af 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 28  pInfo->nSize = (
155b0 75 31 36 29 6e 53 69 7a 65 3b 0a 20 20 7d 65 6c  u16)nSize;.  }el
155b1 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  se{.    /* If th
155b2 65 20 70 61 79 6c 6f 61 64 20 77 69 6c 6c 20 6e  e payload will n
155b3 6f 74 20 66 69 74 20 63 6f 6d 70 6c 65 74 65 6c  ot fit completel
155b4 79 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70  y on the local p
155b5 61 67 65 2c 20 77 65 20 68 61 76 65 0a 20 20 20  age, we have.   
155b6 20 2a 2a 20 74 6f 20 64 65 63 69 64 65 20 68 6f   ** to decide ho
155b7 77 20 6d 75 63 68 20 74 6f 20 73 74 6f 72 65 20  w much to store 
155b8 6c 6f 63 61 6c 6c 79 20 61 6e 64 20 68 6f 77 20  locally and how 
155b9 6d 75 63 68 20 74 6f 20 73 70 69 6c 6c 20 6f 6e  much to spill on
155ba 74 6f 0a 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c  to.    ** overfl
155bb 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65 20 73  ow pages.  The s
155bc 74 72 61 74 65 67 79 20 69 73 20 74 6f 20 6d 69  trategy is to mi
155bd 6e 69 6d 69 7a 65 20 74 68 65 20 61 6d 6f 75 6e  nimize the amoun
155be 74 20 6f 66 20 75 6e 75 73 65 64 0a 20 20 20 20  t of unused.    
155bf 2a 2a 20 73 70 61 63 65 20 6f 6e 20 6f 76 65 72  ** space on over
155c0 66 6c 6f 77 20 70 61 67 65 73 20 77 68 69 6c 65  flow pages while
155c1 20 6b 65 65 70 69 6e 67 20 74 68 65 20 61 6d 6f   keeping the amo
155c2 75 6e 74 20 6f 66 20 6c 6f 63 61 6c 20 73 74 6f  unt of local sto
155c3 72 61 67 65 0a 20 20 20 20 2a 2a 20 69 6e 20 62  rage.    ** in b
155c4 65 74 77 65 65 6e 20 6d 69 6e 4c 6f 63 61 6c 20  etween minLocal 
155c5 61 6e 64 20 6d 61 78 4c 6f 63 61 6c 2e 0a 20 20  and maxLocal..  
155c6 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 61 72 6e    **.    ** Warn
155c7 69 6e 67 3a 20 20 63 68 61 6e 67 69 6e 67 20 74  ing:  changing t
155c8 68 65 20 77 61 79 20 6f 76 65 72 66 6c 6f 77 20  he way overflow 
155c9 70 61 79 6c 6f 61 64 20 69 73 20 64 69 73 74 72  payload is distr
155ca 69 62 75 74 65 64 20 69 6e 20 61 6e 79 0a 20 20  ibuted in any.  
155cb 20 20 2a 2a 20 77 61 79 20 77 69 6c 6c 20 72 65    ** way will re
155cc 73 75 6c 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6d  sult in an incom
155cd 70 61 74 69 62 6c 65 20 66 69 6c 65 20 66 6f 72  patible file for
155ce 6d 61 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  mat..    */.    
155cf 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 2f  int minLocal;  /
155d0 2a 20 4d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74  * Minimum amount
155d1 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64   of payload held
155d2 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20   locally */.    
155d3 69 6e 74 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 2f  int maxLocal;  /
155d4 2a 20 4d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74  * Maximum amount
155d5 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64   of payload held
155d6 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20   locally */.    
155d7 69 6e 74 20 73 75 72 70 6c 75 73 3b 20 20 20 2f  int surplus;   /
155d8 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f  * Overflow paylo
155d9 61 64 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72  ad available for
155da 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 20 2a   local storage *
155db 2f 0a 0a 20 20 20 20 6d 69 6e 4c 6f 63 61 6c 20  /..    minLocal 
155dc 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61  = pPage->minLoca
155dd 6c 3b 0a 20 20 20 20 6d 61 78 4c 6f 63 61 6c 20  l;.    maxLocal 
155de 3d 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61  = pPage->maxLoca
155df 6c 3b 0a 20 20 20 20 73 75 72 70 6c 75 73 20 3d  l;.    surplus =
155e0 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e 50 61   minLocal + (nPa
155e1 79 6c 6f 61 64 20 2d 20 6d 69 6e 4c 6f 63 61 6c  yload - minLocal
155e2 29 25 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75  )%(pPage->pBt->u
155e3 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a  sableSize - 4);.
155e4 20 20 20 20 74 65 73 74 63 61 73 65 28 20 73 75      testcase( su
155e5 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63 61 6c 20  rplus==maxLocal 
155e6 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
155e7 20 73 75 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63   surplus==maxLoc
155e8 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20  al+1 );.    if( 
155e9 73 75 72 70 6c 75 73 20 3c 3d 20 6d 61 78 4c 6f  surplus <= maxLo
155ea 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 70 49 6e  cal ){.      pIn
155eb 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31  fo->nLocal = (u1
155ec 36 29 73 75 72 70 6c 75 73 3b 0a 20 20 20 20 7d  6)surplus;.    }
155ed 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 6e 66  else{.      pInf
155ee 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36  o->nLocal = (u16
155ef 29 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d  )minLocal;.    }
155f0 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65  .    pInfo->iOve
155f1 72 66 6c 6f 77 20 3d 20 28 75 31 36 29 28 70 49  rflow = (u16)(pI
155f2 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 2b 20 6e 29  nfo->nLocal + n)
155f3 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 53 69  ;.    pInfo->nSi
155f4 7a 65 20 3d 20 70 49 6e 66 6f 2d 3e 69 4f 76 65  ze = pInfo->iOve
155f5 72 66 6c 6f 77 20 2b 20 34 3b 0a 20 20 7d 0a 7d  rflow + 4;.  }.}
155f6 0a 23 64 65 66 69 6e 65 20 70 61 72 73 65 43 65  .#define parseCe
155f7 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c  ll(pPage, iCell,
155f8 20 70 49 6e 66 6f 29 20 5c 0a 20 20 62 74 72 65   pInfo) \.  btre
155f9 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 28 70  eParseCellPtr((p
155fa 50 61 67 65 29 2c 20 66 69 6e 64 43 65 6c 6c 28  Page), findCell(
155fb 28 70 50 61 67 65 29 2c 20 28 69 43 65 6c 6c 29  (pPage), (iCell)
155fc 29 2c 20 28 70 49 6e 66 6f 29 29 0a 73 74 61 74  ), (pInfo)).stat
155fd 69 63 20 76 6f 69 64 20 62 74 72 65 65 50 61 72  ic void btreePar
155fe 73 65 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67  seCell(.  MemPag
155ff 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20  e *pPage,       
15600 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69    /* Page contai
15601 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f  ning the cell */
15602 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c 20 20 20  .  int iCell,   
15603 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
15604 65 20 63 65 6c 6c 20 69 6e 64 65 78 2e 20 20 46  e cell index.  F
15605 69 72 73 74 20 63 65 6c 6c 20 69 73 20 30 20 2a  irst cell is 0 *
15606 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49  /.  CellInfo *pI
15607 6e 66 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 46  nfo         /* F
15608 69 6c 6c 20 69 6e 20 74 68 69 73 20 73 74 72 75  ill in this stru
15609 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 70 61  cture */.){.  pa
1560a 72 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69  rseCell(pPage, i
1560b 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 3b 0a 7d 0a  Cell, pInfo);.}.
1560c 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74  ./*.** Compute t
1560d 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
1560e 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 61 20  of bytes that a 
1560f 43 65 6c 6c 20 6e 65 65 64 73 20 69 6e 20 74 68  Cell needs in th
15610 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 61  e cell.** data a
15611 72 65 61 20 6f 66 20 74 68 65 20 62 74 72 65 65  rea of the btree
15612 2d 70 61 67 65 2e 20 20 54 68 65 20 72 65 74 75  -page.  The retu
15613 72 6e 20 6e 75 6d 62 65 72 20 69 6e 63 6c 75 64  rn number includ
15614 65 73 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64  es the cell.** d
15615 61 74 61 20 68 65 61 64 65 72 20 61 6e 64 20 74  ata header and t
15616 68 65 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64  he local payload
15617 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 76  , but not any ov
15618 65 72 66 6c 6f 77 20 70 61 67 65 20 6f 72 0a 2a  erflow page or.*
15619 2a 20 74 68 65 20 73 70 61 63 65 20 75 73 65 64  * the space used
1561a 20 62 79 20 74 68 65 20 63 65 6c 6c 20 70 6f 69   by the cell poi
1561b 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  nter..*/.static 
1561c 75 31 36 20 63 65 6c 6c 53 69 7a 65 50 74 72 28  u16 cellSizePtr(
1561d 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20  MemPage *pPage, 
1561e 75 38 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 75 38  u8 *pCell){.  u8
1561f 20 2a 70 49 74 65 72 20 3d 20 26 70 43 65 6c 6c   *pIter = &pCell
15620 5b 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72  [pPage->childPtr
15621 53 69 7a 65 5d 3b 0a 20 20 75 33 32 20 6e 53 69  Size];.  u32 nSi
15622 7a 65 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ze;..#ifdef SQLI
15623 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a 20 54 68  TE_DEBUG.  /* Th
15624 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
15625 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f   by this functio
15626 6e 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20  n should always 
15627 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 0a 20  be the same as. 
15628 20 2a 2a 20 74 68 65 20 28 43 65 6c 6c 49 6e 66   ** the (CellInf
15629 6f 2e 6e 53 69 7a 65 29 20 76 61 6c 75 65 20 66  o.nSize) value f
1562a 6f 75 6e 64 20 62 79 20 64 6f 69 6e 67 20 61 20  ound by doing a 
1562b 66 75 6c 6c 20 70 61 72 73 65 20 6f 66 20 74 68  full parse of th
1562c 65 0a 20 20 2a 2a 20 63 65 6c 6c 2e 20 49 66 20  e.  ** cell. If 
1562d 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20  SQLITE_DEBUG is 
1562e 64 65 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 65  defined, an asse
1562f 72 74 28 29 20 61 74 20 74 68 65 20 62 6f 74 74  rt() at the bott
15630 6f 6d 20 6f 66 0a 20 20 2a 2a 20 74 68 69 73 20  om of.  ** this 
15631 66 75 6e 63 74 69 6f 6e 20 76 65 72 69 66 69 65  function verifie
15632 73 20 74 68 61 74 20 74 68 69 73 20 69 6e 76 61  s that this inva
15633 72 69 61 6e 74 20 69 73 20 6e 6f 74 20 76 69 6f  riant is not vio
15634 6c 61 74 65 64 2e 20 2a 2f 0a 20 20 43 65 6c 6c  lated. */.  Cell
15635 49 6e 66 6f 20 64 65 62 75 67 69 6e 66 6f 3b 0a  Info debuginfo;.
15636 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c    btreeParseCell
15637 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c  Ptr(pPage, pCell
15638 2c 20 26 64 65 62 75 67 69 6e 66 6f 29 3b 0a 23  , &debuginfo);.#
15639 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 70 50 61  endif..  if( pPa
1563a 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20  ge->intKey ){.  
1563b 20 20 75 38 20 2a 70 45 6e 64 3b 0a 20 20 20 20    u8 *pEnd;.    
1563c 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61  if( pPage->hasDa
1563d 74 61 20 29 7b 0a 20 20 20 20 20 20 70 49 74 65  ta ){.      pIte
1563e 72 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32  r += getVarint32
1563f 28 70 49 74 65 72 2c 20 6e 53 69 7a 65 29 3b 0a  (pIter, nSize);.
15640 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
15641 20 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20   nSize = 0;.    
15642 7d 0a 0a 20 20 20 20 2f 2a 20 70 49 74 65 72 20  }..    /* pIter 
15643 6e 6f 77 20 70 6f 69 6e 74 73 20 61 74 20 74 68  now points at th
15644 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72  e 64-bit integer
15645 20 6b 65 79 20 76 61 6c 75 65 2c 20 61 20 76 61   key value, a va
15646 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 0a 20  riable length . 
15647 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20 54     ** integer. T
15648 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f  he following blo
15649 63 6b 20 6d 6f 76 65 73 20 70 49 74 65 72 20 74  ck moves pIter t
1564a 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 66  o point at the f
1564b 69 72 73 74 20 62 79 74 65 0a 20 20 20 20 2a 2a  irst byte.    **
1564c 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66   past the end of
1564d 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20   the key value. 
1564e 2a 2f 0a 20 20 20 20 70 45 6e 64 20 3d 20 26 70  */.    pEnd = &p
1564f 49 74 65 72 5b 39 5d 3b 0a 20 20 20 20 77 68 69  Iter[9];.    whi
15650 6c 65 28 20 28 2a 70 49 74 65 72 2b 2b 29 26 30  le( (*pIter++)&0
15651 78 38 30 20 26 26 20 70 49 74 65 72 3c 70 45 6e  x80 && pIter<pEn
15652 64 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  d );.  }else{.  
15653 20 20 70 49 74 65 72 20 2b 3d 20 67 65 74 56 61    pIter += getVa
15654 72 69 6e 74 33 32 28 70 49 74 65 72 2c 20 6e 53  rint32(pIter, nS
15655 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 74 65 73  ize);.  }..  tes
15656 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50  tcase( nSize==pP
15657 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b  age->maxLocal );
15658 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69  .  testcase( nSi
15659 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f  ze==pPage->maxLo
1565a 63 61 6c 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e  cal+1 );.  if( n
1565b 53 69 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78 4c  Size>pPage->maxL
1565c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20  ocal ){.    int 
1565d 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67 65  minLocal = pPage
1565e 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20  ->minLocal;.    
1565f 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c  nSize = minLocal
15660 20 2b 20 28 6e 53 69 7a 65 20 2d 20 6d 69 6e 4c   + (nSize - minL
15661 6f 63 61 6c 29 20 25 20 28 70 50 61 67 65 2d 3e  ocal) % (pPage->
15662 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
15663 2d 20 34 29 3b 0a 20 20 20 20 74 65 73 74 63 61  - 4);.    testca
15664 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65  se( nSize==pPage
15665 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20  ->maxLocal );.  
15666 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69 7a    testcase( nSiz
15667 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63  e==pPage->maxLoc
15668 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20  al+1 );.    if( 
15669 6e 53 69 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78  nSize>pPage->max
1566a 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 6e  Local ){.      n
1566b 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c 3b  Size = minLocal;
1566c 0a 20 20 20 20 7d 0a 20 20 20 20 6e 53 69 7a 65  .    }.    nSize
1566d 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 6e 53 69   += 4;.  }.  nSi
1566e 7a 65 20 2b 3d 20 28 75 33 32 29 28 70 49 74 65  ze += (u32)(pIte
1566f 72 20 2d 20 70 43 65 6c 6c 29 3b 0a 0a 20 20 2f  r - pCell);..  /
15670 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 73 69  * The minimum si
15671 7a 65 20 6f 66 20 61 6e 79 20 63 65 6c 6c 20 69  ze of any cell i
15672 73 20 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20  s 4 bytes. */.  
15673 69 66 28 20 6e 53 69 7a 65 3c 34 20 29 7b 0a 20  if( nSize<4 ){. 
15674 20 20 20 6e 53 69 7a 65 20 3d 20 34 3b 0a 20 20     nSize = 4;.  
15675 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 53 69  }..  assert( nSi
15676 7a 65 3d 3d 64 65 62 75 67 69 6e 66 6f 2e 6e 53  ze==debuginfo.nS
15677 69 7a 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ize );.  return 
15678 28 75 31 36 29 6e 53 69 7a 65 3b 0a 7d 0a 23 69  (u16)nSize;.}.#i
15679 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61  fndef NDEBUG.sta
1567a 74 69 63 20 75 31 36 20 63 65 6c 6c 53 69 7a 65  tic u16 cellSize
1567b 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
1567c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 72   int iCell){.  r
1567d 65 74 75 72 6e 20 63 65 6c 6c 53 69 7a 65 50 74  eturn cellSizePt
1567e 72 28 70 50 61 67 65 2c 20 66 69 6e 64 43 65 6c  r(pPage, findCel
1567f 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 29 29  l(pPage, iCell))
15680 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  ;.}.#endif..#ifn
15681 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
15682 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a  AUTOVACUUM./*.**
15683 20 49 66 20 74 68 65 20 63 65 6c 6c 20 70 43 65   If the cell pCe
15684 6c 6c 2c 20 70 61 72 74 20 6f 66 20 70 61 67 65  ll, part of page
15685 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20   pPage contains 
15686 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20  a pointer.** to 
15687 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  an overflow page
15688 2c 20 69 6e 73 65 72 74 20 61 6e 20 65 6e 74 72  , insert an entr
15689 79 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74  y into the point
1568a 65 72 2d 6d 61 70 0a 2a 2a 20 66 6f 72 20 74 68  er-map.** for th
1568b 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e  e overflow page.
1568c 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1568d 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72  ptrmapPutOvflPtr
1568e 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
1568f 20 75 38 20 2a 70 43 65 6c 6c 2c 20 69 6e 74 20   u8 *pCell, int 
15690 2a 70 52 43 29 7b 0a 20 20 43 65 6c 6c 49 6e 66  *pRC){.  CellInf
15691 6f 20 69 6e 66 6f 3b 0a 20 20 69 66 28 20 2a 70  o info;.  if( *p
15692 52 43 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61  RC ) return;.  a
15693 73 73 65 72 74 28 20 70 43 65 6c 6c 21 3d 30 20  ssert( pCell!=0 
15694 29 3b 0a 20 20 62 74 72 65 65 50 61 72 73 65 43  );.  btreeParseC
15695 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43  ellPtr(pPage, pC
15696 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 61  ell, &info);.  a
15697 73 73 65 72 74 28 20 28 69 6e 66 6f 2e 6e 44 61  ssert( (info.nDa
15698 74 61 2b 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65  ta+(pPage->intKe
15699 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79 29 29 3d  y?0:info.nKey))=
1569a 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29  =info.nPayload )
1569b 3b 0a 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76  ;.  if( info.iOv
1569c 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 50 67  erflow ){.    Pg
1569d 6e 6f 20 6f 76 66 6c 20 3d 20 67 65 74 34 62 79  no ovfl = get4by
1569e 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69  te(&pCell[info.i
1569f 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20  Overflow]);.    
156a0 70 74 72 6d 61 70 50 75 74 28 70 50 61 67 65 2d  ptrmapPut(pPage-
156a1 3e 70 42 74 2c 20 6f 76 66 6c 2c 20 50 54 52 4d  >pBt, ovfl, PTRM
156a2 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 70 50  AP_OVERFLOW1, pP
156a3 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 52 43 29 3b  age->pgno, pRC);
156a4 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a  .  }.}.#endif...
156a5 2f 2a 0a 2a 2a 20 44 65 66 72 61 67 6d 65 6e 74  /*.** Defragment
156a6 20 74 68 65 20 70 61 67 65 20 67 69 76 65 6e 2e   the page given.
156a7 20 20 41 6c 6c 20 43 65 6c 6c 73 20 61 72 65 20    All Cells are 
156a8 6d 6f 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20  moved to the.** 
156a9 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 20  end of the page 
156aa 61 6e 64 20 61 6c 6c 20 66 72 65 65 20 73 70 61  and all free spa
156ab 63 65 20 69 73 20 63 6f 6c 6c 65 63 74 65 64 20  ce is collected 
156ac 69 6e 74 6f 20 6f 6e 65 0a 2a 2a 20 62 69 67 20  into one.** big 
156ad 46 72 65 65 42 6c 6b 20 74 68 61 74 20 6f 63 63  FreeBlk that occ
156ae 75 72 73 20 69 6e 20 62 65 74 77 65 65 6e 20 74  urs in between t
156af 68 65 20 68 65 61 64 65 72 20 61 6e 64 20 63 65  he header and ce
156b0 6c 6c 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 61 72  ll.** pointer ar
156b1 72 61 79 20 61 6e 64 20 74 68 65 20 63 65 6c 6c  ray and the cell
156b2 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 0a 2a   content area..*
156b3 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 66  /.static int def
156b4 72 61 67 6d 65 6e 74 50 61 67 65 28 4d 65 6d 50  ragmentPage(MemP
156b5 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69  age *pPage){.  i
156b6 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
156b7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
156b8 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69  p counter */.  i
156b9 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 20  nt pc;          
156ba 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64            /* Add
156bb 72 65 73 73 20 6f 66 20 61 20 69 2d 74 68 20 63  ress of a i-th c
156bc 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72  ell */.  int hdr
156bd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
156be 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f      /* Offset to
156bf 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72   the page header
156c0 20 2a 2f 0a 20 20 69 6e 74 20 73 69 7a 65 3b 20   */.  int size; 
156c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
156c2 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 20 63 65   /* Size of a ce
156c3 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 75 73 61 62  ll */.  int usab
156c4 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20  leSize;         
156c5 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
156c6 75 73 61 62 6c 65 20 62 79 74 65 73 20 6f 6e 20  usable bytes on 
156c7 61 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  a page */.  int 
156c8 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 20 20  cellOffset;     
156c9 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
156ca 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 70 6f 69   to the cell poi
156cb 6e 74 65 72 20 61 72 72 61 79 20 2a 2f 0a 20 20  nter array */.  
156cc 69 6e 74 20 63 62 72 6b 3b 20 20 20 20 20 20 20  int cbrk;       
156cd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66             /* Of
156ce 66 73 65 74 20 74 6f 20 74 68 65 20 63 65 6c 6c  fset to the cell
156cf 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f   content area */
156d0 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20  .  int nCell;   
156d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
156d2 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73   Number of cells
156d3 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a   on the page */.
156d4 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
156d5 2a 64 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20  *data;       /* 
156d6 54 68 65 20 70 61 67 65 20 64 61 74 61 20 2a 2f  The page data */
156d7 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
156d8 20 2a 74 65 6d 70 3b 20 20 20 20 20 20 20 2f 2a   *temp;       /*
156d9 20 54 65 6d 70 20 61 72 65 61 20 66 6f 72 20 63   Temp area for c
156da 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20  ell content */. 
156db 20 69 6e 74 20 69 43 65 6c 6c 46 69 72 73 74 3b   int iCellFirst;
156dc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
156dd 69 72 73 74 20 61 6c 6c 6f 77 61 62 6c 65 20 63  irst allowable c
156de 65 6c 6c 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69  ell index */.  i
156df 6e 74 20 69 43 65 6c 6c 4c 61 73 74 3b 20 20 20  nt iCellLast;   
156e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73            /* Las
156e1 74 20 70 6f 73 73 69 62 6c 65 20 63 65 6c 6c 20  t possible cell 
156e2 69 6e 64 65 78 20 2a 2f 0a 0a 0a 20 20 61 73 73  index */...  ass
156e3 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
156e4 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61  rIswriteable(pPa
156e5 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  ge->pDbPage) );.
156e6 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
156e7 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73  >pBt!=0 );.  ass
156e8 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 2d  ert( pPage->pBt-
156e9 3e 75 73 61 62 6c 65 53 69 7a 65 20 3c 3d 20 53  >usableSize <= S
156ea 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53  QLITE_MAX_PAGE_S
156eb 49 5a 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  IZE );.  assert(
156ec 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f   pPage->nOverflo
156ed 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  w==0 );.  assert
156ee 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
156ef 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d  held(pPage->pBt-
156f0 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 74 65 6d  >mutex) );.  tem
156f1 70 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  p = sqlite3Pager
156f2 54 65 6d 70 53 70 61 63 65 28 70 50 61 67 65 2d  TempSpace(pPage-
156f3 3e 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20  >pBt->pPager);. 
156f4 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61   data = pPage->a
156f5 44 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50  Data;.  hdr = pP
156f6 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a  age->hdrOffset;.
156f7 20 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70    cellOffset = p
156f8 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74  Page->cellOffset
156f9 3b 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67  ;.  nCell = pPag
156fa 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 61 73 73 65  e->nCell;.  asse
156fb 72 74 28 20 6e 43 65 6c 6c 3d 3d 67 65 74 32 62  rt( nCell==get2b
156fc 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d  yte(&data[hdr+3]
156fd 29 20 29 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a  ) );.  usableSiz
156fe 65 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e  e = pPage->pBt->
156ff 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 62  usableSize;.  cb
15700 72 6b 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  rk = get2byte(&d
15701 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 6d  ata[hdr+5]);.  m
15702 65 6d 63 70 79 28 26 74 65 6d 70 5b 63 62 72 6b  emcpy(&temp[cbrk
15703 5d 2c 20 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20  ], &data[cbrk], 
15704 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 63 62 72  usableSize - cbr
15705 6b 29 3b 0a 20 20 63 62 72 6b 20 3d 20 75 73 61  k);.  cbrk = usa
15706 62 6c 65 53 69 7a 65 3b 0a 20 20 69 43 65 6c 6c  bleSize;.  iCell
15707 46 69 72 73 74 20 3d 20 63 65 6c 6c 4f 66 66 73  First = cellOffs
15708 65 74 20 2b 20 32 2a 6e 43 65 6c 6c 3b 0a 20 20  et + 2*nCell;.  
15709 69 43 65 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62  iCellLast = usab
1570a 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 66 6f  leSize - 4;.  fo
1570b 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20  r(i=0; i<nCell; 
1570c 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 41  i++){.    u8 *pA
1570d 64 64 72 3b 20 20 20 20 20 2f 2a 20 54 68 65 20  ddr;     /* The 
1570e 69 2d 74 68 20 63 65 6c 6c 20 70 6f 69 6e 74 65  i-th cell pointe
1570f 72 20 2a 2f 0a 20 20 20 20 70 41 64 64 72 20 3d  r */.    pAddr =
15710 20 26 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65   &data[cellOffse
15711 74 20 2b 20 69 2a 32 5d 3b 0a 20 20 20 20 70 63  t + i*2];.    pc
15712 20 3d 20 67 65 74 32 62 79 74 65 28 70 41 64 64   = get2byte(pAdd
15713 72 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65  r);.    testcase
15714 28 20 70 63 3d 3d 69 43 65 6c 6c 46 69 72 73 74  ( pc==iCellFirst
15715 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
15716 28 20 70 63 3d 3d 69 43 65 6c 6c 4c 61 73 74 20  ( pc==iCellLast 
15717 29 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  );.#if !defined(
15718 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56  SQLITE_ENABLE_OV
15719 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43  ERSIZE_CELL_CHEC
1571a 4b 29 0a 20 20 20 20 2f 2a 20 54 68 65 73 65 20  K).    /* These 
1571b 63 6f 6e 64 69 74 69 6f 6e 73 20 68 61 76 65 20  conditions have 
1571c 61 6c 72 65 61 64 79 20 62 65 65 6e 20 76 65 72  already been ver
1571d 69 66 69 65 64 20 69 6e 20 62 74 72 65 65 49 6e  ified in btreeIn
1571e 69 74 50 61 67 65 28 29 0a 20 20 20 20 2a 2a 20  itPage().    ** 
1571f 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  if SQLITE_ENABLE
15720 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43  _OVERSIZE_CELL_C
15721 48 45 43 4b 20 69 73 20 64 65 66 69 6e 65 64 20  HECK is defined 
15722 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
15723 70 63 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c  pc<iCellFirst ||
15724 20 70 63 3e 69 43 65 6c 6c 4c 61 73 74 20 29 7b   pc>iCellLast ){
15725 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
15726 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
15727 54 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  T;.    }.#endif.
15728 20 20 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d      assert( pc>=
15729 69 43 65 6c 6c 46 69 72 73 74 20 26 26 20 70 63  iCellFirst && pc
1572a 3c 3d 69 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 20  <=iCellLast );. 
1572b 20 20 20 73 69 7a 65 20 3d 20 63 65 6c 6c 53 69     size = cellSi
1572c 7a 65 50 74 72 28 70 50 61 67 65 2c 20 26 74 65  zePtr(pPage, &te
1572d 6d 70 5b 70 63 5d 29 3b 0a 20 20 20 20 63 62 72  mp[pc]);.    cbr
1572e 6b 20 2d 3d 20 73 69 7a 65 3b 0a 23 69 66 20 64  k -= size;.#if d
1572f 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
15730 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45  ABLE_OVERSIZE_CE
15731 4c 4c 5f 43 48 45 43 4b 29 0a 20 20 20 20 69 66  LL_CHECK).    if
15732 28 20 63 62 72 6b 3c 69 43 65 6c 6c 46 69 72 73  ( cbrk<iCellFirs
15733 74 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  t ){.      retur
15734 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
15735 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 23 65 6c  _BKPT;.    }.#el
15736 73 65 0a 20 20 20 20 69 66 28 20 63 62 72 6b 3c  se.    if( cbrk<
15737 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63  iCellFirst || pc
15738 2b 73 69 7a 65 3e 75 73 61 62 6c 65 53 69 7a 65  +size>usableSize
15739 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1573a 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1573b 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  BKPT;.    }.#end
1573c 69 66 0a 20 20 20 20 61 73 73 65 72 74 28 20 63  if.    assert( c
1573d 62 72 6b 2b 73 69 7a 65 3c 3d 75 73 61 62 6c 65  brk+size<=usable
1573e 53 69 7a 65 20 26 26 20 63 62 72 6b 3e 3d 69 43  Size && cbrk>=iC
1573f 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20 20 20 20  ellFirst );.    
15740 74 65 73 74 63 61 73 65 28 20 63 62 72 6b 2b 73  testcase( cbrk+s
15741 69 7a 65 3d 3d 75 73 61 62 6c 65 53 69 7a 65 20  ize==usableSize 
15742 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
15743 20 70 63 2b 73 69 7a 65 3d 3d 75 73 61 62 6c 65   pc+size==usable
15744 53 69 7a 65 20 29 3b 0a 20 20 20 20 6d 65 6d 63  Size );.    memc
15745 70 79 28 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20  py(&data[cbrk], 
15746 26 74 65 6d 70 5b 70 63 5d 2c 20 73 69 7a 65 29  &temp[pc], size)
15747 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65 28 70  ;.    put2byte(p
15748 41 64 64 72 2c 20 63 62 72 6b 29 3b 0a 20 20 7d  Addr, cbrk);.  }
15749 0a 20 20 61 73 73 65 72 74 28 20 63 62 72 6b 3e  .  assert( cbrk>
1574a 3d 69 43 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20  =iCellFirst );. 
1574b 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b   put2byte(&data[
1574c 68 64 72 2b 35 5d 2c 20 63 62 72 6b 29 3b 0a 20  hdr+5], cbrk);. 
1574d 20 64 61 74 61 5b 68 64 72 2b 31 5d 20 3d 20 30   data[hdr+1] = 0
1574e 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 32 5d 20  ;.  data[hdr+2] 
1574f 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b  = 0;.  data[hdr+
15750 37 5d 20 3d 20 30 3b 0a 20 20 6d 65 6d 73 65 74  7] = 0;.  memset
15751 28 26 64 61 74 61 5b 69 43 65 6c 6c 46 69 72 73  (&data[iCellFirs
15752 74 5d 2c 20 30 2c 20 63 62 72 6b 2d 69 43 65 6c  t], 0, cbrk-iCel
15753 6c 46 69 72 73 74 29 3b 0a 20 20 61 73 73 65 72  lFirst);.  asser
15754 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
15755 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65  swriteable(pPage
15756 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
15757 69 66 28 20 63 62 72 6b 2d 69 43 65 6c 6c 46 69  if( cbrk-iCellFi
15758 72 73 74 21 3d 70 50 61 67 65 2d 3e 6e 46 72 65  rst!=pPage->nFre
15759 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
1575a 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
1575b 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  KPT;.  }.  retur
1575c 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
1575d 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e  /*.** Allocate n
1575e 42 79 74 65 20 62 79 74 65 73 20 6f 66 20 73 70  Byte bytes of sp
1575f 61 63 65 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  ace from within 
15760 74 68 65 20 42 2d 54 72 65 65 20 70 61 67 65 20  the B-Tree page 
15761 70 61 73 73 65 64 0a 2a 2a 20 61 73 20 74 68 65  passed.** as the
15762 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e   first argument.
15763 20 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 49 64   Write into *pId
15764 78 20 74 68 65 20 69 6e 64 65 78 20 69 6e 74 6f  x the index into
15765 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 0a   pPage->aData[].
15766 2a 2a 20 6f 66 20 74 68 65 20 66 69 72 73 74 20  ** of the first 
15767 62 79 74 65 20 6f 66 20 61 6c 6c 6f 63 61 74 65  byte of allocate
15768 64 20 73 70 61 63 65 2e 20 52 65 74 75 72 6e 20  d space. Return 
15769 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b  either SQLITE_OK
1576a 20 6f 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20   or.** an error 
1576b 63 6f 64 65 20 28 75 73 75 61 6c 6c 79 20 53 51  code (usually SQ
1576c 4c 49 54 45 5f 43 4f 52 52 55 50 54 29 2e 0a 2a  LITE_CORRUPT)..*
1576d 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20  *.** The caller 
1576e 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20  guarantees that 
1576f 74 68 65 72 65 20 69 73 20 73 75 66 66 69 63 69  there is suffici
15770 65 6e 74 20 73 70 61 63 65 20 74 6f 20 6d 61 6b  ent space to mak
15771 65 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74  e the.** allocat
15772 69 6f 6e 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ion.  This routi
15773 6e 65 20 6d 69 67 68 74 20 6e 65 65 64 20 74 6f  ne might need to
15774 20 64 65 66 72 61 67 6d 65 6e 74 20 69 6e 20 6f   defragment in o
15775 72 64 65 72 20 74 6f 20 62 72 69 6e 67 0a 2a 2a  rder to bring.**
15776 20 61 6c 6c 20 74 68 65 20 73 70 61 63 65 20 74   all the space t
15777 6f 67 65 74 68 65 72 2c 20 68 6f 77 65 76 65 72  ogether, however
15778 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
15779 77 69 6c 6c 20 61 76 6f 69 64 20 75 73 69 6e 67  will avoid using
1577a 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 77  .** the first tw
1577b 6f 20 62 79 74 65 73 20 70 61 73 74 20 74 68 65  o bytes past the
1577c 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72   cell pointer ar
1577d 65 61 20 73 69 6e 63 65 20 70 72 65 73 75 6d 61  ea since presuma
1577e 62 6c 79 20 74 68 69 73 0a 2a 2a 20 61 6c 6c 6f  bly this.** allo
1577f 63 61 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20  cation is being 
15780 6d 61 64 65 20 69 6e 20 6f 72 64 65 72 20 74 6f  made in order to
15781 20 69 6e 73 65 72 74 20 61 20 6e 65 77 20 63 65   insert a new ce
15782 6c 6c 2c 20 73 6f 20 77 65 20 77 69 6c 6c 0a 2a  ll, so we will.*
15783 2a 20 61 6c 73 6f 20 65 6e 64 20 75 70 20 6e 65  * also end up ne
15784 65 64 69 6e 67 20 61 20 6e 65 77 20 63 65 6c 6c  eding a new cell
15785 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61   pointer..*/.sta
15786 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65  tic int allocate
15787 53 70 61 63 65 28 4d 65 6d 50 61 67 65 20 2a 70  Space(MemPage *p
15788 50 61 67 65 2c 20 69 6e 74 20 6e 42 79 74 65 2c  Page, int nByte,
15789 20 69 6e 74 20 2a 70 49 64 78 29 7b 0a 20 20 63   int *pIdx){.  c
1578a 6f 6e 73 74 20 69 6e 74 20 68 64 72 20 3d 20 70  onst int hdr = p
1578b 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b  Page->hdrOffset;
1578c 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63      /* Local cac
1578d 68 65 20 6f 66 20 70 50 61 67 65 2d 3e 68 64 72  he of pPage->hdr
1578e 4f 66 66 73 65 74 20 2a 2f 0a 20 20 75 38 20 2a  Offset */.  u8 *
1578f 20 63 6f 6e 73 74 20 64 61 74 61 20 3d 20 70 50   const data = pP
15790 61 67 65 2d 3e 61 44 61 74 61 3b 20 20 20 20 20  age->aData;     
15791 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20   /* Local cache 
15792 6f 66 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20  of pPage->aData 
15793 2a 2f 0a 20 20 69 6e 74 20 6e 46 72 61 67 3b 20  */.  int nFrag; 
15794 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15795 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
15796 62 65 72 20 6f 66 20 66 72 61 67 6d 65 6e 74 65  ber of fragmente
15797 64 20 62 79 74 65 73 20 6f 6e 20 70 50 61 67 65  d bytes on pPage
15798 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 3b 20 20   */.  int top;  
15799 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1579a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
1579b 72 73 74 20 62 79 74 65 20 6f 66 20 63 65 6c 6c  rst byte of cell
1579c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f   content area */
1579d 0a 20 20 69 6e 74 20 67 61 70 3b 20 20 20 20 20  .  int gap;     
1579e 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65     /* First byte
1579f 20 6f 66 20 67 61 70 20 62 65 74 77 65 65 6e 20   of gap between 
157a0 63 65 6c 6c 20 70 6f 69 6e 74 65 72 73 20 61 6e  cell pointers an
157a1 64 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a  d cell content *
157a2 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  /.  int rc;     
157a3 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 72      /* Integer r
157a4 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
157a5 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
157a6 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
157a7 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  le(pPage->pDbPag
157a8 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  e) );.  assert( 
157a9 70 50 61 67 65 2d 3e 70 42 74 20 29 3b 0a 20 20  pPage->pBt );.  
157aa 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
157ab 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
157ac 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
157ad 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65  .  assert( nByte
157ae 3e 3d 30 20 29 3b 20 20 2f 2a 20 4d 69 6e 69 6d  >=0 );  /* Minim
157af 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 20  um cell size is 
157b0 34 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  4 */.  assert( p
157b1 50 61 67 65 2d 3e 6e 46 72 65 65 3e 3d 6e 42 79  Page->nFree>=nBy
157b2 74 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  te );.  assert( 
157b3 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
157b4 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
157b5 20 6e 42 79 74 65 3c 70 50 61 67 65 2d 3e 70 42   nByte<pPage->pB
157b6 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 38 20  t->usableSize-8 
157b7 29 3b 0a 0a 20 20 6e 46 72 61 67 20 3d 20 64 61  );..  nFrag = da
157b8 74 61 5b 68 64 72 2b 37 5d 3b 0a 20 20 61 73 73  ta[hdr+7];.  ass
157b9 65 72 74 28 20 70 50 61 67 65 2d 3e 63 65 6c 6c  ert( pPage->cell
157ba 4f 66 66 73 65 74 20 3d 3d 20 68 64 72 20 2b 20  Offset == hdr + 
157bb 31 32 20 2d 20 34 2a 70 50 61 67 65 2d 3e 6c 65  12 - 4*pPage->le
157bc 61 66 20 29 3b 0a 20 20 67 61 70 20 3d 20 70 50  af );.  gap = pP
157bd 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20  age->cellOffset 
157be 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  + 2*pPage->nCell
157bf 3b 0a 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79  ;.  top = get2by
157c0 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29  te(&data[hdr+5])
157c1 3b 0a 20 20 69 66 28 20 67 61 70 3e 74 6f 70 20  ;.  if( gap>top 
157c2 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
157c3 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
157c4 74 65 73 74 63 61 73 65 28 20 67 61 70 2b 32 3d  testcase( gap+2=
157c5 3d 74 6f 70 20 29 3b 0a 20 20 74 65 73 74 63 61  =top );.  testca
157c6 73 65 28 20 67 61 70 2b 31 3d 3d 74 6f 70 20 29  se( gap+1==top )
157c7 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 67 61  ;.  testcase( ga
157c8 70 3d 3d 74 6f 70 20 29 3b 0a 0a 20 20 69 66 28  p==top );..  if(
157c9 20 6e 46 72 61 67 3e 3d 36 30 20 29 7b 0a 20 20   nFrag>=60 ){.  
157ca 20 20 2f 2a 20 41 6c 77 61 79 73 20 64 65 66 72    /* Always defr
157cb 61 67 6d 65 6e 74 20 68 69 67 68 6c 79 20 66 72  agment highly fr
157cc 61 67 6d 65 6e 74 65 64 20 70 61 67 65 73 20 2a  agmented pages *
157cd 2f 0a 20 20 20 20 72 63 20 3d 20 64 65 66 72 61  /.    rc = defra
157ce 67 6d 65 6e 74 50 61 67 65 28 70 50 61 67 65 29  gmentPage(pPage)
157cf 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72  ;.    if( rc ) r
157d0 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 74 6f  eturn rc;.    to
157d1 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  p = get2byte(&da
157d2 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 7d 65  ta[hdr+5]);.  }e
157d3 6c 73 65 20 69 66 28 20 67 61 70 2b 32 3c 3d 74  lse if( gap+2<=t
157d4 6f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65 61  op ){.    /* Sea
157d5 72 63 68 20 74 68 65 20 66 72 65 65 6c 69 73 74  rch the freelist
157d6 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 66   looking for a f
157d7 72 65 65 20 73 6c 6f 74 20 62 69 67 20 65 6e 6f  ree slot big eno
157d8 75 67 68 20 74 6f 20 73 61 74 69 73 66 79 20 0a  ugh to satisfy .
157d9 20 20 20 20 2a 2a 20 74 68 65 20 72 65 71 75 65      ** the reque
157da 73 74 2e 20 54 68 65 20 61 6c 6c 6f 63 61 74 69  st. The allocati
157db 6f 6e 20 69 73 20 6d 61 64 65 20 66 72 6f 6d 20  on is made from 
157dc 74 68 65 20 66 69 72 73 74 20 66 72 65 65 20 73  the first free s
157dd 6c 6f 74 20 69 6e 20 0a 20 20 20 20 2a 2a 20 74  lot in .    ** t
157de 68 65 20 6c 69 73 74 20 74 68 61 74 20 69 73 20  he list that is 
157df 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20  large enough to 
157e0 61 63 63 6f 6d 61 64 61 74 65 20 69 74 2e 0a 20  accomadate it.. 
157e1 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 70 63     */.    int pc
157e2 2c 20 61 64 64 72 3b 0a 20 20 20 20 66 6f 72 28  , addr;.    for(
157e3 61 64 64 72 3d 68 64 72 2b 31 3b 20 28 70 63 20  addr=hdr+1; (pc 
157e4 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
157e5 5b 61 64 64 72 5d 29 29 3e 30 3b 20 61 64 64 72  [addr]))>0; addr
157e6 3d 70 63 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  =pc){.      int 
157e7 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28  size = get2byte(
157e8 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b 20 20 20  &data[pc+2]);   
157e9 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 66 72 65    /* Size of fre
157ea 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 20 20 20 20  e slot */.      
157eb 69 66 28 20 73 69 7a 65 3e 3d 6e 42 79 74 65 20  if( size>=nByte 
157ec 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78  ){.        int x
157ed 20 3d 20 73 69 7a 65 20 2d 20 6e 42 79 74 65 3b   = size - nByte;
157ee 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
157ef 65 28 20 78 3d 3d 34 20 29 3b 0a 20 20 20 20 20  e( x==4 );.     
157f0 20 20 20 74 65 73 74 63 61 73 65 28 20 78 3d 3d     testcase( x==
157f1 33 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  3 );.        if(
157f2 20 78 3c 34 20 29 7b 0a 20 20 20 20 20 20 20 20   x<4 ){.        
157f3 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20    /* Remove the 
157f4 73 6c 6f 74 20 66 72 6f 6d 20 74 68 65 20 66 72  slot from the fr
157f5 65 65 2d 6c 69 73 74 2e 20 55 70 64 61 74 65 20  ee-list. Update 
157f6 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20  the number of.  
157f7 20 20 20 20 20 20 20 20 2a 2a 20 66 72 61 67 6d          ** fragm
157f8 65 6e 74 65 64 20 62 79 74 65 73 20 77 69 74 68  ented bytes with
157f9 69 6e 20 74 68 65 20 70 61 67 65 2e 20 2a 2f 0a  in the page. */.
157fa 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79            memcpy
157fb 28 26 64 61 74 61 5b 61 64 64 72 5d 2c 20 26 64  (&data[addr], &d
157fc 61 74 61 5b 70 63 5d 2c 20 32 29 3b 0a 20 20 20  ata[pc], 2);.   
157fd 20 20 20 20 20 20 20 64 61 74 61 5b 68 64 72 2b         data[hdr+
157fe 37 5d 20 3d 20 28 75 38 29 28 6e 46 72 61 67 20  7] = (u8)(nFrag 
157ff 2b 20 78 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  + x);.        }e
15800 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  lse{.          /
15801 2a 20 54 68 65 20 73 6c 6f 74 20 72 65 6d 61 69  * The slot remai
15802 6e 73 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c  ns on the free-l
15803 69 73 74 2e 20 52 65 64 75 63 65 20 69 74 73 20  ist. Reduce its 
15804 73 69 7a 65 20 74 6f 20 61 63 63 6f 75 6e 74 0a  size to account.
15805 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 6f 72            ** for
15806 20 74 68 65 20 70 6f 72 74 69 6f 6e 20 75 73 65   the portion use
15807 64 20 62 79 20 74 68 65 20 6e 65 77 20 61 6c 6c  d by the new all
15808 6f 63 61 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20  ocation. */.    
15809 20 20 20 20 20 20 70 75 74 32 62 79 74 65 28 26        put2byte(&
1580a 64 61 74 61 5b 70 63 2b 32 5d 2c 20 78 29 3b 0a  data[pc+2], x);.
1580b 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1580c 20 20 2a 70 49 64 78 20 3d 20 70 63 20 2b 20 78    *pIdx = pc + x
1580d 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
1580e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
1580f 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
15810 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b   /* Check to mak
15811 65 20 73 75 72 65 20 74 68 65 72 65 20 69 73 20  e sure there is 
15812 65 6e 6f 75 67 68 20 73 70 61 63 65 20 69 6e 20  enough space in 
15813 74 68 65 20 67 61 70 20 74 6f 20 73 61 74 69 73  the gap to satis
15814 66 79 0a 20 20 2a 2a 20 74 68 65 20 61 6c 6c 6f  fy.  ** the allo
15815 63 61 74 69 6f 6e 2e 20 20 49 66 20 6e 6f 74 2c  cation.  If not,
15816 20 64 65 66 72 61 67 6d 65 6e 74 2e 0a 20 20 2a   defragment..  *
15817 2f 0a 20 20 74 65 73 74 63 61 73 65 28 20 67 61  /.  testcase( ga
15818 70 2b 32 2b 6e 42 79 74 65 3d 3d 74 6f 70 20 29  p+2+nByte==top )
15819 3b 0a 20 20 69 66 28 20 67 61 70 2b 32 2b 6e 42  ;.  if( gap+2+nB
1581a 79 74 65 3e 74 6f 70 20 29 7b 0a 20 20 20 20 72  yte>top ){.    r
1581b 63 20 3d 20 64 65 66 72 61 67 6d 65 6e 74 50 61  c = defragmentPa
1581c 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 69  ge(pPage);.    i
1581d 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
1581e 63 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74  c;.    top = get
1581f 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
15820 35 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  5]);.    assert(
15821 20 67 61 70 2b 6e 42 79 74 65 3c 3d 74 6f 70 20   gap+nByte<=top 
15822 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41 6c  );.  }...  /* Al
15823 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 72  locate memory fr
15824 6f 6d 20 74 68 65 20 67 61 70 20 69 6e 20 62 65  om the gap in be
15825 74 77 65 65 6e 20 74 68 65 20 63 65 6c 6c 20 70  tween the cell p
15826 6f 69 6e 74 65 72 20 61 72 72 61 79 0a 20 20 2a  ointer array.  *
15827 2a 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63  * and the cell c
15828 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 54 68  ontent area.  Th
15829 65 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28  e btreeInitPage(
1582a 29 20 63 61 6c 6c 20 68 61 73 20 61 6c 72 65 61  ) call has alrea
1582b 64 79 0a 20 20 2a 2a 20 76 61 6c 69 64 61 74 65  dy.  ** validate
1582c 64 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20  d the freelist. 
1582d 20 47 69 76 65 6e 20 74 68 61 74 20 74 68 65 20   Given that the 
1582e 66 72 65 65 6c 69 73 74 20 69 73 20 76 61 6c 69  freelist is vali
1582f 64 2c 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73  d, there.  ** is
15830 20 6e 6f 20 77 61 79 20 74 68 61 74 20 74 68 65   no way that the
15831 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 63 61 6e 20   allocation can 
15832 65 78 74 65 6e 64 20 6f 66 66 20 74 68 65 20 65  extend off the e
15833 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 2e 0a  nd of the page..
15834 20 20 2a 2a 20 54 68 65 20 61 73 73 65 72 74 28    ** The assert(
15835 29 20 62 65 6c 6f 77 20 76 65 72 69 66 69 65 73  ) below verifies
15836 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65   the previous se
15837 6e 74 65 6e 63 65 2e 0a 20 20 2a 2f 0a 20 20 74  ntence..  */.  t
15838 6f 70 20 2d 3d 20 6e 42 79 74 65 3b 0a 20 20 70  op -= nByte;.  p
15839 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  ut2byte(&data[hd
1583a 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a 20 20 61 73  r+5], top);.  as
1583b 73 65 72 74 28 20 74 6f 70 2b 6e 42 79 74 65 20  sert( top+nByte 
1583c 3c 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75  <= pPage->pBt->u
1583d 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 2a  sableSize );.  *
1583e 70 49 64 78 20 3d 20 74 6f 70 3b 0a 20 20 72 65  pIdx = top;.  re
1583f 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
15840 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
15841 61 20 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65  a section of the
15842 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 74 6f   pPage->aData to
15843 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a   the freelist..*
15844 2a 20 54 68 65 20 66 69 72 73 74 20 62 79 74 65  * The first byte
15845 20 6f 66 20 74 68 65 20 6e 65 77 20 66 72 65 65   of the new free
15846 20 62 6c 6f 63 6b 20 69 73 20 70 50 61 67 65 2d   block is pPage-
15847 3e 61 44 69 73 6b 5b 73 74 61 72 74 5d 0a 2a 2a  >aDisk[start].**
15848 20 61 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66   and the size of
15849 20 74 68 65 20 62 6c 6f 63 6b 20 69 73 20 22 73   the block is "s
1584a 69 7a 65 22 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a  ize" bytes..**.*
1584b 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 65 66  * Most of the ef
1584c 66 6f 72 74 20 68 65 72 65 20 69 73 20 69 6e 76  fort here is inv
1584d 6f 6c 76 65 64 20 69 6e 20 63 6f 61 6c 65 73 69  olved in coalesi
1584e 6e 67 20 61 64 6a 61 63 65 6e 74 0a 2a 2a 20 66  ng adjacent.** f
1584f 72 65 65 20 62 6c 6f 63 6b 73 20 69 6e 74 6f 20  ree blocks into 
15850 61 20 73 69 6e 67 6c 65 20 62 69 67 20 66 72 65  a single big fre
15851 65 20 62 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74  e block..*/.stat
15852 69 63 20 69 6e 74 20 66 72 65 65 53 70 61 63 65  ic int freeSpace
15853 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
15854 20 69 6e 74 20 73 74 61 72 74 2c 20 69 6e 74 20   int start, int 
15855 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 61 64 64  size){.  int add
15856 72 2c 20 70 62 65 67 69 6e 2c 20 68 64 72 3b 0a  r, pbegin, hdr;.
15857 20 20 69 6e 74 20 69 4c 61 73 74 3b 20 20 20 20    int iLast;    
15858 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15859 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 70      /* Largest p
1585a 6f 73 73 69 62 6c 65 20 66 72 65 65 62 6c 6f 63  ossible freebloc
1585b 6b 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 75 6e  k offset */.  un
1585c 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74  signed char *dat
1585d 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61  a = pPage->aData
1585e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ;..  assert( pPa
1585f 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20  ge->pBt!=0 );.  
15860 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
15861 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
15862 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20  pPage->pDbPage) 
15863 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 74 61  );.  assert( sta
15864 72 74 3e 3d 70 50 61 67 65 2d 3e 68 64 72 4f 66  rt>=pPage->hdrOf
15865 66 73 65 74 2b 36 2b 70 50 61 67 65 2d 3e 63 68  fset+6+pPage->ch
15866 69 6c 64 50 74 72 53 69 7a 65 20 29 3b 0a 20 20  ildPtrSize );.  
15867 61 73 73 65 72 74 28 20 28 73 74 61 72 74 20 2b  assert( (start +
15868 20 73 69 7a 65 29 3c 3d 70 50 61 67 65 2d 3e 70   size)<=pPage->p
15869 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29  Bt->usableSize )
1586a 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
1586b 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1586c 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
1586d 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
1586e 69 7a 65 3e 3d 30 20 29 3b 20 20 20 2f 2a 20 4d  ize>=0 );   /* M
1586f 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65  inimum cell size
15870 20 69 73 20 34 20 2a 2f 0a 0a 23 69 66 64 65 66   is 4 */..#ifdef
15871 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44   SQLITE_SECURE_D
15872 45 4c 45 54 45 0a 20 20 2f 2a 20 4f 76 65 72 77  ELETE.  /* Overw
15873 72 69 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66  rite deleted inf
15874 6f 72 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65  ormation with ze
15875 72 6f 73 20 77 68 65 6e 20 74 68 65 20 53 45 43  ros when the SEC
15876 55 52 45 5f 44 45 4c 45 54 45 20 0a 20 20 2a 2a  URE_DELETE .  **
15877 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c   option is enabl
15878 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69  ed at compile-ti
15879 6d 65 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26  me */.  memset(&
1587a 64 61 74 61 5b 73 74 61 72 74 5d 2c 20 30 2c 20  data[start], 0, 
1587b 73 69 7a 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20  size);.#endif.. 
1587c 20 2f 2a 20 41 64 64 20 74 68 65 20 73 70 61 63   /* Add the spac
1587d 65 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20  e back into the 
1587e 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 66  linked list of f
1587f 72 65 65 62 6c 6f 63 6b 73 2e 20 20 4e 6f 74 65  reeblocks.  Note
15880 20 74 68 61 74 0a 20 20 2a 2a 20 65 76 65 6e 20   that.  ** even 
15881 74 68 6f 75 67 68 20 74 68 65 20 66 72 65 65 62  though the freeb
15882 6c 6f 63 6b 20 6c 69 73 74 20 77 61 73 20 63 68  lock list was ch
15883 65 63 6b 65 64 20 62 79 20 62 74 72 65 65 49 6e  ecked by btreeIn
15884 69 74 50 61 67 65 28 29 2c 0a 20 20 2a 2a 20 62  itPage(),.  ** b
15885 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 64  treeInitPage() d
15886 69 64 20 6e 6f 74 20 64 65 74 65 63 74 20 6f 76  id not detect ov
15887 65 72 6c 61 70 70 69 6e 67 20 63 65 6c 6c 73 20  erlapping cells 
15888 6f 72 0a 20 20 2a 2a 20 66 72 65 65 62 6c 6f 63  or.  ** freebloc
15889 6b 73 20 74 68 61 74 20 6f 76 65 72 6c 61 70 70  ks that overlapp
1588a 65 64 20 63 65 6c 6c 73 2e 20 20 20 4e 6f 72 20  ed cells.   Nor 
1588b 64 6f 65 73 20 69 74 20 64 65 74 65 63 74 20 77  does it detect w
1588c 68 65 6e 20 74 68 65 0a 20 20 2a 2a 20 63 65 6c  hen the.  ** cel
1588d 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 65  l content area e
1588e 78 63 65 65 64 73 20 74 68 65 20 76 61 6c 75 65  xceeds the value
1588f 20 69 6e 20 74 68 65 20 70 61 67 65 20 68 65 61   in the page hea
15890 64 65 72 2e 20 20 49 66 20 74 68 65 73 65 0a 20  der.  If these. 
15891 20 2a 2a 20 73 69 74 75 61 74 69 6f 6e 73 20 61   ** situations a
15892 72 69 73 65 2c 20 74 68 65 6e 20 73 75 62 73 65  rise, then subse
15893 71 75 65 6e 74 20 69 6e 73 65 72 74 20 6f 70 65  quent insert ope
15894 72 61 74 69 6f 6e 73 20 6d 69 67 68 74 20 63 6f  rations might co
15895 72 72 75 70 74 0a 20 20 2a 2a 20 74 68 65 20 66  rrupt.  ** the f
15896 72 65 65 6c 69 73 74 2e 20 20 53 6f 20 77 65 20  reelist.  So we 
15897 64 6f 20 6e 65 65 64 20 74 6f 20 63 68 65 63 6b  do need to check
15898 20 66 6f 72 20 63 6f 72 72 75 70 74 69 6f 6e 20   for corruption 
15899 77 68 69 6c 65 20 73 63 61 6e 6e 69 6e 67 0a 20  while scanning. 
1589a 20 2a 2a 20 74 68 65 20 66 72 65 65 6c 69 73 74   ** the freelist
1589b 2e 0a 20 20 2a 2f 0a 20 20 68 64 72 20 3d 20 70  ..  */.  hdr = p
1589c 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b  Page->hdrOffset;
1589d 0a 20 20 61 64 64 72 20 3d 20 68 64 72 20 2b 20  .  addr = hdr + 
1589e 31 3b 0a 20 20 69 4c 61 73 74 20 3d 20 70 50 61  1;.  iLast = pPa
1589f 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53  ge->pBt->usableS
158a0 69 7a 65 20 2d 20 34 3b 0a 20 20 61 73 73 65 72  ize - 4;.  asser
158a1 74 28 20 73 74 61 72 74 3c 3d 69 4c 61 73 74 20  t( start<=iLast 
158a2 29 3b 0a 20 20 77 68 69 6c 65 28 20 28 70 62 65  );.  while( (pbe
158a3 67 69 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26  gin = get2byte(&
158a4 64 61 74 61 5b 61 64 64 72 5d 29 29 3c 73 74 61  data[addr]))<sta
158a5 72 74 20 26 26 20 70 62 65 67 69 6e 3e 30 20 29  rt && pbegin>0 )
158a6 7b 0a 20 20 20 20 69 66 28 20 70 62 65 67 69 6e  {.    if( pbegin
158a7 3c 61 64 64 72 2b 34 20 29 7b 0a 20 20 20 20 20  <addr+4 ){.     
158a8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
158a9 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
158aa 20 7d 0a 20 20 20 20 61 64 64 72 20 3d 20 70 62   }.    addr = pb
158ab 65 67 69 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20  egin;.  }.  if( 
158ac 70 62 65 67 69 6e 3e 69 4c 61 73 74 20 29 7b 0a  pbegin>iLast ){.
158ad 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
158ae 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
158af 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 62    }.  assert( pb
158b0 65 67 69 6e 3e 61 64 64 72 20 7c 7c 20 70 62 65  egin>addr || pbe
158b1 67 69 6e 3d 3d 30 20 29 3b 0a 20 20 70 75 74 32  gin==0 );.  put2
158b2 62 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d  byte(&data[addr]
158b3 2c 20 73 74 61 72 74 29 3b 0a 20 20 70 75 74 32  , start);.  put2
158b4 62 79 74 65 28 26 64 61 74 61 5b 73 74 61 72 74  byte(&data[start
158b5 5d 2c 20 70 62 65 67 69 6e 29 3b 0a 20 20 70 75  ], pbegin);.  pu
158b6 74 32 62 79 74 65 28 26 64 61 74 61 5b 73 74 61  t2byte(&data[sta
158b7 72 74 2b 32 5d 2c 20 73 69 7a 65 29 3b 0a 20 20  rt+2], size);.  
158b8 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 70  pPage->nFree = p
158b9 50 61 67 65 2d 3e 6e 46 72 65 65 20 2b 20 28 75  Page->nFree + (u
158ba 31 36 29 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 43  16)size;..  /* C
158bb 6f 61 6c 65 73 63 65 20 61 64 6a 61 63 65 6e 74  oalesce adjacent
158bc 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a   free blocks */.
158bd 20 20 61 64 64 72 20 3d 20 68 64 72 20 2b 20 31    addr = hdr + 1
158be 3b 0a 20 20 77 68 69 6c 65 28 20 28 70 62 65 67  ;.  while( (pbeg
158bf 69 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  in = get2byte(&d
158c0 61 74 61 5b 61 64 64 72 5d 29 29 3e 30 20 29 7b  ata[addr]))>0 ){
158c1 0a 20 20 20 20 69 6e 74 20 70 6e 65 78 74 2c 20  .    int pnext, 
158c2 70 73 69 7a 65 2c 20 78 3b 0a 20 20 20 20 61 73  psize, x;.    as
158c3 73 65 72 74 28 20 70 62 65 67 69 6e 3e 61 64 64  sert( pbegin>add
158c4 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  r );.    assert(
158c5 20 70 62 65 67 69 6e 3c 3d 70 50 61 67 65 2d 3e   pbegin<=pPage->
158c6 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d  pBt->usableSize-
158c7 34 20 29 3b 0a 20 20 20 20 70 6e 65 78 74 20 3d  4 );.    pnext =
158c8 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
158c9 70 62 65 67 69 6e 5d 29 3b 0a 20 20 20 20 70 73  pbegin]);.    ps
158ca 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26  ize = get2byte(&
158cb 64 61 74 61 5b 70 62 65 67 69 6e 2b 32 5d 29 3b  data[pbegin+2]);
158cc 0a 20 20 20 20 69 66 28 20 70 62 65 67 69 6e 20  .    if( pbegin 
158cd 2b 20 70 73 69 7a 65 20 2b 20 33 20 3e 3d 20 70  + psize + 3 >= p
158ce 6e 65 78 74 20 26 26 20 70 6e 65 78 74 3e 30 20  next && pnext>0 
158cf 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 66 72 61  ){.      int fra
158d0 67 20 3d 20 70 6e 65 78 74 20 2d 20 28 70 62 65  g = pnext - (pbe
158d1 67 69 6e 2b 70 73 69 7a 65 29 3b 0a 20 20 20 20  gin+psize);.    
158d2 20 20 69 66 28 20 28 66 72 61 67 3c 30 29 20 7c    if( (frag<0) |
158d3 7c 20 28 66 72 61 67 3e 28 69 6e 74 29 64 61 74  | (frag>(int)dat
158d4 61 5b 68 64 72 2b 37 5d 29 20 29 7b 0a 20 20 20  a[hdr+7]) ){.   
158d5 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
158d6 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
158d7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64  .      }.      d
158d8 61 74 61 5b 68 64 72 2b 37 5d 20 2d 3d 20 28 75  ata[hdr+7] -= (u
158d9 38 29 66 72 61 67 3b 0a 20 20 20 20 20 20 78 20  8)frag;.      x 
158da 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
158db 5b 70 6e 65 78 74 5d 29 3b 0a 20 20 20 20 20 20  [pnext]);.      
158dc 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  put2byte(&data[p
158dd 62 65 67 69 6e 5d 2c 20 78 29 3b 0a 20 20 20 20  begin], x);.    
158de 20 20 78 20 3d 20 70 6e 65 78 74 20 2b 20 67 65    x = pnext + ge
158df 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 6e 65  t2byte(&data[pne
158e0 78 74 2b 32 5d 29 20 2d 20 70 62 65 67 69 6e 3b  xt+2]) - pbegin;
158e1 0a 20 20 20 20 20 20 70 75 74 32 62 79 74 65 28  .      put2byte(
158e2 26 64 61 74 61 5b 70 62 65 67 69 6e 2b 32 5d 2c  &data[pbegin+2],
158e3 20 78 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a   x);.    }else{.
158e4 20 20 20 20 20 20 61 64 64 72 20 3d 20 70 62 65        addr = pbe
158e5 67 69 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  gin;.    }.  }..
158e6 20 20 2f 2a 20 49 66 20 74 68 65 20 63 65 6c 6c    /* If the cell
158e7 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 62 65   content area be
158e8 67 69 6e 73 20 77 69 74 68 20 61 20 66 72 65 65  gins with a free
158e9 62 6c 6f 63 6b 2c 20 72 65 6d 6f 76 65 20 69 74  block, remove it
158ea 2e 20 2a 2f 0a 20 20 69 66 28 20 64 61 74 61 5b  . */.  if( data[
158eb 68 64 72 2b 31 5d 3d 3d 64 61 74 61 5b 68 64 72  hdr+1]==data[hdr
158ec 2b 35 5d 20 26 26 20 64 61 74 61 5b 68 64 72 2b  +5] && data[hdr+
158ed 32 5d 3d 3d 64 61 74 61 5b 68 64 72 2b 36 5d 20  2]==data[hdr+6] 
158ee 29 7b 0a 20 20 20 20 69 6e 74 20 74 6f 70 3b 0a  ){.    int top;.
158ef 20 20 20 20 70 62 65 67 69 6e 20 3d 20 67 65 74      pbegin = get
158f0 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
158f1 31 5d 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  1]);.    memcpy(
158f2 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20 26 64  &data[hdr+1], &d
158f3 61 74 61 5b 70 62 65 67 69 6e 5d 2c 20 32 29 3b  ata[pbegin], 2);
158f4 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62  .    top = get2b
158f5 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d  yte(&data[hdr+5]
158f6 29 20 2b 20 67 65 74 32 62 79 74 65 28 26 64 61  ) + get2byte(&da
158f7 74 61 5b 70 62 65 67 69 6e 2b 32 5d 29 3b 0a 20  ta[pbegin+2]);. 
158f8 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74     put2byte(&dat
158f9 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a  a[hdr+5], top);.
158fa 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 73 71    }.  assert( sq
158fb 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
158fc 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62  eable(pPage->pDb
158fd 50 61 67 65 29 20 29 3b 0a 20 20 72 65 74 75 72  Page) );.  retur
158fe 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
158ff 2f 2a 0a 2a 2a 20 44 65 63 6f 64 65 20 74 68 65  /*.** Decode the
15900 20 66 6c 61 67 73 20 62 79 74 65 20 28 74 68 65   flags byte (the
15901 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74   first byte of t
15902 68 65 20 68 65 61 64 65 72 29 20 66 6f 72 20 61  he header) for a
15903 20 70 61 67 65 0a 2a 2a 20 61 6e 64 20 69 6e 69   page.** and ini
15904 74 69 61 6c 69 7a 65 20 66 69 65 6c 64 73 20 6f  tialize fields o
15905 66 20 74 68 65 20 4d 65 6d 50 61 67 65 20 73 74  f the MemPage st
15906 72 75 63 74 75 72 65 20 61 63 63 6f 72 64 69 6e  ructure accordin
15907 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20  gly..**.** Only 
15908 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  the following co
15909 6d 62 69 6e 61 74 69 6f 6e 73 20 61 72 65 20 73  mbinations are s
1590a 75 70 70 6f 72 74 65 64 2e 20 20 41 6e 79 74 68  upported.  Anyth
1590b 69 6e 67 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a  ing different.**
1590c 20 69 6e 64 69 63 61 74 65 73 20 61 20 63 6f 72   indicates a cor
1590d 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69  rupt database fi
1590e 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  les:.**.**      
1590f 20 20 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 0a     PTF_ZERODATA.
15910 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f 5a  **         PTF_Z
15911 45 52 4f 44 41 54 41 20 7c 20 50 54 46 5f 4c 45  ERODATA | PTF_LE
15912 41 46 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 54  AF.**         PT
15913 46 5f 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46  F_LEAFDATA | PTF
15914 5f 49 4e 54 4b 45 59 0a 2a 2a 20 20 20 20 20 20  _INTKEY.**      
15915 20 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20     PTF_LEAFDATA 
15916 7c 20 50 54 46 5f 49 4e 54 4b 45 59 20 7c 20 50  | PTF_INTKEY | P
15917 54 46 5f 4c 45 41 46 0a 2a 2f 0a 73 74 61 74 69  TF_LEAF.*/.stati
15918 63 20 69 6e 74 20 64 65 63 6f 64 65 46 6c 61 67  c int decodeFlag
15919 73 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  s(MemPage *pPage
1591a 2c 20 69 6e 74 20 66 6c 61 67 42 79 74 65 29 7b  , int flagByte){
1591b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
1591c 3b 20 20 20 20 20 2f 2a 20 41 20 63 6f 70 79 20  ;     /* A copy 
1591d 6f 66 20 70 50 61 67 65 2d 3e 70 42 74 20 2a 2f  of pPage->pBt */
1591e 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ..  assert( pPag
1591f 65 2d 3e 68 64 72 4f 66 66 73 65 74 3d 3d 28 70  e->hdrOffset==(p
15920 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 3f 20  Page->pgno==1 ? 
15921 31 30 30 20 3a 20 30 29 20 29 3b 0a 20 20 61 73  100 : 0) );.  as
15922 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
15923 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
15924 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
15925 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 3d 20 28   pPage->leaf = (
15926 75 38 29 28 66 6c 61 67 42 79 74 65 3e 3e 33 29  u8)(flagByte>>3)
15927 3b 20 20 61 73 73 65 72 74 28 20 50 54 46 5f 4c  ;  assert( PTF_L
15928 45 41 46 20 3d 3d 20 31 3c 3c 33 20 29 3b 0a 20  EAF == 1<<3 );. 
15929 20 66 6c 61 67 42 79 74 65 20 26 3d 20 7e 50 54   flagByte &= ~PT
1592a 46 5f 4c 45 41 46 3b 0a 20 20 70 50 61 67 65 2d  F_LEAF;.  pPage-
1592b 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 20 3d 20  >childPtrSize = 
1592c 34 2d 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b  4-4*pPage->leaf;
1592d 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e  .  pBt = pPage->
1592e 70 42 74 3b 0a 20 20 69 66 28 20 66 6c 61 67 42  pBt;.  if( flagB
1592f 79 74 65 3d 3d 28 50 54 46 5f 4c 45 41 46 44 41  yte==(PTF_LEAFDA
15930 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 29  TA | PTF_INTKEY)
15931 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69   ){.    pPage->i
15932 6e 74 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 70  ntKey = 1;.    p
15933 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 3d 20  Page->hasData = 
15934 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20  pPage->leaf;.   
15935 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c   pPage->maxLocal
15936 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 3b   = pBt->maxLeaf;
15937 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 69 6e 4c  .    pPage->minL
15938 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c  ocal = pBt->minL
15939 65 61 66 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  eaf;.  }else if(
1593a 20 66 6c 61 67 42 79 74 65 3d 3d 50 54 46 5f 5a   flagByte==PTF_Z
1593b 45 52 4f 44 41 54 41 20 29 7b 0a 20 20 20 20 70  ERODATA ){.    p
1593c 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 30  Page->intKey = 0
1593d 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 68 61 73  ;.    pPage->has
1593e 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20 70 50  Data = 0;.    pP
1593f 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20  age->maxLocal = 
15940 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20  pBt->maxLocal;. 
15941 20 20 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63     pPage->minLoc
15942 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63  al = pBt->minLoc
15943 61 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  al;.  }else{.   
15944 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
15945 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d  ORRUPT_BKPT;.  }
15946 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
15947 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  _OK;.}../*.** In
15948 69 74 69 61 6c 69 7a 65 20 74 68 65 20 61 75 78  itialize the aux
15949 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69  iliary informati
1594a 6f 6e 20 66 6f 72 20 61 20 64 69 73 6b 20 62 6c  on for a disk bl
1594b 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ock..**.** Retur
1594c 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  n SQLITE_OK on s
1594d 75 63 63 65 73 73 2e 20 20 49 66 20 77 65 20 73  uccess.  If we s
1594e 65 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65  ee that the page
1594f 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e   does.** not con
15950 74 61 69 6e 20 61 20 77 65 6c 6c 2d 66 6f 72 6d  tain a well-form
15951 65 64 20 64 61 74 61 62 61 73 65 20 70 61 67 65  ed database page
15952 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 0a 2a  , then return .*
15953 2a 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  * SQLITE_CORRUPT
15954 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 61 20 72  .  Note that a r
15955 65 74 75 72 6e 20 6f 66 20 53 51 4c 49 54 45 5f  eturn of SQLITE_
15956 4f 4b 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 67  OK does not.** g
15957 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68  uarantee that th
15958 65 20 70 61 67 65 20 69 73 20 77 65 6c 6c 2d 66  e page is well-f
15959 6f 72 6d 65 64 2e 20 20 49 74 20 6f 6e 6c 79 20  ormed.  It only 
1595a 73 68 6f 77 73 20 74 68 61 74 0a 2a 2a 20 77 65  shows that.** we
1595b 20 66 61 69 6c 65 64 20 74 6f 20 64 65 74 65 63   failed to detec
1595c 74 20 61 6e 79 20 63 6f 72 72 75 70 74 69 6f 6e  t any corruption
1595d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1595e 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 4d 65  btreeInitPage(Me
1595f 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 0a  mPage *pPage){..
15960 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
15961 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73  >pBt!=0 );.  ass
15962 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
15963 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
15964 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
15965 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70  assert( pPage->p
15966 67 6e 6f 3d 3d 73 71 6c 69 74 65 33 50 61 67 65  gno==sqlite3Page
15967 72 50 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67  rPagenumber(pPag
15968 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  e->pDbPage) );. 
15969 20 61 73 73 65 72 74 28 20 70 50 61 67 65 20 3d   assert( pPage =
1596a 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
1596b 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44  tExtra(pPage->pD
1596c 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65  bPage) );.  asse
1596d 72 74 28 20 70 50 61 67 65 2d 3e 61 44 61 74 61  rt( pPage->aData
1596e 20 3d 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72   == sqlite3Pager
1596f 47 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70  GetData(pPage->p
15970 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 69 66  DbPage) );..  if
15971 28 20 21 70 50 61 67 65 2d 3e 69 73 49 6e 69 74  ( !pPage->isInit
15972 20 29 7b 0a 20 20 20 20 75 31 36 20 70 63 3b 20   ){.    u16 pc; 
15973 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
15974 64 72 65 73 73 20 6f 66 20 61 20 66 72 65 65 62  dress of a freeb
15975 6c 6f 63 6b 20 77 69 74 68 69 6e 20 70 50 61 67  lock within pPag
15976 65 2d 3e 61 44 61 74 61 5b 5d 20 2a 2f 0a 20 20  e->aData[] */.  
15977 20 20 75 38 20 68 64 72 3b 20 20 20 20 20 20 20    u8 hdr;       
15978 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74       /* Offset t
15979 6f 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 70  o beginning of p
1597a 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  age header */.  
1597b 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20    u8 *data;     
1597c 20 20 20 20 20 2f 2a 20 45 71 75 61 6c 20 74 6f       /* Equal to
1597d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f   pPage->aData */
1597e 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70  .    BtShared *p
1597f 42 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68  Bt;        /* Th
15980 65 20 6d 61 69 6e 20 62 74 72 65 65 20 73 74 72  e main btree str
15981 75 63 74 75 72 65 20 2a 2f 0a 20 20 20 20 75 31  ucture */.    u1
15982 36 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20  6 usableSize;   
15983 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 75 73   /* Amount of us
15984 61 62 6c 65 20 73 70 61 63 65 20 6f 6e 20 65 61  able space on ea
15985 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 75  ch page */.    u
15986 31 36 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20  16 cellOffset;  
15987 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f 6d    /* Offset from
15988 20 73 74 61 72 74 20 6f 66 20 70 61 67 65 20 74   start of page t
15989 6f 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69  o first cell poi
1598a 6e 74 65 72 20 2a 2f 0a 20 20 20 20 75 31 36 20  nter */.    u16 
1598b 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20 20 2f  nFree;         /
1598c 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 6e 75 73  * Number of unus
1598d 65 64 20 62 79 74 65 73 20 6f 6e 20 74 68 65 20  ed bytes on the 
1598e 70 61 67 65 20 2a 2f 0a 20 20 20 20 75 31 36 20  page */.    u16 
1598f 74 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f  top;           /
15990 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20  * First byte of 
15991 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
15992 20 61 72 65 61 20 2a 2f 0a 20 20 20 20 69 6e 74   area */.    int
15993 20 69 43 65 6c 6c 46 69 72 73 74 3b 20 20 20 20   iCellFirst;    
15994 2f 2a 20 46 69 72 73 74 20 61 6c 6c 6f 77 61 62  /* First allowab
15995 6c 65 20 63 65 6c 6c 20 6f 72 20 66 72 65 65 62  le cell or freeb
15996 6c 6f 63 6b 20 6f 66 66 73 65 74 20 2a 2f 0a 20  lock offset */. 
15997 20 20 20 69 6e 74 20 69 43 65 6c 6c 4c 61 73 74     int iCellLast
15998 3b 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 70 6f  ;     /* Last po
15999 73 73 69 62 6c 65 20 63 65 6c 6c 20 6f 72 20 66  ssible cell or f
1599a 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73 65 74 20  reeblock offset 
1599b 2a 2f 0a 0a 20 20 20 20 70 42 74 20 3d 20 70 50  */..    pBt = pP
1599c 61 67 65 2d 3e 70 42 74 3b 0a 0a 20 20 20 20 68  age->pBt;..    h
1599d 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f  dr = pPage->hdrO
1599e 66 66 73 65 74 3b 0a 20 20 20 20 64 61 74 61 20  ffset;.    data 
1599f 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a  = pPage->aData;.
159a0 20 20 20 20 69 66 28 20 64 65 63 6f 64 65 46 6c      if( decodeFl
159a1 61 67 73 28 70 50 61 67 65 2c 20 64 61 74 61 5b  ags(pPage, data[
159a2 68 64 72 5d 29 20 29 20 72 65 74 75 72 6e 20 53  hdr]) ) return S
159a3 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
159a4 50 54 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  PT;.    assert( 
159a5 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35  pBt->pageSize>=5
159a6 31 32 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53  12 && pBt->pageS
159a7 69 7a 65 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20  ize<=32768 );.  
159a8 20 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67    pPage->maskPag
159a9 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  e = pBt->pageSiz
159aa 65 20 2d 20 31 3b 0a 20 20 20 20 70 50 61 67 65  e - 1;.    pPage
159ab 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b  ->nOverflow = 0;
159ac 0a 20 20 20 20 75 73 61 62 6c 65 53 69 7a 65 20  .    usableSize 
159ad 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  = pBt->usableSiz
159ae 65 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 63 65  e;.    pPage->ce
159af 6c 6c 4f 66 66 73 65 74 20 3d 20 63 65 6c 6c 4f  llOffset = cellO
159b0 66 66 73 65 74 20 3d 20 68 64 72 20 2b 20 31 32  ffset = hdr + 12
159b1 20 2d 20 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66   - 4*pPage->leaf
159b2 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32  ;.    top = get2
159b3 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
159b4 5d 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e  ]);.    pPage->n
159b5 43 65 6c 6c 20 3d 20 67 65 74 32 62 79 74 65 28  Cell = get2byte(
159b6 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a 20  &data[hdr+3]);. 
159b7 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 43     if( pPage->nC
159b8 65 6c 6c 3e 4d 58 5f 43 45 4c 4c 28 70 42 74 29  ell>MX_CELL(pBt)
159b9 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 6f 20   ){.      /* To 
159ba 6d 61 6e 79 20 63 65 6c 6c 73 20 66 6f 72 20 61  many cells for a
159bb 20 73 69 6e 67 6c 65 20 70 61 67 65 2e 20 20 54   single page.  T
159bc 68 65 20 70 61 67 65 20 6d 75 73 74 20 62 65 20  he page must be 
159bd 63 6f 72 72 75 70 74 20 2a 2f 0a 20 20 20 20 20  corrupt */.     
159be 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
159bf 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
159c0 20 7d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28   }.    testcase(
159c1 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 4d   pPage->nCell==M
159c2 58 5f 43 45 4c 4c 28 70 42 74 29 20 29 3b 0a 0a  X_CELL(pBt) );..
159c3 20 20 20 20 2f 2a 20 41 20 6d 61 6c 66 6f 72 6d      /* A malform
159c4 65 64 20 64 61 74 61 62 61 73 65 20 70 61 67 65  ed database page
159c5 20 6d 69 67 68 74 20 63 61 75 73 65 20 75 73 20   might cause us 
159c6 74 6f 20 72 65 61 64 20 70 61 73 74 20 74 68 65  to read past the
159c7 20 65 6e 64 0a 20 20 20 20 2a 2a 20 6f 66 20 70   end.    ** of p
159c8 61 67 65 20 77 68 65 6e 20 70 61 72 73 69 6e 67  age when parsing
159c9 20 61 20 63 65 6c 6c 2e 20 20 0a 20 20 20 20 2a   a cell.  .    *
159ca 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 6f 6c  *.    ** The fol
159cb 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f 66 20  lowing block of 
159cc 63 6f 64 65 20 63 68 65 63 6b 73 20 65 61 72 6c  code checks earl
159cd 79 20 74 6f 20 73 65 65 20 69 66 20 61 20 63 65  y to see if a ce
159ce 6c 6c 20 65 78 74 65 6e 64 73 0a 20 20 20 20 2a  ll extends.    *
159cf 2a 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f  * past the end o
159d0 66 20 61 20 70 61 67 65 20 62 6f 75 6e 64 61 72  f a page boundar
159d1 79 20 61 6e 64 20 63 61 75 73 65 73 20 53 51 4c  y and causes SQL
159d2 49 54 45 5f 43 4f 52 52 55 50 54 20 74 6f 20 62  ITE_CORRUPT to b
159d3 65 20 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e  e .    ** return
159d4 65 64 20 69 66 20 69 74 20 64 6f 65 73 2e 0a 20  ed if it does.. 
159d5 20 20 20 2a 2f 0a 20 20 20 20 69 43 65 6c 6c 46     */.    iCellF
159d6 69 72 73 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65  irst = cellOffse
159d7 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65  t + 2*pPage->nCe
159d8 6c 6c 3b 0a 20 20 20 20 69 43 65 6c 6c 4c 61 73  ll;.    iCellLas
159d9 74 20 3d 20 75 73 61 62 6c 65 53 69 7a 65 20 2d  t = usableSize -
159da 20 34 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28   4;.#if defined(
159db 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56  SQLITE_ENABLE_OV
159dc 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43  ERSIZE_CELL_CHEC
159dd 4b 29 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69  K).    {.      i
159de 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
159df 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 74   /* Index into t
159e0 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20  he cell pointer 
159e1 61 72 72 61 79 20 2a 2f 0a 20 20 20 20 20 20 69  array */.      i
159e2 6e 74 20 73 7a 3b 20 20 20 20 20 20 20 20 20 20  nt sz;          
159e3 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 20 63 65   /* Size of a ce
159e4 6c 6c 20 2a 2f 0a 0a 20 20 20 20 20 20 69 66 28  ll */..      if(
159e5 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 20   !pPage->leaf ) 
159e6 69 43 65 6c 6c 4c 61 73 74 2d 2d 3b 0a 20 20 20  iCellLast--;.   
159e7 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50     for(i=0; i<pP
159e8 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29  age->nCell; i++)
159e9 7b 0a 20 20 20 20 20 20 20 20 70 63 20 3d 20 67  {.        pc = g
159ea 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 63 65  et2byte(&data[ce
159eb 6c 6c 4f 66 66 73 65 74 2b 69 2a 32 5d 29 3b 0a  llOffset+i*2]);.
159ec 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
159ed 28 20 70 63 3d 3d 69 43 65 6c 6c 46 69 72 73 74  ( pc==iCellFirst
159ee 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74   );.        test
159ef 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c 4c  case( pc==iCellL
159f0 61 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20 69  ast );.        i
159f1 66 28 20 70 63 3c 69 43 65 6c 6c 46 69 72 73 74  f( pc<iCellFirst
159f2 20 7c 7c 20 70 63 3e 69 43 65 6c 6c 4c 61 73 74   || pc>iCellLast
159f3 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
159f4 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
159f5 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20  UPT_BKPT;.      
159f6 20 20 7d 0a 20 20 20 20 20 20 20 20 73 7a 20 3d    }.        sz =
159f7 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61   cellSizePtr(pPa
159f8 67 65 2c 20 26 64 61 74 61 5b 70 63 5d 29 3b 0a  ge, &data[pc]);.
159f9 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
159fa 28 20 70 63 2b 73 7a 3d 3d 75 73 61 62 6c 65 53  ( pc+sz==usableS
159fb 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69  ize );.        i
159fc 66 28 20 70 63 2b 73 7a 3e 75 73 61 62 6c 65 53  f( pc+sz>usableS
159fd 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ize ){.         
159fe 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
159ff 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
15a00 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
15a01 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d       if( !pPage-
15a02 3e 6c 65 61 66 20 29 20 69 43 65 6c 6c 4c 61 73  >leaf ) iCellLas
15a03 74 2b 2b 3b 0a 20 20 20 20 7d 20 20 0a 23 65 6e  t++;.    }  .#en
15a04 64 69 66 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d 70  dif..    /* Comp
15a05 75 74 65 20 74 68 65 20 74 6f 74 61 6c 20 66 72  ute the total fr
15a06 65 65 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20  ee space on the 
15a07 70 61 67 65 20 2a 2f 0a 20 20 20 20 70 63 20 3d  page */.    pc =
15a08 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
15a09 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6e 46 72  hdr+1]);.    nFr
15a0a 65 65 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d  ee = data[hdr+7]
15a0b 20 2b 20 74 6f 70 3b 0a 20 20 20 20 77 68 69 6c   + top;.    whil
15a0c 65 28 20 70 63 3e 30 20 29 7b 0a 20 20 20 20 20  e( pc>0 ){.     
15a0d 20 75 31 36 20 6e 65 78 74 2c 20 73 69 7a 65 3b   u16 next, size;
15a0e 0a 20 20 20 20 20 20 69 66 28 20 70 63 3c 69 43  .      if( pc<iC
15a0f 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 3e 69  ellFirst || pc>i
15a10 43 65 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20  CellLast ){.    
15a11 20 20 20 20 2f 2a 20 53 74 61 72 74 20 6f 66 20      /* Start of 
15a12 66 72 65 65 20 62 6c 6f 63 6b 20 69 73 20 6f 66  free block is of
15a13 66 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20  f the page */.  
15a14 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
15a15 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
15a16 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  ; .      }.     
15a17 20 6e 65 78 74 20 3d 20 67 65 74 32 62 79 74 65   next = get2byte
15a18 28 26 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20  (&data[pc]);.   
15a19 20 20 20 73 69 7a 65 20 3d 20 67 65 74 32 62 79     size = get2by
15a1a 74 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b  te(&data[pc+2]);
15a1b 0a 20 20 20 20 20 20 69 66 28 20 28 6e 65 78 74  .      if( (next
15a1c 3e 30 20 26 26 20 6e 65 78 74 3c 3d 70 63 2b 73  >0 && next<=pc+s
15a1d 69 7a 65 2b 33 29 20 7c 7c 20 70 63 2b 73 69 7a  ize+3) || pc+siz
15a1e 65 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a  e>usableSize ){.
15a1f 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20          /* Free 
15a20 62 6c 6f 63 6b 73 20 6d 75 73 74 20 62 65 20 69  blocks must be i
15a21 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65  n ascending orde
15a22 72 2e 20 41 6e 64 20 74 68 65 20 6c 61 73 74 20  r. And the last 
15a23 62 79 74 65 20 6f 66 0a 09 2a 2a 20 74 68 65 20  byte of..** the 
15a24 66 72 65 65 2d 62 6c 6f 63 6b 20 6d 75 73 74 20  free-block must 
15a25 6c 69 65 20 6f 6e 20 74 68 65 20 64 61 74 61 62  lie on the datab
15a26 61 73 65 20 70 61 67 65 2e 20 20 2a 2f 0a 20 20  ase page.  */.  
15a27 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
15a28 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
15a29 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  ; .      }.     
15a2a 20 6e 46 72 65 65 20 3d 20 6e 46 72 65 65 20 2b   nFree = nFree +
15a2b 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 70 63 20   size;.      pc 
15a2c 3d 20 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 0a 20  = next;.    }.. 
15a2d 20 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f     /* At this po
15a2e 69 6e 74 2c 20 6e 46 72 65 65 20 63 6f 6e 74 61  int, nFree conta
15a2f 69 6e 73 20 74 68 65 20 73 75 6d 20 6f 66 20 74  ins the sum of t
15a30 68 65 20 6f 66 66 73 65 74 20 74 6f 20 74 68 65  he offset to the
15a31 20 73 74 61 72 74 0a 20 20 20 20 2a 2a 20 6f 66   start.    ** of
15a32 20 74 68 65 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e   the cell-conten
15a33 74 20 61 72 65 61 20 70 6c 75 73 20 74 68 65 20  t area plus the 
15a34 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 62  number of free b
15a35 79 74 65 73 20 77 69 74 68 69 6e 0a 20 20 20 20  ytes within.    
15a36 2a 2a 20 74 68 65 20 63 65 6c 6c 2d 63 6f 6e 74  ** the cell-cont
15a37 65 6e 74 20 61 72 65 61 2e 20 49 66 20 74 68 69  ent area. If thi
15a38 73 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  s is greater tha
15a39 6e 20 74 68 65 20 75 73 61 62 6c 65 2d 73 69 7a  n the usable-siz
15a3a 65 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20  e.    ** of the 
15a3b 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 70  page, then the p
15a3c 61 67 65 20 6d 75 73 74 20 62 65 20 63 6f 72 72  age must be corr
15a3d 75 70 74 65 64 2e 20 54 68 69 73 20 63 68 65 63  upted. This chec
15a3e 6b 20 61 6c 73 6f 0a 20 20 20 20 2a 2a 20 73 65  k also.    ** se
15a3f 72 76 65 73 20 74 6f 20 76 65 72 69 66 79 20 74  rves to verify t
15a40 68 61 74 20 74 68 65 20 6f 66 66 73 65 74 20 74  hat the offset t
15a41 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  o the start of t
15a42 68 65 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e 74 0a  he cell-content.
15a43 20 20 20 20 2a 2a 20 61 72 65 61 2c 20 61 63 63      ** area, acc
15a44 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 70 61  ording to the pa
15a45 67 65 20 68 65 61 64 65 72 2c 20 6c 69 65 73 20  ge header, lies 
15a46 77 69 74 68 69 6e 20 74 68 65 20 70 61 67 65 2e  within the page.
15a47 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
15a48 6e 46 72 65 65 3e 75 73 61 62 6c 65 53 69 7a 65  nFree>usableSize
15a49 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
15a4a 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
15a4b 42 4b 50 54 3b 20 0a 20 20 20 20 7d 0a 20 20 20  BKPT; .    }.   
15a4c 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20   pPage->nFree = 
15a4d 28 75 31 36 29 28 6e 46 72 65 65 20 2d 20 69 43  (u16)(nFree - iC
15a4e 65 6c 6c 46 69 72 73 74 29 3b 0a 20 20 20 20 70  ellFirst);.    p
15a4f 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31  Page->isInit = 1
15a50 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
15a51 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
15a52 2a 2a 20 53 65 74 20 75 70 20 61 20 72 61 77 20  ** Set up a raw 
15a53 70 61 67 65 20 73 6f 20 74 68 61 74 20 69 74 20  page so that it 
15a54 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 64 61 74  looks like a dat
15a55 61 62 61 73 65 20 70 61 67 65 20 68 6f 6c 64 69  abase page holdi
15a56 6e 67 0a 2a 2a 20 6e 6f 20 65 6e 74 72 69 65 73  ng.** no entries
15a57 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
15a58 20 7a 65 72 6f 50 61 67 65 28 4d 65 6d 50 61 67   zeroPage(MemPag
15a59 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 66 6c  e *pPage, int fl
15a5a 61 67 73 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  ags){.  unsigned
15a5b 20 63 68 61 72 20 2a 64 61 74 61 20 3d 20 70 50   char *data = pP
15a5c 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 42 74  age->aData;.  Bt
15a5d 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50  Shared *pBt = pP
15a5e 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75 38 20 68  age->pBt;.  u8 h
15a5f 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f  dr = pPage->hdrO
15a60 66 66 73 65 74 3b 0a 20 20 75 31 36 20 66 69 72  ffset;.  u16 fir
15a61 73 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  st;..  assert( s
15a62 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 6e  qlite3PagerPagen
15a63 75 6d 62 65 72 28 70 50 61 67 65 2d 3e 70 44 62  umber(pPage->pDb
15a64 50 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e 70 67  Page)==pPage->pg
15a65 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  no );.  assert( 
15a66 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45  sqlite3PagerGetE
15a67 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62 50  xtra(pPage->pDbP
15a68 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70  age) == (void*)p
15a69 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74  Page );.  assert
15a6a 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  ( sqlite3PagerGe
15a6b 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44 62  tData(pPage->pDb
15a6c 50 61 67 65 29 20 3d 3d 20 64 61 74 61 20 29 3b  Page) == data );
15a6d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
15a6e 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
15a6f 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  le(pPage->pDbPag
15a70 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  e) );.  assert( 
15a71 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
15a72 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  ld(pBt->mutex) )
15a73 3b 0a 20 20 2f 2a 6d 65 6d 73 65 74 28 26 64 61  ;.  /*memset(&da
15a74 74 61 5b 68 64 72 5d 2c 20 30 2c 20 70 42 74 2d  ta[hdr], 0, pBt-
15a75 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 68 64  >usableSize - hd
15a76 72 29 3b 2a 2f 0a 20 20 64 61 74 61 5b 68 64 72  r);*/.  data[hdr
15a77 5d 20 3d 20 28 63 68 61 72 29 66 6c 61 67 73 3b  ] = (char)flags;
15a78 0a 20 20 66 69 72 73 74 20 3d 20 68 64 72 20 2b  .  first = hdr +
15a79 20 38 20 2b 20 34 2a 28 28 66 6c 61 67 73 26 50   8 + 4*((flags&P
15a7a 54 46 5f 4c 45 41 46 29 3d 3d 30 20 3f 31 3a 30  TF_LEAF)==0 ?1:0
15a7b 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74  );.  memset(&dat
15a7c 61 5b 68 64 72 2b 31 5d 2c 20 30 2c 20 34 29 3b  a[hdr+1], 0, 4);
15a7d 0a 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d  .  data[hdr+7] =
15a7e 20 30 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26   0;.  put2byte(&
15a7f 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 70 42 74  data[hdr+5], pBt
15a80 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20  ->usableSize);. 
15a81 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20   pPage->nFree = 
15a82 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
15a83 2d 20 66 69 72 73 74 3b 0a 20 20 64 65 63 6f 64  - first;.  decod
15a84 65 46 6c 61 67 73 28 70 50 61 67 65 2c 20 66 6c  eFlags(pPage, fl
15a85 61 67 73 29 3b 0a 20 20 70 50 61 67 65 2d 3e 68  ags);.  pPage->h
15a86 64 72 4f 66 66 73 65 74 20 3d 20 68 64 72 3b 0a  drOffset = hdr;.
15a87 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66    pPage->cellOff
15a88 73 65 74 20 3d 20 66 69 72 73 74 3b 0a 20 20 70  set = first;.  p
15a89 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20  Page->nOverflow 
15a8a 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70  = 0;.  assert( p
15a8b 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35 31  Bt->pageSize>=51
15a8c 32 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53 69  2 && pBt->pageSi
15a8d 7a 65 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20 70  ze<=32768 );.  p
15a8e 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 3d  Page->maskPage =
15a8f 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d   pBt->pageSize -
15a90 20 31 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65   1;.  pPage->nCe
15a91 6c 6c 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 2d  ll = 0;.  pPage-
15a92 3e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 7d 0a 0a  >isInit = 1;.}..
15a93 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61  ./*.** Convert a
15a94 20 44 62 50 61 67 65 20 6f 62 74 61 69 6e 65 64   DbPage obtained
15a95 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20   from the pager 
15a96 69 6e 74 6f 20 61 20 4d 65 6d 50 61 67 65 20 75  into a MemPage u
15a97 73 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 62 74  sed by.** the bt
15a98 72 65 65 20 6c 61 79 65 72 2e 0a 2a 2f 0a 73 74  ree layer..*/.st
15a99 61 74 69 63 20 4d 65 6d 50 61 67 65 20 2a 62 74  atic MemPage *bt
15a9a 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67  reePageFromDbPag
15a9b 65 28 44 62 50 61 67 65 20 2a 70 44 62 50 61 67  e(DbPage *pDbPag
15a9c 65 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 42 74  e, Pgno pgno, Bt
15a9d 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20  Shared *pBt){.  
15a9e 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d  MemPage *pPage =
15a9f 20 28 4d 65 6d 50 61 67 65 2a 29 73 71 6c 69 74   (MemPage*)sqlit
15aa0 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28  e3PagerGetExtra(
15aa1 70 44 62 50 61 67 65 29 3b 0a 20 20 70 50 61 67  pDbPage);.  pPag
15aa2 65 2d 3e 61 44 61 74 61 20 3d 20 73 71 6c 69 74  e->aData = sqlit
15aa3 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70  e3PagerGetData(p
15aa4 44 62 50 61 67 65 29 3b 0a 20 20 70 50 61 67 65  DbPage);.  pPage
15aa5 2d 3e 70 44 62 50 61 67 65 20 3d 20 70 44 62 50  ->pDbPage = pDbP
15aa6 61 67 65 3b 0a 20 20 70 50 61 67 65 2d 3e 70 42  age;.  pPage->pB
15aa7 74 20 3d 20 70 42 74 3b 0a 20 20 70 50 61 67 65  t = pBt;.  pPage
15aa8 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20  ->pgno = pgno;. 
15aa9 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65   pPage->hdrOffse
15aaa 74 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3d  t = pPage->pgno=
15aab 3d 31 20 3f 20 31 30 30 20 3a 20 30 3b 0a 20 20  =1 ? 100 : 0;.  
15aac 72 65 74 75 72 6e 20 70 50 61 67 65 3b 20 0a 7d  return pPage; .}
15aad 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70 61  ../*.** Get a pa
15aae 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65  ge from the page
15aaf 72 2e 20 20 49 6e 69 74 69 61 6c 69 7a 65 20 74  r.  Initialize t
15ab0 68 65 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 61  he MemPage.pBt a
15ab1 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 44  nd.** MemPage.aD
15ab2 61 74 61 20 65 6c 65 6d 65 6e 74 73 20 69 66 20  ata elements if 
15ab3 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  needed..**.** If
15ab4 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e 74 20 66   the noContent f
15ab5 6c 61 67 20 69 73 20 73 65 74 2c 20 69 74 20 6d  lag is set, it m
15ab6 65 61 6e 73 20 74 68 61 74 20 77 65 20 64 6f 20  eans that we do 
15ab7 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74 0a 2a  not care about.*
15ab8 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  * the content of
15ab9 20 74 68 65 20 70 61 67 65 20 61 74 20 74 68 69   the page at thi
15aba 73 20 74 69 6d 65 2e 20 20 53 6f 20 64 6f 20 6e  s time.  So do n
15abb 6f 74 20 67 6f 20 74 6f 20 74 68 65 20 64 69 73  ot go to the dis
15abc 6b 0a 2a 2a 20 74 6f 20 66 65 74 63 68 20 74 68  k.** to fetch th
15abd 65 20 63 6f 6e 74 65 6e 74 2e 20 20 4a 75 73 74  e content.  Just
15abe 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 63 6f 6e   fill in the con
15abf 74 65 6e 74 20 77 69 74 68 20 7a 65 72 6f 73 20  tent with zeros 
15ac0 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20 49 66 20 69  for now..** If i
15ac1 6e 20 74 68 65 20 66 75 74 75 72 65 20 77 65 20  n the future we 
15ac2 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65  call sqlite3Page
15ac3 72 57 72 69 74 65 28 29 20 6f 6e 20 74 68 69 73  rWrite() on this
15ac4 20 70 61 67 65 2c 20 74 68 61 74 0a 2a 2a 20 6d   page, that.** m
15ac5 65 61 6e 73 20 77 65 20 68 61 76 65 20 73 74 61  eans we have sta
15ac6 72 74 65 64 20 74 6f 20 62 65 20 63 6f 6e 63 65  rted to be conce
15ac7 72 6e 65 64 20 61 62 6f 75 74 20 63 6f 6e 74 65  rned about conte
15ac8 6e 74 20 61 6e 64 20 74 68 65 20 64 69 73 6b 0a  nt and the disk.
15ac9 2a 2a 20 72 65 61 64 20 73 68 6f 75 6c 64 20 6f  ** read should o
15aca 63 63 75 72 20 61 74 20 74 68 61 74 20 70 6f 69  ccur at that poi
15acb 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  nt..*/.static in
15acc 74 20 62 74 72 65 65 47 65 74 50 61 67 65 28 0a  t btreeGetPage(.
15acd 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c    BtShared *pBt,
15ace 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74         /* The bt
15acf 72 65 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67  ree */.  Pgno pg
15ad0 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  no,           /*
15ad1 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70   Number of the p
15ad2 61 67 65 20 74 6f 20 66 65 74 63 68 20 2a 2f 0a  age to fetch */.
15ad3 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61    MemPage **ppPa
15ad4 67 65 2c 20 20 20 20 2f 2a 20 52 65 74 75 72 6e  ge,    /* Return
15ad5 20 74 68 65 20 70 61 67 65 20 69 6e 20 74 68 69   the page in thi
15ad6 73 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20  s parameter */. 
15ad7 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 20 20   int noContent  
15ad8 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20        /* Do not 
15ad9 6c 6f 61 64 20 70 61 67 65 20 63 6f 6e 74 65 6e  load page conten
15ada 74 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a  t if true */.){.
15adb 20 20 69 6e 74 20 72 63 3b 0a 20 20 44 62 50 61    int rc;.  DbPa
15adc 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 0a 20 20  ge *pDbPage;..  
15add 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
15ade 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
15adf 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d  mutex) );.  rc =
15ae0 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71   sqlite3PagerAcq
15ae1 75 69 72 65 28 70 42 74 2d 3e 70 50 61 67 65 72  uire(pBt->pPager
15ae2 2c 20 70 67 6e 6f 2c 20 28 44 62 50 61 67 65 2a  , pgno, (DbPage*
15ae3 2a 29 26 70 44 62 50 61 67 65 2c 20 6e 6f 43 6f  *)&pDbPage, noCo
15ae4 6e 74 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 63  ntent);.  if( rc
15ae5 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
15ae6 2a 70 70 50 61 67 65 20 3d 20 62 74 72 65 65 50  *ppPage = btreeP
15ae7 61 67 65 46 72 6f 6d 44 62 50 61 67 65 28 70 44  ageFromDbPage(pD
15ae8 62 50 61 67 65 2c 20 70 67 6e 6f 2c 20 70 42 74  bPage, pgno, pBt
15ae9 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
15aea 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
15aeb 52 65 74 72 69 65 76 65 20 61 20 70 61 67 65 20  Retrieve a page 
15aec 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 63  from the pager c
15aed 61 63 68 65 2e 20 49 66 20 74 68 65 20 72 65 71  ache. If the req
15aee 75 65 73 74 65 64 20 70 61 67 65 20 69 73 20 6e  uested page is n
15aef 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20 69 6e  ot.** already in
15af0 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65   the pager cache
15af1 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 49 6e   return NULL. In
15af2 69 74 69 61 6c 69 7a 65 20 74 68 65 20 4d 65 6d  itialize the Mem
15af3 50 61 67 65 2e 70 42 74 20 61 6e 64 0a 2a 2a 20  Page.pBt and.** 
15af4 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 20 65 6c  MemPage.aData el
15af5 65 6d 65 6e 74 73 20 69 66 20 6e 65 65 64 65 64  ements if needed
15af6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50  ..*/.static MemP
15af7 61 67 65 20 2a 62 74 72 65 65 50 61 67 65 4c 6f  age *btreePageLo
15af8 6f 6b 75 70 28 42 74 53 68 61 72 65 64 20 2a 70  okup(BtShared *p
15af9 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a  Bt, Pgno pgno){.
15afa 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67    DbPage *pDbPag
15afb 65 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  e;.  assert( sql
15afc 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
15afd 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
15afe 20 70 44 62 50 61 67 65 20 3d 20 73 71 6c 69 74   pDbPage = sqlit
15aff 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 70 42  e3PagerLookup(pB
15b00 74 2d 3e 70 50 61 67 65 72 2c 20 70 67 6e 6f 29  t->pPager, pgno)
15b01 3b 0a 20 20 69 66 28 20 70 44 62 50 61 67 65 20  ;.  if( pDbPage 
15b02 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 62 74  ){.    return bt
15b03 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67  reePageFromDbPag
15b04 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c  e(pDbPage, pgno,
15b05 20 70 42 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74   pBt);.  }.  ret
15b06 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
15b07 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20  Return the size 
15b08 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
15b09 66 69 6c 65 20 69 6e 20 70 61 67 65 73 2e 20 49  file in pages. I
15b0a 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 20 6b  f there is any k
15b0b 69 6e 64 20 6f 66 0a 2a 2a 20 65 72 72 6f 72 2c  ind of.** error,
15b0c 20 72 65 74 75 72 6e 20 28 28 75 6e 73 69 67 6e   return ((unsign
15b0d 65 64 20 69 6e 74 29 2d 31 29 2e 0a 2a 2f 0a 73  ed int)-1)..*/.s
15b0e 74 61 74 69 63 20 50 67 6e 6f 20 70 61 67 65 72  tatic Pgno pager
15b0f 50 61 67 65 63 6f 75 6e 74 28 42 74 53 68 61 72  Pagecount(BtShar
15b10 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20  ed *pBt){.  int 
15b11 6e 50 61 67 65 20 3d 20 2d 31 3b 0a 20 20 69 6e  nPage = -1;.  in
15b12 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  t rc;.  assert( 
15b13 70 42 74 2d 3e 70 50 61 67 65 31 20 29 3b 0a 20  pBt->pPage1 );. 
15b14 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
15b15 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d  erPagecount(pBt-
15b16 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29  >pPager, &nPage)
15b17 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  ;.  assert( rc==
15b18 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6e 50 61  SQLITE_OK || nPa
15b19 67 65 3d 3d 2d 31 20 29 3b 0a 20 20 72 65 74 75  ge==-1 );.  retu
15b1a 72 6e 20 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0a  rn (Pgno)nPage;.
15b1b 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70  }../*.** Get a p
15b1c 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67  age from the pag
15b1d 65 72 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a  er and initializ
15b1e 65 20 69 74 2e 20 20 54 68 69 73 20 72 6f 75 74  e it.  This rout
15b1f 69 6e 65 20 69 73 20 6a 75 73 74 20 61 0a 2a 2a  ine is just a.**
15b20 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61   convenience wra
15b21 70 70 65 72 20 61 72 6f 75 6e 64 20 73 65 70 61  pper around sepa
15b22 72 61 74 65 20 63 61 6c 6c 73 20 74 6f 20 62 74  rate calls to bt
15b23 72 65 65 47 65 74 50 61 67 65 28 29 20 61 6e 64  reeGetPage() and
15b24 20 0a 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 61   .** btreeInitPa
15b25 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  ge()..**.** If a
15b26 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
15b27 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 2a  then the value *
15b28 70 70 50 61 67 65 20 69 73 20 73 65 74 20 74 6f  ppPage is set to
15b29 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 49   is undefined. I
15b2a 74 0a 2a 2a 20 6d 61 79 20 72 65 6d 61 69 6e 20  t.** may remain 
15b2b 75 6e 63 68 61 6e 67 65 64 2c 20 6f 72 20 69 74  unchanged, or it
15b2c 20 6d 61 79 20 62 65 20 73 65 74 20 74 6f 20 61   may be set to a
15b2d 6e 20 69 6e 76 61 6c 69 64 20 76 61 6c 75 65 2e  n invalid value.
15b2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67  .*/.static int g
15b2f 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 0a 20  etAndInitPage(. 
15b30 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20   BtShared *pBt, 
15b31 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
15b32 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
15b33 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20  .  Pgno pgno,   
15b34 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
15b35 72 20 6f 66 20 74 68 65 20 70 61 67 65 20 74 6f  r of the page to
15b36 20 67 65 74 20 2a 2f 0a 20 20 4d 65 6d 50 61 67   get */.  MemPag
15b37 65 20 2a 2a 70 70 50 61 67 65 20 20 20 20 20 2f  e **ppPage     /
15b38 2a 20 57 72 69 74 65 20 74 68 65 20 70 61 67 65  * Write the page
15b39 20 70 6f 69 6e 74 65 72 20 68 65 72 65 20 2a 2f   pointer here */
15b3a 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
15b3b 54 45 53 54 4f 4e 4c 59 28 20 50 67 6e 6f 20 69  TESTONLY( Pgno i
15b3c 4c 61 73 74 50 67 20 3d 20 70 61 67 65 72 50 61  LastPg = pagerPa
15b3d 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 20 29 0a  gecount(pBt); ).
15b3e 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
15b3f 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
15b40 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 72  ->mutex) );..  r
15b41 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65  c = btreeGetPage
15b42 28 70 42 74 2c 20 70 67 6e 6f 2c 20 70 70 50 61  (pBt, pgno, ppPa
15b43 67 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63  ge, 0);.  if( rc
15b44 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
15b45 20 20 20 72 63 20 3d 20 62 74 72 65 65 49 6e 69     rc = btreeIni
15b46 74 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a  tPage(*ppPage);.
15b47 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
15b48 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
15b49 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 50 61  eleasePage(*ppPa
15b4a 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ge);.    }.  }..
15b4b 20 20 2f 2a 20 49 66 20 74 68 65 20 72 65 71 75    /* If the requ
15b4c 65 73 74 65 64 20 70 61 67 65 20 6e 75 6d 62 65  ested page numbe
15b4d 72 20 77 61 73 20 65 69 74 68 65 72 20 30 20 6f  r was either 0 o
15b4e 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74  r greater than t
15b4f 68 65 20 70 61 67 65 0a 20 20 2a 2a 20 6e 75 6d  he page.  ** num
15b50 62 65 72 20 6f 66 20 74 68 65 20 6c 61 73 74 20  ber of the last 
15b51 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61  page in the data
15b52 62 61 73 65 2c 20 74 68 69 73 20 66 75 6e 63 74  base, this funct
15b53 69 6f 6e 20 73 68 6f 75 6c 64 20 72 65 74 75 72  ion should retur
15b54 6e 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 43 4f  n.  ** SQLITE_CO
15b55 52 52 55 50 54 20 6f 72 20 73 6f 6d 65 20 6f 74  RRUPT or some ot
15b56 68 65 72 20 65 72 72 6f 72 20 28 69 2e 65 2e 20  her error (i.e. 
15b57 53 51 4c 49 54 45 5f 46 55 4c 4c 29 2e 20 43 68  SQLITE_FULL). Ch
15b58 65 63 6b 20 74 68 61 74 20 74 68 69 73 0a 20 20  eck that this.  
15b59 2a 2a 20 69 73 20 74 68 65 20 63 61 73 65 2e 20  ** is the case. 
15b5a 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70   */.  assert( (p
15b5b 67 6e 6f 3e 30 20 26 26 20 70 67 6e 6f 3c 3d 69  gno>0 && pgno<=i
15b5c 4c 61 73 74 50 67 29 20 7c 7c 20 72 63 21 3d 53  LastPg) || rc!=S
15b5d 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 74 65  QLITE_OK );.  te
15b5e 73 74 63 61 73 65 28 20 70 67 6e 6f 3d 3d 30 20  stcase( pgno==0 
15b5f 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70  );.  testcase( p
15b60 67 6e 6f 3d 3d 69 4c 61 73 74 50 67 20 29 3b 0a  gno==iLastPg );.
15b61 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
15b62 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61  ./*.** Release a
15b63 20 4d 65 6d 50 61 67 65 2e 20 20 54 68 69 73 20   MemPage.  This 
15b64 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64  should be called
15b65 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 70   once for each p
15b66 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20  rior.** call to 
15b67 62 74 72 65 65 47 65 74 50 61 67 65 2e 0a 2a 2f  btreeGetPage..*/
15b68 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c  .static void rel
15b69 65 61 73 65 50 61 67 65 28 4d 65 6d 50 61 67 65  easePage(MemPage
15b6a 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 66 28 20   *pPage){.  if( 
15b6b 70 50 61 67 65 20 29 7b 0a 20 20 20 20 61 73 73  pPage ){.    ass
15b6c 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65  ert( pPage->nOve
15b6d 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 73 71 6c 69  rflow==0 || sqli
15b6e 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63  te3PagerPageRefc
15b6f 6f 75 6e 74 28 70 50 61 67 65 2d 3e 70 44 62 50  ount(pPage->pDbP
15b70 61 67 65 29 3e 31 20 29 3b 0a 20 20 20 20 61 73  age)>1 );.    as
15b71 73 65 72 74 28 20 70 50 61 67 65 2d 3e 61 44 61  sert( pPage->aDa
15b72 74 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ta );.    assert
15b73 28 20 70 50 61 67 65 2d 3e 70 42 74 20 29 3b 0a  ( pPage->pBt );.
15b74 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
15b75 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61  te3PagerGetExtra
15b76 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
15b77 20 3d 3d 20 28 76 6f 69 64 2a 29 70 50 61 67 65   == (void*)pPage
15b78 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
15b79 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44  sqlite3PagerGetD
15b7a 61 74 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ata(pPage->pDbPa
15b7b 67 65 29 3d 3d 70 50 61 67 65 2d 3e 61 44 61 74  ge)==pPage->aDat
15b7c 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  a );.    assert(
15b7d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
15b7e 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
15b7f 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 73 71  mutex) );.    sq
15b80 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
15b81 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b  pPage->pDbPage);
15b82 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 75  .  }.}../*.** Du
15b83 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c  ring a rollback,
15b84 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72 20   when the pager 
15b85 72 65 6c 6f 61 64 73 20 69 6e 66 6f 72 6d 61 74  reloads informat
15b86 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 63 61 63  ion into the cac
15b87 68 65 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74 68  he.** so that th
15b88 65 20 63 61 63 68 65 20 69 73 20 72 65 73 74 6f  e cache is resto
15b89 72 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67 69  red to its origi
15b8a 6e 61 6c 20 73 74 61 74 65 20 61 74 20 74 68 65  nal state at the
15b8b 20 73 74 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65   start of.** the
15b8c 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 66 6f   transaction, fo
15b8d 72 20 65 61 63 68 20 70 61 67 65 20 72 65 73 74  r each page rest
15b8e 6f 72 65 64 20 74 68 69 73 20 72 6f 75 74 69 6e  ored this routin
15b8f 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a  e is called..**.
15b90 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
15b91 6e 65 65 64 73 20 74 6f 20 72 65 73 65 74 20 74  needs to reset t
15b92 68 65 20 65 78 74 72 61 20 64 61 74 61 20 73 65  he extra data se
15b93 63 74 69 6f 6e 20 61 74 20 74 68 65 20 65 6e 64  ction at the end
15b94 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 20   of the.** page 
15b95 74 6f 20 61 67 72 65 65 20 77 69 74 68 20 74 68  to agree with th
15b96 65 20 72 65 73 74 6f 72 65 64 20 64 61 74 61 2e  e restored data.
15b97 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
15b98 70 61 67 65 52 65 69 6e 69 74 28 44 62 50 61 67  pageReinit(DbPag
15b99 65 20 2a 70 44 61 74 61 29 7b 0a 20 20 4d 65 6d  e *pData){.  Mem
15b9a 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 70  Page *pPage;.  p
15b9b 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 20  Page = (MemPage 
15b9c 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  *)sqlite3PagerGe
15b9d 74 45 78 74 72 61 28 70 44 61 74 61 29 3b 0a 20  tExtra(pData);. 
15b9e 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
15b9f 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e  PagerPageRefcoun
15ba0 74 28 70 44 61 74 61 29 3e 30 20 29 3b 0a 20 20  t(pData)>0 );.  
15ba1 69 66 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69  if( pPage->isIni
15ba2 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  t ){.    assert(
15ba3 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
15ba4 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
15ba5 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 70 50  mutex) );.    pP
15ba6 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b  age->isInit = 0;
15ba7 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
15ba8 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e  PagerPageRefcoun
15ba9 74 28 70 44 61 74 61 29 3e 31 20 29 7b 0a 20 20  t(pData)>1 ){.  
15baa 20 20 20 20 2f 2a 20 70 50 61 67 65 20 6d 69 67      /* pPage mig
15bab 68 74 20 6e 6f 74 20 62 65 20 61 20 62 74 72 65  ht not be a btre
15bac 65 20 70 61 67 65 3b 20 20 69 74 20 6d 69 67 68  e page;  it migh
15bad 74 20 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77  t be an overflow
15bae 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 6f   page.      ** o
15baf 72 20 70 74 72 6d 61 70 20 70 61 67 65 20 6f 72  r ptrmap page or
15bb0 20 61 20 66 72 65 65 20 70 61 67 65 2e 20 20 49   a free page.  I
15bb1 6e 20 74 68 6f 73 65 20 63 61 73 65 73 2c 20 74  n those cases, t
15bb2 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20  he following.   
15bb3 20 20 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 62 74     ** call to bt
15bb4 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 77 69  reeInitPage() wi
15bb5 6c 6c 20 6c 69 6b 65 6c 79 20 72 65 74 75 72 6e  ll likely return
15bb6 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e   SQLITE_CORRUPT.
15bb7 0a 20 20 20 20 20 20 2a 2a 20 42 75 74 20 6e 6f  .      ** But no
15bb8 20 68 61 72 6d 20 69 73 20 64 6f 6e 65 20 62 79   harm is done by
15bb9 20 74 68 69 73 2e 20 20 41 6e 64 20 69 74 20 69   this.  And it i
15bba 73 20 76 65 72 79 20 69 6d 70 6f 72 74 61 6e 74  s very important
15bbb 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 62   that.      ** b
15bbc 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 62  treeInitPage() b
15bbd 65 20 63 61 6c 6c 65 64 20 6f 6e 20 65 76 65 72  e called on ever
15bbe 79 20 62 74 72 65 65 20 70 61 67 65 20 73 6f 20  y btree page so 
15bbf 77 65 20 6d 61 6b 65 0a 20 20 20 20 20 20 2a 2a  we make.      **
15bc0 20 74 68 65 20 63 61 6c 6c 20 66 6f 72 20 65 76   the call for ev
15bc1 65 72 79 20 70 61 67 65 20 74 68 61 74 20 63 6f  ery page that co
15bc2 6d 65 73 20 69 6e 20 66 6f 72 20 72 65 2d 69 6e  mes in for re-in
15bc3 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 20 20  iting. */.      
15bc4 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 50  btreeInitPage(pP
15bc5 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  age);.    }.  }.
15bc6 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20  }../*.** Invoke 
15bc7 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72  the busy handler
15bc8 20 66 6f 72 20 61 20 62 74 72 65 65 2e 0a 2a 2f   for a btree..*/
15bc9 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65  .static int btre
15bca 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c  eInvokeBusyHandl
15bcb 65 72 28 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a  er(void *pArg){.
15bcc 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
15bcd 3d 20 28 42 74 53 68 61 72 65 64 2a 29 70 41 72  = (BtShared*)pAr
15bce 67 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  g;.  assert( pBt
15bcf 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74  ->db );.  assert
15bd0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
15bd1 68 65 6c 64 28 70 42 74 2d 3e 64 62 2d 3e 6d 75  held(pBt->db->mu
15bd2 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  tex) );.  return
15bd3 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75   sqlite3InvokeBu
15bd4 73 79 48 61 6e 64 6c 65 72 28 26 70 42 74 2d 3e  syHandler(&pBt->
15bd5 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 29  db->busyHandler)
15bd6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20  ;.}../*.** Open 
15bd7 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  a database file.
15bd8 0a 2a 2a 20 0a 2a 2a 20 7a 46 69 6c 65 6e 61 6d  .** .** zFilenam
15bd9 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  e is the name of
15bda 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
15bdb 6c 65 2e 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d  le.  If zFilenam
15bdc 65 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 61 20 6e  e is NULL.** a n
15bdd 65 77 20 64 61 74 61 62 61 73 65 20 77 69 74 68  ew database with
15bde 20 61 20 72 61 6e 64 6f 6d 20 6e 61 6d 65 20 69   a random name i
15bdf 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 69 73  s created.  This
15be0 20 72 61 6e 64 6f 6d 6c 79 20 6e 61 6d 65 64 0a   randomly named.
15be1 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
15be2 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 74 65 64   will be deleted
15be3 20 77 68 65 6e 20 73 71 6c 69 74 65 33 42 74 72   when sqlite3Btr
15be4 65 65 43 6c 6f 73 65 28 29 20 69 73 20 63 61 6c  eeClose() is cal
15be5 6c 65 64 2e 0a 2a 2a 20 49 66 20 7a 46 69 6c 65  led..** If zFile
15be6 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79  name is ":memory
15be7 3a 22 20 74 68 65 6e 20 61 6e 20 69 6e 2d 6d 65  :" then an in-me
15be8 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 69 73  mory database is
15be9 20 63 72 65 61 74 65 64 0a 2a 2a 20 74 68 61 74   created.** that
15bea 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   is automaticall
15beb 79 20 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e  y destroyed when
15bec 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a   it is closed..*
15bed 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61  *.** If the data
15bee 62 61 73 65 20 69 73 20 61 6c 72 65 61 64 79 20  base is already 
15bef 6f 70 65 6e 65 64 20 69 6e 20 74 68 65 20 73 61  opened in the sa
15bf0 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
15bf1 65 63 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 77 65  ection.** and we
15bf2 20 61 72 65 20 69 6e 20 73 68 61 72 65 64 20 63   are in shared c
15bf3 61 63 68 65 20 6d 6f 64 65 2c 20 74 68 65 6e 20  ache mode, then 
15bf4 74 68 65 20 6f 70 65 6e 20 77 69 6c 6c 20 66 61  the open will fa
15bf5 69 6c 20 77 69 74 68 20 61 6e 0a 2a 2a 20 53 51  il with an.** SQ
15bf6 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20  LITE_CONSTRAINT 
15bf7 65 72 72 6f 72 2e 20 20 57 65 20 63 61 6e 6e 6f  error.  We canno
15bf8 74 20 61 6c 6c 6f 77 20 74 77 6f 20 6f 72 20 6d  t allow two or m
15bf9 6f 72 65 20 42 74 53 68 61 72 65 64 0a 2a 2a 20  ore BtShared.** 
15bfa 6f 62 6a 65 63 74 73 20 69 6e 20 74 68 65 20 73  objects in the s
15bfb 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ame database con
15bfc 6e 65 63 74 69 6f 6e 20 73 69 6e 63 65 20 64 6f  nection since do
15bfd 69 6e 67 20 73 6f 20 77 69 6c 6c 20 6c 65 61 64  ing so will lead
15bfe 0a 2a 2a 20 74 6f 20 70 72 6f 62 6c 65 6d 73 20  .** to problems 
15bff 77 69 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f  with locking..*/
15c00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
15c01 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
15c02 4f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68  Open(.  const ch
15c03 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20  ar *zFilename,  
15c04 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66  /* Name of the f
15c05 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ile containing t
15c06 68 65 20 42 54 72 65 65 20 64 61 74 61 62 61 73  he BTree databas
15c07 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  e */.  sqlite3 *
15c08 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  db,            /
15c09 2a 20 41 73 73 6f 63 69 61 74 65 64 20 64 61 74  * Associated dat
15c0a 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  abase handle */.
15c0b 20 20 42 74 72 65 65 20 2a 2a 70 70 42 74 72 65    Btree **ppBtre
15c0c 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69  e,        /* Poi
15c0d 6e 74 65 72 20 74 6f 20 6e 65 77 20 42 74 72 65  nter to new Btre
15c0e 65 20 6f 62 6a 65 63 74 20 77 72 69 74 74 65 6e  e object written
15c0f 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66   here */.  int f
15c10 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20  lags,           
15c11 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 73 20 2a 2f     /* Options */
15c12 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20  .  int vfsFlags 
15c13 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c             /* Fl
15c14 61 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75  ags passed throu
15c15 67 68 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 66  gh to sqlite3_vf
15c16 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 29 7b 0a  s.xOpen() */.){.
15c17 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
15c18 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  Vfs;            
15c19 20 2f 2a 20 54 68 65 20 56 46 53 20 74 6f 20 75   /* The VFS to u
15c1a 73 65 20 66 6f 72 20 74 68 69 73 20 62 74 72 65  se for this btre
15c1b 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20  e */.  BtShared 
15c1c 2a 70 42 74 20 3d 20 30 3b 20 20 20 20 20 20 20  *pBt = 0;       
15c1d 20 20 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20        /* Shared 
15c1e 70 61 72 74 20 6f 66 20 62 74 72 65 65 20 73 74  part of btree st
15c1f 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 42 74 72  ructure */.  Btr
15c20 65 65 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20  ee *p;          
15c21 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
15c22 61 6e 64 6c 65 20 74 6f 20 72 65 74 75 72 6e 20  andle to return 
15c23 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  */.  sqlite3_mut
15c24 65 78 20 2a 6d 75 74 65 78 4f 70 65 6e 20 3d 20  ex *mutexOpen = 
15c25 30 3b 20 20 2f 2a 20 50 72 65 76 65 6e 74 73 20  0;  /* Prevents 
15c26 61 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e  a race condition
15c27 2e 20 54 69 63 6b 65 74 20 23 33 35 33 37 20 2a  . Ticket #3537 *
15c28 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  /.  int rc = SQL
15c29 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20  ITE_OK;         
15c2a 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64     /* Result cod
15c2b 65 20 66 72 6f 6d 20 74 68 69 73 20 66 75 6e 63  e from this func
15c2c 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 6e 52 65  tion */.  u8 nRe
15c2d 73 65 72 76 65 3b 20 20 20 20 20 20 20 20 20 20  serve;          
15c2e 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65           /* Byte
15c2f 20 6f 66 20 75 6e 75 73 65 64 20 73 70 61 63 65   of unused space
15c30 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f   on each page */
15c31 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
15c32 20 7a 44 62 48 65 61 64 65 72 5b 31 30 30 5d 3b   zDbHeader[100];
15c33 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 65    /* Database he
15c34 61 64 65 72 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a  ader content */.
15c35 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 76 61  .  /* Set the va
15c36 72 69 61 62 6c 65 20 69 73 4d 65 6d 64 62 20 74  riable isMemdb t
15c37 6f 20 74 72 75 65 20 66 6f 72 20 61 6e 20 69 6e  o true for an in
15c38 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
15c39 2c 20 6f 72 20 0a 20 20 2a 2a 20 66 61 6c 73 65  , or .  ** false
15c3a 20 66 6f 72 20 61 20 66 69 6c 65 2d 62 61 73 65   for a file-base
15c3b 64 20 64 61 74 61 62 61 73 65 2e 20 54 68 69 73  d database. This
15c3c 20 73 79 6d 62 6f 6c 20 69 73 20 6f 6e 6c 79 20   symbol is only 
15c3d 72 65 71 75 69 72 65 64 20 69 66 0a 20 20 2a 2a  required if.  **
15c3e 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 73   either of the s
15c3f 68 61 72 65 64 2d 64 61 74 61 20 6f 72 20 61 75  hared-data or au
15c40 74 6f 76 61 63 75 75 6d 20 66 65 61 74 75 72 65  tovacuum feature
15c41 73 20 61 72 65 20 63 6f 6d 70 69 6c 65 64 20 0a  s are compiled .
15c42 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6c 69    ** into the li
15c43 62 72 61 72 79 2e 0a 20 20 2a 2f 0a 23 69 66 20  brary..  */.#if 
15c44 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
15c45 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
15c46 45 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53  E) || !defined(S
15c47 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
15c48 41 43 55 55 4d 29 0a 20 20 23 69 66 64 65 66 20  ACUUM).  #ifdef 
15c49 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f  SQLITE_OMIT_MEMO
15c4a 52 59 44 42 0a 20 20 20 20 63 6f 6e 73 74 20 69  RYDB.    const i
15c4b 6e 74 20 69 73 4d 65 6d 64 62 20 3d 20 30 3b 0a  nt isMemdb = 0;.
15c4c 20 20 23 65 6c 73 65 0a 20 20 20 20 63 6f 6e 73    #else.    cons
15c4d 74 20 69 6e 74 20 69 73 4d 65 6d 64 62 20 3d 20  t int isMemdb = 
15c4e 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 21 73 74  zFilename && !st
15c4f 72 63 6d 70 28 7a 46 69 6c 65 6e 61 6d 65 2c 20  rcmp(zFilename, 
15c50 22 3a 6d 65 6d 6f 72 79 3a 22 29 3b 0a 20 20 23  ":memory:");.  #
15c51 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 20 20  endif.#endif..  
15c52 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b  assert( db!=0 );
15c53 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
15c54 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
15c55 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70  ->mutex) );..  p
15c56 56 66 73 20 3d 20 64 62 2d 3e 70 56 66 73 3b 0a  Vfs = db->pVfs;.
15c57 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c    p = sqlite3Mal
15c58 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28 42  locZero(sizeof(B
15c59 74 72 65 65 29 29 3b 0a 20 20 69 66 28 20 21 70  tree));.  if( !p
15c5a 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
15c5b 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
15c5c 0a 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20  .  p->inTrans = 
15c5d 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 70 2d  TRANS_NONE;.  p-
15c5e 3e 64 62 20 3d 20 64 62 3b 0a 23 69 66 6e 64 65  >db = db;.#ifnde
15c5f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
15c60 41 52 45 44 5f 43 41 43 48 45 0a 20 20 70 2d 3e  ARED_CACHE.  p->
15c61 6c 6f 63 6b 2e 70 42 74 72 65 65 20 3d 20 70 3b  lock.pBtree = p;
15c62 0a 20 20 70 2d 3e 6c 6f 63 6b 2e 69 54 61 62 6c  .  p->lock.iTabl
15c63 65 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 23  e = 1;.#endif..#
15c64 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
15c65 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
15c66 41 43 48 45 29 20 26 26 20 21 64 65 66 69 6e 65  ACHE) && !define
15c67 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49  d(SQLITE_OMIT_DI
15c68 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20  SKIO).  /*.  ** 
15c69 49 66 20 74 68 69 73 20 42 74 72 65 65 20 69 73  If this Btree is
15c6a 20 61 20 63 61 6e 64 69 64 61 74 65 20 66 6f 72   a candidate for
15c6b 20 73 68 61 72 65 64 20 63 61 63 68 65 2c 20 74   shared cache, t
15c6c 72 79 20 74 6f 20 66 69 6e 64 20 61 6e 0a 20 20  ry to find an.  
15c6d 2a 2a 20 65 78 69 73 74 69 6e 67 20 42 74 53 68  ** existing BtSh
15c6e 61 72 65 64 20 6f 62 6a 65 63 74 20 74 68 61 74  ared object that
15c6f 20 77 65 20 63 61 6e 20 73 68 61 72 65 20 77 69   we can share wi
15c70 74 68 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73  th.  */.  if( is
15c71 4d 65 6d 64 62 3d 3d 30 20 26 26 20 7a 46 69 6c  Memdb==0 && zFil
15c72 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61  ename && zFilena
15c73 6d 65 5b 30 5d 20 29 7b 0a 20 20 20 20 69 66 28  me[0] ){.    if(
15c74 20 76 66 73 46 6c 61 67 73 20 26 20 53 51 4c 49   vfsFlags & SQLI
15c75 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41  TE_OPEN_SHAREDCA
15c76 43 48 45 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  CHE ){.      int
15c77 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d   nFullPathname =
15c78 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d   pVfs->mxPathnam
15c79 65 2b 31 3b 0a 20 20 20 20 20 20 63 68 61 72 20  e+1;.      char 
15c7a 2a 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d  *zFullPathname =
15c7b 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e   sqlite3Malloc(n
15c7c 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20  FullPathname);. 
15c7d 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
15c7e 65 78 20 2a 6d 75 74 65 78 53 68 61 72 65 64 3b  ex *mutexShared;
15c7f 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 72 61 62  .      p->sharab
15c80 6c 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 69 66  le = 1;.      if
15c81 28 20 21 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65  ( !zFullPathname
15c82 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
15c83 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20  te3_free(p);.   
15c84 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
15c85 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
15c86 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  }.      sqlite3O
15c87 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56  sFullPathname(pV
15c88 66 73 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e  fs, zFilename, n
15c89 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 7a 46  FullPathname, zF
15c8a 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20  ullPathname);.  
15c8b 20 20 20 20 6d 75 74 65 78 4f 70 65 6e 20 3d 20      mutexOpen = 
15c8c 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
15c8d 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
15c8e 54 41 54 49 43 5f 4f 50 45 4e 29 3b 0a 20 20 20  TATIC_OPEN);.   
15c8f 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
15c90 5f 65 6e 74 65 72 28 6d 75 74 65 78 4f 70 65 6e  _enter(mutexOpen
15c91 29 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 53 68  );.      mutexSh
15c92 61 72 65 64 20 3d 20 73 71 6c 69 74 65 33 4d 75  ared = sqlite3Mu
15c93 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
15c94 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
15c95 54 45 52 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  TER);.      sqli
15c96 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
15c97 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20  mutexShared);.  
15c98 20 20 20 20 66 6f 72 28 70 42 74 3d 47 4c 4f 42      for(pBt=GLOB
15c99 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c  AL(BtShared*,sql
15c9a 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c  ite3SharedCacheL
15c9b 69 73 74 29 3b 20 70 42 74 3b 20 70 42 74 3d 70  ist); pBt; pBt=p
15c9c 42 74 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  Bt->pNext){.    
15c9d 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d      assert( pBt-
15c9e 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 20 20 20  >nRef>0 );.     
15c9f 20 20 20 69 66 28 20 30 3d 3d 73 74 72 63 6d 70     if( 0==strcmp
15ca0 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20  (zFullPathname, 
15ca1 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65  sqlite3PagerFile
15ca2 6e 61 6d 65 28 70 42 74 2d 3e 70 50 61 67 65 72  name(pBt->pPager
15ca3 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  )).             
15ca4 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 50 61      && sqlite3Pa
15ca5 67 65 72 56 66 73 28 70 42 74 2d 3e 70 50 61 67  gerVfs(pBt->pPag
15ca6 65 72 29 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20  er)==pVfs ){.   
15ca7 20 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a         int iDb;.
15ca8 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 44            for(iD
15ca9 62 3d 64 62 2d 3e 6e 44 62 2d 31 3b 20 69 44 62  b=db->nDb-1; iDb
15caa 3e 3d 30 3b 20 69 44 62 2d 2d 29 7b 0a 20 20 20  >=0; iDb--){.   
15cab 20 20 20 20 20 20 20 20 20 42 74 72 65 65 20 2a           Btree *
15cac 70 45 78 69 73 74 69 6e 67 20 3d 20 64 62 2d 3e  pExisting = db->
15cad 61 44 62 5b 69 44 62 5d 2e 70 42 74 3b 0a 20 20  aDb[iDb].pBt;.  
15cae 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 45            if( pE
15caf 78 69 73 74 69 6e 67 20 26 26 20 70 45 78 69 73  xisting && pExis
15cb0 74 69 6e 67 2d 3e 70 42 74 3d 3d 70 42 74 20 29  ting->pBt==pBt )
15cb1 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
15cb2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
15cb3 61 76 65 28 6d 75 74 65 78 53 68 61 72 65 64 29  ave(mutexShared)
15cb4 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
15cb5 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
15cb6 61 76 65 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a  ave(mutexOpen);.
15cb7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
15cb8 6c 69 74 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c  lite3_free(zFull
15cb9 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20  Pathname);.     
15cba 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
15cbb 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20  _free(p);.      
15cbc 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
15cbd 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
15cbe 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
15cbf 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
15cc0 20 20 20 20 20 20 70 2d 3e 70 42 74 20 3d 20 70        p->pBt = p
15cc1 42 74 3b 0a 20 20 20 20 20 20 20 20 20 20 70 42  Bt;.          pB
15cc2 74 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20  t->nRef++;.     
15cc3 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
15cc4 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
15cc5 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
15cc6 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61  x_leave(mutexSha
15cc7 72 65 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  red);.      sqli
15cc8 74 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61  te3_free(zFullPa
15cc9 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23  thname);.    }.#
15cca 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
15ccb 55 47 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20 20  UG.    else{.   
15ccc 20 20 20 2f 2a 20 49 6e 20 64 65 62 75 67 20 6d     /* In debug m
15ccd 6f 64 65 2c 20 77 65 20 6d 61 72 6b 20 61 6c 6c  ode, we mark all
15cce 20 70 65 72 73 69 73 74 65 6e 74 20 64 61 74 61   persistent data
15ccf 62 61 73 65 73 20 61 73 20 73 68 61 72 61 62 6c  bases as sharabl
15cd0 65 0a 20 20 20 20 20 20 2a 2a 20 65 76 65 6e 20  e.      ** even 
15cd1 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 6e 6f  when they are no
15cd2 74 2e 20 20 54 68 69 73 20 65 78 65 72 63 69 73  t.  This exercis
15cd3 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63  es the locking c
15cd4 6f 64 65 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a  ode and.      **
15cd5 20 67 69 76 65 73 20 6d 6f 72 65 20 6f 70 70 6f   gives more oppo
15cd6 72 74 75 6e 69 74 79 20 66 6f 72 20 61 73 73 65  rtunity for asse
15cd7 72 74 73 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  rts(sqlite3_mute
15cd8 78 5f 68 65 6c 64 28 29 29 0a 20 20 20 20 20 20  x_held()).      
15cd9 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f  ** statements to
15cda 20 66 69 6e 64 20 6c 6f 63 6b 69 6e 67 20 70 72   find locking pr
15cdb 6f 62 6c 65 6d 73 2e 0a 20 20 20 20 20 20 2a 2f  oblems..      */
15cdc 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 72 61 62  .      p->sharab
15cdd 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 23 65  le = 1;.    }.#e
15cde 6e 64 69 66 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ndif.  }.#endif.
15cdf 20 20 69 66 28 20 70 42 74 3d 3d 30 20 29 7b 0a    if( pBt==0 ){.
15ce0 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68      /*.    ** Th
15ce1 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65  e following asse
15ce2 72 74 73 20 6d 61 6b 65 20 73 75 72 65 20 74 68  rts make sure th
15ce3 61 74 20 73 74 72 75 63 74 75 72 65 73 20 75 73  at structures us
15ce4 65 64 20 62 79 20 74 68 65 20 62 74 72 65 65 20  ed by the btree 
15ce5 61 72 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 72  are.    ** the r
15ce6 69 67 68 74 20 73 69 7a 65 2e 20 20 54 68 69 73  ight size.  This
15ce7 20 69 73 20 74 6f 20 67 75 61 72 64 20 61 67 61   is to guard aga
15ce8 69 6e 73 74 20 73 69 7a 65 20 63 68 61 6e 67 65  inst size change
15ce9 73 20 74 68 61 74 20 72 65 73 75 6c 74 0a 20 20  s that result.  
15cea 20 20 2a 2a 20 77 68 65 6e 20 63 6f 6d 70 69 6c    ** when compil
15ceb 69 6e 67 20 6f 6e 20 61 20 64 69 66 66 65 72 65  ing on a differe
15cec 6e 74 20 61 72 63 68 69 74 65 63 74 75 72 65 2e  nt architecture.
15ced 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
15cee 72 74 28 20 73 69 7a 65 6f 66 28 69 36 34 29 3d  rt( sizeof(i64)=
15cef 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28 69 36 34  =8 || sizeof(i64
15cf0 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65  )==4 );.    asse
15cf1 72 74 28 20 73 69 7a 65 6f 66 28 75 36 34 29 3d  rt( sizeof(u64)=
15cf2 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28 75 36 34  =8 || sizeof(u64
15cf3 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65  )==4 );.    asse
15cf4 72 74 28 20 73 69 7a 65 6f 66 28 75 33 32 29 3d  rt( sizeof(u32)=
15cf5 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =4 );.    assert
15cf6 28 20 73 69 7a 65 6f 66 28 75 31 36 29 3d 3d 32  ( sizeof(u16)==2
15cf7 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
15cf8 73 69 7a 65 6f 66 28 50 67 6e 6f 29 3d 3d 34 20  sizeof(Pgno)==4 
15cf9 29 3b 0a 20 20 0a 20 20 20 20 70 42 74 20 3d 20  );.  .    pBt = 
15cfa 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72  sqlite3MallocZer
15cfb 6f 28 20 73 69 7a 65 6f 66 28 2a 70 42 74 29 20  o( sizeof(*pBt) 
15cfc 29 3b 0a 20 20 20 20 69 66 28 20 70 42 74 3d 3d  );.    if( pBt==
15cfd 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
15cfe 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
15cff 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f      goto btree_o
15d00 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  pen_out;.    }. 
15d01 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
15d02 61 67 65 72 4f 70 65 6e 28 70 56 66 73 2c 20 26  agerOpen(pVfs, &
15d03 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a 46 69  pBt->pPager, zFi
15d04 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  lename,.        
15d05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d06 20 20 45 58 54 52 41 5f 53 49 5a 45 2c 20 66 6c    EXTRA_SIZE, fl
15d07 61 67 73 2c 20 76 66 73 46 6c 61 67 73 2c 20 70  ags, vfsFlags, p
15d08 61 67 65 52 65 69 6e 69 74 29 3b 0a 20 20 20 20  ageReinit);.    
15d09 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
15d0a 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  K ){.      rc = 
15d0b 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 61 64  sqlite3PagerRead
15d0c 46 69 6c 65 68 65 61 64 65 72 28 70 42 74 2d 3e  Fileheader(pBt->
15d0d 70 50 61 67 65 72 2c 73 69 7a 65 6f 66 28 7a 44  pPager,sizeof(zD
15d0e 62 48 65 61 64 65 72 29 2c 7a 44 62 48 65 61 64  bHeader),zDbHead
15d0f 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  er);.    }.    i
15d10 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
15d11 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62   ){.      goto b
15d12 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20  tree_open_out;. 
15d13 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 64 62     }.    pBt->db
15d14 20 3d 20 64 62 3b 0a 20 20 20 20 73 71 6c 69 74   = db;.    sqlit
15d15 65 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61  e3PagerSetBusyha
15d16 6e 64 6c 65 72 28 70 42 74 2d 3e 70 50 61 67 65  ndler(pBt->pPage
15d17 72 2c 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75  r, btreeInvokeBu
15d18 73 79 48 61 6e 64 6c 65 72 2c 20 70 42 74 29 3b  syHandler, pBt);
15d19 0a 20 20 20 20 70 2d 3e 70 42 74 20 3d 20 70 42  .    p->pBt = pB
15d1a 74 3b 0a 20 20 0a 20 20 20 20 70 42 74 2d 3e 70  t;.  .    pBt->p
15d1b 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20  Cursor = 0;.    
15d1c 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b  pBt->pPage1 = 0;
15d1d 0a 20 20 20 20 70 42 74 2d 3e 72 65 61 64 4f 6e  .    pBt->readOn
15d1e 6c 79 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  ly = sqlite3Page
15d1f 72 49 73 72 65 61 64 6f 6e 6c 79 28 70 42 74 2d  rIsreadonly(pBt-
15d20 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 70 42  >pPager);.    pB
15d21 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 67 65  t->pageSize = ge
15d22 74 32 62 79 74 65 28 26 7a 44 62 48 65 61 64 65  t2byte(&zDbHeade
15d23 72 5b 31 36 5d 29 3b 0a 20 20 20 20 69 66 28 20  r[16]);.    if( 
15d24 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c 35 31  pBt->pageSize<51
15d25 32 20 7c 7c 20 70 42 74 2d 3e 70 61 67 65 53 69  2 || pBt->pageSi
15d26 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41  ze>SQLITE_MAX_PA
15d27 47 45 5f 53 49 5a 45 0a 20 20 20 20 20 20 20 20  GE_SIZE.        
15d28 20 7c 7c 20 28 28 70 42 74 2d 3e 70 61 67 65 53   || ((pBt->pageS
15d29 69 7a 65 2d 31 29 26 70 42 74 2d 3e 70 61 67 65  ize-1)&pBt->page
15d2a 53 69 7a 65 29 21 3d 30 20 29 7b 0a 20 20 20 20  Size)!=0 ){.    
15d2b 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20    pBt->pageSize 
15d2c 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  = 0;.#ifndef SQL
15d2d 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
15d2e 55 55 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  UUM.      /* If 
15d2f 74 68 65 20 6d 61 67 69 63 20 6e 61 6d 65 20 22  the magic name "
15d30 3a 6d 65 6d 6f 72 79 3a 22 20 77 69 6c 6c 20 63  :memory:" will c
15d31 72 65 61 74 65 20 61 6e 20 69 6e 2d 6d 65 6d 6f  reate an in-memo
15d32 72 79 20 64 61 74 61 62 61 73 65 2c 20 74 68 65  ry database, the
15d33 6e 0a 20 20 20 20 20 20 2a 2a 20 6c 65 61 76 65  n.      ** leave
15d34 20 74 68 65 20 61 75 74 6f 56 61 63 75 75 6d 20   the autoVacuum 
15d35 6d 6f 64 65 20 61 74 20 30 20 28 64 6f 20 6e 6f  mode at 0 (do no
15d36 74 20 61 75 74 6f 2d 76 61 63 75 75 6d 29 2c 20  t auto-vacuum), 
15d37 65 76 65 6e 20 69 66 0a 20 20 20 20 20 20 2a 2a  even if.      **
15d38 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
15d39 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 74 72  AUTOVACUUM is tr
15d3a 75 65 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 72  ue. On the other
15d3b 20 68 61 6e 64 2c 20 69 66 0a 20 20 20 20 20 20   hand, if.      
15d3c 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d  ** SQLITE_OMIT_M
15d3d 45 4d 4f 52 59 44 42 20 68 61 73 20 62 65 65 6e  EMORYDB has been
15d3e 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 22   defined, then "
15d3f 3a 6d 65 6d 6f 72 79 3a 22 20 69 73 20 6a 75 73  :memory:" is jus
15d40 74 20 61 0a 20 20 20 20 20 20 2a 2a 20 72 65 67  t a.      ** reg
15d41 75 6c 61 72 20 66 69 6c 65 2d 6e 61 6d 65 2e 20  ular file-name. 
15d42 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
15d43 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 61 70 70   auto-vacuum app
15d44 6c 69 65 73 20 61 73 20 70 65 72 20 6e 6f 72 6d  lies as per norm
15d45 61 6c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  al..      */.   
15d46 20 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65     if( zFilename
15d47 20 26 26 20 21 69 73 4d 65 6d 64 62 20 29 7b 0a   && !isMemdb ){.
15d48 20 20 20 20 20 20 20 20 70 42 74 2d 3e 61 75 74          pBt->aut
15d49 6f 56 61 63 75 75 6d 20 3d 20 28 53 51 4c 49 54  oVacuum = (SQLIT
15d4a 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41  E_DEFAULT_AUTOVA
15d4b 43 55 55 4d 20 3f 20 31 20 3a 20 30 29 3b 0a 20  CUUM ? 1 : 0);. 
15d4c 20 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 63 72         pBt->incr
15d4d 56 61 63 75 75 6d 20 3d 20 28 53 51 4c 49 54 45  Vacuum = (SQLITE
15d4e 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43  _DEFAULT_AUTOVAC
15d4f 55 55 4d 3d 3d 32 20 3f 20 31 20 3a 20 30 29 3b  UUM==2 ? 1 : 0);
15d50 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
15d51 20 20 20 20 20 20 6e 52 65 73 65 72 76 65 20 3d        nReserve =
15d52 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   0;.    }else{. 
15d53 20 20 20 20 20 6e 52 65 73 65 72 76 65 20 3d 20       nReserve = 
15d54 7a 44 62 48 65 61 64 65 72 5b 32 30 5d 3b 0a 20  zDbHeader[20];. 
15d55 20 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69       pBt->pageSi
15d56 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a 23 69 66  zeFixed = 1;.#if
15d57 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
15d58 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
15d59 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75    pBt->autoVacuu
15d5a 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 7a  m = (get4byte(&z
15d5b 44 62 48 65 61 64 65 72 5b 33 36 20 2b 20 34 2a  DbHeader[36 + 4*
15d5c 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 20 20 20  4])?1:0);.      
15d5d 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20  pBt->incrVacuum 
15d5e 3d 20 28 67 65 74 34 62 79 74 65 28 26 7a 44 62  = (get4byte(&zDb
15d5f 48 65 61 64 65 72 5b 33 36 20 2b 20 37 2a 34 5d  Header[36 + 7*4]
15d60 29 3f 31 3a 30 29 3b 0a 23 65 6e 64 69 66 0a 20  )?1:0);.#endif. 
15d61 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71     }.    rc = sq
15d62 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67  lite3PagerSetPag
15d63 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65  esize(pBt->pPage
15d64 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 69 7a  r, &pBt->pageSiz
15d65 65 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 20  e, nReserve);.  
15d66 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20    if( rc ) goto 
15d67 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a  btree_open_out;.
15d68 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53      pBt->usableS
15d69 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53  ize = pBt->pageS
15d6a 69 7a 65 20 2d 20 6e 52 65 73 65 72 76 65 3b 0a  ize - nReserve;.
15d6b 20 20 20 20 61 73 73 65 72 74 28 20 28 70 42 74      assert( (pBt
15d6c 2d 3e 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d  ->pageSize & 7)=
15d6d 3d 30 20 29 3b 20 20 2f 2a 20 38 2d 62 79 74 65  =0 );  /* 8-byte
15d6e 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 70 61   alignment of pa
15d6f 67 65 53 69 7a 65 20 2a 2f 0a 20 20 20 0a 23 69  geSize */.   .#i
15d70 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
15d71 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
15d72 43 48 45 29 20 26 26 20 21 64 65 66 69 6e 65 64  CHE) && !defined
15d73 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53  (SQLITE_OMIT_DIS
15d74 4b 49 4f 29 0a 20 20 20 20 2f 2a 20 41 64 64 20  KIO).    /* Add 
15d75 74 68 65 20 6e 65 77 20 42 74 53 68 61 72 65 64  the new BtShared
15d76 20 6f 62 6a 65 63 74 20 74 6f 20 74 68 65 20 6c   object to the l
15d77 69 6e 6b 65 64 20 6c 69 73 74 20 73 68 61 72 61  inked list shara
15d78 62 6c 65 20 42 74 53 68 61 72 65 64 73 2e 0a 20  ble BtShareds.. 
15d79 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d     */.    if( p-
15d7a 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20  >sharable ){.   
15d7b 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
15d7c 20 2a 6d 75 74 65 78 53 68 61 72 65 64 3b 0a 20   *mutexShared;. 
15d7d 20 20 20 20 20 70 42 74 2d 3e 6e 52 65 66 20 3d       pBt->nRef =
15d7e 20 31 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 53   1;.      mutexS
15d7f 68 61 72 65 64 20 3d 20 73 71 6c 69 74 65 33 4d  hared = sqlite3M
15d80 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
15d81 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
15d82 53 54 45 52 29 3b 0a 20 20 20 20 20 20 69 66 28  STER);.      if(
15d83 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
15d84 46 45 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f  FE && sqlite3Glo
15d85 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d  balConfig.bCoreM
15d86 75 74 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20  utex ){.        
15d87 70 42 74 2d 3e 6d 75 74 65 78 20 3d 20 73 71 6c  pBt->mutex = sql
15d88 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
15d89 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
15d8a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
15d8b 42 74 2d 3e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a  Bt->mutex==0 ){.
15d8c 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
15d8d 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
15d8e 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f         db->mallo
15d8f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20  cFailed = 0;.   
15d90 20 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65         goto btre
15d91 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20  e_open_out;.    
15d92 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
15d93 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
15d94 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 53 68 61  x_enter(mutexSha
15d95 72 65 64 29 3b 0a 20 20 20 20 20 20 70 42 74 2d  red);.      pBt-
15d96 3e 70 4e 65 78 74 20 3d 20 47 4c 4f 42 41 4c 28  >pNext = GLOBAL(
15d97 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65  BtShared*,sqlite
15d98 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74  3SharedCacheList
15d99 29 3b 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c 28  );.      GLOBAL(
15d9a 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65  BtShared*,sqlite
15d9b 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74  3SharedCacheList
15d9c 29 20 3d 20 70 42 74 3b 0a 20 20 20 20 20 20 73  ) = pBt;.      s
15d9d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
15d9e 76 65 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b  ve(mutexShared);
15d9f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
15da0 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  }..#if !defined(
15da1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
15da2 45 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65  ED_CACHE) && !de
15da3 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
15da4 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 20 49  T_DISKIO).  /* I
15da5 66 20 74 68 65 20 6e 65 77 20 42 74 72 65 65 20  f the new Btree 
15da6 75 73 65 73 20 61 20 73 68 61 72 61 62 6c 65 20  uses a sharable 
15da7 70 42 74 53 68 61 72 65 64 2c 20 74 68 65 6e 20  pBtShared, then 
15da8 6c 69 6e 6b 20 74 68 65 20 6e 65 77 0a 20 20 2a  link the new.  *
15da9 2a 20 42 74 72 65 65 20 69 6e 74 6f 20 74 68 65  * Btree into the
15daa 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 73 68 61   list of all sha
15dab 72 61 62 6c 65 20 42 74 72 65 65 73 20 66 6f 72  rable Btrees for
15dac 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63   the same connec
15dad 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 65 20 6c  tion..  ** The l
15dae 69 73 74 20 69 73 20 6b 65 70 74 20 69 6e 20 61  ist is kept in a
15daf 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62  scending order b
15db0 79 20 70 42 74 20 61 64 64 72 65 73 73 2e 0a 20  y pBt address.. 
15db1 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 73 68 61   */.  if( p->sha
15db2 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74  rable ){.    int
15db3 20 69 3b 0a 20 20 20 20 42 74 72 65 65 20 2a 70   i;.    Btree *p
15db4 53 69 62 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  Sib;.    for(i=0
15db5 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
15db6 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 70 53  ){.      if( (pS
15db7 69 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e  ib = db->aDb[i].
15db8 70 42 74 29 21 3d 30 20 26 26 20 70 53 69 62 2d  pBt)!=0 && pSib-
15db9 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20  >sharable ){.   
15dba 20 20 20 20 20 77 68 69 6c 65 28 20 70 53 69 62       while( pSib
15dbb 2d 3e 70 50 72 65 76 20 29 7b 20 70 53 69 62 20  ->pPrev ){ pSib 
15dbc 3d 20 70 53 69 62 2d 3e 70 50 72 65 76 3b 20 7d  = pSib->pPrev; }
15dbd 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e  .        if( p->
15dbe 70 42 74 3c 70 53 69 62 2d 3e 70 42 74 20 29 7b  pBt<pSib->pBt ){
15dbf 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e  .          p->pN
15dc0 65 78 74 20 3d 20 70 53 69 62 3b 0a 20 20 20 20  ext = pSib;.    
15dc1 20 20 20 20 20 20 70 2d 3e 70 50 72 65 76 20 3d        p->pPrev =
15dc2 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53   0;.          pS
15dc3 69 62 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20  ib->pPrev = p;. 
15dc4 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
15dc5 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70          while( p
15dc6 53 69 62 2d 3e 70 4e 65 78 74 20 26 26 20 70 53  Sib->pNext && pS
15dc7 69 62 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3c 70  ib->pNext->pBt<p
15dc8 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20  ->pBt ){.       
15dc9 20 20 20 20 20 70 53 69 62 20 3d 20 70 53 69 62       pSib = pSib
15dca 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20  ->pNext;.       
15dcb 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70     }.          p
15dcc 2d 3e 70 4e 65 78 74 20 3d 20 70 53 69 62 2d 3e  ->pNext = pSib->
15dcd 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 20  pNext;.         
15dce 20 70 2d 3e 70 50 72 65 76 20 3d 20 70 53 69 62   p->pPrev = pSib
15dcf 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
15dd0 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20  p->pNext ){.    
15dd1 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74          p->pNext
15dd2 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 20  ->pPrev = p;.   
15dd3 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
15dd4 20 20 20 70 53 69 62 2d 3e 70 4e 65 78 74 20 3d     pSib->pNext =
15dd5 20 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   p;.        }.  
15dd6 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
15dd7 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23     }.    }.  }.#
15dd8 65 6e 64 69 66 0a 20 20 2a 70 70 42 74 72 65 65  endif.  *ppBtree
15dd9 20 3d 20 70 3b 0a 0a 62 74 72 65 65 5f 6f 70 65   = p;..btree_ope
15dda 6e 5f 6f 75 74 3a 0a 20 20 69 66 28 20 72 63 21  n_out:.  if( rc!
15ddb 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
15ddc 20 20 69 66 28 20 70 42 74 20 26 26 20 70 42 74    if( pBt && pBt
15ddd 2d 3e 70 50 61 67 65 72 20 29 7b 0a 20 20 20 20  ->pPager ){.    
15dde 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c    sqlite3PagerCl
15ddf 6f 73 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29  ose(pBt->pPager)
15de0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
15de1 74 65 33 5f 66 72 65 65 28 70 42 74 29 3b 0a 20  te3_free(pBt);. 
15de2 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
15de3 70 29 3b 0a 20 20 20 20 2a 70 70 42 74 72 65 65  p);.    *ppBtree
15de4 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 0;.  }.  if( 
15de5 6d 75 74 65 78 4f 70 65 6e 20 29 7b 0a 20 20 20  mutexOpen ){.   
15de6 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
15de7 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 75 74 65  _mutex_held(mute
15de8 78 4f 70 65 6e 29 20 29 3b 0a 20 20 20 20 73 71  xOpen) );.    sq
15de9 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
15dea 65 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20  e(mutexOpen);.  
15deb 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
15dec 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e  ../*.** Decremen
15ded 74 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 6e  t the BtShared.n
15dee 52 65 66 20 63 6f 75 6e 74 65 72 2e 20 20 57 68  Ref counter.  Wh
15def 65 6e 20 69 74 20 72 65 61 63 68 65 73 20 7a 65  en it reaches ze
15df0 72 6f 2c 0a 2a 2a 20 72 65 6d 6f 76 65 20 74 68  ro,.** remove th
15df1 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63  e BtShared struc
15df2 74 75 72 65 20 66 72 6f 6d 20 74 68 65 20 73 68  ture from the sh
15df3 61 72 69 6e 67 20 6c 69 73 74 2e 20 20 52 65 74  aring list.  Ret
15df4 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69 66 20 74  urn.** true if t
15df5 68 65 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66  he BtShared.nRef
15df6 20 63 6f 75 6e 74 65 72 20 72 65 61 63 68 65 73   counter reaches
15df7 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e   zero and return
15df8 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 69 74 20  .** false if it 
15df9 69 73 20 73 74 69 6c 6c 20 70 6f 73 69 74 69 76  is still positiv
15dfa 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
15dfb 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68 61 72 69   removeFromShari
15dfc 6e 67 4c 69 73 74 28 42 74 53 68 61 72 65 64 20  ngList(BtShared 
15dfd 2a 70 42 74 29 7b 0a 23 69 66 6e 64 65 66 20 53  *pBt){.#ifndef S
15dfe 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
15dff 44 5f 43 41 43 48 45 0a 20 20 73 71 6c 69 74 65  D_CACHE.  sqlite
15e00 33 5f 6d 75 74 65 78 20 2a 70 4d 61 73 74 65 72  3_mutex *pMaster
15e01 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 4c  ;.  BtShared *pL
15e02 69 73 74 3b 0a 20 20 69 6e 74 20 72 65 6d 6f 76  ist;.  int remov
15e03 65 64 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72  ed = 0;..  asser
15e04 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
15e05 5f 6e 6f 74 68 65 6c 64 28 70 42 74 2d 3e 6d 75  _notheld(pBt->mu
15e06 74 65 78 29 20 29 3b 0a 20 20 70 4d 61 73 74 65  tex) );.  pMaste
15e07 72 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78  r = sqlite3Mutex
15e08 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
15e09 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
15e0a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
15e0b 65 78 5f 65 6e 74 65 72 28 70 4d 61 73 74 65 72  ex_enter(pMaster
15e0c 29 3b 0a 20 20 70 42 74 2d 3e 6e 52 65 66 2d 2d  );.  pBt->nRef--
15e0d 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 6e 52 65  ;.  if( pBt->nRe
15e0e 66 3c 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  f<=0 ){.    if( 
15e0f 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a  GLOBAL(BtShared*
15e10 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61  ,sqlite3SharedCa
15e11 63 68 65 4c 69 73 74 29 3d 3d 70 42 74 20 29 7b  cheList)==pBt ){
15e12 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c 28 42 74  .      GLOBAL(Bt
15e13 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53  Shared*,sqlite3S
15e14 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 20  haredCacheList) 
15e15 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b 0a 20 20  = pBt->pNext;.  
15e16 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
15e17 4c 69 73 74 20 3d 20 47 4c 4f 42 41 4c 28 42 74  List = GLOBAL(Bt
15e18 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53  Shared*,sqlite3S
15e19 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 3b  haredCacheList);
15e1a 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 41 4c  .      while( AL
15e1b 57 41 59 53 28 70 4c 69 73 74 29 20 26 26 20 70  WAYS(pList) && p
15e1c 4c 69 73 74 2d 3e 70 4e 65 78 74 21 3d 70 42 74  List->pNext!=pBt
15e1d 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 73   ){.        pLis
15e1e 74 3d 70 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a  t=pList->pNext;.
15e1f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
15e20 28 20 41 4c 57 41 59 53 28 70 4c 69 73 74 29 20  ( ALWAYS(pList) 
15e21 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74  ){.        pList
15e22 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70  ->pNext = pBt->p
15e23 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Next;.      }.  
15e24 20 20 7d 0a 20 20 20 20 69 66 28 20 53 51 4c 49    }.    if( SQLI
15e25 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 29 7b  TE_THREADSAFE ){
15e26 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
15e27 75 74 65 78 5f 66 72 65 65 28 70 42 74 2d 3e 6d  utex_free(pBt->m
15e28 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20  utex);.    }.   
15e29 20 72 65 6d 6f 76 65 64 20 3d 20 31 3b 0a 20 20   removed = 1;.  
15e2a 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
15e2b 78 5f 6c 65 61 76 65 28 70 4d 61 73 74 65 72 29  x_leave(pMaster)
15e2c 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 6d 6f 76  ;.  return remov
15e2d 65 64 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75  ed;.#else.  retu
15e2e 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a  rn 1;.#endif.}..
15e2f 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  /*.** Make sure 
15e30 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 70  pBt->pTmpSpace p
15e31 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 6c 6c 6f  oints to an allo
15e32 63 61 74 69 6f 6e 20 6f 66 20 0a 2a 2a 20 4d 58  cation of .** MX
15e33 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20  _CELL_SIZE(pBt) 
15e34 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  bytes..*/.static
15e35 20 76 6f 69 64 20 61 6c 6c 6f 63 61 74 65 54 65   void allocateTe
15e36 6d 70 53 70 61 63 65 28 42 74 53 68 61 72 65 64  mpSpace(BtShared
15e37 20 2a 70 42 74 29 7b 0a 20 20 69 66 28 20 21 70   *pBt){.  if( !p
15e38 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 29 7b  Bt->pTmpSpace ){
15e39 0a 20 20 20 20 70 42 74 2d 3e 70 54 6d 70 53 70  .    pBt->pTmpSp
15e3a 61 63 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67  ace = sqlite3Pag
15e3b 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 61  eMalloc( pBt->pa
15e3c 67 65 53 69 7a 65 20 29 3b 0a 20 20 7d 0a 7d 0a  geSize );.  }.}.
15e3d 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 20  ./*.** Free the 
15e3e 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 61  pBt->pTmpSpace a
15e3f 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61  llocation.*/.sta
15e40 74 69 63 20 76 6f 69 64 20 66 72 65 65 54 65 6d  tic void freeTem
15e41 70 53 70 61 63 65 28 42 74 53 68 61 72 65 64 20  pSpace(BtShared 
15e42 2a 70 42 74 29 7b 0a 20 20 73 71 6c 69 74 65 33  *pBt){.  sqlite3
15e43 50 61 67 65 46 72 65 65 28 20 70 42 74 2d 3e 70  PageFree( pBt->p
15e44 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 70 42 74  TmpSpace);.  pBt
15e45 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 30 3b  ->pTmpSpace = 0;
15e46 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20  .}../*.** Close 
15e47 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65  an open database
15e48 20 61 6e 64 20 69 6e 76 61 6c 69 64 61 74 65 20   and invalidate 
15e49 61 6c 6c 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a  all cursors..*/.
15e4a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
15e4b 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
15e4c 6c 6f 73 65 28 42 74 72 65 65 20 2a 70 29 7b 0a  lose(Btree *p){.
15e4d 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
15e4e 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 43 75  = p->pBt;.  BtCu
15e4f 72 73 6f 72 20 2a 70 43 75 72 3b 0a 0a 20 20 2f  rsor *pCur;..  /
15e50 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 63 75 72 73  * Close all curs
15e51 6f 72 73 20 6f 70 65 6e 65 64 20 76 69 61 20 74  ors opened via t
15e52 68 69 73 20 68 61 6e 64 6c 65 2e 20 20 2a 2f 0a  his handle.  */.
15e53 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
15e54 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
15e55 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
15e56 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
15e57 72 28 70 29 3b 0a 20 20 70 43 75 72 20 3d 20 70  r(p);.  pCur = p
15e58 42 74 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 77  Bt->pCursor;.  w
15e59 68 69 6c 65 28 20 70 43 75 72 20 29 7b 0a 20 20  hile( pCur ){.  
15e5a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 54 6d 70    BtCursor *pTmp
15e5b 20 3d 20 70 43 75 72 3b 0a 20 20 20 20 70 43 75   = pCur;.    pCu
15e5c 72 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b  r = pCur->pNext;
15e5d 0a 20 20 20 20 69 66 28 20 70 54 6d 70 2d 3e 70  .    if( pTmp->p
15e5e 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20  Btree==p ){.    
15e5f 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c    sqlite3BtreeCl
15e60 6f 73 65 43 75 72 73 6f 72 28 70 54 6d 70 29 3b  oseCursor(pTmp);
15e61 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
15e62 20 52 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63   Rollback any ac
15e63 74 69 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  tive transaction
15e64 20 61 6e 64 20 66 72 65 65 20 74 68 65 20 68 61   and free the ha
15e65 6e 64 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a  ndle structure..
15e66 20 20 2a 2a 20 54 68 65 20 63 61 6c 6c 20 74 6f    ** The call to
15e67 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c   sqlite3BtreeRol
15e68 6c 62 61 63 6b 28 29 20 64 72 6f 70 73 20 61 6e  lback() drops an
15e69 79 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 20 68 65  y table-locks he
15e6a 6c 64 20 62 79 0a 20 20 2a 2a 20 74 68 69 73 20  ld by.  ** this 
15e6b 68 61 6e 64 6c 65 2e 0a 20 20 2a 2f 0a 20 20 73  handle..  */.  s
15e6c 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62  qlite3BtreeRollb
15e6d 61 63 6b 28 70 29 3b 0a 20 20 73 71 6c 69 74 65  ack(p);.  sqlite
15e6e 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
15e6f 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61  .  /* If there a
15e70 72 65 20 73 74 69 6c 6c 20 6f 74 68 65 72 20 6f  re still other o
15e71 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72  utstanding refer
15e72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 73 68 61  ences to the sha
15e73 72 65 64 2d 62 74 72 65 65 0a 20 20 2a 2a 20 73  red-btree.  ** s
15e74 74 72 75 63 74 75 72 65 2c 20 72 65 74 75 72 6e  tructure, return
15e75 20 6e 6f 77 2e 20 54 68 65 20 72 65 6d 61 69 6e   now. The remain
15e76 64 65 72 20 6f 66 20 74 68 69 73 20 70 72 6f 63  der of this proc
15e77 65 64 75 72 65 20 63 6c 65 61 6e 73 20 0a 20 20  edure cleans .  
15e78 2a 2a 20 75 70 20 74 68 65 20 73 68 61 72 65 64  ** up the shared
15e79 2d 62 74 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 61  -btree..  */.  a
15e7a 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f  ssert( p->wantTo
15e7b 4c 6f 63 6b 3d 3d 30 20 26 26 20 70 2d 3e 6c 6f  Lock==0 && p->lo
15e7c 63 6b 65 64 3d 3d 30 20 29 3b 0a 20 20 69 66 28  cked==0 );.  if(
15e7d 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c   !p->sharable ||
15e7e 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68 61 72 69   removeFromShari
15e7f 6e 67 4c 69 73 74 28 70 42 74 29 20 29 7b 0a 20  ngList(pBt) ){. 
15e80 20 20 20 2f 2a 20 54 68 65 20 70 42 74 20 69 73     /* The pBt is
15e81 20 6e 6f 20 6c 6f 6e 67 65 72 20 6f 6e 20 74 68   no longer on th
15e82 65 20 73 68 61 72 69 6e 67 20 6c 69 73 74 2c 20  e sharing list, 
15e83 73 6f 20 77 65 20 63 61 6e 20 61 63 63 65 73 73  so we can access
15e84 0a 20 20 20 20 2a 2a 20 69 74 20 77 69 74 68 6f  .    ** it witho
15e85 75 74 20 68 61 76 69 6e 67 20 74 6f 20 68 6f 6c  ut having to hol
15e86 64 20 74 68 65 20 6d 75 74 65 78 2e 0a 20 20 20  d the mutex..   
15e87 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c 65 61 6e   **.    ** Clean
15e88 20 6f 75 74 20 61 6e 64 20 64 65 6c 65 74 65 20   out and delete 
15e89 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a  the BtShared obj
15e8a 65 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ect..    */.    
15e8b 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70 43  assert( !pBt->pC
15e8c 75 72 73 6f 72 20 29 3b 0a 20 20 20 20 73 71 6c  ursor );.    sql
15e8d 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70  ite3PagerClose(p
15e8e 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20  Bt->pPager);.   
15e8f 20 69 66 28 20 70 42 74 2d 3e 78 46 72 65 65 53   if( pBt->xFreeS
15e90 63 68 65 6d 61 20 26 26 20 70 42 74 2d 3e 70 53  chema && pBt->pS
15e91 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 70  chema ){.      p
15e92 42 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 28  Bt->xFreeSchema(
15e93 70 42 74 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  pBt->pSchema);. 
15e94 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
15e95 5f 66 72 65 65 28 70 42 74 2d 3e 70 53 63 68 65  _free(pBt->pSche
15e96 6d 61 29 3b 0a 20 20 20 20 66 72 65 65 54 65 6d  ma);.    freeTem
15e97 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20  pSpace(pBt);.   
15e98 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42   sqlite3_free(pB
15e99 74 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66  t);.  }..#ifndef
15e9a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
15e9b 52 45 44 5f 43 41 43 48 45 0a 20 20 61 73 73 65  RED_CACHE.  asse
15e9c 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63  rt( p->wantToLoc
15e9d 6b 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  k==0 );.  assert
15e9e 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29  ( p->locked==0 )
15e9f 3b 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 65 76  ;.  if( p->pPrev
15ea0 20 29 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65   ) p->pPrev->pNe
15ea1 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20  xt = p->pNext;. 
15ea2 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 20   if( p->pNext ) 
15ea3 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20  p->pNext->pPrev 
15ea4 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 23 65 6e 64  = p->pPrev;.#end
15ea5 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  if..  sqlite3_fr
15ea6 65 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ee(p);.  return 
15ea7 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
15ea8 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6c  .** Change the l
15ea9 69 6d 69 74 20 6f 6e 20 74 68 65 20 6e 75 6d 62  imit on the numb
15eaa 65 72 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f  er of pages allo
15eab 77 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65  wed in the cache
15eac 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69  ..**.** The maxi
15ead 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 61  mum number of ca
15eae 63 68 65 20 70 61 67 65 73 20 69 73 20 73 65 74  che pages is set
15eaf 20 74 6f 20 74 68 65 20 61 62 73 6f 6c 75 74 65   to the absolute
15eb0 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6d 78 50  .** value of mxP
15eb1 61 67 65 2e 20 20 49 66 20 6d 78 50 61 67 65 20  age.  If mxPage 
15eb2 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65  is negative, the
15eb3 20 70 61 67 65 72 20 77 69 6c 6c 0a 2a 2a 20 6f   pager will.** o
15eb4 70 65 72 61 74 65 20 61 73 79 6e 63 68 72 6f 6e  perate asynchron
15eb5 6f 75 73 6c 79 20 2d 20 69 74 20 77 69 6c 6c 20  ously - it will 
15eb6 6e 6f 74 20 73 74 6f 70 20 74 6f 20 64 6f 20 66  not stop to do f
15eb7 73 79 6e 63 28 29 73 0a 2a 2a 20 74 6f 20 69 6e  sync()s.** to in
15eb8 73 75 72 65 20 64 61 74 61 20 69 73 20 77 72 69  sure data is wri
15eb9 74 74 65 6e 20 74 6f 20 74 68 65 20 64 69 73 6b  tten to the disk
15eba 20 73 75 72 66 61 63 65 20 62 65 66 6f 72 65 0a   surface before.
15ebb 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 20 20  ** continuing.  
15ebc 54 72 61 6e 73 61 63 74 69 6f 6e 73 20 73 74 69  Transactions sti
15ebd 6c 6c 20 77 6f 72 6b 20 69 66 20 73 79 6e 63 68  ll work if synch
15ebe 72 6f 6e 6f 75 73 20 69 73 20 6f 66 66 2c 0a 2a  ronous is off,.*
15ebf 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61  * and the databa
15ec0 73 65 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 72  se cannot be cor
15ec1 72 75 70 74 65 64 20 69 66 20 74 68 69 73 20 70  rupted if this p
15ec2 72 6f 67 72 61 6d 0a 2a 2a 20 63 72 61 73 68 65  rogram.** crashe
15ec3 73 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 6f  s.  But if the o
15ec4 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
15ec5 63 72 61 73 68 65 73 20 6f 72 20 74 68 65 72 65  crashes or there
15ec6 20 69 73 0a 2a 2a 20 61 6e 20 61 62 72 75 70 74   is.** an abrupt
15ec7 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77   power failure w
15ec8 68 65 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20  hen synchronous 
15ec9 69 73 20 6f 66 66 2c 20 74 68 65 20 64 61 74 61  is off, the data
15eca 62 61 73 65 0a 2a 2a 20 63 6f 75 6c 64 20 62 65  base.** could be
15ecb 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f   left in an inco
15ecc 6e 73 69 73 74 65 6e 74 20 61 6e 64 20 75 6e 72  nsistent and unr
15ecd 65 63 6f 76 65 72 61 62 6c 65 20 73 74 61 74 65  ecoverable state
15ece 2e 0a 2a 2a 20 53 79 6e 63 68 72 6f 6e 6f 75 73  ..** Synchronous
15ecf 20 69 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c   is on by defaul
15ed0 74 20 73 6f 20 64 61 74 61 62 61 73 65 20 63 6f  t so database co
15ed1 72 72 75 70 74 69 6f 6e 20 69 73 20 6e 6f 74 0a  rruption is not.
15ed2 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 77 6f  ** normally a wo
15ed3 72 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  rry..*/.SQLITE_P
15ed4 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
15ed5 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53  e3BtreeSetCacheS
15ed6 69 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e  ize(Btree *p, in
15ed7 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 42 74 53  t mxPage){.  BtS
15ed8 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
15ed9 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73  pBt;.  assert( s
15eda 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
15edb 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  d(p->db->mutex) 
15edc 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
15edd 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c  eEnter(p);.  sql
15ede 69 74 65 33 50 61 67 65 72 53 65 74 43 61 63 68  ite3PagerSetCach
15edf 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65  esize(pBt->pPage
15ee0 72 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71  r, mxPage);.  sq
15ee1 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
15ee2 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  p);.  return SQL
15ee3 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
15ee4 20 43 68 61 6e 67 65 20 74 68 65 20 77 61 79 20   Change the way 
15ee5 64 61 74 61 20 69 73 20 73 79 6e 63 65 64 20 74  data is synced t
15ee6 6f 20 64 69 73 6b 20 69 6e 20 6f 72 64 65 72 20  o disk in order 
15ee7 74 6f 20 69 6e 63 72 65 61 73 65 20 6f 72 20 64  to increase or d
15ee8 65 63 72 65 61 73 65 0a 2a 2a 20 68 6f 77 20 77  ecrease.** how w
15ee9 65 6c 6c 20 74 68 65 20 64 61 74 61 62 61 73 65  ell the database
15eea 20 72 65 73 69 73 74 73 20 64 61 6d 61 67 65 20   resists damage 
15eeb 64 75 65 20 74 6f 20 4f 53 20 63 72 61 73 68 65  due to OS crashe
15eec 73 20 61 6e 64 20 70 6f 77 65 72 0a 2a 2a 20 66  s and power.** f
15eed 61 69 6c 75 72 65 73 2e 20 20 4c 65 76 65 6c 20  ailures.  Level 
15eee 31 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  1 is the same as
15eef 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73 20 28 6e   asynchronous (n
15ef0 6f 20 73 79 6e 63 73 28 29 20 6f 63 63 75 72 20  o syncs() occur 
15ef1 61 6e 64 0a 2a 2a 20 74 68 65 72 65 20 69 73 20  and.** there is 
15ef2 61 20 68 69 67 68 20 70 72 6f 62 61 62 69 6c 69  a high probabili
15ef3 74 79 20 6f 66 20 64 61 6d 61 67 65 29 20 20 4c  ty of damage)  L
15ef4 65 76 65 6c 20 32 20 69 73 20 74 68 65 20 64 65  evel 2 is the de
15ef5 66 61 75 6c 74 2e 20 20 54 68 65 72 65 0a 2a 2a  fault.  There.**
15ef6 20 69 73 20 61 20 76 65 72 79 20 6c 6f 77 20 62   is a very low b
15ef7 75 74 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62  ut non-zero prob
15ef8 61 62 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67  ability of damag
15ef9 65 2e 20 20 4c 65 76 65 6c 20 33 20 72 65 64 75  e.  Level 3 redu
15efa 63 65 73 20 74 68 65 0a 2a 2a 20 70 72 6f 62 61  ces the.** proba
15efb 62 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65  bility of damage
15efc 20 74 6f 20 6e 65 61 72 20 7a 65 72 6f 20 62 75   to near zero bu
15efd 74 20 77 69 74 68 20 61 20 77 72 69 74 65 20 70  t with a write p
15efe 65 72 66 6f 72 6d 61 6e 63 65 20 72 65 64 75 63  erformance reduc
15eff 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  tion..*/.#ifndef
15f00 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47   SQLITE_OMIT_PAG
15f01 45 52 5f 50 52 41 47 4d 41 53 0a 53 51 4c 49 54  ER_PRAGMAS.SQLIT
15f02 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
15f03 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 61 66  lite3BtreeSetSaf
15f04 65 74 79 4c 65 76 65 6c 28 42 74 72 65 65 20 2a  etyLevel(Btree *
15f05 70 2c 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e  p, int level, in
15f06 74 20 66 75 6c 6c 53 79 6e 63 29 7b 0a 20 20 42  t fullSync){.  B
15f07 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
15f08 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28  ->pBt;.  assert(
15f09 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
15f0a 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  eld(p->db->mutex
15f0b 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  ) );.  sqlite3Bt
15f0c 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 73  reeEnter(p);.  s
15f0d 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 53 61  qlite3PagerSetSa
15f0e 66 65 74 79 4c 65 76 65 6c 28 70 42 74 2d 3e 70  fetyLevel(pBt->p
15f0f 50 61 67 65 72 2c 20 6c 65 76 65 6c 2c 20 66 75  Pager, level, fu
15f10 6c 6c 53 79 6e 63 29 3b 0a 20 20 73 71 6c 69 74  llSync);.  sqlit
15f11 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
15f12 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
15f13 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  _OK;.}.#endif../
15f14 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45  *.** Return TRUE
15f15 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 62 74   if the given bt
15f16 72 65 65 20 69 73 20 73 65 74 20 74 6f 20 73 61  ree is set to sa
15f17 66 65 74 79 20 6c 65 76 65 6c 20 31 2e 20 20 49  fety level 1.  I
15f18 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73  n other.** words
15f19 2c 20 72 65 74 75 72 6e 20 54 52 55 45 20 69 66  , return TRUE if
15f1a 20 6e 6f 20 73 79 6e 63 28 29 20 6f 63 63 75 72   no sync() occur
15f1b 73 20 6f 6e 20 74 68 65 20 64 69 73 6b 20 66 69  s on the disk fi
15f1c 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  les..*/.SQLITE_P
15f1d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
15f1e 65 33 42 74 72 65 65 53 79 6e 63 44 69 73 61 62  e3BtreeSyncDisab
15f1f 6c 65 64 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  led(Btree *p){. 
15f20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
15f21 20 70 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72   p->pBt;.  int r
15f22 63 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  c;.  assert( sql
15f23 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
15f24 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
15f25 20 20 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65    .  sqlite3Btre
15f26 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73  eEnter(p);.  ass
15f27 65 72 74 28 20 70 42 74 20 26 26 20 70 42 74 2d  ert( pBt && pBt-
15f28 3e 70 50 61 67 65 72 20 29 3b 0a 20 20 72 63 20  >pPager );.  rc 
15f29 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4e 6f  = sqlite3PagerNo
15f2a 73 79 6e 63 28 70 42 74 2d 3e 70 50 61 67 65 72  sync(pBt->pPager
15f2b 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
15f2c 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
15f2d 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21  urn rc;.}..#if !
15f2e 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
15f2f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  MIT_PAGER_PRAGMA
15f30 53 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53  S) || !defined(S
15f31 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55  QLITE_OMIT_VACUU
15f32 4d 29 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  M)./*.** Change 
15f33 74 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65  the default page
15f34 73 20 73 69 7a 65 20 61 6e 64 20 74 68 65 20 6e  s size and the n
15f35 75 6d 62 65 72 20 6f 66 20 72 65 73 65 72 76 65  umber of reserve
15f36 64 20 62 79 74 65 73 20 70 65 72 20 70 61 67 65  d bytes per page
15f37 2e 0a 2a 2a 20 4f 72 2c 20 69 66 20 74 68 65 20  ..** Or, if the 
15f38 70 61 67 65 20 73 69 7a 65 20 68 61 73 20 61 6c  page size has al
15f39 72 65 61 64 79 20 62 65 65 6e 20 66 69 78 65 64  ready been fixed
15f3a 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
15f3b 52 45 41 44 4f 4e 4c 59 20 0a 2a 2a 20 77 69 74  READONLY .** wit
15f3c 68 6f 75 74 20 63 68 61 6e 67 69 6e 67 20 61 6e  hout changing an
15f3d 79 74 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68  ything..**.** Th
15f3e 65 20 70 61 67 65 20 73 69 7a 65 20 6d 75 73 74  e page size must
15f3f 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32   be a power of 2
15f40 20 62 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64   between 512 and
15f41 20 36 35 35 33 36 2e 20 20 49 66 20 74 68 65 20   65536.  If the 
15f42 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20 73 75 70  page.** size sup
15f43 70 6c 69 65 64 20 64 6f 65 73 20 6e 6f 74 20 6d  plied does not m
15f44 65 65 74 20 74 68 69 73 20 63 6f 6e 73 74 72 61  eet this constra
15f45 69 6e 74 20 74 68 65 6e 20 74 68 65 20 70 61 67  int then the pag
15f46 65 20 73 69 7a 65 20 69 73 20 6e 6f 74 0a 2a 2a  e size is not.**
15f47 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20   changed..**.** 
15f48 50 61 67 65 20 73 69 7a 65 73 20 61 72 65 20 63  Page sizes are c
15f49 6f 6e 73 74 72 61 69 6e 65 64 20 74 6f 20 62 65  onstrained to be
15f4a 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20   a power of two 
15f4b 73 6f 20 74 68 61 74 20 74 68 65 20 72 65 67 69  so that the regi
15f4c 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 64 61 74  on.** of the dat
15f4d 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64 20  abase file used 
15f4e 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 28 62 65 67  for locking (beg
15f4f 69 6e 6e 69 6e 67 20 61 74 20 50 45 4e 44 49 4e  inning at PENDIN
15f50 47 5f 42 59 54 45 2c 0a 2a 2a 20 74 68 65 20 66  G_BYTE,.** the f
15f51 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74  irst byte past t
15f52 68 65 20 31 47 42 20 62 6f 75 6e 64 61 72 79 2c  he 1GB boundary,
15f53 20 30 78 34 30 30 30 30 30 30 30 29 20 6e 65 65   0x40000000) nee
15f54 64 73 20 74 6f 20 6f 63 63 75 72 0a 2a 2a 20 61  ds to occur.** a
15f55 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  t the beginning 
15f56 6f 66 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a  of a page..**.**
15f57 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 6e 52   If parameter nR
15f58 65 73 65 72 76 65 20 69 73 20 6c 65 73 73 20 74  eserve is less t
15f59 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74  han zero, then t
15f5a 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73  he number of res
15f5b 65 72 76 65 64 0a 2a 2a 20 62 79 74 65 73 20 70  erved.** bytes p
15f5c 65 72 20 70 61 67 65 20 69 73 20 6c 65 66 74 20  er page is left 
15f5d 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a  unchanged..**.**
15f5e 20 49 66 20 74 68 65 20 69 46 69 78 21 3d 30 20   If the iFix!=0 
15f5f 74 68 65 6e 20 74 68 65 20 70 61 67 65 53 69 7a  then the pageSiz
15f60 65 46 69 78 65 64 20 66 6c 61 67 20 69 73 20 73  eFixed flag is s
15f61 65 74 20 73 6f 20 74 68 61 74 20 74 68 65 20 70  et so that the p
15f62 61 67 65 20 73 69 7a 65 0a 2a 2a 20 61 6e 64 20  age size.** and 
15f63 61 75 74 6f 76 61 63 75 75 6d 20 6d 6f 64 65 20  autovacuum mode 
15f64 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65  can no longer be
15f65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c   changed..*/.SQL
15f66 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
15f67 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 50  sqlite3BtreeSetP
15f68 61 67 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70  ageSize(Btree *p
15f69 2c 20 69 6e 74 20 70 61 67 65 53 69 7a 65 2c 20  , int pageSize, 
15f6a 69 6e 74 20 6e 52 65 73 65 72 76 65 2c 20 69 6e  int nReserve, in
15f6b 74 20 69 46 69 78 29 7b 0a 20 20 69 6e 74 20 72  t iFix){.  int r
15f6c 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
15f6d 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
15f6e 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72   p->pBt;.  asser
15f6f 74 28 20 6e 52 65 73 65 72 76 65 3e 3d 2d 31 20  t( nReserve>=-1 
15f70 26 26 20 6e 52 65 73 65 72 76 65 3c 3d 32 35 35  && nReserve<=255
15f71 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72   );.  sqlite3Btr
15f72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66  eeEnter(p);.  if
15f73 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46  ( pBt->pageSizeF
15f74 69 78 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69  ixed ){.    sqli
15f75 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
15f76 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
15f77 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20  ITE_READONLY;.  
15f78 7d 0a 20 20 69 66 28 20 6e 52 65 73 65 72 76 65  }.  if( nReserve
15f79 3c 30 20 29 7b 0a 20 20 20 20 6e 52 65 73 65 72  <0 ){.    nReser
15f7a 76 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69  ve = pBt->pageSi
15f7b 7a 65 20 2d 20 70 42 74 2d 3e 75 73 61 62 6c 65  ze - pBt->usable
15f7c 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65  Size;.  }.  asse
15f7d 72 74 28 20 6e 52 65 73 65 72 76 65 3e 3d 30 20  rt( nReserve>=0 
15f7e 26 26 20 6e 52 65 73 65 72 76 65 3c 3d 32 35 35  && nReserve<=255
15f7f 20 29 3b 0a 20 20 69 66 28 20 70 61 67 65 53 69   );.  if( pageSi
15f80 7a 65 3e 3d 35 31 32 20 26 26 20 70 61 67 65 53  ze>=512 && pageS
15f81 69 7a 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f  ize<=SQLITE_MAX_
15f82 50 41 47 45 5f 53 49 5a 45 20 26 26 0a 20 20 20  PAGE_SIZE &&.   
15f83 20 20 20 20 20 28 28 70 61 67 65 53 69 7a 65 2d       ((pageSize-
15f84 31 29 26 70 61 67 65 53 69 7a 65 29 3d 3d 30 20  1)&pageSize)==0 
15f85 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  ){.    assert( (
15f86 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30  pageSize & 7)==0
15f87 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
15f88 21 70 42 74 2d 3e 70 50 61 67 65 31 20 26 26 20  !pBt->pPage1 && 
15f89 21 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 29 3b  !pBt->pCursor );
15f8a 0a 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69  .    pBt->pageSi
15f8b 7a 65 20 3d 20 28 75 31 36 29 70 61 67 65 53 69  ze = (u16)pageSi
15f8c 7a 65 3b 0a 20 20 20 20 66 72 65 65 54 65 6d 70  ze;.    freeTemp
15f8d 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 7d 0a  Space(pBt);.  }.
15f8e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
15f8f 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 70  gerSetPagesize(p
15f90 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74  Bt->pPager, &pBt
15f91 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6e 52 65 73  ->pageSize, nRes
15f92 65 72 76 65 29 3b 0a 20 20 70 42 74 2d 3e 75 73  erve);.  pBt->us
15f93 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e  ableSize = pBt->
15f94 70 61 67 65 53 69 7a 65 20 2d 20 28 75 31 36 29  pageSize - (u16)
15f95 6e 52 65 73 65 72 76 65 3b 0a 20 20 69 66 28 20  nReserve;.  if( 
15f96 69 46 69 78 20 29 20 70 42 74 2d 3e 70 61 67 65  iFix ) pBt->page
15f97 53 69 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a 20  SizeFixed = 1;. 
15f98 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
15f99 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ve(p);.  return 
15f9a 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  rc;.}../*.** Ret
15f9b 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e 74 6c  urn the currentl
15f9c 79 20 64 65 66 69 6e 65 64 20 70 61 67 65 20 73  y defined page s
15f9d 69 7a 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ize.*/.SQLITE_PR
15f9e 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
15f9f 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a  3BtreeGetPageSiz
15fa0 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72  e(Btree *p){.  r
15fa1 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 61  eturn p->pBt->pa
15fa2 67 65 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  geSize;.}../*.**
15fa3 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
15fa4 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73  er of bytes of s
15fa5 70 61 63 65 20 61 74 20 74 68 65 20 65 6e 64 20  pace at the end 
15fa6 6f 66 20 65 76 65 72 79 20 70 61 67 65 20 74 68  of every page th
15fa7 61 74 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e 74  at.** are intent
15fa8 75 61 6c 6c 79 20 6c 65 66 74 20 75 6e 75 73 65  ually left unuse
15fa9 64 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20  d.  This is the 
15faa 22 72 65 73 65 72 76 65 64 22 20 73 70 61 63 65  "reserved" space
15fab 20 74 68 61 74 20 69 73 0a 2a 2a 20 73 6f 6d 65   that is.** some
15fac 74 69 6d 65 73 20 75 73 65 64 20 62 79 20 65 78  times used by ex
15fad 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c  tensions..*/.SQL
15fae 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
15faf 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 52  sqlite3BtreeGetR
15fb0 65 73 65 72 76 65 28 42 74 72 65 65 20 2a 70 29  eserve(Btree *p)
15fb1 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c  {.  int n;.  sql
15fb2 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
15fb3 29 3b 0a 20 20 6e 20 3d 20 70 2d 3e 70 42 74 2d  );.  n = p->pBt-
15fb4 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 2d 3e 70  >pageSize - p->p
15fb5 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a  Bt->usableSize;.
15fb6 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
15fb7 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
15fb8 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74   n;.}../*.** Set
15fb9 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67   the maximum pag
15fba 65 20 63 6f 75 6e 74 20 66 6f 72 20 61 20 64 61  e count for a da
15fbb 74 61 62 61 73 65 20 69 66 20 6d 78 50 61 67 65  tabase if mxPage
15fbc 20 69 73 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2a   is positive..**
15fbd 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 72 65 20   No changes are 
15fbe 6d 61 64 65 20 69 66 20 6d 78 50 61 67 65 20 69  made if mxPage i
15fbf 73 20 30 20 6f 72 20 6e 65 67 61 74 69 76 65 2e  s 0 or negative.
15fc0 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f  .** Regardless o
15fc1 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d  f the value of m
15fc2 78 50 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68  xPage, return th
15fc3 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63  e maximum page c
15fc4 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ount..*/.SQLITE_
15fc5 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
15fc6 74 65 33 42 74 72 65 65 4d 61 78 50 61 67 65 43  te3BtreeMaxPageC
15fc7 6f 75 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 69  ount(Btree *p, i
15fc8 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 69 6e  nt mxPage){.  in
15fc9 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  t n;.  sqlite3Bt
15fca 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 6e  reeEnter(p);.  n
15fcb 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d   = sqlite3PagerM
15fcc 61 78 50 61 67 65 43 6f 75 6e 74 28 70 2d 3e 70  axPageCount(p->p
15fcd 42 74 2d 3e 70 50 61 67 65 72 2c 20 6d 78 50 61  Bt->pPager, mxPa
15fce 67 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  ge);.  sqlite3Bt
15fcf 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
15fd0 65 74 75 72 6e 20 6e 3b 0a 7d 0a 23 65 6e 64 69  eturn n;.}.#endi
15fd1 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51  f /* !defined(SQ
15fd2 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
15fd3 50 52 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 66  PRAGMAS) || !def
15fd4 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
15fd5 5f 56 41 43 55 55 4d 29 20 2a 2f 0a 0a 2f 2a 0a  _VACUUM) */../*.
15fd6 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 27 61  ** Change the 'a
15fd7 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f 70  uto-vacuum' prop
15fd8 65 72 74 79 20 6f 66 20 74 68 65 20 64 61 74 61  erty of the data
15fd9 62 61 73 65 2e 20 49 66 20 74 68 65 20 27 61 75  base. If the 'au
15fda 74 6f 56 61 63 75 75 6d 27 0a 2a 2a 20 70 61 72  toVacuum'.** par
15fdb 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65  ameter is non-ze
15fdc 72 6f 2c 20 74 68 65 6e 20 61 75 74 6f 2d 76 61  ro, then auto-va
15fdd 63 75 75 6d 20 6d 6f 64 65 20 69 73 20 65 6e 61  cuum mode is ena
15fde 62 6c 65 64 2e 20 49 66 20 7a 65 72 6f 2c 20 69  bled. If zero, i
15fdf 74 0a 2a 2a 20 69 73 20 64 69 73 61 62 6c 65 64  t.** is disabled
15fe0 2e 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61  . The default va
15fe1 6c 75 65 20 66 6f 72 20 74 68 65 20 61 75 74 6f  lue for the auto
15fe2 2d 76 61 63 75 75 6d 20 70 72 6f 70 65 72 74 79  -vacuum property
15fe3 20 69 73 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e   is .** determin
15fe4 65 64 20 62 79 20 74 68 65 20 53 51 4c 49 54 45  ed by the SQLITE
15fe5 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43  _DEFAULT_AUTOVAC
15fe6 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 53 51  UUM macro..*/.SQ
15fe7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
15fe8 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
15fe9 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65  AutoVacuum(Btree
15fea 20 2a 70 2c 20 69 6e 74 20 61 75 74 6f 56 61 63   *p, int autoVac
15feb 75 75 6d 29 7b 0a 23 69 66 64 65 66 20 53 51 4c  uum){.#ifdef SQL
15fec 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
15fed 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  UUM.  return SQL
15fee 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 23 65  ITE_READONLY;.#e
15fef 6c 73 65 0a 20 20 42 74 53 68 61 72 65 64 20 2a  lse.  BtShared *
15ff0 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
15ff1 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
15ff2 4f 4b 3b 0a 20 20 75 38 20 61 76 20 3d 20 28 75  OK;.  u8 av = (u
15ff3 38 29 61 75 74 6f 56 61 63 75 75 6d 3b 0a 0a 20  8)autoVacuum;.. 
15ff4 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
15ff5 65 72 28 70 29 3b 0a 20 20 69 66 28 20 70 42 74  er(p);.  if( pBt
15ff6 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20  ->pageSizeFixed 
15ff7 26 26 20 28 61 76 20 3f 31 3a 30 29 21 3d 70 42  && (av ?1:0)!=pB
15ff8 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b  t->autoVacuum ){
15ff9 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
15ffa 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 65 6c  _READONLY;.  }el
15ffb 73 65 7b 0a 20 20 20 20 70 42 74 2d 3e 61 75 74  se{.    pBt->aut
15ffc 6f 56 61 63 75 75 6d 20 3d 20 61 76 20 3f 31 3a  oVacuum = av ?1:
15ffd 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 63 72  0;.    pBt->incr
15ffe 56 61 63 75 75 6d 20 3d 20 61 76 3d 3d 32 20 3f  Vacuum = av==2 ?
15fff 31 3a 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  1:0;.  }.  sqlit
16000 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
16001 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65  .  return rc;.#e
16002 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ndif.}../*.** Re
16003 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f  turn the value o
16004 66 20 74 68 65 20 27 61 75 74 6f 2d 76 61 63 75  f the 'auto-vacu
16005 75 6d 27 20 70 72 6f 70 65 72 74 79 2e 20 49 66  um' property. If
16006 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 73 20   auto-vacuum is 
16007 0a 2a 2a 20 65 6e 61 62 6c 65 64 20 31 20 69 73  .** enabled 1 is
16008 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72   returned. Other
16009 77 69 73 65 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54  wise 0..*/.SQLIT
1600a 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1600b 6c 69 74 65 33 42 74 72 65 65 47 65 74 41 75 74  lite3BtreeGetAut
1600c 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 70  oVacuum(Btree *p
1600d 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ){.#ifdef SQLITE
1600e 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
1600f 0a 20 20 72 65 74 75 72 6e 20 42 54 52 45 45 5f  .  return BTREE_
16010 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 3b  AUTOVACUUM_NONE;
16011 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 72 63 3b  .#else.  int rc;
16012 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
16013 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20  nter(p);.  rc = 
16014 28 0a 20 20 20 20 28 21 70 2d 3e 70 42 74 2d 3e  (.    (!p->pBt->
16015 61 75 74 6f 56 61 63 75 75 6d 29 3f 42 54 52 45  autoVacuum)?BTRE
16016 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e  E_AUTOVACUUM_NON
16017 45 3a 0a 20 20 20 20 28 21 70 2d 3e 70 42 74 2d  E:.    (!p->pBt-
16018 3e 69 6e 63 72 56 61 63 75 75 6d 29 3f 42 54 52  >incrVacuum)?BTR
16019 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 46 55  EE_AUTOVACUUM_FU
1601a 4c 4c 3a 0a 20 20 20 20 42 54 52 45 45 5f 41 55  LL:.    BTREE_AU
1601b 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52 0a 20 20  TOVACUUM_INCR.  
1601c 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
1601d 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
1601e 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d  urn rc;.#endif.}
1601f 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 72  .../*.** Get a r
16020 65 66 65 72 65 6e 63 65 20 74 6f 20 70 50 61 67  eference to pPag
16021 65 31 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  e1 of the databa
16022 73 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20 77  se file.  This w
16023 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 61 63 71 75  ill.** also acqu
16024 69 72 65 20 61 20 72 65 61 64 6c 6f 63 6b 20 6f  ire a readlock o
16025 6e 20 74 68 61 74 20 66 69 6c 65 2e 0a 2a 2a 0a  n that file..**.
16026 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  ** SQLITE_OK is 
16027 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63  returned on succ
16028 65 73 73 2e 20 20 49 66 20 74 68 65 20 66 69 6c  ess.  If the fil
16029 65 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 77 65  e is not a.** we
1602a 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61  ll-formed databa
1602b 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 53 51  se file, then SQ
1602c 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69 73 20  LITE_CORRUPT is 
1602d 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 53 51 4c  returned..** SQL
1602e 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 75  ITE_BUSY is retu
1602f 72 6e 65 64 20 69 66 20 74 68 65 20 64 61 74 61  rned if the data
16030 62 61 73 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20  base is locked. 
16031 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a   SQLITE_NOMEM.**
16032 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
16033 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65  we run out of me
16034 6d 6f 72 79 2e 20 0a 2a 2f 0a 73 74 61 74 69 63  mory. .*/.static
16035 20 69 6e 74 20 6c 6f 63 6b 42 74 72 65 65 28 42   int lockBtree(B
16036 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20  tShared *pBt){. 
16037 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61   int rc;.  MemPa
16038 67 65 20 2a 70 50 61 67 65 31 3b 0a 20 20 69 6e  ge *pPage1;.  in
16039 74 20 6e 50 61 67 65 3b 0a 0a 20 20 61 73 73 65  t nPage;..  asse
1603a 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1603b 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
1603c 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
1603d 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d 30 20 29  pBt->pPage1==0 )
1603e 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1603f 50 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28  PagerSharedLock(
16040 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20  pBt->pPager);.  
16041 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
16042 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
16043 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61   rc = btreeGetPa
16044 67 65 28 70 42 74 2c 20 31 2c 20 26 70 50 61 67  ge(pBt, 1, &pPag
16045 65 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63  e1, 0);.  if( rc
16046 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
16047 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 44  turn rc;..  /* D
16048 6f 20 73 6f 6d 65 20 63 68 65 63 6b 69 6e 67 20  o some checking 
16049 74 6f 20 68 65 6c 70 20 69 6e 73 75 72 65 20 74  to help insure t
1604a 68 65 20 66 69 6c 65 20 77 65 20 6f 70 65 6e 65  he file we opene
1604b 64 20 72 65 61 6c 6c 79 20 69 73 0a 20 20 2a 2a  d really is.  **
1604c 20 61 20 76 61 6c 69 64 20 64 61 74 61 62 61 73   a valid databas
1604d 65 20 66 69 6c 65 2e 20 0a 20 20 2a 2f 0a 20 20  e file. .  */.  
1604e 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1604f 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e  rPagecount(pBt->
16050 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b  pPager, &nPage);
16051 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
16052 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f  E_OK ){.    goto
16053 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c   page1_init_fail
16054 65 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ed;.  }else if( 
16055 6e 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 69  nPage>0 ){.    i
16056 6e 74 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20  nt pageSize;.   
16057 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b   int usableSize;
16058 0a 20 20 20 20 75 38 20 2a 70 61 67 65 31 20 3d  .    u8 *page1 =
16059 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61 3b 0a   pPage1->aData;.
1605a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1605b 4e 4f 54 41 44 42 3b 0a 20 20 20 20 69 66 28 20  NOTADB;.    if( 
1605c 6d 65 6d 63 6d 70 28 70 61 67 65 31 2c 20 7a 4d  memcmp(page1, zM
1605d 61 67 69 63 48 65 61 64 65 72 2c 20 31 36 29 21  agicHeader, 16)!
1605e 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  =0 ){.      goto
1605f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c   page1_init_fail
16060 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ed;.    }.    if
16061 28 20 70 61 67 65 31 5b 31 38 5d 3e 31 20 29 7b  ( page1[18]>1 ){
16062 0a 20 20 20 20 20 20 70 42 74 2d 3e 72 65 61 64  .      pBt->read
16063 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  Only = 1;.    }.
16064 20 20 20 20 69 66 28 20 70 61 67 65 31 5b 31 39      if( page1[19
16065 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  ]>1 ){.      got
16066 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69  o page1_init_fai
16067 6c 65 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  led;.    }..    
16068 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 65  /* The maximum e
16069 6d 62 65 64 64 65 64 20 66 72 61 63 74 69 6f 6e  mbedded fraction
1606a 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c 79   must be exactly
1606b 20 32 35 25 2e 20 20 41 6e 64 20 74 68 65 20 6d   25%.  And the m
1606c 69 6e 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 65 6d  inimum.    ** em
1606d 62 65 64 64 65 64 20 66 72 61 63 74 69 6f 6e 20  bedded fraction 
1606e 6d 75 73 74 20 62 65 20 31 32 2e 35 25 20 66 6f  must be 12.5% fo
1606f 72 20 62 6f 74 68 20 6c 65 61 66 2d 64 61 74 61  r both leaf-data
16070 20 61 6e 64 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61   and non-leaf-da
16071 74 61 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 6f  ta..    ** The o
16072 72 69 67 69 6e 61 6c 20 64 65 73 69 67 6e 20 61  riginal design a
16073 6c 6c 6f 77 65 64 20 74 68 65 73 65 20 61 6d 6f  llowed these amo
16074 75 6e 74 73 20 74 6f 20 76 61 72 79 2c 20 62 75  unts to vary, bu
16075 74 20 61 73 20 6f 66 0a 20 20 20 20 2a 2a 20 76  t as of.    ** v
16076 65 72 73 69 6f 6e 20 33 2e 36 2e 30 2c 20 77 65  ersion 3.6.0, we
16077 20 72 65 71 75 69 72 65 20 74 68 65 6d 20 74 6f   require them to
16078 20 62 65 20 66 69 78 65 64 2e 0a 20 20 20 20 2a   be fixed..    *
16079 2f 0a 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70  /.    if( memcmp
1607a 28 26 70 61 67 65 31 5b 32 31 5d 2c 20 22 5c 31  (&page1[21], "\1
1607b 30 30 5c 30 34 30 5c 30 34 30 22 2c 33 29 21 3d  00\040\040",3)!=
1607c 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  0 ){.      goto 
1607d 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65  page1_init_faile
1607e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 61 67  d;.    }.    pag
1607f 65 53 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65  eSize = get2byte
16080 28 26 70 61 67 65 31 5b 31 36 5d 29 3b 0a 20 20  (&page1[16]);.  
16081 20 20 69 66 28 20 28 28 70 61 67 65 53 69 7a 65    if( ((pageSize
16082 2d 31 29 26 70 61 67 65 53 69 7a 65 29 21 3d 30  -1)&pageSize)!=0
16083 20 7c 7c 20 70 61 67 65 53 69 7a 65 3c 35 31 32   || pageSize<512
16084 20 7c 7c 0a 20 20 20 20 20 20 20 20 28 53 51 4c   ||.        (SQL
16085 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a  ITE_MAX_PAGE_SIZ
16086 45 3c 33 32 37 36 38 20 26 26 20 70 61 67 65 53  E<32768 && pageS
16087 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50  ize>SQLITE_MAX_P
16088 41 47 45 5f 53 49 5a 45 29 0a 20 20 20 20 29 7b  AGE_SIZE).    ){
16089 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65  .      goto page
1608a 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20  1_init_failed;. 
1608b 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
1608c 20 28 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d   (pageSize & 7)=
1608d 3d 30 20 29 3b 0a 20 20 20 20 75 73 61 62 6c 65  =0 );.    usable
1608e 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 20  Size = pageSize 
1608f 2d 20 70 61 67 65 31 5b 32 30 5d 3b 0a 20 20 20  - page1[20];.   
16090 20 69 66 28 20 70 61 67 65 53 69 7a 65 21 3d 70   if( pageSize!=p
16091 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a  Bt->pageSize ){.
16092 20 20 20 20 20 20 2f 2a 20 41 66 74 65 72 20 72        /* After r
16093 65 61 64 69 6e 67 20 74 68 65 20 66 69 72 73 74  eading the first
16094 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74   page of the dat
16095 61 62 61 73 65 20 61 73 73 75 6d 69 6e 67 20 61  abase assuming a
16096 20 70 61 67 65 20 73 69 7a 65 0a 20 20 20 20 20   page size.     
16097 20 2a 2a 20 6f 66 20 42 74 53 68 61 72 65 64 2e   ** of BtShared.
16098 70 61 67 65 53 69 7a 65 2c 20 77 65 20 68 61 76  pageSize, we hav
16099 65 20 64 69 73 63 6f 76 65 72 65 64 20 74 68 61  e discovered tha
1609a 74 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20  t the page-size 
1609b 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 63 74 75  is.      ** actu
1609c 61 6c 6c 79 20 70 61 67 65 53 69 7a 65 2e 20 55  ally pageSize. U
1609d 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61  nlock the databa
1609e 73 65 2c 20 6c 65 61 76 65 20 70 42 74 2d 3e 70  se, leave pBt->p
1609f 50 61 67 65 31 20 61 74 0a 20 20 20 20 20 20 2a  Page1 at.      *
160a0 2a 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72  * zero and retur
160a1 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 54 68 65  n SQLITE_OK. The
160a2 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 63 61 6c   caller will cal
160a3 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a  l this function.
160a4 20 20 20 20 20 20 2a 2a 20 61 67 61 69 6e 20 77        ** again w
160a5 69 74 68 20 74 68 65 20 63 6f 72 72 65 63 74 20  ith the correct 
160a6 70 61 67 65 2d 73 69 7a 65 2e 0a 20 20 20 20 20  page-size..     
160a7 20 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65 61 73   */.      releas
160a8 65 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a 20  ePage(pPage1);. 
160a9 20 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65       pBt->usable
160aa 53 69 7a 65 20 3d 20 28 75 31 36 29 75 73 61 62  Size = (u16)usab
160ab 6c 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 70 42  leSize;.      pB
160ac 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75  t->pageSize = (u
160ad 31 36 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20  16)pageSize;.   
160ae 20 20 20 66 72 65 65 54 65 6d 70 53 70 61 63 65     freeTempSpace
160af 28 70 42 74 29 3b 0a 20 20 20 20 20 20 72 63 20  (pBt);.      rc 
160b0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65  = sqlite3PagerSe
160b1 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70  tPagesize(pBt->p
160b2 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67  Pager, &pBt->pag
160b3 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 20  eSize,.         
160b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
160b5 20 20 20 20 20 20 20 20 20 20 70 61 67 65 53 69            pageSi
160b6 7a 65 2d 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a  ze-usableSize);.
160b7 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
160b8 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75  .    }.    if( u
160b9 73 61 62 6c 65 53 69 7a 65 3c 34 38 30 20 29 7b  sableSize<480 ){
160ba 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65  .      goto page
160bb 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20  1_init_failed;. 
160bc 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 70 61     }.    pBt->pa
160bd 67 65 53 69 7a 65 20 3d 20 28 75 31 36 29 70 61  geSize = (u16)pa
160be 67 65 53 69 7a 65 3b 0a 20 20 20 20 70 42 74 2d  geSize;.    pBt-
160bf 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 28 75  >usableSize = (u
160c0 31 36 29 75 73 61 62 6c 65 53 69 7a 65 3b 0a 23  16)usableSize;.#
160c1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
160c2 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
160c3 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75    pBt->autoVacuu
160c4 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 70  m = (get4byte(&p
160c5 61 67 65 31 5b 33 36 20 2b 20 34 2a 34 5d 29 3f  age1[36 + 4*4])?
160c6 31 3a 30 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69  1:0);.    pBt->i
160c7 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 67 65 74  ncrVacuum = (get
160c8 34 62 79 74 65 28 26 70 61 67 65 31 5b 33 36 20  4byte(&page1[36 
160c9 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65  + 7*4])?1:0);.#e
160ca 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 6d  ndif.  }..  /* m
160cb 61 78 4c 6f 63 61 6c 20 69 73 20 74 68 65 20 6d  axLocal is the m
160cc 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66  aximum amount of
160cd 20 70 61 79 6c 6f 61 64 20 74 6f 20 73 74 6f 72   payload to stor
160ce 65 20 6c 6f 63 61 6c 6c 79 20 66 6f 72 0a 20 20  e locally for.  
160cf 2a 2a 20 61 20 63 65 6c 6c 2e 20 20 4d 61 6b 65  ** a cell.  Make
160d0 20 73 75 72 65 20 69 74 20 69 73 20 73 6d 61 6c   sure it is smal
160d1 6c 20 65 6e 6f 75 67 68 20 73 6f 20 74 68 61 74  l enough so that
160d2 20 61 74 20 6c 65 61 73 74 20 6d 69 6e 46 61 6e   at least minFan
160d3 6f 75 74 0a 20 20 2a 2a 20 63 65 6c 6c 73 20 63  out.  ** cells c
160d4 61 6e 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 6f  an will fit on o
160d5 6e 65 20 70 61 67 65 2e 20 20 57 65 20 61 73 73  ne page.  We ass
160d6 75 6d 65 20 61 20 31 30 2d 62 79 74 65 20 70 61  ume a 10-byte pa
160d7 67 65 20 68 65 61 64 65 72 2e 0a 20 20 2a 2a 20  ge header..  ** 
160d8 42 65 73 69 64 65 73 20 74 68 65 20 70 61 79 6c  Besides the payl
160d9 6f 61 64 2c 20 74 68 65 20 63 65 6c 6c 20 6d 75  oad, the cell mu
160da 73 74 20 73 74 6f 72 65 3a 0a 20 20 2a 2a 20 20  st store:.  **  
160db 20 20 20 32 2d 62 79 74 65 20 70 6f 69 6e 74 65     2-byte pointe
160dc 72 20 74 6f 20 74 68 65 20 63 65 6c 6c 0a 20 20  r to the cell.  
160dd 2a 2a 20 20 20 20 20 34 2d 62 79 74 65 20 63 68  **     4-byte ch
160de 69 6c 64 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a  ild pointer.  **
160df 20 20 20 20 20 39 2d 62 79 74 65 20 6e 4b 65 79       9-byte nKey
160e0 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20   value.  **     
160e1 34 2d 62 79 74 65 20 6e 44 61 74 61 20 76 61 6c  4-byte nData val
160e2 75 65 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79  ue.  **     4-by
160e3 74 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  te overflow page
160e4 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 53 6f   pointer.  ** So
160e5 20 61 20 63 65 6c 6c 20 63 6f 6e 73 69 73 74 73   a cell consists
160e6 20 6f 66 20 61 20 32 2d 62 79 74 65 20 70 6f 69   of a 2-byte poi
160e7 6e 65 72 2c 20 61 20 68 65 61 64 65 72 20 77 68  ner, a header wh
160e8 69 63 68 20 69 73 20 61 73 20 6d 75 63 68 20 61  ich is as much a
160e9 73 0a 20 20 2a 2a 20 31 37 20 62 79 74 65 73 20  s.  ** 17 bytes 
160ea 6c 6f 6e 67 2c 20 30 20 74 6f 20 4e 20 62 79 74  long, 0 to N byt
160eb 65 73 20 6f 66 20 70 61 79 6c 6f 61 64 2c 20 61  es of payload, a
160ec 6e 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 34  nd an optional 4
160ed 20 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 0a 20   byte overflow. 
160ee 20 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 65 72   ** page pointer
160ef 2e 0a 20 20 2a 2f 0a 20 20 70 42 74 2d 3e 6d 61  ..  */.  pBt->ma
160f0 78 4c 6f 63 61 6c 20 3d 20 28 70 42 74 2d 3e 75  xLocal = (pBt->u
160f1 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a 36 34  sableSize-12)*64
160f2 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 70 42 74  /255 - 23;.  pBt
160f3 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 28 70 42  ->minLocal = (pB
160f4 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32  t->usableSize-12
160f5 29 2a 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20  )*32/255 - 23;. 
160f6 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20 3d 20   pBt->maxLeaf = 
160f7 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
160f8 2d 20 33 35 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e  - 35;.  pBt->min
160f9 4c 65 61 66 20 3d 20 28 70 42 74 2d 3e 75 73 61  Leaf = (pBt->usa
160fa 62 6c 65 53 69 7a 65 2d 31 32 29 2a 33 32 2f 32  bleSize-12)*32/2
160fb 35 35 20 2d 20 32 33 3b 0a 20 20 61 73 73 65 72  55 - 23;.  asser
160fc 74 28 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20  t( pBt->maxLeaf 
160fd 2b 20 32 33 20 3c 3d 20 4d 58 5f 43 45 4c 4c 5f  + 23 <= MX_CELL_
160fe 53 49 5a 45 28 70 42 74 29 20 29 3b 0a 20 20 70  SIZE(pBt) );.  p
160ff 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 70 50 61  Bt->pPage1 = pPa
16100 67 65 31 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ge1;.  return SQ
16101 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65 31 5f  LITE_OK;..page1_
16102 69 6e 69 74 5f 66 61 69 6c 65 64 3a 0a 20 20 72  init_failed:.  r
16103 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65  eleasePage(pPage
16104 31 29 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65  1);.  pBt->pPage
16105 31 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  1 = 0;.  return 
16106 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  rc;.}../*.** If 
16107 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74  there are no out
16108 73 74 61 6e 64 69 6e 67 20 63 75 72 73 6f 72 73  standing cursors
16109 20 61 6e 64 20 77 65 20 61 72 65 20 6e 6f 74 20   and we are not 
1610a 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 0a 2a 2a  in the middle.**
1610b 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f   of a transactio
1610c 6e 20 62 75 74 20 74 68 65 72 65 20 69 73 20 61  n but there is a
1610d 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68   read lock on th
1610e 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e  e database, then
1610f 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
16110 20 75 6e 72 65 66 73 20 74 68 65 20 66 69 72 73   unrefs the firs
16111 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61  t page of the da
16112 74 61 62 61 73 65 20 66 69 6c 65 20 77 68 69 63  tabase file whic
16113 68 20 0a 2a 2a 20 68 61 73 20 74 68 65 20 65 66  h .** has the ef
16114 66 65 63 74 20 6f 66 20 72 65 6c 65 61 73 69 6e  fect of releasin
16115 67 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e  g the read lock.
16116 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  .**.** If there 
16117 69 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  is a transaction
16118 20 69 6e 20 70 72 6f 67 72 65 73 73 2c 20 74 68   in progress, th
16119 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
1611a 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63  no-op..*/.static
1611b 20 76 6f 69 64 20 75 6e 6c 6f 63 6b 42 74 72 65   void unlockBtre
1611c 65 49 66 55 6e 75 73 65 64 28 42 74 53 68 61 72  eIfUnused(BtShar
1611d 65 64 20 2a 70 42 74 29 7b 0a 20 20 61 73 73 65  ed *pBt){.  asse
1611e 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1611f 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
16120 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
16121 70 42 74 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20  pBt->pCursor==0 
16122 7c 7c 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61  || pBt->inTransa
16123 63 74 69 6f 6e 3e 54 52 41 4e 53 5f 4e 4f 4e 45  ction>TRANS_NONE
16124 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 69   );.  if( pBt->i
16125 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52  nTransaction==TR
16126 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 70 42 74 2d  ANS_NONE && pBt-
16127 3e 70 50 61 67 65 31 21 3d 30 20 29 7b 0a 20 20  >pPage1!=0 ){.  
16128 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70    assert( pBt->p
16129 50 61 67 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a  Page1->aData );.
1612a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
1612b 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74  te3PagerRefcount
1612c 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3d 3d 31  (pBt->pPager)==1
1612d 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
1612e 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61  pBt->pPage1->aDa
1612f 74 61 20 29 3b 0a 20 20 20 20 72 65 6c 65 61 73  ta );.    releas
16130 65 50 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65  ePage(pBt->pPage
16131 31 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 50 61  1);.    pBt->pPa
16132 67 65 31 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a  ge1 = 0;.  }.}..
16133 2f 2a 0a 2a 2a 20 49 66 20 70 42 74 20 70 6f 69  /*.** If pBt poi
16134 6e 74 73 20 74 6f 20 61 6e 20 65 6d 70 74 79 20  nts to an empty 
16135 66 69 6c 65 20 74 68 65 6e 20 63 6f 6e 76 65 72  file then conver
16136 74 20 74 68 61 74 20 65 6d 70 74 79 20 66 69 6c  t that empty fil
16137 65 0a 2a 2a 20 69 6e 74 6f 20 61 20 6e 65 77 20  e.** into a new 
16138 65 6d 70 74 79 20 64 61 74 61 62 61 73 65 20 62  empty database b
16139 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74  y initializing t
1613a 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f 66  he first page of
1613b 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
1613c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1613d 6e 65 77 44 61 74 61 62 61 73 65 28 42 74 53 68  newDatabase(BtSh
1613e 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 4d 65  ared *pBt){.  Me
1613f 6d 50 61 67 65 20 2a 70 50 31 3b 0a 20 20 75 6e  mPage *pP1;.  un
16140 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74  signed char *dat
16141 61 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69  a;.  int rc;.  i
16142 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 61 73 73  nt nPage;..  ass
16143 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
16144 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74  ex_held(pBt->mut
16145 65 78 29 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20  ex) );.  /* The 
16146 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 68 61  database size ha
16147 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6d  s already been m
16148 65 61 73 75 72 65 64 20 61 6e 64 20 63 61 63 68  easured and cach
16149 65 64 2c 20 73 6f 20 66 61 69 6c 75 72 65 0a 20  ed, so failure. 
1614a 20 2a 2a 20 69 73 20 69 6d 70 6f 73 73 69 62 6c   ** is impossibl
1614b 65 20 68 65 72 65 2e 20 20 49 66 20 74 68 65 20  e here.  If the 
1614c 6f 72 69 67 69 6e 61 6c 20 73 69 7a 65 20 6d 65  original size me
1614d 61 73 75 72 65 6d 65 6e 74 20 66 61 69 6c 65 64  asurement failed
1614e 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 70 72 6f 63  , then.  ** proc
1614f 65 73 73 69 6e 67 20 61 62 6f 72 74 73 20 62 65  essing aborts be
16150 66 6f 72 65 20 65 6e 74 65 72 69 6e 67 20 74 68  fore entering th
16151 69 73 20 72 6f 75 74 69 6e 65 2e 20 2a 2f 0a 20  is routine. */. 
16152 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
16153 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d  erPagecount(pBt-
16154 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29  >pPager, &nPage)
16155 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 63  ;.  if( NEVER(rc
16156 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 7c 7c 20  !=SQLITE_OK) || 
16157 6e 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 72  nPage>0 ){.    r
16158 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
16159 70 50 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65  pP1 = pBt->pPage
1615a 31 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 31  1;.  assert( pP1
1615b 21 3d 30 20 29 3b 0a 20 20 64 61 74 61 20 3d 20  !=0 );.  data = 
1615c 70 50 31 2d 3e 61 44 61 74 61 3b 0a 20 20 72 63  pP1->aData;.  rc
1615d 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
1615e 72 69 74 65 28 70 50 31 2d 3e 70 44 62 50 61 67  rite(pP1->pDbPag
1615f 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  e);.  if( rc ) r
16160 65 74 75 72 6e 20 72 63 3b 0a 20 20 6d 65 6d 63  eturn rc;.  memc
16161 70 79 28 64 61 74 61 2c 20 7a 4d 61 67 69 63 48  py(data, zMagicH
16162 65 61 64 65 72 2c 20 73 69 7a 65 6f 66 28 7a 4d  eader, sizeof(zM
16163 61 67 69 63 48 65 61 64 65 72 29 29 3b 0a 20 20  agicHeader));.  
16164 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 7a  assert( sizeof(z
16165 4d 61 67 69 63 48 65 61 64 65 72 29 3d 3d 31 36  MagicHeader)==16
16166 20 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26   );.  put2byte(&
16167 64 61 74 61 5b 31 36 5d 2c 20 70 42 74 2d 3e 70  data[16], pBt->p
16168 61 67 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 61  ageSize);.  data
16169 5b 31 38 5d 20 3d 20 31 3b 0a 20 20 64 61 74 61  [18] = 1;.  data
1616a 5b 31 39 5d 20 3d 20 31 3b 0a 20 20 61 73 73 65  [19] = 1;.  asse
1616b 72 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  rt( pBt->usableS
1616c 69 7a 65 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69  ize<=pBt->pageSi
1616d 7a 65 20 26 26 20 70 42 74 2d 3e 75 73 61 62 6c  ze && pBt->usabl
1616e 65 53 69 7a 65 2b 32 35 35 3e 3d 70 42 74 2d 3e  eSize+255>=pBt->
1616f 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 64 61 74  pageSize);.  dat
16170 61 5b 32 30 5d 20 3d 20 28 75 38 29 28 70 42 74  a[20] = (u8)(pBt
16171 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 42 74  ->pageSize - pBt
16172 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20  ->usableSize);. 
16173 20 64 61 74 61 5b 32 31 5d 20 3d 20 36 34 3b 0a   data[21] = 64;.
16174 20 20 64 61 74 61 5b 32 32 5d 20 3d 20 33 32 3b    data[22] = 32;
16175 0a 20 20 64 61 74 61 5b 32 33 5d 20 3d 20 33 32  .  data[23] = 32
16176 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61  ;.  memset(&data
16177 5b 32 34 5d 2c 20 30 2c 20 31 30 30 2d 32 34 29  [24], 0, 100-24)
16178 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 50 31  ;.  zeroPage(pP1
16179 2c 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46  , PTF_INTKEY|PTF
1617a 5f 4c 45 41 46 7c 50 54 46 5f 4c 45 41 46 44 41  _LEAF|PTF_LEAFDA
1617b 54 41 20 29 3b 0a 20 20 70 42 74 2d 3e 70 61 67  TA );.  pBt->pag
1617c 65 53 69 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a  eSizeFixed = 1;.
1617d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1617e 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
1617f 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75   assert( pBt->au
16180 74 6f 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70  toVacuum==1 || p
16181 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d  Bt->autoVacuum==
16182 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
16183 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d  Bt->incrVacuum==
16184 31 20 7c 7c 20 70 42 74 2d 3e 69 6e 63 72 56 61  1 || pBt->incrVa
16185 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 20 70 75 74  cuum==0 );.  put
16186 34 62 79 74 65 28 26 64 61 74 61 5b 33 36 20 2b  4byte(&data[36 +
16187 20 34 2a 34 5d 2c 20 70 42 74 2d 3e 61 75 74 6f   4*4], pBt->auto
16188 56 61 63 75 75 6d 29 3b 0a 20 20 70 75 74 34 62  Vacuum);.  put4b
16189 79 74 65 28 26 64 61 74 61 5b 33 36 20 2b 20 37  yte(&data[36 + 7
1618a 2a 34 5d 2c 20 70 42 74 2d 3e 69 6e 63 72 56 61  *4], pBt->incrVa
1618b 63 75 75 6d 29 3b 0a 23 65 6e 64 69 66 0a 20 20  cuum);.#endif.  
1618c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1618d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d  ;.}../*.** Attem
1618e 70 74 20 74 6f 20 73 74 61 72 74 20 61 20 6e 65  pt to start a ne
1618f 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41  w transaction. A
16190 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69   write-transacti
16191 6f 6e 0a 2a 2a 20 69 73 20 73 74 61 72 74 65 64  on.** is started
16192 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61   if the second a
16193 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 7a 65  rgument is nonze
16194 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 61 20  ro, otherwise a 
16195 72 65 61 64 2d 0a 2a 2a 20 74 72 61 6e 73 61 63  read-.** transac
16196 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 73 65  tion.  If the se
16197 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
16198 20 32 20 6f 72 20 6d 6f 72 65 20 61 6e 64 20 65   2 or more and e
16199 78 63 6c 75 73 69 76 65 0a 2a 2a 20 74 72 61 6e  xclusive.** tran
1619a 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74  saction is start
1619b 65 64 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74  ed, meaning that
1619c 20 6e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73   no other proces
1619d 73 20 69 73 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20  s is allowed.** 
1619e 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 64 61  to access the da
1619f 74 61 62 61 73 65 2e 20 20 41 20 70 72 65 65 78  tabase.  A preex
161a0 69 73 74 69 6e 67 20 74 72 61 6e 73 61 63 74 69  isting transacti
161a1 6f 6e 20 6d 61 79 20 6e 6f 74 20 62 65 0a 2a 2a  on may not be.**
161a2 20 75 70 67 72 61 64 65 64 20 74 6f 20 65 78 63   upgraded to exc
161a3 6c 75 73 69 76 65 20 62 79 20 63 61 6c 6c 69 6e  lusive by callin
161a4 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61  g this routine a
161a5 20 73 65 63 6f 6e 64 20 74 69 6d 65 20 2d 20 74   second time - t
161a6 68 65 0a 2a 2a 20 65 78 63 6c 75 73 69 76 69 74  he.** exclusivit
161a7 79 20 66 6c 61 67 20 6f 6e 6c 79 20 77 6f 72 6b  y flag only work
161a8 73 20 66 6f 72 20 61 20 6e 65 77 20 74 72 61 6e  s for a new tran
161a9 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41  saction..**.** A
161aa 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69   write-transacti
161ab 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 74  on must be start
161ac 65 64 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70  ed before attemp
161ad 74 69 6e 67 20 61 6e 79 20 0a 2a 2a 20 63 68 61  ting any .** cha
161ae 6e 67 65 73 20 74 6f 20 74 68 65 20 64 61 74 61  nges to the data
161af 62 61 73 65 2e 20 20 4e 6f 6e 65 20 6f 66 20 74  base.  None of t
161b0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75  he following rou
161b1 74 69 6e 65 73 20 0a 2a 2a 20 77 69 6c 6c 20 77  tines .** will w
161b2 6f 72 6b 20 75 6e 6c 65 73 73 20 61 20 74 72 61  ork unless a tra
161b3 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72  nsaction is star
161b4 74 65 64 20 66 69 72 73 74 3a 0a 2a 2a 0a 2a 2a  ted first:.**.**
161b5 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
161b6 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 29 0a  eeCreateTable().
161b7 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  **      sqlite3B
161b8 74 72 65 65 43 72 65 61 74 65 49 6e 64 65 78 28  treeCreateIndex(
161b9 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65  ).**      sqlite
161ba 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65  3BtreeClearTable
161bb 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74  ().**      sqlit
161bc 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65  e3BtreeDropTable
161bd 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74  ().**      sqlit
161be 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 29 0a  e3BtreeInsert().
161bf 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  **      sqlite3B
161c0 74 72 65 65 44 65 6c 65 74 65 28 29 0a 2a 2a 20  treeDelete().** 
161c1 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
161c2 65 55 70 64 61 74 65 4d 65 74 61 28 29 0a 2a 2a  eUpdateMeta().**
161c3 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 69 74 69 61  .** If an initia
161c4 6c 20 61 74 74 65 6d 70 74 20 74 6f 20 61 63 71  l attempt to acq
161c5 75 69 72 65 20 74 68 65 20 6c 6f 63 6b 20 66 61  uire the lock fa
161c6 69 6c 73 20 62 65 63 61 75 73 65 20 6f 66 20 6c  ils because of l
161c7 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 0a 2a  ock contention.*
161c8 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61  * and the databa
161c9 73 65 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c  se was previousl
161ca 79 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 6e  y unlocked, then
161cb 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79   invoke the busy
161cc 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 69 66 20 74   handler.** if t
161cd 68 65 72 65 20 69 73 20 6f 6e 65 2e 20 20 42 75  here is one.  Bu
161ce 74 20 69 66 20 74 68 65 72 65 20 77 61 73 20 70  t if there was p
161cf 72 65 76 69 6f 75 73 6c 79 20 61 20 72 65 61 64  reviously a read
161d0 2d 6c 6f 63 6b 2c 20 64 6f 20 6e 6f 74 0a 2a 2a  -lock, do not.**
161d1 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79   invoke the busy
161d2 20 68 61 6e 64 6c 65 72 20 2d 20 6a 75 73 74 20   handler - just 
161d3 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
161d4 53 59 2e 20 20 53 51 4c 49 54 45 5f 42 55 53 59  SY.  SQLITE_BUSY
161d5 20 69 73 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64   is .** returned
161d6 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 61   when there is a
161d7 6c 72 65 61 64 79 20 61 20 72 65 61 64 2d 6c 6f  lready a read-lo
161d8 63 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 61  ck in order to a
161d9 76 6f 69 64 20 61 20 64 65 61 64 6c 6f 63 6b 2e  void a deadlock.
161da 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 74  .**.** Suppose t
161db 68 65 72 65 20 61 72 65 20 74 77 6f 20 70 72 6f  here are two pro
161dc 63 65 73 73 65 73 20 41 20 61 6e 64 20 42 2e 20  cesses A and B. 
161dd 20 41 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f   A has a read lo
161de 63 6b 20 61 6e 64 20 42 20 68 61 73 0a 2a 2a 20  ck and B has.** 
161df 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2e  a reserved lock.
161e0 20 20 42 20 74 72 69 65 73 20 74 6f 20 70 72 6f    B tries to pro
161e1 6d 6f 74 65 20 74 6f 20 65 78 63 6c 75 73 69 76  mote to exclusiv
161e2 65 20 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 64  e but is blocked
161e3 20 62 65 63 61 75 73 65 0a 2a 2a 20 6f 66 20 41   because.** of A
161e4 27 73 20 72 65 61 64 20 6c 6f 63 6b 2e 20 20 41  's read lock.  A
161e5 20 74 72 69 65 73 20 74 6f 20 70 72 6f 6d 6f 74   tries to promot
161e6 65 20 74 6f 20 72 65 73 65 72 76 65 64 20 62 75  e to reserved bu
161e7 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20  t is blocked by 
161e8 42 2e 0a 2a 2a 20 4f 6e 65 20 6f 72 20 74 68 65  B..** One or the
161e9 20 6f 74 68 65 72 20 6f 66 20 74 68 65 20 74 77   other of the tw
161ea 6f 20 70 72 6f 63 65 73 73 65 73 20 6d 75 73 74  o processes must
161eb 20 67 69 76 65 20 77 61 79 20 6f 72 20 74 68 65   give way or the
161ec 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20 6e 6f 20  re can be.** no 
161ed 70 72 6f 67 72 65 73 73 2e 20 20 42 79 20 72 65  progress.  By re
161ee 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 42  turning SQLITE_B
161ef 55 53 59 20 61 6e 64 20 6e 6f 74 20 69 6e 76 6f  USY and not invo
161f0 6b 69 6e 67 20 74 68 65 20 62 75 73 79 20 63 61  king the busy ca
161f1 6c 6c 62 61 63 6b 0a 2a 2a 20 77 68 65 6e 20 41  llback.** when A
161f2 20 61 6c 72 65 61 64 79 20 68 61 73 20 61 20 72   already has a r
161f3 65 61 64 20 6c 6f 63 6b 2c 20 77 65 20 65 6e 63  ead lock, we enc
161f4 6f 75 72 61 67 65 20 41 20 74 6f 20 67 69 76 65  ourage A to give
161f5 20 75 70 20 61 6e 64 20 6c 65 74 20 42 0a 2a 2a   up and let B.**
161f6 20 70 72 6f 63 65 65 64 2e 0a 2a 2f 0a 53 51 4c   proceed..*/.SQL
161f7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
161f8 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69  sqlite3BtreeBegi
161f9 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70 2c  nTrans(Btree *p,
161fa 20 69 6e 74 20 77 72 66 6c 61 67 29 7b 0a 20 20   int wrflag){.  
161fb 73 71 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b 20  sqlite3 *pBlock 
161fc 3d 20 30 3b 0a 20 20 42 74 53 68 61 72 65 64 20  = 0;.  BtShared 
161fd 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
161fe 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
161ff 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42  _OK;..  sqlite3B
16200 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
16201 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70  btreeIntegrity(p
16202 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
16203 62 74 72 65 65 20 69 73 20 61 6c 72 65 61 64 79  btree is already
16204 20 69 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e   in a write-tran
16205 73 61 63 74 69 6f 6e 2c 20 6f 72 20 69 74 0a 20  saction, or it. 
16206 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20 69   ** is already i
16207 6e 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63  n a read-transac
16208 74 69 6f 6e 20 61 6e 64 20 61 20 72 65 61 64 2d  tion and a read-
16209 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a  transaction.  **
1620a 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 74   is requested, t
1620b 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  his is a no-op..
1620c 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 6e    */.  if( p->in
1620d 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49  Trans==TRANS_WRI
1620e 54 45 20 7c 7c 20 28 70 2d 3e 69 6e 54 72 61 6e  TE || (p->inTran
1620f 73 3d 3d 54 52 41 4e 53 5f 52 45 41 44 20 26 26  s==TRANS_READ &&
16210 20 21 77 72 66 6c 61 67 29 20 29 7b 0a 20 20 20   !wrflag) ){.   
16211 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75   goto trans_begu
16212 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 69  n;.  }..  /* Wri
16213 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  te transactions 
16214 61 72 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65  are not possible
16215 20 6f 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20   on a read-only 
16216 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 66  database */.  if
16217 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20  ( pBt->readOnly 
16218 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20  && wrflag ){.   
16219 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41   rc = SQLITE_REA
1621a 44 4f 4e 4c 59 3b 0a 20 20 20 20 67 6f 74 6f 20  DONLY;.    goto 
1621b 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d  trans_begun;.  }
1621c 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1621d 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
1621e 48 45 0a 20 20 2f 2a 20 49 66 20 61 6e 6f 74 68  HE.  /* If anoth
1621f 65 72 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  er database hand
16220 6c 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 6f  le has already o
16221 70 65 6e 65 64 20 61 20 77 72 69 74 65 20 74 72  pened a write tr
16222 61 6e 73 61 63 74 69 6f 6e 20 0a 20 20 2a 2a 20  ansaction .  ** 
16223 6f 6e 20 74 68 69 73 20 73 68 61 72 65 64 2d 62  on this shared-b
16224 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 61  tree structure a
16225 6e 64 20 61 20 73 65 63 6f 6e 64 20 77 72 69 74  nd a second writ
16226 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  e transaction is
16227 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64 2c  .  ** requested,
16228 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c   return SQLITE_L
16229 4f 43 4b 45 44 2e 0a 20 20 2a 2f 0a 20 20 69 66  OCKED..  */.  if
1622a 28 20 28 77 72 66 6c 61 67 20 26 26 20 70 42 74  ( (wrflag && pBt
1622b 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d  ->inTransaction=
1622c 3d 54 52 41 4e 53 5f 57 52 49 54 45 29 20 7c 7c  =TRANS_WRITE) ||
1622d 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20   pBt->isPending 
1622e 29 7b 0a 20 20 20 20 70 42 6c 6f 63 6b 20 3d 20  ){.    pBlock = 
1622f 70 42 74 2d 3e 70 57 72 69 74 65 72 2d 3e 64 62  pBt->pWriter->db
16230 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 77 72  ;.  }else if( wr
16231 66 6c 61 67 3e 31 20 29 7b 0a 20 20 20 20 42 74  flag>1 ){.    Bt
16232 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 20 20 20  Lock *pIter;.   
16233 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e   for(pIter=pBt->
16234 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49  pLock; pIter; pI
16235 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74  ter=pIter->pNext
16236 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74  ){.      if( pIt
16237 65 72 2d 3e 70 42 74 72 65 65 21 3d 70 20 29 7b  er->pBtree!=p ){
16238 0a 20 20 20 20 20 20 20 20 70 42 6c 6f 63 6b 20  .        pBlock 
16239 3d 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 2d  = pIter->pBtree-
1623a 3e 64 62 3b 0a 20 20 20 20 20 20 20 20 62 72 65  >db;.        bre
1623b 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
1623c 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 42 6c 6f  }.  }.  if( pBlo
1623d 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ck ){.    sqlite
1623e 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b  3ConnectionBlock
1623f 65 64 28 70 2d 3e 64 62 2c 20 70 42 6c 6f 63 6b  ed(p->db, pBlock
16240 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
16241 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44  TE_LOCKED_SHARED
16242 43 41 43 48 45 3b 0a 20 20 20 20 67 6f 74 6f 20  CACHE;.    goto 
16243 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d  trans_begun;.  }
16244 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 6e  .#endif..  /* An
16245 79 20 72 65 61 64 2d 6f 6e 6c 79 20 6f 72 20 72  y read-only or r
16246 65 61 64 2d 77 72 69 74 65 20 74 72 61 6e 73 61  ead-write transa
16247 63 74 69 6f 6e 20 69 6d 70 6c 69 65 73 20 61 20  ction implies a 
16248 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 0a 20 20  read-lock on .  
16249 2a 2a 20 70 61 67 65 20 31 2e 20 53 6f 20 69 66  ** page 1. So if
1624a 20 73 6f 6d 65 20 6f 74 68 65 72 20 73 68 61 72   some other shar
1624b 65 64 2d 63 61 63 68 65 20 63 6c 69 65 6e 74 20  ed-cache client 
1624c 61 6c 72 65 61 64 79 20 68 61 73 20 61 20 77 72  already has a wr
1624d 69 74 65 2d 6c 6f 63 6b 20 0a 20 20 2a 2a 20 6f  ite-lock .  ** o
1624e 6e 20 70 61 67 65 20 31 2c 20 74 68 65 20 74 72  n page 1, the tr
1624f 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 6e 6f 74  ansaction cannot
16250 20 62 65 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20   be opened. */. 
16251 20 72 63 20 3d 20 71 75 65 72 79 53 68 61 72 65   rc = queryShare
16252 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28  dCacheTableLock(
16253 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20  p, MASTER_ROOT, 
16254 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 69 66  READ_LOCK);.  if
16255 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20  ( SQLITE_OK!=rc 
16256 29 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67  ) goto trans_beg
16257 75 6e 3b 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20  un;..  do {.    
16258 2f 2a 20 43 61 6c 6c 20 6c 6f 63 6b 42 74 72 65  /* Call lockBtre
16259 65 28 29 20 75 6e 74 69 6c 20 65 69 74 68 65 72  e() until either
1625a 20 70 42 74 2d 3e 70 50 61 67 65 31 20 69 73 20   pBt->pPage1 is 
1625b 70 6f 70 75 6c 61 74 65 64 20 6f 72 0a 20 20 20  populated or.   
1625c 20 2a 2a 20 6c 6f 63 6b 42 74 72 65 65 28 29 20   ** lockBtree() 
1625d 72 65 74 75 72 6e 73 20 73 6f 6d 65 74 68 69 6e  returns somethin
1625e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c  g other than SQL
1625f 49 54 45 5f 4f 4b 2e 20 6c 6f 63 6b 42 74 72 65  ITE_OK. lockBtre
16260 65 28 29 0a 20 20 20 20 2a 2a 20 6d 61 79 20 72  e().    ** may r
16261 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
16262 62 75 74 20 6c 65 61 76 65 20 70 42 74 2d 3e 70  but leave pBt->p
16263 50 61 67 65 31 20 73 65 74 20 74 6f 20 30 20 69  Page1 set to 0 i
16264 66 20 61 66 74 65 72 0a 20 20 20 20 2a 2a 20 72  f after.    ** r
16265 65 61 64 69 6e 67 20 70 61 67 65 20 31 20 69 74  eading page 1 it
16266 20 64 69 73 63 6f 76 65 72 73 20 74 68 61 74 20   discovers that 
16267 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 6f 66  the page-size of
16268 20 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 20   the database . 
16269 20 20 20 2a 2a 20 66 69 6c 65 20 69 73 20 6e 6f     ** file is no
1626a 74 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2e  t pBt->pageSize.
1626b 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 6c 6f   In this case lo
1626c 63 6b 42 74 72 65 65 28 29 20 77 69 6c 6c 20 75  ckBtree() will u
1626d 70 64 61 74 65 0a 20 20 20 20 2a 2a 20 70 42 74  pdate.    ** pBt
1626e 2d 3e 70 61 67 65 53 69 7a 65 20 74 6f 20 74 68  ->pageSize to th
1626f 65 20 70 61 67 65 2d 73 69 7a 65 20 6f 66 20 74  e page-size of t
16270 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e  he file on disk.
16271 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68 69 6c  .    */.    whil
16272 65 28 20 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d  e( pBt->pPage1==
16273 30 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d  0 && SQLITE_OK==
16274 28 72 63 20 3d 20 6c 6f 63 6b 42 74 72 65 65 28  (rc = lockBtree(
16275 70 42 74 29 29 20 29 3b 0a 0a 20 20 20 20 69 66  pBt)) );..    if
16276 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
16277 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20  && wrflag ){.   
16278 20 20 20 69 66 28 20 70 42 74 2d 3e 72 65 61 64     if( pBt->read
16279 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 20 20  Only ){.        
1627a 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 44  rc = SQLITE_READ
1627b 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ONLY;.      }els
1627c 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  e{.        rc = 
1627d 73 71 6c 69 74 65 33 50 61 67 65 72 42 65 67 69  sqlite3PagerBegi
1627e 6e 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 77 72  n(pBt->pPager,wr
1627f 66 6c 61 67 3e 31 2c 73 71 6c 69 74 65 33 54 65  flag>1,sqlite3Te
16280 6d 70 49 6e 4d 65 6d 6f 72 79 28 70 2d 3e 64 62  mpInMemory(p->db
16281 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ));.        if( 
16282 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
16283 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
16284 6e 65 77 44 61 74 61 62 61 73 65 28 70 42 74 29  newDatabase(pBt)
16285 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
16286 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20    }.    }.  .   
16287 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
16288 4f 4b 20 29 7b 0a 20 20 20 20 20 20 75 6e 6c 6f  OK ){.      unlo
16289 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28  ckBtreeIfUnused(
1628a 70 42 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 77  pBt);.    }.  }w
1628b 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45  hile( rc==SQLITE
1628c 5f 42 55 53 59 20 26 26 20 70 42 74 2d 3e 69 6e  _BUSY && pBt->in
1628d 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41  Transaction==TRA
1628e 4e 53 5f 4e 4f 4e 45 20 26 26 0a 20 20 20 20 20  NS_NONE &&.     
1628f 20 20 20 20 20 62 74 72 65 65 49 6e 76 6f 6b 65       btreeInvoke
16290 42 75 73 79 48 61 6e 64 6c 65 72 28 70 42 74 29  BusyHandler(pBt)
16291 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53   );..  if( rc==S
16292 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
16293 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d  if( p->inTrans==
16294 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20  TRANS_NONE ){.  
16295 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61      pBt->nTransa
16296 63 74 69 6f 6e 2b 2b 3b 0a 23 69 66 6e 64 65 66  ction++;.#ifndef
16297 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
16298 52 45 44 5f 43 41 43 48 45 0a 20 20 20 20 20 20  RED_CACHE.      
16299 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20  if( p->sharable 
1629a 29 7b 0a 09 61 73 73 65 72 74 28 20 70 2d 3e 6c  ){..assert( p->l
1629b 6f 63 6b 2e 70 42 74 72 65 65 3d 3d 70 20 26 26  ock.pBtree==p &&
1629c 20 70 2d 3e 6c 6f 63 6b 2e 69 54 61 62 6c 65 3d   p->lock.iTable=
1629d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d  =1 );.        p-
1629e 3e 6c 6f 63 6b 2e 65 4c 6f 63 6b 20 3d 20 52 45  >lock.eLock = RE
1629f 41 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20  AD_LOCK;.       
162a0 20 70 2d 3e 6c 6f 63 6b 2e 70 4e 65 78 74 20 3d   p->lock.pNext =
162a1 20 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 20   pBt->pLock;.   
162a2 20 20 20 20 20 70 42 74 2d 3e 70 4c 6f 63 6b 20       pBt->pLock 
162a3 3d 20 26 70 2d 3e 6c 6f 63 6b 3b 0a 20 20 20 20  = &p->lock;.    
162a4 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d    }.#endif.    }
162a5 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20  .    p->inTrans 
162a6 3d 20 28 77 72 66 6c 61 67 3f 54 52 41 4e 53 5f  = (wrflag?TRANS_
162a7 57 52 49 54 45 3a 54 52 41 4e 53 5f 52 45 41 44  WRITE:TRANS_READ
162a8 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e  );.    if( p->in
162a9 54 72 61 6e 73 3e 70 42 74 2d 3e 69 6e 54 72 61  Trans>pBt->inTra
162aa 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20  nsaction ){.    
162ab 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63    pBt->inTransac
162ac 74 69 6f 6e 20 3d 20 70 2d 3e 69 6e 54 72 61 6e  tion = p->inTran
162ad 73 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  s;.    }.#ifndef
162ae 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
162af 52 45 44 5f 43 41 43 48 45 0a 20 20 20 20 69 66  RED_CACHE.    if
162b0 28 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20  ( wrflag ){.    
162b1 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e    assert( !pBt->
162b2 70 57 72 69 74 65 72 20 29 3b 0a 20 20 20 20 20  pWriter );.     
162b3 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20   pBt->pWriter = 
162b4 70 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 73  p;.      pBt->is
162b5 45 78 63 6c 75 73 69 76 65 20 3d 20 28 75 38 29  Exclusive = (u8)
162b6 28 77 72 66 6c 61 67 3e 31 29 3b 0a 20 20 20 20  (wrflag>1);.    
162b7 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 0a 74  }.#endif.  }...t
162b8 72 61 6e 73 5f 62 65 67 75 6e 3a 0a 20 20 69 66  rans_begun:.  if
162b9 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
162ba 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20  && wrflag ){.   
162bb 20 2f 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 61   /* This call ma
162bc 6b 65 73 20 73 75 72 65 20 74 68 61 74 20 74 68  kes sure that th
162bd 65 20 70 61 67 65 72 20 68 61 73 20 74 68 65 20  e pager has the 
162be 63 6f 72 72 65 63 74 20 6e 75 6d 62 65 72 20 6f  correct number o
162bf 66 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 20 73 61  f.    ** open sa
162c0 76 65 70 6f 69 6e 74 73 2e 20 49 66 20 74 68 65  vepoints. If the
162c1 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
162c2 72 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  r is greater tha
162c3 6e 20 30 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74  n 0 and.    ** t
162c4 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69  he sub-journal i
162c5 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70  s not already op
162c6 65 6e 2c 20 74 68 65 6e 20 69 74 20 77 69 6c 6c  en, then it will
162c7 20 62 65 20 6f 70 65 6e 65 64 20 68 65 72 65 2e   be opened here.
162c8 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d  .    */.    rc =
162c9 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65   sqlite3PagerOpe
162ca 6e 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e  nSavepoint(pBt->
162cb 70 50 61 67 65 72 2c 20 70 2d 3e 64 62 2d 3e 6e  pPager, p->db->n
162cc 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d 0a  Savepoint);.  }.
162cd 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74  .  btreeIntegrit
162ce 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  y(p);.  sqlite3B
162cf 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
162d0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69  return rc;.}..#i
162d1 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
162d2 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 0a 2f 2a  T_AUTOVACUUM../*
162d3 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 6f 69 6e  .** Set the poin
162d4 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20  ter-map entries 
162d5 66 6f 72 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e  for all children
162d6 20 6f 66 20 70 61 67 65 20 70 50 61 67 65 2e 20   of page pPage. 
162d7 41 6c 73 6f 2c 20 69 66 0a 2a 2a 20 70 50 61 67  Also, if.** pPag
162d8 65 20 63 6f 6e 74 61 69 6e 73 20 63 65 6c 6c 73  e contains cells
162d9 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f   that point to o
162da 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 73  verflow pages, s
162db 65 74 20 74 68 65 20 70 6f 69 6e 74 65 72 0a 2a  et the pointer.*
162dc 2a 20 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f  * map entries fo
162dd 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70  r the overflow p
162de 61 67 65 73 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f  ages as well..*/
162df 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 74 43  .static int setC
162e0 68 69 6c 64 50 74 72 6d 61 70 73 28 4d 65 6d 50  hildPtrmaps(MemP
162e1 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69  age *pPage){.  i
162e2 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
162e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
162e4 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 76 61 72    /* Counter var
162e5 69 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  iable */.  int n
162e6 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Cell;           
162e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
162e8 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73   Number of cells
162e9 20 69 6e 20 70 61 67 65 20 70 50 61 67 65 20 2a   in page pPage *
162ea 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  /.  int rc;     
162eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
162ec 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
162ed 20 63 6f 64 65 20 2a 2f 0a 20 20 42 74 53 68 61   code */.  BtSha
162ee 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65  red *pBt = pPage
162ef 2d 3e 70 42 74 3b 0a 20 20 75 38 20 69 73 49 6e  ->pBt;.  u8 isIn
162f0 69 74 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e  itOrig = pPage->
162f1 69 73 49 6e 69 74 3b 0a 20 20 50 67 6e 6f 20 70  isInit;.  Pgno p
162f2 67 6e 6f 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e  gno = pPage->pgn
162f3 6f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  o;..  assert( sq
162f4 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
162f5 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74  (pPage->pBt->mut
162f6 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 62 74  ex) );.  rc = bt
162f7 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67  reeInitPage(pPag
162f8 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  e);.  if( rc!=SQ
162f9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67  LITE_OK ){.    g
162fa 6f 74 6f 20 73 65 74 5f 63 68 69 6c 64 5f 70 74  oto set_child_pt
162fb 72 6d 61 70 73 5f 6f 75 74 3b 0a 20 20 7d 0a 20  rmaps_out;.  }. 
162fc 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e   nCell = pPage->
162fd 6e 43 65 6c 6c 3b 0a 0a 20 20 66 6f 72 28 69 3d  nCell;..  for(i=
162fe 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29  0; i<nCell; i++)
162ff 7b 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20  {.    u8 *pCell 
16300 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65  = findCell(pPage
16301 2c 20 69 29 3b 0a 0a 20 20 20 20 70 74 72 6d 61  , i);..    ptrma
16302 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67  pPutOvflPtr(pPag
16303 65 2c 20 70 43 65 6c 6c 2c 20 26 72 63 29 3b 0a  e, pCell, &rc);.
16304 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d  .    if( !pPage-
16305 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 50  >leaf ){.      P
16306 67 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20  gno childPgno = 
16307 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3b  get4byte(pCell);
16308 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74  .      ptrmapPut
16309 28 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c  (pBt, childPgno,
1630a 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70   PTRMAP_BTREE, p
1630b 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d  gno, &rc);.    }
1630c 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61  .  }..  if( !pPa
1630d 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20  ge->leaf ){.    
1630e 50 67 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d  Pgno childPgno =
1630f 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65   get4byte(&pPage
16310 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68  ->aData[pPage->h
16311 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20  drOffset+8]);.  
16312 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c    ptrmapPut(pBt,
16313 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50 54 52 4d   childPgno, PTRM
16314 41 50 5f 42 54 52 45 45 2c 20 70 67 6e 6f 2c 20  AP_BTREE, pgno, 
16315 26 72 63 29 3b 0a 20 20 7d 0a 0a 73 65 74 5f 63  &rc);.  }..set_c
16316 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74  hild_ptrmaps_out
16317 3a 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69  :.  pPage->isIni
16318 74 20 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b 0a  t = isInitOrig;.
16319 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1631a 2f 2a 0a 2a 2a 20 53 6f 6d 65 77 68 65 72 65 20  /*.** Somewhere 
1631b 6f 6e 20 70 50 61 67 65 20 69 73 20 61 20 70 6f  on pPage is a po
1631c 69 6e 74 65 72 20 74 6f 20 70 61 67 65 20 69 46  inter to page iF
1631d 72 6f 6d 2e 20 20 4d 6f 64 69 66 79 20 74 68 69  rom.  Modify thi
1631e 73 20 70 6f 69 6e 74 65 72 20 73 6f 0a 2a 2a 20  s pointer so.** 
1631f 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74  that it points t
16320 6f 20 69 54 6f 2e 20 50 61 72 61 6d 65 74 65 72  o iTo. Parameter
16321 20 65 54 79 70 65 20 64 65 73 63 72 69 62 65 73   eType describes
16322 20 74 68 65 20 74 79 70 65 20 6f 66 20 70 6f 69   the type of poi
16323 6e 74 65 72 20 74 6f 0a 2a 2a 20 62 65 20 6d 6f  nter to.** be mo
16324 64 69 66 69 65 64 2c 20 61 73 20 20 66 6f 6c 6c  dified, as  foll
16325 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41  ows:.**.** PTRMA
16326 50 5f 42 54 52 45 45 3a 20 20 20 20 20 70 50 61  P_BTREE:     pPa
16327 67 65 20 69 73 20 61 20 62 74 72 65 65 2d 70 61  ge is a btree-pa
16328 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20  ge. The pointer 
16329 70 6f 69 6e 74 73 20 61 74 20 61 20 63 68 69 6c  points at a chil
1632a 64 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  d .**           
1632b 20 20 20 20 20 20 20 20 70 61 67 65 20 6f 66 20          page of 
1632c 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52  pPage..**.** PTR
1632d 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 70  MAP_OVERFLOW1: p
1632e 50 61 67 65 20 69 73 20 61 20 62 74 72 65 65 2d  Page is a btree-
1632f 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65  page. The pointe
16330 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 6f  r points at an o
16331 76 65 72 66 6c 6f 77 0a 2a 2a 20 20 20 20 20 20  verflow.**      
16332 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67               pag
16333 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  e pointed to by 
16334 6f 6e 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 73  one of the cells
16335 20 6f 6e 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a   on pPage..**.**
16336 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
16337 32 3a 20 70 50 61 67 65 20 69 73 20 61 6e 20 6f  2: pPage is an o
16338 76 65 72 66 6c 6f 77 2d 70 61 67 65 2e 20 54 68  verflow-page. Th
16339 65 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73  e pointer points
1633a 20 61 74 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20   at the next.** 
1633b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1633c 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20    overflow page 
1633d 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a  in the list..*/.
1633e 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 64 69 66  static int modif
1633f 79 50 61 67 65 50 6f 69 6e 74 65 72 28 4d 65 6d  yPagePointer(Mem
16340 50 61 67 65 20 2a 70 50 61 67 65 2c 20 50 67 6e  Page *pPage, Pgn
16341 6f 20 69 46 72 6f 6d 2c 20 50 67 6e 6f 20 69 54  o iFrom, Pgno iT
16342 6f 2c 20 75 38 20 65 54 79 70 65 29 7b 0a 20 20  o, u8 eType){.  
16343 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
16344 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
16345 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
16346 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
16347 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
16348 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  le(pPage->pDbPag
16349 65 29 20 29 3b 0a 20 20 69 66 28 20 65 54 79 70  e) );.  if( eTyp
1634a 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  e==PTRMAP_OVERFL
1634b 4f 57 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  OW2 ){.    /* Th
1634c 65 20 70 6f 69 6e 74 65 72 20 69 73 20 61 6c 77  e pointer is alw
1634d 61 79 73 20 74 68 65 20 66 69 72 73 74 20 34 20  ays the first 4 
1634e 62 79 74 65 73 20 6f 66 20 74 68 65 20 70 61 67  bytes of the pag
1634f 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20  e in this case. 
16350 20 2a 2f 0a 20 20 20 20 69 66 28 20 67 65 74 34   */.    if( get4
16351 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74  byte(pPage->aDat
16352 61 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20  a)!=iFrom ){.   
16353 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
16354 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
16355 20 20 20 7d 0a 20 20 20 20 70 75 74 34 62 79 74     }.    put4byt
16356 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20  e(pPage->aData, 
16357 69 54 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  iTo);.  }else{. 
16358 20 20 20 75 38 20 69 73 49 6e 69 74 4f 72 69 67     u8 isInitOrig
16359 20 3d 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74   = pPage->isInit
1635a 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  ;.    int i;.   
1635b 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 0a 20 20 20   int nCell;..   
1635c 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 70   btreeInitPage(p
1635d 50 61 67 65 29 3b 0a 20 20 20 20 6e 43 65 6c 6c  Page);.    nCell
1635e 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b   = pPage->nCell;
1635f 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ..    for(i=0; i
16360 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20  <nCell; i++){.  
16361 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20      u8 *pCell = 
16362 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20  findCell(pPage, 
16363 69 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 54  i);.      if( eT
16364 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52  ype==PTRMAP_OVER
16365 46 4c 4f 57 31 20 29 7b 0a 20 20 20 20 20 20 20  FLOW1 ){.       
16366 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a   CellInfo info;.
16367 20 20 20 20 20 20 20 20 62 74 72 65 65 50 61 72          btreePar
16368 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c  seCellPtr(pPage,
16369 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a   pCell, &info);.
1636a 20 20 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f          if( info
1636b 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20  .iOverflow ){.  
1636c 20 20 20 20 20 20 20 20 69 66 28 20 69 46 72 6f          if( iFro
1636d 6d 3d 3d 67 65 74 34 62 79 74 65 28 26 70 43 65  m==get4byte(&pCe
1636e 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  ll[info.iOverflo
1636f 77 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  w]) ){.         
16370 20 20 20 70 75 74 34 62 79 74 65 28 26 70 43 65     put4byte(&pCe
16371 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  ll[info.iOverflo
16372 77 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20  w], iTo);.      
16373 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
16374 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
16375 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a   }.      }else{.
16376 20 20 20 20 20 20 20 20 69 66 28 20 67 65 74 34          if( get4
16377 62 79 74 65 28 70 43 65 6c 6c 29 3d 3d 69 46 72  byte(pCell)==iFr
16378 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  om ){.          
16379 70 75 74 34 62 79 74 65 28 70 43 65 6c 6c 2c 20  put4byte(pCell, 
1637a 69 54 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20  iTo);.          
1637b 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
1637c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1637d 20 0a 20 20 20 20 69 66 28 20 69 3d 3d 6e 43 65   .    if( i==nCe
1637e 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ll ){.      if( 
1637f 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 42 54  eType!=PTRMAP_BT
16380 52 45 45 20 7c 7c 20 0a 20 20 20 20 20 20 20 20  REE || .        
16381 20 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67    get4byte(&pPag
16382 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e  e->aData[pPage->
16383 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 21 3d 69  hdrOffset+8])!=i
16384 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  From ){.        
16385 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
16386 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
16387 20 20 7d 0a 20 20 20 20 20 20 70 75 74 34 62 79    }.      put4by
16388 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61  te(&pPage->aData
16389 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65  [pPage->hdrOffse
1638a 74 2b 38 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20  t+8], iTo);.    
1638b 7d 0a 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73  }..    pPage->is
1638c 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 69  Init = isInitOri
1638d 67 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  g;.  }.  return 
1638e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f  SQLITE_OK;.}.../
1638f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 6f 70  *.** Move the op
16390 65 6e 20 64 61 74 61 62 61 73 65 20 70 61 67 65  en database page
16391 20 70 44 62 50 61 67 65 20 74 6f 20 6c 6f 63 61   pDbPage to loca
16392 74 69 6f 6e 20 69 46 72 65 65 50 61 67 65 20 69  tion iFreePage i
16393 6e 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61  n the .** databa
16394 73 65 2e 20 54 68 65 20 70 44 62 50 61 67 65 20  se. The pDbPage 
16395 72 65 66 65 72 65 6e 63 65 20 72 65 6d 61 69 6e  reference remain
16396 73 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 54  s valid..**.** T
16397 68 65 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67  he isCommit flag
16398 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20   indicates that 
16399 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64  there is no need
1639a 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 61   to remember tha
1639b 74 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c  t.** the journal
1639c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e   needs to be syn
1639d 63 28 29 65 64 20 62 65 66 6f 72 65 20 64 61 74  c()ed before dat
1639e 61 62 61 73 65 20 70 61 67 65 20 70 44 62 50 61  abase page pDbPa
1639f 67 65 2d 3e 70 67 6e 6f 20 0a 2a 2a 20 63 61 6e  ge->pgno .** can
163a0 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2e 20   be written to. 
163a1 54 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 61  The caller has a
163a2 6c 72 65 61 64 79 20 70 72 6f 6d 69 73 65 64 20  lready promised 
163a3 6e 6f 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20  not to write to 
163a4 74 68 61 74 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2f  that.** page..*/
163a5 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 6c 6f  .static int relo
163a6 63 61 74 65 50 61 67 65 28 0a 20 20 42 74 53 68  catePage(.  BtSh
163a7 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20  ared *pBt,      
163a8 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 2a 2f       /* Btree */
163a9 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 44 62 50  .  MemPage *pDbP
163aa 61 67 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f  age,        /* O
163ab 70 65 6e 20 70 61 67 65 20 74 6f 20 6d 6f 76 65  pen page to move
163ac 20 2a 2f 0a 20 20 75 38 20 65 54 79 70 65 2c 20   */.  u8 eType, 
163ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
163ae 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 27 74  * Pointer map 't
163af 79 70 65 27 20 65 6e 74 72 79 20 66 6f 72 20 70  ype' entry for p
163b0 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f  DbPage */.  Pgno
163b1 20 69 50 74 72 50 61 67 65 2c 20 20 20 20 20 20   iPtrPage,      
163b2 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
163b3 6d 61 70 20 27 70 61 67 65 2d 6e 6f 27 20 65 6e  map 'page-no' en
163b4 74 72 79 20 66 6f 72 20 70 44 62 50 61 67 65 20  try for pDbPage 
163b5 2a 2f 0a 20 20 50 67 6e 6f 20 69 46 72 65 65 50  */.  Pgno iFreeP
163b6 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  age,          /*
163b7 20 54 68 65 20 6c 6f 63 61 74 69 6f 6e 20 74 6f   The location to
163b8 20 6d 6f 76 65 20 70 44 62 50 61 67 65 20 74 6f   move pDbPage to
163b9 20 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6f 6d 6d   */.  int isComm
163ba 69 74 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  it             /
163bb 2a 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20  * isCommit flag 
163bc 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65  passed to sqlite
163bd 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 20 2a  3PagerMovepage *
163be 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  /.){.  MemPage *
163bf 70 50 74 72 50 61 67 65 3b 20 20 20 2f 2a 20 54  pPtrPage;   /* T
163c0 68 65 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e  he page that con
163c1 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  tains a pointer 
163c2 74 6f 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20  to pDbPage */.  
163c3 50 67 6e 6f 20 69 44 62 50 61 67 65 20 3d 20 70  Pgno iDbPage = p
163c4 44 62 50 61 67 65 2d 3e 70 67 6e 6f 3b 0a 20 20  DbPage->pgno;.  
163c5 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20  Pager *pPager = 
163c6 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69  pBt->pPager;.  i
163c7 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74  nt rc;..  assert
163c8 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f  ( eType==PTRMAP_
163c9 4f 56 45 52 46 4c 4f 57 32 20 7c 7c 20 65 54 79  OVERFLOW2 || eTy
163ca 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46  pe==PTRMAP_OVERF
163cb 4c 4f 57 31 20 7c 7c 20 0a 20 20 20 20 20 20 65  LOW1 || .      e
163cc 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52  Type==PTRMAP_BTR
163cd 45 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52  EE || eType==PTR
163ce 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 3b 0a  MAP_ROOTPAGE );.
163cf 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
163d0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
163d1 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
163d2 73 65 72 74 28 20 70 44 62 50 61 67 65 2d 3e 70  sert( pDbPage->p
163d3 42 74 3d 3d 70 42 74 20 29 3b 0a 0a 20 20 2f 2a  Bt==pBt );..  /*
163d4 20 4d 6f 76 65 20 70 61 67 65 20 69 44 62 50 61   Move page iDbPa
163d5 67 65 20 66 72 6f 6d 20 69 74 73 20 63 75 72 72  ge from its curr
163d6 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 74 6f 20  ent location to 
163d7 70 61 67 65 20 6e 75 6d 62 65 72 20 69 46 72 65  page number iFre
163d8 65 50 61 67 65 20 2a 2f 0a 20 20 54 52 41 43 45  ePage */.  TRACE
163d9 28 28 22 41 55 54 4f 56 41 43 55 55 4d 3a 20 4d  (("AUTOVACUUM: M
163da 6f 76 69 6e 67 20 25 64 20 74 6f 20 66 72 65 65  oving %d to free
163db 20 70 61 67 65 20 25 64 20 28 70 74 72 20 70 61   page %d (ptr pa
163dc 67 65 20 25 64 20 74 79 70 65 20 25 64 29 5c 6e  ge %d type %d)\n
163dd 22 2c 20 0a 20 20 20 20 20 20 69 44 62 50 61 67  ", .      iDbPag
163de 65 2c 20 69 46 72 65 65 50 61 67 65 2c 20 69 50  e, iFreePage, iP
163df 74 72 50 61 67 65 2c 20 65 54 79 70 65 29 29 3b  trPage, eType));
163e0 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50  .  rc = sqlite3P
163e1 61 67 65 72 4d 6f 76 65 70 61 67 65 28 70 50 61  agerMovepage(pPa
163e2 67 65 72 2c 20 70 44 62 50 61 67 65 2d 3e 70 44  ger, pDbPage->pD
163e3 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 65  bPage, iFreePage
163e4 2c 20 69 73 43 6f 6d 6d 69 74 29 3b 0a 20 20 69  , isCommit);.  i
163e5 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
163e6 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
163e7 63 3b 0a 20 20 7d 0a 20 20 70 44 62 50 61 67 65  c;.  }.  pDbPage
163e8 2d 3e 70 67 6e 6f 20 3d 20 69 46 72 65 65 50 61  ->pgno = iFreePa
163e9 67 65 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 44 62  ge;..  /* If pDb
163ea 50 61 67 65 20 77 61 73 20 61 20 62 74 72 65 65  Page was a btree
163eb 2d 70 61 67 65 2c 20 74 68 65 6e 20 69 74 20 6d  -page, then it m
163ec 61 79 20 68 61 76 65 20 63 68 69 6c 64 20 70 61  ay have child pa
163ed 67 65 73 20 61 6e 64 2f 6f 72 20 63 65 6c 6c 73  ges and/or cells
163ee 0a 20 20 2a 2a 20 74 68 61 74 20 70 6f 69 6e 74  .  ** that point
163ef 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   to overflow pag
163f0 65 73 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20  es. The pointer 
163f1 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20  map entries for 
163f2 61 6c 6c 20 74 68 65 73 65 0a 20 20 2a 2a 20 70  all these.  ** p
163f3 61 67 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20  ages need to be 
163f4 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2a 0a 20 20  changed..  **.  
163f5 2a 2a 20 49 66 20 70 44 62 50 61 67 65 20 69 73  ** If pDbPage is
163f6 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   an overflow pag
163f7 65 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72 73  e, then the firs
163f8 74 20 34 20 62 79 74 65 73 20 6d 61 79 20 73 74  t 4 bytes may st
163f9 6f 72 65 20 61 0a 20 20 2a 2a 20 70 6f 69 6e 74  ore a.  ** point
163fa 65 72 20 74 6f 20 61 20 73 75 62 73 65 71 75 65  er to a subseque
163fb 6e 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  nt overflow page
163fc 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65  . If this is the
163fd 20 63 61 73 65 2c 20 74 68 65 6e 0a 20 20 2a 2a   case, then.  **
163fe 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   the pointer map
163ff 20 6e 65 65 64 73 20 74 6f 20 62 65 20 75 70 64   needs to be upd
16400 61 74 65 64 20 66 6f 72 20 74 68 65 20 73 75 62  ated for the sub
16401 73 65 71 75 65 6e 74 20 6f 76 65 72 66 6c 6f 77  sequent overflow
16402 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66   page..  */.  if
16403 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f  ( eType==PTRMAP_
16404 42 54 52 45 45 20 7c 7c 20 65 54 79 70 65 3d 3d  BTREE || eType==
16405 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20  PTRMAP_ROOTPAGE 
16406 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 65 74 43  ){.    rc = setC
16407 68 69 6c 64 50 74 72 6d 61 70 73 28 70 44 62 50  hildPtrmaps(pDbP
16408 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63  age);.    if( rc
16409 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1640a 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1640b 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
1640c 20 20 20 50 67 6e 6f 20 6e 65 78 74 4f 76 66 6c     Pgno nextOvfl
1640d 20 3d 20 67 65 74 34 62 79 74 65 28 70 44 62 50   = get4byte(pDbP
1640e 61 67 65 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20  age->aData);.   
1640f 20 69 66 28 20 6e 65 78 74 4f 76 66 6c 21 3d 30   if( nextOvfl!=0
16410 20 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 70   ){.      ptrmap
16411 50 75 74 28 70 42 74 2c 20 6e 65 78 74 4f 76 66  Put(pBt, nextOvf
16412 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  l, PTRMAP_OVERFL
16413 4f 57 32 2c 20 69 46 72 65 65 50 61 67 65 2c 20  OW2, iFreePage, 
16414 26 72 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20  &rc);.      if( 
16415 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
16416 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
16417 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
16418 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 78 20  }.  }..  /* Fix 
16419 74 68 65 20 64 61 74 61 62 61 73 65 20 70 6f 69  the database poi
1641a 6e 74 65 72 20 6f 6e 20 70 61 67 65 20 69 50 74  nter on page iPt
1641b 72 50 61 67 65 20 74 68 61 74 20 70 6f 69 6e 74  rPage that point
1641c 65 64 20 61 74 20 69 44 62 50 61 67 65 20 73 6f  ed at iDbPage so
1641d 0a 20 20 2a 2a 20 74 68 61 74 20 69 74 20 70 6f  .  ** that it po
1641e 69 6e 74 73 20 61 74 20 69 46 72 65 65 50 61 67  ints at iFreePag
1641f 65 2e 20 41 6c 73 6f 20 66 69 78 20 74 68 65 20  e. Also fix the 
16420 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72  pointer map entr
16421 79 20 66 6f 72 0a 20 20 2a 2a 20 69 50 74 72 50  y for.  ** iPtrP
16422 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  age..  */.  if( 
16423 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f  eType!=PTRMAP_RO
16424 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 72 63  OTPAGE ){.    rc
16425 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28   = btreeGetPage(
16426 70 42 74 2c 20 69 50 74 72 50 61 67 65 2c 20 26  pBt, iPtrPage, &
16427 70 50 74 72 50 61 67 65 2c 20 30 29 3b 0a 20 20  pPtrPage, 0);.  
16428 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
16429 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74  _OK ){.      ret
1642a 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
1642b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
1642c 67 65 72 57 72 69 74 65 28 70 50 74 72 50 61 67  gerWrite(pPtrPag
1642d 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  e->pDbPage);.   
1642e 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1642f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65  OK ){.      rele
16430 61 73 65 50 61 67 65 28 70 50 74 72 50 61 67 65  asePage(pPtrPage
16431 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
16432 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  rc;.    }.    rc
16433 20 3d 20 6d 6f 64 69 66 79 50 61 67 65 50 6f 69   = modifyPagePoi
16434 6e 74 65 72 28 70 50 74 72 50 61 67 65 2c 20 69  nter(pPtrPage, i
16435 44 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67  DbPage, iFreePag
16436 65 2c 20 65 54 79 70 65 29 3b 0a 20 20 20 20 72  e, eType);.    r
16437 65 6c 65 61 73 65 50 61 67 65 28 70 50 74 72 50  eleasePage(pPtrP
16438 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63  age);.    if( rc
16439 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1643a 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70       ptrmapPut(p
1643b 42 74 2c 20 69 46 72 65 65 50 61 67 65 2c 20 65  Bt, iFreePage, e
1643c 54 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20  Type, iPtrPage, 
1643d 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  &rc);.    }.  }.
1643e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1643f 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61  /* Forward decla
16440 72 61 74 69 6f 6e 20 72 65 71 75 69 72 65 64 20  ration required 
16441 62 79 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65  by incrVacuumSte
16442 70 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69  p(). */.static i
16443 6e 74 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65  nt allocateBtree
16444 50 61 67 65 28 42 74 53 68 61 72 65 64 20 2a 2c  Page(BtShared *,
16445 20 4d 65 6d 50 61 67 65 20 2a 2a 2c 20 50 67 6e   MemPage **, Pgn
16446 6f 20 2a 2c 20 50 67 6e 6f 2c 20 75 38 29 3b 0a  o *, Pgno, u8);.
16447 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61  ./*.** Perform a
16448 20 73 69 6e 67 6c 65 20 73 74 65 70 20 6f 66 20   single step of 
16449 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 2d 76  an incremental-v
1644a 61 63 75 75 6d 2e 20 49 66 20 73 75 63 63 65 73  acuum. If succes
1644b 73 66 75 6c 2c 0a 2a 2a 20 72 65 74 75 72 6e 20  sful,.** return 
1644c 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 74 68  SQLITE_OK. If th
1644d 65 72 65 20 69 73 20 6e 6f 20 77 6f 72 6b 20 74  ere is no work t
1644e 6f 20 64 6f 20 28 61 6e 64 20 74 68 65 72 65 66  o do (and theref
1644f 6f 72 65 20 6e 6f 0a 2a 2a 20 70 6f 69 6e 74 20  ore no.** point 
16450 69 6e 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20  in calling this 
16451 66 75 6e 63 74 69 6f 6e 20 61 67 61 69 6e 29 2c  function again),
16452 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44   return SQLITE_D
16453 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 72 65 20  ONE..**.** More 
16454 73 70 65 63 69 66 69 63 6c 79 2c 20 74 68 69 73  specificly, this
16455 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70   function attemp
16456 74 73 20 74 6f 20 72 65 2d 6f 72 67 61 6e 69 7a  ts to re-organiz
16457 65 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61  e the .** databa
16458 73 65 20 73 6f 20 74 68 61 74 20 74 68 65 20 6c  se so that the l
16459 61 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20  ast page of the 
1645a 66 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 69  file currently i
1645b 6e 20 75 73 65 0a 2a 2a 20 69 73 20 6e 6f 20 6c  n use.** is no l
1645c 6f 6e 67 65 72 20 69 6e 20 75 73 65 2e 0a 2a 2a  onger in use..**
1645d 0a 2a 2a 20 49 66 20 74 68 65 20 6e 46 69 6e 20  .** If the nFin 
1645e 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e  parameter is non
1645f 2d 7a 65 72 6f 2c 20 74 68 69 73 20 66 75 6e 63  -zero, this func
16460 74 69 6f 6e 20 61 73 73 75 6d 65 73 0a 2a 2a 20  tion assumes.** 
16461 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20  that the caller 
16462 77 69 6c 6c 20 6b 65 65 70 20 63 61 6c 6c 69 6e  will keep callin
16463 67 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70  g incrVacuumStep
16464 28 29 20 75 6e 74 69 6c 0a 2a 2a 20 69 74 20 72  () until.** it r
16465 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 44 4f  eturns SQLITE_DO
16466 4e 45 20 6f 72 20 61 6e 20 65 72 72 6f 72 2c 20  NE or an error, 
16467 61 6e 64 20 74 68 61 74 20 6e 46 69 6e 20 69 73  and that nFin is
16468 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f   the.** number o
16469 66 20 70 61 67 65 73 20 74 68 65 20 64 61 74 61  f pages the data
1646a 62 61 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 63  base file will c
1646b 6f 6e 74 61 69 6e 20 61 66 74 65 72 20 74 68 69  ontain after thi
1646c 73 20 0a 2a 2a 20 70 72 6f 63 65 73 73 20 69 73  s .** process is
1646d 20 63 6f 6d 70 6c 65 74 65 2e 20 20 49 66 20 6e   complete.  If n
1646e 46 69 6e 20 69 73 20 7a 65 72 6f 2c 20 69 74 20  Fin is zero, it 
1646f 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a  is assumed that.
16470 2a 2a 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65  ** incrVacuumSte
16471 70 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c  p() will be call
16472 65 64 20 61 20 66 69 6e 69 74 65 20 61 6d 6f 75  ed a finite amou
16473 6e 74 20 6f 66 20 74 69 6d 65 73 0a 2a 2a 20 77  nt of times.** w
16474 68 69 63 68 20 6d 61 79 20 6f 72 20 6d 61 79 20  hich may or may 
16475 6e 6f 74 20 65 6d 70 74 79 20 74 68 65 20 66 72  not empty the fr
16476 65 65 6c 69 73 74 2e 20 20 41 20 66 75 6c 6c 20  eelist.  A full 
16477 61 75 74 6f 76 61 63 75 75 6d 0a 2a 2a 20 68 61  autovacuum.** ha
16478 73 20 6e 46 69 6e 3e 30 2e 20 20 41 20 22 50 52  s nFin>0.  A "PR
16479 41 47 4d 41 20 69 6e 63 72 65 6d 65 6e 74 61 6c  AGMA incremental
1647a 5f 76 61 63 75 75 6d 22 20 68 61 73 20 6e 46 69  _vacuum" has nFi
1647b 6e 3d 3d 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  n==0..*/.static 
1647c 69 6e 74 20 69 6e 63 72 56 61 63 75 75 6d 53 74  int incrVacuumSt
1647d 65 70 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  ep(BtShared *pBt
1647e 2c 20 50 67 6e 6f 20 6e 46 69 6e 2c 20 50 67 6e  , Pgno nFin, Pgn
1647f 6f 20 69 4c 61 73 74 50 67 29 7b 0a 20 20 50 67  o iLastPg){.  Pg
16480 6e 6f 20 6e 46 72 65 65 4c 69 73 74 3b 20 20 20  no nFreeList;   
16481 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
16482 72 20 6f 66 20 70 61 67 65 73 20 73 74 69 6c 6c  r of pages still
16483 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73   on the free-lis
16484 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  t */..  assert( 
16485 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
16486 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  ld(pBt->mutex) )
16487 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4c 61 73  ;.  assert( iLas
16488 74 50 67 3e 6e 46 69 6e 20 29 3b 0a 0a 20 20 69  tPg>nFin );..  i
16489 66 28 20 21 50 54 52 4d 41 50 5f 49 53 50 41 47  f( !PTRMAP_ISPAG
1648a 45 28 70 42 74 2c 20 69 4c 61 73 74 50 67 29 20  E(pBt, iLastPg) 
1648b 26 26 20 69 4c 61 73 74 50 67 21 3d 50 45 4e 44  && iLastPg!=PEND
1648c 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
1648d 74 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63  t) ){.    int rc
1648e 3b 0a 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a  ;.    u8 eType;.
1648f 20 20 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67      Pgno iPtrPag
16490 65 3b 0a 0a 20 20 20 20 6e 46 72 65 65 4c 69 73  e;..    nFreeLis
16491 74 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 42  t = get4byte(&pB
16492 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61  t->pPage1->aData
16493 5b 33 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e  [36]);.    if( n
16494 46 72 65 65 4c 69 73 74 3d 3d 30 20 29 7b 0a 20  FreeList==0 ){. 
16495 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
16496 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a  TE_DONE;.    }..
16497 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47      rc = ptrmapG
16498 65 74 28 70 42 74 2c 20 69 4c 61 73 74 50 67 2c  et(pBt, iLastPg,
16499 20 26 65 54 79 70 65 2c 20 26 69 50 74 72 50 61   &eType, &iPtrPa
1649a 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  ge);.    if( rc!
1649b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1649c 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1649d 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65 54 79     }.    if( eTy
1649e 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50  pe==PTRMAP_ROOTP
1649f 41 47 45 20 29 7b 0a 20 20 20 20 20 20 72 65 74  AGE ){.      ret
164a0 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
164a1 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a  PT_BKPT;.    }..
164a2 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50      if( eType==P
164a3 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29  TRMAP_FREEPAGE )
164a4 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 46 69 6e  {.      if( nFin
164a5 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ==0 ){.        /
164a6 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 70 61 67  * Remove the pag
164a7 65 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 73  e from the files
164a8 20 66 72 65 65 2d 6c 69 73 74 2e 20 54 68 69 73   free-list. This
164a9 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64   is not required
164aa 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 66 20 6e  .        ** if n
164ab 46 69 6e 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e  Fin is non-zero.
164ac 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 74   In that case, t
164ad 68 65 20 66 72 65 65 2d 6c 69 73 74 20 77 69 6c  he free-list wil
164ae 6c 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  l be.        ** 
164af 74 72 75 6e 63 61 74 65 64 20 74 6f 20 7a 65 72  truncated to zer
164b0 6f 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e  o after this fun
164b1 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 73  ction returns, s
164b2 6f 20 69 74 20 64 6f 65 73 6e 27 74 20 0a 20 20  o it doesn't .  
164b3 20 20 20 20 20 20 2a 2a 20 6d 61 74 74 65 72 20        ** matter 
164b4 69 66 20 69 74 20 73 74 69 6c 6c 20 63 6f 6e 74  if it still cont
164b5 61 69 6e 73 20 73 6f 6d 65 20 67 61 72 62 61 67  ains some garbag
164b6 65 20 65 6e 74 72 69 65 73 2e 0a 20 20 20 20 20  e entries..     
164b7 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 50 67     */.        Pg
164b8 6e 6f 20 69 46 72 65 65 50 67 3b 0a 20 20 20 20  no iFreePg;.    
164b9 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72      MemPage *pFr
164ba 65 65 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63  eePg;.        rc
164bb 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65   = allocateBtree
164bc 50 61 67 65 28 70 42 74 2c 20 26 70 46 72 65 65  Page(pBt, &pFree
164bd 50 67 2c 20 26 69 46 72 65 65 50 67 2c 20 69 4c  Pg, &iFreePg, iL
164be 61 73 74 50 67 2c 20 31 29 3b 0a 20 20 20 20 20  astPg, 1);.     
164bf 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
164c0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
164c1 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
164c2 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61       }.        a
164c3 73 73 65 72 74 28 20 69 46 72 65 65 50 67 3d 3d  ssert( iFreePg==
164c4 69 4c 61 73 74 50 67 20 29 3b 0a 20 20 20 20 20  iLastPg );.     
164c5 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
164c6 46 72 65 65 50 67 29 3b 0a 20 20 20 20 20 20 7d  FreePg);.      }
164c7 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  .    } else {.  
164c8 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50 67      Pgno iFreePg
164c9 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
164ca 20 49 6e 64 65 78 20 6f 66 20 66 72 65 65 20 70   Index of free p
164cb 61 67 65 20 74 6f 20 6d 6f 76 65 20 70 4c 61 73  age to move pLas
164cc 74 50 67 20 74 6f 20 2a 2f 0a 20 20 20 20 20 20  tPg to */.      
164cd 4d 65 6d 50 61 67 65 20 2a 70 4c 61 73 74 50 67  MemPage *pLastPg
164ce 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74  ;..      rc = bt
164cf 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
164d0 69 4c 61 73 74 50 67 2c 20 26 70 4c 61 73 74 50  iLastPg, &pLastP
164d1 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  g, 0);.      if(
164d2 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
164d3 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
164d4 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20   rc;.      }..  
164d5 20 20 20 20 2f 2a 20 49 66 20 6e 46 69 6e 20 69      /* If nFin i
164d6 73 20 7a 65 72 6f 2c 20 74 68 69 73 20 6c 6f 6f  s zero, this loo
164d7 70 20 72 75 6e 73 20 65 78 61 63 74 6c 79 20 6f  p runs exactly o
164d8 6e 63 65 20 61 6e 64 20 70 61 67 65 20 70 4c 61  nce and page pLa
164d9 73 74 50 67 0a 20 20 20 20 20 20 2a 2a 20 69 73  stPg.      ** is
164da 20 73 77 61 70 70 65 64 20 77 69 74 68 20 74 68   swapped with th
164db 65 20 66 69 72 73 74 20 66 72 65 65 20 70 61 67  e first free pag
164dc 65 20 70 75 6c 6c 65 64 20 6f 66 66 20 74 68 65  e pulled off the
164dd 20 66 72 65 65 20 6c 69 73 74 2e 0a 20 20 20 20   free list..    
164de 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4f 6e    **.      ** On
164df 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c   the other hand,
164e0 20 69 66 20 6e 46 69 6e 20 69 73 20 67 72 65 61   if nFin is grea
164e1 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74  ter than zero, t
164e2 68 65 6e 20 6b 65 65 70 0a 20 20 20 20 20 20 2a  hen keep.      *
164e3 2a 20 6c 6f 6f 70 69 6e 67 20 75 6e 74 69 6c 20  * looping until 
164e4 61 20 66 72 65 65 2d 70 61 67 65 20 6c 6f 63 61  a free-page loca
164e5 74 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 66  ted within the f
164e6 69 72 73 74 20 6e 46 69 6e 20 70 61 67 65 73 0a  irst nFin pages.
164e7 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20        ** of the 
164e8 66 69 6c 65 20 69 73 20 66 6f 75 6e 64 2e 0a 20  file is found.. 
164e9 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f       */.      do
164ea 20 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61   {.        MemPa
164eb 67 65 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 20  ge *pFreePg;.   
164ec 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61       rc = alloca
164ed 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c  teBtreePage(pBt,
164ee 20 26 70 46 72 65 65 50 67 2c 20 26 69 46 72 65   &pFreePg, &iFre
164ef 65 50 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  ePg, 0, 0);.    
164f0 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
164f1 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
164f2 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
164f3 4c 61 73 74 50 67 29 3b 0a 20 20 20 20 20 20 20  LastPg);.       
164f4 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
164f5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
164f6 72 65 6c 65 61 73 65 50 61 67 65 28 70 46 72 65  releasePage(pFre
164f7 65 50 67 29 3b 0a 20 20 20 20 20 20 7d 77 68 69  ePg);.      }whi
164f8 6c 65 28 20 6e 46 69 6e 21 3d 30 20 26 26 20 69  le( nFin!=0 && i
164f9 46 72 65 65 50 67 3e 6e 46 69 6e 20 29 3b 0a 20  FreePg>nFin );. 
164fa 20 20 20 20 20 61 73 73 65 72 74 28 20 69 46 72       assert( iFr
164fb 65 65 50 67 3c 69 4c 61 73 74 50 67 20 29 3b 0a  eePg<iLastPg );.
164fc 20 20 20 20 20 20 0a 20 20 20 20 20 20 72 63 20        .      rc 
164fd 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
164fe 69 74 65 28 70 4c 61 73 74 50 67 2d 3e 70 44 62  ite(pLastPg->pDb
164ff 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Page);.      if(
16500 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
16501 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72  {.        rc = r
16502 65 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c  elocatePage(pBt,
16503 20 70 4c 61 73 74 50 67 2c 20 65 54 79 70 65 2c   pLastPg, eType,
16504 20 69 50 74 72 50 61 67 65 2c 20 69 46 72 65 65   iPtrPage, iFree
16505 50 67 2c 20 6e 46 69 6e 21 3d 30 29 3b 0a 20 20  Pg, nFin!=0);.  
16506 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 6c 65      }.      rele
16507 61 73 65 50 61 67 65 28 70 4c 61 73 74 50 67 29  asePage(pLastPg)
16508 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
16509 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1650a 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1650b 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1650c 7d 0a 0a 20 20 69 66 28 20 6e 46 69 6e 3d 3d 30  }..  if( nFin==0
1650d 20 29 7b 0a 20 20 20 20 69 4c 61 73 74 50 67 2d   ){.    iLastPg-
1650e 2d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 4c  -;.    while( iL
1650f 61 73 74 50 67 3d 3d 50 45 4e 44 49 4e 47 5f 42  astPg==PENDING_B
16510 59 54 45 5f 50 41 47 45 28 70 42 74 29 7c 7c 50  YTE_PAGE(pBt)||P
16511 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74  TRMAP_ISPAGE(pBt
16512 2c 20 69 4c 61 73 74 50 67 29 20 29 7b 0a 20 20  , iLastPg) ){.  
16513 20 20 20 20 69 66 28 20 50 54 52 4d 41 50 5f 49      if( PTRMAP_I
16514 53 50 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74  SPAGE(pBt, iLast
16515 50 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 4d  Pg) ){.        M
16516 65 6d 50 61 67 65 20 2a 70 50 67 3b 0a 20 20 20  emPage *pPg;.   
16517 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 62 74       int rc = bt
16518 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
16519 69 4c 61 73 74 50 67 2c 20 26 70 50 67 2c 20 30  iLastPg, &pPg, 0
1651a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
1651b 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1651c 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
1651d 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   rc;.        }. 
1651e 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
1651f 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
16520 67 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  g->pDbPage);.   
16521 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
16522 28 70 50 67 29 3b 0a 20 20 20 20 20 20 20 20 69  (pPg);.        i
16523 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
16524 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
16525 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20  turn rc;.       
16526 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
16527 20 69 4c 61 73 74 50 67 2d 2d 3b 0a 20 20 20 20   iLastPg--;.    
16528 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67  }.    sqlite3Pag
16529 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28  erTruncateImage(
1652a 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 4c 61  pBt->pPager, iLa
1652b 73 74 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74  stPg);.  }.  ret
1652c 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1652d 0a 0a 2f 2a 0a 2a 2a 20 41 20 77 72 69 74 65 2d  ../*.** A write-
1652e 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74  transaction must
1652f 20 62 65 20 6f 70 65 6e 65 64 20 62 65 66 6f 72   be opened befor
16530 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 66  e calling this f
16531 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 49 74 20 70  unction..** It p
16532 65 72 66 6f 72 6d 73 20 61 20 73 69 6e 67 6c 65  erforms a single
16533 20 75 6e 69 74 20 6f 66 20 77 6f 72 6b 20 74 6f   unit of work to
16534 77 61 72 64 73 20 61 6e 20 69 6e 63 72 65 6d 65  wards an increme
16535 6e 74 61 6c 20 76 61 63 75 75 6d 2e 0a 2a 2a 0a  ntal vacuum..**.
16536 2a 2a 20 49 66 20 74 68 65 20 69 6e 63 72 65 6d  ** If the increm
16537 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 69 73 20  ental vacuum is 
16538 66 69 6e 69 73 68 65 64 20 61 66 74 65 72 20 74  finished after t
16539 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 68 61 73  his function has
1653a 20 72 75 6e 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f   run,.** SQLITE_
1653b 44 4f 4e 45 20 69 73 20 72 65 74 75 72 6e 65 64  DONE is returned
1653c 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 66  . If it is not f
1653d 69 6e 69 73 68 65 64 2c 20 62 75 74 20 6e 6f 20  inished, but no 
1653e 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 0a  error occurred,.
1653f 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  ** SQLITE_OK is 
16540 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
16541 69 73 65 20 61 6e 20 53 51 4c 69 74 65 20 65 72  ise an SQLite er
16542 72 6f 72 20 63 6f 64 65 2e 20 0a 2a 2f 0a 53 51  ror code. .*/.SQ
16543 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
16544 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63   sqlite3BtreeInc
16545 72 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 70  rVacuum(Btree *p
16546 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42  ){.  int rc;.  B
16547 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
16548 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74 65  ->pBt;..  sqlite
16549 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
1654a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69    assert( pBt->i
1654b 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52  nTransaction==TR
1654c 41 4e 53 5f 57 52 49 54 45 20 26 26 20 70 2d 3e  ANS_WRITE && p->
1654d 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57  inTrans==TRANS_W
1654e 52 49 54 45 20 29 3b 0a 20 20 69 66 28 20 21 70  RITE );.  if( !p
1654f 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29  Bt->autoVacuum )
16550 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
16551 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 7b  E_DONE;.  }else{
16552 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 41  .    invalidateA
16553 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28  llOverflowCache(
16554 70 42 74 29 3b 0a 20 20 20 20 72 63 20 3d 20 69  pBt);.    rc = i
16555 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 70 42  ncrVacuumStep(pB
16556 74 2c 20 30 2c 20 70 61 67 65 72 50 61 67 65 63  t, 0, pagerPagec
16557 6f 75 6e 74 28 70 42 74 29 29 3b 0a 20 20 7d 0a  ount(pBt));.  }.
16558 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
16559 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
1655a 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
1655b 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
1655c 6c 6c 65 64 20 70 72 69 6f 72 20 74 6f 20 73 71  lled prior to sq
1655d 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74  lite3PagerCommit
1655e 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74   when a transact
1655f 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f 6d 6d 69 74  ion.** is commit
16560 65 64 20 66 6f 72 20 61 6e 20 61 75 74 6f 2d 76  ed for an auto-v
16561 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 2e 0a  acuum database..
16562 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f  **.** If SQLITE_
16563 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20  OK is returned, 
16564 74 68 65 6e 20 2a 70 6e 54 72 75 6e 63 20 69 73  then *pnTrunc is
16565 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62   set to the numb
16566 65 72 20 6f 66 20 70 61 67 65 73 0a 2a 2a 20 74  er of pages.** t
16567 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
16568 20 73 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63   should be trunc
16569 61 74 65 64 20 74 6f 20 64 75 72 69 6e 67 20 74  ated to during t
1656a 68 65 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73  he commit proces
1656b 73 2e 20 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20  s. .** i.e. the 
1656c 64 61 74 61 62 61 73 65 20 68 61 73 20 62 65 65  database has bee
1656d 6e 20 72 65 6f 72 67 61 6e 69 7a 65 64 20 73 6f  n reorganized so
1656e 20 74 68 61 74 20 6f 6e 6c 79 20 74 68 65 20 66   that only the f
1656f 69 72 73 74 20 2a 70 6e 54 72 75 6e 63 0a 2a 2a  irst *pnTrunc.**
16570 20 70 61 67 65 73 20 61 72 65 20 69 6e 20 75 73   pages are in us
16571 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
16572 20 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69   autoVacuumCommi
16573 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29  t(BtShared *pBt)
16574 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
16575 49 54 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20  ITE_OK;.  Pager 
16576 2a 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70  *pPager = pBt->p
16577 50 61 67 65 72 3b 0a 20 20 56 56 41 5f 4f 4e 4c  Pager;.  VVA_ONL
16578 59 28 20 69 6e 74 20 6e 52 65 66 20 3d 20 73 71  Y( int nRef = sq
16579 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75  lite3PagerRefcou
1657a 6e 74 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20  nt(pPager) );.. 
1657b 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1657c 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
1657d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 6e 76  >mutex) );.  inv
1657e 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c  alidateAllOverfl
1657f 6f 77 43 61 63 68 65 28 70 42 74 29 3b 0a 20 20  owCache(pBt);.  
16580 61 73 73 65 72 74 28 70 42 74 2d 3e 61 75 74 6f  assert(pBt->auto
16581 56 61 63 75 75 6d 29 3b 0a 20 20 69 66 28 20 21  Vacuum);.  if( !
16582 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20  pBt->incrVacuum 
16583 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 46 69 6e  ){.    Pgno nFin
16584 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ;         /* Num
16585 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
16586 64 61 74 61 62 61 73 65 20 61 66 74 65 72 20 61  database after a
16587 75 74 6f 76 61 63 75 75 6d 69 6e 67 20 2a 2f 0a  utovacuuming */.
16588 20 20 20 20 50 67 6e 6f 20 6e 46 72 65 65 3b 20      Pgno nFree; 
16589 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1658a 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65   of pages on the
1658b 20 66 72 65 65 6c 69 73 74 20 69 6e 69 74 69 61   freelist initia
1658c 6c 6c 79 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20  lly */.    Pgno 
1658d 6e 50 74 72 6d 61 70 3b 20 20 20 20 20 20 2f 2a  nPtrmap;      /*
1658e 20 4e 75 6d 62 65 72 20 6f 66 20 50 74 72 4d 61   Number of PtrMa
1658f 70 20 70 61 67 65 73 20 74 6f 20 62 65 20 66 72  p pages to be fr
16590 65 65 64 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20  eed */.    Pgno 
16591 69 46 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a  iFree;        /*
16592 20 54 68 65 20 6e 65 78 74 20 70 61 67 65 20 74   The next page t
16593 6f 20 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20  o be freed */.  
16594 20 20 69 6e 74 20 6e 45 6e 74 72 79 3b 20 20 20    int nEntry;   
16595 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
16596 66 20 65 6e 74 72 69 65 73 20 6f 6e 20 6f 6e 65  f entries on one
16597 20 70 74 72 6d 61 70 20 70 61 67 65 20 2a 2f 0a   ptrmap page */.
16598 20 20 20 20 50 67 6e 6f 20 6e 4f 72 69 67 3b 20      Pgno nOrig; 
16599 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
1659a 73 65 20 73 69 7a 65 20 62 65 66 6f 72 65 20 66  se size before f
1659b 72 65 65 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 6e  reeing */..    n
1659c 4f 72 69 67 20 3d 20 70 61 67 65 72 50 61 67 65  Orig = pagerPage
1659d 63 6f 75 6e 74 28 70 42 74 29 3b 0a 20 20 20 20  count(pBt);.    
1659e 69 66 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47  if( PTRMAP_ISPAG
1659f 45 28 70 42 74 2c 20 6e 4f 72 69 67 29 20 7c 7c  E(pBt, nOrig) ||
165a0 20 6e 4f 72 69 67 3d 3d 50 45 4e 44 49 4e 47 5f   nOrig==PENDING_
165a1 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29  BYTE_PAGE(pBt) )
165a2 7b 0a 20 20 20 20 20 20 2f 2a 20 49 74 20 69 73  {.      /* It is
165a3 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f   not possible to
165a4 20 63 72 65 61 74 65 20 61 20 64 61 74 61 62 61   create a databa
165a5 73 65 20 66 6f 72 20 77 68 69 63 68 20 74 68 65  se for which the
165a6 20 66 69 6e 61 6c 20 70 61 67 65 0a 20 20 20 20   final page.    
165a7 20 20 2a 2a 20 69 73 20 65 69 74 68 65 72 20 61    ** is either a
165a8 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67   pointer-map pag
165a9 65 20 6f 72 20 74 68 65 20 70 65 6e 64 69 6e 67  e or the pending
165aa 2d 62 79 74 65 20 70 61 67 65 2e 20 49 66 20 6f  -byte page. If o
165ab 6e 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 65  ne.      ** is e
165ac 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74 68 69 73  ncountered, this
165ad 20 69 6e 64 69 63 61 74 65 73 20 63 6f 72 72 75   indicates corru
165ae 70 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a  ption..      */.
165af 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
165b0 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
165b1 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 46 72  ;.    }..    nFr
165b2 65 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  ee = get4byte(&p
165b3 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74  Bt->pPage1->aDat
165b4 61 5b 33 36 5d 29 3b 0a 20 20 20 20 6e 45 6e 74  a[36]);.    nEnt
165b5 72 79 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65  ry = pBt->usable
165b6 53 69 7a 65 2f 35 3b 0a 20 20 20 20 6e 50 74 72  Size/5;.    nPtr
165b7 6d 61 70 20 3d 20 28 6e 46 72 65 65 2d 6e 4f 72  map = (nFree-nOr
165b8 69 67 2b 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f  ig+PTRMAP_PAGENO
165b9 28 70 42 74 2c 20 6e 4f 72 69 67 29 2b 6e 45 6e  (pBt, nOrig)+nEn
165ba 74 72 79 29 2f 6e 45 6e 74 72 79 3b 0a 20 20 20  try)/nEntry;.   
165bb 20 6e 46 69 6e 20 3d 20 6e 4f 72 69 67 20 2d 20   nFin = nOrig - 
165bc 6e 46 72 65 65 20 2d 20 6e 50 74 72 6d 61 70 3b  nFree - nPtrmap;
165bd 0a 20 20 20 20 69 66 28 20 6e 4f 72 69 67 3e 50  .    if( nOrig>P
165be 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
165bf 28 70 42 74 29 20 26 26 20 6e 46 69 6e 3c 50 45  (pBt) && nFin<PE
165c0 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
165c1 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 46  pBt) ){.      nF
165c2 69 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  in--;.    }.    
165c3 77 68 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53  while( PTRMAP_IS
165c4 50 41 47 45 28 70 42 74 2c 20 6e 46 69 6e 29 20  PAGE(pBt, nFin) 
165c5 7c 7c 20 6e 46 69 6e 3d 3d 50 45 4e 44 49 4e 47  || nFin==PENDING
165c6 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20  _BYTE_PAGE(pBt) 
165c7 29 7b 0a 20 20 20 20 20 20 6e 46 69 6e 2d 2d 3b  ){.      nFin--;
165c8 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e  .    }.    if( n
165c9 46 69 6e 3e 6e 4f 72 69 67 20 29 20 72 65 74 75  Fin>nOrig ) retu
165ca 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
165cb 54 5f 42 4b 50 54 3b 0a 0a 20 20 20 20 66 6f 72  T_BKPT;..    for
165cc 28 69 46 72 65 65 3d 6e 4f 72 69 67 3b 20 69 46  (iFree=nOrig; iF
165cd 72 65 65 3e 6e 46 69 6e 20 26 26 20 72 63 3d 3d  ree>nFin && rc==
165ce 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 46 72 65 65  SQLITE_OK; iFree
165cf 2d 2d 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  --){.      rc = 
165d0 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 70  incrVacuumStep(p
165d1 42 74 2c 20 6e 46 69 6e 2c 20 69 46 72 65 65 29  Bt, nFin, iFree)
165d2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
165d3 28 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45  (rc==SQLITE_DONE
165d4 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   || rc==SQLITE_O
165d5 4b 29 20 26 26 20 6e 46 72 65 65 3e 30 20 29 7b  K) && nFree>0 ){
165d6 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
165d7 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 72 63 20  TE_OK;.      rc 
165d8 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
165d9 69 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31 2d  ite(pBt->pPage1-
165da 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
165db 20 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e   put4byte(&pBt->
165dc 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32  pPage1->aData[32
165dd 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 75 74  ], 0);.      put
165de 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67  4byte(&pBt->pPag
165df 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 30  e1->aData[36], 0
165e0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
165e1 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61  PagerTruncateIma
165e2 67 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  ge(pBt->pPager, 
165e3 6e 46 69 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20  nFin);.    }.   
165e4 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
165e5 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  OK ){.      sqli
165e6 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b  te3PagerRollback
165e7 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a  (pPager);.    }.
165e8 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e    }..  assert( n
165e9 52 65 66 3d 3d 73 71 6c 69 74 65 33 50 61 67 65  Ref==sqlite3Page
165ea 72 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65 72  rRefcount(pPager
165eb 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ) );.  return rc
165ec 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66  ;.}..#else /* if
165ed 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
165ee 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 23  _AUTOVACUUM */.#
165ef 20 64 65 66 69 6e 65 20 73 65 74 43 68 69 6c 64   define setChild
165f0 50 74 72 6d 61 70 73 28 78 29 20 53 51 4c 49 54  Ptrmaps(x) SQLIT
165f1 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  E_OK.#endif../*.
165f2 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
165f3 64 6f 65 73 20 74 68 65 20 66 69 72 73 74 20 70  does the first p
165f4 68 61 73 65 20 6f 66 20 61 20 74 77 6f 2d 70 68  hase of a two-ph
165f5 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68 69  ase commit.  Thi
165f6 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 63 61 75  s routine.** cau
165f7 73 65 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a  ses a rollback j
165f8 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20 63 72 65  ournal to be cre
165f9 61 74 65 64 20 28 69 66 20 69 74 20 64 6f 65 73  ated (if it does
165fa 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69   not already exi
165fb 73 74 29 0a 2a 2a 20 61 6e 64 20 70 6f 70 75 6c  st).** and popul
165fc 61 74 65 64 20 77 69 74 68 20 65 6e 6f 75 67 68  ated with enough
165fd 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 6f 20   information so 
165fe 74 68 61 74 20 69 66 20 61 20 70 6f 77 65 72 20  that if a power 
165ff 6c 6f 73 73 20 6f 63 63 75 72 73 0a 2a 2a 20 74  loss occurs.** t
16600 68 65 20 64 61 74 61 62 61 73 65 20 63 61 6e 20  he database can 
16601 62 65 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69  be restored to i
16602 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74  ts original stat
16603 65 20 62 79 20 70 6c 61 79 69 6e 67 20 62 61 63  e by playing bac
16604 6b 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c  k.** the journal
16605 2e 20 20 54 68 65 6e 20 74 68 65 20 63 6f 6e 74  .  Then the cont
16606 65 6e 74 73 20 6f 66 20 74 68 65 20 6a 6f 75 72  ents of the jour
16607 6e 61 6c 20 61 72 65 20 66 6c 75 73 68 65 64 20  nal are flushed 
16608 6f 75 74 20 74 6f 0a 2a 2a 20 74 68 65 20 64 69  out to.** the di
16609 73 6b 2e 20 20 41 66 74 65 72 20 74 68 65 20 6a  sk.  After the j
1660a 6f 75 72 6e 61 6c 20 69 73 20 73 61 66 65 6c 79  ournal is safely
1660b 20 6f 6e 20 6f 78 69 64 65 2c 20 74 68 65 20 63   on oxide, the c
1660c 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a  hanges to the.**
1660d 20 64 61 74 61 62 61 73 65 20 61 72 65 20 77 72   database are wr
1660e 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 64  itten into the d
1660f 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
16610 20 66 6c 75 73 68 65 64 20 74 6f 20 6f 78 69 64   flushed to oxid
16611 65 2e 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e 64  e..** At the end
16612 20 6f 66 20 74 68 69 73 20 63 61 6c 6c 2c 20 74   of this call, t
16613 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  he rollback jour
16614 6e 61 6c 20 73 74 69 6c 6c 20 65 78 69 73 74 73  nal still exists
16615 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 69 73 6b 20   on the.** disk 
16616 61 6e 64 20 77 65 20 61 72 65 20 73 74 69 6c 6c  and we are still
16617 20 68 6f 6c 64 69 6e 67 20 61 6c 6c 20 6c 6f 63   holding all loc
16618 6b 73 2c 20 73 6f 20 74 68 65 20 74 72 61 6e 73  ks, so the trans
16619 61 63 74 69 6f 6e 20 68 61 73 20 6e 6f 74 0a 2a  action has not.*
1661a 2a 20 63 6f 6d 6d 69 74 74 65 64 2e 20 20 53 65  * committed.  Se
1661b 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  e sqlite3BtreeCo
1661c 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 66  mmitPhaseTwo() f
1661d 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 70 68  or the second ph
1661e 61 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f  ase of the.** co
1661f 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a  mmit process..**
16620 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73  .** This call is
16621 20 61 20 6e 6f 2d 6f 70 20 69 66 20 6e 6f 20 77   a no-op if no w
16622 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
16623 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 63   is currently ac
16624 74 69 76 65 20 6f 6e 20 70 42 74 2e 0a 2a 2a 0a  tive on pBt..**.
16625 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 73 79  ** Otherwise, sy
16626 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20  nc the database 
16627 66 69 6c 65 20 66 6f 72 20 74 68 65 20 62 74 72  file for the btr
16628 65 65 20 70 42 74 2e 20 7a 4d 61 73 74 65 72 20  ee pBt. zMaster 
16629 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65  points to.** the
1662a 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 65   name of a maste
1662b 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74  r journal file t
1662c 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72  hat should be wr
1662d 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 0a 2a  itten into the.*
1662e 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75  * individual jou
1662f 72 6e 61 6c 20 66 69 6c 65 2c 20 6f 72 20 69 73  rnal file, or is
16630 20 4e 55 4c 4c 2c 20 69 6e 64 69 63 61 74 69 6e   NULL, indicatin
16631 67 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72  g no master jour
16632 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 28 73 69  nal file .** (si
16633 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72  ngle database tr
16634 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a  ansaction)..**.*
16635 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20 63  * When this is c
16636 61 6c 6c 65 64 2c 20 74 68 65 20 6d 61 73 74 65  alled, the maste
16637 72 20 6a 6f 75 72 6e 61 6c 20 73 68 6f 75 6c 64  r journal should
16638 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 62 65   already have be
16639 65 6e 0a 2a 2a 20 63 72 65 61 74 65 64 2c 20 70  en.** created, p
1663a 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 74 68  opulated with th
1663b 69 73 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74  is journal point
1663c 65 72 20 61 6e 64 20 73 79 6e 63 65 64 20 74 6f  er and synced to
1663d 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63   disk..**.** Onc
1663e 65 20 74 68 69 73 20 69 73 20 72 6f 75 74 69 6e  e this is routin
1663f 65 20 68 61 73 20 72 65 74 75 72 6e 65 64 2c 20  e has returned, 
16640 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 72  the only thing r
16641 65 71 75 69 72 65 64 20 74 6f 20 63 6f 6d 6d 69  equired to commi
16642 74 0a 2a 2a 20 74 68 65 20 77 72 69 74 65 2d 74  t.** the write-t
16643 72 61 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20 74  ransaction for t
16644 68 69 73 20 64 61 74 61 62 61 73 65 20 66 69 6c  his database fil
16645 65 20 69 73 20 74 6f 20 64 65 6c 65 74 65 20 74  e is to delete t
16646 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a 53  he journal..*/.S
16647 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
16648 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  t sqlite3BtreeCo
16649 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74 72  mmitPhaseOne(Btr
1664a 65 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61  ee *p, const cha
1664b 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 69  r *zMaster){.  i
1664c 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1664d 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72  K;.  if( p->inTr
1664e 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans==TRANS_WRITE
1664f 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64   ){.    BtShared
16650 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
16651 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
16652 45 6e 74 65 72 28 70 29 3b 0a 23 69 66 6e 64 65  Enter(p);.#ifnde
16653 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
16654 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28  TOVACUUM.    if(
16655 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
16656 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61   ){.      rc = a
16657 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74 28  utoVacuumCommit(
16658 70 42 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  pBt);.      if( 
16659 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1665a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1665b 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
1665c 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
1665d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1665e 23 65 6e 64 69 66 0a 20 20 20 20 72 63 20 3d 20  #endif.    rc = 
1665f 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d  sqlite3PagerComm
16660 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2d 3e  itPhaseOne(pBt->
16661 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 2c  pPager, zMaster,
16662 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
16663 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
16664 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
16665 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
16666 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
16667 20 66 72 6f 6d 20 62 6f 74 68 20 42 74 72 65 65   from both Btree
16668 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29  CommitPhaseTwo()
16669 20 61 6e 64 20 42 74 72 65 65 52 6f 6c 6c 62 61   and BtreeRollba
1666a 63 6b 28 29 0a 2a 2a 20 61 74 20 74 68 65 20 63  ck().** at the c
1666b 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 61 20 74  onclusion of a t
1666c 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  ransaction..*/.s
1666d 74 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 65  tatic void btree
1666e 45 6e 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 42  EndTransaction(B
1666f 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68  tree *p){.  BtSh
16670 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
16671 42 74 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a  Bt;.  BtCursor *
16672 70 43 73 72 3b 0a 20 20 61 73 73 65 72 74 28 20  pCsr;.  assert( 
16673 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
16674 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 0a 20 20  sMutex(p) );..  
16675 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20 61 20  /* Search for a 
16676 63 75 72 73 6f 72 20 68 65 6c 64 20 6f 70 65 6e  cursor held open
16677 20 62 79 20 74 68 69 73 20 62 2d 74 72 65 65 20   by this b-tree 
16678 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 66 20 6f  connection. If o
16679 6e 65 20 65 78 69 73 74 73 2c 0a 20 20 2a 2a 20  ne exists,.  ** 
1667a 74 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63  then the transac
1667b 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 64 6f 77  tion will be dow
1667c 6e 67 72 61 64 65 64 20 74 6f 20 61 20 72 65 61  ngraded to a rea
1667d 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74 69  d-only transacti
1667e 6f 6e 0a 20 20 2a 2a 20 69 6e 73 74 65 61 64 20  on.  ** instead 
1667f 6f 66 20 61 63 74 75 61 6c 6c 79 20 63 6f 6e 63  of actually conc
16680 6c 75 64 65 64 2e 20 41 20 73 75 62 73 65 71 75  luded. A subsequ
16681 65 6e 74 20 63 61 6c 6c 20 74 6f 20 43 6f 6d 6d  ent call to Comm
16682 69 74 50 68 61 73 65 54 77 6f 28 29 20 0a 20 20  itPhaseTwo() .  
16683 2a 2a 20 6f 72 20 52 6f 6c 6c 62 61 63 6b 28 29  ** or Rollback()
16684 20 77 69 6c 6c 20 66 69 6e 69 73 68 20 74 68 65   will finish the
16685 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64   transaction and
16686 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61   unlock the data
16687 62 61 73 65 2e 20 20 2a 2f 0a 20 20 66 6f 72 28  base.  */.  for(
16688 70 43 73 72 3d 70 42 74 2d 3e 70 43 75 72 73 6f  pCsr=pBt->pCurso
16689 72 3b 20 70 43 73 72 20 26 26 20 70 43 73 72 2d  r; pCsr && pCsr-
1668a 3e 70 42 74 72 65 65 21 3d 70 3b 20 70 43 73 72  >pBtree!=p; pCsr
1668b 3d 70 43 73 72 2d 3e 70 4e 65 78 74 29 3b 0a 20  =pCsr->pNext);. 
1668c 20 61 73 73 65 72 74 28 20 70 43 73 72 3d 3d 30   assert( pCsr==0
1668d 20 7c 7c 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 54   || p->inTrans>T
1668e 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 0a 20 20  RANS_NONE );..  
1668f 62 74 72 65 65 43 6c 65 61 72 48 61 73 43 6f 6e  btreeClearHasCon
16690 74 65 6e 74 28 70 42 74 29 3b 0a 20 20 69 66 28  tent(pBt);.  if(
16691 20 70 43 73 72 20 29 7b 0a 20 20 20 20 64 6f 77   pCsr ){.    dow
16692 6e 67 72 61 64 65 41 6c 6c 53 68 61 72 65 64 43  ngradeAllSharedC
16693 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70  acheTableLocks(p
16694 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e  );.    p->inTran
16695 73 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a  s = TRANS_READ;.
16696 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
16697 49 66 20 74 68 65 20 68 61 6e 64 6c 65 20 68 61  If the handle ha
16698 64 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 74 72  d any kind of tr
16699 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 20  ansaction open, 
1669a 64 65 63 72 65 6d 65 6e 74 20 74 68 65 20 0a 20  decrement the . 
1669b 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f     ** transactio
1669c 6e 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 73  n count of the s
1669d 68 61 72 65 64 20 62 74 72 65 65 2e 20 49 66 20  hared btree. If 
1669e 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
1669f 63 6f 75 6e 74 20 0a 20 20 20 20 2a 2a 20 72 65  count .    ** re
166a0 61 63 68 65 73 20 30 2c 20 73 65 74 20 74 68 65  aches 0, set the
166a1 20 73 68 61 72 65 64 20 73 74 61 74 65 20 74 6f   shared state to
166a2 20 54 52 41 4e 53 5f 4e 4f 4e 45 2e 20 54 68 65   TRANS_NONE. The
166a3 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e   unlockBtreeIfUn
166a4 75 73 65 64 28 29 0a 20 20 20 20 2a 2a 20 63 61  used().    ** ca
166a5 6c 6c 20 62 65 6c 6f 77 20 77 69 6c 6c 20 75 6e  ll below will un
166a6 6c 6f 63 6b 20 74 68 65 20 70 61 67 65 72 2e 20  lock the pager. 
166a7 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 69   */.    if( p->i
166a8 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f  nTrans!=TRANS_NO
166a9 4e 45 20 29 7b 0a 20 20 20 20 20 20 63 6c 65 61  NE ){.      clea
166aa 72 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54  rAllSharedCacheT
166ab 61 62 6c 65 4c 6f 63 6b 73 28 70 29 3b 0a 20 20  ableLocks(p);.  
166ac 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61      pBt->nTransa
166ad 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20 20 20 20 69  ction--;.      i
166ae 66 28 20 30 3d 3d 70 42 74 2d 3e 6e 54 72 61 6e  f( 0==pBt->nTran
166af 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20  saction ){.     
166b0 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61     pBt->inTransa
166b1 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 4e 4f  ction = TRANS_NO
166b2 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  NE;.      }.    
166b3 7d 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68  }..    /* Set th
166b4 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61  e current transa
166b5 63 74 69 6f 6e 20 73 74 61 74 65 20 74 6f 20 54  ction state to T
166b6 52 41 4e 53 5f 4e 4f 4e 45 20 61 6e 64 20 75 6e  RANS_NONE and un
166b7 6c 6f 63 6b 20 74 68 65 20 0a 20 20 20 20 2a 2a  lock the .    **
166b8 20 70 61 67 65 72 20 69 66 20 74 68 69 73 20 63   pager if this c
166b9 61 6c 6c 20 63 6c 6f 73 65 64 20 74 68 65 20 6f  all closed the o
166ba 6e 6c 79 20 72 65 61 64 20 6f 72 20 77 72 69 74  nly read or writ
166bb 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20  e transaction.  
166bc 2a 2f 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e  */.    p->inTran
166bd 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a  s = TRANS_NONE;.
166be 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49      unlockBtreeI
166bf 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20  fUnused(pBt);.  
166c0 7d 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72  }..  btreeIntegr
166c1 69 74 79 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ity(p);.}../*.**
166c2 20 43 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 6e   Commit the tran
166c3 73 61 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c  saction currentl
166c4 79 20 69 6e 20 70 72 6f 67 72 65 73 73 2e 0a 2a  y in progress..*
166c5 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
166c6 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  e implements the
166c7 20 73 65 63 6f 6e 64 20 70 68 61 73 65 20 6f 66   second phase of
166c8 20 61 20 32 2d 70 68 61 73 65 20 63 6f 6d 6d 69   a 2-phase commi
166c9 74 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74  t.  The.** sqlit
166ca 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
166cb 73 65 4f 6e 65 28 29 20 72 6f 75 74 69 6e 65 20  seOne() routine 
166cc 64 6f 65 73 20 74 68 65 20 66 69 72 73 74 20 70  does the first p
166cd 68 61 73 65 20 61 6e 64 20 73 68 6f 75 6c 64 0a  hase and should.
166ce 2a 2a 20 62 65 20 69 6e 76 6f 6b 65 64 20 70 72  ** be invoked pr
166cf 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74  ior to calling t
166d0 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 54 68  his routine.  Th
166d1 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  e sqlite3BtreeCo
166d2 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 0a 2a  mmitPhaseOne().*
166d3 2a 20 72 6f 75 74 69 6e 65 20 64 69 64 20 61 6c  * routine did al
166d4 6c 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 77 72  l the work of wr
166d5 69 74 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f  iting informatio
166d6 6e 20 6f 75 74 20 74 6f 20 64 69 73 6b 20 61 6e  n out to disk an
166d7 64 20 66 6c 75 73 68 69 6e 67 20 74 68 65 0a 2a  d flushing the.*
166d8 2a 20 63 6f 6e 74 65 6e 74 73 20 73 6f 20 74 68  * contents so th
166d9 61 74 20 74 68 65 79 20 61 72 65 20 77 72 69 74  at they are writ
166da 74 65 6e 20 6f 6e 74 6f 20 74 68 65 20 64 69 73  ten onto the dis
166db 6b 20 70 6c 61 74 74 65 72 2e 20 20 41 6c 6c 20  k platter.  All 
166dc 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  this.** routine 
166dd 68 61 73 20 74 6f 20 64 6f 20 69 73 20 64 65 6c  has to do is del
166de 65 74 65 20 6f 72 20 74 72 75 6e 63 61 74 65 20  ete or truncate 
166df 6f 72 20 7a 65 72 6f 20 74 68 65 20 68 65 61 64  or zero the head
166e0 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 74 68 65  er in the.** the
166e1 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
166e2 6c 20 28 77 68 69 63 68 20 63 61 75 73 65 73 20  l (which causes 
166e3 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
166e4 74 6f 20 63 6f 6d 6d 69 74 29 20 61 6e 64 0a 2a  to commit) and.*
166e5 2a 20 64 72 6f 70 20 6c 6f 63 6b 73 2e 0a 2a 2a  * drop locks..**
166e6 0a 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 72 65  .** This will re
166e7 6c 65 61 73 65 20 74 68 65 20 77 72 69 74 65 20  lease the write 
166e8 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
166e9 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 74  base file.  If t
166ea 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61  here.** are no a
166eb 63 74 69 76 65 20 63 75 72 73 6f 72 73 2c 20 69  ctive cursors, i
166ec 74 20 61 6c 73 6f 20 72 65 6c 65 61 73 65 73 20  t also releases 
166ed 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a  the read lock..*
166ee 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
166ef 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
166f0 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28  eCommitPhaseTwo(
166f1 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53  Btree *p){.  BtS
166f2 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
166f3 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42  pBt;..  sqlite3B
166f4 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
166f5 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70  btreeIntegrity(p
166f6 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
166f7 68 61 6e 64 6c 65 20 68 61 73 20 61 20 77 72 69  handle has a wri
166f8 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  te-transaction o
166f9 70 65 6e 2c 20 63 6f 6d 6d 69 74 20 74 68 65 20  pen, commit the 
166fa 73 68 61 72 65 64 2d 62 74 72 65 65 73 20 0a 20  shared-btrees . 
166fb 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20   ** transaction 
166fc 61 6e 64 20 73 65 74 20 74 68 65 20 73 68 61 72  and set the shar
166fd 65 64 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e  ed state to TRAN
166fe 53 5f 52 45 41 44 2e 0a 20 20 2a 2f 0a 20 20 69  S_READ..  */.  i
166ff 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54  f( p->inTrans==T
16700 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20  RANS_WRITE ){.  
16701 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 61 73    int rc;.    as
16702 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61  sert( pBt->inTra
16703 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f  nsaction==TRANS_
16704 57 52 49 54 45 20 29 3b 0a 20 20 20 20 61 73 73  WRITE );.    ass
16705 65 72 74 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73  ert( pBt->nTrans
16706 61 63 74 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20  action>0 );.    
16707 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
16708 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28  rCommitPhaseTwo(
16709 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20  pBt->pPager);.  
1670a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1670b 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
1670c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
1670d 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
1670e 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42  rc;.    }.    pB
1670f 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
16710 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20   = TRANS_READ;. 
16711 20 7d 0a 0a 20 20 62 74 72 65 65 45 6e 64 54 72   }..  btreeEndTr
16712 61 6e 73 61 63 74 69 6f 6e 28 70 29 3b 0a 20 20  ansaction(p);.  
16713 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
16714 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  e(p);.  return S
16715 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
16716 2a 2a 20 44 6f 20 62 6f 74 68 20 70 68 61 73 65  ** Do both phase
16717 73 20 6f 66 20 61 20 63 6f 6d 6d 69 74 2e 0a 2a  s of a commit..*
16718 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16719 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
1671a 65 43 6f 6d 6d 69 74 28 42 74 72 65 65 20 2a 70  eCommit(Btree *p
1671b 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  ){.  int rc;.  s
1671c 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1671d 28 70 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  (p);.  rc = sqli
1671e 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68  te3BtreeCommitPh
1671f 61 73 65 4f 6e 65 28 70 2c 20 30 29 3b 0a 20 20  aseOne(p, 0);.  
16720 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
16721 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  K ){.    rc = sq
16722 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74  lite3BtreeCommit
16723 50 68 61 73 65 54 77 6f 28 70 29 3b 0a 20 20 7d  PhaseTwo(p);.  }
16724 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
16725 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  eave(p);.  retur
16726 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  n rc;.}..#ifndef
16727 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65   NDEBUG./*.** Re
16728 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
16729 6f 66 20 77 72 69 74 65 2d 63 75 72 73 6f 72 73  of write-cursors
1672a 20 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20 68 61   open on this ha
1672b 6e 64 6c 65 2e 20 54 68 69 73 20 69 73 20 66 6f  ndle. This is fo
1672c 72 20 75 73 65 0a 2a 2a 20 69 6e 20 61 73 73 65  r use.** in asse
1672d 72 74 28 29 20 65 78 70 72 65 73 73 69 6f 6e 73  rt() expressions
1672e 2c 20 73 6f 20 69 74 20 69 73 20 6f 6e 6c 79 20  , so it is only 
1672f 63 6f 6d 70 69 6c 65 64 20 69 66 20 4e 44 45 42  compiled if NDEB
16730 55 47 20 69 73 20 6e 6f 74 0a 2a 2a 20 64 65 66  UG is not.** def
16731 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  ined..**.** For 
16732 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20  the purposes of 
16733 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 20  this routine, a 
16734 77 72 69 74 65 2d 63 75 72 73 6f 72 20 69 73 20  write-cursor is 
16735 61 6e 79 20 63 75 72 73 6f 72 20 74 68 61 74 0a  any cursor that.
16736 2a 2a 20 69 73 20 63 61 70 61 62 6c 65 20 6f 66  ** is capable of
16737 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20   writing to the 
16738 64 61 74 61 62 73 65 2e 20 20 54 68 61 74 20 6d  databse.  That m
16739 65 61 6e 73 20 74 68 65 20 63 75 72 73 6f 72 20  eans the cursor 
1673a 77 61 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c  was.** originall
1673b 79 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69  y opened for wri
1673c 74 69 6e 67 20 61 6e 64 20 74 68 65 20 63 75 72  ting and the cur
1673d 73 6f 72 20 68 61 73 20 6e 6f 74 20 62 65 20 64  sor has not be d
1673e 69 73 61 62 6c 65 64 0a 2a 2a 20 62 79 20 68 61  isabled.** by ha
1673f 76 69 6e 67 20 69 74 73 20 73 74 61 74 65 20 63  ving its state c
16740 68 61 6e 67 65 64 20 74 6f 20 43 55 52 53 4f 52  hanged to CURSOR
16741 5f 46 41 55 4c 54 2e 0a 2a 2f 0a 73 74 61 74 69  _FAULT..*/.stati
16742 63 20 69 6e 74 20 63 6f 75 6e 74 57 72 69 74 65  c int countWrite
16743 43 75 72 73 6f 72 73 28 42 74 53 68 61 72 65 64  Cursors(BtShared
16744 20 2a 70 42 74 29 7b 0a 20 20 42 74 43 75 72 73   *pBt){.  BtCurs
16745 6f 72 20 2a 70 43 75 72 3b 0a 20 20 69 6e 74 20  or *pCur;.  int 
16746 72 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 43 75  r = 0;.  for(pCu
16747 72 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20  r=pBt->pCursor; 
16748 70 43 75 72 3b 20 70 43 75 72 3d 70 43 75 72 2d  pCur; pCur=pCur-
16749 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28  >pNext){.    if(
1674a 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 26 26   pCur->wrFlag &&
1674b 20 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43   pCur->eState!=C
1674c 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 20 72 2b  URSOR_FAULT ) r+
1674d 2b 3b 20 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  +; .  }.  return
1674e 20 72 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a   r;.}.#endif../*
1674f 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
16750 20 73 65 74 73 20 74 68 65 20 73 74 61 74 65 20   sets the state 
16751 74 6f 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20  to CURSOR_FAULT 
16752 61 6e 64 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a  and the error.**
16753 20 63 6f 64 65 20 74 6f 20 65 72 72 43 6f 64 65   code to errCode
16754 20 66 6f 72 20 65 76 65 72 79 20 63 75 72 73 6f   for every curso
16755 72 20 6f 6e 20 42 74 53 68 61 72 65 64 20 74 68  r on BtShared th
16756 61 74 20 70 42 74 72 65 65 0a 2a 2a 20 72 65 66  at pBtree.** ref
16757 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 45  erences..**.** E
16758 76 65 72 79 20 63 75 72 73 6f 72 20 69 73 20 74  very cursor is t
16759 72 69 70 70 65 64 2c 20 69 6e 63 6c 75 64 69 6e  ripped, includin
1675a 67 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 62  g cursors that b
1675b 65 6c 6f 6e 67 0a 2a 2a 20 74 6f 20 6f 74 68 65  elong.** to othe
1675c 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  r database conne
1675d 63 74 69 6f 6e 73 20 74 68 61 74 20 68 61 70 70  ctions that happ
1675e 65 6e 20 74 6f 20 62 65 20 73 68 61 72 69 6e 67  en to be sharing
1675f 0a 2a 2a 20 74 68 65 20 63 61 63 68 65 20 77 69  .** the cache wi
16760 74 68 20 70 42 74 72 65 65 2e 0a 2a 2a 0a 2a 2a  th pBtree..**.**
16761 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65   This routine ge
16762 74 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61  ts called when a
16763 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73   rollback occurs
16764 2e 0a 2a 2a 20 41 6c 6c 20 63 75 72 73 6f 72 73  ..** All cursors
16765 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20   using the same 
16766 63 61 63 68 65 20 6d 75 73 74 20 62 65 20 74 72  cache must be tr
16767 69 70 70 65 64 0a 2a 2a 20 74 6f 20 70 72 65 76  ipped.** to prev
16768 65 6e 74 20 74 68 65 6d 20 66 72 6f 6d 20 74 72  ent them from tr
16769 79 69 6e 67 20 74 6f 20 75 73 65 20 74 68 65 20  ying to use the 
1676a 62 74 72 65 65 20 61 66 74 65 72 0a 2a 2a 20 74  btree after.** t
1676b 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 20 20 54 68  he rollback.  Th
1676c 65 20 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 68  e rollback may h
1676d 61 76 65 20 64 65 6c 65 74 65 64 20 74 61 62 6c  ave deleted tabl
1676e 65 73 0a 2a 2a 20 6f 72 20 6d 6f 76 65 64 20 72  es.** or moved r
1676f 6f 6f 74 20 70 61 67 65 73 2c 20 73 6f 20 69 74  oot pages, so it
16770 20 69 73 20 6e 6f 74 20 73 75 66 66 69 63 69 65   is not sufficie
16771 6e 74 20 74 6f 0a 2a 2a 20 73 61 76 65 20 74 68  nt to.** save th
16772 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 63  e state of the c
16773 75 72 73 6f 72 2e 20 20 54 68 65 20 63 75 72 73  ursor.  The curs
16774 6f 72 20 6d 75 73 74 20 62 65 0a 2a 2a 20 69 6e  or must be.** in
16775 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2f 0a 53 51  validated..*/.SQ
16776 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
16777 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 72  d sqlite3BtreeTr
16778 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 72  ipAllCursors(Btr
16779 65 65 20 2a 70 42 74 72 65 65 2c 20 69 6e 74 20  ee *pBtree, int 
1677a 65 72 72 43 6f 64 65 29 7b 0a 20 20 42 74 43 75  errCode){.  BtCu
1677b 72 73 6f 72 20 2a 70 3b 0a 20 20 73 71 6c 69 74  rsor *p;.  sqlit
1677c 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 42 74  e3BtreeEnter(pBt
1677d 72 65 65 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42  ree);.  for(p=pB
1677e 74 72 65 65 2d 3e 70 42 74 2d 3e 70 43 75 72 73  tree->pBt->pCurs
1677f 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78  or; p; p=p->pNex
16780 74 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20  t){.    int i;. 
16781 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43     sqlite3BtreeC
16782 6c 65 61 72 43 75 72 73 6f 72 28 70 29 3b 0a 20  learCursor(p);. 
16783 20 20 20 70 2d 3e 65 53 74 61 74 65 20 3d 20 43     p->eState = C
16784 55 52 53 4f 52 5f 46 41 55 4c 54 3b 0a 20 20 20  URSOR_FAULT;.   
16785 20 70 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 65   p->skipNext = e
16786 72 72 43 6f 64 65 3b 0a 20 20 20 20 66 6f 72 28  rrCode;.    for(
16787 69 3d 30 3b 20 69 3c 3d 70 2d 3e 69 50 61 67 65  i=0; i<=p->iPage
16788 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65  ; i++){.      re
16789 6c 65 61 73 65 50 61 67 65 28 70 2d 3e 61 70 50  leasePage(p->apP
1678a 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70  age[i]);.      p
1678b 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d 20 30 3b  ->apPage[i] = 0;
1678c 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c  .    }.  }.  sql
1678d 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
1678e 42 74 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Btree);.}../*.**
1678f 20 52 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72   Rollback the tr
16790 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f  ansaction in pro
16791 67 72 65 73 73 2e 20 20 41 6c 6c 20 63 75 72 73  gress.  All curs
16792 6f 72 73 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 69  ors will be.** i
16793 6e 76 61 6c 69 64 65 64 20 62 79 20 74 68 69 73  nvalided by this
16794 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 41 6e 79   operation.  Any
16795 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20   attempt to use 
16796 61 20 63 75 72 73 6f 72 0a 2a 2a 20 74 68 61 74  a cursor.** that
16797 20 77 61 73 20 6f 70 65 6e 20 61 74 20 74 68 65   was open at the
16798 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
16799 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c  is operation wil
1679a 6c 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 61  l result.** in a
1679b 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54  n error..**.** T
1679c 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 61 73 65  his will release
1679d 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 20   the write lock 
1679e 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
1679f 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65 0a  file.  If there.
167a0 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65  ** are no active
167a1 20 63 75 72 73 6f 72 73 2c 20 69 74 20 61 6c 73   cursors, it als
167a2 6f 20 72 65 6c 65 61 73 65 73 20 74 68 65 20 72  o releases the r
167a3 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c  ead lock..*/.SQL
167a4 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
167a5 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c  sqlite3BtreeRoll
167a6 62 61 63 6b 28 42 74 72 65 65 20 2a 70 29 7b 0a  back(Btree *p){.
167a7 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68    int rc;.  BtSh
167a8 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
167a9 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  Bt;.  MemPage *p
167aa 50 61 67 65 31 3b 0a 0a 20 20 73 71 6c 69 74 65  Page1;..  sqlite
167ab 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
167ac 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75    rc = saveAllCu
167ad 72 73 6f 72 73 28 70 42 74 2c 20 30 2c 20 30 29  rsors(pBt, 0, 0)
167ae 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
167af 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
167b0 48 45 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  HE.  if( rc!=SQL
167b1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a  ITE_OK ){.    /*
167b2 20 54 68 69 73 20 69 73 20 61 20 68 6f 72 72 69   This is a horri
167b3 62 6c 65 20 73 69 74 75 61 74 69 6f 6e 2e 20 41  ble situation. A
167b4 6e 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29  n IO or malloc()
167b5 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20   error occurred 
167b6 77 68 69 6c 73 74 0a 20 20 20 20 2a 2a 20 74 72  whilst.    ** tr
167b7 79 69 6e 67 20 74 6f 20 73 61 76 65 20 63 75 72  ying to save cur
167b8 73 6f 72 20 70 6f 73 69 74 69 6f 6e 73 2e 20 49  sor positions. I
167b9 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74  f this is an aut
167ba 6f 6d 61 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20  omatic rollback 
167bb 28 61 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 72  (as.    ** the r
167bc 65 73 75 6c 74 20 6f 66 20 61 20 63 6f 6e 73 74  esult of a const
167bd 72 61 69 6e 74 2c 20 6d 61 6c 6c 6f 63 28 29 20  raint, malloc() 
167be 66 61 69 6c 75 72 65 20 6f 72 20 49 4f 20 65 72  failure or IO er
167bf 72 6f 72 29 20 74 68 65 6e 20 0a 20 20 20 20 2a  ror) then .    *
167c0 2a 20 74 68 65 20 63 61 63 68 65 20 6d 61 79 20  * the cache may 
167c1 62 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 69 6e  be internally in
167c2 63 6f 6e 73 69 73 74 65 6e 74 20 28 6e 6f 74 20  consistent (not 
167c3 63 6f 6e 74 61 69 6e 20 76 61 6c 69 64 20 74 72  contain valid tr
167c4 65 65 73 29 20 73 6f 0a 20 20 20 20 2a 2a 20 77  ees) so.    ** w
167c5 65 20 63 61 6e 6e 6f 74 20 73 69 6d 70 6c 79 20  e cannot simply 
167c6 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72  return the error
167c7 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 20   to the caller. 
167c8 49 6e 73 74 65 61 64 2c 20 61 62 6f 72 74 20 0a  Instead, abort .
167c9 20 20 20 20 2a 2a 20 61 6c 6c 20 71 75 65 72 69      ** all queri
167ca 65 73 20 74 68 61 74 20 6d 61 79 20 62 65 20 75  es that may be u
167cb 73 69 6e 67 20 61 6e 79 20 6f 66 20 74 68 65 20  sing any of the 
167cc 63 75 72 73 6f 72 73 20 74 68 61 74 20 66 61 69  cursors that fai
167cd 6c 65 64 20 74 6f 20 73 61 76 65 2e 0a 20 20 20  led to save..   
167ce 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42   */.    sqlite3B
167cf 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f  treeTripAllCurso
167d0 72 73 28 70 2c 20 72 63 29 3b 0a 20 20 7d 0a 23  rs(p, rc);.  }.#
167d1 65 6e 64 69 66 0a 20 20 62 74 72 65 65 49 6e 74  endif.  btreeInt
167d2 65 67 72 69 74 79 28 70 29 3b 0a 0a 20 20 69 66  egrity(p);..  if
167d3 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  ( p->inTrans==TR
167d4 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20  ANS_WRITE ){.   
167d5 20 69 6e 74 20 72 63 32 3b 0a 0a 20 20 20 20 61   int rc2;..    a
167d6 73 73 65 72 74 28 20 54 52 41 4e 53 5f 57 52 49  ssert( TRANS_WRI
167d7 54 45 3d 3d 70 42 74 2d 3e 69 6e 54 72 61 6e 73  TE==pBt->inTrans
167d8 61 63 74 69 6f 6e 20 29 3b 0a 20 20 20 20 72 63  action );.    rc
167d9 32 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  2 = sqlite3Pager
167da 52 6f 6c 6c 62 61 63 6b 28 70 42 74 2d 3e 70 50  Rollback(pBt->pP
167db 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72  ager);.    if( r
167dc 63 32 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  c2!=SQLITE_OK ){
167dd 0a 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b  .      rc = rc2;
167de 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54  .    }..    /* T
167df 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20  he rollback may 
167e0 68 61 76 65 20 64 65 73 74 72 6f 79 65 64 20 74  have destroyed t
167e1 68 65 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61  he pPage1->aData
167e2 20 76 61 6c 75 65 2e 20 20 53 6f 0a 20 20 20 20   value.  So.    
167e3 2a 2a 20 63 61 6c 6c 20 62 74 72 65 65 47 65 74  ** call btreeGet
167e4 50 61 67 65 28 29 20 6f 6e 20 70 61 67 65 20 31  Page() on page 1
167e5 20 61 67 61 69 6e 20 74 6f 20 6d 61 6b 65 0a 20   again to make. 
167e6 20 20 20 2a 2a 20 73 75 72 65 20 70 50 61 67 65     ** sure pPage
167e7 31 2d 3e 61 44 61 74 61 20 69 73 20 73 65 74 20  1->aData is set 
167e8 63 6f 72 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20  correctly. */.  
167e9 20 20 69 66 28 20 62 74 72 65 65 47 65 74 50 61    if( btreeGetPa
167ea 67 65 28 70 42 74 2c 20 31 2c 20 26 70 50 61 67  ge(pBt, 1, &pPag
167eb 65 31 2c 20 30 29 3d 3d 53 51 4c 49 54 45 5f 4f  e1, 0)==SQLITE_O
167ec 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61  K ){.      relea
167ed 73 65 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a  sePage(pPage1);.
167ee 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
167ef 28 20 63 6f 75 6e 74 57 72 69 74 65 43 75 72 73  ( countWriteCurs
167f0 6f 72 73 28 70 42 74 29 3d 3d 30 20 29 3b 0a 20  ors(pBt)==0 );. 
167f1 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61     pBt->inTransa
167f2 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 45  ction = TRANS_RE
167f3 41 44 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65  AD;.  }..  btree
167f4 45 6e 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 70  EndTransaction(p
167f5 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
167f6 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
167f7 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
167f8 20 53 74 61 72 74 20 61 20 73 74 61 74 65 6d 65   Start a stateme
167f9 6e 74 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f  nt subtransactio
167fa 6e 2e 20 54 68 65 20 73 75 62 74 72 61 6e 73 61  n. The subtransa
167fb 63 74 69 6f 6e 20 63 61 6e 20 63 61 6e 20 62 65  ction can can be
167fc 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63 6b 20   rolled.** back 
167fd 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79 20 6f 66  independently of
167fe 20 74 68 65 20 6d 61 69 6e 20 74 72 61 6e 73 61   the main transa
167ff 63 74 69 6f 6e 2e 20 59 6f 75 20 6d 75 73 74 20  ction. You must 
16800 73 74 61 72 74 20 61 20 74 72 61 6e 73 61 63 74  start a transact
16801 69 6f 6e 20 0a 2a 2a 20 62 65 66 6f 72 65 20 73  ion .** before s
16802 74 61 72 74 69 6e 67 20 61 20 73 75 62 74 72 61  tarting a subtra
16803 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 73 75  nsaction. The su
16804 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20  btransaction is 
16805 65 6e 64 65 64 20 61 75 74 6f 6d 61 74 69 63 61  ended automatica
16806 6c 6c 79 20 0a 2a 2a 20 69 66 20 74 68 65 20 6d  lly .** if the m
16807 61 69 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ain transaction 
16808 63 6f 6d 6d 69 74 73 20 6f 72 20 72 6f 6c 6c 73  commits or rolls
16809 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 74 61   back..**.** Sta
1680a 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61  tement subtransa
1680b 63 74 69 6f 6e 73 20 61 72 65 20 75 73 65 64 20  ctions are used 
1680c 61 72 6f 75 6e 64 20 69 6e 64 69 76 69 64 75 61  around individua
1680d 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  l SQL statements
1680e 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 63 6f 6e  .** that are con
1680f 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20 61 20  tained within a 
16810 42 45 47 49 4e 2e 2e 2e 43 4f 4d 4d 49 54 20 62  BEGIN...COMMIT b
16811 6c 6f 63 6b 2e 20 20 49 66 20 61 20 63 6f 6e 73  lock.  If a cons
16812 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72 6f 72 20  traint.** error 
16813 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 74 68  occurs within th
16814 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65  e statement, the
16815 20 65 66 66 65 63 74 20 6f 66 20 74 68 61 74 20   effect of that 
16816 6f 6e 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  one statement.**
16817 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64 20 62   can be rolled b
16818 61 63 6b 20 77 69 74 68 6f 75 74 20 68 61 76 69  ack without havi
16819 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74  ng to rollback t
1681a 68 65 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61  he entire transa
1681b 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 73  ction..**.** A s
1681c 74 61 74 65 6d 65 6e 74 20 73 75 62 2d 74 72 61  tatement sub-tra
1681d 6e 73 61 63 74 69 6f 6e 20 69 73 20 69 6d 70 6c  nsaction is impl
1681e 65 6d 65 6e 74 65 64 20 61 73 20 61 6e 20 61 6e  emented as an an
1681f 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e  onymous savepoin
16820 74 2e 20 54 68 65 0a 2a 2a 20 76 61 6c 75 65 20  t. The.** value 
16821 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65  passed as the se
16822 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69  cond parameter i
16823 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  s the total numb
16824 65 72 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 73  er of savepoints
16825 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 74  ,.** including t
16826 68 65 20 6e 65 77 20 61 6e 6f 6e 79 6d 6f 75 73  he new anonymous
16827 20 73 61 76 65 70 6f 69 6e 74 2c 20 6f 70 65 6e   savepoint, open
16828 20 6f 6e 20 74 68 65 20 42 2d 54 72 65 65 2e 20   on the B-Tree. 
16829 69 2e 65 2e 20 69 66 20 74 68 65 72 65 0a 2a 2a  i.e. if there.**
1682a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 73   are no active s
1682b 61 76 65 70 6f 69 6e 74 73 20 61 6e 64 20 6e 6f  avepoints and no
1682c 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74   other statement
1682d 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 6f 70  -transactions op
1682e 65 6e 2c 0a 2a 2a 20 69 53 74 61 74 65 6d 65 6e  en,.** iStatemen
1682f 74 20 69 73 20 31 2e 20 54 68 69 73 20 61 6e 6f  t is 1. This ano
16830 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e 74  nymous savepoint
16831 20 63 61 6e 20 62 65 20 72 65 6c 65 61 73 65 64   can be released
16832 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 0a   or rolled back.
16833 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c  ** using the sql
16834 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69  ite3BtreeSavepoi
16835 6e 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  nt() function..*
16836 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16837 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
16838 65 42 65 67 69 6e 53 74 6d 74 28 42 74 72 65 65  eBeginStmt(Btree
16839 20 2a 70 2c 20 69 6e 74 20 69 53 74 61 74 65 6d   *p, int iStatem
1683a 65 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  ent){.  int rc;.
1683b 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
1683c 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69  = p->pBt;.  sqli
1683d 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
1683e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  ;.  assert( p->i
1683f 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52  nTrans==TRANS_WR
16840 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ITE );.  assert(
16841 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d   pBt->readOnly==
16842 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  0 );.  assert( i
16843 53 74 61 74 65 6d 65 6e 74 3e 30 20 29 3b 0a 20  Statement>0 );. 
16844 20 61 73 73 65 72 74 28 20 69 53 74 61 74 65 6d   assert( iStatem
16845 65 6e 74 3e 70 2d 3e 64 62 2d 3e 6e 53 61 76 65  ent>p->db->nSave
16846 70 6f 69 6e 74 20 29 3b 0a 20 20 69 66 28 20 4e  point );.  if( N
16847 45 56 45 52 28 70 2d 3e 69 6e 54 72 61 6e 73 21  EVER(p->inTrans!
16848 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 7c 7c 20  =TRANS_WRITE || 
16849 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 29  pBt->readOnly) )
1684a 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
1684b 45 5f 49 4e 54 45 52 4e 41 4c 3b 0a 20 20 7d 65  E_INTERNAL;.  }e
1684c 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
1684d 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74   pBt->inTransact
1684e 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ion==TRANS_WRITE
1684f 20 29 3b 0a 20 20 20 20 2f 2a 20 41 74 20 74 68   );.    /* At th
16850 65 20 70 61 67 65 72 20 6c 65 76 65 6c 2c 20 61  e pager level, a
16851 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73   statement trans
16852 61 63 74 69 6f 6e 20 69 73 20 61 20 73 61 76 65  action is a save
16853 70 6f 69 6e 74 20 77 69 74 68 0a 20 20 20 20 2a  point with.    *
16854 2a 20 61 6e 20 69 6e 64 65 78 20 67 72 65 61 74  * an index great
16855 65 72 20 74 68 61 6e 20 61 6c 6c 20 73 61 76 65  er than all save
16856 70 6f 69 6e 74 73 20 63 72 65 61 74 65 64 20 65  points created e
16857 78 70 6c 69 63 69 74 6c 79 20 75 73 69 6e 67 0a  xplicitly using.
16858 20 20 20 20 2a 2a 20 53 51 4c 20 73 74 61 74 65      ** SQL state
16859 6d 65 6e 74 73 2e 20 49 74 20 69 73 20 69 6c 6c  ments. It is ill
1685a 65 67 61 6c 20 74 6f 20 6f 70 65 6e 2c 20 72 65  egal to open, re
1685b 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c 62 61 63  lease or rollbac
1685c 6b 20 61 6e 79 0a 20 20 20 20 2a 2a 20 73 75 63  k any.    ** suc
1685d 68 20 73 61 76 65 70 6f 69 6e 74 73 20 77 68 69  h savepoints whi
1685e 6c 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  le the statement
1685f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76   transaction sav
16860 65 70 6f 69 6e 74 20 69 73 20 61 63 74 69 76 65  epoint is active
16861 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20  ..    */.    rc 
16862 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70  = sqlite3PagerOp
16863 65 6e 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d  enSavepoint(pBt-
16864 3e 70 50 61 67 65 72 2c 20 69 53 74 61 74 65 6d  >pPager, iStatem
16865 65 6e 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  ent);.  }.  sqli
16866 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
16867 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
16868 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f  ../*.** The seco
16869 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  nd argument to t
1686a 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 70  his function, op
1686b 2c 20 69 73 20 61 6c 77 61 79 73 20 53 41 56 45  , is always SAVE
1686c 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 0a 2a  POINT_ROLLBACK.*
1686d 2a 20 6f 72 20 53 41 56 45 50 4f 49 4e 54 5f 52  * or SAVEPOINT_R
1686e 45 4c 45 41 53 45 2e 20 54 68 69 73 20 66 75 6e  ELEASE. This fun
1686f 63 74 69 6f 6e 20 65 69 74 68 65 72 20 72 65 6c  ction either rel
16870 65 61 73 65 73 20 6f 72 20 72 6f 6c 6c 73 20 62  eases or rolls b
16871 61 63 6b 20 74 68 65 0a 2a 2a 20 73 61 76 65 70  ack the.** savep
16872 6f 69 6e 74 20 69 64 65 6e 74 69 66 69 65 64 20  oint identified 
16873 62 79 20 70 61 72 61 6d 65 74 65 72 20 69 53 61  by parameter iSa
16874 76 65 70 6f 69 6e 74 2c 20 64 65 70 65 6e 64 69  vepoint, dependi
16875 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20  ng on the value 
16876 0a 2a 2a 20 6f 66 20 6f 70 2e 0a 2a 2a 0a 2a 2a  .** of op..**.**
16877 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 53 61 76 65   Normally, iSave
16878 70 6f 69 6e 74 20 69 73 20 67 72 65 61 74 65 72  point is greater
16879 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
1687a 6f 20 7a 65 72 6f 2e 20 48 6f 77 65 76 65 72 2c  o zero. However,
1687b 20 69 66 20 6f 70 20 69 73 0a 2a 2a 20 53 41 56   if op is.** SAV
1687c 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c  EPOINT_ROLLBACK,
1687d 20 74 68 65 6e 20 69 53 61 76 65 70 6f 69 6e 74   then iSavepoint
1687e 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 2d 31 2e   may also be -1.
1687f 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
16880 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f  e .** contents o
16881 66 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 61  f the entire tra
16882 6e 73 61 63 74 69 6f 6e 20 61 72 65 20 72 6f 6c  nsaction are rol
16883 6c 65 64 20 62 61 63 6b 2e 20 54 68 69 73 20 69  led back. This i
16884 73 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66  s different.** f
16885 72 6f 6d 20 61 20 6e 6f 72 6d 61 6c 20 74 72 61  rom a normal tra
16886 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 62 61 63  nsaction rollbac
16887 6b 2c 20 61 73 20 6e 6f 20 6c 6f 63 6b 73 20 61  k, as no locks a
16888 72 65 20 72 65 6c 65 61 73 65 64 20 61 6e 64 20  re released and 
16889 74 68 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69  the.** transacti
1688a 6f 6e 20 72 65 6d 61 69 6e 73 20 6f 70 65 6e 2e  on remains open.
1688b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1688c 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
1688d 72 65 65 53 61 76 65 70 6f 69 6e 74 28 42 74 72  reeSavepoint(Btr
1688e 65 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69  ee *p, int op, i
1688f 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 7b 0a  nt iSavepoint){.
16890 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
16891 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 20 26 26  E_OK;.  if( p &&
16892 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41   p->inTrans==TRA
16893 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20  NS_WRITE ){.    
16894 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
16895 70 2d 3e 70 42 74 3b 0a 20 20 20 20 61 73 73 65  p->pBt;.    asse
16896 72 74 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e  rt( op==SAVEPOIN
16897 54 5f 52 45 4c 45 41 53 45 20 7c 7c 20 6f 70 3d  T_RELEASE || op=
16898 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42  =SAVEPOINT_ROLLB
16899 41 43 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ACK );.    asser
1689a 74 28 20 69 53 61 76 65 70 6f 69 6e 74 3e 3d 30  t( iSavepoint>=0
1689b 20 7c 7c 20 28 69 53 61 76 65 70 6f 69 6e 74 3d   || (iSavepoint=
1689c 3d 2d 31 20 26 26 20 6f 70 3d 3d 53 41 56 45 50  =-1 && op==SAVEP
1689d 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 29 20 29  OINT_ROLLBACK) )
1689e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
1689f 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 20 20  eeEnter(p);.    
168a0 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
168a1 72 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e  rSavepoint(pBt->
168a2 70 50 61 67 65 72 2c 20 6f 70 2c 20 69 53 61 76  pPager, op, iSav
168a3 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 69 66 28  epoint);.    if(
168a4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
168a5 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 65 77  {.      rc = new
168a6 44 61 74 61 62 61 73 65 28 70 42 74 29 3b 0a 20  Database(pBt);. 
168a7 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
168a8 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
168a9 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
168aa 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  }../*.** Create 
168ab 61 20 6e 65 77 20 63 75 72 73 6f 72 20 66 6f 72  a new cursor for
168ac 20 74 68 65 20 42 54 72 65 65 20 77 68 6f 73 65   the BTree whose
168ad 20 72 6f 6f 74 20 69 73 20 6f 6e 20 74 68 65 20   root is on the 
168ae 70 61 67 65 0a 2a 2a 20 69 54 61 62 6c 65 2e 20  page.** iTable. 
168af 49 66 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 63  If a read-only c
168b0 75 72 73 6f 72 20 69 73 20 72 65 71 75 65 73 74  ursor is request
168b1 65 64 2c 20 69 74 20 69 73 20 61 73 73 75 6d 65  ed, it is assume
168b2 64 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 61  d that.** the ca
168b3 6c 6c 65 72 20 61 6c 72 65 61 64 79 20 68 61 73  ller already has
168b4 20 61 74 20 6c 65 61 73 74 20 61 20 72 65 61 64   at least a read
168b5 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74 69 6f  -only transactio
168b6 6e 20 6f 70 65 6e 0a 2a 2a 20 6f 6e 20 74 68 65  n open.** on the
168b7 20 64 61 74 61 62 61 73 65 20 61 6c 72 65 61 64   database alread
168b8 79 2e 20 49 66 20 61 20 77 72 69 74 65 2d 63 75  y. If a write-cu
168b9 72 73 6f 72 20 69 73 20 72 65 71 75 65 73 74 65  rsor is requeste
168ba 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 63  d, then.** the c
168bb 61 6c 6c 65 72 20 69 73 20 61 73 73 75 6d 65 64  aller is assumed
168bc 20 74 6f 20 68 61 76 65 20 61 6e 20 6f 70 65 6e   to have an open
168bd 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
168be 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72 46  on..**.** If wrF
168bf 6c 61 67 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65  lag==0, then the
168c0 20 63 75 72 73 6f 72 20 63 61 6e 20 6f 6e 6c 79   cursor can only
168c1 20 62 65 20 75 73 65 64 20 66 6f 72 20 72 65 61   be used for rea
168c2 64 69 6e 67 2e 0a 2a 2a 20 49 66 20 77 72 46 6c  ding..** If wrFl
168c3 61 67 3d 3d 31 2c 20 74 68 65 6e 20 74 68 65 20  ag==1, then the 
168c4 63 75 72 73 6f 72 20 63 61 6e 20 62 65 20 75 73  cursor can be us
168c5 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f  ed for reading o
168c6 72 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67  r for.** writing
168c7 20 69 66 20 6f 74 68 65 72 20 63 6f 6e 64 69 74   if other condit
168c8 69 6f 6e 73 20 66 6f 72 20 77 72 69 74 69 6e 67  ions for writing
168c9 20 61 72 65 20 61 6c 73 6f 20 6d 65 74 2e 20 20   are also met.  
168ca 54 68 65 73 65 0a 2a 2a 20 61 72 65 20 74 68 65  These.** are the
168cb 20 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74   conditions that
168cc 20 6d 75 73 74 20 62 65 20 6d 65 74 20 69 6e 20   must be met in 
168cd 6f 72 64 65 72 20 66 6f 72 20 77 72 69 74 69 6e  order for writin
168ce 67 20 74 6f 0a 2a 2a 20 62 65 20 61 6c 6c 6f 77  g to.** be allow
168cf 65 64 3a 0a 2a 2a 0a 2a 2a 20 31 3a 20 20 54 68  ed:.**.** 1:  Th
168d0 65 20 63 75 72 73 6f 72 20 6d 75 73 74 20 68 61  e cursor must ha
168d1 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 77  ve been opened w
168d2 69 74 68 20 77 72 46 6c 61 67 3d 3d 31 0a 2a 2a  ith wrFlag==1.**
168d3 0a 2a 2a 20 32 3a 20 20 4f 74 68 65 72 20 64 61  .** 2:  Other da
168d4 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
168d5 6e 73 20 74 68 61 74 20 73 68 61 72 65 20 74 68  ns that share th
168d6 65 20 73 61 6d 65 20 70 61 67 65 72 20 63 61 63  e same pager cac
168d7 68 65 0a 2a 2a 20 20 20 20 20 62 75 74 20 77 68  he.**     but wh
168d8 69 63 68 20 61 72 65 20 6e 6f 74 20 69 6e 20 74  ich are not in t
168d9 68 65 20 52 45 41 44 5f 55 4e 43 4f 4d 4d 49 54  he READ_UNCOMMIT
168da 54 45 44 20 73 74 61 74 65 20 6d 61 79 20 6e 6f  TED state may no
168db 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 63 75  t have.**     cu
168dc 72 73 6f 72 73 20 6f 70 65 6e 20 77 69 74 68 20  rsors open with 
168dd 77 72 46 6c 61 67 3d 3d 30 20 6f 6e 20 74 68 65  wrFlag==0 on the
168de 20 73 61 6d 65 20 74 61 62 6c 65 2e 20 20 4f 74   same table.  Ot
168df 68 65 72 77 69 73 65 0a 2a 2a 20 20 20 20 20 74  herwise.**     t
168e0 68 65 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20  he changes made 
168e1 62 79 20 74 68 69 73 20 77 72 69 74 65 20 63 75  by this write cu
168e2 72 73 6f 72 20 77 6f 75 6c 64 20 62 65 20 76 69  rsor would be vi
168e3 73 69 62 6c 65 20 74 6f 0a 2a 2a 20 20 20 20 20  sible to.**     
168e4 74 68 65 20 72 65 61 64 20 63 75 72 73 6f 72 73  the read cursors
168e5 20 69 6e 20 74 68 65 20 6f 74 68 65 72 20 64 61   in the other da
168e6 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
168e7 6e 2e 0a 2a 2a 0a 2a 2a 20 33 3a 20 20 54 68 65  n..**.** 3:  The
168e8 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 62   database must b
168e9 65 20 77 72 69 74 61 62 6c 65 20 28 6e 6f 74 20  e writable (not 
168ea 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 65 64  on read-only med
168eb 69 61 29 0a 2a 2a 0a 2a 2a 20 34 3a 20 20 54 68  ia).**.** 4:  Th
168ec 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20 61  ere must be an a
168ed 63 74 69 76 65 20 74 72 61 6e 73 61 63 74 69 6f  ctive transactio
168ee 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 63 68 65 63  n..**.** No chec
168ef 6b 69 6e 67 20 69 73 20 64 6f 6e 65 20 74 6f 20  king is done to 
168f0 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 70  make sure that p
168f1 61 67 65 20 69 54 61 62 6c 65 20 72 65 61 6c 6c  age iTable reall
168f2 79 20 69 73 20 74 68 65 0a 2a 2a 20 72 6f 6f 74  y is the.** root
168f3 20 70 61 67 65 20 6f 66 20 61 20 62 2d 74 72 65   page of a b-tre
168f4 65 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74  e.  If it is not
168f5 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f  , then the curso
168f6 72 20 61 63 71 75 69 72 65 64 0a 2a 2a 20 77 69  r acquired.** wi
168f7 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72  ll not work corr
168f8 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20  ectly..**.** It 
168f9 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20  is assumed that 
168fa 74 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 65  the sqlite3Btree
168fb 43 75 72 73 6f 72 53 69 7a 65 28 29 20 62 79 74  CursorSize() byt
168fc 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 0a 2a 2a  es of memory .**
168fd 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70   pointed to by p
168fe 43 75 72 20 68 61 76 65 20 62 65 65 6e 20 7a 65  Cur have been ze
168ff 72 6f 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c  roed by the call
16900 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  er..*/.static in
16901 74 20 62 74 72 65 65 43 75 72 73 6f 72 28 0a 20  t btreeCursor(. 
16902 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20 20   Btree *p,      
16903 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16904 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62          /* The b
16905 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 54  tree */.  int iT
16906 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20  able,           
16907 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16908 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66   /* Root page of
16909 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a   table to open *
1690a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20  /.  int wrFlag, 
1690b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1690c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20             /* 1 
1690d 74 6f 20 77 72 69 74 65 2e 20 30 20 72 65 61 64  to write. 0 read
1690e 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63  -only */.  struc
1690f 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49  t KeyInfo *pKeyI
16910 6e 66 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20  nfo,            
16911 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74    /* First arg t
16912 6f 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e  o comparison fun
16913 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72  ction */.  BtCur
16914 73 6f 72 20 2a 70 43 75 72 20 20 20 20 20 20 20  sor *pCur       
16915 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16916 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 6e    /* Space for n
16917 65 77 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a  ew cursor */.){.
16918 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
16919 3d 20 70 2d 3e 70 42 74 3b 20 20 20 20 20 20 20  = p->pBt;       
1691a 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 61 72           /* Shar
1691b 65 64 20 62 2d 74 72 65 65 20 68 61 6e 64 6c 65  ed b-tree handle
1691c 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73   */..  assert( s
1691d 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
1691e 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73  Mutex(p) );.  as
1691f 73 65 72 74 28 20 77 72 46 6c 61 67 3d 3d 30 20  sert( wrFlag==0 
16920 7c 7c 20 77 72 46 6c 61 67 3d 3d 31 20 29 3b 0a  || wrFlag==1 );.
16921 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .  /* The follow
16922 69 6e 67 20 61 73 73 65 72 74 20 73 74 61 74 65  ing assert state
16923 6d 65 6e 74 73 20 76 65 72 69 66 79 20 74 68 61  ments verify tha
16924 74 20 69 66 20 74 68 69 73 20 69 73 20 61 20 73  t if this is a s
16925 68 61 72 61 62 6c 65 20 0a 20 20 2a 2a 20 62 2d  harable .  ** b-
16926 74 72 65 65 20 64 61 74 61 62 61 73 65 2c 20 74  tree database, t
16927 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  he connection is
16928 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 72 65 71   holding the req
16929 75 69 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b  uired table lock
1692a 73 2c 20 0a 20 20 2a 2a 20 61 6e 64 20 74 68 61  s, .  ** and tha
1692b 74 20 6e 6f 20 6f 74 68 65 72 20 63 6f 6e 6e 65  t no other conne
1692c 63 74 69 6f 6e 20 68 61 73 20 61 6e 79 20 6f 70  ction has any op
1692d 65 6e 20 63 75 72 73 6f 72 20 74 68 61 74 20 63  en cursor that c
1692e 6f 6e 66 6c 69 63 74 73 20 77 69 74 68 20 0a 20  onflicts with . 
1692f 20 2a 2a 20 74 68 69 73 20 6c 6f 63 6b 2e 20 20   ** this lock.  
16930 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 68 61 73  */.  assert( has
16931 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
16932 4c 6f 63 6b 28 70 2c 20 69 54 61 62 6c 65 2c 20  Lock(p, iTable, 
16933 70 4b 65 79 49 6e 66 6f 21 3d 30 2c 20 77 72 46  pKeyInfo!=0, wrF
16934 6c 61 67 2b 31 29 20 29 3b 0a 20 20 61 73 73 65  lag+1) );.  asse
16935 72 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c 7c  rt( wrFlag==0 ||
16936 20 21 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63   !hasReadConflic
16937 74 73 28 70 2c 20 69 54 61 62 6c 65 29 20 29 3b  ts(p, iTable) );
16938 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68  ..  /* Assert th
16939 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61  at the caller ha
1693a 73 20 6f 70 65 6e 65 64 20 74 68 65 20 72 65 71  s opened the req
1693b 75 69 72 65 64 20 74 72 61 6e 73 61 63 74 69 6f  uired transactio
1693c 6e 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  n. */.  assert( 
1693d 70 2d 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53  p->inTrans>TRANS
1693e 5f 4e 4f 4e 45 20 29 3b 0a 20 20 61 73 73 65 72  _NONE );.  asser
1693f 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20  t( wrFlag==0 || 
16940 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e  p->inTrans==TRAN
16941 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73  S_WRITE );.  ass
16942 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31  ert( pBt->pPage1
16943 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65 31 2d   && pBt->pPage1-
16944 3e 61 44 61 74 61 20 29 3b 0a 0a 20 20 69 66 28  >aData );..  if(
16945 20 4e 45 56 45 52 28 77 72 46 6c 61 67 20 26 26   NEVER(wrFlag &&
16946 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 29 20   pBt->readOnly) 
16947 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
16948 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20  LITE_READONLY;. 
16949 20 7d 0a 20 20 69 66 28 20 69 54 61 62 6c 65 3d   }.  if( iTable=
1694a 3d 31 20 26 26 20 70 61 67 65 72 50 61 67 65 63  =1 && pagerPagec
1694b 6f 75 6e 74 28 70 42 74 29 3d 3d 30 20 29 7b 0a  ount(pBt)==0 ){.
1694c 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1694d 45 5f 45 4d 50 54 59 3b 0a 20 20 7d 0a 0a 20 20  E_EMPTY;.  }..  
1694e 2f 2a 20 4e 6f 77 20 74 68 61 74 20 6e 6f 20 6f  /* Now that no o
1694f 74 68 65 72 20 65 72 72 6f 72 73 20 63 61 6e 20  ther errors can 
16950 6f 63 63 75 72 2c 20 66 69 6e 69 73 68 20 66 69  occur, finish fi
16951 6c 6c 69 6e 67 20 69 6e 20 74 68 65 20 42 74 43  lling in the BtC
16952 75 72 73 6f 72 0a 20 20 2a 2a 20 76 61 72 69 61  ursor.  ** varia
16953 62 6c 65 73 20 61 6e 64 20 6c 69 6e 6b 20 74 68  bles and link th
16954 65 20 63 75 72 73 6f 72 20 69 6e 74 6f 20 74 68  e cursor into th
16955 65 20 42 74 53 68 61 72 65 64 20 6c 69 73 74 2e  e BtShared list.
16956 20 20 2a 2f 0a 20 20 70 43 75 72 2d 3e 70 67 6e    */.  pCur->pgn
16957 6f 52 6f 6f 74 20 3d 20 28 50 67 6e 6f 29 69 54  oRoot = (Pgno)iT
16958 61 62 6c 65 3b 0a 20 20 70 43 75 72 2d 3e 69 50  able;.  pCur->iP
16959 61 67 65 20 3d 20 2d 31 3b 0a 20 20 70 43 75 72  age = -1;.  pCur
1695a 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65  ->pKeyInfo = pKe
1695b 79 49 6e 66 6f 3b 0a 20 20 70 43 75 72 2d 3e 70  yInfo;.  pCur->p
1695c 42 74 72 65 65 20 3d 20 70 3b 0a 20 20 70 43 75  Btree = p;.  pCu
1695d 72 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20  r->pBt = pBt;.  
1695e 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 3d 20 28  pCur->wrFlag = (
1695f 75 38 29 77 72 46 6c 61 67 3b 0a 20 20 70 43 75  u8)wrFlag;.  pCu
16960 72 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e  r->pNext = pBt->
16961 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 70  pCursor;.  if( p
16962 43 75 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20  Cur->pNext ){.  
16963 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 2d 3e 70    pCur->pNext->p
16964 50 72 65 76 20 3d 20 70 43 75 72 3b 0a 20 20 7d  Prev = pCur;.  }
16965 0a 20 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 20  .  pBt->pCursor 
16966 3d 20 70 43 75 72 3b 0a 20 20 70 43 75 72 2d 3e  = pCur;.  pCur->
16967 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f  eState = CURSOR_
16968 49 4e 56 41 4c 49 44 3b 0a 20 20 70 43 75 72 2d  INVALID;.  pCur-
16969 3e 63 61 63 68 65 64 52 6f 77 69 64 20 3d 20 30  >cachedRowid = 0
1696a 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
1696b 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  E_OK;.}.SQLITE_P
1696c 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1696d 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 0a 20  e3BtreeCursor(. 
1696e 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20 20   Btree *p,      
1696f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16970 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
16971 54 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 69  The btree */.  i
16972 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20  nt iTable,      
16973 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16974 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f             /* Ro
16975 6f 74 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65  ot page of table
16976 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e   to open */.  in
16977 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20  t wrFlag,       
16978 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16979 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 74            /* 1 t
1697a 6f 20 77 72 69 74 65 2e 20 30 20 72 65 61 64 2d  o write. 0 read-
1697b 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 74  only */.  struct
1697c 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
1697d 66 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  fo,             
1697e 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61        /* First a
1697f 72 67 20 74 6f 20 78 43 6f 6d 70 61 72 65 28 29  rg to xCompare()
16980 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a   */.  BtCursor *
16981 70 43 75 72 20 20 20 20 20 20 20 20 20 20 20 20  pCur            
16982 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16983 20 20 2f 2a 20 57 72 69 74 65 20 6e 65 77 20 63    /* Write new c
16984 75 72 73 6f 72 20 68 65 72 65 20 2a 2f 0a 29 7b  ursor here */.){
16985 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c  .  int rc;.  sql
16986 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
16987 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 43  );.  rc = btreeC
16988 75 72 73 6f 72 28 70 2c 20 69 54 61 62 6c 65 2c  ursor(p, iTable,
16989 20 77 72 46 6c 61 67 2c 20 70 4b 65 79 49 6e 66   wrFlag, pKeyInf
1698a 6f 2c 20 70 43 75 72 29 3b 0a 20 20 73 71 6c 69  o, pCur);.  sqli
1698b 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
1698c 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
1698d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
1698e 68 65 20 73 69 7a 65 20 6f 66 20 61 20 42 74 43  he size of a BtC
1698f 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 69 6e 20  ursor object in 
16990 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  bytes..**.** Thi
16991 73 20 69 6e 74 65 72 66 61 63 65 73 20 69 73 20  s interfaces is 
16992 6e 65 65 64 65 64 20 73 6f 20 74 68 61 74 20 75  needed so that u
16993 73 65 72 73 20 6f 66 20 63 75 72 73 6f 72 73 20  sers of cursors 
16994 63 61 6e 20 70 72 65 61 6c 6c 6f 63 61 74 65 0a  can preallocate.
16995 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 20 73 74  ** sufficient st
16996 6f 72 61 67 65 20 74 6f 20 68 6f 6c 64 20 61 20  orage to hold a 
16997 63 75 72 73 6f 72 2e 20 20 54 68 65 20 42 74 43  cursor.  The BtC
16998 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 69 73 20  ursor object is 
16999 6f 70 61 71 75 65 0a 2a 2a 20 74 6f 20 75 73 65  opaque.** to use
1699a 72 73 20 73 6f 20 74 68 65 79 20 63 61 6e 6e 6f  rs so they canno
1699b 74 20 64 6f 20 74 68 65 20 73 69 7a 65 6f 66 28  t do the sizeof(
1699c 29 20 74 68 65 6d 73 65 6c 76 65 73 20 2d 20 74  ) themselves - t
1699d 68 65 79 20 6d 75 73 74 20 63 61 6c 6c 0a 2a 2a  hey must call.**
1699e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a   this routine..*
1699f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
169a0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
169a1 65 43 75 72 73 6f 72 53 69 7a 65 28 76 6f 69 64  eCursorSize(void
169a2 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 69 7a 65  ){.  return size
169a3 6f 66 28 42 74 43 75 72 73 6f 72 29 3b 0a 7d 0a  of(BtCursor);.}.
169a4 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63  ./*.** Set the c
169a5 61 63 68 65 64 20 72 6f 77 69 64 20 76 61 6c 75  ached rowid valu
169a6 65 20 6f 66 20 65 76 65 72 79 20 63 75 72 73 6f  e of every curso
169a7 72 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61  r in the same da
169a8 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 61  tabase file.** a
169a9 73 20 70 43 75 72 20 61 6e 64 20 68 61 76 69 6e  s pCur and havin
169aa 67 20 74 68 65 20 73 61 6d 65 20 72 6f 6f 74 20  g the same root 
169ab 70 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20 70  page number as p
169ac 43 75 72 2e 20 20 54 68 65 20 76 61 6c 75 65 20  Cur.  The value 
169ad 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 69 52 6f  is.** set to iRo
169ae 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20  wid..**.** Only 
169af 70 6f 73 69 74 69 76 65 20 72 6f 77 69 64 20 76  positive rowid v
169b0 61 6c 75 65 73 20 61 72 65 20 63 6f 6e 73 69 64  alues are consid
169b1 65 72 65 64 20 76 61 6c 69 64 20 66 6f 72 20 74  ered valid for t
169b2 68 69 73 20 63 61 63 68 65 2e 0a 2a 2a 20 54 68  his cache..** Th
169b3 65 20 63 61 63 68 65 20 69 73 20 69 6e 69 74 69  e cache is initi
169b4 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 2c 20  alized to zero, 
169b5 69 6e 64 69 63 61 74 69 6e 67 20 61 6e 20 69 6e  indicating an in
169b6 76 61 6c 69 64 20 63 61 63 68 65 2e 0a 2a 2a 20  valid cache..** 
169b7 41 20 62 74 72 65 65 20 77 69 6c 6c 20 77 6f 72  A btree will wor
169b8 6b 20 66 69 6e 65 20 77 69 74 68 20 7a 65 72 6f  k fine with zero
169b9 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f 77   or negative row
169ba 69 64 73 2e 20 20 57 65 20 6a 75 73 74 20 63 61  ids.  We just ca
169bb 6e 6e 6f 74 0a 2a 2a 20 63 61 63 68 65 20 7a 65  nnot.** cache ze
169bc 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72  ro or negative r
169bd 6f 77 69 64 73 2c 20 77 68 69 63 68 20 6d 65 61  owids, which mea
169be 6e 73 20 74 61 62 6c 65 73 20 74 68 61 74 20 75  ns tables that u
169bf 73 65 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e 65  se zero or.** ne
169c0 67 61 74 69 76 65 20 72 6f 77 69 64 73 20 6d 69  gative rowids mi
169c1 67 68 74 20 72 75 6e 20 61 20 6c 69 74 74 6c 65  ght run a little
169c2 20 73 6c 6f 77 65 72 2e 20 20 42 75 74 20 69 6e   slower.  But in
169c3 20 70 72 61 63 74 69 63 65 2c 20 7a 65 72 6f 0a   practice, zero.
169c4 2a 2a 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72  ** or negative r
169c5 6f 77 69 64 73 20 61 72 65 20 76 65 72 79 20 75  owids are very u
169c6 6e 63 6f 6d 6d 6f 6e 20 73 6f 20 74 68 69 73 20  ncommon so this 
169c7 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 20  should not be a 
169c8 70 72 6f 62 6c 65 6d 2e 0a 2a 2f 0a 53 51 4c 49  problem..*/.SQLI
169c9 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
169ca 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43  sqlite3BtreeSetC
169cb 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72  achedRowid(BtCur
169cc 73 6f 72 20 2a 70 43 75 72 2c 20 73 71 6c 69 74  sor *pCur, sqlit
169cd 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 29  e3_int64 iRowid)
169ce 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b  {.  BtCursor *p;
169cf 0a 20 20 66 6f 72 28 70 3d 70 43 75 72 2d 3e 70  .  for(p=pCur->p
169d0 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20  Bt->pCursor; p; 
169d1 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  p=p->pNext){.   
169d2 20 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74   if( p->pgnoRoot
169d3 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74  ==pCur->pgnoRoot
169d4 20 29 20 70 2d 3e 63 61 63 68 65 64 52 6f 77 69   ) p->cachedRowi
169d5 64 20 3d 20 69 52 6f 77 69 64 3b 0a 20 20 7d 0a  d = iRowid;.  }.
169d6 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
169d7 63 61 63 68 65 64 52 6f 77 69 64 3d 3d 69 52 6f  cachedRowid==iRo
169d8 77 69 64 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  wid );.}../*.** 
169d9 52 65 74 75 72 6e 20 74 68 65 20 63 61 63 68 65  Return the cache
169da 64 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20  d rowid for the 
169db 67 69 76 65 6e 20 63 75 72 73 6f 72 2e 20 20 41  given cursor.  A
169dc 20 6e 65 67 61 74 69 76 65 20 6f 72 20 7a 65 72   negative or zer
169dd 6f 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75  o.** return valu
169de 65 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74  e indicates that
169df 20 74 68 65 20 72 6f 77 69 64 20 63 61 63 68 65   the rowid cache
169e0 20 69 73 20 69 6e 76 61 6c 69 64 20 61 6e 64 20   is invalid and 
169e1 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 69 67 6e  should be.** ign
169e2 6f 72 65 64 2e 20 20 49 66 20 74 68 65 20 72 6f  ored.  If the ro
169e3 77 69 64 20 63 61 63 68 65 20 68 61 73 20 6e 65  wid cache has ne
169e4 76 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e 20  ver before been 
169e5 73 65 74 2c 20 74 68 65 6e 20 61 0a 2a 2a 20 7a  set, then a.** z
169e6 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ero is returned.
169e7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
169e8 54 45 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  TE sqlite3_int64
169e9 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
169ea 43 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75  CachedRowid(BtCu
169eb 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 72  rsor *pCur){.  r
169ec 65 74 75 72 6e 20 70 43 75 72 2d 3e 63 61 63 68  eturn pCur->cach
169ed 65 64 52 6f 77 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a  edRowid;.}../*.*
169ee 2a 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72  * Close a cursor
169ef 2e 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b  .  The read lock
169f0 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
169f1 20 66 69 6c 65 20 69 73 20 72 65 6c 65 61 73 65   file is release
169f2 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 6c 61  d.** when the la
169f3 73 74 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f  st cursor is clo
169f4 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  sed..*/.SQLITE_P
169f5 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
169f6 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73  e3BtreeCloseCurs
169f7 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  or(BtCursor *pCu
169f8 72 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74  r){.  Btree *pBt
169f9 72 65 65 20 3d 20 70 43 75 72 2d 3e 70 42 74 72  ree = pCur->pBtr
169fa 65 65 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65  ee;.  if( pBtree
169fb 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
169fc 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74     BtShared *pBt
169fd 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 20 20   = pCur->pBt;.  
169fe 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
169ff 74 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20 20  ter(pBtree);.   
16a00 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65   sqlite3BtreeCle
16a01 61 72 43 75 72 73 6f 72 28 70 43 75 72 29 3b 0a  arCursor(pCur);.
16a02 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 70 50      if( pCur->pP
16a03 72 65 76 20 29 7b 0a 20 20 20 20 20 20 70 43 75  rev ){.      pCu
16a04 72 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20  r->pPrev->pNext 
16a05 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20  = pCur->pNext;. 
16a06 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
16a07 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 70  pBt->pCursor = p
16a08 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  Cur->pNext;.    
16a09 7d 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e  }.    if( pCur->
16a0a 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70  pNext ){.      p
16a0b 43 75 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65  Cur->pNext->pPre
16a0c 76 20 3d 20 70 43 75 72 2d 3e 70 50 72 65 76 3b  v = pCur->pPrev;
16a0d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69  .    }.    for(i
16a0e 3d 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61  =0; i<=pCur->iPa
16a0f 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ge; i++){.      
16a10 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72  releasePage(pCur
16a11 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20  ->apPage[i]);.  
16a12 20 20 7d 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74    }.    unlockBt
16a13 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29  reeIfUnused(pBt)
16a14 3b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65  ;.    invalidate
16a15 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 43  OverflowCache(pC
16a16 75 72 29 3b 0a 20 20 20 20 2f 2a 20 73 71 6c 69  ur);.    /* sqli
16a17 74 65 33 5f 66 72 65 65 28 70 43 75 72 29 3b 20  te3_free(pCur); 
16a18 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  */.    sqlite3Bt
16a19 72 65 65 4c 65 61 76 65 28 70 42 74 72 65 65 29  reeLeave(pBtree)
16a1a 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
16a1b 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
16a1c 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  ** Make sure the
16a1d 20 42 74 43 75 72 73 6f 72 2a 20 67 69 76 65 6e   BtCursor* given
16a1e 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74   in the argument
16a1f 20 68 61 73 20 61 20 76 61 6c 69 64 0a 2a 2a 20   has a valid.** 
16a20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 73 74  BtCursor.info st
16a21 72 75 63 74 75 72 65 2e 20 20 49 66 20 69 74 20  ructure.  If it 
16a22 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 76  is not already v
16a23 61 6c 69 64 2c 20 63 61 6c 6c 0a 2a 2a 20 62 74  alid, call.** bt
16a24 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 20 74  reeParseCell() t
16a25 6f 20 66 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a 2a  o fill it in..**
16a26 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66  .** BtCursor.inf
16a27 6f 20 69 73 20 61 20 63 61 63 68 65 20 6f 66 20  o is a cache of 
16a28 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  the information 
16a29 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 63  in the current c
16a2a 65 6c 6c 2e 0a 2a 2a 20 55 73 69 6e 67 20 74 68  ell..** Using th
16a2b 69 73 20 63 61 63 68 65 20 72 65 64 75 63 65 73  is cache reduces
16a2c 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
16a2d 61 6c 6c 73 20 74 6f 20 62 74 72 65 65 50 61 72  alls to btreePar
16a2e 73 65 43 65 6c 6c 28 29 2e 0a 2a 2a 0a 2a 2a 20  seCell()..**.** 
16a2f 32 30 30 37 2d 30 36 2d 32 35 3a 20 20 54 68 65  2007-06-25:  The
16a30 72 65 20 69 73 20 61 20 62 75 67 20 69 6e 20 73  re is a bug in s
16a31 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  ome versions of 
16a32 4d 53 56 43 20 74 68 61 74 20 63 61 75 73 65 20  MSVC that cause 
16a33 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20  the.** compiler 
16a34 74 6f 20 63 72 61 73 68 20 77 68 65 6e 20 67 65  to crash when ge
16a35 74 43 65 6c 6c 49 6e 66 6f 28 29 20 69 73 20 69  tCellInfo() is i
16a36 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20  mplemented as a 
16a37 6d 61 63 72 6f 2e 0a 2a 2a 20 42 75 74 20 74 68  macro..** But th
16a38 65 72 65 20 69 73 20 61 20 6d 65 61 73 75 72 65  ere is a measure
16a39 61 62 6c 65 20 73 70 65 65 64 20 61 64 76 61 6e  able speed advan
16a3a 74 61 67 65 20 74 6f 20 75 73 69 6e 67 20 74 68  tage to using th
16a3b 65 20 6d 61 63 72 6f 20 6f 6e 20 67 63 63 0a 2a  e macro on gcc.*
16a3c 2a 20 28 77 68 65 6e 20 6c 65 73 73 20 63 6f 6d  * (when less com
16a3d 70 69 6c 65 72 20 6f 70 74 69 6d 69 7a 61 74 69  piler optimizati
16a3e 6f 6e 73 20 6c 69 6b 65 20 2d 4f 73 20 6f 72 20  ons like -Os or 
16a3f 2d 4f 30 20 61 72 65 20 75 73 65 64 20 61 6e 64  -O0 are used and
16a40 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72   the.** compiler
16a41 20 69 73 20 6e 6f 74 20 64 6f 69 6e 67 20 61 67   is not doing ag
16a42 72 65 73 73 69 76 65 20 69 6e 6c 69 6e 69 6e 67  ressive inlining
16a43 2e 29 20 20 53 6f 20 77 65 20 75 73 65 20 61 20  .)  So we use a 
16a44 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  real function.**
16a45 20 66 6f 72 20 4d 53 56 43 20 61 6e 64 20 61 20   for MSVC and a 
16a46 6d 61 63 72 6f 20 66 6f 72 20 65 76 65 72 79 74  macro for everyt
16a47 68 69 6e 67 20 65 6c 73 65 2e 20 20 54 69 63 6b  hing else.  Tick
16a48 65 74 20 23 32 34 35 37 2e 0a 2a 2f 0a 23 69 66  et #2457..*/.#if
16a49 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 73 74  ndef NDEBUG.  st
16a4a 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74  atic void assert
16a4b 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f  CellInfo(BtCurso
16a4c 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 20 43 65  r *pCur){.    Ce
16a4d 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20  llInfo info;.   
16a4e 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75   int iPage = pCu
16a4f 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 6d 65  r->iPage;.    me
16a50 6d 73 65 74 28 26 69 6e 66 6f 2c 20 30 2c 20 73  mset(&info, 0, s
16a51 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3b 0a 20 20  izeof(info));.  
16a52 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c    btreeParseCell
16a53 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50  (pCur->apPage[iP
16a54 61 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 49 64  age], pCur->aiId
16a55 78 5b 69 50 61 67 65 5d 2c 20 26 69 6e 66 6f 29  x[iPage], &info)
16a56 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65  ;.    assert( me
16a57 6d 63 6d 70 28 26 69 6e 66 6f 2c 20 26 70 43 75  mcmp(&info, &pCu
16a58 72 2d 3e 69 6e 66 6f 2c 20 73 69 7a 65 6f 66 28  r->info, sizeof(
16a59 69 6e 66 6f 29 29 3d 3d 30 20 29 3b 0a 20 20 7d  info))==0 );.  }
16a5a 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65  .#else.  #define
16a5b 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28   assertCellInfo(
16a5c 78 29 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  x).#endif.#ifdef
16a5d 20 5f 4d 53 43 5f 56 45 52 0a 20 20 2f 2a 20 55   _MSC_VER.  /* U
16a5e 73 65 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69  se a real functi
16a5f 6f 6e 20 69 6e 20 4d 53 56 43 20 74 6f 20 77 6f  on in MSVC to wo
16a60 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 73 20 69  rk around bugs i
16a61 6e 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 2e  n that compiler.
16a62 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69   */.  static voi
16a63 64 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 42 74  d getCellInfo(Bt
16a64 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
16a65 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66     if( pCur->inf
16a66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b 0a 20 20  o.nSize==0 ){.  
16a67 20 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20      int iPage = 
16a68 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20  pCur->iPage;.   
16a69 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c     btreeParseCel
16a6a 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69  l(pCur->apPage[i
16a6b 50 61 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64  Page],pCur->aiId
16a6c 78 5b 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e  x[iPage],&pCur->
16a6d 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 70 43 75  info);.      pCu
16a6e 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31  r->validNKey = 1
16a6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
16a70 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66     assertCellInf
16a71 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20  o(pCur);.    }. 
16a72 20 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 6e   }.#else /* if n
16a73 6f 74 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 20  ot _MSC_VER */. 
16a74 20 2f 2a 20 55 73 65 20 61 20 6d 61 63 72 6f 20   /* Use a macro 
16a75 69 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 6d  in all other com
16a76 70 69 6c 65 72 73 20 73 6f 20 74 68 61 74 20 74  pilers so that t
16a77 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69  he function is i
16a78 6e 6c 69 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e  nlined */.#defin
16a79 65 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43  e getCellInfo(pC
16a7a 75 72 29 20 20 20 20 20 20 20 20 20 20 20 20 20  ur)             
16a7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a7d 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28           \.  if(
16a7e 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a   pCur->info.nSiz
16a7f 65 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20  e==0 ){         
16a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a82 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
16a83 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72  int iPage = pCur
16a84 2d 3e 69 50 61 67 65 3b 20 20 20 20 20 20 20 20  ->iPage;        
16a85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a87 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
16a88 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28   btreeParseCell(
16a89 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61  pCur->apPage[iPa
16a8a 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78 5b  ge],pCur->aiIdx[
16a8b 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69 6e  iPage],&pCur->in
16a8c 66 6f 29 3b 20 5c 0a 20 20 20 20 70 43 75 72 2d  fo); \.    pCur-
16a8d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 20  >validNKey = 1; 
16a8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a91 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b        \.  }else{
16a92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a96 20 20 20 20 20 20 20 5c 0a 20 20 20 20 61 73 73         \.    ass
16a97 65 72 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72  ertCellInfo(pCur
16a98 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
16a99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a9b 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65          \.  }.#e
16a9c 6e 64 69 66 20 2f 2a 20 5f 4d 53 43 5f 56 45 52  ndif /* _MSC_VER
16a9d 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45   */..#ifndef NDE
16a9e 42 55 47 20 20 2f 2a 20 54 68 65 20 6e 65 78 74  BUG  /* The next
16a9f 20 72 6f 75 74 69 6e 65 20 75 73 65 64 20 6f 6e   routine used on
16aa0 6c 79 20 77 69 74 68 69 6e 20 61 73 73 65 72 74  ly within assert
16aa1 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f  () statements */
16aa2 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  ./*.** Return tr
16aa3 75 65 20 69 66 20 74 68 65 20 67 69 76 65 6e 20  ue if the given 
16aa4 42 74 43 75 72 73 6f 72 20 69 73 20 76 61 6c 69  BtCursor is vali
16aa5 64 2e 20 20 41 20 76 61 6c 69 64 20 63 75 72 73  d.  A valid curs
16aa6 6f 72 20 69 73 20 6f 6e 65 0a 2a 2a 20 74 68 61  or is one.** tha
16aa7 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70  t is currently p
16aa8 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 72 6f 77  ointing to a row
16aa9 20 69 6e 20 61 20 28 6e 6f 6e 2d 65 6d 70 74 79   in a (non-empty
16aaa 29 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 69 73  ) table..** This
16aab 20 69 73 20 61 20 76 65 72 69 66 69 63 61 74 69   is a verificati
16aac 6f 6e 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  on routine is us
16aad 65 64 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20 61  ed only within a
16aae 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e  ssert() statemen
16aaf 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ts..*/.SQLITE_PR
16ab0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
16ab1 33 42 74 72 65 65 43 75 72 73 6f 72 49 73 56 61  3BtreeCursorIsVa
16ab2 6c 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43  lid(BtCursor *pC
16ab3 75 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 43  ur){.  return pC
16ab4 75 72 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61  ur && pCur->eSta
16ab5 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
16ab6 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44  ;.}.#endif /* ND
16ab7 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53  EBUG */../*.** S
16ab8 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 74 68 65  et *pSize to the
16ab9 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75 66   size of the buf
16aba 66 65 72 20 6e 65 65 64 65 64 20 74 6f 20 68 6f  fer needed to ho
16abb 6c 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 0a  ld the value of.
16abc 2a 2a 20 74 68 65 20 6b 65 79 20 66 6f 72 20 74  ** the key for t
16abd 68 65 20 63 75 72 72 65 6e 74 20 65 6e 74 72 79  he current entry
16abe 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72  .  If the cursor
16abf 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67   is not pointing
16ac0 0a 2a 2a 20 74 6f 20 61 20 76 61 6c 69 64 20 65  .** to a valid e
16ac1 6e 74 72 79 2c 20 2a 70 53 69 7a 65 20 69 73 20  ntry, *pSize is 
16ac2 73 65 74 20 74 6f 20 30 2e 20 0a 2a 2a 0a 2a 2a  set to 0. .**.**
16ac3 20 46 6f 72 20 61 20 74 61 62 6c 65 20 77 69 74   For a table wit
16ac4 68 20 74 68 65 20 49 4e 54 4b 45 59 20 66 6c 61  h the INTKEY fla
16ac5 67 20 73 65 74 2c 20 74 68 69 73 20 72 6f 75 74  g set, this rout
16ac6 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ine returns the 
16ac7 6b 65 79 0a 2a 2a 20 69 74 73 65 6c 66 2c 20 6e  key.** itself, n
16ac8 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ot the number of
16ac9 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6b 65   bytes in the ke
16aca 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c  y..**.** The cal
16acb 6c 65 72 20 6d 75 73 74 20 70 6f 73 69 74 69 6f  ler must positio
16acc 6e 20 74 68 65 20 63 75 72 73 6f 72 20 70 72 69  n the cursor pri
16acd 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74  or to invoking t
16ace 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 20  his routine..** 
16acf 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
16ad0 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 20 49   cannot fail.  I
16ad1 74 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73  t always returns
16ad2 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 20 0a 2a 2f   SQLITE_OK.  .*/
16ad3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
16ad4 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
16ad5 4b 65 79 53 69 7a 65 28 42 74 43 75 72 73 6f 72  KeySize(BtCursor
16ad6 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 53 69   *pCur, i64 *pSi
16ad7 7a 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63  ze){.  assert( c
16ad8 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
16ad9 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72  pCur) );.  asser
16ada 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
16adb 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20  =CURSOR_INVALID 
16adc 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  || pCur->eState=
16add 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b  =CURSOR_VALID );
16ade 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74  .  if( pCur->eSt
16adf 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate!=CURSOR_VALI
16ae0 44 20 29 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20  D ){.    *pSize 
16ae1 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 0;.  }else{.  
16ae2 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43    getCellInfo(pC
16ae3 75 72 29 3b 0a 20 20 20 20 2a 70 53 69 7a 65 20  ur);.    *pSize 
16ae4 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65  = pCur->info.nKe
16ae5 79 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  y;.  }.  return 
16ae6 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
16ae7 0a 2a 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74  .** Set *pSize t
16ae8 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  o the number of 
16ae9 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 69 6e  bytes of data in
16aea 20 74 68 65 20 65 6e 74 72 79 20 74 68 65 0a 2a   the entry the.*
16aeb 2a 20 63 75 72 73 6f 72 20 63 75 72 72 65 6e 74  * cursor current
16aec 6c 79 20 70 6f 69 6e 74 73 20 74 6f 2e 0a 2a 2a  ly points to..**
16aed 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d  .** The caller m
16aee 75 73 74 20 67 75 61 72 61 6e 74 65 65 20 74 68  ust guarantee th
16aef 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  at the cursor is
16af0 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 6e   pointing to a n
16af1 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 76 61 6c 69 64  on-NULL.** valid
16af2 20 65 6e 74 72 79 2e 20 20 49 6e 20 6f 74 68 65   entry.  In othe
16af3 72 20 77 6f 72 64 73 2c 20 74 68 65 20 63 61 6c  r words, the cal
16af4 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20 6d  ling procedure m
16af5 75 73 74 20 67 75 61 72 61 6e 74 65 65 0a 2a 2a  ust guarantee.**
16af6 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72   that the cursor
16af7 20 68 61 73 20 43 75 72 73 6f 72 2e 65 53 74 61   has Cursor.eSta
16af8 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
16af9 2e 0a 2a 2a 0a 2a 2a 20 46 61 69 6c 75 72 65 20  ..**.** Failure 
16afa 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e  is not possible.
16afb 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20    This function 
16afc 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 53  always returns S
16afd 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 49 74 20  QLITE_OK..** It 
16afe 6d 69 67 68 74 20 6a 75 73 74 20 61 73 20 77 65  might just as we
16aff 6c 6c 20 62 65 20 61 20 70 72 6f 63 65 64 75 72  ll be a procedur
16b00 65 20 28 72 65 74 75 72 6e 69 6e 67 20 76 6f 69  e (returning voi
16b01 64 29 20 62 75 74 20 77 65 20 63 6f 6e 74 69 6e  d) but we contin
16b02 75 65 0a 2a 2a 20 74 6f 20 72 65 74 75 72 6e 20  ue.** to return 
16b03 61 6e 20 69 6e 74 65 67 65 72 20 72 65 73 75 6c  an integer resul
16b04 74 20 63 6f 64 65 20 66 6f 72 20 68 69 73 74 6f  t code for histo
16b05 72 69 63 61 6c 20 72 65 61 73 6f 6e 73 2e 0a 2a  rical reasons..*
16b06 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16b07 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
16b08 65 44 61 74 61 53 69 7a 65 28 42 74 43 75 72 73  eDataSize(BtCurs
16b09 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 2a 70  or *pCur, u32 *p
16b0a 53 69 7a 65 29 7b 0a 20 20 61 73 73 65 72 74 28  Size){.  assert(
16b0b 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
16b0c 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73  x(pCur) );.  ass
16b0d 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74  ert( pCur->eStat
16b0e 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e==CURSOR_VALID 
16b0f 29 3b 0a 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f  );.  getCellInfo
16b10 28 70 43 75 72 29 3b 0a 20 20 2a 70 53 69 7a 65  (pCur);.  *pSize
16b11 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44   = pCur->info.nD
16b12 61 74 61 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ata;.  return SQ
16b13 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
16b14 2a 20 47 69 76 65 6e 20 74 68 65 20 70 61 67 65  * Given the page
16b15 20 6e 75 6d 62 65 72 20 6f 66 20 61 6e 20 6f 76   number of an ov
16b16 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74  erflow page in t
16b17 68 65 20 64 61 74 61 62 61 73 65 20 28 70 61 72  he database (par
16b18 61 6d 65 74 65 72 0a 2a 2a 20 6f 76 66 6c 29 2c  ameter.** ovfl),
16b19 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66   this function f
16b1a 69 6e 64 73 20 74 68 65 20 70 61 67 65 20 6e 75  inds the page nu
16b1b 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74  mber of the next
16b1c 20 70 61 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a   page in the .**
16b1d 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20   linked list of 
16b1e 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20  overflow pages. 
16b1f 49 66 20 70 6f 73 73 69 62 6c 65 2c 20 69 74 20  If possible, it 
16b20 75 73 65 73 20 74 68 65 20 61 75 74 6f 2d 76 61  uses the auto-va
16b21 63 75 75 6d 0a 2a 2a 20 70 6f 69 6e 74 65 72 2d  cuum.** pointer-
16b22 6d 61 70 20 64 61 74 61 20 69 6e 73 74 65 61 64  map data instead
16b23 20 6f 66 20 72 65 61 64 69 6e 67 20 74 68 65 20   of reading the 
16b24 63 6f 6e 74 65 6e 74 20 6f 66 20 70 61 67 65 20  content of page 
16b25 6f 76 66 6c 20 74 6f 20 64 6f 20 73 6f 2e 20 0a  ovfl to do so. .
16b26 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f  **.** If an erro
16b27 72 20 6f 63 63 75 72 73 20 61 6e 20 53 51 4c 69  r occurs an SQLi
16b28 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  te error code is
16b29 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72   returned. Other
16b2a 77 69 73 65 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20  wise:.**.** The 
16b2b 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74  page number of t
16b2c 68 65 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77  he next overflow
16b2d 20 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69 6e   page in the lin
16b2e 6b 65 64 20 6c 69 73 74 20 69 73 20 0a 2a 2a 20  ked list is .** 
16b2f 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 50 67 6e  written to *pPgn
16b30 6f 4e 65 78 74 2e 20 49 66 20 70 61 67 65 20 6f  oNext. If page o
16b31 76 66 6c 20 69 73 20 74 68 65 20 6c 61 73 74 20  vfl is the last 
16b32 70 61 67 65 20 69 6e 20 69 74 73 20 6c 69 6e 6b  page in its link
16b33 65 64 20 0a 2a 2a 20 6c 69 73 74 2c 20 2a 70 50  ed .** list, *pP
16b34 67 6e 6f 4e 65 78 74 20 69 73 20 73 65 74 20 74  gnoNext is set t
16b35 6f 20 7a 65 72 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49  o zero. .**.** I
16b36 66 20 70 70 50 61 67 65 20 69 73 20 6e 6f 74 20  f ppPage is not 
16b37 4e 55 4c 4c 2c 20 61 6e 64 20 61 20 72 65 66 65  NULL, and a refe
16b38 72 65 6e 63 65 20 74 6f 20 74 68 65 20 4d 65 6d  rence to the Mem
16b39 50 61 67 65 20 6f 62 6a 65 63 74 20 63 6f 72 72  Page object corr
16b3a 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20  esponding.** to 
16b3b 70 61 67 65 20 6e 75 6d 62 65 72 20 70 4f 76 66  page number pOvf
16b3c 6c 20 77 61 73 20 6f 62 74 61 69 6e 65 64 2c 20  l was obtained, 
16b3d 74 68 65 6e 20 2a 70 70 50 61 67 65 20 69 73 20  then *ppPage is 
16b3e 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  set to point to 
16b3f 74 68 61 74 0a 2a 2a 20 72 65 66 65 72 65 6e 63  that.** referenc
16b40 65 2e 20 49 74 20 69 73 20 74 68 65 20 72 65 73  e. It is the res
16b41 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74  ponsibility of t
16b42 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 63 61 6c  he caller to cal
16b43 6c 20 72 65 6c 65 61 73 65 50 61 67 65 28 29 0a  l releasePage().
16b44 2a 2a 20 6f 6e 20 2a 70 70 50 61 67 65 20 74 6f  ** on *ppPage to
16b45 20 66 72 65 65 20 74 68 65 20 72 65 66 65 72 65   free the refere
16b46 6e 63 65 2e 20 49 6e 20 6e 6f 20 72 65 66 65 72  nce. In no refer
16b47 65 6e 63 65 20 77 61 73 20 6f 62 74 61 69 6e 65  ence was obtaine
16b48 64 20 28 62 65 63 61 75 73 65 0a 2a 2a 20 74 68  d (because.** th
16b49 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 77 61  e pointer-map wa
16b4a 73 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e  s used to obtain
16b4b 20 74 68 65 20 76 61 6c 75 65 20 66 6f 72 20 2a   the value for *
16b4c 70 50 67 6e 6f 4e 65 78 74 29 2c 20 74 68 65 6e  pPgnoNext), then
16b4d 0a 2a 2a 20 2a 70 70 50 61 67 65 20 69 73 20 73  .** *ppPage is s
16b4e 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2f 0a 73  et to zero..*/.s
16b4f 74 61 74 69 63 20 69 6e 74 20 67 65 74 4f 76 65  tatic int getOve
16b50 72 66 6c 6f 77 50 61 67 65 28 0a 20 20 42 74 53  rflowPage(.  BtS
16b51 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20  hared *pBt,     
16b52 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
16b53 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a   database file *
16b54 2f 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 2c 20 20  /.  Pgno ovfl,  
16b55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b56 20 2f 2a 20 43 75 72 72 65 6e 74 20 6f 76 65 72   /* Current over
16b57 66 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72  flow page number
16b58 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a   */.  MemPage **
16b59 70 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20  ppPage,         
16b5a 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 61     /* OUT: MemPa
16b5b 67 65 20 68 61 6e 64 6c 65 20 28 6d 61 79 20 62  ge handle (may b
16b5c 65 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 50 67 6e  e NULL) */.  Pgn
16b5d 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 20 20 20 20  o *pPgnoNext    
16b5e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
16b5f 3a 20 4e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20  : Next overflow 
16b60 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 29  page number */.)
16b61 7b 0a 20 20 50 67 6e 6f 20 6e 65 78 74 20 3d 20  {.  Pgno next = 
16b62 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  0;.  MemPage *pP
16b63 61 67 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  age = 0;.  int r
16b64 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  c = SQLITE_OK;..
16b65 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
16b66 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
16b67 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
16b68 73 65 72 74 28 70 50 67 6e 6f 4e 65 78 74 29 3b  sert(pPgnoNext);
16b69 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
16b6a 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
16b6b 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e  .  /* Try to fin
16b6c 64 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20  d the next page 
16b6d 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  in the overflow 
16b6e 6c 69 73 74 20 75 73 69 6e 67 20 74 68 65 0a 20  list using the. 
16b6f 20 2a 2a 20 61 75 74 6f 76 61 63 75 75 6d 20 70   ** autovacuum p
16b70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73  ointer-map pages
16b71 2e 20 47 75 65 73 73 20 74 68 61 74 20 74 68 65  . Guess that the
16b72 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 0a 20   next page in . 
16b73 20 2a 2a 20 74 68 65 20 6f 76 65 72 66 6c 6f 77   ** the overflow
16b74 20 6c 69 73 74 20 69 73 20 70 61 67 65 20 6e 75   list is page nu
16b75 6d 62 65 72 20 28 6f 76 66 6c 2b 31 29 2e 20 49  mber (ovfl+1). I
16b76 66 20 74 68 61 74 20 67 75 65 73 73 20 74 75 72  f that guess tur
16b77 6e 73 20 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20  ns .  ** out to 
16b78 62 65 20 77 72 6f 6e 67 2c 20 66 61 6c 6c 20 62  be wrong, fall b
16b79 61 63 6b 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74  ack to loading t
16b7a 68 65 20 64 61 74 61 20 6f 66 20 70 61 67 65 20  he data of page 
16b7b 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 76 66  .  ** number ovf
16b7c 6c 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74  l to determine t
16b7d 68 65 20 6e 65 78 74 20 70 61 67 65 20 6e 75 6d  he next page num
16b7e 62 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ber..  */.  if( 
16b7f 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
16b80 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f  ){.    Pgno pgno
16b81 3b 0a 20 20 20 20 50 67 6e 6f 20 69 47 75 65 73  ;.    Pgno iGues
16b82 73 20 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20 20 20  s = ovfl+1;.    
16b83 75 38 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 77  u8 eType;..    w
16b84 68 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50  hile( PTRMAP_ISP
16b85 41 47 45 28 70 42 74 2c 20 69 47 75 65 73 73 29  AGE(pBt, iGuess)
16b86 20 7c 7c 20 69 47 75 65 73 73 3d 3d 50 45 4e 44   || iGuess==PEND
16b87 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
16b88 74 29 20 29 7b 0a 20 20 20 20 20 20 69 47 75 65  t) ){.      iGue
16b89 73 73 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ss++;.    }..   
16b8a 20 69 66 28 20 69 47 75 65 73 73 3c 3d 70 61 67   if( iGuess<=pag
16b8b 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29  erPagecount(pBt)
16b8c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70   ){.      rc = p
16b8d 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 69 47  trmapGet(pBt, iG
16b8e 75 65 73 73 2c 20 26 65 54 79 70 65 2c 20 26 70  uess, &eType, &p
16b8f 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20  gno);.      if( 
16b90 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
16b91 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f   eType==PTRMAP_O
16b92 56 45 52 46 4c 4f 57 32 20 26 26 20 70 67 6e 6f  VERFLOW2 && pgno
16b93 3d 3d 6f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20  ==ovfl ){.      
16b94 20 20 6e 65 78 74 20 3d 20 69 47 75 65 73 73 3b    next = iGuess;
16b95 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
16b96 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20  LITE_DONE;.     
16b97 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e   }.    }.  }.#en
16b98 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 6e  dif..  assert( n
16b99 65 78 74 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51  ext==0 || rc==SQ
16b9a 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 69  LITE_DONE );.  i
16b9b 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
16b9c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72   ){.    rc = btr
16b9d 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 6f  eeGetPage(pBt, o
16b9e 76 66 6c 2c 20 26 70 50 61 67 65 2c 20 30 29 3b  vfl, &pPage, 0);
16b9f 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d  .    assert( rc=
16ba0 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50  =SQLITE_OK || pP
16ba1 61 67 65 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66  age==0 );.    if
16ba2 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
16ba3 29 7b 0a 20 20 20 20 20 20 6e 65 78 74 20 3d 20  ){.      next = 
16ba4 67 65 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e  get4byte(pPage->
16ba5 61 44 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20  aData);.    }.  
16ba6 7d 0a 0a 20 20 2a 70 50 67 6e 6f 4e 65 78 74 20  }..  *pPgnoNext 
16ba7 3d 20 6e 65 78 74 3b 0a 20 20 69 66 28 20 70 70  = next;.  if( pp
16ba8 50 61 67 65 20 29 7b 0a 20 20 20 20 2a 70 70 50  Page ){.    *ppP
16ba9 61 67 65 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d  age = pPage;.  }
16baa 65 6c 73 65 7b 0a 20 20 20 20 72 65 6c 65 61 73  else{.    releas
16bab 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
16bac 7d 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d  }.  return (rc==
16bad 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51  SQLITE_DONE ? SQ
16bae 4c 49 54 45 5f 4f 4b 20 3a 20 72 63 29 3b 0a 7d  LITE_OK : rc);.}
16baf 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 64 61 74  ../*.** Copy dat
16bb0 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20  a from a buffer 
16bb1 74 6f 20 61 20 70 61 67 65 2c 20 6f 72 20 66 72  to a page, or fr
16bb2 6f 6d 20 61 20 70 61 67 65 20 74 6f 20 61 20 62  om a page to a b
16bb3 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 70 50 61  uffer..**.** pPa
16bb4 79 6c 6f 61 64 20 69 73 20 61 20 70 6f 69 6e 74  yload is a point
16bb5 65 72 20 74 6f 20 64 61 74 61 20 73 74 6f 72 65  er to data store
16bb6 64 20 6f 6e 20 64 61 74 61 62 61 73 65 20 70 61  d on database pa
16bb7 67 65 20 70 44 62 50 61 67 65 2e 0a 2a 2a 20 49  ge pDbPage..** I
16bb8 66 20 61 72 67 75 6d 65 6e 74 20 65 4f 70 20 69  f argument eOp i
16bb9 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 6e 42  s false, then nB
16bba 79 74 65 20 62 79 74 65 73 20 6f 66 20 64 61 74  yte bytes of dat
16bbb 61 20 61 72 65 20 63 6f 70 69 65 64 0a 2a 2a 20  a are copied.** 
16bbc 66 72 6f 6d 20 70 50 61 79 6c 6f 61 64 20 74 6f  from pPayload to
16bbd 20 74 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e   the buffer poin
16bbe 74 65 64 20 61 74 20 62 79 20 70 42 75 66 2e 20  ted at by pBuf. 
16bbf 49 66 20 65 4f 70 20 69 73 20 74 72 75 65 2c 0a  If eOp is true,.
16bc0 2a 2a 20 74 68 65 6e 20 73 71 6c 69 74 65 33 50  ** then sqlite3P
16bc1 61 67 65 72 57 72 69 74 65 28 29 20 69 73 20 63  agerWrite() is c
16bc2 61 6c 6c 65 64 20 6f 6e 20 70 44 62 50 61 67 65  alled on pDbPage
16bc3 20 61 6e 64 20 6e 42 79 74 65 20 62 79 74 65 73   and nByte bytes
16bc4 0a 2a 2a 20 6f 66 20 64 61 74 61 20 61 72 65 20  .** of data are 
16bc5 63 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65 20  copied from the 
16bc6 62 75 66 66 65 72 20 70 42 75 66 20 74 6f 20 70  buffer pBuf to p
16bc7 50 61 79 6c 6f 61 64 2e 0a 2a 2a 0a 2a 2a 20 53  Payload..**.** S
16bc8 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
16bc9 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2c  rned on success,
16bca 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20 65 72   otherwise an er
16bcb 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61  ror code..*/.sta
16bcc 74 69 63 20 69 6e 74 20 63 6f 70 79 50 61 79 6c  tic int copyPayl
16bcd 6f 61 64 28 0a 20 20 76 6f 69 64 20 2a 70 50 61  oad(.  void *pPa
16bce 79 6c 6f 61 64 2c 20 20 20 20 20 20 20 20 20 20  yload,          
16bcf 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70   /* Pointer to p
16bd0 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 76 6f  age data */.  vo
16bd1 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20  id *pBuf,       
16bd2 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
16bd3 65 72 20 74 6f 20 62 75 66 66 65 72 20 2a 2f 0a  er to buffer */.
16bd4 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20    int nByte,    
16bd5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
16bd6 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
16bd7 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 20  o copy */.  int 
16bd8 65 4f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20  eOp,            
16bd9 20 20 20 20 20 20 2f 2a 20 30 20 2d 3e 20 63 6f        /* 0 -> co
16bda 70 79 20 66 72 6f 6d 20 70 61 67 65 2c 20 31 20  py from page, 1 
16bdb 2d 3e 20 63 6f 70 79 20 74 6f 20 70 61 67 65 20  -> copy to page 
16bdc 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62  */.  DbPage *pDb
16bdd 50 61 67 65 20 20 20 20 20 20 20 20 20 20 20 2f  Page           /
16bde 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e  * Page containin
16bdf 67 20 70 50 61 79 6c 6f 61 64 20 2a 2f 0a 29 7b  g pPayload */.){
16be0 0a 20 20 69 66 28 20 65 4f 70 20 29 7b 0a 20 20  .  if( eOp ){.  
16be1 20 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 66    /* Copy data f
16be2 72 6f 6d 20 62 75 66 66 65 72 20 74 6f 20 70 61  rom buffer to pa
16be3 67 65 20 28 61 20 77 72 69 74 65 20 6f 70 65 72  ge (a write oper
16be4 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 69 6e  ation) */.    in
16be5 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61  t rc = sqlite3Pa
16be6 67 65 72 57 72 69 74 65 28 70 44 62 50 61 67 65  gerWrite(pDbPage
16be7 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
16be8 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
16be9 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
16bea 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50   }.    memcpy(pP
16beb 61 79 6c 6f 61 64 2c 20 70 42 75 66 2c 20 6e 42  ayload, pBuf, nB
16bec 79 74 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  yte);.  }else{. 
16bed 20 20 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 20     /* Copy data 
16bee 66 72 6f 6d 20 70 61 67 65 20 74 6f 20 62 75 66  from page to buf
16bef 66 65 72 20 28 61 20 72 65 61 64 20 6f 70 65 72  fer (a read oper
16bf0 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 6d 65  ation) */.    me
16bf1 6d 63 70 79 28 70 42 75 66 2c 20 70 50 61 79 6c  mcpy(pBuf, pPayl
16bf2 6f 61 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d  oad, nByte);.  }
16bf3 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
16bf4 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  _OK;.}../*.** Th
16bf5 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
16bf6 73 65 64 20 74 6f 20 72 65 61 64 20 6f 72 20 6f  sed to read or o
16bf7 76 65 72 77 72 69 74 65 20 70 61 79 6c 6f 61 64  verwrite payload
16bf8 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20   information.** 
16bf9 66 6f 72 20 74 68 65 20 65 6e 74 72 79 20 74 68  for the entry th
16bfa 61 74 20 74 68 65 20 70 43 75 72 20 63 75 72 73  at the pCur curs
16bfb 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74  or is pointing t
16bfc 6f 2e 20 49 66 20 74 68 65 20 65 4f 70 0a 2a 2a  o. If the eOp.**
16bfd 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 2c   parameter is 0,
16bfe 20 74 68 69 73 20 69 73 20 61 20 72 65 61 64 20   this is a read 
16bff 6f 70 65 72 61 74 69 6f 6e 20 28 64 61 74 61 20  operation (data 
16c00 63 6f 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 62  copied into.** b
16c01 75 66 66 65 72 20 70 42 75 66 29 2e 20 49 66 20  uffer pBuf). If 
16c02 69 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  it is non-zero, 
16c03 61 20 77 72 69 74 65 20 28 64 61 74 61 20 63 6f  a write (data co
16c04 70 69 65 64 20 66 72 6f 6d 0a 2a 2a 20 62 75 66  pied from.** buf
16c05 66 65 72 20 70 42 75 66 29 2e 0a 2a 2a 0a 2a 2a  fer pBuf)..**.**
16c06 20 41 20 74 6f 74 61 6c 20 6f 66 20 22 61 6d 74   A total of "amt
16c07 22 20 62 79 74 65 73 20 61 72 65 20 72 65 61 64  " bytes are read
16c08 20 6f 72 20 77 72 69 74 74 65 6e 20 62 65 67 69   or written begi
16c09 6e 6e 69 6e 67 20 61 74 20 22 6f 66 66 73 65 74  nning at "offset
16c0a 22 2e 0a 2a 2a 20 44 61 74 61 20 69 73 20 72 65  "..** Data is re
16c0b 61 64 20 74 6f 20 6f 72 20 66 72 6f 6d 20 74 68  ad to or from th
16c0c 65 20 62 75 66 66 65 72 20 70 42 75 66 2e 0a 2a  e buffer pBuf..*
16c0d 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74  *.** The content
16c0e 20 62 65 69 6e 67 20 72 65 61 64 20 6f 72 20 77   being read or w
16c0f 72 69 74 74 65 6e 20 6d 69 67 68 74 20 61 70 70  ritten might app
16c10 65 61 72 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20  ear on the main 
16c11 70 61 67 65 0a 2a 2a 20 6f 72 20 62 65 20 73 63  page.** or be sc
16c12 61 74 74 65 72 65 64 20 6f 75 74 20 6f 6e 20 6d  attered out on m
16c13 75 6c 74 69 70 6c 65 20 6f 76 65 72 66 6c 6f 77  ultiple overflow
16c14 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66   pages..**.** If
16c15 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e 69 73   the BtCursor.is
16c16 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 66  IncrblobHandle f
16c17 6c 61 67 20 69 73 20 73 65 74 2c 20 61 6e 64 20  lag is set, and 
16c18 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 63  the current.** c
16c19 75 72 73 6f 72 20 65 6e 74 72 79 20 75 73 65 73  ursor entry uses
16c1a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 76 65   one or more ove
16c1b 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 74 68 69  rflow pages, thi
16c1c 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 6c  s function.** al
16c1d 6c 6f 63 61 74 65 73 20 73 70 61 63 65 20 66 6f  locates space fo
16c1e 72 20 61 6e 64 20 6c 61 7a 69 6c 79 20 70 6f 70  r and lazily pop
16c1f 6c 75 61 74 65 73 20 74 68 65 20 6f 76 65 72 66  luates the overf
16c20 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 0a 2a  low page-list .*
16c21 2a 20 63 61 63 68 65 20 61 72 72 61 79 20 28 42  * cache array (B
16c22 74 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f  tCursor.aOverflo
16c23 77 29 2e 20 53 75 62 73 65 71 75 65 6e 74 20 63  w). Subsequent c
16c24 61 6c 6c 73 20 75 73 65 20 74 68 69 73 0a 2a 2a  alls use this.**
16c25 20 63 61 63 68 65 20 74 6f 20 6d 61 6b 65 20 73   cache to make s
16c26 65 65 6b 69 6e 67 20 74 6f 20 74 68 65 20 73 75  eeking to the su
16c27 70 70 6c 69 65 64 20 6f 66 66 73 65 74 20 6d 6f  pplied offset mo
16c28 72 65 20 65 66 66 69 63 69 65 6e 74 2e 0a 2a 2a  re efficient..**
16c29 0a 2a 2a 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72  .** Once an over
16c2a 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63  flow page-list c
16c2b 61 63 68 65 20 68 61 73 20 62 65 65 6e 20 61 6c  ache has been al
16c2c 6c 6f 63 61 74 65 64 2c 20 69 74 20 6d 61 79 20  located, it may 
16c2d 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65  be.** invalidate
16c2e 64 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20  d if some other 
16c2f 63 75 72 73 6f 72 20 77 72 69 74 65 73 20 74 6f  cursor writes to
16c30 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2c   the same table,
16c31 20 6f 72 20 69 66 0a 2a 2a 20 74 68 65 20 63 75   or if.** the cu
16c32 72 73 6f 72 20 69 73 20 6d 6f 76 65 64 20 74 6f  rsor is moved to
16c33 20 61 20 64 69 66 66 65 72 65 6e 74 20 72 6f 77   a different row
16c34 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20  . Additionally, 
16c35 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 2a  in auto-vacuum.*
16c36 2a 20 6d 6f 64 65 2c 20 74 68 65 20 66 6f 6c 6c  * mode, the foll
16c37 6f 77 69 6e 67 20 65 76 65 6e 74 73 20 6d 61 79  owing events may
16c38 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 20 6f   invalidate an o
16c39 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73  verflow page-lis
16c3a 74 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 20  t cache..**.**  
16c3b 20 2a 20 41 6e 20 69 6e 63 72 65 6d 65 6e 74 61   * An incrementa
16c3c 6c 20 76 61 63 75 75 6d 2c 0a 2a 2a 20 20 20 2a  l vacuum,.**   *
16c3d 20 41 20 63 6f 6d 6d 69 74 20 69 6e 20 61 75 74   A commit in aut
16c3e 6f 5f 76 61 63 75 75 6d 3d 22 66 75 6c 6c 22 20  o_vacuum="full" 
16c3f 6d 6f 64 65 2c 0a 2a 2a 20 20 20 2a 20 43 72 65  mode,.**   * Cre
16c40 61 74 69 6e 67 20 61 20 74 61 62 6c 65 20 28 6d  ating a table (m
16c41 61 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69 6e  ay require movin
16c42 67 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61  g an overflow pa
16c43 67 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ge)..*/.static i
16c44 6e 74 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64  nt accessPayload
16c45 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  (.  BtCursor *pC
16c46 75 72 2c 20 20 20 20 20 20 2f 2a 20 43 75 72 73  ur,      /* Curs
16c47 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 65  or pointing to e
16c48 6e 74 72 79 20 74 6f 20 72 65 61 64 20 66 72 6f  ntry to read fro
16c49 6d 20 2a 2f 0a 20 20 75 33 32 20 6f 66 66 73 65  m */.  u32 offse
16c4a 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42  t,          /* B
16c4b 65 67 69 6e 20 72 65 61 64 69 6e 67 20 74 68 69  egin reading thi
16c4c 73 20 66 61 72 20 69 6e 74 6f 20 70 61 79 6c 6f  s far into paylo
16c4d 61 64 20 2a 2f 0a 20 20 75 33 32 20 61 6d 74 2c  ad */.  u32 amt,
16c4e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
16c4f 52 65 61 64 20 74 68 69 73 20 6d 61 6e 79 20 62  Read this many b
16c50 79 74 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  ytes */.  unsign
16c51 65 64 20 63 68 61 72 20 2a 70 42 75 66 2c 20 2f  ed char *pBuf, /
16c52 2a 20 57 72 69 74 65 20 74 68 65 20 62 79 74 65  * Write the byte
16c53 73 20 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66  s into this buff
16c54 65 72 20 2a 2f 20 0a 20 20 69 6e 74 20 65 4f 70  er */ .  int eOp
16c55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
16c56 20 7a 65 72 6f 20 74 6f 20 72 65 61 64 2e 20 6e   zero to read. n
16c57 6f 6e 2d 7a 65 72 6f 20 74 6f 20 77 72 69 74 65  on-zero to write
16c58 2e 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e  . */.){.  unsign
16c59 65 64 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61  ed char *aPayloa
16c5a 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  d;.  int rc = SQ
16c5b 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 33 32 20 6e  LITE_OK;.  u32 n
16c5c 4b 65 79 3b 0a 20 20 69 6e 74 20 69 49 64 78 20  Key;.  int iIdx 
16c5d 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  = 0;.  MemPage *
16c5e 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70  pPage = pCur->ap
16c5f 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
16c60 5d 3b 20 2f 2a 20 42 74 72 65 65 20 70 61 67 65  ]; /* Btree page
16c61 20 6f 66 20 63 75 72 72 65 6e 74 20 65 6e 74 72   of current entr
16c62 79 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20  y */.  BtShared 
16c63 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74  *pBt = pCur->pBt
16c64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
16c65 20 20 20 2f 2a 20 42 74 72 65 65 20 74 68 69 73     /* Btree this
16c66 20 63 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20   cursor belongs 
16c67 74 6f 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  to */..  assert(
16c68 20 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65   pPage );.  asse
16c69 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  rt( pCur->eState
16c6a 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  ==CURSOR_VALID )
16c6b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
16c6c 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
16c6d 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c  age]<pPage->nCel
16c6e 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63  l );.  assert( c
16c6f 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
16c70 70 43 75 72 29 20 29 3b 0a 0a 20 20 67 65 74 43  pCur) );..  getC
16c71 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20  ellInfo(pCur);. 
16c72 20 61 50 61 79 6c 6f 61 64 20 3d 20 70 43 75 72   aPayload = pCur
16c73 2d 3e 69 6e 66 6f 2e 70 43 65 6c 6c 20 2b 20 70  ->info.pCell + p
16c74 43 75 72 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64 65  Cur->info.nHeade
16c75 72 3b 0a 20 20 6e 4b 65 79 20 3d 20 28 70 50 61  r;.  nKey = (pPa
16c76 67 65 2d 3e 69 6e 74 4b 65 79 20 3f 20 30 20 3a  ge->intKey ? 0 :
16c77 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f   (int)pCur->info
16c78 2e 6e 4b 65 79 29 3b 0a 0a 20 20 69 66 28 20 4e  .nKey);..  if( N
16c79 45 56 45 52 28 6f 66 66 73 65 74 2b 61 6d 74 20  EVER(offset+amt 
16c7a 3e 20 6e 4b 65 79 2b 70 43 75 72 2d 3e 69 6e 66  > nKey+pCur->inf
16c7b 6f 2e 6e 44 61 74 61 29 20 0a 20 20 20 7c 7c 20  o.nData) .   || 
16c7c 26 61 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e  &aPayload[pCur->
16c7d 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 5d 20 3e 20 26  info.nLocal] > &
16c7e 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74  pPage->aData[pBt
16c7f 2d 3e 75 73 61 62 6c 65 53 69 7a 65 5d 0a 20 20  ->usableSize].  
16c80 29 7b 0a 20 20 20 20 2f 2a 20 54 72 79 69 6e 67  ){.    /* Trying
16c81 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74   to read or writ
16c82 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f  e past the end o
16c83 66 20 74 68 65 20 64 61 74 61 20 69 73 20 61 6e  f the data is an
16c84 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 72 65   error */.    re
16c85 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
16c86 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20  UPT_BKPT;.  }.. 
16c87 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 64 61 74   /* Check if dat
16c88 61 20 6d 75 73 74 20 62 65 20 72 65 61 64 2f 77  a must be read/w
16c89 72 69 74 74 65 6e 20 74 6f 2f 66 72 6f 6d 20 74  ritten to/from t
16c8a 68 65 20 62 74 72 65 65 20 70 61 67 65 20 69 74  he btree page it
16c8b 73 65 6c 66 2e 20 2a 2f 0a 20 20 69 66 28 20 6f  self. */.  if( o
16c8c 66 66 73 65 74 3c 70 43 75 72 2d 3e 69 6e 66 6f  ffset<pCur->info
16c8d 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69  .nLocal ){.    i
16c8e 6e 74 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20  nt a = amt;.    
16c8f 69 66 28 20 61 2b 6f 66 66 73 65 74 3e 70 43 75  if( a+offset>pCu
16c90 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29  r->info.nLocal )
16c91 7b 0a 20 20 20 20 20 20 61 20 3d 20 70 43 75 72  {.      a = pCur
16c92 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20  ->info.nLocal - 
16c93 6f 66 66 73 65 74 3b 0a 20 20 20 20 7d 0a 20 20  offset;.    }.  
16c94 20 20 72 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f    rc = copyPaylo
16c95 61 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66  ad(&aPayload[off
16c96 73 65 74 5d 2c 20 70 42 75 66 2c 20 61 2c 20 65  set], pBuf, a, e
16c97 4f 70 2c 20 70 50 61 67 65 2d 3e 70 44 62 50 61  Op, pPage->pDbPa
16c98 67 65 29 3b 0a 20 20 20 20 6f 66 66 73 65 74 20  ge);.    offset 
16c99 3d 20 30 3b 0a 20 20 20 20 70 42 75 66 20 2b 3d  = 0;.    pBuf +=
16c9a 20 61 3b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 61   a;.    amt -= a
16c9b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f  ;.  }else{.    o
16c9c 66 66 73 65 74 20 2d 3d 20 70 43 75 72 2d 3e 69  ffset -= pCur->i
16c9d 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 0a  nfo.nLocal;.  }.
16c9e 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
16c9f 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b  E_OK && amt>0 ){
16ca0 0a 20 20 20 20 63 6f 6e 73 74 20 75 33 32 20 6f  .    const u32 o
16ca1 76 66 6c 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75  vflSize = pBt->u
16ca2 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 20 20  sableSize - 4;  
16ca3 2f 2a 20 42 79 74 65 73 20 63 6f 6e 74 65 6e 74  /* Bytes content
16ca4 20 70 65 72 20 6f 76 66 6c 20 70 61 67 65 20 2a   per ovfl page *
16ca5 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 74 50  /.    Pgno nextP
16ca6 61 67 65 3b 0a 0a 20 20 20 20 6e 65 78 74 50 61  age;..    nextPa
16ca7 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 61  ge = get4byte(&a
16ca8 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e  Payload[pCur->in
16ca9 66 6f 2e 6e 4c 6f 63 61 6c 5d 29 3b 0a 0a 23 69  fo.nLocal]);..#i
16caa 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
16cab 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 2f  T_INCRBLOB.    /
16cac 2a 20 49 66 20 74 68 65 20 69 73 49 6e 63 72 62  * If the isIncrb
16cad 6c 6f 62 48 61 6e 64 6c 65 20 66 6c 61 67 20 69  lobHandle flag i
16cae 73 20 73 65 74 20 61 6e 64 20 74 68 65 20 42 74  s set and the Bt
16caf 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77  Cursor.aOverflow
16cb0 5b 5d 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e 6f  [].    ** has no
16cb1 74 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64  t been allocated
16cb2 2c 20 61 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f  , allocate it no
16cb3 77 2e 20 54 68 65 20 61 72 72 61 79 20 69 73 20  w. The array is 
16cb4 73 69 7a 65 64 20 61 74 0a 20 20 20 20 2a 2a 20  sized at.    ** 
16cb5 6f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61  one entry for ea
16cb6 63 68 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  ch overflow page
16cb7 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77   in the overflow
16cb8 20 63 68 61 69 6e 2e 20 54 68 65 0a 20 20 20 20   chain. The.    
16cb9 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  ** page number o
16cba 66 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 72  f the first over
16cbb 66 6c 6f 77 20 70 61 67 65 20 69 73 20 73 74 6f  flow page is sto
16cbc 72 65 64 20 69 6e 20 61 4f 76 65 72 66 6c 6f 77  red in aOverflow
16cbd 5b 30 5d 2c 0a 20 20 20 20 2a 2a 20 65 74 63 2e  [0],.    ** etc.
16cbe 20 41 20 76 61 6c 75 65 20 6f 66 20 30 20 69 6e   A value of 0 in
16cbf 20 74 68 65 20 61 4f 76 65 72 66 6c 6f 77 5b 5d   the aOverflow[]
16cc0 20 61 72 72 61 79 20 6d 65 61 6e 73 20 22 6e 6f   array means "no
16cc1 74 20 79 65 74 20 6b 6e 6f 77 6e 22 0a 20 20 20  t yet known".   
16cc2 20 2a 2a 20 28 74 68 65 20 63 61 63 68 65 20 69   ** (the cache i
16cc3 73 20 6c 61 7a 69 6c 79 20 70 6f 70 75 6c 61 74  s lazily populat
16cc4 65 64 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ed)..    */.    
16cc5 69 66 28 20 70 43 75 72 2d 3e 69 73 49 6e 63 72  if( pCur->isIncr
16cc6 62 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20 21 70  blobHandle && !p
16cc7 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29  Cur->aOverflow )
16cc8 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4f 76 66  {.      int nOvf
16cc9 6c 20 3d 20 28 70 43 75 72 2d 3e 69 6e 66 6f 2e  l = (pCur->info.
16cca 6e 50 61 79 6c 6f 61 64 2d 70 43 75 72 2d 3e 69  nPayload-pCur->i
16ccb 6e 66 6f 2e 6e 4c 6f 63 61 6c 2b 6f 76 66 6c 53  nfo.nLocal+ovflS
16ccc 69 7a 65 2d 31 29 2f 6f 76 66 6c 53 69 7a 65 3b  ize-1)/ovflSize;
16ccd 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 4f 76  .      pCur->aOv
16cce 65 72 66 6c 6f 77 20 3d 20 28 50 67 6e 6f 20 2a  erflow = (Pgno *
16ccf 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65  )sqlite3MallocZe
16cd0 72 6f 28 73 69 7a 65 6f 66 28 50 67 6e 6f 29 2a  ro(sizeof(Pgno)*
16cd1 6e 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 2f 2a  nOvfl);.      /*
16cd2 20 6e 4f 76 66 6c 20 69 73 20 61 6c 77 61 79 73   nOvfl is always
16cd3 20 70 6f 73 69 74 69 76 65 2e 20 20 49 66 20 69   positive.  If i
16cd4 74 20 77 65 72 65 20 7a 65 72 6f 2c 20 66 65 74  t were zero, fet
16cd5 63 68 50 61 79 6c 6f 61 64 20 77 6f 75 6c 64 20  chPayload would 
16cd6 68 61 76 65 0a 20 20 20 20 20 20 2a 2a 20 62 65  have.      ** be
16cd7 65 6e 20 75 73 65 64 20 69 6e 73 74 65 61 64 20  en used instead 
16cd8 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  of this routine.
16cd9 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 41 4c   */.      if( AL
16cda 57 41 59 53 28 6e 4f 76 66 6c 29 20 26 26 20 21  WAYS(nOvfl) && !
16cdb 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20  pCur->aOverflow 
16cdc 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
16cdd 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
16cde 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
16cdf 20 2f 2a 20 49 66 20 74 68 65 20 6f 76 65 72 66   /* If the overf
16ce0 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61  low page-list ca
16ce1 63 68 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c  che has been all
16ce2 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 0a 20  ocated and the. 
16ce3 20 20 20 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20     ** entry for 
16ce4 74 68 65 20 66 69 72 73 74 20 72 65 71 75 69 72  the first requir
16ce5 65 64 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  ed overflow page
16ce6 20 69 73 20 76 61 6c 69 64 2c 20 73 6b 69 70 0a   is valid, skip.
16ce7 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79 20      ** directly 
16ce8 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  to it..    */.  
16ce9 20 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65    if( pCur->aOve
16cea 72 66 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e 61  rflow && pCur->a
16ceb 4f 76 65 72 66 6c 6f 77 5b 6f 66 66 73 65 74 2f  Overflow[offset/
16cec 6f 76 66 6c 53 69 7a 65 5d 20 29 7b 0a 20 20 20  ovflSize] ){.   
16ced 20 20 20 69 49 64 78 20 3d 20 28 6f 66 66 73 65     iIdx = (offse
16cee 74 2f 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20  t/ovflSize);.   
16cef 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 70 43     nextPage = pC
16cf0 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49  ur->aOverflow[iI
16cf1 64 78 5d 3b 0a 20 20 20 20 20 20 6f 66 66 73 65  dx];.      offse
16cf2 74 20 3d 20 28 6f 66 66 73 65 74 25 6f 76 66 6c  t = (offset%ovfl
16cf3 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  Size);.    }.#en
16cf4 64 69 66 0a 0a 20 20 20 20 66 6f 72 28 20 3b 20  dif..    for( ; 
16cf5 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
16cf6 20 61 6d 74 3e 30 20 26 26 20 6e 65 78 74 50 61   amt>0 && nextPa
16cf7 67 65 3b 20 69 49 64 78 2b 2b 29 7b 0a 0a 23 69  ge; iIdx++){..#i
16cf8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
16cf9 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20  T_INCRBLOB.     
16cfa 20 2f 2a 20 49 66 20 72 65 71 75 69 72 65 64 2c   /* If required,
16cfb 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 76   populate the ov
16cfc 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74  erflow page-list
16cfd 20 63 61 63 68 65 2e 20 2a 2f 0a 20 20 20 20 20   cache. */.     
16cfe 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72   if( pCur->aOver
16cff 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20  flow ){.        
16d00 61 73 73 65 72 74 28 21 70 43 75 72 2d 3e 61 4f  assert(!pCur->aO
16d01 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 20 7c 7c  verflow[iIdx] ||
16d02 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77   pCur->aOverflow
16d03 5b 69 49 64 78 5d 3d 3d 6e 65 78 74 50 61 67 65  [iIdx]==nextPage
16d04 29 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d  );.        pCur-
16d05 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d  >aOverflow[iIdx]
16d06 20 3d 20 6e 65 78 74 50 61 67 65 3b 0a 20 20 20   = nextPage;.   
16d07 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
16d08 20 20 20 69 66 28 20 6f 66 66 73 65 74 3e 3d 6f     if( offset>=o
16d09 76 66 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  vflSize ){.     
16d0a 20 20 20 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 72     /* The only r
16d0b 65 61 73 6f 6e 20 74 6f 20 72 65 61 64 20 74 68  eason to read th
16d0c 69 73 20 70 61 67 65 20 69 73 20 74 6f 20 6f 62  is page is to ob
16d0d 74 61 69 6e 20 74 68 65 20 70 61 67 65 0a 20 20  tain the page.  
16d0e 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20        ** number 
16d0f 66 6f 72 20 74 68 65 20 6e 65 78 74 20 70 61 67  for the next pag
16d10 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f  e in the overflo
16d11 77 20 63 68 61 69 6e 2e 20 54 68 65 20 70 61 67  w chain. The pag
16d12 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 61 74  e.        ** dat
16d13 61 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65  a is not require
16d14 64 2e 20 53 6f 20 66 69 72 73 74 20 74 72 79 20  d. So first try 
16d15 74 6f 20 6c 6f 6f 6b 75 70 20 74 68 65 20 6f 76  to lookup the ov
16d16 65 72 66 6c 6f 77 0a 20 20 20 20 20 20 20 20 2a  erflow.        *
16d17 2a 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68  * page-list cach
16d18 65 2c 20 69 66 20 61 6e 79 2c 20 74 68 65 6e 20  e, if any, then 
16d19 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 74 68 65  fall back to the
16d1a 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65   getOverflowPage
16d1b 28 29 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 75  ().        ** fu
16d1c 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20  nction..        
16d1d 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
16d1e 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a  E_OMIT_INCRBLOB.
16d1f 20 20 20 20 20 20 20 20 69 66 28 20 70 43 75 72          if( pCur
16d20 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 26 26 20 70  ->aOverflow && p
16d21 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69  Cur->aOverflow[i
16d22 49 64 78 2b 31 5d 20 29 7b 0a 20 20 20 20 20 20  Idx+1] ){.      
16d23 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 70      nextPage = p
16d24 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69  Cur->aOverflow[i
16d25 49 64 78 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20  Idx+1];.        
16d26 7d 20 65 6c 73 65 20 0a 23 65 6e 64 69 66 0a 20  } else .#endif. 
16d27 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 67 65           rc = ge
16d28 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 70 42  tOverflowPage(pB
16d29 74 2c 20 6e 65 78 74 50 61 67 65 2c 20 30 2c 20  t, nextPage, 0, 
16d2a 26 6e 65 78 74 50 61 67 65 29 3b 0a 20 20 20 20  &nextPage);.    
16d2b 20 20 20 20 6f 66 66 73 65 74 20 2d 3d 20 6f 76      offset -= ov
16d2c 66 6c 53 69 7a 65 3b 0a 20 20 20 20 20 20 7d 65  flSize;.      }e
16d2d 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  lse{.        /* 
16d2e 4e 65 65 64 20 74 6f 20 72 65 61 64 20 74 68 69  Need to read thi
16d2f 73 20 70 61 67 65 20 70 72 6f 70 65 72 6c 79 2e  s page properly.
16d30 20 49 74 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d   It contains som
16d31 65 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20  e of the.       
16d32 20 2a 2a 20 72 61 6e 67 65 20 6f 66 20 64 61 74   ** range of dat
16d33 61 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 20  a that is being 
16d34 72 65 61 64 20 28 65 4f 70 3d 3d 30 29 20 6f 72  read (eOp==0) or
16d35 20 77 72 69 74 74 65 6e 20 28 65 4f 70 21 3d 30   written (eOp!=0
16d36 29 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  )..        */.  
16d37 20 20 20 20 20 20 44 62 50 61 67 65 20 2a 70 44        DbPage *pD
16d38 62 50 61 67 65 3b 0a 20 20 20 20 20 20 20 20 69  bPage;.        i
16d39 6e 74 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20  nt a = amt;.    
16d3a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
16d3b 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50  PagerGet(pBt->pP
16d3c 61 67 65 72 2c 20 6e 65 78 74 50 61 67 65 2c 20  ager, nextPage, 
16d3d 26 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  &pDbPage);.     
16d3e 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
16d3f 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
16d40 20 20 61 50 61 79 6c 6f 61 64 20 3d 20 73 71 6c    aPayload = sql
16d41 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61  ite3PagerGetData
16d42 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  (pDbPage);.     
16d43 20 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20       nextPage = 
16d44 67 65 74 34 62 79 74 65 28 61 50 61 79 6c 6f 61  get4byte(aPayloa
16d45 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  d);.          if
16d46 28 20 61 20 2b 20 6f 66 66 73 65 74 20 3e 20 6f  ( a + offset > o
16d47 76 66 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  vflSize ){.     
16d48 20 20 20 20 20 20 20 61 20 3d 20 6f 76 66 6c 53         a = ovflS
16d49 69 7a 65 20 2d 20 6f 66 66 73 65 74 3b 0a 20 20  ize - offset;.  
16d4a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
16d4b 20 20 20 20 72 63 20 3d 20 63 6f 70 79 50 61 79      rc = copyPay
16d4c 6c 6f 61 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f  load(&aPayload[o
16d4d 66 66 73 65 74 2b 34 5d 2c 20 70 42 75 66 2c 20  ffset+4], pBuf, 
16d4e 61 2c 20 65 4f 70 2c 20 70 44 62 50 61 67 65 29  a, eOp, pDbPage)
16d4f 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
16d50 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44  te3PagerUnref(pD
16d51 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  bPage);.        
16d52 20 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20    offset = 0;.  
16d53 20 20 20 20 20 20 20 20 61 6d 74 20 2d 3d 20 61          amt -= a
16d54 3b 0a 20 20 20 20 20 20 20 20 20 20 70 42 75 66  ;.          pBuf
16d55 20 2b 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 7d   += a;.        }
16d56 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
16d57 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   }..  if( rc==SQ
16d58 4c 49 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30  LITE_OK && amt>0
16d59 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
16d5a 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
16d5b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  PT;.  }.  return
16d5c 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
16d5d 61 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 6b  ad part of the k
16d5e 65 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ey associated wi
16d5f 74 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20  th cursor pCur. 
16d60 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74   Exactly.** "amt
16d61 22 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20  " bytes will be 
16d62 74 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20  transfered into 
16d63 70 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61  pBuf[].  The tra
16d64 6e 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20  nsfer.** begins 
16d65 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a  at "offset"..**.
16d66 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75  ** The caller mu
16d67 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 70  st ensure that p
16d68 43 75 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20  Cur is pointing 
16d69 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 0a 2a  to a valid row.*
16d6a 2a 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a  * in the table..
16d6b 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c  **.** Return SQL
16d6c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73  ITE_OK on succes
16d6d 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f  s or an error co
16d6e 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67  de if anything g
16d6f 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41  oes.** wrong.  A
16d70 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72  n error is retur
16d71 6e 65 64 20 69 66 20 22 6f 66 66 73 65 74 2b 61  ned if "offset+a
16d72 6d 74 22 20 69 73 20 6c 61 72 67 65 72 20 74 68  mt" is larger th
16d73 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c 61  an.** the availa
16d74 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a  ble payload..*/.
16d75 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
16d76 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b  nt sqlite3BtreeK
16d77 65 79 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  ey(BtCursor *pCu
16d78 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75  r, u32 offset, u
16d79 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42  32 amt, void *pB
16d7a 75 66 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63  uf){.  assert( c
16d7b 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
16d7c 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72  pCur) );.  asser
16d7d 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
16d7e 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b  =CURSOR_VALID );
16d7f 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
16d80 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75  >iPage>=0 && pCu
16d81 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
16d82 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 65  iPage] );.  asse
16d83 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  rt( pCur->aiIdx[
16d84 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75  pCur->iPage]<pCu
16d85 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
16d86 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b  iPage]->nCell );
16d87 0a 20 20 72 65 74 75 72 6e 20 61 63 63 65 73 73  .  return access
16d88 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66  Payload(pCur, of
16d89 66 73 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69  fset, amt, (unsi
16d8a 67 6e 65 64 20 63 68 61 72 2a 29 70 42 75 66 2c  gned char*)pBuf,
16d8b 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   0);.}../*.** Re
16d8c 61 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 64  ad part of the d
16d8d 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77  ata associated w
16d8e 69 74 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e  ith cursor pCur.
16d8f 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d    Exactly.** "am
16d90 74 22 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65  t" bytes will be
16d91 20 74 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f   transfered into
16d92 20 70 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72   pBuf[].  The tr
16d93 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73  ansfer.** begins
16d94 20 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a   at "offset"..**
16d95 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
16d96 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20  E_OK on success 
16d97 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  or an error code
16d98 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65   if anything goe
16d99 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20  s.** wrong.  An 
16d9a 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65  error is returne
16d9b 64 20 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74  d if "offset+amt
16d9c 22 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e  " is larger than
16d9d 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c  .** the availabl
16d9e 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51  e payload..*/.SQ
16d9f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
16da0 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74   sqlite3BtreeDat
16da1 61 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  a(BtCursor *pCur
16da2 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33  , u32 offset, u3
16da3 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75  2 amt, void *pBu
16da4 66 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 23  f){.  int rc;..#
16da5 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
16da6 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 69 66  IT_INCRBLOB.  if
16da7 20 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d   ( pCur->eState=
16da8 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20  =CURSOR_INVALID 
16da9 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
16daa 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a  LITE_ABORT;.  }.
16dab 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74  #endif..  assert
16dac 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
16dad 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63  ex(pCur) );.  rc
16dae 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72   = restoreCursor
16daf 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a  Position(pCur);.
16db0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
16db1 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72  _OK ){.    asser
16db2 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
16db3 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b  =CURSOR_VALID );
16db4 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75  .    assert( pCu
16db5 72 2d 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70  r->iPage>=0 && p
16db6 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
16db7 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 20 20  ->iPage] );.    
16db8 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69  assert( pCur->ai
16db9 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
16dba 3c 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43  <pCur->apPage[pC
16dbb 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c  ur->iPage]->nCel
16dbc 6c 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 63  l );.    rc = ac
16dbd 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72  cessPayload(pCur
16dbe 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70  , offset, amt, p
16dbf 42 75 66 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72  Buf, 0);.  }.  r
16dc0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
16dc1 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
16dc2 74 65 72 20 74 6f 20 70 61 79 6c 6f 61 64 20 69  ter to payload i
16dc3 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20  nformation from 
16dc4 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74  the entry that t
16dc5 68 65 20 0a 2a 2a 20 70 43 75 72 20 63 75 72 73  he .** pCur curs
16dc6 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74  or is pointing t
16dc7 6f 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 72 20  o.  The pointer 
16dc8 69 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e  is to the beginn
16dc9 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65  ing of.** the ke
16dca 79 20 69 66 20 73 6b 69 70 4b 65 79 3d 3d 30 20  y if skipKey==0 
16dcb 61 6e 64 20 69 74 20 70 6f 69 6e 74 73 20 74 6f  and it points to
16dcc 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
16dcd 66 20 64 61 74 61 20 69 66 0a 2a 2a 20 73 6b 69  f data if.** ski
16dce 70 4b 65 79 3d 3d 31 2e 20 20 54 68 65 20 6e 75  pKey==1.  The nu
16dcf 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
16dd0 20 61 76 61 69 6c 61 62 6c 65 20 6b 65 79 2f 64   available key/d
16dd1 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 0a 2a  ata is written.*
16dd2 2a 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 20 20 49  * into *pAmt.  I
16dd3 66 20 2a 70 41 6d 74 3d 3d 30 2c 20 74 68 65 6e  f *pAmt==0, then
16dd4 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   the value retur
16dd5 6e 65 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 0a  ned will not be.
16dd6 2a 2a 20 61 20 76 61 6c 69 64 20 70 6f 69 6e 74  ** a valid point
16dd7 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  er..**.** This r
16dd8 6f 75 74 69 6e 65 20 69 73 20 61 6e 20 6f 70 74  outine is an opt
16dd9 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 49 74 20 69  imization.  It i
16dda 73 20 63 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68 65  s common for the
16ddb 20 65 6e 74 69 72 65 20 6b 65 79 0a 2a 2a 20 61   entire key.** a
16ddc 6e 64 20 64 61 74 61 20 74 6f 20 66 69 74 20 6f  nd data to fit o
16ddd 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65  n the local page
16dde 20 61 6e 64 20 66 6f 72 20 74 68 65 72 65 20 74   and for there t
16ddf 6f 20 62 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77  o be no overflow
16de0 0a 2a 2a 20 70 61 67 65 73 2e 20 20 57 68 65 6e  .** pages.  When
16de1 20 74 68 61 74 20 69 73 20 73 6f 2c 20 74 68 69   that is so, thi
16de2 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65  s routine can be
16de3 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20   used to access 
16de4 74 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64  the.** key and d
16de5 61 74 61 20 77 69 74 68 6f 75 74 20 6d 61 6b 69  ata without maki
16de6 6e 67 20 61 20 63 6f 70 79 2e 20 20 49 66 20 74  ng a copy.  If t
16de7 68 65 20 6b 65 79 20 61 6e 64 2f 6f 72 20 64 61  he key and/or da
16de8 74 61 20 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e 74  ta spills.** ont
16de9 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  o overflow pages
16dea 2c 20 74 68 65 6e 20 61 63 63 65 73 73 50 61 79  , then accessPay
16deb 6c 6f 61 64 28 29 20 6d 75 73 74 20 62 65 20 75  load() must be u
16dec 73 65 64 20 74 6f 20 72 65 61 73 73 65 6d 62 6c  sed to reassembl
16ded 65 0a 2a 2a 20 74 68 65 20 6b 65 79 2f 64 61 74  e.** the key/dat
16dee 61 20 61 6e 64 20 63 6f 70 79 20 69 74 20 69 6e  a and copy it in
16def 74 6f 20 61 20 70 72 65 61 6c 6c 6f 63 61 74 65  to a preallocate
16df0 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20  d buffer..**.** 
16df1 54 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75  The pointer retu
16df2 72 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f 75  rned by this rou
16df3 74 69 6e 65 20 6c 6f 6f 6b 73 20 64 69 72 65 63  tine looks direc
16df4 74 6c 79 20 69 6e 74 6f 20 74 68 65 20 63 61 63  tly into the cac
16df5 68 65 64 0a 2a 2a 20 70 61 67 65 20 6f 66 20 74  hed.** page of t
16df6 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68  he database.  Th
16df7 65 20 64 61 74 61 20 6d 69 67 68 74 20 63 68 61  e data might cha
16df8 6e 67 65 20 6f 72 20 6d 6f 76 65 20 74 68 65 20  nge or move the 
16df9 6e 65 78 74 20 74 69 6d 65 0a 2a 2a 20 61 6e 79  next time.** any
16dfa 20 62 74 72 65 65 20 72 6f 75 74 69 6e 65 20 69   btree routine i
16dfb 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61  s called..*/.sta
16dfc 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  tic const unsign
16dfd 65 64 20 63 68 61 72 20 2a 66 65 74 63 68 50 61  ed char *fetchPa
16dfe 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73 6f  yload(.  BtCurso
16dff 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f 2a  r *pCur,      /*
16e00 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67   Cursor pointing
16e01 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 61   to entry to rea
16e02 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20  d from */.  int 
16e03 2a 70 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20  *pAmt,          
16e04 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75   /* Write the nu
16e05 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c  mber of availabl
16e06 65 20 62 79 74 65 73 20 68 65 72 65 20 2a 2f 0a  e bytes here */.
16e07 20 20 69 6e 74 20 73 6b 69 70 4b 65 79 20 20 20    int skipKey   
16e08 20 20 20 20 20 20 20 2f 2a 20 72 65 61 64 20 62         /* read b
16e09 65 67 69 6e 6e 69 6e 67 20 61 74 20 64 61 74 61  eginning at data
16e0a 20 69 66 20 74 68 69 73 20 69 73 20 74 72 75 65   if this is true
16e0b 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65   */.){.  unsigne
16e0c 64 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64  d char *aPayload
16e0d 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ;.  MemPage *pPa
16e0e 67 65 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a  ge;.  u32 nKey;.
16e0f 20 20 75 33 32 20 6e 4c 6f 63 61 6c 3b 0a 0a 20    u32 nLocal;.. 
16e10 20 61 73 73 65 72 74 28 20 70 43 75 72 21 3d 30   assert( pCur!=0
16e11 20 26 26 20 70 43 75 72 2d 3e 69 50 61 67 65 3e   && pCur->iPage>
16e12 3d 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61  =0 && pCur->apPa
16e13 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29  ge[pCur->iPage])
16e14 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
16e15 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
16e16 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65  _VALID );.  asse
16e17 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
16e18 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20  utex(pCur) );.  
16e19 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70  pPage = pCur->ap
16e1a 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
16e1b 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  ];.  assert( pCu
16e1c 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
16e1d 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65  Page]<pPage->nCe
16e1e 6c 6c 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45  ll );.  if( NEVE
16e1f 52 28 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69  R(pCur->info.nSi
16e20 7a 65 3d 3d 30 29 20 29 7b 0a 20 20 20 20 62 74  ze==0) ){.    bt
16e21 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70 43 75  reeParseCell(pCu
16e22 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
16e23 69 50 61 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69  iPage], pCur->ai
16e24 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
16e25 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
16e26 20 20 20 20 20 26 70 43 75 72 2d 3e 69 6e 66 6f       &pCur->info
16e27 29 3b 0a 20 20 7d 0a 20 20 61 50 61 79 6c 6f 61  );.  }.  aPayloa
16e28 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 70  d = pCur->info.p
16e29 43 65 6c 6c 3b 0a 20 20 61 50 61 79 6c 6f 61 64  Cell;.  aPayload
16e2a 20 2b 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e   += pCur->info.n
16e2b 48 65 61 64 65 72 3b 0a 20 20 69 66 28 20 70 50  Header;.  if( pP
16e2c 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20  age->intKey ){. 
16e2d 20 20 20 6e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d     nKey = 0;.  }
16e2e 65 6c 73 65 7b 0a 20 20 20 20 6e 4b 65 79 20 3d  else{.    nKey =
16e2f 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f   (int)pCur->info
16e30 2e 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28  .nKey;.  }.  if(
16e31 20 73 6b 69 70 4b 65 79 20 29 7b 0a 20 20 20 20   skipKey ){.    
16e32 61 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65 79  aPayload += nKey
16e33 3b 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70  ;.    nLocal = p
16e34 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c  Cur->info.nLocal
16e35 20 2d 20 6e 4b 65 79 3b 0a 20 20 7d 65 6c 73 65   - nKey;.  }else
16e36 7b 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70  {.    nLocal = p
16e37 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c  Cur->info.nLocal
16e38 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 4c  ;.    assert( nL
16e39 6f 63 61 6c 3c 3d 6e 4b 65 79 20 29 3b 0a 20 20  ocal<=nKey );.  
16e3a 7d 0a 20 20 2a 70 41 6d 74 20 3d 20 6e 4c 6f 63  }.  *pAmt = nLoc
16e3b 61 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 50 61  al;.  return aPa
16e3c 79 6c 6f 61 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  yload;.}.../*.**
16e3d 20 46 6f 72 20 74 68 65 20 65 6e 74 72 79 20 74   For the entry t
16e3e 68 61 74 20 63 75 72 73 6f 72 20 70 43 75 72 20  hat cursor pCur 
16e3f 69 73 20 70 6f 69 6e 74 20 74 6f 2c 20 72 65 74  is point to, ret
16e40 75 72 6e 20 61 73 0a 2a 2a 20 6d 61 6e 79 20 62  urn as.** many b
16e41 79 74 65 73 20 6f 66 20 74 68 65 20 6b 65 79 20  ytes of the key 
16e42 6f 72 20 64 61 74 61 20 61 73 20 61 72 65 20 61  or data as are a
16e43 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20  vailable on the 
16e44 6c 6f 63 61 6c 0a 2a 2a 20 62 2d 74 72 65 65 20  local.** b-tree 
16e45 70 61 67 65 2e 20 20 57 72 69 74 65 20 74 68 65  page.  Write the
16e46 20 6e 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c   number of avail
16e47 61 62 6c 65 20 62 79 74 65 73 20 69 6e 74 6f 20  able bytes into 
16e48 2a 70 41 6d 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  *pAmt..**.** The
16e49 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65   pointer returne
16e4a 64 20 69 73 20 65 70 68 65 6d 65 72 61 6c 2e 20  d is ephemeral. 
16e4b 20 54 68 65 20 6b 65 79 2f 64 61 74 61 20 6d 61   The key/data ma
16e4c 79 20 6d 6f 76 65 0a 2a 2a 20 6f 72 20 62 65 20  y move.** or be 
16e4d 64 65 73 74 72 6f 79 65 64 20 6f 6e 20 74 68 65  destroyed on the
16e4e 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 61 6e   next call to an
16e4f 79 20 42 74 72 65 65 20 72 6f 75 74 69 6e 65 2c  y Btree routine,
16e50 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 61  .** including ca
16e51 6c 6c 73 20 66 72 6f 6d 20 6f 74 68 65 72 20 74  lls from other t
16e52 68 72 65 61 64 73 20 61 67 61 69 6e 73 74 20 74  hreads against t
16e53 68 65 20 73 61 6d 65 20 63 61 63 68 65 2e 0a 2a  he same cache..*
16e54 2a 20 48 65 6e 63 65 2c 20 61 20 6d 75 74 65 78  * Hence, a mutex
16e55 20 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65 64   on the BtShared
16e56 20 73 68 6f 75 6c 64 20 62 65 20 68 65 6c 64 20   should be held 
16e57 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67  prior to calling
16e58 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
16e59 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  ..**.** These ro
16e5a 75 74 69 6e 65 73 20 69 73 20 75 73 65 64 20 74  utines is used t
16e5b 6f 20 67 65 74 20 71 75 69 63 6b 20 61 63 63 65  o get quick acce
16e5c 73 73 20 74 6f 20 6b 65 79 20 61 6e 64 20 64 61  ss to key and da
16e5d 74 61 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 6d  ta.** in the com
16e5e 6d 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20 6e  mon case where n
16e5f 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  o overflow pages
16e60 20 61 72 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51   are used..*/.SQ
16e61 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
16e62 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
16e63 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 42 74  BtreeKeyFetch(Bt
16e64 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e  Cursor *pCur, in
16e65 74 20 2a 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73  t *pAmt){.  cons
16e66 74 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20  t void *p = 0;. 
16e67 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
16e68 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72  _mutex_held(pCur
16e69 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75  ->pBtree->db->mu
16e6a 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
16e6b 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
16e6c 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66  ex(pCur) );.  if
16e6d 28 20 41 4c 57 41 59 53 28 70 43 75 72 2d 3e 65  ( ALWAYS(pCur->e
16e6e 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
16e6f 4c 49 44 29 20 29 7b 0a 20 20 20 20 70 20 3d 20  LID) ){.    p = 
16e70 28 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 74  (const void*)fet
16e71 63 68 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20  chPayload(pCur, 
16e72 70 41 6d 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  pAmt, 0);.  }.  
16e73 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 53 51 4c 49  return p;.}.SQLI
16e74 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
16e75 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74   void *sqlite3Bt
16e76 72 65 65 44 61 74 61 46 65 74 63 68 28 42 74 43  reeDataFetch(BtC
16e77 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74  ursor *pCur, int
16e78 20 2a 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 74   *pAmt){.  const
16e79 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20   void *p = 0;.  
16e7a 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
16e7b 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d  mutex_held(pCur-
16e7c 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74  >pBtree->db->mut
16e7d 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
16e7e 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
16e7f 78 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28  x(pCur) );.  if(
16e80 20 41 4c 57 41 59 53 28 70 43 75 72 2d 3e 65 53   ALWAYS(pCur->eS
16e81 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c  tate==CURSOR_VAL
16e82 49 44 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 28  ID) ){.    p = (
16e83 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63  const void*)fetc
16e84 68 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70  hPayload(pCur, p
16e85 41 6d 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72  Amt, 1);.  }.  r
16e86 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a  eturn p;.}.../*.
16e87 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73  ** Move the curs
16e88 6f 72 20 64 6f 77 6e 20 74 6f 20 61 20 6e 65 77  or down to a new
16e89 20 63 68 69 6c 64 20 70 61 67 65 2e 20 20 54 68   child page.  Th
16e8a 65 20 6e 65 77 50 67 6e 6f 20 61 72 67 75 6d 65  e newPgno argume
16e8b 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 67  nt is the.** pag
16e8c 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
16e8d 63 68 69 6c 64 20 70 61 67 65 20 74 6f 20 6d 6f  child page to mo
16e8e 76 65 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ve to..**.** Thi
16e8f 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
16e90 6e 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  ns SQLITE_CORRUP
16e91 54 20 69 66 20 74 68 65 20 70 61 67 65 2d 68 65  T if the page-he
16e92 61 64 65 72 20 66 6c 61 67 73 20 66 69 65 6c 64  ader flags field
16e93 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 63   of.** the new c
16e94 68 69 6c 64 20 70 61 67 65 20 64 6f 65 73 20 6e  hild page does n
16e95 6f 74 20 6d 61 74 63 68 20 74 68 65 20 66 6c 61  ot match the fla
16e96 67 73 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20  gs field of the 
16e97 70 61 72 65 6e 74 20 28 69 2e 65 2e 0a 2a 2a 20  parent (i.e..** 
16e98 69 66 20 61 6e 20 69 6e 74 6b 65 79 20 70 61 67  if an intkey pag
16e99 65 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20  e appears to be 
16e9a 74 68 65 20 70 61 72 65 6e 74 20 6f 66 20 61 20  the parent of a 
16e9b 6e 6f 6e 2d 69 6e 74 6b 65 79 20 70 61 67 65 2c  non-intkey page,
16e9c 20 6f 72 0a 2a 2a 20 76 69 63 65 2d 76 65 72 73   or.** vice-vers
16e9d 61 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  a)..*/.static in
16e9e 74 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 42 74  t moveToChild(Bt
16e9f 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33  Cursor *pCur, u3
16ea0 32 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 69 6e  2 newPgno){.  in
16ea1 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 20 3d 20  t rc;.  int i = 
16ea2 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 4d  pCur->iPage;.  M
16ea3 65 6d 50 61 67 65 20 2a 70 4e 65 77 50 61 67 65  emPage *pNewPage
16ea4 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
16ea5 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 0a  t = pCur->pBt;..
16ea6 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
16ea7 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
16ea8 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
16ea9 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
16eaa 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73  OR_VALID );.  as
16eab 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67  sert( pCur->iPag
16eac 65 3c 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44  e<BTCURSOR_MAX_D
16ead 45 50 54 48 20 29 3b 0a 20 20 69 66 28 20 70 43  EPTH );.  if( pC
16eae 75 72 2d 3e 69 50 61 67 65 3e 3d 28 42 54 43 55  ur->iPage>=(BTCU
16eaf 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 2d 31  RSOR_MAX_DEPTH-1
16eb0 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
16eb1 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
16eb2 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  KPT;.  }.  rc = 
16eb3 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70  getAndInitPage(p
16eb4 42 74 2c 20 6e 65 77 50 67 6e 6f 2c 20 26 70 4e  Bt, newPgno, &pN
16eb5 65 77 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72  ewPage);.  if( r
16eb6 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
16eb7 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 2b   pCur->apPage[i+
16eb8 31 5d 20 3d 20 70 4e 65 77 50 61 67 65 3b 0a 20  1] = pNewPage;. 
16eb9 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 2b 31   pCur->aiIdx[i+1
16eba 5d 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69  ] = 0;.  pCur->i
16ebb 50 61 67 65 2b 2b 3b 0a 0a 20 20 70 43 75 72 2d  Page++;..  pCur-
16ebc 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b  >info.nSize = 0;
16ebd 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b  .  pCur->validNK
16ebe 65 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 4e  ey = 0;.  if( pN
16ebf 65 77 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 31 20  ewPage->nCell<1 
16ec0 7c 7c 20 70 4e 65 77 50 61 67 65 2d 3e 69 6e 74  || pNewPage->int
16ec1 4b 65 79 21 3d 70 43 75 72 2d 3e 61 70 50 61 67  Key!=pCur->apPag
16ec2 65 5b 69 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a  e[i]->intKey ){.
16ec3 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
16ec4 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
16ec5 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
16ec6 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64  ITE_OK;.}..#ifnd
16ec7 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20  ef NDEBUG./*.** 
16ec8 50 61 67 65 20 70 50 61 72 65 6e 74 20 69 73 20  Page pParent is 
16ec9 61 6e 20 69 6e 74 65 72 6e 61 6c 20 28 6e 6f 6e  an internal (non
16eca 2d 6c 65 61 66 29 20 74 72 65 65 20 70 61 67 65  -leaf) tree page
16ecb 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
16ecc 0a 2a 2a 20 61 73 73 65 72 74 73 20 74 68 61 74  .** asserts that
16ecd 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 43 68   page number iCh
16ece 69 6c 64 20 69 73 20 74 68 65 20 6c 65 66 74 2d  ild is the left-
16ecf 63 68 69 6c 64 20 69 66 20 74 68 65 20 69 49 64  child if the iId
16ed0 78 27 74 68 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20  x'th.** cell in 
16ed1 70 61 67 65 20 70 50 61 72 65 6e 74 2e 20 4f 72  page pParent. Or
16ed2 2c 20 69 66 20 69 49 64 78 20 69 73 20 65 71 75  , if iIdx is equ
16ed3 61 6c 20 74 6f 20 74 68 65 20 74 6f 74 61 6c 20  al to the total 
16ed4 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 65 6c  number of.** cel
16ed5 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 2c 20 74  ls in pParent, t
16ed6 68 61 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20  hat page number 
16ed7 69 43 68 69 6c 64 20 69 73 20 74 68 65 20 72 69  iChild is the ri
16ed8 67 68 74 2d 63 68 69 6c 64 20 6f 66 0a 2a 2a 20  ght-child of.** 
16ed9 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61  the page..*/.sta
16eda 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74 50  tic void assertP
16edb 61 72 65 6e 74 49 6e 64 65 78 28 4d 65 6d 50 61  arentIndex(MemPa
16edc 67 65 20 2a 70 50 61 72 65 6e 74 2c 20 69 6e 74  ge *pParent, int
16edd 20 69 49 64 78 2c 20 50 67 6e 6f 20 69 43 68 69   iIdx, Pgno iChi
16ede 6c 64 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69  ld){.  assert( i
16edf 49 64 78 3c 3d 70 50 61 72 65 6e 74 2d 3e 6e 43  Idx<=pParent->nC
16ee0 65 6c 6c 20 29 3b 0a 20 20 69 66 28 20 69 49 64  ell );.  if( iId
16ee1 78 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c  x==pParent->nCel
16ee2 6c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  l ){.    assert(
16ee3 20 67 65 74 34 62 79 74 65 28 26 70 50 61 72 65   get4byte(&pPare
16ee4 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e  nt->aData[pParen
16ee5 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  t->hdrOffset+8])
16ee6 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d 65  ==iChild );.  }e
16ee7 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
16ee8 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 65   get4byte(findCe
16ee9 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 49 64 78  ll(pParent, iIdx
16eea 29 29 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20  ))==iChild );.  
16eeb 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 20 64 65 66  }.}.#else.#  def
16eec 69 6e 65 20 61 73 73 65 72 74 50 61 72 65 6e 74  ine assertParent
16eed 49 6e 64 65 78 28 78 2c 79 2c 7a 29 20 0a 23 65  Index(x,y,z) .#e
16eee 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65  ndif../*.** Move
16eef 20 74 68 65 20 63 75 72 73 6f 72 20 75 70 20 74   the cursor up t
16ef0 6f 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67  o the parent pag
16ef1 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 72 2d 3e 69  e..**.** pCur->i
16ef2 64 78 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  dx is set to the
16ef3 20 63 65 6c 6c 20 69 6e 64 65 78 20 74 68 61 74   cell index that
16ef4 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 6f   contains the po
16ef5 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20  inter.** to the 
16ef6 70 61 67 65 20 77 65 20 61 72 65 20 63 6f 6d 69  page we are comi
16ef7 6e 67 20 66 72 6f 6d 2e 20 20 49 66 20 77 65 20  ng from.  If we 
16ef8 61 72 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20  are coming from 
16ef9 74 68 65 0a 2a 2a 20 72 69 67 68 74 2d 6d 6f 73  the.** right-mos
16efa 74 20 63 68 69 6c 64 20 70 61 67 65 20 74 68 65  t child page the
16efb 6e 20 70 43 75 72 2d 3e 69 64 78 20 69 73 20 73  n pCur->idx is s
16efc 65 74 20 74 6f 20 6f 6e 65 20 6d 6f 72 65 20 74  et to one more t
16efd 68 61 6e 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65  han.** the large
16efe 73 74 20 63 65 6c 6c 20 69 6e 64 65 78 2e 0a 2a  st cell index..*
16eff 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 6f  /.static void mo
16f00 76 65 54 6f 50 61 72 65 6e 74 28 42 74 43 75 72  veToParent(BtCur
16f01 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73  sor *pCur){.  as
16f02 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
16f03 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
16f04 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
16f05 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56  eState==CURSOR_V
16f06 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74  ALID );.  assert
16f07 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 30 20  ( pCur->iPage>0 
16f08 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  );.  assert( pCu
16f09 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
16f0a 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 65  iPage] );.  asse
16f0b 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 0a 20  rtParentIndex(. 
16f0c 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b     pCur->apPage[
16f0d 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20  pCur->iPage-1], 
16f0e 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78  .    pCur->aiIdx
16f0f 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c  [pCur->iPage-1],
16f10 20 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61   .    pCur->apPa
16f11 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d  ge[pCur->iPage]-
16f12 3e 70 67 6e 6f 0a 20 20 29 3b 0a 20 20 72 65 6c  >pgno.  );.  rel
16f13 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61  easePage(pCur->a
16f14 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
16f15 65 5d 29 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61  e]);.  pCur->iPa
16f16 67 65 2d 2d 3b 0a 20 20 70 43 75 72 2d 3e 69 6e  ge--;.  pCur->in
16f17 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20  fo.nSize = 0;.  
16f18 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20  pCur->validNKey 
16f19 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f  = 0;.}../*.** Mo
16f1a 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f  ve the cursor to
16f1b 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 72 6f   point to the ro
16f1c 6f 74 20 70 61 67 65 20 6f 66 20 69 74 73 20 62  ot page of its b
16f1d 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 2e  -tree structure.
16f1e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 74 61  .**.** If the ta
16f1f 62 6c 65 20 68 61 73 20 61 20 76 69 72 74 75 61  ble has a virtua
16f20 6c 20 72 6f 6f 74 20 70 61 67 65 2c 20 74 68 65  l root page, the
16f21 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  n the cursor is 
16f22 6d 6f 76 65 64 20 74 6f 20 70 6f 69 6e 74 0a 2a  moved to point.*
16f23 2a 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c  * to the virtual
16f24 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 73 74 65   root page inste
16f25 61 64 20 6f 66 20 74 68 65 20 61 63 74 75 61 6c  ad of the actual
16f26 20 72 6f 6f 74 20 70 61 67 65 2e 20 41 20 74 61   root page. A ta
16f27 62 6c 65 20 68 61 73 20 61 0a 2a 2a 20 76 69 72  ble has a.** vir
16f28 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 77  tual root page w
16f29 68 65 6e 20 74 68 65 20 61 63 74 75 61 6c 20 72  hen the actual r
16f2a 6f 6f 74 20 70 61 67 65 20 63 6f 6e 74 61 69 6e  oot page contain
16f2b 73 20 6e 6f 20 63 65 6c 6c 73 20 61 6e 64 20 61  s no cells and a
16f2c 20 0a 2a 2a 20 73 69 6e 67 6c 65 20 63 68 69 6c   .** single chil
16f2d 64 20 70 61 67 65 2e 20 54 68 69 73 20 63 61 6e  d page. This can
16f2e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 77 69 74   only happen wit
16f2f 68 20 74 68 65 20 74 61 62 6c 65 20 72 6f 6f 74  h the table root
16f30 65 64 20 61 74 20 70 61 67 65 20 31 2e 0a 2a 2a  ed at page 1..**
16f31 0a 2a 2a 20 49 66 20 74 68 65 20 62 2d 74 72 65  .** If the b-tre
16f32 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 65  e structure is e
16f33 6d 70 74 79 2c 20 74 68 65 20 63 75 72 73 6f 72  mpty, the cursor
16f34 20 73 74 61 74 65 20 69 73 20 73 65 74 20 74 6f   state is set to
16f35 20 0a 2a 2a 20 43 55 52 53 4f 52 5f 49 4e 56 41   .** CURSOR_INVA
16f36 4c 49 44 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  LID. Otherwise, 
16f37 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 73 65  the cursor is se
16f38 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  t to point to th
16f39 65 20 66 69 72 73 74 0a 2a 2a 20 63 65 6c 6c 20  e first.** cell 
16f3a 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65 20 72  located on the r
16f3b 6f 6f 74 20 28 6f 72 20 76 69 72 74 75 61 6c 20  oot (or virtual 
16f3c 72 6f 6f 74 29 20 70 61 67 65 20 61 6e 64 20 74  root) page and t
16f3d 68 65 20 63 75 72 73 6f 72 20 73 74 61 74 65 0a  he cursor state.
16f3e 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 43 55 52  ** is set to CUR
16f3f 53 4f 52 5f 56 41 4c 49 44 2e 0a 2a 2a 0a 2a 2a  SOR_VALID..**.**
16f40 20 49 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   If this functio
16f41 6e 20 72 65 74 75 72 6e 73 20 73 75 63 63 65 73  n returns succes
16f42 73 66 75 6c 6c 79 2c 20 69 74 20 6d 61 79 20 62  sfully, it may b
16f43 65 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74  e assumed that t
16f44 68 65 0a 2a 2a 20 70 61 67 65 2d 68 65 61 64 65  he.** page-heade
16f45 72 20 66 6c 61 67 73 20 69 6e 64 69 63 61 74 65  r flags indicate
16f46 20 74 68 61 74 20 74 68 65 20 5b 76 69 72 74 75   that the [virtu
16f47 61 6c 5d 20 72 6f 6f 74 2d 70 61 67 65 20 69 73  al] root-page is
16f48 20 74 68 65 20 65 78 70 65 63 74 65 64 20 0a 2a   the expected .*
16f49 2a 20 6b 69 6e 64 20 6f 66 20 62 2d 74 72 65 65  * kind of b-tree
16f4a 20 70 61 67 65 20 28 69 2e 65 2e 20 69 66 20 77   page (i.e. if w
16f4b 68 65 6e 20 6f 70 65 6e 69 6e 67 20 74 68 65 20  hen opening the 
16f4c 63 75 72 73 6f 72 20 74 68 65 20 63 61 6c 6c 65  cursor the calle
16f4d 72 20 64 69 64 20 6e 6f 74 0a 2a 2a 20 73 70 65  r did not.** spe
16f4e 63 69 66 79 20 61 20 4b 65 79 49 6e 66 6f 20 73  cify a KeyInfo s
16f4f 74 72 75 63 74 75 72 65 20 74 68 65 20 66 6c 61  tructure the fla
16f50 67 73 20 62 79 74 65 20 69 73 20 73 65 74 20 74  gs byte is set t
16f51 6f 20 30 78 30 35 20 6f 72 20 30 78 30 44 2c 0a  o 0x05 or 0x0D,.
16f52 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 61 20  ** indicating a 
16f53 74 61 62 6c 65 20 62 2d 74 72 65 65 2c 20 6f 72  table b-tree, or
16f54 20 69 66 20 74 68 65 20 63 61 6c 6c 65 72 20 64   if the caller d
16f55 69 64 20 73 70 65 63 69 66 79 20 61 20 4b 65 79  id specify a Key
16f56 49 6e 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74 75  Info .** structu
16f57 72 65 20 74 68 65 20 66 6c 61 67 73 20 62 79 74  re the flags byt
16f58 65 20 69 73 20 73 65 74 20 74 6f 20 30 78 30 32  e is set to 0x02
16f59 20 6f 72 20 30 78 30 41 2c 20 69 6e 64 69 63 61   or 0x0A, indica
16f5a 74 69 6e 67 20 61 6e 20 69 6e 64 65 78 0a 2a 2a  ting an index.**
16f5b 20 62 2d 74 72 65 65 29 2e 0a 2a 2f 0a 73 74 61   b-tree)..*/.sta
16f5c 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52 6f  tic int moveToRo
16f5d 6f 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  ot(BtCursor *pCu
16f5e 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  r){.  MemPage *p
16f5f 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d  Root;.  int rc =
16f60 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74   SQLITE_OK;.  Bt
16f61 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70  ree *p = pCur->p
16f62 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61 72 65  Btree;.  BtShare
16f63 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
16f64 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73  ..  assert( curs
16f65 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75  orHoldsMutex(pCu
16f66 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  r) );.  assert( 
16f67 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 3c  CURSOR_INVALID <
16f68 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53   CURSOR_REQUIRES
16f69 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  EEK );.  assert(
16f6a 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 20   CURSOR_VALID   
16f6b 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  < CURSOR_REQUIRE
16f6c 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  SEEK );.  assert
16f6d 28 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 20  ( CURSOR_FAULT  
16f6e 20 3e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52   > CURSOR_REQUIR
16f6f 45 53 45 45 4b 20 29 3b 0a 20 20 69 66 28 20 70  ESEEK );.  if( p
16f70 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52  Cur->eState>=CUR
16f71 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20  SOR_REQUIRESEEK 
16f72 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d  ){.    if( pCur-
16f73 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
16f74 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 20 20 61  FAULT ){.      a
16f75 73 73 65 72 74 28 20 70 43 75 72 2d 3e 73 6b 69  ssert( pCur->ski
16f76 70 4e 65 78 74 21 3d 53 51 4c 49 54 45 5f 4f 4b  pNext!=SQLITE_OK
16f77 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   );.      return
16f78 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b   pCur->skipNext;
16f79 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
16f7a 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73  e3BtreeClearCurs
16f7b 6f 72 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20  or(pCur);.  }.. 
16f7c 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65   if( pCur->iPage
16f7d 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  >=0 ){.    int i
16f7e 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69  ;.    for(i=1; i
16f7f 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69  <=pCur->iPage; i
16f80 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61  ++){.      relea
16f81 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50  sePage(pCur->apP
16f82 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20  age[i]);.    }. 
16f83 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d     pCur->iPage =
16f84 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
16f85 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74   rc = getAndInit
16f86 50 61 67 65 28 70 42 74 2c 20 70 43 75 72 2d 3e  Page(pBt, pCur->
16f87 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 75 72 2d  pgnoRoot, &pCur-
16f88 3e 61 70 50 61 67 65 5b 30 5d 29 3b 0a 20 20 20  >apPage[0]);.   
16f89 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
16f8a 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72  OK ){.      pCur
16f8b 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f  ->eState = CURSO
16f8c 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20  R_INVALID;.     
16f8d 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
16f8e 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67  }.    pCur->iPag
16f8f 65 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 49  e = 0;..    /* I
16f90 66 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f  f pCur->pKeyInfo
16f91 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   is not NULL, th
16f92 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 74 68  en the caller th
16f93 61 74 20 6f 70 65 6e 65 64 20 74 68 69 73 20 63  at opened this c
16f94 75 72 73 6f 72 0a 20 20 20 20 2a 2a 20 65 78 70  ursor.    ** exp
16f95 65 63 74 65 64 20 74 6f 20 6f 70 65 6e 20 69 74  ected to open it
16f96 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 62 2d 74   on an index b-t
16f97 72 65 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ree. Otherwise, 
16f98 69 66 20 70 4b 65 79 49 6e 66 6f 20 69 73 0a 20  if pKeyInfo is. 
16f99 20 20 20 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 20     ** NULL, the 
16f9a 63 61 6c 6c 65 72 20 65 78 70 65 63 74 73 20 61  caller expects a
16f9b 20 74 61 62 6c 65 20 62 2d 74 72 65 65 2e 20 49   table b-tree. I
16f9c 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 74 68  f this is not th
16f9d 65 20 63 61 73 65 2c 0a 20 20 20 20 2a 2a 20 72  e case,.    ** r
16f9e 65 74 75 72 6e 20 61 6e 20 53 51 4c 49 54 45 5f  eturn an SQLITE_
16f9f 43 4f 52 52 55 50 54 20 65 72 72 6f 72 2e 20 20  CORRUPT error.  
16fa0 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  */.    assert( p
16fa1 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e  Cur->apPage[0]->
16fa2 69 6e 74 4b 65 79 3d 3d 31 20 7c 7c 20 70 43 75  intKey==1 || pCu
16fa3 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e  r->apPage[0]->in
16fa4 74 4b 65 79 3d 3d 30 20 29 3b 0a 20 20 20 20 69  tKey==0 );.    i
16fa5 66 28 20 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e  f( (pCur->pKeyIn
16fa6 66 6f 3d 3d 30 29 21 3d 70 43 75 72 2d 3e 61 70  fo==0)!=pCur->ap
16fa7 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20  Page[0]->intKey 
16fa8 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
16fa9 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
16faa 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  KPT;.    }.  }..
16fab 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74    /* Assert that
16fac 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 69   the root page i
16fad 73 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63 74  s of the correct
16fae 20 74 79 70 65 2e 20 54 68 69 73 20 6d 75 73 74   type. This must
16faf 20 62 65 20 74 68 65 0a 20 20 2a 2a 20 63 61 73   be the.  ** cas
16fb0 65 20 61 73 20 74 68 65 20 63 61 6c 6c 20 74 6f  e as the call to
16fb1 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74   this function t
16fb2 68 61 74 20 6c 6f 61 64 65 64 20 74 68 65 20 72  hat loaded the r
16fb3 6f 6f 74 2d 70 61 67 65 20 28 65 69 74 68 65 72  oot-page (either
16fb4 0a 20 20 2a 2a 20 74 68 69 73 20 63 61 6c 6c 20  .  ** this call 
16fb5 6f 72 20 61 20 70 72 65 76 69 6f 75 73 20 69 6e  or a previous in
16fb6 76 6f 63 61 74 69 6f 6e 29 20 77 6f 75 6c 64 20  vocation) would 
16fb7 68 61 76 65 20 64 65 74 65 63 74 65 64 20 63 6f  have detected co
16fb8 72 72 75 70 74 69 6f 6e 20 0a 20 20 2a 2a 20 69  rruption .  ** i
16fb9 66 20 74 68 65 20 61 73 73 75 6d 70 74 69 6f 6e  f the assumption
16fba 20 77 65 72 65 20 6e 6f 74 20 74 72 75 65 2c 20   were not true, 
16fbb 61 6e 64 20 69 74 20 69 73 20 6e 6f 74 20 70 6f  and it is not po
16fbc 73 73 69 62 6c 65 20 66 6f 72 20 74 68 65 20 66  ssible for the f
16fbd 6c 61 67 73 20 0a 20 20 2a 2a 20 62 79 74 65 20  lags .  ** byte 
16fbe 74 6f 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64  to have been mod
16fbf 69 66 69 65 64 20 77 68 69 6c 65 20 74 68 69 73  ified while this
16fc0 20 63 75 72 73 6f 72 20 69 73 20 68 6f 6c 64 69   cursor is holdi
16fc1 6e 67 20 61 20 72 65 66 65 72 65 6e 63 65 0a 20  ng a reference. 
16fc2 20 2a 2a 20 74 6f 20 74 68 65 20 70 61 67 65 2e   ** to the page.
16fc3 20 20 2a 2f 0a 20 20 70 52 6f 6f 74 20 3d 20 70    */.  pRoot = p
16fc4 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3b 0a  Cur->apPage[0];.
16fc5 20 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d    assert( pRoot-
16fc6 3e 70 67 6e 6f 3d 3d 70 43 75 72 2d 3e 70 67 6e  >pgno==pCur->pgn
16fc7 6f 52 6f 6f 74 20 29 3b 0a 20 20 61 73 73 65 72  oRoot );.  asser
16fc8 74 28 20 70 52 6f 6f 74 2d 3e 69 73 49 6e 69 74  t( pRoot->isInit
16fc9 20 26 26 20 28 70 43 75 72 2d 3e 70 4b 65 79 49   && (pCur->pKeyI
16fca 6e 66 6f 3d 3d 30 29 3d 3d 70 52 6f 6f 74 2d 3e  nfo==0)==pRoot->
16fcb 69 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 70 43 75  intKey );..  pCu
16fcc 72 2d 3e 61 69 49 64 78 5b 30 5d 20 3d 20 30 3b  r->aiIdx[0] = 0;
16fcd 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53  .  pCur->info.nS
16fce 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d  ize = 0;.  pCur-
16fcf 3e 61 74 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70  >atLast = 0;.  p
16fd0 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d  Cur->validNKey =
16fd1 20 30 3b 0a 0a 20 20 69 66 28 20 70 52 6f 6f 74   0;..  if( pRoot
16fd2 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26 20 21 70  ->nCell==0 && !p
16fd3 52 6f 6f 74 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  Root->leaf ){.  
16fd4 20 20 50 67 6e 6f 20 73 75 62 70 61 67 65 3b 0a    Pgno subpage;.
16fd5 20 20 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e 70      if( pRoot->p
16fd6 67 6e 6f 21 3d 31 20 29 20 72 65 74 75 72 6e 20  gno!=1 ) return 
16fd7 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
16fd8 4b 50 54 3b 0a 20 20 20 20 73 75 62 70 61 67 65  KPT;.    subpage
16fd9 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 52 6f   = get4byte(&pRo
16fda 6f 74 2d 3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d  ot->aData[pRoot-
16fdb 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a  >hdrOffset+8]);.
16fdc 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65      pCur->eState
16fdd 20 3d 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b   = CURSOR_VALID;
16fde 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f  .    rc = moveTo
16fdf 43 68 69 6c 64 28 70 43 75 72 2c 20 73 75 62 70  Child(pCur, subp
16fe0 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  age);.  }else{. 
16fe1 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20     pCur->eState 
16fe2 3d 20 28 28 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c  = ((pRoot->nCell
16fe3 3e 30 29 3f 43 55 52 53 4f 52 5f 56 41 4c 49 44  >0)?CURSOR_VALID
16fe4 3a 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 29  :CURSOR_INVALID)
16fe5 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
16fe6 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65  c;.}../*.** Move
16fe7 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e   the cursor down
16fe8 20 74 6f 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73   to the left-mos
16fe9 74 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e  t leaf entry ben
16fea 65 61 74 68 20 74 68 65 0a 2a 2a 20 65 6e 74 72  eath the.** entr
16feb 79 20 74 6f 20 77 68 69 63 68 20 69 74 20 69 73  y to which it is
16fec 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
16fed 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c  ing..**.** The l
16fee 65 66 74 2d 6d 6f 73 74 20 6c 65 61 66 20 69 73  eft-most leaf is
16fef 20 74 68 65 20 6f 6e 65 20 77 69 74 68 20 74 68   the one with th
16ff0 65 20 73 6d 61 6c 6c 65 73 74 20 6b 65 79 20 2d  e smallest key -
16ff1 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e   the first.** in
16ff2 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72   ascending order
16ff3 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
16ff4 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 42  moveToLeftmost(B
16ff5 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a  tCursor *pCur){.
16ff6 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69    Pgno pgno;.  i
16ff7 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
16ff8 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  K;.  MemPage *pP
16ff9 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  age;..  assert( 
16ffa 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
16ffb 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
16ffc 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  rt( pCur->eState
16ffd 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  ==CURSOR_VALID )
16ffe 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53  ;.  while( rc==S
16fff 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 50  QLITE_OK && !(pP
17000 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61  age = pCur->apPa
17001 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29  ge[pCur->iPage])
17002 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 61 73  ->leaf ){.    as
17003 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64  sert( pCur->aiId
17004 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70  x[pCur->iPage]<p
17005 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20  Page->nCell );. 
17006 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79     pgno = get4by
17007 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67  te(findCell(pPag
17008 65 2c 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70  e, pCur->aiIdx[p
17009 43 75 72 2d 3e 69 50 61 67 65 5d 29 29 3b 0a 20  Cur->iPage]));. 
1700a 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68     rc = moveToCh
1700b 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b  ild(pCur, pgno);
1700c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1700d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20  ;.}../*.** Move 
1700e 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20  the cursor down 
1700f 74 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73  to the right-mos
17010 74 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e  t leaf entry ben
17011 65 61 74 68 20 74 68 65 0a 2a 2a 20 70 61 67 65  eath the.** page
17012 20 74 6f 20 77 68 69 63 68 20 69 74 20 69 73 20   to which it is 
17013 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69  currently pointi
17014 6e 67 2e 20 20 4e 6f 74 69 63 65 20 74 68 65 20  ng.  Notice the 
17015 64 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20 62 65  difference.** be
17016 74 77 65 65 6e 20 6d 6f 76 65 54 6f 4c 65 66 74  tween moveToLeft
17017 6d 6f 73 74 28 29 20 61 6e 64 20 6d 6f 76 65 54  most() and moveT
17018 6f 52 69 67 68 74 6d 6f 73 74 28 29 2e 20 20 6d  oRightmost().  m
17019 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 29 0a  oveToLeftmost().
1701a 2a 2a 20 66 69 6e 64 73 20 74 68 65 20 6c 65 66  ** finds the lef
1701b 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 65 6e  t-most entry ben
1701c 65 61 74 68 20 74 68 65 20 2a 65 6e 74 72 79 2a  eath the *entry*
1701d 20 77 68 65 72 65 61 73 20 6d 6f 76 65 54 6f 52   whereas moveToR
1701e 69 67 68 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 69  ightmost().** fi
1701f 6e 64 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f  nds the right-mo
17020 73 74 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68  st entry beneath
17021 20 74 68 65 20 2a 70 61 67 65 2a 2e 0a 2a 2a 0a   the *page*..**.
17022 2a 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73  ** The right-mos
17023 74 20 65 6e 74 72 79 20 69 73 20 74 68 65 20 6f  t entry is the o
17024 6e 65 20 77 69 74 68 20 74 68 65 20 6c 61 72 67  ne with the larg
17025 65 73 74 20 6b 65 79 20 2d 20 74 68 65 20 6c 61  est key - the la
17026 73 74 0a 2a 2a 20 6b 65 79 20 69 6e 20 61 73 63  st.** key in asc
17027 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f  ending order..*/
17028 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65  .static int move
17029 54 6f 52 69 67 68 74 6d 6f 73 74 28 42 74 43 75  ToRightmost(BtCu
1702a 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 50  rsor *pCur){.  P
1702b 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 20  gno pgno;.  int 
1702c 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1702d 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
1702e 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28   = 0;..  assert(
1702f 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
17030 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73  x(pCur) );.  ass
17031 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74  ert( pCur->eStat
17032 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e==CURSOR_VALID 
17033 29 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d  );.  while( rc==
17034 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70  SQLITE_OK && !(p
17035 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50  Page = pCur->apP
17036 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
17037 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 70  )->leaf ){.    p
17038 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26  gno = get4byte(&
17039 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61  pPage->aData[pPa
1703a 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  ge->hdrOffset+8]
1703b 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49  );.    pCur->aiI
1703c 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20  dx[pCur->iPage] 
1703d 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a  = pPage->nCell;.
1703e 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43      rc = moveToC
1703f 68 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29  hild(pCur, pgno)
17040 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d  ;.  }.  if( rc==
17041 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
17042 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
17043 72 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 67  r->iPage] = pPag
17044 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20  e->nCell-1;.    
17045 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65  pCur->info.nSize
17046 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e   = 0;.    pCur->
17047 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20  validNKey = 0;. 
17048 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
17049 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63  }../* Move the c
1704a 75 72 73 6f 72 20 74 6f 20 74 68 65 20 66 69 72  ursor to the fir
1704b 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  st entry in the 
1704c 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 53  table.  Return S
1704d 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73  QLITE_OK.** on s
1704e 75 63 63 65 73 73 2e 20 20 53 65 74 20 2a 70 52  uccess.  Set *pR
1704f 65 73 20 74 6f 20 30 20 69 66 20 74 68 65 20 63  es to 0 if the c
17050 75 72 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 70  ursor actually p
17051 6f 69 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 69  oints to somethi
17052 6e 67 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 52  ng.** or set *pR
17053 65 73 20 74 6f 20 31 20 69 66 20 74 68 65 20 74  es to 1 if the t
17054 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a  able is empty..*
17055 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
17056 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
17057 65 46 69 72 73 74 28 42 74 43 75 72 73 6f 72 20  eFirst(BtCursor 
17058 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73  *pCur, int *pRes
17059 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  ){.  int rc;..  
1705a 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f  assert( cursorHo
1705b 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29  ldsMutex(pCur) )
1705c 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
1705d 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1705e 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d  Cur->pBtree->db-
1705f 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20  >mutex) );.  rc 
17060 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75  = moveToRoot(pCu
17061 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  r);.  if( rc==SQ
17062 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
17063 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  f( pCur->eState=
17064 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20  =CURSOR_INVALID 
17065 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
17066 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
17067 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c  ur->iPage]->nCel
17068 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a 70  l==0 );.      *p
17069 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 72  Res = 1;.      r
1706a 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1706b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1706c 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70  assert( pCur->ap
1706d 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
1706e 5d 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20  ]->nCell>0 );.  
1706f 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20      *pRes = 0;. 
17070 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f       rc = moveTo
17071 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a  Leftmost(pCur);.
17072 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
17073 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76  rn rc;.}../* Mov
17074 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20  e the cursor to 
17075 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69  the last entry i
17076 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65  n the table.  Re
17077 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a  turn SQLITE_OK.*
17078 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 53  * on success.  S
17079 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20 69 66  et *pRes to 0 if
1707a 20 74 68 65 20 63 75 72 73 6f 72 20 61 63 74 75   the cursor actu
1707b 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 73  ally points to s
1707c 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 73  omething.** or s
1707d 65 74 20 2a 70 52 65 73 20 74 6f 20 31 20 69 66  et *pRes to 1 if
1707e 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d   the table is em
1707f 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  pty..*/.SQLITE_P
17080 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
17081 65 33 42 74 72 65 65 4c 61 73 74 28 42 74 43 75  e3BtreeLast(BtCu
17082 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20  rsor *pCur, int 
17083 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63  *pRes){.  int rc
17084 3b 0a 20 0a 20 20 61 73 73 65 72 74 28 20 63 75  ;. .  assert( cu
17085 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
17086 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Cur) );.  assert
17087 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
17088 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65  held(pCur->pBtre
17089 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  e->db->mutex) );
1708a 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 75  ..  /* If the cu
1708b 72 73 6f 72 20 61 6c 72 65 61 64 79 20 70 6f 69  rsor already poi
1708c 6e 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74 20  nts to the last 
1708d 65 6e 74 72 79 2c 20 74 68 69 73 20 69 73 20 61  entry, this is a
1708e 20 6e 6f 2d 6f 70 2e 20 2a 2f 0a 20 20 69 66 28   no-op. */.  if(
1708f 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70   CURSOR_VALID==p
17090 43 75 72 2d 3e 65 53 74 61 74 65 20 26 26 20 70  Cur->eState && p
17091 43 75 72 2d 3e 61 74 4c 61 73 74 20 29 7b 0a 23  Cur->atLast ){.#
17092 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
17093 55 47 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 62  UG.    /* This b
17094 6c 6f 63 6b 20 73 65 72 76 65 73 20 74 6f 20 61  lock serves to a
17095 73 73 65 72 74 28 29 20 74 68 61 74 20 74 68 65  ssert() that the
17096 20 63 75 72 73 6f 72 20 72 65 61 6c 6c 79 20 64   cursor really d
17097 6f 65 73 20 70 6f 69 6e 74 20 0a 20 20 20 20 2a  oes point .    *
17098 2a 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e  * to the last en
17099 74 72 79 20 69 6e 20 74 68 65 20 62 2d 74 72 65  try in the b-tre
1709a 65 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69  e. */.    int ii
1709b 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20  ;.    for(ii=0; 
1709c 69 69 3c 70 43 75 72 2d 3e 69 50 61 67 65 3b 20  ii<pCur->iPage; 
1709d 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73  ii++){.      ass
1709e 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78  ert( pCur->aiIdx
1709f 5b 69 69 5d 3d 3d 70 43 75 72 2d 3e 61 70 50 61  [ii]==pCur->apPa
170a0 67 65 5b 69 69 5d 2d 3e 6e 43 65 6c 6c 20 29 3b  ge[ii]->nCell );
170a1 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
170a2 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70  t( pCur->aiIdx[p
170a3 43 75 72 2d 3e 69 50 61 67 65 5d 3d 3d 70 43 75  Cur->iPage]==pCu
170a4 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
170a5 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 2d 31 20  iPage]->nCell-1 
170a6 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
170a7 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
170a8 2d 3e 69 50 61 67 65 5d 2d 3e 6c 65 61 66 20 29  ->iPage]->leaf )
170a9 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65 74  ;.#endif.    ret
170aa 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
170ab 20 7d 0a 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54   }..  rc = moveT
170ac 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 69  oRoot(pCur);.  i
170ad 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
170ae 20 29 7b 0a 20 20 20 20 69 66 28 20 43 55 52 53   ){.    if( CURS
170af 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72  OR_INVALID==pCur
170b0 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20  ->eState ){.    
170b1 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
170b2 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
170b3 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b  ge]->nCell==0 );
170b4 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31  .      *pRes = 1
170b5 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
170b6 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
170b7 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
170b8 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 20 20 2a  VALID );.      *
170b9 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20  pRes = 0;.      
170ba 72 63 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74  rc = moveToRight
170bb 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20  most(pCur);.    
170bc 20 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d    pCur->atLast =
170bd 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f   rc==SQLITE_OK ?
170be 31 3a 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  1:0;.    }.  }. 
170bf 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
170c0 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f  * Move the curso
170c1 72 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69  r so that it poi
170c2 6e 74 73 20 74 6f 20 61 6e 20 65 6e 74 72 79 20  nts to an entry 
170c3 6e 65 61 72 20 74 68 65 20 6b 65 79 20 0a 2a 2a  near the key .**
170c4 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 49   specified by pI
170c5 64 78 4b 65 79 20 6f 72 20 69 6e 74 4b 65 79 2e  dxKey or intKey.
170c6 20 20 20 52 65 74 75 72 6e 20 61 20 73 75 63 63     Return a succ
170c7 65 73 73 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  ess code..**.** 
170c8 46 6f 72 20 49 4e 54 4b 45 59 20 74 61 62 6c 65  For INTKEY table
170c9 73 2c 20 74 68 65 20 69 6e 74 4b 65 79 20 70 61  s, the intKey pa
170ca 72 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 2e  rameter is used.
170cb 20 20 70 49 64 78 4b 65 79 20 0a 2a 2a 20 6d 75    pIdxKey .** mu
170cc 73 74 20 62 65 20 4e 55 4c 4c 2e 20 20 46 6f 72  st be NULL.  For
170cd 20 69 6e 64 65 78 20 74 61 62 6c 65 73 2c 20 70   index tables, p
170ce 49 64 78 4b 65 79 20 69 73 20 75 73 65 64 20 61  IdxKey is used a
170cf 6e 64 20 69 6e 74 4b 65 79 0a 2a 2a 20 69 73 20  nd intKey.** is 
170d0 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  ignored..**.** I
170d1 66 20 61 6e 20 65 78 61 63 74 20 6d 61 74 63 68  f an exact match
170d2 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 74   is not found, t
170d3 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69  hen the cursor i
170d4 73 20 61 6c 77 61 79 73 0a 2a 2a 20 6c 65 66 74  s always.** left
170d5 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 6c   pointing at a l
170d6 65 61 66 20 70 61 67 65 20 77 68 69 63 68 20 77  eaf page which w
170d7 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 6e  ould hold the en
170d8 74 72 79 20 69 66 20 69 74 0a 2a 2a 20 77 65 72  try if it.** wer
170d9 65 20 70 72 65 73 65 6e 74 2e 20 20 54 68 65 20  e present.  The 
170da 63 75 72 73 6f 72 20 6d 69 67 68 74 20 70 6f 69  cursor might poi
170db 6e 74 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 74  nt to an entry t
170dc 68 61 74 20 63 6f 6d 65 73 0a 2a 2a 20 62 65 66  hat comes.** bef
170dd 6f 72 65 20 6f 72 20 61 66 74 65 72 20 74 68 65  ore or after the
170de 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69   key..**.** An i
170df 6e 74 65 67 65 72 20 69 73 20 77 72 69 74 74 65  nteger is writte
170e0 6e 20 69 6e 74 6f 20 2a 70 52 65 73 20 77 68 69  n into *pRes whi
170e1 63 68 20 69 73 20 74 68 65 20 72 65 73 75 6c 74  ch is the result
170e2 20 6f 66 0a 2a 2a 20 63 6f 6d 70 61 72 69 6e 67   of.** comparing
170e3 20 74 68 65 20 6b 65 79 20 77 69 74 68 20 74 68   the key with th
170e4 65 20 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68  e entry to which
170e5 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 0a   the cursor is .
170e6 2a 2a 20 70 6f 69 6e 74 69 6e 67 2e 20 20 54 68  ** pointing.  Th
170e7 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 74 68 65  e meaning of the
170e8 20 69 6e 74 65 67 65 72 20 77 72 69 74 74 65 6e   integer written
170e9 20 69 6e 74 6f 0a 2a 2a 20 2a 70 52 65 73 20 69   into.** *pRes i
170ea 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  s as follows:.**
170eb 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3c 30 20  .**     *pRes<0 
170ec 20 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20       The cursor 
170ed 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67  is left pointing
170ee 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61   at an entry tha
170ef 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
170f0 20 20 20 20 20 20 69 73 20 73 6d 61 6c 6c 65 72        is smaller
170f1 20 74 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64   than intKey/pId
170f2 78 4b 65 79 20 6f 72 20 69 66 20 74 68 65 20 74  xKey or if the t
170f3 61 62 6c 65 20 69 73 20 65 6d 70 74 79 0a 2a 2a  able is empty.**
170f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
170f5 20 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72    and the cursor
170f6 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 6c 65   is therefore le
170f7 66 74 20 70 6f 69 6e 74 20 74 6f 20 6e 6f 74 68  ft point to noth
170f8 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a  ing..**.**     *
170f9 70 52 65 73 3d 3d 30 20 20 20 20 20 54 68 65 20  pRes==0     The 
170fa 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70  cursor is left p
170fb 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e  ointing at an en
170fc 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20  try that.**     
170fd 20 20 20 20 20 20 20 20 20 20 20 20 20 65 78 61               exa
170fe 63 74 6c 79 20 6d 61 74 63 68 65 73 20 69 6e 74  ctly matches int
170ff 4b 65 79 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a  Key/pIdxKey..**.
17100 2a 2a 20 20 20 20 20 2a 70 52 65 73 3e 30 20 20  **     *pRes>0  
17101 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 69      The cursor i
17102 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20  s left pointing 
17103 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74  at an entry that
17104 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
17105 20 20 20 20 20 69 73 20 6c 61 72 67 65 72 20 74       is larger t
17106 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b  han intKey/pIdxK
17107 65 79 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45  ey..**.*/.SQLITE
17108 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
17109 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55  ite3BtreeMovetoU
1710a 6e 70 61 63 6b 65 64 28 0a 20 20 42 74 43 75 72  npacked(.  BtCur
1710b 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20  sor *pCur,      
1710c 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f      /* The curso
1710d 72 20 74 6f 20 62 65 20 6d 6f 76 65 64 20 2a 2f  r to be moved */
1710e 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  .  UnpackedRecor
1710f 64 20 2a 70 49 64 78 4b 65 79 2c 20 2f 2a 20 55  d *pIdxKey, /* U
17110 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65  npacked index ke
17111 79 20 2a 2f 0a 20 20 69 36 34 20 69 6e 74 4b 65  y */.  i64 intKe
17112 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y,              
17113 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 6b 65 79  /* The table key
17114 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61 73 52 69   */.  int biasRi
17115 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ght,           /
17116 2a 20 49 66 20 74 72 75 65 2c 20 62 69 61 73 20  * If true, bias 
17117 74 68 65 20 73 65 61 72 63 68 20 74 6f 20 74 68  the search to th
17118 65 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20  e high end */.  
17119 69 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 20  int *pRes       
1711a 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
1711b 65 20 73 65 61 72 63 68 20 72 65 73 75 6c 74 73  e search results
1711c 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   here */.){.  in
1711d 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
1711e 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
1711f 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73  x(pCur) );.  ass
17120 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
17121 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42  ex_held(pCur->pB
17122 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  tree->db->mutex)
17123 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 52   );.  assert( pR
17124 65 73 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  es );.  assert( 
17125 28 70 49 64 78 4b 65 79 3d 3d 30 29 3d 3d 28 70  (pIdxKey==0)==(p
17126 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30  Cur->pKeyInfo==0
17127 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  ) );..  /* If th
17128 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65  e cursor is alre
17129 61 64 79 20 70 6f 73 69 74 69 6f 6e 65 64 20 61  ady positioned a
1712a 74 20 74 68 65 20 70 6f 69 6e 74 20 77 65 20 61  t the point we a
1712b 72 65 20 74 72 79 69 6e 67 0a 20 20 2a 2a 20 74  re trying.  ** t
1712c 6f 20 6d 6f 76 65 20 74 6f 2c 20 74 68 65 6e 20  o move to, then 
1712d 6a 75 73 74 20 72 65 74 75 72 6e 20 77 69 74 68  just return with
1712e 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f  out doing any wo
1712f 72 6b 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 72  rk */.  if( pCur
17130 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
17131 5f 56 41 4c 49 44 20 26 26 20 70 43 75 72 2d 3e  _VALID && pCur->
17132 76 61 6c 69 64 4e 4b 65 79 20 0a 20 20 20 26 26  validNKey .   &&
17133 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d   pCur->apPage[0]
17134 2d 3e 69 6e 74 4b 65 79 20 0a 20 20 29 7b 0a 20  ->intKey .  ){. 
17135 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66     if( pCur->inf
17136 6f 2e 6e 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 29  o.nKey==intKey )
17137 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20  {.      *pRes = 
17138 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  0;.      return 
17139 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
1713a 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61  .    if( pCur->a
1713b 74 4c 61 73 74 20 26 26 20 70 43 75 72 2d 3e 69  tLast && pCur->i
1713c 6e 66 6f 2e 6e 4b 65 79 3c 69 6e 74 4b 65 79 20  nfo.nKey<intKey 
1713d 29 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d  ){.      *pRes =
1713e 20 2d 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72   -1;.      retur
1713f 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
17140 20 7d 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 6d   }.  }..  rc = m
17141 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b  oveToRoot(pCur);
17142 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20  .  if( rc ){.   
17143 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
17144 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
17145 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
17146 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ge] );.  assert(
17147 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
17148 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 69 73 49 6e  ur->iPage]->isIn
17149 69 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  it );.  assert( 
1714a 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
1714b 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c  r->iPage]->nCell
1714c 3e 30 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61  >0 || pCur->eSta
1714d 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c  te==CURSOR_INVAL
1714e 49 44 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72  ID );.  if( pCur
1714f 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
17150 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20  _INVALID ){.    
17151 2a 70 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20  *pRes = -1;.    
17152 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70  assert( pCur->ap
17153 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
17154 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20  ]->nCell==0 );. 
17155 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
17156 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  _OK;.  }.  asser
17157 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  t( pCur->apPage[
17158 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 70 49  0]->intKey || pI
17159 64 78 4b 65 79 20 29 3b 0a 20 20 66 6f 72 28 3b  dxKey );.  for(;
1715a 3b 29 7b 0a 20 20 20 20 69 6e 74 20 6c 77 72 2c  ;){.    int lwr,
1715b 20 75 70 72 3b 0a 20 20 20 20 50 67 6e 6f 20 63   upr;.    Pgno c
1715c 68 6c 64 50 67 3b 0a 20 20 20 20 4d 65 6d 50 61  hldPg;.    MemPa
1715d 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72  ge *pPage = pCur
1715e 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
1715f 50 61 67 65 5d 3b 0a 20 20 20 20 69 6e 74 20 63  Page];.    int c
17160 3b 0a 0a 20 20 20 20 2f 2a 20 70 50 61 67 65 2d  ;..    /* pPage-
17161 3e 6e 43 65 6c 6c 20 6d 75 73 74 20 62 65 20 67  >nCell must be g
17162 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f  reater than zero
17163 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65  . If this is the
17164 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a   root-page.    *
17165 2a 20 74 68 65 20 63 75 72 73 6f 72 20 77 6f 75  * the cursor wou
17166 6c 64 20 68 61 76 65 20 62 65 65 6e 20 49 4e 56  ld have been INV
17167 41 4c 49 44 20 61 62 6f 76 65 20 61 6e 64 20 74  ALID above and t
17168 68 69 73 20 66 6f 72 28 3b 3b 29 20 6c 6f 6f 70  his for(;;) loop
17169 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 72 75 6e 2e  .    ** not run.
1716a 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20   If this is not 
1716b 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 2c 20 74  the root-page, t
1716c 68 65 6e 20 74 68 65 20 6d 6f 76 65 54 6f 43 68  hen the moveToCh
1716d 69 6c 64 28 29 20 72 6f 75 74 69 6e 65 0a 20 20  ild() routine.  
1716e 20 20 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65 20    ** would have 
1716f 61 6c 72 65 61 64 79 20 64 65 74 65 63 74 65 64  already detected
17170 20 64 62 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20   db corruption. 
17171 53 69 6d 69 6c 61 72 6c 79 2c 20 70 50 61 67 65  Similarly, pPage
17172 20 6d 75 73 74 0a 20 20 20 20 2a 2a 20 62 65 20   must.    ** be 
17173 74 68 65 20 72 69 67 68 74 20 6b 69 6e 64 20 28  the right kind (
17174 69 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 29 20  index or table) 
17175 6f 66 20 62 2d 74 72 65 65 20 70 61 67 65 2e 20  of b-tree page. 
17176 4f 74 68 65 72 77 69 73 65 0a 20 20 20 20 2a 2a  Otherwise.    **
17177 20 61 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 29   a moveToChild()
17178 20 6f 72 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 29   or moveToRoot()
17179 20 63 61 6c 6c 20 77 6f 75 6c 64 20 68 61 76 65   call would have
1717a 20 64 65 74 65 63 74 65 64 20 63 6f 72 72 75 70   detected corrup
1717b 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 61 73  tion.  */.    as
1717c 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65  sert( pPage->nCe
1717d 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65  ll>0 );.    asse
1717e 72 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65  rt( pPage->intKe
1717f 79 3d 3d 28 70 49 64 78 4b 65 79 3d 3d 30 29 20  y==(pIdxKey==0) 
17180 29 3b 0a 20 20 20 20 6c 77 72 20 3d 20 30 3b 0a  );.    lwr = 0;.
17181 20 20 20 20 75 70 72 20 3d 20 70 50 61 67 65 2d      upr = pPage-
17182 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 69 66  >nCell-1;.    if
17183 28 20 62 69 61 73 52 69 67 68 74 20 29 7b 0a 20  ( biasRight ){. 
17184 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78       pCur->aiIdx
17185 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20  [pCur->iPage] = 
17186 28 75 31 36 29 75 70 72 3b 0a 20 20 20 20 7d 65  (u16)upr;.    }e
17187 6c 73 65 7b 0a 20 20 20 20 20 20 70 43 75 72 2d  lse{.      pCur-
17188 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
17189 67 65 5d 20 3d 20 28 75 31 36 29 28 28 75 70 72  ge] = (u16)((upr
1718a 2b 6c 77 72 29 2f 32 29 3b 0a 20 20 20 20 7d 0a  +lwr)/2);.    }.
1718b 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20      for(;;){.   
1718c 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75     int idx = pCu
1718d 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
1718e 50 61 67 65 5d 3b 20 2f 2a 20 49 6e 64 65 78 20  Page]; /* Index 
1718f 6f 66 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20  of current cell 
17190 69 6e 20 70 50 61 67 65 20 2a 2f 0a 20 20 20 20  in pPage */.    
17191 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20    u8 *pCell;    
17192 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17193 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
17194 20 74 6f 20 63 75 72 72 65 6e 74 20 63 65 6c 6c   to current cell
17195 20 69 6e 20 70 50 61 67 65 20 2a 2f 0a 0a 20 20   in pPage */..  
17196 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e      pCur->info.n
17197 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 20 20  Size = 0;.      
17198 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c  pCell = findCell
17199 28 70 50 61 67 65 2c 20 69 64 78 29 20 2b 20 70  (pPage, idx) + p
1719a 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69  Page->childPtrSi
1719b 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50  ze;.      if( pP
1719c 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20  age->intKey ){. 
1719d 20 20 20 20 20 20 20 69 36 34 20 6e 43 65 6c 6c         i64 nCell
1719e 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 69 66 28  Key;.        if(
1719f 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20   pPage->hasData 
171a0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33 32  ){.          u32
171a1 20 64 75 6d 6d 79 3b 0a 20 20 20 20 20 20 20 20   dummy;.        
171a2 20 20 70 43 65 6c 6c 20 2b 3d 20 67 65 74 56 61    pCell += getVa
171a3 72 69 6e 74 33 32 28 70 43 65 6c 6c 2c 20 64 75  rint32(pCell, du
171a4 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  mmy);.        }.
171a5 20 20 20 20 20 20 20 20 67 65 74 56 61 72 69 6e          getVarin
171a6 74 28 70 43 65 6c 6c 2c 20 28 75 36 34 2a 29 26  t(pCell, (u64*)&
171a7 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20  nCellKey);.     
171a8 20 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3d     if( nCellKey=
171a9 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20  =intKey ){.     
171aa 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20 20 20       c = 0;.    
171ab 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 43      }else if( nC
171ac 65 6c 6c 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b  ellKey<intKey ){
171ad 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 2d  .          c = -
171ae 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  1;.        }else
171af 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
171b0 72 74 28 20 6e 43 65 6c 6c 4b 65 79 3e 69 6e 74  rt( nCellKey>int
171b1 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 20 20 20  Key );.         
171b2 20 63 20 3d 20 2b 31 3b 0a 20 20 20 20 20 20 20   c = +1;.       
171b3 20 7d 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d   }.        pCur-
171b4 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 0a  >validNKey = 1;.
171b5 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e          pCur->in
171b6 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b  fo.nKey = nCellK
171b7 65 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ey;.      }else{
171b8 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
171b9 6d 61 78 69 6d 75 6d 20 73 75 70 70 6f 72 74 65  maximum supporte
171ba 64 20 70 61 67 65 2d 73 69 7a 65 20 69 73 20 33  d page-size is 3
171bb 32 37 36 38 20 62 79 74 65 73 2e 20 54 68 69 73  2768 bytes. This
171bc 20 6d 65 61 6e 73 20 74 68 61 74 0a 20 20 20 20   means that.    
171bd 20 20 20 20 2a 2a 20 74 68 65 20 6d 61 78 69 6d      ** the maxim
171be 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 63  um number of rec
171bf 6f 72 64 20 62 79 74 65 73 20 73 74 6f 72 65 64  ord bytes stored
171c0 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 42 2d 54   on an index B-T
171c1 72 65 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70  ree.        ** p
171c2 61 67 65 20 69 73 20 61 74 20 6d 6f 73 74 20 38  age is at most 8
171c3 31 39 38 20 62 79 74 65 73 2c 20 77 68 69 63 68  198 bytes, which
171c4 20 6d 61 79 20 62 65 20 73 74 6f 72 65 64 20 61   may be stored a
171c5 73 20 61 20 32 2d 62 79 74 65 0a 20 20 20 20 20  s a 2-byte.     
171c6 20 20 20 2a 2a 20 76 61 72 69 6e 74 2e 20 54 68     ** varint. Th
171c7 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  is information i
171c8 73 20 75 73 65 64 20 74 6f 20 61 74 74 65 6d 70  s used to attemp
171c9 74 20 74 6f 20 61 76 6f 69 64 20 70 61 72 73 69  t to avoid parsi
171ca 6e 67 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  ng .        ** t
171cb 68 65 20 65 6e 74 69 72 65 20 63 65 6c 6c 20 62  he entire cell b
171cc 79 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 74  y checking for t
171cd 68 65 20 63 61 73 65 73 20 77 68 65 72 65 20 74  he cases where t
171ce 68 65 20 72 65 63 6f 72 64 20 69 73 20 0a 20 20  he record is .  
171cf 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 20        ** stored 
171d0 65 6e 74 69 72 65 6c 79 20 77 69 74 68 69 6e 20  entirely within 
171d1 74 68 65 20 62 2d 74 72 65 65 20 70 61 67 65 20  the b-tree page 
171d2 62 79 20 69 6e 73 70 65 63 74 69 6e 67 20 74 68  by inspecting th
171d3 65 20 66 69 72 73 74 20 0a 20 20 20 20 20 20 20  e first .       
171d4 20 2a 2a 20 32 20 62 79 74 65 73 20 6f 66 20 74   ** 2 bytes of t
171d5 68 65 20 63 65 6c 6c 2e 0a 20 20 20 20 20 20 20  he cell..       
171d6 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20   */.        int 
171d7 6e 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 5b 30 5d  nCell = pCell[0]
171d8 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 28  ;.        if( !(
171d9 6e 43 65 6c 6c 20 26 20 30 78 38 30 29 20 26 26  nCell & 0x80) &&
171da 20 6e 43 65 6c 6c 3c 3d 70 50 61 67 65 2d 3e 6d   nCell<=pPage->m
171db 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20  axLocal ){.     
171dc 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61       /* This bra
171dd 6e 63 68 20 72 75 6e 73 20 69 66 20 74 68 65 20  nch runs if the 
171de 72 65 63 6f 72 64 2d 73 69 7a 65 20 66 69 65 6c  record-size fiel
171df 64 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 73  d of the cell is
171e0 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20   a.          ** 
171e1 73 69 6e 67 6c 65 20 62 79 74 65 20 76 61 72 69  single byte vari
171e2 6e 74 20 61 6e 64 20 74 68 65 20 72 65 63 6f 72  nt and the recor
171e3 64 20 66 69 74 73 20 65 6e 74 69 72 65 6c 79 20  d fits entirely 
171e4 6f 6e 20 74 68 65 20 6d 61 69 6e 0a 20 20 20 20  on the main.    
171e5 20 20 20 20 20 20 2a 2a 20 62 2d 74 72 65 65 20        ** b-tree 
171e6 70 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20  page.  */.      
171e7 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56      c = sqlite3V
171e8 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65  dbeRecordCompare
171e9 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26  (nCell, (void*)&
171ea 70 43 65 6c 6c 5b 31 5d 2c 20 70 49 64 78 4b 65  pCell[1], pIdxKe
171eb 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  y);.        }els
171ec 65 20 69 66 28 20 21 28 70 43 65 6c 6c 5b 31 5d  e if( !(pCell[1]
171ed 20 26 20 30 78 38 30 29 20 0a 20 20 20 20 20 20   & 0x80) .      
171ee 20 20 20 20 26 26 20 28 6e 43 65 6c 6c 20 3d 20      && (nCell = 
171ef 28 28 6e 43 65 6c 6c 26 30 78 37 66 29 3c 3c 37  ((nCell&0x7f)<<7
171f0 29 20 2b 20 70 43 65 6c 6c 5b 31 5d 29 3c 3d 70  ) + pCell[1])<=p
171f1 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 0a 20  Page->maxLocal. 
171f2 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20         ){.      
171f3 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 6f 72      /* The recor
171f4 64 2d 73 69 7a 65 20 66 69 65 6c 64 20 69 73 20  d-size field is 
171f5 61 20 32 20 62 79 74 65 20 76 61 72 69 6e 74 20  a 2 byte varint 
171f6 61 6e 64 20 74 68 65 20 72 65 63 6f 72 64 20 0a  and the record .
171f7 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 69 74            ** fit
171f8 73 20 65 6e 74 69 72 65 6c 79 20 6f 6e 20 74 68  s entirely on th
171f9 65 20 6d 61 69 6e 20 62 2d 74 72 65 65 20 70 61  e main b-tree pa
171fa 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20  ge.  */.        
171fb 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62    c = sqlite3Vdb
171fc 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 6e  eRecordCompare(n
171fd 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 70 43  Cell, (void*)&pC
171fe 65 6c 6c 5b 32 5d 2c 20 70 49 64 78 4b 65 79 29  ell[2], pIdxKey)
171ff 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
17200 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  .          /* Th
17201 65 20 72 65 63 6f 72 64 20 66 6c 6f 77 73 20 6f  e record flows o
17202 76 65 72 20 6f 6e 74 6f 20 6f 6e 65 20 6f 72 20  ver onto one or 
17203 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61  more overflow pa
17204 67 65 73 2e 20 49 6e 0a 20 20 20 20 20 20 20 20  ges. In.        
17205 20 20 2a 2a 20 74 68 69 73 20 63 61 73 65 20 74    ** this case t
17206 68 65 20 77 68 6f 6c 65 20 63 65 6c 6c 20 6e 65  he whole cell ne
17207 65 64 73 20 74 6f 20 62 65 20 70 61 72 73 65 64  eds to be parsed
17208 2c 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63  , a buffer alloc
17209 61 74 65 64 0a 20 20 20 20 20 20 20 20 20 20 2a  ated.          *
1720a 2a 20 61 6e 64 20 61 63 63 65 73 73 50 61 79 6c  * and accessPayl
1720b 6f 61 64 28 29 20 75 73 65 64 20 74 6f 20 72 65  oad() used to re
1720c 74 72 69 65 76 65 20 74 68 65 20 72 65 63 6f 72  trieve the recor
1720d 64 20 69 6e 74 6f 20 74 68 65 0a 20 20 20 20 20  d into the.     
1720e 20 20 20 20 20 2a 2a 20 62 75 66 66 65 72 20 62       ** buffer b
1720f 65 66 6f 72 65 20 56 64 62 65 52 65 63 6f 72 64  efore VdbeRecord
17210 43 6f 6d 70 61 72 65 28 29 20 63 61 6e 20 62 65  Compare() can be
17211 20 63 61 6c 6c 65 64 2e 20 2a 2f 0a 20 20 20 20   called. */.    
17212 20 20 20 20 20 20 76 6f 69 64 20 2a 70 43 65 6c        void *pCel
17213 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20  lKey;.          
17214 75 38 20 2a 20 63 6f 6e 73 74 20 70 43 65 6c 6c  u8 * const pCell
17215 42 6f 64 79 20 3d 20 70 43 65 6c 6c 20 2d 20 70  Body = pCell - p
17216 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69  Page->childPtrSi
17217 7a 65 3b 0a 20 20 20 20 20 20 20 20 20 20 62 74  ze;.          bt
17218 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28  reeParseCellPtr(
17219 70 50 61 67 65 2c 20 70 43 65 6c 6c 42 6f 64 79  pPage, pCellBody
1721a 2c 20 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a  , &pCur->info);.
1721b 20 20 20 20 20 20 20 20 20 20 6e 43 65 6c 6c 20            nCell 
1721c 3d 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66  = (int)pCur->inf
1721d 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 20 20 20 20  o.nKey;.        
1721e 20 20 70 43 65 6c 6c 4b 65 79 20 3d 20 73 71 6c    pCellKey = sql
1721f 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 43 65 6c  ite3Malloc( nCel
17220 6c 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  l );.          i
17221 66 28 20 70 43 65 6c 6c 4b 65 79 3d 3d 30 20 29  f( pCellKey==0 )
17222 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  {.            rc
17223 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
17224 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74  .            got
17225 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b  o moveto_finish;
17226 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
17227 20 20 20 20 20 20 20 72 63 20 3d 20 61 63 63 65         rc = acce
17228 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20  ssPayload(pCur, 
17229 30 2c 20 6e 43 65 6c 6c 2c 20 28 75 6e 73 69 67  0, nCell, (unsig
1722a 6e 65 64 20 63 68 61 72 2a 29 70 43 65 6c 6c 4b  ned char*)pCellK
1722b 65 79 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  ey, 0);.        
1722c 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
1722d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1722e 66 72 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a  free(pCellKey);.
1722f 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f              goto
17230 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a   moveto_finish;.
17231 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
17232 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65        c = sqlite
17233 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61  3VdbeRecordCompa
17234 72 65 28 6e 43 65 6c 6c 2c 20 70 43 65 6c 6c 4b  re(nCell, pCellK
17235 65 79 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20  ey, pIdxKey);.  
17236 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
17237 66 72 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a  free(pCellKey);.
17238 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
17239 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30  }.      if( c==0
1723a 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
1723b 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26  pPage->intKey &&
1723c 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
1723d 0a 20 20 20 20 20 20 20 20 20 20 6c 77 72 20 3d  .          lwr =
1723e 20 69 64 78 3b 0a 20 20 20 20 20 20 20 20 20 20   idx;.          
1723f 75 70 72 20 3d 20 6c 77 72 20 2d 20 31 3b 0a 20  upr = lwr - 1;. 
17240 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
17241 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
17242 20 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d           *pRes =
17243 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63   0;.          rc
17244 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
17245 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76          goto mov
17246 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20  eto_finish;.    
17247 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
17248 20 20 20 20 69 66 28 20 63 3c 30 20 29 7b 0a 20      if( c<0 ){. 
17249 20 20 20 20 20 20 20 6c 77 72 20 3d 20 69 64 78         lwr = idx
1724a 2b 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  +1;.      }else{
1724b 0a 20 20 20 20 20 20 20 20 75 70 72 20 3d 20 69  .        upr = i
1724c 64 78 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  dx-1;.      }.  
1724d 20 20 20 20 69 66 28 20 6c 77 72 3e 75 70 72 20      if( lwr>upr 
1724e 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ){.        break
1724f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
17250 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
17251 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29  ->iPage] = (u16)
17252 28 28 6c 77 72 2b 75 70 72 29 2f 32 29 3b 0a 20  ((lwr+upr)/2);. 
17253 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
17254 20 6c 77 72 3d 3d 75 70 72 2b 31 20 29 3b 0a 20   lwr==upr+1 );. 
17255 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
17256 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20  ->isInit );.    
17257 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20  if( pPage->leaf 
17258 29 7b 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20  ){.      chldPg 
17259 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  = 0;.    }else i
1725a 66 28 20 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e  f( lwr>=pPage->n
1725b 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 63 68  Cell ){.      ch
1725c 6c 64 50 67 20 3d 20 67 65 74 34 62 79 74 65 28  ldPg = get4byte(
1725d 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50  &pPage->aData[pP
1725e 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38  age->hdrOffset+8
1725f 5d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ]);.    }else{. 
17260 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20 67 65       chldPg = ge
17261 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28  t4byte(findCell(
17262 70 50 61 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20  pPage, lwr));.  
17263 20 20 7d 0a 20 20 20 20 69 66 28 20 63 68 6c 64    }.    if( chld
17264 50 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61  Pg==0 ){.      a
17265 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49  ssert( pCur->aiI
17266 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c  dx[pCur->iPage]<
17267 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
17268 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c  r->iPage]->nCell
17269 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20   );.      *pRes 
1726a 3d 20 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  = c;.      rc = 
1726b 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
1726c 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e   goto moveto_fin
1726d 69 73 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ish;.    }.    p
1726e 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
1726f 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 6c  >iPage] = (u16)l
17270 77 72 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e  wr;.    pCur->in
17271 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20  fo.nSize = 0;.  
17272 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65    pCur->validNKe
17273 79 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20  y = 0;.    rc = 
17274 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72  moveToChild(pCur
17275 2c 20 63 68 6c 64 50 67 29 3b 0a 20 20 20 20 69  , chldPg);.    i
17276 66 28 20 72 63 20 29 20 67 6f 74 6f 20 6d 6f 76  f( rc ) goto mov
17277 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 7d 0a  eto_finish;.  }.
17278 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3a 0a 20  moveto_finish:. 
17279 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a   return rc;.}...
1727a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55  /*.** Return TRU
1727b 45 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20  E if the cursor 
1727c 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20  is not pointing 
1727d 61 74 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74  at an entry of t
1727e 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  he table..**.** 
1727f 54 52 55 45 20 77 69 6c 6c 20 62 65 20 72 65 74  TRUE will be ret
17280 75 72 6e 65 64 20 61 66 74 65 72 20 61 20 63 61  urned after a ca
17281 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72  ll to sqlite3Btr
17282 65 65 4e 65 78 74 28 29 20 6d 6f 76 65 73 0a 2a  eeNext() moves.*
17283 2a 20 70 61 73 74 20 74 68 65 20 6c 61 73 74 20  * past the last 
17284 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 62  entry in the tab
17285 6c 65 20 6f 72 20 73 71 6c 69 74 65 33 42 74 72  le or sqlite3Btr
17286 65 65 50 72 65 76 28 29 20 6d 6f 76 65 73 20 70  eePrev() moves p
17287 61 73 74 0a 2a 2a 20 74 68 65 20 66 69 72 73 74  ast.** the first
17288 20 65 6e 74 72 79 2e 20 20 54 52 55 45 20 69 73   entry.  TRUE is
17289 20 61 6c 73 6f 20 72 65 74 75 72 6e 65 64 20 69   also returned i
1728a 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65  f the table is e
1728b 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  mpty..*/.SQLITE_
1728c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1728d 74 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 75  te3BtreeEof(BtCu
1728e 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 2f  rsor *pCur){.  /
1728f 2a 20 54 4f 44 4f 3a 20 57 68 61 74 20 69 66 20  * TODO: What if 
17290 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 69 6e  the cursor is in
17291 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53   CURSOR_REQUIRES
17292 45 45 4b 20 62 75 74 20 61 6c 6c 20 74 61 62 6c  EEK but all tabl
17293 65 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20 68  e entries.  ** h
17294 61 76 65 20 62 65 65 6e 20 64 65 6c 65 74 65 64  ave been deleted
17295 3f 20 54 68 69 73 20 41 50 49 20 77 69 6c 6c 20  ? This API will 
17296 6e 65 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74  need to change t
17297 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  o return an erro
17298 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 73 20 77  r code.  ** as w
17299 65 6c 6c 20 61 73 20 74 68 65 20 62 6f 6f 6c 65  ell as the boole
1729a 61 6e 20 72 65 73 75 6c 74 20 76 61 6c 75 65 2e  an result value.
1729b 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28  .  */.  return (
1729c 43 55 52 53 4f 52 5f 56 41 4c 49 44 21 3d 70 43  CURSOR_VALID!=pC
1729d 75 72 2d 3e 65 53 74 61 74 65 29 3b 0a 7d 0a 0a  ur->eState);.}..
1729e 2f 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74 68  /*.** Advance th
1729f 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20  e cursor to the 
172a0 6e 65 78 74 20 65 6e 74 72 79 20 69 6e 20 74 68  next entry in th
172a1 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 0a  e database.  If.
172a2 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 74 68  ** successful th
172a3 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 2e 20  en set *pRes=0. 
172a4 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 0a 2a   If the cursor.*
172a5 2a 20 77 61 73 20 61 6c 72 65 61 64 79 20 70 6f  * was already po
172a6 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61  inting to the la
172a7 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  st entry in the 
172a8 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a  database before.
172a9 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ** this routine 
172aa 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e  was called, then
172ab 20 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a 2f   set *pRes=1..*/
172ac 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
172ad 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
172ae 4e 65 78 74 28 42 74 43 75 72 73 6f 72 20 2a 70  Next(BtCursor *p
172af 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b  Cur, int *pRes){
172b0 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74  .  int rc;.  int
172b1 20 69 64 78 3b 0a 20 20 4d 65 6d 50 61 67 65 20   idx;.  MemPage 
172b2 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72  *pPage;..  asser
172b3 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
172b4 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72  tex(pCur) );.  r
172b5 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f  c = restoreCurso
172b6 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b  rPosition(pCur);
172b7 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
172b8 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
172b9 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73  rn rc;.  }.  ass
172ba 65 72 74 28 20 70 52 65 73 21 3d 30 20 29 3b 0a  ert( pRes!=0 );.
172bb 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56    if( CURSOR_INV
172bc 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61  ALID==pCur->eSta
172bd 74 65 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20  te ){.    *pRes 
172be 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 1;.    return 
172bf 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
172c0 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e   if( pCur->skipN
172c1 65 78 74 3e 30 20 29 7b 0a 20 20 20 20 70 43 75  ext>0 ){.    pCu
172c2 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b  r->skipNext = 0;
172c3 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a  .    *pRes = 0;.
172c4 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
172c5 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 72  E_OK;.  }.  pCur
172c6 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a  ->skipNext = 0;.
172c7 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d  .  pPage = pCur-
172c8 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
172c9 61 67 65 5d 3b 0a 20 20 69 64 78 20 3d 20 2b 2b  age];.  idx = ++
172ca 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
172cb 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65  ->iPage];.  asse
172cc 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69  rt( pPage->isIni
172cd 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  t );.  assert( i
172ce 64 78 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  dx<=pPage->nCell
172cf 20 29 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 6e 66   );..  pCur->inf
172d0 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70  o.nSize = 0;.  p
172d1 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d  Cur->validNKey =
172d2 20 30 3b 0a 20 20 69 66 28 20 69 64 78 3e 3d 70   0;.  if( idx>=p
172d3 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20  Page->nCell ){. 
172d4 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c     if( !pPage->l
172d5 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20  eaf ){.      rc 
172d6 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43  = moveToChild(pC
172d7 75 72 2c 20 67 65 74 34 62 79 74 65 28 26 70 50  ur, get4byte(&pP
172d8 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65  age->aData[pPage
172d9 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 29  ->hdrOffset+8]))
172da 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
172db 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
172dc 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66    rc = moveToLef
172dd 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20  tmost(pCur);.   
172de 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20     *pRes = 0;.  
172df 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
172e0 20 20 20 7d 0a 20 20 20 20 64 6f 7b 0a 20 20 20     }.    do{.   
172e1 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61     if( pCur->iPa
172e2 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ge==0 ){.       
172e3 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20   *pRes = 1;.    
172e4 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65      pCur->eState
172e5 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49   = CURSOR_INVALI
172e6 44 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  D;.        retur
172e7 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
172e8 20 20 20 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54     }.      moveT
172e9 6f 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20  oParent(pCur);. 
172ea 20 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 75       pPage = pCu
172eb 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
172ec 69 50 61 67 65 5d 3b 0a 20 20 20 20 7d 77 68 69  iPage];.    }whi
172ed 6c 65 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  le( pCur->aiIdx[
172ee 70 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 50  pCur->iPage]>=pP
172ef 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20  age->nCell );.  
172f0 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20    *pRes = 0;.   
172f1 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b   if( pPage->intK
172f2 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  ey ){.      rc =
172f3 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78   sqlite3BtreeNex
172f4 74 28 70 43 75 72 2c 20 70 52 65 73 29 3b 0a 20  t(pCur, pRes);. 
172f5 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
172f6 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
172f7 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
172f8 20 72 63 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73   rc;.  }.  *pRes
172f9 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 50 61 67   = 0;.  if( pPag
172fa 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 72  e->leaf ){.    r
172fb 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
172fc 0a 20 20 7d 0a 20 20 72 63 20 3d 20 6d 6f 76 65  .  }.  rc = move
172fd 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29  ToLeftmost(pCur)
172fe 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
172ff 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 70 20 74 68  .../*.** Step th
17300 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20  e cursor to the 
17301 62 61 63 6b 20 74 6f 20 74 68 65 20 70 72 65 76  back to the prev
17302 69 6f 75 73 20 65 6e 74 72 79 20 69 6e 20 74 68  ious entry in th
17303 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 0a  e database.  If.
17304 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 74 68  ** successful th
17305 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 2e 20  en set *pRes=0. 
17306 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 0a 2a   If the cursor.*
17307 2a 20 77 61 73 20 61 6c 72 65 61 64 79 20 70 6f  * was already po
17308 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 66 69  inting to the fi
17309 72 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65  rst entry in the
1730a 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65   database before
1730b 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
1730c 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 68 65   was called, the
1730d 6e 20 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a  n set *pRes=1..*
1730e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1730f 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
17310 65 50 72 65 76 69 6f 75 73 28 42 74 43 75 72 73  ePrevious(BtCurs
17311 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70  or *pCur, int *p
17312 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Res){.  int rc;.
17313 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
17314 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ;..  assert( cur
17315 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
17316 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65  ur) );.  rc = re
17317 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74  storeCursorPosit
17318 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28  ion(pCur);.  if(
17319 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1731a 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
1731b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 61 74 4c  .  }.  pCur->atL
1731c 61 73 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 43  ast = 0;.  if( C
1731d 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70  URSOR_INVALID==p
1731e 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20  Cur->eState ){. 
1731f 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20     *pRes = 1;.  
17320 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
17321 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43  OK;.  }.  if( pC
17322 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3c 30 20 29  ur->skipNext<0 )
17323 7b 0a 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 70  {.    pCur->skip
17324 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 2a 70  Next = 0;.    *p
17325 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 74  Res = 0;.    ret
17326 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
17327 20 7d 0a 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e   }.  pCur->skipN
17328 65 78 74 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67  ext = 0;..  pPag
17329 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65  e = pCur->apPage
1732a 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20  [pCur->iPage];. 
1732b 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
1732c 69 73 49 6e 69 74 20 29 3b 0a 20 20 69 66 28 20  isInit );.  if( 
1732d 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
1732e 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43      int idx = pC
1732f 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
17330 69 50 61 67 65 5d 3b 0a 20 20 20 20 72 63 20 3d  iPage];.    rc =
17331 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75   moveToChild(pCu
17332 72 2c 20 67 65 74 34 62 79 74 65 28 66 69 6e 64  r, get4byte(find
17333 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29  Cell(pPage, idx)
17334 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  ));.    if( rc )
17335 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
17336 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  c;.    }.    rc 
17337 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73  = moveToRightmos
17338 74 28 70 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65  t(pCur);.  }else
17339 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 43 75  {.    while( pCu
1733a 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
1733b 50 61 67 65 5d 3d 3d 30 20 29 7b 0a 20 20 20 20  Page]==0 ){.    
1733c 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67    if( pCur->iPag
1733d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  e==0 ){.        
1733e 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43  pCur->eState = C
1733f 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20  URSOR_INVALID;. 
17340 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31         *pRes = 1
17341 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
17342 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
17343 20 20 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54 6f    }.      moveTo
17344 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20  Parent(pCur);.  
17345 20 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e    }.    pCur->in
17346 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20  fo.nSize = 0;.  
17347 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65    pCur->validNKe
17348 79 20 3d 20 30 3b 0a 0a 20 20 20 20 70 43 75 72  y = 0;..    pCur
17349 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
1734a 61 67 65 5d 2d 2d 3b 0a 20 20 20 20 70 50 61 67  age]--;.    pPag
1734b 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65  e = pCur->apPage
1734c 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20  [pCur->iPage];. 
1734d 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e     if( pPage->in
1734e 74 4b 65 79 20 26 26 20 21 70 50 61 67 65 2d 3e  tKey && !pPage->
1734f 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63  leaf ){.      rc
17350 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50   = sqlite3BtreeP
17351 72 65 76 69 6f 75 73 28 70 43 75 72 2c 20 70 52  revious(pCur, pR
17352 65 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  es);.    }else{.
17353 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
17354 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  E_OK;.    }.  }.
17355 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 72    *pRes = 0;.  r
17356 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
17357 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65  ** Allocate a ne
17358 77 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20  w page from the 
17359 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
1735a 2a 0a 2a 2a 20 54 68 65 20 6e 65 77 20 70 61 67  *.** The new pag
1735b 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 64  e is marked as d
1735c 69 72 74 79 2e 20 20 28 49 6e 20 6f 74 68 65 72  irty.  (In other
1735d 20 77 6f 72 64 73 2c 20 73 71 6c 69 74 65 33 50   words, sqlite3P
1735e 61 67 65 72 57 72 69 74 65 28 29 0a 2a 2a 20 68  agerWrite().** h
1735f 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
17360 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 6e 65  called on the ne
17361 77 20 70 61 67 65 2e 29 20 20 54 68 65 20 6e 65  w page.)  The ne
17362 77 20 70 61 67 65 20 68 61 73 20 61 6c 73 6f 0a  w page has also.
17363 2a 2a 20 62 65 65 6e 20 72 65 66 65 72 65 6e 63  ** been referenc
17364 65 64 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 69  ed and the calli
17365 6e 67 20 72 6f 75 74 69 6e 65 20 69 73 20 72 65  ng routine is re
17366 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 61  sponsible for ca
17367 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33  lling.** sqlite3
17368 50 61 67 65 72 55 6e 72 65 66 28 29 20 6f 6e 20  PagerUnref() on 
17369 74 68 65 20 6e 65 77 20 70 61 67 65 20 77 68 65  the new page whe
1736a 6e 20 69 74 20 69 73 20 64 6f 6e 65 2e 0a 2a 2a  n it is done..**
1736b 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  .** SQLITE_OK is
1736c 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63   returned on suc
1736d 63 65 73 73 2e 20 20 41 6e 79 20 6f 74 68 65 72  cess.  Any other
1736e 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 6e   return value in
1736f 64 69 63 61 74 65 73 0a 2a 2a 20 61 6e 20 65 72  dicates.** an er
17370 72 6f 72 2e 20 20 2a 70 70 50 61 67 65 20 61 6e  ror.  *ppPage an
17371 64 20 2a 70 50 67 6e 6f 20 61 72 65 20 75 6e 64  d *pPgno are und
17372 65 66 69 6e 65 64 20 69 6e 20 74 68 65 20 65 76  efined in the ev
17373 65 6e 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 2e  ent of an error.
17374 0a 2a 2a 20 44 6f 20 6e 6f 74 20 69 6e 76 6f 6b  .** Do not invok
17375 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e  e sqlite3PagerUn
17376 72 65 66 28 29 20 6f 6e 20 2a 70 70 50 61 67 65  ref() on *ppPage
17377 20 69 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20   if an error is 
17378 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
17379 49 66 20 74 68 65 20 22 6e 65 61 72 62 79 22 20  If the "nearby" 
1737a 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74  parameter is not
1737b 20 30 2c 20 74 68 65 6e 20 61 20 28 66 65 65 62   0, then a (feeb
1737c 6c 65 29 20 65 66 66 6f 72 74 20 69 73 20 6d 61  le) effort is ma
1737d 64 65 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 74 65  de to .** locate
1737e 20 61 20 70 61 67 65 20 63 6c 6f 73 65 20 74 6f   a page close to
1737f 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
17380 20 22 6e 65 61 72 62 79 22 2e 20 20 54 68 69 73   "nearby".  This
17381 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20   can be used in 
17382 61 6e 0a 2a 2a 20 61 74 74 65 6d 70 74 20 74 6f  an.** attempt to
17383 20 6b 65 65 70 20 72 65 6c 61 74 65 64 20 70 61   keep related pa
17384 67 65 73 20 63 6c 6f 73 65 20 74 6f 20 65 61 63  ges close to eac
17385 68 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 64  h other in the d
17386 61 74 61 62 61 73 65 20 66 69 6c 65 2c 0a 2a 2a  atabase file,.**
17387 20 77 68 69 63 68 20 69 6e 20 74 75 72 6e 20 63   which in turn c
17388 61 6e 20 6d 61 6b 65 20 64 61 74 61 62 61 73 65  an make database
17389 20 61 63 63 65 73 73 20 66 61 73 74 65 72 2e 0a   access faster..
1738a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 65 78  **.** If the "ex
1738b 61 63 74 22 20 70 61 72 61 6d 65 74 65 72 20 69  act" parameter i
1738c 73 20 6e 6f 74 20 30 2c 20 61 6e 64 20 74 68 65  s not 0, and the
1738d 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 6e 65 61   page-number nea
1738e 72 62 79 20 65 78 69 73 74 73 20 0a 2a 2a 20 61  rby exists .** a
1738f 6e 79 77 68 65 72 65 20 6f 6e 20 74 68 65 20 66  nywhere on the f
17390 72 65 65 2d 6c 69 73 74 2c 20 74 68 65 6e 20 69  ree-list, then i
17391 74 20 69 73 20 67 75 61 72 65 6e 74 65 65 64 20  t is guarenteed 
17392 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20  to be returned. 
17393 54 68 69 73 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20  This.** is only 
17394 75 73 65 64 20 62 79 20 61 75 74 6f 2d 76 61 63  used by auto-vac
17395 75 75 6d 20 64 61 74 61 62 61 73 65 73 20 77 68  uum databases wh
17396 65 6e 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20  en allocating a 
17397 6e 65 77 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74  new table..*/.st
17398 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74  atic int allocat
17399 65 42 74 72 65 65 50 61 67 65 28 0a 20 20 42 74  eBtreePage(.  Bt
1739a 53 68 61 72 65 64 20 2a 70 42 74 2c 20 0a 20 20  Shared *pBt, .  
1739b 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65  MemPage **ppPage
1739c 2c 20 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e 6f  , .  Pgno *pPgno
1739d 2c 20 0a 20 20 50 67 6e 6f 20 6e 65 61 72 62 79  , .  Pgno nearby
1739e 2c 0a 20 20 75 38 20 65 78 61 63 74 0a 29 7b 0a  ,.  u8 exact.){.
1739f 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
173a0 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75  1;.  int rc;.  u
173a1 33 32 20 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d  32 n;     /* Num
173a2 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e 20  ber of pages on 
173a3 74 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a  the freelist */.
173a4 20 20 75 33 32 20 6b 3b 20 20 20 20 20 2f 2a 20    u32 k;     /* 
173a5 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 76 65 73  Number of leaves
173a6 20 6f 6e 20 74 68 65 20 74 72 75 6e 6b 20 6f 66   on the trunk of
173a7 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f   the freelist */
173a8 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 75  .  MemPage *pTru
173a9 6e 6b 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67  nk = 0;.  MemPag
173aa 65 20 2a 70 50 72 65 76 54 72 75 6e 6b 20 3d 20  e *pPrevTrunk = 
173ab 30 3b 0a 20 20 50 67 6e 6f 20 6d 78 50 61 67 65  0;.  Pgno mxPage
173ac 3b 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73  ;     /* Total s
173ad 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ize of the datab
173ae 61 73 65 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 61  ase file */..  a
173af 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
173b0 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
173b1 75 74 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65  utex) );.  pPage
173b2 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b  1 = pBt->pPage1;
173b3 0a 20 20 6d 78 50 61 67 65 20 3d 20 70 61 67 65  .  mxPage = page
173b4 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b  rPagecount(pBt);
173b5 0a 20 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28  .  n = get4byte(
173b6 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  &pPage1->aData[3
173b7 36 5d 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  6]);.  testcase(
173b8 20 6e 3d 3d 6d 78 50 61 67 65 2d 31 20 29 3b 0a   n==mxPage-1 );.
173b9 20 20 69 66 28 20 6e 3e 3d 6d 78 50 61 67 65 20    if( n>=mxPage 
173ba 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
173bb 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
173bc 54 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 3e 30  T;.  }.  if( n>0
173bd 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 65   ){.    /* There
173be 20 61 72 65 20 70 61 67 65 73 20 6f 6e 20 74 68   are pages on th
173bf 65 20 66 72 65 65 6c 69 73 74 2e 20 20 52 65 75  e freelist.  Reu
173c0 73 65 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20  se one of those 
173c1 70 61 67 65 73 2e 20 2a 2f 0a 20 20 20 20 50 67  pages. */.    Pg
173c2 6e 6f 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 75  no iTrunk;.    u
173c3 38 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30  8 searchList = 0
173c4 3b 20 2f 2a 20 49 66 20 74 68 65 20 66 72 65 65  ; /* If the free
173c5 2d 6c 69 73 74 20 6d 75 73 74 20 62 65 20 73 65  -list must be se
173c6 61 72 63 68 65 64 20 66 6f 72 20 27 6e 65 61 72  arched for 'near
173c7 62 79 27 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20  by' */.    .    
173c8 2f 2a 20 49 66 20 74 68 65 20 27 65 78 61 63 74  /* If the 'exact
173c9 27 20 70 61 72 61 6d 65 74 65 72 20 77 61 73 20  ' parameter was 
173ca 74 72 75 65 20 61 6e 64 20 61 20 71 75 65 72 79  true and a query
173cb 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d   of the pointer-
173cc 6d 61 70 0a 20 20 20 20 2a 2a 20 73 68 6f 77 73  map.    ** shows
173cd 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 27   that the page '
173ce 6e 65 61 72 62 79 27 20 69 73 20 73 6f 6d 65 77  nearby' is somew
173cf 68 65 72 65 20 6f 6e 20 74 68 65 20 66 72 65 65  here on the free
173d0 2d 6c 69 73 74 2c 20 74 68 65 6e 0a 20 20 20 20  -list, then.    
173d1 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 2d 6c 69  ** the entire-li
173d2 73 74 20 77 69 6c 6c 20 62 65 20 73 65 61 72 63  st will be searc
173d3 68 65 64 20 66 6f 72 20 74 68 61 74 20 70 61 67  hed for that pag
173d4 65 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65  e..    */.#ifnde
173d5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
173d6 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28  TOVACUUM.    if(
173d7 20 65 78 61 63 74 20 26 26 20 6e 65 61 72 62 79   exact && nearby
173d8 3c 3d 6d 78 50 61 67 65 20 29 7b 0a 20 20 20 20  <=mxPage ){.    
173d9 20 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 20    u8 eType;.    
173da 20 20 61 73 73 65 72 74 28 20 6e 65 61 72 62 79    assert( nearby
173db 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  >0 );.      asse
173dc 72 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  rt( pBt->autoVac
173dd 75 75 6d 20 29 3b 0a 20 20 20 20 20 20 72 63 20  uum );.      rc 
173de 3d 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c  = ptrmapGet(pBt,
173df 20 6e 65 61 72 62 79 2c 20 26 65 54 79 70 65 2c   nearby, &eType,
173e0 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72   0);.      if( r
173e1 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
173e2 20 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d       if( eType==
173e3 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20  PTRMAP_FREEPAGE 
173e4 29 7b 0a 20 20 20 20 20 20 20 20 73 65 61 72 63  ){.        searc
173e5 68 4c 69 73 74 20 3d 20 31 3b 0a 20 20 20 20 20  hList = 1;.     
173e6 20 7d 0a 20 20 20 20 20 20 2a 70 50 67 6e 6f 20   }.      *pPgno 
173e7 3d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 7d 0a  = nearby;.    }.
173e8 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44  #endif..    /* D
173e9 65 63 72 65 6d 65 6e 74 20 74 68 65 20 66 72 65  ecrement the fre
173ea 65 2d 6c 69 73 74 20 63 6f 75 6e 74 20 62 79 20  e-list count by 
173eb 31 2e 20 53 65 74 20 69 54 72 75 6e 6b 20 74 6f  1. Set iTrunk to
173ec 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
173ed 65 0a 20 20 20 20 2a 2a 20 66 69 72 73 74 20 66  e.    ** first f
173ee 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70  ree-list trunk p
173ef 61 67 65 2e 20 69 50 72 65 76 54 72 75 6e 6b 20  age. iPrevTrunk 
173f0 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 31 2e 0a  is initially 1..
173f1 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20      */.    rc = 
173f2 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
173f3 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67  e(pPage1->pDbPag
173f4 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  e);.    if( rc )
173f5 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
173f6 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 31  put4byte(&pPage1
173f7 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 6e 2d 31  ->aData[36], n-1
173f8 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63  );..    /* The c
173f9 6f 64 65 20 77 69 74 68 69 6e 20 74 68 69 73 20  ode within this 
173fa 6c 6f 6f 70 20 69 73 20 72 75 6e 20 6f 6e 6c 79  loop is run only
173fb 20 6f 6e 63 65 20 69 66 20 74 68 65 20 27 73 65   once if the 'se
173fc 61 72 63 68 4c 69 73 74 27 20 76 61 72 69 61 62  archList' variab
173fd 6c 65 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74  le.    ** is not
173fe 20 74 72 75 65 2e 20 4f 74 68 65 72 77 69 73 65   true. Otherwise
173ff 2c 20 69 74 20 72 75 6e 73 20 6f 6e 63 65 20 66  , it runs once f
17400 6f 72 20 65 61 63 68 20 74 72 75 6e 6b 2d 70 61  or each trunk-pa
17401 67 65 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a  ge on the.    **
17402 20 66 72 65 65 2d 6c 69 73 74 20 75 6e 74 69 6c   free-list until
17403 20 74 68 65 20 70 61 67 65 20 27 6e 65 61 72 62   the page 'nearb
17404 79 27 20 69 73 20 6c 6f 63 61 74 65 64 2e 0a 20  y' is located.. 
17405 20 20 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20     */.    do {. 
17406 20 20 20 20 20 70 50 72 65 76 54 72 75 6e 6b 20       pPrevTrunk 
17407 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20  = pTrunk;.      
17408 69 66 28 20 70 50 72 65 76 54 72 75 6e 6b 20 29  if( pPrevTrunk )
17409 7b 0a 20 20 20 20 20 20 20 20 69 54 72 75 6e 6b  {.        iTrunk
1740a 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 72   = get4byte(&pPr
1740b 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30  evTrunk->aData[0
1740c 5d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ]);.      }else{
1740d 0a 20 20 20 20 20 20 20 20 69 54 72 75 6e 6b 20  .        iTrunk 
1740e 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67  = get4byte(&pPag
1740f 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 3b 0a  e1->aData[32]);.
17410 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 74 65        }.      te
17411 73 74 63 61 73 65 28 20 69 54 72 75 6e 6b 3d 3d  stcase( iTrunk==
17412 6d 78 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20  mxPage );.      
17413 69 66 28 20 69 54 72 75 6e 6b 3e 6d 78 50 61 67  if( iTrunk>mxPag
17414 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  e ){.        rc 
17415 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  = SQLITE_CORRUPT
17416 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 65 6c  _BKPT;.      }el
17417 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  se{.        rc =
17418 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42   btreeGetPage(pB
17419 74 2c 20 69 54 72 75 6e 6b 2c 20 26 70 54 72 75  t, iTrunk, &pTru
1741a 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  nk, 0);.      }.
1741b 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
1741c 20 20 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d          pTrunk =
1741d 20 30 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   0;.        goto
1741e 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61   end_allocate_pa
1741f 67 65 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  ge;.      }..   
17420 20 20 20 6b 20 3d 20 67 65 74 34 62 79 74 65 28     k = get4byte(
17421 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34  &pTrunk->aData[4
17422 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6b 3d  ]);.      if( k=
17423 3d 30 20 26 26 20 21 73 65 61 72 63 68 4c 69 73  =0 && !searchLis
17424 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  t ){.        /* 
17425 54 68 65 20 74 72 75 6e 6b 20 68 61 73 20 6e 6f  The trunk has no
17426 20 6c 65 61 76 65 73 20 61 6e 64 20 74 68 65 20   leaves and the 
17427 6c 69 73 74 20 69 73 20 6e 6f 74 20 62 65 69 6e  list is not bein
17428 67 20 73 65 61 72 63 68 65 64 2e 20 0a 20 20 20  g searched. .   
17429 20 20 20 20 20 2a 2a 20 53 6f 20 65 78 74 72 61       ** So extra
1742a 63 74 20 74 68 65 20 74 72 75 6e 6b 20 70 61 67  ct the trunk pag
1742b 65 20 69 74 73 65 6c 66 20 61 6e 64 20 75 73 65  e itself and use
1742c 20 69 74 20 61 73 20 74 68 65 20 6e 65 77 6c 79   it as the newly
1742d 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c   .        ** all
1742e 6f 63 61 74 65 64 20 70 61 67 65 20 2a 2f 0a 20  ocated page */. 
1742f 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
17430 50 72 65 76 54 72 75 6e 6b 3d 3d 30 20 29 3b 0a  PrevTrunk==0 );.
17431 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
17432 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
17433 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b  Trunk->pDbPage);
17434 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20  .        if( rc 
17435 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  ){.          got
17436 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70  o end_allocate_p
17437 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  age;.        }. 
17438 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20         *pPgno = 
17439 69 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20  iTrunk;.        
1743a 6d 65 6d 63 70 79 28 26 70 50 61 67 65 31 2d 3e  memcpy(&pPage1->
1743b 61 44 61 74 61 5b 33 32 5d 2c 20 26 70 54 72 75  aData[32], &pTru
1743c 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29  nk->aData[0], 4)
1743d 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 50 61 67  ;.        *ppPag
1743e 65 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20  e = pTrunk;.    
1743f 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a      pTrunk = 0;.
17440 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22          TRACE(("
17441 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75  ALLOCATE: %d tru
17442 6e 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61 67  nk - %d free pag
17443 65 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67  es left\n", *pPg
17444 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20 20  no, n-1));.     
17445 20 7d 65 6c 73 65 20 69 66 28 20 6b 3e 28 75 33   }else if( k>(u3
17446 32 29 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  2)(pBt->usableSi
17447 7a 65 2f 34 20 2d 20 32 29 20 29 7b 0a 20 20 20  ze/4 - 2) ){.   
17448 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66       /* Value of
17449 20 6b 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e   k is out of ran
1744a 67 65 2e 20 20 44 61 74 61 62 61 73 65 20 63 6f  ge.  Database co
1744b 72 72 75 70 74 69 6f 6e 20 2a 2f 0a 20 20 20 20  rruption */.    
1744c 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1744d 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
1744e 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61        goto end_a
1744f 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 23 69  llocate_page;.#i
17450 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
17451 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
17452 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 65 61     }else if( sea
17453 72 63 68 4c 69 73 74 20 26 26 20 6e 65 61 72 62  rchList && nearb
17454 79 3d 3d 69 54 72 75 6e 6b 20 29 7b 0a 20 20 20  y==iTrunk ){.   
17455 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 69 73 74       /* The list
17456 20 69 73 20 62 65 69 6e 67 20 73 65 61 72 63 68   is being search
17457 65 64 20 61 6e 64 20 74 68 69 73 20 74 72 75 6e  ed and this trun
17458 6b 20 70 61 67 65 20 69 73 20 74 68 65 20 70 61  k page is the pa
17459 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f  ge.        ** to
1745a 20 61 6c 6c 6f 63 61 74 65 2c 20 72 65 67 61 72   allocate, regar
1745b 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72  dless of whether
1745c 20 69 74 20 68 61 73 20 6c 65 61 76 65 73 2e 0a   it has leaves..
1745d 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
1745e 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e     assert( *pPgn
1745f 6f 3d 3d 69 54 72 75 6e 6b 20 29 3b 0a 20 20 20  o==iTrunk );.   
17460 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70       *ppPage = p
17461 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 73  Trunk;.        s
17462 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20  earchList = 0;. 
17463 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
17464 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54  te3PagerWrite(pT
17465 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a  runk->pDbPage);.
17466 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29          if( rc )
17467 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  {.          goto
17468 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61   end_allocate_pa
17469 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ge;.        }.  
1746a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 29        if( k==0 )
1746b 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
1746c 21 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20  !pPrevTrunk ){. 
1746d 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70             memcp
1746e 79 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61  y(&pPage1->aData
1746f 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61  [32], &pTrunk->a
17470 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20  Data[0], 4);.   
17471 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
17472 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79            memcpy
17473 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44  (&pPrevTrunk->aD
17474 61 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d  ata[0], &pTrunk-
17475 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20  >aData[0], 4);. 
17476 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
17477 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
17478 20 20 20 20 2f 2a 20 54 68 65 20 74 72 75 6e 6b      /* The trunk
17479 20 70 61 67 65 20 69 73 20 72 65 71 75 69 72 65   page is require
1747a 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 20  d by the caller 
1747b 62 75 74 20 69 74 20 63 6f 6e 74 61 69 6e 73 20  but it contains 
1747c 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 6f  .          ** po
1747d 69 6e 74 65 72 73 20 74 6f 20 66 72 65 65 2d 6c  inters to free-l
1747e 69 73 74 20 6c 65 61 76 65 73 2e 20 54 68 65 20  ist leaves. The 
1747f 66 69 72 73 74 20 6c 65 61 66 20 62 65 63 6f 6d  first leaf becom
17480 65 73 20 61 20 74 72 75 6e 6b 0a 20 20 20 20 20  es a trunk.     
17481 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69 6e 20       ** page in 
17482 74 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20 20  this case..     
17483 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
17484 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 54    MemPage *pNewT
17485 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  runk;.          
17486 50 67 6e 6f 20 69 4e 65 77 54 72 75 6e 6b 20 3d  Pgno iNewTrunk =
17487 20 67 65 74 34 62 79 74 65 28 26 70 54 72 75 6e   get4byte(&pTrun
17488 6b 2d 3e 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20  k->aData[8]);.  
17489 20 20 20 20 20 20 20 20 69 66 28 20 69 4e 65 77          if( iNew
1748a 54 72 75 6e 6b 3e 6d 78 50 61 67 65 20 29 7b 20  Trunk>mxPage ){ 
1748b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20  .            rc 
1748c 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  = SQLITE_CORRUPT
1748d 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 20  _BKPT;.         
1748e 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f     goto end_allo
1748f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20  cate_page;.     
17490 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
17491 20 74 65 73 74 63 61 73 65 28 20 69 4e 65 77 54   testcase( iNewT
17492 72 75 6e 6b 3d 3d 6d 78 50 61 67 65 20 29 3b 0a  runk==mxPage );.
17493 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 62            rc = b
17494 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
17495 20 69 4e 65 77 54 72 75 6e 6b 2c 20 26 70 4e 65   iNewTrunk, &pNe
17496 77 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20  wTrunk, 0);.    
17497 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
17498 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
17499 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
1749a 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20  allocate_page;. 
1749b 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1749c 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1749d 33 50 61 67 65 72 57 72 69 74 65 28 70 4e 65 77  3PagerWrite(pNew
1749e 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b  Trunk->pDbPage);
1749f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72  .          if( r
174a0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
174a1 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6c 65              rele
174a2 61 73 65 50 61 67 65 28 70 4e 65 77 54 72 75 6e  asePage(pNewTrun
174a3 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  k);.            
174a4 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74  goto end_allocat
174a5 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20  e_page;.        
174a6 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 6d 65    }.          me
174a7 6d 63 70 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d  mcpy(&pNewTrunk-
174a8 3e 61 44 61 74 61 5b 30 5d 2c 20 26 70 54 72 75  >aData[0], &pTru
174a9 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29  nk->aData[0], 4)
174aa 3b 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 34  ;.          put4
174ab 62 79 74 65 28 26 70 4e 65 77 54 72 75 6e 6b 2d  byte(&pNewTrunk-
174ac 3e 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b  >aData[4], k-1);
174ad 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70  .          memcp
174ae 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44  y(&pNewTrunk->aD
174af 61 74 61 5b 38 5d 2c 20 26 70 54 72 75 6e 6b 2d  ata[8], &pTrunk-
174b0 3e 61 44 61 74 61 5b 31 32 5d 2c 20 28 6b 2d 31  >aData[12], (k-1
174b1 29 2a 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20  )*4);.          
174b2 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77  releasePage(pNew
174b3 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20  Trunk);.        
174b4 20 20 69 66 28 20 21 70 50 72 65 76 54 72 75 6e    if( !pPrevTrun
174b5 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  k ){.           
174b6 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
174b7 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
174b8 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65  (pPage1->pDbPage
174b9 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ) );.           
174ba 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65   put4byte(&pPage
174bb 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69 4e  1->aData[32], iN
174bc 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20  ewTrunk);.      
174bd 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
174be 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
174bf 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
174c0 72 65 76 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67  revTrunk->pDbPag
174c1 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  e);.            
174c2 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
174c3 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64          goto end
174c4 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a  _allocate_page;.
174c5 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
174c6 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79            put4by
174c7 74 65 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e  te(&pPrevTrunk->
174c8 61 44 61 74 61 5b 30 5d 2c 20 69 4e 65 77 54 72  aData[0], iNewTr
174c9 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20  unk);.          
174ca 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
174cb 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a      pTrunk = 0;.
174cc 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22          TRACE(("
174cd 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75  ALLOCATE: %d tru
174ce 6e 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61 67  nk - %d free pag
174cf 65 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67  es left\n", *pPg
174d0 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 23 65 6e 64 69  no, n-1));.#endi
174d1 66 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  f.      }else if
174d2 28 20 6b 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  ( k>0 ){.       
174d3 20 2f 2a 20 45 78 74 72 61 63 74 20 61 20 6c 65   /* Extract a le
174d4 61 66 20 66 72 6f 6d 20 74 68 65 20 74 72 75 6e  af from the trun
174d5 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 33 32  k */.        u32
174d6 20 63 6c 6f 73 65 73 74 3b 0a 20 20 20 20 20 20   closest;.      
174d7 20 20 50 67 6e 6f 20 69 50 61 67 65 3b 0a 20 20    Pgno iPage;.  
174d8 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63        unsigned c
174d9 68 61 72 20 2a 61 44 61 74 61 20 3d 20 70 54 72  har *aData = pTr
174da 75 6e 6b 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20  unk->aData;.    
174db 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
174dc 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e  PagerWrite(pTrun
174dd 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  k->pDbPage);.   
174de 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20       if( rc ){. 
174df 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e           goto en
174e0 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b  d_allocate_page;
174e1 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
174e2 20 20 20 69 66 28 20 6e 65 61 72 62 79 3e 30 20     if( nearby>0 
174e3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33 32  ){.          u32
174e4 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e   i;.          in
174e5 74 20 64 69 73 74 3b 0a 20 20 20 20 20 20 20 20  t dist;.        
174e6 20 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20    closest = 0;. 
174e7 20 20 20 20 20 20 20 20 20 64 69 73 74 20 3d 20           dist = 
174e8 67 65 74 34 62 79 74 65 28 26 61 44 61 74 61 5b  get4byte(&aData[
174e9 38 5d 29 20 2d 20 6e 65 61 72 62 79 3b 0a 20 20  8]) - nearby;.  
174ea 20 20 20 20 20 20 20 20 69 66 28 20 64 69 73 74          if( dist
174eb 3c 30 20 29 20 64 69 73 74 20 3d 20 2d 64 69 73  <0 ) dist = -dis
174ec 74 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  t;.          for
174ed 28 69 3d 31 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b  (i=1; i<k; i++){
174ee 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74  .            int
174ef 20 64 32 20 3d 20 67 65 74 34 62 79 74 65 28 26   d2 = get4byte(&
174f0 61 44 61 74 61 5b 38 2b 69 2a 34 5d 29 20 2d 20  aData[8+i*4]) - 
174f1 6e 65 61 72 62 79 3b 0a 20 20 20 20 20 20 20 20  nearby;.        
174f2 20 20 20 20 69 66 28 20 64 32 3c 30 20 29 20 64      if( d2<0 ) d
174f3 32 20 3d 20 2d 64 32 3b 0a 20 20 20 20 20 20 20  2 = -d2;.       
174f4 20 20 20 20 20 69 66 28 20 64 32 3c 64 69 73 74       if( d2<dist
174f5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
174f6 20 20 63 6c 6f 73 65 73 74 20 3d 20 69 3b 0a 20    closest = i;. 
174f7 20 20 20 20 20 20 20 20 20 20 20 20 20 64 69 73               dis
174f8 74 20 3d 20 64 32 3b 0a 20 20 20 20 20 20 20 20  t = d2;.        
174f9 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
174fa 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  }.        }else{
174fb 0a 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65  .          close
174fc 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  st = 0;.        
174fd 7d 0a 0a 20 20 20 20 20 20 20 20 69 50 61 67 65  }..        iPage
174fe 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 44 61   = get4byte(&aDa
174ff 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 29  ta[8+closest*4])
17500 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  ;.        testca
17501 73 65 28 20 69 50 61 67 65 3d 3d 6d 78 50 61 67  se( iPage==mxPag
17502 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  e );.        if(
17503 20 69 50 61 67 65 3e 6d 78 50 61 67 65 20 29 7b   iPage>mxPage ){
17504 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
17505 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
17506 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67  KPT;.          g
17507 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65  oto end_allocate
17508 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d  _page;.        }
17509 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
1750a 65 28 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 65  e( iPage==mxPage
1750b 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
1750c 21 73 65 61 72 63 68 4c 69 73 74 20 7c 7c 20 69  !searchList || i
1750d 50 61 67 65 3d 3d 6e 65 61 72 62 79 20 29 7b 0a  Page==nearby ){.
1750e 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 6f            int no
1750f 43 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 20 20 20  Content;.       
17510 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 50 61 67     *pPgno = iPag
17511 65 3b 0a 20 20 20 20 20 20 20 20 20 20 54 52 41  e;.          TRA
17512 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25  CE(("ALLOCATE: %
17513 64 20 77 61 73 20 6c 65 61 66 20 25 64 20 6f 66  d was leaf %d of
17514 20 25 64 20 6f 6e 20 74 72 75 6e 6b 20 25 64 22   %d on trunk %d"
17515 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
17516 20 20 22 3a 20 25 64 20 6d 6f 72 65 20 66 72 65    ": %d more fre
17517 65 20 70 61 67 65 73 5c 6e 22 2c 0a 20 20 20 20  e pages\n",.    
17518 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 50               *pP
17519 67 6e 6f 2c 20 63 6c 6f 73 65 73 74 2b 31 2c 20  gno, closest+1, 
1751a 6b 2c 20 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 2c  k, pTrunk->pgno,
1751b 20 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 20 20   n-1));.        
1751c 20 20 69 66 28 20 63 6c 6f 73 65 73 74 3c 6b 2d    if( closest<k-
1751d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  1 ){.           
1751e 20 6d 65 6d 63 70 79 28 26 61 44 61 74 61 5b 38   memcpy(&aData[8
1751f 2b 63 6c 6f 73 65 73 74 2a 34 5d 2c 20 26 61 44  +closest*4], &aD
17520 61 74 61 5b 34 2b 6b 2a 34 5d 2c 20 34 29 3b 0a  ata[4+k*4], 4);.
17521 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
17522 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26        put4byte(&
17523 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a  aData[4], k-1);.
17524 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
17525 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
17526 77 72 69 74 65 61 62 6c 65 28 70 54 72 75 6e 6b  writeable(pTrunk
17527 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
17528 20 20 20 20 20 20 20 20 6e 6f 43 6f 6e 74 65 6e          noConten
17529 74 20 3d 20 21 62 74 72 65 65 47 65 74 48 61 73  t = !btreeGetHas
1752a 43 6f 6e 74 65 6e 74 28 70 42 74 2c 20 2a 70 50  Content(pBt, *pP
1752b 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20  gno);.          
1752c 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67  rc = btreeGetPag
1752d 65 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70  e(pBt, *pPgno, p
1752e 70 50 61 67 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74  pPage, noContent
1752f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
17530 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
17531 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  {.            rc
17532 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
17533 72 69 74 65 28 28 2a 70 70 50 61 67 65 29 2d 3e  rite((*ppPage)->
17534 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
17535 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
17536 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
17537 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65           release
17538 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20  Page(*ppPage);. 
17539 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
1753a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1753b 20 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20     searchList = 
1753c 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  0;.        }.   
1753d 20 20 20 7d 0a 20 20 20 20 20 20 72 65 6c 65 61     }.      relea
1753e 73 65 50 61 67 65 28 70 50 72 65 76 54 72 75 6e  sePage(pPrevTrun
1753f 6b 29 3b 0a 20 20 20 20 20 20 70 50 72 65 76 54  k);.      pPrevT
17540 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 7d 77  runk = 0;.    }w
17541 68 69 6c 65 28 20 73 65 61 72 63 68 4c 69 73 74  hile( searchList
17542 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   );.  }else{.   
17543 20 2f 2a 20 54 68 65 72 65 20 61 72 65 20 6e 6f   /* There are no
17544 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72   pages on the fr
17545 65 65 6c 69 73 74 2c 20 73 6f 20 63 72 65 61 74  eelist, so creat
17546 65 20 61 20 6e 65 77 20 70 61 67 65 20 61 74 20  e a new page at 
17547 74 68 65 0a 20 20 20 20 2a 2a 20 65 6e 64 20 6f  the.    ** end o
17548 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20  f the file */.  
17549 20 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 70 61    int nPage = pa
1754a 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74  gerPagecount(pBt
1754b 29 3b 0a 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20  );.    *pPgno = 
1754c 6e 50 61 67 65 20 2b 20 31 3b 0a 0a 20 20 20 20  nPage + 1;..    
1754d 69 66 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44  if( *pPgno==PEND
1754e 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
1754f 74 29 20 29 7b 0a 20 20 20 20 20 20 28 2a 70 50  t) ){.      (*pP
17550 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 23  gno)++;.    }..#
17551 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
17552 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
17553 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56    if( pBt->autoV
17554 61 63 75 75 6d 20 26 26 20 50 54 52 4d 41 50 5f  acuum && PTRMAP_
17555 49 53 50 41 47 45 28 70 42 74 2c 20 2a 70 50 67  ISPAGE(pBt, *pPg
17556 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  no) ){.      /* 
17557 49 66 20 2a 70 50 67 6e 6f 20 72 65 66 65 72 73  If *pPgno refers
17558 20 74 6f 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61   to a pointer-ma
17559 70 20 70 61 67 65 2c 20 61 6c 6c 6f 63 61 74 65  p page, allocate
1755a 20 74 77 6f 20 6e 65 77 20 70 61 67 65 73 0a 20   two new pages. 
1755b 20 20 20 20 20 2a 2a 20 61 74 20 74 68 65 20 65       ** at the e
1755c 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69  nd of the file i
1755d 6e 73 74 65 61 64 20 6f 66 20 6f 6e 65 2e 20 54  nstead of one. T
1755e 68 65 20 66 69 72 73 74 20 61 6c 6c 6f 63 61 74  he first allocat
1755f 65 64 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a  ed page.      **
17560 20 62 65 63 6f 6d 65 73 20 61 20 6e 65 77 20 70   becomes a new p
17561 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c  ointer-map page,
17562 20 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 75   the second is u
17563 73 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65  sed by the calle
17564 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  r..      */.    
17565 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 67 20 3d    MemPage *pPg =
17566 20 30 3b 0a 20 20 20 20 20 20 54 52 41 43 45 28   0;.      TRACE(
17567 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66  ("ALLOCATE: %d f
17568 72 6f 6d 20 65 6e 64 20 6f 66 20 66 69 6c 65 20  rom end of file 
17569 28 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67  (pointer-map pag
1756a 65 29 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b  e)\n", *pPgno));
1756b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2a  .      assert( *
1756c 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42  pPgno!=PENDING_B
1756d 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b  YTE_PAGE(pBt) );
1756e 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65  .      rc = btre
1756f 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 2a 70  eGetPage(pBt, *p
17570 50 67 6e 6f 2c 20 26 70 50 67 2c 20 30 29 3b 0a  Pgno, &pPg, 0);.
17571 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
17572 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
17573 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
17574 61 67 65 72 57 72 69 74 65 28 70 50 67 2d 3e 70  agerWrite(pPg->p
17575 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  DbPage);.       
17576 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 67   releasePage(pPg
17577 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
17578 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
17579 20 72 63 3b 0a 20 20 20 20 20 20 28 2a 70 50 67   rc;.      (*pPg
1757a 6e 6f 29 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28  no)++;.      if(
1757b 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47   *pPgno==PENDING
1757c 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20  _BYTE_PAGE(pBt) 
1757d 29 7b 20 28 2a 70 50 67 6e 6f 29 2b 2b 3b 20 7d  ){ (*pPgno)++; }
1757e 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  .    }.#endif.. 
1757f 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e     assert( *pPgn
17580 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  o!=PENDING_BYTE_
17581 50 41 47 45 28 70 42 74 29 20 29 3b 0a 20 20 20  PAGE(pBt) );.   
17582 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61   rc = btreeGetPa
17583 67 65 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20  ge(pBt, *pPgno, 
17584 70 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20  ppPage, 0);.    
17585 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
17586 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  rc;.    rc = sql
17587 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 28  ite3PagerWrite((
17588 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 61 67  *ppPage)->pDbPag
17589 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  e);.    if( rc!=
1758a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1758b 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a     releasePage(*
1758c 70 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20  ppPage);.    }. 
1758d 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43     TRACE(("ALLOC
1758e 41 54 45 3a 20 25 64 20 66 72 6f 6d 20 65 6e 64  ATE: %d from end
1758f 20 6f 66 20 66 69 6c 65 5c 6e 22 2c 20 2a 70 50   of file\n", *pP
17590 67 6e 6f 29 29 3b 0a 20 20 7d 0a 0a 20 20 61 73  gno));.  }..  as
17591 73 65 72 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45  sert( *pPgno!=PE
17592 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
17593 70 42 74 29 20 29 3b 0a 0a 65 6e 64 5f 61 6c 6c  pBt) );..end_all
17594 6f 63 61 74 65 5f 70 61 67 65 3a 0a 20 20 72 65  ocate_page:.  re
17595 6c 65 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b  leasePage(pTrunk
17596 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65  );.  releasePage
17597 28 70 50 72 65 76 54 72 75 6e 6b 29 3b 0a 20 20  (pPrevTrunk);.  
17598 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
17599 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  K ){.    if( sql
1759a 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66  ite3PagerPageRef
1759b 63 6f 75 6e 74 28 28 2a 70 70 50 61 67 65 29 2d  count((*ppPage)-
1759c 3e 70 44 62 50 61 67 65 29 3e 31 20 29 7b 0a 20  >pDbPage)>1 ){. 
1759d 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
1759e 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20  (*ppPage);.     
1759f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
175a0 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
175a1 20 7d 0a 20 20 20 20 28 2a 70 70 50 61 67 65 29   }.    (*ppPage)
175a2 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20  ->isInit = 0;.  
175a3 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 50 61  }else{.    *ppPa
175a4 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  ge = 0;.  }.  re
175a5 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
175a6 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
175a7 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20 70  is used to add p
175a8 61 67 65 20 69 50 61 67 65 20 74 6f 20 74 68 65  age iPage to the
175a9 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66   database file f
175aa 72 65 65 2d 6c 69 73 74 2e 20 0a 2a 2a 20 49 74  ree-list. .** It
175ab 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74   is assumed that
175ac 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
175ad 20 61 6c 72 65 61 64 79 20 61 20 70 61 72 74 20   already a part 
175ae 6f 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  of the free-list
175af 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75  ..**.** The valu
175b0 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
175b1 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
175b2 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
175b3 20 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 0a 2a 2a   is optional..**
175b4 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 68   If the caller h
175b5 61 70 70 65 6e 73 20 74 6f 20 68 61 76 65 20 61  appens to have a
175b6 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
175b7 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 0a  MemPage object .
175b8 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ** corresponding
175b9 20 74 6f 20 70 61 67 65 20 69 50 61 67 65 20 68   to page iPage h
175ba 61 6e 64 79 2c 20 69 74 20 6d 61 79 20 70 61 73  andy, it may pas
175bb 73 20 69 74 20 61 73 20 74 68 65 20 73 65 63 6f  s it as the seco
175bc 6e 64 20 76 61 6c 75 65 2e 20 0a 2a 2a 20 4f 74  nd value. .** Ot
175bd 68 65 72 77 69 73 65 2c 20 69 74 20 6d 61 79 20  herwise, it may 
175be 70 61 73 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  pass NULL..**.**
175bf 20 49 66 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   If a pointer to
175c0 20 61 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63   a MemPage objec
175c1 74 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74  t is passed as t
175c2 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
175c3 6e 74 2c 0a 2a 2a 20 69 74 73 20 72 65 66 65 72  nt,.** its refer
175c4 65 6e 63 65 20 63 6f 75 6e 74 20 69 73 20 6e 6f  ence count is no
175c5 74 20 61 6c 74 65 72 65 64 20 62 79 20 74 68 69  t altered by thi
175c6 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  s function..*/.s
175c7 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 50 61  tatic int freePa
175c8 67 65 32 28 42 74 53 68 61 72 65 64 20 2a 70 42  ge2(BtShared *pB
175c9 74 2c 20 4d 65 6d 50 61 67 65 20 2a 70 4d 65 6d  t, MemPage *pMem
175ca 50 61 67 65 2c 20 50 67 6e 6f 20 69 50 61 67 65  Page, Pgno iPage
175cb 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54  ){.  MemPage *pT
175cc 72 75 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 20  runk = 0;       
175cd 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65           /* Free
175ce 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65  -list trunk page
175cf 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 72 75 6e   */.  Pgno iTrun
175d0 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  k = 0;          
175d1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
175d2 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65  e number of free
175d3 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65  -list trunk page
175d4 20 2a 2f 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a   */ .  MemPage *
175d5 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50  pPage1 = pBt->pP
175d6 61 67 65 31 3b 20 20 20 20 20 20 2f 2a 20 4c 6f  age1;      /* Lo
175d7 63 61 6c 20 72 65 66 65 72 65 6e 63 65 20 74 6f  cal reference to
175d8 20 70 61 67 65 20 31 20 2a 2f 0a 20 20 4d 65 6d   page 1 */.  Mem
175d9 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20  Page *pPage;    
175da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
175db 20 2f 2a 20 50 61 67 65 20 62 65 69 6e 67 20 66   /* Page being f
175dc 72 65 65 64 2e 20 4d 61 79 20 62 65 20 4e 55 4c  reed. May be NUL
175dd 4c 2e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20  L. */.  int rc; 
175de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
175df 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
175e0 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20  eturn Code */.  
175e1 69 6e 74 20 6e 46 72 65 65 3b 20 20 20 20 20 20  int nFree;      
175e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
175e3 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e      /* Initial n
175e4 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f  umber of pages o
175e5 6e 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a  n free-list */..
175e6 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
175e7 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
175e8 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
175e9 73 65 72 74 28 20 69 50 61 67 65 3e 31 20 29 3b  sert( iPage>1 );
175ea 0a 20 20 61 73 73 65 72 74 28 20 21 70 4d 65 6d  .  assert( !pMem
175eb 50 61 67 65 20 7c 7c 20 70 4d 65 6d 50 61 67 65  Page || pMemPage
175ec 2d 3e 70 67 6e 6f 3d 3d 69 50 61 67 65 20 29 3b  ->pgno==iPage );
175ed 0a 0a 20 20 69 66 28 20 70 4d 65 6d 50 61 67 65  ..  if( pMemPage
175ee 20 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20   ){.    pPage = 
175ef 70 4d 65 6d 50 61 67 65 3b 0a 20 20 20 20 73 71  pMemPage;.    sq
175f0 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 70 50  lite3PagerRef(pP
175f1 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  age->pDbPage);. 
175f2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67   }else{.    pPag
175f3 65 20 3d 20 62 74 72 65 65 50 61 67 65 4c 6f 6f  e = btreePageLoo
175f4 6b 75 70 28 70 42 74 2c 20 69 50 61 67 65 29 3b  kup(pBt, iPage);
175f5 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65  .  }..  /* Incre
175f6 6d 65 6e 74 20 74 68 65 20 66 72 65 65 20 70 61  ment the free pa
175f7 67 65 20 63 6f 75 6e 74 20 6f 6e 20 70 50 61 67  ge count on pPag
175f8 65 31 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  e1 */.  rc = sql
175f9 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
175fa 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b  Page1->pDbPage);
175fb 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f  .  if( rc ) goto
175fc 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20   freepage_out;. 
175fd 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 74   nFree = get4byt
175fe 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61  e(&pPage1->aData
175ff 5b 33 36 5d 29 3b 0a 20 20 70 75 74 34 62 79 74  [36]);.  put4byt
17600 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61  e(&pPage1->aData
17601 5b 33 36 5d 2c 20 6e 46 72 65 65 2b 31 29 3b 0a  [36], nFree+1);.
17602 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53  .#ifdef SQLITE_S
17603 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f  ECURE_DELETE.  /
17604 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f  * If the SQLITE_
17605 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 63 6f  SECURE_DELETE co
17606 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f  mpile-time optio
17607 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68  n is enabled, th
17608 65 6e 0a 20 20 2a 2a 20 61 6c 77 61 79 73 20 66  en.  ** always f
17609 75 6c 6c 79 20 6f 76 65 72 77 72 69 74 65 20 64  ully overwrite d
1760a 65 6c 65 74 65 64 20 69 6e 66 6f 72 6d 61 74 69  eleted informati
1760b 6f 6e 20 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20  on with zeros.. 
1760c 20 2a 2f 0a 20 20 69 66 28 20 28 21 70 50 61 67   */.  if( (!pPag
1760d 65 20 26 26 20 28 72 63 20 3d 20 62 74 72 65 65  e && (rc = btree
1760e 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 50 61  GetPage(pBt, iPa
1760f 67 65 2c 20 26 70 50 61 67 65 2c 20 30 29 29 29  ge, &pPage, 0)))
17610 0a 20 20 20 7c 7c 20 20 20 20 20 20 20 20 20 20  .   ||          
17611 20 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50    (rc = sqlite3P
17612 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d  agerWrite(pPage-
17613 3e 70 44 62 50 61 67 65 29 29 0a 20 20 29 7b 0a  >pDbPage)).  ){.
17614 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67      goto freepag
17615 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 6d 65 6d  e_out;.  }.  mem
17616 73 65 74 28 70 50 61 67 65 2d 3e 61 44 61 74 61  set(pPage->aData
17617 2c 20 30 2c 20 70 50 61 67 65 2d 3e 70 42 74 2d  , 0, pPage->pBt-
17618 3e 70 61 67 65 53 69 7a 65 29 3b 0a 23 65 6e 64  >pageSize);.#end
17619 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  if..  /* If the 
1761a 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74  database support
1761b 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 77  s auto-vacuum, w
1761c 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e  rite an entry in
1761d 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70   the pointer-map
1761e 0a 20 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74  .  ** to indicat
1761f 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20  e that the page 
17620 69 73 20 66 72 65 65 2e 0a 20 20 2a 2f 0a 20 20  is free..  */.  
17621 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d  if( ISAUTOVACUUM
17622 20 29 7b 0a 20 20 20 20 70 74 72 6d 61 70 50 75   ){.    ptrmapPu
17623 74 28 70 42 74 2c 20 69 50 61 67 65 2c 20 50 54  t(pBt, iPage, PT
17624 52 4d 41 50 5f 46 52 45 45 50 41 47 45 2c 20 30  RMAP_FREEPAGE, 0
17625 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 20  , &rc);.    if( 
17626 72 63 20 29 20 67 6f 74 6f 20 66 72 65 65 70 61  rc ) goto freepa
17627 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f  ge_out;.  }..  /
17628 2a 20 4e 6f 77 20 6d 61 6e 69 70 75 6c 61 74 65  * Now manipulate
17629 20 74 68 65 20 61 63 74 75 61 6c 20 64 61 74 61   the actual data
1762a 62 61 73 65 20 66 72 65 65 2d 6c 69 73 74 20 73  base free-list s
1762b 74 72 75 63 74 75 72 65 2e 20 54 68 65 72 65 20  tructure. There 
1762c 61 72 65 20 74 77 6f 0a 20 20 2a 2a 20 70 6f 73  are two.  ** pos
1762d 73 69 62 69 6c 69 74 69 65 73 2e 20 49 66 20 74  sibilities. If t
1762e 68 65 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20  he free-list is 
1762f 63 75 72 72 65 6e 74 6c 79 20 65 6d 70 74 79 2c  currently empty,
17630 20 6f 72 20 69 66 20 74 68 65 20 66 69 72 73 74   or if the first
17631 0a 20 20 2a 2a 20 74 72 75 6e 6b 20 70 61 67 65  .  ** trunk page
17632 20 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73   in the free-lis
17633 74 20 69 73 20 66 75 6c 6c 2c 20 74 68 65 6e 20  t is full, then 
17634 74 68 69 73 20 70 61 67 65 20 77 69 6c 6c 20 62  this page will b
17635 65 63 6f 6d 65 20 61 0a 20 20 2a 2a 20 6e 65 77  ecome a.  ** new
17636 20 66 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b   free-list trunk
17637 20 70 61 67 65 2e 20 4f 74 68 65 72 77 69 73 65   page. Otherwise
17638 2c 20 69 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65  , it will become
17639 20 61 20 6c 65 61 66 20 6f 66 20 74 68 65 0a 20   a leaf of the. 
1763a 20 2a 2a 20 66 69 72 73 74 20 74 72 75 6e 6b 20   ** first trunk 
1763b 70 61 67 65 20 69 6e 20 74 68 65 20 63 75 72 72  page in the curr
1763c 65 6e 74 20 66 72 65 65 2d 6c 69 73 74 2e 20 54  ent free-list. T
1763d 68 69 73 20 62 6c 6f 63 6b 20 74 65 73 74 73 20  his block tests 
1763e 69 66 20 69 74 0a 20 20 2a 2a 20 69 73 20 70 6f  if it.  ** is po
1763f 73 73 69 62 6c 65 20 74 6f 20 61 64 64 20 74 68  ssible to add th
17640 65 20 70 61 67 65 20 61 73 20 61 20 6e 65 77 20  e page as a new 
17641 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 2e 0a  free-list leaf..
17642 20 20 2a 2f 0a 20 20 69 66 28 20 6e 46 72 65 65    */.  if( nFree
17643 21 3d 30 20 29 7b 0a 20 20 20 20 75 33 32 20 6e  !=0 ){.    u32 n
17644 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 20  Leaf;           
17645 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20       /* Initial 
17646 6e 75 6d 62 65 72 20 6f 66 20 6c 65 61 66 20 63  number of leaf c
17647 65 6c 6c 73 20 6f 6e 20 74 72 75 6e 6b 20 70 61  ells on trunk pa
17648 67 65 20 2a 2f 0a 0a 20 20 20 20 69 54 72 75 6e  ge */..    iTrun
17649 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50  k = get4byte(&pP
1764a 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29  age1->aData[32])
1764b 3b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65  ;.    rc = btree
1764c 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 54 72  GetPage(pBt, iTr
1764d 75 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29  unk, &pTrunk, 0)
1764e 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
1764f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
17650 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f   goto freepage_o
17651 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e  ut;.    }..    n
17652 4c 65 61 66 20 3d 20 67 65 74 34 62 79 74 65 28  Leaf = get4byte(
17653 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34  &pTrunk->aData[4
17654 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ]);.    assert( 
17655 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3e  pBt->usableSize>
17656 33 32 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 4c  32 );.    if( nL
17657 65 61 66 20 3e 20 28 75 33 32 29 70 42 74 2d 3e  eaf > (u32)pBt->
17658 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32  usableSize/4 - 2
17659 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
1765a 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
1765b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66  PT;.      goto f
1765c 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20  reepage_out;.   
1765d 20 7d 0a 20 20 20 20 69 66 28 20 6e 4c 65 61 66   }.    if( nLeaf
1765e 20 3c 20 28 75 33 32 29 70 42 74 2d 3e 75 73 61   < (u32)pBt->usa
1765f 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 29 7b  bleSize/4 - 8 ){
17660 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 74 68 69  .      /* In thi
17661 73 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20  s case there is 
17662 72 6f 6f 6d 20 6f 6e 20 74 68 65 20 74 72 75 6e  room on the trun
17663 6b 20 70 61 67 65 20 74 6f 20 69 6e 73 65 72 74  k page to insert
17664 20 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 20   the page.      
17665 2a 2a 20 62 65 69 6e 67 20 66 72 65 65 64 20 61  ** being freed a
17666 73 20 61 20 6e 65 77 20 6c 65 61 66 2e 0a 20 20  s a new leaf..  
17667 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
17668 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 74 72  Note that the tr
17669 75 6e 6b 20 70 61 67 65 20 69 73 20 6e 6f 74 20  unk page is not 
1766a 72 65 61 6c 6c 79 20 66 75 6c 6c 20 75 6e 74 69  really full unti
1766b 6c 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20  l it contains.  
1766c 20 20 20 20 2a 2a 20 75 73 61 62 6c 65 53 69 7a      ** usableSiz
1766d 65 2f 34 20 2d 20 32 20 65 6e 74 72 69 65 73 2c  e/4 - 2 entries,
1766e 20 6e 6f 74 20 75 73 61 62 6c 65 53 69 7a 65 2f   not usableSize/
1766f 34 20 2d 20 38 20 65 6e 74 72 69 65 73 20 61 73  4 - 8 entries as
17670 20 77 65 20 68 61 76 65 0a 20 20 20 20 20 20 2a   we have.      *
17671 2a 20 63 6f 64 65 64 2e 20 20 42 75 74 20 64 75  * coded.  But du
17672 65 20 74 6f 20 61 20 63 6f 64 69 6e 67 20 65 72  e to a coding er
17673 72 6f 72 20 69 6e 20 76 65 72 73 69 6f 6e 73 20  ror in versions 
17674 6f 66 20 53 51 4c 69 74 65 20 70 72 69 6f 72 20  of SQLite prior 
17675 74 6f 0a 20 20 20 20 20 20 2a 2a 20 33 2e 36 2e  to.      ** 3.6.
17676 30 2c 20 64 61 74 61 62 61 73 65 73 20 77 69 74  0, databases wit
17677 68 20 66 72 65 65 6c 69 73 74 20 74 72 75 6e 6b  h freelist trunk
17678 20 70 61 67 65 73 20 68 6f 6c 64 69 6e 67 20 6d   pages holding m
17679 6f 72 65 20 74 68 61 6e 0a 20 20 20 20 20 20 2a  ore than.      *
1767a 2a 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d  * usableSize/4 -
1767b 20 38 20 65 6e 74 72 69 65 73 20 77 69 6c 6c 20   8 entries will 
1767c 62 65 20 72 65 70 6f 72 74 65 64 20 61 73 20 63  be reported as c
1767d 6f 72 72 75 70 74 2e 20 20 49 6e 20 6f 72 64 65  orrupt.  In orde
1767e 72 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 6d 61  r.      ** to ma
1767f 69 6e 74 61 69 6e 20 62 61 63 6b 77 61 72 64 73  intain backwards
17680 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77   compatibility w
17681 69 74 68 20 6f 6c 64 65 72 20 76 65 72 73 69 6f  ith older versio
17682 6e 73 20 6f 66 20 53 51 4c 69 74 65 2c 0a 20 20  ns of SQLite,.  
17683 20 20 20 20 2a 2a 20 77 65 20 77 69 6c 6c 20 63      ** we will c
17684 6f 6e 74 69 6e 75 65 20 74 6f 20 72 65 73 74 72  ontinue to restr
17685 69 63 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ict the number o
17686 66 20 65 6e 74 72 69 65 73 20 74 6f 20 75 73 61  f entries to usa
17687 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 0a 20 20  bleSize/4 - 8.  
17688 20 20 20 20 2a 2a 20 66 6f 72 20 6e 6f 77 2e 20      ** for now. 
17689 20 41 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69   At some point i
1768a 6e 20 74 68 65 20 66 75 74 75 72 65 20 28 6f 6e  n the future (on
1768b 63 65 20 65 76 65 72 79 6f 6e 65 20 68 61 73 20  ce everyone has 
1768c 75 70 67 72 61 64 65 64 0a 20 20 20 20 20 20 2a  upgraded.      *
1768d 2a 20 74 6f 20 33 2e 36 2e 30 20 6f 72 20 6c 61  * to 3.6.0 or la
1768e 74 65 72 29 20 77 65 20 73 68 6f 75 6c 64 20 63  ter) we should c
1768f 6f 6e 73 69 64 65 72 20 66 69 78 69 6e 67 20 74  onsider fixing t
17690 68 65 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 61  he conditional a
17691 62 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f  bove.      ** to
17692 20 72 65 61 64 20 22 75 73 61 62 6c 65 53 69 7a   read "usableSiz
17693 65 2f 34 2d 32 22 20 69 6e 73 74 65 61 64 20 6f  e/4-2" instead o
17694 66 20 22 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d  f "usableSize/4-
17695 38 22 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  8"..      */.   
17696 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
17697 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b  agerWrite(pTrunk
17698 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
17699 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1769a 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70  _OK ){.        p
1769b 75 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d  ut4byte(&pTrunk-
1769c 3e 61 44 61 74 61 5b 34 5d 2c 20 6e 4c 65 61 66  >aData[4], nLeaf
1769d 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74  +1);.        put
1769e 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61  4byte(&pTrunk->a
1769f 44 61 74 61 5b 38 2b 6e 4c 65 61 66 2a 34 5d 2c  Data[8+nLeaf*4],
176a0 20 69 50 61 67 65 29 3b 0a 23 69 66 6e 64 65 66   iPage);.#ifndef
176a1 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44   SQLITE_SECURE_D
176a2 45 4c 45 54 45 0a 20 20 20 20 20 20 20 20 69 66  ELETE.        if
176a3 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20  ( pPage ){.     
176a4 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
176a5 72 44 6f 6e 74 57 72 69 74 65 28 70 50 61 67 65  rDontWrite(pPage
176a6 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
176a7 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
176a8 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 53       rc = btreeS
176a9 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74  etHasContent(pBt
176aa 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20  , iPage);.      
176ab 7d 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 22  }.      TRACE(("
176ac 46 52 45 45 2d 50 41 47 45 3a 20 25 64 20 6c 65  FREE-PAGE: %d le
176ad 61 66 20 6f 6e 20 74 72 75 6e 6b 20 70 61 67 65  af on trunk page
176ae 20 25 64 5c 6e 22 2c 70 50 61 67 65 2d 3e 70 67   %d\n",pPage->pg
176af 6e 6f 2c 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29  no,pTrunk->pgno)
176b0 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 72  );.      goto fr
176b1 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20  eepage_out;.    
176b2 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 63  }.  }..  /* If c
176b3 6f 6e 74 72 6f 6c 20 66 6c 6f 77 73 20 74 6f 20  ontrol flows to 
176b4 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e  this point, then
176b5 20 69 74 20 77 61 73 20 6e 6f 74 20 70 6f 73 73   it was not poss
176b6 69 62 6c 65 20 74 6f 20 61 64 64 20 74 68 65 0a  ible to add the.
176b7 20 20 2a 2a 20 74 68 65 20 70 61 67 65 20 62 65    ** the page be
176b8 69 6e 67 20 66 72 65 65 64 20 61 73 20 61 20 6c  ing freed as a l
176b9 65 61 66 20 70 61 67 65 20 6f 66 20 74 68 65 20  eaf page of the 
176ba 66 69 72 73 74 20 74 72 75 6e 6b 20 69 6e 20 74  first trunk in t
176bb 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a 20 20  he free-list..  
176bc 2a 2a 20 50 6f 73 73 69 62 6c 79 20 62 65 63 61  ** Possibly beca
176bd 75 73 65 20 74 68 65 20 66 72 65 65 2d 6c 69 73  use the free-lis
176be 74 20 69 73 20 65 6d 70 74 79 2c 20 6f 72 20 70  t is empty, or p
176bf 6f 73 73 69 62 6c 79 20 62 65 63 61 75 73 65 20  ossibly because 
176c0 74 68 65 20 0a 20 20 2a 2a 20 66 69 72 73 74 20  the .  ** first 
176c1 74 72 75 6e 6b 20 69 6e 20 74 68 65 20 66 72 65  trunk in the fre
176c2 65 2d 6c 69 73 74 20 69 73 20 66 75 6c 6c 2e 20  e-list is full. 
176c3 45 69 74 68 65 72 20 77 61 79 2c 20 74 68 65 20  Either way, the 
176c4 70 61 67 65 20 62 65 69 6e 67 20 66 72 65 65 64  page being freed
176c5 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 63 6f 6d  .  ** will becom
176c6 65 20 74 68 65 20 6e 65 77 20 66 69 72 73 74 20  e the new first 
176c7 74 72 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 68  trunk page in th
176c8 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a 20 20 2a  e free-list..  *
176c9 2f 0a 20 20 69 66 28 20 70 50 61 67 65 3d 3d 30  /.  if( pPage==0
176ca 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28   && SQLITE_OK!=(
176cb 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67  rc = btreeGetPag
176cc 65 28 70 42 74 2c 20 69 50 61 67 65 2c 20 26 70  e(pBt, iPage, &p
176cd 50 61 67 65 2c 20 30 29 29 20 29 7b 0a 20 20 20  Page, 0)) ){.   
176ce 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f   goto freepage_o
176cf 75 74 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73  ut;.  }.  rc = s
176d0 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
176d1 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
176d2 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
176d3 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74  TE_OK ){.    got
176d4 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a  o freepage_out;.
176d5 20 20 7d 0a 20 20 70 75 74 34 62 79 74 65 28 70    }.  put4byte(p
176d6 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 69 54 72  Page->aData, iTr
176d7 75 6e 6b 29 3b 0a 20 20 70 75 74 34 62 79 74 65  unk);.  put4byte
176d8 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 34  (&pPage->aData[4
176d9 5d 2c 20 30 29 3b 0a 20 20 70 75 74 34 62 79 74  ], 0);.  put4byt
176da 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61  e(&pPage1->aData
176db 5b 33 32 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20  [32], iPage);.  
176dc 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41 47  TRACE(("FREE-PAG
176dd 45 3a 20 25 64 20 6e 65 77 20 74 72 75 6e 6b 20  E: %d new trunk 
176de 70 61 67 65 20 72 65 70 6c 61 63 69 6e 67 20 25  page replacing %
176df 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e  d\n", pPage->pgn
176e0 6f 2c 20 69 54 72 75 6e 6b 29 29 3b 0a 0a 66 72  o, iTrunk));..fr
176e1 65 65 70 61 67 65 5f 6f 75 74 3a 0a 20 20 69 66  eepage_out:.  if
176e2 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70  ( pPage ){.    p
176e3 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30  Page->isInit = 0
176e4 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50  ;.  }.  releaseP
176e5 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65  age(pPage);.  re
176e6 6c 65 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b  leasePage(pTrunk
176e7 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
176e8 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72  }.static void fr
176e9 65 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a  eePage(MemPage *
176ea 70 50 61 67 65 2c 20 69 6e 74 20 2a 70 52 43 29  pPage, int *pRC)
176eb 7b 0a 20 20 69 66 28 20 28 2a 70 52 43 29 3d 3d  {.  if( (*pRC)==
176ec 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
176ed 20 2a 70 52 43 20 3d 20 66 72 65 65 50 61 67 65   *pRC = freePage
176ee 32 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 70 50  2(pPage->pBt, pP
176ef 61 67 65 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f  age, pPage->pgno
176f0 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
176f1 46 72 65 65 20 61 6e 79 20 6f 76 65 72 66 6c 6f  Free any overflo
176f2 77 20 70 61 67 65 73 20 61 73 73 6f 63 69 61 74  w pages associat
176f3 65 64 20 77 69 74 68 20 74 68 65 20 67 69 76 65  ed with the give
176f4 6e 20 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69  n Cell..*/.stati
176f5 63 20 69 6e 74 20 63 6c 65 61 72 43 65 6c 6c 28  c int clearCell(
176f6 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20  MemPage *pPage, 
176f7 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
176f8 43 65 6c 6c 29 7b 0a 20 20 42 74 53 68 61 72 65  Cell){.  BtShare
176f9 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e  d *pBt = pPage->
176fa 70 42 74 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20  pBt;.  CellInfo 
176fb 69 6e 66 6f 3b 0a 20 20 50 67 6e 6f 20 6f 76 66  info;.  Pgno ovf
176fc 6c 50 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 3b  lPgno;.  int rc;
176fd 0a 20 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a 20 20  .  int nOvfl;.  
176fe 75 31 36 20 6f 76 66 6c 50 61 67 65 53 69 7a 65  u16 ovflPageSize
176ff 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
17700 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
17701 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65  pPage->pBt->mute
17702 78 29 20 29 3b 0a 20 20 62 74 72 65 65 50 61 72  x) );.  btreePar
17703 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c  seCellPtr(pPage,
17704 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a   pCell, &info);.
17705 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72    if( info.iOver
17706 66 6c 6f 77 3d 3d 30 20 29 7b 0a 20 20 20 20 72  flow==0 ){.    r
17707 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
17708 20 20 2f 2a 20 4e 6f 20 6f 76 65 72 66 6c 6f 77    /* No overflow
17709 20 70 61 67 65 73 2e 20 52 65 74 75 72 6e 20 77   pages. Return w
1770a 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79  ithout doing any
1770b 74 68 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 6f  thing */.  }.  o
1770c 76 66 6c 50 67 6e 6f 20 3d 20 67 65 74 34 62 79  vflPgno = get4by
1770d 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69  te(&pCell[info.i
1770e 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 61 73  Overflow]);.  as
1770f 73 65 72 74 28 20 70 42 74 2d 3e 75 73 61 62 6c  sert( pBt->usabl
17710 65 53 69 7a 65 20 3e 20 34 20 29 3b 0a 20 20 6f  eSize > 4 );.  o
17711 76 66 6c 50 61 67 65 53 69 7a 65 20 3d 20 70 42  vflPageSize = pB
17712 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20  t->usableSize - 
17713 34 3b 0a 20 20 6e 4f 76 66 6c 20 3d 20 28 69 6e  4;.  nOvfl = (in
17714 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 2d 20 69 6e  fo.nPayload - in
17715 66 6f 2e 6e 4c 6f 63 61 6c 20 2b 20 6f 76 66 6c  fo.nLocal + ovfl
17716 50 61 67 65 53 69 7a 65 20 2d 20 31 29 2f 6f 76  PageSize - 1)/ov
17717 66 6c 50 61 67 65 53 69 7a 65 3b 0a 20 20 61 73  flPageSize;.  as
17718 73 65 72 74 28 20 6f 76 66 6c 50 67 6e 6f 3d 3d  sert( ovflPgno==
17719 30 20 7c 7c 20 6e 4f 76 66 6c 3e 30 20 29 3b 0a  0 || nOvfl>0 );.
1771a 20 20 77 68 69 6c 65 28 20 6e 4f 76 66 6c 2d 2d    while( nOvfl--
1771b 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 69 4e 65   ){.    Pgno iNe
1771c 78 74 20 3d 20 30 3b 0a 20 20 20 20 4d 65 6d 50  xt = 0;.    MemP
1771d 61 67 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a  age *pOvfl = 0;.
1771e 20 20 20 20 69 66 28 20 6f 76 66 6c 50 67 6e 6f      if( ovflPgno
1771f 3c 32 20 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e 70  <2 || ovflPgno>p
17720 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42  agerPagecount(pB
17721 74 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 30  t) ){.      /* 0
17722 20 69 73 20 6e 6f 74 20 61 20 6c 65 67 61 6c 20   is not a legal 
17723 70 61 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 20  page number and 
17724 70 61 67 65 20 31 20 63 61 6e 6e 6f 74 20 62 65  page 1 cannot be
17725 20 61 6e 20 0a 20 20 20 20 20 20 2a 2a 20 6f 76   an .      ** ov
17726 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 54 68 65  erflow page. The
17727 72 65 66 6f 72 65 20 69 66 20 6f 76 66 6c 50 67  refore if ovflPg
17728 6e 6f 3c 32 20 6f 72 20 70 61 73 74 20 74 68 65  no<2 or past the
17729 20 65 6e 64 20 6f 66 20 74 68 65 20 0a 20 20 20   end of the .   
1772a 20 20 20 2a 2a 20 66 69 6c 65 20 74 68 65 20 64     ** file the d
1772b 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 20  atabase must be 
1772c 63 6f 72 72 75 70 74 2e 20 2a 2f 0a 20 20 20 20  corrupt. */.    
1772d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1772e 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
1772f 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 4f 76 66    }.    if( nOvf
17730 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  l ){.      rc = 
17731 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28  getOverflowPage(
17732 70 42 74 2c 20 6f 76 66 6c 50 67 6e 6f 2c 20 26  pBt, ovflPgno, &
17733 70 4f 76 66 6c 2c 20 26 69 4e 65 78 74 29 3b 0a  pOvfl, &iNext);.
17734 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72        if( rc ) r
17735 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
17736 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 67      rc = freePag
17737 65 32 28 70 42 74 2c 20 70 4f 76 66 6c 2c 20 6f  e2(pBt, pOvfl, o
17738 76 66 6c 50 67 6e 6f 29 3b 0a 20 20 20 20 69 66  vflPgno);.    if
17739 28 20 70 4f 76 66 6c 20 29 7b 0a 20 20 20 20 20  ( pOvfl ){.     
1773a 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
1773b 65 66 28 70 4f 76 66 6c 2d 3e 70 44 62 50 61 67  ef(pOvfl->pDbPag
1773c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  e);.    }.    if
1773d 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
1773e 3b 0a 20 20 20 20 6f 76 66 6c 50 67 6e 6f 20 3d  ;.    ovflPgno =
1773f 20 69 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65   iNext;.  }.  re
17740 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
17741 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  }../*.** Create 
17742 74 68 65 20 62 79 74 65 20 73 65 71 75 65 6e 63  the byte sequenc
17743 65 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73  e used to repres
17744 65 6e 74 20 61 20 63 65 6c 6c 20 6f 6e 20 70 61  ent a cell on pa
17745 67 65 20 70 50 61 67 65 0a 2a 2a 20 61 6e 64 20  ge pPage.** and 
17746 77 72 69 74 65 20 74 68 61 74 20 62 79 74 65 20  write that byte 
17747 73 65 71 75 65 6e 63 65 20 69 6e 74 6f 20 70 43  sequence into pC
17748 65 6c 6c 5b 5d 2e 20 20 4f 76 65 72 66 6c 6f 77  ell[].  Overflow
17749 20 70 61 67 65 73 20 61 72 65 0a 2a 2a 20 61 6c   pages are.** al
1774a 6c 6f 63 61 74 65 64 20 61 6e 64 20 66 69 6c 6c  located and fill
1774b 65 64 20 69 6e 20 61 73 20 6e 65 63 65 73 73 61  ed in as necessa
1774c 72 79 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67  ry.  The calling
1774d 20 70 72 6f 63 65 64 75 72 65 0a 2a 2a 20 69 73   procedure.** is
1774e 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
1774f 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 73 75 66   making sure suf
17750 66 69 63 69 65 6e 74 20 73 70 61 63 65 20 68 61  ficient space ha
17751 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64  s been allocated
17752 0a 2a 2a 20 66 6f 72 20 70 43 65 6c 6c 5b 5d 2e  .** for pCell[].
17753 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  .**.** Note that
17754 20 70 43 65 6c 6c 20 64 6f 65 73 20 6e 6f 74 20   pCell does not 
17755 6e 65 63 65 73 73 61 72 79 20 6e 65 65 64 20 74  necessary need t
17756 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 70  o point to the p
17757 50 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 61  Page->aData.** a
17758 72 65 61 2e 20 20 70 43 65 6c 6c 20 6d 69 67 68  rea.  pCell migh
17759 74 20 70 6f 69 6e 74 20 74 6f 20 73 6f 6d 65 20  t point to some 
1775a 74 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67  temporary storag
1775b 65 2e 20 20 54 68 65 20 63 65 6c 6c 20 77 69 6c  e.  The cell wil
1775c 6c 0a 2a 2a 20 62 65 20 63 6f 6e 73 74 72 75 63  l.** be construc
1775d 74 65 64 20 69 6e 20 74 68 69 73 20 74 65 6d 70  ted in this temp
1775e 6f 72 61 72 79 20 61 72 65 61 20 74 68 65 6e 20  orary area then 
1775f 63 6f 70 69 65 64 20 69 6e 74 6f 20 70 50 61 67  copied into pPag
17760 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 6c 61 74 65  e->aData.** late
17761 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
17762 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 0a 20 20 4d   fillInCell(.  M
17763 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20  emPage *pPage,  
17764 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
17765 20 54 68 65 20 70 61 67 65 20 74 68 61 74 20 63   The page that c
17766 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 65 6c 6c  ontains the cell
17767 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   */.  unsigned c
17768 68 61 72 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20  har *pCell,     
17769 20 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65       /* Complete
1776a 20 74 65 78 74 20 6f 66 20 74 68 65 20 63 65 6c   text of the cel
1776b 6c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69  l */.  const voi
1776c 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65  d *pKey, i64 nKe
1776d 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79  y,    /* The key
1776e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
1776f 20 2a 70 44 61 74 61 2c 69 6e 74 20 6e 44 61 74   *pData,int nDat
17770 61 2c 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61  a,   /* The data
17771 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f 2c   */.  int nZero,
17772 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17773 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 7a 65       /* Extra ze
17774 72 6f 20 62 79 74 65 73 20 74 6f 20 61 70 70 65  ro bytes to appe
17775 6e 64 20 74 6f 20 70 44 61 74 61 20 2a 2f 0a 20  nd to pData */. 
17776 20 69 6e 74 20 2a 70 6e 53 69 7a 65 20 20 20 20   int *pnSize    
17777 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17778 2f 2a 20 57 72 69 74 65 20 63 65 6c 6c 20 73 69  /* Write cell si
17779 7a 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  ze here */.){.  
1777a 69 6e 74 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20  int nPayload;.  
1777b 63 6f 6e 73 74 20 75 38 20 2a 70 53 72 63 3b 0a  const u8 *pSrc;.
1777c 20 20 69 6e 74 20 6e 53 72 63 2c 20 6e 2c 20 72    int nSrc, n, r
1777d 63 3b 0a 20 20 69 6e 74 20 73 70 61 63 65 4c 65  c;.  int spaceLe
1777e 66 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  ft;.  MemPage *p
1777f 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 4d 65 6d 50  Ovfl = 0;.  MemP
17780 61 67 65 20 2a 70 54 6f 52 65 6c 65 61 73 65 20  age *pToRelease 
17781 3d 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  = 0;.  unsigned 
17782 63 68 61 72 20 2a 70 50 72 69 6f 72 3b 0a 20 20  char *pPrior;.  
17783 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
17784 50 61 79 6c 6f 61 64 3b 0a 20 20 42 74 53 68 61  Payload;.  BtSha
17785 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65  red *pBt = pPage
17786 2d 3e 70 42 74 3b 0a 20 20 50 67 6e 6f 20 70 67  ->pBt;.  Pgno pg
17787 6e 6f 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 69 6e  noOvfl = 0;.  in
17788 74 20 6e 48 65 61 64 65 72 3b 0a 20 20 43 65 6c  t nHeader;.  Cel
17789 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 61  lInfo info;..  a
1778a 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1778b 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d  utex_held(pPage-
1778c 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  >pBt->mutex) );.
1778d 0a 20 20 2f 2a 20 70 50 61 67 65 20 69 73 20 6e  .  /* pPage is n
1778e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 77  ot necessarily w
1778f 72 69 74 65 61 62 6c 65 20 73 69 6e 63 65 20 70  riteable since p
17790 43 65 6c 6c 20 6d 69 67 68 74 20 62 65 20 61 75  Cell might be au
17791 78 69 6c 69 61 72 79 0a 20 20 2a 2a 20 62 75 66  xiliary.  ** buf
17792 66 65 72 20 73 70 61 63 65 20 74 68 61 74 20 69  fer space that i
17793 73 20 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20  s separate from 
17794 74 68 65 20 70 50 61 67 65 20 62 75 66 66 65 72  the pPage buffer
17795 20 61 72 65 61 20 2a 2f 0a 20 20 61 73 73 65 72   area */.  asser
17796 74 28 20 70 43 65 6c 6c 3c 70 50 61 67 65 2d 3e  t( pCell<pPage->
17797 61 44 61 74 61 20 7c 7c 20 70 43 65 6c 6c 3e 3d  aData || pCell>=
17798 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42  &pPage->aData[pB
17799 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 20  t->pageSize].   
1779a 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69           || sqli
1779b 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
1779c 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ble(pPage->pDbPa
1779d 67 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c  ge) );..  /* Fil
1779e 6c 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e  l in the header.
1779f 20 2a 2f 0a 20 20 6e 48 65 61 64 65 72 20 3d 20   */.  nHeader = 
177a0 30 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d  0;.  if( !pPage-
177a1 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 6e 48 65  >leaf ){.    nHe
177a2 61 64 65 72 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20  ader += 4;.  }. 
177a3 20 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44   if( pPage->hasD
177a4 61 74 61 20 29 7b 0a 20 20 20 20 6e 48 65 61 64  ata ){.    nHead
177a5 65 72 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 28  er += putVarint(
177a6 26 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c  &pCell[nHeader],
177a7 20 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 3b 0a 20   nData+nZero);. 
177a8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 44 61 74   }else{.    nDat
177a9 61 20 3d 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20  a = nZero = 0;. 
177aa 20 7d 0a 20 20 6e 48 65 61 64 65 72 20 2b 3d 20   }.  nHeader += 
177ab 70 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c  putVarint(&pCell
177ac 5b 6e 48 65 61 64 65 72 5d 2c 20 2a 28 75 36 34  [nHeader], *(u64
177ad 2a 29 26 6e 4b 65 79 29 3b 0a 20 20 62 74 72 65  *)&nKey);.  btre
177ae 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50  eParseCellPtr(pP
177af 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66  age, pCell, &inf
177b0 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e  o);.  assert( in
177b1 66 6f 2e 6e 48 65 61 64 65 72 3d 3d 6e 48 65 61  fo.nHeader==nHea
177b2 64 65 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28  der );.  assert(
177b3 20 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b 65 79   info.nKey==nKey
177b4 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e   );.  assert( in
177b5 66 6f 2e 6e 44 61 74 61 3d 3d 28 75 33 32 29 28  fo.nData==(u32)(
177b6 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 20 29 3b 0a  nData+nZero) );.
177b7 20 20 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20    .  /* Fill in 
177b8 74 68 65 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20  the payload */. 
177b9 20 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 44 61 74   nPayload = nDat
177ba 61 20 2b 20 6e 5a 65 72 6f 3b 0a 20 20 69 66 28  a + nZero;.  if(
177bb 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29   pPage->intKey )
177bc 7b 0a 20 20 20 20 70 53 72 63 20 3d 20 70 44 61  {.    pSrc = pDa
177bd 74 61 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 6e  ta;.    nSrc = n
177be 44 61 74 61 3b 0a 20 20 20 20 6e 44 61 74 61 20  Data;.    nData 
177bf 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a 20  = 0;.  }else{ . 
177c0 20 20 20 69 66 28 20 4e 45 56 45 52 28 6e 4b 65     if( NEVER(nKe
177c1 79 3e 30 78 37 66 66 66 66 66 66 66 20 7c 7c 20  y>0x7fffffff || 
177c2 70 4b 65 79 3d 3d 30 29 20 29 7b 0a 20 20 20 20  pKey==0) ){.    
177c3 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
177c4 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
177c5 20 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64    }.    nPayload
177c6 20 2b 3d 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20   += (int)nKey;. 
177c7 20 20 20 70 53 72 63 20 3d 20 70 4b 65 79 3b 0a     pSrc = pKey;.
177c8 20 20 20 20 6e 53 72 63 20 3d 20 28 69 6e 74 29      nSrc = (int)
177c9 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e 53  nKey;.  }.  *pnS
177ca 69 7a 65 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65  ize = info.nSize
177cb 3b 0a 20 20 73 70 61 63 65 4c 65 66 74 20 3d 20  ;.  spaceLeft = 
177cc 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 70  info.nLocal;.  p
177cd 50 61 79 6c 6f 61 64 20 3d 20 26 70 43 65 6c 6c  Payload = &pCell
177ce 5b 6e 48 65 61 64 65 72 5d 3b 0a 20 20 70 50 72  [nHeader];.  pPr
177cf 69 6f 72 20 3d 20 26 70 43 65 6c 6c 5b 69 6e 66  ior = &pCell[inf
177d0 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a 20  o.iOverflow];.. 
177d1 20 77 68 69 6c 65 28 20 6e 50 61 79 6c 6f 61 64   while( nPayload
177d2 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 70  >0 ){.    if( sp
177d3 61 63 65 4c 65 66 74 3d 3d 30 20 29 7b 0a 23 69  aceLeft==0 ){.#i
177d4 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
177d5 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
177d6 20 20 20 50 67 6e 6f 20 70 67 6e 6f 50 74 72 6d     Pgno pgnoPtrm
177d7 61 70 20 3d 20 70 67 6e 6f 4f 76 66 6c 3b 20 2f  ap = pgnoOvfl; /
177d8 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  * Overflow page 
177d9 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72  pointer-map entr
177da 79 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20  y page */.      
177db 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  if( pBt->autoVac
177dc 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 64  uum ){.        d
177dd 6f 7b 0a 20 20 20 20 20 20 20 20 20 20 70 67 6e  o{.          pgn
177de 6f 4f 76 66 6c 2b 2b 3b 0a 20 20 20 20 20 20 20  oOvfl++;.       
177df 20 7d 20 77 68 69 6c 65 28 20 0a 20 20 20 20 20   } while( .     
177e0 20 20 20 20 20 50 54 52 4d 41 50 5f 49 53 50 41       PTRMAP_ISPA
177e1 47 45 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c  GE(pBt, pgnoOvfl
177e2 29 20 7c 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d 50  ) || pgnoOvfl==P
177e3 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
177e4 28 70 42 74 29 20 0a 20 20 20 20 20 20 20 20 29  (pBt) .        )
177e5 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  ;.      }.#endif
177e6 0a 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f  .      rc = allo
177e7 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42  cateBtreePage(pB
177e8 74 2c 20 26 70 4f 76 66 6c 2c 20 26 70 67 6e 6f  t, &pOvfl, &pgno
177e9 4f 76 66 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20  Ovfl, pgnoOvfl, 
177ea 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  0);.#ifndef SQLI
177eb 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
177ec 55 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74  UM.      /* If t
177ed 68 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70  he database supp
177ee 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d  orts auto-vacuum
177ef 2c 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64  , and the second
177f0 20 6f 72 20 73 75 62 73 65 71 75 65 6e 74 0a 20   or subsequent. 
177f1 20 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77       ** overflow
177f2 20 70 61 67 65 20 69 73 20 62 65 69 6e 67 20 61   page is being a
177f3 6c 6c 6f 63 61 74 65 64 2c 20 61 64 64 20 61 6e  llocated, add an
177f4 20 65 6e 74 72 79 20 74 6f 20 74 68 65 20 70 6f   entry to the po
177f5 69 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 20 20  inter-map.      
177f6 2a 2a 20 66 6f 72 20 74 68 61 74 20 70 61 67 65  ** for that page
177f7 20 6e 6f 77 2e 20 0a 20 20 20 20 20 20 2a 2a 0a   now. .      **.
177f8 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73        ** If this
177f9 20 69 73 20 74 68 65 20 66 69 72 73 74 20 6f 76   is the first ov
177fa 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65  erflow page, the
177fb 6e 20 77 72 69 74 65 20 61 20 70 61 72 74 69 61  n write a partia
177fc 6c 20 65 6e 74 72 79 20 0a 20 20 20 20 20 20 2a  l entry .      *
177fd 2a 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72  * to the pointer
177fe 2d 6d 61 70 2e 20 49 66 20 77 65 20 77 72 69 74  -map. If we writ
177ff 65 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 69  e nothing to thi
17800 73 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 73 6c  s pointer-map sl
17801 6f 74 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  ot,.      ** the
17802 6e 20 74 68 65 20 6f 70 74 69 6d 69 73 74 69 63  n the optimistic
17803 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 20   overflow chain 
17804 70 72 6f 63 65 73 73 69 6e 67 20 69 6e 20 63 6c  processing in cl
17805 65 61 72 43 65 6c 6c 28 29 0a 20 20 20 20 20 20  earCell().      
17806 2a 2a 20 6d 61 79 20 6d 69 73 69 6e 74 65 72 70  ** may misinterp
17807 72 65 74 20 74 68 65 20 75 6e 69 6e 69 74 69 61  ret the uninitia
17808 6c 69 73 65 64 20 76 61 6c 75 65 73 20 61 6e 64  lised values and
17809 20 64 65 6c 65 74 65 20 74 68 65 0a 20 20 20 20   delete the.    
1780a 20 20 2a 2a 20 77 72 6f 6e 67 20 70 61 67 65 73    ** wrong pages
1780b 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61   from the databa
1780c 73 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  se..      */.   
1780d 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f     if( pBt->auto
1780e 56 61 63 75 75 6d 20 26 26 20 72 63 3d 3d 53 51  Vacuum && rc==SQ
1780f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
17810 20 20 20 75 38 20 65 54 79 70 65 20 3d 20 28 70     u8 eType = (p
17811 67 6e 6f 50 74 72 6d 61 70 3f 50 54 52 4d 41 50  gnoPtrmap?PTRMAP
17812 5f 4f 56 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41  _OVERFLOW2:PTRMA
17813 50 5f 4f 56 45 52 46 4c 4f 57 31 29 3b 0a 20 20  P_OVERFLOW1);.  
17814 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28        ptrmapPut(
17815 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 65  pBt, pgnoOvfl, e
17816 54 79 70 65 2c 20 70 67 6e 6f 50 74 72 6d 61 70  Type, pgnoPtrmap
17817 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20  , &rc);.        
17818 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
17819 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
1781a 70 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 20 20  pOvfl);.        
1781b 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  }.      }.#endif
1781c 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b  .      if( rc ){
1781d 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
1781e 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29  Page(pToRelease)
1781f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
17820 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20   rc;.      }..  
17821 20 20 20 20 2f 2a 20 49 66 20 70 54 6f 52 65 6c      /* If pToRel
17822 65 61 73 65 20 69 73 20 6e 6f 74 20 7a 65 72 6f  ease is not zero
17823 20 74 68 61 6e 20 70 50 72 69 6f 72 20 70 6f 69   than pPrior poi
17824 6e 74 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74  nts into the dat
17825 61 20 61 72 65 61 0a 20 20 20 20 20 20 2a 2a 20  a area.      ** 
17826 6f 66 20 70 54 6f 52 65 6c 65 61 73 65 2e 20 20  of pToRelease.  
17827 4d 61 6b 65 20 73 75 72 65 20 70 54 6f 52 65 6c  Make sure pToRel
17828 65 61 73 65 20 69 73 20 73 74 69 6c 6c 20 77 72  ease is still wr
17829 69 74 65 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20  iteable. */.    
1782a 20 20 61 73 73 65 72 74 28 20 70 54 6f 52 65 6c    assert( pToRel
1782b 65 61 73 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ease==0 || sqlit
1782c 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
1782d 6c 65 28 70 54 6f 52 65 6c 65 61 73 65 2d 3e 70  le(pToRelease->p
1782e 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20  DbPage) );..    
1782f 20 20 2f 2a 20 49 66 20 70 50 72 69 6f 72 20 69    /* If pPrior i
17830 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61  s part of the da
17831 74 61 20 61 72 65 61 20 6f 66 20 70 50 61 67 65  ta area of pPage
17832 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 72 65  , then make sure
17833 20 70 50 61 67 65 0a 20 20 20 20 20 20 2a 2a 20   pPage.      ** 
17834 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62  is still writeab
17835 6c 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65  le */.      asse
17836 72 74 28 20 70 50 72 69 6f 72 3c 70 50 61 67 65  rt( pPrior<pPage
17837 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50 72 69 6f  ->aData || pPrio
17838 72 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 61  r>=&pPage->aData
17839 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a  [pBt->pageSize].
1783a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73              || s
1783b 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
1783c 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44  teable(pPage->pD
1783d 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20  bPage) );..     
1783e 20 70 75 74 34 62 79 74 65 28 70 50 72 69 6f 72   put4byte(pPrior
1783f 2c 20 70 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20 20  , pgnoOvfl);.   
17840 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
17841 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 20 20  ToRelease);.    
17842 20 20 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 70    pToRelease = p
17843 4f 76 66 6c 3b 0a 20 20 20 20 20 20 70 50 72 69  Ovfl;.      pPri
17844 6f 72 20 3d 20 70 4f 76 66 6c 2d 3e 61 44 61 74  or = pOvfl->aDat
17845 61 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74  a;.      put4byt
17846 65 28 70 50 72 69 6f 72 2c 20 30 29 3b 0a 20 20  e(pPrior, 0);.  
17847 20 20 20 20 70 50 61 79 6c 6f 61 64 20 3d 20 26      pPayload = &
17848 70 4f 76 66 6c 2d 3e 61 44 61 74 61 5b 34 5d 3b  pOvfl->aData[4];
17849 0a 20 20 20 20 20 20 73 70 61 63 65 4c 65 66 74  .      spaceLeft
1784a 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69   = pBt->usableSi
1784b 7a 65 20 2d 20 34 3b 0a 20 20 20 20 7d 0a 20 20  ze - 4;.    }.  
1784c 20 20 6e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a    n = nPayload;.
1784d 20 20 20 20 69 66 28 20 6e 3e 73 70 61 63 65 4c      if( n>spaceL
1784e 65 66 74 20 29 20 6e 20 3d 20 73 70 61 63 65 4c  eft ) n = spaceL
1784f 65 66 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  eft;..    /* If 
17850 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f  pToRelease is no
17851 74 20 7a 65 72 6f 20 74 68 61 6e 20 70 50 61 79  t zero than pPay
17852 6c 6f 61 64 20 70 6f 69 6e 74 73 20 69 6e 74 6f  load points into
17853 20 74 68 65 20 64 61 74 61 20 61 72 65 61 0a 20   the data area. 
17854 20 20 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65     ** of pToRele
17855 61 73 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20  ase.  Make sure 
17856 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74  pToRelease is st
17857 69 6c 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a  ill writeable. *
17858 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54  /.    assert( pT
17859 6f 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73  oRelease==0 || s
1785a 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
1785b 74 65 61 62 6c 65 28 70 54 6f 52 65 6c 65 61 73  teable(pToReleas
1785c 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a  e->pDbPage) );..
1785d 20 20 20 20 2f 2a 20 49 66 20 70 50 61 79 6c 6f      /* If pPaylo
1785e 61 64 20 69 73 20 70 61 72 74 20 6f 66 20 74 68  ad is part of th
1785f 65 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 70  e data area of p
17860 50 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20  Page, then make 
17861 73 75 72 65 20 70 50 61 67 65 0a 20 20 20 20 2a  sure pPage.    *
17862 2a 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65  * is still write
17863 61 62 6c 65 20 2a 2f 0a 20 20 20 20 61 73 73 65  able */.    asse
17864 72 74 28 20 70 50 61 79 6c 6f 61 64 3c 70 50 61  rt( pPayload<pPa
17865 67 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50 61  ge->aData || pPa
17866 79 6c 6f 61 64 3e 3d 26 70 50 61 67 65 2d 3e 61  yload>=&pPage->a
17867 44 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 69  Data[pBt->pageSi
17868 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ze].            
17869 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  || sqlite3PagerI
1786a 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65  swriteable(pPage
1786b 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20  ->pDbPage) );.. 
1786c 20 20 20 69 66 28 20 6e 53 72 63 3e 30 20 29 7b     if( nSrc>0 ){
1786d 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 6e 53 72  .      if( n>nSr
1786e 63 20 29 20 6e 20 3d 20 6e 53 72 63 3b 0a 20 20  c ) n = nSrc;.  
1786f 20 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63      assert( pSrc
17870 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   );.      memcpy
17871 28 70 50 61 79 6c 6f 61 64 2c 20 70 53 72 63 2c  (pPayload, pSrc,
17872 20 6e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a   n);.    }else{.
17873 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61        memset(pPa
17874 79 6c 6f 61 64 2c 20 30 2c 20 6e 29 3b 0a 20 20  yload, 0, n);.  
17875 20 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64    }.    nPayload
17876 20 2d 3d 20 6e 3b 0a 20 20 20 20 70 50 61 79 6c   -= n;.    pPayl
17877 6f 61 64 20 2b 3d 20 6e 3b 0a 20 20 20 20 70 53  oad += n;.    pS
17878 72 63 20 2b 3d 20 6e 3b 0a 20 20 20 20 6e 53 72  rc += n;.    nSr
17879 63 20 2d 3d 20 6e 3b 0a 20 20 20 20 73 70 61 63  c -= n;.    spac
1787a 65 4c 65 66 74 20 2d 3d 20 6e 3b 0a 20 20 20 20  eLeft -= n;.    
1787b 69 66 28 20 6e 53 72 63 3d 3d 30 20 29 7b 0a 20  if( nSrc==0 ){. 
1787c 20 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 61 74       nSrc = nDat
1787d 61 3b 0a 20 20 20 20 20 20 70 53 72 63 20 3d 20  a;.      pSrc = 
1787e 70 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d  pData;.    }.  }
1787f 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70  .  releasePage(p
17880 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 72 65  ToRelease);.  re
17881 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
17882 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20  }../*.** Remove 
17883 74 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 66 72  the i-th cell fr
17884 6f 6d 20 70 50 61 67 65 2e 20 20 54 68 69 73 20  om pPage.  This 
17885 72 6f 75 74 69 6e 65 20 65 66 66 65 63 74 73 20  routine effects 
17886 70 50 61 67 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54  pPage only..** T
17887 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  he cell content 
17888 69 73 20 6e 6f 74 20 66 72 65 65 64 20 6f 72 20  is not freed or 
17889 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 49 74  deallocated.  It
1788a 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74   is assumed that
1788b 0a 2a 2a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e  .** the cell con
1788c 74 65 6e 74 20 68 61 73 20 62 65 65 6e 20 63 6f  tent has been co
1788d 70 69 65 64 20 73 6f 6d 65 70 6c 61 63 65 20 65  pied someplace e
1788e 6c 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69  lse.  This routi
1788f 6e 65 20 6a 75 73 74 0a 2a 2a 20 72 65 6d 6f 76  ne just.** remov
17890 65 73 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  es the reference
17891 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 66 72 6f   to the cell fro
17892 6d 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 22  m pPage..**.** "
17893 73 7a 22 20 6d 75 73 74 20 62 65 20 74 68 65 20  sz" must be the 
17894 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
17895 69 6e 20 74 68 65 20 63 65 6c 6c 2e 0a 2a 2f 0a  in the cell..*/.
17896 73 74 61 74 69 63 20 76 6f 69 64 20 64 72 6f 70  static void drop
17897 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50  Cell(MemPage *pP
17898 61 67 65 2c 20 69 6e 74 20 69 64 78 2c 20 69 6e  age, int idx, in
17899 74 20 73 7a 2c 20 69 6e 74 20 2a 70 52 43 29 7b  t sz, int *pRC){
1789a 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
1789b 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
1789c 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20  er */.  int pc; 
1789d 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
1789e 74 20 74 6f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e  t to cell conten
1789f 74 20 6f 66 20 63 65 6c 6c 20 62 65 69 6e 67 20  t of cell being 
178a0 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 75 38 20  deleted */.  u8 
178a1 2a 64 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20  *data;       /* 
178a2 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a  pPage->aData */.
178a3 20 20 75 38 20 2a 70 74 72 3b 20 20 20 20 20 20    u8 *ptr;      
178a4 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6d 6f 76    /* Used to mov
178a5 65 20 62 79 74 65 73 20 61 72 6f 75 6e 64 20 77  e bytes around w
178a6 69 74 68 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a  ithin data[] */.
178a7 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
178a8 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20    /* The return 
178a9 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 68 64  code */.  int hd
178aa 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 65 67  r;        /* Beg
178ab 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 68 65  inning of the he
178ac 61 64 65 72 2e 20 20 30 20 6d 6f 73 74 20 70 61  ader.  0 most pa
178ad 67 65 73 2e 20 20 31 30 30 20 70 61 67 65 20 31  ges.  100 page 1
178ae 20 2a 2f 0a 0a 20 20 69 66 28 20 2a 70 52 43 20   */..  if( *pRC 
178af 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73  ) return;..  ass
178b0 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20 69  ert( idx>=0 && i
178b1 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20  dx<pPage->nCell 
178b2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 3d  );.  assert( sz=
178b3 3d 63 65 6c 6c 53 69 7a 65 28 70 50 61 67 65 2c  =cellSize(pPage,
178b4 20 69 64 78 29 20 29 3b 0a 20 20 61 73 73 65 72   idx) );.  asser
178b5 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
178b6 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65  swriteable(pPage
178b7 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
178b8 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
178b9 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
178ba 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
178bb 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d  .  data = pPage-
178bc 3e 61 44 61 74 61 3b 0a 20 20 70 74 72 20 3d 20  >aData;.  ptr = 
178bd 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 63 65 6c  &data[pPage->cel
178be 6c 4f 66 66 73 65 74 20 2b 20 32 2a 69 64 78 5d  lOffset + 2*idx]
178bf 3b 0a 20 20 70 63 20 3d 20 67 65 74 32 62 79 74  ;.  pc = get2byt
178c0 65 28 70 74 72 29 3b 0a 20 20 68 64 72 20 3d 20  e(ptr);.  hdr = 
178c1 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
178c2 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 63  ;.  testcase( pc
178c3 3d 3d 67 65 74 32 62 79 74 65 28 26 64 61 74 61  ==get2byte(&data
178c4 5b 68 64 72 2b 35 5d 29 20 29 3b 0a 20 20 74 65  [hdr+5]) );.  te
178c5 73 74 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d 70  stcase( pc+sz==p
178c6 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c  Page->pBt->usabl
178c7 65 53 69 7a 65 20 29 3b 0a 20 20 69 66 28 20 70  eSize );.  if( p
178c8 63 20 3c 20 67 65 74 32 62 79 74 65 28 26 64 61  c < get2byte(&da
178c9 74 61 5b 68 64 72 2b 35 5d 29 20 7c 7c 20 70 63  ta[hdr+5]) || pc
178ca 2b 73 7a 20 3e 20 70 50 61 67 65 2d 3e 70 42 74  +sz > pPage->pBt
178cb 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a  ->usableSize ){.
178cc 20 20 20 20 2a 70 52 43 20 3d 20 53 51 4c 49 54      *pRC = SQLIT
178cd 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
178ce 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
178cf 20 20 72 63 20 3d 20 66 72 65 65 53 70 61 63 65    rc = freeSpace
178d0 28 70 50 61 67 65 2c 20 70 63 2c 20 73 7a 29 3b  (pPage, pc, sz);
178d1 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20  .  if( rc ){.   
178d2 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20   *pRC = rc;.    
178d3 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f  return;.  }.  fo
178d4 72 28 69 3d 69 64 78 2b 31 3b 20 69 3c 70 50 61  r(i=idx+1; i<pPa
178d5 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 2c 20  ge->nCell; i++, 
178d6 70 74 72 2b 3d 32 29 7b 0a 20 20 20 20 70 74 72  ptr+=2){.    ptr
178d7 5b 30 5d 20 3d 20 70 74 72 5b 32 5d 3b 0a 20 20  [0] = ptr[2];.  
178d8 20 20 70 74 72 5b 31 5d 20 3d 20 70 74 72 5b 33    ptr[1] = ptr[3
178d9 5d 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e  ];.  }.  pPage->
178da 6e 43 65 6c 6c 2d 2d 3b 0a 20 20 70 75 74 32 62  nCell--;.  put2b
178db 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d  yte(&data[hdr+3]
178dc 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b  , pPage->nCell);
178dd 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20  .  pPage->nFree 
178de 2b 3d 20 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  += 2;.}../*.** I
178df 6e 73 65 72 74 20 61 20 6e 65 77 20 63 65 6c 6c  nsert a new cell
178e0 20 6f 6e 20 70 50 61 67 65 20 61 74 20 63 65 6c   on pPage at cel
178e1 6c 20 69 6e 64 65 78 20 22 69 22 2e 20 20 70 43  l index "i".  pC
178e2 65 6c 6c 20 70 6f 69 6e 74 73 20 74 6f 20 74 68  ell points to th
178e3 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20  e.** content of 
178e4 74 68 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20  the cell..**.** 
178e5 49 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  If the cell cont
178e6 65 6e 74 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20  ent will fit on 
178e7 74 68 65 20 70 61 67 65 2c 20 74 68 65 6e 20 70  the page, then p
178e8 75 74 20 69 74 20 74 68 65 72 65 2e 20 20 49 66  ut it there.  If
178e9 20 69 74 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20   it.** will not 
178ea 66 69 74 2c 20 74 68 65 6e 20 6d 61 6b 65 20 61  fit, then make a
178eb 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 65 6c   copy of the cel
178ec 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 70  l content into p
178ed 54 65 6d 70 20 69 66 0a 2a 2a 20 70 54 65 6d 70  Temp if.** pTemp
178ee 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 2e 20 20 52   is not null.  R
178ef 65 67 61 72 64 6c 65 73 73 20 6f 66 20 70 54 65  egardless of pTe
178f0 6d 70 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e  mp, allocate a n
178f1 65 77 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 70  ew entry.** in p
178f2 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 5d 20 61 6e  Page->aOvfl[] an
178f3 64 20 6d 61 6b 65 20 69 74 20 70 6f 69 6e 74 20  d make it point 
178f4 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  to the cell cont
178f5 65 6e 74 20 28 65 69 74 68 65 72 0a 2a 2a 20 69  ent (either.** i
178f6 6e 20 70 54 65 6d 70 20 6f 72 20 74 68 65 20 6f  n pTemp or the o
178f7 72 69 67 69 6e 61 6c 20 70 43 65 6c 6c 29 20 61  riginal pCell) a
178f8 6e 64 20 61 6c 73 6f 20 72 65 63 6f 72 64 20 69  nd also record i
178f9 74 73 20 69 6e 64 65 78 2e 20 0a 2a 2a 20 41 6c  ts index. .** Al
178fa 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 65  locating a new e
178fb 6e 74 72 79 20 69 6e 20 70 50 61 67 65 2d 3e 61  ntry in pPage->a
178fc 43 65 6c 6c 5b 5d 20 69 6d 70 6c 69 65 73 20 74  Cell[] implies t
178fd 68 61 74 20 0a 2a 2a 20 70 50 61 67 65 2d 3e 6e  hat .** pPage->n
178fe 4f 76 65 72 66 6c 6f 77 20 69 73 20 69 6e 63 72  Overflow is incr
178ff 65 6d 65 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  emented..**.** I
17900 66 20 6e 53 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a  f nSkip is non-z
17901 65 72 6f 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74  ero, then do not
17902 20 63 6f 70 79 20 74 68 65 20 66 69 72 73 74 20   copy the first 
17903 6e 53 6b 69 70 20 62 79 74 65 73 20 6f 66 20 74  nSkip bytes of t
17904 68 65 0a 2a 2a 20 63 65 6c 6c 2e 20 54 68 65 20  he.** cell. The 
17905 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6f 76 65 72  caller will over
17906 77 72 69 74 65 20 74 68 65 6d 20 61 66 74 65 72  write them after
17907 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   this function r
17908 65 74 75 72 6e 73 2e 20 49 66 0a 2a 2a 20 6e 53  eturns. If.** nS
17909 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  kip is non-zero,
1790a 20 74 68 65 6e 20 70 43 65 6c 6c 20 6d 61 79 20   then pCell may 
1790b 6e 6f 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20  not point to an 
1790c 69 6e 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 6c  invalid memory l
1790d 6f 63 61 74 69 6f 6e 20 0a 2a 2a 20 28 62 75 74  ocation .** (but
1790e 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 20 69 73 20   pCell+nSkip is 
1790f 61 6c 77 61 79 73 20 76 61 6c 69 64 29 2e 0a 2a  always valid)..*
17910 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e  /.static void in
17911 73 65 72 74 43 65 6c 6c 28 0a 20 20 4d 65 6d 50  sertCell(.  MemP
17912 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a  age *pPage,   /*
17913 20 50 61 67 65 20 69 6e 74 6f 20 77 68 69 63 68   Page into which
17914 20 77 65 20 61 72 65 20 63 6f 70 79 69 6e 67 20   we are copying 
17915 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20 20 20  */.  int i,     
17916 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 63 65         /* New ce
17917 6c 6c 20 62 65 63 6f 6d 65 73 20 74 68 65 20 69  ll becomes the i
17918 2d 74 68 20 63 65 6c 6c 20 6f 66 20 74 68 65 20  -th cell of the 
17919 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 43  page */.  u8 *pC
1791a 65 6c 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20 43  ell,        /* C
1791b 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 6e 65  ontent of the ne
1791c 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20  w cell */.  int 
1791d 73 7a 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  sz,           /*
1791e 20 42 79 74 65 73 20 6f 66 20 63 6f 6e 74 65 6e   Bytes of conten
1791f 74 20 69 6e 20 70 43 65 6c 6c 20 2a 2f 0a 20 20  t in pCell */.  
17920 75 38 20 2a 70 54 65 6d 70 2c 20 20 20 20 20 20  u8 *pTemp,      
17921 20 20 2f 2a 20 54 65 6d 70 20 73 74 6f 72 61 67    /* Temp storag
17922 65 20 73 70 61 63 65 20 66 6f 72 20 70 43 65 6c  e space for pCel
17923 6c 2c 20 69 66 20 6e 65 65 64 65 64 20 2a 2f 0a  l, if needed */.
17924 20 20 50 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20    Pgno iChild,  
17925 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65      /* If non-ze
17926 72 6f 2c 20 72 65 70 6c 61 63 65 20 66 69 72 73  ro, replace firs
17927 74 20 34 20 62 79 74 65 73 20 77 69 74 68 20 74  t 4 bytes with t
17928 68 69 73 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69  his value */.  i
17929 6e 74 20 2a 70 52 43 20 20 20 20 20 20 20 20 20  nt *pRC         
1792a 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72 69   /* Read and wri
1792b 74 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 66  te return code f
1792c 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  rom here */.){. 
1792d 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20   int idx;       
1792e 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 77     /* Where to w
1792f 72 69 74 65 20 6e 65 77 20 63 65 6c 6c 20 63 6f  rite new cell co
17930 6e 74 65 6e 74 20 69 6e 20 64 61 74 61 5b 5d 20  ntent in data[] 
17931 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 20  */.  int j;     
17932 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
17933 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  ounter */.  int 
17934 65 6e 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  end;          /*
17935 20 46 69 72 73 74 20 62 79 74 65 20 70 61 73 74   First byte past
17936 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 70   the last cell p
17937 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d  ointer in data[]
17938 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 73 3b 20 20   */.  int ins;  
17939 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
1793a 20 69 6e 20 64 61 74 61 5b 5d 20 77 68 65 72 65   in data[] where
1793b 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69 6e 74 65   new cell pointe
1793c 72 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f  r is inserted */
1793d 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65  .  int cellOffse
1793e 74 3b 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20  t;   /* Address 
1793f 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f  of first cell po
17940 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d 20  inter in data[] 
17941 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20  */.  u8 *data;  
17942 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f         /* The co
17943 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 77 68 6f  ntent of the who
17944 6c 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20  le page */.  u8 
17945 2a 70 74 72 3b 20 20 20 20 20 20 20 20 20 20 2f  *ptr;          /
17946 2a 20 55 73 65 64 20 66 6f 72 20 6d 6f 76 69 6e  * Used for movin
17947 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 72  g information ar
17948 6f 75 6e 64 20 69 6e 20 64 61 74 61 5b 5d 20 2a  ound in data[] *
17949 2f 0a 0a 20 20 69 6e 74 20 6e 53 6b 69 70 20 3d  /..  int nSkip =
1794a 20 28 69 43 68 69 6c 64 20 3f 20 34 20 3a 20 30   (iChild ? 4 : 0
1794b 29 3b 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 29  );..  if( *pRC )
1794c 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65   return;..  asse
1794d 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 3d 70  rt( i>=0 && i<=p
1794e 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 70 50 61 67  Page->nCell+pPag
1794f 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 3b 0a  e->nOverflow );.
17950 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
17951 3e 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c 28  >nCell<=MX_CELL(
17952 70 50 61 67 65 2d 3e 70 42 74 29 20 26 26 20 4d  pPage->pBt) && M
17953 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42  X_CELL(pPage->pB
17954 74 29 3c 3d 35 34 36 30 20 29 3b 0a 20 20 61 73  t)<=5460 );.  as
17955 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76  sert( pPage->nOv
17956 65 72 66 6c 6f 77 3c 3d 41 72 72 61 79 53 69 7a  erflow<=ArraySiz
17957 65 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 29 20  e(pPage->aOvfl) 
17958 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 3d  );.  assert( sz=
17959 3d 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61  =cellSizePtr(pPa
1795a 67 65 2c 20 70 43 65 6c 6c 29 20 29 3b 0a 20 20  ge, pCell) );.  
1795b 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1795c 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
1795d 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
1795e 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f  .  if( pPage->nO
1795f 76 65 72 66 6c 6f 77 20 7c 7c 20 73 7a 2b 32 3e  verflow || sz+2>
17960 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a  pPage->nFree ){.
17961 20 20 20 20 69 66 28 20 70 54 65 6d 70 20 29 7b      if( pTemp ){
17962 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 54  .      memcpy(pT
17963 65 6d 70 2b 6e 53 6b 69 70 2c 20 70 43 65 6c 6c  emp+nSkip, pCell
17964 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b 69 70  +nSkip, sz-nSkip
17965 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d  );.      pCell =
17966 20 70 54 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 20   pTemp;.    }.  
17967 20 20 69 66 28 20 69 43 68 69 6c 64 20 29 7b 0a    if( iChild ){.
17968 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 70        put4byte(p
17969 43 65 6c 6c 2c 20 69 43 68 69 6c 64 29 3b 0a 20  Cell, iChild);. 
1796a 20 20 20 7d 0a 20 20 20 20 6a 20 3d 20 70 50 61     }.    j = pPa
1796b 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2b 2b 3b  ge->nOverflow++;
1796c 0a 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c 28  .    assert( j<(
1796d 69 6e 74 29 28 73 69 7a 65 6f 66 28 70 50 61 67  int)(sizeof(pPag
1796e 65 2d 3e 61 4f 76 66 6c 29 2f 73 69 7a 65 6f 66  e->aOvfl)/sizeof
1796f 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d  (pPage->aOvfl[0]
17970 29 29 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d  )) );.    pPage-
17971 3e 61 4f 76 66 6c 5b 6a 5d 2e 70 43 65 6c 6c 20  >aOvfl[j].pCell 
17972 3d 20 70 43 65 6c 6c 3b 0a 20 20 20 20 70 50 61  = pCell;.    pPa
17973 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 69 64 78  ge->aOvfl[j].idx
17974 20 3d 20 28 75 31 36 29 69 3b 0a 20 20 7d 65 6c   = (u16)i;.  }el
17975 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d  se{.    int rc =
17976 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
17977 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  te(pPage->pDbPag
17978 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  e);.    if( rc!=
17979 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1797a 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20     *pRC = rc;.  
1797b 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
1797c 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71  }.    assert( sq
1797d 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
1797e 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62  eable(pPage->pDb
1797f 50 61 67 65 29 20 29 3b 0a 20 20 20 20 64 61 74  Page) );.    dat
17980 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61  a = pPage->aData
17981 3b 0a 20 20 20 20 63 65 6c 6c 4f 66 66 73 65 74  ;.    cellOffset
17982 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66   = pPage->cellOf
17983 66 73 65 74 3b 0a 20 20 20 20 65 6e 64 20 3d 20  fset;.    end = 
17984 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70  cellOffset + 2*p
17985 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20  Page->nCell;.   
17986 20 69 6e 73 20 3d 20 63 65 6c 6c 4f 66 66 73 65   ins = cellOffse
17987 74 20 2b 20 32 2a 69 3b 0a 20 20 20 20 72 63 20  t + 2*i;.    rc 
17988 3d 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 28  = allocateSpace(
17989 70 50 61 67 65 2c 20 73 7a 2c 20 26 69 64 78 29  pPage, sz, &idx)
1798a 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 20  ;.    if( rc ){ 
1798b 2a 70 52 43 20 3d 20 72 63 3b 20 72 65 74 75 72  *pRC = rc; retur
1798c 6e 3b 20 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20  n; }.    /* The 
1798d 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 29 20  allocateSpace() 
1798e 72 6f 75 74 69 6e 65 20 67 75 61 72 61 6e 74 65  routine guarante
1798f 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  es the following
17990 20 74 77 6f 20 70 72 6f 70 65 72 74 69 65 73 0a   two properties.
17991 20 20 20 20 2a 2a 20 69 66 20 69 74 20 72 65 74      ** if it ret
17992 75 72 6e 73 20 73 75 63 63 65 73 73 20 2a 2f 0a  urns success */.
17993 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 20      assert( idx 
17994 3e 3d 20 65 6e 64 2b 32 20 29 3b 0a 20 20 20 20  >= end+2 );.    
17995 61 73 73 65 72 74 28 20 69 64 78 2b 73 7a 20 3c  assert( idx+sz <
17996 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73  = pPage->pBt->us
17997 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20  ableSize );.    
17998 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 2b 3b 0a  pPage->nCell++;.
17999 20 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65      pPage->nFree
1799a 20 2d 3d 20 28 75 31 36 29 28 32 20 2b 20 73 7a   -= (u16)(2 + sz
1799b 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64  );.    memcpy(&d
1799c 61 74 61 5b 69 64 78 2b 6e 53 6b 69 70 5d 2c 20  ata[idx+nSkip], 
1799d 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d  pCell+nSkip, sz-
1799e 6e 53 6b 69 70 29 3b 0a 20 20 20 20 69 66 28 20  nSkip);.    if( 
1799f 69 43 68 69 6c 64 20 29 7b 0a 20 20 20 20 20 20  iChild ){.      
179a0 70 75 74 34 62 79 74 65 28 26 64 61 74 61 5b 69  put4byte(&data[i
179a1 64 78 5d 2c 20 69 43 68 69 6c 64 29 3b 0a 20 20  dx], iChild);.  
179a2 20 20 7d 0a 20 20 20 20 66 6f 72 28 6a 3d 65 6e    }.    for(j=en
179a3 64 2c 20 70 74 72 3d 26 64 61 74 61 5b 6a 5d 3b  d, ptr=&data[j];
179a4 20 6a 3e 69 6e 73 3b 20 6a 2d 3d 32 2c 20 70 74   j>ins; j-=2, pt
179a5 72 2d 3d 32 29 7b 0a 20 20 20 20 20 20 70 74 72  r-=2){.      ptr
179a6 5b 30 5d 20 3d 20 70 74 72 5b 2d 32 5d 3b 0a 20  [0] = ptr[-2];. 
179a7 20 20 20 20 20 70 74 72 5b 31 5d 20 3d 20 70 74       ptr[1] = pt
179a8 72 5b 2d 31 5d 3b 0a 20 20 20 20 7d 0a 20 20 20  r[-1];.    }.   
179a9 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b   put2byte(&data[
179aa 69 6e 73 5d 2c 20 69 64 78 29 3b 0a 20 20 20 20  ins], idx);.    
179ab 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  put2byte(&data[p
179ac 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b  Page->hdrOffset+
179ad 33 5d 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  3], pPage->nCell
179ae 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
179af 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
179b0 4d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d  M.    if( pPage-
179b1 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d  >pBt->autoVacuum
179b2 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65   ){.      /* The
179b3 20 63 65 6c 6c 20 6d 61 79 20 63 6f 6e 74 61 69   cell may contai
179b4 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  n a pointer to a
179b5 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e  n overflow page.
179b6 20 49 66 20 73 6f 2c 20 77 72 69 74 65 0a 20 20   If so, write.  
179b7 20 20 20 20 2a 2a 20 74 68 65 20 65 6e 74 72 79      ** the entry
179b8 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f   for the overflo
179b9 77 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20  w page into the 
179ba 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 20 20 20  pointer map..   
179bb 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 74 72 6d     */.      ptrm
179bc 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50 61  apPutOvflPtr(pPa
179bd 67 65 2c 20 70 43 65 6c 6c 2c 20 70 52 43 29 3b  ge, pCell, pRC);
179be 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
179bf 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61  }.}../*.** Add a
179c0 20 6c 69 73 74 20 6f 66 20 63 65 6c 6c 73 20 74   list of cells t
179c1 6f 20 61 20 70 61 67 65 2e 20 20 54 68 65 20 70  o a page.  The p
179c2 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 69 6e  age should be in
179c3 69 74 69 61 6c 6c 79 20 65 6d 70 74 79 2e 0a 2a  itially empty..*
179c4 2a 20 54 68 65 20 63 65 6c 6c 73 20 61 72 65 20  * The cells are 
179c5 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 66 69  guaranteed to fi
179c6 74 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a  t on the page..*
179c7 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 73  /.static void as
179c8 73 65 6d 62 6c 65 50 61 67 65 28 0a 20 20 4d 65  semblePage(.  Me
179c9 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20  mPage *pPage,   
179ca 2f 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20 62  /* The page to b
179cb 65 20 61 73 73 65 6d 62 6c 69 65 64 20 2a 2f 0a  e assemblied */.
179cc 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 20 20 20    int nCell,    
179cd 20 20 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65      /* The numbe
179ce 72 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 64  r of cells to ad
179cf 64 20 74 6f 20 74 68 69 73 20 70 61 67 65 20 2a  d to this page *
179d0 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c 2c  /.  u8 **apCell,
179d1 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
179d2 73 20 74 6f 20 63 65 6c 6c 20 62 6f 64 69 65 73  s to cell bodies
179d3 20 2a 2f 0a 20 20 75 31 36 20 2a 61 53 69 7a 65   */.  u16 *aSize
179d4 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 73          /* Sizes
179d5 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 2a 2f   of the cells */
179d6 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20  .){.  int i;    
179d7 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
179d8 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20  counter */.  u8 
179d9 2a 70 43 65 6c 6c 70 74 72 3b 20 20 20 20 20 2f  *pCellptr;     /
179da 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6e 65 78  * Address of nex
179db 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a  t cell pointer *
179dc 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 62 6f 64 79  /.  int cellbody
179dd 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73  ;     /* Address
179de 20 6f 66 20 6e 65 78 74 20 63 65 6c 6c 20 62 6f   of next cell bo
179df 64 79 20 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e  dy */.  u8 * con
179e0 73 74 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d  st data = pPage-
179e1 3e 61 44 61 74 61 3b 20 20 20 20 20 20 20 20 20  >aData;         
179e2 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
179e3 6f 20 64 61 74 61 20 66 6f 72 20 70 50 61 67 65  o data for pPage
179e4 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20   */.  const int 
179e5 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72  hdr = pPage->hdr
179e6 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20  Offset;         
179e7 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 68    /* Offset of h
179e8 65 61 64 65 72 20 6f 6e 20 70 50 61 67 65 20 2a  eader on pPage *
179e9 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 55  /.  const int nU
179ea 73 61 62 6c 65 20 3d 20 70 50 61 67 65 2d 3e 70  sable = pPage->p
179eb 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 20  Bt->usableSize; 
179ec 2f 2a 20 55 73 61 62 6c 65 20 73 69 7a 65 20 6f  /* Usable size o
179ed 66 20 70 61 67 65 20 2a 2f 0a 0a 20 20 61 73 73  f page */..  ass
179ee 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65  ert( pPage->nOve
179ef 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73  rflow==0 );.  as
179f0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
179f1 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
179f2 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
179f3 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3e 3d   assert( nCell>=
179f4 30 20 26 26 20 6e 43 65 6c 6c 3c 3d 4d 58 5f 43  0 && nCell<=MX_C
179f5 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 20  ELL(pPage->pBt) 
179f6 26 26 20 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65  && MX_CELL(pPage
179f7 2d 3e 70 42 74 29 3c 3d 35 34 36 30 20 29 3b 0a  ->pBt)<=5460 );.
179f8 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
179f9 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
179fa 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
179fb 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  ) );..  /* Check
179fc 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 68   that the page h
179fd 61 73 20 6a 75 73 74 20 62 65 65 6e 20 7a 65 72  as just been zer
179fe 6f 65 64 20 62 79 20 7a 65 72 6f 50 61 67 65 28  oed by zeroPage(
179ff 29 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  ) */.  assert( p
17a00 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29  Page->nCell==0 )
17a01 3b 0a 20 20 61 73 73 65 72 74 28 20 67 65 74 32  ;.  assert( get2
17a02 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
17a03 5d 29 3d 3d 6e 55 73 61 62 6c 65 20 29 3b 0a 0a  ])==nUsable );..
17a04 20 20 70 43 65 6c 6c 70 74 72 20 3d 20 26 64 61    pCellptr = &da
17a05 74 61 5b 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66  ta[pPage->cellOf
17a06 66 73 65 74 20 2b 20 6e 43 65 6c 6c 2a 32 5d 3b  fset + nCell*2];
17a07 0a 20 20 63 65 6c 6c 62 6f 64 79 20 3d 20 6e 55  .  cellbody = nU
17a08 73 61 62 6c 65 3b 0a 20 20 66 6f 72 28 69 3d 6e  sable;.  for(i=n
17a09 43 65 6c 6c 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d  Cell-1; i>=0; i-
17a0a 2d 29 7b 0a 20 20 20 20 70 43 65 6c 6c 70 74 72  -){.    pCellptr
17a0b 20 2d 3d 20 32 3b 0a 20 20 20 20 63 65 6c 6c 62   -= 2;.    cellb
17a0c 6f 64 79 20 2d 3d 20 61 53 69 7a 65 5b 69 5d 3b  ody -= aSize[i];
17a0d 0a 20 20 20 20 70 75 74 32 62 79 74 65 28 70 43  .    put2byte(pC
17a0e 65 6c 6c 70 74 72 2c 20 63 65 6c 6c 62 6f 64 79  ellptr, cellbody
17a0f 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64  );.    memcpy(&d
17a10 61 74 61 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20 61  ata[cellbody], a
17a11 70 43 65 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65 5b  pCell[i], aSize[
17a12 69 5d 29 3b 0a 20 20 7d 0a 20 20 70 75 74 32 62  i]);.  }.  put2b
17a13 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d  yte(&data[hdr+3]
17a14 2c 20 6e 43 65 6c 6c 29 3b 0a 20 20 70 75 74 32  , nCell);.  put2
17a15 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
17a16 5d 2c 20 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20  ], cellbody);.  
17a17 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20  pPage->nFree -= 
17a18 28 6e 43 65 6c 6c 2a 32 20 2b 20 6e 55 73 61 62  (nCell*2 + nUsab
17a19 6c 65 20 2d 20 63 65 6c 6c 62 6f 64 79 29 3b 0a  le - cellbody);.
17a1a 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d    pPage->nCell =
17a1b 20 28 75 31 36 29 6e 43 65 6c 6c 3b 0a 7d 0a 0a   (u16)nCell;.}..
17a1c 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
17a1d 69 6e 67 20 70 61 72 61 6d 65 74 65 72 73 20 64  ing parameters d
17a1e 65 74 65 72 6d 69 6e 65 20 68 6f 77 20 6d 61 6e  etermine how man
17a1f 79 20 61 64 6a 61 63 65 6e 74 20 70 61 67 65 73  y adjacent pages
17a20 20 67 65 74 20 69 6e 76 6f 6c 76 65 64 0a 2a 2a   get involved.**
17a21 20 69 6e 20 61 20 62 61 6c 61 6e 63 69 6e 67 20   in a balancing 
17a22 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4e 4e 20 69  operation.  NN i
17a23 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
17a24 6e 65 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74  neighbors on eit
17a25 68 65 72 20 73 69 64 65 0a 2a 2a 20 6f 66 20 74  her side.** of t
17a26 68 65 20 70 61 67 65 20 74 68 61 74 20 70 61 72  he page that par
17a27 74 69 63 69 70 61 74 65 20 69 6e 20 74 68 65 20  ticipate in the 
17a28 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65 72 61 74  balancing operat
17a29 69 6f 6e 2e 20 20 4e 42 20 69 73 20 74 68 65 0a  ion.  NB is the.
17a2a 2a 2a 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  ** total number 
17a2b 6f 66 20 70 61 67 65 73 20 74 68 61 74 20 70 61  of pages that pa
17a2c 72 74 69 63 69 70 61 74 65 2c 20 69 6e 63 6c 75  rticipate, inclu
17a2d 64 69 6e 67 20 74 68 65 20 74 61 72 67 65 74 20  ding the target 
17a2e 70 61 67 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20 6e  page and.** NN n
17a2f 65 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68  eighbors on eith
17a30 65 72 20 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 54  er side..**.** T
17a31 68 65 20 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65  he minimum value
17a32 20 6f 66 20 4e 4e 20 69 73 20 31 20 28 6f 66 20   of NN is 1 (of 
17a33 63 6f 75 72 73 65 29 2e 20 20 49 6e 63 72 65 61  course).  Increa
17a34 73 69 6e 67 20 4e 4e 20 61 62 6f 76 65 20 31 0a  sing NN above 1.
17a35 2a 2a 20 28 74 6f 20 32 20 6f 72 20 33 29 20 67  ** (to 2 or 3) g
17a36 69 76 65 73 20 61 20 6d 6f 64 65 73 74 20 69 6d  ives a modest im
17a37 70 72 6f 76 65 6d 65 6e 74 20 69 6e 20 53 45 4c  provement in SEL
17a38 45 43 54 20 61 6e 64 20 44 45 4c 45 54 45 20 70  ECT and DELETE p
17a39 65 72 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 6e  erformance.** in
17a3a 20 65 78 63 68 61 6e 67 65 20 66 6f 72 20 61 20   exchange for a 
17a3b 6c 61 72 67 65 72 20 64 65 67 72 61 64 61 74 69  larger degradati
17a3c 6f 6e 20 69 6e 20 49 4e 53 45 52 54 20 61 6e 64  on in INSERT and
17a3d 20 55 50 44 41 54 45 20 70 65 72 66 6f 72 6d 61   UPDATE performa
17a3e 6e 63 65 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75  nce..** The valu
17a3f 65 20 6f 66 20 4e 4e 20 61 70 70 65 61 72 73 20  e of NN appears 
17a40 74 6f 20 67 69 76 65 20 74 68 65 20 62 65 73 74  to give the best
17a41 20 72 65 73 75 6c 74 73 20 6f 76 65 72 61 6c 6c   results overall
17a42 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 4e 20  ..*/.#define NN 
17a43 31 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  1             /*
17a44 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 69 67 68   Number of neigh
17a45 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73  bors on either s
17a46 69 64 65 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a  ide of pPage */.
17a47 23 64 65 66 69 6e 65 20 4e 42 20 28 4e 4e 2a 32  #define NB (NN*2
17a48 2b 31 29 20 20 20 20 20 20 2f 2a 20 54 6f 74 61  +1)      /* Tota
17a49 6c 20 70 61 67 65 73 20 69 6e 76 6f 6c 76 65 64  l pages involved
17a4a 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63 65 20   in the balance 
17a4b 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  */...#ifndef SQL
17a4c 49 54 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41  ITE_OMIT_QUICKBA
17a4d 4c 41 4e 43 45 0a 2f 2a 0a 2a 2a 20 54 68 69 73  LANCE./*.** This
17a4e 20 76 65 72 73 69 6f 6e 20 6f 66 20 62 61 6c 61   version of bala
17a4f 6e 63 65 28 29 20 68 61 6e 64 6c 65 73 20 74 68  nce() handles th
17a50 65 20 63 6f 6d 6d 6f 6e 20 73 70 65 63 69 61 6c  e common special
17a51 20 63 61 73 65 20 77 68 65 72 65 0a 2a 2a 20 61   case where.** a
17a52 20 6e 65 77 20 65 6e 74 72 79 20 69 73 20 62 65   new entry is be
17a53 69 6e 67 20 69 6e 73 65 72 74 65 64 20 6f 6e 20  ing inserted on 
17a54 74 68 65 20 65 78 74 72 65 6d 65 20 72 69 67 68  the extreme righ
17a55 74 2d 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20  t-end of the.** 
17a56 74 72 65 65 2c 20 69 6e 20 6f 74 68 65 72 20 77  tree, in other w
17a57 6f 72 64 73 2c 20 77 68 65 6e 20 74 68 65 20 6e  ords, when the n
17a58 65 77 20 65 6e 74 72 79 20 77 69 6c 6c 20 62 65  ew entry will be
17a59 63 6f 6d 65 20 74 68 65 20 6c 61 72 67 65 73 74  come the largest
17a5a 0a 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68 65  .** entry in the
17a5b 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73   tree..**.** Ins
17a5c 74 65 61 64 20 6f 66 20 74 72 79 69 6e 67 20 74  tead of trying t
17a5d 6f 20 62 61 6c 61 6e 63 65 20 74 68 65 20 33 20  o balance the 3 
17a5e 72 69 67 68 74 2d 6d 6f 73 74 20 6c 65 61 66 20  right-most leaf 
17a5f 70 61 67 65 73 2c 20 6a 75 73 74 20 61 64 64 0a  pages, just add.
17a60 2a 2a 20 61 20 6e 65 77 20 70 61 67 65 20 74 6f  ** a new page to
17a61 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20   the right-hand 
17a62 73 69 64 65 20 61 6e 64 20 70 75 74 20 74 68 65  side and put the
17a63 20 6f 6e 65 20 6e 65 77 20 65 6e 74 72 79 20 69   one new entry i
17a64 6e 0a 2a 2a 20 74 68 61 74 20 70 61 67 65 2e 20  n.** that page. 
17a65 20 54 68 69 73 20 6c 65 61 76 65 73 20 74 68 65   This leaves the
17a66 20 72 69 67 68 74 20 73 69 64 65 20 6f 66 20 74   right side of t
17a67 68 65 20 74 72 65 65 20 73 6f 6d 65 77 68 61 74  he tree somewhat
17a68 0a 2a 2a 20 75 6e 62 61 6c 61 6e 63 65 64 2e 20  .** unbalanced. 
17a69 20 42 75 74 20 6f 64 64 73 20 61 72 65 20 74 68   But odds are th
17a6a 61 74 20 77 65 20 77 69 6c 6c 20 62 65 20 69 6e  at we will be in
17a6b 73 65 72 74 69 6e 67 20 6e 65 77 20 65 6e 74 72  serting new entr
17a6c 69 65 73 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e  ies.** at the en
17a6d 64 20 73 6f 6f 6e 20 61 66 74 65 72 77 61 72 64  d soon afterward
17a6e 73 20 73 6f 20 74 68 65 20 6e 65 61 72 6c 79 20  s so the nearly 
17a6f 65 6d 70 74 79 20 70 61 67 65 20 77 69 6c 6c 20  empty page will 
17a70 71 75 69 63 6b 6c 79 0a 2a 2a 20 66 69 6c 6c 20  quickly.** fill 
17a71 75 70 2e 20 20 4f 6e 20 61 76 65 72 61 67 65 2e  up.  On average.
17a72 0a 2a 2a 0a 2a 2a 20 70 50 61 67 65 20 69 73 20  .**.** pPage is 
17a73 74 68 65 20 6c 65 61 66 20 70 61 67 65 20 77 68  the leaf page wh
17a74 69 63 68 20 69 73 20 74 68 65 20 72 69 67 68 74  ich is the right
17a75 2d 6d 6f 73 74 20 70 61 67 65 20 69 6e 20 74 68  -most page in th
17a76 65 20 74 72 65 65 2e 0a 2a 2a 20 70 50 61 72 65  e tree..** pPare
17a77 6e 74 20 69 73 20 69 74 73 20 70 61 72 65 6e 74  nt is its parent
17a78 2e 20 20 70 50 61 67 65 20 6d 75 73 74 20 68 61  .  pPage must ha
17a79 76 65 20 61 20 73 69 6e 67 6c 65 20 6f 76 65 72  ve a single over
17a7a 66 6c 6f 77 20 65 6e 74 72 79 0a 2a 2a 20 77 68  flow entry.** wh
17a7b 69 63 68 20 69 73 20 61 6c 73 6f 20 74 68 65 20  ich is also the 
17a7c 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79  right-most entry
17a7d 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a   on the page..**
17a7e 0a 2a 2a 20 54 68 65 20 70 53 70 61 63 65 20 62  .** The pSpace b
17a7f 75 66 66 65 72 20 69 73 20 75 73 65 64 20 74 6f  uffer is used to
17a80 20 73 74 6f 72 65 20 61 20 74 65 6d 70 6f 72 61   store a tempora
17a81 72 79 20 63 6f 70 79 20 6f 66 20 74 68 65 20 64  ry copy of the d
17a82 69 76 69 64 65 72 0a 2a 2a 20 63 65 6c 6c 20 74  ivider.** cell t
17a83 68 61 74 20 77 69 6c 6c 20 62 65 20 69 6e 73 65  hat will be inse
17a84 72 74 65 64 20 69 6e 74 6f 20 70 50 61 72 65 6e  rted into pParen
17a85 74 2e 20 53 75 63 68 20 61 20 63 65 6c 6c 20 63  t. Such a cell c
17a86 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 34 0a 2a  onsists of a 4.*
17a87 2a 20 62 79 74 65 20 70 61 67 65 20 6e 75 6d 62  * byte page numb
17a88 65 72 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61  er followed by a
17a89 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68   variable length
17a8a 20 69 6e 74 65 67 65 72 2e 20 49 6e 20 6f 74 68   integer. In oth
17a8b 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 61 74 20  er.** words, at 
17a8c 6d 6f 73 74 20 31 33 20 62 79 74 65 73 2e 20 48  most 13 bytes. H
17a8d 65 6e 63 65 20 74 68 65 20 70 53 70 61 63 65 20  ence the pSpace 
17a8e 62 75 66 66 65 72 20 6d 75 73 74 20 62 65 20 61  buffer must be a
17a8f 74 0a 2a 2a 20 6c 65 61 73 74 20 31 33 20 62 79  t.** least 13 by
17a90 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a  tes in size..*/.
17a91 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e  static int balan
17a92 63 65 5f 71 75 69 63 6b 28 4d 65 6d 50 61 67 65  ce_quick(MemPage
17a93 20 2a 70 50 61 72 65 6e 74 2c 20 4d 65 6d 50 61   *pParent, MemPa
17a94 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a 70  ge *pPage, u8 *p
17a95 53 70 61 63 65 29 7b 0a 20 20 42 74 53 68 61 72  Space){.  BtShar
17a96 65 64 20 2a 63 6f 6e 73 74 20 70 42 74 20 3d 20  ed *const pBt = 
17a97 70 50 61 67 65 2d 3e 70 42 74 3b 20 20 20 20 2f  pPage->pBt;    /
17a98 2a 20 42 2d 54 72 65 65 20 44 61 74 61 62 61 73  * B-Tree Databas
17a99 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a  e */.  MemPage *
17a9a 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 20  pNew;           
17a9b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
17a9c 65 77 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 70  ewly allocated p
17a9d 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  age */.  int rc;
17a9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17a9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
17aa0 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a   Return Code */.
17aa1 20 20 50 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b 20    Pgno pgnoNew; 
17aa2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17aa3 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e         /* Page n
17aa4 75 6d 62 65 72 20 6f 66 20 70 4e 65 77 20 2a 2f  umber of pNew */
17aa5 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
17aa6 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
17aa7 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
17aa8 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
17aa9 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
17aaa 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e  teable(pParent->
17aab 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73  pDbPage) );.  as
17aac 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76  sert( pPage->nOv
17aad 65 72 66 6c 6f 77 3d 3d 31 20 29 3b 0a 0a 20 20  erflow==1 );..  
17aae 69 66 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  if( pPage->nCell
17aaf 3c 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c  <=0 ) return SQL
17ab0 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
17ab1 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  ;..  /* Allocate
17ab2 20 61 20 6e 65 77 20 70 61 67 65 2e 20 54 68 69   a new page. Thi
17ab3 73 20 70 61 67 65 20 77 69 6c 6c 20 62 65 63 6f  s page will beco
17ab4 6d 65 20 74 68 65 20 72 69 67 68 74 2d 73 69 62  me the right-sib
17ab5 6c 69 6e 67 20 6f 66 20 0a 20 20 2a 2a 20 70 50  ling of .  ** pP
17ab6 61 67 65 2e 20 4d 61 6b 65 20 74 68 65 20 70 61  age. Make the pa
17ab7 72 65 6e 74 20 70 61 67 65 20 77 72 69 74 61 62  rent page writab
17ab8 6c 65 2c 20 73 6f 20 74 68 61 74 20 74 68 65 20  le, so that the 
17ab9 6e 65 77 20 64 69 76 69 64 65 72 20 63 65 6c 6c  new divider cell
17aba 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20 69 6e 73  .  ** may be ins
17abb 65 72 74 65 64 2e 20 49 66 20 62 6f 74 68 20 74  erted. If both t
17abc 68 65 73 65 20 6f 70 65 72 61 74 69 6f 6e 73 20  hese operations 
17abd 61 72 65 20 73 75 63 63 65 73 73 66 75 6c 2c 20  are successful, 
17abe 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 20  proceed..  */.  
17abf 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72  rc = allocateBtr
17ac0 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 4e 65  eePage(pBt, &pNe
17ac1 77 2c 20 26 70 67 6e 6f 4e 65 77 2c 20 30 2c 20  w, &pgnoNew, 0, 
17ac2 30 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53  0);..  if( rc==S
17ac3 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 0a 20 20 20  QLITE_OK ){..   
17ac4 20 75 38 20 2a 70 4f 75 74 20 3d 20 26 70 53 70   u8 *pOut = &pSp
17ac5 61 63 65 5b 34 5d 3b 0a 20 20 20 20 75 38 20 2a  ace[4];.    u8 *
17ac6 70 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 61  pCell = pPage->a
17ac7 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20  Ovfl[0].pCell;. 
17ac8 20 20 20 75 31 36 20 73 7a 43 65 6c 6c 20 3d 20     u16 szCell = 
17ac9 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67  cellSizePtr(pPag
17aca 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 75  e, pCell);.    u
17acb 38 20 2a 70 53 74 6f 70 3b 0a 0a 20 20 20 20 61  8 *pStop;..    a
17acc 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
17acd 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
17ace 4e 65 77 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  New->pDbPage) );
17acf 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
17ad0 67 65 2d 3e 61 44 61 74 61 5b 30 5d 3d 3d 28 50  ge->aData[0]==(P
17ad1 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45  TF_INTKEY|PTF_LE
17ad2 41 46 44 41 54 41 7c 50 54 46 5f 4c 45 41 46 29  AFDATA|PTF_LEAF)
17ad3 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67 65   );.    zeroPage
17ad4 28 70 4e 65 77 2c 20 50 54 46 5f 49 4e 54 4b 45  (pNew, PTF_INTKE
17ad5 59 7c 50 54 46 5f 4c 45 41 46 44 41 54 41 7c 50  Y|PTF_LEAFDATA|P
17ad6 54 46 5f 4c 45 41 46 29 3b 0a 20 20 20 20 61 73  TF_LEAF);.    as
17ad7 73 65 6d 62 6c 65 50 61 67 65 28 70 4e 65 77 2c  semblePage(pNew,
17ad8 20 31 2c 20 26 70 43 65 6c 6c 2c 20 26 73 7a 43   1, &pCell, &szC
17ad9 65 6c 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66  ell);..    /* If
17ada 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f   this is an auto
17adb 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65  -vacuum database
17adc 2c 20 75 70 64 61 74 65 20 74 68 65 20 70 6f 69  , update the poi
17add 6e 74 65 72 20 6d 61 70 0a 20 20 20 20 2a 2a 20  nter map.    ** 
17ade 77 69 74 68 20 65 6e 74 72 69 65 73 20 66 6f 72  with entries for
17adf 20 74 68 65 20 6e 65 77 20 70 61 67 65 2c 20 61   the new page, a
17ae0 6e 64 20 61 6e 79 20 70 6f 69 6e 74 65 72 20 66  nd any pointer f
17ae1 72 6f 6d 20 74 68 65 20 0a 20 20 20 20 2a 2a 20  rom the .    ** 
17ae2 63 65 6c 6c 20 6f 6e 20 74 68 65 20 70 61 67 65  cell on the page
17ae3 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20   to an overflow 
17ae4 70 61 67 65 2e 20 49 66 20 65 69 74 68 65 72 20  page. If either 
17ae5 6f 66 20 74 68 65 73 65 0a 20 20 20 20 2a 2a 20  of these.    ** 
17ae6 6f 70 65 72 61 74 69 6f 6e 73 20 66 61 69 6c 73  operations fails
17ae7 2c 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64  , the return cod
17ae8 65 20 69 73 20 73 65 74 2c 20 62 75 74 20 74 68  e is set, but th
17ae9 65 20 63 6f 6e 74 65 6e 74 73 0a 20 20 20 20 2a  e contents.    *
17aea 2a 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20  * of the parent 
17aeb 70 61 67 65 20 61 72 65 20 73 74 69 6c 6c 20 6d  page are still m
17aec 61 6e 69 70 75 6c 61 74 65 64 20 62 79 20 74 68  anipulated by th
17aed 68 20 63 6f 64 65 20 62 65 6c 6f 77 2e 0a 20 20  h code below..  
17aee 20 20 2a 2a 20 54 68 61 74 20 69 73 20 4f 6b 2c    ** That is Ok,
17aef 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74   at this point t
17af0 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 69  he parent page i
17af1 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 0a  s guaranteed to.
17af2 20 20 20 20 2a 2a 20 62 65 20 6d 61 72 6b 65 64      ** be marked
17af3 20 61 73 20 64 69 72 74 79 2e 20 52 65 74 75 72   as dirty. Retur
17af4 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 63 6f  ning an error co
17af5 64 65 20 77 69 6c 6c 20 63 61 75 73 65 20 61 0a  de will cause a.
17af6 20 20 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c      ** rollback,
17af7 20 75 6e 64 6f 69 6e 67 20 61 6e 79 20 63 68 61   undoing any cha
17af8 6e 67 65 73 20 6d 61 64 65 20 74 6f 20 74 68 65  nges made to the
17af9 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 20   parent page..  
17afa 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 49 53 41    */.    if( ISA
17afb 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20  UTOVACUUM ){.   
17afc 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74     ptrmapPut(pBt
17afd 2c 20 70 67 6e 6f 4e 65 77 2c 20 50 54 52 4d 41  , pgnoNew, PTRMA
17afe 50 5f 42 54 52 45 45 2c 20 70 50 61 72 65 6e 74  P_BTREE, pParent
17aff 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20  ->pgno, &rc);.  
17b00 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 3e 70      if( szCell>p
17b01 4e 65 77 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 29 7b  New->minLocal ){
17b02 0a 20 20 20 20 20 20 20 20 70 74 72 6d 61 70 50  .        ptrmapP
17b03 75 74 4f 76 66 6c 50 74 72 28 70 4e 65 77 2c 20  utOvflPtr(pNew, 
17b04 70 43 65 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20  pCell, &rc);.   
17b05 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20     }.    }.  .  
17b06 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 64 69    /* Create a di
17b07 76 69 64 65 72 20 63 65 6c 6c 20 74 6f 20 69 6e  vider cell to in
17b08 73 65 72 74 20 69 6e 74 6f 20 70 50 61 72 65 6e  sert into pParen
17b09 74 2e 20 54 68 65 20 64 69 76 69 64 65 72 20 63  t. The divider c
17b0a 65 6c 6c 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 69  ell.    ** consi
17b0b 73 74 73 20 6f 66 20 61 20 34 2d 62 79 74 65 20  sts of a 4-byte 
17b0c 70 61 67 65 20 6e 75 6d 62 65 72 20 28 74 68 65  page number (the
17b0d 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   page number of 
17b0e 70 50 61 67 65 29 20 61 6e 64 0a 20 20 20 20 2a  pPage) and.    *
17b0f 2a 20 61 20 76 61 72 69 61 62 6c 65 20 6c 65 6e  * a variable len
17b10 67 74 68 20 6b 65 79 20 76 61 6c 75 65 20 28 77  gth key value (w
17b11 68 69 63 68 20 6d 75 73 74 20 62 65 20 74 68 65  hich must be the
17b12 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73 20 74   same value as t
17b13 68 65 0a 20 20 20 20 2a 2a 20 6c 61 72 67 65 73  he.    ** larges
17b14 74 20 6b 65 79 20 6f 6e 20 70 50 61 67 65 29 2e  t key on pPage).
17b15 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
17b16 6f 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65  o find the large
17b17 73 74 20 6b 65 79 20 76 61 6c 75 65 20 6f 6e 20  st key value on 
17b18 70 50 61 67 65 2c 20 66 69 72 73 74 20 66 69 6e  pPage, first fin
17b19 64 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  d the right-most
17b1a 20 0a 20 20 20 20 2a 2a 20 63 65 6c 6c 20 6f 6e   .    ** cell on
17b1b 20 70 50 61 67 65 2e 20 54 68 65 20 66 69 72 73   pPage. The firs
17b1c 74 20 74 77 6f 20 66 69 65 6c 64 73 20 6f 66 20  t two fields of 
17b1d 74 68 69 73 20 63 65 6c 6c 20 61 72 65 20 74 68  this cell are th
17b1e 65 20 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64  e .    ** record
17b1f 2d 6c 65 6e 67 74 68 20 28 61 20 76 61 72 69 61  -length (a varia
17b20 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 67  ble length integ
17b21 65 72 20 61 74 20 6d 6f 73 74 20 33 32 2d 62 69  er at most 32-bi
17b22 74 73 20 69 6e 20 73 69 7a 65 29 0a 20 20 20 20  ts in size).    
17b23 2a 2a 20 61 6e 64 20 74 68 65 20 6b 65 79 20 76  ** and the key v
17b24 61 6c 75 65 20 28 61 20 76 61 72 69 61 62 6c 65  alue (a variable
17b25 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 2c   length integer,
17b26 20 6d 61 79 20 68 61 76 65 20 61 6e 79 20 76 61   may have any va
17b27 6c 75 65 29 2e 0a 20 20 20 20 2a 2a 20 54 68 65  lue)..    ** The
17b28 20 66 69 72 73 74 20 6f 66 20 74 68 65 20 77 68   first of the wh
17b29 69 6c 65 28 2e 2e 2e 29 20 6c 6f 6f 70 73 20 62  ile(...) loops b
17b2a 65 6c 6f 77 20 73 6b 69 70 73 20 6f 76 65 72 20  elow skips over 
17b2b 74 68 65 20 72 65 63 6f 72 64 2d 6c 65 6e 67 74  the record-lengt
17b2c 68 0a 20 20 20 20 2a 2a 20 66 69 65 6c 64 2e 20  h.    ** field. 
17b2d 54 68 65 20 73 65 63 6f 6e 64 20 77 68 69 6c 65  The second while
17b2e 28 2e 2e 2e 29 20 6c 6f 6f 70 20 63 6f 70 69 65  (...) loop copie
17b2f 73 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 20  s the key value 
17b30 66 72 6f 6d 20 74 68 65 0a 20 20 20 20 2a 2a 20  from the.    ** 
17b31 63 65 6c 6c 20 6f 6e 20 70 50 61 67 65 20 69 6e  cell on pPage in
17b32 74 6f 20 74 68 65 20 70 53 70 61 63 65 20 62 75  to the pSpace bu
17b33 66 66 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ffer..    */.   
17b34 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c   pCell = findCel
17b35 6c 28 70 50 61 67 65 2c 20 70 50 61 67 65 2d 3e  l(pPage, pPage->
17b36 6e 43 65 6c 6c 2d 31 29 3b 0a 20 20 20 20 70 53  nCell-1);.    pS
17b37 74 6f 70 20 3d 20 26 70 43 65 6c 6c 5b 39 5d 3b  top = &pCell[9];
17b38 0a 20 20 20 20 77 68 69 6c 65 28 20 28 2a 28 70  .    while( (*(p
17b39 43 65 6c 6c 2b 2b 29 26 30 78 38 30 29 20 26 26  Cell++)&0x80) &&
17b3a 20 70 43 65 6c 6c 3c 70 53 74 6f 70 20 29 3b 0a   pCell<pStop );.
17b3b 20 20 20 20 70 53 74 6f 70 20 3d 20 26 70 43 65      pStop = &pCe
17b3c 6c 6c 5b 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65  ll[9];.    while
17b3d 28 20 28 28 2a 28 70 4f 75 74 2b 2b 29 20 3d 20  ( ((*(pOut++) = 
17b3e 2a 28 70 43 65 6c 6c 2b 2b 29 29 26 30 78 38 30  *(pCell++))&0x80
17b3f 29 20 26 26 20 70 43 65 6c 6c 3c 70 53 74 6f 70  ) && pCell<pStop
17b40 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 73 65   );..    /* Inse
17b41 72 74 20 74 68 65 20 6e 65 77 20 64 69 76 69 64  rt the new divid
17b42 65 72 20 63 65 6c 6c 20 69 6e 74 6f 20 70 50 61  er cell into pPa
17b43 72 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 69 6e 73  rent. */.    ins
17b44 65 72 74 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c  ertCell(pParent,
17b45 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 2c   pParent->nCell,
17b46 20 70 53 70 61 63 65 2c 20 28 69 6e 74 29 28 70   pSpace, (int)(p
17b47 4f 75 74 2d 70 53 70 61 63 65 29 2c 0a 20 20 20  Out-pSpace),.   
17b48 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 70              0, p
17b49 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 26 72 63 29  Page->pgno, &rc)
17b4a 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68  ;..    /* Set th
17b4b 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 70 6f  e right-child po
17b4c 69 6e 74 65 72 20 6f 66 20 70 50 61 72 65 6e 74  inter of pParent
17b4d 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   to point to the
17b4e 20 6e 65 77 20 70 61 67 65 2e 20 2a 2f 0a 20 20   new page. */.  
17b4f 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 72    put4byte(&pPar
17b50 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65  ent->aData[pPare
17b51 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  nt->hdrOffset+8]
17b52 2c 20 70 67 6e 6f 4e 65 77 29 3b 0a 20 20 0a 20  , pgnoNew);.  . 
17b53 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68     /* Release th
17b54 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74  e reference to t
17b55 68 65 20 6e 65 77 20 70 61 67 65 2e 20 2a 2f 0a  he new page. */.
17b56 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
17b57 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a 20 20 72 65  pNew);.  }..  re
17b58 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69  turn rc;.}.#endi
17b59 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
17b5a 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 20 2a 2f  _QUICKBALANCE */
17b5b 0a 0a 23 69 66 20 30 0a 2f 2a 0a 2a 2a 20 54 68  ..#if 0./*.** Th
17b5c 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73  is function does
17b5d 20 6e 6f 74 20 63 6f 6e 74 72 69 62 75 74 65 20   not contribute 
17b5e 61 6e 79 74 68 69 6e 67 20 74 6f 20 74 68 65 20  anything to the 
17b5f 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 53 51 4c  operation of SQL
17b60 69 74 65 2e 0a 2a 2a 20 69 74 20 69 73 20 73 6f  ite..** it is so
17b61 6d 65 74 69 6d 65 73 20 61 63 74 69 76 61 74 65  metimes activate
17b62 64 20 74 65 6d 70 6f 72 61 72 69 6c 79 20 77 68  d temporarily wh
17b63 69 6c 65 20 64 65 62 75 67 67 69 6e 67 20 63 6f  ile debugging co
17b64 64 65 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 0a  de responsible .
17b65 2a 2a 20 66 6f 72 20 73 65 74 74 69 6e 67 20 70  ** for setting p
17b66 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69  ointer-map entri
17b67 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  es..*/.static in
17b68 74 20 70 74 72 6d 61 70 43 68 65 63 6b 50 61 67  t ptrmapCheckPag
17b69 65 73 28 4d 65 6d 50 61 67 65 20 2a 2a 61 70 50  es(MemPage **apP
17b6a 61 67 65 2c 20 69 6e 74 20 6e 50 61 67 65 29 7b  age, int nPage){
17b6b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 66  .  int i, j;.  f
17b6c 6f 72 28 69 3d 30 3b 20 69 3c 6e 50 61 67 65 3b  or(i=0; i<nPage;
17b6d 20 69 2b 2b 29 7b 0a 20 20 20 20 50 67 6e 6f 20   i++){.    Pgno 
17b6e 6e 3b 0a 20 20 20 20 75 38 20 65 3b 0a 20 20 20  n;.    u8 e;.   
17b6f 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20   MemPage *pPage 
17b70 3d 20 61 70 50 61 67 65 5b 69 5d 3b 0a 20 20 20  = apPage[i];.   
17b71 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
17b72 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 20   pPage->pBt;.   
17b73 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
17b74 69 73 49 6e 69 74 20 29 3b 0a 0a 20 20 20 20 66  isInit );..    f
17b75 6f 72 28 6a 3d 30 3b 20 6a 3c 70 50 61 67 65 2d  or(j=0; j<pPage-
17b76 3e 6e 43 65 6c 6c 3b 20 6a 2b 2b 29 7b 0a 20 20  >nCell; j++){.  
17b77 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66      CellInfo inf
17b78 6f 3b 0a 20 20 20 20 20 20 75 38 20 2a 7a 3b 0a  o;.      u8 *z;.
17b79 20 20 20 20 20 0a 20 20 20 20 20 20 7a 20 3d 20       .      z = 
17b7a 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20  findCell(pPage, 
17b7b 6a 29 3b 0a 20 20 20 20 20 20 62 74 72 65 65 50  j);.      btreeP
17b7c 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67  arseCellPtr(pPag
17b7d 65 2c 20 7a 2c 20 26 69 6e 66 6f 29 3b 0a 20 20  e, z, &info);.  
17b7e 20 20 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76      if( info.iOv
17b7f 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20  erflow ){.      
17b80 20 20 50 67 6e 6f 20 6f 76 66 6c 20 3d 20 67 65    Pgno ovfl = ge
17b81 74 34 62 79 74 65 28 26 7a 5b 69 6e 66 6f 2e 69  t4byte(&z[info.i
17b82 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20  Overflow]);.    
17b83 20 20 20 20 70 74 72 6d 61 70 47 65 74 28 70 42      ptrmapGet(pB
17b84 74 2c 20 6f 76 66 6c 2c 20 26 65 2c 20 26 6e 29  t, ovfl, &e, &n)
17b85 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
17b86 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f  ( n==pPage->pgno
17b87 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f 4f 56   && e==PTRMAP_OV
17b88 45 52 46 4c 4f 57 31 20 29 3b 0a 20 20 20 20 20  ERFLOW1 );.     
17b89 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 70 50   }.      if( !pP
17b8a 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
17b8b 20 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 20       Pgno child 
17b8c 3d 20 67 65 74 34 62 79 74 65 28 7a 29 3b 0a 20  = get4byte(z);. 
17b8d 20 20 20 20 20 20 20 70 74 72 6d 61 70 47 65 74         ptrmapGet
17b8e 28 70 42 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c  (pBt, child, &e,
17b8f 20 26 6e 29 3b 0a 20 20 20 20 20 20 20 20 61 73   &n);.        as
17b90 73 65 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e  sert( n==pPage->
17b91 70 67 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41  pgno && e==PTRMA
17b92 50 5f 42 54 52 45 45 20 29 3b 0a 20 20 20 20 20  P_BTREE );.     
17b93 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
17b94 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
17b95 0a 20 20 20 20 20 20 50 67 6e 6f 20 63 68 69 6c  .      Pgno chil
17b96 64 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50  d = get4byte(&pP
17b97 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65  age->aData[pPage
17b98 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b  ->hdrOffset+8]);
17b99 0a 20 20 20 20 20 20 70 74 72 6d 61 70 47 65 74  .      ptrmapGet
17b9a 28 70 42 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c  (pBt, child, &e,
17b9b 20 26 6e 29 3b 0a 20 20 20 20 20 20 61 73 73 65   &n);.      asse
17b9c 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67  rt( n==pPage->pg
17b9d 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f  no && e==PTRMAP_
17b9e 42 54 52 45 45 20 29 3b 0a 20 20 20 20 7d 0a 20  BTREE );.    }. 
17b9f 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d   }.  return 1;.}
17ba0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
17ba1 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
17ba2 75 73 65 64 20 74 6f 20 63 6f 70 79 20 74 68 65  used to copy the
17ba3 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
17ba4 20 62 2d 74 72 65 65 20 6e 6f 64 65 20 73 74 6f   b-tree node sto
17ba5 72 65 64 20 0a 2a 2a 20 6f 6e 20 70 61 67 65 20  red .** on page 
17ba6 70 46 72 6f 6d 20 74 6f 20 70 61 67 65 20 70 54  pFrom to page pT
17ba7 6f 2e 20 49 66 20 70 61 67 65 20 70 46 72 6f 6d  o. If page pFrom
17ba8 20 77 61 73 20 6e 6f 74 20 61 20 6c 65 61 66 20   was not a leaf 
17ba9 70 61 67 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68  page, then.** th
17baa 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e  e pointer-map en
17bab 74 72 69 65 73 20 66 6f 72 20 65 61 63 68 20 63  tries for each c
17bac 68 69 6c 64 20 70 61 67 65 20 61 72 65 20 75 70  hild page are up
17bad 64 61 74 65 64 20 73 6f 20 74 68 61 74 20 74 68  dated so that th
17bae 65 0a 2a 2a 20 70 61 72 65 6e 74 20 70 61 67 65  e.** parent page
17baf 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 70   stored in the p
17bb0 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 70 61  ointer map is pa
17bb1 67 65 20 70 54 6f 2e 20 49 66 20 70 46 72 6f 6d  ge pTo. If pFrom
17bb2 20 63 6f 6e 74 61 69 6e 65 64 0a 2a 2a 20 61 6e   contained.** an
17bb3 79 20 63 65 6c 6c 73 20 77 69 74 68 20 6f 76 65  y cells with ove
17bb4 72 66 6c 6f 77 20 70 61 67 65 20 70 6f 69 6e 74  rflow page point
17bb5 65 72 73 2c 20 74 68 65 6e 20 74 68 65 20 63 6f  ers, then the co
17bb6 72 72 65 73 70 6f 6e 64 69 6e 67 20 70 6f 69 6e  rresponding poin
17bb7 74 65 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69  ter.** map entri
17bb8 65 73 20 61 72 65 20 61 6c 73 6f 20 75 70 64 61  es are also upda
17bb9 74 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20  ted so that the 
17bba 70 61 72 65 6e 74 20 70 61 67 65 20 69 73 20 70  parent page is p
17bbb 61 67 65 20 70 54 6f 2e 0a 2a 2a 0a 2a 2a 20 49  age pTo..**.** I
17bbc 66 20 70 46 72 6f 6d 20 69 73 20 63 75 72 72 65  f pFrom is curre
17bbd 6e 74 6c 79 20 63 61 72 72 79 69 6e 67 20 61 6e  ntly carrying an
17bbe 79 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73  y overflow cells
17bbf 20 28 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65   (entries in the
17bc0 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 4f 76 66  .** MemPage.aOvf
17bc1 6c 5b 5d 20 61 72 72 61 79 29 2c 20 74 68 65 79  l[] array), they
17bc2 20 61 72 65 20 6e 6f 74 20 63 6f 70 69 65 64 20   are not copied 
17bc3 74 6f 20 70 54 6f 2e 20 0a 2a 2a 0a 2a 2a 20 42  to pTo. .**.** B
17bc4 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2c  efore returning,
17bc5 20 70 61 67 65 20 70 54 6f 20 69 73 20 72 65 69   page pTo is rei
17bc6 6e 69 74 69 61 6c 69 7a 65 64 20 75 73 69 6e 67  nitialized using
17bc7 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29   btreeInitPage()
17bc8 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 65 72 66  ..**.** The perf
17bc9 6f 72 6d 61 6e 63 65 20 6f 66 20 74 68 69 73 20  ormance of this 
17bca 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20  function is not 
17bcb 63 72 69 74 69 63 61 6c 2e 20 49 74 20 69 73 20  critical. It is 
17bcc 6f 6e 6c 79 20 75 73 65 64 20 62 79 20 0a 2a 2a  only used by .**
17bcd 20 74 68 65 20 62 61 6c 61 6e 63 65 5f 73 68 61   the balance_sha
17bce 6c 6c 6f 77 65 72 28 29 20 61 6e 64 20 62 61 6c  llower() and bal
17bcf 61 6e 63 65 5f 64 65 65 70 65 72 28 29 20 70 72  ance_deeper() pr
17bd0 6f 63 65 64 75 72 65 73 2c 20 6e 65 69 74 68 65  ocedures, neithe
17bd1 72 20 6f 66 0a 2a 2a 20 77 68 69 63 68 20 61 72  r of.** which ar
17bd2 65 20 63 61 6c 6c 65 64 20 6f 66 74 65 6e 20 75  e called often u
17bd3 6e 64 65 72 20 6e 6f 72 6d 61 6c 20 63 69 72 63  nder normal circ
17bd4 75 6d 73 74 61 6e 63 65 73 2e 0a 2a 2f 0a 73 74  umstances..*/.st
17bd5 61 74 69 63 20 76 6f 69 64 20 63 6f 70 79 4e 6f  atic void copyNo
17bd6 64 65 43 6f 6e 74 65 6e 74 28 4d 65 6d 50 61 67  deContent(MemPag
17bd7 65 20 2a 70 46 72 6f 6d 2c 20 4d 65 6d 50 61 67  e *pFrom, MemPag
17bd8 65 20 2a 70 54 6f 2c 20 69 6e 74 20 2a 70 52 43  e *pTo, int *pRC
17bd9 29 7b 0a 20 20 69 66 28 20 28 2a 70 52 43 29 3d  ){.  if( (*pRC)=
17bda 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
17bdb 20 20 42 74 53 68 61 72 65 64 20 2a 20 63 6f 6e    BtShared * con
17bdc 73 74 20 70 42 74 20 3d 20 70 46 72 6f 6d 2d 3e  st pBt = pFrom->
17bdd 70 42 74 3b 0a 20 20 20 20 75 38 20 2a 20 63 6f  pBt;.    u8 * co
17bde 6e 73 74 20 61 46 72 6f 6d 20 3d 20 70 46 72 6f  nst aFrom = pFro
17bdf 6d 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 75 38  m->aData;.    u8
17be0 20 2a 20 63 6f 6e 73 74 20 61 54 6f 20 3d 20 70   * const aTo = p
17be1 54 6f 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 69  To->aData;.    i
17be2 6e 74 20 63 6f 6e 73 74 20 69 46 72 6f 6d 48 64  nt const iFromHd
17be3 72 20 3d 20 70 46 72 6f 6d 2d 3e 68 64 72 4f 66  r = pFrom->hdrOf
17be4 66 73 65 74 3b 0a 20 20 20 20 69 6e 74 20 63 6f  fset;.    int co
17be5 6e 73 74 20 69 54 6f 48 64 72 20 3d 20 28 28 70  nst iToHdr = ((p
17be6 54 6f 2d 3e 70 67 6e 6f 3d 3d 31 29 20 3f 20 31  To->pgno==1) ? 1
17be7 30 30 20 3a 20 30 29 3b 0a 20 20 20 20 54 45 53  00 : 0);.    TES
17be8 54 4f 4e 4c 59 28 69 6e 74 20 72 63 3b 29 0a 20  TONLY(int rc;). 
17be9 20 20 20 69 6e 74 20 69 44 61 74 61 3b 0a 20 20     int iData;.  
17bea 0a 20 20 0a 20 20 20 20 61 73 73 65 72 74 28 20  .  .    assert( 
17beb 70 46 72 6f 6d 2d 3e 69 73 49 6e 69 74 20 29 3b  pFrom->isInit );
17bec 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 72  .    assert( pFr
17bed 6f 6d 2d 3e 6e 46 72 65 65 3e 3d 69 54 6f 48 64  om->nFree>=iToHd
17bee 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  r );.    assert(
17bef 20 67 65 74 32 62 79 74 65 28 26 61 46 72 6f 6d   get2byte(&aFrom
17bf0 5b 69 46 72 6f 6d 48 64 72 2b 35 5d 29 3c 3d 70  [iFromHdr+5])<=p
17bf1 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29  Bt->usableSize )
17bf2 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 43 6f 70 79  ;.  .    /* Copy
17bf3 20 74 68 65 20 62 2d 74 72 65 65 20 6e 6f 64 65   the b-tree node
17bf4 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 70 61   content from pa
17bf5 67 65 20 70 46 72 6f 6d 20 74 6f 20 70 61 67 65  ge pFrom to page
17bf6 20 70 54 6f 2e 20 2a 2f 0a 20 20 20 20 69 44 61   pTo. */.    iDa
17bf7 74 61 20 3d 20 67 65 74 32 62 79 74 65 28 26 61  ta = get2byte(&a
17bf8 46 72 6f 6d 5b 69 46 72 6f 6d 48 64 72 2b 35 5d  From[iFromHdr+5]
17bf9 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61  );.    memcpy(&a
17bfa 54 6f 5b 69 44 61 74 61 5d 2c 20 26 61 46 72 6f  To[iData], &aFro
17bfb 6d 5b 69 44 61 74 61 5d 2c 20 70 42 74 2d 3e 75  m[iData], pBt->u
17bfc 73 61 62 6c 65 53 69 7a 65 2d 69 44 61 74 61 29  sableSize-iData)
17bfd 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61 54  ;.    memcpy(&aT
17bfe 6f 5b 69 54 6f 48 64 72 5d 2c 20 26 61 46 72 6f  o[iToHdr], &aFro
17bff 6d 5b 69 46 72 6f 6d 48 64 72 5d 2c 20 70 46 72  m[iFromHdr], pFr
17c00 6f 6d 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 2b  om->cellOffset +
17c01 20 32 2a 70 46 72 6f 6d 2d 3e 6e 43 65 6c 6c 29   2*pFrom->nCell)
17c02 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 69 6e  ;.  .    /* Rein
17c03 69 74 69 61 6c 69 7a 65 20 70 61 67 65 20 70 54  itialize page pT
17c04 6f 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f  o so that the co
17c05 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 4d 65  ntents of the Me
17c06 6d 50 61 67 65 20 73 74 72 75 63 74 75 72 65 0a  mPage structure.
17c07 20 20 20 20 2a 2a 20 6d 61 74 63 68 20 74 68 65      ** match the
17c08 20 6e 65 77 20 64 61 74 61 2e 20 54 68 65 20 69   new data. The i
17c09 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66  nitialization of
17c0a 20 70 54 6f 20 22 63 61 6e 6e 6f 74 22 20 66 61   pTo "cannot" fa
17c0b 69 6c 2c 20 61 73 20 74 68 65 0a 20 20 20 20 2a  il, as the.    *
17c0c 2a 20 64 61 74 61 20 63 6f 70 69 65 64 20 66 72  * data copied fr
17c0d 6f 6d 20 70 46 72 6f 6d 20 69 73 20 6b 6e 6f 77  om pFrom is know
17c0e 6e 20 74 6f 20 62 65 20 76 61 6c 69 64 2e 20 20  n to be valid.  
17c0f 2a 2f 0a 20 20 20 20 70 54 6f 2d 3e 69 73 49 6e  */.    pTo->isIn
17c10 69 74 20 3d 20 30 3b 0a 20 20 20 20 54 45 53 54  it = 0;.    TEST
17c11 4f 4e 4c 59 28 72 63 20 3d 20 29 20 62 74 72 65  ONLY(rc = ) btre
17c12 65 49 6e 69 74 50 61 67 65 28 70 54 6f 29 3b 0a  eInitPage(pTo);.
17c13 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
17c14 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 0a  SQLITE_OK );.  .
17c15 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
17c16 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d  s an auto-vacuum
17c17 20 64 61 74 61 62 61 73 65 2c 20 75 70 64 61 74   database, updat
17c18 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  e the pointer-ma
17c19 70 20 65 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a  p entries.    **
17c1a 20 66 6f 72 20 61 6e 79 20 62 2d 74 72 65 65 20   for any b-tree 
17c1b 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  or overflow page
17c1c 73 20 74 68 61 74 20 70 54 6f 20 6e 6f 77 20 63  s that pTo now c
17c1d 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 6f 69 6e  ontains the poin
17c1e 74 65 72 73 20 74 6f 2e 0a 20 20 20 20 2a 2f 0a  ters to..    */.
17c1f 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41      if( ISAUTOVA
17c20 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 2a 70  CUUM ){.      *p
17c21 52 43 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72  RC = setChildPtr
17c22 6d 61 70 73 28 70 54 6f 29 3b 0a 20 20 20 20 7d  maps(pTo);.    }
17c23 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
17c24 69 73 20 72 6f 75 74 69 6e 65 20 72 65 64 69 73  is routine redis
17c25 74 72 69 62 75 74 65 73 20 63 65 6c 6c 73 20 6f  tributes cells o
17c26 6e 20 74 68 65 20 69 50 61 72 65 6e 74 49 64 78  n the iParentIdx
17c27 27 74 68 20 63 68 69 6c 64 20 6f 66 20 70 50 61  'th child of pPa
17c28 72 65 6e 74 0a 2a 2a 20 28 68 65 72 65 61 66 74  rent.** (hereaft
17c29 65 72 20 22 74 68 65 20 70 61 67 65 22 29 20 61  er "the page") a
17c2a 6e 64 20 75 70 20 74 6f 20 32 20 73 69 62 6c 69  nd up to 2 sibli
17c2b 6e 67 73 20 73 6f 20 74 68 61 74 20 61 6c 6c 20  ngs so that all 
17c2c 70 61 67 65 73 20 68 61 76 65 20 61 62 6f 75 74  pages have about
17c2d 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61 6d 6f   the.** same amo
17c2e 75 6e 74 20 6f 66 20 66 72 65 65 20 73 70 61 63  unt of free spac
17c2f 65 2e 20 55 73 75 61 6c 6c 79 20 61 20 73 69 6e  e. Usually a sin
17c30 67 6c 65 20 73 69 62 6c 69 6e 67 20 6f 6e 20 65  gle sibling on e
17c31 69 74 68 65 72 20 73 69 64 65 20 6f 66 20 74 68  ither side of th
17c32 65 0a 2a 2a 20 70 61 67 65 20 61 72 65 20 75 73  e.** page are us
17c33 65 64 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63  ed in the balanc
17c34 69 6e 67 2c 20 74 68 6f 75 67 68 20 62 6f 74 68  ing, though both
17c35 20 73 69 62 6c 69 6e 67 73 20 6d 69 67 68 74 20   siblings might 
17c36 63 6f 6d 65 20 66 72 6f 6d 20 6f 6e 65 0a 2a 2a  come from one.**
17c37 20 73 69 64 65 20 69 66 20 74 68 65 20 70 61 67   side if the pag
17c38 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20 6f  e is the first o
17c39 72 20 6c 61 73 74 20 63 68 69 6c 64 20 6f 66 20  r last child of 
17c3a 69 74 73 20 70 61 72 65 6e 74 2e 20 49 66 20 74  its parent. If t
17c3b 68 65 20 70 61 67 65 20 0a 2a 2a 20 68 61 73 20  he page .** has 
17c3c 66 65 77 65 72 20 74 68 61 6e 20 32 20 73 69 62  fewer than 2 sib
17c3d 6c 69 6e 67 73 20 28 73 6f 6d 65 74 68 69 6e 67  lings (something
17c3e 20 77 68 69 63 68 20 63 61 6e 20 6f 6e 6c 79 20   which can only 
17c3f 68 61 70 70 65 6e 20 69 66 20 74 68 65 20 70 61  happen if the pa
17c40 67 65 0a 2a 2a 20 69 73 20 61 20 72 6f 6f 74 20  ge.** is a root 
17c41 70 61 67 65 20 6f 72 20 61 20 63 68 69 6c 64 20  page or a child 
17c42 6f 66 20 61 20 72 6f 6f 74 20 70 61 67 65 29 20  of a root page) 
17c43 74 68 65 6e 20 61 6c 6c 20 61 76 61 69 6c 61 62  then all availab
17c44 6c 65 20 73 69 62 6c 69 6e 67 73 0a 2a 2a 20 70  le siblings.** p
17c45 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 74 68  articipate in th
17c46 65 20 62 61 6c 61 6e 63 69 6e 67 2e 0a 2a 2a 0a  e balancing..**.
17c47 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** The number of
17c48 20 73 69 62 6c 69 6e 67 73 20 6f 66 20 74 68 65   siblings of the
17c49 20 70 61 67 65 20 6d 69 67 68 74 20 62 65 20 69   page might be i
17c4a 6e 63 72 65 61 73 65 64 20 6f 72 20 64 65 63 72  ncreased or decr
17c4b 65 61 73 65 64 20 62 79 20 0a 2a 2a 20 6f 6e 65  eased by .** one
17c4c 20 6f 72 20 74 77 6f 20 69 6e 20 61 6e 20 65 66   or two in an ef
17c4d 66 6f 72 74 20 74 6f 20 6b 65 65 70 20 70 61 67  fort to keep pag
17c4e 65 73 20 6e 65 61 72 6c 79 20 66 75 6c 6c 20 62  es nearly full b
17c4f 75 74 20 6e 6f 74 20 6f 76 65 72 20 66 75 6c 6c  ut not over full
17c50 2e 20 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68  . .**.** Note th
17c51 61 74 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75  at when this rou
17c52 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20  tine is called, 
17c53 73 6f 6d 65 20 6f 66 20 74 68 65 20 63 65 6c 6c  some of the cell
17c54 73 20 6f 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a  s on the page.**
17c55 20 6d 69 67 68 74 20 6e 6f 74 20 61 63 74 75 61   might not actua
17c56 6c 6c 79 20 62 65 20 73 74 6f 72 65 64 20 69 6e  lly be stored in
17c57 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 5b 5d   MemPage.aData[]
17c58 2e 20 54 68 69 73 20 63 61 6e 20 68 61 70 70 65  . This can happe
17c59 6e 0a 2a 2a 20 69 66 20 74 68 65 20 70 61 67 65  n.** if the page
17c5a 20 69 73 20 6f 76 65 72 66 75 6c 6c 2e 20 54 68   is overfull. Th
17c5b 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 73 75 72  is routine ensur
17c5c 65 73 20 74 68 61 74 20 61 6c 6c 20 63 65 6c 6c  es that all cell
17c5d 73 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 74  s allocated.** t
17c5e 6f 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 69  o the page and i
17c5f 74 73 20 73 69 62 6c 69 6e 67 73 20 66 69 74 20  ts siblings fit 
17c60 69 6e 74 6f 20 4d 65 6d 50 61 67 65 2e 61 44 61  into MemPage.aDa
17c61 74 61 5b 5d 20 62 65 66 6f 72 65 20 72 65 74 75  ta[] before retu
17c62 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  rning..**.** In 
17c63 74 68 65 20 63 6f 75 72 73 65 20 6f 66 20 62 61  the course of ba
17c64 6c 61 6e 63 69 6e 67 20 74 68 65 20 70 61 67 65  lancing the page
17c65 20 61 6e 64 20 69 74 73 20 73 69 62 6c 69 6e 67   and its sibling
17c66 73 2c 20 63 65 6c 6c 73 20 6d 61 79 20 62 65 0a  s, cells may be.
17c67 2a 2a 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f  ** inserted into
17c68 20 6f 72 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d   or removed from
17c69 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
17c6a 20 28 70 50 61 72 65 6e 74 29 2e 20 44 6f 69 6e   (pParent). Doin
17c6b 67 20 73 6f 0a 2a 2a 20 6d 61 79 20 63 61 75 73  g so.** may caus
17c6c 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67  e the parent pag
17c6d 65 20 74 6f 20 62 65 63 6f 6d 65 20 6f 76 65 72  e to become over
17c6e 66 75 6c 6c 20 6f 72 20 75 6e 64 65 72 66 75 6c  full or underful
17c6f 6c 2e 20 49 66 20 74 68 69 73 0a 2a 2a 20 68 61  l. If this.** ha
17c70 70 70 65 6e 73 2c 20 69 74 20 69 73 20 74 68 65  ppens, it is the
17c71 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20   responsibility 
17c72 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f  of the caller to
17c73 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 6f 72 72   invoke the corr
17c74 65 63 74 0a 2a 2a 20 62 61 6c 61 6e 63 69 6e 67  ect.** balancing
17c75 20 72 6f 75 74 69 6e 65 20 74 6f 20 66 69 78 20   routine to fix 
17c76 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 28 73 65  this problem (se
17c77 65 20 74 68 65 20 62 61 6c 61 6e 63 65 28 29 20  e the balance() 
17c78 72 6f 75 74 69 6e 65 29 2e 20 0a 2a 2a 0a 2a 2a  routine). .**.**
17c79 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   If this routine
17c7a 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 72   fails for any r
17c7b 65 61 73 6f 6e 2c 20 69 74 20 6d 69 67 68 74 20  eason, it might 
17c7c 6c 65 61 76 65 20 74 68 65 20 64 61 74 61 62 61  leave the databa
17c7d 73 65 0a 2a 2a 20 69 6e 20 61 20 63 6f 72 72 75  se.** in a corru
17c7e 70 74 65 64 20 73 74 61 74 65 2e 20 53 6f 20 69  pted state. So i
17c7f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66  f this routine f
17c80 61 69 6c 73 2c 20 74 68 65 20 64 61 74 61 62 61  ails, the databa
17c81 73 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20  se should.** be 
17c82 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a  rolled back..**.
17c83 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61 72 67  ** The third arg
17c84 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75  ument to this fu
17c85 6e 63 74 69 6f 6e 2c 20 61 4f 76 66 6c 53 70 61  nction, aOvflSpa
17c86 63 65 2c 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ce, is a pointer
17c87 20 74 6f 20 61 0a 2a 2a 20 62 75 66 66 65 72 20   to a.** buffer 
17c88 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f  big enough to ho
17c89 6c 64 20 6f 6e 65 20 70 61 67 65 2e 20 49 66 20  ld one page. If 
17c8a 77 68 69 6c 65 20 69 6e 73 65 72 74 69 6e 67 20  while inserting 
17c8b 63 65 6c 6c 73 20 69 6e 74 6f 20 74 68 65 20 70  cells into the p
17c8c 61 72 65 6e 74 0a 2a 2a 20 70 61 67 65 20 28 70  arent.** page (p
17c8d 50 61 72 65 6e 74 29 20 74 68 65 20 70 61 72 65  Parent) the pare
17c8e 6e 74 20 70 61 67 65 20 62 65 63 6f 6d 65 73 20  nt page becomes 
17c8f 6f 76 65 72 66 75 6c 6c 2c 20 74 68 69 73 20 62  overfull, this b
17c90 75 66 66 65 72 20 69 73 0a 2a 2a 20 75 73 65 64  uffer is.** used
17c91 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 70 61   to store the pa
17c92 72 65 6e 74 27 73 20 6f 76 65 72 66 6c 6f 77 20  rent's overflow 
17c93 63 65 6c 6c 73 2e 20 42 65 63 61 75 73 65 20 74  cells. Because t
17c94 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 73  his function ins
17c95 65 72 74 73 0a 2a 2a 20 61 20 6d 61 78 69 6d 75  erts.** a maximu
17c96 6d 20 6f 66 20 66 6f 75 72 20 64 69 76 69 64 65  m of four divide
17c97 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20 74 68 65  r cells into the
17c98 20 70 61 72 65 6e 74 20 70 61 67 65 2c 20 61 6e   parent page, an
17c99 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a  d the maximum.**
17c9a 20 73 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20   size of a cell 
17c9b 73 74 6f 72 65 64 20 77 69 74 68 69 6e 20 61 6e  stored within an
17c9c 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 69   internal node i
17c9d 73 20 61 6c 77 61 79 73 20 6c 65 73 73 20 74 68  s always less th
17c9e 61 6e 20 31 2f 34 0a 2a 2a 20 6f 66 20 74 68 65  an 1/4.** of the
17c9f 20 70 61 67 65 2d 73 69 7a 65 2c 20 74 68 65 20   page-size, the 
17ca0 61 4f 76 66 6c 53 70 61 63 65 5b 5d 20 62 75 66  aOvflSpace[] buf
17ca1 66 65 72 20 69 73 20 67 75 61 72 61 6e 74 65 65  fer is guarantee
17ca2 64 20 74 6f 20 62 65 20 6c 61 72 67 65 0a 2a 2a  d to be large.**
17ca3 20 65 6e 6f 75 67 68 20 66 6f 72 20 61 6c 6c 20   enough for all 
17ca4 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a  overflow cells..
17ca5 2a 2a 0a 2a 2a 20 49 66 20 61 4f 76 66 6c 53 70  **.** If aOvflSp
17ca6 61 63 65 20 69 73 20 73 65 74 20 74 6f 20 61 20  ace is set to a 
17ca7 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2c 20 74 68  null pointer, th
17ca8 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
17ca9 72 6e 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e  rns .** SQLITE_N
17caa 4f 4d 45 4d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  OMEM..*/.static 
17cab 69 6e 74 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72  int balance_nonr
17cac 6f 6f 74 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a  oot(.  MemPage *
17cad 70 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20 20  pParent,        
17cae 20 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74         /* Parent
17caf 20 70 61 67 65 20 6f 66 20 73 69 62 6c 69 6e 67   page of sibling
17cb0 73 20 62 65 69 6e 67 20 62 61 6c 61 6e 63 65 64  s being balanced
17cb1 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 65 6e   */.  int iParen
17cb2 74 49 64 78 2c 20 20 20 20 20 20 20 20 20 20 20  tIdx,           
17cb3 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
17cb4 66 20 22 74 68 65 20 70 61 67 65 22 20 69 6e 20  f "the page" in 
17cb5 70 50 61 72 65 6e 74 20 2a 2f 0a 20 20 75 38 20  pParent */.  u8 
17cb6 2a 61 4f 76 66 6c 53 70 61 63 65 2c 20 20 20 20  *aOvflSpace,    
17cb7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17cb8 70 61 67 65 2d 73 69 7a 65 20 62 79 74 65 73 20  page-size bytes 
17cb9 6f 66 20 73 70 61 63 65 20 66 6f 72 20 70 61 72  of space for par
17cba 65 6e 74 20 6f 76 66 6c 20 2a 2f 0a 20 20 69 6e  ent ovfl */.  in
17cbb 74 20 69 73 52 6f 6f 74 20 20 20 20 20 20 20 20  t isRoot        
17cbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
17cbd 20 54 72 75 65 20 69 66 20 70 50 61 72 65 6e 74   True if pParent
17cbe 20 69 73 20 61 20 72 6f 6f 74 2d 70 61 67 65 20   is a root-page 
17cbf 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 72 65 64  */.){.  BtShared
17cc0 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 20   *pBt;          
17cc1 20 20 20 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c       /* The whol
17cc2 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  e database */.  
17cc3 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 30 3b 20 20  int nCell = 0;  
17cc4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17cc5 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20  Number of cells 
17cc6 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20  in apCell[] */. 
17cc7 20 69 6e 74 20 6e 4d 61 78 43 65 6c 6c 73 20 3d   int nMaxCells =
17cc8 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a   0;           /*
17cc9 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20   Allocated size 
17cca 6f 66 20 61 70 43 65 6c 6c 2c 20 73 7a 43 65 6c  of apCell, szCel
17ccb 6c 2c 20 61 46 72 6f 6d 2e 20 2a 2f 0a 20 20 69  l, aFrom. */.  i
17ccc 6e 74 20 6e 4e 65 77 20 3d 20 30 3b 20 20 20 20  nt nNew = 0;    
17ccd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
17cce 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
17ccf 6e 20 61 70 4e 65 77 5b 5d 20 2a 2f 0a 20 20 69  n apNew[] */.  i
17cd0 6e 74 20 6e 4f 6c 64 3b 20 20 20 20 20 20 20 20  nt nOld;        
17cd1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
17cd2 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
17cd3 6e 20 61 70 4f 6c 64 5b 5d 20 2a 2f 0a 20 20 69  n apOld[] */.  i
17cd4 6e 74 20 69 2c 20 6a 2c 20 6b 3b 20 20 20 20 20  nt i, j, k;     
17cd5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
17cd6 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a  oop counters */.
17cd7 20 20 69 6e 74 20 6e 78 44 69 76 3b 20 20 20 20    int nxDiv;    
17cd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
17cd9 2a 20 4e 65 78 74 20 64 69 76 69 64 65 72 20 73  * Next divider s
17cda 6c 6f 74 20 69 6e 20 70 50 61 72 65 6e 74 2d 3e  lot in pParent->
17cdb 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74  aCell[] */.  int
17cdc 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
17cdd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
17cde 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   return code */.
17cdf 20 20 75 31 36 20 6c 65 61 66 43 6f 72 72 65 63    u16 leafCorrec
17ce0 74 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 2f  tion;          /
17ce1 2a 20 34 20 69 66 20 70 50 61 67 65 20 69 73 20  * 4 if pPage is 
17ce2 61 20 6c 65 61 66 2e 20 20 30 20 69 66 20 6e 6f  a leaf.  0 if no
17ce3 74 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 61 66 44  t */.  int leafD
17ce4 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20  ata;            
17ce5 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70      /* True if p
17ce6 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 20 6f  Page is a leaf o
17ce7 66 20 61 20 4c 45 41 46 44 41 54 41 20 74 72 65  f a LEAFDATA tre
17ce8 65 20 2a 2f 0a 20 20 69 6e 74 20 75 73 61 62 6c  e */.  int usabl
17ce9 65 53 70 61 63 65 3b 20 20 20 20 20 20 20 20 20  eSpace;         
17cea 20 20 20 20 2f 2a 20 42 79 74 65 73 20 69 6e 20      /* Bytes in 
17ceb 70 50 61 67 65 20 62 65 79 6f 6e 64 20 74 68 65  pPage beyond the
17cec 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74   header */.  int
17ced 20 70 61 67 65 46 6c 61 67 73 3b 20 20 20 20 20   pageFlags;     
17cee 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c            /* Val
17cef 75 65 20 6f 66 20 70 50 61 67 65 2d 3e 61 44 61  ue of pPage->aDa
17cf0 74 61 5b 30 5d 20 2a 2f 0a 20 20 69 6e 74 20 73  ta[0] */.  int s
17cf1 75 62 74 6f 74 61 6c 3b 20 20 20 20 20 20 20 20  ubtotal;        
17cf2 20 20 20 20 20 20 20 20 2f 2a 20 53 75 62 74 6f          /* Subto
17cf3 74 61 6c 20 6f 66 20 62 79 74 65 73 20 69 6e 20  tal of bytes in 
17cf4 63 65 6c 6c 73 20 6f 6e 20 6f 6e 65 20 70 61 67  cells on one pag
17cf5 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 70 61 63  e */.  int iSpac
17cf6 65 31 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  e1 = 0;         
17cf7 20 20 20 20 2f 2a 20 46 69 72 73 74 20 75 6e 75      /* First unu
17cf8 73 65 64 20 62 79 74 65 20 6f 66 20 61 53 70 61  sed byte of aSpa
17cf9 63 65 31 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69  ce1[] */.  int i
17cfa 4f 76 66 6c 53 70 61 63 65 20 3d 20 30 3b 20 20  OvflSpace = 0;  
17cfb 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
17cfc 20 75 6e 75 73 65 64 20 62 79 74 65 20 6f 66 20   unused byte of 
17cfd 61 4f 76 66 6c 53 70 61 63 65 5b 5d 20 2a 2f 0a  aOvflSpace[] */.
17cfe 20 20 69 6e 74 20 73 7a 53 63 72 61 74 63 68 3b    int szScratch;
17cff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
17d00 2a 20 53 69 7a 65 20 6f 66 20 73 63 72 61 74 63  * Size of scratc
17d01 68 20 6d 65 6d 6f 72 79 20 72 65 71 75 65 73 74  h memory request
17d02 65 64 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  ed */.  MemPage 
17d03 2a 61 70 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20 20  *apOld[NB];     
17d04 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 61 6e       /* pPage an
17d05 64 20 75 70 20 74 6f 20 74 77 6f 20 73 69 62 6c  d up to two sibl
17d06 69 6e 67 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67  ings */.  MemPag
17d07 65 20 2a 61 70 43 6f 70 79 5b 4e 42 5d 3b 20 20  e *apCopy[NB];  
17d08 20 20 20 20 20 20 20 2f 2a 20 50 72 69 76 61 74         /* Privat
17d09 65 20 63 6f 70 69 65 73 20 6f 66 20 61 70 4f 6c  e copies of apOl
17d0a 64 5b 5d 20 70 61 67 65 73 20 2a 2f 0a 20 20 4d  d[] pages */.  M
17d0b 65 6d 50 61 67 65 20 2a 61 70 4e 65 77 5b 4e 42  emPage *apNew[NB
17d0c 2b 32 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 70  +2];        /* p
17d0d 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f 20 4e  Page and up to N
17d0e 42 20 73 69 62 6c 69 6e 67 73 20 61 66 74 65 72  B siblings after
17d0f 20 62 61 6c 61 6e 63 69 6e 67 20 2a 2f 0a 20 20   balancing */.  
17d10 75 38 20 2a 70 52 69 67 68 74 3b 20 20 20 20 20  u8 *pRight;     
17d11 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17d12 4c 6f 63 61 74 69 6f 6e 20 69 6e 20 70 61 72 65  Location in pare
17d13 6e 74 20 6f 66 20 72 69 67 68 74 2d 73 69 62 6c  nt of right-sibl
17d14 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20  ing pointer */. 
17d15 20 75 38 20 2a 61 70 44 69 76 5b 4e 42 2d 31 5d   u8 *apDiv[NB-1]
17d16 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
17d17 20 44 69 76 69 64 65 72 20 63 65 6c 6c 73 20 69   Divider cells i
17d18 6e 20 70 50 61 72 65 6e 74 20 2a 2f 0a 20 20 69  n pParent */.  i
17d19 6e 74 20 63 6e 74 4e 65 77 5b 4e 42 2b 32 5d 3b  nt cntNew[NB+2];
17d1a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
17d1b 6e 64 65 78 20 69 6e 20 61 43 65 6c 6c 5b 5d 20  ndex in aCell[] 
17d1c 6f 66 20 63 65 6c 6c 20 61 66 74 65 72 20 69 2d  of cell after i-
17d1d 74 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74  th page */.  int
17d1e 20 73 7a 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20   szNew[NB+2];   
17d1f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d            /* Com
17d20 62 69 6e 65 64 20 73 69 7a 65 20 6f 66 20 63 65  bined size of ce
17d21 6c 6c 73 20 70 6c 61 63 65 20 6f 6e 20 69 2d 74  lls place on i-t
17d22 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a  h page */.  u8 *
17d23 2a 61 70 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20  *apCell = 0;    
17d24 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20           /* All 
17d25 63 65 6c 6c 73 20 62 65 67 69 6e 20 62 61 6c 61  cells begin bala
17d26 6e 63 65 64 20 2a 2f 0a 20 20 75 31 36 20 2a 73  nced */.  u16 *s
17d27 7a 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  zCell;          
17d28 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20         /* Local 
17d29 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c  size of all cell
17d2a 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f  s in apCell[] */
17d2b 0a 20 20 75 38 20 2a 61 53 70 61 63 65 31 3b 20  .  u8 *aSpace1; 
17d2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17d2d 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 63 6f 70  /* Space for cop
17d2e 69 65 73 20 6f 66 20 64 69 76 69 64 65 72 73 20  ies of dividers 
17d2f 63 65 6c 6c 73 20 2a 2f 0a 20 20 50 67 6e 6f 20  cells */.  Pgno 
17d30 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20  pgno;           
17d31 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20          /* Temp 
17d32 76 61 72 20 74 6f 20 73 74 6f 72 65 20 61 20 70  var to store a p
17d33 61 67 65 20 6e 75 6d 62 65 72 20 69 6e 20 2a 2f  age number in */
17d34 0a 0a 20 20 70 42 74 20 3d 20 70 50 61 72 65 6e  ..  pBt = pParen
17d35 74 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74  t->pBt;.  assert
17d36 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
17d37 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
17d38 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
17d39 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
17d3a 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70  eable(pParent->p
17d3b 44 62 50 61 67 65 29 20 29 3b 0a 0a 23 69 66 20  DbPage) );..#if 
17d3c 30 0a 20 20 54 52 41 43 45 28 28 22 42 41 4c 41  0.  TRACE(("BALA
17d3d 4e 43 45 3a 20 62 65 67 69 6e 20 70 61 67 65 20  NCE: begin page 
17d3e 25 64 20 63 68 69 6c 64 20 6f 66 20 25 64 5c 6e  %d child of %d\n
17d3f 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20  ", pPage->pgno, 
17d40 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 29 29 3b  pParent->pgno));
17d41 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 74  .#endif..  /* At
17d42 20 74 68 69 73 20 70 6f 69 6e 74 20 70 50 61 72   this point pPar
17d43 65 6e 74 20 6d 61 79 20 68 61 76 65 20 61 74 20  ent may have at 
17d44 6d 6f 73 74 20 6f 6e 65 20 6f 76 65 72 66 6c 6f  most one overflo
17d45 77 20 63 65 6c 6c 2e 20 41 6e 64 20 69 66 0a 20  w cell. And if. 
17d46 20 2a 2a 20 74 68 69 73 20 6f 76 65 72 66 6c 6f   ** this overflo
17d47 77 20 63 65 6c 6c 20 69 73 20 70 72 65 73 65 6e  w cell is presen
17d48 74 2c 20 69 74 20 6d 75 73 74 20 62 65 20 74 68  t, it must be th
17d49 65 20 63 65 6c 6c 20 77 69 74 68 20 0a 20 20 2a  e cell with .  *
17d4a 2a 20 69 6e 64 65 78 20 69 50 61 72 65 6e 74 49  * index iParentI
17d4b 64 78 2e 20 54 68 69 73 20 73 63 65 6e 61 72 69  dx. This scenari
17d4c 6f 20 63 6f 6d 65 73 20 61 62 6f 75 74 20 77 68  o comes about wh
17d4d 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
17d4e 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20  .  ** is called 
17d4f 28 69 6e 64 69 72 65 63 74 6c 79 29 20 66 72 6f  (indirectly) fro
17d50 6d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65  m sqlite3BtreeDe
17d51 6c 65 74 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 61  lete()..  */.  a
17d52 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e  ssert( pParent->
17d53 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20  nOverflow==0 || 
17d54 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c  pParent->nOverfl
17d55 6f 77 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65 72  ow==1 );.  asser
17d56 74 28 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65  t( pParent->nOve
17d57 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 70 50 61 72  rflow==0 || pPar
17d58 65 6e 74 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64  ent->aOvfl[0].id
17d59 78 3d 3d 69 50 61 72 65 6e 74 49 64 78 20 29 3b  x==iParentIdx );
17d5a 0a 0a 20 20 69 66 28 20 21 61 4f 76 66 6c 53 70  ..  if( !aOvflSp
17d5b 61 63 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ace ){.    retur
17d5c 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
17d5d 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74    }..  /* Find t
17d5e 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73  he sibling pages
17d5f 20 74 6f 20 62 61 6c 61 6e 63 65 2e 20 41 6c 73   to balance. Als
17d60 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 63 65 6c  o locate the cel
17d61 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 20 0a 20  ls in pParent . 
17d62 20 2a 2a 20 74 68 61 74 20 64 69 76 69 64 65 20   ** that divide 
17d63 74 68 65 20 73 69 62 6c 69 6e 67 73 2e 20 41 6e  the siblings. An
17d64 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
17d65 20 74 6f 20 66 69 6e 64 20 4e 4e 20 73 69 62 6c   to find NN sibl
17d66 69 6e 67 73 20 6f 6e 20 0a 20 20 2a 2a 20 65 69  ings on .  ** ei
17d67 74 68 65 72 20 73 69 64 65 20 6f 66 20 70 50 61  ther side of pPa
17d68 67 65 2e 20 4d 6f 72 65 20 73 69 62 6c 69 6e 67  ge. More sibling
17d69 73 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d  s are taken from
17d6a 20 6f 6e 65 20 73 69 64 65 2c 20 68 6f 77 65 76   one side, howev
17d6b 65 72 2c 20 0a 20 20 2a 2a 20 69 66 20 74 68 65  er, .  ** if the
17d6c 72 65 20 61 72 65 20 66 65 77 65 72 20 74 68 61  re are fewer tha
17d6d 6e 20 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e  n NN siblings on
17d6e 20 74 68 65 20 6f 74 68 65 72 20 73 69 64 65 2e   the other side.
17d6f 20 49 66 20 70 50 61 72 65 6e 74 0a 20 20 2a 2a   If pParent.  **
17d70 20 68 61 73 20 4e 42 20 6f 72 20 66 65 77 65 72   has NB or fewer
17d71 20 63 68 69 6c 64 72 65 6e 20 74 68 65 6e 20 61   children then a
17d72 6c 6c 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 70  ll children of p
17d73 50 61 72 65 6e 74 20 61 72 65 20 74 61 6b 65 6e  Parent are taken
17d74 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  .  .  **.  ** Th
17d75 69 73 20 6c 6f 6f 70 20 61 6c 73 6f 20 64 72 6f  is loop also dro
17d76 70 73 20 74 68 65 20 64 69 76 69 64 65 72 20 63  ps the divider c
17d77 65 6c 6c 73 20 66 72 6f 6d 20 74 68 65 20 70 61  ells from the pa
17d78 72 65 6e 74 20 70 61 67 65 2e 20 54 68 69 73 0a  rent page. This.
17d79 20 20 2a 2a 20 77 61 79 2c 20 74 68 65 20 72 65    ** way, the re
17d7a 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 65 20 66  mainder of the f
17d7b 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74  unction does not
17d7c 20 68 61 76 65 20 74 6f 20 64 65 61 6c 20 77 69   have to deal wi
17d7d 74 68 20 61 6e 79 0a 20 20 2a 2a 20 6f 76 65 72  th any.  ** over
17d7e 66 6c 6f 77 20 63 65 6c 6c 73 20 69 6e 20 74 68  flow cells in th
17d7f 65 20 70 61 72 65 6e 74 20 70 61 67 65 2c 20 73  e parent page, s
17d80 69 6e 63 65 20 69 66 20 61 6e 79 20 65 78 69 73  ince if any exis
17d81 74 65 64 20 74 68 65 79 20 77 69 6c 6c 0a 20 20  ted they will.  
17d82 2a 2a 20 68 61 76 65 20 61 6c 72 65 61 64 79 20  ** have already 
17d83 62 65 65 6e 20 72 65 6d 6f 76 65 64 2e 0a 20 20  been removed..  
17d84 2a 2f 0a 20 20 69 20 3d 20 70 50 61 72 65 6e 74  */.  i = pParent
17d85 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 2b 20 70 50  ->nOverflow + pP
17d86 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3b 0a 20 20  arent->nCell;.  
17d87 69 66 28 20 69 3c 32 20 29 7b 0a 20 20 20 20 6e  if( i<2 ){.    n
17d88 78 44 69 76 20 3d 20 30 3b 0a 20 20 20 20 6e 4f  xDiv = 0;.    nO
17d89 6c 64 20 3d 20 69 2b 31 3b 0a 20 20 7d 65 6c 73  ld = i+1;.  }els
17d8a 65 7b 0a 20 20 20 20 6e 4f 6c 64 20 3d 20 33 3b  e{.    nOld = 3;
17d8b 0a 20 20 20 20 69 66 28 20 69 50 61 72 65 6e 74  .    if( iParent
17d8c 49 64 78 3d 3d 30 20 29 7b 20 20 20 20 20 20 20  Idx==0 ){       
17d8d 20 20 20 20 20 20 20 20 20 20 0a 20 20 20 20 20            .     
17d8e 20 6e 78 44 69 76 20 3d 20 30 3b 0a 20 20 20 20   nxDiv = 0;.    
17d8f 7d 65 6c 73 65 20 69 66 28 20 69 50 61 72 65 6e  }else if( iParen
17d90 74 49 64 78 3d 3d 69 20 29 7b 0a 20 20 20 20 20  tIdx==i ){.     
17d91 20 6e 78 44 69 76 20 3d 20 69 2d 32 3b 0a 20 20   nxDiv = i-2;.  
17d92 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e    }else{.      n
17d93 78 44 69 76 20 3d 20 69 50 61 72 65 6e 74 49 64  xDiv = iParentId
17d94 78 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  x-1;.    }.    i
17d95 20 3d 20 32 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 2;.  }.  if( 
17d96 28 69 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e 74  (i+nxDiv-pParent
17d97 2d 3e 6e 4f 76 65 72 66 6c 6f 77 29 3d 3d 70 50  ->nOverflow)==pP
17d98 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a  arent->nCell ){.
17d99 20 20 20 20 70 52 69 67 68 74 20 3d 20 26 70 50      pRight = &pP
17d9a 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61  arent->aData[pPa
17d9b 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b  rent->hdrOffset+
17d9c 38 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  8];.  }else{.   
17d9d 20 70 52 69 67 68 74 20 3d 20 66 69 6e 64 43 65   pRight = findCe
17d9e 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e 78  ll(pParent, i+nx
17d9f 44 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76  Div-pParent->nOv
17da0 65 72 66 6c 6f 77 29 3b 0a 20 20 7d 0a 20 20 70  erflow);.  }.  p
17da1 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70  gno = get4byte(p
17da2 52 69 67 68 74 29 3b 0a 20 20 77 68 69 6c 65 28  Right);.  while(
17da3 20 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 67   1 ){.    rc = g
17da4 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42  etAndInitPage(pB
17da5 74 2c 20 70 67 6e 6f 2c 20 26 61 70 4f 6c 64 5b  t, pgno, &apOld[
17da6 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  i]);.    if( rc 
17da7 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28  ){.      memset(
17da8 61 70 4f 6c 64 2c 20 30 2c 20 28 69 2b 31 29 2a  apOld, 0, (i+1)*
17da9 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 2a 29  sizeof(MemPage*)
17daa 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 61  );.      goto ba
17dab 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  lance_cleanup;. 
17dac 20 20 20 7d 0a 20 20 20 20 6e 4d 61 78 43 65 6c     }.    nMaxCel
17dad 6c 73 20 2b 3d 20 31 2b 61 70 4f 6c 64 5b 69 5d  ls += 1+apOld[i]
17dae 2d 3e 6e 43 65 6c 6c 2b 61 70 4f 6c 64 5b 69 5d  ->nCell+apOld[i]
17daf 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20  ->nOverflow;.   
17db0 20 69 66 28 20 28 69 2d 2d 29 3d 3d 30 20 29 20   if( (i--)==0 ) 
17db1 62 72 65 61 6b 3b 0a 0a 20 20 20 20 69 66 28 20  break;..    if( 
17db2 69 2b 6e 78 44 69 76 3d 3d 70 50 61 72 65 6e 74  i+nxDiv==pParent
17db3 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 20 26  ->aOvfl[0].idx &
17db4 26 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72  & pParent->nOver
17db5 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 61 70  flow ){.      ap
17db6 44 69 76 5b 69 5d 20 3d 20 70 50 61 72 65 6e 74  Div[i] = pParent
17db7 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c 6c  ->aOvfl[0].pCell
17db8 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 67  ;.      pgno = g
17db9 65 74 34 62 79 74 65 28 61 70 44 69 76 5b 69 5d  et4byte(apDiv[i]
17dba 29 3b 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b 69  );.      szNew[i
17dbb 5d 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28  ] = cellSizePtr(
17dbc 70 50 61 72 65 6e 74 2c 20 61 70 44 69 76 5b 69  pParent, apDiv[i
17dbd 5d 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e  ]);.      pParen
17dbe 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30  t->nOverflow = 0
17dbf 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
17dc0 20 20 20 61 70 44 69 76 5b 69 5d 20 3d 20 66 69     apDiv[i] = fi
17dc1 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20  ndCell(pParent, 
17dc2 69 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 2d  i+nxDiv-pParent-
17dc3 3e 6e 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 20  >nOverflow);.   
17dc4 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79     pgno = get4by
17dc5 74 65 28 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20  te(apDiv[i]);.  
17dc6 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 63      szNew[i] = c
17dc7 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 72 65  ellSizePtr(pPare
17dc8 6e 74 2c 20 61 70 44 69 76 5b 69 5d 29 3b 0a 0a  nt, apDiv[i]);..
17dc9 20 20 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68        /* Drop th
17dca 65 20 63 65 6c 6c 20 66 72 6f 6d 20 74 68 65 20  e cell from the 
17dcb 70 61 72 65 6e 74 20 70 61 67 65 2e 20 61 70 44  parent page. apD
17dcc 69 76 5b 69 5d 20 73 74 69 6c 6c 20 70 6f 69 6e  iv[i] still poin
17dcd 74 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74  ts to.      ** t
17dce 68 65 20 63 65 6c 6c 20 77 69 74 68 69 6e 20 74  he cell within t
17dcf 68 65 20 70 61 72 65 6e 74 2c 20 65 76 65 6e 20  he parent, even 
17dd0 74 68 6f 75 67 68 20 69 74 20 68 61 73 20 62 65  though it has be
17dd1 65 6e 20 64 72 6f 70 70 65 64 2e 0a 20 20 20 20  en dropped..    
17dd2 20 20 2a 2a 20 54 68 69 73 20 69 73 20 73 61 66    ** This is saf
17dd3 65 20 62 65 63 61 75 73 65 20 64 72 6f 70 70 69  e because droppi
17dd4 6e 67 20 61 20 63 65 6c 6c 20 6f 6e 6c 79 20 6f  ng a cell only o
17dd5 76 65 72 77 72 69 74 65 73 20 74 68 65 20 66 69  verwrites the fi
17dd6 72 73 74 0a 20 20 20 20 20 20 2a 2a 20 66 6f 75  rst.      ** fou
17dd7 72 20 62 79 74 65 73 20 6f 66 20 69 74 2c 20 61  r bytes of it, a
17dd8 6e 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  nd this function
17dd9 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
17dda 68 65 20 66 69 72 73 74 0a 20 20 20 20 20 20 2a  he first.      *
17ddb 2a 20 66 6f 75 72 20 62 79 74 65 73 20 6f 66 20  * four bytes of 
17ddc 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c  the divider cell
17ddd 2e 20 53 6f 20 74 68 65 20 70 6f 69 6e 74 65 72  . So the pointer
17dde 20 69 73 20 73 61 66 65 20 74 6f 20 75 73 65 0a   is safe to use.
17ddf 20 20 20 20 20 20 2a 2a 20 6c 61 74 65 72 20 6f        ** later o
17de0 6e 2e 20 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20  n.  .      **.  
17de1 20 20 20 20 2a 2a 20 55 6e 6c 65 73 73 20 53 51      ** Unless SQ
17de2 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64  Lite is compiled
17de3 20 69 6e 20 73 65 63 75 72 65 2d 64 65 6c 65 74   in secure-delet
17de4 65 20 6d 6f 64 65 2e 20 49 6e 20 74 68 69 73 20  e mode. In this 
17de5 63 61 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74  case,.      ** t
17de6 68 65 20 64 72 6f 70 43 65 6c 6c 28 29 20 72 6f  he dropCell() ro
17de7 75 74 69 6e 65 20 77 69 6c 6c 20 6f 76 65 72 77  utine will overw
17de8 72 69 74 65 20 74 68 65 20 65 6e 74 69 72 65 20  rite the entire 
17de9 63 65 6c 6c 20 77 69 74 68 20 7a 65 72 6f 65 73  cell with zeroes
17dea 2e 0a 20 20 20 20 20 20 2a 2a 20 49 6e 20 74 68  ..      ** In th
17deb 69 73 20 63 61 73 65 2c 20 74 65 6d 70 6f 72 61  is case, tempora
17dec 72 69 6c 79 20 63 6f 70 79 20 74 68 65 20 63 65  rily copy the ce
17ded 6c 6c 20 69 6e 74 6f 20 74 68 65 20 61 4f 76 66  ll into the aOvf
17dee 6c 53 70 61 63 65 5b 5d 0a 20 20 20 20 20 20 2a  lSpace[].      *
17def 2a 20 62 75 66 66 65 72 2e 20 49 74 20 77 69 6c  * buffer. It wil
17df0 6c 20 62 65 20 63 6f 70 69 65 64 20 6f 75 74 20  l be copied out 
17df1 61 67 61 69 6e 20 61 73 20 73 6f 6f 6e 20 61 73  again as soon as
17df2 20 74 68 65 20 61 53 70 61 63 65 5b 5d 20 62 75   the aSpace[] bu
17df3 66 66 65 72 0a 20 20 20 20 20 20 2a 2a 20 69 73  ffer.      ** is
17df4 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 2a 2f 0a   allocated.  */.
17df5 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45  #ifdef SQLITE_SE
17df6 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 20 20  CURE_DELETE.    
17df7 20 20 6d 65 6d 63 70 79 28 26 61 4f 76 66 6c 53    memcpy(&aOvflS
17df8 70 61 63 65 5b 61 70 44 69 76 5b 69 5d 2d 70 50  pace[apDiv[i]-pP
17df9 61 72 65 6e 74 2d 3e 61 44 61 74 61 5d 2c 20 61  arent->aData], a
17dfa 70 44 69 76 5b 69 5d 2c 20 73 7a 4e 65 77 5b 69  pDiv[i], szNew[i
17dfb 5d 29 3b 0a 20 20 20 20 20 20 61 70 44 69 76 5b  ]);.      apDiv[
17dfc 69 5d 20 3d 20 26 61 4f 76 66 6c 53 70 61 63 65  i] = &aOvflSpace
17dfd 5b 61 70 44 69 76 5b 69 5d 2d 70 50 61 72 65 6e  [apDiv[i]-pParen
17dfe 74 2d 3e 61 44 61 74 61 5d 3b 0a 23 65 6e 64 69  t->aData];.#endi
17dff 66 0a 20 20 20 20 20 20 64 72 6f 70 43 65 6c 6c  f.      dropCell
17e00 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e 78 44 69  (pParent, i+nxDi
17e01 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72  v-pParent->nOver
17e02 66 6c 6f 77 2c 20 73 7a 4e 65 77 5b 69 5d 2c 20  flow, szNew[i], 
17e03 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  &rc);.    }.  }.
17e04 0a 20 20 2f 2a 20 4d 61 6b 65 20 6e 4d 61 78 43  .  /* Make nMaxC
17e05 65 6c 6c 73 20 61 20 6d 75 6c 74 69 70 6c 65 20  ells a multiple 
17e06 6f 66 20 34 20 69 6e 20 6f 72 64 65 72 20 74 6f  of 4 in order to
17e07 20 70 72 65 73 65 72 76 65 20 38 2d 62 79 74 65   preserve 8-byte
17e08 0a 20 20 2a 2a 20 61 6c 69 67 6e 6d 65 6e 74 20  .  ** alignment 
17e09 2a 2f 0a 20 20 6e 4d 61 78 43 65 6c 6c 73 20 3d  */.  nMaxCells =
17e0a 20 28 6e 4d 61 78 43 65 6c 6c 73 20 2b 20 33 29   (nMaxCells + 3)
17e0b 26 7e 33 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  &~3;..  /*.  ** 
17e0c 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66  Allocate space f
17e0d 6f 72 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74  or memory struct
17e0e 75 72 65 73 0a 20 20 2a 2f 0a 20 20 6b 20 3d 20  ures.  */.  k = 
17e0f 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20  pBt->pageSize + 
17e10 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65  ROUND8(sizeof(Me
17e11 6d 50 61 67 65 29 29 3b 0a 20 20 73 7a 53 63 72  mPage));.  szScr
17e12 61 74 63 68 20 3d 0a 20 20 20 20 20 20 20 6e 4d  atch =.       nM
17e13 61 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75  axCells*sizeof(u
17e14 38 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 20  8*)             
17e15 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 70 43            /* apC
17e16 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 6e 4d  ell */.     + nM
17e17 61 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75  axCells*sizeof(u
17e18 31 36 29 20 20 20 20 20 20 20 20 20 20 20 20 20  16)             
17e19 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 43            /* szC
17e1a 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 70 42  ell */.     + pB
17e1b 74 2d 3e 70 61 67 65 53 69 7a 65 20 20 20 20 20  t->pageSize     
17e1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17e1d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 53 70            /* aSp
17e1e 61 63 65 31 20 2a 2f 0a 20 20 20 20 20 2b 20 6b  ace1 */.     + k
17e1f 2a 6e 4f 6c 64 3b 20 20 20 20 20 20 20 20 20 20  *nOld;          
17e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17e21 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
17e22 67 65 20 63 6f 70 69 65 73 20 28 61 70 43 6f 70  ge copies (apCop
17e23 79 29 20 2a 2f 0a 20 20 61 70 43 65 6c 6c 20 3d  y) */.  apCell =
17e24 20 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d   sqlite3ScratchM
17e25 61 6c 6c 6f 63 28 20 73 7a 53 63 72 61 74 63 68  alloc( szScratch
17e26 20 29 3b 20 0a 20 20 69 66 28 20 61 70 43 65 6c   ); .  if( apCel
17e27 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d  l==0 ){.    rc =
17e28 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
17e29 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f     goto balance_
17e2a 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 73  cleanup;.  }.  s
17e2b 7a 43 65 6c 6c 20 3d 20 28 75 31 36 2a 29 26 61  zCell = (u16*)&a
17e2c 70 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d  pCell[nMaxCells]
17e2d 3b 0a 20 20 61 53 70 61 63 65 31 20 3d 20 28 75  ;.  aSpace1 = (u
17e2e 38 2a 29 26 73 7a 43 65 6c 6c 5b 6e 4d 61 78 43  8*)&szCell[nMaxC
17e2f 65 6c 6c 73 5d 3b 0a 20 20 61 73 73 65 72 74 28  ells];.  assert(
17e30 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47   EIGHT_BYTE_ALIG
17e31 4e 4d 45 4e 54 28 61 53 70 61 63 65 31 29 20 29  NMENT(aSpace1) )
17e32 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4c 6f 61  ;..  /*.  ** Loa
17e33 64 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 61 6c  d pointers to al
17e34 6c 20 63 65 6c 6c 73 20 6f 6e 20 73 69 62 6c 69  l cells on sibli
17e35 6e 67 20 70 61 67 65 73 20 61 6e 64 20 74 68 65  ng pages and the
17e36 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 0a 20   divider cells. 
17e37 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6c 6f 63   ** into the loc
17e38 61 6c 20 61 70 43 65 6c 6c 5b 5d 20 61 72 72 61  al apCell[] arra
17e39 79 2e 20 20 4d 61 6b 65 20 63 6f 70 69 65 73 20  y.  Make copies 
17e3a 6f 66 20 74 68 65 20 64 69 76 69 64 65 72 20 63  of the divider c
17e3b 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 73  ells.  ** into s
17e3c 70 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72  pace obtained fr
17e3d 6f 6d 20 61 53 70 61 63 65 31 5b 5d 20 61 6e 64  om aSpace1[] and
17e3e 20 72 65 6d 6f 76 65 20 74 68 65 20 74 68 65 20   remove the the 
17e3f 64 69 76 69 64 65 72 20 43 65 6c 6c 73 0a 20 20  divider Cells.  
17e40 2a 2a 20 66 72 6f 6d 20 70 50 61 72 65 6e 74 2e  ** from pParent.
17e41 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68  .  **.  ** If th
17e42 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6f  e siblings are o
17e43 6e 20 6c 65 61 66 20 70 61 67 65 73 2c 20 74 68  n leaf pages, th
17e44 65 6e 20 74 68 65 20 63 68 69 6c 64 20 70 6f 69  en the child poi
17e45 6e 74 65 72 73 20 6f 66 20 74 68 65 0a 20 20 2a  nters of the.  *
17e46 2a 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 20  * divider cells 
17e47 61 72 65 20 73 74 72 69 70 70 65 64 20 66 72 6f  are stripped fro
17e48 6d 20 74 68 65 20 63 65 6c 6c 73 20 62 65 66 6f  m the cells befo
17e49 72 65 20 74 68 65 79 20 61 72 65 20 63 6f 70 69  re they are copi
17e4a 65 64 0a 20 20 2a 2a 20 69 6e 74 6f 20 61 53 70  ed.  ** into aSp
17e4b 61 63 65 31 5b 5d 2e 20 20 49 6e 20 74 68 69 73  ace1[].  In this
17e4c 20 77 61 79 2c 20 61 6c 6c 20 63 65 6c 6c 73 20   way, all cells 
17e4d 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 72 65 20  in apCell[] are 
17e4e 77 69 74 68 6f 75 74 0a 20 20 2a 2a 20 63 68 69  without.  ** chi
17e4f 6c 64 20 70 6f 69 6e 74 65 72 73 2e 20 20 49 66  ld pointers.  If
17e50 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6e 6f   siblings are no
17e51 74 20 6c 65 61 76 65 73 2c 20 74 68 65 6e 20 61  t leaves, then a
17e52 6c 6c 20 63 65 6c 6c 20 69 6e 0a 20 20 2a 2a 20  ll cell in.  ** 
17e53 61 70 43 65 6c 6c 5b 5d 20 69 6e 63 6c 75 64 65  apCell[] include
17e54 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 2e   child pointers.
17e55 20 20 45 69 74 68 65 72 20 77 61 79 2c 20 61 6c    Either way, al
17e56 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c  l cells in apCel
17e57 6c 5b 5d 0a 20 20 2a 2a 20 61 72 65 20 61 6c 69  l[].  ** are ali
17e58 6b 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 6c 65  ke..  **.  ** le
17e59 61 66 43 6f 72 72 65 63 74 69 6f 6e 3a 20 20 34  afCorrection:  4
17e5a 20 69 66 20 70 50 61 67 65 20 69 73 20 61 20 6c   if pPage is a l
17e5b 65 61 66 2e 20 20 30 20 69 66 20 70 50 61 67 65  eaf.  0 if pPage
17e5c 20 69 73 20 6e 6f 74 20 61 20 6c 65 61 66 2e 0a   is not a leaf..
17e5d 20 20 2a 2a 20 20 20 20 20 20 20 6c 65 61 66 44    **       leafD
17e5e 61 74 61 3a 20 20 31 20 69 66 20 70 50 61 67 65  ata:  1 if pPage
17e5f 20 68 6f 6c 64 73 20 6b 65 79 2b 64 61 74 61 20   holds key+data 
17e60 61 6e 64 20 70 50 61 72 65 6e 74 20 68 6f 6c 64  and pParent hold
17e61 73 20 6f 6e 6c 79 20 6b 65 79 73 2e 0a 20 20 2a  s only keys..  *
17e62 2f 0a 20 20 6c 65 61 66 43 6f 72 72 65 63 74 69  /.  leafCorrecti
17e63 6f 6e 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 6c  on = apOld[0]->l
17e64 65 61 66 2a 34 3b 0a 20 20 6c 65 61 66 44 61 74  eaf*4;.  leafDat
17e65 61 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 68 61  a = apOld[0]->ha
17e66 73 44 61 74 61 3b 0a 20 20 66 6f 72 28 69 3d 30  sData;.  for(i=0
17e67 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a  ; i<nOld; i++){.
17e68 20 20 20 20 69 6e 74 20 6c 69 6d 69 74 3b 0a 20      int limit;. 
17e69 20 20 20 0a 20 20 20 20 2f 2a 20 42 65 66 6f 72     .    /* Befor
17e6a 65 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67  e doing anything
17e6b 20 65 6c 73 65 2c 20 74 61 6b 65 20 61 20 63 6f   else, take a co
17e6c 70 79 20 6f 66 20 74 68 65 20 69 27 74 68 20 6f  py of the i'th o
17e6d 72 69 67 69 6e 61 6c 20 73 69 62 6c 69 6e 67 0a  riginal sibling.
17e6e 20 20 20 20 2a 2a 20 54 68 65 20 72 65 73 74 20      ** The rest 
17e6f 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  of this function
17e70 20 77 69 6c 6c 20 75 73 65 20 64 61 74 61 20 66   will use data f
17e71 72 6f 6d 20 74 68 65 20 63 6f 70 69 65 73 20 72  rom the copies r
17e72 61 74 68 65 72 0a 20 20 20 20 2a 2a 20 74 68 61  ather.    ** tha
17e73 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70  t the original p
17e74 61 67 65 73 20 73 69 6e 63 65 20 74 68 65 20 6f  ages since the o
17e75 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 77 69  riginal pages wi
17e76 6c 6c 20 62 65 20 69 6e 20 74 68 65 0a 20 20 20  ll be in the.   
17e77 20 2a 2a 20 70 72 6f 63 65 73 73 20 6f 66 20 62   ** process of b
17e78 65 69 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e  eing overwritten
17e79 2e 20 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67  .  */.    MemPag
17e7a 65 20 2a 70 4f 6c 64 20 3d 20 61 70 43 6f 70 79  e *pOld = apCopy
17e7b 5b 69 5d 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29  [i] = (MemPage*)
17e7c 26 61 53 70 61 63 65 31 5b 70 42 74 2d 3e 70 61  &aSpace1[pBt->pa
17e7d 67 65 53 69 7a 65 20 2b 20 6b 2a 69 5d 3b 0a 20  geSize + k*i];. 
17e7e 20 20 20 6d 65 6d 63 70 79 28 70 4f 6c 64 2c 20     memcpy(pOld, 
17e7f 61 70 4f 6c 64 5b 69 5d 2c 20 73 69 7a 65 6f 66  apOld[i], sizeof
17e80 28 4d 65 6d 50 61 67 65 29 29 3b 0a 20 20 20 20  (MemPage));.    
17e81 70 4f 6c 64 2d 3e 61 44 61 74 61 20 3d 20 28 76  pOld->aData = (v
17e82 6f 69 64 2a 29 26 70 4f 6c 64 5b 31 5d 3b 0a 20  oid*)&pOld[1];. 
17e83 20 20 20 6d 65 6d 63 70 79 28 70 4f 6c 64 2d 3e     memcpy(pOld->
17e84 61 44 61 74 61 2c 20 61 70 4f 6c 64 5b 69 5d 2d  aData, apOld[i]-
17e85 3e 61 44 61 74 61 2c 20 70 42 74 2d 3e 70 61 67  >aData, pBt->pag
17e86 65 53 69 7a 65 29 3b 0a 0a 20 20 20 20 6c 69 6d  eSize);..    lim
17e87 69 74 20 3d 20 70 4f 6c 64 2d 3e 6e 43 65 6c 6c  it = pOld->nCell
17e88 2b 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77  +pOld->nOverflow
17e89 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a  ;.    for(j=0; j
17e8a 3c 6c 69 6d 69 74 3b 20 6a 2b 2b 29 7b 0a 20 20  <limit; j++){.  
17e8b 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c      assert( nCel
17e8c 6c 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20  l<nMaxCells );. 
17e8d 20 20 20 20 20 61 70 43 65 6c 6c 5b 6e 43 65 6c       apCell[nCel
17e8e 6c 5d 20 3d 20 66 69 6e 64 4f 76 65 72 66 6c 6f  l] = findOverflo
17e8f 77 43 65 6c 6c 28 70 4f 6c 64 2c 20 6a 29 3b 0a  wCell(pOld, j);.
17e90 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65        szCell[nCe
17e91 6c 6c 5d 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74  ll] = cellSizePt
17e92 72 28 70 4f 6c 64 2c 20 61 70 43 65 6c 6c 5b 6e  r(pOld, apCell[n
17e93 43 65 6c 6c 5d 29 3b 0a 20 20 20 20 20 20 6e 43  Cell]);.      nC
17e94 65 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20  ell++;.    }.   
17e95 20 69 66 28 20 69 3c 6e 4f 6c 64 2d 31 20 26 26   if( i<nOld-1 &&
17e96 20 21 6c 65 61 66 44 61 74 61 29 7b 0a 20 20 20   !leafData){.   
17e97 20 20 20 75 31 36 20 73 7a 20 3d 20 28 75 31 36     u16 sz = (u16
17e98 29 73 7a 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20  )szNew[i];.     
17e99 20 75 38 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20   u8 *pTemp;.    
17e9a 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3c    assert( nCell<
17e9b 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20  nMaxCells );.   
17e9c 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d     szCell[nCell]
17e9d 20 3d 20 73 7a 3b 0a 20 20 20 20 20 20 70 54 65   = sz;.      pTe
17e9e 6d 70 20 3d 20 26 61 53 70 61 63 65 31 5b 69 53  mp = &aSpace1[iS
17e9f 70 61 63 65 31 5d 3b 0a 20 20 20 20 20 20 69 53  pace1];.      iS
17ea0 70 61 63 65 31 20 2b 3d 20 73 7a 3b 0a 20 20 20  pace1 += sz;.   
17ea1 20 20 20 61 73 73 65 72 74 28 20 73 7a 3c 3d 70     assert( sz<=p
17ea2 42 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20 29  Bt->pageSize/4 )
17ea3 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
17ea4 69 53 70 61 63 65 31 3c 3d 70 42 74 2d 3e 70 61  iSpace1<=pBt->pa
17ea5 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20  geSize );.      
17ea6 6d 65 6d 63 70 79 28 70 54 65 6d 70 2c 20 61 70  memcpy(pTemp, ap
17ea7 44 69 76 5b 69 5d 2c 20 73 7a 29 3b 0a 20 20 20  Div[i], sz);.   
17ea8 20 20 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d     apCell[nCell]
17ea9 20 3d 20 70 54 65 6d 70 2b 6c 65 61 66 43 6f 72   = pTemp+leafCor
17eaa 72 65 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 61  rection;.      a
17eab 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72 72 65  ssert( leafCorre
17eac 63 74 69 6f 6e 3d 3d 30 20 7c 7c 20 6c 65 61 66  ction==0 || leaf
17ead 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b  Correction==4 );
17eae 0a 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43  .      szCell[nC
17eaf 65 6c 6c 5d 20 3d 20 73 7a 43 65 6c 6c 5b 6e 43  ell] = szCell[nC
17eb0 65 6c 6c 5d 20 2d 20 6c 65 61 66 43 6f 72 72 65  ell] - leafCorre
17eb1 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 69 66 28  ction;.      if(
17eb2 20 21 70 4f 6c 64 2d 3e 6c 65 61 66 20 29 7b 0a   !pOld->leaf ){.
17eb3 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
17eb4 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d  leafCorrection==
17eb5 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  0 );.        ass
17eb6 65 72 74 28 20 70 4f 6c 64 2d 3e 68 64 72 4f 66  ert( pOld->hdrOf
17eb7 66 73 65 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20  fset==0 );.     
17eb8 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 20     /* The right 
17eb9 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 63  pointer of the c
17eba 68 69 6c 64 20 70 61 67 65 20 70 4f 6c 64 20 62  hild page pOld b
17ebb 65 63 6f 6d 65 73 20 74 68 65 20 6c 65 66 74 0a  ecomes the left.
17ebc 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74          ** point
17ebd 65 72 20 6f 66 20 74 68 65 20 64 69 76 69 64 65  er of the divide
17ebe 72 20 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 20  r cell */.      
17ebf 20 20 6d 65 6d 63 70 79 28 61 70 43 65 6c 6c 5b    memcpy(apCell[
17ec0 6e 43 65 6c 6c 5d 2c 20 26 70 4f 6c 64 2d 3e 61  nCell], &pOld->a
17ec1 44 61 74 61 5b 38 5d 2c 20 34 29 3b 0a 20 20 20  Data[8], 4);.   
17ec2 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
17ec3 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 6f    assert( leafCo
17ec4 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a 20  rrection==4 );. 
17ec5 20 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c         if( szCel
17ec6 6c 5b 6e 43 65 6c 6c 5d 3c 34 20 29 7b 0a 20 20  l[nCell]<4 ){.  
17ec7 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f          /* Do no
17ec8 74 20 61 6c 6c 6f 77 20 61 6e 79 20 63 65 6c 6c  t allow any cell
17ec9 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 34  s smaller than 4
17eca 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 20 20 20   bytes. */.     
17ecb 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c       szCell[nCel
17ecc 6c 5d 20 3d 20 34 3b 0a 20 20 20 20 20 20 20 20  l] = 4;.        
17ecd 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
17ece 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a 20  nCell++;.    }. 
17ecf 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69   }..  /*.  ** Fi
17ed0 67 75 72 65 20 6f 75 74 20 74 68 65 20 6e 75 6d  gure out the num
17ed1 62 65 72 20 6f 66 20 70 61 67 65 73 20 6e 65 65  ber of pages nee
17ed2 64 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20  ded to hold all 
17ed3 6e 43 65 6c 6c 20 63 65 6c 6c 73 2e 0a 20 20 2a  nCell cells..  *
17ed4 2a 20 53 74 6f 72 65 20 74 68 69 73 20 6e 75 6d  * Store this num
17ed5 62 65 72 20 69 6e 20 22 6b 22 2e 20 20 41 6c 73  ber in "k".  Als
17ed6 6f 20 63 6f 6d 70 75 74 65 20 73 7a 4e 65 77 5b  o compute szNew[
17ed7 5d 20 77 68 69 63 68 20 69 73 20 74 68 65 20 74  ] which is the t
17ed8 6f 74 61 6c 0a 20 20 2a 2a 20 73 69 7a 65 20 6f  otal.  ** size o
17ed9 66 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 74  f all cells on t
17eda 68 65 20 69 2d 74 68 20 70 61 67 65 20 61 6e 64  he i-th page and
17edb 20 63 6e 74 4e 65 77 5b 5d 20 77 68 69 63 68 20   cntNew[] which 
17edc 69 73 20 74 68 65 20 69 6e 64 65 78 0a 20 20 2a  is the index.  *
17edd 2a 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 6f 66  * in apCell[] of
17ede 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20 64   the cell that d
17edf 69 76 69 64 65 73 20 70 61 67 65 20 69 20 66 72  ivides page i fr
17ee0 6f 6d 20 70 61 67 65 20 69 2b 31 2e 20 20 0a 20  om page i+1.  . 
17ee1 20 2a 2a 20 63 6e 74 4e 65 77 5b 6b 5d 20 73 68   ** cntNew[k] sh
17ee2 6f 75 6c 64 20 65 71 75 61 6c 20 6e 43 65 6c 6c  ould equal nCell
17ee3 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 6c 75  ..  **.  ** Valu
17ee4 65 73 20 63 6f 6d 70 75 74 65 64 20 62 79 20 74  es computed by t
17ee5 68 69 73 20 62 6c 6f 63 6b 3a 0a 20 20 2a 2a 0a  his block:.  **.
17ee6 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6b    **           k
17ee7 3a 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  : The total numb
17ee8 65 72 20 6f 66 20 73 69 62 6c 69 6e 67 20 70 61  er of sibling pa
17ee9 67 65 73 0a 20 20 2a 2a 20 20 20 20 73 7a 4e 65  ges.  **    szNe
17eea 77 5b 69 5d 3a 20 53 70 61 63 65 64 20 75 73 65  w[i]: Spaced use
17eeb 64 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 73 69  d on the i-th si
17eec 62 6c 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a  bling page..  **
17eed 20 20 20 63 6e 74 4e 65 77 5b 69 5d 3a 20 49 6e     cntNew[i]: In
17eee 64 65 78 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20  dex in apCell[] 
17eef 61 6e 64 20 73 7a 43 65 6c 6c 5b 5d 20 66 6f 72  and szCell[] for
17ef0 20 74 68 65 20 66 69 72 73 74 20 63 65 6c 6c 20   the first cell 
17ef1 74 6f 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20  to.  **         
17ef2 20 20 20 20 20 74 68 65 20 72 69 67 68 74 20 6f       the right o
17ef3 66 20 74 68 65 20 69 2d 74 68 20 73 69 62 6c 69  f the i-th sibli
17ef4 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a 20 75 73  ng page..  ** us
17ef5 61 62 6c 65 53 70 61 63 65 3a 20 4e 75 6d 62 65  ableSpace: Numbe
17ef6 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70  r of bytes of sp
17ef7 61 63 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e  ace available on
17ef8 20 65 61 63 68 20 73 69 62 6c 69 6e 67 2e 0a 20   each sibling.. 
17ef9 20 2a 2a 20 0a 20 20 2a 2f 0a 20 20 75 73 61 62   ** .  */.  usab
17efa 6c 65 53 70 61 63 65 20 3d 20 70 42 74 2d 3e 75  leSpace = pBt->u
17efb 73 61 62 6c 65 53 69 7a 65 20 2d 20 31 32 20 2b  sableSize - 12 +
17efc 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b   leafCorrection;
17efd 0a 20 20 66 6f 72 28 73 75 62 74 6f 74 61 6c 3d  .  for(subtotal=
17efe 6b 3d 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20  k=i=0; i<nCell; 
17eff 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74  i++){.    assert
17f00 28 20 69 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b  ( i<nMaxCells );
17f01 0a 20 20 20 20 73 75 62 74 6f 74 61 6c 20 2b 3d  .    subtotal +=
17f02 20 73 7a 43 65 6c 6c 5b 69 5d 20 2b 20 32 3b 0a   szCell[i] + 2;.
17f03 20 20 20 20 69 66 28 20 73 75 62 74 6f 74 61 6c      if( subtotal
17f04 20 3e 20 75 73 61 62 6c 65 53 70 61 63 65 20 29   > usableSpace )
17f05 7b 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b 6b 5d  {.      szNew[k]
17f06 20 3d 20 73 75 62 74 6f 74 61 6c 20 2d 20 73 7a   = subtotal - sz
17f07 43 65 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 63  Cell[i];.      c
17f08 6e 74 4e 65 77 5b 6b 5d 20 3d 20 69 3b 0a 20 20  ntNew[k] = i;.  
17f09 20 20 20 20 69 66 28 20 6c 65 61 66 44 61 74 61      if( leafData
17f0a 20 29 7b 20 69 2d 2d 3b 20 7d 0a 20 20 20 20 20   ){ i--; }.     
17f0b 20 73 75 62 74 6f 74 61 6c 20 3d 20 30 3b 0a 20   subtotal = 0;. 
17f0c 20 20 20 20 20 6b 2b 2b 3b 0a 20 20 20 20 20 20       k++;.      
17f0d 69 66 28 20 6b 3e 4e 42 2b 31 20 29 7b 20 72 63  if( k>NB+1 ){ rc
17f0e 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50   = SQLITE_CORRUP
17f0f 54 3b 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f  T; goto balance_
17f10 63 6c 65 61 6e 75 70 3b 20 7d 0a 20 20 20 20 7d  cleanup; }.    }
17f11 0a 20 20 7d 0a 20 20 73 7a 4e 65 77 5b 6b 5d 20  .  }.  szNew[k] 
17f12 3d 20 73 75 62 74 6f 74 61 6c 3b 0a 20 20 63 6e  = subtotal;.  cn
17f13 74 4e 65 77 5b 6b 5d 20 3d 20 6e 43 65 6c 6c 3b  tNew[k] = nCell;
17f14 0a 20 20 6b 2b 2b 3b 0a 0a 20 20 2f 2a 0a 20 20  .  k++;..  /*.  
17f15 2a 2a 20 54 68 65 20 70 61 63 6b 69 6e 67 20 63  ** The packing c
17f16 6f 6d 70 75 74 65 64 20 62 79 20 74 68 65 20 70  omputed by the p
17f17 72 65 76 69 6f 75 73 20 62 6c 6f 63 6b 20 69 73  revious block is
17f18 20 62 69 61 73 65 64 20 74 6f 77 61 72 64 20 74   biased toward t
17f19 68 65 20 73 69 62 6c 69 6e 67 73 0a 20 20 2a 2a  he siblings.  **
17f1a 20 6f 6e 20 74 68 65 20 6c 65 66 74 20 73 69 64   on the left sid
17f1b 65 2e 20 20 54 68 65 20 6c 65 66 74 20 73 69 62  e.  The left sib
17f1c 6c 69 6e 67 73 20 61 72 65 20 61 6c 77 61 79 73  lings are always
17f1d 20 6e 65 61 72 6c 79 20 66 75 6c 6c 2c 20 77 68   nearly full, wh
17f1e 69 6c 65 20 74 68 65 0a 20 20 2a 2a 20 72 69 67  ile the.  ** rig
17f1f 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e 67 20  ht-most sibling 
17f20 6d 69 67 68 74 20 62 65 20 6e 65 61 72 6c 79 20  might be nearly 
17f21 65 6d 70 74 79 2e 20 20 54 68 69 73 20 62 6c 6f  empty.  This blo
17f22 63 6b 20 6f 66 20 63 6f 64 65 20 61 74 74 65 6d  ck of code attem
17f23 70 74 73 0a 20 20 2a 2a 20 74 6f 20 61 64 6a 75  pts.  ** to adju
17f24 73 74 20 74 68 65 20 70 61 63 6b 69 6e 67 20 6f  st the packing o
17f25 66 20 73 69 62 6c 69 6e 67 73 20 74 6f 20 67 65  f siblings to ge
17f26 74 20 61 20 62 65 74 74 65 72 20 62 61 6c 61 6e  t a better balan
17f27 63 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  ce..  **.  ** Th
17f28 69 73 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 73  is adjustment is
17f29 20 6d 6f 72 65 20 74 68 61 6e 20 61 6e 20 6f 70   more than an op
17f2a 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65  timization.  The
17f2b 20 70 61 63 6b 69 6e 67 20 61 62 6f 76 65 20 6d   packing above m
17f2c 69 67 68 74 0a 20 20 2a 2a 20 62 65 20 73 6f 20  ight.  ** be so 
17f2d 6f 75 74 20 6f 66 20 62 61 6c 61 6e 63 65 20 61  out of balance a
17f2e 73 20 74 6f 20 62 65 20 69 6c 6c 65 67 61 6c 2e  s to be illegal.
17f2f 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74    For example, t
17f30 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 0a 20 20  he right-most.  
17f31 2a 2a 20 73 69 62 6c 69 6e 67 20 6d 69 67 68 74  ** sibling might
17f32 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65   be completely e
17f33 6d 70 74 79 2e 20 20 54 68 69 73 20 61 64 6a 75  mpty.  This adju
17f34 73 74 6d 65 6e 74 20 69 73 20 6e 6f 74 20 6f 70  stment is not op
17f35 74 69 6f 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 66  tional..  */.  f
17f36 6f 72 28 69 3d 6b 2d 31 3b 20 69 3e 30 3b 20 69  or(i=k-1; i>0; i
17f37 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 73 7a 52  --){.    int szR
17f38 69 67 68 74 20 3d 20 73 7a 4e 65 77 5b 69 5d 3b  ight = szNew[i];
17f39 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 62    /* Size of sib
17f3a 6c 69 6e 67 20 6f 6e 20 74 68 65 20 72 69 67 68  ling on the righ
17f3b 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 4c  t */.    int szL
17f3c 65 66 74 20 3d 20 73 7a 4e 65 77 5b 69 2d 31 5d  eft = szNew[i-1]
17f3d 3b 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 62  ; /* Size of sib
17f3e 6c 69 6e 67 20 6f 6e 20 74 68 65 20 6c 65 66 74  ling on the left
17f3f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 3b 20 20   */.    int r;  
17f40 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
17f41 6e 64 65 78 20 6f 66 20 72 69 67 68 74 2d 6d 6f  ndex of right-mo
17f42 73 74 20 63 65 6c 6c 20 69 6e 20 6c 65 66 74 20  st cell in left 
17f43 73 69 62 6c 69 6e 67 20 2a 2f 0a 20 20 20 20 69  sibling */.    i
17f44 6e 74 20 64 3b 20 20 20 20 20 20 20 20 20 20 20  nt d;           
17f45 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66     /* Index of f
17f46 69 72 73 74 20 63 65 6c 6c 20 74 6f 20 74 68 65  irst cell to the
17f47 20 6c 65 66 74 20 6f 66 20 72 69 67 68 74 20 73   left of right s
17f48 69 62 6c 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 72  ibling */..    r
17f49 20 3d 20 63 6e 74 4e 65 77 5b 69 2d 31 5d 20 2d   = cntNew[i-1] -
17f4a 20 31 3b 0a 20 20 20 20 64 20 3d 20 72 20 2b 20   1;.    d = r + 
17f4b 31 20 2d 20 6c 65 61 66 44 61 74 61 3b 0a 20 20  1 - leafData;.  
17f4c 20 20 61 73 73 65 72 74 28 20 64 3c 6e 4d 61 78    assert( d<nMax
17f4d 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 61 73 73  Cells );.    ass
17f4e 65 72 74 28 20 72 3c 6e 4d 61 78 43 65 6c 6c 73  ert( r<nMaxCells
17f4f 20 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73   );.    while( s
17f50 7a 52 69 67 68 74 3d 3d 30 20 7c 7c 20 73 7a 52  zRight==0 || szR
17f51 69 67 68 74 2b 73 7a 43 65 6c 6c 5b 64 5d 2b 32  ight+szCell[d]+2
17f52 3c 3d 73 7a 4c 65 66 74 2d 28 73 7a 43 65 6c 6c  <=szLeft-(szCell
17f53 5b 72 5d 2b 32 29 20 29 7b 0a 20 20 20 20 20 20  [r]+2) ){.      
17f54 73 7a 52 69 67 68 74 20 2b 3d 20 73 7a 43 65 6c  szRight += szCel
17f55 6c 5b 64 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20  l[d] + 2;.      
17f56 73 7a 4c 65 66 74 20 2d 3d 20 73 7a 43 65 6c 6c  szLeft -= szCell
17f57 5b 72 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 63  [r] + 2;.      c
17f58 6e 74 4e 65 77 5b 69 2d 31 5d 2d 2d 3b 0a 20 20  ntNew[i-1]--;.  
17f59 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69      r = cntNew[i
17f5a 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20 64  -1] - 1;.      d
17f5b 20 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44   = r + 1 - leafD
17f5c 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ata;.    }.    s
17f5d 7a 4e 65 77 5b 69 5d 20 3d 20 73 7a 52 69 67 68  zNew[i] = szRigh
17f5e 74 3b 0a 20 20 20 20 73 7a 4e 65 77 5b 69 2d 31  t;.    szNew[i-1
17f5f 5d 20 3d 20 73 7a 4c 65 66 74 3b 0a 20 20 7d 0a  ] = szLeft;.  }.
17f60 0a 20 20 2f 2a 20 45 69 74 68 65 72 20 77 65 20  .  /* Either we 
17f61 66 6f 75 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72  found one or mor
17f62 65 20 63 65 6c 6c 73 20 28 63 6e 74 6e 65 77 5b  e cells (cntnew[
17f63 30 5d 29 3e 30 29 20 6f 72 20 70 50 61 67 65 20  0])>0) or pPage 
17f64 69 73 0a 20 20 2a 2a 20 61 20 76 69 72 74 75 61  is.  ** a virtua
17f65 6c 20 72 6f 6f 74 20 70 61 67 65 2e 20 20 41 20  l root page.  A 
17f66 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67  virtual root pag
17f67 65 20 69 73 20 77 68 65 6e 20 74 68 65 20 72 65  e is when the re
17f68 61 6c 20 72 6f 6f 74 0a 20 20 2a 2a 20 70 61 67  al root.  ** pag
17f69 65 20 69 73 20 70 61 67 65 20 31 20 61 6e 64 20  e is page 1 and 
17f6a 77 65 20 61 72 65 20 74 68 65 20 6f 6e 6c 79 20  we are the only 
17f6b 63 68 69 6c 64 20 6f 66 20 74 68 61 74 20 70 61  child of that pa
17f6c 67 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ge..  */.  asser
17f6d 74 28 20 63 6e 74 4e 65 77 5b 30 5d 3e 30 20 7c  t( cntNew[0]>0 |
17f6e 7c 20 28 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f  | (pParent->pgno
17f6f 3d 3d 31 20 26 26 20 70 50 61 72 65 6e 74 2d 3e  ==1 && pParent->
17f70 6e 43 65 6c 6c 3d 3d 30 29 20 29 3b 0a 0a 20 20  nCell==0) );..  
17f71 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a  TRACE(("BALANCE:
17f72 20 6f 6c 64 3a 20 25 64 20 25 64 20 25 64 20 20   old: %d %d %d  
17f73 22 2c 0a 20 20 20 20 61 70 4f 6c 64 5b 30 5d 2d  ",.    apOld[0]-
17f74 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 6e 4f 6c 64  >pgno, .    nOld
17f75 3e 3d 32 20 3f 20 61 70 4f 6c 64 5b 31 5d 2d 3e  >=2 ? apOld[1]->
17f76 70 67 6e 6f 20 3a 20 30 2c 0a 20 20 20 20 6e 4f  pgno : 0,.    nO
17f77 6c 64 3e 3d 33 20 3f 20 61 70 4f 6c 64 5b 32 5d  ld>=3 ? apOld[2]
17f78 2d 3e 70 67 6e 6f 20 3a 20 30 0a 20 20 29 29 3b  ->pgno : 0.  ));
17f79 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f  ..  /*.  ** Allo
17f7a 63 61 74 65 20 6b 20 6e 65 77 20 70 61 67 65 73  cate k new pages
17f7b 2e 20 20 52 65 75 73 65 20 6f 6c 64 20 70 61 67  .  Reuse old pag
17f7c 65 73 20 77 68 65 72 65 20 70 6f 73 73 69 62 6c  es where possibl
17f7d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61 70  e..  */.  if( ap
17f7e 4f 6c 64 5b 30 5d 2d 3e 70 67 6e 6f 3c 3d 31 20  Old[0]->pgno<=1 
17f7f 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
17f80 54 45 5f 43 4f 52 52 55 50 54 3b 0a 20 20 20 20  TE_CORRUPT;.    
17f81 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65  goto balance_cle
17f82 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 70 61 67 65  anup;.  }.  page
17f83 46 6c 61 67 73 20 3d 20 61 70 4f 6c 64 5b 30 5d  Flags = apOld[0]
17f84 2d 3e 61 44 61 74 61 5b 30 5d 3b 0a 20 20 66 6f  ->aData[0];.  fo
17f85 72 28 69 3d 30 3b 20 69 3c 6b 3b 20 69 2b 2b 29  r(i=0; i<k; i++)
17f86 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70  {.    MemPage *p
17f87 4e 65 77 3b 0a 20 20 20 20 69 66 28 20 69 3c 6e  New;.    if( i<n
17f88 4f 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 4e 65  Old ){.      pNe
17f89 77 20 3d 20 61 70 4e 65 77 5b 69 5d 20 3d 20 61  w = apNew[i] = a
17f8a 70 4f 6c 64 5b 69 5d 3b 0a 20 20 20 20 20 20 61  pOld[i];.      a
17f8b 70 4f 6c 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 20  pOld[i] = 0;.   
17f8c 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
17f8d 61 67 65 72 57 72 69 74 65 28 70 4e 65 77 2d 3e  agerWrite(pNew->
17f8e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
17f8f 6e 4e 65 77 2b 2b 3b 0a 20 20 20 20 20 20 69 66  nNew++;.      if
17f90 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61  ( rc ) goto bala
17f91 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  nce_cleanup;.   
17f92 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73   }else{.      as
17f93 73 65 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 20  sert( i>0 );.   
17f94 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65     rc = allocate
17f95 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26  BtreePage(pBt, &
17f96 70 4e 65 77 2c 20 26 70 67 6e 6f 2c 20 70 67 6e  pNew, &pgno, pgn
17f97 6f 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  o, 0);.      if(
17f98 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e   rc ) goto balan
17f99 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  ce_cleanup;.    
17f9a 20 20 61 70 4e 65 77 5b 69 5d 20 3d 20 70 4e 65    apNew[i] = pNe
17f9b 77 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b 3b  w;.      nNew++;
17f9c 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74  ..      /* Set t
17f9d 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65  he pointer-map e
17f9e 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6e 65 77  ntry for the new
17f9f 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a   sibling page. *
17fa0 2f 0a 20 20 20 20 20 20 69 66 28 20 49 53 41 55  /.      if( ISAU
17fa1 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20  TOVACUUM ){.    
17fa2 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42      ptrmapPut(pB
17fa3 74 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 50  t, pNew->pgno, P
17fa4 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61  TRMAP_BTREE, pPa
17fa5 72 65 6e 74 2d 3e 70 67 6e 6f 2c 20 26 72 63 29  rent->pgno, &rc)
17fa6 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
17fa7 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
17fa8 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61           goto ba
17fa9 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  lance_cleanup;. 
17faa 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
17fab 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
17fac 20 46 72 65 65 20 61 6e 79 20 6f 6c 64 20 70 61   Free any old pa
17fad 67 65 73 20 74 68 61 74 20 77 65 72 65 20 6e 6f  ges that were no
17fae 74 20 72 65 75 73 65 64 20 61 73 20 6e 65 77 20  t reused as new 
17faf 70 61 67 65 73 2e 0a 20 20 2a 2f 0a 20 20 77 68  pages..  */.  wh
17fb0 69 6c 65 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20  ile( i<nOld ){. 
17fb1 20 20 20 66 72 65 65 50 61 67 65 28 61 70 4f 6c     freePage(apOl
17fb2 64 5b 69 5d 2c 20 26 72 63 29 3b 0a 20 20 20 20  d[i], &rc);.    
17fb3 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61  if( rc ) goto ba
17fb4 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  lance_cleanup;. 
17fb5 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 61     releasePage(a
17fb6 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 20 20 61 70  pOld[i]);.    ap
17fb7 4f 6c 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20  Old[i] = 0;.    
17fb8 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20  i++;.  }..  /*. 
17fb9 20 2a 2a 20 50 75 74 20 74 68 65 20 6e 65 77 20   ** Put the new 
17fba 70 61 67 65 73 20 69 6e 20 61 63 63 65 6e 64 69  pages in accendi
17fbb 6e 67 20 6f 72 64 65 72 2e 20 20 54 68 69 73 20  ng order.  This 
17fbc 68 65 6c 70 73 20 74 6f 0a 20 20 2a 2a 20 6b 65  helps to.  ** ke
17fbd 65 70 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  ep entries in th
17fbe 65 20 64 69 73 6b 20 66 69 6c 65 20 69 6e 20 6f  e disk file in o
17fbf 72 64 65 72 20 73 6f 20 74 68 61 74 20 61 20 73  rder so that a s
17fc0 63 61 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  can.  ** of the 
17fc1 74 61 62 6c 65 20 69 73 20 61 20 6c 69 6e 65 61  table is a linea
17fc2 72 20 73 63 61 6e 20 74 68 72 6f 75 67 68 20 74  r scan through t
17fc3 68 65 20 66 69 6c 65 2e 20 20 54 68 61 74 0a 20  he file.  That. 
17fc4 20 2a 2a 20 69 6e 20 74 75 72 6e 20 68 65 6c 70   ** in turn help
17fc5 73 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20  s the operating 
17fc6 73 79 73 74 65 6d 20 74 6f 20 64 65 6c 69 76 65  system to delive
17fc7 72 20 70 61 67 65 73 0a 20 20 2a 2a 20 66 72 6f  r pages.  ** fro
17fc8 6d 20 74 68 65 20 64 69 73 6b 20 6d 6f 72 65 20  m the disk more 
17fc9 72 61 70 69 64 6c 79 2e 0a 20 20 2a 2a 0a 20 20  rapidly..  **.  
17fca 2a 2a 20 41 6e 20 4f 28 6e 5e 32 29 20 69 6e 73  ** An O(n^2) ins
17fcb 65 72 74 69 6f 6e 20 73 6f 72 74 20 61 6c 67 6f  ertion sort algo
17fcc 72 69 74 68 6d 20 69 73 20 75 73 65 64 2c 20 62  rithm is used, b
17fcd 75 74 20 73 69 6e 63 65 0a 20 20 2a 2a 20 6e 20  ut since.  ** n 
17fce 69 73 20 6e 65 76 65 72 20 6d 6f 72 65 20 74 68  is never more th
17fcf 61 6e 20 4e 42 20 28 61 20 73 6d 61 6c 6c 20 63  an NB (a small c
17fd0 6f 6e 73 74 61 6e 74 29 2c 20 74 68 61 74 20 73  onstant), that s
17fd1 68 6f 75 6c 64 0a 20 20 2a 2a 20 6e 6f 74 20 62  hould.  ** not b
17fd2 65 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 2a  e a problem..  *
17fd3 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 4e 42 3d 3d  *.  ** When NB==
17fd4 33 2c 20 74 68 69 73 20 6f 6e 65 20 6f 70 74 69  3, this one opti
17fd5 6d 69 7a 61 74 69 6f 6e 20 6d 61 6b 65 73 20 74  mization makes t
17fd6 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a  he database.  **
17fd7 20 61 62 6f 75 74 20 32 35 25 20 66 61 73 74 65   about 25% faste
17fd8 72 20 66 6f 72 20 6c 61 72 67 65 20 69 6e 73 65  r for large inse
17fd9 72 74 69 6f 6e 73 20 61 6e 64 20 64 65 6c 65 74  rtions and delet
17fda 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  ions..  */.  for
17fdb 28 69 3d 30 3b 20 69 3c 6b 2d 31 3b 20 69 2b 2b  (i=0; i<k-1; i++
17fdc 29 7b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 56 20  ){.    int minV 
17fdd 3d 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e 6f  = apNew[i]->pgno
17fde 3b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 49 20 3d  ;.    int minI =
17fdf 20 69 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 69 2b   i;.    for(j=i+
17fe0 31 3b 20 6a 3c 6b 3b 20 6a 2b 2b 29 7b 0a 20 20  1; j<k; j++){.  
17fe1 20 20 20 20 69 66 28 20 61 70 4e 65 77 5b 6a 5d      if( apNew[j]
17fe2 2d 3e 70 67 6e 6f 3c 28 75 6e 73 69 67 6e 65 64  ->pgno<(unsigned
17fe3 29 6d 69 6e 56 20 29 7b 0a 20 20 20 20 20 20 20  )minV ){.       
17fe4 20 6d 69 6e 49 20 3d 20 6a 3b 0a 20 20 20 20 20   minI = j;.     
17fe5 20 20 20 6d 69 6e 56 20 3d 20 61 70 4e 65 77 5b     minV = apNew[
17fe6 6a 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 20 20  j]->pgno;.      
17fe7 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
17fe8 6d 69 6e 49 3e 69 20 29 7b 0a 20 20 20 20 20 20  minI>i ){.      
17fe9 69 6e 74 20 74 3b 0a 20 20 20 20 20 20 4d 65 6d  int t;.      Mem
17fea 50 61 67 65 20 2a 70 54 3b 0a 20 20 20 20 20 20  Page *pT;.      
17feb 74 20 3d 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 67  t = apNew[i]->pg
17fec 6e 6f 3b 0a 20 20 20 20 20 20 70 54 20 3d 20 61  no;.      pT = a
17fed 70 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20 61  pNew[i];.      a
17fee 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4e 65 77 5b  pNew[i] = apNew[
17fef 6d 69 6e 49 5d 3b 0a 20 20 20 20 20 20 61 70 4e  minI];.      apN
17ff0 65 77 5b 6d 69 6e 49 5d 20 3d 20 70 54 3b 0a 20  ew[minI] = pT;. 
17ff1 20 20 20 7d 0a 20 20 7d 0a 20 20 54 52 41 43 45     }.  }.  TRACE
17ff2 28 28 22 6e 65 77 3a 20 25 64 28 25 64 29 20 25  (("new: %d(%d) %
17ff3 64 28 25 64 29 20 25 64 28 25 64 29 20 25 64 28  d(%d) %d(%d) %d(
17ff4 25 64 29 20 25 64 28 25 64 29 5c 6e 22 2c 0a 20  %d) %d(%d)\n",. 
17ff5 20 20 20 61 70 4e 65 77 5b 30 5d 2d 3e 70 67 6e     apNew[0]->pgn
17ff6 6f 2c 20 73 7a 4e 65 77 5b 30 5d 2c 0a 20 20 20  o, szNew[0],.   
17ff7 20 6e 4e 65 77 3e 3d 32 20 3f 20 61 70 4e 65 77   nNew>=2 ? apNew
17ff8 5b 31 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e  [1]->pgno : 0, n
17ff9 4e 65 77 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b 31  New>=2 ? szNew[1
17ffa 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e  ] : 0,.    nNew>
17ffb 3d 33 20 3f 20 61 70 4e 65 77 5b 32 5d 2d 3e 70  =3 ? apNew[2]->p
17ffc 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 33  gno : 0, nNew>=3
17ffd 20 3f 20 73 7a 4e 65 77 5b 32 5d 20 3a 20 30 2c   ? szNew[2] : 0,
17ffe 0a 20 20 20 20 6e 4e 65 77 3e 3d 34 20 3f 20 61  .    nNew>=4 ? a
17fff 70 4e 65 77 5b 33 5d 2d 3e 70 67 6e 6f 20 3a 20  pNew[3]->pgno : 
18000 30 2c 20 6e 4e 65 77 3e 3d 34 20 3f 20 73 7a 4e  0, nNew>=4 ? szN
18001 65 77 5b 33 5d 20 3a 20 30 2c 0a 20 20 20 20 6e  ew[3] : 0,.    n
18002 4e 65 77 3e 3d 35 20 3f 20 61 70 4e 65 77 5b 34  New>=5 ? apNew[4
18003 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65  ]->pgno : 0, nNe
18004 77 3e 3d 35 20 3f 20 73 7a 4e 65 77 5b 34 5d 20  w>=5 ? szNew[4] 
18005 3a 20 30 29 29 3b 0a 0a 20 20 61 73 73 65 72 74  : 0));..  assert
18006 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
18007 77 72 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e  writeable(pParen
18008 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  t->pDbPage) );. 
18009 20 70 75 74 34 62 79 74 65 28 70 52 69 67 68 74   put4byte(pRight
1800a 2c 20 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d  , apNew[nNew-1]-
1800b 3e 70 67 6e 6f 29 3b 0a 0a 20 20 2f 2a 0a 20 20  >pgno);..  /*.  
1800c 2a 2a 20 45 76 65 6e 6c 79 20 64 69 73 74 72 69  ** Evenly distri
1800d 62 75 74 65 20 74 68 65 20 64 61 74 61 20 69 6e  bute the data in
1800e 20 61 70 43 65 6c 6c 5b 5d 20 61 63 72 6f 73 73   apCell[] across
1800f 20 74 68 65 20 6e 65 77 20 70 61 67 65 73 2e 0a   the new pages..
18010 20 20 2a 2a 20 49 6e 73 65 72 74 20 64 69 76 69    ** Insert divi
18011 64 65 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20 70  der cells into p
18012 50 61 72 65 6e 74 20 61 73 20 6e 65 63 65 73 73  Parent as necess
18013 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6a 20 3d 20  ary..  */.  j = 
18014 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  0;.  for(i=0; i<
18015 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nNew; i++){.    
18016 2f 2a 20 41 73 73 65 6d 62 6c 65 20 74 68 65 20  /* Assemble the 
18017 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 61 67 65  new sibling page
18018 2e 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65  . */.    MemPage
18019 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 69   *pNew = apNew[i
1801a 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6a  ];.    assert( j
1801b 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20  <nMaxCells );.  
1801c 20 20 7a 65 72 6f 50 61 67 65 28 70 4e 65 77 2c    zeroPage(pNew,
1801d 20 70 61 67 65 46 6c 61 67 73 29 3b 0a 20 20 20   pageFlags);.   
1801e 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 70 4e   assemblePage(pN
1801f 65 77 2c 20 63 6e 74 4e 65 77 5b 69 5d 2d 6a 2c  ew, cntNew[i]-j,
18020 20 26 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 73 7a   &apCell[j], &sz
18021 43 65 6c 6c 5b 6a 5d 29 3b 0a 20 20 20 20 61 73  Cell[j]);.    as
18022 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43 65 6c  sert( pNew->nCel
18023 6c 3e 30 20 7c 7c 20 28 6e 4e 65 77 3d 3d 31 20  l>0 || (nNew==1 
18024 26 26 20 63 6e 74 4e 65 77 5b 30 5d 3d 3d 30 29  && cntNew[0]==0)
18025 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
18026 70 4e 65 77 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d  pNew->nOverflow=
18027 3d 30 20 29 3b 0a 0a 20 20 20 20 6a 20 3d 20 63  =0 );..    j = c
18028 6e 74 4e 65 77 5b 69 5d 3b 0a 0a 20 20 20 20 2f  ntNew[i];..    /
18029 2a 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67  * If the sibling
1802a 20 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20   page assembled 
1802b 61 62 6f 76 65 20 77 61 73 20 6e 6f 74 20 74 68  above was not th
1802c 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 73 69 62  e right-most sib
1802d 6c 69 6e 67 2c 0a 20 20 20 20 2a 2a 20 69 6e 73  ling,.    ** ins
1802e 65 72 74 20 61 20 64 69 76 69 64 65 72 20 63 65  ert a divider ce
1802f 6c 6c 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65  ll into the pare
18030 6e 74 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a  nt page..    */.
18031 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e 4e      assert( i<nN
18032 65 77 2d 31 20 7c 7c 20 6a 3d 3d 6e 43 65 6c 6c  ew-1 || j==nCell
18033 20 29 3b 0a 20 20 20 20 69 66 28 20 6a 3c 6e 43   );.    if( j<nC
18034 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 75 38 20  ell ){.      u8 
18035 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 75 38  *pCell;.      u8
18036 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 69   *pTemp;.      i
18037 6e 74 20 73 7a 3b 0a 0a 20 20 20 20 20 20 61 73  nt sz;..      as
18038 73 65 72 74 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c  sert( j<nMaxCell
18039 73 20 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c  s );.      pCell
1803a 20 3d 20 61 70 43 65 6c 6c 5b 6a 5d 3b 0a 20 20   = apCell[j];.  
1803b 20 20 20 20 73 7a 20 3d 20 73 7a 43 65 6c 6c 5b      sz = szCell[
1803c 6a 5d 20 2b 20 6c 65 61 66 43 6f 72 72 65 63 74  j] + leafCorrect
1803d 69 6f 6e 3b 0a 20 20 20 20 20 20 70 54 65 6d 70  ion;.      pTemp
1803e 20 3d 20 26 61 4f 76 66 6c 53 70 61 63 65 5b 69   = &aOvflSpace[i
1803f 4f 76 66 6c 53 70 61 63 65 5d 3b 0a 20 20 20 20  OvflSpace];.    
18040 20 20 69 66 28 20 21 70 4e 65 77 2d 3e 6c 65 61    if( !pNew->lea
18041 66 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d  f ){.        mem
18042 63 70 79 28 26 70 4e 65 77 2d 3e 61 44 61 74 61  cpy(&pNew->aData
18043 5b 38 5d 2c 20 70 43 65 6c 6c 2c 20 34 29 3b 0a  [8], pCell, 4);.
18044 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
18045 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 20 20  leafData ){.    
18046 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72      /* If the tr
18047 65 65 20 69 73 20 61 20 6c 65 61 66 2d 64 61 74  ee is a leaf-dat
18048 61 20 74 72 65 65 2c 20 61 6e 64 20 74 68 65 20  a tree, and the 
18049 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6c 65 61  siblings are lea
1804a 76 65 73 2c 20 0a 20 20 20 20 20 20 20 20 2a 2a  ves, .        **
1804b 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e   then there is n
1804c 6f 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69  o divider cell i
1804d 6e 20 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73 74  n apCell[]. Inst
1804e 65 61 64 2c 20 74 68 65 20 64 69 76 69 64 65 72  ead, the divider
1804f 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 65 6c   .        ** cel
18050 6c 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68  l consists of th
18051 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 66 6f  e integer key fo
18052 72 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  r the right-most
18053 20 63 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20 20   cell of .      
18054 20 20 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e 67    ** the sibling
18055 2d 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20  -page assembled 
18056 61 62 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20 20  above only..    
18057 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 43      */.        C
18058 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20  ellInfo info;.  
18059 20 20 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20 20        j--;.     
1805a 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c     btreeParseCel
1805b 6c 50 74 72 28 70 4e 65 77 2c 20 61 70 43 65 6c  lPtr(pNew, apCel
1805c 6c 5b 6a 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 20  l[j], &info);.  
1805d 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 70 54        pCell = pT
1805e 65 6d 70 3b 0a 20 20 20 20 20 20 20 20 73 7a 20  emp;.        sz 
1805f 3d 20 34 20 2b 20 70 75 74 56 61 72 69 6e 74 28  = 4 + putVarint(
18060 26 70 43 65 6c 6c 5b 34 5d 2c 20 69 6e 66 6f 2e  &pCell[4], info.
18061 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 70  nKey);.        p
18062 54 65 6d 70 20 3d 20 30 3b 0a 20 20 20 20 20 20  Temp = 0;.      
18063 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
18064 43 65 6c 6c 20 2d 3d 20 34 3b 0a 20 20 20 20 20  Cell -= 4;.     
18065 20 20 20 2f 2a 20 4f 62 73 63 75 72 65 20 63 61     /* Obscure ca
18066 73 65 20 66 6f 72 20 6e 6f 6e 2d 6c 65 61 66 2d  se for non-leaf-
18067 64 61 74 61 20 74 72 65 65 73 3a 20 49 66 20 74  data trees: If t
18068 68 65 20 63 65 6c 6c 20 61 74 20 70 43 65 6c 6c  he cell at pCell
18069 20 77 61 73 0a 20 20 20 20 20 20 20 20 2a 2a 20   was.        ** 
1806a 70 72 65 76 69 6f 75 73 6c 79 20 73 74 6f 72 65  previously store
1806b 64 20 6f 6e 20 61 20 6c 65 61 66 20 6e 6f 64 65  d on a leaf node
1806c 2c 20 61 6e 64 20 69 74 73 20 72 65 70 6f 72 74  , and its report
1806d 65 64 20 73 69 7a 65 20 77 61 73 20 34 0a 20 20  ed size was 4.  
1806e 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73 2c 20        ** bytes, 
1806f 74 68 65 6e 20 69 74 20 6d 61 79 20 61 63 74 75  then it may actu
18070 61 6c 6c 79 20 62 65 20 73 6d 61 6c 6c 65 72 20  ally be smaller 
18071 74 68 61 6e 20 74 68 69 73 20 0a 20 20 20 20 20  than this .     
18072 20 20 20 2a 2a 20 28 73 65 65 20 62 74 72 65 65     ** (see btree
18073 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 2c 20  ParseCellPtr(), 
18074 34 20 62 79 74 65 73 20 69 73 20 74 68 65 20 6d  4 bytes is the m
18075 69 6e 69 6d 75 6d 20 73 69 7a 65 20 6f 66 0a 20  inimum size of. 
18076 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 20 63 65         ** any ce
18077 6c 6c 29 2e 20 42 75 74 20 69 74 20 69 73 20 69  ll). But it is i
18078 6d 70 6f 72 74 61 6e 74 20 74 6f 20 70 61 73 73  mportant to pass
18079 20 74 68 65 20 63 6f 72 72 65 63 74 20 73 69 7a   the correct siz
1807a 65 20 74 6f 20 0a 20 20 20 20 20 20 20 20 2a 2a  e to .        **
1807b 20 69 6e 73 65 72 74 43 65 6c 6c 28 29 2c 20 73   insertCell(), s
1807c 6f 20 72 65 70 61 72 73 65 20 74 68 65 20 63 65  o reparse the ce
1807d 6c 6c 20 6e 6f 77 2e 0a 20 20 20 20 20 20 20 20  ll now..        
1807e 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f  **.        ** No
1807f 74 65 20 74 68 61 74 20 74 68 69 73 20 63 61 6e  te that this can
18080 20 6e 65 76 65 72 20 68 61 70 70 65 6e 20 69 6e   never happen in
18081 20 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61 20   an SQLite data 
18082 66 69 6c 65 2c 20 61 73 20 61 6c 6c 0a 20 20 20  file, as all.   
18083 20 20 20 20 20 2a 2a 20 63 65 6c 6c 73 20 61 72       ** cells ar
18084 65 20 61 74 20 6c 65 61 73 74 20 34 20 62 79 74  e at least 4 byt
18085 65 73 2e 20 49 74 20 6f 6e 6c 79 20 68 61 70 70  es. It only happ
18086 65 6e 73 20 69 6e 20 62 2d 74 72 65 65 73 20 75  ens in b-trees u
18087 73 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  sed.        ** t
18088 6f 20 65 76 61 6c 75 61 74 65 20 22 49 4e 20 28  o evaluate "IN (
18089 53 45 4c 45 43 54 20 2e 2e 2e 29 22 20 61 6e 64  SELECT ...)" and
1808a 20 73 69 6d 69 6c 61 72 20 63 6c 61 75 73 65 73   similar clauses
1808b 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
1808c 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b       if( szCell[
1808d 6a 5d 3d 3d 34 20 29 7b 0a 20 20 20 20 20 20 20  j]==4 ){.       
1808e 20 20 20 61 73 73 65 72 74 28 6c 65 61 66 43 6f     assert(leafCo
1808f 72 72 65 63 74 69 6f 6e 3d 3d 34 29 3b 0a 20 20  rrection==4);.  
18090 20 20 20 20 20 20 20 20 73 7a 20 3d 20 63 65 6c          sz = cel
18091 6c 53 69 7a 65 50 74 72 28 70 50 61 72 65 6e 74  lSizePtr(pParent
18092 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20  , pCell);.      
18093 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
18094 20 20 69 4f 76 66 6c 53 70 61 63 65 20 2b 3d 20    iOvflSpace += 
18095 73 7a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  sz;.      assert
18096 28 20 73 7a 3c 3d 70 42 74 2d 3e 70 61 67 65 53  ( sz<=pBt->pageS
18097 69 7a 65 2f 34 20 29 3b 0a 20 20 20 20 20 20 61  ize/4 );.      a
18098 73 73 65 72 74 28 20 69 4f 76 66 6c 53 70 61 63  ssert( iOvflSpac
18099 65 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  e<=pBt->pageSize
1809a 20 29 3b 0a 20 20 20 20 20 20 69 6e 73 65 72 74   );.      insert
1809b 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78  Cell(pParent, nx
1809c 44 69 76 2c 20 70 43 65 6c 6c 2c 20 73 7a 2c 20  Div, pCell, sz, 
1809d 70 54 65 6d 70 2c 20 70 4e 65 77 2d 3e 70 67 6e  pTemp, pNew->pgn
1809e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 69  o, &rc);.      i
1809f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
180a0 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f   ) goto balance_
180a1 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 61  cleanup;.      a
180a2 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
180a3 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
180a4 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29  Parent->pDbPage)
180a5 20 29 3b 0a 0a 20 20 20 20 20 20 6a 2b 2b 3b 0a   );..      j++;.
180a6 20 20 20 20 20 20 6e 78 44 69 76 2b 2b 3b 0a 20        nxDiv++;. 
180a7 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72     }.  }.  asser
180a8 74 28 20 6a 3d 3d 6e 43 65 6c 6c 20 29 3b 0a 20  t( j==nCell );. 
180a9 20 61 73 73 65 72 74 28 20 6e 4f 6c 64 3e 30 20   assert( nOld>0 
180aa 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 4e 65  );.  assert( nNe
180ab 77 3e 30 20 29 3b 0a 20 20 69 66 28 20 28 70 61  w>0 );.  if( (pa
180ac 67 65 46 6c 61 67 73 20 26 20 50 54 46 5f 4c 45  geFlags & PTF_LE
180ad 41 46 29 3d 3d 30 20 29 7b 0a 20 20 20 20 75 38  AF)==0 ){.    u8
180ae 20 2a 7a 43 68 69 6c 64 20 3d 20 26 61 70 43 6f   *zChild = &apCo
180af 70 79 5b 6e 4f 6c 64 2d 31 5d 2d 3e 61 44 61 74  py[nOld-1]->aDat
180b0 61 5b 38 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79  a[8];.    memcpy
180b1 28 26 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d  (&apNew[nNew-1]-
180b2 3e 61 44 61 74 61 5b 38 5d 2c 20 7a 43 68 69 6c  >aData[8], zChil
180b3 64 2c 20 34 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  d, 4);.  }..  if
180b4 28 20 69 73 52 6f 6f 74 20 26 26 20 70 50 61 72  ( isRoot && pPar
180b5 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26  ent->nCell==0 &&
180b6 20 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66   pParent->hdrOff
180b7 73 65 74 3c 3d 61 70 4e 65 77 5b 30 5d 2d 3e 6e  set<=apNew[0]->n
180b8 46 72 65 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54  Free ){.    /* T
180b9 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20  he root page of 
180ba 74 68 65 20 62 2d 74 72 65 65 20 6e 6f 77 20 63  the b-tree now c
180bb 6f 6e 74 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73  ontains no cells
180bc 2e 20 54 68 65 20 6f 6e 6c 79 20 73 69 62 6c 69  . The only sibli
180bd 6e 67 0a 20 20 20 20 2a 2a 20 70 61 67 65 20 69  ng.    ** page i
180be 73 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c  s the right-chil
180bf 64 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 2e  d of the parent.
180c0 20 43 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e   Copy the conten
180c1 74 73 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a  ts of the.    **
180c2 20 63 68 69 6c 64 20 70 61 67 65 20 69 6e 74 6f   child page into
180c3 20 74 68 65 20 70 61 72 65 6e 74 2c 20 64 65 63   the parent, dec
180c4 72 65 61 73 69 6e 67 20 74 68 65 20 6f 76 65 72  reasing the over
180c5 61 6c 6c 20 68 65 69 67 68 74 20 6f 66 20 74 68  all height of th
180c6 65 0a 20 20 20 20 2a 2a 20 62 2d 74 72 65 65 20  e.    ** b-tree 
180c7 73 74 72 75 63 74 75 72 65 20 62 79 20 6f 6e 65  structure by one
180c8 2e 20 54 68 69 73 20 69 73 20 64 65 73 63 72 69  . This is descri
180c9 62 65 64 20 61 73 20 74 68 65 20 22 62 61 6c 61  bed as the "bala
180ca 6e 63 65 2d 73 68 61 6c 6c 6f 77 65 72 22 0a 20  nce-shallower". 
180cb 20 20 20 2a 2a 20 73 75 62 2d 61 6c 67 6f 72 69     ** sub-algori
180cc 74 68 6d 20 69 6e 20 73 6f 6d 65 20 64 6f 63 75  thm in some docu
180cd 6d 65 6e 74 61 74 69 6f 6e 2e 0a 20 20 20 20 2a  mentation..    *
180ce 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73  *.    ** If this
180cf 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75   is an auto-vacu
180d0 75 6d 20 64 61 74 61 62 61 73 65 2c 20 74 68 65  um database, the
180d1 20 63 61 6c 6c 20 74 6f 20 63 6f 70 79 4e 6f 64   call to copyNod
180d2 65 43 6f 6e 74 65 6e 74 28 29 20 0a 20 20 20 20  eContent() .    
180d3 2a 2a 20 73 65 74 73 20 61 6c 6c 20 70 6f 69 6e  ** sets all poin
180d4 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20  ter-map entries 
180d5 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f  corresponding to
180d6 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 20   database image 
180d7 70 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 66 6f  pages .    ** fo
180d8 72 20 77 68 69 63 68 20 74 68 65 20 70 6f 69 6e  r which the poin
180d9 74 65 72 20 69 73 20 73 74 6f 72 65 64 20 77 69  ter is stored wi
180da 74 68 69 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74  thin the content
180db 20 62 65 69 6e 67 20 63 6f 70 69 65 64 2e 0a 20   being copied.. 
180dc 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
180dd 20 73 65 63 6f 6e 64 20 61 73 73 65 72 74 20 62   second assert b
180de 65 6c 6f 77 20 76 65 72 69 66 69 65 73 20 74 68  elow verifies th
180df 61 74 20 74 68 65 20 63 68 69 6c 64 20 70 61 67  at the child pag
180e0 65 20 69 73 20 64 65 66 72 61 67 6d 65 6e 74 65  e is defragmente
180e1 64 0a 20 20 20 20 2a 2a 20 28 69 74 20 6d 75 73  d.    ** (it mus
180e2 74 20 62 65 2c 20 61 73 20 69 74 20 77 61 73 20  t be, as it was 
180e3 6a 75 73 74 20 72 65 63 6f 6e 73 74 72 75 63 74  just reconstruct
180e4 65 64 20 75 73 69 6e 67 20 61 73 73 65 6d 62 6c  ed using assembl
180e5 65 50 61 67 65 28 29 29 2e 20 54 68 69 73 0a 20  ePage()). This. 
180e6 20 20 20 2a 2a 20 69 73 20 69 6d 70 6f 72 74 61     ** is importa
180e7 6e 74 20 69 66 20 74 68 65 20 70 61 72 65 6e 74  nt if the parent
180e8 20 70 61 67 65 20 68 61 70 70 65 6e 73 20 74 6f   page happens to
180e9 20 62 65 20 70 61 67 65 20 31 20 6f 66 20 74 68   be page 1 of th
180ea 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a  e database.    *
180eb 2a 20 69 6d 61 67 65 2e 20 20 2a 2f 0a 20 20 20  * image.  */.   
180ec 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3d 3d 31   assert( nNew==1
180ed 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
180ee 61 70 4e 65 77 5b 30 5d 2d 3e 6e 46 72 65 65 20  apNew[0]->nFree 
180ef 3d 3d 20 0a 20 20 20 20 20 20 20 20 28 67 65 74  == .        (get
180f0 32 62 79 74 65 28 26 61 70 4e 65 77 5b 30 5d 2d  2byte(&apNew[0]-
180f1 3e 61 44 61 74 61 5b 35 5d 29 2d 61 70 4e 65 77  >aData[5])-apNew
180f2 5b 30 5d 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 2d  [0]->cellOffset-
180f3 61 70 4e 65 77 5b 30 5d 2d 3e 6e 43 65 6c 6c 2a  apNew[0]->nCell*
180f4 32 29 20 0a 20 20 20 20 29 3b 0a 20 20 20 20 63  2) .    );.    c
180f5 6f 70 79 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 61  opyNodeContent(a
180f6 70 4e 65 77 5b 30 5d 2c 20 70 50 61 72 65 6e 74  pNew[0], pParent
180f7 2c 20 26 72 63 29 3b 0a 20 20 20 20 66 72 65 65  , &rc);.    free
180f8 50 61 67 65 28 61 70 4e 65 77 5b 30 5d 2c 20 26  Page(apNew[0], &
180f9 72 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  rc);.  }else if(
180fa 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b   ISAUTOVACUUM ){
180fb 0a 20 20 20 20 2f 2a 20 46 69 78 20 74 68 65 20  .    /* Fix the 
180fc 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72  pointer-map entr
180fd 69 65 73 20 66 6f 72 20 61 6c 6c 20 74 68 65 20  ies for all the 
180fe 63 65 6c 6c 73 20 74 68 61 74 20 77 65 72 65 20  cells that were 
180ff 73 68 69 66 74 65 64 20 61 72 6f 75 6e 64 2e 20  shifted around. 
18100 0a 20 20 20 20 2a 2a 20 54 68 65 72 65 20 61 72  .    ** There ar
18101 65 20 73 65 76 65 72 61 6c 20 64 69 66 66 65 72  e several differ
18102 65 6e 74 20 74 79 70 65 73 20 6f 66 20 70 6f 69  ent types of poi
18103 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73  nter-map entries
18104 20 74 68 61 74 20 6e 65 65 64 20 74 6f 0a 20 20   that need to.  
18105 20 20 2a 2a 20 62 65 20 64 65 61 6c 74 20 77 69    ** be dealt wi
18106 74 68 20 62 79 20 74 68 69 73 20 72 6f 75 74 69  th by this routi
18107 6e 65 2e 20 53 6f 6d 65 20 6f 66 20 74 68 65 73  ne. Some of thes
18108 65 20 68 61 76 65 20 62 65 65 6e 20 73 65 74 20  e have been set 
18109 61 6c 72 65 61 64 79 2c 20 62 75 74 0a 20 20 20  already, but.   
1810a 20 2a 2a 20 6d 61 6e 79 20 68 61 76 65 20 6e 6f   ** many have no
1810b 74 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  t. The following
1810c 20 69 73 20 61 20 73 75 6d 6d 61 72 79 3a 0a 20   is a summary:. 
1810d 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 31     **.    **   1
1810e 29 20 54 68 65 20 65 6e 74 72 69 65 73 20 61 73  ) The entries as
1810f 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 6e 65  sociated with ne
18110 77 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20  w sibling pages 
18111 74 68 61 74 20 77 65 72 65 20 6e 6f 74 0a 20 20  that were not.  
18112 20 20 2a 2a 20 20 20 20 20 20 73 69 62 6c 69 6e    **      siblin
18113 67 73 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e  gs when this fun
18114 63 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64  ction was called
18115 2e 20 54 68 65 73 65 20 68 61 76 65 20 61 6c 72  . These have alr
18116 65 61 64 79 0a 20 20 20 20 2a 2a 20 20 20 20 20  eady.    **     
18117 20 62 65 65 6e 20 73 65 74 2e 20 57 65 20 64 6f   been set. We do
18118 6e 27 74 20 6e 65 65 64 20 74 6f 20 77 6f 72 72  n't need to worr
18119 79 20 61 62 6f 75 74 20 6f 6c 64 20 73 69 62 6c  y about old sibl
1811a 69 6e 67 73 20 74 68 61 74 20 77 65 72 65 0a 20  ings that were. 
1811b 20 20 20 2a 2a 20 20 20 20 20 20 6d 6f 76 65 64     **      moved
1811c 20 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 73   to the free-lis
1811d 74 20 2d 20 74 68 65 20 66 72 65 65 50 61 67 65  t - the freePage
1811e 28 29 20 63 6f 64 65 20 68 61 73 20 74 61 6b 65  () code has take
1811f 6e 20 63 61 72 65 0a 20 20 20 20 2a 2a 20 20 20  n care.    **   
18120 20 20 20 6f 66 20 74 68 6f 73 65 2e 0a 20 20 20     of those..   
18121 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 32 29 20   **.    **   2) 
18122 54 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  The pointer-map 
18123 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74  entries associat
18124 65 64 20 77 69 74 68 20 74 68 65 20 66 69 72 73  ed with the firs
18125 74 20 6f 76 65 72 66 6c 6f 77 0a 20 20 20 20 2a  t overflow.    *
18126 2a 20 20 20 20 20 20 70 61 67 65 20 69 6e 20 61  *      page in a
18127 6e 79 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69  ny overflow chai
18128 6e 73 20 75 73 65 64 20 62 79 20 6e 65 77 20 64  ns used by new d
18129 69 76 69 64 65 72 20 63 65 6c 6c 73 2e 20 54 68  ivider cells. Th
1812a 65 73 65 20 0a 20 20 20 20 2a 2a 20 20 20 20 20  ese .    **     
1812b 20 68 61 76 65 20 61 6c 73 6f 20 61 6c 72 65 61   have also alrea
1812c 64 79 20 62 65 65 6e 20 74 61 6b 65 6e 20 63 61  dy been taken ca
1812d 72 65 20 6f 66 20 62 79 20 74 68 65 20 69 6e 73  re of by the ins
1812e 65 72 74 43 65 6c 6c 28 29 20 63 6f 64 65 2e 0a  ertCell() code..
1812f 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
18130 33 29 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e  3) If the siblin
18131 67 20 70 61 67 65 73 20 61 72 65 20 6e 6f 74 20  g pages are not 
18132 6c 65 61 76 65 73 2c 20 74 68 65 6e 20 74 68 65  leaves, then the
18133 20 63 68 69 6c 64 20 70 61 67 65 73 20 6f 66 0a   child pages of.
18134 20 20 20 20 2a 2a 20 20 20 20 20 20 63 65 6c 6c      **      cell
18135 73 20 73 74 6f 72 65 64 20 6f 6e 20 74 68 65 20  s stored on the 
18136 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 6d 61  sibling pages ma
18137 79 20 6e 65 65 64 20 74 6f 20 62 65 20 75 70 64  y need to be upd
18138 61 74 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  ated..    **.   
18139 20 2a 2a 20 20 20 34 29 20 49 66 20 74 68 65 20   **   4) If the 
1813a 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61 72  sibling pages ar
1813b 65 20 6e 6f 74 20 69 6e 74 65 72 6e 61 6c 20 69  e not internal i
1813c 6e 74 6b 65 79 20 6e 6f 64 65 73 2c 20 74 68 65  ntkey nodes, the
1813d 6e 20 61 6e 79 0a 20 20 20 20 2a 2a 20 20 20 20  n any.    **    
1813e 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73    overflow pages
1813f 20 75 73 65 64 20 62 79 20 74 68 65 73 65 20 63   used by these c
18140 65 6c 6c 73 20 6d 61 79 20 6e 65 65 64 20 74 6f  ells may need to
18141 20 62 65 20 75 70 64 61 74 65 64 0a 20 20 20 20   be updated.    
18142 2a 2a 20 20 20 20 20 20 28 69 6e 74 65 72 6e 61  **      (interna
18143 6c 20 69 6e 74 6b 65 79 20 6e 6f 64 65 73 20 6e  l intkey nodes n
18144 65 76 65 72 20 63 6f 6e 74 61 69 6e 20 70 6f 69  ever contain poi
18145 6e 74 65 72 73 20 74 6f 20 6f 76 65 72 66 6c 6f  nters to overflo
18146 77 20 70 61 67 65 73 29 2e 0a 20 20 20 20 2a 2a  w pages)..    **
18147 0a 20 20 20 20 2a 2a 20 20 20 35 29 20 49 66 20  .    **   5) If 
18148 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65  the sibling page
18149 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 76 65 73  s are not leaves
1814a 2c 20 74 68 65 6e 20 74 68 65 20 70 6f 69 6e 74  , then the point
1814b 65 72 2d 6d 61 70 0a 20 20 20 20 2a 2a 20 20 20  er-map.    **   
1814c 20 20 20 65 6e 74 72 69 65 73 20 66 6f 72 20 74     entries for t
1814d 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 70  he right-child p
1814e 61 67 65 73 20 6f 66 20 65 61 63 68 20 73 69 62  ages of each sib
1814f 6c 69 6e 67 20 6d 61 79 20 6e 65 65 64 0a 20 20  ling may need.  
18150 20 20 2a 2a 20 20 20 20 20 20 74 6f 20 62 65 20    **      to be 
18151 75 70 64 61 74 65 64 2e 0a 20 20 20 20 2a 2a 0a  updated..    **.
18152 20 20 20 20 2a 2a 20 43 61 73 65 73 20 31 20 61      ** Cases 1 a
18153 6e 64 20 32 20 61 72 65 20 64 65 61 6c 74 20 77  nd 2 are dealt w
18154 69 74 68 20 61 62 6f 76 65 20 62 79 20 6f 74 68  ith above by oth
18155 65 72 20 63 6f 64 65 2e 20 54 68 65 20 6e 65 78  er code. The nex
18156 74 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 20 64  t.    ** block d
18157 65 61 6c 73 20 77 69 74 68 20 63 61 73 65 73 20  eals with cases 
18158 33 20 61 6e 64 20 34 20 61 6e 64 20 74 68 65 20  3 and 4 and the 
18159 6f 6e 65 20 61 66 74 65 72 20 74 68 61 74 2c 20  one after that, 
1815a 63 61 73 65 20 35 2e 20 53 69 6e 63 65 0a 20 20  case 5. Since.  
1815b 20 20 2a 2a 20 73 65 74 74 69 6e 67 20 61 20 70    ** setting a p
1815c 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79  ointer map entry
1815d 20 69 73 20 61 20 72 65 6c 61 74 69 76 65 6c 79   is a relatively
1815e 20 65 78 70 65 6e 73 69 76 65 20 6f 70 65 72 61   expensive opera
1815f 74 69 6f 6e 2c 20 74 68 69 73 0a 20 20 20 20 2a  tion, this.    *
18160 2a 20 63 6f 64 65 20 6f 6e 6c 79 20 73 65 74 73  * code only sets
18161 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74   pointer map ent
18162 72 69 65 73 20 66 6f 72 20 63 68 69 6c 64 20 6f  ries for child o
18163 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  r overflow pages
18164 20 74 68 61 74 20 68 61 76 65 0a 20 20 20 20 2a   that have.    *
18165 2a 20 61 63 74 75 61 6c 6c 79 20 6d 6f 76 65 64  * actually moved
18166 20 62 65 74 77 65 65 6e 20 70 61 67 65 73 2e 20   between pages. 
18167 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20   */.    MemPage 
18168 2a 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 30 5d  *pNew = apNew[0]
18169 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70  ;.    MemPage *p
1816a 4f 6c 64 20 3d 20 61 70 43 6f 70 79 5b 30 5d 3b  Old = apCopy[0];
1816b 0a 20 20 20 20 69 6e 74 20 6e 4f 76 65 72 66 6c  .    int nOverfl
1816c 6f 77 20 3d 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72  ow = pOld->nOver
1816d 66 6c 6f 77 3b 0a 20 20 20 20 69 6e 74 20 69 4e  flow;.    int iN
1816e 65 78 74 4f 6c 64 20 3d 20 70 4f 6c 64 2d 3e 6e  extOld = pOld->n
1816f 43 65 6c 6c 20 2b 20 6e 4f 76 65 72 66 6c 6f 77  Cell + nOverflow
18170 3b 0a 20 20 20 20 69 6e 74 20 69 4f 76 65 72 66  ;.    int iOverf
18171 6c 6f 77 20 3d 20 28 6e 4f 76 65 72 66 6c 6f 77  low = (nOverflow
18172 20 3f 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30   ? pOld->aOvfl[0
18173 5d 2e 69 64 78 20 3a 20 2d 31 29 3b 0a 20 20 20  ].idx : -1);.   
18174 20 6a 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20   j = 0;         
18175 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18176 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 27      /* Current '
18177 6f 6c 64 27 20 73 69 62 6c 69 6e 67 20 70 61 67  old' sibling pag
18178 65 20 2a 2f 0a 20 20 20 20 6b 20 3d 20 30 3b 20  e */.    k = 0; 
18179 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1817a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
1817b 75 72 72 65 6e 74 20 27 6e 65 77 27 20 73 69 62  urrent 'new' sib
1817c 6c 69 6e 67 20 70 61 67 65 20 2a 2f 0a 20 20 20  ling page */.   
1817d 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c   for(i=0; i<nCel
1817e 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  l; i++){.      i
1817f 6e 74 20 69 73 44 69 76 69 64 65 72 20 3d 20 30  nt isDivider = 0
18180 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 69  ;.      while( i
18181 3d 3d 69 4e 65 78 74 4f 6c 64 20 29 7b 0a 20 20  ==iNextOld ){.  
18182 20 20 20 20 20 20 2f 2a 20 43 65 6c 6c 20 69 20        /* Cell i 
18183 69 73 20 74 68 65 20 63 65 6c 6c 20 69 6d 6d 65  is the cell imme
18184 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e  diately followin
18185 67 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20  g the last cell 
18186 6f 6e 20 6f 6c 64 0a 20 20 20 20 20 20 20 20 2a  on old.        *
18187 2a 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20 6a  * sibling page j
18188 2e 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67  . If the sibling
18189 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 66 20 70  s are not leaf p
1818a 61 67 65 73 20 6f 66 20 61 6e 0a 20 20 20 20 20  ages of an.     
1818b 20 20 20 2a 2a 20 69 6e 74 6b 65 79 20 62 2d 74     ** intkey b-t
1818c 72 65 65 2c 20 74 68 65 6e 20 63 65 6c 6c 20 69  ree, then cell i
1818d 20 77 61 73 20 61 20 64 69 76 69 64 65 72 20 63   was a divider c
1818e 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ell. */.        
1818f 70 4f 6c 64 20 3d 20 61 70 43 6f 70 79 5b 2b 2b  pOld = apCopy[++
18190 6a 5d 3b 0a 20 20 20 20 20 20 20 20 69 4e 65 78  j];.        iNex
18191 74 4f 6c 64 20 3d 20 69 20 2b 20 21 6c 65 61 66  tOld = i + !leaf
18192 44 61 74 61 20 2b 20 70 4f 6c 64 2d 3e 6e 43 65  Data + pOld->nCe
18193 6c 6c 20 2b 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72  ll + pOld->nOver
18194 66 6c 6f 77 3b 0a 20 20 20 20 20 20 20 20 69 66  flow;.        if
18195 28 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f  ( pOld->nOverflo
18196 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  w ){.          n
18197 4f 76 65 72 66 6c 6f 77 20 3d 20 70 4f 6c 64 2d  Overflow = pOld-
18198 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20  >nOverflow;.    
18199 20 20 20 20 20 20 69 4f 76 65 72 66 6c 6f 77 20        iOverflow 
1819a 3d 20 69 20 2b 20 21 6c 65 61 66 44 61 74 61 20  = i + !leafData 
1819b 2b 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d  + pOld->aOvfl[0]
1819c 2e 69 64 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a  .idx;.        }.
1819d 20 20 20 20 20 20 20 20 69 73 44 69 76 69 64 65          isDivide
1819e 72 20 3d 20 21 6c 65 61 66 44 61 74 61 3b 20 20  r = !leafData;  
1819f 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
181a0 61 73 73 65 72 74 28 6e 4f 76 65 72 66 6c 6f 77  assert(nOverflow
181a1 3e 30 20 7c 7c 20 69 4f 76 65 72 66 6c 6f 77 3c  >0 || iOverflow<
181a2 69 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  i );.      asser
181a3 74 28 6e 4f 76 65 72 66 6c 6f 77 3c 32 20 7c 7c  t(nOverflow<2 ||
181a4 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e   pOld->aOvfl[0].
181a5 69 64 78 3d 3d 70 4f 6c 64 2d 3e 61 4f 76 66 6c  idx==pOld->aOvfl
181a6 5b 31 5d 2e 69 64 78 2d 31 29 3b 0a 20 20 20 20  [1].idx-1);.    
181a7 20 20 61 73 73 65 72 74 28 6e 4f 76 65 72 66 6c    assert(nOverfl
181a8 6f 77 3c 33 20 7c 7c 20 70 4f 6c 64 2d 3e 61 4f  ow<3 || pOld->aO
181a9 76 66 6c 5b 31 5d 2e 69 64 78 3d 3d 70 4f 6c 64  vfl[1].idx==pOld
181aa 2d 3e 61 4f 76 66 6c 5b 32 5d 2e 69 64 78 2d 31  ->aOvfl[2].idx-1
181ab 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d  );.      if( i==
181ac 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20  iOverflow ){.   
181ad 20 20 20 20 20 69 73 44 69 76 69 64 65 72 20 3d       isDivider =
181ae 20 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   1;.        if( 
181af 28 2d 2d 6e 4f 76 65 72 66 6c 6f 77 29 3e 30 20  (--nOverflow)>0 
181b0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 4f 76  ){.          iOv
181b1 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20 20 20 20 20  erflow++;.      
181b2 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20    }.      }..   
181b3 20 20 20 69 66 28 20 69 3d 3d 63 6e 74 4e 65 77     if( i==cntNew
181b4 5b 6b 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  [k] ){.        /
181b5 2a 20 43 65 6c 6c 20 69 20 69 73 20 74 68 65 20  * Cell i is the 
181b6 63 65 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79  cell immediately
181b7 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6c   following the l
181b8 61 73 74 20 63 65 6c 6c 20 6f 6e 20 6e 65 77 0a  ast cell on new.
181b9 20 20 20 20 20 20 20 20 2a 2a 20 73 69 62 6c 69          ** sibli
181ba 6e 67 20 70 61 67 65 20 6b 2e 20 49 66 20 74 68  ng page k. If th
181bb 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6e  e siblings are n
181bc 6f 74 20 6c 65 61 66 20 70 61 67 65 73 20 6f 66  ot leaf pages of
181bd 20 61 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 69   an.        ** i
181be 6e 74 6b 65 79 20 62 2d 74 72 65 65 2c 20 74 68  ntkey b-tree, th
181bf 65 6e 20 63 65 6c 6c 20 69 20 69 73 20 61 20 64  en cell i is a d
181c0 69 76 69 64 65 72 20 63 65 6c 6c 2e 20 20 2a 2f  ivider cell.  */
181c1 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20  .        pNew = 
181c2 61 70 4e 65 77 5b 2b 2b 6b 5d 3b 0a 20 20 20 20  apNew[++k];.    
181c3 20 20 20 20 69 66 28 20 21 6c 65 61 66 44 61 74      if( !leafDat
181c4 61 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  a ) continue;.  
181c5 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65      }.      asse
181c6 72 74 28 20 6a 3c 6e 4f 6c 64 20 29 3b 0a 20 20  rt( j<nOld );.  
181c7 20 20 20 20 61 73 73 65 72 74 28 20 6b 3c 6e 4e      assert( k<nN
181c8 65 77 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  ew );..      /* 
181c9 49 66 20 74 68 65 20 63 65 6c 6c 20 77 61 73 20  If the cell was 
181ca 6f 72 69 67 69 6e 61 6c 6c 79 20 64 69 76 69 64  originally divid
181cb 65 72 20 63 65 6c 6c 20 28 61 6e 64 20 69 73 20  er cell (and is 
181cc 6e 6f 74 20 6e 6f 77 29 20 6f 72 0a 20 20 20 20  not now) or.    
181cd 20 20 2a 2a 20 61 6e 20 6f 76 65 72 66 6c 6f 77    ** an overflow
181ce 20 63 65 6c 6c 2c 20 6f 72 20 69 66 20 74 68 65   cell, or if the
181cf 20 63 65 6c 6c 20 77 61 73 20 6c 6f 63 61 74 65   cell was locate
181d0 64 20 6f 6e 20 61 20 64 69 66 66 65 72 65 6e 74  d on a different
181d1 20 73 69 62 6c 69 6e 67 0a 20 20 20 20 20 20 2a   sibling.      *
181d2 2a 20 70 61 67 65 20 62 65 66 6f 72 65 20 74 68  * page before th
181d3 65 20 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68 65  e balancing, the
181d4 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61  n the pointer ma
181d5 70 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69  p entries associ
181d6 61 74 65 64 0a 20 20 20 20 20 20 2a 2a 20 77 69  ated.      ** wi
181d7 74 68 20 61 6e 79 20 63 68 69 6c 64 20 6f 72 20  th any child or 
181d8 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 6e  overflow pages n
181d9 65 65 64 20 74 6f 20 62 65 20 75 70 64 61 74 65  eed to be update
181da 64 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  d.  */.      if(
181db 20 69 73 44 69 76 69 64 65 72 20 7c 7c 20 70 4f   isDivider || pO
181dc 6c 64 2d 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e  ld->pgno!=pNew->
181dd 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20  pgno ){.        
181de 69 66 28 20 21 6c 65 61 66 43 6f 72 72 65 63 74  if( !leafCorrect
181df 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ion ){.         
181e0 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20   ptrmapPut(pBt, 
181e1 67 65 74 34 62 79 74 65 28 61 70 43 65 6c 6c 5b  get4byte(apCell[
181e2 69 5d 29 2c 20 50 54 52 4d 41 50 5f 42 54 52 45  i]), PTRMAP_BTRE
181e3 45 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 26  E, pNew->pgno, &
181e4 72 63 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  rc);.        }. 
181e5 20 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c         if( szCel
181e6 6c 5b 69 5d 3e 70 4e 65 77 2d 3e 6d 69 6e 4c 6f  l[i]>pNew->minLo
181e7 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  cal ){.         
181e8 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74   ptrmapPutOvflPt
181e9 72 28 70 4e 65 77 2c 20 61 70 43 65 6c 6c 5b 69  r(pNew, apCell[i
181ea 5d 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20  ], &rc);.       
181eb 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
181ec 0a 0a 20 20 20 20 69 66 28 20 21 6c 65 61 66 43  ..    if( !leafC
181ed 6f 72 72 65 63 74 69 6f 6e 20 29 7b 0a 20 20 20  orrection ){.   
181ee 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e     for(i=0; i<nN
181ef 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ew; i++){.      
181f0 20 20 75 33 32 20 6b 65 79 20 3d 20 67 65 74 34    u32 key = get4
181f1 62 79 74 65 28 26 61 70 4e 65 77 5b 69 5d 2d 3e  byte(&apNew[i]->
181f2 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20 20 20 20  aData[8]);.     
181f3 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74     ptrmapPut(pBt
181f4 2c 20 6b 65 79 2c 20 50 54 52 4d 41 50 5f 42 54  , key, PTRMAP_BT
181f5 52 45 45 2c 20 61 70 4e 65 77 5b 69 5d 2d 3e 70  REE, apNew[i]->p
181f6 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20  gno, &rc);.     
181f7 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30 0a   }.    }..#if 0.
181f8 20 20 20 20 2f 2a 20 54 68 65 20 70 74 72 6d 61      /* The ptrma
181f9 70 43 68 65 63 6b 50 61 67 65 73 28 29 20 63 6f  pCheckPages() co
181fa 6e 74 61 69 6e 73 20 61 73 73 65 72 74 28 29 20  ntains assert() 
181fb 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20  statements that 
181fc 76 65 72 69 66 79 20 74 68 61 74 0a 20 20 20 20  verify that.    
181fd 2a 2a 20 61 6c 6c 20 70 6f 69 6e 74 65 72 20 6d  ** all pointer m
181fe 61 70 20 70 61 67 65 73 20 61 72 65 20 73 65 74  ap pages are set
181ff 20 63 6f 72 72 65 63 74 6c 79 2e 20 54 68 69 73   correctly. This
18200 20 69 73 20 68 65 6c 70 66 75 6c 20 77 68 69 6c   is helpful whil
18201 65 20 0a 20 20 20 20 2a 2a 20 64 65 62 75 67 67  e .    ** debugg
18202 69 6e 67 2e 20 54 68 69 73 20 69 73 20 75 73 75  ing. This is usu
18203 61 6c 6c 79 20 64 69 73 61 62 6c 65 64 20 62 65  ally disabled be
18204 63 61 75 73 65 20 61 20 63 6f 72 72 75 70 74 20  cause a corrupt 
18205 64 61 74 61 62 61 73 65 20 6d 61 79 0a 20 20 20  database may.   
18206 20 2a 2a 20 63 61 75 73 65 20 61 6e 20 61 73 73   ** cause an ass
18207 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 20  ert() statement 
18208 74 6f 20 66 61 69 6c 2e 20 20 2a 2f 0a 20 20 20  to fail.  */.   
18209 20 70 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65   ptrmapCheckPage
1820a 73 28 61 70 4e 65 77 2c 20 6e 4e 65 77 29 3b 0a  s(apNew, nNew);.
1820b 20 20 20 20 70 74 72 6d 61 70 43 68 65 63 6b 50      ptrmapCheckP
1820c 61 67 65 73 28 26 70 50 61 72 65 6e 74 2c 20 31  ages(&pParent, 1
1820d 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20  );.#endif.  }.. 
1820e 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74   assert( pParent
1820f 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 54 52  ->isInit );.  TR
18210 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 66  ACE(("BALANCE: f
18211 69 6e 69 73 68 65 64 3a 20 6f 6c 64 3d 25 64 20  inished: old=%d 
18212 6e 65 77 3d 25 64 20 63 65 6c 6c 73 3d 25 64 5c  new=%d cells=%d\
18213 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 6e 4f  n",.          nO
18214 6c 64 2c 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c 29  ld, nNew, nCell)
18215 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 6c  );..  /*.  ** Cl
18216 65 61 6e 75 70 20 62 65 66 6f 72 65 20 72 65 74  eanup before ret
18217 75 72 6e 69 6e 67 2e 0a 20 20 2a 2f 0a 62 61 6c  urning..  */.bal
18218 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3a 0a 20 20  ance_cleanup:.  
18219 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 46 72  sqlite3ScratchFr
1821a 65 65 28 61 70 43 65 6c 6c 29 3b 0a 20 20 66 6f  ee(apCell);.  fo
1821b 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69  r(i=0; i<nOld; i
1821c 2b 2b 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65  ++){.    release
1821d 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a  Page(apOld[i]);.
1821e 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69    }.  for(i=0; i
1821f 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nNew; i++){.   
18220 20 72 65 6c 65 61 73 65 50 61 67 65 28 61 70 4e   releasePage(apN
18221 65 77 5b 69 5d 29 3b 0a 20 20 7d 0a 0a 20 20 72  ew[i]);.  }..  r
18222 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
18223 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
18224 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  n is called when
18225 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f   the root page o
18226 66 20 61 20 62 2d 74 72 65 65 20 73 74 72 75 63  f a b-tree struc
18227 74 75 72 65 20 69 73 0a 2a 2a 20 6f 76 65 72 66  ture is.** overf
18228 75 6c 6c 20 28 68 61 73 20 6f 6e 65 20 6f 72 20  ull (has one or 
18229 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61  more overflow pa
1822a 67 65 73 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65  ges)..**.** A ne
1822b 77 20 63 68 69 6c 64 20 70 61 67 65 20 69 73 20  w child page is 
1822c 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68  allocated and th
1822d 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
1822e 65 20 63 75 72 72 65 6e 74 20 72 6f 6f 74 0a 2a  e current root.*
1822f 2a 20 70 61 67 65 2c 20 69 6e 63 6c 75 64 69 6e  * page, includin
18230 67 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73  g overflow cells
18231 2c 20 61 72 65 20 63 6f 70 69 65 64 20 69 6e 74  , are copied int
18232 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 54 68 65  o the child. The
18233 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73   root.** page is
18234 20 74 68 65 6e 20 6f 76 65 72 77 72 69 74 74 65   then overwritte
18235 6e 20 74 6f 20 6d 61 6b 65 20 69 74 20 61 6e 20  n to make it an 
18236 65 6d 70 74 79 20 70 61 67 65 20 77 69 74 68 20  empty page with 
18237 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20  the right-child 
18238 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e  .** pointer poin
18239 74 69 6e 67 20 74 6f 20 74 68 65 20 6e 65 77 20  ting to the new 
1823a 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f  page..**.** Befo
1823b 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 61 6c  re returning, al
1823c 6c 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e  l pointer-map en
1823d 74 72 69 65 73 20 63 6f 72 72 65 73 70 6f 6e 64  tries correspond
1823e 69 6e 67 20 74 6f 20 70 61 67 65 73 20 0a 2a 2a  ing to pages .**
1823f 20 74 68 61 74 20 74 68 65 20 6e 65 77 20 63 68   that the new ch
18240 69 6c 64 2d 70 61 67 65 20 6e 6f 77 20 63 6f 6e  ild-page now con
18241 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 73 20 74  tains pointers t
18242 6f 20 61 72 65 20 75 70 64 61 74 65 64 2e 20 54  o are updated. T
18243 68 65 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 72 72  he.** entry corr
18244 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65  esponding to the
18245 20 6e 65 77 20 72 69 67 68 74 2d 63 68 69 6c 64   new right-child
18246 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20   pointer of the 
18247 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73 20  root.** page is 
18248 61 6c 73 6f 20 75 70 64 61 74 65 64 2e 0a 2a 2a  also updated..**
18249 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75  .** If successfu
1824a 6c 2c 20 2a 70 70 43 68 69 6c 64 20 69 73 20 73  l, *ppChild is s
1824b 65 74 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20  et to contain a 
1824c 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65  reference to the
1824d 20 63 68 69 6c 64 20 0a 2a 2a 20 70 61 67 65 20   child .** page 
1824e 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  and SQLITE_OK is
1824f 20 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68   returned. In th
18250 69 73 20 63 61 73 65 20 74 68 65 20 63 61 6c 6c  is case the call
18251 65 72 20 69 73 20 72 65 71 75 69 72 65 64 0a 2a  er is required.*
18252 2a 20 74 6f 20 63 61 6c 6c 20 72 65 6c 65 61 73  * to call releas
18253 65 50 61 67 65 28 29 20 6f 6e 20 2a 70 70 43 68  ePage() on *ppCh
18254 69 6c 64 20 65 78 61 63 74 6c 79 20 6f 6e 63 65  ild exactly once
18255 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  . If an error oc
18256 63 75 72 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f  curs,.** an erro
18257 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
18258 65 64 20 61 6e 64 20 2a 70 70 43 68 69 6c 64 20  ed and *ppChild 
18259 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2f 0a  is set to 0..*/.
1825a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e  static int balan
1825b 63 65 5f 64 65 65 70 65 72 28 4d 65 6d 50 61 67  ce_deeper(MemPag
1825c 65 20 2a 70 52 6f 6f 74 2c 20 4d 65 6d 50 61 67  e *pRoot, MemPag
1825d 65 20 2a 2a 70 70 43 68 69 6c 64 29 7b 0a 20 20  e **ppChild){.  
1825e 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
1825f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
18260 2a 20 52 65 74 75 72 6e 20 76 61 6c 75 65 20 66  * Return value f
18261 72 6f 6d 20 73 75 62 70 72 6f 63 65 64 75 72 65  rom subprocedure
18262 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a  s */.  MemPage *
18263 70 43 68 69 6c 64 20 3d 20 30 3b 20 20 20 20 20  pChild = 0;     
18264 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
18265 20 74 6f 20 61 20 6e 65 77 20 63 68 69 6c 64 20   to a new child 
18266 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70  page */.  Pgno p
18267 67 6e 6f 43 68 69 6c 64 20 3d 20 30 3b 20 20 20  gnoChild = 0;   
18268 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
18269 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e   number of the n
1826a 65 77 20 63 68 69 6c 64 20 70 61 67 65 20 2a 2f  ew child page */
1826b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
1826c 20 3d 20 70 52 6f 6f 74 2d 3e 70 42 74 3b 20 20   = pRoot->pBt;  
1826d 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 2a    /* The BTree *
1826e 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 52 6f  /..  assert( pRo
1826f 6f 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20  ot->nOverflow>0 
18270 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
18271 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
18272 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a  pBt->mutex) );..
18273 20 20 2f 2a 20 4d 61 6b 65 20 70 52 6f 6f 74 2c    /* Make pRoot,
18274 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f   the root page o
18275 66 20 74 68 65 20 62 2d 74 72 65 65 2c 20 77 72  f the b-tree, wr
18276 69 74 61 62 6c 65 2e 20 41 6c 6c 6f 63 61 74 65  itable. Allocate
18277 20 61 20 6e 65 77 20 0a 20 20 2a 2a 20 70 61 67   a new .  ** pag
18278 65 20 74 68 61 74 20 77 69 6c 6c 20 62 65 63 6f  e that will beco
18279 6d 65 20 74 68 65 20 6e 65 77 20 72 69 67 68 74  me the new right
1827a 2d 63 68 69 6c 64 20 6f 66 20 70 50 61 67 65 2e  -child of pPage.
1827b 20 43 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e   Copy the conten
1827c 74 73 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e  ts.  ** of the n
1827d 6f 64 65 20 73 74 6f 72 65 64 20 6f 6e 20 70 52  ode stored on pR
1827e 6f 6f 74 20 69 6e 74 6f 20 74 68 65 20 6e 65 77  oot into the new
1827f 20 63 68 69 6c 64 20 70 61 67 65 2e 0a 20 20 2a   child page..  *
18280 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  /.  rc = sqlite3
18281 50 61 67 65 72 57 72 69 74 65 28 70 52 6f 6f 74  PagerWrite(pRoot
18282 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66  ->pDbPage);.  if
18283 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
18284 29 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f  ){.    rc = allo
18285 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42  cateBtreePage(pB
18286 74 2c 26 70 43 68 69 6c 64 2c 26 70 67 6e 6f 43  t,&pChild,&pgnoC
18287 68 69 6c 64 2c 70 52 6f 6f 74 2d 3e 70 67 6e 6f  hild,pRoot->pgno
18288 2c 30 29 3b 0a 20 20 20 20 63 6f 70 79 4e 6f 64  ,0);.    copyNod
18289 65 43 6f 6e 74 65 6e 74 28 70 52 6f 6f 74 2c 20  eContent(pRoot, 
1828a 70 43 68 69 6c 64 2c 20 26 72 63 29 3b 0a 20 20  pChild, &rc);.  
1828b 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55    if( ISAUTOVACU
1828c 55 4d 20 29 7b 0a 20 20 20 20 20 20 70 74 72 6d  UM ){.      ptrm
1828d 61 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 43  apPut(pBt, pgnoC
1828e 68 69 6c 64 2c 20 50 54 52 4d 41 50 5f 42 54 52  hild, PTRMAP_BTR
1828f 45 45 2c 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c  EE, pRoot->pgno,
18290 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d   &rc);.    }.  }
18291 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20  .  if( rc ){.   
18292 20 2a 70 70 43 68 69 6c 64 20 3d 20 30 3b 0a 20   *ppChild = 0;. 
18293 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
18294 43 68 69 6c 64 29 3b 0a 20 20 20 20 72 65 74 75  Child);.    retu
18295 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73  rn rc;.  }.  ass
18296 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
18297 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 43 68  rIswriteable(pCh
18298 69 6c 64 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  ild->pDbPage) );
18299 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1829a 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
1829b 6c 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67  le(pRoot->pDbPag
1829c 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  e) );.  assert( 
1829d 70 43 68 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d 70  pChild->nCell==p
1829e 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a  Root->nCell );..
1829f 20 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43    TRACE(("BALANC
182a0 45 3a 20 63 6f 70 79 20 72 6f 6f 74 20 25 64 20  E: copy root %d 
182a1 69 6e 74 6f 20 25 64 5c 6e 22 2c 20 70 52 6f 6f  into %d\n", pRoo
182a2 74 2d 3e 70 67 6e 6f 2c 20 70 43 68 69 6c 64 2d  t->pgno, pChild-
182a3 3e 70 67 6e 6f 29 29 3b 0a 0a 20 20 2f 2a 20 43  >pgno));..  /* C
182a4 6f 70 79 20 74 68 65 20 6f 76 65 72 66 6c 6f 77  opy the overflow
182a5 20 63 65 6c 6c 73 20 66 72 6f 6d 20 70 52 6f 6f   cells from pRoo
182a6 74 20 74 6f 20 70 43 68 69 6c 64 20 2a 2f 0a 20  t to pChild */. 
182a7 20 6d 65 6d 63 70 79 28 70 43 68 69 6c 64 2d 3e   memcpy(pChild->
182a8 61 4f 76 66 6c 2c 20 70 52 6f 6f 74 2d 3e 61 4f  aOvfl, pRoot->aO
182a9 76 66 6c 2c 20 70 52 6f 6f 74 2d 3e 6e 4f 76 65  vfl, pRoot->nOve
182aa 72 66 6c 6f 77 2a 73 69 7a 65 6f 66 28 70 52 6f  rflow*sizeof(pRo
182ab 6f 74 2d 3e 61 4f 76 66 6c 5b 30 5d 29 29 3b 0a  ot->aOvfl[0]));.
182ac 20 20 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66    pChild->nOverf
182ad 6c 6f 77 20 3d 20 70 52 6f 6f 74 2d 3e 6e 4f 76  low = pRoot->nOv
182ae 65 72 66 6c 6f 77 3b 0a 0a 20 20 2f 2a 20 5a 65  erflow;..  /* Ze
182af 72 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  ro the contents 
182b0 6f 66 20 70 52 6f 6f 74 2e 20 54 68 65 6e 20 69  of pRoot. Then i
182b1 6e 73 74 61 6c 6c 20 70 43 68 69 6c 64 20 61 73  nstall pChild as
182b2 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64   the right-child
182b3 2e 20 2a 2f 0a 20 20 7a 65 72 6f 50 61 67 65 28  . */.  zeroPage(
182b4 70 52 6f 6f 74 2c 20 70 43 68 69 6c 64 2d 3e 61  pRoot, pChild->a
182b5 44 61 74 61 5b 30 5d 20 26 20 7e 50 54 46 5f 4c  Data[0] & ~PTF_L
182b6 45 41 46 29 3b 0a 20 20 70 75 74 34 62 79 74 65  EAF);.  put4byte
182b7 28 26 70 52 6f 6f 74 2d 3e 61 44 61 74 61 5b 70  (&pRoot->aData[p
182b8 52 6f 6f 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b  Root->hdrOffset+
182b9 38 5d 2c 20 70 67 6e 6f 43 68 69 6c 64 29 3b 0a  8], pgnoChild);.
182ba 0a 20 20 2a 70 70 43 68 69 6c 64 20 3d 20 70 43  .  *ppChild = pC
182bb 68 69 6c 64 3b 0a 20 20 72 65 74 75 72 6e 20 53  hild;.  return S
182bc 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
182bd 2a 2a 20 54 68 65 20 70 61 67 65 20 74 68 61 74  ** The page that
182be 20 70 43 75 72 20 63 75 72 72 65 6e 74 6c 79 20   pCur currently 
182bf 70 6f 69 6e 74 73 20 74 6f 20 68 61 73 20 6a 75  points to has ju
182c0 73 74 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64  st been modified
182c1 20 69 6e 0a 2a 2a 20 73 6f 6d 65 20 77 61 79 2e   in.** some way.
182c2 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66   This function f
182c3 69 67 75 72 65 73 20 6f 75 74 20 69 66 20 74 68  igures out if th
182c4 69 73 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20  is modification 
182c5 6d 65 61 6e 73 20 74 68 65 0a 2a 2a 20 74 72 65  means the.** tre
182c6 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 62 61  e needs to be ba
182c7 6c 61 6e 63 65 64 2c 20 61 6e 64 20 69 66 20 73  lanced, and if s
182c8 6f 20 63 61 6c 6c 73 20 74 68 65 20 61 70 70 72  o calls the appr
182c9 6f 70 72 69 61 74 65 20 62 61 6c 61 6e 63 69 6e  opriate balancin
182ca 67 20 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20 42  g .** routine. B
182cb 61 6c 61 6e 63 69 6e 67 20 72 6f 75 74 69 6e 65  alancing routine
182cc 73 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 62  s are:.**.**   b
182cd 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 0a 2a  alance_quick().*
182ce 2a 20 20 20 62 61 6c 61 6e 63 65 5f 64 65 65 70  *   balance_deep
182cf 65 72 28 29 0a 2a 2a 20 20 20 62 61 6c 61 6e 63  er().**   balanc
182d0 65 5f 6e 6f 6e 72 6f 6f 74 28 29 0a 2a 2f 0a 73  e_nonroot().*/.s
182d1 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63  tatic int balanc
182d2 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  e(BtCursor *pCur
182d3 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
182d4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 6f 6e 73 74  LITE_OK;.  const
182d5 20 69 6e 74 20 6e 4d 69 6e 20 3d 20 70 43 75 72   int nMin = pCur
182d6 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  ->pBt->usableSiz
182d7 65 20 2a 20 32 20 2f 20 33 3b 0a 20 20 75 38 20  e * 2 / 3;.  u8 
182d8 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61  aBalanceQuickSpa
182d9 63 65 5b 31 33 5d 3b 0a 20 20 75 38 20 2a 70 46  ce[13];.  u8 *pF
182da 72 65 65 20 3d 20 30 3b 0a 0a 20 20 54 45 53 54  ree = 0;..  TEST
182db 4f 4e 4c 59 28 20 69 6e 74 20 62 61 6c 61 6e 63  ONLY( int balanc
182dc 65 5f 71 75 69 63 6b 5f 63 61 6c 6c 65 64 20 3d  e_quick_called =
182dd 20 30 20 29 3b 0a 20 20 54 45 53 54 4f 4e 4c 59   0 );.  TESTONLY
182de 28 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f 64 65  ( int balance_de
182df 65 70 65 72 5f 63 61 6c 6c 65 64 20 3d 20 30 20  eper_called = 0 
182e0 29 3b 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69  );..  do {.    i
182e1 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d  nt iPage = pCur-
182e2 3e 69 50 61 67 65 3b 0a 20 20 20 20 4d 65 6d 50  >iPage;.    MemP
182e3 61 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75  age *pPage = pCu
182e4 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65 5d  r->apPage[iPage]
182e5 3b 0a 0a 20 20 20 20 69 66 28 20 69 50 61 67 65  ;..    if( iPage
182e6 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ==0 ){.      if(
182e7 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f   pPage->nOverflo
182e8 77 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  w ){.        /* 
182e9 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66  The root page of
182ea 20 74 68 65 20 62 2d 74 72 65 65 20 69 73 20 6f   the b-tree is o
182eb 76 65 72 66 75 6c 6c 2e 20 49 6e 20 74 68 69 73  verfull. In this
182ec 20 63 61 73 65 20 63 61 6c 6c 20 74 68 65 0a 20   case call the. 
182ed 20 20 20 20 20 20 20 2a 2a 20 62 61 6c 61 6e 63         ** balanc
182ee 65 5f 64 65 65 70 65 72 28 29 20 66 75 6e 63 74  e_deeper() funct
182ef 69 6f 6e 20 74 6f 20 63 72 65 61 74 65 20 61 20  ion to create a 
182f0 6e 65 77 20 63 68 69 6c 64 20 66 6f 72 20 74 68  new child for th
182f1 65 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20  e root-page.    
182f2 20 20 20 20 2a 2a 20 61 6e 64 20 63 6f 70 79 20      ** and copy 
182f3 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74  the current cont
182f4 65 6e 74 73 20 6f 66 20 74 68 65 20 72 6f 6f 74  ents of the root
182f5 2d 70 61 67 65 20 74 6f 20 69 74 2e 20 54 68 65  -page to it. The
182f6 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 65 78 74  .        ** next
182f7 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68   iteration of th
182f8 65 20 64 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 62  e do-loop will b
182f9 61 6c 61 6e 63 65 20 74 68 65 20 63 68 69 6c 64  alance the child
182fa 20 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20 2a   page..        *
182fb 2f 20 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  / .        asser
182fc 74 28 20 28 62 61 6c 61 6e 63 65 5f 64 65 65 70  t( (balance_deep
182fd 65 72 5f 63 61 6c 6c 65 64 2b 2b 29 3d 3d 30 20  er_called++)==0 
182fe 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
182ff 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 70  balance_deeper(p
18300 50 61 67 65 2c 20 26 70 43 75 72 2d 3e 61 70 50  Page, &pCur->apP
18301 61 67 65 5b 31 5d 29 3b 0a 20 20 20 20 20 20 20  age[1]);.       
18302 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
18303 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
18304 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 31 3b  pCur->iPage = 1;
18305 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 72 2d  .          pCur-
18306 3e 61 69 49 64 78 5b 30 5d 20 3d 20 30 3b 0a 20  >aiIdx[0] = 0;. 
18307 20 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 61           pCur->a
18308 69 49 64 78 5b 31 5d 20 3d 20 30 3b 0a 20 20 20  iIdx[1] = 0;.   
18309 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1830a 43 75 72 2d 3e 61 70 50 61 67 65 5b 31 5d 2d 3e  Cur->apPage[1]->
1830b 6e 4f 76 65 72 66 6c 6f 77 20 29 3b 0a 20 20 20  nOverflow );.   
1830c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
1830d 73 65 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61  se{.        brea
1830e 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
1830f 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 2d 3e  else if( pPage->
18310 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20  nOverflow==0 && 
18311 70 50 61 67 65 2d 3e 6e 46 72 65 65 3c 3d 6e 4d  pPage->nFree<=nM
18312 69 6e 20 29 7b 0a 20 20 20 20 20 20 62 72 65 61  in ){.      brea
18313 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  k;.    }else{.  
18314 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 20 63 6f      MemPage * co
18315 6e 73 74 20 70 50 61 72 65 6e 74 20 3d 20 70 43  nst pParent = pC
18316 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65  ur->apPage[iPage
18317 2d 31 5d 3b 0a 20 20 20 20 20 20 69 6e 74 20 63  -1];.      int c
18318 6f 6e 73 74 20 69 49 64 78 20 3d 20 70 43 75 72  onst iIdx = pCur
18319 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 2d 31 5d  ->aiIdx[iPage-1]
1831a 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ;..      rc = sq
1831b 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
1831c 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65  pParent->pDbPage
1831d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
1831e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23 69  =SQLITE_OK ){.#i
1831f 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
18320 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 0a 20  T_QUICKBALANCE. 
18321 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65         if( pPage
18322 2d 3e 68 61 73 44 61 74 61 0a 20 20 20 20 20 20  ->hasData.      
18323 20 20 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f 76     && pPage->nOv
18324 65 72 66 6c 6f 77 3d 3d 31 0a 20 20 20 20 20 20  erflow==1.      
18325 20 20 20 26 26 20 70 50 61 67 65 2d 3e 61 4f 76     && pPage->aOv
18326 66 6c 5b 30 5d 2e 69 64 78 3d 3d 70 50 61 67 65  fl[0].idx==pPage
18327 2d 3e 6e 43 65 6c 6c 0a 20 20 20 20 20 20 20 20  ->nCell.        
18328 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e   && pParent->pgn
18329 6f 21 3d 31 0a 20 20 20 20 20 20 20 20 20 26 26  o!=1.         &&
1832a 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d   pParent->nCell=
1832b 3d 69 49 64 78 0a 20 20 20 20 20 20 20 20 29 7b  =iIdx.        ){
1832c 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61  .          /* Ca
1832d 6c 6c 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b  ll balance_quick
1832e 28 29 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e  () to create a n
1832f 65 77 20 73 69 62 6c 69 6e 67 20 6f 66 20 70 50  ew sibling of pP
18330 61 67 65 20 6f 6e 20 77 68 69 63 68 0a 20 20 20  age on which.   
18331 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 73 74 6f         ** to sto
18332 72 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  re the overflow 
18333 63 65 6c 6c 2e 20 62 61 6c 61 6e 63 65 5f 71 75  cell. balance_qu
18334 69 63 6b 28 29 20 69 6e 73 65 72 74 73 20 61 20  ick() inserts a 
18335 6e 65 77 20 63 65 6c 6c 0a 20 20 20 20 20 20 20  new cell.       
18336 20 20 20 2a 2a 20 69 6e 74 6f 20 70 50 61 72 65     ** into pPare
18337 6e 74 2c 20 77 68 69 63 68 20 6d 61 79 20 63 61  nt, which may ca
18338 75 73 65 20 70 50 61 72 65 6e 74 20 6f 76 65 72  use pParent over
18339 66 6c 6f 77 2e 20 49 66 20 74 68 69 73 0a 20 20  flow. If this.  
1833a 20 20 20 20 20 20 20 20 2a 2a 20 68 61 70 70 65          ** happe
1833b 6e 73 2c 20 74 68 65 20 6e 65 78 74 20 69 6e 74  ns, the next int
1833c 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64  eration of the d
1833d 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 62 61 6c 61  o-loop will bala
1833e 6e 63 65 20 70 50 61 72 65 6e 74 20 0a 20 20 20  nce pParent .   
1833f 20 20 20 20 20 20 20 2a 2a 20 75 73 65 20 65 69         ** use ei
18340 74 68 65 72 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e  ther balance_non
18341 72 6f 6f 74 28 29 20 6f 72 20 62 61 6c 61 6e 63  root() or balanc
18342 65 5f 64 65 65 70 65 72 28 29 2e 20 55 6e 74 69  e_deeper(). Unti
18343 6c 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 20  l this.         
18344 20 2a 2a 20 68 61 70 70 65 6e 73 2c 20 74 68 65   ** happens, the
18345 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 69   overflow cell i
18346 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  s stored in the 
18347 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61  aBalanceQuickSpa
18348 63 65 5b 5d 0a 20 20 20 20 20 20 20 20 20 20 2a  ce[].          *
18349 2a 20 62 75 66 66 65 72 2e 20 0a 20 20 20 20 20  * buffer. .     
1834a 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
1834b 20 20 2a 2a 20 54 68 65 20 70 75 72 70 6f 73 65    ** The purpose
1834c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
1834d 67 20 61 73 73 65 72 74 28 29 20 69 73 20 74 6f  g assert() is to
1834e 20 63 68 65 63 6b 20 74 68 61 74 20 6f 6e 6c 79   check that only
1834f 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20   a.          ** 
18350 73 69 6e 67 6c 65 20 63 61 6c 6c 20 74 6f 20 62  single call to b
18351 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 20 69  alance_quick() i
18352 73 20 6d 61 64 65 20 66 6f 72 20 65 61 63 68 20  s made for each 
18353 63 61 6c 6c 20 74 6f 20 74 68 69 73 0a 20 20 20  call to this.   
18354 20 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69         ** functi
18355 6f 6e 2e 20 49 66 20 74 68 69 73 20 77 65 72 65  on. If this were
18356 20 6e 6f 74 20 76 65 72 69 66 69 65 64 2c 20 61   not verified, a
18357 20 73 75 62 74 6c 65 20 62 75 67 20 69 6e 76 6f   subtle bug invo
18358 6c 76 69 6e 67 20 72 65 75 73 65 0a 20 20 20 20  lving reuse.    
18359 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20        ** of the 
1835a 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61  aBalanceQuickSpa
1835b 63 65 5b 5d 20 6d 69 67 68 74 20 73 6e 65 61 6b  ce[] might sneak
1835c 20 69 6e 2e 0a 20 20 20 20 20 20 20 20 20 20 2a   in..          *
1835d 2f 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  /.          asse
1835e 72 74 28 20 28 62 61 6c 61 6e 63 65 5f 71 75 69  rt( (balance_qui
1835f 63 6b 5f 63 61 6c 6c 65 64 2b 2b 29 3d 3d 30 20  ck_called++)==0 
18360 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  );.          rc 
18361 3d 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28  = balance_quick(
18362 70 50 61 72 65 6e 74 2c 20 70 50 61 67 65 2c 20  pParent, pPage, 
18363 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61  aBalanceQuickSpa
18364 63 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  ce);.        }el
18365 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  se.#endif.      
18366 20 20 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a    {.          /*
18367 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 63   In this case, c
18368 61 6c 6c 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72  all balance_nonr
18369 6f 6f 74 28 29 20 74 6f 20 72 65 64 69 73 74 72  oot() to redistr
1836a 69 62 75 74 65 20 63 65 6c 6c 73 0a 20 20 20 20  ibute cells.    
1836b 20 20 20 20 20 20 2a 2a 20 62 65 74 77 65 65 6e        ** between
1836c 20 70 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f   pPage and up to
1836d 20 32 20 6f 66 20 69 74 73 20 73 69 62 6c 69 6e   2 of its siblin
1836e 67 20 70 61 67 65 73 2e 20 54 68 69 73 20 69 6e  g pages. This in
1836f 76 6f 6c 76 65 73 0a 20 20 20 20 20 20 20 20 20  volves.         
18370 20 2a 2a 20 6d 6f 64 69 66 79 69 6e 67 20 74 68   ** modifying th
18371 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 50  e contents of pP
18372 61 72 65 6e 74 2c 20 77 68 69 63 68 20 6d 61 79  arent, which may
18373 20 63 61 75 73 65 20 70 50 61 72 65 6e 74 20 74   cause pParent t
18374 6f 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62  o.          ** b
18375 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c 6c 20 6f  ecome overfull o
18376 72 20 75 6e 64 65 72 66 75 6c 6c 2e 20 54 68 65  r underfull. The
18377 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20   next iteration 
18378 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 0a 20  of the do-loop. 
18379 20 20 20 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c           ** will
1837a 20 62 61 6c 61 6e 63 65 20 74 68 65 20 70 61 72   balance the par
1837b 65 6e 74 20 70 61 67 65 20 74 6f 20 63 6f 72 72  ent page to corr
1837c 65 63 74 20 74 68 69 73 2e 0a 20 20 20 20 20 20  ect this..      
1837d 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 20 20 20      ** .        
1837e 20 20 2a 2a 20 49 66 20 74 68 65 20 70 61 72 65    ** If the pare
1837f 6e 74 20 70 61 67 65 20 62 65 63 6f 6d 65 73 20  nt page becomes 
18380 6f 76 65 72 66 75 6c 6c 2c 20 74 68 65 20 6f 76  overfull, the ov
18381 65 72 66 6c 6f 77 20 63 65 6c 6c 20 6f 72 20 63  erflow cell or c
18382 65 6c 6c 73 0a 20 20 20 20 20 20 20 20 20 20 2a  ells.          *
18383 2a 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20  * are stored in 
18384 74 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65  the pSpace buffe
18385 72 20 61 6c 6c 6f 63 61 74 65 64 20 69 6d 6d 65  r allocated imme
18386 64 69 61 74 65 6c 79 20 62 65 6c 6f 77 2e 20 0a  diately below. .
18387 20 20 20 20 20 20 20 20 20 20 2a 2a 20 41 20 73            ** A s
18388 75 62 73 65 71 75 65 6e 74 20 69 74 65 72 61 74  ubsequent iterat
18389 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f  ion of the do-lo
1838a 6f 70 20 77 69 6c 6c 20 64 65 61 6c 20 77 69 74  op will deal wit
1838b 68 20 74 68 69 73 20 62 79 0a 20 20 20 20 20 20  h this by.      
1838c 20 20 20 20 2a 2a 20 63 61 6c 6c 69 6e 67 20 62      ** calling b
1838d 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29  alance_nonroot()
1838e 20 28 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72   (balance_deeper
1838f 28 29 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64  () may be called
18390 20 66 69 72 73 74 2c 0a 20 20 20 20 20 20 20 20   first,.        
18391 20 20 2a 2a 20 62 75 74 20 69 74 20 64 6f 65 73    ** but it does
18392 6e 27 74 20 64 65 61 6c 20 77 69 74 68 20 6f 76  n't deal with ov
18393 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 2d 20 6a  erflow cells - j
18394 75 73 74 20 6d 6f 76 65 73 20 74 68 65 6d 20 74  ust moves them t
18395 6f 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  o a.          **
18396 20 64 69 66 66 65 72 65 6e 74 20 70 61 67 65 29   different page)
18397 2e 20 4f 6e 63 65 20 74 68 69 73 20 73 75 62 73  . Once this subs
18398 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 20 62  equent call to b
18399 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29  alance_nonroot()
1839a 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 68   .          ** h
1839b 61 73 20 63 6f 6d 70 6c 65 74 65 64 2c 20 69 74  as completed, it
1839c 20 69 73 20 73 61 66 65 20 74 6f 20 72 65 6c 65   is safe to rele
1839d 61 73 65 20 74 68 65 20 70 53 70 61 63 65 20 62  ase the pSpace b
1839e 75 66 66 65 72 20 75 73 65 64 20 62 79 0a 20 20  uffer used by.  
1839f 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 70          ** the p
183a0 72 65 76 69 6f 75 73 20 63 61 6c 6c 2c 20 61 73  revious call, as
183a1 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65   the overflow ce
183a2 6c 6c 20 64 61 74 61 20 77 69 6c 6c 20 68 61 76  ll data will hav
183a3 65 20 62 65 65 6e 20 0a 20 20 20 20 20 20 20 20  e been .        
183a4 20 20 2a 2a 20 63 6f 70 69 65 64 20 65 69 74 68    ** copied eith
183a5 65 72 20 69 6e 74 6f 20 74 68 65 20 62 6f 64 79  er into the body
183a6 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 70   of a database p
183a7 61 67 65 20 6f 72 20 69 6e 74 6f 20 74 68 65 20  age or into the 
183a8 6e 65 77 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  new.          **
183a9 20 70 53 70 61 63 65 20 62 75 66 66 65 72 20 70   pSpace buffer p
183aa 61 73 73 65 64 20 74 6f 20 74 68 65 20 6c 61 74  assed to the lat
183ab 74 65 72 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61  ter call to bala
183ac 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 2e 0a 20  nce_nonroot().. 
183ad 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
183ae 20 20 20 20 20 20 75 38 20 2a 70 53 70 61 63 65        u8 *pSpace
183af 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61   = sqlite3PageMa
183b0 6c 6c 6f 63 28 70 43 75 72 2d 3e 70 42 74 2d 3e  lloc(pCur->pBt->
183b1 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20  pageSize);.     
183b2 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63       rc = balanc
183b3 65 5f 6e 6f 6e 72 6f 6f 74 28 70 50 61 72 65 6e  e_nonroot(pParen
183b4 74 2c 20 69 49 64 78 2c 20 70 53 70 61 63 65 2c  t, iIdx, pSpace,
183b5 20 69 50 61 67 65 3d 3d 31 29 3b 0a 20 20 20 20   iPage==1);.    
183b6 20 20 20 20 20 20 69 66 28 20 70 46 72 65 65 20        if( pFree 
183b7 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f  ){.            /
183b8 2a 20 49 66 20 70 46 72 65 65 20 69 73 20 6e 6f  * If pFree is no
183b9 74 20 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 6e 74  t NULL, it point
183ba 73 20 74 6f 20 74 68 65 20 70 53 70 61 63 65 20  s to the pSpace 
183bb 62 75 66 66 65 72 20 75 73 65 64 20 0a 20 20 20  buffer used .   
183bc 20 20 20 20 20 20 20 20 20 2a 2a 20 62 79 20 61           ** by a
183bd 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 74   previous call t
183be 6f 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f  o balance_nonroo
183bf 74 28 29 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74  t(). Its content
183c0 73 20 61 72 65 0a 20 20 20 20 20 20 20 20 20 20  s are.          
183c1 20 20 2a 2a 20 6e 6f 77 20 73 74 6f 72 65 64 20    ** now stored 
183c2 65 69 74 68 65 72 20 6f 6e 20 72 65 61 6c 20 64  either on real d
183c3 61 74 61 62 61 73 65 20 70 61 67 65 73 20 6f 72  atabase pages or
183c4 20 77 69 74 68 69 6e 20 74 68 65 20 0a 20 20 20   within the .   
183c5 20 20 20 20 20 20 20 20 20 2a 2a 20 6e 65 77 20           ** new 
183c6 70 53 70 61 63 65 20 62 75 66 66 65 72 2c 20 73  pSpace buffer, s
183c7 6f 20 69 74 20 6d 61 79 20 62 65 20 73 61 66 65  o it may be safe
183c8 6c 79 20 66 72 65 65 64 20 68 65 72 65 2e 20 2a  ly freed here. *
183c9 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  /.            sq
183ca 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 70 46  lite3PageFree(pF
183cb 72 65 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ree);.          
183cc 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  }..          /* 
183cd 54 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65  The pSpace buffe
183ce 72 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20  r will be freed 
183cf 61 66 74 65 72 20 74 68 65 20 6e 65 78 74 20 63  after the next c
183d0 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20 20 20 20  all to.         
183d1 20 2a 2a 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72   ** balance_nonr
183d2 6f 6f 74 28 29 2c 20 6f 72 20 6a 75 73 74 20 62  oot(), or just b
183d3 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74  efore this funct
183d4 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 77 68 69  ion returns, whi
183d5 63 68 65 76 65 72 0a 20 20 20 20 20 20 20 20 20  chever.         
183d6 20 2a 2a 20 63 6f 6d 65 73 20 66 69 72 73 74 2e   ** comes first.
183d7 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70 46   */.          pF
183d8 72 65 65 20 3d 20 70 53 70 61 63 65 3b 0a 20 20  ree = pSpace;.  
183d9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
183da 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f  .      pPage->nO
183db 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20 20  verflow = 0;..  
183dc 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20      /* The next 
183dd 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  iteration of the
183de 20 64 6f 2d 6c 6f 6f 70 20 62 61 6c 61 6e 63 65   do-loop balance
183df 73 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67  s the parent pag
183e0 65 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65  e. */.      rele
183e1 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a  asePage(pPage);.
183e2 20 20 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67        pCur->iPag
183e3 65 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 7d 77 68  e--;.    }.  }wh
183e4 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ile( rc==SQLITE_
183e5 4f 4b 20 29 3b 0a 0a 20 20 69 66 28 20 70 46 72  OK );..  if( pFr
183e6 65 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ee ){.    sqlite
183e7 33 50 61 67 65 46 72 65 65 28 70 46 72 65 65 29  3PageFree(pFree)
183e8 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
183e9 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73  c;.}.../*.** Ins
183ea 65 72 74 20 61 20 6e 65 77 20 72 65 63 6f 72 64  ert a new record
183eb 20 69 6e 74 6f 20 74 68 65 20 42 54 72 65 65 2e   into the BTree.
183ec 20 20 54 68 65 20 6b 65 79 20 69 73 20 67 69 76    The key is giv
183ed 65 6e 20 62 79 20 28 70 4b 65 79 2c 6e 4b 65 79  en by (pKey,nKey
183ee 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74  ).** and the dat
183ef 61 20 69 73 20 67 69 76 65 6e 20 62 79 20 28 70  a is given by (p
183f0 44 61 74 61 2c 6e 44 61 74 61 29 2e 20 20 54 68  Data,nData).  Th
183f1 65 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 64  e cursor is used
183f2 20 6f 6e 6c 79 20 74 6f 0a 2a 2a 20 64 65 66 69   only to.** defi
183f3 6e 65 20 77 68 61 74 20 74 61 62 6c 65 20 74 68  ne what table th
183f4 65 20 72 65 63 6f 72 64 20 73 68 6f 75 6c 64 20  e record should 
183f5 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f  be inserted into
183f6 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a  .  The cursor.**
183f7 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
183f8 67 20 61 74 20 61 20 72 61 6e 64 6f 6d 20 6c 6f  g at a random lo
183f9 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f  cation..**.** Fo
183fa 72 20 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c  r an INTKEY tabl
183fb 65 2c 20 6f 6e 6c 79 20 74 68 65 20 6e 4b 65 79  e, only the nKey
183fc 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b 65   value of the ke
183fd 79 20 69 73 20 75 73 65 64 2e 20 20 70 4b 65 79  y is used.  pKey
183fe 20 69 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20   is.** ignored. 
183ff 20 46 6f 72 20 61 20 5a 45 52 4f 44 41 54 41 20   For a ZERODATA 
18400 74 61 62 6c 65 2c 20 74 68 65 20 70 44 61 74 61  table, the pData
18401 20 61 6e 64 20 6e 44 61 74 61 20 61 72 65 20 62   and nData are b
18402 6f 74 68 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a  oth ignored..**.
18403 2a 2a 20 49 66 20 74 68 65 20 73 65 65 6b 52 65  ** If the seekRe
18404 73 75 6c 74 20 70 61 72 61 6d 65 74 65 72 20 69  sult parameter i
18405 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
18406 20 61 20 73 75 63 63 65 73 73 66 75 6c 20 63 61   a successful ca
18407 6c 6c 20 74 6f 0a 2a 2a 20 4d 6f 76 65 74 6f 55  ll to.** MovetoU
18408 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 73 65 65  npacked() to see
18409 6b 20 63 75 72 73 6f 72 20 70 43 75 72 20 74 6f  k cursor pCur to
1840a 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 68 61   (pKey, nKey) ha
1840b 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 65  s already.** bee
1840c 6e 20 70 65 72 66 6f 72 6d 65 64 2e 20 73 65 65  n performed. see
1840d 6b 52 65 73 75 6c 74 20 69 73 20 74 68 65 20 73  kResult is the s
1840e 65 61 72 63 68 20 72 65 73 75 6c 74 20 72 65 74  earch result ret
1840f 75 72 6e 65 64 20 28 61 20 6e 65 67 61 74 69 76  urned (a negativ
18410 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 69 66 20 70  e.** number if p
18411 43 75 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e  Cur points at an
18412 20 65 6e 74 72 79 20 74 68 61 74 20 69 73 20 73   entry that is s
18413 6d 61 6c 6c 65 72 20 74 68 61 6e 20 28 70 4b 65  maller than (pKe
18414 79 2c 20 6e 4b 65 79 29 2c 20 6f 72 0a 2a 2a 20  y, nKey), or.** 
18415 61 20 70 6f 73 69 74 69 76 65 20 76 61 6c 75 65  a positive value
18416 20 69 66 20 70 43 75 72 20 70 6f 69 6e 74 73 20   if pCur points 
18417 61 74 20 61 6e 20 65 74 72 79 20 74 68 61 74 20  at an etry that 
18418 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 0a  is larger than .
18419 2a 2a 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29 29  ** (pKey, nKey))
1841a 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  . .**.** If the 
1841b 73 65 65 6b 52 65 73 75 6c 74 20 70 61 72 61 6d  seekResult param
1841c 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  eter is non-zero
1841d 2c 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 65  , then the calle
1841e 72 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 61  r guarantees tha
1841f 74 0a 2a 2a 20 63 75 72 73 6f 72 20 70 43 75 72  t.** cursor pCur
18420 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 61 74 20   is pointing at 
18421 74 68 65 20 65 78 69 73 74 69 6e 67 20 63 6f 70  the existing cop
18422 79 20 6f 66 20 61 20 72 6f 77 20 74 68 61 74 20  y of a row that 
18423 69 73 20 74 6f 20 62 65 0a 2a 2a 20 6f 76 65 72  is to be.** over
18424 77 72 69 74 74 65 6e 2e 20 20 49 66 20 74 68 65  written.  If the
18425 20 73 65 65 6b 52 65 73 75 6c 74 20 70 61 72 61   seekResult para
18426 6d 65 74 65 72 20 69 73 20 30 2c 20 74 68 65 6e  meter is 0, then
18427 20 63 75 72 73 6f 72 20 70 43 75 72 20 6d 61 79   cursor pCur may
18428 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 61 6e 79  .** point to any
18429 20 65 6e 74 72 79 20 6f 72 20 74 6f 20 6e 6f 20   entry or to no 
1842a 65 6e 74 72 79 20 61 74 20 61 6c 6c 20 61 6e 64  entry at all and
1842b 20 73 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   so this functio
1842c 6e 20 68 61 73 20 74 6f 20 73 65 65 6b 0a 2a 2a  n has to seek.**
1842d 20 74 68 65 20 63 75 72 73 6f 72 20 62 65 66 6f   the cursor befo
1842e 72 65 20 74 68 65 20 6e 65 77 20 6b 65 79 20 63  re the new key c
1842f 61 6e 20 62 65 20 69 6e 73 65 72 74 65 64 2e 0a  an be inserted..
18430 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
18431 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
18432 65 65 49 6e 73 65 72 74 28 0a 20 20 42 74 43 75  eeInsert(.  BtCu
18433 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20  rsor *pCur,     
18434 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
18435 73 65 72 74 20 64 61 74 61 20 69 6e 74 6f 20 74  sert data into t
18436 68 65 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73  he table of this
18437 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e   cursor */.  con
18438 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69  st void *pKey, i
18439 36 34 20 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 54  64 nKey,    /* T
1843a 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 6e 65  he key of the ne
1843b 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 6f  w record */.  co
1843c 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c  nst void *pData,
1843d 20 69 6e 74 20 6e 44 61 74 61 2c 20 20 2f 2a 20   int nData,  /* 
1843e 54 68 65 20 64 61 74 61 20 6f 66 20 74 68 65 20  The data of the 
1843f 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  new record */.  
18440 69 6e 74 20 6e 5a 65 72 6f 2c 20 20 20 20 20 20  int nZero,      
18441 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
18442 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 74 72  * Number of extr
18443 61 20 30 20 62 79 74 65 73 20 74 6f 20 61 70 70  a 0 bytes to app
18444 65 6e 64 20 74 6f 20 64 61 74 61 20 2a 2f 0a 20  end to data */. 
18445 20 69 6e 74 20 61 70 70 65 6e 64 42 69 61 73 2c   int appendBias,
18446 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18447 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20  /* True if this 
18448 69 73 20 6c 69 6b 65 6c 79 20 61 6e 20 61 70 70  is likely an app
18449 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 73 65 65  end */.  int see
1844a 6b 52 65 73 75 6c 74 20 20 20 20 20 20 20 20 20  kResult         
1844b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c          /* Resul
1844c 74 20 6f 66 20 70 72 69 6f 72 20 4d 6f 76 65 74  t of prior Movet
1844d 6f 55 6e 70 61 63 6b 65 64 28 29 20 63 61 6c 6c  oUnpacked() call
1844e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
1844f 0a 20 20 69 6e 74 20 6c 6f 63 20 3d 20 73 65 65  .  int loc = see
18450 6b 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20  kResult;        
18451 20 20 2f 2a 20 2d 31 3a 20 62 65 66 6f 72 65 20    /* -1: before 
18452 64 65 73 69 72 65 64 20 6c 6f 63 61 74 69 6f 6e  desired location
18453 20 20 2b 31 3a 20 61 66 74 65 72 20 2a 2f 0a 20    +1: after */. 
18454 20 69 6e 74 20 73 7a 4e 65 77 3b 0a 20 20 69 6e   int szNew;.  in
18455 74 20 69 64 78 3b 0a 20 20 4d 65 6d 50 61 67 65  t idx;.  MemPage
18456 20 2a 70 50 61 67 65 3b 0a 20 20 42 74 72 65 65   *pPage;.  Btree
18457 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 74 72   *p = pCur->pBtr
18458 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  ee;.  BtShared *
18459 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
1845a 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 6f  unsigned char *o
1845b 6c 64 43 65 6c 6c 3b 0a 20 20 75 6e 73 69 67 6e  ldCell;.  unsign
1845c 65 64 20 63 68 61 72 20 2a 6e 65 77 43 65 6c 6c  ed char *newCell
1845d 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 43 75   = 0;..  if( pCu
1845e 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
1845f 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 61  R_FAULT ){.    a
18460 73 73 65 72 74 28 20 70 43 75 72 2d 3e 73 6b 69  ssert( pCur->ski
18461 70 4e 65 78 74 21 3d 53 51 4c 49 54 45 5f 4f 4b  pNext!=SQLITE_OK
18462 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70   );.    return p
18463 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a 20  Cur->skipNext;. 
18464 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75   }..  assert( cu
18465 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
18466 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Cur) );.  assert
18467 28 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 26  ( pCur->wrFlag &
18468 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63  & pBt->inTransac
18469 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54  tion==TRANS_WRIT
1846a 45 20 26 26 20 21 70 42 74 2d 3e 72 65 61 64 4f  E && !pBt->readO
1846b 6e 6c 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28  nly );.  assert(
1846c 20 68 61 73 53 68 61 72 65 64 43 61 63 68 65 54   hasSharedCacheT
1846d 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 70 43 75 72  ableLock(p, pCur
1846e 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72  ->pgnoRoot, pCur
1846f 2d 3e 70 4b 65 79 49 6e 66 6f 21 3d 30 2c 20 32  ->pKeyInfo!=0, 2
18470 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72  ) );..  /* Asser
18471 74 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65  t that the calle
18472 72 20 68 61 73 20 62 65 65 6e 20 63 6f 6e 73 69  r has been consi
18473 73 74 65 6e 74 2e 20 49 66 20 74 68 69 73 20 63  stent. If this c
18474 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65 64  ursor was opened
18475 0a 20 20 2a 2a 20 65 78 70 65 63 74 69 6e 67 20  .  ** expecting 
18476 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 65 2c  an index b-tree,
18477 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 72   then the caller
18478 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 65 72   should be inser
18479 74 69 6e 67 20 62 6c 6f 62 0a 20 20 2a 2a 20 6b  ting blob.  ** k
1847a 65 79 73 20 77 69 74 68 20 6e 6f 20 61 73 73 6f  eys with no asso
1847b 63 69 61 74 65 64 20 64 61 74 61 2e 20 49 66 20  ciated data. If 
1847c 74 68 65 20 63 75 72 73 6f 72 20 77 61 73 20 6f  the cursor was o
1847d 70 65 6e 65 64 20 65 78 70 65 63 74 69 6e 67 20  pened expecting 
1847e 61 6e 0a 20 20 2a 2a 20 69 6e 74 6b 65 79 20 74  an.  ** intkey t
1847f 61 62 6c 65 2c 20 74 68 65 20 63 61 6c 6c 65 72  able, the caller
18480 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 65 72   should be inser
18481 74 69 6e 67 20 69 6e 74 65 67 65 72 20 6b 65 79  ting integer key
18482 73 20 77 69 74 68 20 61 0a 20 20 2a 2a 20 62 6c  s with a.  ** bl
18483 6f 62 20 6f 66 20 61 73 73 6f 63 69 61 74 65 64  ob of associated
18484 20 64 61 74 61 2e 20 20 2a 2f 0a 20 20 61 73 73   data.  */.  ass
18485 65 72 74 28 20 28 70 4b 65 79 3d 3d 30 29 3d 3d  ert( (pKey==0)==
18486 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d  (pCur->pKeyInfo=
18487 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  =0) );..  /* If 
18488 74 68 69 73 20 69 73 20 61 6e 20 69 6e 73 65 72  this is an inser
18489 74 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 62  t into a table b
1848a 2d 74 72 65 65 2c 20 69 6e 76 61 6c 69 64 61 74  -tree, invalidat
1848b 65 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 0a  e any incrblob .
1848c 20 20 2a 2a 20 63 75 72 73 6f 72 73 20 6f 70 65    ** cursors ope
1848d 6e 20 6f 6e 20 74 68 65 20 72 6f 77 20 62 65 69  n on the row bei
1848e 6e 67 20 72 65 70 6c 61 63 65 64 20 28 61 73 73  ng replaced (ass
1848f 75 6d 69 6e 67 20 74 68 69 73 20 69 73 20 61 20  uming this is a 
18490 72 65 70 6c 61 63 65 0a 20 20 2a 2a 20 6f 70 65  replace.  ** ope
18491 72 61 74 69 6f 6e 20 2d 20 69 66 20 69 74 20 69  ration - if it i
18492 73 20 6e 6f 74 2c 20 74 68 65 20 66 6f 6c 6c 6f  s not, the follo
18493 77 69 6e 67 20 69 73 20 61 20 6e 6f 2d 6f 70 29  wing is a no-op)
18494 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 72  .  */.  if( pCur
18495 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 20 29 7b  ->pKeyInfo==0 ){
18496 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 49  .    invalidateI
18497 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 70  ncrblobCursors(p
18498 2c 20 6e 4b 65 79 2c 20 30 29 3b 0a 20 20 7d 0a  , nKey, 0);.  }.
18499 0a 20 20 2f 2a 20 53 61 76 65 20 74 68 65 20 70  .  /* Save the p
1849a 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e 79 20  ositions of any 
1849b 6f 74 68 65 72 20 63 75 72 73 6f 72 73 20 6f 70  other cursors op
1849c 65 6e 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65  en on this table
1849d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 73  ..  **.  ** In s
1849e 6f 6d 65 20 63 61 73 65 73 2c 20 74 68 65 20 63  ome cases, the c
1849f 61 6c 6c 20 74 6f 20 62 74 72 65 65 4d 6f 76 65  all to btreeMove
184a0 74 6f 28 29 20 62 65 6c 6f 77 20 69 73 20 61 20  to() below is a 
184a1 6e 6f 2d 6f 70 2e 20 46 6f 72 0a 20 20 2a 2a 20  no-op. For.  ** 
184a2 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20 69 6e  example, when in
184a3 73 65 72 74 69 6e 67 20 64 61 74 61 20 69 6e 74  serting data int
184a4 6f 20 61 20 74 61 62 6c 65 20 77 69 74 68 20 61  o a table with a
184a5 75 74 6f 2d 67 65 6e 65 72 61 74 65 64 20 69 6e  uto-generated in
184a6 74 65 67 65 72 0a 20 20 2a 2a 20 6b 65 79 73 2c  teger.  ** keys,
184a7 20 74 68 65 20 56 44 42 45 20 6c 61 79 65 72 20   the VDBE layer 
184a8 69 6e 76 6f 6b 65 73 20 73 71 6c 69 74 65 33 42  invokes sqlite3B
184a9 74 72 65 65 4c 61 73 74 28 29 20 74 6f 20 66 69  treeLast() to fi
184aa 67 75 72 65 20 6f 75 74 20 74 68 65 20 0a 20 20  gure out the .  
184ab 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 79 20 74  ** integer key t
184ac 6f 20 75 73 65 2e 20 49 74 20 74 68 65 6e 20 63  o use. It then c
184ad 61 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69  alls this functi
184ae 6f 6e 20 74 6f 20 61 63 74 75 61 6c 6c 79 20 69  on to actually i
184af 6e 73 65 72 74 20 74 68 65 20 0a 20 20 2a 2a 20  nsert the .  ** 
184b0 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 69 6e  data into the in
184b1 74 6b 65 79 20 42 2d 54 72 65 65 2e 20 49 6e 20  tkey B-Tree. In 
184b2 74 68 69 73 20 63 61 73 65 20 62 74 72 65 65 4d  this case btreeM
184b3 6f 76 65 74 6f 28 29 20 72 65 63 6f 67 6e 69 7a  oveto() recogniz
184b4 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65  es.  ** that the
184b5 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65 61   cursor is alrea
184b6 64 79 20 77 68 65 72 65 20 69 74 20 6e 65 65 64  dy where it need
184b7 73 20 74 6f 20 62 65 20 61 6e 64 20 72 65 74 75  s to be and retu
184b8 72 6e 73 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a  rns without.  **
184b9 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f 72 6b 2e   doing any work.
184ba 20 54 6f 20 61 76 6f 69 64 20 74 68 77 61 72 74   To avoid thwart
184bb 69 6e 67 20 74 68 65 73 65 20 6f 70 74 69 6d 69  ing these optimi
184bc 7a 61 74 69 6f 6e 73 2c 20 69 74 20 69 73 20 69  zations, it is i
184bd 6d 70 6f 72 74 61 6e 74 0a 20 20 2a 2a 20 6e 6f  mportant.  ** no
184be 74 20 74 6f 20 63 6c 65 61 72 20 74 68 65 20 63  t to clear the c
184bf 75 72 73 6f 72 20 68 65 72 65 2e 0a 20 20 2a 2f  ursor here..  */
184c0 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43  .  rc = saveAllC
184c1 75 72 73 6f 72 73 28 70 42 74 2c 20 70 43 75 72  ursors(pBt, pCur
184c2 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72  ->pgnoRoot, pCur
184c3 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
184c4 74 75 72 6e 20 72 63 3b 0a 20 20 69 66 28 20 21  turn rc;.  if( !
184c5 6c 6f 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  loc ){.    rc = 
184c6 62 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 72  btreeMoveto(pCur
184c7 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 61 70  , pKey, nKey, ap
184c8 70 65 6e 64 42 69 61 73 2c 20 26 6c 6f 63 29 3b  pendBias, &loc);
184c9 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65  .    if( rc ) re
184ca 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61  turn rc;.  }.  a
184cb 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74  ssert( pCur->eSt
184cc 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
184cd 44 20 7c 7c 20 28 70 43 75 72 2d 3e 65 53 74 61  D || (pCur->eSta
184ce 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c  te==CURSOR_INVAL
184cf 49 44 20 26 26 20 6c 6f 63 29 20 29 3b 0a 0a 20  ID && loc) );.. 
184d0 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61   pPage = pCur->a
184d1 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
184d2 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50  e];.  assert( pP
184d3 61 67 65 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 6e  age->intKey || n
184d4 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65  Key>=0 );.  asse
184d5 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20  rt( pPage->leaf 
184d6 7c 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65  || !pPage->intKe
184d7 79 20 29 3b 0a 0a 20 20 54 52 41 43 45 28 28 22  y );..  TRACE(("
184d8 49 4e 53 45 52 54 3a 20 74 61 62 6c 65 3d 25 64  INSERT: table=%d
184d9 20 6e 6b 65 79 3d 25 6c 6c 64 20 6e 64 61 74 61   nkey=%lld ndata
184da 3d 25 64 20 70 61 67 65 3d 25 64 20 25 73 5c 6e  =%d page=%d %s\n
184db 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 43 75  ",.          pCu
184dc 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 6e 4b 65  r->pgnoRoot, nKe
184dd 79 2c 20 6e 44 61 74 61 2c 20 70 50 61 67 65 2d  y, nData, pPage-
184de 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20  >pgno,.         
184df 20 6c 6f 63 3d 3d 30 20 3f 20 22 6f 76 65 72 77   loc==0 ? "overw
184e0 72 69 74 65 22 20 3a 20 22 6e 65 77 20 65 6e 74  rite" : "new ent
184e1 72 79 22 29 29 3b 0a 20 20 61 73 73 65 72 74 28  ry"));.  assert(
184e2 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29   pPage->isInit )
184e3 3b 0a 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70  ;.  allocateTemp
184e4 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 6e 65  Space(pBt);.  ne
184e5 77 43 65 6c 6c 20 3d 20 70 42 74 2d 3e 70 54 6d  wCell = pBt->pTm
184e6 70 53 70 61 63 65 3b 0a 20 20 69 66 28 20 6e 65  pSpace;.  if( ne
184e7 77 43 65 6c 6c 3d 3d 30 20 29 20 72 65 74 75 72  wCell==0 ) retur
184e8 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
184e9 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e 43 65 6c    rc = fillInCel
184ea 6c 28 70 50 61 67 65 2c 20 6e 65 77 43 65 6c 6c  l(pPage, newCell
184eb 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 70 44  , pKey, nKey, pD
184ec 61 74 61 2c 20 6e 44 61 74 61 2c 20 6e 5a 65 72  ata, nData, nZer
184ed 6f 2c 20 26 73 7a 4e 65 77 29 3b 0a 20 20 69 66  o, &szNew);.  if
184ee 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f  ( rc ) goto end_
184ef 69 6e 73 65 72 74 3b 0a 20 20 61 73 73 65 72 74  insert;.  assert
184f0 28 20 73 7a 4e 65 77 3d 3d 63 65 6c 6c 53 69 7a  ( szNew==cellSiz
184f1 65 50 74 72 28 70 50 61 67 65 2c 20 6e 65 77 43  ePtr(pPage, newC
184f2 65 6c 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74  ell) );.  assert
184f3 28 20 73 7a 4e 65 77 3c 3d 4d 58 5f 43 45 4c 4c  ( szNew<=MX_CELL
184f4 5f 53 49 5a 45 28 70 42 74 29 20 29 3b 0a 20 20  _SIZE(pBt) );.  
184f5 69 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64  idx = pCur->aiId
184f6 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a  x[pCur->iPage];.
184f7 20 20 69 66 28 20 6c 6f 63 3d 3d 30 20 29 7b 0a    if( loc==0 ){.
184f8 20 20 20 20 75 31 36 20 73 7a 4f 6c 64 3b 0a 20      u16 szOld;. 
184f9 20 20 20 61 73 73 65 72 74 28 20 69 64 78 3c 70     assert( idx<p
184fa 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20  Page->nCell );. 
184fb 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
184fc 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d  agerWrite(pPage-
184fd 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69  >pDbPage);.    i
184fe 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 67  f( rc ){.      g
184ff 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a  oto end_insert;.
18500 20 20 20 20 7d 0a 20 20 20 20 6f 6c 64 43 65 6c      }.    oldCel
18501 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61  l = findCell(pPa
18502 67 65 2c 20 69 64 78 29 3b 0a 20 20 20 20 69 66  ge, idx);.    if
18503 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  ( !pPage->leaf )
18504 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 6e  {.      memcpy(n
18505 65 77 43 65 6c 6c 2c 20 6f 6c 64 43 65 6c 6c 2c  ewCell, oldCell,
18506 20 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73   4);.    }.    s
18507 7a 4f 6c 64 20 3d 20 63 65 6c 6c 53 69 7a 65 50  zOld = cellSizeP
18508 74 72 28 70 50 61 67 65 2c 20 6f 6c 64 43 65 6c  tr(pPage, oldCel
18509 6c 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c 65  l);.    rc = cle
1850a 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 6f 6c  arCell(pPage, ol
1850b 64 43 65 6c 6c 29 3b 0a 20 20 20 20 64 72 6f 70  dCell);.    drop
1850c 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c  Cell(pPage, idx,
1850d 20 73 7a 4f 6c 64 2c 20 26 72 63 29 3b 0a 20 20   szOld, &rc);.  
1850e 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20    if( rc ) goto 
1850f 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 7d 65  end_insert;.  }e
18510 6c 73 65 20 69 66 28 20 6c 6f 63 3c 30 20 26 26  lse if( loc<0 &&
18511 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20   pPage->nCell>0 
18512 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
18513 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20  Page->leaf );.  
18514 20 20 69 64 78 20 3d 20 2b 2b 70 43 75 72 2d 3e    idx = ++pCur->
18515 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67  aiIdx[pCur->iPag
18516 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  e];.  }else{.   
18517 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
18518 6c 65 61 66 20 29 3b 0a 20 20 7d 0a 20 20 69 6e  leaf );.  }.  in
18519 73 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20  sertCell(pPage, 
1851a 69 64 78 2c 20 6e 65 77 43 65 6c 6c 2c 20 73 7a  idx, newCell, sz
1851b 4e 65 77 2c 20 30 2c 20 30 2c 20 26 72 63 29 3b  New, 0, 0, &rc);
1851c 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53  .  assert( rc!=S
1851d 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67  QLITE_OK || pPag
1851e 65 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 70 50  e->nCell>0 || pP
1851f 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30  age->nOverflow>0
18520 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 20   );..  /* If no 
18521 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 65  error has occure
18522 64 20 61 6e 64 20 70 50 61 67 65 20 68 61 73 20  d and pPage has 
18523 61 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c  an overflow cell
18524 2c 20 63 61 6c 6c 20 62 61 6c 61 6e 63 65 28 29  , call balance()
18525 20 0a 20 20 2a 2a 20 74 6f 20 72 65 64 69 73 74   .  ** to redist
18526 72 69 62 75 74 65 20 74 68 65 20 63 65 6c 6c 73  ribute the cells
18527 20 77 69 74 68 69 6e 20 74 68 65 20 74 72 65 65   within the tree
18528 2e 20 53 69 6e 63 65 20 62 61 6c 61 6e 63 65 28  . Since balance(
18529 29 20 6d 61 79 20 6d 6f 76 65 0a 20 20 2a 2a 20  ) may move.  ** 
1852a 74 68 65 20 63 75 72 73 6f 72 2c 20 7a 65 72 6f  the cursor, zero
1852b 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e 69 6e   the BtCursor.in
1852c 66 6f 2e 6e 53 69 7a 65 20 61 6e 64 20 42 74 43  fo.nSize and BtC
1852d 75 72 73 6f 72 2e 76 61 6c 69 64 4e 4b 65 79 0a  ursor.validNKey.
1852e 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 73 2e 0a    ** variables..
1852f 20 20 2a 2a 0a 20 20 2a 2a 20 50 72 65 76 69 6f    **.  ** Previo
18530 75 73 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53  us versions of S
18531 51 4c 69 74 65 20 63 61 6c 6c 65 64 20 6d 6f 76  QLite called mov
18532 65 54 6f 52 6f 6f 74 28 29 20 74 6f 20 6d 6f 76  eToRoot() to mov
18533 65 20 74 68 65 20 63 75 72 73 6f 72 0a 20 20 2a  e the cursor.  *
18534 2a 20 62 61 63 6b 20 74 6f 20 74 68 65 20 72 6f  * back to the ro
18535 6f 74 20 70 61 67 65 20 61 73 20 62 61 6c 61 6e  ot page as balan
18536 63 65 28 29 20 75 73 65 64 20 74 6f 20 69 6e 76  ce() used to inv
18537 61 6c 69 64 61 74 65 20 74 68 65 20 63 6f 6e 74  alidate the cont
18538 65 6e 74 73 0a 20 20 2a 2a 20 6f 66 20 42 74 43  ents.  ** of BtC
18539 75 72 73 6f 72 2e 61 70 50 61 67 65 5b 5d 20 61  ursor.apPage[] a
1853a 6e 64 20 42 74 43 75 72 73 6f 72 2e 61 69 49 64  nd BtCursor.aiId
1853b 78 5b 5d 2e 20 49 6e 73 74 65 61 64 20 6f 66 20  x[]. Instead of 
1853c 64 6f 69 6e 67 20 74 68 61 74 2c 0a 20 20 2a 2a  doing that,.  **
1853d 20 73 65 74 20 74 68 65 20 63 75 72 73 6f 72 20   set the cursor 
1853e 73 74 61 74 65 20 74 6f 20 22 69 6e 76 61 6c 69  state to "invali
1853f 64 22 2e 20 54 68 69 73 20 6d 61 6b 65 73 20 63  d". This makes c
18540 6f 6d 6d 6f 6e 20 69 6e 73 65 72 74 20 6f 70 65  ommon insert ope
18541 72 61 74 69 6f 6e 73 0a 20 20 2a 2a 20 73 6c 69  rations.  ** sli
18542 67 68 74 6c 79 20 66 61 73 74 65 72 2e 0a 20 20  ghtly faster..  
18543 2a 2a 0a 20 20 2a 2a 20 54 68 65 72 65 20 69 73  **.  ** There is
18544 20 61 20 73 75 62 74 6c 65 20 62 75 74 20 69 6d   a subtle but im
18545 70 6f 72 74 61 6e 74 20 6f 70 74 69 6d 69 7a 61  portant optimiza
18546 74 69 6f 6e 20 68 65 72 65 20 74 6f 6f 2e 20 57  tion here too. W
18547 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 0a 20 20  hen inserting.  
18548 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 72 65 63 6f  ** multiple reco
18549 72 64 73 20 69 6e 74 6f 20 61 6e 20 69 6e 74 6b  rds into an intk
1854a 65 79 20 62 2d 74 72 65 65 20 75 73 69 6e 67 20  ey b-tree using 
1854b 61 20 73 69 6e 67 6c 65 20 63 75 72 73 6f 72 20  a single cursor 
1854c 28 61 73 20 63 61 6e 0a 20 20 2a 2a 20 68 61 70  (as can.  ** hap
1854d 70 65 6e 20 77 68 69 6c 65 20 70 72 6f 63 65 73  pen while proces
1854e 73 69 6e 67 20 61 6e 20 22 49 4e 53 45 52 54 20  sing an "INSERT 
1854f 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43 54 22  INTO ... SELECT"
18550 20 73 74 61 74 65 6d 65 6e 74 29 2c 20 69 74 0a   statement), it.
18551 20 20 2a 2a 20 69 73 20 61 64 76 61 6e 74 61 67    ** is advantag
18552 65 6f 75 73 20 74 6f 20 6c 65 61 76 65 20 74 68  eous to leave th
18553 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e  e cursor pointin
18554 67 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e  g to the last en
18555 74 72 79 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20  try in.  ** the 
18556 62 2d 74 72 65 65 20 69 66 20 70 6f 73 73 69 62  b-tree if possib
18557 6c 65 2e 20 49 66 20 74 68 65 20 63 75 72 73 6f  le. If the curso
18558 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69  r is left pointi
18559 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74 0a 20  ng to the last. 
1855a 20 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68 65   ** entry in the
1855b 20 74 61 62 6c 65 2c 20 61 6e 64 20 74 68 65 20   table, and the 
1855c 6e 65 78 74 20 72 6f 77 20 69 6e 73 65 72 74 65  next row inserte
1855d 64 20 68 61 73 20 61 6e 20 69 6e 74 65 67 65 72  d has an integer
1855e 20 6b 65 79 0a 20 20 2a 2a 20 6c 61 72 67 65 72   key.  ** larger
1855f 20 74 68 61 6e 20 74 68 65 20 6c 61 72 67 65 73   than the larges
18560 74 20 65 78 69 73 74 69 6e 67 20 6b 65 79 2c 20  t existing key, 
18561 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74  it is possible t
18562 6f 20 69 6e 73 65 72 74 20 74 68 65 0a 20 20 2a  o insert the.  *
18563 2a 20 72 6f 77 20 77 69 74 68 6f 75 74 20 73 65  * row without se
18564 65 6b 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72  eking the cursor
18565 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 61 20  . This can be a 
18566 62 69 67 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  big performance 
18567 62 6f 6f 73 74 2e 0a 20 20 2a 2f 0a 20 20 70 43  boost..  */.  pC
18568 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d  ur->info.nSize =
18569 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69   0;.  pCur->vali
1856a 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66 28  dNKey = 0;.  if(
1856b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
1856c 26 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  & pPage->nOverfl
1856d 6f 77 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 62  ow ){.    rc = b
1856e 61 6c 61 6e 63 65 28 70 43 75 72 29 3b 0a 0a 20  alance(pCur);.. 
1856f 20 20 20 2f 2a 20 4d 75 73 74 20 6d 61 6b 65 20     /* Must make 
18570 73 75 72 65 20 6e 4f 76 65 72 66 6c 6f 77 20 69  sure nOverflow i
18571 73 20 72 65 73 65 74 20 74 6f 20 7a 65 72 6f 20  s reset to zero 
18572 65 76 65 6e 20 69 66 20 74 68 65 20 62 61 6c 61  even if the bala
18573 6e 63 65 28 29 0a 20 20 20 20 2a 2a 20 66 61 69  nce().    ** fai
18574 6c 73 2e 20 49 6e 74 65 72 6e 61 6c 20 64 61 74  ls. Internal dat
18575 61 20 73 74 72 75 63 74 75 72 65 20 63 6f 72 72  a structure corr
18576 75 70 74 69 6f 6e 20 77 69 6c 6c 20 72 65 73 75  uption will resu
18577 6c 74 20 6f 74 68 65 72 77 69 73 65 2e 20 0a 20  lt otherwise. . 
18578 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 73 65 74 20     ** Also, set 
18579 74 68 65 20 63 75 72 73 6f 72 20 73 74 61 74 65  the cursor state
1857a 20 74 6f 20 69 6e 76 61 6c 69 64 2e 20 54 68 69   to invalid. Thi
1857b 73 20 73 74 6f 70 73 20 73 61 76 65 43 75 72 73  s stops saveCurs
1857c 6f 72 50 6f 73 69 74 69 6f 6e 28 29 0a 20 20 20  orPosition().   
1857d 20 2a 2a 20 66 72 6f 6d 20 74 72 79 69 6e 67 20   ** from trying 
1857e 74 6f 20 73 61 76 65 20 74 68 65 20 63 75 72 72  to save the curr
1857f 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20 6f 66 20  ent position of 
18580 74 68 65 20 63 75 72 73 6f 72 2e 20 20 2a 2f 0a  the cursor.  */.
18581 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65      pCur->apPage
18582 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e  [pCur->iPage]->n
18583 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20  Overflow = 0;.  
18584 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d    pCur->eState =
18585 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b   CURSOR_INVALID;
18586 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
18587 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
18588 2d 3e 69 50 61 67 65 5d 2d 3e 6e 4f 76 65 72 66  ->iPage]->nOverf
18589 6c 6f 77 3d 3d 30 20 29 3b 0a 0a 65 6e 64 5f 69  low==0 );..end_i
1858a 6e 73 65 72 74 3a 0a 20 20 72 65 74 75 72 6e 20  nsert:.  return 
1858b 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c  rc;.}../*.** Del
1858c 65 74 65 20 74 68 65 20 65 6e 74 72 79 20 74 68  ete the entry th
1858d 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  at the cursor is
1858e 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54   pointing to.  T
1858f 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20  he cursor.** is 
18590 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74  left pointing at
18591 20 61 20 61 72 62 69 74 72 61 72 79 20 6c 6f 63   a arbitrary loc
18592 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ation..*/.SQLITE
18593 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
18594 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28  ite3BtreeDelete(
18595 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
18596 0a 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 43  .  Btree *p = pC
18597 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 42 74  ur->pBtree;.  Bt
18598 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
18599 3e 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20  >pBt;           
1859a 20 20 20 0a 20 20 69 6e 74 20 72 63 3b 20 20 20     .  int rc;   
1859b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1859c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
1859d 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 4d  turn code */.  M
1859e 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20  emPage *pPage;  
1859f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
185a0 20 20 20 20 2f 2a 20 50 61 67 65 20 74 6f 20 64      /* Page to d
185a1 65 6c 65 74 65 20 63 65 6c 6c 20 66 72 6f 6d 20  elete cell from 
185a2 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
185a3 61 72 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20  ar *pCell;      
185a4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
185a5 6e 74 65 72 20 74 6f 20 63 65 6c 6c 20 74 6f 20  nter to cell to 
185a6 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20  delete */.  int 
185a7 69 43 65 6c 6c 49 64 78 3b 20 20 20 20 20 20 20  iCellIdx;       
185a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
185a9 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 65 6c   /* Index of cel
185aa 6c 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20  l to delete */. 
185ab 20 69 6e 74 20 69 43 65 6c 6c 44 65 70 74 68 3b   int iCellDepth;
185ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
185ad 20 20 20 20 20 20 2f 2a 20 44 65 70 74 68 20 6f        /* Depth o
185ae 66 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e 69 6e  f node containin
185af 67 20 70 43 65 6c 6c 20 2a 2f 20 0a 0a 20 20 61  g pCell */ ..  a
185b0 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
185b1 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
185b2 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e  .  assert( pBt->
185b3 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54  inTransaction==T
185b4 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20  RANS_WRITE );.  
185b5 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 72 65  assert( !pBt->re
185b6 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 61 73 73 65  adOnly );.  asse
185b7 72 74 28 20 70 43 75 72 2d 3e 77 72 46 6c 61 67  rt( pCur->wrFlag
185b8 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68 61   );.  assert( ha
185b9 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  sSharedCacheTabl
185ba 65 4c 6f 63 6b 28 70 2c 20 70 43 75 72 2d 3e 70  eLock(p, pCur->p
185bb 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 2d 3e 70  gnoRoot, pCur->p
185bc 4b 65 79 49 6e 66 6f 21 3d 30 2c 20 32 29 20 29  KeyInfo!=0, 2) )
185bd 3b 0a 20 20 61 73 73 65 72 74 28 20 21 68 61 73  ;.  assert( !has
185be 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c  ReadConflicts(p,
185bf 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 29   pCur->pgnoRoot)
185c0 20 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52   );..  if( NEVER
185c1 28 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75  (pCur->aiIdx[pCu
185c2 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 43 75 72 2d  r->iPage]>=pCur-
185c3 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
185c4 61 67 65 5d 2d 3e 6e 43 65 6c 6c 29 20 0a 20 20  age]->nCell) .  
185c5 20 7c 7c 20 4e 45 56 45 52 28 70 43 75 72 2d 3e   || NEVER(pCur->
185c6 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56  eState!=CURSOR_V
185c7 41 4c 49 44 29 0a 20 20 29 7b 0a 20 20 20 20 72  ALID).  ){.    r
185c8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
185c9 4f 52 3b 20 20 2f 2a 20 53 6f 6d 65 74 68 69 6e  OR;  /* Somethin
185ca 67 20 68 61 73 20 67 6f 6e 65 20 61 77 72 79 2e  g has gone awry.
185cb 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66   */.  }..  /* If
185cc 20 74 68 69 73 20 69 73 20 61 20 64 65 6c 65 74   this is a delet
185cd 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 72  e operation to r
185ce 65 6d 6f 76 65 20 61 20 72 6f 77 20 66 72 6f 6d  emove a row from
185cf 20 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65 2c   a table b-tree,
185d0 0a 20 20 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65  .  ** invalidate
185d1 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 63 75   any incrblob cu
185d2 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68  rsors open on th
185d3 65 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65  e row being dele
185d4 74 65 64 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70  ted.  */.  if( p
185d5 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30  Cur->pKeyInfo==0
185d6 20 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61   ){.    invalida
185d7 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72  teIncrblobCursor
185d8 73 28 70 2c 20 70 43 75 72 2d 3e 69 6e 66 6f 2e  s(p, pCur->info.
185d9 6e 4b 65 79 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20  nKey, 0);.  }.. 
185da 20 69 43 65 6c 6c 44 65 70 74 68 20 3d 20 70 43   iCellDepth = pC
185db 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 69 43 65  ur->iPage;.  iCe
185dc 6c 6c 49 64 78 20 3d 20 70 43 75 72 2d 3e 61 69  llIdx = pCur->ai
185dd 49 64 78 5b 69 43 65 6c 6c 44 65 70 74 68 5d 3b  Idx[iCellDepth];
185de 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d  .  pPage = pCur-
185df 3e 61 70 50 61 67 65 5b 69 43 65 6c 6c 44 65 70  >apPage[iCellDep
185e0 74 68 5d 3b 0a 20 20 70 43 65 6c 6c 20 3d 20 66  th];.  pCell = f
185e1 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69  indCell(pPage, i
185e2 43 65 6c 6c 49 64 78 29 3b 0a 0a 20 20 2f 2a 20  CellIdx);..  /* 
185e3 49 66 20 74 68 65 20 70 61 67 65 20 63 6f 6e 74  If the page cont
185e4 61 69 6e 69 6e 67 20 74 68 65 20 65 6e 74 72 79  aining the entry
185e5 20 74 6f 20 64 65 6c 65 74 65 20 69 73 20 6e 6f   to delete is no
185e6 74 20 61 20 6c 65 61 66 20 70 61 67 65 2c 20 6d  t a leaf page, m
185e7 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 63 75 72  ove.  ** the cur
185e8 73 6f 72 20 74 6f 20 74 68 65 20 6c 61 72 67 65  sor to the large
185e9 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  st entry in the 
185ea 74 72 65 65 20 74 68 61 74 20 69 73 20 73 6d 61  tree that is sma
185eb 6c 6c 65 72 20 74 68 61 6e 0a 20 20 2a 2a 20 74  ller than.  ** t
185ec 68 65 20 65 6e 74 72 79 20 62 65 69 6e 67 20 64  he entry being d
185ed 65 6c 65 74 65 64 2e 20 54 68 69 73 20 63 65 6c  eleted. This cel
185ee 6c 20 77 69 6c 6c 20 72 65 70 6c 61 63 65 20 74  l will replace t
185ef 68 65 20 63 65 6c 6c 20 62 65 69 6e 67 20 64 65  he cell being de
185f0 6c 65 74 65 64 0a 20 20 2a 2a 20 66 72 6f 6d 20  leted.  ** from 
185f1 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64  the internal nod
185f2 65 2e 20 54 68 65 20 27 70 72 65 76 69 6f 75 73  e. The 'previous
185f3 27 20 65 6e 74 72 79 20 69 73 20 75 73 65 64 20  ' entry is used 
185f4 66 6f 72 20 74 68 69 73 20 69 6e 73 74 65 61 64  for this instead
185f5 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 27 6e 65  .  ** of the 'ne
185f6 78 74 27 20 65 6e 74 72 79 2c 20 61 73 20 74 68  xt' entry, as th
185f7 65 20 70 72 65 76 69 6f 75 73 20 65 6e 74 72 79  e previous entry
185f8 20 69 73 20 61 6c 77 61 79 73 20 61 20 70 61 72   is always a par
185f9 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 73 75  t of the.  ** su
185fa 62 2d 74 72 65 65 20 68 65 61 64 65 64 20 62 79  b-tree headed by
185fb 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20   the child page 
185fc 6f 66 20 74 68 65 20 63 65 6c 6c 20 62 65 69 6e  of the cell bein
185fd 67 20 64 65 6c 65 74 65 64 2e 20 54 68 69 73 20  g deleted. This 
185fe 6d 61 6b 65 73 0a 20 20 2a 2a 20 62 61 6c 61 6e  makes.  ** balan
185ff 63 69 6e 67 20 74 68 65 20 74 72 65 65 20 66 6f  cing the tree fo
18600 6c 6c 6f 77 69 6e 67 20 74 68 65 20 64 65 6c 65  llowing the dele
18601 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 65 61 73  te operation eas
18602 69 65 72 2e 20 20 2a 2f 0a 20 20 69 66 28 20 21  ier.  */.  if( !
18603 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20  pPage->leaf ){. 
18604 20 20 20 69 6e 74 20 6e 6f 74 55 73 65 64 3b 0a     int notUsed;.
18605 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
18606 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 70 43  BtreePrevious(pC
18607 75 72 2c 20 26 6e 6f 74 55 73 65 64 29 3b 0a 20  ur, &notUsed);. 
18608 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75     if( rc ) retu
18609 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  rn rc;.  }..  /*
1860a 20 53 61 76 65 20 74 68 65 20 70 6f 73 69 74 69   Save the positi
1860b 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65 72  ons of any other
1860c 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e   cursors open on
1860d 20 74 68 69 73 20 74 61 62 6c 65 20 62 65 66 6f   this table befo
1860e 72 65 0a 20 20 2a 2a 20 6d 61 6b 69 6e 67 20 61  re.  ** making a
1860f 6e 79 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73  ny modifications
18610 2e 20 4d 61 6b 65 20 74 68 65 20 70 61 67 65 20  . Make the page 
18611 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 65  containing the e
18612 6e 74 72 79 20 74 6f 20 62 65 20 0a 20 20 2a 2a  ntry to be .  **
18613 20 64 65 6c 65 74 65 64 20 77 72 69 74 61 62 6c   deleted writabl
18614 65 2e 20 54 68 65 6e 20 66 72 65 65 20 61 6e 79  e. Then free any
18615 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20   overflow pages 
18616 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
18617 74 68 65 20 0a 20 20 2a 2a 20 65 6e 74 72 79 20  the .  ** entry 
18618 61 6e 64 20 66 69 6e 61 6c 6c 79 20 72 65 6d 6f  and finally remo
18619 76 65 20 74 68 65 20 63 65 6c 6c 20 69 74 73 65  ve the cell itse
1861a 6c 66 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74  lf from within t
1861b 68 65 20 70 61 67 65 2e 20 20 0a 20 20 2a 2f 0a  he page.  .  */.
1861c 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75    rc = saveAllCu
1861d 72 73 6f 72 73 28 70 42 74 2c 20 70 43 75 72 2d  rsors(pBt, pCur-
1861e 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29  >pgnoRoot, pCur)
1861f 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74  ;.  if( rc ) ret
18620 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 73  urn rc;.  rc = s
18621 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
18622 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
18623 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74  ;.  if( rc ) ret
18624 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 63  urn rc;.  rc = c
18625 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20  learCell(pPage, 
18626 70 43 65 6c 6c 29 3b 0a 20 20 64 72 6f 70 43 65  pCell);.  dropCe
18627 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 49  ll(pPage, iCellI
18628 64 78 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28  dx, cellSizePtr(
18629 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 2c 20 26  pPage, pCell), &
1862a 72 63 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20  rc);.  if( rc ) 
1862b 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a  return rc;..  /*
1862c 20 49 66 20 74 68 65 20 63 65 6c 6c 20 64 65 6c   If the cell del
1862d 65 74 65 64 20 77 61 73 20 6e 6f 74 20 6c 6f 63  eted was not loc
1862e 61 74 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 70  ated on a leaf p
1862f 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 63 75  age, then the cu
18630 72 73 6f 72 0a 20 20 2a 2a 20 69 73 20 63 75 72  rsor.  ** is cur
18631 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20  rently pointing 
18632 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 65  to the largest e
18633 6e 74 72 79 20 69 6e 20 74 68 65 20 73 75 62 2d  ntry in the sub-
18634 74 72 65 65 20 68 65 61 64 65 64 0a 20 20 2a 2a  tree headed.  **
18635 20 62 79 20 74 68 65 20 63 68 69 6c 64 2d 70 61   by the child-pa
18636 67 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74  ge of the cell t
18637 68 61 74 20 77 61 73 20 6a 75 73 74 20 64 65 6c  hat was just del
18638 65 74 65 64 20 66 72 6f 6d 20 61 6e 20 69 6e 74  eted from an int
18639 65 72 6e 61 6c 0a 20 20 2a 2a 20 6e 6f 64 65 2e  ernal.  ** node.
1863a 20 54 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 74   The cell from t
1863b 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 6e 65 65  he leaf node nee
1863c 64 73 20 74 6f 20 62 65 20 6d 6f 76 65 64 20 74  ds to be moved t
1863d 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 20  o the internal. 
1863e 20 2a 2a 20 6e 6f 64 65 20 74 6f 20 72 65 70 6c   ** node to repl
1863f 61 63 65 20 74 68 65 20 64 65 6c 65 74 65 64 20  ace the deleted 
18640 63 65 6c 6c 2e 20 20 2a 2f 0a 20 20 69 66 28 20  cell.  */.  if( 
18641 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
18642 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c 65      MemPage *pLe
18643 61 66 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  af = pCur->apPag
18644 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a  e[pCur->iPage];.
18645 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20      int nCell;. 
18646 20 20 20 50 67 6e 6f 20 6e 20 3d 20 70 43 75 72     Pgno n = pCur
18647 2d 3e 61 70 50 61 67 65 5b 69 43 65 6c 6c 44 65  ->apPage[iCellDe
18648 70 74 68 2b 31 5d 2d 3e 70 67 6e 6f 3b 0a 20 20  pth+1]->pgno;.  
18649 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
1864a 2a 70 54 6d 70 3b 0a 0a 20 20 20 20 70 43 65 6c  *pTmp;..    pCel
1864b 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 4c 65  l = findCell(pLe
1864c 61 66 2c 20 70 4c 65 61 66 2d 3e 6e 43 65 6c 6c  af, pLeaf->nCell
1864d 2d 31 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d  -1);.    nCell =
1864e 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4c 65   cellSizePtr(pLe
1864f 61 66 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20  af, pCell);.    
18650 61 73 73 65 72 74 28 20 4d 58 5f 43 45 4c 4c 5f  assert( MX_CELL_
18651 53 49 5a 45 28 70 42 74 29 3e 3d 6e 43 65 6c 6c  SIZE(pBt)>=nCell
18652 20 29 3b 0a 0a 20 20 20 20 61 6c 6c 6f 63 61 74   );..    allocat
18653 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b  eTempSpace(pBt);
18654 0a 20 20 20 20 70 54 6d 70 20 3d 20 70 42 74 2d  .    pTmp = pBt-
18655 3e 70 54 6d 70 53 70 61 63 65 3b 0a 0a 20 20 20  >pTmpSpace;..   
18656 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
18657 65 72 57 72 69 74 65 28 70 4c 65 61 66 2d 3e 70  erWrite(pLeaf->p
18658 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 6e 73  DbPage);.    ins
18659 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20 69  ertCell(pPage, i
1865a 43 65 6c 6c 49 64 78 2c 20 70 43 65 6c 6c 2d 34  CellIdx, pCell-4
1865b 2c 20 6e 43 65 6c 6c 2b 34 2c 20 70 54 6d 70 2c  , nCell+4, pTmp,
1865c 20 6e 2c 20 26 72 63 29 3b 0a 20 20 20 20 64 72   n, &rc);.    dr
1865d 6f 70 43 65 6c 6c 28 70 4c 65 61 66 2c 20 70 4c  opCell(pLeaf, pL
1865e 65 61 66 2d 3e 6e 43 65 6c 6c 2d 31 2c 20 6e 43  eaf->nCell-1, nC
1865f 65 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20 20 69  ell, &rc);.    i
18660 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
18661 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 61 6c  c;.  }..  /* Bal
18662 61 6e 63 65 20 74 68 65 20 74 72 65 65 2e 20 49  ance the tree. I
18663 66 20 74 68 65 20 65 6e 74 72 79 20 64 65 6c 65  f the entry dele
18664 74 65 64 20 77 61 73 20 6c 6f 63 61 74 65 64 20  ted was located 
18665 6f 6e 20 61 20 6c 65 61 66 20 70 61 67 65 2c 0a  on a leaf page,.
18666 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 75    ** then the cu
18667 72 73 6f 72 20 73 74 69 6c 6c 20 70 6f 69 6e 74  rsor still point
18668 73 20 74 6f 20 74 68 61 74 20 70 61 67 65 2e 20  s to that page. 
18669 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
1866a 20 66 69 72 73 74 0a 20 20 2a 2a 20 63 61 6c 6c   first.  ** call
1866b 20 74 6f 20 62 61 6c 61 6e 63 65 28 29 20 72 65   to balance() re
1866c 70 61 69 72 73 20 74 68 65 20 74 72 65 65 2c 20  pairs the tree, 
1866d 61 6e 64 20 74 68 65 20 69 66 28 2e 2e 2e 29 20  and the if(...) 
1866e 63 6f 6e 64 69 74 69 6f 6e 20 69 73 0a 20 20 2a  condition is.  *
1866f 2a 20 6e 65 76 65 72 20 74 72 75 65 2e 0a 20 20  * never true..  
18670 2a 2a 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69 73  **.  ** Otherwis
18671 65 2c 20 69 66 20 74 68 65 20 65 6e 74 72 79 20  e, if the entry 
18672 64 65 6c 65 74 65 64 20 77 61 73 20 6f 6e 20 61  deleted was on a
18673 6e 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20  n internal node 
18674 70 61 67 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20  page, then.  ** 
18675 70 43 75 72 20 69 73 20 70 6f 69 6e 74 69 6e 67  pCur is pointing
18676 20 74 6f 20 74 68 65 20 6c 65 61 66 20 70 61 67   to the leaf pag
18677 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61 20 63  e from which a c
18678 65 6c 6c 20 77 61 73 20 72 65 6d 6f 76 65 64 20  ell was removed 
18679 74 6f 0a 20 20 2a 2a 20 72 65 70 6c 61 63 65 20  to.  ** replace 
1867a 74 68 65 20 63 65 6c 6c 20 64 65 6c 65 74 65 64  the cell deleted
1867b 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e   from the intern
1867c 61 6c 20 6e 6f 64 65 2e 20 54 68 69 73 20 69 73  al node. This is
1867d 20 73 6c 69 67 68 74 6c 79 0a 20 20 2a 2a 20 74   slightly.  ** t
1867e 72 69 63 6b 79 20 61 73 20 74 68 65 20 6c 65 61  ricky as the lea
1867f 66 20 6e 6f 64 65 20 6d 61 79 20 62 65 20 75 6e  f node may be un
18680 64 65 72 66 75 6c 6c 2c 20 61 6e 64 20 74 68 65  derfull, and the
18681 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 6d   internal node m
18682 61 79 0a 20 20 2a 2a 20 62 65 20 65 69 74 68 65  ay.  ** be eithe
18683 72 20 75 6e 64 65 72 20 6f 72 20 6f 76 65 72 66  r under or overf
18684 75 6c 6c 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ull. In this cas
18685 65 20 72 75 6e 20 74 68 65 20 62 61 6c 61 6e 63  e run the balanc
18686 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 0a 20 20  ing algorithm.  
18687 2a 2a 20 6f 6e 20 74 68 65 20 6c 65 61 66 20 6e  ** on the leaf n
18688 6f 64 65 20 66 69 72 73 74 2e 20 49 66 20 74 68  ode first. If th
18689 65 20 62 61 6c 61 6e 63 65 20 70 72 6f 63 65 65  e balance procee
1868a 64 73 20 66 61 72 20 65 6e 6f 75 67 68 20 75 70  ds far enough up
1868b 20 74 68 65 0a 20 20 2a 2a 20 74 72 65 65 20 74   the.  ** tree t
1868c 68 61 74 20 77 65 20 63 61 6e 20 62 65 20 73 75  hat we can be su
1868d 72 65 20 74 68 61 74 20 61 6e 79 20 70 72 6f 62  re that any prob
1868e 6c 65 6d 20 69 6e 20 74 68 65 20 69 6e 74 65 72  lem in the inter
1868f 6e 61 6c 20 6e 6f 64 65 20 68 61 73 0a 20 20 2a  nal node has.  *
18690 2a 20 62 65 65 6e 20 63 6f 72 72 65 63 74 65 64  * been corrected
18691 2c 20 73 6f 20 62 65 20 69 74 2e 20 4f 74 68 65  , so be it. Othe
18692 72 77 69 73 65 2c 20 61 66 74 65 72 20 62 61 6c  rwise, after bal
18693 61 6e 63 69 6e 67 20 74 68 65 20 6c 65 61 66 20  ancing the leaf 
18694 6e 6f 64 65 2c 0a 20 20 2a 2a 20 77 61 6c 6b 20  node,.  ** walk 
18695 74 68 65 20 63 75 72 73 6f 72 20 75 70 20 74 68  the cursor up th
18696 65 20 74 72 65 65 20 74 6f 20 74 68 65 20 69 6e  e tree to the in
18697 74 65 72 6e 61 6c 20 6e 6f 64 65 20 61 6e 64 20  ternal node and 
18698 62 61 6c 61 6e 63 65 20 69 74 20 61 73 20 0a 20  balance it as . 
18699 20 2a 2a 20 77 65 6c 6c 2e 20 20 2a 2f 0a 20 20   ** well.  */.  
1869a 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75  rc = balance(pCu
1869b 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  r);.  if( rc==SQ
1869c 4c 49 54 45 5f 4f 4b 20 26 26 20 70 43 75 72 2d  LITE_OK && pCur-
1869d 3e 69 50 61 67 65 3e 69 43 65 6c 6c 44 65 70 74  >iPage>iCellDept
1869e 68 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20  h ){.    while( 
1869f 70 43 75 72 2d 3e 69 50 61 67 65 3e 69 43 65 6c  pCur->iPage>iCel
186a0 6c 44 65 70 74 68 20 29 7b 0a 20 20 20 20 20 20  lDepth ){.      
186a1 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72  releasePage(pCur
186a2 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
186a3 50 61 67 65 2d 2d 5d 29 3b 0a 20 20 20 20 7d 0a  Page--]);.    }.
186a4 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65      rc = balance
186a5 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 20 69  (pCur);.  }..  i
186a6 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
186a7 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f 52 6f   ){.    moveToRo
186a8 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a 20 20  ot(pCur);.  }.  
186a9 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
186aa 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
186ab 20 42 54 72 65 65 20 74 61 62 6c 65 2e 20 20 57   BTree table.  W
186ac 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 54 61 62  rite into *piTab
186ad 6c 65 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e  le the page.** n
186ae 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72 6f  umber for the ro
186af 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6e  ot page of the n
186b0 65 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ew table..**.** 
186b1 54 68 65 20 74 79 70 65 20 6f 66 20 74 79 70 65  The type of type
186b2 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62   is determined b
186b3 79 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61  y the flags para
186b4 6d 65 74 65 72 2e 20 20 4f 6e 6c 79 20 74 68 65  meter.  Only the
186b5 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61  .** following va
186b6 6c 75 65 73 20 6f 66 20 66 6c 61 67 73 20 61 72  lues of flags ar
186b7 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75  e currently in u
186b8 73 65 2e 20 20 4f 74 68 65 72 20 76 61 6c 75 65  se.  Other value
186b9 73 20 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 20 6d  s for.** flags m
186ba 69 67 68 74 20 6e 6f 74 20 77 6f 72 6b 3a 0a 2a  ight not work:.*
186bb 2a 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 5f 49  *.**     BTREE_I
186bc 4e 54 4b 45 59 7c 42 54 52 45 45 5f 4c 45 41 46  NTKEY|BTREE_LEAF
186bd 44 41 54 41 20 20 20 20 20 55 73 65 64 20 66 6f  DATA     Used fo
186be 72 20 53 51 4c 20 74 61 62 6c 65 73 20 77 69 74  r SQL tables wit
186bf 68 20 72 6f 77 69 64 20 6b 65 79 73 0a 2a 2a 20  h rowid keys.** 
186c0 20 20 20 20 42 54 52 45 45 5f 5a 45 52 4f 44 41      BTREE_ZERODA
186c1 54 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TA              
186c2 20 20 20 20 55 73 65 64 20 66 6f 72 20 53 51 4c      Used for SQL
186c3 20 69 6e 64 69 63 65 73 0a 2a 2f 0a 73 74 61 74   indices.*/.stat
186c4 69 63 20 69 6e 74 20 62 74 72 65 65 43 72 65 61  ic int btreeCrea
186c5 74 65 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70  teTable(Btree *p
186c6 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 2c 20  , int *piTable, 
186c7 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 42 74  int flags){.  Bt
186c8 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
186c9 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20  >pBt;.  MemPage 
186ca 2a 70 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f 20 70  *pRoot;.  Pgno p
186cb 67 6e 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72  gnoRoot;.  int r
186cc 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  c;..  assert( sq
186cd 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d  lite3BtreeHoldsM
186ce 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73  utex(p) );.  ass
186cf 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e  ert( pBt->inTran
186d0 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57  saction==TRANS_W
186d1 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74  RITE );.  assert
186d2 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79  ( !pBt->readOnly
186d3 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   );..#ifdef SQLI
186d4 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
186d5 55 4d 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61  UM.  rc = alloca
186d6 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c  teBtreePage(pBt,
186d7 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f   &pRoot, &pgnoRo
186d8 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 69 66 28  ot, 1, 0);.  if(
186d9 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72   rc ){.    retur
186da 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a  n rc;.  }.#else.
186db 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56    if( pBt->autoV
186dc 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e  acuum ){.    Pgn
186dd 6f 20 70 67 6e 6f 4d 6f 76 65 3b 20 20 20 20 20  o pgnoMove;     
186de 20 2f 2a 20 4d 6f 76 65 20 61 20 70 61 67 65 20   /* Move a page 
186df 68 65 72 65 20 74 6f 20 6d 61 6b 65 20 72 6f 6f  here to make roo
186e0 6d 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70  m for the root-p
186e1 61 67 65 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61  age */.    MemPa
186e2 67 65 20 2a 70 50 61 67 65 4d 6f 76 65 3b 20 2f  ge *pPageMove; /
186e3 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20 6d 6f  * The page to mo
186e4 76 65 20 74 6f 2e 20 2a 2f 0a 0a 20 20 20 20 2f  ve to. */..    /
186e5 2a 20 43 72 65 61 74 69 6e 67 20 61 20 6e 65 77  * Creating a new
186e6 20 74 61 62 6c 65 20 6d 61 79 20 70 72 6f 62 61   table may proba
186e7 62 6c 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69  bly require movi
186e8 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67 20 64  ng an existing d
186e9 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 74  atabase.    ** t
186ea 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f 72 20  o make room for 
186eb 74 68 65 20 6e 65 77 20 74 61 62 6c 65 73 20 72  the new tables r
186ec 6f 6f 74 20 70 61 67 65 2e 20 49 6e 20 63 61 73  oot page. In cas
186ed 65 20 74 68 69 73 20 70 61 67 65 20 74 75 72 6e  e this page turn
186ee 73 0a 20 20 20 20 2a 2a 20 6f 75 74 20 74 6f 20  s.    ** out to 
186ef 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70  be an overflow p
186f0 61 67 65 2c 20 64 65 6c 65 74 65 20 61 6c 6c 20  age, delete all 
186f1 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6d 61  overflow page-ma
186f2 70 20 63 61 63 68 65 73 0a 20 20 20 20 2a 2a 20  p caches.    ** 
186f3 68 65 6c 64 20 62 79 20 6f 70 65 6e 20 63 75 72  held by open cur
186f4 73 6f 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  sors..    */.   
186f5 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76   invalidateAllOv
186f6 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29  erflowCache(pBt)
186f7 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74  ;..    /* Read t
186f8 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 74 61  he value of meta
186f9 5b 33 5d 20 66 72 6f 6d 20 74 68 65 20 64 61 74  [3] from the dat
186fa 61 62 61 73 65 20 74 6f 20 64 65 74 65 72 6d 69  abase to determi
186fb 6e 65 20 77 68 65 72 65 20 74 68 65 0a 20 20 20  ne where the.   
186fc 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66   ** root page of
186fd 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 73   the new table s
186fe 68 6f 75 6c 64 20 67 6f 2e 20 6d 65 74 61 5b 33  hould go. meta[3
186ff 5d 20 69 73 20 74 68 65 20 6c 61 72 67 65 73 74  ] is the largest
18700 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a   root-page.    *
18701 2a 20 63 72 65 61 74 65 64 20 73 6f 20 66 61 72  * created so far
18702 2c 20 73 6f 20 74 68 65 20 6e 65 77 20 72 6f 6f  , so the new roo
18703 74 2d 70 61 67 65 20 69 73 20 28 6d 65 74 61 5b  t-page is (meta[
18704 33 5d 2b 31 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  3]+1)..    */.  
18705 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65    sqlite3BtreeGe
18706 74 4d 65 74 61 28 70 2c 20 42 54 52 45 45 5f 4c  tMeta(p, BTREE_L
18707 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45  ARGEST_ROOT_PAGE
18708 2c 20 26 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20  , &pgnoRoot);.  
18709 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 0a 20    pgnoRoot++;.. 
1870a 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f     /* The new ro
1870b 6f 74 2d 70 61 67 65 20 6d 61 79 20 6e 6f 74 20  ot-page may not 
1870c 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20  be allocated on 
1870d 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61  a pointer-map pa
1870e 67 65 2c 20 6f 72 20 74 68 65 0a 20 20 20 20 2a  ge, or the.    *
1870f 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 70  * PENDING_BYTE p
18710 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  age..    */.    
18711 77 68 69 6c 65 28 20 70 67 6e 6f 52 6f 6f 74 3d  while( pgnoRoot=
18712 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70  =PTRMAP_PAGENO(p
18713 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 29 20 7c 7c  Bt, pgnoRoot) ||
18714 0a 20 20 20 20 20 20 20 20 70 67 6e 6f 52 6f 6f  .        pgnoRoo
18715 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  t==PENDING_BYTE_
18716 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20  PAGE(pBt) ){.   
18717 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 20     pgnoRoot++;. 
18718 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
18719 20 70 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29 3b 0a   pgnoRoot>=3 );.
1871a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  .    /* Allocate
1871b 20 61 20 70 61 67 65 2e 20 54 68 65 20 70 61 67   a page. The pag
1871c 65 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79  e that currently
1871d 20 72 65 73 69 64 65 73 20 61 74 20 70 67 6e 6f   resides at pgno
1871e 52 6f 6f 74 20 77 69 6c 6c 0a 20 20 20 20 2a 2a  Root will.    **
1871f 20 62 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65   be moved to the
18720 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20   allocated page 
18721 28 75 6e 6c 65 73 73 20 74 68 65 20 61 6c 6c 6f  (unless the allo
18722 63 61 74 65 64 20 70 61 67 65 20 68 61 70 70 65  cated page happe
18723 6e 73 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65 73  ns.    ** to res
18724 69 64 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 29  ide at pgnoRoot)
18725 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20  ..    */.    rc 
18726 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50  = allocateBtreeP
18727 61 67 65 28 70 42 74 2c 20 26 70 50 61 67 65 4d  age(pBt, &pPageM
18728 6f 76 65 2c 20 26 70 67 6e 6f 4d 6f 76 65 2c 20  ove, &pgnoMove, 
18729 70 67 6e 6f 52 6f 6f 74 2c 20 31 29 3b 0a 20 20  pgnoRoot, 1);.  
1872a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1872b 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74  _OK ){.      ret
1872c 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20  urn rc;.    }.. 
1872d 20 20 20 69 66 28 20 70 67 6e 6f 4d 6f 76 65 21     if( pgnoMove!
1872e 3d 70 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20 20 20  =pgnoRoot ){.   
1872f 20 20 20 2f 2a 20 70 67 6e 6f 52 6f 6f 74 20 69     /* pgnoRoot i
18730 73 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20  s the page that 
18731 77 69 6c 6c 20 62 65 20 75 73 65 64 20 66 6f 72  will be used for
18732 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f   the root-page o
18733 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e  f.      ** the n
18734 65 77 20 74 61 62 6c 65 20 28 61 73 73 75 6d 69  ew table (assumi
18735 6e 67 20 61 6e 20 65 72 72 6f 72 20 64 69 64 20  ng an error did 
18736 6e 6f 74 20 6f 63 63 75 72 29 2e 20 42 75 74 20  not occur). But 
18737 77 65 20 77 65 72 65 0a 20 20 20 20 20 20 2a 2a  we were.      **
18738 20 61 6c 6c 6f 63 61 74 65 64 20 70 67 6e 6f 4d   allocated pgnoM
18739 6f 76 65 2e 20 49 66 20 72 65 71 75 69 72 65 64  ove. If required
1873a 20 28 69 2e 65 2e 20 69 66 20 69 74 20 77 61 73   (i.e. if it was
1873b 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 64 0a 20   not allocated. 
1873c 20 20 20 20 20 2a 2a 20 62 79 20 65 78 74 65 6e       ** by exten
1873d 64 69 6e 67 20 74 68 65 20 66 69 6c 65 29 2c 20  ding the file), 
1873e 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65  the current page
1873f 20 61 74 20 70 6f 73 69 74 69 6f 6e 20 70 67 6e   at position pgn
18740 6f 4d 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 69  oMove.      ** i
18741 73 20 61 6c 72 65 61 64 79 20 6a 6f 75 72 6e 61  s already journa
18742 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  led..      */.  
18743 20 20 20 20 75 38 20 65 54 79 70 65 20 3d 20 30      u8 eType = 0
18744 3b 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 50 74  ;.      Pgno iPt
18745 72 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 20 20  rPage = 0;..    
18746 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
18747 61 67 65 4d 6f 76 65 29 3b 0a 0a 20 20 20 20 20  ageMove);..     
18748 20 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 67   /* Move the pag
18749 65 20 63 75 72 72 65 6e 74 6c 79 20 61 74 20 70  e currently at p
1874a 67 6e 6f 52 6f 6f 74 20 74 6f 20 70 67 6e 6f 4d  gnoRoot to pgnoM
1874b 6f 76 65 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63  ove. */.      rc
1874c 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28   = btreeGetPage(
1874d 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 26  pBt, pgnoRoot, &
1874e 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 20 20  pRoot, 0);.     
1874f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
18750 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  OK ){.        re
18751 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
18752 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d  .      rc = ptrm
18753 61 70 47 65 74 28 70 42 74 2c 20 70 67 6e 6f 52  apGet(pBt, pgnoR
18754 6f 6f 74 2c 20 26 65 54 79 70 65 2c 20 26 69 50  oot, &eType, &iP
18755 74 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69  trPage);.      i
18756 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50  f( eType==PTRMAP
18757 5f 52 4f 4f 54 50 41 47 45 20 7c 7c 20 65 54 79  _ROOTPAGE || eTy
18758 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50  pe==PTRMAP_FREEP
18759 41 47 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72  AGE ){.        r
1875a 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  c = SQLITE_CORRU
1875b 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d  PT_BKPT;.      }
1875c 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
1875d 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1875e 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
1875f 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20 20  pRoot);.        
18760 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
18761 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   }.      assert(
18762 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 52   eType!=PTRMAP_R
18763 4f 4f 54 50 41 47 45 20 29 3b 0a 20 20 20 20 20  OOTPAGE );.     
18764 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21 3d   assert( eType!=
18765 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20  PTRMAP_FREEPAGE 
18766 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 65  );.      rc = re
18767 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c 20  locatePage(pBt, 
18768 70 52 6f 6f 74 2c 20 65 54 79 70 65 2c 20 69 50  pRoot, eType, iP
18769 74 72 50 61 67 65 2c 20 70 67 6e 6f 4d 6f 76 65  trPage, pgnoMove
1876a 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 65 6c 65  , 0);.      rele
1876b 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a  asePage(pRoot);.
1876c 0a 20 20 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e  .      /* Obtain
1876d 20 74 68 65 20 70 61 67 65 20 61 74 20 70 67 6e   the page at pgn
1876e 6f 52 6f 6f 74 20 2a 2f 0a 20 20 20 20 20 20 69  oRoot */.      i
1876f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
18770 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
18771 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20  rn rc;.      }. 
18772 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47       rc = btreeG
18773 65 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f  etPage(pBt, pgno
18774 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 30 29  Root, &pRoot, 0)
18775 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
18776 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
18777 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
18778 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63        }.      rc
18779 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
1877a 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50  rite(pRoot->pDbP
1877b 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  age);.      if( 
1877c 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1877d 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
1877e 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20  Page(pRoot);.   
1877f 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
18780 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
18781 65 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 20 3d  e{.      pRoot =
18782 20 70 50 61 67 65 4d 6f 76 65 3b 0a 20 20 20 20   pPageMove;.    
18783 7d 20 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 74  } ..    /* Updat
18784 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  e the pointer-ma
18785 70 20 61 6e 64 20 6d 65 74 61 2d 64 61 74 61 20  p and meta-data 
18786 77 69 74 68 20 74 68 65 20 6e 65 77 20 72 6f 6f  with the new roo
18787 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 2a  t-page number. *
18788 2f 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 28  /.    ptrmapPut(
18789 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 50  pBt, pgnoRoot, P
1878a 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20  TRMAP_ROOTPAGE, 
1878b 30 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28  0, &rc);.    if(
1878c 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c   rc ){.      rel
1878d 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b  easePage(pRoot);
1878e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
1878f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
18790 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64   sqlite3BtreeUpd
18791 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 70 67  ateMeta(p, 4, pg
18792 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69 66 28  noRoot);.    if(
18793 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c   rc ){.      rel
18794 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b  easePage(pRoot);
18795 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
18796 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c 73 65  ;.    }..  }else
18797 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63  {.    rc = alloc
18798 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74  ateBtreePage(pBt
18799 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52  , &pRoot, &pgnoR
1879a 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20  oot, 1, 0);.    
1879b 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
1879c 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  rc;.  }.#endif. 
1879d 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1879e 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
1879f 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29  (pRoot->pDbPage)
187a0 20 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 70   );.  zeroPage(p
187a1 52 6f 6f 74 2c 20 66 6c 61 67 73 20 7c 20 50 54  Root, flags | PT
187a2 46 5f 4c 45 41 46 29 3b 0a 20 20 73 71 6c 69 74  F_LEAF);.  sqlit
187a3 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 52 6f  e3PagerUnref(pRo
187a4 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  ot->pDbPage);.  
187a5 2a 70 69 54 61 62 6c 65 20 3d 20 28 69 6e 74 29  *piTable = (int)
187a6 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65 74 75  pgnoRoot;.  retu
187a7 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
187a8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
187a9 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
187aa 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65  reateTable(Btree
187ab 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c   *p, int *piTabl
187ac 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20  e, int flags){. 
187ad 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74   int rc;.  sqlit
187ae 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
187af 0a 20 20 72 63 20 3d 20 62 74 72 65 65 43 72 65  .  rc = btreeCre
187b0 61 74 65 54 61 62 6c 65 28 70 2c 20 70 69 54 61  ateTable(p, piTa
187b1 62 6c 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 73  ble, flags);.  s
187b2 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
187b3 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
187b4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65  ;.}../*.** Erase
187b5 20 74 68 65 20 67 69 76 65 6e 20 64 61 74 61 62   the given datab
187b6 61 73 65 20 70 61 67 65 20 61 6e 64 20 61 6c 6c  ase page and all
187b7 20 69 74 73 20 63 68 69 6c 64 72 65 6e 2e 20 20   its children.  
187b8 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 70 61  Return.** the pa
187b9 67 65 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69  ge to the freeli
187ba 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  st..*/.static in
187bb 74 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 50  t clearDatabaseP
187bc 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20  age(.  BtShared 
187bd 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 20 20  *pBt,           
187be 2f 2a 20 54 68 65 20 42 54 72 65 65 20 74 68 61  /* The BTree tha
187bf 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74  t contains the t
187c0 61 62 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70  able */.  Pgno p
187c1 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20  gno,            
187c2 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74  /* Page number t
187c3 6f 20 63 6c 65 61 72 20 2a 2f 0a 20 20 69 6e 74  o clear */.  int
187c4 20 66 72 65 65 50 61 67 65 46 6c 61 67 2c 20 20   freePageFlag,  
187c5 20 20 20 2f 2a 20 44 65 61 6c 6c 6f 63 61 74 65     /* Deallocate
187c6 20 70 61 67 65 20 69 66 20 74 72 75 65 20 2a 2f   page if true */
187c7 0a 20 20 69 6e 74 20 2a 70 6e 43 68 61 6e 67 65  .  int *pnChange
187c8 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  .){.  MemPage *p
187c9 50 61 67 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  Page;.  int rc;.
187ca 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
187cb 2a 70 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 69 3b  *pCell;.  int i;
187cc 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
187cd 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
187ce 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
187cf 69 66 28 20 70 67 6e 6f 3e 70 61 67 65 72 50 61  if( pgno>pagerPa
187d0 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a  gecount(pBt) ){.
187d1 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
187d2 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
187d3 20 20 7d 0a 0a 20 20 72 63 20 3d 20 67 65 74 41    }..  rc = getA
187d4 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20  ndInitPage(pBt, 
187d5 70 67 6e 6f 2c 20 26 70 50 61 67 65 29 3b 0a 20  pgno, &pPage);. 
187d6 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
187d7 20 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20   rc;.  for(i=0; 
187d8 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20  i<pPage->nCell; 
187d9 69 2b 2b 29 7b 0a 20 20 20 20 70 43 65 6c 6c 20  i++){.    pCell 
187da 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65  = findCell(pPage
187db 2c 20 69 29 3b 0a 20 20 20 20 69 66 28 20 21 70  , i);.    if( !p
187dc 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  Page->leaf ){.  
187dd 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44 61      rc = clearDa
187de 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c 20  tabasePage(pBt, 
187df 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 2c  get4byte(pCell),
187e0 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a 20   1, pnChange);. 
187e1 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f       if( rc ) go
187e2 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73 65  to cleardatabase
187e3 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  page_out;.    }.
187e4 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 43 65      rc = clearCe
187e5 6c 6c 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29  ll(pPage, pCell)
187e6 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67  ;.    if( rc ) g
187e7 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73  oto cleardatabas
187e8 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20  epage_out;.  }. 
187e9 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61   if( !pPage->lea
187ea 66 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 63 6c  f ){.    rc = cl
187eb 65 61 72 44 61 74 61 62 61 73 65 50 61 67 65 28  earDatabasePage(
187ec 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 26 70  pBt, get4byte(&p
187ed 50 61 67 65 2d 3e 61 44 61 74 61 5b 38 5d 29 2c  Page->aData[8]),
187ee 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a 20   1, pnChange);. 
187ef 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f     if( rc ) goto
187f0 20 63 6c 65 61 72 64 61 74 61 62 61 73 65 70 61   cleardatabasepa
187f1 67 65 5f 6f 75 74 3b 0a 20 20 7d 65 6c 73 65 20  ge_out;.  }else 
187f2 69 66 28 20 70 6e 43 68 61 6e 67 65 20 29 7b 0a  if( pnChange ){.
187f3 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
187f4 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 20 20  e->intKey );.   
187f5 20 2a 70 6e 43 68 61 6e 67 65 20 2b 3d 20 70 50   *pnChange += pP
187f6 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 7d 0a  age->nCell;.  }.
187f7 20 20 69 66 28 20 66 72 65 65 50 61 67 65 46 6c    if( freePageFl
187f8 61 67 20 29 7b 0a 20 20 20 20 66 72 65 65 50 61  ag ){.    freePa
187f9 67 65 28 70 50 61 67 65 2c 20 26 72 63 29 3b 0a  ge(pPage, &rc);.
187fa 20 20 7d 65 6c 73 65 20 69 66 28 20 28 72 63 20    }else if( (rc 
187fb 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
187fc 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ite(pPage->pDbPa
187fd 67 65 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 7a  ge))==0 ){.    z
187fe 65 72 6f 50 61 67 65 28 70 50 61 67 65 2c 20 70  eroPage(pPage, p
187ff 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 20 7c  Page->aData[0] |
18800 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 7d 0a   PTF_LEAF);.  }.
18801 0a 63 6c 65 61 72 64 61 74 61 62 61 73 65 70 61  .cleardatabasepa
18802 67 65 5f 6f 75 74 3a 0a 20 20 72 65 6c 65 61 73  ge_out:.  releas
18803 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
18804 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
18805 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 69  .** Delete all i
18806 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20  nformation from 
18807 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 69  a single table i
18808 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  n the database. 
18809 20 69 54 61 62 6c 65 20 69 73 0a 2a 2a 20 74 68   iTable is.** th
1880a 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66  e page number of
1880b 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65   the root of the
1880c 20 74 61 62 6c 65 2e 20 20 41 66 74 65 72 20 74   table.  After t
1880d 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
1880e 72 6e 73 2c 0a 2a 2a 20 74 68 65 20 72 6f 6f 74  rns,.** the root
1880f 20 70 61 67 65 20 69 73 20 65 6d 70 74 79 2c 20   page is empty, 
18810 62 75 74 20 73 74 69 6c 6c 20 65 78 69 73 74 73  but still exists
18811 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
18812 74 69 6e 65 20 77 69 6c 6c 20 66 61 69 6c 20 77  tine will fail w
18813 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  ith SQLITE_LOCKE
18814 44 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61  D if there are a
18815 6e 79 20 6f 70 65 6e 0a 2a 2a 20 72 65 61 64 20  ny open.** read 
18816 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 20 74  cursors on the t
18817 61 62 6c 65 2e 20 20 4f 70 65 6e 20 77 72 69 74  able.  Open writ
18818 65 20 63 75 72 73 6f 72 73 20 61 72 65 20 6d 6f  e cursors are mo
18819 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 72 6f  ved to the.** ro
1881a 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e  ot of the table.
1881b 0a 2a 2a 0a 2a 2a 20 49 66 20 70 6e 43 68 61 6e  .**.** If pnChan
1881c 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  ge is not NULL, 
1881d 74 68 65 6e 20 74 61 62 6c 65 20 69 54 61 62 6c  then table iTabl
1881e 65 20 6d 75 73 74 20 62 65 20 61 6e 20 69 6e 74  e must be an int
1881f 6b 65 79 20 74 61 62 6c 65 2e 20 54 68 65 0a 2a  key table. The.*
18820 2a 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  * integer value 
18821 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 6e  pointed to by pn
18822 43 68 61 6e 67 65 20 69 73 20 69 6e 63 72 65 6d  Change is increm
18823 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d  ented by the num
18824 62 65 72 20 6f 66 0a 2a 2a 20 65 6e 74 72 69 65  ber of.** entrie
18825 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a  s in the table..
18826 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
18827 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
18828 65 65 43 6c 65 61 72 54 61 62 6c 65 28 42 74 72  eeClearTable(Btr
18829 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 62 6c  ee *p, int iTabl
1882a 65 2c 20 69 6e 74 20 2a 70 6e 43 68 61 6e 67 65  e, int *pnChange
1882b 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42  ){.  int rc;.  B
1882c 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
1882d 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33  ->pBt;.  sqlite3
1882e 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
1882f 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72   assert( p->inTr
18830 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans==TRANS_WRITE
18831 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76 61 6c 69   );..  /* Invali
18832 64 61 74 65 20 61 6c 6c 20 69 6e 63 72 62 6c 6f  date all incrblo
18833 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f  b cursors open o
18834 6e 20 74 61 62 6c 65 20 69 54 61 62 6c 65 20 28  n table iTable (
18835 61 73 73 75 6d 69 6e 67 20 69 54 61 62 6c 65 0a  assuming iTable.
18836 20 20 2a 2a 20 69 73 20 74 68 65 20 72 6f 6f 74    ** is the root
18837 20 6f 66 20 61 20 74 61 62 6c 65 20 62 2d 74 72   of a table b-tr
18838 65 65 20 2d 20 69 66 20 69 74 20 69 73 20 6e 6f  ee - if it is no
18839 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  t, the following
1883a 20 63 61 6c 6c 20 69 73 0a 20 20 2a 2a 20 61 20   call is.  ** a 
1883b 6e 6f 2d 6f 70 29 2e 20 20 2a 2f 0a 20 20 69 6e  no-op).  */.  in
1883c 76 61 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f 62  validateIncrblob
1883d 43 75 72 73 6f 72 73 28 70 2c 20 30 2c 20 31 29  Cursors(p, 0, 1)
1883e 3b 0a 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c  ;..  rc = saveAl
1883f 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 28 50  lCursors(pBt, (P
18840 67 6e 6f 29 69 54 61 62 6c 65 2c 20 30 29 3b 0a  gno)iTable, 0);.
18841 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d    if( SQLITE_OK=
18842 3d 72 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  =rc ){.    rc = 
18843 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 61 67  clearDatabasePag
18844 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 54 61  e(pBt, (Pgno)iTa
18845 62 6c 65 2c 20 30 2c 20 70 6e 43 68 61 6e 67 65  ble, 0, pnChange
18846 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
18847 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
18848 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
18849 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 69  *.** Erase all i
1884a 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 61 20  nformation in a 
1884b 74 61 62 6c 65 20 61 6e 64 20 61 64 64 20 74 68  table and add th
1884c 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61  e root of the ta
1884d 62 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 20 66 72  ble to.** the fr
1884e 65 65 6c 69 73 74 2e 20 20 45 78 63 65 70 74 2c  eelist.  Except,
1884f 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65   the root of the
18850 20 70 72 69 6e 63 69 70 6c 65 20 74 61 62 6c 65   principle table
18851 20 28 74 68 65 20 6f 6e 65 20 6f 6e 0a 2a 2a 20   (the one on.** 
18852 70 61 67 65 20 31 29 20 69 73 20 6e 65 76 65 72  page 1) is never
18853 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 66 72   added to the fr
18854 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  eelist..**.** Th
18855 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
18856 66 61 69 6c 20 77 69 74 68 20 53 51 4c 49 54 45  fail with SQLITE
18857 5f 4c 4f 43 4b 45 44 20 69 66 20 74 68 65 72 65  _LOCKED if there
18858 20 61 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a   are any open.**
18859 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 20   cursors on the 
1885a 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  table..**.** If 
1885b 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e  AUTOVACUUM is en
1885c 61 62 6c 65 64 20 61 6e 64 20 74 68 65 20 70 61  abled and the pa
1885d 67 65 20 61 74 20 69 54 61 62 6c 65 20 69 73 20  ge at iTable is 
1885e 6e 6f 74 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20  not the last.** 
1885f 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65  root page in the
18860 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
18861 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20 72 6f  then the last ro
18862 6f 74 20 70 61 67 65 20 0a 2a 2a 20 69 6e 20 74  ot page .** in t
18863 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
18864 20 69 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74   is moved into t
18865 68 65 20 73 6c 6f 74 20 66 6f 72 6d 65 72 6c 79  he slot formerly
18866 20 6f 63 63 75 70 69 65 64 20 62 79 0a 2a 2a 20   occupied by.** 
18867 69 54 61 62 6c 65 20 61 6e 64 20 74 68 61 74 20  iTable and that 
18868 6c 61 73 74 20 73 6c 6f 74 20 66 6f 72 6d 65 72  last slot former
18869 6c 79 20 6f 63 63 75 70 69 65 64 20 62 79 20 74  ly occupied by t
1886a 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67  he last root pag
1886b 65 0a 2a 2a 20 69 73 20 61 64 64 65 64 20 74 6f  e.** is added to
1886c 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 6e   the freelist in
1886d 73 74 65 61 64 20 6f 66 20 69 54 61 62 6c 65 2e  stead of iTable.
1886e 20 20 49 6e 20 74 68 69 73 20 73 61 79 2c 20 61    In this say, a
1886f 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 73  ll.** root pages
18870 20 61 72 65 20 6b 65 70 74 20 61 74 20 74 68 65   are kept at the
18871 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
18872 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
18873 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 6e 65 63   which.** is nec
18874 65 73 73 61 72 79 20 66 6f 72 20 41 55 54 4f 56  essary for AUTOV
18875 41 43 55 55 4d 20 74 6f 20 77 6f 72 6b 20 72 69  ACUUM to work ri
18876 67 68 74 2e 20 20 2a 70 69 4d 6f 76 65 64 20 69  ght.  *piMoved i
18877 73 20 73 65 74 20 74 6f 20 74 68 65 20 0a 2a 2a  s set to the .**
18878 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 68 61   page number tha
18879 74 20 75 73 65 64 20 74 6f 20 62 65 20 74 68 65  t used to be the
1887a 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 65 20   last root page 
1887b 69 6e 20 74 68 65 20 66 69 6c 65 20 62 65 66 6f  in the file befo
1887c 72 65 0a 2a 2a 20 74 68 65 20 6d 6f 76 65 2e 20  re.** the move. 
1887d 20 49 66 20 6e 6f 20 70 61 67 65 20 67 65 74 73   If no page gets
1887e 20 6d 6f 76 65 64 2c 20 2a 70 69 4d 6f 76 65 64   moved, *piMoved
1887f 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2a   is set to 0..**
18880 20 54 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70   The last root p
18881 61 67 65 20 69 73 20 72 65 63 6f 72 64 65 64 20  age is recorded 
18882 69 6e 20 6d 65 74 61 5b 33 5d 20 61 6e 64 20 74  in meta[3] and t
18883 68 65 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 6d  he value of.** m
18884 65 74 61 5b 33 5d 20 69 73 20 75 70 64 61 74 65  eta[3] is update
18885 64 20 62 79 20 74 68 69 73 20 70 72 6f 63 65 64  d by this proced
18886 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ure..*/.static i
18887 6e 74 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c  nt btreeDropTabl
18888 65 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f  e(Btree *p, Pgno
18889 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 69   iTable, int *pi
1888a 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 63  Moved){.  int rc
1888b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ;.  MemPage *pPa
1888c 67 65 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 72  ge = 0;.  BtShar
1888d 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
1888e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
1888f 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75  ite3BtreeHoldsMu
18890 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65  tex(p) );.  asse
18891 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d  rt( p->inTrans==
18892 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 0a  TRANS_WRITE );..
18893 20 20 2f 2a 20 49 74 20 69 73 20 69 6c 6c 65 67    /* It is illeg
18894 61 6c 20 74 6f 20 64 72 6f 70 20 61 20 74 61 62  al to drop a tab
18895 6c 65 20 69 66 20 61 6e 79 20 63 75 72 73 6f 72  le if any cursor
18896 73 20 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 68  s are open on th
18897 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e  e.  ** database.
18898 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65   This is because
18899 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   in auto-vacuum 
1889a 6d 6f 64 65 20 74 68 65 20 62 61 63 6b 65 6e 64  mode the backend
1889b 20 6d 61 79 0a 20 20 2a 2a 20 6e 65 65 64 20 74   may.  ** need t
1889c 6f 20 6d 6f 76 65 20 61 6e 6f 74 68 65 72 20 72  o move another r
1889d 6f 6f 74 2d 70 61 67 65 20 74 6f 20 66 69 6c 6c  oot-page to fill
1889e 20 61 20 67 61 70 20 6c 65 66 74 20 62 79 20 74   a gap left by t
1889f 68 65 20 64 65 6c 65 74 65 64 0a 20 20 2a 2a 20  he deleted.  ** 
188a0 72 6f 6f 74 20 70 61 67 65 2e 20 49 66 20 61 6e  root page. If an
188a1 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 77 61 73   open cursor was
188a2 20 75 73 69 6e 67 20 74 68 69 73 20 70 61 67 65   using this page
188a3 20 61 20 70 72 6f 62 6c 65 6d 20 77 6f 75 6c 64   a problem would
188a4 20 0a 20 20 2a 2a 20 6f 63 63 75 72 2e 0a 20 20   .  ** occur..  
188a5 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 65 72 72  **.  ** This err
188a6 6f 72 20 69 73 20 63 61 75 67 68 74 20 6c 6f 6e  or is caught lon
188a7 67 20 62 65 66 6f 72 65 20 63 6f 6e 74 72 6f 6c  g before control
188a8 20 72 65 61 63 68 65 73 20 74 68 69 73 20 70 6f   reaches this po
188a9 69 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  int..  */.  if( 
188aa 4e 45 56 45 52 28 70 42 74 2d 3e 70 43 75 72 73  NEVER(pBt->pCurs
188ab 6f 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  or) ){.    sqlit
188ac 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63  e3ConnectionBloc
188ad 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e  ked(p->db, pBt->
188ae 70 43 75 72 73 6f 72 2d 3e 70 42 74 72 65 65 2d  pCursor->pBtree-
188af 3e 64 62 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  >db);.    return
188b0 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53   SQLITE_LOCKED_S
188b1 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 7d 0a  HAREDCACHE;.  }.
188b2 0a 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74  .  rc = btreeGet
188b3 50 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29  Page(pBt, (Pgno)
188b4 69 54 61 62 6c 65 2c 20 26 70 50 61 67 65 2c 20  iTable, &pPage, 
188b5 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  0);.  if( rc ) r
188b6 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d  eturn rc;.  rc =
188b7 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65   sqlite3BtreeCle
188b8 61 72 54 61 62 6c 65 28 70 2c 20 69 54 61 62 6c  arTable(p, iTabl
188b9 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20  e, 0);.  if( rc 
188ba 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61  ){.    releasePa
188bb 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72  ge(pPage);.    r
188bc 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
188bd 20 2a 70 69 4d 6f 76 65 64 20 3d 20 30 3b 0a 0a   *piMoved = 0;..
188be 20 20 69 66 28 20 69 54 61 62 6c 65 3e 31 20 29    if( iTable>1 )
188bf 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  {.#ifdef SQLITE_
188c0 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
188c1 20 20 20 20 66 72 65 65 50 61 67 65 28 70 50 61      freePage(pPa
188c2 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 72 65  ge, &rc);.    re
188c3 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29  leasePage(pPage)
188c4 3b 0a 23 65 6c 73 65 0a 20 20 20 20 69 66 28 20  ;.#else.    if( 
188c5 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
188c6 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 6d 61  ){.      Pgno ma
188c7 78 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20  xRootPgno;.     
188c8 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
188c9 4d 65 74 61 28 70 2c 20 42 54 52 45 45 5f 4c 41  Meta(p, BTREE_LA
188ca 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 2c  RGEST_ROOT_PAGE,
188cb 20 26 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a   &maxRootPgno);.
188cc 0a 20 20 20 20 20 20 69 66 28 20 69 54 61 62 6c  .      if( iTabl
188cd 65 3d 3d 6d 61 78 52 6f 6f 74 50 67 6e 6f 20 29  e==maxRootPgno )
188ce 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20  {.        /* If 
188cf 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20  the table being 
188d0 64 72 6f 70 70 65 64 20 69 73 20 74 68 65 20 74  dropped is the t
188d1 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 6c 61  able with the la
188d2 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a  rgest root-page.
188d3 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65          ** numbe
188d4 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  r in the databas
188d5 65 2c 20 70 75 74 20 74 68 65 20 72 6f 6f 74 20  e, put the root 
188d6 70 61 67 65 20 6f 6e 20 74 68 65 20 66 72 65 65  page on the free
188d7 20 6c 69 73 74 2e 20 0a 20 20 20 20 20 20 20 20   list. .        
188d8 2a 2f 0a 20 20 20 20 20 20 20 20 66 72 65 65 50  */.        freeP
188d9 61 67 65 28 70 50 61 67 65 2c 20 26 72 63 29 3b  age(pPage, &rc);
188da 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
188db 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
188dc 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
188dd 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
188de 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
188df 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
188e0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a  else{.        /*
188e1 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   The table being
188e2 20 64 72 6f 70 70 65 64 20 64 6f 65 73 20 6e 6f   dropped does no
188e3 74 20 68 61 76 65 20 74 68 65 20 6c 61 72 67 65  t have the large
188e4 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20  st root-page.   
188e5 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 69       ** number i
188e6 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  n the database. 
188e7 53 6f 20 6d 6f 76 65 20 74 68 65 20 70 61 67 65  So move the page
188e8 20 74 68 61 74 20 64 6f 65 73 20 69 6e 74 6f 20   that does into 
188e9 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  the .        ** 
188ea 67 61 70 20 6c 65 66 74 20 62 79 20 74 68 65 20  gap left by the 
188eb 64 65 6c 65 74 65 64 20 72 6f 6f 74 2d 70 61 67  deleted root-pag
188ec 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  e..        */.  
188ed 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70        MemPage *p
188ee 4d 6f 76 65 3b 0a 20 20 20 20 20 20 20 20 72 65  Move;.        re
188ef 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29  leasePage(pPage)
188f0 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 62  ;.        rc = b
188f1 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
188f2 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 70   maxRootPgno, &p
188f3 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Move, 0);.      
188f4 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
188f5 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
188f6 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
188f7 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63      }.        rc
188f8 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28   = relocatePage(
188f9 70 42 74 2c 20 70 4d 6f 76 65 2c 20 50 54 52 4d  pBt, pMove, PTRM
188fa 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20 30 2c 20  AP_ROOTPAGE, 0, 
188fb 69 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20 20 20  iTable, 0);.    
188fc 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
188fd 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20 20  pMove);.        
188fe 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
188ff 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  K ){.          r
18900 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
18901 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4d 6f 76    }.        pMov
18902 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 72  e = 0;.        r
18903 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65  c = btreeGetPage
18904 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e  (pBt, maxRootPgn
18905 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b 0a 20  o, &pMove, 0);. 
18906 20 20 20 20 20 20 20 66 72 65 65 50 61 67 65 28         freePage(
18907 70 4d 6f 76 65 2c 20 26 72 63 29 3b 0a 20 20 20  pMove, &rc);.   
18908 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
18909 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20  (pMove);.       
1890a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1890b 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
1890c 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
1890d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2a 70 69     }.        *pi
1890e 4d 6f 76 65 64 20 3d 20 6d 61 78 52 6f 6f 74 50  Moved = maxRootP
1890f 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  gno;.      }..  
18910 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6e      /* Set the n
18911 65 77 20 27 6d 61 78 2d 72 6f 6f 74 2d 70 61 67  ew 'max-root-pag
18912 65 27 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20  e' value in the 
18913 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72 2e  database header.
18914 20 54 68 69 73 0a 20 20 20 20 20 20 2a 2a 20 69   This.      ** i
18915 73 20 74 68 65 20 6f 6c 64 20 76 61 6c 75 65 20  s the old value 
18916 6c 65 73 73 20 6f 6e 65 2c 20 6c 65 73 73 20 6f  less one, less o
18917 6e 65 20 6d 6f 72 65 20 69 66 20 74 68 61 74 20  ne more if that 
18918 68 61 70 70 65 6e 73 20 74 6f 0a 20 20 20 20 20  happens to.     
18919 20 2a 2a 20 62 65 20 61 20 72 6f 6f 74 2d 70 61   ** be a root-pa
1891a 67 65 20 6e 75 6d 62 65 72 2c 20 6c 65 73 73 20  ge number, less 
1891b 6f 6e 65 20 61 67 61 69 6e 20 69 66 20 74 68 61  one again if tha
1891c 74 20 69 73 20 74 68 65 0a 20 20 20 20 20 20 2a  t is the.      *
1891d 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  * PENDING_BYTE_P
1891e 41 47 45 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  AGE..      */.  
1891f 20 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d      maxRootPgno-
18920 2d 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  -;.      while( 
18921 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50 45 4e  maxRootPgno==PEN
18922 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
18923 42 74 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  Bt).            
18924 20 7c 7c 20 50 54 52 4d 41 50 5f 49 53 50 41 47   || PTRMAP_ISPAG
18925 45 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67  E(pBt, maxRootPg
18926 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d  no) ){.        m
18927 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20  axRootPgno--;.  
18928 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65      }.      asse
18929 72 74 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 21  rt( maxRootPgno!
1892a 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  =PENDING_BYTE_PA
1892b 47 45 28 70 42 74 29 20 29 3b 0a 0a 20 20 20 20  GE(pBt) );..    
1892c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1892d 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 70 2c  reeUpdateMeta(p,
1892e 20 34 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 29   4, maxRootPgno)
1892f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
18930 20 20 20 66 72 65 65 50 61 67 65 28 70 50 61 67     freePage(pPag
18931 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 72  e, &rc);.      r
18932 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65  eleasePage(pPage
18933 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
18934 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
18935 49 66 20 73 71 6c 69 74 65 33 42 74 72 65 65 44  If sqlite3BtreeD
18936 72 6f 70 54 61 62 6c 65 20 77 61 73 20 63 61 6c  ropTable was cal
18937 6c 65 64 20 6f 6e 20 70 61 67 65 20 31 2e 0a 20  led on page 1.. 
18938 20 20 20 2a 2a 20 54 68 69 73 20 72 65 61 6c 6c     ** This reall
18939 79 20 6e 65 76 65 72 20 73 68 6f 75 6c 64 20 68  y never should h
1893a 61 70 70 65 6e 20 65 78 63 65 70 74 20 69 6e 20  appen except in 
1893b 61 20 63 6f 72 72 75 70 74 0a 20 20 20 20 2a 2a  a corrupt.    **
1893c 20 64 61 74 61 62 61 73 65 2e 20 0a 20 20 20 20   database. .    
1893d 2a 2f 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28  */.    zeroPage(
1893e 70 50 61 67 65 2c 20 50 54 46 5f 49 4e 54 4b 45  pPage, PTF_INTKE
1893f 59 7c 50 54 46 5f 4c 45 41 46 20 29 3b 0a 20 20  Y|PTF_LEAF );.  
18940 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
18941 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  age);.  }.  retu
18942 72 6e 20 72 63 3b 20 20 0a 7d 0a 53 51 4c 49 54  rn rc;  .}.SQLIT
18943 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
18944 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61  lite3BtreeDropTa
18945 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e  ble(Btree *p, in
18946 74 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70  t iTable, int *p
18947 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72  iMoved){.  int r
18948 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  c;.  sqlite3Btre
18949 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20  eEnter(p);.  rc 
1894a 3d 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c 65  = btreeDropTable
1894b 28 70 2c 20 69 54 61 62 6c 65 2c 20 70 69 4d 6f  (p, iTable, piMo
1894c 76 65 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  ved);.  sqlite3B
1894d 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
1894e 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
1894f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
18950 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 63  on may only be c
18951 61 6c 6c 65 64 20 69 66 20 74 68 65 20 62 2d 74  alled if the b-t
18952 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61  ree connection a
18953 6c 72 65 61 64 79 0a 2a 2a 20 68 61 73 20 61 20  lready.** has a 
18954 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 72  read or write tr
18955 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 20 6f  ansaction open o
18956 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a  n the database..
18957 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 6d  **.** Read the m
18958 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  eta-information 
18959 6f 75 74 20 6f 66 20 61 20 64 61 74 61 62 61 73  out of a databas
1895a 65 20 66 69 6c 65 2e 20 20 4d 65 74 61 5b 30 5d  e file.  Meta[0]
1895b 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 65  .** is the numbe
1895c 72 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 20  r of free pages 
1895d 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65  currently in the
1895e 20 64 61 74 61 62 61 73 65 2e 20 20 4d 65 74 61   database.  Meta
1895f 5b 31 5d 0a 2a 2a 20 74 68 72 6f 75 67 68 20 6d  [1].** through m
18960 65 74 61 5b 31 35 5d 20 61 72 65 20 61 76 61 69  eta[15] are avai
18961 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 20 62 79  lable for use by
18962 20 68 69 67 68 65 72 20 6c 61 79 65 72 73 2e 20   higher layers. 
18963 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20 69 73 20 72   Meta[0].** is r
18964 65 61 64 2d 6f 6e 6c 79 2c 20 74 68 65 20 6f 74  ead-only, the ot
18965 68 65 72 73 20 61 72 65 20 72 65 61 64 2f 77 72  hers are read/wr
18966 69 74 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20  ite..** .** The 
18967 73 63 68 65 6d 61 20 6c 61 79 65 72 20 6e 75 6d  schema layer num
18968 62 65 72 73 20 6d 65 74 61 20 76 61 6c 75 65 73  bers meta values
18969 20 64 69 66 66 65 72 65 6e 74 6c 79 2e 20 20 41   differently.  A
1896a 74 20 74 68 65 20 73 63 68 65 6d 61 0a 2a 2a 20  t the schema.** 
1896b 6c 61 79 65 72 20 28 61 6e 64 20 74 68 65 20 53  layer (and the S
1896c 65 74 43 6f 6f 6b 69 65 20 61 6e 64 20 52 65 61  etCookie and Rea
1896d 64 43 6f 6f 6b 69 65 20 6f 70 63 6f 64 65 73 29  dCookie opcodes)
1896e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
1896f 2a 20 66 72 65 65 20 70 61 67 65 73 20 69 73 20  * free pages is 
18970 6e 6f 74 20 76 69 73 69 62 6c 65 2e 20 20 53 6f  not visible.  So
18971 20 43 6f 6f 6b 69 65 5b 30 5d 20 69 73 20 74 68   Cookie[0] is th
18972 65 20 73 61 6d 65 20 61 73 20 4d 65 74 61 5b 31  e same as Meta[1
18973 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  ]..*/.SQLITE_PRI
18974 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
18975 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 42 74  3BtreeGetMeta(Bt
18976 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 64 78 2c  ree *p, int idx,
18977 20 75 33 32 20 2a 70 4d 65 74 61 29 7b 0a 20 20   u32 *pMeta){.  
18978 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
18979 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74  p->pBt;..  sqlit
1897a 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
1897b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e  .  assert( p->in
1897c 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45  Trans>TRANS_NONE
1897d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 53 51   );.  assert( SQ
1897e 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 53 68  LITE_OK==querySh
1897f 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f  aredCacheTableLo
18980 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f  ck(p, MASTER_ROO
18981 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 20 29 3b  T, READ_LOCK) );
18982 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e  .  assert( pBt->
18983 70 50 61 67 65 31 20 29 3b 0a 20 20 61 73 73 65  pPage1 );.  asse
18984 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20 69 64  rt( idx>=0 && id
18985 78 3c 3d 31 35 20 29 3b 0a 0a 20 20 2a 70 4d 65  x<=15 );..  *pMe
18986 74 61 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  ta = get4byte(&p
18987 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74  Bt->pPage1->aDat
18988 61 5b 33 36 20 2b 20 69 64 78 2a 34 5d 29 3b 0a  a[36 + idx*4]);.
18989 0a 20 20 2f 2a 20 49 66 20 61 75 74 6f 2d 76 61  .  /* If auto-va
1898a 63 75 75 6d 20 69 73 20 64 69 73 61 62 6c 65 64  cuum is disabled
1898b 20 69 6e 20 74 68 69 73 20 62 75 69 6c 64 20 61   in this build a
1898c 6e 64 20 74 68 69 73 20 69 73 20 61 6e 20 61 75  nd this is an au
1898d 74 6f 2d 76 61 63 75 75 6d 0a 20 20 2a 2a 20 64  to-vacuum.  ** d
1898e 61 74 61 62 61 73 65 2c 20 6d 61 72 6b 20 74 68  atabase, mark th
1898f 65 20 64 61 74 61 62 61 73 65 20 61 73 20 72 65  e database as re
18990 61 64 2d 6f 6e 6c 79 2e 20 20 2a 2f 0a 23 69 66  ad-only.  */.#if
18991 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
18992 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 28  AUTOVACUUM.  if(
18993 20 69 64 78 3d 3d 42 54 52 45 45 5f 4c 41 52 47   idx==BTREE_LARG
18994 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 20 26 26  EST_ROOT_PAGE &&
18995 20 2a 70 4d 65 74 61 3e 30 20 29 20 70 42 74 2d   *pMeta>0 ) pBt-
18996 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 23  >readOnly = 1;.#
18997 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33  endif..  sqlite3
18998 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 7d  BtreeLeave(p);.}
18999 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d 65  ../*.** Write me
1899a 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 62  ta-information b
1899b 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 74  ack into the dat
1899c 61 62 61 73 65 2e 20 20 4d 65 74 61 5b 30 5d 20  abase.  Meta[0] 
1899d 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 20  is.** read-only 
1899e 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62 65 20 77  and may not be w
1899f 72 69 74 74 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ritten..*/.SQLIT
189a0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
189a1 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65  lite3BtreeUpdate
189a2 4d 65 74 61 28 42 74 72 65 65 20 2a 70 2c 20 69  Meta(Btree *p, i
189a3 6e 74 20 69 64 78 2c 20 75 33 32 20 69 4d 65 74  nt idx, u32 iMet
189a4 61 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  a){.  BtShared *
189a5 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
189a6 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
189a7 50 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  P1;.  int rc;.  
189a8 61 73 73 65 72 74 28 20 69 64 78 3e 3d 31 20 26  assert( idx>=1 &
189a9 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20 73  & idx<=15 );.  s
189aa 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
189ab 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  (p);.  assert( p
189ac 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53  ->inTrans==TRANS
189ad 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65  _WRITE );.  asse
189ae 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 21  rt( pBt->pPage1!
189af 3d 30 20 29 3b 0a 20 20 70 50 31 20 3d 20 70 42  =0 );.  pP1 = pB
189b0 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61  t->pPage1->aData
189b1 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
189b2 50 61 67 65 72 57 72 69 74 65 28 70 42 74 2d 3e  PagerWrite(pBt->
189b3 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29  pPage1->pDbPage)
189b4 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
189b5 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 75 74  TE_OK ){.    put
189b6 34 62 79 74 65 28 26 70 50 31 5b 33 36 20 2b 20  4byte(&pP1[36 + 
189b7 69 64 78 2a 34 5d 2c 20 69 4d 65 74 61 29 3b 0a  idx*4], iMeta);.
189b8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
189b9 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
189ba 20 20 20 69 66 28 20 69 64 78 3d 3d 42 54 52 45     if( idx==BTRE
189bb 45 5f 49 4e 43 52 5f 56 41 43 55 55 4d 20 29 7b  E_INCR_VACUUM ){
189bc 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
189bd 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 7c  Bt->autoVacuum |
189be 7c 20 69 4d 65 74 61 3d 3d 30 20 29 3b 0a 20 20  | iMeta==0 );.  
189bf 20 20 20 20 61 73 73 65 72 74 28 20 69 4d 65 74      assert( iMet
189c0 61 3d 3d 30 20 7c 7c 20 69 4d 65 74 61 3d 3d 31  a==0 || iMeta==1
189c1 20 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69   );.      pBt->i
189c2 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 75 38 29  ncrVacuum = (u8)
189c3 69 4d 65 74 61 3b 0a 20 20 20 20 7d 0a 23 65 6e  iMeta;.    }.#en
189c4 64 69 66 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  dif.  }.  sqlite
189c5 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
189c6 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
189c7 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
189c8 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 2f  MIT_BTREECOUNT./
189c9 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61  *.** The first a
189ca 72 67 75 6d 65 6e 74 2c 20 70 43 75 72 2c 20 69  rgument, pCur, i
189cb 73 20 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 65  s a cursor opene
189cc 64 20 6f 6e 20 73 6f 6d 65 20 62 2d 74 72 65 65  d on some b-tree
189cd 2e 20 43 6f 75 6e 74 20 74 68 65 0a 2a 2a 20 6e  . Count the.** n
189ce 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
189cf 20 69 6e 20 74 68 65 20 62 2d 74 72 65 65 20 61   in the b-tree a
189d0 6e 64 20 77 72 69 74 65 20 74 68 65 20 72 65 73  nd write the res
189d1 75 6c 74 20 74 6f 20 2a 70 6e 45 6e 74 72 79 2e  ult to *pnEntry.
189d2 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  .**.** SQLITE_OK
189d3 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
189d4 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73  the operation is
189d5 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 65 78   successfully ex
189d6 65 63 75 74 65 64 2e 20 0a 2a 2a 20 4f 74 68 65  ecuted. .** Othe
189d7 72 77 69 73 65 2c 20 69 66 20 61 6e 20 65 72 72  rwise, if an err
189d8 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  or is encountere
189d9 64 20 28 69 2e 65 2e 20 61 6e 20 49 4f 20 65 72  d (i.e. an IO er
189da 72 6f 72 20 6f 72 20 64 61 74 61 62 61 73 65 0a  ror or database.
189db 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 29 20 61  ** corruption) a
189dc 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
189dd 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
189de 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
189df 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
189e0 72 65 65 43 6f 75 6e 74 28 42 74 43 75 72 73 6f  reeCount(BtCurso
189e1 72 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 6e  r *pCur, i64 *pn
189e2 45 6e 74 72 79 29 7b 0a 20 20 69 36 34 20 6e 45  Entry){.  i64 nE
189e3 6e 74 72 79 20 3d 20 30 3b 20 20 20 20 20 20 20  ntry = 0;       
189e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
189e5 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72  * Value to retur
189e6 6e 20 69 6e 20 2a 70 6e 45 6e 74 72 79 20 2a 2f  n in *pnEntry */
189e7 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
189e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
189e9 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
189ea 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 72 63 20 3d  n code */.  rc =
189eb 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72   moveToRoot(pCur
189ec 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 65 73 73 20  );..  /* Unless 
189ed 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
189ee 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6c   the following l
189ef 6f 6f 70 20 72 75 6e 73 20 6f 6e 65 20 69 74 65  oop runs one ite
189f0 72 61 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 0a  ration for each.
189f1 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65    ** page in the
189f2 20 42 2d 54 72 65 65 20 73 74 72 75 63 74 75 72   B-Tree structur
189f3 65 20 28 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67  e (not including
189f4 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 29   overflow pages)
189f5 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28  . .  */.  while(
189f6 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
189f7 7b 0a 20 20 20 20 69 6e 74 20 69 49 64 78 3b 20  {.    int iIdx; 
189f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
189f9 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
189fa 78 20 6f 66 20 63 68 69 6c 64 20 6e 6f 64 65 20  x of child node 
189fb 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a 20 20 20  in parent */.   
189fc 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
189fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
189fe 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 70      /* Current p
189ff 61 67 65 20 6f 66 20 74 68 65 20 62 2d 74 72 65  age of the b-tre
18a00 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 20  e */..    /* If 
18a01 74 68 69 73 20 69 73 20 61 20 6c 65 61 66 20 70  this is a leaf p
18a02 61 67 65 20 6f 72 20 74 68 65 20 74 72 65 65 20  age or the tree 
18a03 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 2d 6b 65  is not an int-ke
18a04 79 20 74 72 65 65 2c 20 74 68 65 6e 20 0a 20 20  y tree, then .  
18a05 20 20 2a 2a 20 74 68 69 73 20 70 61 67 65 20 63    ** this page c
18a06 6f 6e 74 61 69 6e 73 20 63 6f 75 6e 74 61 62 6c  ontains countabl
18a07 65 20 65 6e 74 72 69 65 73 2e 20 49 6e 63 72 65  e entries. Incre
18a08 6d 65 6e 74 20 74 68 65 20 65 6e 74 72 79 20 63  ment the entry c
18a09 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a 20 61 63  ounter.    ** ac
18a0a 63 6f 72 64 69 6e 67 6c 79 2e 0a 20 20 20 20 2a  cordingly..    *
18a0b 2f 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 43  /.    pPage = pC
18a0c 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
18a0d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 28  >iPage];.    if(
18a0e 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 7c 7c 20   pPage->leaf || 
18a0f 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29  !pPage->intKey )
18a10 7b 0a 20 20 20 20 20 20 6e 45 6e 74 72 79 20 2b  {.      nEntry +
18a11 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a  = pPage->nCell;.
18a12 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 70 50      }..    /* pP
18a13 61 67 65 20 69 73 20 61 20 6c 65 61 66 20 6e 6f  age is a leaf no
18a14 64 65 2e 20 54 68 69 73 20 6c 6f 6f 70 20 6e 61  de. This loop na
18a15 76 69 67 61 74 65 73 20 74 68 65 20 63 75 72 73  vigates the curs
18a16 6f 72 20 73 6f 20 74 68 61 74 20 69 74 20 0a 20  or so that it . 
18a17 20 20 20 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20     ** points to 
18a18 74 68 65 20 66 69 72 73 74 20 69 6e 74 65 72 69  the first interi
18a19 6f 72 20 63 65 6c 6c 20 74 68 61 74 20 69 74 20  or cell that it 
18a1a 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 70 61  points to the pa
18a1b 72 65 6e 74 20 6f 66 0a 20 20 20 20 2a 2a 20 74  rent of.    ** t
18a1c 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20  he next page in 
18a1d 74 68 65 20 74 72 65 65 20 74 68 61 74 20 68 61  the tree that ha
18a1e 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 76  s not yet been v
18a1f 69 73 69 74 65 64 2e 20 54 68 65 0a 20 20 20 20  isited. The.    
18a20 2a 2a 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70  ** pCur->aiIdx[p
18a21 43 75 72 2d 3e 69 50 61 67 65 5d 20 76 61 6c 75  Cur->iPage] valu
18a22 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  e is set to the 
18a23 69 6e 64 65 78 20 6f 66 20 74 68 65 20 70 61 72  index of the par
18a24 65 6e 74 20 63 65 6c 6c 0a 20 20 20 20 2a 2a 20  ent cell.    ** 
18a25 6f 66 20 74 68 65 20 70 61 67 65 2c 20 6f 72 20  of the page, or 
18a26 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  to the number of
18a27 20 63 65 6c 6c 73 20 69 6e 20 74 68 65 20 70 61   cells in the pa
18a28 67 65 20 69 66 20 74 68 65 20 6e 65 78 74 20 70  ge if the next p
18a29 61 67 65 0a 20 20 20 20 2a 2a 20 74 6f 20 76 69  age.    ** to vi
18a2a 73 69 74 20 69 73 20 74 68 65 20 72 69 67 68 74  sit is the right
18a2b 2d 63 68 69 6c 64 20 6f 66 20 69 74 73 20 70 61  -child of its pa
18a2c 72 65 6e 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  rent..    **.   
18a2d 20 2a 2a 20 49 66 20 61 6c 6c 20 70 61 67 65 73   ** If all pages
18a2e 20 69 6e 20 74 68 65 20 74 72 65 65 20 68 61 76   in the tree hav
18a2f 65 20 62 65 65 6e 20 76 69 73 69 74 65 64 2c 20  e been visited, 
18a30 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
18a31 20 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 63   to the.    ** c
18a32 61 6c 6c 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20  aller..    */.  
18a33 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61    if( pPage->lea
18a34 66 20 29 7b 0a 20 20 20 20 20 20 64 6f 20 7b 0a  f ){.      do {.
18a35 20 20 20 20 20 20 20 20 69 66 28 20 70 43 75 72          if( pCur
18a36 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20  ->iPage==0 ){.  
18a37 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 70          /* All p
18a38 61 67 65 73 20 6f 66 20 74 68 65 20 62 2d 74 72  ages of the b-tr
18a39 65 65 20 68 61 76 65 20 62 65 65 6e 20 76 69 73  ee have been vis
18a3a 69 74 65 64 2e 20 52 65 74 75 72 6e 20 73 75 63  ited. Return suc
18a3b 63 65 73 73 66 75 6c 6c 79 2e 20 2a 2f 0a 20 20  cessfully. */.  
18a3c 20 20 20 20 20 20 20 20 2a 70 6e 45 6e 74 72 79          *pnEntry
18a3d 20 3d 20 6e 45 6e 74 72 79 3b 0a 20 20 20 20 20   = nEntry;.     
18a3e 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
18a3f 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d  TE_OK;.        }
18a40 0a 20 20 20 20 20 20 20 20 6d 6f 76 65 54 6f 50  .        moveToP
18a41 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20  arent(pCur);.   
18a42 20 20 20 7d 77 68 69 6c 65 20 28 20 70 43 75 72     }while ( pCur
18a43 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
18a44 61 67 65 5d 3e 3d 70 43 75 72 2d 3e 61 70 50 61  age]>=pCur->apPa
18a45 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d  ge[pCur->iPage]-
18a46 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 20 20 20  >nCell );..     
18a47 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
18a48 72 2d 3e 69 50 61 67 65 5d 2b 2b 3b 0a 20 20 20  r->iPage]++;.   
18a49 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d     pPage = pCur-
18a4a 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
18a4b 61 67 65 5d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  age];.    }..   
18a4c 20 2f 2a 20 44 65 73 63 65 6e 64 20 74 6f 20 74   /* Descend to t
18a4d 68 65 20 63 68 69 6c 64 20 6e 6f 64 65 20 6f 66  he child node of
18a4e 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20 74   the cell that t
18a4f 68 65 20 63 75 72 73 6f 72 20 63 75 72 72 65 6e  he cursor curren
18a50 74 6c 79 20 0a 20 20 20 20 2a 2a 20 70 6f 69 6e  tly .    ** poin
18a51 74 73 20 61 74 2e 20 54 68 69 73 20 69 73 20 74  ts at. This is t
18a52 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 69  he right-child i
18a53 66 20 28 69 49 64 78 3d 3d 70 50 61 67 65 2d 3e  f (iIdx==pPage->
18a54 6e 43 65 6c 6c 29 2e 0a 20 20 20 20 2a 2f 0a 20  nCell)..    */. 
18a55 20 20 20 69 49 64 78 20 3d 20 70 43 75 72 2d 3e     iIdx = pCur->
18a56 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67  aiIdx[pCur->iPag
18a57 65 5d 3b 0a 20 20 20 20 69 66 28 20 69 49 64 78  e];.    if( iIdx
18a58 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29  ==pPage->nCell )
18a59 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76  {.      rc = mov
18a5a 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67  eToChild(pCur, g
18a5b 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e  et4byte(&pPage->
18a5c 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72  aData[pPage->hdr
18a5d 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20 20 20  Offset+8]));.   
18a5e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
18a5f 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70   = moveToChild(p
18a60 43 75 72 2c 20 67 65 74 34 62 79 74 65 28 66 69  Cur, get4byte(fi
18a61 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 49  ndCell(pPage, iI
18a62 64 78 29 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  dx)));.    }.  }
18a63 0a 0a 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20  ..  /* An error 
18a64 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 52 65  has occurred. Re
18a65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f  turn an error co
18a66 64 65 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  de. */.  return 
18a67 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  rc;.}.#endif../*
18a68 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70  .** Return the p
18a69 61 67 65 72 20 61 73 73 6f 63 69 61 74 65 64 20  ager associated 
18a6a 77 69 74 68 20 61 20 42 54 72 65 65 2e 20 20 54  with a BTree.  T
18a6b 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
18a6c 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69  sed for.** testi
18a6d 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67  ng and debugging
18a6e 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45   only..*/.SQLITE
18a6f 5f 50 52 49 56 41 54 45 20 50 61 67 65 72 20 2a  _PRIVATE Pager *
18a70 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
18a71 72 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72  r(Btree *p){.  r
18a72 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 50  eturn p->pBt->pP
18a73 61 67 65 72 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  ager;.}..#ifndef
18a74 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54   SQLITE_OMIT_INT
18a75 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a  EGRITY_CHECK./*.
18a76 2a 2a 20 41 70 70 65 6e 64 20 61 20 6d 65 73 73  ** Append a mess
18a77 61 67 65 20 74 6f 20 74 68 65 20 65 72 72 6f 72  age to the error
18a78 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 2e   message string.
18a79 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
18a7a 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 0a  checkAppendMsg(.
18a7b 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70    IntegrityCk *p
18a7c 43 68 65 63 6b 2c 0a 20 20 63 68 61 72 20 2a 7a  Check,.  char *z
18a7d 4d 73 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63 68  Msg1,.  const ch
18a7e 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 0a 20 20 2e  ar *zFormat,.  .
18a7f 2e 2e 0a 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20  ...){.  va_list 
18a80 61 70 3b 0a 20 20 69 66 28 20 21 70 43 68 65 63  ap;.  if( !pChec
18a81 6b 2d 3e 6d 78 45 72 72 20 29 20 72 65 74 75 72  k->mxErr ) retur
18a82 6e 3b 0a 20 20 70 43 68 65 63 6b 2d 3e 6d 78 45  n;.  pCheck->mxE
18a83 72 72 2d 2d 3b 0a 20 20 70 43 68 65 63 6b 2d 3e  rr--;.  pCheck->
18a84 6e 45 72 72 2b 2b 3b 0a 20 20 76 61 5f 73 74 61  nErr++;.  va_sta
18a85 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b  rt(ap, zFormat);
18a86 0a 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 65  .  if( pCheck->e
18a87 72 72 4d 73 67 2e 6e 43 68 61 72 20 29 7b 0a 20  rrMsg.nChar ){. 
18a88 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
18a89 75 6d 41 70 70 65 6e 64 28 26 70 43 68 65 63 6b  umAppend(&pCheck
18a8a 2d 3e 65 72 72 4d 73 67 2c 20 22 5c 6e 22 2c 20  ->errMsg, "\n", 
18a8b 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 4d  1);.  }.  if( zM
18a8c 73 67 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  sg1 ){.    sqlit
18a8d 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64  e3StrAccumAppend
18a8e 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67  (&pCheck->errMsg
18a8f 2c 20 7a 4d 73 67 31 2c 20 2d 31 29 3b 0a 20 20  , zMsg1, -1);.  
18a90 7d 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69  }.  sqlite3VXPri
18a91 6e 74 66 28 26 70 43 68 65 63 6b 2d 3e 65 72 72  ntf(&pCheck->err
18a92 4d 73 67 2c 20 31 2c 20 7a 46 6f 72 6d 61 74 2c  Msg, 1, zFormat,
18a93 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61   ap);.  va_end(a
18a94 70 29 3b 0a 20 20 69 66 28 20 70 43 68 65 63 6b  p);.  if( pCheck
18a95 2d 3e 65 72 72 4d 73 67 2e 6d 61 6c 6c 6f 63 46  ->errMsg.mallocF
18a96 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 43 68  ailed ){.    pCh
18a97 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  eck->mallocFaile
18a98 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  d = 1;.  }.}.#en
18a99 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
18a9a 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  IT_INTEGRITY_CHE
18a9b 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  CK */..#ifndef S
18a9c 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47  QLITE_OMIT_INTEG
18a9d 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a  RITY_CHECK./*.**
18a9e 20 41 64 64 20 31 20 74 6f 20 74 68 65 20 72 65   Add 1 to the re
18a9f 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f  ference count fo
18aa0 72 20 70 61 67 65 20 69 50 61 67 65 2e 20 20 49  r page iPage.  I
18aa1 66 20 74 68 69 73 20 69 73 20 74 68 65 20 73 65  f this is the se
18aa2 63 6f 6e 64 0a 2a 2a 20 72 65 66 65 72 65 6e 63  cond.** referenc
18aa3 65 20 74 6f 20 74 68 65 20 70 61 67 65 2c 20 61  e to the page, a
18aa4 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  dd an error mess
18aa5 61 67 65 20 74 6f 20 70 43 68 65 63 6b 2d 3e 7a  age to pCheck->z
18aa6 45 72 72 4d 73 67 2e 0a 2a 2a 20 52 65 74 75 72  ErrMsg..** Retur
18aa7 6e 20 31 20 69 66 20 74 68 65 72 65 20 61 72 65  n 1 if there are
18aa8 20 32 20 6f 72 65 20 6d 6f 72 65 20 72 65 66 65   2 ore more refe
18aa9 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61  rences to the pa
18aaa 67 65 20 61 6e 64 20 30 20 69 66 0a 2a 2a 20 69  ge and 0 if.** i
18aab 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69  f this is the fi
18aac 72 73 74 20 72 65 66 65 72 65 6e 63 65 20 74 6f  rst reference to
18aad 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a   the page..**.**
18aae 20 41 6c 73 6f 20 63 68 65 63 6b 20 74 68 61 74   Also check that
18aaf 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
18ab0 20 69 73 20 69 6e 20 62 6f 75 6e 64 73 2e 0a 2a   is in bounds..*
18ab1 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65  /.static int che
18ab2 63 6b 52 65 66 28 49 6e 74 65 67 72 69 74 79 43  ckRef(IntegrityC
18ab3 6b 20 2a 70 43 68 65 63 6b 2c 20 50 67 6e 6f 20  k *pCheck, Pgno 
18ab4 69 50 61 67 65 2c 20 63 68 61 72 20 2a 7a 43 6f  iPage, char *zCo
18ab5 6e 74 65 78 74 29 7b 0a 20 20 69 66 28 20 69 50  ntext){.  if( iP
18ab6 61 67 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  age==0 ) return 
18ab7 31 3b 0a 20 20 69 66 28 20 69 50 61 67 65 3e 70  1;.  if( iPage>p
18ab8 43 68 65 63 6b 2d 3e 6e 50 61 67 65 20 29 7b 0a  Check->nPage ){.
18ab9 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
18aba 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74  sg(pCheck, zCont
18abb 65 78 74 2c 20 22 69 6e 76 61 6c 69 64 20 70 61  ext, "invalid pa
18abc 67 65 20 6e 75 6d 62 65 72 20 25 64 22 2c 20 69  ge number %d", i
18abd 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72  Page);.    retur
18abe 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  n 1;.  }.  if( p
18abf 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50 61  Check->anRef[iPa
18ac0 67 65 5d 3d 3d 31 20 29 7b 0a 20 20 20 20 63 68  ge]==1 ){.    ch
18ac1 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68  eckAppendMsg(pCh
18ac2 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22  eck, zContext, "
18ac3 32 6e 64 20 72 65 66 65 72 65 6e 63 65 20 74 6f  2nd reference to
18ac4 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 67 65   page %d", iPage
18ac5 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  );.    return 1;
18ac6 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 20 28  .  }.  return  (
18ac7 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50  pCheck->anRef[iP
18ac8 61 67 65 5d 2b 2b 29 3e 31 3b 0a 7d 0a 0a 23 69  age]++)>1;.}..#i
18ac9 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
18aca 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a  T_AUTOVACUUM./*.
18acb 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  ** Check that th
18acc 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70  e entry in the p
18acd 6f 69 6e 74 65 72 2d 6d 61 70 20 66 6f 72 20 70  ointer-map for p
18ace 61 67 65 20 69 43 68 69 6c 64 20 6d 61 70 73 20  age iChild maps 
18acf 74 6f 20 0a 2a 2a 20 70 61 67 65 20 69 50 61 72  to .** page iPar
18ad0 65 6e 74 2c 20 70 6f 69 6e 74 65 72 20 74 79 70  ent, pointer typ
18ad1 65 20 70 74 72 54 79 70 65 2e 20 49 66 20 6e 6f  e ptrType. If no
18ad2 74 2c 20 61 70 70 65 6e 64 20 61 6e 20 65 72 72  t, append an err
18ad3 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 74 6f  or message.** to
18ad4 20 70 43 68 65 63 6b 2e 0a 2a 2f 0a 73 74 61 74   pCheck..*/.stat
18ad5 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 74 72  ic void checkPtr
18ad6 6d 61 70 28 0a 20 20 49 6e 74 65 67 72 69 74 79  map(.  Integrity
18ad7 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 20 2f 2a  Ck *pCheck,   /*
18ad8 20 49 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b   Integrity check
18ad9 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 50 67   context */.  Pg
18ada 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20 20 20 20  no iChild,      
18adb 20 20 20 20 20 2f 2a 20 43 68 69 6c 64 20 70 61       /* Child pa
18adc 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 75  ge number */.  u
18add 38 20 65 54 79 70 65 2c 20 20 20 20 20 20 20 20  8 eType,        
18ade 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74 65        /* Expecte
18adf 64 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 74 79  d pointer map ty
18ae0 70 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 61  pe */.  Pgno iPa
18ae1 72 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20 2f  rent,          /
18ae2 2a 20 45 78 70 65 63 74 65 64 20 70 6f 69 6e 74  * Expected point
18ae3 65 72 20 6d 61 70 20 70 61 72 65 6e 74 20 70 61  er map parent pa
18ae4 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 63  ge number */.  c
18ae5 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 20 20 20  har *zContext   
18ae6 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74        /* Context
18ae7 20 64 65 73 63 72 69 70 74 69 6f 6e 20 28 75 73   description (us
18ae8 65 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 73 67  ed for error msg
18ae9 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  ) */.){.  int rc
18aea 3b 0a 20 20 75 38 20 65 50 74 72 6d 61 70 54 79  ;.  u8 ePtrmapTy
18aeb 70 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74 72 6d  pe;.  Pgno iPtrm
18aec 61 70 50 61 72 65 6e 74 3b 0a 0a 20 20 72 63 20  apParent;..  rc 
18aed 3d 20 70 74 72 6d 61 70 47 65 74 28 70 43 68 65  = ptrmapGet(pChe
18aee 63 6b 2d 3e 70 42 74 2c 20 69 43 68 69 6c 64 2c  ck->pBt, iChild,
18aef 20 26 65 50 74 72 6d 61 70 54 79 70 65 2c 20 26   &ePtrmapType, &
18af0 69 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b 0a  iPtrmapParent);.
18af1 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
18af2 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 72  _OK ){.    if( r
18af3 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
18af4 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f  || rc==SQLITE_IO
18af5 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 70 43 68 65  ERR_NOMEM ) pChe
18af6 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  ck->mallocFailed
18af7 20 3d 20 31 3b 0a 20 20 20 20 63 68 65 63 6b 41   = 1;.    checkA
18af8 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c  ppendMsg(pCheck,
18af9 20 7a 43 6f 6e 74 65 78 74 2c 20 22 46 61 69 6c   zContext, "Fail
18afa 65 64 20 74 6f 20 72 65 61 64 20 70 74 72 6d 61  ed to read ptrma
18afb 70 20 6b 65 79 3d 25 64 22 2c 20 69 43 68 69 6c  p key=%d", iChil
18afc 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  d);.    return;.
18afd 20 20 7d 0a 0a 20 20 69 66 28 20 65 50 74 72 6d    }..  if( ePtrm
18afe 61 70 54 79 70 65 21 3d 65 54 79 70 65 20 7c 7c  apType!=eType ||
18aff 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74 21 3d   iPtrmapParent!=
18b00 69 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 63  iParent ){.    c
18b01 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
18b02 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20  heck, zContext, 
18b03 0a 20 20 20 20 20 20 22 42 61 64 20 70 74 72 20  .      "Bad ptr 
18b04 6d 61 70 20 65 6e 74 72 79 20 6b 65 79 3d 25 64  map entry key=%d
18b05 20 65 78 70 65 63 74 65 64 3d 28 25 64 2c 25 64   expected=(%d,%d
18b06 29 20 67 6f 74 3d 28 25 64 2c 25 64 29 22 2c 20  ) got=(%d,%d)", 
18b07 0a 20 20 20 20 20 20 69 43 68 69 6c 64 2c 20 65  .      iChild, e
18b08 54 79 70 65 2c 20 69 50 61 72 65 6e 74 2c 20 65  Type, iParent, e
18b09 50 74 72 6d 61 70 54 79 70 65 2c 20 69 50 74 72  PtrmapType, iPtr
18b0a 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a  mapParent);.  }.
18b0b 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
18b0c 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72  Check the integr
18b0d 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c  ity of the freel
18b0e 69 73 74 20 6f 72 20 6f 66 20 61 6e 20 6f 76 65  ist or of an ove
18b0f 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 2e  rflow page list.
18b10 0a 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20  .** Verify that 
18b11 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
18b12 67 65 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 20  ges on the list 
18b13 69 73 20 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  is N..*/.static 
18b14 76 6f 69 64 20 63 68 65 63 6b 4c 69 73 74 28 0a  void checkList(.
18b15 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70    IntegrityCk *p
18b16 43 68 65 63 6b 2c 20 20 2f 2a 20 49 6e 74 65 67  Check,  /* Integ
18b17 72 69 74 79 20 63 68 65 63 6b 69 6e 67 20 63 6f  rity checking co
18b18 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69  ntext */.  int i
18b19 73 46 72 65 65 4c 69 73 74 2c 20 20 20 20 20 20  sFreeList,      
18b1a 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 66   /* True for a f
18b1b 72 65 65 6c 69 73 74 2e 20 20 46 61 6c 73 65 20  reelist.  False 
18b1c 66 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  for overflow pag
18b1d 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20  e list */.  int 
18b1e 69 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20  iPage,          
18b1f 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72    /* Page number
18b20 20 66 6f 72 20 66 69 72 73 74 20 70 61 67 65 20   for first page 
18b21 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20  in the list */. 
18b22 20 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20 20   int N,         
18b23 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74         /* Expect
18b24 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  ed number of pag
18b25 65 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a  es in the list *
18b26 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65  /.  char *zConte
18b27 78 74 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  xt        /* Con
18b28 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d  text for error m
18b29 65 73 73 61 67 65 73 20 2a 2f 0a 29 7b 0a 20 20  essages */.){.  
18b2a 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 65 78 70  int i;.  int exp
18b2b 65 63 74 65 64 20 3d 20 4e 3b 0a 20 20 69 6e 74  ected = N;.  int
18b2c 20 69 46 69 72 73 74 20 3d 20 69 50 61 67 65 3b   iFirst = iPage;
18b2d 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20  .  while( N-- > 
18b2e 30 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d 78 45  0 && pCheck->mxE
18b2f 72 72 20 29 7b 0a 20 20 20 20 44 62 50 61 67 65  rr ){.    DbPage
18b30 20 2a 70 4f 76 66 6c 50 61 67 65 3b 0a 20 20 20   *pOvflPage;.   
18b31 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
18b32 70 4f 76 66 6c 44 61 74 61 3b 0a 20 20 20 20 69  pOvflData;.    i
18b33 66 28 20 69 50 61 67 65 3c 31 20 29 7b 0a 20 20  f( iPage<1 ){.  
18b34 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
18b35 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74  sg(pCheck, zCont
18b36 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 22 25  ext,.         "%
18b37 64 20 6f 66 20 25 64 20 70 61 67 65 73 20 6d 69  d of %d pages mi
18b38 73 73 69 6e 67 20 66 72 6f 6d 20 6f 76 65 72 66  ssing from overf
18b39 6c 6f 77 20 6c 69 73 74 20 73 74 61 72 74 69 6e  low list startin
18b3a 67 20 61 74 20 25 64 22 2c 0a 20 20 20 20 20 20  g at %d",.      
18b3b 20 20 20 20 4e 2b 31 2c 20 65 78 70 65 63 74 65      N+1, expecte
18b3c 64 2c 20 69 46 69 72 73 74 29 3b 0a 20 20 20 20  d, iFirst);.    
18b3d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
18b3e 20 20 20 69 66 28 20 63 68 65 63 6b 52 65 66 28     if( checkRef(
18b3f 70 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a  pCheck, iPage, z
18b40 43 6f 6e 74 65 78 74 29 20 29 20 62 72 65 61 6b  Context) ) break
18b41 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
18b42 33 50 61 67 65 72 47 65 74 28 70 43 68 65 63 6b  3PagerGet(pCheck
18b43 2d 3e 70 50 61 67 65 72 2c 20 28 50 67 6e 6f 29  ->pPager, (Pgno)
18b44 69 50 61 67 65 2c 20 26 70 4f 76 66 6c 50 61 67  iPage, &pOvflPag
18b45 65 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63  e) ){.      chec
18b46 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63  kAppendMsg(pChec
18b47 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 66 61  k, zContext, "fa
18b48 69 6c 65 64 20 74 6f 20 67 65 74 20 70 61 67 65  iled to get page
18b49 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 20   %d", iPage);.  
18b4a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
18b4b 0a 20 20 20 20 70 4f 76 66 6c 44 61 74 61 20 3d  .    pOvflData =
18b4c 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
18b4d 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  *)sqlite3PagerGe
18b4e 74 44 61 74 61 28 70 4f 76 66 6c 50 61 67 65 29  tData(pOvflPage)
18b4f 3b 0a 20 20 20 20 69 66 28 20 69 73 46 72 65 65  ;.    if( isFree
18b50 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 69 6e  List ){.      in
18b51 74 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26  t n = get4byte(&
18b52 70 4f 76 66 6c 44 61 74 61 5b 34 5d 29 3b 0a 23  pOvflData[4]);.#
18b53 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
18b54 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
18b55 20 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e      if( pCheck->
18b56 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
18b57 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b  ){.        check
18b58 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69  Ptrmap(pCheck, i
18b59 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45  Page, PTRMAP_FRE
18b5a 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65  EPAGE, 0, zConte
18b5b 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  xt);.      }.#en
18b5c 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 6e 3e  dif.      if( n>
18b5d 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 75 73 61  pCheck->pBt->usa
18b5e 62 6c 65 53 69 7a 65 2f 34 2d 32 20 29 7b 0a 20  bleSize/4-2 ){. 
18b5f 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65         checkAppe
18b60 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43  ndMsg(pCheck, zC
18b61 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20  ontext,.        
18b62 20 20 20 22 66 72 65 65 6c 69 73 74 20 6c 65 61     "freelist lea
18b63 66 20 63 6f 75 6e 74 20 74 6f 6f 20 62 69 67 20  f count too big 
18b64 6f 6e 20 70 61 67 65 20 25 64 22 2c 20 69 50 61  on page %d", iPa
18b65 67 65 29 3b 0a 20 20 20 20 20 20 20 20 4e 2d 2d  ge);.        N--
18b66 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
18b67 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20         for(i=0; 
18b68 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  i<n; i++){.     
18b69 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50       Pgno iFreeP
18b6a 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26  age = get4byte(&
18b6b 70 4f 76 66 6c 44 61 74 61 5b 38 2b 69 2a 34 5d  pOvflData[8+i*4]
18b6c 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
18b6d 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
18b6e 4d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  M.          if( 
18b6f 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74  pCheck->pBt->aut
18b70 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20  oVacuum ){.     
18b71 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d         checkPtrm
18b72 61 70 28 70 43 68 65 63 6b 2c 20 69 46 72 65 65  ap(pCheck, iFree
18b73 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45  Page, PTRMAP_FRE
18b74 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65  EPAGE, 0, zConte
18b75 78 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  xt);.          }
18b76 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20  .#endif.        
18b77 20 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63    checkRef(pChec
18b78 6b 2c 20 69 46 72 65 65 50 61 67 65 2c 20 7a 43  k, iFreePage, zC
18b79 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20  ontext);.       
18b7a 20 7d 0a 20 20 20 20 20 20 20 20 4e 20 2d 3d 20   }.        N -= 
18b7b 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  n;.      }.    }
18b7c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
18b7d 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
18b7e 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20      else{.      
18b7f 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 62  /* If this datab
18b80 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74  ase supports aut
18b81 6f 2d 76 61 63 75 75 6d 20 61 6e 64 20 69 50 61  o-vacuum and iPa
18b82 67 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61  ge is not the la
18b83 73 74 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65  st.      ** page
18b84 20 69 6e 20 74 68 69 73 20 6f 76 65 72 66 6c 6f   in this overflo
18b85 77 20 6c 69 73 74 2c 20 63 68 65 63 6b 20 74 68  w list, check th
18b86 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  at the pointer-m
18b87 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 20  ap entry for.   
18b88 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77     ** the follow
18b89 69 6e 67 20 70 61 67 65 20 6d 61 74 63 68 65 73  ing page matches
18b8a 20 69 50 61 67 65 2e 0a 20 20 20 20 20 20 2a 2f   iPage..      */
18b8b 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 65 63  .      if( pChec
18b8c 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  k->pBt->autoVacu
18b8d 75 6d 20 26 26 20 4e 3e 30 20 29 7b 0a 20 20 20  um && N>0 ){.   
18b8e 20 20 20 20 20 69 20 3d 20 67 65 74 34 62 79 74       i = get4byt
18b8f 65 28 70 4f 76 66 6c 44 61 74 61 29 3b 0a 20 20  e(pOvflData);.  
18b90 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61        checkPtrma
18b91 70 28 70 43 68 65 63 6b 2c 20 69 2c 20 50 54 52  p(pCheck, i, PTR
18b92 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 2c 20 69  MAP_OVERFLOW2, i
18b93 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b  Page, zContext);
18b94 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
18b95 65 6e 64 69 66 0a 20 20 20 20 69 50 61 67 65 20  endif.    iPage 
18b96 3d 20 67 65 74 34 62 79 74 65 28 70 4f 76 66 6c  = get4byte(pOvfl
18b97 44 61 74 61 29 3b 0a 20 20 20 20 73 71 6c 69 74  Data);.    sqlit
18b98 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 4f 76  e3PagerUnref(pOv
18b99 66 6c 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 23  flPage);.  }.}.#
18b9a 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
18b9b 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43  OMIT_INTEGRITY_C
18b9c 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  HECK */..#ifndef
18b9d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54   SQLITE_OMIT_INT
18b9e 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a  EGRITY_CHECK./*.
18b9f 2a 2a 20 44 6f 20 76 61 72 69 6f 75 73 20 73 61  ** Do various sa
18ba0 6e 69 74 79 20 63 68 65 63 6b 73 20 6f 6e 20 61  nity checks on a
18ba1 20 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 20   single page of 
18ba2 61 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 0a  a tree.  Return.
18ba3 2a 2a 20 74 68 65 20 74 72 65 65 20 64 65 70 74  ** the tree dept
18ba4 68 2e 20 20 52 6f 6f 74 20 70 61 67 65 73 20 72  h.  Root pages r
18ba5 65 74 75 72 6e 20 30 2e 20 20 50 61 72 65 6e 74  eturn 0.  Parent
18ba6 73 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 0a  s of root pages.
18ba7 2a 2a 20 72 65 74 75 72 6e 20 31 2c 20 61 6e 64  ** return 1, and
18ba8 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 20 0a 2a   so forth..** .*
18ba9 2a 20 54 68 65 73 65 20 63 68 65 63 6b 73 20 61  * These checks a
18baa 72 65 20 64 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20  re done:.**.**  
18bab 20 20 20 20 31 2e 20 20 4d 61 6b 65 20 73 75 72      1.  Make sur
18bac 65 20 74 68 61 74 20 63 65 6c 6c 73 20 61 6e 64  e that cells and
18bad 20 66 72 65 65 62 6c 6f 63 6b 73 20 64 6f 20 6e   freeblocks do n
18bae 6f 74 20 6f 76 65 72 6c 61 70 0a 2a 2a 20 20 20  ot overlap.**   
18baf 20 20 20 20 20 20 20 62 75 74 20 63 6f 6d 62 69         but combi
18bb0 6e 65 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c 79  ne to completely
18bb1 20 63 6f 76 65 72 20 74 68 65 20 70 61 67 65 2e   cover the page.
18bb2 0a 2a 2a 20 20 4e 4f 20 20 32 2e 20 20 4d 61 6b  .**  NO  2.  Mak
18bb3 65 20 73 75 72 65 20 63 65 6c 6c 20 6b 65 79 73  e sure cell keys
18bb4 20 61 72 65 20 69 6e 20 6f 72 64 65 72 2e 0a 2a   are in order..*
18bb5 2a 20 20 4e 4f 20 20 33 2e 20 20 4d 61 6b 65 20  *  NO  3.  Make 
18bb6 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 73 20 6c  sure no key is l
18bb7 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ess than or equa
18bb8 6c 20 74 6f 20 7a 4c 6f 77 65 72 42 6f 75 6e 64  l to zLowerBound
18bb9 2e 0a 2a 2a 20 20 4e 4f 20 20 34 2e 20 20 4d 61  ..**  NO  4.  Ma
18bba 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 79 20 69  ke sure no key i
18bbb 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f  s greater than o
18bbc 72 20 65 71 75 61 6c 20 74 6f 20 7a 55 70 70 65  r equal to zUppe
18bbd 72 42 6f 75 6e 64 2e 0a 2a 2a 20 20 20 20 20 20  rBound..**      
18bbe 35 2e 20 20 43 68 65 63 6b 20 74 68 65 20 69 6e  5.  Check the in
18bbf 74 65 67 72 69 74 79 20 6f 66 20 6f 76 65 72 66  tegrity of overf
18bc0 6c 6f 77 20 70 61 67 65 73 2e 0a 2a 2a 20 20 20  low pages..**   
18bc1 20 20 20 36 2e 20 20 52 65 63 75 72 73 69 76 65     6.  Recursive
18bc2 6c 79 20 63 61 6c 6c 20 63 68 65 63 6b 54 72 65  ly call checkTre
18bc3 65 50 61 67 65 20 6f 6e 20 61 6c 6c 20 63 68 69  ePage on all chi
18bc4 6c 64 72 65 6e 2e 0a 2a 2a 20 20 20 20 20 20 37  ldren..**      7
18bc5 2e 20 20 56 65 72 69 66 79 20 74 68 61 74 20 74  .  Verify that t
18bc6 68 65 20 64 65 70 74 68 20 6f 66 20 61 6c 6c 20  he depth of all 
18bc7 63 68 69 6c 64 72 65 6e 20 69 73 20 74 68 65 20  children is the 
18bc8 73 61 6d 65 2e 0a 2a 2a 20 20 20 20 20 20 38 2e  same..**      8.
18bc9 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73    Make sure this
18bca 20 70 61 67 65 20 69 73 20 61 74 20 6c 65 61 73   page is at leas
18bcb 74 20 33 33 25 20 66 75 6c 6c 20 6f 72 20 65 6c  t 33% full or el
18bcc 73 65 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 20  se it is.**     
18bcd 20 20 20 20 20 74 68 65 20 72 6f 6f 74 20 6f 66       the root of
18bce 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 73 74   the tree..*/.st
18bcf 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 54 72  atic int checkTr
18bd0 65 65 50 61 67 65 28 0a 20 20 49 6e 74 65 67 72  eePage(.  Integr
18bd1 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20  ityCk *pCheck,  
18bd2 2f 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 74  /* Context for t
18bd3 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 20  he sanity check 
18bd4 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67 65 2c 20  */.  int iPage, 
18bd5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
18bd6 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
18bd7 20 70 61 67 65 20 74 6f 20 63 68 65 63 6b 20 2a   page to check *
18bd8 2f 0a 20 20 63 68 61 72 20 2a 7a 50 61 72 65 6e  /.  char *zParen
18bd9 74 43 6f 6e 74 65 78 74 20 20 2f 2a 20 50 61 72  tContext  /* Par
18bda 65 6e 74 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 29  ent context */.)
18bdb 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  {.  MemPage *pPa
18bdc 67 65 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63 2c  ge;.  int i, rc,
18bdd 20 64 65 70 74 68 2c 20 64 32 2c 20 70 67 6e 6f   depth, d2, pgno
18bde 2c 20 63 6e 74 3b 0a 20 20 69 6e 74 20 68 64 72  , cnt;.  int hdr
18bdf 2c 20 63 65 6c 6c 53 74 61 72 74 3b 0a 20 20 69  , cellStart;.  i
18be0 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 75 38 20 2a  nt nCell;.  u8 *
18be1 64 61 74 61 3b 0a 20 20 42 74 53 68 61 72 65 64  data;.  BtShared
18be2 20 2a 70 42 74 3b 0a 20 20 69 6e 74 20 75 73 61   *pBt;.  int usa
18be3 62 6c 65 53 69 7a 65 3b 0a 20 20 63 68 61 72 20  bleSize;.  char 
18be4 7a 43 6f 6e 74 65 78 74 5b 31 30 30 5d 3b 0a 20  zContext[100];. 
18be5 20 63 68 61 72 20 2a 68 69 74 20 3d 20 30 3b 0a   char *hit = 0;.
18be6 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  .  sqlite3_snpri
18be7 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74  ntf(sizeof(zCont
18be8 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20  ext), zContext, 
18be9 22 50 61 67 65 20 25 64 3a 20 22 2c 20 69 50 61  "Page %d: ", iPa
18bea 67 65 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  ge);..  /* Check
18beb 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 65   that the page e
18bec 78 69 73 74 73 0a 20 20 2a 2f 0a 20 20 70 42 74  xists.  */.  pBt
18bed 20 3d 20 70 43 68 65 63 6b 2d 3e 70 42 74 3b 0a   = pCheck->pBt;.
18bee 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70    usableSize = p
18bef 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a  Bt->usableSize;.
18bf0 20 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20 29    if( iPage==0 )
18bf1 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
18bf2 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 6b   checkRef(pCheck
18bf3 2c 20 69 50 61 67 65 2c 20 7a 50 61 72 65 6e 74  , iPage, zParent
18bf4 43 6f 6e 74 65 78 74 29 20 29 20 72 65 74 75 72  Context) ) retur
18bf5 6e 20 30 3b 0a 20 20 69 66 28 20 28 72 63 20 3d  n 0;.  if( (rc =
18bf6 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42   btreeGetPage(pB
18bf7 74 2c 20 28 50 67 6e 6f 29 69 50 61 67 65 2c 20  t, (Pgno)iPage, 
18bf8 26 70 50 61 67 65 2c 20 30 29 29 21 3d 30 20 29  &pPage, 0))!=0 )
18bf9 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e  {.    checkAppen
18bfa 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f  dMsg(pCheck, zCo
18bfb 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 22 75  ntext,.       "u
18bfc 6e 61 62 6c 65 20 74 6f 20 67 65 74 20 74 68 65  nable to get the
18bfd 20 70 61 67 65 2e 20 65 72 72 6f 72 20 63 6f 64   page. error cod
18bfe 65 3d 25 64 22 2c 20 72 63 29 3b 0a 20 20 20 20  e=%d", rc);.    
18bff 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20  return 0;.  }.. 
18c00 20 2f 2a 20 43 6c 65 61 72 20 4d 65 6d 50 61 67   /* Clear MemPag
18c01 65 2e 69 73 49 6e 69 74 20 74 6f 20 6d 61 6b 65  e.isInit to make
18c02 20 73 75 72 65 20 74 68 65 20 63 6f 72 72 75 70   sure the corrup
18c03 74 69 6f 6e 20 64 65 74 65 63 74 69 6f 6e 20 63  tion detection c
18c04 6f 64 65 20 69 6e 0a 20 20 2a 2a 20 62 74 72 65  ode in.  ** btre
18c05 65 49 6e 69 74 50 61 67 65 28 29 20 69 73 20 65  eInitPage() is e
18c06 78 65 63 75 74 65 64 2e 20 20 2a 2f 0a 20 20 70  xecuted.  */.  p
18c07 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30  Page->isInit = 0
18c08 3b 0a 20 20 69 66 28 20 28 72 63 20 3d 20 62 74  ;.  if( (rc = bt
18c09 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67  reeInitPage(pPag
18c0a 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 61 73  e))!=0 ){.    as
18c0b 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
18c0c 5f 43 4f 52 52 55 50 54 20 29 3b 20 20 2f 2a 20  _CORRUPT );  /* 
18c0d 54 68 65 20 6f 6e 6c 79 20 70 6f 73 73 69 62 6c  The only possibl
18c0e 65 20 65 72 72 6f 72 20 66 72 6f 6d 20 49 6e 69  e error from Ini
18c0f 74 50 61 67 65 20 2a 2f 0a 20 20 20 20 63 68 65  tPage */.    che
18c10 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65  ckAppendMsg(pChe
18c11 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20  ck, zContext, . 
18c12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18c13 20 20 22 62 74 72 65 65 49 6e 69 74 50 61 67 65    "btreeInitPage
18c14 28 29 20 72 65 74 75 72 6e 73 20 65 72 72 6f 72  () returns error
18c15 20 63 6f 64 65 20 25 64 22 2c 20 72 63 29 3b 0a   code %d", rc);.
18c16 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
18c17 70 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75  pPage);.    retu
18c18 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rn 0;.  }..  /* 
18c19 43 68 65 63 6b 20 6f 75 74 20 61 6c 6c 20 74 68  Check out all th
18c1a 65 20 63 65 6c 6c 73 2e 0a 20 20 2a 2f 0a 20 20  e cells..  */.  
18c1b 64 65 70 74 68 20 3d 20 30 3b 0a 20 20 66 6f 72  depth = 0;.  for
18c1c 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e  (i=0; i<pPage->n
18c1d 43 65 6c 6c 20 26 26 20 70 43 68 65 63 6b 2d 3e  Cell && pCheck->
18c1e 6d 78 45 72 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  mxErr; i++){.   
18c1f 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20   u8 *pCell;.    
18c20 75 33 32 20 73 7a 3b 0a 20 20 20 20 43 65 6c 6c  u32 sz;.    Cell
18c21 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 20 20  Info info;..    
18c22 2f 2a 20 43 68 65 63 6b 20 70 61 79 6c 6f 61 64  /* Check payload
18c23 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 0a   overflow pages.
18c24 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
18c25 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65  e3_snprintf(size
18c26 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 7a 43  of(zContext), zC
18c27 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20  ontext,.        
18c28 20 20 20 20 20 22 4f 6e 20 74 72 65 65 20 70 61       "On tree pa
18c29 67 65 20 25 64 20 63 65 6c 6c 20 25 64 3a 20 22  ge %d cell %d: "
18c2a 2c 20 69 50 61 67 65 2c 20 69 29 3b 0a 20 20 20  , iPage, i);.   
18c2b 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c   pCell = findCel
18c2c 6c 28 70 50 61 67 65 2c 69 29 3b 0a 20 20 20 20  l(pPage,i);.    
18c2d 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74  btreeParseCellPt
18c2e 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20  r(pPage, pCell, 
18c2f 26 69 6e 66 6f 29 3b 0a 20 20 20 20 73 7a 20 3d  &info);.    sz =
18c30 20 69 6e 66 6f 2e 6e 44 61 74 61 3b 0a 20 20 20   info.nData;.   
18c31 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69 6e 74   if( !pPage->int
18c32 4b 65 79 20 29 20 73 7a 20 2b 3d 20 28 69 6e 74  Key ) sz += (int
18c33 29 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20  )info.nKey;.    
18c34 61 73 73 65 72 74 28 20 73 7a 3d 3d 69 6e 66 6f  assert( sz==info
18c35 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20 20  .nPayload );.   
18c36 20 69 66 28 20 28 73 7a 3e 69 6e 66 6f 2e 6e 4c   if( (sz>info.nL
18c37 6f 63 61 6c 29 20 0a 20 20 20 20 20 26 26 20 28  ocal) .     && (
18c38 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65  &pCell[info.iOve
18c39 72 66 6c 6f 77 5d 3c 3d 26 70 50 61 67 65 2d 3e  rflow]<=&pPage->
18c3a 61 44 61 74 61 5b 70 42 74 2d 3e 75 73 61 62 6c  aData[pBt->usabl
18c3b 65 53 69 7a 65 5d 29 0a 20 20 20 20 29 7b 0a 20  eSize]).    ){. 
18c3c 20 20 20 20 20 69 6e 74 20 6e 50 61 67 65 20 3d       int nPage =
18c3d 20 28 73 7a 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63   (sz - info.nLoc
18c3e 61 6c 20 2b 20 75 73 61 62 6c 65 53 69 7a 65 20  al + usableSize 
18c3f 2d 20 35 29 2f 28 75 73 61 62 6c 65 53 69 7a 65  - 5)/(usableSize
18c40 20 2d 20 34 29 3b 0a 20 20 20 20 20 20 50 67 6e   - 4);.      Pgn
18c41 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 67 65 74  o pgnoOvfl = get
18c42 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66  4byte(&pCell[inf
18c43 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 23  o.iOverflow]);.#
18c44 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
18c45 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
18c46 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74      if( pBt->aut
18c47 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20  oVacuum ){.     
18c48 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70     checkPtrmap(p
18c49 43 68 65 63 6b 2c 20 70 67 6e 6f 4f 76 66 6c 2c  Check, pgnoOvfl,
18c4a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
18c4b 31 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65  1, iPage, zConte
18c4c 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  xt);.      }.#en
18c4d 64 69 66 0a 20 20 20 20 20 20 63 68 65 63 6b 4c  dif.      checkL
18c4e 69 73 74 28 70 43 68 65 63 6b 2c 20 30 2c 20 70  ist(pCheck, 0, p
18c4f 67 6e 6f 4f 76 66 6c 2c 20 6e 50 61 67 65 2c 20  gnoOvfl, nPage, 
18c50 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d  zContext);.    }
18c51 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 73  ..    /* Check s
18c52 61 6e 69 74 79 20 6f 66 20 6c 65 66 74 20 63 68  anity of left ch
18c53 69 6c 64 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f  ild page..    */
18c54 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d  .    if( !pPage-
18c55 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 70  >leaf ){.      p
18c56 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70  gno = get4byte(p
18c57 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53  Cell);.#ifndef S
18c58 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
18c59 41 43 55 55 4d 0a 20 20 20 20 20 20 69 66 28 20  ACUUM.      if( 
18c5a 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
18c5b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b  ){.        check
18c5c 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70  Ptrmap(pCheck, p
18c5d 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45  gno, PTRMAP_BTRE
18c5e 45 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65  E, iPage, zConte
18c5f 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  xt);.      }.#en
18c60 64 69 66 0a 20 20 20 20 20 20 64 32 20 3d 20 63  dif.      d2 = c
18c61 68 65 63 6b 54 72 65 65 50 61 67 65 28 70 43 68  heckTreePage(pCh
18c62 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74  eck, pgno, zCont
18c63 65 78 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ext);.      if( 
18c64 69 3e 30 20 26 26 20 64 32 21 3d 64 65 70 74 68  i>0 && d2!=depth
18c65 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63   ){.        chec
18c66 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63  kAppendMsg(pChec
18c67 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 43 68  k, zContext, "Ch
18c68 69 6c 64 20 70 61 67 65 20 64 65 70 74 68 20 64  ild page depth d
18c69 69 66 66 65 72 73 22 29 3b 0a 20 20 20 20 20 20  iffers");.      
18c6a 7d 0a 20 20 20 20 20 20 64 65 70 74 68 20 3d 20  }.      depth = 
18c6b 64 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  d2;.    }.  }.  
18c6c 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66  if( !pPage->leaf
18c6d 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67   ){.    pgno = g
18c6e 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e  et4byte(&pPage->
18c6f 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72  aData[pPage->hdr
18c70 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20  Offset+8]);.    
18c71 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
18c72 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74  (sizeof(zContext
18c73 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20  ), zContext, .  
18c74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18c75 20 20 20 22 4f 6e 20 70 61 67 65 20 25 64 20 61     "On page %d a
18c76 74 20 72 69 67 68 74 20 63 68 69 6c 64 3a 20 22  t right child: "
18c77 2c 20 69 50 61 67 65 29 3b 0a 23 69 66 6e 64 65  , iPage);.#ifnde
18c78 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
18c79 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28  TOVACUUM.    if(
18c7a 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
18c7b 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 50   ){.      checkP
18c7c 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 67  trmap(pCheck, pg
18c7d 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45  no, PTRMAP_BTREE
18c7e 2c 20 69 50 61 67 65 2c 20 30 29 3b 0a 20 20 20  , iPage, 0);.   
18c7f 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 68   }.#endif.    ch
18c80 65 63 6b 54 72 65 65 50 61 67 65 28 70 43 68 65  eckTreePage(pChe
18c81 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74 65  ck, pgno, zConte
18c82 78 74 29 3b 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20  xt);.  }. .  /* 
18c83 43 68 65 63 6b 20 66 6f 72 20 63 6f 6d 70 6c 65  Check for comple
18c84 74 65 20 63 6f 76 65 72 61 67 65 20 6f 66 20 74  te coverage of t
18c85 68 65 20 70 61 67 65 0a 20 20 2a 2f 0a 20 20 64  he page.  */.  d
18c86 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61  ata = pPage->aDa
18c87 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67  ta;.  hdr = pPag
18c88 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20  e->hdrOffset;.  
18c89 68 69 74 20 3d 20 73 71 6c 69 74 65 33 50 61 67  hit = sqlite3Pag
18c8a 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 61  eMalloc( pBt->pa
18c8b 67 65 53 69 7a 65 20 29 3b 0a 20 20 69 66 28 20  geSize );.  if( 
18c8c 68 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 43  hit==0 ){.    pC
18c8d 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  heck->mallocFail
18c8e 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b  ed = 1;.  }else{
18c8f 0a 20 20 20 20 75 31 36 20 63 6f 6e 74 65 6e 74  .    u16 content
18c90 4f 66 66 73 65 74 20 3d 20 67 65 74 32 62 79 74  Offset = get2byt
18c91 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b  e(&data[hdr+5]);
18c92 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 6f 6e  .    assert( con
18c93 74 65 6e 74 4f 66 66 73 65 74 3c 3d 75 73 61 62  tentOffset<=usab
18c94 6c 65 53 69 7a 65 20 29 3b 20 20 2f 2a 20 45 6e  leSize );  /* En
18c95 66 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 49  forced by btreeI
18c96 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20  nitPage() */.   
18c97 20 6d 65 6d 73 65 74 28 68 69 74 2b 63 6f 6e 74   memset(hit+cont
18c98 65 6e 74 4f 66 66 73 65 74 2c 20 30 2c 20 75 73  entOffset, 0, us
18c99 61 62 6c 65 53 69 7a 65 2d 63 6f 6e 74 65 6e 74  ableSize-content
18c9a 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 6d 65 6d  Offset);.    mem
18c9b 73 65 74 28 68 69 74 2c 20 31 2c 20 63 6f 6e 74  set(hit, 1, cont
18c9c 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20 20 20 20  entOffset);.    
18c9d 6e 43 65 6c 6c 20 3d 20 67 65 74 32 62 79 74 65  nCell = get2byte
18c9e 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a  (&data[hdr+3]);.
18c9f 20 20 20 20 63 65 6c 6c 53 74 61 72 74 20 3d 20      cellStart = 
18ca0 68 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 61  hdr + 12 - 4*pPa
18ca1 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 66 6f  ge->leaf;.    fo
18ca2 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20  r(i=0; i<nCell; 
18ca3 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  i++){.      int 
18ca4 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  pc = get2byte(&d
18ca5 61 74 61 5b 63 65 6c 6c 53 74 61 72 74 2b 69 2a  ata[cellStart+i*
18ca6 32 5d 29 3b 0a 20 20 20 20 20 20 75 31 36 20 73  2]);.      u16 s
18ca7 69 7a 65 20 3d 20 31 30 32 34 3b 0a 20 20 20 20  ize = 1024;.    
18ca8 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 69    int j;.      i
18ca9 66 28 20 70 63 3c 3d 75 73 61 62 6c 65 53 69 7a  f( pc<=usableSiz
18caa 65 2d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 73  e-4 ){.        s
18cab 69 7a 65 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74  ize = cellSizePt
18cac 72 28 70 50 61 67 65 2c 20 26 64 61 74 61 5b 70  r(pPage, &data[p
18cad 63 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  c]);.      }.   
18cae 20 20 20 69 66 28 20 28 70 63 2b 73 69 7a 65 2d     if( (pc+size-
18caf 31 29 3e 3d 75 73 61 62 6c 65 53 69 7a 65 20 29  1)>=usableSize )
18cb0 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41  {.        checkA
18cb1 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c  ppendMsg(pCheck,
18cb2 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20   0, .           
18cb3 20 22 43 6f 72 72 75 70 74 69 6f 6e 20 64 65 74   "Corruption det
18cb4 65 63 74 65 64 20 69 6e 20 63 65 6c 6c 20 25 64  ected in cell %d
18cb5 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 69 2c 69   on page %d",i,i
18cb6 50 61 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 7d  Page,0);.      }
18cb7 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f  else{.        fo
18cb8 72 28 6a 3d 70 63 2b 73 69 7a 65 2d 31 3b 20 6a  r(j=pc+size-1; j
18cb9 3e 3d 70 63 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a  >=pc; j--) hit[j
18cba 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ]++;.      }.   
18cbb 20 7d 0a 20 20 20 20 69 20 3d 20 67 65 74 32 62   }.    i = get2b
18cbc 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d  yte(&data[hdr+1]
18cbd 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 3e  );.    while( i>
18cbe 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73  0 ){.      int s
18cbf 69 7a 65 2c 20 6a 3b 0a 20 20 20 20 20 20 61 73  ize, j;.      as
18cc0 73 65 72 74 28 20 69 3c 3d 75 73 61 62 6c 65 53  sert( i<=usableS
18cc1 69 7a 65 2d 34 20 29 3b 20 20 20 20 20 2f 2a 20  ize-4 );     /* 
18cc2 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74 72 65  Enforced by btre
18cc3 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20  eInitPage() */. 
18cc4 20 20 20 20 20 73 69 7a 65 20 3d 20 67 65 74 32       size = get2
18cc5 62 79 74 65 28 26 64 61 74 61 5b 69 2b 32 5d 29  byte(&data[i+2])
18cc6 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
18cc7 69 2b 73 69 7a 65 3c 3d 75 73 61 62 6c 65 53 69  i+size<=usableSi
18cc8 7a 65 20 29 3b 20 20 2f 2a 20 45 6e 66 6f 72 63  ze );  /* Enforc
18cc9 65 64 20 62 79 20 62 74 72 65 65 49 6e 69 74 50  ed by btreeInitP
18cca 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 66  age() */.      f
18ccb 6f 72 28 6a 3d 69 2b 73 69 7a 65 2d 31 3b 20 6a  or(j=i+size-1; j
18ccc 3e 3d 69 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d  >=i; j--) hit[j]
18ccd 2b 2b 3b 0a 20 20 20 20 20 20 6a 20 3d 20 67 65  ++;.      j = ge
18cce 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 5d 29  t2byte(&data[i])
18ccf 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
18cd0 6a 3d 3d 30 20 7c 7c 20 6a 3e 69 2b 73 69 7a 65  j==0 || j>i+size
18cd1 20 29 3b 20 20 2f 2a 20 45 6e 66 6f 72 63 65 64   );  /* Enforced
18cd2 20 62 79 20 62 74 72 65 65 49 6e 69 74 50 61 67   by btreeInitPag
18cd3 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 61 73 73  e() */.      ass
18cd4 65 72 74 28 20 6a 3c 3d 75 73 61 62 6c 65 53 69  ert( j<=usableSi
18cd5 7a 65 2d 34 20 29 3b 20 20 20 2f 2a 20 45 6e 66  ze-4 );   /* Enf
18cd6 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 49 6e  orced by btreeIn
18cd7 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 20  itPage() */.    
18cd8 20 20 69 20 3d 20 6a 3b 0a 20 20 20 20 7d 0a 20    i = j;.    }. 
18cd9 20 20 20 66 6f 72 28 69 3d 63 6e 74 3d 30 3b 20     for(i=cnt=0; 
18cda 69 3c 75 73 61 62 6c 65 53 69 7a 65 3b 20 69 2b  i<usableSize; i+
18cdb 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 68 69  +){.      if( hi
18cdc 74 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20  t[i]==0 ){.     
18cdd 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20     cnt++;.      
18cde 7d 65 6c 73 65 20 69 66 28 20 68 69 74 5b 69 5d  }else if( hit[i]
18cdf 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68  >1 ){.        ch
18ce0 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68  eckAppendMsg(pCh
18ce1 65 63 6b 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  eck, 0,.        
18ce2 20 20 22 4d 75 6c 74 69 70 6c 65 20 75 73 65 73    "Multiple uses
18ce3 20 66 6f 72 20 62 79 74 65 20 25 64 20 6f 66 20   for byte %d of 
18ce4 70 61 67 65 20 25 64 22 2c 20 69 2c 20 69 50 61  page %d", i, iPa
18ce5 67 65 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  ge);.        bre
18ce6 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
18ce7 7d 0a 20 20 20 20 69 66 28 20 63 6e 74 21 3d 64  }.    if( cnt!=d
18ce8 61 74 61 5b 68 64 72 2b 37 5d 20 29 7b 0a 20 20  ata[hdr+7] ){.  
18ce9 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
18cea 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a 20  sg(pCheck, 0, . 
18ceb 20 20 20 20 20 20 20 20 20 22 46 72 61 67 6d 65           "Fragme
18cec 6e 74 61 74 69 6f 6e 20 6f 66 20 25 64 20 62 79  ntation of %d by
18ced 74 65 73 20 72 65 70 6f 72 74 65 64 20 61 73 20  tes reported as 
18cee 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 0a  %d on page %d",.
18cef 20 20 20 20 20 20 20 20 20 20 63 6e 74 2c 20 64            cnt, d
18cf0 61 74 61 5b 68 64 72 2b 37 5d 2c 20 69 50 61 67  ata[hdr+7], iPag
18cf1 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e);.    }.  }.  
18cf2 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28  sqlite3PageFree(
18cf3 68 69 74 29 3b 0a 20 20 72 65 6c 65 61 73 65 50  hit);.  releaseP
18cf4 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65  age(pPage);.  re
18cf5 74 75 72 6e 20 64 65 70 74 68 2b 31 3b 0a 7d 0a  turn depth+1;.}.
18cf6 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
18cf7 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f  _OMIT_INTEGRITY_
18cf8 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65  CHECK */..#ifnde
18cf9 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
18cfa 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a  TEGRITY_CHECK./*
18cfb 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
18cfc 20 64 6f 65 73 20 61 20 63 6f 6d 70 6c 65 74 65   does a complete
18cfd 20 63 68 65 63 6b 20 6f 66 20 74 68 65 20 67 69   check of the gi
18cfe 76 65 6e 20 42 54 72 65 65 20 66 69 6c 65 2e 20  ven BTree file. 
18cff 20 61 52 6f 6f 74 5b 5d 20 69 73 0a 2a 2a 20 61   aRoot[] is.** a
18d00 6e 20 61 72 72 61 79 20 6f 66 20 70 61 67 65 73  n array of pages
18d01 20 6e 75 6d 62 65 72 73 20 77 65 72 65 20 65 61   numbers were ea
18d02 63 68 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69  ch page number i
18d03 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  s the root page 
18d04 6f 66 0a 2a 2a 20 61 20 74 61 62 6c 65 2e 20 20  of.** a table.  
18d05 6e 52 6f 6f 74 20 69 73 20 74 68 65 20 6e 75 6d  nRoot is the num
18d06 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
18d07 6e 20 61 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 41  n aRoot..**.** A
18d08 20 72 65 61 64 2d 6f 6e 6c 79 20 6f 72 20 72 65   read-only or re
18d09 61 64 2d 77 72 69 74 65 20 74 72 61 6e 73 61 63  ad-write transac
18d0a 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 6f 70 65  tion must be ope
18d0b 6e 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69  ned before calli
18d0c 6e 67 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74  ng.** this funct
18d0d 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65  ion..**.** Write
18d0e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
18d0f 72 72 6f 72 20 73 65 65 6e 20 69 6e 20 2a 70 6e  rror seen in *pn
18d10 45 72 72 2e 20 20 45 78 63 65 70 74 20 66 6f 72  Err.  Except for
18d11 20 73 6f 6d 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20   some memory.** 
18d12 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72  allocation error
18d13 73 2c 20 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  s,  an error mes
18d14 73 61 67 65 20 68 65 6c 64 20 69 6e 20 6d 65 6d  sage held in mem
18d15 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
18d16 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 69 73 20 72  m.** malloc is r
18d17 65 74 75 72 6e 65 64 20 69 66 20 2a 70 6e 45 72  eturned if *pnEr
18d18 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20  r is non-zero.  
18d19 49 66 20 2a 70 6e 45 72 72 3d 3d 30 20 74 68 65  If *pnErr==0 the
18d1a 6e 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20 72 65 74  n NULL is.** ret
18d1b 75 72 6e 65 64 2e 20 20 49 66 20 61 20 6d 65 6d  urned.  If a mem
18d1c 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65  ory allocation e
18d1d 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c  rror occurs, NUL
18d1e 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  L is returned..*
18d1f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18d20 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74   char *sqlite3Bt
18d21 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 63  reeIntegrityChec
18d22 6b 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 20  k(.  Btree *p,  
18d23 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20     /* The btree 
18d24 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a 2f  to be checked */
18d25 0a 20 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 20  .  int *aRoot,  
18d26 20 2f 2a 20 41 6e 20 61 72 72 61 79 20 6f 66 20   /* An array of 
18d27 72 6f 6f 74 20 70 61 67 65 73 20 6e 75 6d 62 65  root pages numbe
18d28 72 73 20 66 6f 72 20 69 6e 64 69 76 69 64 75 61  rs for individua
18d29 6c 20 74 72 65 65 73 20 2a 2f 0a 20 20 69 6e 74  l trees */.  int
18d2a 20 6e 52 6f 6f 74 2c 20 20 20 20 2f 2a 20 4e 75   nRoot,    /* Nu
18d2b 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
18d2c 69 6e 20 61 52 6f 6f 74 5b 5d 20 2a 2f 0a 20 20  in aRoot[] */.  
18d2d 69 6e 74 20 6d 78 45 72 72 2c 20 20 20 20 2f 2a  int mxErr,    /*
18d2e 20 53 74 6f 70 20 72 65 70 6f 72 74 69 6e 67 20   Stop reporting 
18d2f 65 72 72 6f 72 73 20 61 66 74 65 72 20 74 68 69  errors after thi
18d30 73 20 6d 61 6e 79 20 2a 2f 0a 20 20 69 6e 74 20  s many */.  int 
18d31 2a 70 6e 45 72 72 20 20 20 20 2f 2a 20 57 72 69  *pnErr    /* Wri
18d32 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72  te number of err
18d33 6f 72 73 20 73 65 65 6e 20 74 6f 20 74 68 69 73  ors seen to this
18d34 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 29 7b 0a   variable */.){.
18d35 20 20 50 67 6e 6f 20 69 3b 0a 20 20 69 6e 74 20    Pgno i;.  int 
18d36 6e 52 65 66 3b 0a 20 20 49 6e 74 65 67 72 69 74  nRef;.  Integrit
18d37 79 43 6b 20 73 43 68 65 63 6b 3b 0a 20 20 42 74  yCk sCheck;.  Bt
18d38 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
18d39 3e 70 42 74 3b 0a 20 20 63 68 61 72 20 7a 45 72  >pBt;.  char zEr
18d3a 72 5b 31 30 30 5d 3b 0a 0a 20 20 73 71 6c 69 74  r[100];..  sqlit
18d3b 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
18d3c 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e  .  assert( p->in
18d3d 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45  Trans>TRANS_NONE
18d3e 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73   && pBt->inTrans
18d3f 61 63 74 69 6f 6e 3e 54 52 41 4e 53 5f 4e 4f 4e  action>TRANS_NON
18d40 45 20 29 3b 0a 20 20 6e 52 65 66 20 3d 20 73 71  E );.  nRef = sq
18d41 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75  lite3PagerRefcou
18d42 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b  nt(pBt->pPager);
18d43 0a 20 20 73 43 68 65 63 6b 2e 70 42 74 20 3d 20  .  sCheck.pBt = 
18d44 70 42 74 3b 0a 20 20 73 43 68 65 63 6b 2e 70 50  pBt;.  sCheck.pP
18d45 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 67  ager = pBt->pPag
18d46 65 72 3b 0a 20 20 73 43 68 65 63 6b 2e 6e 50 61  er;.  sCheck.nPa
18d47 67 65 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f  ge = pagerPageco
18d48 75 6e 74 28 73 43 68 65 63 6b 2e 70 42 74 29 3b  unt(sCheck.pBt);
18d49 0a 20 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 20  .  sCheck.mxErr 
18d4a 3d 20 6d 78 45 72 72 3b 0a 20 20 73 43 68 65 63  = mxErr;.  sChec
18d4b 6b 2e 6e 45 72 72 20 3d 20 30 3b 0a 20 20 73 43  k.nErr = 0;.  sC
18d4c 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65  heck.mallocFaile
18d4d 64 20 3d 20 30 3b 0a 20 20 2a 70 6e 45 72 72 20  d = 0;.  *pnErr 
18d4e 3d 20 30 3b 0a 20 20 69 66 28 20 73 43 68 65 63  = 0;.  if( sChec
18d4f 6b 2e 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20  k.nPage==0 ){.  
18d50 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
18d51 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75  ave(p);.    retu
18d52 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 43 68 65  rn 0;.  }.  sChe
18d53 63 6b 2e 61 6e 52 65 66 20 3d 20 73 71 6c 69 74  ck.anRef = sqlit
18d54 65 33 4d 61 6c 6c 6f 63 28 20 28 73 43 68 65 63  e3Malloc( (sChec
18d55 6b 2e 6e 50 61 67 65 2b 31 29 2a 73 69 7a 65 6f  k.nPage+1)*sizeo
18d56 66 28 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 30  f(sCheck.anRef[0
18d57 5d 29 20 29 3b 0a 20 20 69 66 28 20 21 73 43 68  ]) );.  if( !sCh
18d58 65 63 6b 2e 61 6e 52 65 66 20 29 7b 0a 20 20 20  eck.anRef ){.   
18d59 20 2a 70 6e 45 72 72 20 3d 20 31 3b 0a 20 20 20   *pnErr = 1;.   
18d5a 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
18d5b 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72  ve(p);.    retur
18d5c 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  n 0;.  }.  for(i
18d5d 3d 30 3b 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50  =0; i<=sCheck.nP
18d5e 61 67 65 3b 20 69 2b 2b 29 7b 20 73 43 68 65 63  age; i++){ sChec
18d5f 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 30 3b 20  k.anRef[i] = 0; 
18d60 7d 0a 20 20 69 20 3d 20 50 45 4e 44 49 4e 47 5f  }.  i = PENDING_
18d61 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 3b 0a  BYTE_PAGE(pBt);.
18d62 20 20 69 66 28 20 69 3c 3d 73 43 68 65 63 6b 2e    if( i<=sCheck.
18d63 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 73 43 68  nPage ){.    sCh
18d64 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 31  eck.anRef[i] = 1
18d65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53  ;.  }.  sqlite3S
18d66 74 72 41 63 63 75 6d 49 6e 69 74 28 26 73 43 68  trAccumInit(&sCh
18d67 65 63 6b 2e 65 72 72 4d 73 67 2c 20 7a 45 72 72  eck.errMsg, zErr
18d68 2c 20 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20  , sizeof(zErr), 
18d69 32 30 30 30 30 29 3b 0a 0a 20 20 2f 2a 20 43 68  20000);..  /* Ch
18d6a 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72 69 74  eck the integrit
18d6b 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73  y of the freelis
18d6c 74 0a 20 20 2a 2f 0a 20 20 63 68 65 63 6b 4c 69  t.  */.  checkLi
18d6d 73 74 28 26 73 43 68 65 63 6b 2c 20 31 2c 20 67  st(&sCheck, 1, g
18d6e 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50  et4byte(&pBt->pP
18d6f 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29  age1->aData[32])
18d70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 65  ,.            ge
18d71 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61  t4byte(&pBt->pPa
18d72 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 2c  ge1->aData[36]),
18d73 20 22 4d 61 69 6e 20 66 72 65 65 6c 69 73 74 3a   "Main freelist:
18d74 20 22 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b   ");..  /* Check
18d75 20 61 6c 6c 20 74 68 65 20 74 61 62 6c 65 73 2e   all the tables.
18d76 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b  .  */.  for(i=0;
18d77 20 28 69 6e 74 29 69 3c 6e 52 6f 6f 74 20 26 26   (int)i<nRoot &&
18d78 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69   sCheck.mxErr; i
18d79 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 52 6f  ++){.    if( aRo
18d7a 6f 74 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69  ot[i]==0 ) conti
18d7b 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  nue;.#ifndef SQL
18d7c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
18d7d 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42 74 2d  UUM.    if( pBt-
18d7e 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 61  >autoVacuum && a
18d7f 52 6f 6f 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20  Root[i]>1 ){.   
18d80 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 26     checkPtrmap(&
18d81 73 43 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d  sCheck, aRoot[i]
18d82 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47  , PTRMAP_ROOTPAG
18d83 45 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  E, 0, 0);.    }.
18d84 23 65 6e 64 69 66 0a 20 20 20 20 63 68 65 63 6b  #endif.    check
18d85 54 72 65 65 50 61 67 65 28 26 73 43 68 65 63 6b  TreePage(&sCheck
18d86 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 22 4c 69 73  , aRoot[i], "Lis
18d87 74 20 6f 66 20 74 72 65 65 20 72 6f 6f 74 73 3a  t of tree roots:
18d88 20 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d   ");.  }..  /* M
18d89 61 6b 65 20 73 75 72 65 20 65 76 65 72 79 20 70  ake sure every p
18d8a 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c 65 20  age in the file 
18d8b 69 73 20 72 65 66 65 72 65 6e 63 65 64 0a 20 20  is referenced.  
18d8c 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c  */.  for(i=1; i<
18d8d 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65 20 26 26  =sCheck.nPage &&
18d8e 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69   sCheck.mxErr; i
18d8f 2b 2b 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49  ++){.#ifdef SQLI
18d90 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
18d91 55 4d 0a 20 20 20 20 69 66 28 20 73 43 68 65 63  UM.    if( sChec
18d92 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 29 7b  k.anRef[i]==0 ){
18d93 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65  .      checkAppe
18d94 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30  ndMsg(&sCheck, 0
18d95 2c 20 22 50 61 67 65 20 25 64 20 69 73 20 6e 65  , "Page %d is ne
18d96 76 65 72 20 75 73 65 64 22 2c 20 69 29 3b 0a 20  ver used", i);. 
18d97 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 2f     }.#else.    /
18d98 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  * If the databas
18d99 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d  e supports auto-
18d9a 76 61 63 75 75 6d 2c 20 6d 61 6b 65 20 73 75 72  vacuum, make sur
18d9b 65 20 6e 6f 20 74 61 62 6c 65 73 20 63 6f 6e 74  e no tables cont
18d9c 61 69 6e 0a 20 20 20 20 2a 2a 20 72 65 66 65 72  ain.    ** refer
18d9d 65 6e 63 65 73 20 74 6f 20 70 6f 69 6e 74 65 72  ences to pointer
18d9e 2d 6d 61 70 20 70 61 67 65 73 2e 0a 20 20 20 20  -map pages..    
18d9f 2a 2f 0a 20 20 20 20 69 66 28 20 73 43 68 65 63  */.    if( sChec
18da0 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 26 26  k.anRef[i]==0 &&
18da1 20 0a 20 20 20 20 20 20 20 28 50 54 52 4d 41 50   .       (PTRMAP
18da2 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 21  _PAGENO(pBt, i)!
18da3 3d 69 20 7c 7c 20 21 70 42 74 2d 3e 61 75 74 6f  =i || !pBt->auto
18da4 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 20 20 20  Vacuum) ){.     
18da5 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28   checkAppendMsg(
18da6 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50 61 67  &sCheck, 0, "Pag
18da7 65 20 25 64 20 69 73 20 6e 65 76 65 72 20 75 73  e %d is never us
18da8 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20  ed", i);.    }. 
18da9 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e 61 6e     if( sCheck.an
18daa 52 65 66 5b 69 5d 21 3d 30 20 26 26 20 0a 20 20  Ref[i]!=0 && .  
18dab 20 20 20 20 20 28 50 54 52 4d 41 50 5f 50 41 47       (PTRMAP_PAG
18dac 45 4e 4f 28 70 42 74 2c 20 69 29 3d 3d 69 20 26  ENO(pBt, i)==i &
18dad 26 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  & pBt->autoVacuu
18dae 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63  m) ){.      chec
18daf 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65  kAppendMsg(&sChe
18db0 63 6b 2c 20 30 2c 20 22 50 6f 69 6e 74 65 72 20  ck, 0, "Pointer 
18db1 6d 61 70 20 70 61 67 65 20 25 64 20 69 73 20 72  map page %d is r
18db2 65 66 65 72 65 6e 63 65 64 22 2c 20 69 29 3b 0a  eferenced", i);.
18db3 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
18db4 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
18db5 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73 20 64   this analysis d
18db6 69 64 20 6e 6f 74 20 6c 65 61 76 65 20 61 6e 79  id not leave any
18db7 20 75 6e 72 65 66 28 29 20 70 61 67 65 73 2e 0a   unref() pages..
18db8 20 20 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20    ** This is an 
18db9 69 6e 74 65 72 6e 61 6c 20 63 6f 6e 73 69 73 74  internal consist
18dba 65 6e 63 79 20 63 68 65 63 6b 3b 20 61 6e 20 69  ency check; an i
18dbb 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 0a 20  ntegrity check. 
18dbc 20 2a 2a 20 6f 66 20 74 68 65 20 69 6e 74 65 67   ** of the integ
18dbd 72 69 74 79 20 63 68 65 63 6b 2e 0a 20 20 2a 2f  rity check..  */
18dbe 0a 20 20 69 66 28 20 4e 45 56 45 52 28 6e 52 65  .  if( NEVER(nRe
18dbf 66 20 21 3d 20 73 71 6c 69 74 65 33 50 61 67 65  f != sqlite3Page
18dc0 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70  rRefcount(pBt->p
18dc1 50 61 67 65 72 29 29 20 29 7b 0a 20 20 20 20 63  Pager)) ){.    c
18dc2 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73  heckAppendMsg(&s
18dc3 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20  Check, 0, .     
18dc4 20 22 4f 75 74 73 74 61 6e 64 69 6e 67 20 70 61   "Outstanding pa
18dc5 67 65 20 63 6f 75 6e 74 20 67 6f 65 73 20 66 72  ge count goes fr
18dc6 6f 6d 20 25 64 20 74 6f 20 25 64 20 64 75 72 69  om %d to %d duri
18dc7 6e 67 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73  ng this analysis
18dc8 22 2c 0a 20 20 20 20 20 20 6e 52 65 66 2c 20 73  ",.      nRef, s
18dc9 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f  qlite3PagerRefco
18dca 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29  unt(pBt->pPager)
18dcb 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f  .    );.  }..  /
18dcc 2a 20 43 6c 65 61 6e 20 20 75 70 20 61 6e 64 20  * Clean  up and 
18dcd 72 65 70 6f 72 74 20 65 72 72 6f 72 73 2e 0a 20  report errors.. 
18dce 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72   */.  sqlite3Btr
18dcf 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 73 71  eeLeave(p);.  sq
18dd0 6c 69 74 65 33 5f 66 72 65 65 28 73 43 68 65 63  lite3_free(sChec
18dd1 6b 2e 61 6e 52 65 66 29 3b 0a 20 20 69 66 28 20  k.anRef);.  if( 
18dd2 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69  sCheck.mallocFai
18dd3 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  led ){.    sqlit
18dd4 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28  e3StrAccumReset(
18dd5 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 29 3b  &sCheck.errMsg);
18dd6 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 73 43  .    *pnErr = sC
18dd7 68 65 63 6b 2e 6e 45 72 72 2b 31 3b 0a 20 20 20  heck.nErr+1;.   
18dd8 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
18dd9 20 2a 70 6e 45 72 72 20 3d 20 73 43 68 65 63 6b   *pnErr = sCheck
18dda 2e 6e 45 72 72 3b 0a 20 20 69 66 28 20 73 43 68  .nErr;.  if( sCh
18ddb 65 63 6b 2e 6e 45 72 72 3d 3d 30 20 29 20 73 71  eck.nErr==0 ) sq
18ddc 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 73  lite3StrAccumRes
18ddd 65 74 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73  et(&sCheck.errMs
18dde 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  g);.  return sql
18ddf 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69  ite3StrAccumFini
18de0 73 68 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73  sh(&sCheck.errMs
18de1 67 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  g);.}.#endif /* 
18de2 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45  SQLITE_OMIT_INTE
18de3 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a  GRITY_CHECK */..
18de4 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
18de5 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f   full pathname o
18de6 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  f the underlying
18de7 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
18de8 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20  **.** The pager 
18de9 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76 61  filename is inva
18dea 72 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73  riant as long as
18deb 20 74 68 65 20 70 61 67 65 72 20 69 73 0a 2a 2a   the pager is.**
18dec 20 6f 70 65 6e 20 73 6f 20 69 74 20 69 73 20 73   open so it is s
18ded 61 66 65 20 74 6f 20 61 63 63 65 73 73 20 77 69  afe to access wi
18dee 74 68 6f 75 74 20 74 68 65 20 42 74 53 68 61 72  thout the BtShar
18def 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c  ed mutex..*/.SQL
18df0 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
18df1 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42  t char *sqlite3B
18df2 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28  treeGetFilename(
18df3 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73  Btree *p){.  ass
18df4 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61  ert( p->pBt->pPa
18df5 67 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 75  ger!=0 );.  retu
18df6 72 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 46  rn sqlite3PagerF
18df7 69 6c 65 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e  ilename(p->pBt->
18df8 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  pPager);.}../*.*
18df9 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61 74  * Return the pat
18dfa 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75  hname of the jou
18dfb 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 74 68  rnal file for th
18dfc 69 73 20 64 61 74 61 62 61 73 65 2e 20 54 68 65  is database. The
18dfd 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65   return.** value
18dfe 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
18dff 20 69 73 20 74 68 65 20 73 61 6d 65 20 72 65 67   is the same reg
18e00 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68  ardless of wheth
18e01 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  er the journal f
18e02 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20  ile.** has been 
18e03 63 72 65 61 74 65 64 20 6f 72 20 6e 6f 74 2e 0a  created or not..
18e04 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20  **.** The pager 
18e05 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65  journal filename
18e06 20 69 73 20 69 6e 76 61 72 69 61 6e 74 20 61 73   is invariant as
18e07 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 70 61 67   long as the pag
18e08 65 72 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f  er is.** open so
18e09 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 61   it is safe to a
18e0a 63 63 65 73 73 20 77 69 74 68 6f 75 74 20 74 68  ccess without th
18e0b 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78  e BtShared mutex
18e0c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
18e0d 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
18e0e 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a  sqlite3BtreeGetJ
18e0f 6f 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65  ournalname(Btree
18e10 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
18e11 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 21 3d  p->pBt->pPager!=
18e12 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71  0 );.  return sq
18e13 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61  lite3PagerJourna
18e14 6c 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 50  lname(p->pBt->pP
18e15 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ager);.}../*.** 
18e16 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20  Return non-zero 
18e17 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  if a transaction
18e18 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53   is active..*/.S
18e19 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
18e1a 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73  t sqlite3BtreeIs
18e1b 49 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70  InTrans(Btree *p
18e1c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d  ){.  assert( p==
18e1d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
18e1e 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d  ex_held(p->db->m
18e1f 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72  utex) );.  retur
18e20 6e 20 28 70 20 26 26 20 28 70 2d 3e 69 6e 54 72  n (p && (p->inTr
18e21 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans==TRANS_WRITE
18e22 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ));.}../*.** Ret
18e23 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20  urn non-zero if 
18e24 61 20 72 65 61 64 20 28 6f 72 20 77 72 69 74 65  a read (or write
18e25 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  ) transaction is
18e26 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49   active..*/.SQLI
18e27 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
18e28 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52  qlite3BtreeIsInR
18e29 65 61 64 54 72 61 6e 73 28 42 74 72 65 65 20 2a  eadTrans(Btree *
18e2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20  p){.  assert( p 
18e2b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
18e2c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
18e2d 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
18e2e 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 6e 54  .  return p->inT
18e2f 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45  rans!=TRANS_NONE
18e30 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  ;.}..SQLITE_PRIV
18e31 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
18e32 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28 42  treeIsInBackup(B
18e33 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65  tree *p){.  asse
18e34 72 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72  rt( p );.  asser
18e35 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
18e36 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74  _held(p->db->mut
18e37 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ex) );.  return 
18e38 70 2d 3e 6e 42 61 63 6b 75 70 21 3d 30 3b 0a 7d  p->nBackup!=0;.}
18e39 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
18e3a 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20  ction returns a 
18e3b 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c 6f  pointer to a blo
18e3c 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f  b of memory asso
18e3d 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 61  ciated with.** a
18e3e 20 73 69 6e 67 6c 65 20 73 68 61 72 65 64 2d 62   single shared-b
18e3f 74 72 65 65 2e 20 54 68 65 20 6d 65 6d 6f 72 79  tree. The memory
18e40 20 69 73 20 75 73 65 64 20 62 79 20 63 6c 69 65   is used by clie
18e41 6e 74 20 63 6f 64 65 20 66 6f 72 20 69 74 73 20  nt code for its 
18e42 6f 77 6e 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20  own.** purposes 
18e43 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f  (for example, to
18e44 20 73 74 6f 72 65 20 61 20 68 69 67 68 2d 6c 65   store a high-le
18e45 76 65 6c 20 73 63 68 65 6d 61 20 61 73 73 6f 63  vel schema assoc
18e46 69 61 74 65 64 20 77 69 74 68 20 0a 2a 2a 20 74  iated with .** t
18e47 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 29  he shared-btree)
18e48 2e 20 54 68 65 20 62 74 72 65 65 20 6c 61 79 65  . The btree laye
18e49 72 20 6d 61 6e 61 67 65 73 20 72 65 66 65 72 65  r manages refere
18e4a 6e 63 65 20 63 6f 75 6e 74 69 6e 67 20 69 73 73  nce counting iss
18e4b 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ues..**.** The f
18e4c 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 69  irst time this i
18e4d 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 73 68  s called on a sh
18e4e 61 72 65 64 2d 62 74 72 65 65 2c 20 6e 42 79 74  ared-btree, nByt
18e4f 65 73 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f  es bytes of memo
18e50 72 79 0a 2a 2a 20 61 72 65 20 61 6c 6c 6f 63 61  ry.** are alloca
18e51 74 65 64 2c 20 7a 65 72 6f 65 64 2c 20 61 6e 64  ted, zeroed, and
18e52 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65   returned to the
18e53 20 63 61 6c 6c 65 72 2e 20 46 6f 72 20 65 61 63   caller. For eac
18e54 68 20 73 75 62 73 65 71 75 65 6e 74 20 0a 2a 2a  h subsequent .**
18e55 20 63 61 6c 6c 20 74 68 65 20 6e 42 79 74 65 73   call the nBytes
18e56 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 69 67   parameter is ig
18e57 6e 6f 72 65 64 20 61 6e 64 20 61 20 70 6f 69 6e  nored and a poin
18e58 74 65 72 20 74 6f 20 74 68 65 20 73 61 6d 65 20  ter to the same 
18e59 62 6c 6f 62 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72  blob.** of memor
18e5a 79 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 0a  y returned. .**.
18e5b 2a 2a 20 49 66 20 74 68 65 20 6e 42 79 74 65 73  ** If the nBytes
18e5c 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 20   parameter is 0 
18e5d 61 6e 64 20 74 68 65 20 62 6c 6f 62 20 6f 66 20  and the blob of 
18e5e 6d 65 6d 6f 72 79 20 68 61 73 20 6e 6f 74 20 79  memory has not y
18e5f 65 74 20 62 65 65 6e 0a 2a 2a 20 61 6c 6c 6f 63  et been.** alloc
18e60 61 74 65 64 2c 20 61 20 6e 75 6c 6c 20 70 6f 69  ated, a null poi
18e61 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64  nter is returned
18e62 2e 20 49 66 20 74 68 65 20 62 6c 6f 62 20 68 61  . If the blob ha
18e63 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a 2a  s already been.*
18e64 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20  * allocated, it 
18e65 69 73 20 72 65 74 75 72 6e 65 64 20 61 73 20 6e  is returned as n
18e66 6f 72 6d 61 6c 2e 0a 2a 2a 0a 2a 2a 20 4a 75 73  ormal..**.** Jus
18e67 74 20 62 65 66 6f 72 65 20 74 68 65 20 73 68 61  t before the sha
18e68 72 65 64 2d 62 74 72 65 65 20 69 73 20 63 6c 6f  red-btree is clo
18e69 73 65 64 2c 20 74 68 65 20 66 75 6e 63 74 69 6f  sed, the functio
18e6a 6e 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  n passed as the 
18e6b 0a 2a 2a 20 78 46 72 65 65 20 61 72 67 75 6d 65  .** xFree argume
18e6c 6e 74 20 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f  nt when the memo
18e6d 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61  ry allocation wa
18e6e 73 20 6d 61 64 65 20 69 73 20 69 6e 76 6f 6b 65  s made is invoke
18e6f 64 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 62 6c 6f  d on the .** blo
18e70 62 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d  b of allocated m
18e71 65 6d 6f 72 79 2e 20 54 68 69 73 20 66 75 6e 63  emory. This func
18e72 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 20  tion should not 
18e73 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 72 65  call sqlite3_fre
18e74 65 28 29 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 65  e().** on the me
18e75 6d 6f 72 79 2c 20 74 68 65 20 62 74 72 65 65 20  mory, the btree 
18e76 6c 61 79 65 72 20 64 6f 65 73 20 74 68 61 74 2e  layer does that.
18e77 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
18e78 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
18e79 42 74 72 65 65 53 63 68 65 6d 61 28 42 74 72 65  BtreeSchema(Btre
18e7a 65 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 73  e *p, int nBytes
18e7b 2c 20 76 6f 69 64 28 2a 78 46 72 65 65 29 28 76  , void(*xFree)(v
18e7c 6f 69 64 20 2a 29 29 7b 0a 20 20 42 74 53 68 61  oid *)){.  BtSha
18e7d 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
18e7e 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  t;.  sqlite3Btre
18e7f 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28  eEnter(p);.  if(
18e80 20 21 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 26   !pBt->pSchema &
18e81 26 20 6e 42 79 74 65 73 20 29 7b 0a 20 20 20 20  & nBytes ){.    
18e82 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 3d 20 73  pBt->pSchema = s
18e83 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
18e84 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 70 42  (nBytes);.    pB
18e85 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 20 3d  t->xFreeSchema =
18e86 20 78 46 72 65 65 3b 0a 20 20 7d 0a 20 20 73 71   xFree;.  }.  sq
18e87 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
18e88 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 42 74  p);.  return pBt
18e89 2d 3e 70 53 63 68 65 6d 61 3b 0a 7d 0a 0a 2f 2a  ->pSchema;.}../*
18e8a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
18e8b 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43  E_LOCKED_SHAREDC
18e8c 41 43 48 45 20 69 66 20 61 6e 6f 74 68 65 72 20  ACHE if another 
18e8d 75 73 65 72 20 6f 66 20 74 68 65 20 73 61 6d 65  user of the same
18e8e 20 73 68 61 72 65 64 20 0a 2a 2a 20 62 74 72 65   shared .** btre
18e8f 65 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e  e as the argumen
18e90 74 20 68 61 6e 64 6c 65 20 68 6f 6c 64 73 20 61  t handle holds a
18e91 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
18e92 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69   on the .** sqli
18e93 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e  te_master table.
18e94 20 4f 74 68 65 72 77 69 73 65 20 53 51 4c 49 54   Otherwise SQLIT
18e95 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  E_OK..*/.SQLITE_
18e96 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
18e97 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f  te3BtreeSchemaLo
18e98 63 6b 65 64 28 42 74 72 65 65 20 2a 70 29 7b 0a  cked(Btree *p){.
18e99 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65    int rc;.  asse
18e9a 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
18e9b 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
18e9c 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65  tex) );.  sqlite
18e9d 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
18e9e 20 20 72 63 20 3d 20 71 75 65 72 79 53 68 61 72    rc = queryShar
18e9f 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b  edCacheTableLock
18ea0 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c  (p, MASTER_ROOT,
18ea1 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 61   READ_LOCK);.  a
18ea2 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
18ea3 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  E_OK || rc==SQLI
18ea4 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44  TE_LOCKED_SHARED
18ea5 43 41 43 48 45 20 29 3b 0a 20 20 73 71 6c 69 74  CACHE );.  sqlit
18ea6 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
18ea7 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
18ea8 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
18ea9 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
18eaa 48 45 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20  HE./*.** Obtain 
18eab 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61  a lock on the ta
18eac 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 70  ble whose root p
18ead 61 67 65 20 69 73 20 69 54 61 62 2e 20 20 54 68  age is iTab.  Th
18eae 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 20 77  e.** lock is a w
18eaf 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 69 73 57  rite lock if isW
18eb0 72 69 74 65 6c 6f 63 6b 20 69 73 20 74 72 75 65  ritelock is true
18eb1 20 6f 72 20 61 20 72 65 61 64 20 6c 6f 63 6b 0a   or a read lock.
18eb2 2a 2a 20 69 66 20 69 74 20 69 73 20 66 61 6c 73  ** if it is fals
18eb3 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
18eb4 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
18eb5 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42  BtreeLockTable(B
18eb6 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61  tree *p, int iTa
18eb7 62 2c 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63  b, u8 isWriteLoc
18eb8 6b 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  k){.  int rc = S
18eb9 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65  QLITE_OK;.  asse
18eba 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d  rt( p->inTrans!=
18ebb 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20  TRANS_NONE );.  
18ebc 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20  if( p->sharable 
18ebd 29 7b 0a 20 20 20 20 75 38 20 6c 6f 63 6b 54 79  ){.    u8 lockTy
18ebe 70 65 20 3d 20 52 45 41 44 5f 4c 4f 43 4b 20 2b  pe = READ_LOCK +
18ebf 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 0a 20 20   isWriteLock;.  
18ec0 20 20 61 73 73 65 72 74 28 20 52 45 41 44 5f 4c    assert( READ_L
18ec1 4f 43 4b 2b 31 3d 3d 57 52 49 54 45 5f 4c 4f 43  OCK+1==WRITE_LOC
18ec2 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  K );.    assert(
18ec3 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 20   isWriteLock==0 
18ec4 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d  || isWriteLock==
18ec5 31 20 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65  1 );..    sqlite
18ec6 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
18ec7 20 20 20 20 72 63 20 3d 20 71 75 65 72 79 53 68      rc = querySh
18ec8 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f  aredCacheTableLo
18ec9 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c 6f 63 6b  ck(p, iTab, lock
18eca 54 79 70 65 29 3b 0a 20 20 20 20 69 66 28 20 72  Type);.    if( r
18ecb 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
18ecc 20 20 20 20 20 20 72 63 20 3d 20 73 65 74 53 68        rc = setSh
18ecd 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f  aredCacheTableLo
18ece 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c 6f 63 6b  ck(p, iTab, lock
18ecf 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Type);.    }.   
18ed0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
18ed1 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ve(p);.  }.  ret
18ed2 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66  urn rc;.}.#endif
18ed3 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
18ed4 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f  _OMIT_INCRBLOB./
18ed5 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70 43  *.** Argument pC
18ed6 73 72 20 6d 75 73 74 20 62 65 20 61 20 63 75 72  sr must be a cur
18ed7 73 6f 72 20 6f 70 65 6e 65 64 20 66 6f 72 20 77  sor opened for w
18ed8 72 69 74 69 6e 67 20 6f 6e 20 61 6e 20 0a 2a 2a  riting on an .**
18ed9 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 20 63 75   INTKEY table cu
18eda 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67  rrently pointing
18edb 20 61 74 20 61 20 76 61 6c 69 64 20 74 61 62 6c   at a valid tabl
18edc 65 20 65 6e 74 72 79 2e 20 0a 2a 2a 20 54 68 69  e entry. .** Thi
18edd 73 20 66 75 6e 63 74 69 6f 6e 20 6d 6f 64 69 66  s function modif
18ede 69 65 73 20 74 68 65 20 64 61 74 61 20 73 74 6f  ies the data sto
18edf 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74  red as part of t
18ee0 68 61 74 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a  hat entry..**.**
18ee1 20 4f 6e 6c 79 20 74 68 65 20 64 61 74 61 20 63   Only the data c
18ee2 6f 6e 74 65 6e 74 20 6d 61 79 20 6f 6e 6c 79 20  ontent may only 
18ee3 62 65 20 6d 6f 64 69 66 69 65 64 2c 20 69 74 20  be modified, it 
18ee4 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
18ee5 74 6f 20 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68  to .** change th
18ee6 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
18ee7 64 61 74 61 20 73 74 6f 72 65 64 2e 20 49 66 20  data stored. If 
18ee8 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
18ee9 20 63 61 6c 6c 65 64 20 77 69 74 68 0a 2a 2a 20   called with.** 
18eea 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 74 20  parameters that 
18eeb 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65  attempt to write
18eec 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66   past the end of
18eed 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 64 61   the existing da
18eee 74 61 2c 0a 2a 2a 20 6e 6f 20 6d 6f 64 69 66 69  ta,.** no modifi
18eef 63 61 74 69 6f 6e 73 20 61 72 65 20 6d 61 64 65  cations are made
18ef0 20 61 6e 64 20 53 51 4c 49 54 45 5f 43 4f 52 52   and SQLITE_CORR
18ef1 55 50 54 20 69 73 20 72 65 74 75 72 6e 65 64 2e  UPT is returned.
18ef2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
18ef3 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
18ef4 72 65 65 50 75 74 44 61 74 61 28 42 74 43 75 72  reePutData(BtCur
18ef5 73 6f 72 20 2a 70 43 73 72 2c 20 75 33 32 20 6f  sor *pCsr, u32 o
18ef6 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20  ffset, u32 amt, 
18ef7 76 6f 69 64 20 2a 7a 29 7b 0a 20 20 69 6e 74 20  void *z){.  int 
18ef8 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75  rc;.  assert( cu
18ef9 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
18efa 43 73 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Csr) );.  assert
18efb 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
18efc 68 65 6c 64 28 70 43 73 72 2d 3e 70 42 74 72 65  held(pCsr->pBtre
18efd 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  e->db->mutex) );
18efe 0a 20 20 61 73 73 65 72 74 28 20 70 43 73 72 2d  .  assert( pCsr-
18eff 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c  >isIncrblobHandl
18f00 65 20 29 3b 0a 0a 20 20 72 63 20 3d 20 72 65 73  e );..  rc = res
18f01 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69  toreCursorPositi
18f02 6f 6e 28 70 43 73 72 29 3b 0a 20 20 69 66 28 20  on(pCsr);.  if( 
18f03 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
18f04 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
18f05 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43    }.  assert( pC
18f06 73 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53  sr->eState!=CURS
18f07 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29  OR_REQUIRESEEK )
18f08 3b 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 65 53  ;.  if( pCsr->eS
18f09 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c  tate!=CURSOR_VAL
18f0a 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ID ){.    return
18f0b 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20   SQLITE_ABORT;. 
18f0c 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 73   }..  /* Check s
18f0d 6f 6d 65 20 61 73 73 75 6d 70 74 69 6f 6e 73 3a  ome assumptions:
18f0e 20 0a 20 20 2a 2a 20 20 20 28 61 29 20 74 68 65   .  **   (a) the
18f0f 20 63 75 72 73 6f 72 20 69 73 20 6f 70 65 6e 20   cursor is open 
18f10 66 6f 72 20 77 72 69 74 69 6e 67 2c 0a 20 20 2a  for writing,.  *
18f11 2a 20 20 20 28 62 29 20 74 68 65 72 65 20 69 73  *   (b) there is
18f12 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 74 72   a read/write tr
18f13 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 0a  ansaction open,.
18f14 20 20 2a 2a 20 20 20 28 63 29 20 74 68 65 20 63    **   (c) the c
18f15 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f 6c 64 73 20  onnection holds 
18f16 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20  a write-lock on 
18f17 74 68 65 20 74 61 62 6c 65 20 28 69 66 20 72 65  the table (if re
18f18 71 75 69 72 65 64 29 2c 0a 20 20 2a 2a 20 20 20  quired),.  **   
18f19 28 64 29 20 74 68 65 72 65 20 61 72 65 20 6e 6f  (d) there are no
18f1a 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 72 65 61   conflicting rea
18f1b 64 2d 6c 6f 63 6b 73 2c 20 61 6e 64 0a 20 20 2a  d-locks, and.  *
18f1c 2a 20 20 20 28 65 29 20 74 68 65 20 63 75 72 73  *   (e) the curs
18f1d 6f 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20 76  or points at a v
18f1e 61 6c 69 64 20 72 6f 77 20 6f 66 20 61 6e 20 69  alid row of an i
18f1f 6e 74 4b 65 79 20 74 61 62 6c 65 2e 0a 20 20 2a  ntKey table..  *
18f20 2f 0a 20 20 69 66 28 20 21 70 43 73 72 2d 3e 77  /.  if( !pCsr->w
18f21 72 46 6c 61 67 20 29 7b 0a 20 20 20 20 72 65 74  rFlag ){.    ret
18f22 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f  urn SQLITE_READO
18f23 4e 4c 59 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  NLY;.  }.  asser
18f24 74 28 20 21 70 43 73 72 2d 3e 70 42 74 2d 3e 72  t( !pCsr->pBt->r
18f25 65 61 64 4f 6e 6c 79 20 26 26 20 70 43 73 72 2d  eadOnly && pCsr-
18f26 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74  >pBt->inTransact
18f27 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ion==TRANS_WRITE
18f28 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68 61   );.  assert( ha
18f29 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  sSharedCacheTabl
18f2a 65 4c 6f 63 6b 28 70 43 73 72 2d 3e 70 42 74 72  eLock(pCsr->pBtr
18f2b 65 65 2c 20 70 43 73 72 2d 3e 70 67 6e 6f 52 6f  ee, pCsr->pgnoRo
18f2c 6f 74 2c 20 30 2c 20 32 29 20 29 3b 0a 20 20 61  ot, 0, 2) );.  a
18f2d 73 73 65 72 74 28 20 21 68 61 73 52 65 61 64 43  ssert( !hasReadC
18f2e 6f 6e 66 6c 69 63 74 73 28 70 43 73 72 2d 3e 70  onflicts(pCsr->p
18f2f 42 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 67 6e  Btree, pCsr->pgn
18f30 6f 52 6f 6f 74 29 20 29 3b 0a 20 20 61 73 73 65  oRoot) );.  asse
18f31 72 74 28 20 70 43 73 72 2d 3e 61 70 50 61 67 65  rt( pCsr->apPage
18f32 5b 70 43 73 72 2d 3e 69 50 61 67 65 5d 2d 3e 69  [pCsr->iPage]->i
18f33 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 72 65 74 75  ntKey );..  retu
18f34 72 6e 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64  rn accessPayload
18f35 28 70 43 73 72 2c 20 6f 66 66 73 65 74 2c 20 61  (pCsr, offset, a
18f36 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 68  mt, (unsigned ch
18f37 61 72 20 2a 29 7a 2c 20 31 29 3b 0a 7d 0a 0a 2f  ar *)z, 1);.}../
18f38 2a 20 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61 67  * .** Set a flag
18f39 20 6f 6e 20 74 68 69 73 20 63 75 72 73 6f 72 20   on this cursor 
18f3a 74 6f 20 63 61 63 68 65 20 74 68 65 20 6c 6f 63  to cache the loc
18f3b 61 74 69 6f 6e 73 20 6f 66 20 70 61 67 65 73 20  ations of pages 
18f3c 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 6f 76 65  from the .** ove
18f3d 72 66 6c 6f 77 20 6c 69 73 74 20 66 6f 72 20 74  rflow list for t
18f3e 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2e 20  he current row. 
18f3f 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20  This is used by 
18f40 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 0a 2a  cursors opened.*
18f41 2a 20 66 6f 72 20 69 6e 63 72 65 6d 65 6e 74 61  * for incrementa
18f42 6c 20 62 6c 6f 62 20 49 4f 20 6f 6e 6c 79 2e 0a  l blob IO only..
18f43 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
18f44 69 6f 6e 20 73 65 74 73 20 61 20 66 6c 61 67 20  ion sets a flag 
18f45 6f 6e 6c 79 2e 20 54 68 65 20 61 63 74 75 61 6c  only. The actual
18f46 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e 20 63   page location c
18f47 61 63 68 65 0a 2a 2a 20 28 73 74 6f 72 65 64 20  ache.** (stored 
18f48 69 6e 20 42 74 43 75 72 73 6f 72 2e 61 4f 76 65  in BtCursor.aOve
18f49 72 66 6c 6f 77 5b 5d 29 20 69 73 20 61 6c 6c 6f  rflow[]) is allo
18f4a 63 61 74 65 64 20 61 6e 64 20 75 73 65 64 20 62  cated and used b
18f4b 79 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 63  y function.** ac
18f4c 63 65 73 73 50 61 79 6c 6f 61 64 28 29 20 28 74  cessPayload() (t
18f4d 68 65 20 77 6f 72 6b 65 72 20 66 75 6e 63 74 69  he worker functi
18f4e 6f 6e 20 66 6f 72 20 73 71 6c 69 74 65 33 42 74  on for sqlite3Bt
18f4f 72 65 65 44 61 74 61 28 29 20 61 6e 64 0a 2a 2a  reeData() and.**
18f50 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74   sqlite3BtreePut
18f51 44 61 74 61 28 29 29 2e 0a 2a 2f 0a 53 51 4c 49  Data())..*/.SQLI
18f52 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
18f53 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68  sqlite3BtreeCach
18f54 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75 72 73  eOverflow(BtCurs
18f55 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73  or *pCur){.  ass
18f56 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
18f57 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
18f58 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
18f59 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72  _mutex_held(pCur
18f5a 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75  ->pBtree->db->mu
18f5b 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
18f5c 28 21 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c  (!pCur->isIncrbl
18f5d 6f 62 48 61 6e 64 6c 65 29 3b 0a 20 20 61 73 73  obHandle);.  ass
18f5e 65 72 74 28 21 70 43 75 72 2d 3e 61 4f 76 65 72  ert(!pCur->aOver
18f5f 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 2d 3e 69  flow);.  pCur->i
18f60 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20  sIncrblobHandle 
18f61 3d 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  = 1;.}.#endif../
18f62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
18f63 6e 64 20 6f 66 20 62 74 72 65 65 2e 63 20 2a 2a  nd of btree.c **
18f64 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
18f67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
18f68 65 67 69 6e 20 66 69 6c 65 20 62 61 63 6b 75 70  egin file backup
18f69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
18f6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
18f6c 2a 0a 2a 2a 20 32 30 30 39 20 4a 61 6e 75 61 72  *.** 2009 Januar
18f6d 79 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  y 28.**.** The a
18f6e 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
18f6f 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
18f70 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
18f71 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
18f72 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
18f73 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
18f74 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
18f75 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
18f76 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
18f77 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
18f78 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
18f79 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
18f7a 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
18f7b 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
18f7c 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
18f7d 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
18f7e 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
18f7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f83 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
18f84 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d   contains the im
18f85 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
18f86 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  the sqlite3_back
18f87 75 70 5f 58 58 58 28 29 20 0a 2a 2a 20 41 50 49  up_XXX() .** API
18f88 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 74   functions and t
18f89 68 65 20 72 65 6c 61 74 65 64 20 66 65 61 74 75  he related featu
18f8a 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  res..**.** $Id: 
18f8b 62 61 63 6b 75 70 2e 63 2c 76 20 31 2e 31 39 20  backup.c,v 1.19 
18f8c 32 30 30 39 2f 30 37 2f 30 36 20 31 39 3a 30 33  2009/07/06 19:03
18f8d 3a 31 33 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :13 drh Exp $.*/
18f8e 0a 0a 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 66 69  ../* Macro to fi
18f8f 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 6f  nd the minimum o
18f90 66 20 74 77 6f 20 6e 75 6d 65 72 69 63 20 76 61  f two numeric va
18f91 6c 75 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  lues..*/.#ifndef
18f92 20 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20 4d 49   MIN.# define MI
18f93 4e 28 78 2c 79 29 20 28 28 78 29 3c 28 79 29 3f  N(x,y) ((x)<(y)?
18f94 28 78 29 3a 28 79 29 29 0a 23 65 6e 64 69 66 0a  (x):(y)).#endif.
18f95 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65  ./*.** Structure
18f96 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65   allocated for e
18f97 61 63 68 20 62 61 63 6b 75 70 20 6f 70 65 72 61  ach backup opera
18f98 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  tion..*/.struct 
18f99 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 7b  sqlite3_backup {
18f9a 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 44 65 73  .  sqlite3* pDes
18f9b 74 44 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 44  tDb;        /* D
18f9c 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
18f9d 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ase handle */.  
18f9e 42 74 72 65 65 20 2a 70 44 65 73 74 3b 20 20 20  Btree *pDest;   
18f9f 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74           /* Dest
18fa0 69 6e 61 74 69 6f 6e 20 62 2d 74 72 65 65 20 66  ination b-tree f
18fa1 69 6c 65 20 2a 2f 0a 20 20 75 33 32 20 69 44 65  ile */.  u32 iDe
18fa2 73 74 53 63 68 65 6d 61 3b 20 20 20 20 20 20 20  stSchema;       
18fa3 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 73 63    /* Original sc
18fa4 68 65 6d 61 20 63 6f 6f 6b 69 65 20 69 6e 20 64  hema cookie in d
18fa5 65 73 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20  estination */.  
18fa6 69 6e 74 20 62 44 65 73 74 4c 6f 63 6b 65 64 3b  int bDestLocked;
18fa7 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
18fa8 20 6f 6e 63 65 20 61 20 77 72 69 74 65 2d 74 72   once a write-tr
18fa9 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65  ansaction is ope
18faa 6e 20 6f 6e 20 70 44 65 73 74 20 2a 2f 0a 0a 20  n on pDest */.. 
18fab 20 50 67 6e 6f 20 69 4e 65 78 74 3b 20 20 20 20   Pgno iNext;    
18fac 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
18fad 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
18fae 6e 65 78 74 20 73 6f 75 72 63 65 20 70 61 67 65  next source page
18faf 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 73 71   to copy */.  sq
18fb0 6c 69 74 65 33 2a 20 70 53 72 63 44 62 3b 20 20  lite3* pSrcDb;  
18fb1 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65         /* Source
18fb2 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
18fb3 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 53 72   */.  Btree *pSr
18fb4 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  c;             /
18fb5 2a 20 53 6f 75 72 63 65 20 62 2d 74 72 65 65 20  * Source b-tree 
18fb6 66 69 6c 65 20 2a 2f 0a 0a 20 20 69 6e 74 20 72  file */..  int r
18fb7 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
18fb8 20 20 20 20 2f 2a 20 42 61 63 6b 75 70 20 70 72      /* Backup pr
18fb9 6f 63 65 73 73 20 65 72 72 6f 72 20 63 6f 64 65  ocess error code
18fba 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 73 65 20   */..  /* These 
18fbb 74 77 6f 20 76 61 72 69 61 62 6c 65 73 20 61 72  two variables ar
18fbc 65 20 73 65 74 20 62 79 20 65 76 65 72 79 20 63  e set by every c
18fbd 61 6c 6c 20 74 6f 20 62 61 63 6b 75 70 5f 73 74  all to backup_st
18fbe 65 70 28 29 2e 20 54 68 65 79 20 61 72 65 0a 20  ep(). They are. 
18fbf 20 2a 2a 20 72 65 61 64 20 62 79 20 63 61 6c 6c   ** read by call
18fc0 73 20 74 6f 20 62 61 63 6b 75 70 5f 72 65 6d 61  s to backup_rema
18fc1 69 6e 69 6e 67 28 29 20 61 6e 64 20 62 61 63 6b  ining() and back
18fc2 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 2e 0a  up_pagecount()..
18fc3 20 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 52 65 6d    */.  Pgno nRem
18fc4 61 69 6e 69 6e 67 3b 20 20 20 20 20 20 20 20 20  aining;         
18fc5 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67  /* Number of pag
18fc6 65 73 20 6c 65 66 74 20 74 6f 20 63 6f 70 79 20  es left to copy 
18fc7 2a 2f 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 63  */.  Pgno nPagec
18fc8 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  ount;         /*
18fc9 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   Total number of
18fca 20 70 61 67 65 73 20 74 6f 20 63 6f 70 79 20 2a   pages to copy *
18fcb 2f 0a 0a 20 20 69 6e 74 20 69 73 41 74 74 61 63  /..  int isAttac
18fcc 68 65 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  hed;          /*
18fcd 20 54 72 75 65 20 6f 6e 63 65 20 62 61 63 6b 75   True once backu
18fce 70 20 68 61 73 20 62 65 65 6e 20 72 65 67 69 73  p has been regis
18fcf 74 65 72 65 64 20 77 69 74 68 20 70 61 67 65 72  tered with pager
18fd0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 61   */.  sqlite3_ba
18fd1 63 6b 75 70 20 2a 70 4e 65 78 74 3b 20 20 20 2f  ckup *pNext;   /
18fd2 2a 20 4e 65 78 74 20 62 61 63 6b 75 70 20 61 73  * Next backup as
18fd3 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 73 6f  sociated with so
18fd4 75 72 63 65 20 70 61 67 65 72 20 2a 2f 0a 7d 3b  urce pager */.};
18fd5 0a 0a 2f 2a 0a 2a 2a 20 54 48 52 45 41 44 20 53  ../*.** THREAD S
18fd6 41 46 45 54 59 20 4e 4f 54 45 53 3a 0a 2a 2a 0a  AFETY NOTES:.**.
18fd7 2a 2a 20 20 20 4f 6e 63 65 20 69 74 20 68 61 73  **   Once it has
18fd8 20 62 65 65 6e 20 63 72 65 61 74 65 64 20 75 73   been created us
18fd9 69 6e 67 20 62 61 63 6b 75 70 5f 69 6e 69 74 28  ing backup_init(
18fda 29 2c 20 61 20 73 69 6e 67 6c 65 20 73 71 6c 69  ), a single sqli
18fdb 74 65 33 5f 62 61 63 6b 75 70 0a 2a 2a 20 20 20  te3_backup.**   
18fdc 73 74 72 75 63 74 75 72 65 20 6d 61 79 20 62 65  structure may be
18fdd 20 61 63 63 65 73 73 65 64 20 76 69 61 20 74 77   accessed via tw
18fde 6f 20 67 72 6f 75 70 73 20 6f 66 20 74 68 72 65  o groups of thre
18fdf 61 64 2d 73 61 66 65 20 65 6e 74 72 79 20 70 6f  ad-safe entry po
18fe0 69 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ints:.**.**     
18fe1 2a 20 56 69 61 20 74 68 65 20 73 71 6c 69 74 65  * Via the sqlite
18fe2 33 5f 62 61 63 6b 75 70 5f 58 58 58 28 29 20 41  3_backup_XXX() A
18fe3 50 49 20 66 75 6e 63 74 69 6f 6e 20 62 61 63 6b  PI function back
18fe4 75 70 5f 73 74 65 70 28 29 20 61 6e 64 20 0a 2a  up_step() and .*
18fe5 2a 20 20 20 20 20 20 20 62 61 63 6b 75 70 5f 66  *       backup_f
18fe6 69 6e 69 73 68 28 29 2e 20 42 6f 74 68 20 74 68  inish(). Both th
18fe7 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 6f 62  ese functions ob
18fe8 74 61 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20  tain the source 
18fe9 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20  database.**     
18fea 20 20 68 61 6e 64 6c 65 20 6d 75 74 65 78 20 61    handle mutex a
18feb 6e 64 20 74 68 65 20 6d 75 74 65 78 20 61 73 73  nd the mutex ass
18fec 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
18fed 20 73 6f 75 72 63 65 20 42 74 53 68 61 72 65 64   source BtShared
18fee 20 0a 2a 2a 20 20 20 20 20 20 20 73 74 72 75 63   .**       struc
18fef 74 75 72 65 2c 20 69 6e 20 74 68 61 74 20 6f 72  ture, in that or
18ff0 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a  der..**.**     *
18ff1 20 56 69 61 20 74 68 65 20 42 61 63 6b 75 70 55   Via the BackupU
18ff2 70 64 61 74 65 28 29 20 61 6e 64 20 42 61 63 6b  pdate() and Back
18ff3 75 70 52 65 73 74 61 72 74 28 29 20 66 75 6e 63  upRestart() func
18ff4 74 69 6f 6e 73 2c 20 77 68 69 63 68 20 61 72 65  tions, which are
18ff5 0a 2a 2a 20 20 20 20 20 20 20 69 6e 76 6f 6b 65  .**       invoke
18ff6 64 20 62 79 20 74 68 65 20 70 61 67 65 72 20 6c  d by the pager l
18ff7 61 79 65 72 20 74 6f 20 72 65 70 6f 72 74 20 76  ayer to report v
18ff8 61 72 69 6f 75 73 20 73 74 61 74 65 20 63 68 61  arious state cha
18ff9 6e 67 65 73 20 69 6e 0a 2a 2a 20 20 20 20 20 20  nges in.**      
18ffa 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 20   the page cache 
18ffb 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
18ffc 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62  the source datab
18ffd 61 73 65 2e 20 54 68 65 20 6d 75 74 65 78 0a 2a  ase. The mutex.*
18ffe 2a 20 20 20 20 20 20 20 61 73 73 6f 63 69 61 74  *       associat
18fff 65 64 20 77 69 74 68 20 74 68 65 20 73 6f 75 72  ed with the sour
19000 63 65 20 64 61 74 61 62 61 73 65 20 42 74 53 68  ce database BtSh
19001 61 72 65 64 20 73 74 72 75 63 74 75 72 65 20 77  ared structure w
19002 69 6c 6c 20 61 6c 77 61 79 73 20 0a 2a 2a 20 20  ill always .**  
19003 20 20 20 20 20 62 65 20 68 65 6c 64 20 77 68 65       be held whe
19004 6e 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 73  n either of thes
19005 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  e functions are 
19006 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 20  invoked..**.**  
19007 20 54 68 65 20 6f 74 68 65 72 20 73 71 6c 69 74   The other sqlit
19008 65 33 5f 62 61 63 6b 75 70 5f 58 58 58 28 29 20  e3_backup_XXX() 
19009 41 50 49 20 66 75 6e 63 74 69 6f 6e 73 2c 20 62  API functions, b
1900a 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28  ackup_remaining(
1900b 29 20 61 6e 64 0a 2a 2a 20 20 20 62 61 63 6b 75  ) and.**   backu
1900c 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 20 61 72  p_pagecount() ar
1900d 65 20 6e 6f 74 20 74 68 72 65 61 64 2d 73 61 66  e not thread-saf
1900e 65 20 66 75 6e 63 74 69 6f 6e 73 2e 20 49 66 20  e functions. If 
1900f 74 68 65 79 20 61 72 65 20 63 61 6c 6c 65 64 0a  they are called.
19010 2a 2a 20 20 20 77 68 69 6c 65 20 73 6f 6d 65 20  **   while some 
19011 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20  other thread is 
19012 63 61 6c 6c 69 6e 67 20 62 61 63 6b 75 70 5f 73  calling backup_s
19013 74 65 70 28 29 20 6f 72 20 62 61 63 6b 75 70 5f  tep() or backup_
19014 66 69 6e 69 73 68 28 29 2c 0a 2a 2a 20 20 20 74  finish(),.**   t
19015 68 65 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e  he values return
19016 65 64 20 6d 61 79 20 62 65 20 69 6e 76 61 6c 69  ed may be invali
19017 64 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 77  d. There is no w
19018 61 79 20 66 6f 72 20 61 20 63 61 6c 6c 20 74 6f  ay for a call to
19019 0a 2a 2a 20 20 20 42 61 63 6b 75 70 55 70 64 61  .**   BackupUpda
1901a 74 65 28 29 20 6f 72 20 42 61 63 6b 75 70 52 65  te() or BackupRe
1901b 73 74 61 72 74 28 29 20 74 6f 20 69 6e 74 65 72  start() to inter
1901c 66 65 72 65 20 77 69 74 68 20 62 61 63 6b 75 70  fere with backup
1901d 5f 72 65 6d 61 69 6e 69 6e 67 28 29 0a 2a 2a 20  _remaining().** 
1901e 20 20 6f 72 20 62 61 63 6b 75 70 5f 70 61 67 65    or backup_page
1901f 63 6f 75 6e 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 20  count()..**.**  
19020 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68   Depending on th
19021 65 20 53 51 4c 69 74 65 20 63 6f 6e 66 69 67 75  e SQLite configu
19022 72 61 74 69 6f 6e 2c 20 74 68 65 20 64 61 74 61  ration, the data
19023 62 61 73 65 20 68 61 6e 64 6c 65 73 20 61 6e 64  base handles and
19024 2f 6f 72 0a 2a 2a 20 20 20 74 68 65 20 42 74 72  /or.**   the Btr
19025 65 65 20 6f 62 6a 65 63 74 73 20 6d 61 79 20 68  ee objects may h
19026 61 76 65 20 74 68 65 69 72 20 6f 77 6e 20 6d 75  ave their own mu
19027 74 65 78 65 73 20 74 68 61 74 20 72 65 71 75 69  texes that requi
19028 72 65 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20  re locking..**  
19029 20 4e 6f 6e 2d 73 68 61 72 61 62 6c 65 20 42 74   Non-sharable Bt
1902a 72 65 65 73 20 28 69 6e 2d 6d 65 6d 6f 72 79 20  rees (in-memory 
1902b 64 61 74 61 62 61 73 65 73 20 66 6f 72 20 65 78  databases for ex
1902c 61 6d 70 6c 65 29 2c 20 64 6f 20 6e 6f 74 20 68  ample), do not h
1902d 61 76 65 0a 2a 2a 20 20 20 61 73 73 6f 63 69 61  ave.**   associa
1902e 74 65 64 20 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a  ted mutexes..*/.
1902f 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
19030 70 6f 69 6e 74 65 72 20 63 6f 72 72 65 73 70 6f  pointer correspo
19031 6e 64 69 6e 67 20 74 6f 20 64 61 74 61 62 61 73  nding to databas
19032 65 20 7a 44 62 20 28 69 2e 65 2e 20 22 6d 61 69  e zDb (i.e. "mai
19033 6e 22 2c 20 22 74 65 6d 70 22 29 0a 2a 2a 20 69  n", "temp").** i
19034 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e  n connection han
19035 64 6c 65 20 70 44 62 2e 20 49 66 20 73 75 63 68  dle pDb. If such
19036 20 61 20 64 61 74 61 62 61 73 65 20 63 61 6e 6e   a database cann
19037 6f 74 20 62 65 20 66 6f 75 6e 64 2c 20 72 65 74  ot be found, ret
19038 75 72 6e 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f  urn.** a NULL po
19039 69 6e 74 65 72 20 61 6e 64 20 77 72 69 74 65 20  inter and write 
1903a 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
1903b 20 74 6f 20 70 45 72 72 6f 72 44 62 2e 0a 2a 2a   to pErrorDb..**
1903c 0a 2a 2a 20 49 66 20 74 68 65 20 22 74 65 6d 70  .** If the "temp
1903d 22 20 64 61 74 61 62 61 73 65 20 69 73 20 72 65  " database is re
1903e 71 75 65 73 74 65 64 2c 20 69 74 20 6d 61 79 20  quested, it may 
1903f 6e 65 65 64 20 74 6f 20 62 65 20 6f 70 65 6e 65  need to be opene
19040 64 20 62 79 20 74 68 69 73 20 0a 2a 2a 20 66 75  d by this .** fu
19041 6e 63 74 69 6f 6e 2e 20 49 66 20 61 6e 20 65 72  nction. If an er
19042 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65  ror occurs while
19043 20 64 6f 69 6e 67 20 73 6f 2c 20 72 65 74 75 72   doing so, retur
19044 6e 20 30 20 61 6e 64 20 77 72 69 74 65 20 61 6e  n 0 and write an
19045 20 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61   .** error messa
19046 67 65 20 74 6f 20 70 45 72 72 6f 72 44 62 2e 0a  ge to pErrorDb..
19047 2a 2f 0a 73 74 61 74 69 63 20 42 74 72 65 65 20  */.static Btree 
19048 2a 66 69 6e 64 42 74 72 65 65 28 73 71 6c 69 74  *findBtree(sqlit
19049 65 33 20 2a 70 45 72 72 6f 72 44 62 2c 20 73 71  e3 *pErrorDb, sq
1904a 6c 69 74 65 33 20 2a 70 44 62 2c 20 63 6f 6e 73  lite3 *pDb, cons
1904b 74 20 63 68 61 72 20 2a 7a 44 62 29 7b 0a 20 20  t char *zDb){.  
1904c 69 6e 74 20 69 20 3d 20 73 71 6c 69 74 65 33 46  int i = sqlite3F
1904d 69 6e 64 44 62 4e 61 6d 65 28 70 44 62 2c 20 7a  indDbName(pDb, z
1904e 44 62 29 3b 0a 0a 20 20 69 66 28 20 69 3d 3d 31  Db);..  if( i==1
1904f 20 29 7b 0a 20 20 20 20 50 61 72 73 65 20 2a 70   ){.    Parse *p
19050 50 61 72 73 65 3b 0a 20 20 20 20 69 6e 74 20 72  Parse;.    int r
19051 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 72 73  c = 0;.    pPars
19052 65 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63 6b  e = sqlite3Stack
19053 41 6c 6c 6f 63 5a 65 72 6f 28 70 45 72 72 6f 72  AllocZero(pError
19054 44 62 2c 20 73 69 7a 65 6f 66 28 2a 70 50 61 72  Db, sizeof(*pPar
19055 73 65 29 29 3b 0a 20 20 20 20 69 66 28 20 70 50  se));.    if( pP
19056 61 72 73 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20  arse==0 ){.     
19057 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 45   sqlite3Error(pE
19058 72 72 6f 72 44 62 2c 20 53 51 4c 49 54 45 5f 4e  rrorDb, SQLITE_N
19059 4f 4d 45 4d 2c 20 22 6f 75 74 20 6f 66 20 6d 65  OMEM, "out of me
1905a 6d 6f 72 79 22 29 3b 0a 20 20 20 20 20 20 72 63  mory");.      rc
1905b 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
1905c 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1905d 20 20 70 50 61 72 73 65 2d 3e 64 62 20 3d 20 70    pParse->db = p
1905e 44 62 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71  Db;.      if( sq
1905f 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74  lite3OpenTempDat
19060 61 62 61 73 65 28 70 50 61 72 73 65 29 20 29 7b  abase(pParse) ){
19061 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
19062 45 72 72 6f 72 43 6c 65 61 72 28 70 50 61 72 73  ErrorClear(pPars
19063 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  e);.        sqli
19064 74 65 33 45 72 72 6f 72 28 70 45 72 72 6f 72 44  te3Error(pErrorD
19065 62 2c 20 70 50 61 72 73 65 2d 3e 72 63 2c 20 22  b, pParse->rc, "
19066 25 73 22 2c 20 70 50 61 72 73 65 2d 3e 7a 45 72  %s", pParse->zEr
19067 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 20 20 72  rMsg);.        r
19068 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
19069 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1906a 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65  sqlite3StackFree
1906b 28 70 45 72 72 6f 72 44 62 2c 20 70 50 61 72 73  (pErrorDb, pPars
1906c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  e);.    }.    if
1906d 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65  ( rc ){.      re
1906e 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
1906f 7d 0a 0a 20 20 69 66 28 20 69 3c 30 20 29 7b 0a  }..  if( i<0 ){.
19070 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
19071 28 70 45 72 72 6f 72 44 62 2c 20 53 51 4c 49 54  (pErrorDb, SQLIT
19072 45 5f 45 52 52 4f 52 2c 20 22 75 6e 6b 6e 6f 77  E_ERROR, "unknow
19073 6e 20 64 61 74 61 62 61 73 65 20 25 73 22 2c 20  n database %s", 
19074 7a 44 62 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  zDb);.    return
19075 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72   0;.  }..  retur
19076 6e 20 70 44 62 2d 3e 61 44 62 5b 69 5d 2e 70 42  n pDb->aDb[i].pB
19077 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61  t;.}../*.** Crea
19078 74 65 20 61 6e 20 73 71 6c 69 74 65 33 5f 62 61  te an sqlite3_ba
19079 63 6b 75 70 20 70 72 6f 63 65 73 73 20 74 6f 20  ckup process to 
1907a 63 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74  copy the content
1907b 73 20 6f 66 20 7a 53 72 63 44 62 20 66 72 6f 6d  s of zSrcDb from
1907c 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68  .** connection h
1907d 61 6e 64 6c 65 20 70 53 72 63 44 62 20 74 6f 20  andle pSrcDb to 
1907e 7a 44 65 73 74 44 62 20 69 6e 20 70 44 65 73 74  zDestDb in pDest
1907f 44 62 2e 20 49 66 20 73 75 63 63 65 73 73 66 75  Db. If successfu
19080 6c 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70  l, return.** a p
19081 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65  ointer to the ne
19082 77 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  w sqlite3_backup
19083 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49   object..**.** I
19084 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
19085 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72  s, NULL is retur
19086 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 72 6f 72  ned and an error
19087 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f 72 20   code and error 
19088 6d 65 73 73 61 67 65 0a 2a 2a 20 73 74 6f 72 65  message.** store
19089 64 20 69 6e 20 64 61 74 61 62 61 73 65 20 68 61  d in database ha
1908a 6e 64 6c 65 20 70 44 65 73 74 44 62 2e 0a 2a 2f  ndle pDestDb..*/
1908b 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
1908c 74 65 33 5f 62 61 63 6b 75 70 20 2a 73 71 6c 69  te3_backup *sqli
1908d 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28  te3_backup_init(
1908e 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 44 65 73  .  sqlite3* pDes
1908f 74 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  tDb,            
19090 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
19091 62 61 73 65 20 74 6f 20 77 72 69 74 65 20 74 6f  base to write to
19092 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
19093 20 2a 7a 44 65 73 74 44 62 2c 20 20 20 20 20 20   *zDestDb,      
19094 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
19095 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20  ame of database 
19096 77 69 74 68 69 6e 20 70 44 65 73 74 44 62 20 2a  within pDestDb *
19097 2f 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 53 72  /.  sqlite3* pSr
19098 63 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  cDb,            
19099 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
1909a 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1909b 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f   to read from */
1909c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1909d 53 72 63 44 62 20 20 20 20 20 20 20 20 20 20 20  SrcDb           
1909e 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
1909f 20 6f 66 20 64 61 74 61 62 61 73 65 20 77 69 74   of database wit
190a0 68 69 6e 20 70 53 72 63 44 62 20 2a 2f 0a 29 7b  hin pSrcDb */.){
190a1 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  .  sqlite3_backu
190a2 70 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20  p *p;           
190a3 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
190a4 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a  e to return */..
190a5 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 73 6f    /* Lock the so
190a6 75 72 63 65 20 64 61 74 61 62 61 73 65 20 68 61  urce database ha
190a7 6e 64 6c 65 2e 20 54 68 65 20 64 65 73 74 69 6e  ndle. The destin
190a8 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 0a 20  ation database. 
190a9 20 2a 2a 20 68 61 6e 64 6c 65 20 69 73 20 6e 6f   ** handle is no
190aa 74 20 6c 6f 63 6b 65 64 20 69 6e 20 74 68 69 73  t locked in this
190ab 20 72 6f 75 74 69 6e 65 2c 20 62 75 74 20 69 74   routine, but it
190ac 20 69 73 20 6c 6f 63 6b 65 64 20 69 6e 0a 20 20   is locked in.  
190ad 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ** sqlite3_backu
190ae 70 5f 73 74 65 70 28 29 2e 20 54 68 65 20 75 73  p_step(). The us
190af 65 72 20 69 73 20 72 65 71 75 69 72 65 64 20 74  er is required t
190b0 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 6e 6f  o ensure that no
190b1 0a 20 20 2a 2a 20 6f 74 68 65 72 20 74 68 72 65  .  ** other thre
190b2 61 64 20 61 63 63 65 73 73 65 73 20 74 68 65 20  ad accesses the 
190b3 64 65 73 74 69 6e 61 74 69 6f 6e 20 68 61 6e 64  destination hand
190b4 6c 65 20 66 6f 72 20 74 68 65 20 64 75 72 61 74  le for the durat
190b5 69 6f 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  ion.  ** of the 
190b6 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
190b7 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74  .  Any attempt t
190b8 6f 20 75 73 65 20 74 68 65 20 64 65 73 74 69 6e  o use the destin
190b9 61 74 69 6f 6e 0a 20 20 2a 2a 20 64 61 74 61 62  ation.  ** datab
190ba 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77  ase connection w
190bb 68 69 6c 65 20 61 20 62 61 63 6b 75 70 20 69 73  hile a backup is
190bc 20 69 6e 20 70 72 6f 67 72 65 73 73 20 6d 61 79   in progress may
190bd 20 63 61 75 73 65 0a 20 20 2a 2a 20 61 20 6d 61   cause.  ** a ma
190be 6c 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 20 64  lfunction or a d
190bf 65 61 64 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20  eadlock..  */.  
190c0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
190c1 74 65 72 28 70 53 72 63 44 62 2d 3e 6d 75 74 65  ter(pSrcDb->mute
190c2 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  x);.  sqlite3_mu
190c3 74 65 78 5f 65 6e 74 65 72 28 70 44 65 73 74 44  tex_enter(pDestD
190c4 62 2d 3e 6d 75 74 65 78 29 3b 0a 0a 20 20 69 66  b->mutex);..  if
190c5 28 20 70 53 72 63 44 62 3d 3d 70 44 65 73 74 44  ( pSrcDb==pDestD
190c6 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  b ){.    sqlite3
190c7 45 72 72 6f 72 28 0a 20 20 20 20 20 20 20 20 70  Error(.        p
190c8 44 65 73 74 44 62 2c 20 53 51 4c 49 54 45 5f 45  DestDb, SQLITE_E
190c9 52 52 4f 52 2c 20 22 73 6f 75 72 63 65 20 61 6e  RROR, "source an
190ca 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6d 75  d destination mu
190cb 73 74 20 62 65 20 64 69 73 74 69 6e 63 74 22 0a  st be distinct".
190cc 20 20 20 20 29 3b 0a 20 20 20 20 70 20 3d 20 30      );.    p = 0
190cd 3b 0a 20 20 7d 65 6c 73 65 20 7b 0a 20 20 20 20  ;.  }else {.    
190ce 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63  /* Allocate spac
190cf 65 20 66 6f 72 20 61 20 6e 65 77 20 73 71 6c 69  e for a new sqli
190d0 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63  te3_backup objec
190d1 74 20 2a 2f 0a 20 20 20 20 70 20 3d 20 28 73 71  t */.    p = (sq
190d2 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 29 73  lite3_backup *)s
190d3 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
190d4 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 62 61 63  zeof(sqlite3_bac
190d5 6b 75 70 29 29 3b 0a 20 20 20 20 69 66 28 20 21  kup));.    if( !
190d6 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  p ){.      sqlit
190d7 65 33 45 72 72 6f 72 28 70 44 65 73 74 44 62 2c  e3Error(pDestDb,
190d8 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30   SQLITE_NOMEM, 0
190d9 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
190da 2f 2a 20 49 66 20 74 68 65 20 61 6c 6c 6f 63 61  /* If the alloca
190db 74 69 6f 6e 20 73 75 63 63 65 65 64 65 64 2c 20  tion succeeded, 
190dc 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6e 65 77  populate the new
190dd 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 20 69 66   object. */.  if
190de 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65  ( p ){.    memse
190df 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  t(p, 0, sizeof(s
190e0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 29 29 3b  qlite3_backup));
190e1 0a 20 20 20 20 70 2d 3e 70 53 72 63 20 3d 20 66  .    p->pSrc = f
190e2 69 6e 64 42 74 72 65 65 28 70 44 65 73 74 44 62  indBtree(pDestDb
190e3 2c 20 70 53 72 63 44 62 2c 20 7a 53 72 63 44 62  , pSrcDb, zSrcDb
190e4 29 3b 0a 20 20 20 20 70 2d 3e 70 44 65 73 74 20  );.    p->pDest 
190e5 3d 20 66 69 6e 64 42 74 72 65 65 28 70 44 65 73  = findBtree(pDes
190e6 74 44 62 2c 20 70 44 65 73 74 44 62 2c 20 7a 44  tDb, pDestDb, zD
190e7 65 73 74 44 62 29 3b 0a 20 20 20 20 70 2d 3e 70  estDb);.    p->p
190e8 44 65 73 74 44 62 20 3d 20 70 44 65 73 74 44 62  DestDb = pDestDb
190e9 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 44 62 20  ;.    p->pSrcDb 
190ea 3d 20 70 53 72 63 44 62 3b 0a 20 20 20 20 70 2d  = pSrcDb;.    p-
190eb 3e 69 4e 65 78 74 20 3d 20 31 3b 0a 20 20 20 20  >iNext = 1;.    
190ec 70 2d 3e 69 73 41 74 74 61 63 68 65 64 20 3d 20  p->isAttached = 
190ed 30 3b 0a 0a 20 20 20 20 69 66 28 20 30 3d 3d 70  0;..    if( 0==p
190ee 2d 3e 70 53 72 63 20 7c 7c 20 30 3d 3d 70 2d 3e  ->pSrc || 0==p->
190ef 70 44 65 73 74 20 29 7b 0a 20 20 20 20 20 20 2f  pDest ){.      /
190f0 2a 20 4f 6e 65 20 28 6f 72 20 62 6f 74 68 29 20  * One (or both) 
190f1 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 64 61 74  of the named dat
190f2 61 62 61 73 65 73 20 64 69 64 20 6e 6f 74 20 65  abases did not e
190f3 78 69 73 74 2e 20 41 6e 20 65 72 72 6f 72 20 68  xist. An error h
190f4 61 73 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72 65  as.      ** alre
190f5 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e  ady been written
190f6 20 69 6e 74 6f 20 74 68 65 20 70 44 65 73 74 44   into the pDestD
190f7 62 20 68 61 6e 64 6c 65 2e 20 41 6c 6c 20 74 68  b handle. All th
190f8 61 74 20 69 73 20 6c 65 66 74 0a 20 20 20 20 20  at is left.     
190f9 20 2a 2a 20 74 6f 20 64 6f 20 68 65 72 65 20 69   ** to do here i
190fa 73 20 66 72 65 65 20 74 68 65 20 73 71 6c 69 74  s free the sqlit
190fb 65 33 5f 62 61 63 6b 75 70 20 73 74 72 75 63 74  e3_backup struct
190fc 75 72 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ure..      */.  
190fd 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
190fe 28 70 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 30  (p);.      p = 0
190ff 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
19100 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53  ( p ){.    p->pS
19101 72 63 2d 3e 6e 42 61 63 6b 75 70 2b 2b 3b 0a 20  rc->nBackup++;. 
19102 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   }..  sqlite3_mu
19103 74 65 78 5f 6c 65 61 76 65 28 70 44 65 73 74 44  tex_leave(pDestD
19104 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c  b->mutex);.  sql
19105 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
19106 28 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b  (pSrcDb->mutex);
19107 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
19108 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 72  /*.** Argument r
19109 63 20 69 73 20 61 6e 20 53 51 4c 69 74 65 20 65  c is an SQLite e
1910a 72 72 6f 72 20 63 6f 64 65 2e 20 52 65 74 75 72  rror code. Retur
1910b 6e 20 74 72 75 65 20 69 66 20 74 68 69 73 20 65  n true if this e
1910c 72 72 6f 72 20 69 73 20 0a 2a 2a 20 63 6f 6e 73  rror is .** cons
1910d 69 64 65 72 65 64 20 66 61 74 61 6c 20 69 66 20  idered fatal if 
1910e 65 6e 63 6f 75 6e 74 65 72 65 64 20 64 75 72 69  encountered duri
1910f 6e 67 20 61 20 62 61 63 6b 75 70 20 6f 70 65 72  ng a backup oper
19110 61 74 69 6f 6e 2e 20 41 6c 6c 20 65 72 72 6f 72  ation. All error
19111 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 73 69 64 65  s.** are conside
19112 72 65 64 20 66 61 74 61 6c 20 65 78 63 65 70 74  red fatal except
19113 20 66 6f 72 20 53 51 4c 49 54 45 5f 42 55 53 59   for SQLITE_BUSY
19114 20 61 6e 64 20 53 51 4c 49 54 45 5f 4c 4f 43 4b   and SQLITE_LOCK
19115 45 44 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ED..*/.static in
19116 74 20 69 73 46 61 74 61 6c 45 72 72 6f 72 28 69  t isFatalError(i
19117 6e 74 20 72 63 29 7b 0a 20 20 72 65 74 75 72 6e  nt rc){.  return
19118 20 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20   (rc!=SQLITE_OK 
19119 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 55  && rc!=SQLITE_BU
1911a 53 59 20 26 26 20 41 4c 57 41 59 53 28 72 63 21  SY && ALWAYS(rc!
1911b 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 29 29  =SQLITE_LOCKED))
1911c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d  ;.}../*.** Param
1911d 65 74 65 72 20 7a 53 72 63 44 61 74 61 20 70 6f  eter zSrcData po
1911e 69 6e 74 73 20 74 6f 20 61 20 62 75 66 66 65 72  ints to a buffer
1911f 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
19120 64 61 74 61 20 66 6f 72 20 0a 2a 2a 20 70 61 67  data for .** pag
19121 65 20 69 53 72 63 50 67 20 66 72 6f 6d 20 74 68  e iSrcPg from th
19122 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73  e source databas
19123 65 2e 20 43 6f 70 79 20 74 68 69 73 20 64 61 74  e. Copy this dat
19124 61 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 64  a into the .** d
19125 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
19126 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ase..*/.static i
19127 6e 74 20 62 61 63 6b 75 70 4f 6e 65 50 61 67 65  nt backupOnePage
19128 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
19129 2a 70 2c 20 50 67 6e 6f 20 69 53 72 63 50 67 2c  *p, Pgno iSrcPg,
1912a 20 63 6f 6e 73 74 20 75 38 20 2a 7a 53 72 63 44   const u8 *zSrcD
1912b 61 74 61 29 7b 0a 20 20 50 61 67 65 72 20 2a 20  ata){.  Pager * 
1912c 63 6f 6e 73 74 20 70 44 65 73 74 50 61 67 65 72  const pDestPager
1912d 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50   = sqlite3BtreeP
1912e 61 67 65 72 28 70 2d 3e 70 44 65 73 74 29 3b 0a  ager(p->pDest);.
1912f 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 53 72 63    const int nSrc
19130 50 67 73 7a 20 3d 20 73 71 6c 69 74 65 33 42 74  Pgsz = sqlite3Bt
19131 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70  reeGetPageSize(p
19132 2d 3e 70 53 72 63 29 3b 0a 20 20 69 6e 74 20 6e  ->pSrc);.  int n
19133 44 65 73 74 50 67 73 7a 20 3d 20 73 71 6c 69 74  DestPgsz = sqlit
19134 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69  e3BtreeGetPageSi
19135 7a 65 28 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20  ze(p->pDest);.  
19136 63 6f 6e 73 74 20 69 6e 74 20 6e 43 6f 70 79 20  const int nCopy 
19137 3d 20 4d 49 4e 28 6e 53 72 63 50 67 73 7a 2c 20  = MIN(nSrcPgsz, 
19138 6e 44 65 73 74 50 67 73 7a 29 3b 0a 20 20 63 6f  nDestPgsz);.  co
19139 6e 73 74 20 69 36 34 20 69 45 6e 64 20 3d 20 28  nst i64 iEnd = (
1913a 69 36 34 29 69 53 72 63 50 67 2a 28 69 36 34 29  i64)iSrcPg*(i64)
1913b 6e 53 72 63 50 67 73 7a 3b 0a 0a 20 20 69 6e 74  nSrcPgsz;..  int
1913c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1913d 0a 20 20 69 36 34 20 69 4f 66 66 3b 0a 0a 20 20  .  i64 iOff;..  
1913e 61 73 73 65 72 74 28 20 70 2d 3e 62 44 65 73 74  assert( p->bDest
1913f 4c 6f 63 6b 65 64 20 29 3b 0a 20 20 61 73 73 65  Locked );.  asse
19140 72 74 28 20 21 69 73 46 61 74 61 6c 45 72 72 6f  rt( !isFatalErro
19141 72 28 70 2d 3e 72 63 29 20 29 3b 0a 20 20 61 73  r(p->rc) );.  as
19142 73 65 72 74 28 20 69 53 72 63 50 67 21 3d 50 45  sert( iSrcPg!=PE
19143 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
19144 70 2d 3e 70 53 72 63 2d 3e 70 42 74 29 20 29 3b  p->pSrc->pBt) );
19145 0a 20 20 61 73 73 65 72 74 28 20 7a 53 72 63 44  .  assert( zSrcD
19146 61 74 61 20 29 3b 0a 0a 20 20 2f 2a 20 43 61 74  ata );..  /* Cat
19147 63 68 20 74 68 65 20 63 61 73 65 20 77 68 65 72  ch the case wher
19148 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  e the destinatio
19149 6e 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  n is an in-memor
1914a 79 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74  y database and t
1914b 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 73 69 7a  he.  ** page siz
1914c 65 73 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65  es of the source
1914d 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e   and destination
1914e 20 64 69 66 66 65 72 2e 20 0a 20 20 2a 2f 0a 20   differ. .  */. 
1914f 20 69 66 28 20 6e 53 72 63 50 67 73 7a 21 3d 6e   if( nSrcPgsz!=n
19150 44 65 73 74 50 67 73 7a 20 26 26 20 73 71 6c 69  DestPgsz && sqli
19151 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28  te3PagerIsMemdb(
19152 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
19153 72 28 70 2d 3e 70 44 65 73 74 29 29 20 29 7b 0a  r(p->pDest)) ){.
19154 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
19155 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20  READONLY;.  }.. 
19156 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 72 75   /* This loop ru
19157 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ns once for each
19158 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67   destination pag
19159 65 20 73 70 61 6e 6e 65 64 20 62 79 20 74 68 65  e spanned by the
1915a 20 73 6f 75 72 63 65 20 0a 20 20 2a 2a 20 70 61   source .  ** pa
1915b 67 65 2e 20 46 6f 72 20 65 61 63 68 20 69 74 65  ge. For each ite
1915c 72 61 74 69 6f 6e 2c 20 76 61 72 69 61 62 6c 65  ration, variable
1915d 20 69 4f 66 66 20 69 73 20 73 65 74 20 74 6f 20   iOff is set to 
1915e 74 68 65 20 62 79 74 65 20 6f 66 66 73 65 74 0a  the byte offset.
1915f 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 73 74    ** of the dest
19160 69 6e 61 74 69 6f 6e 20 70 61 67 65 2e 0a 20 20  ination page..  
19161 2a 2f 0a 20 20 66 6f 72 28 69 4f 66 66 3d 69 45  */.  for(iOff=iE
19162 6e 64 2d 28 69 36 34 29 6e 53 72 63 50 67 73 7a  nd-(i64)nSrcPgsz
19163 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ; rc==SQLITE_OK 
19164 26 26 20 69 4f 66 66 3c 69 45 6e 64 3b 20 69 4f  && iOff<iEnd; iO
19165 66 66 2b 3d 6e 44 65 73 74 50 67 73 7a 29 7b 0a  ff+=nDestPgsz){.
19166 20 20 20 20 44 62 50 61 67 65 20 2a 70 44 65 73      DbPage *pDes
19167 74 50 67 20 3d 20 30 3b 0a 20 20 20 20 50 67 6e  tPg = 0;.    Pgn
19168 6f 20 69 44 65 73 74 20 3d 20 28 50 67 6e 6f 29  o iDest = (Pgno)
19169 28 69 4f 66 66 2f 6e 44 65 73 74 50 67 73 7a 29  (iOff/nDestPgsz)
1916a 2b 31 3b 0a 20 20 20 20 69 66 28 20 69 44 65 73  +1;.    if( iDes
1916b 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  t==PENDING_BYTE_
1916c 50 41 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e 70  PAGE(p->pDest->p
1916d 42 74 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  Bt) ) continue;.
1916e 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f      if( SQLITE_O
1916f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K==(rc = sqlite3
19170 50 61 67 65 72 47 65 74 28 70 44 65 73 74 50 61  PagerGet(pDestPa
19171 67 65 72 2c 20 69 44 65 73 74 2c 20 26 70 44 65  ger, iDest, &pDe
19172 73 74 50 67 29 29 0a 20 20 20 20 20 26 26 20 53  stPg)).     && S
19173 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20  QLITE_OK==(rc = 
19174 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
19175 65 28 70 44 65 73 74 50 67 29 29 0a 20 20 20 20  e(pDestPg)).    
19176 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75  ){.      const u
19177 38 20 2a 7a 49 6e 20 3d 20 26 7a 53 72 63 44 61  8 *zIn = &zSrcDa
19178 74 61 5b 69 4f 66 66 25 6e 53 72 63 50 67 73 7a  ta[iOff%nSrcPgsz
19179 5d 3b 0a 20 20 20 20 20 20 75 38 20 2a 7a 44 65  ];.      u8 *zDe
1917a 73 74 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33  stData = sqlite3
1917b 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44 65  PagerGetData(pDe
1917c 73 74 50 67 29 3b 0a 20 20 20 20 20 20 75 38 20  stPg);.      u8 
1917d 2a 7a 4f 75 74 20 3d 20 26 7a 44 65 73 74 44 61  *zOut = &zDestDa
1917e 74 61 5b 69 4f 66 66 25 6e 44 65 73 74 50 67 73  ta[iOff%nDestPgs
1917f 7a 5d 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f  z];..      /* Co
19180 70 79 20 74 68 65 20 64 61 74 61 20 66 72 6f 6d  py the data from
19181 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65   the source page
19182 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74 69 6e   into the destin
19183 61 74 69 6f 6e 20 70 61 67 65 2e 0a 20 20 20 20  ation page..    
19184 20 20 2a 2a 20 54 68 65 6e 20 63 6c 65 61 72 20    ** Then clear 
19185 74 68 65 20 42 74 72 65 65 20 6c 61 79 65 72 20  the Btree layer 
19186 4d 65 6d 50 61 67 65 2e 69 73 49 6e 69 74 20 66  MemPage.isInit f
19187 6c 61 67 2e 20 42 6f 74 68 20 74 68 69 73 20 6d  lag. Both this m
19188 6f 64 75 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61  odule.      ** a
19189 6e 64 20 74 68 65 20 70 61 67 65 72 20 63 6f 64  nd the pager cod
1918a 65 20 75 73 65 20 74 68 69 73 20 74 72 69 63 6b  e use this trick
1918b 20 28 63 6c 65 61 72 69 6e 67 20 74 68 65 20 66   (clearing the f
1918c 69 72 73 74 20 62 79 74 65 0a 20 20 20 20 20 20  irst byte.      
1918d 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 20 27  ** of the page '
1918e 65 78 74 72 61 27 20 73 70 61 63 65 20 74 6f 20  extra' space to 
1918f 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 42  invalidate the B
19190 74 72 65 65 20 6c 61 79 65 72 73 0a 20 20 20 20  tree layers.    
19191 20 20 2a 2a 20 63 61 63 68 65 64 20 70 61 72 73    ** cached pars
19192 65 20 6f 66 20 74 68 65 20 70 61 67 65 29 2e 20  e of the page). 
19193 4d 65 6d 50 61 67 65 2e 69 73 49 6e 69 74 20 69  MemPage.isInit i
19194 73 20 6d 61 72 6b 65 64 20 0a 20 20 20 20 20 20  s marked .      
19195 2a 2a 20 22 4d 55 53 54 20 42 45 20 46 49 52 53  ** "MUST BE FIRS
19196 54 22 20 66 6f 72 20 74 68 69 73 20 70 75 72 70  T" for this purp
19197 6f 73 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ose..      */.  
19198 20 20 20 20 6d 65 6d 63 70 79 28 7a 4f 75 74 2c      memcpy(zOut,
19199 20 7a 49 6e 2c 20 6e 43 6f 70 79 29 3b 0a 20 20   zIn, nCopy);.  
1919a 20 20 20 20 28 28 75 38 20 2a 29 73 71 6c 69 74      ((u8 *)sqlit
1919b 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28  e3PagerGetExtra(
1919c 70 44 65 73 74 50 67 29 29 5b 30 5d 20 3d 20 30  pDestPg))[0] = 0
1919d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
1919e 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44  te3PagerUnref(pD
1919f 65 73 74 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 72  estPg);.  }..  r
191a0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
191a1 2a 2a 20 49 66 20 70 46 69 6c 65 20 69 73 20 63  ** If pFile is c
191a2 75 72 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 20  urrently larger 
191a3 74 68 61 6e 20 69 53 69 7a 65 20 62 79 74 65 73  than iSize bytes
191a4 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65 20  , then truncate 
191a5 69 74 20 74 6f 0a 2a 2a 20 65 78 61 63 74 6c 79  it to.** exactly
191a6 20 69 53 69 7a 65 20 62 79 74 65 73 2e 20 49 66   iSize bytes. If
191a7 20 70 46 69 6c 65 20 69 73 20 6e 6f 74 20 6c 61   pFile is not la
191a8 72 67 65 72 20 74 68 61 6e 20 69 53 69 7a 65 20  rger than iSize 
191a9 62 79 74 65 73 2c 20 74 68 65 6e 0a 2a 2a 20 74  bytes, then.** t
191aa 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
191ab 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 52  a no-op..**.** R
191ac 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
191ad 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73  if everything is
191ae 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 20   successful, or 
191af 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20  an SQLite error 
191b0 0a 2a 2a 20 63 6f 64 65 20 69 66 20 61 6e 20 65  .** code if an e
191b1 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a  rror occurs..*/.
191b2 73 74 61 74 69 63 20 69 6e 74 20 62 61 63 6b 75  static int backu
191b3 70 54 72 75 6e 63 61 74 65 46 69 6c 65 28 73 71  pTruncateFile(sq
191b4 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c  lite3_file *pFil
191b5 65 2c 20 69 36 34 20 69 53 69 7a 65 29 7b 0a 20  e, i64 iSize){. 
191b6 20 69 36 34 20 69 43 75 72 72 65 6e 74 3b 0a 20   i64 iCurrent;. 
191b7 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65   int rc = sqlite
191b8 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 46 69 6c  3OsFileSize(pFil
191b9 65 2c 20 26 69 43 75 72 72 65 6e 74 29 3b 0a 20  e, &iCurrent);. 
191ba 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
191bb 4f 4b 20 26 26 20 69 43 75 72 72 65 6e 74 3e 69  OK && iCurrent>i
191bc 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 63 20 3d  Size ){.    rc =
191bd 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61   sqlite3OsTrunca
191be 74 65 28 70 46 69 6c 65 2c 20 69 53 69 7a 65 29  te(pFile, iSize)
191bf 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
191c0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69  c;.}../*.** Regi
191c1 73 74 65 72 20 74 68 69 73 20 62 61 63 6b 75 70  ster this backup
191c2 20 6f 62 6a 65 63 74 20 77 69 74 68 20 74 68 65   object with the
191c3 20 61 73 73 6f 63 69 61 74 65 64 20 73 6f 75 72   associated sour
191c4 63 65 20 70 61 67 65 72 20 66 6f 72 0a 2a 2a 20  ce pager for.** 
191c5 63 61 6c 6c 62 61 63 6b 73 20 77 68 65 6e 20 70  callbacks when p
191c6 61 67 65 73 20 61 72 65 20 63 68 61 6e 67 65 64  ages are changed
191c7 20 6f 72 20 74 68 65 20 63 61 63 68 65 20 69 6e   or the cache in
191c8 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2f 0a 73 74  validated..*/.st
191c9 61 74 69 63 20 76 6f 69 64 20 61 74 74 61 63 68  atic void attach
191ca 42 61 63 6b 75 70 4f 62 6a 65 63 74 28 73 71 6c  BackupObject(sql
191cb 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 7b  ite3_backup *p){
191cc 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  .  sqlite3_backu
191cd 70 20 2a 2a 70 70 3b 0a 20 20 61 73 73 65 72 74  p **pp;.  assert
191ce 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  ( sqlite3BtreeHo
191cf 6c 64 73 4d 75 74 65 78 28 70 2d 3e 70 53 72 63  ldsMutex(p->pSrc
191d0 29 20 29 3b 0a 20 20 70 70 20 3d 20 73 71 6c 69  ) );.  pp = sqli
191d1 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 50 74  te3PagerBackupPt
191d2 72 28 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  r(sqlite3BtreePa
191d3 67 65 72 28 70 2d 3e 70 53 72 63 29 29 3b 0a 20  ger(p->pSrc));. 
191d4 20 70 2d 3e 70 4e 65 78 74 20 3d 20 2a 70 70 3b   p->pNext = *pp;
191d5 0a 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20 70 2d  .  *pp = p;.  p-
191d6 3e 69 73 41 74 74 61 63 68 65 64 20 3d 20 31 3b  >isAttached = 1;
191d7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 6e  .}../*.** Copy n
191d8 50 61 67 65 20 70 61 67 65 73 20 66 72 6f 6d 20  Page pages from 
191d9 74 68 65 20 73 6f 75 72 63 65 20 62 2d 74 72 65  the source b-tre
191da 65 20 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61  e to the destina
191db 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
191dc 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
191dd 62 61 63 6b 75 70 5f 73 74 65 70 28 73 71 6c 69  backup_step(sqli
191de 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c 20 69  te3_backup *p, i
191df 6e 74 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e 74  nt nPage){.  int
191e0 20 72 63 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f   rc;..  sqlite3_
191e1 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70  mutex_enter(p->p
191e2 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  SrcDb->mutex);. 
191e3 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
191e4 65 72 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 69  er(p->pSrc);.  i
191e5 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b  f( p->pDestDb ){
191e6 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
191e7 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 44 65 73  ex_enter(p->pDes
191e8 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  tDb->mutex);.  }
191e9 0a 0a 20 20 72 63 20 3d 20 70 2d 3e 72 63 3b 0a  ..  rc = p->rc;.
191ea 20 20 69 66 28 20 21 69 73 46 61 74 61 6c 45 72    if( !isFatalEr
191eb 72 6f 72 28 72 63 29 20 29 7b 0a 20 20 20 20 50  ror(rc) ){.    P
191ec 61 67 65 72 20 2a 20 63 6f 6e 73 74 20 70 53 72  ager * const pSr
191ed 63 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33  cPager = sqlite3
191ee 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70 53  BtreePager(p->pS
191ef 72 63 29 3b 20 20 20 20 20 2f 2a 20 53 6f 75 72  rc);     /* Sour
191f0 63 65 20 70 61 67 65 72 20 2a 2f 0a 20 20 20 20  ce pager */.    
191f1 50 61 67 65 72 20 2a 20 63 6f 6e 73 74 20 70 44  Pager * const pD
191f2 65 73 74 50 61 67 65 72 20 3d 20 73 71 6c 69 74  estPager = sqlit
191f3 65 33 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e  e3BtreePager(p->
191f4 70 44 65 73 74 29 3b 20 20 20 2f 2a 20 44 65 73  pDest);   /* Des
191f5 74 20 70 61 67 65 72 20 2a 2f 0a 20 20 20 20 69  t pager */.    i
191f6 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20  nt ii;          
191f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
191f8 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61    /* Iterator va
191f9 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e  riable */.    in
191fa 74 20 6e 53 72 63 50 61 67 65 20 3d 20 2d 31 3b  t nSrcPage = -1;
191fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
191fc 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 6f 75 72   /* Size of sour
191fd 63 65 20 64 62 20 69 6e 20 70 61 67 65 73 20 2a  ce db in pages *
191fe 2f 0a 20 20 20 20 69 6e 74 20 62 43 6c 6f 73 65  /.    int bClose
191ff 54 72 61 6e 73 20 3d 20 30 3b 20 20 20 20 20 20  Trans = 0;      
19200 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
19201 20 69 66 20 73 72 63 20 64 62 20 72 65 71 75 69   if src db requi
19202 72 65 73 20 75 6e 6c 6f 63 6b 69 6e 67 20 2a 2f  res unlocking */
19203 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  ..    /* If the 
19204 73 6f 75 72 63 65 20 70 61 67 65 72 20 69 73 20  source pager is 
19205 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61 20 77  currently in a w
19206 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
19207 2c 20 72 65 74 75 72 6e 0a 20 20 20 20 2a 2a 20  , return.    ** 
19208 53 51 4c 49 54 45 5f 42 55 53 59 20 69 6d 6d 65  SQLITE_BUSY imme
19209 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2f 0a  diately..    */.
1920a 20 20 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74      if( p->pDest
1920b 44 62 20 26 26 20 70 2d 3e 70 53 72 63 2d 3e 70  Db && p->pSrc->p
1920c 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f  Bt->inTransactio
1920d 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  n==TRANS_WRITE )
1920e 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
1920f 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65  ITE_BUSY;.    }e
19210 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
19211 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
19212 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63 6b 20 74 68  ..    /* Lock th
19213 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61  e destination da
19214 74 61 62 61 73 65 2c 20 69 66 20 69 74 20 69 73  tabase, if it is
19215 20 6e 6f 74 20 6c 6f 63 6b 65 64 20 61 6c 72 65   not locked alre
19216 61 64 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ady. */.    if( 
19217 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 26 26  SQLITE_OK==rc &&
19218 20 70 2d 3e 62 44 65 73 74 4c 6f 63 6b 65 64 3d   p->bDestLocked=
19219 3d 30 0a 20 20 20 20 20 26 26 20 53 51 4c 49 54  =0.     && SQLIT
1921a 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK==(rc = sqli
1921b 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61  te3BtreeBeginTra
1921c 6e 73 28 70 2d 3e 70 44 65 73 74 2c 20 32 29 29  ns(p->pDest, 2))
1921d 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 70   .    ){.      p
1921e 2d 3e 62 44 65 73 74 4c 6f 63 6b 65 64 20 3d 20  ->bDestLocked = 
1921f 31 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  1;.      sqlite3
19220 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 2d 3e  BtreeGetMeta(p->
19221 70 44 65 73 74 2c 20 42 54 52 45 45 5f 53 43 48  pDest, BTREE_SCH
19222 45 4d 41 5f 56 45 52 53 49 4f 4e 2c 20 26 70 2d  EMA_VERSION, &p-
19223 3e 69 44 65 73 74 53 63 68 65 6d 61 29 3b 0a 20  >iDestSchema);. 
19224 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
19225 74 68 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e  there is no open
19226 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f   read-transactio
19227 6e 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20  n on the source 
19228 64 61 74 61 62 61 73 65 2c 20 6f 70 65 6e 0a 20  database, open. 
19229 20 20 20 2a 2a 20 6f 6e 65 20 6e 6f 77 2e 20 49     ** one now. I
1922a 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  f a transaction 
1922b 69 73 20 6f 70 65 6e 65 64 20 68 65 72 65 2c 20  is opened here, 
1922c 74 68 65 6e 20 69 74 20 77 69 6c 6c 20 62 65 20  then it will be 
1922d 63 6c 6f 73 65 64 0a 20 20 20 20 2a 2a 20 62 65  closed.    ** be
1922e 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69  fore this functi
1922f 6f 6e 20 65 78 69 74 73 2e 0a 20 20 20 20 2a 2f  on exits..    */
19230 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
19231 49 54 45 5f 4f 4b 20 26 26 20 30 3d 3d 73 71 6c  ITE_OK && 0==sql
19232 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65 61  ite3BtreeIsInRea
19233 64 54 72 61 6e 73 28 70 2d 3e 70 53 72 63 29 20  dTrans(p->pSrc) 
19234 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
19235 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54  lite3BtreeBeginT
19236 72 61 6e 73 28 70 2d 3e 70 53 72 63 2c 20 30 29  rans(p->pSrc, 0)
19237 3b 0a 20 20 20 20 20 20 62 43 6c 6f 73 65 54 72  ;.      bCloseTr
19238 61 6e 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  ans = 1;.    }. 
19239 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 74 68 61   .    /* Now tha
1923a 74 20 74 68 65 72 65 20 69 73 20 61 20 72 65 61  t there is a rea
1923b 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 6f  d-lock on the so
1923c 75 72 63 65 20 64 61 74 61 62 61 73 65 2c 20 71  urce database, q
1923d 75 65 72 79 20 74 68 65 0a 20 20 20 20 2a 2a 20  uery the.    ** 
1923e 73 6f 75 72 63 65 20 70 61 67 65 72 20 66 6f 72  source pager for
1923f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
19240 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61  ages in the data
19241 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  base..    */.   
19242 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
19243 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  OK ){.      rc =
19244 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
19245 65 63 6f 75 6e 74 28 70 53 72 63 50 61 67 65 72  ecount(pSrcPager
19246 2c 20 26 6e 53 72 63 50 61 67 65 29 3b 0a 20 20  , &nSrcPage);.  
19247 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 69 3d 30    }.    for(ii=0
19248 3b 20 28 6e 50 61 67 65 3c 30 20 7c 7c 20 69 69  ; (nPage<0 || ii
19249 3c 6e 50 61 67 65 29 20 26 26 20 70 2d 3e 69 4e  <nPage) && p->iN
1924a 65 78 74 3c 3d 28 50 67 6e 6f 29 6e 53 72 63 50  ext<=(Pgno)nSrcP
1924b 61 67 65 20 26 26 20 21 72 63 3b 20 69 69 2b 2b  age && !rc; ii++
1924c 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 50  ){.      const P
1924d 67 6e 6f 20 69 53 72 63 50 67 20 3d 20 70 2d 3e  gno iSrcPg = p->
1924e 69 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20  iNext;          
1924f 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65         /* Source
19250 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a   page number */.
19251 20 20 20 20 20 20 69 66 28 20 69 53 72 63 50 67        if( iSrcPg
19252 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  !=PENDING_BYTE_P
19253 41 47 45 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74  AGE(p->pSrc->pBt
19254 29 20 29 7b 0a 20 20 20 20 20 20 20 20 44 62 50  ) ){.        DbP
19255 61 67 65 20 2a 70 53 72 63 50 67 3b 20 20 20 20  age *pSrcPg;    
19256 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19257 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72           /* Sour
19258 63 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20 2a  ce page object *
19259 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  /.        rc = s
1925a 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70  qlite3PagerGet(p
1925b 53 72 63 50 61 67 65 72 2c 20 69 53 72 63 50 67  SrcPager, iSrcPg
1925c 2c 20 26 70 53 72 63 50 67 29 3b 0a 20 20 20 20  , &pSrcPg);.    
1925d 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1925e 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1925f 20 20 20 72 63 20 3d 20 62 61 63 6b 75 70 4f 6e     rc = backupOn
19260 65 50 61 67 65 28 70 2c 20 69 53 72 63 50 67 2c  ePage(p, iSrcPg,
19261 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
19262 44 61 74 61 28 70 53 72 63 50 67 29 29 3b 0a 20  Data(pSrcPg));. 
19263 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
19264 50 61 67 65 72 55 6e 72 65 66 28 70 53 72 63 50  PagerUnref(pSrcP
19265 67 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  g);.        }.  
19266 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 69      }.      p->i
19267 4e 65 78 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  Next++;.    }.  
19268 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
19269 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  _OK ){.      p->
1926a 6e 50 61 67 65 63 6f 75 6e 74 20 3d 20 6e 53 72  nPagecount = nSr
1926b 63 50 61 67 65 3b 0a 20 20 20 20 20 20 70 2d 3e  cPage;.      p->
1926c 6e 52 65 6d 61 69 6e 69 6e 67 20 3d 20 6e 53 72  nRemaining = nSr
1926d 63 50 61 67 65 2b 31 2d 70 2d 3e 69 4e 65 78 74  cPage+1-p->iNext
1926e 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69  ;.      if( p->i
1926f 4e 65 78 74 3e 28 50 67 6e 6f 29 6e 53 72 63 50  Next>(Pgno)nSrcP
19270 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72  age ){.        r
19271 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  c = SQLITE_DONE;
19272 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
19273 20 21 70 2d 3e 69 73 41 74 74 61 63 68 65 64 20   !p->isAttached 
19274 29 7b 0a 20 20 20 20 20 20 20 20 61 74 74 61 63  ){.        attac
19275 68 42 61 63 6b 75 70 4f 62 6a 65 63 74 28 70 29  hBackupObject(p)
19276 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
19277 20 20 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 65    .    /* Update
19278 20 74 68 65 20 73 63 68 65 6d 61 20 76 65 72 73   the schema vers
19279 69 6f 6e 20 66 69 65 6c 64 20 69 6e 20 74 68 65  ion field in the
1927a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74   destination dat
1927b 61 62 61 73 65 2e 20 54 68 69 73 0a 20 20 20 20  abase. This.    
1927c 2a 2a 20 69 73 20 74 6f 20 6d 61 6b 65 20 73 75  ** is to make su
1927d 72 65 20 74 68 61 74 20 74 68 65 20 73 63 68 65  re that the sche
1927e 6d 61 2d 76 65 72 73 69 6f 6e 20 72 65 61 6c 6c  ma-version reall
1927f 79 20 64 6f 65 73 20 63 68 61 6e 67 65 20 69 6e  y does change in
19280 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 61 73 65  .    ** the case
19281 20 77 68 65 72 65 20 74 68 65 20 73 6f 75 72 63   where the sourc
19282 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f  e and destinatio
19283 6e 20 64 61 74 61 62 61 73 65 73 20 68 61 76 65  n databases have
19284 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 61 6d 65   the.    ** same
19285 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e   schema version.
19286 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
19287 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
19288 0a 20 20 20 20 20 26 26 20 28 72 63 20 3d 20 73  .     && (rc = s
19289 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74  qlite3BtreeUpdat
1928a 65 4d 65 74 61 28 70 2d 3e 70 44 65 73 74 2c 31  eMeta(p->pDest,1
1928b 2c 70 2d 3e 69 44 65 73 74 53 63 68 65 6d 61 2b  ,p->iDestSchema+
1928c 31 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20  1))==SQLITE_OK. 
1928d 20 20 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73     ){.      cons
1928e 74 20 69 6e 74 20 6e 53 72 63 50 61 67 65 73 69  t int nSrcPagesi
1928f 7a 65 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  ze = sqlite3Btre
19290 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e  eGetPageSize(p->
19291 70 53 72 63 29 3b 0a 20 20 20 20 20 20 63 6f 6e  pSrc);.      con
19292 73 74 20 69 6e 74 20 6e 44 65 73 74 50 61 67 65  st int nDestPage
19293 73 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 42 74  size = sqlite3Bt
19294 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70  reeGetPageSize(p
19295 2d 3e 70 44 65 73 74 29 3b 0a 20 20 20 20 20 20  ->pDest);.      
19296 69 6e 74 20 6e 44 65 73 74 54 72 75 6e 63 61 74  int nDestTruncat
19297 65 3b 0a 20 20 0a 20 20 20 20 20 20 69 66 28 20  e;.  .      if( 
19298 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20  p->pDestDb ){.  
19299 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73        sqlite3Res
1929a 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
1929b 28 70 2d 3e 70 44 65 73 74 44 62 2c 20 30 29 3b  (p->pDestDb, 0);
1929c 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
1929d 2f 2a 20 53 65 74 20 6e 44 65 73 74 54 72 75 6e  /* Set nDestTrun
1929e 63 61 74 65 20 74 6f 20 74 68 65 20 66 69 6e 61  cate to the fina
1929f 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  l number of page
192a0 73 20 69 6e 20 74 68 65 20 64 65 73 74 69 6e 61  s in the destina
192a1 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 64 61  tion.      ** da
192a2 74 61 62 61 73 65 2e 20 54 68 65 20 63 6f 6d 70  tabase. The comp
192a3 6c 69 63 61 74 69 6f 6e 20 68 65 72 65 20 69 73  lication here is
192a4 20 74 68 61 74 20 74 68 65 20 64 65 73 74 69 6e   that the destin
192a5 61 74 69 6f 6e 20 70 61 67 65 0a 20 20 20 20 20  ation page.     
192a6 20 2a 2a 20 73 69 7a 65 20 6d 61 79 20 62 65 20   ** size may be 
192a7 64 69 66 66 65 72 65 6e 74 20 74 6f 20 74 68 65  different to the
192a8 20 73 6f 75 72 63 65 20 70 61 67 65 20 73 69 7a   source page siz
192a9 65 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  e. .      **.   
192aa 20 20 20 2a 2a 20 49 66 20 74 68 65 20 73 6f 75     ** If the sou
192ab 72 63 65 20 70 61 67 65 20 73 69 7a 65 20 69 73  rce page size is
192ac 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68   smaller than th
192ad 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61  e destination pa
192ae 67 65 20 73 69 7a 65 2c 20 0a 20 20 20 20 20 20  ge size, .      
192af 2a 2a 20 72 6f 75 6e 64 20 75 70 2e 20 49 6e 20  ** round up. In 
192b0 74 68 69 73 20 63 61 73 65 20 74 68 65 20 63 61  this case the ca
192b1 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 54  ll to sqlite3OsT
192b2 72 75 6e 63 61 74 65 28 29 20 62 65 6c 6f 77 20  runcate() below 
192b3 77 69 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 66 69  will.      ** fi
192b4 78 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  x the size of th
192b5 65 20 66 69 6c 65 2e 20 48 6f 77 65 76 65 72 20  e file. However 
192b6 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20  it is important 
192b7 74 6f 20 63 61 6c 6c 0a 20 20 20 20 20 20 2a 2a  to call.      **
192b8 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75   sqlite3PagerTru
192b9 6e 63 61 74 65 49 6d 61 67 65 28 29 20 68 65 72  ncateImage() her
192ba 65 20 73 6f 20 74 68 61 74 20 61 6e 79 20 70 61  e so that any pa
192bb 67 65 73 20 69 6e 20 74 68 65 20 0a 20 20 20 20  ges in the .    
192bc 20 20 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e    ** destination
192bd 20 66 69 6c 65 20 74 68 61 74 20 6c 69 65 20 62   file that lie b
192be 65 79 6f 6e 64 20 74 68 65 20 6e 44 65 73 74 54  eyond the nDestT
192bf 72 75 6e 63 61 74 65 20 70 61 67 65 20 6d 61 72  runcate page mar
192c0 6b 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 6a  k are.      ** j
192c1 6f 75 72 6e 61 6c 6c 65 64 20 62 79 20 50 61 67  ournalled by Pag
192c2 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  erCommitPhaseOne
192c3 28 29 20 62 65 66 6f 72 65 20 74 68 65 79 20 61  () before they a
192c4 72 65 20 64 65 73 74 72 6f 79 65 64 0a 20 20 20  re destroyed.   
192c5 20 20 20 2a 2a 20 62 79 20 74 68 65 20 66 69 6c     ** by the fil
192c6 65 20 74 72 75 6e 63 61 74 69 6f 6e 2e 0a 20 20  e truncation..  
192c7 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
192c8 20 6e 53 72 63 50 61 67 65 73 69 7a 65 3c 6e 44   nSrcPagesize<nD
192c9 65 73 74 50 61 67 65 73 69 7a 65 20 29 7b 0a 20  estPagesize ){. 
192ca 20 20 20 20 20 20 20 69 6e 74 20 72 61 74 69 6f         int ratio
192cb 20 3d 20 6e 44 65 73 74 50 61 67 65 73 69 7a 65   = nDestPagesize
192cc 2f 6e 53 72 63 50 61 67 65 73 69 7a 65 3b 0a 20  /nSrcPagesize;. 
192cd 20 20 20 20 20 20 20 6e 44 65 73 74 54 72 75 6e         nDestTrun
192ce 63 61 74 65 20 3d 20 28 6e 53 72 63 50 61 67 65  cate = (nSrcPage
192cf 2b 72 61 74 69 6f 2d 31 29 2f 72 61 74 69 6f 3b  +ratio-1)/ratio;
192d0 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 44 65  .        if( nDe
192d1 73 74 54 72 75 6e 63 61 74 65 3d 3d 28 69 6e 74  stTruncate==(int
192d2 29 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  )PENDING_BYTE_PA
192d3 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e 70 42 74  GE(p->pDest->pBt
192d4 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  ) ){.          n
192d5 44 65 73 74 54 72 75 6e 63 61 74 65 2d 2d 3b 0a  DestTruncate--;.
192d6 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
192d7 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6e  }else{.        n
192d8 44 65 73 74 54 72 75 6e 63 61 74 65 20 3d 20 6e  DestTruncate = n
192d9 53 72 63 50 61 67 65 20 2a 20 28 6e 53 72 63 50  SrcPage * (nSrcP
192da 61 67 65 73 69 7a 65 2f 6e 44 65 73 74 50 61 67  agesize/nDestPag
192db 65 73 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a  esize);.      }.
192dc 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
192dd 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28  erTruncateImage(
192de 70 44 65 73 74 50 61 67 65 72 2c 20 6e 44 65 73  pDestPager, nDes
192df 74 54 72 75 6e 63 61 74 65 29 3b 0a 0a 20 20 20  tTruncate);..   
192e0 20 20 20 69 66 28 20 6e 53 72 63 50 61 67 65 73     if( nSrcPages
192e1 69 7a 65 3c 6e 44 65 73 74 50 61 67 65 73 69 7a  ize<nDestPagesiz
192e2 65 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  e ){.        /* 
192e3 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 70 61  If the source pa
192e4 67 65 2d 73 69 7a 65 20 69 73 20 73 6d 61 6c 6c  ge-size is small
192e5 65 72 20 74 68 61 6e 20 74 68 65 20 64 65 73 74  er than the dest
192e6 69 6e 61 74 69 6f 6e 20 70 61 67 65 2d 73 69 7a  ination page-siz
192e7 65 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 77  e,.        ** tw
192e8 6f 20 65 78 74 72 61 20 74 68 69 6e 67 73 20 6d  o extra things m
192e9 61 79 20 6e 65 65 64 20 74 6f 20 68 61 70 70 65  ay need to happe
192ea 6e 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20  n:.        **.  
192eb 20 20 20 20 20 20 2a 2a 20 20 20 2a 20 54 68 65        **   * The
192ec 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6d 61 79   destination may
192ed 20 6e 65 65 64 20 74 6f 20 62 65 20 74 72 75 6e   need to be trun
192ee 63 61 74 65 64 2c 20 61 6e 64 0a 20 20 20 20 20  cated, and.     
192ef 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
192f0 20 20 20 2a 20 44 61 74 61 20 73 74 6f 72 65 64     * Data stored
192f1 20 6f 6e 20 74 68 65 20 70 61 67 65 73 20 69 6d   on the pages im
192f2 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77  mediately follow
192f3 69 6e 67 20 74 68 65 20 0a 20 20 20 20 20 20 20  ing the .       
192f4 20 2a 2a 20 20 20 20 20 70 65 6e 64 69 6e 67 2d   **     pending-
192f5 62 79 74 65 20 70 61 67 65 20 69 6e 20 74 68 65  byte page in the
192f6 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65   source database
192f7 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 0a   may need to be.
192f8 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 63          **     c
192f9 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 64  opied into the d
192fa 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
192fb 61 73 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ase..        */.
192fc 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 69 36          const i6
192fd 34 20 69 53 69 7a 65 20 3d 20 28 69 36 34 29 6e  4 iSize = (i64)n
192fe 53 72 63 50 61 67 65 73 69 7a 65 20 2a 20 28 69  SrcPagesize * (i
192ff 36 34 29 6e 53 72 63 50 61 67 65 3b 0a 20 20 20  64)nSrcPage;.   
19300 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c       sqlite3_fil
19301 65 20 2a 20 63 6f 6e 73 74 20 70 46 69 6c 65 20  e * const pFile 
19302 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 69  = sqlite3PagerFi
19303 6c 65 28 70 44 65 73 74 50 61 67 65 72 29 3b 0a  le(pDestPager);.
19304 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
19305 20 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 20 20   pFile );.      
19306 20 20 61 73 73 65 72 74 28 20 28 69 36 34 29 6e    assert( (i64)n
19307 44 65 73 74 54 72 75 6e 63 61 74 65 2a 28 69 36  DestTruncate*(i6
19308 34 29 6e 44 65 73 74 50 61 67 65 73 69 7a 65 20  4)nDestPagesize 
19309 3e 3d 20 69 53 69 7a 65 20 7c 7c 20 28 0a 20 20  >= iSize || (.  
1930a 20 20 20 20 20 20 20 20 20 20 20 20 6e 44 65 73              nDes
1930b 74 54 72 75 6e 63 61 74 65 3d 3d 28 69 6e 74 29  tTruncate==(int)
1930c 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  (PENDING_BYTE_PA
1930d 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e 70 42 74  GE(p->pDest->pBt
1930e 29 2d 31 29 0a 20 20 20 20 20 20 20 20 20 20 20  )-1).           
1930f 26 26 20 69 53 69 7a 65 3e 3d 50 45 4e 44 49 4e  && iSize>=PENDIN
19310 47 5f 42 59 54 45 20 26 26 20 69 53 69 7a 65 3c  G_BYTE && iSize<
19311 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 6e 44  =PENDING_BYTE+nD
19312 65 73 74 50 61 67 65 73 69 7a 65 0a 20 20 20 20  estPagesize.    
19313 20 20 20 20 29 29 3b 0a 20 20 20 20 20 20 20 20      ));.        
19314 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28  if( SQLITE_OK==(
19315 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
19316 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28  rCommitPhaseOne(
19317 70 44 65 73 74 50 61 67 65 72 2c 20 30 2c 20 31  pDestPager, 0, 1
19318 29 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 53  )).         && S
19319 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20  QLITE_OK==(rc = 
1931a 62 61 63 6b 75 70 54 72 75 6e 63 61 74 65 46 69  backupTruncateFi
1931b 6c 65 28 70 46 69 6c 65 2c 20 69 53 69 7a 65 29  le(pFile, iSize)
1931c 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 53 51  ).         && SQ
1931d 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73  LITE_OK==(rc = s
1931e 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28  qlite3PagerSync(
1931f 70 44 65 73 74 50 61 67 65 72 29 29 0a 20 20 20  pDestPager)).   
19320 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
19321 20 20 69 36 34 20 69 4f 66 66 3b 0a 20 20 20 20    i64 iOff;.    
19322 20 20 20 20 20 20 69 36 34 20 69 45 6e 64 20 3d        i64 iEnd =
19323 20 4d 49 4e 28 50 45 4e 44 49 4e 47 5f 42 59 54   MIN(PENDING_BYT
19324 45 20 2b 20 6e 44 65 73 74 50 61 67 65 73 69 7a  E + nDestPagesiz
19325 65 2c 20 69 53 69 7a 65 29 3b 0a 20 20 20 20 20  e, iSize);.     
19326 20 20 20 20 20 66 6f 72 28 0a 20 20 20 20 20 20       for(.      
19327 20 20 20 20 20 20 69 4f 66 66 3d 50 45 4e 44 49        iOff=PENDI
19328 4e 47 5f 42 59 54 45 2b 6e 53 72 63 50 61 67 65  NG_BYTE+nSrcPage
19329 73 69 7a 65 3b 20 0a 20 20 20 20 20 20 20 20 20  size; .         
1932a 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b     rc==SQLITE_OK
1932b 20 26 26 20 69 4f 66 66 3c 69 45 6e 64 3b 20 0a   && iOff<iEnd; .
1932c 20 20 20 20 20 20 20 20 20 20 20 20 69 4f 66 66              iOff
1932d 2b 3d 6e 53 72 63 50 61 67 65 73 69 7a 65 0a 20  +=nSrcPagesize. 
1932e 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20           ){.    
1932f 20 20 20 20 20 20 20 20 50 67 48 64 72 20 2a 70          PgHdr *p
19330 53 72 63 50 67 20 3d 20 30 3b 0a 20 20 20 20 20  SrcPg = 0;.     
19331 20 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e         const Pgn
19332 6f 20 69 53 72 63 50 67 20 3d 20 28 50 67 6e 6f  o iSrcPg = (Pgno
19333 29 28 28 69 4f 66 66 2f 6e 53 72 63 50 61 67 65  )((iOff/nSrcPage
19334 73 69 7a 65 29 2b 31 29 3b 0a 20 20 20 20 20 20  size)+1);.      
19335 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
19336 65 33 50 61 67 65 72 47 65 74 28 70 53 72 63 50  e3PagerGet(pSrcP
19337 61 67 65 72 2c 20 69 53 72 63 50 67 2c 20 26 70  ager, iSrcPg, &p
19338 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20  SrcPg);.        
19339 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1933a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1933b 20 20 20 20 20 20 20 75 38 20 2a 7a 44 61 74 61         u8 *zData
1933c 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47   = sqlite3PagerG
1933d 65 74 44 61 74 61 28 70 53 72 63 50 67 29 3b 0a  etData(pSrcPg);.
1933e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 63                rc
1933f 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
19340 65 28 70 46 69 6c 65 2c 20 7a 44 61 74 61 2c 20  e(pFile, zData, 
19341 6e 53 72 63 50 61 67 65 73 69 7a 65 2c 20 69 4f  nSrcPagesize, iO
19342 66 66 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ff);.           
19343 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73   }.            s
19344 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
19345 28 70 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20  (pSrcPg);.      
19346 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
19347 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
19348 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
19349 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73  3PagerCommitPhas
1934a 65 4f 6e 65 28 70 44 65 73 74 50 61 67 65 72 2c  eOne(pDestPager,
1934b 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a   0, 0);.      }.
1934c 20 20 0a 20 20 20 20 20 20 2f 2a 20 46 69 6e 69    .      /* Fini
1934d 73 68 20 63 6f 6d 6d 69 74 74 69 6e 67 20 74 68  sh committing th
1934e 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f  e transaction to
1934f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
19350 20 64 61 74 61 62 61 73 65 2e 20 2a 2f 0a 20 20   database. */.  
19351 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f      if( SQLITE_O
19352 4b 3d 3d 72 63 0a 20 20 20 20 20 20 20 26 26 20  K==rc.       && 
19353 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d  SQLITE_OK==(rc =
19354 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
19355 6d 69 74 50 68 61 73 65 54 77 6f 28 70 2d 3e 70  mitPhaseTwo(p->p
19356 44 65 73 74 29 29 0a 20 20 20 20 20 20 29 7b 0a  Dest)).      ){.
19357 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
19358 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20  ITE_DONE;.      
19359 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  }.    }.  .    /
1935a 2a 20 49 66 20 62 43 6c 6f 73 65 54 72 61 6e 73  * If bCloseTrans
1935b 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74   is true, then t
1935c 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6f 70 65  his function ope
1935d 6e 65 64 20 61 20 72 65 61 64 20 74 72 61 6e 73  ned a read trans
1935e 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 6f 6e  action.    ** on
1935f 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
19360 62 61 73 65 2e 20 43 6c 6f 73 65 20 74 68 65 20  base. Close the 
19361 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e  read transaction
19362 20 68 65 72 65 2e 20 54 68 65 72 65 20 69 73 0a   here. There is.
19363 20 20 20 20 2a 2a 20 6e 6f 20 6e 65 65 64 20 74      ** no need t
19364 6f 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 75  o check the retu
19365 72 6e 20 76 61 6c 75 65 73 20 6f 66 20 74 68 65  rn values of the
19366 20 62 74 72 65 65 20 6d 65 74 68 6f 64 73 20 68   btree methods h
19367 65 72 65 2c 20 61 73 0a 20 20 20 20 2a 2a 20 22  ere, as.    ** "
19368 63 6f 6d 6d 69 74 74 69 6e 67 22 20 61 20 72 65  committing" a re
19369 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74  ad-only transact
1936a 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e  ion cannot fail.
1936b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1936c 62 43 6c 6f 73 65 54 72 61 6e 73 20 29 7b 0a 20  bCloseTrans ){. 
1936d 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 69       TESTONLY( i
1936e 6e 74 20 72 63 32 20 29 3b 0a 20 20 20 20 20 20  nt rc2 );.      
1936f 54 45 53 54 4f 4e 4c 59 28 20 72 63 32 20 20 3d  TESTONLY( rc2  =
19370 20 29 20 73 71 6c 69 74 65 33 42 74 72 65 65 43   ) sqlite3BtreeC
19371 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 2d  ommitPhaseOne(p-
19372 3e 70 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 20  >pSrc, 0);.     
19373 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 32 20 7c   TESTONLY( rc2 |
19374 3d 20 29 20 73 71 6c 69 74 65 33 42 74 72 65 65  = ) sqlite3Btree
19375 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70  CommitPhaseTwo(p
19376 2d 3e 70 53 72 63 29 3b 0a 20 20 20 20 20 20 61  ->pSrc);.      a
19377 73 73 65 72 74 28 20 72 63 32 3d 3d 53 51 4c 49  ssert( rc2==SQLI
19378 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 7d 0a 20  TE_OK );.    }. 
19379 20 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63   .    p->rc = rc
1937a 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70  ;.  }.  if( p->p
1937b 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 71  DestDb ){.    sq
1937c 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1937d 65 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d 75  e(p->pDestDb->mu
1937e 74 65 78 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  tex);.  }.  sqli
1937f 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 2d  te3BtreeLeave(p-
19380 3e 70 53 72 63 29 3b 0a 20 20 73 71 6c 69 74 65  >pSrc);.  sqlite
19381 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d  3_mutex_leave(p-
19382 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b  >pSrcDb->mutex);
19383 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
19384 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61  ./*.** Release a
19385 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 61 73 73  ll resources ass
19386 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 6e 20  ociated with an 
19387 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 2a 20  sqlite3_backup* 
19388 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  handle..*/.SQLIT
19389 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1938a 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28  3_backup_finish(
1938b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
1938c 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61  p){.  sqlite3_ba
1938d 63 6b 75 70 20 2a 2a 70 70 3b 20 20 20 20 20 20  ckup **pp;      
1938e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 74             /* Pt
1938f 72 20 74 6f 20 68 65 61 64 20 6f 66 20 70 61 67  r to head of pag
19390 65 72 73 20 62 61 63 6b 75 70 20 6c 69 73 74 20  ers backup list 
19391 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  */.  sqlite3_mut
19392 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20  ex *mutex;      
19393 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74            /* Mut
19394 65 78 20 74 6f 20 70 72 6f 74 65 63 74 20 73 6f  ex to protect so
19395 75 72 63 65 20 64 61 74 61 62 61 73 65 20 2a 2f  urce database */
19396 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
19397 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19398 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
19399 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a 20   to return */.. 
1939a 20 2f 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75   /* Enter the mu
1939b 74 65 78 65 73 20 2a 2f 0a 20 20 69 66 28 20 70  texes */.  if( p
1939c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ==0 ) return SQL
1939d 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74 65  ITE_OK;.  sqlite
1939e 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d  3_mutex_enter(p-
1939f 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b  >pSrcDb->mutex);
193a0 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
193a1 6e 74 65 72 28 70 2d 3e 70 53 72 63 29 3b 0a 20  nter(p->pSrc);. 
193a2 20 6d 75 74 65 78 20 3d 20 70 2d 3e 70 53 72 63   mutex = p->pSrc
193a3 44 62 2d 3e 6d 75 74 65 78 3b 0a 20 20 69 66 28  Db->mutex;.  if(
193a4 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20   p->pDestDb ){. 
193a5 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
193a6 5f 65 6e 74 65 72 28 70 2d 3e 70 44 65 73 74 44  _enter(p->pDestD
193a7 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a  b->mutex);.  }..
193a8 20 20 2f 2a 20 44 65 74 61 63 68 20 74 68 69 73    /* Detach this
193a9 20 62 61 63 6b 75 70 20 66 72 6f 6d 20 74 68 65   backup from the
193aa 20 73 6f 75 72 63 65 20 70 61 67 65 72 2e 20 2a   source pager. *
193ab 2f 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74  /.  if( p->pDest
193ac 44 62 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53 72  Db ){.    p->pSr
193ad 63 2d 3e 6e 42 61 63 6b 75 70 2d 2d 3b 0a 20 20  c->nBackup--;.  
193ae 7d 0a 20 20 69 66 28 20 70 2d 3e 69 73 41 74 74  }.  if( p->isAtt
193af 61 63 68 65 64 20 29 7b 0a 20 20 20 20 70 70 20  ached ){.    pp 
193b0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 61  = sqlite3PagerBa
193b1 63 6b 75 70 50 74 72 28 73 71 6c 69 74 65 33 42  ckupPtr(sqlite3B
193b2 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70 53 72  treePager(p->pSr
193b3 63 29 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  c));.    while( 
193b4 2a 70 70 21 3d 70 20 29 7b 0a 20 20 20 20 20 20  *pp!=p ){.      
193b5 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65  pp = &(*pp)->pNe
193b6 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70  xt;.    }.    *p
193b7 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20  p = p->pNext;.  
193b8 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 74 72 61  }..  /* If a tra
193b9 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 69 6c  nsaction is stil
193ba 6c 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 42 74  l open on the Bt
193bb 72 65 65 2c 20 72 6f 6c 6c 20 69 74 20 62 61 63  ree, roll it bac
193bc 6b 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42  k. */.  sqlite3B
193bd 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 70 2d 3e  treeRollback(p->
193be 70 44 65 73 74 29 3b 0a 0a 20 20 2f 2a 20 53 65  pDest);..  /* Se
193bf 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  t the error code
193c0 20 6f 66 20 74 68 65 20 64 65 73 74 69 6e 61 74   of the destinat
193c1 69 6f 6e 20 64 61 74 61 62 61 73 65 20 68 61 6e  ion database han
193c2 64 6c 65 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 28  dle. */.  rc = (
193c3 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  p->rc==SQLITE_DO
193c4 4e 45 29 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20  NE) ? SQLITE_OK 
193c5 3a 20 70 2d 3e 72 63 3b 0a 20 20 73 71 6c 69 74  : p->rc;.  sqlit
193c6 65 33 45 72 72 6f 72 28 70 2d 3e 70 44 65 73 74  e3Error(p->pDest
193c7 44 62 2c 20 72 63 2c 20 30 29 3b 0a 0a 20 20 2f  Db, rc, 0);..  /
193c8 2a 20 45 78 69 74 20 74 68 65 20 6d 75 74 65 78  * Exit the mutex
193c9 65 73 20 61 6e 64 20 66 72 65 65 20 74 68 65 20  es and free the 
193ca 62 61 63 6b 75 70 20 63 6f 6e 74 65 78 74 20 73  backup context s
193cb 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 20 20 69  tructure. */.  i
193cc 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b  f( p->pDestDb ){
193cd 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
193ce 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 44 65 73  ex_leave(p->pDes
193cf 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  tDb->mutex);.  }
193d0 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
193d1 65 61 76 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20  eave(p->pSrc);. 
193d2 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20   if( p->pDestDb 
193d3 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
193d4 72 65 65 28 70 29 3b 0a 20 20 7d 0a 20 20 73 71  ree(p);.  }.  sq
193d5 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
193d6 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  e(mutex);.  retu
193d7 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
193d8 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
193d9 72 20 6f 66 20 70 61 67 65 73 20 73 74 69 6c 6c  r of pages still
193da 20 74 6f 20 62 65 20 62 61 63 6b 65 64 20 75 70   to be backed up
193db 20 61 73 20 6f 66 20 74 68 65 20 6d 6f 73 74 20   as of the most 
193dc 72 65 63 65 6e 74 0a 2a 2a 20 63 61 6c 6c 20 74  recent.** call t
193dd 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  o sqlite3_backup
193de 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49  _step()..*/.SQLI
193df 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
193e0 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e  e3_backup_remain
193e1 69 6e 67 28 73 71 6c 69 74 65 33 5f 62 61 63 6b  ing(sqlite3_back
193e2 75 70 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  up *p){.  return
193e3 20 70 2d 3e 6e 52 65 6d 61 69 6e 69 6e 67 3b 0a   p->nRemaining;.
193e4 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
193e5 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  the total number
193e6 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65   of pages in the
193e7 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65   source database
193e8 20 61 73 20 6f 66 20 74 68 65 20 6d 6f 73 74 20   as of the most 
193e9 0a 2a 2a 20 72 65 63 65 6e 74 20 63 61 6c 6c 20  .** recent call 
193ea 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  to sqlite3_backu
193eb 70 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c  p_step()..*/.SQL
193ec 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
193ed 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63  te3_backup_pagec
193ee 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 62 61 63  ount(sqlite3_bac
193ef 6b 75 70 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  kup *p){.  retur
193f0 6e 20 70 2d 3e 6e 50 61 67 65 63 6f 75 6e 74 3b  n p->nPagecount;
193f1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
193f2 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
193f3 64 20 61 66 74 65 72 20 74 68 65 20 63 6f 6e 74  d after the cont
193f4 65 6e 74 73 20 6f 66 20 70 61 67 65 20 69 50 61  ents of page iPa
193f5 67 65 20 6f 66 20 74 68 65 0a 2a 2a 20 73 6f 75  ge of the.** sou
193f6 72 63 65 20 64 61 74 61 62 61 73 65 20 68 61 76  rce database hav
193f7 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e  e been modified.
193f8 20 49 66 20 70 61 67 65 20 69 50 61 67 65 20 68   If page iPage h
193f9 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
193fa 0a 2a 2a 20 63 6f 70 69 65 64 20 69 6e 74 6f 20  .** copied into 
193fb 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
193fc 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 74  database, then t
193fd 68 65 20 64 61 74 61 20 77 72 69 74 74 65 6e 20  he data written 
193fe 74 6f 20 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e  to the.** destin
193ff 61 74 69 6f 6e 20 69 73 20 6e 6f 77 20 69 6e 76  ation is now inv
19400 61 6c 69 64 61 74 65 64 2e 20 54 68 65 20 64 65  alidated. The de
19401 73 74 69 6e 61 74 69 6f 6e 20 63 6f 70 79 20 6f  stination copy o
19402 66 20 69 50 61 67 65 20 6e 65 65 64 73 0a 2a 2a  f iPage needs.**
19403 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 77   to be updated w
19404 69 74 68 20 74 68 65 20 6e 65 77 20 64 61 74 61  ith the new data
19405 20 62 65 66 6f 72 65 20 74 68 65 20 62 61 63 6b   before the back
19406 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a  up operation is.
19407 2a 2a 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a 2a 0a  ** complete..**.
19408 2a 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65 64  ** It is assumed
19409 20 74 68 61 74 20 74 68 65 20 6d 75 74 65 78 20   that the mutex 
1940a 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
1940b 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a  the BtShared obj
1940c 65 63 74 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e  ect.** correspon
1940d 64 69 6e 67 20 74 6f 20 74 68 65 20 73 6f 75 72  ding to the sour
1940e 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 68  ce database is h
1940f 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75  eld when this fu
19410 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c  nction is.** cal
19411 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  led..*/.SQLITE_P
19412 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
19413 74 65 33 42 61 63 6b 75 70 55 70 64 61 74 65 28  te3BackupUpdate(
19414 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
19415 70 42 61 63 6b 75 70 2c 20 50 67 6e 6f 20 69 50  pBackup, Pgno iP
19416 61 67 65 2c 20 63 6f 6e 73 74 20 75 38 20 2a 61  age, const u8 *a
19417 44 61 74 61 29 7b 0a 20 20 73 71 6c 69 74 65 33  Data){.  sqlite3
19418 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 20 20 20  _backup *p;     
19419 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1941a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62   Iterator variab
1941b 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70 3d 70 42  le */.  for(p=pB
1941c 61 63 6b 75 70 3b 20 70 3b 20 70 3d 70 2d 3e 70  ackup; p; p=p->p
1941d 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 65 72  Next){.    asser
1941e 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1941f 5f 68 65 6c 64 28 70 2d 3e 70 53 72 63 2d 3e 70  _held(p->pSrc->p
19420 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
19421 20 20 69 66 28 20 21 69 73 46 61 74 61 6c 45 72    if( !isFatalEr
19422 72 6f 72 28 70 2d 3e 72 63 29 20 26 26 20 69 50  ror(p->rc) && iP
19423 61 67 65 3c 70 2d 3e 69 4e 65 78 74 20 29 7b 0a  age<p->iNext ){.
19424 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 61 63        /* The bac
19425 6b 75 70 20 70 72 6f 63 65 73 73 20 70 20 68 61  kup process p ha
19426 73 20 61 6c 72 65 61 64 79 20 63 6f 70 69 65 64  s already copied
19427 20 70 61 67 65 20 69 50 61 67 65 2e 20 42 75 74   page iPage. But
19428 20 6e 6f 77 20 69 74 0a 20 20 20 20 20 20 2a 2a   now it.      **
19429 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69   has been modifi
1942a 65 64 20 62 79 20 61 20 74 72 61 6e 73 61 63 74  ed by a transact
1942b 69 6f 6e 20 6f 6e 20 74 68 65 20 73 6f 75 72 63  ion on the sourc
1942c 65 20 70 61 67 65 72 2e 20 43 6f 70 79 0a 20 20  e pager. Copy.  
1942d 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 77 20 64      ** the new d
1942e 61 74 61 20 69 6e 74 6f 20 74 68 65 20 62 61 63  ata into the bac
1942f 6b 75 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  kup..      */.  
19430 20 20 20 20 69 6e 74 20 72 63 20 3d 20 62 61 63      int rc = bac
19431 6b 75 70 4f 6e 65 50 61 67 65 28 70 2c 20 69 50  kupOnePage(p, iP
19432 61 67 65 2c 20 61 44 61 74 61 29 3b 0a 20 20 20  age, aData);.   
19433 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
19434 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 72 63  QLITE_BUSY && rc
19435 21 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20  !=SQLITE_LOCKED 
19436 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
19437 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
19438 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63        p->rc = rc
19439 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1943a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73    }.}../*.** Res
1943b 74 61 72 74 20 74 68 65 20 62 61 63 6b 75 70 20  tart the backup 
1943c 70 72 6f 63 65 73 73 2e 20 54 68 69 73 20 69 73  process. This is
1943d 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65   called when the
1943e 20 70 61 67 65 72 20 6c 61 79 65 72 0a 2a 2a 20   pager layer.** 
1943f 64 65 74 65 63 74 73 20 74 68 61 74 20 74 68 65  detects that the
19440 20 64 61 74 61 62 61 73 65 20 68 61 73 20 62 65   database has be
19441 65 6e 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61  en modified by a
19442 6e 20 65 78 74 65 72 6e 61 6c 20 64 61 74 61 62  n external datab
19443 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f  ase.** connectio
19444 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  n. In this case 
19445 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20  there is no way 
19446 6f 66 20 6b 6e 6f 77 69 6e 67 20 77 68 69 63 68  of knowing which
19447 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 73   of the.** pages
19448 20 74 68 61 74 20 68 61 76 65 20 62 65 65 6e 20   that have been 
19449 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20  copied into the 
1944a 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61  destination data
1944b 62 61 73 65 20 61 72 65 20 73 74 69 6c 6c 20 0a  base are still .
1944c 2a 2a 20 76 61 6c 69 64 20 61 6e 64 20 77 68 69  ** valid and whi
1944d 63 68 20 61 72 65 20 6e 6f 74 2c 20 73 6f 20 74  ch are not, so t
1944e 68 65 20 65 6e 74 69 72 65 20 70 72 6f 63 65 73  he entire proces
1944f 73 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65  s needs to be re
19450 73 74 61 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  started..**.** I
19451 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61  t is assumed tha
19452 74 20 74 68 65 20 6d 75 74 65 78 20 61 73 73 6f  t the mutex asso
19453 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
19454 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 0a  BtShared object.
19455 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ** corresponding
19456 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20 64   to the source d
19457 61 74 61 62 61 73 65 20 69 73 20 68 65 6c 64 20  atabase is held 
19458 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  when this functi
19459 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e  on is.** called.
1945a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1945b 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
1945c 61 63 6b 75 70 52 65 73 74 61 72 74 28 73 71 6c  ackupRestart(sql
1945d 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 42 61  ite3_backup *pBa
1945e 63 6b 75 70 29 7b 0a 20 20 73 71 6c 69 74 65 33  ckup){.  sqlite3
1945f 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 20 20 20  _backup *p;     
19460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19461 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62   Iterator variab
19462 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70 3d 70 42  le */.  for(p=pB
19463 61 63 6b 75 70 3b 20 70 3b 20 70 3d 70 2d 3e 70  ackup; p; p=p->p
19464 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 65 72  Next){.    asser
19465 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
19466 5f 68 65 6c 64 28 70 2d 3e 70 53 72 63 2d 3e 70  _held(p->pSrc->p
19467 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
19468 20 20 70 2d 3e 69 4e 65 78 74 20 3d 20 31 3b 0a    p->iNext = 1;.
19469 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53    }.}..#ifndef S
1946a 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55  QLITE_OMIT_VACUU
1946b 4d 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 74 68 65  M./*.** Copy the
1946c 20 63 6f 6d 70 6c 65 74 65 20 63 6f 6e 74 65 6e   complete conten
1946d 74 20 6f 66 20 70 42 74 46 72 6f 6d 20 69 6e 74  t of pBtFrom int
1946e 6f 20 70 42 74 54 6f 2e 20 20 41 20 74 72 61 6e  o pBtTo.  A tran
1946f 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20  saction.** must 
19470 62 65 20 61 63 74 69 76 65 20 66 6f 72 20 62 6f  be active for bo
19471 74 68 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  th files..**.** 
19472 54 68 65 20 73 69 7a 65 20 6f 66 20 66 69 6c 65  The size of file
19473 20 70 54 6f 20 6d 61 79 20 62 65 20 72 65 64 75   pTo may be redu
19474 63 65 64 20 62 79 20 74 68 69 73 20 6f 70 65 72  ced by this oper
19475 61 74 69 6f 6e 2e 20 49 66 20 61 6e 79 74 68 69  ation. If anythi
19476 6e 67 20 0a 2a 2a 20 67 6f 65 73 20 77 72 6f 6e  ng .** goes wron
19477 67 2c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  g, the transacti
19478 6f 6e 20 6f 6e 20 70 54 6f 20 69 73 20 72 6f 6c  on on pTo is rol
19479 6c 65 64 20 62 61 63 6b 2e 20 49 66 20 73 75 63  led back. If suc
1947a 63 65 73 73 66 75 6c 2c 20 74 68 65 20 0a 2a 2a  cessful, the .**
1947b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
1947c 63 6f 6d 6d 69 74 74 65 64 20 62 65 66 6f 72 65  committed before
1947d 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53   returning..*/.S
1947e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1947f 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  t sqlite3BtreeCo
19480 70 79 46 69 6c 65 28 42 74 72 65 65 20 2a 70 54  pyFile(Btree *pT
19481 6f 2c 20 42 74 72 65 65 20 2a 70 46 72 6f 6d 29  o, Btree *pFrom)
19482 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71  {.  int rc;.  sq
19483 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 62 3b 0a  lite3_backup b;.
19484 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
19485 74 65 72 28 70 54 6f 29 3b 0a 20 20 73 71 6c 69  ter(pTo);.  sqli
19486 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 46  te3BtreeEnter(pF
19487 72 6f 6d 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20  rom);..  /* Set 
19488 75 70 20 61 6e 20 73 71 6c 69 74 65 33 5f 62 61  up an sqlite3_ba
19489 63 6b 75 70 20 6f 62 6a 65 63 74 2e 20 73 71 6c  ckup object. sql
1948a 69 74 65 33 5f 62 61 63 6b 75 70 2e 70 44 65 73  ite3_backup.pDes
1948b 74 44 62 20 6d 75 73 74 20 62 65 20 73 65 74 0a  tDb must be set.
1948c 20 20 2a 2a 20 74 6f 20 30 2e 20 54 68 69 73 20    ** to 0. This 
1948d 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 69  is used by the i
1948e 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f  mplementations o
1948f 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  f sqlite3_backup
19490 5f 73 74 65 70 28 29 0a 20 20 2a 2a 20 61 6e 64  _step().  ** and
19491 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
19492 66 69 6e 69 73 68 28 29 20 74 6f 20 64 65 74 65  finish() to dete
19493 63 74 20 74 68 61 74 20 74 68 65 79 20 61 72 65  ct that they are
19494 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 0a 20 20   being called.  
19495 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20 66 75 6e  ** from this fun
19496 63 74 69 6f 6e 2c 20 6e 6f 74 20 64 69 72 65 63  ction, not direc
19497 74 6c 79 20 62 79 20 74 68 65 20 75 73 65 72 2e  tly by the user.
19498 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26  .  */.  memset(&
19499 62 2c 20 30 2c 20 73 69 7a 65 6f 66 28 62 29 29  b, 0, sizeof(b))
1949a 3b 0a 20 20 62 2e 70 53 72 63 44 62 20 3d 20 70  ;.  b.pSrcDb = p
1949b 46 72 6f 6d 2d 3e 64 62 3b 0a 20 20 62 2e 70 53  From->db;.  b.pS
1949c 72 63 20 3d 20 70 46 72 6f 6d 3b 0a 20 20 62 2e  rc = pFrom;.  b.
1949d 70 44 65 73 74 20 3d 20 70 54 6f 3b 0a 20 20 62  pDest = pTo;.  b
1949e 2e 69 4e 65 78 74 20 3d 20 31 3b 0a 0a 20 20 2f  .iNext = 1;..  /
1949f 2a 20 30 78 37 46 46 46 46 46 46 46 20 69 73 20  * 0x7FFFFFFF is 
194a0 74 68 65 20 68 61 72 64 20 6c 69 6d 69 74 20 66  the hard limit f
194a1 6f 72 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  or the number of
194a2 20 70 61 67 65 73 20 69 6e 20 61 20 64 61 74 61   pages in a data
194a3 62 61 73 65 0a 20 20 2a 2a 20 66 69 6c 65 2e 20  base.  ** file. 
194a4 42 79 20 70 61 73 73 69 6e 67 20 74 68 69 73 20  By passing this 
194a5 61 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  as the number of
194a6 20 70 61 67 65 73 20 74 6f 20 63 6f 70 79 20 74   pages to copy t
194a7 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 62  o.  ** sqlite3_b
194a8 61 63 6b 75 70 5f 73 74 65 70 28 29 2c 20 77 65  ackup_step(), we
194a9 20 63 61 6e 20 67 75 61 72 61 6e 74 65 65 20 74   can guarantee t
194aa 68 61 74 20 74 68 65 20 63 6f 70 79 20 66 69 6e  hat the copy fin
194ab 69 73 68 65 73 20 0a 20 20 2a 2a 20 77 69 74 68  ishes .  ** with
194ac 69 6e 20 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c  in a single call
194ad 20 28 75 6e 6c 65 73 73 20 61 6e 20 65 72 72 6f   (unless an erro
194ae 72 20 6f 63 63 75 72 73 29 2e 20 54 68 65 20 61  r occurs). The a
194af 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e  ssert() statemen
194b0 74 0a 20 20 2a 2a 20 63 68 65 63 6b 73 20 74 68  t.  ** checks th
194b1 69 73 20 61 73 73 75 6d 70 74 69 6f 6e 20 2d 20  is assumption - 
194b2 28 70 2d 3e 72 63 29 20 73 68 6f 75 6c 64 20 62  (p->rc) should b
194b3 65 20 73 65 74 20 74 6f 20 65 69 74 68 65 72 20  e set to either 
194b4 53 51 4c 49 54 45 5f 44 4f 4e 45 20 0a 20 20 2a  SQLITE_DONE .  *
194b5 2a 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f  * or an error co
194b6 64 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  de..  */.  sqlit
194b7 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 26  e3_backup_step(&
194b8 62 2c 20 30 78 37 46 46 46 46 46 46 46 29 3b 0a  b, 0x7FFFFFFF);.
194b9 20 20 61 73 73 65 72 74 28 20 62 2e 72 63 21 3d    assert( b.rc!=
194ba 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 72  SQLITE_OK );.  r
194bb 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  c = sqlite3_back
194bc 75 70 5f 66 69 6e 69 73 68 28 26 62 29 3b 0a 20  up_finish(&b);. 
194bd 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
194be 4f 4b 20 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 70  OK ){.    pTo->p
194bf 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65  Bt->pageSizeFixe
194c0 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 73 71  d = 0;.  }..  sq
194c1 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
194c2 70 46 72 6f 6d 29 3b 0a 20 20 73 71 6c 69 74 65  pFrom);.  sqlite
194c3 33 42 74 72 65 65 4c 65 61 76 65 28 70 54 6f 29  3BtreeLeave(pTo)
194c4 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
194c5 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
194c6 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20 2a 2f  E_OMIT_VACUUM */
194c7 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
194c8 2a 20 45 6e 64 20 6f 66 20 62 61 63 6b 75 70 2e  * End of backup.
194c9 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
194ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
194cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
194cc 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
194cd 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62  * Begin file vdb
194ce 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  emem.c *********
194cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
194d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
194d1 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79  /./*.** 2004 May
194d2 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   26.**.** The au
194d3 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
194d4 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
194d5 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
194d6 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
194d7 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
194d8 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
194d9 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
194da 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
194db 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
194dc 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
194dd 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
194de 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
194df 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
194e0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
194e1 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
194e2 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
194e3 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
194e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
194e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
194e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
194e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
194e8 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
194e9 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
194ea 20 75 73 65 20 74 6f 20 6d 61 6e 69 70 75 6c 61   use to manipula
194eb 74 65 20 22 4d 65 6d 22 20 73 74 72 75 63 74 75  te "Mem" structu
194ec 72 65 2e 20 20 41 20 22 4d 65 6d 22 0a 2a 2a 20  re.  A "Mem".** 
194ed 73 74 6f 72 65 73 20 61 20 73 69 6e 67 6c 65 20  stores a single 
194ee 76 61 6c 75 65 20 69 6e 20 74 68 65 20 56 44 42  value in the VDB
194ef 45 2e 20 20 4d 65 6d 20 69 73 20 61 6e 20 6f 70  E.  Mem is an op
194f0 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20 76  aque structure v
194f1 69 73 69 62 6c 65 0a 2a 2a 20 6f 6e 6c 79 20 77  isible.** only w
194f2 69 74 68 69 6e 20 74 68 65 20 56 44 42 45 2e 20  ithin the VDBE. 
194f3 20 49 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69   Interface routi
194f4 6e 65 73 20 72 65 66 65 72 20 74 6f 20 61 20 4d  nes refer to a M
194f5 65 6d 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  em using the.** 
194f6 6e 61 6d 65 20 73 71 6c 69 74 65 5f 76 61 6c 75  name sqlite_valu
194f7 65 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62  e.**.** $Id: vdb
194f8 65 6d 65 6d 2e 63 2c 76 20 31 2e 31 35 32 20 32  emem.c,v 1.152 2
194f9 30 30 39 2f 30 37 2f 32 32 20 31 38 3a 30 37 3a  009/07/22 18:07:
194fa 34 31 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  41 drh Exp $.*/.
194fb 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69  ./*.** Call sqli
194fc 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64  te3VdbeMemExpand
194fd 42 6c 6f 62 28 29 20 6f 6e 20 74 68 65 20 73 75  Blob() on the su
194fe 70 70 6c 69 65 64 20 76 61 6c 75 65 20 28 74 79  pplied value (ty
194ff 70 65 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66  pe Mem*).** P if
19500 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64   required..*/.#d
19501 65 66 69 6e 65 20 65 78 70 61 6e 64 42 6c 6f 62  efine expandBlob
19502 28 50 29 20 28 28 28 50 29 2d 3e 66 6c 61 67 73  (P) (((P)->flags
19503 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74  &MEM_Zero)?sqlit
19504 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42  e3VdbeMemExpandB
19505 6c 6f 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a  lob(P):0)../*.**
19506 20 49 66 20 70 4d 65 6d 20 69 73 20 61 6e 20 6f   If pMem is an o
19507 62 6a 65 63 74 20 77 69 74 68 20 61 20 76 61 6c  bject with a val
19508 69 64 20 73 74 72 69 6e 67 20 72 65 70 72 65 73  id string repres
19509 65 6e 74 61 74 69 6f 6e 2c 20 74 68 69 73 20 72  entation, this r
1950a 6f 75 74 69 6e 65 0a 2a 2a 20 65 6e 73 75 72 65  outine.** ensure
1950b 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 65  s the internal e
1950c 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20  ncoding for the 
1950d 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74  string represent
1950e 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 27 64 65 73  ation is.** 'des
1950f 69 72 65 64 45 6e 63 27 2c 20 6f 6e 65 20 6f 66  iredEnc', one of
19510 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51   SQLITE_UTF8, SQ
19511 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 20  LITE_UTF16LE or 
19512 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2e 0a  SQLITE_UTF16BE..
19513 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73  **.** If pMem is
19514 20 6e 6f 74 20 61 20 73 74 72 69 6e 67 20 6f 62   not a string ob
19515 6a 65 63 74 2c 20 6f 72 20 74 68 65 20 65 6e 63  ject, or the enc
19516 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 73 74 72  oding of the str
19517 69 6e 67 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74  ing.** represent
19518 61 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 79  ation is already
19519 20 73 74 6f 72 65 64 20 75 73 69 6e 67 20 74 68   stored using th
1951a 65 20 72 65 71 75 65 73 74 65 64 20 65 6e 63 6f  e requested enco
1951b 64 69 6e 67 2c 20 74 68 65 6e 20 74 68 69 73 0a  ding, then this.
1951c 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  ** routine is a 
1951d 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  no-op..**.** SQL
1951e 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
1951f 65 64 20 69 66 20 74 68 65 20 63 6f 6e 76 65 72  ed if the conver
19520 73 69 6f 6e 20 69 73 20 73 75 63 63 65 73 73 66  sion is successf
19521 75 6c 20 28 6f 72 20 6e 6f 74 20 72 65 71 75 69  ul (or not requi
19522 72 65 64 29 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f  red)..** SQLITE_
19523 4e 4f 4d 45 4d 20 6d 61 79 20 62 65 20 72 65 74  NOMEM may be ret
19524 75 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f  urned if a mallo
19525 63 28 29 20 66 61 69 6c 73 20 64 75 72 69 6e 67  c() fails during
19526 20 63 6f 6e 76 65 72 73 69 6f 6e 0a 2a 2a 20 62   conversion.** b
19527 65 74 77 65 65 6e 20 66 6f 72 6d 61 74 73 2e 0a  etween formats..
19528 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19529 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
1952a 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28  eChangeEncoding(
1952b 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 64  Mem *pMem, int d
1952c 65 73 69 72 65 64 45 6e 63 29 7b 0a 20 20 69 6e  esiredEnc){.  in
1952d 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  t rc;.  assert( 
1952e 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d  (pMem->flags&MEM
1952f 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20  _RowSet)==0 );. 
19530 20 61 73 73 65 72 74 28 20 64 65 73 69 72 65 64   assert( desired
19531 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38  Enc==SQLITE_UTF8
19532 20 7c 7c 20 64 65 73 69 72 65 64 45 6e 63 3d 3d   || desiredEnc==
19533 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 0a 20  SQLITE_UTF16LE. 
19534 20 20 20 20 20 20 20 20 20 20 7c 7c 20 64 65 73            || des
19535 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f  iredEnc==SQLITE_
19536 55 54 46 31 36 42 45 20 29 3b 0a 20 20 69 66 28  UTF16BE );.  if(
19537 20 21 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d   !(pMem->flags&M
19538 45 4d 5f 53 74 72 29 20 7c 7c 20 70 4d 65 6d 2d  EM_Str) || pMem-
19539 3e 65 6e 63 3d 3d 64 65 73 69 72 65 64 45 6e 63  >enc==desiredEnc
1953a 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
1953b 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
1953c 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62  assert( pMem->db
1953d 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  ==0 || sqlite3_m
1953e 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e  utex_held(pMem->
1953f 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 23 69  db->mutex) );.#i
19540 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
19541 5f 55 54 46 31 36 0a 20 20 72 65 74 75 72 6e 20  _UTF16.  return 
19542 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 23 65  SQLITE_ERROR;.#e
19543 6c 73 65 0a 0a 20 20 2f 2a 20 4d 65 6d 54 72 61  lse..  /* MemTra
19544 6e 73 6c 61 74 65 28 29 20 6d 61 79 20 72 65 74  nslate() may ret
19545 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72  urn SQLITE_OK or
19546 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49   SQLITE_NOMEM. I
19547 66 20 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72  f NOMEM is retur
19548 6e 65 64 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74  ned,.  ** then t
19549 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74  he encoding of t
1954a 68 65 20 76 61 6c 75 65 20 6d 61 79 20 6e 6f 74  he value may not
1954b 20 68 61 76 65 20 63 68 61 6e 67 65 64 2e 0a 20   have changed.. 
1954c 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   */.  rc = sqlit
1954d 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61  e3VdbeMemTransla
1954e 74 65 28 70 4d 65 6d 2c 20 28 75 38 29 64 65 73  te(pMem, (u8)des
1954f 69 72 65 64 45 6e 63 29 3b 0a 20 20 61 73 73 65  iredEnc);.  asse
19550 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  rt(rc==SQLITE_OK
19551 20 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54      || rc==SQLIT
19552 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 61 73 73 65  E_NOMEM);.  asse
19553 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  rt(rc==SQLITE_OK
19554 20 20 20 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63      || pMem->enc
19555 21 3d 64 65 73 69 72 65 64 45 6e 63 29 3b 0a 20  !=desiredEnc);. 
19556 20 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49   assert(rc==SQLI
19557 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 70 4d 65 6d  TE_NOMEM || pMem
19558 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 65 64 45 6e  ->enc==desiredEn
19559 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  c);.  return rc;
1955a 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
1955b 20 4d 61 6b 65 20 73 75 72 65 20 70 4d 65 6d 2d   Make sure pMem-
1955c 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 77  >z points to a w
1955d 72 69 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74 69  ritable allocati
1955e 6f 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 0a  on of at least .
1955f 2a 2a 20 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a  ** n bytes..**.*
19560 2a 20 49 66 20 74 68 65 20 6d 65 6d 6f 72 79 20  * If the memory 
19561 63 65 6c 6c 20 63 75 72 72 65 6e 74 6c 79 20 63  cell currently c
19562 6f 6e 74 61 69 6e 73 20 73 74 72 69 6e 67 20 6f  ontains string o
19563 72 20 62 6c 6f 62 20 64 61 74 61 0a 2a 2a 20 61  r blob data.** a
19564 6e 64 20 74 68 65 20 74 68 69 72 64 20 61 72 67  nd the third arg
19565 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20  ument passed to 
19566 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
19567 20 74 72 75 65 2c 20 74 68 65 20 0a 2a 2a 20 63   true, the .** c
19568 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 20 6f  urrent content o
19569 66 20 74 68 65 20 63 65 6c 6c 20 69 73 20 70 72  f the cell is pr
1956a 65 73 65 72 76 65 64 2e 20 4f 74 68 65 72 77 69  eserved. Otherwi
1956b 73 65 2c 20 69 74 20 6d 61 79 0a 2a 2a 20 62 65  se, it may.** be
1956c 20 64 69 73 63 61 72 64 65 64 2e 20 20 0a 2a 2a   discarded.  .**
1956d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
1956e 6e 20 73 65 74 73 20 74 68 65 20 4d 45 4d 5f 44  n sets the MEM_D
1956f 79 6e 20 66 6c 61 67 20 61 6e 64 20 63 6c 65 61  yn flag and clea
19570 72 73 20 61 6e 79 20 78 44 65 6c 20 63 61 6c 6c  rs any xDel call
19571 62 61 63 6b 2e 0a 2a 2a 20 49 74 20 61 6c 73 6f  back..** It also
19572 20 63 6c 65 61 72 73 20 4d 45 4d 5f 45 70 68 65   clears MEM_Ephe
19573 6d 20 61 6e 64 20 4d 45 4d 5f 53 74 61 74 69 63  m and MEM_Static
19574 2e 20 49 66 20 74 68 65 20 70 72 65 73 65 72 76  . If the preserv
19575 65 20 66 6c 61 67 20 69 73 20 0a 2a 2a 20 6e 6f  e flag is .** no
19576 74 20 73 65 74 2c 20 4d 65 6d 2e 6e 20 69 73 20  t set, Mem.n is 
19577 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  zeroed..*/.SQLIT
19578 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
19579 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77  lite3VdbeMemGrow
1957a 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20  (Mem *pMem, int 
1957b 6e 2c 20 69 6e 74 20 70 72 65 73 65 72 76 65 29  n, int preserve)
1957c 7b 0a 20 20 61 73 73 65 72 74 28 20 31 20 3e 3d  {.  assert( 1 >=
1957d 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e 7a 4d 61  .    ((pMem->zMa
1957e 6c 6c 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d  lloc && pMem->zM
1957f 61 6c 6c 6f 63 3d 3d 70 4d 65 6d 2d 3e 7a 29 20  alloc==pMem->z) 
19580 3f 20 31 20 3a 20 30 29 20 2b 0a 20 20 20 20 28  ? 1 : 0) +.    (
19581 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45  ((pMem->flags&ME
19582 4d 5f 44 79 6e 29 26 26 70 4d 65 6d 2d 3e 78 44  M_Dyn)&&pMem->xD
19583 65 6c 29 20 3f 20 31 20 3a 20 30 29 20 2b 20 0a  el) ? 1 : 0) + .
19584 20 20 20 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67      ((pMem->flag
19585 73 26 4d 45 4d 5f 45 70 68 65 6d 29 20 3f 20 31  s&MEM_Ephem) ? 1
19586 20 3a 20 30 29 20 2b 20 0a 20 20 20 20 28 28 70   : 0) + .    ((p
19587 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53  Mem->flags&MEM_S
19588 74 61 74 69 63 29 20 3f 20 31 20 3a 20 30 29 0a  tatic) ? 1 : 0).
19589 20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28    );.  assert( (
1958a 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pMem->flags&MEM_
1958b 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 0a 20  RowSet)==0 );.. 
1958c 20 69 66 28 20 6e 3c 33 32 20 29 20 6e 20 3d 20   if( n<32 ) n = 
1958d 33 32 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  32;.  if( sqlite
1958e 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4d  3DbMallocSize(pM
1958f 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d  em->db, pMem->zM
19590 61 6c 6c 6f 63 29 3c 6e 20 29 7b 0a 20 20 20 20  alloc)<n ){.    
19591 69 66 28 20 70 72 65 73 65 72 76 65 20 26 26 20  if( preserve && 
19592 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e 7a  pMem->z==pMem->z
19593 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20  Malloc ){.      
19594 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e  pMem->z = pMem->
19595 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65  zMalloc = sqlite
19596 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65  3DbReallocOrFree
19597 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d  (pMem->db, pMem-
19598 3e 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 70 72  >z, n);.      pr
19599 65 73 65 72 76 65 20 3d 20 30 3b 0a 20 20 20 20  eserve = 0;.    
1959a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
1959b 69 74 65 33 44 62 46 72 65 65 28 70 4d 65 6d 2d  ite3DbFree(pMem-
1959c 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c  >db, pMem->zMall
1959d 6f 63 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  oc);.      pMem-
1959e 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74  >zMalloc = sqlit
1959f 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d  e3DbMallocRaw(pM
195a0 65 6d 2d 3e 64 62 2c 20 6e 29 3b 0a 20 20 20 20  em->db, n);.    
195a1 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4d 65  }.  }..  if( pMe
195a2 6d 2d 3e 7a 20 26 26 20 70 72 65 73 65 72 76 65  m->z && preserve
195a3 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f   && pMem->zMallo
195a4 63 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d  c && pMem->z!=pM
195a5 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20  em->zMalloc ){. 
195a6 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2d 3e     memcpy(pMem->
195a7 7a 4d 61 6c 6c 6f 63 2c 20 70 4d 65 6d 2d 3e 7a  zMalloc, pMem->z
195a8 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 7d 0a  , pMem->n);.  }.
195a9 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67    if( pMem->flag
195aa 73 26 4d 45 4d 5f 44 79 6e 20 26 26 20 70 4d 65  s&MEM_Dyn && pMe
195ab 6d 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 20 20 70  m->xDel ){.    p
195ac 4d 65 6d 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20  Mem->xDel((void 
195ad 2a 29 28 70 4d 65 6d 2d 3e 7a 29 29 3b 0a 20 20  *)(pMem->z));.  
195ae 7d 0a 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70  }..  pMem->z = p
195af 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20  Mem->zMalloc;.  
195b0 69 66 28 20 70 4d 65 6d 2d 3e 7a 3d 3d 30 20 29  if( pMem->z==0 )
195b1 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  {.    pMem->flag
195b2 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
195b3 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d  }else{.    pMem-
195b4 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f  >flags &= ~(MEM_
195b5 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63  Ephem|MEM_Static
195b6 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 78  );.  }.  pMem->x
195b7 44 65 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75 72  Del = 0;.  retur
195b8 6e 20 28 70 4d 65 6d 2d 3e 7a 20 3f 20 53 51 4c  n (pMem->z ? SQL
195b9 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f  ITE_OK : SQLITE_
195ba 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  NOMEM);.}../*.**
195bb 20 4d 61 6b 65 20 74 68 65 20 67 69 76 65 6e 20   Make the given 
195bc 4d 65 6d 20 6f 62 6a 65 63 74 20 4d 45 4d 5f 44  Mem object MEM_D
195bd 79 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f  yn.  In other wo
195be 72 64 73 2c 20 6d 61 6b 65 20 69 74 20 73 6f 0a  rds, make it so.
195bf 2a 2a 20 74 68 61 74 20 61 6e 79 20 54 45 58 54  ** that any TEXT
195c0 20 6f 72 20 42 4c 4f 42 20 63 6f 6e 74 65 6e 74   or BLOB content
195c1 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 65   is stored in me
195c2 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
195c3 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 2e 20  om.** malloc(). 
195c4 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 77 65   In this way, we
195c5 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 6d   know that the m
195c6 65 6d 6f 72 79 20 69 73 20 73 61 66 65 20 74 6f  emory is safe to
195c7 20 62 65 0a 2a 2a 20 6f 76 65 72 77 72 69 74 74   be.** overwritt
195c8 65 6e 20 6f 72 20 61 6c 74 65 72 65 64 2e 0a 2a  en or altered..*
195c9 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49  *.** Return SQLI
195ca 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73  TE_OK on success
195cb 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d   or SQLITE_NOMEM
195cc 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73   if malloc fails
195cd 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
195ce 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
195cf 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61  dbeMemMakeWritea
195d0 62 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a  ble(Mem *pMem){.
195d1 20 20 69 6e 74 20 66 3b 0a 20 20 61 73 73 65 72    int f;.  asser
195d2 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c  t( pMem->db==0 |
195d3 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
195d4 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d  held(pMem->db->m
195d5 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
195d6 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26  t( (pMem->flags&
195d7 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29  MEM_RowSet)==0 )
195d8 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f 62 28 70  ;.  expandBlob(p
195d9 4d 65 6d 29 3b 0a 20 20 66 20 3d 20 70 4d 65 6d  Mem);.  f = pMem
195da 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 20 28  ->flags;.  if( (
195db 66 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42  f&(MEM_Str|MEM_B
195dc 6c 6f 62 29 29 20 26 26 20 70 4d 65 6d 2d 3e 7a  lob)) && pMem->z
195dd 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20  !=pMem->zMalloc 
195de 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
195df 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d  e3VdbeMemGrow(pM
195e0 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 20 2b 20 32 2c  em, pMem->n + 2,
195e1 20 31 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74   1) ){.      ret
195e2 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
195e3 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d  ;.    }.    pMem
195e4 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 30  ->z[pMem->n] = 0
195e5 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d  ;.    pMem->z[pM
195e6 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20  em->n+1] = 0;.  
195e7 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d    pMem->flags |=
195e8 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 7d 0a 0a   MEM_Term;.  }..
195e9 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
195ea 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  OK;.}../*.** If 
195eb 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 2a 20 68  the given Mem* h
195ec 61 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64  as a zero-filled
195ed 20 74 61 69 6c 2c 20 74 75 72 6e 20 69 74 20 69   tail, turn it i
195ee 6e 74 6f 20 61 6e 20 6f 72 64 69 6e 61 72 79 0a  nto an ordinary.
195ef 2a 2a 20 62 6c 6f 62 20 73 74 6f 72 65 64 20 69  ** blob stored i
195f0 6e 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  n dynamically al
195f1 6c 6f 63 61 74 65 64 20 73 70 61 63 65 2e 0a 2a  located space..*
195f2 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
195f3 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 53  _OMIT_INCRBLOB.S
195f4 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
195f5 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
195f6 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a  ExpandBlob(Mem *
195f7 70 4d 65 6d 29 7b 0a 20 20 69 66 28 20 70 4d 65  pMem){.  if( pMe
195f8 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a  m->flags & MEM_Z
195f9 65 72 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e  ero ){.    int n
195fa 42 79 74 65 3b 0a 20 20 20 20 61 73 73 65 72 74  Byte;.    assert
195fb 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45  ( pMem->flags&ME
195fc 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 61 73  M_Blob );.    as
195fd 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61  sert( (pMem->fla
195fe 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d  gs&MEM_RowSet)==
195ff 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
19600 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20   pMem->db==0 || 
19601 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
19602 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74  ld(pMem->db->mut
19603 65 78 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 53  ex) );..    /* S
19604 65 74 20 6e 42 79 74 65 20 74 6f 20 74 68 65 20  et nByte to the 
19605 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
19606 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72  required to stor
19607 65 20 74 68 65 20 65 78 70 61 6e 64 65 64 20 62  e the expanded b
19608 6c 6f 62 2e 20 2a 2f 0a 20 20 20 20 6e 42 79 74  lob. */.    nByt
19609 65 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2b 20 70 4d  e = pMem->n + pM
1960a 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20  em->u.nZero;.   
1960b 20 69 66 28 20 6e 42 79 74 65 3c 3d 30 20 29 7b   if( nByte<=0 ){
1960c 0a 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20 31  .      nByte = 1
1960d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1960e 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
1960f 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20  ow(pMem, nByte, 
19610 31 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  1) ){.      retu
19611 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
19612 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d 65 6d 73  .    }..    mems
19613 65 74 28 26 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d  et(&pMem->z[pMem
19614 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65 6d 2d 3e 75  ->n], 0, pMem->u
19615 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 70 4d 65  .nZero);.    pMe
19616 6d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e  m->n += pMem->u.
19617 6e 5a 65 72 6f 3b 0a 20 20 20 20 70 4d 65 6d 2d  nZero;.    pMem-
19618 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f  >flags &= ~(MEM_
19619 5a 65 72 6f 7c 4d 45 4d 5f 54 65 72 6d 29 3b 0a  Zero|MEM_Term);.
1961a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
1961b 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66  ITE_OK;.}.#endif
1961c 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75  .../*.** Make su
1961d 72 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d  re the given Mem
1961e 20 69 73 20 5c 75 30 30 30 30 20 74 65 72 6d 69   is \u0000 termi
1961f 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  nated..*/.SQLITE
19620 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
19621 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65  ite3VdbeMemNulTe
19622 72 6d 69 6e 61 74 65 28 4d 65 6d 20 2a 70 4d 65  rminate(Mem *pMe
19623 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d  m){.  assert( pM
19624 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
19625 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
19626 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
19627 20 29 3b 0a 20 20 69 66 28 20 28 70 4d 65 6d 2d   );.  if( (pMem-
19628 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 65 72  >flags & MEM_Ter
19629 6d 29 21 3d 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e  m)!=0 || (pMem->
1962a 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 29  flags & MEM_Str)
1962b 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
1962c 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f  n SQLITE_OK;   /
1962d 2a 20 4e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20  * Nothing to do 
1962e 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c  */.  }.  if( sql
1962f 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
19630 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 2b 32 2c  pMem, pMem->n+2,
19631 20 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72   1) ){.    retur
19632 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
19633 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d    }.  pMem->z[pM
19634 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 70 4d  em->n] = 0;.  pM
19635 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d  em->z[pMem->n+1]
19636 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c   = 0;.  pMem->fl
19637 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b  ags |= MEM_Term;
19638 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
19639 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  _OK;.}../*.** Ad
1963a 64 20 4d 45 4d 5f 53 74 72 20 74 6f 20 74 68 65  d MEM_Str to the
1963b 20 73 65 74 20 6f 66 20 72 65 70 72 65 73 65 6e   set of represen
1963c 74 61 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20  tations for the 
1963d 67 69 76 65 6e 20 4d 65 6d 2e 20 20 4e 75 6d 62  given Mem.  Numb
1963e 65 72 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65  ers.** are conve
1963f 72 74 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74  rted using sqlit
19640 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 2e 20 20  e3_snprintf().  
19641 43 6f 6e 76 65 72 74 69 6e 67 20 61 20 42 4c 4f  Converting a BLO
19642 42 20 74 6f 20 61 20 73 74 72 69 6e 67 0a 2a 2a  B to a string.**
19643 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a   is a no-op..**.
19644 2a 2a 20 45 78 69 73 74 69 6e 67 20 72 65 70 72  ** Existing repr
19645 65 73 65 6e 74 61 74 69 6f 6e 73 20 4d 45 4d 5f  esentations MEM_
19646 49 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c  Int and MEM_Real
19647 20 61 72 65 20 2a 6e 6f 74 2a 20 69 6e 76 61 6c   are *not* inval
19648 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20  idated..**.** A 
19649 4d 45 4d 5f 4e 75 6c 6c 20 76 61 6c 75 65 20 77  MEM_Null value w
1964a 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 70 61 73  ill never be pas
1964b 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63  sed to this func
1964c 74 69 6f 6e 2e 20 54 68 69 73 20 66 75 6e 63 74  tion. This funct
1964d 69 6f 6e 20 69 73 0a 2a 2a 20 75 73 65 64 20 66  ion is.** used f
1964e 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67 20 76 61  or converting va
1964f 6c 75 65 73 20 74 6f 20 74 65 78 74 20 66 6f 72  lues to text for
19650 20 72 65 74 75 72 6e 69 6e 67 20 74 6f 20 74 68   returning to th
19651 65 20 75 73 65 72 20 28 69 2e 65 2e 20 76 69 61  e user (i.e. via
19652 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  .** sqlite3_valu
19653 65 5f 74 65 78 74 28 29 29 2c 20 6f 72 20 66 6f  e_text()), or fo
19654 72 20 65 6e 73 75 72 69 6e 67 20 74 68 61 74 20  r ensuring that 
19655 76 61 6c 75 65 73 20 74 6f 20 62 65 20 75 73 65  values to be use
19656 64 20 61 73 20 62 74 72 65 65 0a 2a 2a 20 6b 65  d as btree.** ke
19657 79 73 20 61 72 65 20 73 74 72 69 6e 67 73 2e 20  ys are strings. 
19658 49 6e 20 74 68 65 20 66 6f 72 6d 65 72 20 63 61  In the former ca
19659 73 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  se a NULL pointe
1965a 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 68  r is returned th
1965b 65 0a 2a 2a 20 75 73 65 72 20 61 6e 64 20 74 68  e.** user and th
1965c 65 20 6c 61 74 65 72 20 69 73 20 61 6e 20 69 6e  e later is an in
1965d 74 65 72 6e 61 6c 20 70 72 6f 67 72 61 6d 6d 69  ternal programmi
1965e 6e 67 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c  ng error..*/.SQL
1965f 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
19660 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74  sqlite3VdbeMemSt
19661 72 69 6e 67 69 66 79 28 4d 65 6d 20 2a 70 4d 65  ringify(Mem *pMe
19662 6d 2c 20 69 6e 74 20 65 6e 63 29 7b 0a 20 20 69  m, int enc){.  i
19663 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
19664 4b 3b 0a 20 20 69 6e 74 20 66 67 20 3d 20 70 4d  K;.  int fg = pM
19665 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 6f 6e  em->flags;.  con
19666 73 74 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 33  st int nByte = 3
19667 32 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d  2;..  assert( pM
19668 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
19669 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1966a 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
1966b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 28   );.  assert( !(
1966c 66 67 26 4d 45 4d 5f 5a 65 72 6f 29 20 29 3b 0a  fg&MEM_Zero) );.
1966d 20 20 61 73 73 65 72 74 28 20 21 28 66 67 26 28    assert( !(fg&(
1966e 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62  MEM_Str|MEM_Blob
1966f 29 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  )) );.  assert( 
19670 66 67 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f  fg&(MEM_Int|MEM_
19671 52 65 61 6c 29 20 29 3b 0a 20 20 61 73 73 65 72  Real) );.  asser
19672 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26  t( (pMem->flags&
19673 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29  MEM_RowSet)==0 )
19674 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48  ;.  assert( EIGH
19675 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54  T_BYTE_ALIGNMENT
19676 28 70 4d 65 6d 29 20 29 3b 0a 0a 0a 20 20 69 66  (pMem) );...  if
19677 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  ( sqlite3VdbeMem
19678 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65  Grow(pMem, nByte
19679 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75  , 0) ){.    retu
1967a 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
1967b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20 61  .  }..  /* For a
1967c 20 52 65 61 6c 20 6f 72 20 49 6e 74 65 67 65 72   Real or Integer
1967d 2c 20 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 70  , use sqlite3_mp
1967e 72 69 6e 74 66 28 29 20 74 6f 20 70 72 6f 64 75  rintf() to produ
1967f 63 65 20 74 68 65 20 55 54 46 2d 38 0a 20 20 2a  ce the UTF-8.  *
19680 2a 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65  * string represe
19681 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 76  ntation of the v
19682 61 6c 75 65 2e 20 54 68 65 6e 2c 20 69 66 20 74  alue. Then, if t
19683 68 65 20 72 65 71 75 69 72 65 64 20 65 6e 63 6f  he required enco
19684 64 69 6e 67 0a 20 20 2a 2a 20 69 73 20 55 54 46  ding.  ** is UTF
19685 2d 31 36 6c 65 20 6f 72 20 55 54 46 2d 31 36 62  -16le or UTF-16b
19686 65 20 64 6f 20 61 20 74 72 61 6e 73 6c 61 74 69  e do a translati
19687 6f 6e 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 46  on..  ** .  ** F
19688 49 58 20 4d 45 3a 20 49 74 20 77 6f 75 6c 64 20  IX ME: It would 
19689 62 65 20 62 65 74 74 65 72 20 69 66 20 73 71 6c  be better if sql
1968a 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20  ite3_snprintf() 
1968b 63 6f 75 6c 64 20 64 6f 20 55 54 46 2d 31 36 2e  could do UTF-16.
1968c 0a 20 20 2a 2f 0a 20 20 69 66 28 20 66 67 20 26  .  */.  if( fg &
1968d 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20   MEM_Int ){.    
1968e 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1968f 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c  (nByte, pMem->z,
19690 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75   "%lld", pMem->u
19691 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  .i);.  }else{.  
19692 20 20 61 73 73 65 72 74 28 20 66 67 20 26 20 4d    assert( fg & M
19693 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 20 20 73  EM_Real );.    s
19694 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
19695 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20  nByte, pMem->z, 
19696 22 25 21 2e 31 35 67 22 2c 20 70 4d 65 6d 2d 3e  "%!.15g", pMem->
19697 72 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e  r);.  }.  pMem->
19698 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
19699 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20  n30(pMem->z);.  
1969a 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49  pMem->enc = SQLI
1969b 54 45 5f 55 54 46 38 3b 0a 20 20 70 4d 65 6d 2d  TE_UTF8;.  pMem-
1969c 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74  >flags |= MEM_St
1969d 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 73 71  r|MEM_Term;.  sq
1969e 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
1969f 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 65 6e  ncoding(pMem, en
196a0 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  c);.  return rc;
196a1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72 79  .}../*.** Memory
196a2 20 63 65 6c 6c 20 70 4d 65 6d 20 63 6f 6e 74 61   cell pMem conta
196a3 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 78 74 20  ins the context 
196a4 6f 66 20 61 6e 20 61 67 67 72 65 67 61 74 65 20  of an aggregate 
196a5 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69  function..** Thi
196a6 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 20  s routine calls 
196a7 74 68 65 20 66 69 6e 61 6c 69 7a 65 20 6d 65 74  the finalize met
196a8 68 6f 64 20 66 6f 72 20 74 68 61 74 20 66 75 6e  hod for that fun
196a9 63 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 72  ction.  The.** r
196aa 65 73 75 6c 74 20 6f 66 20 74 68 65 20 61 67 67  esult of the agg
196ab 72 65 67 61 74 65 20 69 73 20 73 74 6f 72 65 64  regate is stored
196ac 20 62 61 63 6b 20 69 6e 74 6f 20 70 4d 65 6d 2e   back into pMem.
196ad 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  .**.** Return SQ
196ae 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20 74 68  LITE_ERROR if th
196af 65 20 66 69 6e 61 6c 69 7a 65 72 20 72 65 70 6f  e finalizer repo
196b0 72 74 73 20 61 6e 20 65 72 72 6f 72 2e 20 20 53  rts an error.  S
196b1 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 74 68 65  QLITE_OK.** othe
196b2 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  rwise..*/.SQLITE
196b3 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
196b4 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c  ite3VdbeMemFinal
196b5 69 7a 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 46  ize(Mem *pMem, F
196b6 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 29 7b 0a  uncDef *pFunc){.
196b7 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
196b8 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 41 4c 57 41  E_OK;.  if( ALWA
196b9 59 53 28 70 46 75 6e 63 20 26 26 20 70 46 75 6e  YS(pFunc && pFun
196ba 63 2d 3e 78 46 69 6e 61 6c 69 7a 65 29 20 29 7b  c->xFinalize) ){
196bb 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  .    sqlite3_con
196bc 74 65 78 74 20 63 74 78 3b 0a 20 20 20 20 61 73  text ctx;.    as
196bd 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61  sert( (pMem->fla
196be 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d  gs & MEM_Null)!=
196bf 30 20 7c 7c 20 70 46 75 6e 63 3d 3d 70 4d 65 6d  0 || pFunc==pMem
196c0 2d 3e 75 2e 70 44 65 66 20 29 3b 0a 20 20 20 20  ->u.pDef );.    
196c1 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62  assert( pMem->db
196c2 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  ==0 || sqlite3_m
196c3 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e  utex_held(pMem->
196c4 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
196c5 20 20 6d 65 6d 73 65 74 28 26 63 74 78 2c 20 30    memset(&ctx, 0
196c6 2c 20 73 69 7a 65 6f 66 28 63 74 78 29 29 3b 0a  , sizeof(ctx));.
196c7 20 20 20 20 63 74 78 2e 73 2e 66 6c 61 67 73 20      ctx.s.flags 
196c8 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20  = MEM_Null;.    
196c9 63 74 78 2e 73 2e 64 62 20 3d 20 70 4d 65 6d 2d  ctx.s.db = pMem-
196ca 3e 64 62 3b 0a 20 20 20 20 63 74 78 2e 70 4d 65  >db;.    ctx.pMe
196cb 6d 20 3d 20 70 4d 65 6d 3b 0a 20 20 20 20 63 74  m = pMem;.    ct
196cc 78 2e 70 46 75 6e 63 20 3d 20 70 46 75 6e 63 3b  x.pFunc = pFunc;
196cd 0a 20 20 20 20 70 46 75 6e 63 2d 3e 78 46 69 6e  .    pFunc->xFin
196ce 61 6c 69 7a 65 28 26 63 74 78 29 3b 0a 20 20 20  alize(&ctx);.   
196cf 20 61 73 73 65 72 74 28 20 30 3d 3d 28 70 4d 65   assert( 0==(pMe
196d0 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e  m->flags&MEM_Dyn
196d1 29 20 26 26 20 21 70 4d 65 6d 2d 3e 78 44 65 6c  ) && !pMem->xDel
196d2 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   );.    sqlite3D
196d3 62 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c 20  bFree(pMem->db, 
196d4 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a  pMem->zMalloc);.
196d5 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2c      memcpy(pMem,
196d6 20 26 63 74 78 2e 73 2c 20 73 69 7a 65 6f 66 28   &ctx.s, sizeof(
196d7 63 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63 20  ctx.s));.    rc 
196d8 3d 20 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20  = ctx.isError;. 
196d9 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
196da 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  }../*.** If the 
196db 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 63 6f 6e 74  memory cell cont
196dc 61 69 6e 73 20 61 20 73 74 72 69 6e 67 20 76 61  ains a string va
196dd 6c 75 65 20 74 68 61 74 20 6d 75 73 74 20 62 65  lue that must be
196de 20 66 72 65 65 64 20 62 79 0a 2a 2a 20 69 6e 76   freed by.** inv
196df 6f 6b 69 6e 67 20 61 6e 20 65 78 74 65 72 6e 61  oking an externa
196e0 6c 20 63 61 6c 6c 62 61 63 6b 2c 20 66 72 65 65  l callback, free
196e1 20 69 74 20 6e 6f 77 2e 20 43 61 6c 6c 69 6e 67   it now. Calling
196e2 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a   this function.*
196e3 2a 20 64 6f 65 73 20 6e 6f 74 20 66 72 65 65 20  * does not free 
196e4 61 6e 79 20 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20  any Mem.zMalloc 
196e5 62 75 66 66 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  buffer..*/.SQLIT
196e6 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
196e7 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
196e8 65 61 73 65 45 78 74 65 72 6e 61 6c 28 4d 65 6d  easeExternal(Mem
196e9 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
196ea 70 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  p->db==0 || sqli
196eb 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
196ec 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
196ed 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 66    testcase( p->f
196ee 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 20 29  lags & MEM_Agg )
196ef 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 2d  ;.  testcase( p-
196f0 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e  >flags & MEM_Dyn
196f1 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
196f2 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  p->flags & MEM_R
196f3 6f 77 53 65 74 20 29 3b 0a 20 20 74 65 73 74 63  owSet );.  testc
196f4 61 73 65 28 20 70 2d 3e 66 6c 61 67 73 20 26 20  ase( p->flags & 
196f5 4d 45 4d 5f 46 72 61 6d 65 20 29 3b 0a 20 20 69  MEM_Frame );.  i
196f6 66 28 20 70 2d 3e 66 6c 61 67 73 26 28 4d 45 4d  f( p->flags&(MEM
196f7 5f 41 67 67 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d  _Agg|MEM_Dyn|MEM
196f8 5f 52 6f 77 53 65 74 7c 4d 45 4d 5f 46 72 61 6d  _RowSet|MEM_Fram
196f9 65 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  e) ){.    if( p-
196fa 3e 66 6c 61 67 73 26 4d 45 4d 5f 41 67 67 20 29  >flags&MEM_Agg )
196fb 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
196fc 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 70  dbeMemFinalize(p
196fd 2c 20 70 2d 3e 75 2e 70 44 65 66 29 3b 0a 20 20  , p->u.pDef);.  
196fe 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e      assert( (p->
196ff 66 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 29  flags & MEM_Agg)
19700 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==0 );.      sql
19701 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
19702 73 65 28 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65  se(p);.    }else
19703 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45   if( p->flags&ME
19704 4d 5f 44 79 6e 20 26 26 20 70 2d 3e 78 44 65 6c  M_Dyn && p->xDel
19705 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
19706 28 20 28 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  ( (p->flags&MEM_
19707 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20  RowSet)==0 );.  
19708 20 20 20 20 70 2d 3e 78 44 65 6c 28 28 76 6f 69      p->xDel((voi
19709 64 20 2a 29 70 2d 3e 7a 29 3b 0a 20 20 20 20 20  d *)p->z);.     
1970a 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20   p->xDel = 0;.  
1970b 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66    }else if( p->f
1970c 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 20  lags&MEM_RowSet 
1970d 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1970e 52 6f 77 53 65 74 43 6c 65 61 72 28 70 2d 3e 75  RowSetClear(p->u
1970f 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 7d  .pRowSet);.    }
19710 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67  else if( p->flag
19711 73 26 4d 45 4d 5f 46 72 61 6d 65 20 29 7b 0a 20  s&MEM_Frame ){. 
19712 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
19713 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 29 3b 0a 20  MemSetNull(p);. 
19714 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
19715 2a 20 52 65 6c 65 61 73 65 20 61 6e 79 20 6d 65  * Release any me
19716 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 74 68 65  mory held by the
19717 20 4d 65 6d 2e 20 54 68 69 73 20 6d 61 79 20 6c   Mem. This may l
19718 65 61 76 65 20 74 68 65 20 4d 65 6d 20 69 6e 20  eave the Mem in 
19719 61 6e 0a 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 65  an.** inconsiste
1971a 6e 74 20 73 74 61 74 65 2c 20 66 6f 72 20 65 78  nt state, for ex
1971b 61 6d 70 6c 65 20 77 69 74 68 20 28 4d 65 6d 2e  ample with (Mem.
1971c 7a 3d 3d 30 29 20 61 6e 64 0a 2a 2a 20 28 4d 65  z==0) and.** (Me
1971d 6d 2e 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 54  m.type==SQLITE_T
1971e 45 58 54 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  EXT)..*/.SQLITE_
1971f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
19720 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
19721 73 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 73 71  se(Mem *p){.  sq
19722 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
19723 61 73 65 45 78 74 65 72 6e 61 6c 28 70 29 3b 0a  aseExternal(p);.
19724 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
19725 70 2d 3e 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f  p->db, p->zMallo
19726 63 29 3b 0a 20 20 70 2d 3e 7a 20 3d 20 30 3b 0a  c);.  p->z = 0;.
19727 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30    p->zMalloc = 0
19728 3b 0a 20 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b  ;.  p->xDel = 0;
19729 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
1972a 74 20 61 20 36 34 2d 62 69 74 20 49 45 45 45 20  t a 64-bit IEEE 
1972b 64 6f 75 62 6c 65 20 69 6e 74 6f 20 61 20 36 34  double into a 64
1972c 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65  -bit signed inte
1972d 67 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64  ger..** If the d
1972e 6f 75 62 6c 65 20 69 73 20 74 6f 6f 20 6c 61 72  ouble is too lar
1972f 67 65 2c 20 72 65 74 75 72 6e 20 30 78 38 30 30  ge, return 0x800
19730 30 30 30 30 30 30 30 30 30 30 30 30 30 2e 0a 2a  0000000000000..*
19731 2a 0a 2a 2a 20 4d 6f 73 74 20 73 79 73 74 65 6d  *.** Most system
19732 73 20 61 70 70 65 61 72 20 74 6f 20 64 6f 20 74  s appear to do t
19733 68 69 73 20 73 69 6d 70 6c 79 20 62 79 20 61 73  his simply by as
19734 73 69 67 6e 69 6e 67 0a 2a 2a 20 76 61 72 69 61  signing.** varia
19735 62 6c 65 73 20 61 6e 64 20 77 69 74 68 6f 75 74  bles and without
19736 20 74 68 65 20 65 78 74 72 61 20 72 61 6e 67 65   the extra range
19737 20 74 65 73 74 73 2e 20 20 42 75 74 0a 2a 2a 20   tests.  But.** 
19738 74 68 65 72 65 20 61 72 65 20 72 65 70 6f 72 74  there are report
19739 73 20 74 68 61 74 20 77 69 6e 64 6f 77 73 20 74  s that windows t
1973a 68 72 6f 77 73 20 61 6e 20 65 78 70 65 63 74 69  hrows an expecti
1973b 6f 6e 0a 2a 2a 20 69 66 20 74 68 65 20 66 6c 6f  on.** if the flo
1973c 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75  ating point valu
1973d 65 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67  e is out of rang
1973e 65 2e 20 28 53 65 65 20 74 69 63 6b 65 74 20 23  e. (See ticket #
1973f 32 38 38 30 2e 29 0a 2a 2a 20 42 65 63 61 75 73  2880.).** Becaus
19740 65 20 77 65 20 64 6f 20 6e 6f 74 20 63 6f 6d 70  e we do not comp
19741 6c 65 74 65 6c 79 20 75 6e 64 65 72 73 74 61 6e  letely understan
19742 64 20 74 68 65 20 70 72 6f 62 6c 65 6d 2c 20 77  d the problem, w
19743 65 20 77 69 6c 6c 0a 2a 2a 20 74 61 6b 65 20 74  e will.** take t
19744 68 65 20 63 6f 6e 73 65 72 76 61 74 69 76 65 20  he conservative 
19745 61 70 70 72 6f 61 63 68 20 61 6e 64 20 61 6c 77  approach and alw
19746 61 79 73 20 64 6f 20 72 61 6e 67 65 20 74 65 73  ays do range tes
19747 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 61 74 74  ts.** before att
19748 65 6d 70 74 69 6e 67 20 74 68 65 20 63 6f 6e 76  empting the conv
19749 65 72 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  ersion..*/.stati
1974a 63 20 69 36 34 20 64 6f 75 62 6c 65 54 6f 49 6e  c i64 doubleToIn
1974b 74 36 34 28 64 6f 75 62 6c 65 20 72 29 7b 0a 20  t64(double r){. 
1974c 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6e 79 20 63 6f   /*.  ** Many co
1974d 6d 70 69 6c 65 72 73 20 77 65 20 65 6e 63 6f 75  mpilers we encou
1974e 6e 74 65 72 20 64 6f 20 6e 6f 74 20 64 65 66 69  nter do not defi
1974f 6e 65 20 63 6f 6e 73 74 61 6e 74 73 20 66 6f 72  ne constants for
19750 20 74 68 65 0a 20 20 2a 2a 20 6d 69 6e 69 6d 75   the.  ** minimu
19751 6d 20 61 6e 64 20 6d 61 78 69 6d 75 6d 20 36 34  m and maximum 64
19752 2d 62 69 74 20 69 6e 74 65 67 65 72 73 2c 20 6f  -bit integers, o
19753 72 20 74 68 65 79 20 64 65 66 69 6e 65 20 74 68  r they define th
19754 65 6d 0a 20 20 2a 2a 20 69 6e 63 6f 6e 73 69 73  em.  ** inconsis
19755 74 65 6e 74 6c 79 2e 20 20 41 6e 64 20 6d 61 6e  tently.  And man
19756 79 20 64 6f 20 6e 6f 74 20 75 6e 64 65 72 73 74  y do not underst
19757 61 6e 64 20 74 68 65 20 22 4c 4c 22 20 6e 6f 74  and the "LL" not
19758 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53 6f 20 77  ation..  ** So w
19759 65 20 64 65 66 69 6e 65 20 6f 75 72 20 6f 77 6e  e define our own
1975a 20 73 74 61 74 69 63 20 63 6f 6e 73 74 61 6e 74   static constant
1975b 73 20 68 65 72 65 20 75 73 69 6e 67 20 6e 6f 74  s here using not
1975c 68 69 6e 67 0a 20 20 2a 2a 20 6c 61 72 67 65 72  hing.  ** larger
1975d 20 74 68 61 6e 20 61 20 33 32 2d 62 69 74 20 69   than a 32-bit i
1975e 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 2e  nteger constant.
1975f 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63  .  */.  static c
19760 6f 6e 73 74 20 69 36 34 20 6d 61 78 49 6e 74 20  onst i64 maxInt 
19761 3d 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 3b  = LARGEST_INT64;
19762 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
19763 69 36 34 20 6d 69 6e 49 6e 74 20 3d 20 53 4d 41  i64 minInt = SMA
19764 4c 4c 45 53 54 5f 49 4e 54 36 34 3b 0a 0a 20 20  LLEST_INT64;..  
19765 69 66 28 20 72 3c 28 64 6f 75 62 6c 65 29 6d 69  if( r<(double)mi
19766 6e 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75  nInt ){.    retu
19767 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d 65 6c  rn minInt;.  }el
19768 73 65 20 69 66 28 20 72 3e 28 64 6f 75 62 6c 65  se if( r>(double
19769 29 6d 61 78 49 6e 74 20 29 7b 0a 20 20 20 20 2f  )maxInt ){.    /
1976a 2a 20 6d 69 6e 49 6e 74 20 69 73 20 63 6f 72 72  * minInt is corr
1976b 65 63 74 20 68 65 72 65 20 2d 20 6e 6f 74 20 6d  ect here - not m
1976c 61 78 49 6e 74 2e 20 20 49 74 20 74 75 72 6e 73  axInt.  It turns
1976d 20 6f 75 74 20 74 68 61 74 20 61 73 73 69 67 6e   out that assign
1976e 69 6e 67 0a 20 20 20 20 2a 2a 20 61 20 76 65 72  ing.    ** a ver
1976f 79 20 6c 61 72 67 65 20 70 6f 73 69 74 69 76 65  y large positive
19770 20 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 69 6e   number to an in
19771 74 65 67 65 72 20 72 65 73 75 6c 74 73 20 69 6e  teger results in
19772 20 61 20 76 65 72 79 20 6c 61 72 67 65 0a 20 20   a very large.  
19773 20 20 2a 2a 20 6e 65 67 61 74 69 76 65 20 69 6e    ** negative in
19774 74 65 67 65 72 2e 20 20 54 68 69 73 20 6d 61 6b  teger.  This mak
19775 65 73 20 6e 6f 20 73 65 6e 73 65 2c 20 62 75 74  es no sense, but
19776 20 69 74 20 69 73 20 77 68 61 74 20 78 38 36 20   it is what x86 
19777 68 61 72 64 77 61 72 65 0a 20 20 20 20 2a 2a 20  hardware.    ** 
19778 64 6f 65 73 20 73 6f 20 66 6f 72 20 63 6f 6d 70  does so for comp
19779 61 74 69 62 69 6c 69 74 79 20 77 65 20 77 69 6c  atibility we wil
1977a 6c 20 64 6f 20 74 68 65 20 73 61 6d 65 20 69 6e  l do the same in
1977b 20 73 6f 66 74 77 61 72 65 2e 20 2a 2f 0a 20 20   software. */.  
1977c 20 20 72 65 74 75 72 6e 20 6d 69 6e 49 6e 74 3b    return minInt;
1977d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
1977e 74 75 72 6e 20 28 69 36 34 29 72 3b 0a 20 20 7d  turn (i64)r;.  }
1977f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
19780 20 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 69 6e   some kind of in
19781 74 65 67 65 72 20 76 61 6c 75 65 20 77 68 69 63  teger value whic
19782 68 20 69 73 20 74 68 65 20 62 65 73 74 20 77 65  h is the best we
19783 20 63 61 6e 20 64 6f 0a 2a 2a 20 61 74 20 72 65   can do.** at re
19784 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 76  presenting the v
19785 61 6c 75 65 20 74 68 61 74 20 2a 70 4d 65 6d 20  alue that *pMem 
19786 64 65 73 63 72 69 62 65 73 20 61 73 20 61 6e 20  describes as an 
19787 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 49 66 20 70  integer..** If p
19788 4d 65 6d 20 69 73 20 61 6e 20 69 6e 74 65 67 65  Mem is an intege
19789 72 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75  r, then the valu
1978a 65 20 69 73 20 65 78 61 63 74 2e 20 20 49 66 20  e is exact.  If 
1978b 70 4d 65 6d 20 69 73 0a 2a 2a 20 61 20 66 6c 6f  pMem is.** a flo
1978c 61 74 69 6e 67 2d 70 6f 69 6e 74 20 74 68 65 6e  ating-point then
1978d 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   the value retur
1978e 6e 65 64 20 69 73 20 74 68 65 20 69 6e 74 65 67  ned is the integ
1978f 65 72 20 70 61 72 74 2e 0a 2a 2a 20 49 66 20 70  er part..** If p
19790 4d 65 6d 20 69 73 20 61 20 73 74 72 69 6e 67 20  Mem is a string 
19791 6f 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 77 65  or blob, then we
19792 20 6d 61 6b 65 20 61 6e 20 61 74 74 65 6d 70 74   make an attempt
19793 20 74 6f 20 63 6f 6e 76 65 72 74 0a 2a 2a 20 69   to convert.** i
19794 74 20 69 6e 74 6f 20 61 20 69 6e 74 65 67 65 72  t into a integer
19795 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 61 74   and return that
19796 2e 20 20 49 66 20 70 4d 65 6d 20 72 65 70 72 65  .  If pMem repre
19797 73 65 6e 74 73 20 61 6e 0a 2a 2a 20 61 6e 20 53  sents an.** an S
19798 51 4c 2d 4e 55 4c 4c 20 76 61 6c 75 65 2c 20 72  QL-NULL value, r
19799 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 49  eturn 0..**.** I
1979a 66 20 70 4d 65 6d 20 72 65 70 72 65 73 65 6e 74  f pMem represent
1979b 73 20 61 20 73 74 72 69 6e 67 20 76 61 6c 75 65  s a string value
1979c 2c 20 69 74 73 20 65 6e 63 6f 64 69 6e 67 20 6d  , its encoding m
1979d 69 67 68 74 20 62 65 20 63 68 61 6e 67 65 64 2e  ight be changed.
1979e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1979f 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 56 64  TE i64 sqlite3Vd
197a0 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d 20 2a  beIntValue(Mem *
197a1 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61  pMem){.  int fla
197a2 67 73 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d  gs;.  assert( pM
197a3 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
197a4 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
197a5 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
197a6 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49   );.  assert( EI
197a7 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45  GHT_BYTE_ALIGNME
197a8 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20 66 6c  NT(pMem) );.  fl
197a9 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67  ags = pMem->flag
197aa 73 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26  s;.  if( flags &
197ab 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20   MEM_Int ){.    
197ac 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 75 2e 69  return pMem->u.i
197ad 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c  ;.  }else if( fl
197ae 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29  ags & MEM_Real )
197af 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 64 6f 75  {.    return dou
197b0 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d 2d  bleToInt64(pMem-
197b1 3e 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  >r);.  }else if(
197b2 20 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74   flags & (MEM_St
197b3 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20  r|MEM_Blob) ){. 
197b4 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a 20 20     i64 value;.  
197b5 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d    pMem->flags |=
197b6 20 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66   MEM_Str;.    if
197b7 28 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61  ( sqlite3VdbeCha
197b8 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d  ngeEncoding(pMem
197b9 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 0a 20  , SQLITE_UTF8). 
197ba 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33        || sqlite3
197bb 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e  VdbeMemNulTermin
197bc 61 74 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20  ate(pMem) ){.   
197bd 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
197be 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
197bf 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20 73 71  Mem->z );.    sq
197c0 6c 69 74 65 33 41 74 6f 69 36 34 28 70 4d 65 6d  lite3Atoi64(pMem
197c1 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20  ->z, &value);.  
197c2 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65 3b 0a    return value;.
197c3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
197c4 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  urn 0;.  }.}../*
197c5 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 62  .** Return the b
197c6 65 73 74 20 72 65 70 72 65 73 65 6e 74 61 74 69  est representati
197c7 6f 6e 20 6f 66 20 70 4d 65 6d 20 74 68 61 74 20  on of pMem that 
197c8 77 65 20 63 61 6e 20 67 65 74 20 69 6e 74 6f 20  we can get into 
197c9 61 0a 2a 2a 20 64 6f 75 62 6c 65 2e 20 20 49 66  a.** double.  If
197ca 20 70 4d 65 6d 20 69 73 20 61 6c 72 65 61 64 79   pMem is already
197cb 20 61 20 64 6f 75 62 6c 65 20 6f 72 20 61 6e 20   a double or an 
197cc 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20  integer, return 
197cd 69 74 73 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 49  its.** value.  I
197ce 66 20 69 74 20 69 73 20 61 20 73 74 72 69 6e 67  f it is a string
197cf 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f   or blob, try to
197d0 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61   convert it to a
197d1 20 64 6f 75 62 6c 65 2e 0a 2a 2a 20 49 66 20 69   double..** If i
197d2 74 20 69 73 20 61 20 4e 55 4c 4c 2c 20 72 65 74  t is a NULL, ret
197d3 75 72 6e 20 30 2e 30 2e 0a 2a 2f 0a 53 51 4c 49  urn 0.0..*/.SQLI
197d4 54 45 5f 50 52 49 56 41 54 45 20 64 6f 75 62 6c  TE_PRIVATE doubl
197d5 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61  e sqlite3VdbeRea
197d6 6c 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d  lValue(Mem *pMem
197d7 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  ){.  assert( pMe
197d8 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  m->db==0 || sqli
197d9 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
197da 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  Mem->db->mutex) 
197db 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47  );.  assert( EIG
197dc 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e  HT_BYTE_ALIGNMEN
197dd 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20 69 66 28  T(pMem) );.  if(
197de 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d   pMem->flags & M
197df 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72  EM_Real ){.    r
197e0 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 72 3b 0a 20  eturn pMem->r;. 
197e1 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d   }else if( pMem-
197e2 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74  >flags & MEM_Int
197e3 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28   ){.    return (
197e4 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69  double)pMem->u.i
197e5 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d  ;.  }else if( pM
197e6 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  em->flags & (MEM
197e7 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29  _Str|MEM_Blob) )
197e8 7b 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65  {.    /* (double
197e9 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51  )0 In case of SQ
197ea 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
197eb 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20  NG_POINT... */. 
197ec 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d 20     double val = 
197ed 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20 20 70  (double)0;.    p
197ee 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  Mem->flags |= ME
197ef 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66 28 20 73  M_Str;.    if( s
197f0 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
197f1 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 53  Encoding(pMem, S
197f2 51 4c 49 54 45 5f 55 54 46 38 29 0a 20 20 20 20  QLITE_UTF8).    
197f3 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 56 64 62     || sqlite3Vdb
197f4 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65  eMemNulTerminate
197f5 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 20 20  (pMem) ){.      
197f6 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20  /* (double)0 In 
197f7 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f  case of SQLITE_O
197f8 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
197f9 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 72  NT... */.      r
197fa 65 74 75 72 6e 20 28 64 6f 75 62 6c 65 29 30 3b  eturn (double)0;
197fb 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
197fc 74 28 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20  t( pMem->z );.  
197fd 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28 70 4d    sqlite3AtoF(pM
197fe 65 6d 2d 3e 7a 2c 20 26 76 61 6c 29 3b 0a 20 20  em->z, &val);.  
197ff 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20    return val;.  
19800 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 28 64  }else{.    /* (d
19801 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 73 65 20  ouble)0 In case 
19802 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  of SQLITE_OMIT_F
19803 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e  LOATING_POINT...
19804 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 28   */.    return (
19805 64 6f 75 62 6c 65 29 30 3b 0a 20 20 7d 0a 7d 0a  double)0;.  }.}.
19806 0a 2f 2a 0a 2a 2a 20 54 68 65 20 4d 45 4d 20 73  ./*.** The MEM s
19807 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 72 65  tructure is alre
19808 61 64 79 20 61 20 4d 45 4d 5f 52 65 61 6c 2e 20  ady a MEM_Real. 
19809 20 54 72 79 20 74 6f 20 61 6c 73 6f 20 6d 61 6b   Try to also mak
1980a 65 20 69 74 20 61 0a 2a 2a 20 4d 45 4d 5f 49 6e  e it a.** MEM_In
1980b 74 20 69 66 20 77 65 20 63 61 6e 2e 0a 2a 2f 0a  t if we can..*/.
1980c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1980d 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49  oid sqlite3VdbeI
1980e 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 4d  ntegerAffinity(M
1980f 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73  em *pMem){.  ass
19810 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  ert( pMem->flags
19811 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20   & MEM_Real );. 
19812 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e   assert( (pMem->
19813 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53  flags & MEM_RowS
19814 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  et)==0 );.  asse
19815 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20  rt( pMem->db==0 
19816 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
19817 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e  _held(pMem->db->
19818 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
19819 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41  rt( EIGHT_BYTE_A
1981a 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29  LIGNMENT(pMem) )
1981b 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d  ;..  pMem->u.i =
1981c 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 70   doubleToInt64(p
1981d 4d 65 6d 2d 3e 72 29 3b 0a 0a 20 20 2f 2a 20 4f  Mem->r);..  /* O
1981e 6e 6c 79 20 6d 61 72 6b 20 74 68 65 20 76 61 6c  nly mark the val
1981f 75 65 20 61 73 20 61 6e 20 69 6e 74 65 67 65 72  ue as an integer
19820 20 69 66 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20   if.  **.  **   
19821 20 28 31 29 20 74 68 65 20 72 6f 75 6e 64 2d 74   (1) the round-t
19822 72 69 70 20 63 6f 6e 76 65 72 73 69 6f 6e 20 72  rip conversion r
19823 65 61 6c 2d 3e 69 6e 74 2d 3e 72 65 61 6c 20 69  eal->int->real i
19824 73 20 61 20 6e 6f 2d 6f 70 2c 20 61 6e 64 0a 20  s a no-op, and. 
19825 20 2a 2a 20 20 20 20 28 32 29 20 54 68 65 20 69   **    (2) The i
19826 6e 74 65 67 65 72 20 69 73 20 6e 65 69 74 68 65  nteger is neithe
19827 72 20 74 68 65 20 6c 61 72 67 65 73 74 20 6e 6f  r the largest no
19828 72 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 0a 20  r the smallest. 
19829 20 2a 2a 20 20 20 20 20 20 20 20 70 6f 73 73 69   **        possi
1982a 62 6c 65 20 69 6e 74 65 67 65 72 20 28 74 69 63  ble integer (tic
1982b 6b 65 74 20 23 33 39 32 32 29 0a 20 20 2a 2a 0a  ket #3922).  **.
1982c 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20    ** The second 
1982d 61 6e 64 20 74 68 69 72 64 20 74 65 72 6d 73 20  and third terms 
1982e 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  in the following
1982f 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 65 6e 66   conditional enf
19830 6f 72 63 65 73 0a 20 20 2a 2a 20 74 68 65 20 73  orces.  ** the s
19831 65 63 6f 6e 64 20 63 6f 6e 64 69 74 69 6f 6e 20  econd condition 
19832 75 6e 64 65 72 20 74 68 65 20 61 73 73 75 6d 70  under the assump
19833 74 69 6f 6e 20 74 68 61 74 20 61 64 64 69 74 69  tion that additi
19834 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 63 61 75 73  on overflow caus
19835 65 73 0a 20 20 2a 2a 20 76 61 6c 75 65 73 20 74  es.  ** values t
19836 6f 20 77 72 61 70 20 61 72 6f 75 6e 64 2e 20 20  o wrap around.  
19837 4f 6e 20 78 38 36 20 68 61 72 64 77 61 72 65 2c  On x86 hardware,
19838 20 74 68 65 20 74 68 69 72 64 20 74 65 72 6d 20   the third term 
19839 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 74  is always.  ** t
1983a 72 75 65 20 61 6e 64 20 63 6f 75 6c 64 20 62 65  rue and could be
1983b 20 6f 6d 69 74 74 65 64 2e 20 20 42 75 74 20 77   omitted.  But w
1983c 65 20 6c 65 61 76 65 20 69 74 20 69 6e 20 62 65  e leave it in be
1983d 63 61 75 73 65 20 6f 74 68 65 72 0a 20 20 2a 2a  cause other.  **
1983e 20 61 72 63 68 69 74 65 63 74 75 72 65 73 20 6d   architectures m
1983f 69 67 68 74 20 62 65 68 61 76 65 20 64 69 66 66  ight behave diff
19840 65 72 65 6e 74 6c 79 2e 0a 20 20 2a 2f 0a 20 20  erently..  */.  
19841 69 66 28 20 70 4d 65 6d 2d 3e 72 3d 3d 28 64 6f  if( pMem->r==(do
19842 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69 20 26  uble)pMem->u.i &
19843 26 20 70 4d 65 6d 2d 3e 75 2e 69 3e 53 4d 41 4c  & pMem->u.i>SMAL
19844 4c 45 53 54 5f 49 4e 54 36 34 0a 20 20 20 20 20  LEST_INT64.     
19845 20 26 26 20 41 4c 57 41 59 53 28 70 4d 65 6d 2d   && ALWAYS(pMem-
19846 3e 75 2e 69 3c 4c 41 52 47 45 53 54 5f 49 4e 54  >u.i<LARGEST_INT
19847 36 34 29 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d  64) ){.    pMem-
19848 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e  >flags |= MEM_In
19849 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  t;.  }.}../*.** 
1984a 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 74 6f 20  Convert pMem to 
1984b 74 79 70 65 20 69 6e 74 65 67 65 72 2e 20 20 49  type integer.  I
1984c 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72  nvalidate any pr
1984d 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69  ior representati
1984e 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ons..*/.SQLITE_P
1984f 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19850 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72  e3VdbeMemInteger
19851 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a  ify(Mem *pMem){.
19852 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
19853 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
19854 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d  _mutex_held(pMem
19855 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
19856 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d    assert( (pMem-
19857 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
19858 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73  Set)==0 );.  ass
19859 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f  ert( EIGHT_BYTE_
1985a 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20  ALIGNMENT(pMem) 
1985b 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20  );..  pMem->u.i 
1985c 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74  = sqlite3VdbeInt
1985d 56 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d  Value(pMem);.  M
1985e 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4d  emSetTypeFlag(pM
1985f 65 6d 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20  em, MEM_Int);.  
19860 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
19861 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ;.}../*.** Conve
19862 72 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20  rt pMem so that 
19863 69 74 20 69 73 20 6f 66 20 74 79 70 65 20 4d 45  it is of type ME
19864 4d 5f 52 65 61 6c 2e 0a 2a 2a 20 49 6e 76 61 6c  M_Real..** Inval
19865 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20  idate any prior 
19866 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e  representations.
19867 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19868 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
19869 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d 65 6d  beMemRealify(Mem
1986a 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72   *pMem){.  asser
1986b 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c  t( pMem->db==0 |
1986c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
1986d 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d  held(pMem->db->m
1986e 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
1986f 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c  t( EIGHT_BYTE_AL
19870 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b  IGNMENT(pMem) );
19871 0a 0a 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 73 71  ..  pMem->r = sq
19872 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c  lite3VdbeRealVal
19873 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53  ue(pMem);.  MemS
19874 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c  etTypeFlag(pMem,
19875 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 72 65   MEM_Real);.  re
19876 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
19877 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  }../*.** Convert
19878 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 74   pMem so that it
19879 20 68 61 73 20 74 79 70 65 73 20 4d 45 4d 5f 52   has types MEM_R
1987a 65 61 6c 20 6f 72 20 4d 45 4d 5f 49 6e 74 20 6f  eal or MEM_Int o
1987b 72 20 62 6f 74 68 2e 0a 2a 2a 20 49 6e 76 61 6c  r both..** Inval
1987c 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20  idate any prior 
1987d 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e  representations.
1987e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1987f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
19880 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d 65  beMemNumerify(Me
19881 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 64 6f 75 62  m *pMem){.  doub
19882 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 69 36 34  le r1, r2;.  i64
19883 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   i;.  assert( (p
19884 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45  Mem->flags & (ME
19885 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d  M_Int|MEM_Real|M
19886 45 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29 3b 0a  EM_Null))==0 );.
19887 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d    assert( (pMem-
19888 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c  >flags & (MEM_Bl
19889 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 21 3d 30 20  ob|MEM_Str))!=0 
1988a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  );.  assert( pMe
1988b 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  m->db==0 || sqli
1988c 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1988d 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  Mem->db->mutex) 
1988e 29 3b 0a 20 20 72 31 20 3d 20 73 71 6c 69 74 65  );.  r1 = sqlite
1988f 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70  3VdbeRealValue(p
19890 4d 65 6d 29 3b 0a 20 20 69 20 3d 20 64 6f 75 62  Mem);.  i = doub
19891 6c 65 54 6f 49 6e 74 36 34 28 72 31 29 3b 0a 20  leToInt64(r1);. 
19892 20 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 69 3b   r2 = (double)i;
19893 0a 20 20 69 66 28 20 72 31 3d 3d 72 32 20 29 7b  .  if( r1==r2 ){
19894 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
19895 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 4d  MemIntegerify(pM
19896 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  em);.  }else{.  
19897 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 72 31 3b 0a    pMem->r = r1;.
19898 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c      MemSetTypeFl
19899 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61  ag(pMem, MEM_Rea
1989a 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  l);.  }.  return
1989b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
1989c 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20  *.** Delete any 
1989d 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61  previous value a
1989e 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65  nd set the value
1989f 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d   stored in *pMem
198a0 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c   to NULL..*/.SQL
198a1 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
198a2 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
198a3 65 74 4e 75 6c 6c 28 4d 65 6d 20 2a 70 4d 65 6d  etNull(Mem *pMem
198a4 29 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66  ){.  if( pMem->f
198a5 6c 61 67 73 20 26 20 4d 45 4d 5f 46 72 61 6d 65  lags & MEM_Frame
198a6 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
198a7 64 62 65 46 72 61 6d 65 44 65 6c 65 74 65 28 70  dbeFrameDelete(p
198a8 4d 65 6d 2d 3e 75 2e 70 46 72 61 6d 65 29 3b 0a  Mem->u.pFrame);.
198a9 20 20 7d 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e    }.  if( pMem->
198aa 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53  flags & MEM_RowS
198ab 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  et ){.    sqlite
198ac 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 4d 65  3RowSetClear(pMe
198ad 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20  m->u.pRowSet);. 
198ae 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46   }.  MemSetTypeF
198af 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75  lag(pMem, MEM_Nu
198b0 6c 6c 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70  ll);.  pMem->typ
198b1 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b  e = SQLITE_NULL;
198b2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
198b3 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61   any previous va
198b4 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 20  lue and set the 
198b5 76 61 6c 75 65 20 74 6f 20 62 65 20 61 20 42 4c  value to be a BL
198b6 4f 42 20 6f 66 20 6c 65 6e 67 74 68 0a 2a 2a 20  OB of length.** 
198b7 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c  n containing all
198b8 20 7a 65 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54   zeros..*/.SQLIT
198b9 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
198ba 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
198bb 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d  ZeroBlob(Mem *pM
198bc 65 6d 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 73 71  em, int n){.  sq
198bd 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
198be 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65  ase(pMem);.  pMe
198bf 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42  m->flags = MEM_B
198c0 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20  lob|MEM_Zero;.  
198c1 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
198c2 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d 65 6d  ITE_BLOB;.  pMem
198c3 2d 3e 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e  ->n = 0;.  if( n
198c4 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 70 4d  <0 ) n = 0;.  pM
198c5 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e 3b  em->u.nZero = n;
198c6 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53  .  pMem->enc = S
198c7 51 4c 49 54 45 5f 55 54 46 38 3b 0a 0a 23 69 66  QLITE_UTF8;..#if
198c8 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
198c9 49 4e 43 52 42 4c 4f 42 0a 20 20 73 71 6c 69 74  INCRBLOB.  sqlit
198ca 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d  e3VdbeMemGrow(pM
198cb 65 6d 2c 20 6e 2c 20 30 29 3b 0a 20 20 69 66 28  em, n, 0);.  if(
198cc 20 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20   pMem->z ){.    
198cd 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 3b 0a 20 20 20  pMem->n = n;.   
198ce 20 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c   memset(pMem->z,
198cf 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64   0, n);.  }.#end
198d0 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65  if.}../*.** Dele
198d1 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20  te any previous 
198d2 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68  value and set th
198d3 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69  e value stored i
198d4 6e 20 2a 70 4d 65 6d 20 74 6f 20 76 61 6c 2c 0a  n *pMem to val,.
198d5 2a 2a 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65  ** manifest type
198d6 20 49 4e 54 45 47 45 52 2e 0a 2a 2f 0a 53 51 4c   INTEGER..*/.SQL
198d7 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
198d8 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
198d9 65 74 49 6e 74 36 34 28 4d 65 6d 20 2a 70 4d 65  etInt64(Mem *pMe
198da 6d 2c 20 69 36 34 20 76 61 6c 29 7b 0a 20 20 73  m, i64 val){.  s
198db 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
198dc 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d  ease(pMem);.  pM
198dd 65 6d 2d 3e 75 2e 69 20 3d 20 76 61 6c 3b 0a 20  em->u.i = val;. 
198de 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
198df 45 4d 5f 49 6e 74 3b 0a 20 20 70 4d 65 6d 2d 3e  EM_Int;.  pMem->
198e0 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e  type = SQLITE_IN
198e1 54 45 47 45 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TEGER;.}../*.** 
198e2 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 69  Delete any previ
198e3 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65  ous value and se
198e4 74 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72  t the value stor
198e5 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f 20 76  ed in *pMem to v
198e6 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 73 74 20  al,.** manifest 
198e7 74 79 70 65 20 52 45 41 4c 2e 0a 2a 2f 0a 53 51  type REAL..*/.SQ
198e8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
198e9 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
198ea 53 65 74 44 6f 75 62 6c 65 28 4d 65 6d 20 2a 70  SetDouble(Mem *p
198eb 4d 65 6d 2c 20 64 6f 75 62 6c 65 20 76 61 6c 29  Mem, double val)
198ec 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49  {.  if( sqlite3I
198ed 73 4e 61 4e 28 76 61 6c 29 20 29 7b 0a 20 20 20  sNaN(val) ){.   
198ee 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
198ef 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20 20  etNull(pMem);.  
198f0 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
198f1 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
198f2 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d  (pMem);.    pMem
198f3 2d 3e 72 20 3d 20 76 61 6c 3b 0a 20 20 20 20 70  ->r = val;.    p
198f4 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
198f5 5f 52 65 61 6c 3b 0a 20 20 20 20 70 4d 65 6d 2d  _Real;.    pMem-
198f6 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 46  >type = SQLITE_F
198f7 4c 4f 41 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  LOAT;.  }.}../*.
198f8 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72  ** Delete any pr
198f9 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64  evious value and
198fa 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f   set the value o
198fb 66 20 70 4d 65 6d 20 74 6f 20 62 65 20 61 6e 0a  f pMem to be an.
198fc 2a 2a 20 65 6d 70 74 79 20 62 6f 6f 6c 65 61 6e  ** empty boolean
198fd 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54   index..*/.SQLIT
198fe 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
198ff 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
19900 52 6f 77 53 65 74 28 4d 65 6d 20 2a 70 4d 65 6d  RowSet(Mem *pMem
19901 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
19902 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0a 20 20 61   = pMem->db;.  a
19903 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a  ssert( db!=0 );.
19904 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d    assert( (pMem-
19905 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
19906 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c  Set)==0 );.  sql
19907 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
19908 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d  se(pMem);.  pMem
19909 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69  ->zMalloc = sqli
1990a 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
1990b 62 2c 20 36 34 29 3b 0a 20 20 69 66 28 20 64 62  b, 64);.  if( db
1990c 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1990d 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  {.    pMem->flag
1990e 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
1990f 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
19910 74 28 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63  t( pMem->zMalloc
19911 20 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e   );.    pMem->u.
19912 70 52 6f 77 53 65 74 20 3d 20 73 71 6c 69 74 65  pRowSet = sqlite
19913 33 52 6f 77 53 65 74 49 6e 69 74 28 64 62 2c 20  3RowSetInit(db, 
19914 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 0a  pMem->zMalloc, .
19915 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19916 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19917 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
19918 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 70  MallocSize(db, p
19919 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 29 3b 0a  Mem->zMalloc));.
1991a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
1991b 2d 3e 75 2e 70 52 6f 77 53 65 74 21 3d 30 20 29  ->u.pRowSet!=0 )
1991c 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  ;.    pMem->flag
1991d 73 20 3d 20 4d 45 4d 5f 52 6f 77 53 65 74 3b 0a  s = MEM_RowSet;.
1991e 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74    }.}../*.** Ret
1991f 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20  urn true if the 
19920 4d 65 6d 20 6f 62 6a 65 63 74 20 63 6f 6e 74 61  Mem object conta
19921 69 6e 73 20 61 20 54 45 58 54 20 6f 72 20 42 4c  ins a TEXT or BL
19922 4f 42 20 74 68 61 74 20 69 73 0a 2a 2a 20 74 6f  OB that is.** to
19923 6f 20 6c 61 72 67 65 20 2d 20 77 68 6f 73 65 20  o large - whose 
19924 73 69 7a 65 20 65 78 63 65 65 64 73 20 53 51 4c  size exceeds SQL
19925 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 2e 0a  ITE_MAX_LENGTH..
19926 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19927 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
19928 65 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 6d 20 2a  eMemTooBig(Mem *
19929 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  p){.  assert( p-
1992a 3e 64 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  >db!=0 );.  if( 
1992b 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  p->flags & (MEM_
1992c 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b  Str|MEM_Blob) ){
1992d 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 2d 3e  .    int n = p->
1992e 6e 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c  n;.    if( p->fl
1992f 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29  ags & MEM_Zero )
19930 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 70 2d 3e  {.      n += p->
19931 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 0a 20  u.nZero;.    }. 
19932 20 20 20 72 65 74 75 72 6e 20 6e 3e 70 2d 3e 64     return n>p->d
19933 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
19934 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a  _LIMIT_LENGTH];.
19935 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 20    }.  return 0; 
19936 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a 65 20 6f  .}../*.** Size o
19937 66 20 73 74 72 75 63 74 20 4d 65 6d 20 6e 6f 74  f struct Mem not
19938 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 4d   including the M
19939 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 6d 65 6d 62 65  em.zMalloc membe
1993a 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45  r..*/.#define ME
1993b 4d 43 45 4c 4c 53 49 5a 45 20 28 73 69 7a 65 5f  MCELLSIZE (size_
1993c 74 29 28 26 28 28 28 4d 65 6d 20 2a 29 30 29 2d  t)(&(((Mem *)0)-
1993d 3e 7a 4d 61 6c 6c 6f 63 29 29 0a 0a 2f 2a 0a 2a  >zMalloc))../*.*
1993e 2a 20 4d 61 6b 65 20 61 6e 20 73 68 61 6c 6c 6f  * Make an shallo
1993f 77 20 63 6f 70 79 20 6f 66 20 70 46 72 6f 6d 20  w copy of pFrom 
19940 69 6e 74 6f 20 70 54 6f 2e 20 20 50 72 69 6f 72  into pTo.  Prior
19941 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 2a 2a 20   contents of.** 
19942 70 54 6f 20 61 72 65 20 66 72 65 65 64 2e 20 20  pTo are freed.  
19943 54 68 65 20 70 46 72 6f 6d 2d 3e 7a 20 66 69 65  The pFrom->z fie
19944 6c 64 20 69 73 20 6e 6f 74 20 64 75 70 6c 69 63  ld is not duplic
19945 61 74 65 64 2e 20 20 49 66 0a 2a 2a 20 70 46 72  ated.  If.** pFr
19946 6f 6d 2d 3e 7a 20 69 73 20 75 73 65 64 2c 20 74  om->z is used, t
19947 68 65 6e 20 70 54 6f 2d 3e 7a 20 70 6f 69 6e 74  hen pTo->z point
19948 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 68  s to the same th
19949 69 6e 67 20 61 73 20 70 46 72 6f 6d 2d 3e 7a 0a  ing as pFrom->z.
1994a 2a 2a 20 61 6e 64 20 66 6c 61 67 73 20 67 65 74  ** and flags get
1994b 73 20 73 72 63 54 79 70 65 20 28 65 69 74 68 65  s srcType (eithe
1994c 72 20 4d 45 4d 5f 45 70 68 65 6d 20 6f 72 20 4d  r MEM_Ephem or M
1994d 45 4d 5f 53 74 61 74 69 63 29 2e 0a 2a 2f 0a 53  EM_Static)..*/.S
1994e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1994f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  id sqlite3VdbeMe
19950 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 4d 65 6d  mShallowCopy(Mem
19951 20 2a 70 54 6f 2c 20 63 6f 6e 73 74 20 4d 65 6d   *pTo, const Mem
19952 20 2a 70 46 72 6f 6d 2c 20 69 6e 74 20 73 72 63   *pFrom, int src
19953 54 79 70 65 29 7b 0a 20 20 61 73 73 65 72 74 28  Type){.  assert(
19954 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20 26   (pFrom->flags &
19955 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20   MEM_RowSet)==0 
19956 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
19957 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e  MemReleaseExtern
19958 61 6c 28 70 54 6f 29 3b 0a 20 20 6d 65 6d 63 70  al(pTo);.  memcp
19959 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45  y(pTo, pFrom, ME
1995a 4d 43 45 4c 4c 53 49 5a 45 29 3b 0a 20 20 70 54  MCELLSIZE);.  pT
1995b 6f 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 69  o->xDel = 0;.  i
1995c 66 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73  f( (pFrom->flags
1995d 26 4d 45 4d 5f 44 79 6e 29 21 3d 30 20 7c 7c 20  &MEM_Dyn)!=0 || 
1995e 70 46 72 6f 6d 2d 3e 7a 3d 3d 70 46 72 6f 6d 2d  pFrom->z==pFrom-
1995f 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20  >zMalloc ){.    
19960 70 54 6f 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28  pTo->flags &= ~(
19961 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 61 74  MEM_Dyn|MEM_Stat
19962 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20  ic|MEM_Ephem);. 
19963 20 20 20 61 73 73 65 72 74 28 20 73 72 63 54 79     assert( srcTy
19964 70 65 3d 3d 4d 45 4d 5f 45 70 68 65 6d 20 7c 7c  pe==MEM_Ephem ||
19965 20 73 72 63 54 79 70 65 3d 3d 4d 45 4d 5f 53 74   srcType==MEM_St
19966 61 74 69 63 20 29 3b 0a 20 20 20 20 70 54 6f 2d  atic );.    pTo-
19967 3e 66 6c 61 67 73 20 7c 3d 20 73 72 63 54 79 70  >flags |= srcTyp
19968 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  e;.  }.}../*.** 
19969 4d 61 6b 65 20 61 20 66 75 6c 6c 20 63 6f 70 79  Make a full copy
1996a 20 6f 66 20 70 46 72 6f 6d 20 69 6e 74 6f 20 70   of pFrom into p
1996b 54 6f 2e 20 20 50 72 69 6f 72 20 63 6f 6e 74 65  To.  Prior conte
1996c 6e 74 73 20 6f 66 20 70 54 6f 20 61 72 65 0a 2a  nts of pTo are.*
1996d 2a 20 66 72 65 65 64 20 62 65 66 6f 72 65 20 74  * freed before t
1996e 68 65 20 63 6f 70 79 20 69 73 20 6d 61 64 65 2e  he copy is made.
1996f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19970 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
19971 62 65 4d 65 6d 43 6f 70 79 28 4d 65 6d 20 2a 70  beMemCopy(Mem *p
19972 54 6f 2c 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70  To, const Mem *p
19973 46 72 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20  From){.  int rc 
19974 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  = SQLITE_OK;..  
19975 61 73 73 65 72 74 28 20 28 70 46 72 6f 6d 2d 3e  assert( (pFrom->
19976 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53  flags & MEM_RowS
19977 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69  et)==0 );.  sqli
19978 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
19979 65 45 78 74 65 72 6e 61 6c 28 70 54 6f 29 3b 0a  eExternal(pTo);.
1997a 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46    memcpy(pTo, pF
1997b 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a 45  rom, MEMCELLSIZE
1997c 29 3b 0a 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20  );.  pTo->flags 
1997d 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 0a 20 20  &= ~MEM_Dyn;..  
1997e 69 66 28 20 70 54 6f 2d 3e 66 6c 61 67 73 26 28  if( pTo->flags&(
1997f 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62  MEM_Str|MEM_Blob
19980 29 20 29 7b 0a 20 20 20 20 69 66 28 20 30 3d 3d  ) ){.    if( 0==
19981 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45  (pFrom->flags&ME
19982 4d 5f 53 74 61 74 69 63 29 20 29 7b 0a 20 20 20  M_Static) ){.   
19983 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c 3d     pTo->flags |=
19984 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20   MEM_Ephem;.    
19985 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
19986 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62  beMemMakeWriteab
19987 6c 65 28 70 54 6f 29 3b 0a 20 20 20 20 7d 0a 20  le(pTo);.    }. 
19988 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
19989 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66  .}../*.** Transf
1998a 65 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  er the contents 
1998b 6f 66 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f 2e  of pFrom to pTo.
1998c 20 41 6e 79 20 65 78 69 73 74 69 6e 67 20 76 61   Any existing va
1998d 6c 75 65 20 69 6e 20 70 54 6f 20 69 73 0a 2a 2a  lue in pTo is.**
1998e 20 66 72 65 65 64 2e 20 49 66 20 70 46 72 6f 6d   freed. If pFrom
1998f 20 63 6f 6e 74 61 69 6e 73 20 65 70 68 65 6d 65   contains epheme
19990 72 61 6c 20 64 61 74 61 2c 20 61 20 63 6f 70 79  ral data, a copy
19991 20 69 73 20 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a 20   is made..**.** 
19992 70 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 20 61  pFrom contains a
19993 6e 20 53 51 4c 20 4e 55 4c 4c 20 77 68 65 6e 20  n SQL NULL when 
19994 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  this routine ret
19995 75 72 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  urns..*/.SQLITE_
19996 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
19997 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28  ite3VdbeMemMove(
19998 4d 65 6d 20 2a 70 54 6f 2c 20 4d 65 6d 20 2a 70  Mem *pTo, Mem *p
19999 46 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74 28  From){.  assert(
1999a 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c   pFrom->db==0 ||
1999b 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1999c 65 6c 64 28 70 46 72 6f 6d 2d 3e 64 62 2d 3e 6d  eld(pFrom->db->m
1999d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
1999e 74 28 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c  t( pTo->db==0 ||
1999f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
199a0 65 6c 64 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74  eld(pTo->db->mut
199a1 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
199a2 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c   pFrom->db==0 ||
199a3 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70   pTo->db==0 || p
199a4 46 72 6f 6d 2d 3e 64 62 3d 3d 70 54 6f 2d 3e 64  From->db==pTo->d
199a5 62 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 56  b );..  sqlite3V
199a6 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 54  dbeMemRelease(pT
199a7 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f  o);.  memcpy(pTo
199a8 2c 20 70 46 72 6f 6d 2c 20 73 69 7a 65 6f 66 28  , pFrom, sizeof(
199a9 4d 65 6d 29 29 3b 0a 20 20 70 46 72 6f 6d 2d 3e  Mem));.  pFrom->
199aa 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
199ab 3b 0a 20 20 70 46 72 6f 6d 2d 3e 78 44 65 6c 20  ;.  pFrom->xDel 
199ac 3d 20 30 3b 0a 20 20 70 46 72 6f 6d 2d 3e 7a 4d  = 0;.  pFrom->zM
199ad 61 6c 6c 6f 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  alloc = 0;.}../*
199ae 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76  .** Change the v
199af 61 6c 75 65 20 6f 66 20 61 20 4d 65 6d 20 74 6f  alue of a Mem to
199b0 20 62 65 20 61 20 73 74 72 69 6e 67 20 6f 72 20   be a string or 
199b1 61 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68  a BLOB..**.** Th
199b2 65 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d  e memory managem
199b3 65 6e 74 20 73 74 72 61 74 65 67 79 20 64 65 70  ent strategy dep
199b4 65 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75  ends on the valu
199b5 65 20 6f 66 20 74 68 65 20 78 44 65 6c 0a 2a 2a  e of the xDel.**
199b6 20 70 61 72 61 6d 65 74 65 72 2e 20 49 66 20 74   parameter. If t
199b7 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 64 20  he value passed 
199b8 69 73 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  is SQLITE_TRANSI
199b9 45 4e 54 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a  ENT, then the .*
199ba 2a 20 73 74 72 69 6e 67 20 69 73 20 63 6f 70 69  * string is copi
199bb 65 64 20 69 6e 74 6f 20 61 20 28 70 6f 73 73 69  ed into a (possi
199bc 62 6c 79 20 65 78 69 73 74 69 6e 67 29 20 62 75  bly existing) bu
199bd 66 66 65 72 20 6d 61 6e 61 67 65 64 20 62 79 20  ffer managed by 
199be 74 68 65 20 0a 2a 2a 20 4d 65 6d 20 73 74 72 75  the .** Mem stru
199bf 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69 73 65  cture. Otherwise
199c0 2c 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 62  , any existing b
199c1 75 66 66 65 72 20 69 73 20 66 72 65 65 64 20 61  uffer is freed a
199c2 6e 64 20 74 68 65 0a 2a 2a 20 70 6f 69 6e 74 65  nd the.** pointe
199c3 72 20 63 6f 70 69 65 64 2e 0a 2a 2a 0a 2a 2a 20  r copied..**.** 
199c4 49 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 73  If the string is
199c5 20 74 6f 6f 20 6c 61 72 67 65 20 28 69 66 20 69   too large (if i
199c6 74 20 65 78 63 65 65 64 73 20 74 68 65 20 53 51  t exceeds the SQ
199c7 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
199c8 48 0a 2a 2a 20 73 69 7a 65 20 6c 69 6d 69 74 29  H.** size limit)
199c9 20 74 68 65 6e 20 6e 6f 20 6d 65 6d 6f 72 79 20   then no memory 
199ca 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 63 63 75 72  allocation occur
199cb 73 2e 20 20 49 66 20 74 68 65 20 73 74 72 69 6e  s.  If the strin
199cc 67 20 63 61 6e 20 62 65 0a 2a 2a 20 73 74 6f 72  g can be.** stor
199cd 65 64 20 77 69 74 68 6f 75 74 20 61 6c 6c 6f 63  ed without alloc
199ce 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 74 68  ating memory, th
199cf 65 6e 20 69 74 20 69 73 2e 20 20 49 66 20 61 20  en it is.  If a 
199d0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
199d1 6e 0a 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64  n.** is required
199d2 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 74   to store the st
199d3 72 69 6e 67 2c 20 74 68 65 6e 20 76 61 6c 75 65  ring, then value
199d4 20 6f 66 20 70 4d 65 6d 20 69 73 20 75 6e 63 68   of pMem is unch
199d5 61 6e 67 65 64 2e 20 20 49 6e 0a 2a 2a 20 65 69  anged.  In.** ei
199d6 74 68 65 72 20 63 61 73 65 2c 20 53 51 4c 49 54  ther case, SQLIT
199d7 45 5f 54 4f 4f 42 49 47 20 69 73 20 72 65 74 75  E_TOOBIG is retu
199d8 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rned..*/.SQLITE_
199d9 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
199da 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72  te3VdbeMemSetStr
199db 28 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 20  (.  Mem *pMem,  
199dc 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72          /* Memor
199dd 79 20 63 65 6c 6c 20 74 6f 20 73 65 74 20 74 6f  y cell to set to
199de 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 2a 2f   string value */
199df 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
199e0 2c 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67  ,      /* String
199e1 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   pointer */.  in
199e2 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  t n,            
199e3 20 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 73 74    /* Bytes in st
199e4 72 69 6e 67 2c 20 6f 72 20 6e 65 67 61 74 69 76  ring, or negativ
199e5 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20  e */.  u8 enc,  
199e6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e             /* En
199e7 63 6f 64 69 6e 67 20 6f 66 20 7a 2e 20 20 30 20  coding of z.  0 
199e8 66 6f 72 20 42 4c 4f 42 73 20 2a 2f 0a 20 20 76  for BLOBs */.  v
199e9 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64  oid (*xDel)(void
199ea 2a 29 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72  *) /* Destructor
199eb 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a   function */.){.
199ec 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 6e 3b    int nByte = n;
199ed 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c        /* New val
199ee 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 6e 20 2a  ue for pMem->n *
199ef 2f 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b 20  /.  int iLimit; 
199f0 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d          /* Maxim
199f1 75 6d 20 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e  um allowed strin
199f2 67 20 6f 72 20 62 6c 6f 62 20 73 69 7a 65 20 2a  g or blob size *
199f3 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 20 3d 20  /.  u16 flags = 
199f4 30 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76  0;      /* New v
199f5 61 6c 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 66  alue for pMem->f
199f6 6c 61 67 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72  lags */..  asser
199f7 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c  t( pMem->db==0 |
199f8 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
199f9 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d  held(pMem->db->m
199fa 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
199fb 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  t( (pMem->flags 
199fc 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30  & MEM_RowSet)==0
199fd 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 7a 20 69   );..  /* If z i
199fe 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  s a NULL pointer
199ff 2c 20 73 65 74 20 70 4d 65 6d 20 74 6f 20 63 6f  , set pMem to co
19a00 6e 74 61 69 6e 20 61 6e 20 53 51 4c 20 4e 55 4c  ntain an SQL NUL
19a01 4c 2e 20 2a 2f 0a 20 20 69 66 28 20 21 7a 20 29  L. */.  if( !z )
19a02 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
19a03 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d 65 6d  eMemSetNull(pMem
19a04 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
19a05 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
19a06 69 66 28 20 70 4d 65 6d 2d 3e 64 62 20 29 7b 0a  if( pMem->db ){.
19a07 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 70 4d 65      iLimit = pMe
19a08 6d 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  m->db->aLimit[SQ
19a09 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
19a0a 48 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  H];.  }else{.   
19a0b 20 69 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 45   iLimit = SQLITE
19a0c 5f 4d 41 58 5f 4c 45 4e 47 54 48 3b 0a 20 20 7d  _MAX_LENGTH;.  }
19a0d 0a 20 20 66 6c 61 67 73 20 3d 20 28 65 6e 63 3d  .  flags = (enc=
19a0e 3d 30 3f 4d 45 4d 5f 42 6c 6f 62 3a 4d 45 4d 5f  =0?MEM_Blob:MEM_
19a0f 53 74 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 74  Str);.  if( nByt
19a10 65 3c 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  e<0 ){.    asser
19a11 74 28 20 65 6e 63 21 3d 30 20 29 3b 0a 20 20 20  t( enc!=0 );.   
19a12 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45   if( enc==SQLITE
19a13 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 20 20 66  _UTF8 ){.      f
19a14 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74  or(nByte=0; nByt
19a15 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20 7a 5b 6e  e<=iLimit && z[n
19a16 42 79 74 65 5d 3b 20 6e 42 79 74 65 2b 2b 29 7b  Byte]; nByte++){
19a17 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
19a18 20 20 20 66 6f 72 28 6e 42 79 74 65 3d 30 3b 20     for(nByte=0; 
19a19 6e 42 79 74 65 3c 3d 69 4c 69 6d 69 74 20 26 26  nByte<=iLimit &&
19a1a 20 28 7a 5b 6e 42 79 74 65 5d 20 7c 20 7a 5b 6e   (z[nByte] | z[n
19a1b 42 79 74 65 2b 31 5d 29 3b 20 6e 42 79 74 65 2b  Byte+1]); nByte+
19a1c 3d 32 29 7b 7d 0a 20 20 20 20 7d 0a 20 20 20 20  =2){}.    }.    
19a1d 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72  flags |= MEM_Ter
19a1e 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65  m;.  }..  /* The
19a1f 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b   following block
19a20 20 73 65 74 73 20 74 68 65 20 6e 65 77 20 76 61   sets the new va
19a21 6c 75 65 73 20 6f 66 20 4d 65 6d 2e 7a 20 61 6e  lues of Mem.z an
19a22 64 20 4d 65 6d 2e 78 44 65 6c 2e 20 49 74 0a 20  d Mem.xDel. It. 
19a23 20 2a 2a 20 61 6c 73 6f 20 73 65 74 73 20 61 20   ** also sets a 
19a24 66 6c 61 67 20 69 6e 20 6c 6f 63 61 6c 20 76 61  flag in local va
19a25 72 69 61 62 6c 65 20 22 66 6c 61 67 73 22 20 74  riable "flags" t
19a26 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 6d  o indicate the m
19a27 65 6d 6f 72 79 0a 20 20 2a 2a 20 6d 61 6e 61 67  emory.  ** manag
19a28 65 6d 65 6e 74 20 28 6f 6e 65 20 6f 66 20 4d 45  ement (one of ME
19a29 4d 5f 44 79 6e 20 6f 72 20 4d 45 4d 5f 53 74 61  M_Dyn or MEM_Sta
19a2a 74 69 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  tic)..  */.  if(
19a2b 20 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 54 52   xDel==SQLITE_TR
19a2c 41 4e 53 49 45 4e 54 20 29 7b 0a 20 20 20 20 69  ANSIENT ){.    i
19a2d 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 6e 42 79 74  nt nAlloc = nByt
19a2e 65 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73  e;.    if( flags
19a2f 26 4d 45 4d 5f 54 65 72 6d 20 29 7b 0a 20 20 20  &MEM_Term ){.   
19a30 20 20 20 6e 41 6c 6c 6f 63 20 2b 3d 20 28 65 6e     nAlloc += (en
19a31 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31  c==SQLITE_UTF8?1
19a32 3a 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  :2);.    }.    i
19a33 66 28 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20  f( nByte>iLimit 
19a34 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
19a35 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20  SQLITE_TOOBIG;. 
19a36 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
19a37 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
19a38 70 4d 65 6d 2c 20 6e 41 6c 6c 6f 63 2c 20 30 29  pMem, nAlloc, 0)
19a39 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
19a3a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
19a3b 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28     }.    memcpy(
19a3c 70 4d 65 6d 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c 6c  pMem->z, z, nAll
19a3d 6f 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  oc);.  }else if(
19a3e 20 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 44 59   xDel==SQLITE_DY
19a3f 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 73 71 6c  NAMIC ){.    sql
19a40 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
19a41 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d  se(pMem);.    pM
19a42 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d  em->zMalloc = pM
19a43 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a 29  em->z = (char *)
19a44 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 78 44 65  z;.    pMem->xDe
19a45 6c 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  l = 0;.  }else{.
19a46 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
19a47 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b  emRelease(pMem);
19a48 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28  .    pMem->z = (
19a49 63 68 61 72 20 2a 29 7a 3b 0a 20 20 20 20 70 4d  char *)z;.    pM
19a4a 65 6d 2d 3e 78 44 65 6c 20 3d 20 78 44 65 6c 3b  em->xDel = xDel;
19a4b 0a 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 28 28  .    flags |= ((
19a4c 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 53 54 41  xDel==SQLITE_STA
19a4d 54 49 43 29 3f 4d 45 4d 5f 53 74 61 74 69 63 3a  TIC)?MEM_Static:
19a4e 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20 7d 0a 0a 20  MEM_Dyn);.  }.. 
19a4f 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74 65   pMem->n = nByte
19a50 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  ;.  pMem->flags 
19a51 3d 20 66 6c 61 67 73 3b 0a 20 20 70 4d 65 6d 2d  = flags;.  pMem-
19a52 3e 65 6e 63 20 3d 20 28 65 6e 63 3d 3d 30 20 3f  >enc = (enc==0 ?
19a53 20 53 51 4c 49 54 45 5f 55 54 46 38 20 3a 20 65   SQLITE_UTF8 : e
19a54 6e 63 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70  nc);.  pMem->typ
19a55 65 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51  e = (enc==0 ? SQ
19a56 4c 49 54 45 5f 42 4c 4f 42 20 3a 20 53 51 4c 49  LITE_BLOB : SQLI
19a57 54 45 5f 54 45 58 54 29 3b 0a 0a 23 69 66 6e 64  TE_TEXT);..#ifnd
19a58 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
19a59 54 46 31 36 0a 20 20 69 66 28 20 70 4d 65 6d 2d  TF16.  if( pMem-
19a5a 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46  >enc!=SQLITE_UTF
19a5b 38 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65  8 && sqlite3Vdbe
19a5c 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d 65  MemHandleBom(pMe
19a5d 6d 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  m) ){.    return
19a5e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
19a5f 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28   }.#endif..  if(
19a60 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b   nByte>iLimit ){
19a61 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
19a62 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a  TE_TOOBIG;.  }..
19a63 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
19a64 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d  OK;.}../*.** Com
19a65 70 61 72 65 20 74 68 65 20 76 61 6c 75 65 73 20  pare the values 
19a66 63 6f 6e 74 61 69 6e 65 64 20 62 79 20 74 68 65  contained by the
19a67 20 74 77 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c   two memory cell
19a68 73 2c 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20  s, returning.** 
19a69 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 20 6f  negative, zero o
19a6a 72 20 70 6f 73 69 74 69 76 65 20 69 66 20 70 4d  r positive if pM
19a6b 65 6d 31 20 69 73 20 6c 65 73 73 20 74 68 61 6e  em1 is less than
19a6c 2c 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67  , equal to, or g
19a6d 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 70  reater.** than p
19a6e 4d 65 6d 32 2e 20 53 6f 72 74 69 6e 67 20 6f 72  Mem2. Sorting or
19a6f 64 65 72 20 69 73 20 4e 55 4c 4c 27 73 20 66 69  der is NULL's fi
19a70 72 73 74 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79  rst, followed by
19a71 20 6e 75 6d 62 65 72 73 20 28 69 6e 74 65 67 65   numbers (intege
19a72 72 73 0a 2a 2a 20 61 6e 64 20 72 65 61 6c 73 29  rs.** and reals)
19a73 20 73 6f 72 74 65 64 20 6e 75 6d 65 72 69 63 61   sorted numerica
19a74 6c 6c 79 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79  lly, followed by
19a75 20 74 65 78 74 20 6f 72 64 65 72 65 64 20 62 79   text ordered by
19a76 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a   the collating.*
19a77 2a 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c  * sequence pColl
19a78 20 61 6e 64 20 66 69 6e 61 6c 6c 79 20 62 6c 6f   and finally blo
19a79 62 27 73 20 6f 72 64 65 72 65 64 20 62 79 20 6d  b's ordered by m
19a7a 65 6d 63 6d 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 54  emcmp()..**.** T
19a7b 77 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 61  wo NULL values a
19a7c 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 65 71  re considered eq
19a7d 75 61 6c 20 62 79 20 74 68 69 73 20 66 75 6e 63  ual by this func
19a7e 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
19a7f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
19a80 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63 6f  te3MemCompare(co
19a81 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 31 2c 20  nst Mem *pMem1, 
19a82 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 32  const Mem *pMem2
19a83 2c 20 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 20  , const CollSeq 
19a84 2a 70 43 6f 6c 6c 29 7b 0a 20 20 69 6e 74 20 72  *pColl){.  int r
19a85 63 3b 0a 20 20 69 6e 74 20 66 31 2c 20 66 32 3b  c;.  int f1, f2;
19a86 0a 20 20 69 6e 74 20 63 6f 6d 62 69 6e 65 64 5f  .  int combined_
19a87 66 6c 61 67 73 3b 0a 0a 20 20 2f 2a 20 49 6e 74  flags;..  /* Int
19a88 65 72 63 68 61 6e 67 65 20 70 4d 65 6d 31 20 61  erchange pMem1 a
19a89 6e 64 20 70 4d 65 6d 32 20 69 66 20 74 68 65 20  nd pMem2 if the 
19a8a 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
19a8b 63 65 20 73 70 65 63 69 66 69 65 73 0a 20 20 2a  ce specifies.  *
19a8c 2a 20 44 45 53 43 20 6f 72 64 65 72 2e 0a 20 20  * DESC order..  
19a8d 2a 2f 0a 20 20 66 31 20 3d 20 70 4d 65 6d 31 2d  */.  f1 = pMem1-
19a8e 3e 66 6c 61 67 73 3b 0a 20 20 66 32 20 3d 20 70  >flags;.  f2 = p
19a8f 4d 65 6d 32 2d 3e 66 6c 61 67 73 3b 0a 20 20 63  Mem2->flags;.  c
19a90 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 20 3d 20  ombined_flags = 
19a91 66 31 7c 66 32 3b 0a 20 20 61 73 73 65 72 74 28  f1|f2;.  assert(
19a92 20 28 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73   (combined_flags
19a93 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d   & MEM_RowSet)==
19a94 30 20 29 3b 0a 20 0a 20 20 2f 2a 20 49 66 20 6f  0 );. .  /* If o
19a95 6e 65 20 76 61 6c 75 65 20 69 73 20 4e 55 4c 4c  ne value is NULL
19a96 2c 20 69 74 20 69 73 20 6c 65 73 73 20 74 68 61  , it is less tha
19a97 6e 20 74 68 65 20 6f 74 68 65 72 2e 20 49 66 20  n the other. If 
19a98 62 6f 74 68 20 76 61 6c 75 65 73 0a 20 20 2a 2a  both values.  **
19a99 20 61 72 65 20 4e 55 4c 4c 2c 20 72 65 74 75 72   are NULL, retur
19a9a 6e 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  n 0..  */.  if( 
19a9b 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 4d  combined_flags&M
19a9c 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72  EM_Null ){.    r
19a9d 65 74 75 72 6e 20 28 66 32 26 4d 45 4d 5f 4e 75  eturn (f2&MEM_Nu
19a9e 6c 6c 29 20 2d 20 28 66 31 26 4d 45 4d 5f 4e 75  ll) - (f1&MEM_Nu
19a9f 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  ll);.  }..  /* I
19aa0 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 61  f one value is a
19aa1 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 68 65 20   number and the 
19aa2 6f 74 68 65 72 20 69 73 20 6e 6f 74 2c 20 74 68  other is not, th
19aa3 65 20 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73  e number is less
19aa4 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68 20 61  ..  ** If both a
19aa5 72 65 20 6e 75 6d 62 65 72 73 2c 20 63 6f 6d 70  re numbers, comp
19aa6 61 72 65 20 61 73 20 72 65 61 6c 73 20 69 66 20  are as reals if 
19aa7 6f 6e 65 20 69 73 20 61 20 72 65 61 6c 2c 20 6f  one is a real, o
19aa8 72 20 61 73 20 69 6e 74 65 67 65 72 73 0a 20 20  r as integers.  
19aa9 2a 2a 20 69 66 20 62 6f 74 68 20 76 61 6c 75 65  ** if both value
19aaa 73 20 61 72 65 20 69 6e 74 65 67 65 72 73 2e 0a  s are integers..
19aab 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69    */.  if( combi
19aac 6e 65 64 5f 66 6c 61 67 73 26 28 4d 45 4d 5f 49  ned_flags&(MEM_I
19aad 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 29 7b 0a  nt|MEM_Real) ){.
19aae 20 20 20 20 69 66 28 20 21 28 66 31 26 28 4d 45      if( !(f1&(ME
19aaf 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29  M_Int|MEM_Real))
19ab0 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
19ab1 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   1;.    }.    if
19ab2 28 20 21 28 66 32 26 28 4d 45 4d 5f 49 6e 74 7c  ( !(f2&(MEM_Int|
19ab3 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a 20 20  MEM_Real)) ){.  
19ab4 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20      return -1;. 
19ab5 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66 31     }.    if( (f1
19ab6 20 26 20 66 32 20 26 20 4d 45 4d 5f 49 6e 74 29   & f2 & MEM_Int)
19ab7 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 6f 75  ==0 ){.      dou
19ab8 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 20 20  ble r1, r2;.    
19ab9 20 20 69 66 28 20 28 66 31 26 4d 45 4d 5f 52 65    if( (f1&MEM_Re
19aba 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  al)==0 ){.      
19abb 20 20 72 31 20 3d 20 28 64 6f 75 62 6c 65 29 70    r1 = (double)p
19abc 4d 65 6d 31 2d 3e 75 2e 69 3b 0a 20 20 20 20 20  Mem1->u.i;.     
19abd 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
19abe 72 31 20 3d 20 70 4d 65 6d 31 2d 3e 72 3b 0a 20  r1 = pMem1->r;. 
19abf 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
19ac0 20 28 66 32 26 4d 45 4d 5f 52 65 61 6c 29 3d 3d   (f2&MEM_Real)==
19ac1 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 32 20  0 ){.        r2 
19ac2 3d 20 28 64 6f 75 62 6c 65 29 70 4d 65 6d 32 2d  = (double)pMem2-
19ac3 3e 75 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c 73  >u.i;.      }els
19ac4 65 7b 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20  e{.        r2 = 
19ac5 70 4d 65 6d 32 2d 3e 72 3b 0a 20 20 20 20 20 20  pMem2->r;.      
19ac6 7d 0a 20 20 20 20 20 20 69 66 28 20 72 31 3c 72  }.      if( r1<r
19ac7 32 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20  2 ) return -1;. 
19ac8 20 20 20 20 20 69 66 28 20 72 31 3e 72 32 20 29       if( r1>r2 )
19ac9 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
19aca 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
19acb 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65  else{.      asse
19acc 72 74 28 20 66 31 26 4d 45 4d 5f 49 6e 74 20 29  rt( f1&MEM_Int )
19acd 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
19ace 66 32 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20  f2&MEM_Int );.  
19acf 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 75      if( pMem1->u
19ad0 2e 69 20 3c 20 70 4d 65 6d 32 2d 3e 75 2e 69 20  .i < pMem2->u.i 
19ad1 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20  ) return -1;.   
19ad2 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 75 2e     if( pMem1->u.
19ad3 69 20 3e 20 70 4d 65 6d 32 2d 3e 75 2e 69 20 29  i > pMem2->u.i )
19ad4 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
19ad5 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
19ad6 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e  .  }..  /* If on
19ad7 65 20 76 61 6c 75 65 20 69 73 20 61 20 73 74 72  e value is a str
19ad8 69 6e 67 20 61 6e 64 20 74 68 65 20 6f 74 68 65  ing and the othe
19ad9 72 20 69 73 20 61 20 62 6c 6f 62 2c 20 74 68 65  r is a blob, the
19ada 20 73 74 72 69 6e 67 20 69 73 20 6c 65 73 73 2e   string is less.
19adb 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68 20 61 72  .  ** If both ar
19adc 65 20 73 74 72 69 6e 67 73 2c 20 63 6f 6d 70 61  e strings, compa
19add 72 65 20 75 73 69 6e 67 20 74 68 65 20 63 6f 6c  re using the col
19ade 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73  lating functions
19adf 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d  ..  */.  if( com
19ae0 62 69 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d 5f  bined_flags&MEM_
19ae1 53 74 72 20 29 7b 0a 20 20 20 20 69 66 28 20 28  Str ){.    if( (
19ae2 66 31 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30  f1 & MEM_Str)==0
19ae3 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
19ae4 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   1;.    }.    if
19ae5 28 20 28 66 32 20 26 20 4d 45 4d 5f 53 74 72 29  ( (f2 & MEM_Str)
19ae6 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ==0 ){.      ret
19ae7 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 0a 20  urn -1;.    }.. 
19ae8 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 31     assert( pMem1
19ae9 2d 3e 65 6e 63 3d 3d 70 4d 65 6d 32 2d 3e 65 6e  ->enc==pMem2->en
19aea 63 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  c );.    assert(
19aeb 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c   pMem1->enc==SQL
19aec 49 54 45 5f 55 54 46 38 20 7c 7c 20 0a 20 20 20  ITE_UTF8 || .   
19aed 20 20 20 20 20 20 20 20 20 70 4d 65 6d 31 2d 3e           pMem1->
19aee 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31  enc==SQLITE_UTF1
19aef 36 4c 45 20 7c 7c 20 70 4d 65 6d 31 2d 3e 65 6e  6LE || pMem1->en
19af0 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42  c==SQLITE_UTF16B
19af1 45 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65  E );..    /* The
19af2 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
19af3 6e 63 65 20 6d 75 73 74 20 62 65 20 64 65 66 69  nce must be defi
19af4 6e 65 64 20 61 74 20 74 68 69 73 20 70 6f 69 6e  ned at this poin
19af5 74 2c 20 65 76 65 6e 20 69 66 0a 20 20 20 20 2a  t, even if.    *
19af6 2a 20 74 68 65 20 75 73 65 72 20 64 65 6c 65 74  * the user delet
19af7 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  es the collation
19af8 20 73 65 71 75 65 6e 63 65 20 61 66 74 65 72 20   sequence after 
19af9 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d  the vdbe program
19afa 20 69 73 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 69   is.    ** compi
19afb 6c 65 64 20 28 74 68 69 73 20 77 61 73 20 6e 6f  led (this was no
19afc 74 20 61 6c 77 61 79 73 20 74 68 65 20 63 61 73  t always the cas
19afd 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  e)..    */.    a
19afe 73 73 65 72 74 28 20 21 70 43 6f 6c 6c 20 7c 7c  ssert( !pColl ||
19aff 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 29 3b 0a   pColl->xCmp );.
19b00 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29  .    if( pColl )
19b01 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d  {.      if( pMem
19b02 31 2d 3e 65 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e 65  1->enc==pColl->e
19b03 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  nc ){.        /*
19b04 20 54 68 65 20 73 74 72 69 6e 67 73 20 61 72 65   The strings are
19b05 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
19b06 63 6f 72 72 65 63 74 20 65 6e 63 6f 64 69 6e 67  correct encoding
19b07 2e 20 20 43 61 6c 6c 20 74 68 65 0a 20 20 20 20  .  Call the.    
19b08 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f      ** compariso
19b09 6e 20 66 75 6e 63 74 69 6f 6e 20 64 69 72 65 63  n function direc
19b0a 74 6c 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 72  tly */.        r
19b0b 65 74 75 72 6e 20 70 43 6f 6c 6c 2d 3e 78 43 6d  eturn pColl->xCm
19b0c 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 70  p(pColl->pUser,p
19b0d 4d 65 6d 31 2d 3e 6e 2c 70 4d 65 6d 31 2d 3e 7a  Mem1->n,pMem1->z
19b0e 2c 70 4d 65 6d 32 2d 3e 6e 2c 70 4d 65 6d 32 2d  ,pMem2->n,pMem2-
19b0f 3e 7a 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  >z);.      }else
19b10 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20  {.        const 
19b11 76 6f 69 64 20 2a 76 31 2c 20 2a 76 32 3b 0a 20  void *v1, *v2;. 
19b12 20 20 20 20 20 20 20 69 6e 74 20 6e 31 2c 20 6e         int n1, n
19b13 32 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 63  2;.        Mem c
19b14 31 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 63  1;.        Mem c
19b15 32 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65  2;.        memse
19b16 74 28 26 63 31 2c 20 30 2c 20 73 69 7a 65 6f 66  t(&c1, 0, sizeof
19b17 28 63 31 29 29 3b 0a 20 20 20 20 20 20 20 20 6d  (c1));.        m
19b18 65 6d 73 65 74 28 26 63 32 2c 20 30 2c 20 73 69  emset(&c2, 0, si
19b19 7a 65 6f 66 28 63 32 29 29 3b 0a 20 20 20 20 20  zeof(c2));.     
19b1a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
19b1b 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 26 63 31  mShallowCopy(&c1
19b1c 2c 20 70 4d 65 6d 31 2c 20 4d 45 4d 5f 45 70 68  , pMem1, MEM_Eph
19b1d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  em);.        sql
19b1e 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c  ite3VdbeMemShall
19b1f 6f 77 43 6f 70 79 28 26 63 32 2c 20 70 4d 65 6d  owCopy(&c2, pMem
19b20 32 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20  2, MEM_Ephem);. 
19b21 20 20 20 20 20 20 20 76 31 20 3d 20 73 71 6c 69         v1 = sqli
19b22 74 65 33 56 61 6c 75 65 54 65 78 74 28 28 73 71  te3ValueText((sq
19b23 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 26 63 31  lite3_value*)&c1
19b24 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20  , pColl->enc);. 
19b25 20 20 20 20 20 20 20 6e 31 20 3d 20 76 31 3d 3d         n1 = v1==
19b26 30 20 3f 20 30 20 3a 20 63 31 2e 6e 3b 0a 20 20  0 ? 0 : c1.n;.  
19b27 20 20 20 20 20 20 76 32 20 3d 20 73 71 6c 69 74        v2 = sqlit
19b28 65 33 56 61 6c 75 65 54 65 78 74 28 28 73 71 6c  e3ValueText((sql
19b29 69 74 65 33 5f 76 61 6c 75 65 2a 29 26 63 32 2c  ite3_value*)&c2,
19b2a 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20   pColl->enc);.  
19b2b 20 20 20 20 20 20 6e 32 20 3d 20 76 32 3d 3d 30        n2 = v2==0
19b2c 20 3f 20 30 20 3a 20 63 32 2e 6e 3b 0a 20 20 20   ? 0 : c2.n;.   
19b2d 20 20 20 20 20 72 63 20 3d 20 70 43 6f 6c 6c 2d       rc = pColl-
19b2e 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73  >xCmp(pColl->pUs
19b2f 65 72 2c 20 6e 31 2c 20 76 31 2c 20 6e 32 2c 20  er, n1, v1, n2, 
19b30 76 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  v2);.        sql
19b31 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
19b32 73 65 28 26 63 31 29 3b 0a 20 20 20 20 20 20 20  se(&c1);.       
19b33 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
19b34 65 6c 65 61 73 65 28 26 63 32 29 3b 0a 20 20 20  elease(&c2);.   
19b35 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
19b36 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
19b37 20 20 2f 2a 20 49 66 20 61 20 4e 55 4c 4c 20 70    /* If a NULL p
19b38 6f 69 6e 74 65 72 20 77 61 73 20 70 61 73 73 65  ointer was passe
19b39 64 20 61 73 20 74 68 65 20 63 6f 6c 6c 61 74 65  d as the collate
19b3a 20 66 75 6e 63 74 69 6f 6e 2c 20 66 61 6c 6c 20   function, fall 
19b3b 74 68 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20 74  through.    ** t
19b3c 6f 20 74 68 65 20 62 6c 6f 62 20 63 61 73 65 20  o the blob case 
19b3d 61 6e 64 20 75 73 65 20 6d 65 6d 63 6d 70 28 29  and use memcmp()
19b3e 2e 20 20 2a 2f 0a 20 20 7d 0a 20 0a 20 20 2f 2a  .  */.  }. .  /*
19b3f 20 42 6f 74 68 20 76 61 6c 75 65 73 20 6d 75 73   Both values mus
19b40 74 20 62 65 20 62 6c 6f 62 73 2e 20 20 43 6f 6d  t be blobs.  Com
19b41 70 61 72 65 20 75 73 69 6e 67 20 6d 65 6d 63 6d  pare using memcm
19b42 70 28 29 2e 20 20 2a 2f 0a 20 20 72 63 20 3d 20  p().  */.  rc = 
19b43 6d 65 6d 63 6d 70 28 70 4d 65 6d 31 2d 3e 7a 2c  memcmp(pMem1->z,
19b44 20 70 4d 65 6d 32 2d 3e 7a 2c 20 28 70 4d 65 6d   pMem2->z, (pMem
19b45 31 2d 3e 6e 3e 70 4d 65 6d 32 2d 3e 6e 29 3f 70  1->n>pMem2->n)?p
19b46 4d 65 6d 32 2d 3e 6e 3a 70 4d 65 6d 31 2d 3e 6e  Mem2->n:pMem1->n
19b47 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29  );.  if( rc==0 )
19b48 7b 0a 20 20 20 20 72 63 20 3d 20 70 4d 65 6d 31  {.    rc = pMem1
19b49 2d 3e 6e 20 2d 20 70 4d 65 6d 32 2d 3e 6e 3b 0a  ->n - pMem2->n;.
19b4a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
19b4b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 64  .}../*.** Move d
19b4c 61 74 61 20 6f 75 74 20 6f 66 20 61 20 62 74 72  ata out of a btr
19b4d 65 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20 66  ee key or data f
19b4e 69 65 6c 64 20 61 6e 64 20 69 6e 74 6f 20 61 20  ield and into a 
19b4f 4d 65 6d 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  Mem structure..*
19b50 2a 20 54 68 65 20 64 61 74 61 20 6f 72 20 6b 65  * The data or ke
19b51 79 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20  y is taken from 
19b52 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 70  the entry that p
19b53 43 75 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79  Cur is currently
19b54 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 74 6f 2e   pointing.** to.
19b55 20 20 6f 66 66 73 65 74 20 61 6e 64 20 61 6d 74    offset and amt
19b56 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20   determine what 
19b57 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 64  portion of the d
19b58 61 74 61 20 6f 72 20 6b 65 79 20 74 6f 20 72 65  ata or key to re
19b59 74 72 69 65 76 65 2e 0a 2a 2a 20 6b 65 79 20 69  trieve..** key i
19b5a 73 20 74 72 75 65 20 74 6f 20 67 65 74 20 74 68  s true to get th
19b5b 65 20 6b 65 79 20 6f 72 20 66 61 6c 73 65 20 74  e key or false t
19b5c 6f 20 67 65 74 20 64 61 74 61 2e 20 20 54 68 65  o get data.  The
19b5d 20 72 65 73 75 6c 74 20 69 73 20 77 72 69 74 74   result is writt
19b5e 65 6e 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 70  en.** into the p
19b5f 4d 65 6d 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2a 0a  Mem element..**.
19b60 2a 2a 20 54 68 65 20 70 4d 65 6d 20 73 74 72 75  ** The pMem stru
19b61 63 74 75 72 65 20 69 73 20 61 73 73 75 6d 65 64  cture is assumed
19b62 20 74 6f 20 62 65 20 75 6e 69 6e 69 74 69 61 6c   to be uninitial
19b63 69 7a 65 64 2e 20 20 41 6e 79 20 70 72 69 6f 72  ized.  Any prior
19b64 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 73 20 6f   content.** is o
19b65 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 6f  verwritten witho
19b66 75 74 20 62 65 69 6e 67 20 66 72 65 65 64 2e 0a  ut being freed..
19b67 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 72 6f  **.** If this ro
19b68 75 74 69 6e 65 20 66 61 69 6c 73 20 66 6f 72 20  utine fails for 
19b69 61 6e 79 20 72 65 61 73 6f 6e 20 28 6d 61 6c 6c  any reason (mall
19b6a 6f 63 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20  oc returns NULL 
19b6b 6f 72 20 75 6e 61 62 6c 65 0a 2a 2a 20 74 6f 20  or unable.** to 
19b6c 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 69  read from the di
19b6d 73 6b 29 20 74 68 65 6e 20 74 68 65 20 70 4d 65  sk) then the pMe
19b6e 6d 20 69 73 20 6c 65 66 74 20 69 6e 20 61 6e 20  m is left in an 
19b6f 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61  inconsistent sta
19b70 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  te..*/.SQLITE_PR
19b71 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
19b72 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65  3VdbeMemFromBtre
19b73 65 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  e(.  BtCursor *p
19b74 43 75 72 2c 20 20 20 2f 2a 20 43 75 72 73 6f 72  Cur,   /* Cursor
19b75 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 72 65 63   pointing at rec
19b76 6f 72 64 20 74 6f 20 72 65 74 72 69 65 76 65 2e  ord to retrieve.
19b77 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74   */.  int offset
19b78 2c 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65  ,       /* Offse
19b79 74 20 66 72 6f 6d 20 74 68 65 20 73 74 61 72 74  t from the start
19b7a 20 6f 66 20 64 61 74 61 20 74 6f 20 72 65 74 75   of data to retu
19b7b 72 6e 20 62 79 74 65 73 20 66 72 6f 6d 2e 20 2a  rn bytes from. *
19b7c 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20  /.  int amt,    
19b7d 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
19b7e 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 74 75  of bytes to retu
19b7f 72 6e 2e 20 2a 2f 0a 20 20 69 6e 74 20 6b 65 79  rn. */.  int key
19b80 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66  ,          /* If
19b81 20 74 72 75 65 2c 20 72 65 74 72 69 65 76 65 20   true, retrieve 
19b82 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65 20 6b  from the btree k
19b83 65 79 2c 20 6e 6f 74 20 64 61 74 61 2e 20 2a 2f  ey, not data. */
19b84 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 20 20 20  .  Mem *pMem    
19b85 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74       /* OUT: Ret
19b86 75 72 6e 20 64 61 74 61 20 69 6e 20 74 68 69 73  urn data in this
19b87 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 2e 20   Mem structure. 
19b88 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 44  */.){.  char *zD
19b89 61 74 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 44  ata;        /* D
19b8a 61 74 61 20 66 72 6f 6d 20 74 68 65 20 62 74 72  ata from the btr
19b8b 65 65 20 6c 61 79 65 72 20 2a 2f 0a 20 20 69 6e  ee layer */.  in
19b8c 74 20 61 76 61 69 6c 61 62 6c 65 20 3d 20 30 3b  t available = 0;
19b8d 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
19b8e 79 74 65 73 20 61 76 61 69 6c 61 62 6c 65 20 6f  ytes available o
19b8f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 62 74 72 65  n the local btre
19b90 65 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  e page */.  int 
19b91 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
19b92 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
19b93 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  /..  assert( sql
19b94 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49  ite3BtreeCursorI
19b95 73 56 61 6c 69 64 28 70 43 75 72 29 20 29 3b 0a  sValid(pCur) );.
19b96 0a 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 68 65 20  .  /* Note: the 
19b97 63 61 6c 6c 73 20 74 6f 20 42 74 72 65 65 4b 65  calls to BtreeKe
19b98 79 46 65 74 63 68 28 29 20 61 6e 64 20 44 61 74  yFetch() and Dat
19b99 61 46 65 74 63 68 28 29 20 62 65 6c 6f 77 20 61  aFetch() below a
19b9a 73 73 65 72 74 28 29 20 0a 20 20 2a 2a 20 74 68  ssert() .  ** th
19b9b 61 74 20 62 6f 74 68 20 74 68 65 20 42 74 53 68  at both the BtSh
19b9c 61 72 65 64 20 61 6e 64 20 64 61 74 61 62 61 73  ared and databas
19b9d 65 20 68 61 6e 64 6c 65 20 6d 75 74 65 78 65 73  e handle mutexes
19b9e 20 61 72 65 20 68 65 6c 64 2e 20 2a 2f 0a 20 20   are held. */.  
19b9f 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66  assert( (pMem->f
19ba0 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65  lags & MEM_RowSe
19ba1 74 29 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 6b  t)==0 );.  if( k
19ba2 65 79 20 29 7b 0a 20 20 20 20 7a 44 61 74 61 20  ey ){.    zData 
19ba3 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65  = (char *)sqlite
19ba4 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 70  3BtreeKeyFetch(p
19ba5 43 75 72 2c 20 26 61 76 61 69 6c 61 62 6c 65 29  Cur, &available)
19ba6 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a  ;.  }else{.    z
19ba7 44 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29 73  Data = (char *)s
19ba8 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 46  qlite3BtreeDataF
19ba9 65 74 63 68 28 70 43 75 72 2c 20 26 61 76 61 69  etch(pCur, &avai
19baa 6c 61 62 6c 65 29 3b 0a 20 20 7d 0a 20 20 61 73  lable);.  }.  as
19bab 73 65 72 74 28 20 7a 44 61 74 61 21 3d 30 20 29  sert( zData!=0 )
19bac 3b 0a 0a 20 20 69 66 28 20 6f 66 66 73 65 74 2b  ;..  if( offset+
19bad 61 6d 74 3c 3d 61 76 61 69 6c 61 62 6c 65 20 26  amt<=available &
19bae 26 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d  & (pMem->flags&M
19baf 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 7b 0a 20 20  EM_Dyn)==0 ){.  
19bb0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
19bb1 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20  Release(pMem);. 
19bb2 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 26 7a 44     pMem->z = &zD
19bb3 61 74 61 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 20  ata[offset];.   
19bb4 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
19bb5 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 45 70 68 65  EM_Blob|MEM_Ephe
19bb6 6d 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 53  m;.  }else if( S
19bb7 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20  QLITE_OK==(rc = 
19bb8 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
19bb9 6f 77 28 70 4d 65 6d 2c 20 61 6d 74 2b 32 2c 20  ow(pMem, amt+2, 
19bba 30 29 29 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d  0)) ){.    pMem-
19bbb 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f  >flags = MEM_Blo
19bbc 62 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 54 65  b|MEM_Dyn|MEM_Te
19bbd 72 6d 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e  rm;.    pMem->en
19bbe 63 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d  c = 0;.    pMem-
19bbf 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 42  >type = SQLITE_B
19bc0 4c 4f 42 3b 0a 20 20 20 20 69 66 28 20 6b 65 79  LOB;.    if( key
19bc1 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
19bc2 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28 70  qlite3BtreeKey(p
19bc3 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74  Cur, offset, amt
19bc4 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20  , pMem->z);.    
19bc5 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
19bc6 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61  = sqlite3BtreeDa
19bc7 74 61 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c  ta(pCur, offset,
19bc8 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a   amt, pMem->z);.
19bc9 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e      }.    pMem->
19bca 7a 5b 61 6d 74 5d 20 3d 20 30 3b 0a 20 20 20 20  z[amt] = 0;.    
19bcb 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 2b 31 5d 20 3d  pMem->z[amt+1] =
19bcc 20 30 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d   0;.    if( rc!=
19bcd 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
19bce 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
19bcf 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a  mRelease(pMem);.
19bd0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 4d 65 6d      }.  }.  pMem
19bd1 2d 3e 6e 20 3d 20 61 6d 74 3b 0a 0a 20 20 72 65  ->n = amt;..  re
19bd2 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54  turn rc;.}../* T
19bd3 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
19bd4 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69  only available i
19bd5 6e 74 65 72 6e 61 6c 6c 79 2c 20 69 74 20 69 73  nternally, it is
19bd6 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65   not part of the
19bd7 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 41 50 49  .** external API
19bd8 2e 20 49 74 20 77 6f 72 6b 73 20 69 6e 20 61 20  . It works in a 
19bd9 73 69 6d 69 6c 61 72 20 77 61 79 20 74 6f 20 73  similar way to s
19bda 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
19bdb 74 28 29 2c 0a 2a 2a 20 65 78 63 65 70 74 20 74  t(),.** except t
19bdc 68 65 20 64 61 74 61 20 72 65 74 75 72 6e 65 64  he data returned
19bdd 20 69 73 20 69 6e 20 74 68 65 20 65 6e 63 6f 64   is in the encod
19bde 69 6e 67 20 73 70 65 63 69 66 69 65 64 20 62 79  ing specified by
19bdf 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 70   the second.** p
19be0 61 72 61 6d 65 74 65 72 2c 20 77 68 69 63 68 20  arameter, which 
19be1 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20 53  must be one of S
19be2 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53  QLITE_UTF16BE, S
19be3 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72  QLITE_UTF16LE or
19be4 0a 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 38 2e  .** SQLITE_UTF8.
19be5 0a 2a 2a 0a 2a 2a 20 28 32 30 30 36 2d 30 32 2d  .**.** (2006-02-
19be6 31 36 3a 29 20 20 54 68 65 20 65 6e 63 20 76 61  16:)  The enc va
19be7 6c 75 65 20 63 61 6e 20 62 65 20 6f 72 2d 65 64  lue can be or-ed
19be8 20 77 69 74 68 20 53 51 4c 49 54 45 5f 55 54 46   with SQLITE_UTF
19be9 31 36 5f 41 4c 49 47 4e 45 44 2e 0a 2a 2a 20 49  16_ALIGNED..** I
19bea 66 20 74 68 61 74 20 69 73 20 74 68 65 20 63 61  f that is the ca
19beb 73 65 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73  se, then the res
19bec 75 6c 74 20 6d 75 73 74 20 62 65 20 61 6c 69 67  ult must be alig
19bed 6e 65 64 20 6f 6e 20 61 6e 20 65 76 65 6e 20 62  ned on an even b
19bee 79 74 65 0a 2a 2a 20 62 6f 75 6e 64 61 72 79 2e  yte.** boundary.
19bef 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19bf0 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  TE const void *s
19bf1 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28  qlite3ValueText(
19bf2 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 70  sqlite3_value* p
19bf3 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20  Val, u8 enc){.  
19bf4 69 66 28 20 21 70 56 61 6c 20 29 20 72 65 74 75  if( !pVal ) retu
19bf5 72 6e 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28  rn 0;..  assert(
19bf6 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 20 7c 7c 20   pVal->db==0 || 
19bf7 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
19bf8 6c 64 28 70 56 61 6c 2d 3e 64 62 2d 3e 6d 75 74  ld(pVal->db->mut
19bf9 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
19bfa 20 28 65 6e 63 26 33 29 3d 3d 28 65 6e 63 26 7e   (enc&3)==(enc&~
19bfb 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49  SQLITE_UTF16_ALI
19bfc 47 4e 45 44 29 20 29 3b 0a 20 20 61 73 73 65 72  GNED) );.  asser
19bfd 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20  t( (pVal->flags 
19bfe 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30  & MEM_RowSet)==0
19bff 20 29 3b 0a 0a 20 20 69 66 28 20 70 56 61 6c 2d   );..  if( pVal-
19c00 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20  >flags&MEM_Null 
19c01 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
19c02 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28  .  }.  assert( (
19c03 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20 3d 3d 20  MEM_Blob>>3) == 
19c04 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20 70 56 61  MEM_Str );.  pVa
19c05 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 56 61  l->flags |= (pVa
19c06 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42  l->flags & MEM_B
19c07 6c 6f 62 29 3e 3e 33 3b 0a 20 20 65 78 70 61 6e  lob)>>3;.  expan
19c08 64 42 6c 6f 62 28 70 56 61 6c 29 3b 0a 20 20 69  dBlob(pVal);.  i
19c09 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d  f( pVal->flags&M
19c0a 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 73 71  EM_Str ){.    sq
19c0b 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
19c0c 6e 63 6f 64 69 6e 67 28 70 56 61 6c 2c 20 65 6e  ncoding(pVal, en
19c0d 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31  c & ~SQLITE_UTF1
19c0e 36 5f 41 4c 49 47 4e 45 44 29 3b 0a 20 20 20 20  6_ALIGNED);.    
19c0f 69 66 28 20 28 65 6e 63 20 26 20 53 51 4c 49 54  if( (enc & SQLIT
19c10 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29  E_UTF16_ALIGNED)
19c11 21 3d 30 20 26 26 20 31 3d 3d 28 31 26 53 51 4c  !=0 && 1==(1&SQL
19c12 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70  ITE_PTR_TO_INT(p
19c13 56 61 6c 2d 3e 7a 29 29 20 29 7b 0a 20 20 20 20  Val->z)) ){.    
19c14 20 20 61 73 73 65 72 74 28 20 28 70 56 61 6c 2d    assert( (pVal-
19c15 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 45 70  >flags & (MEM_Ep
19c16 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29 29  hem|MEM_Static))
19c17 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28  !=0 );.      if(
19c18 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d   sqlite3VdbeMemM
19c19 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 56 61  akeWriteable(pVa
19c1a 6c 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  l)!=SQLITE_OK ){
19c1b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
19c1c 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  0;.      }.    }
19c1d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
19c1e 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28  MemNulTerminate(
19c1f 70 56 61 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  pVal);.  }else{.
19c20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 56 61      assert( (pVa
19c21 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f  l->flags&MEM_Blo
19c22 62 29 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c  b)==0 );.    sql
19c23 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e  ite3VdbeMemStrin
19c24 67 69 66 79 28 70 56 61 6c 2c 20 65 6e 63 29 3b  gify(pVal, enc);
19c25 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 3d 3d  .    assert( 0==
19c26 28 31 26 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f  (1&SQLITE_PTR_TO
19c27 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 20 29  _INT(pVal->z)) )
19c28 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 70  ;.  }.  assert(p
19c29 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20 26  Val->enc==(enc &
19c2a 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41   ~SQLITE_UTF16_A
19c2b 4c 49 47 4e 45 44 29 20 7c 7c 20 70 56 61 6c 2d  LIGNED) || pVal-
19c2c 3e 64 62 3d 3d 30 0a 20 20 20 20 20 20 20 20 20  >db==0.         
19c2d 20 20 20 20 20 7c 7c 20 70 56 61 6c 2d 3e 64 62       || pVal->db
19c2e 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
19c2f 3b 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 65 6e  ;.  if( pVal->en
19c30 63 3d 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49 54  c==(enc & ~SQLIT
19c31 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29  E_UTF16_ALIGNED)
19c32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70   ){.    return p
19c33 56 61 6c 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65 7b  Val->z;.  }else{
19c34 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
19c35 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61   }.}../*.** Crea
19c36 74 65 20 61 20 6e 65 77 20 73 71 6c 69 74 65 33  te a new sqlite3
19c37 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2e 0a 2a  _value object..*
19c38 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
19c39 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
19c3a 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28  sqlite3ValueNew(
19c3b 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
19c3c 4d 65 6d 20 2a 70 20 3d 20 73 71 6c 69 74 65 33  Mem *p = sqlite3
19c3d 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
19c3e 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20   sizeof(*p));.  
19c3f 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e  if( p ){.    p->
19c40 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
19c41 3b 0a 20 20 20 20 70 2d 3e 74 79 70 65 20 3d 20  ;.    p->type = 
19c42 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 20  SQLITE_NULL;.   
19c43 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 7d   p->db = db;.  }
19c44 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
19c45 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e  /*.** Create a n
19c46 65 77 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ew sqlite3_value
19c47 20 6f 62 6a 65 63 74 2c 20 63 6f 6e 74 61 69 6e   object, contain
19c48 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f 66  ing the value of
19c49 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20 54 68   pExpr..**.** Th
19c4a 69 73 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 6f  is only works fo
19c4b 72 20 76 65 72 79 20 73 69 6d 70 6c 65 20 65 78  r very simple ex
19c4c 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 20 63  pressions that c
19c4d 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20 63 6f  onsist of one co
19c4e 6e 73 74 61 6e 74 0a 2a 2a 20 74 6f 6b 65 6e 20  nstant.** token 
19c4f 28 69 2e 65 2e 20 22 35 22 2c 20 22 35 2e 31 22  (i.e. "5", "5.1"
19c50 2c 20 22 27 61 20 73 74 72 69 6e 67 27 22 29 2e  , "'a string'").
19c51 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69   If the expressi
19c52 6f 6e 20 63 61 6e 0a 2a 2a 20 62 65 20 63 6f 6e  on can.** be con
19c53 76 65 72 74 65 64 20 64 69 72 65 63 74 6c 79 20  verted directly 
19c54 69 6e 74 6f 20 61 20 76 61 6c 75 65 2c 20 74 68  into a value, th
19c55 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73 20  en the value is 
19c56 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 0a 2a 2a  allocated and.**
19c57 20 61 20 70 6f 69 6e 74 65 72 20 77 72 69 74 74   a pointer writt
19c58 65 6e 20 74 6f 20 2a 70 70 56 61 6c 2e 20 54 68  en to *ppVal. Th
19c59 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70  e caller is resp
19c5a 6f 6e 73 69 62 6c 65 20 66 6f 72 20 64 65 61 6c  onsible for deal
19c5b 6c 6f 63 61 74 69 6e 67 0a 2a 2a 20 74 68 65 20  locating.** the 
19c5c 76 61 6c 75 65 20 62 79 20 70 61 73 73 69 6e 67  value by passing
19c5d 20 69 74 20 74 6f 20 73 71 6c 69 74 65 33 56 61   it to sqlite3Va
19c5e 6c 75 65 46 72 65 65 28 29 20 6c 61 74 65 72 20  lueFree() later 
19c5f 6f 6e 2e 20 49 66 20 74 68 65 20 65 78 70 72 65  on. If the expre
19c60 73 73 69 6f 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20  ssion.** cannot 
19c61 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20  be converted to 
19c62 61 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a 70  a value, then *p
19c63 70 56 61 6c 20 69 73 20 73 65 74 20 74 6f 20 4e  pVal is set to N
19c64 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ULL..*/.SQLITE_P
19c65 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19c66 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28  e3ValueFromExpr(
19c67 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
19c68 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19c69 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
19c6a 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70  nection */.  Exp
19c6b 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20  r *pExpr,       
19c6c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78         /* The ex
19c6d 70 72 65 73 73 69 6f 6e 20 74 6f 20 65 76 61 6c  pression to eval
19c6e 75 61 74 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63  uate */.  u8 enc
19c6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
19c70 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20      /* Encoding 
19c71 74 6f 20 75 73 65 20 2a 2f 0a 20 20 75 38 20 61  to use */.  u8 a
19c72 66 66 69 6e 69 74 79 2c 20 20 20 20 20 20 20 20  ffinity,        
19c73 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74        /* Affinit
19c74 79 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 73 71  y to use */.  sq
19c75 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 70  lite3_value **pp
19c76 56 61 6c 20 20 20 20 20 2f 2a 20 57 72 69 74 65  Val     /* Write
19c77 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 20 68   the new value h
19c78 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ere */.){.  int 
19c79 6f 70 3b 0a 20 20 63 68 61 72 20 2a 7a 56 61 6c  op;.  char *zVal
19c7a 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f   = 0;.  sqlite3_
19c7b 76 61 6c 75 65 20 2a 70 56 61 6c 20 3d 20 30 3b  value *pVal = 0;
19c7c 0a 0a 20 20 69 66 28 20 21 70 45 78 70 72 20 29  ..  if( !pExpr )
19c7d 7b 0a 20 20 20 20 2a 70 70 56 61 6c 20 3d 20 30  {.    *ppVal = 0
19c7e 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
19c7f 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 6f 70  ITE_OK;.  }.  op
19c80 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20   = pExpr->op;.  
19c81 69 66 28 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53  if( op==TK_REGIS
19c82 54 45 52 20 29 7b 0a 20 20 20 20 6f 70 20 3d 20  TER ){.    op = 
19c83 70 45 78 70 72 2d 3e 6f 70 32 3b 0a 20 20 7d 0a  pExpr->op2;.  }.
19c84 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 54  .  if( op==TK_ST
19c85 52 49 4e 47 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 46  RING || op==TK_F
19c86 4c 4f 41 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49  LOAT || op==TK_I
19c87 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 70 56  NTEGER ){.    pV
19c88 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75  al = sqlite3Valu
19c89 65 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 69 66  eNew(db);.    if
19c8a 28 20 70 56 61 6c 3d 3d 30 20 29 20 67 6f 74 6f  ( pVal==0 ) goto
19c8b 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 69 66 28   no_mem;.    if(
19c8c 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
19c8d 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61  (pExpr, EP_IntVa
19c8e 6c 75 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71  lue) ){.      sq
19c8f 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49  lite3VdbeMemSetI
19c90 6e 74 36 34 28 70 56 61 6c 2c 20 28 69 36 34 29  nt64(pVal, (i64)
19c91 70 45 78 70 72 2d 3e 75 2e 69 56 61 6c 75 65 29  pExpr->u.iValue)
19c92 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
19c93 20 20 20 7a 56 61 6c 20 3d 20 73 71 6c 69 74 65     zVal = sqlite
19c94 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 45  3DbStrDup(db, pE
19c95 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a  xpr->u.zToken);.
19c96 20 20 20 20 20 20 69 66 28 20 7a 56 61 6c 3d 3d        if( zVal==
19c97 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b  0 ) goto no_mem;
19c98 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61  .      sqlite3Va
19c99 6c 75 65 53 65 74 53 74 72 28 70 56 61 6c 2c 20  lueSetStr(pVal, 
19c9a 2d 31 2c 20 7a 56 61 6c 2c 20 53 51 4c 49 54 45  -1, zVal, SQLITE
19c9b 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 44 59  _UTF8, SQLITE_DY
19c9c 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 69 66  NAMIC);.      if
19c9d 28 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29  ( op==TK_FLOAT )
19c9e 20 70 56 61 6c 2d 3e 74 79 70 65 20 3d 20 53 51   pVal->type = SQ
19c9f 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 20 20  LITE_FLOAT;.    
19ca0 7d 0a 20 20 20 20 69 66 28 20 28 6f 70 3d 3d 54  }.    if( (op==T
19ca1 4b 5f 49 4e 54 45 47 45 52 20 7c 7c 20 6f 70 3d  K_INTEGER || op=
19ca2 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 26 26 20 61  =TK_FLOAT ) && a
19ca3 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f  ffinity==SQLITE_
19ca4 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20  AFF_NONE ){.    
19ca5 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70    sqlite3ValueAp
19ca6 70 6c 79 41 66 66 69 6e 69 74 79 28 70 56 61 6c  plyAffinity(pVal
19ca7 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d  , SQLITE_AFF_NUM
19ca8 45 52 49 43 2c 20 53 51 4c 49 54 45 5f 55 54 46  ERIC, SQLITE_UTF
19ca9 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  8);.    }else{. 
19caa 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75       sqlite3Valu
19cab 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70  eApplyAffinity(p
19cac 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c 20 53  Val, affinity, S
19cad 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 20  QLITE_UTF8);.   
19cae 20 7d 0a 20 20 20 20 69 66 28 20 65 6e 63 21 3d   }.    if( enc!=
19caf 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20  SQLITE_UTF8 ){. 
19cb0 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
19cb1 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70  ChangeEncoding(p
19cb2 56 61 6c 2c 20 65 6e 63 29 3b 0a 20 20 20 20 7d  Val, enc);.    }
19cb3 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d  .  }else if( op=
19cb4 3d 54 4b 5f 55 4d 49 4e 55 53 20 29 20 7b 0a 20  =TK_UMINUS ) {. 
19cb5 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b     if( SQLITE_OK
19cb6 3d 3d 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72  ==sqlite3ValueFr
19cb7 6f 6d 45 78 70 72 28 64 62 2c 70 45 78 70 72 2d  omExpr(db,pExpr-
19cb8 3e 70 4c 65 66 74 2c 65 6e 63 2c 61 66 66 69 6e  >pLeft,enc,affin
19cb9 69 74 79 2c 26 70 56 61 6c 29 20 29 7b 0a 20 20  ity,&pVal) ){.  
19cba 20 20 20 20 70 56 61 6c 2d 3e 75 2e 69 20 3d 20      pVal->u.i = 
19cbb 2d 31 20 2a 20 70 56 61 6c 2d 3e 75 2e 69 3b 0a  -1 * pVal->u.i;.
19cbc 20 20 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65        /* (double
19cbd 29 2d 31 20 49 6e 20 63 61 73 65 20 6f 66 20 53  )-1 In case of S
19cbe 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
19cbf 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a  ING_POINT... */.
19cc0 20 20 20 20 20 20 70 56 61 6c 2d 3e 72 20 3d 20        pVal->r = 
19cc1 28 64 6f 75 62 6c 65 29 2d 31 20 2a 20 70 56 61  (double)-1 * pVa
19cc2 6c 2d 3e 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  l->r;.    }.  }.
19cc3 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
19cc4 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c  MIT_BLOB_LITERAL
19cc5 0a 20 20 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d  .  else if( op==
19cc6 54 4b 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 69  TK_BLOB ){.    i
19cc7 6e 74 20 6e 56 61 6c 3b 0a 20 20 20 20 61 73 73  nt nVal;.    ass
19cc8 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54  ert( pExpr->u.zT
19cc9 6f 6b 65 6e 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20  oken[0]=='x' || 
19cca 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b  pExpr->u.zToken[
19ccb 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20 20 61  0]=='X' );.    a
19ccc 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e  ssert( pExpr->u.
19ccd 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d 27 5c 27 27 20  zToken[1]=='\'' 
19cce 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20 73 71  );.    pVal = sq
19ccf 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64 62  lite3ValueNew(db
19cd0 29 3b 0a 20 20 20 20 69 66 28 20 21 70 56 61 6c  );.    if( !pVal
19cd1 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
19cd2 20 20 20 20 7a 56 61 6c 20 3d 20 26 70 45 78 70      zVal = &pExp
19cd3 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 32 5d 3b 0a  r->u.zToken[2];.
19cd4 20 20 20 20 6e 56 61 6c 20 3d 20 73 71 6c 69 74      nVal = sqlit
19cd5 65 33 53 74 72 6c 65 6e 33 30 28 7a 56 61 6c 29  e3Strlen30(zVal)
19cd6 2d 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  -1;.    assert( 
19cd7 7a 56 61 6c 5b 6e 56 61 6c 5d 3d 3d 27 5c 27 27  zVal[nVal]=='\''
19cd8 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   );.    sqlite3V
19cd9 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 56 61  dbeMemSetStr(pVa
19cda 6c 2c 20 73 71 6c 69 74 65 33 48 65 78 54 6f 42  l, sqlite3HexToB
19cdb 6c 6f 62 28 64 62 2c 20 7a 56 61 6c 2c 20 6e 56  lob(db, zVal, nV
19cdc 61 6c 29 2c 20 6e 56 61 6c 2f 32 2c 0a 20 20 20  al), nVal/2,.   
19cdd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19cde 20 20 20 20 20 20 30 2c 20 53 51 4c 49 54 45 5f        0, SQLITE_
19cdf 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 23 65  DYNAMIC);.  }.#e
19ce0 6e 64 69 66 0a 0a 20 20 2a 70 70 56 61 6c 20 3d  ndif..  *ppVal =
19ce1 20 70 56 61 6c 3b 0a 20 20 72 65 74 75 72 6e 20   pVal;.  return 
19ce2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 6e 6f 5f 6d  SQLITE_OK;..no_m
19ce3 65 6d 3a 0a 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63  em:.  db->malloc
19ce4 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 73 71  Failed = 1;.  sq
19ce5 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
19ce6 7a 56 61 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33  zVal);.  sqlite3
19ce7 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29 3b  ValueFree(pVal);
19ce8 0a 20 20 2a 70 70 56 61 6c 20 3d 20 30 3b 0a 20  .  *ppVal = 0;. 
19ce9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
19cea 4f 4d 45 4d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  OMEM;.}../*.** C
19ceb 68 61 6e 67 65 20 74 68 65 20 73 74 72 69 6e 67  hange the string
19cec 20 76 61 6c 75 65 20 6f 66 20 61 6e 20 73 71 6c   value of an sql
19ced 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63  ite3_value objec
19cee 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  t.*/.SQLITE_PRIV
19cef 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
19cf0 56 61 6c 75 65 53 65 74 53 74 72 28 0a 20 20 73  ValueSetStr(.  s
19cf1 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 76 2c  qlite3_value *v,
19cf2 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f       /* Value to
19cf3 20 62 65 20 73 65 74 20 2a 2f 0a 20 20 69 6e 74   be set */.  int
19cf4 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   n,             
19cf5 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
19cf6 73 74 72 69 6e 67 20 7a 20 2a 2f 0a 20 20 63 6f  string z */.  co
19cf7 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 20 20 20  nst void *z,    
19cf8 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74      /* Text of t
19cf9 68 65 20 6e 65 77 20 73 74 72 69 6e 67 20 2a 2f  he new string */
19cfa 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20  .  u8 enc,      
19cfb 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f           /* Enco
19cfc 64 69 6e 67 20 74 6f 20 75 73 65 20 2a 2f 0a 20  ding to use */. 
19cfd 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
19cfe 69 64 2a 29 20 20 20 2f 2a 20 44 65 73 74 72 75  id*)   /* Destru
19cff 63 74 6f 72 20 66 6f 72 20 74 68 65 20 73 74 72  ctor for the str
19d00 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20  ing */.){.  if( 
19d01 76 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 4d  v ) sqlite3VdbeM
19d02 65 6d 53 65 74 53 74 72 28 28 4d 65 6d 20 2a 29  emSetStr((Mem *)
19d03 76 2c 20 7a 2c 20 6e 2c 20 65 6e 63 2c 20 78 44  v, z, n, enc, xD
19d04 65 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72  el);.}../*.** Fr
19d05 65 65 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61  ee an sqlite3_va
19d06 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51  lue object.*/.SQ
19d07 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
19d08 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72  d sqlite3ValueFr
19d09 65 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ee(sqlite3_value
19d0a 20 2a 76 29 7b 0a 20 20 69 66 28 20 21 76 20 29   *v){.  if( !v )
19d0b 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74   return;.  sqlit
19d0c 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
19d0d 28 28 4d 65 6d 20 2a 29 76 29 3b 0a 20 20 73 71  ((Mem *)v);.  sq
19d0e 6c 69 74 65 33 44 62 46 72 65 65 28 28 28 4d 65  lite3DbFree(((Me
19d0f 6d 2a 29 76 29 2d 3e 64 62 2c 20 76 29 3b 0a 7d  m*)v)->db, v);.}
19d10 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
19d11 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
19d12 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65  es in the sqlite
19d13 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 61  3_value object a
19d14 73 73 75 6d 69 6e 67 0a 2a 2a 20 74 68 61 74 20  ssuming.** that 
19d15 69 74 20 75 73 65 73 20 74 68 65 20 65 6e 63 6f  it uses the enco
19d16 64 69 6e 67 20 22 65 6e 63 22 0a 2a 2f 0a 53 51  ding "enc".*/.SQ
19d17 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
19d18 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74   sqlite3ValueByt
19d19 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  es(sqlite3_value
19d1a 20 2a 70 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b   *pVal, u8 enc){
19d1b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 28 4d 65 6d  .  Mem *p = (Mem
19d1c 2a 29 70 56 61 6c 3b 0a 20 20 69 66 28 20 28 70  *)pVal;.  if( (p
19d1d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c  ->flags & MEM_Bl
19d1e 6f 62 29 21 3d 30 20 7c 7c 20 73 71 6c 69 74 65  ob)!=0 || sqlite
19d1f 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c  3ValueText(pVal,
19d20 20 65 6e 63 29 20 29 7b 0a 20 20 20 20 69 66 28   enc) ){.    if(
19d21 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f   p->flags & MEM_
19d22 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 72 65  Zero ){.      re
19d23 74 75 72 6e 20 70 2d 3e 6e 20 2b 20 70 2d 3e 75  turn p->n + p->u
19d24 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 65 6c 73  .nZero;.    }els
19d25 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  e{.      return 
19d26 70 2d 3e 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  p->n;.    }.  }.
19d27 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
19d28 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
19d29 6e 64 20 6f 66 20 76 64 62 65 6d 65 6d 2e 63 20  nd of vdbemem.c 
19d2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d2b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
19d2d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
19d2e 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 61 75  egin file vdbeau
19d2f 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  x.c ************
19d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d31 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
19d32 2a 0a 2a 2a 20 32 30 30 33 20 53 65 70 74 65 6d  *.** 2003 Septem
19d33 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ber 6.**.** The 
19d34 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
19d35 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
19d36 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
19d37 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
19d38 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
19d39 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
19d3a 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
19d3b 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
19d3c 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
19d3d 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
19d3e 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
19d3f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
19d40 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
19d41 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
19d42 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
19d43 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
19d44 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
19d45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d48 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d49 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
19d4a 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
19d4b 75 73 65 64 20 66 6f 72 20 63 72 65 61 74 69 6e  used for creatin
19d4c 67 2c 20 64 65 73 74 72 6f 79 69 6e 67 2c 20 61  g, destroying, a
19d4d 6e 64 20 70 6f 70 75 6c 61 74 69 6e 67 0a 2a 2a  nd populating.**
19d4e 20 61 20 56 44 42 45 20 28 6f 72 20 61 6e 20 22   a VDBE (or an "
19d4f 73 71 6c 69 74 65 33 5f 73 74 6d 74 22 20 61 73  sqlite3_stmt" as
19d50 20 69 74 20 69 73 20 6b 6e 6f 77 6e 20 74 6f 20   it is known to 
19d51 74 68 65 20 6f 75 74 73 69 64 65 20 77 6f 72 6c  the outside worl
19d52 64 2e 29 20 20 50 72 69 6f 72 0a 2a 2a 20 74 6f  d.)  Prior.** to
19d53 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 37 2c 20   version 2.8.7, 
19d54 61 6c 6c 20 74 68 69 73 20 63 6f 64 65 20 77 61  all this code wa
19d55 73 20 63 6f 6d 62 69 6e 65 64 20 69 6e 74 6f 20  s combined into 
19d56 74 68 65 20 76 64 62 65 2e 63 20 73 6f 75 72 63  the vdbe.c sourc
19d57 65 20 66 69 6c 65 2e 0a 2a 2a 20 42 75 74 20 74  e file..** But t
19d58 68 61 74 20 66 69 6c 65 20 77 61 73 20 67 65 74  hat file was get
19d59 74 69 6e 67 20 74 6f 6f 20 62 69 67 20 73 6f 20  ting too big so 
19d5a 74 68 69 73 20 73 75 62 72 6f 75 74 69 6e 65 73  this subroutines
19d5b 20 77 65 72 65 20 73 70 6c 69 74 20 6f 75 74 2e   were split out.
19d5c 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65  .**.** $Id: vdbe
19d5d 61 75 78 2e 63 2c 76 20 31 2e 34 38 30 20 32 30  aux.c,v 1.480 20
19d5e 30 39 2f 30 38 2f 30 38 20 31 38 3a 30 31 3a 30  09/08/08 18:01:0
19d5f 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  8 drh Exp $.*/..
19d60 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 64 65 62  ../*.** When deb
19d61 75 67 67 69 6e 67 20 74 68 65 20 63 6f 64 65 20  ugging the code 
19d62 67 65 6e 65 72 61 74 6f 72 20 69 6e 20 61 20 73  generator in a s
19d63 79 6d 62 6f 6c 69 63 20 64 65 62 75 67 67 65 72  ymbolic debugger
19d64 2c 20 6f 6e 65 20 63 61 6e 0a 2a 2a 20 73 65 74  , one can.** set
19d65 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 65   the sqlite3Vdbe
19d66 41 64 64 6f 70 54 72 61 63 65 20 74 6f 20 31 20  AddopTrace to 1 
19d67 61 6e 64 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20  and all opcodes 
19d68 77 69 6c 6c 20 62 65 20 70 72 69 6e 74 65 64 0a  will be printed.
19d69 2a 2a 20 61 73 20 74 68 65 79 20 61 72 65 20 61  ** as they are a
19d6a 64 64 65 64 20 74 6f 20 74 68 65 20 69 6e 73 74  dded to the inst
19d6b 72 75 63 74 69 6f 6e 20 73 74 72 65 61 6d 2e 0a  ruction stream..
19d6c 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
19d6d 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52  _DEBUG.SQLITE_PR
19d6e 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
19d6f 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 20  3VdbeAddopTrace 
19d70 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  = 0;.#endif.../*
19d71 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
19d72 20 76 69 72 74 75 61 6c 20 64 61 74 61 62 61 73   virtual databas
19d73 65 20 65 6e 67 69 6e 65 2e 0a 2a 2f 0a 53 51 4c  e engine..*/.SQL
19d74 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65  ITE_PRIVATE Vdbe
19d75 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72 65   *sqlite3VdbeCre
19d76 61 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ate(sqlite3 *db)
19d77 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 70  {.  Vdbe *p;.  p
19d78 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
19d79 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
19d7a 66 28 56 64 62 65 29 20 29 3b 0a 20 20 69 66 28  f(Vdbe) );.  if(
19d7b 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   p==0 ) return 0
19d7c 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a  ;.  p->db = db;.
19d7d 20 20 69 66 28 20 64 62 2d 3e 70 56 64 62 65 20    if( db->pVdbe 
19d7e 29 7b 0a 20 20 20 20 64 62 2d 3e 70 56 64 62 65  ){.    db->pVdbe
19d7f 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 7d  ->pPrev = p;.  }
19d80 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 64 62  .  p->pNext = db
19d81 2d 3e 70 56 64 62 65 3b 0a 20 20 70 2d 3e 70 50  ->pVdbe;.  p->pP
19d82 72 65 76 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 70  rev = 0;.  db->p
19d83 56 64 62 65 20 3d 20 70 3b 0a 20 20 70 2d 3e 6d  Vdbe = p;.  p->m
19d84 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49  agic = VDBE_MAGI
19d85 43 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e  C_INIT;.  return
19d86 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d   p;.}../*.** Rem
19d87 65 6d 62 65 72 20 74 68 65 20 53 51 4c 20 73 74  ember the SQL st
19d88 72 69 6e 67 20 66 6f 72 20 61 20 70 72 65 70 61  ring for a prepa
19d89 72 65 64 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  red statement..*
19d8a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
19d8b 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
19d8c 65 53 65 74 53 71 6c 28 56 64 62 65 20 2a 70 2c  eSetSql(Vdbe *p,
19d8d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
19d8e 69 6e 74 20 6e 2c 20 69 6e 74 20 69 73 50 72 65  int n, int isPre
19d8f 70 61 72 65 56 32 29 7b 0a 20 20 69 66 28 20 70  pareV2){.  if( p
19d90 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69  ==0 ) return;.#i
19d91 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
19d92 5f 54 52 41 43 45 0a 20 20 69 66 28 20 21 69 73  _TRACE.  if( !is
19d93 50 72 65 70 61 72 65 56 32 20 29 20 72 65 74 75  PrepareV2 ) retu
19d94 72 6e 3b 0a 23 65 6e 64 69 66 0a 20 20 61 73 73  rn;.#endif.  ass
19d95 65 72 74 28 20 70 2d 3e 7a 53 71 6c 3d 3d 30 20  ert( p->zSql==0 
19d96 29 3b 0a 20 20 70 2d 3e 7a 53 71 6c 20 3d 20 73  );.  p->zSql = s
19d97 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
19d98 70 2d 3e 64 62 2c 20 7a 2c 20 6e 29 3b 0a 20 20  p->db, z, n);.  
19d99 70 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 3d  p->isPrepareV2 =
19d9a 20 69 73 50 72 65 70 61 72 65 56 32 20 3f 20 31   isPrepareV2 ? 1
19d9b 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   : 0;.}../*.** R
19d9c 65 74 75 72 6e 20 74 68 65 20 53 51 4c 20 61 73  eturn the SQL as
19d9d 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
19d9e 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
19d9f 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  nt.*/.SQLITE_API
19da0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
19da1 69 74 65 33 5f 73 71 6c 28 73 71 6c 69 74 65 33  ite3_sql(sqlite3
19da2 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20  _stmt *pStmt){. 
19da3 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65   Vdbe *p = (Vdbe
19da4 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75   *)pStmt;.  retu
19da5 72 6e 20 28 70 2d 3e 69 73 50 72 65 70 61 72 65  rn (p->isPrepare
19da6 56 32 20 3f 20 70 2d 3e 7a 53 71 6c 20 3a 20 30  V2 ? p->zSql : 0
19da7 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 77 61 70  );.}../*.** Swap
19da8 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 20 62 65 74   all content bet
19da9 77 65 65 6e 20 74 77 6f 20 56 44 42 45 20 73 74  ween two VDBE st
19daa 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 53 51 4c  ructures..*/.SQL
19dab 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
19dac 20 73 71 6c 69 74 65 33 56 64 62 65 53 77 61 70   sqlite3VdbeSwap
19dad 28 56 64 62 65 20 2a 70 41 2c 20 56 64 62 65 20  (Vdbe *pA, Vdbe 
19dae 2a 70 42 29 7b 0a 20 20 56 64 62 65 20 74 6d 70  *pB){.  Vdbe tmp
19daf 2c 20 2a 70 54 6d 70 3b 0a 20 20 63 68 61 72 20  , *pTmp;.  char 
19db0 2a 7a 54 6d 70 3b 0a 20 20 74 6d 70 20 3d 20 2a  *zTmp;.  tmp = *
19db1 70 41 3b 0a 20 20 2a 70 41 20 3d 20 2a 70 42 3b  pA;.  *pA = *pB;
19db2 0a 20 20 2a 70 42 20 3d 20 74 6d 70 3b 0a 20 20  .  *pB = tmp;.  
19db3 70 54 6d 70 20 3d 20 70 41 2d 3e 70 4e 65 78 74  pTmp = pA->pNext
19db4 3b 0a 20 20 70 41 2d 3e 70 4e 65 78 74 20 3d 20  ;.  pA->pNext = 
19db5 70 42 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 42 2d  pB->pNext;.  pB-
19db6 3e 70 4e 65 78 74 20 3d 20 70 54 6d 70 3b 0a 20  >pNext = pTmp;. 
19db7 20 70 54 6d 70 20 3d 20 70 41 2d 3e 70 50 72 65   pTmp = pA->pPre
19db8 76 3b 0a 20 20 70 41 2d 3e 70 50 72 65 76 20 3d  v;.  pA->pPrev =
19db9 20 70 42 2d 3e 70 50 72 65 76 3b 0a 20 20 70 42   pB->pPrev;.  pB
19dba 2d 3e 70 50 72 65 76 20 3d 20 70 54 6d 70 3b 0a  ->pPrev = pTmp;.
19dbb 20 20 7a 54 6d 70 20 3d 20 70 41 2d 3e 7a 53 71    zTmp = pA->zSq
19dbc 6c 3b 0a 20 20 70 41 2d 3e 7a 53 71 6c 20 3d 20  l;.  pA->zSql = 
19dbd 70 42 2d 3e 7a 53 71 6c 3b 0a 20 20 70 42 2d 3e  pB->zSql;.  pB->
19dbe 7a 53 71 6c 20 3d 20 7a 54 6d 70 3b 0a 20 20 70  zSql = zTmp;.  p
19dbf 42 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 3d  B->isPrepareV2 =
19dc0 20 70 41 2d 3e 69 73 50 72 65 70 61 72 65 56 32   pA->isPrepareV2
19dc1 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ;.}..#ifdef SQLI
19dc2 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54  TE_DEBUG./*.** T
19dc3 75 72 6e 20 74 72 61 63 69 6e 67 20 6f 6e 20 6f  urn tracing on o
19dc4 72 20 6f 66 66 0a 2a 2f 0a 53 51 4c 49 54 45 5f  r off.*/.SQLITE_
19dc5 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
19dc6 69 74 65 33 56 64 62 65 54 72 61 63 65 28 56 64  ite3VdbeTrace(Vd
19dc7 62 65 20 2a 70 2c 20 46 49 4c 45 20 2a 74 72 61  be *p, FILE *tra
19dc8 63 65 29 7b 0a 20 20 70 2d 3e 74 72 61 63 65 20  ce){.  p->trace 
19dc9 3d 20 74 72 61 63 65 3b 0a 7d 0a 23 65 6e 64 69  = trace;.}.#endi
19dca 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 69 7a 65 20  f../*.** Resize 
19dcb 74 68 65 20 56 64 62 65 2e 61 4f 70 20 61 72 72  the Vdbe.aOp arr
19dcc 61 79 20 73 6f 20 74 68 61 74 20 69 74 20 69 73  ay so that it is
19dcd 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 70   at least one op
19dce 20 6c 61 72 67 65 72 20 74 68 61 6e 20 0a 2a 2a   larger than .**
19dcf 20 69 74 20 77 61 73 2e 0a 2a 2a 0a 2a 2a 20 49   it was..**.** I
19dd0 66 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f  f an out-of-memo
19dd1 72 79 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  ry error occurs 
19dd2 77 68 69 6c 65 20 72 65 73 69 7a 69 6e 67 20 74  while resizing t
19dd3 68 65 20 61 72 72 61 79 2c 20 72 65 74 75 72 6e  he array, return
19dd4 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  .** SQLITE_NOMEM
19dd5 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 56  . In this case V
19dd6 64 62 65 2e 61 4f 70 20 61 6e 64 20 56 64 62 65  dbe.aOp and Vdbe
19dd7 2e 6e 4f 70 41 6c 6c 6f 63 20 72 65 6d 61 69 6e  .nOpAlloc remain
19dd8 20 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 20 28   .** unchanged (
19dd9 74 68 69 73 20 69 73 20 73 6f 20 74 68 61 74 20  this is so that 
19dda 61 6e 79 20 6f 70 63 6f 64 65 73 20 61 6c 72 65  any opcodes alre
19ddb 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 20 63 61  ady allocated ca
19ddc 6e 20 62 65 20 0a 2a 2a 20 63 6f 72 72 65 63 74  n be .** correct
19ddd 6c 79 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 61  ly deallocated a
19dde 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 72 65  long with the re
19ddf 73 74 20 6f 66 20 74 68 65 20 56 64 62 65 29 2e  st of the Vdbe).
19de0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67  .*/.static int g
19de1 72 6f 77 4f 70 41 72 72 61 79 28 56 64 62 65 20  rowOpArray(Vdbe 
19de2 2a 70 29 7b 0a 20 20 56 64 62 65 4f 70 20 2a 70  *p){.  VdbeOp *p
19de3 4e 65 77 3b 0a 20 20 69 6e 74 20 6e 4e 65 77 20  New;.  int nNew 
19de4 3d 20 28 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3f  = (p->nOpAlloc ?
19de5 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 2a 32 20 3a   p->nOpAlloc*2 :
19de6 20 28 69 6e 74 29 28 31 30 32 34 2f 73 69 7a 65   (int)(1024/size
19de7 6f 66 28 4f 70 29 29 29 3b 0a 20 20 70 4e 65 77  of(Op)));.  pNew
19de8 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c   = sqlite3DbReal
19de9 6c 6f 63 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4f  loc(p->db, p->aO
19dea 70 2c 20 6e 4e 65 77 2a 73 69 7a 65 6f 66 28 4f  p, nNew*sizeof(O
19deb 70 29 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 20  p));.  if( pNew 
19dec 29 7b 0a 20 20 20 20 70 2d 3e 6e 4f 70 41 6c 6c  ){.    p->nOpAll
19ded 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  oc = sqlite3DbMa
19dee 6c 6c 6f 63 53 69 7a 65 28 70 2d 3e 64 62 2c 20  llocSize(p->db, 
19def 70 4e 65 77 29 2f 73 69 7a 65 6f 66 28 4f 70 29  pNew)/sizeof(Op)
19df0 3b 0a 20 20 20 20 70 2d 3e 61 4f 70 20 3d 20 70  ;.    p->aOp = p
19df1 4e 65 77 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  New;.  }.  retur
19df2 6e 20 28 70 4e 65 77 20 3f 20 53 51 4c 49 54 45  n (pNew ? SQLITE
19df3 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d  _OK : SQLITE_NOM
19df4 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  EM);.}../*.** Ad
19df5 64 20 61 20 6e 65 77 20 69 6e 73 74 72 75 63 74  d a new instruct
19df6 69 6f 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20  ion to the list 
19df7 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20  of instructions 
19df8 63 75 72 72 65 6e 74 20 69 6e 20 74 68 65 0a 2a  current in the.*
19df9 2a 20 56 44 42 45 2e 20 20 52 65 74 75 72 6e 20  * VDBE.  Return 
19dfa 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74  the address of t
19dfb 68 65 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69  he new instructi
19dfc 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65  on..**.** Parame
19dfd 74 65 72 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70  ters:.**.**    p
19dfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50                 P
19dff 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 56 44  ointer to the VD
19e00 42 45 0a 2a 2a 0a 2a 2a 20 20 20 20 6f 70 20 20  BE.**.**    op  
19e01 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20              The 
19e02 6f 70 63 6f 64 65 20 66 6f 72 20 74 68 69 73 20  opcode for this 
19e03 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 0a 2a  instruction.**.*
19e04 2a 20 20 20 20 70 31 2c 20 70 32 2c 20 70 33 20  *    p1, p2, p3 
19e05 20 20 20 20 20 4f 70 65 72 61 6e 64 73 0a 2a 2a       Operands.**
19e06 0a 2a 2a 20 55 73 65 20 74 68 65 20 73 71 6c 69  .** Use the sqli
19e07 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
19e08 62 65 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 74  bel() function t
19e09 6f 20 66 69 78 20 61 6e 20 61 64 64 72 65 73 73  o fix an address
19e0a 20 61 6e 64 0a 2a 2a 20 74 68 65 20 73 71 6c 69   and.** the sqli
19e0b 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
19e0c 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 68  ) function to ch
19e0d 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f  ange the value o
19e0e 66 20 74 68 65 20 50 34 0a 2a 2a 20 6f 70 65 72  f the P4.** oper
19e0f 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  and..*/.SQLITE_P
19e10 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19e11 65 33 56 64 62 65 41 64 64 4f 70 33 28 56 64 62  e3VdbeAddOp3(Vdb
19e12 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e  e *p, int op, in
19e13 74 20 70 31 2c 20 69 6e 74 20 70 32 2c 20 69 6e  t p1, int p2, in
19e14 74 20 70 33 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  t p3){.  int i;.
19e15 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 0a    VdbeOp *pOp;..
19e16 20 20 69 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20    i = p->nOp;.  
19e17 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63  assert( p->magic
19e18 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49  ==VDBE_MAGIC_INI
19e19 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f  T );.  assert( o
19e1a 70 3e 30 20 26 26 20 6f 70 3c 30 78 66 66 20 29  p>0 && op<0xff )
19e1b 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 41 6c  ;.  if( p->nOpAl
19e1c 6c 6f 63 3c 3d 69 20 29 7b 0a 20 20 20 20 69 66  loc<=i ){.    if
19e1d 28 20 67 72 6f 77 4f 70 41 72 72 61 79 28 70 29  ( growOpArray(p)
19e1e 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
19e1f 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
19e20 70 2d 3e 6e 4f 70 2b 2b 3b 0a 20 20 70 4f 70 20  p->nOp++;.  pOp 
19e21 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20  = &p->aOp[i];.  
19e22 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 28 75  pOp->opcode = (u
19e23 38 29 6f 70 3b 0a 20 20 70 4f 70 2d 3e 70 35 20  8)op;.  pOp->p5 
19e24 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 70 31 20 3d  = 0;.  pOp->p1 =
19e25 20 70 31 3b 0a 20 20 70 4f 70 2d 3e 70 32 20 3d   p1;.  pOp->p2 =
19e26 20 70 32 3b 0a 20 20 70 4f 70 2d 3e 70 33 20 3d   p2;.  pOp->p3 =
19e27 20 70 33 3b 0a 20 20 70 4f 70 2d 3e 70 34 2e 70   p3;.  pOp->p4.p
19e28 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 70 34 74   = 0;.  pOp->p4t
19e29 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44  ype = P4_NOTUSED
19e2a 3b 0a 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d  ;.  p->expired =
19e2b 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54   0;.#ifdef SQLIT
19e2c 45 5f 44 45 42 55 47 0a 20 20 70 4f 70 2d 3e 7a  E_DEBUG.  pOp->z
19e2d 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 69  Comment = 0;.  i
19e2e 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  f( sqlite3VdbeAd
19e2f 64 6f 70 54 72 61 63 65 20 29 20 73 71 6c 69 74  dopTrace ) sqlit
19e30 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 30 2c  e3VdbePrintOp(0,
19e31 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b   i, &p->aOp[i]);
19e32 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 56  .#endif.#ifdef V
19e33 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 70 4f  DBE_PROFILE.  pO
19e34 70 2d 3e 63 79 63 6c 65 73 20 3d 20 30 3b 0a 20  p->cycles = 0;. 
19e35 20 70 4f 70 2d 3e 63 6e 74 20 3d 20 30 3b 0a 23   pOp->cnt = 0;.#
19e36 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 69  endif.  return i
19e37 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
19e38 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
19e39 62 65 41 64 64 4f 70 30 28 56 64 62 65 20 2a 70  beAddOp0(Vdbe *p
19e3a 2c 20 69 6e 74 20 6f 70 29 7b 0a 20 20 72 65 74  , int op){.  ret
19e3b 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 41  urn sqlite3VdbeA
19e3c 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 30 2c 20  ddOp3(p, op, 0, 
19e3d 30 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  0, 0);.}.SQLITE_
19e3e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
19e3f 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 56 64  te3VdbeAddOp1(Vd
19e40 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69  be *p, int op, i
19e41 6e 74 20 70 31 29 7b 0a 20 20 72 65 74 75 72 6e  nt p1){.  return
19e42 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
19e43 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 30 2c  p3(p, op, p1, 0,
19e44 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52   0);.}.SQLITE_PR
19e45 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
19e46 33 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 65  3VdbeAddOp2(Vdbe
19e47 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74   *p, int op, int
19e48 20 70 31 2c 20 69 6e 74 20 70 32 29 7b 0a 20 20   p1, int p2){.  
19e49 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64  return sqlite3Vd
19e4a 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20  beAddOp3(p, op, 
19e4b 70 31 2c 20 70 32 2c 20 30 29 3b 0a 7d 0a 0a 0a  p1, p2, 0);.}...
19e4c 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 6f 70 63  /*.** Add an opc
19e4d 6f 64 65 20 74 68 61 74 20 69 6e 63 6c 75 64 65  ode that include
19e4e 73 20 74 68 65 20 70 34 20 76 61 6c 75 65 20 61  s the p4 value a
19e4f 73 20 61 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a  s a pointer..*/.
19e50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
19e51 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
19e52 64 4f 70 34 28 0a 20 20 56 64 62 65 20 2a 70 2c  dOp4(.  Vdbe *p,
19e53 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
19e54 64 64 20 74 68 65 20 6f 70 63 6f 64 65 20 74 6f  dd the opcode to
19e55 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 69 6e   this VM */.  in
19e56 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20  t op,           
19e57 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 6f 70 63    /* The new opc
19e58 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70 31 2c  ode */.  int p1,
19e59 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19e5a 54 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 2a  The P1 operand *
19e5b 2f 0a 20 20 69 6e 74 20 70 32 2c 20 20 20 20 20  /.  int p2,     
19e5c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50          /* The P
19e5d 32 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69  2 operand */.  i
19e5e 6e 74 20 70 33 2c 20 20 20 20 20 20 20 20 20 20  nt p3,          
19e5f 20 20 20 2f 2a 20 54 68 65 20 50 33 20 6f 70 65     /* The P3 ope
19e60 72 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  rand */.  const 
19e61 63 68 61 72 20 2a 7a 50 34 2c 20 20 20 20 2f 2a  char *zP4,    /*
19e62 20 54 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20   The P4 operand 
19e63 2a 2f 0a 20 20 69 6e 74 20 70 34 74 79 70 65 20  */.  int p4type 
19e64 20 20 20 20 20 20 20 20 20 2f 2a 20 50 34 20 6f           /* P4 o
19e65 70 65 72 61 6e 64 20 74 79 70 65 20 2a 2f 0a 29  perand type */.)
19e66 7b 0a 20 20 69 6e 74 20 61 64 64 72 20 3d 20 73  {.  int addr = s
19e67 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
19e68 28 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c 20  (p, op, p1, p2, 
19e69 70 33 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  p3);.  sqlite3Vd
19e6a 62 65 43 68 61 6e 67 65 50 34 28 70 2c 20 61 64  beChangeP4(p, ad
19e6b 64 72 2c 20 7a 50 34 2c 20 70 34 74 79 70 65 29  dr, zP4, p4type)
19e6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b  ;.  return addr;
19e6d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  .}../*.** Create
19e6e 20 61 20 6e 65 77 20 73 79 6d 62 6f 6c 69 63 20   a new symbolic 
19e6f 6c 61 62 65 6c 20 66 6f 72 20 61 6e 20 69 6e 73  label for an ins
19e70 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 68 61  truction that ha
19e71 73 20 79 65 74 20 74 6f 20 62 65 0a 2a 2a 20 63  s yet to be.** c
19e72 6f 64 65 64 2e 20 20 54 68 65 20 73 79 6d 62 6f  oded.  The symbo
19e73 6c 69 63 20 6c 61 62 65 6c 20 69 73 20 72 65 61  lic label is rea
19e74 6c 6c 79 20 6a 75 73 74 20 61 20 6e 65 67 61 74  lly just a negat
19e75 69 76 65 20 6e 75 6d 62 65 72 2e 20 20 54 68 65  ive number.  The
19e76 0a 2a 2a 20 6c 61 62 65 6c 20 63 61 6e 20 62 65  .** label can be
19e77 20 75 73 65 64 20 61 73 20 74 68 65 20 50 32 20   used as the P2 
19e78 76 61 6c 75 65 20 6f 66 20 61 6e 20 6f 70 65 72  value of an oper
19e79 61 74 69 6f 6e 2e 20 20 4c 61 74 65 72 2c 20 77  ation.  Later, w
19e7a 68 65 6e 0a 2a 2a 20 74 68 65 20 6c 61 62 65 6c  hen.** the label
19e7b 20 69 73 20 72 65 73 6f 6c 76 65 64 20 74 6f 20   is resolved to 
19e7c 61 20 73 70 65 63 69 66 69 63 20 61 64 64 72 65  a specific addre
19e7d 73 73 2c 20 74 68 65 20 56 44 42 45 20 77 69 6c  ss, the VDBE wil
19e7e 6c 20 73 63 61 6e 0a 2a 2a 20 74 68 72 6f 75 67  l scan.** throug
19e7f 68 20 69 74 73 20 6f 70 65 72 61 74 69 6f 6e 20  h its operation 
19e80 6c 69 73 74 20 61 6e 64 20 63 68 61 6e 67 65 20  list and change 
19e81 61 6c 6c 20 76 61 6c 75 65 73 20 6f 66 20 50 32  all values of P2
19e82 20 77 68 69 63 68 20 6d 61 74 63 68 0a 2a 2a 20   which match.** 
19e83 74 68 65 20 6c 61 62 65 6c 20 69 6e 74 6f 20 74  the label into t
19e84 68 65 20 72 65 73 6f 6c 76 65 64 20 61 64 64 72  he resolved addr
19e85 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56  ess..**.** The V
19e86 44 42 45 20 6b 6e 6f 77 73 20 74 68 61 74 20 61  DBE knows that a
19e87 20 50 32 20 76 61 6c 75 65 20 69 73 20 61 20 6c   P2 value is a l
19e88 61 62 65 6c 20 62 65 63 61 75 73 65 20 6c 61 62  abel because lab
19e89 65 6c 73 20 61 72 65 0a 2a 2a 20 61 6c 77 61 79  els are.** alway
19e8a 73 20 6e 65 67 61 74 69 76 65 20 61 6e 64 20 50  s negative and P
19e8b 32 20 76 61 6c 75 65 73 20 61 72 65 20 73 75 70  2 values are sup
19e8c 70 6f 73 65 20 74 6f 20 62 65 20 6e 6f 6e 2d 6e  pose to be non-n
19e8d 65 67 61 74 69 76 65 2e 0a 2a 2a 20 48 65 6e 63  egative..** Henc
19e8e 65 2c 20 61 20 6e 65 67 61 74 69 76 65 20 50 32  e, a negative P2
19e8f 20 76 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65   value is a labe
19e90 6c 20 74 68 61 74 20 68 61 73 20 79 65 74 20 74  l that has yet t
19e91 6f 20 62 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a  o be resolved..*
19e92 2a 0a 2a 2a 20 5a 65 72 6f 20 69 73 20 72 65 74  *.** Zero is ret
19e93 75 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f  urned if a mallo
19e94 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51  c() fails..*/.SQ
19e95 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
19e96 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
19e97 4c 61 62 65 6c 28 56 64 62 65 20 2a 70 29 7b 0a  Label(Vdbe *p){.
19e98 20 20 69 6e 74 20 69 3b 0a 20 20 69 20 3d 20 70    int i;.  i = p
19e99 2d 3e 6e 4c 61 62 65 6c 2b 2b 3b 0a 20 20 61 73  ->nLabel++;.  as
19e9a 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d  sert( p->magic==
19e9b 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20  VDBE_MAGIC_INIT 
19e9c 29 3b 0a 20 20 69 66 28 20 69 3e 3d 70 2d 3e 6e  );.  if( i>=p->n
19e9d 4c 61 62 65 6c 41 6c 6c 6f 63 20 29 7b 0a 20 20  LabelAlloc ){.  
19e9e 20 20 69 6e 74 20 6e 20 3d 20 70 2d 3e 6e 4c 61    int n = p->nLa
19e9f 62 65 6c 41 6c 6c 6f 63 2a 32 20 2b 20 35 3b 0a  belAlloc*2 + 5;.
19ea0 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20      p->aLabel = 
19ea1 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
19ea2 4f 72 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d  OrFree(p->db, p-
19ea3 3e 61 4c 61 62 65 6c 2c 0a 20 20 20 20 20 20 20  >aLabel,.       
19ea4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19ea5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19ea6 6e 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 4c 61 62  n*sizeof(p->aLab
19ea7 65 6c 5b 30 5d 29 29 3b 0a 20 20 20 20 70 2d 3e  el[0]));.    p->
19ea8 6e 4c 61 62 65 6c 41 6c 6c 6f 63 20 3d 20 73 71  nLabelAlloc = sq
19ea9 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a  lite3DbMallocSiz
19eaa 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62  e(p->db, p->aLab
19eab 65 6c 29 2f 73 69 7a 65 6f 66 28 70 2d 3e 61 4c  el)/sizeof(p->aL
19eac 61 62 65 6c 5b 30 5d 29 3b 0a 20 20 7d 0a 20 20  abel[0]);.  }.  
19ead 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20 29 7b  if( p->aLabel ){
19eae 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 5b 69  .    p->aLabel[i
19eaf 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 72 65  ] = -1;.  }.  re
19eb0 74 75 72 6e 20 2d 31 2d 69 3b 0a 7d 0a 0a 2f 2a  turn -1-i;.}../*
19eb1 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 6c 61 62 65  .** Resolve labe
19eb2 6c 20 22 78 22 20 74 6f 20 62 65 20 74 68 65 20  l "x" to be the 
19eb3 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e  address of the n
19eb4 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ext instruction 
19eb5 74 6f 0a 2a 2a 20 62 65 20 69 6e 73 65 72 74 65  to.** be inserte
19eb6 64 2e 20 20 54 68 65 20 70 61 72 61 6d 65 74 65  d.  The paramete
19eb7 72 20 22 78 22 20 6d 75 73 74 20 68 61 76 65 20  r "x" must have 
19eb8 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 66 72  been obtained fr
19eb9 6f 6d 0a 2a 2a 20 61 20 70 72 69 6f 72 20 63 61  om.** a prior ca
19eba 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62  ll to sqlite3Vdb
19ebb 65 4d 61 6b 65 4c 61 62 65 6c 28 29 2e 0a 2a 2f  eMakeLabel()..*/
19ebc 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
19ebd 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
19ebe 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 56 64 62  ResolveLabel(Vdb
19ebf 65 20 2a 70 2c 20 69 6e 74 20 78 29 7b 0a 20 20  e *p, int x){.  
19ec0 69 6e 74 20 6a 20 3d 20 2d 31 2d 78 3b 0a 20 20  int j = -1-x;.  
19ec1 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63  assert( p->magic
19ec2 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49  ==VDBE_MAGIC_INI
19ec3 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6a  T );.  assert( j
19ec4 3e 3d 30 20 26 26 20 6a 3c 70 2d 3e 6e 4c 61 62  >=0 && j<p->nLab
19ec5 65 6c 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61  el );.  if( p->a
19ec6 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70 2d 3e  Label ){.    p->
19ec7 61 4c 61 62 65 6c 5b 6a 5d 20 3d 20 70 2d 3e 6e  aLabel[j] = p->n
19ec8 4f 70 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 65  Op;.  }.}..#ifde
19ec9 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 0a  f SQLITE_DEBUG..
19eca 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
19ecb 69 6e 67 20 74 79 70 65 20 61 6e 64 20 66 75 6e  ing type and fun
19ecc 63 74 69 6f 6e 20 61 72 65 20 75 73 65 64 20 74  ction are used t
19ecd 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67  o iterate throug
19ece 68 20 61 6c 6c 20 6f 70 63 6f 64 65 73 0a 2a 2a  h all opcodes.**
19ecf 20 69 6e 20 61 20 56 64 62 65 20 6d 61 69 6e 20   in a Vdbe main 
19ed0 70 72 6f 67 72 61 6d 20 61 6e 64 20 65 61 63 68  program and each
19ed1 20 6f 66 20 74 68 65 20 73 75 62 2d 70 72 6f 67   of the sub-prog
19ed2 72 61 6d 73 20 28 74 72 69 67 67 65 72 73 29 20  rams (triggers) 
19ed3 69 74 20 6d 61 79 20 0a 2a 2a 20 69 6e 76 6f 6b  it may .** invok
19ed4 65 20 64 69 72 65 63 74 6c 79 20 6f 72 20 69 6e  e directly or in
19ed5 64 69 72 65 63 74 6c 79 2e 20 49 74 20 73 68 6f  directly. It sho
19ed6 75 6c 64 20 62 65 20 75 73 65 64 20 61 73 20 66  uld be used as f
19ed7 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ollows:.**.**   
19ed8 4f 70 20 2a 70 4f 70 3b 0a 2a 2a 20 20 20 56 64  Op *pOp;.**   Vd
19ed9 62 65 4f 70 49 74 65 72 20 73 49 74 65 72 3b 0a  beOpIter sIter;.
19eda 2a 2a 0a 2a 2a 20 20 20 6d 65 6d 73 65 74 28 26  **.**   memset(&
19edb 73 49 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66  sIter, 0, sizeof
19edc 28 73 49 74 65 72 29 29 3b 0a 2a 2a 20 20 20 73  (sIter));.**   s
19edd 49 74 65 72 2e 76 20 3d 20 76 3b 20 20 20 20 20  Iter.v = v;     
19ede 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19edf 20 20 20 20 20 20 20 2f 2f 20 76 20 69 73 20 6f         // v is o
19ee0 66 20 74 79 70 65 20 56 64 62 65 2a 20 0a 2a 2a  f type Vdbe* .**
19ee1 20 20 20 77 68 69 6c 65 28 20 28 70 4f 70 20 3d     while( (pOp =
19ee2 20 6f 70 49 74 65 72 4e 65 78 74 28 26 73 49 74   opIterNext(&sIt
19ee3 65 72 29 29 20 29 7b 0a 2a 2a 20 20 20 20 20 2f  er)) ){.**     /
19ee4 2f 20 44 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77  / Do something w
19ee5 69 74 68 20 70 4f 70 0a 2a 2a 20 20 20 7d 0a 2a  ith pOp.**   }.*
19ee6 2a 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  *   sqlite3DbFre
19ee7 65 28 76 2d 3e 64 62 2c 20 73 49 74 65 72 2e 61  e(v->db, sIter.a
19ee8 70 53 75 62 29 3b 0a 2a 2a 20 0a 2a 2f 0a 74 79  pSub);.** .*/.ty
19ee9 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62  pedef struct Vdb
19eea 65 4f 70 49 74 65 72 20 56 64 62 65 4f 70 49 74  eOpIter VdbeOpIt
19eeb 65 72 3b 0a 73 74 72 75 63 74 20 56 64 62 65 4f  er;.struct VdbeO
19eec 70 49 74 65 72 20 7b 0a 20 20 56 64 62 65 20 2a  pIter {.  Vdbe *
19eed 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
19eee 20 20 20 20 20 2f 2a 20 56 64 62 65 20 74 6f 20       /* Vdbe to 
19eef 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20  iterate through 
19ef0 74 68 65 20 6f 70 63 6f 64 65 73 20 6f 66 20 2a  the opcodes of *
19ef1 2f 0a 20 20 53 75 62 50 72 6f 67 72 61 6d 20 2a  /.  SubProgram *
19ef2 2a 61 70 53 75 62 3b 20 20 20 20 20 20 20 20 2f  *apSub;        /
19ef3 2a 20 41 72 72 61 79 20 6f 66 20 73 75 62 70 72  * Array of subpr
19ef4 6f 67 72 61 6d 73 20 2a 2f 0a 20 20 69 6e 74 20  ograms */.  int 
19ef5 6e 53 75 62 3b 20 20 20 20 20 20 20 20 20 20 20  nSub;           
19ef6 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
19ef7 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
19ef8 70 53 75 62 20 2a 2f 0a 20 20 69 6e 74 20 69 41  pSub */.  int iA
19ef9 64 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  ddr;            
19efa 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
19efb 6f 66 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74  of next instruct
19efc 69 6f 6e 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f  ion to return */
19efd 0a 20 20 69 6e 74 20 69 53 75 62 3b 20 20 20 20  .  int iSub;    
19efe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19eff 20 30 20 3d 20 6d 61 69 6e 20 70 72 6f 67 72 61   0 = main progra
19f00 6d 2c 20 31 20 3d 20 66 69 72 73 74 20 73 75 62  m, 1 = first sub
19f01 2d 70 72 6f 67 72 61 6d 20 65 74 63 2e 20 2a 2f  -program etc. */
19f02 0a 7d 3b 0a 73 74 61 74 69 63 20 4f 70 20 2a 6f  .};.static Op *o
19f03 70 49 74 65 72 4e 65 78 74 28 56 64 62 65 4f 70  pIterNext(VdbeOp
19f04 49 74 65 72 20 2a 70 29 7b 0a 20 20 56 64 62 65  Iter *p){.  Vdbe
19f05 20 2a 76 20 3d 20 70 2d 3e 76 3b 0a 20 20 4f 70   *v = p->v;.  Op
19f06 20 2a 70 52 65 74 20 3d 20 30 3b 0a 20 20 4f 70   *pRet = 0;.  Op
19f07 20 2a 61 4f 70 3b 0a 20 20 69 6e 74 20 6e 4f 70   *aOp;.  int nOp
19f08 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 69 53 75 62  ;..  if( p->iSub
19f09 3c 3d 70 2d 3e 6e 53 75 62 20 29 7b 0a 0a 20 20  <=p->nSub ){..  
19f0a 20 20 69 66 28 20 70 2d 3e 69 53 75 62 3d 3d 30    if( p->iSub==0
19f0b 20 29 7b 0a 20 20 20 20 20 20 61 4f 70 20 3d 20   ){.      aOp = 
19f0c 76 2d 3e 61 4f 70 3b 0a 20 20 20 20 20 20 6e 4f  v->aOp;.      nO
19f0d 70 20 3d 20 76 2d 3e 6e 4f 70 3b 0a 20 20 20 20  p = v->nOp;.    
19f0e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 4f 70  }else{.      aOp
19f0f 20 3d 20 70 2d 3e 61 70 53 75 62 5b 70 2d 3e 69   = p->apSub[p->i
19f10 53 75 62 2d 31 5d 2d 3e 61 4f 70 3b 0a 20 20 20  Sub-1]->aOp;.   
19f11 20 20 20 6e 4f 70 20 3d 20 70 2d 3e 61 70 53 75     nOp = p->apSu
19f12 62 5b 70 2d 3e 69 53 75 62 2d 31 5d 2d 3e 6e 4f  b[p->iSub-1]->nO
19f13 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  p;.    }.    ass
19f14 65 72 74 28 20 70 2d 3e 69 41 64 64 72 3c 6e 4f  ert( p->iAddr<nO
19f15 70 20 29 3b 0a 0a 20 20 20 20 70 52 65 74 20 3d  p );..    pRet =
19f16 20 26 61 4f 70 5b 70 2d 3e 69 41 64 64 72 5d 3b   &aOp[p->iAddr];
19f17 0a 20 20 20 20 70 2d 3e 69 41 64 64 72 2b 2b 3b  .    p->iAddr++;
19f18 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 41 64 64  .    if( p->iAdd
19f19 72 3d 3d 6e 4f 70 20 29 7b 0a 20 20 20 20 20 20  r==nOp ){.      
19f1a 70 2d 3e 69 53 75 62 2b 2b 3b 0a 20 20 20 20 20  p->iSub++;.     
19f1b 20 70 2d 3e 69 41 64 64 72 20 3d 20 30 3b 0a 20   p->iAddr = 0;. 
19f1c 20 20 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20     }.  .    if( 
19f1d 70 52 65 74 2d 3e 70 34 74 79 70 65 3d 3d 50 34  pRet->p4type==P4
19f1e 5f 53 55 42 50 52 4f 47 52 41 4d 20 29 7b 0a 20  _SUBPROGRAM ){. 
19f1f 20 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d       int nByte =
19f20 20 28 70 2d 3e 6e 53 75 62 2b 31 29 2a 73 69 7a   (p->nSub+1)*siz
19f21 65 6f 66 28 53 75 62 50 72 6f 67 72 61 6d 2a 29  eof(SubProgram*)
19f22 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20  ;.      int j;. 
19f23 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
19f24 70 2d 3e 6e 53 75 62 3b 20 6a 2b 2b 29 7b 0a 20  p->nSub; j++){. 
19f25 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 61 70         if( p->ap
19f26 53 75 62 5b 6a 5d 3d 3d 70 52 65 74 2d 3e 70 34  Sub[j]==pRet->p4
19f27 2e 70 50 72 6f 67 72 61 6d 20 29 20 62 72 65 61  .pProgram ) brea
19f28 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
19f29 20 69 66 28 20 6a 3d 3d 70 2d 3e 6e 53 75 62 20   if( j==p->nSub 
19f2a 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 70  ){.        p->ap
19f2b 53 75 62 20 3d 20 73 71 6c 69 74 65 33 44 62 52  Sub = sqlite3DbR
19f2c 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 76 2d 3e  eallocOrFree(v->
19f2d 64 62 2c 20 70 2d 3e 61 70 53 75 62 2c 20 6e 42  db, p->apSub, nB
19f2e 79 74 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66  yte);.        if
19f2f 28 20 21 70 2d 3e 61 70 53 75 62 20 29 7b 0a 20  ( !p->apSub ){. 
19f30 20 20 20 20 20 20 20 20 20 70 52 65 74 20 3d 20           pRet = 
19f31 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  0;.        }else
19f32 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61  {.          p->a
19f33 70 53 75 62 5b 70 2d 3e 6e 53 75 62 2b 2b 5d 20  pSub[p->nSub++] 
19f34 3d 20 70 52 65 74 2d 3e 70 34 2e 70 50 72 6f 67  = pRet->p4.pProg
19f35 72 61 6d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ram;.        }. 
19f36 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
19f37 0a 0a 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b  ..  return pRet;
19f38 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
19f39 20 74 72 75 65 20 69 66 20 74 68 65 20 70 72 6f   true if the pro
19f3a 67 72 61 6d 20 73 74 6f 72 65 64 20 69 6e 20 74  gram stored in t
19f3b 68 65 20 56 4d 20 70 61 73 73 65 64 20 61 73 20  he VM passed as 
19f3c 61 6e 20 61 72 67 75 6d 65 6e 74 20 6d 61 79 0a  an argument may.
19f3d 2a 2a 20 74 68 72 6f 77 20 61 6e 20 41 42 4f 52  ** throw an ABOR
19f3e 54 20 65 78 63 65 70 74 69 6f 6e 20 28 63 61 75  T exception (cau
19f3f 73 69 6e 67 20 74 68 65 20 73 74 61 74 65 6d 65  sing the stateme
19f40 6e 74 2c 20 62 75 74 20 6e 6f 74 20 74 72 61 6e  nt, but not tran
19f41 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65  saction.** to be
19f42 20 72 6f 6c 6c 65 64 20 62 61 63 6b 29 2e 20 54   rolled back). T
19f43 68 69 73 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73  his condition is
19f44 20 74 72 75 65 20 69 66 20 74 68 65 20 6d 61 69   true if the mai
19f45 6e 20 70 72 6f 67 72 61 6d 20 6f 72 20 61 6e 79  n program or any
19f46 0a 2a 2a 20 73 75 62 2d 70 72 6f 67 72 61 6d 73  .** sub-programs
19f47 20 63 6f 6e 74 61 69 6e 73 20 61 6e 79 20 6f 66   contains any of
19f48 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
19f49 2a 2a 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 48 61  **.**   *  OP_Ha
19f4a 6c 74 20 77 69 74 68 20 50 31 3d 53 51 4c 49 54  lt with P1=SQLIT
19f4b 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64  E_CONSTRAINT and
19f4c 20 50 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a   P2=OE_Abort..**
19f4d 20 20 20 2a 20 20 4f 50 5f 48 61 6c 74 49 66 4e     *  OP_HaltIfN
19f4e 75 6c 6c 20 77 69 74 68 20 50 31 3d 53 51 4c 49  ull with P1=SQLI
19f4f 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e  TE_CONSTRAINT an
19f50 64 20 50 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a  d P2=OE_Abort..*
19f51 2a 20 20 20 2a 20 20 4f 50 5f 44 65 73 74 72 6f  *   *  OP_Destro
19f52 79 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 55 70  y.**   *  OP_VUp
19f53 64 61 74 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f  date.**   *  OP_
19f54 56 52 65 6e 61 6d 65 0a 2a 2a 0a 2a 2a 20 54 68  VRename.**.** Th
19f55 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f  is function is o
19f56 6e 6c 79 20 75 73 65 64 20 61 73 20 70 61 72 74  nly used as part
19f57 20 6f 66 20 61 6e 20 61 73 73 65 72 74 28 29 20   of an assert() 
19f58 73 74 61 74 65 6d 65 6e 74 2e 20 0a 2a 2f 0a 53  statement. .*/.S
19f59 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
19f5a 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 79  t sqlite3VdbeMay
19f5b 41 62 6f 72 74 28 56 64 62 65 20 2a 76 29 7b 0a  Abort(Vdbe *v){.
19f5c 20 20 69 6e 74 20 6d 61 79 41 62 6f 72 74 20 3d    int mayAbort =
19f5d 20 30 3b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20   0;.  Op *pOp;. 
19f5e 20 56 64 62 65 4f 70 49 74 65 72 20 73 49 74 65   VdbeOpIter sIte
19f5f 72 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73 49 74  r;.  memset(&sIt
19f60 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 49  er, 0, sizeof(sI
19f61 74 65 72 29 29 3b 0a 20 20 73 49 74 65 72 2e 76  ter));.  sIter.v
19f62 20 3d 20 76 3b 0a 0a 20 20 77 68 69 6c 65 28 20   = v;..  while( 
19f63 28 70 4f 70 20 3d 20 6f 70 49 74 65 72 4e 65 78  (pOp = opIterNex
19f64 74 28 26 73 49 74 65 72 29 29 21 3d 30 20 29 7b  t(&sIter))!=0 ){
19f65 0a 20 20 20 20 69 6e 74 20 6f 70 63 6f 64 65 20  .    int opcode 
19f66 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20  = pOp->opcode;. 
19f67 20 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f     if( opcode==O
19f68 50 5f 44 65 73 74 72 6f 79 20 7c 7c 20 6f 70 63  P_Destroy || opc
19f69 6f 64 65 3d 3d 4f 50 5f 56 55 70 64 61 74 65 20  ode==OP_VUpdate 
19f6a 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 52  || opcode==OP_VR
19f6b 65 6e 61 6d 65 20 0a 20 20 20 20 20 7c 7c 20 28  ename .     || (
19f6c 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48 61 6c 74  (opcode==OP_Halt
19f6d 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48   || opcode==OP_H
19f6e 61 6c 74 49 66 4e 75 6c 6c 29 20 0a 20 20 20 20  altIfNull) .    
19f6f 20 20 26 26 20 28 70 4f 70 2d 3e 70 31 3d 3d 53    && (pOp->p1==S
19f70 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
19f71 20 26 26 20 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f   && pOp->p2==OE_
19f72 41 62 6f 72 74 29 29 0a 20 20 20 20 29 7b 0a 20  Abort)).    ){. 
19f73 20 20 20 20 20 6d 61 79 41 62 6f 72 74 20 3d 20       mayAbort = 
19f74 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  1;.      break;.
19f75 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c      }.  }..  sql
19f76 69 74 65 33 44 62 46 72 65 65 28 76 2d 3e 64 62  ite3DbFree(v->db
19f77 2c 20 73 49 74 65 72 2e 61 70 53 75 62 29 3b 0a  , sIter.apSub);.
19f78 20 20 72 65 74 75 72 6e 20 6d 61 79 41 62 6f 72    return mayAbor
19f79 74 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  t;.}.#endif../*.
19f7a 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20  ** Loop through 
19f7b 74 68 65 20 70 72 6f 67 72 61 6d 20 6c 6f 6f 6b  the program look
19f7c 69 6e 67 20 66 6f 72 20 50 32 20 76 61 6c 75 65  ing for P2 value
19f7d 73 20 74 68 61 74 20 61 72 65 20 6e 65 67 61 74  s that are negat
19f7e 69 76 65 0a 2a 2a 20 6f 6e 20 6a 75 6d 70 20 69  ive.** on jump i
19f7f 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 20 45 61  nstructions.  Ea
19f80 63 68 20 73 75 63 68 20 76 61 6c 75 65 20 69 73  ch such value is
19f81 20 61 20 6c 61 62 65 6c 2e 20 20 52 65 73 6f 6c   a label.  Resol
19f82 76 65 20 74 68 65 0a 2a 2a 20 6c 61 62 65 6c 20  ve the.** label 
19f83 62 79 20 73 65 74 74 69 6e 67 20 74 68 65 20 50  by setting the P
19f84 32 20 76 61 6c 75 65 20 74 6f 20 69 74 73 20 63  2 value to its c
19f85 6f 72 72 65 63 74 20 6e 6f 6e 2d 7a 65 72 6f 20  orrect non-zero 
19f86 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  value..**.** Thi
19f87 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
19f88 6c 65 64 20 6f 6e 63 65 20 61 66 74 65 72 20 61  led once after a
19f89 6c 6c 20 6f 70 63 6f 64 65 73 20 68 61 76 65 20  ll opcodes have 
19f8a 62 65 65 6e 20 69 6e 73 65 72 74 65 64 2e 0a 2a  been inserted..*
19f8b 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65 20 2a 70  *.** Variable *p
19f8c 4d 61 78 46 75 6e 63 41 72 67 73 20 69 73 20 73  MaxFuncArgs is s
19f8d 65 74 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75  et to the maximu
19f8e 6d 20 76 61 6c 75 65 20 6f 66 20 61 6e 79 20 50  m value of any P
19f8f 32 20 61 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 74  2 argument .** t
19f90 6f 20 61 6e 20 4f 50 5f 46 75 6e 63 74 69 6f 6e  o an OP_Function
19f91 2c 20 4f 50 5f 41 67 67 53 74 65 70 20 6f 72 20  , OP_AggStep or 
19f92 4f 50 5f 56 46 69 6c 74 65 72 20 6f 70 63 6f 64  OP_VFilter opcod
19f93 65 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20  e. This is used 
19f94 62 79 20 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64  by .** sqlite3Vd
19f95 62 65 4d 61 6b 65 52 65 61 64 79 28 29 20 74 6f  beMakeReady() to
19f96 20 73 69 7a 65 20 74 68 65 20 56 64 62 65 2e 61   size the Vdbe.a
19f97 70 41 72 67 5b 5d 20 61 72 72 61 79 2e 0a 2a 2f  pArg[] array..*/
19f98 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 73  .static void res
19f99 6f 6c 76 65 50 32 56 61 6c 75 65 73 28 56 64 62  olveP2Values(Vdb
19f9a 65 20 2a 70 2c 20 69 6e 74 20 2a 70 4d 61 78 46  e *p, int *pMaxF
19f9b 75 6e 63 41 72 67 73 29 7b 0a 20 20 69 6e 74 20  uncArgs){.  int 
19f9c 69 3b 0a 20 20 69 6e 74 20 6e 4d 61 78 41 72 67  i;.  int nMaxArg
19f9d 73 20 3d 20 2a 70 4d 61 78 46 75 6e 63 41 72 67  s = *pMaxFuncArg
19f9e 73 3b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20  s;.  Op *pOp;.  
19f9f 69 6e 74 20 2a 61 4c 61 62 65 6c 20 3d 20 70 2d  int *aLabel = p-
19fa0 3e 61 4c 61 62 65 6c 3b 0a 20 20 70 2d 3e 72 65  >aLabel;.  p->re
19fa1 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20 66 6f  adOnly = 1;.  fo
19fa2 72 28 70 4f 70 3d 70 2d 3e 61 4f 70 2c 20 69 3d  r(pOp=p->aOp, i=
19fa3 70 2d 3e 6e 4f 70 2d 31 3b 20 69 3e 3d 30 3b 20  p->nOp-1; i>=0; 
19fa4 69 2d 2d 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20  i--, pOp++){.   
19fa5 20 75 38 20 6f 70 63 6f 64 65 20 3d 20 70 4f 70   u8 opcode = pOp
19fa6 2d 3e 6f 70 63 6f 64 65 3b 0a 0a 20 20 20 20 69  ->opcode;..    i
19fa7 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 75  f( opcode==OP_Fu
19fa8 6e 63 74 69 6f 6e 20 7c 7c 20 6f 70 63 6f 64 65  nction || opcode
19fa9 3d 3d 4f 50 5f 41 67 67 53 74 65 70 20 29 7b 0a  ==OP_AggStep ){.
19faa 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70        if( pOp->p
19fab 35 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61  5>nMaxArgs ) nMa
19fac 78 41 72 67 73 20 3d 20 70 4f 70 2d 3e 70 35 3b  xArgs = pOp->p5;
19fad 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
19fae 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
19faf 45 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  E.    }else if( 
19fb0 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 55 70 64 61  opcode==OP_VUpda
19fb1 74 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  te ){.      if( 
19fb2 70 4f 70 2d 3e 70 32 3e 6e 4d 61 78 41 72 67 73  pOp->p2>nMaxArgs
19fb3 20 29 20 6e 4d 61 78 41 72 67 73 20 3d 20 70 4f   ) nMaxArgs = pO
19fb4 70 2d 3e 70 32 3b 0a 23 65 6e 64 69 66 0a 20 20  p->p2;.#endif.  
19fb5 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f    }else if( opco
19fb6 64 65 3d 3d 4f 50 5f 54 72 61 6e 73 61 63 74 69  de==OP_Transacti
19fb7 6f 6e 20 26 26 20 70 4f 70 2d 3e 70 32 21 3d 30  on && pOp->p2!=0
19fb8 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 65 61   ){.      p->rea
19fb9 64 4f 6e 6c 79 20 3d 20 30 3b 0a 23 69 66 6e 64  dOnly = 0;.#ifnd
19fba 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
19fbb 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20  IRTUALTABLE.    
19fbc 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65  }else if( opcode
19fbd 3d 3d 4f 50 5f 56 46 69 6c 74 65 72 20 29 7b 0a  ==OP_VFilter ){.
19fbe 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20        int n;.   
19fbf 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f     assert( p->nO
19fc0 70 20 2d 20 69 20 3e 3d 20 33 20 29 3b 0a 20 20  p - i >= 3 );.  
19fc1 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b      assert( pOp[
19fc2 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49  -1].opcode==OP_I
19fc3 6e 74 65 67 65 72 20 29 3b 0a 20 20 20 20 20 20  nteger );.      
19fc4 6e 20 3d 20 70 4f 70 5b 2d 31 5d 2e 70 31 3b 0a  n = pOp[-1].p1;.
19fc5 20 20 20 20 20 20 69 66 28 20 6e 3e 6e 4d 61 78        if( n>nMax
19fc6 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20  Args ) nMaxArgs 
19fc7 3d 20 6e 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  = n;.#endif.    
19fc8 7d 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  }..    if( sqlit
19fc9 65 33 56 64 62 65 4f 70 63 6f 64 65 48 61 73 50  e3VdbeOpcodeHasP
19fca 72 6f 70 65 72 74 79 28 6f 70 63 6f 64 65 2c 20  roperty(opcode, 
19fcb 4f 50 46 4c 47 5f 4a 55 4d 50 29 20 26 26 20 70  OPFLG_JUMP) && p
19fcc 4f 70 2d 3e 70 32 3c 30 20 29 7b 0a 20 20 20 20  Op->p2<0 ){.    
19fcd 20 20 61 73 73 65 72 74 28 20 2d 31 2d 70 4f 70    assert( -1-pOp
19fce 2d 3e 70 32 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29  ->p2<p->nLabel )
19fcf 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70 32 20  ;.      pOp->p2 
19fd0 3d 20 61 4c 61 62 65 6c 5b 2d 31 2d 70 4f 70 2d  = aLabel[-1-pOp-
19fd1 3e 70 32 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  >p2];.    }.  }.
19fd2 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
19fd3 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c  p->db, p->aLabel
19fd4 29 3b 0a 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d  );.  p->aLabel =
19fd5 20 30 3b 0a 0a 20 20 2a 70 4d 61 78 46 75 6e 63   0;..  *pMaxFunc
19fd6 41 72 67 73 20 3d 20 6e 4d 61 78 41 72 67 73 3b  Args = nMaxArgs;
19fd7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
19fd8 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20   the address of 
19fd9 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63  the next instruc
19fda 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e 73 65 72  tion to be inser
19fdb 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ted..*/.SQLITE_P
19fdc 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19fdd 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
19fde 72 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 61 73  r(Vdbe *p){.  as
19fdf 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d  sert( p->magic==
19fe0 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20  VDBE_MAGIC_INIT 
19fe1 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e  );.  return p->n
19fe2 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  Op;.}../*.** Thi
19fe3 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
19fe4 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
19fe5 74 68 65 20 61 72 72 61 79 20 6f 66 20 6f 70 63  the array of opc
19fe6 6f 64 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  odes associated 
19fe7 77 69 74 68 0a 2a 2a 20 74 68 65 20 56 64 62 65  with.** the Vdbe
19fe8 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66   passed as the f
19fe9 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 49  irst argument. I
19fea 74 20 69 73 20 74 68 65 20 63 61 6c 6c 65 72 73  t is the callers
19feb 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 0a   responsibility.
19fec 2a 2a 20 74 6f 20 61 72 72 61 6e 67 65 20 66 6f  ** to arrange fo
19fed 72 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 61  r the returned a
19fee 72 72 61 79 20 74 6f 20 62 65 20 65 76 65 6e 74  rray to be event
19fef 75 61 6c 6c 79 20 66 72 65 65 64 20 75 73 69 6e  ually freed usin
19ff0 67 20 74 68 65 20 0a 2a 2a 20 76 64 62 65 46 72  g the .** vdbeFr
19ff1 65 65 4f 70 41 72 72 61 79 28 29 20 66 75 6e 63  eeOpArray() func
19ff2 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f  tion..**.** Befo
19ff3 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70  re returning, *p
19ff4 6e 4f 70 20 69 73 20 73 65 74 20 74 6f 20 74 68  nOp is set to th
19ff5 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  e number of entr
19ff6 69 65 73 20 69 6e 20 74 68 65 20 72 65 74 75 72  ies in the retur
19ff7 6e 65 64 0a 2a 2a 20 61 72 72 61 79 2e 20 41 6c  ned.** array. Al
19ff8 73 6f 2c 20 2a 70 6e 4d 61 78 41 72 67 20 69 73  so, *pnMaxArg is
19ff9 20 73 65 74 20 74 6f 20 74 68 65 20 6c 61 72 67   set to the larg
19ffa 65 72 20 6f 66 20 69 74 73 20 63 75 72 72 65 6e  er of its curren
19ffb 74 20 76 61 6c 75 65 20 61 6e 64 20 0a 2a 2a 20  t value and .** 
19ffc 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e  the number of en
19ffd 74 72 69 65 73 20 69 6e 20 74 68 65 20 56 64 62  tries in the Vdb
19ffe 65 2e 61 70 41 72 67 5b 5d 20 61 72 72 61 79 20  e.apArg[] array 
19fff 72 65 71 75 69 72 65 64 20 74 6f 20 65 78 65 63  required to exec
1a000 75 74 65 20 74 68 65 20 0a 2a 2a 20 72 65 74 75  ute the .** retu
1a001 72 6e 65 64 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f  rned program..*/
1a002 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a003 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56  VdbeOp *sqlite3V
1a004 64 62 65 54 61 6b 65 4f 70 41 72 72 61 79 28 56  dbeTakeOpArray(V
1a005 64 62 65 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 4f  dbe *p, int *pnO
1a006 70 2c 20 69 6e 74 20 2a 70 6e 4d 61 78 41 72 67  p, int *pnMaxArg
1a007 29 7b 0a 20 20 56 64 62 65 4f 70 20 2a 61 4f 70  ){.  VdbeOp *aOp
1a008 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20 20 61 73 73   = p->aOp;.  ass
1a009 65 72 74 28 20 61 4f 70 20 26 26 20 21 70 2d 3e  ert( aOp && !p->
1a00a 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1a00b 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20   );..  /* Check 
1a00c 74 68 61 74 20 73 71 6c 69 74 65 33 56 64 62 65  that sqlite3Vdbe
1a00d 55 73 65 73 42 74 72 65 65 28 29 20 77 61 73 20  UsesBtree() was 
1a00e 6e 6f 74 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68  not called on th
1a00f 69 73 20 56 4d 20 2a 2f 0a 20 20 61 73 73 65 72  is VM */.  asser
1a010 74 28 20 70 2d 3e 61 4d 75 74 65 78 2e 6e 4d 75  t( p->aMutex.nMu
1a011 74 65 78 3d 3d 30 20 29 3b 0a 0a 20 20 72 65 73  tex==0 );..  res
1a012 6f 6c 76 65 50 32 56 61 6c 75 65 73 28 70 2c 20  olveP2Values(p, 
1a013 70 6e 4d 61 78 41 72 67 29 3b 0a 20 20 2a 70 6e  pnMaxArg);.  *pn
1a014 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 70  Op = p->nOp;.  p
1a015 2d 3e 61 4f 70 20 3d 20 30 3b 0a 20 20 72 65 74  ->aOp = 0;.  ret
1a016 75 72 6e 20 61 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  urn aOp;.}../*.*
1a017 2a 20 41 64 64 20 61 20 77 68 6f 6c 65 20 6c 69  * Add a whole li
1a018 73 74 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73  st of operations
1a019 20 74 6f 20 74 68 65 20 6f 70 65 72 61 74 69 6f   to the operatio
1a01a 6e 20 73 74 61 63 6b 2e 20 20 52 65 74 75 72 6e  n stack.  Return
1a01b 20 74 68 65 0a 2a 2a 20 61 64 64 72 65 73 73 20   the.** address 
1a01c 6f 66 20 74 68 65 20 66 69 72 73 74 20 6f 70 65  of the first ope
1a01d 72 61 74 69 6f 6e 20 61 64 64 65 64 2e 0a 2a 2f  ration added..*/
1a01e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a01f 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
1a020 64 64 4f 70 4c 69 73 74 28 56 64 62 65 20 2a 70  ddOpList(Vdbe *p
1a021 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62 65 4f  , int nOp, VdbeO
1a022 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61 4f 70  pList const *aOp
1a023 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 20  ){.  int addr;. 
1a024 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69   assert( p->magi
1a025 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e  c==VDBE_MAGIC_IN
1a026 49 54 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e  IT );.  if( p->n
1a027 4f 70 20 2b 20 6e 4f 70 20 3e 20 70 2d 3e 6e 4f  Op + nOp > p->nO
1a028 70 41 6c 6c 6f 63 20 26 26 20 67 72 6f 77 4f 70  pAlloc && growOp
1a029 41 72 72 61 79 28 70 29 20 29 7b 0a 20 20 20 20  Array(p) ){.    
1a02a 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
1a02b 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20  addr = p->nOp;. 
1a02c 20 69 66 28 20 41 4c 57 41 59 53 28 6e 4f 70 3e   if( ALWAYS(nOp>
1a02d 30 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  0) ){.    int i;
1a02e 0a 20 20 20 20 56 64 62 65 4f 70 4c 69 73 74 20  .    VdbeOpList 
1a02f 63 6f 6e 73 74 20 2a 70 49 6e 20 3d 20 61 4f 70  const *pIn = aOp
1a030 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
1a031 3c 6e 4f 70 3b 20 69 2b 2b 2c 20 70 49 6e 2b 2b  <nOp; i++, pIn++
1a032 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 32 20  ){.      int p2 
1a033 3d 20 70 49 6e 2d 3e 70 32 3b 0a 20 20 20 20 20  = pIn->p2;.     
1a034 20 56 64 62 65 4f 70 20 2a 70 4f 75 74 20 3d 20   VdbeOp *pOut = 
1a035 26 70 2d 3e 61 4f 70 5b 69 2b 61 64 64 72 5d 3b  &p->aOp[i+addr];
1a036 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 6f 70 63  .      pOut->opc
1a037 6f 64 65 20 3d 20 70 49 6e 2d 3e 6f 70 63 6f 64  ode = pIn->opcod
1a038 65 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70  e;.      pOut->p
1a039 31 20 3d 20 70 49 6e 2d 3e 70 31 3b 0a 20 20 20  1 = pIn->p1;.   
1a03a 20 20 20 69 66 28 20 70 32 3c 30 20 26 26 20 73     if( p2<0 && s
1a03b 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65  qlite3VdbeOpcode
1a03c 48 61 73 50 72 6f 70 65 72 74 79 28 70 4f 75 74  HasProperty(pOut
1a03d 2d 3e 6f 70 63 6f 64 65 2c 20 4f 50 46 4c 47 5f  ->opcode, OPFLG_
1a03e 4a 55 4d 50 29 20 29 7b 0a 20 20 20 20 20 20 20  JUMP) ){.       
1a03f 20 70 4f 75 74 2d 3e 70 32 20 3d 20 61 64 64 72   pOut->p2 = addr
1a040 20 2b 20 41 44 44 52 28 70 32 29 3b 0a 20 20 20   + ADDR(p2);.   
1a041 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1a042 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 70 32 3b    pOut->p2 = p2;
1a043 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
1a044 4f 75 74 2d 3e 70 33 20 3d 20 70 49 6e 2d 3e 70  Out->p3 = pIn->p
1a045 33 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70  3;.      pOut->p
1a046 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53  4type = P4_NOTUS
1a047 45 44 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e  ED;.      pOut->
1a048 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20 20 20  p4.p = 0;.      
1a049 70 4f 75 74 2d 3e 70 35 20 3d 20 30 3b 0a 23 69  pOut->p5 = 0;.#i
1a04a 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
1a04b 47 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 7a 43  G.      pOut->zC
1a04c 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20  omment = 0;.    
1a04d 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
1a04e 65 41 64 64 6f 70 54 72 61 63 65 20 29 7b 0a 20  eAddopTrace ){. 
1a04f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1a050 62 65 50 72 69 6e 74 4f 70 28 30 2c 20 69 2b 61  bePrintOp(0, i+a
1a051 64 64 72 2c 20 26 70 2d 3e 61 4f 70 5b 69 2b 61  ddr, &p->aOp[i+a
1a052 64 64 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 23  ddr]);.      }.#
1a053 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20  endif.    }.    
1a054 70 2d 3e 6e 4f 70 20 2b 3d 20 6e 4f 70 3b 0a 20  p->nOp += nOp;. 
1a055 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72   }.  return addr
1a056 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  ;.}../*.** Chang
1a057 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  e the value of t
1a058 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 66 6f  he P1 operand fo
1a059 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e 73  r a specific ins
1a05a 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69  truction..** Thi
1a05b 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
1a05c 66 75 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 65  ful when a large
1a05d 20 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64   program is load
1a05e 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61  ed from a.** sta
1a05f 74 69 63 20 61 72 72 61 79 20 75 73 69 6e 67 20  tic array using 
1a060 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1a061 4c 69 73 74 20 62 75 74 20 77 65 20 77 61 6e 74  List but we want
1a062 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65   to make a.** fe
1a063 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20  w minor changes 
1a064 74 6f 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a  to the program..
1a065 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1a066 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
1a067 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 65 20  beChangeP1(Vdbe 
1a068 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e  *p, int addr, in
1a069 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74  t val){.  assert
1a06a 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  ( p!=0 );.  asse
1a06b 72 74 28 20 61 64 64 72 3e 3d 30 20 29 3b 0a 20  rt( addr>=0 );. 
1a06c 20 69 66 28 20 70 2d 3e 6e 4f 70 3e 61 64 64 72   if( p->nOp>addr
1a06d 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 61   ){.    p->aOp[a
1a06e 64 64 72 5d 2e 70 31 20 3d 20 76 61 6c 3b 0a 20  ddr].p1 = val;. 
1a06f 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e   }.}../*.** Chan
1a070 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ge the value of 
1a071 74 68 65 20 50 32 20 6f 70 65 72 61 6e 64 20 66  the P2 operand f
1a072 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e  or a specific in
1a073 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68  struction..** Th
1a074 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
1a075 65 66 75 6c 20 66 6f 72 20 73 65 74 74 69 6e 67  eful for setting
1a076 20 61 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 74   a jump destinat
1a077 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
1a078 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1a079 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
1a07a 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64  Vdbe *p, int add
1a07b 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61  r, int val){.  a
1a07c 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20  ssert( p!=0 );. 
1a07d 20 61 73 73 65 72 74 28 20 61 64 64 72 3e 3d 30   assert( addr>=0
1a07e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70   );.  if( p->nOp
1a07f 3e 61 64 64 72 20 29 7b 0a 20 20 20 20 70 2d 3e  >addr ){.    p->
1a080 61 4f 70 5b 61 64 64 72 5d 2e 70 32 20 3d 20 76  aOp[addr].p2 = v
1a081 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  al;.  }.}../*.**
1a082 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75   Change the valu
1a083 65 20 6f 66 20 74 68 65 20 50 33 20 6f 70 65 72  e of the P3 oper
1a084 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66  and for a specif
1a085 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  ic instruction..
1a086 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1a087 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
1a088 62 65 43 68 61 6e 67 65 50 33 28 56 64 62 65 20  beChangeP3(Vdbe 
1a089 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e  *p, int addr, in
1a08a 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74  t val){.  assert
1a08b 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  ( p!=0 );.  asse
1a08c 72 74 28 20 61 64 64 72 3e 3d 30 20 29 3b 0a 20  rt( addr>=0 );. 
1a08d 20 69 66 28 20 70 2d 3e 6e 4f 70 3e 61 64 64 72   if( p->nOp>addr
1a08e 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 61   ){.    p->aOp[a
1a08f 64 64 72 5d 2e 70 33 20 3d 20 76 61 6c 3b 0a 20  ddr].p3 = val;. 
1a090 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e   }.}../*.** Chan
1a091 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ge the value of 
1a092 74 68 65 20 50 35 20 6f 70 65 72 61 6e 64 20 66  the P5 operand f
1a093 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  or the most rece
1a094 6e 74 6c 79 0a 2a 2a 20 61 64 64 65 64 20 6f 70  ntly.** added op
1a095 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  eration..*/.SQLI
1a096 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1a097 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1a098 65 50 35 28 56 64 62 65 20 2a 70 2c 20 75 38 20  eP5(Vdbe *p, u8 
1a099 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20  val){.  assert( 
1a09a 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d  p!=0 );.  if( p-
1a09b 3e 61 4f 70 20 29 7b 0a 20 20 20 20 61 73 73 65  >aOp ){.    asse
1a09c 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a  rt( p->nOp>0 );.
1a09d 20 20 20 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f      p->aOp[p->nO
1a09e 70 2d 31 5d 2e 70 35 20 3d 20 76 61 6c 3b 0a 20  p-1].p5 = val;. 
1a09f 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e   }.}../*.** Chan
1a0a0 67 65 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e  ge the P2 operan
1a0a1 64 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e  d of instruction
1a0a2 20 61 64 64 72 20 73 6f 20 74 68 61 74 20 69 74   addr so that it
1a0a3 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68   points to.** th
1a0a4 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65  e address of the
1a0a5 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   next instructio
1a0a6 6e 20 74 6f 20 62 65 20 63 6f 64 65 64 2e 0a 2a  n to be coded..*
1a0a7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1a0a8 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
1a0a9 65 4a 75 6d 70 48 65 72 65 28 56 64 62 65 20 2a  eJumpHere(Vdbe *
1a0aa 70 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a 20 20  p, int addr){.  
1a0ab 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1a0ac 65 50 32 28 70 2c 20 61 64 64 72 2c 20 70 2d 3e  eP2(p, addr, p->
1a0ad 6e 4f 70 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  nOp);.}.../*.** 
1a0ae 49 66 20 74 68 65 20 69 6e 70 75 74 20 46 75 6e  If the input Fun
1a0af 63 44 65 66 20 73 74 72 75 63 74 75 72 65 20 69  cDef structure i
1a0b0 73 20 65 70 68 65 6d 65 72 61 6c 2c 20 74 68 65  s ephemeral, the
1a0b1 6e 20 66 72 65 65 20 69 74 2e 20 20 49 66 0a 2a  n free it.  If.*
1a0b2 2a 20 74 68 65 20 46 75 6e 63 44 65 66 20 69 73  * the FuncDef is
1a0b3 20 6e 6f 74 20 65 70 68 65 72 6d 61 6c 2c 20 74   not ephermal, t
1a0b4 68 65 6e 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 0a  hen do nothing..
1a0b5 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
1a0b6 72 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63  reeEphemeralFunc
1a0b7 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62  tion(sqlite3 *db
1a0b8 2c 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 29  , FuncDef *pDef)
1a0b9 7b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70  {.  if( ALWAYS(p
1a0ba 44 65 66 29 20 26 26 20 28 70 44 65 66 2d 3e 66  Def) && (pDef->f
1a0bb 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55  lags & SQLITE_FU
1a0bc 4e 43 5f 45 50 48 45 4d 29 21 3d 30 20 29 7b 0a  NC_EPHEM)!=0 ){.
1a0bd 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1a0be 65 28 64 62 2c 20 70 44 65 66 29 3b 0a 20 20 7d  e(db, pDef);.  }
1a0bf 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
1a0c0 20 61 20 50 34 20 76 61 6c 75 65 20 69 66 20 6e   a P4 value if n
1a0c1 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61  ecessary..*/.sta
1a0c2 74 69 63 20 76 6f 69 64 20 66 72 65 65 50 34 28  tic void freeP4(
1a0c3 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
1a0c4 20 70 34 74 79 70 65 2c 20 76 6f 69 64 20 2a 70   p4type, void *p
1a0c5 34 29 7b 0a 20 20 69 66 28 20 70 34 20 29 7b 0a  4){.  if( p4 ){.
1a0c6 20 20 20 20 73 77 69 74 63 68 28 20 70 34 74 79      switch( p4ty
1a0c7 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65  pe ){.      case
1a0c8 20 50 34 5f 52 45 41 4c 3a 0a 20 20 20 20 20 20   P4_REAL:.      
1a0c9 63 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 0a 20  case P4_INT64:. 
1a0ca 20 20 20 20 20 63 61 73 65 20 50 34 5f 4d 50 52       case P4_MPR
1a0cb 49 4e 54 46 3a 0a 20 20 20 20 20 20 63 61 73 65  INTF:.      case
1a0cc 20 50 34 5f 44 59 4e 41 4d 49 43 3a 0a 20 20 20   P4_DYNAMIC:.   
1a0cd 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e     case P4_KEYIN
1a0ce 46 4f 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50  FO:.      case P
1a0cf 34 5f 49 4e 54 41 52 52 41 59 3a 0a 20 20 20 20  4_INTARRAY:.    
1a0d0 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46    case P4_KEYINF
1a0d1 4f 5f 48 41 4e 44 4f 46 46 3a 20 7b 0a 20 20 20  O_HANDOFF: {.   
1a0d2 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1a0d3 65 65 28 64 62 2c 20 70 34 29 3b 0a 20 20 20 20  ee(db, p4);.    
1a0d4 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1a0d5 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34   }.      case P4
1a0d6 5f 56 44 42 45 46 55 4e 43 3a 20 7b 0a 20 20 20  _VDBEFUNC: {.   
1a0d7 20 20 20 20 20 56 64 62 65 46 75 6e 63 20 2a 70       VdbeFunc *p
1a0d8 56 64 62 65 46 75 6e 63 20 3d 20 28 56 64 62 65  VdbeFunc = (Vdbe
1a0d9 46 75 6e 63 20 2a 29 70 34 3b 0a 20 20 20 20 20  Func *)p4;.     
1a0da 20 20 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c     freeEphemeral
1a0db 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 56 64  Function(db, pVd
1a0dc 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 29 3b 0a  beFunc->pFunc);.
1a0dd 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1a0de 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61  dbeDeleteAuxData
1a0df 28 70 56 64 62 65 46 75 6e 63 2c 20 30 29 3b 0a  (pVdbeFunc, 0);.
1a0e0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
1a0e1 62 46 72 65 65 28 64 62 2c 20 70 56 64 62 65 46  bFree(db, pVdbeF
1a0e2 75 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 62 72  unc);.        br
1a0e3 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
1a0e4 20 20 20 63 61 73 65 20 50 34 5f 46 55 4e 43 44     case P4_FUNCD
1a0e5 45 46 3a 20 7b 0a 20 20 20 20 20 20 20 20 66 72  EF: {.        fr
1a0e6 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 74  eeEphemeralFunct
1a0e7 69 6f 6e 28 64 62 2c 20 28 46 75 6e 63 44 65 66  ion(db, (FuncDef
1a0e8 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62  *)p4);.        b
1a0e9 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
1a0ea 20 20 20 20 63 61 73 65 20 50 34 5f 4d 45 4d 3a      case P4_MEM:
1a0eb 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   {.        sqlit
1a0ec 65 33 56 61 6c 75 65 46 72 65 65 28 28 73 71 6c  e3ValueFree((sql
1a0ed 69 74 65 33 5f 76 61 6c 75 65 2a 29 70 34 29 3b  ite3_value*)p4);
1a0ee 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
1a0ef 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
1a0f0 73 65 20 50 34 5f 56 54 41 42 20 3a 20 7b 0a 20  se P4_VTAB : {. 
1a0f1 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 74         sqlite3Vt
1a0f2 61 62 55 6e 6c 6f 63 6b 28 28 56 54 61 62 6c 65  abUnlock((VTable
1a0f3 20 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20   *)p4);.        
1a0f4 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
1a0f5 20 20 20 20 20 63 61 73 65 20 50 34 5f 53 55 42       case P4_SUB
1a0f6 50 52 4f 47 52 41 4d 20 3a 20 7b 0a 20 20 20 20  PROGRAM : {.    
1a0f7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50      sqlite3VdbeP
1a0f8 72 6f 67 72 61 6d 44 65 6c 65 74 65 28 64 62 2c  rogramDelete(db,
1a0f9 20 28 53 75 62 50 72 6f 67 72 61 6d 20 2a 29 70   (SubProgram *)p
1a0fa 34 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 62  4, 1);.        b
1a0fb 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
1a0fc 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
1a0fd 20 46 72 65 65 20 74 68 65 20 73 70 61 63 65 20   Free the space 
1a0fe 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 4f  allocated for aO
1a0ff 70 20 61 6e 64 20 61 6e 79 20 70 34 20 76 61 6c  p and any p4 val
1a100 75 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f  ues allocated fo
1a101 72 20 74 68 65 0a 2a 2a 20 6f 70 63 6f 64 65 73  r the.** opcodes
1a102 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69   contained withi
1a103 6e 2e 20 49 66 20 61 4f 70 20 69 73 20 6e 6f 74  n. If aOp is not
1a104 20 4e 55 4c 4c 20 69 74 20 69 73 20 61 73 73 75   NULL it is assu
1a105 6d 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 0a  med to contain .
1a106 2a 2a 20 6e 4f 70 20 65 6e 74 72 69 65 73 2e 20  ** nOp entries. 
1a107 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1a108 76 64 62 65 46 72 65 65 4f 70 41 72 72 61 79 28  vdbeFreeOpArray(
1a109 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 4f 70 20  sqlite3 *db, Op 
1a10a 2a 61 4f 70 2c 20 69 6e 74 20 6e 4f 70 29 7b 0a  *aOp, int nOp){.
1a10b 20 20 69 66 28 20 61 4f 70 20 29 7b 0a 20 20 20    if( aOp ){.   
1a10c 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 66 6f   Op *pOp;.    fo
1a10d 72 28 70 4f 70 3d 61 4f 70 3b 20 70 4f 70 3c 26  r(pOp=aOp; pOp<&
1a10e 61 4f 70 5b 6e 4f 70 5d 3b 20 70 4f 70 2b 2b 29  aOp[nOp]; pOp++)
1a10f 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34 28 64  {.      freeP4(d
1a110 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20  b, pOp->p4type, 
1a111 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 23 69 66 64  pOp->p4.p);.#ifd
1a112 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
1a113 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
1a114 72 65 65 28 64 62 2c 20 70 4f 70 2d 3e 7a 43 6f  ree(db, pOp->zCo
1a115 6d 6d 65 6e 74 29 3b 0a 23 65 6e 64 69 66 20 20  mment);.#endif  
1a116 20 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20     .    }.  }.  
1a117 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1a118 2c 20 61 4f 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  , aOp);.}../*.**
1a119 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72   Decrement the r
1a11a 65 66 2d 63 6f 75 6e 74 20 6f 6e 20 74 68 65 20  ef-count on the 
1a11b 53 75 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63  SubProgram struc
1a11c 74 75 72 65 20 70 61 73 73 65 64 20 61 73 20 74  ture passed as t
1a11d 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 61 72 67  he.** second arg
1a11e 75 6d 65 6e 74 2e 20 49 66 20 74 68 65 20 72 65  ument. If the re
1a11f 66 2d 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20  f-count reaches 
1a120 7a 65 72 6f 2c 20 66 72 65 65 20 74 68 65 20 73  zero, free the s
1a121 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
1a122 54 68 65 20 61 72 72 61 79 20 6f 66 20 56 44 42  The array of VDB
1a123 45 20 6f 70 63 6f 64 65 73 20 73 74 6f 72 65 64  E opcodes stored
1a124 20 61 73 20 53 75 62 50 72 6f 67 72 61 6d 2e 61   as SubProgram.a
1a125 4f 70 20 69 73 20 66 72 65 65 64 20 69 66 0a 2a  Op is freed if.*
1a126 2a 20 65 69 74 68 65 72 20 74 68 65 20 72 65 66  * either the ref
1a127 2d 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a  -count reaches z
1a128 65 72 6f 20 6f 72 20 70 61 72 61 6d 65 74 65 72  ero or parameter
1a129 20 66 72 65 65 6f 70 20 69 73 20 6e 6f 6e 2d 7a   freeop is non-z
1a12a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65  ero..**.** Since
1a12b 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 6f 70   the array of op
1a12c 63 6f 64 65 73 20 70 6f 69 6e 74 65 64 20 74 6f  codes pointed to
1a12d 20 62 79 20 53 75 62 50 72 6f 67 72 61 6d 2e 61   by SubProgram.a
1a12e 4f 70 20 6d 61 79 20 64 69 72 65 63 74 6c 79 0a  Op may directly.
1a12f 2a 2a 20 6f 72 20 69 6e 64 69 72 65 63 74 6c 79  ** or indirectly
1a130 20 63 6f 6e 74 61 69 6e 20 61 20 72 65 66 65 72   contain a refer
1a131 65 6e 63 65 20 74 6f 20 74 68 65 20 53 75 62 50  ence to the SubP
1a132 72 6f 67 72 61 6d 20 73 74 72 75 63 74 75 72 65  rogram structure
1a133 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 42 79 20 70   itself..** By p
1a134 61 73 73 69 6e 67 20 61 20 6e 6f 6e 2d 7a 65 72  assing a non-zer
1a135 6f 20 66 72 65 65 6f 70 20 70 61 72 61 6d 65 74  o freeop paramet
1a136 65 72 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 6d  er, the caller m
1a137 61 79 20 65 6e 73 75 72 65 20 74 68 61 74 20 61  ay ensure that a
1a138 6c 6c 0a 2a 2a 20 53 75 62 50 72 6f 67 72 61 6d  ll.** SubProgram
1a139 20 73 74 72 75 63 74 75 72 65 73 20 61 6e 64 20   structures and 
1a13a 74 68 65 69 72 20 61 4f 70 20 61 72 72 61 79 73  their aOp arrays
1a13b 20 61 72 65 20 66 72 65 65 64 2c 20 65 76 65 6e   are freed, even
1a13c 20 77 68 65 6e 20 74 68 65 72 65 0a 2a 2a 20 61   when there.** a
1a13d 72 65 20 73 75 63 68 20 63 69 72 63 75 6c 61 72  re such circular
1a13e 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2f 0a   references..*/.
1a13f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1a140 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 50  oid sqlite3VdbeP
1a141 72 6f 67 72 61 6d 44 65 6c 65 74 65 28 73 71 6c  rogramDelete(sql
1a142 69 74 65 33 20 2a 64 62 2c 20 53 75 62 50 72 6f  ite3 *db, SubPro
1a143 67 72 61 6d 20 2a 70 2c 20 69 6e 74 20 66 72 65  gram *p, int fre
1a144 65 6f 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b  eop){.  if( p ){
1a145 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
1a146 6e 52 65 66 3e 30 20 29 3b 0a 20 20 20 20 69 66  nRef>0 );.    if
1a147 28 20 66 72 65 65 6f 70 20 7c 7c 20 70 2d 3e 6e  ( freeop || p->n
1a148 52 65 66 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  Ref==1 ){.      
1a149 4f 70 20 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f 70  Op *aOp = p->aOp
1a14a 3b 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70 20 3d  ;.      p->aOp =
1a14b 20 30 3b 0a 20 20 20 20 20 20 76 64 62 65 46 72   0;.      vdbeFr
1a14c 65 65 4f 70 41 72 72 61 79 28 64 62 2c 20 61 4f  eeOpArray(db, aO
1a14d 70 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 20 20 20 20  p, p->nOp);.    
1a14e 20 20 70 2d 3e 6e 4f 70 20 3d 20 30 3b 0a 20 20    p->nOp = 0;.  
1a14f 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2d    }.    p->nRef-
1a150 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52  -;.    if( p->nR
1a151 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  ef==0 ){.      s
1a152 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1a153 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d   p);.    }.  }.}
1a154 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  .../*.** Change 
1a155 4e 20 6f 70 63 6f 64 65 73 20 73 74 61 72 74 69  N opcodes starti
1a156 6e 67 20 61 74 20 61 64 64 72 20 74 6f 20 4e 6f  ng at addr to No
1a157 2d 6f 70 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  -ops..*/.SQLITE_
1a158 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1a159 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f  ite3VdbeChangeTo
1a15a 4e 6f 6f 70 28 56 64 62 65 20 2a 70 2c 20 69 6e  Noop(Vdbe *p, in
1a15b 74 20 61 64 64 72 2c 20 69 6e 74 20 4e 29 7b 0a  t addr, int N){.
1a15c 20 20 69 66 28 20 70 2d 3e 61 4f 70 20 29 7b 0a    if( p->aOp ){.
1a15d 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 20      VdbeOp *pOp 
1a15e 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b  = &p->aOp[addr];
1a15f 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62  .    sqlite3 *db
1a160 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 77 68   = p->db;.    wh
1a161 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20  ile( N-- ){.    
1a162 20 20 66 72 65 65 50 34 28 64 62 2c 20 70 4f 70    freeP4(db, pOp
1a163 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70 2d 3e 70  ->p4type, pOp->p
1a164 34 2e 70 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73  4.p);.      mems
1a165 65 74 28 70 4f 70 2c 20 30 2c 20 73 69 7a 65 6f  et(pOp, 0, sizeo
1a166 66 28 70 4f 70 5b 30 5d 29 29 3b 0a 20 20 20 20  f(pOp[0]));.    
1a167 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20    pOp->opcode = 
1a168 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 20 20 70  OP_Noop;.      p
1a169 4f 70 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Op++;.    }.  }.
1a16a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  }../*.** Change 
1a16b 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
1a16c 20 50 34 20 6f 70 65 72 61 6e 64 20 66 6f 72 20   P4 operand for 
1a16d 61 20 73 70 65 63 69 66 69 63 20 69 6e 73 74 72  a specific instr
1a16e 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20  uction..** This 
1a16f 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 66 75  routine is usefu
1a170 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 65 20 70  l when a large p
1a171 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64 65 64  rogram is loaded
1a172 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 74 69   from a.** stati
1a173 63 20 61 72 72 61 79 20 75 73 69 6e 67 20 73 71  c array using sq
1a174 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69  lite3VdbeAddOpLi
1a175 73 74 20 62 75 74 20 77 65 20 77 61 6e 74 20 74  st but we want t
1a176 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 77 20  o make a.** few 
1a177 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f  minor changes to
1a178 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a   the program..**
1a179 0a 2a 2a 20 49 66 20 6e 3e 3d 30 20 74 68 65 6e  .** If n>=0 then
1a17a 20 74 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20   the P4 operand 
1a17b 69 73 20 64 79 6e 61 6d 69 63 2c 20 6d 65 61 6e  is dynamic, mean
1a17c 69 6e 67 20 74 68 61 74 20 61 20 63 6f 70 79 20  ing that a copy 
1a17d 6f 66 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67  of.** the string
1a17e 20 69 73 20 6d 61 64 65 20 69 6e 74 6f 20 6d 65   is made into me
1a17f 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
1a180 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  om sqlite3_mallo
1a181 63 28 29 2e 0a 2a 2a 20 41 20 76 61 6c 75 65 20  c()..** A value 
1a182 6f 66 20 6e 3d 3d 30 20 6d 65 61 6e 73 20 63 6f  of n==0 means co
1a183 70 79 20 62 79 74 65 73 20 6f 66 20 7a 50 34 20  py bytes of zP4 
1a184 75 70 20 74 6f 20 61 6e 64 20 69 6e 63 6c 75 64  up to and includ
1a185 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 72 73 74  ing the.** first
1a186 20 6e 75 6c 6c 20 62 79 74 65 2e 20 20 49 66 20   null byte.  If 
1a187 6e 3e 30 20 74 68 65 6e 20 63 6f 70 79 20 6e 2b  n>0 then copy n+
1a188 31 20 62 79 74 65 73 20 6f 66 20 7a 50 34 2e 0a  1 bytes of zP4..
1a189 2a 2a 0a 2a 2a 20 49 66 20 6e 3d 3d 50 34 5f 4b  **.** If n==P4_K
1a18a 45 59 49 4e 46 4f 20 69 74 20 6d 65 61 6e 73 20  EYINFO it means 
1a18b 74 68 61 74 20 7a 50 34 20 69 73 20 61 20 70 6f  that zP4 is a po
1a18c 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e  inter to a KeyIn
1a18d 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  fo structure..**
1a18e 20 41 20 63 6f 70 79 20 69 73 20 6d 61 64 65 20   A copy is made 
1a18f 6f 66 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73  of the KeyInfo s
1a190 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 6d 65  tructure into me
1a191 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
1a192 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61  om.** sqlite3_ma
1a193 6c 6c 6f 63 2c 20 74 6f 20 62 65 20 66 72 65 65  lloc, to be free
1a194 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20  d when the Vdbe 
1a195 69 73 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a  is finalized..**
1a196 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f 48   n==P4_KEYINFO_H
1a197 41 4e 44 4f 46 46 20 69 6e 64 69 63 61 74 65 73  ANDOFF indicates
1a198 20 74 68 61 74 20 7a 50 34 20 70 6f 69 6e 74 73   that zP4 points
1a199 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74   to a KeyInfo st
1a19a 72 75 63 74 75 72 65 0a 2a 2a 20 73 74 6f 72 65  ructure.** store
1a19b 64 20 69 6e 20 6d 65 6d 6f 72 79 20 74 68 61 74  d in memory that
1a19c 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20   the caller has 
1a19d 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
1a19e 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2e 20 54 68  lite3_malloc. Th
1a19f 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 68 6f  e .** caller sho
1a1a0 75 6c 64 20 6e 6f 74 20 66 72 65 65 20 74 68 65  uld not free the
1a1a1 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74 20   allocation, it 
1a1a2 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20 77 68  will be freed wh
1a1a3 65 6e 20 74 68 65 20 56 64 62 65 20 69 73 0a 2a  en the Vdbe is.*
1a1a4 2a 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20  * finalized..** 
1a1a5 0a 2a 2a 20 4f 74 68 65 72 20 76 61 6c 75 65 73  .** Other values
1a1a6 20 6f 66 20 6e 20 28 50 34 5f 53 54 41 54 49 43   of n (P4_STATIC
1a1a7 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 20 65 74 63  , P4_COLLSEQ etc
1a1a8 2e 29 20 69 6e 64 69 63 61 74 65 20 74 68 61 74  .) indicate that
1a1a9 20 7a 50 34 20 70 6f 69 6e 74 73 0a 2a 2a 20 74   zP4 points.** t
1a1aa 6f 20 61 20 73 74 72 69 6e 67 20 6f 72 20 73 74  o a string or st
1a1ab 72 75 63 74 75 72 65 20 74 68 61 74 20 69 73 20  ructure that is 
1a1ac 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78  guaranteed to ex
1a1ad 69 73 74 20 66 6f 72 20 74 68 65 20 6c 69 66 65  ist for the life
1a1ae 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 56  time of.** the V
1a1af 64 62 65 2e 20 49 6e 20 74 68 65 73 65 20 63 61  dbe. In these ca
1a1b0 73 65 73 20 77 65 20 63 61 6e 20 6a 75 73 74 20  ses we can just 
1a1b1 63 6f 70 79 20 74 68 65 20 70 6f 69 6e 74 65 72  copy the pointer
1a1b2 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 64 64 72 3c  ..**.** If addr<
1a1b3 30 20 74 68 65 6e 20 63 68 61 6e 67 65 20 50 34  0 then change P4
1a1b4 20 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63   on the most rec
1a1b5 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64 20 69  ently inserted i
1a1b6 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53  nstruction..*/.S
1a1b7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1a1b8 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  id sqlite3VdbeCh
1a1b9 61 6e 67 65 50 34 28 56 64 62 65 20 2a 70 2c 20  angeP4(Vdbe *p, 
1a1ba 69 6e 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 20  int addr, const 
1a1bb 63 68 61 72 20 2a 7a 50 34 2c 20 69 6e 74 20 6e  char *zP4, int n
1a1bc 29 7b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20  ){.  Op *pOp;.  
1a1bd 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 61  sqlite3 *db;.  a
1a1be 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20  ssert( p!=0 );. 
1a1bf 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 61   db = p->db;.  a
1a1c0 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d  ssert( p->magic=
1a1c1 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54  =VDBE_MAGIC_INIT
1a1c2 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70   );.  if( p->aOp
1a1c3 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  ==0 || db->mallo
1a1c4 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 69  cFailed ){.    i
1a1c5 66 20 28 20 6e 21 3d 50 34 5f 4b 45 59 49 4e 46  f ( n!=P4_KEYINF
1a1c6 4f 20 26 26 20 6e 21 3d 50 34 5f 56 54 41 42 20  O && n!=P4_VTAB 
1a1c7 29 20 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34  ) {.      freeP4
1a1c8 28 64 62 2c 20 6e 2c 20 28 76 6f 69 64 2a 29 2a  (db, n, (void*)*
1a1c9 28 63 68 61 72 2a 2a 29 26 7a 50 34 29 3b 0a 20  (char**)&zP4);. 
1a1ca 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 3b     }.    return;
1a1cb 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
1a1cc 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 20 20 61 73 73  ->nOp>0 );.  ass
1a1cd 65 72 74 28 20 61 64 64 72 3c 70 2d 3e 6e 4f 70  ert( addr<p->nOp
1a1ce 20 29 3b 0a 20 20 69 66 28 20 61 64 64 72 3c 30   );.  if( addr<0
1a1cf 20 29 7b 0a 20 20 20 20 61 64 64 72 20 3d 20 70   ){.    addr = p
1a1d0 2d 3e 6e 4f 70 20 2d 20 31 3b 0a 20 20 7d 0a 20  ->nOp - 1;.  }. 
1a1d1 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61   pOp = &p->aOp[a
1a1d2 64 64 72 5d 3b 0a 20 20 66 72 65 65 50 34 28 64  ddr];.  freeP4(d
1a1d3 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20  b, pOp->p4type, 
1a1d4 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20 20 70 4f  pOp->p4.p);.  pO
1a1d5 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 69  p->p4.p = 0;.  i
1a1d6 66 28 20 6e 3d 3d 50 34 5f 49 4e 54 33 32 20 29  f( n==P4_INT32 )
1a1d7 7b 0a 20 20 20 20 2f 2a 20 4e 6f 74 65 3a 20 74  {.    /* Note: t
1a1d8 68 69 73 20 63 61 73 74 20 69 73 20 73 61 66 65  his cast is safe
1a1d9 2c 20 62 65 63 61 75 73 65 20 74 68 65 20 6f 72  , because the or
1a1da 69 67 69 6e 20 64 61 74 61 20 70 6f 69 6e 74 20  igin data point 
1a1db 77 61 73 20 61 6e 20 69 6e 74 0a 20 20 20 20 2a  was an int.    *
1a1dc 2a 20 74 68 61 74 20 77 61 73 20 63 61 73 74 20  * that was cast 
1a1dd 74 6f 20 61 20 28 63 6f 6e 73 74 20 63 68 61 72  to a (const char
1a1de 20 2a 29 2e 20 2a 2f 0a 20 20 20 20 70 4f 70 2d   *). */.    pOp-
1a1df 3e 70 34 2e 69 20 3d 20 53 51 4c 49 54 45 5f 50  >p4.i = SQLITE_P
1a1e0 54 52 5f 54 4f 5f 49 4e 54 28 7a 50 34 29 3b 0a  TR_TO_INT(zP4);.
1a1e1 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20      pOp->p4type 
1a1e2 3d 20 50 34 5f 49 4e 54 33 32 3b 0a 20 20 7d 65  = P4_INT32;.  }e
1a1e3 6c 73 65 20 69 66 28 20 7a 50 34 3d 3d 30 20 29  lse if( zP4==0 )
1a1e4 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20  {.    pOp->p4.p 
1a1e5 3d 20 30 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34  = 0;.    pOp->p4
1a1e6 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45  type = P4_NOTUSE
1a1e7 44 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e  D;.  }else if( n
1a1e8 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 29 7b 0a  ==P4_KEYINFO ){.
1a1e9 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65      KeyInfo *pKe
1a1ea 79 49 6e 66 6f 3b 0a 20 20 20 20 69 6e 74 20 6e  yInfo;.    int n
1a1eb 46 69 65 6c 64 2c 20 6e 42 79 74 65 3b 0a 0a 20  Field, nByte;.. 
1a1ec 20 20 20 6e 46 69 65 6c 64 20 3d 20 28 28 4b 65     nField = ((Ke
1a1ed 79 49 6e 66 6f 2a 29 7a 50 34 29 2d 3e 6e 46 69  yInfo*)zP4)->nFi
1a1ee 65 6c 64 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d  eld;.    nByte =
1a1ef 20 73 69 7a 65 6f 66 28 2a 70 4b 65 79 49 6e 66   sizeof(*pKeyInf
1a1f0 6f 29 20 2b 20 28 6e 46 69 65 6c 64 2d 31 29 2a  o) + (nField-1)*
1a1f1 73 69 7a 65 6f 66 28 70 4b 65 79 49 6e 66 6f 2d  sizeof(pKeyInfo-
1a1f2 3e 61 43 6f 6c 6c 5b 30 5d 29 20 2b 20 6e 46 69  >aColl[0]) + nFi
1a1f3 65 6c 64 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66  eld;.    pKeyInf
1a1f4 6f 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  o = sqlite3Mallo
1a1f5 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 20 20  c( nByte );.    
1a1f6 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f  pOp->p4.pKeyInfo
1a1f7 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20   = pKeyInfo;.   
1a1f8 20 69 66 28 20 70 4b 65 79 49 6e 66 6f 20 29 7b   if( pKeyInfo ){
1a1f9 0a 20 20 20 20 20 20 75 38 20 2a 61 53 6f 72 74  .      u8 *aSort
1a1fa 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 6d 65 6d  Order;.      mem
1a1fb 63 70 79 28 70 4b 65 79 49 6e 66 6f 2c 20 7a 50  cpy(pKeyInfo, zP
1a1fc 34 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20  4, nByte);.     
1a1fd 20 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 70 4b   aSortOrder = pK
1a1fe 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64  eyInfo->aSortOrd
1a1ff 65 72 3b 0a 20 20 20 20 20 20 69 66 28 20 61 53  er;.      if( aS
1a200 6f 72 74 4f 72 64 65 72 20 29 7b 0a 20 20 20 20  ortOrder ){.    
1a201 20 20 20 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53      pKeyInfo->aS
1a202 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 6e 73 69  ortOrder = (unsi
1a203 67 6e 65 64 20 63 68 61 72 2a 29 26 70 4b 65 79  gned char*)&pKey
1a204 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e 46 69 65  Info->aColl[nFie
1a205 6c 64 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d  ld];.        mem
1a206 63 70 79 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 53  cpy(pKeyInfo->aS
1a207 6f 72 74 4f 72 64 65 72 2c 20 61 53 6f 72 74 4f  ortOrder, aSortO
1a208 72 64 65 72 2c 20 6e 46 69 65 6c 64 29 3b 0a 20  rder, nField);. 
1a209 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f 70       }.      pOp
1a20a 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4b 45  ->p4type = P4_KE
1a20b 59 49 4e 46 4f 3b 0a 20 20 20 20 7d 65 6c 73 65  YINFO;.    }else
1a20c 7b 0a 20 20 20 20 20 20 70 2d 3e 64 62 2d 3e 6d  {.      p->db->m
1a20d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
1a20e 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70 34 74 79  .      pOp->p4ty
1a20f 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b  pe = P4_NOTUSED;
1a210 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
1a211 66 28 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f  f( n==P4_KEYINFO
1a212 5f 48 41 4e 44 4f 46 46 20 29 7b 0a 20 20 20 20  _HANDOFF ){.    
1a213 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69  pOp->p4.p = (voi
1a214 64 2a 29 7a 50 34 3b 0a 20 20 20 20 70 4f 70 2d  d*)zP4;.    pOp-
1a215 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4b 45 59  >p4type = P4_KEY
1a216 49 4e 46 4f 3b 0a 20 20 7d 65 6c 73 65 20 69 66  INFO;.  }else if
1a217 28 20 6e 3d 3d 50 34 5f 56 54 41 42 20 29 7b 0a  ( n==P4_VTAB ){.
1a218 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20      pOp->p4.p = 
1a219 28 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20 20  (void*)zP4;.    
1a21a 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34  pOp->p4type = P4
1a21b 5f 56 54 41 42 3b 0a 20 20 20 20 73 71 6c 69 74  _VTAB;.    sqlit
1a21c 65 33 56 74 61 62 4c 6f 63 6b 28 28 56 54 61 62  e3VtabLock((VTab
1a21d 6c 65 20 2a 29 7a 50 34 29 3b 0a 20 20 20 20 61  le *)zP4);.    a
1a21e 73 73 65 72 74 28 20 28 28 56 54 61 62 6c 65 20  ssert( ((VTable 
1a21f 2a 29 7a 50 34 29 2d 3e 64 62 3d 3d 70 2d 3e 64  *)zP4)->db==p->d
1a220 62 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  b );.  }else if(
1a221 20 6e 3c 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d   n<0 ){.    pOp-
1a222 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a  >p4.p = (void*)z
1a223 50 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74  P4;.    pOp->p4t
1a224 79 70 65 20 3d 20 28 73 69 67 6e 65 64 20 63 68  ype = (signed ch
1a225 61 72 29 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ar)n;.  }else{. 
1a226 20 20 20 69 66 28 20 6e 3d 3d 30 20 29 20 6e 20     if( n==0 ) n 
1a227 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
1a228 30 28 7a 50 34 29 3b 0a 20 20 20 20 70 4f 70 2d  0(zP4);.    pOp-
1a229 3e 70 34 2e 7a 20 3d 20 73 71 6c 69 74 65 33 44  >p4.z = sqlite3D
1a22a 62 53 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c 20  bStrNDup(p->db, 
1a22b 7a 50 34 2c 20 6e 29 3b 0a 20 20 20 20 70 4f 70  zP4, n);.    pOp
1a22c 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 44 59  ->p4type = P4_DY
1a22d 4e 41 4d 49 43 3b 0a 20 20 7d 0a 7d 0a 0a 23 69  NAMIC;.  }.}..#i
1a22e 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a  fndef NDEBUG./*.
1a22f 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 63 6f  ** Change the co
1a230 6d 6d 65 6e 74 20 6f 6e 20 74 68 65 20 74 68 65  mment on the the
1a231 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63   most recently c
1a232 6f 64 65 64 20 69 6e 73 74 72 75 63 74 69 6f 6e  oded instruction
1a233 2e 20 20 4f 72 0a 2a 2a 20 69 6e 73 65 72 74 20  .  Or.** insert 
1a234 61 20 4e 6f 2d 6f 70 20 61 6e 64 20 61 64 64 20  a No-op and add 
1a235 74 68 65 20 63 6f 6d 6d 65 6e 74 20 74 6f 20 74  the comment to t
1a236 68 61 74 20 6e 65 77 20 69 6e 73 74 72 75 63 74  hat new instruct
1a237 69 6f 6e 2e 20 20 54 68 69 73 0a 2a 2a 20 6d 61  ion.  This.** ma
1a238 6b 65 73 20 74 68 65 20 63 6f 64 65 20 65 61 73  kes the code eas
1a239 69 65 72 20 74 6f 20 72 65 61 64 20 64 75 72 69  ier to read duri
1a23a 6e 67 20 64 65 62 75 67 67 69 6e 67 2e 20 20 4e  ng debugging.  N
1a23b 6f 6e 65 20 6f 66 20 74 68 69 73 20 68 61 70 70  one of this happ
1a23c 65 6e 73 0a 2a 2a 20 69 6e 20 61 20 70 72 6f 64  ens.** in a prod
1a23d 75 63 74 69 6f 6e 20 62 75 69 6c 64 2e 0a 2a 2f  uction build..*/
1a23e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a23f 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
1a240 43 6f 6d 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c  Comment(Vdbe *p,
1a241 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
1a242 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  rmat, ...){.  va
1a243 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 66 28 20  _list ap;.  if( 
1a244 21 70 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61  !p ) return;.  a
1a245 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20  ssert( p->nOp>0 
1a246 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b 0a  || p->aOp==0 );.
1a247 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4f 70    assert( p->aOp
1a248 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 2d  ==0 || p->aOp[p-
1a249 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74  >nOp-1].zComment
1a24a 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61  ==0 || p->db->ma
1a24b 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
1a24c 69 66 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20  if( p->nOp ){.  
1a24d 20 20 63 68 61 72 20 2a 2a 70 7a 20 3d 20 26 70    char **pz = &p
1a24e 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e  ->aOp[p->nOp-1].
1a24f 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 76 61  zComment;.    va
1a250 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d  _start(ap, zForm
1a251 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  at);.    sqlite3
1a252 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 2a 70  DbFree(p->db, *p
1a253 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 3d 20 73 71  z);.    *pz = sq
1a254 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 70 2d  lite3VMPrintf(p-
1a255 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70  >db, zFormat, ap
1a256 29 3b 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 70  );.    va_end(ap
1a257 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f  );.  }.}.SQLITE_
1a258 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1a259 69 74 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d  ite3VdbeNoopComm
1a25a 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 63 6f 6e  ent(Vdbe *p, con
1a25b 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74  st char *zFormat
1a25c 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73  , ...){.  va_lis
1a25d 74 20 61 70 3b 0a 20 20 69 66 28 20 21 70 20 29  t ap;.  if( !p )
1a25e 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74   return;.  sqlit
1a25f 65 33 56 64 62 65 41 64 64 4f 70 30 28 70 2c 20  e3VdbeAddOp0(p, 
1a260 4f 50 5f 4e 6f 6f 70 29 3b 0a 20 20 61 73 73 65  OP_Noop);.  asse
1a261 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c 20  rt( p->nOp>0 || 
1a262 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b 0a 20 20 61  p->aOp==0 );.  a
1a263 73 73 65 72 74 28 20 70 2d 3e 61 4f 70 3d 3d 30  ssert( p->aOp==0
1a264 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f   || p->aOp[p->nO
1a265 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d 30  p-1].zComment==0
1a266 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f   || p->db->mallo
1a267 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28  cFailed );.  if(
1a268 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 63   p->nOp ){.    c
1a269 68 61 72 20 2a 2a 70 7a 20 3d 20 26 70 2d 3e 61  har **pz = &p->a
1a26a 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f  Op[p->nOp-1].zCo
1a26b 6d 6d 65 6e 74 3b 0a 20 20 20 20 76 61 5f 73 74  mment;.    va_st
1a26c 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29  art(ap, zFormat)
1a26d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
1a26e 72 65 65 28 70 2d 3e 64 62 2c 20 2a 70 7a 29 3b  ree(p->db, *pz);
1a26f 0a 20 20 20 20 2a 70 7a 20 3d 20 73 71 6c 69 74  .    *pz = sqlit
1a270 65 33 56 4d 50 72 69 6e 74 66 28 70 2d 3e 64 62  e3VMPrintf(p->db
1a271 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a  , zFormat, ap);.
1a272 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a      va_end(ap);.
1a273 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a    }.}.#endif  /*
1a274 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a   NDEBUG */../*.*
1a275 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6f 70 63  * Return the opc
1a276 6f 64 65 20 66 6f 72 20 61 20 67 69 76 65 6e 20  ode for a given 
1a277 61 64 64 72 65 73 73 2e 20 20 49 66 20 74 68 65  address.  If the
1a278 20 61 64 64 72 65 73 73 20 69 73 20 2d 31 2c 20   address is -1, 
1a279 74 68 65 6e 0a 2a 2a 20 72 65 74 75 72 6e 20 74  then.** return t
1a27a 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  he most recently
1a27b 20 69 6e 73 65 72 74 65 64 20 6f 70 63 6f 64 65   inserted opcode
1a27c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d  ..**.** If a mem
1a27d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65  ory allocation e
1a27e 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65  rror has occurre
1a27f 64 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 63  d prior to the c
1a280 61 6c 6c 69 6e 67 20 6f 66 20 74 68 69 73 0a 2a  alling of this.*
1a281 2a 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 6e 20  * routine, then 
1a282 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 64  a pointer to a d
1a283 75 6d 6d 79 20 56 64 62 65 4f 70 20 77 69 6c 6c  ummy VdbeOp will
1a284 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 20 54   be returned.  T
1a285 68 61 74 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73  hat opcode.** is
1a286 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72   readable and wr
1a287 69 74 61 62 6c 65 2c 20 62 75 74 20 69 74 20 68  itable, but it h
1a288 61 73 20 6e 6f 20 65 66 66 65 63 74 2e 20 20 54  as no effect.  T
1a289 68 65 20 72 65 74 75 72 6e 20 6f 66 20 61 20 64  he return of a d
1a28a 75 6d 6d 79 0a 2a 2a 20 6f 70 63 6f 64 65 20 61  ummy.** opcode a
1a28b 6c 6c 6f 77 73 20 74 68 65 20 63 61 6c 6c 20 74  llows the call t
1a28c 6f 20 63 6f 6e 74 69 6e 75 65 20 66 75 6e 63 74  o continue funct
1a28d 69 6f 6e 69 6e 67 20 61 66 74 65 72 20 61 20 4f  ioning after a O
1a28e 4f 4d 20 66 61 75 6c 74 20 77 69 74 68 6f 75 74  OM fault without
1a28f 0a 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 63 68  .** having to ch
1a290 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68  eck to see if th
1a291 65 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 74 68  e return from th
1a292 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
1a293 76 61 6c 69 64 20 70 6f 69 6e 74 65 72 2e 0a 2a  valid pointer..*
1a294 2a 0a 2a 2a 20 41 62 6f 75 74 20 74 68 65 20 23  *.** About the #
1a295 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
1a296 54 5f 54 52 41 43 45 3a 20 20 4e 6f 72 6d 61 6c  T_TRACE:  Normal
1a297 6c 79 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ly, this routine
1a298 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64   is never called
1a299 0a 2a 2a 20 75 6e 6c 65 73 73 20 70 2d 3e 6e 4f  .** unless p->nO
1a29a 70 3e 30 2e 20 20 54 68 69 73 20 69 73 20 62 65  p>0.  This is be
1a29b 63 61 75 73 65 20 69 6e 20 74 68 65 20 61 62 73  cause in the abs
1a29c 65 6e 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f  ense of SQLITE_O
1a29d 4d 49 54 5f 54 52 41 43 45 2c 0a 2a 2a 20 61 6e  MIT_TRACE,.** an
1a29e 20 4f 50 5f 54 72 61 63 65 20 69 6e 73 74 72 75   OP_Trace instru
1a29f 63 74 69 6f 6e 20 69 73 20 61 6c 77 61 79 73 20  ction is always 
1a2a0 69 6e 73 65 72 74 65 64 20 62 79 20 73 71 6c 69  inserted by sqli
1a2a1 74 65 33 56 64 62 65 47 65 74 28 29 20 61 73 20  te3VdbeGet() as 
1a2a2 73 6f 6f 6e 20 61 73 0a 2a 2a 20 61 20 6e 65 77  soon as.** a new
1a2a3 20 56 44 42 45 20 69 73 20 63 72 65 61 74 65 64   VDBE is created
1a2a4 2e 20 20 53 6f 20 77 65 20 61 72 65 20 66 72 65  .  So we are fre
1a2a5 65 20 74 6f 20 73 65 74 20 61 64 64 72 20 74 6f  e to set addr to
1a2a6 20 70 2d 3e 6e 4f 70 2d 31 20 77 69 74 68 6f 75   p->nOp-1 withou
1a2a7 74 0a 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 64  t.** having to d
1a2a8 6f 75 62 6c 65 2d 63 68 65 63 6b 20 74 6f 20 6d  ouble-check to m
1a2a9 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68  ake sure that th
1a2aa 65 20 72 65 73 75 6c 74 20 69 73 20 6e 6f 6e 2d  e result is non-
1a2ab 6e 65 67 61 74 69 76 65 2e 20 42 75 74 0a 2a 2a  negative. But.**
1a2ac 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   if SQLITE_OMIT_
1a2ad 54 52 41 43 45 20 69 73 20 64 65 66 69 6e 65 64  TRACE is defined
1a2ae 2c 20 74 68 65 20 4f 50 5f 54 72 61 63 65 20 69  , the OP_Trace i
1a2af 73 20 6f 6d 69 74 74 65 64 20 61 6e 64 20 77 65  s omitted and we
1a2b0 20 64 6f 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 63   do need to.** c
1a2b1 68 65 63 6b 20 74 68 65 20 76 61 6c 75 65 20 6f  heck the value o
1a2b2 66 20 70 2d 3e 6e 4f 70 2d 31 20 62 65 66 6f 72  f p->nOp-1 befor
1a2b3 65 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 0a 2a 2f  e continuing..*/
1a2b4 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a2b5 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56  VdbeOp *sqlite3V
1a2b6 64 62 65 47 65 74 4f 70 28 56 64 62 65 20 2a 70  dbeGetOp(Vdbe *p
1a2b7 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a 20 20 73  , int addr){.  s
1a2b8 74 61 74 69 63 20 56 64 62 65 4f 70 20 64 75 6d  tatic VdbeOp dum
1a2b9 6d 79 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  my;.  assert( p-
1a2ba 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47  >magic==VDBE_MAG
1a2bb 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28  IC_INIT );.  if(
1a2bc 20 61 64 64 72 3c 30 20 29 7b 0a 23 69 66 64 65   addr<0 ){.#ifde
1a2bd 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
1a2be 41 43 45 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e  ACE.    if( p->n
1a2bf 4f 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 26  Op==0 ) return &
1a2c0 64 75 6d 6d 79 3b 0a 23 65 6e 64 69 66 0a 20 20  dummy;.#endif.  
1a2c1 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 20    addr = p->nOp 
1a2c2 2d 20 31 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  - 1;.  }.  asser
1a2c3 74 28 20 28 61 64 64 72 3e 3d 30 20 26 26 20 61  t( (addr>=0 && a
1a2c4 64 64 72 3c 70 2d 3e 6e 4f 70 29 20 7c 7c 20 70  ddr<p->nOp) || p
1a2c5 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
1a2c6 65 64 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 64  ed );.  if( p->d
1a2c7 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1a2c8 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 64  ){.    return &d
1a2c9 75 6d 6d 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ummy;.  }else{. 
1a2ca 20 20 20 72 65 74 75 72 6e 20 26 70 2d 3e 61 4f     return &p->aO
1a2cb 70 5b 61 64 64 72 5d 3b 0a 20 20 7d 0a 7d 0a 0a  p[addr];.  }.}..
1a2cc 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
1a2cd 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e  ITE_OMIT_EXPLAIN
1a2ce 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e 44  ) || !defined(ND
1a2cf 45 42 55 47 29 20 5c 0a 20 20 20 20 20 7c 7c 20  EBUG) \.     || 
1a2d0 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f  defined(VDBE_PRO
1a2d1 46 49 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65 64  FILE) || defined
1a2d2 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f  (SQLITE_DEBUG)./
1a2d3 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61 20 73  *.** Compute a s
1a2d4 74 72 69 6e 67 20 74 68 61 74 20 64 65 73 63 72  tring that descr
1a2d5 69 62 65 73 20 74 68 65 20 50 34 20 70 61 72 61  ibes the P4 para
1a2d6 6d 65 74 65 72 20 66 6f 72 20 61 6e 20 6f 70 63  meter for an opc
1a2d7 6f 64 65 2e 0a 2a 2a 20 55 73 65 20 7a 54 65 6d  ode..** Use zTem
1a2d8 70 20 66 6f 72 20 61 6e 79 20 72 65 71 75 69 72  p for any requir
1a2d9 65 64 20 74 65 6d 70 6f 72 61 72 79 20 62 75 66  ed temporary buf
1a2da 66 65 72 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74  fer space..*/.st
1a2db 61 74 69 63 20 63 68 61 72 20 2a 64 69 73 70 6c  atic char *displ
1a2dc 61 79 50 34 28 4f 70 20 2a 70 4f 70 2c 20 63 68  ayP4(Op *pOp, ch
1a2dd 61 72 20 2a 7a 54 65 6d 70 2c 20 69 6e 74 20 6e  ar *zTemp, int n
1a2de 54 65 6d 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a  Temp){.  char *z
1a2df 50 34 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 61 73  P4 = zTemp;.  as
1a2e0 73 65 72 74 28 20 6e 54 65 6d 70 3e 3d 32 30 20  sert( nTemp>=20 
1a2e1 29 3b 0a 20 20 73 77 69 74 63 68 28 20 70 4f 70  );.  switch( pOp
1a2e2 2d 3e 70 34 74 79 70 65 20 29 7b 0a 20 20 20 20  ->p4type ){.    
1a2e3 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  case P4_KEYINFO_
1a2e4 53 54 41 54 49 43 3a 0a 20 20 20 20 63 61 73 65  STATIC:.    case
1a2e5 20 50 34 5f 4b 45 59 49 4e 46 4f 3a 20 7b 0a 20   P4_KEYINFO: {. 
1a2e6 20 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20       int i, j;. 
1a2e7 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b       KeyInfo *pK
1a2e8 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34  eyInfo = pOp->p4
1a2e9 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20  .pKeyInfo;.     
1a2ea 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
1a2eb 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20  f(nTemp, zTemp, 
1a2ec 22 6b 65 79 69 6e 66 6f 28 25 64 22 2c 20 70 4b  "keyinfo(%d", pK
1a2ed 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 29 3b  eyInfo->nField);
1a2ee 0a 20 20 20 20 20 20 69 20 3d 20 73 71 6c 69 74  .      i = sqlit
1a2ef 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70  e3Strlen30(zTemp
1a2f0 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  );.      for(j=0
1a2f1 3b 20 6a 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46  ; j<pKeyInfo->nF
1a2f2 69 65 6c 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  ield; j++){.    
1a2f3 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
1a2f4 6c 6c 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61  ll = pKeyInfo->a
1a2f5 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 20  Coll[j];.       
1a2f6 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20   if( pColl ){.  
1a2f7 20 20 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20          int n = 
1a2f8 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
1a2f9 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  pColl->zName);. 
1a2fa 20 20 20 20 20 20 20 20 20 69 66 28 20 69 2b 6e           if( i+n
1a2fb 3e 6e 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20  >nTemp-6 ){.    
1a2fc 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
1a2fd 7a 54 65 6d 70 5b 69 5d 2c 22 2c 2e 2e 2e 22 2c  zTemp[i],",...",
1a2fe 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  4);.            
1a2ff 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
1a300 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7a 54 65   }.          zTe
1a301 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2c 27 3b 0a 20  mp[i++] = ',';. 
1a302 20 20 20 20 20 20 20 20 20 69 66 28 20 70 4b 65           if( pKe
1a303 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65  yInfo->aSortOrde
1a304 72 20 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61  r && pKeyInfo->a
1a305 53 6f 72 74 4f 72 64 65 72 5b 6a 5d 20 29 7b 0a  SortOrder[j] ){.
1a306 20 20 20 20 20 20 20 20 20 20 20 20 7a 54 65 6d              zTem
1a307 70 5b 69 2b 2b 5d 20 3d 20 27 2d 27 3b 0a 20 20  p[i++] = '-';.  
1a308 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1a309 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d      memcpy(&zTem
1a30a 70 5b 69 5d 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61  p[i], pColl->zNa
1a30b 6d 65 2c 6e 2b 31 29 3b 0a 20 20 20 20 20 20 20  me,n+1);.       
1a30c 20 20 20 69 20 2b 3d 20 6e 3b 0a 20 20 20 20 20     i += n;.     
1a30d 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 2b 34     }else if( i+4
1a30e 3c 6e 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20  <nTemp-6 ){.    
1a30f 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54        memcpy(&zT
1a310 65 6d 70 5b 69 5d 2c 22 2c 6e 69 6c 22 2c 34 29  emp[i],",nil",4)
1a311 3b 0a 20 20 20 20 20 20 20 20 20 20 69 20 2b 3d  ;.          i +=
1a312 20 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   4;.        }.  
1a313 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 54 65 6d      }.      zTem
1a314 70 5b 69 2b 2b 5d 20 3d 20 27 29 27 3b 0a 20 20  p[i++] = ')';.  
1a315 20 20 20 20 7a 54 65 6d 70 5b 69 5d 20 3d 20 30      zTemp[i] = 0
1a316 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1a317 69 3c 6e 54 65 6d 70 20 29 3b 0a 20 20 20 20 20  i<nTemp );.     
1a318 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1a319 20 20 63 61 73 65 20 50 34 5f 43 4f 4c 4c 53 45    case P4_COLLSE
1a31a 51 3a 20 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53  Q: {.      CollS
1a31b 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 4f 70 2d  eq *pColl = pOp-
1a31c 3e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20  >p4.pColl;.     
1a31d 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
1a31e 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20  f(nTemp, zTemp, 
1a31f 22 63 6f 6c 6c 73 65 71 28 25 2e 32 30 73 29 22  "collseq(%.20s)"
1a320 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b  , pColl->zName);
1a321 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1a322 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f    }.    case P4_
1a323 46 55 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20 20  FUNCDEF: {.     
1a324 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 20 3d   FuncDef *pDef =
1a325 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a   pOp->p4.pFunc;.
1a326 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
1a327 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54  printf(nTemp, zT
1a328 65 6d 70 2c 20 22 25 73 28 25 64 29 22 2c 20 70  emp, "%s(%d)", p
1a329 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66  Def->zName, pDef
1a32a 2d 3e 6e 41 72 67 29 3b 0a 20 20 20 20 20 20 62  ->nArg);.      b
1a32b 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1a32c 63 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 20 7b  case P4_INT64: {
1a32d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
1a32e 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a  nprintf(nTemp, z
1a32f 54 65 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 2a 70  Temp, "%lld", *p
1a330 4f 70 2d 3e 70 34 2e 70 49 36 34 29 3b 0a 20 20  Op->p4.pI64);.  
1a331 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1a332 0a 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54  .    case P4_INT
1a333 33 32 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  32: {.      sqli
1a334 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65  te3_snprintf(nTe
1a335 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 64 22 2c  mp, zTemp, "%d",
1a336 20 70 4f 70 2d 3e 70 34 2e 69 29 3b 0a 20 20 20   pOp->p4.i);.   
1a337 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1a338 20 20 20 20 63 61 73 65 20 50 34 5f 52 45 41 4c      case P4_REAL
1a339 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
1a33a 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70  3_snprintf(nTemp
1a33b 2c 20 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67 22  , zTemp, "%.16g"
1a33c 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c  , *pOp->p4.pReal
1a33d 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
1a33e 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50      }.    case P
1a33f 34 5f 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 4d  4_MEM: {.      M
1a340 65 6d 20 2a 70 4d 65 6d 20 3d 20 70 4f 70 2d 3e  em *pMem = pOp->
1a341 70 34 2e 70 4d 65 6d 3b 0a 20 20 20 20 20 20 61  p4.pMem;.      a
1a342 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c  ssert( (pMem->fl
1a343 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d  ags & MEM_Null)=
1a344 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  =0 );.      if( 
1a345 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
1a346 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 20 20 20  M_Str ){.       
1a347 20 7a 50 34 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a   zP4 = pMem->z;.
1a348 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
1a349 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
1a34a 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 20 20 20  M_Int ){.       
1a34b 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
1a34c 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20  f(nTemp, zTemp, 
1a34d 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e  "%lld", pMem->u.
1a34e 69 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  i);.      }else 
1a34f 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  if( pMem->flags 
1a350 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20  & MEM_Real ){.  
1a351 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
1a352 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54  printf(nTemp, zT
1a353 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c 20 70 4d  emp, "%.16g", pM
1a354 65 6d 2d 3e 72 29 3b 0a 20 20 20 20 20 20 7d 65  em->r);.      }e
1a355 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73  lse{.        ass
1a356 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  ert( pMem->flags
1a357 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20   & MEM_Blob );. 
1a358 20 20 20 20 20 20 20 7a 50 34 20 3d 20 22 28 62         zP4 = "(b
1a359 6c 6f 62 29 22 3b 0a 20 20 20 20 20 20 7d 0a 20  lob)";.      }. 
1a35a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1a35b 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
1a35c 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1a35d 4c 45 0a 20 20 20 20 63 61 73 65 20 50 34 5f 56  LE.    case P4_V
1a35e 54 41 42 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  TAB: {.      sql
1a35f 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
1a360 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62   = pOp->p4.pVtab
1a361 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20 20 20 73  ->pVtab;.      s
1a362 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1a363 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 76  nTemp, zTemp, "v
1a364 74 61 62 3a 25 70 3a 25 70 22 2c 20 70 56 74 61  tab:%p:%p", pVta
1a365 62 2c 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c  b, pVtab->pModul
1a366 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  e);.      break;
1a367 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
1a368 20 20 63 61 73 65 20 50 34 5f 49 4e 54 41 52 52    case P4_INTARR
1a369 41 59 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  AY: {.      sqli
1a36a 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65  te3_snprintf(nTe
1a36b 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 69 6e 74 61  mp, zTemp, "inta
1a36c 72 72 61 79 22 29 3b 0a 20 20 20 20 20 20 62 72  rray");.      br
1a36d 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
1a36e 61 73 65 20 50 34 5f 53 55 42 50 52 4f 47 52 41  ase P4_SUBPROGRA
1a36f 4d 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  M: {.      sqlit
1a370 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d  e3_snprintf(nTem
1a371 70 2c 20 7a 54 65 6d 70 2c 20 22 70 72 6f 67 72  p, zTemp, "progr
1a372 61 6d 22 29 3b 0a 20 20 20 20 20 20 62 72 65 61  am");.      brea
1a373 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
1a374 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 7a 50  ault: {.      zP
1a375 34 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20  4 = pOp->p4.z;. 
1a376 20 20 20 20 20 69 66 28 20 7a 50 34 3d 3d 30 20       if( zP4==0 
1a377 29 7b 0a 20 20 20 20 20 20 20 20 7a 50 34 20 3d  ){.        zP4 =
1a378 20 7a 54 65 6d 70 3b 0a 20 20 20 20 20 20 20 20   zTemp;.        
1a379 7a 54 65 6d 70 5b 30 5d 20 3d 20 30 3b 0a 20 20  zTemp[0] = 0;.  
1a37a 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1a37b 20 20 61 73 73 65 72 74 28 20 7a 50 34 21 3d 30    assert( zP4!=0
1a37c 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 50 34   );.  return zP4
1a37d 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
1a37e 2a 20 44 65 63 6c 61 72 65 20 74 6f 20 74 68 65  * Declare to the
1a37f 20 56 64 62 65 20 74 68 61 74 20 74 68 65 20 42   Vdbe that the B
1a380 54 72 65 65 20 6f 62 6a 65 63 74 20 61 74 20 64  Tree object at d
1a381 62 2d 3e 61 44 62 5b 69 5d 20 69 73 20 75 73 65  b->aDb[i] is use
1a382 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
1a383 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1a384 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 56  3VdbeUsesBtree(V
1a385 64 62 65 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a  dbe *p, int i){.
1a386 20 20 69 6e 74 20 6d 61 73 6b 3b 0a 20 20 61 73    int mask;.  as
1a387 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
1a388 70 2d 3e 64 62 2d 3e 6e 44 62 20 26 26 20 69 3c  p->db->nDb && i<
1a389 73 69 7a 65 6f 66 28 75 33 32 29 2a 38 20 29 3b  sizeof(u32)*8 );
1a38a 0a 20 20 61 73 73 65 72 74 28 20 69 3c 28 69 6e  .  assert( i<(in
1a38b 74 29 73 69 7a 65 6f 66 28 70 2d 3e 62 74 72 65  t)sizeof(p->btre
1a38c 65 4d 61 73 6b 29 2a 38 20 29 3b 0a 20 20 6d 61  eMask)*8 );.  ma
1a38d 73 6b 20 3d 20 28 28 75 33 32 29 31 29 3c 3c 69  sk = ((u32)1)<<i
1a38e 3b 0a 20 20 69 66 28 20 28 70 2d 3e 62 74 72 65  ;.  if( (p->btre
1a38f 65 4d 61 73 6b 20 26 20 6d 61 73 6b 29 3d 3d 30  eMask & mask)==0
1a390 20 29 7b 0a 20 20 20 20 70 2d 3e 62 74 72 65 65   ){.    p->btree
1a391 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20  Mask |= mask;.  
1a392 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75    sqlite3BtreeMu
1a393 74 65 78 41 72 72 61 79 49 6e 73 65 72 74 28 26  texArrayInsert(&
1a394 70 2d 3e 61 4d 75 74 65 78 2c 20 70 2d 3e 64 62  p->aMutex, p->db
1a395 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 3b 0a 20  ->aDb[i].pBt);. 
1a396 20 7d 0a 7d 0a 0a 0a 23 69 66 20 64 65 66 69 6e   }.}...#if defin
1a397 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29  ed(VDBE_PROFILE)
1a398 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
1a399 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20  TE_DEBUG)./*.** 
1a39a 50 72 69 6e 74 20 61 20 73 69 6e 67 6c 65 20 6f  Print a single o
1a39b 70 63 6f 64 65 2e 20 20 54 68 69 73 20 72 6f 75  pcode.  This rou
1a39c 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72  tine is used for
1a39d 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e   debugging only.
1a39e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1a39f 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
1a3a0 64 62 65 50 72 69 6e 74 4f 70 28 46 49 4c 45 20  dbePrintOp(FILE 
1a3a1 2a 70 4f 75 74 2c 20 69 6e 74 20 70 63 2c 20 4f  *pOut, int pc, O
1a3a2 70 20 2a 70 4f 70 29 7b 0a 20 20 63 68 61 72 20  p *pOp){.  char 
1a3a3 2a 7a 50 34 3b 0a 20 20 63 68 61 72 20 7a 50 74  *zP4;.  char zPt
1a3a4 72 5b 35 30 5d 3b 0a 20 20 73 74 61 74 69 63 20  r[50];.  static 
1a3a5 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
1a3a6 6d 61 74 31 20 3d 20 22 25 34 64 20 25 2d 31 33  mat1 = "%4d %-13
1a3a7 73 20 25 34 64 20 25 34 64 20 25 34 64 20 25 2d  s %4d %4d %4d %-
1a3a8 34 73 20 25 2e 32 58 20 25 73 5c 6e 22 3b 0a 20  4s %.2X %s\n";. 
1a3a9 20 69 66 28 20 70 4f 75 74 3d 3d 30 20 29 20 70   if( pOut==0 ) p
1a3aa 4f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20  Out = stdout;.  
1a3ab 7a 50 34 20 3d 20 64 69 73 70 6c 61 79 50 34 28  zP4 = displayP4(
1a3ac 70 4f 70 2c 20 7a 50 74 72 2c 20 73 69 7a 65 6f  pOp, zPtr, sizeo
1a3ad 66 28 7a 50 74 72 29 29 3b 0a 20 20 66 70 72 69  f(zPtr));.  fpri
1a3ae 6e 74 66 28 70 4f 75 74 2c 20 7a 46 6f 72 6d 61  ntf(pOut, zForma
1a3af 74 31 2c 20 70 63 2c 20 0a 20 20 20 20 20 20 73  t1, pc, .      s
1a3b0 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65  qlite3OpcodeName
1a3b1 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 2c 20 70  (pOp->opcode), p
1a3b2 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 32 2c  Op->p1, pOp->p2,
1a3b3 20 70 4f 70 2d 3e 70 33 2c 20 7a 50 34 2c 20 70   pOp->p3, zP4, p
1a3b4 4f 70 2d 3e 70 35 2c 0a 23 69 66 64 65 66 20 53  Op->p5,.#ifdef S
1a3b5 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20  QLITE_DEBUG.    
1a3b6 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20    pOp->zComment 
1a3b7 3f 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20  ? pOp->zComment 
1a3b8 3a 20 22 22 0a 23 65 6c 73 65 0a 20 20 20 20 20  : "".#else.     
1a3b9 20 22 22 0a 23 65 6e 64 69 66 0a 20 20 29 3b 0a   "".#endif.  );.
1a3ba 20 20 66 66 6c 75 73 68 28 70 4f 75 74 29 3b 0a    fflush(pOut);.
1a3bb 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
1a3bc 52 65 6c 65 61 73 65 20 61 6e 20 61 72 72 61 79  Release an array
1a3bd 20 6f 66 20 4e 20 4d 65 6d 20 65 6c 65 6d 65 6e   of N Mem elemen
1a3be 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ts.*/.static voi
1a3bf 64 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61  d releaseMemArra
1a3c0 79 28 4d 65 6d 20 2a 70 2c 20 69 6e 74 20 4e 29  y(Mem *p, int N)
1a3c1 7b 0a 20 20 69 66 28 20 70 20 26 26 20 4e 20 29  {.  if( p && N )
1a3c2 7b 0a 20 20 20 20 4d 65 6d 20 2a 70 45 6e 64 3b  {.    Mem *pEnd;
1a3c3 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62  .    sqlite3 *db
1a3c4 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 75 38   = p->db;.    u8
1a3c5 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 20 3d   malloc_failed =
1a3c6 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1a3c7 64 3b 0a 20 20 20 20 66 6f 72 28 70 45 6e 64 3d  d;.    for(pEnd=
1a3c8 26 70 5b 4e 5d 3b 20 70 3c 70 45 6e 64 3b 20 70  &p[N]; p<pEnd; p
1a3c9 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  ++){.      asser
1a3ca 74 28 20 28 26 70 5b 31 5d 29 3d 3d 70 45 6e 64  t( (&p[1])==pEnd
1a3cb 20 7c 7c 20 70 5b 30 5d 2e 64 62 3d 3d 70 5b 31   || p[0].db==p[1
1a3cc 5d 2e 64 62 20 29 3b 0a 0a 20 20 20 20 20 20 2f  ].db );..      /
1a3cd 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 69 73 20  * This block is 
1a3ce 72 65 61 6c 6c 79 20 61 6e 20 69 6e 6c 69 6e 65  really an inline
1a3cf 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c  d version of sql
1a3d0 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
1a3d1 73 65 28 29 0a 20 20 20 20 20 20 2a 2a 20 74 68  se().      ** th
1a3d2 61 74 20 74 61 6b 65 73 20 61 64 76 61 6e 74 61  at takes advanta
1a3d3 67 65 20 6f 66 20 74 68 65 20 66 61 63 74 20 74  ge of the fact t
1a3d4 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79 20 63  hat the memory c
1a3d5 65 6c 6c 20 76 61 6c 75 65 20 69 73 20 0a 20 20  ell value is .  
1a3d6 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 73 65 74      ** being set
1a3d7 20 74 6f 20 4e 55 4c 4c 20 61 66 74 65 72 20 72   to NULL after r
1a3d8 65 6c 65 61 73 69 6e 67 20 61 6e 79 20 64 79 6e  eleasing any dyn
1a3d9 61 6d 69 63 20 72 65 73 6f 75 72 63 65 73 2e 0a  amic resources..
1a3da 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
1a3db 2a 20 54 68 65 20 6a 75 73 74 69 66 69 63 61 74  * The justificat
1a3dc 69 6f 6e 20 66 6f 72 20 64 75 70 6c 69 63 61 74  ion for duplicat
1a3dd 69 6e 67 20 63 6f 64 65 20 69 73 20 74 68 61 74  ing code is that
1a3de 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 0a 20   according to . 
1a3df 20 20 20 20 20 2a 2a 20 63 61 6c 6c 67 72 69 6e       ** callgrin
1a3e0 64 2c 20 74 68 69 73 20 63 61 75 73 65 73 20 61  d, this causes a
1a3e1 20 63 65 72 74 61 69 6e 20 74 65 73 74 20 63 61   certain test ca
1a3e2 73 65 20 74 6f 20 68 69 74 20 74 68 65 20 43 50  se to hit the CP
1a3e3 55 20 34 2e 37 20 0a 20 20 20 20 20 20 2a 2a 20  U 4.7 .      ** 
1a3e4 70 65 72 63 65 6e 74 20 6c 65 73 73 20 28 78 38  percent less (x8
1a3e5 36 20 6c 69 6e 75 78 2c 20 67 63 63 20 76 65 72  6 linux, gcc ver
1a3e6 73 69 6f 6e 20 34 2e 31 2e 32 2c 20 2d 4f 36 29  sion 4.1.2, -O6)
1a3e7 20 74 68 61 6e 20 69 66 20 0a 20 20 20 20 20 20   than if .      
1a3e8 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 6c  ** sqlite3MemRel
1a3e9 65 61 73 65 28 29 20 77 65 72 65 20 63 61 6c 6c  ease() were call
1a3ea 65 64 20 66 72 6f 6d 20 68 65 72 65 2e 20 57 69  ed from here. Wi
1a3eb 74 68 20 2d 4f 32 2c 20 74 68 69 73 20 6a 75 6d  th -O2, this jum
1a3ec 70 73 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 36  ps.      ** to 6
1a3ed 2e 36 20 70 65 72 63 65 6e 74 2e 20 54 68 65 20  .6 percent. The 
1a3ee 74 65 73 74 20 63 61 73 65 20 69 73 20 69 6e 73  test case is ins
1a3ef 65 72 74 69 6e 67 20 31 30 30 30 20 72 6f 77 73  erting 1000 rows
1a3f0 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 0a 20   into a table . 
1a3f1 20 20 20 20 20 2a 2a 20 77 69 74 68 20 6e 6f 20       ** with no 
1a3f2 69 6e 64 65 78 65 73 20 75 73 69 6e 67 20 61 20  indexes using a 
1a3f3 73 69 6e 67 6c 65 20 70 72 65 70 61 72 65 64 20  single prepared 
1a3f4 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74  INSERT statement
1a3f5 2c 20 62 69 6e 64 28 29 20 0a 20 20 20 20 20 20  , bind() .      
1a3f6 2a 2a 20 61 6e 64 20 72 65 73 65 74 28 29 2e 20  ** and reset(). 
1a3f7 49 6e 73 65 72 74 73 20 61 72 65 20 67 72 6f 75  Inserts are grou
1a3f8 70 65 64 20 69 6e 74 6f 20 61 20 74 72 61 6e 73  ped into a trans
1a3f9 61 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f  action..      */
1a3fa 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 66 6c  .      if( p->fl
1a3fb 61 67 73 26 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d  ags&(MEM_Agg|MEM
1a3fc 5f 44 79 6e 7c 4d 45 4d 5f 46 72 61 6d 65 7c 4d  _Dyn|MEM_Frame|M
1a3fd 45 4d 5f 52 6f 77 53 65 74 29 20 29 7b 0a 20 20  EM_RowSet) ){.  
1a3fe 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1a3ff 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 29 3b 0a  eMemRelease(p);.
1a400 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
1a401 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20  p->zMalloc ){.  
1a402 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
1a403 72 65 65 28 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c  ree(db, p->zMall
1a404 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  oc);.        p->
1a405 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20  zMalloc = 0;.   
1a406 20 20 20 7d 0a 0a 20 20 20 20 20 20 70 2d 3e 66     }..      p->f
1a407 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b  lags = MEM_Null;
1a408 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 6d  .    }.    db->m
1a409 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 6d 61  allocFailed = ma
1a40a 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20 7d  lloc_failed;.  }
1a40b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
1a40c 20 61 20 56 64 62 65 46 72 61 6d 65 20 6f 62 6a   a VdbeFrame obj
1a40d 65 63 74 20 61 6e 64 20 69 74 73 20 63 6f 6e 74  ect and its cont
1a40e 65 6e 74 73 2e 20 56 64 62 65 46 72 61 6d 65 20  ents. VdbeFrame 
1a40f 6f 62 6a 65 63 74 73 20 61 72 65 0a 2a 2a 20 61  objects are.** a
1a410 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65 20  llocated by the 
1a411 4f 50 5f 50 72 6f 67 72 61 6d 20 6f 70 63 6f 64  OP_Program opcod
1a412 65 20 69 6e 20 73 71 6c 69 74 65 33 56 64 62 65  e in sqlite3Vdbe
1a413 45 78 65 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  Exec()..*/.SQLIT
1a414 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1a415 71 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65 44  qlite3VdbeFrameD
1a416 65 6c 65 74 65 28 56 64 62 65 46 72 61 6d 65 20  elete(VdbeFrame 
1a417 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  *p){.  int i;.  
1a418 4d 65 6d 20 2a 61 4d 65 6d 20 3d 20 56 64 62 65  Mem *aMem = Vdbe
1a419 46 72 61 6d 65 4d 65 6d 28 70 29 3b 0a 20 20 56  FrameMem(p);.  V
1a41a 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 70 43 73  dbeCursor **apCs
1a41b 72 20 3d 20 28 56 64 62 65 43 75 72 73 6f 72 20  r = (VdbeCursor 
1a41c 2a 2a 29 26 61 4d 65 6d 5b 70 2d 3e 6e 43 68 69  **)&aMem[p->nChi
1a41d 6c 64 4d 65 6d 5d 3b 0a 20 20 66 6f 72 28 69 3d  ldMem];.  for(i=
1a41e 30 3b 20 69 3c 70 2d 3e 6e 43 68 69 6c 64 43 73  0; i<p->nChildCs
1a41f 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  r; i++){.    sql
1a420 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73  ite3VdbeFreeCurs
1a421 6f 72 28 70 2d 3e 76 2c 20 61 70 43 73 72 5b 69  or(p->v, apCsr[i
1a422 5d 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73  ]);.  }.  releas
1a423 65 4d 65 6d 41 72 72 61 79 28 61 4d 65 6d 2c 20  eMemArray(aMem, 
1a424 70 2d 3e 6e 43 68 69 6c 64 4d 65 6d 29 3b 0a 20  p->nChildMem);. 
1a425 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
1a426 2d 3e 76 2d 3e 64 62 2c 20 70 29 3b 0a 7d 0a 0a  ->v->db, p);.}..
1a427 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
1a428 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e  NABLE_MEMORY_MAN
1a429 41 47 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50  AGEMENT.SQLITE_P
1a42a 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1a42b 65 33 56 64 62 65 52 65 6c 65 61 73 65 42 75 66  e3VdbeReleaseBuf
1a42c 66 65 72 73 28 56 64 62 65 20 2a 70 29 7b 0a 20  fers(Vdbe *p){. 
1a42d 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 6e   int ii;.  int n
1a42e 46 72 65 65 20 3d 20 30 3b 0a 20 20 61 73 73 65  Free = 0;.  asse
1a42f 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1a430 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
1a431 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 69  tex) );.  for(ii
1a432 3d 31 3b 20 69 69 3c 3d 70 2d 3e 6e 4d 65 6d 3b  =1; ii<=p->nMem;
1a433 20 69 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d 20   ii++){.    Mem 
1a434 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d  *pMem = &p->aMem
1a435 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 4d  [ii];.    if( pM
1a436 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
1a437 52 6f 77 53 65 74 20 29 7b 0a 20 20 20 20 20 20  RowSet ){.      
1a438 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65  sqlite3RowSetCle
1a439 61 72 28 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53  ar(pMem->u.pRowS
1a43a 65 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  et);.    }.    i
1a43b 66 28 20 70 4d 65 6d 2d 3e 7a 20 26 26 20 70 4d  f( pMem->z && pM
1a43c 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79  em->flags&MEM_Dy
1a43d 6e 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  n ){.      asser
1a43e 74 28 20 21 70 4d 65 6d 2d 3e 78 44 65 6c 20 29  t( !pMem->xDel )
1a43f 3b 0a 20 20 20 20 20 20 6e 46 72 65 65 20 2b 3d  ;.      nFree +=
1a440 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
1a441 53 69 7a 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70  Size(pMem->db, p
1a442 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 73  Mem->z);.      s
1a443 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1a444 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20  ease(pMem);.    
1a445 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e  }.  }.  return n
1a446 46 72 65 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  Free;.}.#endif..
1a447 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1a448 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 2f 2a 0a 2a  MIT_EXPLAIN./*.*
1a449 2a 20 47 69 76 65 20 61 20 6c 69 73 74 69 6e 67  * Give a listing
1a44a 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20   of the program 
1a44b 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d  in the virtual m
1a44c 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  achine..**.** Th
1a44d 65 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 74  e interface is t
1a44e 68 65 20 73 61 6d 65 20 61 73 20 73 71 6c 69 74  he same as sqlit
1a44f 65 33 56 64 62 65 45 78 65 63 28 29 2e 20 20 42  e3VdbeExec().  B
1a450 75 74 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a  ut instead of.**
1a451 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 63 6f 64   running the cod
1a452 65 2c 20 69 74 20 69 6e 76 6f 6b 65 73 20 74 68  e, it invokes th
1a453 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 65 20  e callback once 
1a454 66 6f 72 20 65 61 63 68 20 69 6e 73 74 72 75 63  for each instruc
1a455 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 65  tion..** This fe
1a456 61 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f  ature is used to
1a457 20 69 6d 70 6c 65 6d 65 6e 74 20 22 45 58 50 4c   implement "EXPL
1a458 41 49 4e 22 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  AIN"..**.** When
1a459 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 2c 20   p->explain==1, 
1a45a 65 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e  each instruction
1a45b 20 69 73 20 6c 69 73 74 65 64 2e 20 20 57 68 65   is listed.  Whe
1a45c 6e 0a 2a 2a 20 70 2d 3e 65 78 70 6c 61 69 6e 3d  n.** p->explain=
1a45d 3d 32 2c 20 6f 6e 6c 79 20 4f 50 5f 45 78 70 6c  =2, only OP_Expl
1a45e 61 69 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 73  ain instructions
1a45f 20 61 72 65 20 6c 69 73 74 65 64 20 61 6e 64 20   are listed and 
1a460 74 68 65 73 65 0a 2a 2a 20 61 72 65 20 73 68 6f  these.** are sho
1a461 77 6e 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e  wn in a differen
1a462 74 20 66 6f 72 6d 61 74 2e 20 20 70 2d 3e 65 78  t format.  p->ex
1a463 70 6c 61 69 6e 3d 3d 32 20 69 73 20 75 73 65 64  plain==2 is used
1a464 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 0a 2a 2a   to implement.**
1a465 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59 20 50   EXPLAIN QUERY P
1a466 4c 41 4e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  LAN..*/.SQLITE_P
1a467 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1a468 65 33 56 64 62 65 4c 69 73 74 28 0a 20 20 56 64  e3VdbeList(.  Vd
1a469 62 65 20 2a 70 20 20 20 20 20 20 20 20 20 20 20  be *p           
1a46a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
1a46b 44 42 45 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  DBE */.){.  int 
1a46c 6e 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20 20  nRow;           
1a46d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a46e 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72   /* Total number
1a46f 20 6f 66 20 72 6f 77 73 20 74 6f 20 72 65 74 75   of rows to retu
1a470 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 75 62  rn */.  int nSub
1a471 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
1a472 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a473 4e 75 6d 62 65 72 20 6f 66 20 73 75 62 2d 76 64  Number of sub-vd
1a474 62 65 73 20 73 65 65 6e 20 73 6f 20 66 61 72 20  bes seen so far 
1a475 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72 61 6d 20  */.  SubProgram 
1a476 2a 2a 61 70 53 75 62 20 3d 20 30 3b 20 20 20 20  **apSub = 0;    
1a477 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72            /* Arr
1a478 61 79 20 6f 66 20 73 75 62 2d 76 64 62 65 73 20  ay of sub-vdbes 
1a479 2a 2f 0a 20 20 4d 65 6d 20 2a 70 53 75 62 20 3d  */.  Mem *pSub =
1a47a 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   0;.  sqlite3 *d
1a47b 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74  b = p->db;.  int
1a47c 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53   i;.  int rc = S
1a47d 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 20  QLITE_OK;.  Mem 
1a47e 2a 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75  *pMem = p->pResu
1a47f 6c 74 53 65 74 20 3d 20 26 70 2d 3e 61 4d 65 6d  ltSet = &p->aMem
1a480 5b 31 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  [1];..  assert( 
1a481 70 2d 3e 65 78 70 6c 61 69 6e 20 29 3b 0a 20 20  p->explain );.  
1a482 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63  assert( p->magic
1a483 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e  ==VDBE_MAGIC_RUN
1a484 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62   );.  assert( db
1a485 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f  ->magic==SQLITE_
1a486 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a 20 20  MAGIC_BUSY );.  
1a487 61 73 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53  assert( p->rc==S
1a488 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72  QLITE_OK || p->r
1a489 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 7c  c==SQLITE_BUSY |
1a48a 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f  | p->rc==SQLITE_
1a48b 4e 4f 4d 45 4d 20 29 3b 0a 0a 20 20 2f 2a 20 45  NOMEM );..  /* E
1a48c 76 65 6e 20 74 68 6f 75 67 68 20 74 68 69 73 20  ven though this 
1a48d 6f 70 63 6f 64 65 20 64 6f 65 73 20 6e 6f 74 20  opcode does not 
1a48e 75 73 65 20 64 79 6e 61 6d 69 63 20 73 74 72 69  use dynamic stri
1a48f 6e 67 73 20 66 6f 72 0a 20 20 2a 2a 20 74 68 65  ngs for.  ** the
1a490 20 72 65 73 75 6c 74 2c 20 72 65 73 75 6c 74 20   result, result 
1a491 63 6f 6c 75 6d 6e 73 20 6d 61 79 20 62 65 63 6f  columns may beco
1a492 6d 65 20 64 79 6e 61 6d 69 63 20 69 66 20 74 68  me dynamic if th
1a493 65 20 75 73 65 72 20 63 61 6c 6c 73 0a 20 20 2a  e user calls.  *
1a494 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  * sqlite3_column
1a495 5f 74 65 78 74 31 36 28 29 2c 20 63 61 75 73 69  _text16(), causi
1a496 6e 67 20 61 20 74 72 61 6e 73 6c 61 74 69 6f 6e  ng a translation
1a497 20 74 6f 20 55 54 46 2d 31 36 20 65 6e 63 6f 64   to UTF-16 encod
1a498 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 72 65 6c 65  ing..  */.  rele
1a499 61 73 65 4d 65 6d 41 72 72 61 79 28 70 4d 65 6d  aseMemArray(pMem
1a49a 2c 20 38 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e  , 8);..  if( p->
1a49b 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
1a49c 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20   ){.    /* This 
1a49d 68 61 70 70 65 6e 73 20 69 66 20 61 20 6d 61 6c  happens if a mal
1a49e 6c 6f 63 28 29 20 69 6e 73 69 64 65 20 61 20 63  loc() inside a c
1a49f 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63  all to sqlite3_c
1a4a0 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a  olumn_text() or.
1a4a1 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63      ** sqlite3_c
1a4a2 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66  olumn_text16() f
1a4a3 61 69 6c 65 64 2e 20 20 2a 2f 0a 20 20 20 20 64  ailed.  */.    d
1a4a4 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1a4a5 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 1;.    return 
1a4a6 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1a4a7 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  }..  /* Figure o
1a4a8 75 74 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  ut total number 
1a4a9 6f 66 20 72 6f 77 73 20 74 68 61 74 20 77 69 6c  of rows that wil
1a4aa 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79  l be returned by
1a4ab 20 74 68 69 73 20 0a 20 20 2a 2a 20 45 58 50 4c   this .  ** EXPL
1a4ac 41 49 4e 20 70 72 6f 67 72 61 6d 2e 20 20 2a 2f  AIN program.  */
1a4ad 0a 20 20 6e 52 6f 77 20 3d 20 70 2d 3e 6e 4f 70  .  nRow = p->nOp
1a4ae 3b 0a 20 20 69 66 28 20 70 2d 3e 65 78 70 6c 61  ;.  if( p->expla
1a4af 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 70 53 75  in==1 ){.    pSu
1a4b0 62 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 39 5d 3b  b = &p->aMem[9];
1a4b1 0a 20 20 20 20 69 66 28 20 70 53 75 62 2d 3e 66  .    if( pSub->f
1a4b2 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b  lags&MEM_Blob ){
1a4b3 0a 20 20 20 20 20 20 6e 53 75 62 20 3d 20 70 53  .      nSub = pS
1a4b4 75 62 2d 3e 6e 2f 73 69 7a 65 6f 66 28 56 64 62  ub->n/sizeof(Vdb
1a4b5 65 2a 29 3b 0a 20 20 20 20 20 20 61 70 53 75 62  e*);.      apSub
1a4b6 20 3d 20 28 53 75 62 50 72 6f 67 72 61 6d 20 2a   = (SubProgram *
1a4b7 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20 20 20 20 7d  *)pSub->z;.    }
1a4b8 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
1a4b9 6e 53 75 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nSub; i++){.    
1a4ba 20 20 6e 52 6f 77 20 2b 3d 20 61 70 53 75 62 5b    nRow += apSub[
1a4bb 69 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d 0a 20  i]->nOp;.    }. 
1a4bc 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 69 20   }..  do{.    i 
1a4bd 3d 20 70 2d 3e 70 63 2b 2b 3b 0a 20 20 7d 77 68  = p->pc++;.  }wh
1a4be 69 6c 65 28 20 69 3c 6e 52 6f 77 20 26 26 20 70  ile( i<nRow && p
1a4bf 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 26 26 20  ->explain==2 && 
1a4c0 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65  p->aOp[i].opcode
1a4c1 21 3d 4f 50 5f 45 78 70 6c 61 69 6e 20 29 3b 0a  !=OP_Explain );.
1a4c2 20 20 69 66 28 20 69 3e 3d 6e 52 6f 77 20 29 7b    if( i>=nRow ){
1a4c3 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c  .    p->rc = SQL
1a4c4 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72 63 20 3d  ITE_OK;.    rc =
1a4c5 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20   SQLITE_DONE;.  
1a4c6 7d 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 75 31  }else if( db->u1
1a4c7 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 29  .isInterrupted )
1a4c8 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51  {.    p->rc = SQ
1a4c9 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a  LITE_INTERRUPT;.
1a4ca 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1a4cb 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74  ERROR;.    sqlit
1a4cc 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
1a4cd 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73  zErrMsg, db, "%s
1a4ce 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72  ", sqlite3ErrStr
1a4cf 28 70 2d 3e 72 63 29 29 3b 0a 20 20 7d 65 6c 73  (p->rc));.  }els
1a4d0 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 0a  e{.    char *z;.
1a4d1 20 20 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20      Op *pOp;.   
1a4d2 20 69 66 28 20 69 3c 70 2d 3e 6e 4f 70 20 29 7b   if( i<p->nOp ){
1a4d3 0a 20 20 20 20 20 20 70 4f 70 20 3d 20 26 70 2d  .      pOp = &p-
1a4d4 3e 61 4f 70 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c  >aOp[i];.    }el
1a4d5 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b  se{.      int j;
1a4d6 0a 20 20 20 20 20 20 69 20 2d 3d 20 70 2d 3e 6e  .      i -= p->n
1a4d7 4f 70 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d  Op;.      for(j=
1a4d8 30 3b 20 69 3e 3d 61 70 53 75 62 5b 6a 5d 2d 3e  0; i>=apSub[j]->
1a4d9 6e 4f 70 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  nOp; j++){.     
1a4da 20 20 20 69 20 2d 3d 20 61 70 53 75 62 5b 6a 5d     i -= apSub[j]
1a4db 2d 3e 6e 4f 70 3b 0a 20 20 20 20 20 20 7d 0a 20  ->nOp;.      }. 
1a4dc 20 20 20 20 20 70 4f 70 20 3d 20 26 61 70 53 75       pOp = &apSu
1a4dd 62 5b 6a 5d 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20  b[j]->aOp[i];.  
1a4de 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 65    }.    if( p->e
1a4df 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20  xplain==1 ){.   
1a4e0 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
1a4e1 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20   MEM_Int;.      
1a4e2 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
1a4e3 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20  ITE_INTEGER;.   
1a4e4 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 69     pMem->u.i = i
1a4e5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1a4e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a4e7 20 2f 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75 6e   /* Program coun
1a4e8 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65  ter */.      pMe
1a4e9 6d 2b 2b 3b 0a 20 20 0a 20 20 20 20 20 20 70 4d  m++;.  .      pM
1a4ea 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
1a4eb 53 74 61 74 69 63 7c 4d 45 4d 5f 53 74 72 7c 4d  Static|MEM_Str|M
1a4ec 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70  EM_Term;.      p
1a4ed 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29  Mem->z = (char*)
1a4ee 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d  sqlite3OpcodeNam
1a4ef 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 3b 20  e(pOp->opcode); 
1a4f0 20 2f 2a 20 4f 70 63 6f 64 65 20 2a 2f 0a 20 20   /* Opcode */.  
1a4f1 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
1a4f2 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  ->z!=0 );.      
1a4f3 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65  pMem->n = sqlite
1a4f4 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e  3Strlen30(pMem->
1a4f5 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  z);.      pMem->
1a4f6 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45  type = SQLITE_TE
1a4f7 58 54 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  XT;.      pMem->
1a4f8 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46  enc = SQLITE_UTF
1a4f9 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b  8;.      pMem++;
1a4fa 0a 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d  ..      if( pOp-
1a4fb 3e 70 34 74 79 70 65 3d 3d 50 34 5f 53 55 42 50  >p4type==P4_SUBP
1a4fc 52 4f 47 52 41 4d 20 29 7b 0a 20 20 20 20 20 20  ROGRAM ){.      
1a4fd 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 6e    int nByte = (n
1a4fe 53 75 62 2b 31 29 2a 73 69 7a 65 6f 66 28 53 75  Sub+1)*sizeof(Su
1a4ff 62 50 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20 20  bProgram*);.    
1a500 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20      int j;.     
1a501 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 53     for(j=0; j<nS
1a502 75 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  ub; j++){.      
1a503 20 20 20 20 69 66 28 20 61 70 53 75 62 5b 6a 5d      if( apSub[j]
1a504 3d 3d 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67 72  ==pOp->p4.pProgr
1a505 61 6d 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  am ) break;.    
1a506 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
1a507 28 20 6a 3d 3d 6e 53 75 62 20 26 26 20 53 51 4c  ( j==nSub && SQL
1a508 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56  ITE_OK==sqlite3V
1a509 64 62 65 4d 65 6d 47 72 6f 77 28 70 53 75 62 2c  dbeMemGrow(pSub,
1a50a 20 6e 42 79 74 65 2c 20 31 29 20 29 7b 0a 20 20   nByte, 1) ){.  
1a50b 20 20 20 20 20 20 20 20 61 70 53 75 62 20 3d 20          apSub = 
1a50c 28 53 75 62 50 72 6f 67 72 61 6d 20 2a 2a 29 70  (SubProgram **)p
1a50d 53 75 62 2d 3e 7a 3b 0a 20 20 20 20 20 20 20 20  Sub->z;.        
1a50e 20 20 61 70 53 75 62 5b 6e 53 75 62 2b 2b 5d 20    apSub[nSub++] 
1a50f 3d 20 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67 72  = pOp->p4.pProgr
1a510 61 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53  am;.          pS
1a511 75 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d  ub->flags |= MEM
1a512 5f 42 6c 6f 62 3b 0a 20 20 20 20 20 20 20 20 20  _Blob;.         
1a513 20 70 53 75 62 2d 3e 6e 20 3d 20 6e 53 75 62 2a   pSub->n = nSub*
1a514 73 69 7a 65 6f 66 28 53 75 62 50 72 6f 67 72 61  sizeof(SubProgra
1a515 6d 2a 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  m*);.        }. 
1a516 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
1a517 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
1a518 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d 65  MEM_Int;.    pMe
1a519 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 31  m->u.i = pOp->p1
1a51a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1a51b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 31             /* P1
1a51c 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79   */.    pMem->ty
1a51d 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45  pe = SQLITE_INTE
1a51e 47 45 52 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b  GER;.    pMem++;
1a51f 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  ..    pMem->flag
1a520 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20  s = MEM_Int;.   
1a521 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70   pMem->u.i = pOp
1a522 2d 3e 70 32 3b 20 20 20 20 20 20 20 20 20 20 20  ->p2;           
1a523 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1a524 2a 20 50 32 20 2a 2f 0a 20 20 20 20 70 4d 65 6d  * P2 */.    pMem
1a525 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
1a526 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 4d 65  INTEGER;.    pMe
1a527 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70 2d  m++;..    if( p-
1a528 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20  >explain==1 ){. 
1a529 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73       pMem->flags
1a52a 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20   = MEM_Int;.    
1a52b 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f    pMem->u.i = pO
1a52c 70 2d 3e 70 33 3b 20 20 20 20 20 20 20 20 20 20  p->p3;          
1a52d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a52e 2f 2a 20 50 33 20 2a 2f 0a 20 20 20 20 20 20 70  /* P3 */.      p
1a52f 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
1a530 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20  TE_INTEGER;.    
1a531 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 20 20 7d 0a    pMem++;.    }.
1a532 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1a533 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d  VdbeMemGrow(pMem
1a534 2c 20 33 32 2c 20 30 29 20 29 7b 20 20 20 20 20  , 32, 0) ){     
1a535 20 20 20 20 20 20 20 2f 2a 20 50 34 20 2a 2f 0a         /* P4 */.
1a536 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
1a537 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
1a538 64 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  d );.      retur
1a539 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
1a53a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e      }.    pMem->
1a53b 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e 7c  flags = MEM_Dyn|
1a53c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d  MEM_Str|MEM_Term
1a53d 3b 0a 20 20 20 20 7a 20 3d 20 64 69 73 70 6c 61  ;.    z = displa
1a53e 79 50 34 28 70 4f 70 2c 20 70 4d 65 6d 2d 3e 7a  yP4(pOp, pMem->z
1a53f 2c 20 33 32 29 3b 0a 20 20 20 20 69 66 28 20 7a  , 32);.    if( z
1a540 21 3d 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20  !=pMem->z ){.   
1a541 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1a542 6d 53 65 74 53 74 72 28 70 4d 65 6d 2c 20 7a 2c  mSetStr(pMem, z,
1a543 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38   -1, SQLITE_UTF8
1a544 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  , 0);.    }else{
1a545 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1a546 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20  Mem->z!=0 );.   
1a547 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c     pMem->n = sql
1a548 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d 65  ite3Strlen30(pMe
1a549 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65  m->z);.      pMe
1a54a 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f  m->enc = SQLITE_
1a54b 55 54 46 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20  UTF8;.    }.    
1a54c 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
1a54d 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 70 4d  ITE_TEXT;.    pM
1a54e 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70  em++;..    if( p
1a54f 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a  ->explain==1 ){.
1a550 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
1a551 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65  3VdbeMemGrow(pMe
1a552 6d 2c 20 34 2c 20 30 29 20 29 7b 0a 20 20 20 20  m, 4, 0) ){.    
1a553 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64      assert( p->d
1a554 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1a555 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
1a556 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
1a557 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4d        }.      pM
1a558 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
1a559 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f  Dyn|MEM_Str|MEM_
1a55a 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65 6d  Term;.      pMem
1a55b 2d 3e 6e 20 3d 20 32 3b 0a 20 20 20 20 20 20 73  ->n = 2;.      s
1a55c 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1a55d 33 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 2e 32  3, pMem->z, "%.2
1a55e 78 22 2c 20 70 4f 70 2d 3e 70 35 29 3b 20 20 20  x", pOp->p5);   
1a55f 2f 2a 20 50 35 20 2a 2f 0a 20 20 20 20 20 20 70  /* P5 */.      p
1a560 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
1a561 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 70  TE_TEXT;.      p
1a562 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54  Mem->enc = SQLIT
1a563 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70 4d  E_UTF8;.      pM
1a564 65 6d 2b 2b 3b 0a 20 20 0a 23 69 66 64 65 66 20  em++;.  .#ifdef 
1a565 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20  SQLITE_DEBUG.   
1a566 20 20 20 69 66 28 20 70 4f 70 2d 3e 7a 43 6f 6d     if( pOp->zCom
1a567 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20  ment ){.        
1a568 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
1a569 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a  M_Str|MEM_Term;.
1a56a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20          pMem->z 
1a56b 3d 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 3b  = pOp->zComment;
1a56c 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e  .        pMem->n
1a56d 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
1a56e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20  30(pMem->z);.   
1a56f 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d       pMem->enc =
1a570 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20   SQLITE_UTF8;.  
1a571 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65        pMem->type
1a572 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a   = SQLITE_TEXT;.
1a573 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64        }else.#end
1a574 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20  if.      {.     
1a575 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
1a576 20 4d 45 4d 5f 4e 75 6c 6c 3b 20 20 20 20 20 20   MEM_Null;      
1a577 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a578 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 2a 2f 0a 20   /* Comment */. 
1a579 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70         pMem->typ
1a57a 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b  e = SQLITE_NULL;
1a57b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
1a57c 20 20 20 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d      p->nResColum
1a57d 6e 20 3d 20 38 20 2d 20 35 2a 28 70 2d 3e 65 78  n = 8 - 5*(p->ex
1a57e 70 6c 61 69 6e 2d 31 29 3b 0a 20 20 20 20 70 2d  plain-1);.    p-
1a57f 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  >rc = SQLITE_OK;
1a580 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1a581 5f 52 4f 57 3b 0a 20 20 7d 0a 20 20 72 65 74 75  _ROW;.  }.  retu
1a582 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20  rn rc;.}.#endif 
1a583 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45  /* SQLITE_OMIT_E
1a584 58 50 4c 41 49 4e 20 2a 2f 0a 0a 23 69 66 64 65  XPLAIN */..#ifde
1a585 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f  f SQLITE_DEBUG./
1a586 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 20 53  *.** Print the S
1a587 51 4c 20 74 68 61 74 20 77 61 73 20 75 73 65 64  QL that was used
1a588 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 56   to generate a V
1a589 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a  DBE program..*/.
1a58a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1a58b 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 50  oid sqlite3VdbeP
1a58c 72 69 6e 74 53 71 6c 28 56 64 62 65 20 2a 70 29  rintSql(Vdbe *p)
1a58d 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d 20 70 2d  {.  int nOp = p-
1a58e 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f 70 20 2a  >nOp;.  VdbeOp *
1a58f 70 4f 70 3b 0a 20 20 69 66 28 20 6e 4f 70 3c 31  pOp;.  if( nOp<1
1a590 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f 70   ) return;.  pOp
1a591 20 3d 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b 0a 20   = &p->aOp[0];. 
1a592 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
1a593 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 20 70 4f  ==OP_Trace && pO
1a594 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a 20 20  p->p4.z!=0 ){.  
1a595 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20    const char *z 
1a596 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20  = pOp->p4.z;.   
1a597 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49   while( sqlite3I
1a598 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b 2b  sspace(*z) ) z++
1a599 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 53 51  ;.    printf("SQ
1a59a 4c 3a 20 5b 25 73 5d 5c 6e 22 2c 20 7a 29 3b 0a  L: [%s]\n", z);.
1a59b 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69    }.}.#endif..#i
1a59c 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
1a59d 45 5f 4f 4d 49 54 5f 54 52 41 43 45 29 20 26 26  E_OMIT_TRACE) &&
1a59e 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
1a59f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 29 0a  ENABLE_IOTRACE).
1a5a0 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 61 6e 20 49  /*.** Print an I
1a5a1 4f 54 52 41 43 45 20 6d 65 73 73 61 67 65 20 73  OTRACE message s
1a5a2 68 6f 77 69 6e 67 20 53 51 4c 20 63 6f 6e 74 65  howing SQL conte
1a5a3 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nt..*/.SQLITE_PR
1a5a4 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1a5a5 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c  e3VdbeIOTraceSql
1a5a6 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74  (Vdbe *p){.  int
1a5a7 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20   nOp = p->nOp;. 
1a5a8 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20   VdbeOp *pOp;.  
1a5a9 69 66 28 20 73 71 6c 69 74 65 33 49 6f 54 72 61  if( sqlite3IoTra
1a5aa 63 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  ce==0 ) return;.
1a5ab 20 20 69 66 28 20 6e 4f 70 3c 31 20 29 20 72 65    if( nOp<1 ) re
1a5ac 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26 70  turn;.  pOp = &p
1a5ad 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28 20  ->aOp[0];.  if( 
1a5ae 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
1a5af 54 72 61 63 65 20 26 26 20 70 4f 70 2d 3e 70 34  Trace && pOp->p4
1a5b0 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  .z!=0 ){.    int
1a5b1 20 69 2c 20 6a 3b 0a 20 20 20 20 63 68 61 72 20   i, j;.    char 
1a5b2 7a 5b 31 30 30 30 5d 3b 0a 20 20 20 20 73 71 6c  z[1000];.    sql
1a5b3 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
1a5b4 7a 65 6f 66 28 7a 29 2c 20 7a 2c 20 22 25 73 22  zeof(z), z, "%s"
1a5b5 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20  , pOp->p4.z);.  
1a5b6 20 20 66 6f 72 28 69 3d 30 3b 20 73 71 6c 69 74    for(i=0; sqlit
1a5b7 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 3b  e3Isspace(z[i]);
1a5b8 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 66 6f 72 28   i++){}.    for(
1a5b9 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29 7b  j=0; z[i]; i++){
1a5ba 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
1a5bb 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 20  e3Isspace(z[i]) 
1a5bc 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a  ){.        if( z
1a5bd 5b 69 2d 31 5d 21 3d 27 20 27 20 29 7b 0a 20 20  [i-1]!=' ' ){.  
1a5be 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d          z[j++] =
1a5bf 20 27 20 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a   ' ';.        }.
1a5c0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1a5c1 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b       z[j++] = z[
1a5c2 69 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  i];.      }.    
1a5c3 7d 0a 20 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a  }.    z[j] = 0;.
1a5c4 20 20 20 20 73 71 6c 69 74 65 33 49 6f 54 72 61      sqlite3IoTra
1a5c5 63 65 28 22 53 51 4c 20 25 73 5c 6e 22 2c 20 7a  ce("SQL %s\n", z
1a5c6 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20  );.  }.}.#endif 
1a5c7 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  /* !SQLITE_OMIT_
1a5c8 54 52 41 43 45 20 26 26 20 53 51 4c 49 54 45 5f  TRACE && SQLITE_
1a5c9 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 20 2a  ENABLE_IOTRACE *
1a5ca 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  /../*.** Allocat
1a5cb 65 20 73 70 61 63 65 20 66 72 6f 6d 20 61 20 66  e space from a f
1a5cc 69 78 65 64 20 73 69 7a 65 20 62 75 66 66 65 72  ixed size buffer
1a5cd 2e 20 20 4d 61 6b 65 20 2a 70 70 20 70 6f 69 6e  .  Make *pp poin
1a5ce 74 20 74 6f 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f  t to the.** allo
1a5cf 63 61 74 65 64 20 73 70 61 63 65 2e 20 20 28 4e  cated space.  (N
1a5d0 6f 74 65 3a 20 20 70 70 20 69 73 20 61 20 63 68  ote:  pp is a ch
1a5d1 61 72 2a 20 72 61 74 68 65 72 20 74 68 61 6e 20  ar* rather than 
1a5d2 61 20 76 6f 69 64 2a 2a 20 74 6f 0a 2a 2a 20 77  a void** to.** w
1a5d3 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 65 20 70  ork around the p
1a5d4 6f 69 6e 74 65 72 20 61 6c 69 61 73 69 6e 67 20  ointer aliasing 
1a5d5 72 75 6c 65 73 20 6f 66 20 43 2e 29 20 20 2a 70  rules of C.)  *p
1a5d6 70 20 73 68 6f 75 6c 64 20 69 6e 69 74 69 61 6c  p should initial
1a5d7 6c 79 0a 2a 2a 20 62 65 20 7a 65 72 6f 2e 20 20  ly.** be zero.  
1a5d8 49 66 20 2a 70 70 20 69 73 20 6e 6f 74 20 7a 65  If *pp is not ze
1a5d9 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74  ro, that means t
1a5da 68 61 74 20 74 68 65 20 73 70 61 63 65 20 68 61  hat the space ha
1a5db 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 65  s already.** bee
1a5dc 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20  n allocated and 
1a5dd 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
1a5de 61 20 6e 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 6e 42  a noop..**.** nB
1a5df 79 74 65 20 69 73 20 74 68 65 20 6e 75 6d 62 65  yte is the numbe
1a5e0 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70  r of bytes of sp
1a5e1 61 63 65 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a  ace needed..**.*
1a5e2 2a 20 2a 70 70 46 72 6f 6d 20 70 6f 69 6e 74 20  * *ppFrom point 
1a5e3 74 6f 20 61 76 61 69 6c 61 62 6c 65 20 73 70 61  to available spa
1a5e4 63 65 20 61 6e 64 20 70 45 6e 64 20 70 6f 69 6e  ce and pEnd poin
1a5e5 74 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  ts to the end of
1a5e6 20 74 68 65 0a 2a 2a 20 61 76 61 69 6c 61 62 6c   the.** availabl
1a5e7 65 20 73 70 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 2a  e space..**.** *
1a5e8 70 6e 42 79 74 65 20 69 73 20 61 20 63 6f 75 6e  pnByte is a coun
1a5e9 74 65 72 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  ter of the numbe
1a5ea 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70  r of bytes of sp
1a5eb 61 63 65 20 74 68 61 74 20 68 61 76 65 20 66 61  ace that have fa
1a5ec 69 6c 65 64 0a 2a 2a 20 74 6f 20 61 6c 6c 6f 63  iled.** to alloc
1a5ed 61 74 65 2e 20 20 49 66 20 74 68 65 72 65 20 69  ate.  If there i
1a5ee 73 20 69 6e 73 75 66 66 69 63 69 65 6e 74 20 73  s insufficient s
1a5ef 70 61 63 65 20 69 6e 20 2a 70 70 46 72 6f 6d 20  pace in *ppFrom 
1a5f0 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a 2a  to satisfy the.*
1a5f1 2a 20 72 65 71 75 65 73 74 2c 20 74 68 65 6e 20  * request, then 
1a5f2 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e 42 79 74  increment *pnByt
1a5f3 65 20 62 79 20 74 68 65 20 61 6d 6f 75 6e 74 20  e by the amount 
1a5f4 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 2e 0a  of the request..
1a5f5 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
1a5f6 6c 6c 6f 63 53 70 61 63 65 28 0a 20 20 63 68 61  llocSpace(.  cha
1a5f7 72 20 2a 70 70 2c 20 20 20 20 20 20 20 20 20 20  r *pp,          
1a5f8 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 53 65 74    /* IN/OUT: Set
1a5f9 20 2a 70 70 20 74 6f 20 70 6f 69 6e 74 20 74 6f   *pp to point to
1a5fa 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65   allocated buffe
1a5fb 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  r */.  int nByte
1a5fc 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ,           /* N
1a5fd 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
1a5fe 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20  o allocate */.  
1a5ff 75 38 20 2a 2a 70 70 46 72 6f 6d 2c 20 20 20 20  u8 **ppFrom,    
1a600 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20       /* IN/OUT: 
1a601 41 6c 6c 6f 63 61 74 65 20 66 72 6f 6d 20 2a 70  Allocate from *p
1a602 70 46 72 6f 6d 20 2a 2f 0a 20 20 75 38 20 2a 70  pFrom */.  u8 *p
1a603 45 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20 20  End,            
1a604 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 31 20  /* Pointer to 1 
1a605 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65 6e  byte past the en
1a606 64 20 6f 66 20 2a 70 70 46 72 6f 6d 20 62 75 66  d of *ppFrom buf
1a607 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  fer */.  int *pn
1a608 42 79 74 65 20 20 20 20 20 20 20 20 20 20 2f 2a  Byte          /*
1a609 20 49 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 63   If allocation c
1a60a 61 6e 6e 6f 74 20 62 65 20 6d 61 64 65 2c 20 69  annot be made, i
1a60b 6e 63 72 65 6d 65 6e 74 20 2a 70 6e 42 79 74 65  ncrement *pnByte
1a60c 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28   */.){.  assert(
1a60d 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47   EIGHT_BYTE_ALIG
1a60e 4e 4d 45 4e 54 28 2a 70 70 46 72 6f 6d 29 20 29  NMENT(*ppFrom) )
1a60f 3b 0a 20 20 69 66 28 20 28 2a 28 76 6f 69 64 2a  ;.  if( (*(void*
1a610 2a 29 70 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20  *)pp)==0 ){.    
1a611 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 28 6e  nByte = ROUND8(n
1a612 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20 26  Byte);.    if( &
1a613 28 2a 70 70 46 72 6f 6d 29 5b 6e 42 79 74 65 5d  (*ppFrom)[nByte]
1a614 20 3c 3d 20 70 45 6e 64 20 29 7b 0a 20 20 20 20   <= pEnd ){.    
1a615 20 20 2a 28 76 6f 69 64 2a 2a 29 70 70 20 3d 20    *(void**)pp = 
1a616 28 76 6f 69 64 20 2a 29 2a 70 70 46 72 6f 6d 3b  (void *)*ppFrom;
1a617 0a 20 20 20 20 20 20 2a 70 70 46 72 6f 6d 20 2b  .      *ppFrom +
1a618 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 7d 65 6c  = nByte;.    }el
1a619 73 65 7b 0a 20 20 20 20 20 20 2a 70 6e 42 79 74  se{.      *pnByt
1a61a 65 20 2b 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20  e += nByte;.    
1a61b 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  }.  }.}../*.** P
1a61c 72 65 70 61 72 65 20 61 20 76 69 72 74 75 61 6c  repare a virtual
1a61d 20 6d 61 63 68 69 6e 65 20 66 6f 72 20 65 78 65   machine for exe
1a61e 63 75 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 6e  cution.  This in
1a61f 76 6f 6c 76 65 73 20 74 68 69 6e 67 73 20 73 75  volves things su
1a620 63 68 0a 2a 2a 20 61 73 20 61 6c 6c 6f 63 61 74  ch.** as allocat
1a621 69 6e 67 20 73 74 61 63 6b 20 73 70 61 63 65 20  ing stack space 
1a622 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67  and initializing
1a623 20 74 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75   the program cou
1a624 6e 74 65 72 2e 0a 2a 2a 20 41 66 74 65 72 20 74  nter..** After t
1a625 68 65 20 56 44 42 45 20 68 61 73 20 62 65 20 70  he VDBE has be p
1a626 72 65 70 70 65 64 2c 20 69 74 20 63 61 6e 20 62  repped, it can b
1a627 65 20 65 78 65 63 75 74 65 64 20 62 79 20 6f 6e  e executed by on
1a628 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 63 61 6c  e or more.** cal
1a629 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62  ls to sqlite3Vdb
1a62a 65 45 78 65 63 28 29 2e 20 20 0a 2a 2a 0a 2a 2a  eExec().  .**.**
1a62b 20 54 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c   This is the onl
1a62c 79 20 77 61 79 20 74 6f 20 6d 6f 76 65 20 61 20  y way to move a 
1a62d 56 44 42 45 20 66 72 6f 6d 20 56 44 42 45 5f 4d  VDBE from VDBE_M
1a62e 41 47 49 43 5f 49 4e 49 54 20 74 6f 0a 2a 2a 20  AGIC_INIT to.** 
1a62f 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 2e 0a  VDBE_MAGIC_RUN..
1a630 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
1a631 69 6f 6e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65  ion may be calle
1a632 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  d more than once
1a633 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 76 69 72   on a single vir
1a634 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a  tual machine..**
1a635 20 54 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20   The first call 
1a636 69 73 20 6d 61 64 65 20 77 68 69 6c 65 20 63 6f  is made while co
1a637 6d 70 69 6c 69 6e 67 20 74 68 65 20 53 51 4c 20  mpiling the SQL 
1a638 73 74 61 74 65 6d 65 6e 74 2e 20 53 75 62 73 65  statement. Subse
1a639 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 61  quent.** calls a
1a63a 72 65 20 6d 61 64 65 20 61 73 20 70 61 72 74 20  re made as part 
1a63b 6f 66 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f  of the process o
1a63c 66 20 72 65 73 65 74 74 69 6e 67 20 61 20 73 74  f resetting a st
1a63d 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 0a 2a 2a  atement to be.**
1a63e 20 72 65 2d 65 78 65 63 75 74 65 64 20 28 66 72   re-executed (fr
1a63f 6f 6d 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c  om a call to sql
1a640 69 74 65 33 5f 72 65 73 65 74 28 29 29 2e 20 54  ite3_reset()). T
1a641 68 65 20 6e 56 61 72 2c 20 6e 4d 65 6d 2c 20 6e  he nVar, nMem, n
1a642 43 75 72 73 6f 72 20 0a 2a 2a 20 61 6e 64 20 69  Cursor .** and i
1a643 73 45 78 70 6c 61 69 6e 20 70 61 72 61 6d 65 74  sExplain paramet
1a644 65 72 73 20 61 72 65 20 6f 6e 6c 79 20 70 61 73  ers are only pas
1a645 73 65 64 20 63 6f 72 72 65 63 74 20 76 61 6c 75  sed correct valu
1a646 65 73 20 74 68 65 20 66 69 72 73 74 20 74 69 6d  es the first tim
1a647 65 0a 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 6f  e.** the functio
1a648 6e 20 69 73 20 63 61 6c 6c 65 64 2e 20 4f 6e 20  n is called. On 
1a649 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73  subsequent calls
1a64a 2c 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72  , from sqlite3_r
1a64b 65 73 65 74 28 29 2c 20 6e 56 61 72 0a 2a 2a 20  eset(), nVar.** 
1a64c 69 73 20 70 61 73 73 65 64 20 2d 31 20 61 6e 64  is passed -1 and
1a64d 20 6e 4d 65 6d 2c 20 6e 43 75 72 73 6f 72 20 61   nMem, nCursor a
1a64e 6e 64 20 69 73 45 78 70 6c 61 69 6e 20 61 72 65  nd isExplain are
1a64f 20 61 6c 6c 20 70 61 73 73 65 64 20 7a 65 72 6f   all passed zero
1a650 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1a651 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1a652 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 0a 20  VdbeMakeReady(. 
1a653 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20   Vdbe *p,       
1a654 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a655 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f 0a 20  /* The VDBE */. 
1a656 20 69 6e 74 20 6e 56 61 72 2c 20 20 20 20 20 20   int nVar,      
1a657 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a658 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 27 3f 27  /* Number of '?'
1a659 20 73 65 65 20 69 6e 20 74 68 65 20 53 51 4c 20   see in the SQL 
1a65a 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69  statement */.  i
1a65b 6e 74 20 6e 4d 65 6d 2c 20 20 20 20 20 20 20 20  nt nMem,        
1a65c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a65d 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72   Number of memor
1a65e 79 20 63 65 6c 6c 73 20 74 6f 20 61 6c 6c 6f 63  y cells to alloc
1a65f 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75  ate */.  int nCu
1a660 72 73 6f 72 2c 20 20 20 20 20 20 20 20 20 20 20  rsor,           
1a661 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1a662 72 20 6f 66 20 63 75 72 73 6f 72 73 20 74 6f 20  r of cursors to 
1a663 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 69 6e  allocate */.  in
1a664 74 20 6e 41 72 67 2c 20 20 20 20 20 20 20 20 20  t nArg,         
1a665 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a666 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  Maximum number o
1a667 66 20 61 72 67 73 20 69 6e 20 53 75 62 50 72 6f  f args in SubPro
1a668 67 72 61 6d 73 20 2a 2f 0a 20 20 69 6e 74 20 69  grams */.  int i
1a669 73 45 78 70 6c 61 69 6e 2c 20 20 20 20 20 20 20  sExplain,       
1a66a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
1a66b 65 20 69 66 20 74 68 65 20 45 58 50 4c 41 49 4e  e if the EXPLAIN
1a66c 20 6b 65 79 77 6f 72 64 73 20 69 73 20 70 72 65   keywords is pre
1a66d 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 75 73  sent */.  int us
1a66e 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 20 20  esStmtJournal   
1a66f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
1a670 65 20 74 6f 20 73 65 74 20 56 64 62 65 2e 75 73  e to set Vdbe.us
1a671 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 2a 2f  esStmtJournal */
1a672 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73  .){.  int n;.  s
1a673 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e  qlite3 *db = p->
1a674 64 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  db;..  assert( p
1a675 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
1a676 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f   p->magic==VDBE_
1a677 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 0a 20  MAGIC_INIT );.. 
1a678 20 2f 2a 20 54 68 65 72 65 20 73 68 6f 75 6c 64   /* There should
1a679 20 62 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 65   be at least one
1a67a 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20   opcode..  */.  
1a67b 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30  assert( p->nOp>0
1a67c 20 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68   );..  /* Set th
1a67d 65 20 6d 61 67 69 63 20 74 6f 20 56 44 42 45 5f  e magic to VDBE_
1a67e 4d 41 47 49 43 5f 52 55 4e 20 73 6f 6f 6e 65 72  MAGIC_RUN sooner
1a67f 20 72 61 74 68 65 72 20 74 68 61 6e 20 6c 61 74   rather than lat
1a680 65 72 2e 20 2a 2f 0a 20 20 70 2d 3e 6d 61 67 69  er. */.  p->magi
1a681 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 52  c = VDBE_MAGIC_R
1a682 55 4e 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20 65 61  UN;..  /* For ea
1a683 63 68 20 63 75 72 73 6f 72 20 72 65 71 75 69 72  ch cursor requir
1a684 65 64 2c 20 61 6c 73 6f 20 61 6c 6c 6f 63 61 74  ed, also allocat
1a685 65 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e  e a memory cell.
1a686 20 4d 65 6d 6f 72 79 0a 20 20 2a 2a 20 63 65 6c   Memory.  ** cel
1a687 6c 73 20 28 6e 4d 65 6d 2b 31 2d 6e 43 75 72 73  ls (nMem+1-nCurs
1a688 6f 72 29 2e 2e 6e 4d 65 6d 2c 20 69 6e 63 6c 75  or)..nMem, inclu
1a689 73 69 76 65 2c 20 77 69 6c 6c 20 6e 65 76 65 72  sive, will never
1a68a 20 62 65 20 75 73 65 64 20 62 79 0a 20 20 2a 2a   be used by.  **
1a68b 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61   the vdbe progra
1a68c 6d 2e 20 49 6e 73 74 65 61 64 20 74 68 65 79 20  m. Instead they 
1a68d 61 72 65 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f  are used to allo
1a68e 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 0a 20  cate space for. 
1a68f 20 2a 2a 20 56 64 62 65 43 75 72 73 6f 72 2f 42   ** VdbeCursor/B
1a690 74 43 75 72 73 6f 72 20 73 74 72 75 63 74 75 72  tCursor structur
1a691 65 73 2e 20 54 68 65 20 62 6c 6f 62 20 6f 66 20  es. The blob of 
1a692 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65  memory associate
1a693 64 20 77 69 74 68 20 0a 20 20 2a 2a 20 63 75 72  d with .  ** cur
1a694 73 6f 72 20 30 20 69 73 20 73 74 6f 72 65 64 20  sor 0 is stored 
1a695 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 6e  in memory cell n
1a696 4d 65 6d 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c  Mem. Memory cell
1a697 20 28 6e 4d 65 6d 2d 31 29 0a 20 20 2a 2a 20 73   (nMem-1).  ** s
1a698 74 6f 72 65 73 20 74 68 65 20 62 6c 6f 62 20 6f  tores the blob o
1a699 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61  f memory associa
1a69a 74 65 64 20 77 69 74 68 20 63 75 72 73 6f 72 20  ted with cursor 
1a69b 31 2c 20 65 74 63 2e 0a 20 20 2a 2a 0a 20 20 2a  1, etc..  **.  *
1a69c 2a 20 53 65 65 20 61 6c 73 6f 3a 20 61 6c 6c 6f  * See also: allo
1a69d 63 61 74 65 43 75 72 73 6f 72 28 29 2e 0a 20 20  cateCursor()..  
1a69e 2a 2f 0a 20 20 6e 4d 65 6d 20 2b 3d 20 6e 43 75  */.  nMem += nCu
1a69f 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  rsor;..  /* Allo
1a6a0 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 6d  cate space for m
1a6a1 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 2c  emory registers,
1a6a2 20 53 51 4c 20 76 61 72 69 61 62 6c 65 73 2c 20   SQL variables, 
1a6a3 56 44 42 45 20 63 75 72 73 6f 72 73 20 61 6e 64  VDBE cursors and
1a6a4 20 0a 20 20 2a 2a 20 61 6e 20 61 72 72 61 79 20   .  ** an array 
1a6a5 74 6f 20 6d 61 72 73 68 61 6c 20 53 51 4c 20 66  to marshal SQL f
1a6a6 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74  unction argument
1a6a7 73 20 69 6e 2e 20 54 68 69 73 20 69 73 20 6f 6e  s in. This is on
1a6a8 6c 79 20 64 6f 6e 65 20 74 68 65 0a 20 20 2a 2a  ly done the.  **
1a6a9 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73   first time this
1a6aa 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
1a6ab 6c 65 64 20 66 6f 72 20 61 20 67 69 76 65 6e 20  led for a given 
1a6ac 56 44 42 45 2c 20 6e 6f 74 20 77 68 65 6e 20 69  VDBE, not when i
1a6ad 74 20 69 73 0a 20 20 2a 2a 20 62 65 69 6e 67 20  t is.  ** being 
1a6ae 63 61 6c 6c 65 64 20 66 72 6f 6d 20 73 71 6c 69  called from sqli
1a6af 74 65 33 5f 72 65 73 65 74 28 29 20 74 6f 20 72  te3_reset() to r
1a6b0 65 73 65 74 20 74 68 65 20 76 69 72 74 75 61 6c  eset the virtual
1a6b1 20 6d 61 63 68 69 6e 65 2e 0a 20 20 2a 2f 0a 20   machine..  */. 
1a6b2 20 69 66 28 20 6e 56 61 72 3e 3d 30 20 26 26 20   if( nVar>=0 && 
1a6b3 41 4c 57 41 59 53 28 64 62 2d 3e 6d 61 6c 6c 6f  ALWAYS(db->mallo
1a6b4 63 46 61 69 6c 65 64 3d 3d 30 29 20 29 7b 0a 20  cFailed==0) ){. 
1a6b5 20 20 20 75 38 20 2a 7a 43 73 72 20 3d 20 28 75     u8 *zCsr = (u
1a6b6 38 20 2a 29 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e  8 *)&p->aOp[p->n
1a6b7 4f 70 5d 3b 0a 20 20 20 20 75 38 20 2a 7a 45 6e  Op];.    u8 *zEn
1a6b8 64 20 3d 20 28 75 38 20 2a 29 26 70 2d 3e 61 4f  d = (u8 *)&p->aO
1a6b9 70 5b 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 5d 3b 0a  p[p->nOpAlloc];.
1a6ba 20 20 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20      int nByte;. 
1a6bb 20 20 20 72 65 73 6f 6c 76 65 50 32 56 61 6c 75     resolveP2Valu
1a6bc 65 73 28 70 2c 20 26 6e 41 72 67 29 3b 0a 20 20  es(p, &nArg);.  
1a6bd 20 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75    p->usesStmtJou
1a6be 72 6e 61 6c 20 3d 20 75 73 65 73 53 74 6d 74 4a  rnal = usesStmtJ
1a6bf 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 69 66 28 20  ournal;.    if( 
1a6c0 69 73 45 78 70 6c 61 69 6e 20 26 26 20 6e 4d 65  isExplain && nMe
1a6c1 6d 3c 31 30 20 29 7b 0a 20 20 20 20 20 20 6e 4d  m<10 ){.      nM
1a6c2 65 6d 20 3d 20 31 30 3b 0a 20 20 20 20 7d 0a 20  em = 10;.    }. 
1a6c3 20 20 20 6d 65 6d 73 65 74 28 7a 43 73 72 2c 20     memset(zCsr, 
1a6c4 30 2c 20 7a 45 6e 64 2d 7a 43 73 72 29 3b 0a 20  0, zEnd-zCsr);. 
1a6c5 20 20 20 7a 43 73 72 20 2b 3d 20 28 7a 43 73 72     zCsr += (zCsr
1a6c6 20 2d 20 28 75 38 2a 29 30 29 26 37 3b 0a 20 20   - (u8*)0)&7;.  
1a6c7 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f    assert( EIGHT_
1a6c8 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 7a  BYTE_ALIGNMENT(z
1a6c9 43 73 72 29 20 29 3b 0a 0a 20 20 20 20 64 6f 20  Csr) );..    do 
1a6ca 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20  {.      nByte = 
1a6cb 30 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70  0;.      allocSp
1a6cc 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61  ace((char*)&p->a
1a6cd 4d 65 6d 2c 20 6e 4d 65 6d 2a 73 69 7a 65 6f 66  Mem, nMem*sizeof
1a6ce 28 4d 65 6d 29 2c 20 26 7a 43 73 72 2c 20 7a 45  (Mem), &zCsr, zE
1a6cf 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20  nd, &nByte);.   
1a6d0 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 63     allocSpace((c
1a6d1 68 61 72 2a 29 26 70 2d 3e 61 56 61 72 2c 20 6e  har*)&p->aVar, n
1a6d2 56 61 72 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 2c  Var*sizeof(Mem),
1a6d3 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e   &zCsr, zEnd, &n
1a6d4 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c  Byte);.      all
1a6d5 6f 63 53 70 61 63 65 28 28 63 68 61 72 2a 29 26  ocSpace((char*)&
1a6d6 70 2d 3e 61 70 41 72 67 2c 20 6e 41 72 67 2a 73  p->apArg, nArg*s
1a6d7 69 7a 65 6f 66 28 4d 65 6d 2a 29 2c 20 26 7a 43  izeof(Mem*), &zC
1a6d8 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65  sr, zEnd, &nByte
1a6d9 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70  );.      allocSp
1a6da 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61  ace((char*)&p->a
1a6db 7a 56 61 72 2c 20 6e 56 61 72 2a 73 69 7a 65 6f  zVar, nVar*sizeo
1a6dc 66 28 63 68 61 72 2a 29 2c 20 26 7a 43 73 72 2c  f(char*), &zCsr,
1a6dd 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a   zEnd, &nByte);.
1a6de 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 65        allocSpace
1a6df 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 70 43 73  ((char*)&p->apCs
1a6e0 72 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  r, .            
1a6e1 20 20 20 20 20 6e 43 75 72 73 6f 72 2a 73 69 7a       nCursor*siz
1a6e2 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 2a 29  eof(VdbeCursor*)
1a6e3 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26  , &zCsr, zEnd, &
1a6e4 6e 42 79 74 65 0a 20 20 20 20 20 20 29 3b 0a 20  nByte.      );. 
1a6e5 20 20 20 20 20 69 66 28 20 6e 42 79 74 65 20 29       if( nByte )
1a6e6 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 46 72  {.        p->pFr
1a6e7 65 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ee = sqlite3DbMa
1a6e8 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42 79  llocZero(db, nBy
1a6e9 74 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  te);.      }.   
1a6ea 20 20 20 7a 43 73 72 20 3d 20 70 2d 3e 70 46 72     zCsr = p->pFr
1a6eb 65 65 3b 0a 20 20 20 20 20 20 7a 45 6e 64 20 3d  ee;.      zEnd =
1a6ec 20 26 7a 43 73 72 5b 6e 42 79 74 65 5d 3b 0a 20   &zCsr[nByte];. 
1a6ed 20 20 20 7d 77 68 69 6c 65 28 20 6e 42 79 74 65     }while( nByte
1a6ee 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46   && !db->mallocF
1a6ef 61 69 6c 65 64 20 29 3b 0a 0a 20 20 20 20 70 2d  ailed );..    p-
1a6f0 3e 6e 43 75 72 73 6f 72 20 3d 20 28 75 31 36 29  >nCursor = (u16)
1a6f1 6e 43 75 72 73 6f 72 3b 0a 20 20 20 20 69 66 28  nCursor;.    if(
1a6f2 20 70 2d 3e 61 56 61 72 20 29 7b 0a 20 20 20 20   p->aVar ){.    
1a6f3 20 20 70 2d 3e 6e 56 61 72 20 3d 20 28 75 31 36    p->nVar = (u16
1a6f4 29 6e 56 61 72 3b 0a 20 20 20 20 20 20 66 6f 72  )nVar;.      for
1a6f5 28 6e 3d 30 3b 20 6e 3c 6e 56 61 72 3b 20 6e 2b  (n=0; n<nVar; n+
1a6f6 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61  +){.        p->a
1a6f7 56 61 72 5b 6e 5d 2e 66 6c 61 67 73 20 3d 20 4d  Var[n].flags = M
1a6f8 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 20  EM_Null;.       
1a6f9 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 64 62 20 3d   p->aVar[n].db =
1a6fa 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   db;.      }.   
1a6fb 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 61 4d   }.    if( p->aM
1a6fc 65 6d 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61  em ){.      p->a
1a6fd 4d 65 6d 2d 2d 3b 20 20 20 20 20 20 20 20 20 20  Mem--;          
1a6fe 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61              /* a
1a6ff 4d 65 6d 5b 5d 20 67 6f 65 73 20 66 72 6f 6d 20  Mem[] goes from 
1a700 31 2e 2e 6e 4d 65 6d 20 2a 2f 0a 20 20 20 20 20  1..nMem */.     
1a701 20 70 2d 3e 6e 4d 65 6d 20 3d 20 6e 4d 65 6d 3b   p->nMem = nMem;
1a702 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a703 20 2f 2a 20 20 20 20 20 20 20 6e 6f 74 20 66 72   /*       not fr
1a704 6f 6d 20 30 2e 2e 6e 4d 65 6d 2d 31 20 2a 2f 0a  om 0..nMem-1 */.
1a705 20 20 20 20 20 20 66 6f 72 28 6e 3d 31 3b 20 6e        for(n=1; n
1a706 3c 3d 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b 0a 20 20  <=nMem; n++){.  
1a707 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d 5b 6e 5d        p->aMem[n]
1a708 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c  .flags = MEM_Nul
1a709 6c 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 4d  l;.        p->aM
1a70a 65 6d 5b 6e 5d 2e 64 62 20 3d 20 64 62 3b 0a 20  em[n].db = db;. 
1a70b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
1a70c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
1a70d 45 42 55 47 0a 20 20 66 6f 72 28 6e 3d 31 3b 20  EBUG.  for(n=1; 
1a70e 6e 3c 70 2d 3e 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b  n<p->nMem; n++){
1a70f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
1a710 61 4d 65 6d 5b 6e 5d 2e 64 62 3d 3d 64 62 20 29  aMem[n].db==db )
1a711 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
1a712 70 2d 3e 70 63 20 3d 20 2d 31 3b 0a 20 20 70 2d  p->pc = -1;.  p-
1a713 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  >rc = SQLITE_OK;
1a714 0a 20 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f  .  p->errorActio
1a715 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20  n = OE_Abort;.  
1a716 70 2d 3e 65 78 70 6c 61 69 6e 20 7c 3d 20 69 73  p->explain |= is
1a717 45 78 70 6c 61 69 6e 3b 0a 20 20 70 2d 3e 6d 61  Explain;.  p->ma
1a718 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43  gic = VDBE_MAGIC
1a719 5f 52 55 4e 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e  _RUN;.  p->nChan
1a71a 67 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 63 61 63  ge = 0;.  p->cac
1a71b 68 65 43 74 72 20 3d 20 31 3b 0a 20 20 70 2d 3e  heCtr = 1;.  p->
1a71c 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d  minWriteFileForm
1a71d 61 74 20 3d 20 32 35 35 3b 0a 20 20 70 2d 3e 69  at = 255;.  p->i
1a71e 53 74 61 74 65 6d 65 6e 74 20 3d 20 30 3b 0a 23  Statement = 0;.#
1a71f 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49  ifdef VDBE_PROFI
1a720 4c 45 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 69  LE.  {.    int i
1a721 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
1a722 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20  <p->nOp; i++){. 
1a723 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63       p->aOp[i].c
1a724 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d  nt = 0;.      p-
1a725 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 20 3d  >aOp[i].cycles =
1a726 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65   0;.    }.  }.#e
1a727 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  ndif.}../*.** Cl
1a728 6f 73 65 20 61 20 56 44 42 45 20 63 75 72 73 6f  ose a VDBE curso
1a729 72 20 61 6e 64 20 72 65 6c 65 61 73 65 20 61 6c  r and release al
1a72a 6c 20 74 68 65 20 72 65 73 6f 75 72 63 65 73 20  l the resources 
1a72b 74 68 61 74 20 63 75 72 73 6f 72 20 0a 2a 2a 20  that cursor .** 
1a72c 68 61 70 70 65 6e 73 20 74 6f 20 68 6f 6c 64 2e  happens to hold.
1a72d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1a72e 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
1a72f 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 56 64  dbeFreeCursor(Vd
1a730 62 65 20 2a 70 2c 20 56 64 62 65 43 75 72 73 6f  be *p, VdbeCurso
1a731 72 20 2a 70 43 78 29 7b 0a 20 20 69 66 28 20 70  r *pCx){.  if( p
1a732 43 78 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  Cx==0 ){.    ret
1a733 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  urn;.  }.  if( p
1a734 43 78 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 73  Cx->pBt ){.    s
1a735 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65  qlite3BtreeClose
1a736 28 70 43 78 2d 3e 70 42 74 29 3b 0a 20 20 20 20  (pCx->pBt);.    
1a737 2f 2a 20 54 68 65 20 70 43 78 2d 3e 70 43 75 72  /* The pCx->pCur
1a738 73 6f 72 20 77 69 6c 6c 20 62 65 20 63 6c 6f 73  sor will be clos
1a739 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2c  e automatically,
1a73a 20 69 66 20 69 74 20 65 78 69 73 74 73 2c 20 62   if it exists, b
1a73b 79 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 61 6c  y.    ** the cal
1a73c 6c 20 61 62 6f 76 65 2e 20 2a 2f 0a 20 20 7d 65  l above. */.  }e
1a73d 6c 73 65 20 69 66 28 20 70 43 78 2d 3e 70 43 75  lse if( pCx->pCu
1a73e 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69  rsor ){.    sqli
1a73f 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72  te3BtreeCloseCur
1a740 73 6f 72 28 70 43 78 2d 3e 70 43 75 72 73 6f 72  sor(pCx->pCursor
1a741 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  );.  }.#ifndef S
1a742 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1a743 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 70 43  ALTABLE.  if( pC
1a744 78 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 29  x->pVtabCursor )
1a745 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74  {.    sqlite3_vt
1a746 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62  ab_cursor *pVtab
1a747 43 75 72 73 6f 72 20 3d 20 70 43 78 2d 3e 70 56  Cursor = pCx->pV
1a748 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 20 63  tabCursor;.    c
1a749 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
1a74a 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 70  ule *pModule = p
1a74b 43 78 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20  Cx->pModule;.   
1a74c 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64   p->inVtabMethod
1a74d 20 3d 20 31 3b 0a 20 20 20 20 28 76 6f 69 64 29   = 1;.    (void)
1a74e 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
1a74f 28 70 2d 3e 64 62 29 3b 0a 20 20 20 20 70 4d 6f  (p->db);.    pMo
1a750 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 56 74  dule->xClose(pVt
1a751 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 28  abCursor);.    (
1a752 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
1a753 74 79 4f 6e 28 70 2d 3e 64 62 29 3b 0a 20 20 20  tyOn(p->db);.   
1a754 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64   p->inVtabMethod
1a755 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66   = 0;.  }.#endif
1a756 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 74  .}../*.** Copy t
1a757 68 65 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64  he values stored
1a758 20 69 6e 20 74 68 65 20 56 64 62 65 46 72 61 6d   in the VdbeFram
1a759 65 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 69  e structure to i
1a75a 74 73 20 56 64 62 65 2e 20 54 68 69 73 0a 2a 2a  ts Vdbe. This.**
1a75b 20 69 73 20 75 73 65 64 2c 20 66 6f 72 20 65 78   is used, for ex
1a75c 61 6d 70 6c 65 2c 20 77 68 65 6e 20 61 20 74 72  ample, when a tr
1a75d 69 67 67 65 72 20 73 75 62 2d 70 72 6f 67 72 61  igger sub-progra
1a75e 6d 20 69 73 20 68 61 6c 74 65 64 20 74 6f 20 72  m is halted to r
1a75f 65 73 74 6f 72 65 0a 2a 2a 20 63 6f 6e 74 72 6f  estore.** contro
1a760 6c 20 74 6f 20 74 68 65 20 6d 61 69 6e 20 70 72  l to the main pr
1a761 6f 67 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ogram..*/.SQLITE
1a762 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1a763 69 74 65 33 56 64 62 65 46 72 61 6d 65 52 65 73  ite3VdbeFrameRes
1a764 74 6f 72 65 28 56 64 62 65 46 72 61 6d 65 20 2a  tore(VdbeFrame *
1a765 70 46 72 61 6d 65 29 7b 0a 20 20 56 64 62 65 20  pFrame){.  Vdbe 
1a766 2a 76 20 3d 20 70 46 72 61 6d 65 2d 3e 76 3b 0a  *v = pFrame->v;.
1a767 20 20 76 2d 3e 61 4f 70 20 3d 20 70 46 72 61 6d    v->aOp = pFram
1a768 65 2d 3e 61 4f 70 3b 0a 20 20 76 2d 3e 6e 4f 70  e->aOp;.  v->nOp
1a769 20 3d 20 70 46 72 61 6d 65 2d 3e 6e 4f 70 3b 0a   = pFrame->nOp;.
1a76a 20 20 76 2d 3e 61 4d 65 6d 20 3d 20 70 46 72 61    v->aMem = pFra
1a76b 6d 65 2d 3e 61 4d 65 6d 3b 0a 20 20 76 2d 3e 6e  me->aMem;.  v->n
1a76c 4d 65 6d 20 3d 20 70 46 72 61 6d 65 2d 3e 6e 4d  Mem = pFrame->nM
1a76d 65 6d 3b 0a 20 20 76 2d 3e 61 70 43 73 72 20 3d  em;.  v->apCsr =
1a76e 20 70 46 72 61 6d 65 2d 3e 61 70 43 73 72 3b 0a   pFrame->apCsr;.
1a76f 20 20 76 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 70    v->nCursor = p
1a770 46 72 61 6d 65 2d 3e 6e 43 75 72 73 6f 72 3b 0a  Frame->nCursor;.
1a771 20 20 76 2d 3e 64 62 2d 3e 6c 61 73 74 52 6f 77    v->db->lastRow
1a772 69 64 20 3d 20 70 46 72 61 6d 65 2d 3e 6c 61 73  id = pFrame->las
1a773 74 52 6f 77 69 64 3b 0a 20 20 76 2d 3e 6e 43 68  tRowid;.  v->nCh
1a774 61 6e 67 65 20 3d 20 70 46 72 61 6d 65 2d 3e 6e  ange = pFrame->n
1a775 43 68 61 6e 67 65 3b 0a 20 20 72 65 74 75 72 6e  Change;.  return
1a776 20 70 46 72 61 6d 65 2d 3e 70 63 3b 0a 7d 0a 0a   pFrame->pc;.}..
1a777 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6c 6c 20  /*.** Close all 
1a778 63 75 72 73 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 41  cursors..**.** A
1a779 6c 73 6f 20 72 65 6c 65 61 73 65 20 61 6e 79 20  lso release any 
1a77a 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 68  dynamic memory h
1a77b 65 6c 64 20 62 79 20 74 68 65 20 56 4d 20 69 6e  eld by the VM in
1a77c 20 74 68 65 20 56 64 62 65 2e 61 4d 65 6d 20 6d   the Vdbe.aMem m
1a77d 65 6d 6f 72 79 20 0a 2a 2a 20 63 65 6c 6c 20 61  emory .** cell a
1a77e 72 72 61 79 2e 20 54 68 69 73 20 69 73 20 6e 65  rray. This is ne
1a77f 63 65 73 73 61 72 79 20 61 73 20 74 68 65 20 6d  cessary as the m
1a780 65 6d 6f 72 79 20 63 65 6c 6c 20 61 72 72 61 79  emory cell array
1a781 20 6d 61 79 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20   may contain.** 
1a782 70 6f 69 6e 74 65 72 73 20 74 6f 20 56 64 62 65  pointers to Vdbe
1a783 46 72 61 6d 65 20 6f 62 6a 65 63 74 73 2c 20 77  Frame objects, w
1a784 68 69 63 68 20 6d 61 79 20 69 6e 20 74 75 72 6e  hich may in turn
1a785 20 63 6f 6e 74 61 69 6e 20 70 6f 69 6e 74 65 72   contain pointer
1a786 73 20 74 6f 0a 2a 2a 20 6f 70 65 6e 20 63 75 72  s to.** open cur
1a787 73 6f 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  sors..*/.static 
1a788 76 6f 69 64 20 63 6c 6f 73 65 41 6c 6c 43 75 72  void closeAllCur
1a789 73 6f 72 73 28 56 64 62 65 20 2a 70 29 7b 0a 20  sors(Vdbe *p){. 
1a78a 20 69 66 28 20 70 2d 3e 70 46 72 61 6d 65 20 29   if( p->pFrame )
1a78b 7b 0a 20 20 20 20 56 64 62 65 46 72 61 6d 65 20  {.    VdbeFrame 
1a78c 2a 70 46 72 61 6d 65 20 3d 20 70 2d 3e 70 46 72  *pFrame = p->pFr
1a78d 61 6d 65 3b 0a 20 20 20 20 66 6f 72 28 70 46 72  ame;.    for(pFr
1a78e 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65 3b 20 70  ame=p->pFrame; p
1a78f 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 3b 20  Frame->pParent; 
1a790 70 46 72 61 6d 65 3d 70 46 72 61 6d 65 2d 3e 70  pFrame=pFrame->p
1a791 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 73 71 6c  Parent);.    sql
1a792 69 74 65 33 56 64 62 65 46 72 61 6d 65 52 65 73  ite3VdbeFrameRes
1a793 74 6f 72 65 28 70 46 72 61 6d 65 29 3b 0a 20 20  tore(pFrame);.  
1a794 7d 0a 20 20 70 2d 3e 70 46 72 61 6d 65 20 3d 20  }.  p->pFrame = 
1a795 30 3b 0a 20 20 70 2d 3e 6e 46 72 61 6d 65 20 3d  0;.  p->nFrame =
1a796 20 30 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 61 70   0;..  if( p->ap
1a797 43 73 72 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  Csr ){.    int i
1a798 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
1a799 3c 70 2d 3e 6e 43 75 72 73 6f 72 3b 20 69 2b 2b  <p->nCursor; i++
1a79a 29 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72  ){.      VdbeCur
1a79b 73 6f 72 20 2a 70 43 20 3d 20 70 2d 3e 61 70 43  sor *pC = p->apC
1a79c 73 72 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28  sr[i];.      if(
1a79d 20 70 43 20 29 7b 0a 20 20 20 20 20 20 20 20 73   pC ){.        s
1a79e 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75  qlite3VdbeFreeCu
1a79f 72 73 6f 72 28 70 2c 20 70 43 29 3b 0a 20 20 20  rsor(p, pC);.   
1a7a0 20 20 20 20 20 70 2d 3e 61 70 43 73 72 5b 69 5d       p->apCsr[i]
1a7a1 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
1a7a2 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d    }.  }.  if( p-
1a7a3 3e 61 4d 65 6d 20 29 7b 0a 20 20 20 20 72 65 6c  >aMem ){.    rel
1a7a4 65 61 73 65 4d 65 6d 41 72 72 61 79 28 26 70 2d  easeMemArray(&p-
1a7a5 3e 61 4d 65 6d 5b 31 5d 2c 20 70 2d 3e 6e 4d 65  >aMem[1], p->nMe
1a7a6 6d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  m);.  }.}../*.**
1a7a7 20 43 6c 65 61 6e 20 75 70 20 74 68 65 20 56 4d   Clean up the VM
1a7a8 20 61 66 74 65 72 20 65 78 65 63 75 74 69 6f 6e   after execution
1a7a9 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
1a7aa 74 69 6e 65 20 77 69 6c 6c 20 61 75 74 6f 6d 61  tine will automa
1a7ab 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65 20 61 6e  tically close an
1a7ac 79 20 63 75 72 73 6f 72 73 2c 20 6c 69 73 74 73  y cursors, lists
1a7ad 2c 20 61 6e 64 2f 6f 72 0a 2a 2a 20 73 6f 72 74  , and/or.** sort
1a7ae 65 72 73 20 74 68 61 74 20 77 65 72 65 20 6c 65  ers that were le
1a7af 66 74 20 6f 70 65 6e 2e 20 20 49 74 20 61 6c 73  ft open.  It als
1a7b0 6f 20 64 65 6c 65 74 65 73 20 74 68 65 20 76 61  o deletes the va
1a7b1 6c 75 65 73 20 6f 66 0a 2a 2a 20 76 61 72 69 61  lues of.** varia
1a7b2 62 6c 65 73 20 69 6e 20 74 68 65 20 61 56 61 72  bles in the aVar
1a7b3 5b 5d 20 61 72 72 61 79 2e 0a 2a 2f 0a 73 74 61  [] array..*/.sta
1a7b4 74 69 63 20 76 6f 69 64 20 43 6c 65 61 6e 75 70  tic void Cleanup
1a7b5 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c  (Vdbe *p){.  sql
1a7b6 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62  ite3 *db = p->db
1a7b7 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
1a7b8 5f 44 45 42 55 47 0a 20 20 2f 2a 20 45 78 65 63  _DEBUG.  /* Exec
1a7b9 75 74 65 20 61 73 73 65 72 74 28 29 20 73 74 61  ute assert() sta
1a7ba 74 65 6d 65 6e 74 73 20 74 6f 20 65 6e 73 75 72  tements to ensur
1a7bb 65 20 74 68 61 74 20 74 68 65 20 56 64 62 65 2e  e that the Vdbe.
1a7bc 61 70 43 73 72 5b 5d 20 61 6e 64 20 0a 20 20 2a  apCsr[] and .  *
1a7bd 2a 20 56 64 62 65 2e 61 4d 65 6d 5b 5d 20 61 72  * Vdbe.aMem[] ar
1a7be 72 61 79 73 20 68 61 76 65 20 61 6c 72 65 61 64  rays have alread
1a7bf 79 20 62 65 65 6e 20 63 6c 65 61 6e 65 64 20 75  y been cleaned u
1a7c0 70 2e 20 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 0a  p.  */.  int i;.
1a7c1 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
1a7c2 6e 43 75 72 73 6f 72 3b 20 69 2b 2b 29 20 61 73  nCursor; i++) as
1a7c3 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 3d 3d  sert( p->apCsr==
1a7c4 30 20 7c 7c 20 70 2d 3e 61 70 43 73 72 5b 69 5d  0 || p->apCsr[i]
1a7c5 3d 3d 30 20 29 3b 0a 20 20 66 6f 72 28 69 3d 31  ==0 );.  for(i=1
1a7c6 3b 20 69 3c 3d 70 2d 3e 6e 4d 65 6d 3b 20 69 2b  ; i<=p->nMem; i+
1a7c7 2b 29 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4d  +) assert( p->aM
1a7c8 65 6d 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4d 65 6d  em==0 || p->aMem
1a7c9 5b 69 5d 2e 66 6c 61 67 73 3d 3d 4d 45 4d 5f 4e  [i].flags==MEM_N
1a7ca 75 6c 6c 20 29 3b 0a 23 65 6e 64 69 66 0a 0a 20  ull );.#endif.. 
1a7cb 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1a7cc 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a  b, p->zErrMsg);.
1a7cd 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30    p->zErrMsg = 0
1a7ce 3b 0a 20 20 70 2d 3e 70 52 65 73 75 6c 74 53 65  ;.  p->pResultSe
1a7cf 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  t = 0;.}../*.** 
1a7d0 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  Set the number o
1a7d1 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73  f result columns
1a7d2 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 72 65   that will be re
1a7d3 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 53  turned by this S
1a7d4 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e  QL.** statement.
1a7d5 20 54 68 69 73 20 69 73 20 6e 6f 77 20 73 65 74   This is now set
1a7d6 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65   at compile time
1a7d7 2c 20 72 61 74 68 65 72 20 74 68 61 6e 20 64 75  , rather than du
1a7d8 72 69 6e 67 0a 2a 2a 20 65 78 65 63 75 74 69 6f  ring.** executio
1a7d9 6e 20 6f 66 20 74 68 65 20 76 64 62 65 20 70 72  n of the vdbe pr
1a7da 6f 67 72 61 6d 20 73 6f 20 74 68 61 74 20 73 71  ogram so that sq
1a7db 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75  lite3_column_cou
1a7dc 6e 74 28 29 20 63 61 6e 0a 2a 2a 20 62 65 20 63  nt() can.** be c
1a7dd 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 53 51 4c 20  alled on an SQL 
1a7de 73 74 61 74 65 6d 65 6e 74 20 62 65 66 6f 72 65  statement before
1a7df 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e   sqlite3_step().
1a7e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1a7e1 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
1a7e2 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 56 64  dbeSetNumCols(Vd
1a7e3 62 65 20 2a 70 2c 20 69 6e 74 20 6e 52 65 73 43  be *p, int nResC
1a7e4 6f 6c 75 6d 6e 29 7b 0a 20 20 4d 65 6d 20 2a 70  olumn){.  Mem *p
1a7e5 43 6f 6c 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6e  ColName;.  int n
1a7e6 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
1a7e7 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 72 65 6c 65  = p->db;..  rele
1a7e8 61 73 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61  aseMemArray(p->a
1a7e9 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65 73  ColName, p->nRes
1a7ea 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e  Column*COLNAME_N
1a7eb 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
1a7ec 65 65 28 64 62 2c 20 70 2d 3e 61 43 6f 6c 4e 61  ee(db, p->aColNa
1a7ed 6d 65 29 3b 0a 20 20 6e 20 3d 20 6e 52 65 73 43  me);.  n = nResC
1a7ee 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 3b  olumn*COLNAME_N;
1a7ef 0a 20 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e  .  p->nResColumn
1a7f0 20 3d 20 28 75 31 36 29 6e 52 65 73 43 6f 6c 75   = (u16)nResColu
1a7f1 6d 6e 3b 0a 20 20 70 2d 3e 61 43 6f 6c 4e 61 6d  mn;.  p->aColNam
1a7f2 65 20 3d 20 70 43 6f 6c 4e 61 6d 65 20 3d 20 28  e = pColName = (
1a7f3 4d 65 6d 2a 29 73 71 6c 69 74 65 33 44 62 4d 61  Mem*)sqlite3DbMa
1a7f4 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
1a7f5 65 6f 66 28 4d 65 6d 29 2a 6e 20 29 3b 0a 20 20  eof(Mem)*n );.  
1a7f6 69 66 28 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 3d  if( p->aColName=
1a7f7 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 77  =0 ) return;.  w
1a7f8 68 69 6c 65 28 20 6e 2d 2d 20 3e 20 30 20 29 7b  hile( n-- > 0 ){
1a7f9 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2d 3e 66  .    pColName->f
1a7fa 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b  lags = MEM_Null;
1a7fb 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2d 3e 64  .    pColName->d
1a7fc 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 70  b = p->db;.    p
1a7fd 43 6f 6c 4e 61 6d 65 2b 2b 3b 0a 20 20 7d 0a 7d  ColName++;.  }.}
1a7fe 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
1a7ff 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 64 78 27  name of the idx'
1a800 74 68 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20  th column to be 
1a801 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20  returned by the 
1a802 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  SQL statement..*
1a803 2a 20 7a 4e 61 6d 65 20 6d 75 73 74 20 62 65 20  * zName must be 
1a804 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 6e  a pointer to a n
1a805 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 20 73 74  ul terminated st
1a806 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ring..**.** This
1a807 20 63 61 6c 6c 20 6d 75 73 74 20 62 65 20 6d 61   call must be ma
1a808 64 65 20 61 66 74 65 72 20 61 20 63 61 6c 6c 20  de after a call 
1a809 74 6f 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  to sqlite3VdbeSe
1a80a 74 4e 75 6d 43 6f 6c 73 28 29 2e 0a 2a 2a 0a 2a  tNumCols()..**.*
1a80b 2a 20 54 68 65 20 66 69 6e 61 6c 20 70 61 72 61  * The final para
1a80c 6d 65 74 65 72 2c 20 78 44 65 6c 2c 20 6d 75 73  meter, xDel, mus
1a80d 74 20 62 65 20 6f 6e 65 20 6f 66 20 53 51 4c 49  t be one of SQLI
1a80e 54 45 5f 44 59 4e 41 4d 49 43 2c 20 53 51 4c 49  TE_DYNAMIC, SQLI
1a80f 54 45 5f 53 54 41 54 49 43 0a 2a 2a 20 6f 72 20  TE_STATIC.** or 
1a810 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
1a811 2e 20 49 66 20 69 74 20 69 73 20 53 51 4c 49 54  . If it is SQLIT
1a812 45 5f 44 59 4e 41 4d 49 43 2c 20 74 68 65 6e 20  E_DYNAMIC, then 
1a813 74 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74  the buffer point
1a814 65 64 0a 2a 2a 20 74 6f 20 62 79 20 7a 4e 61 6d  ed.** to by zNam
1a815 65 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20  e will be freed 
1a816 62 79 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  by sqlite3DbFree
1a817 28 29 20 77 68 65 6e 20 74 68 65 20 76 64 62 65  () when the vdbe
1a818 20 69 73 20 64 65 73 74 72 6f 79 65 64 2e 0a 2a   is destroyed..*
1a819 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1a81a 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
1a81b 53 65 74 43 6f 6c 4e 61 6d 65 28 0a 20 20 56 64  SetColName(.  Vd
1a81c 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  be *p,          
1a81d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1a81e 2a 20 56 64 62 65 20 62 65 69 6e 67 20 63 6f 6e  * Vdbe being con
1a81f 66 69 67 75 72 65 64 20 2a 2f 0a 20 20 69 6e 74  figured */.  int
1a820 20 69 64 78 2c 20 20 20 20 20 20 20 20 20 20 20   idx,           
1a821 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a822 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e   Index of column
1a823 20 7a 4e 61 6d 65 20 61 70 70 6c 69 65 73 20 74   zName applies t
1a824 6f 20 2a 2f 0a 20 20 69 6e 74 20 76 61 72 2c 20  o */.  int var, 
1a825 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a826 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
1a827 66 20 74 68 65 20 43 4f 4c 4e 41 4d 45 5f 2a 20  f the COLNAME_* 
1a828 63 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a 20 20 63  constants */.  c
1a829 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
1a82a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1a82b 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75  /* Pointer to bu
1a82c 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ffer containing 
1a82d 6e 61 6d 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28  name */.  void (
1a82e 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 20 20  *xDel)(void*)   
1a82f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65             /* Me
1a830 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 20  mory management 
1a831 73 74 72 61 74 65 67 79 20 66 6f 72 20 7a 4e 61  strategy for zNa
1a832 6d 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  me */.){.  int r
1a833 63 3b 0a 20 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61  c;.  Mem *pColNa
1a834 6d 65 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64  me;.  assert( id
1a835 78 3c 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20  x<p->nResColumn 
1a836 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76 61 72  );.  assert( var
1a837 3c 43 4f 4c 4e 41 4d 45 5f 4e 20 29 3b 0a 20 20  <COLNAME_N );.  
1a838 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  if( p->db->mallo
1a839 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 61  cFailed ){.    a
1a83a 73 73 65 72 74 28 20 21 7a 4e 61 6d 65 20 7c 7c  ssert( !zName ||
1a83b 20 78 44 65 6c 21 3d 53 51 4c 49 54 45 5f 44 59   xDel!=SQLITE_DY
1a83c 4e 41 4d 49 43 20 29 3b 0a 20 20 20 20 72 65 74  NAMIC );.    ret
1a83d 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
1a83e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
1a83f 70 2d 3e 61 43 6f 6c 4e 61 6d 65 21 3d 30 20 29  p->aColName!=0 )
1a840 3b 0a 20 20 70 43 6f 6c 4e 61 6d 65 20 3d 20 26  ;.  pColName = &
1a841 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b 69 64 78  (p->aColName[idx
1a842 2b 76 61 72 2a 70 2d 3e 6e 52 65 73 43 6f 6c 75  +var*p->nResColu
1a843 6d 6e 5d 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  mn]);.  rc = sql
1a844 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
1a845 72 28 70 43 6f 6c 4e 61 6d 65 2c 20 7a 4e 61 6d  r(pColName, zNam
1a846 65 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54  e, -1, SQLITE_UT
1a847 46 38 2c 20 78 44 65 6c 29 3b 0a 20 20 61 73 73  F8, xDel);.  ass
1a848 65 72 74 28 20 72 63 21 3d 30 20 7c 7c 20 21 7a  ert( rc!=0 || !z
1a849 4e 61 6d 65 20 7c 7c 20 28 70 43 6f 6c 4e 61 6d  Name || (pColNam
1a84a 65 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 54 65 72  e->flags&MEM_Ter
1a84b 6d 29 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72  m)!=0 );.  retur
1a84c 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  n rc;.}../*.** A
1a84d 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74   read or write t
1a84e 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 6f  ransaction may o
1a84f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 63 74  r may not be act
1a850 69 76 65 20 6f 6e 20 64 61 74 61 62 61 73 65 20  ive on database 
1a851 68 61 6e 64 6c 65 0a 2a 2a 20 64 62 2e 20 49 66  handle.** db. If
1a852 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
1a853 73 20 61 63 74 69 76 65 2c 20 63 6f 6d 6d 69 74  s active, commit
1a854 20 69 74 2e 20 49 66 20 74 68 65 72 65 20 69 73   it. If there is
1a855 20 61 0a 2a 2a 20 77 72 69 74 65 2d 74 72 61 6e   a.** write-tran
1a856 73 61 63 74 69 6f 6e 20 73 70 61 6e 6e 69 6e 67  saction spanning
1a857 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64   more than one d
1a858 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68  atabase file, th
1a859 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 74 61  is routine.** ta
1a85a 6b 65 73 20 63 61 72 65 20 6f 66 20 74 68 65 20  kes care of the 
1a85b 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 74  master journal t
1a85c 72 69 63 6b 65 72 79 2e 0a 2a 2f 0a 73 74 61 74  rickery..*/.stat
1a85d 69 63 20 69 6e 74 20 76 64 62 65 43 6f 6d 6d 69  ic int vdbeCommi
1a85e 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 56  t(sqlite3 *db, V
1a85f 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69  dbe *p){.  int i
1a860 3b 0a 20 20 69 6e 74 20 6e 54 72 61 6e 73 20 3d  ;.  int nTrans =
1a861 20 30 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f   0;  /* Number o
1a862 66 20 64 61 74 61 62 61 73 65 73 20 77 69 74 68  f databases with
1a863 20 61 6e 20 61 63 74 69 76 65 20 77 72 69 74 65   an active write
1a864 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a  -transaction */.
1a865 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1a866 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 65 65 64  E_OK;.  int need
1a867 58 63 6f 6d 6d 69 74 20 3d 20 30 3b 0a 0a 23 69  Xcommit = 0;..#i
1a868 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
1a869 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
1a86a 2f 2a 20 57 69 74 68 20 74 68 69 73 20 6f 70 74  /* With this opt
1a86b 69 6f 6e 2c 20 73 71 6c 69 74 65 33 56 74 61 62  ion, sqlite3Vtab
1a86c 53 79 6e 63 28 29 20 69 73 20 64 65 66 69 6e 65  Sync() is define
1a86d 64 20 74 6f 20 62 65 20 73 69 6d 70 6c 79 20 0a  d to be simply .
1a86e 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 73    ** SQLITE_OK s
1a86f 6f 20 70 20 69 73 20 6e 6f 74 20 75 73 65 64 2e  o p is not used.
1a870 20 0a 20 20 2a 2f 0a 20 20 55 4e 55 53 45 44 5f   .  */.  UNUSED_
1a871 50 41 52 41 4d 45 54 45 52 28 70 29 3b 0a 23 65  PARAMETER(p);.#e
1a872 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65 66 6f 72  ndif..  /* Befor
1a873 65 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67  e doing anything
1a874 20 65 6c 73 65 2c 20 63 61 6c 6c 20 74 68 65 20   else, call the 
1a875 78 53 79 6e 63 28 29 20 63 61 6c 6c 62 61 63 6b  xSync() callback
1a876 20 66 6f 72 20 61 6e 79 0a 20 20 2a 2a 20 76 69   for any.  ** vi
1a877 72 74 75 61 6c 20 6d 6f 64 75 6c 65 20 74 61 62  rtual module tab
1a878 6c 65 73 20 77 72 69 74 74 65 6e 20 69 6e 20 74  les written in t
1a879 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  his transaction.
1a87a 20 54 68 69 73 20 68 61 73 20 74 6f 0a 20 20 2a   This has to.  *
1a87b 2a 20 62 65 20 64 6f 6e 65 20 62 65 66 6f 72 65  * be done before
1a87c 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 77 68 65   determining whe
1a87d 74 68 65 72 20 61 20 6d 61 73 74 65 72 20 6a 6f  ther a master jo
1a87e 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 0a 20  urnal file is . 
1a87f 20 2a 2a 20 72 65 71 75 69 72 65 64 2c 20 61 73   ** required, as
1a880 20 61 6e 20 78 53 79 6e 63 28 29 20 63 61 6c 6c   an xSync() call
1a881 62 61 63 6b 20 6d 61 79 20 61 64 64 20 61 6e 20  back may add an 
1a882 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
1a883 65 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 74 72  e.  ** to the tr
1a884 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a  ansaction..  */.
1a885 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74    rc = sqlite3Vt
1a886 61 62 53 79 6e 63 28 64 62 2c 20 26 70 2d 3e 7a  abSync(db, &p->z
1a887 45 72 72 4d 73 67 29 3b 0a 20 20 69 66 28 20 72  ErrMsg);.  if( r
1a888 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1a889 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1a88a 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f   }..  /* This lo
1a88b 6f 70 20 64 65 74 65 72 6d 69 6e 65 73 20 28 61  op determines (a
1a88c 29 20 69 66 20 74 68 65 20 63 6f 6d 6d 69 74 20  ) if the commit 
1a88d 68 6f 6f 6b 20 73 68 6f 75 6c 64 20 62 65 20 69  hook should be i
1a88e 6e 76 6f 6b 65 64 20 61 6e 64 0a 20 20 2a 2a 20  nvoked and.  ** 
1a88f 28 62 29 20 68 6f 77 20 6d 61 6e 79 20 64 61 74  (b) how many dat
1a890 61 62 61 73 65 20 66 69 6c 65 73 20 68 61 76 65  abase files have
1a891 20 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e   open write tran
1a892 73 61 63 74 69 6f 6e 73 2c 20 6e 6f 74 20 0a 20  sactions, not . 
1a893 20 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 74 68   ** including th
1a894 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 2e  e temp database.
1a895 20 28 62 29 20 69 73 20 69 6d 70 6f 72 74 61 6e   (b) is importan
1a896 74 20 62 65 63 61 75 73 65 20 69 66 20 6d 6f 72  t because if mor
1a897 65 20 74 68 61 6e 20 0a 20 20 2a 2a 20 6f 6e 65  e than .  ** one
1a898 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68   database file h
1a899 61 73 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 65  as an open write
1a89a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 20   transaction, a 
1a89b 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 20  master journal. 
1a89c 20 2a 2a 20 66 69 6c 65 20 69 73 20 72 65 71 75   ** file is requ
1a89d 69 72 65 64 20 66 6f 72 20 61 6e 20 61 74 6f 6d  ired for an atom
1a89e 69 63 20 63 6f 6d 6d 69 74 2e 0a 20 20 2a 2f 20  ic commit..  */ 
1a89f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62  .  for(i=0; i<db
1a8a0 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20  ->nDb; i++){ .  
1a8a1 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64    Btree *pBt = d
1a8a2 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20  b->aDb[i].pBt;. 
1a8a3 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74     if( sqlite3Bt
1a8a4 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 42 74  reeIsInTrans(pBt
1a8a5 29 20 29 7b 0a 20 20 20 20 20 20 6e 65 65 64 58  ) ){.      needX
1a8a6 63 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20  commit = 1;.    
1a8a7 20 20 69 66 28 20 69 21 3d 31 20 29 20 6e 54 72    if( i!=1 ) nTr
1a8a8 61 6e 73 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  ans++;.    }.  }
1a8a9 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
1a8aa 61 72 65 20 61 6e 79 20 77 72 69 74 65 2d 74 72  are any write-tr
1a8ab 61 6e 73 61 63 74 69 6f 6e 73 20 61 74 20 61 6c  ansactions at al
1a8ac 6c 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 6f  l, invoke the co
1a8ad 6d 6d 69 74 20 68 6f 6f 6b 20 2a 2f 0a 20 20 69  mmit hook */.  i
1a8ae 66 28 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 26  f( needXcommit &
1a8af 26 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c  & db->xCommitCal
1a8b0 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 28 76 6f  lback ){.    (vo
1a8b1 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
1a8b2 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 72 63 20  Off(db);.    rc 
1a8b3 3d 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c  = db->xCommitCal
1a8b4 6c 62 61 63 6b 28 64 62 2d 3e 70 43 6f 6d 6d 69  lback(db->pCommi
1a8b5 74 41 72 67 29 3b 0a 20 20 20 20 28 76 6f 69 64  tArg);.    (void
1a8b6 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e  )sqlite3SafetyOn
1a8b7 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 72 63  (db);.    if( rc
1a8b8 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1a8b9 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49   SQLITE_CONSTRAI
1a8ba 4e 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  NT;.    }.  }.. 
1a8bb 20 2f 2a 20 54 68 65 20 73 69 6d 70 6c 65 20 63   /* The simple c
1a8bc 61 73 65 20 2d 20 6e 6f 20 6d 6f 72 65 20 74 68  ase - no more th
1a8bd 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  an one database 
1a8be 66 69 6c 65 20 28 6e 6f 74 20 63 6f 75 6e 74 69  file (not counti
1a8bf 6e 67 20 74 68 65 0a 20 20 2a 2a 20 54 45 4d 50  ng the.  ** TEMP
1a8c0 20 64 61 74 61 62 61 73 65 29 20 68 61 73 20 61   database) has a
1a8c1 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 74   transaction act
1a8c2 69 76 65 2e 20 20 20 54 68 65 72 65 20 69 73 20  ive.   There is 
1a8c3 6e 6f 20 6e 65 65 64 20 66 6f 72 20 74 68 65 0a  no need for the.
1a8c4 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75 72    ** master-jour
1a8c5 6e 61 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  nal..  **.  ** I
1a8c6 66 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  f the return val
1a8c7 75 65 20 6f 66 20 73 71 6c 69 74 65 33 42 74 72  ue of sqlite3Btr
1a8c8 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 29 20  eeGetFilename() 
1a8c9 69 73 20 61 20 7a 65 72 6f 20 6c 65 6e 67 74 68  is a zero length
1a8ca 0a 20 20 2a 2a 20 73 74 72 69 6e 67 2c 20 69 74  .  ** string, it
1a8cb 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e 20   means the main 
1a8cc 64 61 74 61 62 61 73 65 20 69 73 20 3a 6d 65 6d  database is :mem
1a8cd 6f 72 79 3a 20 6f 72 20 61 20 74 65 6d 70 20 66  ory: or a temp f
1a8ce 69 6c 65 2e 20 20 49 6e 20 0a 20 20 2a 2a 20 74  ile.  In .  ** t
1a8cf 68 61 74 20 63 61 73 65 20 77 65 20 64 6f 20 6e  hat case we do n
1a8d0 6f 74 20 73 75 70 70 6f 72 74 20 61 74 6f 6d 69  ot support atomi
1a8d1 63 20 6d 75 6c 74 69 2d 66 69 6c 65 20 63 6f 6d  c multi-file com
1a8d2 6d 69 74 73 2c 20 73 6f 20 75 73 65 20 74 68 65  mits, so use the
1a8d3 20 0a 20 20 2a 2a 20 73 69 6d 70 6c 65 20 63 61   .  ** simple ca
1a8d4 73 65 20 74 68 65 6e 20 74 6f 6f 2e 0a 20 20 2a  se then too..  *
1a8d5 2f 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74  /.  if( 0==sqlit
1a8d6 65 33 53 74 72 6c 65 6e 33 30 28 73 71 6c 69 74  e3Strlen30(sqlit
1a8d7 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61  e3BtreeGetFilena
1a8d8 6d 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42  me(db->aDb[0].pB
1a8d9 74 29 29 0a 20 20 20 7c 7c 20 6e 54 72 61 6e 73  t)).   || nTrans
1a8da 3c 3d 31 0a 20 20 29 7b 0a 20 20 20 20 66 6f 72  <=1.  ){.    for
1a8db 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45  (i=0; rc==SQLITE
1a8dc 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62  _OK && i<db->nDb
1a8dd 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74  ; i++){.      Bt
1a8de 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61  ree *pBt = db->a
1a8df 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20  Db[i].pBt;.     
1a8e0 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20   if( pBt ){.    
1a8e1 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1a8e2 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
1a8e3 4f 6e 65 28 70 42 74 2c 20 30 29 3b 0a 20 20 20  One(pBt, 0);.   
1a8e4 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
1a8e5 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d 6d 69 74  /* Do the commit
1a8e6 20 6f 6e 6c 79 20 69 66 20 61 6c 6c 20 64 61 74   only if all dat
1a8e7 61 62 61 73 65 73 20 73 75 63 63 65 73 73 66 75  abases successfu
1a8e8 6c 6c 79 20 63 6f 6d 70 6c 65 74 65 20 70 68 61  lly complete pha
1a8e9 73 65 20 31 2e 20 0a 20 20 20 20 2a 2a 20 49 66  se 1. .    ** If
1a8ea 20 6f 6e 65 20 6f 66 20 74 68 65 20 42 74 72 65   one of the Btre
1a8eb 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28  eCommitPhaseOne(
1a8ec 29 20 63 61 6c 6c 73 20 66 61 69 6c 73 2c 20 74  ) calls fails, t
1a8ed 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 61 6e  his indicates an
1a8ee 0a 20 20 20 20 2a 2a 20 49 4f 20 65 72 72 6f 72  .    ** IO error
1a8ef 20 77 68 69 6c 65 20 64 65 6c 65 74 69 6e 67 20   while deleting 
1a8f0 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 61 20  or truncating a 
1a8f1 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 74  journal file. It
1a8f2 20 69 73 20 75 6e 6c 69 6b 65 6c 79 2c 0a 20 20   is unlikely,.  
1a8f3 20 20 2a 2a 20 62 75 74 20 63 6f 75 6c 64 20 68    ** but could h
1a8f4 61 70 70 65 6e 2e 20 49 6e 20 74 68 69 73 20 63  appen. In this c
1a8f5 61 73 65 20 61 62 61 6e 64 6f 6e 20 70 72 6f 63  ase abandon proc
1a8f6 65 73 73 69 6e 67 20 61 6e 64 20 72 65 74 75 72  essing and retur
1a8f7 6e 20 74 68 65 20 65 72 72 6f 72 2e 0a 20 20 20  n the error..   
1a8f8 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   */.    for(i=0;
1a8f9 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
1a8fa 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  & i<db->nDb; i++
1a8fb 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a  ){.      Btree *
1a8fc 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d  pBt = db->aDb[i]
1a8fd 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20  .pBt;.      if( 
1a8fe 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72  pBt ){.        r
1a8ff 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1a900 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70  CommitPhaseTwo(p
1a901 42 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  Bt);.      }.   
1a902 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53   }.    if( rc==S
1a903 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1a904 20 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f 6d    sqlite3VtabCom
1a905 6d 69 74 28 64 62 29 3b 0a 20 20 20 20 7d 0a 20  mit(db);.    }. 
1a906 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 63 6f 6d   }..  /* The com
1a907 70 6c 65 78 20 63 61 73 65 20 2d 20 54 68 65 72  plex case - Ther
1a908 65 20 69 73 20 61 20 6d 75 6c 74 69 2d 66 69 6c  e is a multi-fil
1a909 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74  e write-transact
1a90a 69 6f 6e 20 61 63 74 69 76 65 2e 0a 20 20 2a 2a  ion active..  **
1a90b 20 54 68 69 73 20 72 65 71 75 69 72 65 73 20 61   This requires a
1a90c 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1a90d 66 69 6c 65 20 74 6f 20 65 6e 73 75 72 65 20 74  file to ensure t
1a90e 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  he transaction i
1a90f 73 0a 20 20 2a 2a 20 63 6f 6d 6d 69 74 74 65 64  s.  ** committed
1a910 20 61 74 6f 6d 69 63 6c 79 2e 0a 20 20 2a 2f 0a   atomicly..  */.
1a911 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1a912 4d 49 54 5f 44 49 53 4b 49 4f 0a 20 20 65 6c 73  MIT_DISKIO.  els
1a913 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  e{.    sqlite3_v
1a914 66 73 20 2a 70 56 66 73 20 3d 20 64 62 2d 3e 70  fs *pVfs = db->p
1a915 56 66 73 3b 0a 20 20 20 20 69 6e 74 20 6e 65 65  Vfs;.    int nee
1a916 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 20 20 63  dSync = 0;.    c
1a917 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20 30  har *zMaster = 0
1a918 3b 20 20 20 2f 2a 20 46 69 6c 65 2d 6e 61 6d 65  ;   /* File-name
1a919 20 66 6f 72 20 74 68 65 20 6d 61 73 74 65 72 20   for the master 
1a91a 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 63  journal */.    c
1a91b 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61 69 6e  har const *zMain
1a91c 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 42 74  File = sqlite3Bt
1a91d 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 64  reeGetFilename(d
1a91e 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29 3b 0a  b->aDb[0].pBt);.
1a91f 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65      sqlite3_file
1a920 20 2a 70 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20   *pMaster = 0;. 
1a921 20 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20     i64 offset = 
1a922 30 3b 0a 20 20 20 20 69 6e 74 20 72 65 73 3b 0a  0;.    int res;.
1a923 0a 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 61  .    /* Select a
1a924 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1a925 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20  file name */.   
1a926 20 64 6f 20 7b 0a 20 20 20 20 20 20 75 33 32 20   do {.      u32 
1a927 69 52 61 6e 64 6f 6d 3b 0a 20 20 20 20 20 20 73  iRandom;.      s
1a928 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1a929 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20   zMaster);.     
1a92a 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
1a92b 65 73 73 28 73 69 7a 65 6f 66 28 69 52 61 6e 64  ess(sizeof(iRand
1a92c 6f 6d 29 2c 20 26 69 52 61 6e 64 6f 6d 29 3b 0a  om), &iRandom);.
1a92d 20 20 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20        zMaster = 
1a92e 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
1a92f 62 2c 20 22 25 73 2d 6d 6a 25 30 38 58 22 2c 20  b, "%s-mj%08X", 
1a930 7a 4d 61 69 6e 46 69 6c 65 2c 20 69 52 61 6e 64  zMainFile, iRand
1a931 6f 6d 26 30 78 37 66 66 66 66 66 66 66 29 3b 0a  om&0x7fffffff);.
1a932 20 20 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74        if( !zMast
1a933 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  er ){.        re
1a934 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
1a935 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  M;.      }.     
1a936 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41   rc = sqlite3OsA
1a937 63 63 65 73 73 28 70 56 66 73 2c 20 7a 4d 61 73  ccess(pVfs, zMas
1a938 74 65 72 2c 20 53 51 4c 49 54 45 5f 41 43 43 45  ter, SQLITE_ACCE
1a939 53 53 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29  SS_EXISTS, &res)
1a93a 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 72 63  ;.    }while( rc
1a93b 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72  ==SQLITE_OK && r
1a93c 65 73 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63  es );.    if( rc
1a93d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1a93e 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65       /* Open the
1a93f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e   master journal.
1a940 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73   */.      rc = s
1a941 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c  qlite3OsOpenMall
1a942 6f 63 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72  oc(pVfs, zMaster
1a943 2c 20 26 70 4d 61 73 74 65 72 2c 20 0a 20 20 20  , &pMaster, .   
1a944 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
1a945 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c  EN_READWRITE|SQL
1a946 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c  ITE_OPEN_CREATE|
1a947 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54  .          SQLIT
1a948 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45  E_OPEN_EXCLUSIVE
1a949 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53  |SQLITE_OPEN_MAS
1a94a 54 45 52 5f 4a 4f 55 52 4e 41 4c 2c 20 30 0a 20  TER_JOURNAL, 0. 
1a94b 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20       );.    }.  
1a94c 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1a94d 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
1a94e 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
1a94f 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 72  Master);.      r
1a950 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
1a951 20 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74   .    /* Write t
1a952 68 65 20 6e 61 6d 65 20 6f 66 20 65 61 63 68 20  he name of each 
1a953 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e  database file in
1a954 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
1a955 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 0a 20 20   into the new.  
1a956 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72    ** master jour
1a957 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 61 6e 20  nal file. If an 
1a958 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 74 20  error occurs at 
1a959 74 68 69 73 20 70 6f 69 6e 74 20 63 6c 6f 73 65  this point close
1a95a 0a 20 20 20 20 2a 2a 20 61 6e 64 20 64 65 6c 65  .    ** and dele
1a95b 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  te the master jo
1a95c 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 6c 6c 20  urnal file. All 
1a95d 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20 6a  the individual j
1a95e 6f 75 72 6e 61 6c 20 66 69 6c 65 73 0a 20 20 20  ournal files.   
1a95f 20 2a 2a 20 73 74 69 6c 6c 20 68 61 76 65 20 27   ** still have '
1a960 6e 75 6c 6c 27 20 61 73 20 74 68 65 20 6d 61 73  null' as the mas
1a961 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e  ter journal poin
1a962 74 65 72 2c 20 73 6f 20 74 68 65 79 20 77 69 6c  ter, so they wil
1a963 6c 20 72 6f 6c 6c 0a 20 20 20 20 2a 2a 20 62 61  l roll.    ** ba
1a964 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79  ck independently
1a965 20 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63   if a failure oc
1a966 63 75 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  curs..    */.   
1a967 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
1a968 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  nDb; i++){.     
1a969 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62   Btree *pBt = db
1a96a 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20  ->aDb[i].pBt;.  
1a96b 20 20 20 20 69 66 28 20 69 3d 3d 31 20 29 20 63      if( i==1 ) c
1a96c 6f 6e 74 69 6e 75 65 3b 20 20 20 2f 2a 20 49 67  ontinue;   /* Ig
1a96d 6e 6f 72 65 20 74 68 65 20 54 45 4d 50 20 64 61  nore the TEMP da
1a96e 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20  tabase */.      
1a96f 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  if( sqlite3Btree
1a970 49 73 49 6e 54 72 61 6e 73 28 70 42 74 29 20 29  IsInTrans(pBt) )
1a971 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 63  {.        char c
1a972 6f 6e 73 74 20 2a 7a 46 69 6c 65 20 3d 20 73 71  onst *zFile = sq
1a973 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75  lite3BtreeGetJou
1a974 72 6e 61 6c 6e 61 6d 65 28 70 42 74 29 3b 0a 20  rnalname(pBt);. 
1a975 20 20 20 20 20 20 20 69 66 28 20 7a 46 69 6c 65         if( zFile
1a976 5b 30 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75  [0]==0 ) continu
1a977 65 3b 20 20 2f 2a 20 49 67 6e 6f 72 65 20 3a 6d  e;  /* Ignore :m
1a978 65 6d 6f 72 79 3a 20 64 61 74 61 62 61 73 65 73  emory: databases
1a979 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
1a97a 21 6e 65 65 64 53 79 6e 63 20 26 26 20 21 73 71  !needSync && !sq
1a97b 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 44 69  lite3BtreeSyncDi
1a97c 73 61 62 6c 65 64 28 70 42 74 29 20 29 7b 0a 20  sabled(pBt) ){. 
1a97d 20 20 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e           needSyn
1a97e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  c = 1;.        }
1a97f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
1a980 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 4d 61  lite3OsWrite(pMa
1a981 73 74 65 72 2c 20 7a 46 69 6c 65 2c 20 73 71 6c  ster, zFile, sql
1a982 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 46 69  ite3Strlen30(zFi
1a983 6c 65 29 2b 31 2c 20 6f 66 66 73 65 74 29 3b 0a  le)+1, offset);.
1a984 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 20 2b          offset +
1a985 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
1a986 30 28 7a 46 69 6c 65 29 2b 31 3b 0a 20 20 20 20  0(zFile)+1;.    
1a987 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1a988 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1a989 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73     sqlite3OsClos
1a98a 65 46 72 65 65 28 70 4d 61 73 74 65 72 29 3b 0a  eFree(pMaster);.
1a98b 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1a98c 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20  3OsDelete(pVfs, 
1a98d 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20 20  zMaster, 0);.   
1a98e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
1a98f 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72  Free(db, zMaster
1a990 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  );.          ret
1a991 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20  urn rc;.        
1a992 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
1a993 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65  .    /* Sync the
1a994 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1a995 66 69 6c 65 2e 20 49 66 20 74 68 65 20 49 4f 43  file. If the IOC
1a996 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 64 65  AP_SEQUENTIAL de
1a997 76 69 63 65 0a 20 20 20 20 2a 2a 20 66 6c 61 67  vice.    ** flag
1a998 20 69 73 20 73 65 74 20 74 68 69 73 20 69 73 20   is set this is 
1a999 6e 6f 74 20 72 65 71 75 69 72 65 64 2e 0a 20 20  not required..  
1a99a 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 65 65    */.    if( nee
1a99b 64 53 79 6e 63 20 0a 20 20 20 20 20 26 26 20 30  dSync .     && 0
1a99c 3d 3d 28 73 71 6c 69 74 65 33 4f 73 44 65 76 69  ==(sqlite3OsDevi
1a99d 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
1a99e 73 28 70 4d 61 73 74 65 72 29 26 53 51 4c 49 54  s(pMaster)&SQLIT
1a99f 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49  E_IOCAP_SEQUENTI
1a9a0 41 4c 29 0a 20 20 20 20 20 26 26 20 53 51 4c 49  AL).     && SQLI
1a9a1 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c  TE_OK!=(rc = sql
1a9a2 69 74 65 33 4f 73 53 79 6e 63 28 70 4d 61 73 74  ite3OsSync(pMast
1a9a3 65 72 2c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f  er, SQLITE_SYNC_
1a9a4 4e 4f 52 4d 41 4c 29 29 0a 20 20 20 20 29 7b 0a  NORMAL)).    ){.
1a9a5 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43        sqlite3OsC
1a9a6 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65 72  loseFree(pMaster
1a9a7 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1a9a8 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a  OsDelete(pVfs, z
1a9a9 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 20  Master, 0);.    
1a9aa 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1a9ab 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20  db, zMaster);.  
1a9ac 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1a9ad 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 79 6e     }..    /* Syn
1a9ae 63 20 61 6c 6c 20 74 68 65 20 64 62 20 66 69 6c  c all the db fil
1a9af 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 74  es involved in t
1a9b0 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  he transaction. 
1a9b1 54 68 65 20 73 61 6d 65 20 63 61 6c 6c 0a 20 20  The same call.  
1a9b2 20 20 2a 2a 20 73 65 74 73 20 74 68 65 20 6d 61    ** sets the ma
1a9b3 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69  ster journal poi
1a9b4 6e 74 65 72 20 69 6e 20 65 61 63 68 20 69 6e 64  nter in each ind
1a9b5 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 2e  ividual journal.
1a9b6 20 49 66 0a 20 20 20 20 2a 2a 20 61 6e 20 65 72   If.    ** an er
1a9b7 72 6f 72 20 6f 63 63 75 72 73 20 68 65 72 65 2c  ror occurs here,
1a9b8 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74   do not delete t
1a9b9 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
1a9ba 6c 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20  l file..    **. 
1a9bb 20 20 20 2a 2a 20 49 66 20 74 68 65 20 65 72 72     ** If the err
1a9bc 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67  or occurs during
1a9bd 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20   the first call 
1a9be 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  to.    ** sqlite
1a9bf 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73  3BtreeCommitPhas
1a9c0 65 4f 6e 65 28 29 2c 20 74 68 65 6e 20 74 68 65  eOne(), then the
1a9c1 72 65 20 69 73 20 61 20 63 68 61 6e 63 65 20 74  re is a chance t
1a9c2 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 6d  hat the.    ** m
1a9c3 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
1a9c4 6c 65 20 77 69 6c 6c 20 62 65 20 6f 72 70 68 61  le will be orpha
1a9c5 6e 65 64 2e 20 42 75 74 20 77 65 20 63 61 6e 6e  ned. But we cann
1a9c6 6f 74 20 64 65 6c 65 74 65 20 69 74 2c 0a 20 20  ot delete it,.  
1a9c7 20 20 2a 2a 20 69 6e 20 63 61 73 65 20 74 68 65    ** in case the
1a9c8 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1a9c9 66 69 6c 65 20 6e 61 6d 65 20 77 61 73 20 77 72  file name was wr
1a9ca 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a  itten into the j
1a9cb 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69  ournal.    ** fi
1a9cc 6c 65 20 62 65 66 6f 72 65 20 74 68 65 20 66 61  le before the fa
1a9cd 69 6c 75 72 65 20 6f 63 63 75 72 72 65 64 2e 0a  ilure occurred..
1a9ce 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69      */.    for(i
1a9cf 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
1a9d0 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  K && i<db->nDb; 
1a9d1 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 42 74 72  i++){ .      Btr
1a9d2 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44  ee *pBt = db->aD
1a9d3 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20  b[i].pBt;.      
1a9d4 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20  if( pBt ){.     
1a9d5 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1a9d6 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f  treeCommitPhaseO
1a9d7 6e 65 28 70 42 74 2c 20 7a 4d 61 73 74 65 72 29  ne(pBt, zMaster)
1a9d8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1a9d9 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f      sqlite3OsClo
1a9da 73 65 46 72 65 65 28 70 4d 61 73 74 65 72 29 3b  seFree(pMaster);
1a9db 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
1a9dc 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1a9dd 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1a9de 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  , zMaster);.    
1a9df 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
1a9e0 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74   }..    /* Delet
1a9e1 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  e the master jou
1a9e2 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20  rnal file. This 
1a9e3 63 6f 6d 6d 69 74 73 20 74 68 65 20 74 72 61 6e  commits the tran
1a9e4 73 61 63 74 69 6f 6e 2e 20 41 66 74 65 72 0a 20  saction. After. 
1a9e5 20 20 20 2a 2a 20 64 6f 69 6e 67 20 74 68 69 73     ** doing this
1a9e6 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 69   the directory i
1a9e7 73 20 73 79 6e 63 65 64 20 61 67 61 69 6e 20 62  s synced again b
1a9e8 65 66 6f 72 65 20 61 6e 79 20 69 6e 64 69 76 69  efore any indivi
1a9e9 64 75 61 6c 0a 20 20 20 20 2a 2a 20 74 72 61 6e  dual.    ** tran
1a9ea 73 61 63 74 69 6f 6e 20 66 69 6c 65 73 20 61 72  saction files ar
1a9eb 65 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a  e deleted..    *
1a9ec 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
1a9ed 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c  e3OsDelete(pVfs,
1a9ee 20 7a 4d 61 73 74 65 72 2c 20 31 29 3b 0a 20 20   zMaster, 1);.  
1a9ef 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1a9f0 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20  db, zMaster);.  
1a9f1 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20    zMaster = 0;. 
1a9f2 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
1a9f3 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
1a9f4 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 20    }..    /* All 
1a9f5 66 69 6c 65 73 20 61 6e 64 20 64 69 72 65 63 74  files and direct
1a9f6 6f 72 69 65 73 20 68 61 76 65 20 61 6c 72 65 61  ories have alrea
1a9f7 64 79 20 62 65 65 6e 20 73 79 6e 63 65 64 2c 20  dy been synced, 
1a9f8 73 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  so the following
1a9f9 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 73 20 74 6f  .    ** calls to
1a9fa 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
1a9fb 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 61 72  mitPhaseTwo() ar
1a9fc 65 20 6f 6e 6c 79 20 63 6c 6f 73 69 6e 67 20 66  e only closing f
1a9fd 69 6c 65 73 20 61 6e 64 0a 20 20 20 20 2a 2a 20  iles and.    ** 
1a9fe 64 65 6c 65 74 69 6e 67 20 6f 72 20 74 72 75 6e  deleting or trun
1a9ff 63 61 74 69 6e 67 20 6a 6f 75 72 6e 61 6c 73 2e  cating journals.
1aa00 20 49 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f   If something go
1aa01 65 73 20 77 72 6f 6e 67 20 77 68 69 6c 65 0a 20  es wrong while. 
1aa02 20 20 20 2a 2a 20 74 68 69 73 20 69 73 20 68 61     ** this is ha
1aa03 70 70 65 6e 69 6e 67 20 77 65 20 64 6f 6e 27 74  ppening we don't
1aa04 20 72 65 61 6c 6c 79 20 63 61 72 65 2e 20 54 68   really care. Th
1aa05 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 20 74  e integrity of t
1aa06 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61  he.    ** transa
1aa07 63 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 79  ction is already
1aa08 20 67 75 61 72 61 6e 74 65 65 64 2c 20 62 75 74   guaranteed, but
1aa09 20 73 6f 6d 65 20 73 74 72 61 79 20 27 63 6f 6c   some stray 'col
1aa0a 64 27 20 6a 6f 75 72 6e 61 6c 73 0a 20 20 20 20  d' journals.    
1aa0b 2a 2a 20 6d 61 79 20 62 65 20 6c 79 69 6e 67 20  ** may be lying 
1aa0c 61 72 6f 75 6e 64 2e 20 52 65 74 75 72 6e 69 6e  around. Returnin
1aa0d 67 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  g an error code 
1aa0e 77 6f 6e 27 74 20 68 65 6c 70 20 6d 61 74 74 65  won't help matte
1aa0f 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64  rs..    */.    d
1aa10 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64  isable_simulated
1aa11 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20  _io_errors();.  
1aa12 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65    sqlite3BeginBe
1aa13 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20  nignMalloc();.  
1aa14 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
1aa15 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20  >nDb; i++){ .   
1aa16 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20     Btree *pBt = 
1aa17 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a  db->aDb[i].pBt;.
1aa18 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 7b        if( pBt ){
1aa19 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1aa1a 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
1aa1b 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20 20 20  Two(pBt);.      
1aa1c 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
1aa1d 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
1aa1e 6f 63 28 29 3b 0a 20 20 20 20 65 6e 61 62 6c 65  oc();.    enable
1aa1f 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72  _simulated_io_er
1aa20 72 6f 72 73 28 29 3b 0a 0a 20 20 20 20 73 71 6c  rors();..    sql
1aa21 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 64  ite3VtabCommit(d
1aa22 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  b);.  }.#endif..
1aa23 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1aa24 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  /* .** This rout
1aa25 69 6e 65 20 63 68 65 63 6b 73 20 74 68 61 74 20  ine checks that 
1aa26 74 68 65 20 73 71 6c 69 74 65 33 2e 61 63 74 69  the sqlite3.acti
1aa27 76 65 56 64 62 65 43 6e 74 20 63 6f 75 6e 74 20  veVdbeCnt count 
1aa28 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61 74 63  variable.** matc
1aa29 68 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  hes the number o
1aa2a 66 20 76 64 62 65 27 73 20 69 6e 20 74 68 65 20  f vdbe's in the 
1aa2b 6c 69 73 74 20 73 71 6c 69 74 65 33 2e 70 56 64  list sqlite3.pVd
1aa2c 62 65 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 63  be that are.** c
1aa2d 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76 65 2e  urrently active.
1aa2e 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61   An assertion fa
1aa2f 69 6c 73 20 69 66 20 74 68 65 20 74 77 6f 20 63  ils if the two c
1aa30 6f 75 6e 74 73 20 64 6f 20 6e 6f 74 20 6d 61 74  ounts do not mat
1aa31 63 68 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61  ch..** This is a
1aa32 6e 20 69 6e 74 65 72 6e 61 6c 20 73 65 6c 66 2d  n internal self-
1aa33 63 68 65 63 6b 20 6f 6e 6c 79 20 2d 20 69 74 20  check only - it 
1aa34 69 73 20 6e 6f 74 20 61 6e 20 65 73 73 65 6e 74  is not an essent
1aa35 69 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 0a 2a  ial processing.*
1aa36 2a 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a 20 54 68  * step..**.** Th
1aa37 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66  is is a no-op if
1aa38 20 4e 44 45 42 55 47 20 69 73 20 64 65 66 69 6e   NDEBUG is defin
1aa39 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e  ed..*/.#ifndef N
1aa3a 44 45 42 55 47 0a 73 74 61 74 69 63 20 76 6f 69  DEBUG.static voi
1aa3b 64 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 62  d checkActiveVdb
1aa3c 65 43 6e 74 28 73 71 6c 69 74 65 33 20 2a 64 62  eCnt(sqlite3 *db
1aa3d 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20  ){.  Vdbe *p;.  
1aa3e 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 69  int cnt = 0;.  i
1aa3f 6e 74 20 6e 57 72 69 74 65 20 3d 20 30 3b 0a 20  nt nWrite = 0;. 
1aa40 20 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b 0a   p = db->pVdbe;.
1aa41 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20    while( p ){.  
1aa42 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d 3d    if( p->magic==
1aa43 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26  VDBE_MAGIC_RUN &
1aa44 26 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20  & p->pc>=0 ){.  
1aa45 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20      cnt++;.     
1aa46 20 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c 79   if( p->readOnly
1aa47 3d 3d 30 20 29 20 6e 57 72 69 74 65 2b 2b 3b 0a  ==0 ) nWrite++;.
1aa48 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d      }.    p = p-
1aa49 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 61 73  >pNext;.  }.  as
1aa4a 73 65 72 74 28 20 63 6e 74 3d 3d 64 62 2d 3e 61  sert( cnt==db->a
1aa4b 63 74 69 76 65 56 64 62 65 43 6e 74 20 29 3b 0a  ctiveVdbeCnt );.
1aa4c 20 20 61 73 73 65 72 74 28 20 6e 57 72 69 74 65    assert( nWrite
1aa4d 3d 3d 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43  ==db->writeVdbeC
1aa4e 6e 74 20 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64  nt );.}.#else.#d
1aa4f 65 66 69 6e 65 20 63 68 65 63 6b 41 63 74 69 76  efine checkActiv
1aa50 65 56 64 62 65 43 6e 74 28 78 29 0a 23 65 6e 64  eVdbeCnt(x).#end
1aa51 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 65 76  if../*.** For ev
1aa52 65 72 79 20 42 74 72 65 65 20 74 68 61 74 20 69  ery Btree that i
1aa53 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  n database conne
1aa54 63 74 69 6f 6e 20 64 62 20 77 68 69 63 68 20 0a  ction db which .
1aa55 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69  ** has been modi
1aa56 66 69 65 64 2c 20 22 74 72 69 70 22 20 6f 72 20  fied, "trip" or 
1aa57 69 6e 76 61 6c 69 64 61 74 65 20 65 61 63 68 20  invalidate each 
1aa58 63 75 72 73 6f 72 20 69 6e 0a 2a 2a 20 74 68 61  cursor in.** tha
1aa59 74 20 42 74 72 65 65 20 6d 69 67 68 74 20 68 61  t Btree might ha
1aa5a 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64  ve been modified
1aa5b 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 75 72   so that the cur
1aa5c 73 6f 72 0a 2a 2a 20 63 61 6e 20 6e 65 76 65 72  sor.** can never
1aa5d 20 62 65 20 75 73 65 64 20 61 67 61 69 6e 2e 20   be used again. 
1aa5e 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 68   This happens wh
1aa5f 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a  en a rollback.**
1aa60 2a 20 6f 63 63 75 72 73 2e 20 20 57 65 20 68 61  * occurs.  We ha
1aa61 76 65 20 74 6f 20 74 72 69 70 20 61 6c 6c 20 74  ve to trip all t
1aa62 68 65 20 6f 74 68 65 72 20 63 75 72 73 6f 72 73  he other cursors
1aa63 2c 20 65 76 65 6e 0a 2a 2a 20 63 75 72 73 6f 72  , even.** cursor
1aa64 20 66 72 6f 6d 20 6f 74 68 65 72 20 56 4d 73 20   from other VMs 
1aa65 69 6e 20 64 69 66 66 65 72 65 6e 74 20 64 61 74  in different dat
1aa66 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1aa67 73 2c 0a 2a 2a 20 73 6f 20 74 68 61 74 20 6e 6f  s,.** so that no
1aa68 6e 65 20 6f 66 20 74 68 65 6d 20 74 72 79 20 74  ne of them try t
1aa69 6f 20 75 73 65 20 74 68 65 20 64 61 74 61 20 61  o use the data a
1aa6a 74 20 77 68 69 63 68 20 74 68 65 79 0a 2a 2a 20  t which they.** 
1aa6b 77 65 72 65 20 70 6f 69 6e 74 69 6e 67 20 61 6e  were pointing an
1aa6c 64 20 77 68 69 63 68 20 6e 6f 77 20 6d 61 79 20  d which now may 
1aa6d 68 61 76 65 20 62 65 65 6e 20 63 68 61 6e 67 65  have been change
1aa6e 64 20 64 75 65 0a 2a 2a 20 74 6f 20 74 68 65 20  d due.** to the 
1aa6f 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20  rollback..**.** 
1aa70 52 65 6d 65 6d 62 65 72 20 74 68 61 74 20 61 20  Remember that a 
1aa71 72 6f 6c 6c 62 61 63 6b 20 63 61 6e 20 64 65 6c  rollback can del
1aa72 65 74 65 20 74 61 62 6c 65 73 20 63 6f 6d 70 6c  ete tables compl
1aa73 65 74 65 20 61 6e 64 0a 2a 2a 20 72 65 6f 72 64  ete and.** reord
1aa74 65 72 20 72 6f 6f 74 70 61 67 65 73 2e 20 20 53  er rootpages.  S
1aa75 6f 20 69 74 20 69 73 20 6e 6f 74 20 73 75 66 66  o it is not suff
1aa76 69 63 69 65 6e 74 20 6a 75 73 74 20 74 6f 20 73  icient just to s
1aa77 61 76 65 0a 2a 2a 20 74 68 65 20 73 74 61 74 65  ave.** the state
1aa78 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 2e 20   of the cursor. 
1aa79 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e 76 61   We have to inva
1aa7a 6c 69 64 61 74 65 20 74 68 65 20 63 75 72 73 6f  lidate the curso
1aa7b 72 0a 2a 2a 20 73 6f 20 74 68 61 74 20 69 74 20  r.** so that it 
1aa7c 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 61 67  is never used ag
1aa7d 61 69 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ain..*/.static v
1aa7e 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 43 75  oid invalidateCu
1aa7f 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64 42  rsorsOnModifiedB
1aa80 74 72 65 65 73 28 73 71 6c 69 74 65 33 20 2a 64  trees(sqlite3 *d
1aa81 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66  b){.  int i;.  f
1aa82 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
1aa83 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72  b; i++){.    Btr
1aa84 65 65 20 2a 70 20 3d 20 64 62 2d 3e 61 44 62 5b  ee *p = db->aDb[
1aa85 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20  i].pBt;.    if( 
1aa86 70 20 26 26 20 73 71 6c 69 74 65 33 42 74 72 65  p && sqlite3Btre
1aa87 65 49 73 49 6e 54 72 61 6e 73 28 70 29 20 29 7b  eIsInTrans(p) ){
1aa88 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  .      sqlite3Bt
1aa89 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72  reeTripAllCursor
1aa8a 73 28 70 2c 20 53 51 4c 49 54 45 5f 41 42 4f 52  s(p, SQLITE_ABOR
1aa8b 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  T);.    }.  }.}.
1aa8c 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 56 64  ./*.** If the Vd
1aa8d 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65  be passed as the
1aa8e 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
1aa8f 6f 70 65 6e 65 64 20 61 20 73 74 61 74 65 6d 65  opened a stateme
1aa90 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a  nt-transaction,.
1aa91 2a 2a 20 63 6c 6f 73 65 20 69 74 20 6e 6f 77 2e  ** close it now.
1aa92 20 41 72 67 75 6d 65 6e 74 20 65 4f 70 20 6d 75   Argument eOp mu
1aa93 73 74 20 62 65 20 65 69 74 68 65 72 20 53 41 56  st be either SAV
1aa94 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20  EPOINT_ROLLBACK 
1aa95 6f 72 0a 2a 2a 20 53 41 56 45 50 4f 49 4e 54 5f  or.** SAVEPOINT_
1aa96 52 45 4c 45 41 53 45 2e 20 49 66 20 69 74 20 69  RELEASE. If it i
1aa97 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  s SAVEPOINT_ROLL
1aa98 42 41 43 4b 2c 20 74 68 65 6e 20 74 68 65 20 73  BACK, then the s
1aa99 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e  tatement.** tran
1aa9a 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65  saction is rolle
1aa9b 64 20 62 61 63 6b 2e 20 49 66 20 65 4f 70 20 69  d back. If eOp i
1aa9c 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45  s SAVEPOINT_RELE
1aa9d 41 53 45 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a  ASE, then the .*
1aa9e 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  * statement tran
1aa9f 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 74  saction is commt
1aaa0 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ted..**.** If an
1aaa1 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73   IO error occurs
1aaa2 2c 20 61 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  , an SQLITE_IOER
1aaa3 52 5f 58 58 58 20 65 72 72 6f 72 20 63 6f 64 65  R_XXX error code
1aaa4 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a   is returned. .*
1aaa5 2a 20 4f 74 68 65 72 77 69 73 65 20 53 51 4c 49  * Otherwise SQLI
1aaa6 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45  TE_OK..*/.SQLITE
1aaa7 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1aaa8 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 61  ite3VdbeCloseSta
1aaa9 74 65 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c 20  tement(Vdbe *p, 
1aaaa 69 6e 74 20 65 4f 70 29 7b 0a 20 20 73 71 6c 69  int eOp){.  sqli
1aaab 74 65 33 20 2a 63 6f 6e 73 74 20 64 62 20 3d 20  te3 *const db = 
1aaac 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 63 20  p->db;.  int rc 
1aaad 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  = SQLITE_OK;..  
1aaae 2f 2a 20 49 66 20 70 2d 3e 69 53 74 61 74 65 6d  /* If p->iStatem
1aaaf 65 6e 74 20 69 73 20 67 72 65 61 74 65 72 20 74  ent is greater t
1aab0 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74  han zero, then t
1aab1 68 69 73 20 56 64 62 65 20 6f 70 65 6e 65 64 20  his Vdbe opened 
1aab2 61 20 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e  a .  ** statemen
1aab3 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 68  t transaction th
1aab4 61 74 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f  at should be clo
1aab5 73 65 64 20 68 65 72 65 2e 20 54 68 65 20 6f 6e  sed here. The on
1aab6 6c 79 20 65 78 63 65 70 74 69 6f 6e 0a 20 20 2a  ly exception.  *
1aab7 2a 20 69 73 20 74 68 61 74 20 61 6e 20 49 4f 20  * is that an IO 
1aab8 65 72 72 6f 72 20 6d 61 79 20 68 61 76 65 20 6f  error may have o
1aab9 63 63 75 72 65 64 2c 20 63 61 75 73 69 6e 67 20  ccured, causing 
1aaba 61 6e 20 65 6d 65 72 67 65 6e 63 79 20 72 6f 6c  an emergency rol
1aabb 6c 62 61 63 6b 2e 0a 20 20 2a 2a 20 49 6e 20 74  lback..  ** In t
1aabc 68 69 73 20 63 61 73 65 20 28 64 62 2d 3e 6e 53  his case (db->nS
1aabd 74 61 74 65 6d 65 6e 74 3d 3d 30 29 2c 20 61 6e  tatement==0), an
1aabe 64 20 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69  d there is nothi
1aabf 6e 67 20 74 6f 20 64 6f 2e 0a 20 20 2a 2f 0a 20  ng to do..  */. 
1aac0 20 69 66 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d   if( db->nStatem
1aac1 65 6e 74 20 26 26 20 70 2d 3e 69 53 74 61 74 65  ent && p->iState
1aac2 6d 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20  ment ){.    int 
1aac3 69 3b 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74  i;.    const int
1aac4 20 69 53 61 76 65 70 6f 69 6e 74 20 3d 20 70 2d   iSavepoint = p-
1aac5 3e 69 53 74 61 74 65 6d 65 6e 74 2d 31 3b 0a 0a  >iStatement-1;..
1aac6 20 20 20 20 61 73 73 65 72 74 28 20 65 4f 70 3d      assert( eOp=
1aac7 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42  =SAVEPOINT_ROLLB
1aac8 41 43 4b 20 7c 7c 20 65 4f 70 3d 3d 53 41 56 45  ACK || eOp==SAVE
1aac9 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29 3b 0a  POINT_RELEASE);.
1aaca 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
1aacb 6e 53 74 61 74 65 6d 65 6e 74 3e 30 20 29 3b 0a  nStatement>0 );.
1aacc 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69      assert( p->i
1aacd 53 74 61 74 65 6d 65 6e 74 3d 3d 28 64 62 2d 3e  Statement==(db->
1aace 6e 53 74 61 74 65 6d 65 6e 74 2b 64 62 2d 3e 6e  nStatement+db->n
1aacf 53 61 76 65 70 6f 69 6e 74 29 20 29 3b 0a 0a 20  Savepoint) );.. 
1aad0 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62     for(i=0; i<db
1aad1 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20  ->nDb; i++){ .  
1aad2 20 20 20 20 69 6e 74 20 72 63 32 20 3d 20 53 51      int rc2 = SQ
1aad3 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 42  LITE_OK;.      B
1aad4 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e  tree *pBt = db->
1aad5 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20  aDb[i].pBt;.    
1aad6 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20    if( pBt ){.   
1aad7 20 20 20 20 20 69 66 28 20 65 4f 70 3d 3d 53 41       if( eOp==SA
1aad8 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b  VEPOINT_ROLLBACK
1aad9 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
1aada 32 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  2 = sqlite3Btree
1aadb 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2c 20 53  Savepoint(pBt, S
1aadc 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
1aadd 4b 2c 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a  K, iSavepoint);.
1aade 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1aadf 20 20 69 66 28 20 72 63 32 3d 3d 53 51 4c 49 54    if( rc2==SQLIT
1aae0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1aae1 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 42    rc2 = sqlite3B
1aae2 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28 70 42  treeSavepoint(pB
1aae3 74 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c  t, SAVEPOINT_REL
1aae4 45 41 53 45 2c 20 69 53 61 76 65 70 6f 69 6e 74  EASE, iSavepoint
1aae5 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
1aae6 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
1aae7 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1aae8 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20      rc = rc2;.  
1aae9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1aaea 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 6e 53      }.    db->nS
1aaeb 74 61 74 65 6d 65 6e 74 2d 2d 3b 0a 20 20 20 20  tatement--;.    
1aaec 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20  p->iStatement = 
1aaed 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
1aaee 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  rc;.}../*.** If 
1aaef 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c  SQLite is compil
1aaf0 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 73 68  ed to support sh
1aaf1 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20  ared-cache mode 
1aaf2 61 6e 64 20 74 6f 20 62 65 20 74 68 72 65 61 64  and to be thread
1aaf3 73 61 66 65 2c 0a 2a 2a 20 74 68 69 73 20 72 6f  safe,.** this ro
1aaf4 75 74 69 6e 65 20 6f 62 74 61 69 6e 73 20 74 68  utine obtains th
1aaf5 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74  e mutex associat
1aaf6 65 64 20 77 69 74 68 20 65 61 63 68 20 42 74 53  ed with each BtS
1aaf7 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 0a  hared structure.
1aaf8 2a 2a 20 74 68 61 74 20 6d 61 79 20 62 65 20 61  ** that may be a
1aaf9 63 63 65 73 73 65 64 20 62 79 20 74 68 65 20 56  ccessed by the V
1aafa 4d 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61  M passed as an a
1aafb 72 67 75 6d 65 6e 74 2e 20 49 6e 20 64 6f 69 6e  rgument. In doin
1aafc 67 20 73 6f 20 69 74 0a 2a 2a 20 73 65 74 73 20  g so it.** sets 
1aafd 74 68 65 20 42 74 53 68 61 72 65 64 2e 64 62 20  the BtShared.db 
1aafe 6d 65 6d 62 65 72 20 6f 66 20 65 61 63 68 20 6f  member of each o
1aaff 66 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73  f the BtShared s
1ab00 74 72 75 63 74 75 72 65 73 2c 20 65 6e 73 75 72  tructures, ensur
1ab01 69 6e 67 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  ing.** that the 
1ab02 63 6f 72 72 65 63 74 20 62 75 73 79 2d 68 61 6e  correct busy-han
1ab03 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73  dler callback is
1ab04 20 69 6e 76 6f 6b 65 64 20 69 66 20 72 65 71 75   invoked if requ
1ab05 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53  ired..**.** If S
1ab06 51 4c 69 74 65 20 69 73 20 6e 6f 74 20 74 68 72  QLite is not thr
1ab07 65 61 64 73 61 66 65 20 62 75 74 20 64 6f 65 73  eadsafe but does
1ab08 20 73 75 70 70 6f 72 74 20 73 68 61 72 65 64 2d   support shared-
1ab09 63 61 63 68 65 20 6d 6f 64 65 2c 20 74 68 65 6e  cache mode, then
1ab0a 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65  .** sqlite3Btree
1ab0b 45 6e 74 65 72 41 6c 6c 28 29 20 69 73 20 69 6e  EnterAll() is in
1ab0c 76 6f 6b 65 64 20 74 6f 20 73 65 74 20 74 68 65  voked to set the
1ab0d 20 42 74 53 68 61 72 65 64 2e 64 62 20 76 61 72   BtShared.db var
1ab0e 69 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 61 6c 6c  iables.** of all
1ab0f 20 6f 66 20 42 74 53 68 61 72 65 64 20 73 74 72   of BtShared str
1ab10 75 63 74 75 72 65 73 20 61 63 63 65 73 73 69 62  uctures accessib
1ab11 6c 65 20 76 69 61 20 74 68 65 20 64 61 74 61 62  le via the datab
1ab12 61 73 65 20 68 61 6e 64 6c 65 20 0a 2a 2a 20 61  ase handle .** a
1ab13 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
1ab14 68 65 20 56 4d 2e 20 4f 66 20 63 6f 75 72 73 65  he VM. Of course
1ab15 20 6f 6e 6c 79 20 61 20 73 75 62 73 65 74 20 6f   only a subset o
1ab16 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72  f these structur
1ab17 65 73 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 61 63  es.** will be ac
1ab18 63 65 73 73 65 64 20 62 79 20 74 68 65 20 56 4d  cessed by the VM
1ab19 2c 20 61 6e 64 20 77 65 20 63 6f 75 6c 64 20 75  , and we could u
1ab1a 73 65 20 56 64 62 65 2e 62 74 72 65 65 4d 61 73  se Vdbe.btreeMas
1ab1b 6b 20 74 6f 20 66 69 67 75 72 65 0a 2a 2a 20 74  k to figure.** t
1ab1c 68 61 74 20 73 75 62 73 65 74 20 6f 75 74 2c 20  hat subset out, 
1ab1d 62 75 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20  but there is no 
1ab1e 61 64 76 61 6e 74 61 67 65 20 74 6f 20 64 6f 69  advantage to doi
1ab1f 6e 67 20 73 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ng so..**.** If 
1ab20 53 51 4c 69 74 65 20 69 73 20 6e 6f 74 20 74 68  SQLite is not th
1ab21 72 65 61 64 73 61 66 65 20 61 6e 64 20 64 6f 65  readsafe and doe
1ab22 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 73 68  s not support sh
1ab23 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 2c  ared-cache mode,
1ab24 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f   this.** functio
1ab25 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  n is a no-op..*/
1ab26 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1ab27 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
1ab28 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  E.SQLITE_PRIVATE
1ab29 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
1ab2a 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72  eMutexArrayEnter
1ab2b 28 56 64 62 65 20 2a 70 29 7b 0a 23 69 66 20 53  (Vdbe *p){.#if S
1ab2c 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
1ab2d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  .  sqlite3BtreeM
1ab2e 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 26  utexArrayEnter(&
1ab2f 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 23 65 6c 73  p->aMutex);.#els
1ab30 65 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  e.  sqlite3Btree
1ab31 45 6e 74 65 72 41 6c 6c 28 70 2d 3e 64 62 29 3b  EnterAll(p->db);
1ab32 0a 23 65 6e 64 69 66 0a 7d 0a 23 65 6e 64 69 66  .#endif.}.#endif
1ab33 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
1ab34 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74  tine is called t
1ab35 68 65 20 77 68 65 6e 20 61 20 56 44 42 45 20 74  he when a VDBE t
1ab36 72 69 65 73 20 74 6f 20 68 61 6c 74 2e 20 20 49  ries to halt.  I
1ab37 66 20 74 68 65 20 56 44 42 45 0a 2a 2a 20 68 61  f the VDBE.** ha
1ab38 73 20 6d 61 64 65 20 63 68 61 6e 67 65 73 20 61  s made changes a
1ab39 6e 64 20 69 73 20 69 6e 20 61 75 74 6f 63 6f 6d  nd is in autocom
1ab3a 6d 69 74 20 6d 6f 64 65 2c 20 74 68 65 6e 20 63  mit mode, then c
1ab3b 6f 6d 6d 69 74 20 74 68 6f 73 65 0a 2a 2a 20 63  ommit those.** c
1ab3c 68 61 6e 67 65 73 2e 20 20 49 66 20 61 20 72 6f  hanges.  If a ro
1ab3d 6c 6c 62 61 63 6b 20 69 73 20 6e 65 65 64 65 64  llback is needed
1ab3e 2c 20 74 68 65 6e 20 64 6f 20 74 68 65 20 72 6f  , then do the ro
1ab3f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68  llback..**.** Th
1ab40 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
1ab41 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f  e only way to mo
1ab42 76 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20  ve the state of 
1ab43 61 20 56 4d 20 66 72 6f 6d 0a 2a 2a 20 53 51 4c  a VM from.** SQL
1ab44 49 54 45 5f 4d 41 47 49 43 5f 52 55 4e 20 74 6f  ITE_MAGIC_RUN to
1ab45 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 48 41   SQLITE_MAGIC_HA
1ab46 4c 54 2e 20 20 49 74 20 69 73 20 68 61 72 6d 6c  LT.  It is harml
1ab47 65 73 73 20 74 6f 0a 2a 2a 20 63 61 6c 6c 20 74  ess to.** call t
1ab48 68 69 73 20 6f 6e 20 61 20 56 4d 20 74 68 61 74  his on a VM that
1ab49 20 69 73 20 69 6e 20 74 68 65 20 53 51 4c 49 54   is in the SQLIT
1ab4a 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 73 74 61  E_MAGIC_HALT sta
1ab4b 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  te..**.** Return
1ab4c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20   an error code. 
1ab4d 20 49 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 63   If the commit c
1ab4e 6f 75 6c 64 20 6e 6f 74 20 63 6f 6d 70 6c 65 74  ould not complet
1ab4f 65 20 62 65 63 61 75 73 65 20 6f 66 0a 2a 2a 20  e because of.** 
1ab50 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 2c  lock contention,
1ab51 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
1ab52 55 53 59 2e 20 20 49 66 20 53 51 4c 49 54 45 5f  USY.  If SQLITE_
1ab53 42 55 53 59 20 69 73 20 72 65 74 75 72 6e 65 64  BUSY is returned
1ab54 2c 20 69 74 0a 2a 2a 20 6d 65 61 6e 73 20 74 68  , it.** means th
1ab55 65 20 63 6c 6f 73 65 20 64 69 64 20 6e 6f 74 20  e close did not 
1ab56 68 61 70 70 65 6e 20 61 6e 64 20 6e 65 65 64 73  happen and needs
1ab57 20 74 6f 20 62 65 20 72 65 70 65 61 74 65 64 2e   to be repeated.
1ab58 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1ab59 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
1ab5a 62 65 48 61 6c 74 28 56 64 62 65 20 2a 70 29 7b  beHalt(Vdbe *p){
1ab5b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
1ab5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ab5d 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 73 74     /* Used to st
1ab5e 6f 72 65 20 74 72 61 6e 73 69 65 6e 74 20 72 65  ore transient re
1ab5f 74 75 72 6e 20 63 6f 64 65 73 20 2a 2f 0a 20 20  turn codes */.  
1ab60 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d  sqlite3 *db = p-
1ab61 3e 64 62 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20  >db;..  /* This 
1ab62 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e  function contain
1ab63 73 20 74 68 65 20 6c 6f 67 69 63 20 74 68 61 74  s the logic that
1ab64 20 64 65 74 65 72 6d 69 6e 65 73 20 69 66 20 61   determines if a
1ab65 20 73 74 61 74 65 6d 65 6e 74 20 6f 72 0a 20 20   statement or.  
1ab66 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  ** transaction w
1ab67 69 6c 6c 20 62 65 20 63 6f 6d 6d 69 74 74 65 64  ill be committed
1ab68 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20   or rolled back 
1ab69 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 74  as a result of t
1ab6a 68 65 0a 20 20 2a 2a 20 65 78 65 63 75 74 69 6f  he.  ** executio
1ab6b 6e 20 6f 66 20 74 68 69 73 20 76 69 72 74 75 61  n of this virtua
1ab6c 6c 20 6d 61 63 68 69 6e 65 2e 20 0a 20 20 2a 2a  l machine. .  **
1ab6d 0a 20 20 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20  .  ** If any of 
1ab6e 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 72  the following er
1ab6f 72 6f 72 73 20 6f 63 63 75 72 3a 0a 20 20 2a 2a  rors occur:.  **
1ab70 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  .  **     SQLITE
1ab71 5f 4e 4f 4d 45 4d 0a 20 20 2a 2a 20 20 20 20 20  _NOMEM.  **     
1ab72 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 2a  SQLITE_IOERR.  *
1ab73 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 46 55 4c  *     SQLITE_FUL
1ab74 4c 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54  L.  **     SQLIT
1ab75 45 5f 49 4e 54 45 52 52 55 50 54 0a 20 20 2a 2a  E_INTERRUPT.  **
1ab76 0a 20 20 2a 2a 20 54 68 65 6e 20 74 68 65 20 69  .  ** Then the i
1ab77 6e 74 65 72 6e 61 6c 20 63 61 63 68 65 20 6d 69  nternal cache mi
1ab78 67 68 74 20 68 61 76 65 20 62 65 65 6e 20 6c 65  ght have been le
1ab79 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69  ft in an inconsi
1ab7a 73 74 65 6e 74 0a 20 20 2a 2a 20 73 74 61 74 65  stent.  ** state
1ab7b 2e 20 20 57 65 20 6e 65 65 64 20 74 6f 20 72 6f  .  We need to ro
1ab7c 6c 6c 62 61 63 6b 20 74 68 65 20 73 74 61 74 65  llback the state
1ab7d 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
1ab7e 2c 20 69 66 20 74 68 65 72 65 20 69 73 0a 20 20  , if there is.  
1ab7f 2a 2a 20 6f 6e 65 2c 20 6f 72 20 74 68 65 20 63  ** one, or the c
1ab80 6f 6d 70 6c 65 74 65 20 74 72 61 6e 73 61 63 74  omplete transact
1ab81 69 6f 6e 20 69 66 20 74 68 65 72 65 20 69 73 20  ion if there is 
1ab82 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61  no statement tra
1ab83 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 0a  nsaction..  */..
1ab84 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c    if( p->db->mal
1ab85 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
1ab86 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f   p->rc = SQLITE_
1ab87 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 63 6c 6f  NOMEM;.  }.  clo
1ab88 73 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 29 3b  seAllCursors(p);
1ab89 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21  .  if( p->magic!
1ab8a 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20  =VDBE_MAGIC_RUN 
1ab8b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
1ab8c 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 63  LITE_OK;.  }.  c
1ab8d 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 43 6e  heckActiveVdbeCn
1ab8e 74 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 4e 6f 20  t(db);..  /* No 
1ab8f 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61  commit or rollba
1ab90 63 6b 20 6e 65 65 64 65 64 20 69 66 20 74 68 65  ck needed if the
1ab91 20 70 72 6f 67 72 61 6d 20 6e 65 76 65 72 20 73   program never s
1ab92 74 61 72 74 65 64 20 2a 2f 0a 20 20 69 66 28 20  tarted */.  if( 
1ab93 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20  p->pc>=0 ){.    
1ab94 69 6e 74 20 6d 72 63 3b 20 20 20 2f 2a 20 50 72  int mrc;   /* Pr
1ab95 69 6d 61 72 79 20 65 72 72 6f 72 20 63 6f 64 65  imary error code
1ab96 20 66 72 6f 6d 20 70 2d 3e 72 63 20 2a 2f 0a 20   from p->rc */. 
1ab97 20 20 20 69 6e 74 20 65 53 74 61 74 65 6d 65 6e     int eStatemen
1ab98 74 4f 70 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74  tOp = 0;.    int
1ab99 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 3b   isSpecialError;
1ab9a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
1ab9b 65 74 20 74 6f 20 74 72 75 65 20 69 66 20 61 20  et to true if a 
1ab9c 27 73 70 65 63 69 61 6c 27 20 65 72 72 6f 72 20  'special' error 
1ab9d 2a 2f 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63 6b 20  */..    /* Lock 
1ab9e 61 6c 6c 20 62 74 72 65 65 73 20 75 73 65 64 20  all btrees used 
1ab9f 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  by the statement
1aba0 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
1aba1 64 62 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74  dbeMutexArrayEnt
1aba2 65 72 28 70 29 3b 0a 0a 20 20 20 20 2f 2a 20 43  er(p);..    /* C
1aba3 68 65 63 6b 20 66 6f 72 20 6f 6e 65 20 6f 66 20  heck for one of 
1aba4 74 68 65 20 73 70 65 63 69 61 6c 20 65 72 72 6f  the special erro
1aba5 72 73 20 2a 2f 0a 20 20 20 20 6d 72 63 20 3d 20  rs */.    mrc = 
1aba6 70 2d 3e 72 63 20 26 20 30 78 66 66 3b 0a 20 20  p->rc & 0xff;.  
1aba7 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 21    assert( p->rc!
1aba8 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c  =SQLITE_IOERR_BL
1aba9 4f 43 4b 45 44 20 29 3b 20 20 2f 2a 20 54 68 69  OCKED );  /* Thi
1abaa 73 20 65 72 72 6f 72 20 6e 6f 20 6c 6f 6e 67 65  s error no longe
1abab 72 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 20  r exists */.    
1abac 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 20 3d  isSpecialError =
1abad 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d   mrc==SQLITE_NOM
1abae 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54  EM || mrc==SQLIT
1abaf 45 5f 49 4f 45 52 52 0a 20 20 20 20 20 20 20 20  E_IOERR.        
1abb0 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
1abb1 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45  mrc==SQLITE_INTE
1abb2 52 52 55 50 54 20 7c 7c 20 6d 72 63 3d 3d 53 51  RRUPT || mrc==SQ
1abb3 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 69  LITE_FULL;.    i
1abb4 66 28 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f  f( isSpecialErro
1abb5 72 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66  r ){.      /* If
1abb6 20 74 68 65 20 71 75 65 72 79 20 77 61 73 20 72   the query was r
1abb7 65 61 64 2d 6f 6e 6c 79 2c 20 77 65 20 6e 65 65  ead-only, we nee
1abb8 64 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b  d do no rollback
1abb9 20 61 74 20 61 6c 6c 2e 20 4f 74 68 65 72 77 69   at all. Otherwi
1abba 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f  se,.      ** pro
1abbb 63 65 65 64 20 77 69 74 68 20 74 68 65 20 73 70  ceed with the sp
1abbc 65 63 69 61 6c 20 68 61 6e 64 6c 69 6e 67 2e 0a  ecial handling..
1abbd 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
1abbe 66 28 20 21 70 2d 3e 72 65 61 64 4f 6e 6c 79 20  f( !p->readOnly 
1abbf 7c 7c 20 6d 72 63 21 3d 53 51 4c 49 54 45 5f 49  || mrc!=SQLITE_I
1abc0 4e 54 45 52 52 55 50 54 20 29 7b 0a 20 20 20 20  NTERRUPT ){.    
1abc1 20 20 20 20 69 66 28 20 28 6d 72 63 3d 3d 53 51      if( (mrc==SQ
1abc2 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 6d 72  LITE_NOMEM || mr
1abc3 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 29 20  c==SQLITE_FULL) 
1abc4 26 26 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f  && p->usesStmtJo
1abc5 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 20  urnal ){.       
1abc6 20 20 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 20     eStatementOp 
1abc7 3d 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  = SAVEPOINT_ROLL
1abc8 42 41 43 4b 3b 0a 20 20 20 20 20 20 20 20 7d 65  BACK;.        }e
1abc9 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  lse{.          /
1abca 2a 20 57 65 20 61 72 65 20 66 6f 72 63 65 64 20  * We are forced 
1abcb 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 74 68 65  to roll back the
1abcc 20 61 63 74 69 76 65 20 74 72 61 6e 73 61 63 74   active transact
1abcd 69 6f 6e 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e  ion. Before doin
1abce 67 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73  g.          ** s
1abcf 6f 2c 20 61 62 6f 72 74 20 61 6e 79 20 6f 74 68  o, abort any oth
1abd0 65 72 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68  er statements th
1abd1 69 73 20 68 61 6e 64 6c 65 20 63 75 72 72 65 6e  is handle curren
1abd2 74 6c 79 20 68 61 73 20 61 63 74 69 76 65 2e 0a  tly has active..
1abd3 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
1abd4 20 20 20 20 20 20 20 69 6e 76 61 6c 69 64 61 74         invalidat
1abd5 65 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69  eCursorsOnModifi
1abd6 65 64 42 74 72 65 65 73 28 64 62 29 3b 0a 20 20  edBtrees(db);.  
1abd7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52          sqlite3R
1abd8 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a  ollbackAll(db);.
1abd9 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1abda 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73  3CloseSavepoints
1abdb 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20  (db);.          
1abdc 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d  db->autoCommit =
1abdd 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
1abde 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20      }.    }.  . 
1abdf 20 20 20 2f 2a 20 49 66 20 74 68 65 20 61 75 74     /* If the aut
1abe0 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73  o-commit flag is
1abe1 20 73 65 74 20 61 6e 64 20 74 68 69 73 20 69 73   set and this is
1abe2 20 74 68 65 20 6f 6e 6c 79 20 61 63 74 69 76 65   the only active
1abe3 20 77 72 69 74 65 72 20 0a 20 20 20 20 2a 2a 20   writer .    ** 
1abe4 56 4d 2c 20 74 68 65 6e 20 77 65 20 64 6f 20 65  VM, then we do e
1abe5 69 74 68 65 72 20 61 20 63 6f 6d 6d 69 74 20 6f  ither a commit o
1abe6 72 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 74 68  r rollback of th
1abe7 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61  e current transa
1abe8 63 74 69 6f 6e 2e 20 0a 20 20 20 20 2a 2a 0a 20  ction. .    **. 
1abe9 20 20 20 2a 2a 20 4e 6f 74 65 3a 20 54 68 69 73     ** Note: This
1abea 20 62 6c 6f 63 6b 20 61 6c 73 6f 20 72 75 6e 73   block also runs
1abeb 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 73   if one of the s
1abec 70 65 63 69 61 6c 20 65 72 72 6f 72 73 20 68 61  pecial errors ha
1abed 6e 64 6c 65 64 20 0a 20 20 20 20 2a 2a 20 61 62  ndled .    ** ab
1abee 6f 76 65 20 68 61 73 20 6f 63 63 75 72 72 65 64  ove has occurred
1abef 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  . .    */.    if
1abf0 28 20 21 73 71 6c 69 74 65 33 56 74 61 62 49 6e  ( !sqlite3VtabIn
1abf1 53 79 6e 63 28 64 62 29 20 0a 20 20 20 20 20 26  Sync(db) .     &
1abf2 26 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74  & db->autoCommit
1abf3 20 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 77 72   .     && db->wr
1abf4 69 74 65 56 64 62 65 43 6e 74 3d 3d 28 70 2d 3e  iteVdbeCnt==(p->
1abf5 72 65 61 64 4f 6e 6c 79 3d 3d 30 29 20 0a 20 20  readOnly==0) .  
1abf6 20 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70    ){.      if( p
1abf7 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ->rc==SQLITE_OK 
1abf8 7c 7c 20 28 70 2d 3e 65 72 72 6f 72 41 63 74 69  || (p->errorActi
1abf9 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 26 26 20 21  on==OE_Fail && !
1abfa 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 29 20  isSpecialError) 
1abfb 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
1abfc 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c  e auto-commit fl
1abfd 61 67 20 69 73 20 74 72 75 65 2c 20 61 6e 64 20  ag is true, and 
1abfe 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d  the vdbe program
1abff 20 77 61 73 20 0a 20 20 20 20 20 20 20 20 2a 2a   was .        **
1ac00 20 73 75 63 63 65 73 73 66 75 6c 20 6f 72 20 68   successful or h
1ac01 69 74 20 61 6e 20 27 4f 52 20 46 41 49 4c 27 20  it an 'OR FAIL' 
1ac02 63 6f 6e 73 74 72 61 69 6e 74 2e 20 54 68 69 73  constraint. This
1ac03 20 6d 65 61 6e 73 20 61 20 63 6f 6d 6d 69 74 20   means a commit 
1ac04 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 72  .        ** is r
1ac05 65 71 75 69 72 65 64 2e 0a 20 20 20 20 20 20 20  equired..       
1ac06 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d   */.        rc =
1ac07 20 76 64 62 65 43 6f 6d 6d 69 74 28 64 62 2c 20   vdbeCommit(db, 
1ac08 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  p);.        if( 
1ac09 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  rc==SQLITE_BUSY 
1ac0a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
1ac0b 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72  ite3BtreeMutexAr
1ac0c 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d 75  rayLeave(&p->aMu
1ac0d 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 20 20  tex);.          
1ac0e 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
1ac0f 53 59 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  SY;.        }els
1ac10 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45  e if( rc!=SQLITE
1ac11 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
1ac12 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20   p->rc = rc;.   
1ac13 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f         sqlite3Ro
1ac14 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20  llbackAll(db);. 
1ac15 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1ac16 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43          sqlite3C
1ac17 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 61  ommitInternalCha
1ac18 6e 67 65 73 28 64 62 29 3b 0a 20 20 20 20 20 20  nges(db);.      
1ac19 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b    }.      }else{
1ac1a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1ac1b 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b  RollbackAll(db);
1ac1c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64  .      }.      d
1ac1d 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 20 3d 20  b->nStatement = 
1ac1e 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  0;.    }else if(
1ac1f 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 3d 3d 30   eStatementOp==0
1ac20 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d   ){.      if( p-
1ac21 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c  >rc==SQLITE_OK |
1ac22 7c 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e  | p->errorAction
1ac23 3d 3d 4f 45 5f 46 61 69 6c 20 29 7b 0a 20 20 20  ==OE_Fail ){.   
1ac24 20 20 20 20 20 65 53 74 61 74 65 6d 65 6e 74 4f       eStatementO
1ac25 70 20 3d 20 53 41 56 45 50 4f 49 4e 54 5f 52 45  p = SAVEPOINT_RE
1ac26 4c 45 41 53 45 3b 0a 20 20 20 20 20 20 7d 65 6c  LEASE;.      }el
1ac27 73 65 20 69 66 28 20 70 2d 3e 65 72 72 6f 72 41  se if( p->errorA
1ac28 63 74 69 6f 6e 3d 3d 4f 45 5f 41 62 6f 72 74 20  ction==OE_Abort 
1ac29 29 7b 0a 20 20 20 20 20 20 20 20 65 53 74 61 74  ){.        eStat
1ac2a 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f  ementOp = SAVEPO
1ac2b 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20  INT_ROLLBACK;.  
1ac2c 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1ac2d 20 20 20 69 6e 76 61 6c 69 64 61 74 65 43 75 72     invalidateCur
1ac2e 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64 42 74  sorsOnModifiedBt
1ac2f 72 65 65 73 28 64 62 29 3b 0a 20 20 20 20 20 20  rees(db);.      
1ac30 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63    sqlite3Rollbac
1ac31 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20  kAll(db);.      
1ac32 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61    sqlite3CloseSa
1ac33 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20  vepoints(db);.  
1ac34 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f        db->autoCo
1ac35 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20  mmit = 1;.      
1ac36 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  }.    }.  .    /
1ac37 2a 20 49 66 20 65 53 74 61 74 65 6d 65 6e 74 4f  * If eStatementO
1ac38 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  p is non-zero, t
1ac39 68 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20  hen a statement 
1ac3a 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64  transaction need
1ac3b 73 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65 20 63  s to.    ** be c
1ac3c 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c  ommitted or roll
1ac3d 65 64 20 62 61 63 6b 2e 20 43 61 6c 6c 20 73 71  ed back. Call sq
1ac3e 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74  lite3VdbeCloseSt
1ac3f 61 74 65 6d 65 6e 74 28 29 20 74 6f 0a 20 20 20  atement() to.   
1ac40 20 2a 2a 20 64 6f 20 73 6f 2e 20 49 66 20 74 68   ** do so. If th
1ac41 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 72 65 74  is operation ret
1ac42 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 2c 20 61  urns an error, a
1ac43 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  nd the current s
1ac44 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 2a 2a 20  tatement.    ** 
1ac45 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 53 51  error code is SQ
1ac46 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 4c 49 54  LITE_OK or SQLIT
1ac47 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 74 68  E_CONSTRAINT, th
1ac48 65 6e 20 73 65 74 20 74 68 65 20 65 72 72 6f 72  en set the error
1ac49 0a 20 20 20 20 2a 2a 20 63 6f 64 65 20 74 6f 20  .    ** code to 
1ac4a 74 68 65 20 6e 65 77 20 76 61 6c 75 65 2e 0a 20  the new value.. 
1ac4b 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 53     */.    if( eS
1ac4c 74 61 74 65 6d 65 6e 74 4f 70 20 29 7b 0a 20 20  tatementOp ){.  
1ac4d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1ac4e 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65  VdbeCloseStateme
1ac4f 6e 74 28 70 2c 20 65 53 74 61 74 65 6d 65 6e 74  nt(p, eStatement
1ac50 4f 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  Op);.      if( r
1ac51 63 20 26 26 20 28 70 2d 3e 72 63 3d 3d 53 51 4c  c && (p->rc==SQL
1ac52 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d  ITE_OK || p->rc=
1ac53 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49  =SQLITE_CONSTRAI
1ac54 4e 54 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70  NT) ){.        p
1ac55 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 20  ->rc = rc;.     
1ac56 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1ac57 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29  (db, p->zErrMsg)
1ac58 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 7a 45 72  ;.        p->zEr
1ac59 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 20 20  rMsg = 0;.      
1ac5a 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  }.    }.  .    /
1ac5b 2a 20 49 66 20 74 68 69 73 20 77 61 73 20 61 6e  * If this was an
1ac5c 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 20   INSERT, UPDATE 
1ac5d 6f 72 20 44 45 4c 45 54 45 20 61 6e 64 20 6e 6f  or DELETE and no
1ac5e 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73   statement trans
1ac5f 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 68 61  action.    ** ha
1ac60 73 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61  s been rolled ba
1ac61 63 6b 2c 20 75 70 64 61 74 65 20 74 68 65 20 64  ck, update the d
1ac62 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1ac63 6f 6e 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65  on change-counte
1ac64 72 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  r. .    */.    i
1ac65 66 28 20 70 2d 3e 63 68 61 6e 67 65 43 6e 74 4f  f( p->changeCntO
1ac66 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 65  n ){.      if( e
1ac67 53 74 61 74 65 6d 65 6e 74 4f 70 21 3d 53 41 56  StatementOp!=SAV
1ac68 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20  EPOINT_ROLLBACK 
1ac69 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1ac6a 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73  e3VdbeSetChanges
1ac6b 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29  (db, p->nChange)
1ac6c 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1ac6d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1ac6e 62 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c  beSetChanges(db,
1ac6f 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   0);.      }.   
1ac70 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20     p->nChange = 
1ac71 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20  0;.    }.  .    
1ac72 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 6f 72 20 63  /* Rollback or c
1ac73 6f 6d 6d 69 74 20 61 6e 79 20 73 63 68 65 6d 61  ommit any schema
1ac74 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 6f 63   changes that oc
1ac75 63 75 72 72 65 64 2e 20 2a 2f 0a 20 20 20 20 69  curred. */.    i
1ac76 66 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45  f( p->rc!=SQLITE
1ac77 5f 4f 4b 20 26 26 20 64 62 2d 3e 66 6c 61 67 73  _OK && db->flags
1ac78 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68  &SQLITE_InternCh
1ac79 61 6e 67 65 73 20 29 7b 0a 20 20 20 20 20 20 73  anges ){.      s
1ac7a 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
1ac7b 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29  nalSchema(db, 0)
1ac7c 3b 0a 20 20 20 20 20 20 64 62 2d 3e 66 6c 61 67  ;.      db->flag
1ac7d 73 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 7c  s = (db->flags |
1ac7e 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68   SQLITE_InternCh
1ac7f 61 6e 67 65 73 29 3b 0a 20 20 20 20 7d 0a 0a 20  anges);.    }.. 
1ac80 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68     /* Release th
1ac81 65 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 20 20 73  e locks */.    s
1ac82 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
1ac83 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61  ArrayLeave(&p->a
1ac84 4d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f  Mutex);.  }..  /
1ac85 2a 20 57 65 20 68 61 76 65 20 73 75 63 63 65 73  * We have succes
1ac86 73 66 75 6c 6c 79 20 68 61 6c 74 65 64 20 61 6e  sfully halted an
1ac87 64 20 63 6c 6f 73 65 64 20 74 68 65 20 56 4d 2e  d closed the VM.
1ac88 20 20 52 65 63 6f 72 64 20 74 68 69 73 20 66 61    Record this fa
1ac89 63 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ct. */.  if( p->
1ac8a 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d  pc>=0 ){.    db-
1ac8b 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 2d 2d  >activeVdbeCnt--
1ac8c 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 72 65  ;.    if( !p->re
1ac8d 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20  adOnly ){.      
1ac8e 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74  db->writeVdbeCnt
1ac8f 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  --;.    }.    as
1ac90 73 65 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65  sert( db->active
1ac91 56 64 62 65 43 6e 74 3e 3d 64 62 2d 3e 77 72 69  VdbeCnt>=db->wri
1ac92 74 65 56 64 62 65 43 6e 74 20 29 3b 0a 20 20 7d  teVdbeCnt );.  }
1ac93 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44  .  p->magic = VD
1ac94 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 3b 0a 20  BE_MAGIC_HALT;. 
1ac95 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 65   checkActiveVdbe
1ac96 43 6e 74 28 64 62 29 3b 0a 20 20 69 66 28 20 70  Cnt(db);.  if( p
1ac97 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
1ac98 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20  ed ){.    p->rc 
1ac99 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
1ac9a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
1ac9b 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61   auto-commit fla
1ac9c 67 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 65  g is set to true
1ac9d 2c 20 74 68 65 6e 20 61 6e 79 20 6c 6f 63 6b 73  , then any locks
1ac9e 20 74 68 61 74 20 77 65 72 65 20 68 65 6c 64 0a   that were held.
1ac9f 20 20 2a 2a 20 62 79 20 63 6f 6e 6e 65 63 74 69    ** by connecti
1aca0 6f 6e 20 64 62 20 68 61 76 65 20 6e 6f 77 20 62  on db have now b
1aca1 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 20 43 61  een released. Ca
1aca2 6c 6c 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63  ll sqlite3Connec
1aca3 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 29 20 0a  tionUnlocked() .
1aca4 20 20 2a 2a 20 74 6f 20 69 6e 76 6f 6b 65 20 61    ** to invoke a
1aca5 6e 79 20 72 65 71 75 69 72 65 64 20 75 6e 6c 6f  ny required unlo
1aca6 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61  ck-notify callba
1aca7 63 6b 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  cks..  */.  if( 
1aca8 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29  db->autoCommit )
1aca9 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6e  {.    sqlite3Con
1acaa 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28  nectionUnlocked(
1acab 64 62 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65  db);.  }..  asse
1acac 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64  rt( db->activeVd
1acad 62 65 43 6e 74 3e 30 20 7c 7c 20 64 62 2d 3e 61  beCnt>0 || db->a
1acae 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20  utoCommit==0 || 
1acaf 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d  db->nStatement==
1acb0 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  0 );.  return SQ
1acb1 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a  LITE_OK;.}.../*.
1acb2 2a 2a 20 45 61 63 68 20 56 44 42 45 20 68 6f 6c  ** Each VDBE hol
1acb3 64 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  ds the result of
1acb4 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
1acb5 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20   sqlite3_step() 
1acb6 63 61 6c 6c 0a 2a 2a 20 69 6e 20 70 2d 3e 72 63  call.** in p->rc
1acb7 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
1acb8 73 65 74 73 20 74 68 61 74 20 72 65 73 75 6c 74  sets that result
1acb9 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 45 5f   back to SQLITE_
1acba 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  OK..*/.SQLITE_PR
1acbb 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1acbc 65 33 56 64 62 65 52 65 73 65 74 53 74 65 70 52  e3VdbeResetStepR
1acbd 65 73 75 6c 74 28 56 64 62 65 20 2a 70 29 7b 0a  esult(Vdbe *p){.
1acbe 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45    p->rc = SQLITE
1acbf 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  _OK;.}../*.** Cl
1acc0 65 61 6e 20 75 70 20 61 20 56 44 42 45 20 61 66  ean up a VDBE af
1acc1 74 65 72 20 65 78 65 63 75 74 69 6f 6e 20 62 75  ter execution bu
1acc2 74 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20  t do not delete 
1acc3 74 68 65 20 56 44 42 45 20 6a 75 73 74 20 79 65  the VDBE just ye
1acc4 74 2e 0a 2a 2a 20 57 72 69 74 65 20 61 6e 79 20  t..** Write any 
1acc5 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69  error messages i
1acc6 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 20 20  nto *pzErrMsg.  
1acc7 52 65 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c  Return the resul
1acc8 74 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 66  t code..**.** Af
1acc9 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ter this routine
1acca 20 69 73 20 72 75 6e 2c 20 74 68 65 20 56 44 42   is run, the VDB
1accb 45 20 73 68 6f 75 6c 64 20 62 65 20 72 65 61 64  E should be read
1accc 79 20 74 6f 20 62 65 20 65 78 65 63 75 74 65 64  y to be executed
1accd 0a 2a 2a 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a  .** again..**.**
1acce 20 54 6f 20 6c 6f 6f 6b 20 61 74 20 69 74 20 61   To look at it a
1accf 6e 6f 74 68 65 72 20 77 61 79 2c 20 74 68 69 73  nother way, this
1acd0 20 72 6f 75 74 69 6e 65 20 72 65 73 65 74 73 20   routine resets 
1acd1 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65  the state of the
1acd2 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68  .** virtual mach
1acd3 69 6e 65 20 66 72 6f 6d 20 56 44 42 45 5f 4d 41  ine from VDBE_MA
1acd4 47 49 43 5f 52 55 4e 20 6f 72 20 56 44 42 45 5f  GIC_RUN or VDBE_
1acd5 4d 41 47 49 43 5f 48 41 4c 54 20 62 61 63 6b 20  MAGIC_HALT back 
1acd6 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43  to.** VDBE_MAGIC
1acd7 5f 49 4e 49 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45  _INIT..*/.SQLITE
1acd8 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1acd9 69 74 65 33 56 64 62 65 52 65 73 65 74 28 56 64  ite3VdbeReset(Vd
1acda 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65  be *p){.  sqlite
1acdb 33 20 2a 64 62 3b 0a 20 20 64 62 20 3d 20 70 2d  3 *db;.  db = p-
1acdc 3e 64 62 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  >db;..  /* If th
1acdd 65 20 56 4d 20 64 69 64 20 6e 6f 74 20 72 75 6e  e VM did not run
1acde 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f   to completion o
1acdf 72 20 69 66 20 69 74 20 65 6e 63 6f 75 6e 74 65  r if it encounte
1ace0 72 65 64 20 61 6e 0a 20 20 2a 2a 20 65 72 72 6f  red an.  ** erro
1ace1 72 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74  r, then it might
1ace2 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e 20 68   not have been h
1ace3 61 6c 74 65 64 20 70 72 6f 70 65 72 6c 79 2e 20  alted properly. 
1ace4 20 53 6f 20 68 61 6c 74 0a 20 20 2a 2a 20 69 74   So halt.  ** it
1ace5 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 28 76 6f   now..  */.  (vo
1ace6 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
1ace7 4f 6e 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65  On(db);.  sqlite
1ace8 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20  3VdbeHalt(p);.  
1ace9 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
1acea 65 74 79 4f 66 66 28 64 62 29 3b 0a 0a 20 20 2f  etyOff(db);..  /
1aceb 2a 20 49 66 20 74 68 65 20 56 44 42 45 20 68 61  * If the VDBE ha
1acec 73 20 62 65 20 72 75 6e 20 65 76 65 6e 20 70 61  s be run even pa
1aced 72 74 69 61 6c 6c 79 2c 20 74 68 65 6e 20 74 72  rtially, then tr
1acee 61 6e 73 66 65 72 20 74 68 65 20 65 72 72 6f 72  ansfer the error
1acef 20 63 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 20 65   code.  ** and e
1acf0 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f  rror message fro
1acf1 6d 20 74 68 65 20 56 44 42 45 20 69 6e 74 6f 20  m the VDBE into 
1acf2 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
1acf3 65 20 73 74 72 75 63 74 75 72 65 2e 20 20 42 75  e structure.  Bu
1acf4 74 0a 20 20 2a 2a 20 69 66 20 74 68 65 20 56 44  t.  ** if the VD
1acf5 42 45 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e  BE has just been
1acf6 20 73 65 74 20 74 6f 20 72 75 6e 20 62 75 74 20   set to run but 
1acf7 68 61 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79  has not actually
1acf8 20 65 78 65 63 75 74 65 64 20 61 6e 79 0a 20 20   executed any.  
1acf9 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20  ** instructions 
1acfa 79 65 74 2c 20 6c 65 61 76 65 20 74 68 65 20 6d  yet, leave the m
1acfb 61 69 6e 20 64 61 74 61 62 61 73 65 20 65 72 72  ain database err
1acfc 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75  or information u
1acfd 6e 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20  nchanged..  */. 
1acfe 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b   if( p->pc>=0 ){
1acff 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a 45 72 72  .    if( p->zErr
1ad00 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Msg ){.      sql
1ad01 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d  ite3BeginBenignM
1ad02 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 73  alloc();.      s
1ad03 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74  qlite3ValueSetSt
1ad04 72 28 64 62 2d 3e 70 45 72 72 2c 2d 31 2c 70 2d  r(db->pErr,-1,p-
1ad05 3e 7a 45 72 72 4d 73 67 2c 53 51 4c 49 54 45 5f  >zErrMsg,SQLITE_
1ad06 55 54 46 38 2c 53 51 4c 49 54 45 5f 54 52 41 4e  UTF8,SQLITE_TRAN
1ad07 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 73 71  SIENT);.      sq
1ad08 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61  lite3EndBenignMa
1ad09 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 64 62  lloc();.      db
1ad0a 2d 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72  ->errCode = p->r
1ad0b 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  c;.      sqlite3
1ad0c 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45  DbFree(db, p->zE
1ad0d 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 70 2d  rrMsg);.      p-
1ad0e 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  >zErrMsg = 0;.  
1ad0f 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72    }else if( p->r
1ad10 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  c ){.      sqlit
1ad11 65 33 45 72 72 6f 72 28 64 62 2c 20 70 2d 3e 72  e3Error(db, p->r
1ad12 63 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65  c, 0);.    }else
1ad13 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
1ad14 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
1ad15 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  OK, 0);.    }.  
1ad16 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 20  }else if( p->rc 
1ad17 26 26 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b  && p->expired ){
1ad18 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 69  .    /* The expi
1ad19 72 65 64 20 66 6c 61 67 20 77 61 73 20 73 65 74  red flag was set
1ad1a 20 6f 6e 20 74 68 65 20 56 44 42 45 20 62 65 66   on the VDBE bef
1ad1b 6f 72 65 20 74 68 65 20 66 69 72 73 74 20 63 61  ore the first ca
1ad1c 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 73 71 6c  ll.    ** to sql
1ad1d 69 74 65 33 5f 73 74 65 70 28 29 2e 20 46 6f 72  ite3_step(). For
1ad1e 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 28 73 69   consistency (si
1ad1f 6e 63 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70  nce sqlite3_step
1ad20 28 29 20 77 61 73 0a 20 20 20 20 2a 2a 20 63 61  () was.    ** ca
1ad21 6c 6c 65 64 29 2c 20 73 65 74 20 74 68 65 20 64  lled), set the d
1ad22 61 74 61 62 61 73 65 20 65 72 72 6f 72 20 69 6e  atabase error in
1ad23 20 74 68 69 73 20 63 61 73 65 20 61 73 20 77 65   this case as we
1ad24 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  ll..    */.    s
1ad25 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
1ad26 70 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 73  p->rc, 0);.    s
1ad27 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74  qlite3ValueSetSt
1ad28 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c 20  r(db->pErr, -1, 
1ad29 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 53 51 4c 49  p->zErrMsg, SQLI
1ad2a 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f  TE_UTF8, SQLITE_
1ad2b 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20  TRANSIENT);.    
1ad2c 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1ad2d 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20  , p->zErrMsg);. 
1ad2e 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20     p->zErrMsg = 
1ad2f 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 63  0;.  }..  /* Rec
1ad30 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 20  laim all memory 
1ad31 75 73 65 64 20 62 79 20 74 68 65 20 56 44 42 45  used by the VDBE
1ad32 0a 20 20 2a 2f 0a 20 20 43 6c 65 61 6e 75 70 28  .  */.  Cleanup(
1ad33 70 29 3b 0a 0a 20 20 2f 2a 20 53 61 76 65 20 70  p);..  /* Save p
1ad34 72 6f 66 69 6c 69 6e 67 20 69 6e 66 6f 72 6d 61  rofiling informa
1ad35 74 69 6f 6e 20 66 72 6f 6d 20 74 68 69 73 20 56  tion from this V
1ad36 44 42 45 20 72 75 6e 2e 0a 20 20 2a 2f 0a 23 69  DBE run..  */.#i
1ad37 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c  fdef VDBE_PROFIL
1ad38 45 0a 20 20 7b 0a 20 20 20 20 46 49 4c 45 20 2a  E.  {.    FILE *
1ad39 6f 75 74 20 3d 20 66 6f 70 65 6e 28 22 76 64 62  out = fopen("vdb
1ad3a 65 5f 70 72 6f 66 69 6c 65 2e 6f 75 74 22 2c 20  e_profile.out", 
1ad3b 22 61 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75  "a");.    if( ou
1ad3c 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  t ){.      int i
1ad3d 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28  ;.      fprintf(
1ad3e 6f 75 74 2c 20 22 2d 2d 2d 2d 20 22 29 3b 0a 20  out, "---- ");. 
1ad3f 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
1ad40 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20  p->nOp; i++){.  
1ad41 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75        fprintf(ou
1ad42 74 2c 20 22 25 30 32 78 22 2c 20 70 2d 3e 61 4f  t, "%02x", p->aO
1ad43 70 5b 69 5d 2e 6f 70 63 6f 64 65 29 3b 0a 20 20  p[i].opcode);.  
1ad44 20 20 20 20 7d 0a 20 20 20 20 20 20 66 70 72 69      }.      fpri
1ad45 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a  ntf(out, "\n");.
1ad46 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
1ad47 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20  <p->nOp; i++){. 
1ad48 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f         fprintf(o
1ad49 75 74 2c 20 22 25 36 64 20 25 31 30 6c 6c 64 20  ut, "%6d %10lld 
1ad4a 25 38 6c 6c 64 20 22 2c 0a 20 20 20 20 20 20 20  %8lld ",.       
1ad4b 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e      p->aOp[i].cn
1ad4c 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d  t,.           p-
1ad4d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2c 0a  >aOp[i].cycles,.
1ad4e 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61 4f             p->aO
1ad4f 70 5b 69 5d 2e 63 6e 74 3e 30 20 3f 20 70 2d 3e  p[i].cnt>0 ? p->
1ad50 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2f 70 2d  aOp[i].cycles/p-
1ad51 3e 61 4f 70 5b 69 5d 2e 63 6e 74 20 3a 20 30 0a  >aOp[i].cnt : 0.
1ad52 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
1ad53 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72     sqlite3VdbePr
1ad54 69 6e 74 4f 70 28 6f 75 74 2c 20 69 2c 20 26 70  intOp(out, i, &p
1ad55 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 20 20  ->aOp[i]);.     
1ad56 20 7d 0a 20 20 20 20 20 20 66 63 6c 6f 73 65 28   }.      fclose(
1ad57 6f 75 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  out);.    }.  }.
1ad58 23 65 6e 64 69 66 0a 20 20 70 2d 3e 6d 61 67 69  #endif.  p->magi
1ad59 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49  c = VDBE_MAGIC_I
1ad5a 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d  NIT;.  return p-
1ad5b 3e 72 63 20 26 20 64 62 2d 3e 65 72 72 4d 61 73  >rc & db->errMas
1ad5c 6b 3b 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20 43 6c 65  k;.}. ./*.** Cle
1ad5d 61 6e 20 75 70 20 61 6e 64 20 64 65 6c 65 74 65  an up and delete
1ad5e 20 61 20 56 44 42 45 20 61 66 74 65 72 20 65 78   a VDBE after ex
1ad5f 65 63 75 74 69 6f 6e 2e 20 20 52 65 74 75 72 6e  ecution.  Return
1ad60 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63   an integer whic
1ad61 68 20 69 73 0a 2a 2a 20 74 68 65 20 72 65 73 75  h is.** the resu
1ad62 6c 74 20 63 6f 64 65 2e 20 20 57 72 69 74 65 20  lt code.  Write 
1ad63 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67  any error messag
1ad64 65 20 74 65 78 74 20 69 6e 74 6f 20 2a 70 7a 45  e text into *pzE
1ad65 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45  rrMsg..*/.SQLITE
1ad66 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1ad67 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65  ite3VdbeFinalize
1ad68 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74  (Vdbe *p){.  int
1ad69 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1ad6a 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d  .  if( p->magic=
1ad6b 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20  =VDBE_MAGIC_RUN 
1ad6c 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42  || p->magic==VDB
1ad6d 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 29 7b 0a  E_MAGIC_HALT ){.
1ad6e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1ad6f 56 64 62 65 52 65 73 65 74 28 70 29 3b 0a 20 20  VdbeReset(p);.  
1ad70 20 20 61 73 73 65 72 74 28 20 28 72 63 20 26 20    assert( (rc & 
1ad71 70 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d  p->db->errMask)=
1ad72 3d 72 63 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  =rc );.  }.  sql
1ad73 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 28 70  ite3VdbeDelete(p
1ad74 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
1ad75 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68  }../*.** Call th
1ad76 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72  e destructor for
1ad77 20 65 61 63 68 20 61 75 78 64 61 74 61 20 65 6e   each auxdata en
1ad78 74 72 79 20 69 6e 20 70 56 64 62 65 46 75 6e 63  try in pVdbeFunc
1ad79 20 66 6f 72 20 77 68 69 63 68 0a 2a 2a 20 74 68   for which.** th
1ad7a 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
1ad7b 62 69 74 20 69 6e 20 6d 61 73 6b 20 69 73 20 63  bit in mask is c
1ad7c 6c 65 61 72 2e 20 20 41 75 78 64 61 74 61 20 65  lear.  Auxdata e
1ad7d 6e 74 72 69 65 73 20 62 65 79 6f 6e 64 20 33 31  ntries beyond 31
1ad7e 0a 2a 2a 20 61 72 65 20 61 6c 77 61 79 73 20 64  .** are always d
1ad7f 65 73 74 72 6f 79 65 64 2e 20 20 54 6f 20 64 65  estroyed.  To de
1ad80 73 74 72 6f 79 20 61 6c 6c 20 61 75 78 64 61 74  stroy all auxdat
1ad81 61 20 65 6e 74 72 69 65 73 2c 20 63 61 6c 6c 20  a entries, call 
1ad82 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  this.** routine 
1ad83 77 69 74 68 20 6d 61 73 6b 3d 3d 30 2e 0a 2a 2f  with mask==0..*/
1ad84 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1ad85 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
1ad86 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 56 64  DeleteAuxData(Vd
1ad87 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e  beFunc *pVdbeFun
1ad88 63 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a 20 20  c, int mask){.  
1ad89 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30  int i;.  for(i=0
1ad8a 3b 20 69 3c 70 56 64 62 65 46 75 6e 63 2d 3e 6e  ; i<pVdbeFunc->n
1ad8b 41 75 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  Aux; i++){.    s
1ad8c 74 72 75 63 74 20 41 75 78 44 61 74 61 20 2a 70  truct AuxData *p
1ad8d 41 75 78 20 3d 20 26 70 56 64 62 65 46 75 6e 63  Aux = &pVdbeFunc
1ad8e 2d 3e 61 70 41 75 78 5b 69 5d 3b 0a 20 20 20 20  ->apAux[i];.    
1ad8f 69 66 28 20 28 69 3e 33 31 20 7c 7c 20 21 28 6d  if( (i>31 || !(m
1ad90 61 73 6b 26 28 28 28 75 33 32 29 31 29 3c 3c 69  ask&(((u32)1)<<i
1ad91 29 29 29 20 26 26 20 70 41 75 78 2d 3e 70 41 75  ))) && pAux->pAu
1ad92 78 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  x ){.      if( p
1ad93 41 75 78 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a  Aux->xDelete ){.
1ad94 20 20 20 20 20 20 20 20 70 41 75 78 2d 3e 78 44          pAux->xD
1ad95 65 6c 65 74 65 28 70 41 75 78 2d 3e 70 41 75 78  elete(pAux->pAux
1ad96 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1ad97 20 70 41 75 78 2d 3e 70 41 75 78 20 3d 20 30 3b   pAux->pAux = 0;
1ad98 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
1ad99 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e  .** Delete an en
1ad9a 74 69 72 65 20 56 44 42 45 2e 0a 2a 2f 0a 53 51  tire VDBE..*/.SQ
1ad9b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1ad9c 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c  d sqlite3VdbeDel
1ad9d 65 74 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  ete(Vdbe *p){.  
1ad9e 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20  sqlite3 *db;..  
1ad9f 69 66 28 20 4e 45 56 45 52 28 70 3d 3d 30 29 20  if( NEVER(p==0) 
1ada0 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d  ) return;.  db =
1ada1 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 2d   p->db;.  if( p-
1ada2 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 70 2d  >pPrev ){.    p-
1ada3 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20  >pPrev->pNext = 
1ada4 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73  p->pNext;.  }els
1ada5 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  e{.    assert( d
1ada6 62 2d 3e 70 56 64 62 65 3d 3d 70 20 29 3b 0a 20  b->pVdbe==p );. 
1ada7 20 20 20 64 62 2d 3e 70 56 64 62 65 20 3d 20 70     db->pVdbe = p
1ada8 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69  ->pNext;.  }.  i
1ada9 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20  f( p->pNext ){. 
1adaa 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72     p->pNext->pPr
1adab 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 20  ev = p->pPrev;. 
1adac 20 7d 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41   }.  releaseMemA
1adad 72 72 61 79 28 70 2d 3e 61 56 61 72 2c 20 70 2d  rray(p->aVar, p-
1adae 3e 6e 56 61 72 29 3b 0a 20 20 72 65 6c 65 61 73  >nVar);.  releas
1adaf 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61 43 6f  eMemArray(p->aCo
1adb0 6c 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65 73 43 6f  lName, p->nResCo
1adb1 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 29 3b  lumn*COLNAME_N);
1adb2 0a 20 20 76 64 62 65 46 72 65 65 4f 70 41 72 72  .  vdbeFreeOpArr
1adb3 61 79 28 64 62 2c 20 70 2d 3e 61 4f 70 2c 20 70  ay(db, p->aOp, p
1adb4 2d 3e 6e 4f 70 29 3b 0a 20 20 73 71 6c 69 74 65  ->nOp);.  sqlite
1adb5 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61  3DbFree(db, p->a
1adb6 4c 61 62 65 6c 29 3b 0a 20 20 73 71 6c 69 74 65  Label);.  sqlite
1adb7 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61  3DbFree(db, p->a
1adb8 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69  ColName);.  sqli
1adb9 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
1adba 3e 7a 53 71 6c 29 3b 0a 20 20 70 2d 3e 6d 61 67  >zSql);.  p->mag
1adbb 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f  ic = VDBE_MAGIC_
1adbc 44 45 41 44 3b 0a 20 20 73 71 6c 69 74 65 33 44  DEAD;.  sqlite3D
1adbd 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 70 46 72  bFree(db, p->pFr
1adbe 65 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  ee);.  sqlite3Db
1adbf 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a  Free(db, p);.}..
1adc0 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  /*.** Make sure 
1adc1 74 68 65 20 63 75 72 73 6f 72 20 70 20 69 73 20  the cursor p is 
1adc2 72 65 61 64 79 20 74 6f 20 72 65 61 64 20 6f 72  ready to read or
1adc3 20 77 72 69 74 65 20 74 68 65 20 72 6f 77 20 74   write the row t
1adc4 6f 20 77 68 69 63 68 20 69 74 0a 2a 2a 20 77 61  o which it.** wa
1adc5 73 20 6c 61 73 74 20 70 6f 73 69 74 69 6f 6e 65  s last positione
1adc6 64 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 65 72  d.  Return an er
1adc7 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 20 4f  ror code if an O
1adc8 4f 4d 20 66 61 75 6c 74 20 6f 72 20 49 2f 4f 20  OM fault or I/O 
1adc9 65 72 72 6f 72 0a 2a 2a 20 70 72 65 76 65 6e 74  error.** prevent
1adca 73 20 75 73 20 66 72 6f 6d 20 70 6f 73 69 74 69  s us from positi
1adcb 6f 6e 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72  oning the cursor
1adcc 20 74 6f 20 69 74 73 20 63 6f 72 72 65 63 74 20   to its correct 
1adcd 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  position..**.** 
1adce 49 66 20 61 20 4d 6f 76 65 54 6f 20 6f 70 65 72  If a MoveTo oper
1adcf 61 74 69 6f 6e 20 69 73 20 70 65 6e 64 69 6e 67  ation is pending
1add0 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 63 75   on the given cu
1add1 72 73 6f 72 2c 20 74 68 65 6e 20 64 6f 20 74 68  rsor, then do th
1add2 61 74 0a 2a 2a 20 4d 6f 76 65 54 6f 20 6e 6f 77  at.** MoveTo now
1add3 2e 20 20 49 66 20 6e 6f 20 6d 6f 76 65 20 69 73  .  If no move is
1add4 20 70 65 6e 64 69 6e 67 2c 20 63 68 65 63 6b 20   pending, check 
1add5 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 72 6f  to see if the ro
1add6 77 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 64 65  w has been.** de
1add7 6c 65 74 65 64 20 6f 75 74 20 66 72 6f 6d 20 75  leted out from u
1add8 6e 64 65 72 20 74 68 65 20 63 75 72 73 6f 72 20  nder the cursor 
1add9 61 6e 64 20 69 66 20 69 74 20 68 61 73 2c 20 6d  and if it has, m
1adda 61 72 6b 20 74 68 65 20 72 6f 77 20 61 73 0a 2a  ark the row as.*
1addb 2a 20 61 20 4e 55 4c 4c 20 72 6f 77 2e 0a 2a 2a  * a NULL row..**
1addc 0a 2a 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f  .** If the curso
1addd 72 20 69 73 20 61 6c 72 65 61 64 79 20 70 6f 69  r is already poi
1adde 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 63 6f 72  nting to the cor
1addf 72 65 63 74 20 72 6f 77 20 61 6e 64 20 74 68 61  rect row and tha
1ade0 74 20 72 6f 77 20 68 61 73 0a 2a 2a 20 6e 6f 74  t row has.** not
1ade1 20 62 65 65 6e 20 64 65 6c 65 74 65 64 20 6f 75   been deleted ou
1ade2 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 68 65  t from under the
1ade3 20 63 75 72 73 6f 72 2c 20 74 68 65 6e 20 74 68   cursor, then th
1ade4 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
1ade5 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45  no-op..*/.SQLITE
1ade6 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1ade7 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f  ite3VdbeCursorMo
1ade8 76 65 74 6f 28 56 64 62 65 43 75 72 73 6f 72 20  veto(VdbeCursor 
1ade9 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 64 65  *p){.  if( p->de
1adea 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 29 7b 0a  ferredMoveto ){.
1adeb 20 20 20 20 69 6e 74 20 72 65 73 2c 20 72 63 3b      int res, rc;
1adec 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
1aded 45 53 54 0a 20 20 20 20 65 78 74 65 72 6e 20 69  EST.    extern i
1adee 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63  nt sqlite3_searc
1adef 68 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 69 66 0a  h_count;.#endif.
1adf0 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69      assert( p->i
1adf1 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 72 63  sTable );.    rc
1adf2 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d   = sqlite3BtreeM
1adf3 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 2d  ovetoUnpacked(p-
1adf4 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 70 2d 3e  >pCursor, 0, p->
1adf5 6d 6f 76 65 74 6f 54 61 72 67 65 74 2c 20 30 2c  movetoTarget, 0,
1adf6 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20   &res);.    if( 
1adf7 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
1adf8 20 20 20 20 70 2d 3e 6c 61 73 74 52 6f 77 69 64      p->lastRowid
1adf9 20 3d 20 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67   = p->movetoTarg
1adfa 65 74 3b 0a 20 20 20 20 70 2d 3e 72 6f 77 69 64  et;.    p->rowid
1adfb 49 73 56 61 6c 69 64 20 3d 20 41 4c 57 41 59 53  IsValid = ALWAYS
1adfc 28 72 65 73 3d 3d 30 29 20 3f 31 3a 30 3b 0a 20  (res==0) ?1:0;. 
1adfd 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 65 73     if( NEVER(res
1adfe 3c 30 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20  <0) ){.      rc 
1adff 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65  = sqlite3BtreeNe
1ae00 78 74 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 26  xt(p->pCursor, &
1ae01 72 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20  res);.      if( 
1ae02 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
1ae03 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c      }.#ifdef SQL
1ae04 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c  ITE_TEST.    sql
1ae05 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e  ite3_search_coun
1ae06 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  t++;.#endif.    
1ae07 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  p->deferredMovet
1ae08 6f 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 63 61  o = 0;.    p->ca
1ae09 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48  cheStatus = CACH
1ae0a 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 65 6c 73 65  E_STALE;.  }else
1ae0b 20 69 66 28 20 41 4c 57 41 59 53 28 70 2d 3e 70   if( ALWAYS(p->p
1ae0c 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 69  Cursor) ){.    i
1ae0d 6e 74 20 68 61 73 4d 6f 76 65 64 3b 0a 20 20 20  nt hasMoved;.   
1ae0e 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65   int rc = sqlite
1ae0f 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 73 4d  3BtreeCursorHasM
1ae10 6f 76 65 64 28 70 2d 3e 70 43 75 72 73 6f 72 2c  oved(p->pCursor,
1ae11 20 26 68 61 73 4d 6f 76 65 64 29 3b 0a 20 20 20   &hasMoved);.   
1ae12 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
1ae13 20 72 63 3b 0a 20 20 20 20 69 66 28 20 68 61 73   rc;.    if( has
1ae14 4d 6f 76 65 64 20 29 7b 0a 20 20 20 20 20 20 70  Moved ){.      p
1ae15 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20  ->cacheStatus = 
1ae16 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20  CACHE_STALE;.   
1ae17 20 20 20 70 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20     p->nullRow = 
1ae18 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  1;.    }.  }.  r
1ae19 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1ae1a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  .}../*.** The fo
1ae1b 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llowing function
1ae1c 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33  s:.**.** sqlite3
1ae1d 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 29  VdbeSerialType()
1ae1e 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53  .** sqlite3VdbeS
1ae1f 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 29 0a 2a  erialTypeLen().*
1ae20 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  * sqlite3VdbeSer
1ae21 69 61 6c 4c 65 6e 28 29 0a 2a 2a 20 73 71 6c 69  ialLen().** sqli
1ae22 74 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 74  te3VdbeSerialPut
1ae23 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62  ().** sqlite3Vdb
1ae24 65 53 65 72 69 61 6c 47 65 74 28 29 0a 2a 2a 0a  eSerialGet().**.
1ae25 2a 2a 20 65 6e 63 61 70 73 75 6c 61 74 65 20 74  ** encapsulate t
1ae26 68 65 20 63 6f 64 65 20 74 68 61 74 20 73 65 72  he code that ser
1ae27 69 61 6c 69 7a 65 73 20 76 61 6c 75 65 73 20 66  ializes values f
1ae28 6f 72 20 73 74 6f 72 61 67 65 20 69 6e 20 53 51  or storage in SQ
1ae29 4c 69 74 65 0a 2a 2a 20 64 61 74 61 20 61 6e 64  Lite.** data and
1ae2a 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73 2e 20   index records. 
1ae2b 45 61 63 68 20 73 65 72 69 61 6c 69 7a 65 64 20  Each serialized 
1ae2c 76 61 6c 75 65 20 63 6f 6e 73 69 73 74 73 20 6f  value consists o
1ae2d 66 20 61 0a 2a 2a 20 27 73 65 72 69 61 6c 2d 74  f a.** 'serial-t
1ae2e 79 70 65 27 20 61 6e 64 20 61 20 62 6c 6f 62 20  ype' and a blob 
1ae2f 6f 66 20 64 61 74 61 2e 20 54 68 65 20 73 65 72  of data. The ser
1ae30 69 61 6c 20 74 79 70 65 20 69 73 20 61 6e 20 38  ial type is an 8
1ae31 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 0a 2a  -byte unsigned.*
1ae32 2a 20 69 6e 74 65 67 65 72 2c 20 73 74 6f 72 65  * integer, store
1ae33 64 20 61 73 20 61 20 76 61 72 69 6e 74 2e 0a 2a  d as a varint..*
1ae34 2a 0a 2a 2a 20 49 6e 20 61 6e 20 53 51 4c 69 74  *.** In an SQLit
1ae35 65 20 69 6e 64 65 78 20 72 65 63 6f 72 64 2c 20  e index record, 
1ae36 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 20  the serial type 
1ae37 69 73 20 73 74 6f 72 65 64 20 64 69 72 65 63 74  is stored direct
1ae38 6c 79 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65  ly before.** the
1ae39 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 74 68   blob of data th
1ae3a 61 74 20 69 74 20 63 6f 72 72 65 73 70 6f 6e 64  at it correspond
1ae3b 73 20 74 6f 2e 20 49 6e 20 61 20 74 61 62 6c 65  s to. In a table
1ae3c 20 72 65 63 6f 72 64 2c 20 61 6c 6c 20 73 65 72   record, all ser
1ae3d 69 61 6c 0a 2a 2a 20 74 79 70 65 73 20 61 72 65  ial.** types are
1ae3e 20 73 74 6f 72 65 64 20 61 74 20 74 68 65 20 73   stored at the s
1ae3f 74 61 72 74 20 6f 66 20 74 68 65 20 72 65 63 6f  tart of the reco
1ae40 72 64 2c 20 61 6e 64 20 74 68 65 20 62 6c 6f 62  rd, and the blob
1ae41 73 20 6f 66 20 64 61 74 61 20 61 74 0a 2a 2a 20  s of data at.** 
1ae42 74 68 65 20 65 6e 64 2e 20 48 65 6e 63 65 20 74  the end. Hence t
1ae43 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 61  hese functions a
1ae44 6c 6c 6f 77 20 74 68 65 20 63 61 6c 6c 65 72 20  llow the caller 
1ae45 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 0a 2a 2a  to handle the.**
1ae46 20 73 65 72 69 61 6c 2d 74 79 70 65 20 61 6e 64   serial-type and
1ae47 20 64 61 74 61 20 62 6c 6f 62 20 73 65 70 65 72   data blob seper
1ae48 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ately..**.** The
1ae49 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65   following table
1ae4a 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 76   describes the v
1ae4b 61 72 69 6f 75 73 20 73 74 6f 72 61 67 65 20 63  arious storage c
1ae4c 6c 61 73 73 65 73 20 66 6f 72 20 64 61 74 61 3a  lasses for data:
1ae4d 0a 2a 2a 0a 2a 2a 20 20 20 73 65 72 69 61 6c 20  .**.**   serial 
1ae4e 74 79 70 65 20 20 20 20 20 20 20 20 62 79 74 65  type        byte
1ae4f 73 20 6f 66 20 64 61 74 61 20 20 20 20 20 20 74  s of data      t
1ae50 79 70 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d  ype.**   -------
1ae51 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d 2d  -------     ----
1ae52 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 2d  -----------    -
1ae53 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
1ae54 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 20 20  *      0        
1ae55 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20               0  
1ae56 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 0a 2a            NULL.*
1ae57 2a 20 20 20 20 20 20 31 20 20 20 20 20 20 20 20  *      1        
1ae58 20 20 20 20 20 20 20 20 20 20 20 20 20 31 20 20               1  
1ae59 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64            signed
1ae5a 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20   integer.**     
1ae5b 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20   2              
1ae5c 20 20 20 20 20 20 20 32 20 20 20 20 20 20 20 20         2        
1ae5d 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67      signed integ
1ae5e 65 72 0a 2a 2a 20 20 20 20 20 20 33 20 20 20 20  er.**      3    
1ae5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ae60 20 33 20 20 20 20 20 20 20 20 20 20 20 20 73 69   3            si
1ae61 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20  gned integer.** 
1ae62 20 20 20 20 20 34 20 20 20 20 20 20 20 20 20 20       4          
1ae63 20 20 20 20 20 20 20 20 20 20 20 34 20 20 20 20             4    
1ae64 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 20 69          signed i
1ae65 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 35  nteger.**      5
1ae66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ae67 20 20 20 20 20 36 20 20 20 20 20 20 20 20 20 20       6          
1ae68 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72    signed integer
1ae69 0a 2a 2a 20 20 20 20 20 20 36 20 20 20 20 20 20  .**      6      
1ae6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
1ae6b 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e              sign
1ae6c 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20  ed integer.**   
1ae6d 20 20 20 37 20 20 20 20 20 20 20 20 20 20 20 20     7            
1ae6e 20 20 20 20 20 20 20 20 20 38 20 20 20 20 20 20           8      
1ae6f 20 20 20 20 20 20 49 45 45 45 20 66 6c 6f 61 74        IEEE float
1ae70 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 20 20 20  .**      8      
1ae71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
1ae72 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 74 65              Inte
1ae73 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 30 0a 2a  ger constant 0.*
1ae74 2a 20 20 20 20 20 20 39 20 20 20 20 20 20 20 20  *      9        
1ae75 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20               0  
1ae76 20 20 20 20 20 20 20 20 20 20 49 6e 74 65 67 65            Intege
1ae77 72 20 63 6f 6e 73 74 61 6e 74 20 31 0a 2a 2a 20  r constant 1.** 
1ae78 20 20 20 20 31 30 2c 31 31 20 20 20 20 20 20 20      10,11       
1ae79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ae7a 20 20 20 20 20 20 20 20 72 65 73 65 72 76 65 64          reserved
1ae7b 20 66 6f 72 20 65 78 70 61 6e 73 69 6f 6e 0a 2a   for expansion.*
1ae7c 2a 20 20 20 20 4e 3e 3d 31 32 20 61 6e 64 20 65  *    N>=12 and e
1ae7d 76 65 6e 20 20 20 20 20 20 20 28 4e 2d 31 32 29  ven       (N-12)
1ae7e 2f 32 20 20 20 20 20 20 20 20 42 4c 4f 42 0a 2a  /2        BLOB.*
1ae7f 2a 20 20 20 20 4e 3e 3d 31 33 20 61 6e 64 20 6f  *    N>=13 and o
1ae80 64 64 20 20 20 20 20 20 20 20 28 4e 2d 31 33 29  dd        (N-13)
1ae81 2f 32 20 20 20 20 20 20 20 20 74 65 78 74 0a 2a  /2        text.*
1ae82 2a 0a 2a 2a 20 54 68 65 20 38 20 61 6e 64 20 39  *.** The 8 and 9
1ae83 20 74 79 70 65 73 20 77 65 72 65 20 61 64 64 65   types were adde
1ae84 64 20 69 6e 20 33 2e 33 2e 30 2c 20 66 69 6c 65  d in 3.3.0, file
1ae85 20 66 6f 72 6d 61 74 20 34 2e 20 20 50 72 69 6f   format 4.  Prio
1ae86 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f 66  r versions.** of
1ae87 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74   SQLite will not
1ae88 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68 6f 73   understand thos
1ae89 65 20 73 65 72 69 61 6c 20 74 79 70 65 73 2e 0a  e serial types..
1ae8a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  */../*.** Return
1ae8b 20 74 68 65 20 73 65 72 69 61 6c 2d 74 79 70 65   the serial-type
1ae8c 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20 73   for the value s
1ae8d 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d 2e 0a 2a  tored in pMem..*
1ae8e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1ae8f 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 65   u32 sqlite3Vdbe
1ae90 53 65 72 69 61 6c 54 79 70 65 28 4d 65 6d 20 2a  SerialType(Mem *
1ae91 70 4d 65 6d 2c 20 69 6e 74 20 66 69 6c 65 5f 66  pMem, int file_f
1ae92 6f 72 6d 61 74 29 7b 0a 20 20 69 6e 74 20 66 6c  ormat){.  int fl
1ae93 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67  ags = pMem->flag
1ae94 73 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 69  s;.  int n;..  i
1ae95 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c  f( flags&MEM_Nul
1ae96 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  l ){.    return 
1ae97 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 61  0;.  }.  if( fla
1ae98 67 73 26 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20  gs&MEM_Int ){.  
1ae99 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
1ae9a 77 68 65 74 68 65 72 20 74 6f 20 75 73 65 20 31  whether to use 1
1ae9b 2c 20 32 2c 20 34 2c 20 36 20 6f 72 20 38 20 62  , 2, 4, 6 or 8 b
1ae9c 79 74 65 73 2e 20 2a 2f 0a 23 20 20 20 64 65 66  ytes. */.#   def
1ae9d 69 6e 65 20 4d 41 58 5f 36 42 59 54 45 20 28 28  ine MAX_6BYTE ((
1ae9e 28 28 69 36 34 29 30 78 30 30 30 30 38 30 30 30  ((i64)0x00008000
1ae9f 29 3c 3c 33 32 29 2d 31 29 0a 20 20 20 20 69 36  )<<32)-1).    i6
1aea0 34 20 69 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b  4 i = pMem->u.i;
1aea1 0a 20 20 20 20 75 36 34 20 75 3b 0a 20 20 20 20  .    u64 u;.    
1aea2 69 66 28 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3e  if( file_format>
1aea3 3d 34 20 26 26 20 28 69 26 31 29 3d 3d 69 20 29  =4 && (i&1)==i )
1aea4 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 38  {.      return 8
1aea5 2b 28 75 33 32 29 69 3b 0a 20 20 20 20 7d 0a 20  +(u32)i;.    }. 
1aea6 20 20 20 75 20 3d 20 69 3c 30 20 3f 20 2d 69 20     u = i<0 ? -i 
1aea7 3a 20 69 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d  : i;.    if( u<=
1aea8 31 32 37 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  127 ) return 1;.
1aea9 20 20 20 20 69 66 28 20 75 3c 3d 33 32 37 36 37      if( u<=32767
1aeaa 20 29 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20   ) return 2;.   
1aeab 20 69 66 28 20 75 3c 3d 38 33 38 38 36 30 37 20   if( u<=8388607 
1aeac 29 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 20 20  ) return 3;.    
1aead 69 66 28 20 75 3c 3d 32 31 34 37 34 38 33 36 34  if( u<=214748364
1aeae 37 20 29 20 72 65 74 75 72 6e 20 34 3b 0a 20 20  7 ) return 4;.  
1aeaf 20 20 69 66 28 20 75 3c 3d 4d 41 58 5f 36 42 59    if( u<=MAX_6BY
1aeb0 54 45 20 29 20 72 65 74 75 72 6e 20 35 3b 0a 20  TE ) return 5;. 
1aeb1 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 7d     return 6;.  }
1aeb2 0a 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d  .  if( flags&MEM
1aeb3 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 65 74  _Real ){.    ret
1aeb4 75 72 6e 20 37 3b 0a 20 20 7d 0a 20 20 61 73 73  urn 7;.  }.  ass
1aeb5 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 2d 3e 6d  ert( pMem->db->m
1aeb6 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 66  allocFailed || f
1aeb7 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45  lags&(MEM_Str|ME
1aeb8 4d 5f 42 6c 6f 62 29 20 29 3b 0a 20 20 6e 20 3d  M_Blob) );.  n =
1aeb9 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 69 66 28 20   pMem->n;.  if( 
1aeba 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f  flags & MEM_Zero
1aebb 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 70 4d 65   ){.    n += pMe
1aebc 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 7d 0a  m->u.nZero;.  }.
1aebd 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 30 20 29    assert( n>=0 )
1aebe 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 6e 2a 32  ;.  return ((n*2
1aebf 29 20 2b 20 31 32 20 2b 20 28 28 66 6c 61 67 73  ) + 12 + ((flags
1aec0 26 4d 45 4d 5f 53 74 72 29 21 3d 30 29 29 3b 0a  &MEM_Str)!=0));.
1aec1 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1aec2 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68  the length of th
1aec3 65 20 64 61 74 61 20 63 6f 72 72 65 73 70 6f 6e  e data correspon
1aec4 64 69 6e 67 20 74 6f 20 74 68 65 20 73 75 70 70  ding to the supp
1aec5 6c 69 65 64 20 73 65 72 69 61 6c 2d 74 79 70 65  lied serial-type
1aec6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1aec7 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56  ATE u32 sqlite3V
1aec8 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e  dbeSerialTypeLen
1aec9 28 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65  (u32 serial_type
1aeca 29 7b 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f  ){.  if( serial_
1aecb 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 20 20 20  type>=12 ){.    
1aecc 72 65 74 75 72 6e 20 28 73 65 72 69 61 6c 5f 74  return (serial_t
1aecd 79 70 65 2d 31 32 29 2f 32 3b 0a 20 20 7d 65 6c  ype-12)/2;.  }el
1aece 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63  se{.    static c
1aecf 6f 6e 73 74 20 75 38 20 61 53 69 7a 65 5b 5d 20  onst u8 aSize[] 
1aed0 3d 20 7b 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20  = { 0, 1, 2, 3, 
1aed1 34 2c 20 36 2c 20 38 2c 20 38 2c 20 30 2c 20 30  4, 6, 8, 8, 0, 0
1aed2 2c 20 30 2c 20 30 20 7d 3b 0a 20 20 20 20 72 65  , 0, 0 };.    re
1aed3 74 75 72 6e 20 61 53 69 7a 65 5b 73 65 72 69 61  turn aSize[seria
1aed4 6c 5f 74 79 70 65 5d 3b 0a 20 20 7d 0a 7d 0a 0a  l_type];.  }.}..
1aed5 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20  /*.** If we are 
1aed6 6f 6e 20 61 6e 20 61 72 63 68 69 74 65 63 74 75  on an architectu
1aed7 72 65 20 77 69 74 68 20 6d 69 78 65 64 2d 65 6e  re with mixed-en
1aed8 64 69 61 6e 20 66 6c 6f 61 74 69 6e 67 20 0a 2a  dian floating .*
1aed9 2a 20 70 6f 69 6e 74 73 20 28 65 78 3a 20 41 52  * points (ex: AR
1aeda 4d 37 29 20 74 68 65 6e 20 73 77 61 70 20 74 68  M7) then swap th
1aedb 65 20 6c 6f 77 65 72 20 34 20 62 79 74 65 73 20  e lower 4 bytes 
1aedc 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 75 70 70  with the .** upp
1aedd 65 72 20 34 20 62 79 74 65 73 2e 20 20 52 65 74  er 4 bytes.  Ret
1aede 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74 2e 0a  urn the result..
1aedf 2a 2a 0a 2a 2a 20 46 6f 72 20 6d 6f 73 74 20 61  **.** For most a
1aee0 72 63 68 69 74 65 63 74 75 72 65 73 2c 20 74 68  rchitectures, th
1aee1 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  is is a no-op..*
1aee2 2a 0a 2a 2a 20 28 6c 61 74 65 72 29 3a 20 20 49  *.** (later):  I
1aee3 74 20 69 73 20 72 65 70 6f 72 74 65 64 20 74 6f  t is reported to
1aee4 20 6d 65 20 74 68 61 74 20 74 68 65 20 6d 69 78   me that the mix
1aee5 65 64 2d 65 6e 64 69 61 6e 20 70 72 6f 62 6c 65  ed-endian proble
1aee6 6d 0a 2a 2a 20 6f 6e 20 41 52 4d 37 20 69 73 20  m.** on ARM7 is 
1aee7 61 6e 20 69 73 73 75 65 20 77 69 74 68 20 47 43  an issue with GC
1aee8 43 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 65 20  C, not with the 
1aee9 41 52 4d 37 20 63 68 69 70 2e 20 20 49 74 20 73  ARM7 chip.  It s
1aeea 65 65 6d 73 0a 2a 2a 20 74 68 61 74 20 65 61 72  eems.** that ear
1aeeb 6c 79 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 47  ly versions of G
1aeec 43 43 20 73 74 6f 72 65 64 20 74 68 65 20 74 77  CC stored the tw
1aeed 6f 20 77 6f 72 64 73 20 6f 66 20 61 20 36 34 2d  o words of a 64-
1aeee 62 69 74 0a 2a 2a 20 66 6c 6f 61 74 20 69 6e 20  bit.** float in 
1aeef 74 68 65 20 77 72 6f 6e 67 20 6f 72 64 65 72 2e  the wrong order.
1aef0 20 20 41 6e 64 20 74 68 61 74 20 65 72 72 6f 72    And that error
1aef1 20 68 61 73 20 62 65 65 6e 20 70 72 6f 70 61 67   has been propag
1aef2 61 74 65 64 0a 2a 2a 20 65 76 65 72 20 73 69 6e  ated.** ever sin
1aef3 63 65 2e 20 20 54 68 65 20 62 6c 61 6d 65 20 69  ce.  The blame i
1aef4 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c  s not necessaril
1aef5 79 20 77 69 74 68 20 47 43 43 2c 20 74 68 6f 75  y with GCC, thou
1aef6 67 68 2e 0a 2a 2a 20 47 43 43 20 6d 69 67 68 74  gh..** GCC might
1aef7 20 68 61 76 65 20 6a 75 73 74 20 63 6f 70 79 69   have just copyi
1aef8 6e 67 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 66  ng the problem f
1aef9 72 6f 6d 20 61 20 70 72 69 6f 72 20 63 6f 6d 70  rom a prior comp
1aefa 69 6c 65 72 2e 0a 2a 2a 20 49 20 61 6d 20 61 6c  iler..** I am al
1aefb 73 6f 20 74 6f 6c 64 20 74 68 61 74 20 6e 65 77  so told that new
1aefc 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 47  er versions of G
1aefd 43 43 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 61  CC that follow a
1aefe 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 41 42   different.** AB
1aeff 49 20 67 65 74 20 74 68 65 20 62 79 74 65 20 6f  I get the byte o
1af00 72 64 65 72 20 72 69 67 68 74 2e 0a 2a 2a 0a 2a  rder right..**.*
1af01 2a 20 44 65 76 65 6c 6f 70 65 72 73 20 75 73 69  * Developers usi
1af02 6e 67 20 53 51 4c 69 74 65 20 6f 6e 20 61 6e 20  ng SQLite on an 
1af03 41 52 4d 37 20 73 68 6f 75 6c 64 20 63 6f 6d 70  ARM7 should comp
1af04 69 6c 65 20 61 6e 64 20 72 75 6e 20 74 68 65 69  ile and run thei
1af05 72 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e  r.** application
1af06 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 5f   using -DSQLITE_
1af07 44 45 42 55 47 3d 31 20 61 74 20 6c 65 61 73 74  DEBUG=1 at least
1af08 20 6f 6e 63 65 2e 20 20 57 69 74 68 20 44 45 42   once.  With DEB
1af09 55 47 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20 73  UG.** enabled, s
1af0a 6f 6d 65 20 61 73 73 65 72 74 73 20 62 65 6c 6f  ome asserts belo
1af0b 77 20 77 69 6c 6c 20 65 6e 73 75 72 65 20 74 68  w will ensure th
1af0c 61 74 20 74 68 65 20 62 79 74 65 20 6f 72 64 65  at the byte orde
1af0d 72 20 6f 66 0a 2a 2a 20 66 6c 6f 61 74 69 6e 67  r of.** floating
1af0e 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 69 73   point values is
1af0f 20 63 6f 72 72 65 63 74 2e 0a 2a 2a 0a 2a 2a 20   correct..**.** 
1af10 28 32 30 30 37 2d 30 38 2d 33 30 29 20 20 46 72  (2007-08-30)  Fr
1af11 61 6e 6b 20 76 61 6e 20 56 75 67 74 20 68 61 73  ank van Vugt has
1af12 20 73 74 75 64 69 65 64 20 74 68 69 73 20 70 72   studied this pr
1af13 6f 62 6c 65 6d 20 63 6c 6f 73 65 6c 79 0a 2a 2a  oblem closely.**
1af14 20 61 6e 64 20 68 61 73 20 73 65 6e 64 20 68 69   and has send hi
1af15 73 20 66 69 6e 64 69 6e 67 73 20 74 6f 20 74 68  s findings to th
1af16 65 20 53 51 4c 69 74 65 20 64 65 76 65 6c 6f 70  e SQLite develop
1af17 65 72 73 2e 20 20 46 72 61 6e 6b 0a 2a 2a 20 77  ers.  Frank.** w
1af18 72 69 74 65 73 20 74 68 61 74 20 73 6f 6d 65 20  rites that some 
1af19 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20 6f 66  Linux kernels of
1af1a 66 65 72 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  fer floating poi
1af1b 6e 74 20 68 61 72 64 77 61 72 65 0a 2a 2a 20 65  nt hardware.** e
1af1c 6d 75 6c 61 74 69 6f 6e 20 74 68 61 74 20 75 73  mulation that us
1af1d 65 73 20 6f 6e 6c 79 20 33 32 2d 62 69 74 20 6d  es only 32-bit m
1af1e 61 6e 74 69 73 73 61 73 20 69 6e 73 74 65 61 64  antissas instead
1af1f 20 6f 66 20 61 20 66 75 6c 6c 20 0a 2a 2a 20 34   of a full .** 4
1af20 38 2d 62 69 74 73 20 61 73 20 72 65 71 75 69 72  8-bits as requir
1af21 65 64 20 62 79 20 74 68 65 20 49 45 45 45 20 73  ed by the IEEE s
1af22 74 61 6e 64 61 72 64 2e 20 20 28 54 68 69 73 20  tandard.  (This 
1af23 69 73 20 74 68 65 0a 2a 2a 20 43 4f 4e 46 49 47  is the.** CONFIG
1af24 5f 46 50 45 5f 46 41 53 54 46 50 45 20 6f 70 74  _FPE_FASTFPE opt
1af25 69 6f 6e 2e 29 20 20 4f 6e 20 73 75 63 68 20 73  ion.)  On such s
1af26 79 73 74 65 6d 73 2c 20 66 6c 6f 61 74 69 6e 67  ystems, floating
1af27 20 70 6f 69 6e 74 0a 2a 2a 20 62 79 74 65 20 73   point.** byte s
1af28 77 61 70 70 69 6e 67 20 62 65 63 6f 6d 65 73 20  wapping becomes 
1af29 76 65 72 79 20 63 6f 6d 70 6c 69 63 61 74 65 64  very complicated
1af2a 2e 20 20 54 6f 20 61 76 6f 69 64 20 70 72 6f 62  .  To avoid prob
1af2b 6c 65 6d 73 2c 0a 2a 2a 20 74 68 65 20 6e 65 63  lems,.** the nec
1af2c 65 73 73 61 72 79 20 62 79 74 65 20 73 77 61 70  essary byte swap
1af2d 70 69 6e 67 20 69 73 20 63 61 72 72 69 65 64 20  ping is carried 
1af2e 6f 75 74 20 75 73 69 6e 67 20 61 20 36 34 2d 62  out using a 64-b
1af2f 69 74 20 69 6e 74 65 67 65 72 0a 2a 2a 20 72 61  it integer.** ra
1af30 74 68 65 72 20 74 68 61 6e 20 61 20 36 34 2d 62  ther than a 64-b
1af31 69 74 20 66 6c 6f 61 74 2e 20 20 46 72 61 6e 6b  it float.  Frank
1af32 20 61 73 73 75 72 65 73 20 75 73 20 74 68 61 74   assures us that
1af33 20 74 68 65 20 63 6f 64 65 20 68 65 72 65 0a 2a   the code here.*
1af34 2a 20 77 6f 72 6b 73 20 66 6f 72 20 68 69 6d 2e  * works for him.
1af35 20 20 57 65 2c 20 74 68 65 20 64 65 76 65 6c 6f    We, the develo
1af36 70 65 72 73 2c 20 68 61 76 65 20 6e 6f 20 77 61  pers, have no wa
1af37 79 20 74 6f 20 69 6e 64 65 70 65 6e 64 65 6e 74  y to independent
1af38 6c 79 0a 2a 2a 20 76 65 72 69 66 79 20 74 68 69  ly.** verify thi
1af39 73 2c 20 62 75 74 20 46 72 61 6e 6b 20 73 65 65  s, but Frank see
1af3a 6d 73 20 74 6f 20 6b 6e 6f 77 20 77 68 61 74 20  ms to know what 
1af3b 68 65 20 69 73 20 74 61 6c 6b 69 6e 67 20 61 62  he is talking ab
1af3c 6f 75 74 0a 2a 2a 20 73 6f 20 77 65 20 74 72 75  out.** so we tru
1af3d 73 74 20 68 69 6d 2e 0a 2a 2f 0a 23 69 66 64 65  st him..*/.#ifde
1af3e 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f 45  f SQLITE_MIXED_E
1af3f 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f 41  NDIAN_64BIT_FLOA
1af40 54 0a 73 74 61 74 69 63 20 75 36 34 20 66 6c 6f  T.static u64 flo
1af41 61 74 53 77 61 70 28 75 36 34 20 69 6e 29 7b 0a  atSwap(u64 in){.
1af42 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 75 36    union {.    u6
1af43 34 20 72 3b 0a 20 20 20 20 75 33 32 20 69 5b 32  4 r;.    u32 i[2
1af44 5d 3b 0a 20 20 7d 20 75 3b 0a 20 20 75 33 32 20  ];.  } u;.  u32 
1af45 74 3b 0a 0a 20 20 75 2e 72 20 3d 20 69 6e 3b 0a  t;..  u.r = in;.
1af46 20 20 74 20 3d 20 75 2e 69 5b 30 5d 3b 0a 20 20    t = u.i[0];.  
1af47 75 2e 69 5b 30 5d 20 3d 20 75 2e 69 5b 31 5d 3b  u.i[0] = u.i[1];
1af48 0a 20 20 75 2e 69 5b 31 5d 20 3d 20 74 3b 0a 20  .  u.i[1] = t;. 
1af49 20 72 65 74 75 72 6e 20 75 2e 72 3b 0a 7d 0a 23   return u.r;.}.#
1af4a 20 64 65 66 69 6e 65 20 73 77 61 70 4d 69 78 65   define swapMixe
1af4b 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 58 29 20  dEndianFloat(X) 
1af4c 20 58 20 3d 20 66 6c 6f 61 74 53 77 61 70 28 58   X = floatSwap(X
1af4d 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ).#else.# define
1af4e 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e   swapMixedEndian
1af4f 46 6c 6f 61 74 28 58 29 0a 23 65 6e 64 69 66 0a  Float(X).#endif.
1af50 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65  ./*.** Write the
1af51 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 74 61   serialized data
1af52 20 62 6c 6f 62 20 66 6f 72 20 74 68 65 20 76 61   blob for the va
1af53 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 70 4d  lue stored in pM
1af54 65 6d 20 69 6e 74 6f 20 0a 2a 2a 20 62 75 66 2e  em into .** buf.
1af55 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74   It is assumed t
1af56 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20 68  hat the caller h
1af57 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 73 75 66  as allocated suf
1af58 66 69 63 69 65 6e 74 20 73 70 61 63 65 2e 0a 2a  ficient space..*
1af59 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
1af5a 62 65 72 20 6f 66 20 62 79 74 65 73 20 77 72 69  ber of bytes wri
1af5b 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 6e 42 75 66  tten..**.** nBuf
1af5c 20 69 73 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f   is the amount o
1af5d 66 20 73 70 61 63 65 20 6c 65 66 74 20 69 6e 20  f space left in 
1af5e 62 75 66 5b 5d 2e 20 20 6e 42 75 66 20 6d 75 73  buf[].  nBuf mus
1af5f 74 20 61 6c 77 61 79 73 20 62 65 0a 2a 2a 20 6c  t always be.** l
1af60 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 68  arge enough to h
1af61 6f 6c 64 20 74 68 65 20 65 6e 74 69 72 65 20 66  old the entire f
1af62 69 65 6c 64 2e 20 20 45 78 63 65 70 74 2c 20 69  ield.  Except, i
1af63 66 20 74 68 65 20 66 69 65 6c 64 20 69 73 0a 2a  f the field is.*
1af64 2a 20 61 20 62 6c 6f 62 20 77 69 74 68 20 61 20  * a blob with a 
1af65 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c  zero-filled tail
1af66 2c 20 74 68 65 6e 20 62 75 66 5b 5d 20 6d 69 67  , then buf[] mig
1af67 68 74 20 62 65 20 6a 75 73 74 20 74 68 65 20 72  ht be just the r
1af68 69 67 68 74 0a 2a 2a 20 73 69 7a 65 20 74 6f 20  ight.** size to 
1af69 68 6f 6c 64 20 65 76 65 72 79 74 68 69 6e 67 20  hold everything 
1af6a 65 78 63 65 70 74 20 66 6f 72 20 74 68 65 20 7a  except for the z
1af6b 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 2e  ero-filled tail.
1af6c 20 20 49 66 20 62 75 66 5b 5d 0a 2a 2a 20 69 73    If buf[].** is
1af6d 20 6f 6e 6c 79 20 62 69 67 20 65 6e 6f 75 67 68   only big enough
1af6e 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e 6f 6e   to hold the non
1af6f 2d 7a 65 72 6f 20 70 72 65 66 69 78 2c 20 74 68  -zero prefix, th
1af70 65 6e 20 6f 6e 6c 79 20 77 72 69 74 65 20 74 68  en only write th
1af71 61 74 0a 2a 2a 20 70 72 65 66 69 78 20 69 6e 74  at.** prefix int
1af72 6f 20 62 75 66 5b 5d 2e 20 20 42 75 74 20 69 66  o buf[].  But if
1af73 20 62 75 66 5b 5d 20 69 73 20 6c 61 72 67 65 20   buf[] is large 
1af74 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 62  enough to hold b
1af75 6f 74 68 20 74 68 65 0a 2a 2a 20 70 72 65 66 69  oth the.** prefi
1af76 78 20 61 6e 64 20 74 68 65 20 74 61 69 6c 20 74  x and the tail t
1af77 68 65 6e 20 77 72 69 74 65 20 74 68 65 20 70 72  hen write the pr
1af78 65 66 69 78 20 61 6e 64 20 73 65 74 20 74 68 65  efix and set the
1af79 20 74 61 69 6c 20 74 6f 20 61 6c 6c 0a 2a 2a 20   tail to all.** 
1af7a 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  zeros..**.** Ret
1af7b 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
1af7c 66 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79  f bytes actually
1af7d 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 62 75   written into bu
1af7e 66 5b 5d 2e 20 20 54 68 65 20 6e 75 6d 62 65 72  f[].  The number
1af7f 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 69 6e 20  .** of bytes in 
1af80 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20  the zero-filled 
1af81 74 61 69 6c 20 69 73 20 69 6e 63 6c 75 64 65 64  tail is included
1af82 20 69 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76   in the return v
1af83 61 6c 75 65 20 6f 6e 6c 79 0a 2a 2a 20 69 66 20  alue only.** if 
1af84 74 68 6f 73 65 20 62 79 74 65 73 20 77 65 72 65  those bytes were
1af85 20 7a 65 72 6f 65 64 20 69 6e 20 62 75 66 5b 5d   zeroed in buf[]
1af86 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/ .SQLITE_PRI
1af87 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33  VATE u32 sqlite3
1af88 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 75 38  VdbeSerialPut(u8
1af89 20 2a 62 75 66 2c 20 69 6e 74 20 6e 42 75 66 2c   *buf, int nBuf,
1af8a 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20   Mem *pMem, int 
1af8b 66 69 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a 20 20  file_format){.  
1af8c 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 20  u32 serial_type 
1af8d 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  = sqlite3VdbeSer
1af8e 69 61 6c 54 79 70 65 28 70 4d 65 6d 2c 20 66 69  ialType(pMem, fi
1af8f 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 75 33  le_format);.  u3
1af90 32 20 6c 65 6e 3b 0a 0a 20 20 2f 2a 20 49 6e 74  2 len;..  /* Int
1af91 65 67 65 72 20 61 6e 64 20 52 65 61 6c 20 2a 2f  eger and Real */
1af92 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79  .  if( serial_ty
1af93 70 65 3c 3d 37 20 26 26 20 73 65 72 69 61 6c 5f  pe<=7 && serial_
1af94 74 79 70 65 3e 30 20 29 7b 0a 20 20 20 20 75 36  type>0 ){.    u6
1af95 34 20 76 3b 0a 20 20 20 20 75 33 32 20 69 3b 0a  4 v;.    u32 i;.
1af96 20 20 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74      if( serial_t
1af97 79 70 65 3d 3d 37 20 29 7b 0a 20 20 20 20 20 20  ype==7 ){.      
1af98 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 76  assert( sizeof(v
1af99 29 3d 3d 73 69 7a 65 6f 66 28 70 4d 65 6d 2d 3e  )==sizeof(pMem->
1af9a 72 29 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63  r) );.      memc
1af9b 70 79 28 26 76 2c 20 26 70 4d 65 6d 2d 3e 72 2c  py(&v, &pMem->r,
1af9c 20 73 69 7a 65 6f 66 28 76 29 29 3b 0a 20 20 20   sizeof(v));.   
1af9d 20 20 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69     swapMixedEndi
1af9e 61 6e 46 6c 6f 61 74 28 76 29 3b 0a 20 20 20 20  anFloat(v);.    
1af9f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 76 20 3d  }else{.      v =
1afa0 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 20 20   pMem->u.i;.    
1afa1 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 69 20 3d 20  }.    len = i = 
1afa2 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
1afa3 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f  lTypeLen(serial_
1afa4 74 79 70 65 29 3b 0a 20 20 20 20 61 73 73 65 72  type);.    asser
1afa5 74 28 20 6c 65 6e 3c 3d 28 75 33 32 29 6e 42 75  t( len<=(u32)nBu
1afa6 66 20 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  f );.    while( 
1afa7 69 2d 2d 20 29 7b 0a 20 20 20 20 20 20 62 75 66  i-- ){.      buf
1afa8 5b 69 5d 20 3d 20 28 75 38 29 28 76 26 30 78 46  [i] = (u8)(v&0xF
1afa9 46 29 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20  F);.      v >>= 
1afaa 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  8;.    }.    ret
1afab 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20  urn len;.  }..  
1afac 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 62 6c 6f  /* String or blo
1afad 62 20 2a 2f 0a 20 20 69 66 28 20 73 65 72 69 61  b */.  if( seria
1afae 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 20  l_type>=12 ){.  
1afaf 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
1afb0 6e 20 2b 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67  n + ((pMem->flag
1afb1 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 29 3f 70 4d  s & MEM_Zero)?pM
1afb2 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3a 30 29 0a 20  em->u.nZero:0). 
1afb3 20 20 20 20 20 20 20 20 20 20 20 20 3d 3d 20 28              == (
1afb4 69 6e 74 29 73 71 6c 69 74 65 33 56 64 62 65 53  int)sqlite3VdbeS
1afb5 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72  erialTypeLen(ser
1afb6 69 61 6c 5f 74 79 70 65 29 20 29 3b 0a 20 20 20  ial_type) );.   
1afb7 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e   assert( pMem->n
1afb8 3c 3d 6e 42 75 66 20 29 3b 0a 20 20 20 20 6c 65  <=nBuf );.    le
1afb9 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 20  n = pMem->n;.   
1afba 20 6d 65 6d 63 70 79 28 62 75 66 2c 20 70 4d 65   memcpy(buf, pMe
1afbb 6d 2d 3e 7a 2c 20 6c 65 6e 29 3b 0a 20 20 20 20  m->z, len);.    
1afbc 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  if( pMem->flags 
1afbd 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20  & MEM_Zero ){.  
1afbe 20 20 20 20 6c 65 6e 20 2b 3d 20 70 4d 65 6d 2d      len += pMem-
1afbf 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 20 20  >u.nZero;.      
1afc0 61 73 73 65 72 74 28 20 6e 42 75 66 3e 3d 30 20  assert( nBuf>=0 
1afc1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 65 6e  );.      if( len
1afc2 20 3e 20 28 75 33 32 29 6e 42 75 66 20 29 7b 0a   > (u32)nBuf ){.
1afc3 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 28 75          len = (u
1afc4 33 32 29 6e 42 75 66 3b 0a 20 20 20 20 20 20 7d  32)nBuf;.      }
1afc5 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 62  .      memset(&b
1afc6 75 66 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20  uf[pMem->n], 0, 
1afc7 6c 65 6e 2d 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20  len-pMem->n);.  
1afc8 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6c    }.    return l
1afc9 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 55  en;.  }..  /* NU
1afca 4c 4c 20 6f 72 20 63 6f 6e 73 74 61 6e 74 73 20  LL or constants 
1afcb 30 20 6f 72 20 31 20 2a 2f 0a 20 20 72 65 74 75  0 or 1 */.  retu
1afcc 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  rn 0;.}../*.** D
1afcd 65 73 65 72 69 61 6c 69 7a 65 20 74 68 65 20 64  eserialize the d
1afce 61 74 61 20 62 6c 6f 62 20 70 6f 69 6e 74 65 64  ata blob pointed
1afcf 20 74 6f 20 62 79 20 62 75 66 20 61 73 20 73 65   to by buf as se
1afd0 72 69 61 6c 20 74 79 70 65 20 73 65 72 69 61 6c  rial type serial
1afd1 5f 74 79 70 65 0a 2a 2a 20 61 6e 64 20 73 74 6f  _type.** and sto
1afd2 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e  re the result in
1afd3 20 70 4d 65 6d 2e 20 20 52 65 74 75 72 6e 20 74   pMem.  Return t
1afd4 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
1afd5 65 73 20 72 65 61 64 2e 0a 2a 2f 20 0a 53 51 4c  es read..*/ .SQL
1afd6 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20  ITE_PRIVATE u32 
1afd7 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
1afd8 6c 47 65 74 28 0a 20 20 63 6f 6e 73 74 20 75 6e  lGet(.  const un
1afd9 73 69 67 6e 65 64 20 63 68 61 72 20 2a 62 75 66  signed char *buf
1afda 2c 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20  ,     /* Buffer 
1afdb 74 6f 20 64 65 73 65 72 69 61 6c 69 7a 65 20 66  to deserialize f
1afdc 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 73 65 72  rom */.  u32 ser
1afdd 69 61 6c 5f 74 79 70 65 2c 20 20 20 20 20 20 20  ial_type,       
1afde 20 20 20 20 20 20 20 2f 2a 20 53 65 72 69 61 6c         /* Serial
1afdf 20 74 79 70 65 20 74 6f 20 64 65 73 65 72 69 61   type to deseria
1afe0 6c 69 7a 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  lize */.  Mem *p
1afe1 4d 65 6d 20 20 20 20 20 20 20 20 20 20 20 20 20  Mem             
1afe2 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72          /* Memor
1afe3 79 20 63 65 6c 6c 20 74 6f 20 77 72 69 74 65 20  y cell to write 
1afe4 76 61 6c 75 65 20 69 6e 74 6f 20 2a 2f 0a 29 7b  value into */.){
1afe5 0a 20 20 73 77 69 74 63 68 28 20 73 65 72 69 61  .  switch( seria
1afe6 6c 5f 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61  l_type ){.    ca
1afe7 73 65 20 31 30 3a 20 20 20 2f 2a 20 52 65 73 65  se 10:   /* Rese
1afe8 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 65 20  rved for future 
1afe9 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  use */.    case 
1afea 31 31 3a 20 20 20 2f 2a 20 52 65 73 65 72 76 65  11:   /* Reserve
1afeb 64 20 66 6f 72 20 66 75 74 75 72 65 20 75 73 65  d for future use
1afec 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 30 3a 20   */.    case 0: 
1afed 7b 20 20 2f 2a 20 4e 55 4c 4c 20 2a 2f 0a 20 20  {  /* NULL */.  
1afee 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
1afef 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20  = MEM_Null;.    
1aff0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1aff1 20 20 20 63 61 73 65 20 31 3a 20 7b 20 2f 2a 20     case 1: { /* 
1aff2 31 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e  1-byte signed in
1aff3 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 70  teger */.      p
1aff4 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 73 69 67 6e  Mem->u.i = (sign
1aff5 65 64 20 63 68 61 72 29 62 75 66 5b 30 5d 3b 0a  ed char)buf[0];.
1aff6 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
1aff7 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20  s = MEM_Int;.   
1aff8 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
1aff9 20 7d 0a 20 20 20 20 63 61 73 65 20 32 3a 20 7b   }.    case 2: {
1affa 20 2f 2a 20 32 2d 62 79 74 65 20 73 69 67 6e 65   /* 2-byte signe
1affb 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20  d integer */.   
1affc 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28     pMem->u.i = (
1affd 28 28 73 69 67 6e 65 64 20 63 68 61 72 29 62 75  ((signed char)bu
1affe 66 5b 30 5d 29 3c 3c 38 29 20 7c 20 62 75 66 5b  f[0])<<8) | buf[
1afff 31 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  1];.      pMem->
1b000 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
1b001 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b  .      return 2;
1b002 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1b003 33 3a 20 7b 20 2f 2a 20 33 2d 62 79 74 65 20 73  3: { /* 3-byte s
1b004 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f  igned integer */
1b005 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69  .      pMem->u.i
1b006 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68 61   = (((signed cha
1b007 72 29 62 75 66 5b 30 5d 29 3c 3c 31 36 29 20 7c  r)buf[0])<<16) |
1b008 20 28 62 75 66 5b 31 5d 3c 3c 38 29 20 7c 20 62   (buf[1]<<8) | b
1b009 75 66 5b 32 5d 3b 0a 20 20 20 20 20 20 70 4d 65  uf[2];.      pMe
1b00a 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49  m->flags = MEM_I
1b00b 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  nt;.      return
1b00c 20 33 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   3;.    }.    ca
1b00d 73 65 20 34 3a 20 7b 20 2f 2a 20 34 2d 62 79 74  se 4: { /* 4-byt
1b00e 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  e signed integer
1b00f 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e   */.      pMem->
1b010 75 2e 69 20 3d 20 28 62 75 66 5b 30 5d 3c 3c 32  u.i = (buf[0]<<2
1b011 34 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 31 36  4) | (buf[1]<<16
1b012 29 20 7c 20 28 62 75 66 5b 32 5d 3c 3c 38 29 20  ) | (buf[2]<<8) 
1b013 7c 20 62 75 66 5b 33 5d 3b 0a 20 20 20 20 20 20  | buf[3];.      
1b014 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
1b015 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74  M_Int;.      ret
1b016 75 72 6e 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 4;.    }.   
1b017 20 63 61 73 65 20 35 3a 20 7b 20 2f 2a 20 36 2d   case 5: { /* 6-
1b018 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65  byte signed inte
1b019 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 36 34  ger */.      u64
1b01a 20 78 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63   x = (((signed c
1b01b 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 38 29 20  har)buf[0])<<8) 
1b01c 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20 20 20 20  | buf[1];.      
1b01d 75 33 32 20 79 20 3d 20 28 62 75 66 5b 32 5d 3c  u32 y = (buf[2]<
1b01e 3c 32 34 29 20 7c 20 28 62 75 66 5b 33 5d 3c 3c  <24) | (buf[3]<<
1b01f 31 36 29 20 7c 20 28 62 75 66 5b 34 5d 3c 3c 38  16) | (buf[4]<<8
1b020 29 20 7c 20 62 75 66 5b 35 5d 3b 0a 20 20 20 20  ) | buf[5];.    
1b021 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 20 7c 20    x = (x<<32) | 
1b022 79 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75  y;.      pMem->u
1b023 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b 0a  .i = *(i64*)&x;.
1b024 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
1b025 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20  s = MEM_Int;.   
1b026 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 20     return 6;.   
1b027 20 7d 0a 20 20 20 20 63 61 73 65 20 36 3a 20 20   }.    case 6:  
1b028 20 2f 2a 20 38 2d 62 79 74 65 20 73 69 67 6e 65   /* 8-byte signe
1b029 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20  d integer */.   
1b02a 20 63 61 73 65 20 37 3a 20 7b 20 2f 2a 20 49 45   case 7: { /* IE
1b02b 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  EE floating poin
1b02c 74 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 78  t */.      u64 x
1b02d 3b 0a 20 20 20 20 20 20 75 33 32 20 79 3b 0a 23  ;.      u32 y;.#
1b02e 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42  if !defined(NDEB
1b02f 55 47 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  UG) && !defined(
1b030 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
1b031 54 49 4e 47 5f 50 4f 49 4e 54 29 0a 20 20 20 20  TING_POINT).    
1b032 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74    /* Verify that
1b033 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20 66 6c   integers and fl
1b034 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c  oating point val
1b035 75 65 73 20 75 73 65 20 74 68 65 20 73 61 6d 65  ues use the same
1b036 0a 20 20 20 20 20 20 2a 2a 20 62 79 74 65 20 6f  .      ** byte o
1b037 72 64 65 72 2e 20 20 4f 72 2c 20 74 68 61 74 20  rder.  Or, that 
1b038 69 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f  if SQLITE_MIXED_
1b039 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f  ENDIAN_64BIT_FLO
1b03a 41 54 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 64  AT is.      ** d
1b03b 65 66 69 6e 65 64 20 74 68 61 74 20 36 34 2d 62  efined that 64-b
1b03c 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  it floating poin
1b03d 74 20 76 61 6c 75 65 73 20 72 65 61 6c 6c 79 20  t values really 
1b03e 61 72 65 20 6d 69 78 65 64 0a 20 20 20 20 20 20  are mixed.      
1b03f 2a 2a 20 65 6e 64 69 61 6e 2e 0a 20 20 20 20 20  ** endian..     
1b040 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69 63   */.      static
1b041 20 63 6f 6e 73 74 20 75 36 34 20 74 31 20 3d 20   const u64 t1 = 
1b042 28 28 75 36 34 29 30 78 33 66 66 30 30 30 30 30  ((u64)0x3ff00000
1b043 29 3c 3c 33 32 3b 0a 20 20 20 20 20 20 73 74 61  )<<32;.      sta
1b044 74 69 63 20 63 6f 6e 73 74 20 64 6f 75 62 6c 65  tic const double
1b045 20 72 31 20 3d 20 31 2e 30 3b 0a 20 20 20 20 20   r1 = 1.0;.     
1b046 20 75 36 34 20 74 32 20 3d 20 74 31 3b 0a 20 20   u64 t2 = t1;.  
1b047 20 20 20 20 73 77 61 70 4d 69 78 65 64 45 6e 64      swapMixedEnd
1b048 69 61 6e 46 6c 6f 61 74 28 74 32 29 3b 0a 20 20  ianFloat(t2);.  
1b049 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65      assert( size
1b04a 6f 66 28 72 31 29 3d 3d 73 69 7a 65 6f 66 28 74  of(r1)==sizeof(t
1b04b 32 29 20 26 26 20 6d 65 6d 63 6d 70 28 26 72 31  2) && memcmp(&r1
1b04c 2c 20 26 74 32 2c 20 73 69 7a 65 6f 66 28 72 31  , &t2, sizeof(r1
1b04d 29 29 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a  ))==0 );.#endif.
1b04e 0a 20 20 20 20 20 20 78 20 3d 20 28 62 75 66 5b  .      x = (buf[
1b04f 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 31  0]<<24) | (buf[1
1b050 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 32 5d  ]<<16) | (buf[2]
1b051 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20  <<8) | buf[3];. 
1b052 20 20 20 20 20 79 20 3d 20 28 62 75 66 5b 34 5d       y = (buf[4]
1b053 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 35 5d 3c  <<24) | (buf[5]<
1b054 3c 31 36 29 20 7c 20 28 62 75 66 5b 36 5d 3c 3c  <16) | (buf[6]<<
1b055 38 29 20 7c 20 62 75 66 5b 37 5d 3b 0a 20 20 20  8) | buf[7];.   
1b056 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 20 7c     x = (x<<32) |
1b057 20 79 3b 0a 20 20 20 20 20 20 69 66 28 20 73 65   y;.      if( se
1b058 72 69 61 6c 5f 74 79 70 65 3d 3d 36 20 29 7b 0a  rial_type==6 ){.
1b059 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e          pMem->u.
1b05a 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b 0a 20  i = *(i64*)&x;. 
1b05b 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61         pMem->fla
1b05c 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20  gs = MEM_Int;.  
1b05d 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1b05e 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f     assert( sizeo
1b05f 66 28 78 29 3d 3d 38 20 26 26 20 73 69 7a 65 6f  f(x)==8 && sizeo
1b060 66 28 70 4d 65 6d 2d 3e 72 29 3d 3d 38 20 29 3b  f(pMem->r)==8 );
1b061 0a 20 20 20 20 20 20 20 20 73 77 61 70 4d 69 78  .        swapMix
1b062 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 78 29  edEndianFloat(x)
1b063 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  ;.        memcpy
1b064 28 26 70 4d 65 6d 2d 3e 72 2c 20 26 78 2c 20 73  (&pMem->r, &x, s
1b065 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20 20 20  izeof(x));.     
1b066 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
1b067 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 70 4d   sqlite3IsNaN(pM
1b068 65 6d 2d 3e 72 29 20 3f 20 4d 45 4d 5f 4e 75 6c  em->r) ? MEM_Nul
1b069 6c 20 3a 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20  l : MEM_Real;.  
1b06a 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
1b06b 72 6e 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 8;.    }.    
1b06c 63 61 73 65 20 38 3a 20 20 20 20 2f 2a 20 49 6e  case 8:    /* In
1b06d 74 65 67 65 72 20 30 20 2a 2f 0a 20 20 20 20 63  teger 0 */.    c
1b06e 61 73 65 20 39 3a 20 7b 20 20 2f 2a 20 49 6e 74  ase 9: {  /* Int
1b06f 65 67 65 72 20 31 20 2a 2f 0a 20 20 20 20 20 20  eger 1 */.      
1b070 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 65 72 69  pMem->u.i = seri
1b071 61 6c 5f 74 79 70 65 2d 38 3b 0a 20 20 20 20 20  al_type-8;.     
1b072 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
1b073 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65  EM_Int;.      re
1b074 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
1b075 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
1b076 20 20 20 75 33 32 20 6c 65 6e 20 3d 20 28 73 65     u32 len = (se
1b077 72 69 61 6c 5f 74 79 70 65 2d 31 32 29 2f 32 3b  rial_type-12)/2;
1b078 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d  .      pMem->z =
1b079 20 28 63 68 61 72 20 2a 29 62 75 66 3b 0a 20 20   (char *)buf;.  
1b07a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6c 65      pMem->n = le
1b07b 6e 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 78  n;.      pMem->x
1b07c 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  Del = 0;.      i
1b07d 66 28 20 73 65 72 69 61 6c 5f 74 79 70 65 26 30  f( serial_type&0
1b07e 78 30 31 20 29 7b 0a 20 20 20 20 20 20 20 20 70  x01 ){.        p
1b07f 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
1b080 5f 53 74 72 20 7c 20 4d 45 4d 5f 45 70 68 65 6d  _Str | MEM_Ephem
1b081 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1b082 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61         pMem->fla
1b083 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20  gs = MEM_Blob | 
1b084 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20  MEM_Ephem;.     
1b085 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20   }.      return 
1b086 6c 65 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  len;.    }.  }. 
1b087 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f   return 0;.}.../
1b088 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 6e  *.** Given the n
1b089 4b 65 79 2d 62 79 74 65 20 65 6e 63 6f 64 69 6e  Key-byte encodin
1b08a 67 20 6f 66 20 61 20 72 65 63 6f 72 64 20 69 6e  g of a record in
1b08b 20 70 4b 65 79 5b 5d 2c 20 70 61 72 73 65 20 74   pKey[], parse t
1b08c 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20 69 6e 74  he.** record int
1b08d 6f 20 61 20 55 6e 70 61 63 6b 65 64 52 65 63 6f  o a UnpackedReco
1b08e 72 64 20 73 74 72 75 63 74 75 72 65 2e 20 20 52  rd structure.  R
1b08f 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
1b090 74 6f 0a 2a 2a 20 74 68 61 74 20 73 74 72 75 63  to.** that struc
1b091 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ture..**.** The 
1b092 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
1b093 20 6d 69 67 68 74 20 70 72 6f 76 69 64 65 20 73   might provide s
1b094 7a 53 70 61 63 65 20 62 79 74 65 73 20 6f 66 20  zSpace bytes of 
1b095 6d 65 6d 6f 72 79 0a 2a 2a 20 73 70 61 63 65 20  memory.** space 
1b096 61 74 20 70 53 70 61 63 65 2e 20 20 54 68 69 73  at pSpace.  This
1b097 20 73 70 61 63 65 20 63 61 6e 20 62 65 20 75 73   space can be us
1b098 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  ed to hold the r
1b099 65 74 75 72 6e 65 64 0a 2a 2a 20 56 44 62 65 50  eturned.** VDbeP
1b09a 61 72 73 65 64 52 65 63 6f 72 64 20 73 74 72 75  arsedRecord stru
1b09b 63 74 75 72 65 20 69 66 20 69 74 20 69 73 20 6c  cture if it is l
1b09c 61 72 67 65 20 65 6e 6f 75 67 68 2e 20 20 49 66  arge enough.  If
1b09d 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 62 69   it is.** not bi
1b09e 67 20 65 6e 6f 75 67 68 2c 20 73 70 61 63 65 20  g enough, space 
1b09f 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  is obtained from
1b0a0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
1b0a1 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74  )..**.** The ret
1b0a2 75 72 6e 65 64 20 73 74 72 75 63 74 75 72 65 20  urned structure 
1b0a3 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f 73 65 64  should be closed
1b0a4 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a   by a call to.**
1b0a5 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65   sqlite3VdbeDele
1b0a6 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64  teUnpackedRecord
1b0a7 28 29 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50  ()..*/ .SQLITE_P
1b0a8 52 49 56 41 54 45 20 55 6e 70 61 63 6b 65 64 52  RIVATE UnpackedR
1b0a9 65 63 6f 72 64 20 2a 73 71 6c 69 74 65 33 56 64  ecord *sqlite3Vd
1b0aa 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 0a  beRecordUnpack(.
1b0ab 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49    KeyInfo *pKeyI
1b0ac 6e 66 6f 2c 20 20 20 20 20 2f 2a 20 49 6e 66 6f  nfo,     /* Info
1b0ad 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  rmation about th
1b0ae 65 20 72 65 63 6f 72 64 20 66 6f 72 6d 61 74 20  e record format 
1b0af 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20  */.  int nKey,  
1b0b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
1b0b1 69 7a 65 20 6f 66 20 74 68 65 20 62 69 6e 61 72  ize of the binar
1b0b2 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 6f  y record */.  co
1b0b3 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
1b0b4 20 20 20 20 20 2f 2a 20 54 68 65 20 62 69 6e 61       /* The bina
1b0b5 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63  ry record */.  c
1b0b6 68 61 72 20 2a 70 53 70 61 63 65 2c 20 20 20 20  har *pSpace,    
1b0b7 20 20 20 20 20 20 2f 2a 20 55 6e 61 6c 69 67 6e        /* Unalign
1b0b8 65 64 20 73 70 61 63 65 20 61 76 61 69 6c 61 62  ed space availab
1b0b9 6c 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6f  le to hold the o
1b0ba 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 73  bject */.  int s
1b0bb 7a 53 70 61 63 65 20 20 20 20 20 20 20 20 20 20  zSpace          
1b0bc 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 53 70    /* Size of pSp
1b0bd 61 63 65 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a  ace[] in bytes *
1b0be 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73  /.){.  const uns
1b0bf 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 79  igned char *aKey
1b0c0 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e   = (const unsign
1b0c1 65 64 20 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a  ed char *)pKey;.
1b0c2 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
1b0c3 20 2a 70 3b 20 20 2f 2a 20 54 68 65 20 75 6e 70   *p;  /* The unp
1b0c4 61 63 6b 65 64 20 72 65 63 6f 72 64 20 74 68 61  acked record tha
1b0c5 74 20 77 65 20 77 69 6c 6c 20 72 65 74 75 72 6e  t we will return
1b0c6 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b   */.  int nByte;
1b0c7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d            /* Mem
1b0c8 6f 72 79 20 73 70 61 63 65 20 6e 65 65 64 65 64  ory space needed
1b0c9 20 74 6f 20 68 6f 6c 64 20 70 2c 20 69 6e 20 62   to hold p, in b
1b0ca 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 64 3b  ytes */.  int d;
1b0cb 0a 20 20 75 33 32 20 69 64 78 3b 0a 20 20 75 31  .  u32 idx;.  u1
1b0cc 36 20 75 3b 20 20 20 20 20 20 20 20 20 20 20 20  6 u;            
1b0cd 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64 20 6c 6f    /* Unsigned lo
1b0ce 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
1b0cf 75 33 32 20 73 7a 48 64 72 3b 0a 20 20 4d 65 6d  u32 szHdr;.  Mem
1b0d0 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e 74 20 6e 4f   *pMem;.  int nO
1b0d1 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ff;           /*
1b0d2 20 49 6e 63 72 65 61 73 65 20 70 53 70 61 63 65   Increase pSpace
1b0d3 20 62 79 20 74 68 69 73 20 6d 75 63 68 20 74 6f   by this much to
1b0d4 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 20 69 74   8-byte align it
1b0d5 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a   */.  .  /*.  **
1b0d6 20 57 65 20 77 61 6e 74 20 74 6f 20 73 68 69 66   We want to shif
1b0d7 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 70 53  t the pointer pS
1b0d8 70 61 63 65 20 75 70 20 73 75 63 68 20 74 68 61  pace up such tha
1b0d9 74 20 69 74 20 69 73 20 38 2d 62 79 74 65 20 61  t it is 8-byte a
1b0da 6c 69 67 6e 65 64 2e 0a 20 20 2a 2a 20 54 68 75  ligned..  ** Thu
1b0db 73 2c 20 77 65 20 6e 65 65 64 20 74 6f 20 63 61  s, we need to ca
1b0dc 6c 63 75 6c 61 74 65 20 61 20 76 61 6c 75 65 2c  lculate a value,
1b0dd 20 6e 4f 66 66 2c 20 62 65 74 77 65 65 6e 20 30   nOff, between 0
1b0de 20 61 6e 64 20 37 2c 20 74 6f 20 73 68 69 66 74   and 7, to shift
1b0df 20 0a 20 20 2a 2a 20 69 74 20 62 79 2e 20 20 49   .  ** it by.  I
1b0e0 66 20 70 53 70 61 63 65 20 69 73 20 61 6c 72 65  f pSpace is alre
1b0e1 61 64 79 20 38 2d 62 79 74 65 20 61 6c 69 67 6e  ady 8-byte align
1b0e2 65 64 2c 20 6e 4f 66 66 20 73 68 6f 75 6c 64 20  ed, nOff should 
1b0e3 62 65 20 7a 65 72 6f 2e 0a 20 20 2a 2f 0a 20 20  be zero..  */.  
1b0e4 6e 4f 66 66 20 3d 20 28 38 20 2d 20 28 53 51 4c  nOff = (8 - (SQL
1b0e5 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70  ITE_PTR_TO_INT(p
1b0e6 53 70 61 63 65 29 20 26 20 37 29 29 20 26 20 37  Space) & 7)) & 7
1b0e7 3b 0a 20 20 70 53 70 61 63 65 20 2b 3d 20 6e 4f  ;.  pSpace += nO
1b0e8 66 66 3b 0a 20 20 73 7a 53 70 61 63 65 20 2d 3d  ff;.  szSpace -=
1b0e9 20 6e 4f 66 66 3b 0a 20 20 6e 42 79 74 65 20 3d   nOff;.  nByte =
1b0ea 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 55   ROUND8(sizeof(U
1b0eb 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 29 29 20  npackedRecord)) 
1b0ec 2b 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 28 70  + sizeof(Mem)*(p
1b0ed 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 2b  KeyInfo->nField+
1b0ee 31 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3e  1);.  if( nByte>
1b0ef 73 7a 53 70 61 63 65 20 29 7b 0a 20 20 20 20 70  szSpace ){.    p
1b0f0 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
1b0f1 6f 63 52 61 77 28 70 4b 65 79 49 6e 66 6f 2d 3e  ocRaw(pKeyInfo->
1b0f2 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20  db, nByte);.    
1b0f3 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
1b0f4 6e 20 30 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67  n 0;.    p->flag
1b0f5 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45  s = UNPACKED_NEE
1b0f6 44 5f 46 52 45 45 20 7c 20 55 4e 50 41 43 4b 45  D_FREE | UNPACKE
1b0f7 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 3b 0a  D_NEED_DESTROY;.
1b0f8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d    }else{.    p =
1b0f9 20 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64   (UnpackedRecord
1b0fa 2a 29 70 53 70 61 63 65 3b 0a 20 20 20 20 70 2d  *)pSpace;.    p-
1b0fb 3e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45  >flags = UNPACKE
1b0fc 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 3b 0a  D_NEED_DESTROY;.
1b0fd 20 20 7d 0a 20 20 70 2d 3e 70 4b 65 79 49 6e 66    }.  p->pKeyInf
1b0fe 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  o = pKeyInfo;.  
1b0ff 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 70 4b 65 79  p->nField = pKey
1b100 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 31  Info->nField + 1
1b101 3b 0a 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 70 4d  ;.  p->aMem = pM
1b102 65 6d 20 3d 20 28 4d 65 6d 2a 29 26 28 28 63 68  em = (Mem*)&((ch
1b103 61 72 2a 29 70 29 5b 52 4f 55 4e 44 38 28 73 69  ar*)p)[ROUND8(si
1b104 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 65 63  zeof(UnpackedRec
1b105 6f 72 64 29 29 5d 3b 0a 20 20 61 73 73 65 72 74  ord))];.  assert
1b106 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49  ( EIGHT_BYTE_ALI
1b107 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a  GNMENT(pMem) );.
1b108 20 20 69 64 78 20 3d 20 67 65 74 56 61 72 69 6e    idx = getVarin
1b109 74 33 32 28 61 4b 65 79 2c 20 73 7a 48 64 72 29  t32(aKey, szHdr)
1b10a 3b 0a 20 20 64 20 3d 20 73 7a 48 64 72 3b 0a 20  ;.  d = szHdr;. 
1b10b 20 75 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28   u = 0;.  while(
1b10c 20 69 64 78 3c 73 7a 48 64 72 20 26 26 20 75 3c   idx<szHdr && u<
1b10d 70 2d 3e 6e 46 69 65 6c 64 20 26 26 20 64 3c 3d  p->nField && d<=
1b10e 6e 4b 65 79 20 29 7b 0a 20 20 20 20 75 33 32 20  nKey ){.    u32 
1b10f 73 65 72 69 61 6c 5f 74 79 70 65 3b 0a 0a 20 20  serial_type;..  
1b110 20 20 69 64 78 20 2b 3d 20 67 65 74 56 61 72 69    idx += getVari
1b111 6e 74 33 32 28 26 61 4b 65 79 5b 69 64 78 5d 2c  nt32(&aKey[idx],
1b112 20 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20   serial_type);. 
1b113 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 70     pMem->enc = p
1b114 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20 20  KeyInfo->enc;.  
1b115 20 20 70 4d 65 6d 2d 3e 64 62 20 3d 20 70 4b 65    pMem->db = pKe
1b116 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 20 20 70  yInfo->db;.    p
1b117 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 30 3b 0a  Mem->flags = 0;.
1b118 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f      pMem->zMallo
1b119 63 20 3d 20 30 3b 0a 20 20 20 20 64 20 2b 3d 20  c = 0;.    d += 
1b11a 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
1b11b 6c 47 65 74 28 26 61 4b 65 79 5b 64 5d 2c 20 73  lGet(&aKey[d], s
1b11c 65 72 69 61 6c 5f 74 79 70 65 2c 20 70 4d 65 6d  erial_type, pMem
1b11d 29 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20  );.    pMem++;. 
1b11e 20 20 20 75 2b 2b 3b 0a 20 20 7d 0a 20 20 61 73     u++;.  }.  as
1b11f 73 65 72 74 28 20 75 3c 3d 70 4b 65 79 49 6e 66  sert( u<=pKeyInf
1b120 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 20 29 3b  o->nField + 1 );
1b121 0a 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 75  .  p->nField = u
1b122 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64  ;.  return (void
1b123 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  *)p;.}../*.** Th
1b124 69 73 20 72 6f 75 74 69 6e 65 20 64 65 73 74 72  is routine destr
1b125 6f 79 73 20 61 20 55 6e 70 61 63 6b 65 64 52 65  oys a UnpackedRe
1b126 63 6f 72 64 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a  cord object..*/.
1b127 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1b128 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44  oid sqlite3VdbeD
1b129 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63  eleteUnpackedRec
1b12a 6f 72 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f  ord(UnpackedReco
1b12b 72 64 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b  rd *p){.  int i;
1b12c 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 0a 20  .  Mem *pMem;.. 
1b12d 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b   assert( p!=0 );
1b12e 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 66 6c  .  assert( p->fl
1b12f 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 4e  ags & UNPACKED_N
1b130 45 45 44 5f 44 45 53 54 52 4f 59 20 29 3b 0a 20  EED_DESTROY );. 
1b131 20 66 6f 72 28 69 3d 30 2c 20 70 4d 65 6d 3d 70   for(i=0, pMem=p
1b132 2d 3e 61 4d 65 6d 3b 20 69 3c 70 2d 3e 6e 46 69  ->aMem; i<p->nFi
1b133 65 6c 64 3b 20 69 2b 2b 2c 20 70 4d 65 6d 2b 2b  eld; i++, pMem++
1b134 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 75 6e  ){.    /* The un
1b135 70 61 63 6b 65 64 20 72 65 63 6f 72 64 20 69 73  packed record is
1b136 20 61 6c 77 61 79 73 20 63 6f 6e 73 74 72 75 63   always construc
1b137 74 65 64 20 62 79 20 74 68 65 0a 20 20 20 20 2a  ted by the.    *
1b138 2a 20 73 71 6c 69 74 65 33 56 64 62 65 55 6e 70  * sqlite3VdbeUnp
1b139 61 63 6b 52 65 63 6f 72 64 28 29 20 66 75 6e 63  ackRecord() func
1b13a 74 69 6f 6e 20 61 62 6f 76 65 2c 20 77 68 69 63  tion above, whic
1b13b 68 20 6d 61 6b 65 73 20 61 6c 6c 0a 20 20 20 20  h makes all.    
1b13c 2a 2a 20 73 74 72 69 6e 67 73 20 61 6e 64 20 62  ** strings and b
1b13d 6c 6f 62 73 20 73 74 61 74 69 63 2e 20 20 41 6e  lobs static.  An
1b13e 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 65 6c  d none of the el
1b13f 65 6d 65 6e 74 73 20 61 72 65 0a 20 20 20 20 2a  ements are.    *
1b140 2a 20 65 76 65 72 20 74 72 61 6e 73 66 6f 72 6d  * ever transform
1b141 65 64 2c 20 73 6f 20 74 68 65 72 65 20 69 73 20  ed, so there is 
1b142 6e 65 76 65 72 20 61 6e 79 74 68 69 6e 67 20 74  never anything t
1b143 6f 20 64 65 6c 65 74 65 2e 0a 20 20 20 20 2a 2f  o delete..    */
1b144 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 70  .    if( NEVER(p
1b145 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 20 29 20  Mem->zMalloc) ) 
1b146 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
1b147 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 7d  lease(pMem);.  }
1b148 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20  .  if( p->flags 
1b149 26 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f  & UNPACKED_NEED_
1b14a 46 52 45 45 20 29 7b 0a 20 20 20 20 73 71 6c 69  FREE ){.    sqli
1b14b 74 65 33 44 62 46 72 65 65 28 70 2d 3e 70 4b 65  te3DbFree(p->pKe
1b14c 79 49 6e 66 6f 2d 3e 64 62 2c 20 70 29 3b 0a 20  yInfo->db, p);. 
1b14d 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
1b14e 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 70 61 72   function compar
1b14f 65 73 20 74 68 65 20 74 77 6f 20 74 61 62 6c 65  es the two table
1b150 20 72 6f 77 73 20 6f 72 20 69 6e 64 65 78 20 72   rows or index r
1b151 65 63 6f 72 64 73 0a 2a 2a 20 73 70 65 63 69 66  ecords.** specif
1b152 69 65 64 20 62 79 20 7b 6e 4b 65 79 31 2c 20 70  ied by {nKey1, p
1b153 4b 65 79 31 7d 20 61 6e 64 20 70 50 4b 65 79 32  Key1} and pPKey2
1b154 2e 20 20 49 74 20 72 65 74 75 72 6e 73 20 61 20  .  It returns a 
1b155 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 0a 2a  negative, zero.*
1b156 2a 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 6e  * or positive in
1b157 74 65 67 65 72 20 69 66 20 6b 65 79 31 20 69 73  teger if key1 is
1b158 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61   less than, equa
1b159 6c 20 74 6f 20 6f 72 20 0a 2a 2a 20 67 72 65 61  l to or .** grea
1b15a 74 65 72 20 74 68 61 6e 20 6b 65 79 32 2e 20 20  ter than key2.  
1b15b 54 68 65 20 7b 6e 4b 65 79 31 2c 20 70 4b 65 79  The {nKey1, pKey
1b15c 31 7d 20 6b 65 79 20 6d 75 73 74 20 62 65 20 61  1} key must be a
1b15d 20 62 6c 6f 62 0a 2a 2a 20 63 72 65 61 74 65 64   blob.** created
1b15e 20 62 79 20 74 68 20 4f 50 5f 4d 61 6b 65 52 65   by th OP_MakeRe
1b15f 63 6f 72 64 20 6f 70 63 6f 64 65 20 6f 66 20 74  cord opcode of t
1b160 68 65 20 56 44 42 45 2e 20 20 54 68 65 20 70 50  he VDBE.  The pP
1b161 4b 65 79 32 0a 2a 2a 20 6b 65 79 20 6d 75 73 74  Key2.** key must
1b162 20 62 65 20 61 20 70 61 72 73 65 64 20 6b 65 79   be a parsed key
1b163 20 73 75 63 68 20 61 73 20 6f 62 74 61 69 6e 65   such as obtaine
1b164 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65  d from.** sqlite
1b165 33 56 64 62 65 50 61 72 73 65 52 65 63 6f 72 64  3VdbeParseRecord
1b166 2e 0a 2a 2a 0a 2a 2a 20 4b 65 79 31 20 61 6e 64  ..**.** Key1 and
1b167 20 4b 65 79 32 20 64 6f 20 6e 6f 74 20 68 61 76   Key2 do not hav
1b168 65 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65  e to contain the
1b169 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20   same number of 
1b16a 66 69 65 6c 64 73 2e 0a 2a 2a 20 54 68 65 20 6b  fields..** The k
1b16b 65 79 20 77 69 74 68 20 66 65 77 65 72 20 66 69  ey with fewer fi
1b16c 65 6c 64 73 20 69 73 20 75 73 75 61 6c 6c 79 20  elds is usually 
1b16d 63 6f 6d 70 61 72 65 73 20 6c 65 73 73 20 74 68  compares less th
1b16e 61 6e 20 74 68 65 20 0a 2a 2a 20 6c 6f 6e 67 65  an the .** longe
1b16f 72 20 6b 65 79 2e 20 20 48 6f 77 65 76 65 72 20  r key.  However 
1b170 69 66 20 74 68 65 20 55 4e 50 41 43 4b 45 44 5f  if the UNPACKED_
1b171 49 4e 43 52 4b 45 59 20 66 6c 61 67 73 20 69 6e  INCRKEY flags in
1b172 20 70 50 4b 65 79 32 20 69 73 20 73 65 74 0a 2a   pPKey2 is set.*
1b173 2a 20 61 6e 64 20 74 68 65 20 63 6f 6d 6d 6f 6e  * and the common
1b174 20 70 72 65 66 69 78 65 73 20 61 72 65 20 65 71   prefixes are eq
1b175 75 61 6c 2c 20 74 68 65 6e 20 6b 65 79 31 20 69  ual, then key1 i
1b176 73 20 6c 65 73 73 20 74 68 61 6e 20 6b 65 79 32  s less than key2
1b177 2e 0a 2a 2a 20 4f 72 20 69 66 20 74 68 65 20 55  ..** Or if the U
1b178 4e 50 41 43 4b 45 44 5f 4d 41 54 43 48 5f 50 52  NPACKED_MATCH_PR
1b179 45 46 49 58 20 66 6c 61 67 20 69 73 20 73 65 74  EFIX flag is set
1b17a 20 61 6e 64 20 74 68 65 20 70 72 65 66 69 78 65   and the prefixe
1b17b 73 20 61 72 65 0a 2a 2a 20 65 71 75 61 6c 2c 20  s are.** equal, 
1b17c 74 68 65 6e 20 74 68 65 20 6b 65 79 73 20 61 72  then the keys ar
1b17d 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20  e considered to 
1b17e 62 65 20 65 71 75 61 6c 20 61 6e 64 0a 2a 2a 20  be equal and.** 
1b17f 74 68 65 20 70 61 72 74 73 20 62 65 79 6f 6e 64  the parts beyond
1b180 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66   the common pref
1b181 69 78 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a  ix are ignored..
1b182 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 55 4e 50  **.** If the UNP
1b183 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57  ACKED_IGNORE_ROW
1b184 49 44 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20  ID flag is set, 
1b185 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20 62 79  then the last by
1b186 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20 68 65 61  te of.** the hea
1b187 64 65 72 20 6f 66 20 70 4b 65 79 31 20 69 73 20  der of pKey1 is 
1b188 69 67 6e 6f 72 65 64 2e 20 20 49 74 20 69 73 20  ignored.  It is 
1b189 61 73 73 75 6d 65 64 20 74 68 61 74 20 70 4b 65  assumed that pKe
1b18a 79 31 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 64 65  y1 is.** an inde
1b18b 78 20 6b 65 79 2c 20 61 6e 64 20 74 68 75 73 20  x key, and thus 
1b18c 65 6e 64 73 20 77 69 74 68 20 61 20 72 6f 77 69  ends with a rowi
1b18d 64 20 76 61 6c 75 65 2e 20 20 54 68 65 20 6c 61  d value.  The la
1b18e 73 74 20 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68  st byte.** of th
1b18f 65 20 68 65 61 64 65 72 20 77 69 6c 6c 20 74 68  e header will th
1b190 65 72 65 66 6f 72 65 20 62 65 20 74 68 65 20 73  erefore be the s
1b191 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68  erial type of th
1b192 65 20 72 6f 77 69 64 3a 0a 2a 2a 20 6f 6e 65 20  e rowid:.** one 
1b193 6f 66 20 31 2c 20 32 2c 20 33 2c 20 34 2c 20 35  of 1, 2, 3, 4, 5
1b194 2c 20 36 2c 20 38 2c 20 6f 72 20 39 20 2d 20 74  , 6, 8, or 9 - t
1b195 68 65 20 69 6e 74 65 67 65 72 20 73 65 72 69 61  he integer seria
1b196 6c 20 74 79 70 65 73 2e 0a 2a 2a 20 54 68 65 20  l types..** The 
1b197 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74  serial type of t
1b198 68 65 20 66 69 6e 61 6c 20 72 6f 77 69 64 20 77  he final rowid w
1b199 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 61 20  ill always be a 
1b19a 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a 2a 20  single byte..** 
1b19b 42 79 20 69 67 6e 6f 72 69 6e 67 20 74 68 69 73  By ignoring this
1b19c 20 6c 61 73 74 20 62 79 74 65 20 6f 66 20 74 68   last byte of th
1b19d 65 20 68 65 61 64 65 72 2c 20 77 65 20 66 6f 72  e header, we for
1b19e 63 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  ce the compariso
1b19f 6e 0a 2a 2a 20 74 6f 20 69 67 6e 6f 72 65 20 74  n.** to ignore t
1b1a0 68 65 20 72 6f 77 69 64 20 61 74 20 74 68 65 20  he rowid at the 
1b1a1 65 6e 64 20 6f 66 20 6b 65 79 31 2e 0a 2a 2f 0a  end of key1..*/.
1b1a2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1b1a3 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  nt sqlite3VdbeRe
1b1a4 63 6f 72 64 43 6f 6d 70 61 72 65 28 0a 20 20 69  cordCompare(.  i
1b1a5 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 74 20  nt nKey1, const 
1b1a6 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 2f 2a 20  void *pKey1, /* 
1b1a7 4c 65 66 74 20 6b 65 79 20 2a 2f 0a 20 20 55 6e  Left key */.  Un
1b1a8 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 50  packedRecord *pP
1b1a9 4b 65 79 32 20 20 20 20 20 20 20 20 2f 2a 20 52  Key2        /* R
1b1aa 69 67 68 74 20 6b 65 79 20 2a 2f 0a 29 7b 0a 20  ight key */.){. 
1b1ab 20 69 6e 74 20 64 31 3b 20 20 20 20 20 20 20 20   int d1;        
1b1ac 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
1b1ad 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 78  to aKey[] of nex
1b1ae 74 20 64 61 74 61 20 65 6c 65 6d 65 6e 74 20 2a  t data element *
1b1af 2f 0a 20 20 75 33 32 20 69 64 78 31 3b 20 20 20  /.  u32 idx1;   
1b1b0 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
1b1b1 20 69 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20   into aKey[] of 
1b1b2 6e 65 78 74 20 68 65 61 64 65 72 20 65 6c 65 6d  next header elem
1b1b3 65 6e 74 20 2a 2f 0a 20 20 75 33 32 20 73 7a 48  ent */.  u32 szH
1b1b4 64 72 31 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  dr1;        /* N
1b1b5 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
1b1b6 6e 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e  n header */.  in
1b1b7 74 20 69 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e  t i = 0;.  int n
1b1b8 46 69 65 6c 64 3b 0a 20 20 69 6e 74 20 72 63 20  Field;.  int rc 
1b1b9 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73  = 0;.  const uns
1b1ba 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 79  igned char *aKey
1b1bb 31 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67  1 = (const unsig
1b1bc 6e 65 64 20 63 68 61 72 20 2a 29 70 4b 65 79 31  ned char *)pKey1
1b1bd 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65  ;.  KeyInfo *pKe
1b1be 79 49 6e 66 6f 3b 0a 20 20 4d 65 6d 20 6d 65 6d  yInfo;.  Mem mem
1b1bf 31 3b 0a 0a 20 20 70 4b 65 79 49 6e 66 6f 20 3d  1;..  pKeyInfo =
1b1c0 20 70 50 4b 65 79 32 2d 3e 70 4b 65 79 49 6e 66   pPKey2->pKeyInf
1b1c1 6f 3b 0a 20 20 6d 65 6d 31 2e 65 6e 63 20 3d 20  o;.  mem1.enc = 
1b1c2 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20  pKeyInfo->enc;. 
1b1c3 20 6d 65 6d 31 2e 64 62 20 3d 20 70 4b 65 79 49   mem1.db = pKeyI
1b1c4 6e 66 6f 2d 3e 64 62 3b 0a 20 20 6d 65 6d 31 2e  nfo->db;.  mem1.
1b1c5 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 6d 65 6d  flags = 0;.  mem
1b1c6 31 2e 75 2e 69 20 3d 20 30 3b 20 20 2f 2a 20 6e  1.u.i = 0;  /* n
1b1c7 6f 74 20 6e 65 65 64 65 64 2c 20 68 65 72 65 20  ot needed, here 
1b1c8 74 6f 20 73 69 6c 65 6e 63 65 20 63 6f 6d 70 69  to silence compi
1b1c9 6c 65 72 20 77 61 72 6e 69 6e 67 20 2a 2f 0a 20  ler warning */. 
1b1ca 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 20 3d 20   mem1.zMalloc = 
1b1cb 30 3b 0a 20 20 0a 20 20 69 64 78 31 20 3d 20 67  0;.  .  idx1 = g
1b1cc 65 74 56 61 72 69 6e 74 33 32 28 61 4b 65 79 31  etVarint32(aKey1
1b1cd 2c 20 73 7a 48 64 72 31 29 3b 0a 20 20 64 31 20  , szHdr1);.  d1 
1b1ce 3d 20 73 7a 48 64 72 31 3b 0a 20 20 69 66 28 20  = szHdr1;.  if( 
1b1cf 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 20  pPKey2->flags & 
1b1d0 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f  UNPACKED_IGNORE_
1b1d1 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 73 7a 48  ROWID ){.    szH
1b1d2 64 72 31 2d 2d 3b 0a 20 20 7d 0a 20 20 6e 46 69  dr1--;.  }.  nFi
1b1d3 65 6c 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e  eld = pKeyInfo->
1b1d4 6e 46 69 65 6c 64 3b 0a 20 20 77 68 69 6c 65 28  nField;.  while(
1b1d5 20 69 64 78 31 3c 73 7a 48 64 72 31 20 26 26 20   idx1<szHdr1 && 
1b1d6 69 3c 70 50 4b 65 79 32 2d 3e 6e 46 69 65 6c 64  i<pPKey2->nField
1b1d7 20 29 7b 0a 20 20 20 20 75 33 32 20 73 65 72 69   ){.    u32 seri
1b1d8 61 6c 5f 74 79 70 65 31 3b 0a 0a 20 20 20 20 2f  al_type1;..    /
1b1d9 2a 20 52 65 61 64 20 74 68 65 20 73 65 72 69 61  * Read the seria
1b1da 6c 20 74 79 70 65 73 20 66 6f 72 20 74 68 65 20  l types for the 
1b1db 6e 65 78 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20  next element in 
1b1dc 65 61 63 68 20 6b 65 79 2e 20 2a 2f 0a 20 20 20  each key. */.   
1b1dd 20 69 64 78 31 20 2b 3d 20 67 65 74 56 61 72 69   idx1 += getVari
1b1de 6e 74 33 32 28 20 61 4b 65 79 31 2b 69 64 78 31  nt32( aKey1+idx1
1b1df 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 31 20 29  , serial_type1 )
1b1e0 3b 0a 20 20 20 20 69 66 28 20 64 31 3e 3d 6e 4b  ;.    if( d1>=nK
1b1e1 65 79 31 20 26 26 20 73 71 6c 69 74 65 33 56 64  ey1 && sqlite3Vd
1b1e2 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28  beSerialTypeLen(
1b1e3 73 65 72 69 61 6c 5f 74 79 70 65 31 29 3e 30 20  serial_type1)>0 
1b1e4 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 2f 2a  ) break;..    /*
1b1e5 20 45 78 74 72 61 63 74 20 74 68 65 20 76 61 6c   Extract the val
1b1e6 75 65 73 20 74 6f 20 62 65 20 63 6f 6d 70 61 72  ues to be compar
1b1e7 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64  ed..    */.    d
1b1e8 31 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65  1 += sqlite3Vdbe
1b1e9 53 65 72 69 61 6c 47 65 74 28 26 61 4b 65 79 31  SerialGet(&aKey1
1b1ea 5b 64 31 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70  [d1], serial_typ
1b1eb 65 31 2c 20 26 6d 65 6d 31 29 3b 0a 0a 20 20 20  e1, &mem1);..   
1b1ec 20 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d 70 61   /* Do the compa
1b1ed 72 69 73 6f 6e 0a 20 20 20 20 2a 2f 0a 20 20 20  rison.    */.   
1b1ee 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d   rc = sqlite3Mem
1b1ef 43 6f 6d 70 61 72 65 28 26 6d 65 6d 31 2c 20 26  Compare(&mem1, &
1b1f0 70 50 4b 65 79 32 2d 3e 61 4d 65 6d 5b 69 5d 2c  pPKey2->aMem[i],
1b1f1 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1b1f2 20 20 20 20 20 20 20 20 20 20 20 20 69 3c 6e 46              i<nF
1b1f3 69 65 6c 64 20 3f 20 70 4b 65 79 49 6e 66 6f 2d  ield ? pKeyInfo-
1b1f4 3e 61 43 6f 6c 6c 5b 69 5d 20 3a 20 30 29 3b 0a  >aColl[i] : 0);.
1b1f5 20 20 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b      if( rc!=0 ){
1b1f6 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1b1f7 20 20 7d 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d    }.    i++;.  }
1b1f8 0a 0a 20 20 2f 2a 20 4e 6f 20 6d 65 6d 6f 72 79  ..  /* No memory
1b1f9 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 65   allocation is e
1b1fa 76 65 72 20 75 73 65 64 20 6f 6e 20 6d 65 6d 31  ver used on mem1
1b1fb 2e 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52  . */.  if( NEVER
1b1fc 28 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 29 20 29  (mem1.zMalloc) )
1b1fd 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
1b1fe 65 6c 65 61 73 65 28 26 6d 65 6d 31 29 3b 0a 0a  elease(&mem1);..
1b1ff 20 20 2f 2a 20 49 66 20 74 68 65 20 50 52 45 46    /* If the PREF
1b200 49 58 5f 53 45 41 52 43 48 20 66 6c 61 67 20 69  IX_SEARCH flag i
1b201 73 20 73 65 74 20 61 6e 64 20 61 6c 6c 20 66 69  s set and all fi
1b202 65 6c 64 73 20 65 78 63 65 70 74 20 74 68 65 20  elds except the 
1b203 66 69 6e 61 6c 0a 20 20 2a 2a 20 72 6f 77 69 64  final.  ** rowid
1b204 20 66 69 65 6c 64 20 77 65 72 65 20 65 71 75 61   field were equa
1b205 6c 2c 20 74 68 65 6e 20 63 6c 65 61 72 20 74 68  l, then clear th
1b206 65 20 50 52 45 46 49 58 5f 53 45 41 52 43 48 20  e PREFIX_SEARCH 
1b207 66 6c 61 67 20 61 6e 64 20 73 65 74 20 0a 20 20  flag and set .  
1b208 2a 2a 20 70 50 4b 65 79 32 2d 3e 72 6f 77 69 64  ** pPKey2->rowid
1b209 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66   to the value of
1b20a 20 74 68 65 20 72 6f 77 69 64 20 66 69 65 6c 64   the rowid field
1b20b 20 69 6e 20 28 70 4b 65 79 31 2c 20 6e 4b 65 79   in (pKey1, nKey
1b20c 31 29 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 73  1)..  ** This is
1b20d 20 75 73 65 64 20 62 79 20 74 68 65 20 4f 50 5f   used by the OP_
1b20e 49 73 55 6e 69 71 75 65 20 6f 70 63 6f 64 65 2e  IsUnique opcode.
1b20f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 50 4b  .  */.  if( (pPK
1b210 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50  ey2->flags & UNP
1b211 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41  ACKED_PREFIX_SEA
1b212 52 43 48 29 20 26 26 20 69 3d 3d 28 70 50 4b 65  RCH) && i==(pPKe
1b213 79 32 2d 3e 6e 46 69 65 6c 64 2d 31 29 20 29 7b  y2->nField-1) ){
1b214 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78  .    assert( idx
1b215 31 3d 3d 73 7a 48 64 72 31 20 26 26 20 72 63 20  1==szHdr1 && rc 
1b216 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d  );.    assert( m
1b217 65 6d 31 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em1.flags & MEM_
1b218 49 6e 74 20 29 3b 0a 20 20 20 20 70 50 4b 65 79  Int );.    pPKey
1b219 32 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 55 4e 50  2->flags &= ~UNP
1b21a 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41  ACKED_PREFIX_SEA
1b21b 52 43 48 3b 0a 20 20 20 20 70 50 4b 65 79 32 2d  RCH;.    pPKey2-
1b21c 3e 72 6f 77 69 64 20 3d 20 6d 65 6d 31 2e 75 2e  >rowid = mem1.u.
1b21d 69 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63  i;.  }..  if( rc
1b21e 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 72 63  ==0 ){.    /* rc
1b21f 3d 3d 30 20 68 65 72 65 20 6d 65 61 6e 73 20 74  ==0 here means t
1b220 68 61 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 6b  hat one of the k
1b221 65 79 73 20 72 61 6e 20 6f 75 74 20 6f 66 20 66  eys ran out of f
1b222 69 65 6c 64 73 20 61 6e 64 0a 20 20 20 20 2a 2a  ields and.    **
1b223 20 61 6c 6c 20 74 68 65 20 66 69 65 6c 64 73 20   all the fields 
1b224 75 70 20 74 6f 20 74 68 61 74 20 70 6f 69 6e 74  up to that point
1b225 20 77 65 72 65 20 65 71 75 61 6c 2e 20 49 66 20   were equal. If 
1b226 74 68 65 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43  the UNPACKED_INC
1b227 52 4b 45 59 0a 20 20 20 20 2a 2a 20 66 6c 61 67  RKEY.    ** flag
1b228 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 62 72   is set, then br
1b229 65 61 6b 20 74 68 65 20 74 69 65 20 62 79 20 74  eak the tie by t
1b22a 72 65 61 74 69 6e 67 20 6b 65 79 32 20 61 73 20  reating key2 as 
1b22b 6c 61 72 67 65 72 2e 0a 20 20 20 20 2a 2a 20 49  larger..    ** I
1b22c 66 20 74 68 65 20 55 50 41 43 4b 45 44 5f 50 52  f the UPACKED_PR
1b22d 45 46 49 58 5f 4d 41 54 43 48 20 66 6c 61 67 20  EFIX_MATCH flag 
1b22e 69 73 20 73 65 74 2c 20 74 68 65 6e 20 6b 65 79  is set, then key
1b22f 73 20 77 69 74 68 20 63 6f 6d 6d 6f 6e 20 70 72  s with common pr
1b230 65 66 69 78 65 73 0a 20 20 20 20 2a 2a 20 61 72  efixes.    ** ar
1b231 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20  e considered to 
1b232 62 65 20 65 71 75 61 6c 2e 20 20 4f 74 68 65 72  be equal.  Other
1b233 77 69 73 65 2c 20 74 68 65 20 6c 6f 6e 67 65 72  wise, the longer
1b234 20 6b 65 79 20 69 73 20 74 68 65 20 0a 20 20 20   key is the .   
1b235 20 2a 2a 20 6c 61 72 67 65 72 2e 20 20 41 73 20   ** larger.  As 
1b236 69 74 20 68 61 70 70 65 6e 73 2c 20 74 68 65 20  it happens, the 
1b237 70 50 4b 65 79 32 20 77 69 6c 6c 20 61 6c 77 61  pPKey2 will alwa
1b238 79 73 20 62 65 20 74 68 65 20 6c 6f 6e 67 65 72  ys be the longer
1b239 0a 20 20 20 20 2a 2a 20 69 66 20 74 68 65 72 65  .    ** if there
1b23a 20 69 73 20 61 20 64 69 66 66 65 72 65 6e 63 65   is a difference
1b23b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1b23c 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26   pPKey2->flags &
1b23d 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45   UNPACKED_INCRKE
1b23e 59 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  Y ){.      rc = 
1b23f 2d 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  -1;.    }else if
1b240 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20  ( pPKey2->flags 
1b241 26 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49  & UNPACKED_PREFI
1b242 58 5f 4d 41 54 43 48 20 29 7b 0a 20 20 20 20 20  X_MATCH ){.     
1b243 20 2f 2a 20 4c 65 61 76 65 20 72 63 3d 3d 30 20   /* Leave rc==0 
1b244 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  */.    }else if(
1b245 20 69 64 78 31 3c 73 7a 48 64 72 31 20 29 7b 0a   idx1<szHdr1 ){.
1b246 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20        rc = 1;.  
1b247 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
1b248 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f  pKeyInfo->aSortO
1b249 72 64 65 72 20 26 26 20 69 3c 70 4b 65 79 49 6e  rder && i<pKeyIn
1b24a 66 6f 2d 3e 6e 46 69 65 6c 64 0a 20 20 20 20 20  fo->nField.     
1b24b 20 20 20 20 20 20 20 20 20 20 26 26 20 70 4b 65            && pKe
1b24c 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65  yInfo->aSortOrde
1b24d 72 5b 69 5d 20 29 7b 0a 20 20 20 20 72 63 20 3d  r[i] ){.    rc =
1b24e 20 2d 72 63 3b 0a 20 20 7d 0a 0a 20 20 72 65 74   -rc;.  }..  ret
1b24f 75 72 6e 20 72 63 3b 0a 7d 0a 20 0a 0a 2f 2a 0a  urn rc;.}. ../*.
1b250 2a 2a 20 70 43 75 72 20 70 6f 69 6e 74 73 20 61  ** pCur points a
1b251 74 20 61 6e 20 69 6e 64 65 78 20 65 6e 74 72 79  t an index entry
1b252 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 74   created using t
1b253 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  he OP_MakeRecord
1b254 20 6f 70 63 6f 64 65 2e 0a 2a 2a 20 52 65 61 64   opcode..** Read
1b255 20 74 68 65 20 72 6f 77 69 64 20 28 74 68 65 20   the rowid (the 
1b256 6c 61 73 74 20 66 69 65 6c 64 20 69 6e 20 74 68  last field in th
1b257 65 20 72 65 63 6f 72 64 29 20 61 6e 64 20 73 74  e record) and st
1b258 6f 72 65 20 69 74 20 69 6e 20 2a 72 6f 77 69 64  ore it in *rowid
1b259 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49  ..** Return SQLI
1b25a 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68  TE_OK if everyth
1b25b 69 6e 67 20 77 6f 72 6b 73 2c 20 6f 72 20 61 6e  ing works, or an
1b25c 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 74 68 65   error code othe
1b25d 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75  rwise..**.** pCu
1b25e 72 20 6d 69 67 68 74 20 62 65 20 70 6f 69 6e 74  r might be point
1b25f 69 6e 67 20 74 6f 20 74 65 78 74 20 6f 62 74 61  ing to text obta
1b260 69 6e 65 64 20 66 72 6f 6d 20 61 20 63 6f 72 72  ined from a corr
1b261 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69 6c  upt database fil
1b262 65 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 63 6f 6e  e..** So the con
1b263 74 65 6e 74 20 63 61 6e 6e 6f 74 20 62 65 20 74  tent cannot be t
1b264 72 75 73 74 65 64 2e 20 20 44 6f 20 61 70 70 72  rusted.  Do appr
1b265 6f 70 72 69 61 74 65 20 63 68 65 63 6b 73 20 6f  opriate checks o
1b266 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 0a 2a  n the content..*
1b267 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1b268 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
1b269 49 64 78 52 6f 77 69 64 28 73 71 6c 69 74 65 33  IdxRowid(sqlite3
1b26a 20 2a 64 62 2c 20 42 74 43 75 72 73 6f 72 20 2a   *db, BtCursor *
1b26b 70 43 75 72 2c 20 69 36 34 20 2a 72 6f 77 69 64  pCur, i64 *rowid
1b26c 29 7b 0a 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65  ){.  i64 nCellKe
1b26d 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b  y = 0;.  int rc;
1b26e 0a 20 20 75 33 32 20 73 7a 48 64 72 3b 20 20 20  .  u32 szHdr;   
1b26f 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
1b270 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  the header */.  
1b271 75 33 32 20 74 79 70 65 52 6f 77 69 64 3b 20 20  u32 typeRowid;  
1b272 20 20 2f 2a 20 53 65 72 69 61 6c 20 74 79 70 65    /* Serial type
1b273 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20 2a 2f   of the rowid */
1b274 0a 20 20 75 33 32 20 6c 65 6e 52 6f 77 69 64 3b  .  u32 lenRowid;
1b275 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
1b276 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 4d  the rowid */.  M
1b277 65 6d 20 6d 2c 20 76 3b 0a 0a 20 20 55 4e 55 53  em m, v;..  UNUS
1b278 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 62 29  ED_PARAMETER(db)
1b279 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20  ;..  /* Get the 
1b27a 73 69 7a 65 20 6f 66 20 74 68 65 20 69 6e 64 65  size of the inde
1b27b 78 20 65 6e 74 72 79 2e 20 20 4f 6e 6c 79 20 69  x entry.  Only i
1b27c 6e 64 69 63 65 73 20 65 6e 74 72 69 65 73 20 6f  ndices entries o
1b27d 66 20 6c 65 73 73 0a 20 20 2a 2a 20 74 68 61 6e  f less.  ** than
1b27e 20 32 47 69 42 20 61 72 65 20 73 75 70 70 6f 72   2GiB are suppor
1b27f 74 20 2d 20 61 6e 79 74 68 69 6e 67 20 6c 61 72  t - anything lar
1b280 67 65 20 6d 75 73 74 20 62 65 20 64 61 74 61 62  ge must be datab
1b281 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a  ase corruption..
1b282 20 20 2a 2a 20 41 6e 79 20 63 6f 72 72 75 70 74    ** Any corrupt
1b283 69 6f 6e 20 69 73 20 64 65 74 65 63 74 65 64 20  ion is detected 
1b284 69 6e 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  in sqlite3BtreeP
1b285 61 72 73 65 43 65 6c 6c 50 74 72 28 29 2c 20 74  arseCellPtr(), t
1b286 68 6f 75 67 68 2c 20 73 6f 0a 20 20 2a 2a 20 74  hough, so.  ** t
1b287 68 69 73 20 63 6f 64 65 20 63 61 6e 20 73 61 66  his code can saf
1b288 65 6c 79 20 61 73 73 75 6d 65 20 74 68 61 74 20  ely assume that 
1b289 6e 43 65 6c 6c 4b 65 79 20 69 73 20 33 32 2d 62  nCellKey is 32-b
1b28a 69 74 73 20 20 0a 20 20 2a 2f 0a 20 20 61 73 73  its  .  */.  ass
1b28b 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
1b28c 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 70  eCursorIsValid(p
1b28d 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 73  Cur) );.  rc = s
1b28e 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69  qlite3BtreeKeySi
1b28f 7a 65 28 70 43 75 72 2c 20 26 6e 43 65 6c 6c 4b  ze(pCur, &nCellK
1b290 65 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72  ey);.  assert( r
1b291 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20  c==SQLITE_OK ); 
1b292 20 20 20 20 2f 2a 20 70 43 75 72 20 69 73 20 61      /* pCur is a
1b293 6c 77 61 79 73 20 76 61 6c 69 64 20 73 6f 20 4b  lways valid so K
1b294 65 79 53 69 7a 65 20 63 61 6e 6e 6f 74 20 66 61  eySize cannot fa
1b295 69 6c 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  il */.  assert( 
1b296 28 6e 43 65 6c 6c 4b 65 79 20 26 20 53 51 4c 49  (nCellKey & SQLI
1b297 54 45 5f 4d 41 58 5f 55 33 32 29 3d 3d 28 75 36  TE_MAX_U32)==(u6
1b298 34 29 6e 43 65 6c 6c 4b 65 79 20 29 3b 0a 0a 20  4)nCellKey );.. 
1b299 20 2f 2a 20 52 65 61 64 20 69 6e 20 74 68 65 20   /* Read in the 
1b29a 63 6f 6d 70 6c 65 74 65 20 63 6f 6e 74 65 6e 74  complete content
1b29b 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e   of the index en
1b29c 74 72 79 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28  try */.  memset(
1b29d 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 29  &m, 0, sizeof(m)
1b29e 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
1b29f 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65  3VdbeMemFromBtre
1b2a0 65 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74 29  e(pCur, 0, (int)
1b2a1 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d 29  nCellKey, 1, &m)
1b2a2 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  ;.  if( rc ){.  
1b2a3 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
1b2a4 0a 0a 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78  ..  /* The index
1b2a5 20 65 6e 74 72 79 20 6d 75 73 74 20 62 65 67 69   entry must begi
1b2a6 6e 20 77 69 74 68 20 61 20 68 65 61 64 65 72 20  n with a header 
1b2a7 73 69 7a 65 20 2a 2f 0a 20 20 28 76 6f 69 64 29  size */.  (void)
1b2a8 67 65 74 56 61 72 69 6e 74 33 32 28 28 75 38 2a  getVarint32((u8*
1b2a9 29 6d 2e 7a 2c 20 73 7a 48 64 72 29 3b 0a 20 20  )m.z, szHdr);.  
1b2aa 74 65 73 74 63 61 73 65 28 20 73 7a 48 64 72 3d  testcase( szHdr=
1b2ab 3d 33 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65  =3 );.  testcase
1b2ac 28 20 73 7a 48 64 72 3d 3d 6d 2e 6e 20 29 3b 0a  ( szHdr==m.n );.
1b2ad 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 73    if( unlikely(s
1b2ae 7a 48 64 72 3c 33 20 7c 7c 20 28 69 6e 74 29 73  zHdr<3 || (int)s
1b2af 7a 48 64 72 3e 6d 2e 6e 29 20 29 7b 0a 20 20 20  zHdr>m.n) ){.   
1b2b0 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64 5f   goto idx_rowid_
1b2b1 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a  corruption;.  }.
1b2b2 0a 20 20 2f 2a 20 54 68 65 20 6c 61 73 74 20 66  .  /* The last f
1b2b3 69 65 6c 64 20 6f 66 20 74 68 65 20 69 6e 64 65  ield of the inde
1b2b4 78 20 73 68 6f 75 6c 64 20 62 65 20 61 6e 20 69  x should be an i
1b2b5 6e 74 65 67 65 72 20 2d 20 74 68 65 20 52 4f 57  nteger - the ROW
1b2b6 49 44 2e 0a 20 20 2a 2a 20 56 65 72 69 66 79 20  ID..  ** Verify 
1b2b7 74 68 61 74 20 74 68 65 20 6c 61 73 74 20 65 6e  that the last en
1b2b8 74 72 79 20 72 65 61 6c 6c 79 20 69 73 20 61 6e  try really is an
1b2b9 20 69 6e 74 65 67 65 72 2e 20 2a 2f 0a 20 20 28   integer. */.  (
1b2ba 76 6f 69 64 29 67 65 74 56 61 72 69 6e 74 33 32  void)getVarint32
1b2bb 28 28 75 38 2a 29 26 6d 2e 7a 5b 73 7a 48 64 72  ((u8*)&m.z[szHdr
1b2bc 2d 31 5d 2c 20 74 79 70 65 52 6f 77 69 64 29 3b  -1], typeRowid);
1b2bd 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70  .  testcase( typ
1b2be 65 52 6f 77 69 64 3d 3d 31 20 29 3b 0a 20 20 74  eRowid==1 );.  t
1b2bf 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77  estcase( typeRow
1b2c0 69 64 3d 3d 32 20 29 3b 0a 20 20 74 65 73 74 63  id==2 );.  testc
1b2c1 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d  ase( typeRowid==
1b2c2 33 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  3 );.  testcase(
1b2c3 20 74 79 70 65 52 6f 77 69 64 3d 3d 34 20 29 3b   typeRowid==4 );
1b2c4 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70  .  testcase( typ
1b2c5 65 52 6f 77 69 64 3d 3d 35 20 29 3b 0a 20 20 74  eRowid==5 );.  t
1b2c6 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77  estcase( typeRow
1b2c7 69 64 3d 3d 36 20 29 3b 0a 20 20 74 65 73 74 63  id==6 );.  testc
1b2c8 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d  ase( typeRowid==
1b2c9 38 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  8 );.  testcase(
1b2ca 20 74 79 70 65 52 6f 77 69 64 3d 3d 39 20 29 3b   typeRowid==9 );
1b2cb 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28  .  if( unlikely(
1b2cc 74 79 70 65 52 6f 77 69 64 3c 31 20 7c 7c 20 74  typeRowid<1 || t
1b2cd 79 70 65 52 6f 77 69 64 3e 39 20 7c 7c 20 74 79  ypeRowid>9 || ty
1b2ce 70 65 52 6f 77 69 64 3d 3d 37 29 20 29 7b 0a 20  peRowid==7) ){. 
1b2cf 20 20 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69     goto idx_rowi
1b2d0 64 5f 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20  d_corruption;.  
1b2d1 7d 0a 20 20 6c 65 6e 52 6f 77 69 64 20 3d 20 73  }.  lenRowid = s
1b2d2 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
1b2d3 54 79 70 65 4c 65 6e 28 74 79 70 65 52 6f 77 69  TypeLen(typeRowi
1b2d4 64 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20  d);.  testcase( 
1b2d5 28 75 33 32 29 6d 2e 6e 3d 3d 73 7a 48 64 72 2b  (u32)m.n==szHdr+
1b2d6 6c 65 6e 52 6f 77 69 64 20 29 3b 0a 20 20 69 66  lenRowid );.  if
1b2d7 28 20 75 6e 6c 69 6b 65 6c 79 28 28 75 33 32 29  ( unlikely((u32)
1b2d8 6d 2e 6e 3c 73 7a 48 64 72 2b 6c 65 6e 52 6f 77  m.n<szHdr+lenRow
1b2d9 69 64 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  id) ){.    goto 
1b2da 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70  idx_rowid_corrup
1b2db 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  tion;.  }..  /* 
1b2dc 46 65 74 63 68 20 74 68 65 20 69 6e 74 65 67 65  Fetch the intege
1b2dd 72 20 6f 66 66 20 74 68 65 20 65 6e 64 20 6f 66  r off the end of
1b2de 20 74 68 65 20 69 6e 64 65 78 20 72 65 63 6f 72   the index recor
1b2df 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64  d */.  sqlite3Vd
1b2e0 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 2a  beSerialGet((u8*
1b2e1 29 26 6d 2e 7a 5b 6d 2e 6e 2d 6c 65 6e 52 6f 77  )&m.z[m.n-lenRow
1b2e2 69 64 5d 2c 20 74 79 70 65 52 6f 77 69 64 2c 20  id], typeRowid, 
1b2e3 26 76 29 3b 0a 20 20 2a 72 6f 77 69 64 20 3d 20  &v);.  *rowid = 
1b2e4 76 2e 75 2e 69 3b 0a 20 20 73 71 6c 69 74 65 33  v.u.i;.  sqlite3
1b2e5 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26  VdbeMemRelease(&
1b2e6 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  m);.  return SQL
1b2e7 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 4a 75  ITE_OK;..  /* Ju
1b2e8 6d 70 20 68 65 72 65 20 69 66 20 64 61 74 61 62  mp here if datab
1b2e9 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 69  ase corruption i
1b2ea 73 20 64 65 74 65 63 74 65 64 20 61 66 74 65 72  s detected after
1b2eb 20 6d 20 68 61 73 20 62 65 65 6e 0a 20 20 2a 2a   m has been.  **
1b2ec 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 46 72 65   allocated.  Fre
1b2ed 65 20 74 68 65 20 6d 20 6f 62 6a 65 63 74 20 61  e the m object a
1b2ee 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  nd return SQLITE
1b2ef 5f 43 4f 52 52 55 50 54 2e 20 2a 2f 0a 69 64 78  _CORRUPT. */.idx
1b2f0 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 74 69 6f  _rowid_corruptio
1b2f1 6e 3a 0a 20 20 74 65 73 74 63 61 73 65 28 20 6d  n:.  testcase( m
1b2f2 2e 7a 4d 61 6c 6c 6f 63 21 3d 30 20 29 3b 0a 20  .zMalloc!=0 );. 
1b2f3 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
1b2f4 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 65  elease(&m);.  re
1b2f5 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
1b2f6 55 50 54 5f 42 4b 50 54 3b 0a 7d 0a 0a 2f 2a 0a  UPT_BKPT;.}../*.
1b2f7 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 6b  ** Compare the k
1b2f8 65 79 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  ey of the index 
1b2f9 65 6e 74 72 79 20 74 68 61 74 20 63 75 72 73 6f  entry that curso
1b2fa 72 20 70 43 20 69 73 20 70 6f 69 6e 74 69 6e 67  r pC is pointing
1b2fb 20 74 6f 20 61 67 61 69 6e 73 74 0a 2a 2a 20 74   to against.** t
1b2fc 68 65 20 6b 65 79 20 73 74 72 69 6e 67 20 69 6e  he key string in
1b2fd 20 70 55 6e 70 61 63 6b 65 64 2e 20 20 57 72 69   pUnpacked.  Wri
1b2fe 74 65 20 69 6e 74 6f 20 2a 70 52 65 73 20 61 20  te into *pRes a 
1b2ff 6e 75 6d 62 65 72 0a 2a 2a 20 74 68 61 74 20 69  number.** that i
1b300 73 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f  s negative, zero
1b301 2c 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66  , or positive if
1b302 20 70 43 20 69 73 20 6c 65 73 73 20 74 68 61 6e   pC is less than
1b303 2c 20 65 71 75 61 6c 20 74 6f 2c 0a 2a 2a 20 6f  , equal to,.** o
1b304 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 70  r greater than p
1b305 55 6e 70 61 63 6b 65 64 2e 20 20 52 65 74 75 72  Unpacked.  Retur
1b306 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  n SQLITE_OK on s
1b307 75 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 70 55  uccess..**.** pU
1b308 6e 70 61 63 6b 65 64 20 69 73 20 65 69 74 68 65  npacked is eithe
1b309 72 20 63 72 65 61 74 65 64 20 77 69 74 68 6f 75  r created withou
1b30a 74 20 61 20 72 6f 77 69 64 20 6f 72 20 69 73 20  t a rowid or is 
1b30b 74 72 75 6e 63 61 74 65 64 20 73 6f 20 74 68 61  truncated so tha
1b30c 74 20 69 74 0a 2a 2a 20 6f 6d 69 74 73 20 74 68  t it.** omits th
1b30d 65 20 72 6f 77 69 64 20 61 74 20 74 68 65 20 65  e rowid at the e
1b30e 6e 64 2e 20 20 54 68 65 20 72 6f 77 69 64 20 61  nd.  The rowid a
1b30f 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
1b310 20 69 6e 64 65 78 20 65 6e 74 72 79 0a 2a 2a 20   index entry.** 
1b311 69 73 20 69 67 6e 6f 72 65 64 20 61 73 20 77 65  is ignored as we
1b312 6c 6c 2e 20 20 48 65 6e 63 65 2c 20 74 68 69 73  ll.  Hence, this
1b313 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63 6f   routine only co
1b314 6d 70 61 72 65 73 20 74 68 65 20 70 72 65 66 69  mpares the prefi
1b315 78 65 73 20 0a 2a 2a 20 6f 66 20 74 68 65 20 6b  xes .** of the k
1b316 65 79 73 20 70 72 69 6f 72 20 74 6f 20 74 68 65  eys prior to the
1b317 20 66 69 6e 61 6c 20 72 6f 77 69 64 2c 20 6e 6f   final rowid, no
1b318 74 20 74 68 65 20 65 6e 74 69 72 65 20 6b 65 79  t the entire key
1b319 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1b31a 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
1b31b 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 65  dbeIdxKeyCompare
1b31c 28 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  (.  VdbeCursor *
1b31d 70 43 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  pC,             
1b31e 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 74 6f  /* The cursor to
1b31f 20 63 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74   compare against
1b320 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52 65   */.  UnpackedRe
1b321 63 6f 72 64 20 2a 70 55 6e 70 61 63 6b 65 64 2c  cord *pUnpacked,
1b322 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 76 65    /* Unpacked ve
1b323 72 73 69 6f 6e 20 6f 66 20 6b 65 79 20 74 6f 20  rsion of key to 
1b324 63 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20  compare against 
1b325 2a 2f 0a 20 20 69 6e 74 20 2a 72 65 73 20 20 20  */.  int *res   
1b326 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b327 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f   /* Write the co
1b328 6d 70 61 72 69 73 6f 6e 20 72 65 73 75 6c 74 20  mparison result 
1b329 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 36 34  here */.){.  i64
1b32a 20 6e 43 65 6c 6c 4b 65 79 20 3d 20 30 3b 0a 20   nCellKey = 0;. 
1b32b 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 43 75 72   int rc;.  BtCur
1b32c 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 43 2d 3e  sor *pCur = pC->
1b32d 70 43 75 72 73 6f 72 3b 0a 20 20 4d 65 6d 20 6d  pCursor;.  Mem m
1b32e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
1b32f 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49  ite3BtreeCursorI
1b330 73 56 61 6c 69 64 28 70 43 75 72 29 20 29 3b 0a  sValid(pCur) );.
1b331 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1b332 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72 2c  reeKeySize(pCur,
1b333 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 61   &nCellKey);.  a
1b334 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
1b335 45 5f 4f 4b 20 29 3b 20 20 20 20 2f 2a 20 70 43  E_OK );    /* pC
1b336 75 72 20 69 73 20 61 6c 77 61 79 73 20 76 61 6c  ur is always val
1b337 69 64 20 73 6f 20 4b 65 79 53 69 7a 65 20 63 61  id so KeySize ca
1b338 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 2f  nnot fail */.  /
1b339 2a 20 6e 43 65 6c 6c 4b 65 79 20 77 69 6c 6c 20  * nCellKey will 
1b33a 61 6c 77 61 79 73 20 62 65 20 62 65 74 77 65 65  always be betwee
1b33b 6e 20 30 20 61 6e 64 20 30 78 66 66 66 66 66 66  n 0 and 0xffffff
1b33c 66 66 20 62 65 63 61 75 73 65 20 6f 66 20 74 68  ff because of th
1b33d 65 20 73 61 79 0a 20 20 2a 2a 20 74 68 61 74 20  e say.  ** that 
1b33e 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74  btreeParseCellPt
1b33f 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 47  r() and sqlite3G
1b340 65 74 56 61 72 69 6e 74 33 32 28 29 20 61 72 65  etVarint32() are
1b341 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 2a 2f 0a   implemented */.
1b342 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 3d    if( nCellKey<=
1b343 30 20 7c 7c 20 6e 43 65 6c 6c 4b 65 79 3e 30 78  0 || nCellKey>0x
1b344 37 66 66 66 66 66 66 66 20 29 7b 0a 20 20 20 20  7fffffff ){.    
1b345 2a 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65  *res = 0;.    re
1b346 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
1b347 55 50 54 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65  UPT;.  }.  memse
1b348 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28  t(&m, 0, sizeof(
1b349 6d 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  m));.  rc = sqli
1b34a 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74  te3VdbeMemFromBt
1b34b 72 65 65 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c  ree(pC->pCursor,
1b34c 20 30 2c 20 28 69 6e 74 29 6e 43 65 6c 6c 4b 65   0, (int)nCellKe
1b34d 79 2c 20 31 2c 20 26 6d 29 3b 0a 20 20 69 66 28  y, 1, &m);.  if(
1b34e 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72   rc ){.    retur
1b34f 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65  n rc;.  }.  asse
1b350 72 74 28 20 70 55 6e 70 61 63 6b 65 64 2d 3e 66  rt( pUnpacked->f
1b351 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f  lags & UNPACKED_
1b352 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20 29 3b 0a  IGNORE_ROWID );.
1b353 20 20 2a 72 65 73 20 3d 20 73 71 6c 69 74 65 33    *res = sqlite3
1b354 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72  VdbeRecordCompar
1b355 65 28 6d 2e 6e 2c 20 6d 2e 7a 2c 20 70 55 6e 70  e(m.n, m.z, pUnp
1b356 61 63 6b 65 64 29 3b 0a 20 20 73 71 6c 69 74 65  acked);.  sqlite
1b357 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
1b358 26 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  &m);.  return SQ
1b359 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
1b35a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
1b35b 65 74 73 20 74 68 65 20 76 61 6c 75 65 20 74 6f  ets the value to
1b35c 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20   be returned by 
1b35d 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73  subsequent calls
1b35e 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63   to.** sqlite3_c
1b35f 68 61 6e 67 65 73 28 29 20 6f 6e 20 74 68 65 20  hanges() on the 
1b360 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
1b361 27 64 62 27 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45  'db'. .*/.SQLITE
1b362 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1b363 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e  lite3VdbeSetChan
1b364 67 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ges(sqlite3 *db,
1b365 20 69 6e 74 20 6e 43 68 61 6e 67 65 29 7b 0a 20   int nChange){. 
1b366 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1b367 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
1b368 6d 75 74 65 78 29 20 29 3b 0a 20 20 64 62 2d 3e  mutex) );.  db->
1b369 6e 43 68 61 6e 67 65 20 3d 20 6e 43 68 61 6e 67  nChange = nChang
1b36a 65 3b 0a 20 20 64 62 2d 3e 6e 54 6f 74 61 6c 43  e;.  db->nTotalC
1b36b 68 61 6e 67 65 20 2b 3d 20 6e 43 68 61 6e 67 65  hange += nChange
1b36c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 61  ;.}../*.** Set a
1b36d 20 66 6c 61 67 20 69 6e 20 74 68 65 20 76 64 62   flag in the vdb
1b36e 65 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20  e to update the 
1b36f 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 77  change counter w
1b370 68 65 6e 20 69 74 20 69 73 20 66 69 6e 61 6c 69  hen it is finali
1b371 73 65 64 0a 2a 2a 20 6f 72 20 72 65 73 65 74 2e  sed.** or reset.
1b372 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1b373 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
1b374 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28  dbeCountChanges(
1b375 56 64 62 65 20 2a 76 29 7b 0a 20 20 76 2d 3e 63  Vdbe *v){.  v->c
1b376 68 61 6e 67 65 43 6e 74 4f 6e 20 3d 20 31 3b 0a  hangeCntOn = 1;.
1b377 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 65 76  }../*.** Mark ev
1b378 65 72 79 20 70 72 65 70 61 72 65 64 20 73 74 61  ery prepared sta
1b379 74 65 6d 65 6e 74 20 61 73 73 6f 63 69 61 74 65  tement associate
1b37a 64 20 77 69 74 68 20 61 20 64 61 74 61 62 61 73  d with a databas
1b37b 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20  e connection.** 
1b37c 61 73 20 65 78 70 69 72 65 64 2e 0a 2a 2a 0a 2a  as expired..**.*
1b37d 2a 20 41 6e 20 65 78 70 69 72 65 64 20 73 74 61  * An expired sta
1b37e 74 65 6d 65 6e 74 20 6d 65 61 6e 73 20 74 68 61  tement means tha
1b37f 74 20 72 65 63 6f 6d 70 69 6c 61 74 69 6f 6e 20  t recompilation 
1b380 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  of the statement
1b381 20 69 73 0a 2a 2a 20 72 65 63 6f 6d 6d 65 6e 64   is.** recommend
1b382 2e 20 20 53 74 61 74 65 6d 65 6e 74 73 20 65 78  .  Statements ex
1b383 70 69 72 65 20 77 68 65 6e 20 74 68 69 6e 67 73  pire when things
1b384 20 68 61 70 70 65 6e 20 74 68 61 74 20 6d 61 6b   happen that mak
1b385 65 20 74 68 65 69 72 0a 2a 2a 20 70 72 6f 67 72  e their.** progr
1b386 61 6d 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 52  ams obsolete.  R
1b387 65 6d 6f 76 69 6e 67 20 75 73 65 72 2d 64 65 66  emoving user-def
1b388 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 6f  ined functions o
1b389 72 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73  r collating.** s
1b38a 65 71 75 65 6e 63 65 73 2c 20 6f 72 20 63 68 61  equences, or cha
1b38b 6e 67 69 6e 67 20 61 6e 20 61 75 74 68 6f 72 69  nging an authori
1b38c 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  zation function 
1b38d 61 72 65 20 74 68 65 20 74 79 70 65 73 20 6f 66  are the types of
1b38e 0a 2a 2a 20 74 68 69 6e 67 73 20 74 68 61 74 20  .** things that 
1b38f 6d 61 6b 65 20 70 72 65 70 61 72 65 64 20 73 74  make prepared st
1b390 61 74 65 6d 65 6e 74 73 20 6f 62 73 6f 6c 65 74  atements obsolet
1b391 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1b392 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1b393 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53  3ExpirePreparedS
1b394 74 61 74 65 6d 65 6e 74 73 28 73 71 6c 69 74 65  tatements(sqlite
1b395 33 20 2a 64 62 29 7b 0a 20 20 56 64 62 65 20 2a  3 *db){.  Vdbe *
1b396 70 3b 0a 20 20 66 6f 72 28 70 20 3d 20 64 62 2d  p;.  for(p = db-
1b397 3e 70 56 64 62 65 3b 20 70 3b 20 70 3d 70 2d 3e  >pVdbe; p; p=p->
1b398 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 65  pNext){.    p->e
1b399 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  xpired = 1;.  }.
1b39a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1b39b 74 68 65 20 64 61 74 61 62 61 73 65 20 61 73 73  the database ass
1b39c 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
1b39d 20 56 64 62 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   Vdbe..*/.SQLITE
1b39e 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33  _PRIVATE sqlite3
1b39f 20 2a 73 71 6c 69 74 65 33 56 64 62 65 44 62 28   *sqlite3VdbeDb(
1b3a0 56 64 62 65 20 2a 76 29 7b 0a 20 20 72 65 74 75  Vdbe *v){.  retu
1b3a1 72 6e 20 76 2d 3e 64 62 3b 0a 7d 0a 0a 2f 2a 2a  rn v->db;.}../**
1b3a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
1b3a3 20 6f 66 20 76 64 62 65 61 75 78 2e 63 20 2a 2a   of vdbeaux.c **
1b3a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b3a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b3a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
1b3a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
1b3a8 69 6e 20 66 69 6c 65 20 76 64 62 65 61 70 69 2e  in file vdbeapi.
1b3a9 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1b3aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b3ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
1b3ac 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 36 0a 2a  ** 2004 May 26.*
1b3ad 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
1b3ae 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
1b3af 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
1b3b0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
1b3b1 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
1b3b2 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
1b3b3 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
1b3b4 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
1b3b5 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
1b3b6 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
1b3b7 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
1b3b8 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
1b3b9 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
1b3ba 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
1b3bb 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
1b3bc 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
1b3bd 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
1b3be 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
1b3bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b3c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b3c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b3c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
1b3c3 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
1b3c4 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 20  ntains code use 
1b3c5 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 41 50 49  to implement API
1b3c6 73 20 74 68 61 74 20 61 72 65 20 70 61 72 74 20  s that are part 
1b3c7 6f 66 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e 0a  of the.** VDBE..
1b3c8 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 61  **.** $Id: vdbea
1b3c9 70 69 2e 63 2c 76 20 31 2e 31 36 37 20 32 30 30  pi.c,v 1.167 200
1b3ca 39 2f 30 36 2f 32 35 20 30 31 3a 34 37 3a 31 32  9/06/25 01:47:12
1b3cb 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 23   drh Exp $.*/..#
1b3cc 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1b3cd 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a  IT_DEPRECATED./*
1b3ce 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20  .** Return TRUE 
1b3cf 28 6e 6f 6e 2d 7a 65 72 6f 29 20 6f 66 20 74 68  (non-zero) of th
1b3d0 65 20 73 74 61 74 65 6d 65 6e 74 20 73 75 70 70  e statement supp
1b3d1 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d  lied as an argum
1b3d2 65 6e 74 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20  ent needs.** to 
1b3d3 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 2e 20 20  be recompiled.  
1b3d4 41 20 73 74 61 74 65 6d 65 6e 74 20 6e 65 65 64  A statement need
1b3d5 73 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c  s to be recompil
1b3d6 65 64 20 77 68 65 6e 65 76 65 72 20 74 68 65 0a  ed whenever the.
1b3d7 2a 2a 20 65 78 65 63 75 74 69 6f 6e 20 65 6e 76  ** execution env
1b3d8 69 72 6f 6e 6d 65 6e 74 20 63 68 61 6e 67 65 73  ironment changes
1b3d9 20 69 6e 20 61 20 77 61 79 20 74 68 61 74 20 77   in a way that w
1b3da 6f 75 6c 64 20 61 6c 74 65 72 20 74 68 65 20 70  ould alter the p
1b3db 72 6f 67 72 61 6d 0a 2a 2a 20 74 68 61 74 20 73  rogram.** that s
1b3dc 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29  qlite3_prepare()
1b3dd 20 67 65 6e 65 72 61 74 65 73 2e 20 20 46 6f 72   generates.  For
1b3de 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 6e 65 77   example, if new
1b3df 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 0a 2a 2a   functions or.**
1b3e0 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
1b3e1 6e 63 65 73 20 61 72 65 20 72 65 67 69 73 74 65  nces are registe
1b3e2 72 65 64 20 6f 72 20 69 66 20 61 6e 20 61 75 74  red or if an aut
1b3e3 68 6f 72 69 7a 65 72 20 66 75 6e 63 74 69 6f 6e  horizer function
1b3e4 20 69 73 0a 2a 2a 20 61 64 64 65 64 20 6f 72 20   is.** added or 
1b3e5 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49  changed..*/.SQLI
1b3e6 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1b3e7 65 33 5f 65 78 70 69 72 65 64 28 73 71 6c 69 74  e3_expired(sqlit
1b3e8 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b  e3_stmt *pStmt){
1b3e9 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64  .  Vdbe *p = (Vd
1b3ea 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72 65 74  be*)pStmt;.  ret
1b3eb 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 65  urn p==0 || p->e
1b3ec 78 70 69 72 65 64 3b 0a 7d 0a 23 65 6e 64 69 66  xpired;.}.#endif
1b3ed 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
1b3ee 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 64 65  owing routine de
1b3ef 73 74 72 6f 79 73 20 61 20 76 69 72 74 75 61 6c  stroys a virtual
1b3f0 20 6d 61 63 68 69 6e 65 20 74 68 61 74 20 69 73   machine that is
1b3f1 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a 20 74   created by.** t
1b3f2 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69  he sqlite3_compi
1b3f3 6c 65 28 29 20 72 6f 75 74 69 6e 65 2e 20 54 68  le() routine. Th
1b3f4 65 20 69 6e 74 65 67 65 72 20 72 65 74 75 72 6e  e integer return
1b3f5 65 64 20 69 73 20 61 6e 20 53 51 4c 49 54 45 5f  ed is an SQLITE_
1b3f6 0a 2a 2a 20 73 75 63 63 65 73 73 2f 66 61 69 6c  .** success/fail
1b3f7 75 72 65 20 63 6f 64 65 20 74 68 61 74 20 64 65  ure code that de
1b3f8 73 63 72 69 62 65 73 20 74 68 65 20 72 65 73 75  scribes the resu
1b3f9 6c 74 20 6f 66 20 65 78 65 63 75 74 69 6e 67 20  lt of executing 
1b3fa 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 6d  the virtual.** m
1b3fb 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  achine..**.** Th
1b3fc 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20  is routine sets 
1b3fd 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61  the error code a
1b3fe 6e 64 20 73 74 72 69 6e 67 20 72 65 74 75 72 6e  nd string return
1b3ff 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33  ed by.** sqlite3
1b400 5f 65 72 72 63 6f 64 65 28 29 2c 20 73 71 6c 69  _errcode(), sqli
1b401 74 65 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64  te3_errmsg() and
1b402 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31   sqlite3_errmsg1
1b403 36 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  6()..*/.SQLITE_A
1b404 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66  PI int sqlite3_f
1b405 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f  inalize(sqlite3_
1b406 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20  stmt *pStmt){.  
1b407 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 53  int rc;.  if( pS
1b408 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63  tmt==0 ){.    rc
1b409 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1b40a 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64 62 65 20  }else{.    Vdbe 
1b40b 2a 76 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d  *v = (Vdbe*)pStm
1b40c 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a  t;.    sqlite3 *
1b40d 64 62 20 3d 20 76 2d 3e 64 62 3b 0a 23 69 66 20  db = v->db;.#if 
1b40e 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
1b40f 45 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  E.    sqlite3_mu
1b410 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 76 2d 3e  tex *mutex = v->
1b411 64 62 2d 3e 6d 75 74 65 78 3b 0a 23 65 6e 64 69  db->mutex;.#endi
1b412 66 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  f.    sqlite3_mu
1b413 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29  tex_enter(mutex)
1b414 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1b415 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 76  e3VdbeFinalize(v
1b416 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
1b417 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72  te3ApiExit(db, r
1b418 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  c);.    sqlite3_
1b419 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65  mutex_leave(mute
1b41a 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  x);.  }.  return
1b41b 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65   rc;.}../*.** Te
1b41c 72 6d 69 6e 61 74 65 20 74 68 65 20 63 75 72 72  rminate the curr
1b41d 65 6e 74 20 65 78 65 63 75 74 69 6f 6e 20 6f 66  ent execution of
1b41e 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   an SQL statemen
1b41f 74 20 61 6e 64 20 72 65 73 65 74 20 69 74 0a 2a  t and reset it.*
1b420 2a 20 62 61 63 6b 20 74 6f 20 69 74 73 20 73 74  * back to its st
1b421 61 72 74 69 6e 67 20 73 74 61 74 65 20 73 6f 20  arting state so 
1b422 74 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72  that it can be r
1b423 65 75 73 65 64 2e 20 41 20 73 75 63 63 65 73 73  eused. A success
1b424 20 63 6f 64 65 20 66 72 6f 6d 0a 2a 2a 20 74 68   code from.** th
1b425 65 20 70 72 69 6f 72 20 65 78 65 63 75 74 69 6f  e prior executio
1b426 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  n is returned..*
1b427 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1b428 65 20 73 65 74 73 20 74 68 65 20 65 72 72 6f 72  e sets the error
1b429 20 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e 67   code and string
1b42a 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20   returned by.** 
1b42b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28  sqlite3_errcode(
1b42c 29 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  ), sqlite3_errms
1b42d 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  g() and sqlite3_
1b42e 65 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a 53  errmsg16()..*/.S
1b42f 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1b430 6c 69 74 65 33 5f 72 65 73 65 74 28 73 71 6c 69  lite3_reset(sqli
1b431 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29  te3_stmt *pStmt)
1b432 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66  {.  int rc;.  if
1b433 28 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20  ( pStmt==0 ){.  
1b434 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
1b435 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 56  ;.  }else{.    V
1b436 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29  dbe *v = (Vdbe*)
1b437 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69 74  pStmt;.    sqlit
1b438 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 76  e3_mutex_enter(v
1b439 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  ->db->mutex);.  
1b43a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1b43b 62 65 52 65 73 65 74 28 76 29 3b 0a 20 20 20 20  beReset(v);.    
1b43c 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52  sqlite3VdbeMakeR
1b43d 65 61 64 79 28 76 2c 20 2d 31 2c 20 30 2c 20 30  eady(v, -1, 0, 0
1b43e 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  , 0, 0, 0);.    
1b43f 61 73 73 65 72 74 28 20 28 72 63 20 26 20 28 76  assert( (rc & (v
1b440 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29 29 3d  ->db->errMask))=
1b441 3d 72 63 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  =rc );.    rc = 
1b442 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 76  sqlite3ApiExit(v
1b443 2d 3e 64 62 2c 20 72 63 29 3b 0a 20 20 20 20 73  ->db, rc);.    s
1b444 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
1b445 76 65 28 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ve(v->db->mutex)
1b446 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1b447 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  c;.}../*.** Set 
1b448 61 6c 6c 20 74 68 65 20 70 61 72 61 6d 65 74 65  all the paramete
1b449 72 73 20 69 6e 20 74 68 65 20 63 6f 6d 70 69 6c  rs in the compil
1b44a 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ed SQL statement
1b44b 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c   to NULL..*/.SQL
1b44c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1b44d 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e  te3_clear_bindin
1b44e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  gs(sqlite3_stmt 
1b44f 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20 69  *pStmt){.  int i
1b450 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
1b451 49 54 45 5f 4f 4b 3b 0a 20 20 56 64 62 65 20 2a  ITE_OK;.  Vdbe *
1b452 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74  p = (Vdbe*)pStmt
1b453 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  ;.#if SQLITE_THR
1b454 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65  EADSAFE.  sqlite
1b455 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d  3_mutex *mutex =
1b456 20 28 28 56 64 62 65 2a 29 70 53 74 6d 74 29 2d   ((Vdbe*)pStmt)-
1b457 3e 64 62 2d 3e 6d 75 74 65 78 3b 0a 23 65 6e 64  >db->mutex;.#end
1b458 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  if.  sqlite3_mut
1b459 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b  ex_enter(mutex);
1b45a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d  .  for(i=0; i<p-
1b45b 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nVar; i++){.   
1b45c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
1b45d 65 6c 65 61 73 65 28 26 70 2d 3e 61 56 61 72 5b  elease(&p->aVar[
1b45e 69 5d 29 3b 0a 20 20 20 20 70 2d 3e 61 56 61 72  i]);.    p->aVar
1b45f 5b 69 5d 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  [i].flags = MEM_
1b460 4e 75 6c 6c 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  Null;.  }.  sqli
1b461 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
1b462 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
1b463 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a   rc;.}.../******
1b464 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b465 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 76  ****** sqlite3_v
1b466 61 6c 75 65 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  alue_  *********
1b467 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b468 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  ******.** The fo
1b469 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73  llowing routines
1b46a 20 65 78 74 72 61 63 74 20 69 6e 66 6f 72 6d 61   extract informa
1b46b 74 69 6f 6e 20 66 72 6f 6d 20 61 20 4d 65 6d 20  tion from a Mem 
1b46c 6f 72 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  or sqlite3_value
1b46d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  .** structure..*
1b46e 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
1b46f 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
1b470 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 73 71 6c 69  _value_blob(sqli
1b471 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29  te3_value *pVal)
1b472 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 28 4d 65  {.  Mem *p = (Me
1b473 6d 2a 29 70 56 61 6c 3b 0a 20 20 69 66 28 20 70  m*)pVal;.  if( p
1b474 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42  ->flags & (MEM_B
1b475 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 20 29 7b 0a  lob|MEM_Str) ){.
1b476 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1b477 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 70 29 3b  emExpandBlob(p);
1b478 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 26 3d  .    p->flags &=
1b479 20 7e 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 70   ~MEM_Str;.    p
1b47a 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 42  ->flags |= MEM_B
1b47b 6c 6f 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  lob;.    return 
1b47c 70 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  p->z;.  }else{. 
1b47d 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65     return sqlite
1b47e 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 70 56 61  3_value_text(pVa
1b47f 6c 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45  l);.  }.}.SQLITE
1b480 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1b481 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 73 71 6c  _value_bytes(sql
1b482 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c  ite3_value *pVal
1b483 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
1b484 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 70 56  te3ValueBytes(pV
1b485 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29  al, SQLITE_UTF8)
1b486 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ;.}.SQLITE_API i
1b487 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt sqlite3_value
1b488 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74 65 33  _bytes16(sqlite3
1b489 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20  _value *pVal){. 
1b48a 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56   return sqlite3V
1b48b 61 6c 75 65 42 79 74 65 73 28 70 56 61 6c 2c 20  alueBytes(pVal, 
1b48c 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49  SQLITE_UTF16NATI
1b48d 56 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  VE);.}.SQLITE_AP
1b48e 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33  I double sqlite3
1b48f 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 73 71  _value_double(sq
1b490 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
1b491 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  l){.  return sql
1b492 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75  ite3VdbeRealValu
1b493 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d  e((Mem*)pVal);.}
1b494 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1b495 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
1b496 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  t(sqlite3_value 
1b497 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e  *pVal){.  return
1b498 20 28 69 6e 74 29 73 71 6c 69 74 65 33 56 64 62   (int)sqlite3Vdb
1b499 65 49 6e 74 56 61 6c 75 65 28 28 4d 65 6d 2a 29  eIntValue((Mem*)
1b49a 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  pVal);.}.SQLITE_
1b49b 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  API sqlite_int64
1b49c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
1b49d 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 76 61 6c  nt64(sqlite3_val
1b49e 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74  ue *pVal){.  ret
1b49f 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 49  urn sqlite3VdbeI
1b4a0 6e 74 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56  ntValue((Mem*)pV
1b4a1 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  al);.}.SQLITE_AP
1b4a2 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  I const unsigned
1b4a3 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76   char *sqlite3_v
1b4a4 61 6c 75 65 5f 74 65 78 74 28 73 71 6c 69 74 65  alue_text(sqlite
1b4a5 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a  3_value *pVal){.
1b4a6 20 20 72 65 74 75 72 6e 20 28 63 6f 6e 73 74 20    return (const 
1b4a7 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
1b4a8 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74  sqlite3ValueText
1b4a9 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54  (pVal, SQLITE_UT
1b4aa 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53  F8);.}.#ifndef S
1b4ab 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
1b4ac 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
1b4ad 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
1b4ae 76 61 6c 75 65 5f 74 65 78 74 31 36 28 73 71 6c  value_text16(sql
1b4af 69 74 65 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c  ite3_value* pVal
1b4b0 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
1b4b1 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61  te3ValueText(pVa
1b4b2 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e  l, SQLITE_UTF16N
1b4b3 41 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49 54 45  ATIVE);.}.SQLITE
1b4b4 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
1b4b5 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  *sqlite3_value_t
1b4b6 65 78 74 31 36 62 65 28 73 71 6c 69 74 65 33 5f  ext16be(sqlite3_
1b4b7 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20  value *pVal){.  
1b4b8 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61  return sqlite3Va
1b4b9 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51  lueText(pVal, SQ
1b4ba 4c 49 54 45 5f 55 54 46 31 36 42 45 29 3b 0a 7d  LITE_UTF16BE);.}
1b4bb 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
1b4bc 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
1b4bd 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 73  value_text16le(s
1b4be 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
1b4bf 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  al){.  return sq
1b4c0 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70  lite3ValueText(p
1b4c1 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  Val, SQLITE_UTF1
1b4c2 36 4c 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  6LE);.}.#endif /
1b4c3 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  * SQLITE_OMIT_UT
1b4c4 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  F16 */.SQLITE_AP
1b4c5 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61  I int sqlite3_va
1b4c6 6c 75 65 5f 74 79 70 65 28 73 71 6c 69 74 65 33  lue_type(sqlite3
1b4c7 5f 76 61 6c 75 65 2a 20 70 56 61 6c 29 7b 0a 20  _value* pVal){. 
1b4c8 20 72 65 74 75 72 6e 20 70 56 61 6c 2d 3e 74 79   return pVal->ty
1b4c9 70 65 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  pe;.}../********
1b4ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b4cb 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73  **** sqlite3_res
1b4cc 75 6c 74 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ult_  **********
1b4cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b4ce 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  *****.** The fol
1b4cf 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20  lowing routines 
1b4d0 61 72 65 20 75 73 65 64 20 62 79 20 75 73 65 72  are used by user
1b4d1 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
1b4d2 6e 73 20 74 6f 20 73 70 65 63 69 66 79 0a 2a 2a  ns to specify.**
1b4d3 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65   the function re
1b4d4 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  sult..**.** The 
1b4d5 73 65 74 53 74 72 4f 72 45 72 72 6f 72 28 29 20  setStrOrError() 
1b4d6 66 75 6e 74 69 6f 6e 20 63 61 6c 6c 73 20 73 71  funtion calls sq
1b4d7 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53  lite3VdbeMemSetS
1b4d8 74 72 28 29 20 74 6f 20 73 74 6f 72 65 20 74 68  tr() to store th
1b4d9 65 0a 2a 2a 20 72 65 73 75 6c 74 20 61 73 20 61  e.** result as a
1b4da 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20   string or blob 
1b4db 62 75 74 20 69 66 20 74 68 65 20 73 74 72 69 6e  but if the strin
1b4dc 67 20 6f 72 20 62 6c 6f 62 20 69 73 20 74 6f 6f  g or blob is too
1b4dd 20 6c 61 72 67 65 2c 20 69 74 0a 2a 2a 20 74 68   large, it.** th
1b4de 65 6e 20 73 65 74 73 20 74 68 65 20 65 72 72 6f  en sets the erro
1b4df 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49 54 45  r code to SQLITE
1b4e0 5f 54 4f 4f 42 49 47 0a 2a 2f 0a 73 74 61 74 69  _TOOBIG.*/.stati
1b4e1 63 20 76 6f 69 64 20 73 65 74 52 65 73 75 6c 74  c void setResult
1b4e2 53 74 72 4f 72 45 72 72 6f 72 28 0a 20 20 73 71  StrOrError(.  sq
1b4e3 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
1b4e4 43 74 78 2c 20 20 2f 2a 20 46 75 6e 63 74 69 6f  Ctx,  /* Functio
1b4e5 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63  n context */.  c
1b4e6 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 20 20  onst char *z,   
1b4e7 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67         /* String
1b4e8 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   pointer */.  in
1b4e9 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  t n,            
1b4ea 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 69        /* Bytes i
1b4eb 6e 20 73 74 72 69 6e 67 2c 20 6f 72 20 6e 65 67  n string, or neg
1b4ec 61 74 69 76 65 20 2a 2f 0a 20 20 75 38 20 65 6e  ative */.  u8 en
1b4ed 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c,              
1b4ee 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 6f     /* Encoding o
1b4ef 66 20 7a 2e 20 20 30 20 66 6f 72 20 42 4c 4f 42  f z.  0 for BLOB
1b4f0 73 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44  s */.  void (*xD
1b4f1 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 20 20 2f  el)(void*)     /
1b4f2 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 75 6e  * Destructor fun
1b4f3 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 66  ction */.){.  if
1b4f4 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  ( sqlite3VdbeMem
1b4f5 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c  SetStr(&pCtx->s,
1b4f6 20 7a 2c 20 6e 2c 20 65 6e 63 2c 20 78 44 65 6c   z, n, enc, xDel
1b4f7 29 3d 3d 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47  )==SQLITE_TOOBIG
1b4f8 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
1b4f9 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f  result_error_too
1b4fa 62 69 67 28 70 43 74 78 29 3b 0a 20 20 7d 0a 7d  big(pCtx);.  }.}
1b4fb 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
1b4fc 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1b4fd 62 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 5f  blob(.  sqlite3_
1b4fe 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a  context *pCtx, .
1b4ff 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c    const void *z,
1b500 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f   .  int n, .  vo
1b501 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20  id (*xDel)(void 
1b502 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20  *).){.  assert( 
1b503 6e 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  n>=0 );.  assert
1b504 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1b505 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d  held(pCtx->s.db-
1b506 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 65 74  >mutex) );.  set
1b507 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f 72  ResultStrOrError
1b508 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 30 2c 20  (pCtx, z, n, 0, 
1b509 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  xDel);.}.SQLITE_
1b50a 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
1b50b 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73  _result_double(s
1b50c 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1b50d 70 43 74 78 2c 20 64 6f 75 62 6c 65 20 72 56 61  pCtx, double rVa
1b50e 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  l){.  assert( sq
1b50f 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1b510 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74  (pCtx->s.db->mut
1b511 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ex) );.  sqlite3
1b512 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65  VdbeMemSetDouble
1b513 28 26 70 43 74 78 2d 3e 73 2c 20 72 56 61 6c 29  (&pCtx->s, rVal)
1b514 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  ;.}.SQLITE_API v
1b515 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
1b516 6c 74 5f 65 72 72 6f 72 28 73 71 6c 69 74 65 33  lt_error(sqlite3
1b517 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
1b518 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
1b519 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74 28  nt n){.  assert(
1b51a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1b51b 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e  eld(pCtx->s.db->
1b51c 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74 78  mutex) );.  pCtx
1b51d 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49  ->isError = SQLI
1b51e 54 45 5f 45 52 52 4f 52 3b 0a 20 20 73 71 6c 69  TE_ERROR;.  sqli
1b51f 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72  te3VdbeMemSetStr
1b520 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c  (&pCtx->s, z, n,
1b521 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51   SQLITE_UTF8, SQ
1b522 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
1b523 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  .}.#ifndef SQLIT
1b524 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c  E_OMIT_UTF16.SQL
1b525 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
1b526 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
1b527 72 31 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  r16(sqlite3_cont
1b528 65 78 74 20 2a 70 43 74 78 2c 20 63 6f 6e 73 74  ext *pCtx, const
1b529 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 29   void *z, int n)
1b52a 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  {.  assert( sqli
1b52b 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1b52c 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78  Ctx->s.db->mutex
1b52d 29 20 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45  ) );.  pCtx->isE
1b52e 72 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f 45 52  rror = SQLITE_ER
1b52f 52 4f 52 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ROR;.  sqlite3Vd
1b530 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74  beMemSetStr(&pCt
1b531 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49  x->s, z, n, SQLI
1b532 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20  TE_UTF16NATIVE, 
1b533 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
1b534 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 53 51 4c 49  );.}.#endif.SQLI
1b535 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
1b536 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 73  te3_result_int(s
1b537 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1b538 70 43 74 78 2c 20 69 6e 74 20 69 56 61 6c 29 7b  pCtx, int iVal){
1b539 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1b53a 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
1b53b 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29  tx->s.db->mutex)
1b53c 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62   );.  sqlite3Vdb
1b53d 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 70 43  eMemSetInt64(&pC
1b53e 74 78 2d 3e 73 2c 20 28 69 36 34 29 69 56 61 6c  tx->s, (i64)iVal
1b53f 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  );.}.SQLITE_API 
1b540 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
1b541 75 6c 74 5f 69 6e 74 36 34 28 73 71 6c 69 74 65  ult_int64(sqlite
1b542 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c  3_context *pCtx,
1b543 20 69 36 34 20 69 56 61 6c 29 7b 0a 20 20 61 73   i64 iVal){.  as
1b544 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1b545 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
1b546 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
1b547 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1b548 65 74 49 6e 74 36 34 28 26 70 43 74 78 2d 3e 73  etInt64(&pCtx->s
1b549 2c 20 69 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54  , iVal);.}.SQLIT
1b54a 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
1b54b 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 73  e3_result_null(s
1b54c 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1b54d 70 43 74 78 29 7b 0a 20 20 61 73 73 65 72 74 28  pCtx){.  assert(
1b54e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1b54f 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e  eld(pCtx->s.db->
1b550 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69  mutex) );.  sqli
1b551 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c  te3VdbeMemSetNul
1b552 6c 28 26 70 43 74 78 2d 3e 73 29 3b 0a 7d 0a 53  l(&pCtx->s);.}.S
1b553 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
1b554 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
1b555 78 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  xt(.  sqlite3_co
1b556 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20  ntext *pCtx, .  
1b557 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 0a  const char *z, .
1b558 20 20 69 6e 74 20 6e 2c 0a 20 20 76 6f 69 64 20    int n,.  void 
1b559 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a  (*xDel)(void *).
1b55a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
1b55b 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1b55c 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  pCtx->s.db->mute
1b55d 78 29 20 29 3b 0a 20 20 73 65 74 52 65 73 75 6c  x) );.  setResul
1b55e 74 53 74 72 4f 72 45 72 72 6f 72 28 70 43 74 78  tStrOrError(pCtx
1b55f 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55  , z, n, SQLITE_U
1b560 54 46 38 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23 69  TF8, xDel);.}.#i
1b561 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1b562 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41  T_UTF16.SQLITE_A
1b563 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
1b564 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 0a 20  result_text16(. 
1b565 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1b566 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74   *pCtx, .  const
1b567 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74   void *z, .  int
1b568 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44   n, .  void (*xD
1b569 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20  el)(void *).){. 
1b56a 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1b56b 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78  _mutex_held(pCtx
1b56c 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ->s.db->mutex) )
1b56d 3b 0a 20 20 73 65 74 52 65 73 75 6c 74 53 74 72  ;.  setResultStr
1b56e 4f 72 45 72 72 6f 72 28 70 43 74 78 2c 20 7a 2c  OrError(pCtx, z,
1b56f 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36   n, SQLITE_UTF16
1b570 4e 41 54 49 56 45 2c 20 78 44 65 6c 29 3b 0a 7d  NATIVE, xDel);.}
1b571 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
1b572 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1b573 74 65 78 74 31 36 62 65 28 0a 20 20 73 71 6c 69  text16be(.  sqli
1b574 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74  te3_context *pCt
1b575 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  x, .  const void
1b576 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a   *z, .  int n, .
1b577 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76    void (*xDel)(v
1b578 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65  oid *).){.  asse
1b579 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1b57a 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64  x_held(pCtx->s.d
1b57b 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73  b->mutex) );.  s
1b57c 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72  etResultStrOrErr
1b57d 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 53  or(pCtx, z, n, S
1b57e 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 78  QLITE_UTF16BE, x
1b57f 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  Del);.}.SQLITE_A
1b580 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
1b581 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 28  result_text16le(
1b582 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
1b583 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e  xt *pCtx, .  con
1b584 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69  st void *z, .  i
1b585 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a  nt n, .  void (*
1b586 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b  xDel)(void *).){
1b587 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1b588 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
1b589 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29  tx->s.db->mutex)
1b58a 20 29 3b 0a 20 20 73 65 74 52 65 73 75 6c 74 53   );.  setResultS
1b58b 74 72 4f 72 45 72 72 6f 72 28 70 43 74 78 2c 20  trOrError(pCtx, 
1b58c 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46  z, n, SQLITE_UTF
1b58d 31 36 4c 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23  16LE, xDel);.}.#
1b58e 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1b58f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51  OMIT_UTF16 */.SQ
1b590 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
1b591 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c  lite3_result_val
1b592 75 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ue(sqlite3_conte
1b593 78 74 20 2a 70 43 74 78 2c 20 73 71 6c 69 74 65  xt *pCtx, sqlite
1b594 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 29  3_value *pValue)
1b595 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  {.  assert( sqli
1b596 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1b597 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78  Ctx->s.db->mutex
1b598 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ) );.  sqlite3Vd
1b599 62 65 4d 65 6d 43 6f 70 79 28 26 70 43 74 78 2d  beMemCopy(&pCtx-
1b59a 3e 73 2c 20 70 56 61 6c 75 65 29 3b 0a 7d 0a 53  >s, pValue);.}.S
1b59b 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
1b59c 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65  qlite3_result_ze
1b59d 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 63  roblob(sqlite3_c
1b59e 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e  ontext *pCtx, in
1b59f 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74 28 20  t n){.  assert( 
1b5a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1b5a1 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d  ld(pCtx->s.db->m
1b5a2 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74  utex) );.  sqlit
1b5a3 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f  e3VdbeMemSetZero
1b5a4 42 6c 6f 62 28 26 70 43 74 78 2d 3e 73 2c 20 6e  Blob(&pCtx->s, n
1b5a5 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  );.}.SQLITE_API 
1b5a6 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
1b5a7 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28 73  ult_error_code(s
1b5a8 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1b5a9 70 43 74 78 2c 20 69 6e 74 20 65 72 72 43 6f 64  pCtx, int errCod
1b5aa 65 29 7b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72  e){.  pCtx->isEr
1b5ab 72 6f 72 20 3d 20 65 72 72 43 6f 64 65 3b 0a 20  ror = errCode;. 
1b5ac 20 69 66 28 20 70 43 74 78 2d 3e 73 2e 66 6c 61   if( pCtx->s.fla
1b5ad 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b  gs & MEM_Null ){
1b5ae 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1b5af 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d  MemSetStr(&pCtx-
1b5b0 3e 73 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74  >s, sqlite3ErrSt
1b5b1 72 28 65 72 72 43 6f 64 65 29 2c 20 2d 31 2c 20  r(errCode), -1, 
1b5b2 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1b5b3 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
1b5b4 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54  _UTF8, SQLITE_ST
1b5b5 41 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ATIC);.  }.}../*
1b5b6 20 46 6f 72 63 65 20 61 6e 20 53 51 4c 49 54 45   Force an SQLITE
1b5b7 5f 54 4f 4f 42 49 47 20 65 72 72 6f 72 2e 20 2a  _TOOBIG error. *
1b5b8 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
1b5b9 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
1b5ba 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 73 71  _error_toobig(sq
1b5bb 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
1b5bc 43 74 78 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Ctx){.  assert( 
1b5bd 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1b5be 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d  ld(pCtx->s.db->m
1b5bf 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74 78 2d  utex) );.  pCtx-
1b5c0 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 54  >isError = SQLIT
1b5c1 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 73 71 6c 69  E_TOOBIG;.  sqli
1b5c2 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72  te3VdbeMemSetStr
1b5c3 28 26 70 43 74 78 2d 3e 73 2c 20 22 73 74 72 69  (&pCtx->s, "stri
1b5c4 6e 67 20 6f 72 20 62 6c 6f 62 20 74 6f 6f 20 62  ng or blob too b
1b5c5 69 67 22 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20  ig", -1, .      
1b5c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b5c7 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51   SQLITE_UTF8, SQ
1b5c8 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a  LITE_STATIC);.}.
1b5c9 0a 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f 4e 4f  ./* An SQLITE_NO
1b5ca 4d 45 4d 20 65 72 72 6f 72 2e 20 2a 2f 0a 53 51  MEM error. */.SQ
1b5cb 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
1b5cc 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
1b5cd 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33  or_nomem(sqlite3
1b5ce 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 29 7b  _context *pCtx){
1b5cf 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1b5d0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
1b5d1 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29  tx->s.db->mutex)
1b5d2 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62   );.  sqlite3Vdb
1b5d3 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70 43 74  eMemSetNull(&pCt
1b5d4 78 2d 3e 73 29 3b 0a 20 20 70 43 74 78 2d 3e 69  x->s);.  pCtx->i
1b5d5 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f  sError = SQLITE_
1b5d6 4e 4f 4d 45 4d 3b 0a 20 20 70 43 74 78 2d 3e 73  NOMEM;.  pCtx->s
1b5d7 2e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  .db->mallocFaile
1b5d8 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  d = 1;.}../*.** 
1b5d9 45 78 65 63 75 74 65 20 74 68 65 20 73 74 61 74  Execute the stat
1b5da 65 6d 65 6e 74 20 70 53 74 6d 74 2c 20 65 69 74  ement pStmt, eit
1b5db 68 65 72 20 75 6e 74 69 6c 20 61 20 72 6f 77 20  her until a row 
1b5dc 6f 66 20 64 61 74 61 20 69 73 20 72 65 61 64 79  of data is ready
1b5dd 2c 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65  , the.** stateme
1b5de 6e 74 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79  nt is completely
1b5df 20 65 78 65 63 75 74 65 64 20 6f 72 20 61 6e 20   executed or an 
1b5e0 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a  error occurs..**
1b5e1 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1b5e2 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20   implements the 
1b5e3 62 75 6c 6b 20 6f 66 20 74 68 65 20 6c 6f 67 69  bulk of the logi
1b5e4 63 20 62 65 68 69 6e 64 20 74 68 65 20 73 71 6c  c behind the sql
1b5e5 69 74 65 5f 73 74 65 70 28 29 0a 2a 2a 20 41 50  ite_step().** AP
1b5e6 49 2e 20 20 54 68 65 20 6f 6e 6c 79 20 74 68 69  I.  The only thi
1b5e7 6e 67 20 6f 6d 69 74 74 65 64 20 69 73 20 74 68  ng omitted is th
1b5e8 65 20 61 75 74 6f 6d 61 74 69 63 20 72 65 63 6f  e automatic reco
1b5e9 6d 70 69 6c 65 20 69 66 20 61 20 0a 2a 2a 20 73  mpile if a .** s
1b5ea 63 68 65 6d 61 20 63 68 61 6e 67 65 20 68 61 73  chema change has
1b5eb 20 6f 63 63 75 72 72 65 64 2e 20 20 54 68 61 74   occurred.  That
1b5ec 20 64 65 74 61 69 6c 20 69 73 20 68 61 6e 64 6c   detail is handl
1b5ed 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 6f 75 74  ed by the.** out
1b5ee 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  er sqlite3_step(
1b5ef 29 20 77 72 61 70 70 65 72 20 70 72 6f 63 65 64  ) wrapper proced
1b5f0 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ure..*/.static i
1b5f1 6e 74 20 73 71 6c 69 74 65 33 53 74 65 70 28 56  nt sqlite3Step(V
1b5f2 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74  dbe *p){.  sqlit
1b5f3 65 33 20 2a 64 62 3b 0a 20 20 69 6e 74 20 72 63  e3 *db;.  int rc
1b5f4 3b 0a 0a 20 20 61 73 73 65 72 74 28 70 29 3b 0a  ;..  assert(p);.
1b5f5 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d    if( p->magic!=
1b5f6 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29  VDBE_MAGIC_RUN )
1b5f7 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
1b5f8 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a  ITE_MISUSE;.  }.
1b5f9 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61  .  /* Assert tha
1b5fa 74 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20 6e  t malloc() has n
1b5fb 6f 74 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20 64  ot failed */.  d
1b5fc 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28  b = p->db;.  if(
1b5fd 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1b5fe 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  d ){.    return 
1b5ff 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
1b600 7d 0a 0a 20 20 69 66 28 20 70 2d 3e 70 63 3c 3d  }..  if( p->pc<=
1b601 30 20 26 26 20 70 2d 3e 65 78 70 69 72 65 64 20  0 && p->expired 
1b602 29 7b 0a 20 20 20 20 69 66 28 20 41 4c 57 41 59  ){.    if( ALWAY
1b603 53 28 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f  S(p->rc==SQLITE_
1b604 4f 4b 29 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  OK) ){.      p->
1b605 72 63 20 3d 20 53 51 4c 49 54 45 5f 53 43 48 45  rc = SQLITE_SCHE
1b606 4d 41 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  MA;.    }.    rc
1b607 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
1b608 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  .    goto end_of
1b609 5f 73 74 65 70 3b 0a 20 20 7d 0a 20 20 69 66 28  _step;.  }.  if(
1b60a 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e   sqlite3SafetyOn
1b60b 28 64 62 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72  (db) ){.    p->r
1b60c 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  c = SQLITE_MISUS
1b60d 45 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  E;.    return SQ
1b60e 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d  LITE_MISUSE;.  }
1b60f 0a 20 20 69 66 28 20 70 2d 3e 70 63 3c 30 20 29  .  if( p->pc<0 )
1b610 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72  {.    /* If ther
1b611 65 20 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 73  e are no other s
1b612 74 61 74 65 6d 65 6e 74 73 20 63 75 72 72 65 6e  tatements curren
1b613 74 6c 79 20 72 75 6e 6e 69 6e 67 2c 20 74 68 65  tly running, the
1b614 6e 0a 20 20 20 20 2a 2a 20 72 65 73 65 74 20 74  n.    ** reset t
1b615 68 65 20 69 6e 74 65 72 72 75 70 74 20 66 6c 61  he interrupt fla
1b616 67 2e 20 20 54 68 69 73 20 70 72 65 76 65 6e 74  g.  This prevent
1b617 73 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  s a call to sqli
1b618 74 65 33 5f 69 6e 74 65 72 72 75 70 74 0a 20 20  te3_interrupt.  
1b619 20 20 2a 2a 20 66 72 6f 6d 20 69 6e 74 65 72 72    ** from interr
1b61a 75 70 74 69 6e 67 20 61 20 73 74 61 74 65 6d 65  upting a stateme
1b61b 6e 74 20 74 68 61 74 20 68 61 73 20 6e 6f 74 20  nt that has not 
1b61c 79 65 74 20 73 74 61 72 74 65 64 2e 0a 20 20 20  yet started..   
1b61d 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e   */.    if( db->
1b61e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3d 3d 30  activeVdbeCnt==0
1b61f 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 75 31   ){.      db->u1
1b620 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d  .isInterrupted =
1b621 20 30 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64   0;.    }..#ifnd
1b622 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
1b623 52 41 43 45 0a 20 20 20 20 69 66 28 20 64 62 2d  RACE.    if( db-
1b624 3e 78 50 72 6f 66 69 6c 65 20 26 26 20 21 64 62  >xProfile && !db
1b625 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20  ->init.busy ){. 
1b626 20 20 20 20 20 64 6f 75 62 6c 65 20 72 4e 6f 77       double rNow
1b627 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  ;.      sqlite3O
1b628 73 43 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d  sCurrentTime(db-
1b629 3e 70 56 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20  >pVfs, &rNow);. 
1b62a 20 20 20 20 20 70 2d 3e 73 74 61 72 74 54 69 6d       p->startTim
1b62b 65 20 3d 20 28 75 36 34 29 28 28 72 4e 6f 77 20  e = (u64)((rNow 
1b62c 2d 20 28 69 6e 74 29 72 4e 6f 77 29 2a 33 36 30  - (int)rNow)*360
1b62d 30 2e 30 2a 32 34 2e 30 2a 31 30 30 30 30 30 30  0.0*24.0*1000000
1b62e 30 30 30 2e 30 29 3b 0a 20 20 20 20 7d 0a 23 65  000.0);.    }.#e
1b62f 6e 64 69 66 0a 0a 20 20 20 20 64 62 2d 3e 61 63  ndif..    db->ac
1b630 74 69 76 65 56 64 62 65 43 6e 74 2b 2b 3b 0a 20  tiveVdbeCnt++;. 
1b631 20 20 20 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e     if( p->readOn
1b632 6c 79 3d 3d 30 20 29 20 64 62 2d 3e 77 72 69 74  ly==0 ) db->writ
1b633 65 56 64 62 65 43 6e 74 2b 2b 3b 0a 20 20 20 20  eVdbeCnt++;.    
1b634 70 2d 3e 70 63 20 3d 20 30 3b 0a 20 20 7d 0a 23  p->pc = 0;.  }.#
1b635 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1b636 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 69 66 28  IT_EXPLAIN.  if(
1b637 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29 7b 0a 20   p->explain ){. 
1b638 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
1b639 64 62 65 4c 69 73 74 28 70 29 3b 0a 20 20 7d 65  dbeList(p);.  }e
1b63a 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  lse.#endif /* SQ
1b63b 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49  LITE_OMIT_EXPLAI
1b63c 4e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 72 63 20  N */.  {.    rc 
1b63d 3d 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65  = sqlite3VdbeExe
1b63e 63 28 70 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  c(p);.  }..  if(
1b63f 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
1b640 66 28 64 62 29 20 29 7b 0a 20 20 20 20 72 63 20  f(db) ){.    rc 
1b641 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  = SQLITE_MISUSE;
1b642 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  .  }..#ifndef SQ
1b643 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a  LITE_OMIT_TRACE.
1b644 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20    /* Invoke the 
1b645 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b  profile callback
1b646 20 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65   if there is one
1b647 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d  .  */.  if( rc!=
1b648 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 64 62  SQLITE_ROW && db
1b649 2d 3e 78 50 72 6f 66 69 6c 65 20 26 26 20 21 64  ->xProfile && !d
1b64a 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20  b->init.busy && 
1b64b 70 2d 3e 7a 53 71 6c 20 29 7b 0a 20 20 20 20 64  p->zSql ){.    d
1b64c 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20 20 20  ouble rNow;.    
1b64d 75 36 34 20 65 6c 61 70 73 65 54 69 6d 65 3b 0a  u64 elapseTime;.
1b64e 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 75  .    sqlite3OsCu
1b64f 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56  rrentTime(db->pV
1b650 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 20 20  fs, &rNow);.    
1b651 65 6c 61 70 73 65 54 69 6d 65 20 3d 20 28 75 36  elapseTime = (u6
1b652 34 29 28 28 72 4e 6f 77 20 2d 20 28 69 6e 74 29  4)((rNow - (int)
1b653 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a 32 34 2e  rNow)*3600.0*24.
1b654 30 2a 31 30 30 30 30 30 30 30 30 30 2e 30 29 3b  0*1000000000.0);
1b655 0a 20 20 20 20 65 6c 61 70 73 65 54 69 6d 65 20  .    elapseTime 
1b656 2d 3d 20 70 2d 3e 73 74 61 72 74 54 69 6d 65 3b  -= p->startTime;
1b657 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f 66 69 6c  .    db->xProfil
1b658 65 28 64 62 2d 3e 70 50 72 6f 66 69 6c 65 41 72  e(db->pProfileAr
1b659 67 2c 20 70 2d 3e 7a 53 71 6c 2c 20 65 6c 61 70  g, p->zSql, elap
1b65a 73 65 54 69 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e  seTime);.  }.#en
1b65b 64 69 66 0a 0a 20 20 64 62 2d 3e 65 72 72 43 6f  dif..  db->errCo
1b65c 64 65 20 3d 20 72 63 3b 0a 20 20 69 66 28 20 53  de = rc;.  if( S
1b65d 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73 71 6c  QLITE_NOMEM==sql
1b65e 69 74 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64  ite3ApiExit(p->d
1b65f 62 2c 20 70 2d 3e 72 63 29 20 29 7b 0a 20 20 20  b, p->rc) ){.   
1b660 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f   p->rc = SQLITE_
1b661 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 65 6e 64 5f 6f  NOMEM;.  }.end_o
1b662 66 5f 73 74 65 70 3a 0a 20 20 2f 2a 20 41 74 20  f_step:.  /* At 
1b663 74 68 69 73 20 70 6f 69 6e 74 20 6c 6f 63 61 6c  this point local
1b664 20 76 61 72 69 61 62 6c 65 20 72 63 20 68 6f 6c   variable rc hol
1b665 64 73 20 74 68 65 20 76 61 6c 75 65 20 74 68 61  ds the value tha
1b666 74 20 73 68 6f 75 6c 64 20 62 65 20 0a 20 20 2a  t should be .  *
1b667 2a 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68  * returned if th
1b668 69 73 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73  is statement was
1b669 20 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e 67 20   compiled using 
1b66a 74 68 65 20 6c 65 67 61 63 79 20 0a 20 20 2a 2a  the legacy .  **
1b66b 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
1b66c 28 29 20 69 6e 74 65 72 66 61 63 65 2e 20 41 63  () interface. Ac
1b66d 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 64  cording to the d
1b66e 6f 63 73 2c 20 74 68 69 73 20 63 61 6e 20 6f 6e  ocs, this can on
1b66f 6c 79 0a 20 20 2a 2a 20 62 65 20 6f 6e 65 20 6f  ly.  ** be one o
1b670 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20  f the values in 
1b671 74 68 65 20 66 69 72 73 74 20 61 73 73 65 72 74  the first assert
1b672 28 29 20 62 65 6c 6f 77 2e 20 56 61 72 69 61 62  () below. Variab
1b673 6c 65 20 70 2d 3e 72 63 20 0a 20 20 2a 2a 20 63  le p->rc .  ** c
1b674 6f 6e 74 61 69 6e 73 20 74 68 65 20 76 61 6c 75  ontains the valu
1b675 65 20 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20  e that would be 
1b676 72 65 74 75 72 6e 65 64 20 69 66 20 73 71 6c 69  returned if sqli
1b677 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20 0a  te3_finalize() .
1b678 20 20 2a 2a 20 77 65 72 65 20 63 61 6c 6c 65 64    ** were called
1b679 20 6f 6e 20 73 74 61 74 65 6d 65 6e 74 20 70 2e   on statement p.
1b67a 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
1b67b 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 20  rc==SQLITE_ROW  
1b67c 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  || rc==SQLITE_DO
1b67d 4e 45 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  NE   || rc==SQLI
1b67e 54 45 5f 45 52 52 4f 52 20 0a 20 20 20 20 20 20  TE_ERROR .      
1b67f 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42   || rc==SQLITE_B
1b680 55 53 59 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  USY || rc==SQLIT
1b681 45 5f 4d 49 53 55 53 45 0a 20 20 29 3b 0a 20 20  E_MISUSE.  );.  
1b682 61 73 73 65 72 74 28 20 70 2d 3e 72 63 21 3d 53  assert( p->rc!=S
1b683 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 70 2d 3e  QLITE_ROW && p->
1b684 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc!=SQLITE_DONE 
1b685 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69 73 50 72  );.  if( p->isPr
1b686 65 70 61 72 65 56 32 20 26 26 20 72 63 21 3d 53  epareV2 && rc!=S
1b687 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 72 63 21  QLITE_ROW && rc!
1b688 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a  =SQLITE_DONE ){.
1b689 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 73      /* If this s
1b68a 74 61 74 65 6d 65 6e 74 20 77 61 73 20 70 72 65  tatement was pre
1b68b 70 61 72 65 64 20 75 73 69 6e 67 20 73 71 6c 69  pared using sqli
1b68c 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
1b68d 2c 20 61 6e 64 20 61 6e 0a 20 20 20 20 2a 2a 20  , and an.    ** 
1b68e 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 65  error has occure
1b68f 64 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 74  d, then return t
1b690 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 6e  he error code in
1b691 20 70 2d 3e 72 63 20 74 6f 20 74 68 65 0a 20 20   p->rc to the.  
1b692 20 20 2a 2a 20 63 61 6c 6c 65 72 2e 20 53 65 74    ** caller. Set
1b693 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   the error code 
1b694 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
1b695 68 61 6e 64 6c 65 20 74 6f 20 74 68 65 20 73 61  handle to the sa
1b696 6d 65 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f  me value..    */
1b697 20 0a 20 20 20 20 72 63 20 3d 20 64 62 2d 3e 65   .    rc = db->e
1b698 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72 63 3b 0a  rrCode = p->rc;.
1b699 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 72 63    }.  return (rc
1b69a 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3b 0a 7d  &db->errMask);.}
1b69b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ../*.** This is 
1b69c 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20 69 6d  the top-level im
1b69d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
1b69e 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20  sqlite3_step(). 
1b69f 20 43 61 6c 6c 0a 2a 2a 20 73 71 6c 69 74 65 33   Call.** sqlite3
1b6a0 53 74 65 70 28 29 20 74 6f 20 64 6f 20 6d 6f 73  Step() to do mos
1b6a1 74 20 6f 66 20 74 68 65 20 77 6f 72 6b 2e 20 20  t of the work.  
1b6a2 49 66 20 61 20 73 63 68 65 6d 61 20 65 72 72 6f  If a schema erro
1b6a3 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 63 61 6c  r occurs,.** cal
1b6a4 6c 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 61  l sqlite3Reprepa
1b6a5 72 65 28 29 20 61 6e 64 20 74 72 79 20 61 67 61  re() and try aga
1b6a6 69 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  in..*/.SQLITE_AP
1b6a7 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74  I int sqlite3_st
1b6a8 65 70 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  ep(sqlite3_stmt 
1b6a9 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72  *pStmt){.  int r
1b6aa 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  c = SQLITE_MISUS
1b6ab 45 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 20 29  E;.  if( pStmt )
1b6ac 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20  {.    int cnt = 
1b6ad 30 3b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d  0;.    Vdbe *v =
1b6ae 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20   (Vdbe*)pStmt;. 
1b6af 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d     sqlite3 *db =
1b6b0 20 76 2d 3e 64 62 3b 0a 20 20 20 20 73 71 6c 69   v->db;.    sqli
1b6b1 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
1b6b2 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20  db->mutex);.    
1b6b3 77 68 69 6c 65 28 20 28 72 63 20 3d 20 73 71 6c  while( (rc = sql
1b6b4 69 74 65 33 53 74 65 70 28 76 29 29 3d 3d 53 51  ite3Step(v))==SQ
1b6b5 4c 49 54 45 5f 53 43 48 45 4d 41 0a 20 20 20 20  LITE_SCHEMA.    
1b6b6 20 20 20 20 20 20 20 26 26 20 63 6e 74 2b 2b 20         && cnt++ 
1b6b7 3c 20 35 0a 20 20 20 20 20 20 20 20 20 20 20 26  < 5.           &
1b6b8 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 52  & (rc = sqlite3R
1b6b9 65 70 72 65 70 61 72 65 28 76 29 29 3d 3d 53 51  eprepare(v))==SQ
1b6ba 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1b6bb 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
1b6bc 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 76 2d 3e  Stmt);.      v->
1b6bd 65 78 70 69 72 65 64 20 3d 20 30 3b 0a 20 20 20  expired = 0;.   
1b6be 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53   }.    if( rc==S
1b6bf 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 26 26 20  QLITE_SCHEMA && 
1b6c0 41 4c 57 41 59 53 28 76 2d 3e 69 73 50 72 65 70  ALWAYS(v->isPrep
1b6c1 61 72 65 56 32 29 20 26 26 20 41 4c 57 41 59 53  areV2) && ALWAYS
1b6c2 28 64 62 2d 3e 70 45 72 72 29 20 29 7b 0a 20 20  (db->pErr) ){.  
1b6c3 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 73 65      /* This case
1b6c4 20 6f 63 63 75 72 73 20 61 66 74 65 72 20 66 61   occurs after fa
1b6c5 69 6c 69 6e 67 20 74 6f 20 72 65 63 6f 6d 70 69  iling to recompi
1b6c6 6c 65 20 61 6e 20 73 71 6c 20 73 74 61 74 65 6d  le an sql statem
1b6c7 65 6e 74 2e 20 0a 20 20 20 20 20 20 2a 2a 20 54  ent. .      ** T
1b6c8 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
1b6c9 20 66 72 6f 6d 20 74 68 65 20 53 51 4c 20 63 6f   from the SQL co
1b6ca 6d 70 69 6c 65 72 20 68 61 73 20 61 6c 72 65 61  mpiler has alrea
1b6cb 64 79 20 62 65 65 6e 20 6c 6f 61 64 65 64 20 0a  dy been loaded .
1b6cc 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68        ** into th
1b6cd 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
1b6ce 65 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f  e. This block co
1b6cf 70 69 65 73 20 74 68 65 20 65 72 72 6f 72 20 6d  pies the error m
1b6d0 65 73 73 61 67 65 20 0a 20 20 20 20 20 20 2a 2a  essage .      **
1b6d1 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61   from the databa
1b6d2 73 65 20 68 61 6e 64 6c 65 20 69 6e 74 6f 20 74  se handle into t
1b6d3 68 65 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64  he statement and
1b6d4 20 73 65 74 73 20 74 68 65 20 73 74 61 74 65 6d   sets the statem
1b6d5 65 6e 74 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f  ent.      ** pro
1b6d6 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 74 6f 20  gram counter to 
1b6d7 30 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74  0 to ensure that
1b6d8 20 77 68 65 6e 20 74 68 65 20 73 74 61 74 65 6d   when the statem
1b6d9 65 6e 74 20 69 73 20 0a 20 20 20 20 20 20 2a 2a  ent is .      **
1b6da 20 66 69 6e 61 6c 69 7a 65 64 20 6f 72 20 72 65   finalized or re
1b6db 73 65 74 20 74 68 65 20 70 61 72 73 65 72 20 65  set the parser e
1b6dc 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20  rror message is 
1b6dd 61 76 61 69 6c 61 62 6c 65 20 76 69 61 0a 20 20  available via.  
1b6de 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 65      ** sqlite3_e
1b6df 72 72 6d 73 67 28 29 20 61 6e 64 20 73 71 6c 69  rrmsg() and sqli
1b6e0 74 65 33 5f 65 72 72 63 6f 64 65 28 29 2e 0a 20  te3_errcode().. 
1b6e1 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f       */.      co
1b6e2 6e 73 74 20 63 68 61 72 20 2a 7a 45 72 72 20 3d  nst char *zErr =
1b6e3 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
1b6e4 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
1b6e5 74 28 64 62 2d 3e 70 45 72 72 29 3b 20 0a 20 20  t(db->pErr); .  
1b6e6 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1b6e7 65 28 64 62 2c 20 76 2d 3e 7a 45 72 72 4d 73 67  e(db, v->zErrMsg
1b6e8 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 64 62  );.      if( !db
1b6e9 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1b6ea 7b 0a 20 20 20 20 20 20 20 20 76 2d 3e 7a 45 72  {.        v->zEr
1b6eb 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 44 62  rMsg = sqlite3Db
1b6ec 53 74 72 44 75 70 28 64 62 2c 20 7a 45 72 72 29  StrDup(db, zErr)
1b6ed 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b  ;.      } else {
1b6ee 0a 20 20 20 20 20 20 20 20 76 2d 3e 7a 45 72 72  .        v->zErr
1b6ef 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  Msg = 0;.       
1b6f0 20 76 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f   v->rc = SQLITE_
1b6f1 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20  NOMEM;.      }. 
1b6f2 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71     }.    rc = sq
1b6f3 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c  lite3ApiExit(db,
1b6f4 20 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   rc);.    sqlite
1b6f5 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
1b6f6 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20  ->mutex);.  }.  
1b6f7 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1b6f8 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20  .** Extract the 
1b6f9 75 73 65 72 20 64 61 74 61 20 66 72 6f 6d 20 61  user data from a
1b6fa 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1b6fb 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 72   structure and r
1b6fc 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74  eturn a.** point
1b6fd 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c  er to it..*/.SQL
1b6fe 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71  ITE_API void *sq
1b6ff 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28  lite3_user_data(
1b700 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1b701 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
1b702 20 26 26 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a   && p->pFunc );.
1b703 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 46 75 6e    return p->pFun
1b704 63 2d 3e 70 55 73 65 72 44 61 74 61 3b 0a 7d 0a  c->pUserData;.}.
1b705 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74  ./*.** Extract t
1b706 68 65 20 75 73 65 72 20 64 61 74 61 20 66 72 6f  he user data fro
1b707 6d 20 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  m a sqlite3_cont
1b708 65 78 74 20 73 74 72 75 63 74 75 72 65 20 61 6e  ext structure an
1b709 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f  d return a.** po
1b70a 69 6e 74 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a  inter to it..*/.
1b70b 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
1b70c 65 33 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74  e3 *sqlite3_cont
1b70d 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71  ext_db_handle(sq
1b70e 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
1b70f 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26  ){.  assert( p &
1b710 26 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20  & p->pFunc );.  
1b711 72 65 74 75 72 6e 20 70 2d 3e 73 2e 64 62 3b 0a  return p->s.db;.
1b712 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  }../*.** The fol
1b713 6c 6f 77 69 6e 67 20 69 73 20 74 68 65 20 69 6d  lowing is the im
1b714 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
1b715 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  an SQL function 
1b716 74 68 61 74 20 61 6c 77 61 79 73 0a 2a 2a 20 66  that always.** f
1b717 61 69 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72  ails with an err
1b718 6f 72 20 6d 65 73 73 61 67 65 20 73 74 61 74 69  or message stati
1b719 6e 67 20 74 68 61 74 20 74 68 65 20 66 75 6e 63  ng that the func
1b71a 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e 20  tion is used in 
1b71b 74 68 65 0a 2a 2a 20 77 72 6f 6e 67 20 63 6f 6e  the.** wrong con
1b71c 74 65 78 74 2e 20 20 54 68 65 20 73 71 6c 69 74  text.  The sqlit
1b71d 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63  e3_overload_func
1b71e 74 69 6f 6e 28 29 20 41 50 49 20 6d 69 67 68 74  tion() API might
1b71f 20 63 6f 6e 73 74 72 75 63 74 0a 2a 2a 20 53 51   construct.** SQ
1b720 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20  L function that 
1b721 75 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65  use this routine
1b722 20 73 6f 20 74 68 61 74 20 74 68 65 20 66 75 6e   so that the fun
1b723 63 74 69 6f 6e 73 20 77 69 6c 6c 20 65 78 69 73  ctions will exis
1b724 74 0a 2a 2a 20 66 6f 72 20 6e 61 6d 65 20 72 65  t.** for name re
1b725 73 6f 6c 75 74 69 6f 6e 20 62 75 74 20 61 72 65  solution but are
1b726 20 61 63 74 75 61 6c 6c 79 20 6f 76 65 72 6c 6f   actually overlo
1b727 61 64 65 64 20 62 79 20 74 68 65 20 78 46 69 6e  aded by the xFin
1b728 64 46 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 65 74  dFunction.** met
1b729 68 6f 64 20 6f 66 20 76 69 72 74 75 61 6c 20 74  hod of virtual t
1b72a 61 62 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ables..*/.SQLITE
1b72b 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1b72c 6c 69 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63  lite3InvalidFunc
1b72d 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f  tion(.  sqlite3_
1b72e 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
1b72f 2c 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69  ,  /* The functi
1b730 6f 6e 20 63 61 6c 6c 69 6e 67 20 63 6f 6e 74 65  on calling conte
1b731 78 74 20 2a 2f 0a 20 20 69 6e 74 20 4e 6f 74 55  xt */.  int NotU
1b732 73 65 64 2c 20 20 20 20 20 20 20 20 20 20 20 20  sed,            
1b733 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1b734 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65  arguments to the
1b735 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73   function */.  s
1b736 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e  qlite3_value **N
1b737 6f 74 55 73 65 64 32 20 20 20 2f 2a 20 56 61 6c  otUsed2   /* Val
1b738 75 65 20 6f 66 20 65 61 63 68 20 61 72 67 75 6d  ue of each argum
1b739 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73  ent */.){.  cons
1b73a 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20  t char *zName = 
1b73b 63 6f 6e 74 65 78 74 2d 3e 70 46 75 6e 63 2d 3e  context->pFunc->
1b73c 7a 4e 61 6d 65 3b 0a 20 20 63 68 61 72 20 2a 7a  zName;.  char *z
1b73d 45 72 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  Err;.  UNUSED_PA
1b73e 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64  RAMETER2(NotUsed
1b73f 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 7a  , NotUsed2);.  z
1b740 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Err = sqlite3_mp
1b741 72 69 6e 74 66 28 0a 20 20 20 20 20 20 22 75 6e  rintf(.      "un
1b742 61 62 6c 65 20 74 6f 20 75 73 65 20 66 75 6e 63  able to use func
1b743 74 69 6f 6e 20 25 73 20 69 6e 20 74 68 65 20 72  tion %s in the r
1b744 65 71 75 65 73 74 65 64 20 63 6f 6e 74 65 78 74  equested context
1b745 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c  ", zName);.  sql
1b746 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
1b747 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c  r(context, zErr,
1b748 20 2d 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f   -1);.  sqlite3_
1b749 66 72 65 65 28 7a 45 72 72 29 3b 0a 7d 0a 0a 2f  free(zErr);.}../
1b74a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f 72  *.** Allocate or
1b74b 20 72 65 74 75 72 6e 20 74 68 65 20 61 67 67 72   return the aggr
1b74c 65 67 61 74 65 20 63 6f 6e 74 65 78 74 20 66 6f  egate context fo
1b74d 72 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f  r a user functio
1b74e 6e 2e 20 20 41 20 6e 65 77 0a 2a 2a 20 63 6f 6e  n.  A new.** con
1b74f 74 65 78 74 20 69 73 20 61 6c 6c 6f 63 61 74 65  text is allocate
1b750 64 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 63  d on the first c
1b751 61 6c 6c 2e 20 20 53 75 62 73 65 71 75 65 6e 74  all.  Subsequent
1b752 20 63 61 6c 6c 73 20 72 65 74 75 72 6e 20 74 68   calls return th
1b753 65 0a 2a 2a 20 73 61 6d 65 20 63 6f 6e 74 65 78  e.** same contex
1b754 74 20 74 68 61 74 20 77 61 73 20 72 65 74 75 72  t that was retur
1b755 6e 65 64 20 6f 6e 20 70 72 69 6f 72 20 63 61 6c  ned on prior cal
1b756 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ls..*/.SQLITE_AP
1b757 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  I void *sqlite3_
1b758 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
1b759 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  t(sqlite3_contex
1b75a 74 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 29  t *p, int nByte)
1b75b 7b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20  {.  Mem *pMem;. 
1b75c 20 61 73 73 65 72 74 28 20 70 20 26 26 20 70 2d   assert( p && p-
1b75d 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75  >pFunc && p->pFu
1b75e 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 61  nc->xStep );.  a
1b75f 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1b760 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 73 2e 64  utex_held(p->s.d
1b761 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70  b->mutex) );.  p
1b762 4d 65 6d 20 3d 20 70 2d 3e 70 4d 65 6d 3b 0a 20  Mem = p->pMem;. 
1b763 20 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67   if( (pMem->flag
1b764 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20  s & MEM_Agg)==0 
1b765 29 7b 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65  ){.    if( nByte
1b766 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
1b767 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
1b768 73 65 45 78 74 65 72 6e 61 6c 28 70 4d 65 6d 29  seExternal(pMem)
1b769 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  ;.      pMem->fl
1b76a 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a  ags = MEM_Null;.
1b76b 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20        pMem->z = 
1b76c 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  0;.    }else{.  
1b76d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1b76e 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79  emGrow(pMem, nBy
1b76f 74 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 4d  te, 0);.      pM
1b770 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
1b771 41 67 67 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  Agg;.      pMem-
1b772 3e 75 2e 70 44 65 66 20 3d 20 70 2d 3e 70 46 75  >u.pDef = p->pFu
1b773 6e 63 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d  nc;.      if( pM
1b774 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 20  em->z ){.       
1b775 20 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c   memset(pMem->z,
1b776 20 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20   0, nByte);.    
1b777 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
1b778 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 4d  return (void*)pM
1b779 65 6d 2d 3e 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  em->z;.}../*.** 
1b77a 52 65 74 75 72 6e 20 74 68 65 20 61 75 78 69 6c  Return the auxil
1b77b 61 72 79 20 64 61 74 61 20 70 6f 69 6e 74 65 72  ary data pointer
1b77c 2c 20 69 66 20 61 6e 79 2c 20 66 6f 72 20 74 68  , if any, for th
1b77d 65 20 69 41 72 67 27 74 68 20 61 72 67 75 6d 65  e iArg'th argume
1b77e 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 75 73 65  nt to.** the use
1b77f 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e  r-function defin
1b780 65 64 20 62 79 20 70 43 74 78 2e 0a 2a 2f 0a 53  ed by pCtx..*/.S
1b781 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
1b782 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64  sqlite3_get_auxd
1b783 61 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ata(sqlite3_cont
1b784 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 69  ext *pCtx, int i
1b785 41 72 67 29 7b 0a 20 20 56 64 62 65 46 75 6e 63  Arg){.  VdbeFunc
1b786 20 2a 70 56 64 62 65 46 75 6e 63 3b 0a 0a 20 20   *pVdbeFunc;..  
1b787 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1b788 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d  mutex_held(pCtx-
1b789 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  >s.db->mutex) );
1b78a 0a 20 20 70 56 64 62 65 46 75 6e 63 20 3d 20 70  .  pVdbeFunc = p
1b78b 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a  Ctx->pVdbeFunc;.
1b78c 20 20 69 66 28 20 21 70 56 64 62 65 46 75 6e 63    if( !pVdbeFunc
1b78d 20 7c 7c 20 69 41 72 67 3e 3d 70 56 64 62 65 46   || iArg>=pVdbeF
1b78e 75 6e 63 2d 3e 6e 41 75 78 20 7c 7c 20 69 41 72  unc->nAux || iAr
1b78f 67 3c 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  g<0 ){.    retur
1b790 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  n 0;.  }.  retur
1b791 6e 20 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41  n pVdbeFunc->apA
1b792 75 78 5b 69 41 72 67 5d 2e 70 41 75 78 3b 0a 7d  ux[iArg].pAux;.}
1b793 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
1b794 61 75 78 69 6c 61 72 79 20 64 61 74 61 20 70 6f  auxilary data po
1b795 69 6e 74 65 72 20 61 6e 64 20 64 65 6c 65 74 65  inter and delete
1b796 20 66 75 6e 63 74 69 6f 6e 2c 20 66 6f 72 20 74   function, for t
1b797 68 65 20 69 41 72 67 27 74 68 0a 2a 2a 20 61 72  he iArg'th.** ar
1b798 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 75 73  gument to the us
1b799 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66 69  er-function defi
1b79a 6e 65 64 20 62 79 20 70 43 74 78 2e 20 41 6e 79  ned by pCtx. Any
1b79b 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20   previous value 
1b79c 69 73 0a 2a 2a 20 64 65 6c 65 74 65 64 20 62 79  is.** deleted by
1b79d 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 64 65 6c   calling the del
1b79e 65 74 65 20 66 75 6e 63 74 69 6f 6e 20 73 70 65  ete function spe
1b79f 63 69 66 69 65 64 20 77 68 65 6e 20 69 74 20 77  cified when it w
1b7a0 61 73 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54  as set..*/.SQLIT
1b7a1 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
1b7a2 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 0a  e3_set_auxdata(.
1b7a3 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
1b7a4 74 20 2a 70 43 74 78 2c 20 0a 20 20 69 6e 74 20  t *pCtx, .  int 
1b7a5 69 41 72 67 2c 20 0a 20 20 76 6f 69 64 20 2a 70  iArg, .  void *p
1b7a6 41 75 78 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78  Aux, .  void (*x
1b7a7 44 65 6c 65 74 65 29 28 76 6f 69 64 2a 29 0a 29  Delete)(void*).)
1b7a8 7b 0a 20 20 73 74 72 75 63 74 20 41 75 78 44 61  {.  struct AuxDa
1b7a9 74 61 20 2a 70 41 75 78 44 61 74 61 3b 0a 20 20  ta *pAuxData;.  
1b7aa 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46  VdbeFunc *pVdbeF
1b7ab 75 6e 63 3b 0a 20 20 69 66 28 20 69 41 72 67 3c  unc;.  if( iArg<
1b7ac 30 20 29 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b  0 ) goto failed;
1b7ad 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
1b7ae 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1b7af 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78  Ctx->s.db->mutex
1b7b0 29 20 29 3b 0a 20 20 70 56 64 62 65 46 75 6e 63  ) );.  pVdbeFunc
1b7b1 20 3d 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75   = pCtx->pVdbeFu
1b7b2 6e 63 3b 0a 20 20 69 66 28 20 21 70 56 64 62 65  nc;.  if( !pVdbe
1b7b3 46 75 6e 63 20 7c 7c 20 70 56 64 62 65 46 75 6e  Func || pVdbeFun
1b7b4 63 2d 3e 6e 41 75 78 3c 3d 69 41 72 67 20 29 7b  c->nAux<=iArg ){
1b7b5 0a 20 20 20 20 69 6e 74 20 6e 41 75 78 20 3d 20  .    int nAux = 
1b7b6 28 70 56 64 62 65 46 75 6e 63 20 3f 20 70 56 64  (pVdbeFunc ? pVd
1b7b7 62 65 46 75 6e 63 2d 3e 6e 41 75 78 20 3a 20 30  beFunc->nAux : 0
1b7b8 29 3b 0a 20 20 20 20 69 6e 74 20 6e 4d 61 6c 6c  );.    int nMall
1b7b9 6f 63 20 3d 20 73 69 7a 65 6f 66 28 56 64 62 65  oc = sizeof(Vdbe
1b7ba 46 75 6e 63 29 20 2b 20 73 69 7a 65 6f 66 28 73  Func) + sizeof(s
1b7bb 74 72 75 63 74 20 41 75 78 44 61 74 61 29 2a 69  truct AuxData)*i
1b7bc 41 72 67 3b 0a 20 20 20 20 70 56 64 62 65 46 75  Arg;.    pVdbeFu
1b7bd 6e 63 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65  nc = sqlite3DbRe
1b7be 61 6c 6c 6f 63 28 70 43 74 78 2d 3e 73 2e 64 62  alloc(pCtx->s.db
1b7bf 2c 20 70 56 64 62 65 46 75 6e 63 2c 20 6e 4d 61  , pVdbeFunc, nMa
1b7c0 6c 6c 6f 63 29 3b 0a 20 20 20 20 69 66 28 20 21  lloc);.    if( !
1b7c1 70 56 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20  pVdbeFunc ){.   
1b7c2 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a     goto failed;.
1b7c3 20 20 20 20 7d 0a 20 20 20 20 70 43 74 78 2d 3e      }.    pCtx->
1b7c4 70 56 64 62 65 46 75 6e 63 20 3d 20 70 56 64 62  pVdbeFunc = pVdb
1b7c5 65 46 75 6e 63 3b 0a 20 20 20 20 6d 65 6d 73 65  eFunc;.    memse
1b7c6 74 28 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70  t(&pVdbeFunc->ap
1b7c7 41 75 78 5b 6e 41 75 78 5d 2c 20 30 2c 20 73 69  Aux[nAux], 0, si
1b7c8 7a 65 6f 66 28 73 74 72 75 63 74 20 41 75 78 44  zeof(struct AuxD
1b7c9 61 74 61 29 2a 28 69 41 72 67 2b 31 2d 6e 41 75  ata)*(iArg+1-nAu
1b7ca 78 29 29 3b 0a 20 20 20 20 70 56 64 62 65 46 75  x));.    pVdbeFu
1b7cb 6e 63 2d 3e 6e 41 75 78 20 3d 20 69 41 72 67 2b  nc->nAux = iArg+
1b7cc 31 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63  1;.    pVdbeFunc
1b7cd 2d 3e 70 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e  ->pFunc = pCtx->
1b7ce 70 46 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20 70 41  pFunc;.  }..  pA
1b7cf 75 78 44 61 74 61 20 3d 20 26 70 56 64 62 65 46  uxData = &pVdbeF
1b7d0 75 6e 63 2d 3e 61 70 41 75 78 5b 69 41 72 67 5d  unc->apAux[iArg]
1b7d1 3b 0a 20 20 69 66 28 20 70 41 75 78 44 61 74 61  ;.  if( pAuxData
1b7d2 2d 3e 70 41 75 78 20 26 26 20 70 41 75 78 44 61  ->pAux && pAuxDa
1b7d3 74 61 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a 20  ta->xDelete ){. 
1b7d4 20 20 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65     pAuxData->xDe
1b7d5 6c 65 74 65 28 70 41 75 78 44 61 74 61 2d 3e 70  lete(pAuxData->p
1b7d6 41 75 78 29 3b 0a 20 20 7d 0a 20 20 70 41 75 78  Aux);.  }.  pAux
1b7d7 44 61 74 61 2d 3e 70 41 75 78 20 3d 20 70 41 75  Data->pAux = pAu
1b7d8 78 3b 0a 20 20 70 41 75 78 44 61 74 61 2d 3e 78  x;.  pAuxData->x
1b7d9 44 65 6c 65 74 65 20 3d 20 78 44 65 6c 65 74 65  Delete = xDelete
1b7da 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 66 61 69  ;.  return;..fai
1b7db 6c 65 64 3a 0a 20 20 69 66 28 20 78 44 65 6c 65  led:.  if( xDele
1b7dc 74 65 20 29 7b 0a 20 20 20 20 78 44 65 6c 65 74  te ){.    xDelet
1b7dd 65 28 70 41 75 78 29 3b 0a 20 20 7d 0a 7d 0a 0a  e(pAux);.  }.}..
1b7de 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1b7df 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f  MIT_DEPRECATED./
1b7e0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1b7e1 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20  number of times 
1b7e2 74 68 65 20 53 74 65 70 20 66 75 6e 63 74 69 6f  the Step functio
1b7e3 6e 20 6f 66 20 61 20 61 67 67 72 65 67 61 74 65  n of a aggregate
1b7e4 20 68 61 73 20 62 65 65 6e 20 0a 2a 2a 20 63 61   has been .** ca
1b7e5 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  lled..**.** This
1b7e6 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 65 70   function is dep
1b7e7 72 65 63 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74  recated.  Do not
1b7e8 20 75 73 65 20 69 74 20 66 6f 72 20 6e 65 77 20   use it for new 
1b7e9 63 6f 64 65 2e 20 20 49 74 20 69 73 0a 2a 2a 20  code.  It is.** 
1b7ea 70 72 6f 76 69 64 65 20 6f 6e 6c 79 20 74 6f 20  provide only to 
1b7eb 61 76 6f 69 64 20 62 72 65 61 6b 69 6e 67 20 6c  avoid breaking l
1b7ec 65 67 61 63 79 20 63 6f 64 65 2e 20 20 4e 65 77  egacy code.  New
1b7ed 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
1b7ee 69 6f 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  ion.** implement
1b7ef 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6b 65  ations should ke
1b7f0 65 70 20 74 68 65 69 72 20 6f 77 6e 20 63 6f 75  ep their own cou
1b7f1 6e 74 73 20 77 69 74 68 69 6e 20 74 68 65 69 72  nts within their
1b7f2 20 61 67 67 72 65 67 61 74 65 0a 2a 2a 20 63 6f   aggregate.** co
1b7f3 6e 74 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ntext..*/.SQLITE
1b7f4 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1b7f5 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74  _aggregate_count
1b7f6 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1b7f7 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
1b7f8 70 20 26 26 20 70 2d 3e 70 4d 65 6d 20 26 26 20  p && p->pMem && 
1b7f9 70 2d 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e 70  p->pFunc && p->p
1b7fa 46 75 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20  Func->xStep );. 
1b7fb 20 72 65 74 75 72 6e 20 70 2d 3e 70 4d 65 6d 2d   return p->pMem-
1b7fc 3e 6e 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  >n;.}.#endif../*
1b7fd 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
1b7fe 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
1b7ff 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
1b800 65 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 65  et for the state
1b801 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53  ment pStmt..*/.S
1b802 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1b803 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75  lite3_column_cou
1b804 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  nt(sqlite3_stmt 
1b805 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20  *pStmt){.  Vdbe 
1b806 2a 70 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70  *pVm = (Vdbe *)p
1b807 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 70  Stmt;.  return p
1b808 56 6d 20 3f 20 70 56 6d 2d 3e 6e 52 65 73 43 6f  Vm ? pVm->nResCo
1b809 6c 75 6d 6e 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a  lumn : 0;.}../*.
1b80a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
1b80b 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 61  mber of values a
1b80c 76 61 69 6c 61 62 6c 65 20 66 72 6f 6d 20 74 68  vailable from th
1b80d 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66  e current row of
1b80e 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c   the.** currentl
1b80f 79 20 65 78 65 63 75 74 69 6e 67 20 73 74 61 74  y executing stat
1b810 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a  ement pStmt..*/.
1b811 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1b812 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e  qlite3_data_coun
1b813 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  t(sqlite3_stmt *
1b814 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a  pStmt){.  Vdbe *
1b815 70 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53  pVm = (Vdbe *)pS
1b816 74 6d 74 3b 0a 20 20 69 66 28 20 70 56 6d 3d 3d  tmt;.  if( pVm==
1b817 30 20 7c 7c 20 70 56 6d 2d 3e 70 52 65 73 75 6c  0 || pVm->pResul
1b818 74 53 65 74 3d 3d 30 20 29 20 72 65 74 75 72 6e  tSet==0 ) return
1b819 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d   0;.  return pVm
1b81a 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 7d 0a  ->nResColumn;.}.
1b81b 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f  ../*.** Check to
1b81c 20 73 65 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69   see if column i
1b81d 43 6f 6c 20 6f 66 20 74 68 65 20 67 69 76 65 6e  Col of the given
1b81e 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 76 61   statement is va
1b81f 6c 69 64 2e 20 20 49 66 0a 2a 2a 20 69 74 20 69  lid.  If.** it i
1b820 73 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e  s, return a poin
1b821 74 65 72 20 74 6f 20 74 68 65 20 4d 65 6d 20 66  ter to the Mem f
1b822 6f 72 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  or the value of 
1b823 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20  that column..** 
1b824 49 66 20 69 43 6f 6c 20 69 73 20 6e 6f 74 20 76  If iCol is not v
1b825 61 6c 69 64 2c 20 72 65 74 75 72 6e 20 61 20 70  alid, return a p
1b826 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d 20  ointer to a Mem 
1b827 77 68 69 63 68 20 68 61 73 20 61 20 76 61 6c 75  which has a valu
1b828 65 0a 2a 2a 20 6f 66 20 4e 55 4c 4c 2e 0a 2a 2f  e.** of NULL..*/
1b829 0a 73 74 61 74 69 63 20 4d 65 6d 20 2a 63 6f 6c  .static Mem *col
1b82a 75 6d 6e 4d 65 6d 28 73 71 6c 69 74 65 33 5f 73  umnMem(sqlite3_s
1b82b 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1b82c 69 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d 3b  i){.  Vdbe *pVm;
1b82d 0a 20 20 69 6e 74 20 76 61 6c 73 3b 0a 20 20 4d  .  int vals;.  M
1b82e 65 6d 20 2a 70 4f 75 74 3b 0a 0a 20 20 70 56 6d  em *pOut;..  pVm
1b82f 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74   = (Vdbe *)pStmt
1b830 3b 0a 20 20 69 66 28 20 70 56 6d 20 26 26 20 70  ;.  if( pVm && p
1b831 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 21 3d  Vm->pResultSet!=
1b832 30 20 26 26 20 69 3c 70 56 6d 2d 3e 6e 52 65 73  0 && i<pVm->nRes
1b833 43 6f 6c 75 6d 6e 20 26 26 20 69 3e 3d 30 20 29  Column && i>=0 )
1b834 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  {.    sqlite3_mu
1b835 74 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64  tex_enter(pVm->d
1b836 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 76  b->mutex);.    v
1b837 61 6c 73 20 3d 20 73 71 6c 69 74 65 33 5f 64 61  als = sqlite3_da
1b838 74 61 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b  ta_count(pStmt);
1b839 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 70 56 6d  .    pOut = &pVm
1b83a 2d 3e 70 52 65 73 75 6c 74 53 65 74 5b 69 5d 3b  ->pResultSet[i];
1b83b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
1b83c 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 70 61   If the value pa
1b83d 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f  ssed as the seco
1b83e 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f  nd argument is o
1b83f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 72 65 74  ut of range, ret
1b840 75 72 6e 0a 20 20 20 20 2a 2a 20 61 20 70 6f 69  urn.    ** a poi
1b841 6e 74 65 72 20 74 6f 20 74 68 65 20 66 6f 6c 6c  nter to the foll
1b842 6f 77 69 6e 67 20 73 74 61 74 69 63 20 4d 65 6d  owing static Mem
1b843 20 6f 62 6a 65 63 74 20 77 68 69 63 68 20 63 6f   object which co
1b844 6e 74 61 69 6e 73 20 74 68 65 0a 20 20 20 20 2a  ntains the.    *
1b845 2a 20 76 61 6c 75 65 20 53 51 4c 20 4e 55 4c 4c  * value SQL NULL
1b846 2e 20 45 76 65 6e 20 74 68 6f 75 67 68 20 74 68  . Even though th
1b847 65 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 20  e Mem structure 
1b848 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65 6c 65 6d  contains an elem
1b849 65 6e 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74 79  ent.    ** of ty
1b84a 70 65 20 69 36 34 2c 20 6f 6e 20 63 65 72 74 61  pe i64, on certa
1b84b 69 6e 20 61 72 63 68 69 74 65 63 74 75 72 65 20  in architecture 
1b84c 28 78 38 36 29 20 77 69 74 68 20 63 65 72 74 61  (x86) with certa
1b84d 69 6e 20 63 6f 6d 70 69 6c 65 72 0a 20 20 20 20  in compiler.    
1b84e 2a 2a 20 73 77 69 74 63 68 65 73 20 28 2d 4f 73  ** switches (-Os
1b84f 29 2c 20 67 63 63 20 6d 61 79 20 61 6c 69 67 6e  ), gcc may align
1b850 20 74 68 69 73 20 4d 65 6d 20 6f 62 6a 65 63 74   this Mem object
1b851 20 6f 6e 20 61 20 34 2d 62 79 74 65 20 62 6f 75   on a 4-byte bou
1b852 6e 64 61 72 79 0a 20 20 20 20 2a 2a 20 69 6e 73  ndary.    ** ins
1b853 74 65 61 64 20 6f 66 20 61 6e 20 38 2d 62 79 74  tead of an 8-byt
1b854 65 20 6f 6e 65 2e 20 54 68 69 73 20 61 6c 6c 20  e one. This all 
1b855 77 6f 72 6b 73 20 66 69 6e 65 2c 20 65 78 63 65  works fine, exce
1b856 70 74 20 74 68 61 74 20 77 68 65 6e 0a 20 20 20  pt that when.   
1b857 20 2a 2a 20 72 75 6e 6e 69 6e 67 20 77 69 74 68   ** running with
1b858 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 64 65   SQLITE_DEBUG de
1b859 66 69 6e 65 64 20 74 68 65 20 53 51 4c 69 74 65  fined the SQLite
1b85a 20 63 6f 64 65 20 73 6f 6d 65 74 69 6d 65 73 20   code sometimes 
1b85b 61 73 73 65 72 74 28 29 73 0a 20 20 20 20 2a 2a  assert()s.    **
1b85c 20 74 68 61 74 20 61 20 4d 65 6d 20 73 74 72 75   that a Mem stru
1b85d 63 74 75 72 65 20 69 73 20 6c 6f 63 61 74 65 64  cture is located
1b85e 20 6f 6e 20 61 6e 20 38 2d 62 79 74 65 20 62 6f   on an 8-byte bo
1b85f 75 6e 64 61 72 79 2e 20 54 6f 20 70 72 65 76 65  undary. To preve
1b860 6e 74 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 61  nt.    ** this a
1b861 73 73 65 72 74 28 29 20 66 72 6f 6d 20 66 61 69  ssert() from fai
1b862 6c 69 6e 67 2c 20 77 68 65 6e 20 62 75 69 6c 64  ling, when build
1b863 69 6e 67 20 77 69 74 68 20 53 51 4c 49 54 45 5f  ing with SQLITE_
1b864 44 45 42 55 47 20 64 65 66 69 6e 65 64 0a 20 20  DEBUG defined.  
1b865 20 20 2a 2a 20 75 73 69 6e 67 20 67 63 63 2c 20    ** using gcc, 
1b866 66 6f 72 63 65 20 6e 75 6c 6c 4d 65 6d 20 74 6f  force nullMem to
1b867 20 62 65 20 38 2d 62 79 74 65 20 61 6c 69 67 6e   be 8-byte align
1b868 65 64 20 75 73 69 6e 67 20 74 68 65 20 6d 61 67  ed using the mag
1b869 69 63 61 6c 0a 20 20 20 20 2a 2a 20 5f 5f 61 74  ical.    ** __at
1b86a 74 72 69 62 75 74 65 5f 5f 28 28 61 6c 69 67 6e  tribute__((align
1b86b 65 64 28 38 29 29 29 20 6d 61 63 72 6f 2e 20 20  ed(8))) macro.  
1b86c 2a 2f 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f  */.    static co
1b86d 6e 73 74 20 4d 65 6d 20 6e 75 6c 6c 4d 65 6d 20  nst Mem nullMem 
1b86e 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
1b86f 49 54 45 5f 44 45 42 55 47 29 20 26 26 20 64 65  ITE_DEBUG) && de
1b870 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a  fined(__GNUC__).
1b871 20 20 20 20 20 20 5f 5f 61 74 74 72 69 62 75 74        __attribut
1b872 65 5f 5f 28 28 61 6c 69 67 6e 65 64 28 38 29 29  e__((aligned(8))
1b873 29 20 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  ) .#endif.      
1b874 3d 20 7b 7b 30 7d 2c 20 28 64 6f 75 62 6c 65 29  = {{0}, (double)
1b875 30 2c 20 30 2c 20 22 22 2c 20 30 2c 20 4d 45 4d  0, 0, "", 0, MEM
1b876 5f 4e 75 6c 6c 2c 20 53 51 4c 49 54 45 5f 4e 55  _Null, SQLITE_NU
1b877 4c 4c 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 0a  LL, 0, 0, 0 };..
1b878 20 20 20 20 69 66 28 20 70 56 6d 20 26 26 20 41      if( pVm && A
1b879 4c 57 41 59 53 28 70 56 6d 2d 3e 64 62 29 20 29  LWAYS(pVm->db) )
1b87a 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
1b87b 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d  mutex_enter(pVm-
1b87c 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  >db->mutex);.   
1b87d 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
1b87e 70 56 6d 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f  pVm->db, SQLITE_
1b87f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20 20 7d  RANGE, 0);.    }
1b880 0a 20 20 20 20 70 4f 75 74 20 3d 20 28 4d 65 6d  .    pOut = (Mem
1b881 2a 29 26 6e 75 6c 6c 4d 65 6d 3b 0a 20 20 7d 0a  *)&nullMem;.  }.
1b882 20 20 72 65 74 75 72 6e 20 70 4f 75 74 3b 0a 7d    return pOut;.}
1b883 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
1b884 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
1b885 61 66 74 65 72 20 69 6e 76 6f 6b 69 6e 67 20 61  after invoking a
1b886 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  n sqlite3_value_
1b887 58 58 58 20 66 75 6e 63 74 69 6f 6e 20 6f 6e 20  XXX function on 
1b888 61 20 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 76 61 6c  a .** column val
1b889 75 65 20 28 69 2e 65 2e 20 61 20 76 61 6c 75 65  ue (i.e. a value
1b88a 20 72 65 74 75 72 6e 65 64 20 62 79 20 65 76 61   returned by eva
1b88b 6c 75 61 74 69 6e 67 20 61 6e 20 53 51 4c 20 65  luating an SQL e
1b88c 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65  xpression in the
1b88d 0a 2a 2a 20 73 65 6c 65 63 74 20 6c 69 73 74 20  .** select list 
1b88e 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  of a SELECT stat
1b88f 65 6d 65 6e 74 29 20 74 68 61 74 20 6d 61 79 20  ement) that may 
1b890 63 61 75 73 65 20 61 20 6d 61 6c 6c 6f 63 28 29  cause a malloc()
1b891 20 66 61 69 6c 75 72 65 2e 20 49 66 20 0a 2a 2a   failure. If .**
1b892 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20 66 61   malloc() has fa
1b893 69 6c 65 64 2c 20 74 68 65 20 74 68 72 65 61 64  iled, the thread
1b894 73 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66  s mallocFailed f
1b895 6c 61 67 20 69 73 20 63 6c 65 61 72 65 64 20 61  lag is cleared a
1b896 6e 64 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a  nd the result.**
1b897 20 63 6f 64 65 20 6f 66 20 73 74 61 74 65 6d 65   code of stateme
1b898 6e 74 20 70 53 74 6d 74 20 73 65 74 20 74 6f 20  nt pStmt set to 
1b899 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2a  SQLITE_NOMEM..**
1b89a 0a 2a 2a 20 53 70 65 63 69 66 69 63 61 6c 6c 79  .** Specifically
1b89b 2c 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64  , this is called
1b89c 20 66 72 6f 6d 20 77 69 74 68 69 6e 3a 0a 2a 2a   from within:.**
1b89d 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f  .**     sqlite3_
1b89e 63 6f 6c 75 6d 6e 5f 69 6e 74 28 29 0a 2a 2a 20  column_int().** 
1b89f 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75      sqlite3_colu
1b8a0 6d 6e 5f 69 6e 74 36 34 28 29 0a 2a 2a 20 20 20  mn_int64().**   
1b8a1 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
1b8a2 5f 74 65 78 74 28 29 0a 2a 2a 20 20 20 20 20 73  _text().**     s
1b8a3 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
1b8a4 78 74 31 36 28 29 0a 2a 2a 20 20 20 20 20 73 71  xt16().**     sq
1b8a5 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 72 65 61  lite3_column_rea
1b8a6 6c 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  l().**     sqlit
1b8a7 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
1b8a8 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33  ).**     sqlite3
1b8a9 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28  _column_bytes16(
1b8aa 29 0a 2a 2a 0a 2a 2a 20 42 75 74 20 6e 6f 74 20  ).**.** But not 
1b8ab 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  for sqlite3_colu
1b8ac 6d 6e 5f 62 6c 6f 62 28 29 2c 20 77 68 69 63 68  mn_blob(), which
1b8ad 20 6e 65 76 65 72 20 63 61 6c 6c 73 20 6d 61 6c   never calls mal
1b8ae 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  loc()..*/.static
1b8af 20 76 6f 69 64 20 63 6f 6c 75 6d 6e 4d 61 6c 6c   void columnMall
1b8b0 6f 63 46 61 69 6c 75 72 65 28 73 71 6c 69 74 65  ocFailure(sqlite
1b8b1 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 0a 7b  3_stmt *pStmt).{
1b8b2 0a 20 20 2f 2a 20 49 66 20 6d 61 6c 6c 6f 63 28  .  /* If malloc(
1b8b3 29 20 66 61 69 6c 65 64 20 64 75 72 69 6e 67 20  ) failed during 
1b8b4 61 6e 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76  an encoding conv
1b8b5 65 72 73 69 6f 6e 20 77 69 74 68 69 6e 20 61 6e  ersion within an
1b8b6 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f  .  ** sqlite3_co
1b8b7 6c 75 6d 6e 5f 58 58 58 20 41 50 49 2c 20 74 68  lumn_XXX API, th
1b8b8 65 6e 20 73 65 74 20 74 68 65 20 72 65 74 75 72  en set the retur
1b8b9 6e 20 63 6f 64 65 20 6f 66 20 74 68 65 20 73 74  n code of the st
1b8ba 61 74 65 6d 65 6e 74 20 74 6f 0a 20 20 2a 2a 20  atement to.  ** 
1b8bb 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 54 68  SQLITE_NOMEM. Th
1b8bc 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 5f  e next call to _
1b8bd 73 74 65 70 28 29 20 28 69 66 20 61 6e 79 29 20  step() (if any) 
1b8be 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c 49  will return SQLI
1b8bf 54 45 5f 45 52 52 4f 52 0a 20 20 2a 2a 20 61 6e  TE_ERROR.  ** an
1b8c0 64 20 5f 66 69 6e 61 6c 69 7a 65 28 29 20 77 69  d _finalize() wi
1b8c1 6c 6c 20 72 65 74 75 72 6e 20 4e 4f 4d 45 4d 2e  ll return NOMEM.
1b8c2 0a 20 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70 20  .  */.  Vdbe *p 
1b8c3 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b  = (Vdbe *)pStmt;
1b8c4 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
1b8c5 70 2d 3e 72 63 20 3d 20 73 71 6c 69 74 65 33 41  p->rc = sqlite3A
1b8c6 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 70 2d  piExit(p->db, p-
1b8c7 3e 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  >rc);.    sqlite
1b8c8 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d  3_mutex_leave(p-
1b8c9 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  >db->mutex);.  }
1b8ca 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
1b8cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b8cc 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  * sqlite3_column
1b8cd 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  _  *************
1b8ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b8cf 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  **.** The follow
1b8d0 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ing routines are
1b8d1 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20   used to access 
1b8d2 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20  elements of the 
1b8d3 63 75 72 72 65 6e 74 20 72 6f 77 0a 2a 2a 20 69  current row.** i
1b8d4 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
1b8d5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1b8d6 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
1b8d7 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
1b8d8 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1b8d9 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63  tmt, int i){.  c
1b8da 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c 3b 0a  onst void *val;.
1b8db 20 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f    val = sqlite3_
1b8dc 76 61 6c 75 65 5f 62 6c 6f 62 28 20 63 6f 6c 75  value_blob( colu
1b8dd 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29  mnMem(pStmt,i) )
1b8de 3b 0a 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75  ;.  /* Even thou
1b8df 67 68 20 74 68 65 72 65 20 69 73 20 6e 6f 20 65  gh there is no e
1b8e0 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72 73 69  ncoding conversi
1b8e1 6f 6e 2c 20 76 61 6c 75 65 5f 62 6c 6f 62 28 29  on, value_blob()
1b8e2 20 6d 69 67 68 74 0a 20 20 2a 2a 20 6e 65 65 64   might.  ** need
1b8e3 20 74 6f 20 63 61 6c 6c 20 6d 61 6c 6c 6f 63 28   to call malloc(
1b8e4 29 20 74 6f 20 65 78 70 61 6e 64 20 74 68 65 20  ) to expand the 
1b8e5 72 65 73 75 6c 74 20 6f 66 20 61 20 7a 65 72 6f  result of a zero
1b8e6 62 6c 6f 62 28 29 20 0a 20 20 2a 2a 20 65 78 70  blob() .  ** exp
1b8e7 72 65 73 73 69 6f 6e 2e 20 0a 20 20 2a 2f 0a 20  ression. .  */. 
1b8e8 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69   columnMallocFai
1b8e9 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72  lure(pStmt);.  r
1b8ea 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c  eturn val;.}.SQL
1b8eb 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1b8ec 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
1b8ed 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1b8ee 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20  Stmt, int i){.  
1b8ef 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65  int val = sqlite
1b8f0 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 20 63  3_value_bytes( c
1b8f1 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69  olumnMem(pStmt,i
1b8f2 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c  ) );.  columnMal
1b8f3 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74  locFailure(pStmt
1b8f4 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b  );.  return val;
1b8f5 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  .}.SQLITE_API in
1b8f6 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t sqlite3_column
1b8f7 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74 65 33  _bytes16(sqlite3
1b8f8 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
1b8f9 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61 6c 20  t i){.  int val 
1b8fa 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
1b8fb 62 79 74 65 73 31 36 28 20 63 6f 6c 75 6d 6e 4d  bytes16( columnM
1b8fc 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20  em(pStmt,i) );. 
1b8fd 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69   columnMallocFai
1b8fe 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72  lure(pStmt);.  r
1b8ff 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c  eturn val;.}.SQL
1b900 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73  ITE_API double s
1b901 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f  qlite3_column_do
1b902 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  uble(sqlite3_stm
1b903 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
1b904 7b 0a 20 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d  {.  double val =
1b905 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
1b906 6f 75 62 6c 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d  ouble( columnMem
1b907 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63  (pStmt,i) );.  c
1b908 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75  olumnMallocFailu
1b909 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74  re(pStmt);.  ret
1b90a 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54  urn val;.}.SQLIT
1b90b 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1b90c 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c  3_column_int(sql
1b90d 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1b90e 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20  , int i){.  int 
1b90f 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  val = sqlite3_va
1b910 6c 75 65 5f 69 6e 74 28 20 63 6f 6c 75 6d 6e 4d  lue_int( columnM
1b911 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20  em(pStmt,i) );. 
1b912 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69   columnMallocFai
1b913 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72  lure(pStmt);.  r
1b914 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c  eturn val;.}.SQL
1b915 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 5f 69  ITE_API sqlite_i
1b916 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 63 6f 6c  nt64 sqlite3_col
1b917 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c 69 74 65  umn_int64(sqlite
1b918 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
1b919 6e 74 20 69 29 7b 0a 20 20 73 71 6c 69 74 65 5f  nt i){.  sqlite_
1b91a 69 6e 74 36 34 20 76 61 6c 20 3d 20 73 71 6c 69  int64 val = sqli
1b91b 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28  te3_value_int64(
1b91c 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74   columnMem(pStmt
1b91d 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d  ,i) );.  columnM
1b91e 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74  allocFailure(pSt
1b91f 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61  mt);.  return va
1b920 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  l;.}.SQLITE_API 
1b921 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
1b922 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  har *sqlite3_col
1b923 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74 65 33  umn_text(sqlite3
1b924 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
1b925 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e  t i){.  const un
1b926 73 69 67 6e 65 64 20 63 68 61 72 20 2a 76 61 6c  signed char *val
1b927 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
1b928 5f 74 65 78 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d  _text( columnMem
1b929 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63  (pStmt,i) );.  c
1b92a 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75  olumnMallocFailu
1b92b 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74  re(pStmt);.  ret
1b92c 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54  urn val;.}.SQLIT
1b92d 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76 61  E_API sqlite3_va
1b92e 6c 75 65 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  lue *sqlite3_col
1b92f 75 6d 6e 5f 76 61 6c 75 65 28 73 71 6c 69 74 65  umn_value(sqlite
1b930 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
1b931 6e 74 20 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 4f  nt i){.  Mem *pO
1b932 75 74 20 3d 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70  ut = columnMem(p
1b933 53 74 6d 74 2c 20 69 29 3b 0a 20 20 69 66 28 20  Stmt, i);.  if( 
1b934 70 4f 75 74 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pOut->flags&MEM_
1b935 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 70 4f  Static ){.    pO
1b936 75 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45  ut->flags &= ~ME
1b937 4d 5f 53 74 61 74 69 63 3b 0a 20 20 20 20 70 4f  M_Static;.    pO
1b938 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d  ut->flags |= MEM
1b939 5f 45 70 68 65 6d 3b 0a 20 20 7d 0a 20 20 63 6f  _Ephem;.  }.  co
1b93a 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72  lumnMallocFailur
1b93b 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75  e(pStmt);.  retu
1b93c 72 6e 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  rn (sqlite3_valu
1b93d 65 20 2a 29 70 4f 75 74 3b 0a 7d 0a 23 69 66 6e  e *)pOut;.}.#ifn
1b93e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1b93f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49  UTF16.SQLITE_API
1b940 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
1b941 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
1b942 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  16(sqlite3_stmt 
1b943 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a  *pStmt, int i){.
1b944 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61    const void *va
1b945 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  l = sqlite3_valu
1b946 65 5f 74 65 78 74 31 36 28 20 63 6f 6c 75 6d 6e  e_text16( column
1b947 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a  Mem(pStmt,i) );.
1b948 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61    columnMallocFa
1b949 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20  ilure(pStmt);.  
1b94a 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 23 65  return val;.}.#e
1b94b 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1b94c 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c  MIT_UTF16 */.SQL
1b94d 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1b94e 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28  te3_column_type(
1b94f 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1b950 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69  tmt, int i){.  i
1b951 6e 74 20 69 54 79 70 65 20 3d 20 73 71 6c 69 74  nt iType = sqlit
1b952 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 20 63  e3_value_type( c
1b953 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69  olumnMem(pStmt,i
1b954 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c  ) );.  columnMal
1b955 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74  locFailure(pStmt
1b956 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 54 79 70  );.  return iTyp
1b957 65 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c  e;.}../* The fol
1b958 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  lowing function 
1b959 69 73 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20  is experimental 
1b95a 61 6e 64 20 73 75 62 6a 65 63 74 20 74 6f 20 63  and subject to c
1b95b 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f  hange or.** remo
1b95c 76 61 6c 20 2a 2f 0a 2f 2a 69 6e 74 20 73 71 6c  val */./*int sql
1b95d 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 75 6d 65  ite3_column_nume
1b95e 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74 65 33  ric_type(sqlite3
1b95f 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
1b960 74 20 69 29 7b 0a 2a 2a 20 20 72 65 74 75 72 6e  t i){.**  return
1b961 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e   sqlite3_value_n
1b962 75 6d 65 72 69 63 5f 74 79 70 65 28 20 63 6f 6c  umeric_type( col
1b963 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20  umnMem(pStmt,i) 
1b964 29 3b 0a 2a 2a 7d 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  );.**}.*/../*.**
1b965 20 43 6f 6e 76 65 72 74 20 74 68 65 20 4e 2d 74   Convert the N-t
1b966 68 20 65 6c 65 6d 65 6e 74 20 6f 66 20 70 53 74  h element of pSt
1b967 6d 74 2d 3e 70 43 6f 6c 4e 61 6d 65 5b 5d 20 69  mt->pColName[] i
1b968 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 75 73 69  nto a string usi
1b969 6e 67 0a 2a 2a 20 78 46 75 6e 63 28 29 20 74 68  ng.** xFunc() th
1b96a 65 6e 20 72 65 74 75 72 6e 20 74 68 61 74 20 73  en return that s
1b96b 74 72 69 6e 67 2e 20 20 49 66 20 4e 20 69 73 20  tring.  If N is 
1b96c 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 72 65  out of range, re
1b96d 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68  turn 0..**.** Th
1b96e 65 72 65 20 61 72 65 20 75 70 20 74 6f 20 35 20  ere are up to 5 
1b96f 6e 61 6d 65 73 20 66 6f 72 20 65 61 63 68 20 63  names for each c
1b970 6f 6c 75 6d 6e 2e 20 20 75 73 65 54 79 70 65 20  olumn.  useType 
1b971 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69 63 68  determines which
1b972 0a 2a 2a 20 6e 61 6d 65 20 69 73 20 72 65 74 75  .** name is retu
1b973 72 6e 65 64 2e 20 20 48 65 72 65 20 61 72 65 20  rned.  Here are 
1b974 74 68 65 20 6e 61 6d 65 73 3a 0a 2a 2a 0a 2a 2a  the names:.**.**
1b975 20 20 20 20 30 20 20 20 20 20 20 54 68 65 20 63      0      The c
1b976 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61 73 20 69 74  olumn name as it
1b977 20 73 68 6f 75 6c 64 20 62 65 20 64 69 73 70 6c   should be displ
1b978 61 79 65 64 20 66 6f 72 20 6f 75 74 70 75 74 0a  ayed for output.
1b979 2a 2a 20 20 20 20 31 20 20 20 20 20 20 54 68 65  **    1      The
1b97a 20 64 61 74 61 74 79 70 65 20 6e 61 6d 65 20 66   datatype name f
1b97b 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a  or the column.**
1b97c 20 20 20 20 32 20 20 20 20 20 20 54 68 65 20 6e      2      The n
1b97d 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
1b97e 61 73 65 20 74 68 61 74 20 74 68 65 20 63 6f 6c  ase that the col
1b97f 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72 6f 6d  umn derives from
1b980 0a 2a 2a 20 20 20 20 33 20 20 20 20 20 20 54 68  .**    3      Th
1b981 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  e name of the ta
1b982 62 6c 65 20 74 68 61 74 20 74 68 65 20 63 6f 6c  ble that the col
1b983 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72 6f 6d  umn derives from
1b984 0a 2a 2a 20 20 20 20 34 20 20 20 20 20 20 54 68  .**    4      Th
1b985 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  e name of the ta
1b986 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20  ble column that 
1b987 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d  the result colum
1b988 6e 20 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a  n derives from.*
1b989 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75  *.** If the resu
1b98a 6c 74 20 69 73 20 6e 6f 74 20 61 20 73 69 6d 70  lt is not a simp
1b98b 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65  le column refere
1b98c 6e 63 65 20 28 69 66 20 69 74 20 69 73 20 61 6e  nce (if it is an
1b98d 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 6f   expression.** o
1b98e 72 20 61 20 63 6f 6e 73 74 61 6e 74 29 20 74 68  r a constant) th
1b98f 65 6e 20 75 73 65 54 79 70 65 73 20 32 2c 20 33  en useTypes 2, 3
1b990 2c 20 61 6e 64 20 34 20 72 65 74 75 72 6e 20 4e  , and 4 return N
1b991 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ULL..*/.static c
1b992 6f 6e 73 74 20 76 6f 69 64 20 2a 63 6f 6c 75 6d  onst void *colum
1b993 6e 4e 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33  nName(.  sqlite3
1b994 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 0a 20 20  _stmt *pStmt,.  
1b995 69 6e 74 20 4e 2c 0a 20 20 63 6f 6e 73 74 20 76  int N,.  const v
1b996 6f 69 64 20 2a 28 2a 78 46 75 6e 63 29 28 4d 65  oid *(*xFunc)(Me
1b997 6d 2a 29 2c 0a 20 20 69 6e 74 20 75 73 65 54 79  m*),.  int useTy
1b998 70 65 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f  pe.){.  const vo
1b999 69 64 20 2a 72 65 74 20 3d 20 30 3b 0a 20 20 56  id *ret = 0;.  V
1b99a 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a  dbe *p = (Vdbe *
1b99b 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 6e 3b  )pStmt;.  int n;
1b99c 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1b99d 20 70 2d 3e 64 62 3b 0a 20 20 0a 20 20 61 73 73   p->db;.  .  ass
1b99e 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20  ert( db!=0 );.  
1b99f 6e 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  n = sqlite3_colu
1b9a0 6d 6e 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b  mn_count(pStmt);
1b9a1 0a 20 20 69 66 28 20 4e 3c 6e 20 26 26 20 4e 3e  .  if( N<n && N>
1b9a2 3d 30 20 29 7b 0a 20 20 20 20 4e 20 2b 3d 20 75  =0 ){.    N += u
1b9a3 73 65 54 79 70 65 2a 6e 3b 0a 20 20 20 20 73 71  seType*n;.    sq
1b9a4 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
1b9a5 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
1b9a6 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61    assert( db->ma
1b9a7 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 29 3b  llocFailed==0 );
1b9a8 0a 20 20 20 20 72 65 74 20 3d 20 78 46 75 6e 63  .    ret = xFunc
1b9a9 28 26 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b 4e 5d  (&p->aColName[N]
1b9aa 29 3b 0a 20 20 20 20 20 2f 2a 20 41 20 6d 61 6c  );.     /* A mal
1b9ab 6c 6f 63 20 6d 61 79 20 68 61 76 65 20 66 61 69  loc may have fai
1b9ac 6c 65 64 20 69 6e 73 69 64 65 20 6f 66 20 74 68  led inside of th
1b9ad 65 20 78 46 75 6e 63 28 29 20 63 61 6c 6c 2e 20  e xFunc() call. 
1b9ae 49 66 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 69  If this.    ** i
1b9af 73 20 74 68 65 20 63 61 73 65 2c 20 63 6c 65 61  s the case, clea
1b9b0 72 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c  r the mallocFail
1b9b1 65 64 20 66 6c 61 67 20 61 6e 64 20 72 65 74 75  ed flag and retu
1b9b2 72 6e 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a  rn NULL..    */.
1b9b3 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c      if( db->mall
1b9b4 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
1b9b5 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
1b9b6 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65  ed = 0;.      re
1b9b7 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  t = 0;.    }.   
1b9b8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
1b9b9 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
1b9ba 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65  .  }.  return re
1b9bb 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  t;.}../*.** Retu
1b9bc 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  rn the name of t
1b9bd 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  he Nth column of
1b9be 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
1b9bf 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 0a  returned by SQL.
1b9c0 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74  ** statement pSt
1b9c1 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  mt..*/.SQLITE_AP
1b9c2 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  I const char *sq
1b9c3 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d  lite3_column_nam
1b9c4 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
1b9c5 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20  pStmt, int N){. 
1b9c6 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61   return columnNa
1b9c7 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c  me(.      pStmt,
1b9c8 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a   N, (const void*
1b9c9 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65  (*)(Mem*))sqlite
1b9ca 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f  3_value_text, CO
1b9cb 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b 0a 7d 0a 23  LNAME_NAME);.}.#
1b9cc 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1b9cd 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f  IT_UTF16.SQLITE_
1b9ce 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
1b9cf 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
1b9d0 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74  ame16(sqlite3_st
1b9d1 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e  mt *pStmt, int N
1b9d2 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75  ){.  return colu
1b9d3 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53  mnName(.      pS
1b9d4 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76  tmt, N, (const v
1b9d5 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71  oid*(*)(Mem*))sq
1b9d6 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1b9d7 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45  16, COLNAME_NAME
1b9d8 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  );.}.#endif../*.
1b9d9 2a 2a 20 43 6f 6e 73 74 72 61 69 6e 74 3a 20 20  ** Constraint:  
1b9da 49 66 20 79 6f 75 20 68 61 76 65 20 45 4e 41 42  If you have ENAB
1b9db 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41  LE_COLUMN_METADA
1b9dc 54 41 20 74 68 65 6e 20 79 6f 75 20 6d 75 73 74  TA then you must
1b9dd 0a 2a 2a 20 6e 6f 74 20 64 65 66 69 6e 65 20 4f  .** not define O
1b9de 4d 49 54 5f 44 45 43 4c 54 59 50 45 2e 0a 2a 2f  MIT_DECLTYPE..*/
1b9df 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
1b9e0 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50  ITE_OMIT_DECLTYP
1b9e1 45 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51  E) && defined(SQ
1b9e2 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55  LITE_ENABLE_COLU
1b9e3 4d 4e 5f 4d 45 54 41 44 41 54 41 29 0a 23 20 65  MN_METADATA).# e
1b9e4 72 72 6f 72 20 22 4d 75 73 74 20 6e 6f 74 20 64  rror "Must not d
1b9e5 65 66 69 6e 65 20 62 6f 74 68 20 53 51 4c 49 54  efine both SQLIT
1b9e6 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 20  E_OMIT_DECLTYPE 
1b9e7 5c 0a 20 20 20 20 20 20 20 20 20 61 6e 64 20 53  \.         and S
1b9e8 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c  QLITE_ENABLE_COL
1b9e9 55 4d 4e 5f 4d 45 54 41 44 41 54 41 22 0a 23 65  UMN_METADATA".#e
1b9ea 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
1b9eb 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59  LITE_OMIT_DECLTY
1b9ec 50 45 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  PE./*.** Return 
1b9ed 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61  the column decla
1b9ee 72 61 74 69 6f 6e 20 74 79 70 65 20 28 69 66 20  ration type (if 
1b9ef 61 70 70 6c 69 63 61 62 6c 65 29 20 6f 66 20 74  applicable) of t
1b9f0 68 65 20 27 69 27 74 68 20 63 6f 6c 75 6d 6e 0a  he 'i'th column.
1b9f1 2a 2a 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  ** of the result
1b9f2 20 73 65 74 20 6f 66 20 53 51 4c 20 73 74 61 74   set of SQL stat
1b9f3 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a  ement pStmt..*/.
1b9f4 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
1b9f5 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
1b9f6 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 28 73  olumn_decltype(s
1b9f7 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1b9f8 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65  mt, int N){.  re
1b9f9 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28  turn columnName(
1b9fa 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c  .      pStmt, N,
1b9fb 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29   (const void*(*)
1b9fc 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76  (Mem*))sqlite3_v
1b9fd 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41  alue_text, COLNA
1b9fe 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b 0a 7d 0a  ME_DECLTYPE);.}.
1b9ff 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1ba00 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45  MIT_UTF16.SQLITE
1ba01 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
1ba02 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
1ba03 64 65 63 6c 74 79 70 65 31 36 28 73 71 6c 69 74  decltype16(sqlit
1ba04 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1ba05 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e  int N){.  return
1ba06 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20   columnName(.   
1ba07 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f     pStmt, N, (co
1ba08 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d  nst void*(*)(Mem
1ba09 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *))sqlite3_value
1ba0a 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45  _text16, COLNAME
1ba0b 5f 44 45 43 4c 54 59 50 45 29 3b 0a 7d 0a 23 65  _DECLTYPE);.}.#e
1ba0c 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1ba0d 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 23 65 6e  MIT_UTF16 */.#en
1ba0e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1ba0f 49 54 5f 44 45 43 4c 54 59 50 45 20 2a 2f 0a 0a  IT_DECLTYPE */..
1ba10 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
1ba11 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41  ABLE_COLUMN_META
1ba12 44 41 54 41 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  DATA./*.** Retur
1ba13 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  n the name of th
1ba14 65 20 64 61 74 61 62 61 73 65 20 66 72 6f 6d 20  e database from 
1ba15 77 68 69 63 68 20 61 20 72 65 73 75 6c 74 20 63  which a result c
1ba16 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a  olumn derives..*
1ba17 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e  * NULL is return
1ba18 65 64 20 69 66 20 74 68 65 20 72 65 73 75 6c 74  ed if the result
1ba19 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78   column is an ex
1ba1a 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73  pression or cons
1ba1b 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68  tant or.** anyth
1ba1c 69 6e 67 20 65 6c 73 65 20 77 68 69 63 68 20 69  ing else which i
1ba1d 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69 67 75  s not an unabigu
1ba1e 6f 75 73 20 72 65 66 65 72 65 6e 63 65 20 74 6f  ous reference to
1ba1f 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6c 75   a database colu
1ba20 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  mn..*/.SQLITE_AP
1ba21 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  I const char *sq
1ba22 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74  lite3_column_dat
1ba23 61 62 61 73 65 5f 6e 61 6d 65 28 73 71 6c 69 74  abase_name(sqlit
1ba24 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1ba25 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e  int N){.  return
1ba26 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20   columnName(.   
1ba27 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f     pStmt, N, (co
1ba28 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d  nst void*(*)(Mem
1ba29 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *))sqlite3_value
1ba2a 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 44  _text, COLNAME_D
1ba2b 41 54 41 42 41 53 45 29 3b 0a 7d 0a 23 69 66 6e  ATABASE);.}.#ifn
1ba2c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1ba2d 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49  UTF16.SQLITE_API
1ba2e 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
1ba2f 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61  ite3_column_data
1ba30 62 61 73 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69  base_name16(sqli
1ba31 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1ba32 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72   int N){.  retur
1ba33 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20  n columnName(.  
1ba34 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63      pStmt, N, (c
1ba35 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65  onst void*(*)(Me
1ba36 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  m*))sqlite3_valu
1ba37 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d  e_text16, COLNAM
1ba38 45 5f 44 41 54 41 42 41 53 45 29 3b 0a 7d 0a 23  E_DATABASE);.}.#
1ba39 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1ba3a 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f  OMIT_UTF16 */../
1ba3b 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1ba3c 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
1ba3d 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61 20 72  e from which a r
1ba3e 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72  esult column der
1ba3f 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73  ives..** NULL is
1ba40 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65   returned if the
1ba41 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69   result column i
1ba42 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  s an expression 
1ba43 6f 72 20 63 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a  or constant or.*
1ba44 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 20  * anything else 
1ba45 77 68 69 63 68 20 69 73 20 6e 6f 74 20 61 6e 20  which is not an 
1ba46 75 6e 61 62 69 67 75 6f 75 73 20 72 65 66 65 72  unabiguous refer
1ba47 65 6e 63 65 20 74 6f 20 61 20 64 61 74 61 62 61  ence to a databa
1ba48 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51  se column..*/.SQ
1ba49 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63  LITE_API const c
1ba4a 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  har *sqlite3_col
1ba4b 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 28 73  umn_table_name(s
1ba4c 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1ba4d 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65  mt, int N){.  re
1ba4e 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28  turn columnName(
1ba4f 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c  .      pStmt, N,
1ba50 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29   (const void*(*)
1ba51 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76  (Mem*))sqlite3_v
1ba52 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41  alue_text, COLNA
1ba53 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23 69 66  ME_TABLE);.}.#if
1ba54 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1ba55 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50  _UTF16.SQLITE_AP
1ba56 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
1ba57 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62  lite3_column_tab
1ba58 6c 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65  le_name16(sqlite
1ba59 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
1ba5a 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20  nt N){.  return 
1ba5b 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20  columnName(.    
1ba5c 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e    pStmt, N, (con
1ba5d 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a  st void*(*)(Mem*
1ba5e 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ))sqlite3_value_
1ba5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f  text16, COLNAME_
1ba60 54 41 42 4c 45 29 3b 0a 7d 0a 23 65 6e 64 69 66  TABLE);.}.#endif
1ba61 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1ba62 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  UTF16 */../*.** 
1ba63 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20  Return the name 
1ba64 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c  of the table col
1ba65 75 6d 6e 20 66 72 6f 6d 20 77 68 69 63 68 20 61  umn from which a
1ba66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64   result column d
1ba67 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20  erives..** NULL 
1ba68 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74  is returned if t
1ba69 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  he result column
1ba6a 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f   is an expressio
1ba6b 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 6f 72  n or constant or
1ba6c 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c 73  .** anything els
1ba6d 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20 61  e which is not a
1ba6e 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 65 66  n unabiguous ref
1ba6f 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61 74 61  erence to a data
1ba70 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a  base column..*/.
1ba71 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
1ba72 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
1ba73 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d  olumn_origin_nam
1ba74 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
1ba75 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20  pStmt, int N){. 
1ba76 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61   return columnNa
1ba77 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c  me(.      pStmt,
1ba78 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a   N, (const void*
1ba79 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65  (*)(Mem*))sqlite
1ba7a 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f  3_value_text, CO
1ba7b 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b 0a 7d  LNAME_COLUMN);.}
1ba7c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1ba7d 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54  OMIT_UTF16.SQLIT
1ba7e 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
1ba7f 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
1ba80 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 28 73  _origin_name16(s
1ba81 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1ba82 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65  mt, int N){.  re
1ba83 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28  turn columnName(
1ba84 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c  .      pStmt, N,
1ba85 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29   (const void*(*)
1ba86 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76  (Mem*))sqlite3_v
1ba87 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c  alue_text16, COL
1ba88 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b 0a 7d 0a  NAME_COLUMN);.}.
1ba89 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1ba8a 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 23  _OMIT_UTF16 */.#
1ba8b 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1ba8c 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45  ENABLE_COLUMN_ME
1ba8d 54 41 44 41 54 41 20 2a 2f 0a 0a 0a 2f 2a 2a 2a  TADATA */.../***
1ba8e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ba8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c  ************ sql
1ba90 69 74 65 33 5f 62 69 6e 64 5f 20 20 2a 2a 2a 2a  ite3_bind_  ****
1ba91 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ba92 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 0a 2a 2a 20 52  *******.** .** R
1ba93 6f 75 74 69 6e 65 73 20 75 73 65 64 20 74 6f 20  outines used to 
1ba94 61 74 74 61 63 68 20 76 61 6c 75 65 73 20 74 6f  attach values to
1ba95 20 77 69 6c 64 63 61 72 64 73 20 69 6e 20 61 20   wildcards in a 
1ba96 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61  compiled SQL sta
1ba97 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a  tement..*/./*.**
1ba98 20 55 6e 62 69 6e 64 20 74 68 65 20 76 61 6c 75   Unbind the valu
1ba99 65 20 62 6f 75 6e 64 20 74 6f 20 76 61 72 69 61  e bound to varia
1ba9a 62 6c 65 20 69 20 69 6e 20 76 69 72 74 75 61 6c  ble i in virtual
1ba9b 20 6d 61 63 68 69 6e 65 20 70 2e 20 54 68 69 73   machine p. This
1ba9c 20 69 73 20 74 68 65 20 0a 2a 2a 20 74 68 65 20   is the .** the 
1ba9d 73 61 6d 65 20 61 73 20 62 69 6e 64 69 6e 67 20  same as binding 
1ba9e 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20 74 6f 20  a NULL value to 
1ba9f 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 49 66 20 74  the column. If t
1baa0 68 65 20 22 69 22 20 70 61 72 61 6d 65 74 65 72  he "i" parameter
1baa1 20 69 73 0a 2a 2a 20 6f 75 74 20 6f 66 20 72 61   is.** out of ra
1baa2 6e 67 65 2c 20 74 68 65 6e 20 53 51 4c 49 54 45  nge, then SQLITE
1baa3 5f 52 41 4e 47 45 20 69 73 20 72 65 74 75 72 6e  _RANGE is return
1baa4 65 64 2e 20 4f 74 68 65 77 69 73 65 20 53 51 4c  ed. Othewise SQL
1baa5 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 41 20  ITE_OK..**.** A 
1baa6 73 75 63 63 65 73 73 66 75 6c 20 65 76 61 6c 75  successful evalu
1baa7 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 72 6f  ation of this ro
1baa8 75 74 69 6e 65 20 61 63 71 75 69 72 65 73 20 74  utine acquires t
1baa9 68 65 20 6d 75 74 65 78 20 6f 6e 20 70 2e 0a 2a  he mutex on p..*
1baaa 2a 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 72  * the mutex is r
1baab 65 6c 65 61 73 65 64 20 69 66 20 61 6e 79 20 6b  eleased if any k
1baac 69 6e 64 20 6f 66 20 65 72 72 6f 72 20 6f 63 63  ind of error occ
1baad 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65  urs..**.** The e
1baae 72 72 6f 72 20 63 6f 64 65 20 73 74 6f 72 65 64  rror code stored
1baaf 20 69 6e 20 64 61 74 61 62 61 73 65 20 70 2d 3e   in database p->
1bab0 64 62 20 69 73 20 6f 76 65 72 77 72 69 74 74 65  db is overwritte
1bab1 6e 20 77 69 74 68 20 74 68 65 20 72 65 74 75 72  n with the retur
1bab2 6e 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20 61 6e  n.** value in an
1bab3 79 20 63 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69  y case..*/.stati
1bab4 63 20 69 6e 74 20 76 64 62 65 55 6e 62 69 6e 64  c int vdbeUnbind
1bab5 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 69 29  (Vdbe *p, int i)
1bab6 7b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20  {.  Mem *pVar;. 
1bab7 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
1bab8 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
1bab9 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
1baba 78 5f 65 6e 74 65 72 28 70 2d 3e 64 62 2d 3e 6d  x_enter(p->db->m
1babb 75 74 65 78 29 3b 0a 20 20 69 66 28 20 70 2d 3e  utex);.  if( p->
1babc 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49  magic!=VDBE_MAGI
1babd 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e 70 63 3e 3d  C_RUN || p->pc>=
1babe 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
1babf 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c  Error(p->db, SQL
1bac0 49 54 45 5f 4d 49 53 55 53 45 2c 20 30 29 3b 0a  ITE_MISUSE, 0);.
1bac1 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1bac2 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d  x_leave(p->db->m
1bac3 75 74 65 78 29 3b 0a 20 20 20 20 72 65 74 75 72  utex);.    retur
1bac4 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
1bac5 0a 20 20 7d 0a 20 20 69 66 28 20 69 3c 31 20 7c  .  }.  if( i<1 |
1bac6 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 29 7b 0a 20  | i>p->nVar ){. 
1bac7 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
1bac8 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 52 41  p->db, SQLITE_RA
1bac9 4e 47 45 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  NGE, 0);.    sql
1baca 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
1bacb 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  (p->db->mutex);.
1bacc 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1bacd 45 5f 52 41 4e 47 45 3b 0a 20 20 7d 0a 20 20 69  E_RANGE;.  }.  i
1bace 2d 2d 3b 0a 20 20 70 56 61 72 20 3d 20 26 70 2d  --;.  pVar = &p-
1bacf 3e 61 56 61 72 5b 69 5d 3b 0a 20 20 73 71 6c 69  >aVar[i];.  sqli
1bad0 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
1bad1 65 28 70 56 61 72 29 3b 0a 20 20 70 56 61 72 2d  e(pVar);.  pVar-
1bad2 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c  >flags = MEM_Nul
1bad3 6c 3b 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f  l;.  sqlite3Erro
1bad4 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f  r(p->db, SQLITE_
1bad5 4f 4b 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e  OK, 0);.  return
1bad6 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
1bad7 2a 0a 2a 2a 20 42 69 6e 64 20 61 20 74 65 78 74  *.** Bind a text
1bad8 20 6f 72 20 42 4c 4f 42 20 76 61 6c 75 65 2e 0a   or BLOB value..
1bad9 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 69  */.static int bi
1bada 6e 64 54 65 78 74 28 0a 20 20 73 71 6c 69 74 65  ndText(.  sqlite
1badb 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 20  3_stmt *pStmt,  
1badc 20 2f 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e   /* The statemen
1badd 74 20 74 6f 20 62 69 6e 64 20 61 67 61 69 6e 73  t to bind agains
1bade 74 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20  t */.  int i,   
1badf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1bae0 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 70 61   Index of the pa
1bae1 72 61 6d 65 74 65 72 20 74 6f 20 62 69 6e 64 20  rameter to bind 
1bae2 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
1bae3 2a 7a 44 61 74 61 2c 20 20 20 20 20 2f 2a 20 50  *zData,     /* P
1bae4 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 64 61  ointer to the da
1bae5 74 61 20 74 6f 20 62 65 20 62 6f 75 6e 64 20 2a  ta to be bound *
1bae6 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 20  /.  int nData,  
1bae7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1bae8 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
1bae9 20 64 61 74 61 20 74 6f 20 62 65 20 62 6f 75 6e   data to be boun
1baea 64 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44  d */.  void (*xD
1baeb 65 6c 29 28 76 6f 69 64 2a 29 2c 20 20 20 2f 2a  el)(void*),   /*
1baec 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20   Destructor for 
1baed 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 38  the data */.  u8
1baee 20 65 6e 63 6f 64 69 6e 67 20 20 20 20 20 20 20   encoding       
1baef 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67       /* Encoding
1baf0 20 66 6f 72 20 74 68 65 20 64 61 74 61 20 2a 2f   for the data */
1baf1 0a 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20  .){.  Vdbe *p = 
1baf2 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20  (Vdbe *)pStmt;. 
1baf3 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20 20 69 6e   Mem *pVar;.  in
1baf4 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 76 64  t rc;..  rc = vd
1baf5 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a  beUnbind(p, i);.
1baf6 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1baf7 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 7a  _OK ){.    if( z
1baf8 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 20  Data!=0 ){.     
1baf9 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72   pVar = &p->aVar
1bafa 5b 69 2d 31 5d 3b 0a 20 20 20 20 20 20 72 63 20  [i-1];.      rc 
1bafb 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  = sqlite3VdbeMem
1bafc 53 65 74 53 74 72 28 70 56 61 72 2c 20 7a 44 61  SetStr(pVar, zDa
1bafd 74 61 2c 20 6e 44 61 74 61 2c 20 65 6e 63 6f 64  ta, nData, encod
1bafe 69 6e 67 2c 20 78 44 65 6c 29 3b 0a 20 20 20 20  ing, xDel);.    
1baff 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1bb00 5f 4f 4b 20 26 26 20 65 6e 63 6f 64 69 6e 67 21  _OK && encoding!
1bb01 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  =0 ){.        rc
1bb02 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 68   = sqlite3VdbeCh
1bb03 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 56 61  angeEncoding(pVa
1bb04 72 2c 20 45 4e 43 28 70 2d 3e 64 62 29 29 3b 0a  r, ENC(p->db));.
1bb05 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
1bb06 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62  lite3Error(p->db
1bb07 2c 20 72 63 2c 20 30 29 3b 0a 20 20 20 20 20 20  , rc, 0);.      
1bb08 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45  rc = sqlite3ApiE
1bb09 78 69 74 28 70 2d 3e 64 62 2c 20 72 63 29 3b 0a  xit(p->db, rc);.
1bb0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
1bb0b 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d  3_mutex_leave(p-
1bb0c 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  >db->mutex);.  }
1bb0d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1bb0e 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20 61 20 62  ../*.** Bind a b
1bb0f 6c 6f 62 20 76 61 6c 75 65 20 74 6f 20 61 6e 20  lob value to an 
1bb10 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 76 61  SQL statement va
1bb11 72 69 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  riable..*/.SQLIT
1bb12 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1bb13 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 0a 20 20 73  3_bind_blob(.  s
1bb14 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1bb15 6d 74 2c 20 0a 20 20 69 6e 74 20 69 2c 20 0a 20  mt, .  int i, . 
1bb16 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 44 61   const void *zDa
1bb17 74 61 2c 20 0a 20 20 69 6e 74 20 6e 44 61 74 61  ta, .  int nData
1bb18 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c  , .  void (*xDel
1bb19 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 72 65  )(void*).){.  re
1bb1a 74 75 72 6e 20 62 69 6e 64 54 65 78 74 28 70 53  turn bindText(pS
1bb1b 74 6d 74 2c 20 69 2c 20 7a 44 61 74 61 2c 20 6e  tmt, i, zData, n
1bb1c 44 61 74 61 2c 20 78 44 65 6c 2c 20 30 29 3b 0a  Data, xDel, 0);.
1bb1d 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
1bb1e 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f   sqlite3_bind_do
1bb1f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  uble(sqlite3_stm
1bb20 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c  t *pStmt, int i,
1bb21 20 64 6f 75 62 6c 65 20 72 56 61 6c 75 65 29 7b   double rValue){
1bb22 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62  .  int rc;.  Vdb
1bb23 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70  e *p = (Vdbe *)p
1bb24 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62  Stmt;.  rc = vdb
1bb25 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20  eUnbind(p, i);. 
1bb26 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1bb27 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OK ){.    sqlite
1bb28 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c  3VdbeMemSetDoubl
1bb29 65 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c  e(&p->aVar[i-1],
1bb2a 20 72 56 61 6c 75 65 29 3b 0a 20 20 20 20 73 71   rValue);.    sq
1bb2b 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1bb2c 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  e(p->db->mutex);
1bb2d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1bb2e 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ;.}.SQLITE_API i
1bb2f 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
1bb30 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  int(sqlite3_stmt
1bb31 20 2a 70 2c 20 69 6e 74 20 69 2c 20 69 6e 74 20   *p, int i, int 
1bb32 69 56 61 6c 75 65 29 7b 0a 20 20 72 65 74 75 72  iValue){.  retur
1bb33 6e 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  n sqlite3_bind_i
1bb34 6e 74 36 34 28 70 2c 20 69 2c 20 28 69 36 34 29  nt64(p, i, (i64)
1bb35 69 56 61 6c 75 65 29 3b 0a 7d 0a 53 51 4c 49 54  iValue);.}.SQLIT
1bb36 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1bb37 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 71 6c  3_bind_int64(sql
1bb38 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1bb39 2c 20 69 6e 74 20 69 2c 20 73 71 6c 69 74 65 5f  , int i, sqlite_
1bb3a 69 6e 74 36 34 20 69 56 61 6c 75 65 29 7b 0a 20  int64 iValue){. 
1bb3b 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62 65 20   int rc;.  Vdbe 
1bb3c 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74  *p = (Vdbe *)pSt
1bb3d 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 65 55  mt;.  rc = vdbeU
1bb3e 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69  nbind(p, i);.  i
1bb3f 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1bb40 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1bb41 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 26  dbeMemSetInt64(&
1bb42 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c 20 69 56  p->aVar[i-1], iV
1bb43 61 6c 75 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  alue);.    sqlit
1bb44 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
1bb45 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  ->db->mutex);.  
1bb46 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1bb47 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1bb48 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c  sqlite3_bind_nul
1bb49 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  l(sqlite3_stmt *
1bb4a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20  pStmt, int i){. 
1bb4b 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62 65 20   int rc;.  Vdbe 
1bb4c 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d  *p = (Vdbe*)pStm
1bb4d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 65 55 6e  t;.  rc = vdbeUn
1bb4e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69 66  bind(p, i);.  if
1bb4f 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1bb50 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  ){.    sqlite3_m
1bb51 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62  utex_leave(p->db
1bb52 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20  ->mutex);.  }.  
1bb53 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c  return rc;.}.SQL
1bb54 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1bb55 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 20 0a  te3_bind_text( .
1bb56 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
1bb57 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69 2c  pStmt, .  int i,
1bb58 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   .  const char *
1bb59 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e 44  zData, .  int nD
1bb5a 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78  ata, .  void (*x
1bb5b 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20  Del)(void*).){. 
1bb5c 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78 74   return bindText
1bb5d 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74 61  (pStmt, i, zData
1bb5e 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20 53  , nData, xDel, S
1bb5f 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 23  QLITE_UTF8);.}.#
1bb60 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1bb61 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f  IT_UTF16.SQLITE_
1bb62 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1bb63 62 69 6e 64 5f 74 65 78 74 31 36 28 0a 20 20 73  bind_text16(.  s
1bb64 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1bb65 6d 74 2c 20 0a 20 20 69 6e 74 20 69 2c 20 0a 20  mt, .  int i, . 
1bb66 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 44 61   const void *zDa
1bb67 74 61 2c 20 0a 20 20 69 6e 74 20 6e 44 61 74 61  ta, .  int nData
1bb68 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c  , .  void (*xDel
1bb69 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 72 65  )(void*).){.  re
1bb6a 74 75 72 6e 20 62 69 6e 64 54 65 78 74 28 70 53  turn bindText(pS
1bb6b 74 6d 74 2c 20 69 2c 20 7a 44 61 74 61 2c 20 6e  tmt, i, zData, n
1bb6c 44 61 74 61 2c 20 78 44 65 6c 2c 20 53 51 4c 49  Data, xDel, SQLI
1bb6d 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29 3b  TE_UTF16NATIVE);
1bb6e 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1bb6f 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
1bb70 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1bb71 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61   sqlite3_bind_va
1bb72 6c 75 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  lue(sqlite3_stmt
1bb73 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20   *pStmt, int i, 
1bb74 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 61  const sqlite3_va
1bb75 6c 75 65 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20  lue *pValue){.  
1bb76 69 6e 74 20 72 63 3b 0a 20 20 73 77 69 74 63 68  int rc;.  switch
1bb77 28 20 70 56 61 6c 75 65 2d 3e 74 79 70 65 20 29  ( pValue->type )
1bb78 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
1bb79 45 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20  E_INTEGER: {.   
1bb7a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
1bb7b 62 69 6e 64 5f 69 6e 74 36 34 28 70 53 74 6d 74  bind_int64(pStmt
1bb7c 2c 20 69 2c 20 70 56 61 6c 75 65 2d 3e 75 2e 69  , i, pValue->u.i
1bb7d 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
1bb7e 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53      }.    case S
1bb7f 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 7b 0a 20  QLITE_FLOAT: {. 
1bb80 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1bb81 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28 70 53  3_bind_double(pS
1bb82 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65 2d 3e  tmt, i, pValue->
1bb83 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  r);.      break;
1bb84 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1bb85 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 20 7b 0a 20  SQLITE_BLOB: {. 
1bb86 20 20 20 20 20 69 66 28 20 70 56 61 6c 75 65 2d       if( pValue-
1bb87 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72  >flags & MEM_Zer
1bb88 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  o ){.        rc 
1bb89 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a  = sqlite3_bind_z
1bb8a 65 72 6f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 69  eroblob(pStmt, i
1bb8b 2c 20 70 56 61 6c 75 65 2d 3e 75 2e 6e 5a 65 72  , pValue->u.nZer
1bb8c 6f 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  o);.      }else{
1bb8d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
1bb8e 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28  lite3_bind_blob(
1bb8f 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65  pStmt, i, pValue
1bb90 2d 3e 7a 2c 20 70 56 61 6c 75 65 2d 3e 6e 2c 53  ->z, pValue->n,S
1bb91 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
1bb92 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1bb93 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1bb94 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 58   case SQLITE_TEX
1bb95 54 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  T: {.      rc = 
1bb96 62 69 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 69  bindText(pStmt,i
1bb97 2c 20 20 70 56 61 6c 75 65 2d 3e 7a 2c 20 70 56  ,  pValue->z, pV
1bb98 61 6c 75 65 2d 3e 6e 2c 20 53 51 4c 49 54 45 5f  alue->n, SQLITE_
1bb99 54 52 41 4e 53 49 45 4e 54 2c 0a 20 20 20 20 20  TRANSIENT,.     
1bb9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bb9b 20 20 20 20 20 20 20 20 20 70 56 61 6c 75 65 2d           pValue-
1bb9c 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 62 72 65  >enc);.      bre
1bb9d 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65  ak;.    }.    de
1bb9e 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72  fault: {.      r
1bb9f 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  c = sqlite3_bind
1bba0 5f 6e 75 6c 6c 28 70 53 74 6d 74 2c 20 69 29 3b  _null(pStmt, i);
1bba1 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1bba2 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
1bba3 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50   rc;.}.SQLITE_AP
1bba4 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
1bba5 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69  nd_zeroblob(sqli
1bba6 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1bba7 20 69 6e 74 20 69 2c 20 69 6e 74 20 6e 29 7b 0a   int i, int n){.
1bba8 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62 65    int rc;.  Vdbe
1bba9 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53   *p = (Vdbe *)pS
1bbaa 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 65  tmt;.  rc = vdbe
1bbab 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20  Unbind(p, i);.  
1bbac 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1bbad 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  K ){.    sqlite3
1bbae 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c  VdbeMemSetZeroBl
1bbaf 6f 62 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d  ob(&p->aVar[i-1]
1bbb0 2c 20 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , n);.    sqlite
1bbb1 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d  3_mutex_leave(p-
1bbb2 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  >db->mutex);.  }
1bbb3 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1bbb4 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
1bbb5 65 20 6e 75 6d 62 65 72 20 6f 66 20 77 69 6c 64  e number of wild
1bbb6 63 61 72 64 73 20 74 68 61 74 20 63 61 6e 20 62  cards that can b
1bbb7 65 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 62 6f  e potentially bo
1bbb8 75 6e 64 20 74 6f 2e 0a 2a 2a 20 54 68 69 73 20  und to..** This 
1bbb9 72 6f 75 74 69 6e 65 20 69 73 20 61 64 64 65 64  routine is added
1bbba 20 74 6f 20 73 75 70 70 6f 72 74 20 44 42 44 3a   to support DBD:
1bbbb 3a 53 51 4c 69 74 65 2e 20 20 0a 2a 2f 0a 53 51  :SQLite.  .*/.SQ
1bbbc 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1bbbd 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
1bbbe 74 65 72 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65  ter_count(sqlite
1bbbf 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a  3_stmt *pStmt){.
1bbc0 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62    Vdbe *p = (Vdb
1bbc1 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75  e*)pStmt;.  retu
1bbc2 72 6e 20 70 20 3f 20 70 2d 3e 6e 56 61 72 20 3a  rn p ? p->nVar :
1bbc3 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65   0;.}../*.** Cre
1bbc4 61 74 65 20 61 20 6d 61 70 70 69 6e 67 20 66 72  ate a mapping fr
1bbc5 6f 6d 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62  om variable numb
1bbc6 65 72 73 20 74 6f 20 76 61 72 69 61 62 6c 65 20  ers to variable 
1bbc7 6e 61 6d 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20  names.** in the 
1bbc8 56 64 62 65 2e 61 7a 56 61 72 5b 5d 20 61 72 72  Vdbe.azVar[] arr
1bbc9 61 79 2c 20 69 66 20 73 75 63 68 20 61 20 6d 61  ay, if such a ma
1bbca 70 70 69 6e 67 20 64 6f 65 73 20 6e 6f 74 20 61  pping does not a
1bbcb 6c 72 65 61 64 79 0a 2a 2a 20 65 78 69 73 74 2e  lready.** exist.
1bbcc 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1bbcd 63 72 65 61 74 65 56 61 72 4d 61 70 28 56 64 62  createVarMap(Vdb
1bbce 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 70 2d  e *p){.  if( !p-
1bbcf 3e 6f 6b 56 61 72 20 29 7b 0a 20 20 20 20 69 6e  >okVar ){.    in
1bbd0 74 20 6a 3b 0a 20 20 20 20 4f 70 20 2a 70 4f 70  t j;.    Op *pOp
1bbd1 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
1bbd2 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64 62 2d  tex_enter(p->db-
1bbd3 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 2f 2a 20  >mutex);.    /* 
1bbd4 54 68 65 20 72 61 63 65 20 63 6f 6e 64 69 74 69  The race conditi
1bbd5 6f 6e 20 68 65 72 65 20 69 73 20 68 61 72 6d 6c  on here is harml
1bbd6 65 73 73 2e 20 20 49 66 20 74 77 6f 20 74 68 72  ess.  If two thr
1bbd7 65 61 64 73 20 63 61 6c 6c 20 74 68 69 73 0a 20  eads call this. 
1bbd8 20 20 20 2a 2a 20 72 6f 75 74 69 6e 65 20 6f 6e     ** routine on
1bbd9 20 74 68 65 20 73 61 6d 65 20 56 64 62 65 20 61   the same Vdbe a
1bbda 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2c  t the same time,
1bbdb 20 74 68 65 79 20 62 6f 74 68 20 6d 69 67 68 74   they both might
1bbdc 20 65 6e 64 0a 20 20 20 20 2a 2a 20 75 70 20 69   end.    ** up i
1bbdd 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20  nitializing the 
1bbde 56 64 62 65 2e 61 7a 56 61 72 5b 5d 20 61 72 72  Vdbe.azVar[] arr
1bbdf 61 79 2e 20 20 54 68 61 74 20 69 73 20 61 20 6c  ay.  That is a l
1bbe0 69 74 74 6c 65 20 65 78 74 72 61 0a 20 20 20 20  ittle extra.    
1bbe1 2a 2a 20 77 6f 72 6b 20 62 75 74 20 69 74 20 72  ** work but it r
1bbe2 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20 73 61  esults in the sa
1bbe3 6d 65 20 61 6e 73 77 65 72 2e 0a 20 20 20 20 2a  me answer..    *
1bbe4 2f 0a 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70  /.    for(j=0, p
1bbe5 4f 70 3d 70 2d 3e 61 4f 70 3b 20 6a 3c 70 2d 3e  Op=p->aOp; j<p->
1bbe6 6e 4f 70 3b 20 6a 2b 2b 2c 20 70 4f 70 2b 2b 29  nOp; j++, pOp++)
1bbe7 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d  {.      if( pOp-
1bbe8 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 61 72 69  >opcode==OP_Vari
1bbe9 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20  able ){.        
1bbea 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1bbeb 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d  0 && pOp->p1<=p-
1bbec 3e 6e 56 61 72 20 29 3b 0a 20 20 20 20 20 20 20  >nVar );.       
1bbed 20 70 2d 3e 61 7a 56 61 72 5b 70 4f 70 2d 3e 70   p->azVar[pOp->p
1bbee 31 2d 31 5d 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a  1-1] = pOp->p4.z
1bbef 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1bbf0 20 20 20 20 70 2d 3e 6f 6b 56 61 72 20 3d 20 31      p->okVar = 1
1bbf1 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
1bbf2 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d  tex_leave(p->db-
1bbf3 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 7d 0a 0a  >mutex);.  }.}..
1bbf4 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
1bbf5 20 6e 61 6d 65 20 6f 66 20 61 20 77 69 6c 64 63   name of a wildc
1bbf6 61 72 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20  ard parameter.  
1bbf7 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74  Return NULL if t
1bbf8 68 65 20 69 6e 64 65 78 0a 2a 2a 20 69 73 20 6f  he index.** is o
1bbf9 75 74 20 6f 66 20 72 61 6e 67 65 20 6f 72 20 69  ut of range or i
1bbfa 66 20 74 68 65 20 77 69 6c 64 63 61 72 64 20 69  f the wildcard i
1bbfb 73 20 75 6e 6e 61 6d 65 64 2e 0a 2a 2a 0a 2a 2a  s unnamed..**.**
1bbfc 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 61   The result is a
1bbfd 6c 77 61 79 73 20 55 54 46 2d 38 2e 0a 2a 2f 0a  lways UTF-8..*/.
1bbfe 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
1bbff 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 62   char *sqlite3_b
1bc00 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61  ind_parameter_na
1bc01 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  me(sqlite3_stmt 
1bc02 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a  *pStmt, int i){.
1bc03 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62    Vdbe *p = (Vdb
1bc04 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69 66 28 20  e*)pStmt;.  if( 
1bc05 70 3d 3d 30 20 7c 7c 20 69 3c 31 20 7c 7c 20 69  p==0 || i<1 || i
1bc06 3e 70 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20  >p->nVar ){.    
1bc07 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
1bc08 63 72 65 61 74 65 56 61 72 4d 61 70 28 70 29 3b  createVarMap(p);
1bc09 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 61 7a 56  .  return p->azV
1bc0a 61 72 5b 69 2d 31 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a  ar[i-1];.}../*.*
1bc0b 2a 20 47 69 76 65 6e 20 61 20 77 69 6c 64 63 61  * Given a wildca
1bc0c 72 64 20 70 61 72 61 6d 65 74 65 72 20 6e 61 6d  rd parameter nam
1bc0d 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 69 6e  e, return the in
1bc0e 64 65 78 20 6f 66 20 74 68 65 20 76 61 72 69 61  dex of the varia
1bc0f 62 6c 65 0a 2a 2a 20 77 69 74 68 20 74 68 61 74  ble.** with that
1bc10 20 6e 61 6d 65 2e 20 20 49 66 20 74 68 65 72 65   name.  If there
1bc11 20 69 73 20 6e 6f 20 76 61 72 69 61 62 6c 65 20   is no variable 
1bc12 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 6e  with the given n
1bc13 61 6d 65 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 30  ame,.** return 0
1bc14 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1bc15 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
1bc16 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78  _parameter_index
1bc17 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1bc18 53 74 6d 74 2c 20 63 6f 6e 73 74 20 63 68 61 72  Stmt, const char
1bc19 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 56 64 62 65   *zName){.  Vdbe
1bc1a 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74   *p = (Vdbe*)pSt
1bc1b 6d 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  mt;.  int i;.  i
1bc1c 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72  f( p==0 ){.    r
1bc1d 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 63  eturn 0;.  }.  c
1bc1e 72 65 61 74 65 56 61 72 4d 61 70 28 70 29 3b 20  reateVarMap(p); 
1bc1f 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0a  .  if( zName ){.
1bc20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
1bc21 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20 20  ->nVar; i++){.  
1bc22 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
1bc23 7a 20 3d 20 70 2d 3e 61 7a 56 61 72 5b 69 5d 3b  z = p->azVar[i];
1bc24 0a 20 20 20 20 20 20 69 66 28 20 7a 20 26 26 20  .      if( z && 
1bc25 73 74 72 63 6d 70 28 7a 2c 7a 4e 61 6d 65 29 3d  strcmp(z,zName)=
1bc26 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  =0 ){.        re
1bc27 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20  turn i+1;.      
1bc28 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
1bc29 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
1bc2a 20 54 72 61 6e 73 66 65 72 20 61 6c 6c 20 62 69   Transfer all bi
1bc2b 6e 64 69 6e 67 73 20 66 72 6f 6d 20 74 68 65 20  ndings from the 
1bc2c 66 69 72 73 74 20 73 74 61 74 65 6d 65 6e 74 20  first statement 
1bc2d 6f 76 65 72 20 74 6f 20 74 68 65 20 73 65 63 6f  over to the seco
1bc2e 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nd..*/.SQLITE_PR
1bc2f 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1bc30 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67  3TransferBinding
1bc31 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  s(sqlite3_stmt *
1bc32 70 46 72 6f 6d 53 74 6d 74 2c 20 73 71 6c 69 74  pFromStmt, sqlit
1bc33 65 33 5f 73 74 6d 74 20 2a 70 54 6f 53 74 6d 74  e3_stmt *pToStmt
1bc34 29 7b 0a 20 20 56 64 62 65 20 2a 70 46 72 6f 6d  ){.  Vdbe *pFrom
1bc35 20 3d 20 28 56 64 62 65 2a 29 70 46 72 6f 6d 53   = (Vdbe*)pFromS
1bc36 74 6d 74 3b 0a 20 20 56 64 62 65 20 2a 70 54 6f  tmt;.  Vdbe *pTo
1bc37 20 3d 20 28 56 64 62 65 2a 29 70 54 6f 53 74 6d   = (Vdbe*)pToStm
1bc38 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73  t;.  int i;.  as
1bc39 73 65 72 74 28 20 70 54 6f 2d 3e 64 62 3d 3d 70  sert( pTo->db==p
1bc3a 46 72 6f 6d 2d 3e 64 62 20 29 3b 0a 20 20 61 73  From->db );.  as
1bc3b 73 65 72 74 28 20 70 54 6f 2d 3e 6e 56 61 72 3d  sert( pTo->nVar=
1bc3c 3d 70 46 72 6f 6d 2d 3e 6e 56 61 72 20 29 3b 0a  =pFrom->nVar );.
1bc3d 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1bc3e 65 6e 74 65 72 28 70 54 6f 2d 3e 64 62 2d 3e 6d  enter(pTo->db->m
1bc3f 75 74 65 78 29 3b 0a 20 20 66 6f 72 28 69 3d 30  utex);.  for(i=0
1bc40 3b 20 69 3c 70 46 72 6f 6d 2d 3e 6e 56 61 72 3b  ; i<pFrom->nVar;
1bc41 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74   i++){.    sqlit
1bc42 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 70  e3VdbeMemMove(&p
1bc43 54 6f 2d 3e 61 56 61 72 5b 69 5d 2c 20 26 70 46  To->aVar[i], &pF
1bc44 72 6f 6d 2d 3e 61 56 61 72 5b 69 5d 29 3b 0a 20  rom->aVar[i]);. 
1bc45 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   }.  sqlite3_mut
1bc46 65 78 5f 6c 65 61 76 65 28 70 54 6f 2d 3e 64 62  ex_leave(pTo->db
1bc47 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
1bc48 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
1bc49 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1bc4a 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a  OMIT_DEPRECATED.
1bc4b 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61 74 65 64  /*.** Deprecated
1bc4c 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66   external interf
1bc4d 61 63 65 2e 20 20 49 6e 74 65 72 6e 61 6c 2f 63  ace.  Internal/c
1bc4e 6f 72 65 20 53 51 4c 69 74 65 20 63 6f 64 65 0a  ore SQLite code.
1bc4f 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73  ** should call s
1bc50 71 6c 69 74 65 33 54 72 61 6e 73 66 65 72 42 69  qlite3TransferBi
1bc51 6e 64 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 49 73  ndings..**.** Is
1bc52 20 69 73 20 6d 69 73 75 73 65 20 74 6f 20 63 61   is misuse to ca
1bc53 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ll this routine 
1bc54 77 69 74 68 20 73 74 61 74 65 6d 65 6e 74 73 20  with statements 
1bc55 66 72 6f 6d 20 64 69 66 66 65 72 65 6e 74 0a 2a  from different.*
1bc56 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * database conne
1bc57 63 74 69 6f 6e 73 2e 20 20 42 75 74 20 61 73 20  ctions.  But as 
1bc58 74 68 69 73 20 69 73 20 61 20 64 65 70 72 65 63  this is a deprec
1bc59 61 74 65 64 20 69 6e 74 65 72 66 61 63 65 2c 20  ated interface, 
1bc5a 77 65 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62  we.** will not b
1bc5b 6f 74 68 65 72 20 74 6f 20 63 68 65 63 6b 20 66  other to check f
1bc5c 6f 72 20 74 68 61 74 20 63 6f 6e 64 69 74 69 6f  or that conditio
1bc5d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  n..**.** If the 
1bc5e 74 77 6f 20 73 74 61 74 65 6d 65 6e 74 73 20 63  two statements c
1bc5f 6f 6e 74 61 69 6e 20 61 20 64 69 66 66 65 72 65  ontain a differe
1bc60 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 6e  nt number of bin
1bc61 64 69 6e 67 73 2c 20 74 68 65 6e 0a 2a 2a 20 61  dings, then.** a
1bc62 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69  n SQLITE_ERROR i
1bc63 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4e 6f 74  s returned.  Not
1bc64 68 69 6e 67 20 65 6c 73 65 20 63 61 6e 20 67 6f  hing else can go
1bc65 20 77 72 6f 6e 67 2c 20 73 6f 20 6f 74 68 65 72   wrong, so other
1bc66 77 69 73 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f  wise.** SQLITE_O
1bc67 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  K is returned..*
1bc68 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1bc69 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73 66 65   sqlite3_transfe
1bc6a 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74  r_bindings(sqlit
1bc6b 65 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53 74  e3_stmt *pFromSt
1bc6c 6d 74 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  mt, sqlite3_stmt
1bc6d 20 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20 56 64   *pToStmt){.  Vd
1bc6e 62 65 20 2a 70 46 72 6f 6d 20 3d 20 28 56 64 62  be *pFrom = (Vdb
1bc6f 65 2a 29 70 46 72 6f 6d 53 74 6d 74 3b 0a 20 20  e*)pFromStmt;.  
1bc70 56 64 62 65 20 2a 70 54 6f 20 3d 20 28 56 64 62  Vdbe *pTo = (Vdb
1bc71 65 2a 29 70 54 6f 53 74 6d 74 3b 0a 20 20 69 66  e*)pToStmt;.  if
1bc72 28 20 70 46 72 6f 6d 2d 3e 6e 56 61 72 21 3d 70  ( pFrom->nVar!=p
1bc73 54 6f 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20  To->nVar ){.    
1bc74 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
1bc75 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ROR;.  }.  retur
1bc76 6e 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 65  n sqlite3Transfe
1bc77 72 42 69 6e 64 69 6e 67 73 28 70 46 72 6f 6d 53  rBindings(pFromS
1bc78 74 6d 74 2c 20 70 54 6f 53 74 6d 74 29 3b 0a 7d  tmt, pToStmt);.}
1bc79 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52  .#endif../*.** R
1bc7a 65 74 75 72 6e 20 74 68 65 20 73 71 6c 69 74 65  eturn the sqlite
1bc7b 33 2a 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  3* database hand
1bc7c 6c 65 20 74 6f 20 77 68 69 63 68 20 74 68 65 20  le to which the 
1bc7d 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
1bc7e 6e 74 20 67 69 76 65 6e 0a 2a 2a 20 69 6e 20 74  nt given.** in t
1bc7f 68 65 20 61 72 67 75 6d 65 6e 74 20 62 65 6c 6f  he argument belo
1bc80 6e 67 73 2e 20 20 54 68 69 73 20 69 73 20 74 68  ngs.  This is th
1bc81 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20  e same database 
1bc82 68 61 6e 64 6c 65 20 74 68 61 74 20 77 61 73 0a  handle that was.
1bc83 2a 2a 20 74 68 65 20 66 69 72 73 74 20 61 72 67  ** the first arg
1bc84 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 73 71 6c  ument to the sql
1bc85 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 74  ite3_prepare() t
1bc86 68 61 74 20 77 61 73 20 75 73 65 64 20 74 6f 20  hat was used to 
1bc87 63 72 65 61 74 65 0a 2a 2a 20 74 68 65 20 73 74  create.** the st
1bc88 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 66  atement in the f
1bc89 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2f 0a 53  irst place..*/.S
1bc8a 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
1bc8b 33 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f 68 61  3 *sqlite3_db_ha
1bc8c 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  ndle(sqlite3_stm
1bc8d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 72 65 74  t *pStmt){.  ret
1bc8e 75 72 6e 20 70 53 74 6d 74 20 3f 20 28 28 56 64  urn pStmt ? ((Vd
1bc8f 62 65 2a 29 70 53 74 6d 74 29 2d 3e 64 62 20 3a  be*)pStmt)->db :
1bc90 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74   0;.}../*.** Ret
1bc91 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
1bc92 20 74 68 65 20 6e 65 78 74 20 70 72 65 70 61 72   the next prepar
1bc93 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61 66 74  ed statement aft
1bc94 65 72 20 70 53 74 6d 74 20 61 73 73 6f 63 69 61  er pStmt associa
1bc95 74 65 64 0a 2a 2a 20 77 69 74 68 20 64 61 74 61  ted.** with data
1bc96 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
1bc97 70 44 62 2e 20 20 49 66 20 70 53 74 6d 74 20 69  pDb.  If pStmt i
1bc98 73 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 74  s NULL, return t
1bc99 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 72 65 70  he first.** prep
1bc9a 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 66  ared statement f
1bc9b 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20  or the database 
1bc9c 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 52 65 74  connection.  Ret
1bc9d 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65 72  urn NULL if ther
1bc9e 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 6d 6f 72 65  e.** are no more
1bc9f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1bca0 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 71  sqlite3_stmt *sq
1bca1 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28  lite3_next_stmt(
1bca2 73 71 6c 69 74 65 33 20 2a 70 44 62 2c 20 73 71  sqlite3 *pDb, sq
1bca3 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1bca4 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  t){.  sqlite3_st
1bca5 6d 74 20 2a 70 4e 65 78 74 3b 0a 20 20 73 71 6c  mt *pNext;.  sql
1bca6 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
1bca7 28 70 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  (pDb->mutex);.  
1bca8 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a  if( pStmt==0 ){.
1bca9 20 20 20 20 70 4e 65 78 74 20 3d 20 28 73 71 6c      pNext = (sql
1bcaa 69 74 65 33 5f 73 74 6d 74 2a 29 70 44 62 2d 3e  ite3_stmt*)pDb->
1bcab 70 56 64 62 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  pVdbe;.  }else{.
1bcac 20 20 20 20 70 4e 65 78 74 20 3d 20 28 73 71 6c      pNext = (sql
1bcad 69 74 65 33 5f 73 74 6d 74 2a 29 28 28 56 64 62  ite3_stmt*)((Vdb
1bcae 65 2a 29 70 53 74 6d 74 29 2d 3e 70 4e 65 78 74  e*)pStmt)->pNext
1bcaf 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
1bcb0 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 44 62 2d  mutex_leave(pDb-
1bcb1 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  >mutex);.  retur
1bcb2 6e 20 70 4e 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  n pNext;.}../*.*
1bcb3 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c  * Return the val
1bcb4 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 63  ue of a status c
1bcb5 6f 75 6e 74 65 72 20 66 6f 72 20 61 20 70 72 65  ounter for a pre
1bcb6 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a  pared statement.
1bcb7 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
1bcb8 74 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f 73  t sqlite3_stmt_s
1bcb9 74 61 74 75 73 28 73 71 6c 69 74 65 33 5f 73 74  tatus(sqlite3_st
1bcba 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 6f  mt *pStmt, int o
1bcbb 70 2c 20 69 6e 74 20 72 65 73 65 74 46 6c 61 67  p, int resetFlag
1bcbc 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 64 62 65  ){.  Vdbe *pVdbe
1bcbd 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b   = (Vdbe*)pStmt;
1bcbe 0a 20 20 69 6e 74 20 76 20 3d 20 70 56 64 62 65  .  int v = pVdbe
1bcbf 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f 70 2d 31 5d  ->aCounter[op-1]
1bcc0 3b 0a 20 20 69 66 28 20 72 65 73 65 74 46 6c 61  ;.  if( resetFla
1bcc1 67 20 29 20 70 56 64 62 65 2d 3e 61 43 6f 75 6e  g ) pVdbe->aCoun
1bcc2 74 65 72 5b 6f 70 2d 31 5d 20 3d 20 30 3b 0a 20  ter[op-1] = 0;. 
1bcc3 20 72 65 74 75 72 6e 20 76 3b 0a 7d 0a 0a 2f 2a   return v;.}../*
1bcc4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
1bcc5 64 20 6f 66 20 76 64 62 65 61 70 69 2e 63 20 2a  d of vdbeapi.c *
1bcc6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bcc7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bcc8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1bcc9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
1bcca 67 69 6e 20 66 69 6c 65 20 76 64 62 65 2e 63 20  gin file vdbe.c 
1bccb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bccc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bccd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1bcce 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
1bccf 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
1bcd0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
1bcd1 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
1bcd2 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
1bcd3 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
1bcd4 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
1bcd5 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
1bcd6 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
1bcd7 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
1bcd8 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
1bcd9 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
1bcda 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
1bcdb 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
1bcdc 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
1bcdd 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
1bcde 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
1bcdf 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
1bce0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
1bce1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bce2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bce3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bce4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bce5 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65  ****.** The code
1bce6 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 6d   in this file im
1bce7 70 6c 65 6d 65 6e 74 73 20 65 78 65 63 75 74 69  plements executi
1bce8 6f 6e 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65  on method of the
1bce9 20 0a 2a 2a 20 56 69 72 74 75 61 6c 20 44 61 74   .** Virtual Dat
1bcea 61 62 61 73 65 20 45 6e 67 69 6e 65 20 28 56 44  abase Engine (VD
1bceb 42 45 29 2e 20 20 41 20 73 65 70 61 72 61 74 65  BE).  A separate
1bcec 20 66 69 6c 65 20 28 22 76 64 62 65 61 75 78 2e   file ("vdbeaux.
1bced 63 22 29 0a 2a 2a 20 68 61 6e 64 6c 65 73 20 68  c").** handles h
1bcee 6f 75 73 65 6b 65 65 70 69 6e 67 20 64 65 74 61  ousekeeping deta
1bcef 69 6c 73 20 73 75 63 68 20 61 73 20 63 72 65 61  ils such as crea
1bcf0 74 69 6e 67 20 61 6e 64 20 64 65 6c 65 74 69 6e  ting and deletin
1bcf1 67 0a 2a 2a 20 56 44 42 45 20 69 6e 73 74 61 6e  g.** VDBE instan
1bcf2 63 65 73 2e 20 20 54 68 69 73 20 66 69 6c 65 20  ces.  This file 
1bcf3 69 73 20 73 6f 6c 65 6c 79 20 69 6e 74 65 72 65  is solely intere
1bcf4 73 74 65 64 20 69 6e 20 65 78 65 63 75 74 69 6e  sted in executin
1bcf5 67 0a 2a 2a 20 74 68 65 20 56 44 42 45 20 70 72  g.** the VDBE pr
1bcf6 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  ogram..**.** In 
1bcf7 74 68 65 20 65 78 74 65 72 6e 61 6c 20 69 6e 74  the external int
1bcf8 65 72 66 61 63 65 2c 20 61 6e 20 22 73 71 6c 69  erface, an "sqli
1bcf9 74 65 33 5f 73 74 6d 74 2a 22 20 69 73 20 61 6e  te3_stmt*" is an
1bcfa 20 6f 70 61 71 75 65 20 70 6f 69 6e 74 65 72 0a   opaque pointer.
1bcfb 2a 2a 20 74 6f 20 61 20 56 44 42 45 2e 0a 2a 2a  ** to a VDBE..**
1bcfc 0a 2a 2a 20 54 68 65 20 53 51 4c 20 70 61 72 73  .** The SQL pars
1bcfd 65 72 20 67 65 6e 65 72 61 74 65 73 20 61 20 70  er generates a p
1bcfe 72 6f 67 72 61 6d 20 77 68 69 63 68 20 69 73 20  rogram which is 
1bcff 74 68 65 6e 20 65 78 65 63 75 74 65 64 20 62 79  then executed by
1bd00 0a 2a 2a 20 74 68 65 20 56 44 42 45 20 74 6f 20  .** the VDBE to 
1bd01 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 74  do the work of t
1bd02 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
1bd03 2e 20 20 56 44 42 45 20 70 72 6f 67 72 61 6d 73  .  VDBE programs
1bd04 20 61 72 65 20 0a 2a 2a 20 73 69 6d 69 6c 61 72   are .** similar
1bd05 20 69 6e 20 66 6f 72 6d 20 74 6f 20 61 73 73 65   in form to asse
1bd06 6d 62 6c 79 20 6c 61 6e 67 75 61 67 65 2e 20 20  mbly language.  
1bd07 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 6e 73  The program cons
1bd08 69 73 74 73 20 6f 66 0a 2a 2a 20 61 20 6c 69 6e  ists of.** a lin
1bd09 65 61 72 20 73 65 71 75 65 6e 63 65 20 6f 66 20  ear sequence of 
1bd0a 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 45 61 63  operations.  Eac
1bd0b 68 20 6f 70 65 72 61 74 69 6f 6e 20 68 61 73 20  h operation has 
1bd0c 61 6e 20 6f 70 63 6f 64 65 20 0a 2a 2a 20 61 6e  an opcode .** an
1bd0d 64 20 35 20 6f 70 65 72 61 6e 64 73 2e 20 20 4f  d 5 operands.  O
1bd0e 70 65 72 61 6e 64 73 20 50 31 2c 20 50 32 2c 20  perands P1, P2, 
1bd0f 61 6e 64 20 50 33 20 61 72 65 20 69 6e 74 65 67  and P3 are integ
1bd10 65 72 73 2e 20 20 4f 70 65 72 61 6e 64 20 50 34  ers.  Operand P4
1bd11 20 0a 2a 2a 20 69 73 20 61 20 6e 75 6c 6c 2d 74   .** is a null-t
1bd12 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67  erminated string
1bd13 2e 20 20 4f 70 65 72 61 6e 64 20 50 35 20 69 73  .  Operand P5 is
1bd14 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 63 68 61   an unsigned cha
1bd15 72 61 63 74 65 72 2e 0a 2a 2a 20 46 65 77 20 6f  racter..** Few o
1bd16 70 63 6f 64 65 73 20 75 73 65 20 61 6c 6c 20 35  pcodes use all 5
1bd17 20 6f 70 65 72 61 6e 64 73 2e 0a 2a 2a 0a 2a 2a   operands..**.**
1bd18 20 43 6f 6d 70 75 74 61 74 69 6f 6e 20 72 65 73   Computation res
1bd19 75 6c 74 73 20 61 72 65 20 73 74 6f 72 65 64 20  ults are stored 
1bd1a 6f 6e 20 61 20 73 65 74 20 6f 66 20 72 65 67 69  on a set of regi
1bd1b 73 74 65 72 73 20 6e 75 6d 62 65 72 65 64 20 62  sters numbered b
1bd1c 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69 74 68  eginning.** with
1bd1d 20 31 20 61 6e 64 20 67 6f 69 6e 67 20 75 70 20   1 and going up 
1bd1e 74 6f 20 56 64 62 65 2e 6e 4d 65 6d 2e 20 20 45  to Vdbe.nMem.  E
1bd1f 61 63 68 20 72 65 67 69 73 74 65 72 20 63 61 6e  ach register can
1bd20 20 73 74 6f 72 65 0a 2a 2a 20 65 69 74 68 65 72   store.** either
1bd21 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 61 20 6e   an integer, a n
1bd22 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 73  ull-terminated s
1bd23 74 72 69 6e 67 2c 20 61 20 66 6c 6f 61 74 69 6e  tring, a floatin
1bd24 67 20 70 6f 69 6e 74 0a 2a 2a 20 6e 75 6d 62 65  g point.** numbe
1bd25 72 2c 20 6f 72 20 74 68 65 20 53 51 4c 20 22 4e  r, or the SQL "N
1bd26 55 4c 4c 22 20 76 61 6c 75 65 2e 20 20 41 6e 20  ULL" value.  An 
1bd27 69 6d 70 6c 69 63 69 74 20 63 6f 6e 76 65 72 73  implicit convers
1bd28 69 6f 6e 20 66 72 6f 6d 20 6f 6e 65 0a 2a 2a 20  ion from one.** 
1bd29 74 79 70 65 20 74 6f 20 74 68 65 20 6f 74 68 65  type to the othe
1bd2a 72 20 6f 63 63 75 72 73 20 61 73 20 6e 65 63 65  r occurs as nece
1bd2b 73 73 61 72 79 2e 0a 2a 2a 20 0a 2a 2a 20 4d 6f  ssary..** .** Mo
1bd2c 73 74 20 6f 66 20 74 68 65 20 63 6f 64 65 20 69  st of the code i
1bd2d 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 74  n this file is t
1bd2e 61 6b 65 6e 20 75 70 20 62 79 20 74 68 65 20 73  aken up by the s
1bd2f 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29  qlite3VdbeExec()
1bd30 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 68 69  .** function whi
1bd31 63 68 20 64 6f 65 73 20 74 68 65 20 77 6f 72 6b  ch does the work
1bd32 20 6f 66 20 69 6e 74 65 72 70 72 65 74 69 6e 67   of interpreting
1bd33 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 2e   a VDBE program.
1bd34 0a 2a 2a 20 42 75 74 20 6f 74 68 65 72 20 72 6f  .** But other ro
1bd35 75 74 69 6e 65 73 20 61 72 65 20 61 6c 73 6f 20  utines are also 
1bd36 70 72 6f 76 69 64 65 64 20 74 6f 20 68 65 6c 70  provided to help
1bd37 20 69 6e 20 62 75 69 6c 64 69 6e 67 20 75 70 0a   in building up.
1bd38 2a 2a 20 61 20 70 72 6f 67 72 61 6d 20 69 6e 73  ** a program ins
1bd39 74 72 75 63 74 69 6f 6e 20 62 79 20 69 6e 73 74  truction by inst
1bd3a 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 56  ruction..**.** V
1bd3b 61 72 69 6f 75 73 20 73 63 72 69 70 74 73 20 73  arious scripts s
1bd3c 63 61 6e 20 74 68 69 73 20 73 6f 75 72 63 65 20  can this source 
1bd3d 66 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 74 6f  file in order to
1bd3e 20 67 65 6e 65 72 61 74 65 20 48 54 4d 4c 0a 2a   generate HTML.*
1bd3f 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 2c  * documentation,
1bd40 20 68 65 61 64 65 72 73 20 66 69 6c 65 73 2c 20   headers files, 
1bd41 6f 72 20 6f 74 68 65 72 20 64 65 72 69 76 65 64  or other derived
1bd42 20 66 69 6c 65 73 2e 20 20 54 68 65 20 66 6f 72   files.  The for
1bd43 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 66 20 74 68  matting.** of th
1bd44 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66  e code in this f
1bd45 69 6c 65 20 69 73 2c 20 74 68 65 72 65 66 6f 72  ile is, therefor
1bd46 65 2c 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20 53  e, important.  S
1bd47 65 65 20 6f 74 68 65 72 20 63 6f 6d 6d 65 6e 74  ee other comment
1bd48 73 0a 2a 2a 20 69 6e 20 74 68 69 73 20 66 69 6c  s.** in this fil
1bd49 65 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 20 20  e for details.  
1bd4a 49 66 20 69 6e 20 64 6f 75 62 74 2c 20 64 6f 20  If in doubt, do 
1bd4b 6e 6f 74 20 64 65 76 69 61 74 65 20 66 72 6f 6d  not deviate from
1bd4c 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 63 6f 6d   existing.** com
1bd4d 6d 65 6e 74 69 6e 67 20 61 6e 64 20 69 6e 64 65  menting and inde
1bd4e 6e 74 61 74 69 6f 6e 20 70 72 61 63 74 69 63 65  ntation practice
1bd4f 73 20 77 68 65 6e 20 63 68 61 6e 67 69 6e 67 20  s when changing 
1bd50 6f 72 20 61 64 64 69 6e 67 20 63 6f 64 65 2e 0a  or adding code..
1bd51 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 2e  **.** $Id: vdbe.
1bd52 63 2c 76 20 31 2e 38 37 34 20 32 30 30 39 2f 30  c,v 1.874 2009/0
1bd53 37 2f 32 34 20 31 37 3a 35 38 3a 35 33 20 64 61  7/24 17:58:53 da
1bd54 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a  nielk1977 Exp $.
1bd55 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  */../*.** The fo
1bd56 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76  llowing global v
1bd57 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65  ariable is incre
1bd58 6d 65 6e 74 65 64 20 65 76 65 72 79 20 74 69 6d  mented every tim
1bd59 65 20 61 20 63 75 72 73 6f 72 0a 2a 2a 20 6d 6f  e a cursor.** mo
1bd5a 76 65 73 2c 20 65 69 74 68 65 72 20 62 79 20 74  ves, either by t
1bd5b 68 65 20 4f 50 5f 53 65 65 6b 58 58 2c 20 4f 50  he OP_SeekXX, OP
1bd5c 5f 4e 65 78 74 2c 20 6f 72 20 4f 50 5f 50 72 65  _Next, or OP_Pre
1bd5d 76 20 6f 70 63 6f 64 65 73 2e 20 20 54 68 65 20  v opcodes.  The 
1bd5e 74 65 73 74 0a 2a 2a 20 70 72 6f 63 65 64 75 72  test.** procedur
1bd5f 65 73 20 75 73 65 20 74 68 69 73 20 69 6e 66 6f  es use this info
1bd60 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20  rmation to make 
1bd61 73 75 72 65 20 74 68 61 74 20 69 6e 64 69 63 65  sure that indice
1bd62 73 20 61 72 65 0a 2a 2a 20 77 6f 72 6b 69 6e 67  s are.** working
1bd63 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 54 68 69   correctly.  Thi
1bd64 73 20 76 61 72 69 61 62 6c 65 20 68 61 73 20 6e  s variable has n
1bd65 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72  o function other
1bd66 20 74 68 61 6e 20 74 6f 0a 2a 2a 20 68 65 6c 70   than to.** help
1bd67 20 76 65 72 69 66 79 20 74 68 65 20 63 6f 72 72   verify the corr
1bd68 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66  ect operation of
1bd69 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f   the library..*/
1bd6a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
1bd6b 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  EST.SQLITE_API i
1bd6c 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63  nt sqlite3_searc
1bd6d 68 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e  h_count = 0;.#en
1bd6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20  dif../*.** When 
1bd6f 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 69  this global vari
1bd70 61 62 6c 65 20 69 73 20 70 6f 73 69 74 69 76 65  able is positive
1bd71 2c 20 69 74 20 67 65 74 73 20 64 65 63 72 65 6d  , it gets decrem
1bd72 65 6e 74 65 64 20 6f 6e 63 65 20 62 65 66 6f 72  ented once befor
1bd73 65 0a 2a 2a 20 65 61 63 68 20 69 6e 73 74 72 75  e.** each instru
1bd74 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 56 44 42  ction in the VDB
1bd75 45 2e 20 20 57 68 65 6e 20 72 65 61 63 68 65 73  E.  When reaches
1bd76 20 7a 65 72 6f 2c 20 74 68 65 20 75 31 2e 69 73   zero, the u1.is
1bd77 49 6e 74 65 72 72 75 70 74 65 64 0a 2a 2a 20 66  Interrupted.** f
1bd78 69 65 6c 64 20 6f 66 20 74 68 65 20 73 71 6c 69  ield of the sqli
1bd79 74 65 33 20 73 74 72 75 63 74 75 72 65 20 69 73  te3 structure is
1bd7a 20 73 65 74 20 69 6e 20 6f 72 64 65 72 20 74 6f   set in order to
1bd7b 20 73 69 6d 75 6c 61 74 65 20 61 6e 64 20 69 6e   simulate and in
1bd7c 74 65 72 72 75 70 74 2e 0a 2a 2a 0a 2a 2a 20 54  terrupt..**.** T
1bd7d 68 69 73 20 66 61 63 69 6c 69 74 79 20 69 73 20  his facility is 
1bd7e 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  used for testing
1bd7f 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 20   purposes only. 
1bd80 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 66 75 6e   It does not fun
1bd81 63 74 69 6f 6e 0a 2a 2a 20 69 6e 20 61 6e 20 6f  ction.** in an o
1bd82 72 64 69 6e 61 72 79 20 62 75 69 6c 64 2e 0a 2a  rdinary build..*
1bd83 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
1bd84 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20  TEST.SQLITE_API 
1bd85 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 65  int sqlite3_inte
1bd86 72 72 75 70 74 5f 63 6f 75 6e 74 20 3d 20 30 3b  rrupt_count = 0;
1bd87 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
1bd88 68 65 20 6e 65 78 74 20 67 6c 6f 62 61 6c 20 76  he next global v
1bd89 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65  ariable is incre
1bd8a 6d 65 6e 74 65 64 20 65 61 63 68 20 74 79 70 65  mented each type
1bd8b 20 74 68 65 20 4f 50 5f 53 6f 72 74 20 6f 70 63   the OP_Sort opc
1bd8c 6f 64 65 0a 2a 2a 20 69 73 20 65 78 65 63 75 74  ode.** is execut
1bd8d 65 64 2e 20 20 54 68 65 20 74 65 73 74 20 70 72  ed.  The test pr
1bd8e 6f 63 65 64 75 72 65 73 20 75 73 65 20 74 68 69  ocedures use thi
1bd8f 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f  s information to
1bd90 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a   make sure that.
1bd91 2a 2a 20 73 6f 72 74 69 6e 67 20 69 73 20 6f 63  ** sorting is oc
1bd92 63 75 72 72 69 6e 67 20 6f 72 20 6e 6f 74 20 6f  curring or not o
1bd93 63 63 75 72 72 69 6e 67 20 61 74 20 61 70 70 72  ccurring at appr
1bd94 6f 70 72 69 61 74 65 20 74 69 6d 65 73 2e 20 20  opriate times.  
1bd95 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 0a 2a   This variable.*
1bd96 2a 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f  * has no functio
1bd97 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 6f 20  n other than to 
1bd98 68 65 6c 70 20 76 65 72 69 66 79 20 74 68 65 20  help verify the 
1bd99 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f  correct operatio
1bd9a 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 69 62 72  n of the.** libr
1bd9b 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ary..*/.#ifdef S
1bd9c 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
1bd9d 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1bd9e 33 5f 73 6f 72 74 5f 63 6f 75 6e 74 20 3d 20 30  3_sort_count = 0
1bd9f 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
1bda0 54 68 65 20 6e 65 78 74 20 67 6c 6f 62 61 6c 20  The next global 
1bda1 76 61 72 69 61 62 6c 65 20 72 65 63 6f 72 64 73  variable records
1bda2 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
1bda3 20 6c 61 72 67 65 73 74 20 4d 45 4d 5f 42 6c 6f   largest MEM_Blo
1bda4 62 0a 2a 2a 20 6f 72 20 4d 45 4d 5f 53 74 72 20  b.** or MEM_Str 
1bda5 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 75 73  that has been us
1bda6 65 64 20 62 79 20 61 20 56 44 42 45 20 6f 70 63  ed by a VDBE opc
1bda7 6f 64 65 2e 20 20 54 68 65 20 74 65 73 74 20 70  ode.  The test p
1bda8 72 6f 63 65 64 75 72 65 73 0a 2a 2a 20 75 73 65  rocedures.** use
1bda9 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f   this informatio
1bdaa 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  n to make sure t
1bdab 68 61 74 20 74 68 65 20 7a 65 72 6f 2d 62 6c 6f  hat the zero-blo
1bdac 62 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 0a  b functionality.
1bdad 2a 2a 20 69 73 20 77 6f 72 6b 69 6e 67 20 63 6f  ** is working co
1bdae 72 72 65 63 74 6c 79 2e 20 20 20 54 68 69 73 20  rrectly.   This 
1bdaf 76 61 72 69 61 62 6c 65 20 68 61 73 20 6e 6f 20  variable has no 
1bdb0 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74  function other t
1bdb1 68 61 6e 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 76  han to.** help v
1bdb2 65 72 69 66 79 20 74 68 65 20 63 6f 72 72 65 63  erify the correc
1bdb3 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74  t operation of t
1bdb4 68 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23  he library..*/.#
1bdb5 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
1bdb6 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
1bdb7 20 73 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f   sqlite3_max_blo
1bdb8 62 73 69 7a 65 20 3d 20 30 3b 0a 73 74 61 74 69  bsize = 0;.stati
1bdb9 63 20 76 6f 69 64 20 75 70 64 61 74 65 4d 61 78  c void updateMax
1bdba 42 6c 6f 62 73 69 7a 65 28 4d 65 6d 20 2a 70 29  Blobsize(Mem *p)
1bdbb 7b 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67  {.  if( (p->flag
1bdbc 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d  s & (MEM_Str|MEM
1bdbd 5f 42 6c 6f 62 29 29 21 3d 30 20 26 26 20 70 2d  _Blob))!=0 && p-
1bdbe 3e 6e 3e 73 71 6c 69 74 65 33 5f 6d 61 78 5f 62  >n>sqlite3_max_b
1bdbf 6c 6f 62 73 69 7a 65 20 29 7b 0a 20 20 20 20 73  lobsize ){.    s
1bdc0 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73  qlite3_max_blobs
1bdc1 69 7a 65 20 3d 20 70 2d 3e 6e 3b 0a 20 20 7d 0a  ize = p->n;.  }.
1bdc2 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
1bdc3 54 65 73 74 20 61 20 72 65 67 69 73 74 65 72 20  Test a register 
1bdc4 74 6f 20 73 65 65 20 69 66 20 69 74 20 65 78 63  to see if it exc
1bdc5 65 65 64 73 20 74 68 65 20 63 75 72 72 65 6e 74  eeds the current
1bdc6 20 6d 61 78 69 6d 75 6d 20 62 6c 6f 62 20 73 69   maximum blob si
1bdc7 7a 65 2e 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65  ze..** If it doe
1bdc8 73 2c 20 72 65 63 6f 72 64 20 74 68 65 20 6e 65  s, record the ne
1bdc9 77 20 6d 61 78 69 6d 75 6d 20 62 6c 6f 62 20 73  w maximum blob s
1bdca 69 7a 65 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69  ize..*/.#if defi
1bdcb 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29  ned(SQLITE_TEST)
1bdcc 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
1bdcd 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e  ITE_OMIT_BUILTIN
1bdce 5f 54 45 53 54 29 0a 23 20 64 65 66 69 6e 65 20  _TEST).# define 
1bdcf 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53  UPDATE_MAX_BLOBS
1bdd0 49 5a 45 28 50 29 20 20 75 70 64 61 74 65 4d 61  IZE(P)  updateMa
1bdd1 78 42 6c 6f 62 73 69 7a 65 28 50 29 0a 23 65 6c  xBlobsize(P).#el
1bdd2 73 65 0a 23 20 64 65 66 69 6e 65 20 55 50 44 41  se.# define UPDA
1bdd3 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28  TE_MAX_BLOBSIZE(
1bdd4 50 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  P).#endif../*.**
1bdd5 20 43 6f 6e 76 65 72 74 20 74 68 65 20 67 69 76   Convert the giv
1bdd6 65 6e 20 72 65 67 69 73 74 65 72 20 69 6e 74 6f  en register into
1bdd7 20 61 20 73 74 72 69 6e 67 20 69 66 20 69 74 20   a string if it 
1bdd8 69 73 6e 27 74 20 6f 6e 65 0a 2a 2a 20 61 6c 72  isn't one.** alr
1bdd9 65 61 64 79 2e 20 52 65 74 75 72 6e 20 6e 6f 6e  eady. Return non
1bdda 2d 7a 65 72 6f 20 69 66 20 61 20 6d 61 6c 6c 6f  -zero if a mallo
1bddb 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 23 64  c() fails..*/.#d
1bddc 65 66 69 6e 65 20 53 74 72 69 6e 67 69 66 79 28  efine Stringify(
1bddd 50 2c 20 65 6e 63 29 20 5c 0a 20 20 20 69 66 28  P, enc) \.   if(
1bdde 28 28 50 29 2d 3e 66 6c 61 67 73 26 28 4d 45 4d  ((P)->flags&(MEM
1bddf 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d  _Str|MEM_Blob))=
1bde0 3d 30 20 26 26 20 73 71 6c 69 74 65 33 56 64 62  =0 && sqlite3Vdb
1bde1 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 50 2c  eMemStringify(P,
1bde2 65 6e 63 29 29 20 5c 0a 20 20 20 20 20 7b 20 67  enc)) \.     { g
1bde3 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 20 7d 0a 0a 2f  oto no_mem; }../
1bde4 2a 0a 2a 2a 20 41 6e 20 65 70 68 65 6d 65 72 61  *.** An ephemera
1bde5 6c 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 28  l string value (
1bde6 73 69 67 6e 69 66 69 65 64 20 62 79 20 74 68 65  signified by the
1bde7 20 4d 45 4d 5f 45 70 68 65 6d 20 66 6c 61 67 29   MEM_Ephem flag)
1bde8 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 70   contains.** a p
1bde9 6f 69 6e 74 65 72 20 74 6f 20 61 20 64 79 6e 61  ointer to a dyna
1bdea 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65  mically allocate
1bdeb 64 20 73 74 72 69 6e 67 20 77 68 65 72 65 20 73  d string where s
1bdec 6f 6d 65 20 6f 74 68 65 72 20 65 6e 74 69 74 79  ome other entity
1bded 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62  .** is responsib
1bdee 6c 65 20 66 6f 72 20 64 65 61 6c 6c 6f 63 61 74  le for deallocat
1bdef 69 6e 67 20 74 68 61 74 20 73 74 72 69 6e 67 2e  ing that string.
1bdf0 20 20 42 65 63 61 75 73 65 20 74 68 65 20 72 65    Because the re
1bdf1 67 69 73 74 65 72 0a 2a 2a 20 64 6f 65 73 20 6e  gister.** does n
1bdf2 6f 74 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 73  ot control the s
1bdf3 74 72 69 6e 67 2c 20 69 74 20 6d 69 67 68 74 20  tring, it might 
1bdf4 62 65 20 64 65 6c 65 74 65 64 20 77 69 74 68 6f  be deleted witho
1bdf5 75 74 20 74 68 65 20 72 65 67 69 73 74 65 72 0a  ut the register.
1bdf6 2a 2a 20 6b 6e 6f 77 69 6e 67 20 69 74 2e 0a 2a  ** knowing it..*
1bdf7 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1bdf8 65 20 63 6f 6e 76 65 72 74 73 20 61 6e 20 65 70  e converts an ep
1bdf9 68 65 6d 65 72 61 6c 20 73 74 72 69 6e 67 20 69  hemeral string i
1bdfa 6e 74 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c  nto a dynamicall
1bdfb 79 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 73  y allocated.** s
1bdfc 74 72 69 6e 67 20 74 68 61 74 20 74 68 65 20 72  tring that the r
1bdfd 65 67 69 73 74 65 72 20 69 74 73 65 6c 66 20 63  egister itself c
1bdfe 6f 6e 74 72 6f 6c 73 2e 20 20 49 6e 20 6f 74 68  ontrols.  In oth
1bdff 65 72 20 77 6f 72 64 73 2c 20 69 74 0a 2a 2a 20  er words, it.** 
1be00 63 6f 6e 76 65 72 74 73 20 61 6e 20 4d 45 4d 5f  converts an MEM_
1be01 45 70 68 65 6d 20 73 74 72 69 6e 67 20 69 6e 74  Ephem string int
1be02 6f 20 61 6e 20 4d 45 4d 5f 44 79 6e 20 73 74 72  o an MEM_Dyn str
1be03 69 6e 67 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ing..*/.#define 
1be04 44 65 65 70 68 65 6d 65 72 61 6c 69 7a 65 28 50  Deephemeralize(P
1be05 29 20 5c 0a 20 20 20 69 66 28 20 28 28 50 29 2d  ) \.   if( ((P)-
1be06 3e 66 6c 61 67 73 26 4d 45 4d 5f 45 70 68 65 6d  >flags&MEM_Ephem
1be07 29 21 3d 30 20 5c 0a 20 20 20 20 20 20 20 26 26  )!=0 \.       &&
1be08 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d   sqlite3VdbeMemM
1be09 61 6b 65 57 72 69 74 65 61 62 6c 65 28 50 29 20  akeWriteable(P) 
1be0a 29 7b 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 7d  ){ goto no_mem;}
1be0b 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c  ../*.** Call sql
1be0c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e  ite3VdbeMemExpan
1be0d 64 42 6c 6f 62 28 29 20 6f 6e 20 74 68 65 20 73  dBlob() on the s
1be0e 75 70 70 6c 69 65 64 20 76 61 6c 75 65 20 28 74  upplied value (t
1be0f 79 70 65 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69  ype Mem*).** P i
1be10 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23  f required..*/.#
1be11 64 65 66 69 6e 65 20 45 78 70 61 6e 64 42 6c 6f  define ExpandBlo
1be12 62 28 50 29 20 28 28 28 50 29 2d 3e 66 6c 61 67  b(P) (((P)->flag
1be13 73 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69  s&MEM_Zero)?sqli
1be14 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64  te3VdbeMemExpand
1be15 42 6c 6f 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a  Blob(P):0)../*.*
1be16 2a 20 41 72 67 75 6d 65 6e 74 20 70 4d 65 6d 20  * Argument pMem 
1be17 70 6f 69 6e 74 73 20 61 74 20 61 20 72 65 67 69  points at a regi
1be18 73 74 65 72 20 74 68 61 74 20 77 69 6c 6c 20 62  ster that will b
1be19 65 20 70 61 73 73 65 64 20 74 6f 20 61 0a 2a 2a  e passed to a.**
1be1a 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75   user-defined fu
1be1b 6e 63 74 69 6f 6e 20 6f 72 20 72 65 74 75 72 6e  nction or return
1be1c 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 20 61  ed to the user a
1be1d 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  s the result of 
1be1e 61 20 71 75 65 72 79 2e 0a 2a 2a 20 54 68 65 20  a query..** The 
1be1f 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c  second argument,
1be20 20 27 64 62 5f 65 6e 63 27 20 69 73 20 74 68 65   'db_enc' is the
1be21 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75   text encoding u
1be22 73 65 64 20 62 79 20 74 68 65 20 76 64 62 65 20  sed by the vdbe 
1be23 66 6f 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20  for.** register 
1be24 76 61 72 69 61 62 6c 65 73 2e 20 20 54 68 69 73  variables.  This
1be25 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68   routine sets th
1be26 65 20 70 4d 65 6d 2d 3e 65 6e 63 20 61 6e 64 20  e pMem->enc and 
1be27 70 4d 65 6d 2d 3e 74 79 70 65 0a 2a 2a 20 76 61  pMem->type.** va
1be28 72 69 61 62 6c 65 73 20 75 73 65 64 20 62 79 20  riables used by 
1be29 74 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  the sqlite3_valu
1be2a 65 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 2e 0a  e_*() routines..
1be2b 2a 2f 0a 23 64 65 66 69 6e 65 20 73 74 6f 72 65  */.#define store
1be2c 54 79 70 65 49 6e 66 6f 28 41 2c 42 29 20 5f 73  TypeInfo(A,B) _s
1be2d 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 41 29 0a  toreTypeInfo(A).
1be2e 73 74 61 74 69 63 20 76 6f 69 64 20 5f 73 74 6f  static void _sto
1be2f 72 65 54 79 70 65 49 6e 66 6f 28 4d 65 6d 20 2a  reTypeInfo(Mem *
1be30 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61  pMem){.  int fla
1be31 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  gs = pMem->flags
1be32 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20  ;.  if( flags & 
1be33 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  MEM_Null ){.    
1be34 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
1be35 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20  ITE_NULL;.  }.  
1be36 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26  else if( flags &
1be37 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20   MEM_Int ){.    
1be38 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
1be39 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 7d  ITE_INTEGER;.  }
1be3a 0a 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67  .  else if( flag
1be3b 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a  s & MEM_Real ){.
1be3c 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d      pMem->type =
1be3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20   SQLITE_FLOAT;. 
1be3e 20 7d 0a 20 20 65 6c 73 65 20 69 66 28 20 66 6c   }.  else if( fl
1be3f 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b  ags & MEM_Str ){
1be40 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20  .    pMem->type 
1be41 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20  = SQLITE_TEXT;. 
1be42 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d   }else{.    pMem
1be43 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
1be44 42 4c 4f 42 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  BLOB;.  }.}../*.
1be45 2a 2a 20 50 72 6f 70 65 72 74 69 65 73 20 6f 66  ** Properties of
1be46 20 6f 70 63 6f 64 65 73 2e 20 20 54 68 65 20 4f   opcodes.  The O
1be47 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52  PFLG_INITIALIZER
1be48 20 6d 61 63 72 6f 20 69 73 0a 2a 2a 20 63 72 65   macro is.** cre
1be49 61 74 65 64 20 62 79 20 6d 6b 6f 70 63 6f 64 65  ated by mkopcode
1be4a 68 2e 61 77 6b 20 64 75 72 69 6e 67 20 63 6f 6d  h.awk during com
1be4b 70 69 6c 61 74 69 6f 6e 2e 20 20 44 61 74 61 20  pilation.  Data 
1be4c 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66  is obtained.** f
1be4d 72 6f 6d 20 74 68 65 20 63 6f 6d 6d 65 6e 74 73  rom the comments
1be4e 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22   following the "
1be4f 63 61 73 65 20 4f 50 5f 78 78 78 78 3a 22 20 73  case OP_xxxx:" s
1be50 74 61 74 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20  tatements in.** 
1be51 74 68 69 73 20 66 69 6c 65 2e 20 20 0a 2a 2f 0a  this file.  .*/.
1be52 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
1be53 69 67 6e 65 64 20 63 68 61 72 20 6f 70 63 6f 64  igned char opcod
1be54 65 50 72 6f 70 65 72 74 79 5b 5d 20 3d 20 4f 50  eProperty[] = OP
1be55 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 3b  FLG_INITIALIZER;
1be56 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
1be57 72 75 65 20 69 66 20 61 6e 20 6f 70 63 6f 64 65  rue if an opcode
1be58 20 68 61 73 20 61 6e 79 20 6f 66 20 74 68 65 20   has any of the 
1be59 4f 50 46 4c 47 5f 78 78 78 20 70 72 6f 70 65 72  OPFLG_xxx proper
1be5a 74 69 65 73 0a 2a 2a 20 73 70 65 63 69 66 69 65  ties.** specifie
1be5b 64 20 62 79 20 6d 61 73 6b 2e 0a 2a 2f 0a 53 51  d by mask..*/.SQ
1be5c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1be5d 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f   sqlite3VdbeOpco
1be5e 64 65 48 61 73 50 72 6f 70 65 72 74 79 28 69 6e  deHasProperty(in
1be5f 74 20 6f 70 63 6f 64 65 2c 20 69 6e 74 20 6d 61  t opcode, int ma
1be60 73 6b 29 7b 0a 20 20 61 73 73 65 72 74 28 20 6f  sk){.  assert( o
1be61 70 63 6f 64 65 3e 30 20 26 26 20 6f 70 63 6f 64  pcode>0 && opcod
1be62 65 3c 28 69 6e 74 29 73 69 7a 65 6f 66 28 6f 70  e<(int)sizeof(op
1be63 63 6f 64 65 50 72 6f 70 65 72 74 79 29 20 29 3b  codeProperty) );
1be64 0a 20 20 72 65 74 75 72 6e 20 28 6f 70 63 6f 64  .  return (opcod
1be65 65 50 72 6f 70 65 72 74 79 5b 6f 70 63 6f 64 65  eProperty[opcode
1be66 5d 26 6d 61 73 6b 29 21 3d 30 3b 0a 7d 0a 0a 2f  ]&mask)!=0;.}../
1be67 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 56 64  *.** Allocate Vd
1be68 62 65 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  beCursor number 
1be69 69 43 75 72 2e 20 20 52 65 74 75 72 6e 20 61 20  iCur.  Return a 
1be6a 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20  pointer to it.  
1be6b 52 65 74 75 72 6e 20 4e 55 4c 4c 0a 2a 2a 20 69  Return NULL.** i
1be6c 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20  f we run out of 
1be6d 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69  memory..*/.stati
1be6e 63 20 56 64 62 65 43 75 72 73 6f 72 20 2a 61 6c  c VdbeCursor *al
1be6f 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 0a 20 20  locateCursor(.  
1be70 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20  Vdbe *p,        
1be71 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72        /* The vir
1be72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a  tual machine */.
1be73 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20    int iCur,     
1be74 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
1be75 20 6f 66 20 74 68 65 20 6e 65 77 20 56 64 62 65   of the new Vdbe
1be76 43 75 72 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20  Cursor */.  int 
1be77 6e 46 69 65 6c 64 2c 20 20 20 20 20 20 20 20 20  nField,         
1be78 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66    /* Number of f
1be79 69 65 6c 64 73 20 69 6e 20 74 68 65 20 74 61 62  ields in the tab
1be7a 6c 65 20 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20  le or index */. 
1be7b 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20   int iDb,       
1be7c 20 20 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 64         /* When d
1be7d 61 74 61 62 61 73 65 20 74 68 65 20 63 75 72 73  atabase the curs
1be7e 6f 72 20 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 6f  or belongs to, o
1be7f 72 20 2d 31 20 2a 2f 0a 20 20 69 6e 74 20 69 73  r -1 */.  int is
1be80 42 74 72 65 65 43 75 72 73 6f 72 20 20 20 20 20  BtreeCursor     
1be81 2f 2a 20 54 72 75 65 20 66 6f 72 20 42 2d 54 72  /* True for B-Tr
1be82 65 65 2e 20 20 46 61 6c 73 65 20 66 6f 72 20 70  ee.  False for p
1be83 73 65 75 64 6f 2d 74 61 62 6c 65 20 6f 72 20 76  seudo-table or v
1be84 74 61 62 20 2a 2f 0a 29 7b 0a 20 20 2f 2a 20 46  tab */.){.  /* F
1be85 69 6e 64 20 74 68 65 20 6d 65 6d 6f 72 79 20 63  ind the memory c
1be86 65 6c 6c 20 74 68 61 74 20 77 69 6c 6c 20 62 65  ell that will be
1be87 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74   used to store t
1be88 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72  he blob of memor
1be89 79 0a 20 20 2a 2a 20 72 65 71 75 69 72 65 64 20  y.  ** required 
1be8a 66 6f 72 20 74 68 69 73 20 56 64 62 65 43 75 72  for this VdbeCur
1be8b 73 6f 72 20 73 74 72 75 63 74 75 72 65 2e 20 49  sor structure. I
1be8c 74 20 69 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20  t is convenient 
1be8d 74 6f 20 75 73 65 20 61 20 0a 20 20 2a 2a 20 76  to use a .  ** v
1be8e 64 62 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  dbe memory cell 
1be8f 74 6f 20 6d 61 6e 61 67 65 20 74 68 65 20 6d 65  to manage the me
1be90 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
1be91 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 0a 20  required for a. 
1be92 20 2a 2a 20 56 64 62 65 43 75 72 73 6f 72 20 73   ** VdbeCursor s
1be93 74 72 75 63 74 75 72 65 20 66 6f 72 20 74 68 65  tructure for the
1be94 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 61 73 6f   following reaso
1be95 6e 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  ns:.  **.  **   
1be96 2a 20 53 6f 6d 65 74 69 6d 65 73 20 63 75 72 73  * Sometimes curs
1be97 6f 72 20 6e 75 6d 62 65 72 73 20 61 72 65 20 75  or numbers are u
1be98 73 65 64 20 66 6f 72 20 61 20 63 6f 75 70 6c 65  sed for a couple
1be99 20 6f 66 20 64 69 66 66 65 72 65 6e 74 0a 20 20   of different.  
1be9a 2a 2a 20 20 20 20 20 70 75 72 70 6f 73 65 73 20  **     purposes 
1be9b 69 6e 20 61 20 76 64 62 65 20 70 72 6f 67 72 61  in a vdbe progra
1be9c 6d 2e 20 54 68 65 20 64 69 66 66 65 72 65 6e 74  m. The different
1be9d 20 75 73 65 73 20 6d 69 67 68 74 20 72 65 71 75   uses might requ
1be9e 69 72 65 0a 20 20 2a 2a 20 20 20 20 20 64 69 66  ire.  **     dif
1be9f 66 65 72 65 6e 74 20 73 69 7a 65 64 20 61 6c 6c  ferent sized all
1bea0 6f 63 61 74 69 6f 6e 73 2e 20 4d 65 6d 6f 72 79  ocations. Memory
1bea1 20 63 65 6c 6c 73 20 70 72 6f 76 69 64 65 20 67   cells provide g
1bea2 72 6f 77 61 62 6c 65 0a 20 20 2a 2a 20 20 20 20  rowable.  **    
1bea3 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20   allocations..  
1bea4 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e  **.  **   * When
1bea5 20 75 73 69 6e 67 20 45 4e 41 42 4c 45 5f 4d 45   using ENABLE_ME
1bea6 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 2c  MORY_MANAGEMENT,
1bea7 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 62 75 66   memory cell buf
1bea8 66 65 72 73 20 63 61 6e 0a 20 20 2a 2a 20 20 20  fers can.  **   
1bea9 20 20 62 65 20 66 72 65 65 64 20 6c 61 7a 69 6c    be freed lazil
1beaa 79 20 76 69 61 20 74 68 65 20 73 71 6c 69 74 65  y via the sqlite
1beab 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
1beac 28 29 20 41 50 49 2e 20 54 68 69 73 0a 20 20 2a  () API. This.  *
1bead 2a 20 20 20 20 20 6d 69 6e 69 6d 69 7a 65 73 20  *     minimizes 
1beae 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 61  the number of ma
1beaf 6c 6c 6f 63 20 63 61 6c 6c 73 20 6d 61 64 65 20  lloc calls made 
1beb0 62 79 20 74 68 65 20 73 79 73 74 65 6d 2e 0a 20  by the system.. 
1beb1 20 2a 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20   **.  ** Memory 
1beb2 63 65 6c 6c 73 20 66 6f 72 20 63 75 72 73 6f 72  cells for cursor
1beb3 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20  s are allocated 
1beb4 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  at the top of th
1beb5 65 20 61 64 64 72 65 73 73 0a 20 20 2a 2a 20 73  e address.  ** s
1beb6 70 61 63 65 2e 20 4d 65 6d 6f 72 79 20 63 65 6c  pace. Memory cel
1beb7 6c 20 28 70 2d 3e 6e 4d 65 6d 29 20 63 6f 72 72  l (p->nMem) corr
1beb8 65 73 70 6f 6e 64 73 20 74 6f 20 63 75 72 73 6f  esponds to curso
1beb9 72 20 30 2e 20 53 70 61 63 65 20 66 6f 72 0a 20  r 0. Space for. 
1beba 20 2a 2a 20 63 75 72 73 6f 72 20 31 20 69 73 20   ** cursor 1 is 
1bebb 6d 61 6e 61 67 65 64 20 62 79 20 6d 65 6d 6f 72  managed by memor
1bebc 79 20 63 65 6c 6c 20 28 70 2d 3e 6e 4d 65 6d 2d  y cell (p->nMem-
1bebd 31 29 2c 20 65 74 63 2e 0a 20 20 2a 2f 0a 20 20  1), etc..  */.  
1bebe 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e  Mem *pMem = &p->
1bebf 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 2d 69 43 75  aMem[p->nMem-iCu
1bec0 72 5d 3b 0a 0a 20 20 69 6e 74 20 6e 42 79 74 65  r];..  int nByte
1bec1 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  ;.  VdbeCursor *
1bec2 70 43 78 20 3d 20 30 3b 0a 20 20 6e 42 79 74 65  pCx = 0;.  nByte
1bec3 20 3d 20 0a 20 20 20 20 20 20 73 69 7a 65 6f 66   = .      sizeof
1bec4 28 56 64 62 65 43 75 72 73 6f 72 29 20 2b 20 0a  (VdbeCursor) + .
1bec5 20 20 20 20 20 20 28 69 73 42 74 72 65 65 43 75        (isBtreeCu
1bec6 72 73 6f 72 3f 73 71 6c 69 74 65 33 42 74 72 65  rsor?sqlite3Btre
1bec7 65 43 75 72 73 6f 72 53 69 7a 65 28 29 3a 30 29  eCursorSize():0)
1bec8 20 2b 20 0a 20 20 20 20 20 20 32 2a 6e 46 69 65   + .      2*nFie
1bec9 6c 64 2a 73 69 7a 65 6f 66 28 75 33 32 29 3b 0a  ld*sizeof(u32);.
1beca 0a 20 20 61 73 73 65 72 74 28 20 69 43 75 72 3c  .  assert( iCur<
1becb 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1becc 69 66 28 20 70 2d 3e 61 70 43 73 72 5b 69 43 75  if( p->apCsr[iCu
1becd 72 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  r] ){.    sqlite
1bece 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28  3VdbeFreeCursor(
1becf 70 2c 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 72  p, p->apCsr[iCur
1bed0 5d 29 3b 0a 20 20 20 20 70 2d 3e 61 70 43 73 72  ]);.    p->apCsr
1bed1 5b 69 43 75 72 5d 20 3d 20 30 3b 0a 20 20 7d 0a  [iCur] = 0;.  }.
1bed2 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d    if( SQLITE_OK=
1bed3 3d 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47  =sqlite3VdbeMemG
1bed4 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c  row(pMem, nByte,
1bed5 20 30 29 20 29 7b 0a 20 20 20 20 70 2d 3e 61 70   0) ){.    p->ap
1bed6 43 73 72 5b 69 43 75 72 5d 20 3d 20 70 43 78 20  Csr[iCur] = pCx 
1bed7 3d 20 28 56 64 62 65 43 75 72 73 6f 72 2a 29 70  = (VdbeCursor*)p
1bed8 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 6d 65 6d 73  Mem->z;.    mems
1bed9 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e  et(pMem->z, 0, n
1beda 42 79 74 65 29 3b 0a 20 20 20 20 70 43 78 2d 3e  Byte);.    pCx->
1bedb 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 70  iDb = iDb;.    p
1bedc 43 78 2d 3e 6e 46 69 65 6c 64 20 3d 20 6e 46 69  Cx->nField = nFi
1bedd 65 6c 64 3b 0a 20 20 20 20 69 66 28 20 6e 46 69  eld;.    if( nFi
1bede 65 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 43 78  eld ){.      pCx
1bedf 2d 3e 61 54 79 70 65 20 3d 20 28 75 33 32 20 2a  ->aType = (u32 *
1bee0 29 26 70 4d 65 6d 2d 3e 7a 5b 73 69 7a 65 6f 66  )&pMem->z[sizeof
1bee1 28 56 64 62 65 43 75 72 73 6f 72 29 5d 3b 0a 20  (VdbeCursor)];. 
1bee2 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 42     }.    if( isB
1bee3 74 72 65 65 43 75 72 73 6f 72 20 29 7b 0a 20 20  treeCursor ){.  
1bee4 20 20 20 20 70 43 78 2d 3e 70 43 75 72 73 6f 72      pCx->pCursor
1bee5 20 3d 20 28 42 74 43 75 72 73 6f 72 2a 29 0a 20   = (BtCursor*). 
1bee6 20 20 20 20 20 20 20 20 20 26 70 4d 65 6d 2d 3e           &pMem->
1bee7 7a 5b 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72  z[sizeof(VdbeCur
1bee8 73 6f 72 29 2b 32 2a 6e 46 69 65 6c 64 2a 73 69  sor)+2*nField*si
1bee9 7a 65 6f 66 28 75 33 32 29 5d 3b 0a 20 20 20 20  zeof(u32)];.    
1beea 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  }.  }.  return p
1beeb 43 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79  Cx;.}../*.** Try
1beec 20 74 6f 20 63 6f 6e 76 65 72 74 20 61 20 76 61   to convert a va
1beed 6c 75 65 20 69 6e 74 6f 20 61 20 6e 75 6d 65 72  lue into a numer
1beee 69 63 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ic representatio
1beef 6e 20 69 66 20 77 65 20 63 61 6e 0a 2a 2a 20 64  n if we can.** d
1bef0 6f 20 73 6f 20 77 69 74 68 6f 75 74 20 6c 6f 73  o so without los
1bef1 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  s of information
1bef2 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
1bef3 73 2c 20 69 66 20 74 68 65 20 73 74 72 69 6e 67  s, if the string
1bef4 0a 2a 2a 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61  .** looks like a
1bef5 20 6e 75 6d 62 65 72 2c 20 63 6f 6e 76 65 72 74   number, convert
1bef6 20 69 74 20 69 6e 74 6f 20 61 20 6e 75 6d 62 65   it into a numbe
1bef7 72 2e 20 20 49 66 20 69 74 20 64 6f 65 73 20 6e  r.  If it does n
1bef8 6f 74 0a 2a 2a 20 6c 6f 6f 6b 20 6c 69 6b 65 20  ot.** look like 
1bef9 61 20 6e 75 6d 62 65 72 2c 20 6c 65 61 76 65 20  a number, leave 
1befa 69 74 20 61 6c 6f 6e 65 2e 0a 2a 2f 0a 73 74 61  it alone..*/.sta
1befb 74 69 63 20 76 6f 69 64 20 61 70 70 6c 79 4e 75  tic void applyNu
1befc 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 4d 65  mericAffinity(Me
1befd 6d 20 2a 70 52 65 63 29 7b 0a 20 20 69 66 28 20  m *pRec){.  if( 
1befe 28 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 28  (pRec->flags & (
1beff 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74  MEM_Real|MEM_Int
1bf00 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  ))==0 ){.    int
1bf01 20 72 65 61 6c 6e 75 6d 3b 0a 20 20 20 20 73 71   realnum;.    sq
1bf02 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54  lite3VdbeMemNulT
1bf03 65 72 6d 69 6e 61 74 65 28 70 52 65 63 29 3b 0a  erminate(pRec);.
1bf04 20 20 20 20 69 66 28 20 28 70 52 65 63 2d 3e 66      if( (pRec->f
1bf05 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 0a 20 20  lags&MEM_Str).  
1bf06 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65         && sqlite
1bf07 33 49 73 4e 75 6d 62 65 72 28 70 52 65 63 2d 3e  3IsNumber(pRec->
1bf08 7a 2c 20 26 72 65 61 6c 6e 75 6d 2c 20 70 52 65  z, &realnum, pRe
1bf09 63 2d 3e 65 6e 63 29 20 29 7b 0a 20 20 20 20 20  c->enc) ){.     
1bf0a 20 69 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20   i64 value;.    
1bf0b 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1bf0c 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 52 65 63  ngeEncoding(pRec
1bf0d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a  , SQLITE_UTF8);.
1bf0e 20 20 20 20 20 20 69 66 28 20 21 72 65 61 6c 6e        if( !realn
1bf0f 75 6d 20 26 26 20 73 71 6c 69 74 65 33 41 74 6f  um && sqlite3Ato
1bf10 69 36 34 28 70 52 65 63 2d 3e 7a 2c 20 26 76 61  i64(pRec->z, &va
1bf11 6c 75 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20  lue) ){.        
1bf12 70 52 65 63 2d 3e 75 2e 69 20 3d 20 76 61 6c 75  pRec->u.i = valu
1bf13 65 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 53 65  e;.        MemSe
1bf14 74 54 79 70 65 46 6c 61 67 28 70 52 65 63 2c 20  tTypeFlag(pRec, 
1bf15 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 20 20  MEM_Int);.      
1bf16 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
1bf17 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61  qlite3VdbeMemRea
1bf18 6c 69 66 79 28 70 52 65 63 29 3b 0a 20 20 20 20  lify(pRec);.    
1bf19 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a    }.    }.  }.}.
1bf1a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 69 6e  ./*.** Processin
1bf1b 67 20 69 73 20 64 65 74 65 72 6d 69 6e 65 20 62  g is determine b
1bf1c 79 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 70  y the affinity p
1bf1d 61 72 61 6d 65 74 65 72 3a 0a 2a 2a 0a 2a 2a 20  arameter:.**.** 
1bf1e 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47  SQLITE_AFF_INTEG
1bf1f 45 52 3a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46  ER:.** SQLITE_AF
1bf20 46 5f 52 45 41 4c 3a 0a 2a 2a 20 53 51 4c 49 54  F_REAL:.** SQLIT
1bf21 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3a 0a 2a  E_AFF_NUMERIC:.*
1bf22 2a 20 20 20 20 54 72 79 20 74 6f 20 63 6f 6e 76  *    Try to conv
1bf23 65 72 74 20 70 52 65 63 20 74 6f 20 61 6e 20 69  ert pRec to an i
1bf24 6e 74 65 67 65 72 20 72 65 70 72 65 73 65 6e 74  nteger represent
1bf25 61 74 69 6f 6e 20 6f 72 20 61 20 0a 2a 2a 20 20  ation or a .**  
1bf26 20 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74    floating-point
1bf27 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
1bf28 69 66 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65  if an integer re
1bf29 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20  presentation.** 
1bf2a 20 20 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62     is not possib
1bf2b 6c 65 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74  le.  Note that t
1bf2c 68 65 20 69 6e 74 65 67 65 72 20 72 65 70 72 65  he integer repre
1bf2d 73 65 6e 74 61 74 69 6f 6e 20 69 73 0a 2a 2a 20  sentation is.** 
1bf2e 20 20 20 61 6c 77 61 79 73 20 70 72 65 66 65 72     always prefer
1bf2f 72 65 64 2c 20 65 76 65 6e 20 69 66 20 74 68 65  red, even if the
1bf30 20 61 66 66 69 6e 69 74 79 20 69 73 20 52 45 41   affinity is REA
1bf31 4c 2c 20 62 65 63 61 75 73 65 0a 2a 2a 20 20 20  L, because.**   
1bf32 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72   an integer repr
1bf33 65 73 65 6e 74 61 74 69 6f 6e 20 69 73 20 6d 6f  esentation is mo
1bf34 72 65 20 73 70 61 63 65 20 65 66 66 69 63 69 65  re space efficie
1bf35 6e 74 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 0a 2a  nt on disk..**.*
1bf36 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  * SQLITE_AFF_TEX
1bf37 54 3a 0a 2a 2a 20 20 20 20 43 6f 6e 76 65 72 74  T:.**    Convert
1bf38 20 70 52 65 63 20 74 6f 20 61 20 74 65 78 74 20   pRec to a text 
1bf39 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a  representation..
1bf3a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46  **.** SQLITE_AFF
1bf3b 5f 4e 4f 4e 45 3a 0a 2a 2a 20 20 20 20 4e 6f 2d  _NONE:.**    No-
1bf3c 6f 70 2e 20 20 70 52 65 63 20 69 73 20 75 6e 63  op.  pRec is unc
1bf3d 68 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  hanged..*/.stati
1bf3e 63 20 76 6f 69 64 20 61 70 70 6c 79 41 66 66 69  c void applyAffi
1bf3f 6e 69 74 79 28 0a 20 20 4d 65 6d 20 2a 70 52 65  nity(.  Mem *pRe
1bf40 63 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  c,          /* T
1bf41 68 65 20 76 61 6c 75 65 20 74 6f 20 61 70 70 6c  he value to appl
1bf42 79 20 61 66 66 69 6e 69 74 79 20 74 6f 20 2a 2f  y affinity to */
1bf43 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79  .  char affinity
1bf44 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66  ,      /* The af
1bf45 66 69 6e 69 74 79 20 74 6f 20 62 65 20 61 70 70  finity to be app
1bf46 6c 69 65 64 20 2a 2f 0a 20 20 75 38 20 65 6e 63  lied */.  u8 enc
1bf47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1bf48 20 55 73 65 20 74 68 69 73 20 74 65 78 74 20 65   Use this text e
1bf49 6e 63 6f 64 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20  ncoding */.){.  
1bf4a 69 66 28 20 61 66 66 69 6e 69 74 79 3d 3d 53 51  if( affinity==SQ
1bf4b 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 29 7b  LITE_AFF_TEXT ){
1bf4c 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 74 74  .    /* Only att
1bf4d 65 6d 70 74 20 74 68 65 20 63 6f 6e 76 65 72 73  empt the convers
1bf4e 69 6f 6e 20 74 6f 20 54 45 58 54 20 69 66 20 74  ion to TEXT if t
1bf4f 68 65 72 65 20 69 73 20 61 6e 20 69 6e 74 65 67  here is an integ
1bf50 65 72 20 6f 72 20 72 65 61 6c 0a 20 20 20 20 2a  er or real.    *
1bf51 2a 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  * representation
1bf52 20 28 62 6c 6f 62 20 61 6e 64 20 4e 55 4c 4c 20   (blob and NULL 
1bf53 64 6f 20 6e 6f 74 20 67 65 74 20 63 6f 6e 76 65  do not get conve
1bf54 72 74 65 64 29 20 62 75 74 20 6e 6f 20 73 74 72  rted) but no str
1bf55 69 6e 67 0a 20 20 20 20 2a 2a 20 72 65 70 72 65  ing.    ** repre
1bf56 73 65 6e 74 61 74 69 6f 6e 2e 0a 20 20 20 20 2a  sentation..    *
1bf57 2f 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 70 52  /.    if( 0==(pR
1bf58 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74  ec->flags&MEM_St
1bf59 72 29 20 26 26 20 28 70 52 65 63 2d 3e 66 6c 61  r) && (pRec->fla
1bf5a 67 73 26 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d  gs&(MEM_Real|MEM
1bf5b 5f 49 6e 74 29 29 20 29 7b 0a 20 20 20 20 20 20  _Int)) ){.      
1bf5c 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74  sqlite3VdbeMemSt
1bf5d 72 69 6e 67 69 66 79 28 70 52 65 63 2c 20 65 6e  ringify(pRec, en
1bf5e 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 52  c);.    }.    pR
1bf5f 65 63 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d  ec->flags &= ~(M
1bf60 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29  EM_Real|MEM_Int)
1bf61 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 61 66  ;.  }else if( af
1bf62 66 69 6e 69 74 79 21 3d 53 51 4c 49 54 45 5f 41  finity!=SQLITE_A
1bf63 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 61  FF_NONE ){.    a
1bf64 73 73 65 72 74 28 20 61 66 66 69 6e 69 74 79 3d  ssert( affinity=
1bf65 3d 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45  =SQLITE_AFF_INTE
1bf66 47 45 52 20 7c 7c 20 61 66 66 69 6e 69 74 79 3d  GER || affinity=
1bf67 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c  =SQLITE_AFF_REAL
1bf68 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c  .             ||
1bf69 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54   affinity==SQLIT
1bf6a 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b  E_AFF_NUMERIC );
1bf6b 0a 20 20 20 20 61 70 70 6c 79 4e 75 6d 65 72 69  .    applyNumeri
1bf6c 63 41 66 66 69 6e 69 74 79 28 70 52 65 63 29 3b  cAffinity(pRec);
1bf6d 0a 20 20 20 20 69 66 28 20 70 52 65 63 2d 3e 66  .    if( pRec->f
1bf6e 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20  lags & MEM_Real 
1bf6f 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1bf70 56 64 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e  VdbeIntegerAffin
1bf71 69 74 79 28 70 52 65 63 29 3b 0a 20 20 20 20 7d  ity(pRec);.    }
1bf72 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72  .  }.}../*.** Tr
1bf73 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65  y to convert the
1bf74 20 74 79 70 65 20 6f 66 20 61 20 66 75 6e 63 74   type of a funct
1bf75 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 6f 72 20  ion argument or 
1bf76 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 0a  a result column.
1bf77 2a 2a 20 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69  ** into a numeri
1bf78 63 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  c representation
1bf79 2e 20 20 55 73 65 20 65 69 74 68 65 72 20 49 4e  .  Use either IN
1bf7a 54 45 47 45 52 20 6f 72 20 52 45 41 4c 20 77 68  TEGER or REAL wh
1bf7b 69 63 68 65 76 65 72 0a 2a 2a 20 69 73 20 61 70  ichever.** is ap
1bf7c 70 72 6f 70 72 69 61 74 65 2e 20 20 42 75 74 20  propriate.  But 
1bf7d 6f 6e 6c 79 20 64 6f 20 74 68 65 20 63 6f 6e 76  only do the conv
1bf7e 65 72 73 69 6f 6e 20 69 66 20 69 74 20 69 73 20  ersion if it is 
1bf7f 70 6f 73 73 69 62 6c 65 20 77 69 74 68 6f 75 74  possible without
1bf80 0a 2a 2a 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f  .** loss of info
1bf81 72 6d 61 74 69 6f 6e 20 61 6e 64 20 72 65 74 75  rmation and retu
1bf82 72 6e 20 74 68 65 20 72 65 76 69 73 65 64 20 74  rn the revised t
1bf83 79 70 65 20 6f 66 20 74 68 65 20 61 72 67 75 6d  ype of the argum
1bf84 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ent..**.** This 
1bf85 69 73 20 61 6e 20 45 58 50 45 52 49 4d 45 4e 54  is an EXPERIMENT
1bf86 41 4c 20 61 70 69 20 61 6e 64 20 69 73 20 73 75  AL api and is su
1bf87 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20  bject to change 
1bf88 6f 72 20 72 65 6d 6f 76 61 6c 2e 0a 2a 2f 0a 53  or removal..*/.S
1bf89 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1bf8a 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65  lite3_value_nume
1bf8b 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74 65 33  ric_type(sqlite3
1bf8c 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20  _value *pVal){. 
1bf8d 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 28 4d 65   Mem *pMem = (Me
1bf8e 6d 2a 29 70 56 61 6c 3b 0a 20 20 61 70 70 6c 79  m*)pVal;.  apply
1bf8f 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28  NumericAffinity(
1bf90 70 4d 65 6d 29 3b 0a 20 20 73 74 6f 72 65 54 79  pMem);.  storeTy
1bf91 70 65 49 6e 66 6f 28 70 4d 65 6d 2c 20 30 29 3b  peInfo(pMem, 0);
1bf92 0a 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e  .  return pMem->
1bf93 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  type;.}../*.** E
1bf94 78 70 6f 72 74 65 64 20 76 65 72 73 69 6f 6e 20  xported version 
1bf95 6f 66 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79  of applyAffinity
1bf96 28 29 2e 20 54 68 69 73 20 6f 6e 65 20 77 6f 72  (). This one wor
1bf97 6b 73 20 6f 6e 20 73 71 6c 69 74 65 33 5f 76 61  ks on sqlite3_va
1bf98 6c 75 65 2a 2c 20 0a 2a 2a 20 6e 6f 74 20 74 68  lue*, .** not th
1bf99 65 20 69 6e 74 65 72 6e 61 6c 20 4d 65 6d 2a 20  e internal Mem* 
1bf9a 74 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  type..*/.SQLITE_
1bf9b 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1bf9c 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66  ite3ValueApplyAf
1bf9d 66 69 6e 69 74 79 28 0a 20 20 73 71 6c 69 74 65  finity(.  sqlite
1bf9e 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 0a  3_value *pVal, .
1bf9f 20 20 75 38 20 61 66 66 69 6e 69 74 79 2c 20 0a    u8 affinity, .
1bfa0 20 20 75 38 20 65 6e 63 0a 29 7b 0a 20 20 61 70    u8 enc.){.  ap
1bfa1 70 6c 79 41 66 66 69 6e 69 74 79 28 28 4d 65 6d  plyAffinity((Mem
1bfa2 20 2a 29 70 56 61 6c 2c 20 61 66 66 69 6e 69 74   *)pVal, affinit
1bfa3 79 2c 20 65 6e 63 29 3b 0a 7d 0a 0a 23 69 66 64  y, enc);.}..#ifd
1bfa4 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
1bfa5 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 6e 69  /*.** Write a ni
1bfa6 63 65 20 73 74 72 69 6e 67 20 72 65 70 72 65 73  ce string repres
1bfa7 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
1bfa8 63 6f 6e 74 65 6e 74 73 20 6f 66 20 63 65 6c 6c  contents of cell
1bfa9 20 70 4d 65 6d 0a 2a 2a 20 69 6e 74 6f 20 62 75   pMem.** into bu
1bfaa 66 66 65 72 20 7a 42 75 66 2c 20 6c 65 6e 67 74  ffer zBuf, lengt
1bfab 68 20 6e 42 75 66 2e 0a 2a 2f 0a 53 51 4c 49 54  h nBuf..*/.SQLIT
1bfac 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1bfad 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65  qlite3VdbeMemPre
1bfae 74 74 79 50 72 69 6e 74 28 4d 65 6d 20 2a 70 4d  ttyPrint(Mem *pM
1bfaf 65 6d 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b  em, char *zBuf){
1bfb0 0a 20 20 63 68 61 72 20 2a 7a 43 73 72 20 3d 20  .  char *zCsr = 
1bfb1 7a 42 75 66 3b 0a 20 20 69 6e 74 20 66 20 3d 20  zBuf;.  int f = 
1bfb2 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20  pMem->flags;..  
1bfb3 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
1bfb4 72 20 2a 63 6f 6e 73 74 20 65 6e 63 6e 61 6d 65  r *const encname
1bfb5 73 5b 5d 20 3d 20 7b 22 28 58 29 22 2c 20 22 28  s[] = {"(X)", "(
1bfb6 38 29 22 2c 20 22 28 31 36 4c 45 29 22 2c 20 22  8)", "(16LE)", "
1bfb7 28 31 36 42 45 29 22 7d 3b 0a 0a 20 20 69 66 28  (16BE)"};..  if(
1bfb8 20 66 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b 0a 20   f&MEM_Blob ){. 
1bfb9 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 68     int i;.    ch
1bfba 61 72 20 63 3b 0a 20 20 20 20 69 66 28 20 66 20  ar c;.    if( f 
1bfbb 26 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20  & MEM_Dyn ){.   
1bfbc 20 20 20 63 20 3d 20 27 7a 27 3b 0a 20 20 20 20     c = 'z';.    
1bfbd 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28    assert( (f & (
1bfbe 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45  MEM_Static|MEM_E
1bfbf 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20  phem))==0 );.   
1bfc0 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d   }else if( f & M
1bfc1 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20  EM_Static ){.   
1bfc2 20 20 20 63 20 3d 20 27 74 27 3b 0a 20 20 20 20     c = 't';.    
1bfc3 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28    assert( (f & (
1bfc4 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65  MEM_Dyn|MEM_Ephe
1bfc5 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65  m))==0 );.    }e
1bfc6 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f  lse if( f & MEM_
1bfc7 45 70 68 65 6d 20 29 7b 0a 20 20 20 20 20 20 63  Ephem ){.      c
1bfc8 20 3d 20 27 65 27 3b 0a 20 20 20 20 20 20 61 73   = 'e';.      as
1bfc9 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f  sert( (f & (MEM_
1bfca 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29  Static|MEM_Dyn))
1bfcb 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ==0 );.    }else
1bfcc 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 73 27 3b  {.      c = 's';
1bfcd 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
1bfce 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30  te3_snprintf(100
1bfcf 2c 20 7a 43 73 72 2c 20 22 25 63 22 2c 20 63 29  , zCsr, "%c", c)
1bfd0 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71  ;.    zCsr += sq
1bfd1 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43  lite3Strlen30(zC
1bfd2 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  sr);.    sqlite3
1bfd3 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a  _snprintf(100, z
1bfd4 43 73 72 2c 20 22 25 64 5b 22 2c 20 70 4d 65 6d  Csr, "%d[", pMem
1bfd5 2d 3e 6e 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b  ->n);.    zCsr +
1bfd6 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
1bfd7 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 66 6f 72  0(zCsr);.    for
1bfd8 28 69 3d 30 3b 20 69 3c 31 36 20 26 26 20 69 3c  (i=0; i<16 && i<
1bfd9 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20  pMem->n; i++){. 
1bfda 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
1bfdb 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c  rintf(100, zCsr,
1bfdc 20 22 25 30 32 58 22 2c 20 28 28 69 6e 74 29 70   "%02X", ((int)p
1bfdd 4d 65 6d 2d 3e 7a 5b 69 5d 20 26 20 30 78 46 46  Mem->z[i] & 0xFF
1bfde 29 29 3b 0a 20 20 20 20 20 20 7a 43 73 72 20 2b  ));.      zCsr +
1bfdf 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
1bfe0 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 7d 0a 20  0(zCsr);.    }. 
1bfe1 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 36     for(i=0; i<16
1bfe2 20 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 20 69   && i<pMem->n; i
1bfe3 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20  ++){.      char 
1bfe4 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 69 5d 3b 0a  z = pMem->z[i];.
1bfe5 20 20 20 20 20 20 69 66 28 20 7a 3c 33 32 20 7c        if( z<32 |
1bfe6 7c 20 7a 3e 31 32 36 20 29 20 2a 7a 43 73 72 2b  | z>126 ) *zCsr+
1bfe7 2b 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 65  + = '.';.      e
1bfe8 6c 73 65 20 2a 7a 43 73 72 2b 2b 20 3d 20 7a 3b  lse *zCsr++ = z;
1bfe9 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
1bfea 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30  te3_snprintf(100
1bfeb 2c 20 7a 43 73 72 2c 20 22 5d 25 73 22 2c 20 65  , zCsr, "]%s", e
1bfec 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65 6e  ncnames[pMem->en
1bfed 63 5d 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d  c]);.    zCsr +=
1bfee 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
1bfef 28 7a 43 73 72 29 3b 0a 20 20 20 20 69 66 28 20  (zCsr);.    if( 
1bff0 66 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a  f & MEM_Zero ){.
1bff1 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
1bff2 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72  printf(100, zCsr
1bff3 2c 22 2b 25 64 7a 22 2c 70 4d 65 6d 2d 3e 75 2e  ,"+%dz",pMem->u.
1bff4 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 20 20 7a 43  nZero);.      zC
1bff5 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72  sr += sqlite3Str
1bff6 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 20  len30(zCsr);.   
1bff7 20 7d 0a 20 20 20 20 2a 7a 43 73 72 20 3d 20 27   }.    *zCsr = '
1bff8 5c 30 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  \0';.  }else if(
1bff9 20 66 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a   f & MEM_Str ){.
1bffa 20 20 20 20 69 6e 74 20 6a 2c 20 6b 3b 0a 20 20      int j, k;.  
1bffb 20 20 7a 42 75 66 5b 30 5d 20 3d 20 27 20 27 3b    zBuf[0] = ' ';
1bffc 0a 20 20 20 20 69 66 28 20 66 20 26 20 4d 45 4d  .    if( f & MEM
1bffd 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 7a 42  _Dyn ){.      zB
1bffe 75 66 5b 31 5d 20 3d 20 27 7a 27 3b 0a 20 20 20  uf[1] = 'z';.   
1bfff 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20     assert( (f & 
1c000 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f  (MEM_Static|MEM_
1c001 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20  Ephem))==0 );.  
1c002 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20    }else if( f & 
1c003 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20  MEM_Static ){.  
1c004 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 74      zBuf[1] = 't
1c005 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ';.      assert(
1c006 20 28 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d   (f & (MEM_Dyn|M
1c007 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b  EM_Ephem))==0 );
1c008 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66  .    }else if( f
1c009 20 26 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b 0a   & MEM_Ephem ){.
1c00a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20        zBuf[1] = 
1c00b 27 65 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72  'e';.      asser
1c00c 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61  t( (f & (MEM_Sta
1c00d 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30  tic|MEM_Dyn))==0
1c00e 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   );.    }else{. 
1c00f 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27       zBuf[1] = '
1c010 73 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6b 20  s';.    }.    k 
1c011 3d 20 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 2;.    sqlite3
1c012 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 26  _snprintf(100, &
1c013 7a 42 75 66 5b 6b 5d 2c 20 22 25 64 22 2c 20 70  zBuf[k], "%d", p
1c014 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 6b 20 2b  Mem->n);.    k +
1c015 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
1c016 30 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 20  0(&zBuf[k]);.   
1c017 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5b 27   zBuf[k++] = '['
1c018 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a  ;.    for(j=0; j
1c019 3c 31 35 20 26 26 20 6a 3c 70 4d 65 6d 2d 3e 6e  <15 && j<pMem->n
1c01a 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38  ; j++){.      u8
1c01b 20 63 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 6a 5d 3b   c = pMem->z[j];
1c01c 0a 20 20 20 20 20 20 69 66 28 20 63 3e 3d 30 78  .      if( c>=0x
1c01d 32 30 20 26 26 20 63 3c 30 78 37 66 20 29 7b 0a  20 && c<0x7f ){.
1c01e 20 20 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b 2b          zBuf[k++
1c01f 5d 20 3d 20 63 3b 0a 20 20 20 20 20 20 7d 65 6c  ] = c;.      }el
1c020 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 42 75 66  se{.        zBuf
1c021 5b 6b 2b 2b 5d 20 3d 20 27 2e 27 3b 0a 20 20 20  [k++] = '.';.   
1c022 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a     }.    }.    z
1c023 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5d 27 3b 0a  Buf[k++] = ']';.
1c024 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1c025 69 6e 74 66 28 31 30 30 2c 26 7a 42 75 66 5b 6b  intf(100,&zBuf[k
1c026 5d 2c 20 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d  ], encnames[pMem
1c027 2d 3e 65 6e 63 5d 29 3b 0a 20 20 20 20 6b 20 2b  ->enc]);.    k +
1c028 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
1c029 30 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 20  0(&zBuf[k]);.   
1c02a 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 30 3b 0a   zBuf[k++] = 0;.
1c02b 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69    }.}.#endif..#i
1c02c 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
1c02d 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68  G./*.** Print th
1c02e 65 20 76 61 6c 75 65 20 6f 66 20 61 20 72 65 67  e value of a reg
1c02f 69 73 74 65 72 20 66 6f 72 20 74 72 61 63 69 6e  ister for tracin
1c030 67 20 70 75 72 70 6f 73 65 73 3a 0a 2a 2f 0a 73  g purposes:.*/.s
1c031 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 54 72  tatic void memTr
1c032 61 63 65 50 72 69 6e 74 28 46 49 4c 45 20 2a 6f  acePrint(FILE *o
1c033 75 74 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20 69  ut, Mem *p){.  i
1c034 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45  f( p->flags & ME
1c035 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 66 70  M_Null ){.    fp
1c036 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 4e 55 4c  rintf(out, " NUL
1c037 4c 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  L");.  }else if(
1c038 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45   (p->flags & (ME
1c039 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 29 3d  M_Int|MEM_Str))=
1c03a 3d 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74  =(MEM_Int|MEM_St
1c03b 72 29 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74  r) ){.    fprint
1c03c 66 28 6f 75 74 2c 20 22 20 73 69 3a 25 6c 6c 64  f(out, " si:%lld
1c03d 22 2c 20 70 2d 3e 75 2e 69 29 3b 0a 20 20 7d 65  ", p->u.i);.  }e
1c03e 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73  lse if( p->flags
1c03f 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20   & MEM_Int ){.  
1c040 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
1c041 20 69 3a 25 6c 6c 64 22 2c 20 70 2d 3e 75 2e 69   i:%lld", p->u.i
1c042 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
1c043 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
1c044 50 4f 49 4e 54 0a 20 20 7d 65 6c 73 65 20 69 66  POINT.  }else if
1c045 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  ( p->flags & MEM
1c046 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 66 70 72  _Real ){.    fpr
1c047 69 6e 74 66 28 6f 75 74 2c 20 22 20 72 3a 25 67  intf(out, " r:%g
1c048 22 2c 20 70 2d 3e 72 29 3b 0a 23 65 6e 64 69 66  ", p->r);.#endif
1c049 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e  .  }else if( p->
1c04a 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53  flags & MEM_RowS
1c04b 65 74 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74  et ){.    fprint
1c04c 66 28 6f 75 74 2c 20 22 20 28 72 6f 77 73 65 74  f(out, " (rowset
1c04d 29 22 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  )");.  }else{.  
1c04e 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30 30 5d    char zBuf[200]
1c04f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1c050 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28  eMemPrettyPrint(
1c051 70 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 70  p, zBuf);.    fp
1c052 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 22 29 3b  rintf(out, " ");
1c053 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74  .    fprintf(out
1c054 2c 20 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a 20  , "%s", zBuf);. 
1c055 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64   }.}.static void
1c056 20 72 65 67 69 73 74 65 72 54 72 61 63 65 28 46   registerTrace(F
1c057 49 4c 45 20 2a 6f 75 74 2c 20 69 6e 74 20 69 52  ILE *out, int iR
1c058 65 67 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20 66  eg, Mem *p){.  f
1c059 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 52 45 47  printf(out, "REG
1c05a 5b 25 64 5d 20 3d 20 22 2c 20 69 52 65 67 29 3b  [%d] = ", iReg);
1c05b 0a 20 20 6d 65 6d 54 72 61 63 65 50 72 69 6e 74  .  memTracePrint
1c05c 28 6f 75 74 2c 20 70 29 3b 0a 20 20 66 70 72 69  (out, p);.  fpri
1c05d 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a  ntf(out, "\n");.
1c05e 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  }.#endif..#ifdef
1c05f 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 23 20   SQLITE_DEBUG.# 
1c060 20 64 65 66 69 6e 65 20 52 45 47 49 53 54 45 52   define REGISTER
1c061 5f 54 52 41 43 45 28 52 2c 4d 29 20 69 66 28 70  _TRACE(R,M) if(p
1c062 2d 3e 74 72 61 63 65 29 72 65 67 69 73 74 65 72  ->trace)register
1c063 54 72 61 63 65 28 70 2d 3e 74 72 61 63 65 2c 52  Trace(p->trace,R
1c064 2c 4d 29 0a 23 65 6c 73 65 0a 23 20 20 64 65 66  ,M).#else.#  def
1c065 69 6e 65 20 52 45 47 49 53 54 45 52 5f 54 52 41  ine REGISTER_TRA
1c066 43 45 28 52 2c 4d 29 0a 23 65 6e 64 69 66 0a 0a  CE(R,M).#endif..
1c067 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f  .#ifdef VDBE_PRO
1c068 46 49 4c 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74  FILE../* .** hwt
1c069 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69  ime.h contains i
1c06a 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20  nline assembler 
1c06b 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65  code for impleme
1c06c 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70  nting .** high-p
1c06d 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e  erformance timin
1c06e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f  g routines..*/./
1c06f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
1c070 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20  nclude hwtime.h 
1c071 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
1c072 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   vdbe.c ********
1c073 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1c074 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
1c075 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65  egin file hwtime
1c076 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
1c077 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c078 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1c079 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37  *.** 2008 May 27
1c07a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
1c07b 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
1c07c 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
1c07d 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
1c07e 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
1c07f 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
1c080 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
1c081 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
1c082 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
1c083 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
1c084 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
1c085 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
1c086 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
1c087 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
1c088 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
1c089 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
1c08a 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
1c08b 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
1c08c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c08d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c08e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c08f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c090 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
1c091 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e  file contains in
1c092 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f  line asm code fo
1c093 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 69  r retrieving "hi
1c094 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a  gh-performance".
1c095 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20  ** counters for 
1c096 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a  x86 class CPUs..
1c097 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d  **.** $Id: hwtim
1c098 65 2e 68 2c 76 20 31 2e 33 20 32 30 30 38 2f 30  e.h,v 1.3 2008/0
1c099 38 2f 30 31 20 31 34 3a 33 33 3a 31 35 20 73 68  8/01 14:33:15 sh
1c09a 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66  ane Exp $.*/.#if
1c09b 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a  ndef _HWTIME_H_.
1c09c 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f  #define _HWTIME_
1c09d 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  H_../*.** The fo
1c09e 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20  llowing routine 
1c09f 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65  only works on pe
1c0a0 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20  ntium-class (or 
1c0a1 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72  newer) processor
1c0a2 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68  s..** It uses th
1c0a3 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74  e RDTSC opcode t
1c0a4 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c 65  o read the cycle
1c0a5 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74   count value out
1c0a6 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65   of the.** proce
1c0a7 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73  ssor and returns
1c0a8 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68   that value.  Th
1c0a9 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 66  is can be used f
1c0aa 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70  or high-res.** p
1c0ab 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66  rofiling..*/.#if
1c0ac 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43   (defined(__GNUC
1c0ad 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  __) || defined(_
1c0ae 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20  MSC_VER)) && \. 
1c0af 20 20 20 20 20 28 64 65 66 69 6e 65 64 28 69 33       (defined(i3
1c0b0 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  86) || defined(_
1c0b1 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69  _i386__) || defi
1c0b2 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20  ned(_M_IX86)).. 
1c0b3 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47   #if defined(__G
1c0b4 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69  NUC__)..  __inli
1c0b5 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74  ne__ sqlite_uint
1c0b6 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  64 sqlite3Hwtime
1c0b7 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73  (void){.     uns
1c0b8 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69  igned int lo, hi
1c0b9 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f  ;.     __asm__ _
1c0ba 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64  _volatile__ ("rd
1c0bb 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29  tsc" : "=a" (lo)
1c0bc 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20  , "=d" (hi));.  
1c0bd 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74     return (sqlit
1c0be 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33  e_uint64)hi << 3
1c0bf 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23  2 | lo;.  }..  #
1c0c0 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53  elif defined(_MS
1c0c1 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c  C_VER)..  __decl
1c0c2 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e  spec(naked) __in
1c0c3 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74  line sqlite_uint
1c0c4 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74  64 __cdecl sqlit
1c0c5 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a  e3Hwtime(void){.
1c0c6 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20       __asm {.   
1c0c7 20 20 20 20 20 72 64 74 73 63 0a 20 20 20 20 20       rdtsc.     
1c0c8 20 20 20 72 65 74 20 20 20 20 20 20 20 3b 20 72     ret       ; r
1c0c9 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45  eturn value at E
1c0ca 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20  DX:EAX.     }.  
1c0cb 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c  }..  #endif..#el
1c0cc 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e  if (defined(__GN
1c0cd 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64  UC__) && defined
1c0ce 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20  (__x86_64__)).. 
1c0cf 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69   __inline__ sqli
1c0d0 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65  te_uint64 sqlite
1c0d1 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20  3Hwtime(void){. 
1c0d2 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f       unsigned lo
1c0d3 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f  ng val;.      __
1c0d4 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65  asm__ __volatile
1c0d5 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d  __ ("rdtsc" : "=
1c0d6 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20  A" (val));.     
1c0d7 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d   return val;.  }
1c0d8 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65  . .#elif (define
1c0d9 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64  d(__GNUC__) && d
1c0da 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29  efined(__ppc__))
1c0db 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73  ..  __inline__ s
1c0dc 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c  qlite_uint64 sql
1c0dd 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
1c0de 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64  {.      unsigned
1c0df 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61   long long retva
1c0e0 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65  l;.      unsigne
1c0e1 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20  d long junk;.   
1c0e2 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c     __asm__ __vol
1c0e3 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20  atile__ ("\n\.  
1c0e4 20 20 20 20 20 20 20 20 31 3a 20 20 20 20 20 20          1:      
1c0e5 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20  mftbu   %1\n\.  
1c0e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c0e7 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20  mftb    %L0\n\. 
1c0e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c0e9 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20   mftbu   %0\n\. 
1c0ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c0eb 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e   cmpw    %0,%1\n
1c0ec 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
1c0ed 20 20 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a      bne     1b".
1c0ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c0ef 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c    : "=r" (retval
1c0f0 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b  ), "=r" (junk));
1c0f1 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 65  .      return re
1c0f2 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65  tval;.  }..#else
1c0f3 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20  ..  #error Need 
1c0f4 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
1c0f5 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  f sqlite3Hwtime(
1c0f6 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66  ) for your platf
1c0f7 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  orm...  /*.  ** 
1c0f8 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f  To compile witho
1c0f9 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20  ut implementing 
1c0fa 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20  sqlite3Hwtime() 
1c0fb 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72  for your platfor
1c0fc 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20  m,.  ** you can 
1c0fd 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65  remove the above
1c0fe 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20   #error and use 
1c0ff 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20  the following.  
1c100 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e  ** stub function
1c101 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65  .  You will lose
1c102 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20   timing support 
1c103 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66  for many.  ** of
1c104 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 20 61   the debugging a
1c105 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69  nd testing utili
1c106 74 69 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f  ties, but it sho
1c107 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73  uld at.  ** leas
1c108 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75  t compile and ru
1c109 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  n..  */.SQLITE_P
1c10a 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f  RIVATE   sqlite_
1c10b 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77  uint64 sqlite3Hw
1c10c 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75  time(void){ retu
1c10d 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74  rn ((sqlite_uint
1c10e 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66  64)0); }..#endif
1c10f 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  ..#endif /* !def
1c110 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29  ined(_HWTIME_H_)
1c111 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
1c112 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69  **** End of hwti
1c113 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  me.h ***********
1c114 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c115 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c116 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
1c117 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
1c118 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
1c119 66 20 69 6e 20 76 64 62 65 2e 63 20 2a 2a 2a 2a  f in vdbe.c ****
1c11a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c11b 2a 2a 2a 2f 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ***/..#endif../*
1c11c 0a 2a 2a 20 54 68 65 20 43 48 45 43 4b 5f 46 4f  .** The CHECK_FO
1c11d 52 5f 49 4e 54 45 52 52 55 50 54 20 6d 61 63 72  R_INTERRUPT macr
1c11e 6f 20 64 65 66 69 6e 65 64 20 68 65 72 65 20 6c  o defined here l
1c11f 6f 6f 6b 73 20 74 6f 20 73 65 65 20 69 66 20 74  ooks to see if t
1c120 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e  he.** sqlite3_in
1c121 74 65 72 72 75 70 74 28 29 20 72 6f 75 74 69 6e  terrupt() routin
1c122 65 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65  e has been calle
1c123 64 2e 20 20 49 66 20 69 74 20 68 61 73 20 62 65  d.  If it has be
1c124 65 6e 2c 20 74 68 65 6e 0a 2a 2a 20 70 72 6f 63  en, then.** proc
1c125 65 73 73 69 6e 67 20 6f 66 20 74 68 65 20 56 44  essing of the VD
1c126 42 45 20 70 72 6f 67 72 61 6d 20 69 73 20 69 6e  BE program is in
1c127 74 65 72 72 75 70 74 65 64 2e 0a 2a 2a 0a 2a 2a  terrupted..**.**
1c128 20 54 68 69 73 20 6d 61 63 72 6f 20 61 64 64 65   This macro adde
1c129 64 20 74 6f 20 65 76 65 72 79 20 69 6e 73 74 72  d to every instr
1c12a 75 63 74 69 6f 6e 20 74 68 61 74 20 64 6f 65 73  uction that does
1c12b 20 61 20 6a 75 6d 70 20 69 6e 20 6f 72 64 65 72   a jump in order
1c12c 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74   to.** implement
1c12d 20 61 20 6c 6f 6f 70 2e 20 20 54 68 69 73 20 74   a loop.  This t
1c12e 65 73 74 20 75 73 65 64 20 74 6f 20 62 65 20 6f  est used to be o
1c12f 6e 20 65 76 65 72 79 20 73 69 6e 67 6c 65 20 69  n every single i
1c130 6e 73 74 72 75 63 74 69 6f 6e 2c 0a 2a 2a 20 62  nstruction,.** b
1c131 75 74 20 74 68 61 74 20 6d 65 61 6e 74 20 77 65  ut that meant we
1c132 20 6d 6f 72 65 20 74 65 73 74 69 6e 67 20 74 68   more testing th
1c133 61 74 20 77 65 20 6e 65 65 64 65 64 2e 20 20 42  at we needed.  B
1c134 79 20 6f 6e 6c 79 20 74 65 73 74 69 6e 67 20 74  y only testing t
1c135 68 65 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 6a 75  he.** flag on ju
1c136 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 2c  mp instructions,
1c137 20 77 65 20 67 65 74 20 61 20 28 73 6d 61 6c 6c   we get a (small
1c138 29 20 73 70 65 65 64 20 69 6d 70 72 6f 76 65 6d  ) speed improvem
1c139 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ent..*/.#define 
1c13a 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52  CHECK_FOR_INTERR
1c13b 55 50 54 20 5c 0a 20 20 20 69 66 28 20 64 62 2d  UPT \.   if( db-
1c13c 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65  >u1.isInterrupte
1c13d 64 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  d ) goto abort_d
1c13e 75 65 5f 74 6f 5f 69 6e 74 65 72 72 75 70 74 3b  ue_to_interrupt;
1c13f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
1c140 44 45 42 55 47 0a 73 74 61 74 69 63 20 69 6e 74  DEBUG.static int
1c141 20 66 69 6c 65 45 78 69 73 74 73 28 73 71 6c 69   fileExists(sqli
1c142 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
1c143 68 61 72 20 2a 7a 46 69 6c 65 29 7b 0a 20 20 69  har *zFile){.  i
1c144 6e 74 20 72 65 73 20 3d 20 30 3b 0a 20 20 69 6e  nt res = 0;.  in
1c145 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
1c146 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
1c147 54 45 53 54 0a 20 20 2f 2a 20 49 66 20 77 65 20  TEST.  /* If we 
1c148 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 74 65  are currently te
1c149 73 74 69 6e 67 20 49 4f 20 65 72 72 6f 72 73 2c  sting IO errors,
1c14a 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 63 61 6c   then do not cal
1c14b 6c 20 4f 73 41 63 63 65 73 73 28 29 20 74 6f 0a  l OsAccess() to.
1c14c 20 20 2a 2a 20 74 65 73 74 20 66 6f 72 20 74 68    ** test for th
1c14d 65 20 70 72 65 73 65 6e 63 65 20 6f 66 20 7a 46  e presence of zF
1c14e 69 6c 65 2e 20 54 68 69 73 20 69 73 20 62 65 63  ile. This is bec
1c14f 61 75 73 65 20 61 6e 79 20 49 4f 20 65 72 72 6f  ause any IO erro
1c150 72 20 74 68 61 74 0a 20 20 2a 2a 20 6f 63 63 75  r that.  ** occu
1c151 72 73 20 68 65 72 65 20 77 69 6c 6c 20 6e 6f 74  rs here will not
1c152 20 62 65 20 72 65 70 6f 72 74 65 64 2c 20 63 61   be reported, ca
1c153 75 73 69 6e 67 20 74 68 65 20 74 65 73 74 20 74  using the test t
1c154 6f 20 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20 65  o fail..  */.  e
1c155 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65  xtern int sqlite
1c156 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69  3_io_error_pendi
1c157 6e 67 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  ng;.  if( sqlite
1c158 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69  3_io_error_pendi
1c159 6e 67 3c 3d 30 20 29 0a 23 65 6e 64 69 66 0a 20  ng<=0 ).#endif. 
1c15a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
1c15b 73 41 63 63 65 73 73 28 64 62 2d 3e 70 56 66 73  sAccess(db->pVfs
1c15c 2c 20 7a 46 69 6c 65 2c 20 53 51 4c 49 54 45 5f  , zFile, SQLITE_
1c15d 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26  ACCESS_EXISTS, &
1c15e 72 65 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 28  res);.  return (
1c15f 72 65 73 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  res && rc==SQLIT
1c160 45 5f 4f 4b 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  E_OK);.}.#endif.
1c161 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
1c162 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
1c163 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c  ion is only call
1c164 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61  ed from within a
1c165 6e 20 61 73 73 65 72 74 28 29 20 65 78 70 72 65  n assert() expre
1c166 73 73 69 6f 6e 2e 20 49 74 0a 2a 2a 20 63 68 65  ssion. It.** che
1c167 63 6b 73 20 74 68 61 74 20 74 68 65 20 73 71 6c  cks that the sql
1c168 69 74 65 33 2e 6e 54 72 61 6e 73 61 63 74 69 6f  ite3.nTransactio
1c169 6e 20 76 61 72 69 61 62 6c 65 20 69 73 20 63 6f  n variable is co
1c16a 72 72 65 63 74 6c 79 20 73 65 74 20 74 6f 0a 2a  rrectly set to.*
1c16b 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
1c16c 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20  non-transaction 
1c16d 73 61 76 65 70 6f 69 6e 74 73 20 63 75 72 72 65  savepoints curre
1c16e 6e 74 6c 79 20 69 6e 20 74 68 65 20 0a 2a 2a 20  ntly in the .** 
1c16f 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 74 61 72  linked list star
1c170 74 69 6e 67 20 61 74 20 73 71 6c 69 74 65 33 2e  ting at sqlite3.
1c171 70 53 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 20 0a  pSavepoint..** .
1c172 2a 2a 20 55 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20  ** Usage:.**.** 
1c173 20 20 20 20 61 73 73 65 72 74 28 20 63 68 65 63      assert( chec
1c174 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 28  kSavepointCount(
1c175 64 62 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63  db) );.*/.static
1c176 20 69 6e 74 20 63 68 65 63 6b 53 61 76 65 70 6f   int checkSavepo
1c177 69 6e 74 43 6f 75 6e 74 28 73 71 6c 69 74 65 33  intCount(sqlite3
1c178 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 6e 20 3d   *db){.  int n =
1c179 20 30 3b 0a 20 20 53 61 76 65 70 6f 69 6e 74 20   0;.  Savepoint 
1c17a 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 64 62 2d 3e  *p;.  for(p=db->
1c17b 70 53 61 76 65 70 6f 69 6e 74 3b 20 70 3b 20 70  pSavepoint; p; p
1c17c 3d 70 2d 3e 70 4e 65 78 74 29 20 6e 2b 2b 3b 0a  =p->pNext) n++;.
1c17d 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 28 64 62    assert( n==(db
1c17e 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 2b 20 64  ->nSavepoint + d
1c17f 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e  b->isTransaction
1c180 53 61 76 65 70 6f 69 6e 74 29 20 29 3b 0a 20 20  Savepoint) );.  
1c181 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64  return 1;.}.#end
1c182 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74  if../*.** Execut
1c183 65 20 61 73 20 6d 75 63 68 20 6f 66 20 61 20 56  e as much of a V
1c184 44 42 45 20 70 72 6f 67 72 61 6d 20 61 73 20 77  DBE program as w
1c185 65 20 63 61 6e 20 74 68 65 6e 20 72 65 74 75 72  e can then retur
1c186 6e 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33  n..**.** sqlite3
1c187 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 29 20  VdbeMakeReady() 
1c188 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 62  must be called b
1c189 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75 74 69  efore this routi
1c18a 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a  ne in order to.*
1c18b 2a 20 63 6c 6f 73 65 20 74 68 65 20 70 72 6f 67  * close the prog
1c18c 72 61 6d 20 77 69 74 68 20 61 20 66 69 6e 61 6c  ram with a final
1c18d 20 4f 50 5f 48 61 6c 74 20 61 6e 64 20 74 6f 20   OP_Halt and to 
1c18e 73 65 74 20 75 70 20 74 68 65 20 63 61 6c 6c 62  set up the callb
1c18f 61 63 6b 73 0a 2a 2a 20 61 6e 64 20 74 68 65 20  acks.** and the 
1c190 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 70 6f  error message po
1c191 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  inter..**.** Whe
1c192 6e 65 76 65 72 20 61 20 72 6f 77 20 6f 72 20 72  never a row or r
1c193 65 73 75 6c 74 20 64 61 74 61 20 69 73 20 61 76  esult data is av
1c194 61 69 6c 61 62 6c 65 2c 20 74 68 69 73 20 72 6f  ailable, this ro
1c195 75 74 69 6e 65 20 77 69 6c 6c 20 65 69 74 68 65  utine will eithe
1c196 72 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20  r.** invoke the 
1c197 72 65 73 75 6c 74 20 63 61 6c 6c 62 61 63 6b 20  result callback 
1c198 28 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65  (if there is one
1c199 29 20 6f 72 20 72 65 74 75 72 6e 20 77 69 74 68  ) or return with
1c19a 0a 2a 2a 20 53 51 4c 49 54 45 5f 52 4f 57 2e 0a  .** SQLITE_ROW..
1c19b 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 61 74 74 65  **.** If an atte
1c19c 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f  mpt is made to o
1c19d 70 65 6e 20 61 20 6c 6f 63 6b 65 64 20 64 61 74  pen a locked dat
1c19e 61 62 61 73 65 2c 20 74 68 65 6e 20 74 68 69 73  abase, then this
1c19f 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 69 6c 6c   routine.** will
1c1a0 20 65 69 74 68 65 72 20 69 6e 76 6f 6b 65 20 74   either invoke t
1c1a1 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b  he busy callback
1c1a2 20 28 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e   (if there is on
1c1a3 65 29 20 6f 72 20 69 74 20 77 69 6c 6c 0a 2a 2a  e) or it will.**
1c1a4 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
1c1a5 55 53 59 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  USY..**.** If an
1c1a6 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 61   error occurs, a
1c1a7 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
1c1a8 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 6d 65  is written to me
1c1a9 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 0a 2a 2a  mory obtained.**
1c1aa 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61   from sqlite3_ma
1c1ab 6c 6c 6f 63 28 29 20 61 6e 64 20 70 2d 3e 7a 45  lloc() and p->zE
1c1ac 72 72 4d 73 67 20 69 73 20 6d 61 64 65 20 74 6f  rrMsg is made to
1c1ad 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 6d   point to that m
1c1ae 65 6d 6f 72 79 2e 0a 2a 2a 20 54 68 65 20 65 72  emory..** The er
1c1af 72 6f 72 20 63 6f 64 65 20 69 73 20 73 74 6f 72  ror code is stor
1c1b0 65 64 20 69 6e 20 70 2d 3e 72 63 20 61 6e 64 20  ed in p->rc and 
1c1b1 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  this routine ret
1c1b2 75 72 6e 73 20 53 51 4c 49 54 45 5f 45 52 52 4f  urns SQLITE_ERRO
1c1b3 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  R..**.** If the 
1c1b4 63 61 6c 6c 62 61 63 6b 20 65 76 65 72 20 72 65  callback ever re
1c1b5 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  turns non-zero, 
1c1b6 74 68 65 6e 20 74 68 65 20 70 72 6f 67 72 61 6d  then the program
1c1b7 20 65 78 69 74 73 0a 2a 2a 20 69 6d 6d 65 64 69   exits.** immedi
1c1b8 61 74 65 6c 79 2e 20 20 54 68 65 72 65 20 77 69  ately.  There wi
1c1b9 6c 6c 20 62 65 20 6e 6f 20 65 72 72 6f 72 20 6d  ll be no error m
1c1ba 65 73 73 61 67 65 20 62 75 74 20 74 68 65 20 70  essage but the p
1c1bb 2d 3e 72 63 20 66 69 65 6c 64 20 69 73 0a 2a 2a  ->rc field is.**
1c1bc 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 41   set to SQLITE_A
1c1bd 42 4f 52 54 20 61 6e 64 20 74 68 69 73 20 72 6f  BORT and this ro
1c1be 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72  utine will retur
1c1bf 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a  n SQLITE_ERROR..
1c1c0 2a 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72 79 20 61  **.** A memory a
1c1c1 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20  llocation error 
1c1c2 63 61 75 73 65 73 20 70 2d 3e 72 63 20 74 6f 20  causes p->rc to 
1c1c3 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45  be set to SQLITE
1c1c4 5f 4e 4f 4d 45 4d 20 61 6e 64 20 74 68 69 73 0a  _NOMEM and this.
1c1c5 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 72 65  ** routine to re
1c1c6 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
1c1c7 52 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 20 66  R..**.** Other f
1c1c8 61 74 61 6c 20 65 72 72 6f 72 73 20 72 65 74 75  atal errors retu
1c1c9 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e  rn SQLITE_ERROR.
1c1ca 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 69  .**.** After thi
1c1cb 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 66 69  s routine has fi
1c1cc 6e 69 73 68 65 64 2c 20 73 71 6c 69 74 65 33 56  nished, sqlite3V
1c1cd 64 62 65 46 69 6e 61 6c 69 7a 65 28 29 20 73 68  dbeFinalize() sh
1c1ce 6f 75 6c 64 20 62 65 0a 2a 2a 20 75 73 65 64 20  ould be.** used 
1c1cf 74 6f 20 63 6c 65 61 6e 20 75 70 20 74 68 65 20  to clean up the 
1c1d0 6d 65 73 73 20 74 68 61 74 20 77 61 73 20 6c 65  mess that was le
1c1d1 66 74 20 62 65 68 69 6e 64 2e 0a 2a 2f 0a 53 51  ft behind..*/.SQ
1c1d2 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1c1d3 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63   sqlite3VdbeExec
1c1d4 28 0a 20 20 56 64 62 65 20 2a 70 20 20 20 20 20  (.  Vdbe *p     
1c1d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1c1d6 2a 20 54 68 65 20 56 44 42 45 20 2a 2f 0a 29 7b  * The VDBE */.){
1c1d7 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20 20 20  .  int pc;      
1c1d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1c1d9 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75   The program cou
1c1da 6e 74 65 72 20 2a 2f 0a 20 20 4f 70 20 2a 70 4f  nter */.  Op *pO
1c1db 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
1c1dc 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
1c1dd 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69  operation */.  i
1c1de 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1c1df 4b 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c  K;        /* Val
1c1e0 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a  ue to return */.
1c1e1 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1c1e2 70 2d 3e 64 62 3b 20 20 20 20 20 20 20 2f 2a 20  p->db;       /* 
1c1e3 54 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  The database */.
1c1e4 20 20 75 38 20 65 6e 63 6f 64 69 6e 67 20 3d 20    u8 encoding = 
1c1e5 45 4e 43 28 64 62 29 3b 20 20 20 20 20 2f 2a 20  ENC(db);     /* 
1c1e6 54 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 63  The database enc
1c1e7 6f 64 69 6e 67 20 2a 2f 0a 20 20 4d 65 6d 20 2a  oding */.  Mem *
1c1e8 70 49 6e 31 20 3d 20 30 3b 20 20 20 20 20 20 20  pIn1 = 0;       
1c1e9 20 20 20 20 20 20 2f 2a 20 31 73 74 20 69 6e 70        /* 1st inp
1c1ea 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  ut operand */.  
1c1eb 4d 65 6d 20 2a 70 49 6e 32 20 3d 20 30 3b 20 20  Mem *pIn2 = 0;  
1c1ec 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 6e             /* 2n
1c1ed 64 20 69 6e 70 75 74 20 6f 70 65 72 61 6e 64 20  d input operand 
1c1ee 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 33 20 3d  */.  Mem *pIn3 =
1c1ef 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1c1f0 2f 2a 20 33 72 64 20 69 6e 70 75 74 20 6f 70 65  /* 3rd input ope
1c1f1 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  rand */.  Mem *p
1c1f2 4f 75 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Out = 0;        
1c1f3 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 6f       /* Output o
1c1f4 70 65 72 61 6e 64 20 2a 2f 0a 20 20 75 38 20 6f  perand */.  u8 o
1c1f5 70 50 72 6f 70 65 72 74 79 3b 0a 20 20 69 6e 74  pProperty;.  int
1c1f6 20 69 43 6f 6d 70 61 72 65 20 3d 20 30 3b 20 20   iCompare = 0;  
1c1f7 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c          /* Resul
1c1f8 74 20 6f 66 20 6c 61 73 74 20 4f 50 5f 43 6f 6d  t of last OP_Com
1c1f9 70 61 72 65 20 6f 70 65 72 61 74 69 6f 6e 20 2a  pare operation *
1c1fa 2f 0a 20 20 69 6e 74 20 2a 61 50 65 72 6d 75 74  /.  int *aPermut
1c1fb 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f  e = 0;         /
1c1fc 2a 20 50 65 72 6d 75 74 61 74 69 6f 6e 20 6f 66  * Permutation of
1c1fd 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 4f 50 5f   columns for OP_
1c1fe 43 6f 6d 70 61 72 65 20 2a 2f 0a 23 69 66 64 65  Compare */.#ifde
1c1ff 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20  f VDBE_PROFILE. 
1c200 20 75 36 34 20 73 74 61 72 74 3b 20 20 20 20 20   u64 start;     
1c201 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
1c202 50 55 20 63 6c 6f 63 6b 20 63 6f 75 6e 74 20 61  PU clock count a
1c203 74 20 73 74 61 72 74 20 6f 66 20 6f 70 63 6f 64  t start of opcod
1c204 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 69 67 50  e */.  int origP
1c205 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
1c206 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75    /* Program cou
1c207 6e 74 65 72 20 61 74 20 73 74 61 72 74 20 6f 66  nter at start of
1c208 20 6f 70 63 6f 64 65 20 2a 2f 0a 23 65 6e 64 69   opcode */.#endi
1c209 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  f.#ifndef SQLITE
1c20a 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43  _OMIT_PROGRESS_C
1c20b 41 4c 4c 42 41 43 4b 0a 20 20 69 6e 74 20 6e 50  ALLBACK.  int nP
1c20c 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b 20  rogressOps = 0; 
1c20d 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 73 20       /* Opcodes 
1c20e 65 78 65 63 75 74 65 64 20 73 69 6e 63 65 20 70  executed since p
1c20f 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b  rogress callback
1c210 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 2f 2a  . */.#endif.  /*
1c211 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c212 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c213 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c214 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c215 2a 2a 2a 0a 20 20 2a 2a 20 41 75 74 6f 6d 61 74  ***.  ** Automat
1c216 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64  ically generated
1c217 20 63 6f 64 65 0a 20 20 2a 2a 0a 20 20 2a 2a 20   code.  **.  ** 
1c218 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 75 6e  The following un
1c219 69 6f 6e 20 69 73 20 61 75 74 6f 6d 61 74 69 63  ion is automatic
1c21a 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20 62  ally generated b
1c21b 79 20 74 68 65 0a 20 20 2a 2a 20 76 64 62 65 2d  y the.  ** vdbe-
1c21c 63 6f 6d 70 72 65 73 73 2e 74 63 6c 20 73 63 72  compress.tcl scr
1c21d 69 70 74 2e 20 20 54 68 65 20 70 75 72 70 6f 73  ipt.  The purpos
1c21e 65 20 6f 66 20 74 68 69 73 20 75 6e 69 6f 6e 20  e of this union 
1c21f 69 73 20 74 6f 0a 20 20 2a 2a 20 72 65 64 75 63  is to.  ** reduc
1c220 65 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20  e the amount of 
1c221 73 74 61 63 6b 20 73 70 61 63 65 20 72 65 71 75  stack space requ
1c222 69 72 65 64 20 62 79 20 74 68 69 73 20 66 75 6e  ired by this fun
1c223 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53 65 65 20  ction..  ** See 
1c224 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 74 68 65 20  comments in the 
1c225 76 64 62 65 2d 63 6f 6d 70 72 65 73 73 2e 74 63  vdbe-compress.tc
1c226 6c 20 73 63 72 69 70 74 20 66 6f 72 20 64 65 74  l script for det
1c227 61 69 6c 73 2e 0a 20 20 2a 2f 0a 20 20 75 6e 69  ails..  */.  uni
1c228 6f 6e 20 76 64 62 65 45 78 65 63 55 6e 69 6f 6e  on vdbeExecUnion
1c229 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50   {.    struct OP
1c22a 5f 59 69 65 6c 64 5f 73 74 61 63 6b 5f 76 61 72  _Yield_stack_var
1c22b 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 63  s {.      int pc
1c22c 44 65 73 74 3b 0a 20 20 20 20 7d 20 61 61 3b 0a  Dest;.    } aa;.
1c22d 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 56 61      struct OP_Va
1c22e 72 69 61 62 6c 65 5f 73 74 61 63 6b 5f 76 61 72  riable_stack_var
1c22f 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 31  s {.      int p1
1c230 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61  ;          /* Va
1c231 72 69 61 62 6c 65 20 74 6f 20 63 6f 70 79 20 66  riable to copy f
1c232 72 6f 6d 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  rom */.      int
1c233 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 2f 2a   p2;          /*
1c234 20 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70   Register to cop
1c235 79 20 74 6f 20 2a 2f 0a 20 20 20 20 20 20 69 6e  y to */.      in
1c236 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f  t n;           /
1c237 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76 61 6c 75  * Number of valu
1c238 65 73 20 6c 65 66 74 20 74 6f 20 63 6f 70 79 20  es left to copy 
1c239 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 56  */.      Mem *pV
1c23a 61 72 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 6c  ar;       /* Val
1c23b 75 65 20 62 65 69 6e 67 20 74 72 61 6e 73 66 65  ue being transfe
1c23c 72 72 65 64 20 2a 2f 0a 20 20 20 20 7d 20 61 62  rred */.    } ab
1c23d 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c23e 4d 6f 76 65 5f 73 74 61 63 6b 5f 76 61 72 73 20  Move_stack_vars 
1c23f 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4d  {.      char *zM
1c240 61 6c 6c 6f 63 3b 20 20 20 2f 2a 20 48 6f 6c 64  alloc;   /* Hold
1c241 69 6e 67 20 76 61 72 69 61 62 6c 65 20 66 6f 72  ing variable for
1c242 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72   allocated memor
1c243 79 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e  y */.      int n
1c244 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
1c245 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74 65  umber of registe
1c246 72 73 20 6c 65 66 74 20 74 6f 20 63 6f 70 79 20  rs left to copy 
1c247 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 31 3b  */.      int p1;
1c248 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
1c249 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20 66 72  ister to copy fr
1c24a 6f 6d 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  om */.      int 
1c24b 70 32 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  p2;          /* 
1c24c 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70 79  Register to copy
1c24d 20 74 6f 20 2a 2f 0a 20 20 20 20 7d 20 61 63 3b   to */.    } ac;
1c24e 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52  .    struct OP_R
1c24f 65 73 75 6c 74 52 6f 77 5f 73 74 61 63 6b 5f 76  esultRow_stack_v
1c250 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20  ars {.      Mem 
1c251 2a 70 4d 65 6d 3b 0a 20 20 20 20 20 20 69 6e 74  *pMem;.      int
1c252 20 69 3b 0a 20 20 20 20 7d 20 61 64 3b 0a 20 20   i;.    } ad;.  
1c253 20 20 73 74 72 75 63 74 20 4f 50 5f 43 6f 6e 63    struct OP_Conc
1c254 61 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  at_stack_vars {.
1c255 20 20 20 20 20 20 69 36 34 20 6e 42 79 74 65 3b        i64 nByte;
1c256 0a 20 20 20 20 7d 20 61 65 3b 0a 20 20 20 20 73  .    } ae;.    s
1c257 74 72 75 63 74 20 4f 50 5f 52 65 6d 61 69 6e 64  truct OP_Remaind
1c258 65 72 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  er_stack_vars {.
1c259 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 3b        int flags;
1c25a 20 20 20 20 20 20 2f 2a 20 43 6f 6d 62 69 6e 65        /* Combine
1c25b 64 20 4d 45 4d 5f 2a 20 66 6c 61 67 73 20 66 72  d MEM_* flags fr
1c25c 6f 6d 20 62 6f 74 68 20 69 6e 70 75 74 73 20 2a  om both inputs *
1c25d 2f 0a 20 20 20 20 20 20 69 36 34 20 69 41 3b 20  /.      i64 iA; 
1c25e 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67          /* Integ
1c25f 65 72 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74  er value of left
1c260 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20   operand */.    
1c261 20 20 69 36 34 20 69 42 3b 20 20 20 20 20 20 20    i64 iB;       
1c262 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c    /* Integer val
1c263 75 65 20 6f 66 20 72 69 67 68 74 20 6f 70 65 72  ue of right oper
1c264 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20 64 6f 75  and */.      dou
1c265 62 6c 65 20 72 41 3b 20 20 20 20 20 20 2f 2a 20  ble rA;      /* 
1c266 52 65 61 6c 20 76 61 6c 75 65 20 6f 66 20 6c 65  Real value of le
1c267 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  ft operand */.  
1c268 20 20 20 20 64 6f 75 62 6c 65 20 72 42 3b 20 20      double rB;  
1c269 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c 75      /* Real valu
1c26a 65 20 6f 66 20 72 69 67 68 74 20 6f 70 65 72 61  e of right opera
1c26b 6e 64 20 2a 2f 0a 20 20 20 20 7d 20 61 66 3b 0a  nd */.    } af;.
1c26c 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 46 75      struct OP_Fu
1c26d 6e 63 74 69 6f 6e 5f 73 74 61 63 6b 5f 76 61 72  nction_stack_var
1c26e 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b  s {.      int i;
1c26f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 41 72 67  .      Mem *pArg
1c270 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
1c271 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 20  context ctx;.   
1c272 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65     sqlite3_value
1c273 20 2a 2a 61 70 56 61 6c 3b 0a 20 20 20 20 20 20   **apVal;.      
1c274 69 6e 74 20 6e 3b 0a 20 20 20 20 7d 20 61 67 3b  int n;.    } ag;
1c275 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53  .    struct OP_S
1c276 68 69 66 74 52 69 67 68 74 5f 73 74 61 63 6b 5f  hiftRight_stack_
1c277 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 36 34  vars {.      i64
1c278 20 61 3b 0a 20 20 20 20 20 20 69 36 34 20 62 3b   a;.      i64 b;
1c279 0a 20 20 20 20 7d 20 61 68 3b 0a 20 20 20 20 73  .    } ah;.    s
1c27a 74 72 75 63 74 20 4f 50 5f 47 65 5f 73 74 61 63  truct OP_Ge_stac
1c27b 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69  k_vars {.      i
1c27c 6e 74 20 66 6c 61 67 73 3b 0a 20 20 20 20 20 20  nt flags;.      
1c27d 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 63  int res;.      c
1c27e 68 61 72 20 61 66 66 69 6e 69 74 79 3b 0a 20 20  har affinity;.  
1c27f 20 20 7d 20 61 69 3b 0a 20 20 20 20 73 74 72 75    } ai;.    stru
1c280 63 74 20 4f 50 5f 43 6f 6d 70 61 72 65 5f 73 74  ct OP_Compare_st
1c281 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c282 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20 20 69 6e   int n;.      in
1c283 74 20 69 3b 0a 20 20 20 20 20 20 69 6e 74 20 70  t i;.      int p
1c284 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 70 32 3b  1;.      int p2;
1c285 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 4b 65 79  .      const Key
1c286 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a  Info *pKeyInfo;.
1c287 20 20 20 20 20 20 69 6e 74 20 69 64 78 3b 0a 20        int idx;. 
1c288 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43       CollSeq *pC
1c289 6f 6c 6c 3b 20 20 20 20 2f 2a 20 43 6f 6c 6c 61  oll;    /* Colla
1c28a 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 74 6f  ting sequence to
1c28b 20 75 73 65 20 6f 6e 20 74 68 69 73 20 74 65 72   use on this ter
1c28c 6d 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 62  m */.      int b
1c28d 52 65 76 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Rev;          /*
1c28e 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 45 4e   True for DESCEN
1c28f 44 49 4e 47 20 73 6f 72 74 20 6f 72 64 65 72 20  DING sort order 
1c290 2a 2f 0a 20 20 20 20 7d 20 61 6a 3b 0a 20 20 20  */.    } aj;.   
1c291 20 73 74 72 75 63 74 20 4f 50 5f 4f 72 5f 73 74   struct OP_Or_st
1c292 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c293 20 69 6e 74 20 76 31 3b 20 20 20 20 2f 2a 20 4c   int v1;    /* L
1c294 65 66 74 20 6f 70 65 72 61 6e 64 3a 20 20 30 3d  eft operand:  0=
1c295 3d 46 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c  =FALSE, 1==TRUE,
1c296 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e   2==UNKNOWN or N
1c297 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ULL */.      int
1c298 20 76 32 3b 20 20 20 20 2f 2a 20 52 69 67 68 74   v2;    /* Right
1c299 20 6f 70 65 72 61 6e 64 3a 20 30 3d 3d 46 41 4c   operand: 0==FAL
1c29a 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 3d 3d  SE, 1==TRUE, 2==
1c29b 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c 4c 20  UNKNOWN or NULL 
1c29c 2a 2f 0a 20 20 20 20 7d 20 61 6b 3b 0a 20 20 20  */.    } ak;.   
1c29d 20 73 74 72 75 63 74 20 4f 50 5f 49 66 4e 6f 74   struct OP_IfNot
1c29e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c29f 20 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 7d      int c;.    }
1c2a0 20 61 6c 3b 0a 20 20 20 20 73 74 72 75 63 74 20   al;.    struct 
1c2a1 4f 50 5f 43 6f 6c 75 6d 6e 5f 73 74 61 63 6b 5f  OP_Column_stack_
1c2a2 76 61 72 73 20 7b 0a 20 20 20 20 20 20 75 33 32  vars {.      u32
1c2a3 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b 20 20 20   payloadSize;   
1c2a4 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
1c2a5 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64  es in the record
1c2a6 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20 70 61   */.      i64 pa
1c2a7 79 6c 6f 61 64 53 69 7a 65 36 34 3b 20 2f 2a 20  yloadSize64; /* 
1c2a8 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
1c2a9 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f  in the record */
1c2aa 0a 20 20 20 20 20 20 69 6e 74 20 70 31 3b 20 20  .      int p1;  
1c2ab 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 31 20            /* P1 
1c2ac 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 70 63  value of the opc
1c2ad 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ode */.      int
1c2ae 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20   p2;            
1c2af 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  /* column number
1c2b0 20 74 6f 20 72 65 74 72 69 65 76 65 20 2a 2f 0a   to retrieve */.
1c2b1 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72        VdbeCursor
1c2b2 20 2a 70 43 3b 20 20 20 20 2f 2a 20 54 68 65 20   *pC;    /* The 
1c2b3 56 44 42 45 20 63 75 72 73 6f 72 20 2a 2f 0a 20  VDBE cursor */. 
1c2b4 20 20 20 20 20 63 68 61 72 20 2a 7a 52 65 63 3b       char *zRec;
1c2b5 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
1c2b6 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 72  er to complete r
1c2b7 65 63 6f 72 64 2d 64 61 74 61 20 2a 2f 0a 20 20  ecord-data */.  
1c2b8 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43      BtCursor *pC
1c2b9 72 73 72 3b 20 20 20 2f 2a 20 54 68 65 20 42 54  rsr;   /* The BT
1c2ba 72 65 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  ree cursor */.  
1c2bb 20 20 20 20 75 33 32 20 2a 61 54 79 70 65 3b 20      u32 *aType; 
1c2bc 20 20 20 20 20 20 20 2f 2a 20 61 54 79 70 65 5b         /* aType[
1c2bd 69 5d 20 68 6f 6c 64 73 20 74 68 65 20 6e 75 6d  i] holds the num
1c2be 65 72 69 63 20 74 79 70 65 20 6f 66 20 74 68 65  eric type of the
1c2bf 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a   i-th column */.
1c2c0 20 20 20 20 20 20 75 33 32 20 2a 61 4f 66 66 73        u32 *aOffs
1c2c1 65 74 3b 20 20 20 20 20 20 2f 2a 20 61 4f 66 66  et;      /* aOff
1c2c2 73 65 74 5b 69 5d 20 69 73 20 6f 66 66 73 65 74  set[i] is offset
1c2c3 20 74 6f 20 73 74 61 72 74 20 6f 66 20 64 61 74   to start of dat
1c2c4 61 20 66 6f 72 20 69 2d 74 68 20 63 6f 6c 75 6d  a for i-th colum
1c2c5 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e  n */.      int n
1c2c6 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 2f 2a  Field;        /*
1c2c7 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64   number of field
1c2c8 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20  s in the record 
1c2c9 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6c 65 6e  */.      int len
1c2ca 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
1c2cb 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  he length of the
1c2cc 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 74 61   serialized data
1c2cd 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20   for the column 
1c2ce 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20  */.      int i; 
1c2cf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1c2d0 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
1c2d1 20 20 20 20 20 63 68 61 72 20 2a 7a 44 61 74 61       char *zData
1c2d2 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 72 74 20  ;       /* Part 
1c2d3 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 62 65  of the record be
1c2d4 69 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f 0a 20  ing decoded */. 
1c2d5 20 20 20 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b       Mem *pDest;
1c2d6 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65          /* Where
1c2d7 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 65 78   to write the ex
1c2d8 74 72 61 63 74 65 64 20 76 61 6c 75 65 20 2a 2f  tracted value */
1c2d9 0a 20 20 20 20 20 20 4d 65 6d 20 73 4d 65 6d 3b  .      Mem sMem;
1c2da 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
1c2db 20 73 74 6f 72 69 6e 67 20 74 68 65 20 72 65 63   storing the rec
1c2dc 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65  ord being decode
1c2dd 64 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 2a 7a  d */.      u8 *z
1c2de 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Idx;          /*
1c2df 20 49 6e 64 65 78 20 69 6e 74 6f 20 68 65 61 64   Index into head
1c2e0 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 2a  er */.      u8 *
1c2e1 7a 45 6e 64 48 64 72 3b 20 20 20 20 20 20 20 2f  zEndHdr;       /
1c2e2 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 69 72  * Pointer to fir
1c2e3 73 74 20 62 79 74 65 20 61 66 74 65 72 20 74 68  st byte after th
1c2e4 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20  e header */.    
1c2e5 20 20 75 33 32 20 6f 66 66 73 65 74 3b 20 20 20    u32 offset;   
1c2e6 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69       /* Offset i
1c2e7 6e 74 6f 20 74 68 65 20 64 61 74 61 20 2a 2f 0a  nto the data */.
1c2e8 20 20 20 20 20 20 75 36 34 20 6f 66 66 73 65 74        u64 offset
1c2e9 36 34 3b 20 20 20 20 20 20 2f 2a 20 36 34 2d 62  64;      /* 64-b
1c2ea 69 74 20 6f 66 66 73 65 74 2e 20 20 36 34 20 62  it offset.  64 b
1c2eb 69 74 73 20 6e 65 65 64 65 64 20 74 6f 20 63 61  its needed to ca
1c2ec 74 63 68 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a  tch overflow */.
1c2ed 20 20 20 20 20 20 69 6e 74 20 73 7a 48 64 72 3b        int szHdr;
1c2ee 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
1c2ef 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 20 73   of the header s
1c2f0 69 7a 65 20 66 69 65 6c 64 20 61 74 20 73 74 61  ize field at sta
1c2f1 72 74 20 6f 66 20 72 65 63 6f 72 64 20 2a 2f 0a  rt of record */.
1c2f2 20 20 20 20 20 20 69 6e 74 20 61 76 61 69 6c 3b        int avail;
1c2f3 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1c2f4 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 61  er of bytes of a
1c2f5 76 61 69 6c 61 62 6c 65 20 64 61 74 61 20 2a 2f  vailable data */
1c2f6 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 67  .      Mem *pReg
1c2f7 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 73 65  ;         /* Pse
1c2f8 75 64 6f 54 61 62 6c 65 20 69 6e 70 75 74 20 72  udoTable input r
1c2f9 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 7d  egister */.    }
1c2fa 20 61 6d 3b 0a 20 20 20 20 73 74 72 75 63 74 20   am;.    struct 
1c2fb 4f 50 5f 41 66 66 69 6e 69 74 79 5f 73 74 61 63  OP_Affinity_stac
1c2fc 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 63  k_vars {.      c
1c2fd 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20  har *zAffinity; 
1c2fe 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74    /* The affinit
1c2ff 79 20 74 6f 20 62 65 20 61 70 70 6c 69 65 64 20  y to be applied 
1c300 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 44  */.      Mem *pD
1c301 61 74 61 30 3b 20 20 20 20 20 20 20 2f 2a 20 46  ata0;       /* F
1c302 69 72 73 74 20 72 65 67 69 73 74 65 72 20 74 6f  irst register to
1c303 20 77 68 69 63 68 20 74 6f 20 61 70 70 6c 79 20   which to apply 
1c304 61 66 66 69 6e 69 74 79 20 2a 2f 0a 20 20 20 20  affinity */.    
1c305 20 20 4d 65 6d 20 2a 70 4c 61 73 74 3b 20 20 20    Mem *pLast;   
1c306 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 72 65 67       /* Last reg
1c307 69 73 74 65 72 20 74 6f 20 77 68 69 63 68 20 74  ister to which t
1c308 6f 20 61 70 70 6c 79 20 61 66 66 69 6e 69 74 79  o apply affinity
1c309 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70   */.      Mem *p
1c30a 52 65 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Rec;         /* 
1c30b 43 75 72 72 65 6e 74 20 72 65 67 69 73 74 65 72  Current register
1c30c 20 2a 2f 0a 20 20 20 20 7d 20 61 6e 3b 0a 20 20   */.    } an;.  
1c30d 20 20 73 74 72 75 63 74 20 4f 50 5f 4d 61 6b 65    struct OP_Make
1c30e 52 65 63 6f 72 64 5f 73 74 61 63 6b 5f 76 61 72  Record_stack_var
1c30f 73 20 7b 0a 20 20 20 20 20 20 75 38 20 2a 7a 4e  s {.      u8 *zN
1c310 65 77 52 65 63 6f 72 64 3b 20 20 20 20 20 20 20  ewRecord;       
1c311 20 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f 20   /* A buffer to 
1c312 68 6f 6c 64 20 74 68 65 20 64 61 74 61 20 66 6f  hold the data fo
1c313 72 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64  r the new record
1c314 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70   */.      Mem *p
1c315 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  Rec;            
1c316 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 65 63 6f   /* The new reco
1c317 72 64 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 20  rd */.      u64 
1c318 6e 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20  nData;          
1c319 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1c31a 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 73 70  bytes of data sp
1c31b 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ace */.      int
1c31c 20 6e 48 64 72 3b 20 20 20 20 20 20 20 20 20 20   nHdr;          
1c31d 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1c31e 20 62 79 74 65 73 20 6f 66 20 68 65 61 64 65 72   bytes of header
1c31f 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20   space */.      
1c320 69 36 34 20 6e 42 79 74 65 3b 20 20 20 20 20 20  i64 nByte;      
1c321 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 73         /* Data s
1c322 70 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f  pace required fo
1c323 72 20 74 68 69 73 20 72 65 63 6f 72 64 20 2a 2f  r this record */
1c324 0a 20 20 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f  .      int nZero
1c325 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
1c326 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 20   Number of zero 
1c327 62 79 74 65 73 20 61 74 20 74 68 65 20 65 6e 64  bytes at the end
1c328 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 2a   of the record *
1c329 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 56 61 72  /.      int nVar
1c32a 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  int;           /
1c32b 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
1c32c 73 20 69 6e 20 61 20 76 61 72 69 6e 74 20 2a 2f  s in a varint */
1c32d 0a 20 20 20 20 20 20 75 33 32 20 73 65 72 69 61  .      u32 seria
1c32e 6c 5f 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a  l_type;       /*
1c32f 20 54 79 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20   Type field */. 
1c330 20 20 20 20 20 4d 65 6d 20 2a 70 44 61 74 61 30       Mem *pData0
1c331 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  ;           /* F
1c332 69 72 73 74 20 66 69 65 6c 64 20 74 6f 20 62 65  irst field to be
1c333 20 63 6f 6d 62 69 6e 65 64 20 69 6e 74 6f 20 74   combined into t
1c334 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20  he record */.   
1c335 20 20 20 4d 65 6d 20 2a 70 4c 61 73 74 3b 20 20     Mem *pLast;  
1c336 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73            /* Las
1c337 74 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 72  t field of the r
1c338 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69  ecord */.      i
1c339 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20  nt nField;      
1c33a 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1c33b 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65  of fields in the
1c33c 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20   record */.     
1c33d 20 63 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79   char *zAffinity
1c33e 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61  ;       /* The a
1c33f 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66  ffinity string f
1c340 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f  or the record */
1c341 0a 20 20 20 20 20 20 69 6e 74 20 66 69 6c 65 5f  .      int file_
1c342 66 6f 72 6d 61 74 3b 20 20 20 20 20 20 20 2f 2a  format;       /*
1c343 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 74 6f 20   File format to 
1c344 75 73 65 20 66 6f 72 20 65 6e 63 6f 64 69 6e 67  use for encoding
1c345 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 3b   */.      int i;
1c346 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c347 20 2f 2a 20 53 70 61 63 65 20 75 73 65 64 20 69   /* Space used i
1c348 6e 20 7a 4e 65 77 52 65 63 6f 72 64 5b 5d 20 2a  n zNewRecord[] *
1c349 2f 0a 20 20 20 20 20 20 69 6e 74 20 6c 65 6e 3b  /.      int len;
1c34a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1c34b 2a 20 4c 65 6e 67 74 68 20 6f 66 20 61 20 66 69  * Length of a fi
1c34c 65 6c 64 20 2a 2f 0a 20 20 20 20 7d 20 61 6f 3b  eld */.    } ao;
1c34d 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 43  .    struct OP_C
1c34e 6f 75 6e 74 5f 73 74 61 63 6b 5f 76 61 72 73 20  ount_stack_vars 
1c34f 7b 0a 20 20 20 20 20 20 69 36 34 20 6e 45 6e 74  {.      i64 nEnt
1c350 72 79 3b 0a 20 20 20 20 20 20 42 74 43 75 72 73  ry;.      BtCurs
1c351 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 7d  or *pCrsr;.    }
1c352 20 61 70 3b 0a 20 20 20 20 73 74 72 75 63 74 20   ap;.    struct 
1c353 4f 50 5f 53 61 76 65 70 6f 69 6e 74 5f 73 74 61  OP_Savepoint_sta
1c354 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c355 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 20  int p1;         
1c356 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c357 2f 2a 20 56 61 6c 75 65 20 6f 66 20 50 31 20 6f  /* Value of P1 o
1c358 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20  perand */.      
1c359 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
1c35a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c35b 2f 2a 20 4e 61 6d 65 20 6f 66 20 73 61 76 65 70  /* Name of savep
1c35c 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e  oint */.      in
1c35d 74 20 6e 4e 61 6d 65 3b 0a 20 20 20 20 20 20 53  t nName;.      S
1c35e 61 76 65 70 6f 69 6e 74 20 2a 70 4e 65 77 3b 0a  avepoint *pNew;.
1c35f 20 20 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20        Savepoint 
1c360 2a 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20  *pSavepoint;.   
1c361 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 54     Savepoint *pT
1c362 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 53  mp;.      int iS
1c363 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20  avepoint;.      
1c364 69 6e 74 20 69 69 3b 0a 20 20 20 20 7d 20 61 71  int ii;.    } aq
1c365 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c366 41 75 74 6f 43 6f 6d 6d 69 74 5f 73 74 61 63 6b  AutoCommit_stack
1c367 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e  _vars {.      in
1c368 74 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d  t desiredAutoCom
1c369 6d 69 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 69  mit;.      int i
1c36a 52 6f 6c 6c 62 61 63 6b 3b 0a 20 20 20 20 20 20  Rollback;.      
1c36b 69 6e 74 20 74 75 72 6e 4f 6e 41 43 3b 0a 20 20  int turnOnAC;.  
1c36c 20 20 7d 20 61 72 3b 0a 20 20 20 20 73 74 72 75    } ar;.    stru
1c36d 63 74 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f  ct OP_Transactio
1c36e 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  n_stack_vars {. 
1c36f 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 3b       Btree *pBt;
1c370 0a 20 20 20 20 7d 20 61 73 3b 0a 20 20 20 20 73  .    } as;.    s
1c371 74 72 75 63 74 20 4f 50 5f 52 65 61 64 43 6f 6f  truct OP_ReadCoo
1c372 6b 69 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  kie_stack_vars {
1c373 0a 20 20 20 20 20 20 69 6e 74 20 69 4d 65 74 61  .      int iMeta
1c374 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b  ;.      int iDb;
1c375 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6f 6b  .      int iCook
1c376 69 65 3b 0a 20 20 20 20 7d 20 61 74 3b 0a 20 20  ie;.    } at;.  
1c377 20 20 73 74 72 75 63 74 20 4f 50 5f 53 65 74 43    struct OP_SetC
1c378 6f 6f 6b 69 65 5f 73 74 61 63 6b 5f 76 61 72 73  ookie_stack_vars
1c379 20 7b 0a 20 20 20 20 20 20 44 62 20 2a 70 44 62   {.      Db *pDb
1c37a 3b 0a 20 20 20 20 7d 20 61 75 3b 0a 20 20 20 20  ;.    } au;.    
1c37b 73 74 72 75 63 74 20 4f 50 5f 56 65 72 69 66 79  struct OP_Verify
1c37c 43 6f 6f 6b 69 65 5f 73 74 61 63 6b 5f 76 61 72  Cookie_stack_var
1c37d 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 4d  s {.      int iM
1c37e 65 74 61 3b 0a 20 20 20 20 20 20 42 74 72 65 65  eta;.      Btree
1c37f 20 2a 70 42 74 3b 0a 20 20 20 20 7d 20 61 76 3b   *pBt;.    } av;
1c380 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4f  .    struct OP_O
1c381 70 65 6e 57 72 69 74 65 5f 73 74 61 63 6b 5f 76  penWrite_stack_v
1c382 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  ars {.      int 
1c383 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20 20 4b 65  nField;.      Ke
1c384 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b  yInfo *pKeyInfo;
1c385 0a 20 20 20 20 20 20 69 6e 74 20 70 32 3b 0a 20  .      int p2;. 
1c386 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20       int iDb;.  
1c387 20 20 20 20 69 6e 74 20 77 72 46 6c 61 67 3b 0a      int wrFlag;.
1c388 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 58 3b        Btree *pX;
1c389 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f  .      VdbeCurso
1c38a 72 20 2a 70 43 75 72 3b 0a 20 20 20 20 20 20 44  r *pCur;.      D
1c38b 62 20 2a 70 44 62 3b 0a 20 20 20 20 7d 20 61 77  b *pDb;.    } aw
1c38c 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c38d 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 5f 73 74  OpenEphemeral_st
1c38e 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c38f 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78   VdbeCursor *pCx
1c390 3b 0a 20 20 20 20 7d 20 61 78 3b 0a 20 20 20 20  ;.    } ax;.    
1c391 73 74 72 75 63 74 20 4f 50 5f 4f 70 65 6e 50 73  struct OP_OpenPs
1c392 65 75 64 6f 5f 73 74 61 63 6b 5f 76 61 72 73 20  eudo_stack_vars 
1c393 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73  {.      VdbeCurs
1c394 6f 72 20 2a 70 43 78 3b 0a 20 20 20 20 7d 20 61  or *pCx;.    } a
1c395 79 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  y;.    struct OP
1c396 5f 53 65 65 6b 47 74 5f 73 74 61 63 6b 5f 76 61  _SeekGt_stack_va
1c397 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72  rs {.      int r
1c398 65 73 3b 0a 20 20 20 20 20 20 69 6e 74 20 6f 63  es;.      int oc
1c399 3b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73  ;.      VdbeCurs
1c39a 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 55 6e  or *pC;.      Un
1c39b 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a  packedRecord r;.
1c39c 20 20 20 20 20 20 69 6e 74 20 6e 46 69 65 6c 64        int nField
1c39d 3b 0a 20 20 20 20 20 20 69 36 34 20 69 4b 65 79  ;.      i64 iKey
1c39e 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f  ;      /* The ro
1c39f 77 69 64 20 77 65 20 61 72 65 20 74 6f 20 73 65  wid we are to se
1c3a0 65 6b 20 74 6f 20 2a 2f 0a 20 20 20 20 7d 20 61  ek to */.    } a
1c3a1 7a 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  z;.    struct OP
1c3a2 5f 53 65 65 6b 5f 73 74 61 63 6b 5f 76 61 72 73  _Seek_stack_vars
1c3a3 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72   {.      VdbeCur
1c3a4 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 7d 20 62  sor *pC;.    } b
1c3a5 61 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  a;.    struct OP
1c3a6 5f 46 6f 75 6e 64 5f 73 74 61 63 6b 5f 76 61 72  _Found_stack_var
1c3a7 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 6c  s {.      int al
1c3a8 72 65 61 64 79 45 78 69 73 74 73 3b 0a 20 20 20  readyExists;.   
1c3a9 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70     VdbeCursor *p
1c3aa 43 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73  C;.      int res
1c3ab 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64  ;.      Unpacked
1c3ac 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b  Record *pIdxKey;
1c3ad 0a 20 20 20 20 20 20 63 68 61 72 20 61 54 65 6d  .      char aTem
1c3ae 70 52 65 63 5b 52 4f 55 4e 44 38 28 73 69 7a 65  pRec[ROUND8(size
1c3af 6f 66 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  of(UnpackedRecor
1c3b0 64 29 29 20 2b 20 73 69 7a 65 6f 66 28 4d 65 6d  d)) + sizeof(Mem
1c3b1 29 2a 33 20 2b 20 37 5d 3b 0a 20 20 20 20 7d 20  )*3 + 7];.    } 
1c3b2 62 62 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  bb;.    struct O
1c3b3 50 5f 49 73 55 6e 69 71 75 65 5f 73 74 61 63 6b  P_IsUnique_stack
1c3b4 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 75 31  _vars {.      u1
1c3b5 36 20 69 69 3b 0a 20 20 20 20 20 20 56 64 62 65  6 ii;.      Vdbe
1c3b6 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 20  Cursor *pCx;.   
1c3b7 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72     BtCursor *pCr
1c3b8 73 72 3b 0a 20 20 20 20 20 20 75 31 36 20 6e 46  sr;.      u16 nF
1c3b9 69 65 6c 64 3b 0a 20 20 20 20 20 20 4d 65 6d 20  ield;.      Mem 
1c3ba 2a 61 4d 65 6d 3b 0a 20 20 20 20 20 20 55 6e 70  *aMem;.      Unp
1c3bb 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 20 20  ackedRecord r;  
1c3bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c3bd 2f 2a 20 42 2d 54 72 65 65 20 69 6e 64 65 78 20  /* B-Tree index 
1c3be 73 65 61 72 63 68 20 6b 65 79 20 2a 2f 0a 20 20  search key */.  
1c3bf 20 20 20 20 69 36 34 20 52 3b 20 20 20 20 20 20      i64 R;      
1c3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c3c1 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20         /* Rowid 
1c3c2 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74  stored in regist
1c3c3 65 72 20 50 33 20 2a 2f 0a 20 20 20 20 7d 20 62  er P3 */.    } b
1c3c4 63 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  c;.    struct OP
1c3c5 5f 4e 6f 74 45 78 69 73 74 73 5f 73 74 61 63 6b  _NotExists_stack
1c3c6 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64  _vars {.      Vd
1c3c7 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20  beCursor *pC;.  
1c3c8 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43      BtCursor *pC
1c3c9 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72  rsr;.      int r
1c3ca 65 73 3b 0a 20 20 20 20 20 20 75 36 34 20 69 4b  es;.      u64 iK
1c3cb 65 79 3b 0a 20 20 20 20 7d 20 62 64 3b 0a 20 20  ey;.    } bd;.  
1c3cc 20 20 73 74 72 75 63 74 20 4f 50 5f 4e 65 77 52    struct OP_NewR
1c3cd 6f 77 69 64 5f 73 74 61 63 6b 5f 76 61 72 73 20  owid_stack_vars 
1c3ce 7b 0a 20 20 20 20 20 20 69 36 34 20 76 3b 20 20  {.      i64 v;  
1c3cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1c3d0 2a 20 54 68 65 20 6e 65 77 20 72 6f 77 69 64 20  * The new rowid 
1c3d1 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 43 75 72  */.      VdbeCur
1c3d2 73 6f 72 20 2a 70 43 3b 20 20 20 20 20 20 20 20  sor *pC;        
1c3d3 2f 2a 20 43 75 72 73 6f 72 20 6f 66 20 74 61 62  /* Cursor of tab
1c3d4 6c 65 20 74 6f 20 67 65 74 20 74 68 65 20 6e 65  le to get the ne
1c3d5 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 20 20 20  w rowid */.     
1c3d6 20 69 6e 74 20 72 65 73 3b 20 20 20 20 20 20 20   int res;       
1c3d7 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c          /* Resul
1c3d8 74 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 42  t of an sqlite3B
1c3d9 74 72 65 65 4c 61 73 74 28 29 20 2a 2f 0a 20 20  treeLast() */.  
1c3da 20 20 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20      int cnt;    
1c3db 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
1c3dc 75 6e 74 65 72 20 74 6f 20 6c 69 6d 69 74 20 74  unter to limit t
1c3dd 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 61  he number of sea
1c3de 72 63 68 65 73 20 2a 2f 0a 20 20 20 20 20 20 4d  rches */.      M
1c3df 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20  em *pMem;       
1c3e0 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
1c3e1 72 20 68 6f 6c 64 69 6e 67 20 6c 61 72 67 65 73  r holding larges
1c3e2 74 20 72 6f 77 69 64 20 66 6f 72 20 41 55 54 4f  t rowid for AUTO
1c3e3 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 20  INCREMENT */.   
1c3e4 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46     VdbeFrame *pF
1c3e5 72 61 6d 65 3b 20 20 20 20 20 2f 2a 20 52 6f 6f  rame;     /* Roo
1c3e6 74 20 66 72 61 6d 65 20 6f 66 20 56 44 42 45 20  t frame of VDBE 
1c3e7 2a 2f 0a 20 20 20 20 7d 20 62 65 3b 0a 20 20 20  */.    } be;.   
1c3e8 20 73 74 72 75 63 74 20 4f 50 5f 49 6e 73 65 72   struct OP_Inser
1c3e9 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  t_stack_vars {. 
1c3ea 20 20 20 20 20 4d 65 6d 20 2a 70 44 61 74 61 3b       Mem *pData;
1c3eb 20 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65         /* MEM ce
1c3ec 6c 6c 20 68 6f 6c 64 69 6e 67 20 64 61 74 61 20  ll holding data 
1c3ed 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74  for the record t
1c3ee 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f  o be inserted */
1c3ef 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4b 65 79  .      Mem *pKey
1c3f0 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20  ;        /* MEM 
1c3f1 63 65 6c 6c 20 68 6f 6c 64 69 6e 67 20 6b 65 79  cell holding key
1c3f2 20 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64    for the record
1c3f3 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20 69 4b   */.      i64 iK
1c3f4 65 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ey;         /* T
1c3f5 68 65 20 69 6e 74 65 67 65 72 20 52 4f 57 49 44  he integer ROWID
1c3f6 20 6f 72 20 6b 65 79 20 66 6f 72 20 74 68 65 20   or key for the 
1c3f7 72 65 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73  record to be ins
1c3f8 65 72 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 56  erted */.      V
1c3f9 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 20  dbeCursor *pC;  
1c3fa 20 2f 2a 20 43 75 72 73 6f 72 20 74 6f 20 74 61   /* Cursor to ta
1c3fb 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 69  ble into which i
1c3fc 6e 73 65 72 74 20 69 73 20 77 72 69 74 74 65 6e  nsert is written
1c3fd 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 5a   */.      int nZ
1c3fe 65 72 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  ero;        /* N
1c3ff 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 2d 62 79  umber of zero-by
1c400 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20 2a 2f  tes to append */
1c401 0a 20 20 20 20 20 20 69 6e 74 20 73 65 65 6b 52  .      int seekR
1c402 65 73 75 6c 74 3b 20 20 20 2f 2a 20 52 65 73 75  esult;   /* Resu
1c403 6c 74 20 6f 66 20 70 72 69 6f 72 20 73 65 65 6b  lt of prior seek
1c404 20 6f 72 20 30 20 69 66 20 6e 6f 20 55 53 45 53   or 0 if no USES
1c405 45 45 4b 52 45 53 55 4c 54 20 66 6c 61 67 20 2a  EEKRESULT flag *
1c406 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  /.      const ch
1c407 61 72 20 2a 7a 44 62 3b 20 20 2f 2a 20 64 61 74  ar *zDb;  /* dat
1c408 61 62 61 73 65 20 6e 61 6d 65 20 2d 20 75 73 65  abase name - use
1c409 64 20 62 79 20 74 68 65 20 75 70 64 61 74 65 20  d by the update 
1c40a 68 6f 6f 6b 20 2a 2f 0a 20 20 20 20 20 20 63 6f  hook */.      co
1c40b 6e 73 74 20 63 68 61 72 20 2a 7a 54 62 6c 3b 20  nst char *zTbl; 
1c40c 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2d 20  /* Table name - 
1c40d 75 73 65 64 20 62 79 20 74 68 65 20 6f 70 64 61  used by the opda
1c40e 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 20 20 20 20  te hook */.     
1c40f 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20   int op;        
1c410 20 20 20 2f 2a 20 4f 70 63 6f 64 65 20 66 6f 72     /* Opcode for
1c411 20 75 70 64 61 74 65 20 68 6f 6f 6b 3a 20 53 51   update hook: SQ
1c412 4c 49 54 45 5f 55 50 44 41 54 45 20 6f 72 20 53  LITE_UPDATE or S
1c413 51 4c 49 54 45 5f 49 4e 53 45 52 54 20 2a 2f 0a  QLITE_INSERT */.
1c414 20 20 20 20 7d 20 62 66 3b 0a 20 20 20 20 73 74      } bf;.    st
1c415 72 75 63 74 20 4f 50 5f 44 65 6c 65 74 65 5f 73  ruct OP_Delete_s
1c416 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c417 20 20 69 36 34 20 69 4b 65 79 3b 0a 20 20 20 20    i64 iKey;.    
1c418 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1c419 3b 0a 20 20 20 20 7d 20 62 67 3b 0a 20 20 20 20  ;.    } bg;.    
1c41a 73 74 72 75 63 74 20 4f 50 5f 52 6f 77 44 61 74  struct OP_RowDat
1c41b 61 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  a_stack_vars {. 
1c41c 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20       VdbeCursor 
1c41d 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43 75 72  *pC;.      BtCur
1c41e 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20 20  sor *pCrsr;.    
1c41f 20 20 75 33 32 20 6e 3b 0a 20 20 20 20 20 20 69    u32 n;.      i
1c420 36 34 20 6e 36 34 3b 0a 20 20 20 20 7d 20 62 68  64 n64;.    } bh
1c421 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c422 52 6f 77 69 64 5f 73 74 61 63 6b 5f 76 61 72 73  Rowid_stack_vars
1c423 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72   {.      VdbeCur
1c424 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 69  sor *pC;.      i
1c425 36 34 20 76 3b 0a 20 20 20 20 20 20 73 71 6c 69  64 v;.      sqli
1c426 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b  te3_vtab *pVtab;
1c427 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 73 71 6c  .      const sql
1c428 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
1c429 64 75 6c 65 3b 0a 20 20 20 20 7d 20 62 69 3b 0a  dule;.    } bi;.
1c42a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4e 75      struct OP_Nu
1c42b 6c 6c 52 6f 77 5f 73 74 61 63 6b 5f 76 61 72 73  llRow_stack_vars
1c42c 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72   {.      VdbeCur
1c42d 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 7d 20 62  sor *pC;.    } b
1c42e 6a 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  j;.    struct OP
1c42f 5f 4c 61 73 74 5f 73 74 61 63 6b 5f 76 61 72 73  _Last_stack_vars
1c430 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72   {.      VdbeCur
1c431 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42  sor *pC;.      B
1c432 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
1c433 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20        int res;. 
1c434 20 20 20 7d 20 62 6b 3b 0a 20 20 20 20 73 74 72     } bk;.    str
1c435 75 63 74 20 4f 50 5f 52 65 77 69 6e 64 5f 73 74  uct OP_Rewind_st
1c436 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c437 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1c438 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20  .      BtCursor 
1c439 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69 6e  *pCrsr;.      in
1c43a 74 20 72 65 73 3b 0a 20 20 20 20 7d 20 62 6c 3b  t res;.    } bl;
1c43b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4e  .    struct OP_N
1c43c 65 78 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  ext_stack_vars {
1c43d 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f  .      VdbeCurso
1c43e 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43  r *pC;.      BtC
1c43f 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20  ursor *pCrsr;.  
1c440 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20      int res;.   
1c441 20 7d 20 62 6d 3b 0a 20 20 20 20 73 74 72 75 63   } bm;.    struc
1c442 74 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 5f 73  t OP_IdxInsert_s
1c443 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c444 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1c445 3b 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72  ;.      BtCursor
1c446 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69   *pCrsr;.      i
1c447 6e 74 20 6e 4b 65 79 3b 0a 20 20 20 20 20 20 63  nt nKey;.      c
1c448 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4b 65 79 3b  onst char *zKey;
1c449 0a 20 20 20 20 7d 20 62 6e 3b 0a 20 20 20 20 73  .    } bn;.    s
1c44a 74 72 75 63 74 20 4f 50 5f 49 64 78 44 65 6c 65  truct OP_IdxDele
1c44b 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  te_stack_vars {.
1c44c 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72        VdbeCursor
1c44d 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43 75   *pC;.      BtCu
1c44e 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20  rsor *pCrsr;.   
1c44f 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20     int res;.    
1c450 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
1c451 20 72 3b 0a 20 20 20 20 7d 20 62 6f 3b 0a 20 20   r;.    } bo;.  
1c452 20 20 73 74 72 75 63 74 20 4f 50 5f 49 64 78 52    struct OP_IdxR
1c453 6f 77 69 64 5f 73 74 61 63 6b 5f 76 61 72 73 20  owid_stack_vars 
1c454 7b 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72  {.      BtCursor
1c455 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 56   *pCrsr;.      V
1c456 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20  dbeCursor *pC;. 
1c457 20 20 20 20 20 69 36 34 20 72 6f 77 69 64 3b 0a       i64 rowid;.
1c458 20 20 20 20 7d 20 62 70 3b 0a 20 20 20 20 73 74      } bp;.    st
1c459 72 75 63 74 20 4f 50 5f 49 64 78 47 45 5f 73 74  ruct OP_IdxGE_st
1c45a 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c45b 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1c45c 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a  .      int res;.
1c45d 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65        UnpackedRe
1c45e 63 6f 72 64 20 72 3b 0a 20 20 20 20 7d 20 62 71  cord r;.    } bq
1c45f 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c460 44 65 73 74 72 6f 79 5f 73 74 61 63 6b 5f 76 61  Destroy_stack_va
1c461 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  rs {.      int i
1c462 4d 6f 76 65 64 3b 0a 20 20 20 20 20 20 69 6e 74  Moved;.      int
1c463 20 69 43 6e 74 3b 0a 20 20 20 20 20 20 56 64 62   iCnt;.      Vdb
1c464 65 20 2a 70 56 64 62 65 3b 0a 20 20 20 20 20 20  e *pVdbe;.      
1c465 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 7d 20 62  int iDb;.    } b
1c466 72 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  r;.    struct OP
1c467 5f 43 6c 65 61 72 5f 73 74 61 63 6b 5f 76 61 72  _Clear_stack_var
1c468 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 43  s {.      int nC
1c469 68 61 6e 67 65 3b 0a 20 20 20 20 7d 20 62 73 3b  hange;.    } bs;
1c46a 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 43  .    struct OP_C
1c46b 72 65 61 74 65 54 61 62 6c 65 5f 73 74 61 63 6b  reateTable_stack
1c46c 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e  _vars {.      in
1c46d 74 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20 69 6e  t pgno;.      in
1c46e 74 20 66 6c 61 67 73 3b 0a 20 20 20 20 20 20 44  t flags;.      D
1c46f 62 20 2a 70 44 62 3b 0a 20 20 20 20 7d 20 62 74  b *pDb;.    } bt
1c470 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c471 50 61 72 73 65 53 63 68 65 6d 61 5f 73 74 61 63  ParseSchema_stac
1c472 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69  k_vars {.      i
1c473 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 20 63 6f  nt iDb;.      co
1c474 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65  nst char *zMaste
1c475 72 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  r;.      char *z
1c476 53 71 6c 3b 0a 20 20 20 20 20 20 49 6e 69 74 44  Sql;.      InitD
1c477 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0a 20 20  ata initData;.  
1c478 20 20 7d 20 62 75 3b 0a 20 20 20 20 73 74 72 75    } bu;.    stru
1c479 63 74 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43  ct OP_IntegrityC
1c47a 6b 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  k_stack_vars {. 
1c47b 20 20 20 20 20 69 6e 74 20 6e 52 6f 6f 74 3b 20       int nRoot; 
1c47c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1c47d 66 20 74 61 62 6c 65 73 20 74 6f 20 63 68 65 63  f tables to chec
1c47e 6b 2e 20 20 28 4e 75 6d 62 65 72 20 6f 66 20 72  k.  (Number of r
1c47f 6f 6f 74 20 70 61 67 65 73 2e 29 20 2a 2f 0a 20  oot pages.) */. 
1c480 20 20 20 20 20 69 6e 74 20 2a 61 52 6f 6f 74 3b       int *aRoot;
1c481 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66       /* Array of
1c482 20 72 6f 6f 74 70 61 67 65 20 6e 75 6d 62 65 72   rootpage number
1c483 73 20 66 6f 72 20 74 61 62 6c 65 73 20 74 6f 20  s for tables to 
1c484 62 65 20 63 68 65 63 6b 65 64 20 2a 2f 0a 20 20  be checked */.  
1c485 20 20 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20      int j;      
1c486 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
1c487 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ter */.      int
1c488 20 6e 45 72 72 3b 20 20 20 20 20 20 20 2f 2a 20   nErr;       /* 
1c489 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  Number of errors
1c48a 20 72 65 70 6f 72 74 65 64 20 2a 2f 0a 20 20 20   reported */.   
1c48b 20 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20     char *z;     
1c48c 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68     /* Text of th
1c48d 65 20 65 72 72 6f 72 20 72 65 70 6f 72 74 20 2a  e error report *
1c48e 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 6e 45  /.      Mem *pnE
1c48f 72 72 3b 20 20 20 20 20 2f 2a 20 52 65 67 69 73  rr;     /* Regis
1c490 74 65 72 20 6b 65 65 70 69 6e 67 20 74 72 61 63  ter keeping trac
1c491 6b 20 6f 66 20 65 72 72 6f 72 73 20 72 65 6d 61  k of errors rema
1c492 69 6e 69 6e 67 20 2a 2f 0a 20 20 20 20 7d 20 62  ining */.    } b
1c493 76 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  v;.    struct OP
1c494 5f 52 6f 77 53 65 74 41 64 64 5f 73 74 61 63 6b  _RowSetAdd_stack
1c495 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65  _vars {.      Me
1c496 6d 20 2a 70 49 64 78 3b 0a 20 20 20 20 20 20 4d  m *pIdx;.      M
1c497 65 6d 20 2a 70 56 61 6c 3b 0a 20 20 20 20 7d 20  em *pVal;.    } 
1c498 62 77 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  bw;.    struct O
1c499 50 5f 52 6f 77 53 65 74 52 65 61 64 5f 73 74 61  P_RowSetRead_sta
1c49a 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c49b 4d 65 6d 20 2a 70 49 64 78 3b 0a 20 20 20 20 20  Mem *pIdx;.     
1c49c 20 69 36 34 20 76 61 6c 3b 0a 20 20 20 20 7d 20   i64 val;.    } 
1c49d 62 78 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  bx;.    struct O
1c49e 50 5f 52 6f 77 53 65 74 54 65 73 74 5f 73 74 61  P_RowSetTest_sta
1c49f 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c4a0 69 6e 74 20 69 53 65 74 3b 0a 20 20 20 20 20 20  int iSet;.      
1c4a1 69 6e 74 20 65 78 69 73 74 73 3b 0a 20 20 20 20  int exists;.    
1c4a2 7d 20 62 79 3b 0a 20 20 20 20 73 74 72 75 63 74  } by;.    struct
1c4a3 20 4f 50 5f 50 72 6f 67 72 61 6d 5f 73 74 61 63   OP_Program_stac
1c4a4 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69  k_vars {.      i
1c4a5 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20  nt nMem;        
1c4a6 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1c4a7 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 67 69 73   of memory regis
1c4a8 74 65 72 73 20 66 6f 72 20 73 75 62 2d 70 72 6f  ters for sub-pro
1c4a9 67 72 61 6d 20 2a 2f 0a 20 20 20 20 20 20 69 6e  gram */.      in
1c4aa 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20  t nByte;        
1c4ab 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f        /* Bytes o
1c4ac 66 20 72 75 6e 74 69 6d 65 20 73 70 61 63 65 20  f runtime space 
1c4ad 72 65 71 75 69 72 65 64 20 66 6f 72 20 73 75 62  required for sub
1c4ae 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 20 20  -program */.    
1c4af 20 20 4d 65 6d 20 2a 70 52 74 3b 20 20 20 20 20    Mem *pRt;     
1c4b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
1c4b1 69 73 74 65 72 20 74 6f 20 61 6c 6c 6f 63 61 74  ister to allocat
1c4b2 65 20 72 75 6e 74 69 6d 65 20 73 70 61 63 65 20  e runtime space 
1c4b3 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d  */.      Mem *pM
1c4b4 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  em;             
1c4b5 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65 72   /* Used to iter
1c4b6 61 74 65 20 74 68 72 6f 75 67 68 20 6d 65 6d 6f  ate through memo
1c4b7 72 79 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 20 20  ry cells */.    
1c4b8 20 20 4d 65 6d 20 2a 70 45 6e 64 3b 20 20 20 20    Mem *pEnd;    
1c4b9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73            /* Las
1c4ba 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e  t memory cell in
1c4bb 20 6e 65 77 20 61 72 72 61 79 20 2a 2f 0a 20 20   new array */.  
1c4bc 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70      VdbeFrame *p
1c4bd 46 72 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e  Frame;      /* N
1c4be 65 77 20 76 64 62 65 20 66 72 61 6d 65 20 74 6f  ew vdbe frame to
1c4bf 20 65 78 65 63 75 74 65 20 69 6e 20 2a 2f 0a 20   execute in */. 
1c4c0 20 20 20 20 20 53 75 62 50 72 6f 67 72 61 6d 20       SubProgram 
1c4c1 2a 70 50 72 6f 67 72 61 6d 3b 20 20 20 2f 2a 20  *pProgram;   /* 
1c4c2 53 75 62 2d 70 72 6f 67 72 61 6d 20 74 6f 20 65  Sub-program to e
1c4c3 78 65 63 75 74 65 20 2a 2f 0a 20 20 20 20 20 20  xecute */.      
1c4c4 76 6f 69 64 20 2a 74 3b 20 20 20 20 20 20 20 20  void *t;        
1c4c5 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e          /* Token
1c4c6 20 69 64 65 6e 74 69 66 79 69 6e 67 20 74 72 69   identifying tri
1c4c7 67 67 65 72 20 2a 2f 0a 20 20 20 20 7d 20 62 7a  gger */.    } bz
1c4c8 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c4c9 50 61 72 61 6d 5f 73 74 61 63 6b 5f 76 61 72 73  Param_stack_vars
1c4ca 20 7b 0a 20 20 20 20 20 20 56 64 62 65 46 72 61   {.      VdbeFra
1c4cb 6d 65 20 2a 70 46 72 61 6d 65 3b 0a 20 20 20 20  me *pFrame;.    
1c4cc 20 20 4d 65 6d 20 2a 70 49 6e 3b 0a 20 20 20 20    Mem *pIn;.    
1c4cd 7d 20 63 61 3b 0a 20 20 20 20 73 74 72 75 63 74  } ca;.    struct
1c4ce 20 4f 50 5f 4d 65 6d 4d 61 78 5f 73 74 61 63 6b   OP_MemMax_stack
1c4cf 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65  _vars {.      Me
1c4d0 6d 20 2a 70 49 6e 31 3b 0a 20 20 20 20 20 20 56  m *pIn1;.      V
1c4d1 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65  dbeFrame *pFrame
1c4d2 3b 0a 20 20 20 20 7d 20 63 62 3b 0a 20 20 20 20  ;.    } cb;.    
1c4d3 73 74 72 75 63 74 20 4f 50 5f 41 67 67 53 74 65  struct OP_AggSte
1c4d4 70 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  p_stack_vars {. 
1c4d5 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20       int n;.    
1c4d6 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 4d    int i;.      M
1c4d7 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20 20 20 20  em *pMem;.      
1c4d8 4d 65 6d 20 2a 70 52 65 63 3b 0a 20 20 20 20 20  Mem *pRec;.     
1c4d9 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1c4da 20 63 74 78 3b 0a 20 20 20 20 20 20 73 71 6c 69   ctx;.      sqli
1c4db 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61  te3_value **apVa
1c4dc 6c 3b 0a 20 20 20 20 7d 20 63 63 3b 0a 20 20 20  l;.    } cc;.   
1c4dd 20 73 74 72 75 63 74 20 4f 50 5f 41 67 67 46 69   struct OP_AggFi
1c4de 6e 61 6c 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  nal_stack_vars {
1c4df 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d  .      Mem *pMem
1c4e0 3b 0a 20 20 20 20 7d 20 63 64 3b 0a 20 20 20 20  ;.    } cd;.    
1c4e1 73 74 72 75 63 74 20 4f 50 5f 49 6e 63 72 56 61  struct OP_IncrVa
1c4e2 63 75 75 6d 5f 73 74 61 63 6b 5f 76 61 72 73 20  cuum_stack_vars 
1c4e3 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70  {.      Btree *p
1c4e4 42 74 3b 0a 20 20 20 20 7d 20 63 65 3b 0a 20 20  Bt;.    } ce;.  
1c4e5 20 20 73 74 72 75 63 74 20 4f 50 5f 56 42 65 67    struct OP_VBeg
1c4e6 69 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  in_stack_vars {.
1c4e7 20 20 20 20 20 20 56 54 61 62 6c 65 20 2a 70 56        VTable *pV
1c4e8 54 61 62 3b 0a 20 20 20 20 7d 20 63 66 3b 0a 20  Tab;.    } cf;. 
1c4e9 20 20 20 73 74 72 75 63 74 20 4f 50 5f 56 4f 70     struct OP_VOp
1c4ea 65 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  en_stack_vars {.
1c4eb 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72        VdbeCursor
1c4ec 20 2a 70 43 75 72 3b 0a 20 20 20 20 20 20 73 71   *pCur;.      sq
1c4ed 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
1c4ee 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a  r *pVtabCursor;.
1c4ef 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74        sqlite3_vt
1c4f0 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20  ab *pVtab;.     
1c4f1 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
1c4f2 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d 20  *pModule;.    } 
1c4f3 63 67 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  cg;.    struct O
1c4f4 50 5f 56 46 69 6c 74 65 72 5f 73 74 61 63 6b 5f  P_VFilter_stack_
1c4f5 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74  vars {.      int
1c4f6 20 6e 41 72 67 3b 0a 20 20 20 20 20 20 69 6e 74   nArg;.      int
1c4f7 20 69 51 75 65 72 79 3b 0a 20 20 20 20 20 20 63   iQuery;.      c
1c4f8 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
1c4f9 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20  ule *pModule;.  
1c4fa 20 20 20 20 4d 65 6d 20 2a 70 51 75 65 72 79 3b      Mem *pQuery;
1c4fb 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 41 72 67  .      Mem *pArg
1c4fc 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  c;.      sqlite3
1c4fd 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56  _vtab_cursor *pV
1c4fe 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 20 20  tabCursor;.     
1c4ff 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
1c500 56 74 61 62 3b 0a 20 20 20 20 20 20 56 64 62 65  Vtab;.      Vdbe
1c501 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20  Cursor *pCur;.  
1c502 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20      int res;.   
1c503 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
1c504 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 20 20 20  Mem **apArg;.   
1c505 20 7d 20 63 68 3b 0a 20 20 20 20 73 74 72 75 63   } ch;.    struc
1c506 74 20 4f 50 5f 56 43 6f 6c 75 6d 6e 5f 73 74 61  t OP_VColumn_sta
1c507 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c508 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
1c509 74 61 62 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74  tab;.      const
1c50a 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
1c50b 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 20 20  *pModule;.      
1c50c 4d 65 6d 20 2a 70 44 65 73 74 3b 0a 20 20 20 20  Mem *pDest;.    
1c50d 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
1c50e 74 20 73 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20  t sContext;.    
1c50f 7d 20 63 69 3b 0a 20 20 20 20 73 74 72 75 63 74  } ci;.    struct
1c510 20 4f 50 5f 56 4e 65 78 74 5f 73 74 61 63 6b 5f   OP_VNext_stack_
1c511 76 61 72 73 20 7b 0a 20 20 20 20 20 20 73 71 6c  vars {.      sql
1c512 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
1c513 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 73 71  ;.      const sq
1c514 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
1c515 6f 64 75 6c 65 3b 0a 20 20 20 20 20 20 69 6e 74  odule;.      int
1c516 20 72 65 73 3b 0a 20 20 20 20 20 20 56 64 62 65   res;.      Vdbe
1c517 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20  Cursor *pCur;.  
1c518 20 20 7d 20 63 6a 3b 0a 20 20 20 20 73 74 72 75    } cj;.    stru
1c519 63 74 20 4f 50 5f 56 52 65 6e 61 6d 65 5f 73 74  ct OP_VRename_st
1c51a 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c51b 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
1c51c 56 74 61 62 3b 0a 20 20 20 20 20 20 4d 65 6d 20  Vtab;.      Mem 
1c51d 2a 70 4e 61 6d 65 3b 0a 20 20 20 20 7d 20 63 6b  *pName;.    } ck
1c51e 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c51f 56 55 70 64 61 74 65 5f 73 74 61 63 6b 5f 76 61  VUpdate_stack_va
1c520 72 73 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  rs {.      sqlit
1c521 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a  e3_vtab *pVtab;.
1c522 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 6f        sqlite3_mo
1c523 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20  dule *pModule;. 
1c524 20 20 20 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20       int nArg;. 
1c525 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20       int i;.    
1c526 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 72    sqlite_int64 r
1c527 6f 77 69 64 3b 0a 20 20 20 20 20 20 4d 65 6d 20  owid;.      Mem 
1c528 2a 2a 61 70 41 72 67 3b 0a 20 20 20 20 20 20 4d  **apArg;.      M
1c529 65 6d 20 2a 70 58 3b 0a 20 20 20 20 7d 20 63 6c  em *pX;.    } cl
1c52a 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c52b 50 61 67 65 63 6f 75 6e 74 5f 73 74 61 63 6b 5f  Pagecount_stack_
1c52c 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74  vars {.      int
1c52d 20 70 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e   p1;.      int n
1c52e 50 61 67 65 3b 0a 20 20 20 20 20 20 50 61 67 65  Page;.      Page
1c52f 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 20 7d  r *pPager;.    }
1c530 20 63 6d 3b 0a 20 20 20 20 73 74 72 75 63 74 20   cm;.    struct 
1c531 4f 50 5f 54 72 61 63 65 5f 73 74 61 63 6b 5f 76  OP_Trace_stack_v
1c532 61 72 73 20 7b 0a 20 20 20 20 20 20 63 68 61 72  ars {.      char
1c533 20 2a 7a 54 72 61 63 65 3b 0a 20 20 20 20 7d 20   *zTrace;.    } 
1c534 63 6e 3b 0a 20 20 7d 20 75 3b 0a 20 20 2f 2a 20  cn;.  } u;.  /* 
1c535 45 6e 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  End automaticall
1c536 79 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65  y generated code
1c537 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .  *************
1c538 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c539 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c53a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c53b 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 20 61 73 73 65  *******/..  asse
1c53c 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44  rt( p->magic==VD
1c53d 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 3b 20  BE_MAGIC_RUN ); 
1c53e 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70   /* sqlite3_step
1c53f 28 29 20 76 65 72 69 66 69 65 73 20 74 68 69 73  () verifies this
1c540 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64 62   */.  assert( db
1c541 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f  ->magic==SQLITE_
1c542 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a 20 20  MAGIC_BUSY );.  
1c543 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78  sqlite3VdbeMutex
1c544 41 72 72 61 79 45 6e 74 65 72 28 70 29 3b 0a 20  ArrayEnter(p);. 
1c545 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49   if( p->rc==SQLI
1c546 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20  TE_NOMEM ){.    
1c547 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20  /* This happens 
1c548 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 69 6e  if a malloc() in
1c549 73 69 64 65 20 61 20 63 61 6c 6c 20 74 6f 20 73  side a call to s
1c54a 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
1c54b 78 74 28 29 20 6f 72 0a 20 20 20 20 2a 2a 20 73  xt() or.    ** s
1c54c 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
1c54d 78 74 31 36 28 29 20 66 61 69 6c 65 64 2e 20 20  xt16() failed.  
1c54e 2a 2f 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d  */.    goto no_m
1c54f 65 6d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  em;.  }.  assert
1c550 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f  ( p->rc==SQLITE_
1c551 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c  OK || p->rc==SQL
1c552 49 54 45 5f 42 55 53 59 20 29 3b 0a 20 20 70 2d  ITE_BUSY );.  p-
1c553 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  >rc = SQLITE_OK;
1c554 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 65 78  .  assert( p->ex
1c555 70 6c 61 69 6e 3d 3d 30 20 29 3b 0a 20 20 70 2d  plain==0 );.  p-
1c556 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 30 3b  >pResultSet = 0;
1c557 0a 20 20 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c  .  db->busyHandl
1c558 65 72 2e 6e 42 75 73 79 20 3d 20 30 3b 0a 20 20  er.nBusy = 0;.  
1c559 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52  CHECK_FOR_INTERR
1c55a 55 50 54 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  UPT;.  sqlite3Vd
1c55b 62 65 49 4f 54 72 61 63 65 53 71 6c 28 70 29 3b  beIOTraceSql(p);
1c55c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
1c55d 45 42 55 47 0a 20 20 73 71 6c 69 74 65 33 42 65  EBUG.  sqlite3Be
1c55e 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
1c55f 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 63 3d 3d  );.  if( p->pc==
1c560 30 20 0a 20 20 20 26 26 20 28 28 70 2d 3e 64 62  0 .   && ((p->db
1c561 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
1c562 5f 56 64 62 65 4c 69 73 74 69 6e 67 29 20 7c 7c  _VdbeListing) ||
1c563 20 66 69 6c 65 45 78 69 73 74 73 28 64 62 2c 20   fileExists(db, 
1c564 22 76 64 62 65 5f 65 78 70 6c 61 69 6e 22 29 29  "vdbe_explain"))
1c565 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  .  ){.    int i;
1c566 0a 20 20 20 20 70 72 69 6e 74 66 28 22 56 44 42  .    printf("VDB
1c567 45 20 50 72 6f 67 72 61 6d 20 4c 69 73 74 69 6e  E Program Listin
1c568 67 3a 5c 6e 22 29 3b 0a 20 20 20 20 73 71 6c 69  g:\n");.    sqli
1c569 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28  te3VdbePrintSql(
1c56a 70 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  p);.    for(i=0;
1c56b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b   i<p->nOp; i++){
1c56c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1c56d 62 65 50 72 69 6e 74 4f 70 28 73 74 64 6f 75 74  bePrintOp(stdout
1c56e 2c 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d 29  , i, &p->aOp[i])
1c56f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
1c570 28 20 66 69 6c 65 45 78 69 73 74 73 28 64 62 2c  ( fileExists(db,
1c571 20 22 76 64 62 65 5f 74 72 61 63 65 22 29 20 29   "vdbe_trace") )
1c572 7b 0a 20 20 20 20 70 2d 3e 74 72 61 63 65 20 3d  {.    p->trace =
1c573 20 73 74 64 6f 75 74 3b 0a 20 20 7d 0a 20 20 73   stdout;.  }.  s
1c574 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d  qlite3EndBenignM
1c575 61 6c 6c 6f 63 28 29 3b 0a 23 65 6e 64 69 66 0a  alloc();.#endif.
1c576 20 20 66 6f 72 28 70 63 3d 70 2d 3e 70 63 3b 20    for(pc=p->pc; 
1c577 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 70  rc==SQLITE_OK; p
1c578 63 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74  c++){.    assert
1c579 28 20 70 63 3e 3d 30 20 26 26 20 70 63 3c 70 2d  ( pc>=0 && pc<p-
1c57a 3e 6e 4f 70 20 29 3b 0a 20 20 20 20 69 66 28 20  >nOp );.    if( 
1c57b 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1c57c 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
1c57d 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46  #ifdef VDBE_PROF
1c57e 49 4c 45 0a 20 20 20 20 6f 72 69 67 50 63 20 3d  ILE.    origPc =
1c57f 20 70 63 3b 0a 20 20 20 20 73 74 61 72 74 20 3d   pc;.    start =
1c580 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29   sqlite3Hwtime()
1c581 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 4f 70  ;.#endif.    pOp
1c582 20 3d 20 26 70 2d 3e 61 4f 70 5b 70 63 5d 3b 0a   = &p->aOp[pc];.
1c583 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 6c 6c  .    /* Only all
1c584 6f 77 20 74 72 61 63 69 6e 67 20 69 66 20 53 51  ow tracing if SQ
1c585 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20 64 65  LITE_DEBUG is de
1c586 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a 23 69  fined..    */.#i
1c587 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
1c588 47 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61  G.    if( p->tra
1c589 63 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ce ){.      if( 
1c58a 70 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  pc==0 ){.       
1c58b 20 70 72 69 6e 74 66 28 22 56 44 42 45 20 45 78   printf("VDBE Ex
1c58c 65 63 75 74 69 6f 6e 20 54 72 61 63 65 3a 5c 6e  ecution Trace:\n
1c58d 22 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ");.        sqli
1c58e 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28  te3VdbePrintSql(
1c58f 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  p);.      }.    
1c590 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69    sqlite3VdbePri
1c591 6e 74 4f 70 28 70 2d 3e 74 72 61 63 65 2c 20 70  ntOp(p->trace, p
1c592 63 2c 20 70 4f 70 29 3b 0a 20 20 20 20 7d 0a 20  c, pOp);.    }. 
1c593 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 3d     if( p->trace=
1c594 3d 30 20 26 26 20 70 63 3d 3d 30 20 29 7b 0a 20  =0 && pc==0 ){. 
1c595 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69       sqlite3Begi
1c596 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  nBenignMalloc();
1c597 0a 20 20 20 20 20 20 69 66 28 20 66 69 6c 65 45  .      if( fileE
1c598 78 69 73 74 73 28 64 62 2c 20 22 76 64 62 65 5f  xists(db, "vdbe_
1c599 73 71 6c 74 72 61 63 65 22 29 20 29 7b 0a 20 20  sqltrace") ){.  
1c59a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1c59b 65 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20  ePrintSql(p);.  
1c59c 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
1c59d 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
1c59e 6f 63 28 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  oc();.    }.#end
1c59f 69 66 0a 20 20 20 20 20 20 0a 0a 20 20 20 20 2f  if.      ..    /
1c5a0 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
1c5a1 66 20 77 65 20 6e 65 65 64 20 74 6f 20 73 69 6d  f we need to sim
1c5a2 75 6c 61 74 65 20 61 6e 20 69 6e 74 65 72 72 75  ulate an interru
1c5a3 70 74 2e 20 20 54 68 69 73 20 6f 6e 6c 79 20 68  pt.  This only h
1c5a4 61 70 70 65 6e 73 0a 20 20 20 20 2a 2a 20 69 66  appens.    ** if
1c5a5 20 77 65 20 68 61 76 65 20 61 20 73 70 65 63 69   we have a speci
1c5a6 61 6c 20 74 65 73 74 20 62 75 69 6c 64 2e 0a 20  al test build.. 
1c5a7 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c     */.#ifdef SQL
1c5a8 49 54 45 5f 54 45 53 54 0a 20 20 20 20 69 66 28  ITE_TEST.    if(
1c5a9 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
1c5aa 70 74 5f 63 6f 75 6e 74 3e 30 20 29 7b 0a 20 20  pt_count>0 ){.  
1c5ab 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 65      sqlite3_inte
1c5ac 72 72 75 70 74 5f 63 6f 75 6e 74 2d 2d 3b 0a 20  rrupt_count--;. 
1c5ad 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1c5ae 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74  _interrupt_count
1c5af 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ==0 ){.        s
1c5b0 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
1c5b1 28 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  (db);.      }.  
1c5b2 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e    }.#endif..#ifn
1c5b3 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1c5b4 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43  PROGRESS_CALLBAC
1c5b5 4b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 68  K.    /* Call th
1c5b6 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62  e progress callb
1c5b7 61 63 6b 20 69 66 20 69 74 20 69 73 20 63 6f 6e  ack if it is con
1c5b8 66 69 67 75 72 65 64 20 61 6e 64 20 74 68 65 20  figured and the 
1c5b9 72 65 71 75 69 72 65 64 20 6e 75 6d 62 65 72 0a  required number.
1c5ba 20 20 20 20 2a 2a 20 6f 66 20 56 44 42 45 20 6f      ** of VDBE o
1c5bb 70 73 20 68 61 76 65 20 62 65 65 6e 20 65 78 65  ps have been exe
1c5bc 63 75 74 65 64 20 28 65 69 74 68 65 72 20 73 69  cuted (either si
1c5bd 6e 63 65 20 74 68 69 73 20 69 6e 76 6f 63 61 74  nce this invocat
1c5be 69 6f 6e 20 6f 66 0a 20 20 20 20 2a 2a 20 73 71  ion of.    ** sq
1c5bf 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 20  lite3VdbeExec() 
1c5c0 6f 72 20 73 69 6e 63 65 20 6c 61 73 74 20 74 69  or since last ti
1c5c1 6d 65 20 74 68 65 20 70 72 6f 67 72 65 73 73 20  me the progress 
1c5c2 63 61 6c 6c 62 61 63 6b 20 77 61 73 20 63 61 6c  callback was cal
1c5c3 6c 65 64 29 2e 0a 20 20 20 20 2a 2a 20 49 66 20  led)..    ** If 
1c5c4 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  the progress cal
1c5c5 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f  lback returns no
1c5c6 6e 2d 7a 65 72 6f 2c 20 65 78 69 74 20 74 68 65  n-zero, exit the
1c5c7 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
1c5c8 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 61 20 72   with.    ** a r
1c5c9 65 74 75 72 6e 20 63 6f 64 65 20 53 51 4c 49 54  eturn code SQLIT
1c5ca 45 5f 41 42 4f 52 54 2e 0a 20 20 20 20 2a 2f 0a  E_ABORT..    */.
1c5cb 20 20 20 20 69 66 28 20 64 62 2d 3e 78 50 72 6f      if( db->xPro
1c5cc 67 72 65 73 73 20 29 7b 0a 20 20 20 20 20 20 69  gress ){.      i
1c5cd 66 28 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73  f( db->nProgress
1c5ce 4f 70 73 3d 3d 6e 50 72 6f 67 72 65 73 73 4f 70  Ops==nProgressOp
1c5cf 73 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  s ){.        int
1c5d0 20 70 72 63 3b 0a 20 20 20 20 20 20 20 20 69 66   prc;.        if
1c5d1 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
1c5d2 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  ff(db) ) goto ab
1c5d3 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
1c5d4 65 3b 0a 20 20 20 20 20 20 20 20 70 72 63 20 3d  e;.        prc =
1c5d5 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 28 64 62  db->xProgress(db
1c5d6 2d 3e 70 50 72 6f 67 72 65 73 73 41 72 67 29 3b  ->pProgressArg);
1c5d7 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  .        if( sql
1c5d8 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
1c5d9 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
1c5da 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20  e_to_misuse;.   
1c5db 20 20 20 20 20 69 66 28 20 70 72 63 21 3d 30 20       if( prc!=0 
1c5dc 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
1c5dd 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55  = SQLITE_INTERRU
1c5de 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f  PT;.          go
1c5df 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61  to vdbe_error_ha
1c5e0 6c 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  lt;.        }.  
1c5e1 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 73 4f        nProgressO
1c5e2 70 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  ps = 0;.      }.
1c5e3 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 73 4f        nProgressO
1c5e4 70 73 2b 2b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  ps++;.    }.#end
1c5e5 69 66 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f  if..    /* Do co
1c5e6 6d 6d 6f 6e 20 73 65 74 75 70 20 70 72 6f 63 65  mmon setup proce
1c5e7 73 73 69 6e 67 20 66 6f 72 20 61 6e 79 20 6f 70  ssing for any op
1c5e8 63 6f 64 65 20 74 68 61 74 20 69 73 20 6d 61 72  code that is mar
1c5e9 6b 65 64 0a 20 20 20 20 2a 2a 20 77 69 74 68 20  ked.    ** with 
1c5ea 74 68 65 20 22 6f 75 74 32 2d 70 72 65 72 65 6c  the "out2-prerel
1c5eb 65 61 73 65 22 20 74 61 67 2e 20 20 53 75 63 68  ease" tag.  Such
1c5ec 20 6f 70 63 6f 64 65 73 20 68 61 76 65 20 61 20   opcodes have a 
1c5ed 73 69 6e 67 6c 65 0a 20 20 20 20 2a 2a 20 6f 75  single.    ** ou
1c5ee 74 70 75 74 20 77 68 69 63 68 20 69 73 20 73 70  tput which is sp
1c5ef 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 50  ecified by the P
1c5f0 32 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68  2 parameter.  Th
1c5f1 65 20 50 32 20 72 65 67 69 73 74 65 72 0a 20 20  e P2 register.  
1c5f2 20 20 2a 2a 20 69 73 20 69 6e 69 74 69 61 6c 69    ** is initiali
1c5f3 7a 65 64 20 74 6f 20 61 20 4e 55 4c 4c 2e 0a 20  zed to a NULL.. 
1c5f4 20 20 20 2a 2f 0a 20 20 20 20 6f 70 50 72 6f 70     */.    opProp
1c5f5 65 72 74 79 20 3d 20 6f 70 63 6f 64 65 50 72 6f  erty = opcodePro
1c5f6 70 65 72 74 79 5b 70 4f 70 2d 3e 6f 70 63 6f 64  perty[pOp->opcod
1c5f7 65 5d 3b 0a 20 20 20 20 69 66 28 20 28 6f 70 50  e];.    if( (opP
1c5f8 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f  roperty & OPFLG_
1c5f9 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 29  OUT2_PRERELEASE)
1c5fa 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73  !=0 ){.      ass
1c5fb 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29  ert( pOp->p2>0 )
1c5fc 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1c5fd 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p2<=p->nMem
1c5fe 20 29 3b 0a 20 20 20 20 20 20 70 4f 75 74 20 3d   );.      pOut =
1c5ff 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1c600 32 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  2];.      sqlite
1c601 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45  3VdbeMemReleaseE
1c602 78 74 65 72 6e 61 6c 28 70 4f 75 74 29 3b 0a 20  xternal(pOut);. 
1c603 20 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73       pOut->flags
1c604 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20   = MEM_Null;.   
1c605 20 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 30 3b 0a     pOut->n = 0;.
1c606 20 20 20 20 7d 65 6c 73 65 0a 20 0a 20 20 20 20      }else. .    
1c607 2f 2a 20 44 6f 20 63 6f 6d 6d 6f 6e 20 73 65 74  /* Do common set
1c608 75 70 20 66 6f 72 20 6f 70 63 6f 64 65 73 20 6d  up for opcodes m
1c609 61 72 6b 65 64 20 77 69 74 68 20 6f 6e 65 20 6f  arked with one o
1c60a 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  f the following.
1c60b 20 20 20 20 2a 2a 20 63 6f 6d 62 69 6e 61 74 69      ** combinati
1c60c 6f 6e 73 20 6f 66 20 70 72 6f 70 65 72 74 69 65  ons of propertie
1c60d 73 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  s..    **.    **
1c60e 20 20 20 20 20 20 20 20 20 20 20 69 6e 31 0a 20             in1. 
1c60f 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20     **           
1c610 69 6e 31 20 69 6e 32 0a 20 20 20 20 2a 2a 20 20  in1 in2.    **  
1c611 20 20 20 20 20 20 20 20 20 69 6e 31 20 69 6e 32           in1 in2
1c612 20 6f 75 74 33 0a 20 20 20 20 2a 2a 20 20 20 20   out3.    **    
1c613 20 20 20 20 20 20 20 69 6e 31 20 69 6e 33 0a 20         in1 in3. 
1c614 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 56 61 72     **.    ** Var
1c615 69 61 62 6c 65 73 20 70 49 6e 31 2c 20 70 49 6e  iables pIn1, pIn
1c616 32 2c 20 61 6e 64 20 70 49 6e 33 20 61 72 65 20  2, and pIn3 are 
1c617 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f  made to point to
1c618 20 61 70 70 72 6f 70 72 69 61 74 65 0a 20 20 20   appropriate.   
1c619 20 2a 2a 20 72 65 67 69 73 74 65 72 73 20 66 6f   ** registers fo
1c61a 72 20 69 6e 70 75 74 73 2e 20 20 56 61 72 69 61  r inputs.  Varia
1c61b 62 6c 65 20 70 4f 75 74 20 70 6f 69 6e 74 73 20  ble pOut points 
1c61c 74 6f 20 74 68 65 20 6f 75 74 70 75 74 20 72 65  to the output re
1c61d 67 69 73 74 65 72 2e 0a 20 20 20 20 2a 2f 0a 20  gister..    */. 
1c61e 20 20 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72     if( (opProper
1c61f 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 31 29 21  ty & OPFLG_IN1)!
1c620 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  =0 ){.      asse
1c621 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b  rt( pOp->p1>0 );
1c622 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1c623 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p1<=p->nMem 
1c624 29 3b 0a 20 20 20 20 20 20 70 49 6e 31 20 3d 20  );.      pIn1 = 
1c625 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31  &p->aMem[pOp->p1
1c626 5d 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45  ];.      REGISTE
1c627 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c  R_TRACE(pOp->p1,
1c628 20 70 49 6e 31 29 3b 0a 20 20 20 20 20 20 69 66   pIn1);.      if
1c629 28 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20  ( (opProperty & 
1c62a 4f 50 46 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b  OPFLG_IN2)!=0 ){
1c62b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1c62c 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20   pOp->p2>0 );.  
1c62d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
1c62e 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p2<=p->nMem )
1c62f 3b 0a 20 20 20 20 20 20 20 20 70 49 6e 32 20 3d  ;.        pIn2 =
1c630 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1c631 32 5d 3b 0a 20 20 20 20 20 20 20 20 52 45 47 49  2];.        REGI
1c632 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
1c633 70 32 2c 20 70 49 6e 32 29 3b 0a 20 20 20 20 20  p2, pIn2);.     
1c634 20 20 20 2f 2a 20 41 73 20 63 75 72 72 65 6e 74     /* As current
1c635 6c 79 20 69 6d 70 6c 65 6d 65 6e 74 65 64 2c 20  ly implemented, 
1c636 69 6e 32 20 69 6d 70 6c 69 65 73 20 6f 75 74 33  in2 implies out3
1c637 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20 72  .  There is no r
1c638 65 61 73 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a  eason.        **
1c639 20 77 68 79 20 74 68 69 73 20 68 61 73 20 74 6f   why this has to
1c63a 20 62 65 2c 20 69 74 20 6a 75 73 74 20 77 6f 72   be, it just wor
1c63b 6b 65 64 20 6f 75 74 20 74 68 61 74 20 77 61 79  ked out that way
1c63c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73  . */.        ass
1c63d 65 72 74 28 20 28 6f 70 50 72 6f 70 65 72 74 79  ert( (opProperty
1c63e 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33 29 21 3d   & OPFLG_OUT3)!=
1c63f 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  0 );.        ass
1c640 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29  ert( pOp->p3>0 )
1c641 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
1c642 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d  ( pOp->p3<=p->nM
1c643 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4f  em );.        pO
1c644 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  ut = &p->aMem[pO
1c645 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 7d 65  p->p3];.      }e
1c646 6c 73 65 20 69 66 28 20 28 6f 70 50 72 6f 70 65  lse if( (opPrope
1c647 72 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 33 29  rty & OPFLG_IN3)
1c648 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61  !=0 ){.        a
1c649 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30  ssert( pOp->p3>0
1c64a 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65   );.        asse
1c64b 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e  rt( pOp->p3<=p->
1c64c 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20  nMem );.        
1c64d 70 49 6e 33 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pIn3 = &p->aMem[
1c64e 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20  pOp->p3];.      
1c64f 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1c650 28 70 4f 70 2d 3e 70 33 2c 20 70 49 6e 33 29 3b  (pOp->p3, pIn3);
1c651 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
1c652 73 65 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72  se if( (opProper
1c653 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 32 29 21  ty & OPFLG_IN2)!
1c654 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  =0 ){.      asse
1c655 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b  rt( pOp->p2>0 );
1c656 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1c657 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p2<=p->nMem 
1c658 29 3b 0a 20 20 20 20 20 20 70 49 6e 32 20 3d 20  );.      pIn2 = 
1c659 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32  &p->aMem[pOp->p2
1c65a 5d 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45  ];.      REGISTE
1c65b 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c  R_TRACE(pOp->p2,
1c65c 20 70 49 6e 32 29 3b 0a 20 20 20 20 7d 65 6c 73   pIn2);.    }els
1c65d 65 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74  e if( (opPropert
1c65e 79 20 26 20 4f 50 46 4c 47 5f 49 4e 33 29 21 3d  y & OPFLG_IN3)!=
1c65f 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  0 ){.      asser
1c660 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a  t( pOp->p3>0 );.
1c661 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
1c662 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p3<=p->nMem )
1c663 3b 0a 20 20 20 20 20 20 70 49 6e 33 20 3d 20 26  ;.      pIn3 = &
1c664 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  p->aMem[pOp->p3]
1c665 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45 52  ;.      REGISTER
1c666 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20  _TRACE(pOp->p3, 
1c667 70 49 6e 33 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  pIn3);.    }..  
1c668 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f    switch( pOp->o
1c669 70 63 6f 64 65 20 29 7b 0a 0a 2f 2a 2a 2a 2a 2a  pcode ){../*****
1c66a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c66b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c66c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c66d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c66e 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 57 68 61 74  ********.** What
1c66f 20 66 6f 6c 6c 6f 77 73 20 69 73 20 61 20 6d 61   follows is a ma
1c670 73 73 69 76 65 20 73 77 69 74 63 68 20 73 74 61  ssive switch sta
1c671 74 65 6d 65 6e 74 20 77 68 65 72 65 20 65 61 63  tement where eac
1c672 68 20 63 61 73 65 20 69 6d 70 6c 65 6d 65 6e 74  h case implement
1c673 73 20 61 0a 2a 2a 20 73 65 70 61 72 61 74 65 20  s a.** separate 
1c674 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20 74  instruction in t
1c675 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  he virtual machi
1c676 6e 65 2e 20 20 49 66 20 77 65 20 66 6f 6c 6c 6f  ne.  If we follo
1c677 77 20 74 68 65 20 75 73 75 61 6c 0a 2a 2a 20 69  w the usual.** i
1c678 6e 64 65 6e 74 61 74 69 6f 6e 20 63 6f 6e 76 65  ndentation conve
1c679 6e 74 69 6f 6e 73 2c 20 65 61 63 68 20 63 61 73  ntions, each cas
1c67a 65 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 64 65  e should be inde
1c67b 6e 74 65 64 20 62 79 20 36 20 73 70 61 63 65 73  nted by 6 spaces
1c67c 2e 20 20 42 75 74 0a 2a 2a 20 74 68 61 74 20 69  .  But.** that i
1c67d 73 20 61 20 6c 6f 74 20 6f 66 20 77 61 73 74 65  s a lot of waste
1c67e 64 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20 6c  d space on the l
1c67f 65 66 74 20 6d 61 72 67 69 6e 2e 20 20 53 6f 20  eft margin.  So 
1c680 74 68 65 20 63 6f 64 65 20 77 69 74 68 69 6e 0a  the code within.
1c681 2a 2a 20 74 68 65 20 73 77 69 74 63 68 20 73 74  ** the switch st
1c682 61 74 65 6d 65 6e 74 20 77 69 6c 6c 20 62 72 65  atement will bre
1c683 61 6b 20 77 69 74 68 20 63 6f 6e 76 65 6e 74 69  ak with conventi
1c684 6f 6e 20 61 6e 64 20 62 65 20 66 6c 75 73 68 2d  on and be flush-
1c685 6c 65 66 74 2e 20 41 6e 6f 74 68 65 72 0a 2a 2a  left. Another.**
1c686 20 62 69 67 20 63 6f 6d 6d 65 6e 74 20 28 73 69   big comment (si
1c687 6d 69 6c 61 72 20 74 6f 20 74 68 69 73 20 6f 6e  milar to this on
1c688 65 29 20 77 69 6c 6c 20 6d 61 72 6b 20 74 68 65  e) will mark the
1c689 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 63 6f   point in the co
1c68a 64 65 20 77 68 65 72 65 0a 2a 2a 20 77 65 20 74  de where.** we t
1c68b 72 61 6e 73 69 74 69 6f 6e 20 62 61 63 6b 20 74  ransition back t
1c68c 6f 20 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 61  o normal indenta
1c68d 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tion..**.** The 
1c68e 66 6f 72 6d 61 74 74 69 6e 67 20 6f 66 20 65 61  formatting of ea
1c68f 63 68 20 63 61 73 65 20 69 73 20 69 6d 70 6f 72  ch case is impor
1c690 74 61 6e 74 2e 20 20 54 68 65 20 6d 61 6b 65 66  tant.  The makef
1c691 69 6c 65 20 66 6f 72 20 53 51 4c 69 74 65 0a 2a  ile for SQLite.*
1c692 2a 20 67 65 6e 65 72 61 74 65 73 20 74 77 6f 20  * generates two 
1c693 43 20 66 69 6c 65 73 20 22 6f 70 63 6f 64 65 73  C files "opcodes
1c694 2e 68 22 20 61 6e 64 20 22 6f 70 63 6f 64 65 73  .h" and "opcodes
1c695 2e 63 22 20 62 79 20 73 63 61 6e 6e 69 6e 67 20  .c" by scanning 
1c696 74 68 69 73 0a 2a 2a 20 66 69 6c 65 20 6c 6f 6f  this.** file loo
1c697 6b 69 6e 67 20 66 6f 72 20 6c 69 6e 65 73 20 74  king for lines t
1c698 68 61 74 20 62 65 67 69 6e 20 77 69 74 68 20 22  hat begin with "
1c699 63 61 73 65 20 4f 50 5f 22 2e 20 20 54 68 65 20  case OP_".  The 
1c69a 6f 70 63 6f 64 65 73 2e 68 20 66 69 6c 65 73 0a  opcodes.h files.
1c69b 2a 2a 20 77 69 6c 6c 20 62 65 20 66 69 6c 6c 65  ** will be fille
1c69c 64 20 77 69 74 68 20 23 64 65 66 69 6e 65 73 20  d with #defines 
1c69d 74 68 61 74 20 67 69 76 65 20 75 6e 69 71 75 65  that give unique
1c69e 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20   integer values 
1c69f 74 6f 20 65 61 63 68 0a 2a 2a 20 6f 70 63 6f 64  to each.** opcod
1c6a0 65 20 61 6e 64 20 74 68 65 20 6f 70 63 6f 64 65  e and the opcode
1c6a1 73 2e 63 20 66 69 6c 65 20 69 73 20 66 69 6c 6c  s.c file is fill
1c6a2 65 64 20 77 69 74 68 20 61 6e 20 61 72 72 61 79  ed with an array
1c6a3 20 6f 66 20 73 74 72 69 6e 67 73 20 77 68 65 72   of strings wher
1c6a4 65 0a 2a 2a 20 65 61 63 68 20 73 74 72 69 6e 67  e.** each string
1c6a5 20 69 73 20 74 68 65 20 73 79 6d 62 6f 6c 69 63   is the symbolic
1c6a6 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 63 6f   name for the co
1c6a7 72 72 65 73 70 6f 6e 64 69 6e 67 20 6f 70 63 6f  rresponding opco
1c6a8 64 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 63  de.  If the.** c
1c6a9 61 73 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73  ase statement is
1c6aa 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 63   followed by a c
1c6ab 6f 6d 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 6f  omment of the fo
1c6ac 72 6d 20 22 2f 23 20 73 61 6d 65 20 61 73 20 2e  rm "/# same as .
1c6ad 2e 2e 20 23 2f 22 0a 2a 2a 20 74 68 61 74 20 63  .. #/".** that c
1c6ae 6f 6d 6d 65 6e 74 20 69 73 20 75 73 65 64 20 74  omment is used t
1c6af 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  o determine the 
1c6b0 70 61 72 74 69 63 75 6c 61 72 20 76 61 6c 75 65  particular value
1c6b1 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 2e 0a   of the opcode..
1c6b2 2a 2a 0a 2a 2a 20 4f 74 68 65 72 20 6b 65 79 77  **.** Other keyw
1c6b3 6f 72 64 73 20 69 6e 20 74 68 65 20 63 6f 6d 6d  ords in the comm
1c6b4 65 6e 74 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73  ent that follows
1c6b5 20 65 61 63 68 20 63 61 73 65 20 61 72 65 20 75   each case are u
1c6b6 73 65 64 20 74 6f 0a 2a 2a 20 63 6f 6e 73 74 72  sed to.** constr
1c6b7 75 63 74 20 74 68 65 20 4f 50 46 4c 47 5f 49 4e  uct the OPFLG_IN
1c6b8 49 54 49 41 4c 49 5a 45 52 20 76 61 6c 75 65 20  ITIALIZER value 
1c6b9 74 68 61 74 20 69 6e 69 74 69 61 6c 69 7a 65 73  that initializes
1c6ba 20 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b   opcodeProperty[
1c6bb 5d 2e 0a 2a 2a 20 4b 65 79 77 6f 72 64 73 20 69  ]..** Keywords i
1c6bc 6e 63 6c 75 64 65 3a 20 69 6e 31 2c 20 69 6e 32  nclude: in1, in2
1c6bd 2c 20 69 6e 33 2c 20 6f 75 74 32 5f 70 72 65 72  , in3, out2_prer
1c6be 65 6c 65 61 73 65 2c 20 6f 75 74 32 2c 20 6f 75  elease, out2, ou
1c6bf 74 33 2e 20 20 53 65 65 0a 2a 2a 20 74 68 65 20  t3.  See.** the 
1c6c0 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 73 63  mkopcodeh.awk sc
1c6c1 72 69 70 74 20 66 6f 72 20 61 64 64 69 74 69 6f  ript for additio
1c6c2 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
1c6c3 0a 2a 2a 0a 2a 2a 20 44 6f 63 75 6d 65 6e 74 61  .**.** Documenta
1c6c4 74 69 6f 6e 20 61 62 6f 75 74 20 56 44 42 45 20  tion about VDBE 
1c6c5 6f 70 63 6f 64 65 73 20 69 73 20 67 65 6e 65 72  opcodes is gener
1c6c6 61 74 65 64 20 62 79 20 73 63 61 6e 6e 69 6e 67  ated by scanning
1c6c7 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 66 6f   this file.** fo
1c6c8 72 20 6c 69 6e 65 73 20 6f 66 20 74 68 61 74 20  r lines of that 
1c6c9 63 6f 6e 74 61 69 6e 20 22 4f 70 63 6f 64 65 3a  contain "Opcode:
1c6ca 22 2e 20 20 54 68 61 74 20 6c 69 6e 65 20 61 6e  ".  That line an
1c6cb 64 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74  d all subsequent
1c6cc 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20 6c 69 6e 65  .** comment line
1c6cd 73 20 61 72 65 20 75 73 65 64 20 69 6e 20 74 68  s are used in th
1c6ce 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66 20  e generation of 
1c6cf 74 68 65 20 6f 70 63 6f 64 65 2e 68 74 6d 6c 20  the opcode.html 
1c6d0 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a  documentation.**
1c6d1 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 55 4d   file..**.** SUM
1c6d2 4d 41 52 59 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  MARY:.**.**     
1c6d3 46 6f 72 6d 61 74 74 69 6e 67 20 69 73 20 69 6d  Formatting is im
1c6d4 70 6f 72 74 61 6e 74 20 74 6f 20 73 63 72 69 70  portant to scrip
1c6d5 74 73 20 74 68 61 74 20 73 63 61 6e 20 74 68 69  ts that scan thi
1c6d6 73 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 20 20 44  s file..**     D
1c6d7 6f 20 6e 6f 74 20 64 65 76 69 61 74 65 20 66 72  o not deviate fr
1c6d8 6f 6d 20 74 68 65 20 66 6f 72 6d 61 74 74 69 6e  om the formattin
1c6d9 67 20 73 74 79 6c 65 20 63 75 72 72 65 6e 74 6c  g style currentl
1c6da 79 20 69 6e 20 75 73 65 2e 0a 2a 2a 0a 2a 2a 2a  y in use..**.***
1c6db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c6dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c6dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c6de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c6df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20  **********/../* 
1c6e0 4f 70 63 6f 64 65 3a 20 20 47 6f 74 6f 20 2a 20  Opcode:  Goto * 
1c6e1 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41  P2 * * *.**.** A
1c6e2 6e 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c 20  n unconditional 
1c6e3 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73 20  jump to address 
1c6e4 50 32 2e 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20  P2..** The next 
1c6e5 69 6e 73 74 72 75 63 74 69 6f 6e 20 65 78 65 63  instruction exec
1c6e6 75 74 65 64 20 77 69 6c 6c 20 62 65 20 0a 2a 2a  uted will be .**
1c6e7 20 74 68 65 20 6f 6e 65 20 61 74 20 69 6e 64 65   the one at inde
1c6e8 78 20 50 32 20 66 72 6f 6d 20 74 68 65 20 62 65  x P2 from the be
1c6e9 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74 68  ginning of.** th
1c6ea 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 63 61  e program..*/.ca
1c6eb 73 65 20 4f 50 5f 47 6f 74 6f 3a 20 7b 20 20 20  se OP_Goto: {   
1c6ec 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d            /* jum
1c6ed 70 20 2a 2f 0a 20 20 43 48 45 43 4b 5f 46 4f 52  p */.  CHECK_FOR
1c6ee 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 70 63  _INTERRUPT;.  pc
1c6ef 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1c6f0 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1c6f1 70 63 6f 64 65 3a 20 20 47 6f 73 75 62 20 50 31  pcode:  Gosub P1
1c6f2 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1c6f3 57 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e  Write the curren
1c6f4 74 20 61 64 64 72 65 73 73 20 6f 6e 74 6f 20 72  t address onto r
1c6f5 65 67 69 73 74 65 72 20 50 31 0a 2a 2a 20 61 6e  egister P1.** an
1c6f6 64 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 61  d then jump to a
1c6f7 64 64 72 65 73 73 20 50 32 2e 0a 2a 2f 0a 63 61  ddress P2..*/.ca
1c6f8 73 65 20 4f 50 5f 47 6f 73 75 62 3a 20 7b 20 20  se OP_Gosub: {  
1c6f9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d            /* jum
1c6fa 70 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  p */.  assert( p
1c6fb 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 61 73  Op->p1>0 );.  as
1c6fc 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d 70  sert( pOp->p1<=p
1c6fd 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 49 6e 31  ->nMem );.  pIn1
1c6fe 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
1c6ff 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  >p1];.  assert( 
1c700 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d  (pIn1->flags & M
1c701 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20  EM_Dyn)==0 );.  
1c702 70 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pIn1->flags = ME
1c703 4d 5f 49 6e 74 3b 0a 20 20 70 49 6e 31 2d 3e 75  M_Int;.  pIn1->u
1c704 2e 69 20 3d 20 70 63 3b 0a 20 20 52 45 47 49 53  .i = pc;.  REGIS
1c705 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
1c706 31 2c 20 70 49 6e 31 29 3b 0a 20 20 70 63 20 3d  1, pIn1);.  pc =
1c707 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
1c708 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1c709 6f 64 65 3a 20 20 52 65 74 75 72 6e 20 50 31 20  ode:  Return P1 
1c70a 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75  * * * *.**.** Ju
1c70b 6d 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69  mp to the next i
1c70c 6e 73 74 72 75 63 74 69 6f 6e 20 61 66 74 65 72  nstruction after
1c70d 20 74 68 65 20 61 64 64 72 65 73 73 20 69 6e 20   the address in 
1c70e 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f 0a  register P1..*/.
1c70f 63 61 73 65 20 4f 50 5f 52 65 74 75 72 6e 3a 20  case OP_Return: 
1c710 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69  {           /* i
1c711 6e 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  n1 */.  assert( 
1c712 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
1c713 4d 5f 49 6e 74 20 29 3b 0a 20 20 70 63 20 3d 20  M_Int );.  pc = 
1c714 28 69 6e 74 29 70 49 6e 31 2d 3e 75 2e 69 3b 0a  (int)pIn1->u.i;.
1c715 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1c716 70 63 6f 64 65 3a 20 20 59 69 65 6c 64 20 50 31  pcode:  Yield P1
1c717 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53   * * * *.**.** S
1c718 77 61 70 20 74 68 65 20 70 72 6f 67 72 61 6d 20  wap the program 
1c719 63 6f 75 6e 74 65 72 20 77 69 74 68 20 74 68 65  counter with the
1c71a 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1c71b 65 72 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f  er P1..*/.case O
1c71c 50 5f 59 69 65 6c 64 3a 20 7b 20 20 20 20 20 20  P_Yield: {      
1c71d 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a        /* in1 */.
1c71e 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1c71f 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1c720 69 6e 74 6f 20 75 2e 61 61 20 2a 2f 0a 20 20 69  into u.aa */.  i
1c721 6e 74 20 70 63 44 65 73 74 3b 0a 23 65 6e 64 69  nt pcDest;.#endi
1c722 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1c723 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1c724 75 2e 61 61 20 2a 2f 0a 20 20 61 73 73 65 72 74  u.aa */.  assert
1c725 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  ( (pIn1->flags &
1c726 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a   MEM_Dyn)==0 );.
1c727 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20    pIn1->flags = 
1c728 4d 45 4d 5f 49 6e 74 3b 0a 20 20 75 2e 61 61 2e  MEM_Int;.  u.aa.
1c729 70 63 44 65 73 74 20 3d 20 28 69 6e 74 29 70 49  pcDest = (int)pI
1c72a 6e 31 2d 3e 75 2e 69 3b 0a 20 20 70 49 6e 31 2d  n1->u.i;.  pIn1-
1c72b 3e 75 2e 69 20 3d 20 70 63 3b 0a 20 20 52 45 47  >u.i = pc;.  REG
1c72c 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
1c72d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 70 63  >p1, pIn1);.  pc
1c72e 20 3d 20 75 2e 61 61 2e 70 63 44 65 73 74 3b 0a   = u.aa.pcDest;.
1c72f 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1c730 70 63 6f 64 65 3a 20 20 48 61 6c 74 49 66 4e 75  pcode:  HaltIfNu
1c731 6c 6c 20 20 50 31 20 50 32 20 50 33 20 50 34 20  ll  P1 P2 P3 P4 
1c732 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68  *.**.** Check th
1c733 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1c734 74 65 72 20 50 33 2e 20 20 49 66 20 69 73 20 69  ter P3.  If is i
1c735 73 20 4e 55 4c 4c 20 74 68 65 6e 20 48 61 6c 74  s NULL then Halt
1c736 20 75 73 69 6e 67 0a 2a 2a 20 70 61 72 61 6d 65   using.** parame
1c737 74 65 72 20 50 31 2c 20 50 32 2c 20 61 6e 64 20  ter P1, P2, and 
1c738 50 34 20 61 73 20 69 66 20 74 68 69 73 20 77 65  P4 as if this we
1c739 72 65 20 61 20 48 61 6c 74 20 69 6e 73 74 72 75  re a Halt instru
1c73a 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 0a 2a  ction.  If the.*
1c73b 2a 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  * value in regis
1c73c 74 65 72 20 50 33 20 69 73 20 6e 6f 74 20 4e 55  ter P3 is not NU
1c73d 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f  LL, then this ro
1c73e 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
1c73f 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 48 61 6c  ..*/.case OP_Hal
1c740 74 49 66 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20  tIfNull: {      
1c741 2f 2a 20 69 6e 33 20 2a 2f 0a 20 20 69 66 28 20  /* in3 */.  if( 
1c742 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d  (pIn3->flags & M
1c743 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 20 62 72  EM_Null)==0 ) br
1c744 65 61 6b 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20 74  eak;.  /* Fall t
1c745 68 72 6f 75 67 68 20 69 6e 74 6f 20 4f 50 5f 48  hrough into OP_H
1c746 61 6c 74 20 2a 2f 0a 7d 0a 0a 2f 2a 20 4f 70 63  alt */.}../* Opc
1c747 6f 64 65 3a 20 20 48 61 6c 74 20 50 31 20 50 32  ode:  Halt P1 P2
1c748 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78   * P4 *.**.** Ex
1c749 69 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20  it immediately. 
1c74a 20 41 6c 6c 20 6f 70 65 6e 20 63 75 72 73 6f 72   All open cursor
1c74b 73 2c 20 65 74 63 20 61 72 65 20 63 6c 6f 73 65  s, etc are close
1c74c 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c  d.** automatical
1c74d 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20  ly..**.** P1 is 
1c74e 74 68 65 20 72 65 73 75 6c 74 20 63 6f 64 65 20  the result code 
1c74f 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69  returned by sqli
1c750 74 65 33 5f 65 78 65 63 28 29 2c 20 73 71 6c 69  te3_exec(), sqli
1c751 74 65 33 5f 72 65 73 65 74 28 29 2c 0a 2a 2a 20  te3_reset(),.** 
1c752 6f 72 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  or sqlite3_final
1c753 69 7a 65 28 29 2e 20 20 46 6f 72 20 61 20 6e 6f  ize().  For a no
1c754 72 6d 61 6c 20 68 61 6c 74 2c 20 74 68 69 73 20  rmal halt, this 
1c755 73 68 6f 75 6c 64 20 62 65 20 53 51 4c 49 54 45  should be SQLITE
1c756 5f 4f 4b 20 28 30 29 2e 0a 2a 2a 20 46 6f 72 20  _OK (0)..** For 
1c757 65 72 72 6f 72 73 2c 20 69 74 20 63 61 6e 20 62  errors, it can b
1c758 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 76 61 6c  e some other val
1c759 75 65 2e 20 20 49 66 20 50 31 21 3d 30 20 74 68  ue.  If P1!=0 th
1c75a 65 6e 20 50 32 20 77 69 6c 6c 20 64 65 74 65 72  en P2 will deter
1c75b 6d 69 6e 65 0a 2a 2a 20 77 68 65 74 68 65 72 20  mine.** whether 
1c75c 6f 72 20 6e 6f 74 20 74 6f 20 72 6f 6c 6c 62 61  or not to rollba
1c75d 63 6b 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  ck the current t
1c75e 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 44 6f 20  ransaction.  Do 
1c75f 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20  not rollback.** 
1c760 69 66 20 50 32 3d 3d 4f 45 5f 46 61 69 6c 2e 20  if P2==OE_Fail. 
1c761 44 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  Do the rollback 
1c762 69 66 20 50 32 3d 3d 4f 45 5f 52 6f 6c 6c 62 61  if P2==OE_Rollba
1c763 63 6b 2e 20 20 49 66 20 50 32 3d 3d 4f 45 5f 41  ck.  If P2==OE_A
1c764 62 6f 72 74 2c 0a 2a 2a 20 74 68 65 6e 20 62 61  bort,.** then ba
1c765 63 6b 20 6f 75 74 20 61 6c 6c 20 63 68 61 6e 67  ck out all chang
1c766 65 73 20 74 68 61 74 20 68 61 76 65 20 6f 63 63  es that have occ
1c767 75 72 72 65 64 20 64 75 72 69 6e 67 20 74 68 69  urred during thi
1c768 73 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74  s execution of t
1c769 68 65 0a 2a 2a 20 56 44 42 45 2c 20 62 75 74 20  he.** VDBE, but 
1c76a 64 6f 20 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20  do not rollback 
1c76b 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  the transaction.
1c76c 20 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73   .**.** If P4 is
1c76d 20 6e 6f 74 20 6e 75 6c 6c 20 74 68 65 6e 20 69   not null then i
1c76e 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 6d 65  t is an error me
1c76f 73 73 61 67 65 20 73 74 72 69 6e 67 2e 0a 2a 2a  ssage string..**
1c770 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 6e 20  .** There is an 
1c771 69 6d 70 6c 69 65 64 20 22 48 61 6c 74 20 30 20  implied "Halt 0 
1c772 30 20 30 22 20 69 6e 73 74 72 75 63 74 69 6f 6e  0 0" instruction
1c773 20 69 6e 73 65 72 74 65 64 20 61 74 20 74 68 65   inserted at the
1c774 20 76 65 72 79 20 65 6e 64 20 6f 66 0a 2a 2a 20   very end of.** 
1c775 65 76 65 72 79 20 70 72 6f 67 72 61 6d 2e 20 20  every program.  
1c776 53 6f 20 61 20 6a 75 6d 70 20 70 61 73 74 20 74  So a jump past t
1c777 68 65 20 6c 61 73 74 20 69 6e 73 74 72 75 63 74  he last instruct
1c778 69 6f 6e 20 6f 66 20 74 68 65 20 70 72 6f 67 72  ion of the progr
1c779 61 6d 0a 2a 2a 20 69 73 20 74 68 65 20 73 61 6d  am.** is the sam
1c77a 65 20 61 73 20 65 78 65 63 75 74 69 6e 67 20 48  e as executing H
1c77b 61 6c 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  alt..*/.case OP_
1c77c 48 61 6c 74 3a 20 7b 0a 20 20 69 66 28 20 70 4f  Halt: {.  if( pO
1c77d 70 2d 3e 70 31 3d 3d 53 51 4c 49 54 45 5f 4f 4b  p->p1==SQLITE_OK
1c77e 20 26 26 20 70 2d 3e 70 46 72 61 6d 65 20 29 7b   && p->pFrame ){
1c77f 0a 20 20 20 20 2f 2a 20 48 61 6c 74 20 74 68 65  .    /* Halt the
1c780 20 73 75 62 2d 70 72 6f 67 72 61 6d 2e 20 52 65   sub-program. Re
1c781 74 75 72 6e 20 63 6f 6e 74 72 6f 6c 20 74 6f 20  turn control to 
1c782 74 68 65 20 70 61 72 65 6e 74 20 66 72 61 6d 65  the parent frame
1c783 2e 20 2a 2f 0a 20 20 20 20 56 64 62 65 46 72 61  . */.    VdbeFra
1c784 6d 65 20 2a 70 46 72 61 6d 65 20 3d 20 70 2d 3e  me *pFrame = p->
1c785 70 46 72 61 6d 65 3b 0a 20 20 20 20 70 2d 3e 70  pFrame;.    p->p
1c786 46 72 61 6d 65 20 3d 20 70 46 72 61 6d 65 2d 3e  Frame = pFrame->
1c787 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 70 2d 3e  pParent;.    p->
1c788 6e 46 72 61 6d 65 2d 2d 3b 0a 20 20 20 20 73 71  nFrame--;.    sq
1c789 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e  lite3VdbeSetChan
1c78a 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e  ges(db, p->nChan
1c78b 67 65 29 3b 0a 20 20 20 20 70 63 20 3d 20 73 71  ge);.    pc = sq
1c78c 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65 52 65  lite3VdbeFrameRe
1c78d 73 74 6f 72 65 28 70 46 72 61 6d 65 29 3b 0a 20  store(pFrame);. 
1c78e 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d     if( pOp->p2==
1c78f 4f 45 5f 49 67 6e 6f 72 65 20 29 7b 0a 20 20 20  OE_Ignore ){.   
1c790 20 20 20 2f 2a 20 49 6e 73 74 72 75 63 74 69 6f     /* Instructio
1c791 6e 20 70 63 20 69 73 20 74 68 65 20 4f 50 5f 50  n pc is the OP_P
1c792 72 6f 67 72 61 6d 20 74 68 61 74 20 69 6e 76 6f  rogram that invo
1c793 6b 65 64 20 74 68 65 20 73 75 62 2d 70 72 6f 67  ked the sub-prog
1c794 72 61 6d 20 0a 20 20 20 20 20 20 2a 2a 20 63 75  ram .      ** cu
1c795 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 68 61  rrently being ha
1c796 6c 74 65 64 2e 20 49 66 20 74 68 65 20 70 32 20  lted. If the p2 
1c797 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74  instruction of t
1c798 68 69 73 20 4f 50 5f 48 61 6c 74 0a 20 20 20 20  his OP_Halt.    
1c799 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e    ** instruction
1c79a 20 69 73 20 73 65 74 20 74 6f 20 4f 45 5f 49 67   is set to OE_Ig
1c79b 6e 6f 72 65 2c 20 74 68 65 6e 20 74 68 65 20 73  nore, then the s
1c79c 75 62 2d 70 72 6f 67 72 61 6d 20 69 73 20 74 68  ub-program is th
1c79d 72 6f 77 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20  rowing.      ** 
1c79e 61 6e 20 49 47 4e 4f 52 45 20 65 78 63 65 70 74  an IGNORE except
1c79f 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ion. In this cas
1c7a0 65 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 61 64  e jump to the ad
1c7a1 64 72 65 73 73 20 73 70 65 63 69 66 69 65 64 0a  dress specified.
1c7a2 20 20 20 20 20 20 2a 2a 20 61 73 20 74 68 65 20        ** as the 
1c7a3 70 32 20 6f 66 20 74 68 65 20 63 61 6c 6c 69 6e  p2 of the callin
1c7a4 67 20 4f 50 5f 50 72 6f 67 72 61 6d 2e 20 20 2a  g OP_Program.  *
1c7a5 2f 0a 20 20 20 20 20 20 70 63 20 3d 20 70 2d 3e  /.      pc = p->
1c7a6 61 4f 70 5b 70 63 5d 2e 70 32 2d 31 3b 0a 20 20  aOp[pc].p2-1;.  
1c7a7 20 20 7d 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20    }.    break;. 
1c7a8 20 7d 0a 0a 20 20 70 2d 3e 72 63 20 3d 20 70 4f   }..  p->rc = pO
1c7a9 70 2d 3e 70 31 3b 0a 20 20 70 2d 3e 65 72 72 6f  p->p1;.  p->erro
1c7aa 72 41 63 74 69 6f 6e 20 3d 20 28 75 38 29 70 4f  rAction = (u8)pO
1c7ab 70 2d 3e 70 32 3b 0a 20 20 70 2d 3e 70 63 20 3d  p->p2;.  p->pc =
1c7ac 20 70 63 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e   pc;.  if( pOp->
1c7ad 70 34 2e 7a 20 29 7b 0a 20 20 20 20 73 71 6c 69  p4.z ){.    sqli
1c7ae 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
1c7af 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25  >zErrMsg, db, "%
1c7b0 73 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a  s", pOp->p4.z);.
1c7b1 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74    }.  rc = sqlit
1c7b2 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20  e3VdbeHalt(p);. 
1c7b3 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
1c7b4 49 54 45 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d  ITE_BUSY || rc==
1c7b5 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69  SQLITE_OK );.  i
1c7b6 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55  f( rc==SQLITE_BU
1c7b7 53 59 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20  SY ){.    p->rc 
1c7b8 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55  = rc = SQLITE_BU
1c7b9 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  SY;.  }else{.   
1c7ba 20 72 63 20 3d 20 70 2d 3e 72 63 20 3f 20 53 51   rc = p->rc ? SQ
1c7bb 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20 53 51 4c  LITE_ERROR : SQL
1c7bc 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20  ITE_DONE;.  }.  
1c7bd 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e  goto vdbe_return
1c7be 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1c7bf 49 6e 74 65 67 65 72 20 50 31 20 50 32 20 2a 20  Integer P1 P2 * 
1c7c0 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 33 32  * *.**.** The 32
1c7c1 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76 61 6c  -bit integer val
1c7c2 75 65 20 50 31 20 69 73 20 77 72 69 74 74 65 6e  ue P1 is written
1c7c3 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50   into register P
1c7c4 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e  2..*/.case OP_In
1c7c5 74 65 67 65 72 3a 20 7b 20 20 20 20 20 20 20 20  teger: {        
1c7c6 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65   /* out2-prerele
1c7c7 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 2d 3e 66  ase */.  pOut->f
1c7c8 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
1c7c9 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 70 4f    pOut->u.i = pO
1c7ca 70 2d 3e 70 31 3b 0a 20 20 62 72 65 61 6b 3b 0a  p->p1;.  break;.
1c7cb 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e  }../* Opcode: In
1c7cc 74 36 34 20 2a 20 50 32 20 2a 20 50 34 20 2a 0a  t64 * P2 * P4 *.
1c7cd 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f  **.** P4 is a po
1c7ce 69 6e 74 65 72 20 74 6f 20 61 20 36 34 2d 62 69  inter to a 64-bi
1c7cf 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 2e  t integer value.
1c7d0 0a 2a 2a 20 57 72 69 74 65 20 74 68 61 74 20 76  .** Write that v
1c7d1 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74  alue into regist
1c7d2 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f  er P2..*/.case O
1c7d3 50 5f 49 6e 74 36 34 3a 20 7b 20 20 20 20 20 20  P_Int64: {      
1c7d4 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65       /* out2-pre
1c7d5 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73  release */.  ass
1c7d6 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 70 49 36  ert( pOp->p4.pI6
1c7d7 34 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d 3e  4!=0 );.  pOut->
1c7d8 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
1c7d9 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 2a  .  pOut->u.i = *
1c7da 70 4f 70 2d 3e 70 34 2e 70 49 36 34 3b 0a 20 20  pOp->p4.pI64;.  
1c7db 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1c7dc 6f 64 65 3a 20 52 65 61 6c 20 2a 20 50 32 20 2a  ode: Real * P2 *
1c7dd 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69   P4 *.**.** P4 i
1c7de 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
1c7df 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67   64-bit floating
1c7e0 20 70 6f 69 6e 74 20 76 61 6c 75 65 2e 0a 2a 2a   point value..**
1c7e1 20 57 72 69 74 65 20 74 68 61 74 20 76 61 6c 75   Write that valu
1c7e2 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20  e into register 
1c7e3 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52  P2..*/.case OP_R
1c7e4 65 61 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20  eal: {          
1c7e5 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1c7e6 46 4c 4f 41 54 2c 20 6f 75 74 32 2d 70 72 65 72  FLOAT, out2-prer
1c7e7 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74  elease */.  pOut
1c7e8 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 65  ->flags = MEM_Re
1c7e9 61 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 21 73  al;.  assert( !s
1c7ea 71 6c 69 74 65 33 49 73 4e 61 4e 28 2a 70 4f 70  qlite3IsNaN(*pOp
1c7eb 2d 3e 70 34 2e 70 52 65 61 6c 29 20 29 3b 0a 20  ->p4.pReal) );. 
1c7ec 20 70 4f 75 74 2d 3e 72 20 3d 20 2a 70 4f 70 2d   pOut->r = *pOp-
1c7ed 3e 70 34 2e 70 52 65 61 6c 3b 0a 20 20 62 72 65  >p4.pReal;.  bre
1c7ee 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1c7ef 3a 20 53 74 72 69 6e 67 38 20 2a 20 50 32 20 2a  : String8 * P2 *
1c7f0 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 70   P4 *.**.** P4 p
1c7f1 6f 69 6e 74 73 20 74 6f 20 61 20 6e 75 6c 20 74  oints to a nul t
1c7f2 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20  erminated UTF-8 
1c7f3 73 74 72 69 6e 67 2e 20 54 68 69 73 20 6f 70 63  string. This opc
1c7f4 6f 64 65 20 69 73 20 74 72 61 6e 73 66 6f 72 6d  ode is transform
1c7f5 65 64 20 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20 4f  ed .** into an O
1c7f6 50 5f 53 74 72 69 6e 67 20 62 65 66 6f 72 65 20  P_String before 
1c7f7 69 74 20 69 73 20 65 78 65 63 75 74 65 64 20 66  it is executed f
1c7f8 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 69 6d  or the first tim
1c7f9 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 74  e..*/.case OP_St
1c7fa 72 69 6e 67 38 3a 20 7b 20 20 20 20 20 20 20 20  ring8: {        
1c7fb 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53   /* same as TK_S
1c7fc 54 52 49 4e 47 2c 20 6f 75 74 32 2d 70 72 65 72  TRING, out2-prer
1c7fd 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65  elease */.  asse
1c7fe 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30  rt( pOp->p4.z!=0
1c7ff 20 29 3b 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f 64   );.  pOp->opcod
1c800 65 20 3d 20 4f 50 5f 53 74 72 69 6e 67 3b 0a 20  e = OP_String;. 
1c801 20 70 4f 70 2d 3e 70 31 20 3d 20 73 71 6c 69 74   pOp->p1 = sqlit
1c802 65 33 53 74 72 6c 65 6e 33 30 28 70 4f 70 2d 3e  e3Strlen30(pOp->
1c803 70 34 2e 7a 29 3b 0a 0a 23 69 66 6e 64 65 66 20  p4.z);..#ifndef 
1c804 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
1c805 36 0a 20 20 69 66 28 20 65 6e 63 6f 64 69 6e 67  6.  if( encoding
1c806 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b  !=SQLITE_UTF8 ){
1c807 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1c808 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70  3VdbeMemSetStr(p
1c809 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20  Out, pOp->p4.z, 
1c80a 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  -1, SQLITE_UTF8,
1c80b 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
1c80c 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
1c80d 49 54 45 5f 54 4f 4f 42 49 47 20 29 20 67 6f 74  ITE_TOOBIG ) got
1c80e 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 69  o too_big;.    i
1c80f 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  f( SQLITE_OK!=sq
1c810 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
1c811 6e 63 6f 64 69 6e 67 28 70 4f 75 74 2c 20 65 6e  ncoding(pOut, en
1c812 63 6f 64 69 6e 67 29 20 29 20 67 6f 74 6f 20 6e  coding) ) goto n
1c813 6f 5f 6d 65 6d 3b 0a 20 20 20 20 61 73 73 65 72  o_mem;.    asser
1c814 74 28 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63  t( pOut->zMalloc
1c815 3d 3d 70 4f 75 74 2d 3e 7a 20 29 3b 0a 20 20 20  ==pOut->z );.   
1c816 20 61 73 73 65 72 74 28 20 70 4f 75 74 2d 3e 66   assert( pOut->f
1c817 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20 29  lags & MEM_Dyn )
1c818 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c  ;.    pOut->zMal
1c819 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 70 4f 75  loc = 0;.    pOu
1c81a 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f  t->flags |= MEM_
1c81b 53 74 61 74 69 63 3b 0a 20 20 20 20 70 4f 75 74  Static;.    pOut
1c81c 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f  ->flags &= ~MEM_
1c81d 44 79 6e 3b 0a 20 20 20 20 69 66 28 20 70 4f 70  Dyn;.    if( pOp
1c81e 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 44 59 4e  ->p4type==P4_DYN
1c81f 41 4d 49 43 20 29 7b 0a 20 20 20 20 20 20 73 71  AMIC ){.      sq
1c820 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1c821 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20  pOp->p4.z);.    
1c822 7d 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70  }.    pOp->p4typ
1c823 65 20 3d 20 50 34 5f 44 59 4e 41 4d 49 43 3b 0a  e = P4_DYNAMIC;.
1c824 20 20 20 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d 20      pOp->p4.z = 
1c825 70 4f 75 74 2d 3e 7a 3b 0a 20 20 20 20 70 4f 70  pOut->z;.    pOp
1c826 2d 3e 70 31 20 3d 20 70 4f 75 74 2d 3e 6e 3b 0a  ->p1 = pOut->n;.
1c827 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28    }.#endif.  if(
1c828 20 70 4f 70 2d 3e 70 31 3e 64 62 2d 3e 61 4c 69   pOp->p1>db->aLi
1c829 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
1c82a 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20  _LENGTH] ){.    
1c82b 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20  goto too_big;.  
1c82c 7d 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f  }.  /* Fall thro
1c82d 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ugh to the next 
1c82e 63 61 73 65 2c 20 4f 50 5f 53 74 72 69 6e 67 20  case, OP_String 
1c82f 2a 2f 0a 7d 0a 20 20 0a 2f 2a 20 4f 70 63 6f 64  */.}.  ./* Opcod
1c830 65 3a 20 53 74 72 69 6e 67 20 50 31 20 50 32 20  e: String P1 P2 
1c831 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65  * P4 *.**.** The
1c832 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 50 34   string value P4
1c833 20 6f 66 20 6c 65 6e 67 74 68 20 50 31 20 28 62   of length P1 (b
1c834 79 74 65 73 29 20 69 73 20 73 74 6f 72 65 64 20  ytes) is stored 
1c835 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a  in register P2..
1c836 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 74 72 69 6e  */.case OP_Strin
1c837 67 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a  g: {          /*
1c838 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65   out2-prerelease
1c839 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f   */.  assert( pO
1c83a 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a 20 20  p->p4.z!=0 );.  
1c83b 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pOut->flags = ME
1c83c 4d 5f 53 74 72 7c 4d 45 4d 5f 53 74 61 74 69 63  M_Str|MEM_Static
1c83d 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f 75  |MEM_Term;.  pOu
1c83e 74 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a  t->z = pOp->p4.z
1c83f 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 70 4f  ;.  pOut->n = pO
1c840 70 2d 3e 70 31 3b 0a 20 20 70 4f 75 74 2d 3e 65  p->p1;.  pOut->e
1c841 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20  nc = encoding;. 
1c842 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42   UPDATE_MAX_BLOB
1c843 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72  SIZE(pOut);.  br
1c844 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1c845 65 3a 20 4e 75 6c 6c 20 2a 20 50 32 20 2a 20 2a  e: Null * P2 * *
1c846 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61   *.**.** Write a
1c847 20 4e 55 4c 4c 20 69 6e 74 6f 20 72 65 67 69 73   NULL into regis
1c848 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20  ter P2..*/.case 
1c849 4f 50 5f 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20  OP_Null: {      
1c84a 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65       /* out2-pre
1c84b 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 62 72 65  release */.  bre
1c84c 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64  ak;.}.../* Opcod
1c84d 65 3a 20 42 6c 6f 62 20 50 31 20 50 32 20 2a 20  e: Blob P1 P2 * 
1c84e 50 34 0a 2a 2a 0a 2a 2a 20 50 34 20 70 6f 69 6e  P4.**.** P4 poin
1c84f 74 73 20 74 6f 20 61 20 62 6c 6f 62 20 6f 66 20  ts to a blob of 
1c850 64 61 74 61 20 50 31 20 62 79 74 65 73 20 6c 6f  data P1 bytes lo
1c851 6e 67 2e 20 20 53 74 6f 72 65 20 74 68 69 73 0a  ng.  Store this.
1c852 2a 2a 20 62 6c 6f 62 20 69 6e 20 72 65 67 69 73  ** blob in regis
1c853 74 65 72 20 50 32 2e 20 54 68 69 73 20 69 6e 73  ter P2. This ins
1c854 74 72 75 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20  truction is not 
1c855 63 6f 64 65 64 20 64 69 72 65 63 74 6c 79 0a 2a  coded directly.*
1c856 2a 20 62 79 20 74 68 65 20 63 6f 6d 70 69 6c 65  * by the compile
1c857 72 2e 20 49 6e 73 74 65 61 64 2c 20 74 68 65 20  r. Instead, the 
1c858 63 6f 6d 70 69 6c 65 72 20 6c 61 79 65 72 20 73  compiler layer s
1c859 70 65 63 69 66 69 65 73 0a 2a 2a 20 61 6e 20 4f  pecifies.** an O
1c85a 50 5f 48 65 78 42 6c 6f 62 20 6f 70 63 6f 64 65  P_HexBlob opcode
1c85b 2c 20 77 69 74 68 20 74 68 65 20 68 65 78 20 73  , with the hex s
1c85c 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61  tring representa
1c85d 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 62  tion of.** the b
1c85e 6c 6f 62 20 61 73 20 50 34 2e 20 54 68 69 73 20  lob as P4. This 
1c85f 6f 70 63 6f 64 65 20 69 73 20 74 72 61 6e 73 66  opcode is transf
1c860 6f 72 6d 65 64 20 74 6f 20 61 6e 20 4f 50 5f 42  ormed to an OP_B
1c861 6c 6f 62 0a 2a 2a 20 74 68 65 20 66 69 72 73 74  lob.** the first
1c862 20 74 69 6d 65 20 69 74 20 69 73 20 65 78 65 63   time it is exec
1c863 75 74 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  uted..*/.case OP
1c864 5f 42 6c 6f 62 3a 20 7b 20 20 20 20 20 20 20 20  _Blob: {        
1c865 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d          /* out2-
1c866 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20  prerelease */.  
1c867 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 20  assert( pOp->p1 
1c868 3c 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45  <= SQLITE_MAX_LE
1c869 4e 47 54 48 20 29 3b 0a 20 20 73 71 6c 69 74 65  NGTH );.  sqlite
1c86a 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70  3VdbeMemSetStr(p
1c86b 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20  Out, pOp->p4.z, 
1c86c 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 30 29 3b 0a  pOp->p1, 0, 0);.
1c86d 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e    pOut->enc = en
1c86e 63 6f 64 69 6e 67 3b 0a 20 20 55 50 44 41 54 45  coding;.  UPDATE
1c86f 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f  _MAX_BLOBSIZE(pO
1c870 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  ut);.  break;.}.
1c871 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61 72 69  ./* Opcode: Vari
1c872 61 62 6c 65 20 50 31 20 50 32 20 50 33 20 50 34  able P1 P2 P3 P4
1c873 20 2a 0a 2a 2a 0a 2a 2a 20 54 72 61 6e 73 66 65   *.**.** Transfe
1c874 72 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20  r the values of 
1c875 62 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 73  bound parameters
1c876 20 50 31 2e 2e 50 31 2b 50 33 2d 31 20 69 6e 74   P1..P1+P3-1 int
1c877 6f 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 50  o registers.** P
1c878 32 2e 2e 50 32 2b 50 33 2d 31 2e 0a 2a 2a 0a 2a  2..P2+P3-1..**.*
1c879 2a 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 74  * If the paramet
1c87a 65 72 20 69 73 20 6e 61 6d 65 64 2c 20 74 68 65  er is named, the
1c87b 6e 20 69 74 73 20 6e 61 6d 65 20 61 70 70 65 61  n its name appea
1c87c 72 73 20 69 6e 20 50 34 20 61 6e 64 20 50 33 3d  rs in P4 and P3=
1c87d 3d 31 2e 0a 2a 2a 20 54 68 65 20 50 34 20 76 61  =1..** The P4 va
1c87e 6c 75 65 20 69 73 20 75 73 65 64 20 62 79 20 73  lue is used by s
1c87f 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61  qlite3_bind_para
1c880 6d 65 74 65 72 5f 6e 61 6d 65 28 29 2e 0a 2a 2f  meter_name()..*/
1c881 0a 63 61 73 65 20 4f 50 5f 56 61 72 69 61 62 6c  .case OP_Variabl
1c882 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c  e: {.#if 0  /* l
1c883 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1c884 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 62 20 2a  oved into u.ab *
1c885 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20  /.  int p1;     
1c886 20 20 20 20 20 2f 2a 20 56 61 72 69 61 62 6c 65       /* Variable
1c887 20 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a 2f   to copy from */
1c888 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20  .  int p2;      
1c889 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
1c88a 74 6f 20 63 6f 70 79 20 74 6f 20 2a 2f 0a 20 20  to copy to */.  
1c88b 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20  int n;          
1c88c 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76 61   /* Number of va
1c88d 6c 75 65 73 20 6c 65 66 74 20 74 6f 20 63 6f 70  lues left to cop
1c88e 79 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 56 61 72  y */.  Mem *pVar
1c88f 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65  ;       /* Value
1c890 20 62 65 69 6e 67 20 74 72 61 6e 73 66 65 72 72   being transferr
1c891 65 64 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20  ed */.#endif /* 
1c892 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1c893 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 62 20  moved into u.ab 
1c894 2a 2f 0a 0a 20 20 75 2e 61 62 2e 70 31 20 3d 20  */..  u.ab.p1 = 
1c895 70 4f 70 2d 3e 70 31 20 2d 20 31 3b 0a 20 20 75  pOp->p1 - 1;.  u
1c896 2e 61 62 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32  .ab.p2 = pOp->p2
1c897 3b 0a 20 20 75 2e 61 62 2e 6e 20 3d 20 70 4f 70  ;.  u.ab.n = pOp
1c898 2d 3e 70 33 3b 0a 20 20 61 73 73 65 72 74 28 20  ->p3;.  assert( 
1c899 75 2e 61 62 2e 70 31 3e 3d 30 20 26 26 20 75 2e  u.ab.p1>=0 && u.
1c89a 61 62 2e 70 31 2b 75 2e 61 62 2e 6e 3c 3d 70 2d  ab.p1+u.ab.n<=p-
1c89b 3e 6e 56 61 72 20 29 3b 0a 20 20 61 73 73 65 72  >nVar );.  asser
1c89c 74 28 20 75 2e 61 62 2e 70 32 3e 3d 31 20 26 26  t( u.ab.p2>=1 &&
1c89d 20 75 2e 61 62 2e 70 32 2b 75 2e 61 62 2e 6e 2d   u.ab.p2+u.ab.n-
1c89e 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  1<=p->nMem );.  
1c89f 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e  assert( pOp->p4.
1c8a0 7a 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 33 3d  z==0 || pOp->p3=
1c8a1 3d 31 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20  =1 );..  while( 
1c8a2 75 2e 61 62 2e 6e 2d 2d 20 3e 20 30 20 29 7b 0a  u.ab.n-- > 0 ){.
1c8a3 20 20 20 20 75 2e 61 62 2e 70 56 61 72 20 3d 20      u.ab.pVar = 
1c8a4 26 70 2d 3e 61 56 61 72 5b 75 2e 61 62 2e 70 31  &p->aVar[u.ab.p1
1c8a5 2b 2b 5d 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  ++];.    if( sql
1c8a6 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69  ite3VdbeMemTooBi
1c8a7 67 28 75 2e 61 62 2e 70 56 61 72 29 20 29 7b 0a  g(u.ab.pVar) ){.
1c8a8 20 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62        goto too_b
1c8a9 69 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f  ig;.    }.    pO
1c8aa 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e  ut = &p->aMem[u.
1c8ab 61 62 2e 70 32 2b 2b 5d 3b 0a 20 20 20 20 73 71  ab.p2++];.    sq
1c8ac 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
1c8ad 61 73 65 45 78 74 65 72 6e 61 6c 28 70 4f 75 74  aseExternal(pOut
1c8ae 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61  );.    pOut->fla
1c8af 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
1c8b0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1c8b1 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75  mShallowCopy(pOu
1c8b2 74 2c 20 75 2e 61 62 2e 70 56 61 72 2c 20 4d 45  t, u.ab.pVar, ME
1c8b3 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 55  M_Static);.    U
1c8b4 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49  PDATE_MAX_BLOBSI
1c8b5 5a 45 28 70 4f 75 74 29 3b 0a 20 20 7d 0a 20 20  ZE(pOut);.  }.  
1c8b6 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1c8b7 6f 64 65 3a 20 4d 6f 76 65 20 50 31 20 50 32 20  ode: Move P1 P2 
1c8b8 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76  P3 * *.**.** Mov
1c8b9 65 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20  e the values in 
1c8ba 72 65 67 69 73 74 65 72 20 50 31 2e 2e 50 31 2b  register P1..P1+
1c8bb 50 33 2d 31 20 6f 76 65 72 20 69 6e 74 6f 0a 2a  P3-1 over into.*
1c8bc 2a 20 72 65 67 69 73 74 65 72 73 20 50 32 2e 2e  * registers P2..
1c8bd 50 32 2b 50 33 2d 31 2e 20 20 52 65 67 69 73 74  P2+P3-1.  Regist
1c8be 65 72 73 20 50 31 2e 2e 50 31 2b 50 31 2d 31 20  ers P1..P1+P1-1 
1c8bf 61 72 65 0a 2a 2a 20 6c 65 66 74 20 68 6f 6c 64  are.** left hold
1c8c0 69 6e 67 20 61 20 4e 55 4c 4c 2e 20 20 49 74 20  ing a NULL.  It 
1c8c1 69 73 20 61 6e 20 65 72 72 6f 72 20 66 6f 72 20  is an error for 
1c8c2 72 65 67 69 73 74 65 72 20 72 61 6e 67 65 73 0a  register ranges.
1c8c3 2a 2a 20 50 31 2e 2e 50 31 2b 50 33 2d 31 20 61  ** P1..P1+P3-1 a
1c8c4 6e 64 20 50 32 2e 2e 50 32 2b 50 33 2d 31 20 74  nd P2..P2+P3-1 t
1c8c5 6f 20 6f 76 65 72 6c 61 70 2e 0a 2a 2f 0a 63 61  o overlap..*/.ca
1c8c6 73 65 20 4f 50 5f 4d 6f 76 65 3a 20 7b 0a 23 69  se OP_Move: {.#i
1c8c7 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1c8c8 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1c8c9 74 6f 20 75 2e 61 63 20 2a 2f 0a 20 20 63 68 61  to u.ac */.  cha
1c8ca 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 20 20 2f 2a  r *zMalloc;   /*
1c8cb 20 48 6f 6c 64 69 6e 67 20 76 61 72 69 61 62 6c   Holding variabl
1c8cc 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74 65 64 20  e for allocated 
1c8cd 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74 20  memory */.  int 
1c8ce 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  n;           /* 
1c8cf 4e 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74  Number of regist
1c8d0 65 72 73 20 6c 65 66 74 20 74 6f 20 63 6f 70 79  ers left to copy
1c8d1 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20 20   */.  int p1;   
1c8d2 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
1c8d3 65 72 20 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20  er to copy from 
1c8d4 2a 2f 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 20  */.  int p2;    
1c8d5 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
1c8d6 72 20 74 6f 20 63 6f 70 79 20 74 6f 20 2a 2f 0a  r to copy to */.
1c8d7 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1c8d8 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1c8d9 69 6e 74 6f 20 75 2e 61 63 20 2a 2f 0a 0a 20 20  into u.ac */..  
1c8da 75 2e 61 63 2e 6e 20 3d 20 70 4f 70 2d 3e 70 33  u.ac.n = pOp->p3
1c8db 3b 0a 20 20 75 2e 61 63 2e 70 31 20 3d 20 70 4f  ;.  u.ac.p1 = pO
1c8dc 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 63 2e 70 32  p->p1;.  u.ac.p2
1c8dd 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 61 73   = pOp->p2;.  as
1c8de 73 65 72 74 28 20 75 2e 61 63 2e 6e 3e 30 20 26  sert( u.ac.n>0 &
1c8df 26 20 75 2e 61 63 2e 70 31 3e 30 20 26 26 20 75  & u.ac.p1>0 && u
1c8e0 2e 61 63 2e 70 32 3e 30 20 29 3b 0a 20 20 61 73  .ac.p2>0 );.  as
1c8e1 73 65 72 74 28 20 75 2e 61 63 2e 70 31 2b 75 2e  sert( u.ac.p1+u.
1c8e2 61 63 2e 6e 3c 3d 75 2e 61 63 2e 70 32 20 7c 7c  ac.n<=u.ac.p2 ||
1c8e3 20 75 2e 61 63 2e 70 32 2b 75 2e 61 63 2e 6e 3c   u.ac.p2+u.ac.n<
1c8e4 3d 75 2e 61 63 2e 70 31 20 29 3b 0a 0a 20 20 70  =u.ac.p1 );..  p
1c8e5 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75  In1 = &p->aMem[u
1c8e6 2e 61 63 2e 70 31 5d 3b 0a 20 20 70 4f 75 74 20  .ac.p1];.  pOut 
1c8e7 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 63 2e  = &p->aMem[u.ac.
1c8e8 70 32 5d 3b 0a 20 20 77 68 69 6c 65 28 20 75 2e  p2];.  while( u.
1c8e9 61 63 2e 6e 2d 2d 20 29 7b 0a 20 20 20 20 61 73  ac.n-- ){.    as
1c8ea 73 65 72 74 28 20 70 4f 75 74 3c 3d 26 70 2d 3e  sert( pOut<=&p->
1c8eb 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b  aMem[p->nMem] );
1c8ec 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e  .    assert( pIn
1c8ed 31 3c 3d 26 70 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e  1<=&p->aMem[p->n
1c8ee 4d 65 6d 5d 20 29 3b 0a 20 20 20 20 75 2e 61 63  Mem] );.    u.ac
1c8ef 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4f 75 74 2d  .zMalloc = pOut-
1c8f0 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 20 20 70 4f  >zMalloc;.    pO
1c8f1 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b  ut->zMalloc = 0;
1c8f2 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1c8f3 4d 65 6d 4d 6f 76 65 28 70 4f 75 74 2c 20 70 49  MemMove(pOut, pI
1c8f4 6e 31 29 3b 0a 20 20 20 20 70 49 6e 31 2d 3e 7a  n1);.    pIn1->z
1c8f5 4d 61 6c 6c 6f 63 20 3d 20 75 2e 61 63 2e 7a 4d  Malloc = u.ac.zM
1c8f6 61 6c 6c 6f 63 3b 0a 20 20 20 20 52 45 47 49 53  alloc;.    REGIS
1c8f7 54 45 52 5f 54 52 41 43 45 28 75 2e 61 63 2e 70  TER_TRACE(u.ac.p
1c8f8 32 2b 2b 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20  2++, pOut);.    
1c8f9 70 49 6e 31 2b 2b 3b 0a 20 20 20 20 70 4f 75 74  pIn1++;.    pOut
1c8fa 2b 2b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  ++;.  }.  break;
1c8fb 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43  .}../* Opcode: C
1c8fc 6f 70 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  opy P1 P2 * * *.
1c8fd 2a 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70  **.** Make a cop
1c8fe 79 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31  y of register P1
1c8ff 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50   into register P
1c900 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  2..**.** This in
1c901 73 74 72 75 63 74 69 6f 6e 20 6d 61 6b 65 73 20  struction makes 
1c902 61 20 64 65 65 70 20 63 6f 70 79 20 6f 66 20 74  a deep copy of t
1c903 68 65 20 76 61 6c 75 65 2e 20 20 41 20 64 75 70  he value.  A dup
1c904 6c 69 63 61 74 65 0a 2a 2a 20 69 73 20 6d 61 64  licate.** is mad
1c905 65 20 6f 66 20 61 6e 79 20 73 74 72 69 6e 67 20  e of any string 
1c906 6f 72 20 62 6c 6f 62 20 63 6f 6e 73 74 61 6e 74  or blob constant
1c907 2e 20 20 53 65 65 20 61 6c 73 6f 20 4f 50 5f 53  .  See also OP_S
1c908 43 6f 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  Copy..*/.case OP
1c909 5f 43 6f 70 79 3a 20 7b 20 20 20 20 20 20 20 20  _Copy: {        
1c90a 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20       /* in1 */. 
1c90b 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32   assert( pOp->p2
1c90c 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
1c90d 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p2<=p->nMem
1c90e 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d   );.  pOut = &p-
1c90f 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a  >aMem[pOp->p2];.
1c910 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 21 3d    assert( pOut!=
1c911 70 49 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74 65  pIn1 );.  sqlite
1c912 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43  3VdbeMemShallowC
1c913 6f 70 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20  opy(pOut, pIn1, 
1c914 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 44 65  MEM_Ephem);.  De
1c915 65 70 68 65 6d 65 72 61 6c 69 7a 65 28 70 4f 75  ephemeralize(pOu
1c916 74 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54  t);.  REGISTER_T
1c917 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f  RACE(pOp->p2, pO
1c918 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  ut);.  break;.}.
1c919 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 43 6f 70  ./* Opcode: SCop
1c91a 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  y P1 P2 * * *.**
1c91b 0a 2a 2a 20 4d 61 6b 65 20 61 20 73 68 61 6c 6c  .** Make a shall
1c91c 6f 77 20 63 6f 70 79 20 6f 66 20 72 65 67 69 73  ow copy of regis
1c91d 74 65 72 20 50 31 20 69 6e 74 6f 20 72 65 67 69  ter P1 into regi
1c91e 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54  ster P2..**.** T
1c91f 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1c920 6d 61 6b 65 73 20 61 20 73 68 61 6c 6c 6f 77 20  makes a shallow 
1c921 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c 75  copy of the valu
1c922 65 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75 65  e.  If the value
1c923 0a 2a 2a 20 69 73 20 61 20 73 74 72 69 6e 67 20  .** is a string 
1c924 6f 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 74 68  or blob, then th
1c925 65 20 63 6f 70 79 20 69 73 20 6f 6e 6c 79 20 61  e copy is only a
1c926 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a   pointer to the.
1c927 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 61 6e 64 20  ** original and 
1c928 68 65 6e 63 65 20 69 66 20 74 68 65 20 6f 72 69  hence if the ori
1c929 67 69 6e 61 6c 20 63 68 61 6e 67 65 73 20 73 6f  ginal changes so
1c92a 20 77 69 6c 6c 20 74 68 65 20 63 6f 70 79 2e 0a   will the copy..
1c92b 2a 2a 20 57 6f 72 73 65 2c 20 69 66 20 74 68 65  ** Worse, if the
1c92c 20 6f 72 69 67 69 6e 61 6c 20 69 73 20 64 65 61   original is dea
1c92d 6c 6c 6f 63 61 74 65 64 2c 20 74 68 65 20 63 6f  llocated, the co
1c92e 70 79 20 62 65 63 6f 6d 65 73 20 69 6e 76 61 6c  py becomes inval
1c92f 69 64 2e 0a 2a 2a 20 54 68 75 73 20 74 68 65 20  id..** Thus the 
1c930 70 72 6f 67 72 61 6d 20 6d 75 73 74 20 67 75 61  program must gua
1c931 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20  rantee that the 
1c932 6f 72 69 67 69 6e 61 6c 20 77 69 6c 6c 20 6e 6f  original will no
1c933 74 20 63 68 61 6e 67 65 0a 2a 2a 20 64 75 72 69  t change.** duri
1c934 6e 67 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20  ng the lifetime 
1c935 6f 66 20 74 68 65 20 63 6f 70 79 2e 20 20 55 73  of the copy.  Us
1c936 65 20 4f 50 5f 43 6f 70 79 20 74 6f 20 6d 61 6b  e OP_Copy to mak
1c937 65 20 61 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20  e a complete.** 
1c938 63 6f 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  copy..*/.case OP
1c939 5f 53 43 6f 70 79 3a 20 7b 20 20 20 20 20 20 20  _SCopy: {       
1c93a 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20       /* in1 */. 
1c93b 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
1c93c 70 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a  pOp->p1, pIn1);.
1c93d 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1c93e 32 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  2>0 );.  assert(
1c93f 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65   pOp->p2<=p->nMe
1c940 6d 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70  m );.  pOut = &p
1c941 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b  ->aMem[pOp->p2];
1c942 0a 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 21  .  assert( pOut!
1c943 3d 70 49 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74  =pIn1 );.  sqlit
1c944 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77  e3VdbeMemShallow
1c945 43 6f 70 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c  Copy(pOut, pIn1,
1c946 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 52   MEM_Ephem);.  R
1c947 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
1c948 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20  p->p2, pOut);.  
1c949 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1c94a 6f 64 65 3a 20 52 65 73 75 6c 74 52 6f 77 20 50  ode: ResultRow P
1c94b 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1c94c 20 54 68 65 20 72 65 67 69 73 74 65 72 73 20 50   The registers P
1c94d 31 20 74 68 72 6f 75 67 68 20 50 31 2b 50 32 2d  1 through P1+P2-
1c94e 31 20 63 6f 6e 74 61 69 6e 20 61 20 73 69 6e 67  1 contain a sing
1c94f 6c 65 20 72 6f 77 20 6f 66 0a 2a 2a 20 72 65 73  le row of.** res
1c950 75 6c 74 73 2e 20 54 68 69 73 20 6f 70 63 6f 64  ults. This opcod
1c951 65 20 63 61 75 73 65 73 20 74 68 65 20 73 71 6c  e causes the sql
1c952 69 74 65 33 5f 73 74 65 70 28 29 20 63 61 6c 6c  ite3_step() call
1c953 20 74 6f 20 74 65 72 6d 69 6e 61 74 65 0a 2a 2a   to terminate.**
1c954 20 77 69 74 68 20 61 6e 20 53 51 4c 49 54 45 5f   with an SQLITE_
1c955 52 4f 57 20 72 65 74 75 72 6e 20 63 6f 64 65 20  ROW return code 
1c956 61 6e 64 20 69 74 20 73 65 74 73 20 75 70 20 74  and it sets up t
1c957 68 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 0a  he sqlite3_stmt.
1c958 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f 20  ** structure to 
1c959 70 72 6f 76 69 64 65 20 61 63 63 65 73 73 20 74  provide access t
1c95a 6f 20 74 68 65 20 74 6f 70 20 50 31 20 76 61 6c  o the top P1 val
1c95b 75 65 73 20 61 73 20 74 68 65 20 72 65 73 75 6c  ues as the resul
1c95c 74 0a 2a 2a 20 72 6f 77 2e 0a 2a 2f 0a 63 61 73  t.** row..*/.cas
1c95d 65 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 3a 20  e OP_ResultRow: 
1c95e 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  {.#if 0  /* loca
1c95f 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1c960 64 20 69 6e 74 6f 20 75 2e 61 64 20 2a 2f 0a 20  d into u.ad */. 
1c961 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e   Mem *pMem;.  in
1c962 74 20 69 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  t i;.#endif /* l
1c963 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1c964 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 64 20 2a  oved into u.ad *
1c965 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e  /.  assert( p->n
1c966 52 65 73 43 6f 6c 75 6d 6e 3d 3d 70 4f 70 2d 3e  ResColumn==pOp->
1c967 70 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  p2 );.  assert( 
1c968 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 61  pOp->p1>0 );.  a
1c969 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 2b 70  ssert( pOp->p1+p
1c96a 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 2b  Op->p2<=p->nMem+
1c96b 31 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  1 );..  /* If th
1c96c 65 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f  e SQLITE_CountRo
1c96d 77 73 20 66 6c 61 67 20 69 73 20 73 65 74 20 69  ws flag is set i
1c96e 6e 20 73 71 6c 69 74 65 33 2e 66 6c 61 67 73 20  n sqlite3.flags 
1c96f 6d 61 73 6b 2c 20 74 68 65 6e 0a 20 20 2a 2a 20  mask, then.  ** 
1c970 44 4d 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69  DML statements i
1c971 6e 76 6f 6b 65 20 74 68 69 73 20 6f 70 63 6f 64  nvoke this opcod
1c972 65 20 74 6f 20 72 65 74 75 72 6e 20 74 68 65 20  e to return the 
1c973 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 0a 20  number of rows. 
1c974 20 2a 2a 20 6d 6f 64 69 66 69 65 64 20 74 6f 20   ** modified to 
1c975 74 68 65 20 75 73 65 72 2e 20 54 68 69 73 20 69  the user. This i
1c976 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 74  s the only way t
1c977 68 61 74 20 61 20 56 4d 20 74 68 61 74 0a 20 20  hat a VM that.  
1c978 2a 2a 20 6f 70 65 6e 73 20 61 20 73 74 61 74 65  ** opens a state
1c979 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
1c97a 20 6d 61 79 20 69 6e 76 6f 6b 65 20 74 68 69 73   may invoke this
1c97b 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2a 0a 20 20   opcode..  **.  
1c97c 2a 2a 20 49 6e 20 63 61 73 65 20 74 68 69 73 20  ** In case this 
1c97d 69 73 20 73 75 63 68 20 61 20 73 74 61 74 65 6d  is such a statem
1c97e 65 6e 74 2c 20 63 6c 6f 73 65 20 61 6e 79 20 73  ent, close any s
1c97f 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63  tatement transac
1c980 74 69 6f 6e 0a 20 20 2a 2a 20 6f 70 65 6e 65 64  tion.  ** opened
1c981 20 62 79 20 74 68 69 73 20 56 4d 20 62 65 66 6f   by this VM befo
1c982 72 65 20 72 65 74 75 72 6e 69 6e 67 20 63 6f 6e  re returning con
1c983 74 72 6f 6c 20 74 6f 20 74 68 65 20 75 73 65 72  trol to the user
1c984 2e 20 54 68 69 73 20 69 73 20 74 6f 0a 20 20 2a  . This is to.  *
1c985 2a 20 65 6e 73 75 72 65 20 74 68 61 74 20 73 74  * ensure that st
1c986 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74  atement-transact
1c987 69 6f 6e 73 20 61 72 65 20 61 6c 77 61 79 73 20  ions are always 
1c988 6e 65 73 74 65 64 2c 20 6e 6f 74 20 6f 76 65 72  nested, not over
1c989 6c 61 70 70 69 6e 67 2e 0a 20 20 2a 2a 20 49 66  lapping..  ** If
1c98a 20 74 68 65 20 6f 70 65 6e 20 73 74 61 74 65 6d   the open statem
1c98b 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent-transaction 
1c98c 69 73 20 6e 6f 74 20 63 6c 6f 73 65 64 20 68 65  is not closed he
1c98d 72 65 2c 20 74 68 65 6e 20 74 68 65 20 75 73 65  re, then the use
1c98e 72 0a 20 20 2a 2a 20 6d 61 79 20 73 74 65 70 20  r.  ** may step 
1c98f 61 6e 6f 74 68 65 72 20 56 4d 20 74 68 61 74 20  another VM that 
1c990 6f 70 65 6e 73 20 69 74 73 20 6f 77 6e 20 73 74  opens its own st
1c991 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74  atement transact
1c992 69 6f 6e 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6d  ion. This.  ** m
1c993 61 79 20 6c 65 61 64 20 74 6f 20 6f 76 65 72 6c  ay lead to overl
1c994 61 70 70 69 6e 67 20 73 74 61 74 65 6d 65 6e 74  apping statement
1c995 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 20   transactions.. 
1c996 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 74 61   **.  ** The sta
1c997 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
1c998 6f 6e 20 69 73 20 6e 65 76 65 72 20 61 20 74 6f  on is never a to
1c999 70 2d 6c 65 76 65 6c 20 74 72 61 6e 73 61 63 74  p-level transact
1c99a 69 6f 6e 2e 20 20 48 65 6e 63 65 0a 20 20 2a 2a  ion.  Hence.  **
1c99b 20 74 68 65 20 52 45 4c 45 41 53 45 20 63 61 6c   the RELEASE cal
1c99c 6c 20 62 65 6c 6f 77 20 63 61 6e 20 6e 65 76 65  l below can neve
1c99d 72 20 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20 61  r fail..  */.  a
1c99e 73 73 65 72 74 28 20 70 2d 3e 69 53 74 61 74 65  ssert( p->iState
1c99f 6d 65 6e 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 66  ment==0 || db->f
1c9a0 6c 61 67 73 26 53 51 4c 49 54 45 5f 43 6f 75 6e  lags&SQLITE_Coun
1c9a1 74 52 6f 77 73 20 29 3b 0a 20 20 72 63 20 3d 20  tRows );.  rc = 
1c9a2 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65  sqlite3VdbeClose
1c9a3 53 74 61 74 65 6d 65 6e 74 28 70 2c 20 53 41 56  Statement(p, SAV
1c9a4 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29 3b  EPOINT_RELEASE);
1c9a5 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 21  .  if( NEVER(rc!
1c9a6 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20  =SQLITE_OK) ){. 
1c9a7 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20     break;.  }.. 
1c9a8 20 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61   /* Invalidate a
1c9a9 6c 6c 20 65 70 68 65 6d 65 72 61 6c 20 63 75 72  ll ephemeral cur
1c9aa 73 6f 72 20 72 6f 77 20 63 61 63 68 65 73 20 2a  sor row caches *
1c9ab 2f 0a 20 20 70 2d 3e 63 61 63 68 65 43 74 72 20  /.  p->cacheCtr 
1c9ac 3d 20 28 70 2d 3e 63 61 63 68 65 43 74 72 20 2b  = (p->cacheCtr +
1c9ad 20 32 29 7c 31 3b 0a 0a 20 20 2f 2a 20 4d 61 6b   2)|1;..  /* Mak
1c9ae 65 20 73 75 72 65 20 74 68 65 20 72 65 73 75 6c  e sure the resul
1c9af 74 73 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  ts of the curren
1c9b0 74 20 72 6f 77 20 61 72 65 20 5c 30 30 30 20 74  t row are \000 t
1c9b1 65 72 6d 69 6e 61 74 65 64 0a 20 20 2a 2a 20 61  erminated.  ** a
1c9b2 6e 64 20 68 61 76 65 20 61 6e 20 61 73 73 69 67  nd have an assig
1c9b3 6e 65 64 20 74 79 70 65 2e 20 20 54 68 65 20 72  ned type.  The r
1c9b4 65 73 75 6c 74 73 20 61 72 65 20 64 65 2d 65 70  esults are de-ep
1c9b5 68 65 6d 65 72 61 6c 69 7a 65 64 20 61 73 0a 20  hemeralized as. 
1c9b6 20 2a 2a 20 61 73 20 73 69 64 65 20 65 66 66 65   ** as side effe
1c9b7 63 74 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 64 2e  ct..  */.  u.ad.
1c9b8 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75 6c  pMem = p->pResul
1c9b9 74 53 65 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  tSet = &p->aMem[
1c9ba 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28  pOp->p1];.  for(
1c9bb 75 2e 61 64 2e 69 3d 30 3b 20 75 2e 61 64 2e 69  u.ad.i=0; u.ad.i
1c9bc 3c 70 4f 70 2d 3e 70 32 3b 20 75 2e 61 64 2e 69  <pOp->p2; u.ad.i
1c9bd 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ++){.    sqlite3
1c9be 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e  VdbeMemNulTermin
1c9bf 61 74 65 28 26 75 2e 61 64 2e 70 4d 65 6d 5b 75  ate(&u.ad.pMem[u
1c9c0 2e 61 64 2e 69 5d 29 3b 0a 20 20 20 20 73 74 6f  .ad.i]);.    sto
1c9c1 72 65 54 79 70 65 49 6e 66 6f 28 26 75 2e 61 64  reTypeInfo(&u.ad
1c9c2 2e 70 4d 65 6d 5b 75 2e 61 64 2e 69 5d 2c 20 65  .pMem[u.ad.i], e
1c9c3 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 52 45  ncoding);.    RE
1c9c4 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70  GISTER_TRACE(pOp
1c9c5 2d 3e 70 31 2b 75 2e 61 64 2e 69 2c 20 26 75 2e  ->p1+u.ad.i, &u.
1c9c6 61 64 2e 70 4d 65 6d 5b 75 2e 61 64 2e 69 5d 29  ad.pMem[u.ad.i])
1c9c7 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e  ;.  }.  if( db->
1c9c8 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67  mallocFailed ) g
1c9c9 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 0a 20 20 2f  oto no_mem;..  /
1c9ca 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  * Return SQLITE_
1c9cb 52 4f 57 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 63  ROW.  */.  p->pc
1c9cc 20 3d 20 70 63 20 2b 20 31 3b 0a 20 20 72 63 20   = pc + 1;.  rc 
1c9cd 3d 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 20 20  = SQLITE_ROW;.  
1c9ce 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e  goto vdbe_return
1c9cf 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1c9d0 43 6f 6e 63 61 74 20 50 31 20 50 32 20 50 33 20  Concat P1 P2 P3 
1c9d1 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 64 20 74 68  * *.**.** Add th
1c9d2 65 20 74 65 78 74 20 69 6e 20 72 65 67 69 73 74  e text in regist
1c9d3 65 72 20 50 31 20 6f 6e 74 6f 20 74 68 65 20 65  er P1 onto the e
1c9d4 6e 64 20 6f 66 20 74 68 65 20 74 65 78 74 20 69  nd of the text i
1c9d5 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 32  n.** register P2
1c9d6 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72   and store the r
1c9d7 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65  esult in registe
1c9d8 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68  r P3..** If eith
1c9d9 65 72 20 74 68 65 20 50 31 20 6f 72 20 50 32 20  er the P1 or P2 
1c9da 74 65 78 74 20 61 72 65 20 4e 55 4c 4c 20 74 68  text are NULL th
1c9db 65 6e 20 73 74 6f 72 65 20 4e 55 4c 4c 20 69 6e  en store NULL in
1c9dc 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 33 20   P3..**.**   P3 
1c9dd 3d 20 50 32 20 7c 7c 20 50 31 0a 2a 2a 0a 2a 2a  = P2 || P1.**.**
1c9de 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 66   It is illegal f
1c9df 6f 72 20 50 31 20 61 6e 64 20 50 33 20 74 6f 20  or P1 and P3 to 
1c9e0 62 65 20 74 68 65 20 73 61 6d 65 20 72 65 67 69  be the same regi
1c9e1 73 74 65 72 2e 20 53 6f 6d 65 74 69 6d 65 73 2c  ster. Sometimes,
1c9e2 0a 2a 2a 20 69 66 20 50 33 20 69 73 20 74 68 65  .** if P3 is the
1c9e3 20 73 61 6d 65 20 72 65 67 69 73 74 65 72 20 61   same register a
1c9e4 73 20 50 32 2c 20 74 68 65 20 69 6d 70 6c 65 6d  s P2, the implem
1c9e5 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 62 6c 65  entation is able
1c9e6 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 20 61 20 6d  .** to avoid a m
1c9e7 65 6d 63 70 79 28 29 2e 0a 2a 2f 0a 63 61 73 65  emcpy()..*/.case
1c9e8 20 4f 50 5f 43 6f 6e 63 61 74 3a 20 7b 20 20 20   OP_Concat: {   
1c9e9 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
1c9ea 61 73 20 54 4b 5f 43 4f 4e 43 41 54 2c 20 69 6e  as TK_CONCAT, in
1c9eb 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a  1, in2, out3 */.
1c9ec 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1c9ed 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1c9ee 69 6e 74 6f 20 75 2e 61 65 20 2a 2f 0a 20 20 69  into u.ae */.  i
1c9ef 36 34 20 6e 42 79 74 65 3b 0a 23 65 6e 64 69 66  64 nByte;.#endif
1c9f0 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1c9f1 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1c9f2 2e 61 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  .ae */..  assert
1c9f3 28 20 70 49 6e 31 21 3d 70 4f 75 74 20 29 3b 0a  ( pIn1!=pOut );.
1c9f4 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61    if( (pIn1->fla
1c9f5 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73  gs | pIn2->flags
1c9f6 29 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a  ) & MEM_Null ){.
1c9f7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1c9f8 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b  emSetNull(pOut);
1c9f9 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  .    break;.  }.
1c9fa 20 20 69 66 28 20 45 78 70 61 6e 64 42 6c 6f 62    if( ExpandBlob
1c9fb 28 70 49 6e 31 29 20 7c 7c 20 45 78 70 61 6e 64  (pIn1) || Expand
1c9fc 42 6c 6f 62 28 70 49 6e 32 29 20 29 20 67 6f 74  Blob(pIn2) ) got
1c9fd 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 53 74 72 69  o no_mem;.  Stri
1c9fe 6e 67 69 66 79 28 70 49 6e 31 2c 20 65 6e 63 6f  ngify(pIn1, enco
1c9ff 64 69 6e 67 29 3b 0a 20 20 53 74 72 69 6e 67 69  ding);.  Stringi
1ca00 66 79 28 70 49 6e 32 2c 20 65 6e 63 6f 64 69 6e  fy(pIn2, encodin
1ca01 67 29 3b 0a 20 20 75 2e 61 65 2e 6e 42 79 74 65  g);.  u.ae.nByte
1ca02 20 3d 20 70 49 6e 31 2d 3e 6e 20 2b 20 70 49 6e   = pIn1->n + pIn
1ca03 32 2d 3e 6e 3b 0a 20 20 69 66 28 20 75 2e 61 65  2->n;.  if( u.ae
1ca04 2e 6e 42 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69  .nByte>db->aLimi
1ca05 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
1ca06 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f  ENGTH] ){.    go
1ca07 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a  to too_big;.  }.
1ca08 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1ca09 28 70 4f 75 74 2c 20 4d 45 4d 5f 53 74 72 29 3b  (pOut, MEM_Str);
1ca0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64  .  if( sqlite3Vd
1ca0b 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20  beMemGrow(pOut, 
1ca0c 28 69 6e 74 29 75 2e 61 65 2e 6e 42 79 74 65 2b  (int)u.ae.nByte+
1ca0d 32 2c 20 70 4f 75 74 3d 3d 70 49 6e 32 29 20 29  2, pOut==pIn2) )
1ca0e 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65  {.    goto no_me
1ca0f 6d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 75  m;.  }.  if( pOu
1ca10 74 21 3d 70 49 6e 32 20 29 7b 0a 20 20 20 20 6d  t!=pIn2 ){.    m
1ca11 65 6d 63 70 79 28 70 4f 75 74 2d 3e 7a 2c 20 70  emcpy(pOut->z, p
1ca12 49 6e 32 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 6e 29  In2->z, pIn2->n)
1ca13 3b 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 26  ;.  }.  memcpy(&
1ca14 70 4f 75 74 2d 3e 7a 5b 70 49 6e 32 2d 3e 6e 5d  pOut->z[pIn2->n]
1ca15 2c 20 70 49 6e 31 2d 3e 7a 2c 20 70 49 6e 31 2d  , pIn1->z, pIn1-
1ca16 3e 6e 29 3b 0a 20 20 70 4f 75 74 2d 3e 7a 5b 75  >n);.  pOut->z[u
1ca17 2e 61 65 2e 6e 42 79 74 65 5d 20 3d 20 30 3b 0a  .ae.nByte] = 0;.
1ca18 20 20 70 4f 75 74 2d 3e 7a 5b 75 2e 61 65 2e 6e    pOut->z[u.ae.n
1ca19 42 79 74 65 2b 31 5d 20 3d 20 30 3b 0a 20 20 70  Byte+1] = 0;.  p
1ca1a 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  Out->flags |= ME
1ca1b 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f 75 74 2d 3e  M_Term;.  pOut->
1ca1c 6e 20 3d 20 28 69 6e 74 29 75 2e 61 65 2e 6e 42  n = (int)u.ae.nB
1ca1d 79 74 65 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e 63  yte;.  pOut->enc
1ca1e 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 55   = encoding;.  U
1ca1f 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49  PDATE_MAX_BLOBSI
1ca20 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61  ZE(pOut);.  brea
1ca21 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1ca22 20 41 64 64 20 50 31 20 50 32 20 50 33 20 2a 20   Add P1 P2 P3 * 
1ca23 2a 0a 2a 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20  *.**.** Add the 
1ca24 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
1ca25 72 20 50 31 20 74 6f 20 74 68 65 20 76 61 6c 75  r P1 to the valu
1ca26 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32  e in register P2
1ca27 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68  .** and store th
1ca28 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69  e result in regi
1ca29 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65  ster P3..** If e
1ca2a 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e  ither input is N
1ca2b 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20  ULL, the result 
1ca2c 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f  is NULL..*/./* O
1ca2d 70 63 6f 64 65 3a 20 4d 75 6c 74 69 70 6c 79 20  pcode: Multiply 
1ca2e 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
1ca2f 2a 2a 0a 2a 2a 20 4d 75 6c 74 69 70 6c 79 20 74  **.** Multiply t
1ca30 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1ca31 73 74 65 72 20 50 31 20 62 79 20 74 68 65 20 76  ster P1 by the v
1ca32 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1ca33 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65   P2.** and store
1ca34 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72   the result in r
1ca35 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49  egister P3..** I
1ca36 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69  f either input i
1ca37 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75  s NULL, the resu
1ca38 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f  lt is NULL..*/./
1ca39 2a 20 4f 70 63 6f 64 65 3a 20 53 75 62 74 72 61  * Opcode: Subtra
1ca3a 63 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  ct P1 P2 P3 * *.
1ca3b 2a 2a 0a 2a 2a 20 53 75 62 74 72 61 63 74 20 74  **.** Subtract t
1ca3c 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1ca3d 73 74 65 72 20 50 31 20 66 72 6f 6d 20 74 68 65  ster P1 from the
1ca3e 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1ca3f 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f  er P2.** and sto
1ca40 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e  re the result in
1ca41 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a   register P3..**
1ca42 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74   If either input
1ca43 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65   is NULL, the re
1ca44 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f  sult is NULL..*/
1ca45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 69 76 69  ./* Opcode: Divi
1ca46 64 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  de P1 P2 P3 * *.
1ca47 2a 2a 0a 2a 2a 20 44 69 76 69 64 65 20 74 68 65  **.** Divide the
1ca48 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1ca49 65 72 20 50 31 20 62 79 20 74 68 65 20 76 61 6c  er P1 by the val
1ca4a 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1ca4b 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74  2.** and store t
1ca4c 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67  he result in reg
1ca4d 69 73 74 65 72 20 50 33 20 28 50 33 3d 50 32 2f  ister P3 (P3=P2/
1ca4e 50 31 29 2e 20 49 66 20 74 68 65 20 76 61 6c 75  P1). If the valu
1ca4f 65 20 69 6e 20 0a 2a 2a 20 72 65 67 69 73 74 65  e in .** registe
1ca50 72 20 50 31 20 69 73 20 7a 65 72 6f 2c 20 74 68  r P1 is zero, th
1ca51 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  en the result is
1ca52 20 4e 55 4c 4c 2e 20 49 66 20 65 69 74 68 65 72   NULL. If either
1ca53 20 69 6e 70 75 74 20 69 73 20 0a 2a 2a 20 4e 55   input is .** NU
1ca54 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69  LL, the result i
1ca55 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70  s NULL..*/./* Op
1ca56 63 6f 64 65 3a 20 52 65 6d 61 69 6e 64 65 72 20  code: Remainder 
1ca57 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
1ca58 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 72  ** Compute the r
1ca59 65 6d 61 69 6e 64 65 72 20 61 66 74 65 72 20 69  emainder after i
1ca5a 6e 74 65 67 65 72 20 64 69 76 69 73 69 6f 6e 20  nteger division 
1ca5b 6f 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 0a  of the value in.
1ca5c 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31 20 62  ** register P1 b
1ca5d 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  y the value in r
1ca5e 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 20 73  egister P2 and s
1ca5f 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
1ca60 69 6e 20 50 33 2e 20 0a 2a 2a 20 49 66 20 74 68  in P3. .** If th
1ca61 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1ca62 74 65 72 20 50 32 20 69 73 20 7a 65 72 6f 20 74  ter P2 is zero t
1ca63 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c  he result is NUL
1ca64 4c 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20  L..** If either 
1ca65 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2c  operand is NULL,
1ca66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e   the result is N
1ca67 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ULL..*/.case OP_
1ca68 41 64 64 3a 20 20 20 20 20 20 20 20 20 20 20 20  Add:            
1ca69 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1ca6a 73 20 54 4b 5f 50 4c 55 53 2c 20 69 6e 31 2c 20  s TK_PLUS, in1, 
1ca6b 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73  in2, out3 */.cas
1ca6c 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a 20 20  e OP_Subtract:  
1ca6d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
1ca6e 61 6d 65 20 61 73 20 54 4b 5f 4d 49 4e 55 53 2c  ame as TK_MINUS,
1ca6f 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20   in1, in2, out3 
1ca70 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69  */.case OP_Multi
1ca71 70 6c 79 3a 20 20 20 20 20 20 20 20 20 20 20 20  ply:            
1ca72 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1ca73 53 54 41 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20  STAR, in1, in2, 
1ca74 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f  out3 */.case OP_
1ca75 44 69 76 69 64 65 3a 20 20 20 20 20 20 20 20 20  Divide:         
1ca76 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1ca77 73 20 54 4b 5f 53 4c 41 53 48 2c 20 69 6e 31 2c  s TK_SLASH, in1,
1ca78 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61   in2, out3 */.ca
1ca79 73 65 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72 3a  se OP_Remainder:
1ca7a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20   {           /* 
1ca7b 73 61 6d 65 20 61 73 20 54 4b 5f 52 45 4d 2c 20  same as TK_REM, 
1ca7c 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a  in1, in2, out3 *
1ca7d 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  /.#if 0  /* loca
1ca7e 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1ca7f 64 20 69 6e 74 6f 20 75 2e 61 66 20 2a 2f 0a 20  d into u.af */. 
1ca80 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20   int flags;     
1ca81 20 2f 2a 20 43 6f 6d 62 69 6e 65 64 20 4d 45 4d   /* Combined MEM
1ca82 5f 2a 20 66 6c 61 67 73 20 66 72 6f 6d 20 62 6f  _* flags from bo
1ca83 74 68 20 69 6e 70 75 74 73 20 2a 2f 0a 20 20 69  th inputs */.  i
1ca84 36 34 20 69 41 3b 20 20 20 20 20 20 20 20 20 2f  64 iA;         /
1ca85 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20  * Integer value 
1ca86 6f 66 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20  of left operand 
1ca87 2a 2f 0a 20 20 69 36 34 20 69 42 3b 20 20 20 20  */.  i64 iB;    
1ca88 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20       /* Integer 
1ca89 76 61 6c 75 65 20 6f 66 20 72 69 67 68 74 20 6f  value of right o
1ca8a 70 65 72 61 6e 64 20 2a 2f 0a 20 20 64 6f 75 62  perand */.  doub
1ca8b 6c 65 20 72 41 3b 20 20 20 20 20 20 2f 2a 20 52  le rA;      /* R
1ca8c 65 61 6c 20 76 61 6c 75 65 20 6f 66 20 6c 65 66  eal value of lef
1ca8d 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 64  t operand */.  d
1ca8e 6f 75 62 6c 65 20 72 42 3b 20 20 20 20 20 20 2f  ouble rB;      /
1ca8f 2a 20 52 65 61 6c 20 76 61 6c 75 65 20 6f 66 20  * Real value of 
1ca90 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f  right operand */
1ca91 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1ca92 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1ca93 20 69 6e 74 6f 20 75 2e 61 66 20 2a 2f 0a 0a 20   into u.af */.. 
1ca94 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66   applyNumericAff
1ca95 69 6e 69 74 79 28 70 49 6e 31 29 3b 0a 20 20 61  inity(pIn1);.  a
1ca96 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e  pplyNumericAffin
1ca97 69 74 79 28 70 49 6e 32 29 3b 0a 20 20 75 2e 61  ity(pIn2);.  u.a
1ca98 66 2e 66 6c 61 67 73 20 3d 20 70 49 6e 31 2d 3e  f.flags = pIn1->
1ca99 66 6c 61 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c  flags | pIn2->fl
1ca9a 61 67 73 3b 0a 20 20 69 66 28 20 28 75 2e 61 66  ags;.  if( (u.af
1ca9b 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  .flags & MEM_Nul
1ca9c 6c 29 21 3d 30 20 29 20 67 6f 74 6f 20 61 72 69  l)!=0 ) goto ari
1ca9d 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69  thmetic_result_i
1ca9e 73 5f 6e 75 6c 6c 3b 0a 20 20 69 66 28 20 28 70  s_null;.  if( (p
1ca9f 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 70 49 6e  In1->flags & pIn
1caa0 32 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  2->flags & MEM_I
1caa1 6e 74 29 3d 3d 4d 45 4d 5f 49 6e 74 20 29 7b 0a  nt)==MEM_Int ){.
1caa2 20 20 20 20 75 2e 61 66 2e 69 41 20 3d 20 70 49      u.af.iA = pI
1caa3 6e 31 2d 3e 75 2e 69 3b 0a 20 20 20 20 75 2e 61  n1->u.i;.    u.a
1caa4 66 2e 69 42 20 3d 20 70 49 6e 32 2d 3e 75 2e 69  f.iB = pIn2->u.i
1caa5 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 4f  ;.    switch( pO
1caa6 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20  p->opcode ){.   
1caa7 20 20 20 63 61 73 65 20 4f 50 5f 41 64 64 3a 20     case OP_Add: 
1caa8 20 20 20 20 20 20 20 20 75 2e 61 66 2e 69 42 20          u.af.iB 
1caa9 2b 3d 20 75 2e 61 66 2e 69 41 3b 20 20 20 20 20  += u.af.iA;     
1caaa 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
1caab 61 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a  ase OP_Subtract:
1caac 20 20 20 20 75 2e 61 66 2e 69 42 20 2d 3d 20 75      u.af.iB -= u
1caad 2e 61 66 2e 69 41 3b 20 20 20 20 20 20 20 62 72  .af.iA;       br
1caae 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
1caaf 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20  OP_Multiply:    
1cab0 75 2e 61 66 2e 69 42 20 2a 3d 20 75 2e 61 66 2e  u.af.iB *= u.af.
1cab1 69 41 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b  iA;       break;
1cab2 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 44  .      case OP_D
1cab3 69 76 69 64 65 3a 20 7b 0a 20 20 20 20 20 20 20  ivide: {.       
1cab4 20 69 66 28 20 75 2e 61 66 2e 69 41 3d 3d 30 20   if( u.af.iA==0 
1cab5 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69  ) goto arithmeti
1cab6 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c  c_result_is_null
1cab7 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 44 69 76  ;.        /* Div
1cab8 69 64 69 6e 67 20 74 68 65 20 6c 61 72 67 65 73  iding the larges
1cab9 74 20 70 6f 73 73 69 62 6c 65 20 6e 65 67 61 74  t possible negat
1caba 69 76 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67  ive 64-bit integ
1cabb 65 72 20 28 31 3c 3c 36 33 29 20 62 79 0a 20 20  er (1<<63) by.  
1cabc 20 20 20 20 20 20 2a 2a 20 2d 31 20 72 65 74 75        ** -1 retu
1cabd 72 6e 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74  rns an integer t
1cabe 6f 6f 20 6c 61 72 67 65 20 74 6f 20 73 74 6f 72  oo large to stor
1cabf 65 20 69 6e 20 61 20 36 34 2d 62 69 74 20 64 61  e in a 64-bit da
1cac0 74 61 2d 74 79 70 65 2e 20 4f 6e 0a 20 20 20 20  ta-type. On.    
1cac1 20 20 20 20 2a 2a 20 73 6f 6d 65 20 61 72 63 68      ** some arch
1cac2 69 74 65 63 74 75 72 65 73 2c 20 74 68 65 20 76  itectures, the v
1cac3 61 6c 75 65 20 6f 76 65 72 66 6c 6f 77 73 20 74  alue overflows t
1cac4 6f 20 28 31 3c 3c 36 33 29 2e 20 4f 6e 20 6f 74  o (1<<63). On ot
1cac5 68 65 72 73 2c 0a 20 20 20 20 20 20 20 20 2a 2a  hers,.        **
1cac6 20 61 20 53 49 47 46 50 45 20 69 73 20 69 73 73   a SIGFPE is iss
1cac7 75 65 64 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ued. The followi
1cac8 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 6e 6f 72  ng statement nor
1cac9 6d 61 6c 69 7a 65 73 20 74 68 69 73 0a 20 20 20  malizes this.   
1caca 20 20 20 20 20 2a 2a 20 62 65 68 61 76 69 6f 72       ** behavior
1cacb 20 73 6f 20 74 68 61 74 20 61 6c 6c 20 61 72 63   so that all arc
1cacc 68 69 74 65 63 74 75 72 65 73 20 62 65 68 61 76  hitectures behav
1cacd 65 20 61 73 20 69 66 20 69 6e 74 65 67 65 72 0a  e as if integer.
1cace 20 20 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 66          ** overf
1cacf 6c 6f 77 20 6f 63 63 75 72 72 65 64 2e 0a 20 20  low occurred..  
1cad0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
1cad1 20 69 66 28 20 75 2e 61 66 2e 69 41 3d 3d 2d 31   if( u.af.iA==-1
1cad2 20 26 26 20 75 2e 61 66 2e 69 42 3d 3d 53 4d 41   && u.af.iB==SMA
1cad3 4c 4c 45 53 54 5f 49 4e 54 36 34 20 29 20 75 2e  LLEST_INT64 ) u.
1cad4 61 66 2e 69 41 20 3d 20 31 3b 0a 20 20 20 20 20  af.iA = 1;.     
1cad5 20 20 20 75 2e 61 66 2e 69 42 20 2f 3d 20 75 2e     u.af.iB /= u.
1cad6 61 66 2e 69 41 3b 0a 20 20 20 20 20 20 20 20 62  af.iA;.        b
1cad7 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
1cad8 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
1cad9 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e         if( u.af.
1cada 69 41 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69  iA==0 ) goto ari
1cadb 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69  thmetic_result_i
1cadc 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20  s_null;.        
1cadd 69 66 28 20 75 2e 61 66 2e 69 41 3d 3d 2d 31 20  if( u.af.iA==-1 
1cade 29 20 75 2e 61 66 2e 69 41 20 3d 20 31 3b 0a 20  ) u.af.iA = 1;. 
1cadf 20 20 20 20 20 20 20 75 2e 61 66 2e 69 42 20 25         u.af.iB %
1cae0 3d 20 75 2e 61 66 2e 69 41 3b 0a 20 20 20 20 20  = u.af.iA;.     
1cae1 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1cae2 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f 75 74  }.    }.    pOut
1cae3 2d 3e 75 2e 69 20 3d 20 75 2e 61 66 2e 69 42 3b  ->u.i = u.af.iB;
1cae4 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46  .    MemSetTypeF
1cae5 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e  lag(pOut, MEM_In
1cae6 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  t);.  }else{.   
1cae7 20 75 2e 61 66 2e 72 41 20 3d 20 73 71 6c 69 74   u.af.rA = sqlit
1cae8 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28  e3VdbeRealValue(
1cae9 70 49 6e 31 29 3b 0a 20 20 20 20 75 2e 61 66 2e  pIn1);.    u.af.
1caea 72 42 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rB = sqlite3Vdbe
1caeb 52 65 61 6c 56 61 6c 75 65 28 70 49 6e 32 29 3b  RealValue(pIn2);
1caec 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 4f 70  .    switch( pOp
1caed 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20  ->opcode ){.    
1caee 20 20 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20    case OP_Add:  
1caef 20 20 20 20 20 20 20 75 2e 61 66 2e 72 42 20 2b         u.af.rB +
1caf0 3d 20 75 2e 61 66 2e 72 41 3b 20 20 20 20 20 20  = u.af.rA;      
1caf1 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
1caf2 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a 20  se OP_Subtract: 
1caf3 20 20 20 75 2e 61 66 2e 72 42 20 2d 3d 20 75 2e     u.af.rB -= u.
1caf4 61 66 2e 72 41 3b 20 20 20 20 20 20 20 62 72 65  af.rA;       bre
1caf5 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f  ak;.      case O
1caf6 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 75  P_Multiply:    u
1caf7 2e 61 66 2e 72 42 20 2a 3d 20 75 2e 61 66 2e 72  .af.rB *= u.af.r
1caf8 41 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  A;       break;.
1caf9 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 44 69        case OP_Di
1cafa 76 69 64 65 3a 20 7b 0a 20 20 20 20 20 20 20 20  vide: {.        
1cafb 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20  /* (double)0 In 
1cafc 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f  case of SQLITE_O
1cafd 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
1cafe 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 20  NT... */.       
1caff 20 69 66 28 20 75 2e 61 66 2e 72 41 3d 3d 28 64   if( u.af.rA==(d
1cb00 6f 75 62 6c 65 29 30 20 29 20 67 6f 74 6f 20 61  ouble)0 ) goto a
1cb01 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74  rithmetic_result
1cb02 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20  _is_null;.      
1cb03 20 20 75 2e 61 66 2e 72 42 20 2f 3d 20 75 2e 61    u.af.rB /= u.a
1cb04 66 2e 72 41 3b 0a 20 20 20 20 20 20 20 20 62 72  f.rA;.        br
1cb05 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
1cb06 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
1cb07 20 20 20 20 20 20 75 2e 61 66 2e 69 41 20 3d 20        u.af.iA = 
1cb08 28 69 36 34 29 75 2e 61 66 2e 72 41 3b 0a 20 20  (i64)u.af.rA;.  
1cb09 20 20 20 20 20 20 75 2e 61 66 2e 69 42 20 3d 20        u.af.iB = 
1cb0a 28 69 36 34 29 75 2e 61 66 2e 72 42 3b 0a 20 20  (i64)u.af.rB;.  
1cb0b 20 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69        if( u.af.i
1cb0c 41 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74  A==0 ) goto arit
1cb0d 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73  hmetic_result_is
1cb0e 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69  _null;.        i
1cb0f 66 28 20 75 2e 61 66 2e 69 41 3d 3d 2d 31 20 29  f( u.af.iA==-1 )
1cb10 20 75 2e 61 66 2e 69 41 20 3d 20 31 3b 0a 20 20   u.af.iA = 1;.  
1cb11 20 20 20 20 20 20 75 2e 61 66 2e 72 42 20 3d 20        u.af.rB = 
1cb12 28 64 6f 75 62 6c 65 29 28 75 2e 61 66 2e 69 42  (double)(u.af.iB
1cb13 20 25 20 75 2e 61 66 2e 69 41 29 3b 0a 20 20 20   % u.af.iA);.   
1cb14 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1cb15 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
1cb16 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 75  ( sqlite3IsNaN(u
1cb17 2e 61 66 2e 72 42 29 20 29 7b 0a 20 20 20 20 20  .af.rB) ){.     
1cb18 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63   goto arithmetic
1cb19 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b  _result_is_null;
1cb1a 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 2d  .    }.    pOut-
1cb1b 3e 72 20 3d 20 75 2e 61 66 2e 72 42 3b 0a 20 20  >r = u.af.rB;.  
1cb1c 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1cb1d 28 70 4f 75 74 2c 20 4d 45 4d 5f 52 65 61 6c 29  (pOut, MEM_Real)
1cb1e 3b 0a 20 20 20 20 69 66 28 20 28 75 2e 61 66 2e  ;.    if( (u.af.
1cb1f 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c  flags & MEM_Real
1cb20 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  )==0 ){.      sq
1cb21 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 72  lite3VdbeInteger
1cb22 41 66 66 69 6e 69 74 79 28 70 4f 75 74 29 3b 0a  Affinity(pOut);.
1cb23 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61      }.  }.  brea
1cb24 6b 3b 0a 0a 61 72 69 74 68 6d 65 74 69 63 5f 72  k;..arithmetic_r
1cb25 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3a 0a 20  esult_is_null:. 
1cb26 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1cb27 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20  etNull(pOut);.  
1cb28 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1cb29 6f 64 65 3a 20 43 6f 6c 6c 53 65 71 20 2a 20 2a  ode: CollSeq * *
1cb2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20   P4.**.** P4 is 
1cb2b 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 43  a pointer to a C
1cb2c 6f 6c 6c 53 65 71 20 73 74 72 75 63 74 2e 20 49  ollSeq struct. I
1cb2d 66 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20  f the next call 
1cb2e 74 6f 20 61 20 75 73 65 72 20 66 75 6e 63 74 69  to a user functi
1cb2f 6f 6e 0a 2a 2a 20 6f 72 20 61 67 67 72 65 67 61  on.** or aggrega
1cb30 74 65 20 63 61 6c 6c 73 20 73 71 6c 69 74 65 33  te calls sqlite3
1cb31 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 29  GetFuncCollSeq()
1cb32 2c 20 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f 6e  , this collation
1cb33 20 73 65 71 75 65 6e 63 65 20 77 69 6c 6c 0a 2a   sequence will.*
1cb34 2a 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 54  * be returned. T
1cb35 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74  his is used by t
1cb36 68 65 20 62 75 69 6c 74 2d 69 6e 20 6d 69 6e 28  he built-in min(
1cb37 29 2c 20 6d 61 78 28 29 20 61 6e 64 20 6e 75 6c  ), max() and nul
1cb38 6c 69 66 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f  lif().** functio
1cb39 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e  ns..**.** The in
1cb3a 74 65 72 66 61 63 65 20 75 73 65 64 20 62 79 20  terface used by 
1cb3b 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
1cb3c 6f 6e 20 6f 66 20 74 68 65 20 61 66 6f 72 65 6d  on of the aforem
1cb3d 65 6e 74 69 6f 6e 65 64 20 66 75 6e 63 74 69 6f  entioned functio
1cb3e 6e 73 0a 2a 2a 20 74 6f 20 72 65 74 72 69 65 76  ns.** to retriev
1cb3f 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  e the collation 
1cb40 73 65 71 75 65 6e 63 65 20 73 65 74 20 62 79 20  sequence set by 
1cb41 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 6e  this opcode is n
1cb42 6f 74 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20  ot available.** 
1cb43 70 75 62 6c 69 63 6c 79 2c 20 6f 6e 6c 79 20 74  publicly, only t
1cb44 6f 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 73  o user functions
1cb45 20 64 65 66 69 6e 65 64 20 69 6e 20 66 75 6e 63   defined in func
1cb46 2e 63 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43  .c..*/.case OP_C
1cb47 6f 6c 6c 53 65 71 3a 20 7b 0a 20 20 61 73 73 65  ollSeq: {.  asse
1cb48 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d  rt( pOp->p4type=
1cb49 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20  =P4_COLLSEQ );. 
1cb4a 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1cb4b 63 6f 64 65 3a 20 46 75 6e 63 74 69 6f 6e 20 50  code: Function P
1cb4c 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a  1 P2 P3 P4 P5.**
1cb4d 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61 20 75 73 65  .** Invoke a use
1cb4e 72 20 66 75 6e 63 74 69 6f 6e 20 28 50 34 20 69  r function (P4 i
1cb4f 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
1cb50 20 46 75 6e 63 74 69 6f 6e 20 73 74 72 75 63 74   Function struct
1cb51 75 72 65 20 74 68 61 74 0a 2a 2a 20 64 65 66 69  ure that.** defi
1cb52 6e 65 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  nes the function
1cb53 29 20 77 69 74 68 20 50 35 20 61 72 67 75 6d 65  ) with P5 argume
1cb54 6e 74 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72  nts taken from r
1cb55 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 0a 2a  egister P2 and.*
1cb56 2a 20 73 75 63 63 65 73 73 6f 72 73 2e 20 20 54  * successors.  T
1cb57 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65  he result of the
1cb58 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 73 74 6f   function is sto
1cb59 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20  red in register 
1cb5a 50 33 2e 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  P3..** Register 
1cb5b 50 33 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 6f  P3 must not be o
1cb5c 6e 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69  ne of the functi
1cb5d 6f 6e 20 69 6e 70 75 74 73 2e 0a 2a 2a 0a 2a 2a  on inputs..**.**
1cb5e 20 50 31 20 69 73 20 61 20 33 32 2d 62 69 74 20   P1 is a 32-bit 
1cb5f 62 69 74 6d 61 73 6b 20 69 6e 64 69 63 61 74 69  bitmask indicati
1cb60 6e 67 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ng whether or no
1cb61 74 20 65 61 63 68 20 61 72 67 75 6d 65 6e 74 20  t each argument 
1cb62 74 6f 20 74 68 65 20 0a 2a 2a 20 66 75 6e 63 74  to the .** funct
1cb63 69 6f 6e 20 77 61 73 20 64 65 74 65 72 6d 69 6e  ion was determin
1cb64 65 64 20 74 6f 20 62 65 20 63 6f 6e 73 74 61 6e  ed to be constan
1cb65 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d  t at compile tim
1cb66 65 2e 20 49 66 20 74 68 65 20 66 69 72 73 74 0a  e. If the first.
1cb67 2a 2a 20 61 72 67 75 6d 65 6e 74 20 77 61 73 20  ** argument was 
1cb68 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e 20 62 69  constant then bi
1cb69 74 20 30 20 6f 66 20 50 31 20 69 73 20 73 65 74  t 0 of P1 is set
1cb6a 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74  . This is used t
1cb6b 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77  o determine.** w
1cb6c 68 65 74 68 65 72 20 6d 65 74 61 20 64 61 74 61  hether meta data
1cb6d 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
1cb6e 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e   a user function
1cb6f 20 61 72 67 75 6d 65 6e 74 20 75 73 69 6e 67 20   argument using 
1cb70 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73  the.** sqlite3_s
1cb71 65 74 5f 61 75 78 64 61 74 61 28 29 20 41 50 49  et_auxdata() API
1cb72 20 6d 61 79 20 62 65 20 73 61 66 65 6c 79 20 72   may be safely r
1cb73 65 74 61 69 6e 65 64 20 75 6e 74 69 6c 20 74 68  etained until th
1cb74 65 20 6e 65 78 74 0a 2a 2a 20 69 6e 76 6f 63 61  e next.** invoca
1cb75 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6f 70 63  tion of this opc
1cb76 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  ode..**.** See a
1cb77 6c 73 6f 3a 20 41 67 67 53 74 65 70 20 61 6e 64  lso: AggStep and
1cb78 20 41 67 67 46 69 6e 61 6c 0a 2a 2f 0a 63 61 73   AggFinal.*/.cas
1cb79 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 3a 20 7b  e OP_Function: {
1cb7a 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1cb7b 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1cb7c 20 69 6e 74 6f 20 75 2e 61 67 20 2a 2f 0a 20 20   into u.ag */.  
1cb7d 69 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 41  int i;.  Mem *pA
1cb7e 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  rg;.  sqlite3_co
1cb7f 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 73 71 6c  ntext ctx;.  sql
1cb80 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56  ite3_value **apV
1cb81 61 6c 3b 0a 20 20 69 6e 74 20 6e 3b 0a 23 65 6e  al;.  int n;.#en
1cb82 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1cb83 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1cb84 6f 20 75 2e 61 67 20 2a 2f 0a 0a 20 20 75 2e 61  o u.ag */..  u.a
1cb85 67 2e 6e 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 20  g.n = pOp->p5;. 
1cb86 20 75 2e 61 67 2e 61 70 56 61 6c 20 3d 20 70 2d   u.ag.apVal = p-
1cb87 3e 61 70 41 72 67 3b 0a 20 20 61 73 73 65 72 74  >apArg;.  assert
1cb88 28 20 75 2e 61 67 2e 61 70 56 61 6c 20 7c 7c 20  ( u.ag.apVal || 
1cb89 75 2e 61 67 2e 6e 3d 3d 30 20 29 3b 0a 0a 20 20  u.ag.n==0 );..  
1cb8a 61 73 73 65 72 74 28 20 75 2e 61 67 2e 6e 3d 3d  assert( u.ag.n==
1cb8b 30 20 7c 7c 20 28 70 4f 70 2d 3e 70 32 3e 30 20  0 || (pOp->p2>0 
1cb8c 26 26 20 70 4f 70 2d 3e 70 32 2b 75 2e 61 67 2e  && pOp->p2+u.ag.
1cb8d 6e 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 29 20 29 3b  n<=p->nMem+1) );
1cb8e 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1cb8f 70 33 3c 70 4f 70 2d 3e 70 32 20 7c 7c 20 70 4f  p3<pOp->p2 || pO
1cb90 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e 70 32 2b 75  p->p3>=pOp->p2+u
1cb91 2e 61 67 2e 6e 20 29 3b 0a 20 20 75 2e 61 67 2e  .ag.n );.  u.ag.
1cb92 70 41 72 67 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pArg = &p->aMem[
1cb93 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 66 6f 72 28  pOp->p2];.  for(
1cb94 75 2e 61 67 2e 69 3d 30 3b 20 75 2e 61 67 2e 69  u.ag.i=0; u.ag.i
1cb95 3c 75 2e 61 67 2e 6e 3b 20 75 2e 61 67 2e 69 2b  <u.ag.n; u.ag.i+
1cb96 2b 2c 20 75 2e 61 67 2e 70 41 72 67 2b 2b 29 7b  +, u.ag.pArg++){
1cb97 0a 20 20 20 20 75 2e 61 67 2e 61 70 56 61 6c 5b  .    u.ag.apVal[
1cb98 75 2e 61 67 2e 69 5d 20 3d 20 75 2e 61 67 2e 70  u.ag.i] = u.ag.p
1cb99 41 72 67 3b 0a 20 20 20 20 73 74 6f 72 65 54 79  Arg;.    storeTy
1cb9a 70 65 49 6e 66 6f 28 75 2e 61 67 2e 70 41 72 67  peInfo(u.ag.pArg
1cb9b 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20  , encoding);.   
1cb9c 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
1cb9d 70 4f 70 2d 3e 70 32 2c 20 75 2e 61 67 2e 70 41  pOp->p2, u.ag.pA
1cb9e 72 67 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65  rg);.  }..  asse
1cb9f 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d  rt( pOp->p4type=
1cba0 3d 50 34 5f 46 55 4e 43 44 45 46 20 7c 7c 20 70  =P4_FUNCDEF || p
1cba1 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56  Op->p4type==P4_V
1cba2 44 42 45 46 55 4e 43 20 29 3b 0a 20 20 69 66 28  DBEFUNC );.  if(
1cba3 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34   pOp->p4type==P4
1cba4 5f 46 55 4e 43 44 45 46 20 29 7b 0a 20 20 20 20  _FUNCDEF ){.    
1cba5 75 2e 61 67 2e 63 74 78 2e 70 46 75 6e 63 20 3d  u.ag.ctx.pFunc =
1cba6 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a   pOp->p4.pFunc;.
1cba7 20 20 20 20 75 2e 61 67 2e 63 74 78 2e 70 56 64      u.ag.ctx.pVd
1cba8 62 65 46 75 6e 63 20 3d 20 30 3b 0a 20 20 7d 65  beFunc = 0;.  }e
1cba9 6c 73 65 7b 0a 20 20 20 20 75 2e 61 67 2e 63 74  lse{.    u.ag.ct
1cbaa 78 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 28 56  x.pVdbeFunc = (V
1cbab 64 62 65 46 75 6e 63 2a 29 70 4f 70 2d 3e 70 34  dbeFunc*)pOp->p4
1cbac 2e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 20 20  .pVdbeFunc;.    
1cbad 75 2e 61 67 2e 63 74 78 2e 70 46 75 6e 63 20 3d  u.ag.ctx.pFunc =
1cbae 20 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 46   u.ag.ctx.pVdbeF
1cbaf 75 6e 63 2d 3e 70 46 75 6e 63 3b 0a 20 20 7d 0a  unc->pFunc;.  }.
1cbb0 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1cbb1 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c  p3>0 && pOp->p3<
1cbb2 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f  =p->nMem );.  pO
1cbb3 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  ut = &p->aMem[pO
1cbb4 70 2d 3e 70 33 5d 3b 0a 20 20 75 2e 61 67 2e 63  p->p3];.  u.ag.c
1cbb5 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d  tx.s.flags = MEM
1cbb6 5f 4e 75 6c 6c 3b 0a 20 20 75 2e 61 67 2e 63 74  _Null;.  u.ag.ct
1cbb7 78 2e 73 2e 64 62 20 3d 20 64 62 3b 0a 20 20 75  x.s.db = db;.  u
1cbb8 2e 61 67 2e 63 74 78 2e 73 2e 78 44 65 6c 20 3d  .ag.ctx.s.xDel =
1cbb9 20 30 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e 73   0;.  u.ag.ctx.s
1cbba 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 0a 20  .zMalloc = 0;.. 
1cbbb 20 2f 2a 20 54 68 65 20 6f 75 74 70 75 74 20 63   /* The output c
1cbbc 65 6c 6c 20 6d 61 79 20 61 6c 72 65 61 64 79 20  ell may already 
1cbbd 68 61 76 65 20 61 20 62 75 66 66 65 72 20 61 6c  have a buffer al
1cbbe 6c 6f 63 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20  located. Move.  
1cbbf 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74  ** the pointer t
1cbc0 6f 20 75 2e 61 67 2e 63 74 78 2e 73 20 73 6f 20  o u.ag.ctx.s so 
1cbc1 69 6e 20 63 61 73 65 20 74 68 65 20 75 73 65 72  in case the user
1cbc2 2d 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 75 73  -function can us
1cbc3 65 0a 20 20 2a 2a 20 74 68 65 20 61 6c 72 65 61  e.  ** the alrea
1cbc4 64 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66  dy allocated buf
1cbc5 66 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61  fer instead of a
1cbc6 6c 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20  llocating a new 
1cbc7 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  one..  */.  sqli
1cbc8 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26  te3VdbeMemMove(&
1cbc9 75 2e 61 67 2e 63 74 78 2e 73 2c 20 70 4f 75 74  u.ag.ctx.s, pOut
1cbca 29 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46  );.  MemSetTypeF
1cbcb 6c 61 67 28 26 75 2e 61 67 2e 63 74 78 2e 73 2c  lag(&u.ag.ctx.s,
1cbcc 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 75   MEM_Null);..  u
1cbcd 2e 61 67 2e 63 74 78 2e 69 73 45 72 72 6f 72 20  .ag.ctx.isError 
1cbce 3d 20 30 3b 0a 20 20 69 66 28 20 75 2e 61 67 2e  = 0;.  if( u.ag.
1cbcf 63 74 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73  ctx.pFunc->flags
1cbd0 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e   & SQLITE_FUNC_N
1cbd1 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 61  EEDCOLL ){.    a
1cbd2 73 73 65 72 74 28 20 70 4f 70 3e 70 2d 3e 61 4f  ssert( pOp>p->aO
1cbd3 70 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  p );.    assert(
1cbd4 20 70 4f 70 5b 2d 31 5d 2e 70 34 74 79 70 65 3d   pOp[-1].p4type=
1cbd5 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20  =P4_COLLSEQ );. 
1cbd6 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d     assert( pOp[-
1cbd7 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f  1].opcode==OP_Co
1cbd8 6c 6c 53 65 71 20 29 3b 0a 20 20 20 20 75 2e 61  llSeq );.    u.a
1cbd9 67 2e 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f  g.ctx.pColl = pO
1cbda 70 5b 2d 31 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a  p[-1].p4.pColl;.
1cbdb 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65    }.  if( sqlite
1cbdc 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29  3SafetyOff(db) )
1cbdd 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1cbde 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 28 2a 75  to_misuse;.  (*u
1cbdf 2e 61 67 2e 63 74 78 2e 70 46 75 6e 63 2d 3e 78  .ag.ctx.pFunc->x
1cbe0 46 75 6e 63 29 28 26 75 2e 61 67 2e 63 74 78 2c  Func)(&u.ag.ctx,
1cbe1 20 75 2e 61 67 2e 6e 2c 20 75 2e 61 67 2e 61 70   u.ag.n, u.ag.ap
1cbe2 56 61 6c 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  Val);.  if( sqli
1cbe3 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20  te3SafetyOn(db) 
1cbe4 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1cbe5 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 75 2e  beMemRelease(&u.
1cbe6 61 67 2e 63 74 78 2e 73 29 3b 0a 20 20 20 20 67  ag.ctx.s);.    g
1cbe7 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1cbe8 5f 6d 69 73 75 73 65 3b 0a 20 20 7d 0a 20 20 69  _misuse;.  }.  i
1cbe9 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
1cbea 6c 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 45 76  led ){.    /* Ev
1cbeb 65 6e 20 74 68 6f 75 67 68 20 61 20 6d 61 6c 6c  en though a mall
1cbec 6f 63 28 29 20 68 61 73 20 66 61 69 6c 65 64 2c  oc() has failed,
1cbed 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
1cbee 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20 20 20 2a  ion of the.    *
1cbef 2a 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20  * user function 
1cbf0 6d 61 79 20 68 61 76 65 20 63 61 6c 6c 65 64 20  may have called 
1cbf1 61 6e 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  an sqlite3_resul
1cbf2 74 5f 58 58 58 28 29 20 66 75 6e 63 74 69 6f 6e  t_XXX() function
1cbf3 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65 74 75 72  .    ** to retur
1cbf4 6e 20 61 20 76 61 6c 75 65 2e 20 54 68 65 20 66  n a value. The f
1cbf5 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 72 65  ollowing call re
1cbf6 6c 65 61 73 65 73 20 61 6e 79 20 72 65 73 6f 75  leases any resou
1cbf7 72 63 65 73 0a 20 20 20 20 2a 2a 20 61 73 73 6f  rces.    ** asso
1cbf8 63 69 61 74 65 64 20 77 69 74 68 20 73 75 63 68  ciated with such
1cbf9 20 61 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2a   a value..    **
1cbfa 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 3a 20 4d 61  .    ** Note: Ma
1cbfb 79 62 65 20 4d 65 6d 52 65 6c 65 61 73 65 28 29  ybe MemRelease()
1cbfc 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65   should be calle
1cbfd 64 20 69 66 20 73 71 6c 69 74 65 33 53 61 66 65  d if sqlite3Safe
1cbfe 74 79 4f 6e 28 29 0a 20 20 20 20 2a 2a 20 66 61  tyOn().    ** fa
1cbff 69 6c 73 20 61 6c 73 6f 20 28 74 68 65 20 69 66  ils also (the if
1cc00 28 2e 2e 2e 29 20 73 74 61 74 65 6d 65 6e 74 20  (...) statement 
1cc01 61 62 6f 76 65 29 2e 20 42 75 74 20 69 66 20 70  above). But if p
1cc02 65 6f 70 6c 65 20 61 72 65 0a 20 20 20 20 2a 2a  eople are.    **
1cc03 20 6d 69 73 75 73 69 6e 67 20 73 71 6c 69 74 65   misusing sqlite
1cc04 2c 20 74 68 65 79 20 68 61 76 65 20 62 69 67 67  , they have bigg
1cc05 65 72 20 70 72 6f 62 6c 65 6d 73 20 74 68 61 6e  er problems than
1cc06 20 61 20 6c 65 61 6b 65 64 20 76 61 6c 75 65 2e   a leaked value.
1cc07 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
1cc08 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
1cc09 65 28 26 75 2e 61 67 2e 63 74 78 2e 73 29 3b 0a  e(&u.ag.ctx.s);.
1cc0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b      goto no_mem;
1cc0b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e  .  }..  /* If an
1cc0c 79 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61  y auxiliary data
1cc0d 20 66 75 6e 63 74 69 6f 6e 73 20 68 61 76 65 20   functions have 
1cc0e 62 65 65 6e 20 63 61 6c 6c 65 64 20 62 79 20 74  been called by t
1cc0f 68 69 73 20 75 73 65 72 20 66 75 6e 63 74 69 6f  his user functio
1cc10 6e 2c 0a 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74  n,.  ** immediat
1cc11 65 6c 79 20 63 61 6c 6c 20 74 68 65 20 64 65 73  ely call the des
1cc12 74 72 75 63 74 6f 72 20 66 6f 72 20 61 6e 79 20  tructor for any 
1cc13 6e 6f 6e 2d 73 74 61 74 69 63 20 76 61 6c 75 65  non-static value
1cc14 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 75 2e  s..  */.  if( u.
1cc15 61 67 2e 63 74 78 2e 70 56 64 62 65 46 75 6e 63  ag.ctx.pVdbeFunc
1cc16 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1cc17 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61  dbeDeleteAuxData
1cc18 28 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 46  (u.ag.ctx.pVdbeF
1cc19 75 6e 63 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20  unc, pOp->p1);. 
1cc1a 20 20 20 70 4f 70 2d 3e 70 34 2e 70 56 64 62 65     pOp->p4.pVdbe
1cc1b 46 75 6e 63 20 3d 20 75 2e 61 67 2e 63 74 78 2e  Func = u.ag.ctx.
1cc1c 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 20 20 70  pVdbeFunc;.    p
1cc1d 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f  Op->p4type = P4_
1cc1e 56 44 42 45 46 55 4e 43 3b 0a 20 20 7d 0a 0a 20  VDBEFUNC;.  }.. 
1cc1f 20 2f 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74   /* If the funct
1cc20 69 6f 6e 20 72 65 74 75 72 6e 65 64 20 61 6e 20  ion returned an 
1cc21 65 72 72 6f 72 2c 20 74 68 72 6f 77 20 61 6e 20  error, throw an 
1cc22 65 78 63 65 70 74 69 6f 6e 20 2a 2f 0a 20 20 69  exception */.  i
1cc23 66 28 20 75 2e 61 67 2e 63 74 78 2e 69 73 45 72  f( u.ag.ctx.isEr
1cc24 72 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ror ){.    sqlit
1cc25 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
1cc26 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73  zErrMsg, db, "%s
1cc27 22 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ", sqlite3_value
1cc28 5f 74 65 78 74 28 26 75 2e 61 67 2e 63 74 78 2e  _text(&u.ag.ctx.
1cc29 73 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e  s));.    rc = u.
1cc2a 61 67 2e 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a  ag.ctx.isError;.
1cc2b 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74    }..  /* Copy t
1cc2c 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65  he result of the
1cc2d 20 66 75 6e 63 74 69 6f 6e 20 69 6e 74 6f 20 72   function into r
1cc2e 65 67 69 73 74 65 72 20 50 33 20 2a 2f 0a 20 20  egister P3 */.  
1cc2f 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1cc30 65 45 6e 63 6f 64 69 6e 67 28 26 75 2e 61 67 2e  eEncoding(&u.ag.
1cc31 63 74 78 2e 73 2c 20 65 6e 63 6f 64 69 6e 67 29  ctx.s, encoding)
1cc32 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
1cc33 65 6d 4d 6f 76 65 28 70 4f 75 74 2c 20 26 75 2e  emMove(pOut, &u.
1cc34 61 67 2e 63 74 78 2e 73 29 3b 0a 20 20 69 66 28  ag.ctx.s);.  if(
1cc35 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54   sqlite3VdbeMemT
1cc36 6f 6f 42 69 67 28 70 4f 75 74 29 20 29 7b 0a 20  ooBig(pOut) ){. 
1cc37 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b     goto too_big;
1cc38 0a 20 20 7d 0a 20 20 52 45 47 49 53 54 45 52 5f  .  }.  REGISTER_
1cc39 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70  TRACE(pOp->p3, p
1cc3a 4f 75 74 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d  Out);.  UPDATE_M
1cc3b 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74  AX_BLOBSIZE(pOut
1cc3c 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  );.  break;.}../
1cc3d 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 41 6e 64  * Opcode: BitAnd
1cc3e 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a   P1 P2 P3 * *.**
1cc3f 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 62 69 74  .** Take the bit
1cc40 2d 77 69 73 65 20 41 4e 44 20 6f 66 20 74 68 65  -wise AND of the
1cc41 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73   values in regis
1cc42 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61 6e  ter P1 and P2 an
1cc43 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 72  d.** store the r
1cc44 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65  esult in registe
1cc45 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68  r P3..** If eith
1cc46 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c  er input is NULL
1cc47 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  , the result is 
1cc48 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f  NULL..*/./* Opco
1cc49 64 65 3a 20 42 69 74 4f 72 20 50 31 20 50 32 20  de: BitOr P1 P2 
1cc4a 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b  P3 * *.**.** Tak
1cc4b 65 20 74 68 65 20 62 69 74 2d 77 69 73 65 20 4f  e the bit-wise O
1cc4c 52 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20  R of the values 
1cc4d 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 61  in register P1 a
1cc4e 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74 6f  nd P2 and.** sto
1cc4f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e  re the result in
1cc50 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a   register P3..**
1cc51 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74   If either input
1cc52 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65   is NULL, the re
1cc53 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f  sult is NULL..*/
1cc54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 68 69 66  ./* Opcode: Shif
1cc55 74 4c 65 66 74 20 50 31 20 50 32 20 50 33 20 2a  tLeft P1 P2 P3 *
1cc56 20 2a 0a 2a 2a 0a 2a 2a 20 53 68 69 66 74 20 74   *.**.** Shift t
1cc57 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  he integer value
1cc58 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20   in register P2 
1cc59 74 6f 20 74 68 65 20 6c 65 66 74 20 62 79 20 74  to the left by t
1cc5a 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  he.** number of 
1cc5b 62 69 74 73 20 73 70 65 63 69 66 69 65 64 20 62  bits specified b
1cc5c 79 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e  y the integer in
1cc5d 20 72 65 67 69 73 65 72 20 50 31 2e 0a 2a 2a 20   regiser P1..** 
1cc5e 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  Store the result
1cc5f 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e   in register P3.
1cc60 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e  .** If either in
1cc61 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  put is NULL, the
1cc62 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e   result is NULL.
1cc63 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53  .*/./* Opcode: S
1cc64 68 69 66 74 52 69 67 68 74 20 50 31 20 50 32 20  hiftRight P1 P2 
1cc65 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 68 69  P3 * *.**.** Shi
1cc66 66 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 76  ft the integer v
1cc67 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1cc68 20 50 32 20 74 6f 20 74 68 65 20 72 69 67 68 74   P2 to the right
1cc69 20 62 79 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65   by the.** numbe
1cc6a 72 20 6f 66 20 62 69 74 73 20 73 70 65 63 69 66  r of bits specif
1cc6b 69 65 64 20 62 79 20 74 68 65 20 69 6e 74 65 67  ied by the integ
1cc6c 65 72 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  er in register P
1cc6d 31 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20  1..** Store the 
1cc6e 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74  result in regist
1cc6f 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74  er P3..** If eit
1cc70 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c  her input is NUL
1cc71 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  L, the result is
1cc72 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f   NULL..*/.case O
1cc73 50 5f 42 69 74 41 6e 64 3a 20 20 20 20 20 20 20  P_BitAnd:       
1cc74 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
1cc75 65 20 61 73 20 54 4b 5f 42 49 54 41 4e 44 2c 20  e as TK_BITAND, 
1cc76 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a  in1, in2, out3 *
1cc77 2f 0a 63 61 73 65 20 4f 50 5f 42 69 74 4f 72 3a  /.case OP_BitOr:
1cc78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cc79 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1cc7a 42 49 54 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c  BITOR, in1, in2,
1cc7b 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50   out3 */.case OP
1cc7c 5f 53 68 69 66 74 4c 65 66 74 3a 20 20 20 20 20  _ShiftLeft:     
1cc7d 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
1cc7e 20 61 73 20 54 4b 5f 4c 53 48 49 46 54 2c 20 69   as TK_LSHIFT, i
1cc7f 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f  n1, in2, out3 */
1cc80 0a 63 61 73 65 20 4f 50 5f 53 68 69 66 74 52 69  .case OP_ShiftRi
1cc81 67 68 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20  ght: {          
1cc82 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52   /* same as TK_R
1cc83 53 48 49 46 54 2c 20 69 6e 31 2c 20 69 6e 32 2c  SHIFT, in1, in2,
1cc84 20 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20   out3 */.#if 0  
1cc85 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1cc86 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1cc87 61 68 20 2a 2f 0a 20 20 69 36 34 20 61 3b 0a 20  ah */.  i64 a;. 
1cc88 20 69 36 34 20 62 3b 0a 23 65 6e 64 69 66 20 2f   i64 b;.#endif /
1cc89 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1cc8a 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1cc8b 68 20 2a 2f 0a 0a 20 20 69 66 28 20 28 70 49 6e  h */..  if( (pIn
1cc8c 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 6e 32 2d  1->flags | pIn2-
1cc8d 3e 66 6c 61 67 73 29 20 26 20 4d 45 4d 5f 4e 75  >flags) & MEM_Nu
1cc8e 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ll ){.    sqlite
1cc8f 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28  3VdbeMemSetNull(
1cc90 70 4f 75 74 29 3b 0a 20 20 20 20 62 72 65 61 6b  pOut);.    break
1cc91 3b 0a 20 20 7d 0a 20 20 75 2e 61 68 2e 61 20 3d  ;.  }.  u.ah.a =
1cc92 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56   sqlite3VdbeIntV
1cc93 61 6c 75 65 28 70 49 6e 32 29 3b 0a 20 20 75 2e  alue(pIn2);.  u.
1cc94 61 68 2e 62 20 3d 20 73 71 6c 69 74 65 33 56 64  ah.b = sqlite3Vd
1cc95 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29  beIntValue(pIn1)
1cc96 3b 0a 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d  ;.  switch( pOp-
1cc97 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 63  >opcode ){.    c
1cc98 61 73 65 20 4f 50 5f 42 69 74 41 6e 64 3a 20 20  ase OP_BitAnd:  
1cc99 20 20 20 20 75 2e 61 68 2e 61 20 26 3d 20 75 2e      u.ah.a &= u.
1cc9a 61 68 2e 62 3b 20 20 20 20 20 62 72 65 61 6b 3b  ah.b;     break;
1cc9b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 42 69 74  .    case OP_Bit
1cc9c 4f 72 3a 20 20 20 20 20 20 20 75 2e 61 68 2e 61  Or:       u.ah.a
1cc9d 20 7c 3d 20 75 2e 61 68 2e 62 3b 20 20 20 20 20   |= u.ah.b;     
1cc9e 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
1cc9f 4f 50 5f 53 68 69 66 74 4c 65 66 74 3a 20 20 20  OP_ShiftLeft:   
1cca0 75 2e 61 68 2e 61 20 3c 3c 3d 20 75 2e 61 68 2e  u.ah.a <<= u.ah.
1cca1 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  b;    break;.   
1cca2 20 64 65 66 61 75 6c 74 3a 20 20 61 73 73 65 72   default:  asser
1cca3 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  t( pOp->opcode==
1cca4 4f 50 5f 53 68 69 66 74 52 69 67 68 74 20 29 3b  OP_ShiftRight );
1cca5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1cca6 20 20 20 20 20 20 20 20 20 20 75 2e 61 68 2e 61            u.ah.a
1cca7 20 3e 3e 3d 20 75 2e 61 68 2e 62 3b 20 20 20 20   >>= u.ah.b;    
1cca8 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 70 4f 75  break;.  }.  pOu
1cca9 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 68 2e 61 3b  t->u.i = u.ah.a;
1ccaa 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61  .  MemSetTypeFla
1ccab 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29  g(pOut, MEM_Int)
1ccac 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1ccad 20 4f 70 63 6f 64 65 3a 20 41 64 64 49 6d 6d 20   Opcode: AddImm 
1ccae 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 20   P1 P2 * * *.** 
1ccaf 0a 2a 2a 20 41 64 64 20 74 68 65 20 63 6f 6e 73  .** Add the cons
1ccb0 74 61 6e 74 20 50 32 20 74 6f 20 74 68 65 20 76  tant P2 to the v
1ccb1 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1ccb2 20 50 31 2e 0a 2a 2a 20 54 68 65 20 72 65 73 75   P1..** The resu
1ccb3 6c 74 20 69 73 20 61 6c 77 61 79 73 20 61 6e 20  lt is always an 
1ccb4 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54  integer..**.** T
1ccb5 6f 20 66 6f 72 63 65 20 61 6e 79 20 72 65 67 69  o force any regi
1ccb6 73 74 65 72 20 74 6f 20 62 65 20 61 6e 20 69 6e  ster to be an in
1ccb7 74 65 67 65 72 2c 20 6a 75 73 74 20 61 64 64 20  teger, just add 
1ccb8 30 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 64  0..*/.case OP_Ad
1ccb9 64 49 6d 6d 3a 20 7b 20 20 20 20 20 20 20 20 20  dImm: {         
1ccba 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 73     /* in1 */.  s
1ccbb 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74  qlite3VdbeMemInt
1ccbc 65 67 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20  egerify(pIn1);. 
1ccbd 20 70 49 6e 31 2d 3e 75 2e 69 20 2b 3d 20 70 4f   pIn1->u.i += pO
1ccbe 70 2d 3e 70 32 3b 0a 20 20 62 72 65 61 6b 3b 0a  p->p2;.  break;.
1ccbf 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75  }../* Opcode: Mu
1ccc0 73 74 42 65 49 6e 74 20 50 31 20 50 32 20 2a 20  stBeInt P1 P2 * 
1ccc1 2a 20 2a 0a 2a 2a 20 0a 2a 2a 20 46 6f 72 63 65  * *.** .** Force
1ccc2 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1ccc3 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20  gister P1 to be 
1ccc4 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 49 66 20  an integer.  If 
1ccc5 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20  the value.** in 
1ccc6 50 31 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74  P1 is not an int
1ccc7 65 67 65 72 20 61 6e 64 20 63 61 6e 6e 6f 74 20  eger and cannot 
1ccc8 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74  be converted int
1ccc9 6f 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20  o an integer.** 
1ccca 77 69 74 68 6f 75 74 20 64 61 74 61 20 6c 6f 73  without data los
1cccb 73 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d  s, then jump imm
1cccc 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2c 20  ediately to P2, 
1cccd 6f 72 20 69 66 20 50 32 3d 3d 30 0a 2a 2a 20 72  or if P2==0.** r
1ccce 61 69 73 65 20 61 6e 20 53 51 4c 49 54 45 5f 4d  aise an SQLITE_M
1cccf 49 53 4d 41 54 43 48 20 65 78 63 65 70 74 69 6f  ISMATCH exceptio
1ccd0 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 75  n..*/.case OP_Mu
1ccd1 73 74 42 65 49 6e 74 3a 20 7b 20 20 20 20 20 20  stBeInt: {      
1ccd2 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69        /* jump, i
1ccd3 6e 31 20 2a 2f 0a 20 20 61 70 70 6c 79 41 66 66  n1 */.  applyAff
1ccd4 69 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 4c 49  inity(pIn1, SQLI
1ccd5 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 2c 20  TE_AFF_NUMERIC, 
1ccd6 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 69 66 28  encoding);.  if(
1ccd7 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20   (pIn1->flags & 
1ccd8 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20  MEM_Int)==0 ){. 
1ccd9 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d     if( pOp->p2==
1ccda 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
1ccdb 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 3b  SQLITE_MISMATCH;
1ccdc 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72  .      goto abor
1ccdd 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1ccde 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1ccdf 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1cce0 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  1;.    }.  }else
1cce1 7b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65  {.    MemSetType
1cce2 46 6c 61 67 28 70 49 6e 31 2c 20 4d 45 4d 5f 49  Flag(pIn1, MEM_I
1cce3 6e 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  nt);.  }.  break
1cce4 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1cce5 52 65 61 6c 41 66 66 69 6e 69 74 79 20 50 31 20  RealAffinity P1 
1cce6 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66  * * * *.**.** If
1cce7 20 72 65 67 69 73 74 65 72 20 50 31 20 68 6f 6c   register P1 hol
1cce8 64 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f  ds an integer co
1cce9 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 72 65  nvert it to a re
1ccea 61 6c 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20  al value..**.** 
1cceb 54 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 75  This opcode is u
1ccec 73 65 64 20 77 68 65 6e 20 65 78 74 72 61 63 74  sed when extract
1cced 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ing information 
1ccee 66 72 6f 6d 20 61 20 63 6f 6c 75 6d 6e 20 74 68  from a column th
1ccef 61 74 0a 2a 2a 20 68 61 73 20 52 45 41 4c 20 61  at.** has REAL a
1ccf0 66 66 69 6e 69 74 79 2e 20 20 53 75 63 68 20 63  ffinity.  Such c
1ccf1 6f 6c 75 6d 6e 20 76 61 6c 75 65 73 20 6d 61 79  olumn values may
1ccf2 20 73 74 69 6c 6c 20 62 65 20 73 74 6f 72 65 64   still be stored
1ccf3 20 61 73 0a 2a 2a 20 69 6e 74 65 67 65 72 73 2c   as.** integers,
1ccf4 20 66 6f 72 20 73 70 61 63 65 20 65 66 66 69 63   for space effic
1ccf5 69 65 6e 63 79 2c 20 62 75 74 20 61 66 74 65 72  iency, but after
1ccf6 20 65 78 74 72 61 63 74 69 6f 6e 20 77 65 20 77   extraction we w
1ccf7 61 6e 74 20 74 68 65 6d 0a 2a 2a 20 74 6f 20 68  ant them.** to h
1ccf8 61 76 65 20 6f 6e 6c 79 20 61 20 72 65 61 6c 20  ave only a real 
1ccf9 76 61 6c 75 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f  value..*/.case O
1ccfa 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 3a 20  P_RealAffinity: 
1ccfb 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
1ccfc 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 69     /* in1 */.  i
1ccfd 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  f( pIn1->flags &
1ccfe 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20   MEM_Int ){.    
1ccff 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
1cd00 61 6c 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d  alify(pIn1);.  }
1cd01 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66  .  break;.}..#if
1cd02 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1cd03 5f 43 41 53 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a  _CAST./* Opcode:
1cd04 20 54 6f 54 65 78 74 20 50 31 20 2a 20 2a 20 2a   ToText P1 * * *
1cd05 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74   *.**.** Force t
1cd06 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1cd07 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 74 65  ster P1 to be te
1cd08 78 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61  xt..** If the va
1cd09 6c 75 65 20 69 73 20 6e 75 6d 65 72 69 63 2c 20  lue is numeric, 
1cd0a 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20  convert it to a 
1cd0b 73 74 72 69 6e 67 20 75 73 69 6e 67 20 74 68 65  string using the
1cd0c 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f  .** equivalent o
1cd0d 66 20 70 72 69 6e 74 66 28 29 2e 20 20 42 6c 6f  f printf().  Blo
1cd0e 62 20 76 61 6c 75 65 73 20 61 72 65 20 75 6e 63  b values are unc
1cd0f 68 61 6e 67 65 64 20 61 6e 64 0a 2a 2a 20 61 72  hanged and.** ar
1cd10 65 20 61 66 74 65 72 77 61 72 64 73 20 73 69 6d  e afterwards sim
1cd11 70 6c 79 20 69 6e 74 65 72 70 72 65 74 65 64 20  ply interpreted 
1cd12 61 73 20 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 41  as text..**.** A
1cd13 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e   NULL value is n
1cd14 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68  ot changed by th
1cd15 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20  is routine.  It 
1cd16 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f  remains NULL..*/
1cd17 0a 63 61 73 65 20 4f 50 5f 54 6f 54 65 78 74 3a  .case OP_ToText:
1cd18 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
1cd19 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1cd1a 4b 5f 54 4f 5f 54 45 58 54 2c 20 69 6e 31 20 2a  K_TO_TEXT, in1 *
1cd1b 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c  /.  if( pIn1->fl
1cd1c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29  ags & MEM_Null )
1cd1d 20 62 72 65 61 6b 3b 0a 20 20 61 73 73 65 72 74   break;.  assert
1cd1e 28 20 4d 45 4d 5f 53 74 72 3d 3d 28 4d 45 4d 5f  ( MEM_Str==(MEM_
1cd1f 42 6c 6f 62 3e 3e 33 29 20 29 3b 0a 20 20 70 49  Blob>>3) );.  pI
1cd20 6e 31 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 49  n1->flags |= (pI
1cd21 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c  n1->flags&MEM_Bl
1cd22 6f 62 29 3e 3e 33 3b 0a 20 20 61 70 70 6c 79 41  ob)>>3;.  applyA
1cd23 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 53 51  ffinity(pIn1, SQ
1cd24 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 65  LITE_AFF_TEXT, e
1cd25 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 72 63 20 3d  ncoding);.  rc =
1cd26 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31   ExpandBlob(pIn1
1cd27 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e  );.  assert( pIn
1cd28 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53  1->flags & MEM_S
1cd29 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  tr || db->malloc
1cd2a 46 61 69 6c 65 64 20 29 3b 0a 20 20 70 49 6e 31  Failed );.  pIn1
1cd2b 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d  ->flags &= ~(MEM
1cd2c 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45  _Int|MEM_Real|ME
1cd2d 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 29  M_Blob|MEM_Zero)
1cd2e 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42  ;.  UPDATE_MAX_B
1cd2f 4c 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20  LOBSIZE(pIn1);. 
1cd30 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1cd31 63 6f 64 65 3a 20 54 6f 42 6c 6f 62 20 50 31 20  code: ToBlob P1 
1cd32 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f  * * * *.**.** Fo
1cd33 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e  rce the value in
1cd34 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20   register P1 to 
1cd35 62 65 20 61 20 42 4c 4f 42 2e 0a 2a 2a 20 49 66  be a BLOB..** If
1cd36 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 6e 75   the value is nu
1cd37 6d 65 72 69 63 2c 20 63 6f 6e 76 65 72 74 20 69  meric, convert i
1cd38 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 66 69  t to a string fi
1cd39 72 73 74 2e 0a 2a 2a 20 53 74 72 69 6e 67 73 20  rst..** Strings 
1cd3a 61 72 65 20 73 69 6d 70 6c 79 20 72 65 69 6e 74  are simply reint
1cd3b 65 72 70 72 65 74 65 64 20 61 73 20 62 6c 6f 62  erpreted as blob
1cd3c 73 20 77 69 74 68 20 6e 6f 20 63 68 61 6e 67 65  s with no change
1cd3d 0a 2a 2a 20 74 6f 20 74 68 65 20 75 6e 64 65 72  .** to the under
1cd3e 6c 79 69 6e 67 20 64 61 74 61 2e 0a 2a 2a 0a 2a  lying data..**.*
1cd3f 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69  * A NULL value i
1cd40 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79  s not changed by
1cd41 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20   this routine.  
1cd42 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e  It remains NULL.
1cd43 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 42 6c  .*/.case OP_ToBl
1cd44 6f 62 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  ob: {           
1cd45 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1cd46 73 20 54 4b 5f 54 4f 5f 42 4c 4f 42 2c 20 69 6e  s TK_TO_BLOB, in
1cd47 31 20 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d  1 */.  if( pIn1-
1cd48 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  >flags & MEM_Nul
1cd49 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 69 66 28  l ) break;.  if(
1cd4a 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20   (pIn1->flags & 
1cd4b 4d 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 7b 0a  MEM_Blob)==0 ){.
1cd4c 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74      applyAffinit
1cd4d 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41  y(pIn1, SQLITE_A
1cd4e 46 46 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e  FF_TEXT, encodin
1cd4f 67 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  g);.    assert( 
1cd50 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
1cd51 4d 5f 53 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c  M_Str || db->mal
1cd52 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20  locFailed );.   
1cd53 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28   MemSetTypeFlag(
1cd54 70 49 6e 31 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b  pIn1, MEM_Blob);
1cd55 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49  .  }else{.    pI
1cd56 6e 31 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d  n1->flags &= ~(M
1cd57 45 4d 5f 54 79 70 65 4d 61 73 6b 26 7e 4d 45 4d  EM_TypeMask&~MEM
1cd58 5f 42 6c 6f 62 29 3b 0a 20 20 7d 0a 20 20 55 50  _Blob);.  }.  UP
1cd59 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a  DATE_MAX_BLOBSIZ
1cd5a 45 28 70 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b  E(pIn1);.  break
1cd5b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1cd5c 54 6f 4e 75 6d 65 72 69 63 20 50 31 20 2a 20 2a  ToNumeric P1 * *
1cd5d 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65   * *.**.** Force
1cd5e 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1cd5f 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20  gister P1 to be 
1cd60 6e 75 6d 65 72 69 63 20 28 65 69 74 68 65 72 20  numeric (either 
1cd61 61 6e 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6f 72  an.** integer or
1cd62 20 61 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e   a floating-poin
1cd63 74 20 6e 75 6d 62 65 72 2e 29 0a 2a 2a 20 49 66  t number.).** If
1cd64 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 74 65   the value is te
1cd65 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20  xt or blob, try 
1cd66 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f  to convert it to
1cd67 20 61 6e 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a   an using the.**
1cd68 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 61   equivalent of a
1cd69 74 6f 69 28 29 20 6f 72 20 61 74 6f 66 28 29 20  toi() or atof() 
1cd6a 61 6e 64 20 73 74 6f 72 65 20 30 20 69 66 20 6e  and store 0 if n
1cd6b 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f  o such conversio
1cd6c 6e 20 0a 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c  n .** is possibl
1cd6d 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20  e..**.** A NULL 
1cd6e 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61  value is not cha
1cd6f 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 6f 75  nged by this rou
1cd70 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e  tine.  It remain
1cd71 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20  s NULL..*/.case 
1cd72 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 3a 20 7b 20  OP_ToNumeric: { 
1cd73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cd74 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54   /* same as TK_T
1cd75 4f 5f 4e 55 4d 45 52 49 43 2c 20 69 6e 31 20 2a  O_NUMERIC, in1 *
1cd76 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66  /.  if( (pIn1->f
1cd77 6c 61 67 73 20 26 20 28 4d 45 4d 5f 4e 75 6c 6c  lags & (MEM_Null
1cd78 7c 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61  |MEM_Int|MEM_Rea
1cd79 6c 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  l))==0 ){.    sq
1cd7a 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65  lite3VdbeMemNume
1cd7b 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a  rify(pIn1);.  }.
1cd7c 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69    break;.}.#endi
1cd7d 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1cd7e 5f 43 41 53 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63  _CAST */../* Opc
1cd7f 6f 64 65 3a 20 54 6f 49 6e 74 20 50 31 20 2a 20  ode: ToInt P1 * 
1cd80 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63  * * *.**.** Forc
1cd81 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  e the value in r
1cd82 65 67 69 73 74 65 72 20 50 31 20 62 65 20 61 6e  egister P1 be an
1cd83 20 69 6e 74 65 67 65 72 2e 20 20 49 66 0a 2a 2a   integer.  If.**
1cd84 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 63 75   The value is cu
1cd85 72 72 65 6e 74 6c 79 20 61 20 72 65 61 6c 20 6e  rrently a real n
1cd86 75 6d 62 65 72 2c 20 64 72 6f 70 20 69 74 73 20  umber, drop its 
1cd87 66 72 61 63 74 69 6f 6e 61 6c 20 70 61 72 74 2e  fractional part.
1cd88 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65  .** If the value
1cd89 20 69 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62   is text or blob
1cd8a 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74  , try to convert
1cd8b 20 69 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65   it to an intege
1cd8c 72 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65  r using the.** e
1cd8d 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f  quivalent of ato
1cd8e 69 28 29 20 61 6e 64 20 73 74 6f 72 65 20 30 20  i() and store 0 
1cd8f 69 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 65  if no such conve
1cd90 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c  rsion is possibl
1cd91 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20  e..**.** A NULL 
1cd92 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61  value is not cha
1cd93 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 6f 75  nged by this rou
1cd94 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e  tine.  It remain
1cd95 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20  s NULL..*/.case 
1cd96 4f 50 5f 54 6f 49 6e 74 3a 20 7b 20 20 20 20 20  OP_ToInt: {     
1cd97 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1cd98 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 49 4e  same as TK_TO_IN
1cd99 54 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20  T, in1 */.  if( 
1cd9a 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d  (pIn1->flags & M
1cd9b 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20  EM_Null)==0 ){. 
1cd9c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1cd9d 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 31  mIntegerify(pIn1
1cd9e 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  );.  }.  break;.
1cd9f 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
1cda0 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 2f 2a 20 4f  E_OMIT_CAST./* O
1cda1 70 63 6f 64 65 3a 20 54 6f 52 65 61 6c 20 50 31  pcode: ToReal P1
1cda2 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46   * * * *.**.** F
1cda3 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 69  orce the value i
1cda4 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f  n register P1 to
1cda5 20 62 65 20 61 20 66 6c 6f 61 74 69 6e 67 20 70   be a floating p
1cda6 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20  oint number..** 
1cda7 49 66 20 54 68 65 20 76 61 6c 75 65 20 69 73 20  If The value is 
1cda8 63 75 72 72 65 6e 74 6c 79 20 61 6e 20 69 6e 74  currently an int
1cda9 65 67 65 72 2c 20 63 6f 6e 76 65 72 74 20 69 74  eger, convert it
1cdaa 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75  ..** If the valu
1cdab 65 20 69 73 20 74 65 78 74 20 6f 72 20 62 6c 6f  e is text or blo
1cdac 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72  b, try to conver
1cdad 74 20 69 74 20 74 6f 20 61 6e 20 69 6e 74 65 67  t it to an integ
1cdae 65 72 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  er using the.** 
1cdaf 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74  equivalent of at
1cdb0 6f 69 28 29 20 61 6e 64 20 73 74 6f 72 65 20 30  oi() and store 0
1cdb1 2e 30 20 69 66 20 6e 6f 20 73 75 63 68 20 63 6f  .0 if no such co
1cdb2 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73  nversion is poss
1cdb3 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55  ible..**.** A NU
1cdb4 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20  LL value is not 
1cdb5 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20  changed by this 
1cdb6 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d  routine.  It rem
1cdb7 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61  ains NULL..*/.ca
1cdb8 73 65 20 4f 50 5f 54 6f 52 65 61 6c 3a 20 7b 20  se OP_ToReal: { 
1cdb9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cdba 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54   /* same as TK_T
1cdbb 4f 5f 52 45 41 4c 2c 20 69 6e 31 20 2a 2f 0a 20  O_REAL, in1 */. 
1cdbc 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67   if( (pIn1->flag
1cdbd 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30  s & MEM_Null)==0
1cdbe 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1cdbf 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49  dbeMemRealify(pI
1cdc0 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  n1);.  }.  break
1cdc1 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
1cdc2 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a  LITE_OMIT_CAST *
1cdc3 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 74  /../* Opcode: Lt
1cdc4 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a   P1 P2 P3 P4 P5.
1cdc5 2a 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68  **.** Compare th
1cdc6 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69  e values in regi
1cdc7 73 74 65 72 20 50 31 20 61 6e 64 20 50 33 2e 20  ster P1 and P3. 
1cdc8 20 49 66 20 72 65 67 28 50 33 29 3c 72 65 67 28   If reg(P3)<reg(
1cdc9 50 31 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70  P1) then.** jump
1cdca 20 74 6f 20 61 64 64 72 65 73 73 20 50 32 2e 20   to address P2. 
1cdcb 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53   .**.** If the S
1cdcc 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
1cdcd 20 62 69 74 20 6f 66 20 50 35 20 69 73 20 73 65   bit of P5 is se
1cdce 74 20 61 6e 64 20 65 69 74 68 65 72 20 72 65 67  t and either reg
1cdcf 28 50 31 29 20 6f 72 0a 2a 2a 20 72 65 67 28 50  (P1) or.** reg(P
1cdd0 33 29 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20  3) is NULL then 
1cdd1 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 2e 20 20  take the jump.  
1cdd2 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55  If the SQLITE_JU
1cdd3 4d 50 49 46 4e 55 4c 4c 20 0a 2a 2a 20 62 69 74  MPIFNULL .** bit
1cdd4 20 69 73 20 63 6c 65 61 72 20 74 68 65 6e 20 66   is clear then f
1cdd5 61 6c 6c 20 74 68 72 75 20 69 66 20 65 69 74 68  all thru if eith
1cdd6 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55  er operand is NU
1cdd7 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51  LL..**.** The SQ
1cdd8 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 70 6f  LITE_AFF_MASK po
1cdd9 72 74 69 6f 6e 20 6f 66 20 50 35 20 6d 75 73 74  rtion of P5 must
1cdda 20 62 65 20 61 6e 20 61 66 66 69 6e 69 74 79 20   be an affinity 
1cddb 63 68 61 72 61 63 74 65 72 20 2d 0a 2a 2a 20 53  character -.** S
1cddc 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20  QLITE_AFF_TEXT, 
1cddd 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47  SQLITE_AFF_INTEG
1cdde 45 52 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68  ER, and so forth
1cddf 2e 20 41 6e 20 61 74 74 65 6d 70 74 20 69 73 20  . An attempt is 
1cde0 6d 61 64 65 20 0a 2a 2a 20 74 6f 20 63 6f 65 72  made .** to coer
1cde1 63 65 20 62 6f 74 68 20 69 6e 70 75 74 73 20 61  ce both inputs a
1cde2 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73  ccording to this
1cde3 20 61 66 66 69 6e 69 74 79 20 62 65 66 6f 72 65   affinity before
1cde4 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 61 72 69 73   the.** comparis
1cde5 6f 6e 20 69 73 20 6d 61 64 65 2e 20 49 66 20 74  on is made. If t
1cde6 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41  he SQLITE_AFF_MA
1cde7 53 4b 20 69 73 20 30 78 30 30 2c 20 74 68 65 6e  SK is 0x00, then
1cde8 20 6e 75 6d 65 72 69 63 0a 2a 2a 20 61 66 66 69   numeric.** affi
1cde9 6e 69 74 79 20 69 73 20 75 73 65 64 2e 20 4e 6f  nity is used. No
1cdea 74 65 20 74 68 61 74 20 74 68 65 20 61 66 66 69  te that the affi
1cdeb 6e 69 74 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73  nity conversions
1cdec 20 61 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 62   are stored.** b
1cded 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 69 6e 70  ack into the inp
1cdee 75 74 20 72 65 67 69 73 74 65 72 73 20 50 31 20  ut registers P1 
1cdef 61 6e 64 20 50 33 2e 20 20 53 6f 20 74 68 69 73  and P3.  So this
1cdf0 20 6f 70 63 6f 64 65 20 63 61 6e 20 63 61 75 73   opcode can caus
1cdf1 65 0a 2a 2a 20 70 65 72 73 69 73 74 65 6e 74 20  e.** persistent 
1cdf2 63 68 61 6e 67 65 73 20 74 6f 20 72 65 67 69 73  changes to regis
1cdf3 74 65 72 73 20 50 31 20 61 6e 64 20 50 33 2e 0a  ters P1 and P3..
1cdf4 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 61 6e 79 20 63  **.** Once any c
1cdf5 6f 6e 76 65 72 73 69 6f 6e 73 20 68 61 76 65 20  onversions have 
1cdf6 74 61 6b 65 6e 20 70 6c 61 63 65 2c 20 61 6e 64  taken place, and
1cdf7 20 6e 65 69 74 68 65 72 20 76 61 6c 75 65 20 69   neither value i
1cdf8 73 20 4e 55 4c 4c 2c 20 0a 2a 2a 20 74 68 65 20  s NULL, .** the 
1cdf9 76 61 6c 75 65 73 20 61 72 65 20 63 6f 6d 70 61  values are compa
1cdfa 72 65 64 2e 20 49 66 20 62 6f 74 68 20 76 61 6c  red. If both val
1cdfb 75 65 73 20 61 72 65 20 62 6c 6f 62 73 20 74 68  ues are blobs th
1cdfc 65 6e 20 6d 65 6d 63 6d 70 28 29 20 69 73 0a 2a  en memcmp() is.*
1cdfd 2a 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  * used to determ
1cdfe 69 6e 65 20 74 68 65 20 72 65 73 75 6c 74 73 20  ine the results 
1cdff 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  of the compariso
1ce00 6e 2e 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75  n.  If both valu
1ce01 65 73 0a 2a 2a 20 61 72 65 20 74 65 78 74 2c 20  es.** are text, 
1ce02 74 68 65 6e 20 74 68 65 20 61 70 70 72 6f 70 72  then the appropr
1ce03 69 61 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66  iate collating f
1ce04 75 6e 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65  unction specifie
1ce05 64 20 69 6e 0a 2a 2a 20 50 34 20 69 73 20 20 75  d in.** P4 is  u
1ce06 73 65 64 20 74 6f 20 64 6f 20 74 68 65 20 63 6f  sed to do the co
1ce07 6d 70 61 72 69 73 6f 6e 2e 20 20 49 66 20 50 34  mparison.  If P4
1ce08 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65   is not specifie
1ce09 64 20 74 68 65 6e 0a 2a 2a 20 6d 65 6d 63 6d 70  d then.** memcmp
1ce0a 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f  () is used to co
1ce0b 6d 70 61 72 65 20 74 65 78 74 20 73 74 72 69 6e  mpare text strin
1ce0c 67 2e 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75  g.  If both valu
1ce0d 65 73 20 61 72 65 0a 2a 2a 20 6e 75 6d 65 72 69  es are.** numeri
1ce0e 63 2c 20 74 68 65 6e 20 61 20 6e 75 6d 65 72 69  c, then a numeri
1ce0f 63 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20  c comparison is 
1ce10 75 73 65 64 2e 20 49 66 20 74 68 65 20 74 77 6f  used. If the two
1ce11 20 76 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 6f   values.** are o
1ce12 66 20 64 69 66 66 65 72 65 6e 74 20 74 79 70 65  f different type
1ce13 73 2c 20 74 68 65 6e 20 6e 75 6d 62 65 72 73 20  s, then numbers 
1ce14 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c  are considered l
1ce15 65 73 73 20 74 68 61 6e 0a 2a 2a 20 73 74 72 69  ess than.** stri
1ce16 6e 67 73 20 61 6e 64 20 73 74 72 69 6e 67 73 20  ngs and strings 
1ce17 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c  are considered l
1ce18 65 73 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a  ess than blobs..
1ce19 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c  **.** If the SQL
1ce1a 49 54 45 5f 53 54 4f 52 45 50 32 20 62 69 74 20  ITE_STOREP2 bit 
1ce1b 6f 66 20 50 35 20 69 73 20 73 65 74 2c 20 74 68  of P5 is set, th
1ce1c 65 6e 20 64 6f 20 6e 6f 74 20 6a 75 6d 70 2e 20  en do not jump. 
1ce1d 20 49 6e 73 74 65 61 64 2c 0a 2a 2a 20 73 74 6f   Instead,.** sto
1ce1e 72 65 20 61 20 62 6f 6f 6c 65 61 6e 20 72 65 73  re a boolean res
1ce1f 75 6c 74 20 28 65 69 74 68 65 72 20 30 2c 20 6f  ult (either 0, o
1ce20 72 20 31 2c 20 6f 72 20 4e 55 4c 4c 29 20 69 6e  r 1, or NULL) in
1ce21 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f   register P2..*/
1ce22 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 20 50  ./* Opcode: Ne P
1ce23 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a  1 P2 P3 P4 P5.**
1ce24 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a  .** This works j
1ce25 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20  ust like the Lt 
1ce26 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 68  opcode except th
1ce27 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74  at the jump is t
1ce28 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 6f  aken if.** the o
1ce29 70 65 72 61 6e 64 73 20 69 6e 20 72 65 67 69 73  perands in regis
1ce2a 74 65 72 73 20 50 31 20 61 6e 64 20 50 33 20 61  ters P1 and P3 a
1ce2b 72 65 20 6e 6f 74 20 65 71 75 61 6c 2e 20 20 53  re not equal.  S
1ce2c 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65  ee the Lt opcode
1ce2d 20 66 6f 72 0a 2a 2a 20 61 64 64 69 74 69 6f 6e   for.** addition
1ce2e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  al information..
1ce2f 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 45 71  */./* Opcode: Eq
1ce30 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a   P1 P2 P3 P4 P5.
1ce31 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73  **.** This works
1ce32 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c   just like the L
1ce33 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20  t opcode except 
1ce34 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73  that the jump is
1ce35 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65   taken if.** the
1ce36 20 6f 70 65 72 61 6e 64 73 20 69 6e 20 72 65 67   operands in reg
1ce37 69 73 74 65 72 73 20 50 31 20 61 6e 64 20 50 33  isters P1 and P3
1ce38 20 61 72 65 20 65 71 75 61 6c 2e 0a 2a 2a 20 53   are equal..** S
1ce39 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65  ee the Lt opcode
1ce3a 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
1ce3b 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  information..*/.
1ce3c 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 65 20 50 31  /* Opcode: Le P1
1ce3d 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a   P2 P3 P4 P5.**.
1ce3e 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75  ** This works ju
1ce3f 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f  st like the Lt o
1ce40 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61  pcode except tha
1ce41 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61  t the jump is ta
1ce42 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f  ken if.** the co
1ce43 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65  ntent of registe
1ce44 72 20 50 33 20 69 73 20 6c 65 73 73 20 74 68 61  r P3 is less tha
1ce45 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68  n or equal to th
1ce46 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20  e content of.** 
1ce47 72 65 67 69 73 74 65 72 20 50 31 2e 20 20 53 65  register P1.  Se
1ce48 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20  e the Lt opcode 
1ce49 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  for additional i
1ce4a 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f  nformation..*/./
1ce4b 2a 20 4f 70 63 6f 64 65 3a 20 47 74 20 50 31 20  * Opcode: Gt P1 
1ce4c 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a  P2 P3 P4 P5.**.*
1ce4d 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73  * This works jus
1ce4e 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70  t like the Lt op
1ce4f 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 74  code except that
1ce50 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b   the jump is tak
1ce51 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6e  en if.** the con
1ce52 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72  tent of register
1ce53 20 50 33 20 69 73 20 67 72 65 61 74 65 72 20 74   P3 is greater t
1ce54 68 61 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  han the content 
1ce55 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50  of.** register P
1ce56 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f  1.  See the Lt o
1ce57 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69  pcode for additi
1ce58 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
1ce59 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1ce5a 47 65 20 50 31 20 50 32 20 50 33 20 50 34 20 50  Ge P1 P2 P3 P4 P
1ce5b 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72  5.**.** This wor
1ce5c 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65  ks just like the
1ce5d 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70   Lt opcode excep
1ce5e 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20  t that the jump 
1ce5f 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74  is taken if.** t
1ce60 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65  he content of re
1ce61 67 69 73 74 65 72 20 50 33 20 69 73 20 67 72 65  gister P3 is gre
1ce62 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75  ater than or equ
1ce63 61 6c 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e  al to the conten
1ce64 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72  t of.** register
1ce65 20 50 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74   P1.  See the Lt
1ce66 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69   opcode for addi
1ce67 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
1ce68 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 45  on..*/.case OP_E
1ce69 71 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  q:              
1ce6a 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 45   /* same as TK_E
1ce6b 51 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e  Q, jump, in1, in
1ce6c 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 3a  3 */.case OP_Ne:
1ce6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1ce6e 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 45 2c  * same as TK_NE,
1ce6f 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20   jump, in1, in3 
1ce70 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 74 3a 20 20  */.case OP_Lt:  
1ce71 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1ce72 73 61 6d 65 20 61 73 20 54 4b 5f 4c 54 2c 20 6a  same as TK_LT, j
1ce73 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f  ump, in1, in3 */
1ce74 0a 63 61 73 65 20 4f 50 5f 4c 65 3a 20 20 20 20  .case OP_Le:    
1ce75 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
1ce76 6d 65 20 61 73 20 54 4b 5f 4c 45 2c 20 6a 75 6d  me as TK_LE, jum
1ce77 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63  p, in1, in3 */.c
1ce78 61 73 65 20 4f 50 5f 47 74 3a 20 20 20 20 20 20  ase OP_Gt:      
1ce79 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
1ce7a 20 61 73 20 54 4b 5f 47 54 2c 20 6a 75 6d 70 2c   as TK_GT, jump,
1ce7b 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73   in1, in3 */.cas
1ce7c 65 20 4f 50 5f 47 65 3a 20 7b 20 20 20 20 20 20  e OP_Ge: {      
1ce7d 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1ce7e 73 20 54 4b 5f 47 45 2c 20 6a 75 6d 70 2c 20 69  s TK_GE, jump, i
1ce7f 6e 31 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30  n1, in3 */.#if 0
1ce80 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1ce81 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1ce82 75 2e 61 69 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  u.ai */.  int fl
1ce83 61 67 73 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a  ags;.  int res;.
1ce84 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b    char affinity;
1ce85 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1ce86 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1ce87 20 69 6e 74 6f 20 75 2e 61 69 20 2a 2f 0a 0a 20   into u.ai */.. 
1ce88 20 75 2e 61 69 2e 66 6c 61 67 73 20 3d 20 70 49   u.ai.flags = pI
1ce89 6e 31 2d 3e 66 6c 61 67 73 7c 70 49 6e 33 2d 3e  n1->flags|pIn3->
1ce8a 66 6c 61 67 73 3b 0a 0a 20 20 69 66 28 20 75 2e  flags;..  if( u.
1ce8b 61 69 2e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c  ai.flags&MEM_Nul
1ce8c 6c 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 65  l ){.    /* If e
1ce8d 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73  ither operand is
1ce8e 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 72   NULL then the r
1ce8f 65 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20  esult is always 
1ce90 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2a 20 54 68 65  NULL..    ** The
1ce91 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69   jump is taken i
1ce92 66 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d  f the SQLITE_JUM
1ce93 50 49 46 4e 55 4c 4c 20 62 69 74 20 69 73 20 73  PIFNULL bit is s
1ce94 65 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  et..    */.    i
1ce95 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c  f( pOp->p5 & SQL
1ce96 49 54 45 5f 53 54 4f 52 45 50 32 20 29 7b 0a 20  ITE_STOREP2 ){. 
1ce97 20 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e       pOut = &p->
1ce98 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20  aMem[pOp->p2];. 
1ce99 20 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46       MemSetTypeF
1ce9a 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75  lag(pOut, MEM_Nu
1ce9b 6c 6c 29 3b 0a 20 20 20 20 20 20 52 45 47 49 53  ll);.      REGIS
1ce9c 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
1ce9d 32 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 7d 65  2, pOut);.    }e
1ce9e 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70 35 20  lse if( pOp->p5 
1ce9f 26 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  & SQLITE_JUMPIFN
1cea0 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 70 63 20  ULL ){.      pc 
1cea1 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 20  = pOp->p2-1;.   
1cea2 20 7d 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20   }.    break;.  
1cea3 7d 0a 0a 20 20 75 2e 61 69 2e 61 66 66 69 6e 69  }..  u.ai.affini
1cea4 74 79 20 3d 20 70 4f 70 2d 3e 70 35 20 26 20 53  ty = pOp->p5 & S
1cea5 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 3b 0a  QLITE_AFF_MASK;.
1cea6 20 20 69 66 28 20 75 2e 61 69 2e 61 66 66 69 6e    if( u.ai.affin
1cea7 69 74 79 20 29 7b 0a 20 20 20 20 61 70 70 6c 79  ity ){.    apply
1cea8 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 75  Affinity(pIn1, u
1cea9 2e 61 69 2e 61 66 66 69 6e 69 74 79 2c 20 65 6e  .ai.affinity, en
1ceaa 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 61 70 70  coding);.    app
1ceab 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e 33 2c  lyAffinity(pIn3,
1ceac 20 75 2e 61 69 2e 61 66 66 69 6e 69 74 79 2c 20   u.ai.affinity, 
1cead 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 69  encoding);.    i
1ceae 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
1ceaf 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65  led ) goto no_me
1ceb0 6d 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  m;.  }..  assert
1ceb1 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
1ceb2 34 5f 43 4f 4c 4c 53 45 51 20 7c 7c 20 70 4f 70  4_COLLSEQ || pOp
1ceb3 2d 3e 70 34 2e 70 43 6f 6c 6c 3d 3d 30 20 29 3b  ->p4.pColl==0 );
1ceb4 0a 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49  .  ExpandBlob(pI
1ceb5 6e 31 29 3b 0a 20 20 45 78 70 61 6e 64 42 6c 6f  n1);.  ExpandBlo
1ceb6 62 28 70 49 6e 33 29 3b 0a 20 20 75 2e 61 69 2e  b(pIn3);.  u.ai.
1ceb7 72 65 73 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d  res = sqlite3Mem
1ceb8 43 6f 6d 70 61 72 65 28 70 49 6e 33 2c 20 70 49  Compare(pIn3, pI
1ceb9 6e 31 2c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c  n1, pOp->p4.pCol
1ceba 6c 29 3b 0a 20 20 73 77 69 74 63 68 28 20 70 4f  l);.  switch( pO
1cebb 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20  p->opcode ){.   
1cebc 20 63 61 73 65 20 4f 50 5f 45 71 3a 20 20 20 20   case OP_Eq:    
1cebd 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e  u.ai.res = u.ai.
1cebe 72 65 73 3d 3d 30 3b 20 20 20 20 20 62 72 65 61  res==0;     brea
1cebf 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4e  k;.    case OP_N
1cec0 65 3a 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d  e:    u.ai.res =
1cec1 20 75 2e 61 69 2e 72 65 73 21 3d 30 3b 20 20 20   u.ai.res!=0;   
1cec2 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
1cec3 65 20 4f 50 5f 4c 74 3a 20 20 20 20 75 2e 61 69  e OP_Lt:    u.ai
1cec4 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 73 3c  .res = u.ai.res<
1cec5 30 3b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  0;      break;. 
1cec6 20 20 20 63 61 73 65 20 4f 50 5f 4c 65 3a 20 20     case OP_Le:  
1cec7 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61    u.ai.res = u.a
1cec8 69 2e 72 65 73 3c 3d 30 3b 20 20 20 20 20 62 72  i.res<=0;     br
1cec9 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50  eak;.    case OP
1ceca 5f 47 74 3a 20 20 20 20 75 2e 61 69 2e 72 65 73  _Gt:    u.ai.res
1cecb 20 3d 20 75 2e 61 69 2e 72 65 73 3e 30 3b 20 20   = u.ai.res>0;  
1cecc 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64      break;.    d
1cecd 65 66 61 75 6c 74 3a 20 20 20 20 20 20 20 75 2e  efault:       u.
1cece 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65  ai.res = u.ai.re
1cecf 73 3e 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b  s>=0;     break;
1ced0 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4f 70 2d  .  }..  if( pOp-
1ced1 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 53 54 4f  >p5 & SQLITE_STO
1ced2 52 45 50 32 20 29 7b 0a 20 20 20 20 70 4f 75 74  REP2 ){.    pOut
1ced3 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
1ced4 3e 70 32 5d 3b 0a 20 20 20 20 4d 65 6d 53 65 74  >p2];.    MemSet
1ced5 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d  TypeFlag(pOut, M
1ced6 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 70 4f 75  EM_Int);.    pOu
1ced7 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 69 2e 72 65  t->u.i = u.ai.re
1ced8 73 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f  s;.    REGISTER_
1ced9 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70  TRACE(pOp->p2, p
1ceda 4f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  Out);.  }else if
1cedb 28 20 75 2e 61 69 2e 72 65 73 20 29 7b 0a 20 20  ( u.ai.res ){.  
1cedc 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31    pc = pOp->p2-1
1cedd 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1cede 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 65 72  ../* Opcode: Per
1cedf 6d 75 74 61 74 69 6f 6e 20 2a 20 2a 20 2a 20 50  mutation * * * P
1cee0 34 20 2a 0a 2a 2a 0a 2a 2a 20 53 65 74 20 74 68  4 *.**.** Set th
1cee1 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 75 73  e permutation us
1cee2 65 64 20 62 79 20 74 68 65 20 4f 50 5f 43 6f 6d  ed by the OP_Com
1cee3 70 61 72 65 20 6f 70 65 72 61 74 6f 72 20 74 6f  pare operator to
1cee4 20 62 65 20 74 68 65 20 61 72 72 61 79 0a 2a 2a   be the array.**
1cee5 20 6f 66 20 69 6e 74 65 67 65 72 73 20 69 6e 20   of integers in 
1cee6 50 34 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 65  P4..**.** The pe
1cee7 72 6d 75 74 61 74 69 6f 6e 20 69 73 20 6f 6e 6c  rmutation is onl
1cee8 79 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68  y valid until th
1cee9 65 20 6e 65 78 74 20 4f 50 5f 50 65 72 6d 75 74  e next OP_Permut
1ceea 61 74 69 6f 6e 2c 20 4f 50 5f 43 6f 6d 70 61 72  ation, OP_Compar
1ceeb 65 2c 0a 2a 2a 20 4f 50 5f 48 61 6c 74 2c 20 6f  e,.** OP_Halt, o
1ceec 72 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2e 20  r OP_ResultRow. 
1ceed 20 54 79 70 69 63 61 6c 6c 79 20 74 68 65 20 4f   Typically the O
1ceee 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 20 73 68  P_Permutation sh
1ceef 6f 75 6c 64 20 6f 63 63 75 72 0a 2a 2a 20 69 6d  ould occur.** im
1cef0 6d 65 64 69 61 74 65 6c 79 20 70 72 69 6f 72 20  mediately prior 
1cef1 74 6f 20 74 68 65 20 4f 50 5f 43 6f 6d 70 61 72  to the OP_Compar
1cef2 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 65  e..*/.case OP_Pe
1cef3 72 6d 75 74 61 74 69 6f 6e 3a 20 7b 0a 20 20 61  rmutation: {.  a
1cef4 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79  ssert( pOp->p4ty
1cef5 70 65 3d 3d 50 34 5f 49 4e 54 41 52 52 41 59 20  pe==P4_INTARRAY 
1cef6 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
1cef7 2d 3e 70 34 2e 61 69 20 29 3b 0a 20 20 61 50 65  ->p4.ai );.  aPe
1cef8 72 6d 75 74 65 20 3d 20 70 4f 70 2d 3e 70 34 2e  rmute = pOp->p4.
1cef9 61 69 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  ai;.  break;.}..
1cefa 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6d 70 61  /* Opcode: Compa
1cefb 72 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a  re P1 P2 P3 P4 *
1cefc 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74  .**.** Compare t
1cefd 6f 20 76 65 63 74 6f 72 73 20 6f 66 20 72 65 67  o vectors of reg
1cefe 69 73 74 65 72 73 20 69 6e 20 72 65 67 28 50 31  isters in reg(P1
1ceff 29 2e 2e 72 65 67 28 50 31 2b 50 33 2d 31 29 20  )..reg(P1+P3-1) 
1cf00 28 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 6f 6e 65  (all this.** one
1cf01 20 22 41 22 29 20 61 6e 64 20 69 6e 20 72 65 67   "A") and in reg
1cf02 28 50 32 29 2e 2e 72 65 67 28 50 32 2b 50 33 2d  (P2)..reg(P2+P3-
1cf03 31 29 20 28 22 42 22 29 2e 20 20 53 61 76 65 20  1) ("B").  Save 
1cf04 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a  the result of.**
1cf05 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20   the comparison 
1cf06 66 6f 72 20 75 73 65 20 62 79 20 74 68 65 20 6e  for use by the n
1cf07 65 78 74 20 4f 50 5f 4a 75 6d 70 20 69 6e 73 74  ext OP_Jump inst
1cf08 72 75 63 74 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69  ruct..**.** P4 i
1cf09 73 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  s a KeyInfo stru
1cf0a 63 74 75 72 65 20 74 68 61 74 20 64 65 66 69 6e  cture that defin
1cf0b 65 73 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  es collating seq
1cf0c 75 65 6e 63 65 73 20 61 6e 64 20 73 6f 72 74 0a  uences and sort.
1cf0d 2a 2a 20 6f 72 64 65 72 73 20 66 6f 72 20 74 68  ** orders for th
1cf0e 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 54  e comparison.  T
1cf0f 68 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 61  he permutation a
1cf10 70 70 6c 69 65 73 20 74 6f 20 72 65 67 69 73 74  pplies to regist
1cf11 65 72 73 0a 2a 2a 20 6f 6e 6c 79 2e 20 20 54 68  ers.** only.  Th
1cf12 65 20 4b 65 79 49 6e 66 6f 20 65 6c 65 6d 65 6e  e KeyInfo elemen
1cf13 74 73 20 61 72 65 20 75 73 65 64 20 73 65 71 75  ts are used sequ
1cf14 65 6e 74 69 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20  entially..**.** 
1cf15 54 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69  The comparison i
1cf16 73 20 61 20 73 6f 72 74 20 63 6f 6d 70 61 72 69  s a sort compari
1cf17 73 6f 6e 2c 20 73 6f 20 4e 55 4c 4c 73 20 63 6f  son, so NULLs co
1cf18 6d 70 61 72 65 20 65 71 75 61 6c 2c 0a 2a 2a 20  mpare equal,.** 
1cf19 4e 55 4c 4c 73 20 61 72 65 20 6c 65 73 73 20 74  NULLs are less t
1cf1a 68 61 6e 20 6e 75 6d 62 65 72 73 2c 20 6e 75 6d  han numbers, num
1cf1b 62 65 72 73 20 61 72 65 20 6c 65 73 73 20 74 68  bers are less th
1cf1c 61 6e 20 73 74 72 69 6e 67 73 2c 0a 2a 2a 20 61  an strings,.** a
1cf1d 6e 64 20 73 74 72 69 6e 67 73 20 61 72 65 20 6c  nd strings are l
1cf1e 65 73 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a  ess than blobs..
1cf1f 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6d 70 61  */.case OP_Compa
1cf20 72 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20  re: {.#if 0  /* 
1cf21 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1cf22 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6a 20  moved into u.aj 
1cf23 2a 2f 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 69 6e  */.  int n;.  in
1cf24 74 20 69 3b 0a 20 20 69 6e 74 20 70 31 3b 0a 20  t i;.  int p1;. 
1cf25 20 69 6e 74 20 70 32 3b 0a 20 20 63 6f 6e 73 74   int p2;.  const
1cf26 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
1cf27 66 6f 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20  fo;.  int idx;. 
1cf28 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
1cf29 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67      /* Collating
1cf2a 20 73 65 71 75 65 6e 63 65 20 74 6f 20 75 73 65   sequence to use
1cf2b 20 6f 6e 20 74 68 69 73 20 74 65 72 6d 20 2a 2f   on this term */
1cf2c 0a 20 20 69 6e 74 20 62 52 65 76 3b 20 20 20 20  .  int bRev;    
1cf2d 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f        /* True fo
1cf2e 72 20 44 45 53 43 45 4e 44 49 4e 47 20 73 6f 72  r DESCENDING sor
1cf2f 74 20 6f 72 64 65 72 20 2a 2f 0a 23 65 6e 64 69  t order */.#endi
1cf30 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1cf31 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1cf32 75 2e 61 6a 20 2a 2f 0a 0a 20 20 75 2e 61 6a 2e  u.aj */..  u.aj.
1cf33 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 75  n = pOp->p3;.  u
1cf34 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70  .aj.pKeyInfo = p
1cf35 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b  Op->p4.pKeyInfo;
1cf36 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6a 2e  .  assert( u.aj.
1cf37 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  n>0 );.  assert(
1cf38 20 75 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f 21 3d   u.aj.pKeyInfo!=
1cf39 30 20 29 3b 0a 20 20 75 2e 61 6a 2e 70 31 20 3d  0 );.  u.aj.p1 =
1cf3a 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73 65   pOp->p1;.  asse
1cf3b 72 74 28 20 75 2e 61 6a 2e 70 31 3e 30 20 26 26  rt( u.aj.p1>0 &&
1cf3c 20 75 2e 61 6a 2e 70 31 2b 75 2e 61 6a 2e 6e 3c   u.aj.p1+u.aj.n<
1cf3d 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20  =p->nMem+1 );.  
1cf3e 75 2e 61 6a 2e 70 32 20 3d 20 70 4f 70 2d 3e 70  u.aj.p2 = pOp->p
1cf3f 32 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61  2;.  assert( u.a
1cf40 6a 2e 70 32 3e 30 20 26 26 20 75 2e 61 6a 2e 70  j.p2>0 && u.aj.p
1cf41 32 2b 75 2e 61 6a 2e 6e 3c 3d 70 2d 3e 6e 4d 65  2+u.aj.n<=p->nMe
1cf42 6d 2b 31 20 29 3b 0a 20 20 66 6f 72 28 75 2e 61  m+1 );.  for(u.a
1cf43 6a 2e 69 3d 30 3b 20 75 2e 61 6a 2e 69 3c 75 2e  j.i=0; u.aj.i<u.
1cf44 61 6a 2e 6e 3b 20 75 2e 61 6a 2e 69 2b 2b 29 7b  aj.n; u.aj.i++){
1cf45 0a 20 20 20 20 75 2e 61 6a 2e 69 64 78 20 3d 20  .    u.aj.idx = 
1cf46 61 50 65 72 6d 75 74 65 20 3f 20 61 50 65 72 6d  aPermute ? aPerm
1cf47 75 74 65 5b 75 2e 61 6a 2e 69 5d 20 3a 20 75 2e  ute[u.aj.i] : u.
1cf48 61 6a 2e 69 3b 0a 20 20 20 20 52 45 47 49 53 54  aj.i;.    REGIST
1cf49 45 52 5f 54 52 41 43 45 28 75 2e 61 6a 2e 70 31  ER_TRACE(u.aj.p1
1cf4a 2b 75 2e 61 6a 2e 69 64 78 2c 20 26 70 2d 3e 61  +u.aj.idx, &p->a
1cf4b 4d 65 6d 5b 75 2e 61 6a 2e 70 31 2b 75 2e 61 6a  Mem[u.aj.p1+u.aj
1cf4c 2e 69 64 78 5d 29 3b 0a 20 20 20 20 52 45 47 49  .idx]);.    REGI
1cf4d 53 54 45 52 5f 54 52 41 43 45 28 75 2e 61 6a 2e  STER_TRACE(u.aj.
1cf4e 70 32 2b 75 2e 61 6a 2e 69 64 78 2c 20 26 70 2d  p2+u.aj.idx, &p-
1cf4f 3e 61 4d 65 6d 5b 75 2e 61 6a 2e 70 32 2b 75 2e  >aMem[u.aj.p2+u.
1cf50 61 6a 2e 69 64 78 5d 29 3b 0a 20 20 20 20 61 73  aj.idx]);.    as
1cf51 73 65 72 74 28 20 75 2e 61 6a 2e 69 3c 75 2e 61  sert( u.aj.i<u.a
1cf52 6a 2e 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65  j.pKeyInfo->nFie
1cf53 6c 64 20 29 3b 0a 20 20 20 20 75 2e 61 6a 2e 70  ld );.    u.aj.p
1cf54 43 6f 6c 6c 20 3d 20 75 2e 61 6a 2e 70 4b 65 79  Coll = u.aj.pKey
1cf55 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 75 2e 61 6a  Info->aColl[u.aj
1cf56 2e 69 5d 3b 0a 20 20 20 20 75 2e 61 6a 2e 62 52  .i];.    u.aj.bR
1cf57 65 76 20 3d 20 75 2e 61 6a 2e 70 4b 65 79 49 6e  ev = u.aj.pKeyIn
1cf58 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 75  fo->aSortOrder[u
1cf59 2e 61 6a 2e 69 5d 3b 0a 20 20 20 20 69 43 6f 6d  .aj.i];.    iCom
1cf5a 70 61 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 65  pare = sqlite3Me
1cf5b 6d 43 6f 6d 70 61 72 65 28 26 70 2d 3e 61 4d 65  mCompare(&p->aMe
1cf5c 6d 5b 75 2e 61 6a 2e 70 31 2b 75 2e 61 6a 2e 69  m[u.aj.p1+u.aj.i
1cf5d 64 78 5d 2c 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e  dx], &p->aMem[u.
1cf5e 61 6a 2e 70 32 2b 75 2e 61 6a 2e 69 64 78 5d 2c  aj.p2+u.aj.idx],
1cf5f 20 75 2e 61 6a 2e 70 43 6f 6c 6c 29 3b 0a 20 20   u.aj.pColl);.  
1cf60 20 20 69 66 28 20 69 43 6f 6d 70 61 72 65 20 29    if( iCompare )
1cf61 7b 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 6a  {.      if( u.aj
1cf62 2e 62 52 65 76 20 29 20 69 43 6f 6d 70 61 72 65  .bRev ) iCompare
1cf63 20 3d 20 2d 69 43 6f 6d 70 61 72 65 3b 0a 20 20   = -iCompare;.  
1cf64 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1cf65 0a 20 20 7d 0a 20 20 61 50 65 72 6d 75 74 65 20  .  }.  aPermute 
1cf66 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  = 0;.  break;.}.
1cf67 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4a 75 6d 70  ./* Opcode: Jump
1cf68 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a   P1 P2 P3 * *.**
1cf69 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 20  .** Jump to the 
1cf6a 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 74 20 61  instruction at a
1cf6b 64 64 72 65 73 73 20 50 31 2c 20 50 32 2c 20 6f  ddress P1, P2, o
1cf6c 72 20 50 33 20 64 65 70 65 6e 64 69 6e 67 20 6f  r P3 depending o
1cf6d 6e 20 77 68 65 74 68 65 72 0a 2a 2a 20 69 6e 20  n whether.** in 
1cf6e 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
1cf6f 4f 50 5f 43 6f 6d 70 61 72 65 20 69 6e 73 74 72  OP_Compare instr
1cf70 75 63 74 69 6f 6e 20 74 68 65 20 50 31 20 76 65  uction the P1 ve
1cf71 63 74 6f 72 20 77 61 73 20 6c 65 73 73 20 74 68  ctor was less th
1cf72 61 6e 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 2c 20  an.** equal to, 
1cf73 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  or greater than 
1cf74 74 68 65 20 50 32 20 76 65 63 74 6f 72 2c 20 72  the P2 vector, r
1cf75 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2f 0a  espectively..*/.
1cf76 63 61 73 65 20 4f 50 5f 4a 75 6d 70 3a 20 7b 20  case OP_Jump: { 
1cf77 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a              /* j
1cf78 75 6d 70 20 2a 2f 0a 20 20 69 66 28 20 69 43 6f  ump */.  if( iCo
1cf79 6d 70 61 72 65 3c 30 20 29 7b 0a 20 20 20 20 70  mpare<0 ){.    p
1cf7a 63 20 3d 20 70 4f 70 2d 3e 70 31 20 2d 20 31 3b  c = pOp->p1 - 1;
1cf7b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 43 6f  .  }else if( iCo
1cf7c 6d 70 61 72 65 3d 3d 30 20 29 7b 0a 20 20 20 20  mpare==0 ){.    
1cf7d 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
1cf7e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
1cf7f 63 20 3d 20 70 4f 70 2d 3e 70 33 20 2d 20 31 3b  c = pOp->p3 - 1;
1cf80 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1cf81 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 6e 64 20  ./* Opcode: And 
1cf82 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
1cf83 2a 2a 20 54 61 6b 65 20 74 68 65 20 6c 6f 67 69  ** Take the logi
1cf84 63 61 6c 20 41 4e 44 20 6f 66 20 74 68 65 20 76  cal AND of the v
1cf85 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65  alues in registe
1cf86 72 73 20 50 31 20 61 6e 64 20 50 32 20 61 6e 64  rs P1 and P2 and
1cf87 0a 2a 2a 20 77 72 69 74 65 20 74 68 65 20 72 65  .** write the re
1cf88 73 75 6c 74 20 69 6e 74 6f 20 72 65 67 69 73 74  sult into regist
1cf89 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  er P3..**.** If 
1cf8a 65 69 74 68 65 72 20 50 31 20 6f 72 20 50 32 20  either P1 or P2 
1cf8b 69 73 20 30 20 28 66 61 6c 73 65 29 20 74 68 65  is 0 (false) the
1cf8c 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  n the result is 
1cf8d 30 20 65 76 65 6e 20 69 66 0a 2a 2a 20 74 68 65  0 even if.** the
1cf8e 20 6f 74 68 65 72 20 69 6e 70 75 74 20 69 73 20   other input is 
1cf8f 4e 55 4c 4c 2e 20 20 41 20 4e 55 4c 4c 20 61 6e  NULL.  A NULL an
1cf90 64 20 74 72 75 65 20 6f 72 20 74 77 6f 20 4e 55  d true or two NU
1cf91 4c 4c 73 20 67 69 76 65 0a 2a 2a 20 61 20 4e 55  LLs give.** a NU
1cf92 4c 4c 20 6f 75 74 70 75 74 2e 0a 2a 2f 0a 2f 2a  LL output..*/./*
1cf93 20 4f 70 63 6f 64 65 3a 20 4f 72 20 50 31 20 50   Opcode: Or P1 P
1cf94 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54  2 P3 * *.**.** T
1cf95 61 6b 65 20 74 68 65 20 6c 6f 67 69 63 61 6c 20  ake the logical 
1cf96 4f 52 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73  OR of the values
1cf97 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1cf98 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74  and P2 and.** st
1cf99 6f 72 65 20 74 68 65 20 61 6e 73 77 65 72 20 69  ore the answer i
1cf9a 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  n register P3..*
1cf9b 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 50  *.** If either P
1cf9c 31 20 6f 72 20 50 32 20 69 73 20 6e 6f 6e 7a 65  1 or P2 is nonze
1cf9d 72 6f 20 28 74 72 75 65 29 20 74 68 65 6e 20 74  ro (true) then t
1cf9e 68 65 20 72 65 73 75 6c 74 20 69 73 20 31 20 28  he result is 1 (
1cf9f 74 72 75 65 29 0a 2a 2a 20 65 76 65 6e 20 69 66  true).** even if
1cfa0 20 74 68 65 20 6f 74 68 65 72 20 69 6e 70 75 74   the other input
1cfa1 20 69 73 20 4e 55 4c 4c 2e 20 20 41 20 4e 55 4c   is NULL.  A NUL
1cfa2 4c 20 61 6e 64 20 66 61 6c 73 65 20 6f 72 20 74  L and false or t
1cfa3 77 6f 20 4e 55 4c 4c 73 0a 2a 2a 20 67 69 76 65  wo NULLs.** give
1cfa4 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75 74 2e 0a   a NULL output..
1cfa5 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 6e 64 3a 20  */.case OP_And: 
1cfa6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1cfa7 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e 44 2c 20  same as TK_AND, 
1cfa8 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a  in1, in2, out3 *
1cfa9 2f 0a 63 61 73 65 20 4f 50 5f 4f 72 3a 20 7b 20  /.case OP_Or: { 
1cfaa 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
1cfab 61 6d 65 20 61 73 20 54 4b 5f 4f 52 2c 20 69 6e  ame as TK_OR, in
1cfac 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a  1, in2, out3 */.
1cfad 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1cfae 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1cfaf 69 6e 74 6f 20 75 2e 61 6b 20 2a 2f 0a 20 20 69  into u.ak */.  i
1cfb0 6e 74 20 76 31 3b 20 20 20 20 2f 2a 20 4c 65 66  nt v1;    /* Lef
1cfb1 74 20 6f 70 65 72 61 6e 64 3a 20 20 30 3d 3d 46  t operand:  0==F
1cfb2 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32  ALSE, 1==TRUE, 2
1cfb3 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c  ==UNKNOWN or NUL
1cfb4 4c 20 2a 2f 0a 20 20 69 6e 74 20 76 32 3b 20 20  L */.  int v2;  
1cfb5 20 20 2f 2a 20 52 69 67 68 74 20 6f 70 65 72 61    /* Right opera
1cfb6 6e 64 3a 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d  nd: 0==FALSE, 1=
1cfb7 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57  =TRUE, 2==UNKNOW
1cfb8 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 23 65 6e  N or NULL */.#en
1cfb9 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1cfba 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1cfbb 6f 20 75 2e 61 6b 20 2a 2f 0a 0a 20 20 69 66 28  o u.ak */..  if(
1cfbc 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d   pIn1->flags & M
1cfbd 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 75  EM_Null ){.    u
1cfbe 2e 61 6b 2e 76 31 20 3d 20 32 3b 0a 20 20 7d 65  .ak.v1 = 2;.  }e
1cfbf 6c 73 65 7b 0a 20 20 20 20 75 2e 61 6b 2e 76 31  lse{.    u.ak.v1
1cfc0 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e   = sqlite3VdbeIn
1cfc1 74 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 3b  tValue(pIn1)!=0;
1cfc2 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e 32 2d  .  }.  if( pIn2-
1cfc3 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  >flags & MEM_Nul
1cfc4 6c 20 29 7b 0a 20 20 20 20 75 2e 61 6b 2e 76 32  l ){.    u.ak.v2
1cfc5 20 3d 20 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 2;.  }else{. 
1cfc6 20 20 20 75 2e 61 6b 2e 76 32 20 3d 20 73 71 6c     u.ak.v2 = sql
1cfc7 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65  ite3VdbeIntValue
1cfc8 28 70 49 6e 32 29 21 3d 30 3b 0a 20 20 7d 0a 20  (pIn2)!=0;.  }. 
1cfc9 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
1cfca 3d 3d 4f 50 5f 41 6e 64 20 29 7b 0a 20 20 20 20  ==OP_And ){.    
1cfcb 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
1cfcc 69 67 6e 65 64 20 63 68 61 72 20 61 6e 64 5f 6c  igned char and_l
1cfcd 6f 67 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 30 2c  ogic[] = { 0, 0,
1cfce 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20   0, 0, 1, 2, 0, 
1cfcf 32 2c 20 32 20 7d 3b 0a 20 20 20 20 75 2e 61 6b  2, 2 };.    u.ak
1cfd0 2e 76 31 20 3d 20 61 6e 64 5f 6c 6f 67 69 63 5b  .v1 = and_logic[
1cfd1 75 2e 61 6b 2e 76 31 2a 33 2b 75 2e 61 6b 2e 76  u.ak.v1*3+u.ak.v
1cfd2 32 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  2];.  }else{.   
1cfd3 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
1cfd4 73 69 67 6e 65 64 20 63 68 61 72 20 6f 72 5f 6c  signed char or_l
1cfd5 6f 67 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 31 2c  ogic[] = { 0, 1,
1cfd6 20 32 2c 20 31 2c 20 31 2c 20 31 2c 20 32 2c 20   2, 1, 1, 1, 2, 
1cfd7 31 2c 20 32 20 7d 3b 0a 20 20 20 20 75 2e 61 6b  1, 2 };.    u.ak
1cfd8 2e 76 31 20 3d 20 6f 72 5f 6c 6f 67 69 63 5b 75  .v1 = or_logic[u
1cfd9 2e 61 6b 2e 76 31 2a 33 2b 75 2e 61 6b 2e 76 32  .ak.v1*3+u.ak.v2
1cfda 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 75 2e 61  ];.  }.  if( u.a
1cfdb 6b 2e 76 31 3d 3d 32 20 29 7b 0a 20 20 20 20 4d  k.v1==2 ){.    M
1cfdc 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f  emSetTypeFlag(pO
1cfdd 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20  ut, MEM_Null);. 
1cfde 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4f 75 74   }else{.    pOut
1cfdf 2d 3e 75 2e 69 20 3d 20 75 2e 61 6b 2e 76 31 3b  ->u.i = u.ak.v1;
1cfe0 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46  .    MemSetTypeF
1cfe1 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e  lag(pOut, MEM_In
1cfe2 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  t);.  }.  break;
1cfe3 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e  .}../* Opcode: N
1cfe4 6f 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  ot P1 P2 * * *.*
1cfe5 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74  *.** Interpret t
1cfe6 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1cfe7 73 74 65 72 20 50 31 20 61 73 20 61 20 62 6f 6f  ster P1 as a boo
1cfe8 6c 65 61 6e 20 76 61 6c 75 65 2e 20 20 53 74 6f  lean value.  Sto
1cfe9 72 65 20 74 68 65 0a 2a 2a 20 62 6f 6f 6c 65 61  re the.** boolea
1cfea 6e 20 63 6f 6d 70 6c 65 6d 65 6e 74 20 69 6e 20  n complement in 
1cfeb 72 65 67 69 73 74 65 72 20 50 32 2e 20 20 49 66  register P2.  If
1cfec 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1cfed 67 69 73 74 65 72 20 50 31 20 69 73 20 0a 2a 2a  gister P1 is .**
1cfee 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 20 4e 55   NULL, then a NU
1cfef 4c 4c 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  LL is stored in 
1cff0 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e  P2..*/.case OP_N
1cff1 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  ot: {           
1cff2 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1cff3 54 4b 5f 4e 4f 54 2c 20 69 6e 31 20 2a 2f 0a 20  TK_NOT, in1 */. 
1cff4 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d   pOut = &p->aMem
1cff5 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69 66 28  [pOp->p2];.  if(
1cff6 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d   pIn1->flags & M
1cff7 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73  EM_Null ){.    s
1cff8 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
1cff9 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 7d 65  Null(pOut);.  }e
1cffa 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
1cffb 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28  VdbeMemSetInt64(
1cffc 70 4f 75 74 2c 20 21 73 71 6c 69 74 65 33 56 64  pOut, !sqlite3Vd
1cffd 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29  beIntValue(pIn1)
1cffe 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  );.  }.  break;.
1cfff 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69  }../* Opcode: Bi
1d000 74 4e 6f 74 20 50 31 20 50 32 20 2a 20 2a 20 2a  tNot P1 P2 * * *
1d001 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74  .**.** Interpret
1d002 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
1d003 72 65 67 69 73 74 65 72 20 50 31 20 61 73 20 61  register P1 as a
1d004 6e 20 69 6e 74 65 67 65 72 2e 20 20 53 74 6f 72  n integer.  Stor
1d005 65 20 74 68 65 0a 2a 2a 20 6f 6e 65 73 2d 63 6f  e the.** ones-co
1d006 6d 70 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20  mplement of the 
1d007 50 31 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65  P1 value into re
1d008 67 69 73 74 65 72 20 50 32 2e 20 20 49 66 20 50  gister P2.  If P
1d009 31 20 68 6f 6c 64 73 0a 2a 2a 20 61 20 4e 55 4c  1 holds.** a NUL
1d00a 4c 20 74 68 65 6e 20 73 74 6f 72 65 20 61 20 4e  L then store a N
1d00b 55 4c 4c 20 69 6e 20 50 32 2e 0a 2a 2f 0a 63 61  ULL in P2..*/.ca
1d00c 73 65 20 4f 50 5f 42 69 74 4e 6f 74 3a 20 7b 20  se OP_BitNot: { 
1d00d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
1d00e 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54  ame as TK_BITNOT
1d00f 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 4f 75 74 20  , in1 */.  pOut 
1d010 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
1d011 70 32 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d  p2];.  if( pIn1-
1d012 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  >flags & MEM_Nul
1d013 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  l ){.    sqlite3
1d014 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70  VdbeMemSetNull(p
1d015 4f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Out);.  }else{. 
1d016 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1d017 6d 53 65 74 49 6e 74 36 34 28 70 4f 75 74 2c 20  mSetInt64(pOut, 
1d018 7e 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56  ~sqlite3VdbeIntV
1d019 61 6c 75 65 28 70 49 6e 31 29 29 3b 0a 20 20 7d  alue(pIn1));.  }
1d01a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1d01b 4f 70 63 6f 64 65 3a 20 49 66 20 50 31 20 50 32  Opcode: If P1 P2
1d01c 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75   P3 * *.**.** Ju
1d01d 6d 70 20 74 6f 20 50 32 20 69 66 20 74 68 65 20  mp to P2 if the 
1d01e 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
1d01f 72 20 50 31 20 69 73 20 74 72 75 65 2e 20 20 54  r P1 is true.  T
1d020 68 65 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 69  he value is.** i
1d021 73 20 63 6f 6e 73 69 64 65 72 65 64 20 74 72 75  s considered tru
1d022 65 20 69 66 20 69 74 20 69 73 20 6e 75 6d 65 72  e if it is numer
1d023 69 63 20 61 6e 64 20 6e 6f 6e 2d 7a 65 72 6f 2e  ic and non-zero.
1d024 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a    If the value.*
1d025 2a 20 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c 20  * in P1 is NULL 
1d026 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75  then take the ju
1d027 6d 70 20 69 66 20 50 33 20 69 73 20 74 72 75 65  mp if P3 is true
1d028 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1d029 49 66 4e 6f 74 20 50 31 20 50 32 20 50 33 20 2a  IfNot P1 P2 P3 *
1d02a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f   *.**.** Jump to
1d02b 20 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65   P2 if the value
1d02c 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1d02d 69 73 20 46 61 6c 73 65 2e 20 20 54 68 65 20 76  is False.  The v
1d02e 61 6c 75 65 20 69 73 0a 2a 2a 20 69 73 20 63 6f  alue is.** is co
1d02f 6e 73 69 64 65 72 65 64 20 74 72 75 65 20 69 66  nsidered true if
1d030 20 69 74 20 68 61 73 20 61 20 6e 75 6d 65 72 69   it has a numeri
1d031 63 20 76 61 6c 75 65 20 6f 66 20 7a 65 72 6f 2e  c value of zero.
1d032 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a    If the value.*
1d033 2a 20 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c 20  * in P1 is NULL 
1d034 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75  then take the ju
1d035 6d 70 20 69 66 20 50 33 20 69 73 20 74 72 75 65  mp if P3 is true
1d036 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 3a  ..*/.case OP_If:
1d037 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d038 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f   /* jump, in1 */
1d039 0a 63 61 73 65 20 4f 50 5f 49 66 4e 6f 74 3a 20  .case OP_IfNot: 
1d03a 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  {            /* 
1d03b 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 23 69 66  jump, in1 */.#if
1d03c 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1d03d 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1d03e 6f 20 75 2e 61 6c 20 2a 2f 0a 20 20 69 6e 74 20  o u.al */.  int 
1d03f 63 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  c;.#endif /* loc
1d040 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1d041 65 64 20 69 6e 74 6f 20 75 2e 61 6c 20 2a 2f 0a  ed into u.al */.
1d042 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67    if( pIn1->flag
1d043 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a  s & MEM_Null ){.
1d044 20 20 20 20 75 2e 61 6c 2e 63 20 3d 20 70 4f 70      u.al.c = pOp
1d045 2d 3e 70 33 3b 0a 20 20 7d 65 6c 73 65 7b 0a 23  ->p3;.  }else{.#
1d046 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
1d047 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
1d048 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d 20 73 71  .    u.al.c = sq
1d049 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
1d04a 65 28 70 49 6e 31 29 21 3d 30 3b 0a 23 65 6c 73  e(pIn1)!=0;.#els
1d04b 65 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d 20 73  e.    u.al.c = s
1d04c 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61  qlite3VdbeRealVa
1d04d 6c 75 65 28 70 49 6e 31 29 21 3d 30 2e 30 3b 0a  lue(pIn1)!=0.0;.
1d04e 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 70  #endif.    if( p
1d04f 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49  Op->opcode==OP_I
1d050 66 4e 6f 74 20 29 20 75 2e 61 6c 2e 63 20 3d 20  fNot ) u.al.c = 
1d051 21 75 2e 61 6c 2e 63 3b 0a 20 20 7d 0a 20 20 69  !u.al.c;.  }.  i
1d052 66 28 20 75 2e 61 6c 2e 63 20 29 7b 0a 20 20 20  f( u.al.c ){.   
1d053 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b   pc = pOp->p2-1;
1d054 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1d055 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 73 4e 75  ./* Opcode: IsNu
1d056 6c 6c 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  ll P1 P2 * * *.*
1d057 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20  *.** Jump to P2 
1d058 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  if the value in 
1d059 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 4e  register P1 is N
1d05a 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ULL..*/.case OP_
1d05b 49 73 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20  IsNull: {       
1d05c 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1d05d 54 4b 5f 49 53 4e 55 4c 4c 2c 20 6a 75 6d 70 2c  TK_ISNULL, jump,
1d05e 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70   in1 */.  if( (p
1d05f 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
1d060 5f 4e 75 6c 6c 29 21 3d 30 20 29 7b 0a 20 20 20  _Null)!=0 ){.   
1d061 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1d062 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  1;.  }.  break;.
1d063 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f  }../* Opcode: No
1d064 74 4e 75 6c 6c 20 50 31 20 50 32 20 2a 20 2a 20  tNull P1 P2 * * 
1d065 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20  *.**.** Jump to 
1d066 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 20  P2 if the value 
1d067 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69  in register P1 i
1d068 73 20 6e 6f 74 20 4e 55 4c 4c 2e 20 20 0a 2a 2f  s not NULL.  .*/
1d069 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c  .case OP_NotNull
1d06a 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f  : {            /
1d06b 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54  * same as TK_NOT
1d06c 4e 55 4c 4c 2c 20 6a 75 6d 70 2c 20 69 6e 31 20  NULL, jump, in1 
1d06d 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e  */.  if( (pIn1->
1d06e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
1d06f 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 20 3d  )==0 ){.    pc =
1d070 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
1d071 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1d072 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 75 6d 6e 20   Opcode: Column 
1d073 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a  P1 P2 P3 P4 P5.*
1d074 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74  *.** Interpret t
1d075 68 65 20 64 61 74 61 20 74 68 61 74 20 63 75 72  he data that cur
1d076 73 6f 72 20 50 31 20 70 6f 69 6e 74 73 20 74 6f  sor P1 points to
1d077 20 61 73 20 61 20 73 74 72 75 63 74 75 72 65 20   as a structure 
1d078 62 75 69 6c 74 20 75 73 69 6e 67 0a 2a 2a 20 74  built using.** t
1d079 68 65 20 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e  he MakeRecord in
1d07a 73 74 72 75 63 74 69 6f 6e 2e 20 20 28 53 65 65  struction.  (See
1d07b 20 74 68 65 20 4d 61 6b 65 52 65 63 6f 72 64 20   the MakeRecord 
1d07c 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74  opcode for addit
1d07d 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61  ional.** informa
1d07e 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66  tion about the f
1d07f 6f 72 6d 61 74 20 6f 66 20 74 68 65 20 64 61 74  ormat of the dat
1d080 61 2e 29 20 20 45 78 74 72 61 63 74 20 74 68 65  a.)  Extract the
1d081 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e 0a 2a 2a   P2-th column.**
1d082 20 66 72 6f 6d 20 74 68 69 73 20 72 65 63 6f 72   from this recor
1d083 64 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 65  d.  If there are
1d084 20 6c 65 73 73 20 74 68 61 74 20 28 50 32 2b 31   less that (P2+1
1d085 29 20 0a 2a 2a 20 76 61 6c 75 65 73 20 69 6e 20  ) .** values in 
1d086 74 68 65 20 72 65 63 6f 72 64 2c 20 65 78 74 72  the record, extr
1d087 61 63 74 20 61 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  act a NULL..**.*
1d088 2a 20 54 68 65 20 76 61 6c 75 65 20 65 78 74 72  * The value extr
1d089 61 63 74 65 64 20 69 73 20 73 74 6f 72 65 64 20  acted is stored 
1d08a 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a  in register P3..
1d08b 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f 6c  **.** If the col
1d08c 75 6d 6e 20 63 6f 6e 74 61 69 6e 73 20 66 65 77  umn contains few
1d08d 65 72 20 74 68 61 6e 20 50 32 20 66 69 65 6c 64  er than P2 field
1d08e 73 2c 20 74 68 65 6e 20 65 78 74 72 61 63 74 20  s, then extract 
1d08f 61 20 4e 55 4c 4c 2e 20 20 4f 72 2c 0a 2a 2a 20  a NULL.  Or,.** 
1d090 69 66 20 74 68 65 20 50 34 20 61 72 67 75 6d 65  if the P4 argume
1d091 6e 74 20 69 73 20 61 20 50 34 5f 4d 45 4d 20 75  nt is a P4_MEM u
1d092 73 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  se the value of 
1d093 74 68 65 20 50 34 20 61 72 67 75 6d 65 6e 74 20  the P4 argument 
1d094 61 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74  as.** the result
1d095 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f  ..**.** If the O
1d096 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48 45  PFLAG_CLEARCACHE
1d097 20 62 69 74 20 69 73 20 73 65 74 20 6f 6e 20 50   bit is set on P
1d098 35 20 61 6e 64 20 50 31 20 69 73 20 61 20 70 73  5 and P1 is a ps
1d099 65 75 64 6f 2d 74 61 62 6c 65 20 63 75 72 73 6f  eudo-table curso
1d09a 72 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 63  r,.** then the c
1d09b 61 63 68 65 20 6f 66 20 74 68 65 20 63 75 72 73  ache of the curs
1d09c 6f 72 20 69 73 20 72 65 73 65 74 20 70 72 69 6f  or is reset prio
1d09d 72 20 74 6f 20 65 78 74 72 61 63 74 69 6e 67 20  r to extracting 
1d09e 74 68 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 54  the column..** T
1d09f 68 65 20 66 69 72 73 74 20 4f 50 5f 43 6f 6c 75  he first OP_Colu
1d0a0 6d 6e 20 61 67 61 69 6e 73 74 20 61 20 70 73 65  mn against a pse
1d0a1 75 64 6f 2d 74 61 62 6c 65 20 61 66 74 65 72 20  udo-table after 
1d0a2 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
1d0a3 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 72 65 67 69   content.** regi
1d0a4 73 74 65 72 20 68 61 73 20 63 68 61 6e 67 65 64  ster has changed
1d0a5 20 73 68 6f 75 6c 64 20 68 61 76 65 20 74 68 69   should have thi
1d0a6 73 20 62 69 74 20 73 65 74 2e 0a 2a 2f 0a 63 61  s bit set..*/.ca
1d0a7 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 3a 20 7b 0a  se OP_Column: {.
1d0a8 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1d0a9 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1d0aa 69 6e 74 6f 20 75 2e 61 6d 20 2a 2f 0a 20 20 75  into u.am */.  u
1d0ab 33 32 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b 20  32 payloadSize; 
1d0ac 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
1d0ad 79 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f  ytes in the reco
1d0ae 72 64 20 2a 2f 0a 20 20 69 36 34 20 70 61 79 6c  rd */.  i64 payl
1d0af 6f 61 64 53 69 7a 65 36 34 3b 20 2f 2a 20 4e 75  oadSize64; /* Nu
1d0b0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
1d0b1 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20   the record */. 
1d0b2 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20   int p1;        
1d0b3 20 20 20 20 2f 2a 20 50 31 20 76 61 6c 75 65 20      /* P1 value 
1d0b4 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 20 2a 2f  of the opcode */
1d0b5 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20  .  int p2;      
1d0b6 20 20 20 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20        /* column 
1d0b7 6e 75 6d 62 65 72 20 74 6f 20 72 65 74 72 69 65  number to retrie
1d0b8 76 65 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73  ve */.  VdbeCurs
1d0b9 6f 72 20 2a 70 43 3b 20 20 20 20 2f 2a 20 54 68  or *pC;    /* Th
1d0ba 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 2a 2f  e VDBE cursor */
1d0bb 0a 20 20 63 68 61 72 20 2a 7a 52 65 63 3b 20 20  .  char *zRec;  
1d0bc 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
1d0bd 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 72 65 63   to complete rec
1d0be 6f 72 64 2d 64 61 74 61 20 2a 2f 0a 20 20 42 74  ord-data */.  Bt
1d0bf 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 20 20  Cursor *pCrsr;  
1d0c0 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 63 75   /* The BTree cu
1d0c1 72 73 6f 72 20 2a 2f 0a 20 20 75 33 32 20 2a 61  rsor */.  u32 *a
1d0c2 54 79 70 65 3b 20 20 20 20 20 20 20 20 2f 2a 20  Type;        /* 
1d0c3 61 54 79 70 65 5b 69 5d 20 68 6f 6c 64 73 20 74  aType[i] holds t
1d0c4 68 65 20 6e 75 6d 65 72 69 63 20 74 79 70 65 20  he numeric type 
1d0c5 6f 66 20 74 68 65 20 69 2d 74 68 20 63 6f 6c 75  of the i-th colu
1d0c6 6d 6e 20 2a 2f 0a 20 20 75 33 32 20 2a 61 4f 66  mn */.  u32 *aOf
1d0c7 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 61 4f  fset;      /* aO
1d0c8 66 66 73 65 74 5b 69 5d 20 69 73 20 6f 66 66 73  ffset[i] is offs
1d0c9 65 74 20 74 6f 20 73 74 61 72 74 20 6f 66 20 64  et to start of d
1d0ca 61 74 61 20 66 6f 72 20 69 2d 74 68 20 63 6f 6c  ata for i-th col
1d0cb 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69  umn */.  int nFi
1d0cc 65 6c 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 6e  eld;        /* n
1d0cd 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 20  umber of fields 
1d0ce 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f  in the record */
1d0cf 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20  .  int len;     
1d0d0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 6e        /* The len
1d0d1 67 74 68 20 6f 66 20 74 68 65 20 73 65 72 69 61  gth of the seria
1d0d2 6c 69 7a 65 64 20 64 61 74 61 20 66 6f 72 20 74  lized data for t
1d0d3 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69  he column */.  i
1d0d4 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
1d0d5 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
1d0d6 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 61  r */.  char *zDa
1d0d7 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 72  ta;       /* Par
1d0d8 74 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20  t of the record 
1d0d9 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f  being decoded */
1d0da 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 20 20  .  Mem *pDest;  
1d0db 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74        /* Where t
1d0dc 6f 20 77 72 69 74 65 20 74 68 65 20 65 78 74 72  o write the extr
1d0dd 61 63 74 65 64 20 76 61 6c 75 65 20 2a 2f 0a 20  acted value */. 
1d0de 20 4d 65 6d 20 73 4d 65 6d 3b 20 20 20 20 20 20   Mem sMem;      
1d0df 20 20 20 20 2f 2a 20 46 6f 72 20 73 74 6f 72 69      /* For stori
1d0e0 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 62 65  ng the record be
1d0e1 69 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f 0a 20  ing decoded */. 
1d0e2 20 75 38 20 2a 7a 49 64 78 3b 20 20 20 20 20 20   u8 *zIdx;      
1d0e3 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74      /* Index int
1d0e4 6f 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 38  o header */.  u8
1d0e5 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20 20 20 20   *zEndHdr;      
1d0e6 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66   /* Pointer to f
1d0e7 69 72 73 74 20 62 79 74 65 20 61 66 74 65 72 20  irst byte after 
1d0e8 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  the header */.  
1d0e9 75 33 32 20 6f 66 66 73 65 74 3b 20 20 20 20 20  u32 offset;     
1d0ea 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74     /* Offset int
1d0eb 6f 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20  o the data */.  
1d0ec 75 36 34 20 6f 66 66 73 65 74 36 34 3b 20 20 20  u64 offset64;   
1d0ed 20 20 20 2f 2a 20 36 34 2d 62 69 74 20 6f 66 66     /* 64-bit off
1d0ee 73 65 74 2e 20 20 36 34 20 62 69 74 73 20 6e 65  set.  64 bits ne
1d0ef 65 64 65 64 20 74 6f 20 63 61 74 63 68 20 6f 76  eded to catch ov
1d0f0 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20  erflow */.  int 
1d0f1 73 7a 48 64 72 3b 20 20 20 20 20 20 20 20 20 2f  szHdr;         /
1d0f2 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 68 65  * Size of the he
1d0f3 61 64 65 72 20 73 69 7a 65 20 66 69 65 6c 64 20  ader size field 
1d0f4 61 74 20 73 74 61 72 74 20 6f 66 20 72 65 63 6f  at start of reco
1d0f5 72 64 20 2a 2f 0a 20 20 69 6e 74 20 61 76 61 69  rd */.  int avai
1d0f6 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  l;         /* Nu
1d0f7 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
1d0f8 20 61 76 61 69 6c 61 62 6c 65 20 64 61 74 61 20   available data 
1d0f9 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65 67 3b 20  */.  Mem *pReg; 
1d0fa 20 20 20 20 20 20 20 20 2f 2a 20 50 73 65 75 64          /* Pseud
1d0fb 6f 54 61 62 6c 65 20 69 6e 70 75 74 20 72 65 67  oTable input reg
1d0fc 69 73 74 65 72 20 2a 2f 0a 23 65 6e 64 69 66 20  ister */.#endif 
1d0fd 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1d0fe 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1d0ff 61 6d 20 2a 2f 0a 0a 0a 20 20 75 2e 61 6d 2e 70  am */...  u.am.p
1d100 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75  1 = pOp->p1;.  u
1d101 2e 61 6d 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32  .am.p2 = pOp->p2
1d102 3b 0a 20 20 75 2e 61 6d 2e 70 43 20 3d 20 30 3b  ;.  u.am.pC = 0;
1d103 0a 20 20 6d 65 6d 73 65 74 28 26 75 2e 61 6d 2e  .  memset(&u.am.
1d104 73 4d 65 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28  sMem, 0, sizeof(
1d105 75 2e 61 6d 2e 73 4d 65 6d 29 29 3b 0a 20 20 61  u.am.sMem));.  a
1d106 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 31 3c 70  ssert( u.am.p1<p
1d107 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61  ->nCursor );.  a
1d108 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30  ssert( pOp->p3>0
1d109 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e   && pOp->p3<=p->
1d10a 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 61 6d 2e 70  nMem );.  u.am.p
1d10b 44 65 73 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  Dest = &p->aMem[
1d10c 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 4d 65 6d 53  pOp->p3];.  MemS
1d10d 65 74 54 79 70 65 46 6c 61 67 28 75 2e 61 6d 2e  etTypeFlag(u.am.
1d10e 70 44 65 73 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29  pDest, MEM_Null)
1d10f 3b 0a 20 20 75 2e 61 6d 2e 7a 52 65 63 20 3d 20  ;.  u.am.zRec = 
1d110 30 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 62 6c  0;..  /* This bl
1d111 6f 63 6b 20 73 65 74 73 20 74 68 65 20 76 61 72  ock sets the var
1d112 69 61 62 6c 65 20 75 2e 61 6d 2e 70 61 79 6c 6f  iable u.am.paylo
1d113 61 64 53 69 7a 65 20 74 6f 20 62 65 20 74 68 65  adSize to be the
1d114 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
1d115 0a 20 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 74  .  ** bytes in t
1d116 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2a 0a  he record..  **.
1d117 20 20 2a 2a 20 75 2e 61 6d 2e 7a 52 65 63 20 69    ** u.am.zRec i
1d118 73 20 73 65 74 20 74 6f 20 62 65 20 74 68 65 20  s set to be the 
1d119 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66  complete text of
1d11a 20 74 68 65 20 72 65 63 6f 72 64 20 69 66 20 69   the record if i
1d11b 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a  t is available..
1d11c 20 20 2a 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74    ** The complet
1d11d 65 20 72 65 63 6f 72 64 20 74 65 78 74 20 69 73  e record text is
1d11e 20 61 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c   always availabl
1d11f 65 20 66 6f 72 20 70 73 65 75 64 6f 2d 74 61 62  e for pseudo-tab
1d120 6c 65 73 0a 20 20 2a 2a 20 49 66 20 74 68 65 20  les.  ** If the 
1d121 72 65 63 6f 72 64 20 69 73 20 73 74 6f 72 65 64  record is stored
1d122 20 69 6e 20 61 20 63 75 72 73 6f 72 2c 20 74 68   in a cursor, th
1d123 65 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72  e complete recor
1d124 64 20 74 65 78 74 0a 20 20 2a 2a 20 6d 69 67 68  d text.  ** migh
1d125 74 20 62 65 20 61 76 61 69 6c 61 62 6c 65 20 69  t be available i
1d126 6e 20 74 68 65 20 20 75 2e 61 6d 2e 70 43 2d 3e  n the  u.am.pC->
1d127 61 52 6f 77 20 63 61 63 68 65 2e 20 20 4f 72 20  aRow cache.  Or 
1d128 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 2e  it might not be.
1d129 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 64 61 74  .  ** If the dat
1d12a 61 20 69 73 20 75 6e 61 76 61 69 6c 61 62 6c 65  a is unavailable
1d12b 2c 20 20 75 2e 61 6d 2e 7a 52 65 63 20 69 73 20  ,  u.am.zRec is 
1d12c 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 2a  set to NULL..  *
1d12d 2a 0a 20 20 2a 2a 20 57 65 20 61 6c 73 6f 20 63  *.  ** We also c
1d12e 6f 6d 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65  ompute the numbe
1d12f 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
1d130 74 68 65 20 72 65 63 6f 72 64 2e 20 20 46 6f 72  the record.  For
1d131 20 63 75 72 73 6f 72 73 2c 0a 20 20 2a 2a 20 74   cursors,.  ** t
1d132 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
1d133 75 6d 6e 73 20 69 73 20 73 74 6f 72 65 64 20 69  umns is stored i
1d134 6e 20 74 68 65 20 56 64 62 65 43 75 72 73 6f 72  n the VdbeCursor
1d135 2e 6e 46 69 65 6c 64 20 65 6c 65 6d 65 6e 74 2e  .nField element.
1d136 0a 20 20 2a 2f 0a 20 20 75 2e 61 6d 2e 70 43 20  .  */.  u.am.pC 
1d137 3d 20 70 2d 3e 61 70 43 73 72 5b 75 2e 61 6d 2e  = p->apCsr[u.am.
1d138 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75  p1];.  assert( u
1d139 2e 61 6d 2e 70 43 21 3d 30 20 29 3b 0a 23 69 66  .am.pC!=0 );.#if
1d13a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1d13b 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
1d13c 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 43 2d  assert( u.am.pC-
1d13d 3e 70 56 74 61 62 43 75 72 73 6f 72 3d 3d 30 20  >pVtabCursor==0 
1d13e 29 3b 0a 23 65 6e 64 69 66 0a 20 20 75 2e 61 6d  );.#endif.  u.am
1d13f 2e 70 43 72 73 72 20 3d 20 75 2e 61 6d 2e 70 43  .pCrsr = u.am.pC
1d140 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28  ->pCursor;.  if(
1d141 20 75 2e 61 6d 2e 70 43 72 73 72 21 3d 30 20 29   u.am.pCrsr!=0 )
1d142 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63  {.    /* The rec
1d143 6f 72 64 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ord is stored in
1d144 20 61 20 42 2d 54 72 65 65 20 2a 2f 0a 20 20 20   a B-Tree */.   
1d145 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
1d146 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e  eCursorMoveto(u.
1d147 61 6d 2e 70 43 29 3b 0a 20 20 20 20 69 66 28 20  am.pC);.    if( 
1d148 72 63 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  rc ) goto abort_
1d149 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
1d14a 20 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 6e    if( u.am.pC->n
1d14b 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 20 20  ullRow ){.      
1d14c 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.am.payloadSize
1d14d 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20   = 0;.    }else 
1d14e 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 63 61 63  if( u.am.pC->cac
1d14f 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e 63 61 63  heStatus==p->cac
1d150 68 65 43 74 72 20 29 7b 0a 20 20 20 20 20 20 75  heCtr ){.      u
1d151 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20  .am.payloadSize 
1d152 3d 20 75 2e 61 6d 2e 70 43 2d 3e 70 61 79 6c 6f  = u.am.pC->paylo
1d153 61 64 53 69 7a 65 3b 0a 20 20 20 20 20 20 75 2e  adSize;.      u.
1d154 61 6d 2e 7a 52 65 63 20 3d 20 28 63 68 61 72 2a  am.zRec = (char*
1d155 29 75 2e 61 6d 2e 70 43 2d 3e 61 52 6f 77 3b 0a  )u.am.pC->aRow;.
1d156 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e      }else if( u.
1d157 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65 78 20 29  am.pC->isIndex )
1d158 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1d159 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
1d15a 6f 72 49 73 56 61 6c 69 64 28 75 2e 61 6d 2e 70  orIsValid(u.am.p
1d15b 43 72 73 72 29 20 29 3b 0a 20 20 20 20 20 20 72  Crsr) );.      r
1d15c 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1d15d 4b 65 79 53 69 7a 65 28 75 2e 61 6d 2e 70 43 72  KeySize(u.am.pCr
1d15e 73 72 2c 20 26 75 2e 61 6d 2e 70 61 79 6c 6f 61  sr, &u.am.payloa
1d15f 64 53 69 7a 65 36 34 29 3b 0a 20 20 20 20 20 20  dSize64);.      
1d160 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
1d161 54 45 5f 4f 4b 20 29 3b 20 20 20 2f 2a 20 54 72  TE_OK );   /* Tr
1d162 75 65 20 62 65 63 61 75 73 65 20 6f 66 20 43 75  ue because of Cu
1d163 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20 63 61 6c  rsorMoveto() cal
1d164 6c 20 61 62 6f 76 65 20 2a 2f 0a 20 20 20 20 20  l above */.     
1d165 20 2f 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65   /* sqlite3Btree
1d166 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 20 75  ParseCellPtr() u
1d167 73 65 73 20 67 65 74 56 61 72 69 6e 74 33 32 28  ses getVarint32(
1d168 29 20 74 6f 20 65 78 74 72 61 63 74 20 74 68 65  ) to extract the
1d169 0a 20 20 20 20 20 20 2a 2a 20 70 61 79 6c 6f 61  .      ** payloa
1d16a 64 20 73 69 7a 65 2c 20 73 6f 20 69 74 20 69 73  d size, so it is
1d16b 20 69 6d 70 6f 73 73 69 62 6c 65 20 66 6f 72 20   impossible for 
1d16c 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.am.payloadSize
1d16d 36 34 20 74 6f 20 62 65 0a 20 20 20 20 20 20 2a  64 to be.      *
1d16e 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 33 32  * larger than 32
1d16f 20 62 69 74 73 2e 20 2a 2f 0a 20 20 20 20 20 20   bits. */.      
1d170 61 73 73 65 72 74 28 20 28 75 2e 61 6d 2e 70 61  assert( (u.am.pa
1d171 79 6c 6f 61 64 53 69 7a 65 36 34 20 26 20 53 51  yloadSize64 & SQ
1d172 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29 3d 3d 28  LITE_MAX_U32)==(
1d173 75 36 34 29 75 2e 61 6d 2e 70 61 79 6c 6f 61 64  u64)u.am.payload
1d174 53 69 7a 65 36 34 20 29 3b 0a 20 20 20 20 20 20  Size64 );.      
1d175 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.am.payloadSize
1d176 20 3d 20 28 75 33 32 29 75 2e 61 6d 2e 70 61 79   = (u32)u.am.pay
1d177 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20 20 20 20  loadSize64;.    
1d178 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73  }else{.      ass
1d179 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
1d17a 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75  eCursorIsValid(u
1d17b 2e 61 6d 2e 70 43 72 73 72 29 20 29 3b 0a 20 20  .am.pCrsr) );.  
1d17c 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1d17d 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 75 2e  BtreeDataSize(u.
1d17e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e 61 6d 2e  am.pCrsr, &u.am.
1d17f 70 61 79 6c 6f 61 64 53 69 7a 65 29 3b 0a 20 20  payloadSize);.  
1d180 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
1d181 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 2f  SQLITE_OK );   /
1d182 2a 20 44 61 74 61 53 69 7a 65 28 29 20 63 61 6e  * DataSize() can
1d183 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 20 20  not fail */.    
1d184 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e  }.  }else if( u.
1d185 61 6d 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62  am.pC->pseudoTab
1d186 6c 65 52 65 67 3e 30 20 29 7b 0a 20 20 20 20 75  leReg>0 ){.    u
1d187 2e 61 6d 2e 70 52 65 67 20 3d 20 26 70 2d 3e 61  .am.pReg = &p->a
1d188 4d 65 6d 5b 75 2e 61 6d 2e 70 43 2d 3e 70 73 65  Mem[u.am.pC->pse
1d189 75 64 6f 54 61 62 6c 65 52 65 67 5d 3b 0a 20 20  udoTableReg];.  
1d18a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70    assert( u.am.p
1d18b 52 65 67 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Reg->flags & MEM
1d18c 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 75 2e 61  _Blob );.    u.a
1d18d 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20  m.payloadSize = 
1d18e 75 2e 61 6d 2e 70 52 65 67 2d 3e 6e 3b 0a 20 20  u.am.pReg->n;.  
1d18f 20 20 75 2e 61 6d 2e 7a 52 65 63 20 3d 20 75 2e    u.am.zRec = u.
1d190 61 6d 2e 70 52 65 67 2d 3e 7a 3b 0a 20 20 20 20  am.pReg->z;.    
1d191 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.am.pC->cacheSt
1d192 61 74 75 73 20 3d 20 28 70 4f 70 2d 3e 70 35 26  atus = (pOp->p5&
1d193 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48  OPFLAG_CLEARCACH
1d194 45 29 20 3f 20 43 41 43 48 45 5f 53 54 41 4c 45  E) ? CACHE_STALE
1d195 20 3a 20 70 2d 3e 63 61 63 68 65 43 74 72 3b 0a   : p->cacheCtr;.
1d196 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d      assert( u.am
1d197 2e 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d 30 20  .payloadSize==0 
1d198 7c 7c 20 75 2e 61 6d 2e 7a 52 65 63 21 3d 30 20  || u.am.zRec!=0 
1d199 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1d19a 2f 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 65 20  /* Consider the 
1d19b 72 6f 77 20 74 6f 20 62 65 20 4e 55 4c 4c 20 2a  row to be NULL *
1d19c 2f 0a 20 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f  /.    u.am.paylo
1d19d 61 64 53 69 7a 65 20 3d 20 30 3b 0a 20 20 7d 0a  adSize = 0;.  }.
1d19e 0a 20 20 2f 2a 20 49 66 20 75 2e 61 6d 2e 70 61  .  /* If u.am.pa
1d19f 79 6c 6f 61 64 53 69 7a 65 20 69 73 20 30 2c 20  yloadSize is 0, 
1d1a0 74 68 65 6e 20 6a 75 73 74 20 73 74 6f 72 65 20  then just store 
1d1a1 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 66 28 20  a NULL */.  if( 
1d1a2 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.am.payloadSize
1d1a3 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ==0 ){.    asser
1d1a4 74 28 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66  t( u.am.pDest->f
1d1a5 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b  lags&MEM_Null );
1d1a6 0a 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c  .    goto op_col
1d1a7 75 6d 6e 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 61  umn_out;.  }.  a
1d1a8 73 73 65 72 74 28 20 64 62 2d 3e 61 4c 69 6d 69  ssert( db->aLimi
1d1a9 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
1d1aa 45 4e 47 54 48 5d 3e 3d 30 20 29 3b 0a 20 20 69  ENGTH]>=0 );.  i
1d1ab 66 28 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53  f( u.am.payloadS
1d1ac 69 7a 65 20 3e 20 28 75 33 32 29 64 62 2d 3e 61  ize > (u32)db->a
1d1ad 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
1d1ae 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20  IT_LENGTH] ){.  
1d1af 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a    goto too_big;.
1d1b0 20 20 7d 0a 0a 20 20 75 2e 61 6d 2e 6e 46 69 65    }..  u.am.nFie
1d1b1 6c 64 20 3d 20 75 2e 61 6d 2e 70 43 2d 3e 6e 46  ld = u.am.pC->nF
1d1b2 69 65 6c 64 3b 0a 20 20 61 73 73 65 72 74 28 20  ield;.  assert( 
1d1b3 75 2e 61 6d 2e 70 32 3c 75 2e 61 6d 2e 6e 46 69  u.am.p2<u.am.nFi
1d1b4 65 6c 64 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 61  eld );..  /* Rea
1d1b5 64 20 61 6e 64 20 70 61 72 73 65 20 74 68 65 20  d and parse the 
1d1b6 74 61 62 6c 65 20 68 65 61 64 65 72 2e 20 20 53  table header.  S
1d1b7 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73  tore the results
1d1b8 20 6f 66 20 74 68 65 20 70 61 72 73 65 0a 20 20   of the parse.  
1d1b9 2a 2a 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f  ** into the reco
1d1ba 72 64 20 68 65 61 64 65 72 20 63 61 63 68 65 20  rd header cache 
1d1bb 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75  fields of the cu
1d1bc 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61  rsor..  */.  u.a
1d1bd 6d 2e 61 54 79 70 65 20 3d 20 75 2e 61 6d 2e 70  m.aType = u.am.p
1d1be 43 2d 3e 61 54 79 70 65 3b 0a 20 20 69 66 28 20  C->aType;.  if( 
1d1bf 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.am.pC->cacheSt
1d1c0 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 65 43 74  atus==p->cacheCt
1d1c1 72 20 29 7b 0a 20 20 20 20 75 2e 61 6d 2e 61 4f  r ){.    u.am.aO
1d1c2 66 66 73 65 74 20 3d 20 75 2e 61 6d 2e 70 43 2d  ffset = u.am.pC-
1d1c3 3e 61 4f 66 66 73 65 74 3b 0a 20 20 7d 65 6c 73  >aOffset;.  }els
1d1c4 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 75 2e  e{.    assert(u.
1d1c5 61 6d 2e 61 54 79 70 65 29 3b 0a 20 20 20 20 75  am.aType);.    u
1d1c6 2e 61 6d 2e 61 76 61 69 6c 20 3d 20 30 3b 0a 20  .am.avail = 0;. 
1d1c7 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61 4f 66 66     u.am.pC->aOff
1d1c8 73 65 74 20 3d 20 75 2e 61 6d 2e 61 4f 66 66 73  set = u.am.aOffs
1d1c9 65 74 20 3d 20 26 75 2e 61 6d 2e 61 54 79 70 65  et = &u.am.aType
1d1ca 5b 75 2e 61 6d 2e 6e 46 69 65 6c 64 5d 3b 0a 20  [u.am.nField];. 
1d1cb 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 70 61 79 6c     u.am.pC->payl
1d1cc 6f 61 64 53 69 7a 65 20 3d 20 75 2e 61 6d 2e 70  oadSize = u.am.p
1d1cd 61 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20  ayloadSize;.    
1d1ce 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.am.pC->cacheSt
1d1cf 61 74 75 73 20 3d 20 70 2d 3e 63 61 63 68 65 43  atus = p->cacheC
1d1d0 74 72 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75  tr;..    /* Figu
1d1d1 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20  re out how many 
1d1d2 62 79 74 65 73 20 61 72 65 20 69 6e 20 74 68 65  bytes are in the
1d1d3 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 69   header */.    i
1d1d4 66 28 20 75 2e 61 6d 2e 7a 52 65 63 20 29 7b 0a  f( u.am.zRec ){.
1d1d5 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61        u.am.zData
1d1d6 20 3d 20 75 2e 61 6d 2e 7a 52 65 63 3b 0a 20 20   = u.am.zRec;.  
1d1d7 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
1d1d8 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e  f( u.am.pC->isIn
1d1d9 64 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 75  dex ){.        u
1d1da 2e 61 6d 2e 7a 44 61 74 61 20 3d 20 28 63 68 61  .am.zData = (cha
1d1db 72 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 4b  r*)sqlite3BtreeK
1d1dc 65 79 46 65 74 63 68 28 75 2e 61 6d 2e 70 43 72  eyFetch(u.am.pCr
1d1dd 73 72 2c 20 26 75 2e 61 6d 2e 61 76 61 69 6c 29  sr, &u.am.avail)
1d1de 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1d1df 20 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74         u.am.zDat
1d1e0 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74  a = (char*)sqlit
1d1e1 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68  e3BtreeDataFetch
1d1e2 28 75 2e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e  (u.am.pCrsr, &u.
1d1e3 61 6d 2e 61 76 61 69 6c 29 3b 0a 20 20 20 20 20  am.avail);.     
1d1e4 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 4b   }.      /* If K
1d1e5 65 79 46 65 74 63 68 28 29 2f 44 61 74 61 46 65  eyFetch()/DataFe
1d1e6 74 63 68 28 29 20 6d 61 6e 61 67 65 64 20 74 6f  tch() managed to
1d1e7 20 67 65 74 20 74 68 65 20 65 6e 74 69 72 65 20   get the entire 
1d1e8 70 61 79 6c 6f 61 64 2c 0a 20 20 20 20 20 20 2a  payload,.      *
1d1e9 2a 20 73 61 76 65 20 74 68 65 20 70 61 79 6c 6f  * save the paylo
1d1ea 61 64 20 69 6e 20 74 68 65 20 75 2e 61 6d 2e 70  ad in the u.am.p
1d1eb 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 20  C->aRow cache.  
1d1ec 54 68 61 74 20 77 69 6c 6c 20 73 61 76 65 20 75  That will save u
1d1ed 73 20 66 72 6f 6d 0a 20 20 20 20 20 20 2a 2a 20  s from.      ** 
1d1ee 68 61 76 69 6e 67 20 74 6f 20 6d 61 6b 65 20 61  having to make a
1d1ef 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c 6c 73 20  dditional calls 
1d1f0 74 6f 20 66 65 74 63 68 20 74 68 65 20 63 6f 6e  to fetch the con
1d1f1 74 65 6e 74 20 70 6f 72 74 69 6f 6e 20 6f 66 0a  tent portion of.
1d1f2 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65 63        ** the rec
1d1f3 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ord..      */.  
1d1f4 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d      assert( u.am
1d1f5 2e 61 76 61 69 6c 3e 3d 30 20 29 3b 0a 20 20 20  .avail>=0 );.   
1d1f6 20 20 20 69 66 28 20 75 2e 61 6d 2e 70 61 79 6c     if( u.am.payl
1d1f7 6f 61 64 53 69 7a 65 20 3c 3d 20 28 75 33 32 29  oadSize <= (u32)
1d1f8 75 2e 61 6d 2e 61 76 61 69 6c 20 29 7b 0a 20 20  u.am.avail ){.  
1d1f9 20 20 20 20 20 20 75 2e 61 6d 2e 7a 52 65 63 20        u.am.zRec 
1d1fa 3d 20 75 2e 61 6d 2e 7a 44 61 74 61 3b 0a 20 20  = u.am.zData;.  
1d1fb 20 20 20 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61        u.am.pC->a
1d1fc 52 6f 77 20 3d 20 28 75 38 2a 29 75 2e 61 6d 2e  Row = (u8*)u.am.
1d1fd 7a 44 61 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c  zData;.      }el
1d1fe 73 65 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d  se{.        u.am
1d1ff 2e 70 43 2d 3e 61 52 6f 77 20 3d 20 30 3b 0a 20  .pC->aRow = 0;. 
1d200 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1d201 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   /* The followin
1d202 67 20 61 73 73 65 72 74 20 69 73 20 74 72 75 65  g assert is true
1d203 20 69 6e 20 61 6c 6c 20 63 61 73 65 73 20 61 63   in all cases ac
1d204 63 65 70 74 20 77 68 65 6e 0a 20 20 20 20 2a 2a  cept when.    **
1d205 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1d206 6c 65 20 68 61 73 20 62 65 65 6e 20 63 6f 72 72  le has been corr
1d207 75 70 74 65 64 20 65 78 74 65 72 6e 61 6c 6c 79  upted externally
1d208 2e 0a 20 20 20 20 2a 2a 20 20 20 20 61 73 73 65  ..    **    asse
1d209 72 74 28 20 75 2e 61 6d 2e 7a 52 65 63 21 3d 30  rt( u.am.zRec!=0
1d20a 20 7c 7c 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d   || u.am.avail>=
1d20b 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.am.payloadSize
1d20c 20 7c 7c 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d   || u.am.avail>=
1d20d 39 20 29 3b 20 2a 2f 0a 20 20 20 20 75 2e 61 6d  9 ); */.    u.am
1d20e 2e 73 7a 48 64 72 20 3d 20 67 65 74 56 61 72 69  .szHdr = getVari
1d20f 6e 74 33 32 28 28 75 38 2a 29 75 2e 61 6d 2e 7a  nt32((u8*)u.am.z
1d210 44 61 74 61 2c 20 75 2e 61 6d 2e 6f 66 66 73 65  Data, u.am.offse
1d211 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65  t);..    /* Make
1d212 20 73 75 72 65 20 61 20 63 6f 72 72 75 70 74 20   sure a corrupt 
1d213 64 61 74 61 62 61 73 65 20 68 61 73 20 6e 6f 74  database has not
1d214 20 67 69 76 65 6e 20 75 73 20 61 6e 20 6f 76 65   given us an ove
1d215 72 73 69 7a 65 20 68 65 61 64 65 72 2e 0a 20 20  rsize header..  
1d216 20 20 2a 2a 20 44 6f 20 74 68 69 73 20 6e 6f 77    ** Do this now
1d217 20 74 6f 20 61 76 6f 69 64 20 61 6e 20 6f 76 65   to avoid an ove
1d218 72 73 69 7a 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  rsize memory all
1d219 6f 63 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 0a  ocation..    **.
1d21a 20 20 20 20 2a 2a 20 54 79 70 65 20 65 6e 74 72      ** Type entr
1d21b 69 65 73 20 63 61 6e 20 62 65 20 62 65 74 77 65  ies can be betwe
1d21c 65 6e 20 31 20 61 6e 64 20 35 20 62 79 74 65 73  en 1 and 5 bytes
1d21d 20 65 61 63 68 2e 20 20 42 75 74 20 34 20 61 6e   each.  But 4 an
1d21e 64 20 35 20 62 79 74 65 0a 20 20 20 20 2a 2a 20  d 5 byte.    ** 
1d21f 74 79 70 65 73 20 75 73 65 20 73 6f 20 6d 75 63  types use so muc
1d220 68 20 64 61 74 61 20 73 70 61 63 65 20 74 68 61  h data space tha
1d221 74 20 74 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79  t there can only
1d222 20 62 65 20 34 30 39 36 20 61 6e 64 20 33 32 20   be 4096 and 32 
1d223 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 6d 2c 20  of.    ** them, 
1d224 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 20 53  respectively.  S
1d225 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65  o the maximum he
1d226 61 64 65 72 20 6c 65 6e 67 74 68 20 72 65 73 75  ader length resu
1d227 6c 74 73 20 66 72 6f 6d 20 61 0a 20 20 20 20 2a  lts from a.    *
1d228 2a 20 33 2d 62 79 74 65 20 74 79 70 65 20 66 6f  * 3-byte type fo
1d229 72 20 65 61 63 68 20 6f 66 20 74 68 65 20 6d 61  r each of the ma
1d22a 78 69 6d 75 6d 20 6f 66 20 33 32 37 36 38 20 63  ximum of 32768 c
1d22b 6f 6c 75 6d 6e 73 20 70 6c 75 73 20 74 68 72 65  olumns plus thre
1d22c 65 0a 20 20 20 20 2a 2a 20 65 78 74 72 61 20 62  e.    ** extra b
1d22d 79 74 65 73 20 66 6f 72 20 74 68 65 20 68 65 61  ytes for the hea
1d22e 64 65 72 20 6c 65 6e 67 74 68 20 69 74 73 65 6c  der length itsel
1d22f 66 2e 20 20 33 32 37 36 38 2a 33 20 2b 20 33 20  f.  32768*3 + 3 
1d230 3d 20 39 38 33 30 37 2e 0a 20 20 20 20 2a 2f 0a  = 98307..    */.
1d231 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 6f 66 66      if( u.am.off
1d232 73 65 74 20 3e 20 39 38 33 30 37 20 29 7b 0a 20  set > 98307 ){. 
1d233 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1d234 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
1d235 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c       goto op_col
1d236 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a  umn_out;.    }..
1d237 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 69      /* Compute i
1d238 6e 20 75 2e 61 6d 2e 6c 65 6e 20 74 68 65 20 6e  n u.am.len the n
1d239 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
1d23a 66 20 64 61 74 61 20 77 65 20 6e 65 65 64 20 74  f data we need t
1d23b 6f 20 72 65 61 64 20 69 6e 20 6f 72 64 65 72 0a  o read in order.
1d23c 20 20 20 20 2a 2a 20 74 6f 20 67 65 74 20 75 2e      ** to get u.
1d23d 61 6d 2e 6e 46 69 65 6c 64 20 74 79 70 65 20 76  am.nField type v
1d23e 61 6c 75 65 73 2e 20 20 75 2e 61 6d 2e 6f 66 66  alues.  u.am.off
1d23f 73 65 74 20 69 73 20 61 6e 20 75 70 70 65 72 20  set is an upper 
1d240 62 6f 75 6e 64 20 6f 6e 20 74 68 69 73 2e 20 20  bound on this.  
1d241 42 75 74 0a 20 20 20 20 2a 2a 20 75 2e 61 6d 2e  But.    ** u.am.
1d242 6e 46 69 65 6c 64 20 6d 69 67 68 74 20 62 65 20  nField might be 
1d243 73 69 67 6e 69 66 69 63 61 6e 74 6c 79 20 6c 65  significantly le
1d244 73 73 20 74 68 61 6e 20 74 68 65 20 74 72 75 65  ss than the true
1d245 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
1d246 6e 73 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65  ns.    ** in the
1d247 20 74 61 62 6c 65 2c 20 61 6e 64 20 69 6e 20 74   table, and in t
1d248 68 61 74 20 63 61 73 65 2c 20 35 2a 75 2e 61 6d  hat case, 5*u.am
1d249 2e 6e 46 69 65 6c 64 2b 33 20 6d 69 67 68 74 20  .nField+3 might 
1d24a 62 65 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20  be smaller than 
1d24b 75 2e 61 6d 2e 6f 66 66 73 65 74 2e 0a 20 20 20  u.am.offset..   
1d24c 20 2a 2a 20 57 65 20 77 61 6e 74 20 74 6f 20 6d   ** We want to m
1d24d 69 6e 69 6d 69 7a 65 20 75 2e 61 6d 2e 6c 65 6e  inimize u.am.len
1d24e 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6c 69 6d   in order to lim
1d24f 69 74 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  it the size of t
1d250 68 65 20 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a  he memory.    **
1d251 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 65 73 70   allocation, esp
1d252 65 63 69 61 6c 6c 79 20 69 66 20 61 20 63 6f 72  ecially if a cor
1d253 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69  rupt database fi
1d254 6c 65 20 68 61 73 20 63 61 75 73 65 64 20 75 2e  le has caused u.
1d255 61 6d 2e 6f 66 66 73 65 74 0a 20 20 20 20 2a 2a  am.offset.    **
1d256 20 74 6f 20 62 65 20 6f 76 65 72 73 69 7a 65 64   to be oversized
1d257 2e 20 4f 66 66 73 65 74 20 69 73 20 6c 69 6d 69  . Offset is limi
1d258 74 65 64 20 74 6f 20 39 38 33 30 37 20 61 62 6f  ted to 98307 abo
1d259 76 65 2e 20 20 42 75 74 20 39 38 33 30 37 20 6d  ve.  But 98307 m
1d25a 69 67 68 74 0a 20 20 20 20 2a 2a 20 73 74 69 6c  ight.    ** stil
1d25b 6c 20 65 78 63 65 65 64 20 52 6f 62 73 6f 6e 20  l exceed Robson 
1d25c 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
1d25d 6e 20 6c 69 6d 69 74 73 20 6f 6e 20 73 6f 6d 65  n limits on some
1d25e 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 73 2e   configurations.
1d25f 0a 20 20 20 20 2a 2a 20 4f 6e 20 73 79 73 74 65  .    ** On syste
1d260 6d 73 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 74  ms that cannot t
1d261 6f 6c 65 72 61 74 65 20 6c 61 72 67 65 20 6d 65  olerate large me
1d262 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  mory allocations
1d263 2c 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 2a 35 2b  , u.am.nField*5+
1d264 33 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 6c 69  3.    ** will li
1d265 6b 65 6c 79 20 62 65 20 6d 75 63 68 20 73 6d 61  kely be much sma
1d266 6c 6c 65 72 20 73 69 6e 63 65 20 75 2e 61 6d 2e  ller since u.am.
1d267 6e 46 69 65 6c 64 20 77 69 6c 6c 20 6c 69 6b 65  nField will like
1d268 6c 79 20 62 65 20 6c 65 73 73 20 74 68 61 6e 0a  ly be less than.
1d269 20 20 20 20 2a 2a 20 32 30 20 6f 72 20 73 6f 2e      ** 20 or so.
1d26a 20 20 54 68 69 73 20 69 6e 73 75 72 65 73 20 74    This insures t
1d26b 68 61 74 20 52 6f 62 73 6f 6e 20 6d 65 6d 6f 72  hat Robson memor
1d26c 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 69 6d  y allocation lim
1d26d 69 74 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e  its are.    ** n
1d26e 6f 74 20 65 78 63 65 65 64 65 64 20 65 76 65 6e  ot exceeded even
1d26f 20 66 6f 72 20 63 6f 72 72 75 70 74 20 64 61 74   for corrupt dat
1d270 61 62 61 73 65 20 66 69 6c 65 73 2e 0a 20 20 20  abase files..   
1d271 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 2e 6c 65 6e   */.    u.am.len
1d272 20 3d 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 2a 35   = u.am.nField*5
1d273 20 2b 20 33 3b 0a 20 20 20 20 69 66 28 20 75 2e   + 3;.    if( u.
1d274 61 6d 2e 6c 65 6e 20 3e 20 28 69 6e 74 29 75 2e  am.len > (int)u.
1d275 61 6d 2e 6f 66 66 73 65 74 20 29 20 75 2e 61 6d  am.offset ) u.am
1d276 2e 6c 65 6e 20 3d 20 28 69 6e 74 29 75 2e 61 6d  .len = (int)u.am
1d277 2e 6f 66 66 73 65 74 3b 0a 0a 20 20 20 20 2f 2a  .offset;..    /*
1d278 20 54 68 65 20 4b 65 79 46 65 74 63 68 28 29 20   The KeyFetch() 
1d279 6f 72 20 44 61 74 61 46 65 74 63 68 28 29 20 61  or DataFetch() a
1d27a 62 6f 76 65 20 61 72 65 20 66 61 73 74 20 61 6e  bove are fast an
1d27b 64 20 77 69 6c 6c 20 67 65 74 20 74 68 65 20 65  d will get the e
1d27c 6e 74 69 72 65 0a 20 20 20 20 2a 2a 20 72 65 63  ntire.    ** rec
1d27d 6f 72 64 20 68 65 61 64 65 72 20 69 6e 20 6d 6f  ord header in mo
1d27e 73 74 20 63 61 73 65 73 2e 20 20 42 75 74 20 74  st cases.  But t
1d27f 68 65 79 20 77 69 6c 6c 20 66 61 69 6c 20 74 6f  hey will fail to
1d280 20 67 65 74 20 74 68 65 20 63 6f 6d 70 6c 65 74   get the complet
1d281 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20  e.    ** record 
1d282 68 65 61 64 65 72 20 69 66 20 74 68 65 20 72 65  header if the re
1d283 63 6f 72 64 20 68 65 61 64 65 72 20 64 6f 65 73  cord header does
1d284 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 20 73 69   not fit on a si
1d285 6e 67 6c 65 20 70 61 67 65 0a 20 20 20 20 2a 2a  ngle page.    **
1d286 20 69 6e 20 74 68 65 20 42 2d 54 72 65 65 2e 20   in the B-Tree. 
1d287 20 57 68 65 6e 20 74 68 61 74 20 68 61 70 70 65   When that happe
1d288 6e 73 2c 20 75 73 65 20 73 71 6c 69 74 65 33 56  ns, use sqlite3V
1d289 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28  dbeMemFromBtree(
1d28a 29 20 74 6f 0a 20 20 20 20 2a 2a 20 61 63 71 75  ) to.    ** acqu
1d28b 69 72 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65  ire the complete
1d28c 20 68 65 61 64 65 72 20 74 65 78 74 2e 0a 20 20   header text..  
1d28d 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 75 2e    */.    if( !u.
1d28e 61 6d 2e 7a 52 65 63 20 26 26 20 75 2e 61 6d 2e  am.zRec && u.am.
1d28f 61 76 61 69 6c 3c 75 2e 61 6d 2e 6c 65 6e 20 29  avail<u.am.len )
1d290 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 73 4d 65  {.      u.am.sMe
1d291 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20  m.flags = 0;.   
1d292 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e 64 62 20     u.am.sMem.db 
1d293 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  = 0;.      rc = 
1d294 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72  sqlite3VdbeMemFr
1d295 6f 6d 42 74 72 65 65 28 75 2e 61 6d 2e 70 43 72  omBtree(u.am.pCr
1d296 73 72 2c 20 30 2c 20 75 2e 61 6d 2e 6c 65 6e 2c  sr, 0, u.am.len,
1d297 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65   u.am.pC->isInde
1d298 78 2c 20 26 75 2e 61 6d 2e 73 4d 65 6d 29 3b 0a  x, &u.am.sMem);.
1d299 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1d29a 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1d29b 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d     goto op_colum
1d29c 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20  n_out;.      }. 
1d29d 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61 20       u.am.zData 
1d29e 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20  = u.am.sMem.z;. 
1d29f 20 20 20 7d 0a 20 20 20 20 75 2e 61 6d 2e 7a 45     }.    u.am.zE
1d2a0 6e 64 48 64 72 20 3d 20 28 75 38 20 2a 29 26 75  ndHdr = (u8 *)&u
1d2a1 2e 61 6d 2e 7a 44 61 74 61 5b 75 2e 61 6d 2e 6c  .am.zData[u.am.l
1d2a2 65 6e 5d 3b 0a 20 20 20 20 75 2e 61 6d 2e 7a 49  en];.    u.am.zI
1d2a3 64 78 20 3d 20 28 75 38 20 2a 29 26 75 2e 61 6d  dx = (u8 *)&u.am
1d2a4 2e 7a 44 61 74 61 5b 75 2e 61 6d 2e 73 7a 48 64  .zData[u.am.szHd
1d2a5 72 5d 3b 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e  r];..    /* Scan
1d2a6 20 74 68 65 20 68 65 61 64 65 72 20 61 6e 64 20   the header and 
1d2a7 75 73 65 20 69 74 20 74 6f 20 66 69 6c 6c 20 69  use it to fill i
1d2a8 6e 20 74 68 65 20 75 2e 61 6d 2e 61 54 79 70 65  n the u.am.aType
1d2a9 5b 5d 20 61 6e 64 20 75 2e 61 6d 2e 61 4f 66 66  [] and u.am.aOff
1d2aa 73 65 74 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72  set[].    ** arr
1d2ab 61 79 73 2e 20 20 75 2e 61 6d 2e 61 54 79 70 65  ays.  u.am.aType
1d2ac 5b 75 2e 61 6d 2e 69 5d 20 77 69 6c 6c 20 63 6f  [u.am.i] will co
1d2ad 6e 74 61 69 6e 20 74 68 65 20 74 79 70 65 20 69  ntain the type i
1d2ae 6e 74 65 67 65 72 20 66 6f 72 20 74 68 65 20 75  nteger for the u
1d2af 2e 61 6d 2e 69 2d 74 68 0a 20 20 20 20 2a 2a 20  .am.i-th.    ** 
1d2b0 63 6f 6c 75 6d 6e 20 61 6e 64 20 75 2e 61 6d 2e  column and u.am.
1d2b1 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d 20  aOffset[u.am.i] 
1d2b2 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68 65  will contain the
1d2b3 20 75 2e 61 6d 2e 6f 66 66 73 65 74 20 66 72 6f   u.am.offset fro
1d2b4 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a  m the beginning.
1d2b5 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65      ** of the re
1d2b6 63 6f 72 64 20 74 6f 20 74 68 65 20 73 74 61 72  cord to the star
1d2b7 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 66 6f  t of the data fo
1d2b8 72 20 74 68 65 20 75 2e 61 6d 2e 69 2d 74 68 20  r the u.am.i-th 
1d2b9 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2f 0a 20 20  column.    */.  
1d2ba 20 20 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20    u.am.offset64 
1d2bb 3d 20 75 2e 61 6d 2e 6f 66 66 73 65 74 3b 0a 20  = u.am.offset;. 
1d2bc 20 20 20 66 6f 72 28 75 2e 61 6d 2e 69 3d 30 3b     for(u.am.i=0;
1d2bd 20 75 2e 61 6d 2e 69 3c 75 2e 61 6d 2e 6e 46 69   u.am.i<u.am.nFi
1d2be 65 6c 64 3b 20 75 2e 61 6d 2e 69 2b 2b 29 7b 0a  eld; u.am.i++){.
1d2bf 20 20 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 7a        if( u.am.z
1d2c0 49 64 78 3c 75 2e 61 6d 2e 7a 45 6e 64 48 64 72  Idx<u.am.zEndHdr
1d2c1 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d   ){.        u.am
1d2c2 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d  .aOffset[u.am.i]
1d2c3 20 3d 20 28 75 33 32 29 75 2e 61 6d 2e 6f 66 66   = (u32)u.am.off
1d2c4 73 65 74 36 34 3b 0a 20 20 20 20 20 20 20 20 75  set64;.        u
1d2c5 2e 61 6d 2e 7a 49 64 78 20 2b 3d 20 67 65 74 56  .am.zIdx += getV
1d2c6 61 72 69 6e 74 33 32 28 75 2e 61 6d 2e 7a 49 64  arint32(u.am.zId
1d2c7 78 2c 20 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e  x, u.am.aType[u.
1d2c8 61 6d 2e 69 5d 29 3b 0a 20 20 20 20 20 20 20 20  am.i]);.        
1d2c9 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20 2b 3d  u.am.offset64 +=
1d2ca 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
1d2cb 61 6c 54 79 70 65 4c 65 6e 28 75 2e 61 6d 2e 61  alTypeLen(u.am.a
1d2cc 54 79 70 65 5b 75 2e 61 6d 2e 69 5d 29 3b 0a 20  Type[u.am.i]);. 
1d2cd 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1d2ce 20 20 20 20 2f 2a 20 49 66 20 75 2e 61 6d 2e 69      /* If u.am.i
1d2cf 20 69 73 20 6c 65 73 73 20 74 68 61 74 20 75 2e   is less that u.
1d2d0 61 6d 2e 6e 46 69 65 6c 64 2c 20 74 68 65 6e 20  am.nField, then 
1d2d1 74 68 65 72 65 20 61 72 65 20 6c 65 73 73 20 66  there are less f
1d2d2 69 65 6c 64 73 20 69 6e 20 74 68 69 73 0a 20 20  ields in this.  
1d2d3 20 20 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20        ** record 
1d2d4 74 68 61 6e 20 53 65 74 4e 75 6d 43 6f 6c 75 6d  than SetNumColum
1d2d5 6e 73 20 69 6e 64 69 63 61 74 65 64 20 74 68 65  ns indicated the
1d2d6 72 65 20 61 72 65 20 63 6f 6c 75 6d 6e 73 20 69  re are columns i
1d2d7 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  n the.        **
1d2d8 20 74 61 62 6c 65 2e 20 53 65 74 20 74 68 65 20   table. Set the 
1d2d9 75 2e 61 6d 2e 6f 66 66 73 65 74 20 66 6f 72 20  u.am.offset for 
1d2da 61 6e 79 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e  any extra column
1d2db 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e  s not present in
1d2dc 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20  .        ** the 
1d2dd 72 65 63 6f 72 64 20 74 6f 20 30 2e 20 54 68 69  record to 0. Thi
1d2de 73 20 74 65 6c 6c 73 20 63 6f 64 65 20 62 65 6c  s tells code bel
1d2df 6f 77 20 74 6f 20 73 74 6f 72 65 20 61 20 4e 55  ow to store a NU
1d2e0 4c 4c 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e  LL.        ** in
1d2e1 73 74 65 61 64 20 6f 66 20 64 65 73 65 72 69 61  stead of deseria
1d2e2 6c 69 7a 69 6e 67 20 61 20 76 61 6c 75 65 20 66  lizing a value f
1d2e3 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 64 2e 0a  rom the record..
1d2e4 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
1d2e5 20 20 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b     u.am.aOffset[
1d2e6 75 2e 61 6d 2e 69 5d 20 3d 20 30 3b 0a 20 20 20  u.am.i] = 0;.   
1d2e7 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
1d2e8 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1d2e9 65 61 73 65 28 26 75 2e 61 6d 2e 73 4d 65 6d 29  ease(&u.am.sMem)
1d2ea 3b 0a 20 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e  ;.    u.am.sMem.
1d2eb 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
1d2ec 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20  ;..    /* If we 
1d2ed 68 61 76 65 20 72 65 61 64 20 6d 6f 72 65 20 68  have read more h
1d2ee 65 61 64 65 72 20 64 61 74 61 20 74 68 61 6e 20  eader data than 
1d2ef 77 61 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e  was contained in
1d2f0 20 74 68 65 20 68 65 61 64 65 72 2c 0a 20 20 20   the header,.   
1d2f1 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 65 6e   ** or if the en
1d2f2 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 69  d of the last fi
1d2f3 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20 62  eld appears to b
1d2f4 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f  e past the end o
1d2f5 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 63  f the.    ** rec
1d2f6 6f 72 64 2c 20 6f 72 20 69 66 20 74 68 65 20 65  ord, or if the e
1d2f7 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66  nd of the last f
1d2f8 69 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20  ield appears to 
1d2f9 62 65 20 62 65 66 6f 72 65 20 74 68 65 20 65 6e  be before the en
1d2fa 64 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20  d.    ** of the 
1d2fb 72 65 63 6f 72 64 20 28 77 68 65 6e 20 61 6c 6c  record (when all
1d2fc 20 66 69 65 6c 64 73 20 70 72 65 73 65 6e 74 29   fields present)
1d2fd 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74 20 62  , then we must b
1d2fe 65 20 64 65 61 6c 69 6e 67 0a 20 20 20 20 2a 2a  e dealing.    **
1d2ff 20 77 69 74 68 20 61 20 63 6f 72 72 75 70 74 20   with a corrupt 
1d300 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f  database..    */
1d301 0a 20 20 20 20 69 66 28 20 28 75 2e 61 6d 2e 7a  .    if( (u.am.z
1d302 49 64 78 20 3e 20 75 2e 61 6d 2e 7a 45 6e 64 48  Idx > u.am.zEndH
1d303 64 72 29 7c 7c 20 28 75 2e 61 6d 2e 6f 66 66 73  dr)|| (u.am.offs
1d304 65 74 36 34 20 3e 20 75 2e 61 6d 2e 70 61 79 6c  et64 > u.am.payl
1d305 6f 61 64 53 69 7a 65 29 0a 20 20 20 20 20 7c 7c  oadSize).     ||
1d306 20 28 75 2e 61 6d 2e 7a 49 64 78 3d 3d 75 2e 61   (u.am.zIdx==u.a
1d307 6d 2e 7a 45 6e 64 48 64 72 20 26 26 20 75 2e 61  m.zEndHdr && u.a
1d308 6d 2e 6f 66 66 73 65 74 36 34 21 3d 28 75 36 34  m.offset64!=(u64
1d309 29 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a  )u.am.payloadSiz
1d30a 65 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  e) ){.      rc =
1d30b 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1d30c 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f  BKPT;.      goto
1d30d 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a   op_column_out;.
1d30e 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
1d30f 47 65 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69  Get the column i
1d310 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 49 66 20 75  nformation. If u
1d311 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d  .am.aOffset[u.am
1d312 2e 70 32 5d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  .p2] is non-zero
1d313 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 64 65 73 65  , then.  ** dese
1d314 72 69 61 6c 69 7a 65 20 74 68 65 20 76 61 6c 75  rialize the valu
1d315 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f 72  e from the recor
1d316 64 2e 20 49 66 20 75 2e 61 6d 2e 61 4f 66 66 73  d. If u.am.aOffs
1d317 65 74 5b 75 2e 61 6d 2e 70 32 5d 20 69 73 20 7a  et[u.am.p2] is z
1d318 65 72 6f 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74  ero,.  ** then t
1d319 68 65 72 65 20 61 72 65 20 6e 6f 74 20 65 6e 6f  here are not eno
1d31a 75 67 68 20 66 69 65 6c 64 73 20 69 6e 20 74 68  ugh fields in th
1d31b 65 20 72 65 63 6f 72 64 20 74 6f 20 73 61 74 69  e record to sati
1d31c 73 66 79 20 74 68 65 0a 20 20 2a 2a 20 72 65 71  sfy the.  ** req
1d31d 75 65 73 74 2e 20 20 49 6e 20 74 68 69 73 20 63  uest.  In this c
1d31e 61 73 65 2c 20 73 65 74 20 74 68 65 20 76 61 6c  ase, set the val
1d31f 75 65 20 4e 55 4c 4c 20 6f 72 20 74 6f 20 50 34  ue NULL or to P4
1d320 20 69 66 20 50 34 20 69 73 0a 20 20 2a 2a 20 61   if P4 is.  ** a
1d321 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65   pointer to a Me
1d322 6d 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2f 0a 20  m object..  */. 
1d323 20 69 66 28 20 75 2e 61 6d 2e 61 4f 66 66 73 65   if( u.am.aOffse
1d324 74 5b 75 2e 61 6d 2e 70 32 5d 20 29 7b 0a 20 20  t[u.am.p2] ){.  
1d325 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
1d326 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69  LITE_OK );.    i
1d327 66 28 20 75 2e 61 6d 2e 7a 52 65 63 20 29 7b 0a  f( u.am.zRec ){.
1d328 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1d329 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72  eMemReleaseExter
1d32a 6e 61 6c 28 75 2e 61 6d 2e 70 44 65 73 74 29 3b  nal(u.am.pDest);
1d32b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1d32c 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 20  beSerialGet((u8 
1d32d 2a 29 26 75 2e 61 6d 2e 7a 52 65 63 5b 75 2e 61  *)&u.am.zRec[u.a
1d32e 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 70  m.aOffset[u.am.p
1d32f 32 5d 5d 2c 20 75 2e 61 6d 2e 61 54 79 70 65 5b  2]], u.am.aType[
1d330 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e 70  u.am.p2], u.am.p
1d331 44 65 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Dest);.    }else
1d332 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 6c 65 6e  {.      u.am.len
1d333 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65   = sqlite3VdbeSe
1d334 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 2e 61 6d  rialTypeLen(u.am
1d335 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32 5d 29  .aType[u.am.p2])
1d336 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1d337 64 62 65 4d 65 6d 4d 6f 76 65 28 26 75 2e 61 6d  dbeMemMove(&u.am
1d338 2e 73 4d 65 6d 2c 20 75 2e 61 6d 2e 70 44 65 73  .sMem, u.am.pDes
1d339 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  t);.      rc = s
1d33a 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f  qlite3VdbeMemFro
1d33b 6d 42 74 72 65 65 28 75 2e 61 6d 2e 70 43 72 73  mBtree(u.am.pCrs
1d33c 72 2c 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b  r, u.am.aOffset[
1d33d 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e 6c  u.am.p2], u.am.l
1d33e 65 6e 2c 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49  en, u.am.pC->isI
1d33f 6e 64 65 78 2c 20 26 75 2e 61 6d 2e 73 4d 65 6d  ndex, &u.am.sMem
1d340 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
1d341 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1d342 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f        goto op_co
1d343 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20  lumn_out;.      
1d344 7d 0a 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61  }.      u.am.zDa
1d345 74 61 20 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a  ta = u.am.sMem.z
1d346 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1d347 64 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38  dbeSerialGet((u8
1d348 2a 29 75 2e 61 6d 2e 7a 44 61 74 61 2c 20 75 2e  *)u.am.zData, u.
1d349 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32  am.aType[u.am.p2
1d34a 5d 2c 20 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a  ], u.am.pDest);.
1d34b 20 20 20 20 7d 0a 20 20 20 20 75 2e 61 6d 2e 70      }.    u.am.p
1d34c 44 65 73 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f  Dest->enc = enco
1d34d 64 69 6e 67 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ding;.  }else{. 
1d34e 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79     if( pOp->p4ty
1d34f 70 65 3d 3d 50 34 5f 4d 45 4d 20 29 7b 0a 20 20  pe==P4_MEM ){.  
1d350 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1d351 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 75 2e  emShallowCopy(u.
1d352 61 6d 2e 70 44 65 73 74 2c 20 70 4f 70 2d 3e 70  am.pDest, pOp->p
1d353 34 2e 70 4d 65 6d 2c 20 4d 45 4d 5f 53 74 61 74  4.pMem, MEM_Stat
1d354 69 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ic);.    }else{.
1d355 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e        assert( u.
1d356 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 26  am.pDest->flags&
1d357 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20  MEM_Null );.    
1d358 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77  }.  }..  /* If w
1d359 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  e dynamically al
1d35a 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 74 6f  located space to
1d35b 20 68 6f 6c 64 20 74 68 65 20 64 61 74 61 20 28   hold the data (
1d35c 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69  in the.  ** sqli
1d35d 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74  te3VdbeMemFromBt
1d35e 72 65 65 28 29 20 63 61 6c 6c 20 61 62 6f 76 65  ree() call above
1d35f 29 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20  ) then transfer 
1d360 63 6f 6e 74 72 6f 6c 20 6f 66 20 74 68 61 74 0a  control of that.
1d361 20 20 2a 2a 20 64 79 6e 61 6d 69 63 61 6c 6c 79    ** dynamically
1d362 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65   allocated space
1d363 20 6f 76 65 72 20 74 6f 20 74 68 65 20 75 2e 61   over to the u.a
1d364 6d 2e 70 44 65 73 74 20 73 74 72 75 63 74 75 72  m.pDest structur
1d365 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 70 72 65  e..  ** This pre
1d366 76 65 6e 74 73 20 61 20 6d 65 6d 6f 72 79 20 63  vents a memory c
1d367 6f 70 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  opy..  */.  if( 
1d368 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f  u.am.sMem.zMallo
1d369 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  c ){.    assert(
1d36a 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3d 3d 75 2e   u.am.sMem.z==u.
1d36b 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20  am.sMem.zMalloc 
1d36c 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  );.    assert( !
1d36d 28 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61  (u.am.pDest->fla
1d36e 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 20 29 3b  gs & MEM_Dyn) );
1d36f 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 28 75  .    assert( !(u
1d370 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73  .am.pDest->flags
1d371 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d   & (MEM_Blob|MEM
1d372 5f 53 74 72 29 29 20 7c 7c 20 75 2e 61 6d 2e 70  _Str)) || u.am.p
1d373 44 65 73 74 2d 3e 7a 3d 3d 75 2e 61 6d 2e 73 4d  Dest->z==u.am.sM
1d374 65 6d 2e 7a 20 29 3b 0a 20 20 20 20 75 2e 61 6d  em.z );.    u.am
1d375 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 26 3d  .pDest->flags &=
1d376 20 7e 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d   ~(MEM_Ephem|MEM
1d377 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 75 2e  _Static);.    u.
1d378 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 20  am.pDest->flags 
1d379 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20  |= MEM_Term;.   
1d37a 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 20 3d   u.am.pDest->z =
1d37b 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20 20   u.am.sMem.z;.  
1d37c 20 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 4d    u.am.pDest->zM
1d37d 61 6c 6c 6f 63 20 3d 20 75 2e 61 6d 2e 73 4d 65  alloc = u.am.sMe
1d37e 6d 2e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 7d 0a 0a  m.zMalloc;.  }..
1d37f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1d380 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62  beMemMakeWriteab
1d381 6c 65 28 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a  le(u.am.pDest);.
1d382 0a 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3a 0a  .op_column_out:.
1d383 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f    UPDATE_MAX_BLO
1d384 42 53 49 5a 45 28 75 2e 61 6d 2e 70 44 65 73 74  BSIZE(u.am.pDest
1d385 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52  );.  REGISTER_TR
1d386 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 61  ACE(pOp->p3, u.a
1d387 6d 2e 70 44 65 73 74 29 3b 0a 20 20 62 72 65 61  m.pDest);.  brea
1d388 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1d389 20 41 66 66 69 6e 69 74 79 20 50 31 20 50 32 20   Affinity P1 P2 
1d38a 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 41 70 70  * P4 *.**.** App
1d38b 6c 79 20 61 66 66 69 6e 69 74 69 65 73 20 74 6f  ly affinities to
1d38c 20 61 20 72 61 6e 67 65 20 6f 66 20 50 32 20 72   a range of P2 r
1d38d 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e  egisters startin
1d38e 67 20 77 69 74 68 20 50 31 2e 0a 2a 2a 0a 2a 2a  g with P1..**.**
1d38f 20 50 34 20 69 73 20 61 20 73 74 72 69 6e 67 20   P4 is a string 
1d390 74 68 61 74 20 69 73 20 50 32 20 63 68 61 72 61  that is P2 chara
1d391 63 74 65 72 73 20 6c 6f 6e 67 2e 20 54 68 65 20  cters long. The 
1d392 6e 74 68 20 63 68 61 72 61 63 74 65 72 20 6f 66  nth character of
1d393 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69   the.** string i
1d394 6e 64 69 63 61 74 65 73 20 74 68 65 20 63 6f 6c  ndicates the col
1d395 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74 68 61  umn affinity tha
1d396 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64  t should be used
1d397 20 66 6f 72 20 74 68 65 20 6e 74 68 0a 2a 2a 20   for the nth.** 
1d398 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 74  memory cell in t
1d399 68 65 20 72 61 6e 67 65 2e 0a 2a 2f 0a 63 61 73  he range..*/.cas
1d39a 65 20 4f 50 5f 41 66 66 69 6e 69 74 79 3a 20 7b  e OP_Affinity: {
1d39b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1d39c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1d39d 20 69 6e 74 6f 20 75 2e 61 6e 20 2a 2f 0a 20 20   into u.an */.  
1d39e 63 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b  char *zAffinity;
1d39f 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69     /* The affini
1d3a0 74 79 20 74 6f 20 62 65 20 61 70 70 6c 69 65 64  ty to be applied
1d3a1 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 61 74 61   */.  Mem *pData
1d3a2 30 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73  0;       /* Firs
1d3a3 74 20 72 65 67 69 73 74 65 72 20 74 6f 20 77 68  t register to wh
1d3a4 69 63 68 20 74 6f 20 61 70 70 6c 79 20 61 66 66  ich to apply aff
1d3a5 69 6e 69 74 79 20 2a 2f 0a 20 20 4d 65 6d 20 2a  inity */.  Mem *
1d3a6 70 4c 61 73 74 3b 20 20 20 20 20 20 20 20 2f 2a  pLast;        /*
1d3a7 20 4c 61 73 74 20 72 65 67 69 73 74 65 72 20 74   Last register t
1d3a8 6f 20 77 68 69 63 68 20 74 6f 20 61 70 70 6c 79  o which to apply
1d3a9 20 61 66 66 69 6e 69 74 79 20 2a 2f 0a 20 20 4d   affinity */.  M
1d3aa 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 20 20 20  em *pRec;       
1d3ab 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 72 65 67    /* Current reg
1d3ac 69 73 74 65 72 20 2a 2f 0a 23 65 6e 64 69 66 20  ister */.#endif 
1d3ad 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1d3ae 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1d3af 61 6e 20 2a 2f 0a 0a 20 20 75 2e 61 6e 2e 7a 41  an */..  u.an.zA
1d3b0 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70  ffinity = pOp->p
1d3b1 34 2e 7a 3b 0a 20 20 75 2e 61 6e 2e 70 44 61 74  4.z;.  u.an.pDat
1d3b2 61 30 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  a0 = &p->aMem[pO
1d3b3 70 2d 3e 70 31 5d 3b 0a 20 20 75 2e 61 6e 2e 70  p->p1];.  u.an.p
1d3b4 4c 61 73 74 20 3d 20 26 75 2e 61 6e 2e 70 44 61  Last = &u.an.pDa
1d3b5 74 61 30 5b 70 4f 70 2d 3e 70 32 2d 31 5d 3b 0a  ta0[pOp->p2-1];.
1d3b6 20 20 66 6f 72 28 75 2e 61 6e 2e 70 52 65 63 3d    for(u.an.pRec=
1d3b7 75 2e 61 6e 2e 70 44 61 74 61 30 3b 20 75 2e 61  u.an.pData0; u.a
1d3b8 6e 2e 70 52 65 63 3c 3d 75 2e 61 6e 2e 70 4c 61  n.pRec<=u.an.pLa
1d3b9 73 74 3b 20 75 2e 61 6e 2e 70 52 65 63 2b 2b 29  st; u.an.pRec++)
1d3ba 7b 0a 20 20 20 20 45 78 70 61 6e 64 42 6c 6f 62  {.    ExpandBlob
1d3bb 28 75 2e 61 6e 2e 70 52 65 63 29 3b 0a 20 20 20  (u.an.pRec);.   
1d3bc 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 75   applyAffinity(u
1d3bd 2e 61 6e 2e 70 52 65 63 2c 20 75 2e 61 6e 2e 7a  .an.pRec, u.an.z
1d3be 41 66 66 69 6e 69 74 79 5b 75 2e 61 6e 2e 70 52  Affinity[u.an.pR
1d3bf 65 63 2d 75 2e 61 6e 2e 70 44 61 74 61 30 5d 2c  ec-u.an.pData0],
1d3c0 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a   encoding);.  }.
1d3c1 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1d3c2 70 63 6f 64 65 3a 20 4d 61 6b 65 52 65 63 6f 72  pcode: MakeRecor
1d3c3 64 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a  d P1 P2 P3 P4 *.
1d3c4 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 50 32  **.** Convert P2
1d3c5 20 72 65 67 69 73 74 65 72 73 20 62 65 67 69 6e   registers begin
1d3c6 6e 69 6e 67 20 77 69 74 68 20 50 31 20 69 6e 74  ning with P1 int
1d3c7 6f 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79  o a single entry
1d3c8 0a 2a 2a 20 73 75 69 74 61 62 6c 65 20 66 6f 72  .** suitable for
1d3c9 20 75 73 65 20 61 73 20 61 20 64 61 74 61 20 72   use as a data r
1d3ca 65 63 6f 72 64 20 69 6e 20 61 20 64 61 74 61 62  ecord in a datab
1d3cb 61 73 65 20 74 61 62 6c 65 20 6f 72 20 61 73 20  ase table or as 
1d3cc 61 20 6b 65 79 0a 2a 2a 20 69 6e 20 61 6e 20 69  a key.** in an i
1d3cd 6e 64 65 78 2e 20 20 54 68 65 20 64 65 74 61 69  ndex.  The detai
1d3ce 6c 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 61 74  ls of the format
1d3cf 20 61 72 65 20 69 72 72 65 6c 65 76 61 6e 74 20   are irrelevant 
1d3d0 61 73 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 74 68  as long as.** th
1d3d1 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f  e OP_Column opco
1d3d2 64 65 20 63 61 6e 20 64 65 63 6f 64 65 20 74 68  de can decode th
1d3d3 65 20 72 65 63 6f 72 64 20 6c 61 74 65 72 2e 0a  e record later..
1d3d4 2a 2a 20 52 65 66 65 72 20 74 6f 20 73 6f 75 72  ** Refer to sour
1d3d5 63 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73  ce code comments
1d3d6 20 66 6f 72 20 74 68 65 20 64 65 74 61 69 6c 73   for the details
1d3d7 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 0a 2a   of the record.*
1d3d8 2a 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20  * format..**.** 
1d3d9 50 34 20 6d 61 79 20 62 65 20 61 20 73 74 72 69  P4 may be a stri
1d3da 6e 67 20 74 68 61 74 20 69 73 20 50 32 20 63 68  ng that is P2 ch
1d3db 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20  aracters long.  
1d3dc 54 68 65 20 6e 74 68 20 63 68 61 72 61 63 74 65  The nth characte
1d3dd 72 20 6f 66 20 74 68 65 0a 2a 2a 20 73 74 72 69  r of the.** stri
1d3de 6e 67 20 69 6e 64 69 63 61 74 65 73 20 74 68 65  ng indicates the
1d3df 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79   column affinity
1d3e0 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20   that should be 
1d3e1 75 73 65 64 20 66 6f 72 20 74 68 65 20 6e 74 68  used for the nth
1d3e2 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65  .** field of the
1d3e3 20 69 6e 64 65 78 20 6b 65 79 2e 0a 2a 2a 0a 2a   index key..**.*
1d3e4 2a 20 54 68 65 20 6d 61 70 70 69 6e 67 20 66 72  * The mapping fr
1d3e5 6f 6d 20 63 68 61 72 61 63 74 65 72 20 74 6f 20  om character to 
1d3e6 61 66 66 69 6e 69 74 79 20 69 73 20 67 69 76 65  affinity is give
1d3e7 6e 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f  n by the SQLITE_
1d3e8 41 46 46 5f 0a 2a 2a 20 6d 61 63 72 6f 73 20 64  AFF_.** macros d
1d3e9 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65  efined in sqlite
1d3ea 49 6e 74 2e 68 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  Int.h..**.** If 
1d3eb 50 34 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20  P4 is NULL then 
1d3ec 61 6c 6c 20 69 6e 64 65 78 20 66 69 65 6c 64 73  all index fields
1d3ed 20 68 61 76 65 20 74 68 65 20 61 66 66 69 6e 69   have the affini
1d3ee 74 79 20 4e 4f 4e 45 2e 0a 2a 2f 0a 63 61 73 65  ty NONE..*/.case
1d3ef 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 3a 20   OP_MakeRecord: 
1d3f0 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  {.#if 0  /* loca
1d3f1 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1d3f2 64 20 69 6e 74 6f 20 75 2e 61 6f 20 2a 2f 0a 20  d into u.ao */. 
1d3f3 20 75 38 20 2a 7a 4e 65 77 52 65 63 6f 72 64 3b   u8 *zNewRecord;
1d3f4 20 20 20 20 20 20 20 20 2f 2a 20 41 20 62 75 66          /* A buf
1d3f5 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  fer to hold the 
1d3f6 64 61 74 61 20 66 6f 72 20 74 68 65 20 6e 65 77  data for the new
1d3f7 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d   record */.  Mem
1d3f8 20 2a 70 52 65 63 3b 20 20 20 20 20 20 20 20 20   *pRec;         
1d3f9 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72      /* The new r
1d3fa 65 63 6f 72 64 20 2a 2f 0a 20 20 75 36 34 20 6e  ecord */.  u64 n
1d3fb 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  Data;           
1d3fc 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
1d3fd 79 74 65 73 20 6f 66 20 64 61 74 61 20 73 70 61  ytes of data spa
1d3fe 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 64 72  ce */.  int nHdr
1d3ff 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1d400 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
1d401 73 20 6f 66 20 68 65 61 64 65 72 20 73 70 61 63  s of header spac
1d402 65 20 2a 2f 0a 20 20 69 36 34 20 6e 42 79 74 65  e */.  i64 nByte
1d403 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
1d404 20 44 61 74 61 20 73 70 61 63 65 20 72 65 71 75   Data space requ
1d405 69 72 65 64 20 66 6f 72 20 74 68 69 73 20 72 65  ired for this re
1d406 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a  cord */.  int nZ
1d407 65 72 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  ero;            
1d408 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65   /* Number of ze
1d409 72 6f 20 62 79 74 65 73 20 61 74 20 74 68 65 20  ro bytes at the 
1d40a 65 6e 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72  end of the recor
1d40b 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 69  d */.  int nVari
1d40c 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  nt;           /*
1d40d 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
1d40e 20 69 6e 20 61 20 76 61 72 69 6e 74 20 2a 2f 0a   in a varint */.
1d40f 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70    u32 serial_typ
1d410 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65  e;       /* Type
1d411 20 66 69 65 6c 64 20 2a 2f 0a 20 20 4d 65 6d 20   field */.  Mem 
1d412 2a 70 44 61 74 61 30 3b 20 20 20 20 20 20 20 20  *pData0;        
1d413 20 20 20 2f 2a 20 46 69 72 73 74 20 66 69 65 6c     /* First fiel
1d414 64 20 74 6f 20 62 65 20 63 6f 6d 62 69 6e 65 64  d to be combined
1d415 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f 72 64   into the record
1d416 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4c 61 73 74   */.  Mem *pLast
1d417 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
1d418 4c 61 73 74 20 66 69 65 6c 64 20 6f 66 20 74 68  Last field of th
1d419 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e  e record */.  in
1d41a 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20  t nField;       
1d41b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1d41c 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20  f fields in the 
1d41d 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 68 61 72  record */.  char
1d41e 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20 20 20   *zAffinity;    
1d41f 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69     /* The affini
1d420 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68  ty string for th
1d421 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e  e record */.  in
1d422 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 20 20  t file_format;  
1d423 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 66 6f 72       /* File for
1d424 6d 61 74 20 74 6f 20 75 73 65 20 66 6f 72 20 65  mat to use for e
1d425 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74  ncoding */.  int
1d426 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
1d427 20 20 20 20 2f 2a 20 53 70 61 63 65 20 75 73 65      /* Space use
1d428 64 20 69 6e 20 7a 4e 65 77 52 65 63 6f 72 64 5b  d in zNewRecord[
1d429 5d 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 3b 20  ] */.  int len; 
1d42a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1d42b 20 4c 65 6e 67 74 68 20 6f 66 20 61 20 66 69 65   Length of a fie
1d42c 6c 64 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20  ld */.#endif /* 
1d42d 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1d42e 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6f 20  moved into u.ao 
1d42f 2a 2f 0a 0a 20 20 2f 2a 20 41 73 73 75 6d 69 6e  */..  /* Assumin
1d430 67 20 74 68 65 20 72 65 63 6f 72 64 20 63 6f 6e  g the record con
1d431 74 61 69 6e 73 20 4e 20 66 69 65 6c 64 73 2c 20  tains N fields, 
1d432 74 68 65 20 72 65 63 6f 72 64 20 66 6f 72 6d 61  the record forma
1d433 74 20 6c 6f 6f 6b 73 0a 20 20 2a 2a 20 6c 69 6b  t looks.  ** lik
1d434 65 20 74 68 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a  e this:.  **.  *
1d435 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  * --------------
1d436 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d437 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d438 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d439 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20  ----------.  ** 
1d43a 7c 20 68 64 72 2d 73 69 7a 65 20 7c 20 74 79 70  | hdr-size | typ
1d43b 65 20 30 20 7c 20 74 79 70 65 20 31 20 7c 20 2e  e 0 | type 1 | .
1d43c 2e 2e 20 7c 20 74 79 70 65 20 4e 2d 31 20 7c 20  .. | type N-1 | 
1d43d 64 61 74 61 30 20 7c 20 2e 2e 2e 20 7c 20 64 61  data0 | ... | da
1d43e 74 61 20 4e 2d 31 20 7c 0a 20 20 2a 2a 20 2d 2d  ta N-1 |.  ** --
1d43f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d440 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d441 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d442 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d443 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 0a 20 20 2a 2a  ------.  **.  **
1d444 20 44 61 74 61 28 30 29 20 69 73 20 74 61 6b 65   Data(0) is take
1d445 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20  n from register 
1d446 50 31 2e 20 20 44 61 74 61 28 31 29 20 63 6f 6d  P1.  Data(1) com
1d447 65 73 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72  es from register
1d448 20 50 31 2b 31 0a 20 20 2a 2a 20 61 6e 64 20 73   P1+1.  ** and s
1d449 6f 20 66 72 6f 74 68 2e 0a 20 20 2a 2a 0a 20 20  o froth..  **.  
1d44a 2a 2a 20 45 61 63 68 20 74 79 70 65 20 66 69 65  ** Each type fie
1d44b 6c 64 20 69 73 20 61 20 76 61 72 69 6e 74 20 72  ld is a varint r
1d44c 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20  epresenting the 
1d44d 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74  serial type of t
1d44e 68 65 0a 20 20 2a 2a 20 63 6f 72 72 65 73 70 6f  he.  ** correspo
1d44f 6e 64 69 6e 67 20 64 61 74 61 20 65 6c 65 6d 65  nding data eleme
1d450 6e 74 20 28 73 65 65 20 73 71 6c 69 74 65 33 56  nt (see sqlite3V
1d451 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 29 29  dbeSerialType())
1d452 2e 20 54 68 65 0a 20 20 2a 2a 20 68 64 72 2d 73  . The.  ** hdr-s
1d453 69 7a 65 20 66 69 65 6c 64 20 69 73 20 61 6c 73  ize field is als
1d454 6f 20 61 20 76 61 72 69 6e 74 20 77 68 69 63 68  o a varint which
1d455 20 69 73 20 74 68 65 20 6f 66 66 73 65 74 20 66   is the offset f
1d456 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  rom the beginnin
1d457 67 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65  g.  ** of the re
1d458 63 6f 72 64 20 74 6f 20 64 61 74 61 30 2e 0a 20  cord to data0.. 
1d459 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 44 61 74 61   */.  u.ao.nData
1d45a 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a   = 0;         /*
1d45b 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
1d45c 20 6f 66 20 64 61 74 61 20 73 70 61 63 65 20 2a   of data space *
1d45d 2f 0a 20 20 75 2e 61 6f 2e 6e 48 64 72 20 3d 20  /.  u.ao.nHdr = 
1d45e 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  0;          /* N
1d45f 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
1d460 66 20 68 65 61 64 65 72 20 73 70 61 63 65 20 2a  f header space *
1d461 2f 0a 20 20 75 2e 61 6f 2e 6e 42 79 74 65 20 3d  /.  u.ao.nByte =
1d462 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 44   0;         /* D
1d463 61 74 61 20 73 70 61 63 65 20 72 65 71 75 69 72  ata space requir
1d464 65 64 20 66 6f 72 20 74 68 69 73 20 72 65 63 6f  ed for this reco
1d465 72 64 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 5a 65  rd */.  u.ao.nZe
1d466 72 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ro = 0;         
1d467 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72  /* Number of zer
1d468 6f 20 62 79 74 65 73 20 61 74 20 74 68 65 20 65  o bytes at the e
1d469 6e 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64  nd of the record
1d46a 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 46 69 65 6c   */.  u.ao.nFiel
1d46b 64 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75  d = pOp->p1;.  u
1d46c 2e 61 6f 2e 7a 41 66 66 69 6e 69 74 79 20 3d 20  .ao.zAffinity = 
1d46d 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 61 73 73  pOp->p4.z;.  ass
1d46e 65 72 74 28 20 75 2e 61 6f 2e 6e 46 69 65 6c 64  ert( u.ao.nField
1d46f 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 3e 30 20  >0 && pOp->p2>0 
1d470 26 26 20 70 4f 70 2d 3e 70 32 2b 75 2e 61 6f 2e  && pOp->p2+u.ao.
1d471 6e 46 69 65 6c 64 3c 3d 70 2d 3e 6e 4d 65 6d 2b  nField<=p->nMem+
1d472 31 20 29 3b 0a 20 20 75 2e 61 6f 2e 70 44 61 74  1 );.  u.ao.pDat
1d473 61 30 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e  a0 = &p->aMem[u.
1d474 61 6f 2e 6e 46 69 65 6c 64 5d 3b 0a 20 20 75 2e  ao.nField];.  u.
1d475 61 6f 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d  ao.nField = pOp-
1d476 3e 70 32 3b 0a 20 20 75 2e 61 6f 2e 70 4c 61 73  >p2;.  u.ao.pLas
1d477 74 20 3d 20 26 75 2e 61 6f 2e 70 44 61 74 61 30  t = &u.ao.pData0
1d478 5b 75 2e 61 6f 2e 6e 46 69 65 6c 64 2d 31 5d 3b  [u.ao.nField-1];
1d479 0a 20 20 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f 72  .  u.ao.file_for
1d47a 6d 61 74 20 3d 20 70 2d 3e 6d 69 6e 57 72 69 74  mat = p->minWrit
1d47b 65 46 69 6c 65 46 6f 72 6d 61 74 3b 0a 0a 20 20  eFileFormat;..  
1d47c 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20  /* Loop through 
1d47d 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 61  the elements tha
1d47e 74 20 77 69 6c 6c 20 6d 61 6b 65 20 75 70 20 74  t will make up t
1d47f 68 65 20 72 65 63 6f 72 64 20 74 6f 20 66 69 67  he record to fig
1d480 75 72 65 0a 20 20 2a 2a 20 6f 75 74 20 68 6f 77  ure.  ** out how
1d481 20 6d 75 63 68 20 73 70 61 63 65 20 69 73 20 72   much space is r
1d482 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 65 20  equired for the 
1d483 6e 65 77 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2f  new record..  */
1d484 0a 20 20 66 6f 72 28 75 2e 61 6f 2e 70 52 65 63  .  for(u.ao.pRec
1d485 3d 75 2e 61 6f 2e 70 44 61 74 61 30 3b 20 75 2e  =u.ao.pData0; u.
1d486 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f 2e 70 4c  ao.pRec<=u.ao.pL
1d487 61 73 74 3b 20 75 2e 61 6f 2e 70 52 65 63 2b 2b  ast; u.ao.pRec++
1d488 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 61 6f 2e  ){.    if( u.ao.
1d489 7a 41 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20  zAffinity ){.   
1d48a 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79     applyAffinity
1d48b 28 75 2e 61 6f 2e 70 52 65 63 2c 20 75 2e 61 6f  (u.ao.pRec, u.ao
1d48c 2e 7a 41 66 66 69 6e 69 74 79 5b 75 2e 61 6f 2e  .zAffinity[u.ao.
1d48d 70 52 65 63 2d 75 2e 61 6f 2e 70 44 61 74 61 30  pRec-u.ao.pData0
1d48e 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20  ], encoding);.  
1d48f 20 20 7d 0a 20 20 20 20 69 66 28 20 75 2e 61 6f    }.    if( u.ao
1d490 2e 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d  .pRec->flags&MEM
1d491 5f 5a 65 72 6f 20 26 26 20 75 2e 61 6f 2e 70 52  _Zero && u.ao.pR
1d492 65 63 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 20  ec->n>0 ){.     
1d493 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45   sqlite3VdbeMemE
1d494 78 70 61 6e 64 42 6c 6f 62 28 75 2e 61 6f 2e 70  xpandBlob(u.ao.p
1d495 52 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Rec);.    }.    
1d496 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65  u.ao.serial_type
1d497 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65   = sqlite3VdbeSe
1d498 72 69 61 6c 54 79 70 65 28 75 2e 61 6f 2e 70 52  rialType(u.ao.pR
1d499 65 63 2c 20 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f  ec, u.ao.file_fo
1d49a 72 6d 61 74 29 3b 0a 20 20 20 20 75 2e 61 6f 2e  rmat);.    u.ao.
1d49b 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 56 64 62  len = sqlite3Vdb
1d49c 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75  eSerialTypeLen(u
1d49d 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 29  .ao.serial_type)
1d49e 3b 0a 20 20 20 20 75 2e 61 6f 2e 6e 44 61 74 61  ;.    u.ao.nData
1d49f 20 2b 3d 20 75 2e 61 6f 2e 6c 65 6e 3b 0a 20 20   += u.ao.len;.  
1d4a0 20 20 75 2e 61 6f 2e 6e 48 64 72 20 2b 3d 20 73    u.ao.nHdr += s
1d4a1 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28  qlite3VarintLen(
1d4a2 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65  u.ao.serial_type
1d4a3 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 6f 2e  );.    if( u.ao.
1d4a4 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pRec->flags & ME
1d4a5 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20  M_Zero ){.      
1d4a6 2f 2a 20 4f 6e 6c 79 20 70 75 72 65 20 7a 65 72  /* Only pure zer
1d4a7 6f 2d 66 69 6c 6c 65 64 20 42 4c 4f 42 73 20 63  o-filled BLOBs c
1d4a8 61 6e 20 62 65 20 69 6e 70 75 74 20 74 6f 20 74  an be input to t
1d4a9 68 69 73 20 4f 70 63 6f 64 65 2e 0a 20 20 20 20  his Opcode..    
1d4aa 20 20 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20 61    ** We do not a
1d4ab 6c 6c 6f 77 20 62 6c 6f 62 73 20 77 69 74 68 20  llow blobs with 
1d4ac 61 20 70 72 65 66 69 78 20 61 6e 64 20 61 20 7a  a prefix and a z
1d4ad 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 2e  ero-filled tail.
1d4ae 20 2a 2f 0a 20 20 20 20 20 20 75 2e 61 6f 2e 6e   */.      u.ao.n
1d4af 5a 65 72 6f 20 2b 3d 20 75 2e 61 6f 2e 70 52 65  Zero += u.ao.pRe
1d4b0 63 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20  c->u.nZero;.    
1d4b1 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 6f 2e 6c  }else if( u.ao.l
1d4b2 65 6e 20 29 7b 0a 20 20 20 20 20 20 75 2e 61 6f  en ){.      u.ao
1d4b3 2e 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 20 20  .nZero = 0;.    
1d4b4 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20  }.  }..  /* Add 
1d4b5 74 68 65 20 69 6e 69 74 69 61 6c 20 68 65 61 64  the initial head
1d4b6 65 72 20 76 61 72 69 6e 74 20 61 6e 64 20 74 6f  er varint and to
1d4b7 74 61 6c 20 74 68 65 20 73 69 7a 65 20 2a 2f 0a  tal the size */.
1d4b8 20 20 75 2e 61 6f 2e 6e 48 64 72 20 2b 3d 20 75    u.ao.nHdr += u
1d4b9 2e 61 6f 2e 6e 56 61 72 69 6e 74 20 3d 20 73 71  .ao.nVarint = sq
1d4ba 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75  lite3VarintLen(u
1d4bb 2e 61 6f 2e 6e 48 64 72 29 3b 0a 20 20 69 66 28  .ao.nHdr);.  if(
1d4bc 20 75 2e 61 6f 2e 6e 56 61 72 69 6e 74 3c 73 71   u.ao.nVarint<sq
1d4bd 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75  lite3VarintLen(u
1d4be 2e 61 6f 2e 6e 48 64 72 29 20 29 7b 0a 20 20 20  .ao.nHdr) ){.   
1d4bf 20 75 2e 61 6f 2e 6e 48 64 72 2b 2b 3b 0a 20 20   u.ao.nHdr++;.  
1d4c0 7d 0a 20 20 75 2e 61 6f 2e 6e 42 79 74 65 20 3d  }.  u.ao.nByte =
1d4c1 20 75 2e 61 6f 2e 6e 48 64 72 2b 75 2e 61 6f 2e   u.ao.nHdr+u.ao.
1d4c2 6e 44 61 74 61 2d 75 2e 61 6f 2e 6e 5a 65 72 6f  nData-u.ao.nZero
1d4c3 3b 0a 20 20 69 66 28 20 75 2e 61 6f 2e 6e 42 79  ;.  if( u.ao.nBy
1d4c4 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  te>db->aLimit[SQ
1d4c5 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
1d4c6 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74  H] ){.    goto t
1d4c7 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a 20 20 2f  oo_big;.  }..  /
1d4c8 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20  * Make sure the 
1d4c9 6f 75 74 70 75 74 20 72 65 67 69 73 74 65 72 20  output register 
1d4ca 68 61 73 20 61 20 62 75 66 66 65 72 20 6c 61 72  has a buffer lar
1d4cb 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 73 74 6f  ge enough to sto
1d4cc 72 65 0a 20 20 2a 2a 20 74 68 65 20 6e 65 77 20  re.  ** the new 
1d4cd 72 65 63 6f 72 64 2e 20 54 68 65 20 6f 75 74 70  record. The outp
1d4ce 75 74 20 72 65 67 69 73 74 65 72 20 28 70 4f 70  ut register (pOp
1d4cf 2d 3e 70 33 29 20 69 73 20 6e 6f 74 20 61 6c 6c  ->p3) is not all
1d4d0 6f 77 65 64 20 74 6f 0a 20 20 2a 2a 20 62 65 20  owed to.  ** be 
1d4d1 6f 6e 65 20 6f 66 20 74 68 65 20 69 6e 70 75 74  one of the input
1d4d2 20 72 65 67 69 73 74 65 72 73 20 28 62 65 63 61   registers (beca
1d4d3 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  use the followin
1d4d4 67 20 63 61 6c 6c 20 74 6f 0a 20 20 2a 2a 20 73  g call to.  ** s
1d4d5 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f  qlite3VdbeMemGro
1d4d6 77 28 29 20 63 6f 75 6c 64 20 63 6c 6f 62 62 65  w() could clobbe
1d4d7 72 20 74 68 65 20 76 61 6c 75 65 20 62 65 66 6f  r the value befo
1d4d8 72 65 20 69 74 20 69 73 20 75 73 65 64 29 2e 0a  re it is used)..
1d4d9 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
1d4da 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 31 20 7c  Op->p3<pOp->p1 |
1d4db 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e  | pOp->p3>=pOp->
1d4dc 70 31 2b 70 4f 70 2d 3e 70 32 20 29 3b 0a 20 20  p1+pOp->p2 );.  
1d4dd 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pOut = &p->aMem[
1d4de 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20  pOp->p3];.  if( 
1d4df 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
1d4e0 6f 77 28 70 4f 75 74 2c 20 28 69 6e 74 29 75 2e  ow(pOut, (int)u.
1d4e1 61 6f 2e 6e 42 79 74 65 2c 20 30 29 20 29 7b 0a  ao.nByte, 0) ){.
1d4e2 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b      goto no_mem;
1d4e3 0a 20 20 7d 0a 20 20 75 2e 61 6f 2e 7a 4e 65 77  .  }.  u.ao.zNew
1d4e4 52 65 63 6f 72 64 20 3d 20 28 75 38 20 2a 29 70  Record = (u8 *)p
1d4e5 4f 75 74 2d 3e 7a 3b 0a 0a 20 20 2f 2a 20 57 72  Out->z;..  /* Wr
1d4e6 69 74 65 20 74 68 65 20 72 65 63 6f 72 64 20 2a  ite the record *
1d4e7 2f 0a 20 20 75 2e 61 6f 2e 69 20 3d 20 70 75 74  /.  u.ao.i = put
1d4e8 56 61 72 69 6e 74 33 32 28 75 2e 61 6f 2e 7a 4e  Varint32(u.ao.zN
1d4e9 65 77 52 65 63 6f 72 64 2c 20 75 2e 61 6f 2e 6e  ewRecord, u.ao.n
1d4ea 48 64 72 29 3b 0a 20 20 66 6f 72 28 75 2e 61 6f  Hdr);.  for(u.ao
1d4eb 2e 70 52 65 63 3d 75 2e 61 6f 2e 70 44 61 74 61  .pRec=u.ao.pData
1d4ec 30 3b 20 75 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e  0; u.ao.pRec<=u.
1d4ed 61 6f 2e 70 4c 61 73 74 3b 20 75 2e 61 6f 2e 70  ao.pLast; u.ao.p
1d4ee 52 65 63 2b 2b 29 7b 0a 20 20 20 20 75 2e 61 6f  Rec++){.    u.ao
1d4ef 2e 73 65 72 69 61 6c 5f 74 79 70 65 20 3d 20 73  .serial_type = s
1d4f0 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
1d4f1 54 79 70 65 28 75 2e 61 6f 2e 70 52 65 63 2c 20  Type(u.ao.pRec, 
1d4f2 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f 72 6d 61 74  u.ao.file_format
1d4f3 29 3b 0a 20 20 20 20 75 2e 61 6f 2e 69 20 2b 3d  );.    u.ao.i +=
1d4f4 20 70 75 74 56 61 72 69 6e 74 33 32 28 26 75 2e   putVarint32(&u.
1d4f5 61 6f 2e 7a 4e 65 77 52 65 63 6f 72 64 5b 75 2e  ao.zNewRecord[u.
1d4f6 61 6f 2e 69 5d 2c 20 75 2e 61 6f 2e 73 65 72 69  ao.i], u.ao.seri
1d4f7 61 6c 5f 74 79 70 65 29 3b 20 20 20 20 20 20 2f  al_type);      /
1d4f8 2a 20 73 65 72 69 61 6c 20 74 79 70 65 20 2a 2f  * serial type */
1d4f9 0a 20 20 7d 0a 20 20 66 6f 72 28 75 2e 61 6f 2e  .  }.  for(u.ao.
1d4fa 70 52 65 63 3d 75 2e 61 6f 2e 70 44 61 74 61 30  pRec=u.ao.pData0
1d4fb 3b 20 75 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e 61  ; u.ao.pRec<=u.a
1d4fc 6f 2e 70 4c 61 73 74 3b 20 75 2e 61 6f 2e 70 52  o.pLast; u.ao.pR
1d4fd 65 63 2b 2b 29 7b 20 20 2f 2a 20 73 65 72 69 61  ec++){  /* seria
1d4fe 6c 20 64 61 74 61 20 2a 2f 0a 20 20 20 20 75 2e  l data */.    u.
1d4ff 61 6f 2e 69 20 2b 3d 20 73 71 6c 69 74 65 33 56  ao.i += sqlite3V
1d500 64 62 65 53 65 72 69 61 6c 50 75 74 28 26 75 2e  dbeSerialPut(&u.
1d501 61 6f 2e 7a 4e 65 77 52 65 63 6f 72 64 5b 75 2e  ao.zNewRecord[u.
1d502 61 6f 2e 69 5d 2c 20 28 69 6e 74 29 28 75 2e 61  ao.i], (int)(u.a
1d503 6f 2e 6e 42 79 74 65 2d 75 2e 61 6f 2e 69 29 2c  o.nByte-u.ao.i),
1d504 20 75 2e 61 6f 2e 70 52 65 63 2c 75 2e 61 6f 2e   u.ao.pRec,u.ao.
1d505 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20  file_format);.  
1d506 7d 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6f  }.  assert( u.ao
1d507 2e 69 3d 3d 75 2e 61 6f 2e 6e 42 79 74 65 20 29  .i==u.ao.nByte )
1d508 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  ;..  assert( pOp
1d509 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ->p3>0 && pOp->p
1d50a 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  3<=p->nMem );.  
1d50b 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e 74 29 75  pOut->n = (int)u
1d50c 2e 61 6f 2e 6e 42 79 74 65 3b 0a 20 20 70 4f 75  .ao.nByte;.  pOu
1d50d 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42  t->flags = MEM_B
1d50e 6c 6f 62 20 7c 20 4d 45 4d 5f 44 79 6e 3b 0a 20  lob | MEM_Dyn;. 
1d50f 20 70 4f 75 74 2d 3e 78 44 65 6c 20 3d 20 30 3b   pOut->xDel = 0;
1d510 0a 20 20 69 66 28 20 75 2e 61 6f 2e 6e 5a 65 72  .  if( u.ao.nZer
1d511 6f 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 75  o ){.    pOut->u
1d512 2e 6e 5a 65 72 6f 20 3d 20 75 2e 61 6f 2e 6e 5a  .nZero = u.ao.nZ
1d513 65 72 6f 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66  ero;.    pOut->f
1d514 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 5a 65 72 6f  lags |= MEM_Zero
1d515 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e  ;.  }.  pOut->en
1d516 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b  c = SQLITE_UTF8;
1d517 20 20 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65    /* In case the
1d518 20 62 6c 6f 62 20 69 73 20 65 76 65 72 20 63 6f   blob is ever co
1d519 6e 76 65 72 74 65 64 20 74 6f 20 74 65 78 74 20  nverted to text 
1d51a 2a 2f 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52  */.  REGISTER_TR
1d51b 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 4f 75  ACE(pOp->p3, pOu
1d51c 74 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58  t);.  UPDATE_MAX
1d51d 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b  _BLOBSIZE(pOut);
1d51e 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1d51f 4f 70 63 6f 64 65 3a 20 43 6f 75 6e 74 20 50 31  Opcode: Count P1
1d520 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1d521 53 74 6f 72 65 20 74 68 65 20 6e 75 6d 62 65 72  Store the number
1d522 20 6f 66 20 65 6e 74 72 69 65 73 20 28 61 6e 20   of entries (an 
1d523 69 6e 74 65 67 65 72 20 76 61 6c 75 65 29 20 69  integer value) i
1d524 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69  n the table or i
1d525 6e 64 65 78 20 0a 2a 2a 20 6f 70 65 6e 65 64 20  ndex .** opened 
1d526 62 79 20 63 75 72 73 6f 72 20 50 31 20 69 6e 20  by cursor P1 in 
1d527 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2f 0a 23  register P2.*/.#
1d528 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1d529 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 63 61  IT_BTREECOUNT.ca
1d52a 73 65 20 4f 50 5f 43 6f 75 6e 74 3a 20 7b 20 20  se OP_Count: {  
1d52b 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70         /* out2-p
1d52c 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66  rerelease */.#if
1d52d 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1d52e 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1d52f 6f 20 75 2e 61 70 20 2a 2f 0a 20 20 69 36 34 20  o u.ap */.  i64 
1d530 6e 45 6e 74 72 79 3b 0a 20 20 42 74 43 75 72 73  nEntry;.  BtCurs
1d531 6f 72 20 2a 70 43 72 73 72 3b 0a 23 65 6e 64 69  or *pCrsr;.#endi
1d532 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1d533 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1d534 75 2e 61 70 20 2a 2f 0a 0a 20 20 75 2e 61 70 2e  u.ap */..  u.ap.
1d535 70 43 72 73 72 20 3d 20 70 2d 3e 61 70 43 73 72  pCrsr = p->apCsr
1d536 5b 70 4f 70 2d 3e 70 31 5d 2d 3e 70 43 75 72 73  [pOp->p1]->pCurs
1d537 6f 72 3b 0a 20 20 69 66 28 20 75 2e 61 70 2e 70  or;.  if( u.ap.p
1d538 43 72 73 72 20 29 7b 0a 20 20 20 20 72 63 20 3d  Crsr ){.    rc =
1d539 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 75   sqlite3BtreeCou
1d53a 6e 74 28 75 2e 61 70 2e 70 43 72 73 72 2c 20 26  nt(u.ap.pCrsr, &
1d53b 75 2e 61 70 2e 6e 45 6e 74 72 79 29 3b 0a 20 20  u.ap.nEntry);.  
1d53c 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 70 2e  }else{.    u.ap.
1d53d 6e 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 7d 0a  nEntry = 0;.  }.
1d53e 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20    pOut->flags = 
1d53f 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4f 75 74 2d  MEM_Int;.  pOut-
1d540 3e 75 2e 69 20 3d 20 75 2e 61 70 2e 6e 45 6e 74  >u.i = u.ap.nEnt
1d541 72 79 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23  ry;.  break;.}.#
1d542 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 63 6f 64 65  endif../* Opcode
1d543 3a 20 53 61 76 65 70 6f 69 6e 74 20 50 31 20 2a  : Savepoint P1 *
1d544 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70   * P4 *.**.** Op
1d545 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f 72 20 72  en, release or r
1d546 6f 6c 6c 62 61 63 6b 20 74 68 65 20 73 61 76 65  ollback the save
1d547 70 6f 69 6e 74 20 6e 61 6d 65 64 20 62 79 20 70  point named by p
1d548 61 72 61 6d 65 74 65 72 20 50 34 2c 20 64 65 70  arameter P4, dep
1d549 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68 65  ending.** on the
1d54a 20 76 61 6c 75 65 20 6f 66 20 50 31 2e 20 54 6f   value of P1. To
1d54b 20 6f 70 65 6e 20 61 20 6e 65 77 20 73 61 76 65   open a new save
1d54c 70 6f 69 6e 74 2c 20 50 31 3d 3d 30 2e 20 54 6f  point, P1==0. To
1d54d 20 72 65 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74   release (commit
1d54e 29 20 61 6e 0a 2a 2a 20 65 78 69 73 74 69 6e 67  ) an.** existing
1d54f 20 73 61 76 65 70 6f 69 6e 74 2c 20 50 31 3d 3d   savepoint, P1==
1d550 31 2c 20 6f 72 20 74 6f 20 72 6f 6c 6c 62 61 63  1, or to rollbac
1d551 6b 20 61 6e 20 65 78 69 73 74 69 6e 67 20 73 61  k an existing sa
1d552 76 65 70 6f 69 6e 74 20 50 31 3d 3d 32 2e 0a 2a  vepoint P1==2..*
1d553 2f 0a 63 61 73 65 20 4f 50 5f 53 61 76 65 70 6f  /.case OP_Savepo
1d554 69 6e 74 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a  int: {.#if 0  /*
1d555 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1d556 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 71   moved into u.aq
1d557 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20 20   */.  int p1;   
1d558 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d559 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f        /* Value o
1d55a 66 20 50 31 20 6f 70 65 72 61 6e 64 20 2a 2f 0a  f P1 operand */.
1d55b 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
1d55c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d55d 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 73 61 76    /* Name of sav
1d55e 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  epoint */.  int 
1d55f 6e 4e 61 6d 65 3b 0a 20 20 53 61 76 65 70 6f 69  nName;.  Savepoi
1d560 6e 74 20 2a 70 4e 65 77 3b 0a 20 20 53 61 76 65  nt *pNew;.  Save
1d561 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f 69 6e  point *pSavepoin
1d562 74 3b 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 2a  t;.  Savepoint *
1d563 70 54 6d 70 3b 0a 20 20 69 6e 74 20 69 53 61 76  pTmp;.  int iSav
1d564 65 70 6f 69 6e 74 3b 0a 20 20 69 6e 74 20 69 69  epoint;.  int ii
1d565 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1d566 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1d567 64 20 69 6e 74 6f 20 75 2e 61 71 20 2a 2f 0a 0a  d into u.aq */..
1d568 20 20 75 2e 61 71 2e 70 31 20 3d 20 70 4f 70 2d    u.aq.p1 = pOp-
1d569 3e 70 31 3b 0a 20 20 75 2e 61 71 2e 7a 4e 61 6d  >p1;.  u.aq.zNam
1d56a 65 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 0a  e = pOp->p4.z;..
1d56b 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74    /* Assert that
1d56c 20 74 68 65 20 75 2e 61 71 2e 70 31 20 70 61 72   the u.aq.p1 par
1d56d 61 6d 65 74 65 72 20 69 73 20 76 61 6c 69 64 2e  ameter is valid.
1d56e 20 41 6c 73 6f 20 74 68 61 74 20 69 66 20 74 68   Also that if th
1d56f 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e 0a 20  ere is no open. 
1d570 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c   ** transaction,
1d571 20 74 68 65 6e 20 74 68 65 72 65 20 63 61 6e 6e   then there cann
1d572 6f 74 20 62 65 20 61 6e 79 20 73 61 76 65 70 6f  ot be any savepo
1d573 69 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  ints..  */.  ass
1d574 65 72 74 28 20 64 62 2d 3e 70 53 61 76 65 70 6f  ert( db->pSavepo
1d575 69 6e 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 75  int==0 || db->au
1d576 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 3b 0a 20  toCommit==0 );. 
1d577 20 61 73 73 65 72 74 28 20 75 2e 61 71 2e 70 31   assert( u.aq.p1
1d578 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49  ==SAVEPOINT_BEGI
1d579 4e 7c 7c 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45  N||u.aq.p1==SAVE
1d57a 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 7c 7c 75  POINT_RELEASE||u
1d57b 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e  .aq.p1==SAVEPOIN
1d57c 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20  T_ROLLBACK );.  
1d57d 61 73 73 65 72 74 28 20 64 62 2d 3e 70 53 61 76  assert( db->pSav
1d57e 65 70 6f 69 6e 74 20 7c 7c 20 64 62 2d 3e 69 73  epoint || db->is
1d57f 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70  TransactionSavep
1d580 6f 69 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73  oint==0 );.  ass
1d581 65 72 74 28 20 63 68 65 63 6b 53 61 76 65 70 6f  ert( checkSavepo
1d582 69 6e 74 43 6f 75 6e 74 28 64 62 29 20 29 3b 0a  intCount(db) );.
1d583 0a 20 20 69 66 28 20 75 2e 61 71 2e 70 31 3d 3d  .  if( u.aq.p1==
1d584 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49 4e 20  SAVEPOINT_BEGIN 
1d585 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 77  ){.    if( db->w
1d586 72 69 74 65 56 64 62 65 43 6e 74 3e 30 20 29 7b  riteVdbeCnt>0 ){
1d587 0a 20 20 20 20 20 20 2f 2a 20 41 20 6e 65 77 20  .      /* A new 
1d588 73 61 76 65 70 6f 69 6e 74 20 63 61 6e 6e 6f 74  savepoint cannot
1d589 20 62 65 20 63 72 65 61 74 65 64 20 69 66 20 74   be created if t
1d58a 68 65 72 65 20 61 72 65 20 61 63 74 69 76 65 20  here are active 
1d58b 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 73  write.      ** s
1d58c 74 61 74 65 6d 65 6e 74 73 20 28 69 2e 65 2e 20  tatements (i.e. 
1d58d 6f 70 65 6e 20 72 65 61 64 2f 77 72 69 74 65 20  open read/write 
1d58e 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62  incremental blob
1d58f 20 68 61 6e 64 6c 65 73 29 2e 0a 20 20 20 20 20   handles)..     
1d590 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
1d591 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a  3SetString(&p->z
1d592 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e  ErrMsg, db, "can
1d593 6e 6f 74 20 6f 70 65 6e 20 73 61 76 65 70 6f 69  not open savepoi
1d594 6e 74 20 2d 20 22 0a 20 20 20 20 20 20 20 20 22  nt - ".        "
1d595 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69  SQL statements i
1d596 6e 20 70 72 6f 67 72 65 73 73 22 29 3b 0a 20 20  n progress");.  
1d597 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1d598 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  BUSY;.    }else{
1d599 0a 20 20 20 20 20 20 75 2e 61 71 2e 6e 4e 61 6d  .      u.aq.nNam
1d59a 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  e = sqlite3Strle
1d59b 6e 33 30 28 75 2e 61 71 2e 7a 4e 61 6d 65 29 3b  n30(u.aq.zName);
1d59c 0a 0a 20 20 20 20 20 20 2f 2a 20 43 72 65 61 74  ..      /* Creat
1d59d 65 20 61 20 6e 65 77 20 73 61 76 65 70 6f 69 6e  e a new savepoin
1d59e 74 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a  t structure. */.
1d59f 20 20 20 20 20 20 75 2e 61 71 2e 70 4e 65 77 20        u.aq.pNew 
1d5a0 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
1d5a1 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28  cRaw(db, sizeof(
1d5a2 53 61 76 65 70 6f 69 6e 74 29 2b 75 2e 61 71 2e  Savepoint)+u.aq.
1d5a3 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 20 20 20 20  nName+1);.      
1d5a4 69 66 28 20 75 2e 61 71 2e 70 4e 65 77 20 29 7b  if( u.aq.pNew ){
1d5a5 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e 70 4e  .        u.aq.pN
1d5a6 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 61  ew->zName = (cha
1d5a7 72 20 2a 29 26 75 2e 61 71 2e 70 4e 65 77 5b 31  r *)&u.aq.pNew[1
1d5a8 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  ];.        memcp
1d5a9 79 28 75 2e 61 71 2e 70 4e 65 77 2d 3e 7a 4e 61  y(u.aq.pNew->zNa
1d5aa 6d 65 2c 20 75 2e 61 71 2e 7a 4e 61 6d 65 2c 20  me, u.aq.zName, 
1d5ab 75 2e 61 71 2e 6e 4e 61 6d 65 2b 31 29 3b 0a 0a  u.aq.nName+1);..
1d5ac 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68          /* If th
1d5ad 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e 20 74  ere is no open t
1d5ae 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e  ransaction, then
1d5af 20 6d 61 72 6b 20 74 68 69 73 20 61 73 20 61 20   mark this as a 
1d5b0 73 70 65 63 69 61 6c 0a 20 20 20 20 20 20 20 20  special.        
1d5b1 2a 2a 20 22 74 72 61 6e 73 61 63 74 69 6f 6e 20  ** "transaction 
1d5b2 73 61 76 65 70 6f 69 6e 74 22 2e 20 2a 2f 0a 20  savepoint". */. 
1d5b3 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 61         if( db->a
1d5b4 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20  utoCommit ){.   
1d5b5 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43         db->autoC
1d5b6 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20  ommit = 0;.     
1d5b7 20 20 20 20 20 64 62 2d 3e 69 73 54 72 61 6e 73       db->isTrans
1d5b8 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 20  actionSavepoint 
1d5b9 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  = 1;.        }el
1d5ba 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62  se{.          db
1d5bb 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2b 2b 3b 0a  ->nSavepoint++;.
1d5bc 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20          }..     
1d5bd 20 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 6e     /* Link the n
1d5be 65 77 20 73 61 76 65 70 6f 69 6e 74 20 69 6e 74  ew savepoint int
1d5bf 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  o the database h
1d5c0 61 6e 64 6c 65 27 73 20 6c 69 73 74 2e 20 2a 2f  andle's list. */
1d5c1 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e 70 4e  .        u.aq.pN
1d5c2 65 77 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e  ew->pNext = db->
1d5c3 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20  pSavepoint;.    
1d5c4 20 20 20 20 64 62 2d 3e 70 53 61 76 65 70 6f 69      db->pSavepoi
1d5c5 6e 74 20 3d 20 75 2e 61 71 2e 70 4e 65 77 3b 0a  nt = u.aq.pNew;.
1d5c6 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1d5c7 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 71 2e  }else{.    u.aq.
1d5c8 69 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a  iSavepoint = 0;.
1d5c9 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 74 68 65  .    /* Find the
1d5ca 20 6e 61 6d 65 64 20 73 61 76 65 70 6f 69 6e 74   named savepoint
1d5cb 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f  . If there is no
1d5cc 20 73 75 63 68 20 73 61 76 65 70 6f 69 6e 74 2c   such savepoint,
1d5cd 20 74 68 65 6e 20 61 6e 0a 20 20 20 20 2a 2a 20   then an.    ** 
1d5ce 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75  an error is retu
1d5cf 72 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65 72  rned to the user
1d5d0 2e 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 0a 20  .  */.    for(. 
1d5d1 20 20 20 20 20 75 2e 61 71 2e 70 53 61 76 65 70       u.aq.pSavep
1d5d2 6f 69 6e 74 20 3d 20 64 62 2d 3e 70 53 61 76 65  oint = db->pSave
1d5d3 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 75 2e 61  point;.      u.a
1d5d4 71 2e 70 53 61 76 65 70 6f 69 6e 74 20 26 26 20  q.pSavepoint && 
1d5d5 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 75  sqlite3StrICmp(u
1d5d6 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 2d 3e  .aq.pSavepoint->
1d5d7 7a 4e 61 6d 65 2c 20 75 2e 61 71 2e 7a 4e 61 6d  zName, u.aq.zNam
1d5d8 65 29 3b 0a 20 20 20 20 20 20 75 2e 61 71 2e 70  e);.      u.aq.p
1d5d9 53 61 76 65 70 6f 69 6e 74 20 3d 20 75 2e 61 71  Savepoint = u.aq
1d5da 2e 70 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65  .pSavepoint->pNe
1d5db 78 74 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  xt.    ){.      
1d5dc 75 2e 61 71 2e 69 53 61 76 65 70 6f 69 6e 74 2b  u.aq.iSavepoint+
1d5dd 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  +;.    }.    if(
1d5de 20 21 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e   !u.aq.pSavepoin
1d5df 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  t ){.      sqlit
1d5e0 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
1d5e1 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 6e 6f  zErrMsg, db, "no
1d5e2 20 73 75 63 68 20 73 61 76 65 70 6f 69 6e 74 3a   such savepoint:
1d5e3 20 25 73 22 2c 20 75 2e 61 71 2e 7a 4e 61 6d 65   %s", u.aq.zName
1d5e4 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  );.      rc = SQ
1d5e5 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
1d5e6 7d 65 6c 73 65 20 69 66 28 0a 20 20 20 20 20 20  }else if(.      
1d5e7 20 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43    db->writeVdbeC
1d5e8 6e 74 3e 30 20 7c 7c 20 28 75 2e 61 71 2e 70 31  nt>0 || (u.aq.p1
1d5e9 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  ==SAVEPOINT_ROLL
1d5ea 42 41 43 4b 20 26 26 20 64 62 2d 3e 61 63 74 69  BACK && db->acti
1d5eb 76 65 56 64 62 65 43 6e 74 3e 31 29 0a 20 20 20  veVdbeCnt>1).   
1d5ec 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 74 20   ){.      /* It 
1d5ed 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
1d5ee 74 6f 20 72 65 6c 65 61 73 65 20 28 63 6f 6d 6d  to release (comm
1d5ef 69 74 29 20 61 20 73 61 76 65 70 6f 69 6e 74 20  it) a savepoint 
1d5f0 69 66 20 74 68 65 72 65 20 61 72 65 0a 20 20 20  if there are.   
1d5f1 20 20 20 2a 2a 20 61 63 74 69 76 65 20 77 72 69     ** active wri
1d5f2 74 65 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49  te statements. I
1d5f3 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
1d5f4 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20  e to rollback a 
1d5f5 73 61 76 65 70 6f 69 6e 74 0a 20 20 20 20 20 20  savepoint.      
1d5f6 2a 2a 20 69 66 20 74 68 65 72 65 20 61 72 65 20  ** if there are 
1d5f7 61 6e 79 20 61 63 74 69 76 65 20 73 74 61 74 65  any active state
1d5f8 6d 65 6e 74 73 20 61 74 20 61 6c 6c 2e 0a 20 20  ments at all..  
1d5f9 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c      */.      sql
1d5fa 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70  ite3SetString(&p
1d5fb 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 0a 20  ->zErrMsg, db,. 
1d5fc 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 25         "cannot %
1d5fd 73 20 73 61 76 65 70 6f 69 6e 74 20 2d 20 53 51  s savepoint - SQ
1d5fe 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20  L statements in 
1d5ff 70 72 6f 67 72 65 73 73 22 2c 0a 20 20 20 20 20  progress",.     
1d600 20 20 20 28 75 2e 61 71 2e 70 31 3d 3d 53 41 56     (u.aq.p1==SAV
1d601 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20  EPOINT_ROLLBACK 
1d602 3f 20 22 72 6f 6c 6c 62 61 63 6b 22 3a 20 22 72  ? "rollback": "r
1d603 65 6c 65 61 73 65 22 29 0a 20 20 20 20 20 20 29  elease").      )
1d604 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ;.      rc = SQL
1d605 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65  ITE_BUSY;.    }e
1d606 6c 73 65 7b 0a 0a 20 20 20 20 20 20 2f 2a 20 44  lse{..      /* D
1d607 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72  etermine whether
1d608 20 6f 72 20 6e 6f 74 20 74 68 69 73 20 69 73 20   or not this is 
1d609 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61  a transaction sa
1d60a 76 65 70 6f 69 6e 74 2e 20 49 66 20 73 6f 2c 0a  vepoint. If so,.
1d60b 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 69        ** and thi
1d60c 73 20 69 73 20 61 20 52 45 4c 45 41 53 45 20 63  s is a RELEASE c
1d60d 6f 6d 6d 61 6e 64 2c 20 74 68 65 6e 20 74 68 65  ommand, then the
1d60e 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63   current transac
1d60f 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 69 73  tion.      ** is
1d610 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 20 20 20 20   committed..    
1d611 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69    */.      int i
1d612 73 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 75  sTransaction = u
1d613 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 2d 3e  .aq.pSavepoint->
1d614 70 4e 65 78 74 3d 3d 30 20 26 26 20 64 62 2d 3e  pNext==0 && db->
1d615 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76  isTransactionSav
1d616 65 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 69 66  epoint;.      if
1d617 28 20 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 20  ( isTransaction 
1d618 26 26 20 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45  && u.aq.p1==SAVE
1d619 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 29 7b  POINT_RELEASE ){
1d61a 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74  .        db->aut
1d61b 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20  oCommit = 1;.   
1d61c 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1d61d 56 64 62 65 48 61 6c 74 28 70 29 3d 3d 53 51 4c  VdbeHalt(p)==SQL
1d61e 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20  ITE_BUSY ){.    
1d61f 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63        p->pc = pc
1d620 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e  ;.          db->
1d621 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a  autoCommit = 0;.
1d622 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20            p->rc 
1d623 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55  = rc = SQLITE_BU
1d624 53 59 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f  SY;.          go
1d625 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a  to vdbe_return;.
1d626 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1d627 20 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74    db->isTransact
1d628 69 6f 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 30  ionSavepoint = 0
1d629 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70  ;.        rc = p
1d62a 2d 3e 72 63 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ->rc;.      }els
1d62b 65 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e  e{.        u.aq.
1d62c 69 53 61 76 65 70 6f 69 6e 74 20 3d 20 64 62 2d  iSavepoint = db-
1d62d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 2d 20 75 2e  >nSavepoint - u.
1d62e 61 71 2e 69 53 61 76 65 70 6f 69 6e 74 20 2d 20  aq.iSavepoint - 
1d62f 31 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 75  1;.        for(u
1d630 2e 61 71 2e 69 69 3d 30 3b 20 75 2e 61 71 2e 69  .aq.ii=0; u.aq.i
1d631 69 3c 64 62 2d 3e 6e 44 62 3b 20 75 2e 61 71 2e  i<db->nDb; u.aq.
1d632 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  ii++){.         
1d633 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1d634 65 65 53 61 76 65 70 6f 69 6e 74 28 64 62 2d 3e  eeSavepoint(db->
1d635 61 44 62 5b 75 2e 61 71 2e 69 69 5d 2e 70 42 74  aDb[u.aq.ii].pBt
1d636 2c 20 75 2e 61 71 2e 70 31 2c 20 75 2e 61 71 2e  , u.aq.p1, u.aq.
1d637 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20  iSavepoint);.   
1d638 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
1d639 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1d63a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f          goto abo
1d63b 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
1d63c 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1d63d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
1d63e 66 28 20 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45  f( u.aq.p1==SAVE
1d63f 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 26  POINT_ROLLBACK &
1d640 26 20 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  & (db->flags&SQL
1d641 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65  ITE_InternChange
1d642 73 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  s)!=0 ){.       
1d643 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65     sqlite3Expire
1d644 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
1d645 74 73 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20  ts(db);.        
1d646 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
1d647 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
1d648 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   0);.        }. 
1d649 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
1d64a 20 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77   Regardless of w
1d64b 68 65 74 68 65 72 20 74 68 69 73 20 69 73 20 61  hether this is a
1d64c 20 52 45 4c 45 41 53 45 20 6f 72 20 52 4f 4c 4c   RELEASE or ROLL
1d64d 42 41 43 4b 2c 20 64 65 73 74 72 6f 79 20 61 6c  BACK, destroy al
1d64e 6c 0a 20 20 20 20 20 20 2a 2a 20 73 61 76 65 70  l.      ** savep
1d64f 6f 69 6e 74 73 20 6e 65 73 74 65 64 20 69 6e 73  oints nested ins
1d650 69 64 65 20 6f 66 20 74 68 65 20 73 61 76 65 70  ide of the savep
1d651 6f 69 6e 74 20 62 65 69 6e 67 20 6f 70 65 72 61  oint being opera
1d652 74 65 64 20 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20  ted on. */.     
1d653 20 77 68 69 6c 65 28 20 64 62 2d 3e 70 53 61 76   while( db->pSav
1d654 65 70 6f 69 6e 74 21 3d 75 2e 61 71 2e 70 53 61  epoint!=u.aq.pSa
1d655 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 20  vepoint ){.     
1d656 20 20 20 75 2e 61 71 2e 70 54 6d 70 20 3d 20 64     u.aq.pTmp = d
1d657 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20  b->pSavepoint;. 
1d658 20 20 20 20 20 20 20 64 62 2d 3e 70 53 61 76 65         db->pSave
1d659 70 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70 54 6d  point = u.aq.pTm
1d65a 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  p->pNext;.      
1d65b 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1d65c 64 62 2c 20 75 2e 61 71 2e 70 54 6d 70 29 3b 0a  db, u.aq.pTmp);.
1d65d 20 20 20 20 20 20 20 20 64 62 2d 3e 6e 53 61 76          db->nSav
1d65e 65 70 6f 69 6e 74 2d 2d 3b 0a 20 20 20 20 20 20  epoint--;.      
1d65f 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 69  }..      /* If i
1d660 74 20 69 73 20 61 20 52 45 4c 45 41 53 45 2c 20  t is a RELEASE, 
1d661 74 68 65 6e 20 64 65 73 74 72 6f 79 20 74 68 65  then destroy the
1d662 20 73 61 76 65 70 6f 69 6e 74 20 62 65 69 6e 67   savepoint being
1d663 20 6f 70 65 72 61 74 65 64 20 6f 6e 20 74 6f 6f   operated on too
1d664 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 75 2e   */.      if( u.
1d665 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54  aq.p1==SAVEPOINT
1d666 5f 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 20 20  _RELEASE ){.    
1d667 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 71      assert( u.aq
1d668 2e 70 53 61 76 65 70 6f 69 6e 74 3d 3d 64 62 2d  .pSavepoint==db-
1d669 3e 70 53 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20  >pSavepoint );. 
1d66a 20 20 20 20 20 20 20 64 62 2d 3e 70 53 61 76 65         db->pSave
1d66b 70 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70 53 61  point = u.aq.pSa
1d66c 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 3b 0a  vepoint->pNext;.
1d66d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
1d66e 62 46 72 65 65 28 64 62 2c 20 75 2e 61 71 2e 70  bFree(db, u.aq.p
1d66f 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20  Savepoint);.    
1d670 20 20 20 20 69 66 28 20 21 69 73 54 72 61 6e 73      if( !isTrans
1d671 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20  action ){.      
1d672 20 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69      db->nSavepoi
1d673 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a  nt--;.        }.
1d674 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1d675 7d 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  }..  break;.}../
1d676 2a 20 4f 70 63 6f 64 65 3a 20 41 75 74 6f 43 6f  * Opcode: AutoCo
1d677 6d 6d 69 74 20 50 31 20 50 32 20 2a 20 2a 20 2a  mmit P1 P2 * * *
1d678 0a 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 64  .**.** Set the d
1d679 61 74 61 62 61 73 65 20 61 75 74 6f 2d 63 6f 6d  atabase auto-com
1d67a 6d 69 74 20 66 6c 61 67 20 74 6f 20 50 31 20 28  mit flag to P1 (
1d67b 31 20 6f 72 20 30 29 2e 20 49 66 20 50 32 20 69  1 or 0). If P2 i
1d67c 73 20 74 72 75 65 2c 20 72 6f 6c 6c 0a 2a 2a 20  s true, roll.** 
1d67d 62 61 63 6b 20 61 6e 79 20 63 75 72 72 65 6e 74  back any current
1d67e 6c 79 20 61 63 74 69 76 65 20 62 74 72 65 65 20  ly active btree 
1d67f 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 20 49 66  transactions. If
1d680 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 61   there are any a
1d681 63 74 69 76 65 0a 2a 2a 20 56 4d 73 20 28 61 70  ctive.** VMs (ap
1d682 61 72 74 20 66 72 6f 6d 20 74 68 69 73 20 6f 6e  art from this on
1d683 65 29 2c 20 74 68 65 6e 20 61 20 52 4f 4c 4c 42  e), then a ROLLB
1d684 41 43 4b 20 66 61 69 6c 73 2e 20 20 41 20 43 4f  ACK fails.  A CO
1d685 4d 4d 49 54 20 66 61 69 6c 73 20 69 66 0a 2a 2a  MMIT fails if.**
1d686 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76   there are activ
1d687 65 20 77 72 69 74 69 6e 67 20 56 4d 73 20 6f 72  e writing VMs or
1d688 20 61 63 74 69 76 65 20 56 4d 73 20 74 68 61 74   active VMs that
1d689 20 75 73 65 20 73 68 61 72 65 64 20 63 61 63 68   use shared cach
1d68a 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  e..**.** This in
1d68b 73 74 72 75 63 74 69 6f 6e 20 63 61 75 73 65 73  struction causes
1d68c 20 74 68 65 20 56 4d 20 74 6f 20 68 61 6c 74 2e   the VM to halt.
1d68d 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 75 74 6f  .*/.case OP_Auto
1d68e 43 6f 6d 6d 69 74 3a 20 7b 0a 23 69 66 20 30 20  Commit: {.#if 0 
1d68f 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1d690 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1d691 2e 61 72 20 2a 2f 0a 20 20 69 6e 74 20 64 65 73  .ar */.  int des
1d692 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3b 0a  iredAutoCommit;.
1d693 20 20 69 6e 74 20 69 52 6f 6c 6c 62 61 63 6b 3b    int iRollback;
1d694 0a 20 20 69 6e 74 20 74 75 72 6e 4f 6e 41 43 3b  .  int turnOnAC;
1d695 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1d696 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1d697 20 69 6e 74 6f 20 75 2e 61 72 20 2a 2f 0a 0a 20   into u.ar */.. 
1d698 20 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74   u.ar.desiredAut
1d699 6f 43 6f 6d 6d 69 74 20 3d 20 70 4f 70 2d 3e 70  oCommit = pOp->p
1d69a 31 3b 0a 20 20 75 2e 61 72 2e 69 52 6f 6c 6c 62  1;.  u.ar.iRollb
1d69b 61 63 6b 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20  ack = pOp->p2;. 
1d69c 20 75 2e 61 72 2e 74 75 72 6e 4f 6e 41 43 20 3d   u.ar.turnOnAC =
1d69d 20 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74   u.ar.desiredAut
1d69e 6f 43 6f 6d 6d 69 74 20 26 26 20 21 64 62 2d 3e  oCommit && !db->
1d69f 61 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 61 73  autoCommit;.  as
1d6a0 73 65 72 74 28 20 75 2e 61 72 2e 64 65 73 69 72  sert( u.ar.desir
1d6a1 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20  edAutoCommit==1 
1d6a2 7c 7c 20 75 2e 61 72 2e 64 65 73 69 72 65 64 41  || u.ar.desiredA
1d6a3 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 3b 0a  utoCommit==0 );.
1d6a4 20 20 61 73 73 65 72 74 28 20 75 2e 61 72 2e 64    assert( u.ar.d
1d6a5 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74  esiredAutoCommit
1d6a6 3d 3d 31 20 7c 7c 20 75 2e 61 72 2e 69 52 6f 6c  ==1 || u.ar.iRol
1d6a7 6c 62 61 63 6b 3d 3d 30 20 29 3b 0a 20 20 61 73  lback==0 );.  as
1d6a8 73 65 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65  sert( db->active
1d6a9 56 64 62 65 43 6e 74 3e 30 20 29 3b 20 20 2f 2a  VdbeCnt>0 );  /*
1d6aa 20 41 74 20 6c 65 61 73 74 20 74 68 69 73 20 6f   At least this o
1d6ab 6e 65 20 56 4d 20 69 73 20 61 63 74 69 76 65 20  ne VM is active 
1d6ac 2a 2f 0a 0a 20 20 69 66 28 20 75 2e 61 72 2e 74  */..  if( u.ar.t
1d6ad 75 72 6e 4f 6e 41 43 20 26 26 20 75 2e 61 72 2e  urnOnAC && u.ar.
1d6ae 69 52 6f 6c 6c 62 61 63 6b 20 26 26 20 64 62 2d  iRollback && db-
1d6af 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e 31  >activeVdbeCnt>1
1d6b0 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   ){.    /* If th
1d6b1 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69  is instruction i
1d6b2 6d 70 6c 65 6d 65 6e 74 73 20 61 20 52 4f 4c 4c  mplements a ROLL
1d6b3 42 41 43 4b 20 61 6e 64 20 6f 74 68 65 72 20 56  BACK and other V
1d6b4 4d 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 73 74  Ms are.    ** st
1d6b5 69 6c 6c 20 72 75 6e 6e 69 6e 67 2c 20 61 6e 64  ill running, and
1d6b6 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
1d6b7 73 20 61 63 74 69 76 65 2c 20 72 65 74 75 72 6e  s active, return
1d6b8 20 61 6e 20 65 72 72 6f 72 20 69 6e 64 69 63 61   an error indica
1d6b9 74 69 6e 67 0a 20 20 20 20 2a 2a 20 74 68 61 74  ting.    ** that
1d6ba 20 74 68 65 20 6f 74 68 65 72 20 56 4d 73 20 6d   the other VMs m
1d6bb 75 73 74 20 63 6f 6d 70 6c 65 74 65 20 66 69 72  ust complete fir
1d6bc 73 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  st..    */.    s
1d6bd 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
1d6be 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c  &p->zErrMsg, db,
1d6bf 20 22 63 61 6e 6e 6f 74 20 72 6f 6c 6c 62 61 63   "cannot rollbac
1d6c0 6b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20  k transaction - 
1d6c1 22 0a 20 20 20 20 20 20 20 20 22 53 51 4c 20 73  ".        "SQL s
1d6c2 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f  tatements in pro
1d6c3 67 72 65 73 73 22 29 3b 0a 20 20 20 20 72 63 20  gress");.    rc 
1d6c4 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  = SQLITE_BUSY;. 
1d6c5 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 72 2e   }else if( u.ar.
1d6c6 74 75 72 6e 4f 6e 41 43 20 26 26 20 21 75 2e 61  turnOnAC && !u.a
1d6c7 72 2e 69 52 6f 6c 6c 62 61 63 6b 20 26 26 20 64  r.iRollback && d
1d6c8 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 3e  b->writeVdbeCnt>
1d6c9 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74  0 ){.    /* If t
1d6ca 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1d6cb 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 43 4f 4d  implements a COM
1d6cc 4d 49 54 20 61 6e 64 20 6f 74 68 65 72 20 56 4d  MIT and other VM
1d6cd 73 20 61 72 65 20 77 72 69 74 69 6e 67 0a 20 20  s are writing.  
1d6ce 20 20 2a 2a 20 72 65 74 75 72 6e 20 61 6e 20 65    ** return an e
1d6cf 72 72 6f 72 20 69 6e 64 69 63 61 74 69 6e 67 20  rror indicating 
1d6d0 74 68 61 74 20 74 68 65 20 6f 74 68 65 72 20 56  that the other V
1d6d1 4d 73 20 6d 75 73 74 20 63 6f 6d 70 6c 65 74 65  Ms must complete
1d6d2 20 66 69 72 73 74 2e 0a 20 20 20 20 2a 2f 0a 20   first..    */. 
1d6d3 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
1d6d4 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  ing(&p->zErrMsg,
1d6d5 20 64 62 2c 20 22 63 61 6e 6e 6f 74 20 63 6f 6d   db, "cannot com
1d6d6 6d 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  mit transaction 
1d6d7 2d 20 22 0a 20 20 20 20 20 20 20 20 22 53 51 4c  - ".        "SQL
1d6d8 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 70   statements in p
1d6d9 72 6f 67 72 65 73 73 22 29 3b 0a 20 20 20 20 72  rogress");.    r
1d6da 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  c = SQLITE_BUSY;
1d6db 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61  .  }else if( u.a
1d6dc 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d  r.desiredAutoCom
1d6dd 6d 69 74 21 3d 64 62 2d 3e 61 75 74 6f 43 6f 6d  mit!=db->autoCom
1d6de 6d 69 74 20 29 7b 0a 20 20 20 20 69 66 28 20 75  mit ){.    if( u
1d6df 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 6b 20 29 7b  .ar.iRollback ){
1d6e0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75  .      assert( u
1d6e1 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43  .ar.desiredAutoC
1d6e2 6f 6d 6d 69 74 3d 3d 31 20 29 3b 0a 20 20 20 20  ommit==1 );.    
1d6e3 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63    sqlite3Rollbac
1d6e4 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20  kAll(db);.      
1d6e5 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d  db->autoCommit =
1d6e6 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   1;.    }else{. 
1d6e7 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d       db->autoCom
1d6e8 6d 69 74 20 3d 20 28 75 38 29 75 2e 61 72 2e 64  mit = (u8)u.ar.d
1d6e9 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74  esiredAutoCommit
1d6ea 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  ;.      if( sqli
1d6eb 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3d 3d  te3VdbeHalt(p)==
1d6ec 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20  SQLITE_BUSY ){. 
1d6ed 20 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70         p->pc = p
1d6ee 63 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61  c;.        db->a
1d6ef 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 28 75 38 29  utoCommit = (u8)
1d6f0 28 31 2d 75 2e 61 72 2e 64 65 73 69 72 65 64 41  (1-u.ar.desiredA
1d6f1 75 74 6f 43 6f 6d 6d 69 74 29 3b 0a 20 20 20 20  utoCommit);.    
1d6f2 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d      p->rc = rc =
1d6f3 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
1d6f4 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f        goto vdbe_
1d6f5 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
1d6f6 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
1d6f7 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74  ( db->nStatement
1d6f8 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ==0 );.    sqlit
1d6f9 65 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74  e3CloseSavepoint
1d6fa 73 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 70  s(db);.    if( p
1d6fb 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ->rc==SQLITE_OK 
1d6fc 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
1d6fd 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d  LITE_DONE;.    }
1d6fe 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
1d6ff 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1d700 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 76 64     }.    goto vd
1d701 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c  be_return;.  }el
1d702 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53  se{.    sqlite3S
1d703 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72  etString(&p->zEr
1d704 72 4d 73 67 2c 20 64 62 2c 0a 20 20 20 20 20 20  rMsg, db,.      
1d705 20 20 28 21 75 2e 61 72 2e 64 65 73 69 72 65 64    (!u.ar.desired
1d706 41 75 74 6f 43 6f 6d 6d 69 74 29 3f 22 63 61 6e  AutoCommit)?"can
1d707 6e 6f 74 20 73 74 61 72 74 20 61 20 74 72 61 6e  not start a tran
1d708 73 61 63 74 69 6f 6e 20 77 69 74 68 69 6e 20 61  saction within a
1d709 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 3a 28 0a   transaction":(.
1d70a 20 20 20 20 20 20 20 20 28 75 2e 61 72 2e 69 52          (u.ar.iR
1d70b 6f 6c 6c 62 61 63 6b 29 3f 22 63 61 6e 6e 6f 74  ollback)?"cannot
1d70c 20 72 6f 6c 6c 62 61 63 6b 20 2d 20 6e 6f 20 74   rollback - no t
1d70d 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63  ransaction is ac
1d70e 74 69 76 65 22 3a 0a 20 20 20 20 20 20 20 20 20  tive":.         
1d70f 20 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f            "canno
1d710 74 20 63 6f 6d 6d 69 74 20 2d 20 6e 6f 20 74 72  t commit - no tr
1d711 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74  ansaction is act
1d712 69 76 65 22 29 29 3b 0a 0a 20 20 20 20 72 63 20  ive"));..    rc 
1d713 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
1d714 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1d715 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 72 61 6e 73  /* Opcode: Trans
1d716 61 63 74 69 6f 6e 20 50 31 20 50 32 20 2a 20 2a  action P1 P2 * *
1d717 20 2a 0a 2a 2a 0a 2a 2a 20 42 65 67 69 6e 20 61   *.**.** Begin a
1d718 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54   transaction.  T
1d719 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 65  he transaction e
1d71a 6e 64 73 20 77 68 65 6e 20 61 20 43 6f 6d 6d 69  nds when a Commi
1d71b 74 20 6f 72 20 52 6f 6c 6c 62 61 63 6b 0a 2a 2a  t or Rollback.**
1d71c 20 6f 70 63 6f 64 65 20 69 73 20 65 6e 63 6f 75   opcode is encou
1d71d 6e 74 65 72 65 64 2e 20 20 44 65 70 65 6e 64 69  ntered.  Dependi
1d71e 6e 67 20 6f 6e 20 74 68 65 20 4f 4e 20 43 4f 4e  ng on the ON CON
1d71f 46 4c 49 43 54 20 73 65 74 74 69 6e 67 2c 20 74  FLICT setting, t
1d720 68 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  he.** transactio
1d721 6e 20 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20  n might also be 
1d722 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69 66 20 61  rolled back if a
1d723 6e 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75  n error is encou
1d724 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 31  ntered..**.** P1
1d725 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66   is the index of
1d726 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1d727 6c 65 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20  le on which the 
1d728 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a  transaction is.*
1d729 2a 20 73 74 61 72 74 65 64 2e 20 20 49 6e 64 65  * started.  Inde
1d72a 78 20 30 20 69 73 20 74 68 65 20 6d 61 69 6e 20  x 0 is the main 
1d72b 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e  database file an
1d72c 64 20 69 6e 64 65 78 20 31 20 69 73 20 74 68 65  d index 1 is the
1d72d 0a 2a 2a 20 66 69 6c 65 20 75 73 65 64 20 66 6f  .** file used fo
1d72e 72 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  r temporary tabl
1d72f 65 73 2e 20 20 49 6e 64 69 63 65 73 20 6f 66 20  es.  Indices of 
1d730 32 20 6f 72 20 6d 6f 72 65 20 61 72 65 20 75 73  2 or more are us
1d731 65 64 20 66 6f 72 0a 2a 2a 20 61 74 74 61 63 68  ed for.** attach
1d732 65 64 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a  ed databases..**
1d733 0a 2a 2a 20 49 66 20 50 32 20 69 73 20 6e 6f 6e  .** If P2 is non
1d734 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 77 72  -zero, then a wr
1d735 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20  ite-transaction 
1d736 69 73 20 73 74 61 72 74 65 64 2e 20 20 41 20 52  is started.  A R
1d737 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 0a  ESERVED lock is.
1d738 2a 2a 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74  ** obtained on t
1d739 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1d73a 20 77 68 65 6e 20 61 20 77 72 69 74 65 2d 74 72   when a write-tr
1d73b 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61  ansaction is sta
1d73c 72 74 65 64 2e 20 20 4e 6f 0a 2a 2a 20 6f 74 68  rted.  No.** oth
1d73d 65 72 20 70 72 6f 63 65 73 73 20 63 61 6e 20 73  er process can s
1d73e 74 61 72 74 20 61 6e 6f 74 68 65 72 20 77 72 69  tart another wri
1d73f 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  te transaction w
1d740 68 69 6c 65 20 74 68 69 73 20 74 72 61 6e 73 61  hile this transa
1d741 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 6e 64 65  ction is.** unde
1d742 72 77 61 79 2e 20 20 53 74 61 72 74 69 6e 67 20  rway.  Starting 
1d743 61 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74  a write transact
1d744 69 6f 6e 20 61 6c 73 6f 20 63 72 65 61 74 65 73  ion also creates
1d745 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72   a rollback jour
1d746 6e 61 6c 2e 20 41 0a 2a 2a 20 77 72 69 74 65 20  nal. A.** write 
1d747 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74  transaction must
1d748 20 62 65 20 73 74 61 72 74 65 64 20 62 65 66 6f   be started befo
1d749 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 63  re any changes c
1d74a 61 6e 20 62 65 20 6d 61 64 65 20 74 6f 20 74 68  an be made to th
1d74b 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20  e.** database.  
1d74c 49 66 20 50 32 20 69 73 20 32 20 6f 72 20 67 72  If P2 is 2 or gr
1d74d 65 61 74 65 72 20 74 68 65 6e 20 61 6e 20 45 58  eater then an EX
1d74e 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 69 73 20  CLUSIVE lock is 
1d74f 61 6c 73 6f 20 6f 62 74 61 69 6e 65 64 0a 2a 2a  also obtained.**
1d750 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a   on the file..**
1d751 0a 2a 2a 20 49 66 20 61 20 77 72 69 74 65 2d 74  .** If a write-t
1d752 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74  ransaction is st
1d753 61 72 74 65 64 20 61 6e 64 20 74 68 65 20 56 64  arted and the Vd
1d754 62 65 2e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e  be.usesStmtJourn
1d755 61 6c 20 66 6c 61 67 20 69 73 0a 2a 2a 20 74 72  al flag is.** tr
1d756 75 65 20 28 74 68 69 73 20 66 6c 61 67 20 69 73  ue (this flag is
1d757 20 73 65 74 20 69 66 20 74 68 65 20 56 64 62 65   set if the Vdbe
1d758 20 6d 61 79 20 6d 6f 64 69 66 79 20 6d 6f 72 65   may modify more
1d759 20 74 68 61 6e 20 6f 6e 65 20 72 6f 77 20 61 6e   than one row an
1d75a 64 20 6d 61 79 0a 2a 2a 20 74 68 72 6f 77 20 61  d may.** throw a
1d75b 6e 20 41 42 4f 52 54 20 65 78 63 65 70 74 69 6f  n ABORT exceptio
1d75c 6e 29 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20  n), a statement 
1d75d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20  transaction may 
1d75e 61 6c 73 6f 20 62 65 20 6f 70 65 6e 65 64 2e 0a  also be opened..
1d75f 2a 2a 20 4d 6f 72 65 20 73 70 65 63 69 66 69 63  ** More specific
1d760 61 6c 6c 79 2c 20 61 20 73 74 61 74 65 6d 65 6e  ally, a statemen
1d761 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  t transaction is
1d762 20 6f 70 65 6e 65 64 20 69 66 66 20 74 68 65 20   opened iff the 
1d763 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e  database.** conn
1d764 65 63 74 69 6f 6e 20 69 73 20 63 75 72 72 65 6e  ection is curren
1d765 74 6c 79 20 6e 6f 74 20 69 6e 20 61 75 74 6f 63  tly not in autoc
1d766 6f 6d 6d 69 74 20 6d 6f 64 65 2c 20 6f 72 20 69  ommit mode, or i
1d767 66 20 74 68 65 72 65 20 61 72 65 20 6f 74 68 65  f there are othe
1d768 72 0a 2a 2a 20 61 63 74 69 76 65 20 73 74 61 74  r.** active stat
1d769 65 6d 65 6e 74 73 2e 20 41 20 73 74 61 74 65 6d  ements. A statem
1d76a 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent transaction 
1d76b 61 6c 6c 6f 77 73 20 74 68 65 20 61 66 66 65 63  allows the affec
1d76c 74 73 20 6f 66 20 74 68 69 73 0a 2a 2a 20 56 44  ts of this.** VD
1d76d 42 45 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20  BE to be rolled 
1d76e 62 61 63 6b 20 61 66 74 65 72 20 61 6e 20 65 72  back after an er
1d76f 72 6f 72 20 77 69 74 68 6f 75 74 20 68 61 76 69  ror without havi
1d770 6e 67 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20  ng to roll back 
1d771 74 68 65 0a 2a 2a 20 65 6e 74 69 72 65 20 74 72  the.** entire tr
1d772 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 6e 6f  ansaction. If no
1d773 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e   error is encoun
1d774 74 65 72 65 64 2c 20 74 68 65 20 73 74 61 74 65  tered, the state
1d775 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
1d776 0a 2a 2a 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74  .** will automat
1d777 69 63 61 6c 6c 79 20 63 6f 6d 6d 69 74 20 77 68  ically commit wh
1d778 65 6e 20 74 68 65 20 56 44 42 45 20 68 61 6c 74  en the VDBE halt
1d779 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 20 69  s..**.** If P2 i
1d77a 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 72  s zero, then a r
1d77b 65 61 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61  ead-lock is obta
1d77c 69 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61  ined on the data
1d77d 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 63 61  base file..*/.ca
1d77e 73 65 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f  se OP_Transactio
1d77f 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c  n: {.#if 0  /* l
1d780 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1d781 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 73 20 2a  oved into u.as *
1d782 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a  /.  Btree *pBt;.
1d783 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1d784 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1d785 69 6e 74 6f 20 75 2e 61 73 20 2a 2f 0a 0a 20 20  into u.as */..  
1d786 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1d787 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62  =0 && pOp->p1<db
1d788 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72  ->nDb );.  asser
1d789 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b  t( (p->btreeMask
1d78a 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29   & (1<<pOp->p1))
1d78b 21 3d 30 20 29 3b 0a 20 20 75 2e 61 73 2e 70 42  !=0 );.  u.as.pB
1d78c 74 20 3d 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d  t = db->aDb[pOp-
1d78d 3e 70 31 5d 2e 70 42 74 3b 0a 0a 20 20 69 66 28  >p1].pBt;..  if(
1d78e 20 75 2e 61 73 2e 70 42 74 20 29 7b 0a 20 20 20   u.as.pBt ){.   
1d78f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1d790 65 65 42 65 67 69 6e 54 72 61 6e 73 28 75 2e 61  eeBeginTrans(u.a
1d791 73 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 32 29 3b  s.pBt, pOp->p2);
1d792 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
1d793 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20  ITE_BUSY ){.    
1d794 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20    p->pc = pc;.  
1d795 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d      p->rc = rc =
1d796 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
1d797 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65      goto vdbe_re
1d798 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20  turn;.    }.    
1d799 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1d79a 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f  K && rc!=SQLITE_
1d79b 52 45 41 44 4f 4e 4c 59 20 2f 2a 20 26 26 20 72  READONLY /* && r
1d79c 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 2a  c!=SQLITE_BUSY *
1d79d 2f 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  / ){.      goto 
1d79e 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
1d79f 6f 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  or;.    }..    i
1d7a0 66 28 20 70 4f 70 2d 3e 70 32 20 26 26 20 70 2d  f( pOp->p2 && p-
1d7a1 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c  >usesStmtJournal
1d7a2 0a 20 20 20 20 20 26 26 20 28 64 62 2d 3e 61 75  .     && (db->au
1d7a3 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64  toCommit==0 || d
1d7a4 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74  b->activeVdbeCnt
1d7a5 3e 31 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  >1).    ){.     
1d7a6 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1d7a7 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 75  BtreeIsInTrans(u
1d7a8 2e 61 73 2e 70 42 74 29 20 29 3b 0a 20 20 20 20  .as.pBt) );.    
1d7a9 20 20 69 66 28 20 70 2d 3e 69 53 74 61 74 65 6d    if( p->iStatem
1d7aa 65 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ent==0 ){.      
1d7ab 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 53    assert( db->nS
1d7ac 74 61 74 65 6d 65 6e 74 3e 3d 30 20 26 26 20 64  tatement>=0 && d
1d7ad 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3e 3d 30  b->nSavepoint>=0
1d7ae 20 29 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e   );.        db->
1d7af 6e 53 74 61 74 65 6d 65 6e 74 2b 2b 3b 0a 20 20  nStatement++;.  
1d7b0 20 20 20 20 20 20 70 2d 3e 69 53 74 61 74 65 6d        p->iStatem
1d7b1 65 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76 65 70  ent = db->nSavep
1d7b2 6f 69 6e 74 20 2b 20 64 62 2d 3e 6e 53 74 61 74  oint + db->nStat
1d7b3 65 6d 65 6e 74 3b 0a 20 20 20 20 20 20 7d 0a 20  ement;.      }. 
1d7b4 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1d7b5 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28  3BtreeBeginStmt(
1d7b6 75 2e 61 73 2e 70 42 74 2c 20 70 2d 3e 69 53 74  u.as.pBt, p->iSt
1d7b7 61 74 65 6d 65 6e 74 29 3b 0a 20 20 20 20 7d 0a  atement);.    }.
1d7b8 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1d7b9 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61 64 43  /* Opcode: ReadC
1d7ba 6f 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20 2a  ookie P1 P2 P3 *
1d7bb 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 63 6f   *.**.** Read co
1d7bc 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 33 20 66  okie number P3 f
1d7bd 72 6f 6d 20 64 61 74 61 62 61 73 65 20 50 31 20  rom database P1 
1d7be 61 6e 64 20 77 72 69 74 65 20 69 74 20 69 6e 74  and write it int
1d7bf 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a  o register P2..*
1d7c0 2a 20 50 33 3d 3d 31 20 69 73 20 74 68 65 20 73  * P3==1 is the s
1d7c1 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20  chema version.  
1d7c2 50 33 3d 3d 32 20 69 73 20 74 68 65 20 64 61 74  P3==2 is the dat
1d7c3 61 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a  abase format..**
1d7c4 20 50 33 3d 3d 33 20 69 73 20 74 68 65 20 72 65   P3==3 is the re
1d7c5 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20  commended pager 
1d7c6 63 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64 20  cache size, and 
1d7c7 73 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d 30  so forth.  P1==0
1d7c8 20 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20   is.** the main 
1d7c9 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e  database file an
1d7ca 64 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 64  d P1==1 is the d
1d7cb 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 65  atabase file use
1d7cc 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 65  d to store.** te
1d7cd 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a  mporary tables..
1d7ce 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74  **.** There must
1d7cf 20 62 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20   be a read-lock 
1d7d0 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
1d7d1 28 65 69 74 68 65 72 20 61 20 74 72 61 6e 73 61  (either a transa
1d7d2 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 65  ction.** must be
1d7d3 20 73 74 61 72 74 65 64 20 6f 72 20 74 68 65 72   started or ther
1d7d4 65 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65  e must be an ope
1d7d5 6e 20 63 75 72 73 6f 72 29 20 62 65 66 6f 72 65  n cursor) before
1d7d6 0a 2a 2a 20 65 78 65 63 75 74 69 6e 67 20 74 68  .** executing th
1d7d7 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  is instruction..
1d7d8 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 64 43  */.case OP_ReadC
1d7d9 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 20 20  ookie: {        
1d7da 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70         /* out2-p
1d7db 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66  rerelease */.#if
1d7dc 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1d7dd 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1d7de 6f 20 75 2e 61 74 20 2a 2f 0a 20 20 69 6e 74 20  o u.at */.  int 
1d7df 69 4d 65 74 61 3b 0a 20 20 69 6e 74 20 69 44 62  iMeta;.  int iDb
1d7e0 3b 0a 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 3b  ;.  int iCookie;
1d7e1 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1d7e2 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1d7e3 20 69 6e 74 6f 20 75 2e 61 74 20 2a 2f 0a 0a 20   into u.at */.. 
1d7e4 20 75 2e 61 74 2e 69 44 62 20 3d 20 70 4f 70 2d   u.at.iDb = pOp-
1d7e5 3e 70 31 3b 0a 20 20 75 2e 61 74 2e 69 43 6f 6f  >p1;.  u.at.iCoo
1d7e6 6b 69 65 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20  kie = pOp->p3;. 
1d7e7 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33   assert( pOp->p3
1d7e8 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 5f  <SQLITE_N_BTREE_
1d7e9 4d 45 54 41 20 29 3b 0a 20 20 61 73 73 65 72 74  META );.  assert
1d7ea 28 20 75 2e 61 74 2e 69 44 62 3e 3d 30 20 26 26  ( u.at.iDb>=0 &&
1d7eb 20 75 2e 61 74 2e 69 44 62 3c 64 62 2d 3e 6e 44   u.at.iDb<db->nD
1d7ec 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64  b );.  assert( d
1d7ed 62 2d 3e 61 44 62 5b 75 2e 61 74 2e 69 44 62 5d  b->aDb[u.at.iDb]
1d7ee 2e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73  .pBt!=0 );.  ass
1d7ef 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61  ert( (p->btreeMa
1d7f0 73 6b 20 26 20 28 31 3c 3c 75 2e 61 74 2e 69 44  sk & (1<<u.at.iD
1d7f1 62 29 29 21 3d 30 20 29 3b 0a 0a 20 20 73 71 6c  b))!=0 );..  sql
1d7f2 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61  ite3BtreeGetMeta
1d7f3 28 64 62 2d 3e 61 44 62 5b 75 2e 61 74 2e 69 44  (db->aDb[u.at.iD
1d7f4 62 5d 2e 70 42 74 2c 20 75 2e 61 74 2e 69 43 6f  b].pBt, u.at.iCo
1d7f5 6f 6b 69 65 2c 20 28 75 33 32 20 2a 29 26 75 2e  okie, (u32 *)&u.
1d7f6 61 74 2e 69 4d 65 74 61 29 3b 0a 20 20 70 4f 75  at.iMeta);.  pOu
1d7f7 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 74 2e 69 4d  t->u.i = u.at.iM
1d7f8 65 74 61 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70  eta;.  MemSetTyp
1d7f9 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f  eFlag(pOut, MEM_
1d7fa 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Int);.  break;.}
1d7fb 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 74  ../* Opcode: Set
1d7fc 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20  Cookie P1 P2 P3 
1d7fd 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20  * *.**.** Write 
1d7fe 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72  the content of r
1d7ff 65 67 69 73 74 65 72 20 50 33 20 28 69 6e 74 65  egister P3 (inte
1d800 72 70 72 65 74 65 64 20 61 73 20 61 6e 20 69 6e  rpreted as an in
1d801 74 65 67 65 72 29 0a 2a 2a 20 69 6e 74 6f 20 63  teger).** into c
1d802 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 32 20  ookie number P2 
1d803 6f 66 20 64 61 74 61 62 61 73 65 20 50 31 2e 20  of database P1. 
1d804 20 50 32 3d 3d 31 20 69 73 20 74 68 65 20 73 63   P2==1 is the sc
1d805 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 0a  hema version.  .
1d806 2a 2a 20 50 32 3d 3d 32 20 69 73 20 74 68 65 20  ** P2==2 is the 
1d807 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e  database format.
1d808 20 50 32 3d 3d 33 20 69 73 20 74 68 65 20 72 65   P2==3 is the re
1d809 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20  commended pager 
1d80a 63 61 63 68 65 20 0a 2a 2a 20 73 69 7a 65 2c 20  cache .** size, 
1d80b 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 50  and so forth.  P
1d80c 31 3d 3d 30 20 69 73 20 74 68 65 20 6d 61 69 6e  1==0 is the main
1d80d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
1d80e 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20  nd P1==1 is the 
1d80f 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c  .** database fil
1d810 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  e used to store 
1d811 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
1d812 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61  ..**.** A transa
1d813 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74  ction must be st
1d814 61 72 74 65 64 20 62 65 66 6f 72 65 20 65 78 65  arted before exe
1d815 63 75 74 69 6e 67 20 74 68 69 73 20 6f 70 63 6f  cuting this opco
1d816 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  de..*/.case OP_S
1d817 65 74 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20  etCookie: {     
1d818 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a 23 69 66 20    /* in3 */.#if 
1d819 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1d81a 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1d81b 20 75 2e 61 75 20 2a 2f 0a 20 20 44 62 20 2a 70   u.au */.  Db *p
1d81c 44 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f  Db;.#endif /* lo
1d81d 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1d81e 76 65 64 20 69 6e 74 6f 20 75 2e 61 75 20 2a 2f  ved into u.au */
1d81f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1d820 70 32 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45  p2<SQLITE_N_BTRE
1d821 45 5f 4d 45 54 41 20 29 3b 0a 20 20 61 73 73 65  E_META );.  asse
1d822 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1d823 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44  & pOp->p1<db->nD
1d824 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  b );.  assert( (
1d825 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28  p->btreeMask & (
1d826 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20  1<<pOp->p1))!=0 
1d827 29 3b 0a 20 20 75 2e 61 75 2e 70 44 62 20 3d 20  );.  u.au.pDb = 
1d828 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31  &db->aDb[pOp->p1
1d829 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61  ];.  assert( u.a
1d82a 75 2e 70 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b  u.pDb->pBt!=0 );
1d82b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1d82c 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 33  mIntegerify(pIn3
1d82d 29 3b 0a 20 20 2f 2a 20 53 65 65 20 6e 6f 74 65  );.  /* See note
1d82e 20 61 62 6f 75 74 20 69 6e 64 65 78 20 73 68 69   about index shi
1d82f 66 74 69 6e 67 20 6f 6e 20 4f 50 5f 52 65 61 64  fting on OP_Read
1d830 43 6f 6f 6b 69 65 20 2a 2f 0a 20 20 72 63 20 3d  Cookie */.  rc =
1d831 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64   sqlite3BtreeUpd
1d832 61 74 65 4d 65 74 61 28 75 2e 61 75 2e 70 44 62  ateMeta(u.au.pDb
1d833 2d 3e 70 42 74 2c 20 70 4f 70 2d 3e 70 32 2c 20  ->pBt, pOp->p2, 
1d834 28 69 6e 74 29 70 49 6e 33 2d 3e 75 2e 69 29 3b  (int)pIn3->u.i);
1d835 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d  .  if( pOp->p2==
1d836 42 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 45 52  BTREE_SCHEMA_VER
1d837 53 49 4f 4e 20 29 7b 0a 20 20 20 20 2f 2a 20 57  SION ){.    /* W
1d838 68 65 6e 20 74 68 65 20 73 63 68 65 6d 61 20 63  hen the schema c
1d839 6f 6f 6b 69 65 20 63 68 61 6e 67 65 73 2c 20 72  ookie changes, r
1d83a 65 63 6f 72 64 20 74 68 65 20 6e 65 77 20 63 6f  ecord the new co
1d83b 6f 6b 69 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20  okie internally 
1d83c 2a 2f 0a 20 20 20 20 75 2e 61 75 2e 70 44 62 2d  */.    u.au.pDb-
1d83d 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61  >pSchema->schema
1d83e 5f 63 6f 6f 6b 69 65 20 3d 20 28 69 6e 74 29 70  _cookie = (int)p
1d83f 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20 20 64 62  In3->u.i;.    db
1d840 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54  ->flags |= SQLIT
1d841 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b  E_InternChanges;
1d842 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70  .  }else if( pOp
1d843 2d 3e 70 32 3d 3d 42 54 52 45 45 5f 46 49 4c 45  ->p2==BTREE_FILE
1d844 5f 46 4f 52 4d 41 54 20 29 7b 0a 20 20 20 20 2f  _FORMAT ){.    /
1d845 2a 20 52 65 63 6f 72 64 20 63 68 61 6e 67 65 73  * Record changes
1d846 20 69 6e 20 74 68 65 20 66 69 6c 65 20 66 6f 72   in the file for
1d847 6d 61 74 20 2a 2f 0a 20 20 20 20 75 2e 61 75 2e  mat */.    u.au.
1d848 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69  pDb->pSchema->fi
1d849 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 28 75 38 29  le_format = (u8)
1d84a 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20  pIn3->u.i;.  }. 
1d84b 20 69 66 28 20 70 4f 70 2d 3e 70 31 3d 3d 31 20   if( pOp->p1==1 
1d84c 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 76 61 6c 69  ){.    /* Invali
1d84d 64 61 74 65 20 61 6c 6c 20 70 72 65 70 61 72 65  date all prepare
1d84e 64 20 73 74 61 74 65 6d 65 6e 74 73 20 77 68 65  d statements whe
1d84f 6e 65 76 65 72 20 74 68 65 20 54 45 4d 50 20 64  never the TEMP d
1d850 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 73  atabase.    ** s
1d851 63 68 65 6d 61 20 69 73 20 63 68 61 6e 67 65 64  chema is changed
1d852 2e 20 20 54 69 63 6b 65 74 20 23 31 36 34 34 20  .  Ticket #1644 
1d853 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  */.    sqlite3Ex
1d854 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74  pirePreparedStat
1d855 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 7d 0a  ements(db);.  }.
1d856 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1d857 70 63 6f 64 65 3a 20 56 65 72 69 66 79 43 6f 6f  pcode: VerifyCoo
1d858 6b 69 65 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a  kie P1 P2 *.**.*
1d859 2a 20 43 68 65 63 6b 20 74 68 65 20 76 61 6c 75  * Check the valu
1d85a 65 20 6f 66 20 67 6c 6f 62 61 6c 20 64 61 74 61  e of global data
1d85b 62 61 73 65 20 70 61 72 61 6d 65 74 65 72 20 6e  base parameter n
1d85c 75 6d 62 65 72 20 30 20 28 74 68 65 0a 2a 2a 20  umber 0 (the.** 
1d85d 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 29 20  schema version) 
1d85e 61 6e 64 20 6d 61 6b 65 20 73 75 72 65 20 69 74  and make sure it
1d85f 20 69 73 20 65 71 75 61 6c 20 74 6f 20 50 32 2e   is equal to P2.
1d860 20 20 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20    .** P1 is the 
1d861 64 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20  database number 
1d862 77 68 69 63 68 20 69 73 20 30 20 66 6f 72 20 74  which is 0 for t
1d863 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
1d864 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 31 20 66   file.** and 1 f
1d865 6f 72 20 74 68 65 20 66 69 6c 65 20 68 6f 6c 64  or the file hold
1d866 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61  ing temporary ta
1d867 62 6c 65 73 20 61 6e 64 20 73 6f 6d 65 20 68 69  bles and some hi
1d868 67 68 65 72 20 6e 75 6d 62 65 72 0a 2a 2a 20 66  gher number.** f
1d869 6f 72 20 61 75 78 69 6c 69 61 72 79 20 64 61 74  or auxiliary dat
1d86a 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  abases..**.** Th
1d86b 65 20 63 6f 6f 6b 69 65 20 63 68 61 6e 67 65 73  e cookie changes
1d86c 20 69 74 73 20 76 61 6c 75 65 20 77 68 65 6e 65   its value whene
1d86d 76 65 72 20 74 68 65 20 64 61 74 61 62 61 73 65  ver the database
1d86e 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 2e   schema changes.
1d86f 0a 2a 2a 20 54 68 69 73 20 6f 70 65 72 61 74 69  .** This operati
1d870 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 64 65  on is used to de
1d871 74 65 63 74 20 77 68 65 6e 20 74 68 61 74 20 74  tect when that t
1d872 68 65 20 63 6f 6f 6b 69 65 20 68 61 73 20 63 68  he cookie has ch
1d873 61 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 61  anged.** and tha
1d874 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 72  t the current pr
1d875 6f 63 65 73 73 20 6e 65 65 64 73 20 74 6f 20 72  ocess needs to r
1d876 65 72 65 61 64 20 74 68 65 20 73 63 68 65 6d 61  eread the schema
1d877 2e 0a 2a 2a 0a 2a 2a 20 45 69 74 68 65 72 20 61  ..**.** Either a
1d878 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65   transaction nee
1d879 64 73 20 74 6f 20 68 61 76 65 20 62 65 65 6e 20  ds to have been 
1d87a 73 74 61 72 74 65 64 20 6f 72 20 61 6e 20 4f 50  started or an OP
1d87b 5f 4f 70 65 6e 20 6e 65 65 64 73 0a 2a 2a 20 74  _Open needs.** t
1d87c 6f 20 62 65 20 65 78 65 63 75 74 65 64 20 28 74  o be executed (t
1d87d 6f 20 65 73 74 61 62 6c 69 73 68 20 61 20 72 65  o establish a re
1d87e 61 64 20 6c 6f 63 6b 29 20 62 65 66 6f 72 65 20  ad lock) before 
1d87f 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73 0a 2a  this opcode is.*
1d880 2a 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2f 0a 63 61  * invoked..*/.ca
1d881 73 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b  se OP_VerifyCook
1d882 69 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20  ie: {.#if 0  /* 
1d883 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1d884 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 76 20  moved into u.av 
1d885 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a  */.  int iMeta;.
1d886 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 23 65    Btree *pBt;.#e
1d887 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1d888 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1d889 74 6f 20 75 2e 61 76 20 2a 2f 0a 20 20 61 73 73  to u.av */.  ass
1d88a 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20  ert( pOp->p1>=0 
1d88b 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e  && pOp->p1<db->n
1d88c 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Db );.  assert( 
1d88d 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20  (p->btreeMask & 
1d88e 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30  (1<<pOp->p1))!=0
1d88f 20 29 3b 0a 20 20 75 2e 61 76 2e 70 42 74 20 3d   );.  u.av.pBt =
1d890 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31   db->aDb[pOp->p1
1d891 5d 2e 70 42 74 3b 0a 20 20 69 66 28 20 75 2e 61  ].pBt;.  if( u.a
1d892 76 2e 70 42 74 20 29 7b 0a 20 20 20 20 73 71 6c  v.pBt ){.    sql
1d893 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61  ite3BtreeGetMeta
1d894 28 75 2e 61 76 2e 70 42 74 2c 20 42 54 52 45 45  (u.av.pBt, BTREE
1d895 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2c  _SCHEMA_VERSION,
1d896 20 28 75 33 32 20 2a 29 26 75 2e 61 76 2e 69 4d   (u32 *)&u.av.iM
1d897 65 74 61 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  eta);.  }else{. 
1d898 20 20 20 75 2e 61 76 2e 69 4d 65 74 61 20 3d 20     u.av.iMeta = 
1d899 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 75 2e 61  0;.  }.  if( u.a
1d89a 76 2e 69 4d 65 74 61 21 3d 70 4f 70 2d 3e 70 32  v.iMeta!=pOp->p2
1d89b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   ){.    sqlite3D
1d89c 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72  bFree(db, p->zEr
1d89d 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45  rMsg);.    p->zE
1d89e 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 44  rrMsg = sqlite3D
1d89f 62 53 74 72 44 75 70 28 64 62 2c 20 22 64 61 74  bStrDup(db, "dat
1d8a0 61 62 61 73 65 20 73 63 68 65 6d 61 20 68 61 73  abase schema has
1d8a1 20 63 68 61 6e 67 65 64 22 29 3b 0a 20 20 20 20   changed");.    
1d8a2 2f 2a 20 49 66 20 74 68 65 20 73 63 68 65 6d 61  /* If the schema
1d8a3 2d 63 6f 6f 6b 69 65 20 66 72 6f 6d 20 74 68 65  -cookie from the
1d8a4 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6d   database file m
1d8a5 61 74 63 68 65 73 20 74 68 65 20 63 6f 6f 6b 69  atches the cooki
1d8a6 65 0a 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 20  e.    ** stored 
1d8a7 77 69 74 68 20 74 68 65 20 69 6e 2d 6d 65 6d 6f  with the in-memo
1d8a8 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ry representatio
1d8a9 6e 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 2c  n of the schema,
1d8aa 20 64 6f 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 72   do.    ** not r
1d8ab 65 6c 6f 61 64 20 74 68 65 20 73 63 68 65 6d 61  eload the schema
1d8ac 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61   from the databa
1d8ad 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a  se file..    **.
1d8ae 20 20 20 20 2a 2a 20 49 66 20 76 69 72 74 75 61      ** If virtua
1d8af 6c 2d 74 61 62 6c 65 73 20 61 72 65 20 69 6e 20  l-tables are in 
1d8b0 75 73 65 2c 20 74 68 69 73 20 69 73 20 6e 6f 74  use, this is not
1d8b1 20 6a 75 73 74 20 61 6e 20 6f 70 74 69 6d 69 7a   just an optimiz
1d8b2 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 20 4f 66  ation..    ** Of
1d8b3 74 65 6e 2c 20 76 2d 74 61 62 6c 65 73 20 73 74  ten, v-tables st
1d8b4 6f 72 65 20 74 68 65 69 72 20 64 61 74 61 20 69  ore their data i
1d8b5 6e 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 74  n other SQLite t
1d8b6 61 62 6c 65 73 2c 20 77 68 69 63 68 0a 20 20 20  ables, which.   
1d8b7 20 2a 2a 20 61 72 65 20 71 75 65 72 69 65 64 20   ** are queried 
1d8b8 66 72 6f 6d 20 77 69 74 68 69 6e 20 78 4e 65 78  from within xNex
1d8b9 74 28 29 20 61 6e 64 20 6f 74 68 65 72 20 76 2d  t() and other v-
1d8ba 74 61 62 6c 65 20 6d 65 74 68 6f 64 73 20 75 73  table methods us
1d8bb 69 6e 67 0a 20 20 20 20 2a 2a 20 70 72 65 70 61  ing.    ** prepa
1d8bc 72 65 64 20 71 75 65 72 69 65 73 2e 20 49 66 20  red queries. If 
1d8bd 73 75 63 68 20 61 20 71 75 65 72 79 20 69 73 20  such a query is 
1d8be 6f 75 74 2d 6f 66 2d 64 61 74 65 2c 20 77 65 20  out-of-date, we 
1d8bf 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 6f 0a 20  do not want to. 
1d8c0 20 20 20 2a 2a 20 64 69 73 63 61 72 64 20 74 68     ** discard th
1d8c1 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
1d8c2 61 2c 20 61 73 20 74 68 65 20 75 73 65 72 20 63  a, as the user c
1d8c3 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67  ode implementing
1d8c4 20 74 68 65 0a 20 20 20 20 2a 2a 20 76 2d 74 61   the.    ** v-ta
1d8c5 62 6c 65 20 77 6f 75 6c 64 20 68 61 76 65 20 74  ble would have t
1d8c6 6f 20 62 65 20 72 65 61 64 79 20 66 6f 72 20 74  o be ready for t
1d8c7 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  he sqlite3_vtab 
1d8c8 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66  structure itself
1d8c9 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 69 6e  .    ** to be in
1d8ca 76 61 6c 69 64 61 74 65 64 20 77 68 65 6e 65 76  validated whenev
1d8cb 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  er sqlite3_step(
1d8cc 29 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d  ) is called from
1d8cd 20 77 69 74 68 69 6e 0a 20 20 20 20 2a 2a 20 61   within.    ** a
1d8ce 20 76 2d 74 61 62 6c 65 20 6d 65 74 68 6f 64 2e   v-table method.
1d8cf 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1d8d0 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d  db->aDb[pOp->p1]
1d8d1 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61  .pSchema->schema
1d8d2 5f 63 6f 6f 6b 69 65 21 3d 75 2e 61 76 2e 69 4d  _cookie!=u.av.iM
1d8d3 65 74 61 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  eta ){.      sql
1d8d4 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
1d8d5 6c 53 63 68 65 6d 61 28 64 62 2c 20 70 4f 70 2d  lSchema(db, pOp-
1d8d6 3e 70 31 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  >p1);.    }..   
1d8d7 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72   sqlite3ExpirePr
1d8d8 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73  eparedStatements
1d8d9 28 64 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 53  (db);.    rc = S
1d8da 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20  QLITE_SCHEMA;.  
1d8db 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1d8dc 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 52 65 61   Opcode: OpenRea
1d8dd 64 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35  d P1 P2 P3 P4 P5
1d8de 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 72 65  .**.** Open a re
1d8df 61 64 2d 6f 6e 6c 79 20 63 75 72 73 6f 72 20 66  ad-only cursor f
1d8e0 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20  or the database 
1d8e1 74 61 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74  table whose root
1d8e2 20 70 61 67 65 20 69 73 0a 2a 2a 20 50 32 20 69   page is.** P2 i
1d8e3 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c  n a database fil
1d8e4 65 2e 20 20 54 68 65 20 64 61 74 61 62 61 73 65  e.  The database
1d8e5 20 66 69 6c 65 20 69 73 20 64 65 74 65 72 6d 69   file is determi
1d8e6 6e 65 64 20 62 79 20 50 33 2e 20 0a 2a 2a 20 50  ned by P3. .** P
1d8e7 33 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65 20 6d  3==0 means the m
1d8e8 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20 50 33  ain database, P3
1d8e9 3d 3d 31 20 6d 65 61 6e 73 20 74 68 65 20 64 61  ==1 means the da
1d8ea 74 61 62 61 73 65 20 75 73 65 64 20 66 6f 72 20  tabase used for 
1d8eb 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61  .** temporary ta
1d8ec 62 6c 65 73 2c 20 61 6e 64 20 50 33 3e 31 20 6d  bles, and P3>1 m
1d8ed 65 61 6e 73 20 75 73 65 64 20 74 68 65 20 63 6f  eans used the co
1d8ee 72 72 65 73 70 6f 6e 64 69 6e 67 20 61 74 74 61  rresponding atta
1d8ef 63 68 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 65  ched.** database
1d8f0 2e 20 20 47 69 76 65 20 74 68 65 20 6e 65 77 20  .  Give the new 
1d8f1 63 75 72 73 6f 72 20 61 6e 20 69 64 65 6e 74 69  cursor an identi
1d8f2 66 69 65 72 20 6f 66 20 50 31 2e 20 20 54 68 65  fier of P1.  The
1d8f3 20 50 31 0a 2a 2a 20 76 61 6c 75 65 73 20 6e 65   P1.** values ne
1d8f4 65 64 20 6e 6f 74 20 62 65 20 63 6f 6e 74 69 67  ed not be contig
1d8f5 75 6f 75 73 20 62 75 74 20 61 6c 6c 20 50 31 20  uous but all P1 
1d8f6 76 61 6c 75 65 73 20 73 68 6f 75 6c 64 20 62 65  values should be
1d8f7 20 73 6d 61 6c 6c 20 69 6e 74 65 67 65 72 73 2e   small integers.
1d8f8 0a 2a 2a 20 49 74 20 69 73 20 61 6e 20 65 72 72  .** It is an err
1d8f9 6f 72 20 66 6f 72 20 50 31 20 74 6f 20 62 65 20  or for P1 to be 
1d8fa 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20  negative..**.** 
1d8fb 49 66 20 50 35 21 3d 30 20 74 68 65 6e 20 75 73  If P5!=0 then us
1d8fc 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  e the content of
1d8fd 20 72 65 67 69 73 74 65 72 20 50 32 20 61 73 20   register P2 as 
1d8fe 74 68 65 20 72 6f 6f 74 20 70 61 67 65 2c 20 6e  the root page, n
1d8ff 6f 74 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20  ot.** the value 
1d900 6f 66 20 50 32 20 69 74 73 65 6c 66 2e 0a 2a 2a  of P2 itself..**
1d901 0a 2a 2a 20 54 68 65 72 65 20 77 69 6c 6c 20 62  .** There will b
1d902 65 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e  e a read lock on
1d903 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 68   the database wh
1d904 65 6e 65 76 65 72 20 74 68 65 72 65 20 69 73 20  enever there is 
1d905 61 6e 0a 2a 2a 20 6f 70 65 6e 20 63 75 72 73 6f  an.** open curso
1d906 72 2e 20 20 49 66 20 74 68 65 20 64 61 74 61 62  r.  If the datab
1d907 61 73 65 20 77 61 73 20 75 6e 6c 6f 63 6b 65 64  ase was unlocked
1d908 20 70 72 69 6f 72 20 74 6f 20 74 68 69 73 20 69   prior to this i
1d909 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 74 68  nstruction.** th
1d90a 65 6e 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 69  en a read lock i
1d90b 73 20 61 63 71 75 69 72 65 64 20 61 73 20 70 61  s acquired as pa
1d90c 72 74 20 6f 66 20 74 68 69 73 20 69 6e 73 74 72  rt of this instr
1d90d 75 63 74 69 6f 6e 2e 20 20 41 20 72 65 61 64 0a  uction.  A read.
1d90e 2a 2a 20 6c 6f 63 6b 20 61 6c 6c 6f 77 73 20 6f  ** lock allows o
1d90f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 74  ther processes t
1d910 6f 20 72 65 61 64 20 74 68 65 20 64 61 74 61 62  o read the datab
1d911 61 73 65 20 62 75 74 20 70 72 6f 68 69 62 69 74  ase but prohibit
1d912 73 0a 2a 2a 20 61 6e 79 20 6f 74 68 65 72 20 70  s.** any other p
1d913 72 6f 63 65 73 73 20 66 72 6f 6d 20 6d 6f 64 69  rocess from modi
1d914 66 79 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  fying the databa
1d915 73 65 2e 20 20 54 68 65 20 72 65 61 64 20 6c 6f  se.  The read lo
1d916 63 6b 20 69 73 0a 2a 2a 20 72 65 6c 65 61 73 65  ck is.** release
1d917 64 20 77 68 65 6e 20 61 6c 6c 20 63 75 72 73 6f  d when all curso
1d918 72 73 20 61 72 65 20 63 6c 6f 73 65 64 2e 20 20  rs are closed.  
1d919 49 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74  If this instruct
1d91a 69 6f 6e 20 61 74 74 65 6d 70 74 73 0a 2a 2a 20  ion attempts.** 
1d91b 74 6f 20 67 65 74 20 61 20 72 65 61 64 20 6c 6f  to get a read lo
1d91c 63 6b 20 62 75 74 20 66 61 69 6c 73 2c 20 74 68  ck but fails, th
1d91d 65 20 73 63 72 69 70 74 20 74 65 72 6d 69 6e 61  e script termina
1d91e 74 65 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 53  tes with an.** S
1d91f 51 4c 49 54 45 5f 42 55 53 59 20 65 72 72 6f 72  QLITE_BUSY error
1d920 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   code..**.** The
1d921 20 50 34 20 76 61 6c 75 65 20 6d 61 79 20 62 65   P4 value may be
1d922 20 65 69 74 68 65 72 20 61 6e 20 69 6e 74 65 67   either an integ
1d923 65 72 20 28 50 34 5f 49 4e 54 33 32 29 20 6f 72  er (P4_INT32) or
1d924 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a   a pointer to.**
1d925 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63   a KeyInfo struc
1d926 74 75 72 65 20 28 50 34 5f 4b 45 59 49 4e 46 4f  ture (P4_KEYINFO
1d927 29 2e 20 49 66 20 69 74 20 69 73 20 61 20 70 6f  ). If it is a po
1d928 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e  inter to a KeyIn
1d929 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  fo .** structure
1d92a 2c 20 74 68 65 6e 20 73 61 69 64 20 73 74 72 75  , then said stru
1d92b 63 74 75 72 65 20 64 65 66 69 6e 65 73 20 74 68  cture defines th
1d92c 65 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f  e content and co
1d92d 6c 6c 61 74 69 6e 67 20 0a 2a 2a 20 73 65 71 75  llating .** sequ
1d92e 65 6e 63 65 20 6f 66 20 74 68 65 20 69 6e 64 65  ence of the inde
1d92f 78 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20  x being opened. 
1d930 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 50 34  Otherwise, if P4
1d931 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 0a   is an integer .
1d932 2a 2a 20 76 61 6c 75 65 2c 20 69 74 20 69 73 20  ** value, it is 
1d933 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65  set to the numbe
1d934 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
1d935 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  the table..**.**
1d936 20 53 65 65 20 61 6c 73 6f 20 4f 70 65 6e 57 72   See also OpenWr
1d937 69 74 65 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  ite..*/./* Opcod
1d938 65 3a 20 4f 70 65 6e 57 72 69 74 65 20 50 31 20  e: OpenWrite P1 
1d939 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a  P2 P3 P4 P5.**.*
1d93a 2a 20 4f 70 65 6e 20 61 20 72 65 61 64 2f 77 72  * Open a read/wr
1d93b 69 74 65 20 63 75 72 73 6f 72 20 6e 61 6d 65 64  ite cursor named
1d93c 20 50 31 20 6f 6e 20 74 68 65 20 74 61 62 6c 65   P1 on the table
1d93d 20 6f 72 20 69 6e 64 65 78 20 77 68 6f 73 65 20   or index whose 
1d93e 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73 20  root.** page is 
1d93f 50 32 2e 20 20 4f 72 20 69 66 20 50 35 21 3d 30  P2.  Or if P5!=0
1d940 20 75 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74   use the content
1d941 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 32 20   of register P2 
1d942 74 6f 20 66 69 6e 64 20 74 68 65 0a 2a 2a 20 72  to find the.** r
1d943 6f 6f 74 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  oot page..**.** 
1d944 54 68 65 20 50 34 20 76 61 6c 75 65 20 6d 61 79  The P4 value may
1d945 20 62 65 20 65 69 74 68 65 72 20 61 6e 20 69 6e   be either an in
1d946 74 65 67 65 72 20 28 50 34 5f 49 4e 54 33 32 29  teger (P4_INT32)
1d947 20 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   or a pointer to
1d948 0a 2a 2a 20 61 20 4b 65 79 49 6e 66 6f 20 73 74  .** a KeyInfo st
1d949 72 75 63 74 75 72 65 20 28 50 34 5f 4b 45 59 49  ructure (P4_KEYI
1d94a 4e 46 4f 29 2e 20 49 66 20 69 74 20 69 73 20 61  NFO). If it is a
1d94b 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65   pointer to a Ke
1d94c 79 49 6e 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74  yInfo .** struct
1d94d 75 72 65 2c 20 74 68 65 6e 20 73 61 69 64 20 73  ure, then said s
1d94e 74 72 75 63 74 75 72 65 20 64 65 66 69 6e 65 73  tructure defines
1d94f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 61 6e 64   the content and
1d950 20 63 6f 6c 6c 61 74 69 6e 67 20 0a 2a 2a 20 73   collating .** s
1d951 65 71 75 65 6e 63 65 20 6f 66 20 74 68 65 20 69  equence of the i
1d952 6e 64 65 78 20 62 65 69 6e 67 20 6f 70 65 6e 65  ndex being opene
1d953 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66  d. Otherwise, if
1d954 20 50 34 20 69 73 20 61 6e 20 69 6e 74 65 67 65   P4 is an intege
1d955 72 20 0a 2a 2a 20 76 61 6c 75 65 2c 20 69 74 20  r .** value, it 
1d956 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75  is set to the nu
1d957 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
1d958 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20 6f 72  in the table, or
1d959 20 74 6f 20 74 68 65 0a 2a 2a 20 6c 61 72 67 65   to the.** large
1d95a 73 74 20 69 6e 64 65 78 20 6f 66 20 61 6e 79 20  st index of any 
1d95b 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74 61  column of the ta
1d95c 62 6c 65 20 74 68 61 74 20 69 73 20 61 63 74 75  ble that is actu
1d95d 61 6c 6c 79 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a  ally used..**.**
1d95e 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   This instructio
1d95f 6e 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b  n works just lik
1d960 65 20 4f 70 65 6e 52 65 61 64 20 65 78 63 65 70  e OpenRead excep
1d961 74 20 74 68 61 74 20 69 74 20 6f 70 65 6e 73 20  t that it opens 
1d962 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 6e  the cursor.** in
1d963 20 72 65 61 64 2f 77 72 69 74 65 20 6d 6f 64 65   read/write mode
1d964 2e 20 20 46 6f 72 20 61 20 67 69 76 65 6e 20 74  .  For a given t
1d965 61 62 6c 65 2c 20 74 68 65 72 65 20 63 61 6e 20  able, there can 
1d966 62 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 72  be one or more r
1d967 65 61 64 2d 6f 6e 6c 79 0a 2a 2a 20 63 75 72 73  ead-only.** curs
1d968 6f 72 73 20 6f 72 20 61 20 73 69 6e 67 6c 65 20  ors or a single 
1d969 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f  read/write curso
1d96a 72 20 62 75 74 20 6e 6f 74 20 62 6f 74 68 2e 0a  r but not both..
1d96b 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 4f  **.** See also O
1d96c 70 65 6e 52 65 61 64 2e 0a 2a 2f 0a 63 61 73 65  penRead..*/.case
1d96d 20 4f 50 5f 4f 70 65 6e 52 65 61 64 3a 0a 63 61   OP_OpenRead:.ca
1d96e 73 65 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 3a  se OP_OpenWrite:
1d96f 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
1d970 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1d971 65 64 20 69 6e 74 6f 20 75 2e 61 77 20 2a 2f 0a  ed into u.aw */.
1d972 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 20    int nField;.  
1d973 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66  KeyInfo *pKeyInf
1d974 6f 3b 0a 20 20 69 6e 74 20 70 32 3b 0a 20 20 69  o;.  int p2;.  i
1d975 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 77 72  nt iDb;.  int wr
1d976 46 6c 61 67 3b 0a 20 20 42 74 72 65 65 20 2a 70  Flag;.  Btree *p
1d977 58 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  X;.  VdbeCursor 
1d978 2a 70 43 75 72 3b 0a 20 20 44 62 20 2a 70 44 62  *pCur;.  Db *pDb
1d979 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1d97a 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1d97b 64 20 69 6e 74 6f 20 75 2e 61 77 20 2a 2f 0a 0a  d into u.aw */..
1d97c 20 20 75 2e 61 77 2e 6e 46 69 65 6c 64 20 3d 20    u.aw.nField = 
1d97d 30 3b 0a 20 20 75 2e 61 77 2e 70 4b 65 79 49 6e  0;.  u.aw.pKeyIn
1d97e 66 6f 20 3d 20 30 3b 0a 20 20 75 2e 61 77 2e 70  fo = 0;.  u.aw.p
1d97f 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75  2 = pOp->p2;.  u
1d980 2e 61 77 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70  .aw.iDb = pOp->p
1d981 33 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61  3;.  assert( u.a
1d982 77 2e 69 44 62 3e 3d 30 20 26 26 20 75 2e 61 77  w.iDb>=0 && u.aw
1d983 2e 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a  .iDb<db->nDb );.
1d984 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74    assert( (p->bt
1d985 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 75 2e  reeMask & (1<<u.
1d986 61 77 2e 69 44 62 29 29 21 3d 30 20 29 3b 0a 20  aw.iDb))!=0 );. 
1d987 20 75 2e 61 77 2e 70 44 62 20 3d 20 26 64 62 2d   u.aw.pDb = &db-
1d988 3e 61 44 62 5b 75 2e 61 77 2e 69 44 62 5d 3b 0a  >aDb[u.aw.iDb];.
1d989 20 20 75 2e 61 77 2e 70 58 20 3d 20 75 2e 61 77    u.aw.pX = u.aw
1d98a 2e 70 44 62 2d 3e 70 42 74 3b 0a 20 20 61 73 73  .pDb->pBt;.  ass
1d98b 65 72 74 28 20 75 2e 61 77 2e 70 58 21 3d 30 20  ert( u.aw.pX!=0 
1d98c 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70  );.  if( pOp->op
1d98d 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69  code==OP_OpenWri
1d98e 74 65 20 29 7b 0a 20 20 20 20 75 2e 61 77 2e 77  te ){.    u.aw.w
1d98f 72 46 6c 61 67 20 3d 20 31 3b 0a 20 20 20 20 69  rFlag = 1;.    i
1d990 66 28 20 75 2e 61 77 2e 70 44 62 2d 3e 70 53 63  f( u.aw.pDb->pSc
1d991 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61  hema->file_forma
1d992 74 20 3c 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46  t < p->minWriteF
1d993 69 6c 65 46 6f 72 6d 61 74 20 29 7b 0a 20 20 20  ileFormat ){.   
1d994 20 20 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69     p->minWriteFi
1d995 6c 65 46 6f 72 6d 61 74 20 3d 20 75 2e 61 77 2e  leFormat = u.aw.
1d996 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69  pDb->pSchema->fi
1d997 6c 65 5f 66 6f 72 6d 61 74 3b 0a 20 20 20 20 7d  le_format;.    }
1d998 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e  .  }else{.    u.
1d999 61 77 2e 77 72 46 6c 61 67 20 3d 20 30 3b 0a 20  aw.wrFlag = 0;. 
1d99a 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35   }.  if( pOp->p5
1d99b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1d99c 75 2e 61 77 2e 70 32 3e 30 20 29 3b 0a 20 20 20  u.aw.p2>0 );.   
1d99d 20 61 73 73 65 72 74 28 20 75 2e 61 77 2e 70 32   assert( u.aw.p2
1d99e 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20  <=p->nMem );.   
1d99f 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d   pIn2 = &p->aMem
1d9a0 5b 75 2e 61 77 2e 70 32 5d 3b 0a 20 20 20 20 73  [u.aw.p2];.    s
1d9a1 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74  qlite3VdbeMemInt
1d9a2 65 67 65 72 69 66 79 28 70 49 6e 32 29 3b 0a 20  egerify(pIn2);. 
1d9a3 20 20 20 75 2e 61 77 2e 70 32 20 3d 20 28 69 6e     u.aw.p2 = (in
1d9a4 74 29 70 49 6e 32 2d 3e 75 2e 69 3b 0a 20 20 20  t)pIn2->u.i;.   
1d9a5 20 2f 2a 20 54 68 65 20 75 2e 61 77 2e 70 32 20   /* The u.aw.p2 
1d9a6 76 61 6c 75 65 20 61 6c 77 61 79 73 20 63 6f 6d  value always com
1d9a7 65 73 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 20  es from a prior 
1d9a8 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 6f  OP_CreateTable o
1d9a9 70 63 6f 64 65 20 61 6e 64 0a 20 20 20 20 2a 2a  pcode and.    **
1d9aa 20 74 68 61 74 20 6f 70 63 6f 64 65 20 77 69 6c   that opcode wil
1d9ab 6c 20 61 6c 77 61 79 73 20 73 65 74 20 74 68 65  l always set the
1d9ac 20 75 2e 61 77 2e 70 32 20 76 61 6c 75 65 20 74   u.aw.p2 value t
1d9ad 6f 20 32 20 6f 72 20 6d 6f 72 65 20 6f 72 20 65  o 2 or more or e
1d9ae 6c 73 65 20 66 61 69 6c 2e 0a 20 20 20 20 2a 2a  lse fail..    **
1d9af 20 49 66 20 74 68 65 72 65 20 77 65 72 65 20 61   If there were a
1d9b0 20 66 61 69 6c 75 72 65 2c 20 74 68 65 20 70 72   failure, the pr
1d9b1 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
1d9b2 20 77 6f 75 6c 64 20 68 61 76 65 20 68 61 6c 74   would have halt
1d9b3 65 64 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65  ed.    ** before
1d9b4 20 72 65 61 63 68 69 6e 67 20 74 68 69 73 20 69   reaching this i
1d9b5 6e 73 74 72 75 63 74 69 6f 6e 2e 20 2a 2f 0a 20  nstruction. */. 
1d9b6 20 20 20 69 66 28 20 4e 45 56 45 52 28 75 2e 61     if( NEVER(u.a
1d9b7 77 2e 70 32 3c 32 29 20 29 20 7b 0a 20 20 20 20  w.p2<2) ) {.    
1d9b8 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f    rc = SQLITE_CO
1d9b9 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
1d9ba 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65    goto abort_due
1d9bb 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d  _to_error;.    }
1d9bc 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e  .  }.  if( pOp->
1d9bd 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e  p4type==P4_KEYIN
1d9be 46 4f 20 29 7b 0a 20 20 20 20 75 2e 61 77 2e 70  FO ){.    u.aw.p
1d9bf 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70  KeyInfo = pOp->p
1d9c0 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20  4.pKeyInfo;.    
1d9c1 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 2d 3e 65  u.aw.pKeyInfo->e
1d9c2 6e 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62 29 3b  nc = ENC(p->db);
1d9c3 0a 20 20 20 20 75 2e 61 77 2e 6e 46 69 65 6c 64  .    u.aw.nField
1d9c4 20 3d 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f   = u.aw.pKeyInfo
1d9c5 2d 3e 6e 46 69 65 6c 64 2b 31 3b 0a 20 20 7d 65  ->nField+1;.  }e
1d9c6 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70 34 74  lse if( pOp->p4t
1d9c7 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 7b  ype==P4_INT32 ){
1d9c8 0a 20 20 20 20 75 2e 61 77 2e 6e 46 69 65 6c 64  .    u.aw.nField
1d9c9 20 3d 20 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20   = pOp->p4.i;.  
1d9ca 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  }.  assert( pOp-
1d9cb 3e 70 31 3e 3d 30 20 29 3b 0a 20 20 75 2e 61 77  >p1>=0 );.  u.aw
1d9cc 2e 70 43 75 72 20 3d 20 61 6c 6c 6f 63 61 74 65  .pCur = allocate
1d9cd 43 75 72 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70  Cursor(p, pOp->p
1d9ce 31 2c 20 75 2e 61 77 2e 6e 46 69 65 6c 64 2c 20  1, u.aw.nField, 
1d9cf 75 2e 61 77 2e 69 44 62 2c 20 31 29 3b 0a 20 20  u.aw.iDb, 1);.  
1d9d0 69 66 28 20 75 2e 61 77 2e 70 43 75 72 3d 3d 30  if( u.aw.pCur==0
1d9d1 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
1d9d2 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e 6e 75 6c    u.aw.pCur->nul
1d9d3 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 72 63 20 3d  lRow = 1;.  rc =
1d9d4 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
1d9d5 73 6f 72 28 75 2e 61 77 2e 70 58 2c 20 75 2e 61  sor(u.aw.pX, u.a
1d9d6 77 2e 70 32 2c 20 75 2e 61 77 2e 77 72 46 6c 61  w.p2, u.aw.wrFla
1d9d7 67 2c 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f  g, u.aw.pKeyInfo
1d9d8 2c 20 75 2e 61 77 2e 70 43 75 72 2d 3e 70 43 75  , u.aw.pCur->pCu
1d9d9 72 73 6f 72 29 3b 0a 20 20 75 2e 61 77 2e 70 43  rsor);.  u.aw.pC
1d9da 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 75  ur->pKeyInfo = u
1d9db 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 3b 0a 0a 20  .aw.pKeyInfo;.. 
1d9dc 20 2f 2a 20 53 69 6e 63 65 20 69 74 20 70 65 72   /* Since it per
1d9dd 66 6f 72 6d 73 20 6e 6f 20 6d 65 6d 6f 72 79 20  forms no memory 
1d9de 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 72 20 49 4f  allocation or IO
1d9df 2c 20 74 68 65 20 6f 6e 6c 79 20 76 61 6c 75 65  , the only value
1d9e0 73 20 74 68 61 74 0a 20 20 2a 2a 20 73 71 6c 69  s that.  ** sqli
1d9e1 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 29  te3BtreeCursor()
1d9e2 20 6d 61 79 20 72 65 74 75 72 6e 20 61 72 65 20   may return are 
1d9e3 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 61 6e 64  SQLITE_EMPTY and
1d9e4 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2a   SQLITE_OK..  **
1d9e5 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 69 73   SQLITE_EMPTY is
1d9e6 20 6f 6e 6c 79 20 72 65 74 75 72 6e 65 64 20 77   only returned w
1d9e7 68 65 6e 20 61 74 74 65 6d 70 74 69 6e 67 20 74  hen attempting t
1d9e8 6f 20 6f 70 65 6e 20 74 68 65 20 74 61 62 6c 65  o open the table
1d9e9 0a 20 20 2a 2a 20 72 6f 6f 74 65 64 20 61 74 20  .  ** rooted at 
1d9ea 70 61 67 65 20 31 20 6f 66 20 61 20 7a 65 72 6f  page 1 of a zero
1d9eb 2d 62 79 74 65 20 64 61 74 61 62 61 73 65 2e 20  -byte database. 
1d9ec 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 72 63   */.  assert( rc
1d9ed 3d 3d 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 7c  ==SQLITE_EMPTY |
1d9ee 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  | rc==SQLITE_OK 
1d9ef 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
1d9f0 49 54 45 5f 45 4d 50 54 59 20 29 7b 0a 20 20 20  ITE_EMPTY ){.   
1d9f1 20 75 2e 61 77 2e 70 43 75 72 2d 3e 70 43 75 72   u.aw.pCur->pCur
1d9f2 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20 72 63 20  sor = 0;.    rc 
1d9f3 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  = SQLITE_OK;.  }
1d9f4 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 56  ..  /* Set the V
1d9f5 64 62 65 43 75 72 73 6f 72 2e 69 73 54 61 62 6c  dbeCursor.isTabl
1d9f6 65 20 61 6e 64 20 69 73 49 6e 64 65 78 20 76 61  e and isIndex va
1d9f7 72 69 61 62 6c 65 73 2e 20 50 72 65 76 69 6f 75  riables. Previou
1d9f8 73 20 76 65 72 73 69 6f 6e 73 20 6f 66 0a 20 20  s versions of.  
1d9f9 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 64 20 74  ** SQLite used t
1d9fa 6f 20 63 68 65 63 6b 20 69 66 20 74 68 65 20 72  o check if the r
1d9fb 6f 6f 74 2d 70 61 67 65 20 66 6c 61 67 73 20 77  oot-page flags w
1d9fc 65 72 65 20 73 61 6e 65 20 61 74 20 74 68 69 73  ere sane at this
1d9fd 20 70 6f 69 6e 74 0a 20 20 2a 2a 20 61 6e 64 20   point.  ** and 
1d9fe 72 65 70 6f 72 74 20 64 61 74 61 62 61 73 65 20  report database 
1d9ff 63 6f 72 72 75 70 74 69 6f 6e 20 69 66 20 74 68  corruption if th
1da00 65 79 20 77 65 72 65 20 6e 6f 74 2c 20 62 75 74  ey were not, but
1da01 20 74 68 69 73 20 63 68 65 63 6b 20 68 61 73 0a   this check has.
1da02 20 20 2a 2a 20 73 69 6e 63 65 20 6d 6f 76 65 64    ** since moved
1da03 20 69 6e 74 6f 20 74 68 65 20 62 74 72 65 65 20   into the btree 
1da04 6c 61 79 65 72 2e 20 20 2a 2f 0a 20 20 75 2e 61  layer.  */.  u.a
1da05 77 2e 70 43 75 72 2d 3e 69 73 54 61 62 6c 65 20  w.pCur->isTable 
1da06 3d 20 70 4f 70 2d 3e 70 34 74 79 70 65 21 3d 50  = pOp->p4type!=P
1da07 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 75 2e 61  4_KEYINFO;.  u.a
1da08 77 2e 70 43 75 72 2d 3e 69 73 49 6e 64 65 78 20  w.pCur->isIndex 
1da09 3d 20 21 75 2e 61 77 2e 70 43 75 72 2d 3e 69 73  = !u.aw.pCur->is
1da0a 54 61 62 6c 65 3b 0a 20 20 62 72 65 61 6b 3b 0a  Table;.  break;.
1da0b 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70  }../* Opcode: Op
1da0c 65 6e 45 70 68 65 6d 65 72 61 6c 20 50 31 20 50  enEphemeral P1 P
1da0d 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f  2 * P4 *.**.** O
1da0e 70 65 6e 20 61 20 6e 65 77 20 63 75 72 73 6f 72  pen a new cursor
1da0f 20 50 31 20 74 6f 20 61 20 74 72 61 6e 73 69 65   P1 to a transie
1da10 6e 74 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65  nt table..** The
1da11 20 63 75 72 73 6f 72 20 69 73 20 61 6c 77 61 79   cursor is alway
1da12 73 20 6f 70 65 6e 65 64 20 72 65 61 64 2f 77 72  s opened read/wr
1da13 69 74 65 20 65 76 65 6e 20 69 66 20 0a 2a 2a 20  ite even if .** 
1da14 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
1da15 65 20 69 73 20 72 65 61 64 2d 6f 6e 6c 79 2e 20  e is read-only. 
1da16 20 54 68 65 20 74 72 61 6e 73 69 65 6e 74 20 6f   The transient o
1da17 72 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62  r virtual.** tab
1da18 6c 65 20 69 73 20 64 65 6c 65 74 65 64 20 61 75  le is deleted au
1da19 74 6f 6d 61 74 69 63 61 6c 6c 79 20 77 68 65 6e  tomatically when
1da1a 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 63   the cursor is c
1da1b 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 32 20  losed..**.** P2 
1da1c 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
1da1d 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
1da1e 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a  virtual table..*
1da1f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 70 6f 69  * The cursor poi
1da20 6e 74 73 20 74 6f 20 61 20 42 54 72 65 65 20 74  nts to a BTree t
1da21 61 62 6c 65 20 69 66 20 50 34 3d 3d 30 20 61 6e  able if P4==0 an
1da22 64 20 74 6f 20 61 20 42 54 72 65 65 20 69 6e 64  d to a BTree ind
1da23 65 78 0a 2a 2a 20 69 66 20 50 34 20 69 73 20 6e  ex.** if P4 is n
1da24 6f 74 20 30 2e 20 20 49 66 20 50 34 20 69 73 20  ot 0.  If P4 is 
1da25 6e 6f 74 20 4e 55 4c 4c 2c 20 69 74 20 70 6f 69  not NULL, it poi
1da26 6e 74 73 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f  nts to a KeyInfo
1da27 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 74 68   structure.** th
1da28 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 66  at defines the f
1da29 6f 72 6d 61 74 20 6f 66 20 6b 65 79 73 20 69 6e  ormat of keys in
1da2a 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a   the index..**.*
1da2b 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 61  * This opcode wa
1da2c 73 20 6f 6e 63 65 20 63 61 6c 6c 65 64 20 4f 70  s once called Op
1da2d 65 6e 54 65 6d 70 2e 20 20 42 75 74 20 74 68 61  enTemp.  But tha
1da2e 74 20 63 72 65 61 74 65 64 0a 2a 2a 20 63 6f 6e  t created.** con
1da2f 66 75 73 69 6f 6e 20 62 65 63 61 75 73 65 20 74  fusion because t
1da30 68 65 20 74 65 72 6d 20 22 74 65 6d 70 20 74 61  he term "temp ta
1da31 62 6c 65 22 2c 20 6d 69 67 68 74 20 72 65 66 65  ble", might refe
1da32 72 20 65 69 74 68 65 72 0a 2a 2a 20 74 6f 20 61  r either.** to a
1da33 20 54 45 4d 50 20 74 61 62 6c 65 20 61 74 20 74   TEMP table at t
1da34 68 65 20 53 51 4c 20 6c 65 76 65 6c 2c 20 6f 72  he SQL level, or
1da35 20 74 6f 20 61 20 74 61 62 6c 65 20 6f 70 65 6e   to a table open
1da36 65 64 20 62 79 0a 2a 2a 20 74 68 69 73 20 6f 70  ed by.** this op
1da37 63 6f 64 65 2e 20 20 54 68 65 6e 20 74 68 69 73  code.  Then this
1da38 20 6f 70 63 6f 64 65 20 77 61 73 20 63 61 6c 6c   opcode was call
1da39 20 4f 70 65 6e 56 69 72 74 75 61 6c 2e 20 20 42   OpenVirtual.  B
1da3a 75 74 0a 2a 2a 20 74 68 61 74 20 63 72 65 61 74  ut.** that creat
1da3b 65 64 20 63 6f 6e 66 75 73 69 6f 6e 20 77 69 74  ed confusion wit
1da3c 68 20 74 68 65 20 77 68 6f 6c 65 20 76 69 72 74  h the whole virt
1da3d 75 61 6c 2d 74 61 62 6c 65 20 69 64 65 61 2e 0a  ual-table idea..
1da3e 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e 45  */.case OP_OpenE
1da3f 70 68 65 6d 65 72 61 6c 3a 20 7b 0a 23 69 66 20  phemeral: {.#if 
1da40 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1da41 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1da42 20 75 2e 61 78 20 2a 2f 0a 20 20 56 64 62 65 43   u.ax */.  VdbeC
1da43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 23 65 6e 64  ursor *pCx;.#end
1da44 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1da45 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1da46 20 75 2e 61 78 20 2a 2f 0a 20 20 73 74 61 74 69   u.ax */.  stati
1da47 63 20 63 6f 6e 73 74 20 69 6e 74 20 6f 70 65 6e  c const int open
1da48 46 6c 61 67 73 20 3d 0a 20 20 20 20 20 20 53 51  Flags =.      SQ
1da49 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
1da4a 49 54 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49  ITE |.      SQLI
1da4b 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 7c  TE_OPEN_CREATE |
1da4c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50  .      SQLITE_OP
1da4d 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c 0a 20  EN_EXCLUSIVE |. 
1da4e 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
1da4f 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 7c  _DELETEONCLOSE |
1da50 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50  .      SQLITE_OP
1da51 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 3b  EN_TRANSIENT_DB;
1da52 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ..  assert( pOp-
1da53 3e 70 31 3e 3d 30 20 29 3b 0a 20 20 75 2e 61 78  >p1>=0 );.  u.ax
1da54 2e 70 43 78 20 3d 20 61 6c 6c 6f 63 61 74 65 43  .pCx = allocateC
1da55 75 72 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31  ursor(p, pOp->p1
1da56 2c 20 70 4f 70 2d 3e 70 32 2c 20 2d 31 2c 20 31  , pOp->p2, -1, 1
1da57 29 3b 0a 20 20 69 66 28 20 75 2e 61 78 2e 70 43  );.  if( u.ax.pC
1da58 78 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d  x==0 ) goto no_m
1da59 65 6d 3b 0a 20 20 75 2e 61 78 2e 70 43 78 2d 3e  em;.  u.ax.pCx->
1da5a 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 72  nullRow = 1;.  r
1da5b 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1da5c 46 61 63 74 6f 72 79 28 64 62 2c 20 30 2c 20 31  Factory(db, 0, 1
1da5d 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  , SQLITE_DEFAULT
1da5e 5f 54 45 4d 50 5f 43 41 43 48 45 5f 53 49 5a 45  _TEMP_CACHE_SIZE
1da5f 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 0a 20 20 20  , openFlags,.   
1da60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1da61 20 20 20 20 20 20 20 20 26 75 2e 61 78 2e 70 43          &u.ax.pC
1da62 78 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28 20 72  x->pBt);.  if( r
1da63 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1da64 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1da65 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28  BtreeBeginTrans(
1da66 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74 2c 20 31  u.ax.pCx->pBt, 1
1da67 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  );.  }.  if( rc=
1da68 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1da69 20 20 2f 2a 20 49 66 20 61 20 74 72 61 6e 73 69    /* If a transi
1da6a 65 6e 74 20 69 6e 64 65 78 20 69 73 20 72 65 71  ent index is req
1da6b 75 69 72 65 64 2c 20 63 72 65 61 74 65 20 69 74  uired, create it
1da6c 20 62 79 20 63 61 6c 6c 69 6e 67 0a 20 20 20 20   by calling.    
1da6d 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  ** sqlite3BtreeC
1da6e 72 65 61 74 65 54 61 62 6c 65 28 29 20 77 69 74  reateTable() wit
1da6f 68 20 74 68 65 20 42 54 52 45 45 5f 5a 45 52 4f  h the BTREE_ZERO
1da70 44 41 54 41 20 66 6c 61 67 20 62 65 66 6f 72 65  DATA flag before
1da71 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20  .    ** opening 
1da72 69 74 2e 20 49 66 20 61 20 74 72 61 6e 73 69 65  it. If a transie
1da73 6e 74 20 74 61 62 6c 65 20 69 73 20 72 65 71 75  nt table is requ
1da74 69 72 65 64 2c 20 6a 75 73 74 20 75 73 65 20 74  ired, just use t
1da75 68 65 0a 20 20 20 20 2a 2a 20 61 75 74 6f 6d 61  he.    ** automa
1da76 74 69 63 61 6c 6c 79 20 63 72 65 61 74 65 64 20  tically created 
1da77 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d  table with root-
1da78 70 61 67 65 20 31 20 28 61 6e 20 49 4e 54 4b 45  page 1 (an INTKE
1da79 59 20 74 61 62 6c 65 29 2e 0a 20 20 20 20 2a 2f  Y table)..    */
1da7a 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 34  .    if( pOp->p4
1da7b 2e 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20 20  .pKeyInfo ){.   
1da7c 20 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20 20 20     int pgno;.   
1da7d 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
1da7e 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e  p4type==P4_KEYIN
1da7f 46 4f 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  FO );.      rc =
1da80 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65   sqlite3BtreeCre
1da81 61 74 65 54 61 62 6c 65 28 75 2e 61 78 2e 70 43  ateTable(u.ax.pC
1da82 78 2d 3e 70 42 74 2c 20 26 70 67 6e 6f 2c 20 42  x->pBt, &pgno, B
1da83 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 29 3b 0a  TREE_ZERODATA);.
1da84 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
1da85 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1da86 20 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3d     assert( pgno=
1da87 3d 4d 41 53 54 45 52 5f 52 4f 4f 54 2b 31 20 29  =MASTER_ROOT+1 )
1da88 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
1da89 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f  qlite3BtreeCurso
1da8a 72 28 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74 2c  r(u.ax.pCx->pBt,
1da8b 20 70 67 6e 6f 2c 20 31 2c 0a 20 20 20 20 20 20   pgno, 1,.      
1da8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1da8d 20 20 20 20 20 20 20 20 20 20 28 4b 65 79 49 6e            (KeyIn
1da8e 66 6f 2a 29 70 4f 70 2d 3e 70 34 2e 7a 2c 20 75  fo*)pOp->p4.z, u
1da8f 2e 61 78 2e 70 43 78 2d 3e 70 43 75 72 73 6f 72  .ax.pCx->pCursor
1da90 29 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 78 2e  );.        u.ax.
1da91 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20  pCx->pKeyInfo = 
1da92 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f  pOp->p4.pKeyInfo
1da93 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 78 2e 70  ;.        u.ax.p
1da94 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e  Cx->pKeyInfo->en
1da95 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62 29 3b 0a  c = ENC(p->db);.
1da96 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 75 2e        }.      u.
1da97 61 78 2e 70 43 78 2d 3e 69 73 54 61 62 6c 65 20  ax.pCx->isTable 
1da98 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = 0;.    }else{.
1da99 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1da9a 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 75 2e  e3BtreeCursor(u.
1da9b 61 78 2e 70 43 78 2d 3e 70 42 74 2c 20 4d 41 53  ax.pCx->pBt, MAS
1da9c 54 45 52 5f 52 4f 4f 54 2c 20 31 2c 20 30 2c 20  TER_ROOT, 1, 0, 
1da9d 75 2e 61 78 2e 70 43 78 2d 3e 70 43 75 72 73 6f  u.ax.pCx->pCurso
1da9e 72 29 3b 0a 20 20 20 20 20 20 75 2e 61 78 2e 70  r);.      u.ax.p
1da9f 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20 31 3b  Cx->isTable = 1;
1daa0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 75 2e 61  .    }.  }.  u.a
1daa1 78 2e 70 43 78 2d 3e 69 73 49 6e 64 65 78 20 3d  x.pCx->isIndex =
1daa2 20 21 75 2e 61 78 2e 70 43 78 2d 3e 69 73 54 61   !u.ax.pCx->isTa
1daa3 62 6c 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  ble;.  break;.}.
1daa4 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e  ./* Opcode: Open
1daa5 50 73 65 75 64 6f 20 50 31 20 50 32 20 50 33 20  Pseudo P1 P2 P3 
1daa6 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61  * *.**.** Open a
1daa7 20 6e 65 77 20 63 75 72 73 6f 72 20 74 68 61 74   new cursor that
1daa8 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 66 61 6b   points to a fak
1daa9 65 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f 6e  e table that con
1daaa 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 0a 2a  tains a single.*
1daab 2a 20 72 6f 77 20 6f 66 20 64 61 74 61 2e 20 20  * row of data.  
1daac 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74  The content of t
1daad 68 61 74 20 6f 6e 65 20 72 6f 77 20 69 6e 20 74  hat one row in t
1daae 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 6d 65  he content of me
1daaf 6d 6f 72 79 0a 2a 2a 20 72 65 67 69 73 74 65 72  mory.** register
1dab0 20 50 32 2e 20 20 49 6e 20 6f 74 68 65 72 20 77   P2.  In other w
1dab1 6f 72 64 73 2c 20 63 75 72 73 6f 72 20 50 31 20  ords, cursor P1 
1dab2 62 65 63 6f 6d 65 73 20 61 6e 20 61 6c 69 61 73  becomes an alias
1dab3 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 4d 45 4d   for the .** MEM
1dab4 5f 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 20 63 6f  _Blob content co
1dab5 6e 74 61 69 6e 65 64 20 69 6e 20 72 65 67 69 73  ntained in regis
1dab6 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 41 20  ter P2..**.** A 
1dab7 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 63 72 65  pseudo-table cre
1dab8 61 74 65 64 20 62 79 20 74 68 69 73 20 6f 70 63  ated by this opc
1dab9 6f 64 65 20 69 73 20 75 73 65 64 20 74 6f 20 68  ode is used to h
1daba 6f 6c 64 20 74 68 65 20 61 20 73 69 6e 67 6c 65  old the a single
1dabb 0a 2a 2a 20 72 6f 77 20 6f 75 74 70 75 74 20 66  .** row output f
1dabc 72 6f 6d 20 74 68 65 20 73 6f 72 74 65 72 20 73  rom the sorter s
1dabd 6f 20 74 68 61 74 20 74 68 65 20 72 6f 77 20 63  o that the row c
1dabe 61 6e 20 62 65 20 64 65 63 6f 6d 70 6f 73 65 64  an be decomposed
1dabf 20 69 6e 74 6f 0a 2a 2a 20 69 6e 64 69 76 69 64   into.** individ
1dac0 75 61 6c 20 63 6f 6c 75 6d 6e 73 20 75 73 69 6e  ual columns usin
1dac1 67 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20  g the OP_Column 
1dac2 6f 70 63 6f 64 65 2e 20 20 54 68 65 20 4f 50 5f  opcode.  The OP_
1dac3 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 0a 2a 2a  Column opcode.**
1dac4 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 63 75 72   is the only cur
1dac5 73 6f 72 20 6f 70 63 6f 64 65 20 74 68 61 74 20  sor opcode that 
1dac6 77 6f 72 6b 73 20 77 69 74 68 20 61 20 70 73 65  works with a pse
1dac7 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  udo-table..**.**
1dac8 20 50 33 20 69 73 20 74 68 65 20 6e 75 6d 62 65   P3 is the numbe
1dac9 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74  r of fields in t
1daca 68 65 20 72 65 63 6f 72 64 73 20 74 68 61 74 20  he records that 
1dacb 77 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 20 62  will be stored b
1dacc 79 0a 2a 2a 20 74 68 65 20 70 73 65 75 64 6f 2d  y.** the pseudo-
1dacd 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f  table..*/.case O
1dace 50 5f 4f 70 65 6e 50 73 65 75 64 6f 3a 20 7b 0a  P_OpenPseudo: {.
1dacf 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1dad0 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1dad1 69 6e 74 6f 20 75 2e 61 79 20 2a 2f 0a 20 20 56  into u.ay */.  V
1dad2 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a  dbeCursor *pCx;.
1dad3 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1dad4 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1dad5 69 6e 74 6f 20 75 2e 61 79 20 2a 2f 0a 0a 20 20  into u.ay */..  
1dad6 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1dad7 3d 30 20 29 3b 0a 20 20 75 2e 61 79 2e 70 43 78  =0 );.  u.ay.pCx
1dad8 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f   = allocateCurso
1dad9 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f  r(p, pOp->p1, pO
1dada 70 2d 3e 70 33 2c 20 2d 31 2c 20 30 29 3b 0a 20  p->p3, -1, 0);. 
1dadb 20 69 66 28 20 75 2e 61 79 2e 70 43 78 3d 3d 30   if( u.ay.pCx==0
1dadc 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
1dadd 20 20 75 2e 61 79 2e 70 43 78 2d 3e 6e 75 6c 6c    u.ay.pCx->null
1dade 52 6f 77 20 3d 20 31 3b 0a 20 20 75 2e 61 79 2e  Row = 1;.  u.ay.
1dadf 70 43 78 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65  pCx->pseudoTable
1dae0 52 65 67 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20  Reg = pOp->p2;. 
1dae1 20 75 2e 61 79 2e 70 43 78 2d 3e 69 73 54 61 62   u.ay.pCx->isTab
1dae2 6c 65 20 3d 20 31 3b 0a 20 20 75 2e 61 79 2e 70  le = 1;.  u.ay.p
1dae3 43 78 2d 3e 69 73 49 6e 64 65 78 20 3d 20 30 3b  Cx->isIndex = 0;
1dae4 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1dae5 4f 70 63 6f 64 65 3a 20 43 6c 6f 73 65 20 50 31  Opcode: Close P1
1dae6 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43   * * * *.**.** C
1dae7 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 20 70 72  lose a cursor pr
1dae8 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e 65 64 20  eviously opened 
1dae9 61 73 20 50 31 2e 20 20 49 66 20 50 31 20 69 73  as P1.  If P1 is
1daea 20 6e 6f 74 0a 2a 2a 20 63 75 72 72 65 6e 74 6c   not.** currentl
1daeb 79 20 6f 70 65 6e 2c 20 74 68 69 73 20 69 6e 73  y open, this ins
1daec 74 72 75 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f  truction is a no
1daed 2d 6f 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  -op..*/.case OP_
1daee 43 6c 6f 73 65 3a 20 7b 0a 20 20 61 73 73 65 72  Close: {.  asser
1daef 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26  t( pOp->p1>=0 &&
1daf0 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72   pOp->p1<p->nCur
1daf1 73 6f 72 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  sor );.  sqlite3
1daf2 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 70  VdbeFreeCursor(p
1daf3 2c 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e  , p->apCsr[pOp->
1daf4 70 31 5d 29 3b 0a 20 20 70 2d 3e 61 70 43 73 72  p1]);.  p->apCsr
1daf5 5b 70 4f 70 2d 3e 70 31 5d 20 3d 20 30 3b 0a 20  [pOp->p1] = 0;. 
1daf6 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1daf7 63 6f 64 65 3a 20 53 65 65 6b 47 65 20 50 31 20  code: SeekGe P1 
1daf8 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a  P2 P3 P4 *.**.**
1daf9 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 65   If cursor P1 re
1dafa 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 74  fers to an SQL t
1dafb 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74 68 61  able (B-Tree tha
1dafc 74 20 75 73 65 73 20 69 6e 74 65 67 65 72 20 6b  t uses integer k
1dafd 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68  eys), .** use th
1dafe 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1daff 74 65 72 20 50 33 20 61 73 20 74 68 65 20 6b 65  ter P3 as the ke
1db00 79 2e 20 20 49 66 20 63 75 72 73 6f 72 20 50 31  y.  If cursor P1
1db01 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20 61   refers .** to a
1db02 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 74 68 65  n SQL index, the
1db03 6e 20 50 33 20 69 73 20 74 68 65 20 66 69 72 73  n P3 is the firs
1db04 74 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66  t in an array of
1db05 20 50 34 20 72 65 67 69 73 74 65 72 73 20 0a 2a   P4 registers .*
1db06 2a 20 74 68 61 74 20 61 72 65 20 75 73 65 64 20  * that are used 
1db07 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69  as an unpacked i
1db08 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a  ndex key. .**.**
1db09 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 73   Reposition curs
1db0a 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 20 69  or P1 so that  i
1db0b 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  t points to the 
1db0c 73 6d 61 6c 6c 65 73 74 20 65 6e 74 72 79 20 74  smallest entry t
1db0d 68 61 74 20 0a 2a 2a 20 69 73 20 67 72 65 61 74  hat .** is great
1db0e 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  er than or equal
1db0f 20 74 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75   to the key valu
1db10 65 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 20  e. If there are 
1db11 6e 6f 20 72 65 63 6f 72 64 73 20 0a 2a 2a 20 67  no records .** g
1db12 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65  reater than or e
1db13 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20  qual to the key 
1db14 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65  and P2 is not ze
1db15 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f  ro, then jump to
1db16 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61   P2..**.** See a
1db17 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46  lso: Found, NotF
1db18 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20  ound, Distinct, 
1db19 53 65 65 6b 4c 74 2c 20 53 65 65 6b 47 74 2c 20  SeekLt, SeekGt, 
1db1a 53 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63  SeekLe.*/./* Opc
1db1b 6f 64 65 3a 20 53 65 65 6b 47 74 20 50 31 20 50  ode: SeekGt P1 P
1db1c 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20  2 P3 P4 *.**.** 
1db1d 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66  If cursor P1 ref
1db1e 65 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 61  ers to an SQL ta
1db1f 62 6c 65 20 28 42 2d 54 72 65 65 20 74 68 61 74  ble (B-Tree that
1db20 20 75 73 65 73 20 69 6e 74 65 67 65 72 20 6b 65   uses integer ke
1db21 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65  ys), .** use the
1db22 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1db23 65 72 20 50 33 20 61 73 20 61 20 6b 65 79 2e 20  er P3 as a key. 
1db24 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66  If cursor P1 ref
1db25 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51  ers .** to an SQ
1db26 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 50 33  L index, then P3
1db27 20 69 73 20 74 68 65 20 66 69 72 73 74 20 69 6e   is the first in
1db28 20 61 6e 20 61 72 72 61 79 20 6f 66 20 50 34 20   an array of P4 
1db29 72 65 67 69 73 74 65 72 73 20 0a 2a 2a 20 74 68  registers .** th
1db2a 61 74 20 61 72 65 20 75 73 65 64 20 61 73 20 61  at are used as a
1db2b 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78  n unpacked index
1db2c 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70   key. .**.** Rep
1db2d 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f 72 20 50  osition cursor P
1db2e 31 20 73 6f 20 74 68 61 74 20 20 69 74 20 70 6f  1 so that  it po
1db2f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 6d 61 6c  ints to the smal
1db30 6c 65 73 74 20 65 6e 74 72 79 20 74 68 61 74 20  lest entry that 
1db31 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72 20 74  .** is greater t
1db32 68 61 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75  han the key valu
1db33 65 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 20  e. If there are 
1db34 6e 6f 20 72 65 63 6f 72 64 73 20 67 72 65 61 74  no records great
1db35 65 72 20 74 68 61 6e 20 0a 2a 2a 20 74 68 65 20  er than .** the 
1db36 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f  key and P2 is no
1db37 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d  t zero, then jum
1db38 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53  p to P2..**.** S
1db39 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20  ee also: Found, 
1db3a 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e  NotFound, Distin
1db3b 63 74 2c 20 53 65 65 6b 4c 74 2c 20 53 65 65 6b  ct, SeekLt, Seek
1db3c 47 65 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a  Ge, SeekLe.*/./*
1db3d 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 4c 74 20   Opcode: SeekLt 
1db3e 50 31 20 50 32 20 50 33 20 50 34 20 2a 20 0a 2a  P1 P2 P3 P4 * .*
1db3f 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50  *.** If cursor P
1db40 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53  1 refers to an S
1db41 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65  QL table (B-Tree
1db42 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67   that uses integ
1db43 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73  er keys), .** us
1db44 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  e the value in r
1db45 65 67 69 73 74 65 72 20 50 33 20 61 73 20 61 20  egister P3 as a 
1db46 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72 20 50  key. If cursor P
1db47 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20  1 refers .** to 
1db48 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 74 68  an SQL index, th
1db49 65 6e 20 50 33 20 69 73 20 74 68 65 20 66 69 72  en P3 is the fir
1db4a 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f  st in an array o
1db4b 66 20 50 34 20 72 65 67 69 73 74 65 72 73 20 0a  f P4 registers .
1db4c 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73 65 64  ** that are used
1db4d 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 64 20   as an unpacked 
1db4e 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a  index key. .**.*
1db4f 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 75 72  * Reposition cur
1db50 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 20  sor P1 so that  
1db51 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  it points to the
1db52 20 6c 61 72 67 65 73 74 20 65 6e 74 72 79 20 74   largest entry t
1db53 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65 73 73 20  hat .** is less 
1db54 74 68 61 6e 20 74 68 65 20 6b 65 79 20 76 61 6c  than the key val
1db55 75 65 2e 20 49 66 20 74 68 65 72 65 20 61 72 65  ue. If there are
1db56 20 6e 6f 20 72 65 63 6f 72 64 73 20 6c 65 73 73   no records less
1db57 20 74 68 61 6e 20 0a 2a 2a 20 74 68 65 20 6b 65   than .** the ke
1db58 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20  y and P2 is not 
1db59 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20  zero, then jump 
1db5a 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  to P2..**.** See
1db5b 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f   also: Found, No
1db5c 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74  tFound, Distinct
1db5d 2c 20 53 65 65 6b 47 74 2c 20 53 65 65 6b 47 65  , SeekGt, SeekGe
1db5e 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f  , SeekLe.*/./* O
1db5f 70 63 6f 64 65 3a 20 53 65 65 6b 4c 65 20 50 31  pcode: SeekLe P1
1db60 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a   P2 P3 P4 *.**.*
1db61 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72  * If cursor P1 r
1db62 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c 20  efers to an SQL 
1db63 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74 68  table (B-Tree th
1db64 61 74 20 75 73 65 73 20 69 6e 74 65 67 65 72 20  at uses integer 
1db65 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74  keys), .** use t
1db66 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1db67 73 74 65 72 20 50 33 20 61 73 20 61 20 6b 65 79  ster P3 as a key
1db68 2e 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72  . If cursor P1 r
1db69 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e 20  efers .** to an 
1db6a 53 51 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e 20  SQL index, then 
1db6b 50 33 20 69 73 20 74 68 65 20 66 69 72 73 74 20  P3 is the first 
1db6c 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 50  in an array of P
1db6d 34 20 72 65 67 69 73 74 65 72 73 20 0a 2a 2a 20  4 registers .** 
1db6e 74 68 61 74 20 61 72 65 20 75 73 65 64 20 61 73  that are used as
1db6f 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64   an unpacked ind
1db70 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52  ex key. .**.** R
1db71 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f 72  eposition cursor
1db72 20 50 31 20 73 6f 20 74 68 61 74 20 69 74 20 70   P1 so that it p
1db73 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6c 61 72  oints to the lar
1db74 67 65 73 74 20 65 6e 74 72 79 20 74 68 61 74 20  gest entry that 
1db75 0a 2a 2a 20 69 73 20 6c 65 73 73 20 74 68 61 6e  .** is less than
1db76 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65   or equal to the
1db77 20 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74   key value. If t
1db78 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f  here are no reco
1db79 72 64 73 20 0a 2a 2a 20 6c 65 73 73 20 74 68 61  rds .** less tha
1db7a 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68  n or equal to th
1db7b 65 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20  e key and P2 is 
1db7c 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a  not zero, then j
1db7d 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a  ump to P2..**.**
1db7e 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64   See also: Found
1db7f 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74  , NotFound, Dist
1db80 69 6e 63 74 2c 20 53 65 65 6b 47 74 2c 20 53 65  inct, SeekGt, Se
1db81 65 6b 47 65 2c 20 53 65 65 6b 4c 74 0a 2a 2f 0a  ekGe, SeekLt.*/.
1db82 63 61 73 65 20 4f 50 5f 53 65 65 6b 4c 74 3a 20  case OP_SeekLt: 
1db83 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c          /* jump,
1db84 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f   in3 */.case OP_
1db85 53 65 65 6b 4c 65 3a 20 20 20 20 20 20 20 20 20  SeekLe:         
1db86 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a  /* jump, in3 */.
1db87 63 61 73 65 20 4f 50 5f 53 65 65 6b 47 65 3a 20  case OP_SeekGe: 
1db88 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c          /* jump,
1db89 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f   in3 */.case OP_
1db8a 53 65 65 6b 47 74 3a 20 7b 20 20 20 20 20 20 20  SeekGt: {       
1db8b 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a  /* jump, in3 */.
1db8c 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1db8d 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1db8e 69 6e 74 6f 20 75 2e 61 7a 20 2a 2f 0a 20 20 69  into u.az */.  i
1db8f 6e 74 20 72 65 73 3b 0a 20 20 69 6e 74 20 6f 63  nt res;.  int oc
1db90 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  ;.  VdbeCursor *
1db91 70 43 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 65  pC;.  UnpackedRe
1db92 63 6f 72 64 20 72 3b 0a 20 20 69 6e 74 20 6e 46  cord r;.  int nF
1db93 69 65 6c 64 3b 0a 20 20 69 36 34 20 69 4b 65 79  ield;.  i64 iKey
1db94 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f  ;      /* The ro
1db95 77 69 64 20 77 65 20 61 72 65 20 74 6f 20 73 65  wid we are to se
1db96 65 6b 20 74 6f 20 2a 2f 0a 23 65 6e 64 69 66 20  ek to */.#endif 
1db97 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1db98 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1db99 61 7a 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  az */..  assert(
1db9a 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70   pOp->p1>=0 && p
1db9b 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f  Op->p1<p->nCurso
1db9c 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  r );.  assert( p
1db9d 4f 70 2d 3e 70 32 21 3d 30 20 29 3b 0a 20 20 75  Op->p2!=0 );.  u
1db9e 2e 61 7a 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73  .az.pC = p->apCs
1db9f 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73  r[pOp->p1];.  as
1dba0 73 65 72 74 28 20 75 2e 61 7a 2e 70 43 21 3d 30  sert( u.az.pC!=0
1dba1 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e   );.  assert( u.
1dba2 61 7a 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62  az.pC->pseudoTab
1dba3 6c 65 52 65 67 3d 3d 30 20 29 3b 0a 20 20 69 66  leReg==0 );.  if
1dba4 28 20 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73  ( u.az.pC->pCurs
1dba5 6f 72 21 3d 30 20 29 7b 0a 20 20 20 20 75 2e 61  or!=0 ){.    u.a
1dba6 7a 2e 6f 63 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f  z.oc = pOp->opco
1dba7 64 65 3b 0a 20 20 20 20 75 2e 61 7a 2e 70 43 2d  de;.    u.az.pC-
1dba8 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20  >nullRow = 0;.  
1dba9 20 20 69 66 28 20 75 2e 61 7a 2e 70 43 2d 3e 69    if( u.az.pC->i
1dbaa 73 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20  sTable ){.      
1dbab 2f 2a 20 54 68 65 20 69 6e 70 75 74 20 76 61 6c  /* The input val
1dbac 75 65 20 69 6e 20 50 33 20 6d 69 67 68 74 20 62  ue in P3 might b
1dbad 65 20 6f 66 20 61 6e 79 20 74 79 70 65 3a 20 69  e of any type: i
1dbae 6e 74 65 67 65 72 2c 20 72 65 61 6c 2c 20 73 74  nteger, real, st
1dbaf 72 69 6e 67 2c 0a 20 20 20 20 20 20 2a 2a 20 62  ring,.      ** b
1dbb0 6c 6f 62 2c 20 6f 72 20 4e 55 4c 4c 2e 20 20 42  lob, or NULL.  B
1dbb1 75 74 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62  ut it needs to b
1dbb2 65 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65 66  e an integer bef
1dbb3 6f 72 65 20 77 65 20 63 61 6e 20 64 6f 0a 20 20  ore we can do.  
1dbb4 20 20 20 20 2a 2a 20 74 68 65 20 73 65 65 6b 2c      ** the seek,
1dbb5 20 73 6f 20 63 6f 76 65 72 74 20 69 74 2e 20 2a   so covert it. *
1dbb6 2f 0a 20 20 20 20 20 20 61 70 70 6c 79 4e 75 6d  /.      applyNum
1dbb7 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 49 6e  ericAffinity(pIn
1dbb8 33 29 3b 0a 20 20 20 20 20 20 75 2e 61 7a 2e 69  3);.      u.az.i
1dbb9 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62  Key = sqlite3Vdb
1dbba 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 33 29 3b  eIntValue(pIn3);
1dbbb 0a 20 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e  .      u.az.pC->
1dbbc 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30  rowidIsValid = 0
1dbbd 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74  ;..      /* If t
1dbbe 68 65 20 50 33 20 76 61 6c 75 65 20 63 6f 75 6c  he P3 value coul
1dbbf 64 20 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 74  d not be convert
1dbc0 65 64 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67  ed into an integ
1dbc1 65 72 20 77 69 74 68 6f 75 74 0a 20 20 20 20 20  er without.     
1dbc2 20 2a 2a 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f   ** loss of info
1dbc3 72 6d 61 74 69 6f 6e 2c 20 74 68 65 6e 20 73 70  rmation, then sp
1dbc4 65 63 69 61 6c 20 70 72 6f 63 65 73 73 69 6e 67  ecial processing
1dbc5 20 69 73 20 72 65 71 75 69 72 65 64 2e 2e 2e 20   is required... 
1dbc6 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 28 70 49  */.      if( (pI
1dbc7 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n3->flags & MEM_
1dbc8 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Int)==0 ){.     
1dbc9 20 20 20 69 66 28 20 28 70 49 6e 33 2d 3e 66 6c     if( (pIn3->fl
1dbca 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29 3d  ags & MEM_Real)=
1dbcb 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
1dbcc 2f 2a 20 49 66 20 74 68 65 20 50 33 20 76 61 6c  /* If the P3 val
1dbcd 75 65 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e  ue cannot be con
1dbce 76 65 72 74 65 64 20 69 6e 74 6f 20 61 6e 79 20  verted into any 
1dbcf 6b 69 6e 64 20 6f 66 20 61 20 6e 75 6d 62 65 72  kind of a number
1dbd0 2c 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74  ,.          ** t
1dbd1 68 65 6e 20 74 68 65 20 73 65 65 6b 20 69 73 20  hen the seek is 
1dbd2 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2c 20 73 6f  not possible, so
1dbd3 20 6a 75 6d 70 20 74 6f 20 50 32 20 2a 2f 0a 20   jump to P2 */. 
1dbd4 20 20 20 20 20 20 20 20 20 70 63 20 3d 20 70 4f           pc = pO
1dbd5 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20  p->p2 - 1;.     
1dbd6 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1dbd7 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a      }.        /*
1dbd8 20 49 66 20 77 65 20 72 65 61 63 68 20 74 68 69   If we reach thi
1dbd9 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 74 68  s point, then th
1dbda 65 20 50 33 20 76 61 6c 75 65 20 6d 75 73 74 20  e P3 value must 
1dbdb 62 65 20 61 20 66 6c 6f 61 74 69 6e 67 0a 20 20  be a floating.  
1dbdc 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 20 6e        ** point n
1dbdd 75 6d 62 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20  umber. */.      
1dbde 20 20 61 73 73 65 72 74 28 20 28 70 49 6e 33 2d    assert( (pIn3-
1dbdf 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61  >flags & MEM_Rea
1dbe0 6c 29 21 3d 30 20 29 3b 0a 0a 20 20 20 20 20 20  l)!=0 );..      
1dbe1 20 20 69 66 28 20 75 2e 61 7a 2e 69 4b 65 79 3d    if( u.az.iKey=
1dbe2 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20  =SMALLEST_INT64 
1dbe3 26 26 20 28 70 49 6e 33 2d 3e 72 3c 28 64 6f 75  && (pIn3->r<(dou
1dbe4 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 79 20 7c 7c  ble)u.az.iKey ||
1dbe5 20 70 49 6e 33 2d 3e 72 3e 30 29 20 29 7b 0a 20   pIn3->r>0) ){. 
1dbe6 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1dbe7 50 33 20 76 61 6c 75 65 20 69 73 20 74 6f 6f 20  P3 value is too 
1dbe8 6c 61 72 67 65 20 69 6e 20 6d 61 67 6e 69 74 75  large in magnitu
1dbe9 64 65 20 74 6f 20 62 65 20 65 78 70 72 65 73 73  de to be express
1dbea 65 64 20 61 73 20 61 6e 0a 20 20 20 20 20 20 20  ed as an.       
1dbeb 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20 2a     ** integer. *
1dbec 2f 0a 20 20 20 20 20 20 20 20 20 20 75 2e 61 7a  /.          u.az
1dbed 2e 72 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20  .res = 1;.      
1dbee 20 20 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 3c      if( pIn3->r<
1dbef 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
1dbf0 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50   if( u.az.oc==OP
1dbf1 5f 53 65 65 6b 47 74 20 7c 7c 20 75 2e 61 7a 2e  _SeekGt || u.az.
1dbf2 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 29 7b  oc==OP_SeekGe ){
1dbf3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72  .              r
1dbf4 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1dbf5 46 69 72 73 74 28 75 2e 61 7a 2e 70 43 2d 3e 70  First(u.az.pC->p
1dbf6 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a 2e 72 65  Cursor, &u.az.re
1dbf7 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  s);.            
1dbf8 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1dbf9 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  _OK ) goto abort
1dbfa 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20  _due_to_error;. 
1dbfb 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
1dbfc 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1dbfd 20 20 20 20 20 20 20 20 20 20 69 66 28 20 75 2e            if( u.
1dbfe 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74  az.oc==OP_SeekLt
1dbff 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f   || u.az.oc==OP_
1dc00 53 65 65 6b 4c 65 20 29 7b 0a 20 20 20 20 20 20  SeekLe ){.      
1dc01 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1dc02 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 75 2e  ite3BtreeLast(u.
1dc03 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20  az.pC->pCursor, 
1dc04 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20  &u.az.res);.    
1dc05 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63            if( rc
1dc06 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
1dc07 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1dc08 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 20  error;.         
1dc09 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d     }.          }
1dc0a 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 75  .          if( u
1dc0b 2e 61 7a 2e 72 65 73 20 29 7b 0a 20 20 20 20 20  .az.res ){.     
1dc0c 20 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d         pc = pOp-
1dc0d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20  >p2 - 1;.       
1dc0e 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62     }.          b
1dc0f 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65  reak;.        }e
1dc10 6c 73 65 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3d  lse if( u.az.oc=
1dc11 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 75 2e  =OP_SeekLt || u.
1dc12 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65  az.oc==OP_SeekGe
1dc13 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
1dc14 20 55 73 65 20 74 68 65 20 63 65 69 6c 69 6e 67   Use the ceiling
1dc15 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63  () function to c
1dc16 6f 6e 76 65 72 74 20 72 65 61 6c 2d 3e 69 6e 74  onvert real->int
1dc17 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66   */.          if
1dc18 28 20 70 49 6e 33 2d 3e 72 20 3e 20 28 64 6f 75  ( pIn3->r > (dou
1dc19 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 79 20 29 20  ble)u.az.iKey ) 
1dc1a 75 2e 61 7a 2e 69 4b 65 79 2b 2b 3b 0a 20 20 20  u.az.iKey++;.   
1dc1b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1dc1c 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65        /* Use the
1dc1d 20 66 6c 6f 6f 72 28 29 20 66 75 6e 63 74 69 6f   floor() functio
1dc1e 6e 20 74 6f 20 63 6f 6e 76 65 72 74 20 72 65 61  n to convert rea
1dc1f 6c 2d 3e 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20  l->int */.      
1dc20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a      assert( u.az
1dc21 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 7c  .oc==OP_SeekLe |
1dc22 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65  | u.az.oc==OP_Se
1dc23 65 6b 47 74 20 29 3b 0a 20 20 20 20 20 20 20 20  ekGt );.        
1dc24 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 20 3c 20    if( pIn3->r < 
1dc25 28 64 6f 75 62 6c 65 29 75 2e 61 7a 2e 69 4b 65  (double)u.az.iKe
1dc26 79 20 29 20 75 2e 61 7a 2e 69 4b 65 79 2d 2d 3b  y ) u.az.iKey--;
1dc27 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1dc28 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71   }.      rc = sq
1dc29 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f  lite3BtreeMoveto
1dc2a 55 6e 70 61 63 6b 65 64 28 75 2e 61 7a 2e 70 43  Unpacked(u.az.pC
1dc2b 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 75  ->pCursor, 0, (u
1dc2c 36 34 29 75 2e 61 7a 2e 69 4b 65 79 2c 20 30 2c  64)u.az.iKey, 0,
1dc2d 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20   &u.az.res);.   
1dc2e 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1dc2f 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1dc30 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1dc31 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d  o_error;.      }
1dc32 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e  .      if( u.az.
1dc33 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  res==0 ){.      
1dc34 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69 64    u.az.pC->rowid
1dc35 49 73 56 61 6c 69 64 20 3d 20 31 3b 0a 20 20 20  IsValid = 1;.   
1dc36 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 6c 61       u.az.pC->la
1dc37 73 74 52 6f 77 69 64 20 3d 20 75 2e 61 7a 2e 69  stRowid = u.az.i
1dc38 4b 65 79 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  Key;.      }.   
1dc39 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 2e   }else{.      u.
1dc3a 61 7a 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d  az.nField = pOp-
1dc3b 3e 70 34 2e 69 3b 0a 20 20 20 20 20 20 61 73 73  >p4.i;.      ass
1dc3c 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65  ert( pOp->p4type
1dc3d 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20  ==P4_INT32 );.  
1dc3e 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a      assert( u.az
1dc3f 2e 6e 46 69 65 6c 64 3e 30 20 29 3b 0a 20 20 20  .nField>0 );.   
1dc40 20 20 20 75 2e 61 7a 2e 72 2e 70 4b 65 79 49 6e     u.az.r.pKeyIn
1dc41 66 6f 20 3d 20 75 2e 61 7a 2e 70 43 2d 3e 70 4b  fo = u.az.pC->pK
1dc42 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 75 2e  eyInfo;.      u.
1dc43 61 7a 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 28 75  az.r.nField = (u
1dc44 31 36 29 75 2e 61 7a 2e 6e 46 69 65 6c 64 3b 0a  16)u.az.nField;.
1dc45 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 6f        if( u.az.o
1dc46 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 7c 7c 20  c==OP_SeekGt || 
1dc47 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b  u.az.oc==OP_Seek
1dc48 4c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e  Le ){.        u.
1dc49 61 7a 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50  az.r.flags = UNP
1dc4a 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 3b 0a 20  ACKED_INCRKEY;. 
1dc4b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1dc4c 20 20 20 20 75 2e 61 7a 2e 72 2e 66 6c 61 67 73      u.az.r.flags
1dc4d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
1dc4e 20 20 20 20 75 2e 61 7a 2e 72 2e 61 4d 65 6d 20      u.az.r.aMem 
1dc4f 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
1dc50 70 33 5d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  p3];.      rc = 
1dc51 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65  sqlite3BtreeMove
1dc52 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 61 7a 2e  toUnpacked(u.az.
1dc53 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e  pC->pCursor, &u.
1dc54 61 7a 2e 72 2c 20 30 2c 20 30 2c 20 26 75 2e 61  az.r, 0, 0, &u.a
1dc55 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 69 66  z.res);.      if
1dc56 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1dc57 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  ){.        goto 
1dc58 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
1dc59 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  or;.      }.    
1dc5a 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69 64    u.az.pC->rowid
1dc5b 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20  IsValid = 0;.   
1dc5c 20 7d 0a 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e   }.    u.az.pC->
1dc5d 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d  deferredMoveto =
1dc5e 20 30 3b 0a 20 20 20 20 75 2e 61 7a 2e 70 43 2d   0;.    u.az.pC-
1dc5f 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43  >cacheStatus = C
1dc60 41 43 48 45 5f 53 54 41 4c 45 3b 0a 23 69 66 64  ACHE_STALE;.#ifd
1dc61 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
1dc62 20 20 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63     sqlite3_searc
1dc63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69  h_count++;.#endi
1dc64 66 0a 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 6f  f.    if( u.az.o
1dc65 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 7c 7c 20  c==OP_SeekGe || 
1dc66 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b  u.az.oc==OP_Seek
1dc67 47 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  Gt ){.      if( 
1dc68 75 2e 61 7a 2e 72 65 73 3c 30 20 7c 7c 20 28 75  u.az.res<0 || (u
1dc69 2e 61 7a 2e 72 65 73 3d 3d 30 20 26 26 20 75 2e  .az.res==0 && u.
1dc6a 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74  az.oc==OP_SeekGt
1dc6b 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  ) ){.        rc 
1dc6c 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65  = sqlite3BtreeNe
1dc6d 78 74 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72  xt(u.az.pC->pCur
1dc6e 73 6f 72 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b  sor, &u.az.res);
1dc6f 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
1dc70 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
1dc71 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
1dc72 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 75 2e  rror;.        u.
1dc73 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61  az.pC->rowidIsVa
1dc74 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  lid = 0;.      }
1dc75 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 75 2e  else{.        u.
1dc76 61 7a 2e 72 65 73 20 3d 20 30 3b 0a 20 20 20 20  az.res = 0;.    
1dc77 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
1dc78 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61       assert( u.a
1dc79 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20  z.oc==OP_SeekLt 
1dc7a 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53  || u.az.oc==OP_S
1dc7b 65 65 6b 4c 65 20 29 3b 0a 20 20 20 20 20 20 69  eekLe );.      i
1dc7c 66 28 20 75 2e 61 7a 2e 72 65 73 3e 30 20 7c 7c  f( u.az.res>0 ||
1dc7d 20 28 75 2e 61 7a 2e 72 65 73 3d 3d 30 20 26 26   (u.az.res==0 &&
1dc7e 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65   u.az.oc==OP_See
1dc7f 6b 4c 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20  kLt) ){.        
1dc80 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1dc81 65 50 72 65 76 69 6f 75 73 28 75 2e 61 7a 2e 70  ePrevious(u.az.p
1dc82 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 61  C->pCursor, &u.a
1dc83 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20  z.res);.        
1dc84 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1dc85 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  K ) goto abort_d
1dc86 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
1dc87 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f       u.az.pC->ro
1dc88 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a  widIsValid = 0;.
1dc89 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1dc8a 20 20 20 20 20 2f 2a 20 75 2e 61 7a 2e 72 65 73       /* u.az.res
1dc8b 20 6d 69 67 68 74 20 62 65 20 6e 65 67 61 74 69   might be negati
1dc8c 76 65 20 62 65 63 61 75 73 65 20 74 68 65 20 74  ve because the t
1dc8d 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 20 20  able is empty.  
1dc8e 43 68 65 63 6b 20 74 6f 0a 20 20 20 20 20 20 20  Check to.       
1dc8f 20 2a 2a 20 73 65 65 20 69 66 20 74 68 69 73 20   ** see if this 
1dc90 69 73 20 74 68 65 20 63 61 73 65 2e 0a 20 20 20  is the case..   
1dc91 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
1dc92 75 2e 61 7a 2e 72 65 73 20 3d 20 73 71 6c 69 74  u.az.res = sqlit
1dc93 65 33 42 74 72 65 65 45 6f 66 28 75 2e 61 7a 2e  e3BtreeEof(u.az.
1dc94 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20  pC->pCursor);.  
1dc95 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1dc96 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e  assert( pOp->p2>
1dc97 30 20 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 61  0 );.    if( u.a
1dc98 7a 2e 72 65 73 20 29 7b 0a 20 20 20 20 20 20 70  z.res ){.      p
1dc99 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1dc9a 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
1dc9b 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70      /* This happ
1dc9c 65 6e 73 20 77 68 65 6e 20 61 74 74 65 6d 70 74  ens when attempt
1dc9d 69 6e 67 20 74 6f 20 6f 70 65 6e 20 74 68 65 20  ing to open the 
1dc9e 73 71 6c 69 74 65 33 5f 6d 61 73 74 65 72 20 74  sqlite3_master t
1dc9f 61 62 6c 65 0a 20 20 20 20 2a 2a 20 66 6f 72 20  able.    ** for 
1dca0 72 65 61 64 20 61 63 63 65 73 73 20 72 65 74 75  read access retu
1dca1 72 6e 73 20 53 51 4c 49 54 45 5f 45 4d 50 54 59  rns SQLITE_EMPTY
1dca2 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 61  . In this case a
1dca3 6c 77 61 79 73 0a 20 20 20 20 2a 2a 20 74 61 6b  lways.    ** tak
1dca4 65 20 74 68 65 20 6a 75 6d 70 20 28 73 69 6e 63  e the jump (sinc
1dca5 65 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72  e there are no r
1dca6 65 63 6f 72 64 73 20 69 6e 20 74 68 65 20 74 61  ecords in the ta
1dca7 62 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ble)..    */.   
1dca8 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1dca9 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  1;.  }.  break;.
1dcaa 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65  }../* Opcode: Se
1dcab 65 6b 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  ek P1 P2 * * *.*
1dcac 2a 0a 2a 2a 20 50 31 20 69 73 20 61 6e 20 6f 70  *.** P1 is an op
1dcad 65 6e 20 74 61 62 6c 65 20 63 75 72 73 6f 72 20  en table cursor 
1dcae 61 6e 64 20 50 32 20 69 73 20 61 20 72 6f 77 69  and P2 is a rowi
1dcaf 64 20 69 6e 74 65 67 65 72 2e 20 20 41 72 72 61  d integer.  Arra
1dcb0 6e 67 65 0a 2a 2a 20 66 6f 72 20 50 31 20 74 6f  nge.** for P1 to
1dcb1 20 6d 6f 76 65 20 73 6f 20 74 68 61 74 20 69 74   move so that it
1dcb2 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 72   points to the r
1dcb3 6f 77 69 64 20 67 69 76 65 6e 20 62 79 20 50 32  owid given by P2
1dcb4 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ..**.** This is 
1dcb5 61 63 74 75 61 6c 6c 79 20 61 20 64 65 66 65 72  actually a defer
1dcb6 72 65 64 20 73 65 65 6b 2e 20 20 4e 6f 74 68 69  red seek.  Nothi
1dcb7 6e 67 20 61 63 74 75 61 6c 6c 79 20 68 61 70 70  ng actually happ
1dcb8 65 6e 73 20 75 6e 74 69 6c 0a 2a 2a 20 74 68 65  ens until.** the
1dcb9 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 64 20   cursor is used 
1dcba 74 6f 20 72 65 61 64 20 61 20 72 65 63 6f 72 64  to read a record
1dcbb 2e 20 20 54 68 61 74 20 77 61 79 2c 20 69 66 20  .  That way, if 
1dcbc 6e 6f 20 72 65 61 64 73 0a 2a 2a 20 6f 63 63 75  no reads.** occu
1dcbd 72 2c 20 6e 6f 20 75 6e 6e 65 63 65 73 73 61 72  r, no unnecessar
1dcbe 79 20 49 2f 4f 20 68 61 70 70 65 6e 73 2e 0a 2a  y I/O happens..*
1dcbf 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 6b 3a 20  /.case OP_Seek: 
1dcc0 7b 20 20 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a 23  {    /* in2 */.#
1dcc1 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1dcc2 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1dcc3 6e 74 6f 20 75 2e 62 61 20 2a 2f 0a 20 20 56 64  nto u.ba */.  Vd
1dcc4 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 23 65  beCursor *pC;.#e
1dcc5 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1dcc6 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1dcc7 74 6f 20 75 2e 62 61 20 2a 2f 0a 0a 20 20 61 73  to u.ba */..  as
1dcc8 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
1dcc9 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e   && pOp->p1<p->n
1dcca 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 61  Cursor );.  u.ba
1dccb 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  .pC = p->apCsr[p
1dccc 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
1dccd 74 28 20 75 2e 62 61 2e 70 43 21 3d 30 20 29 3b  t( u.ba.pC!=0 );
1dcce 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e  .  if( ALWAYS(u.
1dccf 62 61 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d  ba.pC->pCursor!=
1dcd0 30 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  0) ){.    assert
1dcd1 28 20 75 2e 62 61 2e 70 43 2d 3e 69 73 54 61 62  ( u.ba.pC->isTab
1dcd2 6c 65 20 29 3b 0a 20 20 20 20 75 2e 62 61 2e 70  le );.    u.ba.p
1dcd3 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a  C->nullRow = 0;.
1dcd4 20 20 20 20 75 2e 62 61 2e 70 43 2d 3e 6d 6f 76      u.ba.pC->mov
1dcd5 65 74 6f 54 61 72 67 65 74 20 3d 20 73 71 6c 69  etoTarget = sqli
1dcd6 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28  te3VdbeIntValue(
1dcd7 70 49 6e 32 29 3b 0a 20 20 20 20 75 2e 62 61 2e  pIn2);.    u.ba.
1dcd8 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64  pC->rowidIsValid
1dcd9 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 61 2e 70   = 0;.    u.ba.p
1dcda 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  C->deferredMovet
1dcdb 6f 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65  o = 1;.  }.  bre
1dcdc 61 6b 3b 0a 7d 0a 20 20 0a 0a 2f 2a 20 4f 70 63  ak;.}.  ../* Opc
1dcdd 6f 64 65 3a 20 46 6f 75 6e 64 20 50 31 20 50 32  ode: Found P1 P2
1dcde 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65   P3 * *.**.** Re
1dcdf 67 69 73 74 65 72 20 50 33 20 68 6f 6c 64 73 20  gister P3 holds 
1dce0 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74  a blob construct
1dce1 65 64 20 62 79 20 4d 61 6b 65 52 65 63 6f 72 64  ed by MakeRecord
1dce2 2e 20 20 50 31 20 69 73 20 61 6e 20 69 6e 64 65  .  P1 is an inde
1dce3 78 2e 0a 2a 2a 20 49 66 20 61 6e 20 65 6e 74 72  x..** If an entr
1dce4 79 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 74  y that matches t
1dce5 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1dce6 73 74 65 72 20 70 33 20 65 78 69 73 74 73 20 69  ster p3 exists i
1dce7 6e 20 50 31 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d  n P1 then.** jum
1dce8 70 20 74 6f 20 50 32 2e 20 20 49 66 20 74 68 65  p to P2.  If the
1dce9 20 50 33 20 76 61 6c 75 65 20 64 6f 65 73 20 6e   P3 value does n
1dcea 6f 74 20 6d 61 74 63 68 20 61 6e 79 20 65 6e 74  ot match any ent
1dceb 72 79 20 69 6e 20 50 31 0a 2a 2a 20 74 68 65 6e  ry in P1.** then
1dcec 20 66 61 6c 6c 20 74 68 72 75 2e 20 20 54 68 65   fall thru.  The
1dced 20 50 31 20 63 75 72 73 6f 72 20 69 73 20 6c 65   P1 cursor is le
1dcee 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74  ft pointing at t
1dcef 68 65 20 6d 61 74 63 68 69 6e 67 20 65 6e 74 72  he matching entr
1dcf0 79 0a 2a 2a 20 69 66 20 69 74 20 65 78 69 73 74  y.** if it exist
1dcf1 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  s..**.** This in
1dcf2 73 74 72 75 63 74 69 6f 6e 20 69 73 20 75 73 65  struction is use
1dcf3 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  d to implement t
1dcf4 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 77  he IN operator w
1dcf5 68 65 72 65 20 74 68 65 0a 2a 2a 20 6c 65 66 74  here the.** left
1dcf6 2d 68 61 6e 64 20 73 69 64 65 20 69 73 20 61 20  -hand side is a 
1dcf7 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
1dcf8 2e 20 20 50 31 20 6d 61 79 20 62 65 20 61 20 74  .  P1 may be a t
1dcf9 72 75 65 20 69 6e 64 65 78 2c 20 6f 72 20 69 74  rue index, or it
1dcfa 0a 2a 2a 20 6d 61 79 20 62 65 20 61 20 74 65 6d  .** may be a tem
1dcfb 70 6f 72 61 72 79 20 69 6e 64 65 78 20 74 68 61  porary index tha
1dcfc 74 20 68 6f 6c 64 73 20 74 68 65 20 72 65 73 75  t holds the resu
1dcfd 6c 74 73 20 6f 66 20 74 68 65 20 53 45 4c 45 43  lts of the SELEC
1dcfe 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20  T.** statement. 
1dcff 20 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69    This instructi
1dd00 6f 6e 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20  on is also used 
1dd01 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
1dd02 0a 2a 2a 20 44 49 53 54 49 4e 43 54 20 6b 65 79  .** DISTINCT key
1dd03 77 6f 72 64 20 69 6e 20 53 45 4c 45 43 54 20 73  word in SELECT s
1dd04 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a  tatements..**.**
1dd05 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   This instructio
1dd06 6e 20 63 68 65 63 6b 73 20 69 66 20 69 6e 64 65  n checks if inde
1dd07 78 20 50 31 20 63 6f 6e 74 61 69 6e 73 20 61 20  x P1 contains a 
1dd08 72 65 63 6f 72 64 20 66 6f 72 20 77 68 69 63 68  record for which
1dd09 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 4e   .** the first N
1dd0a 20 73 65 72 69 61 6c 69 7a 65 64 20 76 61 6c 75   serialized valu
1dd0b 65 73 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68  es exactly match
1dd0c 20 74 68 65 20 4e 20 73 65 72 69 61 6c 69 7a 65   the N serialize
1dd0d 64 20 76 61 6c 75 65 73 0a 2a 2a 20 69 6e 20 74  d values.** in t
1dd0e 68 65 20 72 65 63 6f 72 64 20 69 6e 20 72 65 67  he record in reg
1dd0f 69 73 74 65 72 20 50 33 2c 20 77 68 65 72 65 20  ister P3, where 
1dd10 4e 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e  N is the total n
1dd11 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20  umber of values 
1dd12 69 6e 0a 2a 2a 20 74 68 65 20 50 33 20 72 65 63  in.** the P3 rec
1dd13 6f 72 64 20 28 74 68 65 20 50 33 20 72 65 63 6f  ord (the P3 reco
1dd14 72 64 20 69 73 20 61 20 70 72 65 66 69 78 20 6f  rd is a prefix o
1dd15 66 20 74 68 65 20 50 31 20 72 65 63 6f 72 64 29  f the P1 record)
1dd16 2e 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  . .**.** See als
1dd17 6f 3a 20 4e 6f 74 46 6f 75 6e 64 2c 20 49 73 55  o: NotFound, IsU
1dd18 6e 69 71 75 65 2c 20 4e 6f 74 45 78 69 73 74 73  nique, NotExists
1dd19 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e  .*/./* Opcode: N
1dd1a 6f 74 46 6f 75 6e 64 20 50 31 20 50 32 20 50 33  otFound P1 P2 P3
1dd1b 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73   * *.**.** Regis
1dd1c 74 65 72 20 50 33 20 68 6f 6c 64 73 20 61 20 62  ter P3 holds a b
1dd1d 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 64 20  lob constructed 
1dd1e 62 79 20 4d 61 6b 65 52 65 63 6f 72 64 2e 20 20  by MakeRecord.  
1dd1f 50 31 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 64 65  P1 is.** an inde
1dd20 78 2e 20 20 49 66 20 6e 6f 20 65 6e 74 72 79 20  x.  If no entry 
1dd21 65 78 69 73 74 73 20 69 6e 20 50 31 20 74 68 61  exists in P1 tha
1dd22 74 20 6d 61 74 63 68 65 73 20 74 68 65 20 62 6c  t matches the bl
1dd23 6f 62 20 74 68 65 6e 20 6a 75 6d 70 0a 2a 2a 20  ob then jump.** 
1dd24 74 6f 20 50 32 2e 20 20 49 66 20 61 6e 20 65 6e  to P2.  If an en
1dd25 74 72 79 20 64 6f 65 73 20 65 78 69 73 74 69 6e  try does existin
1dd26 67 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 2e  g, fall through.
1dd27 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20    The cursor is 
1dd28 6c 65 66 74 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67  left.** pointing
1dd29 20 74 6f 20 74 68 65 20 65 6e 74 72 79 20 74 68   to the entry th
1dd2a 61 74 20 6d 61 74 63 68 65 73 2e 0a 2a 2a 0a 2a  at matches..**.*
1dd2b 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e  * See also: Foun
1dd2c 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20 49 73  d, NotExists, Is
1dd2d 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 73 65 20 4f  Unique.*/.case O
1dd2e 50 5f 4e 6f 74 46 6f 75 6e 64 3a 20 20 20 20 20  P_NotFound:     
1dd2f 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a    /* jump, in3 *
1dd30 2f 0a 63 61 73 65 20 4f 50 5f 46 6f 75 6e 64 3a  /.case OP_Found:
1dd31 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d   {        /* jum
1dd32 70 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20  p, in3 */.#if 0 
1dd33 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1dd34 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1dd35 2e 62 62 20 2a 2f 0a 20 20 69 6e 74 20 61 6c 72  .bb */.  int alr
1dd36 65 61 64 79 45 78 69 73 74 73 3b 0a 20 20 56 64  eadyExists;.  Vd
1dd37 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20  beCursor *pC;.  
1dd38 69 6e 74 20 72 65 73 3b 0a 20 20 55 6e 70 61 63  int res;.  Unpac
1dd39 6b 65 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b  kedRecord *pIdxK
1dd3a 65 79 3b 0a 20 20 63 68 61 72 20 61 54 65 6d 70  ey;.  char aTemp
1dd3b 52 65 63 5b 52 4f 55 4e 44 38 28 73 69 7a 65 6f  Rec[ROUND8(sizeo
1dd3c 66 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64  f(UnpackedRecord
1dd3d 29 29 20 2b 20 73 69 7a 65 6f 66 28 4d 65 6d 29  )) + sizeof(Mem)
1dd3e 2a 33 20 2b 20 37 5d 3b 0a 23 65 6e 64 69 66 20  *3 + 7];.#endif 
1dd3f 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1dd40 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1dd41 62 62 20 2a 2f 0a 0a 20 20 75 2e 62 62 2e 61 6c  bb */..  u.bb.al
1dd42 72 65 61 64 79 45 78 69 73 74 73 20 3d 20 30 3b  readyExists = 0;
1dd43 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1dd44 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31  p1>=0 && pOp->p1
1dd45 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1dd46 20 75 2e 62 62 2e 70 43 20 3d 20 70 2d 3e 61 70   u.bb.pC = p->ap
1dd47 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Csr[pOp->p1];.  
1dd48 61 73 73 65 72 74 28 20 75 2e 62 62 2e 70 43 21  assert( u.bb.pC!
1dd49 3d 30 20 29 3b 0a 20 20 69 66 28 20 41 4c 57 41  =0 );.  if( ALWA
1dd4a 59 53 28 75 2e 62 62 2e 70 43 2d 3e 70 43 75 72  YS(u.bb.pC->pCur
1dd4b 73 6f 72 21 3d 30 29 20 29 7b 0a 0a 20 20 20 20  sor!=0) ){..    
1dd4c 61 73 73 65 72 74 28 20 75 2e 62 62 2e 70 43 2d  assert( u.bb.pC-
1dd4d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20  >isTable==0 );. 
1dd4e 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d     assert( pIn3-
1dd4f 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f  >flags & MEM_Blo
1dd50 62 20 29 3b 0a 20 20 20 20 45 78 70 61 6e 64 42  b );.    ExpandB
1dd51 6c 6f 62 28 70 49 6e 33 29 3b 0a 20 20 20 20 75  lob(pIn3);.    u
1dd52 2e 62 62 2e 70 49 64 78 4b 65 79 20 3d 20 73 71  .bb.pIdxKey = sq
1dd53 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 55  lite3VdbeRecordU
1dd54 6e 70 61 63 6b 28 75 2e 62 62 2e 70 43 2d 3e 70  npack(u.bb.pC->p
1dd55 4b 65 79 49 6e 66 6f 2c 20 70 49 6e 33 2d 3e 6e  KeyInfo, pIn3->n
1dd56 2c 20 70 49 6e 33 2d 3e 7a 2c 0a 20 20 20 20 20  , pIn3->z,.     
1dd57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dd58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dd59 20 75 2e 62 62 2e 61 54 65 6d 70 52 65 63 2c 20   u.bb.aTempRec, 
1dd5a 73 69 7a 65 6f 66 28 75 2e 62 62 2e 61 54 65 6d  sizeof(u.bb.aTem
1dd5b 70 52 65 63 29 29 3b 0a 20 20 20 20 69 66 28 20  pRec));.    if( 
1dd5c 75 2e 62 62 2e 70 49 64 78 4b 65 79 3d 3d 30 20  u.bb.pIdxKey==0 
1dd5d 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f  ){.      goto no
1dd5e 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _mem;.    }.    
1dd5f 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
1dd60 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20 20  =OP_Found ){.   
1dd61 20 20 20 75 2e 62 62 2e 70 49 64 78 4b 65 79 2d     u.bb.pIdxKey-
1dd62 3e 66 6c 61 67 73 20 7c 3d 20 55 4e 50 41 43 4b  >flags |= UNPACK
1dd63 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 3b  ED_PREFIX_MATCH;
1dd64 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
1dd65 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65  sqlite3BtreeMove
1dd66 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 62 2e  toUnpacked(u.bb.
1dd67 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 75 2e 62  pC->pCursor, u.b
1dd68 62 2e 70 49 64 78 4b 65 79 2c 20 30 2c 20 30 2c  b.pIdxKey, 0, 0,
1dd69 20 26 75 2e 62 62 2e 72 65 73 29 3b 0a 20 20 20   &u.bb.res);.   
1dd6a 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65   sqlite3VdbeDele
1dd6b 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64  teUnpackedRecord
1dd6c 28 75 2e 62 62 2e 70 49 64 78 4b 65 79 29 3b 0a  (u.bb.pIdxKey);.
1dd6d 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1dd6e 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 62  TE_OK ){.      b
1dd6f 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1dd70 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 69 73  u.bb.alreadyExis
1dd71 74 73 20 3d 20 28 75 2e 62 62 2e 72 65 73 3d 3d  ts = (u.bb.res==
1dd72 30 29 3b 0a 20 20 20 20 75 2e 62 62 2e 70 43 2d  0);.    u.bb.pC-
1dd73 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20  >deferredMoveto 
1dd74 3d 20 30 3b 0a 20 20 20 20 75 2e 62 62 2e 70 43  = 0;.    u.bb.pC
1dd75 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20  ->cacheStatus = 
1dd76 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d  CACHE_STALE;.  }
1dd77 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f  .  if( pOp->opco
1dd78 64 65 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a  de==OP_Found ){.
1dd79 20 20 20 20 69 66 28 20 75 2e 62 62 2e 61 6c 72      if( u.bb.alr
1dd7a 65 61 64 79 45 78 69 73 74 73 20 29 20 70 63 20  eadyExists ) pc 
1dd7b 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1dd7c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20   }else{.    if( 
1dd7d 21 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 69  !u.bb.alreadyExi
1dd7e 73 74 73 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e  sts ) pc = pOp->
1dd7f 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72  p2 - 1;.  }.  br
1dd80 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1dd81 65 3a 20 49 73 55 6e 69 71 75 65 20 50 31 20 50  e: IsUnique P1 P
1dd82 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20  2 P3 P4 *.**.** 
1dd83 43 75 72 73 6f 72 20 50 31 20 69 73 20 6f 70 65  Cursor P1 is ope
1dd84 6e 20 6f 6e 20 61 6e 20 69 6e 64 65 78 2e 20 20  n on an index.  
1dd85 53 6f 20 69 74 20 68 61 73 20 6e 6f 20 64 61 74  So it has no dat
1dd86 61 20 61 6e 64 20 69 74 73 20 6b 65 79 20 63 6f  a and its key co
1dd87 6e 73 69 73 74 73 20 0a 2a 2a 20 6f 66 20 61 20  nsists .** of a 
1dd88 72 65 63 6f 72 64 20 67 65 6e 65 72 61 74 65 64  record generated
1dd89 20 62 79 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72   by OP_MakeRecor
1dd8a 64 20 77 68 65 72 65 20 74 68 65 20 6c 61 73 74  d where the last
1dd8b 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 0a 2a   field is the .*
1dd8c 2a 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 65  * rowid of the e
1dd8d 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 69 6e  ntry that the in
1dd8e 64 65 78 20 72 65 66 65 72 73 20 74 6f 2e 0a 2a  dex refers to..*
1dd8f 2a 0a 2a 2a 20 54 68 65 20 50 33 20 72 65 67 69  *.** The P3 regi
1dd90 73 74 65 72 20 63 6f 6e 74 61 69 6e 73 20 61 6e  ster contains an
1dd91 20 69 6e 74 65 67 65 72 20 72 65 63 6f 72 64 20   integer record 
1dd92 6e 75 6d 62 65 72 2e 20 43 61 6c 6c 20 74 68 69  number. Call thi
1dd93 73 20 72 65 63 6f 72 64 20 0a 2a 2a 20 6e 75 6d  s record .** num
1dd94 62 65 72 20 52 2e 20 52 65 67 69 73 74 65 72 20  ber R. Register 
1dd95 50 34 20 69 73 20 74 68 65 20 66 69 72 73 74 20  P4 is the first 
1dd96 69 6e 20 61 20 73 65 74 20 6f 66 20 4e 20 63 6f  in a set of N co
1dd97 6e 74 69 67 75 6f 75 73 20 72 65 67 69 73 74 65  ntiguous registe
1dd98 72 73 0a 2a 2a 20 74 68 61 74 20 6d 61 6b 65 20  rs.** that make 
1dd99 75 70 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69  up an unpacked i
1dd9a 6e 64 65 78 20 6b 65 79 20 74 68 61 74 20 63 61  ndex key that ca
1dd9b 6e 20 62 65 20 75 73 65 64 20 77 69 74 68 20 63  n be used with c
1dd9c 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54 68 65  ursor P1..** The
1dd9d 20 76 61 6c 75 65 20 6f 66 20 4e 20 63 61 6e 20   value of N can 
1dd9e 62 65 20 69 6e 66 65 72 72 65 64 20 66 72 6f 6d  be inferred from
1dd9f 20 74 68 65 20 63 75 72 73 6f 72 2e 20 4e 20 69   the cursor. N i
1dda0 6e 63 6c 75 64 65 73 20 74 68 65 20 72 6f 77 69  ncludes the rowi
1dda1 64 0a 2a 2a 20 76 61 6c 75 65 20 61 70 70 65 6e  d.** value appen
1dda2 64 65 64 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  ded to the end o
1dda3 66 20 74 68 65 20 69 6e 64 65 78 20 72 65 63 6f  f the index reco
1dda4 72 64 2e 20 54 68 69 73 20 72 6f 77 69 64 20 76  rd. This rowid v
1dda5 61 6c 75 65 20 6d 61 79 0a 2a 2a 20 6f 72 20 6d  alue may.** or m
1dda6 61 79 20 6e 6f 74 20 62 65 20 74 68 65 20 73 61  ay not be the sa
1dda7 6d 65 20 61 73 20 52 2e 0a 2a 2a 0a 2a 2a 20 49  me as R..**.** I
1dda8 66 20 61 6e 79 20 6f 66 20 74 68 65 20 4e 20 72  f any of the N r
1dda9 65 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69  egisters beginni
1ddaa 6e 67 20 77 69 74 68 20 72 65 67 69 73 74 65 72  ng with register
1ddab 20 50 34 20 63 6f 6e 74 61 69 6e 73 20 61 20 4e   P4 contains a N
1ddac 55 4c 4c 0a 2a 2a 20 76 61 6c 75 65 2c 20 6a 75  ULL.** value, ju
1ddad 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74  mp immediately t
1ddae 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65  o P2..**.** Othe
1ddaf 72 77 69 73 65 2c 20 74 68 69 73 20 69 6e 73 74  rwise, this inst
1ddb0 72 75 63 74 69 6f 6e 20 63 68 65 63 6b 73 20 69  ruction checks i
1ddb1 66 20 63 75 72 73 6f 72 20 50 31 20 63 6f 6e 74  f cursor P1 cont
1ddb2 61 69 6e 73 20 61 6e 20 65 6e 74 72 79 0a 2a 2a  ains an entry.**
1ddb3 20 77 68 65 72 65 20 74 68 65 20 66 69 72 73 74   where the first
1ddb4 20 28 4e 2d 31 29 20 66 69 65 6c 64 73 20 6d 61   (N-1) fields ma
1ddb5 74 63 68 20 62 75 74 20 74 68 65 20 72 6f 77 69  tch but the rowi
1ddb6 64 20 76 61 6c 75 65 20 61 74 20 74 68 65 20 65  d value at the e
1ddb7 6e 64 0a 2a 2a 20 6f 66 20 74 68 65 20 69 6e 64  nd.** of the ind
1ddb8 65 78 20 65 6e 74 72 79 20 69 73 20 6e 6f 74 20  ex entry is not 
1ddb9 52 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  R. If there is n
1ddba 6f 20 73 75 63 68 20 65 6e 74 72 79 2c 20 63 6f  o such entry, co
1ddbb 6e 74 72 6f 6c 20 6a 75 6d 70 73 0a 2a 2a 20 74  ntrol jumps.** t
1ddbc 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32  o instruction P2
1ddbd 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65  . Otherwise, the
1ddbe 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 63 6f   rowid of the co
1ddbf 6e 66 6c 69 63 74 69 6e 67 20 69 6e 64 65 78 0a  nflicting index.
1ddc0 2a 2a 20 65 6e 74 72 79 20 69 73 20 63 6f 70 69  ** entry is copi
1ddc1 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 20 50  ed to register P
1ddc2 33 20 61 6e 64 20 63 6f 6e 74 72 6f 6c 20 66 61  3 and control fa
1ddc3 6c 6c 73 20 74 68 72 6f 75 67 68 20 74 6f 20 74  lls through to t
1ddc4 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e 73 74 72  he next.** instr
1ddc5 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 65  uction..**.** Se
1ddc6 65 20 61 6c 73 6f 3a 20 4e 6f 74 46 6f 75 6e 64  e also: NotFound
1ddc7 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20 46 6f 75  , NotExists, Fou
1ddc8 6e 64 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 73  nd.*/.case OP_Is
1ddc9 55 6e 69 71 75 65 3a 20 7b 20 20 20 20 20 20 20  Unique: {       
1ddca 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f   /* jump, in3 */
1ddcb 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1ddcc 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1ddcd 20 69 6e 74 6f 20 75 2e 62 63 20 2a 2f 0a 20 20   into u.bc */.  
1ddce 75 31 36 20 69 69 3b 0a 20 20 56 64 62 65 43 75  u16 ii;.  VdbeCu
1ddcf 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 42 74 43  rsor *pCx;.  BtC
1ddd0 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20  ursor *pCrsr;.  
1ddd1 75 31 36 20 6e 46 69 65 6c 64 3b 0a 20 20 4d 65  u16 nField;.  Me
1ddd2 6d 20 2a 61 4d 65 6d 3b 0a 20 20 55 6e 70 61 63  m *aMem;.  Unpac
1ddd3 6b 65 64 52 65 63 6f 72 64 20 72 3b 20 20 20 20  kedRecord r;    
1ddd4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ddd5 20 42 2d 54 72 65 65 20 69 6e 64 65 78 20 73 65   B-Tree index se
1ddd6 61 72 63 68 20 6b 65 79 20 2a 2f 0a 20 20 69 36  arch key */.  i6
1ddd7 34 20 52 3b 20 20 20 20 20 20 20 20 20 20 20 20  4 R;            
1ddd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ddd9 20 2f 2a 20 52 6f 77 69 64 20 73 74 6f 72 65 64   /* Rowid stored
1ddda 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20   in register P3 
1dddb 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  */.#endif /* loc
1dddc 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1dddd 65 64 20 69 6e 74 6f 20 75 2e 62 63 20 2a 2f 0a  ed into u.bc */.
1ddde 0a 20 20 75 2e 62 63 2e 61 4d 65 6d 20 3d 20 26  .  u.bc.aMem = &
1dddf 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 34 2e  p->aMem[pOp->p4.
1dde0 69 5d 3b 0a 20 20 2f 2a 20 41 73 73 65 72 74 20  i];.  /* Assert 
1dde1 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 73 20  that the values 
1dde2 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 50 31  of parameters P1
1dde3 20 61 6e 64 20 50 34 20 61 72 65 20 69 6e 20 72   and P4 are in r
1dde4 61 6e 67 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72  ange. */.  asser
1dde5 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d  t( pOp->p4type==
1dde6 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 61 73  P4_INT32 );.  as
1dde7 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 69 3e  sert( pOp->p4.i>
1dde8 30 20 26 26 20 70 4f 70 2d 3e 70 34 2e 69 3c 3d  0 && pOp->p4.i<=
1dde9 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 61 73 73  p->nMem );.  ass
1ddea 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20  ert( pOp->p1>=0 
1ddeb 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43  && pOp->p1<p->nC
1ddec 75 72 73 6f 72 20 29 3b 0a 0a 20 20 2f 2a 20 46  ursor );..  /* F
1dded 69 6e 64 20 74 68 65 20 69 6e 64 65 78 20 63 75  ind the index cu
1ddee 72 73 6f 72 2e 20 2a 2f 0a 20 20 75 2e 62 63 2e  rsor. */.  u.bc.
1ddef 70 43 78 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  pCx = p->apCsr[p
1ddf0 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
1ddf1 74 28 20 75 2e 62 63 2e 70 43 78 2d 3e 64 65 66  t( u.bc.pCx->def
1ddf2 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29  erredMoveto==0 )
1ddf3 3b 0a 20 20 75 2e 62 63 2e 70 43 78 2d 3e 73 65  ;.  u.bc.pCx->se
1ddf4 65 6b 52 65 73 75 6c 74 20 3d 20 30 3b 0a 20 20  ekResult = 0;.  
1ddf5 75 2e 62 63 2e 70 43 78 2d 3e 63 61 63 68 65 53  u.bc.pCx->cacheS
1ddf6 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54  tatus = CACHE_ST
1ddf7 41 4c 45 3b 0a 20 20 75 2e 62 63 2e 70 43 72 73  ALE;.  u.bc.pCrs
1ddf8 72 20 3d 20 75 2e 62 63 2e 70 43 78 2d 3e 70 43  r = u.bc.pCx->pC
1ddf9 75 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20 49 66 20  ursor;..  /* If 
1ddfa 61 6e 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65  any of the value
1ddfb 73 20 61 72 65 20 4e 55 4c 4c 2c 20 74 61 6b 65  s are NULL, take
1ddfc 20 74 68 65 20 6a 75 6d 70 2e 20 2a 2f 0a 20 20   the jump. */.  
1ddfd 75 2e 62 63 2e 6e 46 69 65 6c 64 20 3d 20 75 2e  u.bc.nField = u.
1ddfe 62 63 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f  bc.pCx->pKeyInfo
1ddff 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 66 6f 72 28  ->nField;.  for(
1de00 75 2e 62 63 2e 69 69 3d 30 3b 20 75 2e 62 63 2e  u.bc.ii=0; u.bc.
1de01 69 69 3c 75 2e 62 63 2e 6e 46 69 65 6c 64 3b 20  ii<u.bc.nField; 
1de02 75 2e 62 63 2e 69 69 2b 2b 29 7b 0a 20 20 20 20  u.bc.ii++){.    
1de03 69 66 28 20 75 2e 62 63 2e 61 4d 65 6d 5b 75 2e  if( u.bc.aMem[u.
1de04 62 63 2e 69 69 5d 2e 66 6c 61 67 73 20 26 20 4d  bc.ii].flags & M
1de05 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20  EM_Null ){.     
1de06 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1de07 31 3b 0a 20 20 20 20 20 20 75 2e 62 63 2e 70 43  1;.      u.bc.pC
1de08 72 73 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 62  rsr = 0;.      b
1de09 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
1de0a 20 20 61 73 73 65 72 74 28 20 28 75 2e 62 63 2e    assert( (u.bc.
1de0b 61 4d 65 6d 5b 75 2e 62 63 2e 6e 46 69 65 6c 64  aMem[u.bc.nField
1de0c 5d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ].flags & MEM_Nu
1de0d 6c 6c 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28  ll)==0 );..  if(
1de0e 20 75 2e 62 63 2e 70 43 72 73 72 21 3d 30 20 29   u.bc.pCrsr!=0 )
1de0f 7b 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74  {.    /* Populat
1de10 65 20 74 68 65 20 69 6e 64 65 78 20 73 65 61 72  e the index sear
1de11 63 68 20 6b 65 79 2e 20 2a 2f 0a 20 20 20 20 75  ch key. */.    u
1de12 2e 62 63 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d  .bc.r.pKeyInfo =
1de13 20 75 2e 62 63 2e 70 43 78 2d 3e 70 4b 65 79 49   u.bc.pCx->pKeyI
1de14 6e 66 6f 3b 0a 20 20 20 20 75 2e 62 63 2e 72 2e  nfo;.    u.bc.r.
1de15 6e 46 69 65 6c 64 20 3d 20 75 2e 62 63 2e 6e 46  nField = u.bc.nF
1de16 69 65 6c 64 20 2b 20 31 3b 0a 20 20 20 20 75 2e  ield + 1;.    u.
1de17 62 63 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50  bc.r.flags = UNP
1de18 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41  ACKED_PREFIX_SEA
1de19 52 43 48 3b 0a 20 20 20 20 75 2e 62 63 2e 72 2e  RCH;.    u.bc.r.
1de1a 61 4d 65 6d 20 3d 20 75 2e 62 63 2e 61 4d 65 6d  aMem = u.bc.aMem
1de1b 3b 0a 0a 20 20 20 20 2f 2a 20 45 78 74 72 61 63  ;..    /* Extrac
1de1c 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 75  t the value of u
1de1d 2e 62 63 2e 52 20 66 72 6f 6d 20 72 65 67 69 73  .bc.R from regis
1de1e 74 65 72 20 50 33 2e 20 2a 2f 0a 20 20 20 20 73  ter P3. */.    s
1de1f 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74  qlite3VdbeMemInt
1de20 65 67 65 72 69 66 79 28 70 49 6e 33 29 3b 0a 20  egerify(pIn3);. 
1de21 20 20 20 75 2e 62 63 2e 52 20 3d 20 70 49 6e 33     u.bc.R = pIn3
1de22 2d 3e 75 2e 69 3b 0a 0a 20 20 20 20 2f 2a 20 53  ->u.i;..    /* S
1de23 65 61 72 63 68 20 74 68 65 20 42 2d 54 72 65 65  earch the B-Tree
1de24 20 69 6e 64 65 78 2e 20 49 66 20 6e 6f 20 63 6f   index. If no co
1de25 6e 66 6c 69 63 74 69 6e 67 20 72 65 63 6f 72 64  nflicting record
1de26 20 69 73 20 66 6f 75 6e 64 2c 20 6a 75 6d 70 0a   is found, jump.
1de27 20 20 20 20 2a 2a 20 74 6f 20 50 32 2e 20 4f 74      ** to P2. Ot
1de28 68 65 72 77 69 73 65 2c 20 63 6f 70 79 20 74 68  herwise, copy th
1de29 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 63  e rowid of the c
1de2a 6f 6e 66 6c 69 63 74 69 6e 67 20 72 65 63 6f 72  onflicting recor
1de2b 64 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 67 69  d to.    ** regi
1de2c 73 74 65 72 20 50 33 20 61 6e 64 20 66 61 6c 6c  ster P3 and fall
1de2d 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20   through to the 
1de2e 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  next instruction
1de2f 2e 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73  .  */.    rc = s
1de30 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74  qlite3BtreeMovet
1de31 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 63 2e 70  oUnpacked(u.bc.p
1de32 43 72 73 72 2c 20 26 75 2e 62 63 2e 72 2c 20 30  Crsr, &u.bc.r, 0
1de33 2c 20 30 2c 20 26 75 2e 62 63 2e 70 43 78 2d 3e  , 0, &u.bc.pCx->
1de34 73 65 65 6b 52 65 73 75 6c 74 29 3b 0a 20 20 20  seekResult);.   
1de35 20 69 66 28 20 28 75 2e 62 63 2e 72 2e 66 6c 61   if( (u.bc.r.fla
1de36 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 50 52  gs & UNPACKED_PR
1de37 45 46 49 58 5f 53 45 41 52 43 48 29 20 7c 7c 20  EFIX_SEARCH) || 
1de38 75 2e 62 63 2e 72 2e 72 6f 77 69 64 3d 3d 75 2e  u.bc.r.rowid==u.
1de39 62 63 2e 52 20 29 7b 0a 20 20 20 20 20 20 70 63  bc.R ){.      pc
1de3a 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1de3b 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1de3c 20 70 49 6e 33 2d 3e 75 2e 69 20 3d 20 75 2e 62   pIn3->u.i = u.b
1de3d 63 2e 72 2e 72 6f 77 69 64 3b 0a 20 20 20 20 7d  c.r.rowid;.    }
1de3e 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1de3f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 45  ./* Opcode: NotE
1de40 78 69 73 74 73 20 50 31 20 50 32 20 50 33 20 2a  xists P1 P2 P3 *
1de41 20 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65   *.**.** Use the
1de42 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69   content of regi
1de43 73 74 65 72 20 50 33 20 61 73 20 61 20 69 6e 74  ster P3 as a int
1de44 65 67 65 72 20 6b 65 79 2e 20 20 49 66 20 61 20  eger key.  If a 
1de45 72 65 63 6f 72 64 20 0a 2a 2a 20 77 69 74 68 20  record .** with 
1de46 74 68 61 74 20 6b 65 79 20 64 6f 65 73 20 6e 6f  that key does no
1de47 74 20 65 78 69 73 74 20 69 6e 20 74 61 62 6c 65  t exist in table
1de48 20 6f 66 20 50 31 2c 20 74 68 65 6e 20 6a 75 6d   of P1, then jum
1de49 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 20 49 66 20  p to P2. .** If 
1de4a 74 68 65 20 72 65 63 6f 72 64 20 64 6f 65 73 20  the record does 
1de4b 65 78 69 73 74 2c 20 74 68 65 6e 20 66 61 6c 6c  exist, then fall
1de4c 20 74 68 72 75 2e 20 20 54 68 65 20 63 75 72 73   thru.  The curs
1de4d 6f 72 20 69 73 20 6c 65 66 74 20 0a 2a 2a 20 70  or is left .** p
1de4e 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 72  ointing to the r
1de4f 65 63 6f 72 64 20 69 66 20 69 74 20 65 78 69 73  ecord if it exis
1de50 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69  ts..**.** The di
1de51 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e  fference between
1de52 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20   this operation 
1de53 61 6e 64 20 4e 6f 74 46 6f 75 6e 64 20 69 73 20  and NotFound is 
1de54 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 6f 70 65  that this.** ope
1de55 72 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74  ration assumes t
1de56 68 65 20 6b 65 79 20 69 73 20 61 6e 20 69 6e 74  he key is an int
1de57 65 67 65 72 20 61 6e 64 20 74 68 61 74 20 50 31  eger and that P1
1de58 20 69 73 20 61 20 74 61 62 6c 65 20 77 68 65 72   is a table wher
1de59 65 61 73 0a 2a 2a 20 4e 6f 74 46 6f 75 6e 64 20  eas.** NotFound 
1de5a 61 73 73 75 6d 65 73 20 6b 65 79 20 69 73 20 61  assumes key is a
1de5b 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65   blob constructe
1de5c 64 20 66 72 6f 6d 20 4d 61 6b 65 52 65 63 6f 72  d from MakeRecor
1de5d 64 20 61 6e 64 0a 2a 2a 20 50 31 20 69 73 20 61  d and.** P1 is a
1de5e 6e 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 53  n index..**.** S
1de5f 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20  ee also: Found, 
1de60 4e 6f 74 46 6f 75 6e 64 2c 20 49 73 55 6e 69 71  NotFound, IsUniq
1de61 75 65 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f  ue.*/.case OP_No
1de62 74 45 78 69 73 74 73 3a 20 7b 20 20 20 20 20 20  tExists: {      
1de63 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a    /* jump, in3 *
1de64 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  /.#if 0  /* loca
1de65 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1de66 64 20 69 6e 74 6f 20 75 2e 62 64 20 2a 2f 0a 20  d into u.bd */. 
1de67 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1de68 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72  .  BtCursor *pCr
1de69 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20  sr;.  int res;. 
1de6a 20 75 36 34 20 69 4b 65 79 3b 0a 23 65 6e 64 69   u64 iKey;.#endi
1de6b 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1de6c 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1de6d 75 2e 62 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72  u.bd */..  asser
1de6e 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26  t( pIn3->flags &
1de6f 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 61 73   MEM_Int );.  as
1de70 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
1de71 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e   && pOp->p1<p->n
1de72 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 64  Cursor );.  u.bd
1de73 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  .pC = p->apCsr[p
1de74 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
1de75 74 28 20 75 2e 62 64 2e 70 43 21 3d 30 20 29 3b  t( u.bd.pC!=0 );
1de76 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e  .  assert( u.bd.
1de77 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20  pC->isTable );. 
1de78 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e 70 43   assert( u.bd.pC
1de79 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67  ->pseudoTableReg
1de7a 3d 3d 30 20 29 3b 0a 20 20 75 2e 62 64 2e 70 43  ==0 );.  u.bd.pC
1de7b 72 73 72 20 3d 20 75 2e 62 64 2e 70 43 2d 3e 70  rsr = u.bd.pC->p
1de7c 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 75 2e  Cursor;.  if( u.
1de7d 62 64 2e 70 43 72 73 72 21 3d 30 20 29 7b 0a 20  bd.pCrsr!=0 ){. 
1de7e 20 20 20 75 2e 62 64 2e 72 65 73 20 3d 20 30 3b     u.bd.res = 0;
1de7f 0a 20 20 20 20 75 2e 62 64 2e 69 4b 65 79 20 3d  .    u.bd.iKey =
1de80 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20 20   pIn3->u.i;.    
1de81 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1de82 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28  eMovetoUnpacked(
1de83 75 2e 62 64 2e 70 43 72 73 72 2c 20 30 2c 20 75  u.bd.pCrsr, 0, u
1de84 2e 62 64 2e 69 4b 65 79 2c 20 30 2c 20 26 75 2e  .bd.iKey, 0, &u.
1de85 62 64 2e 72 65 73 29 3b 0a 20 20 20 20 75 2e 62  bd.res);.    u.b
1de86 64 2e 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 20  d.pC->lastRowid 
1de87 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20  = pIn3->u.i;.   
1de88 20 75 2e 62 64 2e 70 43 2d 3e 72 6f 77 69 64 49   u.bd.pC->rowidI
1de89 73 56 61 6c 69 64 20 3d 20 75 2e 62 64 2e 72 65  sValid = u.bd.re
1de8a 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 20 20 75  s==0 ?1:0;.    u
1de8b 2e 62 64 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20  .bd.pC->nullRow 
1de8c 3d 20 30 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43  = 0;.    u.bd.pC
1de8d 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20  ->cacheStatus = 
1de8e 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20  CACHE_STALE;.   
1de8f 20 75 2e 62 64 2e 70 43 2d 3e 64 65 66 65 72 72   u.bd.pC->deferr
1de90 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20  edMoveto = 0;.  
1de91 20 20 69 66 28 20 75 2e 62 64 2e 72 65 73 21 3d    if( u.bd.res!=
1de92 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20  0 ){.      pc = 
1de93 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20  pOp->p2 - 1;.   
1de94 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e     assert( u.bd.
1de95 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64  pC->rowidIsValid
1de96 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ==0 );.    }.   
1de97 20 75 2e 62 64 2e 70 43 2d 3e 73 65 65 6b 52 65   u.bd.pC->seekRe
1de98 73 75 6c 74 20 3d 20 75 2e 62 64 2e 72 65 73 3b  sult = u.bd.res;
1de99 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
1de9a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 68   This happens wh
1de9b 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f  en an attempt to
1de9c 20 6f 70 65 6e 20 61 20 72 65 61 64 20 63 75 72   open a read cur
1de9d 73 6f 72 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a  sor on the.    *
1de9e 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  * sqlite_master 
1de9f 74 61 62 6c 65 20 72 65 74 75 72 6e 73 20 53 51  table returns SQ
1dea0 4c 49 54 45 5f 45 4d 50 54 59 2e 0a 20 20 20 20  LITE_EMPTY..    
1dea1 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  */.    pc = pOp-
1dea2 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 61 73 73  >p2 - 1;.    ass
1dea3 65 72 74 28 20 75 2e 62 64 2e 70 43 2d 3e 72 6f  ert( u.bd.pC->ro
1dea4 77 69 64 49 73 56 61 6c 69 64 3d 3d 30 20 29 3b  widIsValid==0 );
1dea5 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 73 65  .    u.bd.pC->se
1dea6 65 6b 52 65 73 75 6c 74 20 3d 20 30 3b 0a 20 20  ekResult = 0;.  
1dea7 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1dea8 20 4f 70 63 6f 64 65 3a 20 53 65 71 75 65 6e 63   Opcode: Sequenc
1dea9 65 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  e P1 P2 * * *.**
1deaa 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 6e 65 78  .** Find the nex
1deab 74 20 61 76 61 69 6c 61 62 6c 65 20 73 65 71 75  t available sequ
1deac 65 6e 63 65 20 6e 75 6d 62 65 72 20 66 6f 72 20  ence number for 
1dead 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 57 72  cursor P1..** Wr
1deae 69 74 65 20 74 68 65 20 73 65 71 75 65 6e 63 65  ite the sequence
1deaf 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 72 65 67   number into reg
1deb0 69 73 74 65 72 20 50 32 2e 0a 2a 2a 20 54 68 65  ister P2..** The
1deb1 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72   sequence number
1deb2 20 6f 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69   on the cursor i
1deb3 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 61 66  s incremented af
1deb4 74 65 72 20 74 68 69 73 0a 2a 2a 20 69 6e 73 74  ter this.** inst
1deb5 72 75 63 74 69 6f 6e 2e 20 20 0a 2a 2f 0a 63 61  ruction.  .*/.ca
1deb6 73 65 20 4f 50 5f 53 65 71 75 65 6e 63 65 3a 20  se OP_Sequence: 
1deb7 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f  {           /* o
1deb8 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a  ut2-prerelease *
1deb9 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  /.  assert( pOp-
1deba 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70  >p1>=0 && pOp->p
1debb 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  1<p->nCursor );.
1debc 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43    assert( p->apC
1debd 73 72 5b 70 4f 70 2d 3e 70 31 5d 21 3d 30 20 29  sr[pOp->p1]!=0 )
1debe 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20  ;.  pOut->u.i = 
1debf 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31  p->apCsr[pOp->p1
1dec0 5d 2d 3e 73 65 71 43 6f 75 6e 74 2b 2b 3b 0a 20  ]->seqCount++;. 
1dec1 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28   MemSetTypeFlag(
1dec2 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a  pOut, MEM_Int);.
1dec3 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20    break;.}.../* 
1dec4 4f 70 63 6f 64 65 3a 20 4e 65 77 52 6f 77 69 64  Opcode: NewRowid
1dec5 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a   P1 P2 P3 * *.**
1dec6 0a 2a 2a 20 47 65 74 20 61 20 6e 65 77 20 69 6e  .** Get a new in
1dec7 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e 75 6d  teger record num
1dec8 62 65 72 20 28 61 2e 6b 2e 61 20 22 72 6f 77 69  ber (a.k.a "rowi
1dec9 64 22 29 20 75 73 65 64 20 61 73 20 74 68 65 20  d") used as the 
1deca 6b 65 79 20 74 6f 20 61 20 74 61 62 6c 65 2e 0a  key to a table..
1decb 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 20 6e 75  ** The record nu
1decc 6d 62 65 72 20 69 73 20 6e 6f 74 20 70 72 65 76  mber is not prev
1decd 69 6f 75 73 6c 79 20 75 73 65 64 20 61 73 20 61  iously used as a
1dece 20 6b 65 79 20 69 6e 20 74 68 65 20 64 61 74 61   key in the data
1decf 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 74 68  base.** table th
1ded0 61 74 20 63 75 72 73 6f 72 20 50 31 20 70 6f 69  at cursor P1 poi
1ded1 6e 74 73 20 74 6f 2e 20 20 54 68 65 20 6e 65 77  nts to.  The new
1ded2 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69   record number i
1ded3 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 77 72 69  s written.** wri
1ded4 74 74 65 6e 20 74 6f 20 72 65 67 69 73 74 65 72  tten to register
1ded5 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 33   P2..**.** If P3
1ded6 3e 30 20 74 68 65 6e 20 50 33 20 69 73 20 61 20  >0 then P3 is a 
1ded7 72 65 67 69 73 74 65 72 20 69 6e 20 74 68 65 20  register in the 
1ded8 72 6f 6f 74 20 66 72 61 6d 65 20 6f 66 20 74 68  root frame of th
1ded9 69 73 20 56 44 42 45 20 74 68 61 74 20 68 6f 6c  is VDBE that hol
1deda 64 73 20 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65  ds .** the large
1dedb 73 74 20 70 72 65 76 69 6f 75 73 6c 79 20 67 65  st previously ge
1dedc 6e 65 72 61 74 65 64 20 72 65 63 6f 72 64 20 6e  nerated record n
1dedd 75 6d 62 65 72 2e 20 4e 6f 20 6e 65 77 20 72 65  umber. No new re
1dede 63 6f 72 64 20 6e 75 6d 62 65 72 73 20 61 72 65  cord numbers are
1dedf 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62  .** allowed to b
1dee0 65 20 6c 65 73 73 20 74 68 61 6e 20 74 68 69 73  e less than this
1dee1 20 76 61 6c 75 65 2e 20 57 68 65 6e 20 74 68 69   value. When thi
1dee2 73 20 76 61 6c 75 65 20 72 65 61 63 68 65 73 20  s value reaches 
1dee3 69 74 73 20 6d 61 78 69 6d 75 6d 2c 20 0a 2a 2a  its maximum, .**
1dee4 20 61 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 65   a SQLITE_FULL e
1dee5 72 72 6f 72 20 69 73 20 67 65 6e 65 72 61 74 65  rror is generate
1dee6 64 2e 20 54 68 65 20 50 33 20 72 65 67 69 73 74  d. The P3 regist
1dee7 65 72 20 69 73 20 75 70 64 61 74 65 64 20 77 69  er is updated wi
1dee8 74 68 20 74 68 65 20 27 0a 2a 2a 20 67 65 6e 65  th the '.** gene
1dee9 72 61 74 65 64 20 72 65 63 6f 72 64 20 6e 75 6d  rated record num
1deea 62 65 72 2e 20 54 68 69 73 20 50 33 20 6d 65 63  ber. This P3 mec
1deeb 68 61 6e 69 73 6d 20 69 73 20 75 73 65 64 20 74  hanism is used t
1deec 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65 6e 74  o help implement
1deed 20 74 68 65 0a 2a 2a 20 41 55 54 4f 49 4e 43 52   the.** AUTOINCR
1deee 45 4d 45 4e 54 20 66 65 61 74 75 72 65 2e 0a 2a  EMENT feature..*
1deef 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 77 52 6f 77  /.case OP_NewRow
1def0 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  id: {           
1def1 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61  /* out2-prerelea
1def2 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20  se */.#if 0  /* 
1def3 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1def4 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 65 20  moved into u.be 
1def5 2a 2f 0a 20 20 69 36 34 20 76 3b 20 20 20 20 20  */.  i64 v;     
1def6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1def7 68 65 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a  he new rowid */.
1def8 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1def9 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73  ;        /* Curs
1defa 6f 72 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 67  or of table to g
1defb 65 74 20 74 68 65 20 6e 65 77 20 72 6f 77 69 64  et the new rowid
1defc 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 3b 20 20   */.  int res;  
1defd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1defe 52 65 73 75 6c 74 20 6f 66 20 61 6e 20 73 71 6c  Result of an sql
1deff 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 29 20  ite3BtreeLast() 
1df00 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20  */.  int cnt;   
1df01 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
1df02 6f 75 6e 74 65 72 20 74 6f 20 6c 69 6d 69 74 20  ounter to limit 
1df03 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65  the number of se
1df04 61 72 63 68 65 73 20 2a 2f 0a 20 20 4d 65 6d 20  arches */.  Mem 
1df05 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20  *pMem;          
1df06 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68     /* Register h
1df07 6f 6c 64 69 6e 67 20 6c 61 72 67 65 73 74 20 72  olding largest r
1df08 6f 77 69 64 20 66 6f 72 20 41 55 54 4f 49 4e 43  owid for AUTOINC
1df09 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 56 64 62 65  REMENT */.  Vdbe
1df0a 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20  Frame *pFrame;  
1df0b 20 20 20 2f 2a 20 52 6f 6f 74 20 66 72 61 6d 65     /* Root frame
1df0c 20 6f 66 20 56 44 42 45 20 2a 2f 0a 23 65 6e 64   of VDBE */.#end
1df0d 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1df0e 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1df0f 20 75 2e 62 65 20 2a 2f 0a 0a 20 20 75 2e 62 65   u.be */..  u.be
1df10 2e 76 20 3d 20 30 3b 0a 20 20 75 2e 62 65 2e 72  .v = 0;.  u.be.r
1df11 65 73 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  es = 0;.  assert
1df12 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
1df13 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73  pOp->p1<p->nCurs
1df14 6f 72 20 29 3b 0a 20 20 75 2e 62 65 2e 70 43 20  or );.  u.be.pC 
1df15 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e  = p->apCsr[pOp->
1df16 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75  p1];.  assert( u
1df17 2e 62 65 2e 70 43 21 3d 30 20 29 3b 0a 20 20 69  .be.pC!=0 );.  i
1df18 66 28 20 4e 45 56 45 52 28 75 2e 62 65 2e 70 43  f( NEVER(u.be.pC
1df19 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 29 20 29 7b  ->pCursor==0) ){
1df1a 0a 20 20 20 20 2f 2a 20 54 68 65 20 7a 65 72 6f  .    /* The zero
1df1b 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
1df1c 61 62 6f 76 65 20 69 73 20 61 6c 6c 20 74 68 61  above is all tha
1df1d 74 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20  t is needed */. 
1df1e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54   }else{.    /* T
1df1f 68 65 20 6e 65 78 74 20 72 6f 77 69 64 20 6f 72  he next rowid or
1df20 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 28   record number (
1df21 64 69 66 66 65 72 65 6e 74 20 74 65 72 6d 73 20  different terms 
1df22 66 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20  for the same.   
1df23 20 2a 2a 20 74 68 69 6e 67 29 20 69 73 20 6f 62   ** thing) is ob
1df24 74 61 69 6e 65 64 20 69 6e 20 61 20 74 77 6f 2d  tained in a two-
1df25 73 74 65 70 20 61 6c 67 6f 72 69 74 68 6d 2e 0a  step algorithm..
1df26 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 69      **.    ** Fi
1df27 72 73 74 20 77 65 20 61 74 74 65 6d 70 74 20 74  rst we attempt t
1df28 6f 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65  o find the large
1df29 73 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69  st existing rowi
1df2a 64 20 61 6e 64 20 61 64 64 20 6f 6e 65 0a 20 20  d and add one.  
1df2b 20 20 2a 2a 20 74 6f 20 74 68 61 74 2e 20 20 42    ** to that.  B
1df2c 75 74 20 69 66 20 74 68 65 20 6c 61 72 67 65 73  ut if the larges
1df2d 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64  t existing rowid
1df2e 20 69 73 20 61 6c 72 65 61 64 79 20 74 68 65 20   is already the 
1df2f 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 70  maximum.    ** p
1df30 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 2c  ositive integer,
1df31 20 77 65 20 68 61 76 65 20 74 6f 20 66 61 6c 6c   we have to fall
1df32 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20   through to the 
1df33 73 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 70 72  second.    ** pr
1df34 6f 62 61 62 69 6c 69 73 74 69 63 20 61 6c 67 6f  obabilistic algo
1df35 72 69 74 68 6d 0a 20 20 20 20 2a 2a 0a 20 20 20  rithm.    **.   
1df36 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61   ** The second a
1df37 6c 67 6f 72 69 74 68 6d 20 69 73 20 74 6f 20 73  lgorithm is to s
1df38 65 6c 65 63 74 20 61 20 72 6f 77 69 64 20 61 74  elect a rowid at
1df39 20 72 61 6e 64 6f 6d 20 61 6e 64 20 73 65 65 20   random and see 
1df3a 69 66 0a 20 20 20 20 2a 2a 20 69 74 20 61 6c 72  if.    ** it alr
1df3b 65 61 64 79 20 65 78 69 73 74 73 20 69 6e 20 74  eady exists in t
1df3c 68 65 20 74 61 62 6c 65 2e 20 20 49 66 20 69 74  he table.  If it
1df3d 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c   does not exist,
1df3e 20 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20   we have.    ** 
1df3f 73 75 63 63 65 65 64 65 64 2e 20 20 49 66 20 74  succeeded.  If t
1df40 68 65 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20  he random rowid 
1df41 64 6f 65 73 20 65 78 69 73 74 2c 20 77 65 20 73  does exist, we s
1df42 65 6c 65 63 74 20 61 20 6e 65 77 20 6f 6e 65 0a  elect a new one.
1df43 20 20 20 20 2a 2a 20 61 6e 64 20 74 72 79 20 61      ** and try a
1df44 67 61 69 6e 2c 20 75 70 20 74 6f 20 31 30 30 20  gain, up to 100 
1df45 74 69 6d 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  times..    */.  
1df46 20 20 61 73 73 65 72 74 28 20 75 2e 62 65 2e 70    assert( u.be.p
1df47 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20  C->isTable );.  
1df48 20 20 75 2e 62 65 2e 63 6e 74 20 3d 20 30 3b 0a    u.be.cnt = 0;.
1df49 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 33  .#ifdef SQLITE_3
1df4a 32 42 49 54 5f 52 4f 57 49 44 0a 23 20 20 20 64  2BIT_ROWID.#   d
1df4b 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20  efine MAX_ROWID 
1df4c 30 78 37 66 66 66 66 66 66 66 0a 23 65 6c 73 65  0x7fffffff.#else
1df4d 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d  .    /* Some com
1df4e 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20  pilers complain 
1df4f 61 62 6f 75 74 20 63 6f 6e 73 74 61 6e 74 73 20  about constants 
1df50 6f 66 20 74 68 65 20 66 6f 72 6d 20 30 78 37 66  of the form 0x7f
1df51 66 66 66 66 66 66 66 66 66 66 66 66 66 66 2e 0a  ffffffffffffff..
1df52 20 20 20 20 2a 2a 20 4f 74 68 65 72 73 20 63 6f      ** Others co
1df53 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 30 78 37  mplain about 0x7
1df54 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66  ffffffffffffffff
1df55 4c 4c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  LL.  The followi
1df56 6e 67 20 6d 61 63 72 6f 20 73 65 65 6d 73 0a 20  ng macro seems. 
1df57 20 20 20 2a 2a 20 74 6f 20 70 72 6f 76 69 64 65     ** to provide
1df58 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 77 68   the constant wh
1df59 69 6c 65 20 6d 61 6b 69 6e 67 20 61 6c 6c 20 63  ile making all c
1df5a 6f 6d 70 69 6c 65 72 73 20 68 61 70 70 79 2e 0a  ompilers happy..
1df5b 20 20 20 20 2a 2f 0a 23 20 20 20 64 65 66 69 6e      */.#   defin
1df5c 65 20 4d 41 58 5f 52 4f 57 49 44 20 20 28 69 36  e MAX_ROWID  (i6
1df5d 34 29 28 20 28 28 28 75 36 34 29 30 78 37 66 66  4)( (((u64)0x7ff
1df5e 66 66 66 66 66 29 3c 3c 33 32 29 20 7c 20 28 75  fffff)<<32) | (u
1df5f 36 34 29 30 78 66 66 66 66 66 66 66 66 20 29 0a  64)0xffffffff ).
1df60 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20  #endif..    if( 
1df61 21 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e  !u.be.pC->useRan
1df62 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 20  domRowid ){.    
1df63 20 20 75 2e 62 65 2e 76 20 3d 20 73 71 6c 69 74    u.be.v = sqlit
1df64 65 33 42 74 72 65 65 47 65 74 43 61 63 68 65 64  e3BtreeGetCached
1df65 52 6f 77 69 64 28 75 2e 62 65 2e 70 43 2d 3e 70  Rowid(u.be.pC->p
1df66 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 69  Cursor);.      i
1df67 66 28 20 75 2e 62 65 2e 76 3d 3d 30 20 29 7b 0a  f( u.be.v==0 ){.
1df68 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1df69 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 75 2e  ite3BtreeLast(u.
1df6a 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20  be.pC->pCursor, 
1df6b 26 75 2e 62 65 2e 72 65 73 29 3b 0a 20 20 20 20  &u.be.res);.    
1df6c 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1df6d 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1df6e 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75     goto abort_du
1df6f 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
1df70 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
1df71 28 20 75 2e 62 65 2e 72 65 73 20 29 7b 0a 20 20  ( u.be.res ){.  
1df72 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76 20 3d          u.be.v =
1df73 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73   1;.        }els
1df74 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  e{.          ass
1df75 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
1df76 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75  eCursorIsValid(u
1df77 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29  .be.pC->pCursor)
1df78 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63   );.          rc
1df79 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b   = sqlite3BtreeK
1df7a 65 79 53 69 7a 65 28 75 2e 62 65 2e 70 43 2d 3e  eySize(u.be.pC->
1df7b 70 43 75 72 73 6f 72 2c 20 26 75 2e 62 65 2e 76  pCursor, &u.be.v
1df7c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  );.          ass
1df7d 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
1df7e 4f 4b 20 29 3b 20 20 20 2f 2a 20 43 61 6e 6e 6f  OK );   /* Canno
1df7f 74 20 66 61 69 6c 20 66 6f 6c 6c 6f 77 69 6e 67  t fail following
1df80 20 42 74 72 65 65 4c 61 73 74 28 29 20 2a 2f 0a   BtreeLast() */.
1df81 20 20 20 20 20 20 20 20 20 20 69 66 28 20 75 2e            if( u.
1df82 62 65 2e 76 3d 3d 4d 41 58 5f 52 4f 57 49 44 20  be.v==MAX_ROWID 
1df83 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 75  ){.            u
1df84 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e 64 6f  .be.pC->useRando
1df85 6d 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20  mRowid = 1;.    
1df86 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1df87 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76 2b           u.be.v+
1df88 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  +;.          }. 
1df89 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1df8a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1df8b 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d  _OMIT_AUTOINCREM
1df8c 45 4e 54 0a 20 20 20 20 20 20 69 66 28 20 70 4f  ENT.      if( pO
1df8d 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20 20 20 20  p->p3 ){.       
1df8e 20 69 66 28 20 70 2d 3e 70 46 72 61 6d 65 20 29   if( p->pFrame )
1df8f 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28  {.          for(
1df90 75 2e 62 65 2e 70 46 72 61 6d 65 3d 70 2d 3e 70  u.be.pFrame=p->p
1df91 46 72 61 6d 65 3b 20 75 2e 62 65 2e 70 46 72 61  Frame; u.be.pFra
1df92 6d 65 2d 3e 70 50 61 72 65 6e 74 3b 20 75 2e 62  me->pParent; u.b
1df93 65 2e 70 46 72 61 6d 65 3d 75 2e 62 65 2e 70 46  e.pFrame=u.be.pF
1df94 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a  rame->pParent);.
1df95 20 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e 70            u.be.p
1df96 4d 65 6d 20 3d 20 26 75 2e 62 65 2e 70 46 72 61  Mem = &u.be.pFra
1df97 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33  me->aMem[pOp->p3
1df98 5d 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  ];.        }else
1df99 7b 0a 20 20 20 20 20 20 20 20 20 20 75 2e 62 65  {.          u.be
1df9a 2e 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d  .pMem = &p->aMem
1df9b 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20  [pOp->p3];.     
1df9c 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20     }.        /* 
1df9d 41 73 73 65 72 74 20 74 68 61 74 20 50 33 20 69  Assert that P3 i
1df9e 73 20 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72 79  s a valid memory
1df9f 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20 20   cell. */.      
1dfa0 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1dfa1 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d  3>0 && pOp->p3<=
1dfa2 28 70 2d 3e 70 46 72 61 6d 65 20 3f 20 75 2e 62  (p->pFrame ? u.b
1dfa3 65 2e 70 46 72 61 6d 65 2d 3e 6e 4d 65 6d 20 3a  e.pFrame->nMem :
1dfa4 20 70 2d 3e 6e 4d 65 6d 29 20 29 3b 0a 0a 20 20   p->nMem) );..  
1dfa5 20 20 20 20 20 20 52 45 47 49 53 54 45 52 5f 54        REGISTER_T
1dfa6 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e  RACE(pOp->p3, u.
1dfa7 62 65 2e 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20  be.pMem);.      
1dfa8 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1dfa9 49 6e 74 65 67 65 72 69 66 79 28 75 2e 62 65 2e  Integerify(u.be.
1dfaa 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 61  pMem);.        a
1dfab 73 73 65 72 74 28 20 28 75 2e 62 65 2e 70 4d 65  ssert( (u.be.pMe
1dfac 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  m->flags & MEM_I
1dfad 6e 74 29 21 3d 30 20 29 3b 20 20 2f 2a 20 6d 65  nt)!=0 );  /* me
1dfae 6d 28 50 33 29 20 68 6f 6c 64 73 20 61 6e 20 69  m(P3) holds an i
1dfaf 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20  nteger */.      
1dfb0 20 20 69 66 28 20 75 2e 62 65 2e 70 4d 65 6d 2d    if( u.be.pMem-
1dfb1 3e 75 2e 69 3d 3d 4d 41 58 5f 52 4f 57 49 44 20  >u.i==MAX_ROWID 
1dfb2 7c 7c 20 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52  || u.be.pC->useR
1dfb3 61 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20  andomRowid ){.  
1dfb4 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
1dfb5 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 20  ITE_FULL;.      
1dfb6 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64      goto abort_d
1dfb7 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
1dfb8 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
1dfb9 66 28 20 75 2e 62 65 2e 76 3c 75 2e 62 65 2e 70  f( u.be.v<u.be.p
1dfba 4d 65 6d 2d 3e 75 2e 69 2b 31 20 29 7b 0a 20 20  Mem->u.i+1 ){.  
1dfbb 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76 20 3d          u.be.v =
1dfbc 20 75 2e 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69 20   u.be.pMem->u.i 
1dfbd 2b 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  + 1;.        }. 
1dfbe 20 20 20 20 20 20 20 75 2e 62 65 2e 70 4d 65 6d         u.be.pMem
1dfbf 2d 3e 75 2e 69 20 3d 20 75 2e 62 65 2e 76 3b 0a  ->u.i = u.be.v;.
1dfc0 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a        }.#endif..
1dfc1 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
1dfc2 65 65 53 65 74 43 61 63 68 65 64 52 6f 77 69 64  eeSetCachedRowid
1dfc3 28 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f  (u.be.pC->pCurso
1dfc4 72 2c 20 75 2e 62 65 2e 76 3c 4d 41 58 5f 52 4f  r, u.be.v<MAX_RO
1dfc5 57 49 44 20 3f 20 75 2e 62 65 2e 76 2b 31 20 3a  WID ? u.be.v+1 :
1dfc6 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69   0);.    }.    i
1dfc7 66 28 20 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52  f( u.be.pC->useR
1dfc8 61 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20  andomRowid ){.  
1dfc9 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
1dfca 3e 70 33 3d 3d 30 20 29 3b 20 20 2f 2a 20 57 65  >p3==0 );  /* We
1dfcb 20 63 61 6e 6e 6f 74 20 62 65 20 69 6e 20 72 61   cannot be in ra
1dfcc 6e 64 6f 6d 20 72 6f 77 69 64 20 6d 6f 64 65 20  ndom rowid mode 
1dfcd 69 66 20 74 68 69 73 20 69 73 0a 20 20 20 20 20  if this is.     
1dfce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dfcf 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 20 41 55          ** an AU
1dfd0 54 4f 49 4e 43 52 45 4d 45 4e 54 20 74 61 62 6c  TOINCREMENT tabl
1dfd1 65 2e 20 2a 2f 0a 20 20 20 20 20 20 75 2e 62 65  e. */.      u.be
1dfd2 2e 76 20 3d 20 64 62 2d 3e 6c 61 73 74 52 6f 77  .v = db->lastRow
1dfd3 69 64 3b 0a 20 20 20 20 20 20 75 2e 62 65 2e 63  id;.      u.be.c
1dfd4 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 6f  nt = 0;.      do
1dfd5 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e  {.        if( u.
1dfd6 62 65 2e 63 6e 74 3d 3d 30 20 26 26 20 28 75 2e  be.cnt==0 && (u.
1dfd7 62 65 2e 76 26 30 78 66 66 66 66 66 66 29 3d 3d  be.v&0xffffff)==
1dfd8 75 2e 62 65 2e 76 20 29 7b 0a 20 20 20 20 20 20  u.be.v ){.      
1dfd9 20 20 20 20 75 2e 62 65 2e 76 2b 2b 3b 0a 20 20      u.be.v++;.  
1dfda 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1dfdb 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72         sqlite3_r
1dfdc 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66  andomness(sizeof
1dfdd 28 75 2e 62 65 2e 76 29 2c 20 26 75 2e 62 65 2e  (u.be.v), &u.be.
1dfde 76 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  v);.          if
1dfdf 28 20 75 2e 62 65 2e 63 6e 74 3c 35 20 29 20 75  ( u.be.cnt<5 ) u
1dfe0 2e 62 65 2e 76 20 26 3d 20 30 78 66 66 66 66 66  .be.v &= 0xfffff
1dfe1 66 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  f;.        }.   
1dfe2 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1dfe3 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61  3BtreeMovetoUnpa
1dfe4 63 6b 65 64 28 75 2e 62 65 2e 70 43 2d 3e 70 43  cked(u.be.pC->pC
1dfe5 75 72 73 6f 72 2c 20 30 2c 20 28 75 36 34 29 75  ursor, 0, (u64)u
1dfe6 2e 62 65 2e 76 2c 20 30 2c 20 26 75 2e 62 65 2e  .be.v, 0, &u.be.
1dfe7 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 75 2e  res);.        u.
1dfe8 62 65 2e 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20  be.cnt++;.      
1dfe9 7d 77 68 69 6c 65 28 20 75 2e 62 65 2e 63 6e 74  }while( u.be.cnt
1dfea 3c 31 30 30 20 26 26 20 72 63 3d 3d 53 51 4c 49  <100 && rc==SQLI
1dfeb 54 45 5f 4f 4b 20 26 26 20 75 2e 62 65 2e 72 65  TE_OK && u.be.re
1dfec 73 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66  s==0 );.      if
1dfed 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1dfee 26 26 20 75 2e 62 65 2e 72 65 73 3d 3d 30 20 29  && u.be.res==0 )
1dfef 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  {.        rc = S
1dff0 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20  QLITE_FULL;.    
1dff1 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64      goto abort_d
1dff2 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
1dff3 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 75     }.    }.    u
1dff4 2e 62 65 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56  .be.pC->rowidIsV
1dff5 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 75 2e  alid = 0;.    u.
1dff6 62 65 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d  be.pC->deferredM
1dff7 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 75  oveto = 0;.    u
1dff8 2e 62 65 2e 70 43 2d 3e 63 61 63 68 65 53 74 61  .be.pC->cacheSta
1dff9 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c  tus = CACHE_STAL
1dffa 45 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54  E;.  }.  MemSetT
1dffb 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45  ypeFlag(pOut, ME
1dffc 4d 5f 49 6e 74 29 3b 0a 20 20 70 4f 75 74 2d 3e  M_Int);.  pOut->
1dffd 75 2e 69 20 3d 20 75 2e 62 65 2e 76 3b 0a 20 20  u.i = u.be.v;.  
1dffe 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1dfff 6f 64 65 3a 20 49 6e 73 65 72 74 20 50 31 20 50  ode: Insert P1 P
1e000 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a  2 P3 P4 P5.**.**
1e001 20 57 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20   Write an entry 
1e002 69 6e 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6f  into the table o
1e003 66 20 63 75 72 73 6f 72 20 50 31 2e 20 20 41 20  f cursor P1.  A 
1e004 6e 65 77 20 65 6e 74 72 79 20 69 73 0a 2a 2a 20  new entry is.** 
1e005 63 72 65 61 74 65 64 20 69 66 20 69 74 20 64 6f  created if it do
1e006 65 73 6e 27 74 20 61 6c 72 65 61 64 79 20 65 78  esn't already ex
1e007 69 73 74 20 6f 72 20 74 68 65 20 64 61 74 61 20  ist or the data 
1e008 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 0a  for an existing.
1e009 2a 2a 20 65 6e 74 72 79 20 69 73 20 6f 76 65 72  ** entry is over
1e00a 77 72 69 74 74 65 6e 2e 20 20 54 68 65 20 64 61  written.  The da
1e00b 74 61 20 69 73 20 74 68 65 20 76 61 6c 75 65 20  ta is the value 
1e00c 4d 45 4d 5f 42 6c 6f 62 20 73 74 6f 72 65 64 20  MEM_Blob stored 
1e00d 69 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6e  in register.** n
1e00e 75 6d 62 65 72 20 50 32 2e 20 54 68 65 20 6b 65  umber P2. The ke
1e00f 79 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72  y is stored in r
1e010 65 67 69 73 74 65 72 20 50 33 2e 20 54 68 65 20  egister P3. The 
1e011 6b 65 79 20 6d 75 73 74 0a 2a 2a 20 62 65 20 61  key must.** be a
1e012 20 4d 45 4d 5f 49 6e 74 2e 0a 2a 2a 0a 2a 2a 20   MEM_Int..**.** 
1e013 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 4e 43  If the OPFLAG_NC
1e014 48 41 4e 47 45 20 66 6c 61 67 20 6f 66 20 50 35  HANGE flag of P5
1e015 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68   is set, then th
1e016 65 20 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75  e row change cou
1e017 6e 74 20 69 73 0a 2a 2a 20 69 6e 63 72 65 6d 65  nt is.** increme
1e018 6e 74 65 64 20 28 6f 74 68 65 72 77 69 73 65 20  nted (otherwise 
1e019 6e 6f 74 29 2e 20 20 49 66 20 74 68 65 20 4f 50  not).  If the OP
1e01a 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 20 66  FLAG_LASTROWID f
1e01b 6c 61 67 20 6f 66 20 50 35 20 69 73 20 73 65 74  lag of P5 is set
1e01c 2c 0a 2a 2a 20 74 68 65 6e 20 72 6f 77 69 64 20  ,.** then rowid 
1e01d 69 73 20 73 74 6f 72 65 64 20 66 6f 72 20 73 75  is stored for su
1e01e 62 73 65 71 75 65 6e 74 20 72 65 74 75 72 6e 20  bsequent return 
1e01f 62 79 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65  by the.** sqlite
1e020 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
1e021 77 69 64 28 29 20 66 75 6e 63 74 69 6f 6e 20 28  wid() function (
1e022 6f 74 68 65 72 77 69 73 65 20 69 74 20 69 73 20  otherwise it is 
1e023 75 6e 6d 6f 64 69 66 69 65 64 29 2e 0a 2a 2a 0a  unmodified)..**.
1e024 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 4c 41 47  ** If the OPFLAG
1e025 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66  _USESEEKRESULT f
1e026 6c 61 67 20 6f 66 20 50 35 20 69 73 20 73 65 74  lag of P5 is set
1e027 20 61 6e 64 20 69 66 20 74 68 65 20 72 65 73 75   and if the resu
1e028 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 6c 61 73  lt of.** the las
1e029 74 20 73 65 65 6b 20 6f 70 65 72 61 74 69 6f 6e  t seek operation
1e02a 20 28 4f 50 5f 4e 6f 74 45 78 69 73 74 73 29 20   (OP_NotExists) 
1e02b 77 61 73 20 61 20 73 75 63 63 65 73 73 2c 20 74  was a success, t
1e02c 68 65 6e 20 74 68 69 73 0a 2a 2a 20 6f 70 65 72  hen this.** oper
1e02d 61 74 69 6f 6e 20 77 69 6c 6c 20 6e 6f 74 20 61  ation will not a
1e02e 74 74 65 6d 70 74 20 74 6f 20 66 69 6e 64 20 74  ttempt to find t
1e02f 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 72  he appropriate r
1e030 6f 77 20 62 65 66 6f 72 65 20 64 6f 69 6e 67 0a  ow before doing.
1e031 2a 2a 20 74 68 65 20 69 6e 73 65 72 74 20 62 75  ** the insert bu
1e032 74 20 77 69 6c 6c 20 69 6e 73 74 65 61 64 20 6f  t will instead o
1e033 76 65 72 77 72 69 74 65 20 74 68 65 20 72 6f 77  verwrite the row
1e034 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72   that the cursor
1e035 20 69 73 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79   is.** currently
1e036 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 50   pointing to.  P
1e037 72 65 73 75 6d 61 62 6c 79 2c 20 74 68 65 20 70  resumably, the p
1e038 72 69 6f 72 20 4f 50 5f 4e 6f 74 45 78 69 73 74  rior OP_NotExist
1e039 73 20 6f 70 63 6f 64 65 0a 2a 2a 20 68 61 73 20  s opcode.** has 
1e03a 61 6c 72 65 61 64 79 20 70 6f 73 69 74 69 6f 6e  already position
1e03b 65 64 20 74 68 65 20 63 75 72 73 6f 72 20 63 6f  ed the cursor co
1e03c 72 72 65 63 74 6c 79 2e 20 20 54 68 69 73 20 69  rrectly.  This i
1e03d 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  s an optimizatio
1e03e 6e 0a 2a 2a 20 74 68 61 74 20 62 6f 6f 73 74 73  n.** that boosts
1e03f 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 62 79 20   performance by 
1e040 61 76 6f 69 64 69 6e 67 20 72 65 64 75 6e 64 61  avoiding redunda
1e041 6e 74 20 73 65 65 6b 73 2e 0a 2a 2a 0a 2a 2a 20  nt seeks..**.** 
1e042 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 49 53  If the OPFLAG_IS
1e043 55 50 44 41 54 45 20 66 6c 61 67 20 69 73 20 73  UPDATE flag is s
1e044 65 74 2c 20 74 68 65 6e 20 74 68 69 73 20 6f 70  et, then this op
1e045 63 6f 64 65 20 69 73 20 70 61 72 74 20 6f 66 20  code is part of 
1e046 61 6e 0a 2a 2a 20 55 50 44 41 54 45 20 6f 70 65  an.** UPDATE ope
1e047 72 61 74 69 6f 6e 2e 20 20 4f 74 68 65 72 77 69  ration.  Otherwi
1e048 73 65 20 28 69 66 20 74 68 65 20 66 6c 61 67 20  se (if the flag 
1e049 69 73 20 63 6c 65 61 72 29 20 74 68 65 6e 20 74  is clear) then t
1e04a 68 69 73 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73  his opcode.** is
1e04b 20 70 61 72 74 20 6f 66 20 61 6e 20 49 4e 53 45   part of an INSE
1e04c 52 54 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 54  RT operation.  T
1e04d 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 69 73  he difference is
1e04e 20 6f 6e 6c 79 20 69 6d 70 6f 72 74 61 6e 74 20   only important 
1e04f 74 6f 0a 2a 2a 20 74 68 65 20 75 70 64 61 74 65  to.** the update
1e050 20 68 6f 6f 6b 2e 0a 2a 2a 0a 2a 2a 20 50 61 72   hook..**.** Par
1e051 61 6d 65 74 65 72 20 50 34 20 6d 61 79 20 70 6f  ameter P4 may po
1e052 69 6e 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20  int to a string 
1e053 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74  containing the t
1e054 61 62 6c 65 2d 6e 61 6d 65 2c 20 6f 72 0a 2a 2a  able-name, or.**
1e055 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 20 49 66   may be NULL. If
1e056 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c   it is not NULL,
1e057 20 74 68 65 6e 20 74 68 65 20 75 70 64 61 74 65   then the update
1e058 2d 68 6f 6f 6b 20 0a 2a 2a 20 28 73 71 6c 69 74  -hook .** (sqlit
1e059 65 33 2e 78 55 70 64 61 74 65 43 61 6c 6c 62 61  e3.xUpdateCallba
1e05a 63 6b 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 66  ck) is invoked f
1e05b 6f 6c 6c 6f 77 69 6e 67 20 61 20 73 75 63 63 65  ollowing a succe
1e05c 73 73 66 75 6c 20 69 6e 73 65 72 74 2e 0a 2a 2a  ssful insert..**
1e05d 0a 2a 2a 20 28 57 41 52 4e 49 4e 47 2f 54 4f 44  .** (WARNING/TOD
1e05e 4f 3a 20 49 66 20 50 31 20 69 73 20 61 20 70 73  O: If P1 is a ps
1e05f 65 75 64 6f 2d 63 75 72 73 6f 72 20 61 6e 64 20  eudo-cursor and 
1e060 50 32 20 69 73 20 64 79 6e 61 6d 69 63 61 6c 6c  P2 is dynamicall
1e061 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20  y.** allocated, 
1e062 74 68 65 6e 20 6f 77 6e 65 72 73 68 69 70 20 6f  then ownership o
1e063 66 20 50 32 20 69 73 20 74 72 61 6e 73 66 65 72  f P2 is transfer
1e064 72 65 64 20 74 6f 20 74 68 65 20 70 73 65 75 64  red to the pseud
1e065 6f 2d 63 75 72 73 6f 72 0a 2a 2a 20 61 6e 64 20  o-cursor.** and 
1e066 72 65 67 69 73 74 65 72 20 50 32 20 62 65 63 6f  register P2 beco
1e067 6d 65 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20  mes ephemeral.  
1e068 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  If the cursor is
1e069 20 63 68 61 6e 67 65 64 2c 20 74 68 65 0a 2a 2a   changed, the.**
1e06a 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74   value of regist
1e06b 65 72 20 50 32 20 77 69 6c 6c 20 74 68 65 6e 20  er P2 will then 
1e06c 63 68 61 6e 67 65 2e 20 20 4d 61 6b 65 20 73 75  change.  Make su
1e06d 72 65 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74  re this does not
1e06e 0a 2a 2a 20 63 61 75 73 65 20 61 6e 79 20 70 72  .** cause any pr
1e06f 6f 62 6c 65 6d 73 2e 29 0a 2a 2a 0a 2a 2a 20 54  oblems.).**.** T
1e070 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1e071 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 74 61  only works on ta
1e072 62 6c 65 73 2e 20 20 54 68 65 20 65 71 75 69 76  bles.  The equiv
1e073 61 6c 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f  alent instructio
1e074 6e 0a 2a 2a 20 66 6f 72 20 69 6e 64 69 63 65 73  n.** for indices
1e075 20 69 73 20 4f 50 5f 49 64 78 49 6e 73 65 72 74   is OP_IdxInsert
1e076 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 73  ..*/.case OP_Ins
1e077 65 72 74 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a  ert: {.#if 0  /*
1e078 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e079 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 66   moved into u.bf
1e07a 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 61 74 61   */.  Mem *pData
1e07b 3b 20 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20 63  ;       /* MEM c
1e07c 65 6c 6c 20 68 6f 6c 64 69 6e 67 20 64 61 74 61  ell holding data
1e07d 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20   for the record 
1e07e 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a  to be inserted *
1e07f 2f 0a 20 20 4d 65 6d 20 2a 70 4b 65 79 3b 20 20  /.  Mem *pKey;  
1e080 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65 6c        /* MEM cel
1e081 6c 20 68 6f 6c 64 69 6e 67 20 6b 65 79 20 20 66  l holding key  f
1e082 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f  or the record */
1e083 0a 20 20 69 36 34 20 69 4b 65 79 3b 20 20 20 20  .  i64 iKey;    
1e084 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 74 65       /* The inte
1e085 67 65 72 20 52 4f 57 49 44 20 6f 72 20 6b 65 79  ger ROWID or key
1e086 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20   for the record 
1e087 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a  to be inserted *
1e088 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  /.  VdbeCursor *
1e089 70 43 3b 20 20 20 2f 2a 20 43 75 72 73 6f 72 20  pC;   /* Cursor 
1e08a 74 6f 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68  to table into wh
1e08b 69 63 68 20 69 6e 73 65 72 74 20 69 73 20 77 72  ich insert is wr
1e08c 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e  itten */.  int n
1e08d 5a 65 72 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20  Zero;        /* 
1e08e 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 2d 62  Number of zero-b
1e08f 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20 2a  ytes to append *
1e090 2f 0a 20 20 69 6e 74 20 73 65 65 6b 52 65 73 75  /.  int seekResu
1e091 6c 74 3b 20 20 20 2f 2a 20 52 65 73 75 6c 74 20  lt;   /* Result 
1e092 6f 66 20 70 72 69 6f 72 20 73 65 65 6b 20 6f 72  of prior seek or
1e093 20 30 20 69 66 20 6e 6f 20 55 53 45 53 45 45 4b   0 if no USESEEK
1e094 52 45 53 55 4c 54 20 66 6c 61 67 20 2a 2f 0a 20  RESULT flag */. 
1e095 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
1e096 3b 20 20 2f 2a 20 64 61 74 61 62 61 73 65 20 6e  ;  /* database n
1e097 61 6d 65 20 2d 20 75 73 65 64 20 62 79 20 74 68  ame - used by th
1e098 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f  e update hook */
1e099 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1e09a 54 62 6c 3b 20 2f 2a 20 54 61 62 6c 65 20 6e 61  Tbl; /* Table na
1e09b 6d 65 20 2d 20 75 73 65 64 20 62 79 20 74 68 65  me - used by the
1e09c 20 6f 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a   opdate hook */.
1e09d 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20    int op;       
1e09e 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 20 66 6f      /* Opcode fo
1e09f 72 20 75 70 64 61 74 65 20 68 6f 6f 6b 3a 20 53  r update hook: S
1e0a0 51 4c 49 54 45 5f 55 50 44 41 54 45 20 6f 72 20  QLITE_UPDATE or 
1e0a1 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 20 2a 2f  SQLITE_INSERT */
1e0a2 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1e0a3 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1e0a4 20 69 6e 74 6f 20 75 2e 62 66 20 2a 2f 0a 0a 20   into u.bf */.. 
1e0a5 20 75 2e 62 66 2e 70 44 61 74 61 20 3d 20 26 70   u.bf.pData = &p
1e0a6 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b  ->aMem[pOp->p2];
1e0a7 0a 20 20 75 2e 62 66 2e 70 4b 65 79 20 3d 20 26  .  u.bf.pKey = &
1e0a8 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  p->aMem[pOp->p3]
1e0a9 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1e0aa 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70  >p1>=0 && pOp->p
1e0ab 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  1<p->nCursor );.
1e0ac 20 20 75 2e 62 66 2e 70 43 20 3d 20 70 2d 3e 61    u.bf.pC = p->a
1e0ad 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  pCsr[pOp->p1];. 
1e0ae 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43   assert( u.bf.pC
1e0af 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
1e0b0 20 75 2e 62 66 2e 70 43 2d 3e 70 43 75 72 73 6f   u.bf.pC->pCurso
1e0b1 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  r!=0 );.  assert
1e0b2 28 20 75 2e 62 66 2e 70 43 2d 3e 70 73 65 75 64  ( u.bf.pC->pseud
1e0b3 6f 54 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0a  oTableReg==0 );.
1e0b4 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70    assert( u.bf.p
1e0b5 4b 65 79 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Key->flags & MEM
1e0b6 5f 49 6e 74 20 29 3b 0a 20 20 61 73 73 65 72 74  _Int );.  assert
1e0b7 28 20 75 2e 62 66 2e 70 43 2d 3e 69 73 54 61 62  ( u.bf.pC->isTab
1e0b8 6c 65 20 29 3b 0a 20 20 52 45 47 49 53 54 45 52  le );.  REGISTER
1e0b9 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20  _TRACE(pOp->p2, 
1e0ba 75 2e 62 66 2e 70 44 61 74 61 29 3b 0a 20 20 52  u.bf.pData);.  R
1e0bb 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
1e0bc 70 2d 3e 70 33 2c 20 75 2e 62 66 2e 70 4b 65 79  p->p3, u.bf.pKey
1e0bd 29 3b 0a 0a 20 20 75 2e 62 66 2e 69 4b 65 79 20  );..  u.bf.iKey 
1e0be 3d 20 75 2e 62 66 2e 70 4b 65 79 2d 3e 75 2e 69  = u.bf.pKey->u.i
1e0bf 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20  ;.  if( pOp->p5 
1e0c0 26 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45  & OPFLAG_NCHANGE
1e0c1 20 29 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b   ) p->nChange++;
1e0c2 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26  .  if( pOp->p5 &
1e0c3 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49   OPFLAG_LASTROWI
1e0c4 44 20 29 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69  D ) db->lastRowi
1e0c5 64 20 3d 20 75 2e 62 66 2e 70 4b 65 79 2d 3e 75  d = u.bf.pKey->u
1e0c6 2e 69 3b 0a 20 20 69 66 28 20 75 2e 62 66 2e 70  .i;.  if( u.bf.p
1e0c7 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 4d 45  Data->flags & ME
1e0c8 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 75 2e  M_Null ){.    u.
1e0c9 62 66 2e 70 44 61 74 61 2d 3e 7a 20 3d 20 30 3b  bf.pData->z = 0;
1e0ca 0a 20 20 20 20 75 2e 62 66 2e 70 44 61 74 61 2d  .    u.bf.pData-
1e0cb 3e 6e 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  >n = 0;.  }else{
1e0cc 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62  .    assert( u.b
1e0cd 66 2e 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 26  f.pData->flags &
1e0ce 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53   (MEM_Blob|MEM_S
1e0cf 74 72 29 20 29 3b 0a 20 20 7d 0a 20 20 75 2e 62  tr) );.  }.  u.b
1e0d0 66 2e 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 28  f.seekResult = (
1e0d1 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41  (pOp->p5 & OPFLA
1e0d2 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29  G_USESEEKRESULT)
1e0d3 20 3f 20 75 2e 62 66 2e 70 43 2d 3e 73 65 65 6b   ? u.bf.pC->seek
1e0d4 52 65 73 75 6c 74 20 3a 20 30 29 3b 0a 20 20 69  Result : 0);.  i
1e0d5 66 28 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 66  f( u.bf.pData->f
1e0d6 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20  lags & MEM_Zero 
1e0d7 29 7b 0a 20 20 20 20 75 2e 62 66 2e 6e 5a 65 72  ){.    u.bf.nZer
1e0d8 6f 20 3d 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e  o = u.bf.pData->
1e0d9 75 2e 6e 5a 65 72 6f 3b 0a 20 20 7d 65 6c 73 65  u.nZero;.  }else
1e0da 7b 0a 20 20 20 20 75 2e 62 66 2e 6e 5a 65 72 6f  {.    u.bf.nZero
1e0db 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69   = 0;.  }.  sqli
1e0dc 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65  te3BtreeSetCache
1e0dd 64 52 6f 77 69 64 28 75 2e 62 66 2e 70 43 2d 3e  dRowid(u.bf.pC->
1e0de 70 43 75 72 73 6f 72 2c 20 30 29 3b 0a 20 20 72  pCursor, 0);.  r
1e0df 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1e0e0 49 6e 73 65 72 74 28 75 2e 62 66 2e 70 43 2d 3e  Insert(u.bf.pC->
1e0e1 70 43 75 72 73 6f 72 2c 20 30 2c 20 75 2e 62 66  pCursor, 0, u.bf
1e0e2 2e 69 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20  .iKey,.         
1e0e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e0e4 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 7a 2c 20   u.bf.pData->z, 
1e0e5 75 2e 62 66 2e 70 44 61 74 61 2d 3e 6e 2c 20 75  u.bf.pData->n, u
1e0e6 2e 62 66 2e 6e 5a 65 72 6f 2c 0a 20 20 20 20 20  .bf.nZero,.     
1e0e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e0e8 20 20 20 20 20 70 4f 70 2d 3e 70 35 20 26 20 4f       pOp->p5 & O
1e0e9 50 46 4c 41 47 5f 41 50 50 45 4e 44 2c 20 75 2e  PFLAG_APPEND, u.
1e0ea 62 66 2e 73 65 65 6b 52 65 73 75 6c 74 0a 20 20  bf.seekResult.  
1e0eb 29 3b 0a 20 20 75 2e 62 66 2e 70 43 2d 3e 72 6f  );.  u.bf.pC->ro
1e0ec 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a  widIsValid = 0;.
1e0ed 20 20 75 2e 62 66 2e 70 43 2d 3e 64 65 66 65 72    u.bf.pC->defer
1e0ee 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20  redMoveto = 0;. 
1e0ef 20 75 2e 62 66 2e 70 43 2d 3e 63 61 63 68 65 53   u.bf.pC->cacheS
1e0f0 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54  tatus = CACHE_ST
1e0f1 41 4c 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b  ALE;..  /* Invok
1e0f2 65 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f  e the update-hoo
1e0f3 6b 20 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a  k if required. *
1e0f4 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
1e0f5 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70  TE_OK && db->xUp
1e0f6 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20  dateCallback && 
1e0f7 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20  pOp->p4.z ){.   
1e0f8 20 75 2e 62 66 2e 7a 44 62 20 3d 20 64 62 2d 3e   u.bf.zDb = db->
1e0f9 61 44 62 5b 75 2e 62 66 2e 70 43 2d 3e 69 44 62  aDb[u.bf.pC->iDb
1e0fa 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 75 2e 62  ].zName;.    u.b
1e0fb 66 2e 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e 70 34  f.zTbl = pOp->p4
1e0fc 2e 7a 3b 0a 20 20 20 20 75 2e 62 66 2e 6f 70 20  .z;.    u.bf.op 
1e0fd 3d 20 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50  = ((pOp->p5 & OP
1e0fe 46 4c 41 47 5f 49 53 55 50 44 41 54 45 29 20 3f  FLAG_ISUPDATE) ?
1e0ff 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 20 3a   SQLITE_UPDATE :
1e100 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 29 3b   SQLITE_INSERT);
1e101 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62  .    assert( u.b
1e102 66 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b  f.pC->isTable );
1e103 0a 20 20 20 20 64 62 2d 3e 78 55 70 64 61 74 65  .    db->xUpdate
1e104 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55 70  Callback(db->pUp
1e105 64 61 74 65 41 72 67 2c 20 75 2e 62 66 2e 6f 70  dateArg, u.bf.op
1e106 2c 20 75 2e 62 66 2e 7a 44 62 2c 20 75 2e 62 66  , u.bf.zDb, u.bf
1e107 2e 7a 54 62 6c 2c 20 75 2e 62 66 2e 69 4b 65 79  .zTbl, u.bf.iKey
1e108 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  );.    assert( u
1e109 2e 62 66 2e 70 43 2d 3e 69 44 62 3e 3d 30 20 29  .bf.pC->iDb>=0 )
1e10a 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1e10b 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 65 6c  ../* Opcode: Del
1e10c 65 74 65 20 50 31 20 50 32 20 2a 20 50 34 20 2a  ete P1 P2 * P4 *
1e10d 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68  .**.** Delete th
1e10e 65 20 72 65 63 6f 72 64 20 61 74 20 77 68 69 63  e record at whic
1e10f 68 20 74 68 65 20 50 31 20 63 75 72 73 6f 72 20  h the P1 cursor 
1e110 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  is currently poi
1e111 6e 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  nting..**.** The
1e112 20 63 75 72 73 6f 72 20 77 69 6c 6c 20 62 65 20   cursor will be 
1e113 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74  left pointing at
1e114 20 65 69 74 68 65 72 20 74 68 65 20 6e 65 78 74   either the next
1e115 20 6f 72 20 74 68 65 20 70 72 65 76 69 6f 75 73   or the previous
1e116 0a 2a 2a 20 72 65 63 6f 72 64 20 69 6e 20 74 68  .** record in th
1e117 65 20 74 61 62 6c 65 2e 20 49 66 20 69 74 20 69  e table. If it i
1e118 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20  s left pointing 
1e119 61 74 20 74 68 65 20 6e 65 78 74 20 72 65 63 6f  at the next reco
1e11a 72 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20  rd, then.** the 
1e11b 6e 65 78 74 20 4e 65 78 74 20 69 6e 73 74 72 75  next Next instru
1e11c 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 61 20  ction will be a 
1e11d 6e 6f 2d 6f 70 2e 20 20 48 65 6e 63 65 20 69 74  no-op.  Hence it
1e11e 20 69 73 20 4f 4b 20 74 6f 20 64 65 6c 65 74 65   is OK to delete
1e11f 0a 2a 2a 20 61 20 72 65 63 6f 72 64 20 66 72 6f  .** a record fro
1e120 6d 20 77 69 74 68 69 6e 20 61 6e 20 4e 65 78 74  m within an Next
1e121 20 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   loop..**.** If 
1e122 74 68 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e  the OPFLAG_NCHAN
1e123 47 45 20 66 6c 61 67 20 6f 66 20 50 32 20 69 73  GE flag of P2 is
1e124 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72   set, then the r
1e125 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20  ow change count 
1e126 69 73 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 65  is.** incremente
1e127 64 20 28 6f 74 68 65 72 77 69 73 65 20 6e 6f 74  d (otherwise not
1e128 29 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 6d 75 73 74  )..**.** P1 must
1e129 20 6e 6f 74 20 62 65 20 70 73 65 75 64 6f 2d 74   not be pseudo-t
1e12a 61 62 6c 65 2e 20 20 49 74 20 68 61 73 20 74 6f  able.  It has to
1e12b 20 62 65 20 61 20 72 65 61 6c 20 74 61 62 6c 65   be a real table
1e12c 20 77 69 74 68 0a 2a 2a 20 6d 75 6c 74 69 70 6c   with.** multipl
1e12d 65 20 72 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  e rows..**.** If
1e12e 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c   P4 is not NULL,
1e12f 20 74 68 65 6e 20 69 74 20 69 73 20 74 68 65 20   then it is the 
1e130 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
1e131 65 20 74 68 61 74 20 50 31 20 69 73 0a 2a 2a 20  e that P1 is.** 
1e132 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68  pointing to.  Th
1e133 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 77 69  e update hook wi
1e134 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 2c 20 69  ll be invoked, i
1e135 66 20 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 20  f it exists..** 
1e136 49 66 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c  If P4 is not NUL
1e137 4c 20 74 68 65 6e 20 74 68 65 20 50 31 20 63 75  L then the P1 cu
1e138 72 73 6f 72 20 6d 75 73 74 20 68 61 76 65 20 62  rsor must have b
1e139 65 65 6e 20 70 6f 73 69 74 69 6f 6e 65 64 0a 2a  een positioned.*
1e13a 2a 20 75 73 69 6e 67 20 4f 50 5f 4e 6f 74 46 6f  * using OP_NotFo
1e13b 75 6e 64 20 70 72 69 6f 72 20 74 6f 20 69 6e 76  und prior to inv
1e13c 6f 6b 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64  oking this opcod
1e13d 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 65  e..*/.case OP_De
1e13e 6c 65 74 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f  lete: {.#if 0  /
1e13f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1e140 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1e141 67 20 2a 2f 0a 20 20 69 36 34 20 69 4b 65 79 3b  g */.  i64 iKey;
1e142 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
1e143 43 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  C;.#endif /* loc
1e144 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e145 65 64 20 69 6e 74 6f 20 75 2e 62 67 20 2a 2f 0a  ed into u.bg */.
1e146 0a 20 20 75 2e 62 67 2e 69 4b 65 79 20 3d 20 30  .  u.bg.iKey = 0
1e147 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1e148 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70  >p1>=0 && pOp->p
1e149 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  1<p->nCursor );.
1e14a 20 20 75 2e 62 67 2e 70 43 20 3d 20 70 2d 3e 61    u.bg.pC = p->a
1e14b 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  pCsr[pOp->p1];. 
1e14c 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43   assert( u.bg.pC
1e14d 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
1e14e 20 75 2e 62 67 2e 70 43 2d 3e 70 43 75 72 73 6f   u.bg.pC->pCurso
1e14f 72 21 3d 30 20 29 3b 20 20 2f 2a 20 4f 6e 6c 79  r!=0 );  /* Only
1e150 20 76 61 6c 69 64 20 66 6f 72 20 72 65 61 6c 20   valid for real 
1e151 74 61 62 6c 65 73 2c 20 6e 6f 20 70 73 65 75 64  tables, no pseud
1e152 6f 74 61 62 6c 65 73 20 2a 2f 0a 0a 20 20 2f 2a  otables */..  /*
1e153 20 49 66 20 74 68 65 20 75 70 64 61 74 65 2d 68   If the update-h
1e154 6f 6f 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f  ook will be invo
1e155 6b 65 64 2c 20 73 65 74 20 75 2e 62 67 2e 69 4b  ked, set u.bg.iK
1e156 65 79 20 74 6f 20 74 68 65 20 72 6f 77 69 64 20  ey to the rowid 
1e157 6f 66 20 74 68 65 0a 20 20 2a 2a 20 72 6f 77 20  of the.  ** row 
1e158 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a 20  being deleted.. 
1e159 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 78 55   */.  if( db->xU
1e15a 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26  pdateCallback &&
1e15b 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20   pOp->p4.z ){.  
1e15c 20 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70    assert( u.bg.p
1e15d 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20  C->isTable );.  
1e15e 20 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70    assert( u.bg.p
1e15f 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20  C->rowidIsValid 
1e160 29 3b 20 20 2f 2a 20 6c 61 73 74 52 6f 77 69 64  );  /* lastRowid
1e161 20 73 65 74 20 62 79 20 70 72 65 76 69 6f 75 73   set by previous
1e162 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 2a 2f 0a   OP_NotFound */.
1e163 20 20 20 20 75 2e 62 67 2e 69 4b 65 79 20 3d 20      u.bg.iKey = 
1e164 75 2e 62 67 2e 70 43 2d 3e 6c 61 73 74 52 6f 77  u.bg.pC->lastRow
1e165 69 64 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68  id;.  }..  /* Th
1e166 65 20 4f 50 5f 44 65 6c 65 74 65 20 6f 70 63 6f  e OP_Delete opco
1e167 64 65 20 61 6c 77 61 79 73 20 66 6f 6c 6c 6f 77  de always follow
1e168 73 20 61 6e 20 4f 50 5f 4e 6f 74 45 78 69 73 74  s an OP_NotExist
1e169 73 20 6f 72 20 4f 50 5f 4c 61 73 74 20 6f 72 0a  s or OP_Last or.
1e16a 20 20 2a 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f    ** OP_Column o
1e16b 6e 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65  n the same table
1e16c 20 77 69 74 68 6f 75 74 20 61 6e 79 20 69 6e 74   without any int
1e16d 65 72 76 65 6e 69 6e 67 20 6f 70 65 72 61 74 69  ervening operati
1e16e 6f 6e 73 20 74 68 61 74 0a 20 20 2a 2a 20 6d 69  ons that.  ** mi
1e16f 67 68 74 20 6d 6f 76 65 20 6f 72 20 69 6e 76 61  ght move or inva
1e170 6c 69 64 61 74 65 20 74 68 65 20 63 75 72 73 6f  lidate the curso
1e171 72 2e 20 20 48 65 6e 63 65 20 63 75 72 73 6f 72  r.  Hence cursor
1e172 20 75 2e 62 67 2e 70 43 20 69 73 20 61 6c 77 61   u.bg.pC is alwa
1e173 79 73 20 70 6f 69 6e 74 69 6e 67 0a 20 20 2a 2a  ys pointing.  **
1e174 20 74 6f 20 74 68 65 20 72 6f 77 20 74 6f 20 62   to the row to b
1e175 65 20 64 65 6c 65 74 65 64 20 61 6e 64 20 74 68  e deleted and th
1e176 65 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  e sqlite3VdbeCur
1e177 73 6f 72 4d 6f 76 65 74 6f 28 29 20 6f 70 65 72  sorMoveto() oper
1e178 61 74 69 6f 6e 0a 20 20 2a 2a 20 62 65 6c 6f 77  ation.  ** below
1e179 20 69 73 20 61 6c 77 61 79 73 20 61 20 6e 6f 2d   is always a no-
1e17a 6f 70 20 61 6e 64 20 63 61 6e 6e 6f 74 20 66 61  op and cannot fa
1e17b 69 6c 2e 20 20 57 65 20 77 69 6c 6c 20 72 75 6e  il.  We will run
1e17c 20 69 74 20 61 6e 79 68 6f 77 2c 20 74 68 6f 75   it anyhow, thou
1e17d 67 68 2c 0a 20 20 2a 2a 20 74 6f 20 67 75 61 72  gh,.  ** to guar
1e17e 64 20 61 67 61 69 6e 73 74 20 66 75 74 75 72 65  d against future
1e17f 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20   changes to the 
1e180 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 2e 0a  code generator..
1e181 20 20 2a 2a 2f 0a 20 20 61 73 73 65 72 74 28 20    **/.  assert( 
1e182 75 2e 62 67 2e 70 43 2d 3e 64 65 66 65 72 72 65  u.bg.pC->deferre
1e183 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20  dMoveto==0 );.  
1e184 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
1e185 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e 62  CursorMoveto(u.b
1e186 67 2e 70 43 29 3b 0a 20 20 69 66 28 20 4e 45 56  g.pC);.  if( NEV
1e187 45 52 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  ER(rc!=SQLITE_OK
1e188 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  ) ) goto abort_d
1e189 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 0a 20 20  ue_to_error;..  
1e18a 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43  sqlite3BtreeSetC
1e18b 61 63 68 65 64 52 6f 77 69 64 28 75 2e 62 67 2e  achedRowid(u.bg.
1e18c 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 29 3b  pC->pCursor, 0);
1e18d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
1e18e 74 72 65 65 44 65 6c 65 74 65 28 75 2e 62 67 2e  treeDelete(u.bg.
1e18f 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20  pC->pCursor);.  
1e190 75 2e 62 67 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.bg.pC->cacheSt
1e191 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41  atus = CACHE_STA
1e192 4c 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65  LE;..  /* Invoke
1e193 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b   the update-hook
1e194 20 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f   if required. */
1e195 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1e196 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64  E_OK && db->xUpd
1e197 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70  ateCallback && p
1e198 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20  Op->p4.z ){.    
1e199 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20  const char *zDb 
1e19a 3d 20 64 62 2d 3e 61 44 62 5b 75 2e 62 67 2e 70  = db->aDb[u.bg.p
1e19b 43 2d 3e 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20  C->iDb].zName;. 
1e19c 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
1e19d 54 62 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b  Tbl = pOp->p4.z;
1e19e 0a 20 20 20 20 64 62 2d 3e 78 55 70 64 61 74 65  .    db->xUpdate
1e19f 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55 70  Callback(db->pUp
1e1a0 64 61 74 65 41 72 67 2c 20 53 51 4c 49 54 45 5f  dateArg, SQLITE_
1e1a1 44 45 4c 45 54 45 2c 20 7a 44 62 2c 20 7a 54 62  DELETE, zDb, zTb
1e1a2 6c 2c 20 75 2e 62 67 2e 69 4b 65 79 29 3b 0a 20  l, u.bg.iKey);. 
1e1a3 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e     assert( u.bg.
1e1a4 70 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20  pC->iDb>=0 );.  
1e1a5 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20  }.  if( pOp->p2 
1e1a6 26 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45  & OPFLAG_NCHANGE
1e1a7 20 29 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b   ) p->nChange++;
1e1a8 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 2f 2a 20 4f  .  break;.}./* O
1e1a9 70 63 6f 64 65 3a 20 52 65 73 65 74 43 6f 75 6e  pcode: ResetCoun
1e1aa 74 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  t * * * * *.**.*
1e1ab 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74  * The value of t
1e1ac 68 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65  he change counte
1e1ad 72 20 69 73 20 63 6f 70 69 65 64 20 74 6f 20 74  r is copied to t
1e1ae 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
1e1af 6c 65 0a 2a 2a 20 63 68 61 6e 67 65 20 63 6f 75  le.** change cou
1e1b0 6e 74 65 72 20 28 72 65 74 75 72 6e 65 64 20 62  nter (returned b
1e1b1 79 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c  y subsequent cal
1e1b2 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 68  ls to sqlite3_ch
1e1b3 61 6e 67 65 73 28 29 29 2e 0a 2a 2a 20 54 68 65  anges())..** The
1e1b4 6e 20 74 68 65 20 56 4d 73 20 69 6e 74 65 72 6e  n the VMs intern
1e1b5 61 6c 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65  al change counte
1e1b6 72 20 72 65 73 65 74 73 20 74 6f 20 30 2e 0a 2a  r resets to 0..*
1e1b7 2a 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62  * This is used b
1e1b8 79 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61  y trigger progra
1e1b9 6d 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52  ms..*/.case OP_R
1e1ba 65 73 65 74 43 6f 75 6e 74 3a 20 7b 0a 20 20 73  esetCount: {.  s
1e1bb 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61  qlite3VdbeSetCha
1e1bc 6e 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61  nges(db, p->nCha
1e1bd 6e 67 65 29 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e  nge);.  p->nChan
1e1be 67 65 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b  ge = 0;.  break;
1e1bf 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52  .}../* Opcode: R
1e1c0 6f 77 44 61 74 61 20 50 31 20 50 32 20 2a 20 2a  owData P1 P2 * *
1e1c1 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69   *.**.** Write i
1e1c2 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 20  nto register P2 
1e1c3 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77  the complete row
1e1c4 20 64 61 74 61 20 66 6f 72 20 63 75 72 73 6f 72   data for cursor
1e1c5 20 50 31 2e 0a 2a 2a 20 54 68 65 72 65 20 69 73   P1..** There is
1e1c6 20 6e 6f 20 69 6e 74 65 72 70 72 65 74 61 74 69   no interpretati
1e1c7 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 2e 20  on of the data. 
1e1c8 20 0a 2a 2a 20 49 74 20 69 73 20 6a 75 73 74 20   .** It is just 
1e1c9 63 6f 70 69 65 64 20 6f 6e 74 6f 20 74 68 65 20  copied onto the 
1e1ca 50 32 20 72 65 67 69 73 74 65 72 20 65 78 61 63  P2 register exac
1e1cb 74 6c 79 20 61 73 20 0a 2a 2a 20 69 74 20 69 73  tly as .** it is
1e1cc 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61   found in the da
1e1cd 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a  tabase file..**.
1e1ce 2a 2a 20 49 66 20 74 68 65 20 50 31 20 63 75 72  ** If the P1 cur
1e1cf 73 6f 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e  sor must be poin
1e1d0 74 69 6e 67 20 74 6f 20 61 20 76 61 6c 69 64 20  ting to a valid 
1e1d1 72 6f 77 20 28 6e 6f 74 20 61 20 4e 55 4c 4c 20  row (not a NULL 
1e1d2 72 6f 77 29 0a 2a 2a 20 6f 66 20 61 20 72 65 61  row).** of a rea
1e1d3 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70  l table, not a p
1e1d4 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a  seudo-table..*/.
1e1d5 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 4b 65  /* Opcode: RowKe
1e1d6 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  y P1 P2 * * *.**
1e1d7 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72  .** Write into r
1e1d8 65 67 69 73 74 65 72 20 50 32 20 74 68 65 20 63  egister P2 the c
1e1d9 6f 6d 70 6c 65 74 65 20 72 6f 77 20 6b 65 79 20  omplete row key 
1e1da 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a  for cursor P1..*
1e1db 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 69 6e  * There is no in
1e1dc 74 65 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 20  terpretation of 
1e1dd 74 68 65 20 64 61 74 61 2e 20 20 0a 2a 2a 20 54  the data.  .** T
1e1de 68 65 20 6b 65 79 20 69 73 20 63 6f 70 69 65 64  he key is copied
1e1df 20 6f 6e 74 6f 20 74 68 65 20 50 33 20 72 65 67   onto the P3 reg
1e1e0 69 73 74 65 72 20 65 78 61 63 74 6c 79 20 61 73  ister exactly as
1e1e1 20 0a 2a 2a 20 69 74 20 69 73 20 66 6f 75 6e 64   .** it is found
1e1e2 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
1e1e3 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   file..**.** If 
1e1e4 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75  the P1 cursor mu
1e1e5 73 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74  st be pointing t
1e1e6 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 20 28 6e  o a valid row (n
1e1e7 6f 74 20 61 20 4e 55 4c 4c 20 72 6f 77 29 0a 2a  ot a NULL row).*
1e1e8 2a 20 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c  * of a real tabl
1e1e9 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d  e, not a pseudo-
1e1ea 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f  table..*/.case O
1e1eb 50 5f 52 6f 77 4b 65 79 3a 0a 63 61 73 65 20 4f  P_RowKey:.case O
1e1ec 50 5f 52 6f 77 44 61 74 61 3a 20 7b 0a 23 69 66  P_RowData: {.#if
1e1ed 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1e1ee 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e1ef 6f 20 75 2e 62 68 20 2a 2f 0a 20 20 56 64 62 65  o u.bh */.  Vdbe
1e1f0 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74  Cursor *pC;.  Bt
1e1f1 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20  Cursor *pCrsr;. 
1e1f2 20 75 33 32 20 6e 3b 0a 20 20 69 36 34 20 6e 36   u32 n;.  i64 n6
1e1f3 34 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  4;.#endif /* loc
1e1f4 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e1f5 65 64 20 69 6e 74 6f 20 75 2e 62 68 20 2a 2f 0a  ed into u.bh */.
1e1f6 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d  .  pOut = &p->aM
1e1f7 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 0a 20 20  em[pOp->p2];..  
1e1f8 2f 2a 20 4e 6f 74 65 20 74 68 61 74 20 52 6f 77  /* Note that Row
1e1f9 4b 65 79 20 61 6e 64 20 52 6f 77 44 61 74 61 20  Key and RowData 
1e1fa 61 72 65 20 72 65 61 6c 6c 79 20 65 78 61 63 74  are really exact
1e1fb 6c 79 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74  ly the same inst
1e1fc 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73  ruction */.  ass
1e1fd 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20  ert( pOp->p1>=0 
1e1fe 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43  && pOp->p1<p->nC
1e1ff 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 68 2e  ursor );.  u.bh.
1e200 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f  pC = p->apCsr[pO
1e201 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74  p->p1];.  assert
1e202 28 20 75 2e 62 68 2e 70 43 2d 3e 69 73 54 61 62  ( u.bh.pC->isTab
1e203 6c 65 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64  le || pOp->opcod
1e204 65 3d 3d 4f 50 5f 52 6f 77 4b 65 79 20 29 3b 0a  e==OP_RowKey );.
1e205 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70    assert( u.bh.p
1e206 43 2d 3e 69 73 49 6e 64 65 78 20 7c 7c 20 70 4f  C->isIndex || pO
1e207 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f  p->opcode==OP_Ro
1e208 77 44 61 74 61 20 29 3b 0a 20 20 61 73 73 65 72  wData );.  asser
1e209 74 28 20 75 2e 62 68 2e 70 43 21 3d 30 20 29 3b  t( u.bh.pC!=0 );
1e20a 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e  .  assert( u.bh.
1e20b 70 43 2d 3e 6e 75 6c 6c 52 6f 77 3d 3d 30 20 29  pC->nullRow==0 )
1e20c 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 68  ;.  assert( u.bh
1e20d 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65  .pC->pseudoTable
1e20e 52 65 67 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  Reg==0 );.  asse
1e20f 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e 70 43 75  rt( u.bh.pC->pCu
1e210 72 73 6f 72 21 3d 30 20 29 3b 0a 20 20 75 2e 62  rsor!=0 );.  u.b
1e211 68 2e 70 43 72 73 72 20 3d 20 75 2e 62 68 2e 70  h.pCrsr = u.bh.p
1e212 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 61 73  C->pCursor;.  as
1e213 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
1e214 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28  eeCursorIsValid(
1e215 75 2e 62 68 2e 70 43 72 73 72 29 20 29 3b 0a 0a  u.bh.pCrsr) );..
1e216 20 20 2f 2a 20 54 68 65 20 4f 50 5f 52 6f 77 4b    /* The OP_RowK
1e217 65 79 20 61 6e 64 20 4f 50 5f 52 6f 77 44 61 74  ey and OP_RowDat
1e218 61 20 6f 70 63 6f 64 65 73 20 61 6c 77 61 79 73  a opcodes always
1e219 20 66 6f 6c 6c 6f 77 20 4f 50 5f 4e 6f 74 45 78   follow OP_NotEx
1e21a 69 73 74 73 20 6f 72 0a 20 20 2a 2a 20 4f 50 5f  ists or.  ** OP_
1e21b 52 65 77 69 6e 64 2f 4f 70 5f 4e 65 78 74 20 77  Rewind/Op_Next w
1e21c 69 74 68 20 6e 6f 20 69 6e 74 65 72 76 65 6e 69  ith no interveni
1e21d 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20  ng instructions 
1e21e 74 68 61 74 20 6d 69 67 68 74 20 69 6e 76 61 6c  that might inval
1e21f 69 64 61 74 65 0a 20 20 2a 2a 20 74 68 65 20 63  idate.  ** the c
1e220 75 72 73 6f 72 2e 20 20 48 65 6e 63 65 20 74 68  ursor.  Hence th
1e221 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 71 6c 69  e following sqli
1e222 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76  te3VdbeCursorMov
1e223 65 74 6f 28 29 20 63 61 6c 6c 20 69 73 20 61 6c  eto() call is al
1e224 77 61 79 73 0a 20 20 2a 2a 20 61 20 6e 6f 2d 6f  ways.  ** a no-o
1e225 70 20 61 6e 64 20 63 61 6e 20 6e 65 76 65 72 20  p and can never 
1e226 66 61 69 6c 2e 20 20 42 75 74 20 77 65 20 6c 65  fail.  But we le
1e227 61 76 65 20 69 74 20 69 6e 20 70 6c 61 63 65 20  ave it in place 
1e228 61 73 20 61 20 73 61 66 65 74 79 2e 0a 20 20 2a  as a safety..  *
1e229 2f 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 68  /.  assert( u.bh
1e22a 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76  .pC->deferredMov
1e22b 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d  eto==0 );.  rc =
1e22c 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73   sqlite3VdbeCurs
1e22d 6f 72 4d 6f 76 65 74 6f 28 75 2e 62 68 2e 70 43  orMoveto(u.bh.pC
1e22e 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72  );.  if( NEVER(r
1e22f 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 20  c!=SQLITE_OK) ) 
1e230 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1e231 6f 5f 65 72 72 6f 72 3b 0a 0a 20 20 69 66 28 20  o_error;..  if( 
1e232 75 2e 62 68 2e 70 43 2d 3e 69 73 49 6e 64 65 78  u.bh.pC->isIndex
1e233 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1e234 21 75 2e 62 68 2e 70 43 2d 3e 69 73 54 61 62 6c  !u.bh.pC->isTabl
1e235 65 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  e );.    rc = sq
1e236 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a  lite3BtreeKeySiz
1e237 65 28 75 2e 62 68 2e 70 43 72 73 72 2c 20 26 75  e(u.bh.pCrsr, &u
1e238 2e 62 68 2e 6e 36 34 29 3b 0a 20 20 20 20 61 73  .bh.n64);.    as
1e239 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
1e23a 5f 4f 4b 20 29 3b 20 20 20 20 2f 2a 20 54 72 75  _OK );    /* Tru
1e23b 65 20 62 65 63 61 75 73 65 20 6f 66 20 43 75 72  e because of Cur
1e23c 73 6f 72 4d 6f 76 65 74 6f 28 29 20 63 61 6c 6c  sorMoveto() call
1e23d 20 61 62 6f 76 65 20 2a 2f 0a 20 20 20 20 69 66   above */.    if
1e23e 28 20 75 2e 62 68 2e 6e 36 34 3e 64 62 2d 3e 61  ( u.bh.n64>db->a
1e23f 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
1e240 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20  IT_LENGTH] ){.  
1e241 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67      goto too_big
1e242 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 68  ;.    }.    u.bh
1e243 2e 6e 20 3d 20 28 75 33 32 29 75 2e 62 68 2e 6e  .n = (u32)u.bh.n
1e244 36 34 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  64;.  }else{.   
1e245 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1e246 65 65 44 61 74 61 53 69 7a 65 28 75 2e 62 68 2e  eeDataSize(u.bh.
1e247 70 43 72 73 72 2c 20 26 75 2e 62 68 2e 6e 29 3b  pCrsr, &u.bh.n);
1e248 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d  .    assert( rc=
1e249 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20  =SQLITE_OK );   
1e24a 20 2f 2a 20 44 61 74 61 53 69 7a 65 28 29 20 63   /* DataSize() c
1e24b 61 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20  annot fail */.  
1e24c 20 20 69 66 28 20 75 2e 62 68 2e 6e 3e 28 75 33    if( u.bh.n>(u3
1e24d 32 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  2)db->aLimit[SQL
1e24e 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
1e24f 5d 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ] ){.      goto 
1e250 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20  too_big;.    }. 
1e251 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
1e252 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74  VdbeMemGrow(pOut
1e253 2c 20 75 2e 62 68 2e 6e 2c 20 30 29 20 29 7b 0a  , u.bh.n, 0) ){.
1e254 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b      goto no_mem;
1e255 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d  .  }.  pOut->n =
1e256 20 75 2e 62 68 2e 6e 3b 0a 20 20 4d 65 6d 53 65   u.bh.n;.  MemSe
1e257 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
1e258 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 69 66 28  MEM_Blob);.  if(
1e259 20 75 2e 62 68 2e 70 43 2d 3e 69 73 49 6e 64 65   u.bh.pC->isInde
1e25a 78 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  x ){.    rc = sq
1e25b 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28 75 2e  lite3BtreeKey(u.
1e25c 62 68 2e 70 43 72 73 72 2c 20 30 2c 20 75 2e 62  bh.pCrsr, 0, u.b
1e25d 68 2e 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20  h.n, pOut->z);. 
1e25e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d   }else{.    rc =
1e25f 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74   sqlite3BtreeDat
1e260 61 28 75 2e 62 68 2e 70 43 72 73 72 2c 20 30 2c  a(u.bh.pCrsr, 0,
1e261 20 75 2e 62 68 2e 6e 2c 20 70 4f 75 74 2d 3e 7a   u.bh.n, pOut->z
1e262 29 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 65  );.  }.  pOut->e
1e263 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38  nc = SQLITE_UTF8
1e264 3b 20 20 2f 2a 20 49 6e 20 63 61 73 65 20 74 68  ;  /* In case th
1e265 65 20 62 6c 6f 62 20 69 73 20 65 76 65 72 20 63  e blob is ever c
1e266 61 73 74 20 74 6f 20 74 65 78 74 20 2a 2f 0a 20  ast to text */. 
1e267 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42   UPDATE_MAX_BLOB
1e268 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72  SIZE(pOut);.  br
1e269 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1e26a 65 3a 20 52 6f 77 69 64 20 50 31 20 50 32 20 2a  e: Rowid P1 P2 *
1e26b 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65   * *.**.** Store
1e26c 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20   in register P2 
1e26d 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68  an integer which
1e26e 20 69 73 20 74 68 65 20 6b 65 79 20 6f 66 20 74   is the key of t
1e26f 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20 74  he table entry t
1e270 68 61 74 0a 2a 2a 20 50 31 20 69 73 20 63 75 72  hat.** P1 is cur
1e271 72 65 6e 74 6c 79 20 70 6f 69 6e 74 20 74 6f 2e  rently point to.
1e272 0a 2a 2a 0a 2a 2a 20 50 31 20 63 61 6e 20 62 65  .**.** P1 can be
1e273 20 65 69 74 68 65 72 20 61 6e 20 6f 72 64 69 6e   either an ordin
1e274 61 72 79 20 74 61 62 6c 65 20 6f 72 20 61 20 76  ary table or a v
1e275 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54  irtual table.  T
1e276 68 65 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20  here used to.** 
1e277 62 65 20 61 20 73 65 70 61 72 61 74 65 20 4f 50  be a separate OP
1e278 5f 56 52 6f 77 69 64 20 6f 70 63 6f 64 65 20 66  _VRowid opcode f
1e279 6f 72 20 75 73 65 20 77 69 74 68 20 76 69 72 74  or use with virt
1e27a 75 61 6c 20 74 61 62 6c 65 73 2c 20 62 75 74 20  ual tables, but 
1e27b 74 68 69 73 0a 2a 2a 20 6f 6e 65 20 6f 70 63 6f  this.** one opco
1e27c 64 65 20 6e 6f 77 20 77 6f 72 6b 73 20 66 6f 72  de now works for
1e27d 20 62 6f 74 68 20 74 61 62 6c 65 20 74 79 70 65   both table type
1e27e 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f  s..*/.case OP_Ro
1e27f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20  wid: {          
1e280 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70         /* out2-p
1e281 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66  rerelease */.#if
1e282 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1e283 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e284 6f 20 75 2e 62 69 20 2a 2f 0a 20 20 56 64 62 65  o u.bi */.  Vdbe
1e285 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 36  Cursor *pC;.  i6
1e286 34 20 76 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76  4 v;.  sqlite3_v
1e287 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f  tab *pVtab;.  co
1e288 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
1e289 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 23 65 6e  le *pModule;.#en
1e28a 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1e28b 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e28c 6f 20 75 2e 62 69 20 2a 2f 0a 0a 20 20 61 73 73  o u.bi */..  ass
1e28d 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20  ert( pOp->p1>=0 
1e28e 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43  && pOp->p1<p->nC
1e28f 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 69 2e  ursor );.  u.bi.
1e290 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f  pC = p->apCsr[pO
1e291 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74  p->p1];.  assert
1e292 28 20 75 2e 62 69 2e 70 43 21 3d 30 20 29 3b 0a  ( u.bi.pC!=0 );.
1e293 20 20 61 73 73 65 72 74 28 20 75 2e 62 69 2e 70    assert( u.bi.p
1e294 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65  C->pseudoTableRe
1e295 67 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 75 2e  g==0 );.  if( u.
1e296 62 69 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29  bi.pC->nullRow )
1e297 7b 0a 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68  {.    /* Do noth
1e298 69 6e 67 20 73 6f 20 74 68 61 74 20 72 65 67 5b  ing so that reg[
1e299 50 32 5d 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c  P2] remains NULL
1e29a 20 2a 2f 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20   */.    break;. 
1e29b 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 62 69 2e   }else if( u.bi.
1e29c 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65  pC->deferredMove
1e29d 74 6f 20 29 7b 0a 20 20 20 20 75 2e 62 69 2e 76  to ){.    u.bi.v
1e29e 20 3d 20 75 2e 62 69 2e 70 43 2d 3e 6d 6f 76 65   = u.bi.pC->move
1e29f 74 6f 54 61 72 67 65 74 3b 0a 23 69 66 6e 64 65  toTarget;.#ifnde
1e2a0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
1e2a1 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 7d 65 6c  RTUALTABLE.  }el
1e2a2 73 65 20 69 66 28 20 75 2e 62 69 2e 70 43 2d 3e  se if( u.bi.pC->
1e2a3 70 56 74 61 62 43 75 72 73 6f 72 20 29 7b 0a 20  pVtabCursor ){. 
1e2a4 20 20 20 75 2e 62 69 2e 70 56 74 61 62 20 3d 20     u.bi.pVtab = 
1e2a5 75 2e 62 69 2e 70 43 2d 3e 70 56 74 61 62 43 75  u.bi.pC->pVtabCu
1e2a6 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20 20  rsor->pVtab;.   
1e2a7 20 75 2e 62 69 2e 70 4d 6f 64 75 6c 65 20 3d 20   u.bi.pModule = 
1e2a8 75 2e 62 69 2e 70 56 74 61 62 2d 3e 70 4d 6f 64  u.bi.pVtab->pMod
1e2a9 75 6c 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ule;.    assert(
1e2aa 20 75 2e 62 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78   u.bi.pModule->x
1e2ab 52 6f 77 69 64 20 29 3b 0a 20 20 20 20 69 66 28  Rowid );.    if(
1e2ac 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
1e2ad 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  f(db) ) goto abo
1e2ae 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1e2af 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e 62 69 2e  ;.    rc = u.bi.
1e2b0 70 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64 28  pModule->xRowid(
1e2b1 75 2e 62 69 2e 70 43 2d 3e 70 56 74 61 62 43 75  u.bi.pC->pVtabCu
1e2b2 72 73 6f 72 2c 20 26 75 2e 62 69 2e 76 29 3b 0a  rsor, &u.bi.v);.
1e2b3 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1e2b4 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67  e(db, p->zErrMsg
1e2b5 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73  );.    p->zErrMs
1e2b6 67 20 3d 20 75 2e 62 69 2e 70 56 74 61 62 2d 3e  g = u.bi.pVtab->
1e2b7 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 75 2e 62  zErrMsg;.    u.b
1e2b8 69 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67  i.pVtab->zErrMsg
1e2b9 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71   = 0;.    if( sq
1e2ba 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
1e2bb 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  ) ) goto abort_d
1e2bc 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 23 65  ue_to_misuse;.#e
1e2bd 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1e2be 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1e2bf 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   */.  }else{.   
1e2c0 20 61 73 73 65 72 74 28 20 75 2e 62 69 2e 70 43   assert( u.bi.pC
1e2c1 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 3b 0a  ->pCursor!=0 );.
1e2c2 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1e2c3 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f  VdbeCursorMoveto
1e2c4 28 75 2e 62 69 2e 70 43 29 3b 0a 20 20 20 20 69  (u.bi.pC);.    i
1e2c5 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f  f( rc ) goto abo
1e2c6 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
1e2c7 0a 20 20 20 20 69 66 28 20 75 2e 62 69 2e 70 43  .    if( u.bi.pC
1e2c8 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 29  ->rowidIsValid )
1e2c9 7b 0a 20 20 20 20 20 20 75 2e 62 69 2e 76 20 3d  {.      u.bi.v =
1e2ca 20 75 2e 62 69 2e 70 43 2d 3e 6c 61 73 74 52 6f   u.bi.pC->lastRo
1e2cb 77 69 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  wid;.    }else{.
1e2cc 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1e2cd 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 75  e3BtreeKeySize(u
1e2ce 2e 62 69 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c  .bi.pC->pCursor,
1e2cf 20 26 75 2e 62 69 2e 76 29 3b 0a 20 20 20 20 20   &u.bi.v);.     
1e2d0 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
1e2d1 49 54 45 5f 4f 4b 20 29 3b 20 20 2f 2a 20 41 6c  ITE_OK );  /* Al
1e2d2 77 61 79 73 20 73 6f 20 62 65 63 61 75 73 65 20  ways so because 
1e2d3 6f 66 20 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28  of CursorMoveto(
1e2d4 29 20 61 62 6f 76 65 20 2a 2f 0a 20 20 20 20 7d  ) above */.    }
1e2d5 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e 69  .  }.  pOut->u.i
1e2d6 20 3d 20 75 2e 62 69 2e 76 3b 0a 20 20 4d 65 6d   = u.bi.v;.  Mem
1e2d7 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74  SetTypeFlag(pOut
1e2d8 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72  , MEM_Int);.  br
1e2d9 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1e2da 65 3a 20 4e 75 6c 6c 52 6f 77 20 50 31 20 2a 20  e: NullRow P1 * 
1e2db 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65  * * *.**.** Move
1e2dc 20 74 68 65 20 63 75 72 73 6f 72 20 50 31 20 74   the cursor P1 t
1e2dd 6f 20 61 20 6e 75 6c 6c 20 72 6f 77 2e 20 20 41  o a null row.  A
1e2de 6e 79 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 65  ny OP_Column ope
1e2df 72 61 74 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20  rations.** that 
1e2e0 6f 63 63 75 72 20 77 68 69 6c 65 20 74 68 65 20  occur while the 
1e2e1 63 75 72 73 6f 72 20 69 73 20 6f 6e 20 74 68 65  cursor is on the
1e2e2 20 6e 75 6c 6c 20 72 6f 77 20 77 69 6c 6c 20 61   null row will a
1e2e3 6c 77 61 79 73 0a 2a 2a 20 77 72 69 74 65 20 61  lways.** write a
1e2e4 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f   NULL..*/.case O
1e2e5 50 5f 4e 75 6c 6c 52 6f 77 3a 20 7b 0a 23 69 66  P_NullRow: {.#if
1e2e6 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1e2e7 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e2e8 6f 20 75 2e 62 6a 20 2a 2f 0a 20 20 56 64 62 65  o u.bj */.  Vdbe
1e2e9 43 75 72 73 6f 72 20 2a 70 43 3b 0a 23 65 6e 64  Cursor *pC;.#end
1e2ea 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1e2eb 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e2ec 20 75 2e 62 6a 20 2a 2f 0a 0a 20 20 61 73 73 65   u.bj */..  asse
1e2ed 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1e2ee 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75  & pOp->p1<p->nCu
1e2ef 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6a 2e 70  rsor );.  u.bj.p
1e2f0 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  C = p->apCsr[pOp
1e2f1 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
1e2f2 20 75 2e 62 6a 2e 70 43 21 3d 30 20 29 3b 0a 20   u.bj.pC!=0 );. 
1e2f3 20 75 2e 62 6a 2e 70 43 2d 3e 6e 75 6c 6c 52 6f   u.bj.pC->nullRo
1e2f4 77 20 3d 20 31 3b 0a 20 20 75 2e 62 6a 2e 70 43  w = 1;.  u.bj.pC
1e2f5 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d  ->rowidIsValid =
1e2f6 20 30 3b 0a 20 20 69 66 28 20 75 2e 62 6a 2e 70   0;.  if( u.bj.p
1e2f7 43 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20  C->pCursor ){.  
1e2f8 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c    sqlite3BtreeCl
1e2f9 65 61 72 43 75 72 73 6f 72 28 75 2e 62 6a 2e 70  earCursor(u.bj.p
1e2fa 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d  C->pCursor);.  }
1e2fb 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1e2fc 4f 70 63 6f 64 65 3a 20 4c 61 73 74 20 50 31 20  Opcode: Last P1 
1e2fd 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54  P2 * * *.**.** T
1e2fe 68 65 20 6e 65 78 74 20 75 73 65 20 6f 66 20 74  he next use of t
1e2ff 68 65 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c 75  he Rowid or Colu
1e300 6d 6e 20 6f 72 20 4e 65 78 74 20 69 6e 73 74 72  mn or Next instr
1e301 75 63 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a 2a  uction for P1 .*
1e302 2a 20 77 69 6c 6c 20 72 65 66 65 72 20 74 6f 20  * will refer to 
1e303 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69  the last entry i
1e304 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  n the database t
1e305 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a  able or index..*
1e306 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 6f  * If the table o
1e307 72 20 69 6e 64 65 78 20 69 73 20 65 6d 70 74 79  r index is empty
1e308 20 61 6e 64 20 50 32 3e 30 2c 20 74 68 65 6e 20   and P2>0, then 
1e309 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79  jump immediately
1e30a 20 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 50 32   to P2..** If P2
1e30b 20 69 73 20 30 20 6f 72 20 69 66 20 74 68 65 20   is 0 or if the 
1e30c 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69  table or index i
1e30d 73 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66 61 6c  s not empty, fal
1e30e 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20  l through.** to 
1e30f 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e  the following in
1e310 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61  struction..*/.ca
1e311 73 65 20 4f 50 5f 4c 61 73 74 3a 20 7b 20 20 20  se OP_Last: {   
1e312 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a       /* jump */.
1e313 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1e314 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1e315 69 6e 74 6f 20 75 2e 62 6b 20 2a 2f 0a 20 20 56  into u.bk */.  V
1e316 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20  dbeCursor *pC;. 
1e317 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72   BtCursor *pCrsr
1e318 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 23 65 6e  ;.  int res;.#en
1e319 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1e31a 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e31b 6f 20 75 2e 62 6b 20 2a 2f 0a 0a 20 20 61 73 73  o u.bk */..  ass
1e31c 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20  ert( pOp->p1>=0 
1e31d 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43  && pOp->p1<p->nC
1e31e 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6b 2e  ursor );.  u.bk.
1e31f 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f  pC = p->apCsr[pO
1e320 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74  p->p1];.  assert
1e321 28 20 75 2e 62 6b 2e 70 43 21 3d 30 20 29 3b 0a  ( u.bk.pC!=0 );.
1e322 20 20 75 2e 62 6b 2e 70 43 72 73 72 20 3d 20 75    u.bk.pCrsr = u
1e323 2e 62 6b 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b  .bk.pC->pCursor;
1e324 0a 20 20 69 66 28 20 75 2e 62 6b 2e 70 43 72 73  .  if( u.bk.pCrs
1e325 72 3d 3d 30 20 29 7b 0a 20 20 20 20 75 2e 62 6b  r==0 ){.    u.bk
1e326 2e 72 65 73 20 3d 20 31 3b 0a 20 20 7d 65 6c 73  .res = 1;.  }els
1e327 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  e{.    rc = sqli
1e328 74 65 33 42 74 72 65 65 4c 61 73 74 28 75 2e 62  te3BtreeLast(u.b
1e329 6b 2e 70 43 72 73 72 2c 20 26 75 2e 62 6b 2e 72  k.pCrsr, &u.bk.r
1e32a 65 73 29 3b 0a 20 20 7d 0a 20 20 75 2e 62 6b 2e  es);.  }.  u.bk.
1e32b 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75  pC->nullRow = (u
1e32c 38 29 75 2e 62 6b 2e 72 65 73 3b 0a 20 20 75 2e  8)u.bk.res;.  u.
1e32d 62 6b 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d  bk.pC->deferredM
1e32e 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 75 2e 62  oveto = 0;.  u.b
1e32f 6b 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c  k.pC->rowidIsVal
1e330 69 64 20 3d 20 30 3b 0a 20 20 75 2e 62 6b 2e 70  id = 0;.  u.bk.p
1e331 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  C->cacheStatus =
1e332 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20   CACHE_STALE;.  
1e333 69 66 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26  if( pOp->p2>0 &&
1e334 20 75 2e 62 6b 2e 72 65 73 20 29 7b 0a 20 20 20   u.bk.res ){.   
1e335 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1e336 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  1;.  }.  break;.
1e337 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53  }.../* Opcode: S
1e338 6f 72 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  ort P1 P2 * * *.
1e339 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64  **.** This opcod
1e33a 65 20 64 6f 65 73 20 65 78 61 63 74 6c 79 20 74  e does exactly t
1e33b 68 65 20 73 61 6d 65 20 74 68 69 6e 67 20 61 73  he same thing as
1e33c 20 4f 50 5f 52 65 77 69 6e 64 20 65 78 63 65 70   OP_Rewind excep
1e33d 74 20 74 68 61 74 0a 2a 2a 20 69 74 20 69 6e 63  t that.** it inc
1e33e 72 65 6d 65 6e 74 73 20 61 6e 20 75 6e 64 6f 63  rements an undoc
1e33f 75 6d 65 6e 74 65 64 20 67 6c 6f 62 61 6c 20 76  umented global v
1e340 61 72 69 61 62 6c 65 20 75 73 65 64 20 66 6f 72  ariable used for
1e341 20 74 65 73 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20   testing..**.** 
1e342 53 6f 72 74 69 6e 67 20 69 73 20 61 63 63 6f 6d  Sorting is accom
1e343 70 6c 69 73 68 65 64 20 62 79 20 77 72 69 74 69  plished by writi
1e344 6e 67 20 72 65 63 6f 72 64 73 20 69 6e 74 6f 20  ng records into 
1e345 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 2c  a sorting index,
1e346 0a 2a 2a 20 74 68 65 6e 20 72 65 77 69 6e 64 69  .** then rewindi
1e347 6e 67 20 74 68 61 74 20 69 6e 64 65 78 20 61 6e  ng that index an
1e348 64 20 70 6c 61 79 69 6e 67 20 69 74 20 62 61 63  d playing it bac
1e349 6b 20 66 72 6f 6d 20 62 65 67 69 6e 6e 69 6e 67  k from beginning
1e34a 20 74 6f 0a 2a 2a 20 65 6e 64 2e 20 20 57 65 20   to.** end.  We 
1e34b 75 73 65 20 74 68 65 20 4f 50 5f 53 6f 72 74 20  use the OP_Sort 
1e34c 6f 70 63 6f 64 65 20 69 6e 73 74 65 61 64 20 6f  opcode instead o
1e34d 66 20 4f 50 5f 52 65 77 69 6e 64 20 74 6f 20 64  f OP_Rewind to d
1e34e 6f 20 74 68 65 0a 2a 2a 20 72 65 77 69 6e 64 69  o the.** rewindi
1e34f 6e 67 20 73 6f 20 74 68 61 74 20 74 68 65 20 67  ng so that the g
1e350 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 77  lobal variable w
1e351 69 6c 6c 20 62 65 20 69 6e 63 72 65 6d 65 6e 74  ill be increment
1e352 65 64 20 61 6e 64 0a 2a 2a 20 72 65 67 72 65 73  ed and.** regres
1e353 73 69 6f 6e 20 74 65 73 74 73 20 63 61 6e 20 64  sion tests can d
1e354 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72  etermine whether
1e355 20 6f 72 20 6e 6f 74 20 74 68 65 20 6f 70 74 69   or not the opti
1e356 6d 69 7a 65 72 20 69 73 0a 2a 2a 20 63 6f 72 72  mizer is.** corr
1e357 65 63 74 6c 79 20 6f 70 74 69 6d 69 7a 69 6e 67  ectly optimizing
1e358 20 6f 75 74 20 73 6f 72 74 73 2e 0a 2a 2f 0a 63   out sorts..*/.c
1e359 61 73 65 20 4f 50 5f 53 6f 72 74 3a 20 7b 20 20  ase OP_Sort: {  
1e35a 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f        /* jump */
1e35b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
1e35c 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 73 6f  EST.  sqlite3_so
1e35d 72 74 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 73 71  rt_count++;.  sq
1e35e 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75  lite3_search_cou
1e35f 6e 74 2d 2d 3b 0a 23 65 6e 64 69 66 0a 20 20 70  nt--;.#endif.  p
1e360 2d 3e 61 43 6f 75 6e 74 65 72 5b 53 51 4c 49 54  ->aCounter[SQLIT
1e361 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52  E_STMTSTATUS_SOR
1e362 54 2d 31 5d 2b 2b 3b 0a 20 20 2f 2a 20 46 61 6c  T-1]++;.  /* Fal
1e363 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 4f  l through into O
1e364 50 5f 52 65 77 69 6e 64 20 2a 2f 0a 7d 0a 2f 2a  P_Rewind */.}./*
1e365 20 4f 70 63 6f 64 65 3a 20 52 65 77 69 6e 64 20   Opcode: Rewind 
1e366 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1e367 2a 20 54 68 65 20 6e 65 78 74 20 75 73 65 20 6f  * The next use o
1e368 66 20 74 68 65 20 52 6f 77 69 64 20 6f 72 20 43  f the Rowid or C
1e369 6f 6c 75 6d 6e 20 6f 72 20 4e 65 78 74 20 69 6e  olumn or Next in
1e36a 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 50 31  struction for P1
1e36b 20 0a 2a 2a 20 77 69 6c 6c 20 72 65 66 65 72 20   .** will refer 
1e36c 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 6e 74  to the first ent
1e36d 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ry in the databa
1e36e 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65  se table or inde
1e36f 78 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62  x..** If the tab
1e370 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73 20 65  le or index is e
1e371 6d 70 74 79 20 61 6e 64 20 50 32 3e 30 2c 20 74  mpty and P2>0, t
1e372 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61  hen jump immedia
1e373 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 20 49  tely to P2..** I
1e374 66 20 50 32 20 69 73 20 30 20 6f 72 20 69 66 20  f P2 is 0 or if 
1e375 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  the table or ind
1e376 65 78 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c  ex is not empty,
1e377 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a   fall through.**
1e378 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   to the followin
1e379 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  g instruction..*
1e37a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 77 69 6e 64  /.case OP_Rewind
1e37b 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75  : {        /* ju
1e37c 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20  mp */.#if 0  /* 
1e37d 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1e37e 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6c 20  moved into u.bl 
1e37f 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  */.  VdbeCursor 
1e380 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20  *pC;.  BtCursor 
1e381 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65  *pCrsr;.  int re
1e382 73 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  s;.#endif /* loc
1e383 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e384 65 64 20 69 6e 74 6f 20 75 2e 62 6c 20 2a 2f 0a  ed into u.bl */.
1e385 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1e386 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31  p1>=0 && pOp->p1
1e387 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1e388 20 75 2e 62 6c 2e 70 43 20 3d 20 70 2d 3e 61 70   u.bl.pC = p->ap
1e389 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Csr[pOp->p1];.  
1e38a 61 73 73 65 72 74 28 20 75 2e 62 6c 2e 70 43 21  assert( u.bl.pC!
1e38b 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 75 2e 62  =0 );.  if( (u.b
1e38c 6c 2e 70 43 72 73 72 20 3d 20 75 2e 62 6c 2e 70  l.pCrsr = u.bl.p
1e38d 43 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29  C->pCursor)!=0 )
1e38e 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
1e38f 65 33 42 74 72 65 65 46 69 72 73 74 28 75 2e 62  e3BtreeFirst(u.b
1e390 6c 2e 70 43 72 73 72 2c 20 26 75 2e 62 6c 2e 72  l.pCrsr, &u.bl.r
1e391 65 73 29 3b 0a 20 20 20 20 75 2e 62 6c 2e 70 43  es);.    u.bl.pC
1e392 2d 3e 61 74 46 69 72 73 74 20 3d 20 75 2e 62 6c  ->atFirst = u.bl
1e393 2e 72 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20  .res==0 ?1:0;.  
1e394 20 20 75 2e 62 6c 2e 70 43 2d 3e 64 65 66 65 72    u.bl.pC->defer
1e395 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20  redMoveto = 0;. 
1e396 20 20 20 75 2e 62 6c 2e 70 43 2d 3e 63 61 63 68     u.bl.pC->cach
1e397 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f  eStatus = CACHE_
1e398 53 54 41 4c 45 3b 0a 20 20 20 20 75 2e 62 6c 2e  STALE;.    u.bl.
1e399 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64  pC->rowidIsValid
1e39a 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
1e39b 20 20 20 75 2e 62 6c 2e 72 65 73 20 3d 20 31 3b     u.bl.res = 1;
1e39c 0a 20 20 7d 0a 20 20 75 2e 62 6c 2e 70 43 2d 3e  .  }.  u.bl.pC->
1e39d 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 38 29 75 2e  nullRow = (u8)u.
1e39e 62 6c 2e 72 65 73 3b 0a 20 20 61 73 73 65 72 74  bl.res;.  assert
1e39f 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70  ( pOp->p2>0 && p
1e3a0 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4f 70 20 29 3b  Op->p2<p->nOp );
1e3a1 0a 20 20 69 66 28 20 75 2e 62 6c 2e 72 65 73 20  .  if( u.bl.res 
1e3a2 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  ){.    pc = pOp-
1e3a3 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62  >p2 - 1;.  }.  b
1e3a4 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1e3a5 64 65 3a 20 4e 65 78 74 20 50 31 20 50 32 20 2a  de: Next P1 P2 *
1e3a6 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e   * *.**.** Advan
1e3a7 63 65 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20  ce cursor P1 so 
1e3a8 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74  that it points t
1e3a9 6f 20 74 68 65 20 6e 65 78 74 20 6b 65 79 2f 64  o the next key/d
1e3aa 61 74 61 20 70 61 69 72 20 69 6e 20 69 74 73 0a  ata pair in its.
1e3ab 2a 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65  ** table or inde
1e3ac 78 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 65  x.  If there are
1e3ad 20 6e 6f 20 6d 6f 72 65 20 6b 65 79 2f 76 61 6c   no more key/val
1e3ae 75 65 20 70 61 69 72 73 20 74 68 65 6e 20 66 61  ue pairs then fa
1e3af 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f  ll through.** to
1e3b0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69   the following i
1e3b1 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 42 75 74  nstruction.  But
1e3b2 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 61   if the cursor a
1e3b3 64 76 61 6e 63 65 20 77 61 73 20 73 75 63 63 65  dvance was succe
1e3b4 73 73 66 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 69  ssful,.** jump i
1e3b5 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32  mmediately to P2
1e3b6 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 31 20 63  ..**.** The P1 c
1e3b7 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20 66 6f  ursor must be fo
1e3b8 72 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20  r a real table, 
1e3b9 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62  not a pseudo-tab
1e3ba 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  le..**.** See al
1e3bb 73 6f 3a 20 50 72 65 76 0a 2a 2f 0a 2f 2a 20 4f  so: Prev.*/./* O
1e3bc 70 63 6f 64 65 3a 20 50 72 65 76 20 50 31 20 50  pcode: Prev P1 P
1e3bd 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 61  2 * * *.**.** Ba
1e3be 63 6b 20 75 70 20 63 75 72 73 6f 72 20 50 31 20  ck up cursor P1 
1e3bf 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74  so that it point
1e3c0 73 20 74 6f 20 74 68 65 20 70 72 65 76 69 6f 75  s to the previou
1e3c1 73 20 6b 65 79 2f 64 61 74 61 20 70 61 69 72 20  s key/data pair 
1e3c2 69 6e 20 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20  in its.** table 
1e3c3 6f 72 20 69 6e 64 65 78 2e 20 20 49 66 20 74 68  or index.  If th
1e3c4 65 72 65 20 69 73 20 6e 6f 20 70 72 65 76 69 6f  ere is no previo
1e3c5 75 73 20 6b 65 79 2f 76 61 6c 75 65 20 70 61 69  us key/value pai
1e3c6 72 73 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72  rs then fall thr
1e3c7 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66  ough.** to the f
1e3c8 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63  ollowing instruc
1e3c9 74 69 6f 6e 2e 20 20 42 75 74 20 69 66 20 74 68  tion.  But if th
1e3ca 65 20 63 75 72 73 6f 72 20 62 61 63 6b 75 70 20  e cursor backup 
1e3cb 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 2c 0a  was successful,.
1e3cc 2a 2a 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74  ** jump immediat
1e3cd 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a  ely to P2..**.**
1e3ce 20 54 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d   The P1 cursor m
1e3cf 75 73 74 20 62 65 20 66 6f 72 20 61 20 72 65 61  ust be for a rea
1e3d0 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70  l table, not a p
1e3d1 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a  seudo-table..*/.
1e3d2 63 61 73 65 20 4f 50 5f 50 72 65 76 3a 20 20 20  case OP_Prev:   
1e3d3 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a         /* jump *
1e3d4 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 78 74 3a 20  /.case OP_Next: 
1e3d5 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70  {        /* jump
1e3d6 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f   */.#if 0  /* lo
1e3d7 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1e3d8 76 65 64 20 69 6e 74 6f 20 75 2e 62 6d 20 2a 2f  ved into u.bm */
1e3d9 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
1e3da 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  C;.  BtCursor *p
1e3db 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b  Crsr;.  int res;
1e3dc 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1e3dd 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1e3de 20 69 6e 74 6f 20 75 2e 62 6d 20 2a 2f 0a 0a 20   into u.bm */.. 
1e3df 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52   CHECK_FOR_INTER
1e3e0 52 55 50 54 3b 0a 20 20 61 73 73 65 72 74 28 20  RUPT;.  assert( 
1e3e1 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f  pOp->p1>=0 && pO
1e3e2 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72  p->p1<p->nCursor
1e3e3 20 29 3b 0a 20 20 75 2e 62 6d 2e 70 43 20 3d 20   );.  u.bm.pC = 
1e3e4 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31  p->apCsr[pOp->p1
1e3e5 5d 3b 0a 20 20 69 66 28 20 75 2e 62 6d 2e 70 43  ];.  if( u.bm.pC
1e3e6 3d 3d 30 20 29 7b 0a 20 20 20 20 62 72 65 61 6b  ==0 ){.    break
1e3e7 3b 20 20 2f 2a 20 53 65 65 20 74 69 63 6b 65 74  ;  /* See ticket
1e3e8 20 23 32 32 37 33 20 2a 2f 0a 20 20 7d 0a 20 20   #2273 */.  }.  
1e3e9 75 2e 62 6d 2e 70 43 72 73 72 20 3d 20 75 2e 62  u.bm.pCrsr = u.b
1e3ea 6d 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20  m.pC->pCursor;. 
1e3eb 20 69 66 28 20 75 2e 62 6d 2e 70 43 72 73 72 3d   if( u.bm.pCrsr=
1e3ec 3d 30 20 29 7b 0a 20 20 20 20 75 2e 62 6d 2e 70  =0 ){.    u.bm.p
1e3ed 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a  C->nullRow = 1;.
1e3ee 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20      break;.  }. 
1e3ef 20 75 2e 62 6d 2e 72 65 73 20 3d 20 31 3b 0a 20   u.bm.res = 1;. 
1e3f0 20 61 73 73 65 72 74 28 20 75 2e 62 6d 2e 70 43   assert( u.bm.pC
1e3f1 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f  ->deferredMoveto
1e3f2 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 70 4f  ==0 );.  rc = pO
1e3f3 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 65  p->opcode==OP_Ne
1e3f4 78 74 20 3f 20 73 71 6c 69 74 65 33 42 74 72 65  xt ? sqlite3Btre
1e3f5 65 4e 65 78 74 28 75 2e 62 6d 2e 70 43 72 73 72  eNext(u.bm.pCrsr
1e3f6 2c 20 26 75 2e 62 6d 2e 72 65 73 29 20 3a 0a 20  , &u.bm.res) :. 
1e3f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e3f8 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
1e3f9 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75  ite3BtreePreviou
1e3fa 73 28 75 2e 62 6d 2e 70 43 72 73 72 2c 20 26 75  s(u.bm.pCrsr, &u
1e3fb 2e 62 6d 2e 72 65 73 29 3b 0a 20 20 75 2e 62 6d  .bm.res);.  u.bm
1e3fc 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28  .pC->nullRow = (
1e3fd 75 38 29 75 2e 62 6d 2e 72 65 73 3b 0a 20 20 75  u8)u.bm.res;.  u
1e3fe 2e 62 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 61  .bm.pC->cacheSta
1e3ff 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c  tus = CACHE_STAL
1e400 45 3b 0a 20 20 69 66 28 20 75 2e 62 6d 2e 72 65  E;.  if( u.bm.re
1e401 73 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 20 3d  s==0 ){.    pc =
1e402 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
1e403 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 20    if( pOp->p5 ) 
1e404 70 2d 3e 61 43 6f 75 6e 74 65 72 5b 70 4f 70 2d  p->aCounter[pOp-
1e405 3e 70 35 2d 31 5d 2b 2b 3b 0a 23 69 66 64 65 66  >p5-1]++;.#ifdef
1e406 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20   SQLITE_TEST.   
1e407 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f   sqlite3_search_
1e408 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a  count++;.#endif.
1e409 20 20 7d 0a 20 20 75 2e 62 6d 2e 70 43 2d 3e 72    }.  u.bm.pC->r
1e40a 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b  owidIsValid = 0;
1e40b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1e40c 4f 70 63 6f 64 65 3a 20 49 64 78 49 6e 73 65 72  Opcode: IdxInser
1e40d 74 20 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a  t P1 P2 P3 * P5.
1e40e 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50  **.** Register P
1e40f 32 20 68 6f 6c 64 73 20 61 20 53 51 4c 20 69 6e  2 holds a SQL in
1e410 64 65 78 20 6b 65 79 20 6d 61 64 65 20 75 73 69  dex key made usi
1e411 6e 67 20 74 68 65 0a 2a 2a 20 4d 61 6b 65 52 65  ng the.** MakeRe
1e412 63 6f 72 64 20 69 6e 73 74 72 75 63 74 69 6f 6e  cord instruction
1e413 73 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20  s.  This opcode 
1e414 77 72 69 74 65 73 20 74 68 61 74 20 6b 65 79 0a  writes that key.
1e415 2a 2a 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 65  ** into the inde
1e416 78 20 50 31 2e 20 20 44 61 74 61 20 66 6f 72 20  x P1.  Data for 
1e417 74 68 65 20 65 6e 74 72 79 20 69 73 20 6e 69 6c  the entry is nil
1e418 2e 0a 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 61 20  ..**.** P3 is a 
1e419 66 6c 61 67 20 74 68 61 74 20 70 72 6f 76 69 64  flag that provid
1e41a 65 73 20 61 20 68 69 6e 74 20 74 6f 20 74 68 65  es a hint to the
1e41b 20 62 2d 74 72 65 65 20 6c 61 79 65 72 20 74 68   b-tree layer th
1e41c 61 74 20 74 68 69 73 0a 2a 2a 20 69 6e 73 65 72  at this.** inser
1e41d 74 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62  t is likely to b
1e41e 65 20 61 6e 20 61 70 70 65 6e 64 2e 0a 2a 2a 0a  e an append..**.
1e41f 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74  ** This instruct
1e420 69 6f 6e 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 66  ion only works f
1e421 6f 72 20 69 6e 64 69 63 65 73 2e 20 20 54 68 65  or indices.  The
1e422 20 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 73 74   equivalent inst
1e423 72 75 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74  ruction.** for t
1e424 61 62 6c 65 73 20 69 73 20 4f 50 5f 49 6e 73 65  ables is OP_Inse
1e425 72 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49  rt..*/.case OP_I
1e426 64 78 49 6e 73 65 72 74 3a 20 7b 20 20 20 20 20  dxInsert: {     
1e427 20 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a 23 69 66     /* in2 */.#if
1e428 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1e429 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e42a 6f 20 75 2e 62 6e 20 2a 2f 0a 20 20 56 64 62 65  o u.bn */.  Vdbe
1e42b 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74  Cursor *pC;.  Bt
1e42c 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20  Cursor *pCrsr;. 
1e42d 20 69 6e 74 20 6e 4b 65 79 3b 0a 20 20 63 6f 6e   int nKey;.  con
1e42e 73 74 20 63 68 61 72 20 2a 7a 4b 65 79 3b 0a 23  st char *zKey;.#
1e42f 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1e430 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1e431 6e 74 6f 20 75 2e 62 6e 20 2a 2f 0a 0a 20 20 61  nto u.bn */..  a
1e432 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d  ssert( pOp->p1>=
1e433 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e  0 && pOp->p1<p->
1e434 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62  nCursor );.  u.b
1e435 6e 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b  n.pC = p->apCsr[
1e436 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65  pOp->p1];.  asse
1e437 72 74 28 20 75 2e 62 6e 2e 70 43 21 3d 30 20 29  rt( u.bn.pC!=0 )
1e438 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 32  ;.  assert( pIn2
1e439 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c  ->flags & MEM_Bl
1e43a 6f 62 20 29 3b 0a 20 20 75 2e 62 6e 2e 70 43 72  ob );.  u.bn.pCr
1e43b 73 72 20 3d 20 75 2e 62 6e 2e 70 43 2d 3e 70 43  sr = u.bn.pC->pC
1e43c 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 41 4c 57  ursor;.  if( ALW
1e43d 41 59 53 28 75 2e 62 6e 2e 70 43 72 73 72 21 3d  AYS(u.bn.pCrsr!=
1e43e 30 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  0) ){.    assert
1e43f 28 20 75 2e 62 6e 2e 70 43 2d 3e 69 73 54 61 62  ( u.bn.pC->isTab
1e440 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 20  le==0 );.    rc 
1e441 3d 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e  = ExpandBlob(pIn
1e442 32 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  2);.    if( rc==
1e443 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1e444 20 20 20 75 2e 62 6e 2e 6e 4b 65 79 20 3d 20 70     u.bn.nKey = p
1e445 49 6e 32 2d 3e 6e 3b 0a 20 20 20 20 20 20 75 2e  In2->n;.      u.
1e446 62 6e 2e 7a 4b 65 79 20 3d 20 70 49 6e 32 2d 3e  bn.zKey = pIn2->
1e447 7a 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  z;.      rc = sq
1e448 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74  lite3BtreeInsert
1e449 28 75 2e 62 6e 2e 70 43 72 73 72 2c 20 75 2e 62  (u.bn.pCrsr, u.b
1e44a 6e 2e 7a 4b 65 79 2c 20 75 2e 62 6e 2e 6e 4b 65  n.zKey, u.bn.nKe
1e44b 79 2c 20 22 22 2c 20 30 2c 20 30 2c 20 70 4f 70  y, "", 0, 0, pOp
1e44c 2d 3e 70 33 2c 0a 20 20 20 20 20 20 20 20 20 20  ->p3,.          
1e44d 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c  ((pOp->p5 & OPFL
1e44e 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54  AG_USESEEKRESULT
1e44f 29 20 3f 20 75 2e 62 6e 2e 70 43 2d 3e 73 65 65  ) ? u.bn.pC->see
1e450 6b 52 65 73 75 6c 74 20 3a 20 30 29 0a 20 20 20  kResult : 0).   
1e451 20 20 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65     );.      asse
1e452 72 74 28 20 75 2e 62 6e 2e 70 43 2d 3e 64 65 66  rt( u.bn.pC->def
1e453 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29  erredMoveto==0 )
1e454 3b 0a 20 20 20 20 20 20 75 2e 62 6e 2e 70 43 2d  ;.      u.bn.pC-
1e455 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43  >cacheStatus = C
1e456 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20  ACHE_STALE;.    
1e457 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  }.  }.  break;.}
1e458 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78  ../* Opcode: Idx
1e459 44 65 6c 65 74 65 20 50 31 20 50 32 20 50 33 20  Delete P1 P2 P3 
1e45a 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  * *.**.** The co
1e45b 6e 74 65 6e 74 20 6f 66 20 50 33 20 72 65 67 69  ntent of P3 regi
1e45c 73 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 61  sters starting a
1e45d 74 20 72 65 67 69 73 74 65 72 20 50 32 20 66 6f  t register P2 fo
1e45e 72 6d 0a 2a 2a 20 61 6e 20 75 6e 70 61 63 6b 65  rm.** an unpacke
1e45f 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 54 68 69  d index key. Thi
1e460 73 20 6f 70 63 6f 64 65 20 72 65 6d 6f 76 65 73  s opcode removes
1e461 20 74 68 61 74 20 65 6e 74 72 79 20 66 72 6f 6d   that entry from
1e462 20 74 68 65 20 0a 2a 2a 20 69 6e 64 65 78 20 6f   the .** index o
1e463 70 65 6e 65 64 20 62 79 20 63 75 72 73 6f 72 20  pened by cursor 
1e464 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49  P1..*/.case OP_I
1e465 64 78 44 65 6c 65 74 65 3a 20 7b 0a 23 69 66 20  dxDelete: {.#if 
1e466 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1e467 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e468 20 75 2e 62 6f 20 2a 2f 0a 20 20 56 64 62 65 43   u.bo */.  VdbeC
1e469 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43  ursor *pC;.  BtC
1e46a 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20  ursor *pCrsr;.  
1e46b 69 6e 74 20 72 65 73 3b 0a 20 20 55 6e 70 61 63  int res;.  Unpac
1e46c 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 23 65 6e  kedRecord r;.#en
1e46d 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1e46e 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e46f 6f 20 75 2e 62 6f 20 2a 2f 0a 0a 20 20 61 73 73  o u.bo */..  ass
1e470 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29  ert( pOp->p3>0 )
1e471 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1e472 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32  >p2>0 && pOp->p2
1e473 2b 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65  +pOp->p3<=p->nMe
1e474 6d 2b 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28  m+1 );.  assert(
1e475 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70   pOp->p1>=0 && p
1e476 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f  Op->p1<p->nCurso
1e477 72 20 29 3b 0a 20 20 75 2e 62 6f 2e 70 43 20 3d  r );.  u.bo.pC =
1e478 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70   p->apCsr[pOp->p
1e479 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e  1];.  assert( u.
1e47a 62 6f 2e 70 43 21 3d 30 20 29 3b 0a 20 20 75 2e  bo.pC!=0 );.  u.
1e47b 62 6f 2e 70 43 72 73 72 20 3d 20 75 2e 62 6f 2e  bo.pCrsr = u.bo.
1e47c 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69  pC->pCursor;.  i
1e47d 66 28 20 41 4c 57 41 59 53 28 75 2e 62 6f 2e 70  f( ALWAYS(u.bo.p
1e47e 43 72 73 72 21 3d 30 29 20 29 7b 0a 20 20 20 20  Crsr!=0) ){.    
1e47f 75 2e 62 6f 2e 72 2e 70 4b 65 79 49 6e 66 6f 20  u.bo.r.pKeyInfo 
1e480 3d 20 75 2e 62 6f 2e 70 43 2d 3e 70 4b 65 79 49  = u.bo.pC->pKeyI
1e481 6e 66 6f 3b 0a 20 20 20 20 75 2e 62 6f 2e 72 2e  nfo;.    u.bo.r.
1e482 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29 70 4f  nField = (u16)pO
1e483 70 2d 3e 70 33 3b 0a 20 20 20 20 75 2e 62 6f 2e  p->p3;.    u.bo.
1e484 72 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20  r.flags = 0;.   
1e485 20 75 2e 62 6f 2e 72 2e 61 4d 65 6d 20 3d 20 26   u.bo.r.aMem = &
1e486 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d  p->aMem[pOp->p2]
1e487 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1e488 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70  e3BtreeMovetoUnp
1e489 61 63 6b 65 64 28 75 2e 62 6f 2e 70 43 72 73 72  acked(u.bo.pCrsr
1e48a 2c 20 26 75 2e 62 6f 2e 72 2c 20 30 2c 20 30 2c  , &u.bo.r, 0, 0,
1e48b 20 26 75 2e 62 6f 2e 72 65 73 29 3b 0a 20 20 20   &u.bo.res);.   
1e48c 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1e48d 4f 4b 20 26 26 20 75 2e 62 6f 2e 72 65 73 3d 3d  OK && u.bo.res==
1e48e 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
1e48f 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65  sqlite3BtreeDele
1e490 74 65 28 75 2e 62 6f 2e 70 43 72 73 72 29 3b 0a  te(u.bo.pCrsr);.
1e491 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
1e492 28 20 75 2e 62 6f 2e 70 43 2d 3e 64 65 66 65 72  ( u.bo.pC->defer
1e493 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a  redMoveto==0 );.
1e494 20 20 20 20 75 2e 62 6f 2e 70 43 2d 3e 63 61 63      u.bo.pC->cac
1e495 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
1e496 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 62 72  _STALE;.  }.  br
1e497 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1e498 65 3a 20 49 64 78 52 6f 77 69 64 20 50 31 20 50  e: IdxRowid P1 P
1e499 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72  2 * * *.**.** Wr
1e49a 69 74 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65  ite into registe
1e49b 72 20 50 32 20 61 6e 20 69 6e 74 65 67 65 72 20  r P2 an integer 
1e49c 77 68 69 63 68 20 69 73 20 74 68 65 20 6c 61 73  which is the las
1e49d 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 72  t entry in the r
1e49e 65 63 6f 72 64 20 61 74 0a 2a 2a 20 74 68 65 20  ecord at.** the 
1e49f 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78  end of the index
1e4a0 20 6b 65 79 20 70 6f 69 6e 74 65 64 20 74 6f 20   key pointed to 
1e4a1 62 79 20 63 75 72 73 6f 72 20 50 31 2e 20 20 54  by cursor P1.  T
1e4a2 68 69 73 20 69 6e 74 65 67 65 72 20 73 68 6f 75  his integer shou
1e4a3 6c 64 20 62 65 0a 2a 2a 20 74 68 65 20 72 6f 77  ld be.** the row
1e4a4 69 64 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  id of the table 
1e4a5 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 74  entry to which t
1e4a6 68 69 73 20 69 6e 64 65 78 20 65 6e 74 72 79 20  his index entry 
1e4a7 70 6f 69 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 53 65  points..**.** Se
1e4a8 65 20 61 6c 73 6f 3a 20 52 6f 77 69 64 2c 20 4d  e also: Rowid, M
1e4a9 61 6b 65 52 65 63 6f 72 64 2e 0a 2a 2f 0a 63 61  akeRecord..*/.ca
1e4aa 73 65 20 4f 50 5f 49 64 78 52 6f 77 69 64 3a 20  se OP_IdxRowid: 
1e4ab 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  {              /
1e4ac 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73  * out2-prereleas
1e4ad 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  e */.#if 0  /* l
1e4ae 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1e4af 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 70 20 2a  oved into u.bp *
1e4b0 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  /.  BtCursor *pC
1e4b1 72 73 72 3b 0a 20 20 56 64 62 65 43 75 72 73 6f  rsr;.  VdbeCurso
1e4b2 72 20 2a 70 43 3b 0a 20 20 69 36 34 20 72 6f 77  r *pC;.  i64 row
1e4b3 69 64 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f  id;.#endif /* lo
1e4b4 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1e4b5 76 65 64 20 69 6e 74 6f 20 75 2e 62 70 20 2a 2f  ved into u.bp */
1e4b6 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ..  assert( pOp-
1e4b7 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70  >p1>=0 && pOp->p
1e4b8 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  1<p->nCursor );.
1e4b9 20 20 75 2e 62 70 2e 70 43 20 3d 20 70 2d 3e 61    u.bp.pC = p->a
1e4ba 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  pCsr[pOp->p1];. 
1e4bb 20 61 73 73 65 72 74 28 20 75 2e 62 70 2e 70 43   assert( u.bp.pC
1e4bc 21 3d 30 20 29 3b 0a 20 20 75 2e 62 70 2e 70 43  !=0 );.  u.bp.pC
1e4bd 72 73 72 20 3d 20 75 2e 62 70 2e 70 43 2d 3e 70  rsr = u.bp.pC->p
1e4be 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 41 4c  Cursor;.  if( AL
1e4bf 57 41 59 53 28 75 2e 62 70 2e 70 43 72 73 72 21  WAYS(u.bp.pCrsr!
1e4c0 3d 30 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  =0) ){.    rc = 
1e4c1 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f  sqlite3VdbeCurso
1e4c2 72 4d 6f 76 65 74 6f 28 75 2e 62 70 2e 70 43 29  rMoveto(u.bp.pC)
1e4c3 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28  ;.    if( NEVER(
1e4c4 72 63 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  rc) ) goto abort
1e4c5 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20  _due_to_error;. 
1e4c6 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 70 2e     assert( u.bp.
1e4c7 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65  pC->deferredMove
1e4c8 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  to==0 );.    ass
1e4c9 65 72 74 28 20 75 2e 62 70 2e 70 43 2d 3e 69 73  ert( u.bp.pC->is
1e4ca 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20  Table==0 );.    
1e4cb 69 66 28 20 21 75 2e 62 70 2e 70 43 2d 3e 6e 75  if( !u.bp.pC->nu
1e4cc 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 72  llRow ){.      r
1e4cd 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49  c = sqlite3VdbeI
1e4ce 64 78 52 6f 77 69 64 28 64 62 2c 20 75 2e 62 70  dxRowid(db, u.bp
1e4cf 2e 70 43 72 73 72 2c 20 26 75 2e 62 70 2e 72 6f  .pCrsr, &u.bp.ro
1e4d0 77 69 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20  wid);.      if( 
1e4d1 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1e4d2 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62  .        goto ab
1e4d3 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1e4d4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1e4d5 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
1e4d6 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20  Out, MEM_Int);. 
1e4d7 20 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d       pOut->u.i =
1e4d8 20 75 2e 62 70 2e 72 6f 77 69 64 3b 0a 20 20 20   u.bp.rowid;.   
1e4d9 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a   }.  }.  break;.
1e4da 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64  }../* Opcode: Id
1e4db 78 47 45 20 50 31 20 50 32 20 50 33 20 50 34 20  xGE P1 P2 P3 P4 
1e4dc 50 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20  P5.**.** The P4 
1e4dd 72 65 67 69 73 74 65 72 20 76 61 6c 75 65 73 20  register values 
1e4de 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 50  beginning with P
1e4df 33 20 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b  3 form an unpack
1e4e0 65 64 20 69 6e 64 65 78 20 0a 2a 2a 20 6b 65 79  ed index .** key
1e4e1 20 74 68 61 74 20 6f 6d 69 74 73 20 74 68 65 20   that omits the 
1e4e2 52 4f 57 49 44 2e 20 20 43 6f 6d 70 61 72 65 20  ROWID.  Compare 
1e4e3 74 68 69 73 20 6b 65 79 20 76 61 6c 75 65 20 61  this key value a
1e4e4 67 61 69 6e 73 74 20 74 68 65 20 69 6e 64 65 78  gainst the index
1e4e5 20 0a 2a 2a 20 74 68 61 74 20 50 31 20 69 73 20   .** that P1 is 
1e4e6 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69  currently pointi
1e4e7 6e 67 20 74 6f 2c 20 69 67 6e 6f 72 69 6e 67 20  ng to, ignoring 
1e4e8 74 68 65 20 52 4f 57 49 44 20 6f 6e 20 74 68 65  the ROWID on the
1e4e9 20 50 31 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a   P1 index..**.**
1e4ea 20 49 66 20 74 68 65 20 50 31 20 69 6e 64 65 78   If the P1 index
1e4eb 20 65 6e 74 72 79 20 69 73 20 67 72 65 61 74 65   entry is greate
1e4ec 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  r than or equal 
1e4ed 74 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65  to the key value
1e4ee 0a 2a 2a 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f  .** then jump to
1e4ef 20 50 32 2e 20 20 4f 74 68 65 72 77 69 73 65 20   P2.  Otherwise 
1e4f0 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20  fall through to 
1e4f1 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63  the next instruc
1e4f2 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  tion..**.** If P
1e4f3 35 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68  5 is non-zero th
1e4f4 65 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65  en the key value
1e4f5 20 69 73 20 69 6e 63 72 65 61 73 65 64 20 62 79   is increased by
1e4f6 20 61 6e 20 65 70 73 69 6c 6f 6e 20 0a 2a 2a 20   an epsilon .** 
1e4f7 70 72 69 6f 72 20 74 6f 20 74 68 65 20 63 6f 6d  prior to the com
1e4f8 70 61 72 69 73 6f 6e 2e 20 20 54 68 69 73 20 6d  parison.  This m
1e4f9 61 6b 65 20 74 68 65 20 6f 70 63 6f 64 65 20 77  ake the opcode w
1e4fa 6f 72 6b 20 6c 69 6b 65 20 49 64 78 47 54 20 65  ork like IdxGT e
1e4fb 78 63 65 70 74 0a 2a 2a 20 74 68 61 74 20 69 66  xcept.** that if
1e4fc 20 74 68 65 20 6b 65 79 20 66 72 6f 6d 20 72 65   the key from re
1e4fd 67 69 73 74 65 72 20 50 33 20 69 73 20 61 20 70  gister P3 is a p
1e4fe 72 65 66 69 78 20 6f 66 20 74 68 65 20 6b 65 79  refix of the key
1e4ff 20 69 6e 20 74 68 65 20 63 75 72 73 6f 72 2c 0a   in the cursor,.
1e500 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  ** the result is
1e501 20 66 61 6c 73 65 20 77 68 65 72 65 61 73 20 69   false whereas i
1e502 74 20 77 6f 75 6c 64 20 62 65 20 74 72 75 65 20  t would be true 
1e503 77 69 74 68 20 49 64 78 47 54 2e 0a 2a 2f 0a 2f  with IdxGT..*/./
1e504 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 4c 54 20  * Opcode: IdxLT 
1e505 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a  P1 P2 P3 * P5.**
1e506 0a 2a 2a 20 54 68 65 20 50 34 20 72 65 67 69 73  .** The P4 regis
1e507 74 65 72 20 76 61 6c 75 65 73 20 62 65 67 69 6e  ter values begin
1e508 6e 69 6e 67 20 77 69 74 68 20 50 33 20 66 6f 72  ning with P3 for
1e509 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e  m an unpacked in
1e50a 64 65 78 20 0a 2a 2a 20 6b 65 79 20 74 68 61 74  dex .** key that
1e50b 20 6f 6d 69 74 73 20 74 68 65 20 52 4f 57 49 44   omits the ROWID
1e50c 2e 20 20 43 6f 6d 70 61 72 65 20 74 68 69 73 20  .  Compare this 
1e50d 6b 65 79 20 76 61 6c 75 65 20 61 67 61 69 6e 73  key value agains
1e50e 74 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20  t the index .** 
1e50f 74 68 61 74 20 50 31 20 69 73 20 63 75 72 72 65  that P1 is curre
1e510 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f  ntly pointing to
1e511 2c 20 69 67 6e 6f 72 69 6e 67 20 74 68 65 20 52  , ignoring the R
1e512 4f 57 49 44 20 6f 6e 20 74 68 65 20 50 31 20 69  OWID on the P1 i
1e513 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ndex..**.** If t
1e514 68 65 20 50 31 20 69 6e 64 65 78 20 65 6e 74 72  he P1 index entr
1e515 79 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74  y is less than t
1e516 68 65 20 6b 65 79 20 76 61 6c 75 65 20 74 68 65  he key value the
1e517 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a  n jump to P2..**
1e518 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20   Otherwise fall 
1e519 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e  through to the n
1e51a 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  ext instruction.
1e51b 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73 20  .**.** If P5 is 
1e51c 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68  non-zero then th
1e51d 65 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20 69  e key value is i
1e51e 6e 63 72 65 61 73 65 64 20 62 79 20 61 6e 20 65  ncreased by an e
1e51f 70 73 69 6c 6f 6e 20 70 72 69 6f 72 20 0a 2a 2a  psilon prior .**
1e520 20 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73   to the comparis
1e521 6f 6e 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20  on.  This makes 
1e522 74 68 65 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20  the opcode work 
1e523 6c 69 6b 65 20 49 64 78 4c 45 2e 0a 2a 2f 0a 63  like IdxLE..*/.c
1e524 61 73 65 20 4f 50 5f 49 64 78 4c 54 3a 20 20 20  ase OP_IdxLT:   
1e525 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20         /* jump, 
1e526 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 49  in3 */.case OP_I
1e527 64 78 47 45 3a 20 7b 20 20 20 20 20 20 20 20 2f  dxGE: {        /
1e528 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 23  * jump, in3 */.#
1e529 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1e52a 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1e52b 6e 74 6f 20 75 2e 62 71 20 2a 2f 0a 20 20 56 64  nto u.bq */.  Vd
1e52c 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20  beCursor *pC;.  
1e52d 69 6e 74 20 72 65 73 3b 0a 20 20 55 6e 70 61 63  int res;.  Unpac
1e52e 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 23 65 6e  kedRecord r;.#en
1e52f 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1e530 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e531 6f 20 75 2e 62 71 20 2a 2f 0a 0a 20 20 61 73 73  o u.bq */..  ass
1e532 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20  ert( pOp->p1>=0 
1e533 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43  && pOp->p1<p->nC
1e534 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 71 2e  ursor );.  u.bq.
1e535 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f  pC = p->apCsr[pO
1e536 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74  p->p1];.  assert
1e537 28 20 75 2e 62 71 2e 70 43 21 3d 30 20 29 3b 0a  ( u.bq.pC!=0 );.
1e538 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62    if( ALWAYS(u.b
1e539 71 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30  q.pC->pCursor!=0
1e53a 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ) ){.    assert(
1e53b 20 75 2e 62 71 2e 70 43 2d 3e 64 65 66 65 72 72   u.bq.pC->deferr
1e53c 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20  edMoveto==0 );. 
1e53d 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
1e53e 70 35 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 35  p5==0 || pOp->p5
1e53f 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==1 );.    asser
1e540 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d  t( pOp->p4type==
1e541 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20  P4_INT32 );.    
1e542 75 2e 62 71 2e 72 2e 70 4b 65 79 49 6e 66 6f 20  u.bq.r.pKeyInfo 
1e543 3d 20 75 2e 62 71 2e 70 43 2d 3e 70 4b 65 79 49  = u.bq.pC->pKeyI
1e544 6e 66 6f 3b 0a 20 20 20 20 75 2e 62 71 2e 72 2e  nfo;.    u.bq.r.
1e545 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29 70 4f  nField = (u16)pO
1e546 70 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 69 66 28  p->p4.i;.    if(
1e547 20 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20   pOp->p5 ){.    
1e548 20 20 75 2e 62 71 2e 72 2e 66 6c 61 67 73 20 3d    u.bq.r.flags =
1e549 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45   UNPACKED_INCRKE
1e54a 59 20 7c 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e  Y | UNPACKED_IGN
1e54b 4f 52 45 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d  ORE_ROWID;.    }
1e54c 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 2e 62 71  else{.      u.bq
1e54d 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43  .r.flags = UNPAC
1e54e 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44  KED_IGNORE_ROWID
1e54f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 71  ;.    }.    u.bq
1e550 2e 72 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d  .r.aMem = &p->aM
1e551 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20  em[pOp->p3];.   
1e552 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
1e553 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 75  eIdxKeyCompare(u
1e554 2e 62 71 2e 70 43 2c 20 26 75 2e 62 71 2e 72 2c  .bq.pC, &u.bq.r,
1e555 20 26 75 2e 62 71 2e 72 65 73 29 3b 0a 20 20 20   &u.bq.res);.   
1e556 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
1e557 3d 3d 4f 50 5f 49 64 78 4c 54 20 29 7b 0a 20 20  ==OP_IdxLT ){.  
1e558 20 20 20 20 75 2e 62 71 2e 72 65 73 20 3d 20 2d      u.bq.res = -
1e559 75 2e 62 71 2e 72 65 73 3b 0a 20 20 20 20 7d 65  u.bq.res;.    }e
1e55a 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72  lse{.      asser
1e55b 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  t( pOp->opcode==
1e55c 4f 50 5f 49 64 78 47 45 20 29 3b 0a 20 20 20 20  OP_IdxGE );.    
1e55d 20 20 75 2e 62 71 2e 72 65 73 2b 2b 3b 0a 20 20    u.bq.res++;.  
1e55e 20 20 7d 0a 20 20 20 20 69 66 28 20 75 2e 62 71    }.    if( u.bq
1e55f 2e 72 65 73 3e 30 20 29 7b 0a 20 20 20 20 20 20  .res>0 ){.      
1e560 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
1e561 20 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62   ;.    }.  }.  b
1e562 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1e563 64 65 3a 20 44 65 73 74 72 6f 79 20 50 31 20 50  de: Destroy P1 P
1e564 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44  2 P3 * *.**.** D
1e565 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20  elete an entire 
1e566 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f  database table o
1e567 72 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f  r index whose ro
1e568 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 64  ot page in the d
1e569 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20  atabase.** file 
1e56a 69 73 20 67 69 76 65 6e 20 62 79 20 50 31 2e 0a  is given by P1..
1e56b 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20  **.** The table 
1e56c 62 65 69 6e 67 20 64 65 73 74 72 6f 79 65 64 20  being destroyed 
1e56d 69 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64  is in the main d
1e56e 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20  atabase file if 
1e56f 50 33 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 33  P3==0.  If.** P3
1e570 3d 3d 31 20 74 68 65 6e 20 74 68 65 20 74 61 62  ==1 then the tab
1e571 6c 65 20 74 6f 20 62 65 20 63 6c 65 61 72 20 69  le to be clear i
1e572 73 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61  s in the auxilia
1e573 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ry database file
1e574 0a 2a 2a 20 74 68 61 74 20 69 73 20 75 73 65 64  .** that is used
1e575 20 74 6f 20 73 74 6f 72 65 20 74 61 62 6c 65 73   to store tables
1e576 20 63 72 65 61 74 65 20 75 73 69 6e 67 20 43 52   create using CR
1e577 45 41 54 45 20 54 45 4d 50 4f 52 41 52 59 20 54  EATE TEMPORARY T
1e578 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41  ABLE..**.** If A
1e579 55 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e 61  UTOVACUUM is ena
1e57a 62 6c 65 64 20 74 68 65 6e 20 69 74 20 69 73 20  bled then it is 
1e57b 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e  possible that an
1e57c 6f 74 68 65 72 20 72 6f 6f 74 20 70 61 67 65 0a  other root page.
1e57d 2a 2a 20 6d 69 67 68 74 20 62 65 20 6d 6f 76 65  ** might be move
1e57e 64 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 6c 79  d into the newly
1e57f 20 64 65 6c 65 74 65 64 20 72 6f 6f 74 20 70 61   deleted root pa
1e580 67 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b  ge in order to k
1e581 65 65 70 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20  eep all.** root 
1e582 70 61 67 65 73 20 63 6f 6e 74 69 67 75 6f 75 73  pages contiguous
1e583 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   at the beginnin
1e584 67 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  g of the databas
1e585 65 2e 20 20 54 68 65 20 66 6f 72 6d 65 72 0a 2a  e.  The former.*
1e586 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72  * value of the r
1e587 6f 6f 74 20 70 61 67 65 20 74 68 61 74 20 6d 6f  oot page that mo
1e588 76 65 64 20 2d 20 69 74 73 20 76 61 6c 75 65 20  ved - its value 
1e589 62 65 66 6f 72 65 20 74 68 65 20 6d 6f 76 65 20  before the move 
1e58a 6f 63 63 75 72 72 65 64 20 2d 0a 2a 2a 20 69 73  occurred -.** is
1e58b 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
1e58c 74 65 72 20 50 32 2e 20 20 49 66 20 6e 6f 20 70  ter P2.  If no p
1e58d 61 67 65 20 0a 2a 2a 20 6d 6f 76 65 6d 65 6e 74  age .** movement
1e58e 20 77 61 73 20 72 65 71 75 69 72 65 64 20 28 62   was required (b
1e58f 65 63 61 75 73 65 20 74 68 65 20 74 61 62 6c 65  ecause the table
1e590 20 62 65 69 6e 67 20 64 72 6f 70 70 65 64 20 77   being dropped w
1e591 61 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a 20 74  as already .** t
1e592 68 65 20 6c 61 73 74 20 6f 6e 65 20 69 6e 20 74  he last one in t
1e593 68 65 20 64 61 74 61 62 61 73 65 29 20 74 68 65  he database) the
1e594 6e 20 61 20 7a 65 72 6f 20 69 73 20 73 74 6f 72  n a zero is stor
1e595 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ed in register P
1e596 32 2e 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43  2..** If AUTOVAC
1e597 55 55 4d 20 69 73 20 64 69 73 61 62 6c 65 64 20  UUM is disabled 
1e598 74 68 65 6e 20 61 20 7a 65 72 6f 20 69 73 20 73  then a zero is s
1e599 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65  tored in registe
1e59a 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  r P2..**.** See 
1e59b 61 6c 73 6f 3a 20 43 6c 65 61 72 0a 2a 2f 0a 63  also: Clear.*/.c
1e59c 61 73 65 20 4f 50 5f 44 65 73 74 72 6f 79 3a 20  ase OP_Destroy: 
1e59d 7b 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72  {     /* out2-pr
1e59e 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20  erelease */.#if 
1e59f 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1e5a0 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e5a1 20 75 2e 62 72 20 2a 2f 0a 20 20 69 6e 74 20 69   u.br */.  int i
1e5a2 4d 6f 76 65 64 3b 0a 20 20 69 6e 74 20 69 43 6e  Moved;.  int iCn
1e5a3 74 3b 0a 20 20 56 64 62 65 20 2a 70 56 64 62 65  t;.  Vdbe *pVdbe
1e5a4 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 23 65 6e  ;.  int iDb;.#en
1e5a5 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1e5a6 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e5a7 6f 20 75 2e 62 72 20 2a 2f 0a 23 69 66 6e 64 65  o u.br */.#ifnde
1e5a8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
1e5a9 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 75 2e 62  RTUALTABLE.  u.b
1e5aa 72 2e 69 43 6e 74 20 3d 20 30 3b 0a 20 20 66 6f  r.iCnt = 0;.  fo
1e5ab 72 28 75 2e 62 72 2e 70 56 64 62 65 3d 64 62 2d  r(u.br.pVdbe=db-
1e5ac 3e 70 56 64 62 65 3b 20 75 2e 62 72 2e 70 56 64  >pVdbe; u.br.pVd
1e5ad 62 65 3b 20 75 2e 62 72 2e 70 56 64 62 65 20 3d  be; u.br.pVdbe =
1e5ae 20 75 2e 62 72 2e 70 56 64 62 65 2d 3e 70 4e 65   u.br.pVdbe->pNe
1e5af 78 74 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 62  xt){.    if( u.b
1e5b0 72 2e 70 56 64 62 65 2d 3e 6d 61 67 69 63 3d 3d  r.pVdbe->magic==
1e5b1 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26  VDBE_MAGIC_RUN &
1e5b2 26 20 75 2e 62 72 2e 70 56 64 62 65 2d 3e 69 6e  & u.br.pVdbe->in
1e5b3 56 74 61 62 4d 65 74 68 6f 64 3c 32 20 26 26 20  VtabMethod<2 && 
1e5b4 75 2e 62 72 2e 70 56 64 62 65 2d 3e 70 63 3e 3d  u.br.pVdbe->pc>=
1e5b5 30 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 72 2e  0 ){.      u.br.
1e5b6 69 43 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  iCnt++;.    }.  
1e5b7 7d 0a 23 65 6c 73 65 0a 20 20 75 2e 62 72 2e 69  }.#else.  u.br.i
1e5b8 43 6e 74 20 3d 20 64 62 2d 3e 61 63 74 69 76 65  Cnt = db->active
1e5b9 56 64 62 65 43 6e 74 3b 0a 23 65 6e 64 69 66 0a  VdbeCnt;.#endif.
1e5ba 20 20 69 66 28 20 75 2e 62 72 2e 69 43 6e 74 3e    if( u.br.iCnt>
1e5bb 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  1 ){.    rc = SQ
1e5bc 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20  LITE_LOCKED;.   
1e5bd 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20   p->errorAction 
1e5be 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 7d 65  = OE_Abort;.  }e
1e5bf 6c 73 65 7b 0a 20 20 20 20 75 2e 62 72 2e 69 44  lse{.    u.br.iD
1e5c0 62 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 20  b = pOp->p3;.   
1e5c1 20 61 73 73 65 72 74 28 20 75 2e 62 72 2e 69 43   assert( u.br.iC
1e5c2 6e 74 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73  nt==1 );.    ass
1e5c3 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61  ert( (p->btreeMa
1e5c4 73 6b 20 26 20 28 31 3c 3c 75 2e 62 72 2e 69 44  sk & (1<<u.br.iD
1e5c5 62 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 72 63  b))!=0 );.    rc
1e5c6 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44   = sqlite3BtreeD
1e5c7 72 6f 70 54 61 62 6c 65 28 64 62 2d 3e 61 44 62  ropTable(db->aDb
1e5c8 5b 75 2e 62 72 2e 69 44 62 5d 2e 70 42 74 2c 20  [u.br.iDb].pBt, 
1e5c9 70 4f 70 2d 3e 70 31 2c 20 26 75 2e 62 72 2e 69  pOp->p1, &u.br.i
1e5ca 4d 6f 76 65 64 29 3b 0a 20 20 20 20 4d 65 6d 53  Moved);.    MemS
1e5cb 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c  etTypeFlag(pOut,
1e5cc 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 70   MEM_Int);.    p
1e5cd 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 72 2e  Out->u.i = u.br.
1e5ce 69 4d 6f 76 65 64 3b 0a 23 69 66 6e 64 65 66 20  iMoved;.#ifndef 
1e5cf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
1e5d0 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 72  VACUUM.    if( r
1e5d1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
1e5d2 75 2e 62 72 2e 69 4d 6f 76 65 64 21 3d 30 20 29  u.br.iMoved!=0 )
1e5d3 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  {.      sqlite3R
1e5d4 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 26 64 62  ootPageMoved(&db
1e5d5 2d 3e 61 44 62 5b 75 2e 62 72 2e 69 44 62 5d 2c  ->aDb[u.br.iDb],
1e5d6 20 75 2e 62 72 2e 69 4d 6f 76 65 64 2c 20 70 4f   u.br.iMoved, pO
1e5d7 70 2d 3e 70 31 29 3b 0a 20 20 20 20 7d 0a 23 65  p->p1);.    }.#e
1e5d8 6e 64 69 66 0a 20 20 7d 0a 20 20 62 72 65 61 6b  ndif.  }.  break
1e5d9 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1e5da 43 6c 65 61 72 20 50 31 20 50 32 20 50 33 0a 2a  Clear P1 P2 P3.*
1e5db 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20  *.** Delete all 
1e5dc 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
1e5dd 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f  database table o
1e5de 72 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f  r index whose ro
1e5df 6f 74 20 70 61 67 65 0a 2a 2a 20 69 6e 20 74 68  ot page.** in th
1e5e0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
1e5e1 69 73 20 67 69 76 65 6e 20 62 79 20 50 31 2e 20  is given by P1. 
1e5e2 20 42 75 74 2c 20 75 6e 6c 69 6b 65 20 44 65 73   But, unlike Des
1e5e3 74 72 6f 79 2c 20 64 6f 20 6e 6f 74 0a 2a 2a 20  troy, do not.** 
1e5e4 72 65 6d 6f 76 65 20 74 68 65 20 74 61 62 6c 65  remove the table
1e5e5 20 6f 72 20 69 6e 64 65 78 20 66 72 6f 6d 20 74   or index from t
1e5e6 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1e5e7 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c  ..**.** The tabl
1e5e8 65 20 62 65 69 6e 67 20 63 6c 65 61 72 20 69 73  e being clear is
1e5e9 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74   in the main dat
1e5ea 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 32  abase file if P2
1e5eb 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 32 3d 3d  ==0.  If.** P2==
1e5ec 31 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65  1 then the table
1e5ed 20 74 6f 20 62 65 20 63 6c 65 61 72 20 69 73 20   to be clear is 
1e5ee 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 79  in the auxiliary
1e5ef 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a   database file.*
1e5f0 2a 20 74 68 61 74 20 69 73 20 75 73 65 64 20 74  * that is used t
1e5f1 6f 20 73 74 6f 72 65 20 74 61 62 6c 65 73 20 63  o store tables c
1e5f2 72 65 61 74 65 20 75 73 69 6e 67 20 43 52 45 41  reate using CREA
1e5f3 54 45 20 54 45 4d 50 4f 52 41 52 59 20 54 41 42  TE TEMPORARY TAB
1e5f4 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  LE..**.** If the
1e5f5 20 50 33 20 76 61 6c 75 65 20 69 73 20 6e 6f 6e   P3 value is non
1e5f6 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20  -zero, then the 
1e5f7 74 61 62 6c 65 20 72 65 66 65 72 72 65 64 20 74  table referred t
1e5f8 6f 20 6d 75 73 74 20 62 65 20 61 6e 0a 2a 2a 20  o must be an.** 
1e5f9 69 6e 74 6b 65 79 20 74 61 62 6c 65 20 28 61 6e  intkey table (an
1e5fa 20 53 51 4c 20 74 61 62 6c 65 2c 20 6e 6f 74 20   SQL table, not 
1e5fb 61 6e 20 69 6e 64 65 78 29 2e 20 49 6e 20 74 68  an index). In th
1e5fc 69 73 20 63 61 73 65 20 74 68 65 20 72 6f 77 20  is case the row 
1e5fd 63 68 61 6e 67 65 20 0a 2a 2a 20 63 6f 75 6e 74  change .** count
1e5fe 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20   is incremented 
1e5ff 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  by the number of
1e600 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62   rows in the tab
1e601 6c 65 20 62 65 69 6e 67 20 63 6c 65 61 72 65 64  le being cleared
1e602 2e 20 0a 2a 2a 20 49 66 20 50 33 20 69 73 20 67  . .** If P3 is g
1e603 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f  reater than zero
1e604 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65  , then the value
1e605 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
1e606 74 65 72 20 50 33 20 69 73 0a 2a 2a 20 61 6c 73  ter P3 is.** als
1e607 6f 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79  o incremented by
1e608 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72   the number of r
1e609 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65  ows in the table
1e60a 20 62 65 69 6e 67 20 63 6c 65 61 72 65 64 2e 0a   being cleared..
1e60b 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
1e60c 44 65 73 74 72 6f 79 0a 2a 2f 0a 63 61 73 65 20  Destroy.*/.case 
1e60d 4f 50 5f 43 6c 65 61 72 3a 20 7b 0a 23 69 66 20  OP_Clear: {.#if 
1e60e 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1e60f 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e610 20 75 2e 62 73 20 2a 2f 0a 20 20 69 6e 74 20 6e   u.bs */.  int n
1e611 43 68 61 6e 67 65 3b 0a 23 65 6e 64 69 66 20 2f  Change;.#endif /
1e612 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1e613 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1e614 73 20 2a 2f 0a 0a 20 20 75 2e 62 73 2e 6e 43 68  s */..  u.bs.nCh
1e615 61 6e 67 65 20 3d 20 30 3b 0a 20 20 61 73 73 65  ange = 0;.  asse
1e616 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73  rt( (p->btreeMas
1e617 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 32 29  k & (1<<pOp->p2)
1e618 29 21 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73  )!=0 );.  rc = s
1e619 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72  qlite3BtreeClear
1e61a 54 61 62 6c 65 28 0a 20 20 20 20 20 20 64 62 2d  Table(.      db-
1e61b 3e 61 44 62 5b 70 4f 70 2d 3e 70 32 5d 2e 70 42  >aDb[pOp->p2].pB
1e61c 74 2c 20 70 4f 70 2d 3e 70 31 2c 20 28 70 4f 70  t, pOp->p1, (pOp
1e61d 2d 3e 70 33 20 3f 20 26 75 2e 62 73 2e 6e 43 68  ->p3 ? &u.bs.nCh
1e61e 61 6e 67 65 20 3a 20 30 29 0a 20 20 29 3b 0a 20  ange : 0).  );. 
1e61f 20 69 66 28 20 70 4f 70 2d 3e 70 33 20 29 7b 0a   if( pOp->p3 ){.
1e620 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 2b      p->nChange +
1e621 3d 20 75 2e 62 73 2e 6e 43 68 61 6e 67 65 3b 0a  = u.bs.nChange;.
1e622 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 3e      if( pOp->p3>
1e623 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 4d  0 ){.      p->aM
1e624 65 6d 5b 70 4f 70 2d 3e 70 33 5d 2e 75 2e 69 20  em[pOp->p3].u.i 
1e625 2b 3d 20 75 2e 62 73 2e 6e 43 68 61 6e 67 65 3b  += u.bs.nChange;
1e626 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65  .    }.  }.  bre
1e627 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1e628 3a 20 43 72 65 61 74 65 54 61 62 6c 65 20 50 31  : CreateTable P1
1e629 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1e62a 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 74  Allocate a new t
1e62b 61 62 6c 65 20 69 6e 20 74 68 65 20 6d 61 69 6e  able in the main
1e62c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
1e62d 66 20 50 31 3d 3d 30 20 6f 72 20 69 6e 20 74 68  f P1==0 or in th
1e62e 65 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64  e.** auxiliary d
1e62f 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20  atabase file if 
1e630 50 31 3d 3d 31 20 6f 72 20 69 6e 20 61 6e 20 61  P1==1 or in an a
1e631 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
1e632 20 69 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 72   if.** P1>1.  Wr
1e633 69 74 65 20 74 68 65 20 72 6f 6f 74 20 70 61 67  ite the root pag
1e634 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
1e635 6e 65 77 20 74 61 62 6c 65 20 69 6e 74 6f 0a 2a  new table into.*
1e636 2a 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a  * register P2.**
1e637 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e  .** The differen
1e638 63 65 20 62 65 74 77 65 65 6e 20 61 20 74 61 62  ce between a tab
1e639 6c 65 20 61 6e 64 20 61 6e 20 69 6e 64 65 78 20  le and an index 
1e63a 69 73 20 74 68 69 73 3a 20 20 41 20 74 61 62 6c  is this:  A tabl
1e63b 65 20 6d 75 73 74 0a 2a 2a 20 68 61 76 65 20 61  e must.** have a
1e63c 20 34 2d 62 79 74 65 20 69 6e 74 65 67 65 72 20   4-byte integer 
1e63d 6b 65 79 20 61 6e 64 20 63 61 6e 20 68 61 76 65  key and can have
1e63e 20 61 72 62 69 74 72 61 72 79 20 64 61 74 61 2e   arbitrary data.
1e63f 20 20 41 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 61    An index.** ha
1e640 73 20 61 6e 20 61 72 62 69 74 72 61 72 79 20 6b  s an arbitrary k
1e641 65 79 20 62 75 74 20 6e 6f 20 64 61 74 61 2e 0a  ey but no data..
1e642 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
1e643 43 72 65 61 74 65 49 6e 64 65 78 0a 2a 2f 0a 2f  CreateIndex.*/./
1e644 2a 20 4f 70 63 6f 64 65 3a 20 43 72 65 61 74 65  * Opcode: Create
1e645 49 6e 64 65 78 20 50 31 20 50 32 20 2a 20 2a 20  Index P1 P2 * * 
1e646 2a 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  *.**.** Allocate
1e647 20 61 20 6e 65 77 20 69 6e 64 65 78 20 69 6e 20   a new index in 
1e648 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
1e649 65 20 66 69 6c 65 20 69 66 20 50 31 3d 3d 30 20  e file if P1==0 
1e64a 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 75 78  or in the.** aux
1e64b 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20  iliary database 
1e64c 66 69 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f 72  file if P1==1 or
1e64d 20 69 6e 20 61 6e 20 61 74 74 61 63 68 65 64 20   in an attached 
1e64e 64 61 74 61 62 61 73 65 20 69 66 0a 2a 2a 20 50  database if.** P
1e64f 31 3e 31 2e 20 20 57 72 69 74 65 20 74 68 65 20  1>1.  Write the 
1e650 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72  root page number
1e651 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c   of the new tabl
1e652 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74  e into.** regist
1e653 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  er P2..**.** See
1e654 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f   documentation o
1e655 6e 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65  n OP_CreateTable
1e656 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
1e657 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  information..*/.
1e658 63 61 73 65 20 4f 50 5f 43 72 65 61 74 65 49 6e  case OP_CreateIn
1e659 64 65 78 3a 20 20 20 20 20 20 20 20 20 20 20 20  dex:            
1e65a 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61  /* out2-prerelea
1e65b 73 65 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 72  se */.case OP_Cr
1e65c 65 61 74 65 54 61 62 6c 65 3a 20 7b 20 20 20 20  eateTable: {    
1e65d 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
1e65e 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20  erelease */.#if 
1e65f 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1e660 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e661 20 75 2e 62 74 20 2a 2f 0a 20 20 69 6e 74 20 70   u.bt */.  int p
1e662 67 6e 6f 3b 0a 20 20 69 6e 74 20 66 6c 61 67 73  gno;.  int flags
1e663 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 23 65 6e  ;.  Db *pDb;.#en
1e664 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1e665 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e666 6f 20 75 2e 62 74 20 2a 2f 0a 0a 20 20 75 2e 62  o u.bt */..  u.b
1e667 74 2e 70 67 6e 6f 20 3d 20 30 3b 0a 20 20 61 73  t.pgno = 0;.  as
1e668 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
1e669 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e   && pOp->p1<db->
1e66a 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28  nDb );.  assert(
1e66b 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26   (p->btreeMask &
1e66c 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d   (1<<pOp->p1))!=
1e66d 30 20 29 3b 0a 20 20 75 2e 62 74 2e 70 44 62 20  0 );.  u.bt.pDb 
1e66e 3d 20 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e  = &db->aDb[pOp->
1e66f 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75  p1];.  assert( u
1e670 2e 62 74 2e 70 44 62 2d 3e 70 42 74 21 3d 30 20  .bt.pDb->pBt!=0 
1e671 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70  );.  if( pOp->op
1e672 63 6f 64 65 3d 3d 4f 50 5f 43 72 65 61 74 65 54  code==OP_CreateT
1e673 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 75  able ){.    /* u
1e674 2e 62 74 2e 66 6c 61 67 73 20 3d 20 42 54 52 45  .bt.flags = BTRE
1e675 45 5f 49 4e 54 4b 45 59 3b 20 2a 2f 0a 20 20 20  E_INTKEY; */.   
1e676 20 75 2e 62 74 2e 66 6c 61 67 73 20 3d 20 42 54   u.bt.flags = BT
1e677 52 45 45 5f 4c 45 41 46 44 41 54 41 7c 42 54 52  REE_LEAFDATA|BTR
1e678 45 45 5f 49 4e 54 4b 45 59 3b 0a 20 20 7d 65 6c  EE_INTKEY;.  }el
1e679 73 65 7b 0a 20 20 20 20 75 2e 62 74 2e 66 6c 61  se{.    u.bt.fla
1e67a 67 73 20 3d 20 42 54 52 45 45 5f 5a 45 52 4f 44  gs = BTREE_ZEROD
1e67b 41 54 41 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  ATA;.  }.  rc = 
1e67c 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61  sqlite3BtreeCrea
1e67d 74 65 54 61 62 6c 65 28 75 2e 62 74 2e 70 44 62  teTable(u.bt.pDb
1e67e 2d 3e 70 42 74 2c 20 26 75 2e 62 74 2e 70 67 6e  ->pBt, &u.bt.pgn
1e67f 6f 2c 20 75 2e 62 74 2e 66 6c 61 67 73 29 3b 0a  o, u.bt.flags);.
1e680 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e    pOut->u.i = u.
1e681 62 74 2e 70 67 6e 6f 3b 0a 20 20 4d 65 6d 53 65  bt.pgno;.  MemSe
1e682 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
1e683 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61  MEM_Int);.  brea
1e684 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1e685 20 50 61 72 73 65 53 63 68 65 6d 61 20 50 31 20   ParseSchema P1 
1e686 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20  P2 * P4 *.**.** 
1e687 52 65 61 64 20 61 6e 64 20 70 61 72 73 65 20 61  Read and parse a
1e688 6c 6c 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20  ll entries from 
1e689 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45  the SQLITE_MASTE
1e68a 52 20 74 61 62 6c 65 20 6f 66 20 64 61 74 61 62  R table of datab
1e68b 61 73 65 20 50 31 0a 2a 2a 20 74 68 61 74 20 6d  ase P1.** that m
1e68c 61 74 63 68 20 74 68 65 20 57 48 45 52 45 20 63  atch the WHERE c
1e68d 6c 61 75 73 65 20 50 34 2e 20 20 50 32 20 69 73  lause P4.  P2 is
1e68e 20 74 68 65 20 22 66 6f 72 63 65 22 20 66 6c 61   the "force" fla
1e68f 67 2e 20 20 20 41 6c 77 61 79 73 20 64 6f 0a 2a  g.   Always do.*
1e690 2a 20 74 68 65 20 70 61 72 73 69 6e 67 20 69 66  * the parsing if
1e691 20 50 32 20 69 73 20 74 72 75 65 2e 20 20 49 66   P2 is true.  If
1e692 20 50 32 20 69 73 20 66 61 6c 73 65 2c 20 74 68   P2 is false, th
1e693 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
1e694 69 73 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 20 69 66  is a.** no-op if
1e695 20 74 68 65 20 73 63 68 65 6d 61 20 69 73 20 6e   the schema is n
1e696 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 6c 6f 61  ot currently loa
1e697 64 65 64 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  ded.  In other w
1e698 6f 72 64 73 2c 20 69 66 20 50 32 0a 2a 2a 20 69  ords, if P2.** i
1e699 73 20 66 61 6c 73 65 2c 20 74 68 65 20 53 51 4c  s false, the SQL
1e69a 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65  ITE_MASTER table
1e69b 20 69 73 20 6f 6e 6c 79 20 70 61 72 73 65 64 20   is only parsed 
1e69c 69 66 20 74 68 65 20 72 65 73 74 20 6f 66 20 74  if the rest of t
1e69d 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 69 73 20  he.** schema is 
1e69e 61 6c 72 65 61 64 79 20 6c 6f 61 64 65 64 20 69  already loaded i
1e69f 6e 74 6f 20 74 68 65 20 73 79 6d 62 6f 6c 20 74  nto the symbol t
1e6a0 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  able..**.** This
1e6a1 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20   opcode invokes 
1e6a2 74 68 65 20 70 61 72 73 65 72 20 74 6f 20 63 72  the parser to cr
1e6a3 65 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75  eate a new virtu
1e6a4 61 6c 20 6d 61 63 68 69 6e 65 2c 0a 2a 2a 20 74  al machine,.** t
1e6a5 68 65 6e 20 72 75 6e 73 20 74 68 65 20 6e 65 77  hen runs the new
1e6a6 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
1e6a7 2e 20 20 49 74 20 69 73 20 74 68 75 73 20 61 20  .  It is thus a 
1e6a8 72 65 2d 65 6e 74 72 61 6e 74 20 6f 70 63 6f 64  re-entrant opcod
1e6a9 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61  e..*/.case OP_Pa
1e6aa 72 73 65 53 63 68 65 6d 61 3a 20 7b 0a 23 69 66  rseSchema: {.#if
1e6ab 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1e6ac 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e6ad 6f 20 75 2e 62 75 20 2a 2f 0a 20 20 69 6e 74 20  o u.bu */.  int 
1e6ae 69 44 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  iDb;.  const cha
1e6af 72 20 2a 7a 4d 61 73 74 65 72 3b 0a 20 20 63 68  r *zMaster;.  ch
1e6b0 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 49 6e 69 74  ar *zSql;.  Init
1e6b1 44 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0a 23  Data initData;.#
1e6b2 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1e6b3 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1e6b4 6e 74 6f 20 75 2e 62 75 20 2a 2f 0a 0a 20 20 75  nto u.bu */..  u
1e6b5 2e 62 75 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70  .bu.iDb = pOp->p
1e6b6 31 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62  1;.  assert( u.b
1e6b7 75 2e 69 44 62 3e 3d 30 20 26 26 20 75 2e 62 75  u.iDb>=0 && u.bu
1e6b8 2e 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a  .iDb<db->nDb );.
1e6b9 0a 20 20 2f 2a 20 49 66 20 70 4f 70 2d 3e 70 32  .  /* If pOp->p2
1e6ba 20 69 73 20 30 2c 20 74 68 65 6e 20 74 68 69 73   is 0, then this
1e6bb 20 6f 70 63 6f 64 65 20 69 73 20 62 65 69 6e 67   opcode is being
1e6bc 20 65 78 65 63 75 74 65 64 20 74 6f 20 72 65 61   executed to rea
1e6bd 64 20 61 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 20  d a.  ** single 
1e6be 72 6f 77 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65  row, for example
1e6bf 20 74 68 65 20 72 6f 77 20 63 6f 72 72 65 73 70   the row corresp
1e6c0 6f 6e 64 69 6e 67 20 74 6f 20 61 20 6e 65 77 20  onding to a new 
1e6c1 69 6e 64 65 78 0a 20 20 2a 2a 20 63 72 65 61 74  index.  ** creat
1e6c2 65 64 20 62 79 20 74 68 69 73 20 56 44 42 45 2c  ed by this VDBE,
1e6c3 20 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74 65   from the sqlite
1e6c4 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20 49  _master table. I
1e6c5 74 20 6f 6e 6c 79 0a 20 20 2a 2a 20 64 6f 65 73  t only.  ** does
1e6c6 20 74 68 69 73 20 69 66 20 74 68 65 20 63 6f 72   this if the cor
1e6c7 72 65 73 70 6f 6e 64 69 6e 67 20 69 6e 2d 6d 65  responding in-me
1e6c8 6d 6f 72 79 20 73 63 68 65 6d 61 20 69 73 20 63  mory schema is c
1e6c9 75 72 72 65 6e 74 6c 79 0a 20 20 2a 2a 20 6c 6f  urrently.  ** lo
1e6ca 61 64 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c  aded. Otherwise,
1e6cb 20 74 68 65 20 6e 65 77 20 69 6e 64 65 78 20 64   the new index d
1e6cc 65 66 69 6e 69 74 69 6f 6e 20 63 61 6e 20 62 65  efinition can be
1e6cd 20 6c 6f 61 64 65 64 20 61 6c 6f 6e 67 0a 20 20   loaded along.  
1e6ce 2a 2a 20 77 69 74 68 20 74 68 65 20 72 65 73 74  ** with the rest
1e6cf 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 77   of the schema w
1e6d0 68 65 6e 20 69 74 20 69 73 20 72 65 71 75 69 72  hen it is requir
1e6d1 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6c  ed..  **.  ** Al
1e6d2 74 68 6f 75 67 68 20 74 68 65 20 6d 75 74 65 78  though the mutex
1e6d3 20 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65 64   on the BtShared
1e6d4 20 6f 62 6a 65 63 74 20 74 68 61 74 20 63 6f 72   object that cor
1e6d5 72 65 73 70 6f 6e 64 73 20 74 6f 0a 20 20 2a 2a  responds to.  **
1e6d6 20 64 61 74 61 62 61 73 65 20 75 2e 62 75 2e 69   database u.bu.i
1e6d7 44 62 20 28 74 68 65 20 64 61 74 61 62 61 73 65  Db (the database
1e6d8 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
1e6d9 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
1e6da 62 6c 65 0a 20 20 2a 2a 20 72 65 61 64 20 62 79  ble.  ** read by
1e6db 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   this instructio
1e6dc 6e 29 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  n) is currently 
1e6dd 68 65 6c 64 2c 20 69 74 20 69 73 20 6e 65 63 65  held, it is nece
1e6de 73 73 61 72 79 20 74 6f 0a 20 20 2a 2a 20 6f 62  ssary to.  ** ob
1e6df 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78 65 73  tain the mutexes
1e6e0 20 6f 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64   on all attached
1e6e1 20 64 61 74 61 62 61 73 65 73 20 62 65 66 6f 72   databases befor
1e6e2 65 20 63 68 65 63 6b 69 6e 67 20 69 66 0a 20 20  e checking if.  
1e6e3 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 20 6f 66  ** the schema of
1e6e4 20 75 2e 62 75 2e 69 44 62 20 69 73 20 6c 6f 61   u.bu.iDb is loa
1e6e5 64 65 64 2e 20 54 68 69 73 20 69 73 20 62 65 63  ded. This is bec
1e6e6 61 75 73 65 2c 20 61 74 20 74 68 65 20 73 74 61  ause, at the sta
1e6e7 72 74 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 73  rt of.  ** the s
1e6e8 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63 61  qlite3_exec() ca
1e6e9 6c 6c 20 62 65 6c 6f 77 2c 20 53 51 4c 69 74 65  ll below, SQLite
1e6ea 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 0a 20 20 2a   will invoke.  *
1e6eb 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e  * sqlite3BtreeEn
1e6ec 74 65 72 41 6c 6c 28 29 2e 20 49 66 20 61 6c 6c  terAll(). If all
1e6ed 20 6d 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74   mutexes are not
1e6ee 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 74   already held, t
1e6ef 68 65 0a 20 20 2a 2a 20 75 2e 62 75 2e 69 44 62  he.  ** u.bu.iDb
1e6f0 20 6d 75 74 65 78 20 6d 61 79 20 62 65 20 74 65   mutex may be te
1e6f1 6d 70 6f 72 61 72 69 6c 79 20 72 65 6c 65 61 73  mporarily releas
1e6f2 65 64 20 74 6f 20 61 76 6f 69 64 20 64 65 61 64  ed to avoid dead
1e6f3 6c 6f 63 6b 2e 20 49 66 0a 20 20 2a 2a 20 74 68  lock. If.  ** th
1e6f4 69 73 20 68 61 70 70 65 6e 73 2c 20 74 68 65 6e  is happens, then
1e6f5 20 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 65   some other thre
1e6f6 61 64 20 6d 61 79 20 64 65 6c 65 74 65 20 74 68  ad may delete th
1e6f7 65 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20 2a 2a  e in-memory.  **
1e6f8 20 73 63 68 65 6d 61 20 6f 66 20 64 61 74 61 62   schema of datab
1e6f9 61 73 65 20 75 2e 62 75 2e 69 44 62 20 62 65 66  ase u.bu.iDb bef
1e6fa 6f 72 65 20 74 68 65 20 53 51 4c 20 73 74 61 74  ore the SQL stat
1e6fb 65 6d 65 6e 74 20 72 75 6e 73 2e 20 54 68 65 20  ement runs. The 
1e6fc 73 63 68 65 6d 61 0a 20 20 2a 2a 20 77 69 6c 6c  schema.  ** will
1e6fd 20 6e 6f 74 20 62 65 20 72 65 6c 6f 61 64 65 64   not be reloaded
1e6fe 20 62 65 63 75 61 73 65 20 74 68 65 20 64 62 2d   becuase the db-
1e6ff 3e 69 6e 69 74 2e 62 75 73 79 20 66 6c 61 67 20  >init.busy flag 
1e700 69 73 20 73 65 74 2e 20 54 68 69 73 0a 20 20 2a  is set. This.  *
1e701 2a 20 63 61 6e 20 72 65 73 75 6c 74 20 69 6e 20  * can result in 
1e702 61 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65  a "no such table
1e703 3a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22  : sqlite_master"
1e704 20 6f 72 20 22 6d 61 6c 66 6f 72 6d 65 64 0a 20   or "malformed. 
1e705 20 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63 68   ** database sch
1e706 65 6d 61 22 20 65 72 72 6f 72 20 62 65 69 6e 67  ema" error being
1e707 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65   returned to the
1e708 20 75 73 65 72 2e 0a 20 20 2a 2f 0a 20 20 61 73   user..  */.  as
1e709 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
1e70a 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 64 62 2d  eeHoldsMutex(db-
1e70b 3e 61 44 62 5b 75 2e 62 75 2e 69 44 62 5d 2e 70  >aDb[u.bu.iDb].p
1e70c 42 74 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  Bt) );.  sqlite3
1e70d 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62  BtreeEnterAll(db
1e70e 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32  );.  if( pOp->p2
1e70f 20 7c 7c 20 44 62 48 61 73 50 72 6f 70 65 72 74   || DbHasPropert
1e710 79 28 64 62 2c 20 75 2e 62 75 2e 69 44 62 2c 20  y(db, u.bu.iDb, 
1e711 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29  DB_SchemaLoaded)
1e712 20 29 7b 0a 20 20 20 20 75 2e 62 75 2e 7a 4d 61   ){.    u.bu.zMa
1e713 73 74 65 72 20 3d 20 53 43 48 45 4d 41 5f 54 41  ster = SCHEMA_TA
1e714 42 4c 45 28 75 2e 62 75 2e 69 44 62 29 3b 0a 20  BLE(u.bu.iDb);. 
1e715 20 20 20 75 2e 62 75 2e 69 6e 69 74 44 61 74 61     u.bu.initData
1e716 2e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 75 2e  .db = db;.    u.
1e717 62 75 2e 69 6e 69 74 44 61 74 61 2e 69 44 62 20  bu.initData.iDb 
1e718 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 20 20 75  = pOp->p1;.    u
1e719 2e 62 75 2e 69 6e 69 74 44 61 74 61 2e 70 7a 45  .bu.initData.pzE
1e71a 72 72 4d 73 67 20 3d 20 26 70 2d 3e 7a 45 72 72  rrMsg = &p->zErr
1e71b 4d 73 67 3b 0a 20 20 20 20 75 2e 62 75 2e 7a 53  Msg;.    u.bu.zS
1e71c 71 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  ql = sqlite3MPri
1e71d 6e 74 66 28 64 62 2c 0a 20 20 20 20 20 20 20 22  ntf(db,.       "
1e71e 53 45 4c 45 43 54 20 6e 61 6d 65 2c 20 72 6f 6f  SELECT name, roo
1e71f 74 70 61 67 65 2c 20 73 71 6c 20 46 52 4f 4d 20  tpage, sql FROM 
1e720 27 25 71 27 2e 25 73 20 57 48 45 52 45 20 25 73  '%q'.%s WHERE %s
1e721 22 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e 61 44  ",.       db->aD
1e722 62 5b 75 2e 62 75 2e 69 44 62 5d 2e 7a 4e 61 6d  b[u.bu.iDb].zNam
1e723 65 2c 20 75 2e 62 75 2e 7a 4d 61 73 74 65 72 2c  e, u.bu.zMaster,
1e724 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20   pOp->p4.z);.   
1e725 20 69 66 28 20 75 2e 62 75 2e 7a 53 71 6c 3d 3d   if( u.bu.zSql==
1e726 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
1e727 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
1e728 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28    }else{.      (
1e729 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
1e72a 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20  tyOff(db);.     
1e72b 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69   assert( db->ini
1e72c 74 2e 62 75 73 79 3d 3d 30 20 29 3b 0a 20 20 20  t.busy==0 );.   
1e72d 20 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79     db->init.busy
1e72e 20 3d 20 31 3b 0a 20 20 20 20 20 20 75 2e 62 75   = 1;.      u.bu
1e72f 2e 69 6e 69 74 44 61 74 61 2e 72 63 20 3d 20 53  .initData.rc = S
1e730 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
1e731 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c  assert( !db->mal
1e732 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20  locFailed );.   
1e733 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
1e734 65 78 65 63 28 64 62 2c 20 75 2e 62 75 2e 7a 53  exec(db, u.bu.zS
1e735 71 6c 2c 20 73 71 6c 69 74 65 33 49 6e 69 74 43  ql, sqlite3InitC
1e736 61 6c 6c 62 61 63 6b 2c 20 26 75 2e 62 75 2e 69  allback, &u.bu.i
1e737 6e 69 74 44 61 74 61 2c 20 30 29 3b 0a 20 20 20  nitData, 0);.   
1e738 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1e739 45 5f 4f 4b 20 29 20 72 63 20 3d 20 75 2e 62 75  E_OK ) rc = u.bu
1e73a 2e 69 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20 20  .initData.rc;.  
1e73b 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1e73c 65 28 64 62 2c 20 75 2e 62 75 2e 7a 53 71 6c 29  e(db, u.bu.zSql)
1e73d 3b 0a 20 20 20 20 20 20 64 62 2d 3e 69 6e 69 74  ;.      db->init
1e73e 2e 62 75 73 79 20 3d 20 30 3b 0a 20 20 20 20 20  .busy = 0;.     
1e73f 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
1e740 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20  fetyOn(db);.    
1e741 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42  }.  }.  sqlite3B
1e742 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29  treeLeaveAll(db)
1e743 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
1e744 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20  TE_NOMEM ){.    
1e745 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d  goto no_mem;.  }
1e746 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66  .  break;.}..#if
1e747 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1e748 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 29 0a 2f  _OMIT_ANALYZE)./
1e749 2a 20 4f 70 63 6f 64 65 3a 20 4c 6f 61 64 41 6e  * Opcode: LoadAn
1e74a 61 6c 79 73 69 73 20 50 31 20 2a 20 2a 20 2a 20  alysis P1 * * * 
1e74b 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65  *.**.** Read the
1e74c 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
1e74d 62 6c 65 20 66 6f 72 20 64 61 74 61 62 61 73 65  ble for database
1e74e 20 50 31 20 61 6e 64 20 6c 6f 61 64 20 74 68 65   P1 and load the
1e74f 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74   content.** of t
1e750 68 61 74 20 74 61 62 6c 65 20 69 6e 74 6f 20 74  hat table into t
1e751 68 65 20 69 6e 74 65 72 6e 61 6c 20 69 6e 64 65  he internal inde
1e752 78 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54  x hash table.  T
1e753 68 69 73 20 77 69 6c 6c 20 63 61 75 73 65 0a 2a  his will cause.*
1e754 2a 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 74  * the analysis t
1e755 6f 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 70  o be used when p
1e756 72 65 70 61 72 69 6e 67 20 61 6c 6c 20 73 75 62  reparing all sub
1e757 73 65 71 75 65 6e 74 20 71 75 65 72 69 65 73 2e  sequent queries.
1e758 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 6f 61 64  .*/.case OP_Load
1e759 41 6e 61 6c 79 73 69 73 3a 20 7b 0a 20 20 61 73  Analysis: {.  as
1e75a 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
1e75b 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e   && pOp->p1<db->
1e75c 6e 44 62 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  nDb );.  rc = sq
1e75d 6c 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61  lite3AnalysisLoa
1e75e 64 28 64 62 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a  d(db, pOp->p1);.
1e75f 20 20 62 72 65 61 6b 3b 20 20 0a 7d 0a 23 65 6e    break;  .}.#en
1e760 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
1e761 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c  SQLITE_OMIT_ANAL
1e762 59 5a 45 29 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f  YZE) */../* Opco
1e763 64 65 3a 20 44 72 6f 70 54 61 62 6c 65 20 50 31  de: DropTable P1
1e764 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20   * * P4 *.**.** 
1e765 52 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72  Remove the inter
1e766 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20  nal (in-memory) 
1e767 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20  data structures 
1e768 74 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a  that describe.**
1e769 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 64   the table named
1e76a 20 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 20   P4 in database 
1e76b 50 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c  P1.  This is cal
1e76c 6c 65 64 20 61 66 74 65 72 20 61 20 74 61 62 6c  led after a tabl
1e76d 65 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20  e.** is dropped 
1e76e 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70  in order to keep
1e76f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65   the internal re
1e770 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20  presentation of 
1e771 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f  the.** schema co
1e772 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 68  nsistent with wh
1e773 61 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a  at is on disk..*
1e774 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 54 61  /.case OP_DropTa
1e775 62 6c 65 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33  ble: {.  sqlite3
1e776 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54  UnlinkAndDeleteT
1e777 61 62 6c 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31  able(db, pOp->p1
1e778 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20  , pOp->p4.z);.  
1e779 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1e77a 6f 64 65 3a 20 44 72 6f 70 49 6e 64 65 78 20 50  ode: DropIndex P
1e77b 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a  1 * * P4 *.**.**
1e77c 20 52 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65   Remove the inte
1e77d 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29  rnal (in-memory)
1e77e 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73   data structures
1e77f 20 74 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a   that describe.*
1e780 2a 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65  * the index name
1e781 64 20 50 34 20 69 6e 20 64 61 74 61 62 61 73 65  d P4 in database
1e782 20 50 31 2e 20 20 54 68 69 73 20 69 73 20 63 61   P1.  This is ca
1e783 6c 6c 65 64 20 61 66 74 65 72 20 61 6e 20 69 6e  lled after an in
1e784 64 65 78 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65  dex.** is droppe
1e785 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65  d in order to ke
1e786 65 70 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  ep the internal 
1e787 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f  representation o
1e788 66 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20  f the.** schema 
1e789 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20  consistent with 
1e78a 77 68 61 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e  what is on disk.
1e78b 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70  .*/.case OP_Drop
1e78c 49 6e 64 65 78 3a 20 7b 0a 20 20 73 71 6c 69 74  Index: {.  sqlit
1e78d 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74  e3UnlinkAndDelet
1e78e 65 49 6e 64 65 78 28 64 62 2c 20 70 4f 70 2d 3e  eIndex(db, pOp->
1e78f 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a  p1, pOp->p4.z);.
1e790 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1e791 70 63 6f 64 65 3a 20 44 72 6f 70 54 72 69 67 67  pcode: DropTrigg
1e792 65 72 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a  er P1 * * P4 *.*
1e793 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20  *.** Remove the 
1e794 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d  internal (in-mem
1e795 6f 72 79 29 20 64 61 74 61 20 73 74 72 75 63 74  ory) data struct
1e796 75 72 65 73 20 74 68 61 74 20 64 65 73 63 72 69  ures that descri
1e797 62 65 0a 2a 2a 20 74 68 65 20 74 72 69 67 67 65  be.** the trigge
1e798 72 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61  r named P4 in da
1e799 74 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 73  tabase P1.  This
1e79a 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72   is called after
1e79b 20 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 69 73   a trigger.** is
1e79c 20 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 65   dropped in orde
1e79d 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69 6e  r to keep the in
1e79e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74  ternal represent
1e79f 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20  ation of the.** 
1e7a0 73 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e  schema consisten
1e7a1 74 20 77 69 74 68 20 77 68 61 74 20 69 73 20 6f  t with what is o
1e7a2 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20  n disk..*/.case 
1e7a3 4f 50 5f 44 72 6f 70 54 72 69 67 67 65 72 3a 20  OP_DropTrigger: 
1e7a4 7b 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e  {.  sqlite3Unlin
1e7a5 6b 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65  kAndDeleteTrigge
1e7a6 72 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70  r(db, pOp->p1, p
1e7a7 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65  Op->p4.z);.  bre
1e7a8 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20  ak;.}...#ifndef 
1e7a9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45  SQLITE_OMIT_INTE
1e7aa 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 20 4f  GRITY_CHECK./* O
1e7ab 70 63 6f 64 65 3a 20 49 6e 74 65 67 72 69 74 79  pcode: Integrity
1e7ac 43 6b 20 50 31 20 50 32 20 50 33 20 2a 20 50 35  Ck P1 P2 P3 * P5
1e7ad 0a 2a 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 6e 61  .**.** Do an ana
1e7ae 6c 79 73 69 73 20 6f 66 20 74 68 65 20 63 75 72  lysis of the cur
1e7af 72 65 6e 74 6c 79 20 6f 70 65 6e 20 64 61 74 61  rently open data
1e7b0 62 61 73 65 2e 20 20 53 74 6f 72 65 20 69 6e 0a  base.  Store in.
1e7b1 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31 20 74  ** register P1 t
1e7b2 68 65 20 74 65 78 74 20 6f 66 20 61 6e 20 65 72  he text of an er
1e7b3 72 6f 72 20 6d 65 73 73 61 67 65 20 64 65 73 63  ror message desc
1e7b4 72 69 62 69 6e 67 20 61 6e 79 20 70 72 6f 62 6c  ribing any probl
1e7b5 65 6d 73 2e 0a 2a 2a 20 49 66 20 6e 6f 20 70 72  ems..** If no pr
1e7b6 6f 62 6c 65 6d 73 20 61 72 65 20 66 6f 75 6e 64  oblems are found
1e7b7 2c 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 69  , store a NULL i
1e7b8 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a  n register P1..*
1e7b9 2a 0a 2a 2a 20 54 68 65 20 72 65 67 69 73 74 65  *.** The registe
1e7ba 72 20 50 33 20 63 6f 6e 74 61 69 6e 73 20 74 68  r P3 contains th
1e7bb 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
1e7bc 20 6f 66 20 61 6c 6c 6f 77 65 64 20 65 72 72 6f   of allowed erro
1e7bd 72 73 2e 0a 2a 2a 20 41 74 20 6d 6f 73 74 20 72  rs..** At most r
1e7be 65 67 28 50 33 29 20 65 72 72 6f 72 73 20 77 69  eg(P3) errors wi
1e7bf 6c 6c 20 62 65 20 72 65 70 6f 72 74 65 64 2e 0a  ll be reported..
1e7c0 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  ** In other word
1e7c1 73 2c 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20  s, the analysis 
1e7c2 73 74 6f 70 73 20 61 73 20 73 6f 6f 6e 20 61 73  stops as soon as
1e7c3 20 72 65 67 28 50 31 29 20 65 72 72 6f 72 73 20   reg(P1) errors 
1e7c4 61 72 65 20 0a 2a 2a 20 73 65 65 6e 2e 20 20 52  are .** seen.  R
1e7c5 65 67 28 50 31 29 20 69 73 20 75 70 64 61 74 65  eg(P1) is update
1e7c6 64 20 77 69 74 68 20 74 68 65 20 6e 75 6d 62 65  d with the numbe
1e7c7 72 20 6f 66 20 65 72 72 6f 72 73 20 72 65 6d 61  r of errors rema
1e7c8 69 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ining..**.** The
1e7c9 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65   root page numbe
1e7ca 72 73 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73  rs of all tables
1e7cb 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
1e7cc 20 61 72 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20   are integer.** 
1e7cd 73 74 6f 72 65 64 20 69 6e 20 72 65 67 28 50 31  stored in reg(P1
1e7ce 29 2c 20 72 65 67 28 50 31 2b 31 29 2c 20 72 65  ), reg(P1+1), re
1e7cf 67 28 50 31 2b 32 29 2c 20 2e 2e 2e 2e 20 20 54  g(P1+2), ....  T
1e7d0 68 65 72 65 20 61 72 65 20 50 32 20 74 61 62 6c  here are P2 tabl
1e7d1 65 73 0a 2a 2a 20 74 6f 74 61 6c 2e 0a 2a 2a 0a  es.** total..**.
1e7d2 2a 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 74 20  ** If P5 is not 
1e7d3 7a 65 72 6f 2c 20 74 68 65 20 63 68 65 63 6b 20  zero, the check 
1e7d4 69 73 20 64 6f 6e 65 20 6f 6e 20 74 68 65 20 61  is done on the a
1e7d5 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73  uxiliary databas
1e7d6 65 0a 2a 2a 20 66 69 6c 65 2c 20 6e 6f 74 20 74  e.** file, not t
1e7d7 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
1e7d8 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   file..**.** Thi
1e7d9 73 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64  s opcode is used
1e7da 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
1e7db 65 20 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63  e integrity_chec
1e7dc 6b 20 70 72 61 67 6d 61 2e 0a 2a 2f 0a 63 61 73  k pragma..*/.cas
1e7dd 65 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b  e OP_IntegrityCk
1e7de 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f  : {.#if 0  /* lo
1e7df 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1e7e0 76 65 64 20 69 6e 74 6f 20 75 2e 62 76 20 2a 2f  ved into u.bv */
1e7e1 0a 20 20 69 6e 74 20 6e 52 6f 6f 74 3b 20 20 20  .  int nRoot;   
1e7e2 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1e7e3 74 61 62 6c 65 73 20 74 6f 20 63 68 65 63 6b 2e  tables to check.
1e7e4 20 20 28 4e 75 6d 62 65 72 20 6f 66 20 72 6f 6f    (Number of roo
1e7e5 74 20 70 61 67 65 73 2e 29 20 2a 2f 0a 20 20 69  t pages.) */.  i
1e7e6 6e 74 20 2a 61 52 6f 6f 74 3b 20 20 20 20 20 2f  nt *aRoot;     /
1e7e7 2a 20 41 72 72 61 79 20 6f 66 20 72 6f 6f 74 70  * Array of rootp
1e7e8 61 67 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20  age numbers for 
1e7e9 74 61 62 6c 65 73 20 74 6f 20 62 65 20 63 68 65  tables to be che
1e7ea 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b  cked */.  int j;
1e7eb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
1e7ec 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69  p counter */.  i
1e7ed 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 2f  nt nErr;       /
1e7ee 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f  * Number of erro
1e7ef 72 73 20 72 65 70 6f 72 74 65 64 20 2a 2f 0a 20  rs reported */. 
1e7f0 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20   char *z;       
1e7f1 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20   /* Text of the 
1e7f2 65 72 72 6f 72 20 72 65 70 6f 72 74 20 2a 2f 0a  error report */.
1e7f3 20 20 4d 65 6d 20 2a 70 6e 45 72 72 3b 20 20 20    Mem *pnErr;   
1e7f4 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 6b 65    /* Register ke
1e7f5 65 70 69 6e 67 20 74 72 61 63 6b 20 6f 66 20 65  eping track of e
1e7f6 72 72 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 20  rrors remaining 
1e7f7 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  */.#endif /* loc
1e7f8 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e7f9 65 64 20 69 6e 74 6f 20 75 2e 62 76 20 2a 2f 0a  ed into u.bv */.
1e7fa 0a 20 20 75 2e 62 76 2e 6e 52 6f 6f 74 20 3d 20  .  u.bv.nRoot = 
1e7fb 70 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72  pOp->p2;.  asser
1e7fc 74 28 20 75 2e 62 76 2e 6e 52 6f 6f 74 3e 30 20  t( u.bv.nRoot>0 
1e7fd 29 3b 0a 20 20 75 2e 62 76 2e 61 52 6f 6f 74 20  );.  u.bv.aRoot 
1e7fe 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
1e7ff 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28  cRaw(db, sizeof(
1e800 69 6e 74 29 2a 28 75 2e 62 76 2e 6e 52 6f 6f 74  int)*(u.bv.nRoot
1e801 2b 31 29 20 29 3b 0a 20 20 69 66 28 20 75 2e 62  +1) );.  if( u.b
1e802 76 2e 61 52 6f 6f 74 3d 3d 30 20 29 20 67 6f 74  v.aRoot==0 ) got
1e803 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 61 73 73 65  o no_mem;.  asse
1e804 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26  rt( pOp->p3>0 &&
1e805 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65   pOp->p3<=p->nMe
1e806 6d 20 29 3b 0a 20 20 75 2e 62 76 2e 70 6e 45 72  m );.  u.bv.pnEr
1e807 72 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  r = &p->aMem[pOp
1e808 2d 3e 70 33 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p3];.  assert(
1e809 20 28 75 2e 62 76 2e 70 6e 45 72 72 2d 3e 66 6c   (u.bv.pnErr->fl
1e80a 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 21 3d  ags & MEM_Int)!=
1e80b 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  0 );.  assert( (
1e80c 75 2e 62 76 2e 70 6e 45 72 72 2d 3e 66 6c 61 67  u.bv.pnErr->flag
1e80d 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d  s & (MEM_Str|MEM
1e80e 5f 42 6c 6f 62 29 29 3d 3d 30 20 29 3b 0a 20 20  _Blob))==0 );.  
1e80f 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pIn1 = &p->aMem[
1e810 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28  pOp->p1];.  for(
1e811 75 2e 62 76 2e 6a 3d 30 3b 20 75 2e 62 76 2e 6a  u.bv.j=0; u.bv.j
1e812 3c 75 2e 62 76 2e 6e 52 6f 6f 74 3b 20 75 2e 62  <u.bv.nRoot; u.b
1e813 76 2e 6a 2b 2b 29 7b 0a 20 20 20 20 75 2e 62 76  v.j++){.    u.bv
1e814 2e 61 52 6f 6f 74 5b 75 2e 62 76 2e 6a 5d 20 3d  .aRoot[u.bv.j] =
1e815 20 28 69 6e 74 29 73 71 6c 69 74 65 33 56 64 62   (int)sqlite3Vdb
1e816 65 49 6e 74 56 61 6c 75 65 28 26 70 49 6e 31 5b  eIntValue(&pIn1[
1e817 75 2e 62 76 2e 6a 5d 29 3b 0a 20 20 7d 0a 20 20  u.bv.j]);.  }.  
1e818 75 2e 62 76 2e 61 52 6f 6f 74 5b 75 2e 62 76 2e  u.bv.aRoot[u.bv.
1e819 6a 5d 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  j] = 0;.  assert
1e81a 28 20 70 4f 70 2d 3e 70 35 3c 64 62 2d 3e 6e 44  ( pOp->p5<db->nD
1e81b 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  b );.  assert( (
1e81c 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28  p->btreeMask & (
1e81d 31 3c 3c 70 4f 70 2d 3e 70 35 29 29 21 3d 30 20  1<<pOp->p5))!=0 
1e81e 29 3b 0a 20 20 75 2e 62 76 2e 7a 20 3d 20 73 71  );.  u.bv.z = sq
1e81f 6c 69 74 65 33 42 74 72 65 65 49 6e 74 65 67 72  lite3BtreeIntegr
1e820 69 74 79 43 68 65 63 6b 28 64 62 2d 3e 61 44 62  ityCheck(db->aDb
1e821 5b 70 4f 70 2d 3e 70 35 5d 2e 70 42 74 2c 20 75  [pOp->p5].pBt, u
1e822 2e 62 76 2e 61 52 6f 6f 74 2c 20 75 2e 62 76 2e  .bv.aRoot, u.bv.
1e823 6e 52 6f 6f 74 2c 0a 20 20 20 20 20 20 20 20 20  nRoot,.         
1e824 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e825 20 20 20 20 20 20 20 20 28 69 6e 74 29 75 2e 62          (int)u.b
1e826 76 2e 70 6e 45 72 72 2d 3e 75 2e 69 2c 20 26 75  v.pnErr->u.i, &u
1e827 2e 62 76 2e 6e 45 72 72 29 3b 0a 20 20 73 71 6c  .bv.nErr);.  sql
1e828 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 75  ite3DbFree(db, u
1e829 2e 62 76 2e 61 52 6f 6f 74 29 3b 0a 20 20 75 2e  .bv.aRoot);.  u.
1e82a 62 76 2e 70 6e 45 72 72 2d 3e 75 2e 69 20 2d 3d  bv.pnErr->u.i -=
1e82b 20 75 2e 62 76 2e 6e 45 72 72 3b 0a 20 20 73 71   u.bv.nErr;.  sq
1e82c 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e  lite3VdbeMemSetN
1e82d 75 6c 6c 28 70 49 6e 31 29 3b 0a 20 20 69 66 28  ull(pIn1);.  if(
1e82e 20 75 2e 62 76 2e 6e 45 72 72 3d 3d 30 20 29 7b   u.bv.nErr==0 ){
1e82f 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62  .    assert( u.b
1e830 76 2e 7a 3d 3d 30 20 29 3b 0a 20 20 7d 65 6c 73  v.z==0 );.  }els
1e831 65 20 69 66 28 20 75 2e 62 76 2e 7a 3d 3d 30 20  e if( u.bv.z==0 
1e832 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d  ){.    goto no_m
1e833 65 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  em;.  }else{.   
1e834 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1e835 65 74 53 74 72 28 70 49 6e 31 2c 20 75 2e 62 76  etStr(pIn1, u.bv
1e836 2e 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55  .z, -1, SQLITE_U
1e837 54 46 38 2c 20 73 71 6c 69 74 65 33 5f 66 72 65  TF8, sqlite3_fre
1e838 65 29 3b 0a 20 20 7d 0a 20 20 55 50 44 41 54 45  e);.  }.  UPDATE
1e839 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 49  _MAX_BLOBSIZE(pI
1e83a 6e 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  n1);.  sqlite3Vd
1e83b 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67  beChangeEncoding
1e83c 28 70 49 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 29  (pIn1, encoding)
1e83d 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e  ;.  break;.}.#en
1e83e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1e83f 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  IT_INTEGRITY_CHE
1e840 43 4b 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65  CK */../* Opcode
1e841 3a 20 52 6f 77 53 65 74 41 64 64 20 50 31 20 50  : RowSetAdd P1 P
1e842 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e  2 * * *.**.** In
1e843 73 65 72 74 20 74 68 65 20 69 6e 74 65 67 65 72  sert the integer
1e844 20 76 61 6c 75 65 20 68 65 6c 64 20 62 79 20 72   value held by r
1e845 65 67 69 73 74 65 72 20 50 32 20 69 6e 74 6f 20  egister P2 into 
1e846 61 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 0a  a boolean index.
1e847 2a 2a 20 68 65 6c 64 20 69 6e 20 72 65 67 69 73  ** held in regis
1e848 74 65 72 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 41 6e  ter P1..**.** An
1e849 20 61 73 73 65 72 74 69 6f 6e 20 66 61 69 6c 73   assertion fails
1e84a 20 69 66 20 50 32 20 69 73 20 6e 6f 74 20 61 6e   if P2 is not an
1e84b 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 63 61 73   integer..*/.cas
1e84c 65 20 4f 50 5f 52 6f 77 53 65 74 41 64 64 3a 20  e OP_RowSetAdd: 
1e84d 7b 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20 2a  {       /* in2 *
1e84e 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  /.#if 0  /* loca
1e84f 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1e850 64 20 69 6e 74 6f 20 75 2e 62 77 20 2a 2f 0a 20  d into u.bw */. 
1e851 20 4d 65 6d 20 2a 70 49 64 78 3b 0a 20 20 4d 65   Mem *pIdx;.  Me
1e852 6d 20 2a 70 56 61 6c 3b 0a 23 65 6e 64 69 66 20  m *pVal;.#endif 
1e853 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1e854 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1e855 62 77 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  bw */.  assert( 
1e856 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70  pOp->p1>0 && pOp
1e857 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ->p1<=p->nMem );
1e858 0a 20 20 75 2e 62 77 2e 70 49 64 78 20 3d 20 26  .  u.bw.pIdx = &
1e859 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d  p->aMem[pOp->p1]
1e85a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1e85b 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32  >p2>0 && pOp->p2
1e85c 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75  <=p->nMem );.  u
1e85d 2e 62 77 2e 70 56 61 6c 20 3d 20 26 70 2d 3e 61  .bw.pVal = &p->a
1e85e 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20  Mem[pOp->p2];.  
1e85f 61 73 73 65 72 74 28 20 28 75 2e 62 77 2e 70 56  assert( (u.bw.pV
1e860 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  al->flags & MEM_
1e861 49 6e 74 29 21 3d 30 20 29 3b 0a 20 20 69 66 28  Int)!=0 );.  if(
1e862 20 28 75 2e 62 77 2e 70 49 64 78 2d 3e 66 6c 61   (u.bw.pIdx->fla
1e863 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29  gs & MEM_RowSet)
1e864 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
1e865 65 33 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53  e3VdbeMemSetRowS
1e866 65 74 28 75 2e 62 77 2e 70 49 64 78 29 3b 0a 20  et(u.bw.pIdx);. 
1e867 20 20 20 69 66 28 20 28 75 2e 62 77 2e 70 49 64     if( (u.bw.pId
1e868 78 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  x->flags & MEM_R
1e869 6f 77 53 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f  owSet)==0 ) goto
1e86a 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 73   no_mem;.  }.  s
1e86b 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65  qlite3RowSetInse
1e86c 72 74 28 75 2e 62 77 2e 70 49 64 78 2d 3e 75 2e  rt(u.bw.pIdx->u.
1e86d 70 52 6f 77 53 65 74 2c 20 75 2e 62 77 2e 70 56  pRowSet, u.bw.pV
1e86e 61 6c 2d 3e 75 2e 69 29 3b 0a 20 20 62 72 65 61  al->u.i);.  brea
1e86f 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1e870 20 52 6f 77 53 65 74 52 65 61 64 20 50 31 20 50   RowSetRead P1 P
1e871 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 45  2 P3 * *.**.** E
1e872 78 74 72 61 63 74 20 74 68 65 20 73 6d 61 6c 6c  xtract the small
1e873 65 73 74 20 76 61 6c 75 65 20 66 72 6f 6d 20 62  est value from b
1e874 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 20 50 31 20  oolean index P1 
1e875 61 6e 64 20 70 75 74 20 74 68 61 74 20 76 61 6c  and put that val
1e876 75 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73  ue into.** regis
1e877 74 65 72 20 50 33 2e 20 20 4f 72 2c 20 69 66 20  ter P3.  Or, if 
1e878 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 20 50 31  boolean index P1
1e879 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 65 6d   is initially em
1e87a 70 74 79 2c 20 6c 65 61 76 65 20 50 33 0a 2a 2a  pty, leave P3.**
1e87b 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 6a   unchanged and j
1e87c 75 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69  ump to instructi
1e87d 6f 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f  on P2..*/.case O
1e87e 50 5f 52 6f 77 53 65 74 52 65 61 64 3a 20 7b 20  P_RowSetRead: { 
1e87f 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 6f        /* jump, o
1e880 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a  ut3 */.#if 0  /*
1e881 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e882 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 78   moved into u.bx
1e883 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 64 78 3b   */.  Mem *pIdx;
1e884 0a 20 20 69 36 34 20 76 61 6c 3b 0a 23 65 6e 64  .  i64 val;.#end
1e885 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1e886 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e887 20 75 2e 62 78 20 2a 2f 0a 20 20 61 73 73 65 72   u.bx */.  asser
1e888 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20  t( pOp->p1>0 && 
1e889 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p1<=p->nMem
1e88a 20 29 3b 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f   );.  CHECK_FOR_
1e88b 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 75 2e 62  INTERRUPT;.  u.b
1e88c 78 2e 70 49 64 78 20 3d 20 26 70 2d 3e 61 4d 65  x.pIdx = &p->aMe
1e88d 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 4f  m[pOp->p1];.  pO
1e88e 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  ut = &p->aMem[pO
1e88f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 28 75  p->p3];.  if( (u
1e890 2e 62 78 2e 70 49 64 78 2d 3e 66 6c 61 67 73 20  .bx.pIdx->flags 
1e891 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30  & MEM_RowSet)==0
1e892 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 52 6f  .   || sqlite3Ro
1e893 77 53 65 74 4e 65 78 74 28 75 2e 62 78 2e 70 49  wSetNext(u.bx.pI
1e894 64 78 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 26  dx->u.pRowSet, &
1e895 75 2e 62 78 2e 76 61 6c 29 3d 3d 30 0a 20 20 29  u.bx.val)==0.  )
1e896 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 62 6f 6f  {.    /* The boo
1e897 6c 65 61 6e 20 69 6e 64 65 78 20 69 73 20 65 6d  lean index is em
1e898 70 74 79 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  pty */.    sqlit
1e899 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c  e3VdbeMemSetNull
1e89a 28 75 2e 62 78 2e 70 49 64 78 29 3b 0a 20 20 20  (u.bx.pIdx);.   
1e89b 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1e89c 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  1;.  }else{.    
1e89d 2f 2a 20 41 20 76 61 6c 75 65 20 77 61 73 20 70  /* A value was p
1e89e 75 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 69  ulled from the i
1e89f 6e 64 65 78 20 2a 2f 0a 20 20 20 20 61 73 73 65  ndex */.    asse
1e8a0 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26  rt( pOp->p3>0 &&
1e8a1 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65   pOp->p3<=p->nMe
1e8a2 6d 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  m );.    sqlite3
1e8a3 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28  VdbeMemSetInt64(
1e8a4 70 4f 75 74 2c 20 75 2e 62 78 2e 76 61 6c 29 3b  pOut, u.bx.val);
1e8a5 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1e8a6 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53  ./* Opcode: RowS
1e8a7 65 74 54 65 73 74 20 50 31 20 50 32 20 50 33 20  etTest P1 P2 P3 
1e8a8 50 34 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65  P4.**.** Registe
1e8a9 72 20 50 33 20 69 73 20 61 73 73 75 6d 65 64 20  r P3 is assumed 
1e8aa 74 6f 20 68 6f 6c 64 20 61 20 36 34 2d 62 69 74  to hold a 64-bit
1e8ab 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 20   integer value. 
1e8ac 49 66 20 72 65 67 69 73 74 65 72 20 50 31 0a 2a  If register P1.*
1e8ad 2a 20 63 6f 6e 74 61 69 6e 73 20 61 20 52 6f 77  * contains a Row
1e8ae 53 65 74 20 6f 62 6a 65 63 74 20 61 6e 64 20 74  Set object and t
1e8af 68 61 74 20 52 6f 77 53 65 74 20 6f 62 6a 65 63  hat RowSet objec
1e8b0 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68  t contains.** th
1e8b1 65 20 76 61 6c 75 65 20 68 65 6c 64 20 69 6e 20  e value held in 
1e8b2 50 33 2c 20 6a 75 6d 70 20 74 6f 20 72 65 67 69  P3, jump to regi
1e8b3 73 74 65 72 20 50 32 2e 20 4f 74 68 65 72 77 69  ster P2. Otherwi
1e8b4 73 65 2c 20 69 6e 73 65 72 74 20 74 68 65 0a 2a  se, insert the.*
1e8b5 2a 20 69 6e 74 65 67 65 72 20 69 6e 20 50 33 20  * integer in P3 
1e8b6 69 6e 74 6f 20 74 68 65 20 52 6f 77 53 65 74 20  into the RowSet 
1e8b7 61 6e 64 20 63 6f 6e 74 69 6e 75 65 20 6f 6e 20  and continue on 
1e8b8 74 6f 20 74 68 65 0a 2a 2a 20 6e 65 78 74 20 6f  to the.** next o
1e8b9 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  pcode..**.** The
1e8ba 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 20 69   RowSet object i
1e8bb 73 20 6f 70 74 69 6d 69 7a 65 64 20 66 6f 72 20  s optimized for 
1e8bc 74 68 65 20 63 61 73 65 20 77 68 65 72 65 20 73  the case where s
1e8bd 75 63 63 65 73 73 69 76 65 20 73 65 74 73 0a 2a  uccessive sets.*
1e8be 2a 20 6f 66 20 69 6e 74 65 67 65 72 73 2c 20 77  * of integers, w
1e8bf 68 65 72 65 20 65 61 63 68 20 73 65 74 20 63 6f  here each set co
1e8c0 6e 74 61 69 6e 73 20 6e 6f 20 64 75 70 6c 69 63  ntains no duplic
1e8c1 61 74 65 73 2e 20 45 61 63 68 20 73 65 74 0a 2a  ates. Each set.*
1e8c2 2a 20 6f 66 20 76 61 6c 75 65 73 20 69 73 20 69  * of values is i
1e8c3 64 65 6e 74 69 66 69 65 64 20 62 79 20 61 20 75  dentified by a u
1e8c4 6e 69 71 75 65 20 50 34 20 76 61 6c 75 65 2e 20  nique P4 value. 
1e8c5 54 68 65 20 66 69 72 73 74 20 73 65 74 0a 2a 2a  The first set.**
1e8c6 20 6d 75 73 74 20 68 61 76 65 20 50 34 3d 3d 30   must have P4==0
1e8c7 2c 20 74 68 65 20 66 69 6e 61 6c 20 73 65 74 20  , the final set 
1e8c8 50 34 3d 2d 31 2e 20 20 50 34 20 6d 75 73 74 20  P4=-1.  P4 must 
1e8c9 62 65 20 65 69 74 68 65 72 20 2d 31 20 6f 72 0a  be either -1 or.
1e8ca 2a 2a 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e  ** non-negative.
1e8cb 20 20 46 6f 72 20 6e 6f 6e 2d 6e 65 67 61 74 69    For non-negati
1e8cc 76 65 20 76 61 6c 75 65 73 20 6f 66 20 50 34 20  ve values of P4 
1e8cd 6f 6e 6c 79 20 74 68 65 20 6c 6f 77 65 72 20 34  only the lower 4
1e8ce 0a 2a 2a 20 62 69 74 73 20 61 72 65 20 73 69 67  .** bits are sig
1e8cf 6e 69 66 69 63 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20  nificant..**.** 
1e8d0 54 68 69 73 20 61 6c 6c 6f 77 73 20 6f 70 74 69  This allows opti
1e8d1 6d 69 7a 61 74 69 6f 6e 73 3a 20 28 61 29 20 77  mizations: (a) w
1e8d2 68 65 6e 20 50 34 3d 3d 30 20 74 68 65 72 65 20  hen P4==0 there 
1e8d3 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 74 65  is no need to te
1e8d4 73 74 0a 2a 2a 20 74 68 65 20 72 6f 77 73 65 74  st.** the rowset
1e8d5 20 6f 62 6a 65 63 74 20 66 6f 72 20 50 33 2c 20   object for P3, 
1e8d6 61 73 20 69 74 20 69 73 20 67 75 61 72 61 6e 74  as it is guarant
1e8d7 65 65 64 20 6e 6f 74 20 74 6f 20 63 6f 6e 74 61  eed not to conta
1e8d8 69 6e 20 69 74 2c 0a 2a 2a 20 28 62 29 20 77 68  in it,.** (b) wh
1e8d9 65 6e 20 50 34 3d 3d 2d 31 20 74 68 65 72 65 20  en P4==-1 there 
1e8da 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 69 6e  is no need to in
1e8db 73 65 72 74 20 74 68 65 20 76 61 6c 75 65 2c 20  sert the value, 
1e8dc 61 73 20 69 74 20 77 69 6c 6c 0a 2a 2a 20 6e 65  as it will.** ne
1e8dd 76 65 72 20 62 65 20 74 65 73 74 65 64 20 66 6f  ver be tested fo
1e8de 72 2c 20 61 6e 64 20 28 63 29 20 77 68 65 6e 20  r, and (c) when 
1e8df 61 20 76 61 6c 75 65 20 74 68 61 74 20 69 73 20  a value that is 
1e8e0 70 61 72 74 20 6f 66 20 73 65 74 20 58 20 69 73  part of set X is
1e8e1 0a 2a 2a 20 69 6e 73 65 72 74 65 64 2c 20 74 68  .** inserted, th
1e8e2 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74  ere is no need t
1e8e3 6f 20 73 65 61 72 63 68 20 74 6f 20 73 65 65 20  o search to see 
1e8e4 69 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75  if the same valu
1e8e5 65 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75  e was.** previou
1e8e6 73 6c 79 20 69 6e 73 65 72 74 65 64 20 61 73 20  sly inserted as 
1e8e7 70 61 72 74 20 6f 66 20 73 65 74 20 58 20 28 6f  part of set X (o
1e8e8 6e 6c 79 20 69 66 20 69 74 20 77 61 73 20 70 72  nly if it was pr
1e8e9 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 69 6e 73 65  eviously.** inse
1e8ea 72 74 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  rted as part of 
1e8eb 73 6f 6d 65 20 6f 74 68 65 72 20 73 65 74 29 2e  some other set).
1e8ec 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53  .*/.case OP_RowS
1e8ed 65 74 54 65 73 74 3a 20 7b 20 20 20 20 20 20 20  etTest: {       
1e8ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1e8ef 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20   jump, in1, in3 
1e8f0 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  */.#if 0  /* loc
1e8f1 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e8f2 65 64 20 69 6e 74 6f 20 75 2e 62 79 20 2a 2f 0a  ed into u.by */.
1e8f3 20 20 69 6e 74 20 69 53 65 74 3b 0a 20 20 69 6e    int iSet;.  in
1e8f4 74 20 65 78 69 73 74 73 3b 0a 23 65 6e 64 69 66  t exists;.#endif
1e8f5 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1e8f6 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1e8f7 2e 62 79 20 2a 2f 0a 0a 20 20 75 2e 62 79 2e 69  .by */..  u.by.i
1e8f8 53 65 74 20 3d 20 70 4f 70 2d 3e 70 34 2e 69 3b  Set = pOp->p4.i;
1e8f9 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d  .  assert( pIn3-
1e8fa 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29  >flags&MEM_Int )
1e8fb 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  ;..  /* If there
1e8fc 20 69 73 20 61 6e 79 74 68 69 6e 67 20 6f 74 68   is anything oth
1e8fd 65 72 20 74 68 61 6e 20 61 20 72 6f 77 73 65 74  er than a rowset
1e8fe 20 6f 62 6a 65 63 74 20 69 6e 20 6d 65 6d 6f 72   object in memor
1e8ff 79 20 63 65 6c 6c 20 50 31 2c 0a 20 20 2a 2a 20  y cell P1,.  ** 
1e900 64 65 6c 65 74 65 20 69 74 20 6e 6f 77 20 61 6e  delete it now an
1e901 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 50 31 20  d initialize P1 
1e902 77 69 74 68 20 61 6e 20 65 6d 70 74 79 20 72 6f  with an empty ro
1e903 77 73 65 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20  wset.  */.  if( 
1e904 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d  (pIn1->flags & M
1e905 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 7b  EM_RowSet)==0 ){
1e906 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1e907 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 70 49 6e  MemSetRowSet(pIn
1e908 31 29 3b 0a 20 20 20 20 69 66 28 20 28 70 49 6e  1);.    if( (pIn
1e909 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  1->flags & MEM_R
1e90a 6f 77 53 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f  owSet)==0 ) goto
1e90b 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 20   no_mem;.  }..  
1e90c 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74  assert( pOp->p4t
1e90d 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b  ype==P4_INT32 );
1e90e 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 79 2e  .  assert( u.by.
1e90f 69 53 65 74 3d 3d 2d 31 20 7c 7c 20 75 2e 62 79  iSet==-1 || u.by
1e910 2e 69 53 65 74 3e 3d 30 20 29 3b 0a 20 20 69 66  .iSet>=0 );.  if
1e911 28 20 75 2e 62 79 2e 69 53 65 74 20 29 7b 0a 20  ( u.by.iSet ){. 
1e912 20 20 20 75 2e 62 79 2e 65 78 69 73 74 73 20 3d     u.by.exists =
1e913 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65   sqlite3RowSetTe
1e914 73 74 28 70 49 6e 31 2d 3e 75 2e 70 52 6f 77 53  st(pIn1->u.pRowS
1e915 65 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  et,.            
1e916 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e917 20 20 20 28 75 38 29 28 75 2e 62 79 2e 69 53 65     (u8)(u.by.iSe
1e918 74 3e 3d 30 20 3f 20 75 2e 62 79 2e 69 53 65 74  t>=0 ? u.by.iSet
1e919 20 26 20 30 78 66 20 3a 20 30 78 66 66 29 2c 0a   & 0xf : 0xff),.
1e91a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e91b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
1e91c 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 69  In3->u.i);.    i
1e91d 66 28 20 75 2e 62 79 2e 65 78 69 73 74 73 20 29  f( u.by.exists )
1e91e 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70  {.      pc = pOp
1e91f 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20  ->p2 - 1;.      
1e920 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
1e921 0a 20 20 69 66 28 20 75 2e 62 79 2e 69 53 65 74  .  if( u.by.iSet
1e922 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  >=0 ){.    sqlit
1e923 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 70  e3RowSetInsert(p
1e924 49 6e 31 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20  In1->u.pRowSet, 
1e925 70 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 7d 0a  pIn3->u.i);.  }.
1e926 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66    break;.}...#if
1e927 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1e928 5f 54 52 49 47 47 45 52 0a 0a 2f 2a 20 4f 70 63  _TRIGGER../* Opc
1e929 6f 64 65 3a 20 50 72 6f 67 72 61 6d 20 50 31 20  ode: Program P1 
1e92a 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a  P2 P3 P4 *.**.**
1e92b 20 45 78 65 63 75 74 65 20 74 68 65 20 74 72 69   Execute the tri
1e92c 67 67 65 72 20 70 72 6f 67 72 61 6d 20 70 61 73  gger program pas
1e92d 73 65 64 20 61 73 20 50 34 20 28 74 79 70 65 20  sed as P4 (type 
1e92e 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 29 2e 20  P4_SUBPROGRAM). 
1e92f 0a 2a 2a 0a 2a 2a 20 50 31 20 63 6f 6e 74 61 69  .**.** P1 contai
1e930 6e 73 20 74 68 65 20 61 64 64 72 65 73 73 20 6f  ns the address o
1e931 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c  f the memory cel
1e932 6c 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  l that contains 
1e933 74 68 65 20 66 69 72 73 74 20 6d 65 6d 6f 72 79  the first memory
1e934 20 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20 61 6e 20   .** cell in an 
1e935 61 72 72 61 79 20 6f 66 20 76 61 6c 75 65 73 20  array of values 
1e936 75 73 65 64 20 61 73 20 61 72 67 75 6d 65 6e 74  used as argument
1e937 73 20 74 6f 20 74 68 65 20 73 75 62 2d 70 72 6f  s to the sub-pro
1e938 67 72 61 6d 2e 20 50 32 20 0a 2a 2a 20 63 6f 6e  gram. P2 .** con
1e939 74 61 69 6e 73 20 74 68 65 20 61 64 64 72 65 73  tains the addres
1e93a 73 20 74 6f 20 6a 75 6d 70 20 74 6f 20 69 66 20  s to jump to if 
1e93b 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20  the sub-program 
1e93c 74 68 72 6f 77 73 20 61 6e 20 49 47 4e 4f 52 45  throws an IGNORE
1e93d 20 0a 2a 2a 20 65 78 63 65 70 74 69 6f 6e 20 75   .** exception u
1e93e 73 69 6e 67 20 74 68 65 20 52 41 49 53 45 28 29  sing the RAISE()
1e93f 20 66 75 6e 63 74 69 6f 6e 2e 20 52 65 67 69 73   function. Regis
1e940 74 65 72 20 50 33 20 63 6f 6e 74 61 69 6e 73 20  ter P3 contains 
1e941 74 68 65 20 61 64 64 72 65 73 73 20 0a 2a 2a 20  the address .** 
1e942 6f 66 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  of a memory cell
1e943 20 69 6e 20 74 68 69 73 20 28 74 68 65 20 70 61   in this (the pa
1e944 72 65 6e 74 29 20 56 4d 20 74 68 61 74 20 69 73  rent) VM that is
1e945 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74   used to allocat
1e946 65 20 74 68 65 20 0a 2a 2a 20 6d 65 6d 6f 72 79  e the .** memory
1e947 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65   required by the
1e948 20 73 75 62 2d 76 64 62 65 20 61 74 20 72 75 6e   sub-vdbe at run
1e949 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69  time..**.** P4 i
1e94a 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
1e94b 68 65 20 56 4d 20 63 6f 6e 74 61 69 6e 69 6e 67  he VM containing
1e94c 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72 6f   the trigger pro
1e94d 67 72 61 6d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  gram..*/.case OP
1e94e 5f 50 72 6f 67 72 61 6d 3a 20 7b 20 20 20 20 20  _Program: {     
1e94f 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69     /* jump */.#i
1e950 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1e951 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1e952 74 6f 20 75 2e 62 7a 20 2a 2f 0a 20 20 69 6e 74  to u.bz */.  int
1e953 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20   nMem;          
1e954 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1e955 66 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65  f memory registe
1e956 72 73 20 66 6f 72 20 73 75 62 2d 70 72 6f 67 72  rs for sub-progr
1e957 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74  am */.  int nByt
1e958 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
1e959 2f 2a 20 42 79 74 65 73 20 6f 66 20 72 75 6e 74  /* Bytes of runt
1e95a 69 6d 65 20 73 70 61 63 65 20 72 65 71 75 69 72  ime space requir
1e95b 65 64 20 66 6f 72 20 73 75 62 2d 70 72 6f 67 72  ed for sub-progr
1e95c 61 6d 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 74  am */.  Mem *pRt
1e95d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1e95e 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 61  /* Register to a
1e95f 6c 6c 6f 63 61 74 65 20 72 75 6e 74 69 6d 65 20  llocate runtime 
1e960 73 70 61 63 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a  space */.  Mem *
1e961 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20  pMem;           
1e962 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74     /* Used to it
1e963 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 6d 65  erate through me
1e964 6d 6f 72 79 20 63 65 6c 6c 73 20 2a 2f 0a 20 20  mory cells */.  
1e965 4d 65 6d 20 2a 70 45 6e 64 3b 20 20 20 20 20 20  Mem *pEnd;      
1e966 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20          /* Last 
1e967 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 6e  memory cell in n
1e968 65 77 20 61 72 72 61 79 20 2a 2f 0a 20 20 56 64  ew array */.  Vd
1e969 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b  beFrame *pFrame;
1e96a 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 64 62        /* New vdb
1e96b 65 20 66 72 61 6d 65 20 74 6f 20 65 78 65 63 75  e frame to execu
1e96c 74 65 20 69 6e 20 2a 2f 0a 20 20 53 75 62 50 72  te in */.  SubPr
1e96d 6f 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b  ogram *pProgram;
1e96e 20 20 20 2f 2a 20 53 75 62 2d 70 72 6f 67 72 61     /* Sub-progra
1e96f 6d 20 74 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a  m to execute */.
1e970 20 20 76 6f 69 64 20 2a 74 3b 20 20 20 20 20 20    void *t;      
1e971 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b            /* Tok
1e972 65 6e 20 69 64 65 6e 74 69 66 79 69 6e 67 20 74  en identifying t
1e973 72 69 67 67 65 72 20 2a 2f 0a 23 65 6e 64 69 66  rigger */.#endif
1e974 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1e975 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1e976 2e 62 7a 20 2a 2f 0a 0a 20 20 75 2e 62 7a 2e 70  .bz */..  u.bz.p
1e977 50 72 6f 67 72 61 6d 20 3d 20 70 4f 70 2d 3e 70  Program = pOp->p
1e978 34 2e 70 50 72 6f 67 72 61 6d 3b 0a 20 20 75 2e  4.pProgram;.  u.
1e979 62 7a 2e 70 52 74 20 3d 20 26 70 2d 3e 61 4d 65  bz.pRt = &p->aMe
1e97a 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 61 73  m[pOp->p3];.  as
1e97b 73 65 72 74 28 20 75 2e 62 7a 2e 70 50 72 6f 67  sert( u.bz.pProg
1e97c 72 61 6d 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 0a 20  ram->nOp>0 );.. 
1e97d 20 2f 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54   /* If the SQLIT
1e97e 45 5f 52 65 63 54 72 69 67 67 65 72 73 20 66 6c  E_RecTriggers fl
1e97f 61 67 20 69 73 20 63 6c 65 61 72 2c 20 74 68 65  ag is clear, the
1e980 6e 20 72 65 63 75 72 73 69 76 65 20 69 6e 76 6f  n recursive invo
1e981 63 61 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74  cation of.  ** t
1e982 72 69 67 67 65 72 73 20 69 73 20 64 69 73 61 62  riggers is disab
1e983 6c 65 64 20 66 6f 72 20 62 61 63 6b 77 61 72 64  led for backward
1e984 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20  s compatibility 
1e985 28 66 6c 61 67 20 73 65 74 2f 63 6c 65 61 72 65  (flag set/cleare
1e986 64 20 62 79 0a 20 20 2a 2a 20 74 68 65 20 22 50  d by.  ** the "P
1e987 52 41 47 4d 41 20 72 65 63 75 72 73 69 76 65 5f  RAGMA recursive_
1e988 74 72 69 67 67 65 72 73 22 20 63 6f 6d 6d 61 6e  triggers" comman
1e989 64 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 74  d)..  **.  ** It
1e98a 20 69 73 20 72 65 63 75 72 73 69 76 65 20 69 6e   is recursive in
1e98b 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 72 69 67  vocation of trig
1e98c 67 65 72 73 2c 20 61 74 20 74 68 65 20 53 51 4c  gers, at the SQL
1e98d 20 6c 65 76 65 6c 2c 20 74 68 61 74 20 69 73 0a   level, that is.
1e98e 20 20 2a 2a 20 64 69 73 61 62 6c 65 64 2e 20 49    ** disabled. I
1e98f 6e 20 73 6f 6d 65 20 63 61 73 65 73 20 61 20 73  n some cases a s
1e990 69 6e 67 6c 65 20 74 72 69 67 67 65 72 20 6d 61  ingle trigger ma
1e991 79 20 67 65 6e 65 72 61 74 65 20 6d 6f 72 65 20  y generate more 
1e992 74 68 61 6e 20 6f 6e 65 0a 20 20 2a 2a 20 53 75  than one.  ** Su
1e993 62 50 72 6f 67 72 61 6d 20 28 69 66 20 74 68 65  bProgram (if the
1e994 20 74 72 69 67 67 65 72 20 6d 61 79 20 62 65 20   trigger may be 
1e995 65 78 65 63 75 74 65 64 20 77 69 74 68 20 6d 6f  executed with mo
1e996 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 69 66 66  re than one diff
1e997 65 72 65 6e 74 0a 20 20 2a 2a 20 4f 4e 20 43 4f  erent.  ** ON CO
1e998 4e 46 4c 49 43 54 20 61 6c 67 6f 72 69 74 68 6d  NFLICT algorithm
1e999 29 2e 20 53 75 62 50 72 6f 67 72 61 6d 20 73 74  ). SubProgram st
1e99a 72 75 63 74 75 72 65 73 20 61 73 73 6f 63 69 61  ructures associa
1e99b 74 65 64 20 77 69 74 68 20 61 0a 20 20 2a 2a 20  ted with a.  ** 
1e99c 73 69 6e 67 6c 65 20 74 72 69 67 67 65 72 20 61  single trigger a
1e99d 6c 6c 20 68 61 76 65 20 74 68 65 20 73 61 6d 65  ll have the same
1e99e 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 53   value for the S
1e99f 75 62 50 72 6f 67 72 61 6d 2e 74 6f 6b 65 6e 0a  ubProgram.token.
1e9a0 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 2e 0a 20    ** variable.. 
1e9a1 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 28 64 62   */.  if( 0==(db
1e9a2 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52  ->flags&SQLITE_R
1e9a3 65 63 54 72 69 67 67 65 72 73 29 20 29 7b 0a 20  ecTriggers) ){. 
1e9a4 20 20 20 75 2e 62 7a 2e 74 20 3d 20 75 2e 62 7a     u.bz.t = u.bz
1e9a5 2e 70 50 72 6f 67 72 61 6d 2d 3e 74 6f 6b 65 6e  .pProgram->token
1e9a6 3b 0a 20 20 20 20 66 6f 72 28 75 2e 62 7a 2e 70  ;.    for(u.bz.p
1e9a7 46 72 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65 3b  Frame=p->pFrame;
1e9a8 20 75 2e 62 7a 2e 70 46 72 61 6d 65 20 26 26 20   u.bz.pFrame && 
1e9a9 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 74 6f 6b  u.bz.pFrame->tok
1e9aa 65 6e 21 3d 75 2e 62 7a 2e 74 3b 20 75 2e 62 7a  en!=u.bz.t; u.bz
1e9ab 2e 70 46 72 61 6d 65 3d 75 2e 62 7a 2e 70 46 72  .pFrame=u.bz.pFr
1e9ac 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20  ame->pParent);. 
1e9ad 20 20 20 69 66 28 20 75 2e 62 7a 2e 70 46 72 61     if( u.bz.pFra
1e9ae 6d 65 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  me ) break;.  }.
1e9af 0a 20 20 69 66 28 20 70 2d 3e 6e 46 72 61 6d 65  .  if( p->nFrame
1e9b0 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
1e9b1 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45 52  TE_LIMIT_TRIGGER
1e9b2 5f 44 45 50 54 48 5d 20 29 7b 0a 20 20 20 20 72  _DEPTH] ){.    r
1e9b3 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
1e9b4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74  ;.    sqlite3Set
1e9b5 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d  String(&p->zErrM
1e9b6 73 67 2c 20 64 62 2c 20 22 74 6f 6f 20 6d 61 6e  sg, db, "too man
1e9b7 79 20 6c 65 76 65 6c 73 20 6f 66 20 74 72 69 67  y levels of trig
1e9b8 67 65 72 20 72 65 63 75 72 73 69 6f 6e 22 29 3b  ger recursion");
1e9b9 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  .    break;.  }.
1e9ba 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 75  .  /* Register u
1e9bb 2e 62 7a 2e 70 52 74 20 69 73 20 75 73 65 64 20  .bz.pRt is used 
1e9bc 74 6f 20 73 74 6f 72 65 20 74 68 65 20 6d 65 6d  to store the mem
1e9bd 6f 72 79 20 72 65 71 75 69 72 65 64 20 74 6f 20  ory required to 
1e9be 73 61 76 65 20 74 68 65 20 73 74 61 74 65 0a 20  save the state. 
1e9bf 20 2a 2a 20 6f 66 20 74 68 65 20 63 75 72 72 65   ** of the curre
1e9c0 6e 74 20 70 72 6f 67 72 61 6d 2c 20 61 6e 64 20  nt program, and 
1e9c1 74 68 65 20 6d 65 6d 6f 72 79 20 72 65 71 75 69  the memory requi
1e9c2 72 65 64 20 61 74 20 72 75 6e 74 69 6d 65 20 74  red at runtime t
1e9c3 6f 20 65 78 65 63 75 74 65 0a 20 20 2a 2a 20 74  o execute.  ** t
1e9c4 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  he trigger progr
1e9c5 61 6d 2e 20 49 66 20 74 68 69 73 20 74 72 69 67  am. If this trig
1e9c6 67 65 72 20 68 61 73 20 62 65 65 6e 20 66 69 72  ger has been fir
1e9c7 65 64 20 62 65 66 6f 72 65 2c 20 74 68 65 6e 20  ed before, then 
1e9c8 75 2e 62 7a 2e 70 52 74 0a 20 20 2a 2a 20 69 73  u.bz.pRt.  ** is
1e9c9 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74   already allocat
1e9ca 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  ed. Otherwise, i
1e9cb 74 20 6d 75 73 74 20 62 65 20 69 6e 69 74 69 61  t must be initia
1e9cc 6c 69 7a 65 64 2e 20 20 2a 2f 0a 20 20 69 66 28  lized.  */.  if(
1e9cd 20 28 75 2e 62 7a 2e 70 52 74 2d 3e 66 6c 61 67   (u.bz.pRt->flag
1e9ce 73 26 4d 45 4d 5f 46 72 61 6d 65 29 3d 3d 30 20  s&MEM_Frame)==0 
1e9cf 29 7b 0a 20 20 20 20 2f 2a 20 53 75 62 50 72 6f  ){.    /* SubPro
1e9d0 67 72 61 6d 2e 6e 4d 65 6d 20 69 73 20 73 65 74  gram.nMem is set
1e9d1 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   to the number o
1e9d2 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 75  f memory cells u
1e9d3 73 65 64 20 62 79 20 74 68 65 0a 20 20 20 20 2a  sed by the.    *
1e9d4 2a 20 70 72 6f 67 72 61 6d 20 73 74 6f 72 65 64  * program stored
1e9d5 20 69 6e 20 53 75 62 50 72 6f 67 72 61 6d 2e 61   in SubProgram.a
1e9d6 4f 70 2e 20 41 73 20 77 65 6c 6c 20 61 73 20 74  Op. As well as t
1e9d7 68 65 73 65 2c 20 6f 6e 65 20 6d 65 6d 6f 72 79  hese, one memory
1e9d8 0a 20 20 20 20 2a 2a 20 63 65 6c 6c 20 69 73 20  .    ** cell is 
1e9d9 72 65 71 75 69 72 65 64 20 66 6f 72 20 65 61 63  required for eac
1e9da 68 20 63 75 72 73 6f 72 20 75 73 65 64 20 62 79  h cursor used by
1e9db 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 53 65   the program. Se
1e9dc 74 20 6c 6f 63 61 6c 0a 20 20 20 20 2a 2a 20 76  t local.    ** v
1e9dd 61 72 69 61 62 6c 65 20 75 2e 62 7a 2e 6e 4d 65  ariable u.bz.nMe
1e9de 6d 20 28 61 6e 64 20 6c 61 74 65 72 2c 20 56 64  m (and later, Vd
1e9df 62 65 46 72 61 6d 65 2e 6e 43 68 69 6c 64 4d 65  beFrame.nChildMe
1e9e0 6d 29 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65  m) to this value
1e9e1 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 75 2e 62  ..    */.    u.b
1e9e2 7a 2e 6e 4d 65 6d 20 3d 20 75 2e 62 7a 2e 70 50  z.nMem = u.bz.pP
1e9e3 72 6f 67 72 61 6d 2d 3e 6e 4d 65 6d 20 2b 20 75  rogram->nMem + u
1e9e4 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 43  .bz.pProgram->nC
1e9e5 73 72 3b 0a 20 20 20 20 75 2e 62 7a 2e 6e 42 79  sr;.    u.bz.nBy
1e9e6 74 65 20 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65  te = ROUND8(size
1e9e7 6f 66 28 56 64 62 65 46 72 61 6d 65 29 29 0a 20  of(VdbeFrame)). 
1e9e8 20 20 20 20 20 20 20 20 20 20 20 20 20 2b 20 75               + u
1e9e9 2e 62 7a 2e 6e 4d 65 6d 20 2a 20 73 69 7a 65 6f  .bz.nMem * sizeo
1e9ea 66 28 4d 65 6d 29 0a 20 20 20 20 20 20 20 20 20  f(Mem).         
1e9eb 20 20 20 20 20 2b 20 75 2e 62 7a 2e 70 50 72 6f       + u.bz.pPro
1e9ec 67 72 61 6d 2d 3e 6e 43 73 72 20 2a 20 73 69 7a  gram->nCsr * siz
1e9ed 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 20 2a  eof(VdbeCursor *
1e9ee 29 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72 61  );.    u.bz.pFra
1e9ef 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  me = sqlite3DbMa
1e9f0 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 75 2e 62  llocZero(db, u.b
1e9f1 7a 2e 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66  z.nByte);.    if
1e9f2 28 20 21 75 2e 62 7a 2e 70 46 72 61 6d 65 20 29  ( !u.bz.pFrame )
1e9f3 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f  {.      goto no_
1e9f4 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  mem;.    }.    s
1e9f5 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1e9f6 65 61 73 65 28 75 2e 62 7a 2e 70 52 74 29 3b 0a  ease(u.bz.pRt);.
1e9f7 20 20 20 20 75 2e 62 7a 2e 70 52 74 2d 3e 66 6c      u.bz.pRt->fl
1e9f8 61 67 73 20 3d 20 4d 45 4d 5f 46 72 61 6d 65 3b  ags = MEM_Frame;
1e9f9 0a 20 20 20 20 75 2e 62 7a 2e 70 52 74 2d 3e 75  .    u.bz.pRt->u
1e9fa 2e 70 46 72 61 6d 65 20 3d 20 75 2e 62 7a 2e 70  .pFrame = u.bz.p
1e9fb 46 72 61 6d 65 3b 0a 0a 20 20 20 20 75 2e 62 7a  Frame;..    u.bz
1e9fc 2e 70 46 72 61 6d 65 2d 3e 76 20 3d 20 70 3b 0a  .pFrame->v = p;.
1e9fd 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d      u.bz.pFrame-
1e9fe 3e 6e 43 68 69 6c 64 4d 65 6d 20 3d 20 75 2e 62  >nChildMem = u.b
1e9ff 7a 2e 6e 4d 65 6d 3b 0a 20 20 20 20 75 2e 62 7a  z.nMem;.    u.bz
1ea00 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 43  .pFrame->nChildC
1ea01 73 72 20 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72  sr = u.bz.pProgr
1ea02 61 6d 2d 3e 6e 43 73 72 3b 0a 20 20 20 20 75 2e  am->nCsr;.    u.
1ea03 62 7a 2e 70 46 72 61 6d 65 2d 3e 70 63 20 3d 20  bz.pFrame->pc = 
1ea04 70 63 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72  pc;.    u.bz.pFr
1ea05 61 6d 65 2d 3e 61 4d 65 6d 20 3d 20 70 2d 3e 61  ame->aMem = p->a
1ea06 4d 65 6d 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46  Mem;.    u.bz.pF
1ea07 72 61 6d 65 2d 3e 6e 4d 65 6d 20 3d 20 70 2d 3e  rame->nMem = p->
1ea08 6e 4d 65 6d 3b 0a 20 20 20 20 75 2e 62 7a 2e 70  nMem;.    u.bz.p
1ea09 46 72 61 6d 65 2d 3e 61 70 43 73 72 20 3d 20 70  Frame->apCsr = p
1ea0a 2d 3e 61 70 43 73 72 3b 0a 20 20 20 20 75 2e 62  ->apCsr;.    u.b
1ea0b 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 75 72 73 6f  z.pFrame->nCurso
1ea0c 72 20 3d 20 70 2d 3e 6e 43 75 72 73 6f 72 3b 0a  r = p->nCursor;.
1ea0d 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d      u.bz.pFrame-
1ea0e 3e 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20  >aOp = p->aOp;. 
1ea0f 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e     u.bz.pFrame->
1ea10 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20  nOp = p->nOp;.  
1ea11 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 74    u.bz.pFrame->t
1ea12 6f 6b 65 6e 20 3d 20 75 2e 62 7a 2e 70 50 72 6f  oken = u.bz.pPro
1ea13 67 72 61 6d 2d 3e 74 6f 6b 65 6e 3b 0a 0a 20 20  gram->token;..  
1ea14 20 20 75 2e 62 7a 2e 70 45 6e 64 20 3d 20 26 56    u.bz.pEnd = &V
1ea15 64 62 65 46 72 61 6d 65 4d 65 6d 28 75 2e 62 7a  dbeFrameMem(u.bz
1ea16 2e 70 46 72 61 6d 65 29 5b 75 2e 62 7a 2e 70 46  .pFrame)[u.bz.pF
1ea17 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d 5d  rame->nChildMem]
1ea18 3b 0a 20 20 20 20 66 6f 72 28 75 2e 62 7a 2e 70  ;.    for(u.bz.p
1ea19 4d 65 6d 3d 56 64 62 65 46 72 61 6d 65 4d 65 6d  Mem=VdbeFrameMem
1ea1a 28 75 2e 62 7a 2e 70 46 72 61 6d 65 29 3b 20 75  (u.bz.pFrame); u
1ea1b 2e 62 7a 2e 70 4d 65 6d 21 3d 75 2e 62 7a 2e 70  .bz.pMem!=u.bz.p
1ea1c 45 6e 64 3b 20 75 2e 62 7a 2e 70 4d 65 6d 2b 2b  End; u.bz.pMem++
1ea1d 29 7b 0a 20 20 20 20 20 20 75 2e 62 7a 2e 70 4d  ){.      u.bz.pM
1ea1e 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
1ea1f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 75 2e 62 7a  Null;.      u.bz
1ea20 2e 70 4d 65 6d 2d 3e 64 62 20 3d 20 64 62 3b 0a  .pMem->db = db;.
1ea21 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
1ea22 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 20 3d     u.bz.pFrame =
1ea23 20 75 2e 62 7a 2e 70 52 74 2d 3e 75 2e 70 46 72   u.bz.pRt->u.pFr
1ea24 61 6d 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ame;.    assert(
1ea25 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e   u.bz.pProgram->
1ea26 6e 4d 65 6d 2b 75 2e 62 7a 2e 70 50 72 6f 67 72  nMem+u.bz.pProgr
1ea27 61 6d 2d 3e 6e 43 73 72 3d 3d 75 2e 62 7a 2e 70  am->nCsr==u.bz.p
1ea28 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d  Frame->nChildMem
1ea29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
1ea2a 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e  u.bz.pProgram->n
1ea2b 43 73 72 3d 3d 75 2e 62 7a 2e 70 46 72 61 6d 65  Csr==u.bz.pFrame
1ea2c 2d 3e 6e 43 68 69 6c 64 43 73 72 20 29 3b 0a 20  ->nChildCsr );. 
1ea2d 20 20 20 61 73 73 65 72 74 28 20 70 63 3d 3d 75     assert( pc==u
1ea2e 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 70 63 20 29  .bz.pFrame->pc )
1ea2f 3b 0a 20 20 7d 0a 0a 20 20 70 2d 3e 6e 46 72 61  ;.  }..  p->nFra
1ea30 6d 65 2b 2b 3b 0a 20 20 75 2e 62 7a 2e 70 46 72  me++;.  u.bz.pFr
1ea31 61 6d 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70  ame->pParent = p
1ea32 2d 3e 70 46 72 61 6d 65 3b 0a 20 20 75 2e 62 7a  ->pFrame;.  u.bz
1ea33 2e 70 46 72 61 6d 65 2d 3e 6c 61 73 74 52 6f 77  .pFrame->lastRow
1ea34 69 64 20 3d 20 64 62 2d 3e 6c 61 73 74 52 6f 77  id = db->lastRow
1ea35 69 64 3b 0a 20 20 75 2e 62 7a 2e 70 46 72 61 6d  id;.  u.bz.pFram
1ea36 65 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70 2d 3e  e->nChange = p->
1ea37 6e 43 68 61 6e 67 65 3b 0a 20 20 70 2d 3e 6e 43  nChange;.  p->nC
1ea38 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70 2d 3e  hange = 0;.  p->
1ea39 70 46 72 61 6d 65 20 3d 20 75 2e 62 7a 2e 70 46  pFrame = u.bz.pF
1ea3a 72 61 6d 65 3b 0a 20 20 70 2d 3e 61 4d 65 6d 20  rame;.  p->aMem 
1ea3b 3d 20 26 56 64 62 65 46 72 61 6d 65 4d 65 6d 28  = &VdbeFrameMem(
1ea3c 75 2e 62 7a 2e 70 46 72 61 6d 65 29 5b 2d 31 5d  u.bz.pFrame)[-1]
1ea3d 3b 0a 20 20 70 2d 3e 6e 4d 65 6d 20 3d 20 75 2e  ;.  p->nMem = u.
1ea3e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c  bz.pFrame->nChil
1ea3f 64 4d 65 6d 3b 0a 20 20 70 2d 3e 6e 43 75 72 73  dMem;.  p->nCurs
1ea40 6f 72 20 3d 20 28 75 31 36 29 75 2e 62 7a 2e 70  or = (u16)u.bz.p
1ea41 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 43 73 72  Frame->nChildCsr
1ea42 3b 0a 20 20 70 2d 3e 61 70 43 73 72 20 3d 20 28  ;.  p->apCsr = (
1ea43 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 29 26 70  VdbeCursor **)&p
1ea44 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 2b 31  ->aMem[p->nMem+1
1ea45 5d 3b 0a 20 20 70 2d 3e 61 4f 70 20 3d 20 75 2e  ];.  p->aOp = u.
1ea46 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 61 4f 70  bz.pProgram->aOp
1ea47 3b 0a 20 20 70 2d 3e 6e 4f 70 20 3d 20 75 2e 62  ;.  p->nOp = u.b
1ea48 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 4f 70 3b  z.pProgram->nOp;
1ea49 0a 20 20 70 63 20 3d 20 2d 31 3b 0a 0a 20 20 62  .  pc = -1;..  b
1ea4a 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1ea4b 64 65 3a 20 50 61 72 61 6d 20 50 31 20 50 32 20  de: Param P1 P2 
1ea4c 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  * * *.**.** This
1ea4d 20 6f 70 63 6f 64 65 20 69 73 20 6f 6e 6c 79 20   opcode is only 
1ea4e 65 76 65 72 20 70 72 65 73 65 6e 74 20 69 6e 20  ever present in 
1ea4f 73 75 62 2d 70 72 6f 67 72 61 6d 73 20 63 61 6c  sub-programs cal
1ea50 6c 65 64 20 76 69 61 20 74 68 65 20 0a 2a 2a 20  led via the .** 
1ea51 4f 50 5f 50 72 6f 67 72 61 6d 20 69 6e 73 74 72  OP_Program instr
1ea52 75 63 74 69 6f 6e 2e 20 43 6f 70 79 20 61 20 76  uction. Copy a v
1ea53 61 6c 75 65 20 63 75 72 72 65 6e 74 6c 79 20 73  alue currently s
1ea54 74 6f 72 65 64 20 69 6e 20 61 20 6d 65 6d 6f 72  tored in a memor
1ea55 79 20 0a 2a 2a 20 63 65 6c 6c 20 6f 66 20 74 68  y .** cell of th
1ea56 65 20 63 61 6c 6c 69 6e 67 20 28 70 61 72 65 6e  e calling (paren
1ea57 74 29 20 66 72 61 6d 65 20 74 6f 20 63 65 6c 6c  t) frame to cell
1ea58 20 50 32 20 69 6e 20 74 68 65 20 63 75 72 72 65   P2 in the curre
1ea59 6e 74 20 66 72 61 6d 65 73 20 0a 2a 2a 20 61 64  nt frames .** ad
1ea5a 64 72 65 73 73 20 73 70 61 63 65 2e 20 54 68 69  dress space. Thi
1ea5b 73 20 69 73 20 75 73 65 64 20 62 79 20 74 72 69  s is used by tri
1ea5c 67 67 65 72 20 70 72 6f 67 72 61 6d 73 20 74 6f  gger programs to
1ea5d 20 61 63 63 65 73 73 20 74 68 65 20 6e 65 77 2e   access the new.
1ea5e 2a 20 0a 2a 2a 20 61 6e 64 20 6f 6c 64 2e 2a 20  * .** and old.* 
1ea5f 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  values..**.** Th
1ea60 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65  e address of the
1ea61 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61 72   cell in the par
1ea62 65 6e 74 20 66 72 61 6d 65 20 69 73 20 64 65 74  ent frame is det
1ea63 65 72 6d 69 6e 65 64 20 62 79 20 61 64 64 69 6e  ermined by addin
1ea64 67 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f  g.** the value o
1ea65 66 20 74 68 65 20 50 31 20 61 72 67 75 6d 65 6e  f the P1 argumen
1ea66 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f  t to the value o
1ea67 66 20 74 68 65 20 50 31 20 61 72 67 75 6d 65 6e  f the P1 argumen
1ea68 74 20 74 6f 20 74 68 65 0a 2a 2a 20 63 61 6c 6c  t to the.** call
1ea69 69 6e 67 20 4f 50 5f 50 72 6f 67 72 61 6d 20 69  ing OP_Program i
1ea6a 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63  nstruction..*/.c
1ea6b 61 73 65 20 4f 50 5f 50 61 72 61 6d 3a 20 7b 20  ase OP_Param: { 
1ea6c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74            /* out
1ea6d 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a  2-prerelease */.
1ea6e 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1ea6f 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1ea70 69 6e 74 6f 20 75 2e 63 61 20 2a 2f 0a 20 20 56  into u.ca */.  V
1ea71 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65  dbeFrame *pFrame
1ea72 3b 0a 20 20 4d 65 6d 20 2a 70 49 6e 3b 0a 23 65  ;.  Mem *pIn;.#e
1ea73 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1ea74 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1ea75 74 6f 20 75 2e 63 61 20 2a 2f 0a 20 20 75 2e 63  to u.ca */.  u.c
1ea76 61 2e 70 46 72 61 6d 65 20 3d 20 70 2d 3e 70 46  a.pFrame = p->pF
1ea77 72 61 6d 65 3b 0a 20 20 75 2e 63 61 2e 70 49 6e  rame;.  u.ca.pIn
1ea78 20 3d 20 26 75 2e 63 61 2e 70 46 72 61 6d 65 2d   = &u.ca.pFrame-
1ea79 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 20 2b 20  >aMem[pOp->p1 + 
1ea7a 75 2e 63 61 2e 70 46 72 61 6d 65 2d 3e 61 4f 70  u.ca.pFrame->aOp
1ea7b 5b 75 2e 63 61 2e 70 46 72 61 6d 65 2d 3e 70 63  [u.ca.pFrame->pc
1ea7c 5d 2e 70 31 5d 3b 0a 20 20 73 71 6c 69 74 65 33  ].p1];.  sqlite3
1ea7d 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f  VdbeMemShallowCo
1ea7e 70 79 28 70 4f 75 74 2c 20 75 2e 63 61 2e 70 49  py(pOut, u.ca.pI
1ea7f 6e 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20  n, MEM_Ephem);. 
1ea80 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 65 6e 64 69   break;.}..#endi
1ea81 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c  f /* #ifndef SQL
1ea82 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
1ea83 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
1ea84 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
1ea85 52 45 4d 45 4e 54 0a 2f 2a 20 4f 70 63 6f 64 65  REMENT./* Opcode
1ea86 3a 20 4d 65 6d 4d 61 78 20 50 31 20 50 32 20 2a  : MemMax P1 P2 *
1ea87 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73   * *.**.** P1 is
1ea88 20 61 20 72 65 67 69 73 74 65 72 20 69 6e 20 74   a register in t
1ea89 68 65 20 72 6f 6f 74 20 66 72 61 6d 65 20 6f 66  he root frame of
1ea8a 20 74 68 69 73 20 56 4d 20 28 74 68 65 20 72 6f   this VM (the ro
1ea8b 6f 74 20 66 72 61 6d 65 20 69 73 0a 2a 2a 20 64  ot frame is.** d
1ea8c 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68  ifferent from th
1ea8d 65 20 63 75 72 72 65 6e 74 20 66 72 61 6d 65 20  e current frame 
1ea8e 69 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74  if this instruct
1ea8f 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 65 78 65  ion is being exe
1ea90 63 75 74 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20  cuted.** within 
1ea91 61 20 73 75 62 2d 70 72 6f 67 72 61 6d 29 2e 20  a sub-program). 
1ea92 53 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66  Set the value of
1ea93 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20   register P1 to 
1ea94 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20 0a  the maximum of .
1ea95 2a 2a 20 69 74 73 20 63 75 72 72 65 6e 74 20 76  ** its current v
1ea96 61 6c 75 65 20 61 6e 64 20 74 68 65 20 76 61 6c  alue and the val
1ea97 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1ea98 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  2..**.** This in
1ea99 73 74 72 75 63 74 69 6f 6e 20 74 68 72 6f 77 73  struction throws
1ea9a 20 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65   an error if the
1ea9b 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 20   memory cell is 
1ea9c 6e 6f 74 20 69 6e 69 74 69 61 6c 6c 79 0a 2a 2a  not initially.**
1ea9d 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a   an integer..*/.
1ea9e 63 61 73 65 20 4f 50 5f 4d 65 6d 4d 61 78 3a 20  case OP_MemMax: 
1ea9f 7b 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20  {        /* in2 
1eaa0 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  */.#if 0  /* loc
1eaa1 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1eaa2 65 64 20 69 6e 74 6f 20 75 2e 63 62 20 2a 2f 0a  ed into u.cb */.
1eaa3 20 20 4d 65 6d 20 2a 70 49 6e 31 3b 0a 20 20 56    Mem *pIn1;.  V
1eaa4 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65  dbeFrame *pFrame
1eaa5 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1eaa6 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1eaa7 64 20 69 6e 74 6f 20 75 2e 63 62 20 2a 2f 0a 20  d into u.cb */. 
1eaa8 20 69 66 28 20 70 2d 3e 70 46 72 61 6d 65 20 29   if( p->pFrame )
1eaa9 7b 0a 20 20 20 20 66 6f 72 28 75 2e 63 62 2e 70  {.    for(u.cb.p
1eaaa 46 72 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65 3b  Frame=p->pFrame;
1eaab 20 75 2e 63 62 2e 70 46 72 61 6d 65 2d 3e 70 50   u.cb.pFrame->pP
1eaac 61 72 65 6e 74 3b 20 75 2e 63 62 2e 70 46 72 61  arent; u.cb.pFra
1eaad 6d 65 3d 75 2e 63 62 2e 70 46 72 61 6d 65 2d 3e  me=u.cb.pFrame->
1eaae 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 75 2e  pParent);.    u.
1eaaf 63 62 2e 70 49 6e 31 20 3d 20 26 75 2e 63 62 2e  cb.pIn1 = &u.cb.
1eab0 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70  pFrame->aMem[pOp
1eab1 2d 3e 70 31 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ->p1];.  }else{.
1eab2 20 20 20 20 75 2e 63 62 2e 70 49 6e 31 20 3d 20      u.cb.pIn1 = 
1eab3 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31  &p->aMem[pOp->p1
1eab4 5d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  ];.  }.  sqlite3
1eab5 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66  VdbeMemIntegerif
1eab6 79 28 75 2e 63 62 2e 70 49 6e 31 29 3b 0a 20 20  y(u.cb.pIn1);.  
1eab7 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e  sqlite3VdbeMemIn
1eab8 74 65 67 65 72 69 66 79 28 70 49 6e 32 29 3b 0a  tegerify(pIn2);.
1eab9 20 20 69 66 28 20 75 2e 63 62 2e 70 49 6e 31 2d    if( u.cb.pIn1-
1eaba 3e 75 2e 69 3c 70 49 6e 32 2d 3e 75 2e 69 29 7b  >u.i<pIn2->u.i){
1eabb 0a 20 20 20 20 75 2e 63 62 2e 70 49 6e 31 2d 3e  .    u.cb.pIn1->
1eabc 75 2e 69 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b  u.i = pIn2->u.i;
1eabd 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1eabe 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1eabf 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d  _OMIT_AUTOINCREM
1eac0 45 4e 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64  ENT */../* Opcod
1eac1 65 3a 20 49 66 50 6f 73 20 50 31 20 50 32 20 2a  e: IfPos P1 P2 *
1eac2 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68   * *.**.** If th
1eac3 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73  e value of regis
1eac4 74 65 72 20 50 31 20 69 73 20 31 20 6f 72 20 67  ter P1 is 1 or g
1eac5 72 65 61 74 65 72 2c 20 6a 75 6d 70 20 74 6f 20  reater, jump to 
1eac6 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20  P2..**.** It is 
1eac7 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 74  illegal to use t
1eac8 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1eac9 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 68  on a register th
1eaca 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63  at does.** not c
1eacb 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65  ontain an intege
1eacc 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e  r.  An assertion
1eacd 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 75   fault will resu
1eace 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a  lt if you try..*
1eacf 2f 0a 63 61 73 65 20 4f 50 5f 49 66 50 6f 73 3a  /.case OP_IfPos:
1ead0 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d   {        /* jum
1ead1 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65  p, in1 */.  asse
1ead2 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26  rt( pIn1->flags&
1ead3 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 28  MEM_Int );.  if(
1ead4 20 70 49 6e 31 2d 3e 75 2e 69 3e 30 20 29 7b 0a   pIn1->u.i>0 ){.
1ead5 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70       pc = pOp->p
1ead6 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65  2 - 1;.  }.  bre
1ead7 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1ead8 3a 20 49 66 4e 65 67 20 50 31 20 50 32 20 2a 20  : IfNeg P1 P2 * 
1ead9 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  * *.**.** If the
1eada 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74   value of regist
1eadb 65 72 20 50 31 20 69 73 20 6c 65 73 73 20 74 68  er P1 is less th
1eadc 61 6e 20 7a 65 72 6f 2c 20 6a 75 6d 70 20 74 6f  an zero, jump to
1eadd 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69   P2. .**.** It i
1eade 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65  s illegal to use
1eadf 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   this instructio
1eae0 6e 20 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20  n on a register 
1eae1 74 68 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74  that does.** not
1eae2 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65   contain an inte
1eae3 67 65 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69  ger.  An asserti
1eae4 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65  on fault will re
1eae5 73 75 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e  sult if you try.
1eae6 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 4e 65  .*/.case OP_IfNe
1eae7 67 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a  g: {        /* j
1eae8 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73  ump, in1 */.  as
1eae9 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67  sert( pIn1->flag
1eaea 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69  s&MEM_Int );.  i
1eaeb 66 28 20 70 49 6e 31 2d 3e 75 2e 69 3c 30 20 29  f( pIn1->u.i<0 )
1eaec 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  {.     pc = pOp-
1eaed 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62  >p2 - 1;.  }.  b
1eaee 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1eaef 64 65 3a 20 49 66 5a 65 72 6f 20 50 31 20 50 32  de: IfZero P1 P2
1eaf0 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20   * * *.**.** If 
1eaf1 74 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67  the value of reg
1eaf2 69 73 74 65 72 20 50 31 20 69 73 20 65 78 61 63  ister P1 is exac
1eaf3 74 6c 79 20 30 2c 20 6a 75 6d 70 20 74 6f 20 50  tly 0, jump to P
1eaf4 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20  2. .**.** It is 
1eaf5 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 74  illegal to use t
1eaf6 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1eaf7 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 68  on a register th
1eaf8 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63  at does.** not c
1eaf9 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65  ontain an intege
1eafa 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e  r.  An assertion
1eafb 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 75   fault will resu
1eafc 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a  lt if you try..*
1eafd 2f 0a 63 61 73 65 20 4f 50 5f 49 66 5a 65 72 6f  /.case OP_IfZero
1eafe 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75  : {        /* ju
1eaff 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73  mp, in1 */.  ass
1eb00 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73  ert( pIn1->flags
1eb01 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66  &MEM_Int );.  if
1eb02 28 20 70 49 6e 31 2d 3e 75 2e 69 3d 3d 30 20 29  ( pIn1->u.i==0 )
1eb03 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  {.     pc = pOp-
1eb04 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62  >p2 - 1;.  }.  b
1eb05 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1eb06 64 65 3a 20 41 67 67 53 74 65 70 20 2a 20 50 32  de: AggStep * P2
1eb07 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20   P3 P4 P5.**.** 
1eb08 45 78 65 63 75 74 65 20 74 68 65 20 73 74 65 70  Execute the step
1eb09 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e   function for an
1eb0a 20 61 67 67 72 65 67 61 74 65 2e 20 20 54 68 65   aggregate.  The
1eb0b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61 73  .** function has
1eb0c 20 50 35 20 61 72 67 75 6d 65 6e 74 73 2e 20 20   P5 arguments.  
1eb0d 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72   P4 is a pointer
1eb0e 20 74 6f 20 74 68 65 20 46 75 6e 63 44 65 66 0a   to the FuncDef.
1eb0f 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 68 61  ** structure tha
1eb10 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20  t specifies the 
1eb11 66 75 6e 63 74 69 6f 6e 2e 20 20 55 73 65 20 72  function.  Use r
1eb12 65 67 69 73 74 65 72 0a 2a 2a 20 50 33 20 61 73  egister.** P3 as
1eb13 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72   the accumulator
1eb14 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 35 20 61  ..**.** The P5 a
1eb15 72 67 75 6d 65 6e 74 73 20 61 72 65 20 74 61 6b  rguments are tak
1eb16 65 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72  en from register
1eb17 20 50 32 20 61 6e 64 20 69 74 73 0a 2a 2a 20 73   P2 and its.** s
1eb18 75 63 63 65 73 73 6f 72 73 2e 0a 2a 2f 0a 63 61  uccessors..*/.ca
1eb19 73 65 20 4f 50 5f 41 67 67 53 74 65 70 3a 20 7b  se OP_AggStep: {
1eb1a 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1eb1b 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1eb1c 20 69 6e 74 6f 20 75 2e 63 63 20 2a 2f 0a 20 20   into u.cc */.  
1eb1d 69 6e 74 20 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a  int n;.  int i;.
1eb1e 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 4d    Mem *pMem;.  M
1eb1f 65 6d 20 2a 70 52 65 63 3b 0a 20 20 73 71 6c 69  em *pRec;.  sqli
1eb20 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b  te3_context ctx;
1eb21 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
1eb22 20 2a 2a 61 70 56 61 6c 3b 0a 23 65 6e 64 69 66   **apVal;.#endif
1eb23 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1eb24 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1eb25 2e 63 63 20 2a 2f 0a 0a 20 20 75 2e 63 63 2e 6e  .cc */..  u.cc.n
1eb26 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 20 20 61 73   = pOp->p5;.  as
1eb27 73 65 72 74 28 20 75 2e 63 63 2e 6e 3e 3d 30 20  sert( u.cc.n>=0 
1eb28 29 3b 0a 20 20 75 2e 63 63 2e 70 52 65 63 20 3d  );.  u.cc.pRec =
1eb29 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1eb2a 32 5d 3b 0a 20 20 75 2e 63 63 2e 61 70 56 61 6c  2];.  u.cc.apVal
1eb2b 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 61   = p->apArg;.  a
1eb2c 73 73 65 72 74 28 20 75 2e 63 63 2e 61 70 56 61  ssert( u.cc.apVa
1eb2d 6c 20 7c 7c 20 75 2e 63 63 2e 6e 3d 3d 30 20 29  l || u.cc.n==0 )
1eb2e 3b 0a 20 20 66 6f 72 28 75 2e 63 63 2e 69 3d 30  ;.  for(u.cc.i=0
1eb2f 3b 20 75 2e 63 63 2e 69 3c 75 2e 63 63 2e 6e 3b  ; u.cc.i<u.cc.n;
1eb30 20 75 2e 63 63 2e 69 2b 2b 2c 20 75 2e 63 63 2e   u.cc.i++, u.cc.
1eb31 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 75 2e 63  pRec++){.    u.c
1eb32 63 2e 61 70 56 61 6c 5b 75 2e 63 63 2e 69 5d 20  c.apVal[u.cc.i] 
1eb33 3d 20 75 2e 63 63 2e 70 52 65 63 3b 0a 20 20 20  = u.cc.pRec;.   
1eb34 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 75   storeTypeInfo(u
1eb35 2e 63 63 2e 70 52 65 63 2c 20 65 6e 63 6f 64 69  .cc.pRec, encodi
1eb36 6e 67 29 3b 0a 20 20 7d 0a 20 20 75 2e 63 63 2e  ng);.  }.  u.cc.
1eb37 63 74 78 2e 70 46 75 6e 63 20 3d 20 70 4f 70 2d  ctx.pFunc = pOp-
1eb38 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20 61 73 73  >p4.pFunc;.  ass
1eb39 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26  ert( pOp->p3>0 &
1eb3a 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d  & pOp->p3<=p->nM
1eb3b 65 6d 20 29 3b 0a 20 20 75 2e 63 63 2e 63 74 78  em );.  u.cc.ctx
1eb3c 2e 70 4d 65 6d 20 3d 20 75 2e 63 63 2e 70 4d 65  .pMem = u.cc.pMe
1eb3d 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  m = &p->aMem[pOp
1eb3e 2d 3e 70 33 5d 3b 0a 20 20 75 2e 63 63 2e 70 4d  ->p3];.  u.cc.pM
1eb3f 65 6d 2d 3e 6e 2b 2b 3b 0a 20 20 75 2e 63 63 2e  em->n++;.  u.cc.
1eb40 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45  ctx.s.flags = ME
1eb41 4d 5f 4e 75 6c 6c 3b 0a 20 20 75 2e 63 63 2e 63  M_Null;.  u.cc.c
1eb42 74 78 2e 73 2e 7a 20 3d 20 30 3b 0a 20 20 75 2e  tx.s.z = 0;.  u.
1eb43 63 63 2e 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f 63  cc.ctx.s.zMalloc
1eb44 20 3d 20 30 3b 0a 20 20 75 2e 63 63 2e 63 74 78   = 0;.  u.cc.ctx
1eb45 2e 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 75  .s.xDel = 0;.  u
1eb46 2e 63 63 2e 63 74 78 2e 73 2e 64 62 20 3d 20 64  .cc.ctx.s.db = d
1eb47 62 3b 0a 20 20 75 2e 63 63 2e 63 74 78 2e 69 73  b;.  u.cc.ctx.is
1eb48 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 75 2e 63  Error = 0;.  u.c
1eb49 63 2e 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 30 3b  c.ctx.pColl = 0;
1eb4a 0a 20 20 69 66 28 20 75 2e 63 63 2e 63 74 78 2e  .  if( u.cc.ctx.
1eb4b 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26 20 53  pFunc->flags & S
1eb4c 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43  QLITE_FUNC_NEEDC
1eb4d 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 73 73 65 72  OLL ){.    asser
1eb4e 74 28 20 70 4f 70 3e 70 2d 3e 61 4f 70 20 29 3b  t( pOp>p->aOp );
1eb4f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70  .    assert( pOp
1eb50 5b 2d 31 5d 2e 70 34 74 79 70 65 3d 3d 50 34 5f  [-1].p4type==P4_
1eb51 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 20 20 20 61  COLLSEQ );.    a
1eb52 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f  ssert( pOp[-1].o
1eb53 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65  pcode==OP_CollSe
1eb54 71 20 29 3b 0a 20 20 20 20 75 2e 63 63 2e 63 74  q );.    u.cc.ct
1eb55 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31  x.pColl = pOp[-1
1eb56 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a  ].p4.pColl;.  }.
1eb57 20 20 28 75 2e 63 63 2e 63 74 78 2e 70 46 75 6e    (u.cc.ctx.pFun
1eb58 63 2d 3e 78 53 74 65 70 29 28 26 75 2e 63 63 2e  c->xStep)(&u.cc.
1eb59 63 74 78 2c 20 75 2e 63 63 2e 6e 2c 20 75 2e 63  ctx, u.cc.n, u.c
1eb5a 63 2e 61 70 56 61 6c 29 3b 0a 20 20 69 66 28 20  c.apVal);.  if( 
1eb5b 75 2e 63 63 2e 63 74 78 2e 69 73 45 72 72 6f 72  u.cc.ctx.isError
1eb5c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
1eb5d 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72  etString(&p->zEr
1eb5e 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20  rMsg, db, "%s", 
1eb5f 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1eb60 78 74 28 26 75 2e 63 63 2e 63 74 78 2e 73 29 29  xt(&u.cc.ctx.s))
1eb61 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 63 2e  ;.    rc = u.cc.
1eb62 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20 7d  ctx.isError;.  }
1eb63 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1eb64 6d 52 65 6c 65 61 73 65 28 26 75 2e 63 63 2e 63  mRelease(&u.cc.c
1eb65 74 78 2e 73 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  tx.s);.  break;.
1eb66 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67  }../* Opcode: Ag
1eb67 67 46 69 6e 61 6c 20 50 31 20 50 32 20 2a 20 50  gFinal P1 P2 * P
1eb68 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74  4 *.**.** Execut
1eb69 65 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20  e the finalizer 
1eb6a 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20  function for an 
1eb6b 61 67 67 72 65 67 61 74 65 2e 20 20 50 31 20 69  aggregate.  P1 i
1eb6c 73 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20  s.** the memory 
1eb6d 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73  location that is
1eb6e 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72   the accumulator
1eb6f 20 66 6f 72 20 74 68 65 20 61 67 67 72 65 67 61   for the aggrega
1eb70 74 65 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20  te..**.** P2 is 
1eb71 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72  the number of ar
1eb72 67 75 6d 65 6e 74 73 20 74 68 61 74 20 74 68 65  guments that the
1eb73 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 74   step function t
1eb74 61 6b 65 73 20 61 6e 64 0a 2a 2a 20 50 34 20 69  akes and.** P4 i
1eb75 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
1eb76 68 65 20 46 75 6e 63 44 65 66 20 66 6f 72 20 74  he FuncDef for t
1eb77 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54  his function.  T
1eb78 68 65 20 50 32 0a 2a 2a 20 61 72 67 75 6d 65 6e  he P2.** argumen
1eb79 74 20 69 73 20 6e 6f 74 20 75 73 65 64 20 62 79  t is not used by
1eb7a 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 49   this opcode.  I
1eb7b 74 20 69 73 20 6f 6e 6c 79 20 74 68 65 72 65 20  t is only there 
1eb7c 74 6f 20 64 69 73 61 6d 62 69 67 75 61 74 65 0a  to disambiguate.
1eb7d 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61  ** functions tha
1eb7e 74 20 63 61 6e 20 74 61 6b 65 20 76 61 72 79 69  t can take varyi
1eb7f 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 72  ng numbers of ar
1eb80 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 0a 2a 2a  guments.  The.**
1eb81 20 50 34 20 61 72 67 75 6d 65 6e 74 20 69 73 20   P4 argument is 
1eb82 6f 6e 6c 79 20 6e 65 65 64 65 64 20 66 6f 72 20  only needed for 
1eb83 74 68 65 20 64 65 67 65 6e 65 72 61 74 65 20 63  the degenerate c
1eb84 61 73 65 20 77 68 65 72 65 0a 2a 2a 20 74 68 65  ase where.** the
1eb85 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 77   step function w
1eb86 61 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c  as not previousl
1eb87 79 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 63 61 73  y called..*/.cas
1eb88 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c 3a 20 7b  e OP_AggFinal: {
1eb89 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1eb8a 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1eb8b 20 69 6e 74 6f 20 75 2e 63 64 20 2a 2f 0a 20 20   into u.cd */.  
1eb8c 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 23 65 6e 64 69  Mem *pMem;.#endi
1eb8d 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1eb8e 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1eb8f 75 2e 63 64 20 2a 2f 0a 20 20 61 73 73 65 72 74  u.cd */.  assert
1eb90 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70  ( pOp->p1>0 && p
1eb91 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p1<=p->nMem 
1eb92 29 3b 0a 20 20 75 2e 63 64 2e 70 4d 65 6d 20 3d  );.  u.cd.pMem =
1eb93 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1eb94 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 75  1];.  assert( (u
1eb95 2e 63 64 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  .cd.pMem->flags 
1eb96 26 20 7e 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d  & ~(MEM_Null|MEM
1eb97 5f 41 67 67 29 29 3d 3d 30 20 29 3b 0a 20 20 72  _Agg))==0 );.  r
1eb98 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
1eb99 65 6d 46 69 6e 61 6c 69 7a 65 28 75 2e 63 64 2e  emFinalize(u.cd.
1eb9a 70 4d 65 6d 2c 20 70 4f 70 2d 3e 70 34 2e 70 46  pMem, pOp->p4.pF
1eb9b 75 6e 63 29 3b 0a 20 20 69 66 28 20 72 63 20 29  unc);.  if( rc )
1eb9c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74  {.    sqlite3Set
1eb9d 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d  String(&p->zErrM
1eb9e 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71  sg, db, "%s", sq
1eb9f 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1eba0 28 75 2e 63 64 2e 70 4d 65 6d 29 29 3b 0a 20 20  (u.cd.pMem));.  
1eba1 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43  }.  sqlite3VdbeC
1eba2 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 75 2e  hangeEncoding(u.
1eba3 63 64 2e 70 4d 65 6d 2c 20 65 6e 63 6f 64 69 6e  cd.pMem, encodin
1eba4 67 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58  g);.  UPDATE_MAX
1eba5 5f 42 4c 4f 42 53 49 5a 45 28 75 2e 63 64 2e 70  _BLOBSIZE(u.cd.p
1eba6 4d 65 6d 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  Mem);.  if( sqli
1eba7 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67  te3VdbeMemTooBig
1eba8 28 75 2e 63 64 2e 70 4d 65 6d 29 20 29 7b 0a 20  (u.cd.pMem) ){. 
1eba9 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b     goto too_big;
1ebaa 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1ebab 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
1ebac 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55  QLITE_OMIT_VACUU
1ebad 4d 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  M) && !defined(S
1ebae 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43  QLITE_OMIT_ATTAC
1ebaf 48 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61  H)./* Opcode: Va
1ebb0 63 75 75 6d 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a  cuum * * * * *.*
1ebb1 2a 0a 2a 2a 20 56 61 63 75 75 6d 20 74 68 65 20  *.** Vacuum the 
1ebb2 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 2e  entire database.
1ebb3 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 69    This opcode wi
1ebb4 6c 6c 20 63 61 75 73 65 20 6f 74 68 65 72 20 76  ll cause other v
1ebb5 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e  irtual.** machin
1ebb6 65 73 20 74 6f 20 62 65 20 63 72 65 61 74 65 64  es to be created
1ebb7 20 61 6e 64 20 72 75 6e 2e 20 20 49 74 20 6d 61   and run.  It ma
1ebb8 79 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20  y not be called 
1ebb9 66 72 6f 6d 20 77 69 74 68 69 6e 0a 2a 2a 20 61  from within.** a
1ebba 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f   transaction..*/
1ebbb 0a 63 61 73 65 20 4f 50 5f 56 61 63 75 75 6d 3a  .case OP_Vacuum:
1ebbc 20 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   {.  if( sqlite3
1ebbd 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20  SafetyOff(db) ) 
1ebbe 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1ebbf 6f 5f 6d 69 73 75 73 65 3b 20 0a 20 20 72 63 20  o_misuse; .  rc 
1ebc0 3d 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75  = sqlite3RunVacu
1ebc1 75 6d 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  um(&p->zErrMsg, 
1ebc2 64 62 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  db);.  if( sqlit
1ebc3 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29  e3SafetyOn(db) )
1ebc4 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1ebc5 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 62 72 65  to_misuse;.  bre
1ebc6 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  ak;.}.#endif..#i
1ebc7 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
1ebc8 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
1ebc9 4d 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e  M)./* Opcode: In
1ebca 63 72 56 61 63 75 75 6d 20 50 31 20 50 32 20 2a  crVacuum P1 P2 *
1ebcb 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 65 72 66 6f   * *.**.** Perfo
1ebcc 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70  rm a single step
1ebcd 20 6f 66 20 74 68 65 20 69 6e 63 72 65 6d 65 6e   of the incremen
1ebce 74 61 6c 20 76 61 63 75 75 6d 20 70 72 6f 63 65  tal vacuum proce
1ebcf 64 75 72 65 20 6f 6e 0a 2a 2a 20 74 68 65 20 50  dure on.** the P
1ebd0 31 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 74  1 database. If t
1ebd1 68 65 20 76 61 63 75 75 6d 20 68 61 73 20 66 69  he vacuum has fi
1ebd2 6e 69 73 68 65 64 2c 20 6a 75 6d 70 20 74 6f 20  nished, jump to 
1ebd3 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 50  instruction.** P
1ebd4 32 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61  2. Otherwise, fa
1ebd5 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68  ll through to th
1ebd6 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69  e next instructi
1ebd7 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49  on..*/.case OP_I
1ebd8 6e 63 72 56 61 63 75 75 6d 3a 20 7b 20 20 20 20  ncrVacuum: {    
1ebd9 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23      /* jump */.#
1ebda 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1ebdb 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1ebdc 6e 74 6f 20 75 2e 63 65 20 2a 2f 0a 20 20 42 74  nto u.ce */.  Bt
1ebdd 72 65 65 20 2a 70 42 74 3b 0a 23 65 6e 64 69 66  ree *pBt;.#endif
1ebde 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1ebdf 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1ebe0 2e 63 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  .ce */..  assert
1ebe1 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
1ebe2 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20  pOp->p1<db->nDb 
1ebe3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d  );.  assert( (p-
1ebe4 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c  >btreeMask & (1<
1ebe5 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b  <pOp->p1))!=0 );
1ebe6 0a 20 20 75 2e 63 65 2e 70 42 74 20 3d 20 64 62  .  u.ce.pBt = db
1ebe7 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70  ->aDb[pOp->p1].p
1ebe8 42 74 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  Bt;.  rc = sqlit
1ebe9 65 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75  e3BtreeIncrVacuu
1ebea 6d 28 75 2e 63 65 2e 70 42 74 29 3b 0a 20 20 69  m(u.ce.pBt);.  i
1ebeb 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  f( rc==SQLITE_DO
1ebec 4e 45 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70  NE ){.    pc = p
1ebed 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20  Op->p2 - 1;.    
1ebee 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1ebef 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23    }.  break;.}.#
1ebf0 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 63 6f 64 65  endif../* Opcode
1ebf1 3a 20 45 78 70 69 72 65 20 50 31 20 2a 20 2a 20  : Expire P1 * * 
1ebf2 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 61 75 73 65 20  * *.**.** Cause 
1ebf3 70 72 65 63 6f 6d 70 69 6c 65 64 20 73 74 61 74  precompiled stat
1ebf4 65 6d 65 6e 74 73 20 74 6f 20 62 65 63 6f 6d 65  ements to become
1ebf5 20 65 78 70 69 72 65 64 2e 20 41 6e 20 65 78 70   expired. An exp
1ebf6 69 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a  ired statement.*
1ebf7 2a 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 20  * fails with an 
1ebf8 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 53 51  error code of SQ
1ebf9 4c 49 54 45 5f 53 43 48 45 4d 41 20 69 66 20 69  LITE_SCHEMA if i
1ebfa 74 20 69 73 20 65 76 65 72 20 65 78 65 63 75 74  t is ever execut
1ebfb 65 64 20 0a 2a 2a 20 28 76 69 61 20 73 71 6c 69  ed .** (via sqli
1ebfc 74 65 33 5f 73 74 65 70 28 29 29 2e 0a 2a 2a 20  te3_step())..** 
1ebfd 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 30 2c 20  .** If P1 is 0, 
1ebfe 74 68 65 6e 20 61 6c 6c 20 53 51 4c 20 73 74 61  then all SQL sta
1ebff 74 65 6d 65 6e 74 73 20 62 65 63 6f 6d 65 20 65  tements become e
1ec00 78 70 69 72 65 64 2e 20 49 66 20 50 31 20 69 73  xpired. If P1 is
1ec01 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68   non-zero,.** th
1ec02 65 6e 20 6f 6e 6c 79 20 74 68 65 20 63 75 72 72  en only the curr
1ec03 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20  ently executing 
1ec04 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 66 66  statement is aff
1ec05 65 63 74 65 64 2e 20 0a 2a 2f 0a 63 61 73 65 20  ected. .*/.case 
1ec06 4f 50 5f 45 78 70 69 72 65 3a 20 7b 0a 20 20 69  OP_Expire: {.  i
1ec07 66 28 20 21 70 4f 70 2d 3e 70 31 20 29 7b 0a 20  f( !pOp->p1 ){. 
1ec08 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65     sqlite3Expire
1ec09 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
1ec0a 74 73 28 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b  ts(db);.  }else{
1ec0b 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20  .    p->expired 
1ec0c 3d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  = 1;.  }.  break
1ec0d 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
1ec0e 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
1ec0f 43 41 43 48 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a  CACHE./* Opcode:
1ec10 20 54 61 62 6c 65 4c 6f 63 6b 20 50 31 20 50 32   TableLock P1 P2
1ec11 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f   P3 P4 *.**.** O
1ec12 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20  btain a lock on 
1ec13 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 61 62  a particular tab
1ec14 6c 65 2e 20 54 68 69 73 20 69 6e 73 74 72 75 63  le. This instruc
1ec15 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65  tion is only use
1ec16 64 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 73 68  d when.** the sh
1ec17 61 72 65 64 2d 63 61 63 68 65 20 66 65 61 74 75  ared-cache featu
1ec18 72 65 20 69 73 20 65 6e 61 62 6c 65 64 2e 20 0a  re is enabled. .
1ec19 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20  **.** P1 is the 
1ec1a 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74  index of the dat
1ec1b 61 62 61 73 65 20 69 6e 20 73 71 6c 69 74 65 33  abase in sqlite3
1ec1c 2e 61 44 62 5b 5d 20 6f 66 20 74 68 65 20 64 61  .aDb[] of the da
1ec1d 74 61 62 61 73 65 0a 2a 2a 20 6f 6e 20 77 68 69  tabase.** on whi
1ec1e 63 68 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61  ch the lock is a
1ec1f 63 71 75 69 72 65 64 2e 20 20 41 20 72 65 61 64  cquired.  A read
1ec20 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64  lock is obtained
1ec21 20 69 66 20 50 33 3d 3d 30 20 6f 72 0a 2a 2a 20   if P3==0 or.** 
1ec22 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20  a write lock if 
1ec23 50 33 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 50 32 20  P3==1..**.** P2 
1ec24 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f 6f  contains the roo
1ec25 74 2d 70 61 67 65 20 6f 66 20 74 68 65 20 74 61  t-page of the ta
1ec26 62 6c 65 20 74 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a  ble to lock..**.
1ec27 2a 2a 20 50 34 20 63 6f 6e 74 61 69 6e 73 20 61  ** P4 contains a
1ec28 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
1ec29 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
1ec2a 65 20 62 65 69 6e 67 20 6c 6f 63 6b 65 64 2e 20  e being locked. 
1ec2b 54 68 69 73 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20  This is only.** 
1ec2c 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65  used to generate
1ec2d 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
1ec2e 65 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 63 61  e if the lock ca
1ec2f 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64  nnot be obtained
1ec30 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 61 62  ..*/.case OP_Tab
1ec31 6c 65 4c 6f 63 6b 3a 20 7b 0a 20 20 75 38 20 69  leLock: {.  u8 i
1ec32 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 28 75 38  sWriteLock = (u8
1ec33 29 70 4f 70 2d 3e 70 33 3b 0a 20 20 69 66 28 20  )pOp->p3;.  if( 
1ec34 69 73 57 72 69 74 65 4c 6f 63 6b 20 7c 7c 20 30  isWriteLock || 0
1ec35 3d 3d 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  ==(db->flags&SQL
1ec36 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74  ITE_ReadUncommit
1ec37 74 65 64 29 20 29 7b 0a 20 20 20 20 69 6e 74 20  ted) ){.    int 
1ec38 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 0a 20  p1 = pOp->p1; . 
1ec39 20 20 20 61 73 73 65 72 74 28 20 70 31 3e 3d 30     assert( p1>=0
1ec3a 20 26 26 20 70 31 3c 64 62 2d 3e 6e 44 62 20 29   && p1<db->nDb )
1ec3b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  ;.    assert( (p
1ec3c 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31  ->btreeMask & (1
1ec3d 3c 3c 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 20  <<p1))!=0 );.   
1ec3e 20 61 73 73 65 72 74 28 20 69 73 57 72 69 74 65   assert( isWrite
1ec3f 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 69 73 57 72 69  Lock==0 || isWri
1ec40 74 65 4c 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 20  teLock==1 );.   
1ec41 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1ec42 65 65 4c 6f 63 6b 54 61 62 6c 65 28 64 62 2d 3e  eeLockTable(db->
1ec43 61 44 62 5b 70 31 5d 2e 70 42 74 2c 20 70 4f 70  aDb[p1].pBt, pOp
1ec44 2d 3e 70 32 2c 20 69 73 57 72 69 74 65 4c 6f 63  ->p2, isWriteLoc
1ec45 6b 29 3b 0a 20 20 20 20 69 66 28 20 28 72 63 26  k);.    if( (rc&
1ec46 30 78 46 46 29 3d 3d 53 51 4c 49 54 45 5f 4c 4f  0xFF)==SQLITE_LO
1ec47 43 4b 45 44 20 29 7b 0a 20 20 20 20 20 20 63 6f  CKED ){.      co
1ec48 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f  nst char *z = pO
1ec49 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 73  p->p4.z;.      s
1ec4a 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
1ec4b 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c  &p->zErrMsg, db,
1ec4c 20 22 64 61 74 61 62 61 73 65 20 74 61 62 6c 65   "database table
1ec4d 20 69 73 20 6c 6f 63 6b 65 64 3a 20 25 73 22 2c   is locked: %s",
1ec4e 20 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20   z);.    }.  }. 
1ec4f 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1ec50 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1ec51 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a  SHARED_CACHE */.
1ec52 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1ec53 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1ec54 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 42 65  E./* Opcode: VBe
1ec55 67 69 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a  gin * * * P4 *.*
1ec56 2a 0a 2a 2a 20 50 34 20 6d 61 79 20 62 65 20 61  *.** P4 may be a
1ec57 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73   pointer to an s
1ec58 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75  qlite3_vtab stru
1ec59 63 74 75 72 65 2e 20 49 66 20 73 6f 2c 20 63 61  cture. If so, ca
1ec5a 6c 6c 20 74 68 65 20 0a 2a 2a 20 78 42 65 67 69  ll the .** xBegi
1ec5b 6e 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 61  n method for tha
1ec5c 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41  t table..**.** A
1ec5d 6c 73 6f 2c 20 77 68 65 74 68 65 72 20 6f 72 20  lso, whether or 
1ec5e 6e 6f 74 20 50 34 20 69 73 20 73 65 74 2c 20 63  not P4 is set, c
1ec5f 68 65 63 6b 20 74 68 61 74 20 74 68 69 73 20 69  heck that this i
1ec60 73 20 6e 6f 74 20 62 65 69 6e 67 20 63 61 6c 6c  s not being call
1ec61 65 64 20 66 72 6f 6d 0a 2a 2a 20 77 69 74 68 69  ed from.** withi
1ec62 6e 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20  n a callback to 
1ec63 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
1ec64 78 53 79 6e 63 28 29 20 6d 65 74 68 6f 64 2e 20  xSync() method. 
1ec65 49 66 20 69 74 20 69 73 2c 20 74 68 65 20 65 72  If it is, the er
1ec66 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 77 69 6c 6c  ror.** code will
1ec67 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54   be set to SQLIT
1ec68 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 63 61 73  E_LOCKED..*/.cas
1ec69 65 20 4f 50 5f 56 42 65 67 69 6e 3a 20 7b 0a 23  e OP_VBegin: {.#
1ec6a 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1ec6b 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1ec6c 6e 74 6f 20 75 2e 63 66 20 2a 2f 0a 20 20 56 54  nto u.cf */.  VT
1ec6d 61 62 6c 65 20 2a 70 56 54 61 62 3b 0a 23 65 6e  able *pVTab;.#en
1ec6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1ec6f 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1ec70 6f 20 75 2e 63 66 20 2a 2f 0a 20 20 75 2e 63 66  o u.cf */.  u.cf
1ec71 2e 70 56 54 61 62 20 3d 20 70 4f 70 2d 3e 70 34  .pVTab = pOp->p4
1ec72 2e 70 56 74 61 62 3b 0a 20 20 72 63 20 3d 20 73  .pVtab;.  rc = s
1ec73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 28  qlite3VtabBegin(
1ec74 64 62 2c 20 75 2e 63 66 2e 70 56 54 61 62 29 3b  db, u.cf.pVTab);
1ec75 0a 20 20 69 66 28 20 75 2e 63 66 2e 70 56 54 61  .  if( u.cf.pVTa
1ec76 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  b ){.    sqlite3
1ec77 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45  DbFree(db, p->zE
1ec78 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a  rrMsg);.    p->z
1ec79 45 72 72 4d 73 67 20 3d 20 75 2e 63 66 2e 70 56  ErrMsg = u.cf.pV
1ec7a 54 61 62 2d 3e 70 56 74 61 62 2d 3e 7a 45 72 72  Tab->pVtab->zErr
1ec7b 4d 73 67 3b 0a 20 20 20 20 75 2e 63 66 2e 70 56  Msg;.    u.cf.pV
1ec7c 54 61 62 2d 3e 70 56 74 61 62 2d 3e 7a 45 72 72  Tab->pVtab->zErr
1ec7d 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62  Msg = 0;.  }.  b
1ec7e 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  reak;.}.#endif /
1ec7f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  * SQLITE_OMIT_VI
1ec80 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23  RTUALTABLE */..#
1ec81 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1ec82 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
1ec83 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 43 72 65 61  /* Opcode: VCrea
1ec84 74 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a  te P1 * * P4 *.*
1ec85 2a 0a 2a 2a 20 50 34 20 69 73 20 74 68 65 20 6e  *.** P4 is the n
1ec86 61 6d 65 20 6f 66 20 61 20 76 69 72 74 75 61 6c  ame of a virtual
1ec87 20 74 61 62 6c 65 20 69 6e 20 64 61 74 61 62 61   table in databa
1ec88 73 65 20 50 31 2e 20 43 61 6c 6c 20 74 68 65 20  se P1. Call the 
1ec89 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a  xCreate method.*
1ec8a 2a 20 66 6f 72 20 74 68 61 74 20 74 61 62 6c 65  * for that table
1ec8b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 43 72  ..*/.case OP_VCr
1ec8c 65 61 74 65 3a 20 7b 0a 20 20 72 63 20 3d 20 73  eate: {.  rc = s
1ec8d 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 72  qlite3VtabCallCr
1ec8e 65 61 74 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31  eate(db, pOp->p1
1ec8f 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 26 70 2d  , pOp->p4.z, &p-
1ec90 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 62 72 65  >zErrMsg);.  bre
1ec91 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
1ec92 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1ec93 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66  UALTABLE */..#if
1ec94 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1ec95 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a  _VIRTUALTABLE./*
1ec96 20 4f 70 63 6f 64 65 3a 20 56 44 65 73 74 72 6f   Opcode: VDestro
1ec97 79 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a  y P1 * * P4 *.**
1ec98 0a 2a 2a 20 50 34 20 69 73 20 74 68 65 20 6e 61  .** P4 is the na
1ec99 6d 65 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20  me of a virtual 
1ec9a 74 61 62 6c 65 20 69 6e 20 64 61 74 61 62 61 73  table in databas
1ec9b 65 20 50 31 2e 20 20 43 61 6c 6c 20 74 68 65 20  e P1.  Call the 
1ec9c 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 0a  xDestroy method.
1ec9d 2a 2a 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65  ** of that table
1ec9e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 44 65  ..*/.case OP_VDe
1ec9f 73 74 72 6f 79 3a 20 7b 0a 20 20 70 2d 3e 69 6e  stroy: {.  p->in
1eca0 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 32 3b 0a  VtabMethod = 2;.
1eca1 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74    rc = sqlite3Vt
1eca2 61 62 43 61 6c 6c 44 65 73 74 72 6f 79 28 64 62  abCallDestroy(db
1eca3 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e  , pOp->p1, pOp->
1eca4 70 34 2e 7a 29 3b 0a 20 20 70 2d 3e 69 6e 56 74  p4.z);.  p->inVt
1eca5 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20  abMethod = 0;.  
1eca6 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20  break;.}.#endif 
1eca7 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
1eca8 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a  IRTUALTABLE */..
1eca9 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1ecaa 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1ecab 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4f 70 65  ./* Opcode: VOpe
1ecac 6e 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a  n P1 * * P4 *.**
1ecad 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e  .** P4 is a poin
1ecae 74 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c  ter to a virtual
1ecaf 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61   table object, a
1ecb0 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73  n sqlite3_vtab s
1ecb1 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 50 31 20  tructure..** P1 
1ecb2 69 73 20 61 20 63 75 72 73 6f 72 20 6e 75 6d 62  is a cursor numb
1ecb3 65 72 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65  er.  This opcode
1ecb4 20 6f 70 65 6e 73 20 61 20 63 75 72 73 6f 72 20   opens a cursor 
1ecb5 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a  to the virtual.*
1ecb6 2a 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f 72  * table and stor
1ecb7 65 73 20 74 68 61 74 20 63 75 72 73 6f 72 20 69  es that cursor i
1ecb8 6e 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  n P1..*/.case OP
1ecb9 5f 56 4f 70 65 6e 3a 20 7b 0a 23 69 66 20 30 20  _VOpen: {.#if 0 
1ecba 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1ecbb 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1ecbc 2e 63 67 20 2a 2f 0a 20 20 56 64 62 65 43 75 72  .cg */.  VdbeCur
1ecbd 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 73 71 6c  sor *pCur;.  sql
1ecbe 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
1ecbf 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20   *pVtabCursor;. 
1ecc0 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
1ecc1 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Vtab;.  sqlite3_
1ecc2 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b  module *pModule;
1ecc3 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1ecc4 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1ecc5 20 69 6e 74 6f 20 75 2e 63 67 20 2a 2f 0a 0a 20   into u.cg */.. 
1ecc6 20 75 2e 63 67 2e 70 43 75 72 20 3d 20 30 3b 0a   u.cg.pCur = 0;.
1ecc7 20 20 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73    u.cg.pVtabCurs
1ecc8 6f 72 20 3d 20 30 3b 0a 20 20 75 2e 63 67 2e 70  or = 0;.  u.cg.p
1ecc9 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70  Vtab = pOp->p4.p
1ecca 56 74 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 75  Vtab->pVtab;.  u
1eccb 2e 63 67 2e 70 4d 6f 64 75 6c 65 20 3d 20 28 73  .cg.pModule = (s
1eccc 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29  qlite3_module *)
1eccd 75 2e 63 67 2e 70 56 74 61 62 2d 3e 70 4d 6f 64  u.cg.pVtab->pMod
1ecce 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 75 2e  ule;.  assert(u.
1eccf 63 67 2e 70 56 74 61 62 20 26 26 20 75 2e 63 67  cg.pVtab && u.cg
1ecd0 2e 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 69 66 28  .pModule);.  if(
1ecd1 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
1ecd2 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  f(db) ) goto abo
1ecd3 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1ecd4 3b 0a 20 20 72 63 20 3d 20 75 2e 63 67 2e 70 4d  ;.  rc = u.cg.pM
1ecd5 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 75 2e 63  odule->xOpen(u.c
1ecd6 67 2e 70 56 74 61 62 2c 20 26 75 2e 63 67 2e 70  g.pVtab, &u.cg.p
1ecd7 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 73  VtabCursor);.  s
1ecd8 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1ecd9 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20   p->zErrMsg);.  
1ecda 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63  p->zErrMsg = u.c
1ecdb 67 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67  g.pVtab->zErrMsg
1ecdc 3b 0a 20 20 75 2e 63 67 2e 70 56 74 61 62 2d 3e  ;.  u.cg.pVtab->
1ecdd 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69  zErrMsg = 0;.  i
1ecde 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1ecdf 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  On(db) ) goto ab
1ece0 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
1ece1 65 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  e;.  if( SQLITE_
1ece2 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20 2f 2a  OK==rc ){.    /*
1ece3 20 49 6e 69 74 69 61 6c 69 7a 65 20 73 71 6c 69   Initialize sqli
1ece4 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
1ece5 62 61 73 65 20 63 6c 61 73 73 20 2a 2f 0a 20 20  base class */.  
1ece6 20 20 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73    u.cg.pVtabCurs
1ece7 6f 72 2d 3e 70 56 74 61 62 20 3d 20 75 2e 63 67  or->pVtab = u.cg
1ece8 2e 70 56 74 61 62 3b 0a 0a 20 20 20 20 2f 2a 20  .pVtab;..    /* 
1ece9 49 6e 69 74 69 61 6c 69 73 65 20 76 64 62 65 20  Initialise vdbe 
1ecea 63 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f  cursor object */
1eceb 0a 20 20 20 20 75 2e 63 67 2e 70 43 75 72 20 3d  .    u.cg.pCur =
1ecec 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28   allocateCursor(
1eced 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 2d  p, pOp->p1, 0, -
1ecee 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 75  1, 0);.    if( u
1ecef 2e 63 67 2e 70 43 75 72 20 29 7b 0a 20 20 20 20  .cg.pCur ){.    
1ecf0 20 20 75 2e 63 67 2e 70 43 75 72 2d 3e 70 56 74    u.cg.pCur->pVt
1ecf1 61 62 43 75 72 73 6f 72 20 3d 20 75 2e 63 67 2e  abCursor = u.cg.
1ecf2 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20  pVtabCursor;.   
1ecf3 20 20 20 75 2e 63 67 2e 70 43 75 72 2d 3e 70 4d     u.cg.pCur->pM
1ecf4 6f 64 75 6c 65 20 3d 20 75 2e 63 67 2e 70 56 74  odule = u.cg.pVt
1ecf5 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 2d  abCursor->pVtab-
1ecf6 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d 65  >pModule;.    }e
1ecf7 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d  lse{.      db->m
1ecf8 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
1ecf9 0a 20 20 20 20 20 20 75 2e 63 67 2e 70 4d 6f 64  .      u.cg.pMod
1ecfa 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 75 2e 63 67  ule->xClose(u.cg
1ecfb 2e 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20  .pVtabCursor);. 
1ecfc 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b     }.  }.  break
1ecfd 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
1ecfe 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1ecff 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64  LTABLE */..#ifnd
1ed00 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
1ed01 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f  IRTUALTABLE./* O
1ed02 70 63 6f 64 65 3a 20 56 46 69 6c 74 65 72 20 50  pcode: VFilter P
1ed03 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a  1 P2 P3 P4 *.**.
1ed04 2a 2a 20 50 31 20 69 73 20 61 20 63 75 72 73 6f  ** P1 is a curso
1ed05 72 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 56  r opened using V
1ed06 4f 70 65 6e 2e 20 20 50 32 20 69 73 20 61 6e 20  Open.  P2 is an 
1ed07 61 64 64 72 65 73 73 20 74 6f 20 6a 75 6d 70 20  address to jump 
1ed08 74 6f 20 69 66 0a 2a 2a 20 74 68 65 20 66 69 6c  to if.** the fil
1ed09 74 65 72 65 64 20 72 65 73 75 6c 74 20 73 65 74  tered result set
1ed0a 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a   is empty..**.**
1ed0b 20 50 34 20 69 73 20 65 69 74 68 65 72 20 4e 55   P4 is either NU
1ed0c 4c 4c 20 6f 72 20 61 20 73 74 72 69 6e 67 20 74  LL or a string t
1ed0d 68 61 74 20 77 61 73 20 67 65 6e 65 72 61 74 65  hat was generate
1ed0e 64 20 62 79 20 74 68 65 20 78 42 65 73 74 49 6e  d by the xBestIn
1ed0f 64 65 78 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66  dex.** method of
1ed10 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68   the module.  Th
1ed11 65 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e  e interpretation
1ed12 20 6f 66 20 74 68 65 20 50 34 20 73 74 72 69 6e   of the P4 strin
1ed13 67 20 69 73 20 6c 65 66 74 0a 2a 2a 20 74 6f 20  g is left.** to 
1ed14 74 68 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65  the module imple
1ed15 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  mentation..**.**
1ed16 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76   This opcode inv
1ed17 6f 6b 65 73 20 74 68 65 20 78 46 69 6c 74 65 72  okes the xFilter
1ed18 20 6d 65 74 68 6f 64 20 6f 6e 20 74 68 65 20 76   method on the v
1ed19 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 70 65  irtual table spe
1ed1a 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 50 31 2e  cified.** by P1.
1ed1b 20 20 54 68 65 20 69 6e 74 65 67 65 72 20 71 75    The integer qu
1ed1c 65 72 79 20 70 6c 61 6e 20 70 61 72 61 6d 65 74  ery plan paramet
1ed1d 65 72 20 74 6f 20 78 46 69 6c 74 65 72 20 69 73  er to xFilter is
1ed1e 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
1ed1f 74 65 72 0a 2a 2a 20 50 33 2e 20 52 65 67 69 73  ter.** P3. Regis
1ed20 74 65 72 20 50 33 2b 31 20 73 74 6f 72 65 73 20  ter P3+1 stores 
1ed21 74 68 65 20 61 72 67 63 20 70 61 72 61 6d 65 74  the argc paramet
1ed22 65 72 20 74 6f 20 62 65 20 70 61 73 73 65 64 20  er to be passed 
1ed23 74 6f 20 74 68 65 0a 2a 2a 20 78 46 69 6c 74 65  to the.** xFilte
1ed24 72 20 6d 65 74 68 6f 64 2e 20 52 65 67 69 73 74  r method. Regist
1ed25 65 72 73 20 50 33 2b 32 2e 2e 50 33 2b 31 2b 61  ers P3+2..P3+1+a
1ed26 72 67 63 20 61 72 65 20 74 68 65 20 61 72 67 63  rgc are the argc
1ed27 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 70  .** additional p
1ed28 61 72 61 6d 65 74 65 72 73 20 77 68 69 63 68 20  arameters which 
1ed29 61 72 65 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a  are passed to.**
1ed2a 20 78 46 69 6c 74 65 72 20 61 73 20 61 72 67 76   xFilter as argv
1ed2b 2e 20 52 65 67 69 73 74 65 72 20 50 33 2b 32 20  . Register P3+2 
1ed2c 62 65 63 6f 6d 65 73 20 61 72 67 76 5b 30 5d 20  becomes argv[0] 
1ed2d 77 68 65 6e 20 70 61 73 73 65 64 20 74 6f 20 78  when passed to x
1ed2e 46 69 6c 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20  Filter..**.** A 
1ed2f 6a 75 6d 70 20 69 73 20 6d 61 64 65 20 74 6f 20  jump is made to 
1ed30 50 32 20 69 66 20 74 68 65 20 72 65 73 75 6c 74  P2 if the result
1ed31 20 73 65 74 20 61 66 74 65 72 20 66 69 6c 74 65   set after filte
1ed32 72 69 6e 67 20 77 6f 75 6c 64 20 62 65 20 65 6d  ring would be em
1ed33 70 74 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  pty..*/.case OP_
1ed34 56 46 69 6c 74 65 72 3a 20 7b 20 20 20 2f 2a 20  VFilter: {   /* 
1ed35 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f  jump */.#if 0  /
1ed36 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1ed37 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63  s moved into u.c
1ed38 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 3b  h */.  int nArg;
1ed39 0a 20 20 69 6e 74 20 69 51 75 65 72 79 3b 0a 20  .  int iQuery;. 
1ed3a 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
1ed3b 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a  odule *pModule;.
1ed3c 20 20 4d 65 6d 20 2a 70 51 75 65 72 79 3b 0a 20    Mem *pQuery;. 
1ed3d 20 4d 65 6d 20 2a 70 41 72 67 63 3b 0a 20 20 73   Mem *pArgc;.  s
1ed3e 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
1ed3f 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b  or *pVtabCursor;
1ed40 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
1ed41 2a 70 56 74 61 62 3b 0a 20 20 56 64 62 65 43 75  *pVtab;.  VdbeCu
1ed42 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 69 6e  rsor *pCur;.  in
1ed43 74 20 72 65 73 3b 0a 20 20 69 6e 74 20 69 3b 0a  t res;.  int i;.
1ed44 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 23    Mem **apArg;.#
1ed45 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1ed46 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1ed47 6e 74 6f 20 75 2e 63 68 20 2a 2f 0a 0a 20 20 75  nto u.ch */..  u
1ed48 2e 63 68 2e 70 51 75 65 72 79 20 3d 20 26 70 2d  .ch.pQuery = &p-
1ed49 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  >aMem[pOp->p3];.
1ed4a 20 20 75 2e 63 68 2e 70 41 72 67 63 20 3d 20 26    u.ch.pArgc = &
1ed4b 75 2e 63 68 2e 70 51 75 65 72 79 5b 31 5d 3b 0a  u.ch.pQuery[1];.
1ed4c 20 20 75 2e 63 68 2e 70 43 75 72 20 3d 20 70 2d    u.ch.pCur = p-
1ed4d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
1ed4e 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43  .  REGISTER_TRAC
1ed4f 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 63 68 2e  E(pOp->p3, u.ch.
1ed50 70 51 75 65 72 79 29 3b 0a 20 20 61 73 73 65 72  pQuery);.  asser
1ed51 74 28 20 75 2e 63 68 2e 70 43 75 72 2d 3e 70 56  t( u.ch.pCur->pV
1ed52 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 75  tabCursor );.  u
1ed53 2e 63 68 2e 70 56 74 61 62 43 75 72 73 6f 72 20  .ch.pVtabCursor 
1ed54 3d 20 75 2e 63 68 2e 70 43 75 72 2d 3e 70 56 74  = u.ch.pCur->pVt
1ed55 61 62 43 75 72 73 6f 72 3b 0a 20 20 75 2e 63 68  abCursor;.  u.ch
1ed56 2e 70 56 74 61 62 20 3d 20 75 2e 63 68 2e 70 56  .pVtab = u.ch.pV
1ed57 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62  tabCursor->pVtab
1ed58 3b 0a 20 20 75 2e 63 68 2e 70 4d 6f 64 75 6c 65  ;.  u.ch.pModule
1ed59 20 3d 20 75 2e 63 68 2e 70 56 74 61 62 2d 3e 70   = u.ch.pVtab->p
1ed5a 4d 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a 20 47 72  Module;..  /* Gr
1ed5b 61 62 20 74 68 65 20 69 6e 64 65 78 20 6e 75 6d  ab the index num
1ed5c 62 65 72 20 61 6e 64 20 61 72 67 63 20 70 61 72  ber and argc par
1ed5d 61 6d 65 74 65 72 73 20 2a 2f 0a 20 20 61 73 73  ameters */.  ass
1ed5e 65 72 74 28 20 28 75 2e 63 68 2e 70 51 75 65 72  ert( (u.ch.pQuer
1ed5f 79 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74  y->flags&MEM_Int
1ed60 29 21 3d 30 20 26 26 20 75 2e 63 68 2e 70 41 72  )!=0 && u.ch.pAr
1ed61 67 63 2d 3e 66 6c 61 67 73 3d 3d 4d 45 4d 5f 49  gc->flags==MEM_I
1ed62 6e 74 20 29 3b 0a 20 20 75 2e 63 68 2e 6e 41 72  nt );.  u.ch.nAr
1ed63 67 20 3d 20 28 69 6e 74 29 75 2e 63 68 2e 70 41  g = (int)u.ch.pA
1ed64 72 67 63 2d 3e 75 2e 69 3b 0a 20 20 75 2e 63 68  rgc->u.i;.  u.ch
1ed65 2e 69 51 75 65 72 79 20 3d 20 28 69 6e 74 29 75  .iQuery = (int)u
1ed66 2e 63 68 2e 70 51 75 65 72 79 2d 3e 75 2e 69 3b  .ch.pQuery->u.i;
1ed67 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68  ..  /* Invoke th
1ed68 65 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64  e xFilter method
1ed69 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 2e 63 68   */.  {.    u.ch
1ed6a 2e 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 75 2e  .res = 0;.    u.
1ed6b 63 68 2e 61 70 41 72 67 20 3d 20 70 2d 3e 61 70  ch.apArg = p->ap
1ed6c 41 72 67 3b 0a 20 20 20 20 66 6f 72 28 75 2e 63  Arg;.    for(u.c
1ed6d 68 2e 69 20 3d 20 30 3b 20 75 2e 63 68 2e 69 3c  h.i = 0; u.ch.i<
1ed6e 75 2e 63 68 2e 6e 41 72 67 3b 20 75 2e 63 68 2e  u.ch.nArg; u.ch.
1ed6f 69 2b 2b 29 7b 0a 20 20 20 20 20 20 75 2e 63 68  i++){.      u.ch
1ed70 2e 61 70 41 72 67 5b 75 2e 63 68 2e 69 5d 20 3d  .apArg[u.ch.i] =
1ed71 20 26 75 2e 63 68 2e 70 41 72 67 63 5b 75 2e 63   &u.ch.pArgc[u.c
1ed72 68 2e 69 2b 31 5d 3b 0a 20 20 20 20 20 20 73 74  h.i+1];.      st
1ed73 6f 72 65 54 79 70 65 49 6e 66 6f 28 75 2e 63 68  oreTypeInfo(u.ch
1ed74 2e 61 70 41 72 67 5b 75 2e 63 68 2e 69 5d 2c 20  .apArg[u.ch.i], 
1ed75 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  0);.    }..    i
1ed76 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1ed77 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61  Off(db) ) goto a
1ed78 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75  bort_due_to_misu
1ed79 73 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61  se;.    p->inVta
1ed7a 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 20  bMethod = 1;.   
1ed7b 20 72 63 20 3d 20 75 2e 63 68 2e 70 4d 6f 64 75   rc = u.ch.pModu
1ed7c 6c 65 2d 3e 78 46 69 6c 74 65 72 28 75 2e 63 68  le->xFilter(u.ch
1ed7d 2e 70 56 74 61 62 43 75 72 73 6f 72 2c 20 75 2e  .pVtabCursor, u.
1ed7e 63 68 2e 69 51 75 65 72 79 2c 20 70 4f 70 2d 3e  ch.iQuery, pOp->
1ed7f 70 34 2e 7a 2c 20 75 2e 63 68 2e 6e 41 72 67 2c  p4.z, u.ch.nArg,
1ed80 20 75 2e 63 68 2e 61 70 41 72 67 29 3b 0a 20 20   u.ch.apArg);.  
1ed81 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f    p->inVtabMetho
1ed82 64 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74  d = 0;.    sqlit
1ed83 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
1ed84 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d  zErrMsg);.    p-
1ed85 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 68 2e  >zErrMsg = u.ch.
1ed86 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a  pVtab->zErrMsg;.
1ed87 20 20 20 20 75 2e 63 68 2e 70 56 74 61 62 2d 3e      u.ch.pVtab->
1ed88 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20  zErrMsg = 0;.   
1ed89 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1ed8a 4f 4b 20 29 7b 0a 20 20 20 20 20 20 75 2e 63 68  OK ){.      u.ch
1ed8b 2e 72 65 73 20 3d 20 75 2e 63 68 2e 70 4d 6f 64  .res = u.ch.pMod
1ed8c 75 6c 65 2d 3e 78 45 6f 66 28 75 2e 63 68 2e 70  ule->xEof(u.ch.p
1ed8d 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 20  VtabCursor);.   
1ed8e 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74   }.    if( sqlit
1ed8f 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29  e3SafetyOn(db) )
1ed90 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1ed91 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 20 20  to_misuse;..    
1ed92 69 66 28 20 75 2e 63 68 2e 72 65 73 20 29 7b 0a  if( u.ch.res ){.
1ed93 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e        pc = pOp->
1ed94 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  p2 - 1;.    }.  
1ed95 7d 0a 20 20 75 2e 63 68 2e 70 43 75 72 2d 3e 6e  }.  u.ch.pCur->n
1ed96 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 0a 20 20 62  ullRow = 0;..  b
1ed97 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  reak;.}.#endif /
1ed98 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  * SQLITE_OMIT_VI
1ed99 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23  RTUALTABLE */..#
1ed9a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1ed9b 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
1ed9c 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 43 6f 6c 75  /* Opcode: VColu
1ed9d 6d 6e 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  mn P1 P2 P3 * *.
1ed9e 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20  **.** Store the 
1ed9f 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 32 2d  value of the P2-
1eda0 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a 20  th column of.** 
1eda1 74 68 65 20 72 6f 77 20 6f 66 20 74 68 65 20 76  the row of the v
1eda2 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 74 68 61  irtual-table tha
1eda3 74 20 74 68 65 20 0a 2a 2a 20 50 31 20 63 75 72  t the .** P1 cur
1eda4 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20  sor is pointing 
1eda5 74 6f 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  to into register
1eda6 20 50 33 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f   P3..*/.case OP_
1eda7 56 43 6f 6c 75 6d 6e 3a 20 7b 0a 23 69 66 20 30  VColumn: {.#if 0
1eda8 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1eda9 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1edaa 75 2e 63 69 20 2a 2f 0a 20 20 73 71 6c 69 74 65  u.ci */.  sqlite
1edab 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20  3_vtab *pVtab;. 
1edac 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
1edad 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a  odule *pModule;.
1edae 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 0a 20 20    Mem *pDest;.  
1edaf 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1edb0 73 43 6f 6e 74 65 78 74 3b 0a 23 65 6e 64 69 66  sContext;.#endif
1edb1 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1edb2 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1edb3 2e 63 69 20 2a 2f 0a 0a 20 20 56 64 62 65 43 75  .ci */..  VdbeCu
1edb4 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e  rsor *pCur = p->
1edb5 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  apCsr[pOp->p1];.
1edb6 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
1edb7 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20  pVtabCursor );. 
1edb8 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33   assert( pOp->p3
1edb9 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70  >0 && pOp->p3<=p
1edba 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 63 69  ->nMem );.  u.ci
1edbb 2e 70 44 65 73 74 20 3d 20 26 70 2d 3e 61 4d 65  .pDest = &p->aMe
1edbc 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66  m[pOp->p3];.  if
1edbd 28 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20  ( pCur->nullRow 
1edbe 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1edbf 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 75 2e 63  beMemSetNull(u.c
1edc0 69 2e 70 44 65 73 74 29 3b 0a 20 20 20 20 62 72  i.pDest);.    br
1edc1 65 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e 63 69 2e  eak;.  }.  u.ci.
1edc2 70 56 74 61 62 20 3d 20 70 43 75 72 2d 3e 70 56  pVtab = pCur->pV
1edc3 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62  tabCursor->pVtab
1edc4 3b 0a 20 20 75 2e 63 69 2e 70 4d 6f 64 75 6c 65  ;.  u.ci.pModule
1edc5 20 3d 20 75 2e 63 69 2e 70 56 74 61 62 2d 3e 70   = u.ci.pVtab->p
1edc6 4d 6f 64 75 6c 65 3b 0a 20 20 61 73 73 65 72 74  Module;.  assert
1edc7 28 20 75 2e 63 69 2e 70 4d 6f 64 75 6c 65 2d 3e  ( u.ci.pModule->
1edc8 78 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 6d 65 6d  xColumn );.  mem
1edc9 73 65 74 28 26 75 2e 63 69 2e 73 43 6f 6e 74 65  set(&u.ci.sConte
1edca 78 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 2e  xt, 0, sizeof(u.
1edcb 63 69 2e 73 43 6f 6e 74 65 78 74 29 29 3b 0a 0a  ci.sContext));..
1edcc 20 20 2f 2a 20 54 68 65 20 6f 75 74 70 75 74 20    /* The output 
1edcd 63 65 6c 6c 20 6d 61 79 20 61 6c 72 65 61 64 79  cell may already
1edce 20 68 61 76 65 20 61 20 62 75 66 66 65 72 20 61   have a buffer a
1edcf 6c 6c 6f 63 61 74 65 64 2e 20 4d 6f 76 65 0a 20  llocated. Move. 
1edd0 20 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20   ** the current 
1edd1 63 6f 6e 74 65 6e 74 73 20 74 6f 20 75 2e 63 69  contents to u.ci
1edd2 2e 73 43 6f 6e 74 65 78 74 2e 73 20 73 6f 20 69  .sContext.s so i
1edd3 6e 20 63 61 73 65 20 74 68 65 20 75 73 65 72 2d  n case the user-
1edd4 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 63 61  function.  ** ca
1edd5 6e 20 75 73 65 20 74 68 65 20 61 6c 72 65 61 64  n use the alread
1edd6 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66  y allocated buff
1edd7 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 6c  er instead of al
1edd8 6c 6f 63 61 74 69 6e 67 20 61 0a 20 20 2a 2a 20  locating a.  ** 
1edd9 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20  new one..  */.  
1edda 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f  sqlite3VdbeMemMo
1eddb 76 65 28 26 75 2e 63 69 2e 73 43 6f 6e 74 65 78  ve(&u.ci.sContex
1eddc 74 2e 73 2c 20 75 2e 63 69 2e 70 44 65 73 74 29  t.s, u.ci.pDest)
1eddd 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c  ;.  MemSetTypeFl
1edde 61 67 28 26 75 2e 63 69 2e 73 43 6f 6e 74 65 78  ag(&u.ci.sContex
1eddf 74 2e 73 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a  t.s, MEM_Null);.
1ede0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
1ede1 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f  fetyOff(db) ) go
1ede2 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1ede3 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 75  misuse;.  rc = u
1ede4 2e 63 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 43 6f  .ci.pModule->xCo
1ede5 6c 75 6d 6e 28 70 43 75 72 2d 3e 70 56 74 61 62  lumn(pCur->pVtab
1ede6 43 75 72 73 6f 72 2c 20 26 75 2e 63 69 2e 73 43  Cursor, &u.ci.sC
1ede7 6f 6e 74 65 78 74 2c 20 70 4f 70 2d 3e 70 32 29  ontext, pOp->p2)
1ede8 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
1ede9 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67  e(db, p->zErrMsg
1edea 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20  );.  p->zErrMsg 
1edeb 3d 20 75 2e 63 69 2e 70 56 74 61 62 2d 3e 7a 45  = u.ci.pVtab->zE
1edec 72 72 4d 73 67 3b 0a 20 20 75 2e 63 69 2e 70 56  rrMsg;.  u.ci.pV
1eded 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30  tab->zErrMsg = 0
1edee 3b 0a 20 20 69 66 28 20 75 2e 63 69 2e 73 43 6f  ;.  if( u.ci.sCo
1edef 6e 74 65 78 74 2e 69 73 45 72 72 6f 72 20 29 7b  ntext.isError ){
1edf0 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 69 2e 73  .    rc = u.ci.s
1edf1 43 6f 6e 74 65 78 74 2e 69 73 45 72 72 6f 72 3b  Context.isError;
1edf2 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20  .  }..  /* Copy 
1edf3 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68  the result of th
1edf4 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68  e function to th
1edf5 65 20 50 33 20 72 65 67 69 73 74 65 72 2e 20 57  e P3 register. W
1edf6 65 0a 20 20 2a 2a 20 64 6f 20 74 68 69 73 20 72  e.  ** do this r
1edf7 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65  egardless of whe
1edf8 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65  ther or not an e
1edf9 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 74 6f  rror occurred to
1edfa 20 65 6e 73 75 72 65 20 61 6e 79 0a 20 20 2a 2a   ensure any.  **
1edfb 20 64 79 6e 61 6d 69 63 20 61 6c 6c 6f 63 61 74   dynamic allocat
1edfc 69 6f 6e 20 69 6e 20 75 2e 63 69 2e 73 43 6f 6e  ion in u.ci.sCon
1edfd 74 65 78 74 2e 73 20 28 61 20 4d 65 6d 20 73 74  text.s (a Mem st
1edfe 72 75 63 74 29 20 69 73 20 20 72 65 6c 65 61 73  ruct) is  releas
1edff 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ed..  */.  sqlit
1ee00 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
1ee01 64 69 6e 67 28 26 75 2e 63 69 2e 73 43 6f 6e 74  ding(&u.ci.sCont
1ee02 65 78 74 2e 73 2c 20 65 6e 63 6f 64 69 6e 67 29  ext.s, encoding)
1ee03 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41  ;.  REGISTER_TRA
1ee04 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 63 69  CE(pOp->p3, u.ci
1ee05 2e 70 44 65 73 74 29 3b 0a 20 20 73 71 6c 69 74  .pDest);.  sqlit
1ee06 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 75 2e  e3VdbeMemMove(u.
1ee07 63 69 2e 70 44 65 73 74 2c 20 26 75 2e 63 69 2e  ci.pDest, &u.ci.
1ee08 73 43 6f 6e 74 65 78 74 2e 73 29 3b 0a 20 20 55  sContext.s);.  U
1ee09 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49  PDATE_MAX_BLOBSI
1ee0a 5a 45 28 75 2e 63 69 2e 70 44 65 73 74 29 3b 0a  ZE(u.ci.pDest);.
1ee0b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
1ee0c 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20  fetyOn(db) ){.  
1ee0d 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65    goto abort_due
1ee0e 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 7d 0a  _to_misuse;.  }.
1ee0f 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
1ee10 65 4d 65 6d 54 6f 6f 42 69 67 28 75 2e 63 69 2e  eMemTooBig(u.ci.
1ee11 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20 67 6f  pDest) ){.    go
1ee12 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a  to too_big;.  }.
1ee13 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69    break;.}.#endi
1ee14 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1ee15 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
1ee16 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1ee17 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1ee18 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4e  LE./* Opcode: VN
1ee19 65 78 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  ext P1 P2 * * *.
1ee1a 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 76 69  **.** Advance vi
1ee1b 72 74 75 61 6c 20 74 61 62 6c 65 20 50 31 20 74  rtual table P1 t
1ee1c 6f 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20 69  o the next row i
1ee1d 6e 20 69 74 73 20 72 65 73 75 6c 74 20 73 65 74  n its result set
1ee1e 20 61 6e 64 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20   and.** jump to 
1ee1f 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e 20  instruction P2. 
1ee20 20 4f 72 2c 20 69 66 20 74 68 65 20 76 69 72 74   Or, if the virt
1ee21 75 61 6c 20 74 61 62 6c 65 20 68 61 73 20 72 65  ual table has re
1ee22 61 63 68 65 64 0a 2a 2a 20 74 68 65 20 65 6e 64  ached.** the end
1ee23 20 6f 66 20 69 74 73 20 72 65 73 75 6c 74 20 73   of its result s
1ee24 65 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74 68  et, then fall th
1ee25 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78  rough to the nex
1ee26 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  t instruction..*
1ee27 2f 0a 63 61 73 65 20 4f 50 5f 56 4e 65 78 74 3a  /.case OP_VNext:
1ee28 20 7b 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a   {   /* jump */.
1ee29 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1ee2a 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1ee2b 69 6e 74 6f 20 75 2e 63 6a 20 2a 2f 0a 20 20 73  into u.cj */.  s
1ee2c 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
1ee2d 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69  ab;.  const sqli
1ee2e 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
1ee2f 75 6c 65 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a  ule;.  int res;.
1ee30 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1ee31 75 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f  ur;.#endif /* lo
1ee32 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1ee33 76 65 64 20 69 6e 74 6f 20 75 2e 63 6a 20 2a 2f  ved into u.cj */
1ee34 0a 0a 20 20 75 2e 63 6a 2e 72 65 73 20 3d 20 30  ..  u.cj.res = 0
1ee35 3b 0a 20 20 75 2e 63 6a 2e 70 43 75 72 20 3d 20  ;.  u.cj.pCur = 
1ee36 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31  p->apCsr[pOp->p1
1ee37 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63  ];.  assert( u.c
1ee38 6a 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72  j.pCur->pVtabCur
1ee39 73 6f 72 20 29 3b 0a 20 20 69 66 28 20 75 2e 63  sor );.  if( u.c
1ee3a 6a 2e 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20  j.pCur->nullRow 
1ee3b 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  ){.    break;.  
1ee3c 7d 0a 20 20 75 2e 63 6a 2e 70 56 74 61 62 20 3d  }.  u.cj.pVtab =
1ee3d 20 75 2e 63 6a 2e 70 43 75 72 2d 3e 70 56 74 61   u.cj.pCur->pVta
1ee3e 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a  bCursor->pVtab;.
1ee3f 20 20 75 2e 63 6a 2e 70 4d 6f 64 75 6c 65 20 3d    u.cj.pModule =
1ee40 20 75 2e 63 6a 2e 70 56 74 61 62 2d 3e 70 4d 6f   u.cj.pVtab->pMo
1ee41 64 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20  dule;.  assert( 
1ee42 75 2e 63 6a 2e 70 4d 6f 64 75 6c 65 2d 3e 78 4e  u.cj.pModule->xN
1ee43 65 78 74 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76  ext );..  /* Inv
1ee44 6f 6b 65 20 74 68 65 20 78 4e 65 78 74 28 29 20  oke the xNext() 
1ee45 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 6d 6f  method of the mo
1ee46 64 75 6c 65 2e 20 54 68 65 72 65 20 69 73 20 6e  dule. There is n
1ee47 6f 20 77 61 79 20 66 6f 72 20 74 68 65 0a 20 20  o way for the.  
1ee48 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 69 6d  ** underlying im
1ee49 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 6f 20  plementation to 
1ee4a 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  return an error 
1ee4b 69 66 20 6f 6e 65 20 6f 63 63 75 72 73 20 64 75  if one occurs du
1ee4c 72 69 6e 67 0a 20 20 2a 2a 20 78 4e 65 78 74 28  ring.  ** xNext(
1ee4d 29 2e 20 49 6e 73 74 65 61 64 2c 20 69 66 20 61  ). Instead, if a
1ee4e 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
1ee4f 74 72 75 65 20 69 73 20 72 65 74 75 72 6e 65 64  true is returned
1ee50 20 28 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61   (indicating tha
1ee51 74 0a 20 20 2a 2a 20 64 61 74 61 20 69 73 20 61  t.  ** data is a
1ee52 76 61 69 6c 61 62 6c 65 29 20 61 6e 64 20 74 68  vailable) and th
1ee53 65 20 65 72 72 6f 72 20 63 6f 64 65 20 72 65 74  e error code ret
1ee54 75 72 6e 65 64 20 77 68 65 6e 20 78 43 6f 6c 75  urned when xColu
1ee55 6d 6e 20 6f 72 0a 20 20 2a 2a 20 73 6f 6d 65 20  mn or.  ** some 
1ee56 6f 74 68 65 72 20 6d 65 74 68 6f 64 20 69 73 20  other method is 
1ee57 6e 65 78 74 20 69 6e 76 6f 6b 65 64 20 6f 6e 20  next invoked on 
1ee58 74 68 65 20 73 61 76 65 20 76 69 72 74 75 61 6c  the save virtual
1ee59 20 74 61 62 6c 65 20 63 75 72 73 6f 72 2e 0a 20   table cursor.. 
1ee5a 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
1ee5b 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29  3SafetyOff(db) )
1ee5c 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1ee5d 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 70 2d 3e  to_misuse;.  p->
1ee5e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 31  inVtabMethod = 1
1ee5f 3b 0a 20 20 72 63 20 3d 20 75 2e 63 6a 2e 70 4d  ;.  rc = u.cj.pM
1ee60 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 75 2e 63  odule->xNext(u.c
1ee61 6a 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72  j.pCur->pVtabCur
1ee62 73 6f 72 29 3b 0a 20 20 70 2d 3e 69 6e 56 74 61  sor);.  p->inVta
1ee63 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 73  bMethod = 0;.  s
1ee64 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1ee65 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20   p->zErrMsg);.  
1ee66 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63  p->zErrMsg = u.c
1ee67 6a 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67  j.pVtab->zErrMsg
1ee68 3b 0a 20 20 75 2e 63 6a 2e 70 56 74 61 62 2d 3e  ;.  u.cj.pVtab->
1ee69 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69  zErrMsg = 0;.  i
1ee6a 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1ee6b 20 29 7b 0a 20 20 20 20 75 2e 63 6a 2e 72 65 73   ){.    u.cj.res
1ee6c 20 3d 20 75 2e 63 6a 2e 70 4d 6f 64 75 6c 65 2d   = u.cj.pModule-
1ee6d 3e 78 45 6f 66 28 75 2e 63 6a 2e 70 43 75 72 2d  >xEof(u.cj.pCur-
1ee6e 3e 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20  >pVtabCursor);. 
1ee6f 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
1ee70 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67  SafetyOn(db) ) g
1ee71 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1ee72 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 69 66 28 20  _misuse;..  if( 
1ee73 21 75 2e 63 6a 2e 72 65 73 20 29 7b 0a 20 20 20  !u.cj.res ){.   
1ee74 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
1ee75 64 61 74 61 2c 20 6a 75 6d 70 20 74 6f 20 50 32  data, jump to P2
1ee76 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f 70   */.    pc = pOp
1ee77 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20  ->p2 - 1;.  }.  
1ee78 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20  break;.}.#endif 
1ee79 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
1ee7a 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a  IRTUALTABLE */..
1ee7b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1ee7c 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1ee7d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 52 65 6e  ./* Opcode: VRen
1ee7e 61 6d 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a  ame P1 * * P4 *.
1ee7f 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f  **.** P4 is a po
1ee80 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72 74 75  inter to a virtu
1ee81 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c  al table object,
1ee82 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62   an sqlite3_vtab
1ee83 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54   structure..** T
1ee84 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b  his opcode invok
1ee85 65 73 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  es the correspon
1ee86 64 69 6e 67 20 78 52 65 6e 61 6d 65 20 6d 65 74  ding xRename met
1ee87 68 6f 64 2e 20 54 68 65 20 76 61 6c 75 65 0a 2a  hod. The value.*
1ee88 2a 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  * in register P1
1ee89 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68   is passed as th
1ee8a 65 20 7a 4e 61 6d 65 20 61 72 67 75 6d 65 6e 74  e zName argument
1ee8b 20 74 6f 20 74 68 65 20 78 52 65 6e 61 6d 65 20   to the xRename 
1ee8c 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 63 61 73 65 20  method..*/.case 
1ee8d 4f 50 5f 56 52 65 6e 61 6d 65 3a 20 7b 0a 23 69  OP_VRename: {.#i
1ee8e 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1ee8f 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1ee90 74 6f 20 75 2e 63 6b 20 2a 2f 0a 20 20 73 71 6c  to u.ck */.  sql
1ee91 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
1ee92 3b 0a 20 20 4d 65 6d 20 2a 70 4e 61 6d 65 3b 0a  ;.  Mem *pName;.
1ee93 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1ee94 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1ee95 69 6e 74 6f 20 75 2e 63 6b 20 2a 2f 0a 0a 20 20  into u.ck */..  
1ee96 75 2e 63 6b 2e 70 56 74 61 62 20 3d 20 70 4f 70  u.ck.pVtab = pOp
1ee97 2d 3e 70 34 2e 70 56 74 61 62 2d 3e 70 56 74 61  ->p4.pVtab->pVta
1ee98 62 3b 0a 20 20 75 2e 63 6b 2e 70 4e 61 6d 65 20  b;.  u.ck.pName 
1ee99 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
1ee9a 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75  p1];.  assert( u
1ee9b 2e 63 6b 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75  .ck.pVtab->pModu
1ee9c 6c 65 2d 3e 78 52 65 6e 61 6d 65 20 29 3b 0a 20  le->xRename );. 
1ee9d 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
1ee9e 70 4f 70 2d 3e 70 31 2c 20 75 2e 63 6b 2e 70 4e  pOp->p1, u.ck.pN
1ee9f 61 6d 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ame);.  assert( 
1eea0 75 2e 63 6b 2e 70 4e 61 6d 65 2d 3e 66 6c 61 67  u.ck.pName->flag
1eea1 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 3b 0a 20  s & MEM_Str );. 
1eea2 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
1eea3 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f  tyOff(db) ) goto
1eea4 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
1eea5 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 75 2e 63  suse;.  rc = u.c
1eea6 6b 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  k.pVtab->pModule
1eea7 2d 3e 78 52 65 6e 61 6d 65 28 75 2e 63 6b 2e 70  ->xRename(u.ck.p
1eea8 56 74 61 62 2c 20 75 2e 63 6b 2e 70 4e 61 6d 65  Vtab, u.ck.pName
1eea9 2d 3e 7a 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ->z);.  sqlite3D
1eeaa 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72  bFree(db, p->zEr
1eeab 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72  rMsg);.  p->zErr
1eeac 4d 73 67 20 3d 20 75 2e 63 6b 2e 70 56 74 61 62  Msg = u.ck.pVtab
1eead 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 75 2e 63  ->zErrMsg;.  u.c
1eeae 6b 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67  k.pVtab->zErrMsg
1eeaf 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69   = 0;.  if( sqli
1eeb0 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20  te3SafetyOn(db) 
1eeb1 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1eeb2 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 62  _to_misuse;..  b
1eeb3 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  reak;.}.#endif..
1eeb4 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1eeb5 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1eeb6 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 55 70 64  ./* Opcode: VUpd
1eeb7 61 74 65 20 50 31 20 50 32 20 50 33 20 50 34 20  ate P1 P2 P3 P4 
1eeb8 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20  *.**.** P4 is a 
1eeb9 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72  pointer to a vir
1eeba 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63  tual table objec
1eebb 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74  t, an sqlite3_vt
1eebc 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ab structure..**
1eebd 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76   This opcode inv
1eebe 6f 6b 65 73 20 74 68 65 20 63 6f 72 72 65 73 70  okes the corresp
1eebf 6f 6e 64 69 6e 67 20 78 55 70 64 61 74 65 20 6d  onding xUpdate m
1eec0 65 74 68 6f 64 2e 20 50 32 20 76 61 6c 75 65 73  ethod. P2 values
1eec1 0a 2a 2a 20 61 72 65 20 63 6f 6e 74 69 67 75 6f  .** are contiguo
1eec2 75 73 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  us memory cells 
1eec3 73 74 61 72 74 69 6e 67 20 61 74 20 50 33 20 74  starting at P3 t
1eec4 6f 20 70 61 73 73 20 74 6f 20 74 68 65 20 78 55  o pass to the xU
1eec5 70 64 61 74 65 20 0a 2a 2a 20 69 6e 76 6f 63 61  pdate .** invoca
1eec6 74 69 6f 6e 2e 20 54 68 65 20 76 61 6c 75 65 20  tion. The value 
1eec7 69 6e 20 72 65 67 69 73 74 65 72 20 28 50 33 2b  in register (P3+
1eec8 50 32 2d 31 29 20 63 6f 72 72 65 73 70 6f 6e 64  P2-1) correspond
1eec9 73 20 74 6f 20 74 68 65 20 0a 2a 2a 20 70 32 74  s to the .** p2t
1eeca 68 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65  h element of the
1eecb 20 61 72 67 76 20 61 72 72 61 79 20 70 61 73 73   argv array pass
1eecc 65 64 20 74 6f 20 78 55 70 64 61 74 65 2e 0a 2a  ed to xUpdate..*
1eecd 2a 0a 2a 2a 20 54 68 65 20 78 55 70 64 61 74 65  *.** The xUpdate
1eece 20 6d 65 74 68 6f 64 20 77 69 6c 6c 20 64 6f 20   method will do 
1eecf 61 20 44 45 4c 45 54 45 20 6f 72 20 61 6e 20 49  a DELETE or an I
1eed0 4e 53 45 52 54 20 6f 72 20 62 6f 74 68 2e 0a 2a  NSERT or both..*
1eed1 2a 20 54 68 65 20 61 72 67 76 5b 30 5d 20 65 6c  * The argv[0] el
1eed2 65 6d 65 6e 74 20 28 77 68 69 63 68 20 63 6f 72  ement (which cor
1eed3 72 65 73 70 6f 6e 64 73 20 74 6f 20 6d 65 6d 6f  responds to memo
1eed4 72 79 20 63 65 6c 6c 20 50 33 29 0a 2a 2a 20 69  ry cell P3).** i
1eed5 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 61  s the rowid of a
1eed6 20 72 6f 77 20 74 6f 20 64 65 6c 65 74 65 2e 20   row to delete. 
1eed7 20 49 66 20 61 72 67 76 5b 30 5d 20 69 73 20 4e   If argv[0] is N
1eed8 55 4c 4c 20 74 68 65 6e 20 6e 6f 20 0a 2a 2a 20  ULL then no .** 
1eed9 64 65 6c 65 74 69 6f 6e 20 6f 63 63 75 72 73 2e  deletion occurs.
1eeda 20 20 54 68 65 20 61 72 67 76 5b 31 5d 20 65 6c    The argv[1] el
1eedb 65 6d 65 6e 74 20 69 73 20 74 68 65 20 72 6f 77  ement is the row
1eedc 69 64 20 6f 66 20 74 68 65 20 6e 65 77 20 0a 2a  id of the new .*
1eedd 2a 20 72 6f 77 2e 20 20 54 68 69 73 20 63 61 6e  * row.  This can
1eede 20 62 65 20 4e 55 4c 4c 20 74 6f 20 68 61 76 65   be NULL to have
1eedf 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
1eee0 6c 65 20 73 65 6c 65 63 74 20 74 68 65 20 6e 65  le select the ne
1eee1 77 20 0a 2a 2a 20 72 6f 77 69 64 20 66 6f 72 20  w .** rowid for 
1eee2 69 74 73 65 6c 66 2e 20 20 54 68 65 20 73 75 62  itself.  The sub
1eee3 73 65 71 75 65 6e 74 20 65 6c 65 6d 65 6e 74 73  sequent elements
1eee4 20 69 6e 20 74 68 65 20 61 72 72 61 79 20 61 72   in the array ar
1eee5 65 20 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 73  e .** the values
1eee6 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
1eee7 68 65 20 6e 65 77 20 72 6f 77 2e 0a 2a 2a 0a 2a  he new row..**.*
1eee8 2a 20 49 66 20 50 32 3d 3d 31 20 74 68 65 6e 20  * If P2==1 then 
1eee9 6e 6f 20 69 6e 73 65 72 74 20 69 73 20 70 65 72  no insert is per
1eeea 66 6f 72 6d 65 64 2e 20 20 61 72 67 76 5b 30 5d  formed.  argv[0]
1eeeb 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66   is the rowid of
1eeec 0a 2a 2a 20 61 20 72 6f 77 20 74 6f 20 64 65 6c  .** a row to del
1eeed 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73  ete..**.** P1 is
1eeee 20 61 20 62 6f 6f 6c 65 61 6e 20 66 6c 61 67 2e   a boolean flag.
1eeef 20 49 66 20 69 74 20 69 73 20 73 65 74 20 74 6f   If it is set to
1eef0 20 74 72 75 65 20 61 6e 64 20 74 68 65 20 78 55   true and the xU
1eef1 70 64 61 74 65 20 63 61 6c 6c 0a 2a 2a 20 69 73  pdate call.** is
1eef2 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65   successful, the
1eef3 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75  n the value retu
1eef4 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  rned by sqlite3_
1eef5 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
1eef6 64 28 29 20 0a 2a 2a 20 69 73 20 73 65 74 20 74  d() .** is set t
1eef7 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  o the value of t
1eef8 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65  he rowid for the
1eef9 20 72 6f 77 20 6a 75 73 74 20 69 6e 73 65 72 74   row just insert
1eefa 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56  ed..*/.case OP_V
1eefb 55 70 64 61 74 65 3a 20 7b 0a 23 69 66 20 30 20  Update: {.#if 0 
1eefc 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1eefd 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1eefe 2e 63 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  .cl */.  sqlite3
1eeff 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20  _vtab *pVtab;.  
1ef00 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a  sqlite3_module *
1ef01 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 6e  pModule;.  int n
1ef02 41 72 67 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  Arg;.  int i;.  
1ef03 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 72 6f 77  sqlite_int64 row
1ef04 69 64 3b 0a 20 20 4d 65 6d 20 2a 2a 61 70 41 72  id;.  Mem **apAr
1ef05 67 3b 0a 20 20 4d 65 6d 20 2a 70 58 3b 0a 23 65  g;.  Mem *pX;.#e
1ef06 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1ef07 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1ef08 74 6f 20 75 2e 63 6c 20 2a 2f 0a 0a 20 20 75 2e  to u.cl */..  u.
1ef09 63 6c 2e 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e  cl.pVtab = pOp->
1ef0a 70 34 2e 70 56 74 61 62 2d 3e 70 56 74 61 62 3b  p4.pVtab->pVtab;
1ef0b 0a 20 20 75 2e 63 6c 2e 70 4d 6f 64 75 6c 65 20  .  u.cl.pModule 
1ef0c 3d 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  = (sqlite3_modul
1ef0d 65 20 2a 29 75 2e 63 6c 2e 70 56 74 61 62 2d 3e  e *)u.cl.pVtab->
1ef0e 70 4d 6f 64 75 6c 65 3b 0a 20 20 75 2e 63 6c 2e  pModule;.  u.cl.
1ef0f 6e 41 72 67 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a  nArg = pOp->p2;.
1ef10 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1ef11 34 74 79 70 65 3d 3d 50 34 5f 56 54 41 42 20 29  4type==P4_VTAB )
1ef12 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 75  ;.  if( ALWAYS(u
1ef13 2e 63 6c 2e 70 4d 6f 64 75 6c 65 2d 3e 78 55 70  .cl.pModule->xUp
1ef14 64 61 74 65 29 20 29 7b 0a 20 20 20 20 75 2e 63  date) ){.    u.c
1ef15 6c 2e 61 70 41 72 67 20 3d 20 70 2d 3e 61 70 41  l.apArg = p->apA
1ef16 72 67 3b 0a 20 20 20 20 75 2e 63 6c 2e 70 58 20  rg;.    u.cl.pX 
1ef17 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
1ef18 70 33 5d 3b 0a 20 20 20 20 66 6f 72 28 75 2e 63  p3];.    for(u.c
1ef19 6c 2e 69 3d 30 3b 20 75 2e 63 6c 2e 69 3c 75 2e  l.i=0; u.cl.i<u.
1ef1a 63 6c 2e 6e 41 72 67 3b 20 75 2e 63 6c 2e 69 2b  cl.nArg; u.cl.i+
1ef1b 2b 29 7b 0a 20 20 20 20 20 20 73 74 6f 72 65 54  +){.      storeT
1ef1c 79 70 65 49 6e 66 6f 28 75 2e 63 6c 2e 70 58 2c  ypeInfo(u.cl.pX,
1ef1d 20 30 29 3b 0a 20 20 20 20 20 20 75 2e 63 6c 2e   0);.      u.cl.
1ef1e 61 70 41 72 67 5b 75 2e 63 6c 2e 69 5d 20 3d 20  apArg[u.cl.i] = 
1ef1f 75 2e 63 6c 2e 70 58 3b 0a 20 20 20 20 20 20 75  u.cl.pX;.      u
1ef20 2e 63 6c 2e 70 58 2b 2b 3b 0a 20 20 20 20 7d 0a  .cl.pX++;.    }.
1ef21 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
1ef22 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67  afetyOff(db) ) g
1ef23 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1ef24 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 72 63 20  _misuse;.    rc 
1ef25 3d 20 75 2e 63 6c 2e 70 4d 6f 64 75 6c 65 2d 3e  = u.cl.pModule->
1ef26 78 55 70 64 61 74 65 28 75 2e 63 6c 2e 70 56 74  xUpdate(u.cl.pVt
1ef27 61 62 2c 20 75 2e 63 6c 2e 6e 41 72 67 2c 20 75  ab, u.cl.nArg, u
1ef28 2e 63 6c 2e 61 70 41 72 67 2c 20 26 75 2e 63 6c  .cl.apArg, &u.cl
1ef29 2e 72 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c  .rowid);.    sql
1ef2a 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1ef2b 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  ->zErrMsg);.    
1ef2c 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63  p->zErrMsg = u.c
1ef2d 6c 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67  l.pVtab->zErrMsg
1ef2e 3b 0a 20 20 20 20 75 2e 63 6c 2e 70 56 74 61 62  ;.    u.cl.pVtab
1ef2f 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20  ->zErrMsg = 0;. 
1ef30 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61     if( sqlite3Sa
1ef31 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74  fetyOn(db) ) got
1ef32 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d  o abort_due_to_m
1ef33 69 73 75 73 65 3b 0a 20 20 20 20 69 66 28 20 72  isuse;.    if( r
1ef34 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
1ef35 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 20  pOp->p1 ){.     
1ef36 20 61 73 73 65 72 74 28 20 75 2e 63 6c 2e 6e 41   assert( u.cl.nA
1ef37 72 67 3e 31 20 26 26 20 75 2e 63 6c 2e 61 70 41  rg>1 && u.cl.apA
1ef38 72 67 5b 30 5d 20 26 26 20 28 75 2e 63 6c 2e 61  rg[0] && (u.cl.a
1ef39 70 41 72 67 5b 30 5d 2d 3e 66 6c 61 67 73 26 4d  pArg[0]->flags&M
1ef3a 45 4d 5f 4e 75 6c 6c 29 20 29 3b 0a 20 20 20 20  EM_Null) );.    
1ef3b 20 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20    db->lastRowid 
1ef3c 3d 20 75 2e 63 6c 2e 72 6f 77 69 64 3b 0a 20 20  = u.cl.rowid;.  
1ef3d 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 43 68 61 6e    }.    p->nChan
1ef3e 67 65 2b 2b 3b 0a 20 20 7d 0a 20 20 62 72 65 61  ge++;.  }.  brea
1ef3f 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  k;.}.#endif /* S
1ef40 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1ef41 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e  ALTABLE */..#ifn
1ef42 64 65 66 20 20 53 51 4c 49 54 45 5f 4f 4d 49 54  def  SQLITE_OMIT
1ef43 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 2f  _PAGER_PRAGMAS./
1ef44 2a 20 4f 70 63 6f 64 65 3a 20 50 61 67 65 63 6f  * Opcode: Pageco
1ef45 75 6e 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  unt P1 P2 * * *.
1ef46 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20  **.** Write the 
1ef47 63 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f  current number o
1ef48 66 20 70 61 67 65 73 20 69 6e 20 64 61 74 61 62  f pages in datab
1ef49 61 73 65 20 50 31 20 74 6f 20 6d 65 6d 6f 72 79  ase P1 to memory
1ef4a 20 63 65 6c 6c 20 50 32 2e 0a 2a 2f 0a 63 61 73   cell P2..*/.cas
1ef4b 65 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 3a 20  e OP_Pagecount: 
1ef4c 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  {            /* 
1ef4d 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
1ef4e 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  */.#if 0  /* loc
1ef4f 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1ef50 65 64 20 69 6e 74 6f 20 75 2e 63 6d 20 2a 2f 0a  ed into u.cm */.
1ef51 20 20 69 6e 74 20 70 31 3b 0a 20 20 69 6e 74 20    int p1;.  int 
1ef52 6e 50 61 67 65 3b 0a 20 20 50 61 67 65 72 20 2a  nPage;.  Pager *
1ef53 70 50 61 67 65 72 3b 0a 23 65 6e 64 69 66 20 2f  pPager;.#endif /
1ef54 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1ef55 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63  s moved into u.c
1ef56 6d 20 2a 2f 0a 0a 20 20 75 2e 63 6d 2e 70 31 20  m */..  u.cm.p1 
1ef57 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 63  = pOp->p1;.  u.c
1ef58 6d 2e 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74  m.pPager = sqlit
1ef59 65 33 42 74 72 65 65 50 61 67 65 72 28 64 62 2d  e3BtreePager(db-
1ef5a 3e 61 44 62 5b 75 2e 63 6d 2e 70 31 5d 2e 70 42  >aDb[u.cm.p1].pB
1ef5b 74 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  t);.  rc = sqlit
1ef5c 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
1ef5d 28 75 2e 63 6d 2e 70 50 61 67 65 72 2c 20 26 75  (u.cm.pPager, &u
1ef5e 2e 63 6d 2e 6e 50 61 67 65 29 3b 0a 20 20 2f 2a  .cm.nPage);.  /*
1ef5f 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 20 69 73   OP_Pagecount is
1ef60 20 61 6c 77 61 79 73 20 63 61 6c 6c 65 64 20 66   always called f
1ef61 72 6f 6d 20 77 69 74 68 69 6e 20 61 20 72 65 61  rom within a rea
1ef62 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20  d transaction.  
1ef63 54 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 63 6f  The.  ** page co
1ef64 75 6e 74 20 68 61 73 20 61 6c 72 65 61 64 79 20  unt has already 
1ef65 62 65 65 6e 20 73 75 63 63 65 73 73 66 75 6c 6c  been successfull
1ef66 79 20 72 65 61 64 20 61 6e 64 20 63 61 63 68 65  y read and cache
1ef67 64 2e 20 20 53 6f 20 74 68 65 0a 20 20 2a 2a 20  d.  So the.  ** 
1ef68 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
1ef69 63 6f 75 6e 74 28 29 20 63 61 6c 6c 20 61 62 6f  count() call abo
1ef6a 76 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20  ve cannot fail. 
1ef6b 2a 2f 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28  */.  if( ALWAYS(
1ef6c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29  rc==SQLITE_OK) )
1ef6d 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67  {.    pOut->flag
1ef6e 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20  s = MEM_Int;.   
1ef6f 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 63   pOut->u.i = u.c
1ef70 6d 2e 6e 50 61 67 65 3b 0a 20 20 7d 0a 20 20 62  m.nPage;.  }.  b
1ef71 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  reak;.}.#endif..
1ef72 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1ef73 4d 49 54 5f 54 52 41 43 45 0a 2f 2a 20 4f 70 63  MIT_TRACE./* Opc
1ef74 6f 64 65 3a 20 54 72 61 63 65 20 2a 20 2a 20 2a  ode: Trace * * *
1ef75 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74   P4 *.**.** If t
1ef76 72 61 63 69 6e 67 20 69 73 20 65 6e 61 62 6c 65  racing is enable
1ef77 64 20 28 62 79 20 74 68 65 20 73 71 6c 69 74 65  d (by the sqlite
1ef78 33 5f 74 72 61 63 65 28 29 29 20 69 6e 74 65 72  3_trace()) inter
1ef79 66 61 63 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68  face, then.** th
1ef7a 65 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 63  e UTF-8 string c
1ef7b 6f 6e 74 61 69 6e 65 64 20 69 6e 20 50 34 20 69  ontained in P4 i
1ef7c 73 20 65 6d 69 74 74 65 64 20 6f 6e 20 74 68 65  s emitted on the
1ef7d 20 74 72 61 63 65 20 63 61 6c 6c 62 61 63 6b 2e   trace callback.
1ef7e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 72 61 63  .*/.case OP_Trac
1ef7f 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c  e: {.#if 0  /* l
1ef80 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1ef81 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6e 20 2a  oved into u.cn *
1ef82 2f 0a 20 20 63 68 61 72 20 2a 7a 54 72 61 63 65  /.  char *zTrace
1ef83 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1ef84 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1ef85 64 20 69 6e 74 6f 20 75 2e 63 6e 20 2a 2f 0a 0a  d into u.cn */..
1ef86 20 20 75 2e 63 6e 2e 7a 54 72 61 63 65 20 3d 20    u.cn.zTrace = 
1ef87 28 70 4f 70 2d 3e 70 34 2e 7a 20 3f 20 70 4f 70  (pOp->p4.z ? pOp
1ef88 2d 3e 70 34 2e 7a 20 3a 20 70 2d 3e 7a 53 71 6c  ->p4.z : p->zSql
1ef89 29 3b 0a 20 20 69 66 28 20 75 2e 63 6e 2e 7a 54  );.  if( u.cn.zT
1ef8a 72 61 63 65 20 29 7b 0a 20 20 20 20 69 66 28 20  race ){.    if( 
1ef8b 64 62 2d 3e 78 54 72 61 63 65 20 29 7b 0a 20 20  db->xTrace ){.  
1ef8c 20 20 20 20 64 62 2d 3e 78 54 72 61 63 65 28 64      db->xTrace(d
1ef8d 62 2d 3e 70 54 72 61 63 65 41 72 67 2c 20 75 2e  b->pTraceArg, u.
1ef8e 63 6e 2e 7a 54 72 61 63 65 29 3b 0a 20 20 20 20  cn.zTrace);.    
1ef8f 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  }.#ifdef SQLITE_
1ef90 44 45 42 55 47 0a 20 20 20 20 69 66 28 20 28 64  DEBUG.    if( (d
1ef91 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
1ef92 45 5f 53 71 6c 54 72 61 63 65 29 21 3d 30 20 29  E_SqlTrace)!=0 )
1ef93 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  {.      sqlite3D
1ef94 65 62 75 67 50 72 69 6e 74 66 28 22 53 51 4c 2d  ebugPrintf("SQL-
1ef95 74 72 61 63 65 3a 20 25 73 5c 6e 22 2c 20 75 2e  trace: %s\n", u.
1ef96 63 6e 2e 7a 54 72 61 63 65 29 3b 0a 20 20 20 20  cn.zTrace);.    
1ef97 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1ef98 54 45 5f 44 45 42 55 47 20 2a 2f 0a 20 20 7d 0a  TE_DEBUG */.  }.
1ef99 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69    break;.}.#endi
1ef9a 66 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e  f.../* Opcode: N
1ef9b 6f 6f 70 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a  oop * * * * *.**
1ef9c 0a 2a 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20  .** Do nothing. 
1ef9d 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   This instructio
1ef9e 6e 20 69 73 20 6f 66 74 65 6e 20 75 73 65 66 75  n is often usefu
1ef9f 6c 20 61 73 20 61 20 6a 75 6d 70 0a 2a 2a 20 64  l as a jump.** d
1efa0 65 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f  estination..*/./
1efa1 2a 0a 2a 2a 20 54 68 65 20 6d 61 67 69 63 20 45  *.** The magic E
1efa2 78 70 6c 61 69 6e 20 6f 70 63 6f 64 65 20 61 72  xplain opcode ar
1efa3 65 20 6f 6e 6c 79 20 69 6e 73 65 72 74 65 64 20  e only inserted 
1efa4 77 68 65 6e 20 65 78 70 6c 61 69 6e 3d 3d 32 20  when explain==2 
1efa5 28 77 68 69 63 68 0a 2a 2a 20 69 73 20 74 6f 20  (which.** is to 
1efa6 73 61 79 20 77 68 65 6e 20 74 68 65 20 45 58 50  say when the EXP
1efa7 4c 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 20  LAIN QUERY PLAN 
1efa8 73 79 6e 74 61 78 20 69 73 20 75 73 65 64 2e 29  syntax is used.)
1efa9 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20  .** This opcode 
1efaa 72 65 63 6f 72 64 73 20 69 6e 66 6f 72 6d 61 74  records informat
1efab 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 6f 70 74  ion from the opt
1efac 69 6d 69 7a 65 72 2e 20 20 49 74 20 69 73 20 74  imizer.  It is t
1efad 68 65 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61  he.** the same a
1efae 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 54 68 69 73  s a no-op.  This
1efaf 20 6f 70 63 6f 64 65 73 6e 65 76 65 72 20 61 70   opcodesnever ap
1efb0 70 65 61 72 73 20 69 6e 20 61 20 72 65 61 6c 20  pears in a real 
1efb1 56 4d 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 64  VM program..*/.d
1efb2 65 66 61 75 6c 74 3a 20 7b 20 20 20 20 20 20 20  efault: {       
1efb3 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 72 65     /* This is re
1efb4 61 6c 6c 79 20 4f 50 5f 4e 6f 6f 70 20 61 6e 64  ally OP_Noop and
1efb5 20 4f 50 5f 45 78 70 6c 61 69 6e 20 2a 2f 0a 20   OP_Explain */. 
1efb6 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a   break;.}../****
1efb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1efb8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1efb9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1efba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1efbb 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65  *********.** The
1efbc 20 63 61 73 65 73 20 6f 66 20 74 68 65 20 73 77   cases of the sw
1efbd 69 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 61  itch statement a
1efbe 62 6f 76 65 20 74 68 69 73 20 6c 69 6e 65 20 73  bove this line s
1efbf 68 6f 75 6c 64 20 61 6c 6c 20 62 65 20 69 6e 64  hould all be ind
1efc0 65 6e 74 65 64 0a 2a 2a 20 62 79 20 36 20 73 70  ented.** by 6 sp
1efc1 61 63 65 73 2e 20 20 42 75 74 20 74 68 65 20 6c  aces.  But the l
1efc2 65 66 74 2d 6d 6f 73 74 20 36 20 73 70 61 63 65  eft-most 6 space
1efc3 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 6d 6f  s have been remo
1efc4 76 65 64 20 74 6f 20 69 6d 70 72 6f 76 65 20 74  ved to improve t
1efc5 68 65 0a 2a 2a 20 72 65 61 64 61 62 69 6c 69 74  he.** readabilit
1efc6 79 2e 20 20 46 72 6f 6d 20 74 68 69 73 20 70 6f  y.  From this po
1efc7 69 6e 74 20 6f 6e 20 64 6f 77 6e 2c 20 74 68 65  int on down, the
1efc8 20 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 61 74   normal indentat
1efc9 69 6f 6e 20 72 75 6c 65 73 20 61 72 65 0a 2a 2a  ion rules are.**
1efca 20 72 65 73 74 6f 72 65 64 2e 0a 2a 2a 2a 2a 2a   restored..*****
1efcb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1efcc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1efcd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1efce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1efcf 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 20 20 20 20 7d 0a  ********/.    }.
1efd0 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f  .#ifdef VDBE_PRO
1efd1 46 49 4c 45 0a 20 20 20 20 7b 0a 20 20 20 20 20  FILE.    {.     
1efd2 20 75 36 34 20 65 6c 61 70 73 65 64 20 3d 20 73   u64 elapsed = s
1efd3 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 2d  qlite3Hwtime() -
1efd4 20 73 74 61 72 74 3b 0a 20 20 20 20 20 20 70 4f   start;.      pO
1efd5 70 2d 3e 63 79 63 6c 65 73 20 2b 3d 20 65 6c 61  p->cycles += ela
1efd6 70 73 65 64 3b 0a 20 20 20 20 20 20 70 4f 70 2d  psed;.      pOp-
1efd7 3e 63 6e 74 2b 2b 3b 0a 23 69 66 20 30 0a 20 20  >cnt++;.#if 0.  
1efd8 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74        fprintf(st
1efd9 64 6f 75 74 2c 20 22 25 31 30 6c 6c 75 20 22 2c  dout, "%10llu ",
1efda 20 65 6c 61 70 73 65 64 29 3b 0a 20 20 20 20 20   elapsed);.     
1efdb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72     sqlite3VdbePr
1efdc 69 6e 74 4f 70 28 73 74 64 6f 75 74 2c 20 6f 72  intOp(stdout, or
1efdd 69 67 50 63 2c 20 26 70 2d 3e 61 4f 70 5b 6f 72  igPc, &p->aOp[or
1efde 69 67 50 63 5d 29 3b 0a 23 65 6e 64 69 66 0a 20  igPc]);.#endif. 
1efdf 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
1efe0 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   /* The followin
1efe1 67 20 63 6f 64 65 20 61 64 64 73 20 6e 6f 74 68  g code adds noth
1efe2 69 6e 67 20 74 6f 20 74 68 65 20 61 63 74 75 61  ing to the actua
1efe3 6c 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 0a  l functionality.
1efe4 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 72      ** of the pr
1efe5 6f 67 72 61 6d 2e 20 20 49 74 20 69 73 20 6f 6e  ogram.  It is on
1efe6 6c 79 20 68 65 72 65 20 66 6f 72 20 74 65 73 74  ly here for test
1efe7 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e  ing and debuggin
1efe8 67 2e 0a 20 20 20 20 2a 2a 20 4f 6e 20 74 68 65  g..    ** On the
1efe9 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 74 20   other hand, it 
1efea 64 6f 65 73 20 62 75 72 6e 20 43 50 55 20 63 79  does burn CPU cy
1efeb 63 6c 65 73 20 65 76 65 72 79 20 74 69 6d 65 20  cles every time 
1efec 74 68 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20 74  through.    ** t
1efed 68 65 20 65 76 61 6c 75 61 74 6f 72 20 6c 6f 6f  he evaluator loo
1efee 70 2e 20 20 53 6f 20 77 65 20 63 61 6e 20 6c 65  p.  So we can le
1efef 61 76 65 20 69 74 20 6f 75 74 20 77 68 65 6e 20  ave it out when 
1eff0 4e 44 45 42 55 47 20 69 73 20 64 65 66 69 6e 65  NDEBUG is define
1eff1 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65  d..    */.#ifnde
1eff2 66 20 4e 44 45 42 55 47 0a 20 20 20 20 61 73 73  f NDEBUG.    ass
1eff3 65 72 74 28 20 70 63 3e 3d 2d 31 20 26 26 20 70  ert( pc>=-1 && p
1eff4 63 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 0a 23 69 66  c<p->nOp );..#if
1eff5 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
1eff6 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63  .    if( p->trac
1eff7 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72  e ){.      if( r
1eff8 63 21 3d 30 20 29 20 66 70 72 69 6e 74 66 28 70  c!=0 ) fprintf(p
1eff9 2d 3e 74 72 61 63 65 2c 22 72 63 3d 25 64 5c 6e  ->trace,"rc=%d\n
1effa 22 2c 72 63 29 3b 0a 20 20 20 20 20 20 69 66 28  ",rc);.      if(
1effb 20 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50   opProperty & OP
1effc 46 4c 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45  FLG_OUT2_PRERELE
1effd 41 53 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ASE ){.        r
1effe 65 67 69 73 74 65 72 54 72 61 63 65 28 70 2d 3e  egisterTrace(p->
1efff 74 72 61 63 65 2c 20 70 4f 70 2d 3e 70 32 2c 20  trace, pOp->p2, 
1f000 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20  pOut);.      }. 
1f001 20 20 20 20 20 69 66 28 20 6f 70 50 72 6f 70 65       if( opPrope
1f002 72 74 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33  rty & OPFLG_OUT3
1f003 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 67 69   ){.        regi
1f004 73 74 65 72 54 72 61 63 65 28 70 2d 3e 74 72 61  sterTrace(p->tra
1f005 63 65 2c 20 70 4f 70 2d 3e 70 33 2c 20 70 4f 75  ce, pOp->p3, pOu
1f006 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  t);.      }.    
1f007 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c  }.#endif  /* SQL
1f008 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 23 65 6e  ITE_DEBUG */.#en
1f009 64 69 66 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a  dif  /* NDEBUG *
1f00a 2f 0a 20 20 7d 20 20 2f 2a 20 54 68 65 20 65 6e  /.  }  /* The en
1f00b 64 20 6f 66 20 74 68 65 20 66 6f 72 28 3b 3b 29  d of the for(;;)
1f00c 20 6c 6f 6f 70 20 74 68 65 20 6c 6f 6f 70 73 20   loop the loops 
1f00d 74 68 72 6f 75 67 68 20 6f 70 63 6f 64 65 73 20  through opcodes 
1f00e 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 72  */..  /* If we r
1f00f 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c  each this point,
1f010 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 65   it means that e
1f011 78 65 63 75 74 69 6f 6e 20 69 73 20 66 69 6e 69  xecution is fini
1f012 73 68 65 64 20 77 69 74 68 0a 20 20 2a 2a 20 61  shed with.  ** a
1f013 6e 20 65 72 72 6f 72 20 6f 66 20 73 6f 6d 65 20  n error of some 
1f014 6b 69 6e 64 2e 0a 20 20 2a 2f 0a 76 64 62 65 5f  kind..  */.vdbe_
1f015 65 72 72 6f 72 5f 68 61 6c 74 3a 0a 20 20 61 73  error_halt:.  as
1f016 73 65 72 74 28 20 72 63 20 29 3b 0a 20 20 70 2d  sert( rc );.  p-
1f017 3e 72 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69  >rc = rc;.  sqli
1f018 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a  te3VdbeHalt(p);.
1f019 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1f01a 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 64  _IOERR_NOMEM ) d
1f01b 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1f01c 3d 20 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49  = 1;.  rc = SQLI
1f01d 54 45 5f 45 52 52 4f 52 3b 0a 0a 20 20 2f 2a 20  TE_ERROR;..  /* 
1f01e 54 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79  This is the only
1f01f 20 77 61 79 20 6f 75 74 20 6f 66 20 74 68 69 73   way out of this
1f020 20 70 72 6f 63 65 64 75 72 65 2e 20 20 57 65 20   procedure.  We 
1f021 68 61 76 65 20 74 6f 0a 20 20 2a 2a 20 72 65 6c  have to.  ** rel
1f022 65 61 73 65 20 74 68 65 20 6d 75 74 65 78 65 73  ease the mutexes
1f023 20 6f 6e 20 62 74 72 65 65 73 20 74 68 61 74 20   on btrees that 
1f024 77 65 72 65 20 61 63 71 75 69 72 65 64 20 61 74  were acquired at
1f025 20 74 68 65 0a 20 20 2a 2a 20 74 6f 70 2e 20 2a   the.  ** top. *
1f026 2f 0a 76 64 62 65 5f 72 65 74 75 72 6e 3a 0a 20  /.vdbe_return:. 
1f027 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74   sqlite3BtreeMut
1f028 65 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d  exArrayLeave(&p-
1f029 3e 61 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  >aMutex);.  retu
1f02a 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 4a 75 6d  rn rc;..  /* Jum
1f02b 70 20 74 6f 20 68 65 72 65 20 69 66 20 61 20 73  p to here if a s
1f02c 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 6c 61  tring or blob la
1f02d 72 67 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45  rger than SQLITE
1f02e 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a 20 20 2a 2a  _MAX_LENGTH.  **
1f02f 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e   is encountered.
1f030 0a 20 20 2a 2f 0a 74 6f 6f 5f 62 69 67 3a 0a 20  .  */.too_big:. 
1f031 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
1f032 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64  g(&p->zErrMsg, d
1f033 62 2c 20 22 73 74 72 69 6e 67 20 6f 72 20 62 6c  b, "string or bl
1f034 6f 62 20 74 6f 6f 20 62 69 67 22 29 3b 0a 20 20  ob too big");.  
1f035 72 63 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42  rc = SQLITE_TOOB
1f036 49 47 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f  IG;.  goto vdbe_
1f037 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f  error_halt;..  /
1f038 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69  * Jump to here i
1f039 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  f a malloc() fai
1f03a 6c 73 2e 0a 20 20 2a 2f 0a 6e 6f 5f 6d 65 6d 3a  ls..  */.no_mem:
1f03b 0a 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  .  db->mallocFai
1f03c 6c 65 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74  led = 1;.  sqlit
1f03d 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
1f03e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 6f 75  zErrMsg, db, "ou
1f03f 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20  t of memory");. 
1f040 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
1f041 45 4d 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f  EM;.  goto vdbe_
1f042 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f  error_halt;..  /
1f043 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 66  * Jump to here f
1f044 6f 72 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 53  or an SQLITE_MIS
1f045 55 53 45 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a  USE error..  */.
1f046 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73  abort_due_to_mis
1f047 75 73 65 3a 0a 20 20 72 63 20 3d 20 53 51 4c 49  use:.  rc = SQLI
1f048 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 2f 2a 20  TE_MISUSE;.  /* 
1f049 46 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 61  Fall thru into a
1f04a 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1f04b 72 20 2a 2f 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20  r */..  /* Jump 
1f04c 74 6f 20 68 65 72 65 20 66 6f 72 20 61 6e 79 20  to here for any 
1f04d 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 66 61  other kind of fa
1f04e 74 61 6c 20 65 72 72 6f 72 2e 20 20 54 68 65 20  tal error.  The 
1f04f 22 72 63 22 20 76 61 72 69 61 62 6c 65 0a 20 20  "rc" variable.  
1f050 2a 2a 20 73 68 6f 75 6c 64 20 68 6f 6c 64 20 74  ** should hold t
1f051 68 65 20 65 72 72 6f 72 20 6e 75 6d 62 65 72 2e  he error number.
1f052 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f  .  */.abort_due_
1f053 74 6f 5f 65 72 72 6f 72 3a 0a 20 20 61 73 73 65  to_error:.  asse
1f054 72 74 28 20 70 2d 3e 7a 45 72 72 4d 73 67 3d 3d  rt( p->zErrMsg==
1f055 30 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d  0 );.  if( db->m
1f056 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 63  allocFailed ) rc
1f057 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
1f058 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
1f059 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b  E_IOERR_NOMEM ){
1f05a 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
1f05b 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
1f05c 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c  g, db, "%s", sql
1f05d 69 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b  ite3ErrStr(rc));
1f05e 0a 20 20 7d 0a 20 20 67 6f 74 6f 20 76 64 62 65  .  }.  goto vdbe
1f05f 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20  _error_halt;..  
1f060 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20  /* Jump to here 
1f061 69 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  if the sqlite3_i
1f062 6e 74 65 72 72 75 70 74 28 29 20 41 50 49 20 73  nterrupt() API s
1f063 65 74 73 20 74 68 65 20 69 6e 74 65 72 72 75 70  ets the interrup
1f064 74 0a 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 2a  t.  ** flag..  *
1f065 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69  /.abort_due_to_i
1f066 6e 74 65 72 72 75 70 74 3a 0a 20 20 61 73 73 65  nterrupt:.  asse
1f067 72 74 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74  rt( db->u1.isInt
1f068 65 72 72 75 70 74 65 64 20 29 3b 0a 20 20 72 63  errupted );.  rc
1f069 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52   = SQLITE_INTERR
1f06a 55 50 54 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72  UPT;.  p->rc = r
1f06b 63 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 74 53  c;.  sqlite3SetS
1f06c 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
1f06d 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c  g, db, "%s", sql
1f06e 69 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b  ite3ErrStr(rc));
1f06f 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72  .  goto vdbe_err
1f070 6f 72 5f 68 61 6c 74 3b 0a 7d 0a 0a 2f 2a 2a 2a  or_halt;.}../***
1f071 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
1f072 6f 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a  of vdbe.c ******
1f073 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f074 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f075 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
1f076 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
1f077 6e 20 66 69 6c 65 20 76 64 62 65 62 6c 6f 62 2e  n file vdbeblob.
1f078 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1f079 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f07a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
1f07b 2a 20 32 30 30 37 20 4d 61 79 20 31 0a 2a 2a 0a  * 2007 May 1.**.
1f07c 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
1f07d 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
1f07e 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
1f07f 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
1f080 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
1f081 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
1f082 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
1f083 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
1f084 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
1f085 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1f086 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
1f087 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
1f088 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
1f089 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1f08a 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
1f08b 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
1f08c 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
1f08d 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
1f08e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f08f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f091 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
1f092 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
1f093 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74  ains code used t
1f094 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 69 6e 63 72  o implement incr
1f095 65 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f  emental BLOB I/O
1f096 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62  ..**.** $Id: vdb
1f097 65 62 6c 6f 62 2e 63 2c 76 20 31 2e 33 35 20 32  eblob.c,v 1.35 2
1f098 30 30 39 2f 30 37 2f 30 32 20 30 37 3a 34 37 3a  009/07/02 07:47:
1f099 33 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45  33 danielk1977 E
1f09a 78 70 20 24 0a 2a 2f 0a 0a 0a 23 69 66 6e 64 65  xp $.*/...#ifnde
1f09b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
1f09c 43 52 42 4c 4f 42 0a 0a 2f 2a 0a 2a 2a 20 56 61  CRBLOB../*.** Va
1f09d 6c 69 64 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  lid sqlite3_blob
1f09e 2a 20 68 61 6e 64 6c 65 73 20 70 6f 69 6e 74 20  * handles point 
1f09f 74 6f 20 49 6e 63 72 62 6c 6f 62 20 73 74 72 75  to Incrblob stru
1f0a0 63 74 75 72 65 73 2e 0a 2a 2f 0a 74 79 70 65 64  ctures..*/.typed
1f0a1 65 66 20 73 74 72 75 63 74 20 49 6e 63 72 62 6c  ef struct Incrbl
1f0a2 6f 62 20 49 6e 63 72 62 6c 6f 62 3b 0a 73 74 72  ob Incrblob;.str
1f0a3 75 63 74 20 49 6e 63 72 62 6c 6f 62 20 7b 0a 20  uct Incrblob {. 
1f0a4 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20   int flags;     
1f0a5 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79           /* Copy
1f0a6 20 6f 66 20 22 66 6c 61 67 73 22 20 70 61 73 73   of "flags" pass
1f0a7 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 6c  ed to sqlite3_bl
1f0a8 6f 62 5f 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69  ob_open() */.  i
1f0a9 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20  nt nByte;       
1f0aa 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
1f0ab 66 20 6f 70 65 6e 20 62 6c 6f 62 2c 20 69 6e 20  f open blob, in 
1f0ac 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69  bytes */.  int i
1f0ad 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20  Offset;         
1f0ae 20 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65     /* Byte offse
1f0af 74 20 6f 66 20 62 6c 6f 62 20 69 6e 20 63 75 72  t of blob in cur
1f0b0 73 6f 72 20 64 61 74 61 20 2a 2f 0a 20 20 42 74  sor data */.  Bt
1f0b1 43 75 72 73 6f 72 20 2a 70 43 73 72 3b 20 20 20  Cursor *pCsr;   
1f0b2 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
1f0b3 70 6f 69 6e 74 69 6e 67 20 61 74 20 62 6c 6f 62  pointing at blob
1f0b4 20 72 6f 77 20 2a 2f 0a 20 20 73 71 6c 69 74 65   row */.  sqlite
1f0b5 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20  3_stmt *pStmt;  
1f0b6 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 68    /* Statement h
1f0b7 6f 6c 64 69 6e 67 20 63 75 72 73 6f 72 20 6f 70  olding cursor op
1f0b8 65 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  en */.  sqlite3 
1f0b9 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  *db;            
1f0ba 2f 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 65  /* The associate
1f0bb 64 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 7d 3b  d database */.};
1f0bc 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 62  ../*.** Open a b
1f0bd 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53  lob handle..*/.S
1f0be 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1f0bf 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28  lite3_blob_open(
1f0c0 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20  .  sqlite3* db, 
1f0c1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1f0c2 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
1f0c3 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  ction */.  const
1f0c4 20 63 68 61 72 20 2a 7a 44 62 2c 20 20 20 20 20   char *zDb,     
1f0c5 20 20 20 2f 2a 20 54 68 65 20 61 74 74 61 63 68     /* The attach
1f0c6 65 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74  ed database cont
1f0c7 61 69 6e 69 6e 67 20 74 68 65 20 62 6c 6f 62 20  aining the blob 
1f0c8 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1f0c9 2a 7a 54 61 62 6c 65 2c 20 20 20 20 20 2f 2a 20  *zTable,     /* 
1f0ca 54 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69  The table contai
1f0cb 6e 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f  ning the blob */
1f0cc 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1f0cd 43 6f 6c 75 6d 6e 2c 20 20 20 20 2f 2a 20 54 68  Column,    /* Th
1f0ce 65 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e  e column contain
1f0cf 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a  ing the blob */.
1f0d0 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
1f0d1 52 6f 77 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  Row,      /* The
1f0d2 20 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20   row containing 
1f0d3 74 68 65 20 67 6c 6f 62 20 2a 2f 0a 20 20 69 6e  the glob */.  in
1f0d4 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20  t flags,        
1f0d5 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 2d 3e        /* True ->
1f0d6 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65   read/write acce
1f0d7 73 73 2c 20 66 61 6c 73 65 20 2d 3e 20 72 65 61  ss, false -> rea
1f0d8 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 71 6c 69  d-only */.  sqli
1f0d9 74 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f  te3_blob **ppBlo
1f0da 62 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f  b   /* Handle fo
1f0db 72 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20  r accessing the 
1f0dc 62 6c 6f 62 20 72 65 74 75 72 6e 65 64 20 68 65  blob returned he
1f0dd 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e  re */.){.  int n
1f0de 41 74 74 65 6d 70 74 20 3d 20 30 3b 0a 20 20 69  Attempt = 0;.  i
1f0df 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20  nt iCol;        
1f0e0 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
1f0e1 6f 66 20 7a 43 6f 6c 75 6d 6e 20 69 6e 20 72 6f  of zColumn in ro
1f0e2 77 2d 72 65 63 6f 72 64 20 2a 2f 0a 0a 20 20 2f  w-record */..  /
1f0e3 2a 20 54 68 69 73 20 56 44 42 45 20 70 72 6f 67  * This VDBE prog
1f0e4 72 61 6d 20 73 65 65 6b 73 20 61 20 62 74 72 65  ram seeks a btre
1f0e5 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20  e cursor to the 
1f0e6 69 64 65 6e 74 69 66 69 65 64 20 0a 20 20 2a 2a  identified .  **
1f0e7 20 64 62 2f 74 61 62 6c 65 2f 72 6f 77 20 65 6e   db/table/row en
1f0e8 74 72 79 2e 20 54 68 65 20 72 65 61 73 6f 6e 20  try. The reason 
1f0e9 66 6f 72 20 75 73 69 6e 67 20 61 20 76 64 62 65  for using a vdbe
1f0ea 20 70 72 6f 67 72 61 6d 20 69 6e 73 74 65 61 64   program instead
1f0eb 0a 20 20 2a 2a 20 6f 66 20 77 72 69 74 69 6e 67  .  ** of writing
1f0ec 20 63 6f 64 65 20 74 6f 20 75 73 65 20 74 68 65   code to use the
1f0ed 20 62 2d 74 72 65 65 20 6c 61 79 65 72 20 64 69   b-tree layer di
1f0ee 72 65 63 74 6c 79 20 69 73 20 74 68 61 74 20 74  rectly is that t
1f0ef 68 65 0a 20 20 2a 2a 20 76 64 62 65 20 70 72 6f  he.  ** vdbe pro
1f0f0 67 72 61 6d 20 77 69 6c 6c 20 74 61 6b 65 20 61  gram will take a
1f0f1 64 76 61 6e 74 61 67 65 20 6f 66 20 74 68 65 20  dvantage of the 
1f0f2 76 61 72 69 6f 75 73 20 74 72 61 6e 73 61 63 74  various transact
1f0f3 69 6f 6e 2c 0a 20 20 2a 2a 20 6c 6f 63 6b 69 6e  ion,.  ** lockin
1f0f4 67 20 61 6e 64 20 65 72 72 6f 72 20 68 61 6e 64  g and error hand
1f0f5 6c 69 6e 67 20 69 6e 66 72 61 73 74 72 75 63 74  ling infrastruct
1f0f6 75 72 65 20 62 75 69 6c 74 20 69 6e 74 6f 20 74  ure built into t
1f0f7 68 65 20 76 64 62 65 2e 0a 20 20 2a 2a 0a 20 20  he vdbe..  **.  
1f0f8 2a 2a 20 41 66 74 65 72 20 73 65 65 6b 69 6e 67  ** After seeking
1f0f9 20 74 68 65 20 63 75 72 73 6f 72 2c 20 74 68 65   the cursor, the
1f0fa 20 76 64 62 65 20 65 78 65 63 75 74 65 73 20 61   vdbe executes a
1f0fb 6e 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2e 0a  n OP_ResultRow..
1f0fc 20 20 2a 2a 20 43 6f 64 65 20 65 78 74 65 72 6e    ** Code extern
1f0fd 61 6c 20 74 6f 20 74 68 65 20 56 64 62 65 20 74  al to the Vdbe t
1f0fe 68 65 6e 20 22 62 6f 72 72 6f 77 73 22 20 74 68  hen "borrows" th
1f0ff 65 20 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20  e b-tree cursor 
1f100 61 6e 64 0a 20 20 2a 2a 20 75 73 65 73 20 69 74  and.  ** uses it
1f101 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
1f102 65 20 62 6c 6f 62 5f 72 65 61 64 28 29 2c 20 62  e blob_read(), b
1f103 6c 6f 62 5f 77 72 69 74 65 28 29 20 61 6e 64 20  lob_write() and 
1f104 0a 20 20 2a 2a 20 62 6c 6f 62 5f 62 79 74 65 73  .  ** blob_bytes
1f105 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20  () functions..  
1f106 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69  **.  ** The sqli
1f107 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29  te3_blob_close()
1f108 20 66 75 6e 63 74 69 6f 6e 20 66 69 6e 61 6c 69   function finali
1f109 7a 65 73 20 74 68 65 20 76 64 62 65 20 70 72 6f  zes the vdbe pro
1f10a 67 72 61 6d 2c 0a 20 20 2a 2a 20 77 68 69 63 68  gram,.  ** which
1f10b 20 63 6c 6f 73 65 73 20 74 68 65 20 62 2d 74 72   closes the b-tr
1f10c 65 65 20 63 75 72 73 6f 72 20 61 6e 64 20 28 70  ee cursor and (p
1f10d 6f 73 73 69 62 6c 79 29 20 63 6f 6d 6d 69 74 73  ossibly) commits
1f10e 20 74 68 65 20 0a 20 20 2a 2a 20 74 72 61 6e 73   the .  ** trans
1f10f 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73  action..  */.  s
1f110 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65  tatic const Vdbe
1f111 4f 70 4c 69 73 74 20 6f 70 65 6e 42 6c 6f 62 5b  OpList openBlob[
1f112 5d 20 3d 20 7b 0a 20 20 20 20 7b 4f 50 5f 54 72  ] = {.    {OP_Tr
1f113 61 6e 73 61 63 74 69 6f 6e 2c 20 30 2c 20 30 2c  ansaction, 0, 0,
1f114 20 30 7d 2c 20 20 20 20 20 2f 2a 20 30 3a 20 53   0},     /* 0: S
1f115 74 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 69  tart a transacti
1f116 6f 6e 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 56 65  on */.    {OP_Ve
1f117 72 69 66 79 43 6f 6f 6b 69 65 2c 20 30 2c 20 30  rifyCookie, 0, 0
1f118 2c 20 30 7d 2c 20 20 20 20 2f 2a 20 31 3a 20 43  , 0},    /* 1: C
1f119 68 65 63 6b 20 74 68 65 20 73 63 68 65 6d 61 20  heck the schema 
1f11a 63 6f 6f 6b 69 65 20 2a 2f 0a 20 20 20 20 7b 4f  cookie */.    {O
1f11b 50 5f 54 61 62 6c 65 4c 6f 63 6b 2c 20 30 2c 20  P_TableLock, 0, 
1f11c 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20  0, 0},       /* 
1f11d 32 3a 20 41 63 71 75 69 72 65 20 61 20 72 65 61  2: Acquire a rea
1f11e 64 20 6f 72 20 77 72 69 74 65 20 6c 6f 63 6b 20  d or write lock 
1f11f 2a 2f 0a 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 6f  */..    /* One o
1f120 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
1f121 74 77 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 73  two instructions
1f122 20 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20   is replaced by 
1f123 61 6e 20 4f 50 5f 4e 6f 6f 70 2e 20 2a 2f 0a 20  an OP_Noop. */. 
1f124 20 20 20 7b 4f 50 5f 4f 70 65 6e 52 65 61 64 2c     {OP_OpenRead,
1f125 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20   0, 0, 0},      
1f126 20 20 2f 2a 20 33 3a 20 4f 70 65 6e 20 63 75 72    /* 3: Open cur
1f127 73 6f 72 20 30 20 66 6f 72 20 72 65 61 64 69 6e  sor 0 for readin
1f128 67 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4f 70 65  g */.    {OP_Ope
1f129 6e 57 72 69 74 65 2c 20 30 2c 20 30 2c 20 30 7d  nWrite, 0, 0, 0}
1f12a 2c 20 20 20 20 20 20 20 2f 2a 20 34 3a 20 4f 70  ,       /* 4: Op
1f12b 65 6e 20 63 75 72 73 6f 72 20 30 20 66 6f 72 20  en cursor 0 for 
1f12c 72 65 61 64 2f 77 72 69 74 65 20 2a 2f 0a 0a 20  read/write */.. 
1f12d 20 20 20 7b 4f 50 5f 56 61 72 69 61 62 6c 65 2c     {OP_Variable,
1f12e 20 31 2c 20 31 2c 20 31 7d 2c 20 20 20 20 20 20   1, 1, 1},      
1f12f 20 20 2f 2a 20 35 3a 20 50 75 73 68 20 74 68 65    /* 5: Push the
1f130 20 72 6f 77 69 64 20 74 6f 20 74 68 65 20 73 74   rowid to the st
1f131 61 63 6b 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4e  ack */.    {OP_N
1f132 6f 74 45 78 69 73 74 73 2c 20 30 2c 20 39 2c 20  otExists, 0, 9, 
1f133 31 7d 2c 20 20 20 20 20 20 20 2f 2a 20 36 3a 20  1},       /* 6: 
1f134 53 65 65 6b 20 74 68 65 20 63 75 72 73 6f 72 20  Seek the cursor 
1f135 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43 6f 6c 75 6d  */.    {OP_Colum
1f136 6e 2c 20 30 2c 20 30 2c 20 31 7d 2c 20 20 20 20  n, 0, 0, 1},    
1f137 20 20 20 20 20 20 2f 2a 20 37 20 20 2a 2f 0a 20        /* 7  */. 
1f138 20 20 20 7b 4f 50 5f 52 65 73 75 6c 74 52 6f 77     {OP_ResultRow
1f139 2c 20 31 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20  , 1, 0, 0},     
1f13a 20 20 2f 2a 20 38 20 20 2a 2f 0a 20 20 20 20 7b    /* 8  */.    {
1f13b 4f 50 5f 43 6c 6f 73 65 2c 20 30 2c 20 30 2c 20  OP_Close, 0, 0, 
1f13c 30 7d 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  0},           /*
1f13d 20 39 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 48   9  */.    {OP_H
1f13e 61 6c 74 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20  alt, 0, 0, 0},  
1f13f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 30 20            /* 10 
1f140 2a 2f 0a 20 20 7d 3b 0a 0a 20 20 56 64 62 65 20  */.  };..  Vdbe 
1f141 2a 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63  *v = 0;.  int rc
1f142 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1f143 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a  char *zErr = 0;.
1f144 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20    Table *pTab;. 
1f145 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a   Parse *pParse;.
1f146 0a 20 20 2a 70 70 42 6c 6f 62 20 3d 20 30 3b 0a  .  *ppBlob = 0;.
1f147 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1f148 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
1f149 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 73 71 6c  ;.  pParse = sql
1f14a 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61  ite3StackAllocRa
1f14b 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 50  w(db, sizeof(*pP
1f14c 61 72 73 65 29 29 3b 0a 20 20 69 66 28 20 70 50  arse));.  if( pP
1f14d 61 72 73 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72  arse==0 ){.    r
1f14e 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
1f14f 3b 0a 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f  ;.    goto blob_
1f150 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20  open_out;.  }.  
1f151 64 6f 20 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28  do {.    memset(
1f152 70 50 61 72 73 65 2c 20 30 2c 20 73 69 7a 65 6f  pParse, 0, sizeo
1f153 66 28 50 61 72 73 65 29 29 3b 0a 20 20 20 20 70  f(Parse));.    p
1f154 50 61 72 73 65 2d 3e 64 62 20 3d 20 64 62 3b 0a  Parse->db = db;.
1f155 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1f156 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a  SafetyOn(db) ){.
1f157 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
1f158 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20  ree(db, zErr);. 
1f159 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 63       sqlite3Stac
1f15a 6b 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65  kFree(db, pParse
1f15b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1f15c 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
1f15d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 72  >mutex);.      r
1f15e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
1f15f 55 53 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  USE;.    }..    
1f160 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
1f161 72 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 70 54  rAll(db);.    pT
1f162 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61  ab = sqlite3Loca
1f163 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  teTable(pParse, 
1f164 30 2c 20 7a 54 61 62 6c 65 2c 20 7a 44 62 29 3b  0, zTable, zDb);
1f165 0a 20 20 20 20 69 66 28 20 70 54 61 62 20 26 26  .    if( pTab &&
1f166 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
1f167 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d   ){.      pTab =
1f168 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   0;.      sqlite
1f169 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1f16a 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76  , "cannot open v
1f16b 69 72 74 75 61 6c 20 74 61 62 6c 65 3a 20 25 73  irtual table: %s
1f16c 22 2c 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20  ", zTable);.    
1f16d 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
1f16e 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 20 20 69  _OMIT_VIEW.    i
1f16f 66 28 20 70 54 61 62 20 26 26 20 70 54 61 62 2d  f( pTab && pTab-
1f170 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  >pSelect ){.    
1f171 20 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20    pTab = 0;.    
1f172 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1f173 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f  g(pParse, "canno
1f174 74 20 6f 70 65 6e 20 76 69 65 77 3a 20 25 73 22  t open view: %s"
1f175 2c 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d  , zTable);.    }
1f176 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20  .#endif.    if( 
1f177 21 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 69  !pTab ){.      i
1f178 66 28 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  f( pParse->zErrM
1f179 73 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  sg ){.        sq
1f17a 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1f17b 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 20 20 7a  zErr);.        z
1f17c 45 72 72 20 3d 20 70 50 61 72 73 65 2d 3e 7a 45  Err = pParse->zE
1f17d 72 72 4d 73 67 3b 0a 20 20 20 20 20 20 20 20 70  rrMsg;.        p
1f17e 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d  Parse->zErrMsg =
1f17f 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
1f180 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
1f181 52 4f 52 3b 0a 20 20 20 20 20 20 28 76 6f 69 64  ROR;.      (void
1f182 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66  )sqlite3SafetyOf
1f183 66 28 64 62 29 3b 0a 20 20 20 20 20 20 73 71 6c  f(db);.      sql
1f184 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c  ite3BtreeLeaveAl
1f185 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 67 6f 74  l(db);.      got
1f186 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b  o blob_open_out;
1f187 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4e  .    }..    /* N
1f188 6f 77 20 73 65 61 72 63 68 20 70 54 61 62 20 66  ow search pTab f
1f189 6f 72 20 74 68 65 20 65 78 61 63 74 20 63 6f 6c  or the exact col
1f18a 75 6d 6e 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28  umn. */.    for(
1f18b 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 20 3c 20 70  iCol=0; iCol < p
1f18c 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b  Tab->nCol; iCol+
1f18d 2b 29 20 7b 0a 20 20 20 20 20 20 69 66 28 20 73  +) {.      if( s
1f18e 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 54  qlite3StrICmp(pT
1f18f 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a  ab->aCol[iCol].z
1f190 4e 61 6d 65 2c 20 7a 43 6f 6c 75 6d 6e 29 3d 3d  Name, zColumn)==
1f191 30 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65  0 ){.        bre
1f192 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
1f193 7d 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d  }.    if( iCol==
1f194 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20  pTab->nCol ){.  
1f195 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1f196 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20  e(db, zErr);.   
1f197 20 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65     zErr = sqlite
1f198 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f  3MPrintf(db, "no
1f199 20 73 75 63 68 20 63 6f 6c 75 6d 6e 3a 20 5c 22   such column: \"
1f19a 25 73 5c 22 22 2c 20 7a 43 6f 6c 75 6d 6e 29 3b  %s\"", zColumn);
1f19b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
1f19c 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20  TE_ERROR;.      
1f19d 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
1f19e 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20  etyOff(db);.    
1f19f 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
1f1a0 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20  aveAll(db);.    
1f1a1 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e    goto blob_open
1f1a2 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  _out;.    }..   
1f1a3 20 2f 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65   /* If the value
1f1a4 20 69 73 20 62 65 69 6e 67 20 6f 70 65 6e 65 64   is being opened
1f1a5 20 66 6f 72 20 77 72 69 74 69 6e 67 2c 20 63 68   for writing, ch
1f1a6 65 63 6b 20 74 68 61 74 20 74 68 65 0a 20 20 20  eck that the.   
1f1a7 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f   ** column is no
1f1a8 74 20 69 6e 64 65 78 65 64 2e 20 49 74 20 69 73  t indexed. It is
1f1a9 20 61 67 61 69 6e 73 74 20 74 68 65 20 72 75 6c   against the rul
1f1aa 65 73 20 74 6f 20 6f 70 65 6e 20 61 6e 0a 20 20  es to open an.  
1f1ab 20 20 2a 2a 20 69 6e 64 65 78 65 64 20 63 6f 6c    ** indexed col
1f1ac 75 6d 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 2e  umn for writing.
1f1ad 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1f1ae 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 20 20 49  flags ){.      I
1f1af 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20  ndex *pIdx;.    
1f1b0 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d    for(pIdx=pTab-
1f1b1 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
1f1b2 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
1f1b3 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b  {.        int j;
1f1b4 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  .        for(j=0
1f1b5 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  ; j<pIdx->nColum
1f1b6 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  n; j++){.       
1f1b7 20 20 20 69 66 28 20 70 49 64 78 2d 3e 61 69 43     if( pIdx->aiC
1f1b8 6f 6c 75 6d 6e 5b 6a 5d 3d 3d 69 43 6f 6c 20 29  olumn[j]==iCol )
1f1b9 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  {.            sq
1f1ba 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1f1bb 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 20 20 20  zErr);.         
1f1bc 20 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65     zErr = sqlite
1f1bd 33 4d 50 72 69 6e 74 66 28 64 62 2c 0a 20 20 20  3MPrintf(db,.   
1f1be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f1bf 20 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f            "canno
1f1c0 74 20 6f 70 65 6e 20 69 6e 64 65 78 65 64 20 63  t open indexed c
1f1c1 6f 6c 75 6d 6e 20 66 6f 72 20 77 72 69 74 69 6e  olumn for writin
1f1c2 67 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  g");.           
1f1c3 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
1f1c4 4f 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  OR;.            
1f1c5 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
1f1c6 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20  etyOff(db);.    
1f1c7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42          sqlite3B
1f1c8 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29  treeLeaveAll(db)
1f1c9 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f  ;.            go
1f1ca 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74  to blob_open_out
1f1cb 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
1f1cc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1f1cd 20 20 20 20 7d 0a 0a 20 20 20 20 76 20 3d 20 73      }..    v = s
1f1ce 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 65  qlite3VdbeCreate
1f1cf 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 76 20  (db);.    if( v 
1f1d0 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62  ){.      int iDb
1f1d1 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
1f1d2 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62  ToIndex(db, pTab
1f1d3 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20  ->pSchema);.    
1f1d4 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1f1d5 4f 70 4c 69 73 74 28 76 2c 20 73 69 7a 65 6f 66  OpList(v, sizeof
1f1d6 28 6f 70 65 6e 42 6c 6f 62 29 2f 73 69 7a 65 6f  (openBlob)/sizeo
1f1d7 66 28 56 64 62 65 4f 70 4c 69 73 74 29 2c 20 6f  f(VdbeOpList), o
1f1d8 70 65 6e 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20  penBlob);.      
1f1d9 66 6c 61 67 73 20 3d 20 21 21 66 6c 61 67 73 3b  flags = !!flags;
1f1da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f1db 20 2f 2a 20 66 6c 61 67 73 20 3d 20 28 66 6c 61   /* flags = (fla
1f1dc 67 73 20 3f 20 31 20 3a 20 30 29 3b 20 2a 2f 0a  gs ? 1 : 0); */.
1f1dd 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67  .      /* Config
1f1de 75 72 65 20 74 68 65 20 4f 50 5f 54 72 61 6e 73  ure the OP_Trans
1f1df 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  action */.      
1f1e0 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1f1e1 65 50 31 28 76 2c 20 30 2c 20 69 44 62 29 3b 0a  eP1(v, 0, iDb);.
1f1e2 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f1e3 65 43 68 61 6e 67 65 50 32 28 76 2c 20 30 2c 20  eChangeP2(v, 0, 
1f1e4 66 6c 61 67 73 29 3b 0a 0a 20 20 20 20 20 20 2f  flags);..      /
1f1e5 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20  * Configure the 
1f1e6 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 20  OP_VerifyCookie 
1f1e7 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
1f1e8 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
1f1e9 31 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73  1, iDb);.      s
1f1ea 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1f1eb 50 32 28 76 2c 20 31 2c 20 70 54 61 62 2d 3e 70  P2(v, 1, pTab->p
1f1ec 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63  Schema->schema_c
1f1ed 6f 6f 6b 69 65 29 3b 0a 0a 20 20 20 20 20 20 2f  ookie);..      /
1f1ee 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 20 6d 75  * Make sure a mu
1f1ef 74 65 78 20 69 73 20 68 65 6c 64 20 6f 6e 20 74  tex is held on t
1f1f0 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 61  he table to be a
1f1f1 63 63 65 73 73 65 64 20 2a 2f 0a 20 20 20 20 20  ccessed */.     
1f1f2 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73   sqlite3VdbeUses
1f1f3 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 20 0a  Btree(v, iDb); .
1f1f4 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67  .      /* Config
1f1f5 75 72 65 20 74 68 65 20 4f 50 5f 54 61 62 6c 65  ure the OP_Table
1f1f6 4c 6f 63 6b 20 69 6e 73 74 72 75 63 74 69 6f 6e  Lock instruction
1f1f7 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
1f1f8 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c  3VdbeChangeP1(v,
1f1f9 20 32 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20   2, iDb);.      
1f1fa 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1f1fb 65 50 32 28 76 2c 20 32 2c 20 70 54 61 62 2d 3e  eP2(v, 2, pTab->
1f1fc 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c  tnum);.      sql
1f1fd 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33  ite3VdbeChangeP3
1f1fe 28 76 2c 20 32 2c 20 66 6c 61 67 73 29 3b 0a 20  (v, 2, flags);. 
1f1ff 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f200 43 68 61 6e 67 65 50 34 28 76 2c 20 32 2c 20 70  ChangeP4(v, 2, p
1f201 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 54  Tab->zName, P4_T
1f202 52 41 4e 53 49 45 4e 54 29 3b 0a 0a 20 20 20 20  RANSIENT);..    
1f203 20 20 2f 2a 20 52 65 6d 6f 76 65 20 65 69 74 68    /* Remove eith
1f204 65 72 20 74 68 65 20 4f 50 5f 4f 70 65 6e 57 72  er the OP_OpenWr
1f205 69 74 65 20 6f 72 20 4f 70 65 6e 52 65 61 64 2e  ite or OpenRead.
1f206 20 53 65 74 20 74 68 65 20 50 32 20 0a 20 20 20   Set the P2 .   
1f207 20 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 20     ** parameter 
1f208 6f 66 20 74 68 65 20 6f 74 68 65 72 20 74 6f 20  of the other to 
1f209 70 54 61 62 2d 3e 74 6e 75 6d 2e 20 20 2a 2f 0a  pTab->tnum.  */.
1f20a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f20b 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c  eChangeToNoop(v,
1f20c 20 34 20 2d 20 66 6c 61 67 73 2c 20 31 29 3b 0a   4 - flags, 1);.
1f20d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f20e 65 43 68 61 6e 67 65 50 32 28 76 2c 20 33 20 2b  eChangeP2(v, 3 +
1f20f 20 66 6c 61 67 73 2c 20 70 54 61 62 2d 3e 74 6e   flags, pTab->tn
1f210 75 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  um);.      sqlit
1f211 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76  e3VdbeChangeP3(v
1f212 2c 20 33 20 2b 20 66 6c 61 67 73 2c 20 69 44 62  , 3 + flags, iDb
1f213 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e  );..      /* Con
1f214 66 69 67 75 72 65 20 74 68 65 20 6e 75 6d 62 65  figure the numbe
1f215 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e 20 43 6f  r of columns. Co
1f216 6e 66 69 67 75 72 65 20 74 68 65 20 63 75 72 73  nfigure the curs
1f217 6f 72 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74  or to.      ** t
1f218 68 69 6e 6b 20 74 68 61 74 20 74 68 65 20 74 61  hink that the ta
1f219 62 6c 65 20 68 61 73 20 6f 6e 65 20 6d 6f 72 65  ble has one more
1f21a 20 63 6f 6c 75 6d 6e 20 74 68 61 6e 20 69 74 20   column than it 
1f21b 72 65 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20  really.      ** 
1f21c 64 6f 65 73 2e 20 41 6e 20 4f 50 5f 43 6f 6c 75  does. An OP_Colu
1f21d 6d 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 74  mn to retrieve t
1f21e 68 69 73 20 69 6d 61 67 69 6e 61 72 79 20 63 6f  his imaginary co
1f21f 6c 75 6d 6e 20 77 69 6c 6c 0a 20 20 20 20 20 20  lumn will.      
1f220 2a 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e  ** always return
1f221 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 54 68   an SQL NULL. Th
1f222 69 73 20 69 73 20 75 73 65 66 75 6c 20 62 65 63  is is useful bec
1f223 61 75 73 65 20 69 74 20 6d 65 61 6e 73 0a 20 20  ause it means.  
1f224 20 20 20 20 2a 2a 20 77 65 20 63 61 6e 20 69 6e      ** we can in
1f225 76 6f 6b 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74  voke OP_Column t
1f226 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 76 64  o fill in the vd
1f227 62 65 20 63 75 72 73 6f 72 73 20 74 79 70 65 20  be cursors type 
1f228 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6f 66  .      ** and of
1f229 66 73 65 74 20 63 61 63 68 65 20 77 69 74 68 6f  fset cache witho
1f22a 75 74 20 63 61 75 73 69 6e 67 20 61 6e 79 20 49  ut causing any I
1f22b 4f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  O..      */.    
1f22c 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1f22d 6e 67 65 50 34 28 76 2c 20 33 2b 66 6c 61 67 73  ngeP4(v, 3+flags
1f22e 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f  , SQLITE_INT_TO_
1f22f 50 54 52 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31  PTR(pTab->nCol+1
1f230 29 2c 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20  ),P4_INT32);.   
1f231 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
1f232 61 6e 67 65 50 32 28 76 2c 20 37 2c 20 70 54 61  angeP2(v, 7, pTa
1f233 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20  b->nCol);.      
1f234 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  if( !db->mallocF
1f235 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20  ailed ){.       
1f236 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
1f237 52 65 61 64 79 28 76 2c 20 31 2c 20 31 2c 20 31  Ready(v, 1, 1, 1
1f238 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  , 0, 0, 0);.    
1f239 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 0a 20 20    }.    }.   .  
1f23a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
1f23b 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20  aveAll(db);.    
1f23c 72 63 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65  rc = sqlite3Safe
1f23d 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 69  tyOff(db);.    i
1f23e 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c  f( NEVER(rc!=SQL
1f23f 49 54 45 5f 4f 4b 29 20 7c 7c 20 64 62 2d 3e 6d  ITE_OK) || db->m
1f240 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1f241 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f       goto blob_o
1f242 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a  pen_out;.    }..
1f243 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64      sqlite3_bind
1f244 5f 69 6e 74 36 34 28 28 73 71 6c 69 74 65 33 5f  _int64((sqlite3_
1f245 73 74 6d 74 20 2a 29 76 2c 20 31 2c 20 69 52 6f  stmt *)v, 1, iRo
1f246 77 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  w);.    rc = sql
1f247 69 74 65 33 5f 73 74 65 70 28 28 73 71 6c 69 74  ite3_step((sqlit
1f248 65 33 5f 73 74 6d 74 20 2a 29 76 29 3b 0a 20 20  e3_stmt *)v);.  
1f249 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1f24a 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 6e 41  _ROW ){.      nA
1f24b 74 74 65 6d 70 74 2b 2b 3b 0a 20 20 20 20 20 20  ttempt++;.      
1f24c 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e  rc = sqlite3_fin
1f24d 61 6c 69 7a 65 28 28 73 71 6c 69 74 65 33 5f 73  alize((sqlite3_s
1f24e 74 6d 74 20 2a 29 76 29 3b 0a 20 20 20 20 20 20  tmt *)v);.      
1f24f 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1f250 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7a  , zErr);.      z
1f251 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Err = sqlite3MPr
1f252 69 6e 74 66 28 64 62 2c 20 73 71 6c 69 74 65 33  intf(db, sqlite3
1f253 5f 65 72 72 6d 73 67 28 64 62 29 29 3b 0a 20 20  _errmsg(db));.  
1f254 20 20 20 20 76 20 3d 20 30 3b 0a 20 20 20 20 7d      v = 0;.    }
1f255 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e 41 74 74  .  } while( nAtt
1f256 65 6d 70 74 3c 35 20 26 26 20 72 63 3d 3d 53 51  empt<5 && rc==SQ
1f257 4c 49 54 45 5f 53 43 48 45 4d 41 20 29 3b 0a 0a  LITE_SCHEMA );..
1f258 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1f259 5f 52 4f 57 20 29 7b 0a 20 20 20 20 2f 2a 20 54  _ROW ){.    /* T
1f25a 68 65 20 72 6f 77 2d 72 65 63 6f 72 64 20 68 61  he row-record ha
1f25b 73 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 73 75  s been opened su
1f25c 63 63 65 73 73 66 75 6c 6c 79 2e 20 43 68 65 63  ccessfully. Chec
1f25d 6b 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a  k that the.    *
1f25e 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 71 75 65 73  * column in ques
1f25f 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 65  tion contains te
1f260 78 74 20 6f 72 20 61 20 62 6c 6f 62 2e 20 49 66  xt or a blob. If
1f261 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20   it contains.   
1f262 20 2a 2a 20 74 65 78 74 2c 20 69 74 20 69 73 20   ** text, it is 
1f263 75 70 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  up to the caller
1f264 20 74 6f 20 67 65 74 20 74 68 65 20 65 6e 63 6f   to get the enco
1f265 64 69 6e 67 20 72 69 67 68 74 2e 0a 20 20 20 20  ding right..    
1f266 2a 2f 0a 20 20 20 20 49 6e 63 72 62 6c 6f 62 20  */.    Incrblob 
1f267 2a 70 42 6c 6f 62 3b 0a 20 20 20 20 75 33 32 20  *pBlob;.    u32 
1f268 74 79 70 65 20 3d 20 76 2d 3e 61 70 43 73 72 5b  type = v->apCsr[
1f269 30 5d 2d 3e 61 54 79 70 65 5b 69 43 6f 6c 5d 3b  0]->aType[iCol];
1f26a 0a 0a 20 20 20 20 69 66 28 20 74 79 70 65 3c 31  ..    if( type<1
1f26b 32 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  2 ){.      sqlit
1f26c 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72  e3DbFree(db, zEr
1f26d 72 29 3b 0a 20 20 20 20 20 20 7a 45 72 72 20 3d  r);.      zErr =
1f26e 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
1f26f 64 62 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e  db, "cannot open
1f270 20 76 61 6c 75 65 20 6f 66 20 74 79 70 65 20 25   value of type %
1f271 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20 74 79  s",.          ty
1f272 70 65 3d 3d 30 3f 22 6e 75 6c 6c 22 3a 20 74 79  pe==0?"null": ty
1f273 70 65 3d 3d 37 3f 22 72 65 61 6c 22 3a 20 22 69  pe==7?"real": "i
1f274 6e 74 65 67 65 72 22 0a 20 20 20 20 20 20 29 3b  nteger".      );
1f275 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
1f276 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20  TE_ERROR;.      
1f277 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f  goto blob_open_o
1f278 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42  ut;.    }.    pB
1f279 6c 6f 62 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20  lob = (Incrblob 
1f27a 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  *)sqlite3DbMallo
1f27b 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
1f27c 28 49 6e 63 72 62 6c 6f 62 29 29 3b 0a 20 20 20  (Incrblob));.   
1f27d 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
1f27e 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 73  ailed ){.      s
1f27f 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1f280 20 70 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 67   pBlob);.      g
1f281 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75  oto blob_open_ou
1f282 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 6c  t;.    }.    pBl
1f283 6f 62 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67  ob->flags = flag
1f284 73 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70 43  s;.    pBlob->pC
1f285 73 72 20 3d 20 20 76 2d 3e 61 70 43 73 72 5b 30  sr =  v->apCsr[0
1f286 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 20 20  ]->pCursor;.    
1f287 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
1f288 72 43 75 72 73 6f 72 28 70 42 6c 6f 62 2d 3e 70  rCursor(pBlob->p
1f289 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Csr);.    sqlite
1f28a 33 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 66  3BtreeCacheOverf
1f28b 6c 6f 77 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29  low(pBlob->pCsr)
1f28c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
1f28d 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 70 42  eeLeaveCursor(pB
1f28e 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20  lob->pCsr);.    
1f28f 70 42 6c 6f 62 2d 3e 70 53 74 6d 74 20 3d 20 28  pBlob->pStmt = (
1f290 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76  sqlite3_stmt *)v
1f291 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 69 4f 66  ;.    pBlob->iOf
1f292 66 73 65 74 20 3d 20 76 2d 3e 61 70 43 73 72 5b  fset = v->apCsr[
1f293 30 5d 2d 3e 61 4f 66 66 73 65 74 5b 69 43 6f 6c  0]->aOffset[iCol
1f294 5d 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 6e 42  ];.    pBlob->nB
1f295 79 74 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62  yte = sqlite3Vdb
1f296 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 74  eSerialTypeLen(t
1f297 79 70 65 29 3b 0a 20 20 20 20 70 42 6c 6f 62 2d  ype);.    pBlob-
1f298 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 2a 70  >db = db;.    *p
1f299 70 42 6c 6f 62 20 3d 20 28 73 71 6c 69 74 65 33  pBlob = (sqlite3
1f29a 5f 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20  _blob *)pBlob;. 
1f29b 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
1f29c 4b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72  K;.  }else if( r
1f29d 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1f29e 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1f29f 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20  e(db, zErr);.   
1f2a0 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d   zErr = sqlite3M
1f2a1 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f 20 73  Printf(db, "no s
1f2a2 75 63 68 20 72 6f 77 69 64 3a 20 25 6c 6c 64 22  uch rowid: %lld"
1f2a3 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20 72 63 20  , iRow);.    rc 
1f2a4 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
1f2a5 20 20 7d 0a 0a 62 6c 6f 62 5f 6f 70 65 6e 5f 6f    }..blob_open_o
1f2a6 75 74 3a 0a 20 20 69 66 28 20 76 20 26 26 20 28  ut:.  if( v && (
1f2a7 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
1f2a8 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1f2a9 64 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  d) ){.    sqlite
1f2aa 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29  3VdbeFinalize(v)
1f2ab 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45  ;.  }.  sqlite3E
1f2ac 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 7a 45 72  rror(db, rc, zEr
1f2ad 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  r);.  sqlite3DbF
1f2ae 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20  ree(db, zErr);. 
1f2af 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65   sqlite3StackFre
1f2b0 65 28 64 62 2c 20 70 50 61 72 73 65 29 3b 0a 20  e(db, pParse);. 
1f2b1 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
1f2b2 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
1f2b3 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1f2b4 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
1f2b5 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1f2b6 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 62 6c  /*.** Close a bl
1f2b7 6f 62 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77  ob handle that w
1f2b8 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 63 72  as previously cr
1f2b9 65 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73  eated using.** s
1f2ba 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e  qlite3_blob_open
1f2bb 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ()..*/.SQLITE_AP
1f2bc 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c  I int sqlite3_bl
1f2bd 6f 62 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33  ob_close(sqlite3
1f2be 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20  _blob *pBlob){. 
1f2bf 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28   Incrblob *p = (
1f2c0 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62  Incrblob *)pBlob
1f2c1 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71  ;.  int rc;.  sq
1f2c2 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66  lite3 *db;..  if
1f2c3 28 20 70 20 29 7b 0a 20 20 20 20 64 62 20 3d 20  ( p ){.    db = 
1f2c4 70 2d 3e 64 62 3b 0a 20 20 20 20 73 71 6c 69 74  p->db;.    sqlit
1f2c5 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
1f2c6 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72  b->mutex);.    r
1f2c7 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  c = sqlite3_fina
1f2c8 6c 69 7a 65 28 70 2d 3e 70 53 74 6d 74 29 3b 0a  lize(p->pStmt);.
1f2c9 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1f2ca 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 73 71  e(db, p);.    sq
1f2cb 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1f2cc 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
1f2cd 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
1f2ce 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
1f2cf 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1f2d0 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 72  *.** Perform a r
1f2d1 65 61 64 20 6f 72 20 77 72 69 74 65 20 6f 70 65  ead or write ope
1f2d2 72 61 74 69 6f 6e 20 6f 6e 20 61 20 62 6c 6f 62  ration on a blob
1f2d3 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
1f2d4 6c 6f 62 52 65 61 64 57 72 69 74 65 28 0a 20 20  lobReadWrite(.  
1f2d5 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42  sqlite3_blob *pB
1f2d6 6c 6f 62 2c 20 0a 20 20 76 6f 69 64 20 2a 7a 2c  lob, .  void *z,
1f2d7 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 69 6e   .  int n, .  in
1f2d8 74 20 69 4f 66 66 73 65 74 2c 20 0a 20 20 69 6e  t iOffset, .  in
1f2d9 74 20 28 2a 78 43 61 6c 6c 29 28 42 74 43 75 72  t (*xCall)(BtCur
1f2da 73 6f 72 2a 2c 20 75 33 32 2c 20 75 33 32 2c 20  sor*, u32, u32, 
1f2db 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20  void*).){.  int 
1f2dc 72 63 3b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a  rc;.  Incrblob *
1f2dd 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29  p = (Incrblob *)
1f2de 70 42 6c 6f 62 3b 0a 20 20 56 64 62 65 20 2a 76  pBlob;.  Vdbe *v
1f2df 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  ;.  sqlite3 *db;
1f2e0 0a 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ..  if( p==0 ) r
1f2e1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
1f2e2 55 53 45 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64  USE;.  db = p->d
1f2e3 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  b;.  sqlite3_mut
1f2e4 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
1f2e5 65 78 29 3b 0a 20 20 76 20 3d 20 28 56 64 62 65  ex);.  v = (Vdbe
1f2e6 2a 29 70 2d 3e 70 53 74 6d 74 3b 0a 0a 20 20 69  *)p->pStmt;..  i
1f2e7 66 28 20 6e 3c 30 20 7c 7c 20 69 4f 66 66 73 65  f( n<0 || iOffse
1f2e8 74 3c 30 20 7c 7c 20 28 69 4f 66 66 73 65 74 2b  t<0 || (iOffset+
1f2e9 6e 29 3e 70 2d 3e 6e 42 79 74 65 20 29 7b 0a 20  n)>p->nByte ){. 
1f2ea 20 20 20 2f 2a 20 52 65 71 75 65 73 74 20 69 73     /* Request is
1f2eb 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 52   out of range. R
1f2ec 65 74 75 72 6e 20 61 20 74 72 61 6e 73 69 65 6e  eturn a transien
1f2ed 74 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 20 20  t error. */.    
1f2ee 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
1f2ef 52 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  R;.    sqlite3Er
1f2f0 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 45  ror(db, SQLITE_E
1f2f1 52 52 4f 52 2c 20 30 29 3b 0a 20 20 7d 20 65 6c  RROR, 0);.  } el
1f2f2 73 65 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20  se if( v==0 ){. 
1f2f3 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69     /* If there i
1f2f4 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 68  s no statement h
1f2f5 61 6e 64 6c 65 2c 20 74 68 65 6e 20 74 68 65 20  andle, then the 
1f2f6 62 6c 6f 62 2d 68 61 6e 64 6c 65 20 68 61 73 0a  blob-handle has.
1f2f7 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 62      ** already b
1f2f8 65 65 6e 20 69 6e 76 61 6c 69 64 61 74 65 64 2e  een invalidated.
1f2f9 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41   Return SQLITE_A
1f2fa 42 4f 52 54 20 69 6e 20 74 68 69 73 20 63 61 73  BORT in this cas
1f2fb 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63  e..    */.    rc
1f2fc 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b   = SQLITE_ABORT;
1f2fd 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
1f2fe 20 43 61 6c 6c 20 65 69 74 68 65 72 20 42 74 72   Call either Btr
1f2ff 65 65 44 61 74 61 28 29 20 6f 72 20 42 74 72 65  eeData() or Btre
1f300 65 50 75 74 44 61 74 61 28 29 2e 20 49 66 20 53  ePutData(). If S
1f301 51 4c 49 54 45 5f 41 42 4f 52 54 20 69 73 0a 20  QLITE_ABORT is. 
1f302 20 20 20 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20     ** returned, 
1f303 63 6c 65 61 6e 2d 75 70 20 74 68 65 20 73 74 61  clean-up the sta
1f304 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 0a 20  tement handle.. 
1f305 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
1f306 28 20 64 62 20 3d 3d 20 76 2d 3e 64 62 20 29 3b  ( db == v->db );
1f307 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1f308 65 45 6e 74 65 72 43 75 72 73 6f 72 28 70 2d 3e  eEnterCursor(p->
1f309 70 43 73 72 29 3b 0a 20 20 20 20 72 63 20 3d 20  pCsr);.    rc = 
1f30a 78 43 61 6c 6c 28 70 2d 3e 70 43 73 72 2c 20 69  xCall(p->pCsr, i
1f30b 4f 66 66 73 65 74 2b 70 2d 3e 69 4f 66 66 73 65  Offset+p->iOffse
1f30c 74 2c 20 6e 2c 20 7a 29 3b 0a 20 20 20 20 73 71  t, n, z);.    sq
1f30d 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 43  lite3BtreeLeaveC
1f30e 75 72 73 6f 72 28 70 2d 3e 70 43 73 72 29 3b 0a  ursor(p->pCsr);.
1f30f 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1f310 54 45 5f 41 42 4f 52 54 20 29 7b 0a 20 20 20 20  TE_ABORT ){.    
1f311 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e    sqlite3VdbeFin
1f312 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20 20 20  alize(v);.      
1f313 70 2d 3e 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20  p->pStmt = 0;.  
1f314 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64    }else{.      d
1f315 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b  b->errCode = rc;
1f316 0a 20 20 20 20 20 20 76 2d 3e 72 63 20 3d 20 72  .      v->rc = r
1f317 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  c;.    }.  }.  r
1f318 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
1f319 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71  it(db, rc);.  sq
1f31a 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1f31b 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
1f31c 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1f31d 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 72  .** Read data fr
1f31e 6f 6d 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65  om a blob handle
1f31f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1f320 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
1f321 5f 72 65 61 64 28 73 71 6c 69 74 65 33 5f 62 6c  _read(sqlite3_bl
1f322 6f 62 20 2a 70 42 6c 6f 62 2c 20 76 6f 69 64 20  ob *pBlob, void 
1f323 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69  *z, int n, int i
1f324 4f 66 66 73 65 74 29 7b 0a 20 20 72 65 74 75 72  Offset){.  retur
1f325 6e 20 62 6c 6f 62 52 65 61 64 57 72 69 74 65 28  n blobReadWrite(
1f326 70 42 6c 6f 62 2c 20 7a 2c 20 6e 2c 20 69 4f 66  pBlob, z, n, iOf
1f327 66 73 65 74 2c 20 73 71 6c 69 74 65 33 42 74 72  fset, sqlite3Btr
1f328 65 65 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  eeData);.}../*.*
1f329 2a 20 57 72 69 74 65 20 64 61 74 61 20 74 6f 20  * Write data to 
1f32a 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a  a blob handle..*
1f32b 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1f32c 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72   sqlite3_blob_wr
1f32d 69 74 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ite(sqlite3_blob
1f32e 20 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73 74 20 76   *pBlob, const v
1f32f 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69  oid *z, int n, i
1f330 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a 20 20 72  nt iOffset){.  r
1f331 65 74 75 72 6e 20 62 6c 6f 62 52 65 61 64 57 72  eturn blobReadWr
1f332 69 74 65 28 70 42 6c 6f 62 2c 20 28 76 6f 69 64  ite(pBlob, (void
1f333 20 2a 29 7a 2c 20 6e 2c 20 69 4f 66 66 73 65 74   *)z, n, iOffset
1f334 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75  , sqlite3BtreePu
1f335 74 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  tData);.}../*.**
1f336 20 51 75 65 72 79 20 61 20 62 6c 6f 62 20 68 61   Query a blob ha
1f337 6e 64 6c 65 20 66 6f 72 20 74 68 65 20 73 69 7a  ndle for the siz
1f338 65 20 6f 66 20 74 68 65 20 64 61 74 61 2e 0a 2a  e of the data..*
1f339 2a 0a 2a 2a 20 54 68 65 20 49 6e 63 72 62 6c 6f  *.** The Incrblo
1f33a 62 2e 6e 42 79 74 65 20 66 69 65 6c 64 20 69 73  b.nByte field is
1f33b 20 66 69 78 65 64 20 66 6f 72 20 74 68 65 20 6c   fixed for the l
1f33c 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65 20 49  ifetime of the I
1f33d 6e 63 72 62 6c 6f 62 0a 2a 2a 20 73 6f 20 6e 6f  ncrblob.** so no
1f33e 20 6d 75 74 65 78 20 69 73 20 72 65 71 75 69 72   mutex is requir
1f33f 65 64 20 66 6f 72 20 61 63 63 65 73 73 2e 0a 2a  ed for access..*
1f340 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1f341 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79   sqlite3_blob_by
1f342 74 65 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62  tes(sqlite3_blob
1f343 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 49 6e 63 72   *pBlob){.  Incr
1f344 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62  blob *p = (Incrb
1f345 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 72  lob *)pBlob;.  r
1f346 65 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e 42 79  eturn p ? p->nBy
1f347 74 65 20 3a 20 30 3b 0a 7d 0a 0a 23 65 6e 64 69  te : 0;.}..#endi
1f348 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c  f /* #ifndef SQL
1f349 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
1f34a 42 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  B */../*********
1f34b 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62  ***** End of vdb
1f34c 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  eblob.c ********
1f34d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f34e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f34f 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1f350 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
1f351 20 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a   journal.c *****
1f352 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f353 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f354 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
1f355 20 41 75 67 75 73 74 20 32 32 0a 2a 2a 0a 2a 2a   August 22.**.**
1f356 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1f357 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1f358 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1f359 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1f35a 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
1f35b 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
1f35c 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
1f35d 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
1f35e 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
1f35f 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1f360 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
1f361 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
1f362 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1f363 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1f364 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1f365 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1f366 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1f367 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f368 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f369 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f36a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f36b 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
1f36c 40 28 23 29 20 24 49 64 3a 20 6a 6f 75 72 6e 61  @(#) $Id: journa
1f36d 6c 2e 63 2c 76 20 31 2e 39 20 32 30 30 39 2f 30  l.c,v 1.9 2009/0
1f36e 31 2f 32 30 20 31 37 3a 30 36 3a 32 37 20 64 61  1/20 17:06:27 da
1f36f 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a  nielk1977 Exp $.
1f370 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
1f371 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f  E_ENABLE_ATOMIC_
1f372 57 52 49 54 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  WRITE../*.** Thi
1f373 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  s file implement
1f374 73 20 61 20 73 70 65 63 69 61 6c 20 6b 69 6e 64  s a special kind
1f375 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65   of sqlite3_file
1f376 20 6f 62 6a 65 63 74 20 75 73 65 64 0a 2a 2a 20   object used.** 
1f377 62 79 20 53 51 4c 69 74 65 20 74 6f 20 63 72 65  by SQLite to cre
1f378 61 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ate journal file
1f379 73 20 69 66 20 74 68 65 20 61 74 6f 6d 69 63 2d  s if the atomic-
1f37a 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69  write optimizati
1f37b 6f 6e 0a 2a 2a 20 69 73 20 65 6e 61 62 6c 65 64  on.** is enabled
1f37c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 73 74  ..**.** The dist
1f37d 69 6e 63 74 69 76 65 20 63 68 61 72 61 63 74 65  inctive characte
1f37e 72 69 73 74 69 63 20 6f 66 20 74 68 69 73 20 73  ristic of this s
1f37f 71 6c 69 74 65 33 5f 66 69 6c 65 20 69 73 20 74  qlite3_file is t
1f380 68 61 74 20 74 68 65 0a 2a 2a 20 61 63 74 75 61  hat the.** actua
1f381 6c 20 6f 6e 20 64 69 73 6b 20 66 69 6c 65 20 69  l on disk file i
1f382 73 20 63 72 65 61 74 65 64 20 6c 61 7a 69 6c 79  s created lazily
1f383 2e 20 57 68 65 6e 20 74 68 65 20 66 69 6c 65 20  . When the file 
1f384 69 73 20 63 72 65 61 74 65 64 2c 0a 2a 2a 20 74  is created,.** t
1f385 68 65 20 63 61 6c 6c 65 72 20 73 70 65 63 69 66  he caller specif
1f386 69 65 73 20 61 20 62 75 66 66 65 72 20 73 69 7a  ies a buffer siz
1f387 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f  e for an in-memo
1f388 72 79 20 62 75 66 66 65 72 20 74 6f 0a 2a 2a 20  ry buffer to.** 
1f389 62 65 20 75 73 65 64 20 74 6f 20 73 65 72 76 69  be used to servi
1f38a 63 65 20 72 65 61 64 28 29 20 61 6e 64 20 77 72  ce read() and wr
1f38b 69 74 65 28 29 20 72 65 71 75 65 73 74 73 2e 20  ite() requests. 
1f38c 54 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 0a  The actual file.
1f38d 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 73 20 6e 6f  ** on disk is no
1f38e 74 20 63 72 65 61 74 65 64 20 6f 72 20 70 6f 70  t created or pop
1f38f 75 6c 61 74 65 64 20 75 6e 74 69 6c 20 65 69 74  ulated until eit
1f390 68 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20  her:.**.**   1) 
1f391 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65  The in-memory re
1f392 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 67 72 6f  presentation gro
1f393 77 73 20 74 6f 6f 20 6c 61 72 67 65 20 66 6f 72  ws too large for
1f394 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 0a   the allocated .
1f395 2a 2a 20 20 20 20 20 20 62 75 66 66 65 72 2c 20  **      buffer, 
1f396 6f 72 0a 2a 2a 20 20 20 32 29 20 54 68 65 20 73  or.**   2) The s
1f397 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65  qlite3JournalCre
1f398 61 74 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69  ate() function i
1f399 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 0a 0a 0a  s called..*/....
1f39a 2f 2a 0a 2a 2a 20 41 20 4a 6f 75 72 6e 61 6c 46  /*.** A JournalF
1f39b 69 6c 65 20 6f 62 6a 65 63 74 20 69 73 20 61 20  ile object is a 
1f39c 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69  subclass of sqli
1f39d 74 65 33 5f 66 69 6c 65 20 75 73 65 64 20 62 79  te3_file used by
1f39e 0a 2a 2a 20 61 73 20 61 6e 20 6f 70 65 6e 20 66  .** as an open f
1f39f 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f 72 20 6a  ile handle for j
1f3a0 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f  ournal files..*/
1f3a1 0a 73 74 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46  .struct JournalF
1f3a2 69 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  ile {.  sqlite3_
1f3a3 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74  io_methods *pMet
1f3a4 68 6f 64 3b 20 20 20 20 2f 2a 20 49 2f 4f 20 6d  hod;    /* I/O m
1f3a5 65 74 68 6f 64 73 20 6f 6e 20 6a 6f 75 72 6e 61  ethods on journa
1f3a6 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 20 69 6e 74  l files */.  int
1f3a7 20 6e 42 75 66 3b 20 20 20 20 20 20 20 20 20 20   nBuf;          
1f3a8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1f3a9 53 69 7a 65 20 6f 66 20 7a 42 75 66 5b 5d 20 69  Size of zBuf[] i
1f3aa 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61  n bytes */.  cha
1f3ab 72 20 2a 7a 42 75 66 3b 20 20 20 20 20 20 20 20  r *zBuf;        
1f3ac 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1f3ad 53 70 61 63 65 20 74 6f 20 62 75 66 66 65 72 20  Space to buffer 
1f3ae 6a 6f 75 72 6e 61 6c 20 77 72 69 74 65 73 20 2a  journal writes *
1f3af 2f 0a 20 20 69 6e 74 20 69 53 69 7a 65 3b 20 20  /.  int iSize;  
1f3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f3b1 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66      /* Amount of
1f3b2 20 7a 42 75 66 5b 5d 20 63 75 72 72 65 6e 74 6c   zBuf[] currentl
1f3b3 79 20 75 73 65 64 20 2a 2f 0a 20 20 69 6e 74 20  y used */.  int 
1f3b4 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  flags;          
1f3b5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
1f3b6 4f 70 65 6e 20 66 6c 61 67 73 20 2a 2f 0a 20 20  Open flags */.  
1f3b7 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
1f3b8 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
1f3b9 2f 2a 20 54 68 65 20 22 72 65 61 6c 22 20 75 6e  /* The "real" un
1f3ba 64 65 72 6c 79 69 6e 67 20 56 46 53 20 2a 2f 0a  derlying VFS */.
1f3bb 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
1f3bc 70 52 65 61 6c 3b 20 20 20 20 20 20 20 20 20 20  pReal;          
1f3bd 20 20 2f 2a 20 54 68 65 20 22 72 65 61 6c 22 20    /* The "real" 
1f3be 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c 65 20  underlying file 
1f3bf 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20  descriptor */.  
1f3c0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4a 6f 75  const char *zJou
1f3c1 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20  rnal;           
1f3c2 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a  /* Name of the j
1f3c3 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 7d  ournal file */.}
1f3c4 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
1f3c5 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 4a 6f 75   JournalFile Jou
1f3c6 72 6e 61 6c 46 69 6c 65 3b 0a 0a 2f 2a 0a 2a 2a  rnalFile;../*.**
1f3c7 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20   If it does not 
1f3c8 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2c 20  already exists, 
1f3c9 63 72 65 61 74 65 20 61 6e 64 20 70 6f 70 75 6c  create and popul
1f3ca 61 74 65 20 74 68 65 20 6f 6e 2d 64 69 73 6b 20  ate the on-disk 
1f3cb 66 69 6c 65 20 0a 2a 2a 20 66 6f 72 20 4a 6f 75  file .** for Jou
1f3cc 72 6e 61 6c 46 69 6c 65 20 70 2e 0a 2a 2f 0a 73  rnalFile p..*/.s
1f3cd 74 61 74 69 63 20 69 6e 74 20 63 72 65 61 74 65  tatic int create
1f3ce 46 69 6c 65 28 4a 6f 75 72 6e 61 6c 46 69 6c 65  File(JournalFile
1f3cf 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d   *p){.  int rc =
1f3d0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
1f3d1 28 20 21 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20  ( !p->pReal ){. 
1f3d2 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20     sqlite3_file 
1f3d3 2a 70 52 65 61 6c 20 3d 20 28 73 71 6c 69 74 65  *pReal = (sqlite
1f3d4 33 5f 66 69 6c 65 20 2a 29 26 70 5b 31 5d 3b 0a  3_file *)&p[1];.
1f3d5 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1f3d6 4f 73 4f 70 65 6e 28 70 2d 3e 70 56 66 73 2c 20  OsOpen(p->pVfs, 
1f3d7 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 52 65  p->zJournal, pRe
1f3d8 61 6c 2c 20 70 2d 3e 66 6c 61 67 73 2c 20 30 29  al, p->flags, 0)
1f3d9 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
1f3da 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1f3db 20 70 2d 3e 70 52 65 61 6c 20 3d 20 70 52 65 61   p->pReal = pRea
1f3dc 6c 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  l;.      if( p->
1f3dd 69 53 69 7a 65 3e 30 20 29 7b 0a 20 20 20 20 20  iSize>0 ){.     
1f3de 20 20 20 61 73 73 65 72 74 28 70 2d 3e 69 53 69     assert(p->iSi
1f3df 7a 65 3c 3d 70 2d 3e 6e 42 75 66 29 3b 0a 20 20  ze<=p->nBuf);.  
1f3e0 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1f3e1 65 33 4f 73 57 72 69 74 65 28 70 2d 3e 70 52 65  e3OsWrite(p->pRe
1f3e2 61 6c 2c 20 70 2d 3e 7a 42 75 66 2c 20 70 2d 3e  al, p->zBuf, p->
1f3e3 69 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20 20  iSize, 0);.     
1f3e4 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
1f3e5 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
1f3e6 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 69 6c  ** Close the fil
1f3e7 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1f3e8 20 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 6c 69 74   jrnlClose(sqlit
1f3e9 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a  e3_file *pJfd){.
1f3ea 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70    JournalFile *p
1f3eb 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20   = (JournalFile 
1f3ec 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d  *)pJfd;.  if( p-
1f3ed 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 73 71  >pReal ){.    sq
1f3ee 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 2d 3e  lite3OsClose(p->
1f3ef 70 52 65 61 6c 29 3b 0a 20 20 7d 0a 20 20 73 71  pReal);.  }.  sq
1f3f0 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 42  lite3_free(p->zB
1f3f1 75 66 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  uf);.  return SQ
1f3f2 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
1f3f3 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d  * Read data from
1f3f4 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74   the file..*/.st
1f3f5 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 52 65 61  atic int jrnlRea
1f3f6 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  d(.  sqlite3_fil
1f3f7 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54  e *pJfd,    /* T
1f3f8 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
1f3f9 66 72 6f 6d 20 77 68 69 63 68 20 74 6f 20 72 65  from which to re
1f3fa 61 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a 42  ad */.  void *zB
1f3fb 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  uf,            /
1f3fc 2a 20 50 75 74 20 74 68 65 20 72 65 73 75 6c 74  * Put the result
1f3fd 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20  s here */.  int 
1f3fe 69 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20  iAmt,           
1f3ff 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1f400 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f  bytes to read */
1f401 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
1f402 69 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67  iOfst     /* Beg
1f403 69 6e 20 72 65 61 64 69 6e 67 20 61 74 20 74 68  in reading at th
1f404 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a  is offset */.){.
1f405 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1f406 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46  E_OK;.  JournalF
1f407 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61  ile *p = (Journa
1f408 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20  lFile *)pJfd;.  
1f409 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a  if( p->pReal ){.
1f40a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1f40b 4f 73 52 65 61 64 28 70 2d 3e 70 52 65 61 6c 2c  OsRead(p->pReal,
1f40c 20 7a 42 75 66 2c 20 69 41 6d 74 2c 20 69 4f 66   zBuf, iAmt, iOf
1f40d 73 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  st);.  }else if(
1f40e 20 28 69 41 6d 74 2b 69 4f 66 73 74 29 3e 70 2d   (iAmt+iOfst)>p-
1f40f 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 63  >iSize ){.    rc
1f410 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
1f411 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 65  SHORT_READ;.  }e
1f412 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28  lse{.    memcpy(
1f413 7a 42 75 66 2c 20 26 70 2d 3e 7a 42 75 66 5b 69  zBuf, &p->zBuf[i
1f414 4f 66 73 74 5d 2c 20 69 41 6d 74 29 3b 0a 20 20  Ofst], iAmt);.  
1f415 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1f416 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61  ../*.** Write da
1f417 74 61 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a  ta to the file..
1f418 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72  */.static int jr
1f419 6e 6c 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74  nlWrite(.  sqlit
1f41a 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20  e3_file *pJfd,  
1f41b 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c    /* The journal
1f41c 20 66 69 6c 65 20 69 6e 74 6f 20 77 68 69 63 68   file into which
1f41d 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 63   to write */.  c
1f41e 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c  onst void *zBuf,
1f41f 20 20 20 20 20 20 2f 2a 20 54 61 6b 65 20 64 61        /* Take da
1f420 74 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e  ta to be written
1f421 20 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 20 20   from here */.  
1f422 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20  int iAmt,       
1f423 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1f424 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69   of bytes to wri
1f425 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69  te */.  sqlite_i
1f426 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f  nt64 iOfst     /
1f427 2a 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67 20  * Begin writing 
1f428 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 69  at this offset i
1f429 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a  nto the file */.
1f42a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
1f42b 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e  LITE_OK;.  Journ
1f42c 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75  alFile *p = (Jou
1f42d 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b  rnalFile *)pJfd;
1f42e 0a 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61 6c  .  if( !p->pReal
1f42f 20 26 26 20 28 69 4f 66 73 74 2b 69 41 6d 74 29   && (iOfst+iAmt)
1f430 3e 70 2d 3e 6e 42 75 66 20 29 7b 0a 20 20 20 20  >p->nBuf ){.    
1f431 72 63 20 3d 20 63 72 65 61 74 65 46 69 6c 65 28  rc = createFile(
1f432 70 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  p);.  }.  if( rc
1f433 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1f434 20 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20     if( p->pReal 
1f435 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
1f436 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d 3e  lite3OsWrite(p->
1f437 70 52 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d  pReal, zBuf, iAm
1f438 74 2c 20 69 4f 66 73 74 29 3b 0a 20 20 20 20 7d  t, iOfst);.    }
1f439 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63  else{.      memc
1f43a 70 79 28 26 70 2d 3e 7a 42 75 66 5b 69 4f 66 73  py(&p->zBuf[iOfs
1f43b 74 5d 2c 20 7a 42 75 66 2c 20 69 41 6d 74 29 3b  t], zBuf, iAmt);
1f43c 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 53  .      if( p->iS
1f43d 69 7a 65 3c 28 69 4f 66 73 74 2b 69 41 6d 74 29  ize<(iOfst+iAmt)
1f43e 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69   ){.        p->i
1f43f 53 69 7a 65 20 3d 20 28 69 4f 66 73 74 2b 69 41  Size = (iOfst+iA
1f440 6d 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  mt);.      }.   
1f441 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1f442 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75  rc;.}../*.** Tru
1f443 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65 2e 0a  ncate the file..
1f444 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72  */.static int jr
1f445 6e 6c 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74  nlTruncate(sqlit
1f446 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73  e3_file *pJfd, s
1f447 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 69 7a 65  qlite_int64 size
1f448 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
1f449 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e  LITE_OK;.  Journ
1f44a 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75  alFile *p = (Jou
1f44b 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b  rnalFile *)pJfd;
1f44c 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20  .  if( p->pReal 
1f44d 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
1f44e 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 2d  te3OsTruncate(p-
1f44f 3e 70 52 65 61 6c 2c 20 73 69 7a 65 29 3b 0a 20  >pReal, size);. 
1f450 20 7d 65 6c 73 65 20 69 66 28 20 73 69 7a 65 3c   }else if( size<
1f451 70 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 20  p->iSize ){.    
1f452 70 2d 3e 69 53 69 7a 65 20 3d 20 73 69 7a 65 3b  p->iSize = size;
1f453 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1f454 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20  ;.}../*.** Sync 
1f455 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  the file..*/.sta
1f456 74 69 63 20 69 6e 74 20 6a 72 6e 6c 53 79 6e 63  tic int jrnlSync
1f457 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70  (sqlite3_file *p
1f458 4a 66 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b  Jfd, int flags){
1f459 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4a 6f 75  .  int rc;.  Jou
1f45a 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a  rnalFile *p = (J
1f45b 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66  ournalFile *)pJf
1f45c 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61  d;.  if( p->pRea
1f45d 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  l ){.    rc = sq
1f45e 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 2d 3e 70  lite3OsSync(p->p
1f45f 52 65 61 6c 2c 20 66 6c 61 67 73 29 3b 0a 20 20  Real, flags);.  
1f460 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
1f461 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
1f462 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1f463 2a 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 73  *.** Query the s
1f464 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ize of the file 
1f465 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61  in bytes..*/.sta
1f466 74 69 63 20 69 6e 74 20 6a 72 6e 6c 46 69 6c 65  tic int jrnlFile
1f467 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  Size(sqlite3_fil
1f468 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f  e *pJfd, sqlite_
1f469 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20  int64 *pSize){. 
1f46a 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1f46b 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69  _OK;.  JournalFi
1f46c 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c  le *p = (Journal
1f46d 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69  File *)pJfd;.  i
1f46e 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20  f( p->pReal ){. 
1f46f 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
1f470 73 46 69 6c 65 53 69 7a 65 28 70 2d 3e 70 52 65  sFileSize(p->pRe
1f471 61 6c 2c 20 70 53 69 7a 65 29 3b 0a 20 20 7d 65  al, pSize);.  }e
1f472 6c 73 65 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20  lse{.    *pSize 
1f473 3d 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29  = (sqlite_int64)
1f474 20 70 2d 3e 69 53 69 7a 65 3b 0a 20 20 7d 0a 20   p->iSize;.  }. 
1f475 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1f476 2a 0a 2a 2a 20 54 61 62 6c 65 20 6f 66 20 6d 65  *.** Table of me
1f477 74 68 6f 64 73 20 66 6f 72 20 4a 6f 75 72 6e 61  thods for Journa
1f478 6c 46 69 6c 65 20 73 71 6c 69 74 65 33 5f 66 69  lFile sqlite3_fi
1f479 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74  le object..*/.st
1f47a 61 74 69 63 20 73 74 72 75 63 74 20 73 71 6c 69  atic struct sqli
1f47b 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4a  te3_io_methods J
1f47c 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64  ournalFileMethod
1f47d 73 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 20  s = {.  1,      
1f47e 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69         /* iVersi
1f47f 6f 6e 20 2a 2f 0a 20 20 6a 72 6e 6c 43 6c 6f 73  on */.  jrnlClos
1f480 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65  e,     /* xClose
1f481 20 2a 2f 0a 20 20 6a 72 6e 6c 52 65 61 64 2c 20   */.  jrnlRead, 
1f482 20 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f       /* xRead */
1f483 0a 20 20 6a 72 6e 6c 57 72 69 74 65 2c 20 20 20  .  jrnlWrite,   
1f484 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f 0a 20    /* xWrite */. 
1f485 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65 2c 20 20   jrnlTruncate,  
1f486 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 0a  /* xTruncate */.
1f487 20 20 6a 72 6e 6c 53 79 6e 63 2c 20 20 20 20 20    jrnlSync,     
1f488 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 0a 20 20 6a   /* xSync */.  j
1f489 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f 2a  rnlFileSize,  /*
1f48a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20 20   xFileSize */.  
1f48b 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0,             /
1f48c 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20  * xLock */.  0, 
1f48d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
1f48e 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20  Unlock */.  0,  
1f48f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43             /* xC
1f490 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
1f491 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20   */.  0,        
1f492 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 43 6f 6e       /* xFileCon
1f493 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c 20 20 20 20  trol */.  0,    
1f494 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63           /* xSec
1f495 74 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 20 20  torSize */.  0  
1f496 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
1f497 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
1f498 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20  stics */.};../* 
1f499 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f 75 72 6e  .** Open a journ
1f49a 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49  al file..*/.SQLI
1f49b 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1f49c 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65  qlite3JournalOpe
1f49d 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  n(.  sqlite3_vfs
1f49e 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20   *pVfs,         
1f49f 2f 2a 20 54 68 65 20 56 46 53 20 74 6f 20 75 73  /* The VFS to us
1f4a0 65 20 66 6f 72 20 61 63 74 75 61 6c 20 66 69 6c  e for actual fil
1f4a1 65 20 49 2f 4f 20 2a 2f 0a 20 20 63 6f 6e 73 74  e I/O */.  const
1f4a2 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20   char *zName,   
1f4a3 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
1f4a4 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
1f4a5 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66  e */.  sqlite3_f
1f4a6 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 20 20  ile *pJfd,      
1f4a7 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 74 65    /* Preallocate
1f4a8 64 2c 20 62 6c 61 6e 6b 20 66 69 6c 65 20 68 61  d, blank file ha
1f4a9 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  ndle */.  int fl
1f4aa 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  ags,            
1f4ab 20 20 20 20 20 2f 2a 20 4f 70 65 6e 69 6e 67 20       /* Opening 
1f4ac 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  flags */.  int n
1f4ad 42 75 66 20 20 20 20 20 20 20 20 20 20 20 20 20  Buf             
1f4ae 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 62        /* Bytes b
1f4af 75 66 66 65 72 65 64 20 62 65 66 6f 72 65 20 6f  uffered before o
1f4b0 70 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 20  pening the file 
1f4b1 2a 2f 0a 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46  */.){.  JournalF
1f4b2 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61  ile *p = (Journa
1f4b3 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20  lFile *)pJfd;.  
1f4b4 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 71 6c  memset(p, 0, sql
1f4b5 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28  ite3JournalSize(
1f4b6 70 56 66 73 29 29 3b 0a 20 20 69 66 28 20 6e 42  pVfs));.  if( nB
1f4b7 75 66 3e 30 20 29 7b 0a 20 20 20 20 70 2d 3e 7a  uf>0 ){.    p->z
1f4b8 42 75 66 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  Buf = sqlite3Mal
1f4b9 6c 6f 63 5a 65 72 6f 28 6e 42 75 66 29 3b 0a 20  locZero(nBuf);. 
1f4ba 20 20 20 69 66 28 20 21 70 2d 3e 7a 42 75 66 20     if( !p->zBuf 
1f4bb 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
1f4bc 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
1f4bd 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
1f4be 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4f   return sqlite3O
1f4bf 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4e 61 6d  sOpen(pVfs, zNam
1f4c0 65 2c 20 70 4a 66 64 2c 20 66 6c 61 67 73 2c 20  e, pJfd, flags, 
1f4c1 30 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4d 65  0);.  }.  p->pMe
1f4c2 74 68 6f 64 20 3d 20 26 4a 6f 75 72 6e 61 6c 46  thod = &JournalF
1f4c3 69 6c 65 4d 65 74 68 6f 64 73 3b 0a 20 20 70 2d  ileMethods;.  p-
1f4c4 3e 6e 42 75 66 20 3d 20 6e 42 75 66 3b 0a 20 20  >nBuf = nBuf;.  
1f4c5 70 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 73  p->flags = flags
1f4c6 3b 0a 20 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 20  ;.  p->zJournal 
1f4c7 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 2d 3e 70 56  = zName;.  p->pV
1f4c8 66 73 20 3d 20 70 56 66 73 3b 0a 20 20 72 65 74  fs = pVfs;.  ret
1f4c9 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1f4ca 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61  ../*.** If the a
1f4cb 72 67 75 6d 65 6e 74 20 70 20 70 6f 69 6e 74 73  rgument p points
1f4cc 20 74 6f 20 61 20 4a 6f 75 72 6e 61 6c 46 69 6c   to a JournalFil
1f4cd 65 20 73 74 72 75 63 74 75 72 65 2c 20 61 6e 64  e structure, and
1f4ce 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a   the underlying.
1f4cf 2a 2a 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 20  ** file has not 
1f4d0 79 65 74 20 62 65 65 6e 20 63 72 65 61 74 65 64  yet been created
1f4d1 2c 20 63 72 65 61 74 65 20 69 74 20 6e 6f 77 2e  , create it now.
1f4d2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1f4d3 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f  TE int sqlite3Jo
1f4d4 75 72 6e 61 6c 43 72 65 61 74 65 28 73 71 6c 69  urnalCreate(sqli
1f4d5 74 65 33 5f 66 69 6c 65 20 2a 70 29 7b 0a 20 20  te3_file *p){.  
1f4d6 69 66 28 20 70 2d 3e 70 4d 65 74 68 6f 64 73 21  if( p->pMethods!
1f4d7 3d 26 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74  =&JournalFileMet
1f4d8 68 6f 64 73 20 29 7b 0a 20 20 20 20 72 65 74 75  hods ){.    retu
1f4d9 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
1f4da 7d 0a 20 20 72 65 74 75 72 6e 20 63 72 65 61 74  }.  return creat
1f4db 65 46 69 6c 65 28 28 4a 6f 75 72 6e 61 6c 46 69  eFile((JournalFi
1f4dc 6c 65 20 2a 29 70 29 3b 0a 7d 0a 0a 2f 2a 20 0a  le *)p);.}../* .
1f4dd 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
1f4de 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65  mber of bytes re
1f4df 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20  quired to store 
1f4e0 61 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 74 68  a JournalFile th
1f4e1 61 74 20 75 73 65 73 20 76 66 73 0a 2a 2a 20 70  at uses vfs.** p
1f4e2 56 66 73 20 74 6f 20 63 72 65 61 74 65 20 74 68  Vfs to create th
1f4e3 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 6e 2d  e underlying on-
1f4e4 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53  disk files..*/.S
1f4e5 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1f4e6 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c  t sqlite3Journal
1f4e7 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 76 66 73  Size(sqlite3_vfs
1f4e8 20 2a 70 56 66 73 29 7b 0a 20 20 72 65 74 75 72   *pVfs){.  retur
1f4e9 6e 20 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c  n (pVfs->szOsFil
1f4ea 65 2b 73 69 7a 65 6f 66 28 4a 6f 75 72 6e 61 6c  e+sizeof(Journal
1f4eb 46 69 6c 65 29 29 3b 0a 7d 0a 23 65 6e 64 69 66  File));.}.#endif
1f4ec 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
1f4ed 2a 20 45 6e 64 20 6f 66 20 6a 6f 75 72 6e 61 6c  * End of journal
1f4ee 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1f4ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f4f1 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
1f4f2 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d  * Begin file mem
1f4f3 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a  journal.c ******
1f4f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f4f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f4f6 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74  /./*.** 2008 Oct
1f4f7 6f 62 65 72 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65  ober 7.**.** The
1f4f8 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
1f4f9 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
1f4fa 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
1f4fb 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
1f4fc 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
1f4fd 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
1f4fe 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
1f4ff 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
1f500 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
1f501 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
1f502 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
1f503 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
1f504 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
1f505 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
1f506 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
1f507 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
1f508 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
1f509 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f50a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f50b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f50c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f50d 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
1f50e 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
1f50f 6f 64 65 20 75 73 65 20 74 6f 20 69 6d 70 6c 65  ode use to imple
1f510 6d 65 6e 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  ment an in-memor
1f511 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  y rollback journ
1f512 61 6c 2e 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d 65  al..** The in-me
1f513 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f  mory rollback jo
1f514 75 72 6e 61 6c 20 69 73 20 75 73 65 64 20 74 6f  urnal is used to
1f515 20 6a 6f 75 72 6e 61 6c 20 74 72 61 6e 73 61 63   journal transac
1f516 74 69 6f 6e 73 20 66 6f 72 0a 2a 2a 20 22 3a 6d  tions for.** ":m
1f517 65 6d 6f 72 79 3a 22 20 64 61 74 61 62 61 73 65  emory:" database
1f518 73 20 61 6e 64 20 77 68 65 6e 20 74 68 65 20 6a  s and when the j
1f519 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4d 45 4d 4f  ournal_mode=MEMO
1f51a 52 59 20 70 72 61 67 6d 61 20 69 73 20 75 73 65  RY pragma is use
1f51b 64 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49  d..**.** @(#) $I
1f51c 64 3a 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 2c  d: memjournal.c,
1f51d 76 20 31 2e 31 32 20 32 30 30 39 2f 30 35 2f 30  v 1.12 2009/05/0
1f51e 34 20 31 31 3a 34 32 3a 33 30 20 64 61 6e 69 65  4 11:42:30 danie
1f51f 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
1f520 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65  ./* Forward refe
1f521 72 65 6e 63 65 73 20 74 6f 20 69 6e 74 65 72 6e  rences to intern
1f522 61 6c 20 73 74 72 75 63 74 75 72 65 73 20 2a 2f  al structures */
1f523 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
1f524 4d 65 6d 4a 6f 75 72 6e 61 6c 20 4d 65 6d 4a 6f  MemJournal MemJo
1f525 75 72 6e 61 6c 3b 0a 74 79 70 65 64 65 66 20 73  urnal;.typedef s
1f526 74 72 75 63 74 20 46 69 6c 65 50 6f 69 6e 74 20  truct FilePoint 
1f527 46 69 6c 65 50 6f 69 6e 74 3b 0a 74 79 70 65 64  FilePoint;.typed
1f528 65 66 20 73 74 72 75 63 74 20 46 69 6c 65 43 68  ef struct FileCh
1f529 75 6e 6b 20 46 69 6c 65 43 68 75 6e 6b 3b 0a 0a  unk FileChunk;..
1f52a 2f 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64  /* Space to hold
1f52b 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
1f52c 75 72 6e 61 6c 20 69 73 20 61 6c 6c 6f 63 61 74  urnal is allocat
1f52d 65 64 20 69 6e 20 69 6e 63 72 65 6d 65 6e 74 73  ed in increments
1f52e 20 6f 66 0a 2a 2a 20 74 68 69 73 20 6d 61 6e 79   of.** this many
1f52f 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   bytes..**.** Th
1f530 65 20 73 69 7a 65 20 63 68 6f 73 65 6e 20 69 73  e size chosen is
1f531 20 61 20 6c 69 74 74 6c 65 20 6c 65 73 73 20 74   a little less t
1f532 68 61 6e 20 61 20 70 6f 77 65 72 20 6f 66 20 74  han a power of t
1f533 77 6f 2e 20 20 54 68 61 74 20 77 61 79 2c 0a 2a  wo.  That way,.*
1f534 2a 20 74 68 65 20 46 69 6c 65 43 68 75 6e 6b 20  * the FileChunk 
1f535 6f 62 6a 65 63 74 20 77 69 6c 6c 20 68 61 76 65  object will have
1f536 20 61 20 73 69 7a 65 20 74 68 61 74 20 61 6c 6d   a size that alm
1f537 6f 73 74 20 65 78 61 63 74 6c 79 20 66 69 6c 6c  ost exactly fill
1f538 73 0a 2a 2a 20 61 20 70 6f 77 65 72 2d 6f 66 2d  s.** a power-of-
1f539 74 77 6f 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20  two allocation. 
1f53a 20 54 68 69 73 20 6d 69 6d 69 6d 69 7a 65 73 20   This mimimizes 
1f53b 77 61 73 74 65 64 20 73 70 61 63 65 20 69 6e 20  wasted space in 
1f53c 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 0a 2a 2a 20  power-of-two.** 
1f53d 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
1f53e 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f  s..*/.#define JO
1f53f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20  URNAL_CHUNKSIZE 
1f540 28 28 69 6e 74 29 28 31 30 32 34 2d 73 69 7a 65  ((int)(1024-size
1f541 6f 66 28 46 69 6c 65 43 68 75 6e 6b 2a 29 29 29  of(FileChunk*)))
1f542 0a 0a 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 66 69  ../* Macro to fi
1f543 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 6f  nd the minimum o
1f544 66 20 74 77 6f 20 6e 75 6d 65 72 69 63 20 76 61  f two numeric va
1f545 6c 75 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  lues..*/.#ifndef
1f546 20 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20 4d 49   MIN.# define MI
1f547 4e 28 78 2c 79 29 20 28 28 78 29 3c 28 79 29 3f  N(x,y) ((x)<(y)?
1f548 28 78 29 3a 28 79 29 29 0a 23 65 6e 64 69 66 0a  (x):(y)).#endif.
1f549 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62  ./*.** The rollb
1f54a 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 63  ack journal is c
1f54b 6f 6d 70 6f 73 65 64 20 6f 66 20 61 20 6c 69 6e  omposed of a lin
1f54c 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 68 65 73  ked list of thes
1f54d 65 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f  e structures..*/
1f54e 0a 73 74 72 75 63 74 20 46 69 6c 65 43 68 75 6e  .struct FileChun
1f54f 6b 20 7b 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20  k {.  FileChunk 
1f550 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20  *pNext;         
1f551 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68        /* Next ch
1f552 75 6e 6b 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  unk in the journ
1f553 61 6c 20 2a 2f 0a 20 20 75 38 20 7a 43 68 75 6e  al */.  u8 zChun
1f554 6b 5b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53  k[JOURNAL_CHUNKS
1f555 49 5a 45 5d 3b 20 20 20 2f 2a 20 43 6f 6e 74 65  IZE];   /* Conte
1f556 6e 74 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b  nt of this chunk
1f557 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
1f558 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
1f559 73 20 6f 62 6a 65 63 74 20 73 65 72 76 65 73 20  s object serves 
1f55a 61 73 20 61 20 63 75 72 73 6f 72 20 69 6e 74 6f  as a cursor into
1f55b 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
1f55c 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 63 75  urnal..** The cu
1f55d 72 73 6f 72 20 63 61 6e 20 62 65 20 65 69 74 68  rsor can be eith
1f55e 65 72 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f  er for reading o
1f55f 72 20 77 72 69 74 69 6e 67 2e 0a 2a 2f 0a 73 74  r writing..*/.st
1f560 72 75 63 74 20 46 69 6c 65 50 6f 69 6e 74 20 7b  ruct FilePoint {
1f561 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
1f562 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20   iOffset;       
1f563 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f     /* Offset fro
1f564 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  m the beginning 
1f565 6f 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20  of the file */. 
1f566 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75   FileChunk *pChu
1f567 6e 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nk;             
1f568 20 2f 2a 20 53 70 65 63 69 66 69 63 20 63 68 75   /* Specific chu
1f569 6e 6b 20 69 6e 74 6f 20 77 68 69 63 68 20 63 75  nk into which cu
1f56a 72 73 6f 72 20 70 6f 69 6e 74 73 20 2a 2f 0a 7d  rsor points */.}
1f56b 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 75  ;../*.** This su
1f56c 62 63 6c 61 73 73 20 69 73 20 61 20 73 75 62 63  bclass is a subc
1f56d 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f  lass of sqlite3_
1f56e 66 69 6c 65 2e 20 20 45 61 63 68 20 6f 70 65 6e  file.  Each open
1f56f 20 6d 65 6d 6f 72 79 2d 6a 6f 75 72 6e 61 6c 0a   memory-journal.
1f570 2a 2a 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  ** is an instanc
1f571 65 20 6f 66 20 74 68 69 73 20 63 6c 61 73 73 2e  e of this class.
1f572 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 4a 6f  .*/.struct MemJo
1f573 75 72 6e 61 6c 20 7b 0a 20 20 73 71 6c 69 74 65  urnal {.  sqlite
1f574 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d  3_io_methods *pM
1f575 65 74 68 6f 64 3b 20 20 20 20 2f 2a 20 50 61 72  ethod;    /* Par
1f576 65 6e 74 20 63 6c 61 73 73 2e 20 4d 55 53 54 20  ent class. MUST 
1f577 42 45 20 46 49 52 53 54 20 2a 2f 0a 20 20 46 69  BE FIRST */.  Fi
1f578 6c 65 43 68 75 6e 6b 20 2a 70 46 69 72 73 74 3b  leChunk *pFirst;
1f579 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1f57a 20 48 65 61 64 20 6f 66 20 69 6e 2d 6d 65 6d 6f   Head of in-memo
1f57b 72 79 20 63 68 75 6e 6b 2d 6c 69 73 74 20 2a 2f  ry chunk-list */
1f57c 0a 20 20 46 69 6c 65 50 6f 69 6e 74 20 65 6e 64  .  FilePoint end
1f57d 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20  point;          
1f57e 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
1f57f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
1f580 66 69 6c 65 20 2a 2f 0a 20 20 46 69 6c 65 50 6f  file */.  FilePo
1f581 69 6e 74 20 72 65 61 64 70 6f 69 6e 74 3b 20 20  int readpoint;  
1f582 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
1f583 6e 74 65 72 20 74 6f 20 74 68 65 20 65 6e 64 20  nter to the end 
1f584 6f 66 20 74 68 65 20 6c 61 73 74 20 78 52 65 61  of the last xRea
1f585 64 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  d() */.};../*.**
1f586 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20   Read data from 
1f587 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f  the in-memory jo
1f588 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 54 68 69  urnal file.  Thi
1f589 73 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65  s is the impleme
1f58a 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68  ntation.** of th
1f58b 65 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 52  e sqlite3_vfs.xR
1f58c 65 61 64 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73  ead method..*/.s
1f58d 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e  tatic int memjrn
1f58e 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33  lRead(.  sqlite3
1f58f 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20  _file *pJfd,    
1f590 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66  /* The journal f
1f591 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 74  ile from which t
1f592 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64  o read */.  void
1f593 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 20 20   *zBuf,         
1f594 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20 72 65     /* Put the re
1f595 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20  sults here */.  
1f596 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20  int iAmt,       
1f597 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1f598 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61   of bytes to rea
1f599 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e  d */.  sqlite_in
1f59a 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a  t64 iOfst     /*
1f59b 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61   Begin reading a
1f59c 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f  t this offset */
1f59d 0a 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c  .){.  MemJournal
1f59e 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61   *p = (MemJourna
1f59f 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 75 38 20 2a  l *)pJfd;.  u8 *
1f5a0 7a 4f 75 74 20 3d 20 7a 42 75 66 3b 0a 20 20 69  zOut = zBuf;.  i
1f5a1 6e 74 20 6e 52 65 61 64 20 3d 20 69 41 6d 74 3b  nt nRead = iAmt;
1f5a2 0a 20 20 69 6e 74 20 69 43 68 75 6e 6b 4f 66 66  .  int iChunkOff
1f5a3 73 65 74 3b 0a 20 20 46 69 6c 65 43 68 75 6e 6b  set;.  FileChunk
1f5a4 20 2a 70 43 68 75 6e 6b 3b 0a 0a 20 20 2f 2a 20   *pChunk;..  /* 
1f5a5 53 51 4c 69 74 65 20 6e 65 76 65 72 20 74 72 69  SQLite never tri
1f5a6 65 73 20 74 6f 20 72 65 61 64 20 70 61 73 74 20  es to read past 
1f5a7 74 68 65 20 65 6e 64 20 6f 66 20 61 20 72 6f 6c  the end of a rol
1f5a8 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69  lback journal fi
1f5a9 6c 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  le */.  assert( 
1f5aa 69 4f 66 73 74 2b 69 41 6d 74 3c 3d 70 2d 3e 65  iOfst+iAmt<=p->e
1f5ab 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20  ndpoint.iOffset 
1f5ac 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 72 65 61  );..  if( p->rea
1f5ad 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 21 3d  dpoint.iOffset!=
1f5ae 69 4f 66 73 74 20 7c 7c 20 69 4f 66 73 74 3d 3d  iOfst || iOfst==
1f5af 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
1f5b0 5f 69 6e 74 36 34 20 69 4f 66 66 20 3d 20 30 3b  _int64 iOff = 0;
1f5b1 0a 20 20 20 20 66 6f 72 28 70 43 68 75 6e 6b 3d  .    for(pChunk=
1f5b2 70 2d 3e 70 46 69 72 73 74 3b 20 0a 20 20 20 20  p->pFirst; .    
1f5b3 20 20 20 20 41 4c 57 41 59 53 28 70 43 68 75 6e      ALWAYS(pChun
1f5b4 6b 29 20 26 26 20 28 69 4f 66 66 2b 4a 4f 55 52  k) && (iOff+JOUR
1f5b5 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 29 3c 3d  NAL_CHUNKSIZE)<=
1f5b6 69 4f 66 73 74 3b 0a 20 20 20 20 20 20 20 20 70  iOfst;.        p
1f5b7 43 68 75 6e 6b 3d 70 43 68 75 6e 6b 2d 3e 70 4e  Chunk=pChunk->pN
1f5b8 65 78 74 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  ext.    ){.     
1f5b9 20 69 4f 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c   iOff += JOURNAL
1f5ba 5f 43 48 55 4e 4b 53 49 5a 45 3b 0a 20 20 20 20  _CHUNKSIZE;.    
1f5bb 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  }.  }else{.    p
1f5bc 43 68 75 6e 6b 20 3d 20 70 2d 3e 72 65 61 64 70  Chunk = p->readp
1f5bd 6f 69 6e 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 7d  oint.pChunk;.  }
1f5be 0a 0a 20 20 69 43 68 75 6e 6b 4f 66 66 73 65 74  ..  iChunkOffset
1f5bf 20 3d 20 28 69 6e 74 29 28 69 4f 66 73 74 25 4a   = (int)(iOfst%J
1f5c0 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45  OURNAL_CHUNKSIZE
1f5c1 29 3b 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69 6e  );.  do {.    in
1f5c2 74 20 69 53 70 61 63 65 20 3d 20 4a 4f 55 52 4e  t iSpace = JOURN
1f5c3 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69  AL_CHUNKSIZE - i
1f5c4 43 68 75 6e 6b 4f 66 66 73 65 74 3b 0a 20 20 20  ChunkOffset;.   
1f5c5 20 69 6e 74 20 6e 43 6f 70 79 20 3d 20 4d 49 4e   int nCopy = MIN
1f5c6 28 6e 52 65 61 64 2c 20 28 4a 4f 55 52 4e 41 4c  (nRead, (JOURNAL
1f5c7 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68  _CHUNKSIZE - iCh
1f5c8 75 6e 6b 4f 66 66 73 65 74 29 29 3b 0a 20 20 20  unkOffset));.   
1f5c9 20 6d 65 6d 63 70 79 28 7a 4f 75 74 2c 20 26 70   memcpy(zOut, &p
1f5ca 43 68 75 6e 6b 2d 3e 7a 43 68 75 6e 6b 5b 69 43  Chunk->zChunk[iC
1f5cb 68 75 6e 6b 4f 66 66 73 65 74 5d 2c 20 6e 43 6f  hunkOffset], nCo
1f5cc 70 79 29 3b 0a 20 20 20 20 7a 4f 75 74 20 2b 3d  py);.    zOut +=
1f5cd 20 6e 43 6f 70 79 3b 0a 20 20 20 20 6e 52 65 61   nCopy;.    nRea
1f5ce 64 20 2d 3d 20 69 53 70 61 63 65 3b 0a 20 20 20  d -= iSpace;.   
1f5cf 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20   iChunkOffset = 
1f5d0 30 3b 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e 52  0;.  } while( nR
1f5d1 65 61 64 3e 3d 30 20 26 26 20 28 70 43 68 75 6e  ead>=0 && (pChun
1f5d2 6b 3d 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 29  k=pChunk->pNext)
1f5d3 21 3d 30 20 26 26 20 6e 52 65 61 64 3e 30 20 29  !=0 && nRead>0 )
1f5d4 3b 0a 20 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74  ;.  p->readpoint
1f5d5 2e 69 4f 66 66 73 65 74 20 3d 20 69 4f 66 73 74  .iOffset = iOfst
1f5d6 2b 69 41 6d 74 3b 0a 20 20 70 2d 3e 72 65 61 64  +iAmt;.  p->read
1f5d7 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 20 3d 20 70  point.pChunk = p
1f5d8 43 68 75 6e 6b 3b 0a 0a 20 20 72 65 74 75 72 6e  Chunk;..  return
1f5d9 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
1f5da 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20  *.** Write data 
1f5db 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a  to the file..*/.
1f5dc 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72  static int memjr
1f5dd 6e 6c 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74  nlWrite(.  sqlit
1f5de 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20  e3_file *pJfd,  
1f5df 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c    /* The journal
1f5e0 20 66 69 6c 65 20 69 6e 74 6f 20 77 68 69 63 68   file into which
1f5e1 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 63   to write */.  c
1f5e2 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c  onst void *zBuf,
1f5e3 20 20 20 20 20 20 2f 2a 20 54 61 6b 65 20 64 61        /* Take da
1f5e4 74 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e  ta to be written
1f5e5 20 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 20 20   from here */.  
1f5e6 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20  int iAmt,       
1f5e7 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1f5e8 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69   of bytes to wri
1f5e9 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69  te */.  sqlite_i
1f5ea 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f  nt64 iOfst     /
1f5eb 2a 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67 20  * Begin writing 
1f5ec 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 69  at this offset i
1f5ed 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a  nto the file */.
1f5ee 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20  ){.  MemJournal 
1f5ef 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c  *p = (MemJournal
1f5f0 20 2a 29 70 4a 66 64 3b 0a 20 20 69 6e 74 20 6e   *)pJfd;.  int n
1f5f1 57 72 69 74 65 20 3d 20 69 41 6d 74 3b 0a 20 20  Write = iAmt;.  
1f5f2 75 38 20 2a 7a 57 72 69 74 65 20 3d 20 28 75 38  u8 *zWrite = (u8
1f5f3 20 2a 29 7a 42 75 66 3b 0a 0a 20 20 2f 2a 20 41   *)zBuf;..  /* A
1f5f4 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72  n in-memory jour
1f5f5 6e 61 6c 20 66 69 6c 65 20 73 68 6f 75 6c 64 20  nal file should 
1f5f6 6f 6e 6c 79 20 65 76 65 72 20 62 65 20 61 70 70  only ever be app
1f5f7 65 6e 64 65 64 20 74 6f 2e 20 52 61 6e 64 6f 6d  ended to. Random
1f5f8 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 77 72 69  .  ** access wri
1f5f9 74 65 73 20 61 72 65 20 6e 6f 74 20 72 65 71 75  tes are not requ
1f5fa 69 72 65 64 20 62 79 20 73 71 6c 69 74 65 2e 0a  ired by sqlite..
1f5fb 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69    */.  assert( i
1f5fc 4f 66 73 74 3d 3d 70 2d 3e 65 6e 64 70 6f 69 6e  Ofst==p->endpoin
1f5fd 74 2e 69 4f 66 66 73 65 74 20 29 3b 0a 20 20 55  t.iOffset );.  U
1f5fe 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
1f5ff 69 4f 66 73 74 29 3b 0a 0a 20 20 77 68 69 6c 65  iOfst);..  while
1f600 28 20 6e 57 72 69 74 65 3e 30 20 29 7b 0a 20 20  ( nWrite>0 ){.  
1f601 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68    FileChunk *pCh
1f602 75 6e 6b 20 3d 20 70 2d 3e 65 6e 64 70 6f 69 6e  unk = p->endpoin
1f603 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 20 20 69 6e  t.pChunk;.    in
1f604 74 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d  t iChunkOffset =
1f605 20 28 69 6e 74 29 28 70 2d 3e 65 6e 64 70 6f 69   (int)(p->endpoi
1f606 6e 74 2e 69 4f 66 66 73 65 74 25 4a 4f 55 52 4e  nt.iOffset%JOURN
1f607 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 29 3b 0a 20  AL_CHUNKSIZE);. 
1f608 20 20 20 69 6e 74 20 69 53 70 61 63 65 20 3d 20     int iSpace = 
1f609 4d 49 4e 28 6e 57 72 69 74 65 2c 20 4a 4f 55 52  MIN(nWrite, JOUR
1f60a 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20  NAL_CHUNKSIZE - 
1f60b 69 43 68 75 6e 6b 4f 66 66 73 65 74 29 3b 0a 0a  iChunkOffset);..
1f60c 20 20 20 20 69 66 28 20 69 43 68 75 6e 6b 4f 66      if( iChunkOf
1f60d 66 73 65 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  fset==0 ){.     
1f60e 20 2f 2a 20 4e 65 77 20 63 68 75 6e 6b 20 69 73   /* New chunk is
1f60f 20 72 65 71 75 69 72 65 64 20 74 6f 20 65 78 74   required to ext
1f610 65 6e 64 20 74 68 65 20 66 69 6c 65 2e 20 2a 2f  end the file. */
1f611 0a 20 20 20 20 20 20 46 69 6c 65 43 68 75 6e 6b  .      FileChunk
1f612 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33   *pNew = sqlite3
1f613 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46  _malloc(sizeof(F
1f614 69 6c 65 43 68 75 6e 6b 29 29 3b 0a 20 20 20 20  ileChunk));.    
1f615 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20    if( !pNew ){. 
1f616 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
1f617 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d  LITE_IOERR_NOMEM
1f618 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1f619 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d 20 30 3b  pNew->pNext = 0;
1f61a 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 75 6e  .      if( pChun
1f61b 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  k ){.        ass
1f61c 65 72 74 28 20 70 2d 3e 70 46 69 72 73 74 20 29  ert( p->pFirst )
1f61d 3b 0a 20 20 20 20 20 20 20 20 70 43 68 75 6e 6b  ;.        pChunk
1f61e 2d 3e 70 4e 65 78 74 20 3d 20 70 4e 65 77 3b 0a  ->pNext = pNew;.
1f61f 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1f620 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 2d       assert( !p-
1f621 3e 70 46 69 72 73 74 20 29 3b 0a 20 20 20 20 20  >pFirst );.     
1f622 20 20 20 70 2d 3e 70 46 69 72 73 74 20 3d 20 70     p->pFirst = p
1f623 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  New;.      }.   
1f624 20 20 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70     p->endpoint.p
1f625 43 68 75 6e 6b 20 3d 20 70 4e 65 77 3b 0a 20 20  Chunk = pNew;.  
1f626 20 20 7d 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28    }..    memcpy(
1f627 26 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68  &p->endpoint.pCh
1f628 75 6e 6b 2d 3e 7a 43 68 75 6e 6b 5b 69 43 68 75  unk->zChunk[iChu
1f629 6e 6b 4f 66 66 73 65 74 5d 2c 20 7a 57 72 69 74  nkOffset], zWrit
1f62a 65 2c 20 69 53 70 61 63 65 29 3b 0a 20 20 20 20  e, iSpace);.    
1f62b 7a 57 72 69 74 65 20 2b 3d 20 69 53 70 61 63 65  zWrite += iSpace
1f62c 3b 0a 20 20 20 20 6e 57 72 69 74 65 20 2d 3d 20  ;.    nWrite -= 
1f62d 69 53 70 61 63 65 3b 0a 20 20 20 20 70 2d 3e 65  iSpace;.    p->e
1f62e 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20  ndpoint.iOffset 
1f62f 2b 3d 20 69 53 70 61 63 65 3b 0a 20 20 7d 0a 0a  += iSpace;.  }..
1f630 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1f631 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75  OK;.}../*.** Tru
1f632 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65 2e 0a  ncate the file..
1f633 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65  */.static int me
1f634 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 65 28 73 71  mjrnlTruncate(sq
1f635 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64  lite3_file *pJfd
1f636 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73  , sqlite_int64 s
1f637 69 7a 65 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e  ize){.  MemJourn
1f638 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72  al *p = (MemJour
1f639 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 46 69  nal *)pJfd;.  Fi
1f63a 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b  leChunk *pChunk;
1f63b 0a 20 20 61 73 73 65 72 74 28 73 69 7a 65 3d 3d  .  assert(size==
1f63c 30 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  0);.  UNUSED_PAR
1f63d 41 4d 45 54 45 52 28 73 69 7a 65 29 3b 0a 20 20  AMETER(size);.  
1f63e 70 43 68 75 6e 6b 20 3d 20 70 2d 3e 70 46 69 72  pChunk = p->pFir
1f63f 73 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 43 68  st;.  while( pCh
1f640 75 6e 6b 20 29 7b 0a 20 20 20 20 46 69 6c 65 43  unk ){.    FileC
1f641 68 75 6e 6b 20 2a 70 54 6d 70 20 3d 20 70 43 68  hunk *pTmp = pCh
1f642 75 6e 6b 3b 0a 20 20 20 20 70 43 68 75 6e 6b 20  unk;.    pChunk 
1f643 3d 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 3b  = pChunk->pNext;
1f644 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
1f645 65 28 70 54 6d 70 29 3b 0a 20 20 7d 0a 20 20 73  e(pTmp);.  }.  s
1f646 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c  qlite3MemJournal
1f647 4f 70 65 6e 28 70 4a 66 64 29 3b 0a 20 20 72 65  Open(pJfd);.  re
1f648 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1f649 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74  }../*.** Close t
1f64a 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74  he file..*/.stat
1f64b 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 43 6c  ic int memjrnlCl
1f64c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ose(sqlite3_file
1f64d 20 2a 70 4a 66 64 29 7b 0a 20 20 6d 65 6d 6a 72   *pJfd){.  memjr
1f64e 6e 6c 54 72 75 6e 63 61 74 65 28 70 4a 66 64 2c  nlTruncate(pJfd,
1f64f 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51   0);.  return SQ
1f650 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a  LITE_OK;.}.../*.
1f651 2a 2a 20 53 79 6e 63 20 74 68 65 20 66 69 6c 65  ** Sync the file
1f652 2e 0a 2a 2a 0a 2a 2a 20 53 79 6e 63 69 6e 67 20  ..**.** Syncing 
1f653 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75  an in-memory jou
1f654 72 6e 61 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  rnal is a no-op.
1f655 20 20 41 6e 64 2c 20 69 6e 20 66 61 63 74 2c 20    And, in fact, 
1f656 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
1f657 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20  is never called 
1f658 69 6e 20 61 20 77 6f 72 6b 69 6e 67 20 69 6d 70  in a working imp
1f659 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68  lementation.  Th
1f65a 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  is implementatio
1f65b 6e 0a 2a 2a 20 65 78 69 73 74 73 20 70 75 72 65  n.** exists pure
1f65c 6c 79 20 61 73 20 61 20 63 6f 6e 74 69 6e 67 65  ly as a continge
1f65d 6e 63 79 2c 20 69 6e 20 63 61 73 65 20 73 6f 6d  ncy, in case som
1f65e 65 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 20 69 6e  e malfunction in
1f65f 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70   some other.** p
1f660 61 72 74 20 6f 66 20 53 51 4c 69 74 65 20 63 61  art of SQLite ca
1f661 75 73 65 73 20 53 79 6e 63 20 74 6f 20 62 65 20  uses Sync to be 
1f662 63 61 6c 6c 65 64 20 62 79 20 6d 69 73 74 61 6b  called by mistak
1f663 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1f664 20 6d 65 6d 6a 72 6e 6c 53 79 6e 63 28 73 71 6c   memjrnlSync(sql
1f665 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73  ite3_file *NotUs
1f666 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32  ed, int NotUsed2
1f667 29 7b 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f  ){   /*NO_TEST*/
1f668 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
1f669 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
1f66a 74 55 73 65 64 32 29 3b 20 20 20 20 20 20 20 20  tUsed2);        
1f66b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f66c 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20 61 73  /*NO_TEST*/.  as
1f66d 73 65 72 74 28 20 30 20 29 3b 20 20 20 20 20 20  sert( 0 );      
1f66e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f66f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f670 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f             /*NO_
1f671 54 45 53 54 2a 2f 0a 20 20 72 65 74 75 72 6e 20  TEST*/.  return 
1f672 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
1f673 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f674 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f675 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a        /*NO_TEST*
1f676 2f 0a 7d 20 20 20 20 20 20 20 20 20 20 20 20 20  /.}             
1f677 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f678 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f679 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f67a 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 0a 2f 2a   /*NO_TEST*/../*
1f67b 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 73 69  .** Query the si
1f67c 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69  ze of the file i
1f67d 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74  n bytes..*/.stat
1f67e 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 46 69  ic int memjrnlFi
1f67f 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66  leSize(sqlite3_f
1f680 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74  ile *pJfd, sqlit
1f681 65 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b  e_int64 *pSize){
1f682 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70  .  MemJournal *p
1f683 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a   = (MemJournal *
1f684 29 70 4a 66 64 3b 0a 20 20 2a 70 53 69 7a 65 20  )pJfd;.  *pSize 
1f685 3d 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29  = (sqlite_int64)
1f686 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66   p->endpoint.iOf
1f687 66 73 65 74 3b 0a 20 20 72 65 74 75 72 6e 20 53  fset;.  return S
1f688 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
1f689 2a 2a 20 54 61 62 6c 65 20 6f 66 20 6d 65 74 68  ** Table of meth
1f68a 6f 64 73 20 66 6f 72 20 4d 65 6d 4a 6f 75 72 6e  ods for MemJourn
1f68b 61 6c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  al sqlite3_file 
1f68c 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69  object..*/.stati
1f68d 63 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  c struct sqlite3
1f68e 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4d 65 6d 4a  _io_methods MemJ
1f68f 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 20 3d 20  ournalMethods = 
1f690 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20  {.  1,          
1f691 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
1f692 6e 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 43 6c  n */.  memjrnlCl
1f693 6f 73 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f  ose,     /* xClo
1f694 73 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 52  se */.  memjrnlR
1f695 65 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 65  ead,      /* xRe
1f696 61 64 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 57  ad */.  memjrnlW
1f697 72 69 74 65 2c 20 20 20 20 20 2f 2a 20 78 57 72  rite,     /* xWr
1f698 69 74 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c  ite */.  memjrnl
1f699 54 72 75 6e 63 61 74 65 2c 20 20 2f 2a 20 78 54  Truncate,  /* xT
1f69a 72 75 6e 63 61 74 65 20 2a 2f 0a 20 20 6d 65 6d  runcate */.  mem
1f69b 6a 72 6e 6c 53 79 6e 63 2c 20 20 20 20 20 20 2f  jrnlSync,      /
1f69c 2a 20 78 53 79 6e 63 20 2a 2f 0a 20 20 6d 65 6d  * xSync */.  mem
1f69d 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f  jrnlFileSize,  /
1f69e 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20  * xFileSize */. 
1f69f 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
1f6a0 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20     /* xLock */. 
1f6a1 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
1f6a2 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f     /* xUnlock */
1f6a3 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20  .  0,           
1f6a4 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65       /* xCheckRe
1f6a5 73 65 72 76 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20  servedLock */.  
1f6a6 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
1f6a7 20 20 2f 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f    /* xFileContro
1f6a8 6c 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20  l */.  0,       
1f6a9 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63           /* xSec
1f6aa 74 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 20 20  torSize */.  0  
1f6ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1f6ac 2a 20 78 44 65 76 69 63 65 43 68 61 72 61 63 74  * xDeviceCharact
1f6ad 65 72 69 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a  eristics */.};..
1f6ae 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f  /* .** Open a jo
1f6af 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53  urnal file..*/.S
1f6b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1f6b1 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75  id sqlite3MemJou
1f6b2 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33  rnalOpen(sqlite3
1f6b3 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20  _file *pJfd){.  
1f6b4 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20  MemJournal *p = 
1f6b5 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a  (MemJournal *)pJ
1f6b6 66 64 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49  fd;.  assert( EI
1f6b7 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45  GHT_BYTE_ALIGNME
1f6b8 4e 54 28 70 29 20 29 3b 0a 20 20 6d 65 6d 73 65  NT(p) );.  memse
1f6b9 74 28 70 2c 20 30 2c 20 73 71 6c 69 74 65 33 4d  t(p, 0, sqlite3M
1f6ba 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29 29  emJournalSize())
1f6bb 3b 0a 20 20 70 2d 3e 70 4d 65 74 68 6f 64 20 3d  ;.  p->pMethod =
1f6bc 20 26 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68   &MemJournalMeth
1f6bd 6f 64 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ods;.}../*.** Re
1f6be 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65  turn true if the
1f6bf 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 70 61 73   file-handle pas
1f6c0 73 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65  sed as an argume
1f6c1 6e 74 20 69 73 20 0a 2a 2a 20 61 6e 20 69 6e 2d  nt is .** an in-
1f6c2 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 0a  memory journal .
1f6c3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1f6c4 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4d  E int sqlite3IsM
1f6c5 65 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65  emJournal(sqlite
1f6c6 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20  3_file *pJfd){. 
1f6c7 20 72 65 74 75 72 6e 20 70 4a 66 64 2d 3e 70 4d   return pJfd->pM
1f6c8 65 74 68 6f 64 73 3d 3d 26 4d 65 6d 4a 6f 75 72  ethods==&MemJour
1f6c9 6e 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 2f  nalMethods;.}../
1f6ca 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  * .** Return the
1f6cb 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
1f6cc 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f   required to sto
1f6cd 72 65 20 61 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20  re a MemJournal 
1f6ce 74 68 61 74 20 75 73 65 73 20 76 66 73 0a 2a 2a  that uses vfs.**
1f6cf 20 70 56 66 73 20 74 6f 20 63 72 65 61 74 65 20   pVfs to create 
1f6d0 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f  the underlying o
1f6d1 6e 2d 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f  n-disk files..*/
1f6d2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1f6d3 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f  int sqlite3MemJo
1f6d4 75 72 6e 61 6c 53 69 7a 65 28 76 6f 69 64 29 7b  urnalSize(void){
1f6d5 0a 20 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66  .  return sizeof
1f6d6 28 4d 65 6d 4a 6f 75 72 6e 61 6c 29 3b 0a 7d 0a  (MemJournal);.}.
1f6d7 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1f6d8 20 45 6e 64 20 6f 66 20 6d 65 6d 6a 6f 75 72 6e   End of memjourn
1f6d9 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  al.c ***********
1f6da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f6db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1f6dc 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1f6dd 20 42 65 67 69 6e 20 66 69 6c 65 20 77 61 6c 6b   Begin file walk
1f6de 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.c ***********
1f6df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1f6e1 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75  ./*.** 2008 Augu
1f6e2 73 74 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20  st 16.**.** The 
1f6e3 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
1f6e4 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
1f6e5 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
1f6e6 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
1f6e7 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
1f6e8 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
1f6e9 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
1f6ea 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
1f6eb 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
1f6ec 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
1f6ed 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
1f6ee 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
1f6ef 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
1f6f0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
1f6f1 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
1f6f2 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
1f6f3 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
1f6f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f6f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f6f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f6f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f6f8 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
1f6f9 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 69  e contains routi
1f6fa 6e 65 73 20 75 73 65 64 20 66 6f 72 20 77 61 6c  nes used for wal
1f6fb 6b 69 6e 67 20 74 68 65 20 70 61 72 73 65 72 20  king the parser 
1f6fc 74 72 65 65 20 66 6f 72 0a 2a 2a 20 61 6e 20 53  tree for.** an S
1f6fd 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  QL statement..**
1f6fe 0a 2a 2a 20 24 49 64 3a 20 77 61 6c 6b 65 72 2e  .** $Id: walker.
1f6ff 63 2c 76 20 31 2e 37 20 32 30 30 39 2f 30 36 2f  c,v 1.7 2009/06/
1f700 31 35 20 32 33 3a 31 35 3a 35 39 20 64 72 68 20  15 23:15:59 drh 
1f701 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a  Exp $.*/.../*.**
1f702 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73   Walk an express
1f703 69 6f 6e 20 74 72 65 65 2e 20 20 49 6e 76 6f 6b  ion tree.  Invok
1f704 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f  e the callback o
1f705 6e 63 65 20 66 6f 72 20 65 61 63 68 20 6e 6f 64  nce for each nod
1f706 65 0a 2a 2a 20 6f 66 20 74 68 65 20 65 78 70 72  e.** of the expr
1f707 65 73 73 69 6f 6e 2c 20 77 68 69 6c 65 20 64 65  ession, while de
1f708 63 65 6e 64 69 6e 67 2e 20 20 28 49 6e 20 6f 74  cending.  (In ot
1f709 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 63  her words, the c
1f70a 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 69 6e  allback.** is in
1f70b 76 6f 6b 65 64 20 62 65 66 6f 72 65 20 76 69 73  voked before vis
1f70c 69 74 69 6e 67 20 63 68 69 6c 64 72 65 6e 2e 29  iting children.)
1f70d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  .**.** The retur
1f70e 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65  n value from the
1f70f 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64   callback should
1f710 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 57   be one of the W
1f711 52 43 5f 2a 0a 2a 2a 20 63 6f 6e 73 74 61 6e 74  RC_*.** constant
1f712 73 20 74 6f 20 73 70 65 63 69 66 79 20 68 6f 77  s to specify how
1f713 20 74 6f 20 70 72 6f 63 65 65 64 20 77 69 74 68   to proceed with
1f714 20 74 68 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a 2a   the walk..**.**
1f715 20 20 20 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65      WRC_Continue
1f716 20 20 20 20 20 20 43 6f 6e 74 69 6e 75 65 20 64        Continue d
1f717 65 73 63 65 6e 64 69 6e 67 20 64 6f 77 6e 20 74  escending down t
1f718 68 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 20  he tree..**.**  
1f719 20 20 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20    WRC_Prune     
1f71a 20 20 20 20 44 6f 20 6e 6f 74 20 64 65 73 63 65      Do not desce
1f71b 6e 64 20 69 6e 74 6f 20 63 68 69 6c 64 20 6e 6f  nd into child no
1f71c 64 65 73 2e 20 20 42 75 74 20 61 6c 6c 6f 77 0a  des.  But allow.
1f71d 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
1f71e 20 20 20 20 20 20 20 20 74 68 65 20 77 61 6c 6b          the walk
1f71f 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74   to continue wit
1f720 68 20 73 69 62 6c 69 6e 67 20 6e 6f 64 65 73 2e  h sibling nodes.
1f721 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 41 62  .**.**    WRC_Ab
1f722 6f 72 74 20 20 20 20 20 20 20 20 20 44 6f 20 6e  ort         Do n
1f723 6f 20 6d 6f 72 65 20 63 61 6c 6c 62 61 63 6b 73  o more callbacks
1f724 2e 20 20 55 6e 77 69 6e 64 20 74 68 65 20 73 74  .  Unwind the st
1f725 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20  ack and.**      
1f726 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f727 72 65 74 75 72 6e 20 74 68 65 20 74 6f 70 2d 6c  return the top-l
1f728 65 76 65 6c 20 77 61 6c 6b 20 63 61 6c 6c 2e 0a  evel walk call..
1f729 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  **.** The return
1f72a 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 69 73   value from this
1f72b 20 72 6f 75 74 69 6e 65 20 69 73 20 57 52 43 5f   routine is WRC_
1f72c 41 62 6f 72 74 20 74 6f 20 61 62 61 6e 64 6f 6e  Abort to abandon
1f72d 20 74 68 65 20 74 72 65 65 20 77 61 6c 6b 0a 2a   the tree walk.*
1f72e 2a 20 61 6e 64 20 57 52 43 5f 43 6f 6e 74 69 6e  * and WRC_Contin
1f72f 75 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 2e 0a  ue to continue..
1f730 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1f731 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c  E int sqlite3Wal
1f732 6b 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57  kExpr(Walker *pW
1f733 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78  alker, Expr *pEx
1f734 70 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  pr){.  int rc;. 
1f735 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20   if( pExpr==0 ) 
1f736 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69  return WRC_Conti
1f737 6e 75 65 3b 0a 20 20 74 65 73 74 63 61 73 65 28  nue;.  testcase(
1f738 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
1f739 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e  (pExpr, EP_Token
1f73a 4f 6e 6c 79 29 20 29 3b 0a 20 20 74 65 73 74 63  Only) );.  testc
1f73b 61 73 65 28 20 45 78 70 72 48 61 73 50 72 6f 70  ase( ExprHasProp
1f73c 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 52  erty(pExpr, EP_R
1f73d 65 64 75 63 65 64 29 20 29 3b 0a 20 20 72 63 20  educed) );.  rc 
1f73e 3d 20 70 57 61 6c 6b 65 72 2d 3e 78 45 78 70 72  = pWalker->xExpr
1f73f 43 61 6c 6c 62 61 63 6b 28 70 57 61 6c 6b 65 72  Callback(pWalker
1f740 2c 20 70 45 78 70 72 29 3b 0a 20 20 69 66 28 20  , pExpr);.  if( 
1f741 72 63 3d 3d 57 52 43 5f 43 6f 6e 74 69 6e 75 65  rc==WRC_Continue
1f742 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26  .              &
1f743 26 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f  & !ExprHasAnyPro
1f744 70 65 72 74 79 28 70 45 78 70 72 2c 45 50 5f 54  perty(pExpr,EP_T
1f745 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20  okenOnly) ){.   
1f746 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b   if( sqlite3Walk
1f747 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 45  Expr(pWalker, pE
1f748 78 70 72 2d 3e 70 4c 65 66 74 29 20 29 20 72 65  xpr->pLeft) ) re
1f749 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
1f74a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57      if( sqlite3W
1f74b 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c  alkExpr(pWalker,
1f74c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29 20   pExpr->pRight) 
1f74d 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
1f74e 72 74 3b 0a 20 20 20 20 69 66 28 20 45 78 70 72  rt;.    if( Expr
1f74f 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
1f750 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
1f751 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71   ){.      if( sq
1f752 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28  lite3WalkSelect(
1f753 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e  pWalker, pExpr->
1f754 78 2e 70 53 65 6c 65 63 74 29 20 29 20 72 65 74  x.pSelect) ) ret
1f755 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
1f756 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1f757 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45  if( sqlite3WalkE
1f758 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c  xprList(pWalker,
1f759 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 29   pExpr->x.pList)
1f75a 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
1f75b 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ort;.    }.  }. 
1f75c 20 72 65 74 75 72 6e 20 72 63 20 26 20 57 52 43   return rc & WRC
1f75d 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  _Abort;.}../*.**
1f75e 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 57 61 6c   Call sqlite3Wal
1f75f 6b 45 78 70 72 28 29 20 66 6f 72 20 65 76 65 72  kExpr() for ever
1f760 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  y expression in 
1f761 6c 69 73 74 20 70 20 6f 72 20 75 6e 74 69 6c 0a  list p or until.
1f762 2a 2a 20 61 6e 20 61 62 6f 72 74 20 72 65 71 75  ** an abort requ
1f763 65 73 74 20 69 73 20 73 65 65 6e 2e 0a 2a 2f 0a  est is seen..*/.
1f764 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1f765 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78  nt sqlite3WalkEx
1f766 70 72 4c 69 73 74 28 57 61 6c 6b 65 72 20 2a 70  prList(Walker *p
1f767 57 61 6c 6b 65 72 2c 20 45 78 70 72 4c 69 73 74  Walker, ExprList
1f768 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20   *p){.  int i;. 
1f769 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
1f76a 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20  _item *pItem;.  
1f76b 69 66 28 20 70 20 29 7b 0a 20 20 20 20 66 6f 72  if( p ){.    for
1f76c 28 69 3d 70 2d 3e 6e 45 78 70 72 2c 20 70 49 74  (i=p->nExpr, pIt
1f76d 65 6d 3d 70 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d  em=p->a; i>0; i-
1f76e 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  -, pItem++){.   
1f76f 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61     if( sqlite3Wa
1f770 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20  lkExpr(pWalker, 
1f771 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 20 29 20  pItem->pExpr) ) 
1f772 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
1f773 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
1f774 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75  turn WRC_Continu
1f775 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b  e;.}../*.** Walk
1f776 20 61 6c 6c 20 65 78 70 72 65 73 73 69 6f 6e 73   all expressions
1f777 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
1f778 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1f779 74 20 70 2e 20 20 44 6f 0a 2a 2a 20 6e 6f 74 20  t p.  Do.** not 
1f77a 69 6e 76 6f 6b 65 20 74 68 65 20 53 45 4c 45 43  invoke the SELEC
1f77b 54 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 70 2c  T callback on p,
1f77c 20 62 75 74 20 64 6f 20 28 6f 66 20 63 6f 75 72   but do (of cour
1f77d 73 65 29 20 69 6e 76 6f 6b 65 0a 2a 2a 20 61 6e  se) invoke.** an
1f77e 79 20 65 78 70 72 20 63 61 6c 6c 62 61 63 6b 73  y expr callbacks
1f77f 20 61 6e 64 20 53 45 4c 45 43 54 20 63 61 6c 6c   and SELECT call
1f780 62 61 63 6b 73 20 74 68 61 74 20 63 6f 6d 65 20  backs that come 
1f781 66 72 6f 6d 20 73 75 62 71 75 65 72 69 65 73 2e  from subqueries.
1f782 0a 2a 2a 20 52 65 74 75 72 6e 20 57 52 43 5f 41  .** Return WRC_A
1f783 62 6f 72 74 20 6f 72 20 57 52 43 5f 43 6f 6e 74  bort or WRC_Cont
1f784 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  inue..*/.SQLITE_
1f785 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1f786 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 70  te3WalkSelectExp
1f787 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65  r(Walker *pWalke
1f788 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20  r, Select *p){. 
1f789 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b   if( sqlite3Walk
1f78a 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72  ExprList(pWalker
1f78b 2c 20 70 2d 3e 70 45 4c 69 73 74 29 20 29 20 72  , p->pEList) ) r
1f78c 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
1f78d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61  .  if( sqlite3Wa
1f78e 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20  lkExpr(pWalker, 
1f78f 70 2d 3e 70 57 68 65 72 65 29 20 29 20 72 65 74  p->pWhere) ) ret
1f790 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
1f791 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b   if( sqlite3Walk
1f792 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72  ExprList(pWalker
1f793 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 29 20 29  , p->pGroupBy) )
1f794 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
1f795 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  t;.  if( sqlite3
1f796 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72  WalkExpr(pWalker
1f797 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 20 29 20  , p->pHaving) ) 
1f798 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
1f799 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57  ;.  if( sqlite3W
1f79a 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c  alkExprList(pWal
1f79b 6b 65 72 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79  ker, p->pOrderBy
1f79c 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
1f79d 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69  bort;.  if( sqli
1f79e 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c  te3WalkExpr(pWal
1f79f 6b 65 72 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 20  ker, p->pLimit) 
1f7a0 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
1f7a1 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  rt;.  if( sqlite
1f7a2 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65  3WalkExpr(pWalke
1f7a3 72 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29 20 29  r, p->pOffset) )
1f7a4 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
1f7a5 74 3b 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f  t;.  return WRC_
1f7a6 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a  Continue;.}../*.
1f7a7 2a 2a 20 57 61 6c 6b 20 74 68 65 20 70 61 72 73  ** Walk the pars
1f7a8 65 20 74 72 65 65 73 20 61 73 73 6f 63 69 61 74  e trees associat
1f7a9 65 64 20 77 69 74 68 20 61 6c 6c 20 73 75 62 71  ed with all subq
1f7aa 75 65 72 69 65 73 20 69 6e 20 74 68 65 0a 2a 2a  ueries in the.**
1f7ab 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
1f7ac 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
1f7ad 20 70 2e 20 20 44 6f 20 6e 6f 74 20 69 6e 76 6f   p.  Do not invo
1f7ae 6b 65 20 74 68 65 20 73 65 6c 65 63 74 0a 2a 2a  ke the select.**
1f7af 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 70 2c 20   callback on p, 
1f7b0 62 75 74 20 64 6f 20 69 6e 76 6f 6b 65 20 69 74  but do invoke it
1f7b1 20 6f 6e 20 65 61 63 68 20 46 52 4f 4d 20 63 6c   on each FROM cl
1f7b2 61 75 73 65 20 73 75 62 71 75 65 72 79 0a 2a 2a  ause subquery.**
1f7b3 20 61 6e 64 20 6f 6e 20 61 6e 79 20 73 75 62 71   and on any subq
1f7b4 75 65 72 69 65 73 20 66 75 72 74 68 65 72 20 64  ueries further d
1f7b5 6f 77 6e 20 69 6e 20 74 68 65 20 74 72 65 65 2e  own in the tree.
1f7b6 20 20 52 65 74 75 72 6e 20 0a 2a 2a 20 57 52 43    Return .** WRC
1f7b7 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 5f 43 6f  _Abort or WRC_Co
1f7b8 6e 74 69 6e 75 65 3b 0a 2a 2f 0a 53 51 4c 49 54  ntinue;.*/.SQLIT
1f7b9 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1f7ba 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46  lite3WalkSelectF
1f7bb 72 6f 6d 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c  rom(Walker *pWal
1f7bc 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b  ker, Select *p){
1f7bd 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  .  SrcList *pSrc
1f7be 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72  ;.  int i;.  str
1f7bf 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
1f7c0 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 70 53 72 63   *pItem;..  pSrc
1f7c1 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 69 66   = p->pSrc;.  if
1f7c2 28 20 41 4c 57 41 59 53 28 70 53 72 63 29 20 29  ( ALWAYS(pSrc) )
1f7c3 7b 0a 20 20 20 20 66 6f 72 28 69 3d 70 53 72 63  {.    for(i=pSrc
1f7c4 2d 3e 6e 53 72 63 2c 20 70 49 74 65 6d 3d 70 53  ->nSrc, pItem=pS
1f7c5 72 63 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c  rc->a; i>0; i--,
1f7c6 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
1f7c7 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b   if( sqlite3Walk
1f7c8 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20  Select(pWalker, 
1f7c9 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29 20  pItem->pSelect) 
1f7ca 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
1f7cb 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20  n WRC_Abort;.   
1f7cc 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
1f7cd 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74   return WRC_Cont
1f7ce 69 6e 75 65 3b 0a 7d 20 0a 0a 2f 2a 0a 2a 2a 20  inue;.} ../*.** 
1f7cf 43 61 6c 6c 20 73 71 6c 69 74 65 33 57 61 6c 6b  Call sqlite3Walk
1f7d0 45 78 70 72 28 29 20 66 6f 72 20 65 76 65 72 79  Expr() for every
1f7d1 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 53   expression in S
1f7d2 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20  elect statement 
1f7d3 70 2e 0a 2a 2a 20 49 6e 76 6f 6b 65 20 73 71 6c  p..** Invoke sql
1f7d4 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 29  ite3WalkSelect()
1f7d5 20 66 6f 72 20 73 75 62 71 75 65 72 69 65 73 20   for subqueries 
1f7d6 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
1f7d7 73 65 20 61 6e 64 0a 2a 2a 20 6f 6e 20 74 68 65  se and.** on the
1f7d8 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74   compound select
1f7d9 20 63 68 61 69 6e 2c 20 70 2d 3e 70 50 72 69 6f   chain, p->pPrio
1f7da 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  r..**.** Return 
1f7db 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 75 6e 64  WRC_Continue und
1f7dc 65 72 20 6e 6f 72 6d 61 6c 20 63 6f 6e 64 69 74  er normal condit
1f7dd 69 6f 6e 73 2e 20 20 52 65 74 75 72 6e 20 57 52  ions.  Return WR
1f7de 43 5f 41 62 6f 72 74 20 69 66 0a 2a 2a 20 74 68  C_Abort if.** th
1f7df 65 72 65 20 69 73 20 61 6e 20 61 62 6f 72 74 20  ere is an abort 
1f7e0 72 65 71 75 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 49  request..**.** I
1f7e1 66 20 74 68 65 20 57 61 6c 6b 65 72 20 64 6f 65  f the Walker doe
1f7e2 73 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 78 53  s not have an xS
1f7e3 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 28 29 20  electCallback() 
1f7e4 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  then this routin
1f7e5 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 20  e.** is a no-op 
1f7e6 72 65 74 75 72 6e 69 6e 67 20 57 52 43 5f 43 6f  returning WRC_Co
1f7e7 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54  ntinue..*/.SQLIT
1f7e8 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1f7e9 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28  lite3WalkSelect(
1f7ea 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c  Walker *pWalker,
1f7eb 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 69   Select *p){.  i
1f7ec 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 3d 3d  nt rc;.  if( p==
1f7ed 30 20 7c 7c 20 70 57 61 6c 6b 65 72 2d 3e 78 53  0 || pWalker->xS
1f7ee 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 3d 3d 30  electCallback==0
1f7ef 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f   ) return WRC_Co
1f7f0 6e 74 69 6e 75 65 3b 0a 20 20 72 63 20 3d 20 57  ntinue;.  rc = W
1f7f1 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 77  RC_Continue;.  w
1f7f2 68 69 6c 65 28 20 70 20 20 29 7b 0a 20 20 20 20  hile( p  ){.    
1f7f3 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 78 53  rc = pWalker->xS
1f7f4 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 28 70 57  electCallback(pW
1f7f5 61 6c 6b 65 72 2c 20 70 29 3b 0a 20 20 20 20 69  alker, p);.    i
1f7f6 66 28 20 72 63 20 29 20 62 72 65 61 6b 3b 0a 20  f( rc ) break;. 
1f7f7 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61     if( sqlite3Wa
1f7f8 6c 6b 53 65 6c 65 63 74 45 78 70 72 28 70 57 61  lkSelectExpr(pWa
1f7f9 6c 6b 65 72 2c 20 70 29 20 29 20 72 65 74 75 72  lker, p) ) retur
1f7fa 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20  n WRC_Abort;.   
1f7fb 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b   if( sqlite3Walk
1f7fc 53 65 6c 65 63 74 46 72 6f 6d 28 70 57 61 6c 6b  SelectFrom(pWalk
1f7fd 65 72 2c 20 70 29 20 29 20 72 65 74 75 72 6e 20  er, p) ) return 
1f7fe 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 70  WRC_Abort;.    p
1f7ff 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20   = p->pPrior;.  
1f800 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 20  }.  return rc & 
1f801 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a  WRC_Abort;.}../*
1f802 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
1f803 64 20 6f 66 20 77 61 6c 6b 65 72 2e 63 20 2a 2a  d of walker.c **
1f804 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f805 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f806 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1f807 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
1f808 67 69 6e 20 66 69 6c 65 20 72 65 73 6f 6c 76 65  gin file resolve
1f809 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1f80a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f80b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1f80c 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 73 74 20  .** 2008 August 
1f80d 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  18.**.** The aut
1f80e 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
1f80f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
1f810 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
1f811 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
1f812 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
1f813 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
1f814 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
1f815 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
1f816 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
1f817 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
1f818 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
1f819 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
1f81a 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
1f81b 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
1f81c 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
1f81d 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
1f81e 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
1f81f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f821 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f822 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f823 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
1f824 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 69  e contains routi
1f825 6e 65 73 20 75 73 65 64 20 66 6f 72 20 77 61 6c  nes used for wal
1f826 6b 69 6e 67 20 74 68 65 20 70 61 72 73 65 72 20  king the parser 
1f827 74 72 65 65 20 61 6e 64 0a 2a 2a 20 72 65 73 6f  tree and.** reso
1f828 6c 76 65 20 61 6c 6c 20 69 64 65 6e 74 69 66 69  lve all identifi
1f829 65 72 73 20 62 79 20 61 73 73 6f 63 69 61 74 69  ers by associati
1f82a 6e 67 20 74 68 65 6d 20 77 69 74 68 20 61 20 70  ng them with a p
1f82b 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 74 61 62  articular.** tab
1f82c 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a  le and column..*
1f82d 2a 0a 2a 2a 20 24 49 64 3a 20 72 65 73 6f 6c 76  *.** $Id: resolv
1f82e 65 2e 63 2c 76 20 31 2e 33 30 20 32 30 30 39 2f  e.c,v 1.30 2009/
1f82f 30 36 2f 31 35 20 32 33 3a 31 35 3a 35 39 20 64  06/15 23:15:59 d
1f830 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a  rh Exp $.*/../*.
1f831 2a 2a 20 54 75 72 6e 20 74 68 65 20 70 45 78 70  ** Turn the pExp
1f832 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 74  r expression int
1f833 6f 20 61 6e 20 61 6c 69 61 73 20 66 6f 72 20 74  o an alias for t
1f834 68 65 20 69 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d  he iCol-th colum
1f835 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 73 75  n of the.** resu
1f836 6c 74 20 73 65 74 20 69 6e 20 70 45 4c 69 73 74  lt set in pEList
1f837 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72  ..**.** If the r
1f838 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e  esult set column
1f839 20 69 73 20 61 20 73 69 6d 70 6c 65 20 63 6f 6c   is a simple col
1f83a 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 2c 20 74  umn reference, t
1f83b 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
1f83c 0a 2a 2a 20 6d 61 6b 65 73 20 61 6e 20 65 78 61  .** makes an exa
1f83d 63 74 20 63 6f 70 79 2e 20 20 42 75 74 20 66 6f  ct copy.  But fo
1f83e 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64  r any other kind
1f83f 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 2c 20   of expression, 
1f840 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  this.** routine 
1f841 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74  make a copy of t
1f842 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f  he result set co
1f843 6c 75 6d 6e 20 61 73 20 74 68 65 20 61 72 67 75  lumn as the argu
1f844 6d 65 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 54  ment to the.** T
1f845 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 2e 20 20  K_AS operator.  
1f846 54 68 65 20 54 4b 5f 41 53 20 6f 70 65 72 61 74  The TK_AS operat
1f847 6f 72 20 63 61 75 73 65 73 20 74 68 65 20 65 78  or causes the ex
1f848 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 0a 2a  pression to be.*
1f849 2a 20 65 76 61 6c 75 61 74 65 64 20 6a 75 73 74  * evaluated just
1f84a 20 6f 6e 63 65 20 61 6e 64 20 74 68 65 6e 20 72   once and then r
1f84b 65 75 73 65 64 20 66 6f 72 20 65 61 63 68 20 61  eused for each a
1f84c 6c 69 61 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  lias..**.** The 
1f84d 72 65 61 73 6f 6e 20 66 6f 72 20 73 75 70 70 72  reason for suppr
1f84e 65 73 73 69 6e 67 20 74 68 65 20 54 4b 5f 41 53  essing the TK_AS
1f84f 20 74 65 72 6d 20 77 68 65 6e 20 74 68 65 20 65   term when the e
1f850 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 73  xpression is a s
1f851 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20  imple.** column 
1f852 72 65 66 65 72 65 6e 63 65 20 69 73 20 73 6f 20  reference is so 
1f853 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20  that the column 
1f854 72 65 66 65 72 65 6e 63 65 20 77 69 6c 6c 20 62  reference will b
1f855 65 20 72 65 63 6f 67 6e 69 7a 65 64 20 61 73 0a  e recognized as.
1f856 2a 2a 20 75 73 61 62 6c 65 20 62 79 20 69 6e 64  ** usable by ind
1f857 69 63 65 73 20 77 69 74 68 69 6e 20 74 68 65 20  ices within the 
1f858 57 48 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f  WHERE clause pro
1f859 63 65 73 73 69 6e 67 20 6c 6f 67 69 63 2e 20 0a  cessing logic. .
1f85a 2a 2a 0a 2a 2a 20 48 61 63 6b 3a 20 20 54 68 65  **.** Hack:  The
1f85b 20 54 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 20   TK_AS operator 
1f85c 69 73 20 69 6e 68 69 62 69 74 65 64 20 69 66 20  is inhibited if 
1f85d 7a 54 79 70 65 5b 30 5d 3d 3d 27 47 27 2e 20 20  zType[0]=='G'.  
1f85e 54 68 69 73 20 6d 65 61 6e 73 0a 2a 2a 20 74 68  This means.** th
1f85f 61 74 20 69 6e 20 61 20 47 52 4f 55 50 20 42 59  at in a GROUP BY
1f860 20 63 6c 61 75 73 65 2c 20 74 68 65 20 65 78 70   clause, the exp
1f861 72 65 73 73 69 6f 6e 20 69 73 20 65 76 61 6c 75  ression is evalu
1f862 61 74 65 64 20 74 77 69 63 65 2e 20 20 48 65 6e  ated twice.  Hen
1f863 63 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45  ce:.**.**     SE
1f864 4c 45 43 54 20 72 61 6e 64 6f 6d 28 29 25 35 20  LECT random()%5 
1f865 41 53 20 78 2c 20 63 6f 75 6e 74 28 2a 29 20 46  AS x, count(*) F
1f866 52 4f 4d 20 74 61 62 20 47 52 4f 55 50 20 42 59  ROM tab GROUP BY
1f867 20 78 0a 2a 2a 0a 2a 2a 20 49 73 20 65 71 75 69   x.**.** Is equi
1f868 76 61 6c 65 6e 74 20 74 6f 3a 0a 2a 2a 0a 2a 2a  valent to:.**.**
1f869 20 20 20 20 20 53 45 4c 45 43 54 20 72 61 6e 64       SELECT rand
1f86a 6f 6d 28 29 25 35 20 41 53 20 78 2c 20 63 6f 75  om()%5 AS x, cou
1f86b 6e 74 28 2a 29 20 46 52 4f 4d 20 74 61 62 20 47  nt(*) FROM tab G
1f86c 52 4f 55 50 20 42 59 20 72 61 6e 64 6f 6d 28 29  ROUP BY random()
1f86d 25 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73  %5.**.** The res
1f86e 75 6c 74 20 6f 66 20 72 61 6e 64 6f 6d 28 29 25  ult of random()%
1f86f 35 20 69 6e 20 74 68 65 20 47 52 4f 55 50 20 42  5 in the GROUP B
1f870 59 20 63 6c 61 75 73 65 20 69 73 20 70 72 6f 62  Y clause is prob
1f871 61 62 6c 79 20 64 69 66 66 65 72 65 6e 74 0a 2a  ably different.*
1f872 2a 20 66 72 6f 6d 20 74 68 65 20 72 65 73 75 6c  * from the resul
1f873 74 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d  t in the result-
1f874 73 65 74 2e 20 20 57 65 20 6d 69 67 68 74 20 66  set.  We might f
1f875 69 78 20 74 68 69 73 20 73 6f 6d 65 64 61 79 2e  ix this someday.
1f876 20 20 4f 72 0a 2a 2a 20 74 68 65 6e 20 61 67 61    Or.** then aga
1f877 69 6e 2c 20 77 65 20 6d 69 67 68 74 20 6e 6f 74  in, we might not
1f878 2e 2e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ....*/.static vo
1f879 69 64 20 72 65 73 6f 6c 76 65 41 6c 69 61 73 28  id resolveAlias(
1f87a 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
1f87b 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  ,         /* Par
1f87c 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
1f87d 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
1f87e 73 74 2c 20 20 20 20 20 20 2f 2a 20 41 20 72 65  st,      /* A re
1f87f 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 69 6e  sult set */.  in
1f880 74 20 69 43 6f 6c 2c 20 20 20 20 20 20 20 20 20  t iCol,         
1f881 20 20 20 20 20 2f 2a 20 41 20 63 6f 6c 75 6d 6e       /* A column
1f882 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
1f883 65 74 2e 20 20 30 2e 2e 70 45 4c 69 73 74 2d 3e  et.  0..pEList->
1f884 6e 45 78 70 72 2d 31 20 2a 2f 0a 20 20 45 78 70  nExpr-1 */.  Exp
1f885 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20  r *pExpr,       
1f886 20 20 20 20 2f 2a 20 54 72 61 6e 73 66 6f 72 6d      /* Transform
1f887 20 74 68 69 73 20 69 6e 74 6f 20 61 6e 20 61 6c   this into an al
1f888 69 61 73 20 74 6f 20 74 68 65 20 72 65 73 75 6c  ias to the resul
1f889 74 20 73 65 74 20 2a 2f 0a 20 20 63 6f 6e 73 74  t set */.  const
1f88a 20 63 68 61 72 20 2a 7a 54 79 70 65 20 20 20 20   char *zType    
1f88b 20 20 2f 2a 20 22 47 52 4f 55 50 22 20 6f 72 20    /* "GROUP" or 
1f88c 22 4f 52 44 45 52 22 20 6f 72 20 22 22 20 2a 2f  "ORDER" or "" */
1f88d 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 4f 72 69  .){.  Expr *pOri
1f88e 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  g;           /* 
1f88f 54 68 65 20 69 43 6f 6c 2d 74 68 20 63 6f 6c 75  The iCol-th colu
1f890 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  mn of the result
1f891 20 73 65 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a   set */.  Expr *
1f892 70 44 75 70 3b 20 20 20 20 20 20 20 20 20 20 20  pDup;           
1f893 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70 4f 72 69   /* Copy of pOri
1f894 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  g */.  sqlite3 *
1f895 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  db;           /*
1f896 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
1f897 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61  nnection */..  a
1f898 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d 30 20 26  ssert( iCol>=0 &
1f899 26 20 69 43 6f 6c 3c 70 45 4c 69 73 74 2d 3e 6e  & iCol<pEList->n
1f89a 45 78 70 72 20 29 3b 0a 20 20 70 4f 72 69 67 20  Expr );.  pOrig 
1f89b 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c  = pEList->a[iCol
1f89c 5d 2e 70 45 78 70 72 3b 0a 20 20 61 73 73 65 72  ].pExpr;.  asser
1f89d 74 28 20 70 4f 72 69 67 21 3d 30 20 29 3b 0a 20  t( pOrig!=0 );. 
1f89e 20 61 73 73 65 72 74 28 20 70 4f 72 69 67 2d 3e   assert( pOrig->
1f89f 66 6c 61 67 73 20 26 20 45 50 5f 52 65 73 6f 6c  flags & EP_Resol
1f8a0 76 65 64 20 29 3b 0a 20 20 64 62 20 3d 20 70 50  ved );.  db = pP
1f8a1 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20  arse->db;.  if( 
1f8a2 70 4f 72 69 67 2d 3e 6f 70 21 3d 54 4b 5f 43 4f  pOrig->op!=TK_CO
1f8a3 4c 55 4d 4e 20 26 26 20 7a 54 79 70 65 5b 30 5d  LUMN && zType[0]
1f8a4 21 3d 27 47 27 20 29 7b 0a 20 20 20 20 70 44 75  !='G' ){.    pDu
1f8a5 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  p = sqlite3ExprD
1f8a6 75 70 28 64 62 2c 20 70 4f 72 69 67 2c 20 30 29  up(db, pOrig, 0)
1f8a7 3b 0a 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c  ;.    pDup = sql
1f8a8 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
1f8a9 2c 20 54 4b 5f 41 53 2c 20 70 44 75 70 2c 20 30  , TK_AS, pDup, 0
1f8aa 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 44  , 0);.    if( pD
1f8ab 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  up==0 ) return;.
1f8ac 20 20 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e      if( pEList->
1f8ad 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 61 73 3d 3d  a[iCol].iAlias==
1f8ae 30 20 29 7b 0a 20 20 20 20 20 20 70 45 4c 69 73  0 ){.      pELis
1f8af 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 61  t->a[iCol].iAlia
1f8b0 73 20 3d 20 28 75 31 36 29 28 2b 2b 70 50 61 72  s = (u16)(++pPar
1f8b1 73 65 2d 3e 6e 41 6c 69 61 73 29 3b 0a 20 20 20  se->nAlias);.   
1f8b2 20 7d 0a 20 20 20 20 70 44 75 70 2d 3e 69 54 61   }.    pDup->iTa
1f8b3 62 6c 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b  ble = pEList->a[
1f8b4 69 43 6f 6c 5d 2e 69 41 6c 69 61 73 3b 0a 20 20  iCol].iAlias;.  
1f8b5 7d 65 6c 73 65 20 69 66 28 20 45 78 70 72 48 61  }else if( ExprHa
1f8b6 73 50 72 6f 70 65 72 74 79 28 70 4f 72 69 67 2c  sProperty(pOrig,
1f8b7 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 7c 7c   EP_IntValue) ||
1f8b8 20 70 4f 72 69 67 2d 3e 75 2e 7a 54 6f 6b 65 6e   pOrig->u.zToken
1f8b9 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44 75 70 20  ==0 ){.    pDup 
1f8ba 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
1f8bb 28 64 62 2c 20 70 4f 72 69 67 2c 20 30 29 3b 0a  (db, pOrig, 0);.
1f8bc 20 20 20 20 69 66 28 20 70 44 75 70 3d 3d 30 20      if( pDup==0 
1f8bd 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73  ) return;.  }els
1f8be 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 6f  e{.    char *zTo
1f8bf 6b 65 6e 20 3d 20 70 4f 72 69 67 2d 3e 75 2e 7a  ken = pOrig->u.z
1f8c0 54 6f 6b 65 6e 3b 0a 20 20 20 20 61 73 73 65 72  Token;.    asser
1f8c1 74 28 20 7a 54 6f 6b 65 6e 21 3d 30 20 29 3b 0a  t( zToken!=0 );.
1f8c2 20 20 20 20 70 4f 72 69 67 2d 3e 75 2e 7a 54 6f      pOrig->u.zTo
1f8c3 6b 65 6e 20 3d 20 30 3b 0a 20 20 20 20 70 44 75  ken = 0;.    pDu
1f8c4 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  p = sqlite3ExprD
1f8c5 75 70 28 64 62 2c 20 70 4f 72 69 67 2c 20 30 29  up(db, pOrig, 0)
1f8c6 3b 0a 20 20 20 20 70 4f 72 69 67 2d 3e 75 2e 7a  ;.    pOrig->u.z
1f8c7 54 6f 6b 65 6e 20 3d 20 7a 54 6f 6b 65 6e 3b 0a  Token = zToken;.
1f8c8 20 20 20 20 69 66 28 20 70 44 75 70 3d 3d 30 20      if( pDup==0 
1f8c9 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 61 73  ) return;.    as
1f8ca 73 65 72 74 28 20 28 70 44 75 70 2d 3e 66 6c 61  sert( (pDup->fla
1f8cb 67 73 20 26 20 28 45 50 5f 52 65 64 75 63 65 64  gs & (EP_Reduced
1f8cc 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 29 3d  |EP_TokenOnly))=
1f8cd 3d 30 20 29 3b 0a 20 20 20 20 70 44 75 70 2d 3e  =0 );.    pDup->
1f8ce 66 6c 61 67 73 32 20 7c 3d 20 45 50 32 5f 4d 61  flags2 |= EP2_Ma
1f8cf 6c 6c 6f 63 65 64 54 6f 6b 65 6e 3b 0a 20 20 20  llocedToken;.   
1f8d0 20 70 44 75 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20   pDup->u.zToken 
1f8d1 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
1f8d2 70 28 64 62 2c 20 7a 54 6f 6b 65 6e 29 3b 0a 20  p(db, zToken);. 
1f8d3 20 7d 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e   }.  if( pExpr->
1f8d4 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f  flags & EP_ExpCo
1f8d5 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 70 44 75  llate ){.    pDu
1f8d6 70 2d 3e 70 43 6f 6c 6c 20 3d 20 70 45 78 70 72  p->pColl = pExpr
1f8d7 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 70 44 75  ->pColl;.    pDu
1f8d8 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45  p->flags |= EP_E
1f8d9 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 7d 0a 20  xpCollate;.  }. 
1f8da 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61   sqlite3ExprClea
1f8db 72 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 20 20  r(db, pExpr);.  
1f8dc 6d 65 6d 63 70 79 28 70 45 78 70 72 2c 20 70 44  memcpy(pExpr, pD
1f8dd 75 70 2c 20 73 69 7a 65 6f 66 28 2a 70 45 78 70  up, sizeof(*pExp
1f8de 72 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  r));.  sqlite3Db
1f8df 46 72 65 65 28 64 62 2c 20 70 44 75 70 29 3b 0a  Free(db, pDup);.
1f8e0 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74  }../*.** Given t
1f8e1 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c  he name of a col
1f8e2 75 6d 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  umn of the form 
1f8e3 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 6f 72 20  X.Y.Z or Y.Z or 
1f8e4 6a 75 73 74 20 5a 2c 20 6c 6f 6f 6b 20 75 70 0a  just Z, look up.
1f8e5 2a 2a 20 74 68 61 74 20 6e 61 6d 65 20 69 6e 20  ** that name in 
1f8e6 74 68 65 20 73 65 74 20 6f 66 20 73 6f 75 72 63  the set of sourc
1f8e7 65 20 74 61 62 6c 65 73 20 69 6e 20 70 53 72 63  e tables in pSrc
1f8e8 4c 69 73 74 20 61 6e 64 20 6d 61 6b 65 20 74 68  List and make th
1f8e9 65 20 70 45 78 70 72 20 0a 2a 2a 20 65 78 70 72  e pExpr .** expr
1f8ea 65 73 73 69 6f 6e 20 6e 6f 64 65 20 72 65 66 65  ession node refe
1f8eb 72 20 62 61 63 6b 20 74 6f 20 74 68 61 74 20 73  r back to that s
1f8ec 6f 75 72 63 65 20 63 6f 6c 75 6d 6e 2e 20 20 54  ource column.  T
1f8ed 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61  he following cha
1f8ee 6e 67 65 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65  nges.** are made
1f8ef 20 74 6f 20 70 45 78 70 72 3a 0a 2a 2a 0a 2a 2a   to pExpr:.**.**
1f8f0 20 20 20 20 70 45 78 70 72 2d 3e 69 44 62 20 20      pExpr->iDb  
1f8f1 20 20 20 20 20 20 20 20 20 53 65 74 20 74 68 65           Set the
1f8f2 20 69 6e 64 65 78 20 69 6e 20 64 62 2d 3e 61 44   index in db->aD
1f8f3 62 5b 5d 20 6f 66 20 74 68 65 20 64 61 74 61 62  b[] of the datab
1f8f4 61 73 65 20 58 0a 2a 2a 20 20 20 20 20 20 20 20  ase X.**        
1f8f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f8f6 20 28 65 76 65 6e 20 69 66 20 58 20 69 73 20 69   (even if X is i
1f8f7 6d 70 6c 69 65 64 29 2e 0a 2a 2a 20 20 20 20 70  mplied)..**    p
1f8f8 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 20 20 20  Expr->iTable    
1f8f9 20 20 20 20 53 65 74 20 74 6f 20 74 68 65 20 63      Set to the c
1f8fa 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72  ursor number for
1f8fb 20 74 68 65 20 74 61 62 6c 65 20 6f 62 74 61 69   the table obtai
1f8fc 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ned.**          
1f8fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66                 f
1f8fe 72 6f 6d 20 70 53 72 63 4c 69 73 74 2e 0a 2a 2a  rom pSrcList..**
1f8ff 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20      pExpr->pTab 
1f900 20 20 20 20 20 20 20 20 20 50 6f 69 6e 74 73 20           Points 
1f901 74 6f 20 74 68 65 20 54 61 62 6c 65 20 73 74 72  to the Table str
1f902 75 63 74 75 72 65 20 6f 66 20 58 2e 59 20 28 65  ucture of X.Y (e
1f903 76 65 6e 20 69 66 0a 2a 2a 20 20 20 20 20 20 20  ven if.**       
1f904 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f905 20 20 58 20 61 6e 64 2f 6f 72 20 59 20 61 72 65    X and/or Y are
1f906 20 69 6d 70 6c 69 65 64 2e 29 0a 2a 2a 20 20 20   implied.).**   
1f907 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20   pExpr->iColumn 
1f908 20 20 20 20 20 20 53 65 74 20 74 6f 20 74 68 65        Set to the
1f909 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77   column number w
1f90a 69 74 68 69 6e 20 74 68 65 20 74 61 62 6c 65 2e  ithin the table.
1f90b 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70  .**    pExpr->op
1f90c 20 20 20 20 20 20 20 20 20 20 20 20 53 65 74 20              Set 
1f90d 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 2e 0a 2a 2a  to TK_COLUMN..**
1f90e 20 20 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 74      pExpr->pLeft
1f90f 20 20 20 20 20 20 20 20 20 41 6e 79 20 65 78 70           Any exp
1f910 72 65 73 73 69 6f 6e 20 74 68 69 73 20 70 6f 69  ression this poi
1f911 6e 74 73 20 74 6f 20 69 73 20 64 65 6c 65 74 65  nts to is delete
1f912 64 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 70  d.**    pExpr->p
1f913 52 69 67 68 74 20 20 20 20 20 20 20 20 41 6e 79  Right        Any
1f914 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 69 73   expression this
1f915 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20 64 65   points to is de
1f916 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  leted..**.** The
1f917 20 7a 44 62 20 76 61 72 69 61 62 6c 65 20 69 73   zDb variable is
1f918 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
1f919 20 64 61 74 61 62 61 73 65 20 28 74 68 65 20 22   database (the "
1f91a 58 22 29 2e 20 20 54 68 69 73 20 76 61 6c 75 65  X").  This value
1f91b 20 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 20   may be.** NULL 
1f91c 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 6e 61 6d  meaning that nam
1f91d 65 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  e is of the form
1f91e 20 59 2e 5a 20 6f 72 20 5a 2e 20 20 41 6e 79 20   Y.Z or Z.  Any 
1f91f 61 76 61 69 6c 61 62 6c 65 20 64 61 74 61 62 61  available databa
1f920 73 65 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65  se.** can be use
1f921 64 2e 20 20 54 68 65 20 7a 54 61 62 6c 65 20 76  d.  The zTable v
1f922 61 72 69 61 62 6c 65 20 69 73 20 74 68 65 20 6e  ariable is the n
1f923 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
1f924 20 28 74 68 65 20 22 59 22 29 2e 20 20 54 68 69   (the "Y").  Thi
1f925 73 0a 2a 2a 20 76 61 6c 75 65 20 63 61 6e 20 62  s.** value can b
1f926 65 20 4e 55 4c 4c 20 69 66 20 7a 44 62 20 69 73  e NULL if zDb is
1f927 20 61 6c 73 6f 20 4e 55 4c 4c 2e 20 20 49 66 20   also NULL.  If 
1f928 7a 54 61 62 6c 65 20 69 73 20 4e 55 4c 4c 20 69  zTable is NULL i
1f929 74 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20  t.** means that 
1f92a 74 68 65 20 66 6f 72 6d 20 6f 66 20 74 68 65 20  the form of the 
1f92b 6e 61 6d 65 20 69 73 20 5a 20 61 6e 64 20 74 68  name is Z and th
1f92c 61 74 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20  at columns from 
1f92d 61 6e 79 20 74 61 62 6c 65 0a 2a 2a 20 63 61 6e  any table.** can
1f92e 20 62 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20   be used..**.** 
1f92f 49 66 20 74 68 65 20 6e 61 6d 65 20 63 61 6e 6e  If the name cann
1f930 6f 74 20 62 65 20 72 65 73 6f 6c 76 65 64 20 75  ot be resolved u
1f931 6e 61 6d 62 69 67 75 6f 75 73 6c 79 2c 20 6c 65  nambiguously, le
1f932 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ave an error mes
1f933 73 61 67 65 0a 2a 2a 20 69 6e 20 70 50 61 72 73  sage.** in pPars
1f934 65 20 61 6e 64 20 72 65 74 75 72 6e 20 57 52 43  e and return WRC
1f935 5f 41 62 6f 72 74 2e 20 20 52 65 74 75 72 6e 20  _Abort.  Return 
1f936 57 52 43 5f 50 72 75 6e 65 20 6f 6e 20 73 75 63  WRC_Prune on suc
1f937 63 65 73 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  cess..*/.static 
1f938 69 6e 74 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 0a  int lookupName(.
1f939 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
1f93a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
1f93b 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
1f93c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1f93d 44 62 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  Db,     /* Name 
1f93e 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1f93f 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65  containing table
1f940 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63  , or NULL */.  c
1f941 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 2c  onst char *zTab,
1f942 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
1f943 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  able containing 
1f944 63 6f 6c 75 6d 6e 2c 20 6f 72 20 4e 55 4c 4c 20  column, or NULL 
1f945 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1f946 2a 7a 43 6f 6c 2c 20 20 20 20 2f 2a 20 4e 61 6d  *zCol,    /* Nam
1f947 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e  e of the column.
1f948 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78   */.  NameContex
1f949 74 20 2a 70 4e 43 2c 20 20 20 20 2f 2a 20 54 68  t *pNC,    /* Th
1f94a 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 20 75  e name context u
1f94b 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 65 20 74  sed to resolve t
1f94c 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 45 78 70  he name */.  Exp
1f94d 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 20 20  r *pExpr        
1f94e 20 20 2f 2a 20 4d 61 6b 65 20 74 68 69 73 20 45    /* Make this E
1f94f 58 50 52 20 6e 6f 64 65 20 70 6f 69 6e 74 20 74  XPR node point t
1f950 6f 20 74 68 65 20 73 65 6c 65 63 74 65 64 20 63  o the selected c
1f951 6f 6c 75 6d 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e  olumn */.){.  in
1f952 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20  t i, j;         
1f953 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
1f954 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74  ers */.  int cnt
1f955 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
1f956 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1f957 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 69 6e 67  mber of matching
1f958 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a 2f   column names */
1f959 0a 20 20 69 6e 74 20 63 6e 74 54 61 62 20 3d 20  .  int cntTab = 
1f95a 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
1f95b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1f95c 66 20 6d 61 74 63 68 69 6e 67 20 74 61 62 6c 65  f matching table
1f95d 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 73 71 6c 69   names */.  sqli
1f95e 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
1f95f 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a  ->db;         /*
1f960 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
1f961 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 74  nnection */.  st
1f962 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
1f963 6d 20 2a 70 49 74 65 6d 3b 20 20 20 20 20 20 20  m *pItem;       
1f964 2f 2a 20 55 73 65 20 66 6f 72 20 6c 6f 6f 70 69  /* Use for loopi
1f965 6e 67 20 6f 76 65 72 20 70 53 72 63 4c 69 73 74  ng over pSrcList
1f966 20 69 74 65 6d 73 20 2a 2f 0a 20 20 73 74 72 75   items */.  stru
1f967 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
1f968 2a 70 4d 61 74 63 68 20 3d 20 30 3b 20 20 2f 2a  *pMatch = 0;  /*
1f969 20 54 68 65 20 6d 61 74 63 68 69 6e 67 20 70 53   The matching pS
1f96a 72 63 4c 69 73 74 20 69 74 65 6d 20 2a 2f 0a 20  rcList item */. 
1f96b 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 54   NameContext *pT
1f96c 6f 70 4e 43 20 3d 20 70 4e 43 3b 20 20 20 20 20  opNC = pNC;     
1f96d 20 20 20 2f 2a 20 46 69 72 73 74 20 6e 61 6d 65     /* First name
1f96e 63 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 20 6c  context in the l
1f96f 69 73 74 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20  ist */.  Schema 
1f970 2a 70 53 63 68 65 6d 61 20 3d 20 30 3b 20 20 20  *pSchema = 0;   
1f971 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 63             /* Sc
1f972 68 65 6d 61 20 6f 66 20 74 68 65 20 65 78 70 72  hema of the expr
1f973 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ession */.  int 
1f974 69 73 54 72 69 67 67 65 72 20 3d 20 30 3b 0a 0a  isTrigger = 0;..
1f975 20 20 61 73 73 65 72 74 28 20 70 4e 43 20 29 3b    assert( pNC );
1f976 20 20 20 20 20 2f 2a 20 74 68 65 20 6e 61 6d 65       /* the name
1f977 20 63 6f 6e 74 65 78 74 20 63 61 6e 6e 6f 74 20   context cannot 
1f978 62 65 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73  be NULL. */.  as
1f979 73 65 72 74 28 20 7a 43 6f 6c 20 29 3b 20 20 20  sert( zCol );   
1f97a 20 2f 2a 20 54 68 65 20 5a 20 69 6e 20 58 2e 59   /* The Z in X.Y
1f97b 2e 5a 20 63 61 6e 6e 6f 74 20 62 65 20 4e 55 4c  .Z cannot be NUL
1f97c 4c 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 7e  L */.  assert( ~
1f97d 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72  ExprHasAnyProper
1f97e 74 79 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b  ty(pExpr, EP_Tok
1f97f 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65  enOnly|EP_Reduce
1f980 64 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74  d) );..  /* Init
1f981 69 61 6c 69 7a 65 20 74 68 65 20 6e 6f 64 65 20  ialize the node 
1f982 74 6f 20 6e 6f 2d 6d 61 74 63 68 20 2a 2f 0a 20  to no-match */. 
1f983 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d   pExpr->iTable =
1f984 20 2d 31 3b 0a 20 20 70 45 78 70 72 2d 3e 70 54   -1;.  pExpr->pT
1f985 61 62 20 3d 20 30 3b 0a 20 20 45 78 70 72 53 65  ab = 0;.  ExprSe
1f986 74 49 72 72 65 64 75 63 69 62 6c 65 28 70 45 78  tIrreducible(pEx
1f987 70 72 29 3b 0a 0a 20 20 2f 2a 20 53 74 61 72 74  pr);..  /* Start
1f988 20 61 74 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f   at the inner-mo
1f989 73 74 20 63 6f 6e 74 65 78 74 20 61 6e 64 20 6d  st context and m
1f98a 6f 76 65 20 6f 75 74 77 61 72 64 20 75 6e 74 69  ove outward unti
1f98b 6c 20 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75  l a match is fou
1f98c 6e 64 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 70  nd */.  while( p
1f98d 4e 43 20 26 26 20 63 6e 74 3d 3d 30 20 29 7b 0a  NC && cnt==0 ){.
1f98e 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45      ExprList *pE
1f98f 4c 69 73 74 3b 0a 20 20 20 20 53 72 63 4c 69 73  List;.    SrcLis
1f990 74 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70 4e  t *pSrcList = pN
1f991 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 0a 20 20  C->pSrcList;..  
1f992 20 20 69 66 28 20 70 53 72 63 4c 69 73 74 20 29    if( pSrcList )
1f993 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c  {.      for(i=0,
1f994 20 70 49 74 65 6d 3d 70 53 72 63 4c 69 73 74 2d   pItem=pSrcList-
1f995 3e 61 3b 20 69 3c 70 53 72 63 4c 69 73 74 2d 3e  >a; i<pSrcList->
1f996 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  nSrc; i++, pItem
1f997 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 54 61 62  ++){.        Tab
1f998 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 20 20  le *pTab;.      
1f999 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20    int iDb;.     
1f99a 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b     Column *pCol;
1f99b 0a 20 20 0a 20 20 20 20 20 20 20 20 70 54 61 62  .  .        pTab
1f99c 20 3d 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0a   = pItem->pTab;.
1f99d 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1f99e 70 54 61 62 21 3d 30 20 26 26 20 70 54 61 62 2d  pTab!=0 && pTab-
1f99f 3e 7a 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 20 20  >zName!=0 );.   
1f9a0 20 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74       iDb = sqlit
1f9a1 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
1f9a2 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
1f9a3 61 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  a);.        asse
1f9a4 72 74 28 20 70 54 61 62 2d 3e 6e 43 6f 6c 3e 30  rt( pTab->nCol>0
1f9a5 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
1f9a6 7a 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20  zTab ){.        
1f9a7 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 41 6c    if( pItem->zAl
1f9a8 69 61 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ias ){.         
1f9a9 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d     char *zTabNam
1f9aa 65 20 3d 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61  e = pItem->zAlia
1f9ab 73 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  s;.            i
1f9ac 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
1f9ad 70 28 7a 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62  p(zTabName, zTab
1f9ae 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  )!=0 ) continue;
1f9af 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
1f9b0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68  {.            ch
1f9b1 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 70  ar *zTabName = p
1f9b2 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20  Tab->zName;.    
1f9b3 20 20 20 20 20 20 20 20 69 66 28 20 4e 45 56 45          if( NEVE
1f9b4 52 28 7a 54 61 62 4e 61 6d 65 3d 3d 30 29 20 7c  R(zTabName==0) |
1f9b5 7c 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  | sqlite3StrICmp
1f9b6 28 7a 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62 29  (zTabName, zTab)
1f9b7 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  !=0 ){.         
1f9b8 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
1f9b9 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
1f9ba 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 44 62           if( zDb
1f9bb 21 3d 30 20 26 26 20 73 71 6c 69 74 65 33 53 74  !=0 && sqlite3St
1f9bc 72 49 43 6d 70 28 64 62 2d 3e 61 44 62 5b 69 44  rICmp(db->aDb[iD
1f9bd 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 44 62 29 21 3d  b].zName, zDb)!=
1f9be 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
1f9bf 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
1f9c0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1f9c1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
1f9c2 0a 20 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d  .        if( 0==
1f9c3 28 63 6e 74 54 61 62 2b 2b 29 20 29 7b 0a 20 20  (cntTab++) ){.  
1f9c4 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69          pExpr->i
1f9c5 54 61 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69  Table = pItem->i
1f9c6 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 20 20  Cursor;.        
1f9c7 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20    pExpr->pTab = 
1f9c8 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20  pTab;.          
1f9c9 70 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e  pSchema = pTab->
1f9ca 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 20 20 20  pSchema;.       
1f9cb 20 20 20 70 4d 61 74 63 68 20 3d 20 70 49 74 65     pMatch = pIte
1f9cc 6d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  m;.        }.   
1f9cd 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 43       for(j=0, pC
1f9ce 6f 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c 3b 20 6a  ol=pTab->aCol; j
1f9cf 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b  <pTab->nCol; j++
1f9d0 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20  , pCol++){.     
1f9d1 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1f9d2 53 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e  StrICmp(pCol->zN
1f9d3 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b  ame, zCol)==0 ){
1f9d4 0a 20 20 20 20 20 20 20 20 20 20 20 20 49 64 4c  .            IdL
1f9d5 69 73 74 20 2a 70 55 73 69 6e 67 3b 0a 20 20 20  ist *pUsing;.   
1f9d6 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a           cnt++;.
1f9d7 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70              pExp
1f9d8 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49 74 65  r->iTable = pIte
1f9d9 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20  m->iCursor;.    
1f9da 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70          pExpr->p
1f9db 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20 20  Tab = pTab;.    
1f9dc 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 20 3d          pMatch =
1f9dd 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 20   pItem;.        
1f9de 20 20 20 20 70 53 63 68 65 6d 61 20 3d 20 70 54      pSchema = pT
1f9df 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20  ab->pSchema;.   
1f9e0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 62 73           /* Subs
1f9e1 74 69 74 75 74 65 20 74 68 65 20 72 6f 77 69 64  titute the rowid
1f9e2 20 28 63 6f 6c 75 6d 6e 20 2d 31 29 20 66 6f 72   (column -1) for
1f9e3 20 74 68 65 20 49 4e 54 45 47 45 52 20 50 52 49   the INTEGER PRI
1f9e4 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 20 20  MARY KEY */.    
1f9e5 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69          pExpr->i
1f9e6 43 6f 6c 75 6d 6e 20 3d 20 6a 3d 3d 70 54 61 62  Column = j==pTab
1f9e7 2d 3e 69 50 4b 65 79 20 3f 20 2d 31 20 3a 20 28  ->iPKey ? -1 : (
1f9e8 69 31 36 29 6a 3b 0a 20 20 20 20 20 20 20 20 20  i16)j;.         
1f9e9 20 20 20 69 66 28 20 69 3c 70 53 72 63 4c 69 73     if( i<pSrcLis
1f9ea 74 2d 3e 6e 53 72 63 2d 31 20 29 7b 0a 20 20 20  t->nSrc-1 ){.   
1f9eb 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70             if( p
1f9ec 49 74 65 6d 5b 31 5d 2e 6a 6f 69 6e 74 79 70 65  Item[1].jointype
1f9ed 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c 20 29 7b   & JT_NATURAL ){
1f9ee 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1f9ef 20 2f 2a 20 49 66 20 74 68 69 73 20 6d 61 74 63   /* If this matc
1f9f0 68 20 6f 63 63 75 72 72 65 64 20 69 6e 20 74 68  h occurred in th
1f9f1 65 20 6c 65 66 74 20 74 61 62 6c 65 20 6f 66 20  e left table of 
1f9f2 61 20 6e 61 74 75 72 61 6c 20 6a 6f 69 6e 2c 0a  a natural join,.
1f9f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f9f4 2a 2a 20 74 68 65 6e 20 73 6b 69 70 20 74 68 65  ** then skip the
1f9f5 20 72 69 67 68 74 20 74 61 62 6c 65 20 74 6f 20   right table to 
1f9f6 61 76 6f 69 64 20 61 20 64 75 70 6c 69 63 61 74  avoid a duplicat
1f9f7 65 20 6d 61 74 63 68 20 2a 2f 0a 20 20 20 20 20  e match */.     
1f9f8 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d             pItem
1f9f9 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ++;.            
1f9fa 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20      i++;.       
1f9fb 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
1f9fc 20 28 70 55 73 69 6e 67 20 3d 20 70 49 74 65 6d   (pUsing = pItem
1f9fd 5b 31 5d 2e 70 55 73 69 6e 67 29 21 3d 30 20 29  [1].pUsing)!=0 )
1f9fe 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
1f9ff 20 20 2f 2a 20 49 66 20 74 68 69 73 20 6d 61 74    /* If this mat
1fa00 63 68 20 6f 63 63 75 72 73 20 6f 6e 20 61 20 63  ch occurs on a c
1fa01 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 69 6e  olumn that is in
1fa02 20 74 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73   the USING claus
1fa03 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e.              
1fa04 20 20 2a 2a 20 6f 66 20 61 20 6a 6f 69 6e 2c 20    ** of a join, 
1fa05 73 6b 69 70 20 74 68 65 20 73 65 61 72 63 68 20  skip the search 
1fa06 6f 66 20 74 68 65 20 72 69 67 68 74 20 74 61 62  of the right tab
1fa07 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69 6e 0a 20  le of the join. 
1fa08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
1fa09 2a 20 74 6f 20 61 76 6f 69 64 20 61 20 64 75 70  * to avoid a dup
1fa0a 6c 69 63 61 74 65 20 6d 61 74 63 68 20 74 68 65  licate match the
1fa0b 72 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  re. */.         
1fa0c 20 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20         int k;.  
1fa0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 6f                fo
1fa0e 72 28 6b 3d 30 3b 20 6b 3c 70 55 73 69 6e 67 2d  r(k=0; k<pUsing-
1fa0f 3e 6e 49 64 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20  >nId; k++){.    
1fa10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
1fa11 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
1fa12 28 70 55 73 69 6e 67 2d 3e 61 5b 6b 5d 2e 7a 4e  (pUsing->a[k].zN
1fa13 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b  ame, zCol)==0 ){
1fa14 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1fa15 20 20 20 20 20 70 49 74 65 6d 2b 2b 3b 0a 20 20       pItem++;.  
1fa16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa17 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20    i++;.         
1fa18 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
1fa19 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
1fa1a 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1fa1b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1fa1c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1fa1d 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1fa1e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
1fa1f 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
1fa20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69       }.    }..#i
1fa21 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1fa22 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 2f 2a  T_TRIGGER.    /*
1fa23 20 49 66 20 77 65 20 68 61 76 65 20 6e 6f 74 20   If we have not 
1fa24 61 6c 72 65 61 64 79 20 72 65 73 6f 6c 76 65 64  already resolved
1fa25 20 74 68 65 20 6e 61 6d 65 2c 20 74 68 65 6e 20   the name, then 
1fa26 6d 61 79 62 65 20 0a 20 20 20 20 2a 2a 20 69 74  maybe .    ** it
1fa27 20 69 73 20 61 20 6e 65 77 2e 2a 20 6f 72 20 6f   is a new.* or o
1fa28 6c 64 2e 2a 20 74 72 69 67 67 65 72 20 61 72 67  ld.* trigger arg
1fa29 75 6d 65 6e 74 20 72 65 66 65 72 65 6e 63 65 0a  ument reference.
1fa2a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a      */.    if( z
1fa2b 44 62 3d 3d 30 20 26 26 20 7a 54 61 62 21 3d 30  Db==0 && zTab!=0
1fa2c 20 26 26 20 63 6e 74 3d 3d 30 20 26 26 20 70 50   && cnt==0 && pP
1fa2d 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61  arse->pTriggerTa
1fa2e 62 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e  b!=0 ){.      in
1fa2f 74 20 6f 70 20 3d 20 70 50 61 72 73 65 2d 3e 65  t op = pParse->e
1fa30 54 72 69 67 67 65 72 4f 70 3b 0a 20 20 20 20 20  TriggerOp;.     
1fa31 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30   Table *pTab = 0
1fa32 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1fa33 6f 70 3d 3d 54 4b 5f 44 45 4c 45 54 45 20 7c 7c  op==TK_DELETE ||
1fa34 20 6f 70 3d 3d 54 4b 5f 55 50 44 41 54 45 20 7c   op==TK_UPDATE |
1fa35 7c 20 6f 70 3d 3d 54 4b 5f 49 4e 53 45 52 54 20  | op==TK_INSERT 
1fa36 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 70 21  );.      if( op!
1fa37 3d 54 4b 5f 44 45 4c 45 54 45 20 26 26 20 73 71  =TK_DELETE && sq
1fa38 6c 69 74 65 33 53 74 72 49 43 6d 70 28 22 6e 65  lite3StrICmp("ne
1fa39 77 22 2c 7a 54 61 62 29 20 3d 3d 20 30 20 29 7b  w",zTab) == 0 ){
1fa3a 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e  .        pExpr->
1fa3b 69 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20  iTable = 1;.    
1fa3c 20 20 20 20 70 54 61 62 20 3d 20 70 50 61 72 73      pTab = pPars
1fa3d 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 3b 0a  e->pTriggerTab;.
1fa3e 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
1fa3f 6f 70 21 3d 54 4b 5f 49 4e 53 45 52 54 20 26 26  op!=TK_INSERT &&
1fa40 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
1fa41 22 6f 6c 64 22 2c 7a 54 61 62 29 3d 3d 30 20 29  "old",zTab)==0 )
1fa42 7b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d  {.        pExpr-
1fa43 3e 69 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20 20  >iTable = 0;.   
1fa44 20 20 20 20 20 70 54 61 62 20 3d 20 70 50 61 72       pTab = pPar
1fa45 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 3b  se->pTriggerTab;
1fa46 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
1fa47 69 66 28 20 70 54 61 62 20 29 7b 20 0a 20 20 20  if( pTab ){ .   
1fa48 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20       int iCol;. 
1fa49 20 20 20 20 20 20 20 70 53 63 68 65 6d 61 20 3d         pSchema =
1fa4a 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a   pTab->pSchema;.
1fa4b 20 20 20 20 20 20 20 20 63 6e 74 54 61 62 2b 2b          cntTab++
1fa4c 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71  ;.        if( sq
1fa4d 6c 69 74 65 33 49 73 52 6f 77 69 64 28 7a 43 6f  lite3IsRowid(zCo
1fa4e 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  l) ){.          
1fa4f 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 20  iCol = -1;.     
1fa50 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1fa51 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20      for(iCol=0; 
1fa52 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  iCol<pTab->nCol;
1fa53 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20   iCol++){.      
1fa54 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43        Column *pC
1fa55 6f 6c 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c  ol = &pTab->aCol
1fa56 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 20 20 20 20  [iCol];.        
1fa57 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
1fa58 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61  trICmp(pCol->zNa
1fa59 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a  me, zCol)==0 ){.
1fa5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
1fa5b 28 20 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 69 50  ( iCol==pTab->iP
1fa5c 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Key ){.         
1fa5d 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 2d 31         iCol = -1
1fa5e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
1fa5f 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }.              
1fa60 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
1fa61 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d     }.          }
1fa62 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1fa63 20 20 20 69 66 28 20 69 43 6f 6c 3c 70 54 61 62     if( iCol<pTab
1fa64 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20  ->nCol ){.      
1fa65 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20      cnt++;.     
1fa66 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20       if( iCol<0 
1fa67 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ){.            p
1fa68 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d  Expr->affinity =
1fa69 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45   SQLITE_AFF_INTE
1fa6a 47 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  GER;.          }
1fa6b 65 6c 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e  else if( pExpr->
1fa6c 69 54 61 62 6c 65 3d 3d 30 20 29 7b 0a 20 20 20  iTable==0 ){.   
1fa6d 20 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73           testcas
1fa6e 65 28 20 69 43 6f 6c 3d 3d 33 31 20 29 3b 0a 20  e( iCol==31 );. 
1fa6f 20 20 20 20 20 20 20 20 20 20 20 74 65 73 74 63             testc
1fa70 61 73 65 28 20 69 43 6f 6c 3d 3d 33 32 20 29 3b  ase( iCol==32 );
1fa71 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61  .            pPa
1fa72 72 73 65 2d 3e 6f 6c 64 6d 61 73 6b 20 7c 3d 20  rse->oldmask |= 
1fa73 28 69 43 6f 6c 3e 3d 33 32 20 3f 20 30 78 66 66  (iCol>=32 ? 0xff
1fa74 66 66 66 66 66 66 20 3a 20 28 28 28 75 33 32 29  ffffff : (((u32)
1fa75 31 29 3c 3c 69 43 6f 6c 29 29 3b 0a 20 20 20 20  1)<<iCol));.    
1fa76 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1fa77 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e    pExpr->iColumn
1fa78 20 3d 20 28 69 31 36 29 69 43 6f 6c 3b 0a 20 20   = (i16)iCol;.  
1fa79 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70          pExpr->p
1fa7a 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20 20  Tab = pTab;.    
1fa7b 20 20 20 20 20 20 69 73 54 72 69 67 67 65 72 20        isTrigger 
1fa7c 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 1;.        }. 
1fa7d 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e       }.    }.#en
1fa7e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
1fa7f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
1fa80 47 45 52 29 20 2a 2f 0a 0a 20 20 20 20 2f 2a 0a  GER) */..    /*.
1fa81 20 20 20 20 2a 2a 20 50 65 72 68 61 70 73 20 74      ** Perhaps t
1fa82 68 65 20 6e 61 6d 65 20 69 73 20 61 20 72 65 66  he name is a ref
1fa83 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 52 4f  erence to the RO
1fa84 57 49 44 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  WID.    */.    i
1fa85 66 28 20 63 6e 74 3d 3d 30 20 26 26 20 63 6e 74  f( cnt==0 && cnt
1fa86 54 61 62 3d 3d 31 20 26 26 20 73 71 6c 69 74 65  Tab==1 && sqlite
1fa87 33 49 73 52 6f 77 69 64 28 7a 43 6f 6c 29 20 29  3IsRowid(zCol) )
1fa88 7b 0a 20 20 20 20 20 20 63 6e 74 20 3d 20 31 3b  {.      cnt = 1;
1fa89 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43  .      pExpr->iC
1fa8a 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20  olumn = -1;.    
1fa8b 20 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74    pExpr->affinit
1fa8c 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 49  y = SQLITE_AFF_I
1fa8d 4e 54 45 47 45 52 3b 0a 20 20 20 20 7d 0a 0a 20  NTEGER;.    }.. 
1fa8e 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 49 66 20     /*.    ** If 
1fa8f 74 68 65 20 69 6e 70 75 74 20 69 73 20 6f 66 20  the input is of 
1fa90 74 68 65 20 66 6f 72 6d 20 5a 20 28 6e 6f 74 20  the form Z (not 
1fa91 59 2e 5a 20 6f 72 20 58 2e 59 2e 5a 29 20 74 68  Y.Z or X.Y.Z) th
1fa92 65 6e 20 74 68 65 20 6e 61 6d 65 20 5a 0a 20 20  en the name Z.  
1fa93 20 20 2a 2a 20 6d 69 67 68 74 20 72 65 66 65 72    ** might refer
1fa94 20 74 6f 20 61 6e 20 72 65 73 75 6c 74 2d 73 65   to an result-se
1fa95 74 20 61 6c 69 61 73 2e 20 20 54 68 69 73 20 68  t alias.  This h
1fa96 61 70 70 65 6e 73 2c 20 66 6f 72 20 65 78 61 6d  appens, for exam
1fa97 70 6c 65 2c 20 77 68 65 6e 0a 20 20 20 20 2a 2a  ple, when.    **
1fa98 20 77 65 20 61 72 65 20 72 65 73 6f 6c 76 69 6e   we are resolvin
1fa99 67 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 57  g names in the W
1fa9a 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74  HERE clause of t
1fa9b 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d  he following com
1fa9c 6d 61 6e 64 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  mand:.    **.   
1fa9d 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 61   **     SELECT a
1fa9e 2b 62 20 41 53 20 78 20 46 52 4f 4d 20 74 61 62  +b AS x FROM tab
1fa9f 6c 65 20 57 48 45 52 45 20 78 3c 31 30 3b 0a 20  le WHERE x<10;. 
1faa0 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 6e 20     **.    ** In 
1faa1 63 61 73 65 73 20 6c 69 6b 65 20 74 68 69 73 2c  cases like this,
1faa2 20 72 65 70 6c 61 63 65 20 70 45 78 70 72 20 77   replace pExpr w
1faa3 69 74 68 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ith a copy of th
1faa4 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  e expression tha
1faa5 74 0a 20 20 20 20 2a 2a 20 66 6f 72 6d 73 20 74  t.    ** forms t
1faa6 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 65 6e  he result set en
1faa7 74 72 79 20 28 22 61 2b 62 22 20 69 6e 20 74 68  try ("a+b" in th
1faa8 65 20 65 78 61 6d 70 6c 65 29 20 61 6e 64 20 72  e example) and r
1faa9 65 74 75 72 6e 20 69 6d 6d 65 64 69 61 74 65 6c  eturn immediatel
1faaa 79 2e 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74  y..    ** Note t
1faab 68 61 74 20 74 68 65 20 65 78 70 72 65 73 73 69  hat the expressi
1faac 6f 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  on in the result
1faad 20 73 65 74 20 73 68 6f 75 6c 64 20 68 61 76 65   set should have
1faae 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a 20 20   already been.  
1faaf 20 20 2a 2a 20 72 65 73 6f 6c 76 65 64 20 62 79    ** resolved by
1fab0 20 74 68 65 20 74 69 6d 65 20 74 68 65 20 57 48   the time the WH
1fab1 45 52 45 20 63 6c 61 75 73 65 20 69 73 20 72 65  ERE clause is re
1fab2 73 6f 6c 76 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  solved..    */. 
1fab3 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 26 26     if( cnt==0 &&
1fab4 20 28 70 45 4c 69 73 74 20 3d 20 70 4e 43 2d 3e   (pEList = pNC->
1fab5 70 45 4c 69 73 74 29 21 3d 30 20 26 26 20 7a 54  pEList)!=0 && zT
1fab6 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66  ab==0 ){.      f
1fab7 6f 72 28 6a 3d 30 3b 20 6a 3c 70 45 4c 69 73 74  or(j=0; j<pEList
1fab8 2d 3e 6e 45 78 70 72 3b 20 6a 2b 2b 29 7b 0a 20  ->nExpr; j++){. 
1fab9 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 41 73         char *zAs
1faba 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e   = pEList->a[j].
1fabb 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 69  zName;.        i
1fabc 66 28 20 7a 41 73 21 3d 30 20 26 26 20 73 71 6c  f( zAs!=0 && sql
1fabd 69 74 65 33 53 74 72 49 43 6d 70 28 7a 41 73 2c  ite3StrICmp(zAs,
1fabe 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20   zCol)==0 ){.   
1fabf 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 4f 72         Expr *pOr
1fac0 69 67 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73  ig;.          as
1fac1 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c 65  sert( pExpr->pLe
1fac2 66 74 3d 3d 30 20 26 26 20 70 45 78 70 72 2d 3e  ft==0 && pExpr->
1fac3 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 20 20 20  pRight==0 );.   
1fac4 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1fac5 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3d 3d 30  Expr->x.pList==0
1fac6 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73   );.          as
1fac7 73 65 72 74 28 20 70 45 78 70 72 2d 3e 78 2e 70  sert( pExpr->x.p
1fac8 53 65 6c 65 63 74 3d 3d 30 20 29 3b 0a 20 20 20  Select==0 );.   
1fac9 20 20 20 20 20 20 20 70 4f 72 69 67 20 3d 20 70         pOrig = p
1faca 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70  EList->a[j].pExp
1facb 72 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  r;.          if(
1facc 20 21 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20   !pNC->allowAgg 
1facd 26 26 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  && ExprHasProper
1face 74 79 28 70 4f 72 69 67 2c 20 45 50 5f 41 67 67  ty(pOrig, EP_Agg
1facf 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
1fad0 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1fad1 28 70 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65  (pParse, "misuse
1fad2 20 6f 66 20 61 6c 69 61 73 65 64 20 61 67 67 72   of aliased aggr
1fad3 65 67 61 74 65 20 25 73 22 2c 20 7a 41 73 29 3b  egate %s", zAs);
1fad4 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74  .            ret
1fad5 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
1fad6 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1fad7 20 20 20 20 20 72 65 73 6f 6c 76 65 41 6c 69 61       resolveAlia
1fad8 73 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74  s(pParse, pEList
1fad9 2c 20 6a 2c 20 70 45 78 70 72 2c 20 22 22 29 3b  , j, pExpr, "");
1fada 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 20 3d  .          cnt =
1fadb 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4d   1;.          pM
1fadc 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 20 20  atch = 0;.      
1fadd 20 20 20 20 61 73 73 65 72 74 28 20 7a 54 61 62      assert( zTab
1fade 3d 3d 30 20 26 26 20 7a 44 62 3d 3d 30 20 29 3b  ==0 && zDb==0 );
1fadf 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
1fae0 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 3b 0a  lookupname_end;.
1fae1 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1fae2 7d 20 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  } .    }..    /*
1fae3 20 41 64 76 61 6e 63 65 20 74 6f 20 74 68 65 20   Advance to the 
1fae4 6e 65 78 74 20 6e 61 6d 65 20 63 6f 6e 74 65 78  next name contex
1fae5 74 2e 20 20 54 68 65 20 6c 6f 6f 70 20 77 69 6c  t.  The loop wil
1fae6 6c 20 65 78 69 74 20 77 68 65 6e 20 65 69 74 68  l exit when eith
1fae7 65 72 0a 20 20 20 20 2a 2a 20 77 65 20 68 61 76  er.    ** we hav
1fae8 65 20 61 20 6d 61 74 63 68 20 28 63 6e 74 3e 30  e a match (cnt>0
1fae9 29 20 6f 72 20 77 68 65 6e 20 77 65 20 72 75 6e  ) or when we run
1faea 20 6f 75 74 20 6f 66 20 6e 61 6d 65 20 63 6f 6e   out of name con
1faeb 74 65 78 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  texts..    */.  
1faec 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 29 7b 0a    if( cnt==0 ){.
1faed 20 20 20 20 20 20 70 4e 43 20 3d 20 70 4e 43 2d        pNC = pNC-
1faee 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20  >pNext;.    }.  
1faef 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 49 66 20  }..  /*.  ** If 
1faf0 58 20 61 6e 64 20 59 20 61 72 65 20 4e 55 4c 4c  X and Y are NULL
1faf1 20 28 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73   (in other words
1faf2 20 69 66 20 6f 6e 6c 79 20 74 68 65 20 63 6f 6c   if only the col
1faf3 75 6d 6e 20 6e 61 6d 65 20 5a 20 69 73 0a 20 20  umn name Z is.  
1faf4 2a 2a 20 73 75 70 70 6c 69 65 64 29 20 61 6e 64  ** supplied) and
1faf5 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 5a 20   the value of Z 
1faf6 69 73 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 64  is enclosed in d
1faf7 6f 75 62 6c 65 2d 71 75 6f 74 65 73 2c 20 74 68  ouble-quotes, th
1faf8 65 6e 0a 20 20 2a 2a 20 5a 20 69 73 20 61 20 73  en.  ** Z is a s
1faf9 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 20 69 66  tring literal if
1fafa 20 69 74 20 64 6f 65 73 6e 27 74 20 6d 61 74 63   it doesn't matc
1fafb 68 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6e 61 6d  h any column nam
1fafc 65 73 2e 20 20 49 6e 20 74 68 61 74 0a 20 20 2a  es.  In that.  *
1fafd 2a 20 63 61 73 65 2c 20 77 65 20 6e 65 65 64 20  * case, we need 
1fafe 74 6f 20 72 65 74 75 72 6e 20 72 69 67 68 74 20  to return right 
1faff 61 77 61 79 20 61 6e 64 20 6e 6f 74 20 6d 61 6b  away and not mak
1fb00 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 74 6f  e any changes to
1fb01 0a 20 20 2a 2a 20 70 45 78 70 72 2e 0a 20 20 2a  .  ** pExpr..  *
1fb02 2a 0a 20 20 2a 2a 20 42 65 63 61 75 73 65 20 6e  *.  ** Because n
1fb03 6f 20 72 65 66 65 72 65 6e 63 65 20 77 61 73 20  o reference was 
1fb04 6d 61 64 65 20 74 6f 20 6f 75 74 65 72 20 63 6f  made to outer co
1fb05 6e 74 65 78 74 73 2c 20 74 68 65 20 70 4e 43 2d  ntexts, the pNC-
1fb06 3e 6e 52 65 66 0a 20 20 2a 2a 20 66 69 65 6c 64  >nRef.  ** field
1fb07 73 20 61 72 65 20 6e 6f 74 20 63 68 61 6e 67 65  s are not change
1fb08 64 20 69 6e 20 61 6e 79 20 63 6f 6e 74 65 78 74  d in any context
1fb09 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6e 74  ..  */.  if( cnt
1fb0a 3d 3d 30 20 26 26 20 7a 54 61 62 3d 3d 30 20 26  ==0 && zTab==0 &
1fb0b 26 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  & ExprHasPropert
1fb0c 79 28 70 45 78 70 72 2c 45 50 5f 44 62 6c 51 75  y(pExpr,EP_DblQu
1fb0d 6f 74 65 64 29 20 29 7b 0a 20 20 20 20 70 45 78  oted) ){.    pEx
1fb0e 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 53 54 52 49  pr->op = TK_STRI
1fb0f 4e 47 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 70  NG;.    pExpr->p
1fb10 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 72 65 74  Tab = 0;.    ret
1fb11 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20  urn WRC_Prune;. 
1fb12 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 63 6e   }..  /*.  ** cn
1fb13 74 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65 72 65  t==0 means there
1fb14 20 77 61 73 20 6e 6f 74 20 6d 61 74 63 68 2e 20   was not match. 
1fb15 20 63 6e 74 3e 31 20 6d 65 61 6e 73 20 74 68 65   cnt>1 means the
1fb16 72 65 20 77 65 72 65 20 74 77 6f 20 6f 72 0a 20  re were two or. 
1fb17 20 2a 2a 20 6d 6f 72 65 20 6d 61 74 63 68 65 73   ** more matches
1fb18 2e 20 20 45 69 74 68 65 72 20 77 61 79 2c 20 77  .  Either way, w
1fb19 65 20 68 61 76 65 20 61 6e 20 65 72 72 6f 72 2e  e have an error.
1fb1a 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6e 74 21  .  */.  if( cnt!
1fb1b 3d 31 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  =1 ){.    const 
1fb1c 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20 20 20  char *zErr;.    
1fb1d 7a 45 72 72 20 3d 20 63 6e 74 3d 3d 30 20 3f 20  zErr = cnt==0 ? 
1fb1e 22 6e 6f 20 73 75 63 68 20 63 6f 6c 75 6d 6e 22  "no such column"
1fb1f 20 3a 20 22 61 6d 62 69 67 75 6f 75 73 20 63 6f   : "ambiguous co
1fb20 6c 75 6d 6e 20 6e 61 6d 65 22 3b 0a 20 20 20 20  lumn name";.    
1fb21 69 66 28 20 7a 44 62 20 29 7b 0a 20 20 20 20 20  if( zDb ){.     
1fb22 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1fb23 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73  (pParse, "%s: %s
1fb24 2e 25 73 2e 25 73 22 2c 20 7a 45 72 72 2c 20 7a  .%s.%s", zErr, z
1fb25 44 62 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b  Db, zTab, zCol);
1fb26 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 7a  .    }else if( z
1fb27 54 61 62 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Tab ){.      sql
1fb28 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1fb29 72 73 65 2c 20 22 25 73 3a 20 25 73 2e 25 73 22  rse, "%s: %s.%s"
1fb2a 2c 20 7a 45 72 72 2c 20 7a 54 61 62 2c 20 7a 43  , zErr, zTab, zC
1fb2b 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ol);.    }else{.
1fb2c 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1fb2d 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25  orMsg(pParse, "%
1fb2e 73 3a 20 25 73 22 2c 20 7a 45 72 72 2c 20 7a 43  s: %s", zErr, zC
1fb2f 6f 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ol);.    }.    p
1fb30 54 6f 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20  TopNC->nErr++;. 
1fb31 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 63 6f   }..  /* If a co
1fb32 6c 75 6d 6e 20 66 72 6f 6d 20 61 20 74 61 62 6c  lumn from a tabl
1fb33 65 20 69 6e 20 70 53 72 63 4c 69 73 74 20 69 73  e in pSrcList is
1fb34 20 72 65 66 65 72 65 6e 63 65 64 2c 20 74 68 65   referenced, the
1fb35 6e 20 72 65 63 6f 72 64 0a 20 20 2a 2a 20 74 68  n record.  ** th
1fb36 69 73 20 66 61 63 74 20 69 6e 20 74 68 65 20 70  is fact in the p
1fb37 53 72 63 4c 69 73 74 2e 61 5b 5d 2e 63 6f 6c 55  SrcList.a[].colU
1fb38 73 65 64 20 62 69 74 6d 61 73 6b 2e 20 20 43 6f  sed bitmask.  Co
1fb39 6c 75 6d 6e 20 30 20 63 61 75 73 65 73 0a 20 20  lumn 0 causes.  
1fb3a 2a 2a 20 62 69 74 20 30 20 74 6f 20 62 65 20 73  ** bit 0 to be s
1fb3b 65 74 2e 20 20 43 6f 6c 75 6d 6e 20 31 20 73 65  et.  Column 1 se
1fb3c 74 73 20 62 69 74 20 31 2e 20 20 41 6e 64 20 73  ts bit 1.  And s
1fb3d 6f 20 66 6f 72 74 68 2e 20 20 49 66 20 74 68 65  o forth.  If the
1fb3e 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d  .  ** column num
1fb3f 62 65 72 20 69 73 20 67 72 65 61 74 65 72 20 74  ber is greater t
1fb40 68 61 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  han the number o
1fb41 66 20 62 69 74 73 20 69 6e 20 74 68 65 20 62 69  f bits in the bi
1fb42 74 6d 61 73 6b 0a 20 20 2a 2a 20 74 68 65 6e 20  tmask.  ** then 
1fb43 73 65 74 20 74 68 65 20 68 69 67 68 2d 6f 72 64  set the high-ord
1fb44 65 72 20 62 69 74 20 6f 66 20 74 68 65 20 62 69  er bit of the bi
1fb45 74 6d 61 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66  tmask..  */.  if
1fb46 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  ( pExpr->iColumn
1fb47 3e 3d 30 20 26 26 20 70 4d 61 74 63 68 21 3d 30  >=0 && pMatch!=0
1fb48 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20   ){.    int n = 
1fb49 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a  pExpr->iColumn;.
1fb4a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d      testcase( n=
1fb4b 3d 42 4d 53 2d 31 20 29 3b 0a 20 20 20 20 69 66  =BMS-1 );.    if
1fb4c 28 20 6e 3e 3d 42 4d 53 20 29 7b 0a 20 20 20 20  ( n>=BMS ){.    
1fb4d 20 20 6e 20 3d 20 42 4d 53 2d 31 3b 0a 20 20 20    n = BMS-1;.   
1fb4e 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
1fb4f 4d 61 74 63 68 2d 3e 69 43 75 72 73 6f 72 3d 3d  Match->iCursor==
1fb50 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 29 3b  pExpr->iTable );
1fb51 0a 20 20 20 20 70 4d 61 74 63 68 2d 3e 63 6f 6c  .    pMatch->col
1fb52 55 73 65 64 20 7c 3d 20 28 28 42 69 74 6d 61 73  Used |= ((Bitmas
1fb53 6b 29 31 29 3c 3c 6e 3b 0a 20 20 7d 0a 0a 20 20  k)1)<<n;.  }..  
1fb54 2f 2a 20 43 6c 65 61 6e 20 75 70 20 61 6e 64 20  /* Clean up and 
1fb55 72 65 74 75 72 6e 0a 20 20 2a 2f 0a 20 20 73 71  return.  */.  sq
1fb56 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
1fb57 64 62 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  db, pExpr->pLeft
1fb58 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 4c 65 66  );.  pExpr->pLef
1fb59 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  t = 0;.  sqlite3
1fb5a 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
1fb5b 45 78 70 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20  Expr->pRight);. 
1fb5c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 3d   pExpr->pRight =
1fb5d 20 30 3b 0a 20 20 70 45 78 70 72 2d 3e 6f 70 20   0;.  pExpr->op 
1fb5e 3d 20 28 69 73 54 72 69 67 67 65 72 20 3f 20 54  = (isTrigger ? T
1fb5f 4b 5f 54 52 49 47 47 45 52 20 3a 20 54 4b 5f 43  K_TRIGGER : TK_C
1fb60 4f 4c 55 4d 4e 29 3b 0a 6c 6f 6f 6b 75 70 6e 61  OLUMN);.lookupna
1fb61 6d 65 5f 65 6e 64 3a 0a 20 20 69 66 28 20 63 6e  me_end:.  if( cn
1fb62 74 3d 3d 31 20 29 7b 0a 20 20 20 20 61 73 73 65  t==1 ){.    asse
1fb63 72 74 28 20 70 4e 43 21 3d 30 20 29 3b 0a 20 20  rt( pNC!=0 );.  
1fb64 20 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61    sqlite3AuthRea
1fb65 64 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c  d(pParse, pExpr,
1fb66 20 70 53 63 68 65 6d 61 2c 20 70 4e 43 2d 3e 70   pSchema, pNC->p
1fb67 53 72 63 4c 69 73 74 29 3b 0a 20 20 20 20 2f 2a  SrcList);.    /*
1fb68 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 6e   Increment the n
1fb69 52 65 66 20 76 61 6c 75 65 20 6f 6e 20 61 6c 6c  Ref value on all
1fb6a 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 73 20 66   name contexts f
1fb6b 72 6f 6d 20 54 6f 70 4e 43 20 75 70 20 74 6f 0a  rom TopNC up to.
1fb6c 20 20 20 20 2a 2a 20 74 68 65 20 70 6f 69 6e 74      ** the point
1fb6d 20 77 68 65 72 65 20 74 68 65 20 6e 61 6d 65 20   where the name 
1fb6e 6d 61 74 63 68 65 64 2e 20 2a 2f 0a 20 20 20 20  matched. */.    
1fb6f 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 20 20 61  for(;;){.      a
1fb70 73 73 65 72 74 28 20 70 54 6f 70 4e 43 21 3d 30  ssert( pTopNC!=0
1fb71 20 29 3b 0a 20 20 20 20 20 20 70 54 6f 70 4e 43   );.      pTopNC
1fb72 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20  ->nRef++;.      
1fb73 69 66 28 20 70 54 6f 70 4e 43 3d 3d 70 4e 43 20  if( pTopNC==pNC 
1fb74 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 70  ) break;.      p
1fb75 54 6f 70 4e 43 20 3d 20 70 54 6f 70 4e 43 2d 3e  TopNC = pTopNC->
1fb76 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20  pNext;.    }.   
1fb77 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e   return WRC_Prun
1fb78 65 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  e;.  } else {.  
1fb79 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f    return WRC_Abo
1fb7a 72 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  rt;.  }.}../*.**
1fb7b 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
1fb7c 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 73 71   callback for sq
1fb7d 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 29 2e  lite3WalkExpr().
1fb7e 0a 2a 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 73  .**.** Resolve s
1fb7f 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 73 20 69 6e  ymbolic names in
1fb80 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20 6f 70 65  to TK_COLUMN ope
1fb81 72 61 74 6f 72 73 20 66 6f 72 20 74 68 65 20 63  rators for the c
1fb82 75 72 72 65 6e 74 0a 2a 2a 20 6e 6f 64 65 20 69  urrent.** node i
1fb83 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  n the expression
1fb84 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 30   tree.  Return 0
1fb85 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74 68 65   to continue the
1fb86 20 73 65 61 72 63 68 20 64 6f 77 6e 0a 2a 2a 20   search down.** 
1fb87 74 68 65 20 74 72 65 65 20 6f 72 20 32 20 74 6f  the tree or 2 to
1fb88 20 61 62 6f 72 74 20 74 68 65 20 74 72 65 65 20   abort the tree 
1fb89 77 61 6c 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  walk..**.** This
1fb8a 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 64 6f   routine also do
1fb8b 65 73 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e  es error checkin
1fb8c 67 20 61 6e 64 20 6e 61 6d 65 20 72 65 73 6f 6c  g and name resol
1fb8d 75 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 66 75 6e  ution for.** fun
1fb8e 63 74 69 6f 6e 20 6e 61 6d 65 73 2e 20 20 54 68  ction names.  Th
1fb8f 65 20 6f 70 65 72 61 74 6f 72 20 66 6f 72 20 61  e operator for a
1fb90 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
1fb91 6e 73 20 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a  ns is changed.**
1fb92 20 74 6f 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54   to TK_AGG_FUNCT
1fb93 49 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ION..*/.static i
1fb94 6e 74 20 72 65 73 6f 6c 76 65 45 78 70 72 53 74  nt resolveExprSt
1fb95 65 70 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b  ep(Walker *pWalk
1fb96 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29  er, Expr *pExpr)
1fb97 7b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  {.  NameContext 
1fb98 2a 70 4e 43 3b 0a 20 20 50 61 72 73 65 20 2a 70  *pNC;.  Parse *p
1fb99 50 61 72 73 65 3b 0a 0a 20 20 70 4e 43 20 3d 20  Parse;..  pNC = 
1fb9a 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a  pWalker->u.pNC;.
1fb9b 20 20 61 73 73 65 72 74 28 20 70 4e 43 21 3d 30    assert( pNC!=0
1fb9c 20 29 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 70   );.  pParse = p
1fb9d 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 61 73  NC->pParse;.  as
1fb9e 73 65 72 74 28 20 70 50 61 72 73 65 3d 3d 70 57  sert( pParse==pW
1fb9f 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 20 29 3b  alker->pParse );
1fba0 0a 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 41  ..  if( ExprHasA
1fba1 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  nyProperty(pExpr
1fba2 2c 20 45 50 5f 52 65 73 6f 6c 76 65 64 29 20 29  , EP_Resolved) )
1fba3 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e   return WRC_Prun
1fba4 65 3b 0a 20 20 45 78 70 72 53 65 74 50 72 6f 70  e;.  ExprSetProp
1fba5 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 52  erty(pExpr, EP_R
1fba6 65 73 6f 6c 76 65 64 29 3b 0a 23 69 66 6e 64 65  esolved);.#ifnde
1fba7 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 70  f NDEBUG.  if( p
1fba8 4e 43 2d 3e 70 53 72 63 4c 69 73 74 20 26 26 20  NC->pSrcList && 
1fba9 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 2d 3e 6e  pNC->pSrcList->n
1fbaa 41 6c 6c 6f 63 3e 30 20 29 7b 0a 20 20 20 20 53  Alloc>0 ){.    S
1fbab 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74  rcList *pSrcList
1fbac 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74   = pNC->pSrcList
1fbad 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  ;.    int i;.   
1fbae 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4e 43 2d   for(i=0; i<pNC-
1fbaf 3e 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3b  >pSrcList->nSrc;
1fbb0 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73   i++){.      ass
1fbb1 65 72 74 28 20 70 53 72 63 4c 69 73 74 2d 3e 61  ert( pSrcList->a
1fbb2 5b 69 5d 2e 69 43 75 72 73 6f 72 3e 3d 30 20 26  [i].iCursor>=0 &
1fbb3 26 20 70 53 72 63 4c 69 73 74 2d 3e 61 5b 69 5d  & pSrcList->a[i]
1fbb4 2e 69 43 75 72 73 6f 72 3c 70 50 61 72 73 65 2d  .iCursor<pParse-
1fbb5 3e 6e 54 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20  >nTab);.    }.  
1fbb6 7d 0a 23 65 6e 64 69 66 0a 20 20 73 77 69 74 63  }.#endif.  switc
1fbb7 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a  h( pExpr->op ){.
1fbb8 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
1fbb9 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54  ITE_ENABLE_UPDAT
1fbba 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20  E_DELETE_LIMIT) 
1fbbb 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
1fbbc 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
1fbbd 29 0a 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65  ).    /* The spe
1fbbe 63 69 61 6c 20 6f 70 65 72 61 74 6f 72 20 54 4b  cial operator TK
1fbbf 5f 52 4f 57 20 6d 65 61 6e 73 20 75 73 65 20 74  _ROW means use t
1fbc0 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65  he rowid for the
1fbc1 20 66 69 72 73 74 0a 20 20 20 20 2a 2a 20 63 6f   first.    ** co
1fbc2 6c 75 6d 6e 20 69 6e 20 74 68 65 20 46 52 4f 4d  lumn in the FROM
1fbc3 20 63 6c 61 75 73 65 2e 20 20 54 68 69 73 20 69   clause.  This i
1fbc4 73 20 75 73 65 64 20 62 79 20 74 68 65 20 4c 49  s used by the LI
1fbc5 4d 49 54 20 61 6e 64 20 4f 52 44 45 52 20 42 59  MIT and ORDER BY
1fbc6 0a 20 20 20 20 2a 2a 20 63 6c 61 75 73 65 20 70  .    ** clause p
1fbc7 72 6f 63 65 73 73 69 6e 67 20 6f 6e 20 55 50 44  rocessing on UPD
1fbc8 41 54 45 20 61 6e 64 20 44 45 4c 45 54 45 20 73  ATE and DELETE s
1fbc9 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a  tatements..    *
1fbca 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 4f  /.    case TK_RO
1fbcb 57 3a 20 7b 0a 20 20 20 20 20 20 53 72 63 4c 69  W: {.      SrcLi
1fbcc 73 74 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70  st *pSrcList = p
1fbcd 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 20 20  NC->pSrcList;.  
1fbce 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69      struct SrcLi
1fbcf 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
1fbd0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53        assert( pS
1fbd1 72 63 4c 69 73 74 20 26 26 20 70 53 72 63 4c 69  rcList && pSrcLi
1fbd2 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20  st->nSrc==1 );. 
1fbd3 20 20 20 20 20 70 49 74 65 6d 20 3d 20 70 53 72       pItem = pSr
1fbd4 63 4c 69 73 74 2d 3e 61 3b 20 0a 20 20 20 20 20  cList->a; .     
1fbd5 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f   pExpr->op = TK_
1fbd6 43 4f 4c 55 4d 4e 3b 0a 20 20 20 20 20 20 70 45  COLUMN;.      pE
1fbd7 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 49 74 65  xpr->pTab = pIte
1fbd8 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 70  m->pTab;.      p
1fbd9 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70  Expr->iTable = p
1fbda 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20  Item->iCursor;. 
1fbdb 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c       pExpr->iCol
1fbdc 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20  umn = -1;.      
1fbdd 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20  pExpr->affinity 
1fbde 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54  = SQLITE_AFF_INT
1fbdf 45 47 45 52 3b 0a 20 20 20 20 20 20 62 72 65 61  EGER;.      brea
1fbe0 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20  k;.    }.#endif 
1fbe1 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  /* defined(SQLIT
1fbe2 45 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f  E_ENABLE_UPDATE_
1fbe3 44 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26  DELETE_LIMIT) &&
1fbe4 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1fbe5 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20  _OMIT_SUBQUERY) 
1fbe6 2a 2f 0a 0a 20 20 20 20 2f 2a 20 41 20 6c 6f 6e  */..    /* A lon
1fbe7 65 20 69 64 65 6e 74 69 66 69 65 72 20 69 73 20  e identifier is 
1fbe8 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 63 6f  the name of a co
1fbe9 6c 75 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  lumn..    */.   
1fbea 20 63 61 73 65 20 54 4b 5f 49 44 3a 20 7b 0a 20   case TK_ID: {. 
1fbeb 20 20 20 20 20 72 65 74 75 72 6e 20 6c 6f 6f 6b       return look
1fbec 75 70 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 30  upName(pParse, 0
1fbed 2c 20 30 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54  , 0, pExpr->u.zT
1fbee 6f 6b 65 6e 2c 20 70 4e 43 2c 20 70 45 78 70 72  oken, pNC, pExpr
1fbef 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20  );.    }.  .    
1fbf0 2f 2a 20 41 20 74 61 62 6c 65 20 6e 61 6d 65 20  /* A table name 
1fbf1 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 3a  and column name:
1fbf2 20 20 20 20 20 49 44 2e 49 44 0a 20 20 20 20 2a       ID.ID.    *
1fbf3 2a 20 4f 72 20 61 20 64 61 74 61 62 61 73 65 2c  * Or a database,
1fbf4 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d   table and colum
1fbf5 6e 3a 20 20 49 44 2e 49 44 2e 49 44 0a 20 20 20  n:  ID.ID.ID.   
1fbf6 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   */.    case TK_
1fbf7 44 4f 54 3a 20 7b 0a 20 20 20 20 20 20 63 6f 6e  DOT: {.      con
1fbf8 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e  st char *zColumn
1fbf9 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  ;.      const ch
1fbfa 61 72 20 2a 7a 54 61 62 6c 65 3b 0a 20 20 20 20  ar *zTable;.    
1fbfb 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
1fbfc 62 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  b;.      Expr *p
1fbfd 52 69 67 68 74 3b 0a 0a 20 20 20 20 20 20 2f 2a  Right;..      /*
1fbfe 20 69 66 28 20 70 53 72 63 4c 69 73 74 3d 3d 30   if( pSrcList==0
1fbff 20 29 20 62 72 65 61 6b 3b 20 2a 2f 0a 20 20 20   ) break; */.   
1fc00 20 20 20 70 52 69 67 68 74 20 3d 20 70 45 78 70     pRight = pExp
1fc01 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20  r->pRight;.     
1fc02 20 69 66 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d   if( pRight->op=
1fc03 3d 54 4b 5f 49 44 20 29 7b 0a 20 20 20 20 20 20  =TK_ID ){.      
1fc04 20 20 7a 44 62 20 3d 20 30 3b 0a 20 20 20 20 20    zDb = 0;.     
1fc05 20 20 20 7a 54 61 62 6c 65 20 3d 20 70 45 78 70     zTable = pExp
1fc06 72 2d 3e 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b  r->pLeft->u.zTok
1fc07 65 6e 3b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c  en;.        zCol
1fc08 75 6d 6e 20 3d 20 70 52 69 67 68 74 2d 3e 75 2e  umn = pRight->u.
1fc09 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 7d 65  zToken;.      }e
1fc0a 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73  lse{.        ass
1fc0b 65 72 74 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d  ert( pRight->op=
1fc0c 3d 54 4b 5f 44 4f 54 20 29 3b 0a 20 20 20 20 20  =TK_DOT );.     
1fc0d 20 20 20 7a 44 62 20 3d 20 70 45 78 70 72 2d 3e     zDb = pExpr->
1fc0e 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b  pLeft->u.zToken;
1fc0f 0a 20 20 20 20 20 20 20 20 7a 54 61 62 6c 65 20  .        zTable 
1fc10 3d 20 70 52 69 67 68 74 2d 3e 70 4c 65 66 74 2d  = pRight->pLeft-
1fc11 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20  >u.zToken;.     
1fc12 20 20 20 7a 43 6f 6c 75 6d 6e 20 3d 20 70 52 69     zColumn = pRi
1fc13 67 68 74 2d 3e 70 52 69 67 68 74 2d 3e 75 2e 7a  ght->pRight->u.z
1fc14 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  Token;.      }. 
1fc15 20 20 20 20 20 72 65 74 75 72 6e 20 6c 6f 6f 6b       return look
1fc16 75 70 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a  upName(pParse, z
1fc17 44 62 2c 20 7a 54 61 62 6c 65 2c 20 7a 43 6f 6c  Db, zTable, zCol
1fc18 75 6d 6e 2c 20 70 4e 43 2c 20 70 45 78 70 72 29  umn, pNC, pExpr)
1fc19 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
1fc1a 52 65 73 6f 6c 76 65 20 66 75 6e 63 74 69 6f 6e  Resolve function
1fc1b 20 6e 61 6d 65 73 0a 20 20 20 20 2a 2f 0a 20 20   names.    */.  
1fc1c 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f    case TK_CONST_
1fc1d 46 55 4e 43 3a 0a 20 20 20 20 63 61 73 65 20 54  FUNC:.    case T
1fc1e 4b 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20  K_FUNCTION: {.  
1fc1f 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c      ExprList *pL
1fc20 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70  ist = pExpr->x.p
1fc21 4c 69 73 74 3b 20 20 20 20 2f 2a 20 54 68 65 20  List;    /* The 
1fc22 61 72 67 75 6d 65 6e 74 20 6c 69 73 74 20 2a 2f  argument list */
1fc23 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 70  .      int n = p
1fc24 4c 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45  List ? pList->nE
1fc25 78 70 72 20 3a 20 30 3b 20 20 20 20 2f 2a 20 4e  xpr : 0;    /* N
1fc26 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
1fc27 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  ts */.      int 
1fc28 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20 30  no_such_func = 0
1fc29 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  ;       /* True 
1fc2a 69 66 20 6e 6f 20 73 75 63 68 20 66 75 6e 63 74  if no such funct
1fc2b 69 6f 6e 20 65 78 69 73 74 73 20 2a 2f 0a 20 20  ion exists */.  
1fc2c 20 20 20 20 69 6e 74 20 77 72 6f 6e 67 5f 6e 75      int wrong_nu
1fc2d 6d 5f 61 72 67 73 20 3d 20 30 3b 20 20 20 20 20  m_args = 0;     
1fc2e 2f 2a 20 54 72 75 65 20 69 66 20 77 72 6f 6e 67  /* True if wrong
1fc2f 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   number of argum
1fc30 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e  ents */.      in
1fc31 74 20 69 73 5f 61 67 67 20 3d 20 30 3b 20 20 20  t is_agg = 0;   
1fc32 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
1fc33 65 20 69 66 20 69 73 20 61 6e 20 61 67 67 72 65  e if is an aggre
1fc34 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  gate function */
1fc35 0a 20 20 20 20 20 20 69 6e 74 20 61 75 74 68 3b  .      int auth;
1fc36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc37 20 20 20 2f 2a 20 41 75 74 68 6f 72 69 7a 61 74     /* Authorizat
1fc38 69 6f 6e 20 74 6f 20 75 73 65 20 74 68 65 20 66  ion to use the f
1fc39 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  unction */.     
1fc3a 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20 20 20   int nId;       
1fc3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1fc3c 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63  Number of charac
1fc3d 74 65 72 73 20 69 6e 20 66 75 6e 63 74 69 6f 6e  ters in function
1fc3e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 63   name */.      c
1fc3f 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 64 3b 20  onst char *zId; 
1fc40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1fc41 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 2e  e function name.
1fc42 20 2a 2f 0a 20 20 20 20 20 20 46 75 6e 63 44 65   */.      FuncDe
1fc43 66 20 2a 70 44 65 66 3b 20 20 20 20 20 20 20 20  f *pDef;        
1fc44 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61        /* Informa
1fc45 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66  tion about the f
1fc46 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  unction */.     
1fc47 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 70 50   u8 enc = ENC(pP
1fc48 61 72 73 65 2d 3e 64 62 29 3b 20 20 20 2f 2a 20  arse->db);   /* 
1fc49 54 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 63  The database enc
1fc4a 6f 64 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 20 20  oding */..      
1fc4b 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d  testcase( pExpr-
1fc4c 3e 6f 70 3d 3d 54 4b 5f 43 4f 4e 53 54 5f 46 55  >op==TK_CONST_FU
1fc4d 4e 43 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  NC );.      asse
1fc4e 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
1fc4f 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78  erty(pExpr, EP_x
1fc50 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20  IsSelect) );.   
1fc51 20 20 20 7a 49 64 20 3d 20 70 45 78 70 72 2d 3e     zId = pExpr->
1fc52 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20  u.zToken;.      
1fc53 6e 49 64 20 3d 20 73 71 6c 69 74 65 33 53 74 72  nId = sqlite3Str
1fc54 6c 65 6e 33 30 28 7a 49 64 29 3b 0a 20 20 20 20  len30(zId);.    
1fc55 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33    pDef = sqlite3
1fc56 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61  FindFunction(pPa
1fc57 72 73 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e 49  rse->db, zId, nI
1fc58 64 2c 20 6e 2c 20 65 6e 63 2c 20 30 29 3b 0a 20  d, n, enc, 0);. 
1fc59 20 20 20 20 20 69 66 28 20 70 44 65 66 3d 3d 30       if( pDef==0
1fc5a 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 65 66   ){.        pDef
1fc5b 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75   = sqlite3FindFu
1fc5c 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2d 3e 64  nction(pParse->d
1fc5d 62 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 2d 31 2c  b, zId, nId, -1,
1fc5e 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20   enc, 0);.      
1fc5f 20 20 69 66 28 20 70 44 65 66 3d 3d 30 20 29 7b    if( pDef==0 ){
1fc60 0a 20 20 20 20 20 20 20 20 20 20 6e 6f 5f 73 75  .          no_su
1fc61 63 68 5f 66 75 6e 63 20 3d 20 31 3b 0a 20 20 20  ch_func = 1;.   
1fc62 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1fc63 20 20 20 20 20 20 77 72 6f 6e 67 5f 6e 75 6d 5f        wrong_num_
1fc64 61 72 67 73 20 3d 20 31 3b 0a 20 20 20 20 20 20  args = 1;.      
1fc65 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b    }.      }else{
1fc66 0a 20 20 20 20 20 20 20 20 69 73 5f 61 67 67 20  .        is_agg 
1fc67 3d 20 70 44 65 66 2d 3e 78 46 75 6e 63 3d 3d 30  = pDef->xFunc==0
1fc68 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65  ;.      }.#ifnde
1fc69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
1fc6a 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20  THORIZATION.    
1fc6b 20 20 69 66 28 20 70 44 65 66 20 29 7b 0a 20 20    if( pDef ){.  
1fc6c 20 20 20 20 20 20 61 75 74 68 20 3d 20 73 71 6c        auth = sql
1fc6d 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
1fc6e 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 46 55 4e  arse, SQLITE_FUN
1fc6f 43 54 49 4f 4e 2c 20 30 2c 20 70 44 65 66 2d 3e  CTION, 0, pDef->
1fc70 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20  zName, 0);.     
1fc71 20 20 20 69 66 28 20 61 75 74 68 21 3d 53 51 4c     if( auth!=SQL
1fc72 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1fc73 20 20 20 20 69 66 28 20 61 75 74 68 3d 3d 53 51      if( auth==SQ
1fc74 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20  LITE_DENY ){.   
1fc75 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1fc76 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
1fc77 20 22 6e 6f 74 20 61 75 74 68 6f 72 69 7a 65 64   "not authorized
1fc78 20 74 6f 20 75 73 65 20 66 75 6e 63 74 69 6f 6e   to use function
1fc79 3a 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20  : %s",.         
1fc7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc7b 20 20 20 20 20 20 20 20 20 20 20 70 44 65 66 2d             pDef-
1fc7c 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20  >zName);.       
1fc7d 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b       pNC->nErr++
1fc7e 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
1fc7f 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f          pExpr->o
1fc80 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 20  p = TK_NULL;.   
1fc81 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52         return WR
1fc82 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 20 20 20  C_Prune;.       
1fc83 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69   }.      }.#endi
1fc84 66 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61  f.      if( is_a
1fc85 67 67 20 26 26 20 21 70 4e 43 2d 3e 61 6c 6c 6f  gg && !pNC->allo
1fc86 77 41 67 67 20 29 7b 0a 20 20 20 20 20 20 20 20  wAgg ){.        
1fc87 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1fc88 70 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65 20  pParse, "misuse 
1fc89 6f 66 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  of aggregate fun
1fc8a 63 74 69 6f 6e 20 25 2e 2a 73 28 29 22 2c 20 6e  ction %.*s()", n
1fc8b 49 64 2c 7a 49 64 29 3b 0a 20 20 20 20 20 20 20  Id,zId);.       
1fc8c 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20   pNC->nErr++;.  
1fc8d 20 20 20 20 20 20 69 73 5f 61 67 67 20 3d 20 30        is_agg = 0
1fc8e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
1fc8f 28 20 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 29  ( no_such_func )
1fc90 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1fc91 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1fc92 2c 20 22 6e 6f 20 73 75 63 68 20 66 75 6e 63 74  , "no such funct
1fc93 69 6f 6e 3a 20 25 2e 2a 73 22 2c 20 6e 49 64 2c  ion: %.*s", nId,
1fc94 20 7a 49 64 29 3b 0a 20 20 20 20 20 20 20 20 70   zId);.        p
1fc95 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20  NC->nErr++;.    
1fc96 20 20 7d 65 6c 73 65 20 69 66 28 20 77 72 6f 6e    }else if( wron
1fc97 67 5f 6e 75 6d 5f 61 72 67 73 20 29 7b 0a 20 20  g_num_args ){.  
1fc98 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1fc99 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22 77 72  orMsg(pParse,"wr
1fc9a 6f 6e 67 20 6e 75 6d 62 65 72 20 6f 66 20 61 72  ong number of ar
1fc9b 67 75 6d 65 6e 74 73 20 74 6f 20 66 75 6e 63 74  guments to funct
1fc9c 69 6f 6e 20 25 2e 2a 73 28 29 22 2c 0a 20 20 20  ion %.*s()",.   
1fc9d 20 20 20 20 20 20 20 20 20 20 6e 49 64 2c 20 7a            nId, z
1fc9e 49 64 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43  Id);.        pNC
1fc9f 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20  ->nErr++;.      
1fca0 7d 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61  }.      if( is_a
1fca1 67 67 20 29 7b 0a 20 20 20 20 20 20 20 20 70 45  gg ){.        pE
1fca2 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 41 47 47  xpr->op = TK_AGG
1fca3 5f 46 55 4e 43 54 49 4f 4e 3b 0a 20 20 20 20 20  _FUNCTION;.     
1fca4 20 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d     pNC->hasAgg =
1fca5 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
1fca6 20 20 69 66 28 20 69 73 5f 61 67 67 20 29 20 70    if( is_agg ) p
1fca7 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 3d 20 30  NC->allowAgg = 0
1fca8 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 57  ;.      sqlite3W
1fca9 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c  alkExprList(pWal
1fcaa 6b 65 72 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20  ker, pList);.   
1fcab 20 20 20 69 66 28 20 69 73 5f 61 67 67 20 29 20     if( is_agg ) 
1fcac 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 3d 20  pNC->allowAgg = 
1fcad 31 3b 0a 20 20 20 20 20 20 2f 2a 20 46 49 58 20  1;.      /* FIX 
1fcae 4d 45 3a 20 20 43 6f 6d 70 75 74 65 20 70 45 78  ME:  Compute pEx
1fcaf 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 62 61 73  pr->affinity bas
1fcb0 65 64 20 6f 6e 20 74 68 65 20 65 78 70 65 63 74  ed on the expect
1fcb1 65 64 20 72 65 74 75 72 6e 0a 20 20 20 20 20 20  ed return.      
1fcb2 2a 2a 20 74 79 70 65 20 6f 66 20 74 68 65 20 66  ** type of the f
1fcb3 75 6e 63 74 69 6f 6e 20 0a 20 20 20 20 20 20 2a  unction .      *
1fcb4 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 57  /.      return W
1fcb5 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 7d 0a  RC_Prune;.    }.
1fcb6 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1fcb7 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20  MIT_SUBQUERY.   
1fcb8 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a   case TK_SELECT:
1fcb9 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49  .    case TK_EXI
1fcba 53 54 53 3a 20 20 74 65 73 74 63 61 73 65 28 20  STS:  testcase( 
1fcbb 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58  pExpr->op==TK_EX
1fcbc 49 53 54 53 20 29 3b 0a 23 65 6e 64 69 66 0a 20  ISTS );.#endif. 
1fcbd 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a 20 7b     case TK_IN: {
1fcbe 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1fcbf 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49   pExpr->op==TK_I
1fcc0 4e 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 45  N );.      if( E
1fcc1 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
1fcc2 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65  Expr, EP_xIsSele
1fcc3 63 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ct) ){.        i
1fcc4 6e 74 20 6e 52 65 66 20 3d 20 70 4e 43 2d 3e 6e  nt nRef = pNC->n
1fcc5 52 65 66 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  Ref;.#ifndef SQL
1fcc6 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20  ITE_OMIT_CHECK. 
1fcc7 20 20 20 20 20 20 20 69 66 28 20 70 4e 43 2d 3e         if( pNC->
1fcc8 69 73 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 20  isCheck ){.     
1fcc9 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1fcca 72 4d 73 67 28 70 50 61 72 73 65 2c 22 73 75 62  rMsg(pParse,"sub
1fccb 71 75 65 72 69 65 73 20 70 72 6f 68 69 62 69 74  queries prohibit
1fccc 65 64 20 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73  ed in CHECK cons
1fccd 74 72 61 69 6e 74 73 22 29 3b 0a 20 20 20 20 20  traints");.     
1fcce 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
1fccf 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53      sqlite3WalkS
1fcd0 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70  elect(pWalker, p
1fcd1 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29  Expr->x.pSelect)
1fcd2 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
1fcd3 28 20 70 4e 43 2d 3e 6e 52 65 66 3e 3d 6e 52 65  ( pNC->nRef>=nRe
1fcd4 66 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  f );.        if(
1fcd5 20 6e 52 65 66 21 3d 70 4e 43 2d 3e 6e 52 65 66   nRef!=pNC->nRef
1fcd6 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78   ){.          Ex
1fcd7 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 45  prSetProperty(pE
1fcd8 78 70 72 2c 20 45 50 5f 56 61 72 53 65 6c 65 63  xpr, EP_VarSelec
1fcd9 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  t);.        }.  
1fcda 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
1fcdb 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  k;.    }.#ifndef
1fcdc 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45   SQLITE_OMIT_CHE
1fcdd 43 4b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 56  CK.    case TK_V
1fcde 41 52 49 41 42 4c 45 3a 20 7b 0a 20 20 20 20 20  ARIABLE: {.     
1fcdf 20 69 66 28 20 70 4e 43 2d 3e 69 73 43 68 65 63   if( pNC->isChec
1fce0 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  k ){.        sql
1fce1 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1fce2 72 73 65 2c 22 70 61 72 61 6d 65 74 65 72 73 20  rse,"parameters 
1fce3 70 72 6f 68 69 62 69 74 65 64 20 69 6e 20 43 48  prohibited in CH
1fce4 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 22  ECK constraints"
1fce5 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1fce6 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
1fce7 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72  ndif.  }.  retur
1fce8 6e 20 28 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  n (pParse->nErr 
1fce9 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d  || pParse->db->m
1fcea 61 6c 6c 6f 63 46 61 69 6c 65 64 29 20 3f 20 57  allocFailed) ? W
1fceb 52 43 5f 41 62 6f 72 74 20 3a 20 57 52 43 5f 43  RC_Abort : WRC_C
1fcec 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  ontinue;.}../*.*
1fced 2a 20 70 45 4c 69 73 74 20 69 73 20 61 20 6c 69  * pEList is a li
1fcee 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e  st of expression
1fcef 73 20 77 68 69 63 68 20 61 72 65 20 72 65 61 6c  s which are real
1fcf0 6c 79 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  ly the result se
1fcf1 74 20 6f 66 20 74 68 65 0a 2a 2a 20 61 20 53 45  t of the.** a SE
1fcf2 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20  LECT statement. 
1fcf3 20 70 45 20 69 73 20 61 20 74 65 72 6d 20 69 6e   pE is a term in
1fcf4 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20   an ORDER BY or 
1fcf5 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e  GROUP BY clause.
1fcf6 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1fcf7 20 63 68 65 63 6b 73 20 74 6f 20 73 65 65 20 69   checks to see i
1fcf8 66 20 70 45 20 69 73 20 61 20 73 69 6d 70 6c 65  f pE is a simple
1fcf9 20 69 64 65 6e 74 69 66 69 65 72 20 77 68 69 63   identifier whic
1fcfa 68 20 63 6f 72 72 65 73 70 6f 6e 64 73 0a 2a 2a  h corresponds.**
1fcfb 20 74 6f 20 74 68 65 20 41 53 2d 6e 61 6d 65 20   to the AS-name 
1fcfc 6f 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 65  of one of the te
1fcfd 72 6d 73 20 6f 66 20 74 68 65 20 65 78 70 72 65  rms of the expre
1fcfe 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20 49 66 20  ssion list.  If 
1fcff 69 74 20 69 73 2c 0a 2a 2a 20 74 68 69 73 20 72  it is,.** this r
1fd00 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 20 61 6e  outine return an
1fd01 20 69 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e   integer between
1fd02 20 31 20 61 6e 64 20 4e 20 77 68 65 72 65 20 4e   1 and N where N
1fd03 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
1fd04 66 0a 2a 2a 20 65 6c 65 6d 65 6e 74 73 20 69 6e  f.** elements in
1fd05 20 70 45 4c 69 73 74 2c 20 63 6f 72 72 65 73 70   pEList, corresp
1fd06 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 6d 61  onding to the ma
1fd07 74 63 68 69 6e 67 20 65 6e 74 72 79 2e 20 20 49  tching entry.  I
1fd08 66 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f  f there is.** no
1fd09 20 6d 61 74 63 68 2c 20 6f 72 20 69 66 20 70 45   match, or if pE
1fd0a 20 69 73 20 6e 6f 74 20 61 20 73 69 6d 70 6c 65   is not a simple
1fd0b 20 69 64 65 6e 74 69 66 69 65 72 2c 20 74 68 65   identifier, the
1fd0c 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  n this routine.*
1fd0d 2a 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a  * return 0..**.*
1fd0e 2a 20 70 45 4c 69 73 74 20 68 61 73 20 62 65 65  * pEList has bee
1fd0f 6e 20 72 65 73 6f 6c 76 65 64 2e 20 20 70 45 20  n resolved.  pE 
1fd10 68 61 73 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74  has not..*/.stat
1fd11 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 41 73  ic int resolveAs
1fd12 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a 70  Name(.  Parse *p
1fd13 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61  Parse,     /* Pa
1fd14 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f  rsing context fo
1fd15 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73  r error messages
1fd16 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
1fd17 70 45 4c 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74  pEList,  /* List
1fd18 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20   of expressions 
1fd19 74 6f 20 73 63 61 6e 20 2a 2f 0a 20 20 45 78 70  to scan */.  Exp
1fd1a 72 20 2a 70 45 20 20 20 20 20 20 20 20 20 20 20  r *pE           
1fd1b 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 77 65  /* Expression we
1fd1c 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f 20 6d   are trying to m
1fd1d 61 74 63 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  atch */.){.  int
1fd1e 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
1fd1f 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
1fd20 2a 2f 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  */..  UNUSED_PAR
1fd21 41 4d 45 54 45 52 28 70 50 61 72 73 65 29 3b 0a  AMETER(pParse);.
1fd22 0a 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54  .  if( pE->op==T
1fd23 4b 5f 49 44 20 29 7b 0a 20 20 20 20 63 68 61 72  K_ID ){.    char
1fd24 20 2a 7a 43 6f 6c 20 3d 20 70 45 2d 3e 75 2e 7a   *zCol = pE->u.z
1fd25 54 6f 6b 65 6e 3b 0a 20 20 20 20 66 6f 72 28 69  Token;.    for(i
1fd26 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45  =0; i<pEList->nE
1fd27 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  xpr; i++){.     
1fd28 20 63 68 61 72 20 2a 7a 41 73 20 3d 20 70 45 4c   char *zAs = pEL
1fd29 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3b  ist->a[i].zName;
1fd2a 0a 20 20 20 20 20 20 69 66 28 20 7a 41 73 21 3d  .      if( zAs!=
1fd2b 30 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49  0 && sqlite3StrI
1fd2c 43 6d 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d  Cmp(zAs, zCol)==
1fd2d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  0 ){.        ret
1fd2e 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20 7d  urn i+1;.      }
1fd2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
1fd30 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
1fd31 70 45 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  pE is a pointer 
1fd32 74 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  to an expression
1fd33 20 77 68 69 63 68 20 69 73 20 61 20 73 69 6e 67   which is a sing
1fd34 6c 65 20 74 65 72 6d 20 69 6e 20 74 68 65 0a 2a  le term in the.*
1fd35 2a 20 4f 52 44 45 52 20 42 59 20 6f 66 20 61 20  * ORDER BY of a 
1fd36 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 2e  compound SELECT.
1fd37 20 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e    The expression
1fd38 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 0a 2a 2a   has not been.**
1fd39 20 6e 61 6d 65 20 72 65 73 6f 6c 76 65 64 2e 0a   name resolved..
1fd3a 2a 2a 0a 2a 2a 20 41 74 20 74 68 65 20 70 6f 69  **.** At the poi
1fd3b 6e 74 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  nt this routine 
1fd3c 69 73 20 63 61 6c 6c 65 64 2c 20 77 65 20 61 6c  is called, we al
1fd3d 72 65 61 64 79 20 6b 6e 6f 77 20 74 68 61 74 20  ready know that 
1fd3e 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20  the.** ORDER BY 
1fd3f 74 65 72 6d 20 69 73 20 6e 6f 74 20 61 6e 20 69  term is not an i
1fd40 6e 74 65 67 65 72 20 69 6e 64 65 78 20 69 6e 74  nteger index int
1fd41 6f 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  o the result set
1fd42 2e 20 20 54 68 61 74 0a 2a 2a 20 63 61 73 65 20  .  That.** case 
1fd43 69 73 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68  is handled by th
1fd44 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e  e calling routin
1fd45 65 2e 0a 2a 2a 0a 2a 2a 20 41 74 74 65 6d 70 74  e..**.** Attempt
1fd46 20 74 6f 20 6d 61 74 63 68 20 70 45 20 61 67 61   to match pE aga
1fd47 69 6e 73 74 20 72 65 73 75 6c 74 20 73 65 74 20  inst result set 
1fd48 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6c  columns in the l
1fd49 65 66 74 2d 6d 6f 73 74 0a 2a 2a 20 53 45 4c 45  eft-most.** SELE
1fd4a 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 52  CT statement.  R
1fd4b 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20  eturn the index 
1fd4c 69 20 6f 66 20 74 68 65 20 6d 61 74 63 68 69 6e  i of the matchin
1fd4d 67 20 63 6f 6c 75 6d 6e 2c 0a 2a 2a 20 61 73 20  g column,.** as 
1fd4e 61 6e 20 69 6e 64 69 63 61 74 69 6f 6e 20 74 6f  an indication to
1fd4f 20 74 68 65 20 63 61 6c 6c 65 72 20 74 68 61 74   the caller that
1fd50 20 69 74 20 73 68 6f 75 6c 64 20 73 6f 72 74 20   it should sort 
1fd51 62 79 20 74 68 65 20 69 2d 74 68 20 63 6f 6c 75  by the i-th colu
1fd52 6d 6e 2e 0a 2a 2a 20 54 68 65 20 6c 65 66 74 2d  mn..** The left-
1fd53 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69 73 20 31  most column is 1
1fd54 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
1fd55 73 2c 20 74 68 65 20 76 61 6c 75 65 20 72 65 74  s, the value ret
1fd56 75 72 6e 65 64 20 69 73 20 74 68 65 0a 2a 2a 20  urned is the.** 
1fd57 73 61 6d 65 20 69 6e 74 65 67 65 72 20 76 61 6c  same integer val
1fd58 75 65 20 74 68 61 74 20 77 6f 75 6c 64 20 62 65  ue that would be
1fd59 20 75 73 65 64 20 69 6e 20 74 68 65 20 53 51 4c   used in the SQL
1fd5a 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 69 6e   statement to in
1fd5b 64 69 63 61 74 65 0a 2a 2a 20 74 68 65 20 63 6f  dicate.** the co
1fd5c 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  lumn..**.** If t
1fd5d 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68  here is no match
1fd5e 2c 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74  , return 0.  Ret
1fd5f 75 72 6e 20 2d 31 20 69 66 20 61 6e 20 65 72 72  urn -1 if an err
1fd60 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74  or occurs..*/.st
1fd61 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65  atic int resolve
1fd62 4f 72 64 65 72 42 79 54 65 72 6d 54 6f 45 78 70  OrderByTermToExp
1fd63 72 4c 69 73 74 28 0a 20 20 50 61 72 73 65 20 2a  rList(.  Parse *
1fd64 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50  pParse,     /* P
1fd65 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 66  arsing context f
1fd66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  or error message
1fd67 73 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  s */.  Select *p
1fd68 53 65 6c 65 63 74 2c 20 20 20 2f 2a 20 54 68 65  Select,   /* The
1fd69 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1fd6a 74 20 77 69 74 68 20 74 68 65 20 4f 52 44 45 52  t with the ORDER
1fd6b 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   BY clause */.  
1fd6c 45 78 70 72 20 2a 70 45 20 20 20 20 20 20 20 20  Expr *pE        
1fd6d 20 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69 66     /* The specif
1fd6e 69 63 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  ic ORDER BY term
1fd6f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20   */.){.  int i; 
1fd70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1fd71 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
1fd72 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
1fd73 74 3b 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d  t;  /* The colum
1fd74 6e 73 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  ns of the result
1fd75 20 73 65 74 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f   set */.  NameCo
1fd76 6e 74 65 78 74 20 6e 63 3b 20 20 20 20 2f 2a 20  ntext nc;    /* 
1fd77 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72  Name context for
1fd78 20 72 65 73 6f 6c 76 69 6e 67 20 70 45 20 2a 2f   resolving pE */
1fd79 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
1fd7a 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72  te3ExprIsInteger
1fd7b 28 70 45 2c 20 26 69 29 3d 3d 30 20 29 3b 0a 20  (pE, &i)==0 );. 
1fd7c 20 70 45 4c 69 73 74 20 3d 20 70 53 65 6c 65 63   pEList = pSelec
1fd7d 74 2d 3e 70 45 4c 69 73 74 3b 0a 0a 20 20 2f 2a  t->pEList;..  /*
1fd7e 20 52 65 73 6f 6c 76 65 20 61 6c 6c 20 6e 61 6d   Resolve all nam
1fd7f 65 73 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20  es in the ORDER 
1fd80 42 59 20 74 65 72 6d 20 65 78 70 72 65 73 73 69  BY term expressi
1fd81 6f 6e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74  on.  */.  memset
1fd82 28 26 6e 63 2c 20 30 2c 20 73 69 7a 65 6f 66 28  (&nc, 0, sizeof(
1fd83 6e 63 29 29 3b 0a 20 20 6e 63 2e 70 50 61 72 73  nc));.  nc.pPars
1fd84 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 6e 63  e = pParse;.  nc
1fd85 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 53 65 6c  .pSrcList = pSel
1fd86 65 63 74 2d 3e 70 53 72 63 3b 0a 20 20 6e 63 2e  ect->pSrc;.  nc.
1fd87 70 45 4c 69 73 74 20 3d 20 70 45 4c 69 73 74 3b  pEList = pEList;
1fd88 0a 20 20 6e 63 2e 61 6c 6c 6f 77 41 67 67 20 3d  .  nc.allowAgg =
1fd89 20 31 3b 0a 20 20 6e 63 2e 6e 45 72 72 20 3d 20   1;.  nc.nErr = 
1fd8a 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  0;.  if( sqlite3
1fd8b 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
1fd8c 28 26 6e 63 2c 20 70 45 29 20 29 7b 0a 20 20 20  (&nc, pE) ){.   
1fd8d 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65   sqlite3ErrorCle
1fd8e 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  ar(pParse);.    
1fd8f 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20  return 0;.  }.. 
1fd90 20 2f 2a 20 54 72 79 20 74 6f 20 6d 61 74 63 68   /* Try to match
1fd91 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 65 78   the ORDER BY ex
1fd92 70 72 65 73 73 69 6f 6e 20 61 67 61 69 6e 73 74  pression against
1fd93 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 20   an expression. 
1fd94 20 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c   ** in the resul
1fd95 74 20 73 65 74 2e 20 20 52 65 74 75 72 6e 20 61  t set.  Return a
1fd96 6e 20 31 2d 62 61 73 65 64 20 69 6e 64 65 78 20  n 1-based index 
1fd97 6f 66 20 74 68 65 20 6d 61 74 63 68 69 6e 67 0a  of the matching.
1fd98 20 20 2a 2a 20 72 65 73 75 6c 74 2d 73 65 74 20    ** result-set 
1fd99 65 6e 74 72 79 2e 0a 20 20 2a 2f 0a 20 20 66 6f  entry..  */.  fo
1fd9a 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d  r(i=0; i<pEList-
1fd9b 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20  >nExpr; i++){.  
1fd9c 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70    if( sqlite3Exp
1fd9d 72 43 6f 6d 70 61 72 65 28 70 45 4c 69 73 74 2d  rCompare(pEList-
1fd9e 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 70 45 29  >a[i].pExpr, pE)
1fd9f 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1fda0 20 69 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   i+1;.    }.  }.
1fda1 0a 20 20 2f 2a 20 49 66 20 6e 6f 20 6d 61 74 63  .  /* If no matc
1fda2 68 2c 20 72 65 74 75 72 6e 20 30 2e 20 2a 2f 0a  h, return 0. */.
1fda3 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
1fda4 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e  *.** Generate an
1fda5 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f   ORDER BY or GRO
1fda6 55 50 20 42 59 20 74 65 72 6d 20 6f 75 74 2d 6f  UP BY term out-o
1fda7 66 2d 72 61 6e 67 65 20 65 72 72 6f 72 2e 0a 2a  f-range error..*
1fda8 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  /.static void re
1fda9 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45  solveOutOfRangeE
1fdaa 72 72 6f 72 28 0a 20 20 50 61 72 73 65 20 2a 70  rror(.  Parse *p
1fdab 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f  Parse,         /
1fdac 2a 20 54 68 65 20 65 72 72 6f 72 20 63 6f 6e 74  * The error cont
1fdad 65 78 74 20 69 6e 74 6f 20 77 68 69 63 68 20 74  ext into which t
1fdae 6f 20 77 72 69 74 65 20 74 68 65 20 65 72 72 6f  o write the erro
1fdaf 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  r */.  const cha
1fdb0 72 20 2a 7a 54 79 70 65 2c 20 20 20 20 20 2f 2a  r *zType,     /*
1fdb1 20 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52 4f   "ORDER" or "GRO
1fdb2 55 50 22 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20  UP" */.  int i, 
1fdb3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fdb4 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 28 31 2d  /* The index (1-
1fdb5 62 61 73 65 64 29 20 6f 66 20 74 68 65 20 74 65  based) of the te
1fdb6 72 6d 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20  rm out of range 
1fdb7 2a 2f 0a 20 20 69 6e 74 20 6d 78 20 20 20 20 20  */.  int mx     
1fdb8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1fdb9 61 72 67 65 73 74 20 70 65 72 6d 69 73 73 69 62  argest permissib
1fdba 6c 65 20 76 61 6c 75 65 20 6f 66 20 69 20 2a 2f  le value of i */
1fdbb 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 45 72 72  .){.  sqlite3Err
1fdbc 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20  orMsg(pParse, . 
1fdbd 20 20 20 22 25 72 20 25 73 20 42 59 20 74 65 72     "%r %s BY ter
1fdbe 6d 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2d  m out of range -
1fdbf 20 73 68 6f 75 6c 64 20 62 65 20 22 0a 20 20 20   should be ".   
1fdc0 20 22 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20   "between 1 and 
1fdc1 25 64 22 2c 20 69 2c 20 7a 54 79 70 65 2c 20 6d  %d", i, zType, m
1fdc2 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61  x);.}../*.** Ana
1fdc3 6c 79 7a 65 20 74 68 65 20 4f 52 44 45 52 20 42  lyze the ORDER B
1fdc4 59 20 63 6c 61 75 73 65 20 69 6e 20 61 20 63 6f  Y clause in a co
1fdc5 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 73 74  mpound SELECT st
1fdc6 61 74 65 6d 65 6e 74 2e 20 20 20 4d 6f 64 69 66  atement.   Modif
1fdc7 79 0a 2a 2a 20 65 61 63 68 20 74 65 72 6d 20 6f  y.** each term o
1fdc8 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  f the ORDER BY c
1fdc9 6c 61 75 73 65 20 69 73 20 61 20 63 6f 6e 73 74  lause is a const
1fdca 61 6e 74 20 69 6e 74 65 67 65 72 20 62 65 74 77  ant integer betw
1fdcb 65 65 6e 20 31 0a 2a 2a 20 61 6e 64 20 4e 20 77  een 1.** and N w
1fdcc 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75  here N is the nu
1fdcd 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
1fdce 69 6e 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20  in the compound 
1fdcf 53 45 4c 45 43 54 2e 0a 2a 2a 0a 2a 2a 20 4f 52  SELECT..**.** OR
1fdd0 44 45 52 20 42 59 20 74 65 72 6d 73 20 74 68 61  DER BY terms tha
1fdd1 74 20 61 72 65 20 61 6c 72 65 61 64 79 20 61 6e  t are already an
1fdd2 20 69 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e   integer between
1fdd3 20 31 20 61 6e 64 20 4e 20 61 72 65 0a 2a 2a 20   1 and N are.** 
1fdd4 75 6e 6d 6f 64 69 66 69 65 64 2e 20 20 4f 52 44  unmodified.  ORD
1fdd5 45 52 20 42 59 20 74 65 72 6d 73 20 74 68 61 74  ER BY terms that
1fdd6 20 61 72 65 20 69 6e 74 65 67 65 72 73 20 6f 75   are integers ou
1fdd7 74 73 69 64 65 20 74 68 65 20 72 61 6e 67 65 20  tside the range 
1fdd8 6f 66 0a 2a 2a 20 31 20 74 68 72 6f 75 67 68 20  of.** 1 through 
1fdd9 4e 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65 72  N generate an er
1fdda 72 6f 72 2e 20 20 4f 52 44 45 52 20 42 59 20 74  ror.  ORDER BY t
1fddb 65 72 6d 73 20 74 68 61 74 20 61 72 65 20 65 78  erms that are ex
1fddc 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 61 72 65  pressions.** are
1fddd 20 6d 61 74 63 68 65 64 20 61 67 61 69 6e 73 74   matched against
1fdde 20 72 65 73 75 6c 74 20 73 65 74 20 65 78 70 72   result set expr
1fddf 65 73 73 69 6f 6e 73 20 6f 66 20 63 6f 6d 70 6f  essions of compo
1fde0 75 6e 64 20 53 45 4c 45 43 54 0a 2a 2a 20 62 65  und SELECT.** be
1fde1 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 74 68 65  ginning with the
1fde2 20 6c 65 66 74 2d 6d 6f 73 74 20 53 45 4c 45 43   left-most SELEC
1fde3 54 20 61 6e 64 20 77 6f 72 6b 69 6e 67 20 74 6f  T and working to
1fde4 77 61 72 64 20 74 68 65 20 72 69 67 68 74 2e 0a  ward the right..
1fde5 2a 2a 20 41 74 20 74 68 65 20 66 69 72 73 74 20  ** At the first 
1fde6 6d 61 74 63 68 2c 20 74 68 65 20 4f 52 44 45 52  match, the ORDER
1fde7 20 42 59 20 65 78 70 72 65 73 73 69 6f 6e 20 69   BY expression i
1fde8 73 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 69 6e  s transformed in
1fde9 74 6f 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65  to.** the intege
1fdea 72 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e  r column number.
1fdeb 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  .**.** Return th
1fdec 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f  e number of erro
1fded 72 73 20 73 65 65 6e 2e 0a 2a 2f 0a 73 74 61 74  rs seen..*/.stat
1fdee 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 43 6f  ic int resolveCo
1fdef 6d 70 6f 75 6e 64 4f 72 64 65 72 42 79 28 0a 20  mpoundOrderBy(. 
1fdf0 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
1fdf1 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
1fdf2 67 20 63 6f 6e 74 65 78 74 2e 20 20 4c 65 61 76  g context.  Leav
1fdf3 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73  e error messages
1fdf4 20 68 65 72 65 20 2a 2f 0a 20 20 53 65 6c 65 63   here */.  Selec
1fdf5 74 20 2a 70 53 65 6c 65 63 74 20 20 20 20 20 20  t *pSelect      
1fdf6 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73   /* The SELECT s
1fdf7 74 61 74 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e  tatement contain
1fdf8 69 6e 67 20 74 68 65 20 4f 52 44 45 52 20 42 59  ing the ORDER BY
1fdf9 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   */.){.  int i;.
1fdfa 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64    ExprList *pOrd
1fdfb 65 72 42 79 3b 0a 20 20 45 78 70 72 4c 69 73 74  erBy;.  ExprList
1fdfc 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73 71 6c 69   *pEList;.  sqli
1fdfd 74 65 33 20 2a 64 62 3b 0a 20 20 69 6e 74 20 6d  te3 *db;.  int m
1fdfe 6f 72 65 54 6f 44 6f 20 3d 20 31 3b 0a 0a 20 20  oreToDo = 1;..  
1fdff 70 4f 72 64 65 72 42 79 20 3d 20 70 53 65 6c 65  pOrderBy = pSele
1fe00 63 74 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20  ct->pOrderBy;.  
1fe01 69 66 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20  if( pOrderBy==0 
1fe02 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 64 62  ) return 0;.  db
1fe03 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23   = pParse->db;.#
1fe04 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f  if SQLITE_MAX_CO
1fe05 4c 55 4d 4e 0a 20 20 69 66 28 20 70 4f 72 64 65  LUMN.  if( pOrde
1fe06 72 42 79 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61  rBy->nExpr>db->a
1fe07 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
1fe08 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20  IT_COLUMN] ){.  
1fe09 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1fe0a 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d  g(pParse, "too m
1fe0b 61 6e 79 20 74 65 72 6d 73 20 69 6e 20 4f 52 44  any terms in ORD
1fe0c 45 52 20 42 59 20 63 6c 61 75 73 65 22 29 3b 0a  ER BY clause");.
1fe0d 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
1fe0e 7d 0a 23 65 6e 64 69 66 0a 20 20 66 6f 72 28 69  }.#endif.  for(i
1fe0f 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e  =0; i<pOrderBy->
1fe10 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nExpr; i++){.   
1fe11 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e   pOrderBy->a[i].
1fe12 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  done = 0;.  }.  
1fe13 70 53 65 6c 65 63 74 2d 3e 70 4e 65 78 74 20 3d  pSelect->pNext =
1fe14 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 70 53 65   0;.  while( pSe
1fe15 6c 65 63 74 2d 3e 70 50 72 69 6f 72 20 29 7b 0a  lect->pPrior ){.
1fe16 20 20 20 20 70 53 65 6c 65 63 74 2d 3e 70 50 72      pSelect->pPr
1fe17 69 6f 72 2d 3e 70 4e 65 78 74 20 3d 20 70 53 65  ior->pNext = pSe
1fe18 6c 65 63 74 3b 0a 20 20 20 20 70 53 65 6c 65 63  lect;.    pSelec
1fe19 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 50 72  t = pSelect->pPr
1fe1a 69 6f 72 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65  ior;.  }.  while
1fe1b 28 20 70 53 65 6c 65 63 74 20 26 26 20 6d 6f 72  ( pSelect && mor
1fe1c 65 54 6f 44 6f 20 29 7b 0a 20 20 20 20 73 74 72  eToDo ){.    str
1fe1d 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
1fe1e 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 6d 6f  m *pItem;.    mo
1fe1f 72 65 54 6f 44 6f 20 3d 20 30 3b 0a 20 20 20 20  reToDo = 0;.    
1fe20 70 45 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74  pEList = pSelect
1fe21 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 61 73  ->pEList;.    as
1fe22 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20  sert( pEList!=0 
1fe23 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20  );.    for(i=0, 
1fe24 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e  pItem=pOrderBy->
1fe25 61 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e  a; i<pOrderBy->n
1fe26 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  Expr; i++, pItem
1fe27 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  ++){.      int i
1fe28 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20  Col = -1;.      
1fe29 45 78 70 72 20 2a 70 45 2c 20 2a 70 44 75 70 3b  Expr *pE, *pDup;
1fe2a 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d  .      if( pItem
1fe2b 2d 3e 64 6f 6e 65 20 29 20 63 6f 6e 74 69 6e 75  ->done ) continu
1fe2c 65 3b 0a 20 20 20 20 20 20 70 45 20 3d 20 70 49  e;.      pE = pI
1fe2d 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20  tem->pExpr;.    
1fe2e 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70    if( sqlite3Exp
1fe2f 72 49 73 49 6e 74 65 67 65 72 28 70 45 2c 20 26  rIsInteger(pE, &
1fe30 69 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 20  iCol) ){.       
1fe31 20 69 66 28 20 69 43 6f 6c 3c 3d 30 20 7c 7c 20   if( iCol<=0 || 
1fe32 69 43 6f 6c 3e 70 45 4c 69 73 74 2d 3e 6e 45 78  iCol>pEList->nEx
1fe33 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  pr ){.          
1fe34 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67  resolveOutOfRang
1fe35 65 45 72 72 6f 72 28 70 50 61 72 73 65 2c 20 22  eError(pParse, "
1fe36 4f 52 44 45 52 22 2c 20 69 2b 31 2c 20 70 45 4c  ORDER", i+1, pEL
1fe37 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 20  ist->nExpr);.   
1fe38 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b         return 1;
1fe39 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1fe3a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1fe3b 69 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 65 41 73  iCol = resolveAs
1fe3c 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 45 4c  Name(pParse, pEL
1fe3d 69 73 74 2c 20 70 45 29 3b 0a 20 20 20 20 20 20  ist, pE);.      
1fe3e 20 20 69 66 28 20 69 43 6f 6c 3d 3d 30 20 29 7b    if( iCol==0 ){
1fe3f 0a 20 20 20 20 20 20 20 20 20 20 70 44 75 70 20  .          pDup 
1fe40 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
1fe41 28 64 62 2c 20 70 45 2c 20 30 29 3b 0a 20 20 20  (db, pE, 0);.   
1fe42 20 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e         if( !db->
1fe43 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
1fe44 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65              asse
1fe45 72 74 28 70 44 75 70 29 3b 0a 20 20 20 20 20 20  rt(pDup);.      
1fe46 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 73        iCol = res
1fe47 6f 6c 76 65 4f 72 64 65 72 42 79 54 65 72 6d 54  olveOrderByTermT
1fe48 6f 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 65  oExprList(pParse
1fe49 2c 20 70 53 65 6c 65 63 74 2c 20 70 44 75 70 29  , pSelect, pDup)
1fe4a 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
1fe4b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
1fe4c 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 44  xprDelete(db, pD
1fe4d 75 70 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  up);.        }. 
1fe4e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
1fe4f 20 69 43 6f 6c 3e 30 20 29 7b 0a 20 20 20 20 20   iCol>0 ){.     
1fe50 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
1fe51 6c 20 3d 20 70 45 2d 3e 70 43 6f 6c 6c 3b 0a 20  l = pE->pColl;. 
1fe52 20 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73         int flags
1fe53 20 3d 20 70 45 2d 3e 66 6c 61 67 73 20 26 20 45   = pE->flags & E
1fe54 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20  P_ExpCollate;.  
1fe55 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
1fe56 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45 29 3b  rDelete(db, pE);
1fe57 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e  .        pItem->
1fe58 70 45 78 70 72 20 3d 20 70 45 20 3d 20 73 71 6c  pExpr = pE = sql
1fe59 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f  ite3Expr(db, TK_
1fe5a 49 4e 54 45 47 45 52 2c 20 30 29 3b 0a 20 20 20  INTEGER, 0);.   
1fe5b 20 20 20 20 20 69 66 28 20 70 45 3d 3d 30 20 29       if( pE==0 )
1fe5c 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
1fe5d 20 20 20 70 45 2d 3e 70 43 6f 6c 6c 20 3d 20 70     pE->pColl = p
1fe5e 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 45  Coll;.        pE
1fe5f 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 49 6e  ->flags |= EP_In
1fe60 74 56 61 6c 75 65 20 7c 20 66 6c 61 67 73 3b 0a  tValue | flags;.
1fe61 20 20 20 20 20 20 20 20 70 45 2d 3e 75 2e 69 56          pE->u.iV
1fe62 61 6c 75 65 20 3d 20 69 43 6f 6c 3b 0a 20 20 20  alue = iCol;.   
1fe63 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c       pItem->iCol
1fe64 20 3d 20 28 75 31 36 29 69 43 6f 6c 3b 0a 20 20   = (u16)iCol;.  
1fe65 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 64 6f 6e        pItem->don
1fe66 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c  e = 1;.      }el
1fe67 73 65 7b 0a 20 20 20 20 20 20 20 20 6d 6f 72 65  se{.        more
1fe68 54 6f 44 6f 20 3d 20 31 3b 0a 20 20 20 20 20 20  ToDo = 1;.      
1fe69 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 65 6c  }.    }.    pSel
1fe6a 65 63 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70  ect = pSelect->p
1fe6b 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  Next;.  }.  for(
1fe6c 69 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d  i=0; i<pOrderBy-
1fe6d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20  >nExpr; i++){.  
1fe6e 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 2d 3e    if( pOrderBy->
1fe6f 61 5b 69 5d 2e 64 6f 6e 65 3d 3d 30 20 29 7b 0a  a[i].done==0 ){.
1fe70 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1fe71 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25  orMsg(pParse, "%
1fe72 72 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20  r ORDER BY term 
1fe73 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 61  does not match a
1fe74 6e 79 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  ny ".           
1fe75 20 22 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20   "column in the 
1fe76 72 65 73 75 6c 74 20 73 65 74 22 2c 20 69 2b 31  result set", i+1
1fe77 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
1fe78 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  1;.    }.  }.  r
1fe79 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
1fe7a 2a 20 43 68 65 63 6b 20 65 76 65 72 79 20 74 65  * Check every te
1fe7b 72 6d 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20  rm in the ORDER 
1fe7c 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63  BY or GROUP BY c
1fe7d 6c 61 75 73 65 20 70 4f 72 64 65 72 42 79 20 6f  lause pOrderBy o
1fe7e 66 0a 2a 2a 20 74 68 65 20 53 45 4c 45 43 54 20  f.** the SELECT 
1fe7f 73 74 61 74 65 6d 65 6e 74 20 70 53 65 6c 65 63  statement pSelec
1fe80 74 2e 20 20 49 66 20 61 6e 79 20 74 65 72 6d 20  t.  If any term 
1fe81 69 73 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20  is reference to 
1fe82 61 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65 74 20  a.** result set 
1fe83 65 78 70 72 65 73 73 69 6f 6e 20 28 61 73 20 64  expression (as d
1fe84 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65  etermined by the
1fe85 20 45 78 70 72 4c 69 73 74 2e 61 2e 69 43 6f 6c   ExprList.a.iCol
1fe86 20 66 69 65 6c 64 29 0a 2a 2a 20 74 68 65 6e 20   field).** then 
1fe87 63 6f 6e 76 65 72 74 20 74 68 61 74 20 74 65 72  convert that ter
1fe88 6d 20 69 6e 74 6f 20 61 20 63 6f 70 79 20 6f 66  m into a copy of
1fe89 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
1fe8a 6e 67 20 72 65 73 75 6c 74 20 73 65 74 0a 2a 2a  ng result set.**
1fe8b 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49   column..**.** I
1fe8c 66 20 61 6e 79 20 65 72 72 6f 72 73 20 61 72 65  f any errors are
1fe8d 20 64 65 74 65 63 74 65 64 2c 20 61 64 64 20 61   detected, add a
1fe8e 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
1fe8f 74 6f 20 70 50 61 72 73 65 20 61 6e 64 0a 2a 2a  to pParse and.**
1fe90 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f   return non-zero
1fe91 2e 20 20 52 65 74 75 72 6e 20 7a 65 72 6f 20 69  .  Return zero i
1fe92 66 20 6e 6f 20 65 72 72 6f 72 73 20 61 72 65 20  f no errors are 
1fe93 73 65 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  seen..*/.SQLITE_
1fe94 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1fe95 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47  te3ResolveOrderG
1fe96 72 6f 75 70 42 79 28 0a 20 20 50 61 72 73 65 20  roupBy(.  Parse 
1fe97 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
1fe98 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
1fe99 78 74 2e 20 20 4c 65 61 76 65 20 65 72 72 6f 72  xt.  Leave error
1fe9a 20 6d 65 73 73 61 67 65 73 20 68 65 72 65 20 2a   messages here *
1fe9b 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
1fe9c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  ect,      /* The
1fe9d 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1fe9e 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  t containing the
1fe9f 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70   clause */.  Exp
1fea0 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c  rList *pOrderBy,
1fea1 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20     /* The ORDER 
1fea2 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63  BY or GROUP BY c
1fea3 6c 61 75 73 65 20 74 6f 20 62 65 20 70 72 6f 63  lause to be proc
1fea4 65 73 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  essed */.  const
1fea5 20 63 68 61 72 20 2a 7a 54 79 70 65 20 20 20 20   char *zType    
1fea6 20 2f 2a 20 22 4f 52 44 45 52 22 20 6f 72 20 22   /* "ORDER" or "
1fea7 47 52 4f 55 50 22 20 2a 2f 0a 29 7b 0a 20 20 69  GROUP" */.){.  i
1fea8 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74 65 33 20  nt i;.  sqlite3 
1fea9 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
1feaa 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  ;.  ExprList *pE
1feab 4c 69 73 74 3b 0a 20 20 73 74 72 75 63 74 20 45  List;.  struct E
1feac 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
1fead 74 65 6d 3b 0a 0a 20 20 69 66 28 20 70 4f 72 64  tem;..  if( pOrd
1feae 65 72 42 79 3d 3d 30 20 7c 7c 20 70 50 61 72 73  erBy==0 || pPars
1feaf 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  e->db->mallocFai
1feb0 6c 65 64 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  led ) return 0;.
1feb1 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43  #if SQLITE_MAX_C
1feb2 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 70 4f 72 64  OLUMN.  if( pOrd
1feb3 65 72 42 79 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e  erBy->nExpr>db->
1feb4 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
1feb5 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20  MIT_COLUMN] ){. 
1feb6 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1feb7 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20  sg(pParse, "too 
1feb8 6d 61 6e 79 20 74 65 72 6d 73 20 69 6e 20 25 73  many terms in %s
1feb9 20 42 59 20 63 6c 61 75 73 65 22 2c 20 7a 54 79   BY clause", zTy
1feba 70 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  pe);.    return 
1febb 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  1;.  }.#endif.  
1febc 70 45 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74  pEList = pSelect
1febd 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 61 73 73 65  ->pEList;.  asse
1febe 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b  rt( pEList!=0 );
1febf 20 20 2f 2a 20 73 71 6c 69 74 65 33 53 65 6c 65    /* sqlite3Sele
1fec0 63 74 4e 65 77 28 29 20 67 75 61 72 61 6e 74 65  ctNew() guarante
1fec1 65 73 20 74 68 69 73 20 2a 2f 0a 20 20 66 6f 72  es this */.  for
1fec2 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64  (i=0, pItem=pOrd
1fec3 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65  erBy->a; i<pOrde
1fec4 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c  rBy->nExpr; i++,
1fec5 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 69   pItem++){.    i
1fec6 66 28 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 29  f( pItem->iCol )
1fec7 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65  {.      if( pIte
1fec8 6d 2d 3e 69 43 6f 6c 3e 70 45 4c 69 73 74 2d 3e  m->iCol>pEList->
1fec9 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 20  nExpr ){.       
1feca 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e   resolveOutOfRan
1fecb 67 65 45 72 72 6f 72 28 70 50 61 72 73 65 2c 20  geError(pParse, 
1fecc 7a 54 79 70 65 2c 20 69 2b 31 2c 20 70 45 4c 69  zType, i+1, pELi
1fecd 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20  st->nExpr);.    
1fece 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
1fecf 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 73 6f      }.      reso
1fed0 6c 76 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c  lveAlias(pParse,
1fed1 20 70 45 4c 69 73 74 2c 20 70 49 74 65 6d 2d 3e   pEList, pItem->
1fed2 69 43 6f 6c 2d 31 2c 20 70 49 74 65 6d 2d 3e 70  iCol-1, pItem->p
1fed3 45 78 70 72 2c 20 7a 54 79 70 65 29 3b 0a 20 20  Expr, zType);.  
1fed4 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
1fed5 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 4f 72   0;.}../*.** pOr
1fed6 64 65 72 42 79 20 69 73 20 61 6e 20 4f 52 44 45  derBy is an ORDE
1fed7 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59  R BY or GROUP BY
1fed8 20 63 6c 61 75 73 65 20 69 6e 20 53 45 4c 45 43   clause in SELEC
1fed9 54 20 73 74 61 74 65 6d 65 6e 74 20 70 53 65 6c  T statement pSel
1feda 65 63 74 2e 0a 2a 2a 20 54 68 65 20 4e 61 6d 65  ect..** The Name
1fedb 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20   context of the 
1fedc 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
1fedd 20 69 73 20 70 4e 43 2e 20 20 7a 54 79 70 65 20   is pNC.  zType 
1fede 69 73 20 65 69 74 68 65 72 0a 2a 2a 20 22 4f 52  is either.** "OR
1fedf 44 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22 20  DER" or "GROUP" 
1fee0 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 69  depending on whi
1fee1 63 68 20 74 79 70 65 20 6f 66 20 63 6c 61 75 73  ch type of claus
1fee2 65 20 70 4f 72 64 65 72 42 79 20 69 73 2e 0a 2a  e pOrderBy is..*
1fee3 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1fee4 65 20 72 65 73 6f 6c 76 65 73 20 65 61 63 68 20  e resolves each 
1fee5 74 65 72 6d 20 6f 66 20 74 68 65 20 63 6c 61 75  term of the clau
1fee6 73 65 20 69 6e 74 6f 20 61 6e 20 65 78 70 72 65  se into an expre
1fee7 73 73 69 6f 6e 2e 0a 2a 2a 20 49 66 20 74 68 65  ssion..** If the
1fee8 20 6f 72 64 65 72 2d 62 79 20 74 65 72 6d 20 69   order-by term i
1fee9 73 20 61 6e 20 69 6e 74 65 67 65 72 20 49 20 62  s an integer I b
1feea 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 28  etween 1 and N (
1feeb 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a  where N is the.*
1feec 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  * number of colu
1feed 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c  mns in the resul
1feee 74 20 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c  t set of the SEL
1feef 45 43 54 29 20 74 68 65 6e 20 74 68 65 20 65 78  ECT) then the ex
1fef0 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 6e 20 74  pression.** in t
1fef1 68 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 69 73  he resolution is
1fef2 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 49   a copy of the I
1fef3 2d 74 68 20 72 65 73 75 6c 74 2d 73 65 74 20 65  -th result-set e
1fef4 78 70 72 65 73 73 69 6f 6e 2e 20 20 49 66 0a 2a  xpression.  If.*
1fef5 2a 20 74 68 65 20 6f 72 64 65 72 2d 62 79 20 74  * the order-by t
1fef6 65 72 6d 20 69 73 20 61 6e 20 69 64 65 6e 74 69  erm is an identi
1fef7 66 79 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f  fy that correspo
1fef8 6e 64 73 20 74 6f 20 74 68 65 20 41 53 2d 6e 61  nds to the AS-na
1fef9 6d 65 20 6f 66 0a 2a 2a 20 61 20 72 65 73 75 6c  me of.** a resul
1fefa 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e  t-set expression
1fefb 2c 20 74 68 65 6e 20 74 68 65 20 74 65 72 6d 20  , then the term 
1fefc 72 65 73 6f 6c 76 65 73 20 74 6f 20 61 20 63 6f  resolves to a co
1fefd 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 73  py of the.** res
1fefe 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 69  ult-set expressi
1feff 6f 6e 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20  on.  Otherwise, 
1ff00 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
1ff01 73 20 72 65 73 6f 6c 76 65 64 20 69 6e 0a 2a 2a  s resolved in.**
1ff02 20 74 68 65 20 75 73 75 61 6c 20 77 61 79 20 2d   the usual way -
1ff03 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 52 65   using sqlite3Re
1ff04 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29  solveExprNames()
1ff05 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
1ff06 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65  tine returns the
1ff07 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72   number of error
1ff08 73 2e 20 20 49 66 20 65 72 72 6f 72 73 20 6f 63  s.  If errors oc
1ff09 63 75 72 2c 20 74 68 65 6e 0a 2a 2a 20 61 6e 20  cur, then.** an 
1ff0a 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f  appropriate erro
1ff0b 72 20 6d 65 73 73 61 67 65 20 6d 69 67 68 74 20  r message might 
1ff0c 62 65 20 6c 65 66 74 20 69 6e 20 70 50 61 72 73  be left in pPars
1ff0d 65 2e 20 20 28 4f 4f 4d 20 65 72 72 6f 72 73 0a  e.  (OOM errors.
1ff0e 2a 2a 20 65 78 63 65 70 74 65 64 2e 29 0a 2a 2f  ** excepted.).*/
1ff0f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f  .static int reso
1ff10 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28  lveOrderGroupBy(
1ff11 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a  .  NameContext *
1ff12 70 4e 43 2c 20 20 20 20 20 2f 2a 20 54 68 65 20  pNC,     /* The 
1ff13 6e 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f 66 20  name context of 
1ff14 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  the SELECT state
1ff15 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74  ment */.  Select
1ff16 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20   *pSelect,      
1ff17 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74  /* The SELECT st
1ff18 61 74 65 6d 65 6e 74 20 68 6f 6c 64 69 6e 67 20  atement holding 
1ff19 70 4f 72 64 65 72 42 79 20 2a 2f 0a 20 20 45 78  pOrderBy */.  Ex
1ff1a 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
1ff1b 2c 20 20 20 2f 2a 20 41 6e 20 4f 52 44 45 52 20  ,   /* An ORDER 
1ff1c 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63  BY or GROUP BY c
1ff1d 6c 61 75 73 65 20 74 6f 20 72 65 73 6f 6c 76 65  lause to resolve
1ff1e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
1ff1f 20 2a 7a 54 79 70 65 20 20 20 20 20 2f 2a 20 45   *zType     /* E
1ff20 69 74 68 65 72 20 22 4f 52 44 45 52 22 20 6f 72  ither "ORDER" or
1ff21 20 22 47 52 4f 55 50 22 2c 20 61 73 20 61 70 70   "GROUP", as app
1ff22 72 6f 70 72 69 61 74 65 20 2a 2f 0a 29 7b 0a 20  ropriate */.){. 
1ff23 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
1ff24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff25 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
1ff26 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20  */.  int iCol;  
1ff27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff28 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75      /* Column nu
1ff29 6d 62 65 72 20 2a 2f 0a 20 20 73 74 72 75 63 74  mber */.  struct
1ff2a 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
1ff2b 70 49 74 65 6d 3b 20 20 20 2f 2a 20 41 20 74 65  pItem;   /* A te
1ff2c 72 6d 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20  rm of the ORDER 
1ff2d 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 50  BY clause */.  P
1ff2e 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20  arse *pParse;   
1ff2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ff30 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
1ff31 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 73 75 6c   */.  int nResul
1ff32 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
1ff33 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1ff34 66 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 72  f terms in the r
1ff35 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 0a 20 20  esult set */..  
1ff36 69 66 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20  if( pOrderBy==0 
1ff37 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e 52  ) return 0;.  nR
1ff38 65 73 75 6c 74 20 3d 20 70 53 65 6c 65 63 74 2d  esult = pSelect-
1ff39 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a  >pEList->nExpr;.
1ff3a 20 20 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e    pParse = pNC->
1ff3b 70 50 61 72 73 65 3b 0a 20 20 66 6f 72 28 69 3d  pParse;.  for(i=
1ff3c 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42  0, pItem=pOrderB
1ff3d 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 72 42 79  y->a; i<pOrderBy
1ff3e 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49  ->nExpr; i++, pI
1ff3f 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72  tem++){.    Expr
1ff40 20 2a 70 45 20 3d 20 70 49 74 65 6d 2d 3e 70 45   *pE = pItem->pE
1ff41 78 70 72 3b 0a 20 20 20 20 69 43 6f 6c 20 3d 20  xpr;.    iCol = 
1ff42 72 65 73 6f 6c 76 65 41 73 4e 61 6d 65 28 70 50  resolveAsName(pP
1ff43 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2d 3e 70  arse, pSelect->p
1ff44 45 4c 69 73 74 2c 20 70 45 29 3b 0a 20 20 20 20  EList, pE);.    
1ff45 69 66 28 20 69 43 6f 6c 3e 30 20 29 7b 0a 20 20  if( iCol>0 ){.  
1ff46 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 41 53 2d      /* If an AS-
1ff47 6e 61 6d 65 20 6d 61 74 63 68 20 69 73 20 66 6f  name match is fo
1ff48 75 6e 64 2c 20 6d 61 72 6b 20 74 68 69 73 20 4f  und, mark this O
1ff49 52 44 45 52 20 42 59 20 63 6f 6c 75 6d 6e 20 61  RDER BY column a
1ff4a 73 20 62 65 69 6e 67 0a 20 20 20 20 20 20 2a 2a  s being.      **
1ff4b 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 69   a copy of the i
1ff4c 43 6f 6c 2d 74 68 20 72 65 73 75 6c 74 2d 73 65  Col-th result-se
1ff4d 74 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 73  t column.  The s
1ff4e 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74  ubsequent call t
1ff4f 6f 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74  o.      ** sqlit
1ff50 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72  e3ResolveOrderGr
1ff51 6f 75 70 42 79 28 29 20 77 69 6c 6c 20 63 6f 6e  oupBy() will con
1ff52 76 65 72 74 20 74 68 65 20 65 78 70 72 65 73 73  vert the express
1ff53 69 6f 6e 20 74 6f 20 61 0a 20 20 20 20 20 20 2a  ion to a.      *
1ff54 2a 20 63 6f 70 79 20 6f 66 20 74 68 65 20 69 43  * copy of the iC
1ff55 6f 6c 2d 74 68 20 72 65 73 75 6c 74 2d 73 65 74  ol-th result-set
1ff56 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 2a 2f 0a   expression. */.
1ff57 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f        pItem->iCo
1ff58 6c 20 3d 20 28 75 31 36 29 69 43 6f 6c 3b 0a 20  l = (u16)iCol;. 
1ff59 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
1ff5a 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
1ff5b 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65  ite3ExprIsIntege
1ff5c 72 28 70 45 2c 20 26 69 43 6f 6c 29 20 29 7b 0a  r(pE, &iCol) ){.
1ff5d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44        /* The ORD
1ff5e 45 52 20 42 59 20 74 65 72 6d 20 69 73 20 61 6e  ER BY term is an
1ff5f 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e   integer constan
1ff60 74 2e 20 20 41 67 61 69 6e 2c 20 73 65 74 20 74  t.  Again, set t
1ff61 68 65 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 20 20  he column.      
1ff62 2a 2a 20 6e 75 6d 62 65 72 20 73 6f 20 74 68 61  ** number so tha
1ff63 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  t sqlite3Resolve
1ff64 4f 72 64 65 72 47 72 6f 75 70 42 79 28 29 20 77  OrderGroupBy() w
1ff65 69 6c 6c 20 63 6f 6e 76 65 72 74 20 74 68 65 0a  ill convert the.
1ff66 20 20 20 20 20 20 2a 2a 20 6f 72 64 65 72 2d 62        ** order-b
1ff67 79 20 74 65 72 6d 20 74 6f 20 61 20 63 6f 70 79  y term to a copy
1ff68 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73   of the result-s
1ff69 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  et expression */
1ff6a 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c  .      if( iCol<
1ff6b 31 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 73  1 ){.        res
1ff6c 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72  olveOutOfRangeEr
1ff6d 72 6f 72 28 70 50 61 72 73 65 2c 20 7a 54 79 70  ror(pParse, zTyp
1ff6e 65 2c 20 69 2b 31 2c 20 6e 52 65 73 75 6c 74 29  e, i+1, nResult)
1ff6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
1ff70 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
1ff71 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20    pItem->iCol = 
1ff72 28 75 31 36 29 69 43 6f 6c 3b 0a 20 20 20 20 20  (u16)iCol;.     
1ff73 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d   continue;.    }
1ff74 0a 0a 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 69  ..    /* Otherwi
1ff75 73 65 2c 20 74 72 65 61 74 20 74 68 65 20 4f 52  se, treat the OR
1ff76 44 45 52 20 42 59 20 74 65 72 6d 20 61 73 20 61  DER BY term as a
1ff77 6e 20 6f 72 64 69 6e 61 72 79 20 65 78 70 72 65  n ordinary expre
1ff78 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 70 49 74  ssion */.    pIt
1ff79 65 6d 2d 3e 69 43 6f 6c 20 3d 20 30 3b 0a 20 20  em->iCol = 0;.  
1ff7a 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73    if( sqlite3Res
1ff7b 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 70 4e  olveExprNames(pN
1ff7c 43 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 20 20  C, pE) ){.      
1ff7d 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
1ff7e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c    }.  return sql
1ff7f 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 72  ite3ResolveOrder
1ff80 47 72 6f 75 70 42 79 28 70 50 61 72 73 65 2c 20  GroupBy(pParse, 
1ff81 70 53 65 6c 65 63 74 2c 20 70 4f 72 64 65 72 42  pSelect, pOrderB
1ff82 79 2c 20 7a 54 79 70 65 29 3b 0a 7d 0a 0a 2f 2a  y, zType);.}../*
1ff83 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 6e 61 6d 65  .** Resolve name
1ff84 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 20  s in the SELECT 
1ff85 73 74 61 74 65 6d 65 6e 74 20 70 20 61 6e 64 20  statement p and 
1ff86 61 6c 6c 20 6f 66 20 69 74 73 20 64 65 73 63 65  all of its desce
1ff87 6e 64 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69  ndents..*/.stati
1ff88 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 53 65 6c  c int resolveSel
1ff89 65 63 74 53 74 65 70 28 57 61 6c 6b 65 72 20 2a  ectStep(Walker *
1ff8a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20  pWalker, Select 
1ff8b 2a 70 29 7b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65  *p){.  NameConte
1ff8c 78 74 20 2a 70 4f 75 74 65 72 4e 43 3b 20 20 2f  xt *pOuterNC;  /
1ff8d 2a 20 43 6f 6e 74 65 78 74 20 74 68 61 74 20 63  * Context that c
1ff8e 6f 6e 74 61 69 6e 73 20 74 68 69 73 20 53 45 4c  ontains this SEL
1ff8f 45 43 54 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e  ECT */.  NameCon
1ff90 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20 20  text sNC;       
1ff91 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74   /* Name context
1ff92 20 6f 66 20 74 68 69 73 20 53 45 4c 45 43 54 20   of this SELECT 
1ff93 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6f 6d 70 6f  */.  int isCompo
1ff94 75 6e 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  und;         /* 
1ff95 54 72 75 65 20 69 66 20 70 20 69 73 20 61 20 63  True if p is a c
1ff96 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 2a  ompound select *
1ff97 2f 0a 20 20 69 6e 74 20 6e 43 6f 6d 70 6f 75 6e  /.  int nCompoun
1ff98 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  d;          /* N
1ff99 75 6d 62 65 72 20 6f 66 20 63 6f 6d 70 6f 75 6e  umber of compoun
1ff9a 64 20 74 65 72 6d 73 20 70 72 6f 63 65 73 73 65  d terms processe
1ff9b 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 50 61  d so far */.  Pa
1ff9c 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20  rse *pParse;    
1ff9d 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
1ff9e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78   context */.  Ex
1ff9f 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20  prList *pEList; 
1ffa0 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20        /* Result 
1ffa1 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20 6c  set expression l
1ffa2 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20  ist */.  int i; 
1ffa3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ffa4 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
1ffa5 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
1ffa6 70 47 72 6f 75 70 42 79 3b 20 20 20 20 20 2f 2a  pGroupBy;     /*
1ffa7 20 54 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c   The GROUP BY cl
1ffa8 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74  ause */.  Select
1ffa9 20 2a 70 4c 65 66 74 6d 6f 73 74 3b 20 20 20 20   *pLeftmost;    
1ffaa 20 20 2f 2a 20 4c 65 66 74 2d 6d 6f 73 74 20 6f    /* Left-most o
1ffab 66 20 53 45 4c 45 43 54 20 6f 66 20 61 20 63 6f  f SELECT of a co
1ffac 6d 70 6f 75 6e 64 20 2a 2f 0a 20 20 73 71 6c 69  mpound */.  sqli
1ffad 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
1ffae 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
1ffaf 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
1ffb0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30  ..  assert( p!=0
1ffb1 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c   );.  if( p->sel
1ffb2 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73 6f 6c  Flags & SF_Resol
1ffb3 76 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ved ){.    retur
1ffb4 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d  n WRC_Prune;.  }
1ffb5 0a 20 20 70 4f 75 74 65 72 4e 43 20 3d 20 70 57  .  pOuterNC = pW
1ffb6 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20  alker->u.pNC;.  
1ffb7 70 50 61 72 73 65 20 3d 20 70 57 61 6c 6b 65 72  pParse = pWalker
1ffb8 2d 3e 70 50 61 72 73 65 3b 0a 20 20 64 62 20 3d  ->pParse;.  db =
1ffb9 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20   pParse->db;..  
1ffba 2f 2a 20 4e 6f 72 6d 61 6c 6c 79 20 73 71 6c 69  /* Normally sqli
1ffbb 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28  te3SelectExpand(
1ffbc 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64  ) will be called
1ffbd 20 66 69 72 73 74 20 61 6e 64 20 77 69 6c 6c 20   first and will 
1ffbe 68 61 76 65 0a 20 20 2a 2a 20 61 6c 72 65 61 64  have.  ** alread
1ffbf 79 20 65 78 70 61 6e 64 65 64 20 74 68 69 73 20  y expanded this 
1ffc0 53 45 4c 45 43 54 2e 20 20 48 6f 77 65 76 65 72  SELECT.  However
1ffc1 2c 20 69 66 20 74 68 69 73 20 69 73 20 61 20 73  , if this is a s
1ffc2 75 62 71 75 65 72 79 20 77 69 74 68 69 6e 0a 20  ubquery within. 
1ffc3 20 2a 2a 20 61 6e 20 65 78 70 72 65 73 73 69 6f   ** an expressio
1ffc4 6e 2c 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  n, sqlite3Resolv
1ffc5 65 45 78 70 72 4e 61 6d 65 73 28 29 20 77 69 6c  eExprNames() wil
1ffc6 6c 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74 68  l be called with
1ffc7 6f 75 74 20 61 0a 20 20 2a 2a 20 70 72 69 6f 72  out a.  ** prior
1ffc8 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
1ffc9 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 2e 20  SelectExpand(). 
1ffca 20 57 68 65 6e 20 74 68 61 74 20 68 61 70 70 65   When that happe
1ffcb 6e 73 2c 20 6c 65 74 0a 20 20 2a 2a 20 73 71 6c  ns, let.  ** sql
1ffcc 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 29  ite3SelectPrep()
1ffcd 20 64 6f 20 61 6c 6c 20 6f 66 20 74 68 65 20 70   do all of the p
1ffce 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 74 68  rocessing for th
1ffcf 69 73 20 53 45 4c 45 43 54 2e 0a 20 20 2a 2a 20  is SELECT..  ** 
1ffd0 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65  sqlite3SelectPre
1ffd1 70 28 29 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20  p() will invoke 
1ffd2 62 6f 74 68 20 73 71 6c 69 74 65 33 53 65 6c 65  both sqlite3Sele
1ffd3 63 74 45 78 70 61 6e 64 28 29 20 61 6e 64 0a 20  ctExpand() and. 
1ffd4 20 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65   ** this routine
1ffd5 20 69 6e 20 74 68 65 20 63 6f 72 72 65 63 74 20   in the correct 
1ffd6 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66  order..  */.  if
1ffd7 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26  ( (p->selFlags &
1ffd8 20 53 46 5f 45 78 70 61 6e 64 65 64 29 3d 3d 30   SF_Expanded)==0
1ffd9 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
1ffda 65 6c 65 63 74 50 72 65 70 28 70 50 61 72 73 65  electPrep(pParse
1ffdb 2c 20 70 2c 20 70 4f 75 74 65 72 4e 43 29 3b 0a  , p, pOuterNC);.
1ffdc 20 20 20 20 72 65 74 75 72 6e 20 28 70 50 61 72      return (pPar
1ffdd 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e  se->nErr || db->
1ffde 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 20 3f 20  mallocFailed) ? 
1ffdf 57 52 43 5f 41 62 6f 72 74 20 3a 20 57 52 43 5f  WRC_Abort : WRC_
1ffe0 50 72 75 6e 65 3b 0a 20 20 7d 0a 0a 20 20 69 73  Prune;.  }..  is
1ffe1 43 6f 6d 70 6f 75 6e 64 20 3d 20 70 2d 3e 70 50  Compound = p->pP
1ffe2 72 69 6f 72 21 3d 30 3b 0a 20 20 6e 43 6f 6d 70  rior!=0;.  nComp
1ffe3 6f 75 6e 64 20 3d 20 30 3b 0a 20 20 70 4c 65 66  ound = 0;.  pLef
1ffe4 74 6d 6f 73 74 20 3d 20 70 3b 0a 20 20 77 68 69  tmost = p;.  whi
1ffe5 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 61 73 73  le( p ){.    ass
1ffe6 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67  ert( (p->selFlag
1ffe7 73 20 26 20 53 46 5f 45 78 70 61 6e 64 65 64 29  s & SF_Expanded)
1ffe8 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  !=0 );.    asser
1ffe9 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20  t( (p->selFlags 
1ffea 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 29 3d 3d  & SF_Resolved)==
1ffeb 30 20 29 3b 0a 20 20 20 20 70 2d 3e 73 65 6c 46  0 );.    p->selF
1ffec 6c 61 67 73 20 7c 3d 20 53 46 5f 52 65 73 6f 6c  lags |= SF_Resol
1ffed 76 65 64 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 73  ved;..    /* Res
1ffee 6f 6c 76 65 20 74 68 65 20 65 78 70 72 65 73 73  olve the express
1ffef 69 6f 6e 73 20 69 6e 20 74 68 65 20 4c 49 4d 49  ions in the LIMI
1fff0 54 20 61 6e 64 20 4f 46 46 53 45 54 20 63 6c 61  T and OFFSET cla
1fff1 75 73 65 73 2e 20 54 68 65 73 65 0a 20 20 20 20  uses. These.    
1fff2 2a 2a 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77  ** are not allow
1fff3 65 64 20 74 6f 20 72 65 66 65 72 20 74 6f 20 61  ed to refer to a
1fff4 6e 79 20 6e 61 6d 65 73 2c 20 73 6f 20 70 61 73  ny names, so pas
1fff5 73 20 61 6e 20 65 6d 70 74 79 20 4e 61 6d 65 43  s an empty NameC
1fff6 6f 6e 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20  ontext..    */. 
1fff7 20 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20     memset(&sNC, 
1fff8 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b  0, sizeof(sNC));
1fff9 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20  .    sNC.pParse 
1fffa 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 20 69 66  = pParse;.    if
1fffb 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  ( sqlite3Resolve
1fffc 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20  ExprNames(&sNC, 
1fffd 70 2d 3e 70 4c 69 6d 69 74 29 20 7c 7c 0a 20 20  p->pLimit) ||.  
1fffe 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73        sqlite3Res
1ffff 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73  olveExprNames(&s
20000 4e 43 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29 20  NC, p->pOffset) 
20001 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
20002 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d  WRC_Abort;.    }
20003 0a 20 20 0a 20 20 20 20 2f 2a 20 53 65 74 20 75  .  .    /* Set u
20004 70 20 74 68 65 20 6c 6f 63 61 6c 20 6e 61 6d 65  p the local name
20005 2d 63 6f 6e 74 65 78 74 20 74 6f 20 70 61 73 73  -context to pass
20006 20 74 6f 20 73 71 6c 69 74 65 33 52 65 73 6f 6c   to sqlite3Resol
20007 76 65 45 78 70 72 4e 61 6d 65 73 28 29 20 74 6f  veExprNames() to
20008 0a 20 20 20 20 2a 2a 20 72 65 73 6f 6c 76 65 20  .    ** resolve 
20009 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 20 65  the result-set e
2000a 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a  xpression list..
2000b 20 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 61      */.    sNC.a
2000c 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 20 20 20  llowAgg = 1;.   
2000d 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20   sNC.pSrcList = 
2000e 70 2d 3e 70 53 72 63 3b 0a 20 20 20 20 73 4e 43  p->pSrc;.    sNC
2000f 2e 70 4e 65 78 74 20 3d 20 70 4f 75 74 65 72 4e  .pNext = pOuterN
20010 43 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 73  C;.  .    /* Res
20011 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 74 68  olve names in th
20012 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 2a 2f  e result set. */
20013 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70 2d  .    pEList = p-
20014 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 61 73 73  >pEList;.    ass
20015 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29  ert( pEList!=0 )
20016 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
20017 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  <pEList->nExpr; 
20018 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72  i++){.      Expr
20019 20 2a 70 58 20 3d 20 70 45 4c 69 73 74 2d 3e 61   *pX = pEList->a
2001a 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  [i].pExpr;.     
2001b 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f   if( sqlite3Reso
2001c 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e  lveExprNames(&sN
2001d 43 2c 20 70 58 29 20 29 7b 0a 20 20 20 20 20 20  C, pX) ){.      
2001e 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f    return WRC_Abo
2001f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rt;.      }.    
20020 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 63 75  }.  .    /* Recu
20021 72 73 69 76 65 6c 79 20 72 65 73 6f 6c 76 65 20  rsively resolve 
20022 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 73 75 62  names in all sub
20023 71 75 65 72 69 65 73 0a 20 20 20 20 2a 2f 0a 20  queries.    */. 
20024 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d     for(i=0; i<p-
20025 3e 70 53 72 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b  >pSrc->nSrc; i++
20026 29 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20  ){.      struct 
20027 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  SrcList_item *pI
20028 74 65 6d 20 3d 20 26 70 2d 3e 70 53 72 63 2d 3e  tem = &p->pSrc->
20029 61 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20  a[i];.      if( 
2002a 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 29  pItem->pSelect )
2002b 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20  {.        const 
2002c 63 68 61 72 20 2a 7a 53 61 76 65 64 43 6f 6e 74  char *zSavedCont
2002d 65 78 74 20 3d 20 70 50 61 72 73 65 2d 3e 7a 41  ext = pParse->zA
2002e 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20  uthContext;.    
2002f 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a      if( pItem->z
20030 4e 61 6d 65 20 29 20 70 50 61 72 73 65 2d 3e 7a  Name ) pParse->z
20031 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 49  AuthContext = pI
20032 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20  tem->zName;.    
20033 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c      sqlite3Resol
20034 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 70 50  veSelectNames(pP
20035 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 53 65  arse, pItem->pSe
20036 6c 65 63 74 2c 20 70 4f 75 74 65 72 4e 43 29 3b  lect, pOuterNC);
20037 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d  .        pParse-
20038 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20  >zAuthContext = 
20039 7a 53 61 76 65 64 43 6f 6e 74 65 78 74 3b 0a 20  zSavedContext;. 
2003a 20 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73         if( pPars
2003b 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d  e->nErr || db->m
2003c 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65  allocFailed ) re
2003d 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
2003e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2003f 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65  .    /* If there
20040 20 61 72 65 20 6e 6f 20 61 67 67 72 65 67 61 74   are no aggregat
20041 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74  e functions in t
20042 68 65 20 72 65 73 75 6c 74 2d 73 65 74 2c 20 61  he result-set, a
20043 6e 64 20 6e 6f 20 47 52 4f 55 50 20 42 59 20 0a  nd no GROUP BY .
20044 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f      ** expressio
20045 6e 2c 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20  n, do not allow 
20046 61 67 67 72 65 67 61 74 65 73 20 69 6e 20 61 6e  aggregates in an
20047 79 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 65  y of the other e
20048 78 70 72 65 73 73 69 6f 6e 73 2e 0a 20 20 20 20  xpressions..    
20049 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  */.    assert( (
2004a 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  p->selFlags & SF
2004b 5f 41 67 67 72 65 67 61 74 65 29 3d 3d 30 20 29  _Aggregate)==0 )
2004c 3b 0a 20 20 20 20 70 47 72 6f 75 70 42 79 20 3d  ;.    pGroupBy =
2004d 20 70 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20   p->pGroupBy;.  
2004e 20 20 69 66 28 20 70 47 72 6f 75 70 42 79 20 7c    if( pGroupBy |
2004f 7c 20 73 4e 43 2e 68 61 73 41 67 67 20 29 7b 0a  | sNC.hasAgg ){.
20050 20 20 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67        p->selFlag
20051 73 20 7c 3d 20 53 46 5f 41 67 67 72 65 67 61 74  s |= SF_Aggregat
20052 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  e;.    }else{.  
20053 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67      sNC.allowAgg
20054 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20   = 0;.    }.  . 
20055 20 20 20 2f 2a 20 49 66 20 61 20 48 41 56 49 4e     /* If a HAVIN
20056 47 20 63 6c 61 75 73 65 20 69 73 20 70 72 65 73  G clause is pres
20057 65 6e 74 2c 20 74 68 65 6e 20 74 68 65 72 65 20  ent, then there 
20058 6d 75 73 74 20 62 65 20 61 20 47 52 4f 55 50 20  must be a GROUP 
20059 42 59 20 63 6c 61 75 73 65 2e 0a 20 20 20 20 2a  BY clause..    *
2005a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 48 61  /.    if( p->pHa
2005b 76 69 6e 67 20 26 26 20 21 70 47 72 6f 75 70 42  ving && !pGroupB
2005c 79 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  y ){.      sqlit
2005d 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2005e 65 2c 20 22 61 20 47 52 4f 55 50 20 42 59 20 63  e, "a GROUP BY c
2005f 6c 61 75 73 65 20 69 73 20 72 65 71 75 69 72 65  lause is require
20060 64 20 62 65 66 6f 72 65 20 48 41 56 49 4e 47 22  d before HAVING"
20061 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
20062 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d  WRC_Abort;.    }
20063 0a 20 20 0a 20 20 20 20 2f 2a 20 41 64 64 20 74  .  .    /* Add t
20064 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  he expression li
20065 73 74 20 74 6f 20 74 68 65 20 6e 61 6d 65 2d 63  st to the name-c
20066 6f 6e 74 65 78 74 20 62 65 66 6f 72 65 20 70 61  ontext before pa
20067 72 73 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a  rsing the.    **
20068 20 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f   other expressio
20069 6e 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54  ns in the SELECT
2006a 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 69 73   statement. This
2006b 20 69 73 20 73 6f 20 74 68 61 74 0a 20 20 20 20   is so that.    
2006c 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69  ** expressions i
2006d 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  n the WHERE clau
2006e 73 65 20 28 65 74 63 2e 29 20 63 61 6e 20 72 65  se (etc.) can re
2006f 66 65 72 20 74 6f 20 65 78 70 72 65 73 73 69 6f  fer to expressio
20070 6e 73 20 62 79 0a 20 20 20 20 2a 2a 20 61 6c 69  ns by.    ** ali
20071 61 73 65 73 20 69 6e 20 74 68 65 20 72 65 73 75  ases in the resu
20072 6c 74 20 73 65 74 2e 0a 20 20 20 20 2a 2a 0a 20  lt set..    **. 
20073 20 20 20 2a 2a 20 4d 69 6e 6f 72 20 70 6f 69 6e     ** Minor poin
20074 74 3a 20 49 66 20 74 68 69 73 20 69 73 20 74 68  t: If this is th
20075 65 20 63 61 73 65 2c 20 74 68 65 6e 20 74 68 65  e case, then the
20076 20 65 78 70 72 65 73 73 69 6f 6e 20 77 69 6c 6c   expression will
20077 20 62 65 0a 20 20 20 20 2a 2a 20 72 65 2d 65 76   be.    ** re-ev
20078 61 6c 75 61 74 65 64 20 66 6f 72 20 65 61 63 68  aluated for each
20079 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 69 74   reference to it
2007a 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43  ..    */.    sNC
2007b 2e 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c  .pEList = p->pEL
2007c 69 73 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  ist;.    if( sql
2007d 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
2007e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 57  ames(&sNC, p->pW
2007f 68 65 72 65 29 20 7c 7c 0a 20 20 20 20 20 20 20  here) ||.       
20080 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
20081 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d  prNames(&sNC, p-
20082 3e 70 48 61 76 69 6e 67 29 0a 20 20 20 20 29 7b  >pHaving).    ){
20083 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52  .      return WR
20084 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 0a  C_Abort;.    }..
20085 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52      /* The ORDER
20086 20 42 59 20 61 6e 64 20 47 52 4f 55 50 20 42 59   BY and GROUP BY
20087 20 63 6c 61 75 73 65 73 20 6d 61 79 20 6e 6f 74   clauses may not
20088 20 72 65 66 65 72 20 74 6f 20 74 65 72 6d 73 20   refer to terms 
20089 69 6e 0a 20 20 20 20 2a 2a 20 6f 75 74 65 72 20  in.    ** outer 
2008a 71 75 65 72 69 65 73 20 0a 20 20 20 20 2a 2f 0a  queries .    */.
2008b 20 20 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d 20      sNC.pNext = 
2008c 30 3b 0a 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77  0;.    sNC.allow
2008d 41 67 67 20 3d 20 31 3b 0a 0a 20 20 20 20 2f 2a  Agg = 1;..    /*
2008e 20 50 72 6f 63 65 73 73 20 74 68 65 20 4f 52 44   Process the ORD
2008f 45 52 20 42 59 20 63 6c 61 75 73 65 20 66 6f 72  ER BY clause for
20090 20 73 69 6e 67 6c 65 74 6f 6e 20 53 45 4c 45 43   singleton SELEC
20091 54 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20  T statements..  
20092 20 20 2a 2a 20 54 68 65 20 4f 52 44 45 52 20 42    ** The ORDER B
20093 59 20 63 6c 61 75 73 65 20 66 6f 72 20 63 6f 6d  Y clause for com
20094 70 6f 75 6e 64 73 20 53 45 4c 45 43 54 20 73 74  pounds SELECT st
20095 61 74 65 6d 65 6e 74 73 20 69 73 20 68 61 6e 64  atements is hand
20096 6c 65 64 0a 20 20 20 20 2a 2a 20 62 65 6c 6f 77  led.    ** below
20097 2c 20 61 66 74 65 72 20 61 6c 6c 20 6f 66 20 74  , after all of t
20098 68 65 20 72 65 73 75 6c 74 2d 73 65 74 73 20 66  he result-sets f
20099 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 65 6c  or all of the el
2009a 65 6d 65 6e 74 73 20 6f 66 0a 20 20 20 20 2a 2a  ements of.    **
2009b 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 68 61   the compound ha
2009c 76 65 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64  ve been resolved
2009d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
2009e 20 21 69 73 43 6f 6d 70 6f 75 6e 64 20 26 26 20   !isCompound && 
2009f 72 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75  resolveOrderGrou
200a0 70 42 79 28 26 73 4e 43 2c 20 70 2c 20 70 2d 3e  pBy(&sNC, p, p->
200a1 70 4f 72 64 65 72 42 79 2c 20 22 4f 52 44 45 52  pOrderBy, "ORDER
200a2 22 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  ") ){.      retu
200a3 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
200a4 20 20 7d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e    }.    if( db->
200a5 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
200a6 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
200a7 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20  _Abort;.    }.  
200a8 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20  .    /* Resolve 
200a9 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61  the GROUP BY cla
200aa 75 73 65 2e 20 20 41 74 20 74 68 65 20 73 61 6d  use.  At the sam
200ab 65 20 74 69 6d 65 2c 20 6d 61 6b 65 20 73 75 72  e time, make sur
200ac 65 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 47 52  e .    ** the GR
200ad 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 64 6f  OUP BY clause do
200ae 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61  es not contain a
200af 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
200b0 6e 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ns..    */.    i
200b1 66 28 20 70 47 72 6f 75 70 42 79 20 29 7b 0a 20  f( pGroupBy ){. 
200b2 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72       struct Expr
200b3 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
200b4 3b 0a 20 20 20 20 0a 20 20 20 20 20 20 69 66 28  ;.    .      if(
200b5 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f   resolveOrderGro
200b6 75 70 42 79 28 26 73 4e 43 2c 20 70 2c 20 70 47  upBy(&sNC, p, pG
200b7 72 6f 75 70 42 79 2c 20 22 47 52 4f 55 50 22 29  roupBy, "GROUP")
200b8 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
200b9 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20  iled ){.        
200ba 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
200bb 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
200bc 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70  for(i=0, pItem=p
200bd 47 72 6f 75 70 42 79 2d 3e 61 3b 20 69 3c 70 47  GroupBy->a; i<pG
200be 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 3b 20 69  roupBy->nExpr; i
200bf 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  ++, pItem++){.  
200c0 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61        if( ExprHa
200c1 73 50 72 6f 70 65 72 74 79 28 70 49 74 65 6d 2d  sProperty(pItem-
200c2 3e 70 45 78 70 72 2c 20 45 50 5f 41 67 67 29 20  >pExpr, EP_Agg) 
200c3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
200c4 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
200c5 72 73 65 2c 20 22 61 67 67 72 65 67 61 74 65 20  rse, "aggregate 
200c6 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f  functions are no
200c7 74 20 61 6c 6c 6f 77 65 64 20 69 6e 20 22 0a 20  t allowed in ". 
200c8 20 20 20 20 20 20 20 20 20 20 20 20 20 22 74 68               "th
200c9 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  e GROUP BY claus
200ca 65 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72  e");.          r
200cb 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
200cc 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
200cd 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a   }.    }..    /*
200ce 20 41 64 76 61 6e 63 65 20 74 6f 20 74 68 65 20   Advance to the 
200cf 6e 65 78 74 20 74 65 72 6d 20 6f 66 20 74 68 65  next term of the
200d0 20 63 6f 6d 70 6f 75 6e 64 0a 20 20 20 20 2a 2f   compound.    */
200d1 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70 50 72 69  .    p = p->pPri
200d2 6f 72 3b 0a 20 20 20 20 6e 43 6f 6d 70 6f 75 6e  or;.    nCompoun
200d3 64 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52  d++;.  }..  /* R
200d4 65 73 6f 6c 76 65 20 74 68 65 20 4f 52 44 45 52  esolve the ORDER
200d5 20 42 59 20 6f 6e 20 61 20 63 6f 6d 70 6f 75 6e   BY on a compoun
200d6 64 20 53 45 4c 45 43 54 20 61 66 74 65 72 20 61  d SELECT after a
200d7 6c 6c 20 74 65 72 6d 73 20 6f 66 0a 20 20 2a 2a  ll terms of.  **
200d8 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 68 61   the compound ha
200d9 76 65 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64  ve been resolved
200da 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 43  ..  */.  if( isC
200db 6f 6d 70 6f 75 6e 64 20 26 26 20 72 65 73 6f 6c  ompound && resol
200dc 76 65 43 6f 6d 70 6f 75 6e 64 4f 72 64 65 72 42  veCompoundOrderB
200dd 79 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 6d  y(pParse, pLeftm
200de 6f 73 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75  ost) ){.    retu
200df 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
200e0 7d 0a 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f  }..  return WRC_
200e1 50 72 75 6e 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Prune;.}../*.** 
200e2 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 6c  This routine wal
200e3 6b 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  ks an expression
200e4 20 74 72 65 65 20 61 6e 64 20 72 65 73 6f 6c 76   tree and resolv
200e5 65 73 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f  es references to
200e6 0a 2a 2a 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e  .** table column
200e7 73 20 61 6e 64 20 72 65 73 75 6c 74 2d 73 65 74  s and result-set
200e8 20 63 6f 6c 75 6d 6e 73 2e 20 20 41 74 20 74 68   columns.  At th
200e9 65 20 73 61 6d 65 20 74 69 6d 65 2c 20 64 6f 20  e same time, do 
200ea 65 72 72 6f 72 0a 2a 2a 20 63 68 65 63 6b 69 6e  error.** checkin
200eb 67 20 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 75 73  g on function us
200ec 61 67 65 20 61 6e 64 20 73 65 74 20 61 20 66 6c  age and set a fl
200ed 61 67 20 69 66 20 61 6e 79 20 61 67 67 72 65 67  ag if any aggreg
200ee 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a  ate functions.**
200ef 20 61 72 65 20 73 65 65 6e 2e 0a 2a 2a 0a 2a 2a   are seen..**.**
200f0 20 54 6f 20 72 65 73 6f 6c 76 65 20 74 61 62 6c   To resolve tabl
200f1 65 20 63 6f 6c 75 6d 6e 73 20 72 65 66 65 72 65  e columns refere
200f2 6e 63 65 73 20 77 65 20 6c 6f 6f 6b 20 66 6f 72  nces we look for
200f3 20 6e 6f 64 65 73 20 28 6f 72 20 73 75 62 74 72   nodes (or subtr
200f4 65 65 73 29 20 6f 66 20 74 68 65 20 0a 2a 2a 20  ees) of the .** 
200f5 66 6f 72 6d 20 58 2e 59 2e 5a 20 6f 72 20 59 2e  form X.Y.Z or Y.
200f6 5a 20 6f 72 20 6a 75 73 74 20 5a 20 77 68 65 72  Z or just Z wher
200f7 65 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 58 3a 20  e.**.**      X: 
200f8 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20    The name of a 
200f9 64 61 74 61 62 61 73 65 2e 20 20 45 78 3a 20 20  database.  Ex:  
200fa 22 6d 61 69 6e 22 20 6f 72 20 22 74 65 6d 70 22  "main" or "temp"
200fb 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   or.**          
200fc 20 74 68 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61   the symbolic na
200fd 6d 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20 61  me assigned to a
200fe 6e 20 41 54 54 41 43 48 2d 65 64 20 64 61 74 61  n ATTACH-ed data
200ff 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  base..**.**     
20100 20 59 3a 20 20 20 54 68 65 20 6e 61 6d 65 20 6f   Y:   The name o
20101 66 20 61 20 74 61 62 6c 65 20 69 6e 20 61 20 46  f a table in a F
20102 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 4f 72 20  ROM clause.  Or 
20103 69 6e 20 61 20 74 72 69 67 67 65 72 0a 2a 2a 20  in a trigger.** 
20104 20 20 20 20 20 20 20 20 20 20 6f 6e 65 20 6f 66            one of
20105 20 74 68 65 20 73 70 65 63 69 61 6c 20 6e 61 6d   the special nam
20106 65 73 20 22 6f 6c 64 22 20 6f 72 20 22 6e 65 77  es "old" or "new
20107 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 5a 3a  "..**.**      Z:
20108 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61     The name of a
20109 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 61 62 6c 65   column in table
2010a 20 59 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 6f   Y..**.** The no
2010b 64 65 20 61 74 20 74 68 65 20 72 6f 6f 74 20 6f  de at the root o
2010c 66 20 74 68 65 20 73 75 62 74 72 65 65 20 69 73  f the subtree is
2010d 20 6d 6f 64 69 66 69 65 64 20 61 73 20 66 6f 6c   modified as fol
2010e 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 45  lows:.**.**    E
2010f 78 70 72 2e 6f 70 20 20 20 20 20 20 20 20 43 68  xpr.op        Ch
20110 61 6e 67 65 64 20 74 6f 20 54 4b 5f 43 4f 4c 55  anged to TK_COLU
20111 4d 4e 0a 2a 2a 20 20 20 20 45 78 70 72 2e 70 54  MN.**    Expr.pT
20112 61 62 20 20 20 20 20 20 50 6f 69 6e 74 73 20 74  ab      Points t
20113 6f 20 74 68 65 20 54 61 62 6c 65 20 6f 62 6a 65  o the Table obje
20114 63 74 20 66 6f 72 20 58 2e 59 0a 2a 2a 20 20 20  ct for X.Y.**   
20115 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 20 20   Expr.iColumn   
20116 54 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78  The column index
20117 20 69 6e 20 58 2e 59 2e 20 20 2d 31 20 66 6f 72   in X.Y.  -1 for
20118 20 74 68 65 20 72 6f 77 69 64 2e 0a 2a 2a 20 20   the rowid..**  
20119 20 20 45 78 70 72 2e 69 54 61 62 6c 65 20 20 20    Expr.iTable   
2011a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72   The VDBE cursor
2011b 20 6e 75 6d 62 65 72 20 66 6f 72 20 58 2e 59 0a   number for X.Y.
2011c 2a 2a 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f  **.**.** To reso
2011d 6c 76 65 20 72 65 73 75 6c 74 2d 73 65 74 20 72  lve result-set r
2011e 65 66 65 72 65 6e 63 65 73 2c 20 6c 6f 6f 6b 20  eferences, look 
2011f 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 20 6e  for expression n
20120 6f 64 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 66  odes of the.** f
20121 6f 72 6d 20 5a 20 28 77 69 74 68 20 6e 6f 20 58  orm Z (with no X
20122 20 61 6e 64 20 59 20 70 72 65 66 69 78 29 20 77   and Y prefix) w
20123 68 65 72 65 20 74 68 65 20 5a 20 6d 61 74 63 68  here the Z match
20124 65 73 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e  es the right-han
20125 64 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 61 6e 20  d.** size of an 
20126 41 53 20 63 6c 61 75 73 65 20 69 6e 20 74 68 65  AS clause in the
20127 20 72 65 73 75 6c 74 2d 73 65 74 20 6f 66 20 61   result-set of a
20128 20 53 45 4c 45 43 54 2e 20 20 54 68 65 20 5a 20   SELECT.  The Z 
20129 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73  expression.** is
2012a 20 72 65 70 6c 61 63 65 64 20 62 79 20 61 20 63   replaced by a c
2012b 6f 70 79 20 6f 66 20 74 68 65 20 6c 65 66 74 2d  opy of the left-
2012c 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65  hand side of the
2012d 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72   result-set expr
2012e 65 73 73 69 6f 6e 2e 0a 2a 2a 20 54 61 62 6c 65  ession..** Table
2012f 2d 6e 61 6d 65 20 61 6e 64 20 66 75 6e 63 74 69  -name and functi
20130 6f 6e 20 72 65 73 6f 6c 75 74 69 6f 6e 20 6f 63  on resolution oc
20131 63 75 72 73 20 6f 6e 20 74 68 65 20 73 75 62 73  curs on the subs
20132 74 69 74 75 74 65 64 20 65 78 70 72 65 73 73 69  tituted expressi
20133 6f 6e 0a 2a 2a 20 74 72 65 65 2e 20 20 46 6f 72  on.** tree.  For
20134 20 65 78 61 6d 70 6c 65 2c 20 69 6e 3a 0a 2a 2a   example, in:.**
20135 0a 2a 2a 20 20 20 20 20 20 53 45 4c 45 43 54 20  .**      SELECT 
20136 61 2b 62 20 41 53 20 78 2c 20 63 2b 64 20 41 53  a+b AS x, c+d AS
20137 20 79 20 46 52 4f 4d 20 74 31 20 4f 52 44 45 52   y FROM t1 ORDER
20138 20 42 59 20 78 3b 0a 2a 2a 0a 2a 2a 20 54 68 65   BY x;.**.** The
20139 20 22 78 22 20 74 65 72 6d 20 6f 66 20 74 68 65   "x" term of the
2013a 20 6f 72 64 65 72 20 62 79 20 69 73 20 72 65 70   order by is rep
2013b 6c 61 63 65 64 20 62 79 20 22 61 2b 62 22 20 74  laced by "a+b" t
2013c 6f 20 72 65 6e 64 65 72 3a 0a 2a 2a 0a 2a 2a 20  o render:.**.** 
2013d 20 20 20 20 20 53 45 4c 45 43 54 20 61 2b 62 20       SELECT a+b 
2013e 41 53 20 78 2c 20 63 2b 64 20 41 53 20 79 20 46  AS x, c+d AS y F
2013f 52 4f 4d 20 74 31 20 4f 52 44 45 52 20 42 59 20  ROM t1 ORDER BY 
20140 61 2b 62 3b 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74  a+b;.**.** Funct
20141 69 6f 6e 20 63 61 6c 6c 73 20 61 72 65 20 63 68  ion calls are ch
20142 65 63 6b 65 64 20 74 6f 20 6d 61 6b 65 20 73 75  ecked to make su
20143 72 65 20 74 68 61 74 20 74 68 65 20 66 75 6e 63  re that the func
20144 74 69 6f 6e 20 69 73 20 0a 2a 2a 20 64 65 66 69  tion is .** defi
20145 6e 65 64 20 61 6e 64 20 74 68 61 74 20 74 68 65  ned and that the
20146 20 63 6f 72 72 65 63 74 20 6e 75 6d 62 65 72 20   correct number 
20147 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65  of arguments are
20148 20 73 70 65 63 69 66 69 65 64 2e 0a 2a 2a 20 49   specified..** I
20149 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69  f the function i
2014a 73 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 66  s an aggregate f
2014b 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68  unction, then th
2014c 65 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 69 73  e pNC->hasAgg is
2014d 0a 2a 2a 20 73 65 74 20 61 6e 64 20 74 68 65 20  .** set and the 
2014e 6f 70 63 6f 64 65 20 69 73 20 63 68 61 6e 67 65  opcode is change
2014f 64 20 66 72 6f 6d 20 54 4b 5f 46 55 4e 43 54 49  d from TK_FUNCTI
20150 4f 4e 20 74 6f 20 54 4b 5f 41 47 47 5f 46 55 4e  ON to TK_AGG_FUN
20151 43 54 49 4f 4e 2e 0a 2a 2a 20 49 66 20 61 6e 20  CTION..** If an 
20152 65 78 70 72 65 73 73 69 6f 6e 20 63 6f 6e 74 61  expression conta
20153 69 6e 73 20 61 67 67 72 65 67 61 74 65 20 66 75  ins aggregate fu
20154 6e 63 74 69 6f 6e 73 20 74 68 65 6e 20 74 68 65  nctions then the
20155 20 45 50 5f 41 67 67 0a 2a 2a 20 70 72 6f 70 65   EP_Agg.** prope
20156 72 74 79 20 6f 6e 20 74 68 65 20 65 78 70 72 65  rty on the expre
20157 73 73 69 6f 6e 20 69 73 20 73 65 74 2e 0a 2a 2a  ssion is set..**
20158 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65 73  .** An error mes
20159 73 61 67 65 20 69 73 20 6c 65 66 74 20 69 6e 20  sage is left in 
2015a 70 50 61 72 73 65 20 69 66 20 61 6e 79 74 68 69  pParse if anythi
2015b 6e 67 20 69 73 20 61 6d 69 73 73 2e 20 20 54 68  ng is amiss.  Th
2015c 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 69 66 20 65  e number.** if e
2015d 72 72 6f 72 73 20 69 73 20 72 65 74 75 72 6e 65  rrors is returne
2015e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
2015f 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
20160 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
20161 28 20 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  ( .  NameContext
20162 20 2a 70 4e 43 2c 20 20 20 20 20 20 20 2f 2a 20   *pNC,       /* 
20163 4e 61 6d 65 73 70 61 63 65 20 74 6f 20 72 65 73  Namespace to res
20164 6f 6c 76 65 20 65 78 70 72 65 73 73 69 6f 6e 73  olve expressions
20165 20 69 6e 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a   in. */.  Expr *
20166 70 45 78 70 72 20 20 20 20 20 20 20 20 20 20 20  pExpr           
20167 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73    /* The express
20168 69 6f 6e 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a  ion to be analyz
20169 65 64 2e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ed. */.){.  int 
2016a 73 61 76 65 64 48 61 73 41 67 67 3b 0a 20 20 57  savedHasAgg;.  W
2016b 61 6c 6b 65 72 20 77 3b 0a 0a 20 20 69 66 28 20  alker w;..  if( 
2016c 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72  pExpr==0 ) retur
2016d 6e 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f  n 0;.#if SQLITE_
2016e 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30  MAX_EXPR_DEPTH>0
2016f 0a 20 20 7b 0a 20 20 20 20 50 61 72 73 65 20 2a  .  {.    Parse *
20170 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50  pParse = pNC->pP
20171 61 72 73 65 3b 0a 20 20 20 20 69 66 28 20 73 71  arse;.    if( sq
20172 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b 48 65  lite3ExprCheckHe
20173 69 67 68 74 28 70 50 61 72 73 65 2c 20 70 45 78  ight(pParse, pEx
20174 70 72 2d 3e 6e 48 65 69 67 68 74 2b 70 4e 43 2d  pr->nHeight+pNC-
20175 3e 70 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74  >pParse->nHeight
20176 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
20177 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  n 1;.    }.    p
20178 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2b  Parse->nHeight +
20179 3d 20 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74  = pExpr->nHeight
2017a 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 73  ;.  }.#endif.  s
2017b 61 76 65 64 48 61 73 41 67 67 20 3d 20 70 4e 43  avedHasAgg = pNC
2017c 2d 3e 68 61 73 41 67 67 3b 0a 20 20 70 4e 43 2d  ->hasAgg;.  pNC-
2017d 3e 68 61 73 41 67 67 20 3d 20 30 3b 0a 20 20 77  >hasAgg = 0;.  w
2017e 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d  .xExprCallback =
2017f 20 72 65 73 6f 6c 76 65 45 78 70 72 53 74 65 70   resolveExprStep
20180 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c  ;.  w.xSelectCal
20181 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 53  lback = resolveS
20182 65 6c 65 63 74 53 74 65 70 3b 0a 20 20 77 2e 70  electStep;.  w.p
20183 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61  Parse = pNC->pPa
20184 72 73 65 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d  rse;.  w.u.pNC =
20185 20 70 4e 43 3b 0a 20 20 73 71 6c 69 74 65 33 57   pNC;.  sqlite3W
20186 61 6c 6b 45 78 70 72 28 26 77 2c 20 70 45 78 70  alkExpr(&w, pExp
20187 72 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d  r);.#if SQLITE_M
20188 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a  AX_EXPR_DEPTH>0.
20189 20 20 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 6e    pNC->pParse->n
2018a 48 65 69 67 68 74 20 2d 3d 20 70 45 78 70 72 2d  Height -= pExpr-
2018b 3e 6e 48 65 69 67 68 74 3b 0a 23 65 6e 64 69 66  >nHeight;.#endif
2018c 0a 20 20 69 66 28 20 70 4e 43 2d 3e 6e 45 72 72  .  if( pNC->nErr
2018d 3e 30 20 7c 7c 20 77 2e 70 50 61 72 73 65 2d 3e  >0 || w.pParse->
2018e 6e 45 72 72 3e 30 20 29 7b 0a 20 20 20 20 45 78  nErr>0 ){.    Ex
2018f 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 45  prSetProperty(pE
20190 78 70 72 2c 20 45 50 5f 45 72 72 6f 72 29 3b 0a  xpr, EP_Error);.
20191 20 20 7d 0a 20 20 69 66 28 20 70 4e 43 2d 3e 68    }.  if( pNC->h
20192 61 73 41 67 67 20 29 7b 0a 20 20 20 20 45 78 70  asAgg ){.    Exp
20193 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78  rSetProperty(pEx
20194 70 72 2c 20 45 50 5f 41 67 67 29 3b 0a 20 20 7d  pr, EP_Agg);.  }
20195 65 6c 73 65 20 69 66 28 20 73 61 76 65 64 48 61  else if( savedHa
20196 73 41 67 67 20 29 7b 0a 20 20 20 20 70 4e 43 2d  sAgg ){.    pNC-
20197 3e 68 61 73 41 67 67 20 3d 20 31 3b 0a 20 20 7d  >hasAgg = 1;.  }
20198 0a 20 20 72 65 74 75 72 6e 20 45 78 70 72 48 61  .  return ExprHa
20199 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
2019a 20 45 50 5f 45 72 72 6f 72 29 3b 0a 7d 0a 0a 0a   EP_Error);.}...
2019b 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 61 6c  /*.** Resolve al
2019c 6c 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 65  l names in all e
2019d 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 61 20  xpressions of a 
2019e 53 45 4c 45 43 54 20 61 6e 64 20 69 6e 20 61 6c  SELECT and in al
2019f 6c 0a 2a 2a 20 64 65 63 65 6e 64 65 6e 74 73 20  l.** decendents 
201a0 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2c 20 69  of the SELECT, i
201a1 6e 63 6c 75 64 69 6e 67 20 63 6f 6d 70 6f 75 6e  ncluding compoun
201a2 64 73 20 6f 66 66 20 6f 66 20 70 2d 3e 70 50 72  ds off of p->pPr
201a3 69 6f 72 2c 0a 2a 2a 20 73 75 62 71 75 65 72 69  ior,.** subqueri
201a4 65 73 20 69 6e 20 65 78 70 72 65 73 73 69 6f 6e  es in expression
201a5 73 2c 20 61 6e 64 20 73 75 62 71 75 65 72 69 65  s, and subquerie
201a6 73 20 75 73 65 64 20 61 73 20 46 52 4f 4d 20 63  s used as FROM c
201a7 6c 61 75 73 65 0a 2a 2a 20 74 65 72 6d 73 2e 0a  lause.** terms..
201a8 2a 2a 0a 2a 2a 20 53 65 65 20 73 71 6c 69 74 65  **.** See sqlite
201a9 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
201aa 73 28 29 20 66 6f 72 20 61 20 64 65 73 63 72 69  s() for a descri
201ab 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 6b 69 6e  ption of the kin
201ac 64 73 20 6f 66 0a 2a 2a 20 74 72 61 6e 73 66 6f  ds of.** transfo
201ad 72 6d 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 63  rmations that oc
201ae 63 75 72 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 53  cur..**.** All S
201af 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73  ELECT statements
201b0 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65   should have bee
201b1 6e 20 65 78 70 61 6e 64 65 64 20 75 73 69 6e 67  n expanded using
201b2 0a 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63  .** sqlite3Selec
201b3 74 45 78 70 61 6e 64 28 29 20 70 72 69 6f 72 20  tExpand() prior 
201b4 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73  to invoking this
201b5 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c   routine..*/.SQL
201b6 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
201b7 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53   sqlite3ResolveS
201b8 65 6c 65 63 74 4e 61 6d 65 73 28 0a 20 20 50 61  electNames(.  Pa
201b9 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
201ba 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
201bb 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
201bc 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20  Select *p,      
201bd 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45         /* The SE
201be 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 62  LECT statement b
201bf 65 69 6e 67 20 63 6f 64 65 64 2e 20 2a 2f 0a 20  eing coded. */. 
201c0 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4f   NameContext *pO
201c1 75 74 65 72 4e 43 20 20 2f 2a 20 4e 61 6d 65 20  uterNC  /* Name 
201c2 63 6f 6e 74 65 78 74 20 66 6f 72 20 70 61 72 65  context for pare
201c3 6e 74 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  nt SELECT statem
201c4 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 57 61 6c 6b  ent */.){.  Walk
201c5 65 72 20 77 3b 0a 0a 20 20 61 73 73 65 72 74 28  er w;..  assert(
201c6 20 70 21 3d 30 20 29 3b 0a 20 20 77 2e 78 45 78   p!=0 );.  w.xEx
201c7 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73  prCallback = res
201c8 6f 6c 76 65 45 78 70 72 53 74 65 70 3b 0a 20 20  olveExprStep;.  
201c9 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63  w.xSelectCallbac
201ca 6b 20 3d 20 72 65 73 6f 6c 76 65 53 65 6c 65 63  k = resolveSelec
201cb 74 53 74 65 70 3b 0a 20 20 77 2e 70 50 61 72 73  tStep;.  w.pPars
201cc 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 77 2e  e = pParse;.  w.
201cd 75 2e 70 4e 43 20 3d 20 70 4f 75 74 65 72 4e 43  u.pNC = pOuterNC
201ce 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53  ;.  sqlite3WalkS
201cf 65 6c 65 63 74 28 26 77 2c 20 70 29 3b 0a 7d 0a  elect(&w, p);.}.
201d0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
201d1 20 45 6e 64 20 6f 66 20 72 65 73 6f 6c 76 65 2e   End of resolve.
201d2 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
201d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
201d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
201d5 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
201d6 20 42 65 67 69 6e 20 66 69 6c 65 20 65 78 70 72   Begin file expr
201d7 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
201d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
201d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
201da 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
201db 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ember 15.**.** T
201dc 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
201dd 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
201de 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
201df 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
201e0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
201e1 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
201e2 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
201e3 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
201e4 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
201e5 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
201e6 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
201e7 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
201e8 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
201e9 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
201ea 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
201eb 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
201ec 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
201ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
201ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
201ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
201f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
201f1 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
201f2 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f  file contains ro
201f3 75 74 69 6e 65 73 20 75 73 65 64 20 66 6f 72 20  utines used for 
201f4 61 6e 61 6c 79 7a 69 6e 67 20 65 78 70 72 65 73  analyzing expres
201f5 73 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20 66 6f 72  sions and.** for
201f6 20 67 65 6e 65 72 61 74 69 6e 67 20 56 44 42 45   generating VDBE
201f7 20 63 6f 64 65 20 74 68 61 74 20 65 76 61 6c 75   code that evalu
201f8 61 74 65 73 20 65 78 70 72 65 73 73 69 6f 6e 73  ates expressions
201f9 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 0a   in SQLite..*/..
201fa 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
201fb 20 27 61 66 66 69 6e 69 74 79 27 20 6f 66 20 74   'affinity' of t
201fc 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45  he expression pE
201fd 78 70 72 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a  xpr if any..**.*
201fe 2a 20 49 66 20 70 45 78 70 72 20 69 73 20 61 20  * If pExpr is a 
201ff 63 6f 6c 75 6d 6e 2c 20 61 20 72 65 66 65 72 65  column, a refere
20200 6e 63 65 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20  nce to a column 
20201 76 69 61 20 61 6e 20 27 41 53 27 20 61 6c 69 61  via an 'AS' alia
20202 73 2c 0a 2a 2a 20 6f 72 20 61 20 73 75 62 2d 73  s,.** or a sub-s
20203 65 6c 65 63 74 20 77 69 74 68 20 61 20 63 6f 6c  elect with a col
20204 75 6d 6e 20 61 73 20 74 68 65 20 72 65 74 75 72  umn as the retur
20205 6e 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 68  n value, then th
20206 65 20 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 6f  e .** affinity o
20207 66 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 69 73  f that column is
20208 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72   returned. Other
20209 77 69 73 65 2c 20 30 78 30 30 20 69 73 20 72 65  wise, 0x00 is re
2020a 74 75 72 6e 65 64 2c 0a 2a 2a 20 69 6e 64 69 63  turned,.** indic
2020b 61 74 69 6e 67 20 6e 6f 20 61 66 66 69 6e 69 74  ating no affinit
2020c 79 20 66 6f 72 20 74 68 65 20 65 78 70 72 65 73  y for the expres
2020d 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e  sion..**.** i.e.
2020e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2020f 65 20 65 78 70 72 65 73 73 73 69 6f 6e 73 20 69  e expresssions i
20210 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
20211 73 74 61 74 65 6d 65 6e 74 73 20 61 6c 6c 0a 2a  statements all.*
20212 2a 20 68 61 76 65 20 61 6e 20 61 66 66 69 6e 69  * have an affini
20213 74 79 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45  ty:.**.** CREATE
20214 20 54 41 42 4c 45 20 74 31 28 61 29 3b 0a 2a 2a   TABLE t1(a);.**
20215 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74   SELECT * FROM t
20216 31 20 57 48 45 52 45 20 61 3b 0a 2a 2a 20 53 45  1 WHERE a;.** SE
20217 4c 45 43 54 20 61 20 41 53 20 62 20 46 52 4f 4d  LECT a AS b FROM
20218 20 74 31 20 57 48 45 52 45 20 62 3b 0a 2a 2a 20   t1 WHERE b;.** 
20219 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31  SELECT * FROM t1
2021a 20 57 48 45 52 45 20 28 73 65 6c 65 63 74 20 61   WHERE (select a
2021b 20 66 72 6f 6d 20 74 31 29 3b 0a 2a 2f 0a 53 51   from t1);.*/.SQ
2021c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
2021d 72 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66  r sqlite3ExprAff
2021e 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70  inity(Expr *pExp
2021f 72 29 7b 0a 20 20 69 6e 74 20 6f 70 20 3d 20 70  r){.  int op = p
20220 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 69 66 28 20  Expr->op;.  if( 
20221 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 7b  op==TK_SELECT ){
20222 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78  .    assert( pEx
20223 70 72 2d 3e 66 6c 61 67 73 26 45 50 5f 78 49 73  pr->flags&EP_xIs
20224 53 65 6c 65 63 74 20 29 3b 0a 20 20 20 20 72 65  Select );.    re
20225 74 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72  turn sqlite3Expr
20226 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e  Affinity(pExpr->
20227 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73  x.pSelect->pELis
20228 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a  t->a[0].pExpr);.
20229 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
2022a 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20 69  TE_OMIT_CAST.  i
2022b 66 28 20 6f 70 3d 3d 54 4b 5f 43 41 53 54 20 29  f( op==TK_CAST )
2022c 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 45  {.    assert( !E
2022d 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
2022e 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75  Expr, EP_IntValu
2022f 65 29 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  e) );.    return
20230 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79   sqlite3Affinity
20231 54 79 70 65 28 70 45 78 70 72 2d 3e 75 2e 7a 54  Type(pExpr->u.zT
20232 6f 6b 65 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  oken);.  }.#endi
20233 66 0a 20 20 69 66 28 20 28 6f 70 3d 3d 54 4b 5f  f.  if( (op==TK_
20234 41 47 47 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70  AGG_COLUMN || op
20235 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f  ==TK_COLUMN || o
20236 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 29 20  p==TK_REGISTER) 
20237 0a 20 20 20 26 26 20 70 45 78 70 72 2d 3e 70 54  .   && pExpr->pT
20238 61 62 21 3d 30 0a 20 20 29 7b 0a 20 20 20 20 2f  ab!=0.  ){.    /
20239 2a 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45  * op==TK_REGISTE
2023a 52 20 26 26 20 70 45 78 70 72 2d 3e 70 54 61 62  R && pExpr->pTab
2023b 21 3d 30 20 68 61 70 70 65 6e 73 20 77 68 65 6e  !=0 happens when
2023c 20 70 45 78 70 72 20 77 61 73 20 6f 72 69 67 69   pExpr was origi
2023d 6e 61 6c 6c 79 0a 20 20 20 20 2a 2a 20 61 20 54  nally.    ** a T
2023e 4b 5f 43 4f 4c 55 4d 4e 20 62 75 74 20 77 61 73  K_COLUMN but was
2023f 20 70 72 65 76 69 6f 75 73 6c 79 20 65 76 61 6c   previously eval
20240 75 61 74 65 64 20 61 6e 64 20 63 61 63 68 65 64  uated and cached
20241 20 69 6e 20 61 20 72 65 67 69 73 74 65 72 20 2a   in a register *
20242 2f 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 45  /.    int j = pE
20243 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20  xpr->iColumn;.  
20244 20 20 69 66 28 20 6a 3c 30 20 29 20 72 65 74 75    if( j<0 ) retu
20245 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e  rn SQLITE_AFF_IN
20246 54 45 47 45 52 3b 0a 20 20 20 20 61 73 73 65 72  TEGER;.    asser
20247 74 28 20 70 45 78 70 72 2d 3e 70 54 61 62 20 26  t( pExpr->pTab &
20248 26 20 6a 3c 70 45 78 70 72 2d 3e 70 54 61 62 2d  & j<pExpr->pTab-
20249 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 72 65 74  >nCol );.    ret
2024a 75 72 6e 20 70 45 78 70 72 2d 3e 70 54 61 62 2d  urn pExpr->pTab-
2024b 3e 61 43 6f 6c 5b 6a 5d 2e 61 66 66 69 6e 69 74  >aCol[j].affinit
2024c 79 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  y;.  }.  return 
2024d 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3b  pExpr->affinity;
2024e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
2024f 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  e collating sequ
20250 65 6e 63 65 20 66 6f 72 20 65 78 70 72 65 73 73  ence for express
20251 69 6f 6e 20 70 45 78 70 72 20 74 6f 20 62 65 20  ion pExpr to be 
20252 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a  the collating.**
20253 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 64 20   sequence named 
20254 62 79 20 70 54 6f 6b 65 6e 2e 20 20 20 52 65 74  by pToken.   Ret
20255 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
20256 20 74 68 65 20 72 65 76 69 73 65 64 20 65 78 70   the revised exp
20257 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20  ression..** The 
20258 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
20259 63 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20  ce is marked as 
2025a 22 65 78 70 6c 69 63 69 74 22 20 75 73 69 6e 67  "explicit" using
2025b 20 74 68 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61   the EP_ExpColla
2025c 74 65 0a 2a 2a 20 66 6c 61 67 2e 20 20 41 6e 20  te.** flag.  An 
2025d 65 78 70 6c 69 63 69 74 20 63 6f 6c 6c 61 74 69  explicit collati
2025e 6e 67 20 73 65 71 75 65 6e 63 65 20 77 69 6c 6c  ng sequence will
2025f 20 6f 76 65 72 72 69 64 65 20 69 6d 70 6c 69 63   override implic
20260 69 74 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20  it.** collating 
20261 73 65 71 75 65 6e 63 65 73 2e 0a 2a 2f 0a 53 51  sequences..*/.SQ
20262 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
20263 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 53 65  r *sqlite3ExprSe
20264 74 43 6f 6c 6c 28 50 61 72 73 65 20 2a 70 50 61  tColl(Parse *pPa
20265 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72  rse, Expr *pExpr
20266 2c 20 54 6f 6b 65 6e 20 2a 70 43 6f 6c 6c 4e 61  , Token *pCollNa
20267 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 43 6f  me){.  char *zCo
20268 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ll = 0;         
20269 20 20 20 2f 2a 20 44 65 71 75 6f 74 65 64 20 6e     /* Dequoted n
2026a 61 6d 65 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e  ame of collation
2026b 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 20 20 43   sequence */.  C
2026c 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20  ollSeq *pColl;. 
2026d 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
2026e 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 7a 43 6f  Parse->db;.  zCo
2026f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  ll = sqlite3Name
20270 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 43  FromToken(db, pC
20271 6f 6c 6c 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  ollName);.  if( 
20272 70 45 78 70 72 20 26 26 20 7a 43 6f 6c 6c 20 29  pExpr && zColl )
20273 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71  {.    pColl = sq
20274 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53  lite3LocateCollS
20275 65 71 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c  eq(pParse, zColl
20276 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c  );.    if( pColl
20277 20 29 7b 0a 20 20 20 20 20 20 70 45 78 70 72 2d   ){.      pExpr-
20278 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a  >pColl = pColl;.
20279 20 20 20 20 20 20 70 45 78 70 72 2d 3e 66 6c 61        pExpr->fla
2027a 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c  gs |= EP_ExpColl
2027b 61 74 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ate;.    }.  }. 
2027c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2027d 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 72 65 74  b, zColl);.  ret
2027e 75 72 6e 20 70 45 78 70 72 3b 0a 7d 0a 0a 2f 2a  urn pExpr;.}../*
2027f 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64  .** Return the d
20280 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e  efault collation
20281 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68   sequence for th
20282 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78  e expression pEx
20283 70 72 2e 20 49 66 0a 2a 2a 20 74 68 65 72 65 20  pr. If.** there 
20284 69 73 20 6e 6f 20 64 65 66 61 75 6c 74 20 63 6f  is no default co
20285 6c 6c 61 74 69 6f 6e 20 74 79 70 65 2c 20 72 65  llation type, re
20286 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54  turn 0..*/.SQLIT
20287 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65  E_PRIVATE CollSe
20288 71 20 2a 73 71 6c 69 74 65 33 45 78 70 72 43 6f  q *sqlite3ExprCo
20289 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50 61  llSeq(Parse *pPa
2028a 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72  rse, Expr *pExpr
2028b 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43  ){.  CollSeq *pC
2028c 6f 6c 6c 20 3d 20 30 3b 0a 20 20 45 78 70 72 20  oll = 0;.  Expr 
2028d 2a 70 20 3d 20 70 45 78 70 72 3b 0a 20 20 77 68  *p = pExpr;.  wh
2028e 69 6c 65 28 20 41 4c 57 41 59 53 28 70 29 20 29  ile( ALWAYS(p) )
2028f 7b 0a 20 20 20 20 69 6e 74 20 6f 70 3b 0a 20 20  {.    int op;.  
20290 20 20 70 43 6f 6c 6c 20 3d 20 70 2d 3e 70 43 6f    pColl = p->pCo
20291 6c 6c 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c  ll;.    if( pCol
20292 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 6f  l ) break;.    o
20293 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 20 20 20 69  p = p->op;.    i
20294 66 28 20 70 2d 3e 70 54 61 62 21 3d 30 20 26 26  f( p->pTab!=0 &&
20295 20 28 0a 20 20 20 20 20 20 20 20 6f 70 3d 3d 54   (.        op==T
20296 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 7c 7c 20  K_AGG_COLUMN || 
20297 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c  op==TK_COLUMN ||
20298 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52   op==TK_REGISTER
20299 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 54 52 49 47 47   || op==TK_TRIGG
2029a 45 52 0a 20 20 20 20 29 29 7b 0a 20 20 20 20 20  ER.    )){.     
2029b 20 2f 2a 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53   /* op==TK_REGIS
2029c 54 45 52 20 26 26 20 70 2d 3e 70 54 61 62 21 3d  TER && p->pTab!=
2029d 30 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 70  0 happens when p
2029e 45 78 70 72 20 77 61 73 20 6f 72 69 67 69 6e 61  Expr was origina
2029f 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20 61 20 54  lly.      ** a T
202a0 4b 5f 43 4f 4c 55 4d 4e 20 62 75 74 20 77 61 73  K_COLUMN but was
202a1 20 70 72 65 76 69 6f 75 73 6c 79 20 65 76 61 6c   previously eval
202a2 75 61 74 65 64 20 61 6e 64 20 63 61 63 68 65 64  uated and cached
202a3 20 69 6e 20 61 20 72 65 67 69 73 74 65 72 20 2a   in a register *
202a4 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  /.      const ch
202a5 61 72 20 2a 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20  ar *zColl;.     
202a6 20 69 6e 74 20 6a 20 3d 20 70 2d 3e 69 43 6f 6c   int j = p->iCol
202a7 75 6d 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 6a  umn;.      if( j
202a8 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73  >=0 ){.        s
202a9 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
202aa 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 20  rse->db;.       
202ab 20 7a 43 6f 6c 6c 20 3d 20 70 2d 3e 70 54 61 62   zColl = p->pTab
202ac 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b  ->aCol[j].zColl;
202ad 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d  .        pColl =
202ae 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c   sqlite3FindColl
202af 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62 29 2c  Seq(db, ENC(db),
202b0 20 7a 43 6f 6c 6c 2c 20 30 29 3b 0a 20 20 20 20   zColl, 0);.    
202b1 20 20 20 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c      pExpr->pColl
202b2 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20   = pColl;.      
202b3 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
202b4 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6f 70 21     }.    if( op!
202b5 3d 54 4b 5f 43 41 53 54 20 26 26 20 6f 70 21 3d  =TK_CAST && op!=
202b6 54 4b 5f 55 50 4c 55 53 20 29 7b 0a 20 20 20 20  TK_UPLUS ){.    
202b7 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
202b8 20 20 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b     p = p->pLeft;
202b9 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74  .  }.  if( sqlit
202ba 65 33 43 68 65 63 6b 43 6f 6c 6c 53 65 71 28 70  e3CheckCollSeq(p
202bb 50 61 72 73 65 2c 20 70 43 6f 6c 6c 29 20 29 7b  Parse, pColl) ){
202bc 20 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 30 3b   .    pColl = 0;
202bd 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 43  .  }.  return pC
202be 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45  oll;.}../*.** pE
202bf 78 70 72 20 69 73 20 61 6e 20 6f 70 65 72 61 6e  xpr is an operan
202c0 64 20 6f 66 20 61 20 63 6f 6d 70 61 72 69 73 6f  d of a compariso
202c1 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20 61 66 66  n operator.  aff
202c2 32 20 69 73 20 74 68 65 0a 2a 2a 20 74 79 70 65  2 is the.** type
202c3 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65   affinity of the
202c4 20 6f 74 68 65 72 20 6f 70 65 72 61 6e 64 2e 20   other operand. 
202c5 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
202c6 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 74 79 70  turns the.** typ
202c7 65 20 61 66 66 69 6e 69 74 79 20 74 68 61 74 20  e affinity that 
202c8 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66  should be used f
202c9 6f 72 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  or the compariso
202ca 6e 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 53  n operator..*/.S
202cb 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
202cc 61 72 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72  ar sqlite3Compar
202cd 65 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a  eAffinity(Expr *
202ce 70 45 78 70 72 2c 20 63 68 61 72 20 61 66 66 32  pExpr, char aff2
202cf 29 7b 0a 20 20 63 68 61 72 20 61 66 66 31 20 3d  ){.  char aff1 =
202d0 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69   sqlite3ExprAffi
202d1 6e 69 74 79 28 70 45 78 70 72 29 3b 0a 20 20 69  nity(pExpr);.  i
202d2 66 28 20 61 66 66 31 20 26 26 20 61 66 66 32 20  f( aff1 && aff2 
202d3 29 7b 0a 20 20 20 20 2f 2a 20 42 6f 74 68 20 73  ){.    /* Both s
202d4 69 64 65 73 20 6f 66 20 74 68 65 20 63 6f 6d 70  ides of the comp
202d5 61 72 69 73 6f 6e 20 61 72 65 20 63 6f 6c 75 6d  arison are colum
202d6 6e 73 2e 20 49 66 20 6f 6e 65 20 68 61 73 20 6e  ns. If one has n
202d7 75 6d 65 72 69 63 0a 20 20 20 20 2a 2a 20 61 66  umeric.    ** af
202d8 66 69 6e 69 74 79 2c 20 75 73 65 20 74 68 61 74  finity, use that
202d9 2e 20 4f 74 68 65 72 77 69 73 65 20 75 73 65 20  . Otherwise use 
202da 6e 6f 20 61 66 66 69 6e 69 74 79 2e 0a 20 20 20  no affinity..   
202db 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69   */.    if( sqli
202dc 74 65 33 49 73 4e 75 6d 65 72 69 63 41 66 66 69  te3IsNumericAffi
202dd 6e 69 74 79 28 61 66 66 31 29 20 7c 7c 20 73 71  nity(aff1) || sq
202de 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 63 41 66  lite3IsNumericAf
202df 66 69 6e 69 74 79 28 61 66 66 32 29 20 29 7b 0a  finity(aff2) ){.
202e0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
202e1 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3b  ITE_AFF_NUMERIC;
202e2 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
202e3 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
202e4 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 7d 0a  AFF_NONE;.    }.
202e5 20 20 7d 65 6c 73 65 20 69 66 28 20 21 61 66 66    }else if( !aff
202e6 31 20 26 26 20 21 61 66 66 32 20 29 7b 0a 20 20  1 && !aff2 ){.  
202e7 20 20 2f 2a 20 4e 65 69 74 68 65 72 20 73 69 64    /* Neither sid
202e8 65 20 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69  e of the compari
202e9 73 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2e  son is a column.
202ea 20 20 43 6f 6d 70 61 72 65 20 74 68 65 0a 20 20    Compare the.  
202eb 20 20 2a 2a 20 72 65 73 75 6c 74 73 20 64 69 72    ** results dir
202ec 65 63 74 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20  ectly..    */.  
202ed 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
202ee 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 65 6c 73  AFF_NONE;.  }els
202ef 65 7b 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 73 69  e{.    /* One si
202f0 64 65 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20  de is a column, 
202f1 74 68 65 20 6f 74 68 65 72 20 69 73 20 6e 6f 74  the other is not
202f2 2e 20 55 73 65 20 74 68 65 20 63 6f 6c 75 6d 6e  . Use the column
202f3 73 20 61 66 66 69 6e 69 74 79 2e 20 2a 2f 0a 20  s affinity. */. 
202f4 20 20 20 61 73 73 65 72 74 28 20 61 66 66 31 3d     assert( aff1=
202f5 3d 30 20 7c 7c 20 61 66 66 32 3d 3d 30 20 29 3b  =0 || aff2==0 );
202f6 0a 20 20 20 20 72 65 74 75 72 6e 20 28 61 66 66  .    return (aff
202f7 31 20 2b 20 61 66 66 32 29 3b 0a 20 20 7d 0a 7d  1 + aff2);.  }.}
202f8 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73  ../*.** pExpr is
202f9 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70   a comparison op
202fa 65 72 61 74 6f 72 2e 20 20 52 65 74 75 72 6e 20  erator.  Return 
202fb 74 68 65 20 74 79 70 65 20 61 66 66 69 6e 69 74  the type affinit
202fc 79 20 74 68 61 74 20 73 68 6f 75 6c 64 0a 2a 2a  y that should.**
202fd 20 62 65 20 61 70 70 6c 69 65 64 20 74 6f 20 62   be applied to b
202fe 6f 74 68 20 6f 70 65 72 61 6e 64 73 20 70 72 69  oth operands pri
202ff 6f 72 20 74 6f 20 64 6f 69 6e 67 20 74 68 65 20  or to doing the 
20300 63 6f 6d 70 61 72 69 73 6f 6e 2e 0a 2a 2f 0a 73  comparison..*/.s
20301 74 61 74 69 63 20 63 68 61 72 20 63 6f 6d 70 61  tatic char compa
20302 72 69 73 6f 6e 41 66 66 69 6e 69 74 79 28 45 78  risonAffinity(Ex
20303 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 63 68  pr *pExpr){.  ch
20304 61 72 20 61 66 66 3b 0a 20 20 61 73 73 65 72 74  ar aff;.  assert
20305 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
20306 45 51 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d  EQ || pExpr->op=
20307 3d 54 4b 5f 49 4e 20 7c 7c 20 70 45 78 70 72 2d  =TK_IN || pExpr-
20308 3e 6f 70 3d 3d 54 4b 5f 4c 54 20 7c 7c 0a 20 20  >op==TK_LT ||.  
20309 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f          pExpr->o
2030a 70 3d 3d 54 4b 5f 47 54 20 7c 7c 20 70 45 78 70  p==TK_GT || pExp
2030b 72 2d 3e 6f 70 3d 3d 54 4b 5f 47 45 20 7c 7c 20  r->op==TK_GE || 
2030c 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c 45  pExpr->op==TK_LE
2030d 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 70 45   ||.          pE
2030e 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4e 45 20 29  xpr->op==TK_NE )
2030f 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70  ;.  assert( pExp
20310 72 2d 3e 70 4c 65 66 74 20 29 3b 0a 20 20 61 66  r->pLeft );.  af
20311 66 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  f = sqlite3ExprA
20312 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70  ffinity(pExpr->p
20313 4c 65 66 74 29 3b 0a 20 20 69 66 28 20 70 45 78  Left);.  if( pEx
20314 70 72 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20 20  pr->pRight ){.  
20315 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 43    aff = sqlite3C
20316 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70  ompareAffinity(p
20317 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 61 66  Expr->pRight, af
20318 66 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  f);.  }else if( 
20319 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
2031a 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
2031b 65 63 74 29 20 29 7b 0a 20 20 20 20 61 66 66 20  ect) ){.    aff 
2031c 3d 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65  = sqlite3Compare
2031d 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e  Affinity(pExpr->
2031e 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73  x.pSelect->pELis
2031f 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c 20 61  t->a[0].pExpr, a
20320 66 66 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ff);.  }else if(
20321 20 21 61 66 66 20 29 7b 0a 20 20 20 20 61 66 66   !aff ){.    aff
20322 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f   = SQLITE_AFF_NO
20323 4e 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  NE;.  }.  return
20324 20 61 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70   aff;.}../*.** p
20325 45 78 70 72 20 69 73 20 61 20 63 6f 6d 70 61 72  Expr is a compar
20326 69 73 6f 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c  ison expression,
20327 20 65 67 2e 20 27 3d 27 2c 20 27 3c 27 2c 20 49   eg. '=', '<', I
20328 4e 28 2e 2e 2e 29 20 65 74 63 2e 0a 2a 2a 20 69  N(...) etc..** i
20329 64 78 5f 61 66 66 69 6e 69 74 79 20 69 73 20 74  dx_affinity is t
2032a 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 61  he affinity of a
2032b 6e 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e  n indexed column
2032c 2e 20 52 65 74 75 72 6e 20 74 72 75 65 0a 2a 2a  . Return true.**
2032d 20 69 66 20 74 68 65 20 69 6e 64 65 78 20 77 69   if the index wi
2032e 74 68 20 61 66 66 69 6e 69 74 79 20 69 64 78 5f  th affinity idx_
2032f 61 66 66 69 6e 69 74 79 20 6d 61 79 20 62 65 20  affinity may be 
20330 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
20331 74 0a 2a 2a 20 74 68 65 20 63 6f 6d 70 61 72 69  t.** the compari
20332 73 6f 6e 20 69 6e 20 70 45 78 70 72 2e 0a 2a 2f  son in pExpr..*/
20333 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20334 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78  int sqlite3Index
20335 41 66 66 69 6e 69 74 79 4f 6b 28 45 78 70 72 20  AffinityOk(Expr 
20336 2a 70 45 78 70 72 2c 20 63 68 61 72 20 69 64 78  *pExpr, char idx
20337 5f 61 66 66 69 6e 69 74 79 29 7b 0a 20 20 63 68  _affinity){.  ch
20338 61 72 20 61 66 66 20 3d 20 63 6f 6d 70 61 72 69  ar aff = compari
20339 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 45 78 70  sonAffinity(pExp
2033a 72 29 3b 0a 20 20 73 77 69 74 63 68 28 20 61 66  r);.  switch( af
2033b 66 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  f ){.    case SQ
2033c 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a 0a 20  LITE_AFF_NONE:. 
2033d 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
2033e 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41     case SQLITE_A
2033f 46 46 5f 54 45 58 54 3a 0a 20 20 20 20 20 20 72  FF_TEXT:.      r
20340 65 74 75 72 6e 20 69 64 78 5f 61 66 66 69 6e 69  eturn idx_affini
20341 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54  ty==SQLITE_AFF_T
20342 45 58 54 3b 0a 20 20 20 20 64 65 66 61 75 6c 74  EXT;.    default
20343 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 73  :.      return s
20344 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 63 41  qlite3IsNumericA
20345 66 66 69 6e 69 74 79 28 69 64 78 5f 61 66 66 69  ffinity(idx_affi
20346 6e 69 74 79 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  nity);.  }.}../*
20347 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 50  .** Return the P
20348 35 20 76 61 6c 75 65 20 74 68 61 74 20 73 68 6f  5 value that sho
20349 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 20  uld be used for 
2034a 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61 72 69  a binary compari
2034b 73 6f 6e 0a 2a 2a 20 6f 70 63 6f 64 65 20 28 4f  son.** opcode (O
2034c 50 5f 45 71 2c 20 4f 50 5f 47 65 20 65 74 63 2e  P_Eq, OP_Ge etc.
2034d 29 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 61 72  ) used to compar
2034e 65 20 70 45 78 70 72 31 20 61 6e 64 20 70 45 78  e pExpr1 and pEx
2034f 70 72 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75  pr2..*/.static u
20350 38 20 62 69 6e 61 72 79 43 6f 6d 70 61 72 65 50  8 binaryCompareP
20351 35 28 45 78 70 72 20 2a 70 45 78 70 72 31 2c 20  5(Expr *pExpr1, 
20352 45 78 70 72 20 2a 70 45 78 70 72 32 2c 20 69 6e  Expr *pExpr2, in
20353 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20  t jumpIfNull){. 
20354 20 75 38 20 61 66 66 20 3d 20 28 63 68 61 72 29   u8 aff = (char)
20355 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e  sqlite3ExprAffin
20356 69 74 79 28 70 45 78 70 72 32 29 3b 0a 20 20 61  ity(pExpr2);.  a
20357 66 66 20 3d 20 28 75 38 29 73 71 6c 69 74 65 33  ff = (u8)sqlite3
20358 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28  CompareAffinity(
20359 70 45 78 70 72 31 2c 20 61 66 66 29 20 7c 20 28  pExpr1, aff) | (
2035a 75 38 29 6a 75 6d 70 49 66 4e 75 6c 6c 3b 0a 20  u8)jumpIfNull;. 
2035b 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a   return aff;.}..
2035c 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70  /*.** Return a p
2035d 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 6f  ointer to the co
2035e 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
2035f 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20   that should be 
20360 75 73 65 64 20 62 79 0a 2a 2a 20 61 20 62 69 6e  used by.** a bin
20361 61 72 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f  ary comparison o
20362 70 65 72 61 74 6f 72 20 63 6f 6d 70 61 72 69 6e  perator comparin
20363 67 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67  g pLeft and pRig
20364 68 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ht..**.** If the
20365 20 6c 65 66 74 20 68 61 6e 64 20 65 78 70 72 65   left hand expre
20366 73 73 69 6f 6e 20 68 61 73 20 61 20 63 6f 6c 6c  ssion has a coll
20367 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 74  ating sequence t
20368 79 70 65 2c 20 74 68 65 6e 20 69 74 20 69 73 0a  ype, then it is.
20369 2a 2a 20 75 73 65 64 2e 20 4f 74 68 65 72 77 69  ** used. Otherwi
2036a 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  se the collation
2036b 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68   sequence for th
2036c 65 20 72 69 67 68 74 20 68 61 6e 64 20 65 78 70  e right hand exp
2036d 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 75 73  ression.** is us
2036e 65 64 2c 20 6f 72 20 74 68 65 20 64 65 66 61 75  ed, or the defau
2036f 6c 74 20 28 42 49 4e 41 52 59 29 20 69 66 20 6e  lt (BINARY) if n
20370 65 69 74 68 65 72 20 65 78 70 72 65 73 73 69 6f  either expressio
20371 6e 20 68 61 73 20 61 20 63 6f 6c 6c 61 74 69 6e  n has a collatin
20372 67 0a 2a 2a 20 74 79 70 65 2e 0a 2a 2a 0a 2a 2a  g.** type..**.**
20373 20 41 72 67 75 6d 65 6e 74 20 70 52 69 67 68 74   Argument pRight
20374 20 28 62 75 74 20 6e 6f 74 20 70 4c 65 66 74 29   (but not pLeft)
20375 20 6d 61 79 20 62 65 20 61 20 6e 75 6c 6c 20 70   may be a null p
20376 6f 69 6e 74 65 72 2e 20 49 6e 20 74 68 69 73 20  ointer. In this 
20377 63 61 73 65 2c 0a 2a 2a 20 69 74 20 69 73 20 6e  case,.** it is n
20378 6f 74 20 63 6f 6e 73 69 64 65 72 65 64 2e 0a 2a  ot considered..*
20379 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2037a 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65   CollSeq *sqlite
2037b 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f  3BinaryCompareCo
2037c 6c 6c 53 65 71 28 0a 20 20 50 61 72 73 65 20 2a  llSeq(.  Parse *
2037d 70 50 61 72 73 65 2c 20 0a 20 20 45 78 70 72 20  pParse, .  Expr 
2037e 2a 70 4c 65 66 74 2c 20 0a 20 20 45 78 70 72 20  *pLeft, .  Expr 
2037f 2a 70 52 69 67 68 74 0a 29 7b 0a 20 20 43 6f 6c  *pRight.){.  Col
20380 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 61  lSeq *pColl;.  a
20381 73 73 65 72 74 28 20 70 4c 65 66 74 20 29 3b 0a  ssert( pLeft );.
20382 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 66 6c 61    if( pLeft->fla
20383 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61  gs & EP_ExpColla
20384 74 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  te ){.    assert
20385 28 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 20 29  ( pLeft->pColl )
20386 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 4c  ;.    pColl = pL
20387 65 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65  eft->pColl;.  }e
20388 6c 73 65 20 69 66 28 20 70 52 69 67 68 74 20 26  lse if( pRight &
20389 26 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20  & pRight->flags 
2038a 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20  & EP_ExpCollate 
2038b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
2038c 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 20 29 3b 0a  Right->pColl );.
2038d 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 52 69 67      pColl = pRig
2038e 68 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65 6c  ht->pColl;.  }el
2038f 73 65 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20  se{.    pColl = 
20390 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53  sqlite3ExprCollS
20391 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74  eq(pParse, pLeft
20392 29 3b 0a 20 20 20 20 69 66 28 20 21 70 43 6f 6c  );.    if( !pCol
20393 6c 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 6c  l ){.      pColl
20394 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
20395 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 52  llSeq(pParse, pR
20396 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ight);.    }.  }
20397 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b  .  return pColl;
20398 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
20399 74 65 20 74 68 65 20 6f 70 65 72 61 6e 64 73 20  te the operands 
2039a 66 6f 72 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e  for a comparison
2039b 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 65 66   operation.  Bef
2039c 6f 72 65 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6e  ore.** generatin
2039d 67 20 74 68 65 20 63 6f 64 65 20 66 6f 72 20 65  g the code for e
2039e 61 63 68 20 6f 70 65 72 61 6e 64 2c 20 73 65 74  ach operand, set
2039f 20 74 68 65 20 45 50 5f 41 6e 79 41 66 66 0a 2a   the EP_AnyAff.*
203a0 2a 20 66 6c 61 67 20 6f 6e 20 74 68 65 20 65 78  * flag on the ex
203a1 70 72 65 73 73 69 6f 6e 20 73 6f 20 74 68 61 74  pression so that
203a2 20 69 74 20 77 69 6c 6c 20 62 65 20 61 62 6c 65   it will be able
203a3 20 74 6f 20 75 73 65 64 20 61 0a 2a 2a 20 63 61   to used a.** ca
203a4 63 68 65 64 20 63 6f 6c 75 6d 6e 20 76 61 6c 75  ched column valu
203a5 65 20 74 68 61 74 20 68 61 73 20 70 72 65 76 69  e that has previ
203a6 6f 75 73 6c 79 20 75 6e 64 65 72 67 6f 6e 65 20  ously undergone 
203a7 61 6e 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 63  an.** affinity c
203a8 68 61 6e 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  hange..*/.static
203a9 20 76 6f 69 64 20 63 6f 64 65 43 6f 6d 70 61 72   void codeCompar
203aa 65 4f 70 65 72 61 6e 64 73 28 0a 20 20 50 61 72  eOperands(.  Par
203ab 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f  se *pParse,    /
203ac 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f  * Parsing and co
203ad 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  de generating co
203ae 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20  ntext */.  Expr 
203af 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a 20  *pLeft,      /* 
203b0 54 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64  The left operand
203b1 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 67 4c   */.  int *pRegL
203b2 65 66 74 2c 20 20 20 20 2f 2a 20 52 65 67 69 73  eft,    /* Regis
203b3 74 65 72 20 77 68 65 72 65 20 6c 65 66 74 20 6f  ter where left o
203b4 70 65 72 61 6e 64 20 69 73 20 73 74 6f 72 65 64  perand is stored
203b5 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 46 72 65 65   */.  int *pFree
203b6 4c 65 66 74 2c 20 20 20 2f 2a 20 46 72 65 65 20  Left,   /* Free 
203b7 74 68 69 73 20 72 65 67 69 73 74 65 72 20 77 68  this register wh
203b8 65 6e 20 64 6f 6e 65 20 2a 2f 0a 20 20 45 78 70  en done */.  Exp
203b9 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20 20 2f  r *pRight,     /
203ba 2a 20 54 68 65 20 72 69 67 68 74 20 6f 70 65 72  * The right oper
203bb 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52  and */.  int *pR
203bc 65 67 52 69 67 68 74 2c 20 20 20 2f 2a 20 52 65  egRight,   /* Re
203bd 67 69 73 74 65 72 20 77 68 65 72 65 20 72 69 67  gister where rig
203be 68 74 20 6f 70 65 72 61 6e 64 20 69 73 20 73 74  ht operand is st
203bf 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  ored */.  int *p
203c0 46 72 65 65 52 69 67 68 74 20 20 20 2f 2a 20 57  FreeRight   /* W
203c1 72 69 74 65 20 74 65 6d 70 20 72 65 67 69 73 74  rite temp regist
203c2 65 72 20 66 6f 72 20 72 69 67 68 74 20 6f 70 65  er for right ope
203c3 72 61 6e 64 20 74 68 65 72 65 20 2a 2f 0a 29 7b  rand there */.){
203c4 0a 20 20 77 68 69 6c 65 28 20 70 4c 65 66 74 2d  .  while( pLeft-
203c5 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20  >op==TK_UPLUS ) 
203c6 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 2d 3e 70  pLeft = pLeft->p
203c7 4c 65 66 74 3b 0a 20 20 70 4c 65 66 74 2d 3e 66  Left;.  pLeft->f
203c8 6c 61 67 73 20 7c 3d 20 45 50 5f 41 6e 79 41 66  lags |= EP_AnyAf
203c9 66 3b 0a 20 20 2a 70 52 65 67 4c 65 66 74 20 3d  f;.  *pRegLeft =
203ca 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
203cb 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 4c 65  Temp(pParse, pLe
203cc 66 74 2c 20 70 46 72 65 65 4c 65 66 74 29 3b 0a  ft, pFreeLeft);.
203cd 20 20 77 68 69 6c 65 28 20 70 52 69 67 68 74 2d    while( pRight-
203ce 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20  >op==TK_UPLUS ) 
203cf 70 52 69 67 68 74 20 3d 20 70 52 69 67 68 74 2d  pRight = pRight-
203d0 3e 70 4c 65 66 74 3b 0a 20 20 70 52 69 67 68 74  >pLeft;.  pRight
203d1 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 41 6e  ->flags |= EP_An
203d2 79 41 66 66 3b 0a 20 20 2a 70 52 65 67 52 69 67  yAff;.  *pRegRig
203d3 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ht = sqlite3Expr
203d4 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c  CodeTemp(pParse,
203d5 20 70 52 69 67 68 74 2c 20 70 46 72 65 65 52 69   pRight, pFreeRi
203d6 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  ght);.}../*.** G
203d7 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
203d8 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70   a comparison op
203d9 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69  erator..*/.stati
203da 63 20 69 6e 74 20 63 6f 64 65 43 6f 6d 70 61 72  c int codeCompar
203db 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
203dc 73 65 2c 20 20 20 20 2f 2a 20 54 68 65 20 70 61  se,    /* The pa
203dd 72 73 69 6e 67 20 28 61 6e 64 20 63 6f 64 65 20  rsing (and code 
203de 67 65 6e 65 72 61 74 69 6e 67 29 20 63 6f 6e 74  generating) cont
203df 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ext */.  Expr *p
203e0 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a 20 54 68  Left,      /* Th
203e1 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a  e left operand *
203e2 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74  /.  Expr *pRight
203e3 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67  ,     /* The rig
203e4 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  ht operand */.  
203e5 69 6e 74 20 6f 70 63 6f 64 65 2c 20 20 20 20 20  int opcode,     
203e6 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 61 72 69    /* The compari
203e7 73 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20  son opcode */.  
203e8 69 6e 74 20 69 6e 31 2c 20 69 6e 74 20 69 6e 32  int in1, int in2
203e9 2c 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f  , /* Register ho
203ea 6c 64 69 6e 67 20 6f 70 65 72 61 6e 64 73 20 2a  lding operands *
203eb 2f 0a 20 20 69 6e 74 20 64 65 73 74 2c 20 20 20  /.  int dest,   
203ec 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65        /* Jump he
203ed 72 65 20 69 66 20 74 72 75 65 2e 20 20 2a 2f 0a  re if true.  */.
203ee 20 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c    int jumpIfNull
203ef 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20      /* If true, 
203f0 6a 75 6d 70 20 69 66 20 65 69 74 68 65 72 20 6f  jump if either o
203f1 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 2a  perand is NULL *
203f2 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 35 3b 0a 20  /.){.  int p5;. 
203f3 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 43 6f 6c   int addr;.  Col
203f4 6c 53 65 71 20 2a 70 34 3b 0a 0a 20 20 70 34 20  lSeq *p4;..  p4 
203f5 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43  = sqlite3BinaryC
203f6 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50  ompareCollSeq(pP
203f7 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 69  arse, pLeft, pRi
203f8 67 68 74 29 3b 0a 20 20 70 35 20 3d 20 62 69 6e  ght);.  p5 = bin
203f9 61 72 79 43 6f 6d 70 61 72 65 50 35 28 70 4c 65  aryCompareP5(pLe
203fa 66 74 2c 20 70 52 69 67 68 74 2c 20 6a 75 6d 70  ft, pRight, jump
203fb 49 66 4e 75 6c 6c 29 3b 0a 20 20 61 64 64 72 20  IfNull);.  addr 
203fc 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
203fd 4f 70 34 28 70 50 61 72 73 65 2d 3e 70 56 64 62  Op4(pParse->pVdb
203fe 65 2c 20 6f 70 63 6f 64 65 2c 20 69 6e 32 2c 20  e, opcode, in2, 
203ff 64 65 73 74 2c 20 69 6e 31 2c 0a 20 20 20 20 20  dest, in1,.     
20400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20401 20 20 20 20 20 20 28 76 6f 69 64 2a 29 70 34 2c        (void*)p4,
20402 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 20   P4_COLLSEQ);.  
20403 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
20404 65 50 35 28 70 50 61 72 73 65 2d 3e 70 56 64 62  eP5(pParse->pVdb
20405 65 2c 20 28 75 38 29 70 35 29 3b 0a 20 20 69 66  e, (u8)p5);.  if
20406 28 20 28 70 35 20 26 20 53 51 4c 49 54 45 5f 41  ( (p5 & SQLITE_A
20407 46 46 5f 4d 41 53 4b 29 21 3d 53 51 4c 49 54 45  FF_MASK)!=SQLITE
20408 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20  _AFF_NONE ){.   
20409 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
2040a 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28  eAffinityChange(
2040b 70 50 61 72 73 65 2c 20 69 6e 31 2c 20 31 29 3b  pParse, in1, 1);
2040c 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2040d 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
2040e 6e 67 65 28 70 50 61 72 73 65 2c 20 69 6e 32 2c  nge(pParse, in2,
2040f 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   1);.  }.  retur
20410 6e 20 61 64 64 72 3b 0a 7d 0a 0a 23 69 66 20 53  n addr;.}..#if S
20411 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44  QLITE_MAX_EXPR_D
20412 45 50 54 48 3e 30 0a 2f 2a 0a 2a 2a 20 43 68 65  EPTH>0./*.** Che
20413 63 6b 20 74 68 61 74 20 61 72 67 75 6d 65 6e 74  ck that argument
20414 20 6e 48 65 69 67 68 74 20 69 73 20 6c 65 73 73   nHeight is less
20415 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
20416 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a  o the maximum.**
20417 20 65 78 70 72 65 73 73 69 6f 6e 20 64 65 70 74   expression dept
20418 68 20 61 6c 6c 6f 77 65 64 2e 20 49 66 20 69 74  h allowed. If it
20419 20 69 73 20 6e 6f 74 2c 20 6c 65 61 76 65 20 61   is not, leave a
2041a 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
2041b 69 6e 0a 2a 2a 20 70 50 61 72 73 65 2e 0a 2a 2f  in.** pParse..*/
2041c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2041d 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43  int sqlite3ExprC
2041e 68 65 63 6b 48 65 69 67 68 74 28 50 61 72 73 65  heckHeight(Parse
2041f 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6e 48   *pParse, int nH
20420 65 69 67 68 74 29 7b 0a 20 20 69 6e 74 20 72 63  eight){.  int rc
20421 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
20422 69 6e 74 20 6d 78 48 65 69 67 68 74 20 3d 20 70  int mxHeight = p
20423 50 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d 69  Parse->db->aLimi
20424 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45  t[SQLITE_LIMIT_E
20425 58 50 52 5f 44 45 50 54 48 5d 3b 0a 20 20 69 66  XPR_DEPTH];.  if
20426 28 20 6e 48 65 69 67 68 74 3e 6d 78 48 65 69 67  ( nHeight>mxHeig
20427 68 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ht ){.    sqlite
20428 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
20429 2c 20 0a 20 20 20 20 20 20 20 22 45 78 70 72 65  , .       "Expre
2042a 73 73 69 6f 6e 20 74 72 65 65 20 69 73 20 74 6f  ssion tree is to
2042b 6f 20 6c 61 72 67 65 20 28 6d 61 78 69 6d 75 6d  o large (maximum
2042c 20 64 65 70 74 68 20 25 64 29 22 2c 20 6d 78 48   depth %d)", mxH
2042d 65 69 67 68 74 0a 20 20 20 20 29 3b 0a 20 20 20  eight.    );.   
2042e 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
2042f 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  OR;.  }.  return
20430 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66   rc;.}../* The f
20431 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 66  ollowing three f
20432 75 6e 63 74 69 6f 6e 73 2c 20 68 65 69 67 68 74  unctions, height
20433 4f 66 45 78 70 72 28 29 2c 20 68 65 69 67 68 74  OfExpr(), height
20434 4f 66 45 78 70 72 4c 69 73 74 28 29 0a 2a 2a 20  OfExprList().** 
20435 61 6e 64 20 68 65 69 67 68 74 4f 66 53 65 6c 65  and heightOfSele
20436 63 74 28 29 2c 20 61 72 65 20 75 73 65 64 20 74  ct(), are used t
20437 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  o determine the 
20438 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 0a 2a  maximum height.*
20439 2a 20 6f 66 20 61 6e 79 20 65 78 70 72 65 73 73  * of any express
2043a 69 6f 6e 20 74 72 65 65 20 72 65 66 65 72 65 6e  ion tree referen
2043b 63 65 64 20 62 79 20 74 68 65 20 73 74 72 75 63  ced by the struc
2043c 74 75 72 65 20 70 61 73 73 65 64 20 61 73 20 74  ture passed as t
2043d 68 65 0a 2a 2a 20 66 69 72 73 74 20 61 72 67 75  he.** first argu
2043e 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ment..**.** If t
2043f 68 69 73 20 6d 61 78 69 6d 75 6d 20 68 65 69 67  his maximum heig
20440 68 74 20 69 73 20 67 72 65 61 74 65 72 20 74 68  ht is greater th
20441 61 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 76  an the current v
20442 61 6c 75 65 20 70 6f 69 6e 74 65 64 0a 2a 2a 20  alue pointed.** 
20443 74 6f 20 62 79 20 70 6e 48 65 69 67 68 74 2c 20  to by pnHeight, 
20444 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  the second param
20445 65 74 65 72 2c 20 74 68 65 6e 20 73 65 74 20 2a  eter, then set *
20446 70 6e 48 65 69 67 68 74 20 74 6f 20 74 68 61 74  pnHeight to that
20447 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74  .** value..*/.st
20448 61 74 69 63 20 76 6f 69 64 20 68 65 69 67 68 74  atic void height
20449 4f 66 45 78 70 72 28 45 78 70 72 20 2a 70 2c 20  OfExpr(Expr *p, 
2044a 69 6e 74 20 2a 70 6e 48 65 69 67 68 74 29 7b 0a  int *pnHeight){.
2044b 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69    if( p ){.    i
2044c 66 28 20 70 2d 3e 6e 48 65 69 67 68 74 3e 2a 70  f( p->nHeight>*p
2044d 6e 48 65 69 67 68 74 20 29 7b 0a 20 20 20 20 20  nHeight ){.     
2044e 20 2a 70 6e 48 65 69 67 68 74 20 3d 20 70 2d 3e   *pnHeight = p->
2044f 6e 48 65 69 67 68 74 3b 0a 20 20 20 20 7d 0a 20  nHeight;.    }. 
20450 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64   }.}.static void
20451 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73   heightOfExprLis
20452 74 28 45 78 70 72 4c 69 73 74 20 2a 70 2c 20 69  t(ExprList *p, i
20453 6e 74 20 2a 70 6e 48 65 69 67 68 74 29 7b 0a 20  nt *pnHeight){. 
20454 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 6e   if( p ){.    in
20455 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  t i;.    for(i=0
20456 3b 20 69 3c 70 2d 3e 6e 45 78 70 72 3b 20 69 2b  ; i<p->nExpr; i+
20457 2b 29 7b 0a 20 20 20 20 20 20 68 65 69 67 68 74  +){.      height
20458 4f 66 45 78 70 72 28 70 2d 3e 61 5b 69 5d 2e 70  OfExpr(p->a[i].p
20459 45 78 70 72 2c 20 70 6e 48 65 69 67 68 74 29 3b  Expr, pnHeight);
2045a 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61  .    }.  }.}.sta
2045b 74 69 63 20 76 6f 69 64 20 68 65 69 67 68 74 4f  tic void heightO
2045c 66 53 65 6c 65 63 74 28 53 65 6c 65 63 74 20 2a  fSelect(Select *
2045d 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 68 74  p, int *pnHeight
2045e 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  ){.  if( p ){.  
2045f 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70    heightOfExpr(p
20460 2d 3e 70 57 68 65 72 65 2c 20 70 6e 48 65 69 67  ->pWhere, pnHeig
20461 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f  ht);.    heightO
20462 66 45 78 70 72 28 70 2d 3e 70 48 61 76 69 6e 67  fExpr(p->pHaving
20463 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20  , pnHeight);.   
20464 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d   heightOfExpr(p-
20465 3e 70 4c 69 6d 69 74 2c 20 70 6e 48 65 69 67 68  >pLimit, pnHeigh
20466 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66  t);.    heightOf
20467 45 78 70 72 28 70 2d 3e 70 4f 66 66 73 65 74 2c  Expr(p->pOffset,
20468 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20   pnHeight);.    
20469 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74  heightOfExprList
2046a 28 70 2d 3e 70 45 4c 69 73 74 2c 20 70 6e 48 65  (p->pEList, pnHe
2046b 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68  ight);.    heigh
2046c 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d 3e 70  tOfExprList(p->p
2046d 47 72 6f 75 70 42 79 2c 20 70 6e 48 65 69 67 68  GroupBy, pnHeigh
2046e 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66  t);.    heightOf
2046f 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 4f 72 64  ExprList(p->pOrd
20470 65 72 42 79 2c 20 70 6e 48 65 69 67 68 74 29 3b  erBy, pnHeight);
20471 0a 20 20 20 20 68 65 69 67 68 74 4f 66 53 65 6c  .    heightOfSel
20472 65 63 74 28 70 2d 3e 70 50 72 69 6f 72 2c 20 70  ect(p->pPrior, p
20473 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a 7d 0a  nHeight);.  }.}.
20474 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45  ./*.** Set the E
20475 78 70 72 2e 6e 48 65 69 67 68 74 20 76 61 72 69  xpr.nHeight vari
20476 61 62 6c 65 20 69 6e 20 74 68 65 20 73 74 72 75  able in the stru
20477 63 74 75 72 65 20 70 61 73 73 65 64 20 61 73 20  cture passed as 
20478 61 6e 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e  an .** argument.
20479 20 41 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 77   An expression w
2047a 69 74 68 20 6e 6f 20 63 68 69 6c 64 72 65 6e 2c  ith no children,
2047b 20 45 78 70 72 2e 70 4c 69 73 74 20 6f 72 20 0a   Expr.pList or .
2047c 2a 2a 20 45 78 70 72 2e 70 53 65 6c 65 63 74 20  ** Expr.pSelect 
2047d 6d 65 6d 62 65 72 20 68 61 73 20 61 20 68 65 69  member has a hei
2047e 67 68 74 20 6f 66 20 31 2e 20 41 6e 79 20 6f 74  ght of 1. Any ot
2047f 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a  her expression.*
20480 2a 20 68 61 73 20 61 20 68 65 69 67 68 74 20 65  * has a height e
20481 71 75 61 6c 20 74 6f 20 74 68 65 20 6d 61 78 69  qual to the maxi
20482 6d 75 6d 20 68 65 69 67 68 74 20 6f 66 20 61 6e  mum height of an
20483 79 20 6f 74 68 65 72 20 0a 2a 2a 20 72 65 66 65  y other .** refe
20484 72 65 6e 63 65 64 20 45 78 70 72 20 70 6c 75 73  renced Expr plus
20485 20 6f 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   one..*/.static 
20486 76 6f 69 64 20 65 78 70 72 53 65 74 48 65 69 67  void exprSetHeig
20487 68 74 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 69  ht(Expr *p){.  i
20488 6e 74 20 6e 48 65 69 67 68 74 20 3d 20 30 3b 0a  nt nHeight = 0;.
20489 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70    heightOfExpr(p
2048a 2d 3e 70 4c 65 66 74 2c 20 26 6e 48 65 69 67 68  ->pLeft, &nHeigh
2048b 74 29 3b 0a 20 20 68 65 69 67 68 74 4f 66 45 78  t);.  heightOfEx
2048c 70 72 28 70 2d 3e 70 52 69 67 68 74 2c 20 26 6e  pr(p->pRight, &n
2048d 48 65 69 67 68 74 29 3b 0a 20 20 69 66 28 20 45  Height);.  if( E
2048e 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
2048f 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
20490 29 7b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 53  ){.    heightOfS
20491 65 6c 65 63 74 28 70 2d 3e 78 2e 70 53 65 6c 65  elect(p->x.pSele
20492 63 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20  ct, &nHeight);. 
20493 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 65 69 67   }else{.    heig
20494 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d 3e  htOfExprList(p->
20495 78 2e 70 4c 69 73 74 2c 20 26 6e 48 65 69 67 68  x.pList, &nHeigh
20496 74 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6e 48 65  t);.  }.  p->nHe
20497 69 67 68 74 20 3d 20 6e 48 65 69 67 68 74 20 2b  ight = nHeight +
20498 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74   1;.}../*.** Set
20499 20 74 68 65 20 45 78 70 72 2e 6e 48 65 69 67 68   the Expr.nHeigh
2049a 74 20 76 61 72 69 61 62 6c 65 20 75 73 69 6e 67  t variable using
2049b 20 74 68 65 20 65 78 70 72 53 65 74 48 65 69 67   the exprSetHeig
2049c 68 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 49  ht() function. I
2049d 66 0a 2a 2a 20 74 68 65 20 68 65 69 67 68 74 20  f.** the height 
2049e 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
2049f 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f  the maximum allo
204a0 77 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 64  wed expression d
204a1 65 70 74 68 2c 0a 2a 2a 20 6c 65 61 76 65 20 61  epth,.** leave a
204a2 6e 20 65 72 72 6f 72 20 69 6e 20 70 50 61 72 73  n error in pPars
204a3 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
204a4 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
204a5 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 50  3ExprSetHeight(P
204a6 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
204a7 70 72 20 2a 70 29 7b 0a 20 20 65 78 70 72 53 65  pr *p){.  exprSe
204a8 74 48 65 69 67 68 74 28 70 29 3b 0a 20 20 73 71  tHeight(p);.  sq
204a9 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b 48 65  lite3ExprCheckHe
204aa 69 67 68 74 28 70 50 61 72 73 65 2c 20 70 2d 3e  ight(pParse, p->
204ab 6e 48 65 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a  nHeight);.}../*.
204ac 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d 61  ** Return the ma
204ad 78 69 6d 75 6d 20 68 65 69 67 68 74 20 6f 66 20  ximum height of 
204ae 61 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74  any expression t
204af 72 65 65 20 72 65 66 65 72 65 6e 63 65 64 0a 2a  ree referenced.*
204b0 2a 20 62 79 20 74 68 65 20 73 65 6c 65 63 74 20  * by the select 
204b1 73 74 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64  statement passed
204b2 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e   as an argument.
204b3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
204b4 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65  TE int sqlite3Se
204b5 6c 65 63 74 45 78 70 72 48 65 69 67 68 74 28 53  lectExprHeight(S
204b6 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 69 6e 74  elect *p){.  int
204b7 20 6e 48 65 69 67 68 74 20 3d 20 30 3b 0a 20 20   nHeight = 0;.  
204b8 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70  heightOfSelect(p
204b9 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 72  , &nHeight);.  r
204ba 65 74 75 72 6e 20 6e 48 65 69 67 68 74 3b 0a 7d  eturn nHeight;.}
204bb 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65  .#else.  #define
204bc 20 65 78 70 72 53 65 74 48 65 69 67 68 74 28 79   exprSetHeight(y
204bd 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ).#endif /* SQLI
204be 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54  TE_MAX_EXPR_DEPT
204bf 48 3e 30 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  H>0 */../*.** Th
204c0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
204c1 65 20 63 6f 72 65 20 61 6c 6c 6f 63 61 74 6f 72  e core allocator
204c2 20 66 6f 72 20 45 78 70 72 20 6e 6f 64 65 73 2e   for Expr nodes.
204c3 0a 2a 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74  .**.** Construct
204c4 20 61 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f   a new expressio
204c5 6e 20 6e 6f 64 65 20 61 6e 64 20 72 65 74 75 72  n node and retur
204c6 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  n a pointer to i
204c7 74 2e 20 20 4d 65 6d 6f 72 79 0a 2a 2a 20 66 6f  t.  Memory.** fo
204c8 72 20 74 68 69 73 20 6e 6f 64 65 20 61 6e 64 20  r this node and 
204c9 66 6f 72 20 74 68 65 20 70 54 6f 6b 65 6e 20 61  for the pToken a
204ca 72 67 75 6d 65 6e 74 20 69 73 20 61 20 73 69 6e  rgument is a sin
204cb 67 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a  gle allocation.*
204cc 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  * obtained from 
204cd 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 28  sqlite3DbMalloc(
204ce 29 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20  ).  The calling 
204cf 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 72  function.** is r
204d0 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 6d  esponsible for m
204d1 61 6b 69 6e 67 20 73 75 72 65 20 74 68 65 20 6e  aking sure the n
204d2 6f 64 65 20 65 76 65 6e 74 75 61 6c 6c 79 20 67  ode eventually g
204d3 65 74 73 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a  ets freed..**.**
204d4 20 49 66 20 64 65 71 75 6f 74 65 20 69 73 20 74   If dequote is t
204d5 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 74 6f  rue, then the to
204d6 6b 65 6e 20 28 69 66 20 69 74 20 65 78 69 73 74  ken (if it exist
204d7 73 29 20 69 73 20 64 65 71 75 6f 74 65 64 2e 0a  s) is dequoted..
204d8 2a 2a 20 49 66 20 64 65 71 75 6f 74 65 20 69 73  ** If dequote is
204d9 20 66 61 6c 73 65 2c 20 6e 6f 20 64 65 71 75 6f   false, no dequo
204da 74 69 6e 67 20 69 73 20 70 65 72 66 6f 72 6d 61  ting is performa
204db 6e 63 65 2e 20 20 54 68 65 20 64 65 51 75 6f 74  nce.  The deQuot
204dc 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69  e.** parameter i
204dd 73 20 69 67 6e 6f 72 65 64 20 69 66 20 70 54 6f  s ignored if pTo
204de 6b 65 6e 20 69 73 20 4e 55 4c 4c 20 6f 72 20 69  ken is NULL or i
204df 66 20 74 68 65 20 74 6f 6b 65 6e 20 64 6f 65 73  f the token does
204e0 20 6e 6f 74 0a 2a 2a 20 61 70 70 65 61 72 20 74   not.** appear t
204e1 6f 20 62 65 20 71 75 6f 74 65 64 2e 20 20 49 66  o be quoted.  If
204e2 20 74 68 65 20 71 75 6f 74 65 73 20 77 65 72 65   the quotes were
204e3 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 2e 2e   of the form "..
204e4 2e 22 20 28 64 6f 75 62 6c 65 2d 71 75 6f 74 65  ." (double-quote
204e5 73 29 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 45  s).** then the E
204e6 50 5f 44 62 6c 51 75 6f 74 65 64 20 66 6c 61 67  P_DblQuoted flag
204e7 20 69 73 20 73 65 74 20 6f 6e 20 74 68 65 20 65   is set on the e
204e8 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 2e 0a  xpression node..
204e9 2a 2a 0a 2a 2a 20 53 70 65 63 69 61 6c 20 63 61  **.** Special ca
204ea 73 65 3a 20 20 49 66 20 6f 70 3d 3d 54 4b 5f 49  se:  If op==TK_I
204eb 4e 54 45 47 45 52 20 61 6e 64 20 70 54 6f 6b 65  NTEGER and pToke
204ec 6e 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 74  n points to a st
204ed 72 69 6e 67 20 74 68 61 74 0a 2a 2a 20 63 61 6e  ring that.** can
204ee 20 62 65 20 74 72 61 6e 73 6c 61 74 65 64 20 69   be translated i
204ef 6e 74 6f 20 61 20 33 32 2d 62 69 74 20 69 6e 74  nto a 32-bit int
204f0 65 67 65 72 2c 20 74 68 65 6e 20 74 68 65 20 74  eger, then the t
204f1 6f 6b 65 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20 73  oken is not.** s
204f2 74 6f 72 65 64 20 69 6e 20 75 2e 7a 54 6f 6b 65  tored in u.zToke
204f3 6e 2e 20 20 49 6e 73 74 65 61 64 2c 20 74 68 65  n.  Instead, the
204f4 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20   integer values 
204f5 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69 6e  is written.** in
204f6 74 6f 20 75 2e 69 56 61 6c 75 65 20 61 6e 64 20  to u.iValue and 
204f7 74 68 65 20 45 50 5f 49 6e 74 56 61 6c 75 65 20  the EP_IntValue 
204f8 66 6c 61 67 20 69 73 20 73 65 74 2e 20 20 4e 6f  flag is set.  No
204f9 20 65 78 74 72 61 20 73 74 6f 72 61 67 65 0a 2a   extra storage.*
204fa 2a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 74  * is allocated t
204fb 6f 20 68 6f 6c 64 20 74 68 65 20 69 6e 74 65 67  o hold the integ
204fc 65 72 20 74 65 78 74 20 61 6e 64 20 74 68 65 20  er text and the 
204fd 64 65 71 75 6f 74 65 20 66 6c 61 67 20 69 73 20  dequote flag is 
204fe 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49  ignored..*/.SQLI
204ff 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
20500 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f  *sqlite3ExprAllo
20501 63 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  c(.  sqlite3 *db
20502 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
20503 48 61 6e 64 6c 65 20 66 6f 72 20 73 71 6c 69 74  Handle for sqlit
20504 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 29  e3DbMallocZero()
20505 20 28 6d 61 79 20 62 65 20 6e 75 6c 6c 29 20 2a   (may be null) *
20506 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20  /.  int op,     
20507 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
20508 78 70 72 65 73 73 69 6f 6e 20 6f 70 63 6f 64 65  xpression opcode
20509 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65   */.  const Toke
2050a 6e 20 2a 70 54 6f 6b 65 6e 2c 20 20 20 20 2f 2a  n *pToken,    /*
2050b 20 54 6f 6b 65 6e 20 61 72 67 75 6d 65 6e 74 2e   Token argument.
2050c 20 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20    Might be NULL 
2050d 2a 2f 0a 20 20 69 6e 74 20 64 65 71 75 6f 74 65  */.  int dequote
2050e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2050f 54 72 75 65 20 74 6f 20 64 65 71 75 6f 74 65 20  True to dequote 
20510 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e  */.){.  Expr *pN
20511 65 77 3b 0a 20 20 69 6e 74 20 6e 45 78 74 72 61  ew;.  int nExtra
20512 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 56 61 6c   = 0;.  int iVal
20513 75 65 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70  ue = 0;..  if( p
20514 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 69 66 28  Token ){.    if(
20515 20 6f 70 21 3d 54 4b 5f 49 4e 54 45 47 45 52 20   op!=TK_INTEGER 
20516 7c 7c 20 70 54 6f 6b 65 6e 2d 3e 7a 3d 3d 30 0a  || pToken->z==0.
20517 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c            || sql
20518 69 74 65 33 47 65 74 49 6e 74 33 32 28 70 54 6f  ite3GetInt32(pTo
20519 6b 65 6e 2d 3e 7a 2c 20 26 69 56 61 6c 75 65 29  ken->z, &iValue)
2051a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 45 78  ==0 ){.      nEx
2051b 74 72 61 20 3d 20 70 54 6f 6b 65 6e 2d 3e 6e 2b  tra = pToken->n+
2051c 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  1;.    }.  }.  p
2051d 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  New = sqlite3DbM
2051e 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
2051f 7a 65 6f 66 28 45 78 70 72 29 2b 6e 45 78 74 72  zeof(Expr)+nExtr
20520 61 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 29  a);.  if( pNew )
20521 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6f 70 20 3d  {.    pNew->op =
20522 20 28 75 38 29 6f 70 3b 0a 20 20 20 20 70 4e 65   (u8)op;.    pNe
20523 77 2d 3e 69 41 67 67 20 3d 20 2d 31 3b 0a 20 20  w->iAgg = -1;.  
20524 20 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a    if( pToken ){.
20525 20 20 20 20 20 20 69 66 28 20 6e 45 78 74 72 61        if( nExtra
20526 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ==0 ){.        p
20527 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50  New->flags |= EP
20528 5f 49 6e 74 56 61 6c 75 65 3b 0a 20 20 20 20 20  _IntValue;.     
20529 20 20 20 70 4e 65 77 2d 3e 75 2e 69 56 61 6c 75     pNew->u.iValu
2052a 65 20 3d 20 69 56 61 6c 75 65 3b 0a 20 20 20 20  e = iValue;.    
2052b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2052c 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 20 20 20   int c;.        
2052d 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d  pNew->u.zToken =
2052e 20 28 63 68 61 72 2a 29 26 70 4e 65 77 5b 31 5d   (char*)&pNew[1]
2052f 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  ;.        memcpy
20530 28 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c  (pNew->u.zToken,
20531 20 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 70 54 6f 6b   pToken->z, pTok
20532 65 6e 2d 3e 6e 29 3b 0a 20 20 20 20 20 20 20 20  en->n);.        
20533 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 70  pNew->u.zToken[p
20534 54 6f 6b 65 6e 2d 3e 6e 5d 20 3d 20 30 3b 0a 20  Token->n] = 0;. 
20535 20 20 20 20 20 20 20 69 66 28 20 64 65 71 75 6f         if( dequo
20536 74 65 20 26 26 20 6e 45 78 74 72 61 3e 3d 33 20  te && nExtra>=3 
20537 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26  .             &&
20538 20 28 28 63 20 3d 20 70 54 6f 6b 65 6e 2d 3e 7a   ((c = pToken->z
20539 5b 30 5d 29 3d 3d 27 5c 27 27 20 7c 7c 20 63 3d  [0])=='\'' || c=
2053a 3d 27 22 27 20 7c 7c 20 63 3d 3d 27 5b 27 20 7c  ='"' || c=='[' |
2053b 7c 20 63 3d 3d 27 60 27 29 20 29 7b 0a 20 20 20  | c=='`') ){.   
2053c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65         sqlite3De
2053d 71 75 6f 74 65 28 70 4e 65 77 2d 3e 75 2e 7a 54  quote(pNew->u.zT
2053e 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 20  oken);.         
2053f 20 69 66 28 20 63 3d 3d 27 22 27 20 29 20 70 4e   if( c=='"' ) pN
20540 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f  ew->flags |= EP_
20541 44 62 6c 51 75 6f 74 65 64 3b 0a 20 20 20 20 20  DblQuoted;.     
20542 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
20543 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41   }.#if SQLITE_MA
20544 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20  X_EXPR_DEPTH>0. 
20545 20 20 20 70 4e 65 77 2d 3e 6e 48 65 69 67 68 74     pNew->nHeight
20546 20 3d 20 31 3b 0a 23 65 6e 64 69 66 20 20 0a 20   = 1;.#endif  . 
20547 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77   }.  return pNew
20548 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  ;.}../*.** Alloc
20549 61 74 65 20 61 20 6e 65 77 20 65 78 70 72 65 73  ate a new expres
2054a 73 69 6f 6e 20 6e 6f 64 65 20 66 72 6f 6d 20 61  sion node from a
2054b 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
2054c 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 61 73 0a   token that has.
2054d 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ** already been 
2054e 64 65 71 75 6f 74 65 64 2e 0a 2a 2f 0a 53 51 4c  dequoted..*/.SQL
2054f 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
20550 20 2a 73 71 6c 69 74 65 33 45 78 70 72 28 0a 20   *sqlite3Expr(. 
20551 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
20552 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64           /* Hand
20553 6c 65 20 66 6f 72 20 73 71 6c 69 74 65 33 44 62  le for sqlite3Db
20554 4d 61 6c 6c 6f 63 5a 65 72 6f 28 29 20 28 6d 61  MallocZero() (ma
20555 79 20 62 65 20 6e 75 6c 6c 29 20 2a 2f 0a 20 20  y be null) */.  
20556 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20  int op,         
20557 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65          /* Expre
20558 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a  ssion opcode */.
20559 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
2055a 6f 6b 65 6e 20 20 20 20 20 20 2f 2a 20 54 6f 6b  oken      /* Tok
2055b 65 6e 20 61 72 67 75 6d 65 6e 74 2e 20 20 4d 69  en argument.  Mi
2055c 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 29  ght be NULL */.)
2055d 7b 0a 20 20 54 6f 6b 65 6e 20 78 3b 0a 20 20 78  {.  Token x;.  x
2055e 2e 7a 20 3d 20 7a 54 6f 6b 65 6e 3b 0a 20 20 78  .z = zToken;.  x
2055f 2e 6e 20 3d 20 7a 54 6f 6b 65 6e 20 3f 20 73 71  .n = zToken ? sq
20560 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54  lite3Strlen30(zT
20561 6f 6b 65 6e 29 20 3a 20 30 3b 0a 20 20 72 65 74  oken) : 0;.  ret
20562 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72 41  urn sqlite3ExprA
20563 6c 6c 6f 63 28 64 62 2c 20 6f 70 2c 20 26 78 2c  lloc(db, op, &x,
20564 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74   0);.}../*.** At
20565 74 61 63 68 20 73 75 62 74 72 65 65 73 20 70 4c  tach subtrees pL
20566 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 20 74  eft and pRight t
20567 6f 20 74 68 65 20 45 78 70 72 20 6e 6f 64 65 20  o the Expr node 
20568 70 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  pRoot..**.** If 
20569 70 52 6f 6f 74 3d 3d 4e 55 4c 4c 20 74 68 61 74  pRoot==NULL that
2056a 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 65   means that a me
2056b 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
2056c 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72  error has occurr
2056d 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 61 74 20 63  ed..** In that c
2056e 61 73 65 2c 20 64 65 6c 65 74 65 20 74 68 65 20  ase, delete the 
2056f 73 75 62 74 72 65 65 73 20 70 4c 65 66 74 20 61  subtrees pLeft a
20570 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2f 0a 53 51  nd pRight..*/.SQ
20571 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
20572 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 74 74  d sqlite3ExprAtt
20573 61 63 68 53 75 62 74 72 65 65 73 28 0a 20 20 73  achSubtrees(.  s
20574 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 45 78  qlite3 *db,.  Ex
20575 70 72 20 2a 70 52 6f 6f 74 2c 0a 20 20 45 78 70  pr *pRoot,.  Exp
20576 72 20 2a 70 4c 65 66 74 2c 0a 20 20 45 78 70 72  r *pLeft,.  Expr
20577 20 2a 70 52 69 67 68 74 0a 29 7b 0a 20 20 69 66   *pRight.){.  if
20578 28 20 70 52 6f 6f 74 3d 3d 30 20 29 7b 0a 20 20  ( pRoot==0 ){.  
20579 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61    assert( db->ma
2057a 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
2057b 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
2057c 65 74 65 28 64 62 2c 20 70 4c 65 66 74 29 3b 0a  ete(db, pLeft);.
2057d 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
2057e 65 6c 65 74 65 28 64 62 2c 20 70 52 69 67 68 74  elete(db, pRight
2057f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
20580 69 66 28 20 70 52 69 67 68 74 20 29 7b 0a 20 20  if( pRight ){.  
20581 20 20 20 20 70 52 6f 6f 74 2d 3e 70 52 69 67 68      pRoot->pRigh
20582 74 20 3d 20 70 52 69 67 68 74 3b 0a 20 20 20 20  t = pRight;.    
20583 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 66 6c    if( pRight->fl
20584 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c  ags & EP_ExpColl
20585 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ate ){.        p
20586 52 6f 6f 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45  Root->flags |= E
20587 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20  P_ExpCollate;.  
20588 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 70 43 6f        pRoot->pCo
20589 6c 6c 20 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f  ll = pRight->pCo
2058a 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ll;.      }.    
2058b 7d 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74 20  }.    if( pLeft 
2058c 29 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e  ){.      pRoot->
2058d 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b 0a 20  pLeft = pLeft;. 
2058e 20 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e       if( pLeft->
2058f 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f  flags & EP_ExpCo
20590 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20  llate ){.       
20591 20 70 52 6f 6f 74 2d 3e 66 6c 61 67 73 20 7c 3d   pRoot->flags |=
20592 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a   EP_ExpCollate;.
20593 20 20 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 70          pRoot->p
20594 43 6f 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e 70 43  Coll = pLeft->pC
20595 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  oll;.      }.   
20596 20 7d 0a 20 20 20 20 65 78 70 72 53 65 74 48 65   }.    exprSetHe
20597 69 67 68 74 28 70 52 6f 6f 74 29 3b 0a 20 20 7d  ight(pRoot);.  }
20598 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  .}../*.** Alloca
20599 74 65 20 61 20 45 78 70 72 20 6e 6f 64 65 20 77  te a Expr node w
2059a 68 69 63 68 20 6a 6f 69 6e 73 20 61 73 20 6d 61  hich joins as ma
2059b 6e 79 20 61 73 20 74 77 6f 20 73 75 62 74 72 65  ny as two subtre
2059c 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 6f 72  es..**.** One or
2059d 20 62 6f 74 68 20 6f 66 20 74 68 65 20 73 75 62   both of the sub
2059e 74 72 65 65 73 20 63 61 6e 20 62 65 20 4e 55 4c  trees can be NUL
2059f 4c 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69  L.  Return a poi
205a0 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 0a  nter to the new.
205a1 2a 2a 20 45 78 70 72 20 6e 6f 64 65 2e 20 20 4f  ** Expr node.  O
205a2 72 2c 20 69 66 20 61 6e 20 4f 4f 4d 20 65 72 72  r, if an OOM err
205a3 6f 72 20 6f 63 63 75 72 73 2c 20 73 65 74 20 70  or occurs, set p
205a4 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
205a5 63 46 61 69 6c 65 64 2c 0a 2a 2a 20 66 72 65 65  cFailed,.** free
205a6 20 74 68 65 20 73 75 62 74 72 65 65 73 20 61 6e   the subtrees an
205a7 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a  d return NULL..*
205a8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
205a9 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 50 45   Expr *sqlite3PE
205aa 78 70 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50  xpr(.  Parse *pP
205ab 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
205ac 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
205ad 74 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20  t */.  int op,  
205ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
205af 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6f 70 63  * Expression opc
205b0 6f 64 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ode */.  Expr *p
205b1 4c 65 66 74 2c 20 20 20 20 20 20 20 20 20 20 20  Left,           
205b2 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e 64   /* Left operand
205b3 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67   */.  Expr *pRig
205b4 68 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ht,           /*
205b5 20 52 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a   Right operand *
205b6 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20  /.  const Token 
205b7 2a 70 54 6f 6b 65 6e 20 20 20 20 20 2f 2a 20 41  *pToken     /* A
205b8 72 67 75 6d 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f  rgument token */
205b9 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20  .){.  Expr *p = 
205ba 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f 63  sqlite3ExprAlloc
205bb 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 6f 70 2c  (pParse->db, op,
205bc 20 70 54 6f 6b 65 6e 2c 20 31 29 3b 0a 20 20 73   pToken, 1);.  s
205bd 71 6c 69 74 65 33 45 78 70 72 41 74 74 61 63 68  qlite3ExprAttach
205be 53 75 62 74 72 65 65 73 28 70 50 61 72 73 65 2d  Subtrees(pParse-
205bf 3e 64 62 2c 20 70 2c 20 70 4c 65 66 74 2c 20 70  >db, p, pLeft, p
205c0 52 69 67 68 74 29 3b 0a 20 20 72 65 74 75 72 6e  Right);.  return
205c1 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4a 6f 69   p;.}../*.** Joi
205c2 6e 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e  n two expression
205c3 73 20 75 73 69 6e 67 20 61 6e 20 41 4e 44 20 6f  s using an AND o
205c4 70 65 72 61 74 6f 72 2e 20 20 49 66 20 65 69 74  perator.  If eit
205c5 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69  her expression i
205c6 73 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 6e 20  s.** NULL, then 
205c7 6a 75 73 74 20 72 65 74 75 72 6e 20 74 68 65 20  just return the 
205c8 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e  other expression
205c9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
205ca 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
205cb 33 45 78 70 72 41 6e 64 28 73 71 6c 69 74 65 33  3ExprAnd(sqlite3
205cc 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 4c 65 66   *db, Expr *pLef
205cd 74 2c 20 45 78 70 72 20 2a 70 52 69 67 68 74 29  t, Expr *pRight)
205ce 7b 0a 20 20 69 66 28 20 70 4c 65 66 74 3d 3d 30  {.  if( pLeft==0
205cf 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70   ){.    return p
205d0 52 69 67 68 74 3b 0a 20 20 7d 65 6c 73 65 20 69  Right;.  }else i
205d1 66 28 20 70 52 69 67 68 74 3d 3d 30 20 29 7b 0a  f( pRight==0 ){.
205d2 20 20 20 20 72 65 74 75 72 6e 20 70 4c 65 66 74      return pLeft
205d3 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 45  ;.  }else{.    E
205d4 78 70 72 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69  xpr *pNew = sqli
205d5 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 64 62 2c  te3ExprAlloc(db,
205d6 20 54 4b 5f 41 4e 44 2c 20 30 2c 20 30 29 3b 0a   TK_AND, 0, 0);.
205d7 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 41      sqlite3ExprA
205d8 74 74 61 63 68 53 75 62 74 72 65 65 73 28 64 62  ttachSubtrees(db
205d9 2c 20 70 4e 65 77 2c 20 70 4c 65 66 74 2c 20 70  , pNew, pLeft, p
205da 52 69 67 68 74 29 3b 0a 20 20 20 20 72 65 74 75  Right);.    retu
205db 72 6e 20 70 4e 65 77 3b 0a 20 20 7d 0a 7d 0a 0a  rn pNew;.  }.}..
205dc 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20  /*.** Construct 
205dd 61 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e  a new expression
205de 20 6e 6f 64 65 20 66 6f 72 20 61 20 66 75 6e 63   node for a func
205df 74 69 6f 6e 20 77 69 74 68 20 6d 75 6c 74 69 70  tion with multip
205e0 6c 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 2e  le.** arguments.
205e1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
205e2 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
205e3 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 50 61 72  ExprFunction(Par
205e4 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
205e5 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 54 6f 6b  List *pList, Tok
205e6 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 45  en *pToken){.  E
205e7 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 73 71 6c  xpr *pNew;.  sql
205e8 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
205e9 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28  e->db;.  assert(
205ea 20 70 54 6f 6b 65 6e 20 29 3b 0a 20 20 70 4e 65   pToken );.  pNe
205eb 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  w = sqlite3ExprA
205ec 6c 6c 6f 63 28 64 62 2c 20 54 4b 5f 46 55 4e 43  lloc(db, TK_FUNC
205ed 54 49 4f 4e 2c 20 70 54 6f 6b 65 6e 2c 20 31 29  TION, pToken, 1)
205ee 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20  ;.  if( pNew==0 
205ef 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  ){.    sqlite3Ex
205f0 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
205f1 20 70 4c 69 73 74 29 3b 20 2f 2a 20 41 76 6f 69   pList); /* Avoi
205f2 64 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 77 68  d memory leak wh
205f3 65 6e 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20  en malloc fails 
205f4 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  */.    return 0;
205f5 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 78 2e 70  .  }.  pNew->x.p
205f6 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a 20 20  List = pList;.  
205f7 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
205f8 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20 45  Property(pNew, E
205f9 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a  P_xIsSelect) );.
205fa 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74    sqlite3ExprSet
205fb 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 70  Height(pParse, p
205fc 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  New);.  return p
205fd 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73  New;.}../*.** As
205fe 73 69 67 6e 20 61 20 76 61 72 69 61 62 6c 65 20  sign a variable 
205ff 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 65 78 70  number to an exp
20600 72 65 73 73 69 6f 6e 20 74 68 61 74 20 65 6e 63  ression that enc
20601 6f 64 65 73 20 61 20 77 69 6c 64 63 61 72 64 0a  odes a wildcard.
20602 2a 2a 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e  ** in the origin
20603 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  al SQL statement
20604 2e 20 20 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61  .  .**.** Wildca
20605 72 64 73 20 63 6f 6e 73 69 73 74 69 6e 67 20 6f  rds consisting o
20606 66 20 61 20 73 69 6e 67 6c 65 20 22 3f 22 20 61  f a single "?" a
20607 72 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 20  re assigned the 
20608 6e 65 78 74 20 73 65 71 75 65 6e 74 69 61 6c 0a  next sequential.
20609 2a 2a 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62  ** variable numb
2060a 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61  er..**.** Wildca
2060b 72 64 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  rds of the form 
2060c 22 3f 6e 6e 6e 22 20 61 72 65 20 61 73 73 69 67  "?nnn" are assig
2060d 6e 65 64 20 74 68 65 20 6e 75 6d 62 65 72 20 22  ned the number "
2060e 6e 6e 6e 22 2e 20 20 57 65 20 6d 61 6b 65 0a 2a  nnn".  We make.*
2060f 2a 20 73 75 72 65 20 22 6e 6e 6e 22 20 69 73 20  * sure "nnn" is 
20610 6e 6f 74 20 74 6f 6f 20 62 65 20 74 6f 20 61 76  not too be to av
20611 6f 69 64 20 61 20 64 65 6e 69 61 6c 20 6f 66 20  oid a denial of 
20612 73 65 72 76 69 63 65 20 61 74 74 61 63 6b 20 77  service attack w
20613 68 65 6e 0a 2a 2a 20 74 68 65 20 53 51 4c 20 73  hen.** the SQL s
20614 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 73 20 66  tatement comes f
20615 72 6f 6d 20 61 6e 20 65 78 74 65 72 6e 61 6c 20  rom an external 
20616 73 6f 75 72 63 65 2e 0a 2a 2a 0a 2a 2a 20 57 69  source..**.** Wi
20617 6c 64 63 61 72 64 73 20 6f 66 20 74 68 65 20 66  ldcards of the f
20618 6f 72 6d 20 22 3a 61 61 61 22 2c 20 22 40 61 61  orm ":aaa", "@aa
20619 61 22 2c 20 6f 72 20 22 24 61 61 61 22 20 61 72  a", or "$aaa" ar
2061a 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 20 73  e assigned the s
2061b 61 6d 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 61 73  ame number.** as
2061c 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 69 6e   the previous in
2061d 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73 61  stance of the sa
2061e 6d 65 20 77 69 6c 64 63 61 72 64 2e 20 20 4f 72  me wildcard.  Or
2061f 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20   if this is the 
20620 66 69 72 73 74 0a 2a 2a 20 69 6e 73 74 61 6e 63  first.** instanc
20621 65 20 6f 66 20 74 68 65 20 77 69 6c 64 63 61 72  e of the wildcar
20622 64 2c 20 74 68 65 20 6e 65 78 74 20 73 65 71 75  d, the next sequ
20623 65 6e 69 61 6c 20 76 61 72 69 61 62 6c 65 20 6e  enial variable n
20624 75 6d 62 65 72 20 69 73 0a 2a 2a 20 61 73 73 69  umber is.** assi
20625 67 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  gned..*/.SQLITE_
20626 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
20627 69 74 65 33 45 78 70 72 41 73 73 69 67 6e 56 61  ite3ExprAssignVa
20628 72 4e 75 6d 62 65 72 28 50 61 72 73 65 20 2a 70  rNumber(Parse *p
20629 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78  Parse, Expr *pEx
2062a 70 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  pr){.  sqlite3 *
2062b 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2062c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2062d 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d  ;..  if( pExpr==
2062e 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73  0 ) return;.  as
2062f 73 65 72 74 28 20 21 45 78 70 72 48 61 73 41 6e  sert( !ExprHasAn
20630 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  yProperty(pExpr,
20631 20 45 50 5f 49 6e 74 56 61 6c 75 65 7c 45 50 5f   EP_IntValue|EP_
20632 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e  Reduced|EP_Token
20633 4f 6e 6c 79 29 20 29 3b 0a 20 20 7a 20 3d 20 70  Only) );.  z = p
20634 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a  Expr->u.zToken;.
20635 20 20 61 73 73 65 72 74 28 20 7a 21 3d 30 20 29    assert( z!=0 )
20636 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 5b 30 5d  ;.  assert( z[0]
20637 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 7a 5b 31  !=0 );.  if( z[1
20638 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57  ]==0 ){.    /* W
20639 69 6c 64 63 61 72 64 20 6f 66 20 74 68 65 20 66  ildcard of the f
2063a 6f 72 6d 20 22 3f 22 2e 20 20 41 73 73 69 67 6e  orm "?".  Assign
2063b 20 74 68 65 20 6e 65 78 74 20 76 61 72 69 61 62   the next variab
2063c 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20  le number */.   
2063d 20 61 73 73 65 72 74 28 20 7a 5b 30 5d 3d 3d 27   assert( z[0]=='
2063e 3f 27 20 29 3b 0a 20 20 20 20 70 45 78 70 72 2d  ?' );.    pExpr-
2063f 3e 69 54 61 62 6c 65 20 3d 20 2b 2b 70 50 61 72  >iTable = ++pPar
20640 73 65 2d 3e 6e 56 61 72 3b 0a 20 20 7d 65 6c 73  se->nVar;.  }els
20641 65 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 3f 27 20  e if( z[0]=='?' 
20642 29 7b 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 61  ){.    /* Wildca
20643 72 64 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  rd of the form "
20644 3f 6e 6e 6e 22 2e 20 20 43 6f 6e 76 65 72 74 20  ?nnn".  Convert 
20645 22 6e 6e 6e 22 20 74 6f 20 61 6e 20 69 6e 74 65  "nnn" to an inte
20646 67 65 72 20 61 6e 64 0a 20 20 20 20 2a 2a 20 75  ger and.    ** u
20647 73 65 20 69 74 20 61 73 20 74 68 65 20 76 61 72  se it as the var
20648 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a  iable number */.
20649 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 70      int i;.    p
2064a 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 69  Expr->iTable = i
2064b 20 3d 20 61 74 6f 69 28 28 63 68 61 72 2a 29 26   = atoi((char*)&
2064c 7a 5b 31 5d 29 3b 0a 20 20 20 20 74 65 73 74 63  z[1]);.    testc
2064d 61 73 65 28 20 69 3d 3d 30 20 29 3b 0a 20 20 20  ase( i==0 );.   
2064e 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 20   testcase( i==1 
2064f 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
20650 20 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53   i==db->aLimit[S
20651 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49  QLITE_LIMIT_VARI
20652 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 2d 31 20 29  ABLE_NUMBER]-1 )
20653 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
20654 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  i==db->aLimit[SQ
20655 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41  LITE_LIMIT_VARIA
20656 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 3b 0a 20  BLE_NUMBER] );. 
20657 20 20 20 69 66 28 20 69 3c 31 20 7c 7c 20 69 3e     if( i<1 || i>
20658 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
20659 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45  E_LIMIT_VARIABLE
2065a 5f 4e 55 4d 42 45 52 5d 20 29 7b 0a 20 20 20 20  _NUMBER] ){.    
2065b 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2065c 67 28 70 50 61 72 73 65 2c 20 22 76 61 72 69 61  g(pParse, "varia
2065d 62 6c 65 20 6e 75 6d 62 65 72 20 6d 75 73 74 20  ble number must 
2065e 62 65 20 62 65 74 77 65 65 6e 20 3f 31 20 61 6e  be between ?1 an
2065f 64 20 3f 25 64 22 2c 0a 20 20 20 20 20 20 20 20  d ?%d",.        
20660 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c    db->aLimit[SQL
20661 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42  ITE_LIMIT_VARIAB
20662 4c 45 5f 4e 55 4d 42 45 52 5d 29 3b 0a 20 20 20  LE_NUMBER]);.   
20663 20 7d 0a 20 20 20 20 69 66 28 20 69 3e 70 50 61   }.    if( i>pPa
20664 72 73 65 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20  rse->nVar ){.   
20665 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 20     pParse->nVar 
20666 3d 20 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  = i;.    }.  }el
20667 73 65 7b 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63  se{.    /* Wildc
20668 61 72 64 73 20 6c 69 6b 65 20 22 3a 61 61 61 22  ards like ":aaa"
20669 2c 20 22 24 61 61 61 22 20 6f 72 20 22 40 61 61  , "$aaa" or "@aa
2066a 61 22 2e 20 20 52 65 75 73 65 20 74 68 65 20 73  a".  Reuse the s
2066b 61 6d 65 20 76 61 72 69 61 62 6c 65 0a 20 20 20  ame variable.   
2066c 20 2a 2a 20 6e 75 6d 62 65 72 20 61 73 20 74 68   ** number as th
2066d 65 20 70 72 69 6f 72 20 61 70 70 65 61 72 61 6e  e prior appearan
2066e 63 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e  ce of the same n
2066f 61 6d 65 2c 20 6f 72 20 69 66 20 74 68 65 20 6e  ame, or if the n
20670 61 6d 65 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e  ame.    ** has n
20671 65 76 65 72 20 61 70 70 65 61 72 65 64 20 62 65  ever appeared be
20672 66 6f 72 65 2c 20 72 65 75 73 65 20 74 68 65 20  fore, reuse the 
20673 73 61 6d 65 20 76 61 72 69 61 62 6c 65 20 6e 75  same variable nu
20674 6d 62 65 72 0a 20 20 20 20 2a 2f 0a 20 20 20 20  mber.    */.    
20675 69 6e 74 20 69 3b 0a 20 20 20 20 75 33 32 20 6e  int i;.    u32 n
20676 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65  ;.    n = sqlite
20677 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b 0a 20 20  3Strlen30(z);.  
20678 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61    for(i=0; i<pPa
20679 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 3b 20 69  rse->nVarExpr; i
2067a 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20  ++){.      Expr 
2067b 2a 70 45 20 3d 20 70 50 61 72 73 65 2d 3e 61 70  *pE = pParse->ap
2067c 56 61 72 45 78 70 72 5b 69 5d 3b 0a 20 20 20 20  VarExpr[i];.    
2067d 20 20 61 73 73 65 72 74 28 20 70 45 21 3d 30 20    assert( pE!=0 
2067e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d  );.      if( mem
2067f 63 6d 70 28 70 45 2d 3e 75 2e 7a 54 6f 6b 65 6e  cmp(pE->u.zToken
20680 2c 20 7a 2c 20 6e 29 3d 3d 30 20 26 26 20 70 45  , z, n)==0 && pE
20681 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 6e 5d 3d 3d 30  ->u.zToken[n]==0
20682 20 29 7b 0a 20 20 20 20 20 20 20 20 70 45 78 70   ){.        pExp
20683 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 2d 3e  r->iTable = pE->
20684 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 20 20  iTable;.        
20685 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
20686 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3e 3d     }.    if( i>=
20687 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72  pParse->nVarExpr
20688 20 29 7b 0a 20 20 20 20 20 20 70 45 78 70 72 2d   ){.      pExpr-
20689 3e 69 54 61 62 6c 65 20 3d 20 2b 2b 70 50 61 72  >iTable = ++pPar
2068a 73 65 2d 3e 6e 56 61 72 3b 0a 20 20 20 20 20 20  se->nVar;.      
2068b 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72  if( pParse->nVar
2068c 45 78 70 72 3e 3d 70 50 61 72 73 65 2d 3e 6e 56  Expr>=pParse->nV
2068d 61 72 45 78 70 72 41 6c 6c 6f 63 2d 31 20 29 7b  arExprAlloc-1 ){
2068e 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d  .        pParse-
2068f 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 20 2b  >nVarExprAlloc +
20690 3d 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78  = pParse->nVarEx
20691 70 72 41 6c 6c 6f 63 20 2b 20 31 30 3b 0a 20 20  prAlloc + 10;.  
20692 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70        pParse->ap
20693 56 61 72 45 78 70 72 20 3d 0a 20 20 20 20 20 20  VarExpr =.      
20694 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 52        sqlite3DbR
20695 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 0a 20 20  eallocOrFree(.  
20696 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2c 0a              db,.
20697 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50                pP
20698 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 2c  arse->apVarExpr,
20699 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
2069a 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41  Parse->nVarExprA
2069b 6c 6c 6f 63 2a 73 69 7a 65 6f 66 28 70 50 61 72  lloc*sizeof(pPar
2069c 73 65 2d 3e 61 70 56 61 72 45 78 70 72 5b 30 5d  se->apVarExpr[0]
2069d 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 29 3b  ).            );
2069e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
2069f 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  f( !db->mallocFa
206a0 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20  iled ){.        
206a1 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
206a2 61 70 56 61 72 45 78 70 72 21 3d 30 20 29 3b 0a  apVarExpr!=0 );.
206a3 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
206a4 61 70 56 61 72 45 78 70 72 5b 70 50 61 72 73 65  apVarExpr[pParse
206a5 2d 3e 6e 56 61 72 45 78 70 72 2b 2b 5d 20 3d 20  ->nVarExpr++] = 
206a6 70 45 78 70 72 3b 0a 20 20 20 20 20 20 7d 0a 20  pExpr;.      }. 
206a7 20 20 20 7d 0a 20 20 7d 20 0a 20 20 69 66 28 20     }.  } .  if( 
206a8 21 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 26 26  !pParse->nErr &&
206a9 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 3e 64 62   pParse->nVar>db
206aa 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
206ab 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e  LIMIT_VARIABLE_N
206ac 55 4d 42 45 52 5d 20 29 7b 0a 20 20 20 20 73 71  UMBER] ){.    sq
206ad 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
206ae 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20  arse, "too many 
206af 53 51 4c 20 76 61 72 69 61 62 6c 65 73 22 29 3b  SQL variables");
206b0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  .  }.}../*.** Cl
206b1 65 61 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f  ear an expressio
206b2 6e 20 73 74 72 75 63 74 75 72 65 20 77 69 74 68  n structure with
206b3 6f 75 74 20 64 65 6c 65 74 69 6e 67 20 74 68 65  out deleting the
206b4 20 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c   structure itsel
206b5 66 2e 0a 2a 2a 20 53 75 62 73 74 72 75 63 74 75  f..** Substructu
206b6 72 65 20 69 73 20 64 65 6c 65 74 65 64 2e 0a 2a  re is deleted..*
206b7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
206b8 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
206b9 72 43 6c 65 61 72 28 73 71 6c 69 74 65 33 20 2a  rClear(sqlite3 *
206ba 64 62 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20  db, Expr *p){.  
206bb 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a  assert( p!=0 );.
206bc 20 20 69 66 28 20 21 45 78 70 72 48 61 73 41 6e    if( !ExprHasAn
206bd 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f  yProperty(p, EP_
206be 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20  TokenOnly) ){.  
206bf 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
206c0 65 74 65 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74  ete(db, p->pLeft
206c1 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
206c2 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e  prDelete(db, p->
206c3 70 52 69 67 68 74 29 3b 0a 20 20 20 20 69 66 28  pRight);.    if(
206c4 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
206c5 79 28 70 2c 20 45 50 5f 52 65 64 75 63 65 64 29  y(p, EP_Reduced)
206c6 20 26 26 20 28 70 2d 3e 66 6c 61 67 73 32 20 26   && (p->flags2 &
206c7 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b   EP2_MallocedTok
206c8 65 6e 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  en)!=0 ){.      
206c9 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
206ca 2c 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a  , p->u.zToken);.
206cb 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 45 78      }.    if( Ex
206cc 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c  prHasProperty(p,
206cd 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
206ce 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  {.      sqlite3S
206cf 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
206d0 70 2d 3e 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20  p->x.pSelect);. 
206d1 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
206d2 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
206d3 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 78 2e 70  elete(db, p->x.p
206d4 4c 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  List);.    }.  }
206d5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 75 72 73  .}../*.** Recurs
206d6 69 76 65 6c 79 20 64 65 6c 65 74 65 20 61 6e 20  ively delete an 
206d7 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e  expression tree.
206d8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
206d9 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
206da 78 70 72 44 65 6c 65 74 65 28 73 71 6c 69 74 65  xprDelete(sqlite
206db 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 29 7b  3 *db, Expr *p){
206dc 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
206dd 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 45  turn;.  sqlite3E
206de 78 70 72 43 6c 65 61 72 28 64 62 2c 20 70 29 3b  xprClear(db, p);
206df 0a 20 20 69 66 28 20 21 45 78 70 72 48 61 73 50  .  if( !ExprHasP
206e0 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 53 74  roperty(p, EP_St
206e1 61 74 69 63 29 20 29 7b 0a 20 20 20 20 73 71 6c  atic) ){.    sql
206e2 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
206e3 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
206e4 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
206e5 72 20 6f 66 20 62 79 74 65 73 20 61 6c 6c 6f 63  r of bytes alloc
206e6 61 74 65 64 20 66 6f 72 20 74 68 65 20 65 78 70  ated for the exp
206e7 72 65 73 73 69 6f 6e 20 73 74 72 75 63 74 75 72  ression structur
206e8 65 20 0a 2a 2a 20 70 61 73 73 65 64 20 61 73 20  e .** passed as 
206e9 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
206ea 6e 74 2e 20 54 68 69 73 20 69 73 20 61 6c 77 61  nt. This is alwa
206eb 79 73 20 6f 6e 65 20 6f 66 20 45 58 50 52 5f 46  ys one of EXPR_F
206ec 55 4c 4c 53 49 5a 45 2c 0a 2a 2a 20 45 58 50 52  ULLSIZE,.** EXPR
206ed 5f 52 45 44 55 43 45 44 53 49 5a 45 20 6f 72 20  _REDUCEDSIZE or 
206ee 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49  EXPR_TOKENONLYSI
206ef 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ZE..*/.static in
206f0 74 20 65 78 70 72 53 74 72 75 63 74 53 69 7a 65  t exprStructSize
206f1 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 28  (Expr *p){.  if(
206f2 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
206f3 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79  (p, EP_TokenOnly
206f4 29 20 29 20 72 65 74 75 72 6e 20 45 58 50 52 5f  ) ) return EXPR_
206f5 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 3b 0a 20  TOKENONLYSIZE;. 
206f6 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70   if( ExprHasProp
206f7 65 72 74 79 28 70 2c 20 45 50 5f 52 65 64 75 63  erty(p, EP_Reduc
206f8 65 64 29 20 29 20 72 65 74 75 72 6e 20 45 58 50  ed) ) return EXP
206f9 52 5f 52 45 44 55 43 45 44 53 49 5a 45 3b 0a 20  R_REDUCEDSIZE;. 
206fa 20 72 65 74 75 72 6e 20 45 58 50 52 5f 46 55 4c   return EXPR_FUL
206fb 4c 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  LSIZE;.}../*.** 
206fc 54 68 65 20 64 75 70 65 64 45 78 70 72 2a 53 69  The dupedExpr*Si
206fd 7a 65 28 29 20 72 6f 75 74 69 6e 65 73 20 65 61  ze() routines ea
206fe 63 68 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75  ch return the nu
206ff 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65  mber of bytes re
20700 71 75 69 72 65 64 0a 2a 2a 20 74 6f 20 73 74 6f  quired.** to sto
20701 72 65 20 61 20 63 6f 70 79 20 6f 66 20 61 6e 20  re a copy of an 
20702 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 65 78  expression or ex
20703 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20  pression tree.  
20704 54 68 65 79 20 64 69 66 66 65 72 20 69 6e 0a 2a  They differ in.*
20705 2a 20 68 6f 77 20 6d 75 63 68 20 6f 66 20 74 68  * how much of th
20706 65 20 74 72 65 65 20 69 73 20 6d 65 61 73 75 72  e tree is measur
20707 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 64 75  ed..**.**     du
20708 70 65 64 45 78 70 72 53 74 72 75 63 74 53 69 7a  pedExprStructSiz
20709 65 28 29 20 20 20 20 20 53 69 7a 65 20 6f 66 20  e()     Size of 
2070a 6f 6e 6c 79 20 74 68 65 20 45 78 70 72 20 73 74  only the Expr st
2070b 72 75 63 74 75 72 65 20 0a 2a 2a 20 20 20 20 20  ructure .**     
2070c 64 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a  dupedExprNodeSiz
2070d 65 28 29 20 20 20 20 20 20 20 53 69 7a 65 20 6f  e()       Size o
2070e 66 20 45 78 70 72 20 2b 20 73 70 61 63 65 20 66  f Expr + space f
2070f 6f 72 20 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20  or token.**     
20710 64 75 70 65 64 45 78 70 72 53 69 7a 65 28 29 20  dupedExprSize() 
20711 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20 2b            Expr +
20712 20 74 6f 6b 65 6e 20 2b 20 73 75 62 74 72 65 65   token + subtree
20713 20 63 6f 6d 70 6f 6e 65 6e 74 73 0a 2a 2a 0a 2a   components.**.*
20714 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20715 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20716 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20717 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20718 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
20719 20 54 68 65 20 64 75 70 65 64 45 78 70 72 53 74   The dupedExprSt
2071a 72 75 63 74 53 69 7a 65 28 29 20 66 75 6e 63 74  ructSize() funct
2071b 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 77 6f 20  ion returns two 
2071c 76 61 6c 75 65 73 20 4f 52 2d 65 64 20 74 6f 67  values OR-ed tog
2071d 65 74 68 65 72 3a 20 20 0a 2a 2a 20 28 31 29 20  ether:  .** (1) 
2071e 74 68 65 20 73 70 61 63 65 20 72 65 71 75 69 72  the space requir
2071f 65 64 20 66 6f 72 20 61 20 63 6f 70 79 20 6f 66  ed for a copy of
20720 20 74 68 65 20 45 78 70 72 20 73 74 72 75 63 74   the Expr struct
20721 75 72 65 20 6f 6e 6c 79 20 61 6e 64 20 0a 2a 2a  ure only and .**
20722 20 28 32 29 20 74 68 65 20 45 50 5f 78 78 78 20   (2) the EP_xxx 
20723 66 6c 61 67 73 20 74 68 61 74 20 69 6e 64 69 63  flags that indic
20724 61 74 65 20 77 68 61 74 20 74 68 65 20 73 74 72  ate what the str
20725 75 63 74 75 72 65 20 73 69 7a 65 20 73 68 6f 75  ucture size shou
20726 6c 64 20 62 65 2e 0a 2a 2a 20 54 68 65 20 72 65  ld be..** The re
20727 74 75 72 6e 20 76 61 6c 75 65 73 20 69 73 20 61  turn values is a
20728 6c 77 61 79 73 20 6f 6e 65 20 6f 66 3a 0a 2a 2a  lways one of:.**
20729 0a 2a 2a 20 20 20 20 20 20 45 58 50 52 5f 46 55  .**      EXPR_FU
2072a 4c 4c 53 49 5a 45 0a 2a 2a 20 20 20 20 20 20 45  LLSIZE.**      E
2072b 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20  XPR_REDUCEDSIZE 
2072c 20 20 7c 20 45 50 5f 52 65 64 75 63 65 64 0a 2a    | EP_Reduced.*
2072d 2a 20 20 20 20 20 20 45 58 50 52 5f 54 4f 4b 45  *      EXPR_TOKE
2072e 4e 4f 4e 4c 59 53 49 5a 45 20 7c 20 45 50 5f 54  NONLYSIZE | EP_T
2072f 6f 6b 65 6e 4f 6e 6c 79 0a 2a 2a 0a 2a 2a 20 54  okenOnly.**.** T
20730 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 73  he size of the s
20731 74 72 75 63 74 75 72 65 20 63 61 6e 20 62 65 20  tructure can be 
20732 66 6f 75 6e 64 20 62 79 20 6d 61 73 6b 69 6e 67  found by masking
20733 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
20734 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20 72 6f 75  e.** of this rou
20735 74 69 6e 65 20 77 69 74 68 20 30 78 66 66 66 2e  tine with 0xfff.
20736 20 20 54 68 65 20 66 6c 61 67 73 20 63 61 6e 20    The flags can 
20737 62 65 20 66 6f 75 6e 64 20 62 79 20 6d 61 73 6b  be found by mask
20738 69 6e 67 20 74 68 65 0a 2a 2a 20 72 65 74 75 72  ing the.** retur
20739 6e 20 76 61 6c 75 65 20 77 69 74 68 20 45 50 5f  n value with EP_
2073a 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e  Reduced|EP_Token
2073b 4f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65  Only..**.** Note
2073c 20 74 68 61 74 20 77 69 74 68 20 66 6c 61 67 73   that with flags
2073d 3d 3d 45 58 50 52 44 55 50 5f 52 45 44 55 43 45  ==EXPRDUP_REDUCE
2073e 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 73 20  , this routines 
2073f 77 6f 72 6b 73 20 6f 6e 20 66 75 6c 6c 2d 73 69  works on full-si
20740 7a 65 0a 2a 2a 20 28 75 6e 72 65 64 75 63 65 64  ze.** (unreduced
20741 29 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20 61  ) Expr objects a
20742 73 20 74 68 65 79 20 6f 72 20 6f 72 69 67 69 6e  s they or origin
20743 61 6c 6c 79 20 63 6f 6e 73 74 72 75 63 74 65 64  ally constructed
20744 20 62 79 20 74 68 65 20 70 61 72 73 65 72 2e 0a   by the parser..
20745 2a 2a 20 44 75 72 69 6e 67 20 65 78 70 72 65 73  ** During expres
20746 73 69 6f 6e 20 61 6e 61 6c 79 73 69 73 2c 20 65  sion analysis, e
20747 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  xtra information
20748 20 69 73 20 63 6f 6d 70 75 74 65 64 20 61 6e 64   is computed and
20749 20 6d 6f 76 65 64 20 69 6e 74 6f 0a 2a 2a 20 6c   moved into.** l
2074a 61 74 65 72 20 70 61 72 74 73 20 6f 66 20 74 65  ater parts of te
2074b 68 20 45 78 70 72 20 6f 62 6a 65 63 74 20 61 6e  h Expr object an
2074c 64 20 74 68 61 74 20 65 78 74 72 61 20 69 6e 66  d that extra inf
2074d 6f 72 6d 61 74 69 6f 6e 20 6d 69 67 68 74 20 67  ormation might g
2074e 65 74 20 63 68 6f 70 70 65 64 0a 2a 2a 20 6f 66  et chopped.** of
2074f 66 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73  f if the express
20750 69 6f 6e 20 69 73 20 72 65 64 75 63 65 64 2e 20  ion is reduced. 
20751 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20   Note also that 
20752 69 74 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b  it does not work
20753 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 61 20 45 58   to.** make a EX
20754 50 52 44 55 50 5f 52 45 44 55 43 45 20 63 6f 70  PRDUP_REDUCE cop
20755 79 20 6f 66 20 61 20 72 65 64 75 63 65 64 20 65  y of a reduced e
20756 78 70 72 65 73 73 69 6f 6e 2e 20 20 49 74 20 69  xpression.  It i
20757 73 20 6f 6e 6c 79 20 6c 65 67 61 6c 0a 2a 2a 20  s only legal.** 
20758 74 6f 20 72 65 64 75 63 65 20 61 20 70 72 69 73  to reduce a pris
20759 74 69 6e 65 20 65 78 70 72 65 73 73 69 6f 6e 20  tine expression 
2075a 74 72 65 65 20 66 72 6f 6d 20 74 68 65 20 70 61  tree from the pa
2075b 72 73 65 72 2e 20 20 54 68 65 20 69 6d 70 6c 65  rser.  The imple
2075c 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20  mentation.** of 
2075d 64 75 70 65 64 45 78 70 72 53 74 72 75 63 74 53  dupedExprStructS
2075e 69 7a 65 28 29 20 63 6f 6e 74 61 69 6e 20 6d 75  ize() contain mu
2075f 6c 74 69 70 6c 65 20 61 73 73 65 72 74 28 29 20  ltiple assert() 
20760 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20  statements that 
20761 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e  attempt.** to en
20762 66 6f 72 63 65 20 74 68 69 73 20 63 6f 6e 73 74  force this const
20763 72 61 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  raint..*/.static
20764 20 69 6e 74 20 64 75 70 65 64 45 78 70 72 53 74   int dupedExprSt
20765 72 75 63 74 53 69 7a 65 28 45 78 70 72 20 2a 70  ructSize(Expr *p
20766 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20  , int flags){.  
20767 69 6e 74 20 6e 53 69 7a 65 3b 0a 20 20 61 73 73  int nSize;.  ass
20768 65 72 74 28 20 66 6c 61 67 73 3d 3d 45 58 50 52  ert( flags==EXPR
20769 44 55 50 5f 52 45 44 55 43 45 20 7c 7c 20 66 6c  DUP_REDUCE || fl
2076a 61 67 73 3d 3d 30 20 29 3b 20 2f 2a 20 4f 6e 6c  ags==0 ); /* Onl
2076b 79 20 6f 6e 65 20 66 6c 61 67 20 76 61 6c 75 65  y one flag value
2076c 20 61 6c 6c 6f 77 65 64 20 2a 2f 0a 20 20 69 66   allowed */.  if
2076d 28 20 30 3d 3d 28 66 6c 61 67 73 26 45 58 50 52  ( 0==(flags&EXPR
2076e 44 55 50 5f 52 45 44 55 43 45 29 20 29 7b 0a 20  DUP_REDUCE) ){. 
2076f 20 20 20 6e 53 69 7a 65 20 3d 20 45 58 50 52 5f     nSize = EXPR_
20770 46 55 4c 4c 53 49 5a 45 3b 0a 20 20 7d 65 6c 73  FULLSIZE;.  }els
20771 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  e{.    assert( !
20772 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72  ExprHasAnyProper
20773 74 79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e  ty(p, EP_TokenOn
20774 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29  ly|EP_Reduced) )
20775 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 45  ;.    assert( !E
20776 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
20777 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29  , EP_FromJoin) )
20778 3b 20 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  ; .    assert( (
20779 70 2d 3e 66 6c 61 67 73 32 20 26 20 45 50 32 5f  p->flags2 & EP2_
2077a 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e 29 3d 3d  MallocedToken)==
2077b 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
2077c 20 28 70 2d 3e 66 6c 61 67 73 32 20 26 20 45 50   (p->flags2 & EP
2077d 32 5f 49 72 72 65 64 75 63 69 62 6c 65 29 3d 3d  2_Irreducible)==
2077e 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  0 );.    if( p->
2077f 70 4c 65 66 74 20 7c 7c 20 70 2d 3e 70 52 69 67  pLeft || p->pRig
20780 68 74 20 7c 7c 20 70 2d 3e 70 43 6f 6c 6c 20 7c  ht || p->pColl |
20781 7c 20 70 2d 3e 78 2e 70 4c 69 73 74 20 29 7b 0a  | p->x.pList ){.
20782 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 45 58        nSize = EX
20783 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 7c  PR_REDUCEDSIZE |
20784 20 45 50 5f 52 65 64 75 63 65 64 3b 0a 20 20 20   EP_Reduced;.   
20785 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 53   }else{.      nS
20786 69 7a 65 20 3d 20 45 58 50 52 5f 54 4f 4b 45 4e  ize = EXPR_TOKEN
20787 4f 4e 4c 59 53 49 5a 45 20 7c 20 45 50 5f 54 6f  ONLYSIZE | EP_To
20788 6b 65 6e 4f 6e 6c 79 3b 0a 20 20 20 20 7d 0a 20  kenOnly;.    }. 
20789 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 53 69 7a   }.  return nSiz
2078a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  e;.}../*.** This
2078b 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
2078c 73 20 74 68 65 20 73 70 61 63 65 20 69 6e 20 62  s the space in b
2078d 79 74 65 73 20 72 65 71 75 69 72 65 64 20 74 6f  ytes required to
2078e 20 73 74 6f 72 65 20 74 68 65 20 63 6f 70 79 20   store the copy 
2078f 0a 2a 2a 20 6f 66 20 74 68 65 20 45 78 70 72 20  .** of the Expr 
20790 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 61 20  structure and a 
20791 63 6f 70 79 20 6f 66 20 74 68 65 20 45 78 70 72  copy of the Expr
20792 2e 75 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67  .u.zToken string
20793 20 28 69 66 20 74 68 61 74 0a 2a 2a 20 73 74 72   (if that.** str
20794 69 6e 67 20 69 73 20 64 65 66 69 6e 65 64 2e 29  ing is defined.)
20795 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
20796 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a 65  upedExprNodeSize
20797 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c  (Expr *p, int fl
20798 61 67 73 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74  ags){.  int nByt
20799 65 20 3d 20 64 75 70 65 64 45 78 70 72 53 74 72  e = dupedExprStr
2079a 75 63 74 53 69 7a 65 28 70 2c 20 66 6c 61 67 73  uctSize(p, flags
2079b 29 20 26 20 30 78 66 66 66 3b 0a 20 20 69 66 28  ) & 0xfff;.  if(
2079c 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
2079d 79 28 70 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65  y(p, EP_IntValue
2079e 29 20 26 26 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e  ) && p->u.zToken
2079f 20 29 7b 0a 20 20 20 20 6e 42 79 74 65 20 2b 3d   ){.    nByte +=
207a0 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
207a1 28 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 2b 31 3b  (p->u.zToken)+1;
207a2 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 52 4f  .  }.  return RO
207a3 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 7d 0a 0a  UND8(nByte);.}..
207a4 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
207a5 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
207a6 20 72 65 71 75 69 72 65 64 20 74 6f 20 63 72 65   required to cre
207a7 61 74 65 20 61 20 64 75 70 6c 69 63 61 74 65 20  ate a duplicate 
207a8 6f 66 20 74 68 65 20 0a 2a 2a 20 65 78 70 72 65  of the .** expre
207a9 73 73 69 6f 6e 20 70 61 73 73 65 64 20 61 73 20  ssion passed as 
207aa 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
207ab 6e 74 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 61  nt. The second a
207ac 72 67 75 6d 65 6e 74 20 69 73 20 61 0a 2a 2a 20  rgument is a.** 
207ad 6d 61 73 6b 20 63 6f 6e 74 61 69 6e 69 6e 67 20  mask containing 
207ae 45 58 50 52 44 55 50 5f 58 58 58 20 66 6c 61 67  EXPRDUP_XXX flag
207af 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c  s..**.** The val
207b0 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e 63 6c  ue returned incl
207b1 75 64 65 73 20 73 70 61 63 65 20 74 6f 20 63 72  udes space to cr
207b2 65 61 74 65 20 61 20 63 6f 70 79 20 6f 66 20 74  eate a copy of t
207b3 68 65 20 45 78 70 72 20 73 74 72 75 63 74 0a 2a  he Expr struct.*
207b4 2a 20 69 74 73 65 6c 66 20 61 6e 64 20 74 68 65  * itself and the
207b5 20 62 75 66 66 65 72 20 72 65 66 65 72 72 65 64   buffer referred
207b6 20 74 6f 20 62 79 20 45 78 70 72 2e 75 2e 7a 54   to by Expr.u.zT
207b7 6f 6b 65 6e 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a  oken, if any..**
207b8 0a 2a 2a 20 49 66 20 74 68 65 20 45 58 50 52 44  .** If the EXPRD
207b9 55 50 5f 52 45 44 55 43 45 20 66 6c 61 67 20 69  UP_REDUCE flag i
207ba 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20  s set, then the 
207bb 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 63  return value inc
207bc 6c 75 64 65 73 20 0a 2a 2a 20 73 70 61 63 65 20  ludes .** space 
207bd 74 6f 20 64 75 70 6c 69 63 61 74 65 20 61 6c 6c  to duplicate all
207be 20 45 78 70 72 20 6e 6f 64 65 73 20 69 6e 20 74   Expr nodes in t
207bf 68 65 20 74 72 65 65 20 66 6f 72 6d 65 64 20 62  he tree formed b
207c0 79 20 45 78 70 72 2e 70 4c 65 66 74 20 0a 2a 2a  y Expr.pLeft .**
207c1 20 61 6e 64 20 45 78 70 72 2e 70 52 69 67 68 74   and Expr.pRight
207c2 20 76 61 72 69 61 62 6c 65 73 20 28 62 75 74 20   variables (but 
207c3 6e 6f 74 20 66 6f 72 20 61 6e 79 20 73 74 72 75  not for any stru
207c4 63 74 75 72 65 73 20 70 6f 69 6e 74 65 64 20 74  ctures pointed t
207c5 6f 20 6f 72 20 0a 2a 2a 20 64 65 73 63 65 6e 64  o or .** descend
207c6 65 64 20 66 72 6f 6d 20 74 68 65 20 45 78 70 72  ed from the Expr
207c7 2e 78 2e 70 4c 69 73 74 20 6f 72 20 45 78 70 72  .x.pList or Expr
207c8 2e 78 2e 70 53 65 6c 65 63 74 20 76 61 72 69 61  .x.pSelect varia
207c9 62 6c 65 73 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  bles)..*/.static
207ca 20 69 6e 74 20 64 75 70 65 64 45 78 70 72 53 69   int dupedExprSi
207cb 7a 65 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20  ze(Expr *p, int 
207cc 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 6e 42  flags){.  int nB
207cd 79 74 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70  yte = 0;.  if( p
207ce 20 29 7b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20   ){.    nByte = 
207cf 64 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a  dupedExprNodeSiz
207d0 65 28 70 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20  e(p, flags);.   
207d1 20 69 66 28 20 66 6c 61 67 73 26 45 58 50 52 44   if( flags&EXPRD
207d2 55 50 5f 52 45 44 55 43 45 20 29 7b 0a 20 20 20  UP_REDUCE ){.   
207d3 20 20 20 6e 42 79 74 65 20 2b 3d 20 64 75 70 65     nByte += dupe
207d4 64 45 78 70 72 53 69 7a 65 28 70 2d 3e 70 4c 65  dExprSize(p->pLe
207d5 66 74 2c 20 66 6c 61 67 73 29 20 2b 20 64 75 70  ft, flags) + dup
207d6 65 64 45 78 70 72 53 69 7a 65 28 70 2d 3e 70 52  edExprSize(p->pR
207d7 69 67 68 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20  ight, flags);.  
207d8 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
207d9 20 6e 42 79 74 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   nByte;.}../*.**
207da 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
207db 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 73 71 6c  s similar to sql
207dc 69 74 65 33 45 78 70 72 44 75 70 28 29 2c 20 65  ite3ExprDup(), e
207dd 78 63 65 70 74 20 74 68 61 74 20 69 66 20 70 7a  xcept that if pz
207de 42 75 66 66 65 72 20 0a 2a 2a 20 69 73 20 6e 6f  Buffer .** is no
207df 74 20 4e 55 4c 4c 20 74 68 65 6e 20 2a 70 7a 42  t NULL then *pzB
207e0 75 66 66 65 72 20 69 73 20 61 73 73 75 6d 65 64  uffer is assumed
207e1 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 20 62   to point to a b
207e2 75 66 66 65 72 20 6c 61 72 67 65 20 65 6e 6f 75  uffer large enou
207e3 67 68 20 0a 2a 2a 20 74 6f 20 73 74 6f 72 65 20  gh .** to store 
207e4 74 68 65 20 63 6f 70 79 20 6f 66 20 65 78 70 72  the copy of expr
207e5 65 73 73 69 6f 6e 20 70 2c 20 74 68 65 20 63 6f  ession p, the co
207e6 70 69 65 73 20 6f 66 20 70 2d 3e 75 2e 7a 54 6f  pies of p->u.zTo
207e7 6b 65 6e 0a 2a 2a 20 28 69 66 20 61 70 70 6c 69  ken.** (if appli
207e8 63 61 62 6c 65 29 2c 20 61 6e 64 20 74 68 65 20  cable), and the 
207e9 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20 70 2d  copies of the p-
207ea 3e 70 4c 65 66 74 20 61 6e 64 20 70 2d 3e 70 52  >pLeft and p->pR
207eb 69 67 68 74 20 65 78 70 72 65 73 73 69 6f 6e 73  ight expressions
207ec 2c 0a 2a 2a 20 69 66 20 61 6e 79 2e 20 42 65 66  ,.** if any. Bef
207ed 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 2a  ore returning, *
207ee 70 7a 42 75 66 66 65 72 20 69 73 20 73 65 74 20  pzBuffer is set 
207ef 74 6f 20 74 68 65 20 66 69 72 73 74 20 62 79 74  to the first byt
207f0 65 20 70 61 73 73 65 64 20 74 68 65 0a 2a 2a 20  e passed the.** 
207f1 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 62  portion of the b
207f2 75 66 66 65 72 20 63 6f 70 69 65 64 20 69 6e 74  uffer copied int
207f3 6f 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  o by this functi
207f4 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 45 78  on..*/.static Ex
207f5 70 72 20 2a 65 78 70 72 44 75 70 28 73 71 6c 69  pr *exprDup(sqli
207f6 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70  te3 *db, Expr *p
207f7 2c 20 69 6e 74 20 66 6c 61 67 73 2c 20 75 38 20  , int flags, u8 
207f8 2a 2a 70 7a 42 75 66 66 65 72 29 7b 0a 20 20 45  **pzBuffer){.  E
207f9 78 70 72 20 2a 70 4e 65 77 20 3d 20 30 3b 20 20  xpr *pNew = 0;  
207fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
207fb 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20      /* Value to 
207fc 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 66 28 20  return */.  if( 
207fd 70 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69  p ){.    const i
207fe 6e 74 20 69 73 52 65 64 75 63 65 64 20 3d 20 28  nt isReduced = (
207ff 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f 52 45  flags&EXPRDUP_RE
20800 44 55 43 45 29 3b 0a 20 20 20 20 75 38 20 2a 7a  DUCE);.    u8 *z
20801 41 6c 6c 6f 63 3b 0a 20 20 20 20 75 33 32 20 73  Alloc;.    u32 s
20802 74 61 74 69 63 46 6c 61 67 20 3d 20 30 3b 0a 0a  taticFlag = 0;..
20803 20 20 20 20 61 73 73 65 72 74 28 20 70 7a 42 75      assert( pzBu
20804 66 66 65 72 3d 3d 30 20 7c 7c 20 69 73 52 65 64  ffer==0 || isRed
20805 75 63 65 64 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  uced );..    /* 
20806 46 69 67 75 72 65 20 6f 75 74 20 77 68 65 72 65  Figure out where
20807 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 6e 65   to write the ne
20808 77 20 45 78 70 72 20 73 74 72 75 63 74 75 72 65  w Expr structure
20809 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 7a 42  . */.    if( pzB
2080a 75 66 66 65 72 20 29 7b 0a 20 20 20 20 20 20 7a  uffer ){.      z
2080b 41 6c 6c 6f 63 20 3d 20 2a 70 7a 42 75 66 66 65  Alloc = *pzBuffe
2080c 72 3b 0a 20 20 20 20 20 20 73 74 61 74 69 63 46  r;.      staticF
2080d 6c 61 67 20 3d 20 45 50 5f 53 74 61 74 69 63 3b  lag = EP_Static;
2080e 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2080f 20 20 7a 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74    zAlloc = sqlit
20810 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
20811 2c 20 64 75 70 65 64 45 78 70 72 53 69 7a 65 28  , dupedExprSize(
20812 70 2c 20 66 6c 61 67 73 29 29 3b 0a 20 20 20 20  p, flags));.    
20813 7d 0a 20 20 20 20 70 4e 65 77 20 3d 20 28 45 78  }.    pNew = (Ex
20814 70 72 20 2a 29 7a 41 6c 6c 6f 63 3b 0a 0a 20 20  pr *)zAlloc;..  
20815 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20    if( pNew ){.  
20816 20 20 20 20 2f 2a 20 53 65 74 20 6e 4e 65 77 53      /* Set nNewS
20817 69 7a 65 20 74 6f 20 74 68 65 20 73 69 7a 65 20  ize to the size 
20818 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 68  allocated for th
20819 65 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e  e structure poin
2081a 74 65 64 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20  ted to.      ** 
2081b 62 79 20 70 4e 65 77 2e 20 54 68 69 73 20 69 73  by pNew. This is
2081c 20 65 69 74 68 65 72 20 45 58 50 52 5f 46 55 4c   either EXPR_FUL
2081d 4c 53 49 5a 45 2c 20 45 58 50 52 5f 52 45 44 55  LSIZE, EXPR_REDU
2081e 43 45 44 53 49 5a 45 20 6f 72 0a 20 20 20 20 20  CEDSIZE or.     
2081f 20 2a 2a 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e   ** EXPR_TOKENON
20820 4c 59 53 49 5a 45 2e 20 6e 54 6f 6b 65 6e 20 69  LYSIZE. nToken i
20821 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d  s set to the num
20822 62 65 72 20 6f 66 20 62 79 74 65 73 20 63 6f 6e  ber of bytes con
20823 73 75 6d 65 64 0a 20 20 20 20 20 20 2a 2a 20 62  sumed.      ** b
20824 79 20 74 68 65 20 63 6f 70 79 20 6f 66 20 74 68  y the copy of th
20825 65 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 73 74  e p->u.zToken st
20826 72 69 6e 67 20 28 69 66 20 61 6e 79 29 2e 0a 20  ring (if any).. 
20827 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f       */.      co
20828 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 6e 53 74  nst unsigned nSt
20829 72 75 63 74 53 69 7a 65 20 3d 20 64 75 70 65 64  ructSize = duped
2082a 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 70  ExprStructSize(p
2082b 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 20 20  , flags);.      
2082c 63 6f 6e 73 74 20 69 6e 74 20 6e 4e 65 77 53 69  const int nNewSi
2082d 7a 65 20 3d 20 6e 53 74 72 75 63 74 53 69 7a 65  ze = nStructSize
2082e 20 26 20 30 78 66 66 66 3b 0a 20 20 20 20 20 20   & 0xfff;.      
2082f 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20  int nToken;.    
20830 20 20 69 66 28 20 21 45 78 70 72 48 61 73 50 72    if( !ExprHasPr
20831 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 49 6e 74  operty(p, EP_Int
20832 56 61 6c 75 65 29 20 26 26 20 70 2d 3e 75 2e 7a  Value) && p->u.z
20833 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20 20  Token ){.       
20834 20 6e 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65   nToken = sqlite
20835 33 53 74 72 6c 65 6e 33 30 28 70 2d 3e 75 2e 7a  3Strlen30(p->u.z
20836 54 6f 6b 65 6e 29 20 2b 20 31 3b 0a 20 20 20 20  Token) + 1;.    
20837 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
20838 20 6e 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 20   nToken = 0;.   
20839 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69     }.      if( i
2083a 73 52 65 64 75 63 65 64 20 29 7b 0a 20 20 20 20  sReduced ){.    
2083b 20 20 20 20 61 73 73 65 72 74 28 20 45 78 70 72      assert( Expr
2083c 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45  HasProperty(p, E
2083d 50 5f 52 65 64 75 63 65 64 29 3d 3d 30 20 29 3b  P_Reduced)==0 );
2083e 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
2083f 7a 41 6c 6c 6f 63 2c 20 70 2c 20 6e 4e 65 77 53  zAlloc, p, nNewS
20840 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ize);.      }els
20841 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e  e{.        int n
20842 53 69 7a 65 20 3d 20 65 78 70 72 53 74 72 75 63  Size = exprStruc
20843 74 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 20 20  tSize(p);.      
20844 20 20 6d 65 6d 63 70 79 28 7a 41 6c 6c 6f 63 2c    memcpy(zAlloc,
20845 20 70 2c 20 6e 53 69 7a 65 29 3b 0a 20 20 20 20   p, nSize);.    
20846 20 20 20 20 6d 65 6d 73 65 74 28 26 7a 41 6c 6c      memset(&zAll
20847 6f 63 5b 6e 53 69 7a 65 5d 2c 20 30 2c 20 45 58  oc[nSize], 0, EX
20848 50 52 5f 46 55 4c 4c 53 49 5a 45 2d 6e 53 69 7a  PR_FULLSIZE-nSiz
20849 65 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  e);.      }..   
2084a 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 45 50     /* Set the EP
2084b 5f 52 65 64 75 63 65 64 2c 20 45 50 5f 54 6f 6b  _Reduced, EP_Tok
2084c 65 6e 4f 6e 6c 79 2c 20 61 6e 64 20 45 50 5f 53  enOnly, and EP_S
2084d 74 61 74 69 63 20 66 6c 61 67 73 20 61 70 70 72  tatic flags appr
2084e 6f 70 72 69 61 74 65 6c 79 2e 20 2a 2f 0a 20 20  opriately. */.  
2084f 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20      pNew->flags 
20850 26 3d 20 7e 28 45 50 5f 52 65 64 75 63 65 64 7c  &= ~(EP_Reduced|
20851 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f  EP_TokenOnly|EP_
20852 53 74 61 74 69 63 29 3b 0a 20 20 20 20 20 20 70  Static);.      p
20853 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 6e 53  New->flags |= nS
20854 74 72 75 63 74 53 69 7a 65 20 26 20 28 45 50 5f  tructSize & (EP_
20855 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e  Reduced|EP_Token
20856 4f 6e 6c 79 29 3b 0a 20 20 20 20 20 20 70 4e 65  Only);.      pNe
20857 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 73 74 61 74  w->flags |= stat
20858 69 63 46 6c 61 67 3b 0a 0a 20 20 20 20 20 20 2f  icFlag;..      /
20859 2a 20 43 6f 70 79 20 74 68 65 20 70 2d 3e 75 2e  * Copy the p->u.
2085a 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67 2c 20 69  zToken string, i
2085b 66 20 61 6e 79 2e 20 2a 2f 0a 20 20 20 20 20 20  f any. */.      
2085c 69 66 28 20 6e 54 6f 6b 65 6e 20 29 7b 0a 20 20  if( nToken ){.  
2085d 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 6f 6b        char *zTok
2085e 65 6e 20 3d 20 70 4e 65 77 2d 3e 75 2e 7a 54 6f  en = pNew->u.zTo
2085f 6b 65 6e 20 3d 20 28 63 68 61 72 2a 29 26 7a 41  ken = (char*)&zA
20860 6c 6c 6f 63 5b 6e 4e 65 77 53 69 7a 65 5d 3b 0a  lloc[nNewSize];.
20861 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a          memcpy(z
20862 54 6f 6b 65 6e 2c 20 70 2d 3e 75 2e 7a 54 6f 6b  Token, p->u.zTok
20863 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20  en, nToken);.   
20864 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20     }..      if( 
20865 30 3d 3d 28 28 70 2d 3e 66 6c 61 67 73 7c 70 4e  0==((p->flags|pN
20866 65 77 2d 3e 66 6c 61 67 73 29 20 26 20 45 50 5f  ew->flags) & EP_
20867 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20  TokenOnly) ){.  
20868 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e        /* Fill in
20869 20 74 68 65 20 70 4e 65 77 2d 3e 78 2e 70 53 65   the pNew->x.pSe
2086a 6c 65 63 74 20 6f 72 20 70 4e 65 77 2d 3e 78 2e  lect or pNew->x.
2086b 70 4c 69 73 74 20 6d 65 6d 62 65 72 2e 20 2a 2f  pList member. */
2086c 0a 20 20 20 20 20 20 20 20 69 66 28 20 45 78 70  .        if( Exp
2086d 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20  rHasProperty(p, 
2086e 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b  EP_xIsSelect) ){
2086f 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d  .          pNew-
20870 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c  >x.pSelect = sql
20871 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62  ite3SelectDup(db
20872 2c 20 70 2d 3e 78 2e 70 53 65 6c 65 63 74 2c 20  , p->x.pSelect, 
20873 69 73 52 65 64 75 63 65 64 29 3b 0a 20 20 20 20  isReduced);.    
20874 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
20875 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e 70 4c 69       pNew->x.pLi
20876 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
20877 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 78  ListDup(db, p->x
20878 2e 70 4c 69 73 74 2c 20 69 73 52 65 64 75 63 65  .pList, isReduce
20879 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  d);.        }.  
2087a 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
2087b 46 69 6c 6c 20 69 6e 20 70 4e 65 77 2d 3e 70 4c  Fill in pNew->pL
2087c 65 66 74 20 61 6e 64 20 70 4e 65 77 2d 3e 70 52  eft and pNew->pR
2087d 69 67 68 74 2e 20 2a 2f 0a 20 20 20 20 20 20 69  ight. */.      i
2087e 66 28 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f  f( ExprHasAnyPro
2087f 70 65 72 74 79 28 70 4e 65 77 2c 20 45 50 5f 52  perty(pNew, EP_R
20880 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f  educed|EP_TokenO
20881 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 20 20 20  nly) ){.        
20882 7a 41 6c 6c 6f 63 20 2b 3d 20 64 75 70 65 64 45  zAlloc += dupedE
20883 78 70 72 4e 6f 64 65 53 69 7a 65 28 70 2c 20 66  xprNodeSize(p, f
20884 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 20 20 69  lags);.        i
20885 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  f( ExprHasProper
20886 74 79 28 70 4e 65 77 2c 20 45 50 5f 52 65 64 75  ty(pNew, EP_Redu
20887 63 65 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ced) ){.        
20888 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20    pNew->pLeft = 
20889 65 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70  exprDup(db, p->p
2088a 4c 65 66 74 2c 20 45 58 50 52 44 55 50 5f 52 45  Left, EXPRDUP_RE
2088b 44 55 43 45 2c 20 26 7a 41 6c 6c 6f 63 29 3b 0a  DUCE, &zAlloc);.
2088c 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e            pNew->
2088d 70 52 69 67 68 74 20 3d 20 65 78 70 72 44 75 70  pRight = exprDup
2088e 28 64 62 2c 20 70 2d 3e 70 52 69 67 68 74 2c 20  (db, p->pRight, 
2088f 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 2c 20  EXPRDUP_REDUCE, 
20890 26 7a 41 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20  &zAlloc);.      
20891 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
20892 70 7a 42 75 66 66 65 72 20 29 7b 0a 20 20 20 20  pzBuffer ){.    
20893 20 20 20 20 20 20 2a 70 7a 42 75 66 66 65 72 20        *pzBuffer 
20894 3d 20 7a 41 6c 6c 6f 63 3b 0a 20 20 20 20 20 20  = zAlloc;.      
20895 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b    }.      }else{
20896 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 66  .        pNew->f
20897 6c 61 67 73 32 20 3d 20 30 3b 0a 20 20 20 20 20  lags2 = 0;.     
20898 20 20 20 69 66 28 20 21 45 78 70 72 48 61 73 41     if( !ExprHasA
20899 6e 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50  nyProperty(p, EP
2089a 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20  _TokenOnly) ){. 
2089b 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70           pNew->p
2089c 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78  Left = sqlite3Ex
2089d 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c 65  prDup(db, p->pLe
2089e 66 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  ft, 0);.        
2089f 20 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 20 3d    pNew->pRight =
208a0 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
208a1 64 62 2c 20 70 2d 3e 70 52 69 67 68 74 2c 20 30  db, p->pRight, 0
208a2 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
208a3 20 20 20 7d 0a 0a 20 20 20 20 7d 0a 20 20 7d 0a     }..    }.  }.
208a4 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d    return pNew;.}
208a5 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
208a6 6f 77 69 6e 67 20 67 72 6f 75 70 20 6f 66 20 72  owing group of r
208a7 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20 64 65 65  outines make dee
208a8 70 20 63 6f 70 69 65 73 20 6f 66 20 65 78 70 72  p copies of expr
208a9 65 73 73 69 6f 6e 73 2c 0a 2a 2a 20 65 78 70 72  essions,.** expr
208aa 65 73 73 69 6f 6e 20 6c 69 73 74 73 2c 20 49 44  ession lists, ID
208ab 20 6c 69 73 74 73 2c 20 61 6e 64 20 73 65 6c 65   lists, and sele
208ac 63 74 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20  ct statements.  
208ad 54 68 65 20 63 6f 70 69 65 73 20 63 61 6e 0a 2a  The copies can.*
208ae 2a 20 62 65 20 64 65 6c 65 74 65 64 20 28 62 79  * be deleted (by
208af 20 62 65 69 6e 67 20 70 61 73 73 65 64 20 74 6f   being passed to
208b0 20 74 68 65 69 72 20 72 65 73 70 65 63 74 69 76   their respectiv
208b1 65 20 2e 2e 2e 44 65 6c 65 74 65 28 29 20 72 6f  e ...Delete() ro
208b2 75 74 69 6e 65 73 29 0a 2a 2a 20 77 69 74 68 6f  utines).** witho
208b3 75 74 20 65 66 66 65 63 74 69 6e 67 20 74 68 65  ut effecting the
208b4 20 6f 72 69 67 69 6e 61 6c 73 2e 0a 2a 2a 0a 2a   originals..**.*
208b5 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e  * The expression
208b6 20 6c 69 73 74 2c 20 49 44 2c 20 61 6e 64 20 73   list, ID, and s
208b7 6f 75 72 63 65 20 6c 69 73 74 73 20 72 65 74 75  ource lists retu
208b8 72 6e 20 62 79 20 73 71 6c 69 74 65 33 45 78 70  rn by sqlite3Exp
208b9 72 4c 69 73 74 44 75 70 28 29 2c 0a 2a 2a 20 73  rListDup(),.** s
208ba 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 28  qlite3IdListDup(
208bb 29 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 53 72  ), and sqlite3Sr
208bc 63 4c 69 73 74 44 75 70 28 29 20 63 61 6e 20 6e  cListDup() can n
208bd 6f 74 20 62 65 20 66 75 72 74 68 65 72 20 65 78  ot be further ex
208be 70 61 6e 64 65 64 20 0a 2a 2a 20 62 79 20 73 75  panded .** by su
208bf 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74  bsequent calls t
208c0 6f 20 73 71 6c 69 74 65 2a 4c 69 73 74 41 70 70  o sqlite*ListApp
208c1 65 6e 64 28 29 20 72 6f 75 74 69 6e 65 73 2e 0a  end() routines..
208c2 2a 2a 0a 2a 2a 20 41 6e 79 20 74 61 62 6c 65 73  **.** Any tables
208c3 20 74 68 61 74 20 74 68 65 20 53 72 63 4c 69 73   that the SrcLis
208c4 74 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f  t might point to
208c5 20 61 72 65 20 6e 6f 74 20 64 75 70 6c 69 63 61   are not duplica
208c6 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ted..**.** The f
208c7 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 63  lags parameter c
208c8 6f 6e 74 61 69 6e 73 20 61 20 63 6f 6d 62 69 6e  ontains a combin
208c9 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 45 58 50  ation of the EXP
208ca 52 44 55 50 5f 58 58 58 20 66 6c 61 67 73 2e 0a  RDUP_XXX flags..
208cb 2a 2a 20 49 66 20 74 68 65 20 45 58 50 52 44 55  ** If the EXPRDU
208cc 50 5f 52 45 44 55 43 45 20 66 6c 61 67 20 69 73  P_REDUCE flag is
208cd 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 73   set, then the s
208ce 74 72 75 63 74 75 72 65 20 72 65 74 75 72 6e 65  tructure returne
208cf 64 20 69 73 20 61 0a 2a 2a 20 74 72 75 6e 63 61  d is a.** trunca
208d0 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  ted version of t
208d1 68 65 20 75 73 75 61 6c 20 45 78 70 72 20 73 74  he usual Expr st
208d2 72 75 63 74 75 72 65 20 74 68 61 74 20 77 69 6c  ructure that wil
208d3 6c 20 62 65 20 73 74 6f 72 65 64 20 61 73 0a 2a  l be stored as.*
208d4 2a 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e  * part of the in
208d5 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e  -memory represen
208d6 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61  tation of the da
208d7 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 0a 2a  tabase schema..*
208d8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
208d9 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78   Expr *sqlite3Ex
208da 70 72 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64  prDup(sqlite3 *d
208db 62 2c 20 45 78 70 72 20 2a 70 2c 20 69 6e 74 20  b, Expr *p, int 
208dc 66 6c 61 67 73 29 7b 0a 20 20 72 65 74 75 72 6e  flags){.  return
208dd 20 65 78 70 72 44 75 70 28 64 62 2c 20 70 2c 20   exprDup(db, p, 
208de 66 6c 61 67 73 2c 20 30 29 3b 0a 7d 0a 53 51 4c  flags, 0);.}.SQL
208df 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
208e0 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78 70  List *sqlite3Exp
208e1 72 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 33  rListDup(sqlite3
208e2 20 2a 64 62 2c 20 45 78 70 72 4c 69 73 74 20 2a   *db, ExprList *
208e3 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20  p, int flags){. 
208e4 20 45 78 70 72 4c 69 73 74 20 2a 70 4e 65 77 3b   ExprList *pNew;
208e5 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69  .  struct ExprLi
208e6 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 2c 20  st_item *pItem, 
208e7 2a 70 4f 6c 64 49 74 65 6d 3b 0a 20 20 69 6e 74  *pOldItem;.  int
208e8 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29   i;.  if( p==0 )
208e9 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65   return 0;.  pNe
208ea 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  w = sqlite3DbMal
208eb 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f  locRaw(db, sizeo
208ec 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20 69 66  f(*pNew) );.  if
208ed 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75  ( pNew==0 ) retu
208ee 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 69 45  rn 0;.  pNew->iE
208ef 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 70 4e  Cursor = 0;.  pN
208f0 65 77 2d 3e 6e 45 78 70 72 20 3d 20 70 4e 65 77  ew->nExpr = pNew
208f1 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 45  ->nAlloc = p->nE
208f2 78 70 72 3b 0a 20 20 70 4e 65 77 2d 3e 61 20 3d  xpr;.  pNew->a =
208f3 20 70 49 74 65 6d 20 3d 20 73 71 6c 69 74 65 33   pItem = sqlite3
208f4 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
208f5 20 70 2d 3e 6e 45 78 70 72 2a 73 69 7a 65 6f 66   p->nExpr*sizeof
208f6 28 70 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 69  (p->a[0]) );.  i
208f7 66 28 20 70 49 74 65 6d 3d 3d 30 20 29 7b 0a 20  f( pItem==0 ){. 
208f8 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
208f9 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20  (db, pNew);.    
208fa 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 20 0a 20  return 0;.  } . 
208fb 20 70 4f 6c 64 49 74 65 6d 20 3d 20 70 2d 3e 61   pOldItem = p->a
208fc 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
208fd 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49  ->nExpr; i++, pI
208fe 74 65 6d 2b 2b 2c 20 70 4f 6c 64 49 74 65 6d 2b  tem++, pOldItem+
208ff 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 4f  +){.    Expr *pO
20900 6c 64 45 78 70 72 20 3d 20 70 4f 6c 64 49 74 65  ldExpr = pOldIte
20901 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 70 49  m->pExpr;.    pI
20902 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c  tem->pExpr = sql
20903 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
20904 70 4f 6c 64 45 78 70 72 2c 20 66 6c 61 67 73 29  pOldExpr, flags)
20905 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61  ;.    pItem->zNa
20906 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  me = sqlite3DbSt
20907 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65  rDup(db, pOldIte
20908 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70  m->zName);.    p
20909 49 74 65 6d 2d 3e 7a 53 70 61 6e 20 3d 20 73 71  Item->zSpan = sq
2090a 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
2090b 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 53 70 61  , pOldItem->zSpa
2090c 6e 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 73  n);.    pItem->s
2090d 6f 72 74 4f 72 64 65 72 20 3d 20 70 4f 6c 64 49  ortOrder = pOldI
2090e 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b 0a  tem->sortOrder;.
2090f 20 20 20 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20      pItem->done 
20910 3d 20 30 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e  = 0;.    pItem->
20911 69 43 6f 6c 20 3d 20 70 4f 6c 64 49 74 65 6d 2d  iCol = pOldItem-
20912 3e 69 43 6f 6c 3b 0a 20 20 20 20 70 49 74 65 6d  >iCol;.    pItem
20913 2d 3e 69 41 6c 69 61 73 20 3d 20 70 4f 6c 64 49  ->iAlias = pOldI
20914 74 65 6d 2d 3e 69 41 6c 69 61 73 3b 0a 20 20 7d  tem->iAlias;.  }
20915 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a  .  return pNew;.
20916 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 75 72 73  }../*.** If curs
20917 6f 72 73 2c 20 74 72 69 67 67 65 72 73 2c 20 76  ors, triggers, v
20918 69 65 77 73 20 61 6e 64 20 73 75 62 71 75 65 72  iews and subquer
20919 69 65 73 20 61 72 65 20 61 6c 6c 20 6f 6d 69 74  ies are all omit
2091a 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20  ted from.** the 
2091b 62 75 69 6c 64 2c 20 74 68 65 6e 20 6e 6f 6e 65  build, then none
2091c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
2091d 67 20 72 6f 75 74 69 6e 65 73 2c 20 65 78 63 65  g routines, exce
2091e 70 74 20 66 6f 72 20 0a 2a 2a 20 73 71 6c 69 74  pt for .** sqlit
2091f 65 33 53 65 6c 65 63 74 44 75 70 28 29 2c 20 63  e3SelectDup(), c
20920 61 6e 20 62 65 20 63 61 6c 6c 65 64 2e 20 73 71  an be called. sq
20921 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 29  lite3SelectDup()
20922 20 69 73 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a   is sometimes.**
20923 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 4e   called with a N
20924 55 4c 4c 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f  ULL argument..*/
20925 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
20926 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20  LITE_OMIT_VIEW) 
20927 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  || !defined(SQLI
20928 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29  TE_OMIT_TRIGGER)
20929 20 5c 0a 20 7c 7c 20 21 64 65 66 69 6e 65 64 28   \. || !defined(
2092a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
2092b 55 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 49  UERY).SQLITE_PRI
2092c 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71  VATE SrcList *sq
2092d 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28  lite3SrcListDup(
2092e 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 72 63  sqlite3 *db, Src
2092f 4c 69 73 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61  List *p, int fla
20930 67 73 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a  gs){.  SrcList *
20931 70 4e 65 77 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  pNew;.  int i;. 
20932 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69 66   int nByte;.  if
20933 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( p==0 ) return 
20934 30 3b 0a 20 20 6e 42 79 74 65 20 3d 20 73 69 7a  0;.  nByte = siz
20935 65 6f 66 28 2a 70 29 20 2b 20 28 70 2d 3e 6e 53  eof(*p) + (p->nS
20936 72 63 3e 30 20 3f 20 73 69 7a 65 6f 66 28 70 2d  rc>0 ? sizeof(p-
20937 3e 61 5b 30 5d 29 20 2a 20 28 70 2d 3e 6e 53 72  >a[0]) * (p->nSr
20938 63 2d 31 29 20 3a 20 30 29 3b 0a 20 20 70 4e 65  c-1) : 0);.  pNe
20939 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  w = sqlite3DbMal
2093a 6c 6f 63 52 61 77 28 64 62 2c 20 6e 42 79 74 65  locRaw(db, nByte
2093b 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d   );.  if( pNew==
2093c 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
2093d 70 4e 65 77 2d 3e 6e 53 72 63 20 3d 20 70 4e 65  pNew->nSrc = pNe
2093e 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e  w->nAlloc = p->n
2093f 53 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  Src;.  for(i=0; 
20940 69 3c 70 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b  i<p->nSrc; i++){
20941 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c  .    struct SrcL
20942 69 73 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74  ist_item *pNewIt
20943 65 6d 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d  em = &pNew->a[i]
20944 3b 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63  ;.    struct Src
20945 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49  List_item *pOldI
20946 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a  tem = &p->a[i];.
20947 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b      Table *pTab;
20948 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a  .    pNewItem->z
20949 44 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74  Database = sqlit
2094a 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70  e3DbStrDup(db, p
2094b 4f 6c 64 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61  OldItem->zDataba
2094c 73 65 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65  se);.    pNewIte
2094d 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  m->zName = sqlit
2094e 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70  e3DbStrDup(db, p
2094f 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b  OldItem->zName);
20950 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a  .    pNewItem->z
20951 41 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 33 44  Alias = sqlite3D
20952 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64  bStrDup(db, pOld
20953 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20  Item->zAlias);. 
20954 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 6a 6f 69     pNewItem->joi
20955 6e 74 79 70 65 20 3d 20 70 4f 6c 64 49 74 65 6d  ntype = pOldItem
20956 2d 3e 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 20 20  ->jointype;.    
20957 70 4e 65 77 49 74 65 6d 2d 3e 69 43 75 72 73 6f  pNewItem->iCurso
20958 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 43  r = pOldItem->iC
20959 75 72 73 6f 72 3b 0a 20 20 20 20 70 4e 65 77 49  ursor;.    pNewI
2095a 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64  tem->isPopulated
2095b 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 73 50   = pOldItem->isP
2095c 6f 70 75 6c 61 74 65 64 3b 0a 20 20 20 20 70 4e  opulated;.    pN
2095d 65 77 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 20 3d  ewItem->zIndex =
2095e 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
2095f 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a  (db, pOldItem->z
20960 49 6e 64 65 78 29 3b 0a 20 20 20 20 70 4e 65 77  Index);.    pNew
20961 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64  Item->notIndexed
20962 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 6e 6f 74   = pOldItem->not
20963 49 6e 64 65 78 65 64 3b 0a 20 20 20 20 70 4e 65  Indexed;.    pNe
20964 77 49 74 65 6d 2d 3e 70 49 6e 64 65 78 20 3d 20  wItem->pIndex = 
20965 70 4f 6c 64 49 74 65 6d 2d 3e 70 49 6e 64 65 78  pOldItem->pIndex
20966 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 70 4e 65  ;.    pTab = pNe
20967 77 49 74 65 6d 2d 3e 70 54 61 62 20 3d 20 70 4f  wItem->pTab = pO
20968 6c 64 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20  ldItem->pTab;.  
20969 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20    if( pTab ){.  
2096a 20 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 2b 2b      pTab->nRef++
2096b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77  ;.    }.    pNew
2096c 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20  Item->pSelect = 
2096d 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70  sqlite3SelectDup
2096e 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 70  (db, pOldItem->p
2096f 53 65 6c 65 63 74 2c 20 66 6c 61 67 73 29 3b 0a  Select, flags);.
20970 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 4f      pNewItem->pO
20971 6e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  n = sqlite3ExprD
20972 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d  up(db, pOldItem-
20973 3e 70 4f 6e 2c 20 66 6c 61 67 73 29 3b 0a 20 20  >pOn, flags);.  
20974 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 55 73 69    pNewItem->pUsi
20975 6e 67 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69  ng = sqlite3IdLi
20976 73 74 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74  stDup(db, pOldIt
20977 65 6d 2d 3e 70 55 73 69 6e 67 29 3b 0a 20 20 20  em->pUsing);.   
20978 20 70 4e 65 77 49 74 65 6d 2d 3e 63 6f 6c 55 73   pNewItem->colUs
20979 65 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 63  ed = pOldItem->c
2097a 6f 6c 55 73 65 64 3b 0a 20 20 7d 0a 20 20 72 65  olUsed;.  }.  re
2097b 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c  turn pNew;.}.SQL
2097c 49 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c 69  ITE_PRIVATE IdLi
2097d 73 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 73  st *sqlite3IdLis
2097e 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62  tDup(sqlite3 *db
2097f 2c 20 49 64 4c 69 73 74 20 2a 70 29 7b 0a 20 20  , IdList *p){.  
20980 49 64 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20  IdList *pNew;.  
20981 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d  int i;.  if( p==
20982 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
20983 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62  pNew = sqlite3Db
20984 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69  MallocRaw(db, si
20985 7a 65 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20  zeof(*pNew) );. 
20986 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72   if( pNew==0 ) r
20987 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d  eturn 0;.  pNew-
20988 3e 6e 49 64 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c  >nId = pNew->nAl
20989 6c 6f 63 20 3d 20 70 2d 3e 6e 49 64 3b 0a 20 20  loc = p->nId;.  
2098a 70 4e 65 77 2d 3e 61 20 3d 20 73 71 6c 69 74 65  pNew->a = sqlite
2098b 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
2098c 20 70 2d 3e 6e 49 64 2a 73 69 7a 65 6f 66 28 70   p->nId*sizeof(p
2098d 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28  ->a[0]) );.  if(
2098e 20 70 4e 65 77 2d 3e 61 3d 3d 30 20 29 7b 0a 20   pNew->a==0 ){. 
2098f 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
20990 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20  (db, pNew);.    
20991 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
20992 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 49  for(i=0; i<p->nI
20993 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72  d; i++){.    str
20994 75 63 74 20 49 64 4c 69 73 74 5f 69 74 65 6d 20  uct IdList_item 
20995 2a 70 4e 65 77 49 74 65 6d 20 3d 20 26 70 4e 65  *pNewItem = &pNe
20996 77 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 74 72  w->a[i];.    str
20997 75 63 74 20 49 64 4c 69 73 74 5f 69 74 65 6d 20  uct IdList_item 
20998 2a 70 4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d 3e  *pOldItem = &p->
20999 61 5b 69 5d 3b 0a 20 20 20 20 70 4e 65 77 49 74  a[i];.    pNewIt
2099a 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69  em->zName = sqli
2099b 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
2099c 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29  pOldItem->zName)
2099d 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e  ;.    pNewItem->
2099e 69 64 78 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e  idx = pOldItem->
2099f 69 64 78 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  idx;.  }.  retur
209a0 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45  n pNew;.}.SQLITE
209a1 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63 74 20  _PRIVATE Select 
209a2 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75  *sqlite3SelectDu
209a3 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53  p(sqlite3 *db, S
209a4 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 66 6c  elect *p, int fl
209a5 61 67 73 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a  ags){.  Select *
209a6 70 4e 65 77 3b 0a 20 20 69 66 28 20 70 3d 3d 30  pNew;.  if( p==0
209a7 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70   ) return 0;.  p
209a8 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  New = sqlite3DbM
209a9 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a  allocRaw(db, siz
209aa 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 69 66 28  eof(*p) );.  if(
209ab 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72   pNew==0 ) retur
209ac 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 70 45 4c  n 0;.  pNew->pEL
209ad 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ist = sqlite3Exp
209ae 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e  rListDup(db, p->
209af 70 45 4c 69 73 74 2c 20 66 6c 61 67 73 29 3b 0a  pEList, flags);.
209b0 20 20 70 4e 65 77 2d 3e 70 53 72 63 20 3d 20 73    pNew->pSrc = s
209b1 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70  qlite3SrcListDup
209b2 28 64 62 2c 20 70 2d 3e 70 53 72 63 2c 20 66 6c  (db, p->pSrc, fl
209b3 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 57  ags);.  pNew->pW
209b4 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78  here = sqlite3Ex
209b5 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 57 68  prDup(db, p->pWh
209b6 65 72 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70  ere, flags);.  p
209b7 4e 65 77 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20  New->pGroupBy = 
209b8 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
209b9 75 70 28 64 62 2c 20 70 2d 3e 70 47 72 6f 75 70  up(db, p->pGroup
209ba 42 79 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e  By, flags);.  pN
209bb 65 77 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 71  ew->pHaving = sq
209bc 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
209bd 20 70 2d 3e 70 48 61 76 69 6e 67 2c 20 66 6c 61   p->pHaving, fla
209be 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 72  gs);.  pNew->pOr
209bf 64 65 72 42 79 20 3d 20 73 71 6c 69 74 65 33 45  derBy = sqlite3E
209c0 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70  xprListDup(db, p
209c1 2d 3e 70 4f 72 64 65 72 42 79 2c 20 66 6c 61 67  ->pOrderBy, flag
209c2 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d  s);.  pNew->op =
209c3 20 70 2d 3e 6f 70 3b 0a 20 20 70 4e 65 77 2d 3e   p->op;.  pNew->
209c4 70 50 72 69 6f 72 20 3d 20 73 71 6c 69 74 65 33  pPrior = sqlite3
209c5 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 2d  SelectDup(db, p-
209c6 3e 70 50 72 69 6f 72 2c 20 66 6c 61 67 73 29 3b  >pPrior, flags);
209c7 0a 20 20 70 4e 65 77 2d 3e 70 4c 69 6d 69 74 20  .  pNew->pLimit 
209c8 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
209c9 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 2c 20  (db, p->pLimit, 
209ca 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e  flags);.  pNew->
209cb 70 4f 66 66 73 65 74 20 3d 20 73 71 6c 69 74 65  pOffset = sqlite
209cc 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e  3ExprDup(db, p->
209cd 70 4f 66 66 73 65 74 2c 20 66 6c 61 67 73 29 3b  pOffset, flags);
209ce 0a 20 20 70 4e 65 77 2d 3e 69 4c 69 6d 69 74 20  .  pNew->iLimit 
209cf 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 69 4f 66  = 0;.  pNew->iOf
209d0 66 73 65 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77  fset = 0;.  pNew
209d1 2d 3e 73 65 6c 46 6c 61 67 73 20 3d 20 70 2d 3e  ->selFlags = p->
209d2 73 65 6c 46 6c 61 67 73 20 26 20 7e 53 46 5f 55  selFlags & ~SF_U
209d3 73 65 73 45 70 68 65 6d 65 72 61 6c 3b 0a 20 20  sesEphemeral;.  
209d4 70 4e 65 77 2d 3e 70 52 69 67 68 74 6d 6f 73 74  pNew->pRightmost
209d5 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 61 64   = 0;.  pNew->ad
209d6 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20  drOpenEphm[0] = 
209d7 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72  -1;.  pNew->addr
209d8 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d 31  OpenEphm[1] = -1
209d9 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70  ;.  pNew->addrOp
209da 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a  enEphm[2] = -1;.
209db 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d    return pNew;.}
209dc 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52  .#else.SQLITE_PR
209dd 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71  IVATE Select *sq
209de 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 73  lite3SelectDup(s
209df 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c 65  qlite3 *db, Sele
209e0 63 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73  ct *p, int flags
209e1 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d  ){.  assert( p==
209e2 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b  0 );.  return 0;
209e3 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  .}.#endif.../*.*
209e4 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65 6d  * Add a new elem
209e5 65 6e 74 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  ent to the end o
209e6 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  f an expression 
209e7 6c 69 73 74 2e 20 20 49 66 20 70 4c 69 73 74 20  list.  If pList 
209e8 69 73 0a 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 20  is.** initially 
209e9 4e 55 4c 4c 2c 20 74 68 65 6e 20 63 72 65 61 74  NULL, then creat
209ea 65 20 61 20 6e 65 77 20 65 78 70 72 65 73 73 69  e a new expressi
209eb 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49  on list..**.** I
209ec 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  f a memory alloc
209ed 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75  ation error occu
209ee 72 73 2c 20 74 68 65 20 65 6e 74 69 72 65 20 6c  rs, the entire l
209ef 69 73 74 20 69 73 20 66 72 65 65 64 20 61 6e 64  ist is freed and
209f0 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  .** NULL is retu
209f1 72 6e 65 64 2e 20 20 49 66 20 6e 6f 6e 2d 4e 55  rned.  If non-NU
209f2 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20  LL is returned, 
209f3 74 68 65 6e 20 69 74 20 69 73 20 67 75 61 72 61  then it is guara
209f4 6e 74 65 65 64 0a 2a 2a 20 74 68 61 74 20 74 68  nteed.** that th
209f5 65 20 6e 65 77 20 65 6e 74 72 79 20 77 61 73 20  e new entry was 
209f6 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 70 70  successfully app
209f7 65 6e 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ended..*/.SQLITE
209f8 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73  _PRIVATE ExprLis
209f9 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69  t *sqlite3ExprLi
209fa 73 74 41 70 70 65 6e 64 28 0a 20 20 50 61 72 73  stAppend(.  Pars
209fb 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
209fc 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
209fd 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72  ontext */.  Expr
209fe 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20  List *pList,    
209ff 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 77      /* List to w
20a00 68 69 63 68 20 74 6f 20 61 70 70 65 6e 64 2e 20  hich to append. 
20a01 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f  Might be NULL */
20a02 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 20  .  Expr *pExpr  
20a03 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78             /* Ex
20a04 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 61  pression to be a
20a05 70 70 65 6e 64 65 64 2e 20 4d 69 67 68 74 20 62  ppended. Might b
20a06 65 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 73  e NULL */.){.  s
20a07 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
20a08 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70  rse->db;.  if( p
20a09 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70  List==0 ){.    p
20a0a 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62  List = sqlite3Db
20a0b 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
20a0c 69 7a 65 6f 66 28 45 78 70 72 4c 69 73 74 29 20  izeof(ExprList) 
20a0d 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74  );.    if( pList
20a0e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  ==0 ){.      got
20a0f 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a  o no_mem;.    }.
20a10 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73      assert( pLis
20a11 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a  t->nAlloc==0 );.
20a12 20 20 7d 0a 20 20 69 66 28 20 70 4c 69 73 74 2d    }.  if( pList-
20a13 3e 6e 41 6c 6c 6f 63 3c 3d 70 4c 69 73 74 2d 3e  >nAlloc<=pList->
20a14 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 73 74 72  nExpr ){.    str
20a15 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
20a16 6d 20 2a 61 3b 0a 20 20 20 20 69 6e 74 20 6e 20  m *a;.    int n 
20a17 3d 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2a  = pList->nAlloc*
20a18 32 20 2b 20 34 3b 0a 20 20 20 20 61 20 3d 20 73  2 + 4;.    a = s
20a19 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28  qlite3DbRealloc(
20a1a 64 62 2c 20 70 4c 69 73 74 2d 3e 61 2c 20 6e 2a  db, pList->a, n*
20a1b 73 69 7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b  sizeof(pList->a[
20a1c 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 61 3d  0]));.    if( a=
20a1d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  =0 ){.      goto
20a1e 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20   no_mem;.    }. 
20a1f 20 20 20 70 4c 69 73 74 2d 3e 61 20 3d 20 61 3b     pList->a = a;
20a20 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c  .    pList->nAll
20a21 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  oc = sqlite3DbMa
20a22 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 61 29 2f  llocSize(db, a)/
20a23 73 69 7a 65 6f 66 28 61 5b 30 5d 29 3b 0a 20 20  sizeof(a[0]);.  
20a24 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73  }.  assert( pLis
20a25 74 2d 3e 61 21 3d 30 20 29 3b 0a 20 20 69 66 28  t->a!=0 );.  if(
20a26 20 31 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74   1 ){.    struct
20a27 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
20a28 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e  pItem = &pList->
20a29 61 5b 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2b 2b  a[pList->nExpr++
20a2a 5d 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 49  ];.    memset(pI
20a2b 74 65 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a  tem, 0, sizeof(*
20a2c 70 49 74 65 6d 29 29 3b 0a 20 20 20 20 70 49 74  pItem));.    pIt
20a2d 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 45 78 70  em->pExpr = pExp
20a2e 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  r;.  }.  return 
20a2f 70 4c 69 73 74 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 20  pList;..no_mem: 
20a30 20 20 20 20 0a 20 20 2f 2a 20 41 76 6f 69 64 20      .  /* Avoid 
20a31 6c 65 61 6b 69 6e 67 20 6d 65 6d 6f 72 79 20 69  leaking memory i
20a32 66 20 6d 61 6c 6c 6f 63 20 68 61 73 20 66 61 69  f malloc has fai
20a33 6c 65 64 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  led. */.  sqlite
20a34 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
20a35 70 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 65  pExpr);.  sqlite
20a36 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
20a37 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 72 65  db, pList);.  re
20a38 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
20a39 20 53 65 74 20 74 68 65 20 45 78 70 72 4c 69 73   Set the ExprLis
20a3a 74 2e 61 5b 5d 2e 7a 4e 61 6d 65 20 65 6c 65 6d  t.a[].zName elem
20a3b 65 6e 74 20 6f 66 20 74 68 65 20 6d 6f 73 74 20  ent of the most 
20a3c 72 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 69  recently added i
20a3d 74 65 6d 0a 2a 2a 20 6f 6e 20 74 68 65 20 65 78  tem.** on the ex
20a3e 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a  pression list..*
20a3f 2a 0a 2a 2a 20 70 4c 69 73 74 20 6d 69 67 68 74  *.** pList might
20a40 20 62 65 20 4e 55 4c 4c 20 66 6f 6c 6c 6f 77 69   be NULL followi
20a41 6e 67 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e  ng an OOM error.
20a42 20 20 42 75 74 20 70 4e 61 6d 65 20 73 68 6f 75    But pName shou
20a43 6c 64 20 6e 65 76 65 72 20 62 65 0a 2a 2a 20 4e  ld never be.** N
20a44 55 4c 4c 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72  ULL.  If a memor
20a45 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69  y allocation fai
20a46 6c 73 2c 20 74 68 65 20 70 50 61 72 73 65 2d 3e  ls, the pParse->
20a47 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
20a48 20 66 6c 61 67 0a 2a 2a 20 69 73 20 73 65 74 2e   flag.** is set.
20a49 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20a4a 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
20a4b 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 0a  xprListSetName(.
20a4c 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
20a4d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
20a4e 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
20a4f 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
20a50 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73  t,        /* Lis
20a51 74 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61 64  t to which to ad
20a52 64 20 74 68 65 20 73 70 61 6e 2e 20 2a 2f 0a 20  d the span. */. 
20a53 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 2c 20 20   Token *pName,  
20a54 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
20a55 20 74 6f 20 62 65 20 61 64 64 65 64 20 2a 2f 0a   to be added */.
20a56 20 20 69 6e 74 20 64 65 71 75 6f 74 65 20 20 20    int dequote   
20a57 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
20a58 65 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 6e  e to cause the n
20a59 61 6d 65 20 74 6f 20 62 65 20 64 65 71 75 6f 74  ame to be dequot
20a5a 65 64 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72  ed */.){.  asser
20a5b 74 28 20 70 4c 69 73 74 21 3d 30 20 7c 7c 20 70  t( pList!=0 || p
20a5c 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
20a5d 63 46 61 69 6c 65 64 21 3d 30 20 29 3b 0a 20 20  cFailed!=0 );.  
20a5e 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20  if( pList ){.   
20a5f 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
20a60 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20  _item *pItem;.  
20a61 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d    assert( pList-
20a62 3e 6e 45 78 70 72 3e 30 20 29 3b 0a 20 20 20 20  >nExpr>0 );.    
20a63 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e  pItem = &pList->
20a64 61 5b 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31  a[pList->nExpr-1
20a65 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ];.    assert( p
20a66 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 29  Item->zName==0 )
20a67 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61  ;.    pItem->zNa
20a68 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  me = sqlite3DbSt
20a69 72 4e 44 75 70 28 70 50 61 72 73 65 2d 3e 64 62  rNDup(pParse->db
20a6a 2c 20 70 4e 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d  , pName->z, pNam
20a6b 65 2d 3e 6e 29 3b 0a 20 20 20 20 69 66 28 20 64  e->n);.    if( d
20a6c 65 71 75 6f 74 65 20 26 26 20 70 49 74 65 6d 2d  equote && pItem-
20a6d 3e 7a 4e 61 6d 65 20 29 20 73 71 6c 69 74 65 33  >zName ) sqlite3
20a6e 44 65 71 75 6f 74 65 28 70 49 74 65 6d 2d 3e 7a  Dequote(pItem->z
20a6f 4e 61 6d 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  Name);.  }.}../*
20a70 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72  .** Set the Expr
20a71 4c 69 73 74 2e 61 5b 5d 2e 7a 53 70 61 6e 20 65  List.a[].zSpan e
20a72 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 6d 6f  lement of the mo
20a73 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65  st recently adde
20a74 64 20 69 74 65 6d 0a 2a 2a 20 6f 6e 20 74 68 65  d item.** on the
20a75 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
20a76 2e 0a 2a 2a 0a 2a 2a 20 70 4c 69 73 74 20 6d 69  ..**.** pList mi
20a77 67 68 74 20 62 65 20 4e 55 4c 4c 20 66 6f 6c 6c  ght be NULL foll
20a78 6f 77 69 6e 67 20 61 6e 20 4f 4f 4d 20 65 72 72  owing an OOM err
20a79 6f 72 2e 20 20 42 75 74 20 70 53 70 61 6e 20 73  or.  But pSpan s
20a7a 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 0a 2a  hould never be.*
20a7b 2a 20 4e 55 4c 4c 2e 20 20 49 66 20 61 20 6d 65  * NULL.  If a me
20a7c 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
20a7d 66 61 69 6c 73 2c 20 74 68 65 20 70 50 61 72 73  fails, the pPars
20a7e 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  e->db->mallocFai
20a7f 6c 65 64 20 66 6c 61 67 0a 2a 2a 20 69 73 20 73  led flag.** is s
20a80 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  et..*/.SQLITE_PR
20a81 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
20a82 65 33 45 78 70 72 4c 69 73 74 53 65 74 53 70 61  e3ExprListSetSpa
20a83 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  n(.  Parse *pPar
20a84 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  se,          /* 
20a85 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
20a86 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
20a87 4c 69 73 74 2c 20 20 20 20 20 20 20 20 2f 2a 20  List,        /* 
20a88 4c 69 73 74 20 74 6f 20 77 68 69 63 68 20 74 6f  List to which to
20a89 20 61 64 64 20 74 68 65 20 73 70 61 6e 2e 20 2a   add the span. *
20a8a 2f 0a 20 20 45 78 70 72 53 70 61 6e 20 2a 70 53  /.  ExprSpan *pS
20a8b 70 61 6e 20 20 20 20 20 20 20 20 20 2f 2a 20 54  pan         /* T
20a8c 68 65 20 73 70 61 6e 20 74 6f 20 62 65 20 61 64  he span to be ad
20a8d 64 65 64 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  ded */.){.  sqli
20a8e 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
20a8f 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20  ->db;.  assert( 
20a90 70 4c 69 73 74 21 3d 30 20 7c 7c 20 64 62 2d 3e  pList!=0 || db->
20a91 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 21 3d 30 20  mallocFailed!=0 
20a92 29 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 20 29  );.  if( pList )
20a93 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45 78 70  {.    struct Exp
20a94 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  rList_item *pIte
20a95 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c  m = &pList->a[pL
20a96 69 73 74 2d 3e 6e 45 78 70 72 2d 31 5d 3b 0a 20  ist->nExpr-1];. 
20a97 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74     assert( pList
20a98 2d 3e 6e 45 78 70 72 3e 30 20 29 3b 0a 20 20 20  ->nExpr>0 );.   
20a99 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c   assert( db->mal
20a9a 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 70 49 74  locFailed || pIt
20a9b 65 6d 2d 3e 70 45 78 70 72 3d 3d 70 53 70 61 6e  em->pExpr==pSpan
20a9c 2d 3e 70 45 78 70 72 20 29 3b 0a 20 20 20 20 73  ->pExpr );.    s
20a9d 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
20a9e 20 70 49 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b 0a   pItem->zSpan);.
20a9f 20 20 20 20 70 49 74 65 6d 2d 3e 7a 53 70 61 6e      pItem->zSpan
20aa0 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e   = sqlite3DbStrN
20aa1 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70  Dup(db, (char*)p
20aa2 53 70 61 6e 2d 3e 7a 53 74 61 72 74 2c 0a 20 20  Span->zStart,.  
20aa3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20aa4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20aa5 20 20 28 69 6e 74 29 28 70 53 70 61 6e 2d 3e 7a    (int)(pSpan->z
20aa6 45 6e 64 20 2d 20 70 53 70 61 6e 2d 3e 7a 53 74  End - pSpan->zSt
20aa7 61 72 74 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  art));.  }.}../*
20aa8 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65  .** If the expre
20aa9 73 73 69 6f 6e 20 6c 69 73 74 20 70 45 4c 69 73  ssion list pELis
20aaa 74 20 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65 20  t contains more 
20aab 74 68 61 6e 20 69 4c 69 6d 69 74 20 65 6c 65 6d  than iLimit elem
20aac 65 6e 74 73 2c 0a 2a 2a 20 6c 65 61 76 65 20 61  ents,.** leave a
20aad 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
20aae 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51  in pParse..*/.SQ
20aaf 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
20ab0 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  d sqlite3ExprLis
20ab1 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 0a 20 20  tCheckLength(.  
20ab2 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 0a 20  Parse *pParse,. 
20ab3 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
20ab4 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  t,.  const char 
20ab5 2a 7a 4f 62 6a 65 63 74 0a 29 7b 0a 20 20 69 6e  *zObject.){.  in
20ab6 74 20 6d 78 20 3d 20 70 50 61 72 73 65 2d 3e 64  t mx = pParse->d
20ab7 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
20ab8 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 3b 0a  _LIMIT_COLUMN];.
20ab9 20 20 74 65 73 74 63 61 73 65 28 20 70 45 4c 69    testcase( pELi
20aba 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45  st && pEList->nE
20abb 78 70 72 3d 3d 6d 78 20 29 3b 0a 20 20 74 65 73  xpr==mx );.  tes
20abc 74 63 61 73 65 28 20 70 45 4c 69 73 74 20 26 26  tcase( pEList &&
20abd 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d   pEList->nExpr==
20abe 6d 78 2b 31 20 29 3b 0a 20 20 69 66 28 20 70 45  mx+1 );.  if( pE
20abf 4c 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e  List && pEList->
20ac0 6e 45 78 70 72 3e 6d 78 20 29 7b 0a 20 20 20 20  nExpr>mx ){.    
20ac1 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
20ac2 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e  pParse, "too man
20ac3 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 25 73 22  y columns in %s"
20ac4 2c 20 7a 4f 62 6a 65 63 74 29 3b 0a 20 20 7d 0a  , zObject);.  }.
20ac5 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  }../*.** Delete 
20ac6 61 6e 20 65 6e 74 69 72 65 20 65 78 70 72 65 73  an entire expres
20ac7 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51  sion list..*/.SQ
20ac8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
20ac9 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  d sqlite3ExprLis
20aca 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20  tDelete(sqlite3 
20acb 2a 64 62 2c 20 45 78 70 72 4c 69 73 74 20 2a 70  *db, ExprList *p
20acc 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  List){.  int i;.
20acd 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
20ace 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20  t_item *pItem;. 
20acf 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20   if( pList==0 ) 
20ad0 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74  return;.  assert
20ad1 28 20 70 4c 69 73 74 2d 3e 61 21 3d 30 20 7c 7c  ( pList->a!=0 ||
20ad2 20 28 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d   (pList->nExpr==
20ad3 30 20 26 26 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c  0 && pList->nAll
20ad4 6f 63 3d 3d 30 29 20 29 3b 0a 20 20 61 73 73 65  oc==0) );.  asse
20ad5 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72  rt( pList->nExpr
20ad6 3c 3d 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20  <=pList->nAlloc 
20ad7 29 3b 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70  );.  for(pItem=p
20ad8 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c  List->a, i=0; i<
20ad9 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b  pList->nExpr; i+
20ada 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  +, pItem++){.   
20adb 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
20adc 74 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 45  te(db, pItem->pE
20add 78 70 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  xpr);.    sqlite
20ade 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 74 65  3DbFree(db, pIte
20adf 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73  m->zName);.    s
20ae0 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
20ae1 20 70 49 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b 0a   pItem->zSpan);.
20ae2 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46    }.  sqlite3DbF
20ae3 72 65 65 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61  ree(db, pList->a
20ae4 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
20ae5 65 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 7d  ee(db, pList);.}
20ae6 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  ../*.** These ro
20ae7 75 74 69 6e 65 73 20 61 72 65 20 57 61 6c 6b 65  utines are Walke
20ae8 72 20 63 61 6c 6c 62 61 63 6b 73 2e 20 20 57 61  r callbacks.  Wa
20ae9 6c 6b 65 72 2e 75 2e 70 69 20 69 73 20 61 20 70  lker.u.pi is a p
20aea 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e 20  ointer.** to an 
20aeb 69 6e 74 65 67 65 72 2e 20 20 54 68 65 73 65 20  integer.  These 
20aec 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 68 65  routines are che
20aed 63 6b 69 6e 67 20 61 6e 20 65 78 70 72 65 73 73  cking an express
20aee 69 6f 6e 20 74 6f 20 73 65 65 0a 2a 2a 20 69 66  ion to see.** if
20aef 20 69 74 20 69 73 20 61 20 63 6f 6e 73 74 61 6e   it is a constan
20af0 74 2e 20 20 53 65 74 20 2a 57 61 6c 6b 65 72 2e  t.  Set *Walker.
20af1 75 2e 70 69 20 74 6f 20 30 20 69 66 20 74 68 65  u.pi to 0 if the
20af2 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 0a 2a   expression is.*
20af3 2a 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 2e 0a  * not constant..
20af4 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 61 6c 6c  **.** These call
20af5 62 61 63 6b 20 72 6f 75 74 69 6e 65 73 20 61 72  back routines ar
20af6 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d  e used to implem
20af7 65 6e 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ent the followin
20af8 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c  g:.**.**     sql
20af9 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61  ite3ExprIsConsta
20afa 6e 74 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69  nt().**     sqli
20afb 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e  te3ExprIsConstan
20afc 74 4e 6f 74 4a 6f 69 6e 28 29 0a 2a 2a 20 20 20  tNotJoin().**   
20afd 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43    sqlite3ExprIsC
20afe 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f  onstantOrFunctio
20aff 6e 28 29 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63  n().**.*/.static
20b00 20 69 6e 74 20 65 78 70 72 4e 6f 64 65 49 73 43   int exprNodeIsC
20b01 6f 6e 73 74 61 6e 74 28 57 61 6c 6b 65 72 20 2a  onstant(Walker *
20b02 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70  pWalker, Expr *p
20b03 45 78 70 72 29 7b 0a 0a 20 20 2f 2a 20 49 66 20  Expr){..  /* If 
20b04 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 69 73 20  pWalker->u.i is 
20b05 33 20 74 68 65 6e 20 61 6e 79 20 74 65 72 6d 20  3 then any term 
20b06 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  of the expressio
20b07 6e 20 74 68 61 74 20 63 6f 6d 65 73 20 66 72 6f  n that comes fro
20b08 6d 0a 20 20 2a 2a 20 74 68 65 20 4f 4e 20 6f 72  m.  ** the ON or
20b09 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 6f   USING clauses o
20b0a 66 20 61 20 6a 6f 69 6e 20 64 69 73 71 75 61 6c  f a join disqual
20b0b 69 66 69 65 73 20 74 68 65 20 65 78 70 72 65 73  ifies the expres
20b0c 73 69 6f 6e 0a 20 20 2a 2a 20 66 72 6f 6d 20 62  sion.  ** from b
20b0d 65 69 6e 67 20 63 6f 6e 73 69 64 65 72 65 64 20  eing considered 
20b0e 63 6f 6e 73 74 61 6e 74 2e 20 2a 2f 0a 20 20 69  constant. */.  i
20b0f 66 28 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d  f( pWalker->u.i=
20b10 3d 33 20 26 26 20 45 78 70 72 48 61 73 41 6e 79  =3 && ExprHasAny
20b11 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
20b12 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a  EP_FromJoin) ){.
20b13 20 20 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69      pWalker->u.i
20b14 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 0;.    return
20b15 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a   WRC_Abort;.  }.
20b16 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72  .  switch( pExpr
20b17 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43  ->op ){.    /* C
20b18 6f 6e 73 69 64 65 72 20 66 75 6e 63 74 69 6f 6e  onsider function
20b19 73 20 74 6f 20 62 65 20 63 6f 6e 73 74 61 6e 74  s to be constant
20b1a 20 69 66 20 61 6c 6c 20 74 68 65 69 72 20 61 72   if all their ar
20b1b 67 75 6d 65 6e 74 73 20 61 72 65 20 63 6f 6e 73  guments are cons
20b1c 74 61 6e 74 0a 20 20 20 20 2a 2a 20 61 6e 64 20  tant.    ** and 
20b1d 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 32 20  pWalker->u.i==2 
20b1e 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46  */.    case TK_F
20b1f 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 20 20 69  UNCTION:.      i
20b20 66 28 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d  f( pWalker->u.i=
20b21 3d 32 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =2 ) return 0;. 
20b22 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72       /* Fall thr
20b23 6f 75 67 68 20 2a 2f 0a 20 20 20 20 63 61 73 65  ough */.    case
20b24 20 54 4b 5f 49 44 3a 0a 20 20 20 20 63 61 73 65   TK_ID:.    case
20b25 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20   TK_COLUMN:.    
20b26 63 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43  case TK_AGG_FUNC
20b27 54 49 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20 54  TION:.    case T
20b28 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20  K_AGG_COLUMN:.  
20b29 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45      testcase( pE
20b2a 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29  xpr->op==TK_ID )
20b2b 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
20b2c 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
20b2d 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20  COLUMN );.      
20b2e 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d  testcase( pExpr-
20b2f 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 46 55 4e 43  >op==TK_AGG_FUNC
20b30 54 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 74 65  TION );.      te
20b31 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f  stcase( pExpr->o
20b32 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  p==TK_AGG_COLUMN
20b33 20 29 3b 0a 20 20 20 20 20 20 70 57 61 6c 6b 65   );.      pWalke
20b34 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 20 20  r->u.i = 0;.    
20b35 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f    return WRC_Abo
20b36 72 74 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a  rt;.    default:
20b37 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
20b38 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53   pExpr->op==TK_S
20b39 45 4c 45 43 54 20 29 3b 20 2f 2a 20 73 65 6c 65  ELECT ); /* sele
20b3a 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74  ctNodeIsConstant
20b3b 20 77 69 6c 6c 20 64 69 73 61 6c 6c 6f 77 20 2a   will disallow *
20b3c 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
20b3d 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
20b3e 45 58 49 53 54 53 20 29 3b 20 2f 2a 20 73 65 6c  EXISTS ); /* sel
20b3f 65 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e  ectNodeIsConstan
20b40 74 20 77 69 6c 6c 20 64 69 73 61 6c 6c 6f 77 20  t will disallow 
20b41 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  */.      return 
20b42 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20  WRC_Continue;.  
20b43 7d 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73  }.}.static int s
20b44 65 6c 65 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74  electNodeIsConst
20b45 61 6e 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c  ant(Walker *pWal
20b46 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 4e 6f 74  ker, Select *Not
20b47 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f  Used){.  UNUSED_
20b48 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
20b49 64 29 3b 0a 20 20 70 57 61 6c 6b 65 72 2d 3e 75  d);.  pWalker->u
20b4a 2e 69 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e  .i = 0;.  return
20b4b 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 73 74   WRC_Abort;.}.st
20b4c 61 74 69 63 20 69 6e 74 20 65 78 70 72 49 73 43  atic int exprIsC
20b4d 6f 6e 73 74 28 45 78 70 72 20 2a 70 2c 20 69 6e  onst(Expr *p, in
20b4e 74 20 69 6e 69 74 46 6c 61 67 29 7b 0a 20 20 57  t initFlag){.  W
20b4f 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 75 2e 69  alker w;.  w.u.i
20b50 20 3d 20 69 6e 69 74 46 6c 61 67 3b 0a 20 20 77   = initFlag;.  w
20b51 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d  .xExprCallback =
20b52 20 65 78 70 72 4e 6f 64 65 49 73 43 6f 6e 73 74   exprNodeIsConst
20b53 61 6e 74 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74  ant;.  w.xSelect
20b54 43 61 6c 6c 62 61 63 6b 20 3d 20 73 65 6c 65 63  Callback = selec
20b55 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 3b  tNodeIsConstant;
20b56 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78  .  sqlite3WalkEx
20b57 70 72 28 26 77 2c 20 70 29 3b 0a 20 20 72 65 74  pr(&w, p);.  ret
20b58 75 72 6e 20 77 2e 75 2e 69 3b 0a 7d 0a 0a 2f 2a  urn w.u.i;.}../*
20b59 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72  .** Walk an expr
20b5a 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65  ession tree.  Re
20b5b 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 65 78  turn 1 if the ex
20b5c 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73  pression is cons
20b5d 74 61 6e 74 0a 2a 2a 20 61 6e 64 20 30 20 69 66  tant.** and 0 if
20b5e 20 69 74 20 69 6e 76 6f 6c 76 65 73 20 76 61 72   it involves var
20b5f 69 61 62 6c 65 73 20 6f 72 20 66 75 6e 63 74 69  iables or functi
20b60 6f 6e 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20  on calls..**.** 
20b61 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73  For the purposes
20b62 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   of this functio
20b63 6e 2c 20 61 20 64 6f 75 62 6c 65 2d 71 75 6f 74  n, a double-quot
20b64 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 22  ed string (ex: "
20b65 61 62 63 22 29 0a 2a 2a 20 69 73 20 63 6f 6e 73  abc").** is cons
20b66 69 64 65 72 65 64 20 61 20 76 61 72 69 61 62 6c  idered a variabl
20b67 65 20 62 75 74 20 61 20 73 69 6e 67 6c 65 2d 71  e but a single-q
20b68 75 6f 74 65 64 20 73 74 72 69 6e 67 20 28 65 78  uoted string (ex
20b69 3a 20 27 61 62 63 27 29 20 69 73 0a 2a 2a 20 61  : 'abc') is.** a
20b6a 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51   constant..*/.SQ
20b6b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
20b6c 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f   sqlite3ExprIsCo
20b6d 6e 73 74 61 6e 74 28 45 78 70 72 20 2a 70 29 7b  nstant(Expr *p){
20b6e 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 49 73  .  return exprIs
20b6f 43 6f 6e 73 74 28 70 2c 20 31 29 3b 0a 7d 0a 0a  Const(p, 1);.}..
20b70 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78  /*.** Walk an ex
20b71 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20  pression tree.  
20b72 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20  Return 1 if the 
20b73 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f  expression is co
20b74 6e 73 74 61 6e 74 0a 2a 2a 20 74 68 61 74 20 64  nstant.** that d
20b75 6f 65 73 20 6e 6f 20 6f 72 69 67 69 6e 61 74 65  oes no originate
20b76 20 66 72 6f 6d 20 74 68 65 20 4f 4e 20 6f 72 20   from the ON or 
20b77 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 6f 66  USING clauses of
20b78 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 20 52 65 74 75   a join..** Retu
20b79 72 6e 20 30 20 69 66 20 69 74 20 69 6e 76 6f 6c  rn 0 if it invol
20b7a 76 65 73 20 76 61 72 69 61 62 6c 65 73 20 6f 72  ves variables or
20b7b 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20   function calls 
20b7c 6f 72 20 74 65 72 6d 73 20 66 72 6f 6d 0a 2a 2a  or terms from.**
20b7d 20 61 6e 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20   an ON or USING 
20b7e 63 6c 61 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  clause..*/.SQLIT
20b7f 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
20b80 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
20b81 61 6e 74 4e 6f 74 4a 6f 69 6e 28 45 78 70 72 20  antNotJoin(Expr 
20b82 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 78  *p){.  return ex
20b83 70 72 49 73 43 6f 6e 73 74 28 70 2c 20 33 29 3b  prIsConst(p, 3);
20b84 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61  .}../*.** Walk a
20b85 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  n expression tre
20b86 65 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20  e.  Return 1 if 
20b87 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
20b88 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 6f 72  s constant.** or
20b89 20 61 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c   a function call
20b8a 20 77 69 74 68 20 63 6f 6e 73 74 61 6e 74 20 61   with constant a
20b8b 72 67 75 6d 65 6e 74 73 2e 20 20 52 65 74 75 72  rguments.  Retur
20b8c 6e 20 61 6e 64 20 30 20 69 66 20 74 68 65 72 65  n and 0 if there
20b8d 0a 2a 2a 20 61 72 65 20 61 6e 79 20 76 61 72 69  .** are any vari
20b8e 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  ables..**.** For
20b8f 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66   the purposes of
20b90 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20   this function, 
20b91 61 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20  a double-quoted 
20b92 73 74 72 69 6e 67 20 28 65 78 3a 20 22 61 62 63  string (ex: "abc
20b93 22 29 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65  ").** is conside
20b94 72 65 64 20 61 20 76 61 72 69 61 62 6c 65 20 62  red a variable b
20b95 75 74 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74  ut a single-quot
20b96 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 27  ed string (ex: '
20b97 61 62 63 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f  abc') is.** a co
20b98 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  nstant..*/.SQLIT
20b99 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
20b9a 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
20b9b 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 45 78  antOrFunction(Ex
20b9c 70 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  pr *p){.  return
20b9d 20 65 78 70 72 49 73 43 6f 6e 73 74 28 70 2c 20   exprIsConst(p, 
20b9e 32 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  2);.}../*.** If 
20b9f 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70  the expression p
20ba0 20 63 6f 64 65 73 20 61 20 63 6f 6e 73 74 61 6e   codes a constan
20ba1 74 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69  t integer that i
20ba2 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 0a 2a  s small enough.*
20ba3 2a 20 74 6f 20 66 69 74 20 69 6e 20 61 20 33 32  * to fit in a 32
20ba4 2d 62 69 74 20 69 6e 74 65 67 65 72 2c 20 72 65  -bit integer, re
20ba5 74 75 72 6e 20 31 20 61 6e 64 20 70 75 74 20 74  turn 1 and put t
20ba6 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
20ba7 69 6e 74 65 67 65 72 0a 2a 2a 20 69 6e 20 2a 70  integer.** in *p
20ba8 56 61 6c 75 65 2e 20 20 49 66 20 74 68 65 20 65  Value.  If the e
20ba9 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6e 6f 74  xpression is not
20baa 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 69   an integer or i
20bab 66 20 69 74 20 69 73 20 74 6f 6f 20 62 69 67 0a  f it is too big.
20bac 2a 2a 20 74 6f 20 66 69 74 20 69 6e 20 61 20 73  ** to fit in a s
20bad 69 67 6e 65 64 20 33 32 2d 62 69 74 20 69 6e 74  igned 32-bit int
20bae 65 67 65 72 2c 20 72 65 74 75 72 6e 20 30 20 61  eger, return 0 a
20baf 6e 64 20 6c 65 61 76 65 20 2a 70 56 61 6c 75 65  nd leave *pValue
20bb0 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53   unchanged..*/.S
20bb1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
20bb2 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49  t sqlite3ExprIsI
20bb3 6e 74 65 67 65 72 28 45 78 70 72 20 2a 70 2c 20  nteger(Expr *p, 
20bb4 69 6e 74 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20  int *pValue){.  
20bb5 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 69 66  int rc = 0;.  if
20bb6 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  ( p->flags & EP_
20bb7 49 6e 74 56 61 6c 75 65 20 29 7b 0a 20 20 20 20  IntValue ){.    
20bb8 2a 70 56 61 6c 75 65 20 3d 20 70 2d 3e 75 2e 69  *pValue = p->u.i
20bb9 56 61 6c 75 65 3b 0a 20 20 20 20 72 65 74 75 72  Value;.    retur
20bba 6e 20 31 3b 0a 20 20 7d 0a 20 20 73 77 69 74 63  n 1;.  }.  switc
20bbb 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20  h( p->op ){.    
20bbc 63 61 73 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a  case TK_INTEGER:
20bbd 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71   {.      rc = sq
20bbe 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28 70 2d  lite3GetInt32(p-
20bbf 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 70 56 61 6c 75  >u.zToken, pValu
20bc0 65 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  e);.      assert
20bc1 28 20 72 63 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ( rc==0 );.     
20bc2 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
20bc3 20 20 63 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a    case TK_UPLUS:
20bc4 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71   {.      rc = sq
20bc5 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67  lite3ExprIsInteg
20bc6 65 72 28 70 2d 3e 70 4c 65 66 74 2c 20 70 56 61  er(p->pLeft, pVa
20bc7 6c 75 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61  lue);.      brea
20bc8 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
20bc9 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20  e TK_UMINUS: {. 
20bca 20 20 20 20 20 69 6e 74 20 76 3b 0a 20 20 20 20       int v;.    
20bcb 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70    if( sqlite3Exp
20bcc 72 49 73 49 6e 74 65 67 65 72 28 70 2d 3e 70 4c  rIsInteger(p->pL
20bcd 65 66 74 2c 20 26 76 29 20 29 7b 0a 20 20 20 20  eft, &v) ){.    
20bce 20 20 20 20 2a 70 56 61 6c 75 65 20 3d 20 2d 76      *pValue = -v
20bcf 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 31  ;.        rc = 1
20bd0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
20bd1 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
20bd2 20 64 65 66 61 75 6c 74 3a 20 62 72 65 61 6b 3b   default: break;
20bd3 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 20 29 7b  .  }.  if( rc ){
20bd4 0a 20 20 20 20 61 73 73 65 72 74 28 20 45 78 70  .    assert( Exp
20bd5 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28  rHasAnyProperty(
20bd6 70 2c 20 45 50 5f 52 65 64 75 63 65 64 7c 45 50  p, EP_Reduced|EP
20bd7 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 0a 20 20 20 20  _TokenOnly).    
20bd8 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 70             || (p
20bd9 2d 3e 66 6c 61 67 73 32 20 26 20 45 50 32 5f 4d  ->flags2 & EP2_M
20bda 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e 29 3d 3d 30  allocedToken)==0
20bdb 20 29 3b 0a 20 20 20 20 70 2d 3e 6f 70 20 3d 20   );.    p->op = 
20bdc 54 4b 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20  TK_INTEGER;.    
20bdd 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 49  p->flags |= EP_I
20bde 6e 74 56 61 6c 75 65 3b 0a 20 20 20 20 70 2d 3e  ntValue;.    p->
20bdf 75 2e 69 56 61 6c 75 65 20 3d 20 2a 70 56 61 6c  u.iValue = *pVal
20be0 75 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ue;.  }.  return
20be1 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
20be2 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65  turn TRUE if the
20be3 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 69 73   given string is
20be4 20 61 20 72 6f 77 2d 69 64 20 63 6f 6c 75 6d 6e   a row-id column
20be5 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   name..*/.SQLITE
20be6 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
20be7 69 74 65 33 49 73 52 6f 77 69 64 28 63 6f 6e 73  ite3IsRowid(cons
20be8 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 66  t char *z){.  if
20be9 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
20bea 28 7a 2c 20 22 5f 52 4f 57 49 44 5f 22 29 3d 3d  (z, "_ROWID_")==
20beb 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  0 ) return 1;.  
20bec 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
20bed 6d 70 28 7a 2c 20 22 52 4f 57 49 44 22 29 3d 3d  mp(z, "ROWID")==
20bee 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  0 ) return 1;.  
20bef 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
20bf0 6d 70 28 7a 2c 20 22 4f 49 44 22 29 3d 3d 30 20  mp(z, "OID")==0 
20bf1 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65  ) return 1;.  re
20bf2 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
20bf3 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
20bf4 77 65 20 61 72 65 20 61 62 6c 65 20 74 6f 20 74  we are able to t
20bf5 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 6f  he IN operator o
20bf6 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e 20 61  ptimization on a
20bf7 0a 2a 2a 20 71 75 65 72 79 20 6f 66 20 74 68 65  .** query of the
20bf8 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20   form.**.**     
20bf9 20 20 78 20 49 4e 20 28 53 45 4c 45 43 54 20 2e    x IN (SELECT .
20bfa 2e 2e 29 0a 2a 2a 0a 2a 2a 20 57 68 65 72 65 20  ..).**.** Where 
20bfb 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e 20 63 6c  the SELECT... cl
20bfc 61 75 73 65 20 69 73 20 61 73 20 73 70 65 63 69  ause is as speci
20bfd 66 69 65 64 20 62 79 20 74 68 65 20 70 61 72 61  fied by the para
20bfe 6d 65 74 65 72 20 74 6f 20 74 68 69 73 0a 2a 2a  meter to this.**
20bff 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20   routine..**.** 
20c00 54 68 65 20 53 65 6c 65 63 74 20 6f 62 6a 65 63  The Select objec
20c01 74 20 70 61 73 73 65 64 20 69 6e 20 68 61 73 20  t passed in has 
20c02 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70 72 65  already been pre
20c03 70 72 6f 63 65 73 73 65 64 20 61 6e 64 20 6e 6f  processed and no
20c04 0a 2a 2a 20 65 72 72 6f 72 73 20 68 61 76 65 20  .** errors have 
20c05 62 65 65 6e 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 23  been found..*/.#
20c06 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
20c07 49 54 5f 53 55 42 51 55 45 52 59 0a 73 74 61 74  IT_SUBQUERY.stat
20c08 69 63 20 69 6e 74 20 69 73 43 61 6e 64 69 64 61  ic int isCandida
20c09 74 65 46 6f 72 49 6e 4f 70 74 28 53 65 6c 65 63  teForInOpt(Selec
20c0a 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69 73 74  t *p){.  SrcList
20c0b 20 2a 70 53 72 63 3b 0a 20 20 45 78 70 72 4c 69   *pSrc;.  ExprLi
20c0c 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 54 61  st *pEList;.  Ta
20c0d 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 69 66 28  ble *pTab;.  if(
20c0e 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   p==0 ) return 0
20c0f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
20c10 20 20 20 20 2f 2a 20 72 69 67 68 74 2d 68 61 6e      /* right-han
20c11 64 20 73 69 64 65 20 6f 66 20 49 4e 20 69 73 20  d side of IN is 
20c12 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 66 28 20  SELECT */.  if( 
20c13 70 2d 3e 70 50 72 69 6f 72 20 29 20 72 65 74 75  p->pPrior ) retu
20c14 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20  rn 0;           
20c15 20 20 20 2f 2a 20 4e 6f 74 20 61 20 63 6f 6d 70     /* Not a comp
20c16 6f 75 6e 64 20 53 45 4c 45 43 54 20 2a 2f 0a 20  ound SELECT */. 
20c17 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73   if( p->selFlags
20c18 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74 7c   & (SF_Distinct|
20c19 53 46 5f 41 67 67 72 65 67 61 74 65 29 20 29 7b  SF_Aggregate) ){
20c1a 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 28  .    testcase( (
20c1b 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53  p->selFlags & (S
20c1c 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67  F_Distinct|SF_Ag
20c1d 67 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 44 69  gregate))==SF_Di
20c1e 73 74 69 6e 63 74 20 29 3b 0a 20 20 20 20 74 65  stinct );.    te
20c1f 73 74 63 61 73 65 28 20 28 70 2d 3e 73 65 6c 46  stcase( (p->selF
20c20 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73 74 69  lags & (SF_Disti
20c21 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74 65  nct|SF_Aggregate
20c22 29 29 3d 3d 53 46 5f 41 67 67 72 65 67 61 74 65  ))==SF_Aggregate
20c23 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   );.    return 0
20c24 3b 20 2f 2a 20 4e 6f 20 44 49 53 54 49 4e 43 54  ; /* No DISTINCT
20c25 20 6b 65 79 77 6f 72 64 20 61 6e 64 20 6e 6f 20   keyword and no 
20c26 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
20c27 6f 6e 73 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73  ons */.  }.  ass
20c28 65 72 74 28 20 70 2d 3e 70 47 72 6f 75 70 42 79  ert( p->pGroupBy
20c29 3d 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20 20  ==0 );          
20c2a 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 47 52      /* Has no GR
20c2b 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  OUP BY clause */
20c2c 0a 20 20 69 66 28 20 70 2d 3e 70 4c 69 6d 69 74  .  if( p->pLimit
20c2d 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
20c2e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73            /* Has
20c2f 20 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75 73 65   no LIMIT clause
20c30 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d   */.  assert( p-
20c31 3e 70 4f 66 66 73 65 74 3d 3d 30 20 29 3b 20 20  >pOffset==0 );  
20c32 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
20c33 4e 6f 20 4c 49 4d 49 54 20 6d 65 61 6e 73 20 6e  No LIMIT means n
20c34 6f 20 4f 46 46 53 45 54 20 2a 2f 0a 20 20 69 66  o OFFSET */.  if
20c35 28 20 70 2d 3e 70 57 68 65 72 65 20 29 20 72 65  ( p->pWhere ) re
20c36 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20  turn 0;         
20c37 20 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 57       /* Has no W
20c38 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
20c39 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b   pSrc = p->pSrc;
20c3a 0a 20 20 61 73 73 65 72 74 28 20 70 53 72 63 21  .  assert( pSrc!
20c3b 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 53 72 63  =0 );.  if( pSrc
20c3c 2d 3e 6e 53 72 63 21 3d 31 20 29 20 72 65 74 75  ->nSrc!=1 ) retu
20c3d 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f  rn 0;          /
20c3e 2a 20 53 69 6e 67 6c 65 20 74 65 72 6d 20 69 6e  * Single term in
20c3f 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a   FROM clause */.
20c40 20 20 69 66 28 20 70 53 72 63 2d 3e 61 5b 30 5d    if( pSrc->a[0]
20c41 2e 70 53 65 6c 65 63 74 20 29 20 72 65 74 75 72  .pSelect ) retur
20c42 6e 20 30 3b 20 20 20 20 20 2f 2a 20 46 52 4f 4d  n 0;     /* FROM
20c43 20 69 73 20 6e 6f 74 20 61 20 73 75 62 71 75 65   is not a subque
20c44 72 79 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20  ry or view */.  
20c45 70 54 61 62 20 3d 20 70 53 72 63 2d 3e 61 5b 30  pTab = pSrc->a[0
20c46 5d 2e 70 54 61 62 3b 0a 20 20 69 66 28 20 4e 45  ].pTab;.  if( NE
20c47 56 45 52 28 70 54 61 62 3d 3d 30 29 20 29 20 72  VER(pTab==0) ) r
20c48 65 74 75 72 6e 20 30 3b 0a 20 20 61 73 73 65 72  eturn 0;.  asser
20c49 74 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  t( pTab->pSelect
20c4a 3d 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20 20  ==0 );          
20c4b 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65    /* FROM clause
20c4c 20 69 73 20 6e 6f 74 20 61 20 76 69 65 77 20 2a   is not a view *
20c4d 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61  /.  if( IsVirtua
20c4e 6c 28 70 54 61 62 29 20 29 20 72 65 74 75 72 6e  l(pTab) ) return
20c4f 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 52   0;        /* FR
20c50 4f 4d 20 63 6c 61 75 73 65 20 6e 6f 74 20 61 20  OM clause not a 
20c51 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f  virtual table */
20c52 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70  .  pEList = p->p
20c53 45 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 45 4c  EList;.  if( pEL
20c54 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29 20  ist->nExpr!=1 ) 
20c55 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20  return 0;       
20c56 2f 2a 20 4f 6e 65 20 63 6f 6c 75 6d 6e 20 69 6e  /* One column in
20c57 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
20c58 2a 2f 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d  */.  if( pEList-
20c59 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 21  >a[0].pExpr->op!
20c5a 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20 72 65 74  =TK_COLUMN ) ret
20c5b 75 72 6e 20 30 3b 20 2f 2a 20 52 65 73 75 6c 74  urn 0; /* Result
20c5c 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 2a 2f 0a   is a column */.
20c5d 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65    return 1;.}.#e
20c5e 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
20c5f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a  MIT_SUBQUERY */.
20c60 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
20c61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20  tion is used by 
20c62 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
20c63 6f 6e 20 6f 66 20 74 68 65 20 49 4e 20 28 2e 2e  on of the IN (..
20c64 2e 29 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 20  .) operator..** 
20c65 49 74 27 73 20 6a 6f 62 20 69 73 20 74 6f 20 66  It's job is to f
20c66 69 6e 64 20 6f 72 20 63 72 65 61 74 65 20 61 20  ind or create a 
20c67 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65  b-tree structure
20c68 20 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 65   that may be use
20c69 64 0a 2a 2a 20 65 69 74 68 65 72 20 74 6f 20 74  d.** either to t
20c6a 65 73 74 20 66 6f 72 20 6d 65 6d 62 65 72 73 68  est for membersh
20c6b 69 70 20 6f 66 20 74 68 65 20 28 2e 2e 2e 29 20  ip of the (...) 
20c6c 73 65 74 20 6f 72 20 74 6f 20 69 74 65 72 61 74  set or to iterat
20c6d 65 20 74 68 72 6f 75 67 68 0a 2a 2a 20 69 74 73  e through.** its
20c6e 20 6d 65 6d 62 65 72 73 2c 20 73 6b 69 70 70 69   members, skippi
20c6f 6e 67 20 64 75 70 6c 69 63 61 74 65 73 2e 0a 2a  ng duplicates..*
20c70 2a 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 6f  *.** The index o
20c71 66 20 74 68 65 20 63 75 72 73 6f 72 20 6f 70 65  f the cursor ope
20c72 6e 65 64 20 6f 6e 20 74 68 65 20 62 2d 74 72 65  ned on the b-tre
20c73 65 20 28 64 61 74 61 62 61 73 65 20 74 61 62 6c  e (database tabl
20c74 65 2c 20 64 61 74 61 62 61 73 65 20 69 6e 64 65  e, database inde
20c75 78 20 0a 2a 2a 20 6f 72 20 65 70 68 65 72 6d 61  x .** or epherma
20c76 6c 20 74 61 62 6c 65 29 20 69 73 20 73 74 6f 72  l table) is stor
20c77 65 64 20 69 6e 20 70 58 2d 3e 69 54 61 62 6c 65  ed in pX->iTable
20c78 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e   before this fun
20c79 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a 2a  ction returns..*
20c7a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76  * The returned v
20c7b 61 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e  alue of this fun
20c7c 63 74 69 6f 6e 20 69 6e 64 69 63 61 74 65 73 20  ction indicates 
20c7d 74 68 65 20 62 2d 74 72 65 65 20 74 79 70 65 2c  the b-tree type,
20c7e 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
20c7f 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 52 4f  **   IN_INDEX_RO
20c80 57 49 44 20 2d 20 54 68 65 20 63 75 72 73 6f 72  WID - The cursor
20c81 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61   was opened on a
20c82 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e   database table.
20c83 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 49  .**   IN_INDEX_I
20c84 4e 44 45 58 20 2d 20 54 68 65 20 63 75 72 73 6f  NDEX - The curso
20c85 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20  r was opened on 
20c86 61 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78  a database index
20c87 2e 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f  ..**   IN_INDEX_
20c88 45 50 48 20 2d 20 20 20 54 68 65 20 63 75 72 73  EPH -   The curs
20c89 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e  or was opened on
20c8a 20 61 20 73 70 65 63 69 61 6c 6c 79 20 63 72 65   a specially cre
20c8b 61 74 65 64 20 61 6e 64 0a 2a 2a 20 20 20 20 20  ated and.**     
20c8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
20c8d 6f 70 75 6c 61 74 65 64 20 65 70 68 65 72 65 6d  opulated epherem
20c8e 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  al table..**.** 
20c8f 41 6e 20 65 78 69 73 74 69 6e 67 20 62 2d 74 72  An existing b-tr
20c90 65 65 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75  ee may only be u
20c91 73 65 64 20 69 66 20 74 68 65 20 53 45 4c 45 43  sed if the SELEC
20c92 54 20 69 73 20 6f 66 20 74 68 65 20 73 69 6d 70  T is of the simp
20c93 6c 65 0a 2a 2a 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a  le.** form:.**.*
20c94 2a 20 20 20 20 20 53 45 4c 45 43 54 20 3c 63 6f  *     SELECT <co
20c95 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 6c  lumn> FROM <tabl
20c96 65 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e>.**.** If the 
20c97 70 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 6d  prNotFound param
20c98 65 74 65 72 20 69 73 20 30 2c 20 74 68 65 6e 20  eter is 0, then 
20c99 74 68 65 20 62 2d 74 72 65 65 20 77 69 6c 6c 20  the b-tree will 
20c9a 62 65 20 75 73 65 64 20 74 6f 20 69 74 65 72 61  be used to itera
20c9b 74 65 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68  te.** through th
20c9c 65 20 73 65 74 20 6d 65 6d 62 65 72 73 2c 20 73  e set members, s
20c9d 6b 69 70 70 69 6e 67 20 61 6e 79 20 64 75 70 6c  kipping any dupl
20c9e 69 63 61 74 65 73 2e 20 49 6e 20 74 68 69 73 20  icates. In this 
20c9f 63 61 73 65 20 61 6e 0a 2a 2a 20 65 70 68 65 72  case an.** epher
20ca0 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75 73 74 20  emal table must 
20ca1 62 65 20 75 73 65 64 20 75 6e 6c 65 73 73 20 74  be used unless t
20ca2 68 65 20 73 65 6c 65 63 74 65 64 20 3c 63 6f 6c  he selected <col
20ca3 75 6d 6e 3e 20 69 73 20 67 75 61 72 61 6e 74 65  umn> is guarante
20ca4 65 64 0a 2a 2a 20 74 6f 20 62 65 20 75 6e 69 71  ed.** to be uniq
20ca5 75 65 20 2d 20 65 69 74 68 65 72 20 62 65 63 61  ue - either beca
20ca6 75 73 65 20 69 74 20 69 73 20 61 6e 20 49 4e 54  use it is an INT
20ca7 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
20ca8 20 6f 72 20 69 74 0a 2a 2a 20 68 61 73 20 61 20   or it.** has a 
20ca9 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e  UNIQUE constrain
20caa 74 20 6f 72 20 55 4e 49 51 55 45 20 69 6e 64 65  t or UNIQUE inde
20cab 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  x..**.** If the 
20cac 70 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 6d  prNotFound param
20cad 65 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 74  eter is not 0, t
20cae 68 65 6e 20 74 68 65 20 62 2d 74 72 65 65 20 77  hen the b-tree w
20caf 69 6c 6c 20 62 65 20 75 73 65 64 20 0a 2a 2a 20  ill be used .** 
20cb0 66 6f 72 20 66 61 73 74 20 73 65 74 20 6d 65 6d  for fast set mem
20cb1 62 65 72 73 68 69 70 20 74 65 73 74 73 2e 20 49  bership tests. I
20cb2 6e 20 74 68 69 73 20 63 61 73 65 20 61 6e 20 65  n this case an e
20cb3 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 20 6d  pheremal table m
20cb4 75 73 74 20 0a 2a 2a 20 62 65 20 75 73 65 64 20  ust .** be used 
20cb5 75 6e 6c 65 73 73 20 3c 63 6f 6c 75 6d 6e 3e 20  unless <column> 
20cb6 69 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52  is an INTEGER PR
20cb7 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 61 6e 20  IMARY KEY or an 
20cb8 69 6e 64 65 78 20 63 61 6e 20 0a 2a 2a 20 62 65  index can .** be
20cb9 20 66 6f 75 6e 64 20 77 69 74 68 20 3c 63 6f 6c   found with <col
20cba 75 6d 6e 3e 20 61 73 20 69 74 73 20 6c 65 66 74  umn> as its left
20cbb 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a  -most column..**
20cbc 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 62 2d 74  .** When the b-t
20cbd 72 65 65 20 69 73 20 62 65 69 6e 67 20 75 73 65  ree is being use
20cbe 64 20 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 70  d for membership
20cbf 20 74 65 73 74 73 2c 20 74 68 65 20 63 61 6c 6c   tests, the call
20cc0 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  ing function.** 
20cc1 6e 65 65 64 73 20 74 6f 20 6b 6e 6f 77 20 77 68  needs to know wh
20cc2 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
20cc3 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61   structure conta
20cc4 69 6e 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20  ins an SQL NULL 
20cc5 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20 6f 72 64  .** value in ord
20cc6 65 72 20 74 6f 20 63 6f 72 72 65 63 74 6c 79 20  er to correctly 
20cc7 65 76 61 6c 75 61 74 65 20 65 78 70 72 65 73 73  evaluate express
20cc8 69 6f 6e 73 20 6c 69 6b 65 20 22 58 20 49 4e 20  ions like "X IN 
20cc9 28 59 2c 20 5a 29 22 2e 0a 2a 2a 20 49 66 20 74  (Y, Z)"..** If t
20cca 68 65 72 65 20 69 73 20 61 20 63 68 61 6e 63 65  here is a chance
20ccb 20 74 68 61 74 20 74 68 65 20 62 2d 74 72 65 65   that the b-tree
20ccc 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 61   might contain a
20ccd 20 4e 55 4c 4c 20 76 61 6c 75 65 20 61 74 0a 2a   NULL value at.*
20cce 2a 20 72 75 6e 74 69 6d 65 2c 20 74 68 65 6e 20  * runtime, then 
20ccf 61 20 72 65 67 69 73 74 65 72 20 69 73 20 61 6c  a register is al
20cd0 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 20  located and the 
20cd1 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20  register number 
20cd2 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70  written.** to *p
20cd3 72 4e 6f 74 46 6f 75 6e 64 2e 20 49 66 20 74 68  rNotFound. If th
20cd4 65 72 65 20 69 73 20 6e 6f 20 63 68 61 6e 63 65  ere is no chance
20cd5 20 74 68 61 74 20 74 68 65 20 62 2d 74 72 65 65   that the b-tree
20cd6 20 63 6f 6e 74 61 69 6e 73 20 61 0a 2a 2a 20 4e   contains a.** N
20cd7 55 4c 4c 20 76 61 6c 75 65 2c 20 74 68 65 6e 20  ULL value, then 
20cd8 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 69 73 20 6c  *prNotFound is l
20cd9 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a  eft unchanged..*
20cda 2a 0a 2a 2a 20 49 66 20 61 20 72 65 67 69 73 74  *.** If a regist
20cdb 65 72 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  er is allocated 
20cdc 61 6e 64 20 69 74 73 20 6c 6f 63 61 74 69 6f 6e  and its location
20cdd 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 72 4e 6f   stored in *prNo
20cde 74 46 6f 75 6e 64 2c 20 74 68 65 6e 0a 2a 2a 20  tFound, then.** 
20cdf 69 74 73 20 69 6e 69 74 69 61 6c 20 76 61 6c 75  its initial valu
20ce0 65 20 69 73 20 4e 55 4c 4c 2e 20 49 66 20 74 68  e is NULL. If th
20ce1 65 20 62 2d 74 72 65 65 20 64 6f 65 73 20 6e 6f  e b-tree does no
20ce2 74 20 72 65 6d 61 69 6e 20 63 6f 6e 73 74 61 6e  t remain constan
20ce3 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 75 72  t.** for the dur
20ce4 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 65  ation of the que
20ce5 72 79 20 28 69 2e 65 2e 20 74 68 65 20 53 45 4c  ry (i.e. the SEL
20ce6 45 43 54 20 74 68 61 74 20 67 65 6e 65 72 61 74  ECT that generat
20ce7 65 73 20 74 68 65 20 62 2d 74 72 65 65 0a 2a 2a  es the b-tree.**
20ce8 20 69 73 20 61 20 63 6f 72 72 65 6c 61 74 65 64   is a correlated
20ce9 20 73 75 62 71 75 65 72 79 29 20 74 68 65 6e 20   subquery) then 
20cea 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
20ceb 20 61 6c 6c 6f 63 61 74 65 64 20 72 65 67 69 73   allocated regis
20cec 74 65 72 20 69 73 0a 2a 2a 20 72 65 73 65 74 20  ter is.** reset 
20ced 74 6f 20 4e 55 4c 4c 20 65 61 63 68 20 74 69 6d  to NULL each tim
20cee 65 20 74 68 65 20 62 2d 74 72 65 65 20 69 73 20  e the b-tree is 
20cef 72 65 70 6f 70 75 6c 61 74 65 64 2e 20 54 68 69  repopulated. Thi
20cf0 73 20 61 6c 6c 6f 77 73 20 74 68 65 0a 2a 2a 20  s allows the.** 
20cf1 63 61 6c 6c 65 72 20 74 6f 20 75 73 65 20 76 64  caller to use vd
20cf2 62 65 20 63 6f 64 65 20 65 71 75 69 76 61 6c 65  be code equivale
20cf3 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77  nt to the follow
20cf4 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28  ing:.**.**   if(
20cf5 20 72 65 67 69 73 74 65 72 3d 3d 4e 55 4c 4c 20   register==NULL 
20cf6 29 7b 0a 2a 2a 20 20 20 20 20 68 61 73 5f 6e 75  ){.**     has_nu
20cf7 6c 6c 20 3d 20 3c 74 65 73 74 20 69 66 20 64 61  ll = <test if da
20cf8 74 61 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e  ta structure con
20cf9 74 61 69 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a 20 20  tains null>.**  
20cfa 20 20 20 72 65 67 69 73 74 65 72 20 3d 20 31 0a     register = 1.
20cfb 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 69 6e 20  **   }.**.** in 
20cfc 6f 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20 72  order to avoid r
20cfd 75 6e 6e 69 6e 67 20 74 68 65 20 3c 74 65 73 74  unning the <test
20cfe 20 69 66 20 64 61 74 61 20 73 74 72 75 63 74 75   if data structu
20cff 72 65 20 63 6f 6e 74 61 69 6e 73 20 6e 75 6c 6c  re contains null
20d00 3e 0a 2a 2a 20 74 65 73 74 20 6d 6f 72 65 20 6f  >.** test more o
20d01 66 74 65 6e 20 74 68 61 6e 20 69 73 20 6e 65 63  ften than is nec
20d02 65 73 73 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e 64  essary..*/.#ifnd
20d03 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
20d04 55 42 51 55 45 52 59 0a 53 51 4c 49 54 45 5f 50  UBQUERY.SQLITE_P
20d05 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
20d06 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 50 61  e3FindInIndex(Pa
20d07 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70  rse *pParse, Exp
20d08 72 20 2a 70 58 2c 20 69 6e 74 20 2a 70 72 4e 6f  r *pX, int *prNo
20d09 74 46 6f 75 6e 64 29 7b 0a 20 20 53 65 6c 65 63  tFound){.  Selec
20d0a 74 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20  t *p;           
20d0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20d0c 20 2f 2a 20 53 45 4c 45 43 54 20 74 6f 20 74 68   /* SELECT to th
20d0d 65 20 72 69 67 68 74 20 6f 66 20 49 4e 20 6f 70  e right of IN op
20d0e 65 72 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20  erator */.  int 
20d0f 65 54 79 70 65 20 3d 20 30 3b 20 20 20 20 20 20  eType = 0;      
20d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20d11 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 52 48 53    /* Type of RHS
20d12 20 74 61 62 6c 65 2e 20 49 4e 5f 49 4e 44 45 58   table. IN_INDEX
20d13 5f 2a 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62  _* */.  int iTab
20d14 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
20d15 2b 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  +;            /*
20d16 20 43 75 72 73 6f 72 20 6f 66 20 74 68 65 20 52   Cursor of the R
20d17 48 53 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  HS table */.  in
20d18 74 20 6d 75 73 74 42 65 55 6e 69 71 75 65 20 3d  t mustBeUnique =
20d19 20 28 70 72 4e 6f 74 46 6f 75 6e 64 3d 3d 30 29   (prNotFound==0)
20d1a 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 52  ;   /* True if R
20d1b 48 53 20 6d 75 73 74 20 62 65 20 75 6e 69 71 75  HS must be uniqu
20d1c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  e */..  /* Check
20d1d 20 74 6f 20 73 65 65 20 69 66 20 61 6e 20 65 78   to see if an ex
20d1e 69 73 74 69 6e 67 20 74 61 62 6c 65 20 6f 72 20  isting table or 
20d1f 69 6e 64 65 78 20 63 61 6e 20 62 65 20 75 73 65  index can be use
20d20 64 20 74 6f 0a 20 20 2a 2a 20 73 61 74 69 73 66  d to.  ** satisf
20d21 79 20 74 68 65 20 71 75 65 72 79 2e 20 20 54 68  y the query.  Th
20d22 69 73 20 69 73 20 70 72 65 66 65 72 61 62 6c 65  is is preferable
20d23 20 74 6f 20 67 65 6e 65 72 61 74 69 6e 67 20 61   to generating a
20d24 20 6e 65 77 20 0a 20 20 2a 2a 20 65 70 68 65 6d   new .  ** ephem
20d25 65 72 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f  eral table..  */
20d26 0a 20 20 70 20 3d 20 28 45 78 70 72 48 61 73 50  .  p = (ExprHasP
20d27 72 6f 70 65 72 74 79 28 70 58 2c 20 45 50 5f 78  roperty(pX, EP_x
20d28 49 73 53 65 6c 65 63 74 29 20 3f 20 70 58 2d 3e  IsSelect) ? pX->
20d29 78 2e 70 53 65 6c 65 63 74 20 3a 20 30 29 3b 0a  x.pSelect : 0);.
20d2a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 50 61    if( ALWAYS(pPa
20d2b 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 29 20 26 26  rse->nErr==0) &&
20d2c 20 69 73 43 61 6e 64 69 64 61 74 65 46 6f 72 49   isCandidateForI
20d2d 6e 4f 70 74 28 70 29 20 29 7b 0a 20 20 20 20 73  nOpt(p) ){.    s
20d2e 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
20d2f 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20  rse->db;        
20d30 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
20d31 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
20d32 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 20      Expr *pExpr 
20d33 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30  = p->pEList->a[0
20d34 5d 2e 70 45 78 70 72 3b 20 20 20 2f 2a 20 45 78  ].pExpr;   /* Ex
20d35 70 72 65 73 73 69 6f 6e 20 3c 63 6f 6c 75 6d 6e  pression <column
20d36 3e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f  > */.    int iCo
20d37 6c 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  l = pExpr->iColu
20d38 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  mn;             
20d39 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75  /* Index of colu
20d3a 6d 6e 20 3c 63 6f 6c 75 6d 6e 3e 20 2a 2f 0a 20  mn <column> */. 
20d3b 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c     Vdbe *v = sql
20d3c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
20d3d 73 65 29 3b 20 20 20 20 20 20 2f 2a 20 56 69 72  se);      /* Vir
20d3e 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 62 65 69  tual machine bei
20d3f 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20 20 20  ng coded */.    
20d40 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 2d  Table *pTab = p-
20d41 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 62  >pSrc->a[0].pTab
20d42 3b 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20  ;      /* Table 
20d43 3c 74 61 62 6c 65 3e 2e 20 2a 2f 0a 20 20 20 20  <table>. */.    
20d44 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
20d45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20d46 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
20d47 73 65 20 69 64 78 20 66 6f 72 20 70 54 61 62 20  se idx for pTab 
20d48 2a 2f 0a 20 20 20 0a 20 20 20 20 2f 2a 20 43 6f  */.   .    /* Co
20d49 64 65 20 61 6e 20 4f 50 5f 56 65 72 69 66 79 43  de an OP_VerifyC
20d4a 6f 6f 6b 69 65 20 61 6e 64 20 4f 50 5f 54 61 62  ookie and OP_Tab
20d4b 6c 65 4c 6f 63 6b 20 66 6f 72 20 3c 74 61 62 6c  leLock for <tabl
20d4c 65 3e 2e 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d  e>. */.    iDb =
20d4d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
20d4e 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e  Index(db, pTab->
20d4f 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 73 71  pSchema);.    sq
20d50 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53  lite3CodeVerifyS
20d51 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44  chema(pParse, iD
20d52 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 54  b);.    sqlite3T
20d53 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c  ableLock(pParse,
20d54 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d   iDb, pTab->tnum
20d55 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  , 0, pTab->zName
20d56 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20  );..    /* This 
20d57 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79  function is only
20d58 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 77 6f   called from two
20d59 20 70 6c 61 63 65 73 2e 20 49 6e 20 62 6f 74 68   places. In both
20d5a 20 63 61 73 65 73 20 74 68 65 20 76 64 62 65 0a   cases the vdbe.
20d5b 20 20 20 20 2a 2a 20 68 61 73 20 61 6c 72 65 61      ** has alrea
20d5c 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65  dy been allocate
20d5d 64 2e 20 53 6f 20 61 73 73 75 6d 65 20 73 71 6c  d. So assume sql
20d5e 69 74 65 33 47 65 74 56 64 62 65 28 29 20 69 73  ite3GetVdbe() is
20d5f 20 61 6c 77 61 79 73 0a 20 20 20 20 2a 2a 20 73   always.    ** s
20d60 75 63 63 65 73 73 66 75 6c 20 68 65 72 65 2e 0a  uccessful here..
20d61 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
20d62 74 28 76 29 3b 0a 20 20 20 20 69 66 28 20 69 43  t(v);.    if( iC
20d63 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 69 6e  ol<0 ){.      in
20d64 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73  t iMem = ++pPars
20d65 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 69  e->nMem;.      i
20d66 6e 74 20 69 41 64 64 72 3b 0a 0a 20 20 20 20 20  nt iAddr;..     
20d67 20 69 41 64 64 72 20 3d 20 73 71 6c 69 74 65 33   iAddr = sqlite3
20d68 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
20d69 5f 49 66 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20  _If, iMem);.    
20d6a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
20d6b 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
20d6c 72 2c 20 31 2c 20 69 4d 65 6d 29 3b 0a 0a 20 20  r, 1, iMem);..  
20d6d 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54      sqlite3OpenT
20d6e 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 54 61  able(pParse, iTa
20d6f 62 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50  b, iDb, pTab, OP
20d70 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 20 20  _OpenRead);.    
20d71 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44    eType = IN_IND
20d72 45 58 5f 52 4f 57 49 44 3b 0a 0a 20 20 20 20 20  EX_ROWID;..     
20d73 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
20d74 48 65 72 65 28 76 2c 20 69 41 64 64 72 29 3b 0a  Here(v, iAddr);.
20d75 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
20d76 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20   Index *pIdx;   
20d77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20d78 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f        /* Iterato
20d79 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 0a 20  r variable */.. 
20d7a 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c       /* The coll
20d7b 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 75  ation sequence u
20d7c 73 65 64 20 62 79 20 74 68 65 20 63 6f 6d 70 61  sed by the compa
20d7d 72 69 73 6f 6e 2e 20 49 66 20 61 6e 20 69 6e 64  rison. If an ind
20d7e 65 78 20 69 73 20 74 6f 0a 20 20 20 20 20 20 2a  ex is to.      *
20d7f 2a 20 62 65 20 75 73 65 64 20 69 6e 20 70 6c 61  * be used in pla
20d80 63 65 20 6f 66 20 61 20 74 65 6d 70 2d 74 61 62  ce of a temp-tab
20d81 6c 65 2c 20 69 74 20 6d 75 73 74 20 62 65 20 6f  le, it must be o
20d82 72 64 65 72 65 64 20 61 63 63 6f 72 64 69 6e 67  rdered according
20d83 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 69  .      ** to thi
20d84 73 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  s collation sequ
20d85 65 6e 63 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20  ence.  */.      
20d86 43 6f 6c 6c 53 65 71 20 2a 70 52 65 71 20 3d 20  CollSeq *pReq = 
20d87 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d  sqlite3BinaryCom
20d88 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72  pareCollSeq(pPar
20d89 73 65 2c 20 70 58 2d 3e 70 4c 65 66 74 2c 20 70  se, pX->pLeft, p
20d8a 45 78 70 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  Expr);..      /*
20d8b 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20   Check that the 
20d8c 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 77 69  affinity that wi
20d8d 6c 6c 20 62 65 20 75 73 65 64 20 74 6f 20 70 65  ll be used to pe
20d8e 72 66 6f 72 6d 20 74 68 65 20 0a 20 20 20 20 20  rform the .     
20d8f 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69   ** comparison i
20d90 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68  s the same as th
20d91 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68  e affinity of th
20d92 65 20 63 6f 6c 75 6d 6e 2e 20 49 66 0a 20 20 20  e column. If.   
20d93 20 20 20 2a 2a 20 69 74 20 69 73 20 6e 6f 74 2c     ** it is not,
20d94 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69   it is not possi
20d95 62 6c 65 20 74 6f 20 75 73 65 20 61 6e 79 20 69  ble to use any i
20d96 6e 64 65 78 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ndex..      */. 
20d97 20 20 20 20 20 63 68 61 72 20 61 66 66 20 3d 20       char aff = 
20d98 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69  comparisonAffini
20d99 74 79 28 70 58 29 3b 0a 20 20 20 20 20 20 69 6e  ty(pX);.      in
20d9a 74 20 61 66 66 69 6e 69 74 79 5f 6f 6b 20 3d 20  t affinity_ok = 
20d9b 28 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c  (pTab->aCol[iCol
20d9c 5d 2e 61 66 66 69 6e 69 74 79 3d 3d 61 66 66 7c  ].affinity==aff|
20d9d 7c 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46  |aff==SQLITE_AFF
20d9e 5f 4e 4f 4e 45 29 3b 0a 0a 20 20 20 20 20 20 66  _NONE);..      f
20d9f 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  or(pIdx=pTab->pI
20da0 6e 64 65 78 3b 20 70 49 64 78 20 26 26 20 65 54  ndex; pIdx && eT
20da1 79 70 65 3d 3d 30 20 26 26 20 61 66 66 69 6e 69  ype==0 && affini
20da2 74 79 5f 6f 6b 3b 20 70 49 64 78 3d 70 49 64 78  ty_ok; pIdx=pIdx
20da3 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20  ->pNext){.      
20da4 20 20 69 66 28 20 28 70 49 64 78 2d 3e 61 69 43    if( (pIdx->aiC
20da5 6f 6c 75 6d 6e 5b 30 5d 3d 3d 69 43 6f 6c 29 0a  olumn[0]==iCol).
20da6 20 20 20 20 20 20 20 20 20 26 26 20 73 71 6c 69           && sqli
20da7 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64  te3FindCollSeq(d
20da8 62 2c 20 45 4e 43 28 64 62 29 2c 20 70 49 64 78  b, ENC(db), pIdx
20da9 2d 3e 61 7a 43 6f 6c 6c 5b 30 5d 2c 20 30 29 3d  ->azColl[0], 0)=
20daa 3d 70 52 65 71 0a 20 20 20 20 20 20 20 20 20 26  =pReq.         &
20dab 26 20 28 21 6d 75 73 74 42 65 55 6e 69 71 75 65  & (!mustBeUnique
20dac 20 7c 7c 20 28 70 49 64 78 2d 3e 6e 43 6f 6c 75   || (pIdx->nColu
20dad 6d 6e 3d 3d 31 20 26 26 20 70 49 64 78 2d 3e 6f  mn==1 && pIdx->o
20dae 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 29  nError!=OE_None)
20daf 29 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20  ).        ){.   
20db0 20 20 20 20 20 20 20 69 6e 74 20 69 4d 65 6d 20         int iMem 
20db1 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
20db2 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  ;.          int 
20db3 69 41 64 64 72 3b 0a 20 20 20 20 20 20 20 20 20  iAddr;.         
20db4 20 63 68 61 72 20 2a 70 4b 65 79 3b 0a 20 20 0a   char *pKey;.  .
20db5 20 20 20 20 20 20 20 20 20 20 70 4b 65 79 20 3d            pKey =
20db6 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33   (char *)sqlite3
20db7 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61  IndexKeyinfo(pPa
20db8 72 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 20  rse, pIdx);.    
20db9 20 20 20 20 20 20 69 41 64 64 72 20 3d 20 73 71        iAddr = sq
20dba 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
20dbb 76 2c 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 29 3b  v, OP_If, iMem);
20dbc 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
20dbd 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
20dbe 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69  OP_Integer, 1, i
20dbf 4d 65 6d 29 3b 0a 20 20 0a 20 20 20 20 20 20 20  Mem);.  .       
20dc0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
20dc1 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52  dOp4(v, OP_OpenR
20dc2 65 61 64 2c 20 69 54 61 62 2c 20 70 49 64 78 2d  ead, iTab, pIdx-
20dc3 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20  >tnum, iDb,.    
20dc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20dc5 20 20 20 20 20 20 20 20 20 20 20 70 4b 65 79 2c             pKey,
20dc6 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
20dc7 46 46 29 3b 0a 20 20 20 20 20 20 20 20 20 20 56  FF);.          V
20dc8 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
20dc9 25 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65  %s", pIdx->zName
20dca 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 65 54  ));.          eT
20dcb 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 49  ype = IN_INDEX_I
20dcc 4e 44 45 58 3b 0a 0a 20 20 20 20 20 20 20 20 20  NDEX;..         
20dcd 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
20dce 48 65 72 65 28 76 2c 20 69 41 64 64 72 29 3b 0a  Here(v, iAddr);.
20dcf 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 72            if( pr
20dd0 4e 6f 74 46 6f 75 6e 64 20 26 26 20 21 70 54 61  NotFound && !pTa
20dd1 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 6e 6f  b->aCol[iCol].no
20dd2 74 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20  tNull ){.       
20dd3 20 20 20 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64       *prNotFound
20dd4 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
20dd5 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  m;.          }. 
20dd6 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
20dd7 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66  .    }.  }..  if
20dd8 28 20 65 54 79 70 65 3d 3d 30 20 29 7b 0a 20 20  ( eType==0 ){.  
20dd9 20 20 2f 2a 20 43 6f 75 6c 64 20 6e 6f 74 20 66    /* Could not f
20dda 6f 75 6e 64 20 61 6e 20 65 78 69 73 74 69 6e 67  ound an existing
20ddb 20 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 74   able or index t
20ddc 6f 20 75 73 65 20 61 73 20 74 68 65 20 52 48 53  o use as the RHS
20ddd 20 62 2d 74 72 65 65 2e 0a 20 20 20 20 2a 2a 20   b-tree..    ** 
20dde 57 65 20 77 69 6c 6c 20 68 61 76 65 20 74 6f 20  We will have to 
20ddf 67 65 6e 65 72 61 74 65 20 61 6e 20 65 70 68 65  generate an ephe
20de0 6d 65 72 61 6c 20 74 61 62 6c 65 20 74 6f 20 64  meral table to d
20de1 6f 20 74 68 65 20 6a 6f 62 2e 0a 20 20 20 20 2a  o the job..    *
20de2 2f 0a 20 20 20 20 69 6e 74 20 72 4d 61 79 48 61  /.    int rMayHa
20de3 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 20  veNull = 0;.    
20de4 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58  eType = IN_INDEX
20de5 5f 45 50 48 3b 0a 20 20 20 20 69 66 28 20 70 72  _EPH;.    if( pr
20de6 4e 6f 74 46 6f 75 6e 64 20 29 7b 0a 20 20 20 20  NotFound ){.    
20de7 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d 20    *prNotFound = 
20de8 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20 2b  rMayHaveNull = +
20de9 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
20dea 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 58 2d     }else if( pX-
20deb 3e 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3c  >pLeft->iColumn<
20dec 30 20 26 26 20 21 45 78 70 72 48 61 73 41 6e 79  0 && !ExprHasAny
20ded 50 72 6f 70 65 72 74 79 28 70 58 2c 20 45 50 5f  Property(pX, EP_
20dee 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20  xIsSelect) ){.  
20def 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49      eType = IN_I
20df0 4e 44 45 58 5f 52 4f 57 49 44 3b 0a 20 20 20 20  NDEX_ROWID;.    
20df1 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64  }.    sqlite3Cod
20df2 65 53 75 62 73 65 6c 65 63 74 28 70 50 61 72 73  eSubselect(pPars
20df3 65 2c 20 70 58 2c 20 72 4d 61 79 48 61 76 65 4e  e, pX, rMayHaveN
20df4 75 6c 6c 2c 20 65 54 79 70 65 3d 3d 49 4e 5f 49  ull, eType==IN_I
20df5 4e 44 45 58 5f 52 4f 57 49 44 29 3b 0a 20 20 7d  NDEX_ROWID);.  }
20df6 65 6c 73 65 7b 0a 20 20 20 20 70 58 2d 3e 69 54  else{.    pX->iT
20df7 61 62 6c 65 20 3d 20 69 54 61 62 3b 0a 20 20 7d  able = iTab;.  }
20df8 0a 20 20 72 65 74 75 72 6e 20 65 54 79 70 65 3b  .  return eType;
20df9 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
20dfa 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66   Generate code f
20dfb 6f 72 20 73 63 61 6c 61 72 20 73 75 62 71 75 65  or scalar subque
20dfc 72 69 65 73 20 75 73 65 64 20 61 73 20 61 6e 20  ries used as an 
20dfd 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 61 6e  expression.** an
20dfe 64 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 2e 20  d IN operators. 
20dff 20 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a   Examples:.**.**
20e00 20 20 20 20 20 28 53 45 4c 45 43 54 20 61 20 46       (SELECT a F
20e01 52 4f 4d 20 62 29 20 20 20 20 20 20 20 20 20 20  ROM b)          
20e02 2d 2d 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 20  -- subquery.**  
20e03 20 20 20 45 58 49 53 54 53 20 28 53 45 4c 45 43     EXISTS (SELEC
20e04 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20 2d 2d  T a FROM b)   --
20e05 20 45 58 49 53 54 53 20 73 75 62 71 75 65 72 79   EXISTS subquery
20e06 0a 2a 2a 20 20 20 20 20 78 20 49 4e 20 28 34 2c  .**     x IN (4,
20e07 35 2c 31 31 29 20 20 20 20 20 20 20 20 20 20 20  5,11)           
20e08 20 20 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f     -- IN operato
20e09 72 20 77 69 74 68 20 6c 69 73 74 20 6f 6e 20 72  r with list on r
20e0a 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 0a 2a  ight-hand side.*
20e0b 2a 20 20 20 20 20 78 20 49 4e 20 28 53 45 4c 45  *     x IN (SELE
20e0c 43 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20 20  CT a FROM b)    
20e0d 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f 72 20   -- IN operator 
20e0e 77 69 74 68 20 73 75 62 71 75 65 72 79 20 6f 6e  with subquery on
20e0f 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 0a 2a 2a   the right.**.**
20e10 20 54 68 65 20 70 45 78 70 72 20 70 61 72 61 6d   The pExpr param
20e11 65 74 65 72 20 64 65 73 63 72 69 62 65 73 20 74  eter describes t
20e12 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  he expression th
20e13 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
20e14 49 4e 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 6f  IN.** operator o
20e15 72 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a 0a 2a  r subquery..**.*
20e16 2a 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 69  * If parameter i
20e17 73 52 6f 77 69 64 20 69 73 20 6e 6f 6e 2d 7a 65  sRowid is non-ze
20e18 72 6f 2c 20 74 68 65 6e 20 65 78 70 72 65 73 73  ro, then express
20e19 69 6f 6e 20 70 45 78 70 72 20 69 73 20 67 75 61  ion pExpr is gua
20e1a 72 61 6e 74 65 65 64 0a 2a 2a 20 74 6f 20 62 65  ranteed.** to be
20e1b 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3c 72   of the form "<r
20e1c 6f 77 69 64 3e 20 49 4e 20 28 3f 2c 20 3f 2c 20  owid> IN (?, ?, 
20e1d 3f 29 22 2c 20 77 68 65 72 65 20 3c 72 6f 77 69  ?)", where <rowi
20e1e 64 3e 20 69 73 20 61 20 72 65 66 65 72 65 6e 63  d> is a referenc
20e1f 65 0a 2a 2a 20 74 6f 20 73 6f 6d 65 20 69 6e 74  e.** to some int
20e20 65 67 65 72 20 6b 65 79 20 63 6f 6c 75 6d 6e 20  eger key column 
20e21 6f 66 20 61 20 74 61 62 6c 65 20 42 2d 54 72 65  of a table B-Tre
20e22 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c  e. In this case,
20e23 20 75 73 65 20 61 6e 0a 2a 2a 20 69 6e 74 6b 65   use an.** intke
20e24 79 20 42 2d 54 72 65 65 20 74 6f 20 73 74 6f 72  y B-Tree to stor
20e25 65 20 74 68 65 20 73 65 74 20 6f 66 20 49 4e 28  e the set of IN(
20e26 2e 2e 2e 29 20 76 61 6c 75 65 73 20 69 6e 73 74  ...) values inst
20e27 65 61 64 20 6f 66 20 74 68 65 20 75 73 75 61 6c  ead of the usual
20e28 0a 2a 2a 20 28 73 6c 6f 77 65 72 29 20 76 61 72  .** (slower) var
20e29 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 6b 65 79  iable length key
20e2a 73 20 42 2d 54 72 65 65 2e 0a 2a 2a 0a 2a 2a 20  s B-Tree..**.** 
20e2b 49 66 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20  If rMayHaveNull 
20e2c 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 61  is non-zero, tha
20e2d 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  t means that the
20e2e 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6e   operation is an
20e2f 20 49 4e 0a 2a 2a 20 28 6e 6f 74 20 61 20 53 45   IN.** (not a SE
20e30 4c 45 43 54 20 6f 72 20 45 58 49 53 54 53 29 20  LECT or EXISTS) 
20e31 61 6e 64 20 74 68 61 74 20 74 68 65 20 52 48 53  and that the RHS
20e32 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 73 20   might contains 
20e33 4e 55 4c 4c 73 2e 0a 2a 2a 20 46 75 72 74 68 65  NULLs..** Furthe
20e34 72 6d 6f 72 65 2c 20 74 68 65 20 49 4e 20 69 73  rmore, the IN is
20e35 20 69 6e 20 61 20 57 48 45 52 45 20 63 6c 61 75   in a WHERE clau
20e36 73 65 20 61 6e 64 20 74 68 61 74 20 77 65 20 72  se and that we r
20e37 65 61 6c 6c 79 20 77 61 6e 74 0a 2a 2a 20 74 6f  eally want.** to
20e38 20 69 74 65 72 61 74 65 20 6f 76 65 72 20 74 68   iterate over th
20e39 65 20 52 48 53 20 6f 66 20 74 68 65 20 49 4e 20  e RHS of the IN 
20e3a 6f 70 65 72 61 74 6f 72 20 69 6e 20 6f 72 64 65  operator in orde
20e3b 72 20 74 6f 20 71 75 69 63 6b 6c 79 20 6c 6f 63  r to quickly loc
20e3c 61 74 65 0a 2a 2a 20 61 6c 6c 20 63 6f 72 72 65  ate.** all corre
20e3d 73 70 6f 6e 64 69 6e 67 20 4c 48 53 20 65 6c 65  sponding LHS ele
20e3e 6d 65 6e 74 73 2e 20 20 41 6c 6c 20 74 68 69 73  ments.  All this
20e3f 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 69 73   routine does is
20e40 20 69 6e 69 74 69 61 6c 69 7a 65 0a 2a 2a 20 74   initialize.** t
20e41 68 65 20 72 65 67 69 73 74 65 72 20 67 69 76 65  he register give
20e42 6e 20 62 79 20 72 4d 61 79 48 61 76 65 4e 75 6c  n by rMayHaveNul
20e43 6c 20 74 6f 20 4e 55 4c 4c 2e 20 20 43 61 6c 6c  l to NULL.  Call
20e44 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 77 69 6c  ing routines wil
20e45 6c 20 74 61 6b 65 0a 2a 2a 20 63 61 72 65 20 6f  l take.** care o
20e46 66 20 63 68 61 6e 67 69 6e 67 20 74 68 69 73 20  f changing this 
20e47 72 65 67 69 73 74 65 72 20 76 61 6c 75 65 20 74  register value t
20e48 6f 20 6e 6f 6e 2d 4e 55 4c 4c 20 69 66 20 74 68  o non-NULL if th
20e49 65 20 52 48 53 20 69 73 20 4e 55 4c 4c 2d 66 72  e RHS is NULL-fr
20e4a 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 4d 61  ee..**.** If rMa
20e4b 79 48 61 76 65 4e 75 6c 6c 20 69 73 20 7a 65 72  yHaveNull is zer
20e4c 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  o, that means th
20e4d 61 74 20 74 68 65 20 73 75 62 71 75 65 72 79 20  at the subquery 
20e4e 69 73 20 62 65 69 6e 67 20 75 73 65 64 0a 2a 2a  is being used.**
20e4f 20 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 70 20   for membership 
20e50 74 65 73 74 69 6e 67 20 6f 6e 6c 79 2e 20 20 54  testing only.  T
20e51 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20  here is no need 
20e52 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 61 6e  to initialize an
20e53 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20 74  y.** registers t
20e54 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 70  o indicate the p
20e55 72 65 73 65 6e 73 65 20 6f 72 20 61 62 73 65 6e  resense or absen
20e56 63 65 20 6f 66 20 4e 55 4c 4c 73 20 6f 6e 20 74  ce of NULLs on t
20e57 68 65 20 52 48 53 2e 0a 2a 2f 0a 23 69 66 6e 64  he RHS..*/.#ifnd
20e58 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
20e59 55 42 51 55 45 52 59 0a 53 51 4c 49 54 45 5f 50  UBQUERY.SQLITE_P
20e5a 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
20e5b 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74  te3CodeSubselect
20e5c 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
20e5d 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  e,          /* P
20e5e 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
20e5f 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c  /.  Expr *pExpr,
20e60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
20e61 68 65 20 49 4e 2c 20 53 45 4c 45 43 54 2c 20 6f  he IN, SELECT, o
20e62 72 20 45 58 49 53 54 53 20 6f 70 65 72 61 74 6f  r EXISTS operato
20e63 72 20 2a 2f 0a 20 20 69 6e 74 20 72 4d 61 79 48  r */.  int rMayH
20e64 61 76 65 4e 75 6c 6c 2c 20 20 20 20 20 20 20 2f  aveNull,       /
20e65 2a 20 52 65 67 69 73 74 65 72 20 74 68 61 74 20  * Register that 
20e66 72 65 63 6f 72 64 73 20 77 68 65 74 68 65 72 20  records whether 
20e67 4e 55 4c 4c 73 20 65 78 69 73 74 20 69 6e 20 52  NULLs exist in R
20e68 48 53 20 2a 2f 0a 20 20 69 6e 74 20 69 73 52 6f  HS */.  int isRo
20e69 77 69 64 20 20 20 20 20 20 20 20 20 20 20 20 20  wid             
20e6a 2f 2a 20 49 66 20 74 72 75 65 2c 20 4c 48 53 20  /* If true, LHS 
20e6b 6f 66 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 69  of IN operator i
20e6c 73 20 61 20 72 6f 77 69 64 20 2a 2f 0a 29 7b 0a  s a rowid */.){.
20e6d 20 20 69 6e 74 20 74 65 73 74 41 64 64 72 20 3d    int testAddr =
20e6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
20e6f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
20e70 2d 74 69 6d 65 20 74 65 73 74 20 61 64 64 72 65  -time test addre
20e71 73 73 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20  ss */.  Vdbe *v 
20e72 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
20e73 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
20e74 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 20 72 65  NEVER(v==0) ) re
20e75 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 45  turn;.  sqlite3E
20e76 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50 61  xprCachePush(pPa
20e77 72 73 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73  rse);..  /* This
20e78 20 63 6f 64 65 20 6d 75 73 74 20 62 65 20 72 75   code must be ru
20e79 6e 20 69 6e 20 69 74 73 20 65 6e 74 69 72 65 74  n in its entiret
20e7a 79 20 65 76 65 72 79 20 74 69 6d 65 20 69 74 20  y every time it 
20e7b 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 20  is encountered. 
20e7c 20 2a 2a 20 69 66 20 61 6e 79 20 6f 66 20 74 68   ** if any of th
20e7d 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74  e following is t
20e7e 72 75 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  rue:.  **.  **  
20e7f 20 20 2a 20 20 54 68 65 20 72 69 67 68 74 2d 68    *  The right-h
20e80 61 6e 64 20 73 69 64 65 20 69 73 20 61 20 63 6f  and side is a co
20e81 72 72 65 6c 61 74 65 64 20 73 75 62 71 75 65 72  rrelated subquer
20e82 79 0a 20 20 2a 2a 20 20 20 20 2a 20 20 54 68 65  y.  **    *  The
20e83 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65   right-hand side
20e84 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f   is an expressio
20e85 6e 20 6c 69 73 74 20 63 6f 6e 74 61 69 6e 69 6e  n list containin
20e86 67 20 76 61 72 69 61 62 6c 65 73 0a 20 20 2a 2a  g variables.  **
20e87 20 20 20 20 2a 20 20 57 65 20 61 72 65 20 69 6e      *  We are in
20e88 73 69 64 65 20 61 20 74 72 69 67 67 65 72 0a 20  side a trigger. 
20e89 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61 6c 6c 20   **.  ** If all 
20e8a 6f 66 20 74 68 65 20 61 62 6f 76 65 20 61 72 65  of the above are
20e8b 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 77 65 20   false, then we 
20e8c 63 61 6e 20 72 75 6e 20 74 68 69 73 20 63 6f 64  can run this cod
20e8d 65 20 6a 75 73 74 20 6f 6e 63 65 0a 20 20 2a 2a  e just once.  **
20e8e 20 73 61 76 65 20 74 68 65 20 72 65 73 75 6c 74   save the result
20e8f 73 2c 20 61 6e 64 20 72 65 75 73 65 20 74 68 65  s, and reuse the
20e90 20 73 61 6d 65 20 72 65 73 75 6c 74 20 6f 6e 20   same result on 
20e91 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f 63  subsequent invoc
20e92 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69  ations..  */.  i
20e93 66 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 72  f( !ExprHasAnyPr
20e94 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
20e95 5f 56 61 72 53 65 6c 65 63 74 29 20 26 26 20 21  _VarSelect) && !
20e96 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72  pParse->pTrigger
20e97 54 61 62 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d  Tab ){.    int m
20e98 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  em = ++pParse->n
20e99 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Mem;.    sqlite3
20e9a 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
20e9b 5f 49 66 2c 20 6d 65 6d 29 3b 0a 20 20 20 20 74  _If, mem);.    t
20e9c 65 73 74 41 64 64 72 20 3d 20 73 71 6c 69 74 65  estAddr = sqlite
20e9d 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
20e9e 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 6d 65  P_Integer, 1, me
20e9f 6d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  m);.    assert( 
20ea0 74 65 73 74 41 64 64 72 3e 30 20 7c 7c 20 70 50  testAddr>0 || pP
20ea1 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  arse->db->malloc
20ea2 46 61 69 6c 65 64 20 29 3b 0a 20 20 7d 0a 0a 20  Failed );.  }.. 
20ea3 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e   switch( pExpr->
20ea4 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54  op ){.    case T
20ea5 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 63 68  K_IN: {.      ch
20ea6 61 72 20 61 66 66 69 6e 69 74 79 3b 0a 20 20 20  ar affinity;.   
20ea7 20 20 20 4b 65 79 49 6e 66 6f 20 6b 65 79 49 6e     KeyInfo keyIn
20ea8 66 6f 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 64  fo;.      int ad
20ea9 64 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 64  dr;        /* Ad
20eaa 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f 70 65 6e  dress of OP_Open
20eab 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72 75  Ephemeral instru
20eac 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45  ction */.      E
20ead 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78  xpr *pLeft = pEx
20eae 70 72 2d 3e 70 4c 65 66 74 3b 0a 0a 20 20 20 20  pr->pLeft;..    
20eaf 20 20 69 66 28 20 72 4d 61 79 48 61 76 65 4e 75    if( rMayHaveNu
20eb0 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ll ){.        sq
20eb1 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
20eb2 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72  v, OP_Null, 0, r
20eb3 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20  MayHaveNull);.  
20eb4 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 66 66      }..      aff
20eb5 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33 45  inity = sqlite3E
20eb6 78 70 72 41 66 66 69 6e 69 74 79 28 70 4c 65 66  xprAffinity(pLef
20eb7 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 57 68  t);..      /* Wh
20eb8 65 74 68 65 72 20 74 68 69 73 20 69 73 20 61 6e  ether this is an
20eb9 20 27 78 20 49 4e 28 53 45 4c 45 43 54 2e 2e 2e   'x IN(SELECT...
20eba 29 27 20 6f 72 20 61 6e 20 27 78 20 49 4e 28 3c  )' or an 'x IN(<
20ebb 65 78 70 72 6c 69 73 74 3e 29 27 0a 20 20 20 20  exprlist>)'.    
20ebc 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20    ** expression 
20ebd 69 74 20 69 73 20 68 61 6e 64 6c 65 64 20 74 68  it is handled th
20ebe 65 20 73 61 6d 65 20 77 61 79 2e 20 41 20 76 69  e same way. A vi
20ebf 72 74 75 61 6c 20 74 61 62 6c 65 20 69 73 20 0a  rtual table is .
20ec0 20 20 20 20 20 20 2a 2a 20 66 69 6c 6c 65 64 20        ** filled 
20ec1 77 69 74 68 20 73 69 6e 67 6c 65 2d 66 69 65 6c  with single-fiel
20ec2 64 20 69 6e 64 65 78 20 6b 65 79 73 20 72 65 70  d index keys rep
20ec3 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 72 65  resenting the re
20ec4 73 75 6c 74 73 0a 20 20 20 20 20 20 2a 2a 20 66  sults.      ** f
20ec5 72 6f 6d 20 74 68 65 20 53 45 4c 45 43 54 20 6f  rom the SELECT o
20ec6 72 20 74 68 65 20 3c 65 78 70 72 6c 69 73 74 3e  r the <exprlist>
20ec7 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  ..      **.     
20ec8 20 2a 2a 20 49 66 20 74 68 65 20 27 78 27 20 65   ** If the 'x' e
20ec9 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 63  xpression is a c
20eca 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 6f 72 20  olumn value, or 
20ecb 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e 0a 20 20  the SELECT....  
20ecc 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74      ** statement
20ecd 20 72 65 74 75 72 6e 73 20 61 20 63 6f 6c 75 6d   returns a colum
20ece 6e 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 68  n value, then th
20ecf 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68  e affinity of th
20ed0 61 74 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6c 75  at.      ** colu
20ed1 6d 6e 20 69 73 20 75 73 65 64 20 74 6f 20 62 75  mn is used to bu
20ed2 69 6c 64 20 74 68 65 20 69 6e 64 65 78 20 6b 65  ild the index ke
20ed3 79 73 2e 20 49 66 20 62 6f 74 68 20 27 78 27 20  ys. If both 'x' 
20ed4 61 6e 64 20 74 68 65 0a 20 20 20 20 20 20 2a 2a  and the.      **
20ed5 20 53 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74 65   SELECT... state
20ed6 6d 65 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e 73  ment are columns
20ed7 2c 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 20 61  , then numeric a
20ed8 66 66 69 6e 69 74 79 20 69 73 20 75 73 65 64 0a  ffinity is used.
20ed9 20 20 20 20 20 20 2a 2a 20 69 66 20 65 69 74 68        ** if eith
20eda 65 72 20 63 6f 6c 75 6d 6e 20 68 61 73 20 4e 55  er column has NU
20edb 4d 45 52 49 43 20 6f 72 20 49 4e 54 45 47 45 52  MERIC or INTEGER
20edc 20 61 66 66 69 6e 69 74 79 2e 20 49 66 20 6e 65   affinity. If ne
20edd 69 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 27  ither.      ** '
20ede 78 27 20 6e 6f 72 20 74 68 65 20 53 45 4c 45 43  x' nor the SELEC
20edf 54 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 20 61  T... statement a
20ee0 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e  re columns, then
20ee1 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69 74   numeric affinit
20ee2 79 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 75 73  y.      ** is us
20ee3 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ed..      */.   
20ee4 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65     pExpr->iTable
20ee5 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
20ee6 2b 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20  +;.      addr = 
20ee7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
20ee8 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65  2(v, OP_OpenEphe
20ee9 6d 65 72 61 6c 2c 20 70 45 78 70 72 2d 3e 69 54  meral, pExpr->iT
20eea 61 62 6c 65 2c 20 21 69 73 52 6f 77 69 64 29 3b  able, !isRowid);
20eeb 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 6b  .      memset(&k
20eec 65 79 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f  eyInfo, 0, sizeo
20eed 66 28 6b 65 79 49 6e 66 6f 29 29 3b 0a 20 20 20  f(keyInfo));.   
20eee 20 20 20 6b 65 79 49 6e 66 6f 2e 6e 46 69 65 6c     keyInfo.nFiel
20eef 64 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 69 66  d = 1;..      if
20ef0 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  ( ExprHasPropert
20ef1 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53  y(pExpr, EP_xIsS
20ef2 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20  elect) ){.      
20ef3 20 20 2f 2a 20 43 61 73 65 20 31 3a 20 20 20 20    /* Case 1:    
20ef4 20 65 78 70 72 20 49 4e 20 28 53 45 4c 45 43 54   expr IN (SELECT
20ef5 20 2e 2e 2e 29 0a 20 20 20 20 20 20 20 20 2a 2a   ...).        **
20ef6 0a 20 20 20 20 20 20 20 20 2a 2a 20 47 65 6e 65  .        ** Gene
20ef7 72 61 74 65 20 63 6f 64 65 20 74 6f 20 77 72 69  rate code to wri
20ef8 74 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f  te the results o
20ef9 66 20 74 68 65 20 73 65 6c 65 63 74 20 69 6e 74  f the select int
20efa 6f 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 0a  o the temporary.
20efb 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65          ** table
20efc 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 6f   allocated and o
20efd 70 65 6e 65 64 20 61 62 6f 76 65 2e 0a 20 20 20  pened above..   
20efe 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
20eff 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b  SelectDest dest;
20f00 0a 20 20 20 20 20 20 20 20 45 78 70 72 4c 69 73  .        ExprLis
20f01 74 20 2a 70 45 4c 69 73 74 3b 0a 0a 20 20 20 20  t *pEList;..    
20f02 20 20 20 20 61 73 73 65 72 74 28 20 21 69 73 52      assert( !isR
20f03 6f 77 69 64 20 29 3b 0a 20 20 20 20 20 20 20 20  owid );.        
20f04 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73  sqlite3SelectDes
20f05 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54  tInit(&dest, SRT
20f06 5f 53 65 74 2c 20 70 45 78 70 72 2d 3e 69 54 61  _Set, pExpr->iTa
20f07 62 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 64 65  ble);.        de
20f08 73 74 2e 61 66 66 69 6e 69 74 79 20 3d 20 28 75  st.affinity = (u
20f09 38 29 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 20  8)affinity;.    
20f0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 45 78      assert( (pEx
20f0b 70 72 2d 3e 69 54 61 62 6c 65 26 30 78 30 30 30  pr->iTable&0x000
20f0c 30 46 46 46 46 29 3d 3d 70 45 78 70 72 2d 3e 69  0FFFF)==pExpr->i
20f0d 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 20 20 20  Table );.       
20f0e 20 69 66 28 20 73 71 6c 69 74 65 33 53 65 6c 65   if( sqlite3Sele
20f0f 63 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  ct(pParse, pExpr
20f10 2d 3e 78 2e 70 53 65 6c 65 63 74 2c 20 26 64 65  ->x.pSelect, &de
20f11 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  st) ){.         
20f12 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20   return;.       
20f13 20 7d 0a 20 20 20 20 20 20 20 20 70 45 4c 69 73   }.        pELis
20f14 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 53 65  t = pExpr->x.pSe
20f15 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20  lect->pEList;.  
20f16 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53        if( ALWAYS
20f17 28 70 45 4c 69 73 74 21 3d 30 20 26 26 20 70 45  (pEList!=0 && pE
20f18 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 30 29 20 29  List->nExpr>0) )
20f19 7b 20 0a 20 20 20 20 20 20 20 20 20 20 6b 65 79  { .          key
20f1a 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d 20  Info.aColl[0] = 
20f1b 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d  sqlite3BinaryCom
20f1c 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72  pareCollSeq(pPar
20f1d 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
20f1e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
20f1f 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
20f20 70 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  pr);.        }. 
20f21 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
20f22 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 21 3d 30  Expr->x.pList!=0
20f23 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43   ){.        /* C
20f24 61 73 65 20 32 3a 20 20 20 20 20 65 78 70 72 20  ase 2:     expr 
20f25 49 4e 20 28 65 78 70 72 6c 69 73 74 29 0a 20 20  IN (exprlist).  
20f26 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
20f27 20 2a 2a 20 46 6f 72 20 65 61 63 68 20 65 78 70   ** For each exp
20f28 72 65 73 73 69 6f 6e 2c 20 62 75 69 6c 64 20 61  ression, build a
20f29 6e 20 69 6e 64 65 78 20 6b 65 79 20 66 72 6f 6d  n index key from
20f2a 20 74 68 65 20 65 76 61 6c 75 61 74 69 6f 6e 20   the evaluation 
20f2b 61 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 73  and.        ** s
20f2c 74 6f 72 65 20 69 74 20 69 6e 20 74 68 65 20 74  tore it in the t
20f2d 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2e 20  emporary table. 
20f2e 49 66 20 3c 65 78 70 72 3e 20 69 73 20 61 20 63  If <expr> is a c
20f2f 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 75 73 65 0a  olumn, then use.
20f30 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20          ** that 
20f31 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e 69 74 79  columns affinity
20f32 20 77 68 65 6e 20 62 75 69 6c 64 69 6e 67 20 69   when building i
20f33 6e 64 65 78 20 6b 65 79 73 2e 20 49 66 20 3c 65  ndex keys. If <e
20f34 78 70 72 3e 20 69 73 20 6e 6f 74 0a 20 20 20 20  xpr> is not.    
20f35 20 20 20 20 2a 2a 20 61 20 63 6f 6c 75 6d 6e 2c      ** a column,
20f36 20 75 73 65 20 6e 75 6d 65 72 69 63 20 61 66 66   use numeric aff
20f37 69 6e 69 74 79 2e 0a 20 20 20 20 20 20 20 20 2a  inity..        *
20f38 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b  /.        int i;
20f39 0a 20 20 20 20 20 20 20 20 45 78 70 72 4c 69 73  .        ExprLis
20f3a 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 70 72  t *pList = pExpr
20f3b 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 20  ->x.pList;.     
20f3c 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69     struct ExprLi
20f3d 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
20f3e 20 20 20 20 20 20 20 20 69 6e 74 20 72 31 2c 20          int r1, 
20f3f 72 32 2c 20 72 33 3b 0a 0a 20 20 20 20 20 20 20  r2, r3;..       
20f40 20 69 66 28 20 21 61 66 66 69 6e 69 74 79 20 29   if( !affinity )
20f41 7b 0a 20 20 20 20 20 20 20 20 20 20 61 66 66 69  {.          affi
20f42 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46  nity = SQLITE_AF
20f43 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 20 20  F_NONE;.        
20f44 7d 0a 20 20 20 20 20 20 20 20 6b 65 79 49 6e 66  }.        keyInf
20f45 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d 20 73 71 6c  o.aColl[0] = sql
20f46 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28  ite3ExprCollSeq(
20f47 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
20f48 4c 65 66 74 29 3b 0a 0a 20 20 20 20 20 20 20 20  Left);..        
20f49 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20  /* Loop through 
20f4a 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20  each expression 
20f4b 69 6e 20 3c 65 78 70 72 6c 69 73 74 3e 2e 20 2a  in <exprlist>. *
20f4c 2f 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20 73  /.        r1 = s
20f4d 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
20f4e 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
20f4f 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 47 65    r2 = sqlite3Ge
20f50 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
20f51 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
20f52 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
20f53 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 32 29 3b 0a  P_Null, 0, r2);.
20f54 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 70 4c          for(i=pL
20f55 69 73 74 2d 3e 6e 45 78 70 72 2c 20 70 49 74 65  ist->nExpr, pIte
20f56 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3e 30 3b  m=pList->a; i>0;
20f57 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i--, pItem++){.
20f58 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20 2a            Expr *
20f59 70 45 32 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78  pE2 = pItem->pEx
20f5a 70 72 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f  pr;..          /
20f5b 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73  * If the express
20f5c 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 6e 73 74  ion is not const
20f5d 61 6e 74 20 74 68 65 6e 20 77 65 20 77 69 6c 6c  ant then we will
20f5e 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20 20   need to.       
20f5f 20 20 20 2a 2a 20 64 69 73 61 62 6c 65 20 74 68     ** disable th
20f60 65 20 74 65 73 74 20 74 68 61 74 20 77 61 73 20  e test that was 
20f61 67 65 6e 65 72 61 74 65 64 20 61 62 6f 76 65 20  generated above 
20f62 74 68 61 74 20 6d 61 6b 65 73 20 73 75 72 65 0a  that makes sure.
20f63 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69            ** thi
20f64 73 20 63 6f 64 65 20 6f 6e 6c 79 20 65 78 65 63  s code only exec
20f65 75 74 65 73 20 6f 6e 63 65 2e 20 20 42 65 63 61  utes once.  Beca
20f66 75 73 65 20 66 6f 72 20 61 20 6e 6f 6e 2d 63 6f  use for a non-co
20f67 6e 73 74 61 6e 74 0a 20 20 20 20 20 20 20 20 20  nstant.         
20f68 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 77   ** expression w
20f69 65 20 6e 65 65 64 20 74 6f 20 72 65 72 75 6e 20  e need to rerun 
20f6a 74 68 69 73 20 63 6f 64 65 20 65 61 63 68 20 74  this code each t
20f6b 69 6d 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  ime..          *
20f6c 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  /.          if( 
20f6d 74 65 73 74 41 64 64 72 20 26 26 20 21 73 71 6c  testAddr && !sql
20f6e 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61  ite3ExprIsConsta
20f6f 6e 74 28 70 45 32 29 20 29 7b 0a 20 20 20 20 20  nt(pE2) ){.     
20f70 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
20f71 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76  beChangeToNoop(v
20f72 2c 20 74 65 73 74 41 64 64 72 2d 31 2c 20 32 29  , testAddr-1, 2)
20f73 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 74 65  ;.            te
20f74 73 74 41 64 64 72 20 3d 20 30 3b 0a 20 20 20 20  stAddr = 0;.    
20f75 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
20f76 20 20 20 2f 2a 20 45 76 61 6c 75 61 74 65 20 74     /* Evaluate t
20f77 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e  he expression an
20f78 64 20 69 6e 73 65 72 74 20 69 74 20 69 6e 74 6f  d insert it into
20f79 20 74 68 65 20 74 65 6d 70 20 74 61 62 6c 65 20   the temp table 
20f7a 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 33 20  */.          r3 
20f7b 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
20f7c 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20  eTarget(pParse, 
20f7d 70 45 32 2c 20 72 31 29 3b 0a 20 20 20 20 20 20  pE2, r1);.      
20f7e 20 20 20 20 69 66 28 20 69 73 52 6f 77 69 64 20      if( isRowid 
20f7f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  ){.            s
20f80 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
20f81 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74  (v, OP_MustBeInt
20f82 2c 20 72 33 2c 20 73 71 6c 69 74 65 33 56 64 62  , r3, sqlite3Vdb
20f83 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 2b  eCurrentAddr(v)+
20f84 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  2);.            
20f85 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
20f86 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  3(v, OP_Insert, 
20f87 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72  pExpr->iTable, r
20f88 32 2c 20 72 33 29 3b 0a 20 20 20 20 20 20 20 20  2, r3);.        
20f89 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
20f8a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
20f8b 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp4(v, OP_Mak
20f8c 65 52 65 63 6f 72 64 2c 20 72 33 2c 20 31 2c 20  eRecord, r3, 1, 
20f8d 72 32 2c 20 26 61 66 66 69 6e 69 74 79 2c 20 31  r2, &affinity, 1
20f8e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  );.            s
20f8f 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
20f90 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50  ffinityChange(pP
20f91 61 72 73 65 2c 20 72 33 2c 20 31 29 3b 0a 20 20  arse, r3, 1);.  
20f92 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
20f93 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
20f94 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 70 45 78  P_IdxInsert, pEx
20f95 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72 32 29 3b  pr->iTable, r2);
20f96 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
20f97 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73       }.        s
20f98 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
20f99 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29  pReg(pParse, r1)
20f9a 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
20f9b 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
20f9c 70 50 61 72 73 65 2c 20 72 32 29 3b 0a 20 20 20  pParse, r2);.   
20f9d 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21     }.      if( !
20f9e 69 73 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20  isRowid ){.     
20f9f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
20fa0 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2c 20  angeP4(v, addr, 
20fa1 28 76 6f 69 64 20 2a 29 26 6b 65 79 49 6e 66 6f  (void *)&keyInfo
20fa2 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20  , P4_KEYINFO);. 
20fa3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
20fa4 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63  ak;.    }..    c
20fa5 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20  ase TK_EXISTS:. 
20fa6 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43     case TK_SELEC
20fa7 54 3a 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  T:.    default: 
20fa8 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  {.      /* If th
20fa9 69 73 20 68 61 73 20 74 6f 20 62 65 20 61 20 73  is has to be a s
20faa 63 61 6c 61 72 20 53 45 4c 45 43 54 2e 20 20 47  calar SELECT.  G
20fab 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
20fac 70 75 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a  put the.      **
20fad 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 73   value of this s
20fae 65 6c 65 63 74 20 69 6e 20 61 20 6d 65 6d 6f 72  elect in a memor
20faf 79 20 63 65 6c 6c 20 61 6e 64 20 72 65 63 6f 72  y cell and recor
20fb0 64 20 74 68 65 20 6e 75 6d 62 65 72 0a 20 20 20  d the number.   
20fb1 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 6d 65 6d     ** of the mem
20fb2 6f 72 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f 6c  ory cell in iCol
20fb3 75 6d 6e 2e 20 20 49 66 20 74 68 69 73 20 69 73  umn.  If this is
20fb4 20 61 6e 20 45 58 49 53 54 53 2c 20 77 72 69 74   an EXISTS, writ
20fb5 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 69 6e  e.      ** an in
20fb6 74 65 67 65 72 20 30 20 28 6e 6f 74 20 65 78 69  teger 0 (not exi
20fb7 73 74 73 29 20 6f 72 20 31 20 28 65 78 69 73 74  sts) or 1 (exist
20fb8 73 29 20 69 6e 74 6f 20 61 20 6d 65 6d 6f 72 79  s) into a memory
20fb9 20 63 65 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 61   cell.      ** a
20fba 6e 64 20 72 65 63 6f 72 64 20 74 68 61 74 20 6d  nd record that m
20fbb 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 69 43  emory cell in iC
20fbc 6f 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a  olumn..      */.
20fbd 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e        static con
20fbe 73 74 20 54 6f 6b 65 6e 20 6f 6e 65 20 3d 20 7b  st Token one = {
20fbf 20 22 31 22 2c 20 31 20 7d 3b 20 20 2f 2a 20 54   "1", 1 };  /* T
20fc0 6f 6b 65 6e 20 66 6f 72 20 6c 69 74 65 72 61 6c  oken for literal
20fc1 20 76 61 6c 75 65 20 31 20 2a 2f 0a 20 20 20 20   value 1 */.    
20fc2 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20    Select *pSel; 
20fc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20fc4 20 20 20 20 20 20 20 20 2f 2a 20 53 45 4c 45 43          /* SELEC
20fc5 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 65  T statement to e
20fc6 6e 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 53  ncode */.      S
20fc7 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b 20  electDest dest; 
20fc8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20fc9 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64       /* How to d
20fca 65 61 6c 20 77 69 74 68 20 53 45 4c 45 43 74 20  eal with SELECt 
20fcb 72 65 73 75 6c 74 20 2a 2f 0a 0a 20 20 20 20 20  result */..     
20fcc 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72   testcase( pExpr
20fcd 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20  ->op==TK_EXISTS 
20fce 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
20fcf 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  e( pExpr->op==TK
20fd0 5f 53 45 4c 45 43 54 20 29 3b 0a 20 20 20 20 20  _SELECT );.     
20fd1 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
20fd2 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 7c 7c  op==TK_EXISTS ||
20fd3 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53   pExpr->op==TK_S
20fd4 45 4c 45 43 54 20 29 3b 0a 0a 20 20 20 20 20 20  ELECT );..      
20fd5 61 73 73 65 72 74 28 20 45 78 70 72 48 61 73 50  assert( ExprHasP
20fd6 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
20fd7 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a  P_xIsSelect) );.
20fd8 20 20 20 20 20 20 70 53 65 6c 20 3d 20 70 45 78        pSel = pEx
20fd9 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 3b 0a 20  pr->x.pSelect;. 
20fda 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65       sqlite3Sele
20fdb 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74  ctDestInit(&dest
20fdc 2c 20 30 2c 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  , 0, ++pParse->n
20fdd 4d 65 6d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  Mem);.      if( 
20fde 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45  pExpr->op==TK_SE
20fdf 4c 45 43 54 20 29 7b 0a 20 20 20 20 20 20 20 20  LECT ){.        
20fe0 64 65 73 74 2e 65 44 65 73 74 20 3d 20 53 52 54  dest.eDest = SRT
20fe1 5f 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 73 71  _Mem;.        sq
20fe2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
20fe3 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 64  v, OP_Null, 0, d
20fe4 65 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20  est.iParm);.    
20fe5 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
20fe6 28 76 2c 20 22 49 6e 69 74 20 73 75 62 71 75 65  (v, "Init subque
20fe7 72 79 20 72 65 73 75 6c 74 22 29 29 3b 0a 20 20  ry result"));.  
20fe8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
20fe9 20 20 20 64 65 73 74 2e 65 44 65 73 74 20 3d 20     dest.eDest = 
20fea 53 52 54 5f 45 78 69 73 74 73 3b 0a 20 20 20 20  SRT_Exists;.    
20feb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
20fec 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
20fed 67 65 72 2c 20 30 2c 20 64 65 73 74 2e 69 50 61  ger, 0, dest.iPa
20fee 72 6d 29 3b 0a 20 20 20 20 20 20 20 20 56 64 62  rm);.        Vdb
20fef 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 49 6e  eComment((v, "In
20ff0 69 74 20 45 58 49 53 54 53 20 72 65 73 75 6c 74  it EXISTS result
20ff1 22 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  "));.      }.   
20ff2 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
20ff3 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
20ff4 20 70 53 65 6c 2d 3e 70 4c 69 6d 69 74 29 3b 0a   pSel->pLimit);.
20ff5 20 20 20 20 20 20 70 53 65 6c 2d 3e 70 4c 69 6d        pSel->pLim
20ff6 69 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  it = sqlite3PExp
20ff7 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 54  r(pParse, TK_INT
20ff8 45 47 45 52 2c 20 30 2c 20 30 2c 20 26 6f 6e 65  EGER, 0, 0, &one
20ff9 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  );.      if( sql
20ffa 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73  ite3Select(pPars
20ffb 65 2c 20 70 53 65 6c 2c 20 26 64 65 73 74 29 20  e, pSel, &dest) 
20ffc 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
20ffd 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  n;.      }.     
20ffe 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20   pExpr->iColumn 
20fff 3d 20 28 69 31 36 29 64 65 73 74 2e 69 50 61 72  = (i16)dest.iPar
21000 6d 3b 0a 20 20 20 20 20 20 45 78 70 72 53 65 74  m;.      ExprSet
21001 49 72 72 65 64 75 63 69 62 6c 65 28 70 45 78 70  Irreducible(pExp
21002 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  r);.      break;
21003 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66  .    }.  }..  if
21004 28 20 74 65 73 74 41 64 64 72 20 29 7b 0a 20 20  ( testAddr ){.  
21005 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
21006 70 48 65 72 65 28 76 2c 20 74 65 73 74 41 64 64  pHere(v, testAdd
21007 72 2d 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  r-1);.  }.  sqli
21008 74 65 33 45 78 70 72 43 61 63 68 65 50 6f 70 28  te3ExprCachePop(
21009 70 50 61 72 73 65 2c 20 31 29 3b 0a 0a 20 20 72  pParse, 1);..  r
2100a 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20  eturn;.}.#endif 
2100b 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  /* SQLITE_OMIT_S
2100c 55 42 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a  UBQUERY */../*.*
2100d 2a 20 44 75 70 6c 69 63 61 74 65 20 61 6e 20 38  * Duplicate an 8
2100e 2d 62 79 74 65 20 76 61 6c 75 65 0a 2a 2f 0a 73  -byte value.*/.s
2100f 74 61 74 69 63 20 63 68 61 72 20 2a 64 75 70 38  tatic char *dup8
21010 62 79 74 65 73 28 56 64 62 65 20 2a 76 2c 20 63  bytes(Vdbe *v, c
21011 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 29 7b 0a  onst char *in){.
21012 20 20 63 68 61 72 20 2a 6f 75 74 20 3d 20 73 71    char *out = sq
21013 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
21014 28 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76  (sqlite3VdbeDb(v
21015 29 2c 20 38 29 3b 0a 20 20 69 66 28 20 6f 75 74  ), 8);.  if( out
21016 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 6f   ){.    memcpy(o
21017 75 74 2c 20 69 6e 2c 20 38 29 3b 0a 20 20 7d 0a  ut, in, 8);.  }.
21018 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a    return out;.}.
21019 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
2101a 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74  an instruction t
2101b 68 61 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65  hat will put the
2101c 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a   floating point.
2101d 2a 2a 20 76 61 6c 75 65 20 64 65 73 63 72 69 62  ** value describ
2101e 65 64 20 62 79 20 7a 5b 30 2e 2e 6e 2d 31 5d 20  ed by z[0..n-1] 
2101f 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 69 4d  into register iM
21020 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b  em..**.** The z[
21021 5d 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 72  ] string will pr
21022 6f 62 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65  obably not be ze
21023 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20  ro-terminated.  
21024 42 75 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d  But the .** z[n]
21025 20 63 68 61 72 61 63 74 65 72 20 69 73 20 67 75   character is gu
21026 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20 73  aranteed to be s
21027 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 20 64 6f  omething that do
21028 65 73 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c  es not look.** l
21029 69 6b 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 61  ike the continua
2102a 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62  tion of the numb
2102b 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  er..*/.static vo
2102c 69 64 20 63 6f 64 65 52 65 61 6c 28 56 64 62 65  id codeReal(Vdbe
2102d 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20   *v, const char 
2102e 2a 7a 2c 20 69 6e 74 20 6e 65 67 61 74 65 46 6c  *z, int negateFl
2102f 61 67 2c 20 69 6e 74 20 69 4d 65 6d 29 7b 0a 20  ag, int iMem){. 
21030 20 69 66 28 20 41 4c 57 41 59 53 28 7a 21 3d 30   if( ALWAYS(z!=0
21031 29 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20  ) ){.    double 
21032 76 61 6c 75 65 3b 0a 20 20 20 20 63 68 61 72 20  value;.    char 
21033 2a 7a 56 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  *zV;.    sqlite3
21034 41 74 6f 46 28 7a 2c 20 26 76 61 6c 75 65 29 3b  AtoF(z, &value);
21035 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 73 71  .    assert( !sq
21036 6c 69 74 65 33 49 73 4e 61 4e 28 76 61 6c 75 65  lite3IsNaN(value
21037 29 20 29 3b 20 2f 2a 20 54 68 65 20 6e 65 77 20  ) ); /* The new 
21038 41 74 6f 46 20 6e 65 76 65 72 20 72 65 74 75 72  AtoF never retur
21039 6e 73 20 4e 61 4e 20 2a 2f 0a 20 20 20 20 69 66  ns NaN */.    if
2103a 28 20 6e 65 67 61 74 65 46 6c 61 67 20 29 20 76  ( negateFlag ) v
2103b 61 6c 75 65 20 3d 20 2d 76 61 6c 75 65 3b 0a 20  alue = -value;. 
2103c 20 20 20 7a 56 20 3d 20 64 75 70 38 62 79 74 65     zV = dup8byte
2103d 73 28 76 2c 20 28 63 68 61 72 2a 29 26 76 61 6c  s(v, (char*)&val
2103e 75 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ue);.    sqlite3
2103f 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
21040 5f 52 65 61 6c 2c 20 30 2c 20 69 4d 65 6d 2c 20  _Real, 0, iMem, 
21041 30 2c 20 7a 56 2c 20 50 34 5f 52 45 41 4c 29 3b  0, zV, P4_REAL);
21042 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47  .  }.}.../*.** G
21043 65 6e 65 72 61 74 65 20 61 6e 20 69 6e 73 74 72  enerate an instr
21044 75 63 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c  uction that will
21045 20 70 75 74 20 74 68 65 20 69 6e 74 65 67 65 72   put the integer
21046 20 64 65 73 63 72 69 62 65 20 62 79 0a 2a 2a 20   describe by.** 
21047 74 65 78 74 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69  text z[0..n-1] i
21048 6e 74 6f 20 72 65 67 69 73 74 65 72 20 69 4d 65  nto register iMe
21049 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d  m..**.** The z[]
2104a 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 72 6f   string will pro
2104b 62 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65 72  bably not be zer
2104c 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 42  o-terminated.  B
2104d 75 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20  ut the .** z[n] 
2104e 63 68 61 72 61 63 74 65 72 20 69 73 20 67 75 61  character is gua
2104f 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20 73 6f  ranteed to be so
21050 6d 65 74 68 69 6e 67 20 74 68 61 74 20 64 6f 65  mething that doe
21051 73 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69  s not look.** li
21052 6b 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 74  ke the continuat
21053 69 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  ion of the numbe
21054 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  r..*/.static voi
21055 64 20 63 6f 64 65 49 6e 74 65 67 65 72 28 56 64  d codeInteger(Vd
21056 62 65 20 2a 76 2c 20 45 78 70 72 20 2a 70 45 78  be *v, Expr *pEx
21057 70 72 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67 2c  pr, int negFlag,
21058 20 69 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 69 66   int iMem){.  if
21059 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26  ( pExpr->flags &
2105a 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 29 7b 0a   EP_IntValue ){.
2105b 20 20 20 20 69 6e 74 20 69 20 3d 20 70 45 78 70      int i = pExp
2105c 72 2d 3e 75 2e 69 56 61 6c 75 65 3b 0a 20 20 20  r->u.iValue;.   
2105d 20 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 69   if( negFlag ) i
2105e 20 3d 20 2d 69 3b 0a 20 20 20 20 73 71 6c 69 74   = -i;.    sqlit
2105f 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
21060 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 69  OP_Integer, i, i
21061 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Mem);.  }else{. 
21062 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
21063 20 3d 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b   = pExpr->u.zTok
21064 65 6e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  en;.    assert( 
21065 7a 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  z!=0 );.    if( 
21066 73 71 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42  sqlite3FitsIn64B
21067 69 74 73 28 7a 2c 20 6e 65 67 46 6c 61 67 29 20  its(z, negFlag) 
21068 29 7b 0a 20 20 20 20 20 20 69 36 34 20 76 61 6c  ){.      i64 val
21069 75 65 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a  ue;.      char *
2106a 7a 56 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  zV;.      sqlite
2106b 33 41 74 6f 69 36 34 28 7a 2c 20 26 76 61 6c 75  3Atoi64(z, &valu
2106c 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 65  e);.      if( ne
2106d 67 46 6c 61 67 20 29 20 76 61 6c 75 65 20 3d 20  gFlag ) value = 
2106e 2d 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 7a 56  -value;.      zV
2106f 20 3d 20 64 75 70 38 62 79 74 65 73 28 76 2c 20   = dup8bytes(v, 
21070 28 63 68 61 72 2a 29 26 76 61 6c 75 65 29 3b 0a  (char*)&value);.
21071 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21072 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49 6e  eAddOp4(v, OP_In
21073 74 36 34 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 2c  t64, 0, iMem, 0,
21074 20 7a 56 2c 20 50 34 5f 49 4e 54 36 34 29 3b 0a   zV, P4_INT64);.
21075 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
21076 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 7a 2c 20   codeReal(v, z, 
21077 6e 65 67 46 6c 61 67 2c 20 69 4d 65 6d 29 3b 0a  negFlag, iMem);.
21078 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
21079 2a 2a 20 43 6c 65 61 72 20 61 20 63 61 63 68 65  ** Clear a cache
2107a 20 65 6e 74 72 79 2e 0a 2a 2f 0a 73 74 61 74 69   entry..*/.stati
2107b 63 20 76 6f 69 64 20 63 61 63 68 65 45 6e 74 72  c void cacheEntr
2107c 79 43 6c 65 61 72 28 50 61 72 73 65 20 2a 70 50  yClear(Parse *pP
2107d 61 72 73 65 2c 20 73 74 72 75 63 74 20 79 43 6f  arse, struct yCo
2107e 6c 43 61 63 68 65 20 2a 70 29 7b 0a 20 20 69 66  lCache *p){.  if
2107f 28 20 70 2d 3e 74 65 6d 70 52 65 67 20 29 7b 0a  ( p->tempReg ){.
21080 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e      if( pParse->
21081 6e 54 65 6d 70 52 65 67 3c 41 72 72 61 79 53 69  nTempReg<ArraySi
21082 7a 65 28 70 50 61 72 73 65 2d 3e 61 54 65 6d 70  ze(pParse->aTemp
21083 52 65 67 29 20 29 7b 0a 20 20 20 20 20 20 70 50  Reg) ){.      pP
21084 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 5b 70  arse->aTempReg[p
21085 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 2b  Parse->nTempReg+
21086 2b 5d 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 20  +] = p->iReg;.  
21087 20 20 7d 0a 20 20 20 20 70 2d 3e 74 65 6d 70 52    }.    p->tempR
21088 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a  eg = 0;.  }.}...
21089 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 69 6e 20  /*.** Record in 
2108a 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65  the column cache
2108b 20 74 68 61 74 20 61 20 70 61 72 74 69 63 75 6c   that a particul
2108c 61 72 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 61  ar column from a
2108d 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20 74  .** particular t
2108e 61 62 6c 65 20 69 73 20 73 74 6f 72 65 64 20 69  able is stored i
2108f 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 72  n a particular r
21090 65 67 69 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49  egister..*/.SQLI
21091 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
21092 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
21093 53 74 6f 72 65 28 50 61 72 73 65 20 2a 70 50 61  Store(Parse *pPa
21094 72 73 65 2c 20 69 6e 74 20 69 54 61 62 2c 20 69  rse, int iTab, i
21095 6e 74 20 69 43 6f 6c 2c 20 69 6e 74 20 69 52 65  nt iCol, int iRe
21096 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  g){.  int i;.  i
21097 6e 74 20 6d 69 6e 4c 72 75 3b 0a 20 20 69 6e 74  nt minLru;.  int
21098 20 69 64 78 4c 72 75 3b 0a 20 20 73 74 72 75 63   idxLru;.  struc
21099 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a  t yColCache *p;.
2109a 0a 20 20 61 73 73 65 72 74 28 20 69 52 65 67 3e  .  assert( iReg>
2109b 30 20 29 3b 20 20 2f 2a 20 52 65 67 69 73 74 65  0 );  /* Registe
2109c 72 20 6e 75 6d 62 65 72 73 20 61 72 65 20 61 6c  r numbers are al
2109d 77 61 79 73 20 70 6f 73 69 74 69 76 65 20 2a 2f  ways positive */
2109e 0a 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3e  .  assert( iCol>
2109f 3d 2d 31 20 26 26 20 69 43 6f 6c 3c 33 32 37 36  =-1 && iCol<3276
210a0 38 20 29 3b 20 20 2f 2a 20 46 69 6e 69 74 65 20  8 );  /* Finite 
210a1 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 73 20 2a  column numbers *
210a2 2f 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 72 65  /..  /* First re
210a3 70 6c 61 63 65 20 61 6e 79 20 65 78 69 73 74 69  place any existi
210a4 6e 67 20 65 6e 74 72 79 20 2a 2f 0a 20 20 66 6f  ng entry */.  fo
210a5 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d  r(i=0, p=pParse-
210a6 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51  >aColCache; i<SQ
210a7 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b  LITE_N_COLCACHE;
210a8 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20   i++, p++){.    
210a9 69 66 28 20 70 2d 3e 69 52 65 67 20 26 26 20 70  if( p->iReg && p
210aa 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 26  ->iTable==iTab &
210ab 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 69 43  & p->iColumn==iC
210ac 6f 6c 20 29 7b 0a 20 20 20 20 20 20 63 61 63 68  ol ){.      cach
210ad 65 45 6e 74 72 79 43 6c 65 61 72 28 70 50 61 72  eEntryClear(pPar
210ae 73 65 2c 20 70 29 3b 0a 20 20 20 20 20 20 70 2d  se, p);.      p-
210af 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 65  >iLevel = pParse
210b0 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 0a 20  ->iCacheLevel;. 
210b1 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 69       p->iReg = i
210b2 52 65 67 3b 0a 20 20 20 20 20 20 70 2d 3e 61 66  Reg;.      p->af
210b3 66 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 20  fChange = 0;.   
210b4 20 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61 72     p->lru = pPar
210b5 73 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b 3b  se->iCacheCnt++;
210b6 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
210b7 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46     }.  }..  /* F
210b8 69 6e 64 20 61 6e 20 65 6d 70 74 79 20 73 6c 6f  ind an empty slo
210b9 74 20 61 6e 64 20 72 65 70 6c 61 63 65 20 69 74  t and replace it
210ba 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70   */.  for(i=0, p
210bb 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63  =pParse->aColCac
210bc 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43  he; i<SQLITE_N_C
210bd 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b  OLCACHE; i++, p+
210be 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69  +){.    if( p->i
210bf 52 65 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Reg==0 ){.      
210c0 70 2d 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61 72  p->iLevel = pPar
210c1 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b  se->iCacheLevel;
210c2 0a 20 20 20 20 20 20 70 2d 3e 69 54 61 62 6c 65  .      p->iTable
210c3 20 3d 20 69 54 61 62 3b 0a 20 20 20 20 20 20 70   = iTab;.      p
210c4 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c  ->iColumn = iCol
210c5 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20  ;.      p->iReg 
210c6 3d 20 69 52 65 67 3b 0a 20 20 20 20 20 20 70 2d  = iReg;.      p-
210c7 3e 61 66 66 43 68 61 6e 67 65 20 3d 20 30 3b 0a  >affChange = 0;.
210c8 20 20 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67        p->tempReg
210c9 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 6c   = 0;.      p->l
210ca 72 75 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61  ru = pParse->iCa
210cb 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 20 20  cheCnt++;.      
210cc 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
210cd 7d 0a 0a 20 20 2f 2a 20 52 65 70 6c 61 63 65 20  }..  /* Replace 
210ce 74 68 65 20 6c 61 73 74 20 72 65 63 65 6e 74 6c  the last recentl
210cf 79 20 75 73 65 64 20 2a 2f 0a 20 20 6d 69 6e 4c  y used */.  minL
210d0 72 75 20 3d 20 30 78 37 66 66 66 66 66 66 66 3b  ru = 0x7fffffff;
210d1 0a 20 20 69 64 78 4c 72 75 20 3d 20 2d 31 3b 0a  .  idxLru = -1;.
210d2 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61    for(i=0, p=pPa
210d3 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20  rse->aColCache; 
210d4 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41  i<SQLITE_N_COLCA
210d5 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a  CHE; i++, p++){.
210d6 20 20 20 20 69 66 28 20 70 2d 3e 6c 72 75 3c 6d      if( p->lru<m
210d7 69 6e 4c 72 75 20 29 7b 0a 20 20 20 20 20 20 69  inLru ){.      i
210d8 64 78 4c 72 75 20 3d 20 69 3b 0a 20 20 20 20 20  dxLru = i;.     
210d9 20 6d 69 6e 4c 72 75 20 3d 20 70 2d 3e 6c 72 75   minLru = p->lru
210da 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
210db 28 20 41 4c 57 41 59 53 28 69 64 78 4c 72 75 3e  ( ALWAYS(idxLru>
210dc 3d 30 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 26  =0) ){.    p = &
210dd 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68  pParse->aColCach
210de 65 5b 69 64 78 4c 72 75 5d 3b 0a 20 20 20 20 70  e[idxLru];.    p
210df 2d 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61 72 73  ->iLevel = pPars
210e0 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 0a  e->iCacheLevel;.
210e1 20 20 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20      p->iTable = 
210e2 69 54 61 62 3b 0a 20 20 20 20 70 2d 3e 69 43 6f  iTab;.    p->iCo
210e3 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20 20  lumn = iCol;.   
210e4 20 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b   p->iReg = iReg;
210e5 0a 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e 67  .    p->affChang
210e6 65 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 74 65  e = 0;.    p->te
210e7 6d 70 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 70  mpReg = 0;.    p
210e8 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e  ->lru = pParse->
210e9 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20  iCacheCnt++;.   
210ea 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 7d 0a 0a   return;.  }.}..
210eb 2f 2a 0a 2a 2a 20 49 6e 64 69 63 61 74 65 20 74  /*.** Indicate t
210ec 68 61 74 20 61 20 72 65 67 69 73 74 65 72 20 69  hat a register i
210ed 73 20 62 65 69 6e 67 20 6f 76 65 72 77 72 69 74  s being overwrit
210ee 74 65 6e 2e 20 20 50 75 72 67 65 20 74 68 65 20  ten.  Purge the 
210ef 72 65 67 69 73 74 65 72 0a 2a 2a 20 66 72 6f 6d  register.** from
210f0 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68   the column cach
210f1 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
210f2 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
210f3 33 45 78 70 72 43 61 63 68 65 52 65 6d 6f 76 65  3ExprCacheRemove
210f4 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
210f5 69 6e 74 20 69 52 65 67 29 7b 0a 20 20 69 6e 74  int iReg){.  int
210f6 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f   i;.  struct yCo
210f7 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72  lCache *p;.  for
210f8 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e  (i=0, p=pParse->
210f9 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c  aColCache; i<SQL
210fa 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20  ITE_N_COLCACHE; 
210fb 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69  i++, p++){.    i
210fc 66 28 20 70 2d 3e 69 52 65 67 3d 3d 69 52 65 67  f( p->iReg==iReg
210fd 20 29 7b 0a 20 20 20 20 20 20 63 61 63 68 65 45   ){.      cacheE
210fe 6e 74 72 79 43 6c 65 61 72 28 70 50 61 72 73 65  ntryClear(pParse
210ff 2c 20 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69  , p);.      p->i
21100 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  Reg = 0;.    }. 
21101 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 65   }.}../*.** Reme
21102 6d 62 65 72 20 74 68 65 20 63 75 72 72 65 6e 74  mber the current
21103 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 63 6f   column cache co
21104 6e 74 65 78 74 2e 20 20 41 6e 79 20 6e 65 77 20  ntext.  Any new 
21105 65 6e 74 72 69 65 73 20 61 64 64 65 64 0a 2a 2a  entries added.**
21106 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 63 6f   added to the co
21107 6c 75 6d 6e 20 63 61 63 68 65 20 61 66 74 65 72  lumn cache after
21108 20 74 68 69 73 20 63 61 6c 6c 20 61 72 65 20 72   this call are r
21109 65 6d 6f 76 65 64 20 77 68 65 6e 20 74 68 65 0a  emoved when the.
2110a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ** corresponding
2110b 20 70 6f 70 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a   pop occurs..*/.
2110c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2110d 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
2110e 61 63 68 65 50 75 73 68 28 50 61 72 73 65 20 2a  achePush(Parse *
2110f 70 50 61 72 73 65 29 7b 0a 20 20 70 50 61 72 73  pParse){.  pPars
21110 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 2b 2b  e->iCacheLevel++
21111 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76  ;.}../*.** Remov
21112 65 20 66 72 6f 6d 20 74 68 65 20 63 6f 6c 75 6d  e from the colum
21113 6e 20 63 61 63 68 65 20 61 6e 79 20 65 6e 74 72  n cache any entr
21114 69 65 73 20 74 68 61 74 20 77 65 72 65 20 61 64  ies that were ad
21115 64 65 64 20 73 69 6e 63 65 20 74 68 65 0a 2a 2a  ded since the.**
21116 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 4e 20   the previous N 
21117 50 75 73 68 20 6f 70 65 72 61 74 69 6f 6e 73 2e  Push operations.
21118 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
21119 2c 20 72 65 73 74 6f 72 65 20 74 68 65 20 63 61  , restore the ca
2111a 63 68 65 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74  che.** to the st
2111b 61 74 65 20 69 74 20 77 61 73 20 69 6e 20 4e 20  ate it was in N 
2111c 50 75 73 68 65 73 20 61 67 6f 2e 0a 2a 2f 0a 53  Pushes ago..*/.S
2111d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2111e 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61  id sqlite3ExprCa
2111f 63 68 65 50 6f 70 28 50 61 72 73 65 20 2a 70 50  chePop(Parse *pP
21120 61 72 73 65 2c 20 69 6e 74 20 4e 29 7b 0a 20 20  arse, int N){.  
21121 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
21122 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20  yColCache *p;.  
21123 61 73 73 65 72 74 28 20 4e 3e 30 20 29 3b 0a 20  assert( N>0 );. 
21124 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
21125 3e 69 43 61 63 68 65 4c 65 76 65 6c 3e 3d 4e 20  >iCacheLevel>=N 
21126 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 69 43 61  );.  pParse->iCa
21127 63 68 65 4c 65 76 65 6c 20 2d 3d 20 4e 3b 0a 20  cheLevel -= N;. 
21128 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72   for(i=0, p=pPar
21129 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69  se->aColCache; i
2112a 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43  <SQLITE_N_COLCAC
2112b 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20  HE; i++, p++){. 
2112c 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 20 26     if( p->iReg &
2112d 26 20 70 2d 3e 69 4c 65 76 65 6c 3e 70 50 61 72  & p->iLevel>pPar
2112e 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 20  se->iCacheLevel 
2112f 29 7b 0a 20 20 20 20 20 20 63 61 63 68 65 45 6e  ){.      cacheEn
21130 74 72 79 43 6c 65 61 72 28 70 50 61 72 73 65 2c  tryClear(pParse,
21131 20 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52   p);.      p->iR
21132 65 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  eg = 0;.    }.  
21133 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20  }.}../*.** When 
21134 61 20 63 61 63 68 65 64 20 63 6f 6c 75 6d 6e 20  a cached column 
21135 69 73 20 72 65 75 73 65 64 2c 20 6d 61 6b 65 20  is reused, make 
21136 73 75 72 65 20 74 68 61 74 20 69 74 73 20 72 65  sure that its re
21137 67 69 73 74 65 72 20 69 73 0a 2a 2a 20 6e 6f 20  gister is.** no 
21138 6c 6f 6e 67 65 72 20 61 76 61 69 6c 61 62 6c 65  longer available
21139 20 61 73 20 61 20 74 65 6d 70 20 72 65 67 69 73   as a temp regis
2113a 74 65 72 2e 20 20 74 69 63 6b 65 74 20 23 33 38  ter.  ticket #38
2113b 37 39 3a 20 20 74 68 61 74 20 73 61 6d 65 0a 2a  79:  that same.*
2113c 2a 20 72 65 67 69 73 74 65 72 20 6d 69 67 68 74  * register might
2113d 20 62 65 20 69 6e 20 74 68 65 20 63 61 63 68 65   be in the cache
2113e 20 69 6e 20 6d 75 6c 74 69 70 6c 65 20 70 6c 61   in multiple pla
2113f 63 65 73 2c 20 73 6f 20 62 65 20 73 75 72 65 20  ces, so be sure 
21140 74 6f 0a 2a 2a 20 67 65 74 20 74 68 65 6d 20 61  to.** get them a
21141 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ll..*/.static vo
21142 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61  id sqlite3ExprCa
21143 63 68 65 50 69 6e 52 65 67 69 73 74 65 72 28 50  chePinRegister(P
21144 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
21145 74 20 69 52 65 67 29 7b 0a 20 20 69 6e 74 20 69  t iReg){.  int i
21146 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43  ;.  struct yColC
21147 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69  ache *p;.  for(i
21148 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43  =0, p=pParse->aC
21149 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54  olCache; i<SQLIT
2114a 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b  E_N_COLCACHE; i+
2114b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28  +, p++){.    if(
2114c 20 70 2d 3e 69 52 65 67 3d 3d 69 52 65 67 20 29   p->iReg==iReg )
2114d 7b 0a 20 20 20 20 20 20 70 2d 3e 74 65 6d 70 52  {.      p->tempR
2114e 65 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  eg = 0;.    }.  
2114f 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  }.}../*.** Gener
21150 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69  ate code that wi
21151 6c 6c 20 65 78 74 72 61 63 74 20 74 68 65 20 69  ll extract the i
21152 43 6f 6c 75 6d 6e 2d 74 68 20 63 6f 6c 75 6d 6e  Column-th column
21153 20 66 72 6f 6d 0a 2a 2a 20 74 61 62 6c 65 20 70   from.** table p
21154 54 61 62 20 61 6e 64 20 73 74 6f 72 65 20 74 68  Tab and store th
21155 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69  e column value i
21156 6e 20 61 20 72 65 67 69 73 74 65 72 2e 20 20 41  n a register.  A
21157 6e 20 65 66 66 6f 72 74 0a 2a 2a 20 69 73 20 6d  n effort.** is m
21158 61 64 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65  ade to store the
21159 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 6e   column value in
2115a 20 72 65 67 69 73 74 65 72 20 69 52 65 67 2c 20   register iReg, 
2115b 62 75 74 20 74 68 69 73 20 69 73 0a 2a 2a 20 6e  but this is.** n
2115c 6f 74 20 67 75 61 72 61 6e 74 65 65 64 2e 20 20  ot guaranteed.  
2115d 54 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20  The location of 
2115e 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65  the column value
2115f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
21160 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20 62  .** There must b
21161 65 20 61 6e 20 6f 70 65 6e 20 63 75 72 73 6f 72  e an open cursor
21162 20 74 6f 20 70 54 61 62 20 69 6e 20 69 54 61 62   to pTab in iTab
21163 6c 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75  le when this rou
21164 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65  tine.** is calle
21165 64 2e 20 20 49 66 20 69 43 6f 6c 75 6d 6e 3c 30  d.  If iColumn<0
21166 20 74 68 65 6e 20 63 6f 64 65 20 69 73 20 67 65   then code is ge
21167 6e 65 72 61 74 65 64 20 74 68 61 74 20 65 78 74  nerated that ext
21168 72 61 63 74 73 20 74 68 65 20 72 6f 77 69 64 2e  racts the rowid.
21169 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
2116a 69 6e 65 20 6d 69 67 68 74 20 61 74 74 65 6d 70  ine might attemp
2116b 74 20 74 6f 20 72 65 75 73 65 20 74 68 65 20 76  t to reuse the v
2116c 61 6c 75 65 20 6f 66 20 74 68 65 20 63 6f 6c 75  alue of the colu
2116d 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61 73 20 61  mn that.** has a
2116e 6c 72 65 61 64 79 20 62 65 65 6e 20 6c 6f 61 64  lready been load
2116f 65 64 20 69 6e 74 6f 20 61 20 72 65 67 69 73 74  ed into a regist
21170 65 72 2e 20 20 54 68 65 20 76 61 6c 75 65 20 77  er.  The value w
21171 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 62 65  ill always.** be
21172 20 75 73 65 64 20 69 66 20 69 74 20 68 61 73 20   used if it has 
21173 6e 6f 74 20 75 6e 64 65 72 67 6f 6e 65 20 61 6e  not undergone an
21174 79 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67  y affinity chang
21175 65 73 2e 20 20 42 75 74 20 69 66 0a 2a 2a 20 61  es.  But if.** a
21176 6e 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67  n affinity chang
21177 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 2c 20  e has occurred, 
21178 74 68 65 6e 20 74 68 65 20 63 61 63 68 65 64 20  then the cached 
21179 76 61 6c 75 65 20 77 69 6c 6c 20 6f 6e 6c 79 20  value will only 
2117a 62 65 0a 2a 2a 20 75 73 65 64 20 69 66 20 61 6c  be.** used if al
2117b 6c 6f 77 41 66 66 43 68 6e 67 20 69 73 20 74 72  lowAffChng is tr
2117c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ue..*/.SQLITE_PR
2117d 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2117e 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c 75  3ExprCodeGetColu
2117f 6d 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  mn(.  Parse *pPa
21180 72 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 69 6e  rse,   /* Parsin
21181 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72  g and code gener
21182 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  ating context */
21183 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  .  Table *pTab, 
21184 20 20 20 20 2f 2a 20 44 65 73 63 72 69 70 74 69      /* Descripti
21185 6f 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  on of the table 
21186 77 65 20 61 72 65 20 72 65 61 64 69 6e 67 20 66  we are reading f
21187 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f  rom */.  int iCo
21188 6c 75 6d 6e 2c 20 20 20 20 20 2f 2a 20 49 6e 64  lumn,     /* Ind
21189 65 78 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ex of the table 
2118a 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20  column */.  int 
2118b 69 54 61 62 6c 65 2c 20 20 20 20 20 20 2f 2a 20  iTable,      /* 
2118c 54 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74  The cursor point
2118d 69 6e 67 20 74 6f 20 74 68 65 20 74 61 62 6c 65  ing to the table
2118e 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65 67 2c 20   */.  int iReg, 
2118f 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20         /* Store 
21190 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a  results here */.
21191 20 20 69 6e 74 20 61 6c 6c 6f 77 41 66 66 43 68    int allowAffCh
21192 6e 67 20 2f 2a 20 54 72 75 65 20 69 66 20 70 72  ng /* True if pr
21193 69 6f 72 20 61 66 66 69 6e 69 74 79 20 63 68 61  ior affinity cha
21194 6e 67 65 73 20 61 72 65 20 4f 4b 20 2a 2f 0a 29  nges are OK */.)
21195 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  {.  Vdbe *v = pP
21196 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
21197 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79  nt i;.  struct y
21198 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20 20  ColCache *p;..  
21199 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73  for(i=0, p=pPars
2119a 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c  e->aColCache; i<
2119b 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48  SQLITE_N_COLCACH
2119c 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20  E; i++, p++){.  
2119d 20 20 69 66 28 20 70 2d 3e 69 52 65 67 3e 30 20    if( p->iReg>0 
2119e 26 26 20 70 2d 3e 69 54 61 62 6c 65 3d 3d 69 54  && p->iTable==iT
2119f 61 62 6c 65 20 26 26 20 70 2d 3e 69 43 6f 6c 75  able && p->iColu
211a0 6d 6e 3d 3d 69 43 6f 6c 75 6d 6e 0a 20 20 20 20  mn==iColumn.    
211a1 20 20 20 20 20 20 20 26 26 20 28 21 70 2d 3e 61         && (!p->a
211a2 66 66 43 68 61 6e 67 65 20 7c 7c 20 61 6c 6c 6f  ffChange || allo
211a3 77 41 66 66 43 68 6e 67 29 20 29 7b 0a 20 20 20  wAffChng) ){.   
211a4 20 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61 72     p->lru = pPar
211a5 73 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b 3b  se->iCacheCnt++;
211a6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
211a7 70 72 43 61 63 68 65 50 69 6e 52 65 67 69 73 74  prCachePinRegist
211a8 65 72 28 70 50 61 72 73 65 2c 20 70 2d 3e 69 52  er(pParse, p->iR
211a9 65 67 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  eg);.      retur
211aa 6e 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 7d  n p->iReg;.    }
211ab 0a 20 20 7d 20 20 0a 20 20 61 73 73 65 72 74 28  .  }  .  assert(
211ac 20 76 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 69   v!=0 );.  if( i
211ad 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 20 20  Column<0 ){.    
211ae 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
211af 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69  2(v, OP_Rowid, i
211b0 54 61 62 6c 65 2c 20 69 52 65 67 29 3b 0a 20 20  Table, iReg);.  
211b1 7d 65 6c 73 65 20 69 66 28 20 41 4c 57 41 59 53  }else if( ALWAYS
211b2 28 70 54 61 62 21 3d 30 29 20 29 7b 0a 20 20 20  (pTab!=0) ){.   
211b3 20 69 6e 74 20 6f 70 20 3d 20 49 73 56 69 72 74   int op = IsVirt
211b4 75 61 6c 28 70 54 61 62 29 20 3f 20 4f 50 5f 56  ual(pTab) ? OP_V
211b5 43 6f 6c 75 6d 6e 20 3a 20 4f 50 5f 43 6f 6c 75  Column : OP_Colu
211b6 6d 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  mn;.    sqlite3V
211b7 64 62 65 41 64 64 4f 70 33 28 76 2c 20 6f 70 2c  dbeAddOp3(v, op,
211b8 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e   iTable, iColumn
211b9 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 73 71 6c  , iReg);.    sql
211ba 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c  ite3ColumnDefaul
211bb 74 28 76 2c 20 70 54 61 62 2c 20 69 43 6f 6c 75  t(v, pTab, iColu
211bc 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 20 7d 0a 20  mn, iReg);.  }. 
211bd 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
211be 65 53 74 6f 72 65 28 70 50 61 72 73 65 2c 20 69  eStore(pParse, i
211bf 54 61 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20  Table, iColumn, 
211c0 69 52 65 67 29 3b 0a 20 20 72 65 74 75 72 6e 20  iReg);.  return 
211c1 69 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  iReg;.}../*.** C
211c2 6c 65 61 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 20  lear all column 
211c3 63 61 63 68 65 20 65 6e 74 72 69 65 73 2e 0a 2a  cache entries..*
211c4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
211c5 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
211c6 72 43 61 63 68 65 43 6c 65 61 72 28 50 61 72 73  rCacheClear(Pars
211c7 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 6e  e *pParse){.  in
211c8 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43  t i;.  struct yC
211c9 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20 20 66  olCache *p;..  f
211ca 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65  or(i=0, p=pParse
211cb 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53  ->aColCache; i<S
211cc 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45  QLITE_N_COLCACHE
211cd 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20  ; i++, p++){.   
211ce 20 69 66 28 20 70 2d 3e 69 52 65 67 20 29 7b 0a   if( p->iReg ){.
211cf 20 20 20 20 20 20 63 61 63 68 65 45 6e 74 72 79        cacheEntry
211d0 43 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70 29  Clear(pParse, p)
211d1 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20  ;.      p->iReg 
211d2 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  = 0;.    }.  }.}
211d3 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 74  ../*.** Record t
211d4 68 65 20 66 61 63 74 20 74 68 61 74 20 61 6e 20  he fact that an 
211d5 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20  affinity change 
211d6 68 61 73 20 6f 63 63 75 72 72 65 64 20 6f 6e 20  has occurred on 
211d7 69 43 6f 75 6e 74 0a 2a 2a 20 72 65 67 69 73 74  iCount.** regist
211d8 65 72 73 20 73 74 61 72 74 69 6e 67 20 77 69 74  ers starting wit
211d9 68 20 69 53 74 61 72 74 2e 0a 2a 2f 0a 53 51 4c  h iStart..*/.SQL
211da 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
211db 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
211dc 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28  eAffinityChange(
211dd 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
211de 6e 74 20 69 53 74 61 72 74 2c 20 69 6e 74 20 69  nt iStart, int i
211df 43 6f 75 6e 74 29 7b 0a 20 20 69 6e 74 20 69 45  Count){.  int iE
211e0 6e 64 20 3d 20 69 53 74 61 72 74 20 2b 20 69 43  nd = iStart + iC
211e1 6f 75 6e 74 20 2d 20 31 3b 0a 20 20 69 6e 74 20  ount - 1;.  int 
211e2 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c  i;.  struct yCol
211e3 43 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28  Cache *p;.  for(
211e4 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61  i=0, p=pParse->a
211e5 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49  ColCache; i<SQLI
211e6 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69  TE_N_COLCACHE; i
211e7 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 6e  ++, p++){.    in
211e8 74 20 72 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20  t r = p->iReg;. 
211e9 20 20 20 69 66 28 20 72 3e 3d 69 53 74 61 72 74     if( r>=iStart
211ea 20 26 26 20 72 3c 3d 69 45 6e 64 20 29 7b 0a 20   && r<=iEnd ){. 
211eb 20 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e 67       p->affChang
211ec 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  e = 1;.    }.  }
211ed 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
211ee 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 76 65 20  te code to move 
211ef 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 72 65 67  content from reg
211f0 69 73 74 65 72 73 20 69 46 72 6f 6d 2e 2e 2e 69  isters iFrom...i
211f1 46 72 6f 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20 6f  From+nReg-1.** o
211f2 76 65 72 20 74 6f 20 69 54 6f 2e 2e 69 54 6f 2b  ver to iTo..iTo+
211f3 6e 52 65 67 2d 31 2e 20 4b 65 65 70 20 74 68 65  nReg-1. Keep the
211f4 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 75 70   column cache up
211f5 2d 74 6f 2d 64 61 74 65 2e 0a 2a 2f 0a 53 51 4c  -to-date..*/.SQL
211f6 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
211f7 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
211f8 4d 6f 76 65 28 50 61 72 73 65 20 2a 70 50 61 72  Move(Parse *pPar
211f9 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69  se, int iFrom, i
211fa 6e 74 20 69 54 6f 2c 20 69 6e 74 20 6e 52 65 67  nt iTo, int nReg
211fb 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74  ){.  int i;.  st
211fc 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a  ruct yColCache *
211fd 70 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 69  p;.  if( NEVER(i
211fe 46 72 6f 6d 3d 3d 69 54 6f 29 20 29 20 72 65 74  From==iTo) ) ret
211ff 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  urn;.  sqlite3Vd
21200 62 65 41 64 64 4f 70 33 28 70 50 61 72 73 65 2d  beAddOp3(pParse-
21201 3e 70 56 64 62 65 2c 20 4f 50 5f 4d 6f 76 65 2c  >pVdbe, OP_Move,
21202 20 69 46 72 6f 6d 2c 20 69 54 6f 2c 20 6e 52 65   iFrom, iTo, nRe
21203 67 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70  g);.  for(i=0, p
21204 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63  =pParse->aColCac
21205 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43  he; i<SQLITE_N_C
21206 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b  OLCACHE; i++, p+
21207 2b 29 7b 0a 20 20 20 20 69 6e 74 20 78 20 3d 20  +){.    int x = 
21208 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 69 66 28  p->iReg;.    if(
21209 20 78 3e 3d 69 46 72 6f 6d 20 26 26 20 78 3c 69   x>=iFrom && x<i
2120a 46 72 6f 6d 2b 6e 52 65 67 20 29 7b 0a 20 20 20  From+nReg ){.   
2120b 20 20 20 70 2d 3e 69 52 65 67 20 2b 3d 20 69 54     p->iReg += iT
2120c 6f 2d 69 46 72 6f 6d 3b 0a 20 20 20 20 7d 0a 20  o-iFrom;.    }. 
2120d 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65   }.}../*.** Gene
2120e 72 61 74 65 20 63 6f 64 65 20 74 6f 20 63 6f 70  rate code to cop
2120f 79 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 72  y content from r
21210 65 67 69 73 74 65 72 73 20 69 46 72 6f 6d 2e 2e  egisters iFrom..
21211 2e 69 46 72 6f 6d 2b 6e 52 65 67 2d 31 0a 2a 2a  .iFrom+nReg-1.**
21212 20 6f 76 65 72 20 74 6f 20 69 54 6f 2e 2e 69 54   over to iTo..iT
21213 6f 2b 6e 52 65 67 2d 31 2e 0a 2a 2f 0a 53 51 4c  o+nReg-1..*/.SQL
21214 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
21215 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
21216 43 6f 70 79 28 50 61 72 73 65 20 2a 70 50 61 72  Copy(Parse *pPar
21217 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69  se, int iFrom, i
21218 6e 74 20 69 54 6f 2c 20 69 6e 74 20 6e 52 65 67  nt iTo, int nReg
21219 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66  ){.  int i;.  if
2121a 28 20 4e 45 56 45 52 28 69 46 72 6f 6d 3d 3d 69  ( NEVER(iFrom==i
2121b 54 6f 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  To) ) return;.  
2121c 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65 67 3b  for(i=0; i<nReg;
2121d 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74   i++){.    sqlit
2121e 65 33 56 64 62 65 41 64 64 4f 70 32 28 70 50 61  e3VdbeAddOp2(pPa
2121f 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 43  rse->pVdbe, OP_C
21220 6f 70 79 2c 20 69 46 72 6f 6d 2b 69 2c 20 69 54  opy, iFrom+i, iT
21221 6f 2b 69 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  o+i);.  }.}../*.
21222 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  ** Return true i
21223 66 20 61 6e 79 20 72 65 67 69 73 74 65 72 20 69  f any register i
21224 6e 20 74 68 65 20 72 61 6e 67 65 20 69 46 72 6f  n the range iFro
21225 6d 2e 2e 69 54 6f 20 28 69 6e 63 6c 75 73 69 76  m..iTo (inclusiv
21226 65 29 0a 2a 2a 20 69 73 20 75 73 65 64 20 61 73  e).** is used as
21227 20 70 61 72 74 20 6f 66 20 74 68 65 20 63 6f 6c   part of the col
21228 75 6d 6e 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74  umn cache..*/.st
21229 61 74 69 63 20 69 6e 74 20 75 73 65 64 41 73 43  atic int usedAsC
2122a 6f 6c 75 6d 6e 43 61 63 68 65 28 50 61 72 73 65  olumnCache(Parse
2122b 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 46   *pParse, int iF
2122c 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 29 7b 0a 20  rom, int iTo){. 
2122d 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74   int i;.  struct
2122e 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20   yColCache *p;. 
2122f 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72   for(i=0, p=pPar
21230 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69  se->aColCache; i
21231 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43  <SQLITE_N_COLCAC
21232 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20  HE; i++, p++){. 
21233 20 20 20 69 6e 74 20 72 20 3d 20 70 2d 3e 69 52     int r = p->iR
21234 65 67 3b 0a 20 20 20 20 69 66 28 20 72 3e 3d 69  eg;.    if( r>=i
21235 46 72 6f 6d 20 26 26 20 72 3c 3d 69 54 6f 20 29  From && r<=iTo )
21236 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20   return 1;.  }. 
21237 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
21238 0a 2a 2a 20 49 66 20 74 68 65 20 6c 61 73 74 20  .** If the last 
21239 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f 64 65  instruction code
2123a 64 20 69 73 20 61 6e 20 65 70 68 65 6d 65 72 61  d is an ephemera
2123b 6c 20 63 6f 70 79 20 6f 66 20 61 6e 79 20 6f 66  l copy of any of
2123c 0a 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65 72  .** the register
2123d 73 20 69 6e 20 74 68 65 20 6e 52 65 67 20 72 65  s in the nReg re
2123e 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e  gisters beginnin
2123f 67 20 77 69 74 68 20 69 52 65 67 2c 20 74 68 65  g with iReg, the
21240 6e 0a 2a 2a 20 63 6f 6e 76 65 72 74 20 74 68 65  n.** convert the
21241 20 6c 61 73 74 20 69 6e 73 74 72 75 63 74 69 6f   last instructio
21242 6e 20 66 72 6f 6d 20 4f 50 5f 53 43 6f 70 79 20  n from OP_SCopy 
21243 74 6f 20 4f 50 5f 43 6f 70 79 2e 0a 2a 2f 0a 53  to OP_Copy..*/.S
21244 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
21245 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 48 61  id sqlite3ExprHa
21246 72 64 43 6f 70 79 28 50 61 72 73 65 20 2a 70 50  rdCopy(Parse *pP
21247 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67 2c 20  arse, int iReg, 
21248 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 56 64 62  int nReg){.  Vdb
21249 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 56 64 62 65  eOp *pOp;.  Vdbe
2124a 20 2a 76 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   *v;..  assert( 
2124b 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c  pParse->db->mall
2124c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 29 3b 0a 20  ocFailed==0 );. 
2124d 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64   v = pParse->pVd
2124e 62 65 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21  be;.  assert( v!
2124f 3d 30 20 29 3b 0a 20 20 70 4f 70 20 3d 20 73 71  =0 );.  pOp = sq
21250 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76  lite3VdbeGetOp(v
21251 2c 20 2d 31 29 3b 0a 20 20 61 73 73 65 72 74 28  , -1);.  assert(
21252 20 70 4f 70 21 3d 30 20 29 3b 0a 20 20 69 66 28   pOp!=0 );.  if(
21253 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
21254 5f 53 43 6f 70 79 20 26 26 20 70 4f 70 2d 3e 70  _SCopy && pOp->p
21255 31 3e 3d 69 52 65 67 20 26 26 20 70 4f 70 2d 3e  1>=iReg && pOp->
21256 70 31 3c 69 52 65 67 2b 6e 52 65 67 20 29 7b 0a  p1<iReg+nReg ){.
21257 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20      pOp->opcode 
21258 3d 20 4f 50 5f 43 6f 70 79 3b 0a 20 20 7d 0a 7d  = OP_Copy;.  }.}
21259 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
2125a 20 63 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 74   code to store t
2125b 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
2125c 69 41 6c 69 61 73 2d 74 68 20 61 6c 69 61 73 20  iAlias-th alias 
2125d 69 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 74  in register.** t
2125e 61 72 67 65 74 2e 20 20 54 68 65 20 66 69 72 73  arget.  The firs
2125f 74 20 74 69 6d 65 20 74 68 69 73 20 69 73 20 63  t time this is c
21260 61 6c 6c 65 64 2c 20 70 45 78 70 72 20 69 73 20  alled, pExpr is 
21261 65 76 61 6c 75 61 74 65 64 20 74 6f 20 63 6f 6d  evaluated to com
21262 70 75 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c 75  pute.** the valu
21263 65 20 6f 66 20 74 68 65 20 61 6c 69 61 73 2e 20  e of the alias. 
21264 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 73 74   The value is st
21265 6f 72 65 64 20 69 6e 20 61 6e 20 61 75 78 69 6c  ored in an auxil
21266 69 61 72 79 20 72 65 67 69 73 74 65 72 0a 2a 2a  iary register.**
21267 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20   and the number 
21268 6f 66 20 74 68 61 74 20 72 65 67 69 73 74 65 72  of that register
21269 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f   is returned.  O
2126a 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c  n subsequent cal
2126b 6c 73 2c 0a 2a 2a 20 74 68 65 20 72 65 67 69 73  ls,.** the regis
2126c 74 65 72 20 6e 75 6d 62 65 72 20 69 73 20 72 65  ter number is re
2126d 74 75 72 6e 65 64 20 77 69 74 68 6f 75 74 20 67  turned without g
2126e 65 6e 65 72 61 74 69 6e 67 20 61 6e 79 20 63 6f  enerating any co
2126f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74  de..**.** Note t
21270 68 61 74 20 69 6e 20 6f 72 64 65 72 20 66 6f 72  hat in order for
21271 20 74 68 69 73 20 74 6f 20 77 6f 72 6b 2c 20 63   this to work, c
21272 6f 64 65 20 6d 75 73 74 20 62 65 20 67 65 6e 65  ode must be gene
21273 72 61 74 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20  rated in the.** 
21274 73 61 6d 65 20 6f 72 64 65 72 20 74 68 61 74 20  same order that 
21275 69 74 20 69 73 20 65 78 65 63 75 74 65 64 2e 0a  it is executed..
21276 2a 2a 0a 2a 2a 20 41 6c 69 61 73 65 73 20 61 72  **.** Aliases ar
21277 65 20 6e 75 6d 62 65 72 65 64 20 73 74 61 72 74  e numbered start
21278 69 6e 67 20 77 69 74 68 20 31 2e 20 20 53 6f 20  ing with 1.  So 
21279 69 41 6c 69 61 73 20 69 73 20 69 6e 20 74 68 65  iAlias is in the
2127a 20 72 61 6e 67 65 0a 2a 2a 20 6f 66 20 31 20 74   range.** of 1 t
2127b 6f 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73  o pParse->nAlias
2127c 20 69 6e 63 6c 75 73 69 76 65 2e 20 20 0a 2a 2a   inclusive.  .**
2127d 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 61 41 6c 69  .** pParse->aAli
2127e 61 73 5b 69 41 6c 69 61 73 2d 31 5d 20 72 65 63  as[iAlias-1] rec
2127f 6f 72 64 73 20 74 68 65 20 72 65 67 69 73 74 65  ords the registe
21280 72 20 6e 75 6d 62 65 72 20 77 68 65 72 65 20 74  r number where t
21281 68 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74  he value.** of t
21282 68 65 20 69 41 6c 69 61 73 2d 74 68 20 61 6c 69  he iAlias-th ali
21283 61 73 20 69 73 20 73 74 6f 72 65 64 2e 20 20 49  as is stored.  I
21284 66 20 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 61  f zero, that mea
21285 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61  ns that the.** a
21286 6c 69 61 73 20 68 61 73 20 6e 6f 74 20 79 65 74  lias has not yet
21287 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 2e 0a   been computed..
21288 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f  */.static int co
21289 64 65 41 6c 69 61 73 28 50 61 72 73 65 20 2a 70  deAlias(Parse *p
2128a 50 61 72 73 65 2c 20 69 6e 74 20 69 41 6c 69 61  Parse, int iAlia
2128b 73 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20  s, Expr *pExpr, 
2128c 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 23 69 66  int target){.#if
2128d 20 30 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62   0.  sqlite3 *db
2128e 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
2128f 20 69 6e 74 20 69 52 65 67 3b 0a 20 20 69 66 28   int iReg;.  if(
21290 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41   pParse->nAliasA
21291 6c 6c 6f 63 3c 70 50 61 72 73 65 2d 3e 6e 41 6c  lloc<pParse->nAl
21292 69 61 73 20 29 7b 0a 20 20 20 20 70 50 61 72 73  ias ){.    pPars
21293 65 2d 3e 61 41 6c 69 61 73 20 3d 20 73 71 6c 69  e->aAlias = sqli
21294 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72  te3DbReallocOrFr
21295 65 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61  ee(db, pParse->a
21296 41 6c 69 61 73 2c 0a 20 20 20 20 20 20 20 20 20  Alias,.         
21297 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21298 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70          sizeof(p
21299 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 30 5d  Parse->aAlias[0]
2129a 29 2a 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73  )*pParse->nAlias
2129b 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
2129c 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
2129d 65 64 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 41  ed && pParse->nA
2129e 6c 69 61 73 41 6c 6c 6f 63 3e 30 20 29 3b 0a 20  liasAlloc>0 );. 
2129f 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f     if( db->mallo
212a0 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e  cFailed ) return
212a1 20 30 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26   0;.    memset(&
212a2 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 70  pParse->aAlias[p
212a3 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c  Parse->nAliasAll
212a4 6f 63 5d 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  oc], 0,.        
212a5 20 20 20 28 70 50 61 72 73 65 2d 3e 6e 41 6c 69     (pParse->nAli
212a6 61 73 2d 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61  as-pParse->nAlia
212a7 73 41 6c 6c 6f 63 29 2a 73 69 7a 65 6f 66 28 70  sAlloc)*sizeof(p
212a8 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 30 5d  Parse->aAlias[0]
212a9 29 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  ));.    pParse->
212aa 6e 41 6c 69 61 73 41 6c 6c 6f 63 20 3d 20 70 50  nAliasAlloc = pP
212ab 61 72 73 65 2d 3e 6e 41 6c 69 61 73 3b 0a 20 20  arse->nAlias;.  
212ac 7d 0a 20 20 61 73 73 65 72 74 28 20 69 41 6c 69  }.  assert( iAli
212ad 61 73 3e 30 20 26 26 20 69 41 6c 69 61 73 3c 3d  as>0 && iAlias<=
212ae 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29  pParse->nAlias )
212af 3b 0a 20 20 69 52 65 67 20 3d 20 70 50 61 72 73  ;.  iReg = pPars
212b0 65 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61 73  e->aAlias[iAlias
212b1 2d 31 5d 3b 0a 20 20 69 66 28 20 69 52 65 67 3d  -1];.  if( iReg=
212b2 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50  =0 ){.    if( pP
212b3 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65  arse->iCacheLeve
212b4 6c 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 52 65  l>0 ){.      iRe
212b5 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  g = sqlite3ExprC
212b6 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65  odeTarget(pParse
212b7 2c 20 70 45 78 70 72 2c 20 74 61 72 67 65 74 29  , pExpr, target)
212b8 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
212b9 20 20 20 69 52 65 67 20 3d 20 2b 2b 70 50 61 72     iReg = ++pPar
212ba 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20  se->nMem;.      
212bb 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
212bc 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 69  pParse, pExpr, i
212bd 52 65 67 29 3b 0a 20 20 20 20 20 20 70 50 61 72  Reg);.      pPar
212be 73 65 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61  se->aAlias[iAlia
212bf 73 2d 31 5d 20 3d 20 69 52 65 67 3b 0a 20 20 20  s-1] = iReg;.   
212c0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
212c1 69 52 65 67 3b 0a 23 65 6c 73 65 0a 20 20 55 4e  iReg;.#else.  UN
212c2 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69  USED_PARAMETER(i
212c3 41 6c 69 61 73 29 3b 0a 20 20 72 65 74 75 72 6e  Alias);.  return
212c4 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
212c5 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70  Target(pParse, p
212c6 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 23  Expr, target);.#
212c7 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  endif.}../*.** G
212c8 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e 74  enerate code int
212c9 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 56 64  o the current Vd
212ca 62 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74  be to evaluate t
212cb 68 65 20 67 69 76 65 6e 0a 2a 2a 20 65 78 70 72  he given.** expr
212cc 65 73 73 69 6f 6e 2e 20 20 41 74 74 65 6d 70 74  ession.  Attempt
212cd 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 72 65   to store the re
212ce 73 75 6c 74 73 20 69 6e 20 72 65 67 69 73 74 65  sults in registe
212cf 72 20 22 74 61 72 67 65 74 22 2e 0a 2a 2a 20 52  r "target"..** R
212d0 65 74 75 72 6e 20 74 68 65 20 72 65 67 69 73 74  eturn the regist
212d1 65 72 20 77 68 65 72 65 20 72 65 73 75 6c 74 73  er where results
212d2 20 61 72 65 20 73 74 6f 72 65 64 2e 0a 2a 2a 0a   are stored..**.
212d3 2a 2a 20 57 69 74 68 20 74 68 69 73 20 72 6f 75  ** With this rou
212d4 74 69 6e 65 2c 20 74 68 65 72 65 20 69 73 20 6e  tine, there is n
212d5 6f 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74  o guarantee that
212d6 20 72 65 73 75 6c 74 73 20 77 69 6c 6c 0a 2a 2a   results will.**
212d7 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 74 61   be stored in ta
212d8 72 67 65 74 2e 20 20 54 68 65 20 72 65 73 75 6c  rget.  The resul
212d9 74 20 6d 69 67 68 74 20 62 65 20 73 74 6f 72 65  t might be store
212da 64 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72 0a  d in some other.
212db 2a 2a 20 72 65 67 69 73 74 65 72 20 69 66 20 69  ** register if i
212dc 74 20 69 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20  t is convenient 
212dd 74 6f 20 64 6f 20 73 6f 2e 20 20 54 68 65 20 63  to do so.  The c
212de 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a  alling function.
212df 2a 2a 20 6d 75 73 74 20 63 68 65 63 6b 20 74 68  ** must check th
212e0 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 61 6e  e return code an
212e1 64 20 6d 6f 76 65 20 74 68 65 20 72 65 73 75 6c  d move the resul
212e2 74 73 20 74 6f 20 74 68 65 20 64 65 73 69 72 65  ts to the desire
212e3 64 0a 2a 2a 20 72 65 67 69 73 74 65 72 2e 0a 2a  d.** register..*
212e4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
212e5 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
212e6 43 6f 64 65 54 61 72 67 65 74 28 50 61 72 73 65  CodeTarget(Parse
212e7 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
212e8 70 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65  pExpr, int targe
212e9 74 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  t){.  Vdbe *v = 
212ea 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20  pParse->pVdbe;  
212eb 2f 2a 20 54 68 65 20 56 4d 20 75 6e 64 65 72 20  /* The VM under 
212ec 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a  construction */.
212ed 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20    int op;       
212ee 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
212ef 68 65 20 6f 70 63 6f 64 65 20 62 65 69 6e 67 20  he opcode being 
212f0 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  coded */.  int i
212f1 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 20 20  nReg = target;  
212f2 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20       /* Results 
212f3 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74  stored in regist
212f4 65 72 20 69 6e 52 65 67 20 2a 2f 0a 20 20 69 6e  er inReg */.  in
212f5 74 20 72 65 67 46 72 65 65 31 20 3d 20 30 3b 20  t regFree1 = 0; 
212f6 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f          /* If no
212f7 6e 2d 7a 65 72 6f 20 66 72 65 65 20 74 68 69 73  n-zero free this
212f8 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73   temporary regis
212f9 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ter */.  int reg
212fa 46 72 65 65 32 20 3d 20 30 3b 20 20 20 20 20 20  Free2 = 0;      
212fb 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72     /* If non-zer
212fc 6f 20 66 72 65 65 20 74 68 69 73 20 74 65 6d 70  o free this temp
212fd 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 20 2a  orary register *
212fe 2f 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 2c 20  /.  int r1, r2, 
212ff 72 33 2c 20 72 34 3b 20 20 20 20 20 20 20 2f 2a  r3, r4;       /*
21300 20 56 61 72 69 6f 75 73 20 72 65 67 69 73 74 65   Various registe
21301 72 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20 73  r numbers */.  s
21302 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
21303 72 73 65 2d 3e 64 62 3b 20 2f 2a 20 54 68 65 20  rse->db; /* The 
21304 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
21305 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ion */..  assert
21306 28 20 74 61 72 67 65 74 3e 30 20 26 26 20 74 61  ( target>0 && ta
21307 72 67 65 74 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d  rget<=pParse->nM
21308 65 6d 20 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30  em );.  if( v==0
21309 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
2130a 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c  pParse->db->mall
2130b 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20  ocFailed );.    
2130c 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20  return 0;.  }.. 
2130d 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 7b   if( pExpr==0 ){
2130e 0a 20 20 20 20 6f 70 20 3d 20 54 4b 5f 4e 55 4c  .    op = TK_NUL
2130f 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  L;.  }else{.    
21310 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a  op = pExpr->op;.
21311 20 20 7d 0a 20 20 73 77 69 74 63 68 28 20 6f 70    }.  switch( op
21312 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   ){.    case TK_
21313 41 47 47 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20  AGG_COLUMN: {.  
21314 20 20 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67      AggInfo *pAg
21315 67 49 6e 66 6f 20 3d 20 70 45 78 70 72 2d 3e 70  gInfo = pExpr->p
21316 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73  AggInfo;.      s
21317 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f  truct AggInfo_co
21318 6c 20 2a 70 43 6f 6c 20 3d 20 26 70 41 67 67 49  l *pCol = &pAggI
21319 6e 66 6f 2d 3e 61 43 6f 6c 5b 70 45 78 70 72 2d  nfo->aCol[pExpr-
2131a 3e 69 41 67 67 5d 3b 0a 20 20 20 20 20 20 69 66  >iAgg];.      if
2131b 28 20 21 70 41 67 67 49 6e 66 6f 2d 3e 64 69 72  ( !pAggInfo->dir
2131c 65 63 74 4d 6f 64 65 20 29 7b 0a 20 20 20 20 20  ectMode ){.     
2131d 20 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c 2d     assert( pCol-
2131e 3e 69 4d 65 6d 3e 30 20 29 3b 0a 20 20 20 20 20  >iMem>0 );.     
2131f 20 20 20 69 6e 52 65 67 20 3d 20 70 43 6f 6c 2d     inReg = pCol-
21320 3e 69 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 62  >iMem;.        b
21321 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73  reak;.      }els
21322 65 20 69 66 28 20 70 41 67 67 49 6e 66 6f 2d 3e  e if( pAggInfo->
21323 75 73 65 53 6f 72 74 69 6e 67 49 64 78 20 29 7b  useSortingIdx ){
21324 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
21325 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
21326 5f 43 6f 6c 75 6d 6e 2c 20 70 41 67 67 49 6e 66  _Column, pAggInf
21327 6f 2d 3e 73 6f 72 74 69 6e 67 49 64 78 2c 0a 20  o->sortingIdx,. 
21328 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21329 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f               pCo
2132a 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e  l->iSorterColumn
2132b 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
2132c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2132d 7d 0a 20 20 20 20 20 20 2f 2a 20 4f 74 68 65 72  }.      /* Other
2132e 77 69 73 65 2c 20 66 61 6c 6c 20 74 68 72 75 20  wise, fall thru 
2132f 69 6e 74 6f 20 74 68 65 20 54 4b 5f 43 4f 4c 55  into the TK_COLU
21330 4d 4e 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 7d  MN case */.    }
21331 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c  .    case TK_COL
21332 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 69 66 28  UMN: {.      if(
21333 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3c 30   pExpr->iTable<0
21334 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54   ){.        /* T
21335 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73  his only happens
21336 20 77 68 65 6e 20 63 6f 64 69 6e 67 20 63 68 65   when coding che
21337 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a  ck constraints *
21338 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  /.        assert
21339 28 20 70 50 61 72 73 65 2d 3e 63 6b 42 61 73 65  ( pParse->ckBase
2133a 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 6e  >0 );.        in
2133b 52 65 67 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f  Reg = pExpr->iCo
2133c 6c 75 6d 6e 20 2b 20 70 50 61 72 73 65 2d 3e 63  lumn + pParse->c
2133d 6b 42 61 73 65 3b 0a 20 20 20 20 20 20 7d 65 6c  kBase;.      }el
2133e 73 65 7b 0a 20 20 20 20 20 20 20 20 74 65 73 74  se{.        test
2133f 63 61 73 65 28 20 28 70 45 78 70 72 2d 3e 66 6c  case( (pExpr->fl
21340 61 67 73 20 26 20 45 50 5f 41 6e 79 41 66 66 29  ags & EP_AnyAff)
21341 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69  !=0 );.        i
21342 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78  nReg = sqlite3Ex
21343 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28  prCodeGetColumn(
21344 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
21345 54 61 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Tab,.           
21346 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21347 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f        pExpr->iCo
21348 6c 75 6d 6e 2c 20 70 45 78 70 72 2d 3e 69 54 61  lumn, pExpr->iTa
21349 62 6c 65 2c 20 74 61 72 67 65 74 2c 0a 20 20 20  ble, target,.   
2134a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2134b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
2134c 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  xpr->flags & EP_
2134d 41 6e 79 41 66 66 29 3b 0a 20 20 20 20 20 20 7d  AnyAff);.      }
2134e 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2134f 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
21350 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20  INTEGER: {.     
21351 20 63 6f 64 65 49 6e 74 65 67 65 72 28 76 2c 20   codeInteger(v, 
21352 70 45 78 70 72 2c 20 30 2c 20 74 61 72 67 65 74  pExpr, 0, target
21353 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
21354 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
21355 4b 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20  K_FLOAT: {.     
21356 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
21357 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
21358 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b   EP_IntValue) );
21359 0a 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c 28  .      codeReal(
2135a 76 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b  v, pExpr->u.zTok
2135b 65 6e 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a  en, 0, target);.
2135c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2135d 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53   }.    case TK_S
2135e 54 52 49 4e 47 3a 20 7b 0a 20 20 20 20 20 20 61  TRING: {.      a
2135f 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50  ssert( !ExprHasP
21360 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
21361 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20  P_IntValue) );. 
21362 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21363 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
21364 69 6e 67 38 2c 20 30 2c 20 74 61 72 67 65 74 2c  ing8, 0, target,
21365 20 30 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f   0, pExpr->u.zTo
21366 6b 65 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 62  ken, 0);.      b
21367 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
21368 63 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a 20 7b 0a  case TK_NULL: {.
21369 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2136a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
2136b 6c 6c 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a  ll, 0, target);.
2136c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2136d 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
2136e 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45  E_OMIT_BLOB_LITE
2136f 52 41 4c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  RAL.    case TK_
21370 42 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 69 6e  BLOB: {.      in
21371 74 20 6e 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74  t n;.      const
21372 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 20 20   char *z;.      
21373 63 68 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20 20  char *zBlob;.   
21374 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
21375 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
21376 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20  r, EP_IntValue) 
21377 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
21378 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e   pExpr->u.zToken
21379 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20 70 45 78 70  [0]=='x' || pExp
2137a 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d  r->u.zToken[0]==
2137b 27 58 27 20 29 3b 0a 20 20 20 20 20 20 61 73 73  'X' );.      ass
2137c 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54  ert( pExpr->u.zT
2137d 6f 6b 65 6e 5b 31 5d 3d 3d 27 5c 27 27 20 29 3b  oken[1]=='\'' );
2137e 0a 20 20 20 20 20 20 7a 20 3d 20 26 70 45 78 70  .      z = &pExp
2137f 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 32 5d 3b 0a  r->u.zToken[2];.
21380 20 20 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65        n = sqlite
21381 33 53 74 72 6c 65 6e 33 30 28 7a 29 20 2d 20 31  3Strlen30(z) - 1
21382 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
21383 7a 5b 6e 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20  z[n]=='\'' );.  
21384 20 20 20 20 7a 42 6c 6f 62 20 3d 20 73 71 6c 69      zBlob = sqli
21385 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c  te3HexToBlob(sql
21386 69 74 65 33 56 64 62 65 44 62 28 76 29 2c 20 7a  ite3VdbeDb(v), z
21387 2c 20 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , n);.      sqli
21388 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
21389 20 4f 50 5f 42 6c 6f 62 2c 20 6e 2f 32 2c 20 74   OP_Blob, n/2, t
2138a 61 72 67 65 74 2c 20 30 2c 20 7a 42 6c 6f 62 2c  arget, 0, zBlob,
2138b 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20   P4_DYNAMIC);.  
2138c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2138d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65  .#endif.    case
2138e 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b 0a   TK_VARIABLE: {.
2138f 20 20 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f        VdbeOp *pO
21390 70 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  p;.      assert(
21391 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
21392 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56  y(pExpr, EP_IntV
21393 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 61  alue) );.      a
21394 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e  ssert( pExpr->u.
21395 7a 54 6f 6b 65 6e 21 3d 30 20 29 3b 0a 20 20 20  zToken!=0 );.   
21396 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
21397 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 21 3d 30  ->u.zToken[0]!=0
21398 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45   );.      if( pE
21399 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d  xpr->u.zToken[1]
2139a 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26 20  ==0.         && 
2139b 28 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64  (pOp = sqlite3Vd
2139c 62 65 47 65 74 4f 70 28 76 2c 20 2d 31 29 29 2d  beGetOp(v, -1))-
2139d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 61 72 69  >opcode==OP_Vari
2139e 61 62 6c 65 0a 20 20 20 20 20 20 20 20 20 26 26  able.         &&
2139f 20 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 33   pOp->p1+pOp->p3
213a0 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 0a  ==pExpr->iTable.
213a1 20 20 20 20 20 20 20 20 20 26 26 20 70 4f 70 2d           && pOp-
213a2 3e 70 32 2b 70 4f 70 2d 3e 70 33 3d 3d 74 61 72  >p2+pOp->p3==tar
213a3 67 65 74 0a 20 20 20 20 20 20 20 20 20 26 26 20  get.         && 
213a4 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 0a 20 20 20  pOp->p4.z==0.   
213a5 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a     ){.        /*
213a6 20 49 66 20 74 68 65 20 70 72 65 76 69 6f 75 73   If the previous
213a7 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 61 73   instruction was
213a8 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 70   a copy of the p
213a9 72 65 76 69 6f 75 73 20 75 6e 6e 61 6d 65 64 0a  revious unnamed.
213aa 20 20 20 20 20 20 20 20 2a 2a 20 70 61 72 61 6d          ** param
213ab 65 74 65 72 20 69 6e 74 6f 20 74 68 65 20 70 72  eter into the pr
213ac 65 76 69 6f 75 73 20 72 65 67 69 73 74 65 72 2c  evious register,
213ad 20 74 68 65 6e 20 73 69 6d 70 6c 79 20 69 6e 63   then simply inc
213ae 72 65 6d 65 6e 74 20 74 68 65 0a 20 20 20 20 20  rement the.     
213af 20 20 20 2a 2a 20 72 65 70 65 61 74 20 63 6f 75     ** repeat cou
213b0 6e 74 20 6f 6e 20 74 68 65 20 70 72 69 6f 72 20  nt on the prior 
213b1 69 6e 73 74 72 75 63 74 69 6f 6e 20 72 61 74 68  instruction rath
213b2 65 72 20 74 68 61 6e 20 6d 61 6b 69 6e 67 20 61  er than making a
213b3 20 6e 65 77 0a 20 20 20 20 20 20 20 20 2a 2a 20   new.        ** 
213b4 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 20 20 20  instruction..   
213b5 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
213b6 70 4f 70 2d 3e 70 33 2b 2b 3b 0a 20 20 20 20 20  pOp->p3++;.     
213b7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
213b8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
213b9 33 28 76 2c 20 4f 50 5f 56 61 72 69 61 62 6c 65  3(v, OP_Variable
213ba 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c  , pExpr->iTable,
213bb 20 74 61 72 67 65 74 2c 20 31 29 3b 0a 20 20 20   target, 1);.   
213bc 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e       if( pExpr->
213bd 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 21 3d 30 20 29  u.zToken[1]!=0 )
213be 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
213bf 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
213c0 76 2c 20 2d 31 2c 20 70 45 78 70 72 2d 3e 75 2e  v, -1, pExpr->u.
213c1 7a 54 6f 6b 65 6e 2c 20 30 29 3b 0a 20 20 20 20  zToken, 0);.    
213c2 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
213c3 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
213c4 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45 47  .    case TK_REG
213c5 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 20 69  ISTER: {.      i
213c6 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e 69 54  nReg = pExpr->iT
213c7 61 62 6c 65 3b 0a 20 20 20 20 20 20 62 72 65 61  able;.      brea
213c8 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
213c9 65 20 54 4b 5f 41 53 3a 20 7b 0a 20 20 20 20 20  e TK_AS: {.     
213ca 20 69 6e 52 65 67 20 3d 20 63 6f 64 65 41 6c 69   inReg = codeAli
213cb 61 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  as(pParse, pExpr
213cc 2d 3e 69 54 61 62 6c 65 2c 20 70 45 78 70 72 2d  ->iTable, pExpr-
213cd 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b  >pLeft, target);
213ce 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
213cf 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
213d0 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20 20  TE_OMIT_CAST.   
213d1 20 63 61 73 65 20 54 4b 5f 43 41 53 54 3a 20 7b   case TK_CAST: {
213d2 0a 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73  .      /* Expres
213d3 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 6f 72  sions of the for
213d4 6d 3a 20 20 20 43 41 53 54 28 70 4c 65 66 74 20  m:   CAST(pLeft 
213d5 41 53 20 74 6f 6b 65 6e 29 20 2a 2f 0a 20 20 20  AS token) */.   
213d6 20 20 20 69 6e 74 20 61 66 66 2c 20 74 6f 5f 6f     int aff, to_o
213d7 70 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d  p;.      inReg =
213d8 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
213d9 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70  Target(pParse, p
213da 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72  Expr->pLeft, tar
213db 67 65 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65  get);.      asse
213dc 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
213dd 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49  erty(pExpr, EP_I
213de 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20  ntValue) );.    
213df 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 41    aff = sqlite3A
213e0 66 66 69 6e 69 74 79 54 79 70 65 28 70 45 78 70  ffinityType(pExp
213e1 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20  r->u.zToken);.  
213e2 20 20 20 20 74 6f 5f 6f 70 20 3d 20 61 66 66 20      to_op = aff 
213e3 2d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  - SQLITE_AFF_TEX
213e4 54 20 2b 20 4f 50 5f 54 6f 54 65 78 74 3b 0a 20  T + OP_ToText;. 
213e5 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f       assert( to_
213e6 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 20 20  op==OP_ToText   
213e7 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f   || aff!=SQLITE_
213e8 41 46 46 5f 54 45 58 54 20 20 20 20 29 3b 0a 20  AFF_TEXT    );. 
213e9 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f       assert( to_
213ea 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 20 20 20  op==OP_ToBlob   
213eb 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f   || aff!=SQLITE_
213ec 41 46 46 5f 4e 4f 4e 45 20 20 20 20 29 3b 0a 20  AFF_NONE    );. 
213ed 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f       assert( to_
213ee 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 63  op==OP_ToNumeric
213ef 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f   || aff!=SQLITE_
213f0 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20  AFF_NUMERIC );. 
213f1 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f       assert( to_
213f2 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 20 20 20  op==OP_ToInt    
213f3 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f   || aff!=SQLITE_
213f4 41 46 46 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20  AFF_INTEGER );. 
213f5 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f       assert( to_
213f6 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 20 20  op==OP_ToReal   
213f7 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f   || aff!=SQLITE_
213f8 41 46 46 5f 52 45 41 4c 20 20 20 20 29 3b 0a 20  AFF_REAL    );. 
213f9 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74       testcase( t
213fa 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20  o_op==OP_ToText 
213fb 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
213fc 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 42  e( to_op==OP_ToB
213fd 6c 6f 62 20 29 3b 0a 20 20 20 20 20 20 74 65 73  lob );.      tes
213fe 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50  tcase( to_op==OP
213ff 5f 54 6f 4e 75 6d 65 72 69 63 20 29 3b 0a 20 20  _ToNumeric );.  
21400 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74 6f      testcase( to
21401 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 29 3b  _op==OP_ToInt );
21402 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21403 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61   to_op==OP_ToRea
21404 6c 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69  l );.      if( i
21405 6e 52 65 67 21 3d 74 61 72 67 65 74 20 29 7b 0a  nReg!=target ){.
21406 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
21407 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
21408 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20 74 61  SCopy, inReg, ta
21409 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 69  rget);.        i
2140a 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a 20  nReg = target;. 
2140b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
2140c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2140d 2c 20 74 6f 5f 6f 70 2c 20 69 6e 52 65 67 29 3b  , to_op, inReg);
2140e 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2140f 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63   usedAsColumnCac
21410 68 65 28 70 50 61 72 73 65 2c 20 69 6e 52 65 67  he(pParse, inReg
21411 2c 20 69 6e 52 65 67 29 20 29 3b 0a 20 20 20 20  , inReg) );.    
21412 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
21413 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
21414 28 70 50 61 72 73 65 2c 20 69 6e 52 65 67 2c 20  (pParse, inReg, 
21415 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  1);.      break;
21416 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  .    }.#endif /*
21417 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53   SQLITE_OMIT_CAS
21418 54 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b  T */.    case TK
21419 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _LT:.    case TK
2141a 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _LE:.    case TK
2141b 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _GT:.    case TK
2141c 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _GE:.    case TK
2141d 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _NE:.    case TK
2141e 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  _EQ: {.      ass
2141f 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c  ert( TK_LT==OP_L
21420 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  t );.      asser
21421 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20  t( TK_LE==OP_Le 
21422 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
21423 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 3b   TK_GT==OP_Gt );
21424 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
21425 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20  K_GE==OP_Ge );. 
21426 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
21427 45 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 20  EQ==OP_Eq );.   
21428 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 45     assert( TK_NE
21429 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20  ==OP_Ne );.     
2142a 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
2142b 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 74 65  K_LT );.      te
2142c 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c  stcase( op==TK_L
2142d 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  E );.      testc
2142e 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 29  ase( op==TK_GT )
2142f 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21430 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20  ( op==TK_GE );. 
21431 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
21432 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20 20  p==TK_EQ );.    
21433 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
21434 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20 63  TK_NE );.      c
21435 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e  odeCompareOperan
21436 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  ds(pParse, pExpr
21437 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72  ->pLeft, &r1, &r
21438 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20  egFree1,.       
21439 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2143a 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
2143b 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26  ->pRight, &r2, &
2143c 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20  regFree2);.     
2143d 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61   codeCompare(pPa
2143e 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
2143f 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  t, pExpr->pRight
21440 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 20  , op,.          
21441 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20          r1, r2, 
21442 69 6e 52 65 67 2c 20 53 51 4c 49 54 45 5f 53 54  inReg, SQLITE_ST
21443 4f 52 45 50 32 29 3b 0a 20 20 20 20 20 20 74 65  OREP2);.      te
21444 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31  stcase( regFree1
21445 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ==0 );.      tes
21446 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d  tcase( regFree2=
21447 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61  =0 );.      brea
21448 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
21449 65 20 54 4b 5f 41 4e 44 3a 0a 20 20 20 20 63 61  e TK_AND:.    ca
2144a 73 65 20 54 4b 5f 4f 52 3a 0a 20 20 20 20 63 61  se TK_OR:.    ca
2144b 73 65 20 54 4b 5f 50 4c 55 53 3a 0a 20 20 20 20  se TK_PLUS:.    
2144c 63 61 73 65 20 54 4b 5f 53 54 41 52 3a 0a 20 20  case TK_STAR:.  
2144d 20 20 63 61 73 65 20 54 4b 5f 4d 49 4e 55 53 3a    case TK_MINUS:
2144e 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45 4d  .    case TK_REM
2144f 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49  :.    case TK_BI
21450 54 41 4e 44 3a 0a 20 20 20 20 63 61 73 65 20 54  TAND:.    case T
21451 4b 5f 42 49 54 4f 52 3a 0a 20 20 20 20 63 61 73  K_BITOR:.    cas
21452 65 20 54 4b 5f 53 4c 41 53 48 3a 0a 20 20 20 20  e TK_SLASH:.    
21453 63 61 73 65 20 54 4b 5f 4c 53 48 49 46 54 3a 0a  case TK_LSHIFT:.
21454 20 20 20 20 63 61 73 65 20 54 4b 5f 52 53 48 49      case TK_RSHI
21455 46 54 3a 20 0a 20 20 20 20 63 61 73 65 20 54 4b  FT: .    case TK
21456 5f 43 4f 4e 43 41 54 3a 20 7b 0a 20 20 20 20 20  _CONCAT: {.     
21457 20 61 73 73 65 72 74 28 20 54 4b 5f 41 4e 44 3d   assert( TK_AND=
21458 3d 4f 50 5f 41 6e 64 20 29 3b 0a 20 20 20 20 20  =OP_And );.     
21459 20 61 73 73 65 72 74 28 20 54 4b 5f 4f 52 3d 3d   assert( TK_OR==
2145a 4f 50 5f 4f 72 20 29 3b 0a 20 20 20 20 20 20 61  OP_Or );.      a
2145b 73 73 65 72 74 28 20 54 4b 5f 50 4c 55 53 3d 3d  ssert( TK_PLUS==
2145c 4f 50 5f 41 64 64 20 29 3b 0a 20 20 20 20 20 20  OP_Add );.      
2145d 61 73 73 65 72 74 28 20 54 4b 5f 4d 49 4e 55 53  assert( TK_MINUS
2145e 3d 3d 4f 50 5f 53 75 62 74 72 61 63 74 20 29 3b  ==OP_Subtract );
2145f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
21460 4b 5f 52 45 4d 3d 3d 4f 50 5f 52 65 6d 61 69 6e  K_REM==OP_Remain
21461 64 65 72 20 29 3b 0a 20 20 20 20 20 20 61 73 73  der );.      ass
21462 65 72 74 28 20 54 4b 5f 42 49 54 41 4e 44 3d 3d  ert( TK_BITAND==
21463 4f 50 5f 42 69 74 41 6e 64 20 29 3b 0a 20 20 20  OP_BitAnd );.   
21464 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 42 49     assert( TK_BI
21465 54 4f 52 3d 3d 4f 50 5f 42 69 74 4f 72 20 29 3b  TOR==OP_BitOr );
21466 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
21467 4b 5f 53 4c 41 53 48 3d 3d 4f 50 5f 44 69 76 69  K_SLASH==OP_Divi
21468 64 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  de );.      asse
21469 72 74 28 20 54 4b 5f 4c 53 48 49 46 54 3d 3d 4f  rt( TK_LSHIFT==O
2146a 50 5f 53 68 69 66 74 4c 65 66 74 20 29 3b 0a 20  P_ShiftLeft );. 
2146b 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
2146c 52 53 48 49 46 54 3d 3d 4f 50 5f 53 68 69 66 74  RSHIFT==OP_Shift
2146d 52 69 67 68 74 20 29 3b 0a 20 20 20 20 20 20 61  Right );.      a
2146e 73 73 65 72 74 28 20 54 4b 5f 43 4f 4e 43 41 54  ssert( TK_CONCAT
2146f 3d 3d 4f 50 5f 43 6f 6e 63 61 74 20 29 3b 0a 20  ==OP_Concat );. 
21470 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
21471 70 3d 3d 54 4b 5f 41 4e 44 20 29 3b 0a 20 20 20  p==TK_AND );.   
21472 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
21473 3d 54 4b 5f 4f 52 20 29 3b 0a 20 20 20 20 20 20  =TK_OR );.      
21474 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
21475 5f 50 4c 55 53 20 29 3b 0a 20 20 20 20 20 20 74  _PLUS );.      t
21476 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
21477 4d 49 4e 55 53 20 29 3b 0a 20 20 20 20 20 20 74  MINUS );.      t
21478 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
21479 52 45 4d 20 29 3b 0a 20 20 20 20 20 20 74 65 73  REM );.      tes
2147a 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 42 49  tcase( op==TK_BI
2147b 54 41 4e 44 20 29 3b 0a 20 20 20 20 20 20 74 65  TAND );.      te
2147c 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 42  stcase( op==TK_B
2147d 49 54 4f 52 20 29 3b 0a 20 20 20 20 20 20 74 65  ITOR );.      te
2147e 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 53  stcase( op==TK_S
2147f 4c 41 53 48 20 29 3b 0a 20 20 20 20 20 20 74 65  LASH );.      te
21480 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c  stcase( op==TK_L
21481 53 48 49 46 54 20 29 3b 0a 20 20 20 20 20 20 74  SHIFT );.      t
21482 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
21483 52 53 48 49 46 54 20 29 3b 0a 20 20 20 20 20 20  RSHIFT );.      
21484 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
21485 5f 43 4f 4e 43 41 54 20 29 3b 0a 20 20 20 20 20  _CONCAT );.     
21486 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70   r1 = sqlite3Exp
21487 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65  rCodeTemp(pParse
21488 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
21489 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20  &regFree1);.    
2148a 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78    r2 = sqlite3Ex
2148b 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
2148c 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  e, pExpr->pRight
2148d 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20  , &regFree2);.  
2148e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2148f 64 64 4f 70 33 28 76 2c 20 6f 70 2c 20 72 32 2c  ddOp3(v, op, r2,
21490 20 72 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20   r1, target);.  
21491 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65      testcase( re
21492 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20  gFree1==0 );.   
21493 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
21494 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20  Free2==0 );.    
21495 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
21496 20 20 20 63 61 73 65 20 54 4b 5f 55 4d 49 4e 55     case TK_UMINU
21497 53 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 20  S: {.      Expr 
21498 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e  *pLeft = pExpr->
21499 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 61 73 73  pLeft;.      ass
2149a 65 72 74 28 20 70 4c 65 66 74 20 29 3b 0a 20 20  ert( pLeft );.  
2149b 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f      if( pLeft->o
2149c 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 7b 0a 20  p==TK_FLOAT ){. 
2149d 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21         assert( !
2149e 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
2149f 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c  pExpr, EP_IntVal
214a0 75 65 29 20 29 3b 0a 20 20 20 20 20 20 20 20 63  ue) );.        c
214a1 6f 64 65 52 65 61 6c 28 76 2c 20 70 4c 65 66 74  odeReal(v, pLeft
214a2 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 31 2c 20 74  ->u.zToken, 1, t
214a3 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 65  arget);.      }e
214a4 6c 73 65 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f  lse if( pLeft->o
214a5 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b  p==TK_INTEGER ){
214a6 0a 20 20 20 20 20 20 20 20 63 6f 64 65 49 6e 74  .        codeInt
214a7 65 67 65 72 28 76 2c 20 70 4c 65 66 74 2c 20 31  eger(v, pLeft, 1
214a8 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
214a9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
214aa 72 65 67 46 72 65 65 31 20 3d 20 72 31 20 3d 20  regFree1 = r1 = 
214ab 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
214ac 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  g(pParse);.     
214ad 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
214ae 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
214af 65 72 2c 20 30 2c 20 72 31 29 3b 0a 20 20 20 20  er, 0, r1);.    
214b0 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33      r2 = sqlite3
214b1 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61  ExprCodeTemp(pPa
214b2 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
214b3 74 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20  t, &regFree2);. 
214b4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
214b5 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53  beAddOp3(v, OP_S
214b6 75 62 74 72 61 63 74 2c 20 72 32 2c 20 72 31 2c  ubtract, r2, r1,
214b7 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20   target);.      
214b8 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46    testcase( regF
214b9 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ree2==0 );.     
214ba 20 7d 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d   }.      inReg =
214bb 20 74 61 72 67 65 74 3b 0a 20 20 20 20 20 20 62   target;.      b
214bc 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
214bd 63 61 73 65 20 54 4b 5f 42 49 54 4e 4f 54 3a 0a  case TK_BITNOT:.
214be 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 3a      case TK_NOT:
214bf 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
214c0 20 54 4b 5f 42 49 54 4e 4f 54 3d 3d 4f 50 5f 42   TK_BITNOT==OP_B
214c1 69 74 4e 6f 74 20 29 3b 0a 20 20 20 20 20 20 61  itNot );.      a
214c2 73 73 65 72 74 28 20 54 4b 5f 4e 4f 54 3d 3d 4f  ssert( TK_NOT==O
214c3 50 5f 4e 6f 74 20 29 3b 0a 20 20 20 20 20 20 74  P_Not );.      t
214c4 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
214c5 42 49 54 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20  BITNOT );.      
214c6 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
214c7 5f 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20 72 31  _NOT );.      r1
214c8 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
214c9 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70  deTemp(pParse, p
214ca 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65  Expr->pLeft, &re
214cb 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 74  gFree1);.      t
214cc 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
214cd 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 6e  1==0 );.      in
214ce 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a 20 20  Reg = target;.  
214cf 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
214d0 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72 31 2c  ddOp2(v, op, r1,
214d1 20 69 6e 52 65 67 29 3b 0a 20 20 20 20 20 20 62   inReg);.      b
214d2 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
214d3 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0a  case TK_ISNULL:.
214d4 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 4e      case TK_NOTN
214d5 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  ULL: {.      int
214d6 20 61 64 64 72 3b 0a 20 20 20 20 20 20 61 73 73   addr;.      ass
214d7 65 72 74 28 20 54 4b 5f 49 53 4e 55 4c 4c 3d 3d  ert( TK_ISNULL==
214d8 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 20  OP_IsNull );.   
214d9 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f     assert( TK_NO
214da 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c  TNULL==OP_NotNul
214db 6c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  l );.      testc
214dc 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55  ase( op==TK_ISNU
214dd 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  LL );.      test
214de 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54  case( op==TK_NOT
214df 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 73 71  NULL );.      sq
214e0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
214e1 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31  v, OP_Integer, 1
214e2 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
214e3 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70   r1 = sqlite3Exp
214e4 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65  rCodeTemp(pParse
214e5 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
214e6 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20  &regFree1);.    
214e7 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46    testcase( regF
214e8 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ree1==0 );.     
214e9 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
214ea 64 62 65 41 64 64 4f 70 31 28 76 2c 20 6f 70 2c  dbeAddOp1(v, op,
214eb 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   r1);.      sqli
214ec 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
214ed 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 74 61 72 67   OP_AddImm, targ
214ee 65 74 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 73  et, -1);.      s
214ef 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
214f0 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 20  re(v, addr);.   
214f1 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
214f2 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f      case TK_AGG_
214f3 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20  FUNCTION: {.    
214f4 20 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66 6f    AggInfo *pInfo
214f5 20 3d 20 70 45 78 70 72 2d 3e 70 41 67 67 49 6e   = pExpr->pAggIn
214f6 66 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49  fo;.      if( pI
214f7 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  nfo==0 ){.      
214f8 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
214f9 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
214fa 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29  , EP_IntValue) )
214fb 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
214fc 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
214fd 2c 20 22 6d 69 73 75 73 65 20 6f 66 20 61 67 67  , "misuse of agg
214fe 72 65 67 61 74 65 3a 20 25 73 28 29 22 2c 20 70  regate: %s()", p
214ff 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b  Expr->u.zToken);
21500 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
21501 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 49        inReg = pI
21502 6e 66 6f 2d 3e 61 46 75 6e 63 5b 70 45 78 70 72  nfo->aFunc[pExpr
21503 2d 3e 69 41 67 67 5d 2e 69 4d 65 6d 3b 0a 20 20  ->iAgg].iMem;.  
21504 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
21505 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
21506 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a  e TK_CONST_FUNC:
21507 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e  .    case TK_FUN
21508 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 45  CTION: {.      E
21509 78 70 72 4c 69 73 74 20 2a 70 46 61 72 67 3b 20  xprList *pFarg; 
2150a 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66        /* List of
2150b 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65   function argume
2150c 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  nts */.      int
2150d 20 6e 46 61 72 67 3b 20 20 20 20 20 20 20 20 20   nFarg;         
2150e 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
2150f 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65   function argume
21510 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 46 75 6e  nts */.      Fun
21511 63 44 65 66 20 2a 70 44 65 66 3b 20 20 20 20 20  cDef *pDef;     
21512 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 74      /* The funct
21513 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f  ion definition o
21514 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 20 20 69  bject */.      i
21515 6e 74 20 6e 49 64 3b 20 20 20 20 20 20 20 20 20  nt nId;         
21516 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
21517 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  of the function 
21518 6e 61 6d 65 20 69 6e 20 62 79 74 65 73 20 2a 2f  name in bytes */
21519 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
2151a 72 20 2a 7a 49 64 3b 20 20 20 20 20 20 20 2f 2a  r *zId;       /*
2151b 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61   The function na
2151c 6d 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  me */.      int 
2151d 63 6f 6e 73 74 4d 61 73 6b 20 3d 20 30 3b 20 20  constMask = 0;  
2151e 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 66 75     /* Mask of fu
2151f 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73  nction arguments
21520 20 74 68 61 74 20 61 72 65 20 63 6f 6e 73 74 61   that are consta
21521 6e 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  nt */.      int 
21522 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
21523 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
21524 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 65  er */.      u8 e
21525 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 20 20 20  nc = ENC(db);   
21526 20 20 20 2f 2a 20 54 68 65 20 74 65 78 74 20 65     /* The text e
21527 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 20  ncoding used by 
21528 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f  this database */
21529 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  .      CollSeq *
2152a 70 43 6f 6c 6c 20 3d 20 30 3b 20 20 20 20 2f 2a  pColl = 0;    /*
2152b 20 41 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71   A collating seq
2152c 75 65 6e 63 65 20 2a 2f 0a 0a 20 20 20 20 20 20  uence */..      
2152d 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
2152e 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
2152f 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b  EP_xIsSelect) );
21530 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21531 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 53 54 5f 46 55   op==TK_CONST_FU
21532 4e 43 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  NC );.      test
21533 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 46 55 4e  case( op==TK_FUN
21534 43 54 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 69  CTION );.      i
21535 66 28 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f  f( ExprHasAnyPro
21536 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
21537 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20  TokenOnly) ){.  
21538 20 20 20 20 20 20 70 46 61 72 67 20 3d 20 30 3b        pFarg = 0;
21539 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2153a 20 20 20 20 20 20 70 46 61 72 67 20 3d 20 70 45        pFarg = pE
2153b 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20  xpr->x.pList;.  
2153c 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 46 61 72      }.      nFar
2153d 67 20 3d 20 70 46 61 72 67 20 3f 20 70 46 61 72  g = pFarg ? pFar
2153e 67 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 0a 20 20  g->nExpr : 0;.  
2153f 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
21540 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
21541 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29  pr, EP_IntValue)
21542 20 29 3b 0a 20 20 20 20 20 20 7a 49 64 20 3d 20   );.      zId = 
21543 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b  pExpr->u.zToken;
21544 0a 20 20 20 20 20 20 6e 49 64 20 3d 20 73 71 6c  .      nId = sql
21545 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 49 64  ite3Strlen30(zId
21546 29 3b 0a 20 20 20 20 20 20 70 44 65 66 20 3d 20  );.      pDef = 
21547 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74  sqlite3FindFunct
21548 69 6f 6e 28 64 62 2c 20 7a 49 64 2c 20 6e 49 64  ion(db, zId, nId
21549 2c 20 6e 46 61 72 67 2c 20 65 6e 63 2c 20 30 29  , nFarg, enc, 0)
2154a 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44 65 66  ;.      if( pDef
2154b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ==0 ){.        s
2154c 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2154d 50 61 72 73 65 2c 20 22 75 6e 6b 6e 6f 77 6e 20  Parse, "unknown 
2154e 66 75 6e 63 74 69 6f 6e 3a 20 25 2e 2a 73 28 29  function: %.*s()
2154f 22 2c 20 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20  ", nId, zId);.  
21550 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21551 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
21552 46 61 72 67 20 29 7b 0a 20 20 20 20 20 20 20 20  Farg ){.        
21553 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r1 = sqlite3GetT
21554 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
21555 20 6e 46 61 72 67 29 3b 0a 20 20 20 20 20 20 20   nFarg);.       
21556 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
21557 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 20 20  ePush(pParse);  
21558 20 20 20 2f 2a 20 54 69 63 6b 65 74 20 32 65 61     /* Ticket 2ea
21559 32 34 32 35 64 33 34 62 65 20 2a 2f 0a 20 20 20  2425d34be */.   
2155a 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2155b 43 6f 64 65 45 78 70 72 4c 69 73 74 28 70 50 61  CodeExprList(pPa
2155c 72 73 65 2c 20 70 46 61 72 67 2c 20 72 31 2c 20  rse, pFarg, r1, 
2155d 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  1);.        sqli
2155e 74 65 33 45 78 70 72 43 61 63 68 65 50 6f 70 28  te3ExprCachePop(
2155f 70 50 61 72 73 65 2c 20 31 29 3b 20 20 20 2f 2a  pParse, 1);   /*
21560 20 54 69 63 6b 65 74 20 32 65 61 32 34 32 35 64   Ticket 2ea2425d
21561 33 34 62 65 20 2a 2f 0a 20 20 20 20 20 20 7d 65  34be */.      }e
21562 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 31 20  lse{.        r1 
21563 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66  = 0;.      }.#if
21564 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
21565 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
21566 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20      /* Possibly 
21567 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 66 75 6e  overload the fun
21568 63 74 69 6f 6e 20 69 66 20 74 68 65 20 66 69 72  ction if the fir
21569 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 0a 20  st argument is. 
2156a 20 20 20 20 20 2a 2a 20 61 20 76 69 72 74 75 61       ** a virtua
2156b 6c 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 2e 0a  l table column..
2156c 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
2156d 2a 20 46 6f 72 20 69 6e 66 69 78 20 66 75 6e 63  * For infix func
2156e 74 69 6f 6e 73 20 28 4c 49 4b 45 2c 20 47 4c 4f  tions (LIKE, GLO
2156f 42 2c 20 52 45 47 45 58 50 2c 20 61 6e 64 20 4d  B, REGEXP, and M
21570 41 54 43 48 29 20 75 73 65 20 74 68 65 0a 20 20  ATCH) use the.  
21571 20 20 20 20 2a 2a 20 73 65 63 6f 6e 64 20 61 72      ** second ar
21572 67 75 6d 65 6e 74 2c 20 6e 6f 74 20 74 68 65 20  gument, not the 
21573 66 69 72 73 74 2c 20 61 73 20 74 68 65 20 61 72  first, as the ar
21574 67 75 6d 65 6e 74 20 74 6f 20 74 65 73 74 20 74  gument to test t
21575 6f 0a 20 20 20 20 20 20 2a 2a 20 73 65 65 20 69  o.      ** see i
21576 66 20 69 74 20 69 73 20 61 20 63 6f 6c 75 6d 6e  f it is a column
21577 20 69 6e 20 61 20 76 69 72 74 75 61 6c 20 74 61   in a virtual ta
21578 62 6c 65 2e 20 20 54 68 69 73 20 69 73 20 64 6f  ble.  This is do
21579 6e 65 20 62 65 63 61 75 73 65 0a 20 20 20 20 20  ne because.     
2157a 20 2a 2a 20 74 68 65 20 6c 65 66 74 20 6f 70 65   ** the left ope
2157b 72 61 6e 64 20 6f 66 20 69 6e 66 69 78 20 66 75  rand of infix fu
2157c 6e 63 74 69 6f 6e 73 20 28 74 68 65 20 6f 70 65  nctions (the ope
2157d 72 61 6e 64 20 77 65 20 77 61 6e 74 20 74 6f 0a  rand we want to.
2157e 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 72 6f 6c        ** control
2157f 20 6f 76 65 72 6c 6f 61 64 69 6e 67 29 20 65 6e   overloading) en
21580 64 73 20 75 70 20 61 73 20 74 68 65 20 73 65 63  ds up as the sec
21581 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  ond argument to 
21582 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 66 75 6e  the.      ** fun
21583 63 74 69 6f 6e 2e 20 20 54 68 65 20 65 78 70 72  ction.  The expr
21584 65 73 73 69 6f 6e 20 22 41 20 67 6c 6f 62 20 42  ession "A glob B
21585 22 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20  " is equivalent 
21586 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 22 67 6c  to .      ** "gl
21587 6f 62 28 42 2c 41 29 2e 20 20 57 65 20 77 61 6e  ob(B,A).  We wan
21588 74 20 74 6f 20 75 73 65 20 74 68 65 20 41 20 69  t to use the A i
21589 6e 20 22 41 20 67 6c 6f 62 20 42 22 20 74 6f 20  n "A glob B" to 
2158a 74 65 73 74 0a 20 20 20 20 20 20 2a 2a 20 66 6f  test.      ** fo
2158b 72 20 66 75 6e 63 74 69 6f 6e 20 6f 76 65 72 6c  r function overl
2158c 6f 61 64 69 6e 67 2e 20 20 42 75 74 20 77 65 20  oading.  But we 
2158d 75 73 65 20 74 68 65 20 42 20 74 65 72 6d 20 69  use the B term i
2158e 6e 20 22 67 6c 6f 62 28 42 2c 41 29 22 2e 0a 20  n "glob(B,A)".. 
2158f 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
21590 28 20 6e 46 61 72 67 3e 3d 32 20 26 26 20 28 70  ( nFarg>=2 && (p
21591 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50  Expr->flags & EP
21592 5f 49 6e 66 69 78 46 75 6e 63 29 20 29 7b 0a 20  _InfixFunc) ){. 
21593 20 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 71         pDef = sq
21594 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c 6f 61  lite3VtabOverloa
21595 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 44  dFunction(db, pD
21596 65 66 2c 20 6e 46 61 72 67 2c 20 70 46 61 72 67  ef, nFarg, pFarg
21597 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 29 3b 0a 20  ->a[1].pExpr);. 
21598 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e       }else if( n
21599 46 61 72 67 3e 30 20 29 7b 0a 20 20 20 20 20 20  Farg>0 ){.      
2159a 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33    pDef = sqlite3
2159b 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63  VtabOverloadFunc
2159c 74 69 6f 6e 28 64 62 2c 20 70 44 65 66 2c 20 6e  tion(db, pDef, n
2159d 46 61 72 67 2c 20 70 46 61 72 67 2d 3e 61 5b 30  Farg, pFarg->a[0
2159e 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20  ].pExpr);.      
2159f 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 66  }.#endif.      f
215a0 6f 72 28 69 3d 30 3b 20 69 3c 6e 46 61 72 67 3b  or(i=0; i<nFarg;
215a1 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69   i++){.        i
215a2 66 28 20 69 3c 33 32 20 26 26 20 73 71 6c 69 74  f( i<32 && sqlit
215a3 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74  e3ExprIsConstant
215a4 28 70 46 61 72 67 2d 3e 61 5b 69 5d 2e 70 45 78  (pFarg->a[i].pEx
215a5 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  pr) ){.         
215a6 20 63 6f 6e 73 74 4d 61 73 6b 20 7c 3d 20 28 31   constMask |= (1
215a7 3c 3c 69 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  <<i);.        }.
215a8 20 20 20 20 20 20 20 20 69 66 28 20 28 70 44 65          if( (pDe
215a9 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f->flags & SQLIT
215aa 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 29  E_FUNC_NEEDCOLL)
215ab 21 3d 30 20 26 26 20 21 70 43 6f 6c 6c 20 29 7b  !=0 && !pColl ){
215ac 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 6c  .          pColl
215ad 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
215ae 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 46  llSeq(pParse, pF
215af 61 72 67 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29  arg->a[i].pExpr)
215b0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
215b1 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 44    }.      if( pD
215b2 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  ef->flags & SQLI
215b3 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c  TE_FUNC_NEEDCOLL
215b4 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
215b5 21 70 43 6f 6c 6c 20 29 20 70 43 6f 6c 6c 20 3d  !pColl ) pColl =
215b6 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 20   db->pDfltColl; 
215b7 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
215b8 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
215b9 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c 20 30 2c 20  _CollSeq, 0, 0, 
215ba 30 2c 20 28 63 68 61 72 20 2a 29 70 43 6f 6c 6c  0, (char *)pColl
215bb 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20  , P4_COLLSEQ);. 
215bc 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
215bd 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
215be 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 63  , OP_Function, c
215bf 6f 6e 73 74 4d 61 73 6b 2c 20 72 31 2c 20 74 61  onstMask, r1, ta
215c0 72 67 65 74 2c 0a 20 20 20 20 20 20 20 20 20 20  rget,.          
215c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63                (c
215c2 68 61 72 2a 29 70 44 65 66 2c 20 50 34 5f 46 55  har*)pDef, P4_FU
215c3 4e 43 44 45 46 29 3b 0a 20 20 20 20 20 20 73 71  NCDEF);.      sq
215c4 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
215c5 35 28 76 2c 20 28 75 38 29 6e 46 61 72 67 29 3b  5(v, (u8)nFarg);
215c6 0a 20 20 20 20 20 20 69 66 28 20 6e 46 61 72 67  .      if( nFarg
215c7 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
215c8 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61  te3ReleaseTempRa
215c9 6e 67 65 28 70 50 61 72 73 65 2c 20 72 31 2c 20  nge(pParse, r1, 
215ca 6e 46 61 72 67 29 3b 0a 20 20 20 20 20 20 7d 0a  nFarg);.      }.
215cb 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
215cc 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68  rCacheAffinityCh
215cd 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 31 2c  ange(pParse, r1,
215ce 20 6e 46 61 72 67 29 3b 0a 20 20 20 20 20 20 62   nFarg);.      b
215cf 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e  reak;.    }.#ifn
215d0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
215d1 53 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 73  SUBQUERY.    cas
215d2 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20 20 20  e TK_EXISTS:.   
215d3 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a   case TK_SELECT:
215d4 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73   {.      testcas
215d5 65 28 20 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53  e( op==TK_EXISTS
215d6 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
215d7 73 65 28 20 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43  se( op==TK_SELEC
215d8 54 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  T );.      sqlit
215d9 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28  e3CodeSubselect(
215da 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 30  pParse, pExpr, 0
215db 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 6e 52 65  , 0);.      inRe
215dc 67 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  g = pExpr->iColu
215dd 6d 6e 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  mn;.      break;
215de 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
215df 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 69  TK_IN: {.      i
215e0 6e 74 20 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 30  nt rNotFound = 0
215e1 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 4d 61 79  ;.      int rMay
215e2 48 61 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20  HaveNull = 0;.  
215e3 20 20 20 20 69 6e 74 20 6a 32 2c 20 6a 33 2c 20      int j2, j3, 
215e4 6a 34 2c 20 6a 35 3b 0a 20 20 20 20 20 20 63 68  j4, j5;.      ch
215e5 61 72 20 61 66 66 69 6e 69 74 79 3b 0a 20 20 20  ar affinity;.   
215e6 20 20 20 69 6e 74 20 65 54 79 70 65 3b 0a 0a 20     int eType;.. 
215e7 20 20 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d       VdbeNoopCom
215e8 6d 65 6e 74 28 28 76 2c 20 22 62 65 67 69 6e 20  ment((v, "begin 
215e9 49 4e 20 65 78 70 72 20 72 25 64 22 2c 20 74 61  IN expr r%d", ta
215ea 72 67 65 74 29 29 3b 0a 20 20 20 20 20 20 65 54  rget));.      eT
215eb 79 70 65 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  ype = sqlite3Fin
215ec 64 49 6e 49 6e 64 65 78 28 70 50 61 72 73 65 2c  dInIndex(pParse,
215ed 20 70 45 78 70 72 2c 20 26 72 4d 61 79 48 61 76   pExpr, &rMayHav
215ee 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 69 66  eNull);.      if
215ef 28 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 29  ( rMayHaveNull )
215f0 7b 0a 20 20 20 20 20 20 20 20 72 4e 6f 74 46 6f  {.        rNotFo
215f1 75 6e 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  und = ++pParse->
215f2 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  nMem;.      }.. 
215f3 20 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f       /* Figure o
215f4 75 74 20 74 68 65 20 61 66 66 69 6e 69 74 79 20  ut the affinity 
215f5 74 6f 20 75 73 65 20 74 6f 20 63 72 65 61 74 65  to use to create
215f6 20 61 20 6b 65 79 20 66 72 6f 6d 20 74 68 65 20   a key from the 
215f7 72 65 73 75 6c 74 73 0a 20 20 20 20 20 20 2a 2a  results.      **
215f8 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
215f9 6f 6e 2e 20 61 66 66 69 6e 69 74 79 53 74 72 20  on. affinityStr 
215fa 73 74 6f 72 65 73 20 61 20 73 74 61 74 69 63 20  stores a static 
215fb 73 74 72 69 6e 67 20 73 75 69 74 61 62 6c 65 20  string suitable 
215fc 66 6f 72 0a 20 20 20 20 20 20 2a 2a 20 50 34 20  for.      ** P4 
215fd 6f 66 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  of OP_MakeRecord
215fe 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
215ff 20 61 66 66 69 6e 69 74 79 20 3d 20 63 6f 6d 70   affinity = comp
21600 61 72 69 73 6f 6e 41 66 66 69 6e 69 74 79 28 70  arisonAffinity(p
21601 45 78 70 72 29 3b 0a 0a 0a 20 20 20 20 20 20 2f  Expr);...      /
21602 2a 20 43 6f 64 65 20 74 68 65 20 3c 65 78 70 72  * Code the <expr
21603 3e 20 66 72 6f 6d 20 22 3c 65 78 70 72 3e 20 49  > from "<expr> I
21604 4e 20 28 2e 2e 2e 29 22 2e 20 54 68 65 20 74 65  N (...)". The te
21605 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 0a 20 20  mporary table.  
21606 20 20 20 20 2a 2a 20 70 45 78 70 72 2d 3e 69 54      ** pExpr->iT
21607 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  able contains th
21608 65 20 76 61 6c 75 65 73 20 74 68 61 74 20 6d 61  e values that ma
21609 6b 65 20 75 70 20 74 68 65 20 28 2e 2e 2e 29 20  ke up the (...) 
2160a 73 65 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  set..      */.  
2160b 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2160c 61 63 68 65 50 75 73 68 28 70 50 61 72 73 65 29  achePush(pParse)
2160d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
2160e 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
2160f 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61  pExpr->pLeft, ta
21610 72 67 65 74 29 3b 0a 20 20 20 20 20 20 6a 32 20  rget);.      j2 
21611 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
21612 4f 70 31 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c  Op1(v, OP_IsNull
21613 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
21614 20 69 66 28 20 65 54 79 70 65 3d 3d 49 4e 5f 49   if( eType==IN_I
21615 4e 44 45 58 5f 52 4f 57 49 44 20 29 7b 0a 20 20  NDEX_ROWID ){.  
21616 20 20 20 20 20 20 6a 33 20 3d 20 73 71 6c 69 74        j3 = sqlit
21617 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
21618 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 74 61  OP_MustBeInt, ta
21619 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6a  rget);.        j
2161a 34 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  4 = sqlite3VdbeA
2161b 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45  ddOp3(v, OP_NotE
2161c 78 69 73 74 73 2c 20 70 45 78 70 72 2d 3e 69 54  xists, pExpr->iT
2161d 61 62 6c 65 2c 20 30 2c 20 74 61 72 67 65 74 29  able, 0, target)
2161e 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2161f 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
21620 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 74 61  P_Integer, 1, ta
21621 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6a  rget);.        j
21622 35 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  5 = sqlite3VdbeA
21623 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp0(v, OP_Goto
21624 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
21625 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
21626 2c 20 6a 33 29 3b 0a 20 20 20 20 20 20 20 20 73  , j3);.        s
21627 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
21628 72 65 28 76 2c 20 6a 34 29 3b 0a 20 20 20 20 20  re(v, j4);.     
21629 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2162a 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
2162b 65 72 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a  er, 0, target);.
2162c 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2162d 20 20 20 20 20 72 32 20 3d 20 72 65 67 46 72 65       r2 = regFre
2162e 65 32 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  e2 = sqlite3GetT
2162f 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
21630 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 72 65 61  .        /* Crea
21631 74 65 20 61 20 72 65 63 6f 72 64 20 61 6e 64 20  te a record and 
21632 74 65 73 74 20 66 6f 72 20 73 65 74 20 6d 65 6d  test for set mem
21633 62 65 72 73 68 69 70 2e 20 49 66 20 74 68 65 20  bership. If the 
21634 73 65 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20  set contains.   
21635 20 20 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75       ** the valu
21636 65 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20  e, then jump to 
21637 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 74  the end of the t
21638 65 73 74 20 63 6f 64 65 2e 20 54 68 65 20 74 61  est code. The ta
21639 72 67 65 74 0a 20 20 20 20 20 20 20 20 2a 2a 20  rget.        ** 
2163a 72 65 67 69 73 74 65 72 20 73 74 69 6c 6c 20 63  register still c
2163b 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 72 75 65  ontains the true
2163c 20 28 31 29 20 76 61 6c 75 65 20 77 72 69 74 74   (1) value writt
2163d 65 6e 20 74 6f 20 69 74 20 65 61 72 6c 69 65 72  en to it earlier
2163e 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
2163f 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21640 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp4(v, OP_Mak
21641 65 52 65 63 6f 72 64 2c 20 74 61 72 67 65 74 2c  eRecord, target,
21642 20 31 2c 20 72 32 2c 20 26 61 66 66 69 6e 69 74   1, r2, &affinit
21643 79 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73  y, 1);.        s
21644 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
21645 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
21646 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  1, target);.    
21647 20 20 20 20 6a 35 20 3d 20 73 71 6c 69 74 65 33      j5 = sqlite3
21648 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
21649 5f 46 6f 75 6e 64 2c 20 70 45 78 70 72 2d 3e 69  _Found, pExpr->i
2164a 54 61 62 6c 65 2c 20 30 2c 20 72 32 29 3b 0a 0a  Table, 0, r2);..
2164b 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68          /* If th
2164c 65 20 73 65 74 20 6d 65 6d 62 65 72 73 68 69 70  e set membership
2164d 20 74 65 73 74 20 66 61 69 6c 73 2c 20 74 68 65   test fails, the
2164e 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  n the result of 
2164f 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  the .        ** 
21650 22 78 20 49 4e 20 28 2e 2e 2e 29 22 20 65 78 70  "x IN (...)" exp
21651 72 65 73 73 69 6f 6e 20 6d 75 73 74 20 62 65 20  ression must be 
21652 65 69 74 68 65 72 20 30 20 6f 72 20 4e 55 4c 4c  either 0 or NULL
21653 2e 20 49 66 20 74 68 65 20 73 65 74 0a 20 20 20  . If the set.   
21654 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73       ** contains
21655 20 6e 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2c   no NULL values,
21656 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74   then the result
21657 20 69 73 20 30 2e 20 49 66 20 74 68 65 20 73 65   is 0. If the se
21658 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f  t .        ** co
21659 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f  ntains one or mo
2165a 72 65 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2c 20  re NULL values, 
2165b 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20  then the result 
2165c 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  of the.        *
2165d 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  * expression is 
2165e 61 6c 73 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 20  also NULL..     
2165f 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66     */.        if
21660 28 20 72 4e 6f 74 46 6f 75 6e 64 3d 3d 30 20 29  ( rNotFound==0 )
21661 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  {.          /* T
21662 68 69 73 20 62 72 61 6e 63 68 20 72 75 6e 73 20  his branch runs 
21663 69 66 20 69 74 20 69 73 20 6b 6e 6f 77 6e 20 61  if it is known a
21664 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20 28  t compile time (
21665 6e 6f 77 29 20 74 68 61 74 20 0a 20 20 20 20 20  now) that .     
21666 20 20 20 20 20 2a 2a 20 74 68 65 20 73 65 74 20       ** the set 
21667 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 4e 55 4c 4c  contains no NULL
21668 20 76 61 6c 75 65 73 2e 20 54 68 69 73 20 68 61   values. This ha
21669 70 70 65 6e 73 20 61 73 20 74 68 65 20 72 65 73  ppens as the res
2166a 75 6c 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ult.          **
2166b 20 6f 66 20 61 20 22 4e 4f 54 20 4e 55 4c 4c 22   of a "NOT NULL"
2166c 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 6e 20 74   constraint in t
2166d 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
2166e 6d 61 2e 20 4e 6f 20 6e 65 65 64 0a 20 20 20 20  ma. No need.    
2166f 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 65 73 74        ** to test
21670 20 74 68 65 20 64 61 74 61 20 73 74 72 75 63 74   the data struct
21671 75 72 65 20 61 74 20 72 75 6e 74 69 6d 65 20 69  ure at runtime i
21672 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20 20  n this case..   
21673 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
21674 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21675 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
21676 67 65 72 2c 20 30 2c 20 74 61 72 67 65 74 29 3b  ger, 0, target);
21677 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
21678 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69            /* Thi
21679 73 20 62 6c 6f 63 6b 20 70 6f 70 75 6c 61 74 65  s block populate
2167a 73 20 74 68 65 20 72 4e 6f 74 46 6f 75 6e 64 20  s the rNotFound 
2167b 72 65 67 69 73 74 65 72 20 77 69 74 68 20 65 69  register with ei
2167c 74 68 65 72 20 4e 55 4c 4c 0a 20 20 20 20 20 20  ther NULL.      
2167d 20 20 20 20 2a 2a 20 6f 72 20 30 20 28 61 6e 20      ** or 0 (an 
2167e 69 6e 74 65 67 65 72 20 76 61 6c 75 65 29 2e 20  integer value). 
2167f 49 66 20 74 68 65 20 64 61 74 61 20 73 74 72 75  If the data stru
21680 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 6f  cture contains o
21681 6e 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  ne.          ** 
21682 6f 72 20 6d 6f 72 65 20 4e 55 4c 4c 73 2c 20 74  or more NULLs, t
21683 68 65 6e 20 73 65 74 20 72 4e 6f 74 46 6f 75 6e  hen set rNotFoun
21684 64 20 74 6f 20 4e 55 4c 4c 2e 20 4f 74 68 65 72  d to NULL. Other
21685 77 69 73 65 2c 20 73 65 74 20 69 74 0a 20 20 20  wise, set it.   
21686 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 30 2e 20         ** to 0. 
21687 49 66 20 72 65 67 69 73 74 65 72 20 72 4d 61 79  If register rMay
21688 48 61 76 65 4e 75 6c 6c 20 69 73 20 61 6c 72 65  HaveNull is alre
21689 61 64 79 20 73 65 74 20 74 6f 20 73 6f 6d 65 20  ady set to some 
2168a 76 61 6c 75 65 0a 20 20 20 20 20 20 20 20 20 20  value.          
2168b 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e 20 4e 55  ** other than NU
2168c 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 74 65 73  LL, then the tes
2168d 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  t has already be
2168e 65 6e 20 72 75 6e 20 61 6e 64 20 0a 20 20 20 20  en run and .    
2168f 20 20 20 20 20 20 2a 2a 20 72 4e 6f 74 46 6f 75        ** rNotFou
21690 6e 64 20 69 73 20 61 6c 72 65 61 64 79 20 70 6f  nd is already po
21691 70 75 6c 61 74 65 64 2e 0a 20 20 20 20 20 20 20  pulated..       
21692 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
21693 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
21694 72 20 6e 75 6c 6c 52 65 63 6f 72 64 5b 5d 20 3d  r nullRecord[] =
21695 20 7b 20 30 78 30 32 2c 20 30 78 30 30 20 7d 3b   { 0x02, 0x00 };
21696 0a 20 20 20 20 20 20 20 20 20 20 6a 33 20 3d 20  .          j3 = 
21697 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21698 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c  1(v, OP_NotNull,
21699 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a   rMayHaveNull);.
2169a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2169b 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2169c 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 4e 6f 74 46  P_Null, 0, rNotF
2169d 6f 75 6e 64 29 3b 0a 20 20 20 20 20 20 20 20 20  ound);.         
2169e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2169f 70 34 28 76 2c 20 4f 50 5f 42 6c 6f 62 2c 20 32  p4(v, OP_Blob, 2
216a0 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 2c 20  , rMayHaveNull, 
216a1 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  0, .            
216a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
216a3 20 6e 75 6c 6c 52 65 63 6f 72 64 2c 20 50 34 5f   nullRecord, P4_
216a4 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
216a5 20 20 20 6a 34 20 3d 20 73 71 6c 69 74 65 33 56     j4 = sqlite3V
216a6 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
216a7 46 6f 75 6e 64 2c 20 70 45 78 70 72 2d 3e 69 54  Found, pExpr->iT
216a8 61 62 6c 65 2c 20 30 2c 20 72 4d 61 79 48 61 76  able, 0, rMayHav
216a9 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20  eNull);.        
216aa 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
216ab 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
216ac 72 2c 20 30 2c 20 72 4e 6f 74 46 6f 75 6e 64 29  r, 0, rNotFound)
216ad 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
216ae 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
216af 76 2c 20 6a 34 29 3b 0a 20 20 20 20 20 20 20 20  v, j4);.        
216b0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
216b1 70 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a 0a 20  pHere(v, j3);.. 
216b2 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79           /* Copy
216b3 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65   the value of re
216b4 67 69 73 74 65 72 20 72 4e 6f 74 46 6f 75 6e 64  gister rNotFound
216b5 20 28 77 68 69 63 68 20 69 73 20 65 69 74 68 65   (which is eithe
216b6 72 20 4e 55 4c 4c 20 6f 72 20 30 29 0a 20 20 20  r NULL or 0).   
216b7 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74         ** into t
216b8 68 65 20 74 61 72 67 65 74 20 72 65 67 69 73 74  he target regist
216b9 65 72 2e 20 54 68 69 73 20 77 69 6c 6c 20 62 65  er. This will be
216ba 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74   the result of t
216bb 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  he.          ** 
216bc 65 78 70 72 65 73 73 69 6f 6e 2e 0a 20 20 20 20  expression..    
216bd 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
216be 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
216bf 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c  dOp2(v, OP_Copy,
216c0 20 72 4e 6f 74 46 6f 75 6e 64 2c 20 74 61 72 67   rNotFound, targ
216c1 65 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  et);.        }. 
216c2 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
216c3 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
216c4 28 76 2c 20 6a 32 29 3b 0a 20 20 20 20 20 20 73  (v, j2);.      s
216c5 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
216c6 72 65 28 76 2c 20 6a 35 29 3b 0a 20 20 20 20 20  re(v, j5);.     
216c7 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
216c8 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b  ePop(pParse, 1);
216c9 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
216ca 6e 74 28 28 76 2c 20 22 65 6e 64 20 49 4e 20 65  nt((v, "end IN e
216cb 78 70 72 20 72 25 64 22 2c 20 74 61 72 67 65 74  xpr r%d", target
216cc 29 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ));.      break;
216cd 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
216ce 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20 20 20 78    /*.    **    x
216cf 20 42 45 54 57 45 45 4e 20 79 20 41 4e 44 20 7a   BETWEEN y AND z
216d0 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
216d1 68 69 73 20 69 73 20 65 71 75 69 76 61 6c 65 6e  his is equivalen
216d2 74 20 74 6f 0a 20 20 20 20 2a 2a 0a 20 20 20 20  t to.    **.    
216d3 2a 2a 20 20 20 20 78 3e 3d 79 20 41 4e 44 20 78  **    x>=y AND x
216d4 3c 3d 7a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  <=z.    **.    *
216d5 2a 20 58 20 69 73 20 73 74 6f 72 65 64 20 69 6e  * X is stored in
216d6 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2e 0a 20   pExpr->pLeft.. 
216d7 20 20 20 2a 2a 20 59 20 69 73 20 73 74 6f 72 65     ** Y is store
216d8 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73  d in pExpr->pLis
216d9 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2e 0a 20  t->a[0].pExpr.. 
216da 20 20 20 2a 2a 20 5a 20 69 73 20 73 74 6f 72 65     ** Z is store
216db 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73  d in pExpr->pLis
216dc 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 2e 0a 20  t->a[1].pExpr.. 
216dd 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54     */.    case T
216de 4b 5f 42 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20  K_BETWEEN: {.   
216df 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d     Expr *pLeft =
216e0 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20   pExpr->pLeft;. 
216e1 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72       struct Expr
216e2 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c 49 74 65  List_item *pLIte
216e3 6d 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  m = pExpr->x.pLi
216e4 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 45 78 70  st->a;.      Exp
216e5 72 20 2a 70 52 69 67 68 74 20 3d 20 70 4c 49 74  r *pRight = pLIt
216e6 65 6d 2d 3e 70 45 78 70 72 3b 0a 0a 20 20 20 20  em->pExpr;..    
216e7 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65    codeCompareOpe
216e8 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 4c  rands(pParse, pL
216e9 65 66 74 2c 20 26 72 31 2c 20 26 72 65 67 46 72  eft, &r1, &regFr
216ea 65 65 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ee1,.           
216eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
216ec 20 20 20 20 20 20 20 70 52 69 67 68 74 2c 20 26         pRight, &
216ed 72 32 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a  r2, &regFree2);.
216ee 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
216ef 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20  regFree1==0 );. 
216f0 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
216f1 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20  egFree2==0 );.  
216f2 20 20 20 20 72 33 20 3d 20 73 71 6c 69 74 65 33      r3 = sqlite3
216f3 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
216f4 65 29 3b 0a 20 20 20 20 20 20 72 34 20 3d 20 73  e);.      r4 = s
216f5 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
216f6 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
216f7 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72  codeCompare(pPar
216f8 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68  se, pLeft, pRigh
216f9 74 2c 20 4f 50 5f 47 65 2c 0a 20 20 20 20 20 20  t, OP_Ge,.      
216fa 20 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20              r1, 
216fb 72 32 2c 20 72 33 2c 20 53 51 4c 49 54 45 5f 53  r2, r3, SQLITE_S
216fc 54 4f 52 45 50 32 29 3b 0a 20 20 20 20 20 20 70  TOREP2);.      p
216fd 4c 49 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 70  LItem++;.      p
216fe 52 69 67 68 74 20 3d 20 70 4c 49 74 65 6d 2d 3e  Right = pLItem->
216ff 70 45 78 70 72 3b 0a 20 20 20 20 20 20 73 71 6c  pExpr;.      sql
21700 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
21701 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 72  eg(pParse, regFr
21702 65 65 32 29 3b 0a 20 20 20 20 20 20 72 32 20 3d  ee2);.      r2 =
21703 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
21704 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 52 69  Temp(pParse, pRi
21705 67 68 74 2c 20 26 72 65 67 46 72 65 65 32 29 3b  ght, &regFree2);
21706 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21707 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a   regFree2==0 );.
21708 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72        codeCompar
21709 65 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c  e(pParse, pLeft,
2170a 20 70 52 69 67 68 74 2c 20 4f 50 5f 4c 65 2c 20   pRight, OP_Le, 
2170b 72 31 2c 20 72 32 2c 20 72 34 2c 20 53 51 4c 49  r1, r2, r4, SQLI
2170c 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a 20 20 20  TE_STOREP2);.   
2170d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2170e 64 4f 70 33 28 76 2c 20 4f 50 5f 41 6e 64 2c 20  dOp3(v, OP_And, 
2170f 72 33 2c 20 72 34 2c 20 74 61 72 67 65 74 29 3b  r3, r4, target);
21710 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
21711 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
21712 72 73 65 2c 20 72 33 29 3b 0a 20 20 20 20 20 20  rse, r3);.      
21713 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
21714 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 34  mpReg(pParse, r4
21715 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
21716 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
21717 4b 5f 55 50 4c 55 53 3a 20 7b 0a 20 20 20 20 20  K_UPLUS: {.     
21718 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33   inReg = sqlite3
21719 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70  ExprCodeTarget(p
2171a 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
2171b 65 66 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  eft, target);.  
2171c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2171d 0a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 54 52  ..    case TK_TR
2171e 49 47 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 2f  IGGER: {.      /
2171f 2a 20 49 66 20 74 68 65 20 6f 70 63 6f 64 65 20  * If the opcode 
21720 69 73 20 54 4b 5f 54 52 49 47 47 45 52 2c 20 74  is TK_TRIGGER, t
21721 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69  hen the expressi
21722 6f 6e 20 69 73 20 61 20 72 65 66 65 72 65 6e 63  on is a referenc
21723 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 61 20  e.      ** to a 
21724 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 6e 65  column in the ne
21725 77 2e 2a 20 6f 72 20 6f 6c 64 2e 2a 20 70 73 65  w.* or old.* pse
21726 75 64 6f 2d 74 61 62 6c 65 73 20 61 76 61 69 6c  udo-tables avail
21727 61 62 6c 65 20 74 6f 0a 20 20 20 20 20 20 2a 2a  able to.      **
21728 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
21729 73 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  s. In this case 
2172a 45 78 70 72 2e 69 54 61 62 6c 65 20 69 73 20 73  Expr.iTable is s
2172b 65 74 20 74 6f 20 31 20 66 6f 72 20 74 68 65 0a  et to 1 for the.
2172c 20 20 20 20 20 20 2a 2a 20 6e 65 77 2e 2a 20 70        ** new.* p
2172d 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 6f 72 20  seudo-table, or 
2172e 30 20 66 6f 72 20 74 68 65 20 6f 6c 64 2e 2a 20  0 for the old.* 
2172f 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 20 45 78  pseudo-table. Ex
21730 70 72 2e 69 43 6f 6c 75 6d 6e 0a 20 20 20 20 20  pr.iColumn.     
21731 20 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68   ** is set to th
21732 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  e column of the 
21733 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 74 6f 20  pseudo-table to 
21734 72 65 61 64 2c 20 6f 72 20 74 6f 20 2d 31 20 74  read, or to -1 t
21735 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 64 20  o.      ** read 
21736 74 68 65 20 72 6f 77 69 64 20 66 69 65 6c 64 2e  the rowid field.
21737 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
21738 2a 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f  ** The expressio
21739 6e 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64  n is implemented
2173a 20 75 73 69 6e 67 20 61 6e 20 4f 50 5f 50 61 72   using an OP_Par
2173b 61 6d 20 6f 70 63 6f 64 65 2e 20 54 68 65 20 70  am opcode. The p
2173c 31 0a 20 20 20 20 20 20 2a 2a 20 70 61 72 61 6d  1.      ** param
2173d 65 74 65 72 20 69 73 20 73 65 74 20 74 6f 20 30  eter is set to 0
2173e 20 66 6f 72 20 61 6e 20 6f 6c 64 2e 72 6f 77 69   for an old.rowi
2173f 64 20 72 65 66 65 72 65 6e 63 65 2c 20 6f 72 20  d reference, or 
21740 74 6f 20 28 69 2b 31 29 0a 20 20 20 20 20 20 2a  to (i+1).      *
21741 2a 20 74 6f 20 72 65 66 65 72 65 6e 63 65 20 61  * to reference a
21742 6e 6f 74 68 65 72 20 63 6f 6c 75 6d 6e 20 6f 66  nother column of
21743 20 74 68 65 20 6f 6c 64 2e 2a 20 70 73 65 75 64   the old.* pseud
21744 6f 2d 74 61 62 6c 65 2c 20 77 68 65 72 65 20 0a  o-table, where .
21745 20 20 20 20 20 20 2a 2a 20 69 20 69 73 20 74 68        ** i is th
21746 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63  e index of the c
21747 6f 6c 75 6d 6e 2e 20 46 6f 72 20 61 20 6e 65 77  olumn. For a new
21748 2e 72 6f 77 69 64 20 72 65 66 65 72 65 6e 63 65  .rowid reference
21749 2c 20 70 31 20 69 73 0a 20 20 20 20 20 20 2a 2a  , p1 is.      **
2174a 20 73 65 74 20 74 6f 20 28 6e 2b 31 29 2c 20 77   set to (n+1), w
2174b 68 65 72 65 20 6e 20 69 73 20 74 68 65 20 6e 75  here n is the nu
2174c 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
2174d 69 6e 20 65 61 63 68 20 70 73 65 75 64 6f 2d 74  in each pseudo-t
2174e 61 62 6c 65 2e 0a 20 20 20 20 20 20 2a 2a 20 46  able..      ** F
2174f 6f 72 20 61 20 72 65 66 65 72 65 6e 63 65 20 74  or a reference t
21750 6f 20 61 6e 79 20 6f 74 68 65 72 20 63 6f 6c 75  o any other colu
21751 6d 6e 20 69 6e 20 74 68 65 20 6e 65 77 2e 2a 20  mn in the new.* 
21752 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 70 31  pseudo-table, p1
21753 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 73 65 74  .      ** is set
21754 20 74 6f 20 28 6e 2b 32 2b 69 29 2c 20 77 68 65   to (n+2+i), whe
21755 72 65 20 6e 20 61 6e 64 20 69 20 61 72 65 20 61  re n and i are a
21756 73 20 64 65 66 69 6e 65 64 20 70 72 65 76 69 6f  s defined previo
21757 75 73 6c 79 2e 20 46 6f 72 0a 20 20 20 20 20 20  usly. For.      
21758 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74  ** example, if t
21759 68 65 20 74 61 62 6c 65 20 6f 6e 20 77 68 69 63  he table on whic
2175a 68 20 74 72 69 67 67 65 72 73 20 61 72 65 20 62  h triggers are b
2175b 65 69 6e 67 20 66 69 72 65 64 20 69 73 0a 20 20  eing fired is.  
2175c 20 20 20 20 2a 2a 20 64 65 63 6c 61 72 65 64 20      ** declared 
2175d 61 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  as:.      **.   
2175e 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20 54     **   CREATE T
2175f 41 42 4c 45 20 74 31 28 61 2c 20 62 29 3b 0a 20  ABLE t1(a, b);. 
21760 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
21761 20 54 68 65 6e 20 70 31 20 69 73 20 69 6e 74 65   Then p1 is inte
21762 72 70 72 65 74 65 64 20 61 73 20 66 6f 6c 6c 6f  rpreted as follo
21763 77 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  ws:.      **.   
21764 20 20 20 2a 2a 20 20 20 70 31 3d 3d 30 20 20 20     **   p1==0   
21765 2d 3e 20 20 20 20 6f 6c 64 2e 72 6f 77 69 64 20  ->    old.rowid 
21766 20 20 20 20 70 31 3d 3d 33 20 20 20 2d 3e 20 20      p1==3   ->  
21767 20 20 6e 65 77 2e 72 6f 77 69 64 0a 20 20 20 20    new.rowid.    
21768 20 20 2a 2a 20 20 20 70 31 3d 3d 31 20 20 20 2d    **   p1==1   -
21769 3e 20 20 20 20 6f 6c 64 2e 61 20 20 20 20 20 20  >    old.a      
2176a 20 20 20 70 31 3d 3d 34 20 20 20 2d 3e 20 20 20     p1==4   ->   
2176b 20 6e 65 77 2e 61 0a 20 20 20 20 20 20 2a 2a 20   new.a.      ** 
2176c 20 20 70 31 3d 3d 32 20 20 20 2d 3e 20 20 20 20    p1==2   ->    
2176d 6f 6c 64 2e 62 20 20 20 20 20 20 20 20 20 70 31  old.b         p1
2176e 3d 3d 35 20 20 20 2d 3e 20 20 20 20 6e 65 77 2e  ==5   ->    new.
2176f 62 20 20 20 20 20 20 20 0a 20 20 20 20 20 20 2a  b       .      *
21770 2f 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70  /.      Table *p
21771 54 61 62 20 3d 20 70 45 78 70 72 2d 3e 70 54 61  Tab = pExpr->pTa
21772 62 3b 0a 20 20 20 20 20 20 69 6e 74 20 70 31 20  b;.      int p1 
21773 3d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20  = pExpr->iTable 
21774 2a 20 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29  * (pTab->nCol+1)
21775 20 2b 20 31 20 2b 20 70 45 78 70 72 2d 3e 69 43   + 1 + pExpr->iC
21776 6f 6c 75 6d 6e 3b 0a 0a 20 20 20 20 20 20 61 73  olumn;..      as
21777 73 65 72 74 28 20 70 45 78 70 72 2d 3e 69 54 61  sert( pExpr->iTa
21778 62 6c 65 3d 3d 30 20 7c 7c 20 70 45 78 70 72 2d  ble==0 || pExpr-
21779 3e 69 54 61 62 6c 65 3d 3d 31 20 29 3b 0a 20 20  >iTable==1 );.  
2177a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
2177b 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 2d 31 20 26  r->iColumn>=-1 &
2177c 26 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  & pExpr->iColumn
2177d 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20  <pTab->nCol );. 
2177e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61       assert( pTa
2177f 62 2d 3e 69 50 4b 65 79 3c 30 20 7c 7c 20 70 45  b->iPKey<0 || pE
21780 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 70 54  xpr->iColumn!=pT
21781 61 62 2d 3e 69 50 4b 65 79 20 29 3b 0a 20 20 20  ab->iPKey );.   
21782 20 20 20 61 73 73 65 72 74 28 20 70 31 3e 3d 30     assert( p1>=0
21783 20 26 26 20 70 31 3c 28 70 54 61 62 2d 3e 6e 43   && p1<(pTab->nC
21784 6f 6c 2a 32 2b 32 29 20 29 3b 0a 0a 20 20 20 20  ol*2+2) );..    
21785 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21786 4f 70 32 28 76 2c 20 4f 50 5f 50 61 72 61 6d 2c  Op2(v, OP_Param,
21787 20 70 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20   p1, target);.  
21788 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
21789 28 76 2c 20 22 25 73 2e 25 73 20 2d 3e 20 24 25  (v, "%s.%s -> $%
2178a 64 22 2c 0a 20 20 20 20 20 20 20 20 28 70 45 78  d",.        (pEx
2178b 70 72 2d 3e 69 54 61 62 6c 65 20 3f 20 22 6e 65  pr->iTable ? "ne
2178c 77 22 20 3a 20 22 6f 6c 64 22 29 2c 0a 20 20 20  w" : "old"),.   
2178d 20 20 20 20 20 28 70 45 78 70 72 2d 3e 69 43 6f       (pExpr->iCo
2178e 6c 75 6d 6e 3c 30 20 3f 20 22 72 6f 77 69 64 22  lumn<0 ? "rowid"
2178f 20 3a 20 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e   : pExpr->pTab->
21790 61 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 43 6f 6c  aCol[pExpr->iCol
21791 75 6d 6e 5d 2e 7a 4e 61 6d 65 29 2c 0a 20 20 20  umn].zName),.   
21792 20 20 20 20 20 74 61 72 67 65 74 0a 20 20 20 20       target.    
21793 20 20 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20    ));..      /* 
21794 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 68 61  If the column ha
21795 73 20 52 45 41 4c 20 61 66 66 69 6e 69 74 79 2c  s REAL affinity,
21796 20 69 74 20 6d 61 79 20 63 75 72 72 65 6e 74 6c   it may currentl
21797 79 20 62 65 20 73 74 6f 72 65 64 20 61 73 20 61  y be stored as a
21798 6e 0a 20 20 20 20 20 20 2a 2a 20 69 6e 74 65 67  n.      ** integ
21799 65 72 2e 20 55 73 65 20 4f 50 5f 52 65 61 6c 41  er. Use OP_RealA
2179a 66 66 69 6e 69 74 79 20 74 6f 20 6d 61 6b 65 20  ffinity to make 
2179b 73 75 72 65 20 69 74 20 69 73 20 72 65 61 6c 6c  sure it is reall
2179c 79 20 72 65 61 6c 2e 20 20 2a 2f 0a 20 20 20 20  y real.  */.    
2179d 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f    if( pExpr->iCo
2179e 6c 75 6d 6e 3e 3d 30 20 0a 20 20 20 20 20 20 20  lumn>=0 .       
2179f 26 26 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 45  && pTab->aCol[pE
217a0 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e 61 66  xpr->iColumn].af
217a1 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41  finity==SQLITE_A
217a2 46 46 5f 52 45 41 4c 0a 20 20 20 20 20 20 29 7b  FF_REAL.      ){
217a3 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
217a4 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
217a5 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 2c 20 74  _RealAffinity, t
217a6 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 0a  arget);.      }.
217a7 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
217a8 20 7d 0a 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20   }...    /*.    
217a9 2a 2a 20 46 6f 72 6d 20 41 3a 0a 20 20 20 20 2a  ** Form A:.    *
217aa 2a 20 20 20 43 41 53 45 20 78 20 57 48 45 4e 20  *   CASE x WHEN 
217ab 65 31 20 54 48 45 4e 20 72 31 20 57 48 45 4e 20  e1 THEN r1 WHEN 
217ac 65 32 20 54 48 45 4e 20 72 32 20 2e 2e 2e 20 57  e2 THEN r2 ... W
217ad 48 45 4e 20 65 4e 20 54 48 45 4e 20 72 4e 20 45  HEN eN THEN rN E
217ae 4c 53 45 20 79 20 45 4e 44 0a 20 20 20 20 2a 2a  LSE y END.    **
217af 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20 42 3a 0a  .    ** Form B:.
217b0 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20 57 48      **   CASE WH
217b1 45 4e 20 65 31 20 54 48 45 4e 20 72 31 20 57 48  EN e1 THEN r1 WH
217b2 45 4e 20 65 32 20 54 48 45 4e 20 72 32 20 2e 2e  EN e2 THEN r2 ..
217b3 2e 20 57 48 45 4e 20 65 4e 20 54 48 45 4e 20 72  . WHEN eN THEN r
217b4 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a 20 20 20  N ELSE y END.   
217b5 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20   **.    ** Form 
217b6 41 20 69 73 20 63 61 6e 20 62 65 20 74 72 61 6e  A is can be tran
217b7 73 66 6f 72 6d 65 64 20 69 6e 74 6f 20 74 68 65  sformed into the
217b8 20 65 71 75 69 76 61 6c 65 6e 74 20 66 6f 72 6d   equivalent form
217b9 20 42 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20   B as follows:. 
217ba 20 20 20 2a 2a 20 20 20 43 41 53 45 20 57 48 45     **   CASE WHE
217bb 4e 20 78 3d 65 31 20 54 48 45 4e 20 72 31 20 57  N x=e1 THEN r1 W
217bc 48 45 4e 20 78 3d 65 32 20 54 48 45 4e 20 72 32  HEN x=e2 THEN r2
217bd 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20 20 20 20   ....    **     
217be 20 20 20 57 48 45 4e 20 78 3d 65 4e 20 54 48 45     WHEN x=eN THE
217bf 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a  N rN ELSE y END.
217c0 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 58 20      **.    ** X 
217c1 28 69 66 20 69 74 20 65 78 69 73 74 73 29 20 69  (if it exists) i
217c2 73 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 65 66  s in pExpr->pLef
217c3 74 2e 0a 20 20 20 20 2a 2a 20 59 20 69 73 20 69  t..    ** Y is i
217c4 6e 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2e  n pExpr->pRight.
217c5 20 20 54 68 65 20 59 20 69 73 20 61 6c 73 6f 20    The Y is also 
217c6 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 66 20 74 68  optional.  If th
217c7 65 72 65 20 69 73 20 6e 6f 0a 20 20 20 20 2a 2a  ere is no.    **
217c8 20 45 4c 53 45 20 63 6c 61 75 73 65 20 61 6e 64   ELSE clause and
217c9 20 6e 6f 20 6f 74 68 65 72 20 74 65 72 6d 20 6d   no other term m
217ca 61 74 63 68 65 73 2c 20 74 68 65 6e 20 74 68 65  atches, then the
217cb 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 0a 20   result of the. 
217cc 20 20 20 2a 2a 20 65 78 70 72 73 73 69 6f 6e 20     ** exprssion 
217cd 69 73 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2a 20  is NULL..    ** 
217ce 45 69 20 69 73 20 69 6e 20 70 45 78 70 72 2d 3e  Ei is in pExpr->
217cf 70 4c 69 73 74 2d 3e 61 5b 69 2a 32 5d 20 61 6e  pList->a[i*2] an
217d0 64 20 52 69 20 69 73 20 70 45 78 70 72 2d 3e 70  d Ri is pExpr->p
217d1 4c 69 73 74 2d 3e 61 5b 69 2a 32 2b 31 5d 2e 0a  List->a[i*2+1]..
217d2 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
217d3 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20  e result of the 
217d4 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 68  expression is th
217d5 65 20 52 69 20 66 6f 72 20 74 68 65 20 66 69 72  e Ri for the fir
217d6 73 74 20 6d 61 74 63 68 69 6e 67 20 45 69 2c 0a  st matching Ei,.
217d7 20 20 20 20 2a 2a 20 6f 72 20 69 66 20 74 68 65      ** or if the
217d8 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 69 6e  re is no matchin
217d9 67 20 45 69 2c 20 74 68 65 20 45 4c 53 45 20 74  g Ei, the ELSE t
217da 65 72 6d 20 59 2c 20 6f 72 20 69 66 20 74 68 65  erm Y, or if the
217db 72 65 20 69 73 0a 20 20 20 20 2a 2a 20 6e 6f 20  re is.    ** no 
217dc 45 4c 53 45 20 74 65 72 6d 2c 20 4e 55 4c 4c 2e  ELSE term, NULL.
217dd 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 65 66 61  .    */.    defa
217de 75 6c 74 3a 20 61 73 73 65 72 74 28 20 6f 70 3d  ult: assert( op=
217df 3d 54 4b 5f 43 41 53 45 20 29 3b 20 7b 0a 20 20  =TK_CASE ); {.  
217e0 20 20 20 20 69 6e 74 20 65 6e 64 4c 61 62 65 6c      int endLabel
217e1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
217e2 20 20 20 20 20 20 2f 2a 20 47 4f 54 4f 20 6c 61        /* GOTO la
217e3 62 65 6c 20 66 6f 72 20 65 6e 64 20 6f 66 20 43  bel for end of C
217e4 41 53 45 20 73 74 6d 74 20 2a 2f 0a 20 20 20 20  ASE stmt */.    
217e5 20 20 69 6e 74 20 6e 65 78 74 43 61 73 65 3b 20    int nextCase; 
217e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
217e7 20 20 20 20 2f 2a 20 47 4f 54 4f 20 6c 61 62 65      /* GOTO labe
217e8 6c 20 66 6f 72 20 6e 65 78 74 20 57 48 45 4e 20  l for next WHEN 
217e9 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20  clause */.      
217ea 69 6e 74 20 6e 45 78 70 72 3b 20 20 20 20 20 20  int nExpr;      
217eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
217ec 20 20 2f 2a 20 32 78 20 6e 75 6d 62 65 72 20 6f    /* 2x number o
217ed 66 20 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a  f WHEN terms */.
217ee 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20        int i;    
217ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
217f0 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
217f1 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20  counter */.     
217f2 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
217f3 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
217f4 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 57 48     /* List of WH
217f5 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20  EN terms */.    
217f6 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
217f7 74 5f 69 74 65 6d 20 2a 61 4c 69 73 74 65 6c 65  t_item *aListele
217f8 6d 3b 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20  m;  /* Array of 
217f9 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20  WHEN terms */.  
217fa 20 20 20 20 45 78 70 72 20 6f 70 43 6f 6d 70 61      Expr opCompa
217fb 72 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  re;             
217fc 20 20 20 20 20 20 2f 2a 20 54 68 65 20 58 3d 3d        /* The X==
217fd 45 69 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  Ei expression */
217fe 0a 20 20 20 20 20 20 45 78 70 72 20 63 61 63 68  .      Expr cach
217ff 65 58 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  eX;             
21800 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68           /* Cach
21801 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 58 20  ed expression X 
21802 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  */.      Expr *p
21803 58 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  X;              
21804 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
21805 65 20 58 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  e X expression *
21806 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 54  /.      Expr *pT
21807 65 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  est = 0;        
21808 20 20 20 20 20 20 20 20 20 20 2f 2a 20 58 3d 3d            /* X==
21809 45 69 20 28 66 6f 72 6d 20 41 29 20 6f 72 20 6a  Ei (form A) or j
2180a 75 73 74 20 45 69 20 28 66 6f 72 6d 20 42 29 20  ust Ei (form B) 
2180b 2a 2f 0a 20 20 20 20 20 20 56 56 41 5f 4f 4e 4c  */.      VVA_ONL
2180c 59 28 20 69 6e 74 20 69 43 61 63 68 65 4c 65 76  Y( int iCacheLev
2180d 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61  el = pParse->iCa
2180e 63 68 65 4c 65 76 65 6c 3b 20 29 0a 0a 20 20 20  cheLevel; )..   
2180f 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
21810 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
21811 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
21812 20 26 26 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69   && pExpr->x.pLi
21813 73 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  st );.      asse
21814 72 74 28 28 70 45 78 70 72 2d 3e 78 2e 70 4c 69  rt((pExpr->x.pLi
21815 73 74 2d 3e 6e 45 78 70 72 20 25 20 32 29 20 3d  st->nExpr % 2) =
21816 3d 20 30 29 3b 0a 20 20 20 20 20 20 61 73 73 65  = 0);.      asse
21817 72 74 28 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73  rt(pExpr->x.pLis
21818 74 2d 3e 6e 45 78 70 72 20 3e 20 30 29 3b 0a 20  t->nExpr > 0);. 
21819 20 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70 45       pEList = pE
2181a 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20  xpr->x.pList;.  
2181b 20 20 20 20 61 4c 69 73 74 65 6c 65 6d 20 3d 20      aListelem = 
2181c 70 45 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20  pEList->a;.     
2181d 20 6e 45 78 70 72 20 3d 20 70 45 4c 69 73 74 2d   nExpr = pEList-
2181e 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 65 6e  >nExpr;.      en
2181f 64 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74 65 33  dLabel = sqlite3
21820 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
21821 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 58 20  ;.      if( (pX 
21822 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 21  = pExpr->pLeft)!
21823 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61  =0 ){.        ca
21824 63 68 65 58 20 3d 20 2a 70 58 3b 0a 20 20 20 20  cheX = *pX;.    
21825 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 58      testcase( pX
21826 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20  ->op==TK_COLUMN 
21827 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63  );.        testc
21828 61 73 65 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f  ase( pX->op==TK_
21829 52 45 47 49 53 54 45 52 20 29 3b 0a 20 20 20 20  REGISTER );.    
2182a 20 20 20 20 63 61 63 68 65 58 2e 69 54 61 62 6c      cacheX.iTabl
2182b 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  e = sqlite3ExprC
2182c 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20  odeTemp(pParse, 
2182d 70 58 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a  pX, &regFree1);.
2182e 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
2182f 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b  ( regFree1==0 );
21830 0a 20 20 20 20 20 20 20 20 63 61 63 68 65 58 2e  .        cacheX.
21831 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52  op = TK_REGISTER
21832 3b 0a 20 20 20 20 20 20 20 20 6f 70 43 6f 6d 70  ;.        opComp
21833 61 72 65 2e 6f 70 20 3d 20 54 4b 5f 45 51 3b 0a  are.op = TK_EQ;.
21834 20 20 20 20 20 20 20 20 6f 70 43 6f 6d 70 61 72          opCompar
21835 65 2e 70 4c 65 66 74 20 3d 20 26 63 61 63 68 65  e.pLeft = &cache
21836 58 3b 0a 20 20 20 20 20 20 20 20 70 54 65 73 74  X;.        pTest
21837 20 3d 20 26 6f 70 43 6f 6d 70 61 72 65 3b 0a 20   = &opCompare;. 
21838 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72       }.      for
21839 28 69 3d 30 3b 20 69 3c 6e 45 78 70 72 3b 20 69  (i=0; i<nExpr; i
2183a 3d 69 2b 32 29 7b 0a 20 20 20 20 20 20 20 20 73  =i+2){.        s
2183b 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50  qlite3ExprCacheP
2183c 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20 20 20  ush(pParse);.   
2183d 20 20 20 20 20 69 66 28 20 70 58 20 29 7b 0a 20       if( pX ){. 
2183e 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
2183f 20 70 54 65 73 74 21 3d 30 20 29 3b 0a 20 20 20   pTest!=0 );.   
21840 20 20 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65         opCompare
21841 2e 70 52 69 67 68 74 20 3d 20 61 4c 69 73 74 65  .pRight = aListe
21842 6c 65 6d 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20  lem[i].pExpr;.  
21843 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
21844 20 20 20 20 20 20 20 70 54 65 73 74 20 3d 20 61         pTest = a
21845 4c 69 73 74 65 6c 65 6d 5b 69 5d 2e 70 45 78 70  Listelem[i].pExp
21846 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  r;.        }.   
21847 20 20 20 20 20 6e 65 78 74 43 61 73 65 20 3d 20       nextCase = 
21848 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
21849 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20  abel(v);.       
2184a 20 74 65 73 74 63 61 73 65 28 20 70 54 65 73 74   testcase( pTest
2184b 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20  ->op==TK_COLUMN 
2184c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2184d 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50  e3ExprIfFalse(pP
2184e 61 72 73 65 2c 20 70 54 65 73 74 2c 20 6e 65 78  arse, pTest, nex
2184f 74 43 61 73 65 2c 20 53 51 4c 49 54 45 5f 4a 55  tCase, SQLITE_JU
21850 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20  MPIFNULL);.     
21851 20 20 20 74 65 73 74 63 61 73 65 28 20 61 4c 69     testcase( aLi
21852 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 70  stelem[i+1].pExp
21853 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e  r->op==TK_COLUMN
21854 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74   );.        test
21855 63 61 73 65 28 20 61 4c 69 73 74 65 6c 65 6d 5b  case( aListelem[
21856 69 2b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d  i+1].pExpr->op==
21857 54 4b 5f 52 45 47 49 53 54 45 52 20 29 3b 0a 20  TK_REGISTER );. 
21858 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
21859 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 61  prCode(pParse, a
2185a 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45  Listelem[i+1].pE
2185b 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  xpr, target);.  
2185c 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2185d 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
2185e 74 6f 2c 20 30 2c 20 65 6e 64 4c 61 62 65 6c 29  to, 0, endLabel)
2185f 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
21860 33 45 78 70 72 43 61 63 68 65 50 6f 70 28 70 50  3ExprCachePop(pP
21861 61 72 73 65 2c 20 31 29 3b 0a 20 20 20 20 20 20  arse, 1);.      
21862 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
21863 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 6e 65 78  olveLabel(v, nex
21864 74 43 61 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a  tCase);.      }.
21865 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
21866 3e 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  >pRight ){.     
21867 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
21868 63 68 65 50 75 73 68 28 70 50 61 72 73 65 29 3b  chePush(pParse);
21869 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2186a 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
2186b 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20   pExpr->pRight, 
2186c 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20  target);.       
2186d 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
2186e 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b  ePop(pParse, 1);
2186f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
21870 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21871 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
21872 6c 6c 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a  ll, 0, target);.
21873 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73        }.      as
21874 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63  sert( db->malloc
21875 46 61 69 6c 65 64 20 7c 7c 20 70 50 61 72 73 65  Failed || pParse
21876 2d 3e 6e 45 72 72 3e 30 20 0a 20 20 20 20 20 20  ->nErr>0 .      
21877 20 20 20 20 20 7c 7c 20 70 50 61 72 73 65 2d 3e       || pParse->
21878 69 43 61 63 68 65 4c 65 76 65 6c 3d 3d 69 43 61  iCacheLevel==iCa
21879 63 68 65 4c 65 76 65 6c 20 29 3b 0a 20 20 20 20  cheLevel );.    
2187a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
2187b 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64  olveLabel(v, end
2187c 4c 61 62 65 6c 29 3b 0a 20 20 20 20 20 20 62 72  Label);.      br
2187d 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64  eak;.    }.#ifnd
2187e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
2187f 52 49 47 47 45 52 0a 20 20 20 20 63 61 73 65 20  RIGGER.    case 
21880 54 4b 5f 52 41 49 53 45 3a 20 7b 0a 20 20 20 20  TK_RAISE: {.    
21881 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
21882 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 52 6f  >affinity==OE_Ro
21883 6c 6c 62 61 63 6b 20 0a 20 20 20 20 20 20 20 20  llback .        
21884 20 20 20 7c 7c 20 70 45 78 70 72 2d 3e 61 66 66     || pExpr->aff
21885 69 6e 69 74 79 3d 3d 4f 45 5f 41 62 6f 72 74 0a  inity==OE_Abort.
21886 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 45             || pE
21887 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f  xpr->affinity==O
21888 45 5f 46 61 69 6c 0a 20 20 20 20 20 20 20 20 20  E_Fail.         
21889 20 20 7c 7c 20 70 45 78 70 72 2d 3e 61 66 66 69    || pExpr->affi
2188a 6e 69 74 79 3d 3d 4f 45 5f 49 67 6e 6f 72 65 0a  nity==OE_Ignore.
2188b 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 69        );.      i
2188c 66 28 20 21 70 50 61 72 73 65 2d 3e 70 54 72 69  f( !pParse->pTri
2188d 67 67 65 72 54 61 62 20 29 7b 0a 20 20 20 20 20  ggerTab ){.     
2188e 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2188f 73 67 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20  sg(pParse,.     
21890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21891 20 20 22 52 41 49 53 45 28 29 20 6d 61 79 20 6f    "RAISE() may o
21892 6e 6c 79 20 62 65 20 75 73 65 64 20 77 69 74 68  nly be used with
21893 69 6e 20 61 20 74 72 69 67 67 65 72 2d 70 72 6f  in a trigger-pro
21894 67 72 61 6d 22 29 3b 0a 20 20 20 20 20 20 20 20  gram");.        
21895 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
21896 7d 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70  }.      if( pExp
21897 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f  r->affinity==OE_
21898 41 62 6f 72 74 20 29 7b 0a 20 20 20 20 20 20 20  Abort ){.       
21899 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74   sqlite3MayAbort
2189a 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
2189b 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  }.      assert( 
2189c 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
2189d 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61  (pExpr, EP_IntVa
2189e 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 69 66  lue) );.      if
2189f 28 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74  ( pExpr->affinit
218a0 79 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 29 7b 0a  y==OE_Ignore ){.
218a1 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
218a2 64 62 65 41 64 64 4f 70 34 28 0a 20 20 20 20 20  dbeAddOp4(.     
218a3 20 20 20 20 20 20 20 76 2c 20 4f 50 5f 48 61 6c         v, OP_Hal
218a4 74 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 4f 45  t, SQLITE_OK, OE
218a5 5f 49 67 6e 6f 72 65 2c 20 30 2c 20 70 45 78 70  _Ignore, 0, pExp
218a6 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 30 29 3b 0a  r->u.zToken,0);.
218a7 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
218a8 20 20 20 20 20 73 71 6c 69 74 65 33 48 61 6c 74       sqlite3Halt
218a9 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 72 73  Constraint(pPars
218aa 65 2c 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69  e, pExpr->affini
218ab 74 79 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f  ty, pExpr->u.zTo
218ac 6b 65 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d  ken, 0);.      }
218ad 0a 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ..      break;. 
218ae 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
218af 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
218b0 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
218b1 72 65 67 46 72 65 65 31 29 3b 0a 20 20 73 71 6c  regFree1);.  sql
218b2 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
218b3 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 72  eg(pParse, regFr
218b4 65 65 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 69  ee2);.  return i
218b5 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  nReg;.}../*.** G
218b6 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
218b7 65 76 61 6c 75 61 74 65 20 61 6e 20 65 78 70 72  evaluate an expr
218b8 65 73 73 69 6f 6e 20 61 6e 64 20 73 74 6f 72 65  ession and store
218b9 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20   the results.** 
218ba 69 6e 74 6f 20 61 20 72 65 67 69 73 74 65 72 2e  into a register.
218bb 20 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 67    Return the reg
218bc 69 73 74 65 72 20 6e 75 6d 62 65 72 20 77 68 65  ister number whe
218bd 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a  re the results.*
218be 2a 20 61 72 65 20 73 74 6f 72 65 64 2e 0a 2a 2a  * are stored..**
218bf 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 67 69 73  .** If the regis
218c0 74 65 72 20 69 73 20 61 20 74 65 6d 70 6f 72 61  ter is a tempora
218c1 72 79 20 72 65 67 69 73 74 65 72 20 74 68 61 74  ry register that
218c2 20 63 61 6e 20 62 65 20 64 65 61 6c 6c 6f 63 61   can be dealloca
218c3 74 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 77 72 69  ted,.** then wri
218c4 74 65 20 69 74 73 20 6e 75 6d 62 65 72 20 69 6e  te its number in
218c5 74 6f 20 2a 70 52 65 67 2e 20 20 49 66 20 74 68  to *pReg.  If th
218c6 65 20 72 65 73 75 6c 74 20 72 65 67 69 73 74 65  e result registe
218c7 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 74 65  r is not.** a te
218c8 6d 70 6f 72 61 72 79 2c 20 74 68 65 6e 20 73 65  mporary, then se
218c9 74 20 2a 70 52 65 67 20 74 6f 20 7a 65 72 6f 2e  t *pReg to zero.
218ca 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
218cb 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78  TE int sqlite3Ex
218cc 70 72 43 6f 64 65 54 65 6d 70 28 50 61 72 73 65  prCodeTemp(Parse
218cd 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
218ce 70 45 78 70 72 2c 20 69 6e 74 20 2a 70 52 65 67  pExpr, int *pReg
218cf 29 7b 0a 20 20 69 6e 74 20 72 31 20 3d 20 73 71  ){.  int r1 = sq
218d0 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
218d1 70 50 61 72 73 65 29 3b 0a 20 20 69 6e 74 20 72  pParse);.  int r
218d2 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  2 = sqlite3ExprC
218d3 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65  odeTarget(pParse
218d4 2c 20 70 45 78 70 72 2c 20 72 31 29 3b 0a 20 20  , pExpr, r1);.  
218d5 69 66 28 20 72 32 3d 3d 72 31 20 29 7b 0a 20 20  if( r2==r1 ){.  
218d6 20 20 2a 70 52 65 67 20 3d 20 72 31 3b 0a 20 20    *pReg = r1;.  
218d7 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
218d8 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
218d9 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20  (pParse, r1);.  
218da 20 20 2a 70 52 65 67 20 3d 20 30 3b 0a 20 20 7d    *pReg = 0;.  }
218db 0a 20 20 72 65 74 75 72 6e 20 72 32 3b 0a 7d 0a  .  return r2;.}.
218dc 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
218dd 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65  code that will e
218de 76 61 6c 75 61 74 65 20 65 78 70 72 65 73 73 69  valuate expressi
218df 6f 6e 20 70 45 78 70 72 20 61 6e 64 20 73 74 6f  on pExpr and sto
218e0 72 65 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74  re the.** result
218e1 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 74 61  s in register ta
218e2 72 67 65 74 2e 20 20 54 68 65 20 72 65 73 75 6c  rget.  The resul
218e3 74 73 20 61 72 65 20 67 75 61 72 61 6e 74 65 65  ts are guarantee
218e4 64 20 74 6f 20 61 70 70 65 61 72 0a 2a 2a 20 69  d to appear.** i
218e5 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 67 65  n register targe
218e6 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
218e7 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
218e8 45 78 70 72 43 6f 64 65 28 50 61 72 73 65 20 2a  ExprCode(Parse *
218e9 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45  pParse, Expr *pE
218ea 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74 29  xpr, int target)
218eb 7b 0a 20 20 69 6e 74 20 69 6e 52 65 67 3b 0a 0a  {.  int inReg;..
218ec 20 20 61 73 73 65 72 74 28 20 74 61 72 67 65 74    assert( target
218ed 3e 30 20 26 26 20 74 61 72 67 65 74 3c 3d 70 50  >0 && target<=pP
218ee 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  arse->nMem );.  
218ef 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45  inReg = sqlite3E
218f0 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50  xprCodeTarget(pP
218f1 61 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61 72  arse, pExpr, tar
218f2 67 65 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20  get);.  assert( 
218f3 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 7c 7c  pParse->pVdbe ||
218f4 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c   pParse->db->mal
218f5 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69  locFailed );.  i
218f6 66 28 20 69 6e 52 65 67 21 3d 74 61 72 67 65 74  f( inReg!=target
218f7 20 26 26 20 70 50 61 72 73 65 2d 3e 70 56 64 62   && pParse->pVdb
218f8 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  e ){.    sqlite3
218f9 56 64 62 65 41 64 64 4f 70 32 28 70 50 61 72 73  VdbeAddOp2(pPars
218fa 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 53 43 6f  e->pVdbe, OP_SCo
218fb 70 79 2c 20 69 6e 52 65 67 2c 20 74 61 72 67 65  py, inReg, targe
218fc 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  t);.  }.  return
218fd 20 74 61 72 67 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a   target;.}../*.*
218fe 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
218ff 74 68 61 74 20 65 76 61 6c 75 74 65 73 20 74 68  that evalutes th
21900 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 69  e given expressi
21901 6f 6e 20 61 6e 64 20 70 75 74 73 20 74 68 65 20  on and puts the 
21902 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 72 65 67  result.** in reg
21903 69 73 74 65 72 20 74 61 72 67 65 74 2e 0a 2a 2a  ister target..**
21904 0a 2a 2a 20 41 6c 73 6f 20 6d 61 6b 65 20 61 20  .** Also make a 
21905 63 6f 70 79 20 6f 66 20 74 68 65 20 65 78 70 72  copy of the expr
21906 65 73 73 69 6f 6e 20 72 65 73 75 6c 74 73 20 69  ession results i
21907 6e 74 6f 20 61 6e 6f 74 68 65 72 20 22 63 61 63  nto another "cac
21908 68 65 22 20 72 65 67 69 73 74 65 72 0a 2a 2a 20  he" register.** 
21909 61 6e 64 20 6d 6f 64 69 66 79 20 74 68 65 20 65  and modify the e
2190a 78 70 72 65 73 73 69 6f 6e 20 73 6f 20 74 68 61  xpression so tha
2190b 74 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20  t the next time 
2190c 69 74 20 69 73 20 65 76 61 6c 75 61 74 65 64 2c  it is evaluated,
2190d 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 69  .** the result i
2190e 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  s a copy of the 
2190f 63 61 63 68 65 20 72 65 67 69 73 74 65 72 2e 0a  cache register..
21910 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
21911 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 65  ne is used for e
21912 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 20  xpressions that 
21913 61 72 65 20 75 73 65 64 20 6d 75 6c 74 69 70 6c  are used multipl
21914 65 20 0a 2a 2a 20 74 69 6d 65 73 2e 20 20 54 68  e .** times.  Th
21915 65 79 20 61 72 65 20 65 76 61 6c 75 61 74 65 64  ey are evaluated
21916 20 6f 6e 63 65 20 61 6e 64 20 74 68 65 20 72 65   once and the re
21917 73 75 6c 74 73 20 6f 66 20 74 68 65 20 65 78 70  sults of the exp
21918 72 65 73 73 69 6f 6e 0a 2a 2a 20 61 72 65 20 72  ression.** are r
21919 65 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  eused..*/.SQLITE
2191a 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2191b 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64 43  ite3ExprCodeAndC
2191c 61 63 68 65 28 50 61 72 73 65 20 2a 70 50 61 72  ache(Parse *pPar
2191d 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c  se, Expr *pExpr,
2191e 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20 20   int target){.  
2191f 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
21920 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69  ->pVdbe;.  int i
21921 6e 52 65 67 3b 0a 20 20 69 6e 52 65 67 20 3d 20  nReg;.  inReg = 
21922 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
21923 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 74  pParse, pExpr, t
21924 61 72 67 65 74 29 3b 0a 20 20 61 73 73 65 72 74  arget);.  assert
21925 28 20 74 61 72 67 65 74 3e 30 20 29 3b 0a 20 20  ( target>0 );.  
21926 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  /* This routine 
21927 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 74 65  is called for te
21928 72 6d 73 20 74 6f 20 49 4e 53 45 52 54 20 6f 72  rms to INSERT or
21929 20 55 50 44 41 54 45 2e 20 20 41 6e 64 20 74 68   UPDATE.  And th
2192a 65 20 6f 6e 6c 79 0a 20 20 2a 2a 20 6f 74 68 65  e only.  ** othe
2192b 72 20 70 6c 61 63 65 20 77 68 65 72 65 20 65 78  r place where ex
2192c 70 72 65 73 73 69 6f 6e 73 20 63 61 6e 20 62 65  pressions can be
2192d 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20   converted into 
2192e 54 4b 5f 52 45 47 49 53 54 45 52 20 69 73 0a 20  TK_REGISTER is. 
2192f 20 2a 2a 20 69 6e 20 57 48 45 52 45 20 63 6c 61   ** in WHERE cla
21930 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 2e 20  use processing. 
21931 20 53 6f 20 61 73 20 63 75 72 72 65 6e 74 6c 79   So as currently
21932 20 69 6d 70 6c 65 6d 65 6e 74 65 64 2c 20 74 68   implemented, th
21933 65 72 65 20 69 73 0a 20 20 2a 2a 20 6e 6f 20 77  ere is.  ** no w
21934 61 79 20 66 6f 72 20 61 20 54 4b 5f 52 45 47 49  ay for a TK_REGI
21935 53 54 45 52 20 74 6f 20 65 78 69 73 74 20 68 65  STER to exist he
21936 72 65 2e 20 20 42 75 74 20 69 74 20 73 65 65 6d  re.  But it seem
21937 73 20 70 72 75 64 65 6e 74 20 74 6f 0a 20 20 2a  s prudent to.  *
21938 2a 20 6b 65 65 70 20 74 68 65 20 41 4c 57 41 59  * keep the ALWAY
21939 53 28 29 20 69 6e 20 63 61 73 65 20 74 68 65 20  S() in case the 
2193a 63 6f 6e 64 69 74 69 6f 6e 73 20 61 62 6f 76 65  conditions above
2193b 20 63 68 61 6e 67 65 20 77 69 74 68 20 66 75 74   change with fut
2193c 75 72 65 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 63  ure.  ** modific
2193d 61 74 69 6f 6e 73 20 6f 72 20 65 6e 68 61 6e 63  ations or enhanc
2193e 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20 20 69 66 28  ements. */.  if(
2193f 20 41 4c 57 41 59 53 28 70 45 78 70 72 2d 3e 6f   ALWAYS(pExpr->o
21940 70 21 3d 54 4b 5f 52 45 47 49 53 54 45 52 29 20  p!=TK_REGISTER) 
21941 29 7b 20 20 0a 20 20 20 20 69 6e 74 20 69 4d 65  ){  .    int iMe
21942 6d 3b 0a 20 20 20 20 69 4d 65 6d 20 3d 20 2b 2b  m;.    iMem = ++
21943 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
21944 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21945 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20  Op2(v, OP_Copy, 
21946 69 6e 52 65 67 2c 20 69 4d 65 6d 29 3b 0a 20 20  inReg, iMem);.  
21947 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20    pExpr->iTable 
21948 3d 20 69 4d 65 6d 3b 0a 20 20 20 20 70 45 78 70  = iMem;.    pExp
21949 72 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53  r->op = TK_REGIS
2194a 54 45 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  TER;.  }.  retur
2194b 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a  n inReg;.}../*.*
2194c 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66  * Return TRUE if
2194d 20 70 45 78 70 72 20 69 73 20 61 6e 20 63 6f 6e   pExpr is an con
2194e 73 74 61 6e 74 20 65 78 70 72 65 73 73 69 6f 6e  stant expression
2194f 20 74 68 61 74 20 69 73 20 61 70 70 72 6f 70 72   that is appropr
21950 69 61 74 65 0a 2a 2a 20 66 6f 72 20 66 61 63 74  iate.** for fact
21951 6f 72 69 6e 67 20 6f 75 74 20 6f 66 20 61 20 6c  oring out of a l
21952 6f 6f 70 2e 20 20 41 70 70 72 6f 70 72 69 61 74  oop.  Appropriat
21953 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72  e expressions ar
21954 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 41  e:.**.**    *  A
21955 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  ny expression th
21956 61 74 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20  at evaluates to 
21957 74 77 6f 20 6f 72 20 6d 6f 72 65 20 6f 70 63 6f  two or more opco
21958 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20  des..**.**    * 
21959 20 41 6e 79 20 4f 50 5f 49 6e 74 65 67 65 72 2c   Any OP_Integer,
2195a 20 4f 50 5f 52 65 61 6c 2c 20 4f 50 5f 53 74 72   OP_Real, OP_Str
2195b 69 6e 67 2c 20 4f 50 5f 42 6c 6f 62 2c 20 4f 50  ing, OP_Blob, OP
2195c 5f 4e 75 6c 6c 2c 20 0a 2a 2a 20 20 20 20 20 20  _Null, .**      
2195d 20 6f 72 20 4f 50 5f 56 61 72 69 61 62 6c 65 20   or OP_Variable 
2195e 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65  that does not ne
2195f 65 64 20 74 6f 20 62 65 20 70 6c 61 63 65 64 20  ed to be placed 
21960 69 6e 20 61 20 0a 2a 2a 20 20 20 20 20 20 20 73  in a .**       s
21961 70 65 63 69 66 69 63 20 72 65 67 69 73 74 65 72  pecific register
21962 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73  ..**.** There is
21963 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 66 61 63   no point in fac
21964 74 6f 72 69 6e 67 20 6f 75 74 20 73 69 6e 67 6c  toring out singl
21965 65 2d 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f  e-instruction co
21966 6e 73 74 61 6e 74 0a 2a 2a 20 65 78 70 72 65 73  nstant.** expres
21967 73 69 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20  sions that need 
21968 74 6f 20 62 65 20 70 6c 61 63 65 64 20 69 6e 20  to be placed in 
21969 61 20 70 61 72 74 69 63 75 6c 61 72 20 72 65 67  a particular reg
2196a 69 73 74 65 72 2e 20 20 0a 2a 2a 20 57 65 20 63  ister.  .** We c
2196b 6f 75 6c 64 20 66 61 63 74 6f 72 20 74 68 65 6d  ould factor them
2196c 20 6f 75 74 2c 20 62 75 74 20 74 68 65 6e 20 77   out, but then w
2196d 65 20 77 6f 75 6c 64 20 65 6e 64 20 75 70 20 61  e would end up a
2196e 64 64 69 6e 67 20 61 6e 0a 2a 2a 20 4f 50 5f 53  dding an.** OP_S
2196f 43 6f 70 79 20 69 6e 73 74 72 75 63 74 69 6f 6e  Copy instruction
21970 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 76 61 6c   to move the val
21971 75 65 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 72  ue into the corr
21972 65 63 74 20 72 65 67 69 73 74 65 72 0a 2a 2a 20  ect register.** 
21973 6c 61 74 65 72 2e 20 20 57 65 20 6d 69 67 68 74  later.  We might
21974 20 61 73 20 77 65 6c 6c 20 6a 75 73 74 20 75 73   as well just us
21975 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 69  e the original i
21976 6e 73 74 72 75 63 74 69 6f 6e 20 61 6e 64 0a 2a  nstruction and.*
21977 2a 20 61 76 6f 69 64 20 74 68 65 20 4f 50 5f 53  * avoid the OP_S
21978 43 6f 70 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  Copy..*/.static 
21979 69 6e 74 20 69 73 41 70 70 72 6f 70 72 69 61 74  int isAppropriat
2197a 65 46 6f 72 46 61 63 74 6f 72 69 6e 67 28 45 78  eForFactoring(Ex
2197b 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 73  pr *p){.  if( !s
2197c 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73  qlite3ExprIsCons
2197d 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 70 29 20 29  tantNotJoin(p) )
2197e 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
2197f 20 2f 2a 20 4f 6e 6c 79 20 63 6f 6e 73 74 61 6e   /* Only constan
21980 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72  t expressions ar
21981 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f  e appropriate fo
21982 72 20 66 61 63 74 6f 72 69 6e 67 20 2a 2f 0a 20  r factoring */. 
21983 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61   }.  if( (p->fla
21984 67 73 20 26 20 45 50 5f 46 69 78 65 64 44 65 73  gs & EP_FixedDes
21985 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  t)==0 ){.    ret
21986 75 72 6e 20 31 3b 20 20 2f 2a 20 41 6e 79 20 63  urn 1;  /* Any c
21987 6f 6e 73 74 61 6e 74 20 77 69 74 68 6f 75 74 20  onstant without 
21988 61 20 66 69 78 65 64 20 64 65 73 74 69 6e 61 74  a fixed destinat
21989 69 6f 6e 20 69 73 20 61 70 70 72 6f 70 72 69 61  ion is appropria
2198a 74 65 20 2a 2f 0a 20 20 7d 0a 20 20 77 68 69 6c  te */.  }.  whil
2198b 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c  e( p->op==TK_UPL
2198c 55 53 20 29 20 70 20 3d 20 70 2d 3e 70 4c 65 66  US ) p = p->pLef
2198d 74 3b 0a 20 20 73 77 69 74 63 68 28 20 70 2d 3e  t;.  switch( p->
2198e 6f 70 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51  op ){.#ifndef SQ
2198f 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c  LITE_OMIT_BLOB_L
21990 49 54 45 52 41 4c 0a 20 20 20 20 63 61 73 65 20  ITERAL.    case 
21991 54 4b 5f 42 4c 4f 42 3a 0a 23 65 6e 64 69 66 0a  TK_BLOB:.#endif.
21992 20 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49      case TK_VARI
21993 41 42 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54  ABLE:.    case T
21994 4b 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 63  K_INTEGER:.    c
21995 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 0a 20 20  ase TK_FLOAT:.  
21996 20 20 63 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a 0a    case TK_NULL:.
21997 20 20 20 20 63 61 73 65 20 54 4b 5f 53 54 52 49      case TK_STRI
21998 4e 47 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74  NG: {.      test
21999 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f  case( p->op==TK_
2199a 42 4c 4f 42 20 29 3b 0a 20 20 20 20 20 20 74 65  BLOB );.      te
2199b 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54  stcase( p->op==T
2199c 4b 5f 56 41 52 49 41 42 4c 45 20 29 3b 0a 20 20  K_VARIABLE );.  
2199d 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d      testcase( p-
2199e 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20  >op==TK_INTEGER 
2199f 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
219a0 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f  e( p->op==TK_FLO
219a1 41 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  AT );.      test
219a2 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f  case( p->op==TK_
219a3 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65  NULL );.      te
219a4 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54  stcase( p->op==T
219a5 4b 5f 53 54 52 49 4e 47 20 29 3b 0a 20 20 20 20  K_STRING );.    
219a6 20 20 2f 2a 20 53 69 6e 67 6c 65 2d 69 6e 73 74    /* Single-inst
219a7 72 75 63 74 69 6f 6e 20 63 6f 6e 73 74 61 6e 74  ruction constant
219a8 73 20 77 69 74 68 20 61 20 66 69 78 65 64 20 64  s with a fixed d
219a9 65 73 74 69 6e 61 74 69 6f 6e 20 61 72 65 0a 20  estination are. 
219aa 20 20 20 20 20 2a 2a 20 62 65 74 74 65 72 20 64       ** better d
219ab 6f 6e 65 20 69 6e 2d 6c 69 6e 65 2e 20 20 49 66  one in-line.  If
219ac 20 77 65 20 66 61 63 74 6f 72 20 74 68 65 6d 2c   we factor them,
219ad 20 74 68 65 79 20 77 69 6c 6c 20 6a 75 73 74 20   they will just 
219ae 65 6e 64 0a 20 20 20 20 20 20 2a 2a 20 75 70 20  end.      ** up 
219af 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 20 4f 50  generating an OP
219b0 5f 53 43 6f 70 79 20 74 6f 20 6d 6f 76 65 20 74  _SCopy to move t
219b1 68 65 20 76 61 6c 75 65 20 74 6f 20 74 68 65 20  he value to the 
219b2 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 20 20  destination.    
219b3 20 20 2a 2a 20 72 65 67 69 73 74 65 72 2e 20 2a    ** register. *
219b4 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  /.      return 0
219b5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
219b6 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20   TK_UMINUS: {.  
219b7 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 65 66 74      if( p->pLeft
219b8 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c  ->op==TK_FLOAT |
219b9 7c 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d  | p->pLeft->op==
219ba 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20  TK_INTEGER ){.  
219bb 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
219bc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
219bd 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  eak;.    }.    d
219be 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
219bf 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
219c0 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
219c1 2f 2a 0a 2a 2a 20 49 66 20 70 45 78 70 72 20 69  /*.** If pExpr i
219c2 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 65 78 70  s a constant exp
219c3 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20  ression that is 
219c4 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 0a  appropriate for.
219c5 2a 2a 20 66 61 63 74 6f 72 69 6e 67 20 6f 75 74  ** factoring out
219c6 20 6f 66 20 61 20 6c 6f 6f 70 2c 20 74 68 65 6e   of a loop, then
219c7 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 65 78   evaluate the ex
219c8 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 6e 74 6f  pression.** into
219c9 20 61 20 72 65 67 69 73 74 65 72 20 61 6e 64 20   a register and 
219ca 63 6f 6e 76 65 72 74 20 74 68 65 20 65 78 70 72  convert the expr
219cb 65 73 73 69 6f 6e 20 69 6e 74 6f 20 61 20 54 4b  ession into a TK
219cc 5f 52 45 47 49 53 54 45 52 0a 2a 2a 20 65 78 70  _REGISTER.** exp
219cd 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  ression..*/.stat
219ce 69 63 20 69 6e 74 20 65 76 61 6c 43 6f 6e 73 74  ic int evalConst
219cf 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61  Expr(Walker *pWa
219d0 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70  lker, Expr *pExp
219d1 72 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61  r){.  Parse *pPa
219d2 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70  rse = pWalker->p
219d3 50 61 72 73 65 3b 0a 20 20 73 77 69 74 63 68 28  Parse;.  switch(
219d4 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20   pExpr->op ){.  
219d5 20 20 63 61 73 65 20 54 4b 5f 52 45 47 49 53 54    case TK_REGIST
219d6 45 52 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75  ER: {.      retu
219d7 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20  rn WRC_Prune;.  
219d8 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
219d9 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 63 61  FUNCTION:.    ca
219da 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  se TK_AGG_FUNCTI
219db 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  ON:.    case TK_
219dc 43 4f 4e 53 54 5f 46 55 4e 43 3a 20 7b 0a 20 20  CONST_FUNC: {.  
219dd 20 20 20 20 2f 2a 20 54 68 65 20 61 72 67 75 6d      /* The argum
219de 65 6e 74 73 20 74 6f 20 61 20 66 75 6e 63 74 69  ents to a functi
219df 6f 6e 20 68 61 76 65 20 61 20 66 69 78 65 64 20  on have a fixed 
219e0 64 65 73 74 69 6e 61 74 69 6f 6e 2e 0a 20 20 20  destination..   
219e1 20 20 20 2a 2a 20 4d 61 72 6b 20 74 68 65 6d 20     ** Mark them 
219e2 74 68 69 73 20 77 61 79 20 74 6f 20 61 76 6f 69  this way to avoi
219e3 64 20 67 65 6e 65 72 61 74 65 64 20 75 6e 6e 65  d generated unne
219e4 65 64 65 64 20 4f 50 5f 53 43 6f 70 79 0a 20 20  eded OP_SCopy.  
219e5 20 20 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69      ** instructi
219e6 6f 6e 73 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20  ons. .      */. 
219e7 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70       ExprList *p
219e8 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e  List = pExpr->x.
219e9 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 61 73 73  pList;.      ass
219ea 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f  ert( !ExprHasPro
219eb 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
219ec 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20  xIsSelect) );.  
219ed 20 20 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b      if( pList ){
219ee 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 20 3d  .        int i =
219ef 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20   pList->nExpr;. 
219f0 20 20 20 20 20 20 20 73 74 72 75 63 74 20 45 78         struct Ex
219f1 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  prList_item *pIt
219f2 65 6d 20 3d 20 70 4c 69 73 74 2d 3e 61 3b 0a 20  em = pList->a;. 
219f3 20 20 20 20 20 20 20 66 6f 72 28 3b 20 69 3e 30         for(; i>0
219f4 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i--, pItem++){
219f5 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 41  .          if( A
219f6 4c 57 41 59 53 28 70 49 74 65 6d 2d 3e 70 45 78  LWAYS(pItem->pEx
219f7 70 72 29 20 29 20 70 49 74 65 6d 2d 3e 70 45 78  pr) ) pItem->pEx
219f8 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f  pr->flags |= EP_
219f9 46 69 78 65 64 44 65 73 74 3b 0a 20 20 20 20 20  FixedDest;.     
219fa 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
219fb 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
219fc 20 20 7d 0a 20 20 69 66 28 20 69 73 41 70 70 72    }.  if( isAppr
219fd 6f 70 72 69 61 74 65 46 6f 72 46 61 63 74 6f 72  opriateForFactor
219fe 69 6e 67 28 70 45 78 70 72 29 20 29 7b 0a 20 20  ing(pExpr) ){.  
219ff 20 20 69 6e 74 20 72 31 20 3d 20 2b 2b 70 50 61    int r1 = ++pPa
21a00 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 69  rse->nMem;.    i
21a01 6e 74 20 72 32 3b 0a 20 20 20 20 72 32 20 3d 20  nt r2;.    r2 = 
21a02 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
21a03 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45  arget(pParse, pE
21a04 78 70 72 2c 20 72 31 29 3b 0a 20 20 20 20 69 66  xpr, r1);.    if
21a05 28 20 4e 45 56 45 52 28 72 31 21 3d 72 32 29 20  ( NEVER(r1!=r2) 
21a06 29 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65  ) sqlite3Release
21a07 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
21a08 72 31 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e  r1);.    pExpr->
21a09 6f 70 32 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b  op2 = pExpr->op;
21a0a 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d  .    pExpr->op =
21a0b 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20   TK_REGISTER;.  
21a0c 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20    pExpr->iTable 
21a0d 3d 20 72 32 3b 0a 20 20 20 20 72 65 74 75 72 6e  = r2;.    return
21a0e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a   WRC_Prune;.  }.
21a0f 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e    return WRC_Con
21a10 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  tinue;.}../*.** 
21a11 50 72 65 65 76 61 6c 75 61 74 65 20 63 6f 6e 73  Preevaluate cons
21a12 74 61 6e 74 20 73 75 62 65 78 70 72 65 73 73 69  tant subexpressi
21a13 6f 6e 73 20 77 69 74 68 69 6e 20 70 45 78 70 72  ons within pExpr
21a14 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 0a 2a   and store the.*
21a15 2a 20 72 65 73 75 6c 74 73 20 69 6e 20 72 65 67  * results in reg
21a16 69 73 74 65 72 73 2e 20 20 4d 6f 64 69 66 79 20  isters.  Modify 
21a17 70 45 78 70 72 20 73 6f 20 74 68 61 74 20 74 68  pExpr so that th
21a18 65 20 63 6f 6e 73 74 61 6e 74 20 73 75 62 65 78  e constant subex
21a19 70 72 65 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20  presions.** are 
21a1a 54 4b 5f 52 45 47 49 53 54 45 52 20 6f 70 63 6f  TK_REGISTER opco
21a1b 64 65 73 20 74 68 61 74 20 72 65 66 65 72 20 74  des that refer t
21a1c 6f 20 74 68 65 20 70 72 65 63 6f 6d 70 75 74 65  o the precompute
21a1d 64 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 53 51 4c  d values..*/.SQL
21a1e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
21a1f 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
21a20 43 6f 6e 73 74 61 6e 74 73 28 50 61 72 73 65 20  Constants(Parse 
21a21 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
21a22 45 78 70 72 29 7b 0a 20 20 57 61 6c 6b 65 72 20  Expr){.  Walker 
21a23 77 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c  w;.  w.xExprCall
21a24 62 61 63 6b 20 3d 20 65 76 61 6c 43 6f 6e 73 74  back = evalConst
21a25 45 78 70 72 3b 0a 20 20 77 2e 78 53 65 6c 65 63  Expr;.  w.xSelec
21a26 74 43 61 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20  tCallback = 0;. 
21a27 20 77 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72   w.pParse = pPar
21a28 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c  se;.  sqlite3Wal
21a29 6b 45 78 70 72 28 26 77 2c 20 70 45 78 70 72 29  kExpr(&w, pExpr)
21a2a 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  ;.}.../*.** Gene
21a2b 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 70  rate code that p
21a2c 75 73 68 65 73 20 74 68 65 20 76 61 6c 75 65 20  ushes the value 
21a2d 6f 66 20 65 76 65 72 79 20 65 6c 65 6d 65 6e 74  of every element
21a2e 20 6f 66 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a   of the given.**
21a2f 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
21a30 20 69 6e 74 6f 20 61 20 73 65 71 75 65 6e 63 65   into a sequence
21a31 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 62 65   of registers be
21a32 67 69 6e 6e 69 6e 67 20 61 74 20 74 61 72 67 65  ginning at targe
21a33 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  t..**.** Return 
21a34 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c  the number of el
21a35 65 6d 65 6e 74 73 20 65 76 61 6c 75 61 74 65 64  ements evaluated
21a36 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
21a37 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
21a38 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28  xprCodeExprList(
21a39 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
21a3a 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67  ,     /* Parsing
21a3b 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78   context */.  Ex
21a3c 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20  prList *pList,  
21a3d 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
21a3e 6f 6e 20 6c 69 73 74 20 74 6f 20 62 65 20 63 6f  on list to be co
21a3f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 74 61 72  ded */.  int tar
21a40 67 65 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 57  get,        /* W
21a41 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 72 65  here to write re
21a42 73 75 6c 74 73 20 2a 2f 0a 20 20 69 6e 74 20 64  sults */.  int d
21a43 6f 48 61 72 64 43 6f 70 79 20 20 20 20 20 2f 2a  oHardCopy     /*
21a44 20 4d 61 6b 65 20 61 20 68 61 72 64 20 63 6f 70   Make a hard cop
21a45 79 20 6f 66 20 65 76 65 72 79 20 65 6c 65 6d 65  y of every eleme
21a46 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63  nt */.){.  struc
21a47 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
21a48 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 2c  *pItem;.  int i,
21a49 20 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c   n;.  assert( pL
21a4a 69 73 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  ist!=0 );.  asse
21a4b 72 74 28 20 74 61 72 67 65 74 3e 30 20 29 3b 0a  rt( target>0 );.
21a4c 20 20 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78    n = pList->nEx
21a4d 70 72 3b 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d  pr;.  for(pItem=
21a4e 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69  pList->a, i=0; i
21a4f 3c 6e 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b  <n; i++, pItem++
21a50 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d  ){.    if( pItem
21a51 2d 3e 69 41 6c 69 61 73 20 29 7b 0a 20 20 20 20  ->iAlias ){.    
21a52 20 20 69 6e 74 20 69 52 65 67 20 3d 20 63 6f 64    int iReg = cod
21a53 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c 20 70  eAlias(pParse, p
21a54 49 74 65 6d 2d 3e 69 41 6c 69 61 73 2c 20 70 49  Item->iAlias, pI
21a55 74 65 6d 2d 3e 70 45 78 70 72 2c 20 74 61 72 67  tem->pExpr, targ
21a56 65 74 2b 69 29 3b 0a 20 20 20 20 20 20 56 64 62  et+i);.      Vdb
21a57 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65  e *v = sqlite3Ge
21a58 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
21a59 20 20 20 20 20 69 66 28 20 69 52 65 67 21 3d 74       if( iReg!=t
21a5a 61 72 67 65 74 2b 69 20 29 7b 0a 20 20 20 20 20  arget+i ){.     
21a5b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21a5c 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79  dOp2(v, OP_SCopy
21a5d 2c 20 69 52 65 67 2c 20 74 61 72 67 65 74 2b 69  , iReg, target+i
21a5e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
21a5f 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
21a60 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
21a61 73 65 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72  se, pItem->pExpr
21a62 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a 20 20 20  , target+i);.   
21a63 20 7d 0a 20 20 20 20 69 66 28 20 64 6f 48 61 72   }.    if( doHar
21a64 64 43 6f 70 79 20 26 26 20 21 70 50 61 72 73 65  dCopy && !pParse
21a65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
21a66 65 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ed ){.      sqli
21a67 74 65 33 45 78 70 72 48 61 72 64 43 6f 70 79 28  te3ExprHardCopy(
21a68 70 50 61 72 73 65 2c 20 74 61 72 67 65 74 2c 20  pParse, target, 
21a69 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  n);.    }.  }.  
21a6a 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a  return n;.}../*.
21a6b 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
21a6c 20 66 6f 72 20 61 20 62 6f 6f 6c 65 61 6e 20 65   for a boolean e
21a6d 78 70 72 65 73 73 69 6f 6e 20 73 75 63 68 20 74  xpression such t
21a6e 68 61 74 20 61 20 6a 75 6d 70 20 69 73 20 6d 61  hat a jump is ma
21a6f 64 65 0a 2a 2a 20 74 6f 20 74 68 65 20 6c 61 62  de.** to the lab
21a70 65 6c 20 22 64 65 73 74 22 20 69 66 20 74 68 65  el "dest" if the
21a71 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74   expression is t
21a72 72 75 65 20 62 75 74 20 65 78 65 63 75 74 69 6f  rue but executio
21a73 6e 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 73 20 73  n.** continues s
21a74 74 72 61 69 67 68 74 20 74 68 72 75 20 69 66 20  traight thru if 
21a75 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
21a76 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 49  s false..**.** I
21a77 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
21a78 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20 4e 55   evaluates to NU
21a79 4c 4c 20 28 6e 65 69 74 68 65 72 20 74 72 75 65  LL (neither true
21a7a 20 6e 6f 72 20 66 61 6c 73 65 29 2c 20 74 68 65   nor false), the
21a7b 6e 0a 2a 2a 20 74 61 6b 65 20 74 68 65 20 6a 75  n.** take the ju
21a7c 6d 70 20 69 66 20 74 68 65 20 6a 75 6d 70 49 66  mp if the jumpIf
21a7d 4e 75 6c 6c 20 66 6c 61 67 20 69 73 20 53 51 4c  Null flag is SQL
21a7e 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 2e 0a  ITE_JUMPIFNULL..
21a7f 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65 20  **.** This code 
21a80 64 65 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 66  depends on the f
21a81 61 63 74 20 74 68 61 74 20 63 65 72 74 61 69 6e  act that certain
21a82 20 74 6f 6b 65 6e 20 76 61 6c 75 65 73 20 28 65   token values (e
21a83 78 3a 20 54 4b 5f 45 51 29 0a 2a 2a 20 61 72 65  x: TK_EQ).** are
21a84 20 74 68 65 20 73 61 6d 65 20 61 73 20 6f 70 63   the same as opc
21a85 6f 64 65 20 76 61 6c 75 65 73 20 28 65 78 3a 20  ode values (ex: 
21a86 4f 50 5f 45 71 29 20 74 68 61 74 20 69 6d 70 6c  OP_Eq) that impl
21a87 65 6d 65 6e 74 20 74 68 65 20 63 6f 72 72 65 73  ement the corres
21a88 70 6f 6e 64 69 6e 67 0a 2a 2a 20 6f 70 65 72 61  ponding.** opera
21a89 74 69 6f 6e 2e 20 20 53 70 65 63 69 61 6c 20 63  tion.  Special c
21a8a 6f 6d 6d 65 6e 74 73 20 69 6e 20 76 64 62 65 2e  omments in vdbe.
21a8b 63 20 61 6e 64 20 74 68 65 20 6d 6b 6f 70 63 6f  c and the mkopco
21a8c 64 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20 69  deh.awk script i
21a8d 6e 0a 2a 2a 20 74 68 65 20 6d 61 6b 65 20 70 72  n.** the make pr
21a8e 6f 63 65 73 73 20 63 61 75 73 65 20 74 68 65 73  ocess cause thes
21a8f 65 20 76 61 6c 75 65 73 20 74 6f 20 61 6c 69 67  e values to alig
21a90 6e 2e 20 20 41 73 73 65 72 74 28 29 73 20 69 6e  n.  Assert()s in
21a91 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 62 65 6c   the code.** bel
21a92 6f 77 20 76 65 72 69 66 79 20 74 68 61 74 20 74  ow verify that t
21a93 68 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20 61  he numbers are a
21a94 6c 69 67 6e 65 64 20 63 6f 72 72 65 63 74 6c 79  ligned correctly
21a95 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
21a96 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
21a97 45 78 70 72 49 66 54 72 75 65 28 50 61 72 73 65  ExprIfTrue(Parse
21a98 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
21a99 70 45 78 70 72 2c 20 69 6e 74 20 64 65 73 74 2c  pExpr, int dest,
21a9a 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29   int jumpIfNull)
21a9b 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  {.  Vdbe *v = pP
21a9c 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
21a9d 6e 74 20 6f 70 20 3d 20 30 3b 0a 20 20 69 6e 74  nt op = 0;.  int
21a9e 20 72 65 67 46 72 65 65 31 20 3d 20 30 3b 0a 20   regFree1 = 0;. 
21a9f 20 69 6e 74 20 72 65 67 46 72 65 65 32 20 3d 20   int regFree2 = 
21aa0 30 3b 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 3b  0;.  int r1, r2;
21aa1 0a 0a 20 20 61 73 73 65 72 74 28 20 6a 75 6d 70  ..  assert( jump
21aa2 49 66 4e 75 6c 6c 3d 3d 53 51 4c 49 54 45 5f 4a  IfNull==SQLITE_J
21aa3 55 4d 50 49 46 4e 55 4c 4c 20 7c 7c 20 6a 75 6d  UMPIFNULL || jum
21aa4 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20  pIfNull==0 );.  
21aa5 69 66 28 20 4e 45 56 45 52 28 76 3d 3d 30 29 20  if( NEVER(v==0) 
21aa6 29 20 20 20 20 20 72 65 74 75 72 6e 3b 20 20 2f  )     return;  /
21aa7 2a 20 45 78 69 73 74 61 6e 63 65 20 6f 66 20 56  * Existance of V
21aa8 44 42 45 20 63 68 65 63 6b 65 64 20 62 79 20 63  DBE checked by c
21aa9 61 6c 6c 65 72 20 2a 2f 0a 20 20 69 66 28 20 4e  aller */.  if( N
21aaa 45 56 45 52 28 70 45 78 70 72 3d 3d 30 29 20 29  EVER(pExpr==0) )
21aab 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 4e 6f 20   return;  /* No 
21aac 77 61 79 20 74 68 69 73 20 63 61 6e 20 68 61 70  way this can hap
21aad 70 65 6e 20 2a 2f 0a 20 20 6f 70 20 3d 20 70 45  pen */.  op = pE
21aae 78 70 72 2d 3e 6f 70 3b 0a 20 20 73 77 69 74 63  xpr->op;.  switc
21aaf 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73  h( op ){.    cas
21ab0 65 20 54 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20 20  e TK_AND: {.    
21ab1 20 20 69 6e 74 20 64 32 20 3d 20 73 71 6c 69 74    int d2 = sqlit
21ab2 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
21ab3 76 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  v);.      testca
21ab4 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d  se( jumpIfNull==
21ab5 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 );.      sqlit
21ab6 65 33 45 78 70 72 43 61 63 68 65 50 75 73 68 28  e3ExprCachePush(
21ab7 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73  pParse);.      s
21ab8 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73  qlite3ExprIfFals
21ab9 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
21aba 3e 70 4c 65 66 74 2c 20 64 32 2c 6a 75 6d 70 49  >pLeft, d2,jumpI
21abb 66 4e 75 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55 4d  fNull^SQLITE_JUM
21abc 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20  PIFNULL);.      
21abd 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75  sqlite3ExprIfTru
21abe 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
21abf 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a  >pRight, dest, j
21ac0 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20  umpIfNull);.    
21ac1 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
21ac2 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 32 29  olveLabel(v, d2)
21ac3 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
21ac4 78 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72  xprCachePop(pPar
21ac5 73 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 62 72  se, 1);.      br
21ac6 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
21ac7 61 73 65 20 54 4b 5f 4f 52 3a 20 7b 0a 20 20 20  ase TK_OR: {.   
21ac8 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d     testcase( jum
21ac9 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20  pIfNull==0 );.  
21aca 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
21acb 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 70 45  fTrue(pParse, pE
21acc 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74  xpr->pLeft, dest
21acd 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20  , jumpIfNull);. 
21ace 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
21acf 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 70  IfTrue(pParse, p
21ad0 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 65  Expr->pRight, de
21ad1 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b  st, jumpIfNull);
21ad2 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
21ad3 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
21ad4 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 74 65 73  NOT: {.      tes
21ad5 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c  tcase( jumpIfNul
21ad6 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71  l==0 );.      sq
21ad7 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65  lite3ExprIfFalse
21ad8 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
21ad9 70 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d  pLeft, dest, jum
21ada 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  pIfNull);.      
21adb 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
21adc 20 63 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20   case TK_LT:.   
21add 20 63 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20   case TK_LE:.   
21ade 20 63 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20   case TK_GT:.   
21adf 20 63 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20   case TK_GE:.   
21ae0 20 63 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20   case TK_NE:.   
21ae1 20 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20   case TK_EQ: {. 
21ae2 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
21ae3 4c 54 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 20 20 20  LT==OP_Lt );.   
21ae4 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 45     assert( TK_LE
21ae5 3d 3d 4f 50 5f 4c 65 20 29 3b 0a 20 20 20 20 20  ==OP_Le );.     
21ae6 20 61 73 73 65 72 74 28 20 54 4b 5f 47 54 3d 3d   assert( TK_GT==
21ae7 4f 50 5f 47 74 20 29 3b 0a 20 20 20 20 20 20 61  OP_Gt );.      a
21ae8 73 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 4f 50  ssert( TK_GE==OP
21ae9 5f 47 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73  _Ge );.      ass
21aea 65 72 74 28 20 54 4b 5f 45 51 3d 3d 4f 50 5f 45  ert( TK_EQ==OP_E
21aeb 71 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  q );.      asser
21aec 74 28 20 54 4b 5f 4e 45 3d 3d 4f 50 5f 4e 65 20  t( TK_NE==OP_Ne 
21aed 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21aee 65 28 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b 0a  e( op==TK_LT );.
21aef 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21af0 6f 70 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20 20  op==TK_LE );.   
21af1 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
21af2 3d 54 4b 5f 47 54 20 29 3b 0a 20 20 20 20 20 20  =TK_GT );.      
21af3 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
21af4 5f 47 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73  _GE );.      tes
21af5 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 51  tcase( op==TK_EQ
21af6 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21af7 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b  se( op==TK_NE );
21af8 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21af9 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29   jumpIfNull==0 )
21afa 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70  ;.      codeComp
21afb 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72  areOperands(pPar
21afc 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
21afd 2c 20 26 72 31 2c 20 26 72 65 67 46 72 65 65 31  , &r1, &regFree1
21afe 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
21aff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21b00 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68      pExpr->pRigh
21b01 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72 65 65  t, &r2, &regFree
21b02 32 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f  2);.      codeCo
21b03 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 45  mpare(pParse, pE
21b04 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70  xpr->pLeft, pExp
21b05 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20  r->pRight, op,. 
21b06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21b07 20 72 31 2c 20 72 32 2c 20 64 65 73 74 2c 20 6a   r1, r2, dest, j
21b08 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20  umpIfNull);.    
21b09 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46    testcase( regF
21b0a 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ree1==0 );.     
21b0b 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72   testcase( regFr
21b0c 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ee2==0 );.      
21b0d 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
21b0e 20 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a   case TK_ISNULL:
21b0f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54  .    case TK_NOT
21b10 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 61 73  NULL: {.      as
21b11 73 65 72 74 28 20 54 4b 5f 49 53 4e 55 4c 4c 3d  sert( TK_ISNULL=
21b12 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20  =OP_IsNull );.  
21b13 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e      assert( TK_N
21b14 4f 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75  OTNULL==OP_NotNu
21b15 6c 6c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ll );.      test
21b16 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e  case( op==TK_ISN
21b17 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ULL );.      tes
21b18 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f  tcase( op==TK_NO
21b19 54 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 72  TNULL );.      r
21b1a 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  1 = sqlite3ExprC
21b1b 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20  odeTemp(pParse, 
21b1c 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72  pExpr->pLeft, &r
21b1d 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20  egFree1);.      
21b1e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21b1f 32 28 76 2c 20 6f 70 2c 20 72 31 2c 20 64 65 73  2(v, op, r1, des
21b20 74 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  t);.      testca
21b21 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20  se( regFree1==0 
21b22 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
21b23 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
21b24 4b 5f 42 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20  K_BETWEEN: {.   
21b25 20 20 20 2f 2a 20 20 20 20 78 20 42 45 54 57 45     /*    x BETWE
21b26 45 4e 20 79 20 41 4e 44 20 7a 0a 20 20 20 20 20  EN y AND z.     
21b27 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 73 20   **.      ** Is 
21b28 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a 20  equivalent to . 
21b29 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
21b2a 20 20 20 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d      x>=y AND x<=
21b2b 7a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  z.      **.     
21b2c 20 2a 2a 20 43 6f 64 65 20 69 74 20 61 73 20 73   ** Code it as s
21b2d 75 63 68 2c 20 74 61 6b 69 6e 67 20 63 61 72 65  uch, taking care
21b2e 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 6d 6f   to do the commo
21b2f 6e 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 0a  n subexpression.
21b30 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74        ** element
21b31 61 74 69 6f 6e 20 6f 66 20 78 2e 0a 20 20 20 20  ation of x..    
21b32 20 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20    */.      Expr 
21b33 65 78 70 72 41 6e 64 3b 0a 20 20 20 20 20 20 45  exprAnd;.      E
21b34 78 70 72 20 63 6f 6d 70 4c 65 66 74 3b 0a 20 20  xpr compLeft;.  
21b35 20 20 20 20 45 78 70 72 20 63 6f 6d 70 52 69 67      Expr compRig
21b36 68 74 3b 0a 20 20 20 20 20 20 45 78 70 72 20 65  ht;.      Expr e
21b37 78 70 72 58 3b 0a 0a 20 20 20 20 20 20 61 73 73  xprX;..      ass
21b38 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f  ert( !ExprHasPro
21b39 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
21b3a 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20  xIsSelect) );.  
21b3b 20 20 20 20 65 78 70 72 58 20 3d 20 2a 70 45 78      exprX = *pEx
21b3c 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20  pr->pLeft;.     
21b3d 20 65 78 70 72 41 6e 64 2e 6f 70 20 3d 20 54 4b   exprAnd.op = TK
21b3e 5f 41 4e 44 3b 0a 20 20 20 20 20 20 65 78 70 72  _AND;.      expr
21b3f 41 6e 64 2e 70 4c 65 66 74 20 3d 20 26 63 6f 6d  And.pLeft = &com
21b40 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 65 78 70  pLeft;.      exp
21b41 72 41 6e 64 2e 70 52 69 67 68 74 20 3d 20 26 63  rAnd.pRight = &c
21b42 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20  ompRight;.      
21b43 63 6f 6d 70 4c 65 66 74 2e 6f 70 20 3d 20 54 4b  compLeft.op = TK
21b44 5f 47 45 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c  _GE;.      compL
21b45 65 66 74 2e 70 4c 65 66 74 20 3d 20 26 65 78 70  eft.pLeft = &exp
21b46 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65  rX;.      compLe
21b47 66 74 2e 70 52 69 67 68 74 20 3d 20 70 45 78 70  ft.pRight = pExp
21b48 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 30 5d  r->x.pList->a[0]
21b49 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 63 6f  .pExpr;.      co
21b4a 6d 70 52 69 67 68 74 2e 6f 70 20 3d 20 54 4b 5f  mpRight.op = TK_
21b4b 4c 45 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52 69  LE;.      compRi
21b4c 67 68 74 2e 70 4c 65 66 74 20 3d 20 26 65 78 70  ght.pLeft = &exp
21b4d 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52 69  rX;.      compRi
21b4e 67 68 74 2e 70 52 69 67 68 74 20 3d 20 70 45 78  ght.pRight = pEx
21b4f 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 31  pr->x.pList->a[1
21b50 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 65  ].pExpr;.      e
21b51 78 70 72 58 2e 69 54 61 62 6c 65 20 3d 20 73 71  xprX.iTable = sq
21b52 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d  lite3ExprCodeTem
21b53 70 28 70 50 61 72 73 65 2c 20 26 65 78 70 72 58  p(pParse, &exprX
21b54 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20  , &regFree1);.  
21b55 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65      testcase( re
21b56 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20  gFree1==0 );.   
21b57 20 20 20 65 78 70 72 58 2e 6f 70 20 3d 20 54 4b     exprX.op = TK
21b58 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20 20 20  _REGISTER;.     
21b59 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49   testcase( jumpI
21b5a 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  fNull==0 );.    
21b5b 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 54    sqlite3ExprIfT
21b5c 72 75 65 28 70 50 61 72 73 65 2c 20 26 65 78 70  rue(pParse, &exp
21b5d 72 41 6e 64 2c 20 64 65 73 74 2c 20 6a 75 6d 70  rAnd, dest, jump
21b5e 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62  IfNull);.      b
21b5f 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
21b60 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
21b61 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70   r1 = sqlite3Exp
21b62 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65  rCodeTemp(pParse
21b63 2c 20 70 45 78 70 72 2c 20 26 72 65 67 46 72 65  , pExpr, &regFre
21b64 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  e1);.      sqlit
21b65 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
21b66 4f 50 5f 49 66 2c 20 72 31 2c 20 64 65 73 74 2c  OP_If, r1, dest,
21b67 20 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d 30 29 3b   jumpIfNull!=0);
21b68 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21b69 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a   regFree1==0 );.
21b6a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21b6b 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b  jumpIfNull==0 );
21b6c 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
21b6d 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65    }.  }.  sqlite
21b6e 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
21b6f 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 31  pParse, regFree1
21b70 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65  );.  sqlite3Rele
21b71 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
21b72 65 2c 20 72 65 67 46 72 65 65 32 29 3b 20 20 0a  e, regFree2);  .
21b73 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
21b74 65 20 63 6f 64 65 20 66 6f 72 20 61 20 62 6f 6f  e code for a boo
21b75 6c 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  lean expression 
21b76 73 75 63 68 20 74 68 61 74 20 61 20 6a 75 6d 70  such that a jump
21b77 20 69 73 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 74   is made.** to t
21b78 68 65 20 6c 61 62 65 6c 20 22 64 65 73 74 22 20  he label "dest" 
21b79 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  if the expressio
21b7a 6e 20 69 73 20 66 61 6c 73 65 20 62 75 74 20 65  n is false but e
21b7b 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 63 6f 6e 74  xecution.** cont
21b7c 69 6e 75 65 73 20 73 74 72 61 69 67 68 74 20 74  inues straight t
21b7d 68 72 75 20 69 66 20 74 68 65 20 65 78 70 72 65  hru if the expre
21b7e 73 73 69 6f 6e 20 69 73 20 74 72 75 65 2e 0a 2a  ssion is true..*
21b7f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72  *.** If the expr
21b80 65 73 73 69 6f 6e 20 65 76 61 6c 75 61 74 65 73  ession evaluates
21b81 20 74 6f 20 4e 55 4c 4c 20 28 6e 65 69 74 68 65   to NULL (neithe
21b82 72 20 74 72 75 65 20 6e 6f 72 20 66 61 6c 73 65  r true nor false
21b83 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 69  ) then.** jump i
21b84 66 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 69 73 20  f jumpIfNull is 
21b85 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c  SQLITE_JUMPIFNUL
21b86 4c 20 6f 72 20 66 61 6c 6c 20 74 68 72 6f 75 67  L or fall throug
21b87 68 20 69 66 20 6a 75 6d 70 49 66 4e 75 6c 6c 0a  h if jumpIfNull.
21b88 2a 2a 20 69 73 20 30 2e 0a 2a 2f 0a 53 51 4c 49  ** is 0..*/.SQLI
21b89 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
21b8a 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c  sqlite3ExprIfFal
21b8b 73 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  se(Parse *pParse
21b8c 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69  , Expr *pExpr, i
21b8d 6e 74 20 64 65 73 74 2c 20 69 6e 74 20 6a 75 6d  nt dest, int jum
21b8e 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 56 64 62 65  pIfNull){.  Vdbe
21b8f 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
21b90 64 62 65 3b 0a 20 20 69 6e 74 20 6f 70 20 3d 20  dbe;.  int op = 
21b91 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65  0;.  int regFree
21b92 31 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67  1 = 0;.  int reg
21b93 46 72 65 65 32 20 3d 20 30 3b 0a 20 20 69 6e 74  Free2 = 0;.  int
21b94 20 72 31 2c 20 72 32 3b 0a 0a 20 20 61 73 73 65   r1, r2;..  asse
21b95 72 74 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d  rt( jumpIfNull==
21b96 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c  SQLITE_JUMPIFNUL
21b97 4c 20 7c 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d  L || jumpIfNull=
21b98 3d 30 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45  =0 );.  if( NEVE
21b99 52 28 76 3d 3d 30 29 20 29 20 72 65 74 75 72 6e  R(v==0) ) return
21b9a 3b 20 2f 2a 20 45 78 69 73 74 61 6e 63 65 20 6f  ; /* Existance o
21b9b 66 20 56 44 42 45 20 63 68 65 63 6b 65 64 20 62  f VDBE checked b
21b9c 79 20 63 61 6c 6c 65 72 20 2a 2f 0a 20 20 69 66  y caller */.  if
21b9d 28 20 70 45 78 70 72 3d 3d 30 20 29 20 20 20 20  ( pExpr==0 )    
21b9e 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 54 68  return;..  /* Th
21b9f 65 20 76 61 6c 75 65 20 6f 66 20 70 45 78 70 72  e value of pExpr
21ba0 2d 3e 6f 70 20 61 6e 64 20 6f 70 20 61 72 65 20  ->op and op are 
21ba1 72 65 6c 61 74 65 64 20 61 73 20 66 6f 6c 6c 6f  related as follo
21ba2 77 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  ws:.  **.  **   
21ba3 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 20 20      pExpr->op   
21ba4 20 20 20 20 20 20 20 20 20 6f 70 0a 20 20 2a 2a           op.  **
21ba5 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d         ---------
21ba6 20 20 20 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d            ------
21ba7 2d 2d 2d 2d 0a 20 20 2a 2a 20 20 20 20 20 20 20  ----.  **       
21ba8 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 20 20 20 20  TK_ISNULL       
21ba9 20 20 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 0a 20 20     OP_NotNull.  
21baa 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4e 4f 54 4e  **       TK_NOTN
21bab 55 4c 4c 20 20 20 20 20 20 20 20 20 4f 50 5f 49  ULL         OP_I
21bac 73 4e 75 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 20  sNull.  **      
21bad 20 54 4b 5f 4e 45 20 20 20 20 20 20 20 20 20 20   TK_NE          
21bae 20 20 20 20 4f 50 5f 45 71 0a 20 20 2a 2a 20 20      OP_Eq.  **  
21baf 20 20 20 20 20 54 4b 5f 45 51 20 20 20 20 20 20       TK_EQ      
21bb0 20 20 20 20 20 20 20 20 4f 50 5f 4e 65 0a 20 20          OP_Ne.  
21bb1 2a 2a 20 20 20 20 20 20 20 54 4b 5f 47 54 20 20  **       TK_GT  
21bb2 20 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 4c              OP_L
21bb3 65 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f  e.  **       TK_
21bb4 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LE              
21bb5 4f 50 5f 47 74 0a 20 20 2a 2a 20 20 20 20 20 20  OP_Gt.  **      
21bb6 20 54 4b 5f 47 45 20 20 20 20 20 20 20 20 20 20   TK_GE          
21bb7 20 20 20 20 4f 50 5f 4c 74 0a 20 20 2a 2a 20 20      OP_Lt.  **  
21bb8 20 20 20 20 20 54 4b 5f 4c 54 20 20 20 20 20 20       TK_LT      
21bb9 20 20 20 20 20 20 20 20 4f 50 5f 47 65 0a 20 20          OP_Ge.  
21bba 2a 2a 0a 20 20 2a 2a 20 46 6f 72 20 6f 74 68 65  **.  ** For othe
21bbb 72 20 76 61 6c 75 65 73 20 6f 66 20 70 45 78 70  r values of pExp
21bbc 72 2d 3e 6f 70 2c 20 6f 70 20 69 73 20 75 6e 64  r->op, op is und
21bbd 65 66 69 6e 65 64 20 61 6e 64 20 75 6e 75 73 65  efined and unuse
21bbe 64 2e 0a 20 20 2a 2a 20 54 68 65 20 76 61 6c 75  d..  ** The valu
21bbf 65 20 6f 66 20 54 4b 5f 20 61 6e 64 20 4f 50 5f  e of TK_ and OP_
21bc0 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 61   constants are a
21bc1 72 72 61 6e 67 65 64 20 73 75 63 68 20 74 68 61  rranged such tha
21bc2 74 20 77 65 0a 20 20 2a 2a 20 63 61 6e 20 63 6f  t we.  ** can co
21bc3 6d 70 75 74 65 20 74 68 65 20 6d 61 70 70 69 6e  mpute the mappin
21bc4 67 20 61 62 6f 76 65 20 75 73 69 6e 67 20 74 68  g above using th
21bc5 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 70 72  e following expr
21bc6 65 73 73 69 6f 6e 2e 0a 20 20 2a 2a 20 41 73 73  ession..  ** Ass
21bc7 65 72 74 28 29 73 20 76 65 72 69 66 79 20 74 68  ert()s verify th
21bc8 61 74 20 74 68 65 20 63 6f 6d 70 75 74 61 74 69  at the computati
21bc9 6f 6e 20 69 73 20 63 6f 72 72 65 63 74 2e 0a 20  on is correct.. 
21bca 20 2a 2f 0a 20 20 6f 70 20 3d 20 28 28 70 45 78   */.  op = ((pEx
21bcb 70 72 2d 3e 6f 70 2b 28 54 4b 5f 49 53 4e 55 4c  pr->op+(TK_ISNUL
21bcc 4c 26 31 29 29 5e 31 29 2d 28 54 4b 5f 49 53 4e  L&1))^1)-(TK_ISN
21bcd 55 4c 4c 26 31 29 3b 0a 0a 20 20 2f 2a 20 56 65  ULL&1);..  /* Ve
21bce 72 69 66 79 20 63 6f 72 72 65 63 74 20 61 6c 69  rify correct ali
21bcf 67 6e 6d 65 6e 74 20 6f 66 20 54 4b 5f 20 61 6e  gnment of TK_ an
21bd0 64 20 4f 50 5f 20 63 6f 6e 73 74 61 6e 74 73 0a  d OP_ constants.
21bd1 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
21bd2 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49 53 4e  Expr->op!=TK_ISN
21bd3 55 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e 6f  ULL || op==OP_No
21bd4 74 4e 75 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72  tNull );.  asser
21bd5 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  t( pExpr->op!=TK
21bd6 5f 4e 4f 54 4e 55 4c 4c 20 7c 7c 20 6f 70 3d 3d  _NOTNULL || op==
21bd7 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 61  OP_IsNull );.  a
21bd8 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70  ssert( pExpr->op
21bd9 21 3d 54 4b 5f 4e 45 20 7c 7c 20 6f 70 3d 3d 4f  !=TK_NE || op==O
21bda 50 5f 45 71 20 29 3b 0a 20 20 61 73 73 65 72 74  P_Eq );.  assert
21bdb 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f  ( pExpr->op!=TK_
21bdc 45 51 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e 65 20  EQ || op==OP_Ne 
21bdd 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78  );.  assert( pEx
21bde 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 54 20 7c 7c  pr->op!=TK_LT ||
21bdf 20 6f 70 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20   op==OP_Ge );.  
21be0 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f  assert( pExpr->o
21be1 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20 6f 70 3d 3d  p!=TK_LE || op==
21be2 4f 50 5f 47 74 20 29 3b 0a 20 20 61 73 73 65 72  OP_Gt );.  asser
21be3 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  t( pExpr->op!=TK
21be4 5f 47 54 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c 65  _GT || op==OP_Le
21be5 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45   );.  assert( pE
21be6 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 47 45 20 7c  xpr->op!=TK_GE |
21be7 7c 20 6f 70 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 0a  | op==OP_Lt );..
21be8 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d    switch( pExpr-
21be9 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20  >op ){.    case 
21bea 54 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20 20 20 20  TK_AND: {.      
21beb 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66  testcase( jumpIf
21bec 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20  Null==0 );.     
21bed 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61   sqlite3ExprIfFa
21bee 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  lse(pParse, pExp
21bef 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c 20  r->pLeft, dest, 
21bf0 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20  jumpIfNull);.   
21bf1 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
21bf2 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45  False(pParse, pE
21bf3 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73  xpr->pRight, des
21bf4 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a  t, jumpIfNull);.
21bf5 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21bf6 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f   }.    case TK_O
21bf7 52 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64  R: {.      int d
21bf8 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  2 = sqlite3VdbeM
21bf9 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
21bfa 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d     testcase( jum
21bfb 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20  pIfNull==0 );.  
21bfc 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
21bfd 61 63 68 65 50 75 73 68 28 70 50 61 72 73 65 29  achePush(pParse)
21bfe 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
21bff 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65  xprIfTrue(pParse
21c00 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
21c01 64 32 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 5e 53  d2, jumpIfNull^S
21c02 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
21c03 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
21c04 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72  ExprIfFalse(pPar
21c05 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  se, pExpr->pRigh
21c06 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e  t, dest, jumpIfN
21c07 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ull);.      sqli
21c08 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
21c09 62 65 6c 28 76 2c 20 64 32 29 3b 0a 20 20 20 20  bel(v, d2);.    
21c0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
21c0b 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29  hePop(pParse, 1)
21c0c 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
21c0d 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
21c0e 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 73 71  _NOT: {.      sq
21c0f 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28  lite3ExprIfTrue(
21c10 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
21c11 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70  Left, dest, jump
21c12 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62  IfNull);.      b
21c13 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
21c14 63 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20  case TK_LT:.    
21c15 63 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20  case TK_LE:.    
21c16 63 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20  case TK_GT:.    
21c17 63 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20  case TK_GE:.    
21c18 63 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20  case TK_NE:.    
21c19 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20  case TK_EQ: {.  
21c1a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
21c1b 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20  ==TK_LT );.     
21c1c 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
21c1d 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65  K_LE );.      te
21c1e 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47  stcase( op==TK_G
21c1f 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  T );.      testc
21c20 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29  ase( op==TK_GE )
21c21 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21c22 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20  ( op==TK_EQ );. 
21c23 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
21c24 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20  p==TK_NE );.    
21c25 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70    testcase( jump
21c26 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20  IfNull==0 );.   
21c27 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70     codeCompareOp
21c28 65 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70  erands(pParse, p
21c29 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31  Expr->pLeft, &r1
21c2a 2c 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20  , &regFree1,.   
21c2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
21c2d 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72  Expr->pRight, &r
21c2e 32 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20  2, &regFree2);. 
21c2f 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65       codeCompare
21c30 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
21c31 70 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52  pLeft, pExpr->pR
21c32 69 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20  ight, op,.      
21c33 20 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20              r1, 
21c34 72 32 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66  r2, dest, jumpIf
21c35 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 74 65 73  Null);.      tes
21c36 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d  tcase( regFree1=
21c37 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  =0 );.      test
21c38 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d  case( regFree2==
21c39 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  0 );.      break
21c3a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
21c3b 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20   TK_ISNULL:.    
21c3c 63 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a  case TK_NOTNULL:
21c3d 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73   {.      testcas
21c3e 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c  e( op==TK_ISNULL
21c3f 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21c40 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55  se( op==TK_NOTNU
21c41 4c 4c 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d  LL );.      r1 =
21c42 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
21c43 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78  Temp(pParse, pEx
21c44 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46  pr->pLeft, &regF
21c45 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  ree1);.      sql
21c46 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
21c47 2c 20 6f 70 2c 20 72 31 2c 20 64 65 73 74 29 3b  , op, r1, dest);
21c48 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21c49 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a   regFree1==0 );.
21c4a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21c4b 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42   }.    case TK_B
21c4c 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20  ETWEEN: {.      
21c4d 2f 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e 20  /*    x BETWEEN 
21c4e 79 20 41 4e 44 20 7a 0a 20 20 20 20 20 20 2a 2a  y AND z.      **
21c4f 0a 20 20 20 20 20 20 2a 2a 20 49 73 20 65 71 75  .      ** Is equ
21c50 69 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20  ivalent to .    
21c51 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20    **.      **   
21c52 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20   x>=y AND x<=z. 
21c53 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
21c54 20 43 6f 64 65 20 69 74 20 61 73 20 73 75 63 68   Code it as such
21c55 2c 20 74 61 6b 69 6e 67 20 63 61 72 65 20 74 6f  , taking care to
21c56 20 64 6f 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 73   do the common s
21c57 75 62 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 20  ubexpression.   
21c58 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 61 74 69     ** elementati
21c59 6f 6e 20 6f 66 20 78 2e 0a 20 20 20 20 20 20 2a  on of x..      *
21c5a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 65 78 70  /.      Expr exp
21c5b 72 41 6e 64 3b 0a 20 20 20 20 20 20 45 78 70 72  rAnd;.      Expr
21c5c 20 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20   compLeft;.     
21c5d 20 45 78 70 72 20 63 6f 6d 70 52 69 67 68 74 3b   Expr compRight;
21c5e 0a 20 20 20 20 20 20 45 78 70 72 20 65 78 70 72  .      Expr expr
21c5f 58 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74  X;..      assert
21c60 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
21c61 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73  ty(pExpr, EP_xIs
21c62 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20  Select) );.     
21c63 20 65 78 70 72 58 20 3d 20 2a 70 45 78 70 72 2d   exprX = *pExpr-
21c64 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 65 78  >pLeft;.      ex
21c65 70 72 41 6e 64 2e 6f 70 20 3d 20 54 4b 5f 41 4e  prAnd.op = TK_AN
21c66 44 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e 64  D;.      exprAnd
21c67 2e 70 4c 65 66 74 20 3d 20 26 63 6f 6d 70 4c 65  .pLeft = &compLe
21c68 66 74 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e  ft;.      exprAn
21c69 64 2e 70 52 69 67 68 74 20 3d 20 26 63 6f 6d 70  d.pRight = &comp
21c6a 52 69 67 68 74 3b 0a 20 20 20 20 20 20 63 6f 6d  Right;.      com
21c6b 70 4c 65 66 74 2e 6f 70 20 3d 20 54 4b 5f 47 45  pLeft.op = TK_GE
21c6c 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74  ;.      compLeft
21c6d 2e 70 4c 65 66 74 20 3d 20 26 65 78 70 72 58 3b  .pLeft = &exprX;
21c6e 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e  .      compLeft.
21c6f 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e  pRight = pExpr->
21c70 78 2e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  x.pList->a[0].pE
21c71 78 70 72 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52  xpr;.      compR
21c72 69 67 68 74 2e 6f 70 20 3d 20 54 4b 5f 4c 45 3b  ight.op = TK_LE;
21c73 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74  .      compRight
21c74 2e 70 4c 65 66 74 20 3d 20 26 65 78 70 72 58 3b  .pLeft = &exprX;
21c75 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74  .      compRight
21c76 2e 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d  .pRight = pExpr-
21c77 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70  >x.pList->a[1].p
21c78 45 78 70 72 3b 0a 20 20 20 20 20 20 65 78 70 72  Expr;.      expr
21c79 58 2e 69 54 61 62 6c 65 20 3d 20 73 71 6c 69 74  X.iTable = sqlit
21c7a 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70  e3ExprCodeTemp(p
21c7b 50 61 72 73 65 2c 20 26 65 78 70 72 58 2c 20 26  Parse, &exprX, &
21c7c 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20  regFree1);.     
21c7d 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72   testcase( regFr
21c7e 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ee1==0 );.      
21c7f 65 78 70 72 58 2e 6f 70 20 3d 20 54 4b 5f 52 45  exprX.op = TK_RE
21c80 47 49 53 54 45 52 3b 0a 20 20 20 20 20 20 74 65  GISTER;.      te
21c81 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75  stcase( jumpIfNu
21c82 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73  ll==0 );.      s
21c83 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73  qlite3ExprIfFals
21c84 65 28 70 50 61 72 73 65 2c 20 26 65 78 70 72 41  e(pParse, &exprA
21c85 6e 64 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66  nd, dest, jumpIf
21c86 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65  Null);.      bre
21c87 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65  ak;.    }.    de
21c88 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72  fault: {.      r
21c89 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  1 = sqlite3ExprC
21c8a 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20  odeTemp(pParse, 
21c8b 70 45 78 70 72 2c 20 26 72 65 67 46 72 65 65 31  pExpr, &regFree1
21c8c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
21c8d 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
21c8e 5f 49 66 4e 6f 74 2c 20 72 31 2c 20 64 65 73 74  _IfNot, r1, dest
21c8f 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d 30 29  , jumpIfNull!=0)
21c90 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21c91 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b  ( regFree1==0 );
21c92 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21c93 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29   jumpIfNull==0 )
21c94 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
21c95 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
21c96 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
21c97 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65  (pParse, regFree
21c98 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c  1);.  sqlite3Rel
21c99 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
21c9a 73 65 2c 20 72 65 67 46 72 65 65 32 29 3b 0a 7d  se, regFree2);.}
21c9b 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 20 64 65 65  ../*.** Do a dee
21c9c 70 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20  p comparison of 
21c9d 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e 20 74  two expression t
21c9e 72 65 65 73 2e 20 20 52 65 74 75 72 6e 20 54 52  rees.  Return TR
21c9f 55 45 20 28 6e 6f 6e 2d 7a 65 72 6f 29 0a 2a 2a  UE (non-zero).**
21ca0 20 69 66 20 74 68 65 79 20 61 72 65 20 69 64 65   if they are ide
21ca1 6e 74 69 63 61 6c 20 61 6e 64 20 72 65 74 75 72  ntical and retur
21ca2 6e 20 46 41 4c 53 45 20 69 66 20 74 68 65 79 20  n FALSE if they 
21ca3 64 69 66 66 65 72 20 69 6e 20 61 6e 79 20 77 61  differ in any wa
21ca4 79 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d  y..**.** Sometim
21ca5 65 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  es this routine 
21ca6 77 69 6c 6c 20 72 65 74 75 72 6e 20 46 41 4c 53  will return FALS
21ca7 45 20 65 76 65 6e 20 69 66 20 74 68 65 20 74 77  E even if the tw
21ca8 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a  o expressions.**
21ca9 20 72 65 61 6c 6c 79 20 61 72 65 20 65 71 75 69   really are equi
21caa 76 61 6c 65 6e 74 2e 20 20 49 66 20 77 65 20 63  valent.  If we c
21cab 61 6e 6e 6f 74 20 70 72 6f 76 65 20 74 68 61 74  annot prove that
21cac 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73   the expressions
21cad 20 61 72 65 0a 2a 2a 20 69 64 65 6e 74 69 63 61   are.** identica
21cae 6c 2c 20 77 65 20 72 65 74 75 72 6e 20 46 41 4c  l, we return FAL
21caf 53 45 20 6a 75 73 74 20 74 6f 20 62 65 20 73 61  SE just to be sa
21cb0 66 65 2e 20 20 53 6f 20 69 66 20 74 68 69 73 20  fe.  So if this 
21cb1 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72  routine.** retur
21cb2 6e 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 79  ns false, then y
21cb3 6f 75 20 64 6f 20 6e 6f 74 20 72 65 61 6c 6c 79  ou do not really
21cb4 20 6b 6e 6f 77 20 66 6f 72 20 63 65 72 74 61 69   know for certai
21cb5 6e 20 69 66 20 74 68 65 20 74 77 6f 0a 2a 2a 20  n if the two.** 
21cb6 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20  expressions are 
21cb7 74 68 65 20 73 61 6d 65 2e 20 20 42 75 74 20 69  the same.  But i
21cb8 66 20 79 6f 75 20 67 65 74 20 61 20 54 52 55 45  f you get a TRUE
21cb9 20 72 65 74 75 72 6e 2c 20 74 68 65 6e 20 79 6f   return, then yo
21cba 75 0a 2a 2a 20 63 61 6e 20 62 65 20 73 75 72 65  u.** can be sure
21cbb 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73   the expressions
21cbc 20 61 72 65 20 74 68 65 20 73 61 6d 65 2e 20 20   are the same.  
21cbd 49 6e 20 74 68 65 20 70 6c 61 63 65 73 20 77 68  In the places wh
21cbe 65 72 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74  ere.** this rout
21cbf 69 6e 65 20 69 73 20 75 73 65 64 2c 20 69 74 20  ine is used, it 
21cc0 64 6f 65 73 20 6e 6f 74 20 68 75 72 74 20 74 6f  does not hurt to
21cc1 20 67 65 74 20 61 6e 20 65 78 74 72 61 20 46 41   get an extra FA
21cc2 4c 53 45 20 2d 20 74 68 61 74 0a 2a 2a 20 6a 75  LSE - that.** ju
21cc3 73 74 20 6d 69 67 68 74 20 72 65 73 75 6c 74 20  st might result 
21cc4 69 6e 20 73 6f 6d 65 20 73 6c 69 67 68 74 6c 79  in some slightly
21cc5 20 73 6c 6f 77 65 72 20 63 6f 64 65 2e 20 20 42   slower code.  B
21cc6 75 74 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20  ut returning.** 
21cc7 61 6e 20 69 6e 63 6f 72 72 65 63 74 20 54 52 55  an incorrect TRU
21cc8 45 20 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f 20  E could lead to 
21cc9 61 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e 0a 2a  a malfunction..*
21cca 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
21ccb 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
21ccc 43 6f 6d 70 61 72 65 28 45 78 70 72 20 2a 70 41  Compare(Expr *pA
21ccd 2c 20 45 78 70 72 20 2a 70 42 29 7b 0a 20 20 69  , Expr *pB){.  i
21cce 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 41 3d 3d  nt i;.  if( pA==
21ccf 30 7c 7c 70 42 3d 3d 30 20 29 7b 0a 20 20 20 20  0||pB==0 ){.    
21cd0 72 65 74 75 72 6e 20 70 42 3d 3d 70 41 3b 0a 20  return pB==pA;. 
21cd1 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 45 78   }.  assert( !Ex
21cd2 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  prHasAnyProperty
21cd3 28 70 41 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c  (pA, EP_TokenOnl
21cd4 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b  y|EP_Reduced) );
21cd5 0a 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72  .  assert( !Expr
21cd6 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70  HasAnyProperty(p
21cd7 42 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c  B, EP_TokenOnly|
21cd8 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 20  EP_Reduced) );. 
21cd9 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70   if( ExprHasProp
21cda 65 72 74 79 28 70 41 2c 20 45 50 5f 78 49 73 53  erty(pA, EP_xIsS
21cdb 65 6c 65 63 74 29 20 7c 7c 20 45 78 70 72 48 61  elect) || ExprHa
21cdc 73 50 72 6f 70 65 72 74 79 28 70 42 2c 20 45 50  sProperty(pB, EP
21cdd 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20  _xIsSelect) ){. 
21cde 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
21cdf 0a 20 20 69 66 28 20 28 70 41 2d 3e 66 6c 61 67  .  if( (pA->flag
21ce0 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63 74 29  s & EP_Distinct)
21ce1 21 3d 28 70 42 2d 3e 66 6c 61 67 73 20 26 20 45  !=(pB->flags & E
21ce2 50 5f 44 69 73 74 69 6e 63 74 29 20 29 20 72 65  P_Distinct) ) re
21ce3 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 41  turn 0;.  if( pA
21ce4 2d 3e 6f 70 21 3d 70 42 2d 3e 6f 70 20 29 20 72  ->op!=pB->op ) r
21ce5 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 21  eturn 0;.  if( !
21ce6 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61  sqlite3ExprCompa
21ce7 72 65 28 70 41 2d 3e 70 4c 65 66 74 2c 20 70 42  re(pA->pLeft, pB
21ce8 2d 3e 70 4c 65 66 74 29 20 29 20 72 65 74 75 72  ->pLeft) ) retur
21ce9 6e 20 30 3b 0a 20 20 69 66 28 20 21 73 71 6c 69  n 0;.  if( !sqli
21cea 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70  te3ExprCompare(p
21ceb 41 2d 3e 70 52 69 67 68 74 2c 20 70 42 2d 3e 70  A->pRight, pB->p
21cec 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20  Right) ) return 
21ced 30 3b 0a 0a 20 20 69 66 28 20 70 41 2d 3e 78 2e  0;..  if( pA->x.
21cee 70 4c 69 73 74 20 26 26 20 70 42 2d 3e 78 2e 70  pList && pB->x.p
21cef 4c 69 73 74 20 29 7b 0a 20 20 20 20 69 66 28 20  List ){.    if( 
21cf0 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78  pA->x.pList->nEx
21cf1 70 72 21 3d 70 42 2d 3e 78 2e 70 4c 69 73 74 2d  pr!=pB->x.pList-
21cf2 3e 6e 45 78 70 72 20 29 20 72 65 74 75 72 6e 20  >nExpr ) return 
21cf3 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  0;.    for(i=0; 
21cf4 69 3c 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e  i<pA->x.pList->n
21cf5 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
21cf6 20 20 45 78 70 72 20 2a 70 45 78 70 72 41 20 3d    Expr *pExprA =
21cf7 20 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b   pA->x.pList->a[
21cf8 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  i].pExpr;.      
21cf9 45 78 70 72 20 2a 70 45 78 70 72 42 20 3d 20 70  Expr *pExprB = p
21cfa 42 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 69 5d  B->x.pList->a[i]
21cfb 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69 66  .pExpr;.      if
21cfc 28 20 21 73 71 6c 69 74 65 33 45 78 70 72 43 6f  ( !sqlite3ExprCo
21cfd 6d 70 61 72 65 28 70 45 78 70 72 41 2c 20 70 45  mpare(pExprA, pE
21cfe 78 70 72 42 29 20 29 20 72 65 74 75 72 6e 20 30  xprB) ) return 0
21cff 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
21d00 69 66 28 20 70 41 2d 3e 78 2e 70 4c 69 73 74 20  if( pA->x.pList 
21d01 7c 7c 20 70 42 2d 3e 78 2e 70 4c 69 73 74 20 29  || pB->x.pList )
21d02 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
21d03 20 20 7d 0a 0a 20 20 69 66 28 20 70 41 2d 3e 69    }..  if( pA->i
21d04 54 61 62 6c 65 21 3d 70 42 2d 3e 69 54 61 62 6c  Table!=pB->iTabl
21d05 65 20 7c 7c 20 70 41 2d 3e 69 43 6f 6c 75 6d 6e  e || pA->iColumn
21d06 21 3d 70 42 2d 3e 69 43 6f 6c 75 6d 6e 20 29 20  !=pB->iColumn ) 
21d07 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
21d08 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
21d09 70 41 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29  pA, EP_IntValue)
21d0a 20 29 7b 0a 20 20 20 20 69 66 28 20 21 45 78 70   ){.    if( !Exp
21d0b 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 42 2c  rHasProperty(pB,
21d0c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 7c 7c   EP_IntValue) ||
21d0d 20 70 41 2d 3e 75 2e 69 56 61 6c 75 65 21 3d 70   pA->u.iValue!=p
21d0e 42 2d 3e 75 2e 69 56 61 6c 75 65 20 29 7b 0a 20  B->u.iValue ){. 
21d0f 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
21d10 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
21d11 20 70 41 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55   pA->op!=TK_COLU
21d12 4d 4e 20 26 26 20 70 41 2d 3e 75 2e 7a 54 6f 6b  MN && pA->u.zTok
21d13 65 6e 20 29 7b 0a 20 20 20 20 69 66 28 20 45 78  en ){.    if( Ex
21d14 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 42  prHasProperty(pB
21d15 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 7c  , EP_IntValue) |
21d16 7c 20 4e 45 56 45 52 28 70 42 2d 3e 75 2e 7a 54  | NEVER(pB->u.zT
21d17 6f 6b 65 6e 3d 3d 30 29 20 29 20 72 65 74 75 72  oken==0) ) retur
21d18 6e 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  n 0;.    if( sql
21d19 69 74 65 33 53 74 72 49 43 6d 70 28 70 41 2d 3e  ite3StrICmp(pA->
21d1a 75 2e 7a 54 6f 6b 65 6e 2c 70 42 2d 3e 75 2e 7a  u.zToken,pB->u.z
21d1b 54 6f 6b 65 6e 29 21 3d 30 20 29 7b 0a 20 20 20  Token)!=0 ){.   
21d1c 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
21d1d 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
21d1e 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  1;.}.../*.** Add
21d1f 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74   a new element t
21d20 6f 20 74 68 65 20 70 41 67 67 49 6e 66 6f 2d 3e  o the pAggInfo->
21d21 61 43 6f 6c 5b 5d 20 61 72 72 61 79 2e 20 20 52  aCol[] array.  R
21d22 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20  eturn the index 
21d23 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 65 6c  of.** the new el
21d24 65 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 61  ement.  Return a
21d25 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72   negative number
21d26 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73   if malloc fails
21d27 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
21d28 61 64 64 41 67 67 49 6e 66 6f 43 6f 6c 75 6d 6e  addAggInfoColumn
21d29 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 41 67  (sqlite3 *db, Ag
21d2a 67 49 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b 0a 20  gInfo *pInfo){. 
21d2b 20 69 6e 74 20 69 3b 0a 20 20 70 49 6e 66 6f 2d   int i;.  pInfo-
21d2c 3e 61 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 41  >aCol = sqlite3A
21d2d 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20  rrayAllocate(.  
21d2e 20 20 20 20 20 64 62 2c 0a 20 20 20 20 20 20 20       db,.       
21d2f 70 49 6e 66 6f 2d 3e 61 43 6f 6c 2c 0a 20 20 20  pInfo->aCol,.   
21d30 20 20 20 20 73 69 7a 65 6f 66 28 70 49 6e 66 6f      sizeof(pInfo
21d31 2d 3e 61 43 6f 6c 5b 30 5d 29 2c 0a 20 20 20 20  ->aCol[0]),.    
21d32 20 20 20 33 2c 0a 20 20 20 20 20 20 20 26 70 49     3,.       &pI
21d33 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 2c 0a 20 20  nfo->nColumn,.  
21d34 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 43 6f       &pInfo->nCo
21d35 6c 75 6d 6e 41 6c 6c 6f 63 2c 0a 20 20 20 20 20  lumnAlloc,.     
21d36 20 20 26 69 0a 20 20 29 3b 0a 20 20 72 65 74 75    &i.  );.  retu
21d37 72 6e 20 69 3b 0a 7d 20 20 20 20 0a 0a 2f 2a 0a  rn i;.}    ../*.
21d38 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65  ** Add a new ele
21d39 6d 65 6e 74 20 74 6f 20 74 68 65 20 70 41 67 67  ment to the pAgg
21d3a 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d 20 61 72  Info->aFunc[] ar
21d3b 72 61 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65  ray.  Return the
21d3c 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65   index of.** the
21d3d 20 6e 65 77 20 65 6c 65 6d 65 6e 74 2e 20 20 52   new element.  R
21d3e 65 74 75 72 6e 20 61 20 6e 65 67 61 74 69 76 65  eturn a negative
21d3f 20 6e 75 6d 62 65 72 20 69 66 20 6d 61 6c 6c 6f   number if mallo
21d40 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74  c fails..*/.stat
21d41 69 63 20 69 6e 74 20 61 64 64 41 67 67 49 6e 66  ic int addAggInf
21d42 6f 46 75 6e 63 28 73 71 6c 69 74 65 33 20 2a 64  oFunc(sqlite3 *d
21d43 62 2c 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66  b, AggInfo *pInf
21d44 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 70  o){.  int i;.  p
21d45 49 6e 66 6f 2d 3e 61 46 75 6e 63 20 3d 20 73 71  Info->aFunc = sq
21d46 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61  lite3ArrayAlloca
21d47 74 65 28 0a 20 20 20 20 20 20 20 64 62 2c 20 0a  te(.       db, .
21d48 20 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 46         pInfo->aF
21d49 75 6e 63 2c 0a 20 20 20 20 20 20 20 73 69 7a 65  unc,.       size
21d4a 6f 66 28 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b  of(pInfo->aFunc[
21d4b 30 5d 29 2c 0a 20 20 20 20 20 20 20 33 2c 0a 20  0]),.       3,. 
21d4c 20 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 46        &pInfo->nF
21d4d 75 6e 63 2c 0a 20 20 20 20 20 20 20 26 70 49 6e  unc,.       &pIn
21d4e 66 6f 2d 3e 6e 46 75 6e 63 41 6c 6c 6f 63 2c 0a  fo->nFuncAlloc,.
21d4f 20 20 20 20 20 20 20 26 69 0a 20 20 29 3b 0a 20         &i.  );. 
21d50 20 72 65 74 75 72 6e 20 69 3b 0a 7d 20 20 20 20   return i;.}    
21d51 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ../*.** This is 
21d52 74 68 65 20 78 45 78 70 72 43 61 6c 6c 62 61 63  the xExprCallbac
21d53 6b 20 66 6f 72 20 61 20 74 72 65 65 20 77 61 6c  k for a tree wal
21d54 6b 65 72 2e 20 20 49 74 20 69 73 20 75 73 65 64  ker.  It is used
21d55 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74   to.** implement
21d56 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c   sqlite3ExprAnal
21d57 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 29 2e  yzeAggregates().
21d58 20 20 53 65 65 20 73 71 6c 69 74 65 33 45 78 70    See sqlite3Exp
21d59 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74  rAnalyzeAggregat
21d5a 65 73 0a 2a 2a 20 66 6f 72 20 61 64 64 69 74 69  es.** for additi
21d5b 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
21d5c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
21d5d 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65  analyzeAggregate
21d5e 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72  (Walker *pWalker
21d5f 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a  , Expr *pExpr){.
21d60 20 20 69 6e 74 20 69 3b 0a 20 20 4e 61 6d 65 43    int i;.  NameC
21d61 6f 6e 74 65 78 74 20 2a 70 4e 43 20 3d 20 70 57  ontext *pNC = pW
21d62 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20  alker->u.pNC;.  
21d63 50 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20  Parse *pParse = 
21d64 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 53  pNC->pParse;.  S
21d65 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74  rcList *pSrcList
21d66 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74   = pNC->pSrcList
21d67 3b 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67  ;.  AggInfo *pAg
21d68 67 49 6e 66 6f 20 3d 20 70 4e 43 2d 3e 70 41 67  gInfo = pNC->pAg
21d69 67 49 6e 66 6f 3b 0a 0a 20 20 73 77 69 74 63 68  gInfo;..  switch
21d6a 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20  ( pExpr->op ){. 
21d6b 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43     case TK_AGG_C
21d6c 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 73 65 20  OLUMN:.    case 
21d6d 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20  TK_COLUMN: {.   
21d6e 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78     testcase( pEx
21d6f 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43  pr->op==TK_AGG_C
21d70 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 74  OLUMN );.      t
21d71 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e  estcase( pExpr->
21d72 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b  op==TK_COLUMN );
21d73 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20  .      /* Check 
21d74 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 63 6f  to see if the co
21d75 6c 75 6d 6e 20 69 73 20 69 6e 20 6f 6e 65 20 6f  lumn is in one o
21d76 66 20 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20  f the tables in 
21d77 74 68 65 20 46 52 4f 4d 0a 20 20 20 20 20 20 2a  the FROM.      *
21d78 2a 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20  * clause of the 
21d79 61 67 67 72 65 67 61 74 65 20 71 75 65 72 79 20  aggregate query 
21d7a 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57  */.      if( ALW
21d7b 41 59 53 28 70 53 72 63 4c 69 73 74 21 3d 30 29  AYS(pSrcList!=0)
21d7c 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72 75   ){.        stru
21d7d 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
21d7e 2a 70 49 74 65 6d 20 3d 20 70 53 72 63 4c 69 73  *pItem = pSrcLis
21d7f 74 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 66 6f  t->a;.        fo
21d80 72 28 69 3d 30 3b 20 69 3c 70 53 72 63 4c 69 73  r(i=0; i<pSrcLis
21d81 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49  t->nSrc; i++, pI
21d82 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  tem++){.        
21d83 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f    struct AggInfo
21d84 5f 63 6f 6c 20 2a 70 43 6f 6c 3b 0a 20 20 20 20  _col *pCol;.    
21d85 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45        assert( !E
21d86 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74  xprHasAnyPropert
21d87 79 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65  y(pExpr, EP_Toke
21d88 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65 64  nOnly|EP_Reduced
21d89 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ) );.          i
21d8a 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  f( pExpr->iTable
21d8b 3d 3d 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72  ==pItem->iCursor
21d8c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
21d8d 2f 2a 20 49 66 20 77 65 20 72 65 61 63 68 20 74  /* If we reach t
21d8e 68 69 73 20 70 6f 69 6e 74 2c 20 69 74 20 6d 65  his point, it me
21d8f 61 6e 73 20 74 68 61 74 20 70 45 78 70 72 20 72  ans that pExpr r
21d90 65 66 65 72 73 20 74 6f 20 61 20 74 61 62 6c 65  efers to a table
21d91 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
21d92 74 68 61 74 20 69 73 20 69 6e 20 74 68 65 20 46  that is in the F
21d93 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68  ROM clause of th
21d94 65 20 61 67 67 72 65 67 61 74 65 20 71 75 65 72  e aggregate quer
21d95 79 2e 20 20 0a 20 20 20 20 20 20 20 20 20 20 20  y.  .           
21d96 20 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 20 20   **.            
21d97 2a 2a 20 4d 61 6b 65 20 61 6e 20 65 6e 74 72 79  ** Make an entry
21d98 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20   for the column 
21d99 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f  in pAggInfo->aCo
21d9a 6c 5b 5d 20 69 66 20 74 68 65 72 65 0a 20 20 20  l[] if there.   
21d9b 20 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 6e           ** is n
21d9c 6f 74 20 61 6e 20 65 6e 74 72 79 20 74 68 65 72  ot an entry ther
21d9d 65 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 20  e already..     
21d9e 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
21d9f 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20        int k;.   
21da0 20 20 20 20 20 20 20 20 20 70 43 6f 6c 20 3d 20           pCol = 
21da1 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 3b 0a  pAggInfo->aCol;.
21da2 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28              for(
21da3 6b 3d 30 3b 20 6b 3c 70 41 67 67 49 6e 66 6f 2d  k=0; k<pAggInfo-
21da4 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b 2c 20 70  >nColumn; k++, p
21da5 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  Col++){.        
21da6 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e        if( pCol->
21da7 69 54 61 62 6c 65 3d 3d 70 45 78 70 72 2d 3e 69  iTable==pExpr->i
21da8 54 61 62 6c 65 20 26 26 0a 20 20 20 20 20 20 20  Table &&.       
21da9 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d             pCol-
21daa 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72 2d  >iColumn==pExpr-
21dab 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20  >iColumn ){.    
21dac 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
21dad 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  k;.             
21dae 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d   }.            }
21daf 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
21db0 20 28 6b 3e 3d 70 41 67 67 49 6e 66 6f 2d 3e 6e   (k>=pAggInfo->n
21db1 43 6f 6c 75 6d 6e 29 0a 20 20 20 20 20 20 20 20  Column).        
21db2 20 20 20 20 20 26 26 20 28 6b 20 3d 20 61 64 64       && (k = add
21db3 41 67 67 49 6e 66 6f 43 6f 6c 75 6d 6e 28 70 50  AggInfoColumn(pP
21db4 61 72 73 65 2d 3e 64 62 2c 20 70 41 67 67 49 6e  arse->db, pAggIn
21db5 66 6f 29 29 3e 3d 30 20 0a 20 20 20 20 20 20 20  fo))>=0 .       
21db6 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
21db7 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 26 70 41        pCol = &pA
21db8 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 6b 5d 3b  ggInfo->aCol[k];
21db9 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
21dba 43 6f 6c 2d 3e 70 54 61 62 20 3d 20 70 45 78 70  Col->pTab = pExp
21dbb 72 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 20  r->pTab;.       
21dbc 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 54 61         pCol->iTa
21dbd 62 6c 65 20 3d 20 70 45 78 70 72 2d 3e 69 54 61  ble = pExpr->iTa
21dbe 62 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ble;.           
21dbf 20 20 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e     pCol->iColumn
21dc0 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d   = pExpr->iColum
21dc1 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n;.             
21dc2 20 70 43 6f 6c 2d 3e 69 4d 65 6d 20 3d 20 2b 2b   pCol->iMem = ++
21dc3 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
21dc4 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c              pCol
21dc5 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 20  ->iSorterColumn 
21dc6 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 20  = -1;.          
21dc7 20 20 20 20 70 43 6f 6c 2d 3e 70 45 78 70 72 20      pCol->pExpr 
21dc8 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20  = pExpr;.       
21dc9 20 20 20 20 20 20 20 69 66 28 20 70 41 67 67 49         if( pAggI
21dca 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79 20 29 7b  nfo->pGroupBy ){
21dcb 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
21dcc 20 69 6e 74 20 6a 2c 20 6e 3b 0a 20 20 20 20 20   int j, n;.     
21dcd 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72 4c             ExprL
21dce 69 73 74 20 2a 70 47 42 20 3d 20 70 41 67 67 49  ist *pGB = pAggI
21dcf 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20  nfo->pGroupBy;. 
21dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
21dd1 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
21dd2 74 65 6d 20 2a 70 54 65 72 6d 20 3d 20 70 47 42  tem *pTerm = pGB
21dd3 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ->a;.           
21dd4 20 20 20 20 20 6e 20 3d 20 70 47 42 2d 3e 6e 45       n = pGB->nE
21dd5 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20  xpr;.           
21dd6 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
21dd7 6e 3b 20 6a 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29  n; j++, pTerm++)
21dd8 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
21dd9 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70      Expr *pE = p
21dda 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20  Term->pExpr;.   
21ddb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
21ddc 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  f( pE->op==TK_CO
21ddd 4c 55 4d 4e 20 26 26 20 70 45 2d 3e 69 54 61 62  LUMN && pE->iTab
21dde 6c 65 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c  le==pExpr->iTabl
21ddf 65 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20  e &&.           
21de0 20 20 20 20 20 20 20 20 20 20 20 70 45 2d 3e 69             pE->i
21de1 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72 2d 3e 69  Column==pExpr->i
21de2 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20  Column ){.      
21de3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43                pC
21de4 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d  ol->iSorterColum
21de5 6e 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20  n = j;.         
21de6 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
21de7 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
21de8 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
21de9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
21dea 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
21deb 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e        if( pCol->
21dec 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3c 30 20  iSorterColumn<0 
21ded 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
21dee 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72     pCol->iSorter
21def 43 6f 6c 75 6d 6e 20 3d 20 70 41 67 67 49 6e 66  Column = pAggInf
21df0 6f 2d 3e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d  o->nSortingColum
21df1 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  n++;.           
21df2 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
21df3 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f   }.            /
21df4 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 77 20 61  * There is now a
21df5 6e 20 65 6e 74 72 79 20 66 6f 72 20 70 45 78 70  n entry for pExp
21df6 72 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61  r in pAggInfo->a
21df7 43 6f 6c 5b 5d 20 28 65 69 74 68 65 72 0a 20 20  Col[] (either.  
21df8 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 63            ** bec
21df9 61 75 73 65 20 69 74 20 77 61 73 20 74 68 65 72  ause it was ther
21dfa 65 20 62 65 66 6f 72 65 20 6f 72 20 62 65 63 61  e before or beca
21dfb 75 73 65 20 77 65 20 6a 75 73 74 20 63 72 65 61  use we just crea
21dfc 74 65 64 20 69 74 29 2e 0a 20 20 20 20 20 20 20  ted it)..       
21dfd 20 20 20 20 20 2a 2a 20 43 6f 6e 76 65 72 74 20       ** Convert 
21dfe 74 68 65 20 70 45 78 70 72 20 74 6f 20 62 65 20  the pExpr to be 
21dff 61 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20  a TK_AGG_COLUMN 
21e00 72 65 66 65 72 72 69 6e 67 20 74 6f 20 74 68 61  referring to tha
21e01 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  t.            **
21e02 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b   pAggInfo->aCol[
21e03 5d 20 65 6e 74 72 79 2e 0a 20 20 20 20 20 20 20  ] entry..       
21e04 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
21e05 20 20 20 20 45 78 70 72 53 65 74 49 72 72 65 64      ExprSetIrred
21e06 75 63 69 62 6c 65 28 70 45 78 70 72 29 3b 0a 20  ucible(pExpr);. 
21e07 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
21e08 2d 3e 70 41 67 67 49 6e 66 6f 20 3d 20 70 41 67  ->pAggInfo = pAg
21e09 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 20  gInfo;.         
21e0a 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54     pExpr->op = T
21e0b 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3b 0a 20 20  K_AGG_COLUMN;.  
21e0c 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
21e0d 3e 69 41 67 67 20 3d 20 28 69 31 36 29 6b 3b 0a  >iAgg = (i16)k;.
21e0e 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
21e0f 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 20 2f  k;.          } /
21e10 2a 20 65 6e 64 69 66 20 70 45 78 70 72 2d 3e 69  * endif pExpr->i
21e11 54 61 62 6c 65 3d 3d 70 49 74 65 6d 2d 3e 69 43  Table==pItem->iC
21e12 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20  ursor */.       
21e13 20 7d 20 2f 2a 20 65 6e 64 20 6c 6f 6f 70 20 6f   } /* end loop o
21e14 76 65 72 20 70 53 72 63 4c 69 73 74 20 2a 2f 0a  ver pSrcList */.
21e15 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
21e16 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a  turn WRC_Prune;.
21e17 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
21e18 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 20  K_AGG_FUNCTION: 
21e19 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70  {.      /* The p
21e1a 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d 30 20 74 65  NC->nDepth==0 te
21e1b 73 74 20 63 61 75 73 65 73 20 61 67 67 72 65 67  st causes aggreg
21e1c 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e  ate functions in
21e1d 20 73 75 62 71 75 65 72 69 65 73 0a 20 20 20 20   subqueries.    
21e1e 20 20 2a 2a 20 74 6f 20 62 65 20 69 67 6e 6f 72    ** to be ignor
21e1f 65 64 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  ed */.      if( 
21e20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d 30 20 29  pNC->nDepth==0 )
21e21 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65  {.        /* Che
21e22 63 6b 20 74 6f 20 73 65 65 20 69 66 20 70 45 78  ck to see if pEx
21e23 70 72 20 69 73 20 61 20 64 75 70 6c 69 63 61 74  pr is a duplicat
21e24 65 20 6f 66 20 61 6e 6f 74 68 65 72 20 61 67 67  e of another agg
21e25 72 65 67 61 74 65 20 0a 20 20 20 20 20 20 20 20  regate .        
21e26 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74  ** function that
21e27 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74   is already in t
21e28 68 65 20 70 41 67 67 49 6e 66 6f 20 73 74 72 75  he pAggInfo stru
21e29 63 74 75 72 65 0a 20 20 20 20 20 20 20 20 2a 2f  cture.        */
21e2a 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20  .        struct 
21e2b 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 2a 70 49  AggInfo_func *pI
21e2c 74 65 6d 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e  tem = pAggInfo->
21e2d 61 46 75 6e 63 3b 0a 20 20 20 20 20 20 20 20 66  aFunc;.        f
21e2e 6f 72 28 69 3d 30 3b 20 69 3c 70 41 67 67 49 6e  or(i=0; i<pAggIn
21e2f 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20  fo->nFunc; i++, 
21e30 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pItem++){.      
21e31 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45      if( sqlite3E
21e32 78 70 72 43 6f 6d 70 61 72 65 28 70 49 74 65 6d  xprCompare(pItem
21e33 2d 3e 70 45 78 70 72 2c 20 70 45 78 70 72 29 20  ->pExpr, pExpr) 
21e34 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  ){.            b
21e35 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
21e36 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
21e37 20 20 20 20 69 66 28 20 69 3e 3d 70 41 67 67 49      if( i>=pAggI
21e38 6e 66 6f 2d 3e 6e 46 75 6e 63 20 29 7b 0a 20 20  nfo->nFunc ){.  
21e39 20 20 20 20 20 20 20 20 2f 2a 20 70 45 78 70 72          /* pExpr
21e3a 20 69 73 20 6f 72 69 67 69 6e 61 6c 2e 20 20 4d   is original.  M
21e3b 61 6b 65 20 61 20 6e 65 77 20 65 6e 74 72 79 20  ake a new entry 
21e3c 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75  in pAggInfo->aFu
21e3d 6e 63 5b 5d 0a 20 20 20 20 20 20 20 20 20 20 2a  nc[].          *
21e3e 2f 0a 20 20 20 20 20 20 20 20 20 20 75 38 20 65  /.          u8 e
21e3f 6e 63 20 3d 20 45 4e 43 28 70 50 61 72 73 65 2d  nc = ENC(pParse-
21e40 3e 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20  >db);.          
21e41 69 20 3d 20 61 64 64 41 67 67 49 6e 66 6f 46 75  i = addAggInfoFu
21e42 6e 63 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  nc(pParse->db, p
21e43 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  AggInfo);.      
21e44 20 20 20 20 69 66 28 20 69 3e 3d 30 20 29 7b 0a      if( i>=0 ){.
21e45 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65              asse
21e46 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
21e47 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78  erty(pExpr, EP_x
21e48 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20  IsSelect) );.   
21e49 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 20 3d           pItem =
21e4a 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e   &pAggInfo->aFun
21e4b 63 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  c[i];.          
21e4c 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d    pItem->pExpr =
21e4d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20   pExpr;.        
21e4e 20 20 20 20 70 49 74 65 6d 2d 3e 69 4d 65 6d 20      pItem->iMem 
21e4f 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
21e50 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73  ;.            as
21e51 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72  sert( !ExprHasPr
21e52 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
21e53 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20  _IntValue) );.  
21e54 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d            pItem-
21e55 3e 70 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33  >pFunc = sqlite3
21e56 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61  FindFunction(pPa
21e57 72 73 65 2d 3e 64 62 2c 0a 20 20 20 20 20 20 20  rse->db,.       
21e58 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70              pExp
21e59 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 73 71 6c  r->u.zToken, sql
21e5a 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 45 78  ite3Strlen30(pEx
21e5b 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 2c 0a 20  pr->u.zToken),. 
21e5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21e5d 20 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74    pExpr->x.pList
21e5e 20 3f 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73   ? pExpr->x.pLis
21e5f 74 2d 3e 6e 45 78 70 72 20 3a 20 30 2c 20 65 6e  t->nExpr : 0, en
21e60 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  c, 0);.         
21e61 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 66 6c     if( pExpr->fl
21e62 61 67 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63  ags & EP_Distinc
21e63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  t ){.           
21e64 20 20 20 70 49 74 65 6d 2d 3e 69 44 69 73 74 69     pItem->iDisti
21e65 6e 63 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  nct = pParse->nT
21e66 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  ab++;.          
21e67 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
21e68 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 44         pItem->iD
21e69 69 73 74 69 6e 63 74 20 3d 20 2d 31 3b 0a 20 20  istinct = -1;.  
21e6a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
21e6b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
21e6c 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b  }.        /* Mak
21e6d 65 20 70 45 78 70 72 20 70 6f 69 6e 74 20 74 6f  e pExpr point to
21e6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
21e6f 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63   pAggInfo->aFunc
21e70 5b 5d 20 65 6e 74 72 79 0a 20 20 20 20 20 20 20  [] entry.       
21e71 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65   */.        asse
21e72 72 74 28 20 21 45 78 70 72 48 61 73 41 6e 79 50  rt( !ExprHasAnyP
21e73 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
21e74 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52  P_TokenOnly|EP_R
21e75 65 64 75 63 65 64 29 20 29 3b 0a 20 20 20 20 20  educed) );.     
21e76 20 20 20 45 78 70 72 53 65 74 49 72 72 65 64 75     ExprSetIrredu
21e77 63 69 62 6c 65 28 70 45 78 70 72 29 3b 0a 20 20  cible(pExpr);.  
21e78 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 41 67        pExpr->iAg
21e79 67 20 3d 20 28 69 31 36 29 69 3b 0a 20 20 20 20  g = (i16)i;.    
21e7a 20 20 20 20 70 45 78 70 72 2d 3e 70 41 67 67 49      pExpr->pAggI
21e7b 6e 66 6f 20 3d 20 70 41 67 67 49 6e 66 6f 3b 0a  nfo = pAggInfo;.
21e7c 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 57          return W
21e7d 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 20 20  RC_Prune;.      
21e7e 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
21e7f 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75  turn WRC_Continu
21e80 65 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  e;.}.static int 
21e81 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65  analyzeAggregate
21e82 73 49 6e 53 65 6c 65 63 74 28 57 61 6c 6b 65 72  sInSelect(Walker
21e83 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63   *pWalker, Selec
21e84 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a 20 20 4e  t *pSelect){.  N
21e85 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 20  ameContext *pNC 
21e86 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43  = pWalker->u.pNC
21e87 3b 0a 20 20 69 66 28 20 70 4e 43 2d 3e 6e 44 65  ;.  if( pNC->nDe
21e88 70 74 68 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e  pth==0 ){.    pN
21e89 43 2d 3e 6e 44 65 70 74 68 2b 2b 3b 0a 20 20 20  C->nDepth++;.   
21e8a 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65   sqlite3WalkSele
21e8b 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 53 65 6c  ct(pWalker, pSel
21e8c 65 63 74 29 3b 0a 20 20 20 20 70 4e 43 2d 3e 6e  ect);.    pNC->n
21e8d 44 65 70 74 68 2d 2d 3b 0a 20 20 20 20 72 65 74  Depth--;.    ret
21e8e 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20  urn WRC_Prune;. 
21e8f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
21e90 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b  rn WRC_Continue;
21e91 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e  .  }.}../*.** An
21e92 61 6c 79 7a 65 20 74 68 65 20 67 69 76 65 6e 20  alyze the given 
21e93 65 78 70 72 65 73 73 69 6f 6e 20 6c 6f 6f 6b 69  expression looki
21e94 6e 67 20 66 6f 72 20 61 67 67 72 65 67 61 74 65  ng for aggregate
21e95 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 0a 2a   functions and.*
21e96 2a 20 66 6f 72 20 76 61 72 69 61 62 6c 65 73 20  * for variables 
21e97 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20  that need to be 
21e98 61 64 64 65 64 20 74 6f 20 74 68 65 20 70 50 61  added to the pPa
21e99 72 73 65 2d 3e 61 41 67 67 5b 5d 20 61 72 72 61  rse->aAgg[] arra
21e9a 79 2e 0a 2a 2a 20 4d 61 6b 65 20 61 64 64 69 74  y..** Make addit
21e9b 69 6f 6e 61 6c 20 65 6e 74 72 69 65 73 20 74 6f  ional entries to
21e9c 20 74 68 65 20 70 50 61 72 73 65 2d 3e 61 41 67   the pParse->aAg
21e9d 67 5b 5d 20 61 72 72 61 79 20 61 73 20 6e 65 63  g[] array as nec
21e9e 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  essary..**.** Th
21e9f 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c  is routine shoul
21ea0 64 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64  d only be called
21ea1 20 61 66 74 65 72 20 74 68 65 20 65 78 70 72 65   after the expre
21ea2 73 73 69 6f 6e 20 68 61 73 20 62 65 65 6e 0a 2a  ssion has been.*
21ea3 2a 20 61 6e 61 6c 79 7a 65 64 20 62 79 20 73 71  * analyzed by sq
21ea4 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
21ea5 4e 61 6d 65 73 28 29 2e 0a 2a 2f 0a 53 51 4c 49  Names()..*/.SQLI
21ea6 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
21ea7 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79  sqlite3ExprAnaly
21ea8 7a 65 41 67 67 72 65 67 61 74 65 73 28 4e 61 6d  zeAggregates(Nam
21ea9 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 45  eContext *pNC, E
21eaa 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 57  xpr *pExpr){.  W
21eab 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 78 45 78  alker w;.  w.xEx
21eac 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 61 6e 61  prCallback = ana
21ead 6c 79 7a 65 41 67 67 72 65 67 61 74 65 3b 0a 20  lyzeAggregate;. 
21eae 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61   w.xSelectCallba
21eaf 63 6b 20 3d 20 61 6e 61 6c 79 7a 65 41 67 67 72  ck = analyzeAggr
21eb0 65 67 61 74 65 73 49 6e 53 65 6c 65 63 74 3b 0a  egatesInSelect;.
21eb1 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4e 43 3b    w.u.pNC = pNC;
21eb2 0a 20 20 61 73 73 65 72 74 28 20 70 4e 43 2d 3e  .  assert( pNC->
21eb3 70 53 72 63 4c 69 73 74 21 3d 30 20 29 3b 0a 20  pSrcList!=0 );. 
21eb4 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72   sqlite3WalkExpr
21eb5 28 26 77 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a  (&w, pExpr);.}..
21eb6 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74  /*.** Call sqlit
21eb7 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
21eb8 72 65 67 61 74 65 73 28 29 20 66 6f 72 20 65 76  regates() for ev
21eb9 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69  ery expression i
21eba 6e 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69  n an.** expressi
21ebb 6f 6e 20 6c 69 73 74 2e 20 20 52 65 74 75 72 6e  on list.  Return
21ebc 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
21ebd 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  rrors..**.** If 
21ebe 61 6e 20 65 72 72 6f 72 20 69 73 20 66 6f 75 6e  an error is foun
21ebf 64 2c 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20  d, the analysis 
21ec0 69 73 20 63 75 74 20 73 68 6f 72 74 2e 0a 2a 2f  is cut short..*/
21ec1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
21ec2 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
21ec3 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 4e  AnalyzeAggList(N
21ec4 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c  ameContext *pNC,
21ec5 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
21ec6 29 7b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72  ){.  struct Expr
21ec7 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
21ec8 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28  ;.  int i;.  if(
21ec9 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 66 6f   pList ){.    fo
21eca 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61  r(pItem=pList->a
21ecb 2c 20 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e  , i=0; i<pList->
21ecc 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65  nExpr; i++, pIte
21ecd 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  m++){.      sqli
21ece 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67  te3ExprAnalyzeAg
21ecf 67 72 65 67 61 74 65 73 28 70 4e 43 2c 20 70 49  gregates(pNC, pI
21ed0 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20  tem->pExpr);.   
21ed1 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
21ed2 41 6c 6c 6f 63 61 74 65 20 61 20 73 69 6e 67 6c  Allocate a singl
21ed3 65 20 6e 65 77 20 72 65 67 69 73 74 65 72 20 66  e new register f
21ed4 6f 72 20 75 73 65 20 74 6f 20 68 6f 6c 64 20 73  or use to hold s
21ed5 6f 6d 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65  ome intermediate
21ed6 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a 53 51 4c 49   result..*/.SQLI
21ed7 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
21ed8 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
21ed9 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b  (Parse *pParse){
21eda 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
21edb 54 65 6d 70 52 65 67 3d 3d 30 20 29 7b 0a 20 20  TempReg==0 ){.  
21edc 20 20 72 65 74 75 72 6e 20 2b 2b 70 50 61 72 73    return ++pPars
21edd 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72  e->nMem;.  }.  r
21ede 65 74 75 72 6e 20 70 50 61 72 73 65 2d 3e 61 54  eturn pParse->aT
21edf 65 6d 70 52 65 67 5b 2d 2d 70 50 61 72 73 65 2d  empReg[--pParse-
21ee0 3e 6e 54 65 6d 70 52 65 67 5d 3b 0a 7d 0a 0a 2f  >nTempReg];.}../
21ee1 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20  *.** Deallocate 
21ee2 61 20 72 65 67 69 73 74 65 72 2c 20 6d 61 6b 69  a register, maki
21ee3 6e 67 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72  ng available for
21ee4 20 72 65 75 73 65 20 66 6f 72 20 73 6f 6d 65 20   reuse for some 
21ee5 6f 74 68 65 72 0a 2a 2a 20 70 75 72 70 6f 73 65  other.** purpose
21ee6 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 72 65 67  ..**.** If a reg
21ee7 69 73 74 65 72 20 69 73 20 63 75 72 72 65 6e 74  ister is current
21ee8 6c 79 20 62 65 69 6e 67 20 75 73 65 64 20 62 79  ly being used by
21ee9 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68   the column cach
21eea 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 64  e, then.** the d
21eeb 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 64 65  allocation is de
21eec 66 65 72 72 65 64 20 75 6e 74 69 6c 20 74 68 65  ferred until the
21eed 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 6c 69   column cache li
21eee 6e 65 20 74 68 61 74 20 75 73 65 73 0a 2a 2a 20  ne that uses.** 
21eef 74 68 65 20 72 65 67 69 73 74 65 72 20 62 65 63  the register bec
21ef0 6f 6d 65 73 20 73 74 61 6c 65 2e 0a 2a 2f 0a 53  omes stale..*/.S
21ef1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
21ef2 69 64 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  id sqlite3Releas
21ef3 65 54 65 6d 70 52 65 67 28 50 61 72 73 65 20 2a  eTempReg(Parse *
21ef4 70 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67  pParse, int iReg
21ef5 29 7b 0a 20 20 69 66 28 20 69 52 65 67 20 26 26  ){.  if( iReg &&
21ef6 20 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65   pParse->nTempRe
21ef7 67 3c 41 72 72 61 79 53 69 7a 65 28 70 50 61 72  g<ArraySize(pPar
21ef8 73 65 2d 3e 61 54 65 6d 70 52 65 67 29 20 29 7b  se->aTempReg) ){
21ef9 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
21efa 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65  struct yColCache
21efb 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30   *p;.    for(i=0
21efc 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c  , p=pParse->aCol
21efd 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f  Cache; i<SQLITE_
21efe 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c  N_COLCACHE; i++,
21eff 20 70 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   p++){.      if(
21f00 20 70 2d 3e 69 52 65 67 3d 3d 69 52 65 67 20 29   p->iReg==iReg )
21f01 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 74 65 6d  {.        p->tem
21f02 70 52 65 67 20 3d 20 31 3b 0a 20 20 20 20 20 20  pReg = 1;.      
21f03 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
21f04 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72  }.    }.    pPar
21f05 73 65 2d 3e 61 54 65 6d 70 52 65 67 5b 70 50 61  se->aTempReg[pPa
21f06 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 2b 2b 5d  rse->nTempReg++]
21f07 20 3d 20 69 52 65 67 3b 0a 20 20 7d 0a 7d 0a 0a   = iReg;.  }.}..
21f08 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f  /*.** Allocate o
21f09 72 20 64 65 61 6c 6c 6f 63 61 74 65 20 61 20 62  r deallocate a b
21f0a 6c 6f 63 6b 20 6f 66 20 6e 52 65 67 20 63 6f 6e  lock of nReg con
21f0b 73 65 63 75 74 69 76 65 20 72 65 67 69 73 74 65  secutive registe
21f0c 72 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  rs.*/.SQLITE_PRI
21f0d 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
21f0e 47 65 74 54 65 6d 70 52 61 6e 67 65 28 50 61 72  GetTempRange(Par
21f0f 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
21f10 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 2c 20  nReg){.  int i, 
21f11 6e 3b 0a 20 20 69 20 3d 20 70 50 61 72 73 65 2d  n;.  i = pParse-
21f12 3e 69 52 61 6e 67 65 52 65 67 3b 0a 20 20 6e 20  >iRangeReg;.  n 
21f13 3d 20 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65  = pParse->nRange
21f14 52 65 67 3b 0a 20 20 69 66 28 20 6e 52 65 67 3c  Reg;.  if( nReg<
21f15 3d 6e 20 26 26 20 21 75 73 65 64 41 73 43 6f 6c  =n && !usedAsCol
21f16 75 6d 6e 43 61 63 68 65 28 70 50 61 72 73 65 2c  umnCache(pParse,
21f17 20 69 2c 20 69 2b 6e 2d 31 29 20 29 7b 0a 20 20   i, i+n-1) ){.  
21f18 20 20 70 50 61 72 73 65 2d 3e 69 52 61 6e 67 65    pParse->iRange
21f19 52 65 67 20 2b 3d 20 6e 52 65 67 3b 0a 20 20 20  Reg += nReg;.   
21f1a 20 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52   pParse->nRangeR
21f1b 65 67 20 2d 3d 20 6e 52 65 67 3b 0a 20 20 7d 65  eg -= nReg;.  }e
21f1c 6c 73 65 7b 0a 20 20 20 20 69 20 3d 20 70 50 61  lse{.    i = pPa
21f1d 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 20  rse->nMem+1;.   
21f1e 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d   pParse->nMem +=
21f1f 20 6e 52 65 67 3b 0a 20 20 7d 0a 20 20 72 65 74   nReg;.  }.  ret
21f20 75 72 6e 20 69 3b 0a 7d 0a 53 51 4c 49 54 45 5f  urn i;.}.SQLITE_
21f21 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
21f22 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
21f23 61 6e 67 65 28 50 61 72 73 65 20 2a 70 50 61 72  ange(Parse *pPar
21f24 73 65 2c 20 69 6e 74 20 69 52 65 67 2c 20 69 6e  se, int iReg, in
21f25 74 20 6e 52 65 67 29 7b 0a 20 20 69 66 28 20 6e  t nReg){.  if( n
21f26 52 65 67 3e 70 50 61 72 73 65 2d 3e 6e 52 61 6e  Reg>pParse->nRan
21f27 67 65 52 65 67 20 29 7b 0a 20 20 20 20 70 50 61  geReg ){.    pPa
21f28 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65 67 20 3d  rse->nRangeReg =
21f29 20 6e 52 65 67 3b 0a 20 20 20 20 70 50 61 72 73   nReg;.    pPars
21f2a 65 2d 3e 69 52 61 6e 67 65 52 65 67 20 3d 20 69  e->iRangeReg = i
21f2b 52 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a  Reg;.  }.}../***
21f2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
21f2d 6f 66 20 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a  of expr.c ******
21f2e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21f2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
21f31 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
21f32 6e 20 66 69 6c 65 20 61 6c 74 65 72 2e 63 20 2a  n file alter.c *
21f33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21f34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21f35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
21f36 2a 20 32 30 30 35 20 46 65 62 72 75 61 72 79 20  * 2005 February 
21f37 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
21f38 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
21f39 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
21f3a 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
21f3b 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
21f3c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
21f3d 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
21f3e 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
21f3f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
21f40 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
21f41 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
21f42 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
21f43 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
21f44 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
21f45 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
21f46 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
21f47 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
21f48 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
21f49 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21f4a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21f4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21f4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21f4d 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
21f4e 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72  ontains C code r
21f4f 6f 75 74 69 6e 65 73 20 74 68 61 74 20 75 73 65  outines that use
21f50 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 56 44  d to generate VD
21f51 42 45 20 63 6f 64 65 0a 2a 2a 20 74 68 61 74 20  BE code.** that 
21f52 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 41  implements the A
21f53 4c 54 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61  LTER TABLE comma
21f54 6e 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 61  nd..**.** $Id: a
21f55 6c 74 65 72 2e 63 2c 76 20 31 2e 36 32 20 32 30  lter.c,v 1.62 20
21f56 30 39 2f 30 37 2f 32 34 20 31 37 3a 35 38 3a 35  09/07/24 17:58:5
21f57 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78  3 danielk1977 Ex
21f58 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  p $.*/../*.** Th
21f59 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66  e code in this f
21f5a 69 6c 65 20 6f 6e 6c 79 20 65 78 69 73 74 73 20  ile only exists 
21f5b 69 66 20 77 65 20 61 72 65 20 6e 6f 74 20 6f 6d  if we are not om
21f5c 69 74 74 69 6e 67 20 74 68 65 0a 2a 2a 20 41 4c  itting the.** AL
21f5d 54 45 52 20 54 41 42 4c 45 20 6c 6f 67 69 63 20  TER TABLE logic 
21f5e 66 72 6f 6d 20 74 68 65 20 62 75 69 6c 64 2e 0a  from the build..
21f5f 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
21f60 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c  E_OMIT_ALTERTABL
21f61 45 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  E.../*.** This f
21f62 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
21f63 62 79 20 53 51 4c 20 67 65 6e 65 72 61 74 65 64  by SQL generated
21f64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
21f65 65 20 0a 2a 2a 20 41 4c 54 45 52 20 54 41 42 4c  e .** ALTER TABL
21f66 45 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 20 66  E command. The f
21f67 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73  irst argument is
21f68 20 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 43   the text of a C
21f69 52 45 41 54 45 20 54 41 42 4c 45 20 6f 72 0a 2a  REATE TABLE or.*
21f6a 2a 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 63  * CREATE INDEX c
21f6b 6f 6d 6d 61 6e 64 2e 20 54 68 65 20 73 65 63 6f  ommand. The seco
21f6c 6e 64 20 69 73 20 61 20 74 61 62 6c 65 20 6e 61  nd is a table na
21f6d 6d 65 2e 20 54 68 65 20 74 61 62 6c 65 20 6e 61  me. The table na
21f6e 6d 65 20 69 6e 20 0a 2a 2a 20 74 68 65 20 43 52  me in .** the CR
21f6f 45 41 54 45 20 54 41 42 4c 45 20 6f 72 20 43 52  EATE TABLE or CR
21f70 45 41 54 45 20 49 4e 44 45 58 20 73 74 61 74 65  EATE INDEX state
21f71 6d 65 6e 74 20 69 73 20 72 65 70 6c 61 63 65 64  ment is replaced
21f72 20 77 69 74 68 20 74 68 65 20 74 68 69 72 64 0a   with the third.
21f73 2a 2a 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 20  ** argument and 
21f74 74 68 65 20 72 65 73 75 6c 74 20 72 65 74 75 72  the result retur
21f75 6e 65 64 2e 20 45 78 61 6d 70 6c 65 73 3a 0a 2a  ned. Examples:.*
21f76 2a 0a 2a 2a 20 73 71 6c 69 74 65 5f 72 65 6e 61  *.** sqlite_rena
21f77 6d 65 5f 74 61 62 6c 65 28 27 43 52 45 41 54 45  me_table('CREATE
21f78 20 54 41 42 4c 45 20 61 62 63 28 61 2c 20 62 2c   TABLE abc(a, b,
21f79 20 63 29 27 2c 20 27 64 65 66 27 29 0a 2a 2a 20   c)', 'def').** 
21f7a 20 20 20 20 2d 3e 20 27 43 52 45 41 54 45 20 54      -> 'CREATE T
21f7b 41 42 4c 45 20 64 65 66 28 61 2c 20 62 2c 20 63  ABLE def(a, b, c
21f7c 29 27 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 5f  )'.**.** sqlite_
21f7d 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 27 43 52  rename_table('CR
21f7e 45 41 54 45 20 49 4e 44 45 58 20 69 20 4f 4e 20  EATE INDEX i ON 
21f7f 61 62 63 28 61 29 27 2c 20 27 64 65 66 27 29 0a  abc(a)', 'def').
21f80 2a 2a 20 20 20 20 20 2d 3e 20 27 43 52 45 41 54  **     -> 'CREAT
21f81 45 20 49 4e 44 45 58 20 69 20 4f 4e 20 64 65 66  E INDEX i ON def
21f82 28 61 2c 20 62 2c 20 63 29 27 0a 2a 2f 0a 73 74  (a, b, c)'.*/.st
21f83 61 74 69 63 20 76 6f 69 64 20 72 65 6e 61 6d 65  atic void rename
21f84 54 61 62 6c 65 46 75 6e 63 28 0a 20 20 73 71 6c  TableFunc(.  sql
21f85 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
21f86 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74  ntext,.  int Not
21f87 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f  Used,.  sqlite3_
21f88 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
21f89 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
21f8a 63 6f 6e 73 74 20 2a 7a 53 71 6c 20 3d 20 73 71  const *zSql = sq
21f8b 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
21f8c 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 75 6e 73  (argv[0]);.  uns
21f8d 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74  igned char const
21f8e 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 20 3d 20 73   *zTableName = s
21f8f 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
21f90 74 28 61 72 67 76 5b 31 5d 29 3b 0a 0a 20 20 69  t(argv[1]);..  i
21f91 6e 74 20 74 6f 6b 65 6e 3b 0a 20 20 54 6f 6b 65  nt token;.  Toke
21f92 6e 20 74 6e 61 6d 65 3b 0a 20 20 75 6e 73 69 67  n tname;.  unsig
21f93 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ned char const *
21f94 7a 43 73 72 20 3d 20 7a 53 71 6c 3b 0a 20 20 69  zCsr = zSql;.  i
21f95 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 63 68  nt len = 0;.  ch
21f96 61 72 20 2a 7a 52 65 74 3b 0a 0a 20 20 73 71 6c  ar *zRet;..  sql
21f97 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74  ite3 *db = sqlit
21f98 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
21f99 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 0a  ndle(context);..
21f9a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
21f9b 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20  ER(NotUsed);..  
21f9c 2f 2a 20 54 68 65 20 70 72 69 6e 63 69 70 6c 65  /* The principle
21f9d 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 20   used to locate 
21f9e 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69  the table name i
21f9f 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42  n the CREATE TAB
21fa0 4c 45 20 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65  LE .  ** stateme
21fa1 6e 74 20 69 73 20 74 68 61 74 20 74 68 65 20 74  nt is that the t
21fa2 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 74 68 65  able name is the
21fa3 20 66 69 72 73 74 20 6e 6f 6e 2d 73 70 61 63 65   first non-space
21fa4 20 74 6f 6b 65 6e 20 74 68 61 74 0a 20 20 2a 2a   token that.  **
21fa5 20 69 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20   is immediately 
21fa6 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 54 4b  followed by a TK
21fa7 5f 4c 50 20 6f 72 20 54 4b 5f 55 53 49 4e 47 20  _LP or TK_USING 
21fa8 74 6f 6b 65 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66  token..  */.  if
21fa9 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20 64 6f  ( zSql ){.    do
21faa 20 7b 0a 20 20 20 20 20 20 69 66 28 20 21 2a 7a   {.      if( !*z
21fab 43 73 72 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  Csr ){.        /
21fac 2a 20 52 61 6e 20 6f 75 74 20 6f 66 20 69 6e 70  * Ran out of inp
21fad 75 74 20 62 65 66 6f 72 65 20 66 69 6e 64 69 6e  ut before findin
21fae 67 20 61 6e 20 6f 70 65 6e 69 6e 67 20 62 72 61  g an opening bra
21faf 63 6b 65 74 2e 20 52 65 74 75 72 6e 20 4e 55 4c  cket. Return NUL
21fb0 4c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65  L. */.        re
21fb1 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  turn;.      }.. 
21fb2 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68       /* Store th
21fb3 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 7a 43 73  e token that zCs
21fb4 72 20 70 6f 69 6e 74 73 20 74 6f 20 69 6e 20 74  r points to in t
21fb5 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 74  name. */.      t
21fb6 6e 61 6d 65 2e 7a 20 3d 20 28 63 68 61 72 2a 29  name.z = (char*)
21fb7 7a 43 73 72 3b 0a 20 20 20 20 20 20 74 6e 61 6d  zCsr;.      tnam
21fb8 65 2e 6e 20 3d 20 6c 65 6e 3b 0a 0a 20 20 20 20  e.n = len;..    
21fb9 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 7a 43 73    /* Advance zCs
21fba 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74 6f  r to the next to
21fbb 6b 65 6e 2e 20 53 74 6f 72 65 20 74 68 61 74 20  ken. Store that 
21fbc 74 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20 27 74  token type in 't
21fbd 6f 6b 65 6e 27 2c 0a 20 20 20 20 20 20 2a 2a 20  oken',.      ** 
21fbe 61 6e 64 20 69 74 73 20 6c 65 6e 67 74 68 20 69  and its length i
21fbf 6e 20 27 6c 65 6e 27 20 28 74 6f 20 62 65 20 75  n 'len' (to be u
21fc0 73 65 64 20 6e 65 78 74 20 69 74 65 72 61 74 69  sed next iterati
21fc1 6f 6e 20 6f 66 20 74 68 69 73 20 6c 6f 6f 70 29  on of this loop)
21fc2 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
21fc3 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 7a 43   do {.        zC
21fc4 73 72 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20  sr += len;.     
21fc5 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33     len = sqlite3
21fc6 47 65 74 54 6f 6b 65 6e 28 7a 43 73 72 2c 20 26  GetToken(zCsr, &
21fc7 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d 20  token);.      } 
21fc8 77 68 69 6c 65 28 20 74 6f 6b 65 6e 3d 3d 54 4b  while( token==TK
21fc9 5f 53 50 41 43 45 20 29 3b 0a 20 20 20 20 20 20  _SPACE );.      
21fca 61 73 73 65 72 74 28 20 6c 65 6e 3e 30 20 29 3b  assert( len>0 );
21fcb 0a 20 20 20 20 7d 20 77 68 69 6c 65 28 20 74 6f  .    } while( to
21fcc 6b 65 6e 21 3d 54 4b 5f 4c 50 20 26 26 20 74 6f  ken!=TK_LP && to
21fcd 6b 65 6e 21 3d 54 4b 5f 55 53 49 4e 47 20 29 3b  ken!=TK_USING );
21fce 0a 0a 20 20 20 20 7a 52 65 74 20 3d 20 73 71 6c  ..    zRet = sql
21fcf 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
21fd0 22 25 2e 2a 73 5c 22 25 77 5c 22 25 73 22 2c 20  "%.*s\"%w\"%s", 
21fd1 28 28 75 38 2a 29 74 6e 61 6d 65 2e 7a 29 20 2d  ((u8*)tname.z) -
21fd2 20 7a 53 71 6c 2c 20 7a 53 71 6c 2c 20 0a 20 20   zSql, zSql, .  
21fd3 20 20 20 20 20 7a 54 61 62 6c 65 4e 61 6d 65 2c       zTableName,
21fd4 20 74 6e 61 6d 65 2e 7a 2b 74 6e 61 6d 65 2e 6e   tname.z+tname.n
21fd5 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
21fd6 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
21fd7 78 74 2c 20 7a 52 65 74 2c 20 2d 31 2c 20 53 51  xt, zRet, -1, SQ
21fd8 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  LITE_DYNAMIC);. 
21fd9 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51   }.}..#ifndef SQ
21fda 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
21fdb 52 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69  R./* This functi
21fdc 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 53 51  on is used by SQ
21fdd 4c 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 69  L generated to i
21fde 6d 70 6c 65 6d 65 6e 74 20 74 68 65 0a 2a 2a 20  mplement the.** 
21fdf 41 4c 54 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d  ALTER TABLE comm
21fe0 61 6e 64 2e 20 54 68 65 20 66 69 72 73 74 20 61  and. The first a
21fe1 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 74  rgument is the t
21fe2 65 78 74 20 6f 66 20 61 20 43 52 45 41 54 45 20  ext of a CREATE 
21fe3 54 52 49 47 47 45 52 20 0a 2a 2a 20 73 74 61 74  TRIGGER .** stat
21fe4 65 6d 65 6e 74 2e 20 54 68 65 20 73 65 63 6f 6e  ement. The secon
21fe5 64 20 69 73 20 61 20 74 61 62 6c 65 20 6e 61 6d  d is a table nam
21fe6 65 2e 20 54 68 65 20 74 61 62 6c 65 20 6e 61 6d  e. The table nam
21fe7 65 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20  e in the CREATE 
21fe8 0a 2a 2a 20 54 52 49 47 47 45 52 20 73 74 61 74  .** TRIGGER stat
21fe9 65 6d 65 6e 74 20 69 73 20 72 65 70 6c 61 63 65  ement is replace
21fea 64 20 77 69 74 68 20 74 68 65 20 74 68 69 72 64  d with the third
21feb 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 74 68   argument and th
21fec 65 20 72 65 73 75 6c 74 20 0a 2a 2a 20 72 65 74  e result .** ret
21fed 75 72 6e 65 64 2e 20 54 68 69 73 20 69 73 20 61  urned. This is a
21fee 6e 61 6c 61 67 6f 75 73 20 74 6f 20 72 65 6e 61  nalagous to rena
21fef 6d 65 54 61 62 6c 65 46 75 6e 63 28 29 20 61 62  meTableFunc() ab
21ff0 6f 76 65 2c 20 65 78 63 65 70 74 20 66 6f 72 20  ove, except for 
21ff1 43 52 45 41 54 45 0a 2a 2a 20 54 52 49 47 47 45  CREATE.** TRIGGE
21ff2 52 2c 20 6e 6f 74 20 43 52 45 41 54 45 20 49 4e  R, not CREATE IN
21ff3 44 45 58 20 61 6e 64 20 43 52 45 41 54 45 20 54  DEX and CREATE T
21ff4 41 42 4c 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ABLE..*/.static 
21ff5 76 6f 69 64 20 72 65 6e 61 6d 65 54 72 69 67 67  void renameTrigg
21ff6 65 72 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  erFunc(.  sqlite
21ff7 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
21ff8 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65  xt,.  int NotUse
21ff9 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  d,.  sqlite3_val
21ffa 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 75  ue **argv.){.  u
21ffb 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e  nsigned char con
21ffc 73 74 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74  st *zSql = sqlit
21ffd 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
21ffe 67 76 5b 30 5d 29 3b 0a 20 20 75 6e 73 69 67 6e  gv[0]);.  unsign
21fff 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a  ed char const *z
22000 54 61 62 6c 65 4e 61 6d 65 20 3d 20 73 71 6c 69  TableName = sqli
22001 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
22002 72 67 76 5b 31 5d 29 3b 0a 0a 20 20 69 6e 74 20  rgv[1]);..  int 
22003 74 6f 6b 65 6e 3b 0a 20 20 54 6f 6b 65 6e 20 74  token;.  Token t
22004 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 64 69 73 74  name;.  int dist
22005 20 3d 20 33 3b 0a 20 20 75 6e 73 69 67 6e 65 64   = 3;.  unsigned
22006 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43 73   char const *zCs
22007 72 20 3d 20 7a 53 71 6c 3b 0a 20 20 69 6e 74 20  r = zSql;.  int 
22008 6c 65 6e 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  len = 0;.  char 
22009 2a 7a 52 65 74 3b 0a 20 20 73 71 6c 69 74 65 33  *zRet;.  sqlite3
2200a 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63   *db = sqlite3_c
2200b 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
2200c 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 55 4e  (context);..  UN
2200d 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
2200e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 2f 2a 20 54  otUsed);..  /* T
2200f 68 65 20 70 72 69 6e 63 69 70 6c 65 20 75 73 65  he principle use
22010 64 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20  d to locate the 
22011 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68  table name in th
22012 65 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52  e CREATE TRIGGER
22013 20 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74   .  ** statement
22014 20 69 73 20 74 68 61 74 20 74 68 65 20 74 61 62   is that the tab
22015 6c 65 20 6e 61 6d 65 20 69 73 20 74 68 65 20 66  le name is the f
22016 69 72 73 74 20 74 6f 6b 65 6e 20 74 68 61 74 20  irst token that 
22017 69 73 20 69 6d 6d 65 64 69 61 74 65 64 6c 79 0a  is immediatedly.
22018 20 20 2a 2a 20 70 72 65 63 65 64 65 64 20 62 79    ** preceded by
22019 20 65 69 74 68 65 72 20 54 4b 5f 4f 4e 20 6f 72   either TK_ON or
2201a 20 54 4b 5f 44 4f 54 20 61 6e 64 20 69 6d 6d 65   TK_DOT and imme
2201b 64 69 61 74 65 64 6c 79 20 66 6f 6c 6c 6f 77 65  diatedly followe
2201c 64 20 62 79 20 6f 6e 65 0a 20 20 2a 2a 20 6f 66  d by one.  ** of
2201d 20 54 4b 5f 57 48 45 4e 2c 20 54 4b 5f 42 45 47   TK_WHEN, TK_BEG
2201e 49 4e 20 6f 72 20 54 4b 5f 46 4f 52 2e 0a 20 20  IN or TK_FOR..  
2201f 2a 2f 0a 20 20 69 66 28 20 7a 53 71 6c 20 29 7b  */.  if( zSql ){
22020 0a 20 20 20 20 64 6f 20 7b 0a 0a 20 20 20 20 20  .    do {..     
22021 20 69 66 28 20 21 2a 7a 43 73 72 20 29 7b 0a 20   if( !*zCsr ){. 
22022 20 20 20 20 20 20 20 2f 2a 20 52 61 6e 20 6f 75         /* Ran ou
22023 74 20 6f 66 20 69 6e 70 75 74 20 62 65 66 6f 72  t of input befor
22024 65 20 66 69 6e 64 69 6e 67 20 74 68 65 20 74 61  e finding the ta
22025 62 6c 65 20 6e 61 6d 65 2e 20 52 65 74 75 72 6e  ble name. Return
22026 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 20 20 20 20   NULL. */.      
22027 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
22028 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 74 6f 72  }..      /* Stor
22029 65 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74  e the token that
2202a 20 7a 43 73 72 20 70 6f 69 6e 74 73 20 74 6f 20   zCsr points to 
2202b 69 6e 20 74 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20  in tname. */.   
2202c 20 20 20 74 6e 61 6d 65 2e 7a 20 3d 20 28 63 68     tname.z = (ch
2202d 61 72 2a 29 7a 43 73 72 3b 0a 20 20 20 20 20 20  ar*)zCsr;.      
2202e 74 6e 61 6d 65 2e 6e 20 3d 20 6c 65 6e 3b 0a 0a  tname.n = len;..
2202f 20 20 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65        /* Advance
22030 20 7a 43 73 72 20 74 6f 20 74 68 65 20 6e 65 78   zCsr to the nex
22031 74 20 74 6f 6b 65 6e 2e 20 53 74 6f 72 65 20 74  t token. Store t
22032 68 61 74 20 74 6f 6b 65 6e 20 74 79 70 65 20 69  hat token type i
22033 6e 20 27 74 6f 6b 65 6e 27 2c 0a 20 20 20 20 20  n 'token',.     
22034 20 2a 2a 20 61 6e 64 20 69 74 73 20 6c 65 6e 67   ** and its leng
22035 74 68 20 69 6e 20 27 6c 65 6e 27 20 28 74 6f 20  th in 'len' (to 
22036 62 65 20 75 73 65 64 20 6e 65 78 74 20 69 74 65  be used next ite
22037 72 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6c  ration of this l
22038 6f 6f 70 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  oop)..      */. 
22039 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20       do {.      
2203a 20 20 7a 43 73 72 20 2b 3d 20 6c 65 6e 3b 0a 20    zCsr += len;. 
2203b 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c         len = sql
2203c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 7a 43 73  ite3GetToken(zCs
2203d 72 2c 20 26 74 6f 6b 65 6e 29 3b 0a 20 20 20 20  r, &token);.    
2203e 20 20 7d 77 68 69 6c 65 28 20 74 6f 6b 65 6e 3d    }while( token=
2203f 3d 54 4b 5f 53 50 41 43 45 20 29 3b 0a 20 20 20  =TK_SPACE );.   
22040 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e 3e 30     assert( len>0
22041 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 56 61   );..      /* Va
22042 72 69 61 62 6c 65 20 27 64 69 73 74 27 20 73 74  riable 'dist' st
22043 6f 72 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20  ores the number 
22044 6f 66 20 74 6f 6b 65 6e 73 20 72 65 61 64 20 73  of tokens read s
22045 69 6e 63 65 20 74 68 65 20 6d 6f 73 74 0a 20 20  ince the most.  
22046 20 20 20 20 2a 2a 20 72 65 63 65 6e 74 20 54 4b      ** recent TK
22047 5f 44 4f 54 20 6f 72 20 54 4b 5f 4f 4e 2e 20 54  _DOT or TK_ON. T
22048 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 77  his means that w
22049 68 65 6e 20 61 20 57 48 45 4e 2c 20 46 4f 52 20  hen a WHEN, FOR 
2204a 6f 72 20 42 45 47 49 4e 20 0a 20 20 20 20 20 20  or BEGIN .      
2204b 2a 2a 20 74 6f 6b 65 6e 20 69 73 20 72 65 61 64  ** token is read
2204c 20 61 6e 64 20 27 64 69 73 74 27 20 65 71 75 61   and 'dist' equa
2204d 6c 73 20 32 2c 20 74 68 65 20 63 6f 6e 64 69 74  ls 2, the condit
2204e 69 6f 6e 20 73 74 61 74 65 64 20 61 62 6f 76 65  ion stated above
2204f 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 62 65 20  .      ** to be 
22050 6d 65 74 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20  met..      **.  
22051 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74      ** Note that
22052 20 4f 4e 20 63 61 6e 6e 6f 74 20 62 65 20 61 20   ON cannot be a 
22053 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 20  database, table 
22054 6f 72 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2c 20  or column name, 
22055 73 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 72  so.      ** ther
22056 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20  e is no need to 
22057 77 6f 72 72 79 20 61 62 6f 75 74 20 73 79 6e 74  worry about synt
22058 61 78 20 6c 69 6b 65 20 0a 20 20 20 20 20 20 2a  ax like .      *
22059 2a 20 22 43 52 45 41 54 45 20 54 52 49 47 47 45  * "CREATE TRIGGE
2205a 52 20 2e 2e 2e 20 4f 4e 20 4f 4e 2e 4f 4e 20 42  R ... ON ON.ON B
2205b 45 47 49 4e 20 2e 2e 2e 22 20 65 74 63 2e 0a 20  EGIN ..." etc.. 
2205c 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 69       */.      di
2205d 73 74 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20  st++;.      if( 
2205e 74 6f 6b 65 6e 3d 3d 54 4b 5f 44 4f 54 20 7c 7c  token==TK_DOT ||
2205f 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 4f 4e 20 29 7b   token==TK_ON ){
22060 0a 20 20 20 20 20 20 20 20 64 69 73 74 20 3d 20  .        dist = 
22061 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  0;.      }.    }
22062 20 77 68 69 6c 65 28 20 64 69 73 74 21 3d 32 20   while( dist!=2 
22063 7c 7c 20 28 74 6f 6b 65 6e 21 3d 54 4b 5f 57 48  || (token!=TK_WH
22064 45 4e 20 26 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f  EN && token!=TK_
22065 46 4f 52 20 26 26 20 74 6f 6b 65 6e 21 3d 54 4b  FOR && token!=TK
22066 5f 42 45 47 49 4e 29 20 29 3b 0a 0a 20 20 20 20  _BEGIN) );..    
22067 2f 2a 20 56 61 72 69 61 62 6c 65 20 74 6e 61 6d  /* Variable tnam
22068 65 20 6e 6f 77 20 63 6f 6e 74 61 69 6e 73 20 74  e now contains t
22069 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 69 73  he token that is
2206a 20 74 68 65 20 6f 6c 64 20 74 61 62 6c 65 2d 6e   the old table-n
2206b 61 6d 65 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68  ame.    ** in th
2206c 65 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52  e CREATE TRIGGER
2206d 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20   statement..    
2206e 2a 2f 0a 20 20 20 20 7a 52 65 74 20 3d 20 73 71  */.    zRet = sq
2206f 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
22070 20 22 25 2e 2a 73 5c 22 25 77 5c 22 25 73 22 2c   "%.*s\"%w\"%s",
22071 20 28 28 75 38 2a 29 74 6e 61 6d 65 2e 7a 29 20   ((u8*)tname.z) 
22072 2d 20 7a 53 71 6c 2c 20 7a 53 71 6c 2c 20 0a 20  - zSql, zSql, . 
22073 20 20 20 20 20 20 7a 54 61 62 6c 65 4e 61 6d 65        zTableName
22074 2c 20 74 6e 61 6d 65 2e 7a 2b 74 6e 61 6d 65 2e  , tname.z+tname.
22075 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  n);.    sqlite3_
22076 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
22077 65 78 74 2c 20 7a 52 65 74 2c 20 2d 31 2c 20 53  ext, zRet, -1, S
22078 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a  QLITE_DYNAMIC);.
22079 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 20 20 2f    }.}.#endif   /
2207a 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  * !SQLITE_OMIT_T
2207b 52 49 47 47 45 52 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  RIGGER */../*.**
2207c 20 52 65 67 69 73 74 65 72 20 62 75 69 6c 74 2d   Register built-
2207d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65  in functions use
2207e 64 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d  d to help implem
2207f 65 6e 74 20 41 4c 54 45 52 20 54 41 42 4c 45 0a  ent ALTER TABLE.
22080 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
22081 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c  E void sqlite3Al
22082 74 65 72 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c  terFunctions(sql
22083 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c  ite3 *db){.  sql
22084 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64  ite3CreateFunc(d
22085 62 2c 20 22 73 71 6c 69 74 65 5f 72 65 6e 61 6d  b, "sqlite_renam
22086 65 5f 74 61 62 6c 65 22 2c 20 32 2c 20 53 51 4c  e_table", 2, SQL
22087 49 54 45 5f 55 54 46 38 2c 20 30 2c 0a 20 20 20  ITE_UTF8, 0,.   
22088 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22089 20 20 20 20 20 20 72 65 6e 61 6d 65 54 61 62 6c        renameTabl
2208a 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 23 69  eFunc, 0, 0);.#i
2208b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2208c 54 5f 54 52 49 47 47 45 52 0a 20 20 73 71 6c 69  T_TRIGGER.  sqli
2208d 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62  te3CreateFunc(db
2208e 2c 20 22 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65  , "sqlite_rename
2208f 5f 74 72 69 67 67 65 72 22 2c 20 32 2c 20 53 51  _trigger", 2, SQ
22090 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 0a 20 20  LITE_UTF8, 0,.  
22091 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22092 20 20 20 20 20 20 20 72 65 6e 61 6d 65 54 72 69         renameTri
22093 67 67 65 72 46 75 6e 63 2c 20 30 2c 20 30 29 3b  ggerFunc, 0, 0);
22094 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
22095 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 74 65   Generate the te
22096 78 74 20 6f 66 20 61 20 57 48 45 52 45 20 65 78  xt of a WHERE ex
22097 70 72 65 73 73 69 6f 6e 20 77 68 69 63 68 20 63  pression which c
22098 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65  an be used to se
22099 6c 65 63 74 20 61 6c 6c 0a 2a 2a 20 74 65 6d 70  lect all.** temp
2209a 6f 72 61 72 79 20 74 72 69 67 67 65 72 73 20 6f  orary triggers o
2209b 6e 20 74 61 62 6c 65 20 70 54 61 62 20 66 72 6f  n table pTab fro
2209c 6d 20 74 68 65 20 73 71 6c 69 74 65 5f 74 65 6d  m the sqlite_tem
2209d 70 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20  p_master table. 
2209e 49 66 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62  If.** table pTab
2209f 20 68 61 73 20 6e 6f 20 74 65 6d 70 6f 72 61 72   has no temporar
220a0 79 20 74 72 69 67 67 65 72 73 2c 20 6f 72 20 69  y triggers, or i
220a1 73 20 69 74 73 65 6c 66 20 73 74 6f 72 65 64 20  s itself stored 
220a2 69 6e 20 74 68 65 20 0a 2a 2a 20 74 65 6d 70 6f  in the .** tempo
220a3 72 61 72 79 20 64 61 74 61 62 61 73 65 2c 20 4e  rary database, N
220a4 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ULL is returned.
220a5 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20  .*/.static char 
220a6 2a 77 68 65 72 65 54 65 6d 70 54 72 69 67 67 65  *whereTempTrigge
220a7 72 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  rs(Parse *pParse
220a8 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a  , Table *pTab){.
220a9 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67    Trigger *pTrig
220aa 3b 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65  ;.  char *zWhere
220ab 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 74 6d   = 0;.  char *tm
220ac 70 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 53  p = 0;.  const S
220ad 63 68 65 6d 61 20 2a 70 54 65 6d 70 53 63 68 65  chema *pTempSche
220ae 6d 61 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d  ma = pParse->db-
220af 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 3b  >aDb[1].pSchema;
220b0 20 2f 2a 20 54 65 6d 70 20 64 62 20 73 63 68 65   /* Temp db sche
220b1 6d 61 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74  ma */..  /* If t
220b2 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20  he table is not 
220b3 6c 6f 63 61 74 65 64 20 69 6e 20 74 68 65 20 74  located in the t
220b4 65 6d 70 2d 64 62 20 28 69 6e 20 77 68 69 63 68  emp-db (in which
220b5 20 63 61 73 65 20 4e 55 4c 4c 20 69 73 20 0a 20   case NULL is . 
220b6 20 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20 6c 6f   ** returned, lo
220b7 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 74  op through the t
220b8 61 62 6c 65 73 20 6c 69 73 74 20 6f 66 20 74 72  ables list of tr
220b9 69 67 67 65 72 73 2e 20 46 6f 72 20 65 61 63 68  iggers. For each
220ba 20 74 72 69 67 67 65 72 0a 20 20 2a 2a 20 74 68   trigger.  ** th
220bb 61 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f  at is not part o
220bc 66 20 74 68 65 20 74 65 6d 70 2d 64 62 20 73 63  f the temp-db sc
220bd 68 65 6d 61 2c 20 61 64 64 20 61 20 63 6c 61 75  hema, add a clau
220be 73 65 20 74 6f 20 74 68 65 20 57 48 45 52 45 20  se to the WHERE 
220bf 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  .  ** expression
220c0 20 62 65 69 6e 67 20 62 75 69 6c 74 20 75 70 20   being built up 
220c1 69 6e 20 7a 57 68 65 72 65 2e 0a 20 20 2a 2f 0a  in zWhere..  */.
220c2 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 63 68    if( pTab->pSch
220c3 65 6d 61 21 3d 70 54 65 6d 70 53 63 68 65 6d 61  ema!=pTempSchema
220c4 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20   ){.    sqlite3 
220c5 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
220c6 3b 0a 20 20 20 20 66 6f 72 28 70 54 72 69 67 3d  ;.    for(pTrig=
220c7 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69  sqlite3TriggerLi
220c8 73 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 29  st(pParse, pTab)
220c9 3b 20 70 54 72 69 67 3b 20 70 54 72 69 67 3d 70  ; pTrig; pTrig=p
220ca 54 72 69 67 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  Trig->pNext){.  
220cb 20 20 20 20 69 66 28 20 70 54 72 69 67 2d 3e 70      if( pTrig->p
220cc 53 63 68 65 6d 61 3d 3d 70 54 65 6d 70 53 63 68  Schema==pTempSch
220cd 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ema ){.        i
220ce 66 28 20 21 7a 57 68 65 72 65 20 29 7b 0a 20 20  f( !zWhere ){.  
220cf 20 20 20 20 20 20 20 20 7a 57 68 65 72 65 20 3d          zWhere =
220d0 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
220d1 64 62 2c 20 22 6e 61 6d 65 3d 25 51 22 2c 20 70  db, "name=%Q", p
220d2 54 72 69 67 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  Trig->zName);.  
220d3 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
220d4 20 20 20 20 20 20 20 74 6d 70 20 3d 20 7a 57 68         tmp = zWh
220d5 65 72 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7a  ere;.          z
220d6 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 4d  Where = sqlite3M
220d7 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 20 4f  Printf(db, "%s O
220d8 52 20 6e 61 6d 65 3d 25 51 22 2c 20 7a 57 68 65  R name=%Q", zWhe
220d9 72 65 2c 20 70 54 72 69 67 2d 3e 7a 4e 61 6d 65  re, pTrig->zName
220da 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
220db 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 74  ite3DbFree(db, t
220dc 6d 70 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  mp);.        }. 
220dd 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
220de 0a 20 20 72 65 74 75 72 6e 20 7a 57 68 65 72 65  .  return zWhere
220df 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ;.}../*.** Gener
220e0 61 74 65 20 63 6f 64 65 20 74 6f 20 64 72 6f 70  ate code to drop
220e1 20 61 6e 64 20 72 65 6c 6f 61 64 20 74 68 65 20   and reload the 
220e2 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65  internal represe
220e3 6e 74 61 74 69 6f 6e 20 6f 66 20 74 61 62 6c 65  ntation of table
220e4 0a 2a 2a 20 70 54 61 62 20 66 72 6f 6d 20 74 68  .** pTab from th
220e5 65 20 64 61 74 61 62 61 73 65 2c 20 69 6e 63 6c  e database, incl
220e6 75 64 69 6e 67 20 74 72 69 67 67 65 72 73 20 61  uding triggers a
220e7 6e 64 20 74 65 6d 70 6f 72 61 72 79 20 74 72 69  nd temporary tri
220e8 67 67 65 72 73 2e 0a 2a 2a 20 41 72 67 75 6d 65  ggers..** Argume
220e9 6e 74 20 7a 4e 61 6d 65 20 69 73 20 74 68 65 20  nt zName is the 
220ea 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
220eb 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  e in the databas
220ec 65 20 73 63 68 65 6d 61 20 61 74 0a 2a 2a 20 74  e schema at.** t
220ed 68 65 20 74 69 6d 65 20 74 68 65 20 67 65 6e 65  he time the gene
220ee 72 61 74 65 64 20 63 6f 64 65 20 69 73 20 65 78  rated code is ex
220ef 65 63 75 74 65 64 2e 20 54 68 69 73 20 63 61 6e  ecuted. This can
220f0 20 62 65 20 64 69 66 66 65 72 65 6e 74 20 66 72   be different fr
220f1 6f 6d 0a 2a 2a 20 70 54 61 62 2d 3e 7a 4e 61 6d  om.** pTab->zNam
220f2 65 20 69 66 20 74 68 69 73 20 66 75 6e 63 74 69  e if this functi
220f3 6f 6e 20 69 73 20 62 65 69 6e 67 20 63 61 6c 6c  on is being call
220f4 65 64 20 74 6f 20 63 6f 64 65 20 70 61 72 74 20  ed to code part 
220f5 6f 66 20 61 6e 20 0a 2a 2a 20 22 41 4c 54 45 52  of an .** "ALTER
220f6 20 54 41 42 4c 45 20 52 45 4e 41 4d 45 20 54 4f   TABLE RENAME TO
220f7 22 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a  " statement..*/.
220f8 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 6f  static void relo
220f9 61 64 54 61 62 6c 65 53 63 68 65 6d 61 28 50 61  adTableSchema(Pa
220fa 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62  rse *pParse, Tab
220fb 6c 65 20 2a 70 54 61 62 2c 20 63 6f 6e 73 74 20  le *pTab, const 
220fc 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20  char *zName){.  
220fd 56 64 62 65 20 2a 76 3b 0a 20 20 63 68 61 72 20  Vdbe *v;.  char 
220fe 2a 7a 57 68 65 72 65 3b 0a 20 20 69 6e 74 20 69  *zWhere;.  int i
220ff 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
22100 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
22101 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61  f database conta
22102 69 6e 69 6e 67 20 70 54 61 62 20 2a 2f 0a 23 69  ining pTab */.#i
22103 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
22104 54 5f 54 52 49 47 47 45 52 0a 20 20 54 72 69 67  T_TRIGGER.  Trig
22105 67 65 72 20 2a 70 54 72 69 67 3b 0a 23 65 6e 64  ger *pTrig;.#end
22106 69 66 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  if..  v = sqlite
22107 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
22108 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 76 3d  ;.  if( NEVER(v=
22109 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  =0) ) return;.  
2210a 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
2210b 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65  treeHoldsAllMute
2210c 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20  xes(pParse->db) 
2210d 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74  );.  iDb = sqlit
2210e 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
2210f 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62  pParse->db, pTab
22110 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73  ->pSchema);.  as
22111 73 65 72 74 28 20 69 44 62 3e 3d 30 20 29 3b 0a  sert( iDb>=0 );.
22112 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
22113 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 2f  OMIT_TRIGGER.  /
22114 2a 20 44 72 6f 70 20 61 6e 79 20 74 61 62 6c 65  * Drop any table
22115 20 74 72 69 67 67 65 72 73 20 66 72 6f 6d 20 74   triggers from t
22116 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65  he internal sche
22117 6d 61 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 54 72  ma. */.  for(pTr
22118 69 67 3d 73 71 6c 69 74 65 33 54 72 69 67 67 65  ig=sqlite3Trigge
22119 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 54  rList(pParse, pT
2211a 61 62 29 3b 20 70 54 72 69 67 3b 20 70 54 72 69  ab); pTrig; pTri
2211b 67 3d 70 54 72 69 67 2d 3e 70 4e 65 78 74 29 7b  g=pTrig->pNext){
2211c 0a 20 20 20 20 69 6e 74 20 69 54 72 69 67 44 62  .    int iTrigDb
2211d 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
2211e 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e  ToIndex(pParse->
2211f 64 62 2c 20 70 54 72 69 67 2d 3e 70 53 63 68 65  db, pTrig->pSche
22120 6d 61 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ma);.    assert(
22121 20 69 54 72 69 67 44 62 3d 3d 69 44 62 20 7c 7c   iTrigDb==iDb ||
22122 20 69 54 72 69 67 44 62 3d 3d 31 20 29 3b 0a 20   iTrigDb==1 );. 
22123 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22124 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54  dOp4(v, OP_DropT
22125 72 69 67 67 65 72 2c 20 69 54 72 69 67 44 62 2c  rigger, iTrigDb,
22126 20 30 2c 20 30 2c 20 70 54 72 69 67 2d 3e 7a 4e   0, 0, pTrig->zN
22127 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 23 65 6e  ame, 0);.  }.#en
22128 64 69 66 0a 0a 20 20 2f 2a 20 44 72 6f 70 20 74  dif..  /* Drop t
22129 68 65 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64  he table and ind
2212a 65 78 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65  ex from the inte
2212b 72 6e 61 6c 20 73 63 68 65 6d 61 20 2a 2f 0a 20  rnal schema */. 
2212c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2212d 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 61 62  p4(v, OP_DropTab
2212e 6c 65 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 70  le, iDb, 0, 0, p
2212f 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a  Tab->zName, 0);.
22130 0a 20 20 2f 2a 20 52 65 6c 6f 61 64 20 74 68 65  .  /* Reload the
22131 20 74 61 62 6c 65 2c 20 69 6e 64 65 78 20 61 6e   table, index an
22132 64 20 70 65 72 6d 61 6e 65 6e 74 20 74 72 69 67  d permanent trig
22133 67 65 72 20 73 63 68 65 6d 61 73 2e 20 2a 2f 0a  ger schemas. */.
22134 20 20 7a 57 68 65 72 65 20 3d 20 73 71 6c 69 74    zWhere = sqlit
22135 65 33 4d 50 72 69 6e 74 66 28 70 50 61 72 73 65  e3MPrintf(pParse
22136 2d 3e 64 62 2c 20 22 74 62 6c 5f 6e 61 6d 65 3d  ->db, "tbl_name=
22137 25 51 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69  %Q", zName);.  i
22138 66 28 20 21 7a 57 68 65 72 65 20 29 20 72 65 74  f( !zWhere ) ret
22139 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  urn;.  sqlite3Vd
2213a 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50  beAddOp4(v, OP_P
2213b 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44 62 2c  arseSchema, iDb,
2213c 20 30 2c 20 30 2c 20 7a 57 68 65 72 65 2c 20 50   0, 0, zWhere, P
2213d 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 0a 23 69 66  4_DYNAMIC);..#if
2213e 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2213f 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 4e 6f  _TRIGGER.  /* No
22140 77 2c 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  w, if the table 
22141 69 73 20 6e 6f 74 20 73 74 6f 72 65 64 20 69 6e  is not stored in
22142 20 74 68 65 20 74 65 6d 70 20 64 61 74 61 62 61   the temp databa
22143 73 65 2c 20 72 65 6c 6f 61 64 20 61 6e 79 20 74  se, reload any t
22144 65 6d 70 20 0a 20 20 2a 2a 20 74 72 69 67 67 65  emp .  ** trigge
22145 72 73 2e 20 44 6f 6e 27 74 20 75 73 65 20 49 4e  rs. Don't use IN
22146 28 2e 2e 2e 29 20 69 6e 20 63 61 73 65 20 53 51  (...) in case SQ
22147 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
22148 52 59 20 69 73 20 64 65 66 69 6e 65 64 2e 20 0a  RY is defined. .
22149 20 20 2a 2f 0a 20 20 69 66 28 20 28 7a 57 68 65    */.  if( (zWhe
2214a 72 65 3d 77 68 65 72 65 54 65 6d 70 54 72 69 67  re=whereTempTrig
2214b 67 65 72 73 28 70 50 61 72 73 65 2c 20 70 54 61  gers(pParse, pTa
2214c 62 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71  b))!=0 ){.    sq
2214d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2214e 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d  v, OP_ParseSchem
2214f 61 2c 20 31 2c 20 30 2c 20 30 2c 20 7a 57 68 65  a, 1, 0, 0, zWhe
22150 72 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b  re, P4_DYNAMIC);
22151 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  .  }.#endif.}../
22152 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
22153 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  de to implement 
22154 74 68 65 20 22 41 4c 54 45 52 20 54 41 42 4c 45  the "ALTER TABLE
22155 20 78 78 78 20 52 45 4e 41 4d 45 20 54 4f 20 79   xxx RENAME TO y
22156 79 79 22 20 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2e  yy" .** command.
22157 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
22158 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
22159 41 6c 74 65 72 52 65 6e 61 6d 65 54 61 62 6c 65  AlterRenameTable
2215a 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2215b 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e,            /*
2215c 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 2e   Parser context.
2215d 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
2215e 53 72 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  Src,            
2215f 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20  /* The table to 
22160 72 65 6e 61 6d 65 2e 20 2a 2f 0a 20 20 54 6f 6b  rename. */.  Tok
22161 65 6e 20 2a 70 4e 61 6d 65 20 20 20 20 20 20 20  en *pName       
22162 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65         /* The ne
22163 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f  w table name. */
22164 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20  .){.  int iDb;  
22165 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22166 2f 2a 20 44 61 74 61 62 61 73 65 20 74 68 61 74  /* Database that
22167 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 61   contains the ta
22168 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ble */.  char *z
22169 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
2216a 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61     /* Name of da
2216b 74 61 62 61 73 65 20 69 44 62 20 2a 2f 0a 20 20  tabase iDb */.  
2216c 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20  Table *pTab;    
2216d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62            /* Tab
2216e 6c 65 20 62 65 69 6e 67 20 72 65 6e 61 6d 65 64  le being renamed
2216f 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d   */.  char *zNam
22170 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  e = 0;          
22171 2f 2a 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e 61 74  /* NULL-terminat
22172 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 4e  ed version of pN
22173 61 6d 65 20 2a 2f 20 0a 20 20 73 71 6c 69 74 65  ame */ .  sqlite
22174 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
22175 64 62 3b 20 2f 2a 20 44 61 74 61 62 61 73 65 20  db; /* Database 
22176 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
22177 69 6e 74 20 6e 54 61 62 4e 61 6d 65 3b 20 20 20  int nTabName;   
22178 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
22179 62 65 72 20 6f 66 20 55 54 46 2d 38 20 63 68 61  ber of UTF-8 cha
2217a 72 61 63 74 65 72 73 20 69 6e 20 7a 54 61 62 4e  racters in zTabN
2217b 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ame */.  const c
2217c 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 3b 20 20  har *zTabName;  
2217d 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6e     /* Original n
2217e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
2217f 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 0a 23   */.  Vdbe *v;.#
22180 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
22181 49 54 5f 54 52 49 47 47 45 52 0a 20 20 63 68 61  IT_TRIGGER.  cha
22182 72 20 2a 7a 57 68 65 72 65 20 3d 20 30 3b 20 20  r *zWhere = 0;  
22183 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20         /* Where 
22184 63 6c 61 75 73 65 20 74 6f 20 6c 6f 63 61 74 65  clause to locate
22185 20 74 65 6d 70 20 74 72 69 67 67 65 72 73 20 2a   temp triggers *
22186 2f 0a 23 65 6e 64 69 66 0a 20 20 56 54 61 62 6c  /.#endif.  VTabl
22187 65 20 2a 70 56 54 61 62 20 3d 20 30 3b 20 20 20  e *pVTab = 0;   
22188 20 20 20 20 20 2f 2a 20 4e 6f 6e 2d 7a 65 72 6f       /* Non-zero
22189 20 69 66 20 74 68 69 73 20 69 73 20 61 20 76 2d   if this is a v-
2218a 74 61 62 20 77 69 74 68 20 61 6e 20 78 52 65 6e  tab with an xRen
2218b 61 6d 65 28 29 20 2a 2f 0a 20 20 0a 20 20 69 66  ame() */.  .  if
2218c 28 20 4e 45 56 45 52 28 64 62 2d 3e 6d 61 6c 6c  ( NEVER(db->mall
2218d 6f 63 46 61 69 6c 65 64 29 20 29 20 67 6f 74 6f  ocFailed) ) goto
2218e 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62   exit_rename_tab
2218f 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53  le;.  assert( pS
22190 72 63 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20  rc->nSrc==1 );. 
22191 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
22192 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74  BtreeHoldsAllMut
22193 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29  exes(pParse->db)
22194 20 29 3b 0a 0a 20 20 70 54 61 62 20 3d 20 73 71   );..  pTab = sq
22195 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
22196 28 70 50 61 72 73 65 2c 20 30 2c 20 70 53 72 63  (pParse, 0, pSrc
22197 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 53  ->a[0].zName, pS
22198 72 63 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61  rc->a[0].zDataba
22199 73 65 29 3b 0a 20 20 69 66 28 20 21 70 54 61 62  se);.  if( !pTab
2219a 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e   ) goto exit_ren
2219b 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 69 44 62  ame_table;.  iDb
2219c 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
2219d 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e  ToIndex(pParse->
2219e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
2219f 61 29 3b 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e  a);.  zDb = db->
221a0 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a  aDb[iDb].zName;.
221a1 0a 20 20 2f 2a 20 47 65 74 20 61 20 4e 55 4c 4c  .  /* Get a NULL
221a2 20 74 65 72 6d 69 6e 61 74 65 64 20 76 65 72 73   terminated vers
221a3 69 6f 6e 20 6f 66 20 74 68 65 20 6e 65 77 20 74  ion of the new t
221a4 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20  able name. */.  
221a5 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e  zName = sqlite3N
221a6 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
221a7 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21   pName);.  if( !
221a8 7a 4e 61 6d 65 20 29 20 67 6f 74 6f 20 65 78 69  zName ) goto exi
221a9 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a  t_rename_table;.
221aa 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74  .  /* Check that
221ab 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65   a table or inde
221ac 78 20 6e 61 6d 65 64 20 27 7a 4e 61 6d 65 27 20  x named 'zName' 
221ad 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  does not already
221ae 20 65 78 69 73 74 0a 20 20 2a 2a 20 69 6e 20 64   exist.  ** in d
221af 61 74 61 62 61 73 65 20 69 44 62 2e 20 49 66 20  atabase iDb. If 
221b0 73 6f 2c 20 74 68 69 73 20 69 73 20 61 6e 20 65  so, this is an e
221b1 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  rror..  */.  if(
221b2 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
221b3 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62  e(db, zName, zDb
221b4 29 20 7c 7c 20 73 71 6c 69 74 65 33 46 69 6e 64  ) || sqlite3Find
221b5 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c  Index(db, zName,
221b6 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 73 71 6c   zDb) ){.    sql
221b7 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
221b8 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 74  rse, .        "t
221b9 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20  here is already 
221ba 61 6e 6f 74 68 65 72 20 74 61 62 6c 65 20 6f 72  another table or
221bb 20 69 6e 64 65 78 20 77 69 74 68 20 74 68 69 73   index with this
221bc 20 6e 61 6d 65 3a 20 25 73 22 2c 20 7a 4e 61 6d   name: %s", zNam
221bd 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69  e);.    goto exi
221be 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a  t_rename_table;.
221bf 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73    }..  /* Make s
221c0 75 72 65 20 69 74 20 69 73 20 6e 6f 74 20 61 20  ure it is not a 
221c1 73 79 73 74 65 6d 20 74 61 62 6c 65 20 62 65 69  system table bei
221c2 6e 67 20 61 6c 74 65 72 65 64 2c 20 6f 72 20 61  ng altered, or a
221c3 20 72 65 73 65 72 76 65 64 20 6e 61 6d 65 0a 20   reserved name. 
221c4 20 2a 2a 20 74 68 61 74 20 74 68 65 20 74 61 62   ** that the tab
221c5 6c 65 20 69 73 20 62 65 69 6e 67 20 72 65 6e 61  le is being rena
221c6 6d 65 64 20 74 6f 2e 0a 20 20 2a 2f 0a 20 20 69  med to..  */.  i
221c7 66 28 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  f( sqlite3Strlen
221c8 33 30 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3e  30(pTab->zName)>
221c9 36 20 0a 20 20 20 26 26 20 30 3d 3d 73 71 6c 69  6 .   && 0==sqli
221ca 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54 61 62  te3StrNICmp(pTab
221cb 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65  ->zName, "sqlite
221cc 5f 22 2c 20 37 29 0a 20 20 29 7b 0a 20 20 20 20  _", 7).  ){.    
221cd 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
221ce 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25  pParse, "table %
221cf 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c 74  s may not be alt
221d0 65 72 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61  ered", pTab->zNa
221d1 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78  me);.    goto ex
221d2 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b  it_rename_table;
221d3 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54  .  }.  if( SQLIT
221d4 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 43 68 65  E_OK!=sqlite3Che
221d5 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 70 50 61  ckObjectName(pPa
221d6 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20  rse, zName) ){. 
221d7 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e     goto exit_ren
221d8 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a  ame_table;.  }..
221d9 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
221da 4d 49 54 5f 56 49 45 57 0a 20 20 69 66 28 20 70  MIT_VIEW.  if( p
221db 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  Tab->pSelect ){.
221dc 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
221dd 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 69 65  Msg(pParse, "vie
221de 77 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20  w %s may not be 
221df 61 6c 74 65 72 65 64 22 2c 20 70 54 61 62 2d 3e  altered", pTab->
221e0 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f  zName);.    goto
221e1 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62   exit_rename_tab
221e2 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  le;.  }.#endif..
221e3 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
221e4 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
221e5 4e 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68  N.  /* Invoke th
221e6 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  e authorization 
221e7 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20 20 69  callback. */.  i
221e8 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
221e9 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
221ea 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 2c 20  TE_ALTER_TABLE, 
221eb 7a 44 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  zDb, pTab->zName
221ec 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  , 0) ){.    goto
221ed 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62   exit_rename_tab
221ee 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  le;.  }.#endif..
221ef 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
221f0 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
221f1 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 69  .  if( sqlite3Vi
221f2 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73  ewGetColumnNames
221f3 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 29  (pParse, pTab) )
221f4 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  {.    goto exit_
221f5 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20  rename_table;.  
221f6 7d 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61  }.  if( IsVirtua
221f7 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 70  l(pTab) ){.    p
221f8 56 54 61 62 20 3d 20 73 71 6c 69 74 65 33 47 65  VTab = sqlite3Ge
221f9 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62  tVTable(db, pTab
221fa 29 3b 0a 20 20 20 20 69 66 28 20 70 56 54 61 62  );.    if( pVTab
221fb 2d 3e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  ->pVtab->pModule
221fc 2d 3e 78 52 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a  ->xRename==0 ){.
221fd 20 20 20 20 20 20 70 56 54 61 62 20 3d 20 30 3b        pVTab = 0;
221fe 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
221ff 66 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61 20  f..  /* Begin a 
22200 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20  transaction and 
22201 63 6f 64 65 20 74 68 65 20 56 65 72 69 66 79 43  code the VerifyC
22202 6f 6f 6b 69 65 20 66 6f 72 20 64 61 74 61 62 61  ookie for databa
22203 73 65 20 69 44 62 2e 20 0a 20 20 2a 2a 20 54 68  se iDb. .  ** Th
22204 65 6e 20 6d 6f 64 69 66 79 20 74 68 65 20 73 63  en modify the sc
22205 68 65 6d 61 20 63 6f 6f 6b 69 65 20 28 73 69 6e  hema cookie (sin
22206 63 65 20 74 68 65 20 41 4c 54 45 52 20 54 41 42  ce the ALTER TAB
22207 4c 45 20 6d 6f 64 69 66 69 65 73 20 74 68 65 0a  LE modifies the.
22208 20 20 2a 2a 20 73 63 68 65 6d 61 29 2e 20 4f 70    ** schema). Op
22209 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74  en a statement t
2220a 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68  ransaction if th
2220b 65 20 74 61 62 6c 65 20 69 73 20 61 20 76 69 72  e table is a vir
2220c 74 75 61 6c 0a 20 20 2a 2a 20 74 61 62 6c 65 2e  tual.  ** table.
2220d 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69  .  */.  v = sqli
2220e 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
2220f 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29  e);.  if( v==0 )
22210 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  {.    goto exit_
22211 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20  rename_table;.  
22212 7d 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e  }.  sqlite3Begin
22213 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70  WriteOperation(p
22214 50 61 72 73 65 2c 20 70 56 54 61 62 21 3d 30 2c  Parse, pVTab!=0,
22215 20 69 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 33   iDb);.  sqlite3
22216 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61  ChangeCookie(pPa
22217 72 73 65 2c 20 69 44 62 29 3b 0a 0a 20 20 2f 2a  rse, iDb);..  /*
22218 20 49 66 20 74 68 69 73 20 69 73 20 61 20 76 69   If this is a vi
22219 72 74 75 61 6c 20 74 61 62 6c 65 2c 20 69 6e 76  rtual table, inv
2221a 6f 6b 65 20 74 68 65 20 78 52 65 6e 61 6d 65 28  oke the xRename(
2221b 29 20 66 75 6e 63 74 69 6f 6e 20 69 66 0a 20 20  ) function if.  
2221c 2a 2a 20 6f 6e 65 20 69 73 20 64 65 66 69 6e 65  ** one is define
2221d 64 2e 20 54 68 65 20 78 52 65 6e 61 6d 65 28 29  d. The xRename()
2221e 20 63 61 6c 6c 62 61 63 6b 20 77 69 6c 6c 20 6d   callback will m
2221f 6f 64 69 66 79 20 74 68 65 20 6e 61 6d 65 73 0a  odify the names.
22220 20 20 2a 2a 20 6f 66 20 61 6e 79 20 72 65 73 6f    ** of any reso
22221 75 72 63 65 73 20 75 73 65 64 20 62 79 20 74 68  urces used by th
22222 65 20 76 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d  e v-table implem
22223 65 6e 74 61 74 69 6f 6e 20 28 69 6e 63 6c 75 64  entation (includ
22224 69 6e 67 20 6f 74 68 65 72 0a 20 20 2a 2a 20 53  ing other.  ** S
22225 51 4c 69 74 65 20 74 61 62 6c 65 73 29 20 74 68  QLite tables) th
22226 61 74 20 61 72 65 20 69 64 65 6e 74 69 66 69 65  at are identifie
22227 64 20 62 79 20 74 68 65 20 6e 61 6d 65 20 6f 66  d by the name of
22228 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
22229 6c 65 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66  le..  */.#ifndef
2222a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
2222b 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20  TUALTABLE.  if( 
2222c 70 56 54 61 62 20 29 7b 0a 20 20 20 20 69 6e 74  pVTab ){.    int
2222d 20 69 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e   i = ++pParse->n
2222e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Mem;.    sqlite3
2222f 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
22230 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 69 2c 20  _String8, 0, i, 
22231 30 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  0, zName, 0);.  
22232 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22233 4f 70 34 28 76 2c 20 4f 50 5f 56 52 65 6e 61 6d  Op4(v, OP_VRenam
22234 65 2c 20 69 2c 20 30 2c 20 30 2c 28 63 6f 6e 73  e, i, 0, 0,(cons
22235 74 20 63 68 61 72 2a 29 70 56 54 61 62 2c 20 50  t char*)pVTab, P
22236 34 5f 56 54 41 42 29 3b 0a 20 20 20 20 73 71 6c  4_VTAB);.    sql
22237 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61  ite3MayAbort(pPa
22238 72 73 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  rse);.  }.#endif
22239 0a 0a 20 20 2f 2a 20 66 69 67 75 72 65 20 6f 75  ..  /* figure ou
2223a 74 20 68 6f 77 20 6d 61 6e 79 20 55 54 46 2d 38  t how many UTF-8
2223b 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20   characters are 
2223c 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7a 54  in zName */.  zT
2223d 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a  abName = pTab->z
2223e 4e 61 6d 65 3b 0a 20 20 6e 54 61 62 4e 61 6d 65  Name;.  nTabName
2223f 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 43 68   = sqlite3Utf8Ch
22240 61 72 4c 65 6e 28 7a 54 61 62 4e 61 6d 65 2c 20  arLen(zTabName, 
22241 2d 31 29 3b 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66  -1);..  /* Modif
22242 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  y the sqlite_mas
22243 74 65 72 20 74 61 62 6c 65 20 74 6f 20 75 73 65  ter table to use
22244 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e   the new table n
22245 61 6d 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ame. */.  sqlite
22246 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61  3NestedParse(pPa
22247 72 73 65 2c 0a 20 20 20 20 20 20 22 55 50 44 41  rse,.      "UPDA
22248 54 45 20 25 51 2e 25 73 20 53 45 54 20 22 0a 23  TE %Q.%s SET ".#
22249 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
2224a 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 20 20  T_TRIGGER.      
2224b 20 20 20 20 22 73 71 6c 20 3d 20 73 71 6c 69 74      "sql = sqlit
2224c 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 73  e_rename_table(s
2224d 71 6c 2c 20 25 51 29 2c 20 22 0a 23 65 6c 73 65  ql, %Q), ".#else
2224e 0a 20 20 20 20 20 20 20 20 20 20 22 73 71 6c 20  .          "sql 
2224f 3d 20 43 41 53 45 20 22 0a 20 20 20 20 20 20 20  = CASE ".       
22250 20 20 20 20 20 22 57 48 45 4e 20 74 79 70 65 20       "WHEN type 
22251 3d 20 27 74 72 69 67 67 65 72 27 20 54 48 45 4e  = 'trigger' THEN
22252 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74   sqlite_rename_t
22253 72 69 67 67 65 72 28 73 71 6c 2c 20 25 51 29 22  rigger(sql, %Q)"
22254 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 45 4c  .            "EL
22255 53 45 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65  SE sqlite_rename
22256 5f 74 61 62 6c 65 28 73 71 6c 2c 20 25 51 29 20  _table(sql, %Q) 
22257 45 4e 44 2c 20 22 0a 23 65 6e 64 69 66 0a 20 20  END, ".#endif.  
22258 20 20 20 20 20 20 20 20 22 74 62 6c 5f 6e 61 6d          "tbl_nam
22259 65 20 3d 20 25 51 2c 20 22 0a 20 20 20 20 20 20  e = %Q, ".      
2225a 20 20 20 20 22 6e 61 6d 65 20 3d 20 43 41 53 45      "name = CASE
2225b 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22   ".            "
2225c 57 48 45 4e 20 74 79 70 65 3d 27 74 61 62 6c 65  WHEN type='table
2225d 27 20 54 48 45 4e 20 25 51 20 22 0a 20 20 20 20  ' THEN %Q ".    
2225e 20 20 20 20 20 20 20 20 22 57 48 45 4e 20 6e 61          "WHEN na
2225f 6d 65 20 4c 49 4b 45 20 27 73 71 6c 69 74 65 5f  me LIKE 'sqlite_
22260 61 75 74 6f 69 6e 64 65 78 25 25 27 20 41 4e 44  autoindex%%' AND
22261 20 74 79 70 65 3d 27 69 6e 64 65 78 27 20 54 48   type='index' TH
22262 45 4e 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  EN ".           
22263 20 20 22 27 73 71 6c 69 74 65 5f 61 75 74 6f 69    "'sqlite_autoi
22264 6e 64 65 78 5f 27 20 7c 7c 20 25 51 20 7c 7c 20  ndex_' || %Q || 
22265 73 75 62 73 74 72 28 6e 61 6d 65 2c 25 64 2b 31  substr(name,%d+1
22266 38 29 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  8) ".           
22267 20 22 45 4c 53 45 20 6e 61 6d 65 20 45 4e 44 20   "ELSE name END 
22268 22 0a 20 20 20 20 20 20 22 57 48 45 52 45 20 74  ".      "WHERE t
22269 62 6c 5f 6e 61 6d 65 3d 25 51 20 41 4e 44 20 22  bl_name=%Q AND "
2226a 0a 20 20 20 20 20 20 20 20 20 20 22 28 74 79 70  .          "(typ
2226b 65 3d 27 74 61 62 6c 65 27 20 4f 52 20 74 79 70  e='table' OR typ
2226c 65 3d 27 69 6e 64 65 78 27 20 4f 52 20 74 79 70  e='index' OR typ
2226d 65 3d 27 74 72 69 67 67 65 72 27 29 3b 22 2c 20  e='trigger');", 
2226e 0a 20 20 20 20 20 20 7a 44 62 2c 20 53 43 48 45  .      zDb, SCHE
2226f 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 7a  MA_TABLE(iDb), z
22270 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 4e 61  Name, zName, zNa
22271 6d 65 2c 20 0a 23 69 66 6e 64 65 66 20 53 51 4c  me, .#ifndef SQL
22272 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
22273 0a 20 20 20 20 20 20 7a 4e 61 6d 65 2c 0a 23 65  .      zName,.#e
22274 6e 64 69 66 0a 20 20 20 20 20 20 7a 4e 61 6d 65  ndif.      zName
22275 2c 20 6e 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62  , nTabName, zTab
22276 4e 61 6d 65 0a 20 20 29 3b 0a 0a 23 69 66 6e 64  Name.  );..#ifnd
22277 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
22278 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 2f  UTOINCREMENT.  /
22279 2a 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 5f  * If the sqlite_
2227a 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 65  sequence table e
2227b 78 69 73 74 73 20 69 6e 20 74 68 69 73 20 64 61  xists in this da
2227c 74 61 62 61 73 65 2c 20 74 68 65 6e 20 75 70 64  tabase, then upd
2227d 61 74 65 20 0a 20 20 2a 2a 20 69 74 20 77 69 74  ate .  ** it wit
2227e 68 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20  h the new table 
2227f 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  name..  */.  if(
22280 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
22281 65 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 65  e(db, "sqlite_se
22282 71 75 65 6e 63 65 22 2c 20 7a 44 62 29 20 29 7b  quence", zDb) ){
22283 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74  .    sqlite3Nest
22284 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a  edParse(pParse,.
22285 20 20 20 20 20 20 20 20 22 55 50 44 41 54 45 20          "UPDATE 
22286 5c 22 25 77 5c 22 2e 73 71 6c 69 74 65 5f 73 65  \"%w\".sqlite_se
22287 71 75 65 6e 63 65 20 73 65 74 20 6e 61 6d 65 20  quence set name 
22288 3d 20 25 51 20 57 48 45 52 45 20 6e 61 6d 65 20  = %Q WHERE name 
22289 3d 20 25 51 22 2c 0a 20 20 20 20 20 20 20 20 7a  = %Q",.        z
2228a 44 62 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62 2d  Db, zName, pTab-
2228b 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e  >zName);.  }.#en
2228c 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
2228d 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
2228e 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61  .  /* If there a
2228f 72 65 20 54 45 4d 50 20 74 72 69 67 67 65 72 73  re TEMP triggers
22290 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2c 20   on this table, 
22291 6d 6f 64 69 66 79 20 74 68 65 20 73 71 6c 69 74  modify the sqlit
22292 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 0a 20 20  e_temp_master.  
22293 2a 2a 20 74 61 62 6c 65 2e 20 44 6f 6e 27 74 20  ** table. Don't 
22294 64 6f 20 74 68 69 73 20 69 66 20 74 68 65 20 74  do this if the t
22295 61 62 6c 65 20 62 65 69 6e 67 20 41 4c 54 45 52  able being ALTER
22296 65 64 20 69 73 20 69 74 73 65 6c 66 20 6c 6f 63  ed is itself loc
22297 61 74 65 64 20 69 6e 0a 20 20 2a 2a 20 74 68 65  ated in.  ** the
22298 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 2e 0a   temp database..
22299 20 20 2a 2f 0a 20 20 69 66 28 20 28 7a 57 68 65    */.  if( (zWhe
2229a 72 65 3d 77 68 65 72 65 54 65 6d 70 54 72 69 67  re=whereTempTrig
2229b 67 65 72 73 28 70 50 61 72 73 65 2c 20 70 54 61  gers(pParse, pTa
2229c 62 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71  b))!=0 ){.    sq
2229d 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
2229e 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20  (pParse, .      
2229f 20 20 22 55 50 44 41 54 45 20 73 71 6c 69 74 65    "UPDATE sqlite
222a0 5f 74 65 6d 70 5f 6d 61 73 74 65 72 20 53 45 54  _temp_master SET
222a1 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22   ".            "
222a2 73 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e  sql = sqlite_ren
222a3 61 6d 65 5f 74 72 69 67 67 65 72 28 73 71 6c 2c  ame_trigger(sql,
222a4 20 25 51 29 2c 20 22 0a 20 20 20 20 20 20 20 20   %Q), ".        
222a5 20 20 20 20 22 74 62 6c 5f 6e 61 6d 65 20 3d 20      "tbl_name = 
222a6 25 51 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  %Q ".           
222a7 20 22 57 48 45 52 45 20 25 73 3b 22 2c 20 7a 4e   "WHERE %s;", zN
222a8 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 57 68 65  ame, zName, zWhe
222a9 72 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  re);.    sqlite3
222aa 44 62 46 72 65 65 28 64 62 2c 20 7a 57 68 65 72  DbFree(db, zWher
222ab 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  e);.  }.#endif..
222ac 20 20 2f 2a 20 44 72 6f 70 20 61 6e 64 20 72 65    /* Drop and re
222ad 6c 6f 61 64 20 74 68 65 20 69 6e 74 65 72 6e 61  load the interna
222ae 6c 20 74 61 62 6c 65 20 73 63 68 65 6d 61 2e 20  l table schema. 
222af 2a 2f 0a 20 20 72 65 6c 6f 61 64 54 61 62 6c 65  */.  reloadTable
222b0 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 70  Schema(pParse, p
222b1 54 61 62 2c 20 7a 4e 61 6d 65 29 3b 0a 0a 65 78  Tab, zName);..ex
222b2 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3a  it_rename_table:
222b3 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
222b4 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72 63  tDelete(db, pSrc
222b5 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
222b6 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 7d  ee(db, zName);.}
222b7 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  .../*.** Generat
222b8 65 20 63 6f 64 65 20 74 6f 20 6d 61 6b 65 20 73  e code to make s
222b9 75 72 65 20 74 68 65 20 66 69 6c 65 20 66 6f 72  ure the file for
222ba 6d 61 74 20 6e 75 6d 62 65 72 20 69 73 20 61 74  mat number is at
222bb 20 6c 65 61 73 74 20 6d 69 6e 46 6f 72 6d 61 74   least minFormat
222bc 2e 0a 2a 2a 20 54 68 65 20 67 65 6e 65 72 61 74  ..** The generat
222bd 65 64 20 63 6f 64 65 20 77 69 6c 6c 20 69 6e 63  ed code will inc
222be 72 65 61 73 65 20 74 68 65 20 66 69 6c 65 20 66  rease the file f
222bf 6f 72 6d 61 74 20 6e 75 6d 62 65 72 20 69 66 20  ormat number if 
222c0 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 53 51  necessary..*/.SQ
222c1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
222c2 64 20 73 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d  d sqlite3Minimum
222c3 46 69 6c 65 46 6f 72 6d 61 74 28 50 61 72 73 65  FileFormat(Parse
222c4 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44   *pParse, int iD
222c5 62 2c 20 69 6e 74 20 6d 69 6e 46 6f 72 6d 61 74  b, int minFormat
222c6 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  ){.  Vdbe *v;.  
222c7 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
222c8 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 2f 2a  be(pParse);.  /*
222c9 20 54 68 65 20 56 44 42 45 20 73 68 6f 75 6c 64   The VDBE should
222ca 20 68 61 76 65 20 62 65 65 6e 20 61 6c 6c 6f 63   have been alloc
222cb 61 74 65 64 20 62 65 66 6f 72 65 20 74 68 69 73  ated before this
222cc 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
222cd 65 64 2e 0a 20 20 2a 2a 20 49 66 20 74 68 61 74  ed..  ** If that
222ce 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c   allocation fail
222cf 65 64 2c 20 77 65 20 77 6f 75 6c 64 20 68 61 76  ed, we would hav
222d0 65 20 71 75 69 74 20 62 65 66 6f 72 65 20 72 65  e quit before re
222d1 61 63 68 69 6e 67 20 74 68 69 73 0a 20 20 2a 2a  aching this.  **
222d2 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 69 66 28 20   point */.  if( 
222d3 41 4c 57 41 59 53 28 76 29 20 29 7b 0a 20 20 20  ALWAYS(v) ){.   
222d4 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65   int r1 = sqlite
222d5 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
222d6 73 65 29 3b 0a 20 20 20 20 69 6e 74 20 72 32 20  se);.    int r2 
222d7 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
222d8 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
222d9 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20 73 71 6c   int j1;.    sql
222da 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
222db 2c 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c  , OP_ReadCookie,
222dc 20 69 44 62 2c 20 72 31 2c 20 42 54 52 45 45 5f   iDb, r1, BTREE_
222dd 46 49 4c 45 5f 46 4f 52 4d 41 54 29 3b 0a 20 20  FILE_FORMAT);.  
222de 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65    sqlite3VdbeUse
222df 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a  sBtree(v, iDb);.
222e0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
222e1 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
222e2 67 65 72 2c 20 6d 69 6e 46 6f 72 6d 61 74 2c 20  ger, minFormat, 
222e3 72 32 29 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71  r2);.    j1 = sq
222e4 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
222e5 76 2c 20 4f 50 5f 47 65 2c 20 72 32 2c 20 30 2c  v, OP_Ge, r2, 0,
222e6 20 72 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   r1);.    sqlite
222e7 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
222e8 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62  P_SetCookie, iDb
222e9 2c 20 42 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52  , BTREE_FILE_FOR
222ea 4d 41 54 2c 20 72 32 29 3b 0a 20 20 20 20 73 71  MAT, r2);.    sq
222eb 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
222ec 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 73 71  e(v, j1);.    sq
222ed 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
222ee 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
222ef 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65  .    sqlite3Rele
222f0 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
222f1 65 2c 20 72 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  e, r2);.  }.}../
222f2 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
222f3 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74  on is called aft
222f4 65 72 20 61 6e 20 22 41 4c 54 45 52 20 54 41 42  er an "ALTER TAB
222f5 4c 45 20 2e 2e 2e 20 41 44 44 22 20 73 74 61 74  LE ... ADD" stat
222f6 65 6d 65 6e 74 0a 2a 2a 20 68 61 73 20 62 65 65  ement.** has bee
222f7 6e 20 70 61 72 73 65 64 2e 20 41 72 67 75 6d 65  n parsed. Argume
222f8 6e 74 20 70 43 6f 6c 44 65 66 20 63 6f 6e 74 61  nt pColDef conta
222f9 69 6e 73 20 74 68 65 20 74 65 78 74 20 6f 66 20  ins the text of 
222fa 74 68 65 20 6e 65 77 0a 2a 2a 20 63 6f 6c 75 6d  the new.** colum
222fb 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a  n definition..**
222fc 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65 20 73 74  .** The Table st
222fd 72 75 63 74 75 72 65 20 70 50 61 72 73 65 2d 3e  ructure pParse->
222fe 70 4e 65 77 54 61 62 6c 65 20 77 61 73 20 65 78  pNewTable was ex
222ff 74 65 6e 64 65 64 20 74 6f 20 69 6e 63 6c 75 64  tended to includ
22300 65 0a 2a 2a 20 74 68 65 20 6e 65 77 20 63 6f 6c  e.** the new col
22301 75 6d 6e 20 64 75 72 69 6e 67 20 70 61 72 73 69  umn during parsi
22302 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ng..*/.SQLITE_PR
22303 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
22304 65 33 41 6c 74 65 72 46 69 6e 69 73 68 41 64 64  e3AlterFinishAdd
22305 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 70 50  Column(Parse *pP
22306 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 43 6f  arse, Token *pCo
22307 6c 44 65 66 29 7b 0a 20 20 54 61 62 6c 65 20 2a  lDef){.  Table *
22308 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 20  pNew;           
22309 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70 50     /* Copy of pP
2230a 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20  arse->pNewTable 
2230b 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
2230c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2230d 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 20 61 6c  * Table being al
2230e 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  tered */.  int i
2230f 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
22310 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
22311 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 63 6f 6e   number */.  con
22312 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20  st char *zDb;   
22313 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
22314 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e  se name */.  con
22315 73 74 20 63 68 61 72 20 2a 7a 54 61 62 3b 20 20  st char *zTab;  
22316 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
22317 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  name */.  char *
22318 7a 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  zCol;           
22319 20 20 20 20 2f 2a 20 4e 75 6c 6c 2d 74 65 72 6d      /* Null-term
2231a 69 6e 61 74 65 64 20 63 6f 6c 75 6d 6e 20 64 65  inated column de
2231b 66 69 6e 69 74 69 6f 6e 20 2a 2f 0a 20 20 43 6f  finition */.  Co
2231c 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 20 20 20 20 20  lumn *pCol;     
2231d 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e          /* The n
2231e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45  ew column */.  E
2231f 78 70 72 20 2a 70 44 66 6c 74 3b 20 20 20 20 20  xpr *pDflt;     
22320 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61           /* Defa
22321 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68  ult value for th
22322 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a  e new column */.
22323 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
22324 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
22325 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
22326 65 63 74 69 6f 6e 3b 20 2a 2f 0a 0a 20 20 64 62  ection; */..  db
22327 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
22328 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
22329 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  r || db->mallocF
2232a 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a  ailed ) return;.
2232b 20 20 70 4e 65 77 20 3d 20 70 50 61 72 73 65 2d    pNew = pParse-
2232c 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 61 73  >pNewTable;.  as
2232d 73 65 72 74 28 20 70 4e 65 77 20 29 3b 0a 0a 20  sert( pNew );.. 
2232e 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
2232f 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74  BtreeHoldsAllMut
22330 65 78 65 73 28 64 62 29 20 29 3b 0a 20 20 69 44  exes(db) );.  iD
22331 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
22332 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 4e 65  aToIndex(db, pNe
22333 77 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 7a  w->pSchema);.  z
22334 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  Db = db->aDb[iDb
22335 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7a 54 61 62 20  ].zName;.  zTab 
22336 3d 20 26 70 4e 65 77 2d 3e 7a 4e 61 6d 65 5b 31  = &pNew->zName[1
22337 36 5d 3b 20 20 2f 2a 20 53 6b 69 70 20 74 68 65  6];  /* Skip the
22338 20 22 73 71 6c 69 74 65 5f 61 6c 74 65 72 74 61   "sqlite_alterta
22339 62 5f 22 20 70 72 65 66 69 78 20 6f 6e 20 74 68  b_" prefix on th
2233a 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 70 43 6f 6c  e name */.  pCol
2233b 20 3d 20 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b 70   = &pNew->aCol[p
2233c 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 5d 3b 0a 20 20  New->nCol-1];.  
2233d 70 44 66 6c 74 20 3d 20 70 43 6f 6c 2d 3e 70 44  pDflt = pCol->pD
2233e 66 6c 74 3b 0a 20 20 70 54 61 62 20 3d 20 73 71  flt;.  pTab = sq
2233f 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
22340 62 2c 20 7a 54 61 62 2c 20 7a 44 62 29 3b 0a 20  b, zTab, zDb);. 
22341 20 61 73 73 65 72 74 28 20 70 54 61 62 20 29 3b   assert( pTab );
22342 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
22343 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
22344 49 4f 4e 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20  ION.  /* Invoke 
22345 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f  the authorizatio
22346 6e 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20  n callback. */. 
22347 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68   if( sqlite3Auth
22348 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51  Check(pParse, SQ
22349 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45  LITE_ALTER_TABLE
2234a 2c 20 7a 44 62 2c 20 70 54 61 62 2d 3e 7a 4e 61  , zDb, pTab->zNa
2234b 6d 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65  me, 0) ){.    re
2234c 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  turn;.  }.#endif
2234d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 65  ..  /* If the de
2234e 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20  fault value for 
2234f 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 77  the new column w
22350 61 73 20 73 70 65 63 69 66 69 65 64 20 77 69 74  as specified wit
22351 68 20 61 20 0a 20 20 2a 2a 20 6c 69 74 65 72 61  h a .  ** litera
22352 6c 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 73 65 74  l NULL, then set
22353 20 70 44 66 6c 74 20 74 6f 20 30 2e 20 54 68 69   pDflt to 0. Thi
22354 73 20 73 69 6d 70 6c 69 66 69 65 73 20 63 68 65  s simplifies che
22355 63 6b 69 6e 67 0a 20 20 2a 2a 20 66 6f 72 20 61  cking.  ** for a
22356 6e 20 53 51 4c 20 4e 55 4c 4c 20 64 65 66 61 75  n SQL NULL defau
22357 6c 74 20 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a 20  lt below..  */. 
22358 20 69 66 28 20 70 44 66 6c 74 20 26 26 20 70 44   if( pDflt && pD
22359 66 6c 74 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c  flt->op==TK_NULL
2235a 20 29 7b 0a 20 20 20 20 70 44 66 6c 74 20 3d 20   ){.    pDflt = 
2235b 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65  0;.  }..  /* Che
2235c 63 6b 20 74 68 61 74 20 74 68 65 20 6e 65 77 20  ck that the new 
2235d 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 73 70  column is not sp
2235e 65 63 69 66 69 65 64 20 61 73 20 50 52 49 4d 41  ecified as PRIMA
2235f 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45  RY KEY or UNIQUE
22360 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 72 65 20  ..  ** If there 
22361 69 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f  is a NOT NULL co
22362 6e 73 74 72 61 69 6e 74 2c 20 74 68 65 6e 20 74  nstraint, then t
22363 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
22364 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 63 6f   for the.  ** co
22365 6c 75 6d 6e 20 6d 75 73 74 20 6e 6f 74 20 62 65  lumn must not be
22366 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 69 66   NULL..  */.  if
22367 28 20 70 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b 65  ( pCol->isPrimKe
22368 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  y ){.    sqlite3
22369 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2236a 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 50   "Cannot add a P
2236b 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d  RIMARY KEY colum
2236c 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  n");.    return;
2236d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e 65 77 2d  .  }.  if( pNew-
2236e 3e 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 73  >pIndex ){.    s
2236f 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
22370 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61  Parse, "Cannot a
22371 64 64 20 61 20 55 4e 49 51 55 45 20 63 6f 6c 75  dd a UNIQUE colu
22372 6d 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  mn");.    return
22373 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 6f 6c  ;.  }.  if( pCol
22374 2d 3e 6e 6f 74 4e 75 6c 6c 20 26 26 20 21 70 44  ->notNull && !pD
22375 66 6c 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  flt ){.    sqlit
22376 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
22377 65 2c 20 0a 20 20 20 20 20 20 20 20 22 43 61 6e  e, .        "Can
22378 6e 6f 74 20 61 64 64 20 61 20 4e 4f 54 20 4e 55  not add a NOT NU
22379 4c 4c 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 64  LL column with d
2237a 65 66 61 75 6c 74 20 76 61 6c 75 65 20 4e 55 4c  efault value NUL
2237b 4c 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  L");.    return;
2237c 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 72  .  }..  /* Ensur
2237d 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 65 78  e the default ex
2237e 70 72 65 73 73 69 6f 6e 20 69 73 20 73 6f 6d 65  pression is some
2237f 74 68 69 6e 67 20 74 68 61 74 20 73 71 6c 69 74  thing that sqlit
22380 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28  e3ValueFromExpr(
22381 29 0a 20 20 2a 2a 20 63 61 6e 20 68 61 6e 64 6c  ).  ** can handl
22382 65 20 28 69 2e 65 2e 20 6e 6f 74 20 43 55 52 52  e (i.e. not CURR
22383 45 4e 54 5f 54 49 4d 45 20 65 74 63 2e 29 0a 20  ENT_TIME etc.). 
22384 20 2a 2f 0a 20 20 69 66 28 20 70 44 66 6c 74 20   */.  if( pDflt 
22385 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  ){.    sqlite3_v
22386 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20 20 20 20  alue *pVal;.    
22387 69 66 28 20 73 71 6c 69 74 65 33 56 61 6c 75 65  if( sqlite3Value
22388 46 72 6f 6d 45 78 70 72 28 64 62 2c 20 70 44 66  FromExpr(db, pDf
22389 6c 74 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  lt, SQLITE_UTF8,
2238a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45   SQLITE_AFF_NONE
2238b 2c 20 26 70 56 61 6c 29 20 29 7b 0a 20 20 20 20  , &pVal) ){.    
2238c 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
2238d 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65  ed = 1;.      re
2238e 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20  turn;.    }.    
2238f 69 66 28 20 21 70 56 61 6c 20 29 7b 0a 20 20 20  if( !pVal ){.   
22390 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
22391 73 67 28 70 50 61 72 73 65 2c 20 22 43 61 6e 6e  sg(pParse, "Cann
22392 6f 74 20 61 64 64 20 61 20 63 6f 6c 75 6d 6e 20  ot add a column 
22393 77 69 74 68 20 6e 6f 6e 2d 63 6f 6e 73 74 61 6e  with non-constan
22394 74 20 64 65 66 61 75 6c 74 22 29 3b 0a 20 20 20  t default");.   
22395 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
22396 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75  .    sqlite3Valu
22397 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20 7d  eFree(pVal);.  }
22398 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66 79 20 74 68  ..  /* Modify th
22399 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  e CREATE TABLE s
2239a 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 7a  tatement. */.  z
2239b 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 53  Col = sqlite3DbS
2239c 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72  trNDup(db, (char
2239d 2a 29 70 43 6f 6c 44 65 66 2d 3e 7a 2c 20 70 43  *)pColDef->z, pC
2239e 6f 6c 44 65 66 2d 3e 6e 29 3b 0a 20 20 69 66 28  olDef->n);.  if(
2239f 20 7a 43 6f 6c 20 29 7b 0a 20 20 20 20 63 68 61   zCol ){.    cha
223a0 72 20 2a 7a 45 6e 64 20 3d 20 26 7a 43 6f 6c 5b  r *zEnd = &zCol[
223a1 70 43 6f 6c 44 65 66 2d 3e 6e 2d 31 5d 3b 0a 20  pColDef->n-1];. 
223a2 20 20 20 77 68 69 6c 65 28 20 7a 45 6e 64 3e 7a     while( zEnd>z
223a3 43 6f 6c 20 26 26 20 28 2a 7a 45 6e 64 3d 3d 27  Col && (*zEnd=='
223a4 3b 27 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 73  ;' || sqlite3Iss
223a5 70 61 63 65 28 2a 7a 45 6e 64 29 29 20 29 7b 0a  pace(*zEnd)) ){.
223a6 20 20 20 20 20 20 2a 7a 45 6e 64 2d 2d 20 3d 20        *zEnd-- = 
223a7 27 5c 30 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20  '\0';.    }.    
223a8 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
223a9 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  se(pParse, .    
223aa 20 20 20 20 22 55 50 44 41 54 45 20 5c 22 25 77      "UPDATE \"%w
223ab 5c 22 2e 25 73 20 53 45 54 20 22 0a 20 20 20 20  \".%s SET ".    
223ac 20 20 20 20 20 20 22 73 71 6c 20 3d 20 73 75 62        "sql = sub
223ad 73 74 72 28 73 71 6c 2c 31 2c 25 64 29 20 7c 7c  str(sql,1,%d) ||
223ae 20 27 2c 20 27 20 7c 7c 20 25 51 20 7c 7c 20 73   ', ' || %Q || s
223af 75 62 73 74 72 28 73 71 6c 2c 25 64 29 20 22 0a  ubstr(sql,%d) ".
223b0 20 20 20 20 20 20 20 20 22 57 48 45 52 45 20 74          "WHERE t
223b1 79 70 65 20 3d 20 27 74 61 62 6c 65 27 20 41 4e  ype = 'table' AN
223b2 44 20 6e 61 6d 65 20 3d 20 25 51 22 2c 20 0a 20  D name = %Q", . 
223b3 20 20 20 20 20 7a 44 62 2c 20 53 43 48 45 4d 41       zDb, SCHEMA
223b4 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 70 4e 65  _TABLE(iDb), pNe
223b5 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 2c  w->addColOffset,
223b6 20 7a 43 6f 6c 2c 20 70 4e 65 77 2d 3e 61 64 64   zCol, pNew->add
223b7 43 6f 6c 4f 66 66 73 65 74 2b 31 2c 0a 20 20 20  ColOffset+1,.   
223b8 20 20 20 7a 54 61 62 0a 20 20 20 20 29 3b 0a 20     zTab.    );. 
223b9 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
223ba 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 7d 0a  (db, zCol);.  }.
223bb 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 65 66  .  /* If the def
223bc 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 74 68  ault value of th
223bd 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69 73 20  e new column is 
223be 4e 55 4c 4c 2c 20 74 68 65 6e 20 73 65 74 20 74  NULL, then set t
223bf 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 66 6f 72  he file.  ** for
223c0 6d 61 74 20 74 6f 20 32 2e 20 49 66 20 74 68 65  mat to 2. If the
223c1 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f   default value o
223c2 66 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e  f the new column
223c3 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 0a 20 20   is not NULL,.  
223c4 2a 2a 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d  ** the file form
223c5 61 74 20 62 65 63 6f 6d 65 73 20 33 2e 0a 20 20  at becomes 3..  
223c6 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4d 69 6e 69  */.  sqlite3Mini
223c7 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28 70 50  mumFileFormat(pP
223c8 61 72 73 65 2c 20 69 44 62 2c 20 70 44 66 6c 74  arse, iDb, pDflt
223c9 20 3f 20 33 20 3a 20 32 29 3b 0a 0a 20 20 2f 2a   ? 3 : 2);..  /*
223ca 20 52 65 6c 6f 61 64 20 74 68 65 20 73 63 68 65   Reload the sche
223cb 6d 61 20 6f 66 20 74 68 65 20 6d 6f 64 69 66 69  ma of the modifi
223cc 65 64 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 72  ed table. */.  r
223cd 65 6c 6f 61 64 54 61 62 6c 65 53 63 68 65 6d 61  eloadTableSchema
223ce 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70  (pParse, pTab, p
223cf 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a  Tab->zName);.}..
223d0 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
223d1 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79  ion is called by
223d2 20 74 68 65 20 70 61 72 73 65 72 20 61 66 74 65   the parser afte
223d3 72 20 74 68 65 20 74 61 62 6c 65 2d 6e 61 6d 65  r the table-name
223d4 20 69 6e 0a 2a 2a 20 61 6e 20 22 41 4c 54 45 52   in.** an "ALTER
223d5 20 54 41 42 4c 45 20 3c 74 61 62 6c 65 2d 6e 61   TABLE <table-na
223d6 6d 65 3e 20 41 44 44 22 20 73 74 61 74 65 6d 65  me> ADD" stateme
223d7 6e 74 20 69 73 20 70 61 72 73 65 64 2e 20 41 72  nt is parsed. Ar
223d8 67 75 6d 65 6e 74 20 0a 2a 2a 20 70 53 72 63 20  gument .** pSrc 
223d9 69 73 20 74 68 65 20 66 75 6c 6c 2d 6e 61 6d 65  is the full-name
223da 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 62 65   of the table be
223db 69 6e 67 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a  ing altered..**.
223dc 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
223dd 6d 61 6b 65 73 20 61 20 28 70 61 72 74 69 61 6c  makes a (partial
223de 29 20 63 6f 70 79 20 6f 66 20 74 68 65 20 54 61  ) copy of the Ta
223df 62 6c 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a  ble structure.**
223e0 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 62   for the table b
223e1 65 69 6e 67 20 61 6c 74 65 72 65 64 20 61 6e 64  eing altered and
223e2 20 73 65 74 73 20 50 61 72 73 65 2e 70 4e 65 77   sets Parse.pNew
223e3 54 61 62 6c 65 20 74 6f 20 70 6f 69 6e 74 0a 2a  Table to point.*
223e4 2a 20 74 6f 20 69 74 2e 20 52 6f 75 74 69 6e 65  * to it. Routine
223e5 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  s called by the 
223e6 70 61 72 73 65 72 20 61 73 20 74 68 65 20 63 6f  parser as the co
223e7 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 0a  lumn definition.
223e8 2a 2a 20 69 73 20 70 61 72 73 65 64 20 28 69 2e  ** is parsed (i.
223e9 65 2e 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c  e. sqlite3AddCol
223ea 75 6d 6e 28 29 29 20 61 64 64 20 74 68 65 20 6e  umn()) add the n
223eb 65 77 20 43 6f 6c 75 6d 6e 20 64 61 74 61 20 74  ew Column data t
223ec 6f 20 0a 2a 2a 20 74 68 65 20 63 6f 70 79 2e 20  o .** the copy. 
223ed 54 68 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20  The copy of the 
223ee 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20  Table structure 
223ef 69 73 20 64 65 6c 65 74 65 64 20 62 79 20 74 6f  is deleted by to
223f0 6b 65 6e 69 7a 65 2e 63 20 0a 2a 2a 20 61 66 74  kenize.c .** aft
223f1 65 72 20 70 61 72 73 69 6e 67 20 69 73 20 66 69  er parsing is fi
223f2 6e 69 73 68 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 6f  nished..**.** Ro
223f3 75 74 69 6e 65 20 73 71 6c 69 74 65 33 41 6c 74  utine sqlite3Alt
223f4 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d  erFinishAddColum
223f5 6e 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c  n() will be call
223f6 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65 0a 2a  ed to complete.*
223f7 2a 20 63 6f 64 69 6e 67 20 74 68 65 20 22 41 4c  * coding the "AL
223f8 54 45 52 20 54 41 42 4c 45 20 2e 2e 2e 20 41 44  TER TABLE ... AD
223f9 44 22 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f  D" statement..*/
223fa 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
223fb 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65  void sqlite3Alte
223fc 72 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d 6e 28  rBeginAddColumn(
223fd 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53  Parse *pParse, S
223fe 72 63 4c 69 73 74 20 2a 70 53 72 63 29 7b 0a 20  rcList *pSrc){. 
223ff 20 54 61 62 6c 65 20 2a 70 4e 65 77 3b 0a 20 20   Table *pNew;.  
22400 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56  Table *pTab;.  V
22401 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69 44  dbe *v;.  int iD
22402 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  b;.  int i;.  in
22403 74 20 6e 41 6c 6c 6f 63 3b 0a 20 20 73 71 6c 69  t nAlloc;.  sqli
22404 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
22405 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b  ->db;..  /* Look
22406 20 75 70 20 74 68 65 20 74 61 62 6c 65 20 62 65   up the table be
22407 69 6e 67 20 61 6c 74 65 72 65 64 2e 20 2a 2f 0a  ing altered. */.
22408 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
22409 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d 30 20 29  ->pNewTable==0 )
2240a 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
2240b 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c  te3BtreeHoldsAll
2240c 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b 0a 20  Mutexes(db) );. 
2240d 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
2240e 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 65 78 69  ailed ) goto exi
2240f 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75  t_begin_add_colu
22410 6d 6e 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c  mn;.  pTab = sql
22411 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
22412 70 50 61 72 73 65 2c 20 30 2c 20 70 53 72 63 2d  pParse, 0, pSrc-
22413 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 53 72  >a[0].zName, pSr
22414 63 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73  c->a[0].zDatabas
22415 65 29 3b 0a 20 20 69 66 28 20 21 70 54 61 62 20  e);.  if( !pTab 
22416 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69  ) goto exit_begi
22417 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 0a 23  n_add_column;..#
22418 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
22419 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
2241a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
2241b 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73 71 6c  pTab) ){.    sql
2241c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2241d 72 73 65 2c 20 22 76 69 72 74 75 61 6c 20 74 61  rse, "virtual ta
2241e 62 6c 65 73 20 6d 61 79 20 6e 6f 74 20 62 65 20  bles may not be 
2241f 61 6c 74 65 72 65 64 22 29 3b 0a 20 20 20 20 67  altered");.    g
22420 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61  oto exit_begin_a
22421 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 23  dd_column;.  }.#
22422 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d 61 6b 65  endif..  /* Make
22423 20 73 75 72 65 20 74 68 69 73 20 69 73 20 6e 6f   sure this is no
22424 74 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20  t an attempt to 
22425 41 4c 54 45 52 20 61 20 76 69 65 77 2e 20 2a 2f  ALTER a view. */
22426 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65  .  if( pTab->pSe
22427 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  lect ){.    sqli
22428 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
22429 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20  se, "Cannot add 
2242a 61 20 63 6f 6c 75 6d 6e 20 74 6f 20 61 20 76 69  a column to a vi
2242b 65 77 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  ew");.    goto e
2242c 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f  xit_begin_add_co
2242d 6c 75 6d 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  lumn;.  }..  ass
2242e 65 72 74 28 20 70 54 61 62 2d 3e 61 64 64 43 6f  ert( pTab->addCo
2242f 6c 4f 66 66 73 65 74 3e 30 20 29 3b 0a 20 20 69  lOffset>0 );.  i
22430 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
22431 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54  maToIndex(db, pT
22432 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 20  ab->pSchema);.. 
22433 20 2f 2a 20 50 75 74 20 61 20 63 6f 70 79 20 6f   /* Put a copy o
22434 66 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75  f the Table stru
22435 63 74 20 69 6e 20 50 61 72 73 65 2e 70 4e 65 77  ct in Parse.pNew
22436 54 61 62 6c 65 20 66 6f 72 20 74 68 65 0a 20 20  Table for the.  
22437 2a 2a 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c  ** sqlite3AddCol
22438 75 6d 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20 61  umn() function a
22439 6e 64 20 66 72 69 65 6e 64 73 20 74 6f 20 6d 6f  nd friends to mo
2243a 64 69 66 79 2e 20 20 42 75 74 20 6d 6f 64 69 66  dify.  But modif
2243b 79 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65 20  y.  ** the name 
2243c 62 79 20 61 64 64 69 6e 67 20 61 6e 20 22 73 71  by adding an "sq
2243d 6c 69 74 65 5f 61 6c 74 65 72 74 61 62 5f 22 20  lite_altertab_" 
2243e 70 72 65 66 69 78 2e 20 20 42 79 20 61 64 64 69  prefix.  By addi
2243f 6e 67 20 74 68 69 73 0a 20 20 2a 2a 20 70 72 65  ng this.  ** pre
22440 66 69 78 2c 20 77 65 20 69 6e 73 75 72 65 20 74  fix, we insure t
22441 68 61 74 20 74 68 65 20 6e 61 6d 65 20 77 69 6c  hat the name wil
22442 6c 20 6e 6f 74 20 63 6f 6c 6c 69 64 65 20 77 69  l not collide wi
22443 74 68 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 20  th an existing. 
22444 20 2a 2a 20 74 61 62 6c 65 20 62 65 63 61 75 73   ** table becaus
22445 65 20 75 73 65 72 20 74 61 62 6c 65 20 61 72 65  e user table are
22446 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20   not allowed to 
22447 68 61 76 65 20 74 68 65 20 22 73 71 6c 69 74 65  have the "sqlite
22448 5f 22 0a 20 20 2a 2a 20 70 72 65 66 69 78 20 6f  _".  ** prefix o
22449 6e 20 74 68 65 69 72 20 6e 61 6d 65 2e 0a 20 20  n their name..  
2244a 2a 2f 0a 20 20 70 4e 65 77 20 3d 20 28 54 61 62  */.  pNew = (Tab
2244b 6c 65 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c  le*)sqlite3DbMal
2244c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
2244d 6f 66 28 54 61 62 6c 65 29 29 3b 0a 20 20 69 66  of(Table));.  if
2244e 28 20 21 70 4e 65 77 20 29 20 67 6f 74 6f 20 65  ( !pNew ) goto e
2244f 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f  xit_begin_add_co
22450 6c 75 6d 6e 3b 0a 20 20 70 50 61 72 73 65 2d 3e  lumn;.  pParse->
22451 70 4e 65 77 54 61 62 6c 65 20 3d 20 70 4e 65 77  pNewTable = pNew
22452 3b 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d  ;.  pNew->nRef =
22453 20 31 3b 0a 20 20 70 4e 65 77 2d 3e 64 62 4d 65   1;.  pNew->dbMe
22454 6d 20 3d 20 70 54 61 62 2d 3e 64 62 4d 65 6d 3b  m = pTab->dbMem;
22455 0a 20 20 70 4e 65 77 2d 3e 6e 43 6f 6c 20 3d 20  .  pNew->nCol = 
22456 70 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 61 73  pTab->nCol;.  as
22457 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43 6f 6c  sert( pNew->nCol
22458 3e 30 20 29 3b 0a 20 20 6e 41 6c 6c 6f 63 20 3d  >0 );.  nAlloc =
22459 20 28 28 28 70 4e 65 77 2d 3e 6e 43 6f 6c 2d 31   (((pNew->nCol-1
2245a 29 2f 38 29 2a 38 29 2b 38 3b 0a 20 20 61 73 73  )/8)*8)+8;.  ass
2245b 65 72 74 28 20 6e 41 6c 6c 6f 63 3e 3d 70 4e 65  ert( nAlloc>=pNe
2245c 77 2d 3e 6e 43 6f 6c 20 26 26 20 6e 41 6c 6c 6f  w->nCol && nAllo
2245d 63 25 38 3d 3d 30 20 26 26 20 6e 41 6c 6c 6f 63  c%8==0 && nAlloc
2245e 2d 70 4e 65 77 2d 3e 6e 43 6f 6c 3c 38 20 29 3b  -pNew->nCol<8 );
2245f 0a 20 20 70 4e 65 77 2d 3e 61 43 6f 6c 20 3d 20  .  pNew->aCol = 
22460 28 43 6f 6c 75 6d 6e 2a 29 73 71 6c 69 74 65 33  (Column*)sqlite3
22461 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
22462 20 73 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a   sizeof(Column)*
22463 6e 41 6c 6c 6f 63 29 3b 0a 20 20 70 4e 65 77 2d  nAlloc);.  pNew-
22464 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  >zName = sqlite3
22465 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 73 71 6c  MPrintf(db, "sql
22466 69 74 65 5f 61 6c 74 65 72 74 61 62 5f 25 73 22  ite_altertab_%s"
22467 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
22468 20 20 69 66 28 20 21 70 4e 65 77 2d 3e 61 43 6f    if( !pNew->aCo
22469 6c 20 7c 7c 20 21 70 4e 65 77 2d 3e 7a 4e 61 6d  l || !pNew->zNam
2246a 65 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c  e ){.    db->mal
2246b 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
2246c 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67     goto exit_beg
2246d 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20  in_add_column;. 
2246e 20 7d 0a 20 20 6d 65 6d 63 70 79 28 70 4e 65 77   }.  memcpy(pNew
2246f 2d 3e 61 43 6f 6c 2c 20 70 54 61 62 2d 3e 61 43  ->aCol, pTab->aC
22470 6f 6c 2c 20 73 69 7a 65 6f 66 28 43 6f 6c 75 6d  ol, sizeof(Colum
22471 6e 29 2a 70 4e 65 77 2d 3e 6e 43 6f 6c 29 3b 0a  n)*pNew->nCol);.
22472 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4e 65    for(i=0; i<pNe
22473 77 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20  w->nCol; i++){. 
22474 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20     Column *pCol 
22475 3d 20 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b 69 5d  = &pNew->aCol[i]
22476 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d  ;.    pCol->zNam
22477 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  e = sqlite3DbStr
22478 44 75 70 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 4e  Dup(db, pCol->zN
22479 61 6d 65 29 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e  ame);.    pCol->
2247a 7a 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 70  zColl = 0;.    p
2247b 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 30 3b 0a  Col->zType = 0;.
2247c 20 20 20 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 20      pCol->pDflt 
2247d 3d 20 30 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a  = 0;.    pCol->z
2247e 44 66 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  Dflt = 0;.  }.  
2247f 70 4e 65 77 2d 3e 70 53 63 68 65 6d 61 20 3d 20  pNew->pSchema = 
22480 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
22481 68 65 6d 61 3b 0a 20 20 70 4e 65 77 2d 3e 61 64  hema;.  pNew->ad
22482 64 43 6f 6c 4f 66 66 73 65 74 20 3d 20 70 54 61  dColOffset = pTa
22483 62 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 3b  b->addColOffset;
22484 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d 20  .  pNew->nRef = 
22485 31 3b 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61  1;..  /* Begin a
22486 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64   transaction and
22487 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 73   increment the s
22488 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 20 20 2a  chema cookie.  *
22489 2f 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e  /.  sqlite3Begin
2248a 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70  WriteOperation(p
2248b 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a  Parse, 0, iDb);.
2248c 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
2248d 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
2248e 69 66 28 20 21 76 20 29 20 67 6f 74 6f 20 65 78  if( !v ) goto ex
2248f 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c  it_begin_add_col
22490 75 6d 6e 3b 0a 20 20 73 71 6c 69 74 65 33 43 68  umn;.  sqlite3Ch
22491 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73  angeCookie(pPars
22492 65 2c 20 69 44 62 29 3b 0a 0a 65 78 69 74 5f 62  e, iDb);..exit_b
22493 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3a  egin_add_column:
22494 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
22495 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72 63  tDelete(db, pSrc
22496 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 23  );.  return;.}.#
22497 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 45  endif  /* SQLITE
22498 5f 41 4c 54 45 52 5f 54 41 42 4c 45 20 2a 2f 0a  _ALTER_TABLE */.
22499 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2249a 20 45 6e 64 20 6f 66 20 61 6c 74 65 72 2e 63 20   End of alter.c 
2249b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2249c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2249d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2249e 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2249f 20 42 65 67 69 6e 20 66 69 6c 65 20 61 6e 61 6c   Begin file anal
224a0 79 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  yze.c **********
224a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
224a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
224a3 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4a 75 6c 79  ./*.** 2005 July
224a4 20 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74   8.**.** The aut
224a5 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
224a6 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
224a7 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
224a8 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
224a9 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
224aa 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
224ab 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
224ac 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
224ad 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
224ae 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
224af 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
224b0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
224b1 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
224b2 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
224b3 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
224b4 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
224b5 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
224b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
224b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
224b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
224b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
224ba 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
224bb 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 61 73 73  ontains code ass
224bc 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
224bd 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64   ANALYZE command
224be 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64  ..**.** @(#) $Id
224bf 3a 20 61 6e 61 6c 79 7a 65 2e 63 2c 76 20 31 2e  : analyze.c,v 1.
224c0 35 32 20 32 30 30 39 2f 30 34 2f 31 36 20 31 37  52 2009/04/16 17
224c1 3a 34 35 3a 34 38 20 64 72 68 20 45 78 70 20 24  :45:48 drh Exp $
224c2 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
224c3 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 0a  TE_OMIT_ANALYZE.
224c4 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
224c5 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f  ine generates co
224c6 64 65 20 74 68 61 74 20 6f 70 65 6e 73 20 74 68  de that opens th
224c7 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74  e sqlite_stat1 t
224c8 61 62 6c 65 20 66 6f 72 0a 2a 2a 20 77 72 69 74  able for.** writ
224c9 69 6e 67 20 77 69 74 68 20 63 75 72 73 6f 72 20  ing with cursor 
224ca 69 53 74 61 74 43 75 72 2e 20 49 66 20 74 68 65  iStatCur. If the
224cb 20 6c 69 62 72 61 72 79 20 77 61 73 20 62 75 69   library was bui
224cc 6c 74 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 53  lt with the.** S
224cd 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41  QLITE_ENABLE_STA
224ce 54 32 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64  T2 macro defined
224cf 2c 20 74 68 65 6e 20 74 68 65 20 73 71 6c 69 74  , then the sqlit
224d0 65 5f 73 74 61 74 32 20 74 61 62 6c 65 20 69 73  e_stat2 table is
224d1 0a 2a 2a 20 6f 70 65 6e 65 64 20 66 6f 72 20 77  .** opened for w
224d2 72 69 74 69 6e 67 20 75 73 69 6e 67 20 63 75 72  riting using cur
224d3 73 6f 72 20 28 69 53 74 61 74 43 75 72 2b 31 29  sor (iStatCur+1)
224d4 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 71  .**.** If the sq
224d5 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65  lite_stat1 table
224d6 73 20 64 6f 65 73 20 6e 6f 74 20 70 72 65 76 69  s does not previ
224d7 6f 75 73 6c 79 20 65 78 69 73 74 2c 20 69 74 20  ously exist, it 
224d8 69 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 20 53  is created..** S
224d9 69 6d 69 6c 61 72 6c 79 2c 20 69 66 20 74 68 65  imilarly, if the
224da 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20 74 61   sqlite_stat2 ta
224db 62 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  ble does not exi
224dc 73 74 20 61 6e 64 20 74 68 65 20 6c 69 62 72 61  st and the libra
224dd 72 79 0a 2a 2a 20 69 73 20 63 6f 6d 70 69 6c 65  ry.** is compile
224de 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 45 4e  d with SQLITE_EN
224df 41 42 4c 45 5f 53 54 41 54 32 20 64 65 66 69 6e  ABLE_STAT2 defin
224e0 65 64 2c 20 69 74 20 69 73 20 63 72 65 61 74 65  ed, it is create
224e1 64 2e 20 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65  d. .**.** Argume
224e2 6e 74 20 7a 57 68 65 72 65 20 6d 61 79 20 62 65  nt zWhere may be
224e3 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
224e4 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e  buffer containin
224e5 67 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 2c 0a  g a table name,.
224e6 2a 2a 20 6f 72 20 69 74 20 6d 61 79 20 62 65 20  ** or it may be 
224e7 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20  a NULL pointer. 
224e8 49 66 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c  If it is not NUL
224e9 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 65 6e 74 72  L, then all entr
224ea 69 65 73 20 69 6e 0a 2a 2a 20 74 68 65 20 73 71  ies in.** the sq
224eb 6c 69 74 65 5f 73 74 61 74 31 20 61 6e 64 20 28  lite_stat1 and (
224ec 69 66 20 61 70 70 6c 69 63 61 62 6c 65 29 20 73  if applicable) s
224ed 71 6c 69 74 65 5f 73 74 61 74 32 20 74 61 62 6c  qlite_stat2 tabl
224ee 65 73 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a  es associated.**
224ef 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65 64 20   with the named 
224f0 74 61 62 6c 65 20 61 72 65 20 64 65 6c 65 74 65  table are delete
224f1 64 2e 20 49 66 20 7a 57 68 65 72 65 3d 3d 30 2c  d. If zWhere==0,
224f2 20 74 68 65 6e 20 63 6f 64 65 20 69 73 20 67 65   then code is ge
224f3 6e 65 72 61 74 65 64 0a 2a 2a 20 74 6f 20 64 65  nerated.** to de
224f4 6c 65 74 65 20 61 6c 6c 20 73 74 61 74 20 74 61  lete all stat ta
224f5 62 6c 65 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a  ble entries..*/.
224f6 73 74 61 74 69 63 20 76 6f 69 64 20 6f 70 65 6e  static void open
224f7 53 74 61 74 54 61 62 6c 65 28 0a 20 20 50 61 72  StatTable(.  Par
224f8 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
224f9 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
224fa 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74  context */.  int
224fb 20 69 44 62 2c 20 20 20 20 20 20 20 20 20 20 20   iDb,           
224fc 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
224fd 62 61 73 65 20 77 65 20 61 72 65 20 6c 6f 6f 6b  base we are look
224fe 69 6e 67 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20  ing in */.  int 
224ff 69 53 74 61 74 43 75 72 2c 20 20 20 20 20 20 20  iStatCur,       
22500 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20      /* Open the 
22501 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62  sqlite_stat1 tab
22502 6c 65 20 6f 6e 20 74 68 69 73 20 63 75 72 73 6f  le on this curso
22503 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  r */.  const cha
22504 72 20 2a 7a 57 68 65 72 65 20 20 20 20 20 20 2f  r *zWhere      /
22505 2a 20 44 65 6c 65 74 65 20 65 6e 74 72 69 65 73  * Delete entries
22506 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
22507 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 29   this table */.)
22508 7b 0a 20 20 73 74 61 74 69 63 20 73 74 72 75 63  {.  static struc
22509 74 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  t {.    const ch
2250a 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 20 20 63  ar *zName;.    c
2250b 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 73  onst char *zCols
2250c 3b 0a 20 20 7d 20 61 54 61 62 6c 65 5b 5d 20 3d  ;.  } aTable[] =
2250d 20 7b 0a 20 20 20 20 7b 20 22 73 71 6c 69 74 65   {.    { "sqlite
2250e 5f 73 74 61 74 31 22 2c 20 22 74 62 6c 2c 69 64  _stat1", "tbl,id
2250f 78 2c 73 74 61 74 22 20 7d 2c 0a 23 69 66 64 65  x,stat" },.#ifde
22510 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
22511 53 54 41 54 32 0a 20 20 20 20 7b 20 22 73 71 6c  STAT2.    { "sql
22512 69 74 65 5f 73 74 61 74 32 22 2c 20 22 74 62 6c  ite_stat2", "tbl
22513 2c 69 64 78 2c 73 61 6d 70 6c 65 6e 6f 2c 73 61  ,idx,sampleno,sa
22514 6d 70 6c 65 22 20 7d 2c 0a 23 65 6e 64 69 66 0a  mple" },.#endif.
22515 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 61 52 6f 6f    };..  int aRoo
22516 74 5b 5d 20 3d 20 7b 30 2c 20 30 7d 3b 0a 20 20  t[] = {0, 0};.  
22517 75 38 20 61 43 72 65 61 74 65 54 62 6c 5b 5d 20  u8 aCreateTbl[] 
22518 3d 20 7b 30 2c 20 30 7d 3b 0a 0a 20 20 69 6e 74  = {0, 0};..  int
22519 20 69 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   i;.  sqlite3 *d
2251a 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2251b 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 56 64 62    Db *pDb;.  Vdb
2251c 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65  e *v = sqlite3Ge
2251d 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
2251e 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75   if( v==0 ) retu
2251f 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  rn;.  assert( sq
22520 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41  lite3BtreeHoldsA
22521 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b  llMutexes(db) );
22522 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
22523 65 33 56 64 62 65 44 62 28 76 29 3d 3d 64 62 20  e3VdbeDb(v)==db 
22524 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e  );.  pDb = &db->
22525 61 44 62 5b 69 44 62 5d 3b 0a 0a 20 20 66 6f 72  aDb[iDb];..  for
22526 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a  (i=0; i<ArraySiz
22527 65 28 61 54 61 62 6c 65 29 3b 20 69 2b 2b 29 7b  e(aTable); i++){
22528 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
22529 2a 7a 54 61 62 20 3d 20 61 54 61 62 6c 65 5b 69  *zTab = aTable[i
2252a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 54 61 62  ].zName;.    Tab
2252b 6c 65 20 2a 70 53 74 61 74 3b 0a 20 20 20 20 69  le *pStat;.    i
2252c 66 28 20 28 70 53 74 61 74 20 3d 20 73 71 6c 69  f( (pStat = sqli
2252d 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c  te3FindTable(db,
2252e 20 7a 54 61 62 2c 20 70 44 62 2d 3e 7a 4e 61 6d   zTab, pDb->zNam
2252f 65 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  e))==0 ){.      
22530 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74  /* The sqlite_st
22531 61 74 5b 31 32 5d 20 74 61 62 6c 65 20 64 6f 65  at[12] table doe
22532 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 43 72 65  s not exist. Cre
22533 61 74 65 20 69 74 2e 20 4e 6f 74 65 20 74 68 61  ate it. Note tha
22534 74 20 61 20 0a 20 20 20 20 20 20 2a 2a 20 73 69  t a .      ** si
22535 64 65 2d 65 66 66 65 63 74 20 6f 66 20 74 68 65  de-effect of the
22536 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
22537 61 74 65 6d 65 6e 74 20 69 73 20 74 6f 20 6c 65  atement is to le
22538 61 76 65 20 74 68 65 20 72 6f 6f 74 70 61 67 65  ave the rootpage
22539 20 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68   .      ** of th
2253a 65 20 6e 65 77 20 74 61 62 6c 65 20 69 6e 20 72  e new table in r
2253b 65 67 69 73 74 65 72 20 70 50 61 72 73 65 2d 3e  egister pParse->
2253c 72 65 67 52 6f 6f 74 2e 20 54 68 69 73 20 69 73  regRoot. This is
2253d 20 69 6d 70 6f 72 74 61 6e 74 20 0a 20 20 20 20   important .    
2253e 20 20 2a 2a 20 62 65 63 61 75 73 65 20 74 68 65    ** because the
2253f 20 4f 70 65 6e 57 72 69 74 65 20 6f 70 63 6f 64   OpenWrite opcod
22540 65 20 62 65 6c 6f 77 20 77 69 6c 6c 20 62 65 20  e below will be 
22541 6e 65 65 64 69 6e 67 20 69 74 2e 20 2a 2f 0a 20  needing it. */. 
22542 20 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74       sqlite3Nest
22543 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a  edParse(pParse,.
22544 20 20 20 20 20 20 20 20 20 20 22 43 52 45 41 54            "CREAT
22545 45 20 54 41 42 4c 45 20 25 51 2e 25 73 28 25 73  E TABLE %Q.%s(%s
22546 29 22 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20  )", pDb->zName, 
22547 7a 54 61 62 2c 20 61 54 61 62 6c 65 5b 69 5d 2e  zTab, aTable[i].
22548 7a 43 6f 6c 73 0a 20 20 20 20 20 20 29 3b 0a 20  zCols.      );. 
22549 20 20 20 20 20 61 52 6f 6f 74 5b 69 5d 20 3d 20       aRoot[i] = 
2254a 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 3b  pParse->regRoot;
2254b 0a 20 20 20 20 20 20 61 43 72 65 61 74 65 54 62  .      aCreateTb
2254c 6c 5b 69 5d 20 3d 20 31 3b 0a 20 20 20 20 7d 65  l[i] = 1;.    }e
2254d 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  lse{.      /* Th
2254e 65 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20  e table already 
2254f 65 78 69 73 74 73 2e 20 49 66 20 7a 57 68 65 72  exists. If zWher
22550 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 64  e is not NULL, d
22551 65 6c 65 74 65 20 61 6c 6c 20 65 6e 74 72 69 65  elete all entrie
22552 73 20 0a 20 20 20 20 20 20 2a 2a 20 61 73 73 6f  s .      ** asso
22553 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
22554 74 61 62 6c 65 20 7a 57 68 65 72 65 2e 20 49 66  table zWhere. If
22555 20 7a 57 68 65 72 65 20 69 73 20 4e 55 4c 4c 2c   zWhere is NULL,
22556 20 64 65 6c 65 74 65 20 74 68 65 0a 20 20 20 20   delete the.    
22557 20 20 2a 2a 20 65 6e 74 69 72 65 20 63 6f 6e 74    ** entire cont
22558 65 6e 74 73 20 6f 66 20 74 68 65 20 74 61 62 6c  ents of the tabl
22559 65 2e 20 2a 2f 0a 20 20 20 20 20 20 61 52 6f 6f  e. */.      aRoo
2255a 74 5b 69 5d 20 3d 20 70 53 74 61 74 2d 3e 74 6e  t[i] = pStat->tn
2255b 75 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  um;.      sqlite
2255c 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73  3TableLock(pPars
2255d 65 2c 20 69 44 62 2c 20 61 52 6f 6f 74 5b 69 5d  e, iDb, aRoot[i]
2255e 2c 20 31 2c 20 7a 54 61 62 29 3b 0a 20 20 20 20  , 1, zTab);.    
2255f 20 20 69 66 28 20 7a 57 68 65 72 65 20 29 7b 0a    if( zWhere ){.
22560 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e          sqlite3N
22561 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73  estedParse(pPars
22562 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 22 44  e,.           "D
22563 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 25 73  ELETE FROM %Q.%s
22564 20 57 48 45 52 45 20 74 62 6c 3d 25 51 22 2c 20   WHERE tbl=%Q", 
22565 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 54 61 62  pDb->zName, zTab
22566 2c 20 7a 57 68 65 72 65 0a 20 20 20 20 20 20 20  , zWhere.       
22567 20 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   );.      }else{
22568 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
22569 73 71 6c 69 74 65 5f 73 74 61 74 5b 31 32 5d 20  sqlite_stat[12] 
2256a 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65 78  table already ex
2256b 69 73 74 73 2e 20 20 44 65 6c 65 74 65 20 61 6c  ists.  Delete al
2256c 6c 20 72 6f 77 73 2e 20 2a 2f 0a 20 20 20 20 20  l rows. */.     
2256d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2256e 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 65 61 72  dOp2(v, OP_Clear
2256f 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 69 44 62 29  , aRoot[i], iDb)
22570 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
22571 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74    }..  /* Open t
22572 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 5b 31  he sqlite_stat[1
22573 32 5d 20 74 61 62 6c 65 73 20 66 6f 72 20 77 72  2] tables for wr
22574 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 66 6f 72 28  iting. */.  for(
22575 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65  i=0; i<ArraySize
22576 28 61 54 61 62 6c 65 29 3b 20 69 2b 2b 29 7b 0a  (aTable); i++){.
22577 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22578 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp3(v, OP_Open
22579 57 72 69 74 65 2c 20 69 53 74 61 74 43 75 72 2b  Write, iStatCur+
2257a 69 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 69 44 62  i, aRoot[i], iDb
2257b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2257c 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31  beChangeP4(v, -1
2257d 2c 20 28 63 68 61 72 20 2a 29 33 2c 20 50 34 5f  , (char *)3, P4_
2257e 49 4e 54 33 32 29 3b 0a 20 20 20 20 73 71 6c 69  INT32);.    sqli
2257f 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
22580 76 2c 20 61 43 72 65 61 74 65 54 62 6c 5b 69 5d  v, aCreateTbl[i]
22581 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
22582 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
22583 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20   do an analysis 
22584 6f 66 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 61  of all indices a
22585 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a  ssociated with.*
22586 2a 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65  * a single table
22587 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
22588 20 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65   analyzeOneTable
22589 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2258a 65 2c 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63  e,   /* Parser c
2258b 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c  ontext */.  Tabl
2258c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a 20  e *pTab,     /* 
2258d 54 61 62 6c 65 20 77 68 6f 73 65 20 69 6e 64 69  Table whose indi
2258e 63 65 73 20 61 72 65 20 74 6f 20 62 65 20 61 6e  ces are to be an
2258f 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20  alyzed */.  int 
22590 69 53 74 61 74 43 75 72 2c 20 20 20 20 2f 2a 20  iStatCur,    /* 
22591 49 6e 64 65 78 20 6f 66 20 56 64 62 65 43 75 72  Index of VdbeCur
22592 73 6f 72 20 74 68 61 74 20 77 72 69 74 65 73 20  sor that writes 
22593 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31  the sqlite_stat1
22594 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
22595 69 4d 65 6d 20 20 20 20 20 20 20 20 20 2f 2a 20  iMem         /* 
22596 41 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79  Available memory
22597 20 6c 6f 63 61 74 69 6f 6e 73 20 62 65 67 69 6e   locations begin
22598 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 73 71   here */.){.  sq
22599 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2259a 73 65 2d 3e 64 62 3b 20 20 20 20 2f 2a 20 44 61  se->db;    /* Da
2259b 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f  tabase handle */
2259c 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20  .  Index *pIdx; 
2259d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2259e 2f 2a 20 41 6e 20 69 6e 64 65 78 20 74 6f 20 62  /* An index to b
2259f 65 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 2a 2f  eing analyzed */
225a0 0a 20 20 69 6e 74 20 69 49 64 78 43 75 72 3b 20  .  int iIdxCur; 
225a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
225a2 2f 2a 20 43 75 72 73 6f 72 20 6f 70 65 6e 20 6f  /* Cursor open o
225a3 6e 20 69 6e 64 65 78 20 62 65 69 6e 67 20 61 6e  n index being an
225a4 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 56 64 62 65  alyzed */.  Vdbe
225a5 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20   *v;            
225a6 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
225a7 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20  virtual machine 
225a8 62 65 69 6e 67 20 62 75 69 6c 74 20 75 70 20 2a  being built up *
225a9 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  /.  int i;      
225aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
225ab 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
225ac 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 4f 66 4c   */.  int topOfL
225ad 6f 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  oop;            
225ae 20 20 20 2f 2a 20 54 68 65 20 74 6f 70 20 6f 66     /* The top of
225af 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69   the loop */.  i
225b0 6e 74 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20 20 20  nt endOfLoop;   
225b1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
225b2 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 6f  he end of the lo
225b3 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  op */.  int addr
225b4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
225b5 20 20 20 20 20 2f 2a 20 54 68 65 20 61 64 64 72       /* The addr
225b6 65 73 73 20 6f 66 20 61 6e 20 69 6e 73 74 72 75  ess of an instru
225b7 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  ction */.  int i
225b8 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
225b9 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
225ba 20 6f 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e   of database con
225bb 74 61 69 6e 69 6e 67 20 70 54 61 62 20 2a 2f 0a  taining pTab */.
225bc 20 20 69 6e 74 20 72 65 67 54 61 62 6e 61 6d 65    int regTabname
225bd 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 2f   = iMem++;     /
225be 2a 20 52 65 67 69 73 74 65 72 20 63 6f 6e 74 61  * Register conta
225bf 69 6e 69 6e 67 20 74 61 62 6c 65 20 6e 61 6d 65  ining table name
225c0 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 49 64 78   */.  int regIdx
225c1 6e 61 6d 65 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20  name = iMem++;  
225c2 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 63     /* Register c
225c3 6f 6e 74 61 69 6e 69 6e 67 20 69 6e 64 65 78 20  ontaining index 
225c4 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65  name */.  int re
225c5 67 53 61 6d 70 6c 65 6e 6f 20 3d 20 69 4d 65 6d  gSampleno = iMem
225c6 2b 2b 3b 20 20 20 20 2f 2a 20 52 65 67 69 73 74  ++;    /* Regist
225c7 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6e 65  er containing ne
225c8 78 74 20 73 61 6d 70 6c 65 20 6e 75 6d 62 65 72  xt sample number
225c9 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 43 6f 6c   */.  int regCol
225ca 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20   = iMem++;      
225cb 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66     /* Content of
225cc 20 61 20 63 6f 6c 75 6d 6e 20 61 6e 61 6c 79 7a   a column analyz
225cd 65 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  ed table */.  in
225ce 74 20 72 65 67 52 65 63 20 3d 20 69 4d 65 6d 2b  t regRec = iMem+
225cf 2b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  +;         /* Re
225d0 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 63  gister holding c
225d1 6f 6d 70 6c 65 74 65 64 20 72 65 63 6f 72 64 20  ompleted record 
225d2 2a 2f 0a 20 20 69 6e 74 20 72 65 67 54 65 6d 70  */.  int regTemp
225d3 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20   = iMem++;      
225d4 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 75    /* Temporary u
225d5 73 65 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20  se register */. 
225d6 20 69 6e 74 20 72 65 67 52 6f 77 69 64 20 3d 20   int regRowid = 
225d7 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 2f 2a  iMem++;       /*
225d8 20 52 6f 77 69 64 20 66 6f 72 20 74 68 65 20 69   Rowid for the i
225d9 6e 73 65 72 74 65 64 20 72 65 63 6f 72 64 20 2a  nserted record *
225da 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
225db 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 20 20  _ENABLE_STAT2.  
225dc 69 6e 74 20 72 65 67 54 65 6d 70 32 20 3d 20 69  int regTemp2 = i
225dd 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 2f 2a 20  Mem++;       /* 
225de 54 65 6d 70 6f 72 61 72 79 20 75 73 65 20 72 65  Temporary use re
225df 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  gister */.  int 
225e0 72 65 67 53 61 6d 70 6c 65 72 65 63 6e 6f 20 3d  regSamplerecno =
225e1 20 69 4d 65 6d 2b 2b 3b 20 2f 2a 20 49 6e 64 65   iMem++; /* Inde
225e2 78 20 6f 66 20 6e 65 78 74 20 73 61 6d 70 6c 65  x of next sample
225e3 20 74 6f 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20   to record */.  
225e4 69 6e 74 20 72 65 67 52 65 63 6e 6f 20 3d 20 69  int regRecno = i
225e5 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 2f 2a 20  Mem++;       /* 
225e6 43 75 72 72 65 6e 74 20 73 61 6d 70 6c 65 20 69  Current sample i
225e7 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65  ndex */.  int re
225e8 67 4c 61 73 74 20 3d 20 69 4d 65 6d 2b 2b 3b 20  gLast = iMem++; 
225e9 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
225ea 6f 66 20 6c 61 73 74 20 73 61 6d 70 6c 65 20 74  of last sample t
225eb 6f 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e  o record */.  in
225ec 74 20 72 65 67 46 69 72 73 74 20 3d 20 69 4d 65  t regFirst = iMe
225ed 6d 2b 2b 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e  m++;       /* In
225ee 64 65 78 20 6f 66 20 66 69 72 73 74 20 73 61 6d  dex of first sam
225ef 70 6c 65 20 74 6f 20 72 65 63 6f 72 64 20 2a 2f  ple to record */
225f0 0a 23 65 6e 64 69 66 0a 0a 20 20 76 20 3d 20 73  .#endif..  v = s
225f1 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
225f2 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d  arse);.  if( v==
225f3 30 20 7c 7c 20 4e 45 56 45 52 28 70 54 61 62 3d  0 || NEVER(pTab=
225f4 3d 30 29 20 7c 7c 20 70 54 61 62 2d 3e 70 49 6e  =0) || pTab->pIn
225f5 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  dex==0 ){.    /*
225f6 20 44 6f 20 6e 6f 20 61 6e 61 6c 79 73 69 73 20   Do no analysis 
225f7 66 6f 72 20 74 61 62 6c 65 73 20 74 68 61 74 20  for tables that 
225f8 68 61 76 65 20 6e 6f 20 69 6e 64 69 63 65 73 20  have no indices 
225f9 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  */.    return;. 
225fa 20 7d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c   }.  assert( sql
225fb 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c  ite3BtreeHoldsAl
225fc 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b 0a  lMutexes(db) );.
225fd 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
225fe 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
225ff 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
22600 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d  .  assert( iDb>=
22601 30 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  0 );.#ifndef SQL
22602 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
22603 5a 41 54 49 4f 4e 0a 20 20 69 66 28 20 73 71 6c  ZATION.  if( sql
22604 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
22605 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 4e 41  arse, SQLITE_ANA
22606 4c 59 5a 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  LYZE, pTab->zNam
22607 65 2c 20 30 2c 0a 20 20 20 20 20 20 64 62 2d 3e  e, 0,.      db->
22608 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 20 29  aDb[iDb].zName )
22609 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
2260a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
2260b 20 45 73 74 61 62 6c 69 73 68 20 61 20 72 65 61   Establish a rea
2260c 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61  d-lock on the ta
2260d 62 6c 65 20 61 74 20 74 68 65 20 73 68 61 72 65  ble at the share
2260e 64 2d 63 61 63 68 65 20 6c 65 76 65 6c 2e 20 2a  d-cache level. *
2260f 2f 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65  /.  sqlite3Table
22610 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62  Lock(pParse, iDb
22611 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c  , pTab->tnum, 0,
22612 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a   pTab->zName);..
22613 20 20 69 49 64 78 43 75 72 20 3d 20 70 50 61 72    iIdxCur = pPar
22614 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 66 6f  se->nTab++;.  fo
22615 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
22616 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
22617 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
22618 20 20 69 6e 74 20 6e 43 6f 6c 20 3d 20 70 49 64    int nCol = pId
22619 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  x->nColumn;.    
2261a 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20  KeyInfo *pKey = 
2261b 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69  sqlite3IndexKeyi
2261c 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 64 78  nfo(pParse, pIdx
2261d 29 3b 0a 0a 20 20 20 20 69 66 28 20 69 4d 65 6d  );..    if( iMem
2261e 2b 31 2b 28 6e 43 6f 6c 2a 32 29 3e 70 50 61 72  +1+(nCol*2)>pPar
2261f 73 65 2d 3e 6e 4d 65 6d 20 29 7b 0a 20 20 20 20  se->nMem ){.    
22620 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d    pParse->nMem =
22621 20 69 4d 65 6d 2b 31 2b 28 6e 43 6f 6c 2a 32 29   iMem+1+(nCol*2)
22622 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
22623 4f 70 65 6e 20 61 20 63 75 72 73 6f 72 20 74 6f  Open a cursor to
22624 20 74 68 65 20 69 6e 64 65 78 20 74 6f 20 62 65   the index to be
22625 20 61 6e 61 6c 79 7a 65 64 2e 20 2a 2f 0a 20 20   analyzed. */.  
22626 20 20 61 73 73 65 72 74 28 20 69 44 62 3d 3d 73    assert( iDb==s
22627 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
22628 64 65 78 28 64 62 2c 20 70 49 64 78 2d 3e 70 53  dex(db, pIdx->pS
22629 63 68 65 6d 61 29 20 29 3b 0a 20 20 20 20 73 71  chema) );.    sq
2262a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2262b 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20  v, OP_OpenRead, 
2262c 69 49 64 78 43 75 72 2c 20 70 49 64 78 2d 3e 74  iIdxCur, pIdx->t
2262d 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 20  num, iDb,.      
2262e 20 20 28 63 68 61 72 20 2a 29 70 4b 65 79 2c 20    (char *)pKey, 
2262f 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
22630 46 46 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d  FF);.    VdbeCom
22631 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70  ment((v, "%s", p
22632 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 0a 20  Idx->zName));.. 
22633 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74     /* Populate t
22634 68 65 20 72 65 67 69 73 74 65 72 73 20 63 6f 6e  he registers con
22635 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c  taining the tabl
22636 65 20 61 6e 64 20 69 6e 64 65 78 20 6e 61 6d 65  e and index name
22637 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 54  s. */.    if( pT
22638 61 62 2d 3e 70 49 6e 64 65 78 3d 3d 70 49 64 78  ab->pIndex==pIdx
22639 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2263a 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
2263b 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65  P_String8, 0, re
2263c 67 54 61 62 6e 61 6d 65 2c 20 30 2c 20 70 54 61  gTabname, 0, pTa
2263d 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  b->zName, 0);.  
2263e 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
2263f 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
22640 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67 49  String8, 0, regI
22641 64 78 6e 61 6d 65 2c 20 30 2c 20 70 49 64 78 2d  dxname, 0, pIdx-
22642 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 0a 23 69 66  >zName, 0);..#if
22643 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
22644 45 5f 53 54 41 54 32 0a 0a 20 20 20 20 2f 2a 20  E_STAT2..    /* 
22645 49 66 20 74 68 69 73 20 69 74 65 72 61 74 69 6f  If this iteratio
22646 6e 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 69 73  n of the loop is
22647 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65   generating code
22648 20 74 6f 20 61 6e 61 6c 79 7a 65 20 74 68 65 0a   to analyze the.
22649 20 20 20 20 2a 2a 20 66 69 72 73 74 20 69 6e 64      ** first ind
2264a 65 78 20 69 6e 20 74 68 65 20 70 54 61 62 2d 3e  ex in the pTab->
2264b 70 49 6e 64 65 78 20 6c 69 73 74 2c 20 74 68 65  pIndex list, the
2264c 6e 20 72 65 67 69 73 74 65 72 20 72 65 67 4c 61  n register regLa
2264d 73 74 20 68 61 73 0a 20 20 20 20 2a 2a 20 6e 6f  st has.    ** no
2264e 74 20 62 65 65 6e 20 70 6f 70 75 6c 61 74 65 64  t been populated
2264f 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 70  . In this case p
22650 6f 70 75 6c 61 74 65 20 69 74 20 6e 6f 77 2e 20  opulate it now. 
22651 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 54 61 62   */.    if( pTab
22652 2d 3e 70 49 6e 64 65 78 3d 3d 70 49 64 78 20 29  ->pIndex==pIdx )
22653 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
22654 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
22655 49 6e 74 65 67 65 72 2c 20 53 51 4c 49 54 45 5f  Integer, SQLITE_
22656 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 2c 20 72  INDEX_SAMPLES, r
22657 65 67 53 61 6d 70 6c 65 72 65 63 6e 6f 29 3b 0a  egSamplerecno);.
22658 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
22659 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
2265a 74 65 67 65 72 2c 20 53 51 4c 49 54 45 5f 49 4e  teger, SQLITE_IN
2265b 44 45 58 5f 53 41 4d 50 4c 45 53 2a 32 2d 31 2c  DEX_SAMPLES*2-1,
2265c 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20 20   regTemp);.     
2265d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2265e 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
2265f 2c 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53  , SQLITE_INDEX_S
22660 41 4d 50 4c 45 53 2a 32 2c 20 72 65 67 54 65 6d  AMPLES*2, regTem
22661 70 32 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69  p2);..      sqli
22662 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
22663 20 4f 50 5f 43 6f 75 6e 74 2c 20 69 49 64 78 43   OP_Count, iIdxC
22664 75 72 2c 20 72 65 67 4c 61 73 74 29 3b 0a 20 20  ur, regLast);.  
22665 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22666 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
22667 2c 20 30 2c 20 72 65 67 46 69 72 73 74 29 3b 0a  , 0, regFirst);.
22668 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
22669 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2266a 2c 20 4f 50 5f 4c 74 2c 20 72 65 67 53 61 6d 70  , OP_Lt, regSamp
2266b 6c 65 72 65 63 6e 6f 2c 20 30 2c 20 72 65 67 4c  lerecno, 0, regL
2266c 61 73 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ast);.      sqli
2266d 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2266e 20 4f 50 5f 44 69 76 69 64 65 2c 20 72 65 67 54   OP_Divide, regT
2266f 65 6d 70 32 2c 20 72 65 67 4c 61 73 74 2c 20 72  emp2, regLast, r
22670 65 67 46 69 72 73 74 29 3b 0a 20 20 20 20 20 20  egFirst);.      
22671 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22672 33 28 76 2c 20 4f 50 5f 4d 75 6c 74 69 70 6c 79  3(v, OP_Multiply
22673 2c 20 72 65 67 4c 61 73 74 2c 20 72 65 67 54 65  , regLast, regTe
22674 6d 70 2c 20 72 65 67 4c 61 73 74 29 3b 0a 20 20  mp, regLast);.  
22675 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22676 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
22677 6d 6d 2c 20 72 65 67 4c 61 73 74 2c 20 53 51 4c  mm, regLast, SQL
22678 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45  ITE_INDEX_SAMPLE
22679 53 2a 32 2d 32 29 3b 0a 20 20 20 20 20 20 73 71  S*2-2);.      sq
2267a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2267b 76 2c 20 4f 50 5f 44 69 76 69 64 65 2c 20 20 72  v, OP_Divide,  r
2267c 65 67 54 65 6d 70 32 2c 20 72 65 67 4c 61 73 74  egTemp2, regLast
2267d 2c 20 72 65 67 4c 61 73 74 29 3b 0a 20 20 20 20  , regLast);.    
2267e 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
2267f 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a  pHere(v, addr);.
22680 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 5a 65      }..    /* Ze
22681 72 6f 20 74 68 65 20 72 65 67 53 61 6d 70 6c 65  ro the regSample
22682 6e 6f 20 61 6e 64 20 72 65 67 52 65 63 6e 6f 20  no and regRecno 
22683 72 65 67 69 73 74 65 72 73 2e 20 2a 2f 0a 20 20  registers. */.  
22684 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22685 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
22686 72 2c 20 30 2c 20 72 65 67 53 61 6d 70 6c 65 6e  r, 0, regSamplen
22687 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  o);.    sqlite3V
22688 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
22689 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 52  Integer, 0, regR
2268a 65 63 6e 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74  ecno);.    sqlit
2268b 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2268c 4f 50 5f 43 6f 70 79 2c 20 72 65 67 46 69 72 73  OP_Copy, regFirs
2268d 74 2c 20 72 65 67 53 61 6d 70 6c 65 72 65 63 6e  t, regSamplerecn
2268e 6f 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20  o);.#endif..    
2268f 2f 2a 20 54 68 65 20 62 6c 6f 63 6b 20 6f 66 20  /* The block of 
22690 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 69 6e 69  memory cells ini
22691 74 69 61 6c 69 7a 65 64 20 68 65 72 65 20 69 73  tialized here is
22692 20 75 73 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73   used as follows
22693 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
22694 20 20 20 69 4d 65 6d 3a 20 20 20 20 20 20 20 20     iMem:        
22695 20 20 20 20 20 20 20 20 0a 20 20 20 20 2a 2a 20          .    ** 
22696 20 20 20 20 20 20 20 54 68 65 20 74 6f 74 61 6c         The total
22697 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   number of rows 
22698 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20  in the table..  
22699 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 69    **.    **    i
2269a 4d 65 6d 2b 31 20 2e 2e 20 69 4d 65 6d 2b 6e 43  Mem+1 .. iMem+nC
2269b 6f 6c 3a 20 0a 20 20 20 20 2a 2a 20 20 20 20 20  ol: .    **     
2269c 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 64 69 73     Number of dis
2269d 74 69 6e 63 74 20 65 6e 74 72 69 65 73 20 69 6e  tinct entries in
2269e 20 69 6e 64 65 78 20 63 6f 6e 73 69 64 65 72 69   index consideri
2269f 6e 67 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 20  ng the .    **  
226a0 20 20 20 20 20 20 6c 65 66 74 2d 6d 6f 73 74 20        left-most 
226a1 4e 20 63 6f 6c 75 6d 6e 73 20 6f 6e 6c 79 2c 20  N columns only, 
226a2 77 68 65 72 65 20 4e 20 69 73 20 62 65 74 77 65  where N is betwe
226a3 65 6e 20 31 20 61 6e 64 20 6e 43 6f 6c 2c 20 0a  en 1 and nCol, .
226a4 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 69 6e      **        in
226a5 63 6c 75 73 69 76 65 2e 0a 20 20 20 20 2a 2a 0a  clusive..    **.
226a6 20 20 20 20 2a 2a 20 20 20 20 69 4d 65 6d 2b 6e      **    iMem+n
226a7 43 6f 6c 2b 31 20 2e 2e 20 4d 65 6d 2b 32 2a 6e  Col+1 .. Mem+2*n
226a8 43 6f 6c 3a 20 20 0a 20 20 20 20 2a 2a 20 20 20  Col:  .    **   
226a9 20 20 20 20 20 50 72 65 76 69 6f 75 73 20 76 61       Previous va
226aa 6c 75 65 20 6f 66 20 69 6e 64 65 78 65 64 20 63  lue of indexed c
226ab 6f 6c 75 6d 6e 73 2c 20 66 72 6f 6d 20 6c 65 66  olumns, from lef
226ac 74 20 74 6f 20 72 69 67 68 74 2e 0a 20 20 20 20  t to right..    
226ad 2a 2a 0a 20 20 20 20 2a 2a 20 43 65 6c 6c 73 20  **.    ** Cells 
226ae 69 4d 65 6d 20 74 68 72 6f 75 67 68 20 69 4d 65  iMem through iMe
226af 6d 2b 6e 43 6f 6c 20 61 72 65 20 69 6e 69 74 69  m+nCol are initi
226b0 61 6c 69 7a 65 64 20 74 6f 20 30 2e 20 54 68 65  alized to 0. The
226b1 20 6f 74 68 65 72 73 20 61 72 65 20 0a 20 20 20   others are .   
226b2 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   ** initialized 
226b3 74 6f 20 63 6f 6e 74 61 69 6e 20 61 6e 20 53 51  to contain an SQ
226b4 4c 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20  L NULL..    */. 
226b5 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 6e     for(i=0; i<=n
226b6 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
226b7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
226b8 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
226b9 2c 20 30 2c 20 69 4d 65 6d 2b 69 29 3b 0a 20 20  , 0, iMem+i);.  
226ba 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b    }.    for(i=0;
226bb 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20   i<nCol; i++){. 
226bc 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
226bd 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
226be 6c 2c 20 30 2c 20 69 4d 65 6d 2b 6e 43 6f 6c 2b  l, 0, iMem+nCol+
226bf 69 2b 31 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  i+1);.    }..   
226c0 20 2f 2a 20 53 74 61 72 74 20 74 68 65 20 61 6e   /* Start the an
226c1 61 6c 79 73 69 73 20 6c 6f 6f 70 2e 20 54 68 69  alysis loop. Thi
226c2 73 20 6c 6f 6f 70 20 72 75 6e 73 20 74 68 72 6f  s loop runs thro
226c3 75 67 68 20 61 6c 6c 20 74 68 65 20 65 6e 74 72  ugh all the entr
226c4 69 65 73 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68  ies in.    ** th
226c5 65 20 69 6e 64 65 78 20 62 2d 74 72 65 65 2e 20  e index b-tree. 
226c6 20 2a 2f 0a 20 20 20 20 65 6e 64 4f 66 4c 6f 6f   */.    endOfLoo
226c7 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  p = sqlite3VdbeM
226c8 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
226c9 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
226ca 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c  p2(v, OP_Rewind,
226cb 20 69 49 64 78 43 75 72 2c 20 65 6e 64 4f 66 4c   iIdxCur, endOfL
226cc 6f 6f 70 29 3b 0a 20 20 20 20 74 6f 70 4f 66 4c  oop);.    topOfL
226cd 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  oop = sqlite3Vdb
226ce 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b  eCurrentAddr(v);
226cf 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
226d0 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64  AddOp2(v, OP_Add
226d1 49 6d 6d 2c 20 69 4d 65 6d 2c 20 31 29 3b 0a 0a  Imm, iMem, 1);..
226d2 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
226d3 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
226d4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
226d5 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
226d6 20 69 49 64 78 43 75 72 2c 20 69 2c 20 72 65 67   iIdxCur, i, reg
226d7 43 6f 6c 29 3b 0a 23 69 66 64 65 66 20 53 51 4c  Col);.#ifdef SQL
226d8 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32  ITE_ENABLE_STAT2
226d9 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d 30 20  .      if( i==0 
226da 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 68  ){.        /* Ch
226db 65 63 6b 20 69 66 20 74 68 65 20 72 65 63 6f 72  eck if the recor
226dc 64 20 74 68 61 74 20 63 75 72 73 6f 72 20 69 49  d that cursor iI
226dd 64 78 43 75 72 20 70 6f 69 6e 74 73 20 74 6f 20  dxCur points to 
226de 63 6f 6e 74 61 69 6e 73 20 61 0a 20 20 20 20 20  contains a.     
226df 20 20 20 2a 2a 20 76 61 6c 75 65 20 74 68 61 74     ** value that
226e0 20 73 68 6f 75 6c 64 20 62 65 20 73 74 6f 72 65   should be store
226e1 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f  d in the sqlite_
226e2 73 74 61 74 32 20 74 61 62 6c 65 2e 20 49 66 20  stat2 table. If 
226e3 73 6f 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 73  so,.        ** s
226e4 74 6f 72 65 20 69 74 2e 20 20 2a 2f 0a 20 20 20  tore it.  */.   
226e5 20 20 20 20 20 69 6e 74 20 6e 65 20 3d 20 73 71       int ne = sq
226e6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
226e7 76 2c 20 4f 50 5f 4e 65 2c 20 72 65 67 52 65 63  v, OP_Ne, regRec
226e8 6e 6f 2c 20 30 2c 20 72 65 67 53 61 6d 70 6c 65  no, 0, regSample
226e9 72 65 63 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20  recno);.        
226ea 61 73 73 65 72 74 28 20 72 65 67 54 61 62 6e 61  assert( regTabna
226eb 6d 65 2b 31 3d 3d 72 65 67 49 64 78 6e 61 6d 65  me+1==regIdxname
226ec 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 26   .             &
226ed 26 20 72 65 67 54 61 62 6e 61 6d 65 2b 32 3d 3d  & regTabname+2==
226ee 72 65 67 53 61 6d 70 6c 65 6e 6f 0a 20 20 20 20  regSampleno.    
226ef 20 20 20 20 20 20 20 20 20 26 26 20 72 65 67 54           && regT
226f0 61 62 6e 61 6d 65 2b 33 3d 3d 72 65 67 43 6f 6c  abname+3==regCol
226f1 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20  .        );.    
226f2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
226f3 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54  hangeP5(v, SQLIT
226f4 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20  E_JUMPIFNULL);. 
226f5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
226f6 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d  beAddOp4(v, OP_M
226f7 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 54 61  akeRecord, regTa
226f8 62 6e 61 6d 65 2c 20 34 2c 20 72 65 67 52 65 63  bname, 4, regRec
226f9 2c 20 22 61 61 61 62 22 2c 20 30 29 3b 0a 20 20  , "aaab", 0);.  
226fa 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
226fb 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
226fc 77 52 6f 77 69 64 2c 20 69 53 74 61 74 43 75 72  wRowid, iStatCur
226fd 2b 31 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  +1, regRowid);. 
226fe 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
226ff 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49  beAddOp3(v, OP_I
22700 6e 73 65 72 74 2c 20 69 53 74 61 74 43 75 72 2b  nsert, iStatCur+
22701 31 2c 20 72 65 67 52 65 63 2c 20 72 65 67 52 6f  1, regRec, regRo
22702 77 69 64 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f  wid);..        /
22703 2a 20 43 61 6c 63 75 6c 61 74 65 20 6e 65 77 20  * Calculate new 
22704 76 61 6c 75 65 73 20 66 6f 72 20 72 65 67 53 61  values for regSa
22705 6d 70 6c 65 72 65 63 6e 6f 20 61 6e 64 20 72 65  mplerecno and re
22706 67 53 61 6d 70 6c 65 6e 6f 2e 0a 20 20 20 20 20  gSampleno..     
22707 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
22708 20 20 20 73 61 6d 70 6c 65 6e 6f 20 3d 20 73 61     sampleno = sa
22709 6d 70 6c 65 6e 6f 20 2b 20 31 0a 20 20 20 20 20  mpleno + 1.     
2270a 20 20 20 2a 2a 20 20 20 73 61 6d 70 6c 65 72 65     **   samplere
2270b 63 6e 6f 20 3d 20 73 61 6d 70 6c 65 72 65 63 6e  cno = samplerecn
2270c 6f 2b 28 72 65 6d 61 69 6e 69 6e 67 20 72 65 63  o+(remaining rec
2270d 6f 72 64 73 29 2f 28 72 65 6d 61 69 6e 69 6e 67  ords)/(remaining
2270e 20 73 61 6d 70 6c 65 73 29 0a 20 20 20 20 20 20   samples).      
2270f 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c    */.        sql
22710 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
22711 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67  , OP_AddImm, reg
22712 53 61 6d 70 6c 65 6e 6f 2c 20 31 29 3b 0a 20 20  Sampleno, 1);.  
22713 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
22714 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 75  eAddOp3(v, OP_Su
22715 62 74 72 61 63 74 2c 20 72 65 67 52 65 63 6e 6f  btract, regRecno
22716 2c 20 72 65 67 4c 61 73 74 2c 20 72 65 67 54 65  , regLast, regTe
22717 6d 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  mp);.        sql
22718 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
22719 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67  , OP_AddImm, reg
2271a 54 65 6d 70 2c 20 2d 31 29 3b 0a 20 20 20 20 20  Temp, -1);.     
2271b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2271c 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
2271d 65 72 2c 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  er, SQLITE_INDEX
2271e 5f 53 41 4d 50 4c 45 53 2c 20 72 65 67 54 65 6d  _SAMPLES, regTem
2271f 70 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  p2);.        sql
22720 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
22721 2c 20 4f 50 5f 53 75 62 74 72 61 63 74 2c 20 72  , OP_Subtract, r
22722 65 67 53 61 6d 70 6c 65 6e 6f 2c 20 72 65 67 54  egSampleno, regT
22723 65 6d 70 32 2c 20 72 65 67 54 65 6d 70 32 29 3b  emp2, regTemp2);
22724 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
22725 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
22726 5f 44 69 76 69 64 65 2c 20 72 65 67 54 65 6d 70  _Divide, regTemp
22727 32 2c 20 72 65 67 54 65 6d 70 2c 20 72 65 67 54  2, regTemp, regT
22728 65 6d 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71  emp);.        sq
22729 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2272a 76 2c 20 4f 50 5f 41 64 64 2c 20 72 65 67 53 61  v, OP_Add, regSa
2272b 6d 70 6c 65 72 65 63 6e 6f 2c 20 72 65 67 54 65  mplerecno, regTe
2272c 6d 70 2c 20 72 65 67 53 61 6d 70 6c 65 72 65 63  mp, regSamplerec
2272d 6e 6f 29 3b 0a 0a 20 20 20 20 20 20 20 20 73 71  no);..        sq
2272e 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
2272f 65 28 76 2c 20 6e 65 29 3b 0a 20 20 20 20 20 20  e(v, ne);.      
22730 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22731 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d  Op2(v, OP_AddImm
22732 2c 20 72 65 67 52 65 63 6e 6f 2c 20 31 29 3b 0a  , regRecno, 1);.
22733 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a        }.#endif..
22734 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
22735 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65  eAddOp3(v, OP_Ne
22736 2c 20 72 65 67 43 6f 6c 2c 20 30 2c 20 69 4d 65  , regCol, 0, iMe
22737 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20  m+nCol+i+1);.   
22738 20 20 20 2f 2a 2a 2a 2a 20 54 4f 44 4f 3a 20 20     /**** TODO:  
22739 61 64 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  add collating se
2273a 71 75 65 6e 63 65 20 2a 2a 2a 2a 2a 2f 0a 20 20  quence *****/.  
2273b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2273c 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54  hangeP5(v, SQLIT
2273d 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20  E_JUMPIFNULL);. 
2273e 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64 62 2d     }.    if( db-
2273f 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
22740 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 61 20 6d  .      /* If a m
22741 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 68 61  alloc failure ha
22742 73 20 6f 63 63 75 72 72 65 64 2c 20 74 68 65 6e  s occurred, then
22743 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74   the result of t
22744 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 0a 20  he expression . 
22745 20 20 20 20 20 2a 2a 20 70 61 73 73 65 64 20 61       ** passed a
22746 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  s the second arg
22747 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 63 61 6c  ument to the cal
22748 6c 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 65  l to sqlite3Vdbe
22749 4a 75 6d 70 48 65 72 65 28 29 20 0a 20 20 20 20  JumpHere() .    
2274a 20 20 2a 2a 20 62 65 6c 6f 77 20 6d 61 79 20 62    ** below may b
2274b 65 20 6e 65 67 61 74 69 76 65 2e 20 57 68 69 63  e negative. Whic
2274c 68 20 63 61 75 73 65 73 20 61 6e 20 61 73 73 65  h causes an asse
2274d 72 74 28 29 20 74 6f 20 66 61 69 6c 20 28 6f 72  rt() to fail (or
2274e 20 61 6e 0a 20 20 20 20 20 20 2a 2a 20 6f 75 74   an.      ** out
2274f 2d 6f 66 2d 62 6f 75 6e 64 73 20 77 72 69 74 65  -of-bounds write
22750 20 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47   if SQLITE_DEBUG
22751 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29   is not defined)
22752 2e 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75  .  */.      retu
22753 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  rn;.    }.    sq
22754 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
22755 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 65  v, OP_Goto, 0, e
22756 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 66  ndOfLoop);.    f
22757 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20  or(i=0; i<nCol; 
22758 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i++){.      sqli
22759 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
2275a 76 2c 20 73 71 6c 69 74 65 33 56 64 62 65 43 75  v, sqlite3VdbeCu
2275b 72 72 65 6e 74 41 64 64 72 28 76 29 2d 28 6e 43  rrentAddr(v)-(nC
2275c 6f 6c 2a 32 29 29 3b 0a 20 20 20 20 20 20 73 71  ol*2));.      sq
2275d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2275e 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 69 4d  v, OP_AddImm, iM
2275f 65 6d 2b 69 2b 31 2c 20 31 29 3b 0a 20 20 20 20  em+i+1, 1);.    
22760 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22761 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
22762 2c 20 69 49 64 78 43 75 72 2c 20 69 2c 20 69 4d  , iIdxCur, i, iM
22763 65 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20  em+nCol+i+1);.  
22764 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 45 6e 64 20    }..    /* End 
22765 6f 66 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20  of the analysis 
22766 6c 6f 6f 70 2e 20 2a 2f 0a 20 20 20 20 73 71 6c  loop. */.    sql
22767 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
22768 61 62 65 6c 28 76 2c 20 65 6e 64 4f 66 4c 6f 6f  abel(v, endOfLoo
22769 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  p);.    sqlite3V
2276a 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2276b 4e 65 78 74 2c 20 69 49 64 78 43 75 72 2c 20 74  Next, iIdxCur, t
2276c 6f 70 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 73  opOfLoop);.    s
2276d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2276e 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 49  (v, OP_Close, iI
2276f 64 78 43 75 72 29 3b 0a 0a 20 20 20 20 2f 2a 20  dxCur);..    /* 
22770 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  Store the result
22771 73 20 69 6e 20 73 71 6c 69 74 65 5f 73 74 61 74  s in sqlite_stat
22772 31 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  1..    **.    **
22773 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 61   The result is a
22774 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74   single row of t
22775 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20  he sqlite_stat1 
22776 74 61 62 6c 65 2e 20 20 54 68 65 20 66 69 72 73  table.  The firs
22777 74 0a 20 20 20 20 2a 2a 20 74 77 6f 20 63 6f 6c  t.    ** two col
22778 75 6d 6e 73 20 61 72 65 20 74 68 65 20 6e 61 6d  umns are the nam
22779 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  es of the table 
2277a 61 6e 64 20 69 6e 64 65 78 2e 20 20 54 68 65 20  and index.  The 
2277b 74 68 69 72 64 20 63 6f 6c 75 6d 6e 0a 20 20 20  third column.   
2277c 20 2a 2a 20 69 73 20 61 20 73 74 72 69 6e 67 20   ** is a string 
2277d 63 6f 6d 70 6f 73 65 64 20 6f 66 20 61 20 6c 69  composed of a li
2277e 73 74 20 6f 66 20 69 6e 74 65 67 65 72 20 73 74  st of integer st
2277f 61 74 69 73 74 69 63 73 20 61 62 6f 75 74 20 74  atistics about t
22780 68 65 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78 2e  he.    ** index.
22781 20 20 54 68 65 20 66 69 72 73 74 20 69 6e 74 65    The first inte
22782 67 65 72 20 69 6e 20 74 68 65 20 6c 69 73 74 20  ger in the list 
22783 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d  is the total num
22784 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 0a 20  ber of entries. 
22785 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 69 6e 64     ** in the ind
22786 65 78 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e  ex.  There is on
22787 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 74  e additional int
22788 65 67 65 72 20 69 6e 20 74 68 65 20 6c 69 73 74  eger in the list
22789 20 66 6f 72 20 65 61 63 68 0a 20 20 20 20 2a 2a   for each.    **
2278a 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74   column of the t
2278b 61 62 6c 65 2e 20 20 54 68 69 73 20 61 64 64 69  able.  This addi
2278c 74 69 6f 6e 61 6c 20 69 6e 74 65 67 65 72 20 69  tional integer i
2278d 73 20 61 20 67 75 65 73 73 20 6f 66 20 68 6f 77  s a guess of how
2278e 20 6d 61 6e 79 0a 20 20 20 20 2a 2a 20 72 6f 77   many.    ** row
2278f 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  s of the table t
22790 68 65 20 69 6e 64 65 78 20 77 69 6c 6c 20 73 65  he index will se
22791 6c 65 63 74 2e 20 20 49 66 20 44 20 69 73 20 74  lect.  If D is t
22792 68 65 20 63 6f 75 6e 74 20 6f 66 20 64 69 73 74  he count of dist
22793 69 6e 63 74 0a 20 20 20 20 2a 2a 20 76 61 6c 75  inct.    ** valu
22794 65 73 20 61 6e 64 20 4b 20 69 73 20 74 68 65 20  es and K is the 
22795 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
22796 72 6f 77 73 2c 20 74 68 65 6e 20 74 68 65 20 69  rows, then the i
22797 6e 74 65 67 65 72 20 69 73 20 63 6f 6d 70 75 74  nteger is comput
22798 65 64 0a 20 20 20 20 2a 2a 20 61 73 3a 0a 20 20  ed.    ** as:.  
22799 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20    **.    **     
2279a 20 20 20 49 20 3d 20 28 4b 2b 44 2d 31 29 2f 44     I = (K+D-1)/D
2279b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49  .    **.    ** I
2279c 66 20 4b 3d 3d 30 20 74 68 65 6e 20 6e 6f 20 65  f K==0 then no e
2279d 6e 74 72 79 20 69 73 20 6d 61 64 65 20 69 6e 74  ntry is made int
2279e 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  o the sqlite_sta
2279f 74 31 20 74 61 62 6c 65 2e 20 20 0a 20 20 20 20  t1 table.  .    
227a0 2a 2a 20 49 66 20 4b 3e 30 20 74 68 65 6e 20 69  ** If K>0 then i
227a1 74 20 69 73 20 61 6c 77 61 79 73 20 74 68 65 20  t is always the 
227a2 63 61 73 65 20 74 68 65 20 44 3e 30 20 73 6f 20  case the D>0 so 
227a3 64 69 76 69 73 69 6f 6e 20 62 79 20 7a 65 72 6f  division by zero
227a4 0a 20 20 20 20 2a 2a 20 69 73 20 6e 65 76 65 72  .    ** is never
227a5 20 70 6f 73 73 69 62 6c 65 2e 0a 20 20 20 20 2a   possible..    *
227a6 2f 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c  /.    addr = sql
227a7 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
227a8 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 69 4d 65 6d  , OP_IfNot, iMem
227a9 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
227aa 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
227ab 43 6f 70 79 2c 20 69 4d 65 6d 2c 20 72 65 67 53  Copy, iMem, regS
227ac 61 6d 70 6c 65 6e 6f 29 3b 0a 20 20 20 20 66 6f  ampleno);.    fo
227ad 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69  r(i=0; i<nCol; i
227ae 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
227af 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
227b0 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72  OP_String8, 0, r
227b1 65 67 54 65 6d 70 2c 20 30 2c 20 22 20 22 2c 20  egTemp, 0, " ", 
227b2 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
227b3 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
227b4 50 5f 43 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d  P_Concat, regTem
227b5 70 2c 20 72 65 67 53 61 6d 70 6c 65 6e 6f 2c 20  p, regSampleno, 
227b6 72 65 67 53 61 6d 70 6c 65 6e 6f 29 3b 0a 20 20  regSampleno);.  
227b7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
227b8 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 64 64 2c  ddOp3(v, OP_Add,
227b9 20 69 4d 65 6d 2c 20 69 4d 65 6d 2b 69 2b 31 2c   iMem, iMem+i+1,
227ba 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20 20   regTemp);.     
227bb 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
227bc 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c  p2(v, OP_AddImm,
227bd 20 72 65 67 54 65 6d 70 2c 20 2d 31 29 3b 0a 20   regTemp, -1);. 
227be 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
227bf 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 44 69 76  AddOp3(v, OP_Div
227c0 69 64 65 2c 20 69 4d 65 6d 2b 69 2b 31 2c 20 72  ide, iMem+i+1, r
227c1 65 67 54 65 6d 70 2c 20 72 65 67 54 65 6d 70 29  egTemp, regTemp)
227c2 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
227c3 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
227c4 54 6f 49 6e 74 2c 20 72 65 67 54 65 6d 70 29 3b  ToInt, regTemp);
227c5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
227c6 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
227c7 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d 70 2c 20  oncat, regTemp, 
227c8 72 65 67 53 61 6d 70 6c 65 6e 6f 2c 20 72 65 67  regSampleno, reg
227c9 53 61 6d 70 6c 65 6e 6f 29 3b 0a 20 20 20 20 7d  Sampleno);.    }
227ca 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
227cb 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp4(v, OP_Mak
227cc 65 52 65 63 6f 72 64 2c 20 72 65 67 54 61 62 6e  eRecord, regTabn
227cd 61 6d 65 2c 20 33 2c 20 72 65 67 52 65 63 2c 20  ame, 3, regRec, 
227ce 22 61 61 61 22 2c 20 30 29 3b 0a 20 20 20 20 73  "aaa", 0);.    s
227cf 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
227d0 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c  (v, OP_NewRowid,
227d1 20 69 53 74 61 74 43 75 72 2c 20 72 65 67 52 6f   iStatCur, regRo
227d2 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  wid);.    sqlite
227d3 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
227d4 50 5f 49 6e 73 65 72 74 2c 20 69 53 74 61 74 43  P_Insert, iStatC
227d5 75 72 2c 20 72 65 67 52 65 63 2c 20 72 65 67 52  ur, regRec, regR
227d6 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  owid);.    sqlit
227d7 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
227d8 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29  , OPFLAG_APPEND)
227d9 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
227da 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
227db 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  r);.  }.}../*.**
227dc 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
227dd 68 61 74 20 77 69 6c 6c 20 63 61 75 73 65 20 74  hat will cause t
227de 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 69  he most recent i
227df 6e 64 65 78 20 61 6e 61 6c 79 73 69 73 20 74 6f  ndex analysis to
227e0 0a 2a 2a 20 62 65 20 6c 61 6f 64 65 64 20 69 6e  .** be laoded in
227e1 74 6f 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68  to internal hash
227e2 20 74 61 62 6c 65 73 20 77 68 65 72 65 20 69 73   tables where is
227e3 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a 2a 2f   can be used..*/
227e4 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 61  .static void loa
227e5 64 41 6e 61 6c 79 73 69 73 28 50 61 72 73 65 20  dAnalysis(Parse 
227e6 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44 62  *pParse, int iDb
227e7 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73  ){.  Vdbe *v = s
227e8 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
227e9 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29  arse);.  if( v )
227ea 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
227eb 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4c 6f  eAddOp1(v, OP_Lo
227ec 61 64 41 6e 61 6c 79 73 69 73 2c 20 69 44 62 29  adAnalysis, iDb)
227ed 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  ;.  }.}../*.** G
227ee 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
227ef 74 20 77 69 6c 6c 20 64 6f 20 61 6e 20 61 6e 61  t will do an ana
227f0 6c 79 73 69 73 20 6f 66 20 61 6e 20 65 6e 74 69  lysis of an enti
227f1 72 65 20 64 61 74 61 62 61 73 65 0a 2a 2f 0a 73  re database.*/.s
227f2 74 61 74 69 63 20 76 6f 69 64 20 61 6e 61 6c 79  tatic void analy
227f3 7a 65 44 61 74 61 62 61 73 65 28 50 61 72 73 65  zeDatabase(Parse
227f4 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44   *pParse, int iD
227f5 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  b){.  sqlite3 *d
227f6 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
227f7 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
227f8 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  a = db->aDb[iDb]
227f9 2e 70 53 63 68 65 6d 61 3b 20 20 20 20 2f 2a 20  .pSchema;    /* 
227fa 53 63 68 65 6d 61 20 6f 66 20 64 61 74 61 62 61  Schema of databa
227fb 73 65 20 69 44 62 20 2a 2f 0a 20 20 48 61 73 68  se iDb */.  Hash
227fc 45 6c 65 6d 20 2a 6b 3b 0a 20 20 69 6e 74 20 69  Elem *k;.  int i
227fd 53 74 61 74 43 75 72 3b 0a 20 20 69 6e 74 20 69  StatCur;.  int i
227fe 4d 65 6d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42  Mem;..  sqlite3B
227ff 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
22800 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44  on(pParse, 0, iD
22801 62 29 3b 0a 20 20 69 53 74 61 74 43 75 72 20 3d  b);.  iStatCur =
22802 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a 20   pParse->nTab;. 
22803 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 2b 3d   pParse->nTab +=
22804 20 32 3b 0a 20 20 6f 70 65 6e 53 74 61 74 54 61   2;.  openStatTa
22805 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44 62 2c  ble(pParse, iDb,
22806 20 69 53 74 61 74 43 75 72 2c 20 30 29 3b 0a 20   iStatCur, 0);. 
22807 20 69 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e   iMem = pParse->
22808 6e 4d 65 6d 2b 31 3b 0a 20 20 66 6f 72 28 6b 3d  nMem+1;.  for(k=
22809 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28  sqliteHashFirst(
2280a 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73  &pSchema->tblHas
2280b 68 29 3b 20 6b 3b 20 6b 3d 73 71 6c 69 74 65 48  h); k; k=sqliteH
2280c 61 73 68 4e 65 78 74 28 6b 29 29 7b 0a 20 20 20  ashNext(k)){.   
2280d 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 28   Table *pTab = (
2280e 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 48 61 73  Table*)sqliteHas
2280f 68 44 61 74 61 28 6b 29 3b 0a 20 20 20 20 61 6e  hData(k);.    an
22810 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 70 50  alyzeOneTable(pP
22811 61 72 73 65 2c 20 70 54 61 62 2c 20 69 53 74 61  arse, pTab, iSta
22812 74 43 75 72 2c 20 69 4d 65 6d 29 3b 0a 20 20 7d  tCur, iMem);.  }
22813 0a 20 20 6c 6f 61 64 41 6e 61 6c 79 73 69 73 28  .  loadAnalysis(
22814 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 7d 0a  pParse, iDb);.}.
22815 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
22816 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 64  code that will d
22817 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66  o an analysis of
22818 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20   a single table 
22819 69 6e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65  in.** a database
2281a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2281b 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28 50 61   analyzeTable(Pa
2281c 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62  rse *pParse, Tab
2281d 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 69 6e 74  le *pTab){.  int
2281e 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 53 74 61   iDb;.  int iSta
2281f 74 43 75 72 3b 0a 0a 20 20 61 73 73 65 72 74 28  tCur;..  assert(
22820 20 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 61 73   pTab!=0 );.  as
22821 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
22822 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65  eeHoldsAllMutexe
22823 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b  s(pParse->db) );
22824 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  .  iDb = sqlite3
22825 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50  SchemaToIndex(pP
22826 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e  arse->db, pTab->
22827 70 53 63 68 65 6d 61 29 3b 0a 20 20 73 71 6c 69  pSchema);.  sqli
22828 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
22829 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30  ration(pParse, 0
2282a 2c 20 69 44 62 29 3b 0a 20 20 69 53 74 61 74 43  , iDb);.  iStatC
2282b 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  ur = pParse->nTa
2282c 62 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 54 61  b;.  pParse->nTa
2282d 62 20 2b 3d 20 32 3b 0a 20 20 6f 70 65 6e 53 74  b += 2;.  openSt
2282e 61 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  atTable(pParse, 
2282f 69 44 62 2c 20 69 53 74 61 74 43 75 72 2c 20 70  iDb, iStatCur, p
22830 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 61  Tab->zName);.  a
22831 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 70  nalyzeOneTable(p
22832 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 53 74  Parse, pTab, iSt
22833 61 74 43 75 72 2c 20 70 50 61 72 73 65 2d 3e 6e  atCur, pParse->n
22834 4d 65 6d 2b 31 29 3b 0a 20 20 6c 6f 61 64 41 6e  Mem+1);.  loadAn
22835 61 6c 79 73 69 73 28 70 50 61 72 73 65 2c 20 69  alysis(pParse, i
22836 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  Db);.}../*.** Ge
22837 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
22838 74 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d  the ANALYZE comm
22839 61 6e 64 2e 20 20 54 68 65 20 70 61 72 73 65 72  and.  The parser
2283a 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74   calls this rout
2283b 69 6e 65 0a 2a 2a 20 77 68 65 6e 20 69 74 20 72  ine.** when it r
2283c 65 63 6f 67 6e 69 7a 65 73 20 61 6e 20 41 4e 41  ecognizes an ANA
2283d 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a  LYZE command..**
2283e 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e 41 4c 59  .**        ANALY
2283f 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ZE              
22840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d                --
22841 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e 41   1.**        ANA
22842 4c 59 5a 45 20 20 3c 64 61 74 61 62 61 73 65 3e  LYZE  <database>
22843 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22844 2d 2d 20 32 0a 2a 2a 20 20 20 20 20 20 20 20 41  -- 2.**        A
22845 4e 41 4c 59 5a 45 20 20 3f 3c 64 61 74 61 62 61  NALYZE  ?<databa
22846 73 65 3e 2e 3f 3c 74 61 62 6c 65 6e 61 6d 65 3e  se>.?<tablename>
22847 20 20 2d 2d 20 33 0a 2a 2a 0a 2a 2a 20 46 6f 72    -- 3.**.** For
22848 6d 20 31 20 63 61 75 73 65 73 20 61 6c 6c 20 69  m 1 causes all i
22849 6e 64 69 63 65 73 20 69 6e 20 61 6c 6c 20 61 74  ndices in all at
2284a 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73  tached databases
2284b 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e   to be analyzed.
2284c 0a 2a 2a 20 46 6f 72 6d 20 32 20 61 6e 61 6c 79  .** Form 2 analy
2284d 7a 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20  zes all indices 
2284e 74 68 65 20 73 69 6e 67 6c 65 20 64 61 74 61 62  the single datab
2284f 61 73 65 20 6e 61 6d 65 64 2e 0a 2a 2a 20 46 6f  ase named..** Fo
22850 72 6d 20 33 20 61 6e 61 6c 79 7a 65 73 20 61 6c  rm 3 analyzes al
22851 6c 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69  l indices associ
22852 61 74 65 64 20 77 69 74 68 20 74 68 65 20 6e 61  ated with the na
22853 6d 65 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51  med table..*/.SQ
22854 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
22855 64 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65  d sqlite3Analyze
22856 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
22857 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 54  Token *pName1, T
22858 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a 20  oken *pName2){. 
22859 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
2285a 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74  Parse->db;.  int
2285b 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20   iDb;.  int i;. 
2285c 20 63 68 61 72 20 2a 7a 2c 20 2a 7a 44 62 3b 0a   char *z, *zDb;.
2285d 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20    Table *pTab;. 
2285e 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61   Token *pTableNa
2285f 6d 65 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74  me;..  /* Read t
22860 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
22861 6d 61 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  ma. If an error 
22862 6f 63 63 75 72 73 2c 20 6c 65 61 76 65 20 61 6e  occurs, leave an
22863 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20   error message. 
22864 20 2a 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e 20   ** and code in 
22865 70 50 61 72 73 65 20 61 6e 64 20 72 65 74 75 72  pParse and retur
22866 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73 73  n NULL. */.  ass
22867 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
22868 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73  eHoldsAllMutexes
22869 28 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a  (pParse->db) );.
2286a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
2286b 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65  =sqlite3ReadSche
2286c 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20  ma(pParse) ){.  
2286d 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
2286e 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 32 21   assert( pName2!
2286f 3d 30 20 7c 7c 20 70 4e 61 6d 65 31 3d 3d 30 20  =0 || pName1==0 
22870 29 3b 0a 20 20 69 66 28 20 70 4e 61 6d 65 31 3d  );.  if( pName1=
22871 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 46 6f 72  =0 ){.    /* For
22872 6d 20 31 3a 20 20 41 6e 61 6c 79 7a 65 20 65 76  m 1:  Analyze ev
22873 65 72 79 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20  erything */.    
22874 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
22875 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  Db; i++){.      
22876 69 66 28 20 69 3d 3d 31 20 29 20 63 6f 6e 74 69  if( i==1 ) conti
22877 6e 75 65 3b 20 20 2f 2a 20 44 6f 20 6e 6f 74 20  nue;  /* Do not 
22878 61 6e 61 6c 79 7a 65 20 74 68 65 20 54 45 4d 50  analyze the TEMP
22879 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20   database */.   
2287a 20 20 20 61 6e 61 6c 79 7a 65 44 61 74 61 62 61     analyzeDataba
2287b 73 65 28 70 50 61 72 73 65 2c 20 69 29 3b 0a 20  se(pParse, i);. 
2287c 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
2287d 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20 29 7b   pName2->n==0 ){
2287e 0a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 32 3a 20  .    /* Form 2: 
2287f 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 64 61 74   Analyze the dat
22880 61 62 61 73 65 20 6f 72 20 74 61 62 6c 65 20 6e  abase or table n
22881 61 6d 65 64 20 2a 2f 0a 20 20 20 20 69 44 62 20  amed */.    iDb 
22882 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28  = sqlite3FindDb(
22883 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20  db, pName1);.   
22884 20 69 66 28 20 69 44 62 3e 3d 30 20 29 7b 0a 20   if( iDb>=0 ){. 
22885 20 20 20 20 20 61 6e 61 6c 79 7a 65 44 61 74 61       analyzeData
22886 62 61 73 65 28 70 50 61 72 73 65 2c 20 69 44 62  base(pParse, iDb
22887 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
22888 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e      z = sqlite3N
22889 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
2288a 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 20 20   pName1);.      
2288b 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 20  if( z ){.       
2288c 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c   pTab = sqlite3L
2288d 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73  ocateTable(pPars
2288e 65 2c 20 30 2c 20 7a 2c 20 30 29 3b 0a 20 20 20  e, 0, z, 0);.   
2288f 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
22890 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20  ee(db, z);.     
22891 20 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20     if( pTab ){. 
22892 20 20 20 20 20 20 20 20 20 61 6e 61 6c 79 7a 65           analyze
22893 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54  Table(pParse, pT
22894 61 62 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ab);.        }. 
22895 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
22896 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 46 6f 72  else{.    /* For
22897 6d 20 33 3a 20 41 6e 61 6c 79 7a 65 20 74 68 65  m 3: Analyze the
22898 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64   fully qualified
22899 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20   table name */. 
2289a 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33     iDb = sqlite3
2289b 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72  TwoPartName(pPar
2289c 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d  se, pName1, pNam
2289d 65 32 2c 20 26 70 54 61 62 6c 65 4e 61 6d 65 29  e2, &pTableName)
2289e 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3e 3d 30  ;.    if( iDb>=0
2289f 20 29 7b 0a 20 20 20 20 20 20 7a 44 62 20 3d 20   ){.      zDb = 
228a0 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
228a1 6d 65 3b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71  me;.      z = sq
228a2 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
228a3 65 6e 28 64 62 2c 20 70 54 61 62 6c 65 4e 61 6d  en(db, pTableNam
228a4 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 20  e);.      if( z 
228a5 29 7b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20  ){.        pTab 
228a6 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54  = sqlite3LocateT
228a7 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20  able(pParse, 0, 
228a8 7a 2c 20 7a 44 62 29 3b 0a 20 20 20 20 20 20 20  z, zDb);.       
228a9 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
228aa 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20 20 20 69  b, z);.        i
228ab 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 20  f( pTab ){.     
228ac 20 20 20 20 20 61 6e 61 6c 79 7a 65 54 61 62 6c       analyzeTabl
228ad 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b  e(pParse, pTab);
228ae 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
228af 20 7d 0a 20 20 20 20 7d 20 20 20 0a 20 20 7d 0a   }.    }   .  }.
228b0 7d 0a 0a 2f 2a 0a 2a 2a 20 55 73 65 64 20 74 6f  }../*.** Used to
228b1 20 70 61 73 73 20 69 6e 66 6f 72 6d 61 74 69 6f   pass informatio
228b2 6e 20 66 72 6f 6d 20 74 68 65 20 61 6e 61 6c 79  n from the analy
228b3 7a 65 72 20 72 65 61 64 65 72 20 74 68 72 6f 75  zer reader throu
228b4 67 68 20 74 6f 20 74 68 65 0a 2a 2a 20 63 61 6c  gh to the.** cal
228b5 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 2e 0a 2a  lback routine..*
228b6 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
228b7 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 61 6e   analysisInfo an
228b8 61 6c 79 73 69 73 49 6e 66 6f 3b 0a 73 74 72 75  alysisInfo;.stru
228b9 63 74 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20  ct analysisInfo 
228ba 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
228bb 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
228bc 44 61 74 61 62 61 73 65 3b 0a 7d 3b 0a 0a 2f 2a  Database;.};../*
228bd 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 62 61 63  .** This callbac
228be 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63  k is invoked onc
228bf 65 20 66 6f 72 20 65 61 63 68 20 69 6e 64 65 78  e for each index
228c0 20 77 68 65 6e 20 72 65 61 64 69 6e 67 20 74 68   when reading th
228c1 65 0a 2a 2a 20 73 71 6c 69 74 65 5f 73 74 61 74  e.** sqlite_stat
228c2 31 20 74 61 62 6c 65 2e 20 20 0a 2a 2a 0a 2a 2a  1 table.  .**.**
228c3 20 20 20 20 20 61 72 67 76 5b 30 5d 20 3d 20 6e       argv[0] = n
228c4 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78  ame of the index
228c5 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b 31 5d 20  .**     argv[1] 
228c6 3d 20 72 65 73 75 6c 74 73 20 6f 66 20 61 6e 61  = results of ana
228c7 6c 79 73 69 73 20 2d 20 6f 6e 20 69 6e 74 65 67  lysis - on integ
228c8 65 72 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75  er for each colu
228c9 6d 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  mn.*/.static int
228ca 20 61 6e 61 6c 79 73 69 73 4c 6f 61 64 65 72 28   analysisLoader(
228cb 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74  void *pData, int
228cc 20 61 72 67 63 2c 20 63 68 61 72 20 2a 2a 61 72   argc, char **ar
228cd 67 76 2c 20 63 68 61 72 20 2a 2a 4e 6f 74 55 73  gv, char **NotUs
228ce 65 64 29 7b 0a 20 20 61 6e 61 6c 79 73 69 73 49  ed){.  analysisI
228cf 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 28 61 6e  nfo *pInfo = (an
228d0 61 6c 79 73 69 73 49 6e 66 6f 2a 29 70 44 61 74  alysisInfo*)pDat
228d1 61 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64  a;.  Index *pInd
228d2 65 78 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a  ex;.  int i, c;.
228d3 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 76    unsigned int v
228d4 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
228d5 7a 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72  z;..  assert( ar
228d6 67 63 3d 3d 32 20 29 3b 0a 20 20 55 4e 55 53 45  gc==2 );.  UNUSE
228d7 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74  D_PARAMETER2(Not
228d8 55 73 65 64 2c 20 61 72 67 63 29 3b 0a 0a 20 20  Used, argc);..  
228d9 69 66 28 20 61 72 67 76 3d 3d 30 20 7c 7c 20 61  if( argv==0 || a
228da 72 67 76 5b 30 5d 3d 3d 30 20 7c 7c 20 61 72 67  rgv[0]==0 || arg
228db 76 5b 31 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 72  v[1]==0 ){.    r
228dc 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70  eturn 0;.  }.  p
228dd 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 46  Index = sqlite3F
228de 69 6e 64 49 6e 64 65 78 28 70 49 6e 66 6f 2d 3e  indIndex(pInfo->
228df 64 62 2c 20 61 72 67 76 5b 30 5d 2c 20 70 49 6e  db, argv[0], pIn
228e0 66 6f 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a  fo->zDatabase);.
228e1 20 20 69 66 28 20 70 49 6e 64 65 78 3d 3d 30 20    if( pIndex==0 
228e2 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
228e3 0a 20 20 7d 0a 20 20 7a 20 3d 20 61 72 67 76 5b  .  }.  z = argv[
228e4 31 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 2a  1];.  for(i=0; *
228e5 7a 20 26 26 20 69 3c 3d 70 49 6e 64 65 78 2d 3e  z && i<=pIndex->
228e6 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20  nColumn; i++){. 
228e7 20 20 20 76 20 3d 20 30 3b 0a 20 20 20 20 77 68     v = 0;.    wh
228e8 69 6c 65 28 20 28 63 3d 7a 5b 30 5d 29 3e 3d 27  ile( (c=z[0])>='
228e9 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b 0a  0' && c<='9' ){.
228ea 20 20 20 20 20 20 76 20 3d 20 76 2a 31 30 20 2b        v = v*10 +
228eb 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20   c - '0';.      
228ec 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  z++;.    }.    p
228ed 49 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73 74 5b  Index->aiRowEst[
228ee 69 5d 20 3d 20 76 3b 0a 20 20 20 20 69 66 28 20  i] = v;.    if( 
228ef 2a 7a 3d 3d 27 20 27 20 29 20 7a 2b 2b 3b 0a 20  *z==' ' ) z++;. 
228f0 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
228f1 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 49  ../*.** If the I
228f2 6e 64 65 78 2e 61 53 61 6d 70 6c 65 20 76 61 72  ndex.aSample var
228f3 69 61 62 6c 65 20 69 73 20 6e 6f 74 20 4e 55 4c  iable is not NUL
228f4 4c 2c 20 64 65 6c 65 74 65 20 74 68 65 20 61 53  L, delete the aS
228f5 61 6d 70 6c 65 5b 5d 20 61 72 72 61 79 0a 2a 2a  ample[] array.**
228f6 20 61 6e 64 20 69 74 73 20 63 6f 6e 74 65 6e 74   and its content
228f7 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
228f8 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
228f9 33 44 65 6c 65 74 65 49 6e 64 65 78 53 61 6d 70  3DeleteIndexSamp
228fa 6c 65 73 28 49 6e 64 65 78 20 2a 70 49 64 78 29  les(Index *pIdx)
228fb 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  {.#ifdef SQLITE_
228fc 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 20 20 69  ENABLE_STAT2.  i
228fd 66 28 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65  f( pIdx->aSample
228fe 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 3b 0a 20   ){.    int j;. 
228ff 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 4d 65     sqlite3 *dbMe
22900 6d 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65  m = pIdx->pTable
22901 2d 3e 64 62 4d 65 6d 3b 0a 20 20 20 20 66 6f 72  ->dbMem;.    for
22902 28 6a 3d 30 3b 20 6a 3c 53 51 4c 49 54 45 5f 49  (j=0; j<SQLITE_I
22903 4e 44 45 58 5f 53 41 4d 50 4c 45 53 3b 20 6a 2b  NDEX_SAMPLES; j+
22904 2b 29 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 53  +){.      IndexS
22905 61 6d 70 6c 65 20 2a 70 20 3d 20 26 70 49 64 78  ample *p = &pIdx
22906 2d 3e 61 53 61 6d 70 6c 65 5b 6a 5d 3b 0a 20 20  ->aSample[j];.  
22907 20 20 20 20 69 66 28 20 70 2d 3e 65 54 79 70 65      if( p->eType
22908 3d 3d 53 51 4c 49 54 45 5f 54 45 58 54 20 7c 7c  ==SQLITE_TEXT ||
22909 20 70 2d 3e 65 54 79 70 65 3d 3d 53 51 4c 49 54   p->eType==SQLIT
2290a 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 20 20  E_BLOB ){.      
2290b 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2290c 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 64 62  pIdx->pTable->db
2290d 4d 65 6d 2c 20 70 2d 3e 75 2e 7a 29 3b 0a 20 20  Mem, p->u.z);.  
2290e 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2290f 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
22910 4d 65 6d 2c 20 70 49 64 78 2d 3e 61 53 61 6d 70  Mem, pIdx->aSamp
22911 6c 65 29 3b 0a 20 20 20 20 70 49 64 78 2d 3e 61  le);.    pIdx->a
22912 53 61 6d 70 6c 65 20 3d 20 30 3b 0a 20 20 7d 0a  Sample = 0;.  }.
22913 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50  #else.  UNUSED_P
22914 41 52 41 4d 45 54 45 52 28 70 49 64 78 29 3b 0a  ARAMETER(pIdx);.
22915 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
22916 4c 6f 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74  Load the content
22917 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73   of the sqlite_s
22918 74 61 74 31 20 61 6e 64 20 73 71 6c 69 74 65 5f  tat1 and sqlite_
22919 73 74 61 74 32 20 74 61 62 6c 65 73 2e 20 54 68  stat2 tables. Th
2291a 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66  e.** contents of
2291b 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 61 72   sqlite_stat1 ar
2291c 65 20 75 73 65 64 20 74 6f 20 70 6f 70 75 6c 61  e used to popula
2291d 74 65 20 74 68 65 20 49 6e 64 65 78 2e 61 69 52  te the Index.aiR
2291e 6f 77 45 73 74 5b 5d 0a 2a 2a 20 61 72 72 61 79  owEst[].** array
2291f 73 2e 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20  s. The contents 
22920 6f 66 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20  of sqlite_stat2 
22921 61 72 65 20 75 73 65 64 20 74 6f 20 70 6f 70 75  are used to popu
22922 6c 61 74 65 20 74 68 65 0a 2a 2a 20 49 6e 64 65  late the.** Inde
22923 78 2e 61 53 61 6d 70 6c 65 5b 5d 20 61 72 72 61  x.aSample[] arra
22924 79 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ys..**.** If the
22925 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
22926 62 6c 65 20 69 73 20 6e 6f 74 20 70 72 65 73 65  ble is not prese
22927 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  nt in the databa
22928 73 65 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  se, SQLITE_ERROR
22929 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e  .** is returned.
2292a 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 65   In this case, e
2292b 76 65 6e 20 69 66 20 53 51 4c 49 54 45 5f 45 4e  ven if SQLITE_EN
2292c 41 42 4c 45 5f 53 54 41 54 32 20 77 61 73 20 64  ABLE_STAT2 was d
2292d 65 66 69 6e 65 64 20 0a 2a 2a 20 64 75 72 69 6e  efined .** durin
2292e 67 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 61 6e  g compilation an
2292f 64 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  d the sqlite_sta
22930 74 32 20 74 61 62 6c 65 20 69 73 20 70 72 65 73  t2 table is pres
22931 65 6e 74 2c 20 6e 6f 20 64 61 74 61 20 69 73 20  ent, no data is 
22932 0a 2a 2a 20 72 65 61 64 20 66 72 6f 6d 20 69 74  .** read from it
22933 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54  ..**.** If SQLIT
22934 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 20 77  E_ENABLE_STAT2 w
22935 61 73 20 64 65 66 69 6e 65 64 20 64 75 72 69 6e  as defined durin
22936 67 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 61 6e  g compilation an
22937 64 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65  d the .** sqlite
22938 5f 73 74 61 74 32 20 74 61 62 6c 65 20 69 73 20  _stat2 table is 
22939 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e 20 74  not present in t
2293a 68 65 20 64 61 74 61 62 61 73 65 2c 20 53 51 4c  he database, SQL
2293b 49 54 45 5f 45 52 52 4f 52 20 69 73 0a 2a 2a 20  ITE_ERROR is.** 
2293c 72 65 74 75 72 6e 65 64 2e 20 48 6f 77 65 76 65  returned. Howeve
2293d 72 2c 20 69 6e 20 74 68 69 73 20 63 61 73 65 2c  r, in this case,
2293e 20 64 61 74 61 20 69 73 20 72 65 61 64 20 66 72   data is read fr
2293f 6f 6d 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  om the sqlite_st
22940 61 74 31 0a 2a 2a 20 74 61 62 6c 65 20 28 69 66  at1.** table (if
22941 20 69 74 20 69 73 20 70 72 65 73 65 6e 74 29 20   it is present) 
22942 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
22943 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 4f 4f  ..**.** If an OO
22944 4d 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  M error occurs, 
22945 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c  this function al
22946 77 61 79 73 20 73 65 74 73 20 64 62 2d 3e 6d 61  ways sets db->ma
22947 6c 6c 6f 63 46 61 69 6c 65 64 2e 0a 2a 2a 20 54  llocFailed..** T
22948 68 69 73 20 6d 65 61 6e 73 20 69 66 20 74 68 65  his means if the
22949 20 63 61 6c 6c 65 72 20 64 6f 65 73 20 6e 6f 74   caller does not
2294a 20 63 61 72 65 20 61 62 6f 75 74 20 6f 74 68 65   care about othe
2294b 72 20 65 72 72 6f 72 73 2c 20 74 68 65 20 72 65  r errors, the re
2294c 74 75 72 6e 0a 2a 2a 20 63 6f 64 65 20 6d 61 79  turn.** code may
2294d 20 62 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a   be ignored..*/.
2294e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2294f 6e 74 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 73  nt sqlite3Analys
22950 69 73 4c 6f 61 64 28 73 71 6c 69 74 65 33 20 2a  isLoad(sqlite3 *
22951 64 62 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20  db, int iDb){.  
22952 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 73 49 6e  analysisInfo sIn
22953 66 6f 3b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a  fo;.  HashElem *
22954 69 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b  i;.  char *zSql;
22955 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73  .  int rc;..  as
22956 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20  sert( iDb>=0 && 
22957 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20  iDb<db->nDb );. 
22958 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62   assert( db->aDb
22959 5b 69 44 62 5d 2e 70 42 74 21 3d 30 20 29 3b 0a  [iDb].pBt!=0 );.
2295a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
2295b 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78  3BtreeHoldsMutex
2295c 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42  (db->aDb[iDb].pB
2295d 74 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 6c 65 61  t) );..  /* Clea
2295e 72 20 61 6e 79 20 70 72 69 6f 72 20 73 74 61 74  r any prior stat
2295f 69 73 74 69 63 73 20 2a 2f 0a 20 20 66 6f 72 28  istics */.  for(
22960 69 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  i=sqliteHashFirs
22961 74 28 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  t(&db->aDb[iDb].
22962 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68  pSchema->idxHash
22963 29 3b 69 3b 69 3d 73 71 6c 69 74 65 48 61 73 68  );i;i=sqliteHash
22964 4e 65 78 74 28 69 29 29 7b 0a 20 20 20 20 49 6e  Next(i)){.    In
22965 64 65 78 20 2a 70 49 64 78 20 3d 20 73 71 6c 69  dex *pIdx = sqli
22966 74 65 48 61 73 68 44 61 74 61 28 69 29 3b 0a 20  teHashData(i);. 
22967 20 20 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c     sqlite3Defaul
22968 74 52 6f 77 45 73 74 28 70 49 64 78 29 3b 0a 20  tRowEst(pIdx);. 
22969 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65     sqlite3Delete
2296a 49 6e 64 65 78 53 61 6d 70 6c 65 73 28 70 49 64  IndexSamples(pId
2296b 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68  x);.  }..  /* Ch
2296c 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  eck to make sure
2296d 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
2296e 31 20 74 61 62 6c 65 20 65 78 69 73 74 73 20 2a  1 table exists *
2296f 2f 0a 20 20 73 49 6e 66 6f 2e 64 62 20 3d 20 64  /.  sInfo.db = d
22970 62 3b 0a 20 20 73 49 6e 66 6f 2e 7a 44 61 74 61  b;.  sInfo.zData
22971 62 61 73 65 20 3d 20 64 62 2d 3e 61 44 62 5b 69  base = db->aDb[i
22972 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28  Db].zName;.  if(
22973 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
22974 65 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 74  e(db, "sqlite_st
22975 61 74 31 22 2c 20 73 49 6e 66 6f 2e 7a 44 61 74  at1", sInfo.zDat
22976 61 62 61 73 65 29 3d 3d 30 20 29 7b 0a 20 20 20  abase)==0 ){.   
22977 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
22978 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  RROR;.  }..  /* 
22979 4c 6f 61 64 20 6e 65 77 20 73 74 61 74 69 73 74  Load new statist
2297a 69 63 73 20 6f 75 74 20 6f 66 20 74 68 65 20 73  ics out of the s
2297b 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c  qlite_stat1 tabl
2297c 65 20 2a 2f 0a 20 20 7a 53 71 6c 20 3d 20 73 71  e */.  zSql = sq
2297d 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
2297e 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54 20   .      "SELECT 
2297f 69 64 78 2c 20 73 74 61 74 20 46 52 4f 4d 20 25  idx, stat FROM %
22980 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c  Q.sqlite_stat1",
22981 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73 65   sInfo.zDatabase
22982 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30  );.  if( zSql==0
22983 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
22984 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c  ITE_NOMEM;.  }el
22985 73 65 7b 0a 20 20 20 20 28 76 6f 69 64 29 73 71  se{.    (void)sq
22986 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
22987 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  b);.    rc = sql
22988 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53  ite3_exec(db, zS
22989 71 6c 2c 20 61 6e 61 6c 79 73 69 73 4c 6f 61 64  ql, analysisLoad
2298a 65 72 2c 20 26 73 49 6e 66 6f 2c 20 30 29 3b 0a  er, &sInfo, 0);.
2298b 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
2298c 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20  3SafetyOn(db);. 
2298d 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2298e 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 7d 0a  (db, zSql);.  }.
2298f 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20  ..  /* Load the 
22990 73 74 61 74 69 73 74 69 63 73 20 66 72 6f 6d 20  statistics from 
22991 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 32  the sqlite_stat2
22992 20 74 61 62 6c 65 2e 20 2a 2f 0a 23 69 66 64 65   table. */.#ifde
22993 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
22994 53 54 41 54 32 0a 20 20 69 66 28 20 72 63 3d 3d  STAT2.  if( rc==
22995 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 73 71  SQLITE_OK && !sq
22996 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
22997 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 74 32  b, "sqlite_stat2
22998 22 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61  ", sInfo.zDataba
22999 73 65 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  se) ){.    rc = 
2299a 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
2299b 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  }.  if( rc==SQLI
2299c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c  TE_OK ){.    sql
2299d 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
2299e 20 3d 20 30 3b 0a 0a 20 20 20 20 7a 53 71 6c 20   = 0;..    zSql 
2299f 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
229a0 28 64 62 2c 20 0a 20 20 20 20 20 20 20 20 22 53  (db, .        "S
229a1 45 4c 45 43 54 20 69 64 78 2c 73 61 6d 70 6c 65  ELECT idx,sample
229a2 6e 6f 2c 73 61 6d 70 6c 65 20 46 52 4f 4d 20 25  no,sample FROM %
229a3 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 32 22 2c  Q.sqlite_stat2",
229a4 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73 65   sInfo.zDatabase
229a5 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 53 71 6c  );.    if( !zSql
229a6 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
229a7 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
229a8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 76   }else{.      (v
229a9 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
229aa 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20  yOff(db);.      
229ab 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65  rc = sqlite3_pre
229ac 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d  pare(db, zSql, -
229ad 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20  1, &pStmt, 0);. 
229ae 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74       (void)sqlit
229af 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a  e3SafetyOn(db);.
229b0 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
229b1 72 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20  ree(db, zSql);. 
229b2 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63     }..    if( rc
229b3 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
229b4 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74       (void)sqlit
229b5 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
229b6 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 73 71  .      while( sq
229b7 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74  lite3_step(pStmt
229b8 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  )==SQLITE_ROW ){
229b9 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a  .        char *z
229ba 49 6e 64 65 78 20 3d 20 28 63 68 61 72 20 2a 29  Index = (char *)
229bb 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
229bc 65 78 74 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20  ext(pStmt, 0);. 
229bd 20 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49         Index *pI
229be 64 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  dx = sqlite3Find
229bf 49 6e 64 65 78 28 64 62 2c 20 7a 49 6e 64 65 78  Index(db, zIndex
229c0 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73  , sInfo.zDatabas
229c1 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  e);.        if( 
229c2 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20 20 20  pIdx ){.        
229c3 20 20 69 6e 74 20 69 53 61 6d 70 6c 65 20 3d 20    int iSample = 
229c4 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
229c5 6e 74 28 70 53 74 6d 74 2c 20 31 29 3b 0a 20 20  nt(pStmt, 1);.  
229c6 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 20          sqlite3 
229c7 2a 64 62 4d 65 6d 20 3d 20 70 49 64 78 2d 3e 70  *dbMem = pIdx->p
229c8 54 61 62 6c 65 2d 3e 64 62 4d 65 6d 3b 0a 20 20  Table->dbMem;.  
229c9 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
229ca 64 62 4d 65 6d 3d 3d 64 62 20 7c 7c 20 64 62 4d  dbMem==db || dbM
229cb 65 6d 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  em==0 );.       
229cc 20 20 20 69 66 28 20 69 53 61 6d 70 6c 65 3c 53     if( iSample<S
229cd 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50  QLITE_INDEX_SAMP
229ce 4c 45 53 20 26 26 20 69 53 61 6d 70 6c 65 3e 3d  LES && iSample>=
229cf 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
229d0 20 69 6e 74 20 65 54 79 70 65 20 3d 20 73 71 6c   int eType = sql
229d1 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65  ite3_column_type
229d2 28 70 53 74 6d 74 2c 20 32 29 3b 0a 0a 20 20 20  (pStmt, 2);..   
229d3 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64           if( pId
229d4 78 2d 3e 61 53 61 6d 70 6c 65 3d 3d 30 20 29 7b  x->aSample==0 ){
229d5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  .              s
229d6 74 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20  tatic const int 
229d7 73 7a 20 3d 20 73 69 7a 65 6f 66 28 49 6e 64 65  sz = sizeof(Inde
229d8 78 53 61 6d 70 6c 65 29 2a 53 51 4c 49 54 45 5f  xSample)*SQLITE_
229d9 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 3b 0a 20  INDEX_SAMPLES;. 
229da 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 64               pId
229db 78 2d 3e 61 53 61 6d 70 6c 65 20 3d 20 28 49 6e  x->aSample = (In
229dc 64 65 78 53 61 6d 70 6c 65 20 2a 29 73 71 6c 69  dexSample *)sqli
229dd 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
229de 64 62 4d 65 6d 2c 20 73 7a 29 3b 0a 20 20 20 20  dbMem, sz);.    
229df 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49            if( pI
229e0 64 78 2d 3e 61 53 61 6d 70 6c 65 3d 3d 30 20 29  dx->aSample==0 )
229e1 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
229e2 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
229e3 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ed = 1;.        
229e4 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
229e5 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
229e6 20 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20             }..  
229e7 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
229e8 28 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 20  ( pIdx->aSample 
229e9 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  );.            {
229ea 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 49  .              I
229eb 6e 64 65 78 53 61 6d 70 6c 65 20 2a 70 53 61 6d  ndexSample *pSam
229ec 70 6c 65 20 3d 20 26 70 49 64 78 2d 3e 61 53 61  ple = &pIdx->aSa
229ed 6d 70 6c 65 5b 69 53 61 6d 70 6c 65 5d 3b 0a 20  mple[iSample];. 
229ee 20 20 20 20 20 20 20 20 20 20 20 20 20 70 53 61               pSa
229ef 6d 70 6c 65 2d 3e 65 54 79 70 65 20 3d 20 28 75  mple->eType = (u
229f0 38 29 65 54 79 70 65 3b 0a 20 20 20 20 20 20 20  8)eType;.       
229f1 20 20 20 20 20 20 20 69 66 28 20 65 54 79 70 65         if( eType
229f2 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  ==SQLITE_INTEGER
229f3 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54   || eType==SQLIT
229f4 45 5f 46 4c 4f 41 54 20 29 7b 0a 20 20 20 20 20  E_FLOAT ){.     
229f5 20 20 20 20 20 20 20 20 20 20 20 70 53 61 6d 70             pSamp
229f6 6c 65 2d 3e 75 2e 72 20 3d 20 73 71 6c 69 74 65  le->u.r = sqlite
229f7 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28  3_column_double(
229f8 70 53 74 6d 74 2c 20 32 29 3b 0a 20 20 20 20 20  pStmt, 2);.     
229f9 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69           }else i
229fa 66 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45  f( eType==SQLITE
229fb 5f 54 45 58 54 20 7c 7c 20 65 54 79 70 65 3d 3d  _TEXT || eType==
229fc 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20  SQLITE_BLOB ){. 
229fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
229fe 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28  onst char *z = (
229ff 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 28 0a 20  const char *)(. 
22a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22a01 20 20 20 28 65 54 79 70 65 3d 3d 53 51 4c 49 54     (eType==SQLIT
22a02 45 5f 42 4c 4f 42 29 20 3f 0a 20 20 20 20 20 20  E_BLOB) ?.      
22a03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
22a04 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
22a05 62 28 70 53 74 6d 74 2c 20 32 29 3a 0a 20 20 20  b(pStmt, 2):.   
22a06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22a07 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
22a08 74 65 78 74 28 70 53 74 6d 74 2c 20 32 29 0a 20  text(pStmt, 2). 
22a09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 29                 )
22a0a 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
22a0b 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65    int n = sqlite
22a0c 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70  3_column_bytes(p
22a0d 53 74 6d 74 2c 20 32 29 3b 0a 20 20 20 20 20 20  Stmt, 2);.      
22a0e 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 3e            if( n>
22a0f 32 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  24 ){.          
22a10 20 20 20 20 20 20 20 20 6e 20 3d 20 32 34 3b 0a          n = 24;.
22a11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22a12 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }.              
22a13 20 20 70 53 61 6d 70 6c 65 2d 3e 6e 42 79 74 65    pSample->nByte
22a14 20 3d 20 28 75 38 29 6e 3b 0a 20 20 20 20 20 20   = (u8)n;.      
22a15 20 20 20 20 20 20 20 20 20 20 70 53 61 6d 70 6c            pSampl
22a16 65 2d 3e 75 2e 7a 20 3d 20 73 71 6c 69 74 65 33  e->u.z = sqlite3
22a17 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 4d 65  DbMallocRaw(dbMe
22a18 6d 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20 20 20  m, n);.         
22a19 20 20 20 20 20 20 20 69 66 28 20 70 53 61 6d 70         if( pSamp
22a1a 6c 65 2d 3e 75 2e 7a 20 29 7b 0a 20 20 20 20 20  le->u.z ){.     
22a1b 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d               mem
22a1c 63 70 79 28 70 53 61 6d 70 6c 65 2d 3e 75 2e 7a  cpy(pSample->u.z
22a1d 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20  , z, n);.       
22a1e 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
22a1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22a20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
22a21 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ed = 1;.        
22a22 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
22a23 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
22a24 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   }.             
22a25 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d   }.            }
22a26 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
22a27 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
22a28 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
22a29 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74  3_finalize(pStmt
22a2a 29 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73  );.      (void)s
22a2b 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
22a2c 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  b);.    }.  }.#e
22a2d 6e 64 69 66 0a 0a 20 20 69 66 28 20 72 63 3d 3d  ndif..  if( rc==
22a2e 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a  SQLITE_NOMEM ){.
22a2f 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
22a30 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  iled = 1;.  }.  
22a31 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23  return rc;.}...#
22a32 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
22a33 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 20 2a 2f 0a  OMIT_ANALYZE */.
22a34 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
22a35 20 45 6e 64 20 6f 66 20 61 6e 61 6c 79 7a 65 2e   End of analyze.
22a36 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
22a37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a38 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
22a39 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
22a3a 20 42 65 67 69 6e 20 66 69 6c 65 20 61 74 74 61   Begin file atta
22a3b 63 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ch.c ***********
22a3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
22a3e 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 41 70 72 69  ./*.** 2003 Apri
22a3f 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75  l 6.**.** The au
22a40 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
22a41 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
22a42 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
22a43 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
22a44 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
22a45 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
22a46 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
22a47 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
22a48 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
22a49 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
22a4a 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
22a4b 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
22a4c 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
22a4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
22a4e 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
22a4f 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
22a50 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
22a51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a55 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
22a56 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73  contains code us
22a57 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
22a58 74 68 65 20 41 54 54 41 43 48 20 61 6e 64 20 44  the ATTACH and D
22a59 45 54 41 43 48 20 63 6f 6d 6d 61 6e 64 73 2e 0a  ETACH commands..
22a5a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 61 74 74 61 63  **.** $Id: attac
22a5b 68 2e 63 2c 76 20 31 2e 39 33 20 32 30 30 39 2f  h.c,v 1.93 2009/
22a5c 30 35 2f 33 31 20 32 31 3a 32 31 3a 34 31 20 64  05/31 21:21:41 d
22a5d 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66  rh Exp $.*/..#if
22a5e 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
22a5f 5f 41 54 54 41 43 48 0a 2f 2a 0a 2a 2a 20 52 65  _ATTACH./*.** Re
22a60 73 6f 6c 76 65 20 61 6e 20 65 78 70 72 65 73 73  solve an express
22a61 69 6f 6e 20 74 68 61 74 20 77 61 73 20 70 61 72  ion that was par
22a62 74 20 6f 66 20 61 6e 20 41 54 54 41 43 48 20 6f  t of an ATTACH o
22a63 72 20 44 45 54 41 43 48 20 73 74 61 74 65 6d 65  r DETACH stateme
22a64 6e 74 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20 73  nt. This.** is s
22a65 6c 69 67 68 74 6c 79 20 64 69 66 66 65 72 65 6e  lightly differen
22a66 74 20 66 72 6f 6d 20 72 65 73 6f 6c 76 69 6e 67  t from resolving
22a67 20 61 20 6e 6f 72 6d 61 6c 20 53 51 4c 20 65 78   a normal SQL ex
22a68 70 72 65 73 73 69 6f 6e 2c 20 62 65 63 61 75 73  pression, becaus
22a69 65 20 73 69 6d 70 6c 65 0a 2a 2a 20 69 64 65 6e  e simple.** iden
22a6a 74 69 66 69 65 72 73 20 61 72 65 20 74 72 65 61  tifiers are trea
22a6b 74 65 64 20 61 73 20 73 74 72 69 6e 67 73 2c 20  ted as strings, 
22a6c 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 63 6f 6c  not possible col
22a6d 75 6d 6e 20 6e 61 6d 65 73 20 6f 72 20 61 6c 69  umn names or ali
22a6e 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e  ases..**.** i.e.
22a6f 20 69 66 20 74 68 65 20 70 61 72 73 65 72 20 73   if the parser s
22a70 65 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41  ees:.**.**     A
22a71 54 54 41 43 48 20 44 41 54 41 42 41 53 45 20 61  TTACH DATABASE a
22a72 62 63 20 41 53 20 64 65 66 0a 2a 2a 0a 2a 2a 20  bc AS def.**.** 
22a73 69 74 20 74 72 65 61 74 73 20 74 68 65 20 74 77  it treats the tw
22a74 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 73  o expressions as
22a75 20 6c 69 74 65 72 61 6c 20 73 74 72 69 6e 67 73   literal strings
22a76 20 27 61 62 63 27 20 61 6e 64 20 27 64 65 66 27   'abc' and 'def'
22a77 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 20 6c   instead of.** l
22a78 6f 6f 6b 69 6e 67 20 66 6f 72 20 63 6f 6c 75 6d  ooking for colum
22a79 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e  ns of the same n
22a7a 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ame..**.** This 
22a7b 6f 6e 6c 79 20 61 70 70 6c 69 65 73 20 74 6f 20  only applies to 
22a7c 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6f 66  the root node of
22a7d 20 70 45 78 70 72 2c 20 73 6f 20 74 68 65 20 73   pExpr, so the s
22a7e 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20  tatement:.**.** 
22a7f 20 20 20 20 41 54 54 41 43 48 20 44 41 54 41 42      ATTACH DATAB
22a80 41 53 45 20 61 62 63 7c 7c 64 65 66 20 41 53 20  ASE abc||def AS 
22a81 27 64 62 32 27 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c  'db2'.**.** will
22a82 20 66 61 69 6c 20 62 65 63 61 75 73 65 20 6e 65   fail because ne
22a83 69 74 68 65 72 20 61 62 63 20 6f 72 20 64 65 66  ither abc or def
22a84 20 63 61 6e 20 62 65 20 72 65 73 6f 6c 76 65 64   can be resolved
22a85 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
22a86 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70  resolveAttachExp
22a87 72 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70  r(NameContext *p
22a88 4e 61 6d 65 2c 20 45 78 70 72 20 2a 70 45 78 70  Name, Expr *pExp
22a89 72 29 0a 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  r).{.  int rc = 
22a8a 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
22a8b 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20 69 66   pExpr ){.    if
22a8c 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f  ( pExpr->op!=TK_
22a8d 49 44 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  ID ){.      rc =
22a8e 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
22a8f 78 70 72 4e 61 6d 65 73 28 70 4e 61 6d 65 2c 20  xprNames(pName, 
22a90 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 69 66  pExpr);.      if
22a91 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
22a92 26 26 20 21 73 71 6c 69 74 65 33 45 78 70 72 49  && !sqlite3ExprI
22a93 73 43 6f 6e 73 74 61 6e 74 28 70 45 78 70 72 29  sConstant(pExpr)
22a94 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
22a95 74 65 33 45 72 72 6f 72 4d 73 67 28 70 4e 61 6d  te3ErrorMsg(pNam
22a96 65 2d 3e 70 50 61 72 73 65 2c 20 22 69 6e 76 61  e->pParse, "inva
22a97 6c 69 64 20 6e 61 6d 65 3a 20 5c 22 25 73 5c 22  lid name: \"%s\"
22a98 22 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b  ", pExpr->u.zTok
22a99 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  en);.        ret
22a9a 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
22a9b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
22a9c 6c 73 65 7b 0a 20 20 20 20 20 20 70 45 78 70 72  lse{.      pExpr
22a9d 2d 3e 6f 70 20 3d 20 54 4b 5f 53 54 52 49 4e 47  ->op = TK_STRING
22a9e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
22a9f 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
22aa0 2a 20 41 6e 20 53 51 4c 20 75 73 65 72 2d 66 75  * An SQL user-fu
22aa1 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65  nction registere
22aa2 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b  d to do the work
22aa3 20 6f 66 20 61 6e 20 41 54 54 41 43 48 20 73 74   of an ATTACH st
22aa4 61 74 65 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20  atement. The.** 
22aa5 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 20  three arguments 
22aa6 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  to the function 
22aa7 63 6f 6d 65 20 64 69 72 65 63 74 6c 79 20 66 72  come directly fr
22aa8 6f 6d 20 61 6e 20 61 74 74 61 63 68 20 73 74 61  om an attach sta
22aa9 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20  tement:.**.**   
22aaa 20 20 41 54 54 41 43 48 20 44 41 54 41 42 41 53    ATTACH DATABAS
22aab 45 20 78 20 41 53 20 79 20 4b 45 59 20 7a 0a 2a  E x AS y KEY z.*
22aac 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20  *.**     SELECT 
22aad 73 71 6c 69 74 65 5f 61 74 74 61 63 68 28 78 2c  sqlite_attach(x,
22aae 20 79 2c 20 7a 29 0a 2a 2a 0a 2a 2a 20 49 66 20   y, z).**.** If 
22aaf 74 68 65 20 6f 70 74 69 6f 6e 61 6c 20 22 4b 45  the optional "KE
22ab0 59 20 7a 22 20 73 79 6e 74 61 78 20 69 73 20 6f  Y z" syntax is o
22ab1 6d 69 74 74 65 64 2c 20 61 6e 20 53 51 4c 20 4e  mitted, an SQL N
22ab2 55 4c 4c 20 69 73 20 70 61 73 73 65 64 20 61 73  ULL is passed as
22ab3 20 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 72   the.** third ar
22ab4 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69  gument..*/.stati
22ab5 63 20 76 6f 69 64 20 61 74 74 61 63 68 46 75 6e  c void attachFun
22ab6 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
22ab7 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
22ab8 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20   int NotUsed,.  
22ab9 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
22aba 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  argv.){.  int i;
22abb 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20  .  int rc = 0;. 
22abc 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
22abd 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
22abe 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
22abf 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  );.  const char 
22ac0 2a 7a 4e 61 6d 65 3b 0a 20 20 63 6f 6e 73 74 20  *zName;.  const 
22ac1 63 68 61 72 20 2a 7a 46 69 6c 65 3b 0a 20 20 44  char *zFile;.  D
22ac2 62 20 2a 61 4e 65 77 3b 0a 20 20 63 68 61 72 20  b *aNew;.  char 
22ac3 2a 7a 45 72 72 44 79 6e 20 3d 20 30 3b 0a 0a 20  *zErrDyn = 0;.. 
22ac4 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
22ac5 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 7a  R(NotUsed);..  z
22ac6 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68  File = (const ch
22ac7 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
22ac8 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
22ac9 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e  ;.  zName = (con
22aca 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65  st char *)sqlite
22acb 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
22acc 76 5b 31 5d 29 3b 0a 20 20 69 66 28 20 7a 46 69  v[1]);.  if( zFi
22acd 6c 65 3d 3d 30 20 29 20 7a 46 69 6c 65 20 3d 20  le==0 ) zFile = 
22ace 22 22 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d  "";.  if( zName=
22acf 3d 30 20 29 20 7a 4e 61 6d 65 20 3d 20 22 22 3b  =0 ) zName = "";
22ad0 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72  ..  /* Check for
22ad1 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65   the following e
22ad2 72 72 6f 72 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  rrors:.  **.  **
22ad3 20 20 20 20 20 2a 20 54 6f 6f 20 6d 61 6e 79 20       * Too many 
22ad4 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
22ad5 65 73 2c 0a 20 20 2a 2a 20 20 20 20 20 2a 20 54  es,.  **     * T
22ad6 72 61 6e 73 61 63 74 69 6f 6e 20 63 75 72 72 65  ransaction curre
22ad7 6e 74 6c 79 20 6f 70 65 6e 0a 20 20 2a 2a 20 20  ntly open.  **  
22ad8 20 20 20 2a 20 53 70 65 63 69 66 69 65 64 20 64     * Specified d
22ad9 61 74 61 62 61 73 65 20 6e 61 6d 65 20 61 6c 72  atabase name alr
22ada 65 61 64 79 20 62 65 69 6e 67 20 75 73 65 64 2e  eady being used.
22adb 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  .  */.  if( db->
22adc 6e 44 62 3e 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b  nDb>=db->aLimit[
22add 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54  SQLITE_LIMIT_ATT
22ade 41 43 48 45 44 5d 2b 32 20 29 7b 0a 20 20 20 20  ACHED]+2 ){.    
22adf 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65  zErrDyn = sqlite
22ae0 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 74 6f  3MPrintf(db, "to
22ae1 6f 20 6d 61 6e 79 20 61 74 74 61 63 68 65 64 20  o many attached 
22ae2 64 61 74 61 62 61 73 65 73 20 2d 20 6d 61 78 20  databases - max 
22ae3 25 64 22 2c 20 0a 20 20 20 20 20 20 64 62 2d 3e  %d", .      db->
22ae4 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
22ae5 4d 49 54 5f 41 54 54 41 43 48 45 44 5d 0a 20 20  MIT_ATTACHED].  
22ae6 20 20 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 74    );.    goto at
22ae7 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a  tach_error;.  }.
22ae8 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f 43    if( !db->autoC
22ae9 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 7a 45 72  ommit ){.    zEr
22aea 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50  rDyn = sqlite3MP
22aeb 72 69 6e 74 66 28 64 62 2c 20 22 63 61 6e 6e 6f  rintf(db, "canno
22aec 74 20 41 54 54 41 43 48 20 64 61 74 61 62 61 73  t ATTACH databas
22aed 65 20 77 69 74 68 69 6e 20 74 72 61 6e 73 61 63  e within transac
22aee 74 69 6f 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f  tion");.    goto
22aef 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20   attach_error;. 
22af0 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c   }.  for(i=0; i<
22af1 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20  db->nDb; i++){. 
22af2 20 20 20 63 68 61 72 20 2a 7a 20 3d 20 64 62 2d     char *z = db-
22af3 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20  >aDb[i].zName;. 
22af4 20 20 20 61 73 73 65 72 74 28 20 7a 20 26 26 20     assert( z && 
22af5 7a 4e 61 6d 65 20 29 3b 0a 20 20 20 20 69 66 28  zName );.    if(
22af6 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
22af7 7a 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a  z, zName)==0 ){.
22af8 20 20 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20        zErrDyn = 
22af9 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
22afa 62 2c 20 22 64 61 74 61 62 61 73 65 20 25 73 20  b, "database %s 
22afb 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 75 73  is already in us
22afc 65 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  e", zName);.    
22afd 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 72    goto attach_er
22afe 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ror;.    }.  }..
22aff 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68    /* Allocate th
22b00 65 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 20 74  e new entry in t
22b01 68 65 20 64 62 2d 3e 61 44 62 5b 5d 20 61 72 72  he db->aDb[] arr
22b02 61 79 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 73  ay and initialis
22b03 65 20 74 68 65 20 73 63 68 65 6d 61 0a 20 20 2a  e the schema.  *
22b04 2a 20 68 61 73 68 20 74 61 62 6c 65 73 2e 0a 20  * hash tables.. 
22b05 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 61 44   */.  if( db->aD
22b06 62 3d 3d 64 62 2d 3e 61 44 62 53 74 61 74 69 63  b==db->aDbStatic
22b07 20 29 7b 0a 20 20 20 20 61 4e 65 77 20 3d 20 73   ){.    aNew = s
22b08 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
22b09 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 64 62 2d  w(db, sizeof(db-
22b0a 3e 61 44 62 5b 30 5d 29 2a 33 20 29 3b 0a 20 20  >aDb[0])*3 );.  
22b0b 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 20    if( aNew==0 ) 
22b0c 72 65 74 75 72 6e 3b 0a 20 20 20 20 6d 65 6d 63  return;.    memc
22b0d 70 79 28 61 4e 65 77 2c 20 64 62 2d 3e 61 44 62  py(aNew, db->aDb
22b0e 2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62  , sizeof(db->aDb
22b0f 5b 30 5d 29 2a 32 29 3b 0a 20 20 7d 65 6c 73 65  [0])*2);.  }else
22b10 7b 0a 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c  {.    aNew = sql
22b11 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62  ite3DbRealloc(db
22b12 2c 20 64 62 2d 3e 61 44 62 2c 20 73 69 7a 65 6f  , db->aDb, sizeo
22b13 66 28 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 28 64  f(db->aDb[0])*(d
22b14 62 2d 3e 6e 44 62 2b 31 29 20 29 3b 0a 20 20 20  b->nDb+1) );.   
22b15 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 20 72   if( aNew==0 ) r
22b16 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 64 62 2d  eturn;.  }.  db-
22b17 3e 61 44 62 20 3d 20 61 4e 65 77 3b 0a 20 20 61  >aDb = aNew;.  a
22b18 4e 65 77 20 3d 20 26 64 62 2d 3e 61 44 62 5b 64  New = &db->aDb[d
22b19 62 2d 3e 6e 44 62 5d 3b 0a 20 20 6d 65 6d 73 65  b->nDb];.  memse
22b1a 74 28 61 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f  t(aNew, 0, sizeo
22b1b 66 28 2a 61 4e 65 77 29 29 3b 0a 0a 20 20 2f 2a  f(*aNew));..  /*
22b1c 20 4f 70 65 6e 20 74 68 65 20 64 61 74 61 62 61   Open the databa
22b1d 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20  se file. If the 
22b1e 62 74 72 65 65 20 69 73 20 73 75 63 63 65 73 73  btree is success
22b1f 66 75 6c 6c 79 20 6f 70 65 6e 65 64 2c 20 75 73  fully opened, us
22b20 65 0a 20 20 2a 2a 20 69 74 20 74 6f 20 6f 62 74  e.  ** it to obt
22b21 61 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  ain the database
22b22 20 73 63 68 65 6d 61 2e 20 41 74 20 74 68 69 73   schema. At this
22b23 20 70 6f 69 6e 74 20 74 68 65 20 73 63 68 65 6d   point the schem
22b24 61 20 6d 61 79 0a 20 20 2a 2a 20 6f 72 20 6d 61  a may.  ** or ma
22b25 79 20 6e 6f 74 20 62 65 20 69 6e 69 74 69 61 6c  y not be initial
22b26 69 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20  ised..  */.  rc 
22b27 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 61  = sqlite3BtreeFa
22b28 63 74 6f 72 79 28 64 62 2c 20 7a 46 69 6c 65 2c  ctory(db, zFile,
22b29 20 30 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55   0, SQLITE_DEFAU
22b2a 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 2c 0a 20  LT_CACHE_SIZE,. 
22b2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22b2c 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6f 70            db->op
22b2d 65 6e 46 6c 61 67 73 20 7c 20 53 51 4c 49 54 45  enFlags | SQLITE
22b2e 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 2c 0a 20  _OPEN_MAIN_DB,. 
22b2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22b30 20 20 20 20 20 20 20 20 20 20 26 61 4e 65 77 2d            &aNew-
22b31 3e 70 42 74 29 3b 0a 20 20 64 62 2d 3e 6e 44 62  >pBt);.  db->nDb
22b32 2b 2b 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  ++;.  if( rc==SQ
22b33 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20  LITE_CONSTRAINT 
22b34 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
22b35 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7a 45  TE_ERROR;.    zE
22b36 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d  rrDyn = sqlite3M
22b37 50 72 69 6e 74 66 28 64 62 2c 20 22 64 61 74 61  Printf(db, "data
22b38 62 61 73 65 20 69 73 20 61 6c 72 65 61 64 79 20  base is already 
22b39 61 74 74 61 63 68 65 64 22 29 3b 0a 20 20 7d 65  attached");.  }e
22b3a 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  lse if( rc==SQLI
22b3b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 50 61 67  TE_OK ){.    Pag
22b3c 65 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 20  er *pPager;.    
22b3d 61 4e 65 77 2d 3e 70 53 63 68 65 6d 61 20 3d 20  aNew->pSchema = 
22b3e 73 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74  sqlite3SchemaGet
22b3f 28 64 62 2c 20 61 4e 65 77 2d 3e 70 42 74 29 3b  (db, aNew->pBt);
22b40 0a 20 20 20 20 69 66 28 20 21 61 4e 65 77 2d 3e  .    if( !aNew->
22b41 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20  pSchema ){.     
22b42 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
22b43 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  EM;.    }else if
22b44 28 20 61 4e 65 77 2d 3e 70 53 63 68 65 6d 61 2d  ( aNew->pSchema-
22b45 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20 26 26 20  >file_format && 
22b46 61 4e 65 77 2d 3e 70 53 63 68 65 6d 61 2d 3e 65  aNew->pSchema->e
22b47 6e 63 21 3d 45 4e 43 28 64 62 29 20 29 7b 0a 20  nc!=ENC(db) ){. 
22b48 20 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20 73       zErrDyn = s
22b49 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
22b4a 2c 20 0a 20 20 20 20 20 20 20 20 22 61 74 74 61  , .        "atta
22b4b 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20 6d  ched databases m
22b4c 75 73 74 20 75 73 65 20 74 68 65 20 73 61 6d 65  ust use the same
22b4d 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 61   text encoding a
22b4e 73 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 22  s main database"
22b4f 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  );.      rc = SQ
22b50 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
22b51 7d 0a 20 20 20 20 70 50 61 67 65 72 20 3d 20 73  }.    pPager = s
22b52 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
22b53 28 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 20  (aNew->pBt);.   
22b54 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63   sqlite3PagerLoc
22b55 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67 65 72 2c  kingMode(pPager,
22b56 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64   db->dfltLockMod
22b57 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50  e);.    sqlite3P
22b58 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28  agerJournalMode(
22b59 70 50 61 67 65 72 2c 20 64 62 2d 3e 64 66 6c 74  pPager, db->dflt
22b5a 4a 6f 75 72 6e 61 6c 4d 6f 64 65 29 3b 0a 20 20  JournalMode);.  
22b5b 7d 0a 20 20 61 4e 65 77 2d 3e 7a 4e 61 6d 65 20  }.  aNew->zName 
22b5c 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
22b5d 70 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  p(db, zName);.  
22b5e 61 4e 65 77 2d 3e 73 61 66 65 74 79 5f 6c 65 76  aNew->safety_lev
22b5f 65 6c 20 3d 20 33 3b 0a 0a 23 69 66 20 53 51 4c  el = 3;..#if SQL
22b60 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20  ITE_HAS_CODEC.  
22b61 7b 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e 74  {.    extern int
22b62 20 73 71 6c 69 74 65 33 43 6f 64 65 63 41 74 74   sqlite3CodecAtt
22b63 61 63 68 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e  ach(sqlite3*, in
22b64 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20  t, const void*, 
22b65 69 6e 74 29 3b 0a 20 20 20 20 65 78 74 65 72 6e  int);.    extern
22b66 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64   void sqlite3Cod
22b67 65 63 47 65 74 4b 65 79 28 73 71 6c 69 74 65 33  ecGetKey(sqlite3
22b68 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 2a 2a 2c 20  *, int, void**, 
22b69 69 6e 74 2a 29 3b 0a 20 20 20 20 69 6e 74 20 6e  int*);.    int n
22b6a 4b 65 79 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a  Key;.    char *z
22b6b 4b 65 79 3b 0a 20 20 20 20 69 6e 74 20 74 20 3d  Key;.    int t =
22b6c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
22b6d 79 70 65 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20  ype(argv[2]);.  
22b6e 20 20 73 77 69 74 63 68 28 20 74 20 29 7b 0a 20    switch( t ){. 
22b6f 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45       case SQLITE
22b70 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 20 20  _INTEGER:.      
22b71 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41  case SQLITE_FLOA
22b72 54 3a 0a 20 20 20 20 20 20 20 20 7a 45 72 72 44  T:.        zErrD
22b73 79 6e 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  yn = sqlite3DbSt
22b74 72 44 75 70 28 64 62 2c 20 22 49 6e 76 61 6c 69  rDup(db, "Invali
22b75 64 20 6b 65 79 20 76 61 6c 75 65 22 29 3b 0a 20  d key value");. 
22b76 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
22b77 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20  TE_ERROR;.      
22b78 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
22b79 20 0a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c   .      case SQL
22b7a 49 54 45 5f 54 45 58 54 3a 0a 20 20 20 20 20 20  ITE_TEXT:.      
22b7b 63 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42  case SQLITE_BLOB
22b7c 3a 0a 20 20 20 20 20 20 20 20 6e 4b 65 79 20 3d  :.        nKey =
22b7d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
22b7e 79 74 65 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20  ytes(argv[2]);. 
22b7f 20 20 20 20 20 20 20 7a 4b 65 79 20 3d 20 28 63         zKey = (c
22b80 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  har *)sqlite3_va
22b81 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 32 5d  lue_blob(argv[2]
22b82 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
22b83 65 33 43 6f 64 65 63 41 74 74 61 63 68 28 64 62  e3CodecAttach(db
22b84 2c 20 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65  , db->nDb-1, zKe
22b85 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20  y, nKey);.      
22b86 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 20 20    break;..      
22b87 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  case SQLITE_NULL
22b88 3a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20  :.        /* No 
22b89 6b 65 79 20 73 70 65 63 69 66 69 65 64 2e 20 20  key specified.  
22b8a 55 73 65 20 74 68 65 20 6b 65 79 20 66 72 6f 6d  Use the key from
22b8b 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
22b8c 73 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71  se */.        sq
22b8d 6c 69 74 65 33 43 6f 64 65 63 47 65 74 4b 65 79  lite3CodecGetKey
22b8e 28 64 62 2c 20 30 2c 20 28 76 6f 69 64 2a 2a 29  (db, 0, (void**)
22b8f 26 7a 4b 65 79 2c 20 26 6e 4b 65 79 29 3b 0a 20  &zKey, &nKey);. 
22b90 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f         sqlite3Co
22b91 64 65 63 41 74 74 61 63 68 28 64 62 2c 20 64 62  decAttach(db, db
22b92 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65 79 2c 20 6e  ->nDb-1, zKey, n
22b93 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 62 72  Key);.        br
22b94 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  eak;.    }.  }.#
22b95 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74  endif..  /* If t
22b96 68 65 20 66 69 6c 65 20 77 61 73 20 6f 70 65 6e  he file was open
22b97 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c  ed successfully,
22b98 20 72 65 61 64 20 74 68 65 20 73 63 68 65 6d 61   read the schema
22b99 20 66 6f 72 20 74 68 65 20 6e 65 77 20 64 61 74   for the new dat
22b9a 61 62 61 73 65 2e 0a 20 20 2a 2a 20 49 66 20 74  abase..  ** If t
22b9b 68 69 73 20 66 61 69 6c 73 2c 20 6f 72 20 69 66  his fails, or if
22b9c 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 69 6c   opening the fil
22b9d 65 20 66 61 69 6c 65 64 2c 20 74 68 65 6e 20 63  e failed, then c
22b9e 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 61 6e  lose the file an
22b9f 64 20 0a 20 20 2a 2a 20 72 65 6d 6f 76 65 20 74  d .  ** remove t
22ba0 68 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68  he entry from th
22ba1 65 20 64 62 2d 3e 61 44 62 5b 5d 20 61 72 72 61  e db->aDb[] arra
22ba2 79 2e 20 69 2e 65 2e 20 70 75 74 20 65 76 65 72  y. i.e. put ever
22ba3 79 74 68 69 6e 67 20 62 61 63 6b 20 74 68 65 20  ything back the 
22ba4 77 61 79 0a 20 20 2a 2a 20 77 65 20 66 6f 75 6e  way.  ** we foun
22ba5 64 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  d it..  */.  if(
22ba6 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
22ba7 7b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69  {.    (void)sqli
22ba8 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b  te3SafetyOn(db);
22ba9 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
22baa 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20  eEnterAll(db);. 
22bab 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49     rc = sqlite3I
22bac 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 44 79 6e  nit(db, &zErrDyn
22bad 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  );.    sqlite3Bt
22bae 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b  reeLeaveAll(db);
22baf 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74  .    (void)sqlit
22bb0 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
22bb1 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 20 29 7b  .  }.  if( rc ){
22bb2 0a 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20 64  .    int iDb = d
22bb3 62 2d 3e 6e 44 62 20 2d 20 31 3b 0a 20 20 20 20  b->nDb - 1;.    
22bb4 61 73 73 65 72 74 28 20 69 44 62 3e 3d 32 20 29  assert( iDb>=2 )
22bb5 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44  ;.    if( db->aD
22bb6 62 5b 69 44 62 5d 2e 70 42 74 20 29 7b 0a 20 20  b[iDb].pBt ){.  
22bb7 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
22bb8 43 6c 6f 73 65 28 64 62 2d 3e 61 44 62 5b 69 44  Close(db->aDb[iD
22bb9 62 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20 20 64  b].pBt);.      d
22bba 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 20  b->aDb[iDb].pBt 
22bbb 3d 20 30 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61  = 0;.      db->a
22bbc 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 20  Db[iDb].pSchema 
22bbd 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  = 0;.    }.    s
22bbe 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
22bbf 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29  nalSchema(db, 0)
22bc0 3b 0a 20 20 20 20 64 62 2d 3e 6e 44 62 20 3d 20  ;.    db->nDb = 
22bc1 69 44 62 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  iDb;.    if( rc=
22bc2 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c  =SQLITE_NOMEM ||
22bc3 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52   rc==SQLITE_IOER
22bc4 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20  R_NOMEM ){.     
22bc5 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
22bc6 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 73 71 6c  d = 1;.      sql
22bc7 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
22bc8 45 72 72 44 79 6e 29 3b 0a 20 20 20 20 20 20 7a  ErrDyn);.      z
22bc9 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33  ErrDyn = sqlite3
22bca 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6f 75 74  MPrintf(db, "out
22bcb 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20   of memory");.  
22bcc 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 45 72 72    }else if( zErr
22bcd 44 79 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Dyn==0 ){.      
22bce 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65  zErrDyn = sqlite
22bcf 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 75 6e  3MPrintf(db, "un
22bd0 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 64 61 74  able to open dat
22bd1 61 62 61 73 65 3a 20 25 73 22 2c 20 7a 46 69 6c  abase: %s", zFil
22bd2 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f  e);.    }.    go
22bd3 74 6f 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b  to attach_error;
22bd4 0a 20 20 7d 0a 20 20 0a 20 20 72 65 74 75 72 6e  .  }.  .  return
22bd5 3b 0a 0a 61 74 74 61 63 68 5f 65 72 72 6f 72 3a  ;..attach_error:
22bd6 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 6e 20  .  /* Return an 
22bd7 65 72 72 6f 72 20 69 66 20 77 65 20 67 65 74 20  error if we get 
22bd8 68 65 72 65 20 2a 2f 0a 20 20 69 66 28 20 7a 45  here */.  if( zE
22bd9 72 72 44 79 6e 20 29 7b 0a 20 20 20 20 73 71 6c  rrDyn ){.    sql
22bda 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
22bdb 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 44  r(context, zErrD
22bdc 79 6e 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c  yn, -1);.    sql
22bdd 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
22bde 45 72 72 44 79 6e 29 3b 0a 20 20 7d 0a 20 20 69  ErrDyn);.  }.  i
22bdf 66 28 20 72 63 20 29 20 73 71 6c 69 74 65 33 5f  f( rc ) sqlite3_
22be0 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64  result_error_cod
22be1 65 28 63 6f 6e 74 65 78 74 2c 20 72 63 29 3b 0a  e(context, rc);.
22be2 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20  }../*.** An SQL 
22be3 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 72 65  user-function re
22be4 67 69 73 74 65 72 65 64 20 74 6f 20 64 6f 20 74  gistered to do t
22be5 68 65 20 77 6f 72 6b 20 6f 66 20 61 6e 20 44 45  he work of an DE
22be6 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 20  TACH statement. 
22be7 54 68 65 0a 2a 2a 20 74 68 72 65 65 20 61 72 67  The.** three arg
22be8 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75  uments to the fu
22be9 6e 63 74 69 6f 6e 20 63 6f 6d 65 20 64 69 72 65  nction come dire
22bea 63 74 6c 79 20 66 72 6f 6d 20 61 20 64 65 74 61  ctly from a deta
22beb 63 68 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a  ch statement:.**
22bec 0a 2a 2a 20 20 20 20 20 44 45 54 41 43 48 20 44  .**     DETACH D
22bed 41 54 41 42 41 53 45 20 78 0a 2a 2a 0a 2a 2a 20  ATABASE x.**.** 
22bee 20 20 20 20 53 45 4c 45 43 54 20 73 71 6c 69 74      SELECT sqlit
22bef 65 5f 64 65 74 61 63 68 28 78 29 0a 2a 2f 0a 73  e_detach(x).*/.s
22bf0 74 61 74 69 63 20 76 6f 69 64 20 64 65 74 61 63  tatic void detac
22bf1 68 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  hFunc(.  sqlite3
22bf2 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
22bf3 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64  t,.  int NotUsed
22bf4 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
22bf5 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f  e **argv.){.  co
22bf6 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20  nst char *zName 
22bf7 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
22bf8 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
22bf9 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 73  xt(argv[0]);.  s
22bfa 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c  qlite3 *db = sql
22bfb 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
22bfc 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b  handle(context);
22bfd 0a 20 20 69 6e 74 20 69 3b 0a 20 20 44 62 20 2a  .  int i;.  Db *
22bfe 70 44 62 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  pDb = 0;.  char 
22bff 7a 45 72 72 5b 31 32 38 5d 3b 0a 0a 20 20 55 4e  zErr[128];..  UN
22c00 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
22c01 6f 74 55 73 65 64 29 3b 0a 0a 20 20 69 66 28 20  otUsed);..  if( 
22c02 7a 4e 61 6d 65 3d 3d 30 20 29 20 7a 4e 61 6d 65  zName==0 ) zName
22c03 20 3d 20 22 22 3b 0a 20 20 66 6f 72 28 69 3d 30   = "";.  for(i=0
22c04 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
22c05 29 7b 0a 20 20 20 20 70 44 62 20 3d 20 26 64 62  ){.    pDb = &db
22c06 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 66  ->aDb[i];.    if
22c07 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 20  ( pDb->pBt==0 ) 
22c08 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66  continue;.    if
22c09 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
22c0a 28 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61  (pDb->zName, zNa
22c0b 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a  me)==0 ) break;.
22c0c 20 20 7d 0a 0a 20 20 69 66 28 20 69 3e 3d 64 62    }..  if( i>=db
22c0d 2d 3e 6e 44 62 20 29 7b 0a 20 20 20 20 73 71 6c  ->nDb ){.    sql
22c0e 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
22c0f 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c  zeof(zErr),zErr,
22c10 20 22 6e 6f 20 73 75 63 68 20 64 61 74 61 62 61   "no such databa
22c11 73 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b  se: %s", zName);
22c12 0a 20 20 20 20 67 6f 74 6f 20 64 65 74 61 63 68  .    goto detach
22c13 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66  _error;.  }.  if
22c14 28 20 69 3c 32 20 29 7b 0a 20 20 20 20 73 71 6c  ( i<2 ){.    sql
22c15 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
22c16 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c  zeof(zErr),zErr,
22c17 20 22 63 61 6e 6e 6f 74 20 64 65 74 61 63 68 20   "cannot detach 
22c18 64 61 74 61 62 61 73 65 20 25 73 22 2c 20 7a 4e  database %s", zN
22c19 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64  ame);.    goto d
22c1a 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d  etach_error;.  }
22c1b 0a 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f  .  if( !db->auto
22c1c 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73 71  Commit ){.    sq
22c1d 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
22c1e 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72  izeof(zErr), zEr
22c1f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
22c20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 20          "cannot 
22c21 44 45 54 41 43 48 20 64 61 74 61 62 61 73 65 20  DETACH database 
22c22 77 69 74 68 69 6e 20 74 72 61 6e 73 61 63 74 69  within transacti
22c23 6f 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64  on");.    goto d
22c24 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d  etach_error;.  }
22c25 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74  .  if( sqlite3Bt
22c26 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73  reeIsInReadTrans
22c27 28 70 44 62 2d 3e 70 42 74 29 20 7c 7c 20 73 71  (pDb->pBt) || sq
22c28 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 42 61  lite3BtreeIsInBa
22c29 63 6b 75 70 28 70 44 62 2d 3e 70 42 74 29 20 29  ckup(pDb->pBt) )
22c2a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  {.    sqlite3_sn
22c2b 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45  printf(sizeof(zE
22c2c 72 72 29 2c 7a 45 72 72 2c 20 22 64 61 74 61 62  rr),zErr, "datab
22c2d 61 73 65 20 25 73 20 69 73 20 6c 6f 63 6b 65 64  ase %s is locked
22c2e 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67  ", zName);.    g
22c2f 6f 74 6f 20 64 65 74 61 63 68 5f 65 72 72 6f 72  oto detach_error
22c30 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33  ;.  }..  sqlite3
22c31 42 74 72 65 65 43 6c 6f 73 65 28 70 44 62 2d 3e  BtreeClose(pDb->
22c32 70 42 74 29 3b 0a 20 20 70 44 62 2d 3e 70 42 74  pBt);.  pDb->pBt
22c33 20 3d 20 30 3b 0a 20 20 70 44 62 2d 3e 70 53 63   = 0;.  pDb->pSc
22c34 68 65 6d 61 20 3d 20 30 3b 0a 20 20 73 71 6c 69  hema = 0;.  sqli
22c35 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c  te3ResetInternal
22c36 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20  Schema(db, 0);. 
22c37 20 72 65 74 75 72 6e 3b 0a 0a 64 65 74 61 63 68   return;..detach
22c38 5f 65 72 72 6f 72 3a 0a 20 20 73 71 6c 69 74 65  _error:.  sqlite
22c39 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63  3_result_error(c
22c3a 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31  ontext, zErr, -1
22c3b 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
22c3c 20 70 72 6f 63 65 64 75 72 65 20 67 65 6e 65 72   procedure gener
22c3d 61 74 65 73 20 56 44 42 45 20 63 6f 64 65 20 66  ates VDBE code f
22c3e 6f 72 20 61 20 73 69 6e 67 6c 65 20 69 6e 76 6f  or a single invo
22c3f 63 61 74 69 6f 6e 20 6f 66 20 65 69 74 68 65 72  cation of either
22c40 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 5f 64   the.** sqlite_d
22c41 65 74 61 63 68 28 29 20 6f 72 20 73 71 6c 69 74  etach() or sqlit
22c42 65 5f 61 74 74 61 63 68 28 29 20 53 51 4c 20 75  e_attach() SQL u
22c43 73 65 72 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a  ser functions..*
22c44 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f  /.static void co
22c45 64 65 41 74 74 61 63 68 28 0a 20 20 50 61 72 73  deAttach(.  Pars
22c46 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
22c47 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
22c48 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  ontext */.  int 
22c49 74 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 20  type,           
22c4a 20 2f 2a 20 45 69 74 68 65 72 20 53 51 4c 49 54   /* Either SQLIT
22c4b 45 5f 41 54 54 41 43 48 20 6f 72 20 53 51 4c 49  E_ATTACH or SQLI
22c4c 54 45 5f 44 45 54 41 43 48 20 2a 2f 0a 20 20 46  TE_DETACH */.  F
22c4d 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 2c 20 20  uncDef *pFunc,  
22c4e 20 20 20 20 2f 2a 20 46 75 6e 63 44 65 66 20 77      /* FuncDef w
22c4f 72 61 70 70 65 72 20 66 6f 72 20 64 65 74 61 63  rapper for detac
22c50 68 46 75 6e 63 28 29 20 6f 72 20 61 74 74 61 63  hFunc() or attac
22c51 68 46 75 6e 63 28 29 20 2a 2f 0a 20 20 45 78 70  hFunc() */.  Exp
22c52 72 20 2a 70 41 75 74 68 41 72 67 2c 20 20 20 20  r *pAuthArg,    
22c53 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20    /* Expression 
22c54 74 6f 20 70 61 73 73 20 74 6f 20 61 75 74 68 6f  to pass to autho
22c55 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63  rization callbac
22c56 6b 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 46 69  k */.  Expr *pFi
22c57 6c 65 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e  lename,     /* N
22c58 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20  ame of database 
22c59 66 69 6c 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a  file */.  Expr *
22c5a 70 44 62 6e 61 6d 65 2c 20 20 20 20 20 20 20 2f  pDbname,       /
22c5b 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  * Name of the da
22c5c 74 61 62 61 73 65 20 74 6f 20 75 73 65 20 69 6e  tabase to use in
22c5d 74 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 45 78  ternally */.  Ex
22c5e 70 72 20 2a 70 4b 65 79 20 20 20 20 20 20 20 20  pr *pKey        
22c5f 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6b     /* Database k
22c60 65 79 20 66 6f 72 20 65 6e 63 72 79 70 74 69 6f  ey for encryptio
22c61 6e 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 29  n extension */.)
22c62 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4e 61  {.  int rc;.  Na
22c63 6d 65 43 6f 6e 74 65 78 74 20 73 4e 61 6d 65 3b  meContext sName;
22c64 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71  .  Vdbe *v;.  sq
22c65 6c 69 74 65 33 2a 20 64 62 20 3d 20 70 50 61 72  lite3* db = pPar
22c66 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 65  se->db;.  int re
22c67 67 41 72 67 73 3b 0a 0a 20 20 6d 65 6d 73 65 74  gArgs;..  memset
22c68 28 26 73 4e 61 6d 65 2c 20 30 2c 20 73 69 7a 65  (&sName, 0, size
22c69 6f 66 28 4e 61 6d 65 43 6f 6e 74 65 78 74 29 29  of(NameContext))
22c6a 3b 0a 20 20 73 4e 61 6d 65 2e 70 50 61 72 73 65  ;.  sName.pParse
22c6b 20 3d 20 70 50 61 72 73 65 3b 0a 0a 20 20 69 66   = pParse;..  if
22c6c 28 20 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f  ( .      SQLITE_
22c6d 4f 4b 21 3d 28 72 63 20 3d 20 72 65 73 6f 6c 76  OK!=(rc = resolv
22c6e 65 41 74 74 61 63 68 45 78 70 72 28 26 73 4e 61  eAttachExpr(&sNa
22c6f 6d 65 2c 20 70 46 69 6c 65 6e 61 6d 65 29 29 20  me, pFilename)) 
22c70 7c 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f  ||.      SQLITE_
22c71 4f 4b 21 3d 28 72 63 20 3d 20 72 65 73 6f 6c 76  OK!=(rc = resolv
22c72 65 41 74 74 61 63 68 45 78 70 72 28 26 73 4e 61  eAttachExpr(&sNa
22c73 6d 65 2c 20 70 44 62 6e 61 6d 65 29 29 20 7c 7c  me, pDbname)) ||
22c74 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b  .      SQLITE_OK
22c75 21 3d 28 72 63 20 3d 20 72 65 73 6f 6c 76 65 41  !=(rc = resolveA
22c76 74 74 61 63 68 45 78 70 72 28 26 73 4e 61 6d 65  ttachExpr(&sName
22c77 2c 20 70 4b 65 79 29 29 0a 20 20 29 7b 0a 20 20  , pKey)).  ){.  
22c78 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b    pParse->nErr++
22c79 3b 0a 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63  ;.    goto attac
22c7a 68 5f 65 6e 64 3b 0a 20 20 7d 0a 0a 23 69 66 6e  h_end;.  }..#ifn
22c7b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
22c7c 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20  AUTHORIZATION.  
22c7d 69 66 28 20 70 41 75 74 68 41 72 67 20 29 7b 0a  if( pAuthArg ){.
22c7e 20 20 20 20 63 68 61 72 20 2a 7a 41 75 74 68 41      char *zAuthA
22c7f 72 67 20 3d 20 70 41 75 74 68 41 72 67 2d 3e 75  rg = pAuthArg->u
22c80 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 69 66 28  .zToken;.    if(
22c81 20 4e 45 56 45 52 28 7a 41 75 74 68 41 72 67 3d   NEVER(zAuthArg=
22c82 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  =0) ){.      got
22c83 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20  o attach_end;.  
22c84 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c    }.    rc = sql
22c85 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
22c86 61 72 73 65 2c 20 74 79 70 65 2c 20 7a 41 75 74  arse, type, zAut
22c87 68 41 72 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20  hArg, 0, 0);.   
22c88 20 69 66 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f   if(rc!=SQLITE_O
22c89 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  K ){.      goto 
22c8a 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20 20 20  attach_end;.    
22c8b 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  }.  }.#endif /* 
22c8c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
22c8d 4f 52 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 0a 20  ORIZATION */... 
22c8e 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
22c8f 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 72  dbe(pParse);.  r
22c90 65 67 41 72 67 73 20 3d 20 73 71 6c 69 74 65 33  egArgs = sqlite3
22c91 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61  GetTempRange(pPa
22c92 72 73 65 2c 20 34 29 3b 0a 20 20 73 71 6c 69 74  rse, 4);.  sqlit
22c93 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
22c94 65 2c 20 70 46 69 6c 65 6e 61 6d 65 2c 20 72 65  e, pFilename, re
22c95 67 41 72 67 73 29 3b 0a 20 20 73 71 6c 69 74 65  gArgs);.  sqlite
22c96 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
22c97 2c 20 70 44 62 6e 61 6d 65 2c 20 72 65 67 41 72  , pDbname, regAr
22c98 67 73 2b 31 29 3b 0a 20 20 73 71 6c 69 74 65 33  gs+1);.  sqlite3
22c99 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
22c9a 20 70 4b 65 79 2c 20 72 65 67 41 72 67 73 2b 32   pKey, regArgs+2
22c9b 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 76 20  );..  assert( v 
22c9c 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
22c9d 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 76 20 29  led );.  if( v )
22c9e 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
22c9f 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 75  eAddOp3(v, OP_Fu
22ca0 6e 63 74 69 6f 6e 2c 20 30 2c 20 72 65 67 41 72  nction, 0, regAr
22ca1 67 73 2b 33 2d 70 46 75 6e 63 2d 3e 6e 41 72 67  gs+3-pFunc->nArg
22ca2 2c 20 72 65 67 41 72 67 73 2b 33 29 3b 0a 20 20  , regArgs+3);.  
22ca3 20 20 61 73 73 65 72 74 28 20 70 46 75 6e 63 2d    assert( pFunc-
22ca4 3e 6e 41 72 67 3d 3d 2d 31 20 7c 7c 20 28 70 46  >nArg==-1 || (pF
22ca5 75 6e 63 2d 3e 6e 41 72 67 26 30 78 66 66 29 3d  unc->nArg&0xff)=
22ca6 3d 70 46 75 6e 63 2d 3e 6e 41 72 67 20 29 3b 0a  =pFunc->nArg );.
22ca7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
22ca8 68 61 6e 67 65 50 35 28 76 2c 20 28 75 38 29 28  hangeP5(v, (u8)(
22ca9 70 46 75 6e 63 2d 3e 6e 41 72 67 29 29 3b 0a 20  pFunc->nArg));. 
22caa 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
22cab 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63  angeP4(v, -1, (c
22cac 68 61 72 20 2a 29 70 46 75 6e 63 2c 20 50 34 5f  har *)pFunc, P4_
22cad 46 55 4e 43 44 45 46 29 3b 0a 0a 20 20 20 20 2f  FUNCDEF);..    /
22cae 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f 45 78 70  * Code an OP_Exp
22caf 69 72 65 2e 20 46 6f 72 20 61 6e 20 41 54 54 41  ire. For an ATTA
22cb0 43 48 20 73 74 61 74 65 6d 65 6e 74 2c 20 73 65  CH statement, se
22cb1 74 20 50 31 20 74 6f 20 74 72 75 65 20 28 65 78  t P1 to true (ex
22cb2 70 69 72 65 20 74 68 69 73 0a 20 20 20 20 2a 2a  pire this.    **
22cb3 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e 6c 79 29   statement only)
22cb4 2e 20 46 6f 72 20 44 45 54 41 43 48 2c 20 73 65  . For DETACH, se
22cb5 74 20 69 74 20 74 6f 20 66 61 6c 73 65 20 28 65  t it to false (e
22cb6 78 70 69 72 65 20 61 6c 6c 20 65 78 69 73 74 69  xpire all existi
22cb7 6e 67 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d  ng.    ** statem
22cb8 65 6e 74 73 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  ents)..    */.  
22cb9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22cba 4f 70 31 28 76 2c 20 4f 50 5f 45 78 70 69 72 65  Op1(v, OP_Expire
22cbb 2c 20 28 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f  , (type==SQLITE_
22cbc 41 54 54 41 43 48 29 29 3b 0a 20 20 7d 0a 20 20  ATTACH));.  }.  
22cbd 0a 61 74 74 61 63 68 5f 65 6e 64 3a 0a 20 20 73  .attach_end:.  s
22cbe 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
22cbf 28 64 62 2c 20 70 46 69 6c 65 6e 61 6d 65 29 3b  (db, pFilename);
22cc0 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
22cc1 6c 65 74 65 28 64 62 2c 20 70 44 62 6e 61 6d 65  lete(db, pDbname
22cc2 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
22cc3 44 65 6c 65 74 65 28 64 62 2c 20 70 4b 65 79 29  Delete(db, pKey)
22cc4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65  ;.}../*.** Calle
22cc5 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20  d by the parser 
22cc6 74 6f 20 63 6f 6d 70 69 6c 65 20 61 20 44 45 54  to compile a DET
22cc7 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ACH statement..*
22cc8 2a 0a 2a 2a 20 20 20 20 20 44 45 54 41 43 48 20  *.**     DETACH 
22cc9 70 44 62 6e 61 6d 65 0a 2a 2f 0a 53 51 4c 49 54  pDbname.*/.SQLIT
22cca 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
22ccb 71 6c 69 74 65 33 44 65 74 61 63 68 28 50 61 72  qlite3Detach(Par
22ccc 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
22ccd 20 2a 70 44 62 6e 61 6d 65 29 7b 0a 20 20 73 74   *pDbname){.  st
22cce 61 74 69 63 20 46 75 6e 63 44 65 66 20 64 65 74  atic FuncDef det
22ccf 61 63 68 5f 66 75 6e 63 20 3d 20 7b 0a 20 20 20  ach_func = {.   
22cd0 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   1,             
22cd1 20 20 20 2f 2a 20 6e 41 72 67 20 2a 2f 0a 20 20     /* nArg */.  
22cd2 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20    SQLITE_UTF8,  
22cd3 20 20 20 20 2f 2a 20 69 50 72 65 66 45 6e 63 20      /* iPrefEnc 
22cd4 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20  */.    0,       
22cd5 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67           /* flag
22cd6 73 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20  s */.    0,     
22cd7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 55             /* pU
22cd8 73 65 72 44 61 74 61 20 2a 2f 0a 20 20 20 20 30  serData */.    0
22cd9 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
22cda 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20   /* pNext */.   
22cdb 20 64 65 74 61 63 68 46 75 6e 63 2c 20 20 20 20   detachFunc,    
22cdc 20 20 20 2f 2a 20 78 46 75 6e 63 20 2a 2f 0a 20     /* xFunc */. 
22cdd 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
22cde 20 20 20 20 20 2f 2a 20 78 53 74 65 70 20 2a 2f       /* xStep */
22cdf 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  .    0,         
22ce0 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6e 61 6c         /* xFinal
22ce1 69 7a 65 20 2a 2f 0a 20 20 20 20 22 73 71 6c 69  ize */.    "sqli
22ce2 74 65 5f 64 65 74 61 63 68 22 2c 20 20 2f 2a 20  te_detach",  /* 
22ce3 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 20 20  zName */.    0  
22ce4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
22ce5 2a 20 70 48 61 73 68 20 2a 2f 0a 20 20 7d 3b 0a  * pHash */.  };.
22ce6 20 20 63 6f 64 65 41 74 74 61 63 68 28 70 50 61    codeAttach(pPa
22ce7 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 54 41  rse, SQLITE_DETA
22ce8 43 48 2c 20 26 64 65 74 61 63 68 5f 66 75 6e 63  CH, &detach_func
22ce9 2c 20 70 44 62 6e 61 6d 65 2c 20 30 2c 20 30 2c  , pDbname, 0, 0,
22cea 20 70 44 62 6e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a   pDbname);.}../*
22ceb 0a 2a 2a 20 43 61 6c 6c 65 64 20 62 79 20 74 68  .** Called by th
22cec 65 20 70 61 72 73 65 72 20 74 6f 20 63 6f 6d 70  e parser to comp
22ced 69 6c 65 20 61 6e 20 41 54 54 41 43 48 20 73 74  ile an ATTACH st
22cee 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20  atement..**.**  
22cef 20 20 20 41 54 54 41 43 48 20 70 20 41 53 20 70     ATTACH p AS p
22cf0 44 62 6e 61 6d 65 20 4b 45 59 20 70 4b 65 79 0a  Dbname KEY pKey.
22cf1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
22cf2 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 74  E void sqlite3At
22cf3 74 61 63 68 28 50 61 72 73 65 20 2a 70 50 61 72  tach(Parse *pPar
22cf4 73 65 2c 20 45 78 70 72 20 2a 70 2c 20 45 78 70  se, Expr *p, Exp
22cf5 72 20 2a 70 44 62 6e 61 6d 65 2c 20 45 78 70 72  r *pDbname, Expr
22cf6 20 2a 70 4b 65 79 29 7b 0a 20 20 73 74 61 74 69   *pKey){.  stati
22cf7 63 20 46 75 6e 63 44 65 66 20 61 74 74 61 63 68  c FuncDef attach
22cf8 5f 66 75 6e 63 20 3d 20 7b 0a 20 20 20 20 33 2c  _func = {.    3,
22cf9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22cfa 2f 2a 20 6e 41 72 67 20 2a 2f 0a 20 20 20 20 53  /* nArg */.    S
22cfb 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 20  QLITE_UTF8,     
22cfc 20 2f 2a 20 69 50 72 65 66 45 6e 63 20 2a 2f 0a   /* iPrefEnc */.
22cfd 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20      0,          
22cfe 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 2a        /* flags *
22cff 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  /.    0,        
22d00 20 20 20 20 20 20 20 20 2f 2a 20 70 55 73 65 72          /* pUser
22d01 44 61 74 61 20 2a 2f 0a 20 20 20 20 30 2c 20 20  Data */.    0,  
22d02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
22d03 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 61 74   pNext */.    at
22d04 74 61 63 68 46 75 6e 63 2c 20 20 20 20 20 20 20  tachFunc,       
22d05 2f 2a 20 78 46 75 6e 63 20 2a 2f 0a 20 20 20 20  /* xFunc */.    
22d06 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
22d07 20 20 2f 2a 20 78 53 74 65 70 20 2a 2f 0a 20 20    /* xStep */.  
22d08 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
22d09 20 20 20 20 2f 2a 20 78 46 69 6e 61 6c 69 7a 65      /* xFinalize
22d0a 20 2a 2f 0a 20 20 20 20 22 73 71 6c 69 74 65 5f   */.    "sqlite_
22d0b 61 74 74 61 63 68 22 2c 20 20 2f 2a 20 7a 4e 61  attach",  /* zNa
22d0c 6d 65 20 2a 2f 0a 20 20 20 20 30 20 20 20 20 20  me */.    0     
22d0d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70              /* p
22d0e 48 61 73 68 20 2a 2f 0a 20 20 7d 3b 0a 20 20 63  Hash */.  };.  c
22d0f 6f 64 65 41 74 74 61 63 68 28 70 50 61 72 73 65  odeAttach(pParse
22d10 2c 20 53 51 4c 49 54 45 5f 41 54 54 41 43 48 2c  , SQLITE_ATTACH,
22d11 20 26 61 74 74 61 63 68 5f 66 75 6e 63 2c 20 70   &attach_func, p
22d12 2c 20 70 2c 20 70 44 62 6e 61 6d 65 2c 20 70 4b  , p, pDbname, pK
22d13 65 79 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  ey);.}.#endif /*
22d14 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54   SQLITE_OMIT_ATT
22d15 41 43 48 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e  ACH */../*.** In
22d16 69 74 69 61 6c 69 7a 65 20 61 20 44 62 46 69 78  itialize a DbFix
22d17 65 72 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  er structure.  T
22d18 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74  his routine must
22d19 20 62 65 20 63 61 6c 6c 65 64 20 70 72 69 6f 72   be called prior
22d1a 0a 2a 2a 20 74 6f 20 70 61 73 73 69 6e 67 20 74  .** to passing t
22d1b 68 65 20 73 74 72 75 63 74 75 72 65 20 74 6f 20  he structure to 
22d1c 6f 6e 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74  one of the sqlit
22d1d 65 46 69 78 41 41 41 41 28 29 20 72 6f 75 74 69  eFixAAAA() routi
22d1e 6e 65 73 20 62 65 6c 6f 77 2e 0a 2a 2a 0a 2a 2a  nes below..**.**
22d1f 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
22d20 65 20 69 6e 64 69 63 61 74 65 73 20 77 68 65 74  e indicates whet
22d21 68 65 72 20 6f 72 20 6e 6f 74 20 66 69 78 61 74  her or not fixat
22d22 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64 2e  ion is required.
22d23 20 20 54 52 55 45 0a 2a 2a 20 6d 65 61 6e 73 20    TRUE.** means 
22d24 77 65 20 64 6f 20 6e 65 65 64 20 74 6f 20 66 69  we do need to fi
22d25 78 20 74 68 65 20 64 61 74 61 62 61 73 65 20 72  x the database r
22d26 65 66 65 72 65 6e 63 65 73 2c 20 46 41 4c 53 45  eferences, FALSE
22d27 20 6d 65 61 6e 73 20 77 65 20 64 6f 20 6e 6f 74   means we do not
22d28 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
22d29 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
22d2a 69 78 49 6e 69 74 28 0a 20 20 44 62 46 69 78 65  ixInit(.  DbFixe
22d2b 72 20 2a 70 46 69 78 2c 20 20 20 20 20 20 2f 2a  r *pFix,      /*
22d2c 20 54 68 65 20 66 69 78 65 72 20 74 6f 20 62 65   The fixer to be
22d2d 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a   initialized */.
22d2e 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
22d2f 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d        /* Error m
22d30 65 73 73 61 67 65 73 20 77 69 6c 6c 20 62 65 20  essages will be 
22d31 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a  written here */.
22d32 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20    int iDb,      
22d33 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73        /* This is
22d34 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68   the database th
22d35 61 74 20 6d 75 73 74 20 62 65 20 75 73 65 64 20  at must be used 
22d36 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
22d37 2a 7a 54 79 70 65 2c 20 20 2f 2a 20 22 76 69 65  *zType,  /* "vie
22d38 77 22 2c 20 22 74 72 69 67 67 65 72 22 2c 20 6f  w", "trigger", o
22d39 72 20 22 69 6e 64 65 78 22 20 2a 2f 0a 20 20 63  r "index" */.  c
22d3a 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  onst Token *pNam
22d3b 65 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68  e  /* Name of th
22d3c 65 20 76 69 65 77 2c 20 74 72 69 67 67 65 72 2c  e view, trigger,
22d3d 20 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 29 7b 0a   or index */.){.
22d3e 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a    sqlite3 *db;..
22d3f 20 20 69 66 28 20 4e 45 56 45 52 28 69 44 62 3c    if( NEVER(iDb<
22d40 30 29 20 7c 7c 20 69 44 62 3d 3d 31 20 29 20 72  0) || iDb==1 ) r
22d41 65 74 75 72 6e 20 30 3b 0a 20 20 64 62 20 3d 20  eturn 0;.  db = 
22d42 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73  pParse->db;.  as
22d43 73 65 72 74 28 20 64 62 2d 3e 6e 44 62 3e 69 44  sert( db->nDb>iD
22d44 62 20 29 3b 0a 20 20 70 46 69 78 2d 3e 70 50 61  b );.  pFix->pPa
22d45 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20  rse = pParse;.  
22d46 70 46 69 78 2d 3e 7a 44 62 20 3d 20 64 62 2d 3e  pFix->zDb = db->
22d47 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a  aDb[iDb].zName;.
22d48 20 20 70 46 69 78 2d 3e 7a 54 79 70 65 20 3d 20    pFix->zType = 
22d49 7a 54 79 70 65 3b 0a 20 20 70 46 69 78 2d 3e 70  zType;.  pFix->p
22d4a 4e 61 6d 65 20 3d 20 70 4e 61 6d 65 3b 0a 20 20  Name = pName;.  
22d4b 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a  return 1;.}../*.
22d4c 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
22d4d 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73   set of routines
22d4e 20 77 61 6c 6b 20 74 68 72 6f 75 67 68 20 74 68   walk through th
22d4f 65 20 70 61 72 73 65 20 74 72 65 65 20 61 6e 64  e parse tree and
22d50 20 61 73 73 69 67 6e 0a 2a 2a 20 61 20 73 70 65   assign.** a spe
22d51 63 69 66 69 63 20 64 61 74 61 62 61 73 65 20 74  cific database t
22d52 6f 20 61 6c 6c 20 74 61 62 6c 65 20 72 65 66 65  o all table refe
22d53 72 65 6e 63 65 73 20 77 68 65 72 65 20 74 68 65  rences where the
22d54 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a   database name.*
22d55 2a 20 77 61 73 20 6c 65 66 74 20 75 6e 73 70 65  * was left unspe
22d56 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 6f 72  cified in the or
22d57 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74 65  iginal SQL state
22d58 6d 65 6e 74 2e 20 20 54 68 65 20 70 46 69 78 20  ment.  The pFix 
22d59 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 6d 75 73  structure.** mus
22d5a 74 20 68 61 76 65 20 62 65 65 6e 20 69 6e 69 74  t have been init
22d5b 69 61 6c 69 7a 65 64 20 62 79 20 61 20 70 72 69  ialized by a pri
22d5c 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  or call to sqlit
22d5d 65 33 46 69 78 49 6e 69 74 28 29 2e 0a 2a 2a 0a  e3FixInit()..**.
22d5e 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
22d5f 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 6d 61  s are used to ma
22d60 6b 65 20 73 75 72 65 20 74 68 61 74 20 61 6e 20  ke sure that an 
22d61 69 6e 64 65 78 2c 20 74 72 69 67 67 65 72 2c 20  index, trigger, 
22d62 6f 72 0a 2a 2a 20 76 69 65 77 20 69 6e 20 6f 6e  or.** view in on
22d63 65 20 64 61 74 61 62 61 73 65 20 64 6f 65 73 20  e database does 
22d64 6e 6f 74 20 72 65 66 65 72 20 74 6f 20 6f 62 6a  not refer to obj
22d65 65 63 74 73 20 69 6e 20 61 20 64 69 66 66 65 72  ects in a differ
22d66 65 6e 74 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a  ent database..**
22d67 20 28 45 78 63 65 70 74 69 6f 6e 3a 20 69 6e 64   (Exception: ind
22d68 69 63 65 73 2c 20 74 72 69 67 67 65 72 73 2c 20  ices, triggers, 
22d69 61 6e 64 20 76 69 65 77 73 20 69 6e 20 74 68 65  and views in the
22d6a 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 20 61   TEMP database a
22d6b 72 65 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 74 6f  re.** allowed to
22d6c 20 72 65 66 65 72 20 74 6f 20 61 6e 79 74 68 69   refer to anythi
22d6d 6e 67 2e 29 20 20 49 66 20 61 20 72 65 66 65 72  ng.)  If a refer
22d6e 65 6e 63 65 20 69 73 20 65 78 70 6c 69 63 69 74  ence is explicit
22d6f 6c 79 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 61 6e  ly made.** to an
22d70 20 6f 62 6a 65 63 74 20 69 6e 20 61 20 64 69 66   object in a dif
22d71 66 65 72 65 6e 74 20 64 61 74 61 62 61 73 65 2c  ferent database,
22d72 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
22d73 65 20 69 73 20 61 64 64 65 64 20 74 6f 0a 2a 2a  e is added to.**
22d74 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
22d75 20 61 6e 64 20 74 68 65 73 65 20 72 6f 75 74 69   and these routi
22d76 6e 65 73 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a  nes return non-z
22d77 65 72 6f 2e 20 20 49 66 20 65 76 65 72 79 74 68  ero.  If everyth
22d78 69 6e 67 0a 2a 2a 20 63 68 65 63 6b 73 20 6f 75  ing.** checks ou
22d79 74 2c 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  t, these routine
22d7a 73 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53  s return 0..*/.S
22d7b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
22d7c 74 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c  t sqlite3FixSrcL
22d7d 69 73 74 28 0a 20 20 44 62 46 69 78 65 72 20 2a  ist(.  DbFixer *
22d7e 70 46 69 78 2c 20 20 20 20 20 20 20 2f 2a 20 43  pFix,       /* C
22d7f 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66 69  ontext of the fi
22d80 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 53 72 63 4c  xation */.  SrcL
22d81 69 73 74 20 2a 70 4c 69 73 74 20 20 20 20 20 20  ist *pList      
22d82 20 2f 2a 20 54 68 65 20 53 6f 75 72 63 65 20 6c   /* The Source l
22d83 69 73 74 20 74 6f 20 63 68 65 63 6b 20 61 6e 64  ist to check and
22d84 20 6d 6f 64 69 66 79 20 2a 2f 0a 29 7b 0a 20 20   modify */.){.  
22d85 69 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74 20 63  int i;.  const c
22d86 68 61 72 20 2a 7a 44 62 3b 0a 20 20 73 74 72 75  har *zDb;.  stru
22d87 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
22d88 2a 70 49 74 65 6d 3b 0a 0a 20 20 69 66 28 20 4e  *pItem;..  if( N
22d89 45 56 45 52 28 70 4c 69 73 74 3d 3d 30 29 20 29  EVER(pList==0) )
22d8a 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7a 44 62   return 0;.  zDb
22d8b 20 3d 20 70 46 69 78 2d 3e 7a 44 62 3b 0a 20 20   = pFix->zDb;.  
22d8c 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70  for(i=0, pItem=p
22d8d 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74  List->a; i<pList
22d8e 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74  ->nSrc; i++, pIt
22d8f 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70  em++){.    if( p
22d90 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 3d  Item->zDatabase=
22d91 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 49 74 65  =0 ){.      pIte
22d92 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d 20 73  m->zDatabase = s
22d93 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 70  qlite3DbStrDup(p
22d94 46 69 78 2d 3e 70 50 61 72 73 65 2d 3e 64 62 2c  Fix->pParse->db,
22d95 20 7a 44 62 29 3b 0a 20 20 20 20 7d 65 6c 73 65   zDb);.    }else
22d96 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
22d97 43 6d 70 28 70 49 74 65 6d 2d 3e 7a 44 61 74 61  Cmp(pItem->zData
22d98 62 61 73 65 2c 7a 44 62 29 21 3d 30 20 29 7b 0a  base,zDb)!=0 ){.
22d99 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
22d9a 6f 72 4d 73 67 28 70 46 69 78 2d 3e 70 50 61 72  orMsg(pFix->pPar
22d9b 73 65 2c 0a 20 20 20 20 20 20 20 20 20 22 25 73  se,.         "%s
22d9c 20 25 54 20 63 61 6e 6e 6f 74 20 72 65 66 65 72   %T cannot refer
22d9d 65 6e 63 65 20 6f 62 6a 65 63 74 73 20 69 6e 20  ence objects in 
22d9e 64 61 74 61 62 61 73 65 20 25 73 22 2c 0a 20 20  database %s",.  
22d9f 20 20 20 20 20 20 20 70 46 69 78 2d 3e 7a 54 79         pFix->zTy
22da0 70 65 2c 20 70 46 69 78 2d 3e 70 4e 61 6d 65 2c  pe, pFix->pName,
22da1 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73   pItem->zDatabas
22da2 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  e);.      return
22da3 20 31 3b 0a 20 20 20 20 7d 0a 23 69 66 20 21 64   1;.    }.#if !d
22da4 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
22da5 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66  IT_VIEW) || !def
22da6 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
22da7 5f 54 52 49 47 47 45 52 29 0a 20 20 20 20 69 66  _TRIGGER).    if
22da8 28 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65  ( sqlite3FixSele
22da9 63 74 28 70 46 69 78 2c 20 70 49 74 65 6d 2d 3e  ct(pFix, pItem->
22daa 70 53 65 6c 65 63 74 29 20 29 20 72 65 74 75 72  pSelect) ) retur
22dab 6e 20 31 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  n 1;.    if( sql
22dac 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78  ite3FixExpr(pFix
22dad 2c 20 70 49 74 65 6d 2d 3e 70 4f 6e 29 20 29 20  , pItem->pOn) ) 
22dae 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66  return 1;.#endif
22daf 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
22db0 0a 7d 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  .}.#if !defined(
22db1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
22db2 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51  ) || !defined(SQ
22db3 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
22db4 52 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  R).SQLITE_PRIVAT
22db5 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78  E int sqlite3Fix
22db6 53 65 6c 65 63 74 28 0a 20 20 44 62 46 69 78 65  Select(.  DbFixe
22db7 72 20 2a 70 46 69 78 2c 20 20 20 20 20 20 20 2f  r *pFix,       /
22db8 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65  * Context of the
22db9 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 53   fixation */.  S
22dba 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 20  elect *pSelect  
22dbb 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43      /* The SELEC
22dbc 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62  T statement to b
22dbd 65 20 66 69 78 65 64 20 74 6f 20 6f 6e 65 20 64  e fixed to one d
22dbe 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20  atabase */.){.  
22dbf 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74 20 29  while( pSelect )
22dc0 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
22dc1 33 46 69 78 45 78 70 72 4c 69 73 74 28 70 46 69  3FixExprList(pFi
22dc2 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69  x, pSelect->pELi
22dc3 73 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  st) ){.      ret
22dc4 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
22dc5 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 53   if( sqlite3FixS
22dc6 72 63 4c 69 73 74 28 70 46 69 78 2c 20 70 53 65  rcList(pFix, pSe
22dc7 6c 65 63 74 2d 3e 70 53 72 63 29 20 29 7b 0a 20  lect->pSrc) ){. 
22dc8 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
22dc9 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
22dca 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78  ite3FixExpr(pFix
22dcb 2c 20 70 53 65 6c 65 63 74 2d 3e 70 57 68 65 72  , pSelect->pWher
22dcc 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  e) ){.      retu
22dcd 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
22dce 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78  if( sqlite3FixEx
22dcf 70 72 28 70 46 69 78 2c 20 70 53 65 6c 65 63 74  pr(pFix, pSelect
22dd0 2d 3e 70 48 61 76 69 6e 67 29 20 29 7b 0a 20 20  ->pHaving) ){.  
22dd1 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
22dd2 20 20 7d 0a 20 20 20 20 70 53 65 6c 65 63 74 20    }.    pSelect 
22dd3 3d 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f  = pSelect->pPrio
22dd4 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  r;.  }.  return 
22dd5 30 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  0;.}.SQLITE_PRIV
22dd6 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
22dd7 69 78 45 78 70 72 28 0a 20 20 44 62 46 69 78 65  ixExpr(.  DbFixe
22dd8 72 20 2a 70 46 69 78 2c 20 20 20 20 20 2f 2a 20  r *pFix,     /* 
22dd9 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66  Context of the f
22dda 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70  ixation */.  Exp
22ddb 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 20 20  r *pExpr        
22ddc 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f  /* The expressio
22ddd 6e 20 74 6f 20 62 65 20 66 69 78 65 64 20 74 6f  n to be fixed to
22dde 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 2a 2f   one database */
22ddf 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 45 78  .){.  while( pEx
22de0 70 72 20 29 7b 0a 20 20 20 20 69 66 28 20 45 78  pr ){.    if( Ex
22de1 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  prHasAnyProperty
22de2 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e  (pExpr, EP_Token
22de3 4f 6e 6c 79 29 20 29 20 62 72 65 61 6b 3b 0a 20  Only) ) break;. 
22de4 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72     if( ExprHasPr
22de5 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
22de6 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20  _xIsSelect) ){. 
22de7 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
22de8 46 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c 20  FixSelect(pFix, 
22de9 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74  pExpr->x.pSelect
22dea 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  ) ) return 1;.  
22deb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
22dec 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70  f( sqlite3FixExp
22ded 72 4c 69 73 74 28 70 46 69 78 2c 20 70 45 78 70  rList(pFix, pExp
22dee 72 2d 3e 78 2e 70 4c 69 73 74 29 20 29 20 72 65  r->x.pList) ) re
22def 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
22df0 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78    if( sqlite3Fix
22df1 45 78 70 72 28 70 46 69 78 2c 20 70 45 78 70 72  Expr(pFix, pExpr
22df2 2d 3e 70 52 69 67 68 74 29 20 29 7b 0a 20 20 20  ->pRight) ){.   
22df3 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
22df4 20 7d 0a 20 20 20 20 70 45 78 70 72 20 3d 20 70   }.    pExpr = p
22df5 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 7d  Expr->pLeft;.  }
22df6 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53  .  return 0;.}.S
22df7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
22df8 74 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72  t sqlite3FixExpr
22df9 4c 69 73 74 28 0a 20 20 44 62 46 69 78 65 72 20  List(.  DbFixer 
22dfa 2a 70 46 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f  *pFix,     /* Co
22dfb 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66 69 78  ntext of the fix
22dfc 61 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 4c  ation */.  ExprL
22dfd 69 73 74 20 2a 70 4c 69 73 74 20 20 20 20 2f 2a  ist *pList    /*
22dfe 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
22dff 74 6f 20 62 65 20 66 69 78 65 64 20 74 6f 20 6f  to be fixed to o
22e00 6e 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 29  ne database */.)
22e01 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72  {.  int i;.  str
22e02 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
22e03 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20  m *pItem;.  if( 
22e04 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72  pList==0 ) retur
22e05 6e 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20  n 0;.  for(i=0, 
22e06 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20  pItem=pList->a; 
22e07 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  i<pList->nExpr; 
22e08 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i++, pItem++){. 
22e09 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
22e0a 78 45 78 70 72 28 70 46 69 78 2c 20 70 49 74 65  xExpr(pFix, pIte
22e0b 6d 2d 3e 70 45 78 70 72 29 20 29 7b 0a 20 20 20  m->pExpr) ){.   
22e0c 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
22e0d 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
22e0e 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  0;.}.#endif..#if
22e0f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
22e10 5f 54 52 49 47 47 45 52 0a 53 51 4c 49 54 45 5f  _TRIGGER.SQLITE_
22e11 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
22e12 74 65 33 46 69 78 54 72 69 67 67 65 72 53 74 65  te3FixTriggerSte
22e13 70 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46  p(.  DbFixer *pF
22e14 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65  ix,     /* Conte
22e15 78 74 20 6f 66 20 74 68 65 20 66 69 78 61 74 69  xt of the fixati
22e16 6f 6e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53  on */.  TriggerS
22e17 74 65 70 20 2a 70 53 74 65 70 20 2f 2a 20 54 68  tep *pStep /* Th
22e18 65 20 74 72 69 67 67 65 72 20 73 74 65 70 20 62  e trigger step b
22e19 65 20 66 69 78 65 64 20 74 6f 20 6f 6e 65 20 64  e fixed to one d
22e1a 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20  atabase */.){.  
22e1b 77 68 69 6c 65 28 20 70 53 74 65 70 20 29 7b 0a  while( pStep ){.
22e1c 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
22e1d 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c 20 70  ixSelect(pFix, p
22e1e 53 74 65 70 2d 3e 70 53 65 6c 65 63 74 29 20 29  Step->pSelect) )
22e1f 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
22e20 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
22e21 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70  sqlite3FixExpr(p
22e22 46 69 78 2c 20 70 53 74 65 70 2d 3e 70 57 68 65  Fix, pStep->pWhe
22e23 72 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  re) ){.      ret
22e24 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
22e25 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45   if( sqlite3FixE
22e26 78 70 72 4c 69 73 74 28 70 46 69 78 2c 20 70 53  xprList(pFix, pS
22e27 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 29 20  tep->pExprList) 
22e28 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
22e29 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 74  1;.    }.    pSt
22e2a 65 70 20 3d 20 70 53 74 65 70 2d 3e 70 4e 65 78  ep = pStep->pNex
22e2b 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  t;.  }.  return 
22e2c 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a  0;.}.#endif../**
22e2d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
22e2e 20 6f 66 20 61 74 74 61 63 68 2e 63 20 2a 2a 2a   of attach.c ***
22e2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22e31 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
22e32 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
22e33 69 6e 20 66 69 6c 65 20 61 75 74 68 2e 63 20 2a  in file auth.c *
22e34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22e35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22e36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
22e37 2a 2a 20 32 30 30 33 20 4a 61 6e 75 61 72 79 20  ** 2003 January 
22e38 31 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  11.**.** The aut
22e39 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
22e3a 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
22e3b 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
22e3c 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
22e3d 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
22e3e 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
22e3f 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
22e40 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
22e41 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
22e42 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
22e43 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
22e44 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
22e45 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
22e46 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
22e47 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
22e48 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
22e49 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
22e4a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22e4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22e4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22e4d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22e4e 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
22e4f 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65  ontains code use
22e50 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  d to implement t
22e51 68 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  he sqlite3_set_a
22e52 75 74 68 6f 72 69 7a 65 72 28 29 0a 2a 2a 20 41  uthorizer().** A
22e53 50 49 2e 20 20 54 68 69 73 20 66 61 63 69 6c 69  PI.  This facili
22e54 74 79 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e 61  ty is an optiona
22e55 6c 20 66 65 61 74 75 72 65 20 6f 66 20 74 68 65  l feature of the
22e56 20 6c 69 62 72 61 72 79 2e 20 20 45 6d 62 65 64   library.  Embed
22e57 64 65 64 0a 2a 2a 20 73 79 73 74 65 6d 73 20 74  ded.** systems t
22e58 68 61 74 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20  hat do not need 
22e59 74 68 69 73 20 66 61 63 69 6c 69 74 79 20 6d 61  this facility ma
22e5a 79 20 6f 6d 69 74 20 69 74 20 62 79 20 72 65 63  y omit it by rec
22e5b 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 74 68 65 20  ompiling.** the 
22e5c 6c 69 62 72 61 72 79 20 77 69 74 68 20 2d 44 53  library with -DS
22e5d 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
22e5e 52 49 5a 41 54 49 4f 4e 3d 31 0a 2a 2a 0a 2a 2a  RIZATION=1.**.**
22e5f 20 24 49 64 3a 20 61 75 74 68 2e 63 2c 76 20 31   $Id: auth.c,v 1
22e60 2e 33 32 20 32 30 30 39 2f 30 37 2f 30 32 20 31  .32 2009/07/02 1
22e61 38 3a 34 30 3a 33 35 20 64 61 6e 69 65 6c 6b 31  8:40:35 danielk1
22e62 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  977 Exp $.*/../*
22e63 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 63  .** All of the c
22e64 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ode in this file
22e65 20 6d 61 79 20 62 65 20 6f 6d 69 74 74 65 64 20   may be omitted 
22e66 62 79 20 64 65 66 69 6e 69 6e 67 20 61 20 73 69  by defining a si
22e67 6e 67 6c 65 0a 2a 2a 20 6d 61 63 72 6f 2e 0a 2a  ngle.** macro..*
22e68 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
22e69 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
22e6a 49 4f 4e 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f  ION../*.** Set o
22e6b 72 20 63 6c 65 61 72 20 74 68 65 20 61 63 63 65  r clear the acce
22e6c 73 73 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  ss authorization
22e6d 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
22e6e 20 54 68 65 20 61 63 63 65 73 73 20 61 75 74 68   The access auth
22e6f 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69  orization functi
22e70 6f 6e 20 69 73 20 62 65 20 63 61 6c 6c 65 64 20  on is be called 
22e71 64 75 72 69 6e 67 20 74 68 65 20 63 6f 6d 70 69  during the compi
22e72 6c 61 74 69 6f 6e 0a 2a 2a 20 70 68 61 73 65 20  lation.** phase 
22e73 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 20 74  to verify that t
22e74 68 65 20 75 73 65 72 20 68 61 73 20 72 65 61 64  he user has read
22e75 20 61 6e 64 2f 6f 72 20 77 72 69 74 65 20 61 63   and/or write ac
22e76 63 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 20  cess permission 
22e77 6f 6e 0a 2a 2a 20 76 61 72 69 6f 75 73 20 66 69  on.** various fi
22e78 65 6c 64 73 20 6f 66 20 74 68 65 20 64 61 74 61  elds of the data
22e79 62 61 73 65 2e 20 20 54 68 65 20 66 69 72 73 74  base.  The first
22e7a 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
22e7b 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 0a 2a   auth function.*
22e7c 2a 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74  * is a copy of t
22e7d 68 65 20 33 72 64 20 61 72 67 75 6d 65 6e 74 20  he 3rd argument 
22e7e 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  to this routine.
22e7f 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67    The second arg
22e80 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20  ument.** to the 
22e81 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 69 73  auth function is
22e82 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 63 6f   one of these co
22e83 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20  nstants:.**.**  
22e84 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41       SQLITE_CREA
22e85 54 45 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20  TE_INDEX.**     
22e86 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f    SQLITE_CREATE_
22e87 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53  TABLE.**       S
22e88 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d  QLITE_CREATE_TEM
22e89 50 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20  P_INDEX.**      
22e8a 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54   SQLITE_CREATE_T
22e8b 45 4d 50 5f 54 41 42 4c 45 0a 2a 2a 20 20 20 20  EMP_TABLE.**    
22e8c 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45     SQLITE_CREATE
22e8d 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 0a 2a 2a  _TEMP_TRIGGER.**
22e8e 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52         SQLITE_CR
22e8f 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 57 0a 2a  EATE_TEMP_VIEW.*
22e90 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43  *       SQLITE_C
22e91 52 45 41 54 45 5f 54 52 49 47 47 45 52 0a 2a 2a  REATE_TRIGGER.**
22e92 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52         SQLITE_CR
22e93 45 41 54 45 5f 56 49 45 57 0a 2a 2a 20 20 20 20  EATE_VIEW.**    
22e94 20 20 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45     SQLITE_DELETE
22e95 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
22e96 5f 44 52 4f 50 5f 49 4e 44 45 58 0a 2a 2a 20 20  _DROP_INDEX.**  
22e97 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50       SQLITE_DROP
22e98 5f 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20  _TABLE.**       
22e99 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50  SQLITE_DROP_TEMP
22e9a 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 20  _INDEX.**       
22e9b 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50  SQLITE_DROP_TEMP
22e9c 5f 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20  _TABLE.**       
22e9d 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50  SQLITE_DROP_TEMP
22e9e 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20 20 20 20  _TRIGGER.**     
22e9f 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45    SQLITE_DROP_TE
22ea0 4d 50 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20  MP_VIEW.**      
22ea1 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49   SQLITE_DROP_TRI
22ea2 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51  GGER.**       SQ
22ea3 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 0a 2a  LITE_DROP_VIEW.*
22ea4 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49  *       SQLITE_I
22ea5 4e 53 45 52 54 0a 2a 2a 20 20 20 20 20 20 20 53  NSERT.**       S
22ea6 51 4c 49 54 45 5f 50 52 41 47 4d 41 0a 2a 2a 20  QLITE_PRAGMA.** 
22ea7 20 20 20 20 20 20 53 51 4c 49 54 45 5f 52 45 41        SQLITE_REA
22ea8 44 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  D.**       SQLIT
22ea9 45 5f 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20  E_SELECT.**     
22eaa 20 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43    SQLITE_TRANSAC
22eab 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 20 53 51  TION.**       SQ
22eac 4c 49 54 45 5f 55 50 44 41 54 45 0a 2a 2a 0a 2a  LITE_UPDATE.**.*
22ead 2a 20 54 68 65 20 74 68 69 72 64 20 61 6e 64 20  * The third and 
22eae 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 73  fourth arguments
22eaf 20 74 6f 20 74 68 65 20 61 75 74 68 20 66 75 6e   to the auth fun
22eb0 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20 6e 61  ction are the na
22eb1 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62  me of.** the tab
22eb2 6c 65 20 61 6e 64 20 74 68 65 20 63 6f 6c 75 6d  le and the colum
22eb3 6e 20 74 68 61 74 20 61 72 65 20 62 65 69 6e 67  n that are being
22eb4 20 61 63 63 65 73 73 65 64 2e 20 20 54 68 65 20   accessed.  The 
22eb5 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  auth function.**
22eb6 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 65   should return e
22eb7 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b 2c  ither SQLITE_OK,
22eb8 20 53 51 4c 49 54 45 5f 44 45 4e 59 2c 20 6f 72   SQLITE_DENY, or
22eb9 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 2e 20   SQLITE_IGNORE. 
22eba 20 49 66 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b   If.** SQLITE_OK
22ebb 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 69 74   is returned, it
22ebc 20 6d 65 61 6e 73 20 74 68 61 74 20 61 63 63 65   means that acce
22ebd 73 73 20 69 73 20 61 6c 6c 6f 77 65 64 2e 20 20  ss is allowed.  
22ebe 53 51 4c 49 54 45 5f 44 45 4e 59 0a 2a 2a 20 6d  SQLITE_DENY.** m
22ebf 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 53 51  eans that the SQ
22ec0 4c 20 73 74 61 74 65 6d 65 6e 74 20 77 69 6c 6c  L statement will
22ec1 20 6e 65 76 65 72 2d 72 75 6e 20 2d 20 74 68 65   never-run - the
22ec2 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20   sqlite3_exec() 
22ec3 63 61 6c 6c 0a 2a 2a 20 77 69 6c 6c 20 72 65 74  call.** will ret
22ec4 75 72 6e 20 77 69 74 68 20 61 6e 20 65 72 72 6f  urn with an erro
22ec5 72 2e 20 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52  r.  SQLITE_IGNOR
22ec6 45 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  E means that the
22ec7 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a 2a   SQL statement.*
22ec8 2a 20 73 68 6f 75 6c 64 20 72 75 6e 20 62 75 74  * should run but
22ec9 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 61   attempts to rea
22eca 64 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  d the specified 
22ecb 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 72 65 74 75  column will retu
22ecc 72 6e 20 4e 55 4c 4c 0a 2a 2a 20 61 6e 64 20 61  rn NULL.** and a
22ecd 74 74 65 6d 70 74 73 20 74 6f 20 77 72 69 74 65  ttempts to write
22ece 20 74 68 65 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c   the column will
22ecf 20 62 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a   be ignored..**.
22ed0 2a 2a 20 53 65 74 74 69 6e 67 20 74 68 65 20 61  ** Setting the a
22ed1 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  uth function to 
22ed2 4e 55 4c 4c 20 64 69 73 61 62 6c 65 73 20 74 68  NULL disables th
22ed3 69 73 20 68 6f 6f 6b 2e 20 20 54 68 65 20 64 65  is hook.  The de
22ed4 66 61 75 6c 74 0a 2a 2a 20 73 65 74 74 69 6e 67  fault.** setting
22ed5 20 6f 66 20 74 68 65 20 61 75 74 68 20 66 75 6e   of the auth fun
22ed6 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0a 2a  ction is NULL..*
22ed7 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
22ed8 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74   sqlite3_set_aut
22ed9 68 6f 72 69 7a 65 72 28 0a 20 20 73 71 6c 69 74  horizer(.  sqlit
22eda 65 33 20 2a 64 62 2c 0a 20 20 69 6e 74 20 28 2a  e3 *db,.  int (*
22edb 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74  xAuth)(void*,int
22edc 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
22edd 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
22ede 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
22edf 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 0a  ),.  void *pArg.
22ee0 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ){.  sqlite3_mut
22ee1 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
22ee2 65 78 29 3b 0a 20 20 64 62 2d 3e 78 41 75 74 68  ex);.  db->xAuth
22ee3 20 3d 20 78 41 75 74 68 3b 0a 20 20 64 62 2d 3e   = xAuth;.  db->
22ee4 70 41 75 74 68 41 72 67 20 3d 20 70 41 72 67 3b  pAuthArg = pArg;
22ee5 0a 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65  .  sqlite3Expire
22ee6 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
22ee7 74 73 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65  ts(db);.  sqlite
22ee8 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
22ee9 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
22eea 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
22eeb 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20  ./*.** Write an 
22eec 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e  error message in
22eed 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  to pParse->zErrM
22eee 73 67 20 74 68 61 74 20 65 78 70 6c 61 69 6e 73  sg that explains
22eef 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 75 73 65   that the.** use
22ef0 72 2d 73 75 70 70 6c 69 65 64 20 61 75 74 68 6f  r-supplied autho
22ef1 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  rization functio
22ef2 6e 20 72 65 74 75 72 6e 65 64 20 61 6e 20 69 6c  n returned an il
22ef3 6c 65 67 61 6c 20 76 61 6c 75 65 2e 0a 2a 2f 0a  legal value..*/.
22ef4 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
22ef5 74 65 41 75 74 68 42 61 64 52 65 74 75 72 6e 43  teAuthBadReturnC
22ef6 6f 64 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ode(Parse *pPars
22ef7 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 45 72 72  e){.  sqlite3Err
22ef8 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61  orMsg(pParse, "a
22ef9 75 74 68 6f 72 69 7a 65 72 20 6d 61 6c 66 75 6e  uthorizer malfun
22efa 63 74 69 6f 6e 22 29 3b 0a 20 20 70 50 61 72 73  ction");.  pPars
22efb 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45  e->rc = SQLITE_E
22efc 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  RROR;.}../*.** T
22efd 68 65 20 70 45 78 70 72 20 73 68 6f 75 6c 64 20  he pExpr should 
22efe 62 65 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 65  be a TK_COLUMN e
22eff 78 70 72 65 73 73 69 6f 6e 2e 20 20 54 68 65 20  xpression.  The 
22f00 74 61 62 6c 65 20 72 65 66 65 72 72 65 64 20 74  table referred t
22f01 6f 0a 2a 2a 20 69 73 20 69 6e 20 70 54 61 62 4c  o.** is in pTabL
22f02 69 73 74 20 6f 72 20 65 6c 73 65 20 69 74 20 69  ist or else it i
22f03 73 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c 44  s the NEW or OLD
22f04 20 74 61 62 6c 65 20 6f 66 20 61 20 74 72 69 67   table of a trig
22f05 67 65 72 2e 20 20 0a 2a 2a 20 43 68 65 63 6b 20  ger.  .** Check 
22f06 74 6f 20 73 65 65 20 69 66 20 69 74 20 69 73 20  to see if it is 
22f07 4f 4b 20 74 6f 20 72 65 61 64 20 74 68 69 73 20  OK to read this 
22f08 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6c 75 6d  particular colum
22f09 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  n..**.** If the 
22f0a 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 72 65  auth function re
22f0b 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 49 47 4e  turns SQLITE_IGN
22f0c 4f 52 45 2c 20 63 68 61 6e 67 65 20 74 68 65 20  ORE, change the 
22f0d 54 4b 5f 43 4f 4c 55 4d 4e 20 0a 2a 2a 20 69 6e  TK_COLUMN .** in
22f0e 73 74 72 75 63 74 69 6f 6e 20 69 6e 74 6f 20 61  struction into a
22f0f 20 54 4b 5f 4e 55 4c 4c 2e 20 20 49 66 20 74 68   TK_NULL.  If th
22f10 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20  e auth function 
22f11 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 44  returns SQLITE_D
22f12 45 4e 59 2c 0a 2a 2a 20 74 68 65 6e 20 67 65 6e  ENY,.** then gen
22f13 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 2e 0a  erate an error..
22f14 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
22f15 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75  E void sqlite3Au
22f16 74 68 52 65 61 64 28 0a 20 20 50 61 72 73 65 20  thRead(.  Parse 
22f17 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
22f18 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f  /* The parser co
22f19 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20  ntext */.  Expr 
22f1a 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20  *pExpr,         
22f1b 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
22f1c 6f 6e 20 74 6f 20 63 68 65 63 6b 20 61 75 74 68  on to check auth
22f1d 6f 72 69 7a 61 74 69 6f 6e 20 6f 6e 20 2a 2f 0a  orization on */.
22f1e 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
22f1f 61 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73  a,      /* The s
22f20 63 68 65 6d 61 20 6f 66 20 74 68 65 20 65 78 70  chema of the exp
22f21 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 53 72 63  ression */.  Src
22f22 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 20 20  List *pTabList  
22f23 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 20     /* All table 
22f24 74 68 61 74 20 70 45 78 70 72 20 6d 69 67 68 74  that pExpr might
22f25 20 72 65 66 65 72 20 74 6f 20 2a 2f 0a 29 7b 0a   refer to */.){.
22f26 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
22f27 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e  pParse->db;.  in
22f28 74 20 72 63 3b 0a 20 20 54 61 62 6c 65 20 2a 70  t rc;.  Table *p
22f29 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a  Tab = 0;      /*
22f2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   The table being
22f2b 20 72 65 61 64 20 2a 2f 0a 20 20 63 6f 6e 73 74   read */.  const
22f2c 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20   char *zCol;    
22f2d 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
22f2e 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74 61  column of the ta
22f2f 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 72  ble */.  int iSr
22f30 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  c;             /
22f31 2a 20 49 6e 64 65 78 20 69 6e 20 70 54 61 62 4c  * Index in pTabL
22f32 69 73 74 2d 3e 61 5b 5d 20 6f 66 20 74 61 62 6c  ist->a[] of tabl
22f33 65 20 62 65 69 6e 67 20 72 65 61 64 20 2a 2f 0a  e being read */.
22f34 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
22f35 42 61 73 65 3b 20 20 20 2f 2a 20 4e 61 6d 65 20  Base;   /* Name 
22f36 6f 66 20 64 61 74 61 62 61 73 65 20 62 65 69 6e  of database bein
22f37 67 20 61 63 63 65 73 73 65 64 20 2a 2f 0a 20 20  g accessed */.  
22f38 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
22f39 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64        /* The ind
22f3a 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ex of the databa
22f3b 73 65 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  se the expressio
22f3c 6e 20 72 65 66 65 72 73 20 74 6f 20 2a 2f 0a 20  n refers to */. 
22f3d 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20   int iCol;      
22f3e 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
22f3f 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 61 62  of column in tab
22f40 6c 65 20 2a 2f 0a 0a 20 20 69 66 28 20 64 62 2d  le */..  if( db-
22f41 3e 78 41 75 74 68 3d 3d 30 20 29 20 72 65 74 75  >xAuth==0 ) retu
22f42 72 6e 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69  rn;.  iDb = sqli
22f43 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
22f44 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 63  (pParse->db, pSc
22f45 68 65 6d 61 29 3b 0a 20 20 69 66 28 20 69 44 62  hema);.  if( iDb
22f46 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6e 20  <0 ){.    /* An 
22f47 61 74 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20  attempt to read 
22f48 61 20 63 6f 6c 75 6d 6e 20 6f 75 74 20 6f 66 20  a column out of 
22f49 61 20 73 75 62 71 75 65 72 79 20 6f 72 20 6f 74  a subquery or ot
22f4a 68 65 72 0a 20 20 20 20 2a 2a 20 74 65 6d 70 6f  her.    ** tempo
22f4b 72 61 72 79 20 74 61 62 6c 65 2e 20 2a 2f 0a 20  rary table. */. 
22f4c 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
22f4d 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
22f4e 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c  >op==TK_COLUMN |
22f4f 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  | pExpr->op==TK_
22f50 54 52 49 47 47 45 52 20 29 3b 0a 20 20 69 66 28  TRIGGER );.  if(
22f51 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 54   pExpr->op==TK_T
22f52 52 49 47 47 45 52 20 29 7b 0a 20 20 20 20 70 54  RIGGER ){.    pT
22f53 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 54 72  ab = pParse->pTr
22f54 69 67 67 65 72 54 61 62 3b 0a 20 20 7d 65 6c 73  iggerTab;.  }els
22f55 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
22f56 54 61 62 4c 69 73 74 20 29 3b 0a 20 20 20 20 66  TabList );.    f
22f57 6f 72 28 69 53 72 63 3d 30 3b 20 41 4c 57 41 59  or(iSrc=0; ALWAY
22f58 53 28 69 53 72 63 3c 70 54 61 62 4c 69 73 74 2d  S(iSrc<pTabList-
22f59 3e 6e 53 72 63 29 3b 20 69 53 72 63 2b 2b 29 7b  >nSrc); iSrc++){
22f5a 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72  .      if( pExpr
22f5b 2d 3e 69 54 61 62 6c 65 3d 3d 70 54 61 62 4c 69  ->iTable==pTabLi
22f5c 73 74 2d 3e 61 5b 69 53 72 63 5d 2e 69 43 75 72  st->a[iSrc].iCur
22f5d 73 6f 72 20 29 7b 0a 20 20 20 20 20 20 20 20 70  sor ){.        p
22f5e 54 61 62 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e  Tab = pTabList->
22f5f 61 5b 69 53 72 63 5d 2e 70 54 61 62 3b 0a 20 20  a[iSrc].pTab;.  
22f60 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
22f61 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
22f62 20 69 43 6f 6c 20 3d 20 70 45 78 70 72 2d 3e 69   iCol = pExpr->i
22f63 43 6f 6c 75 6d 6e 3b 0a 20 20 69 66 28 20 4e 45  Column;.  if( NE
22f64 56 45 52 28 70 54 61 62 3d 3d 30 29 20 29 20 72  VER(pTab==0) ) r
22f65 65 74 75 72 6e 3b 0a 0a 20 20 69 66 28 20 69 43  eturn;..  if( iC
22f66 6f 6c 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73  ol>=0 ){.    ass
22f67 65 72 74 28 20 69 43 6f 6c 3c 70 54 61 62 2d 3e  ert( iCol<pTab->
22f68 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 7a 43 6f 6c  nCol );.    zCol
22f69 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43   = pTab->aCol[iC
22f6a 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c  ol].zName;.  }el
22f6b 73 65 20 69 66 28 20 70 54 61 62 2d 3e 69 50 4b  se if( pTab->iPK
22f6c 65 79 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73  ey>=0 ){.    ass
22f6d 65 72 74 28 20 70 54 61 62 2d 3e 69 50 4b 65 79  ert( pTab->iPKey
22f6e 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20  <pTab->nCol );. 
22f6f 20 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e     zCol = pTab->
22f70 61 43 6f 6c 5b 70 54 61 62 2d 3e 69 50 4b 65 79  aCol[pTab->iPKey
22f71 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73 65  ].zName;.  }else
22f72 7b 0a 20 20 20 20 7a 43 6f 6c 20 3d 20 22 52 4f  {.    zCol = "RO
22f73 57 49 44 22 3b 0a 20 20 7d 0a 20 20 61 73 73 65  WID";.  }.  asse
22f74 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44  rt( iDb>=0 && iD
22f75 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 7a  b<db->nDb );.  z
22f76 44 42 61 73 65 20 3d 20 64 62 2d 3e 61 44 62 5b  DBase = db->aDb[
22f77 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 72 63  iDb].zName;.  rc
22f78 20 3d 20 64 62 2d 3e 78 41 75 74 68 28 64 62 2d   = db->xAuth(db-
22f79 3e 70 41 75 74 68 41 72 67 2c 20 53 51 4c 49 54  >pAuthArg, SQLIT
22f7a 45 5f 52 45 41 44 2c 20 70 54 61 62 2d 3e 7a 4e  E_READ, pTab->zN
22f7b 61 6d 65 2c 20 7a 43 6f 6c 2c 20 7a 44 42 61 73  ame, zCol, zDBas
22f7c 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  e, .            
22f7d 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 41 75       pParse->zAu
22f7e 74 68 43 6f 6e 74 65 78 74 29 3b 0a 20 20 69 66  thContext);.  if
22f7f 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 47 4e  ( rc==SQLITE_IGN
22f80 4f 52 45 20 29 7b 0a 20 20 20 20 70 45 78 70 72  ORE ){.    pExpr
22f81 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a  ->op = TK_NULL;.
22f82 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d    }else if( rc==
22f83 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20  SQLITE_DENY ){. 
22f84 20 20 20 69 66 28 20 64 62 2d 3e 6e 44 62 3e 32     if( db->nDb>2
22f85 20 7c 7c 20 69 44 62 21 3d 30 20 29 7b 0a 20 20   || iDb!=0 ){.  
22f86 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
22f87 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 63 63  Msg(pParse, "acc
22f88 65 73 73 20 74 6f 20 25 73 2e 25 73 2e 25 73 20  ess to %s.%s.%s 
22f89 69 73 20 70 72 6f 68 69 62 69 74 65 64 22 2c 20  is prohibited", 
22f8a 0a 20 20 20 20 20 20 20 20 20 7a 44 42 61 73 65  .         zDBase
22f8b 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a  , pTab->zName, z
22f8c 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  Col);.    }else{
22f8d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
22f8e 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
22f8f 61 63 63 65 73 73 20 74 6f 20 25 73 2e 25 73 20  access to %s.%s 
22f90 69 73 20 70 72 6f 68 69 62 69 74 65 64 22 2c 70  is prohibited",p
22f91 54 61 62 2d 3e 7a 4e 61 6d 65 2c 7a 43 6f 6c 29  Tab->zName,zCol)
22f92 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72  ;.    }.    pPar
22f93 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f  se->rc = SQLITE_
22f94 41 55 54 48 3b 0a 20 20 7d 65 6c 73 65 20 69 66  AUTH;.  }else if
22f95 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
22f96 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 41 75 74  ){.    sqliteAut
22f97 68 42 61 64 52 65 74 75 72 6e 43 6f 64 65 28 70  hBadReturnCode(p
22f98 50 61 72 73 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  Parse);.  }.}../
22f99 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 75 74 68 6f  *.** Do an autho
22f9a 72 69 7a 61 74 69 6f 6e 20 63 68 65 63 6b 20 75  rization check u
22f9b 73 69 6e 67 20 74 68 65 20 63 6f 64 65 20 61 6e  sing the code an
22f9c 64 20 61 72 67 75 6d 65 6e 74 73 20 67 69 76 65  d arguments give
22f9d 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 65 69  n.  Return.** ei
22f9e 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b 20 28  ther SQLITE_OK (
22f9f 7a 65 72 6f 29 20 6f 72 20 53 51 4c 49 54 45 5f  zero) or SQLITE_
22fa0 49 47 4e 4f 52 45 20 6f 72 20 53 51 4c 49 54 45  IGNORE or SQLITE
22fa1 5f 44 45 4e 59 2e 20 20 49 66 20 53 51 4c 49 54  _DENY.  If SQLIT
22fa2 45 5f 44 45 4e 59 0a 2a 2a 20 69 73 20 72 65 74  E_DENY.** is ret
22fa3 75 72 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 20  urned, then the 
22fa4 65 72 72 6f 72 20 63 6f 75 6e 74 20 61 6e 64 20  error count and 
22fa5 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e  error message in
22fa6 20 70 50 61 72 73 65 20 61 72 65 0a 2a 2a 20 6d   pParse are.** m
22fa7 6f 64 69 66 69 65 64 20 61 70 70 72 6f 70 72 69  odified appropri
22fa8 61 74 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ately..*/.SQLITE
22fa9 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
22faa 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 0a 20  ite3AuthCheck(. 
22fab 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 0a   Parse *pParse,.
22fac 20 20 69 6e 74 20 63 6f 64 65 2c 0a 20 20 63 6f    int code,.  co
22fad 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67 31 2c  nst char *zArg1,
22fae 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
22faf 41 72 67 32 2c 0a 20 20 63 6f 6e 73 74 20 63 68  Arg2,.  const ch
22fb0 61 72 20 2a 7a 41 72 67 33 0a 29 7b 0a 20 20 73  ar *zArg3.){.  s
22fb1 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
22fb2 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72  rse->db;.  int r
22fb3 63 3b 0a 0a 20 20 2f 2a 20 44 6f 6e 27 74 20 64  c;..  /* Don't d
22fb4 6f 20 61 6e 79 20 61 75 74 68 6f 72 69 7a 61 74  o any authorizat
22fb5 69 6f 6e 20 63 68 65 63 6b 73 20 69 66 20 74 68  ion checks if th
22fb6 65 20 64 61 74 61 62 61 73 65 20 69 73 20 69 6e  e database is in
22fb7 69 74 69 61 6c 69 73 69 6e 67 0a 20 20 2a 2a 20  itialising.  ** 
22fb8 6f 72 20 69 66 20 74 68 65 20 70 61 72 73 65 72  or if the parser
22fb9 20 69 73 20 62 65 69 6e 67 20 69 6e 76 6f 6b 65   is being invoke
22fba 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 73 71  d from within sq
22fbb 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74  lite3_declare_vt
22fbc 61 62 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64  ab..  */.  if( d
22fbd 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c 7c 20  b->init.busy || 
22fbe 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20  IN_DECLARE_VTAB 
22fbf 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
22fc0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
22fc1 69 66 28 20 64 62 2d 3e 78 41 75 74 68 3d 3d 30  if( db->xAuth==0
22fc2 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
22fc3 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
22fc4 72 63 20 3d 20 64 62 2d 3e 78 41 75 74 68 28 64  rc = db->xAuth(d
22fc5 62 2d 3e 70 41 75 74 68 41 72 67 2c 20 63 6f 64  b->pAuthArg, cod
22fc6 65 2c 20 7a 41 72 67 31 2c 20 7a 41 72 67 32 2c  e, zArg1, zArg2,
22fc7 20 7a 41 72 67 33 2c 20 70 50 61 72 73 65 2d 3e   zArg3, pParse->
22fc8 7a 41 75 74 68 43 6f 6e 74 65 78 74 29 3b 0a 20  zAuthContext);. 
22fc9 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
22fca 44 45 4e 59 20 29 7b 0a 20 20 20 20 73 71 6c 69  DENY ){.    sqli
22fcb 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
22fcc 73 65 2c 20 22 6e 6f 74 20 61 75 74 68 6f 72 69  se, "not authori
22fcd 7a 65 64 22 29 3b 0a 20 20 20 20 70 50 61 72 73  zed");.    pPars
22fce 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 41  e->rc = SQLITE_A
22fcf 55 54 48 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  UTH;.  }else if(
22fd0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc!=SQLITE_OK &
22fd1 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 47 4e  & rc!=SQLITE_IGN
22fd2 4f 52 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ORE ){.    rc = 
22fd3 53 51 4c 49 54 45 5f 44 45 4e 59 3b 0a 20 20 20  SQLITE_DENY;.   
22fd4 20 73 71 6c 69 74 65 41 75 74 68 42 61 64 52 65   sqliteAuthBadRe
22fd5 74 75 72 6e 43 6f 64 65 28 70 50 61 72 73 65 29  turnCode(pParse)
22fd6 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
22fd7 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 75 73 68  c;.}../*.** Push
22fd8 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f   an authorizatio
22fd9 6e 20 63 6f 6e 74 65 78 74 2e 20 20 41 66 74 65  n context.  Afte
22fda 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  r this routine i
22fdb 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 0a 2a 2a  s called, the.**
22fdc 20 7a 41 72 67 33 20 61 72 67 75 6d 65 6e 74 20   zArg3 argument 
22fdd 74 6f 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  to authorization
22fde 20 63 61 6c 6c 62 61 63 6b 73 20 77 69 6c 6c 20   callbacks will 
22fdf 62 65 20 7a 43 6f 6e 74 65 78 74 20 75 6e 74 69  be zContext unti
22fe0 6c 0a 2a 2a 20 70 6f 70 70 65 64 2e 20 20 4f 72  l.** popped.  Or
22fe1 20 69 66 20 70 50 61 72 73 65 3d 3d 30 2c 20 74   if pParse==0, t
22fe2 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
22fe3 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54   no-op..*/.SQLIT
22fe4 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
22fe5 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78  qlite3AuthContex
22fe6 74 50 75 73 68 28 0a 20 20 50 61 72 73 65 20 2a  tPush(.  Parse *
22fe7 70 50 61 72 73 65 2c 0a 20 20 41 75 74 68 43 6f  pParse,.  AuthCo
22fe8 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c  ntext *pContext,
22fe9 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   .  const char *
22fea 7a 43 6f 6e 74 65 78 74 0a 29 7b 0a 20 20 61 73  zContext.){.  as
22feb 73 65 72 74 28 20 70 50 61 72 73 65 20 29 3b 0a  sert( pParse );.
22fec 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72    pContext->pPar
22fed 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 70  se = pParse;.  p
22fee 43 6f 6e 74 65 78 74 2d 3e 7a 41 75 74 68 43 6f  Context->zAuthCo
22fef 6e 74 65 78 74 20 3d 20 70 50 61 72 73 65 2d 3e  ntext = pParse->
22ff0 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20  zAuthContext;.  
22ff1 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e  pParse->zAuthCon
22ff2 74 65 78 74 20 3d 20 7a 43 6f 6e 74 65 78 74 3b  text = zContext;
22ff3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 20 61 6e  .}../*.** Pop an
22ff4 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63   authorization c
22ff5 6f 6e 74 65 78 74 20 74 68 61 74 20 77 61 73 20  ontext that was 
22ff6 70 72 65 76 69 6f 75 73 6c 79 20 70 75 73 68 65  previously pushe
22ff7 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 41  d.** by sqlite3A
22ff8 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68 0a 2a  uthContextPush.*
22ff9 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
22ffa 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74   void sqlite3Aut
22ffb 68 43 6f 6e 74 65 78 74 50 6f 70 28 41 75 74 68  hContextPop(Auth
22ffc 43 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78  Context *pContex
22ffd 74 29 7b 0a 20 20 69 66 28 20 70 43 6f 6e 74 65  t){.  if( pConte
22ffe 78 74 2d 3e 70 50 61 72 73 65 20 29 7b 0a 20 20  xt->pParse ){.  
22fff 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72    pContext->pPar
23000 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74  se->zAuthContext
23001 20 3d 20 70 43 6f 6e 74 65 78 74 2d 3e 7a 41 75   = pContext->zAu
23002 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 70  thContext;.    p
23003 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72 73 65 20  Context->pParse 
23004 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64  = 0;.  }.}..#end
23005 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
23006 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 20  T_AUTHORIZATION 
23007 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
23008 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 75 74 68 2e  *** End of auth.
23009 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2300a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2300b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2300c 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
2300d 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62  *** Begin file b
2300e 75 69 6c 64 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  uild.c *********
2300f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23011 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
23012 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
23013 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
23014 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
23015 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
23016 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
23017 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
23018 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
23019 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
2301a 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
2301b 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
2301c 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
2301d 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
2301e 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
2301f 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
23020 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
23021 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
23022 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
23023 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
23024 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
23025 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23026 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23027 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23028 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
23029 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
2302a 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65 73   C code routines
2302b 20 74 68 61 74 20 61 72 65 20 63 61 6c 6c 65 64   that are called
2302c 20 62 79 20 74 68 65 20 53 51 4c 69 74 65 20 70   by the SQLite p
2302d 61 72 73 65 72 0a 2a 2a 20 77 68 65 6e 20 73 79  arser.** when sy
2302e 6e 74 61 78 20 72 75 6c 65 73 20 61 72 65 20 72  ntax rules are r
2302f 65 64 75 63 65 64 2e 20 20 54 68 65 20 72 6f 75  educed.  The rou
23030 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69  tines in this fi
23031 6c 65 20 68 61 6e 64 6c 65 20 74 68 65 0a 2a 2a  le handle the.**
23032 20 66 6f 6c 6c 6f 77 69 6e 67 20 6b 69 6e 64 73   following kinds
23033 20 6f 66 20 53 51 4c 20 73 79 6e 74 61 78 3a 0a   of SQL syntax:.
23034 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45  **.**     CREATE
23035 20 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 44 52   TABLE.**     DR
23036 4f 50 20 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20  OP TABLE.**     
23037 43 52 45 41 54 45 20 49 4e 44 45 58 0a 2a 2a 20  CREATE INDEX.** 
23038 20 20 20 20 44 52 4f 50 20 49 4e 44 45 58 0a 2a      DROP INDEX.*
23039 2a 20 20 20 20 20 63 72 65 61 74 69 6e 67 20 49  *     creating I
2303a 44 20 6c 69 73 74 73 0a 2a 2a 20 20 20 20 20 42  D lists.**     B
2303b 45 47 49 4e 20 54 52 41 4e 53 41 43 54 49 4f 4e  EGIN TRANSACTION
2303c 0a 2a 2a 20 20 20 20 20 43 4f 4d 4d 49 54 0a 2a  .**     COMMIT.*
2303d 2a 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b 0a 2a  *     ROLLBACK.*
2303e 2a 0a 2a 2a 20 24 49 64 3a 20 62 75 69 6c 64 2e  *.** $Id: build.
2303f 63 2c 76 20 31 2e 35 35 37 20 32 30 30 39 2f 30  c,v 1.557 2009/0
23040 37 2f 32 34 20 31 37 3a 35 38 3a 35 33 20 64 61  7/24 17:58:53 da
23041 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a  nielk1977 Exp $.
23042 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  */../*.** This r
23043 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
23044 20 77 68 65 6e 20 61 20 6e 65 77 20 53 51 4c 20   when a new SQL 
23045 73 74 61 74 65 6d 65 6e 74 20 69 73 20 62 65 67  statement is beg
23046 69 6e 6e 69 6e 67 20 74 6f 0a 2a 2a 20 62 65 20  inning to.** be 
23047 70 61 72 73 65 64 2e 20 20 49 6e 69 74 69 61 6c  parsed.  Initial
23048 69 7a 65 20 74 68 65 20 70 50 61 72 73 65 20 73  ize the pParse s
23049 74 72 75 63 74 75 72 65 20 61 73 20 6e 65 65 64  tructure as need
2304a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
2304b 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2304c 65 33 42 65 67 69 6e 50 61 72 73 65 28 50 61 72  e3BeginParse(Par
2304d 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
2304e 65 78 70 6c 61 69 6e 46 6c 61 67 29 7b 0a 20 20  explainFlag){.  
2304f 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e 20  pParse->explain 
23050 3d 20 28 75 38 29 65 78 70 6c 61 69 6e 46 6c 61  = (u8)explainFla
23051 67 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 56 61  g;.  pParse->nVa
23052 72 20 3d 20 30 3b 0a 7d 0a 0a 23 69 66 6e 64 65  r = 0;.}..#ifnde
23053 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
23054 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a  ARED_CACHE./*.**
23055 20 54 68 65 20 54 61 62 6c 65 4c 6f 63 6b 20 73   The TableLock s
23056 74 72 75 63 74 75 72 65 20 69 73 20 6f 6e 6c 79  tructure is only
23057 20 75 73 65 64 20 62 79 20 74 68 65 20 73 71 6c   used by the sql
23058 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29 20  ite3TableLock() 
23059 61 6e 64 0a 2a 2a 20 63 6f 64 65 54 61 62 6c 65  and.** codeTable
2305a 4c 6f 63 6b 73 28 29 20 66 75 6e 63 74 69 6f 6e  Locks() function
2305b 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54 61 62  s..*/.struct Tab
2305c 6c 65 4c 6f 63 6b 20 7b 0a 20 20 69 6e 74 20 69  leLock {.  int i
2305d 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
2305e 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
2305f 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74  containing the t
23060 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65  able to be locke
23061 64 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 3b  d */.  int iTab;
23062 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
23063 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20  he root page of 
23064 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20  the table to be 
23065 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 38 20 69  locked */.  u8 i
23066 73 57 72 69 74 65 4c 6f 63 6b 3b 20 20 20 20 20  sWriteLock;     
23067 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 77 72 69   /* True for wri
23068 74 65 20 6c 6f 63 6b 2e 20 20 46 61 6c 73 65 20  te lock.  False 
23069 66 6f 72 20 61 20 72 65 61 64 20 6c 6f 63 6b 20  for a read lock 
2306a 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
2306b 2a 7a 4e 61 6d 65 3b 20 20 20 2f 2a 20 4e 61 6d  *zName;   /* Nam
2306c 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a  e of the table *
2306d 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f  /.};../*.** Reco
2306e 72 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74  rd the fact that
2306f 20 77 65 20 77 61 6e 74 20 74 6f 20 6c 6f 63 6b   we want to lock
23070 20 61 20 74 61 62 6c 65 20 61 74 20 72 75 6e 2d   a table at run-
23071 74 69 6d 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68  time.  .**.** Th
23072 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f  e table to be lo
23073 63 6b 65 64 20 68 61 73 20 72 6f 6f 74 20 70 61  cked has root pa
23074 67 65 20 69 54 61 62 20 61 6e 64 20 69 73 20 66  ge iTab and is f
23075 6f 75 6e 64 20 69 6e 20 64 61 74 61 62 61 73 65  ound in database
23076 20 69 44 62 2e 0a 2a 2a 20 41 20 72 65 61 64 20   iDb..** A read 
23077 6f 72 20 61 20 77 72 69 74 65 20 6c 6f 63 6b 20  or a write lock 
23078 63 61 6e 20 62 65 20 74 61 6b 65 6e 20 64 65 70  can be taken dep
23079 65 6e 64 69 6e 67 20 6f 6e 20 69 73 57 72 69 74  ending on isWrit
2307a 65 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  elock..**.** Thi
2307b 73 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 20 72  s routine just r
2307c 65 63 6f 72 64 73 20 74 68 65 20 66 61 63 74 20  ecords the fact 
2307d 74 68 61 74 20 74 68 65 20 6c 6f 63 6b 20 69 73  that the lock is
2307e 20 64 65 73 69 72 65 64 2e 20 20 54 68 65 0a 2a   desired.  The.*
2307f 2a 20 63 6f 64 65 20 74 6f 20 6d 61 6b 65 20 74  * code to make t
23080 68 65 20 6c 6f 63 6b 20 6f 63 63 75 72 20 69 73  he lock occur is
23081 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 61 20   generated by a 
23082 6c 61 74 65 72 20 63 61 6c 6c 20 74 6f 0a 2a 2a  later call to.**
23083 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28   codeTableLocks(
23084 29 20 77 68 69 63 68 20 6f 63 63 75 72 73 20 64  ) which occurs d
23085 75 72 69 6e 67 20 73 71 6c 69 74 65 33 46 69 6e  uring sqlite3Fin
23086 69 73 68 43 6f 64 69 6e 67 28 29 2e 0a 2a 2f 0a  ishCoding()..*/.
23087 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
23088 6f 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65  oid sqlite3Table
23089 4c 6f 63 6b 28 0a 20 20 50 61 72 73 65 20 2a 70  Lock(.  Parse *p
2308a 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61  Parse,     /* Pa
2308b 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
2308c 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20  .  int iDb,     
2308d 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
2308e 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  f the database c
2308f 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61  ontaining the ta
23090 62 6c 65 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20  ble to lock */. 
23091 20 69 6e 74 20 69 54 61 62 2c 20 20 20 20 20 20   int iTab,      
23092 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65      /* Root page
23093 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 74   number of the t
23094 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65  able to be locke
23095 64 20 2a 2f 0a 20 20 75 38 20 69 73 57 72 69 74  d */.  u8 isWrit
23096 65 4c 6f 63 6b 2c 20 20 20 20 2f 2a 20 54 72 75  eLock,    /* Tru
23097 65 20 66 6f 72 20 61 20 77 72 69 74 65 20 6c 6f  e for a write lo
23098 63 6b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ck */.  const ch
23099 61 72 20 2a 7a 4e 61 6d 65 20 20 2f 2a 20 4e 61  ar *zName  /* Na
2309a 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
2309b 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a  to be locked */.
2309c 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 54 6f 70  ){.  Parse *pTop
2309d 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65 33 50  level = sqlite3P
2309e 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50 61  arseToplevel(pPa
2309f 72 73 65 29 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  rse);.  int i;. 
230a0 20 69 6e 74 20 6e 42 79 74 65 73 3b 0a 20 20 54   int nBytes;.  T
230a1 61 62 6c 65 4c 6f 63 6b 20 2a 70 3b 0a 20 20 61  ableLock *p;.  a
230a2 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 29 3b  ssert( iDb>=0 );
230a3 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ..  for(i=0; i<p
230a4 54 6f 70 6c 65 76 65 6c 2d 3e 6e 54 61 62 6c 65  Toplevel->nTable
230a5 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Lock; i++){.    
230a6 70 20 3d 20 26 70 54 6f 70 6c 65 76 65 6c 2d 3e  p = &pToplevel->
230a7 61 54 61 62 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a 20  aTableLock[i];. 
230a8 20 20 20 69 66 28 20 70 2d 3e 69 44 62 3d 3d 69     if( p->iDb==i
230a9 44 62 20 26 26 20 70 2d 3e 69 54 61 62 3d 3d 69  Db && p->iTab==i
230aa 54 61 62 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  Tab ){.      p->
230ab 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 28 70  isWriteLock = (p
230ac 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20 7c 7c  ->isWriteLock ||
230ad 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b 0a 20   isWriteLock);. 
230ae 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
230af 20 7d 0a 20 20 7d 0a 0a 20 20 6e 42 79 74 65 73   }.  }..  nBytes
230b0 20 3d 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 4c   = sizeof(TableL
230b1 6f 63 6b 29 20 2a 20 28 70 54 6f 70 6c 65 76 65  ock) * (pTopleve
230b2 6c 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 2b 31 29  l->nTableLock+1)
230b3 3b 0a 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 61  ;.  pToplevel->a
230b4 54 61 62 6c 65 4c 6f 63 6b 20 3d 0a 20 20 20 20  TableLock =.    
230b5 20 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c    sqlite3DbReall
230b6 6f 63 4f 72 46 72 65 65 28 70 54 6f 70 6c 65 76  ocOrFree(pToplev
230b7 65 6c 2d 3e 64 62 2c 20 70 54 6f 70 6c 65 76 65  el->db, pTopleve
230b8 6c 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 2c 20 6e  l->aTableLock, n
230b9 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 70 54  Bytes);.  if( pT
230ba 6f 70 6c 65 76 65 6c 2d 3e 61 54 61 62 6c 65 4c  oplevel->aTableL
230bb 6f 63 6b 20 29 7b 0a 20 20 20 20 70 20 3d 20 26  ock ){.    p = &
230bc 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 54 61 62 6c  pToplevel->aTabl
230bd 65 4c 6f 63 6b 5b 70 54 6f 70 6c 65 76 65 6c 2d  eLock[pToplevel-
230be 3e 6e 54 61 62 6c 65 4c 6f 63 6b 2b 2b 5d 3b 0a  >nTableLock++];.
230bf 20 20 20 20 70 2d 3e 69 44 62 20 3d 20 69 44 62      p->iDb = iDb
230c0 3b 0a 20 20 20 20 70 2d 3e 69 54 61 62 20 3d 20  ;.    p->iTab = 
230c1 69 54 61 62 3b 0a 20 20 20 20 70 2d 3e 69 73 57  iTab;.    p->isW
230c2 72 69 74 65 4c 6f 63 6b 20 3d 20 69 73 57 72 69  riteLock = isWri
230c3 74 65 4c 6f 63 6b 3b 0a 20 20 20 20 70 2d 3e 7a  teLock;.    p->z
230c4 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20  Name = zName;.  
230c5 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 6f 70 6c  }else{.    pTopl
230c6 65 76 65 6c 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b  evel->nTableLock
230c7 20 3d 20 30 3b 0a 20 20 20 20 70 54 6f 70 6c 65   = 0;.    pTople
230c8 76 65 6c 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  vel->db->mallocF
230c9 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d  ailed = 1;.  }.}
230ca 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20  ../*.** Code an 
230cb 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 69 6e 73  OP_TableLock ins
230cc 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 65 61 63  truction for eac
230cd 68 20 74 61 62 6c 65 20 6c 6f 63 6b 65 64 20 62  h table locked b
230ce 79 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65  y the.** stateme
230cf 6e 74 20 28 63 6f 6e 66 69 67 75 72 65 64 20 62  nt (configured b
230d0 79 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  y calls to sqlit
230d1 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2e 0a  e3TableLock())..
230d2 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
230d3 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 50 61  odeTableLocks(Pa
230d4 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20  rse *pParse){.  
230d5 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 20 2a 70  int i;.  Vdbe *p
230d6 56 64 62 65 3b 20 0a 0a 20 20 70 56 64 62 65 20  Vdbe; ..  pVdbe 
230d7 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
230d8 28 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73 65  (pParse);.  asse
230d9 72 74 28 20 70 56 64 62 65 21 3d 30 20 29 3b 20  rt( pVdbe!=0 ); 
230da 2f 2a 20 73 71 6c 69 74 65 33 47 65 74 56 64 62  /* sqlite3GetVdb
230db 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c 3a 20 56  e cannot fail: V
230dc 44 42 45 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f  DBE already allo
230dd 63 61 74 65 64 20 2a 2f 0a 0a 20 20 66 6f 72 28  cated */..  for(
230de 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e  i=0; i<pParse->n
230df 54 61 62 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29 7b  TableLock; i++){
230e0 0a 20 20 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a  .    TableLock *
230e1 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61 54 61  p = &pParse->aTa
230e2 62 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20 20  bleLock[i];.    
230e3 69 6e 74 20 70 31 20 3d 20 70 2d 3e 69 44 62 3b  int p1 = p->iDb;
230e4 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
230e5 41 64 64 4f 70 34 28 70 56 64 62 65 2c 20 4f 50  AddOp4(pVdbe, OP
230e6 5f 54 61 62 6c 65 4c 6f 63 6b 2c 20 70 31 2c 20  _TableLock, p1, 
230e7 70 2d 3e 69 54 61 62 2c 20 70 2d 3e 69 73 57 72  p->iTab, p->isWr
230e8 69 74 65 4c 6f 63 6b 2c 0a 20 20 20 20 20 20 20  iteLock,.       
230e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
230ea 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54  ->zName, P4_STAT
230eb 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65  IC);.  }.}.#else
230ec 0a 20 20 23 64 65 66 69 6e 65 20 63 6f 64 65 54  .  #define codeT
230ed 61 62 6c 65 4c 6f 63 6b 73 28 78 29 0a 23 65 6e  ableLocks(x).#en
230ee 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  dif../*.** This 
230ef 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
230f0 64 20 61 66 74 65 72 20 61 20 73 69 6e 67 6c 65  d after a single
230f1 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 68   SQL statement h
230f2 61 73 20 62 65 65 6e 0a 2a 2a 20 70 61 72 73 65  as been.** parse
230f3 64 20 61 6e 64 20 61 20 56 44 42 45 20 70 72 6f  d and a VDBE pro
230f4 67 72 61 6d 20 74 6f 20 65 78 65 63 75 74 65 20  gram to execute 
230f5 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 20 68  that statement h
230f6 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72 65 70 61  as been.** prepa
230f7 72 65 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69  red.  This routi
230f8 6e 65 20 70 75 74 73 20 74 68 65 20 66 69 6e 69  ne puts the fini
230f9 73 68 69 6e 67 20 74 6f 75 63 68 65 73 20 6f 6e  shing touches on
230fa 20 74 68 65 0a 2a 2a 20 56 44 42 45 20 70 72 6f   the.** VDBE pro
230fb 67 72 61 6d 20 61 6e 64 20 72 65 73 65 74 73 20  gram and resets 
230fc 74 68 65 20 70 50 61 72 73 65 20 73 74 72 75 63  the pParse struc
230fd 74 75 72 65 20 66 6f 72 20 74 68 65 20 6e 65 78  ture for the nex
230fe 74 0a 2a 2a 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a  t.** parse..**.*
230ff 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 61  * Note that if a
23100 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64  n error occurred
23101 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20 74 68  , it might be th
23102 65 20 63 61 73 65 20 74 68 61 74 0a 2a 2a 20 6e  e case that.** n
23103 6f 20 56 44 42 45 20 63 6f 64 65 20 77 61 73 20  o VDBE code was 
23104 67 65 6e 65 72 61 74 65 64 2e 0a 2a 2f 0a 53 51  generated..*/.SQ
23105 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
23106 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43  d sqlite3FinishC
23107 6f 64 69 6e 67 28 50 61 72 73 65 20 2a 70 50 61  oding(Parse *pPa
23108 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  rse){.  sqlite3 
23109 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a  *db;.  Vdbe *v;.
2310a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  .  db = pParse->
2310b 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  db;.  if( db->ma
2310c 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74  llocFailed ) ret
2310d 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72 73  urn;.  if( pPars
2310e 65 2d 3e 6e 65 73 74 65 64 20 29 20 72 65 74 75  e->nested ) retu
2310f 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  rn;.  if( pParse
23110 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72 6e 3b  ->nErr ) return;
23111 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 62 79 20  ..  /* Begin by 
23112 67 65 6e 65 72 61 74 69 6e 67 20 73 6f 6d 65 20  generating some 
23113 74 65 72 6d 69 6e 61 74 69 6f 6e 20 63 6f 64 65  termination code
23114 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74   at the end of t
23115 68 65 0a 20 20 2a 2a 20 76 64 62 65 20 70 72 6f  he.  ** vdbe pro
23116 67 72 61 6d 0a 20 20 2a 2f 0a 20 20 76 20 3d 20  gram.  */.  v = 
23117 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
23118 50 61 72 73 65 29 3b 0a 20 20 61 73 73 65 72 74  Parse);.  assert
23119 28 20 70 50 61 72 73 65 2d 3e 69 73 4d 75 6c 74  ( pParse->isMult
2311a 69 57 72 69 74 65 3d 3d 30 20 7c 7c 20 73 71 6c  iWrite==0 || sql
2311b 69 74 65 33 56 64 62 65 4d 61 79 41 62 6f 72 74  ite3VdbeMayAbort
2311c 28 76 29 3d 3d 70 50 61 72 73 65 2d 3e 6d 61 79  (v)==pParse->may
2311d 41 62 6f 72 74 20 29 3b 0a 20 20 69 66 28 20 76  Abort );.  if( v
2311e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
2311f 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f  dbeAddOp0(v, OP_
23120 48 61 6c 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 54  Halt);..    /* T
23121 68 65 20 63 6f 6f 6b 69 65 20 6d 61 73 6b 20 63  he cookie mask c
23122 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 62 69 74 20  ontains one bit 
23123 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73  for each databas
23124 65 20 66 69 6c 65 20 6f 70 65 6e 2e 0a 20 20 20  e file open..   
23125 20 2a 2a 20 28 42 69 74 20 30 20 69 73 20 66 6f   ** (Bit 0 is fo
23126 72 20 6d 61 69 6e 2c 20 62 69 74 20 31 20 69 73  r main, bit 1 is
23127 20 66 6f 72 20 74 65 6d 70 2c 20 61 6e 64 20 73   for temp, and s
23128 6f 20 66 6f 72 74 68 2e 29 20 20 42 69 74 73 20  o forth.)  Bits 
23129 61 72 65 0a 20 20 20 20 2a 2a 20 73 65 74 20 66  are.    ** set f
2312a 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73 65  or each database
2312b 20 74 68 61 74 20 69 73 20 75 73 65 64 2e 20 20   that is used.  
2312c 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
2312d 20 73 74 61 72 74 20 61 0a 20 20 20 20 2a 2a 20   start a.    ** 
2312e 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 65  transaction on e
2312f 61 63 68 20 75 73 65 64 20 64 61 74 61 62 61 73  ach used databas
23130 65 20 61 6e 64 20 74 6f 20 76 65 72 69 66 79 20  e and to verify 
23131 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  the schema cooki
23132 65 0a 20 20 20 20 2a 2a 20 6f 6e 20 65 61 63 68  e.    ** on each
23133 20 75 73 65 64 20 64 61 74 61 62 61 73 65 2e 0a   used database..
23134 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
23135 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74  Parse->cookieGot
23136 6f 3e 30 20 29 7b 0a 20 20 20 20 20 20 75 33 32  o>0 ){.      u32
23137 20 6d 61 73 6b 3b 0a 20 20 20 20 20 20 69 6e 74   mask;.      int
23138 20 69 44 62 3b 0a 20 20 20 20 20 20 73 71 6c 69   iDb;.      sqli
23139 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
2313a 76 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69  v, pParse->cooki
2313b 65 47 6f 74 6f 2d 31 29 3b 0a 20 20 20 20 20 20  eGoto-1);.      
2313c 66 6f 72 28 69 44 62 3d 30 2c 20 6d 61 73 6b 3d  for(iDb=0, mask=
2313d 31 3b 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20  1; iDb<db->nDb; 
2313e 6d 61 73 6b 3c 3c 3d 31 2c 20 69 44 62 2b 2b 29  mask<<=1, iDb++)
2313f 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 6d  {.        if( (m
23140 61 73 6b 20 26 20 70 50 61 72 73 65 2d 3e 63 6f  ask & pParse->co
23141 6f 6b 69 65 4d 61 73 6b 29 3d 3d 30 20 29 20 63  okieMask)==0 ) c
23142 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
23143 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73   sqlite3VdbeUses
23144 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20  Btree(v, iDb);. 
23145 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
23146 62 65 41 64 64 4f 70 32 28 76 2c 4f 50 5f 54 72  beAddOp2(v,OP_Tr
23147 61 6e 73 61 63 74 69 6f 6e 2c 20 69 44 62 2c 20  ansaction, iDb, 
23148 28 6d 61 73 6b 20 26 20 70 50 61 72 73 65 2d 3e  (mask & pParse->
23149 77 72 69 74 65 4d 61 73 6b 29 21 3d 30 29 3b 0a  writeMask)!=0);.
2314a 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e          if( db->
2314b 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29 7b 0a  init.busy==0 ){.
2314c 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2314d 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 4f 50  3VdbeAddOp2(v,OP
2314e 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 2c 20 69  _VerifyCookie, i
2314f 44 62 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b  Db, pParse->cook
23150 69 65 56 61 6c 75 65 5b 69 44 62 5d 29 3b 0a 20  ieValue[iDb]);. 
23151 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
23152 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
23153 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
23154 45 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20  E.      {.      
23155 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20    int i;.       
23156 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 72   for(i=0; i<pPar
23157 73 65 2d 3e 6e 56 74 61 62 4c 6f 63 6b 3b 20 69  se->nVtabLock; i
23158 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63  ++){.          c
23159 68 61 72 20 2a 76 74 61 62 20 3d 20 28 63 68 61  har *vtab = (cha
2315a 72 20 2a 29 73 71 6c 69 74 65 33 47 65 74 56 54  r *)sqlite3GetVT
2315b 61 62 6c 65 28 64 62 2c 20 70 50 61 72 73 65 2d  able(db, pParse-
2315c 3e 61 70 56 74 61 62 4c 6f 63 6b 5b 69 5d 29 3b  >apVtabLock[i]);
2315d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2315e 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2315f 4f 50 5f 56 42 65 67 69 6e 2c 20 30 2c 20 30 2c  OP_VBegin, 0, 0,
23160 20 30 2c 20 76 74 61 62 2c 20 50 34 5f 56 54 41   0, vtab, P4_VTA
23161 42 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  B);.        }.  
23162 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56        pParse->nV
23163 74 61 62 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20  tabLock = 0;.   
23164 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
23165 20 20 20 2f 2a 20 4f 6e 63 65 20 61 6c 6c 20 74     /* Once all t
23166 68 65 20 63 6f 6f 6b 69 65 73 20 68 61 76 65 20  he cookies have 
23167 62 65 65 6e 20 76 65 72 69 66 69 65 64 20 61 6e  been verified an
23168 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 6f  d transactions o
23169 70 65 6e 65 64 2c 20 0a 20 20 20 20 20 20 2a 2a  pened, .      **
2316a 20 6f 62 74 61 69 6e 20 74 68 65 20 72 65 71 75   obtain the requ
2316b 69 72 65 64 20 74 61 62 6c 65 2d 6c 6f 63 6b 73  ired table-locks
2316c 2e 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f  . This is a no-o
2316d 70 20 75 6e 6c 65 73 73 20 74 68 65 20 0a 20 20  p unless the .  
2316e 20 20 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 61      ** shared-ca
2316f 63 68 65 20 66 65 61 74 75 72 65 20 69 73 20 65  che feature is e
23170 6e 61 62 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f  nabled..      */
23171 0a 20 20 20 20 20 20 63 6f 64 65 54 61 62 6c 65  .      codeTable
23172 4c 6f 63 6b 73 28 70 50 61 72 73 65 29 3b 0a 0a  Locks(pParse);..
23173 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c        /* Initial
23174 69 7a 65 20 61 6e 79 20 41 55 54 4f 49 4e 43 52  ize any AUTOINCR
23175 45 4d 45 4e 54 20 64 61 74 61 20 73 74 72 75 63  EMENT data struc
23176 74 75 72 65 73 20 72 65 71 75 69 72 65 64 2e 0a  tures required..
23177 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
23178 71 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d  qlite3Autoincrem
23179 65 6e 74 42 65 67 69 6e 28 70 50 61 72 73 65 29  entBegin(pParse)
2317a 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69 6e 61  ;..      /* Fina
2317b 6c 6c 79 2c 20 6a 75 6d 70 20 62 61 63 6b 20 74  lly, jump back t
2317c 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  o the beginning 
2317d 6f 66 20 74 68 65 20 65 78 65 63 75 74 61 62 6c  of the executabl
2317e 65 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 20 20 20  e code. */.     
2317f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
23180 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
23181 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65  , pParse->cookie
23182 47 6f 74 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Goto);.    }.  }
23183 0a 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20  ...  /* Get the 
23184 56 44 42 45 20 70 72 6f 67 72 61 6d 20 72 65 61  VDBE program rea
23185 64 79 20 66 6f 72 20 65 78 65 63 75 74 69 6f 6e  dy for execution
23186 0a 20 20 2a 2f 0a 20 20 69 66 28 20 76 20 26 26  .  */.  if( v &&
23187 20 41 4c 57 41 59 53 28 70 50 61 72 73 65 2d 3e   ALWAYS(pParse->
23188 6e 45 72 72 3d 3d 30 29 20 26 26 20 21 64 62 2d  nErr==0) && !db-
23189 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
2318a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
2318b 45 42 55 47 0a 20 20 20 20 46 49 4c 45 20 2a 74  EBUG.    FILE *t
2318c 72 61 63 65 20 3d 20 28 64 62 2d 3e 66 6c 61 67  race = (db->flag
2318d 73 20 26 20 53 51 4c 49 54 45 5f 56 64 62 65 54  s & SQLITE_VdbeT
2318e 72 61 63 65 29 21 3d 30 20 3f 20 73 74 64 6f 75  race)!=0 ? stdou
2318f 74 20 3a 20 30 3b 0a 20 20 20 20 73 71 6c 69 74  t : 0;.    sqlit
23190 65 33 56 64 62 65 54 72 61 63 65 28 76 2c 20 74  e3VdbeTrace(v, t
23191 72 61 63 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20  race);.#endif.  
23192 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
23193 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3d 3d 30  ->iCacheLevel==0
23194 20 29 3b 20 20 2f 2a 20 44 69 73 61 62 6c 65 73   );  /* Disables
23195 20 61 6e 64 20 72 65 2d 65 6e 61 62 6c 65 73 20   and re-enables 
23196 6d 61 74 63 68 20 2a 2f 0a 20 20 20 20 73 71 6c  match */.    sql
23197 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64  ite3VdbeMakeRead
23198 79 28 76 2c 20 70 50 61 72 73 65 2d 3e 6e 56 61  y(v, pParse->nVa
23199 72 2c 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2c  r, pParse->nMem,
2319a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2319b 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65            pParse
2319c 2d 3e 6e 54 61 62 2c 20 70 50 61 72 73 65 2d 3e  ->nTab, pParse->
2319d 6e 4d 61 78 41 72 67 2c 20 70 50 61 72 73 65 2d  nMaxArg, pParse-
2319e 3e 65 78 70 6c 61 69 6e 2c 0a 20 20 20 20 20 20  >explain,.      
2319f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
231a0 20 20 20 70 50 61 72 73 65 2d 3e 69 73 4d 75 6c     pParse->isMul
231a1 74 69 57 72 69 74 65 20 26 26 20 70 50 61 72 73  tiWrite && pPars
231a2 65 2d 3e 6d 61 79 41 62 6f 72 74 29 3b 0a 20 20  e->mayAbort);.  
231a3 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
231a4 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20  QLITE_DONE;.    
231a5 70 50 61 72 73 65 2d 3e 63 6f 6c 4e 61 6d 65 73  pParse->colNames
231a6 53 65 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  Set = 0;.  }else
231a7 20 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63 3d   if( pParse->rc=
231a8 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
231a9 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
231aa 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
231ab 0a 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20  .  pParse->nTab 
231ac 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e  = 0;.  pParse->n
231ad 4d 65 6d 20 3d 20 30 3b 0a 20 20 70 50 61 72 73  Mem = 0;.  pPars
231ae 65 2d 3e 6e 53 65 74 20 3d 20 30 3b 0a 20 20 70  e->nSet = 0;.  p
231af 50 61 72 73 65 2d 3e 6e 56 61 72 20 3d 20 30 3b  Parse->nVar = 0;
231b0 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69  .  pParse->cooki
231b1 65 4d 61 73 6b 20 3d 20 30 3b 0a 20 20 70 50 61  eMask = 0;.  pPa
231b2 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 20  rse->cookieGoto 
231b3 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 75  = 0;.}../*.** Ru
231b4 6e 20 74 68 65 20 70 61 72 73 65 72 20 61 6e 64  n the parser and
231b5 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20   code generator 
231b6 72 65 63 75 72 73 69 76 65 6c 79 20 69 6e 20 6f  recursively in o
231b7 72 64 65 72 20 74 6f 20 67 65 6e 65 72 61 74 65  rder to generate
231b8 0a 2a 2a 20 63 6f 64 65 20 66 6f 72 20 74 68 65  .** code for the
231b9 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 67   SQL statement g
231ba 69 76 65 6e 20 6f 6e 74 6f 20 74 68 65 20 65 6e  iven onto the en
231bb 64 20 6f 66 20 74 68 65 20 70 50 61 72 73 65 20  d of the pParse 
231bc 63 6f 6e 74 65 78 74 0a 2a 2a 20 63 75 72 72 65  context.** curre
231bd 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74  ntly under const
231be 72 75 63 74 69 6f 6e 2e 20 20 57 68 65 6e 20 74  ruction.  When t
231bf 68 65 20 70 61 72 73 65 72 20 69 73 20 72 75 6e  he parser is run
231c0 20 72 65 63 75 72 73 69 76 65 6c 79 0a 2a 2a 20   recursively.** 
231c1 74 68 69 73 20 77 61 79 2c 20 74 68 65 20 66 69  this way, the fi
231c2 6e 61 6c 20 4f 50 5f 48 61 6c 74 20 69 73 20 6e  nal OP_Halt is n
231c3 6f 74 20 61 70 70 65 6e 64 65 64 20 61 6e 64 20  ot appended and 
231c4 6f 74 68 65 72 20 69 6e 69 74 69 61 6c 69 7a 61  other initializa
231c5 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 66 69 6e 61  tion.** and fina
231c6 6c 69 7a 61 74 69 6f 6e 20 73 74 65 70 73 20 61  lization steps a
231c7 72 65 20 6f 6d 69 74 74 65 64 20 62 65 63 61 75  re omitted becau
231c8 73 65 20 74 68 6f 73 65 20 61 72 65 20 68 61 6e  se those are han
231c9 64 6c 69 6e 67 20 62 79 20 74 68 65 0a 2a 2a 20  dling by the.** 
231ca 6f 75 74 65 72 6d 6f 73 74 20 70 61 72 73 65 72  outermost parser
231cb 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 20 65 76 65 72  ..**.** Not ever
231cc 79 74 68 69 6e 67 20 69 73 20 6e 65 73 74 61 62  ything is nestab
231cd 6c 65 2e 20 20 54 68 69 73 20 66 61 63 69 6c 69  le.  This facili
231ce 74 79 20 69 73 20 64 65 73 69 67 6e 65 64 20 74  ty is designed t
231cf 6f 20 70 65 72 6d 69 74 0a 2a 2a 20 49 4e 53 45  o permit.** INSE
231d0 52 54 2c 20 55 50 44 41 54 45 2c 20 61 6e 64 20  RT, UPDATE, and 
231d1 44 45 4c 45 54 45 20 6f 70 65 72 61 74 69 6f 6e  DELETE operation
231d2 73 20 61 67 61 69 6e 73 74 20 53 51 4c 49 54 45  s against SQLITE
231d3 5f 4d 41 53 54 45 52 2e 20 20 55 73 65 0a 2a 2a  _MASTER.  Use.**
231d4 20 63 61 72 65 20 69 66 20 79 6f 75 20 64 65 63   care if you dec
231d5 69 64 65 20 74 6f 20 74 72 79 20 74 6f 20 75 73  ide to try to us
231d6 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66  e this routine f
231d7 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 75  or some other pu
231d8 72 70 6f 73 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54  rposes..*/.SQLIT
231d9 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
231da 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
231db 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
231dc 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
231dd 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  rmat, ...){.  va
231de 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72  _list ap;.  char
231df 20 2a 7a 53 71 6c 3b 0a 20 20 63 68 61 72 20 2a   *zSql;.  char *
231e0 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 73  zErrMsg = 0;.  s
231e1 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
231e2 72 73 65 2d 3e 64 62 3b 0a 23 20 64 65 66 69 6e  rse->db;.# defin
231e3 65 20 53 41 56 45 5f 53 5a 20 20 28 73 69 7a 65  e SAVE_SZ  (size
231e4 6f 66 28 50 61 72 73 65 29 20 2d 20 6f 66 66 73  of(Parse) - offs
231e5 65 74 6f 66 28 50 61 72 73 65 2c 6e 56 61 72 29  etof(Parse,nVar)
231e6 29 0a 20 20 63 68 61 72 20 73 61 76 65 42 75 66  ).  char saveBuf
231e7 5b 53 41 56 45 5f 53 5a 5d 3b 0a 0a 20 20 69 66  [SAVE_SZ];..  if
231e8 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29  ( pParse->nErr )
231e9 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72   return;.  asser
231ea 74 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  t( pParse->neste
231eb 64 3c 31 30 20 29 3b 20 20 2f 2a 20 4e 65 73 74  d<10 );  /* Nest
231ec 69 6e 67 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20  ing should only 
231ed 62 65 20 6f 66 20 6c 69 6d 69 74 65 64 20 64 65  be of limited de
231ee 70 74 68 20 2a 2f 0a 20 20 76 61 5f 73 74 61 72  pth */.  va_star
231ef 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a  t(ap, zFormat);.
231f0 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33    zSql = sqlite3
231f1 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f  VMPrintf(db, zFo
231f2 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f  rmat, ap);.  va_
231f3 65 6e 64 28 61 70 29 3b 0a 20 20 69 66 28 20 7a  end(ap);.  if( z
231f4 53 71 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  Sql==0 ){.    re
231f5 74 75 72 6e 3b 20 20 20 2f 2a 20 41 20 6d 61 6c  turn;   /* A mal
231f6 6c 6f 63 20 6d 75 73 74 20 68 61 76 65 20 66 61  loc must have fa
231f7 69 6c 65 64 20 2a 2f 0a 20 20 7d 0a 20 20 70 50  iled */.  }.  pP
231f8 61 72 73 65 2d 3e 6e 65 73 74 65 64 2b 2b 3b 0a  arse->nested++;.
231f9 20 20 6d 65 6d 63 70 79 28 73 61 76 65 42 75 66    memcpy(saveBuf
231fa 2c 20 26 70 50 61 72 73 65 2d 3e 6e 56 61 72 2c  , &pParse->nVar,
231fb 20 53 41 56 45 5f 53 5a 29 3b 0a 20 20 6d 65 6d   SAVE_SZ);.  mem
231fc 73 65 74 28 26 70 50 61 72 73 65 2d 3e 6e 56 61  set(&pParse->nVa
231fd 72 2c 20 30 2c 20 53 41 56 45 5f 53 5a 29 3b 0a  r, 0, SAVE_SZ);.
231fe 20 20 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73    sqlite3RunPars
231ff 65 72 28 70 50 61 72 73 65 2c 20 7a 53 71 6c 2c  er(pParse, zSql,
23200 20 26 7a 45 72 72 4d 73 67 29 3b 0a 20 20 73 71   &zErrMsg);.  sq
23201 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
23202 7a 45 72 72 4d 73 67 29 3b 0a 20 20 73 71 6c 69  zErrMsg);.  sqli
23203 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53  te3DbFree(db, zS
23204 71 6c 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26 70  ql);.  memcpy(&p
23205 50 61 72 73 65 2d 3e 6e 56 61 72 2c 20 73 61 76  Parse->nVar, sav
23206 65 42 75 66 2c 20 53 41 56 45 5f 53 5a 29 3b 0a  eBuf, SAVE_SZ);.
23207 20 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64    pParse->nested
23208 2d 2d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63  --;.}../*.** Loc
23209 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72  ate the in-memor
2320a 79 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  y structure that
2320b 20 64 65 73 63 72 69 62 65 73 20 61 20 70 61 72   describes a par
2320c 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65  ticular database
2320d 0a 2a 2a 20 74 61 62 6c 65 20 67 69 76 65 6e 20  .** table given 
2320e 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 61 74  the name of that
2320f 20 74 61 62 6c 65 20 61 6e 64 20 28 6f 70 74 69   table and (opti
23210 6f 6e 61 6c 6c 79 29 20 74 68 65 20 6e 61 6d 65  onally) the name
23211 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62   of the.** datab
23212 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ase containing t
23213 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72  he table.  Retur
23214 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 66 6f  n NULL if not fo
23215 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 44  und..**.** If zD
23216 61 74 61 62 61 73 65 20 69 73 20 30 2c 20 61 6c  atabase is 0, al
23217 6c 20 64 61 74 61 62 61 73 65 73 20 61 72 65 20  l databases are 
23218 73 65 61 72 63 68 65 64 20 66 6f 72 20 74 68 65  searched for the
23219 20 74 61 62 6c 65 20 61 6e 64 20 74 68 65 0a 2a   table and the.*
2321a 2a 20 66 69 72 73 74 20 6d 61 74 63 68 69 6e 67  * first matching
2321b 20 74 61 62 6c 65 20 69 73 20 72 65 74 75 72 6e   table is return
2321c 65 64 2e 20 20 28 4e 6f 20 63 68 65 63 6b 69 6e  ed.  (No checkin
2321d 67 20 66 6f 72 20 64 75 70 6c 69 63 61 74 65 20  g for duplicate 
2321e 74 61 62 6c 65 0a 2a 2a 20 6e 61 6d 65 73 20 69  table.** names i
2321f 73 20 64 6f 6e 65 2e 29 20 20 54 68 65 20 73 65  s done.)  The se
23220 61 72 63 68 20 6f 72 64 65 72 20 69 73 20 54 45  arch order is TE
23221 4d 50 20 66 69 72 73 74 2c 20 74 68 65 6e 20 4d  MP first, then M
23222 41 49 4e 2c 20 74 68 65 6e 20 61 6e 79 0a 2a 2a  AIN, then any.**
23223 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62   auxiliary datab
23224 61 73 65 73 20 61 64 64 65 64 20 75 73 69 6e 67  ases added using
23225 20 74 68 65 20 41 54 54 41 43 48 20 63 6f 6d 6d   the ATTACH comm
23226 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  and..**.** See a
23227 6c 73 6f 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  lso sqlite3Locat
23228 65 54 61 62 6c 65 28 29 2e 0a 2a 2f 0a 53 51 4c  eTable()..*/.SQL
23229 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c  ITE_PRIVATE Tabl
2322a 65 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 54 61  e *sqlite3FindTa
2322b 62 6c 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ble(sqlite3 *db,
2322c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
2322d 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  me, const char *
2322e 7a 44 61 74 61 62 61 73 65 29 7b 0a 20 20 54 61  zDatabase){.  Ta
2322f 62 6c 65 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e  ble *p = 0;.  in
23230 74 20 69 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65  t i;.  int nName
23231 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 4e 61 6d  ;.  assert( zNam
23232 65 21 3d 30 20 29 3b 0a 20 20 6e 4e 61 6d 65 20  e!=0 );.  nName 
23233 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
23234 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 28  0(zName);.  for(
23235 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69  i=OMIT_TEMPDB; i
23236 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
23237 20 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32      int j = (i<2
23238 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 20 2f  ) ? i^1 : i;   /
23239 2a 20 53 65 61 72 63 68 20 54 45 4d 50 20 62 65  * Search TEMP be
2323a 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20  fore MAIN */.   
2323b 20 69 66 28 20 7a 44 61 74 61 62 61 73 65 21 3d   if( zDatabase!=
2323c 30 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49  0 && sqlite3StrI
2323d 43 6d 70 28 7a 44 61 74 61 62 61 73 65 2c 20 64  Cmp(zDatabase, d
2323e 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 29  b->aDb[j].zName)
2323f 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
23240 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68   p = sqlite3Hash
23241 46 69 6e 64 28 26 64 62 2d 3e 61 44 62 5b 6a 5d  Find(&db->aDb[j]
23242 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73  .pSchema->tblHas
23243 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  h, zName, nName)
23244 3b 0a 20 20 20 20 69 66 28 20 70 20 29 20 62 72  ;.    if( p ) br
23245 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  eak;.  }.  retur
23246 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f  n p;.}../*.** Lo
23247 63 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f  cate the in-memo
23248 72 79 20 73 74 72 75 63 74 75 72 65 20 74 68 61  ry structure tha
23249 74 20 64 65 73 63 72 69 62 65 73 20 61 20 70 61  t describes a pa
2324a 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73  rticular databas
2324b 65 0a 2a 2a 20 74 61 62 6c 65 20 67 69 76 65 6e  e.** table given
2324c 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 61   the name of tha
2324d 74 20 74 61 62 6c 65 20 61 6e 64 20 28 6f 70 74  t table and (opt
2324e 69 6f 6e 61 6c 6c 79 29 20 74 68 65 20 6e 61 6d  ionally) the nam
2324f 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61  e of the.** data
23250 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
23251 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75  the table.  Retu
23252 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 66  rn NULL if not f
23253 6f 75 6e 64 2e 20 20 41 6c 73 6f 20 6c 65 61 76  ound.  Also leav
23254 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 6d 65  e an.** error me
23255 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 2d  ssage in pParse-
23256 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20  >zErrMsg..**.** 
23257 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62  The difference b
23258 65 74 77 65 65 6e 20 74 68 69 73 20 72 6f 75 74  etween this rout
23259 69 6e 65 20 61 6e 64 20 73 71 6c 69 74 65 33 46  ine and sqlite3F
2325a 69 6e 64 54 61 62 6c 65 28 29 20 69 73 20 74 68  indTable() is th
2325b 61 74 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69  at this.** routi
2325c 6e 65 20 6c 65 61 76 65 73 20 61 6e 20 65 72 72  ne leaves an err
2325d 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50  or message in pP
2325e 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 77 68  arse->zErrMsg wh
2325f 65 72 65 0a 2a 2a 20 73 71 6c 69 74 65 33 46 69  ere.** sqlite3Fi
23260 6e 64 54 61 62 6c 65 28 29 20 64 6f 65 73 20 6e  ndTable() does n
23261 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ot..*/.SQLITE_PR
23262 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c  IVATE Table *sql
23263 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
23264 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
23265 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6e  ,         /* con
23266 74 65 78 74 20 69 6e 20 77 68 69 63 68 20 74 6f  text in which to
23267 20 72 65 70 6f 72 74 20 65 72 72 6f 72 73 20 2a   report errors *
23268 2f 0a 20 20 69 6e 74 20 69 73 56 69 65 77 2c 20  /.  int isView, 
23269 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
2326a 75 65 20 69 66 20 6c 6f 6f 6b 69 6e 67 20 66 6f  ue if looking fo
2326b 72 20 61 20 56 49 45 57 20 72 61 74 68 65 72 20  r a VIEW rather 
2326c 74 68 61 6e 20 61 20 54 41 42 4c 45 20 2a 2f 0a  than a TABLE */.
2326d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
2326e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ame,     /* Name
2326f 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 77 65   of the table we
23270 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72   are looking for
23271 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
23272 20 2a 7a 44 62 61 73 65 20 20 20 20 20 2f 2a 20   *zDbase     /* 
23273 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  Name of the data
23274 62 61 73 65 2e 20 20 4d 69 67 68 74 20 62 65 20  base.  Might be 
23275 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 54 61 62  NULL */.){.  Tab
23276 6c 65 20 2a 70 3b 0a 0a 20 20 2f 2a 20 52 65 61  le *p;..  /* Rea
23277 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  d the database s
23278 63 68 65 6d 61 2e 20 49 66 20 61 6e 20 65 72 72  chema. If an err
23279 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65 61 76 65  or occurs, leave
2327a 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
2327b 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f 64 65 20  e.  ** and code 
2327c 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72 65  in pParse and re
2327d 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20  turn NULL. */.  
2327e 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73  if( SQLITE_OK!=s
2327f 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
23280 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20  (pParse) ){.    
23281 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20  return 0;.  }.. 
23282 20 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64   p = sqlite3Find
23283 54 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 64 62  Table(pParse->db
23284 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62 61 73 65 29  , zName, zDbase)
23285 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a  ;.  if( p==0 ){.
23286 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
23287 7a 4d 73 67 20 3d 20 69 73 56 69 65 77 20 3f 20  zMsg = isView ? 
23288 22 6e 6f 20 73 75 63 68 20 76 69 65 77 22 20 3a  "no such view" :
23289 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 22   "no such table"
2328a 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 61 73 65  ;.    if( zDbase
2328b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2328c 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2328d 2c 20 22 25 73 3a 20 25 73 2e 25 73 22 2c 20 7a  , "%s: %s.%s", z
2328e 4d 73 67 2c 20 7a 44 62 61 73 65 2c 20 7a 4e 61  Msg, zDbase, zNa
2328f 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  me);.    }else{.
23290 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
23291 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25  orMsg(pParse, "%
23292 73 3a 20 25 73 22 2c 20 7a 4d 73 67 2c 20 7a 4e  s: %s", zMsg, zN
23293 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ame);.    }.    
23294 70 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68  pParse->checkSch
23295 65 6d 61 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72  ema = 1;.  }.  r
23296 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
23297 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 2d  * Locate the in-
23298 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65  memory structure
23299 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20   that describes 
2329a 0a 2a 2a 20 61 20 70 61 72 74 69 63 75 6c 61 72  .** a particular
2329b 20 69 6e 64 65 78 20 67 69 76 65 6e 20 74 68 65   index given the
2329c 20 6e 61 6d 65 20 6f 66 20 74 68 61 74 20 69 6e   name of that in
2329d 64 65 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e  dex.** and the n
2329e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
2329f 61 73 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ase that contain
232a0 73 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 20  s the index..** 
232a1 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e  Return NULL if n
232a2 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20  ot found..**.** 
232a3 49 66 20 7a 44 61 74 61 62 61 73 65 20 69 73 20  If zDatabase is 
232a4 30 2c 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73  0, all databases
232a5 20 61 72 65 20 73 65 61 72 63 68 65 64 20 66 6f   are searched fo
232a6 72 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 61  r the.** table a
232a7 6e 64 20 74 68 65 20 66 69 72 73 74 20 6d 61 74  nd the first mat
232a8 63 68 69 6e 67 20 69 6e 64 65 78 20 69 73 20 72  ching index is r
232a9 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63 68  eturned.  (No ch
232aa 65 63 6b 69 6e 67 0a 2a 2a 20 66 6f 72 20 64 75  ecking.** for du
232ab 70 6c 69 63 61 74 65 20 69 6e 64 65 78 20 6e 61  plicate index na
232ac 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29 20 20 54  mes is done.)  T
232ad 68 65 20 73 65 61 72 63 68 20 6f 72 64 65 72 20  he search order 
232ae 69 73 0a 2a 2a 20 54 45 4d 50 20 66 69 72 73 74  is.** TEMP first
232af 2c 20 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65  , then MAIN, the
232b0 6e 20 61 6e 79 20 61 75 78 69 6c 69 61 72 79 20  n any auxiliary 
232b1 64 61 74 61 62 61 73 65 73 20 61 64 64 65 64 0a  databases added.
232b2 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 41 54 54  ** using the ATT
232b3 41 43 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a  ACH command..*/.
232b4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49  SQLITE_PRIVATE I
232b5 6e 64 65 78 20 2a 73 71 6c 69 74 65 33 46 69 6e  ndex *sqlite3Fin
232b6 64 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a  dIndex(sqlite3 *
232b7 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
232b8 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61  zName, const cha
232b9 72 20 2a 7a 44 62 29 7b 0a 20 20 49 6e 64 65 78  r *zDb){.  Index
232ba 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69   *p = 0;.  int i
232bb 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20  ;.  int nName = 
232bc 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
232bd 7a 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 28 69 3d  zName);.  for(i=
232be 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69 3c 64  OMIT_TEMPDB; i<d
232bf 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20  b->nDb; i++){.  
232c0 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32 29 20    int j = (i<2) 
232c1 3f 20 69 5e 31 20 3a 20 69 3b 20 20 2f 2a 20 53  ? i^1 : i;  /* S
232c2 65 61 72 63 68 20 54 45 4d 50 20 62 65 66 6f 72  earch TEMP befor
232c3 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20 20 53 63  e MAIN */.    Sc
232c4 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20  hema *pSchema = 
232c5 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 63 68 65  db->aDb[j].pSche
232c6 6d 61 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ma;.    assert( 
232c7 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 69  pSchema );.    i
232c8 66 28 20 7a 44 62 20 26 26 20 73 71 6c 69 74 65  f( zDb && sqlite
232c9 33 53 74 72 49 43 6d 70 28 7a 44 62 2c 20 64 62  3StrICmp(zDb, db
232ca 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 29 20  ->aDb[j].zName) 
232cb 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
232cc 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46  p = sqlite3HashF
232cd 69 6e 64 28 26 70 53 63 68 65 6d 61 2d 3e 69 64  ind(&pSchema->id
232ce 78 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e  xHash, zName, nN
232cf 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70 20  ame);.    if( p 
232d0 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 72  ) break;.  }.  r
232d1 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
232d2 2a 20 52 65 63 6c 61 69 6d 20 74 68 65 20 6d 65  * Reclaim the me
232d3 6d 6f 72 79 20 75 73 65 64 20 62 79 20 61 6e 20  mory used by an 
232d4 69 6e 64 65 78 0a 2a 2f 0a 73 74 61 74 69 63 20  index.*/.static 
232d5 76 6f 69 64 20 66 72 65 65 49 6e 64 65 78 28 49  void freeIndex(I
232d6 6e 64 65 78 20 2a 70 29 7b 0a 20 20 73 71 6c 69  ndex *p){.  sqli
232d7 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 70 54 61  te3 *db = p->pTa
232d8 62 6c 65 2d 3e 64 62 4d 65 6d 3b 0a 20 20 2f 2a  ble->dbMem;.  /*
232d9 20 74 65 73 74 63 61 73 65 28 20 64 62 3d 3d 30   testcase( db==0
232da 20 29 3b 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33   ); */.  sqlite3
232db 44 65 6c 65 74 65 49 6e 64 65 78 53 61 6d 70 6c  DeleteIndexSampl
232dc 65 73 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  es(p);.  sqlite3
232dd 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 43  DbFree(db, p->zC
232de 6f 6c 41 66 66 29 3b 0a 20 20 73 71 6c 69 74 65  olAff);.  sqlite
232df 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a  3DbFree(db, p);.
232e0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20  }../*.** Remove 
232e1 74 68 65 20 67 69 76 65 6e 20 69 6e 64 65 78 20  the given index 
232e2 66 72 6f 6d 20 74 68 65 20 69 6e 64 65 78 20 68  from the index h
232e3 61 73 68 20 74 61 62 6c 65 2c 20 61 6e 64 20 66  ash table, and f
232e4 72 65 65 0a 2a 2a 20 69 74 73 20 6d 65 6d 6f 72  ree.** its memor
232e5 79 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2a  y structures..**
232e6 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 69 73  .** The index is
232e7 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68   removed from th
232e8 65 20 64 61 74 61 62 61 73 65 20 68 61 73 68 20  e database hash 
232e9 74 61 62 6c 65 73 20 62 75 74 0a 2a 2a 20 69 74  tables but.** it
232ea 20 69 73 20 6e 6f 74 20 75 6e 6c 69 6e 6b 65 64   is not unlinked
232eb 20 66 72 6f 6d 20 74 68 65 20 54 61 62 6c 65 20   from the Table 
232ec 74 68 61 74 20 69 74 20 69 6e 64 65 78 65 73 2e  that it indexes.
232ed 0a 2a 2a 20 55 6e 6c 69 6e 6b 69 6e 67 20 66 72  .** Unlinking fr
232ee 6f 6d 20 74 68 65 20 54 61 62 6c 65 20 6d 75 73  om the Table mus
232ef 74 20 62 65 20 64 6f 6e 65 20 62 79 20 74 68 65  t be done by the
232f0 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f   calling functio
232f1 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
232f2 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 49  d sqlite3DeleteI
232f3 6e 64 65 78 28 49 6e 64 65 78 20 2a 70 29 7b 0a  ndex(Index *p){.
232f4 20 20 49 6e 64 65 78 20 2a 70 4f 6c 64 3b 0a 20    Index *pOld;. 
232f5 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
232f6 6d 65 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b 0a 0a  me = p->zName;..
232f7 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33    pOld = sqlite3
232f8 48 61 73 68 49 6e 73 65 72 74 28 26 70 2d 3e 70  HashInsert(&p->p
232f9 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 2c  Schema->idxHash,
232fa 20 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20   zName,.        
232fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
232fc 20 20 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e     sqlite3Strlen
232fd 33 30 28 7a 4e 61 6d 65 29 2c 20 30 29 3b 0a 20  30(zName), 0);. 
232fe 20 61 73 73 65 72 74 28 20 70 4f 6c 64 3d 3d 30   assert( pOld==0
232ff 20 7c 7c 20 70 4f 6c 64 3d 3d 70 20 29 3b 0a 20   || pOld==p );. 
23300 20 66 72 65 65 49 6e 64 65 78 28 70 29 3b 0a 7d   freeIndex(p);.}
23301 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20  ../*.** For the 
23302 69 6e 64 65 78 20 63 61 6c 6c 65 64 20 7a 49 64  index called zId
23303 78 4e 61 6d 65 20 77 68 69 63 68 20 69 73 20 66  xName which is f
23304 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61  ound in the data
23305 62 61 73 65 20 69 44 62 2c 0a 2a 2a 20 75 6e 6c  base iDb,.** unl
23306 69 6b 65 20 74 68 61 74 20 69 6e 64 65 78 20 66  ike that index f
23307 72 6f 6d 20 69 74 73 20 54 61 62 6c 65 20 74 68  rom its Table th
23308 65 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 69 6e  en remove the in
23309 64 65 78 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20  dex from.** the 
2330a 69 6e 64 65 78 20 68 61 73 68 20 74 61 62 6c 65  index hash table
2330b 20 61 6e 64 20 66 72 65 65 20 61 6c 6c 20 6d 65   and free all me
2330c 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 73 20  mory structures 
2330d 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69  associated.** wi
2330e 74 68 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2f  th the index..*/
2330f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
23310 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69  void sqlite3Unli
23311 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78  nkAndDeleteIndex
23312 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
23313 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61  t iDb, const cha
23314 72 20 2a 7a 49 64 78 4e 61 6d 65 29 7b 0a 20 20  r *zIdxName){.  
23315 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20  Index *pIndex;. 
23316 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 48 61 73 68   int len;.  Hash
23317 20 2a 70 48 61 73 68 20 3d 20 26 64 62 2d 3e 61   *pHash = &db->a
23318 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d  Db[iDb].pSchema-
23319 3e 69 64 78 48 61 73 68 3b 0a 0a 20 20 6c 65 6e  >idxHash;..  len
2331a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
2331b 33 30 28 7a 49 64 78 4e 61 6d 65 29 3b 0a 20 20  30(zIdxName);.  
2331c 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33  pIndex = sqlite3
2331d 48 61 73 68 49 6e 73 65 72 74 28 70 48 61 73 68  HashInsert(pHash
2331e 2c 20 7a 49 64 78 4e 61 6d 65 2c 20 6c 65 6e 2c  , zIdxName, len,
2331f 20 30 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65   0);.  if( pInde
23320 78 20 29 7b 0a 20 20 20 20 69 66 28 20 70 49 6e  x ){.    if( pIn
23321 64 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e  dex->pTable->pIn
23322 64 65 78 3d 3d 70 49 6e 64 65 78 20 29 7b 0a 20  dex==pIndex ){. 
23323 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 54 61       pIndex->pTa
23324 62 6c 65 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49  ble->pIndex = pI
23325 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  ndex->pNext;.   
23326 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 49 6e   }else{.      In
23327 64 65 78 20 2a 70 3b 0a 20 20 20 20 20 20 2f 2a  dex *p;.      /*
23328 20 4a 75 73 74 69 66 69 63 61 74 69 6f 6e 20 6f   Justification o
23329 66 20 41 4c 57 41 59 53 28 29 3b 20 20 54 68 65  f ALWAYS();  The
2332a 20 69 6e 64 65 78 20 6d 75 73 74 20 62 65 20 6f   index must be o
2332b 6e 20 74 68 65 20 6c 69 73 74 20 6f 66 0a 20 20  n the list of.  
2332c 20 20 20 20 2a 2a 20 69 6e 64 69 63 65 73 2e 20      ** indices. 
2332d 2a 2f 0a 20 20 20 20 20 20 70 20 3d 20 70 49 6e  */.      p = pIn
2332e 64 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e  dex->pTable->pIn
2332f 64 65 78 3b 0a 20 20 20 20 20 20 77 68 69 6c 65  dex;.      while
23330 28 20 41 4c 57 41 59 53 28 70 29 20 26 26 20 70  ( ALWAYS(p) && p
23331 2d 3e 70 4e 65 78 74 21 3d 70 49 6e 64 65 78 20  ->pNext!=pIndex 
23332 29 7b 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b  ){ p = p->pNext;
23333 20 7d 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57   }.      if( ALW
23334 41 59 53 28 70 20 26 26 20 70 2d 3e 70 4e 65 78  AYS(p && p->pNex
23335 74 3d 3d 70 49 6e 64 65 78 29 20 29 7b 0a 20 20  t==pIndex) ){.  
23336 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d        p->pNext =
23337 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a   pIndex->pNext;.
23338 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
23339 20 20 66 72 65 65 49 6e 64 65 78 28 70 49 6e 64    freeIndex(pInd
2333a 65 78 29 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 66  ex);.  }.  db->f
2333b 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49  lags |= SQLITE_I
2333c 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a  nternChanges;.}.
2333d 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c  ./*.** Erase all
2333e 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74   schema informat
2333f 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d  ion from the in-
23340 6d 65 6d 6f 72 79 20 68 61 73 68 20 74 61 62 6c  memory hash tabl
23341 65 73 20 6f 66 0a 2a 2a 20 61 20 73 69 6e 67 6c  es of.** a singl
23342 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69  e database.  Thi
23343 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
23344 6c 65 64 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d  led to reclaim m
23345 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20  emory.** before 
23346 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6c 6f  the database clo
23347 73 65 73 2e 20 20 49 74 20 69 73 20 61 6c 73 6f  ses.  It is also
23348 20 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 61   called during a
23349 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20   rollback.** if 
2334a 74 68 65 72 65 20 77 65 72 65 20 73 63 68 65 6d  there were schem
2334b 61 20 63 68 61 6e 67 65 73 20 64 75 72 69 6e 67  a changes during
2334c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
2334d 20 6f 72 20 69 66 20 61 0a 2a 2a 20 73 63 68 65   or if a.** sche
2334e 6d 61 2d 63 6f 6f 6b 69 65 20 6d 69 73 6d 61 74  ma-cookie mismat
2334f 63 68 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a  ch occurs..**.**
23350 20 49 66 20 69 44 62 3d 3d 30 20 74 68 65 6e 20   If iDb==0 then 
23351 72 65 73 65 74 20 74 68 65 20 69 6e 74 65 72 6e  reset the intern
23352 61 6c 20 73 63 68 65 6d 61 20 74 61 62 6c 65 73  al schema tables
23353 20 66 6f 72 20 61 6c 6c 20 64 61 74 61 62 61 73   for all databas
23354 65 0a 2a 2a 20 66 69 6c 65 73 2e 20 20 49 66 20  e.** files.  If 
23355 69 44 62 3e 3d 31 20 74 68 65 6e 20 72 65 73 65  iDb>=1 then rese
23356 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73  t the internal s
23357 63 68 65 6d 61 20 66 6f 72 20 6f 6e 6c 79 20 74  chema for only t
23358 68 65 0a 2a 2a 20 73 69 6e 67 6c 65 20 66 69 6c  he.** single fil
23359 65 20 69 6e 64 69 63 61 74 65 64 2e 0a 2a 2f 0a  e indicated..*/.
2335a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2335b 6f 69 64 20 73 71 6c 69 74 65 33 52 65 73 65 74  oid sqlite3Reset
2335c 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 73  InternalSchema(s
2335d 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
2335e 69 44 62 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a  iDb){.  int i, j
2335f 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e  ;.  assert( iDb>
23360 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44  =0 && iDb<db->nD
23361 62 20 29 3b 0a 0a 20 20 69 66 28 20 69 44 62 3d  b );..  if( iDb=
23362 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
23363 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64  3BtreeEnterAll(d
23364 62 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  b);.  }.  for(i=
23365 69 44 62 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  iDb; i<db->nDb; 
23366 69 2b 2b 29 7b 0a 20 20 20 20 44 62 20 2a 70 44  i++){.    Db *pD
23367 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b  b = &db->aDb[i];
23368 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 53  .    if( pDb->pS
23369 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 61  chema ){.      a
2336a 73 73 65 72 74 28 69 3d 3d 31 20 7c 7c 20 28 70  ssert(i==1 || (p
2336b 44 62 2d 3e 70 42 74 20 26 26 20 73 71 6c 69 74  Db->pBt && sqlit
2336c 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65  e3BtreeHoldsMute
2336d 78 28 70 44 62 2d 3e 70 42 74 29 29 29 3b 0a 20  x(pDb->pBt)));. 
2336e 20 20 20 20 20 73 71 6c 69 74 65 33 53 63 68 65       sqlite3Sche
2336f 6d 61 46 72 65 65 28 70 44 62 2d 3e 70 53 63 68  maFree(pDb->pSch
23370 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ema);.    }.    
23371 69 66 28 20 69 44 62 3e 30 20 29 20 72 65 74 75  if( iDb>0 ) retu
23372 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  rn;.  }.  assert
23373 28 20 69 44 62 3d 3d 30 20 29 3b 0a 20 20 64 62  ( iDb==0 );.  db
23374 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49  ->flags &= ~SQLI
23375 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73  TE_InternChanges
23376 3b 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 55  ;.  sqlite3VtabU
23377 6e 6c 6f 63 6b 4c 69 73 74 28 64 62 29 3b 0a 20  nlockList(db);. 
23378 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
23379 76 65 41 6c 6c 28 64 62 29 3b 0a 0a 20 20 2f 2a  veAll(db);..  /*
2337a 20 49 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20   If one or more 
2337b 6f 66 20 74 68 65 20 61 75 78 69 6c 69 61 72 79  of the auxiliary
2337c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20   database files 
2337d 68 61 73 20 62 65 65 6e 20 63 6c 6f 73 65 64 2c  has been closed,
2337e 0a 20 20 2a 2a 20 74 68 65 6e 20 72 65 6d 6f 76  .  ** then remov
2337f 65 20 74 68 65 6d 20 66 72 6f 6d 20 74 68 65 20  e them from the 
23380 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61  auxiliary databa
23381 73 65 20 6c 69 73 74 2e 20 20 57 65 20 74 61 6b  se list.  We tak
23382 65 20 74 68 65 0a 20 20 2a 2a 20 6f 70 70 6f 72  e the.  ** oppor
23383 74 75 6e 69 74 79 20 74 6f 20 64 6f 20 74 68 69  tunity to do thi
23384 73 20 68 65 72 65 20 73 69 6e 63 65 20 77 65 20  s here since we 
23385 68 61 76 65 20 6a 75 73 74 20 64 65 6c 65 74 65  have just delete
23386 64 20 61 6c 6c 20 6f 66 20 74 68 65 0a 20 20 2a  d all of the.  *
23387 2a 20 73 63 68 65 6d 61 20 68 61 73 68 20 74 61  * schema hash ta
23388 62 6c 65 73 20 61 6e 64 20 74 68 65 72 65 66 6f  bles and therefo
23389 72 65 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74  re do not have t
2338a 6f 20 6d 61 6b 65 20 61 6e 79 20 63 68 61 6e 67  o make any chang
2338b 65 73 0a 20 20 2a 2a 20 74 6f 20 61 6e 79 20 6f  es.  ** to any o
2338c 66 20 74 68 6f 73 65 20 74 61 62 6c 65 73 2e 0a  f those tables..
2338d 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 6a 3d 32    */.  for(i=j=2
2338e 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
2338f 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 44 62  ){.    struct Db
23390 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62   *pDb = &db->aDb
23391 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62  [i];.    if( pDb
23392 2d 3e 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20  ->pBt==0 ){.    
23393 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
23394 64 62 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 3b  db, pDb->zName);
23395 0a 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d  .      pDb->zNam
23396 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 6f 6e  e = 0;.      con
23397 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20  tinue;.    }.   
23398 20 69 66 28 20 6a 3c 69 20 29 7b 0a 20 20 20 20   if( j<i ){.    
23399 20 20 64 62 2d 3e 61 44 62 5b 6a 5d 20 3d 20 64    db->aDb[j] = d
2339a 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 7d  b->aDb[i];.    }
2339b 0a 20 20 20 20 6a 2b 2b 3b 0a 20 20 7d 0a 20 20  .    j++;.  }.  
2339c 6d 65 6d 73 65 74 28 26 64 62 2d 3e 61 44 62 5b  memset(&db->aDb[
2339d 6a 5d 2c 20 30 2c 20 28 64 62 2d 3e 6e 44 62 2d  j], 0, (db->nDb-
2339e 6a 29 2a 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44  j)*sizeof(db->aD
2339f 62 5b 6a 5d 29 29 3b 0a 20 20 64 62 2d 3e 6e 44  b[j]));.  db->nD
233a0 62 20 3d 20 6a 3b 0a 20 20 69 66 28 20 64 62 2d  b = j;.  if( db-
233a1 3e 6e 44 62 3c 3d 32 20 26 26 20 64 62 2d 3e 61  >nDb<=2 && db->a
233a2 44 62 21 3d 64 62 2d 3e 61 44 62 53 74 61 74 69  Db!=db->aDbStati
233a3 63 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28  c ){.    memcpy(
233a4 64 62 2d 3e 61 44 62 53 74 61 74 69 63 2c 20 64  db->aDbStatic, d
233a5 62 2d 3e 61 44 62 2c 20 32 2a 73 69 7a 65 6f 66  b->aDb, 2*sizeof
233a6 28 64 62 2d 3e 61 44 62 5b 30 5d 29 29 3b 0a 20  (db->aDb[0]));. 
233a7 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
233a8 28 64 62 2c 20 64 62 2d 3e 61 44 62 29 3b 0a 20  (db, db->aDb);. 
233a9 20 20 20 64 62 2d 3e 61 44 62 20 3d 20 64 62 2d     db->aDb = db-
233aa 3e 61 44 62 53 74 61 74 69 63 3b 0a 20 20 7d 0a  >aDbStatic;.  }.
233ab 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
233ac 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
233ad 77 68 65 6e 20 61 20 63 6f 6d 6d 69 74 20 6f 63  when a commit oc
233ae 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  curs..*/.SQLITE_
233af 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
233b0 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e  ite3CommitIntern
233b1 61 6c 43 68 61 6e 67 65 73 28 73 71 6c 69 74 65  alChanges(sqlite
233b2 33 20 2a 64 62 29 7b 0a 20 20 64 62 2d 3e 66 6c  3 *db){.  db->fl
233b3 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 49  ags &= ~SQLITE_I
233b4 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a  nternChanges;.}.
233b5 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65  ./*.** Clear the
233b6 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 72   column names fr
233b7 6f 6d 20 61 20 74 61 62 6c 65 20 6f 72 20 76 69  om a table or vi
233b8 65 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ew..*/.static vo
233b9 69 64 20 73 71 6c 69 74 65 52 65 73 65 74 43 6f  id sqliteResetCo
233ba 6c 75 6d 6e 4e 61 6d 65 73 28 54 61 62 6c 65 20  lumnNames(Table 
233bb 2a 70 54 61 62 6c 65 29 7b 0a 20 20 69 6e 74 20  *pTable){.  int 
233bc 69 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f  i;.  Column *pCo
233bd 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  l;.  sqlite3 *db
233be 20 3d 20 70 54 61 62 6c 65 2d 3e 64 62 4d 65 6d   = pTable->dbMem
233bf 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 64 62  ;.  testcase( db
233c0 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
233c1 20 70 54 61 62 6c 65 21 3d 30 20 29 3b 0a 20 20   pTable!=0 );.  
233c2 69 66 28 20 28 70 43 6f 6c 20 3d 20 70 54 61 62  if( (pCol = pTab
233c3 6c 65 2d 3e 61 43 6f 6c 29 21 3d 30 20 29 7b 0a  le->aCol)!=0 ){.
233c4 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
233c5 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  Table->nCol; i++
233c6 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20  , pCol++){.     
233c7 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
233c8 62 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b  b, pCol->zName);
233c9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
233ca 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f  prDelete(db, pCo
233cb 6c 2d 3e 70 44 66 6c 74 29 3b 0a 20 20 20 20 20  l->pDflt);.     
233cc 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
233cd 62 2c 20 70 43 6f 6c 2d 3e 7a 44 66 6c 74 29 3b  b, pCol->zDflt);
233ce 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
233cf 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a  Free(db, pCol->z
233d0 54 79 70 65 29 3b 0a 20 20 20 20 20 20 73 71 6c  Type);.      sql
233d1 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
233d2 43 6f 6c 2d 3e 7a 43 6f 6c 6c 29 3b 0a 20 20 20  Col->zColl);.   
233d3 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62   }.    sqlite3Db
233d4 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d  Free(db, pTable-
233d5 3e 61 43 6f 6c 29 3b 0a 20 20 7d 0a 20 20 70 54  >aCol);.  }.  pT
233d6 61 62 6c 65 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a  able->aCol = 0;.
233d7 20 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d    pTable->nCol =
233d8 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d   0;.}../*.** Rem
233d9 6f 76 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 64  ove the memory d
233da 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 61  ata structures a
233db 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
233dc 68 65 20 67 69 76 65 6e 0a 2a 2a 20 54 61 62 6c  he given.** Tabl
233dd 65 2e 20 20 4e 6f 20 63 68 61 6e 67 65 73 20 61  e.  No changes a
233de 72 65 20 6d 61 64 65 20 74 6f 20 64 69 73 6b 20  re made to disk 
233df 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  by this routine.
233e0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
233e1 69 6e 65 20 6a 75 73 74 20 64 65 6c 65 74 65 73  ine just deletes
233e2 20 74 68 65 20 64 61 74 61 20 73 74 72 75 63 74   the data struct
233e3 75 72 65 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f  ure.  It does no
233e4 74 20 75 6e 6c 69 6e 6b 0a 2a 2a 20 74 68 65 20  t unlink.** the 
233e5 74 61 62 6c 65 20 64 61 74 61 20 73 74 72 75 63  table data struc
233e6 74 75 72 65 20 66 72 6f 6d 20 74 68 65 20 68 61  ture from the ha
233e7 73 68 20 74 61 62 6c 65 2e 20 20 42 75 74 20 69  sh table.  But i
233e8 74 20 64 6f 65 73 20 64 65 73 74 72 6f 79 0a 2a  t does destroy.*
233e9 2a 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75  * memory structu
233ea 72 65 73 20 6f 66 20 74 68 65 20 69 6e 64 69 63  res of the indic
233eb 65 73 20 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b  es and foreign k
233ec 65 79 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  eys associated w
233ed 69 74 68 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c  ith .** the tabl
233ee 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
233ef 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
233f0 33 44 65 6c 65 74 65 54 61 62 6c 65 28 54 61 62  3DeleteTable(Tab
233f1 6c 65 20 2a 70 54 61 62 6c 65 29 7b 0a 20 20 49  le *pTable){.  I
233f2 6e 64 65 78 20 2a 70 49 6e 64 65 78 2c 20 2a 70  ndex *pIndex, *p
233f3 4e 65 78 74 3b 0a 20 20 46 4b 65 79 20 2a 70 46  Next;.  FKey *pF
233f4 4b 65 79 2c 20 2a 70 4e 65 78 74 46 4b 65 79 3b  Key, *pNextFKey;
233f5 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
233f6 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d 30  .  if( pTable==0
233f7 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20   ) return;.  db 
233f8 3d 20 70 54 61 62 6c 65 2d 3e 64 62 4d 65 6d 3b  = pTable->dbMem;
233f9 0a 20 20 74 65 73 74 63 61 73 65 28 20 64 62 3d  .  testcase( db=
233fa 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 44 6f 20 6e  =0 );..  /* Do n
233fb 6f 74 20 64 65 6c 65 74 65 20 74 68 65 20 74 61  ot delete the ta
233fc 62 6c 65 20 75 6e 74 69 6c 20 74 68 65 20 72 65  ble until the re
233fd 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 72 65  ference count re
233fe 61 63 68 65 73 20 7a 65 72 6f 2e 20 2a 2f 0a 20  aches zero. */. 
233ff 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66 2d 2d 3b   pTable->nRef--;
23400 0a 20 20 69 66 28 20 70 54 61 62 6c 65 2d 3e 6e  .  if( pTable->n
23401 52 65 66 3e 30 20 29 7b 0a 20 20 20 20 72 65 74  Ref>0 ){.    ret
23402 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  urn;.  }.  asser
23403 74 28 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66 3d  t( pTable->nRef=
23404 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65  =0 );..  /* Dele
23405 74 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 61  te all indices a
23406 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
23407 68 69 73 20 74 61 62 6c 65 0a 20 20 2a 2f 0a 20  his table.  */. 
23408 20 66 6f 72 28 70 49 6e 64 65 78 20 3d 20 70 54   for(pIndex = pT
23409 61 62 6c 65 2d 3e 70 49 6e 64 65 78 3b 20 70 49  able->pIndex; pI
2340a 6e 64 65 78 3b 20 70 49 6e 64 65 78 3d 70 4e 65  ndex; pIndex=pNe
2340b 78 74 29 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d  xt){.    pNext =
2340c 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a   pIndex->pNext;.
2340d 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 64      assert( pInd
2340e 65 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61  ex->pSchema==pTa
2340f 62 6c 65 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a  ble->pSchema );.
23410 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
23411 65 49 6e 64 65 78 28 70 49 6e 64 65 78 29 3b 0a  eIndex(pIndex);.
23412 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
23413 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e  ITE_OMIT_FOREIGN
23414 5f 4b 45 59 0a 20 20 2f 2a 20 44 65 6c 65 74 65  _KEY.  /* Delete
23415 20 61 6c 6c 20 66 6f 72 65 69 67 6e 20 6b 65 79   all foreign key
23416 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
23417 68 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f  h this table. */
23418 0a 20 20 66 6f 72 28 70 46 4b 65 79 3d 70 54 61  .  for(pFKey=pTa
23419 62 6c 65 2d 3e 70 46 4b 65 79 3b 20 70 46 4b 65  ble->pFKey; pFKe
2341a 79 3b 20 70 46 4b 65 79 3d 70 4e 65 78 74 46 4b  y; pFKey=pNextFK
2341b 65 79 29 7b 0a 20 20 20 20 70 4e 65 78 74 46 4b  ey){.    pNextFK
2341c 65 79 20 3d 20 70 46 4b 65 79 2d 3e 70 4e 65 78  ey = pFKey->pNex
2341d 74 46 72 6f 6d 3b 0a 20 20 20 20 73 71 6c 69 74  tFrom;.    sqlit
2341e 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 46 4b  e3DbFree(db, pFK
2341f 65 79 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ey);.  }.#endif.
23420 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65  .  /* Delete the
23421 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   Table structure
23422 20 69 74 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20   itself..  */.  
23423 73 71 6c 69 74 65 52 65 73 65 74 43 6f 6c 75 6d  sqliteResetColum
23424 6e 4e 61 6d 65 73 28 70 54 61 62 6c 65 29 3b 0a  nNames(pTable);.
23425 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
23426 64 62 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d  db, pTable->zNam
23427 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  e);.  sqlite3DbF
23428 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e  ree(db, pTable->
23429 7a 43 6f 6c 41 66 66 29 3b 0a 20 20 73 71 6c 69  zColAff);.  sqli
2342a 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
2342b 64 62 2c 20 70 54 61 62 6c 65 2d 3e 70 53 65 6c  db, pTable->pSel
2342c 65 63 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  ect);.#ifndef SQ
2342d 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a  LITE_OMIT_CHECK.
2342e 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
2342f 65 74 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e  ete(db, pTable->
23430 70 43 68 65 63 6b 29 3b 0a 23 65 6e 64 69 66 0a  pCheck);.#endif.
23431 20 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65    sqlite3VtabCle
23432 61 72 28 70 54 61 62 6c 65 29 3b 0a 20 20 73 71  ar(pTable);.  sq
23433 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
23434 70 54 61 62 6c 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  pTable);.}../*.*
23435 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 67 69 76  * Unlink the giv
23436 65 6e 20 74 61 62 6c 65 20 66 72 6f 6d 20 74 68  en table from th
23437 65 20 68 61 73 68 20 74 61 62 6c 65 73 20 61 6e  e hash tables an
23438 64 20 74 68 65 20 64 65 6c 65 74 65 20 74 68 65  d the delete the
23439 0a 2a 2a 20 74 61 62 6c 65 20 73 74 72 75 63 74  .** table struct
2343a 75 72 65 20 77 69 74 68 20 61 6c 6c 20 69 74 73  ure with all its
2343b 20 69 6e 64 69 63 65 73 20 61 6e 64 20 66 6f 72   indices and for
2343c 65 69 67 6e 20 6b 65 79 73 2e 0a 2a 2f 0a 53 51  eign keys..*/.SQ
2343d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2343e 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41  d sqlite3UnlinkA
2343f 6e 64 44 65 6c 65 74 65 54 61 62 6c 65 28 73 71  ndDeleteTable(sq
23440 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69  lite3 *db, int i
23441 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  Db, const char *
23442 7a 54 61 62 4e 61 6d 65 29 7b 0a 20 20 54 61 62  zTabName){.  Tab
23443 6c 65 20 2a 70 3b 0a 20 20 44 62 20 2a 70 44 62  le *p;.  Db *pDb
23444 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 21  ;..  assert( db!
23445 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
23446 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62  iDb>=0 && iDb<db
23447 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72  ->nDb );.  asser
23448 74 28 20 7a 54 61 62 4e 61 6d 65 20 26 26 20 7a  t( zTabName && z
23449 54 61 62 4e 61 6d 65 5b 30 5d 20 29 3b 0a 20 20  TabName[0] );.  
2344a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69  pDb = &db->aDb[i
2344b 44 62 5d 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74  Db];.  p = sqlit
2344c 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 44  e3HashInsert(&pD
2344d 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48  b->pSchema->tblH
2344e 61 73 68 2c 20 7a 54 61 62 4e 61 6d 65 2c 0a 20  ash, zTabName,. 
2344f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23450 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74         sqlite3St
23451 72 6c 65 6e 33 30 28 7a 54 61 62 4e 61 6d 65 29  rlen30(zTabName)
23452 2c 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65  ,0);.  sqlite3De
23453 6c 65 74 65 54 61 62 6c 65 28 70 29 3b 0a 20 20  leteTable(p);.  
23454 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c  db->flags |= SQL
23455 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65  ITE_InternChange
23456 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65  s;.}../*.** Give
23457 6e 20 61 20 74 6f 6b 65 6e 2c 20 72 65 74 75 72  n a token, retur
23458 6e 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20  n a string that 
23459 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 65 20  consists of the 
2345a 74 65 78 74 20 6f 66 20 74 68 61 74 0a 2a 2a 20  text of that.** 
2345b 74 6f 6b 65 6e 2e 20 20 53 70 61 63 65 20 74 6f  token.  Space to
2345c 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e   hold the return
2345d 65 64 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20  ed string.** is 
2345e 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
2345f 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64  liteMalloc() and
23460 20 6d 75 73 74 20 62 65 20 66 72 65 65 64 20 62   must be freed b
23461 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a  y the calling.**
23462 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
23463 20 41 6e 79 20 71 75 6f 74 61 74 69 6f 6e 20 6d   Any quotation m
23464 61 72 6b 73 20 28 65 78 3a 20 20 22 6e 61 6d 65  arks (ex:  "name
23465 22 2c 20 27 6e 61 6d 65 27 2c 20 5b 6e 61 6d 65  ", 'name', [name
23466 5d 2c 20 6f 72 20 60 6e 61 6d 65 60 29 20 74 68  ], or `name`) th
23467 61 74 0a 2a 2a 20 73 75 72 72 6f 75 6e 64 20 74  at.** surround t
23468 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 74  he body of the t
23469 6f 6b 65 6e 20 61 72 65 20 72 65 6d 6f 76 65 64  oken are removed
2346a 2e 0a 2a 2a 0a 2a 2a 20 54 6f 6b 65 6e 73 20 61  ..**.** Tokens a
2346b 72 65 20 6f 66 74 65 6e 20 6a 75 73 74 20 70 6f  re often just po
2346c 69 6e 74 65 72 73 20 69 6e 74 6f 20 74 68 65 20  inters into the 
2346d 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 65 78  original SQL tex
2346e 74 20 61 6e 64 20 73 6f 0a 2a 2a 20 61 72 65 20  t and so.** are 
2346f 6e 6f 74 20 5c 30 30 30 20 74 65 72 6d 69 6e 61  not \000 termina
23470 74 65 64 20 61 6e 64 20 61 72 65 20 6e 6f 74 20  ted and are not 
23471 70 65 72 73 69 73 74 65 6e 74 2e 20 20 54 68 65  persistent.  The
23472 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67   returned string
23473 0a 2a 2a 20 69 73 20 5c 30 30 30 20 74 65 72 6d  .** is \000 term
23474 69 6e 61 74 65 64 20 61 6e 64 20 69 73 20 70 65  inated and is pe
23475 72 73 69 73 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c  rsistent..*/.SQL
23476 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
23477 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f   *sqlite3NameFro
23478 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33 20 2a  mToken(sqlite3 *
23479 64 62 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  db, Token *pName
2347a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  ){.  char *zName
2347b 3b 0a 20 20 69 66 28 20 70 4e 61 6d 65 20 29 7b  ;.  if( pName ){
2347c 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c  .    zName = sql
2347d 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62  ite3DbStrNDup(db
2347e 2c 20 28 63 68 61 72 2a 29 70 4e 61 6d 65 2d 3e  , (char*)pName->
2347f 7a 2c 20 70 4e 61 6d 65 2d 3e 6e 29 3b 0a 20 20  z, pName->n);.  
23480 20 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65    sqlite3Dequote
23481 28 7a 4e 61 6d 65 29 3b 0a 20 20 7d 65 6c 73 65  (zName);.  }else
23482 7b 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 30 3b  {.    zName = 0;
23483 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e  .  }.  return zN
23484 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70  ame;.}../*.** Op
23485 65 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  en the sqlite_ma
23486 73 74 65 72 20 74 61 62 6c 65 20 73 74 6f 72 65  ster table store
23487 64 20 69 6e 20 64 61 74 61 62 61 73 65 20 6e 75  d in database nu
23488 6d 62 65 72 20 69 44 62 20 66 6f 72 0a 2a 2a 20  mber iDb for.** 
23489 77 72 69 74 69 6e 67 2e 20 54 68 65 20 74 61 62  writing. The tab
2348a 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 75 73 69  le is opened usi
2348b 6e 67 20 63 75 72 73 6f 72 20 30 2e 0a 2a 2f 0a  ng cursor 0..*/.
2348c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2348d 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d  oid sqlite3OpenM
2348e 61 73 74 65 72 54 61 62 6c 65 28 50 61 72 73 65  asterTable(Parse
2348f 20 2a 70 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20   *p, int iDb){. 
23490 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74   Vdbe *v = sqlit
23491 65 33 47 65 74 56 64 62 65 28 70 29 3b 0a 20 20  e3GetVdbe(p);.  
23492 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b  sqlite3TableLock
23493 28 70 2c 20 69 44 62 2c 20 4d 41 53 54 45 52 5f  (p, iDb, MASTER_
23494 52 4f 4f 54 2c 20 31 2c 20 53 43 48 45 4d 41 5f  ROOT, 1, SCHEMA_
23495 54 41 42 4c 45 28 69 44 62 29 29 3b 0a 20 20 73  TABLE(iDb));.  s
23496 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
23497 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65  (v, OP_OpenWrite
23498 2c 20 30 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54  , 0, MASTER_ROOT
23499 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c 69 74 65  , iDb);.  sqlite
2349a 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
2349b 20 2d 31 2c 20 28 63 68 61 72 20 2a 29 35 2c 20   -1, (char *)5, 
2349c 50 34 5f 49 4e 54 33 32 29 3b 20 20 2f 2a 20 35  P4_INT32);  /* 5
2349d 20 63 6f 6c 75 6d 6e 20 74 61 62 6c 65 20 2a 2f   column table */
2349e 0a 20 20 69 66 28 20 70 2d 3e 6e 54 61 62 3d 3d  .  if( p->nTab==
2349f 30 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 54 61 62  0 ){.    p->nTab
234a0 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   = 1;.  }.}../*.
234a1 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a 4e 61  ** Parameter zNa
234a2 6d 65 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 6e  me points to a n
234a3 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 62 75  ul-terminated bu
234a4 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ffer containing 
234a5 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 61  the name.** of a
234a6 20 64 61 74 61 62 61 73 65 20 28 22 6d 61 69 6e   database ("main
234a7 22 2c 20 22 74 65 6d 70 22 20 6f 72 20 74 68 65  ", "temp" or the
234a8 20 6e 61 6d 65 20 6f 66 20 61 6e 20 61 74 74 61   name of an atta
234a9 63 68 65 64 20 64 62 29 2e 20 54 68 69 73 0a 2a  ched db). This.*
234aa 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  * function retur
234ab 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  ns the index of 
234ac 74 68 65 20 6e 61 6d 65 64 20 64 61 74 61 62 61  the named databa
234ad 73 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 2c  se in db->aDb[],
234ae 20 6f 72 0a 2a 2a 20 2d 31 20 69 66 20 74 68 65   or.** -1 if the
234af 20 6e 61 6d 65 64 20 64 62 20 63 61 6e 6e 6f 74   named db cannot
234b0 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 53 51   be found..*/.SQ
234b1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
234b2 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 4e 61   sqlite3FindDbNa
234b3 6d 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  me(sqlite3 *db, 
234b4 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
234b5 65 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 2d 31  e){.  int i = -1
234b6 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74  ;         /* Dat
234b7 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a  abase number */.
234b8 20 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0a 20    if( zName ){. 
234b9 20 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 20 20     Db *pDb;.    
234ba 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53  int n = sqlite3S
234bb 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a  trlen30(zName);.
234bc 20 20 20 20 66 6f 72 28 69 3d 28 64 62 2d 3e 6e      for(i=(db->n
234bd 44 62 2d 31 29 2c 20 70 44 62 3d 26 64 62 2d 3e  Db-1), pDb=&db->
234be 61 44 62 5b 69 5d 3b 20 69 3e 3d 30 3b 20 69 2d  aDb[i]; i>=0; i-
234bf 2d 2c 20 70 44 62 2d 2d 29 7b 0a 20 20 20 20 20  -, pDb--){.     
234c0 20 69 66 28 20 28 21 4f 4d 49 54 5f 54 45 4d 50   if( (!OMIT_TEMP
234c1 44 42 20 7c 7c 20 69 21 3d 31 20 29 20 26 26 20  DB || i!=1 ) && 
234c2 6e 3d 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  n==sqlite3Strlen
234c3 33 30 28 70 44 62 2d 3e 7a 4e 61 6d 65 29 20 26  30(pDb->zName) &
234c4 26 20 0a 20 20 20 20 20 20 20 20 20 20 30 3d 3d  & .          0==
234c5 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
234c6 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65  Db->zName, zName
234c7 29 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65  ) ){.        bre
234c8 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
234c9 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69  }.  }.  return i
234ca 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74  ;.}../*.** The t
234cb 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 63 6f 6e 74  oken *pName cont
234cc 61 69 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  ains the name of
234cd 20 61 20 64 61 74 61 62 61 73 65 20 28 65 69 74   a database (eit
234ce 68 65 72 20 22 6d 61 69 6e 22 20 6f 72 0a 2a 2a  her "main" or.**
234cf 20 22 74 65 6d 70 22 20 6f 72 20 74 68 65 20 6e   "temp" or the n
234d0 61 6d 65 20 6f 66 20 61 6e 20 61 74 74 61 63 68  ame of an attach
234d1 65 64 20 64 62 29 2e 20 54 68 69 73 20 72 6f 75  ed db). This rou
234d2 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65  tine returns the
234d3 0a 2a 2a 20 69 6e 64 65 78 20 6f 66 20 74 68 65  .** index of the
234d4 20 6e 61 6d 65 64 20 64 61 74 61 62 61 73 65 20   named database 
234d5 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 2c 20 6f 72  in db->aDb[], or
234d6 20 2d 31 20 69 66 20 74 68 65 20 6e 61 6d 65 64   -1 if the named
234d7 20 64 62 20 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74   db .** does not
234d8 20 65 78 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54   exist..*/.SQLIT
234d9 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
234da 6c 69 74 65 33 46 69 6e 64 44 62 28 73 71 6c 69  lite3FindDb(sqli
234db 74 65 33 20 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a  te3 *db, Token *
234dc 70 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 69 3b  pName){.  int i;
234dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
234de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
234df 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d 62 65  * Database numbe
234e0 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61  r */.  char *zNa
234e1 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  me;             
234e2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
234e3 61 6d 65 20 77 65 20 61 72 65 20 73 65 61 72 63  ame we are searc
234e4 68 69 6e 67 20 66 6f 72 20 2a 2f 0a 20 20 7a 4e  hing for */.  zN
234e5 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  ame = sqlite3Nam
234e6 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70  eFromToken(db, p
234e7 4e 61 6d 65 29 3b 0a 20 20 69 20 3d 20 73 71 6c  Name);.  i = sql
234e8 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 28 64  ite3FindDbName(d
234e9 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c  b, zName);.  sql
234ea 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
234eb 4e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20  Name);.  return 
234ec 69 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 74 61 62  i;.}../* The tab
234ed 6c 65 20 6f 72 20 76 69 65 77 20 6f 72 20 74 72  le or view or tr
234ee 69 67 67 65 72 20 6e 61 6d 65 20 69 73 20 70 61  igger name is pa
234ef 73 73 65 64 20 74 6f 20 74 68 69 73 20 72 6f 75  ssed to this rou
234f0 74 69 6e 65 20 76 69 61 20 74 6f 6b 65 6e 73 0a  tine via tokens.
234f1 2a 2a 20 70 4e 61 6d 65 31 20 61 6e 64 20 70 4e  ** pName1 and pN
234f2 61 6d 65 32 2e 20 49 66 20 74 68 65 20 74 61 62  ame2. If the tab
234f3 6c 65 20 6e 61 6d 65 20 77 61 73 20 66 75 6c 6c  le name was full
234f4 79 20 71 75 61 6c 69 66 69 65 64 2c 20 66 6f 72  y qualified, for
234f5 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20   example:.**.** 
234f6 43 52 45 41 54 45 20 54 41 42 4c 45 20 78 78 78  CREATE TABLE xxx
234f7 2e 79 79 79 20 28 2e 2e 2e 29 3b 0a 2a 2a 20 0a  .yyy (...);.** .
234f8 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20 69  ** Then pName1 i
234f9 73 20 73 65 74 20 74 6f 20 22 78 78 78 22 20 61  s set to "xxx" a
234fa 6e 64 20 70 4e 61 6d 65 32 20 22 79 79 79 22 2e  nd pName2 "yyy".
234fb 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61   On the other ha
234fc 6e 64 20 69 66 0a 2a 2a 20 74 68 65 20 74 61 62  nd if.** the tab
234fd 6c 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 66  le name is not f
234fe 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 2c 20  ully qualified, 
234ff 69 2e 65 2e 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41  i.e.:.**.** CREA
23500 54 45 20 54 41 42 4c 45 20 79 79 79 28 2e 2e 2e  TE TABLE yyy(...
23501 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 70 4e  );.**.** Then pN
23502 61 6d 65 31 20 69 73 20 73 65 74 20 74 6f 20 22  ame1 is set to "
23503 79 79 79 22 20 61 6e 64 20 70 4e 61 6d 65 32 20  yyy" and pName2 
23504 69 73 20 22 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  is ""..**.** Thi
23505 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74  s routine sets t
23506 68 65 20 2a 70 70 55 6e 71 75 61 6c 20 70 6f 69  he *ppUnqual poi
23507 6e 74 65 72 20 74 6f 20 70 6f 69 6e 74 20 61 74  nter to point at
23508 20 74 68 65 20 74 6f 6b 65 6e 20 28 70 4e 61 6d   the token (pNam
23509 65 31 20 6f 72 0a 2a 2a 20 70 4e 61 6d 65 32 29  e1 or.** pName2)
2350a 20 74 68 61 74 20 73 74 6f 72 65 73 20 74 68 65   that stores the
2350b 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74 61 62   unqualified tab
2350c 6c 65 20 6e 61 6d 65 2e 20 20 54 68 65 20 69 6e  le name.  The in
2350d 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61  dex of the.** da
2350e 74 61 62 61 73 65 20 22 78 78 78 22 20 69 73 20  tabase "xxx" is 
2350f 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c  returned..*/.SQL
23510 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
23511 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61  sqlite3TwoPartNa
23512 6d 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  me(.  Parse *pPa
23513 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72  rse,      /* Par
23514 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65  sing and code ge
23515 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74  nerating context
23516 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
23517 6d 65 31 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  me1,      /* The
23518 20 22 78 78 78 22 20 69 6e 20 74 68 65 20 6e 61   "xxx" in the na
23519 6d 65 20 22 78 78 78 2e 79 79 79 22 20 6f 72 20  me "xxx.yyy" or 
2351a 22 78 78 78 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e  "xxx" */.  Token
2351b 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 20 2f   *pName2,      /
2351c 2a 20 54 68 65 20 22 79 79 79 22 20 69 6e 20 74  * The "yyy" in t
2351d 68 65 20 6e 61 6d 65 20 22 78 78 78 2e 79 79 79  he name "xxx.yyy
2351e 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 2a 70  " */.  Token **p
2351f 55 6e 71 75 61 6c 20 20 20 20 20 2f 2a 20 57 72  Unqual     /* Wr
23520 69 74 65 20 74 68 65 20 75 6e 71 75 61 6c 69 66  ite the unqualif
23521 69 65 64 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20  ied object name 
23522 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  here */.){.  int
23523 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   iDb;           
23524 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
23525 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 68 65  base holding the
23526 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73 71 6c   object */.  sql
23527 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
23528 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 41 4c  e->db;..  if( AL
23529 57 41 59 53 28 70 4e 61 6d 65 32 21 3d 30 29 20  WAYS(pName2!=0) 
2352a 26 26 20 70 4e 61 6d 65 32 2d 3e 6e 3e 30 20 29  && pName2->n>0 )
2352b 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 69 6e  {.    if( db->in
2352c 69 74 2e 62 75 73 79 20 29 20 7b 0a 20 20 20 20  it.busy ) {.    
2352d 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2352e 67 28 70 50 61 72 73 65 2c 20 22 63 6f 72 72 75  g(pParse, "corru
2352f 70 74 20 64 61 74 61 62 61 73 65 22 29 3b 0a 20  pt database");. 
23530 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72       pParse->nEr
23531 72 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72  r++;.      retur
23532 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n -1;.    }.    
23533 2a 70 55 6e 71 75 61 6c 20 3d 20 70 4e 61 6d 65  *pUnqual = pName
23534 32 3b 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c  2;.    iDb = sql
23535 69 74 65 33 46 69 6e 64 44 62 28 64 62 2c 20 70  ite3FindDb(db, p
23536 4e 61 6d 65 31 29 3b 0a 20 20 20 20 69 66 28 20  Name1);.    if( 
23537 69 44 62 3c 30 20 29 7b 0a 20 20 20 20 20 20 73  iDb<0 ){.      s
23538 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
23539 50 61 72 73 65 2c 20 22 75 6e 6b 6e 6f 77 6e 20  Parse, "unknown 
2353a 64 61 74 61 62 61 73 65 20 25 54 22 2c 20 70 4e  database %T", pN
2353b 61 6d 65 31 29 3b 0a 20 20 20 20 20 20 70 50 61  ame1);.      pPa
2353c 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20  rse->nErr++;.   
2353d 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20     return -1;.  
2353e 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
2353f 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69   assert( db->ini
23540 74 2e 69 44 62 3d 3d 30 20 7c 7c 20 64 62 2d 3e  t.iDb==0 || db->
23541 69 6e 69 74 2e 62 75 73 79 20 29 3b 0a 20 20 20  init.busy );.   
23542 20 69 44 62 20 3d 20 64 62 2d 3e 69 6e 69 74 2e   iDb = db->init.
23543 69 44 62 3b 0a 20 20 20 20 2a 70 55 6e 71 75 61  iDb;.    *pUnqua
23544 6c 20 3d 20 70 4e 61 6d 65 31 3b 0a 20 20 7d 0a  l = pName1;.  }.
23545 20 20 72 65 74 75 72 6e 20 69 44 62 3b 0a 7d 0a    return iDb;.}.
23546 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
23547 69 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20 63  ine is used to c
23548 68 65 63 6b 20 69 66 20 74 68 65 20 55 54 46 2d  heck if the UTF-
23549 38 20 73 74 72 69 6e 67 20 7a 4e 61 6d 65 20 69  8 string zName i
2354a 73 20 61 20 6c 65 67 61 6c 0a 2a 2a 20 75 6e 71  s a legal.** unq
2354b 75 61 6c 69 66 69 65 64 20 6e 61 6d 65 20 66 6f  ualified name fo
2354c 72 20 61 20 6e 65 77 20 73 63 68 65 6d 61 20 6f  r a new schema o
2354d 62 6a 65 63 74 20 28 74 61 62 6c 65 2c 20 69 6e  bject (table, in
2354e 64 65 78 2c 20 76 69 65 77 20 6f 72 0a 2a 2a 20  dex, view or.** 
2354f 74 72 69 67 67 65 72 29 2e 20 41 6c 6c 20 6e 61  trigger). All na
23550 6d 65 73 20 61 72 65 20 6c 65 67 61 6c 20 65 78  mes are legal ex
23551 63 65 70 74 20 74 68 6f 73 65 20 74 68 61 74 20  cept those that 
23552 62 65 67 69 6e 20 77 69 74 68 20 74 68 65 20 73  begin with the s
23553 74 72 69 6e 67 0a 2a 2a 20 22 73 71 6c 69 74 65  tring.** "sqlite
23554 5f 22 20 28 69 6e 20 75 70 70 65 72 2c 20 6c 6f  _" (in upper, lo
23555 77 65 72 20 6f 72 20 6d 69 78 65 64 20 63 61 73  wer or mixed cas
23556 65 29 2e 20 54 68 69 73 20 70 6f 72 74 69 6f 6e  e). This portion
23557 20 6f 66 20 74 68 65 20 6e 61 6d 65 73 70 61 63   of the namespac
23558 65 0a 2a 2a 20 69 73 20 72 65 73 65 72 76 65 64  e.** is reserved
23559 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73   for internal us
2355a 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
2355b 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
2355c 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28  CheckObjectName(
2355d 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63  Parse *pParse, c
2355e 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2355f 29 7b 0a 20 20 69 66 28 20 21 70 50 61 72 73 65  ){.  if( !pParse
23560 2d 3e 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20  ->db->init.busy 
23561 26 26 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  && pParse->neste
23562 64 3d 3d 30 20 0a 20 20 20 20 20 20 20 20 20 20  d==0 .          
23563 26 26 20 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e  && (pParse->db->
23564 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 57  flags & SQLITE_W
23565 72 69 74 65 53 63 68 65 6d 61 29 3d 3d 30 0a 20  riteSchema)==0. 
23566 20 20 20 20 20 20 20 20 20 26 26 20 30 3d 3d 73           && 0==s
23567 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a  qlite3StrNICmp(z
23568 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c  Name, "sqlite_",
23569 20 37 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74   7) ){.    sqlit
2356a 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2356b 65 2c 20 22 6f 62 6a 65 63 74 20 6e 61 6d 65 20  e, "object name 
2356c 72 65 73 65 72 76 65 64 20 66 6f 72 20 69 6e 74  reserved for int
2356d 65 72 6e 61 6c 20 75 73 65 3a 20 25 73 22 2c 20  ernal use: %s", 
2356e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75  zName);.    retu
2356f 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
23570 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
23571 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
23572 2a 20 42 65 67 69 6e 20 63 6f 6e 73 74 72 75 63  * Begin construc
23573 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62 6c 65  ting a new table
23574 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
23575 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 54 68 69 73  in memory.  This
23576 20 69 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74   is.** the first
23577 20 6f 66 20 73 65 76 65 72 61 6c 20 61 63 74 69   of several acti
23578 6f 6e 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74  on routines that
23579 20 67 65 74 20 63 61 6c 6c 65 64 20 69 6e 20 72   get called in r
2357a 65 73 70 6f 6e 73 65 0a 2a 2a 20 74 6f 20 61 20  esponse.** to a 
2357b 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
2357c 74 65 6d 65 6e 74 2e 20 20 49 6e 20 70 61 72 74  tement.  In part
2357d 69 63 75 6c 61 72 2c 20 74 68 69 73 20 72 6f 75  icular, this rou
2357e 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 0a 2a  tine is called.*
2357f 2a 20 61 66 74 65 72 20 73 65 65 69 6e 67 20 74  * after seeing t
23580 6f 6b 65 6e 73 20 22 43 52 45 41 54 45 22 20 61  okens "CREATE" a
23581 6e 64 20 22 54 41 42 4c 45 22 20 61 6e 64 20 74  nd "TABLE" and t
23582 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54  he table name. T
23583 68 65 20 69 73 54 65 6d 70 0a 2a 2a 20 66 6c 61  he isTemp.** fla
23584 67 20 69 73 20 74 72 75 65 20 69 66 20 74 68 65  g is true if the
23585 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 20 62 65   table should be
23586 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 61   stored in the a
23587 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73  uxiliary databas
23588 65 0a 2a 2a 20 66 69 6c 65 20 69 6e 73 74 65 61  e.** file instea
23589 64 20 6f 66 20 69 6e 20 74 68 65 20 6d 61 69 6e  d of in the main
2358a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
2358b 20 54 68 69 73 20 69 73 20 6e 6f 72 6d 61 6c 6c   This is normall
2358c 79 20 74 68 65 20 63 61 73 65 0a 2a 2a 20 77 68  y the case.** wh
2358d 65 6e 20 74 68 65 20 22 54 45 4d 50 22 20 6f 72  en the "TEMP" or
2358e 20 22 54 45 4d 50 4f 52 41 52 59 22 20 6b 65 79   "TEMPORARY" key
2358f 77 6f 72 64 20 6f 63 63 75 72 73 20 69 6e 20 62  word occurs in b
23590 65 74 77 65 65 6e 0a 2a 2a 20 43 52 45 41 54 45  etween.** CREATE
23591 20 61 6e 64 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a   and TABLE..**.*
23592 2a 20 54 68 65 20 6e 65 77 20 74 61 62 6c 65 20  * The new table 
23593 72 65 63 6f 72 64 20 69 73 20 69 6e 69 74 69 61  record is initia
23594 6c 69 7a 65 64 20 61 6e 64 20 70 75 74 20 69 6e  lized and put in
23595 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
23596 6c 65 2e 0a 2a 2a 20 41 73 20 6d 6f 72 65 20 6f  le..** As more o
23597 66 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42  f the CREATE TAB
23598 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  LE statement is 
23599 70 61 72 73 65 64 2c 20 61 64 64 69 74 69 6f 6e  parsed, addition
2359a 61 6c 20 61 63 74 69 6f 6e 0a 2a 2a 20 72 6f 75  al action.** rou
2359b 74 69 6e 65 73 20 77 69 6c 6c 20 62 65 20 63 61  tines will be ca
2359c 6c 6c 65 64 20 74 6f 20 61 64 64 20 6d 6f 72 65  lled to add more
2359d 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20   information to 
2359e 74 68 69 73 20 72 65 63 6f 72 64 2e 0a 2a 2a 20  this record..** 
2359f 41 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  At the end of th
235a0 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  e CREATE TABLE s
235a1 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 20 73 71  tatement, the sq
235a2 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 29 20  lite3EndTable() 
235a3 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61  routine.** is ca
235a4 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65  lled to complete
235a5 20 74 68 65 20 63 6f 6e 73 74 72 75 63 74 69 6f   the constructio
235a6 6e 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62  n of the new tab
235a7 6c 65 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 53 51  le record..*/.SQ
235a8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
235a9 64 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 61  d sqlite3StartTa
235aa 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ble(.  Parse *pP
235ab 61 72 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 65  arse,   /* Parse
235ac 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  r context */.  T
235ad 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20  oken *pName1,   
235ae 2f 2a 20 46 69 72 73 74 20 70 61 72 74 20 6f 66  /* First part of
235af 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
235b0 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 2a   table or view *
235b1 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  /.  Token *pName
235b2 32 2c 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70  2,   /* Second p
235b3 61 72 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 20  art of the name 
235b4 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20  of the table or 
235b5 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 69 73  view */.  int is
235b6 54 65 6d 70 2c 20 20 20 20 20 20 2f 2a 20 54 72  Temp,      /* Tr
235b7 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20  ue if this is a 
235b8 54 45 4d 50 20 74 61 62 6c 65 20 2a 2f 0a 20 20  TEMP table */.  
235b9 69 6e 74 20 69 73 56 69 65 77 2c 20 20 20 20 20  int isView,     
235ba 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73   /* True if this
235bb 20 69 73 20 61 20 56 49 45 57 20 2a 2f 0a 20 20   is a VIEW */.  
235bc 69 6e 74 20 69 73 56 69 72 74 75 61 6c 2c 20 20  int isVirtual,  
235bd 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73   /* True if this
235be 20 69 73 20 61 20 56 49 52 54 55 41 4c 20 74 61   is a VIRTUAL ta
235bf 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45  ble */.  int noE
235c0 72 72 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20  rr        /* Do 
235c1 6e 6f 74 68 69 6e 67 20 69 66 20 74 61 62 6c 65  nothing if table
235c2 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20   already exists 
235c3 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  */.){.  Table *p
235c4 54 61 62 6c 65 3b 0a 20 20 63 68 61 72 20 2a 7a  Table;.  char *z
235c5 4e 61 6d 65 20 3d 20 30 3b 20 2f 2a 20 54 68 65  Name = 0; /* The
235c6 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6e 65 77   name of the new
235c7 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71 6c 69   table */.  sqli
235c8 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
235c9 2d 3e 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b  ->db;.  Vdbe *v;
235ca 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20  .  int iDb;     
235cb 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
235cc 6e 75 6d 62 65 72 20 74 6f 20 63 72 65 61 74 65  number to create
235cd 20 74 68 65 20 74 61 62 6c 65 20 69 6e 20 2a 2f   the table in */
235ce 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b  .  Token *pName;
235cf 20 20 20 20 2f 2a 20 55 6e 71 75 61 6c 69 66 69      /* Unqualifi
235d0 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  ed name of the t
235d1 61 62 6c 65 20 74 6f 20 63 72 65 61 74 65 20 2a  able to create *
235d2 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 74 61 62 6c  /..  /* The tabl
235d3 65 20 6f 72 20 76 69 65 77 20 6e 61 6d 65 20 74  e or view name t
235d4 6f 20 63 72 65 61 74 65 20 69 73 20 70 61 73 73  o create is pass
235d5 65 64 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69  ed to this routi
235d6 6e 65 20 76 69 61 20 74 6f 6b 65 6e 73 0a 20 20  ne via tokens.  
235d7 2a 2a 20 70 4e 61 6d 65 31 20 61 6e 64 20 70 4e  ** pName1 and pN
235d8 61 6d 65 32 2e 20 49 66 20 74 68 65 20 74 61 62  ame2. If the tab
235d9 6c 65 20 6e 61 6d 65 20 77 61 73 20 66 75 6c 6c  le name was full
235da 79 20 71 75 61 6c 69 66 69 65 64 2c 20 66 6f 72  y qualified, for
235db 20 65 78 61 6d 70 6c 65 3a 0a 20 20 2a 2a 0a 20   example:.  **. 
235dc 20 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45   ** CREATE TABLE
235dd 20 78 78 78 2e 79 79 79 20 28 2e 2e 2e 29 3b 0a   xxx.yyy (...);.
235de 20 20 2a 2a 20 0a 20 20 2a 2a 20 54 68 65 6e 20    ** .  ** Then 
235df 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20 74 6f  pName1 is set to
235e0 20 22 78 78 78 22 20 61 6e 64 20 70 4e 61 6d 65   "xxx" and pName
235e1 32 20 22 79 79 79 22 2e 20 4f 6e 20 74 68 65 20  2 "yyy". On the 
235e2 6f 74 68 65 72 20 68 61 6e 64 20 69 66 0a 20 20  other hand if.  
235e3 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d  ** the table nam
235e4 65 20 69 73 20 6e 6f 74 20 66 75 6c 6c 79 20 71  e is not fully q
235e5 75 61 6c 69 66 69 65 64 2c 20 69 2e 65 2e 3a 0a  ualified, i.e.:.
235e6 20 20 2a 2a 0a 20 20 2a 2a 20 43 52 45 41 54 45    **.  ** CREATE
235e7 20 54 41 42 4c 45 20 79 79 79 28 2e 2e 2e 29 3b   TABLE yyy(...);
235e8 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e 20  .  **.  ** Then 
235e9 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20 74 6f  pName1 is set to
235ea 20 22 79 79 79 22 20 61 6e 64 20 70 4e 61 6d 65   "yyy" and pName
235eb 32 20 69 73 20 22 22 2e 0a 20 20 2a 2a 0a 20 20  2 is ""..  **.  
235ec 2a 2a 20 54 68 65 20 63 61 6c 6c 20 62 65 6c 6f  ** The call belo
235ed 77 20 73 65 74 73 20 74 68 65 20 70 4e 61 6d 65  w sets the pName
235ee 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 6f 69 6e   pointer to poin
235ef 74 20 61 74 20 74 68 65 20 74 6f 6b 65 6e 20 28  t at the token (
235f0 70 4e 61 6d 65 31 20 6f 72 0a 20 20 2a 2a 20 70  pName1 or.  ** p
235f1 4e 61 6d 65 32 29 20 74 68 61 74 20 73 74 6f 72  Name2) that stor
235f2 65 73 20 74 68 65 20 75 6e 71 75 61 6c 69 66 69  es the unqualifi
235f3 65 64 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54  ed table name. T
235f4 68 65 20 76 61 72 69 61 62 6c 65 20 69 44 62 20  he variable iDb 
235f5 69 73 0a 20 20 2a 2a 20 73 65 74 20 74 6f 20 74  is.  ** set to t
235f6 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
235f7 64 61 74 61 62 61 73 65 20 74 68 61 74 20 74 68  database that th
235f8 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20  e table or view 
235f9 69 73 20 74 6f 20 62 65 0a 20 20 2a 2a 20 63 72  is to be.  ** cr
235fa 65 61 74 65 64 20 69 6e 2e 0a 20 20 2a 2f 0a 20  eated in..  */. 
235fb 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77   iDb = sqlite3Tw
235fc 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65  oPartName(pParse
235fd 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32  , pName1, pName2
235fe 2c 20 26 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28  , &pName);.  if(
235ff 20 69 44 62 3c 30 20 29 20 72 65 74 75 72 6e 3b   iDb<0 ) return;
23600 0a 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d  .  if( !OMIT_TEM
23601 50 44 42 20 26 26 20 69 73 54 65 6d 70 20 26 26  PDB && isTemp &&
23602 20 69 44 62 3e 31 20 29 7b 0a 20 20 20 20 2f 2a   iDb>1 ){.    /*
23603 20 49 66 20 63 72 65 61 74 69 6e 67 20 61 20 74   If creating a t
23604 65 6d 70 20 74 61 62 6c 65 2c 20 74 68 65 20 6e  emp table, the n
23605 61 6d 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 71  ame may not be q
23606 75 61 6c 69 66 69 65 64 20 2a 2f 0a 20 20 20 20  ualified */.    
23607 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
23608 70 50 61 72 73 65 2c 20 22 74 65 6d 70 6f 72 61  pParse, "tempora
23609 72 79 20 74 61 62 6c 65 20 6e 61 6d 65 20 6d 75  ry table name mu
2360a 73 74 20 62 65 20 75 6e 71 75 61 6c 69 66 69 65  st be unqualifie
2360b 64 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  d");.    return;
2360c 0a 20 20 7d 0a 20 20 69 66 28 20 21 4f 4d 49 54  .  }.  if( !OMIT
2360d 5f 54 45 4d 50 44 42 20 26 26 20 69 73 54 65 6d  _TEMPDB && isTem
2360e 70 20 29 20 69 44 62 20 3d 20 31 3b 0a 0a 20 20  p ) iDb = 1;..  
2360f 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b  pParse->sNameTok
23610 65 6e 20 3d 20 2a 70 4e 61 6d 65 3b 0a 20 20 7a  en = *pName;.  z
23611 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61  Name = sqlite3Na
23612 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
23613 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 4e  pName);.  if( zN
23614 61 6d 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ame==0 ) return;
23615 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
23616 21 3d 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62  !=sqlite3CheckOb
23617 6a 65 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c  jectName(pParse,
23618 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 67   zName) ){.    g
23619 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f  oto begin_table_
2361a 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28  error;.  }.  if(
2361b 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3d 3d 31   db->init.iDb==1
2361c 20 29 20 69 73 54 65 6d 70 20 3d 20 31 3b 0a 23   ) isTemp = 1;.#
2361d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2361e 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
2361f 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 54 65  .  assert( (isTe
23620 6d 70 20 26 20 31 29 3d 3d 69 73 54 65 6d 70 20  mp & 1)==isTemp 
23621 29 3b 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 63  );.  {.    int c
23622 6f 64 65 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a  ode;.    char *z
23623 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  Db = db->aDb[iDb
23624 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 69 66 28  ].zName;.    if(
23625 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
23626 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
23627 5f 49 4e 53 45 52 54 2c 20 53 43 48 45 4d 41 5f  _INSERT, SCHEMA_
23628 54 41 42 4c 45 28 69 73 54 65 6d 70 29 2c 20 30  TABLE(isTemp), 0
23629 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20  , zDb) ){.      
2362a 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65  goto begin_table
2362b 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20  _error;.    }.  
2362c 20 20 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a    if( isView ){.
2362d 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f        if( !OMIT_
2362e 54 45 4d 50 44 42 20 26 26 20 69 73 54 65 6d 70  TEMPDB && isTemp
2362f 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65   ){.        code
23630 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45   = SQLITE_CREATE
23631 5f 54 45 4d 50 5f 56 49 45 57 3b 0a 20 20 20 20  _TEMP_VIEW;.    
23632 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
23633 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43   code = SQLITE_C
23634 52 45 41 54 45 5f 56 49 45 57 3b 0a 20 20 20 20  REATE_VIEW;.    
23635 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
23636 20 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54       if( !OMIT_T
23637 45 4d 50 44 42 20 26 26 20 69 73 54 65 6d 70 20  EMPDB && isTemp 
23638 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20  ){.        code 
23639 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f  = SQLITE_CREATE_
2363a 54 45 4d 50 5f 54 41 42 4c 45 3b 0a 20 20 20 20  TEMP_TABLE;.    
2363b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2363c 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43   code = SQLITE_C
2363d 52 45 41 54 45 5f 54 41 42 4c 45 3b 0a 20 20 20  REATE_TABLE;.   
2363e 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
2363f 66 28 20 21 69 73 56 69 72 74 75 61 6c 20 26 26  f( !isVirtual &&
23640 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
23641 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65 2c 20  k(pParse, code, 
23642 7a 4e 61 6d 65 2c 20 30 2c 20 7a 44 62 29 20 29  zName, 0, zDb) )
23643 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67  {.      goto beg
23644 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a  in_table_error;.
23645 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
23646 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
23647 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e   the new table n
23648 61 6d 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6c  ame does not col
23649 6c 69 64 65 20 77 69 74 68 20 61 6e 20 65 78 69  lide with an exi
2364a 73 74 69 6e 67 0a 20 20 2a 2a 20 69 6e 64 65 78  sting.  ** index
2364b 20 6f 72 20 74 61 62 6c 65 20 6e 61 6d 65 20 69   or table name i
2364c 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  n the same datab
2364d 61 73 65 2e 20 20 49 73 73 75 65 20 61 6e 20 65  ase.  Issue an e
2364e 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 66 0a  rror message if.
2364f 20 20 2a 2a 20 69 74 20 64 6f 65 73 2e 20 54 68    ** it does. Th
23650 65 20 65 78 63 65 70 74 69 6f 6e 20 69 73 20 69  e exception is i
23651 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  f the statement 
23652 62 65 69 6e 67 20 70 61 72 73 65 64 20 77 61 73  being parsed was
23653 20 70 61 73 73 65 64 0a 20 20 2a 2a 20 74 6f 20   passed.  ** to 
23654 61 6e 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61  an sqlite3_decla
23655 72 65 5f 76 74 61 62 28 29 20 63 61 6c 6c 2e 20  re_vtab() call. 
23656 49 6e 20 74 68 61 74 20 63 61 73 65 20 6f 6e 6c  In that case onl
23657 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  y the column nam
23658 65 73 0a 20 20 2a 2a 20 61 6e 64 20 74 79 70 65  es.  ** and type
23659 73 20 77 69 6c 6c 20 62 65 20 75 73 65 64 2c 20  s will be used, 
2365a 73 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e  so there is no n
2365b 65 65 64 20 74 6f 20 74 65 73 74 20 66 6f 72 20  eed to test for 
2365c 6e 61 6d 65 73 70 61 63 65 0a 20 20 2a 2a 20 63  namespace.  ** c
2365d 6f 6c 6c 69 73 69 6f 6e 73 2e 0a 20 20 2a 2f 0a  ollisions..  */.
2365e 20 20 69 66 28 20 21 49 4e 5f 44 45 43 4c 41 52    if( !IN_DECLAR
2365f 45 5f 56 54 41 42 20 29 7b 0a 20 20 20 20 69 66  E_VTAB ){.    if
23660 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c  ( SQLITE_OK!=sql
23661 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70  ite3ReadSchema(p
23662 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 20 20  Parse) ){.      
23663 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65  goto begin_table
23664 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20  _error;.    }.  
23665 20 20 70 54 61 62 6c 65 20 3d 20 73 71 6c 69 74    pTable = sqlit
23666 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
23667 7a 4e 61 6d 65 2c 20 64 62 2d 3e 61 44 62 5b 69  zName, db->aDb[i
23668 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  Db].zName);.    
23669 69 66 28 20 70 54 61 62 6c 65 20 29 7b 0a 20 20  if( pTable ){.  
2366a 20 20 20 20 69 66 28 20 21 6e 6f 45 72 72 20 29      if( !noErr )
2366b 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2366c 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2366d 2c 20 22 74 61 62 6c 65 20 25 54 20 61 6c 72 65  , "table %T alre
2366e 61 64 79 20 65 78 69 73 74 73 22 2c 20 70 4e 61  ady exists", pNa
2366f 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  me);.      }.   
23670 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61     goto begin_ta
23671 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d  ble_error;.    }
23672 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
23673 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 4e  FindIndex(db, zN
23674 61 6d 65 2c 20 30 29 21 3d 30 20 26 26 20 28 69  ame, 0)!=0 && (i
23675 44 62 3d 3d 30 20 7c 7c 20 21 64 62 2d 3e 69 6e  Db==0 || !db->in
23676 69 74 2e 62 75 73 79 29 20 29 7b 0a 20 20 20 20  it.busy) ){.    
23677 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
23678 67 28 70 50 61 72 73 65 2c 20 22 74 68 65 72 65  g(pParse, "there
23679 20 69 73 20 61 6c 72 65 61 64 79 20 61 6e 20 69   is already an i
2367a 6e 64 65 78 20 6e 61 6d 65 64 20 25 73 22 2c 20  ndex named %s", 
2367b 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f  zName);.      go
2367c 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65  to begin_table_e
2367d 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rror;.    }.  }.
2367e 0a 20 20 70 54 61 62 6c 65 20 3d 20 73 71 6c 69  .  pTable = sqli
2367f 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
23680 64 62 2c 20 73 69 7a 65 6f 66 28 54 61 62 6c 65  db, sizeof(Table
23681 29 29 3b 0a 20 20 69 66 28 20 70 54 61 62 6c 65  ));.  if( pTable
23682 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d  ==0 ){.    db->m
23683 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
23684 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20  .    pParse->rc 
23685 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
23686 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72      pParse->nErr
23687 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 62 65 67  ++;.    goto beg
23688 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a  in_table_error;.
23689 20 20 7d 0a 20 20 70 54 61 62 6c 65 2d 3e 7a 4e    }.  pTable->zN
2368a 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70  ame = zName;.  p
2368b 54 61 62 6c 65 2d 3e 69 50 4b 65 79 20 3d 20 2d  Table->iPKey = -
2368c 31 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 70 53 63  1;.  pTable->pSc
2368d 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69  hema = db->aDb[i
2368e 44 62 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70  Db].pSchema;.  p
2368f 54 61 62 6c 65 2d 3e 6e 52 65 66 20 3d 20 31 3b  Table->nRef = 1;
23690 0a 20 20 70 54 61 62 6c 65 2d 3e 64 62 4d 65 6d  .  pTable->dbMem
23691 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
23692 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
23693 65 3d 3d 30 20 29 3b 0a 20 20 70 50 61 72 73 65  e==0 );.  pParse
23694 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20 70 54  ->pNewTable = pT
23695 61 62 6c 65 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  able;..  /* If t
23696 68 69 73 20 69 73 20 74 68 65 20 6d 61 67 69 63  his is the magic
23697 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65   sqlite_sequence
23698 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20 61   table used by a
23699 75 74 6f 69 6e 63 72 65 6d 65 6e 74 2c 0a 20 20  utoincrement,.  
2369a 2a 2a 20 74 68 65 6e 20 72 65 63 6f 72 64 20 61  ** then record a
2369b 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73   pointer to this
2369c 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 6d 61   table in the ma
2369d 69 6e 20 64 61 74 61 62 61 73 65 20 73 74 72 75  in database stru
2369e 63 74 75 72 65 0a 20 20 2a 2a 20 73 6f 20 74 68  cture.  ** so th
2369f 61 74 20 49 4e 53 45 52 54 20 63 61 6e 20 66 69  at INSERT can fi
236a0 6e 64 20 74 68 65 20 74 61 62 6c 65 20 65 61 73  nd the table eas
236a1 69 6c 79 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65  ily..  */.#ifnde
236a2 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
236a3 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 69 66  TOINCREMENT.  if
236a4 28 20 21 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  ( !pParse->neste
236a5 64 20 26 26 20 73 74 72 63 6d 70 28 7a 4e 61 6d  d && strcmp(zNam
236a6 65 2c 20 22 73 71 6c 69 74 65 5f 73 65 71 75 65  e, "sqlite_seque
236a7 6e 63 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  nce")==0 ){.    
236a8 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 2d  pTable->pSchema-
236a9 3e 70 53 65 71 54 61 62 20 3d 20 70 54 61 62 6c  >pSeqTab = pTabl
236aa 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  e;.  }.#endif.. 
236ab 20 2f 2a 20 42 65 67 69 6e 20 67 65 6e 65 72 61   /* Begin genera
236ac 74 69 6e 67 20 74 68 65 20 63 6f 64 65 20 74 68  ting the code th
236ad 61 74 20 77 69 6c 6c 20 69 6e 73 65 72 74 20 74  at will insert t
236ae 68 65 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20  he table record 
236af 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20 53 51  into.  ** the SQ
236b0 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c  LITE_MASTER tabl
236b1 65 2e 20 20 4e 6f 74 65 20 69 6e 20 70 61 72 74  e.  Note in part
236b2 69 63 75 6c 61 72 20 74 68 61 74 20 77 65 20 6d  icular that we m
236b3 75 73 74 20 67 6f 20 61 68 65 61 64 0a 20 20 2a  ust go ahead.  *
236b4 2a 20 61 6e 64 20 61 6c 6c 6f 63 61 74 65 20 74  * and allocate t
236b5 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  he record number
236b6 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 65   for the table e
236b7 6e 74 72 79 20 6e 6f 77 2e 20 20 42 65 66 6f 72  ntry now.  Befor
236b8 65 20 61 6e 79 0a 20 20 2a 2a 20 50 52 49 4d 41  e any.  ** PRIMA
236b9 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45  RY KEY or UNIQUE
236ba 20 6b 65 79 77 6f 72 64 73 20 61 72 65 20 70 61   keywords are pa
236bb 72 73 65 64 2e 20 20 54 68 6f 73 65 20 6b 65 79  rsed.  Those key
236bc 77 6f 72 64 73 20 77 69 6c 6c 20 63 61 75 73 65  words will cause
236bd 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73 20 74 6f  .  ** indices to
236be 20 62 65 20 63 72 65 61 74 65 64 20 61 6e 64 20   be created and 
236bf 74 68 65 20 74 61 62 6c 65 20 72 65 63 6f 72 64  the table record
236c0 20 6d 75 73 74 20 63 6f 6d 65 20 62 65 66 6f 72   must come befor
236c1 65 20 74 68 65 20 0a 20 20 2a 2a 20 69 6e 64 69  e the .  ** indi
236c2 63 65 73 2e 20 20 48 65 6e 63 65 2c 20 74 68 65  ces.  Hence, the
236c3 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 66   record number f
236c4 6f 72 20 74 68 65 20 74 61 62 6c 65 20 6d 75 73  or the table mus
236c5 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a 20  t be allocated. 
236c6 20 2a 2a 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20   ** now..  */.  
236c7 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75  if( !db->init.bu
236c8 73 79 20 26 26 20 28 76 20 3d 20 73 71 6c 69 74  sy && (v = sqlit
236c9 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
236ca 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  ))!=0 ){.    int
236cb 20 6a 31 3b 0a 20 20 20 20 69 6e 74 20 66 69 6c   j1;.    int fil
236cc 65 46 6f 72 6d 61 74 3b 0a 20 20 20 20 69 6e 74  eFormat;.    int
236cd 20 72 65 67 31 2c 20 72 65 67 32 2c 20 72 65 67   reg1, reg2, reg
236ce 33 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65  3;.    sqlite3Be
236cf 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
236d0 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62  n(pParse, 0, iDb
236d1 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
236d2 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
236d3 41 42 4c 45 0a 20 20 20 20 69 66 28 20 69 73 56  ABLE.    if( isV
236d4 69 72 74 75 61 6c 20 29 7b 0a 20 20 20 20 20 20  irtual ){.      
236d5 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
236d6 30 28 76 2c 20 4f 50 5f 56 42 65 67 69 6e 29 3b  0(v, OP_VBegin);
236d7 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  .    }.#endif.. 
236d8 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c     /* If the fil
236d9 65 20 66 6f 72 6d 61 74 20 61 6e 64 20 65 6e 63  e format and enc
236da 6f 64 69 6e 67 20 69 6e 20 74 68 65 20 64 61 74  oding in the dat
236db 61 62 61 73 65 20 68 61 76 65 20 6e 6f 74 20 62  abase have not b
236dc 65 65 6e 20 73 65 74 2c 20 0a 20 20 20 20 2a 2a  een set, .    **
236dd 20 73 65 74 20 74 68 65 6d 20 6e 6f 77 2e 0a 20   set them now.. 
236de 20 20 20 2a 2f 0a 20 20 20 20 72 65 67 31 20 3d     */.    reg1 =
236df 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 77 69   pParse->regRowi
236e0 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  d = ++pParse->nM
236e1 65 6d 3b 0a 20 20 20 20 72 65 67 32 20 3d 20 70  em;.    reg2 = p
236e2 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 20 3d  Parse->regRoot =
236e3 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
236e4 0a 20 20 20 20 72 65 67 33 20 3d 20 2b 2b 70 50  .    reg3 = ++pP
236e5 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
236e6 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
236e7 33 28 76 2c 20 4f 50 5f 52 65 61 64 43 6f 6f 6b  3(v, OP_ReadCook
236e8 69 65 2c 20 69 44 62 2c 20 72 65 67 33 2c 20 42  ie, iDb, reg3, B
236e9 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54  TREE_FILE_FORMAT
236ea 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
236eb 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69  beUsesBtree(v, i
236ec 44 62 29 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71  Db);.    j1 = sq
236ed 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
236ee 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 33 29 3b  v, OP_If, reg3);
236ef 0a 20 20 20 20 66 69 6c 65 46 6f 72 6d 61 74 20  .    fileFormat 
236f0 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  = (db->flags & S
236f1 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c 65  QLITE_LegacyFile
236f2 46 6d 74 29 21 3d 30 20 3f 0a 20 20 20 20 20 20  Fmt)!=0 ?.      
236f3 20 20 20 20 20 20 20 20 20 20 20 20 31 20 3a 20              1 : 
236f4 53 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c 45 5f  SQLITE_MAX_FILE_
236f5 46 4f 52 4d 41 54 3b 0a 20 20 20 20 73 71 6c 69  FORMAT;.    sqli
236f6 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
236f7 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 66 69 6c   OP_Integer, fil
236f8 65 46 6f 72 6d 61 74 2c 20 72 65 67 33 29 3b 0a  eFormat, reg3);.
236f9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
236fa 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43  ddOp3(v, OP_SetC
236fb 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 42 54 52 45  ookie, iDb, BTRE
236fc 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 2c 20 72  E_FILE_FORMAT, r
236fd 65 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  eg3);.    sqlite
236fe 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
236ff 50 5f 49 6e 74 65 67 65 72 2c 20 45 4e 43 28 64  P_Integer, ENC(d
23700 62 29 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73  b), reg3);.    s
23701 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
23702 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65  (v, OP_SetCookie
23703 2c 20 69 44 62 2c 20 42 54 52 45 45 5f 54 45 58  , iDb, BTREE_TEX
23704 54 5f 45 4e 43 4f 44 49 4e 47 2c 20 72 65 67 33  T_ENCODING, reg3
23705 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
23706 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31  beJumpHere(v, j1
23707 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20  );..    /* This 
23708 6a 75 73 74 20 63 72 65 61 74 65 73 20 61 20 70  just creates a p
23709 6c 61 63 65 2d 68 6f 6c 64 65 72 20 72 65 63 6f  lace-holder reco
2370a 72 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65  rd in the sqlite
2370b 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 0a 20  _master table.. 
2370c 20 20 20 2a 2a 20 54 68 65 20 72 65 63 6f 72 64     ** The record
2370d 20 63 72 65 61 74 65 64 20 64 6f 65 73 20 6e 6f   created does no
2370e 74 20 63 6f 6e 74 61 69 6e 20 61 6e 79 74 68 69  t contain anythi
2370f 6e 67 20 79 65 74 2e 20 20 49 74 20 77 69 6c 6c  ng yet.  It will
23710 20 62 65 20 72 65 70 6c 61 63 65 64 0a 20 20 20   be replaced.   
23711 20 2a 2a 20 62 79 20 74 68 65 20 72 65 61 6c 20   ** by the real 
23712 65 6e 74 72 79 20 69 6e 20 63 6f 64 65 20 67 65  entry in code ge
23713 6e 65 72 61 74 65 64 20 61 74 20 73 71 6c 69 74  nerated at sqlit
23714 65 33 45 6e 64 54 61 62 6c 65 28 29 2e 0a 20 20  e3EndTable()..  
23715 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20    **.    ** The 
23716 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 6e 65  rowid for the ne
23717 77 20 65 6e 74 72 79 20 69 73 20 6c 65 66 74 20  w entry is left 
23718 69 6e 20 72 65 67 69 73 74 65 72 20 70 50 61 72  in register pPar
23719 73 65 2d 3e 72 65 67 52 6f 77 69 64 2e 0a 20 20  se->regRowid..  
2371a 20 20 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 61    ** The root pa
2371b 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
2371c 20 6e 65 77 20 74 61 62 6c 65 20 69 73 20 6c 65   new table is le
2371d 66 74 20 69 6e 20 72 65 67 20 70 50 61 72 73 65  ft in reg pParse
2371e 2d 3e 72 65 67 52 6f 6f 74 2e 0a 20 20 20 20 2a  ->regRoot..    *
2371f 2a 20 54 68 65 20 72 6f 77 69 64 20 61 6e 64 20  * The rowid and 
23720 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72  root page number
23721 20 76 61 6c 75 65 73 20 61 72 65 20 6e 65 65 64   values are need
23722 65 64 20 62 79 20 74 68 65 20 63 6f 64 65 20 74  ed by the code t
23723 68 61 74 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74  hat.    ** sqlit
23724 65 33 45 6e 64 54 61 62 6c 65 20 77 69 6c 6c 20  e3EndTable will 
23725 67 65 6e 65 72 61 74 65 2e 0a 20 20 20 20 2a 2f  generate..    */
23726 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
23727 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20  LITE_OMIT_VIEW) 
23728 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  || !defined(SQLI
23729 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
2372a 41 42 4c 45 29 0a 20 20 20 20 69 66 28 20 69 73  ABLE).    if( is
2372b 56 69 65 77 20 7c 7c 20 69 73 56 69 72 74 75 61  View || isVirtua
2372c 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  l ){.      sqlit
2372d 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2372e 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72  OP_Integer, 0, r
2372f 65 67 32 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a  eg2);.    }else.
23730 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20 20 20  #endif.    {.   
23731 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
23732 64 4f 70 32 28 76 2c 20 4f 50 5f 43 72 65 61 74  dOp2(v, OP_Creat
23733 65 54 61 62 6c 65 2c 20 69 44 62 2c 20 72 65 67  eTable, iDb, reg
23734 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  2);.    }.    sq
23735 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54  lite3OpenMasterT
23736 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44 62  able(pParse, iDb
23737 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
23738 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
23739 65 77 52 6f 77 69 64 2c 20 30 2c 20 72 65 67 31  ewRowid, 0, reg1
2373a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2373b 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
2373c 75 6c 6c 2c 20 30 2c 20 72 65 67 33 29 3b 0a 20  ull, 0, reg3);. 
2373d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2373e 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72  dOp3(v, OP_Inser
2373f 74 2c 20 30 2c 20 72 65 67 33 2c 20 72 65 67 31  t, 0, reg3, reg1
23740 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
23741 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50  beChangeP5(v, OP
23742 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20  FLAG_APPEND);.  
23743 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
23744 4f 70 30 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 29  Op0(v, OP_Close)
23745 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 72 6d  ;.  }..  /* Norm
23746 61 6c 20 28 6e 6f 6e 2d 65 72 72 6f 72 29 20 72  al (non-error) r
23747 65 74 75 72 6e 2e 20 2a 2f 0a 20 20 72 65 74 75  eturn. */.  retu
23748 72 6e 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20  rn;..  /* If an 
23749 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 77 65  error occurs, we
2374a 20 6a 75 6d 70 20 68 65 72 65 20 2a 2f 0a 62 65   jump here */.be
2374b 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3a  gin_table_error:
2374c 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
2374d 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 72  (db, zName);.  r
2374e 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  eturn;.}../*.** 
2374f 54 68 69 73 20 6d 61 63 72 6f 20 69 73 20 75 73  This macro is us
23750 65 64 20 74 6f 20 63 6f 6d 70 61 72 65 20 74 77  ed to compare tw
23751 6f 20 73 74 72 69 6e 67 73 20 69 6e 20 61 20 63  o strings in a c
23752 61 73 65 2d 69 6e 73 65 6e 73 69 74 69 76 65 20  ase-insensitive 
23753 6d 61 6e 6e 65 72 2e 0a 2a 2a 20 49 74 20 69 73  manner..** It is
23754 20 73 6c 69 67 68 74 6c 79 20 66 61 73 74 65 72   slightly faster
23755 20 74 68 61 6e 20 63 61 6c 6c 69 6e 67 20 73 71   than calling sq
23756 6c 69 74 65 33 53 74 72 49 43 6d 70 28 29 20 64  lite3StrICmp() d
23757 69 72 65 63 74 6c 79 2c 20 62 75 74 0a 2a 2a 20  irectly, but.** 
23758 70 72 6f 64 75 63 65 73 20 6c 61 72 67 65 72 20  produces larger 
23759 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 57 41 52 4e  code..**.** WARN
2375a 49 4e 47 3a 20 54 68 69 73 20 6d 61 63 72 6f 20  ING: This macro 
2375b 69 73 20 6e 6f 74 20 63 6f 6d 70 61 74 69 62 6c  is not compatibl
2375c 65 20 77 69 74 68 20 74 68 65 20 73 74 72 63 6d  e with the strcm
2375d 70 28 29 20 66 61 6d 69 6c 79 2e 20 49 74 0a 2a  p() family. It.*
2375e 2a 20 72 65 74 75 72 6e 73 20 74 72 75 65 20 69  * returns true i
2375f 66 20 74 68 65 20 74 77 6f 20 73 74 72 69 6e 67  f the two string
23760 73 20 61 72 65 20 65 71 75 61 6c 2c 20 6f 74 68  s are equal, oth
23761 65 72 77 69 73 65 20 66 61 6c 73 65 2e 0a 2a 2f  erwise false..*/
23762 0a 23 64 65 66 69 6e 65 20 53 54 52 49 43 4d 50  .#define STRICMP
23763 28 78 2c 20 79 29 20 28 5c 0a 73 71 6c 69 74 65  (x, y) (\.sqlite
23764 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 28  3UpperToLower[*(
23765 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
23766 28 78 29 5d 3d 3d 20 20 20 5c 0a 73 71 6c 69 74  (x)]==   \.sqlit
23767 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a  e3UpperToLower[*
23768 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
23769 29 28 79 29 5d 20 20 20 20 20 5c 0a 26 26 20 73  )(y)]     \.&& s
2376a 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 28 78  qlite3StrICmp((x
2376b 29 2b 31 2c 28 79 29 2b 31 29 3d 3d 30 20 29 0a  )+1,(y)+1)==0 ).
2376c 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77  ./*.** Add a new
2376d 20 63 6f 6c 75 6d 6e 20 74 6f 20 74 68 65 20 74   column to the t
2376e 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 62  able currently b
2376f 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64  eing constructed
23770 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73  ..**.** The pars
23771 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f  er calls this ro
23772 75 74 69 6e 65 20 6f 6e 63 65 20 66 6f 72 20 65  utine once for e
23773 61 63 68 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61  ach column decla
23774 72 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20 61 20 43  ration.** in a C
23775 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
23776 65 6d 65 6e 74 2e 20 20 73 71 6c 69 74 65 33 53  ement.  sqlite3S
23777 74 61 72 74 54 61 62 6c 65 28 29 20 67 65 74 73  tartTable() gets
23778 20 63 61 6c 6c 65 64 0a 2a 2a 20 66 69 72 73 74   called.** first
23779 20 74 6f 20 67 65 74 20 74 68 69 6e 67 73 20 67   to get things g
2377a 6f 69 6e 67 2e 20 20 54 68 65 6e 20 74 68 69 73  oing.  Then this
2377b 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
2377c 65 64 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 63  ed for each.** c
2377d 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  olumn..*/.SQLITE
2377e 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2377f 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28 50  lite3AddColumn(P
23780 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f  arse *pParse, To
23781 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 54  ken *pName){.  T
23782 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20 69  able *p;.  int i
23783 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 43  ;.  char *z;.  C
23784 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73  olumn *pCol;.  s
23785 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
23786 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 28  rse->db;.  if( (
23787 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  p = pParse->pNew
23788 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65 74 75  Table)==0 ) retu
23789 72 6e 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d  rn;.#if SQLITE_M
2378a 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 20  AX_COLUMN.  if( 
2378b 70 2d 3e 6e 43 6f 6c 2b 31 3e 64 62 2d 3e 61 4c  p->nCol+1>db->aL
2378c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
2378d 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20  T_COLUMN] ){.   
2378e 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2378f 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61  (pParse, "too ma
23790 6e 79 20 63 6f 6c 75 6d 6e 73 20 6f 6e 20 25 73  ny columns on %s
23791 22 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  ", p->zName);.  
23792 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65    return;.  }.#e
23793 6e 64 69 66 0a 20 20 7a 20 3d 20 73 71 6c 69 74  ndif.  z = sqlit
23794 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
23795 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66  db, pName);.  if
23796 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( z==0 ) return;
23797 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d  .  for(i=0; i<p-
23798 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nCol; i++){.   
23799 20 69 66 28 20 53 54 52 49 43 4d 50 28 7a 2c 20   if( STRICMP(z, 
2379a 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65  p->aCol[i].zName
2379b 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) ){.      sqlit
2379c 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2379d 65 2c 20 22 64 75 70 6c 69 63 61 74 65 20 63 6f  e, "duplicate co
2379e 6c 75 6d 6e 20 6e 61 6d 65 3a 20 25 73 22 2c 20  lumn name: %s", 
2379f 7a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  z);.      sqlite
237a0 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a  3DbFree(db, z);.
237a1 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
237a2 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 28 70    }.  }.  if( (p
237a3 2d 3e 6e 43 6f 6c 20 26 20 30 78 37 29 3d 3d 30  ->nCol & 0x7)==0
237a4 20 29 7b 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a   ){.    Column *
237a5 61 4e 65 77 3b 0a 20 20 20 20 61 4e 65 77 20 3d  aNew;.    aNew =
237a6 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f   sqlite3DbReallo
237a7 63 28 64 62 2c 70 2d 3e 61 43 6f 6c 2c 28 70 2d  c(db,p->aCol,(p-
237a8 3e 6e 43 6f 6c 2b 38 29 2a 73 69 7a 65 6f 66 28  >nCol+8)*sizeof(
237a9 70 2d 3e 61 43 6f 6c 5b 30 5d 29 29 3b 0a 20 20  p->aCol[0]));.  
237aa 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b    if( aNew==0 ){
237ab 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
237ac 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20  Free(db, z);.   
237ad 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
237ae 0a 20 20 20 20 70 2d 3e 61 43 6f 6c 20 3d 20 61  .    p->aCol = a
237af 4e 65 77 3b 0a 20 20 7d 0a 20 20 70 43 6f 6c 20  New;.  }.  pCol 
237b0 3d 20 26 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43  = &p->aCol[p->nC
237b1 6f 6c 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 70 43  ol];.  memset(pC
237b2 6f 6c 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 2d  ol, 0, sizeof(p-
237b3 3e 61 43 6f 6c 5b 30 5d 29 29 3b 0a 20 20 70 43  >aCol[0]));.  pC
237b4 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 3b 0a 20  ol->zName = z;. 
237b5 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69  .  /* If there i
237b6 73 20 6e 6f 20 74 79 70 65 20 73 70 65 63 69 66  s no type specif
237b7 69 65 64 2c 20 63 6f 6c 75 6d 6e 73 20 68 61 76  ied, columns hav
237b8 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 61 66  e the default af
237b9 66 69 6e 69 74 79 0a 20 20 2a 2a 20 27 4e 4f 4e  finity.  ** 'NON
237ba 45 27 2e 20 49 66 20 74 68 65 72 65 20 69 73 20  E'. If there is 
237bb 61 20 74 79 70 65 20 73 70 65 63 69 66 69 65 64  a type specified
237bc 2c 20 74 68 65 6e 20 73 71 6c 69 74 65 33 41 64  , then sqlite3Ad
237bd 64 43 6f 6c 75 6d 6e 54 79 70 65 28 29 20 77 69  dColumnType() wi
237be 6c 6c 0a 20 20 2a 2a 20 62 65 20 63 61 6c 6c 65  ll.  ** be calle
237bf 64 20 6e 65 78 74 20 74 6f 20 73 65 74 20 70 43  d next to set pC
237c0 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 63 6f 72  ol->affinity cor
237c1 72 65 63 74 6c 79 2e 0a 20 20 2a 2f 0a 20 20 70  rectly..  */.  p
237c2 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20  Col->affinity = 
237c3 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b  SQLITE_AFF_NONE;
237c4 0a 20 20 70 2d 3e 6e 43 6f 6c 2b 2b 3b 0a 7d 0a  .  p->nCol++;.}.
237c5 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
237c6 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79  ine is called by
237c7 20 74 68 65 20 70 61 72 73 65 72 20 77 68 69 6c   the parser whil
237c8 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  e in the middle 
237c9 6f 66 0a 2a 2a 20 70 61 72 73 69 6e 67 20 61 20  of.** parsing a 
237ca 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
237cb 74 65 6d 65 6e 74 2e 20 20 41 20 22 4e 4f 54 20  tement.  A "NOT 
237cc 4e 55 4c 4c 22 20 63 6f 6e 73 74 72 61 69 6e 74  NULL" constraint
237cd 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20 73 65 65   has.** been see
237ce 6e 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 2e 20 20  n on a column.  
237cf 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74  This routine set
237d0 73 20 74 68 65 20 6e 6f 74 4e 75 6c 6c 20 66 6c  s the notNull fl
237d1 61 67 20 6f 6e 0a 2a 2a 20 74 68 65 20 63 6f 6c  ag on.** the col
237d2 75 6d 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 6e  umn currently un
237d3 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
237d4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
237d5 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
237d6 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 72 73 65  AddNotNull(Parse
237d7 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6f 6e   *pParse, int on
237d8 45 72 72 6f 72 29 7b 0a 20 20 54 61 62 6c 65 20  Error){.  Table 
237d9 2a 70 3b 0a 20 20 70 20 3d 20 70 50 61 72 73 65  *p;.  p = pParse
237da 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69  ->pNewTable;.  i
237db 66 28 20 70 3d 3d 30 20 7c 7c 20 4e 45 56 45 52  f( p==0 || NEVER
237dc 28 70 2d 3e 6e 43 6f 6c 3c 31 29 20 29 20 72 65  (p->nCol<1) ) re
237dd 74 75 72 6e 3b 0a 20 20 70 2d 3e 61 43 6f 6c 5b  turn;.  p->aCol[
237de 70 2d 3e 6e 43 6f 6c 2d 31 5d 2e 6e 6f 74 4e 75  p->nCol-1].notNu
237df 6c 6c 20 3d 20 28 75 38 29 6f 6e 45 72 72 6f 72  ll = (u8)onError
237e0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 63 61 6e 20  ;.}../*.** Scan 
237e1 74 68 65 20 63 6f 6c 75 6d 6e 20 74 79 70 65 20  the column type 
237e2 6e 61 6d 65 20 7a 54 79 70 65 20 28 6c 65 6e 67  name zType (leng
237e3 74 68 20 6e 54 79 70 65 29 20 61 6e 64 20 72 65  th nType) and re
237e4 74 75 72 6e 20 74 68 65 0a 2a 2a 20 61 73 73 6f  turn the.** asso
237e5 63 69 61 74 65 64 20 61 66 66 69 6e 69 74 79 20  ciated affinity 
237e6 74 79 70 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  type..**.** This
237e7 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 61 20   routine does a 
237e8 63 61 73 65 2d 69 6e 64 65 70 65 6e 64 65 6e 74  case-independent
237e9 20 73 65 61 72 63 68 20 6f 66 20 7a 54 79 70 65   search of zType
237ea 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 73 75 62   for the .** sub
237eb 73 74 72 69 6e 67 73 20 69 6e 20 74 68 65 20 66  strings in the f
237ec 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 2e 20  ollowing table. 
237ed 49 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 75  If one of the su
237ee 62 73 74 72 69 6e 67 73 20 69 73 0a 2a 2a 20 66  bstrings is.** f
237ef 6f 75 6e 64 2c 20 74 68 65 20 63 6f 72 72 65 73  ound, the corres
237f0 70 6f 6e 64 69 6e 67 20 61 66 66 69 6e 69 74 79  ponding affinity
237f1 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66   is returned. If
237f2 20 7a 54 79 70 65 20 63 6f 6e 74 61 69 6e 73 0a   zType contains.
237f3 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  ** more than one
237f4 20 6f 66 20 74 68 65 20 73 75 62 73 74 72 69 6e   of the substrin
237f5 67 73 2c 20 65 6e 74 72 69 65 73 20 74 6f 77 61  gs, entries towa
237f6 72 64 20 74 68 65 20 74 6f 70 20 6f 66 20 0a 2a  rd the top of .*
237f7 2a 20 74 68 65 20 74 61 62 6c 65 20 74 61 6b 65  * the table take
237f8 20 70 72 69 6f 72 69 74 79 2e 20 46 6f 72 20 65   priority. For e
237f9 78 61 6d 70 6c 65 2c 20 69 66 20 7a 54 79 70 65  xample, if zType
237fa 20 69 73 20 27 42 4c 4f 42 49 4e 54 27 2c 20 0a   is 'BLOBINT', .
237fb 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e  ** SQLITE_AFF_IN
237fc 54 45 47 45 52 20 69 73 20 72 65 74 75 72 6e 65  TEGER is returne
237fd 64 2e 0a 2a 2a 0a 2a 2a 20 53 75 62 73 74 72 69  d..**.** Substri
237fe 6e 67 20 20 20 20 20 7c 20 41 66 66 69 6e 69 74  ng     | Affinit
237ff 79 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  y.** -----------
23800 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
23801 2d 2d 2d 2d 2d 0a 2a 2a 20 27 49 4e 54 27 20 20  -----.** 'INT'  
23802 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f         | SQLITE_
23803 41 46 46 5f 49 4e 54 45 47 45 52 0a 2a 2a 20 27  AFF_INTEGER.** '
23804 43 48 41 52 27 20 20 20 20 20 20 20 20 7c 20 53  CHAR'        | S
23805 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a  QLITE_AFF_TEXT.*
23806 2a 20 27 43 4c 4f 42 27 20 20 20 20 20 20 20 20  * 'CLOB'        
23807 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  | SQLITE_AFF_TEX
23808 54 0a 2a 2a 20 27 54 45 58 54 27 20 20 20 20 20  T.** 'TEXT'     
23809 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f     | SQLITE_AFF_
2380a 54 45 58 54 0a 2a 2a 20 27 42 4c 4f 42 27 20 20  TEXT.** 'BLOB'  
2380b 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41        | SQLITE_A
2380c 46 46 5f 4e 4f 4e 45 0a 2a 2a 20 27 52 45 41 4c  FF_NONE.** 'REAL
2380d 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54  '        | SQLIT
2380e 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a 20 27 46  E_AFF_REAL.** 'F
2380f 4c 4f 41 27 20 20 20 20 20 20 20 20 7c 20 53 51  LOA'        | SQ
23810 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a  LITE_AFF_REAL.**
23811 20 27 44 4f 55 42 27 20 20 20 20 20 20 20 20 7c   'DOUB'        |
23812 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c   SQLITE_AFF_REAL
23813 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 6e 65 20 6f  .**.** If none o
23814 66 20 74 68 65 20 73 75 62 73 74 72 69 6e 67 73  f the substrings
23815 20 69 6e 20 74 68 65 20 61 62 6f 76 65 20 74 61   in the above ta
23816 62 6c 65 20 61 72 65 20 66 6f 75 6e 64 2c 0a 2a  ble are found,.*
23817 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d  * SQLITE_AFF_NUM
23818 45 52 49 43 20 69 73 20 72 65 74 75 72 6e 65 64  ERIC is returned
23819 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2381a 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33  ATE char sqlite3
2381b 41 66 66 69 6e 69 74 79 54 79 70 65 28 63 6f 6e  AffinityType(con
2381c 73 74 20 63 68 61 72 20 2a 7a 49 6e 29 7b 0a 20  st char *zIn){. 
2381d 20 75 33 32 20 68 20 3d 20 30 3b 0a 20 20 63 68   u32 h = 0;.  ch
2381e 61 72 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f  ar aff = SQLITE_
2381f 41 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 0a 20 20  AFF_NUMERIC;..  
23820 69 66 28 20 7a 49 6e 20 29 20 77 68 69 6c 65 28  if( zIn ) while(
23821 20 7a 49 6e 5b 30 5d 20 29 7b 0a 20 20 20 20 68   zIn[0] ){.    h
23822 20 3d 20 28 68 3c 3c 38 29 20 2b 20 73 71 6c 69   = (h<<8) + sqli
23823 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  te3UpperToLower[
23824 28 2a 7a 49 6e 29 26 30 78 66 66 5d 3b 0a 20 20  (*zIn)&0xff];.  
23825 20 20 7a 49 6e 2b 2b 3b 0a 20 20 20 20 69 66 28    zIn++;.    if(
23826 20 68 3d 3d 28 28 27 63 27 3c 3c 32 34 29 2b 28   h==(('c'<<24)+(
23827 27 68 27 3c 3c 31 36 29 2b 28 27 61 27 3c 3c 38  'h'<<16)+('a'<<8
23828 29 2b 27 72 27 29 20 29 7b 20 20 20 20 20 20 20  )+'r') ){       
23829 20 20 20 20 20 20 2f 2a 20 43 48 41 52 20 2a 2f        /* CHAR */
2382a 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c  .      aff = SQL
2382b 49 54 45 5f 41 46 46 5f 54 45 58 54 3b 20 0a 20  ITE_AFF_TEXT; . 
2382c 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d     }else if( h==
2382d 28 28 27 63 27 3c 3c 32 34 29 2b 28 27 6c 27 3c  (('c'<<24)+('l'<
2382e 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29 2b 27 62  <16)+('o'<<8)+'b
2382f 27 29 20 29 7b 20 20 20 20 20 20 20 2f 2a 20 43  ') ){       /* C
23830 4c 4f 42 20 2a 2f 0a 20 20 20 20 20 20 61 66 66  LOB */.      aff
23831 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45   = SQLITE_AFF_TE
23832 58 54 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  XT;.    }else if
23833 28 20 68 3d 3d 28 28 27 74 27 3c 3c 32 34 29 2b  ( h==(('t'<<24)+
23834 28 27 65 27 3c 3c 31 36 29 2b 28 27 78 27 3c 3c  ('e'<<16)+('x'<<
23835 38 29 2b 27 74 27 29 20 29 7b 20 20 20 20 20 20  8)+'t') ){      
23836 20 2f 2a 20 54 45 58 54 20 2a 2f 0a 20 20 20 20   /* TEXT */.    
23837 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41    aff = SQLITE_A
23838 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 7d 65 6c  FF_TEXT;.    }el
23839 73 65 20 69 66 28 20 68 3d 3d 28 28 27 62 27 3c  se if( h==(('b'<
2383a 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b 28  <24)+('l'<<16)+(
2383b 27 6f 27 3c 3c 38 29 2b 27 62 27 29 20 20 20 20  'o'<<8)+'b')    
2383c 20 20 20 20 20 20 2f 2a 20 42 4c 4f 42 20 2a 2f        /* BLOB */
2383d 0a 20 20 20 20 20 20 20 20 26 26 20 28 61 66 66  .        && (aff
2383e 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d  ==SQLITE_AFF_NUM
2383f 45 52 49 43 20 7c 7c 20 61 66 66 3d 3d 53 51 4c  ERIC || aff==SQL
23840 49 54 45 5f 41 46 46 5f 52 45 41 4c 29 20 29 7b  ITE_AFF_REAL) ){
23841 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c  .      aff = SQL
23842 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 23 69  ITE_AFF_NONE;.#i
23843 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
23844 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
23845 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68  .    }else if( h
23846 3d 3d 28 28 27 72 27 3c 3c 32 34 29 2b 28 27 65  ==(('r'<<24)+('e
23847 27 3c 3c 31 36 29 2b 28 27 61 27 3c 3c 38 29 2b  '<<16)+('a'<<8)+
23848 27 6c 27 29 20 20 20 20 20 20 20 20 20 20 2f 2a  'l')          /*
23849 20 52 45 41 4c 20 2a 2f 0a 20 20 20 20 20 20 20   REAL */.       
2384a 20 26 26 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f   && aff==SQLITE_
2384b 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 7b 0a 20  AFF_NUMERIC ){. 
2384c 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54       aff = SQLIT
2384d 45 5f 41 46 46 5f 52 45 41 4c 3b 0a 20 20 20 20  E_AFF_REAL;.    
2384e 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27  }else if( h==(('
2384f 66 27 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36  f'<<24)+('l'<<16
23850 29 2b 28 27 6f 27 3c 3c 38 29 2b 27 61 27 29 20  )+('o'<<8)+'a') 
23851 20 20 20 20 20 20 20 20 20 2f 2a 20 46 4c 4f 41           /* FLOA
23852 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26 20 61   */.        && a
23853 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ff==SQLITE_AFF_N
23854 55 4d 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20  UMERIC ){.      
23855 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  aff = SQLITE_AFF
23856 5f 52 45 41 4c 3b 0a 20 20 20 20 7d 65 6c 73 65  _REAL;.    }else
23857 20 69 66 28 20 68 3d 3d 28 28 27 64 27 3c 3c 32   if( h==(('d'<<2
23858 34 29 2b 28 27 6f 27 3c 3c 31 36 29 2b 28 27 75  4)+('o'<<16)+('u
23859 27 3c 3c 38 29 2b 27 62 27 29 20 20 20 20 20 20  '<<8)+'b')      
2385a 20 20 20 20 2f 2a 20 44 4f 55 42 20 2a 2f 0a 20      /* DOUB */. 
2385b 20 20 20 20 20 20 20 26 26 20 61 66 66 3d 3d 53         && aff==S
2385c 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49  QLITE_AFF_NUMERI
2385d 43 20 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d  C ){.      aff =
2385e 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c   SQLITE_AFF_REAL
2385f 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c  ;.#endif.    }el
23860 73 65 20 69 66 28 20 28 68 26 30 78 30 30 46 46  se if( (h&0x00FF
23861 46 46 46 46 29 3d 3d 28 28 27 69 27 3c 3c 31 36  FFFF)==(('i'<<16
23862 29 2b 28 27 6e 27 3c 3c 38 29 2b 27 74 27 29 20  )+('n'<<8)+'t') 
23863 29 7b 20 20 20 20 2f 2a 20 49 4e 54 20 2a 2f 0a  ){    /* INT */.
23864 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49        aff = SQLI
23865 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a  TE_AFF_INTEGER;.
23866 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
23867 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
23868 20 61 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54   aff;.}../*.** T
23869 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
2386a 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72  alled by the par
2386b 73 65 72 20 77 68 69 6c 65 20 69 6e 20 74 68 65  ser while in the
2386c 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a 20 70 61   middle of.** pa
2386d 72 73 69 6e 67 20 61 20 43 52 45 41 54 45 20 54  rsing a CREATE T
2386e 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20  ABLE statement. 
2386f 20 54 68 65 20 70 46 69 72 73 74 20 74 6f 6b 65   The pFirst toke
23870 6e 20 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a  n is the first.*
23871 2a 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 73  * token in the s
23872 65 71 75 65 6e 63 65 20 6f 66 20 74 6f 6b 65 6e  equence of token
23873 73 20 74 68 61 74 20 64 65 73 63 72 69 62 65 20  s that describe 
23874 74 68 65 20 74 79 70 65 20 6f 66 20 74 68 65 0a  the type of the.
23875 2a 2a 20 63 6f 6c 75 6d 6e 20 63 75 72 72 65 6e  ** column curren
23876 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  tly under constr
23877 75 63 74 69 6f 6e 2e 20 20 20 70 4c 61 73 74 20  uction.   pLast 
23878 69 73 20 74 68 65 20 6c 61 73 74 20 74 6f 6b 65  is the last toke
23879 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 73 65 71 75  n.** in the sequ
2387a 65 6e 63 65 2e 20 20 55 73 65 20 74 68 69 73 20  ence.  Use this 
2387b 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 63  information to c
2387c 6f 6e 73 74 72 75 63 74 20 61 20 73 74 72 69 6e  onstruct a strin
2387d 67 0a 2a 2a 20 74 68 61 74 20 63 6f 6e 74 61 69  g.** that contai
2387e 6e 73 20 74 68 65 20 74 79 70 65 6e 61 6d 65 20  ns the typename 
2387f 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 6e  of the column an
23880 64 20 73 74 6f 72 65 20 74 68 61 74 20 73 74 72  d store that str
23881 69 6e 67 0a 2a 2a 20 69 6e 20 7a 54 79 70 65 2e  ing.** in zType.
23882 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/ .SQLITE_PRIV
23883 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
23884 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 50 61  AddColumnType(Pa
23885 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b  rse *pParse, Tok
23886 65 6e 20 2a 70 54 79 70 65 29 7b 0a 20 20 54 61  en *pType){.  Ta
23887 62 6c 65 20 2a 70 3b 0a 20 20 43 6f 6c 75 6d 6e  ble *p;.  Column
23888 20 2a 70 43 6f 6c 3b 0a 0a 20 20 70 20 3d 20 70   *pCol;..  p = p
23889 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2388a 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20  ;.  if( p==0 || 
2388b 4e 45 56 45 52 28 70 2d 3e 6e 43 6f 6c 3c 31 29  NEVER(p->nCol<1)
2388c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 43 6f   ) return;.  pCo
2388d 6c 20 3d 20 26 70 2d 3e 61 43 6f 6c 5b 70 2d 3e  l = &p->aCol[p->
2388e 6e 43 6f 6c 2d 31 5d 3b 0a 20 20 61 73 73 65 72  nCol-1];.  asser
2388f 74 28 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 3d 3d  t( pCol->zType==
23890 30 20 29 3b 0a 20 20 70 43 6f 6c 2d 3e 7a 54 79  0 );.  pCol->zTy
23891 70 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  pe = sqlite3Name
23892 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65  FromToken(pParse
23893 2d 3e 64 62 2c 20 70 54 79 70 65 29 3b 0a 20 20  ->db, pType);.  
23894 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 3d  pCol->affinity =
23895 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79   sqlite3Affinity
23896 54 79 70 65 28 70 43 6f 6c 2d 3e 7a 54 79 70 65  Type(pCol->zType
23897 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  );.}../*.** The 
23898 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 68  expression is th
23899 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20  e default value 
2389a 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63  for the most rec
2389b 65 6e 74 6c 79 20 61 64 64 65 64 20 63 6f 6c 75  ently added colu
2389c 6d 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 74 61 62  mn.** of the tab
2389d 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64  le currently und
2389e 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e  er construction.
2389f 0a 2a 2a 0a 2a 2a 20 44 65 66 61 75 6c 74 20 76  .**.** Default v
238a0 61 6c 75 65 20 65 78 70 72 65 73 73 69 6f 6e 73  alue expressions
238a1 20 6d 75 73 74 20 62 65 20 63 6f 6e 73 74 61 6e   must be constan
238a2 74 2e 20 20 52 61 69 73 65 20 61 6e 20 65 78 63  t.  Raise an exc
238a3 65 70 74 69 6f 6e 20 69 66 20 74 68 69 73 0a 2a  eption if this.*
238a4 2a 20 69 73 20 6e 6f 74 20 74 68 65 20 63 61 73  * is not the cas
238a5 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  e..**.** This ro
238a6 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
238a7 62 79 20 74 68 65 20 70 61 72 73 65 72 20 77 68  by the parser wh
238a8 69 6c 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  ile in the middl
238a9 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69 6e 67 20  e of.** parsing 
238aa 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  a CREATE TABLE s
238ab 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c  tatement..*/.SQL
238ac 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
238ad 20 73 71 6c 69 74 65 33 41 64 64 44 65 66 61 75   sqlite3AddDefau
238ae 6c 74 56 61 6c 75 65 28 50 61 72 73 65 20 2a 70  ltValue(Parse *p
238af 50 61 72 73 65 2c 20 45 78 70 72 53 70 61 6e 20  Parse, ExprSpan 
238b0 2a 70 53 70 61 6e 29 7b 0a 20 20 54 61 62 6c 65  *pSpan){.  Table
238b1 20 2a 70 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70   *p;.  Column *p
238b2 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  Col;.  sqlite3 *
238b3 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
238b4 0a 20 20 70 20 3d 20 70 50 61 72 73 65 2d 3e 70  .  p = pParse->p
238b5 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 20  NewTable;.  if( 
238b6 70 21 3d 30 20 29 7b 0a 20 20 20 20 70 43 6f 6c  p!=0 ){.    pCol
238b7 20 3d 20 26 28 70 2d 3e 61 43 6f 6c 5b 70 2d 3e   = &(p->aCol[p->
238b8 6e 43 6f 6c 2d 31 5d 29 3b 0a 20 20 20 20 69 66  nCol-1]);.    if
238b9 28 20 21 73 71 6c 69 74 65 33 45 78 70 72 49 73  ( !sqlite3ExprIs
238ba 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74 69  ConstantOrFuncti
238bb 6f 6e 28 70 53 70 61 6e 2d 3e 70 45 78 70 72 29  on(pSpan->pExpr)
238bc 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
238bd 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
238be 2c 20 22 64 65 66 61 75 6c 74 20 76 61 6c 75 65  , "default value
238bf 20 6f 66 20 63 6f 6c 75 6d 6e 20 5b 25 73 5d 20   of column [%s] 
238c0 69 73 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 22  is not constant"
238c1 2c 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c  ,.          pCol
238c2 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65  ->zName);.    }e
238c3 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20  lse{.      /* A 
238c4 63 6f 70 79 20 6f 66 20 70 45 78 70 72 20 69 73  copy of pExpr is
238c5 20 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66   used instead of
238c6 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 2c 20 61   the original, a
238c7 73 20 70 45 78 70 72 20 63 6f 6e 74 61 69 6e 73  s pExpr contains
238c8 0a 20 20 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 73  .      ** tokens
238c9 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 76   that point to v
238ca 6f 6c 61 74 69 6c 65 20 6d 65 6d 6f 72 79 2e 20  olatile memory. 
238cb 54 68 65 20 27 73 70 61 6e 27 20 6f 66 20 74 68  The 'span' of th
238cc 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 20  e expression.   
238cd 20 20 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65     ** is require
238ce 64 20 62 79 20 70 72 61 67 6d 61 20 74 61 62 6c  d by pragma tabl
238cf 65 5f 69 6e 66 6f 2e 0a 20 20 20 20 20 20 2a 2f  e_info..      */
238d0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
238d1 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f  prDelete(db, pCo
238d2 6c 2d 3e 70 44 66 6c 74 29 3b 0a 20 20 20 20 20  l->pDflt);.     
238d3 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 20 3d 20 73   pCol->pDflt = s
238d4 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
238d5 2c 20 70 53 70 61 6e 2d 3e 70 45 78 70 72 2c 20  , pSpan->pExpr, 
238d6 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b  EXPRDUP_REDUCE);
238d7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
238d8 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a  Free(db, pCol->z
238d9 44 66 6c 74 29 3b 0a 20 20 20 20 20 20 70 43 6f  Dflt);.      pCo
238da 6c 2d 3e 7a 44 66 6c 74 20 3d 20 73 71 6c 69 74  l->zDflt = sqlit
238db 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20  e3DbStrNDup(db, 
238dc 28 63 68 61 72 2a 29 70 53 70 61 6e 2d 3e 7a 53  (char*)pSpan->zS
238dd 74 61 72 74 2c 0a 20 20 20 20 20 20 20 20 20 20  tart,.          
238de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
238df 20 20 20 20 20 20 20 20 20 20 20 28 69 6e 74 29             (int)
238e0 28 70 53 70 61 6e 2d 3e 7a 45 6e 64 20 2d 20 70  (pSpan->zEnd - p
238e1 53 70 61 6e 2d 3e 7a 53 74 61 72 74 29 29 3b 0a  Span->zStart));.
238e2 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69      }.  }.  sqli
238e3 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
238e4 2c 20 70 53 70 61 6e 2d 3e 70 45 78 70 72 29 3b  , pSpan->pExpr);
238e5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 69 67 6e  .}../*.** Design
238e6 61 74 65 20 74 68 65 20 50 52 49 4d 41 52 59 20  ate the PRIMARY 
238e7 4b 45 59 20 66 6f 72 20 74 68 65 20 74 61 62 6c  KEY for the tabl
238e8 65 2e 20 20 70 4c 69 73 74 20 69 73 20 61 20 6c  e.  pList is a l
238e9 69 73 74 20 6f 66 20 6e 61 6d 65 73 20 0a 2a 2a  ist of names .**
238ea 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74   of columns that
238eb 20 66 6f 72 6d 20 74 68 65 20 70 72 69 6d 61 72   form the primar
238ec 79 20 6b 65 79 2e 20 20 49 66 20 70 4c 69 73 74  y key.  If pList
238ed 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74   is NULL, then t
238ee 68 65 0a 2a 2a 20 6d 6f 73 74 20 72 65 63 65 6e  he.** most recen
238ef 74 6c 79 20 61 64 64 65 64 20 63 6f 6c 75 6d 6e  tly added column
238f0 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 73   of the table is
238f1 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79   the primary key
238f2 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 61 62 6c 65 20  ..**.** A table 
238f3 63 61 6e 20 68 61 76 65 20 61 74 20 6d 6f 73 74  can have at most
238f4 20 6f 6e 65 20 70 72 69 6d 61 72 79 20 6b 65 79   one primary key
238f5 2e 20 20 49 66 20 74 68 65 20 74 61 62 6c 65 20  .  If the table 
238f6 61 6c 72 65 61 64 79 20 68 61 73 0a 2a 2a 20 61  already has.** a
238f7 20 70 72 69 6d 61 72 79 20 6b 65 79 20 28 61 6e   primary key (an
238f8 64 20 74 68 69 73 20 69 73 20 74 68 65 20 73 65  d this is the se
238f9 63 6f 6e 64 20 70 72 69 6d 61 72 79 20 6b 65 79  cond primary key
238fa 29 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 6e  ) then create an
238fb 0a 2a 2a 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a  .** error..**.**
238fc 20 49 66 20 74 68 65 20 50 52 49 4d 41 52 59 20   If the PRIMARY 
238fd 4b 45 59 20 69 73 20 6f 6e 20 61 20 73 69 6e 67  KEY is on a sing
238fe 6c 65 20 63 6f 6c 75 6d 6e 20 77 68 6f 73 65 20  le column whose 
238ff 64 61 74 61 74 79 70 65 20 69 73 20 49 4e 54 45  datatype is INTE
23900 47 45 52 2c 0a 2a 2a 20 74 68 65 6e 20 77 65 20  GER,.** then we 
23901 77 69 6c 6c 20 74 72 79 20 74 6f 20 75 73 65 20  will try to use 
23902 74 68 61 74 20 63 6f 6c 75 6d 6e 20 61 73 20 74  that column as t
23903 68 65 20 72 6f 77 69 64 2e 20 20 53 65 74 20 74  he rowid.  Set t
23904 68 65 20 54 61 62 6c 65 2e 69 50 4b 65 79 0a 2a  he Table.iPKey.*
23905 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 74  * field of the t
23906 61 62 6c 65 20 75 6e 64 65 72 20 63 6f 6e 73 74  able under const
23907 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 74 68  ruction to be th
23908 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a  e index of the.*
23909 2a 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  * INTEGER PRIMAR
2390a 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 2e 20 20 54  Y KEY column.  T
2390b 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20 73 65  able.iPKey is se
2390c 74 20 74 6f 20 2d 31 20 69 66 20 74 68 65 72 65  t to -1 if there
2390d 20 69 73 0a 2a 2a 20 6e 6f 20 49 4e 54 45 47 45   is.** no INTEGE
2390e 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2e 0a 2a  R PRIMARY KEY..*
2390f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6b 65 79 20  *.** If the key 
23910 69 73 20 6e 6f 74 20 61 6e 20 49 4e 54 45 47 45  is not an INTEGE
23911 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 74  R PRIMARY KEY, t
23912 68 65 6e 20 63 72 65 61 74 65 20 61 20 75 6e 69  hen create a uni
23913 71 75 65 0a 2a 2a 20 69 6e 64 65 78 20 66 6f 72  que.** index for
23914 20 74 68 65 20 6b 65 79 2e 20 20 4e 6f 20 69 6e   the key.  No in
23915 64 65 78 20 69 73 20 63 72 65 61 74 65 64 20 66  dex is created f
23916 6f 72 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  or INTEGER PRIMA
23917 52 59 20 4b 45 59 73 2e 0a 2a 2f 0a 53 51 4c 49  RY KEYs..*/.SQLI
23918 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
23919 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61 72  sqlite3AddPrimar
2391a 79 4b 65 79 28 0a 20 20 50 61 72 73 65 20 2a 70  yKey(.  Parse *p
2391b 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 50 61 72  Parse,    /* Par
2391c 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
2391d 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
2391e 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 66  t,  /* List of f
2391f 69 65 6c 64 20 6e 61 6d 65 73 20 74 6f 20 62 65  ield names to be
23920 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e   indexed */.  in
23921 74 20 6f 6e 45 72 72 6f 72 2c 20 20 20 20 20 20  t onError,      
23922 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69  /* What to do wi
23923 74 68 20 61 20 75 6e 69 71 75 65 6e 65 73 73 20  th a uniqueness 
23924 63 6f 6e 66 6c 69 63 74 20 2a 2f 0a 20 20 69 6e  conflict */.  in
23925 74 20 61 75 74 6f 49 6e 63 2c 20 20 20 20 20 20  t autoInc,      
23926 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 41  /* True if the A
23927 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 6b 65 79  UTOINCREMENT key
23928 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 20  word is present 
23929 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 4f 72 64  */.  int sortOrd
2392a 65 72 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45  er     /* SQLITE
2392b 5f 53 4f 5f 41 53 43 20 6f 72 20 53 51 4c 49 54  _SO_ASC or SQLIT
2392c 45 5f 53 4f 5f 44 45 53 43 20 2a 2f 0a 29 7b 0a  E_SO_DESC */.){.
2392d 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
2392e 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
2392f 65 3b 0a 20 20 63 68 61 72 20 2a 7a 54 79 70 65  e;.  char *zType
23930 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c   = 0;.  int iCol
23931 20 3d 20 2d 31 2c 20 69 3b 0a 20 20 69 66 28 20   = -1, i;.  if( 
23932 70 54 61 62 3d 3d 30 20 7c 7c 20 49 4e 5f 44 45  pTab==0 || IN_DE
23933 43 4c 41 52 45 5f 56 54 41 42 20 29 20 67 6f 74  CLARE_VTAB ) got
23934 6f 20 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78  o primary_key_ex
23935 69 74 3b 0a 20 20 69 66 28 20 70 54 61 62 2d 3e  it;.  if( pTab->
23936 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 48 61  tabFlags & TF_Ha
23937 73 50 72 69 6d 61 72 79 4b 65 79 20 29 7b 0a 20  sPrimaryKey ){. 
23938 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
23939 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  sg(pParse, .    
2393a 20 20 22 74 61 62 6c 65 20 5c 22 25 73 5c 22 20    "table \"%s\" 
2393b 68 61 73 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  has more than on
2393c 65 20 70 72 69 6d 61 72 79 20 6b 65 79 22 2c 20  e primary key", 
2393d 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pTab->zName);.  
2393e 20 20 67 6f 74 6f 20 70 72 69 6d 61 72 79 5f 6b    goto primary_k
2393f 65 79 5f 65 78 69 74 3b 0a 20 20 7d 0a 20 20 70  ey_exit;.  }.  p
23940 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d  Tab->tabFlags |=
23941 20 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b 65   TF_HasPrimaryKe
23942 79 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d  y;.  if( pList==
23943 30 20 29 7b 0a 20 20 20 20 69 43 6f 6c 20 3d 20  0 ){.    iCol = 
23944 70 54 61 62 2d 3e 6e 43 6f 6c 20 2d 20 31 3b 0a  pTab->nCol - 1;.
23945 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69      pTab->aCol[i
23946 43 6f 6c 5d 2e 69 73 50 72 69 6d 4b 65 79 20 3d  Col].isPrimKey =
23947 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   1;.  }else{.   
23948 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73   for(i=0; i<pLis
23949 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  t->nExpr; i++){.
2394a 20 20 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30        for(iCol=0
2394b 3b 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f  ; iCol<pTab->nCo
2394c 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20  l; iCol++){.    
2394d 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
2394e 74 72 49 43 6d 70 28 70 4c 69 73 74 2d 3e 61 5b  trICmp(pList->a[
2394f 69 5d 2e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e  i].zName, pTab->
23950 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65  aCol[iCol].zName
23951 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
23952 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
23953 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
23954 20 69 66 28 20 69 43 6f 6c 3c 70 54 61 62 2d 3e   if( iCol<pTab->
23955 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  nCol ){.        
23956 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d  pTab->aCol[iCol]
23957 2e 69 73 50 72 69 6d 4b 65 79 20 3d 20 31 3b 0a  .isPrimKey = 1;.
23958 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
23959 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e 45 78    if( pList->nEx
2395a 70 72 3e 31 20 29 20 69 43 6f 6c 20 3d 20 2d 31  pr>1 ) iCol = -1
2395b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 43 6f 6c  ;.  }.  if( iCol
2395c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 54 61 62  >=0 && iCol<pTab
2395d 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 7a 54  ->nCol ){.    zT
2395e 79 70 65 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c  ype = pTab->aCol
2395f 5b 69 43 6f 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20  [iCol].zType;.  
23960 7d 0a 20 20 69 66 28 20 7a 54 79 70 65 20 26 26  }.  if( zType &&
23961 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
23962 7a 54 79 70 65 2c 20 22 49 4e 54 45 47 45 52 22  zType, "INTEGER"
23963 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 26 26 20  )==0.        && 
23964 73 6f 72 74 4f 72 64 65 72 3d 3d 53 51 4c 49 54  sortOrder==SQLIT
23965 45 5f 53 4f 5f 41 53 43 20 29 7b 0a 20 20 20 20  E_SO_ASC ){.    
23966 70 54 61 62 2d 3e 69 50 4b 65 79 20 3d 20 69 43  pTab->iPKey = iC
23967 6f 6c 3b 0a 20 20 20 20 70 54 61 62 2d 3e 6b 65  ol;.    pTab->ke
23968 79 43 6f 6e 66 20 3d 20 28 75 38 29 6f 6e 45 72  yConf = (u8)onEr
23969 72 6f 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ror;.    assert(
2396a 20 61 75 74 6f 49 6e 63 3d 3d 30 20 7c 7c 20 61   autoInc==0 || a
2396b 75 74 6f 49 6e 63 3d 3d 31 20 29 3b 0a 20 20 20  utoInc==1 );.   
2396c 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20   pTab->tabFlags 
2396d 7c 3d 20 61 75 74 6f 49 6e 63 2a 54 46 5f 41 75  |= autoInc*TF_Au
2396e 74 6f 69 6e 63 72 65 6d 65 6e 74 3b 0a 20 20 7d  toincrement;.  }
2396f 65 6c 73 65 20 69 66 28 20 61 75 74 6f 49 6e 63  else if( autoInc
23970 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   ){.#ifndef SQLI
23971 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52  TE_OMIT_AUTOINCR
23972 45 4d 45 4e 54 0a 20 20 20 20 73 71 6c 69 74 65  EMENT.    sqlite
23973 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
23974 2c 20 22 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54  , "AUTOINCREMENT
23975 20 69 73 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64   is only allowed
23976 20 6f 6e 20 61 6e 20 22 0a 20 20 20 20 20 20 20   on an ".       
23977 22 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59  "INTEGER PRIMARY
23978 20 4b 45 59 22 29 3b 0a 23 65 6e 64 69 66 0a 20   KEY");.#endif. 
23979 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
2397a 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 28 70  te3CreateIndex(p
2397b 50 61 72 73 65 2c 20 30 2c 20 30 2c 20 30 2c 20  Parse, 0, 0, 0, 
2397c 70 4c 69 73 74 2c 20 6f 6e 45 72 72 6f 72 2c 20  pList, onError, 
2397d 30 2c 20 30 2c 20 73 6f 72 74 4f 72 64 65 72 2c  0, 0, sortOrder,
2397e 20 30 29 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d   0);.    pList =
2397f 20 30 3b 0a 20 20 7d 0a 0a 70 72 69 6d 61 72 79   0;.  }..primary
23980 5f 6b 65 79 5f 65 78 69 74 3a 0a 20 20 73 71 6c  _key_exit:.  sql
23981 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
23982 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  te(pParse->db, p
23983 4c 69 73 74 29 3b 0a 20 20 72 65 74 75 72 6e 3b  List);.  return;
23984 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20  .}../*.** Add a 
23985 6e 65 77 20 43 48 45 43 4b 20 63 6f 6e 73 74 72  new CHECK constr
23986 61 69 6e 74 20 74 6f 20 74 68 65 20 74 61 62 6c  aint to the tabl
23987 65 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65  e currently unde
23988 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  r construction..
23989 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2398a 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64  E void sqlite3Ad
2398b 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e 74  dCheckConstraint
2398c 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2398d 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67  e,    /* Parsing
2398e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78   context */.  Ex
2398f 70 72 20 2a 70 43 68 65 63 6b 45 78 70 72 20 20  pr *pCheckExpr  
23990 2f 2a 20 54 68 65 20 63 68 65 63 6b 20 65 78 70  /* The check exp
23991 72 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20  ression */.){.  
23992 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
23993 61 72 73 65 2d 3e 64 62 3b 0a 23 69 66 6e 64 65  arse->db;.#ifnde
23994 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48  f SQLITE_OMIT_CH
23995 45 43 4b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  ECK.  Table *pTa
23996 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  b = pParse->pNew
23997 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 54 61  Table;.  if( pTa
23998 62 20 26 26 20 21 49 4e 5f 44 45 43 4c 41 52 45  b && !IN_DECLARE
23999 5f 56 54 41 42 20 29 7b 0a 20 20 20 20 70 54 61  _VTAB ){.    pTa
2399a 62 2d 3e 70 43 68 65 63 6b 20 3d 20 73 71 6c 69  b->pCheck = sqli
2399b 74 65 33 45 78 70 72 41 6e 64 28 64 62 2c 20 70  te3ExprAnd(db, p
2399c 54 61 62 2d 3e 70 43 68 65 63 6b 2c 20 70 43 68  Tab->pCheck, pCh
2399d 65 63 6b 45 78 70 72 29 3b 0a 20 20 7d 65 6c 73  eckExpr);.  }els
2399e 65 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20  e.#endif.  {.   
2399f 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
239a0 74 65 28 64 62 2c 20 70 43 68 65 63 6b 45 78 70  te(db, pCheckExp
239a1 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  r);.  }.}../*.**
239a2 20 53 65 74 20 74 68 65 20 63 6f 6c 6c 61 74 69   Set the collati
239a3 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 6f 66 20 74  on function of t
239a4 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  he most recently
239a5 20 70 61 72 73 65 64 20 74 61 62 6c 65 20 63 6f   parsed table co
239a6 6c 75 6d 6e 0a 2a 2a 20 74 6f 20 74 68 65 20 43  lumn.** to the C
239a7 6f 6c 6c 53 65 71 20 67 69 76 65 6e 2e 0a 2a 2f  ollSeq given..*/
239a8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
239a9 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 43  void sqlite3AddC
239aa 6f 6c 6c 61 74 65 54 79 70 65 28 50 61 72 73 65  ollateType(Parse
239ab 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20   *pParse, Token 
239ac 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 54 61 62 6c  *pToken){.  Tabl
239ad 65 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  e *p;.  int i;. 
239ae 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20 20 20   char *zColl;   
239af 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65             /* De
239b0 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 63  quoted name of c
239b1 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
239b2 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  e */.  sqlite3 *
239b3 64 62 3b 0a 0a 20 20 69 66 28 20 28 70 20 3d 20  db;..  if( (p = 
239b4 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
239b5 65 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  e)==0 ) return;.
239b6 20 20 69 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b    i = p->nCol-1;
239b7 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  .  db = pParse->
239b8 64 62 3b 0a 20 20 7a 43 6f 6c 6c 20 3d 20 73 71  db;.  zColl = sq
239b9 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
239ba 65 6e 28 64 62 2c 20 70 54 6f 6b 65 6e 29 3b 0a  en(db, pToken);.
239bb 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20 29 20 72    if( !zColl ) r
239bc 65 74 75 72 6e 3b 0a 0a 20 20 69 66 28 20 73 71  eturn;..  if( sq
239bd 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53  lite3LocateCollS
239be 65 71 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c  eq(pParse, zColl
239bf 29 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a  ) ){.    Index *
239c0 70 49 64 78 3b 0a 20 20 20 20 70 2d 3e 61 43 6f  pIdx;.    p->aCo
239c1 6c 5b 69 5d 2e 7a 43 6f 6c 6c 20 3d 20 7a 43 6f  l[i].zColl = zCo
239c2 6c 6c 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66  ll;.  .    /* If
239c3 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 64   the column is d
239c4 65 63 6c 61 72 65 64 20 61 73 20 22 3c 6e 61 6d  eclared as "<nam
239c5 65 3e 20 50 52 49 4d 41 52 59 20 4b 45 59 20 43  e> PRIMARY KEY C
239c6 4f 4c 4c 41 54 45 20 3c 74 79 70 65 3e 22 2c 0a  OLLATE <type>",.
239c7 20 20 20 20 2a 2a 20 74 68 65 6e 20 61 6e 20 69      ** then an i
239c8 6e 64 65 78 20 6d 61 79 20 68 61 76 65 20 62 65  ndex may have be
239c9 65 6e 20 63 72 65 61 74 65 64 20 6f 6e 20 74 68  en created on th
239ca 69 73 20 63 6f 6c 75 6d 6e 20 62 65 66 6f 72 65  is column before
239cb 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c 6c   the.    ** coll
239cc 61 74 69 6f 6e 20 74 79 70 65 20 77 61 73 20 61  ation type was a
239cd 64 64 65 64 2e 20 43 6f 72 72 65 63 74 20 74 68  dded. Correct th
239ce 69 73 20 69 66 20 69 74 20 69 73 20 74 68 65 20  is if it is the 
239cf 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  case..    */.   
239d0 20 66 6f 72 28 70 49 64 78 3d 70 2d 3e 70 49 6e   for(pIdx=p->pIn
239d1 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
239d2 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
239d3 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78      assert( pIdx
239d4 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a  ->nColumn==1 );.
239d5 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e        if( pIdx->
239d6 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 69 20 29  aiColumn[0]==i )
239d7 7b 0a 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e  {.        pIdx->
239d8 61 7a 43 6f 6c 6c 5b 30 5d 20 3d 20 70 2d 3e 61  azColl[0] = p->a
239d9 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20  Col[i].zColl;.  
239da 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
239db 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
239dc 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c  DbFree(db, zColl
239dd 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
239de 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
239df 74 75 72 6e 73 20 74 68 65 20 63 6f 6c 6c 61 74  turns the collat
239e0 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 6f 72  ion sequence for
239e1 20 64 61 74 61 62 61 73 65 20 6e 61 74 69 76 65   database native
239e2 20 74 65 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e   text.** encodin
239e3 67 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20  g identified by 
239e4 74 68 65 20 73 74 72 69 6e 67 20 7a 4e 61 6d 65  the string zName
239e5 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a  , length nName..
239e6 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 71  **.** If the req
239e7 75 65 73 74 65 64 20 63 6f 6c 6c 61 74 69 6f 6e  uested collation
239e8 20 73 65 71 75 65 6e 63 65 20 69 73 20 6e 6f 74   sequence is not
239e9 20 61 76 61 69 6c 61 62 6c 65 2c 20 6f 72 20 6e   available, or n
239ea 6f 74 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20  ot available.** 
239eb 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
239ec 6e 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 2c  native encoding,
239ed 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66   the collation f
239ee 61 63 74 6f 72 79 20 69 73 20 69 6e 76 6f 6b 65  actory is invoke
239ef 64 20 74 6f 0a 2a 2a 20 72 65 71 75 65 73 74 20  d to.** request 
239f0 69 74 2e 20 49 66 20 74 68 65 20 63 6f 6c 6c 61  it. If the colla
239f1 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 64 6f 65  tion factory doe
239f2 73 20 6e 6f 74 20 73 75 70 70 6c 79 20 73 75 63  s not supply suc
239f3 68 20 61 20 73 65 71 75 65 6e 63 65 2c 0a 2a 2a  h a sequence,.**
239f4 20 61 6e 64 20 74 68 65 20 73 65 71 75 65 6e 63   and the sequenc
239f5 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 69  e is available i
239f6 6e 20 61 6e 6f 74 68 65 72 20 74 65 78 74 20 65  n another text e
239f7 6e 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20 74 68  ncoding, then th
239f8 61 74 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65  at is.** returne
239f9 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a  d instead..**.**
239fa 20 49 66 20 6e 6f 20 76 65 72 73 69 6f 6e 73 20   If no versions 
239fb 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64  of the requested
239fc 20 63 6f 6c 6c 61 74 69 6f 6e 73 20 73 65 71 75   collations sequ
239fd 65 6e 63 65 20 61 72 65 20 61 76 61 69 6c 61 62  ence are availab
239fe 6c 65 2c 20 6f 72 0a 2a 2a 20 61 6e 6f 74 68 65  le, or.** anothe
239ff 72 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  r error occurs, 
23a00 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64  NULL is returned
23a01 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65   and an error me
23a02 73 73 61 67 65 20 77 72 69 74 74 65 6e 20 69 6e  ssage written in
23a03 74 6f 0a 2a 2a 20 70 50 61 72 73 65 2e 0a 2a 2a  to.** pParse..**
23a04 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
23a05 20 69 73 20 61 20 77 72 61 70 70 65 72 20 61 72   is a wrapper ar
23a06 6f 75 6e 64 20 73 71 6c 69 74 65 33 46 69 6e 64  ound sqlite3Find
23a07 43 6f 6c 6c 53 65 71 28 29 2e 20 20 54 68 69 73  CollSeq().  This
23a08 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 6e 76 6f   routine.** invo
23a09 6b 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f  kes the collatio
23a0a 6e 20 66 61 63 74 6f 72 79 20 69 66 20 74 68 65  n factory if the
23a0b 20 6e 61 6d 65 64 20 63 6f 6c 6c 61 74 69 6f 6e   named collation
23a0c 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64   cannot be found
23a0d 0a 2a 2a 20 61 6e 64 20 67 65 6e 65 72 61 74 65  .** and generate
23a0e 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  s an error messa
23a0f 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  ge..**.** See al
23a10 73 6f 3a 20 73 71 6c 69 74 65 33 46 69 6e 64 43  so: sqlite3FindC
23a11 6f 6c 6c 53 65 71 28 29 2c 20 73 71 6c 69 74 65  ollSeq(), sqlite
23a12 33 47 65 74 43 6f 6c 6c 53 65 71 28 29 0a 2a 2f  3GetCollSeq().*/
23a13 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
23a14 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33  CollSeq *sqlite3
23a15 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 50 61  LocateCollSeq(Pa
23a16 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e  rse *pParse, con
23a17 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b  st char *zName){
23a18 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
23a19 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 75   pParse->db;.  u
23a1a 38 20 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b  8 enc = ENC(db);
23a1b 0a 20 20 75 38 20 69 6e 69 74 62 75 73 79 20 3d  .  u8 initbusy =
23a1c 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3b 0a   db->init.busy;.
23a1d 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
23a1e 3b 0a 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c  ;..  pColl = sql
23a1f 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28  ite3FindCollSeq(
23a20 64 62 2c 20 65 6e 63 2c 20 7a 4e 61 6d 65 2c 20  db, enc, zName, 
23a21 69 6e 69 74 62 75 73 79 29 3b 0a 20 20 69 66 28  initbusy);.  if(
23a22 20 21 69 6e 69 74 62 75 73 79 20 26 26 20 28 21   !initbusy && (!
23a23 70 43 6f 6c 6c 20 7c 7c 20 21 70 43 6f 6c 6c 2d  pColl || !pColl-
23a24 3e 78 43 6d 70 29 20 29 7b 0a 20 20 20 20 70 43  >xCmp) ){.    pC
23a25 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74  oll = sqlite3Get
23a26 43 6f 6c 6c 53 65 71 28 64 62 2c 20 65 6e 63 2c  CollSeq(db, enc,
23a27 20 70 43 6f 6c 6c 2c 20 7a 4e 61 6d 65 29 3b 0a   pColl, zName);.
23a28 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29      if( !pColl )
23a29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
23a2a 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
23a2b 22 6e 6f 20 73 75 63 68 20 63 6f 6c 6c 61 74 69  "no such collati
23a2c 6f 6e 20 73 65 71 75 65 6e 63 65 3a 20 25 73 22  on sequence: %s"
23a2d 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a  , zName);.    }.
23a2e 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 43    }..  return pC
23a2f 6f 6c 6c 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47  oll;.}.../*.** G
23a30 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
23a31 74 20 77 69 6c 6c 20 69 6e 63 72 65 6d 65 6e 74  t will increment
23a32 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b   the schema cook
23a33 69 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 63  ie..**.** The sc
23a34 68 65 6d 61 20 63 6f 6f 6b 69 65 20 69 73 20 75  hema cookie is u
23a35 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  sed to determine
23a36 20 77 68 65 6e 20 74 68 65 20 73 63 68 65 6d 61   when the schema
23a37 20 66 6f 72 20 74 68 65 0a 2a 2a 20 64 61 74 61   for the.** data
23a38 62 61 73 65 20 63 68 61 6e 67 65 73 2e 20 20 41  base changes.  A
23a39 66 74 65 72 20 65 61 63 68 20 73 63 68 65 6d 61  fter each schema
23a3a 20 63 68 61 6e 67 65 2c 20 74 68 65 20 63 6f 6f   change, the coo
23a3b 6b 69 65 20 76 61 6c 75 65 0a 2a 2a 20 63 68 61  kie value.** cha
23a3c 6e 67 65 73 2e 20 20 57 68 65 6e 20 61 20 70 72  nges.  When a pr
23a3d 6f 63 65 73 73 20 66 69 72 73 74 20 72 65 61 64  ocess first read
23a3e 73 20 74 68 65 20 73 63 68 65 6d 61 20 69 74 20  s the schema it 
23a3f 72 65 63 6f 72 64 73 20 74 68 65 0a 2a 2a 20 63  records the.** c
23a40 6f 6f 6b 69 65 2e 20 20 54 68 65 72 65 61 66 74  ookie.  Thereaft
23a41 65 72 2c 20 77 68 65 6e 65 76 65 72 20 69 74 20  er, whenever it 
23a42 67 6f 65 73 20 74 6f 20 61 63 63 65 73 73 20 74  goes to access t
23a43 68 65 20 64 61 74 61 62 61 73 65 2c 0a 2a 2a 20  he database,.** 
23a44 69 74 20 63 68 65 63 6b 73 20 74 68 65 20 63 6f  it checks the co
23a45 6f 6b 69 65 20 74 6f 20 6d 61 6b 65 20 73 75 72  okie to make sur
23a46 65 20 74 68 65 20 73 63 68 65 6d 61 20 68 61 73  e the schema has
23a47 20 6e 6f 74 20 63 68 61 6e 67 65 64 0a 2a 2a 20   not changed.** 
23a48 73 69 6e 63 65 20 69 74 20 77 61 73 20 6c 61 73  since it was las
23a49 74 20 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  t read..**.** Th
23a4a 69 73 20 70 6c 61 6e 20 69 73 20 6e 6f 74 20 63  is plan is not c
23a4b 6f 6d 70 6c 65 74 65 6c 79 20 62 75 6c 6c 65 74  ompletely bullet
23a4c 2d 70 72 6f 6f 66 2e 20 20 49 74 20 69 73 20 70  -proof.  It is p
23a4d 6f 73 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 74  ossible for.** t
23a4e 68 65 20 73 63 68 65 6d 61 20 74 6f 20 63 68 61  he schema to cha
23a4f 6e 67 65 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d  nge multiple tim
23a50 65 73 20 61 6e 64 20 66 6f 72 20 74 68 65 20 63  es and for the c
23a51 6f 6f 6b 69 65 20 74 6f 20 62 65 0a 2a 2a 20 73  ookie to be.** s
23a52 65 74 20 62 61 63 6b 20 74 6f 20 70 72 69 6f 72  et back to prior
23a53 20 76 61 6c 75 65 2e 20 20 42 75 74 20 73 63 68   value.  But sch
23a54 65 6d 61 20 63 68 61 6e 67 65 73 20 61 72 65 20  ema changes are 
23a55 69 6e 66 72 65 71 75 65 6e 74 0a 2a 2a 20 61 6e  infrequent.** an
23a56 64 20 74 68 65 20 70 72 6f 62 61 62 69 6c 69 74  d the probabilit
23a57 79 20 6f 66 20 68 69 74 74 69 6e 67 20 74 68 65  y of hitting the
23a58 20 73 61 6d 65 20 63 6f 6f 6b 69 65 20 76 61 6c   same cookie val
23a59 75 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 31 20  ue is only.** 1 
23a5a 63 68 61 6e 63 65 20 69 6e 20 32 5e 33 32 2e 20  chance in 2^32. 
23a5b 20 53 6f 20 77 65 27 72 65 20 73 61 66 65 20 65   So we're safe e
23a5c 6e 6f 75 67 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45  nough..*/.SQLITE
23a5d 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
23a5e 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69  lite3ChangeCooki
23a5f 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
23a60 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 69 6e 74   int iDb){.  int
23a61 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
23a62 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
23a63 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
23a64 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 56   pParse->db;.  V
23a65 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
23a66 3e 70 56 64 62 65 3b 0a 20 20 73 71 6c 69 74 65  >pVdbe;.  sqlite
23a67 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
23a68 50 5f 49 6e 74 65 67 65 72 2c 20 64 62 2d 3e 61  P_Integer, db->a
23a69 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d  Db[iDb].pSchema-
23a6a 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 2b 31  >schema_cookie+1
23a6b 2c 20 72 31 29 3b 0a 20 20 73 71 6c 69 74 65 33  , r1);.  sqlite3
23a6c 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
23a6d 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c  _SetCookie, iDb,
23a6e 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 45   BTREE_SCHEMA_VE
23a6f 52 53 49 4f 4e 2c 20 72 31 29 3b 0a 20 20 73 71  RSION, r1);.  sq
23a70 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
23a71 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
23a72 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 61 73 75 72  .}../*.** Measur
23a73 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
23a74 63 68 61 72 61 63 74 65 72 73 20 6e 65 65 64 65  characters neede
23a75 64 20 74 6f 20 6f 75 74 70 75 74 20 74 68 65 20  d to output the 
23a76 67 69 76 65 6e 0a 2a 2a 20 69 64 65 6e 74 69 66  given.** identif
23a77 69 65 72 2e 20 20 54 68 65 20 6e 75 6d 62 65 72  ier.  The number
23a78 20 72 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64   returned includ
23a79 65 73 20 61 6e 79 20 71 75 6f 74 65 73 20 75 73  es any quotes us
23a7a 65 64 0a 2a 2a 20 62 75 74 20 64 6f 65 73 20 6e  ed.** but does n
23a7b 6f 74 20 69 6e 63 6c 75 64 65 20 74 68 65 20 6e  ot include the n
23a7c 75 6c 6c 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a  ull terminator..
23a7d 2a 2a 0a 2a 2a 20 54 68 65 20 65 73 74 69 6d 61  **.** The estima
23a7e 74 65 20 69 73 20 63 6f 6e 73 65 72 76 61 74 69  te is conservati
23a7f 76 65 2e 20 20 49 74 20 6d 69 67 68 74 20 62 65  ve.  It might be
23a80 20 6c 61 72 67 65 72 20 74 68 61 74 20 77 68 61   larger that wha
23a81 74 20 69 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 6e  t is.** really n
23a82 65 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  eeded..*/.static
23a83 20 69 6e 74 20 69 64 65 6e 74 4c 65 6e 67 74 68   int identLength
23a84 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
23a85 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 66 6f 72 28  .  int n;.  for(
23a86 6e 3d 30 3b 20 2a 7a 3b 20 6e 2b 2b 2c 20 7a 2b  n=0; *z; n++, z+
23a87 2b 29 7b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d  +){.    if( *z==
23a88 27 22 27 20 29 7b 20 6e 2b 2b 3b 20 7d 0a 20 20  '"' ){ n++; }.  
23a89 7d 0a 20 20 72 65 74 75 72 6e 20 6e 20 2b 20 32  }.  return n + 2
23a8a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  ;.}../*.** The f
23a8b 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69  irst parameter i
23a8c 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
23a8d 6e 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2e  n output buffer.
23a8e 20 54 68 65 20 73 65 63 6f 6e 64 20 0a 2a 2a 20   The second .** 
23a8f 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 70  parameter is a p
23a90 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 74  ointer to an int
23a91 65 67 65 72 20 74 68 61 74 20 63 6f 6e 74 61 69  eger that contai
23a92 6e 73 20 74 68 65 20 6f 66 66 73 65 74 20 61 74  ns the offset at
23a93 0a 2a 2a 20 77 68 69 63 68 20 74 6f 20 77 72 69  .** which to wri
23a94 74 65 20 69 6e 74 6f 20 74 68 65 20 6f 75 74 70  te into the outp
23a95 75 74 20 62 75 66 66 65 72 2e 20 54 68 69 73 20  ut buffer. This 
23a96 66 75 6e 63 74 69 6f 6e 20 63 6f 70 69 65 73 20  function copies 
23a97 74 68 65 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69  the.** nul-termi
23a98 6e 61 74 65 64 20 73 74 72 69 6e 67 20 70 6f 69  nated string poi
23a99 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 74  nted to by the t
23a9a 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 2c 20  hird parameter, 
23a9b 7a 53 69 67 6e 65 64 49 64 65 6e 74 2c 0a 2a 2a  zSignedIdent,.**
23a9c 20 74 6f 20 74 68 65 20 73 70 65 63 69 66 69 65   to the specifie
23a9d 64 20 6f 66 66 73 65 74 20 69 6e 20 74 68 65 20  d offset in the 
23a9e 62 75 66 66 65 72 20 61 6e 64 20 75 70 64 61 74  buffer and updat
23a9f 65 73 20 2a 70 49 64 78 20 74 6f 20 72 65 66 65  es *pIdx to refe
23aa0 72 0a 2a 2a 20 74 6f 20 74 68 65 20 66 69 72 73  r.** to the firs
23aa1 74 20 62 79 74 65 20 61 66 74 65 72 20 74 68 65  t byte after the
23aa2 20 6c 61 73 74 20 62 79 74 65 20 77 72 69 74 74   last byte writt
23aa3 65 6e 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  en before return
23aa4 69 6e 67 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74  ing..** .** If t
23aa5 68 65 20 73 74 72 69 6e 67 20 7a 53 69 67 6e 65  he string zSigne
23aa6 64 49 64 65 6e 74 20 63 6f 6e 73 69 73 74 73 20  dIdent consists 
23aa7 65 6e 74 69 72 65 6c 79 20 6f 66 20 61 6c 70 68  entirely of alph
23aa8 61 2d 6e 75 6d 65 72 69 63 0a 2a 2a 20 63 68 61  a-numeric.** cha
23aa9 72 61 63 74 65 72 73 2c 20 64 6f 65 73 20 6e 6f  racters, does no
23aaa 74 20 62 65 67 69 6e 20 77 69 74 68 20 61 20 64  t begin with a d
23aab 69 67 69 74 20 61 6e 64 20 69 73 20 6e 6f 74 20  igit and is not 
23aac 61 6e 20 53 51 4c 20 6b 65 79 77 6f 72 64 2c 0a  an SQL keyword,.
23aad 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20 63 6f  ** then it is co
23aae 70 69 65 64 20 74 6f 20 74 68 65 20 6f 75 74 70  pied to the outp
23aaf 75 74 20 62 75 66 66 65 72 20 65 78 61 63 74 6c  ut buffer exactl
23ab0 79 20 61 73 20 69 74 20 69 73 2e 20 4f 74 68 65  y as it is. Othe
23ab1 72 77 69 73 65 2c 0a 2a 2a 20 69 74 20 69 73 20  rwise,.** it is 
23ab2 71 75 6f 74 65 64 20 75 73 69 6e 67 20 64 6f 75  quoted using dou
23ab3 62 6c 65 2d 71 75 6f 74 65 73 2e 0a 2a 2f 0a 73  ble-quotes..*/.s
23ab4 74 61 74 69 63 20 76 6f 69 64 20 69 64 65 6e 74  tatic void ident
23ab5 50 75 74 28 63 68 61 72 20 2a 7a 2c 20 69 6e 74  Put(char *z, int
23ab6 20 2a 70 49 64 78 2c 20 63 68 61 72 20 2a 7a 53   *pIdx, char *zS
23ab7 69 67 6e 65 64 49 64 65 6e 74 29 7b 0a 20 20 75  ignedIdent){.  u
23ab8 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49  nsigned char *zI
23ab9 64 65 6e 74 20 3d 20 28 75 6e 73 69 67 6e 65 64  dent = (unsigned
23aba 20 63 68 61 72 2a 29 7a 53 69 67 6e 65 64 49 64   char*)zSignedId
23abb 65 6e 74 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c  ent;.  int i, j,
23abc 20 6e 65 65 64 51 75 6f 74 65 3b 0a 20 20 69 20   needQuote;.  i 
23abd 3d 20 2a 70 49 64 78 3b 0a 0a 20 20 66 6f 72 28  = *pIdx;..  for(
23abe 6a 3d 30 3b 20 7a 49 64 65 6e 74 5b 6a 5d 3b 20  j=0; zIdent[j]; 
23abf 6a 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 21 73  j++){.    if( !s
23ac0 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28 7a 49  qlite3Isalnum(zI
23ac1 64 65 6e 74 5b 6a 5d 29 20 26 26 20 7a 49 64 65  dent[j]) && zIde
23ac2 6e 74 5b 6a 5d 21 3d 27 5f 27 20 29 20 62 72 65  nt[j]!='_' ) bre
23ac3 61 6b 3b 0a 20 20 7d 0a 20 20 6e 65 65 64 51 75  ak;.  }.  needQu
23ac4 6f 74 65 20 3d 20 73 71 6c 69 74 65 33 49 73 64  ote = sqlite3Isd
23ac5 69 67 69 74 28 7a 49 64 65 6e 74 5b 30 5d 29 20  igit(zIdent[0]) 
23ac6 7c 7c 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72  || sqlite3Keywor
23ac7 64 43 6f 64 65 28 7a 49 64 65 6e 74 2c 20 6a 29  dCode(zIdent, j)
23ac8 21 3d 54 4b 5f 49 44 3b 0a 20 20 69 66 28 20 21  !=TK_ID;.  if( !
23ac9 6e 65 65 64 51 75 6f 74 65 20 29 7b 0a 20 20 20  needQuote ){.   
23aca 20 6e 65 65 64 51 75 6f 74 65 20 3d 20 7a 49 64   needQuote = zId
23acb 65 6e 74 5b 6a 5d 3b 0a 20 20 7d 0a 0a 20 20 69  ent[j];.  }..  i
23acc 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20 7a  f( needQuote ) z
23acd 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a 20 20 66  [i++] = '"';.  f
23ace 6f 72 28 6a 3d 30 3b 20 7a 49 64 65 6e 74 5b 6a  or(j=0; zIdent[j
23acf 5d 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 7a 5b 69  ]; j++){.    z[i
23ad0 2b 2b 5d 20 3d 20 7a 49 64 65 6e 74 5b 6a 5d 3b  ++] = zIdent[j];
23ad1 0a 20 20 20 20 69 66 28 20 7a 49 64 65 6e 74 5b  .    if( zIdent[
23ad2 6a 5d 3d 3d 27 22 27 20 29 20 7a 5b 69 2b 2b 5d  j]=='"' ) z[i++]
23ad3 20 3d 20 27 22 27 3b 0a 20 20 7d 0a 20 20 69 66   = '"';.  }.  if
23ad4 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20 7a 5b  ( needQuote ) z[
23ad5 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a 20 20 7a 5b  i++] = '"';.  z[
23ad6 69 5d 20 3d 20 30 3b 0a 20 20 2a 70 49 64 78 20  i] = 0;.  *pIdx 
23ad7 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  = i;.}../*.** Ge
23ad8 6e 65 72 61 74 65 20 61 20 43 52 45 41 54 45 20  nerate a CREATE 
23ad9 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20  TABLE statement 
23ada 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20  appropriate for 
23adb 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 74 61 62  the given.** tab
23adc 6c 65 2e 20 20 4d 65 6d 6f 72 79 20 74 6f 20 68  le.  Memory to h
23add 6f 6c 64 20 74 68 65 20 74 65 78 74 20 6f 66 20  old the text of 
23ade 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73  the statement is
23adf 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f   obtained.** fro
23ae0 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29  m sqliteMalloc()
23ae1 20 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65   and must be fre
23ae2 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e  ed by the callin
23ae3 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  g function..*/.s
23ae4 74 61 74 69 63 20 63 68 61 72 20 2a 63 72 65 61  tatic char *crea
23ae5 74 65 54 61 62 6c 65 53 74 6d 74 28 73 71 6c 69  teTableStmt(sqli
23ae6 74 65 33 20 2a 64 62 2c 20 54 61 62 6c 65 20 2a  te3 *db, Table *
23ae7 70 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6b 2c 20  p){.  int i, k, 
23ae8 6e 3b 0a 20 20 63 68 61 72 20 2a 7a 53 74 6d 74  n;.  char *zStmt
23ae9 3b 0a 20 20 63 68 61 72 20 2a 7a 53 65 70 2c 20  ;.  char *zSep, 
23aea 2a 7a 53 65 70 32 2c 20 2a 7a 45 6e 64 3b 0a 20  *zSep2, *zEnd;. 
23aeb 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20   Column *pCol;. 
23aec 20 6e 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 43   n = 0;.  for(pC
23aed 6f 6c 20 3d 20 70 2d 3e 61 43 6f 6c 2c 20 69 3d  ol = p->aCol, i=
23aee 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b  0; i<p->nCol; i+
23aef 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20  +, pCol++){.    
23af0 6e 20 2b 3d 20 69 64 65 6e 74 4c 65 6e 67 74 68  n += identLength
23af1 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 20 2b 20  (pCol->zName) + 
23af2 35 3b 0a 20 20 7d 0a 20 20 6e 20 2b 3d 20 69 64  5;.  }.  n += id
23af3 65 6e 74 4c 65 6e 67 74 68 28 70 2d 3e 7a 4e 61  entLength(p->zNa
23af4 6d 65 29 3b 0a 20 20 69 66 28 20 6e 3c 35 30 20  me);.  if( n<50 
23af5 29 7b 20 0a 20 20 20 20 7a 53 65 70 20 3d 20 22  ){ .    zSep = "
23af6 22 3b 0a 20 20 20 20 7a 53 65 70 32 20 3d 20 22  ";.    zSep2 = "
23af7 2c 22 3b 0a 20 20 20 20 7a 45 6e 64 20 3d 20 22  ,";.    zEnd = "
23af8 29 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  )";.  }else{.   
23af9 20 7a 53 65 70 20 3d 20 22 5c 6e 20 20 22 3b 0a   zSep = "\n  ";.
23afa 20 20 20 20 7a 53 65 70 32 20 3d 20 22 2c 5c 6e      zSep2 = ",\n
23afb 20 20 22 3b 0a 20 20 20 20 7a 45 6e 64 20 3d 20    ";.    zEnd = 
23afc 22 5c 6e 29 22 3b 0a 20 20 7d 0a 20 20 6e 20 2b  "\n)";.  }.  n +
23afd 3d 20 33 35 20 2b 20 36 2a 70 2d 3e 6e 43 6f 6c  = 35 + 6*p->nCol
23afe 3b 0a 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69  ;.  zStmt = sqli
23aff 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a  te3Malloc( n );.
23b00 20 20 69 66 28 20 7a 53 74 6d 74 3d 3d 30 20 29    if( zStmt==0 )
23b01 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63  {.    db->malloc
23b02 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
23b03 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
23b04 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
23b05 28 6e 2c 20 7a 53 74 6d 74 2c 20 22 43 52 45 41  (n, zStmt, "CREA
23b06 54 45 20 54 41 42 4c 45 20 22 29 3b 0a 20 20 6b  TE TABLE ");.  k
23b07 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
23b08 33 30 28 7a 53 74 6d 74 29 3b 0a 20 20 69 64 65  30(zStmt);.  ide
23b09 6e 74 50 75 74 28 7a 53 74 6d 74 2c 20 26 6b 2c  ntPut(zStmt, &k,
23b0a 20 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7a 53   p->zName);.  zS
23b0b 74 6d 74 5b 6b 2b 2b 5d 20 3d 20 27 28 27 3b 0a  tmt[k++] = '(';.
23b0c 20 20 66 6f 72 28 70 43 6f 6c 3d 70 2d 3e 61 43    for(pCol=p->aC
23b0d 6f 6c 2c 20 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43  ol, i=0; i<p->nC
23b0e 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29  ol; i++, pCol++)
23b0f 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e  {.    static con
23b10 73 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20  st char * const 
23b11 61 7a 54 79 70 65 5b 5d 20 3d 20 7b 0a 20 20 20  azType[] = {.   
23b12 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41       /* SQLITE_A
23b13 46 46 5f 54 45 58 54 20 20 20 20 2a 2f 20 22 20  FF_TEXT    */ " 
23b14 54 45 58 54 22 2c 0a 20 20 20 20 20 20 20 20 2f  TEXT",.        /
23b15 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  * SQLITE_AFF_NON
23b16 45 20 20 20 20 2a 2f 20 22 22 2c 0a 20 20 20 20  E    */ "",.    
23b17 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 46      /* SQLITE_AF
23b18 46 5f 4e 55 4d 45 52 49 43 20 2a 2f 20 22 20 4e  F_NUMERIC */ " N
23b19 55 4d 22 2c 0a 20 20 20 20 20 20 20 20 2f 2a 20  UM",.        /* 
23b1a 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47  SQLITE_AFF_INTEG
23b1b 45 52 20 2a 2f 20 22 20 49 4e 54 22 2c 0a 20 20  ER */ " INT",.  
23b1c 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f        /* SQLITE_
23b1d 41 46 46 5f 52 45 41 4c 20 20 20 20 2a 2f 20 22  AFF_REAL    */ "
23b1e 20 52 45 41 4c 22 0a 20 20 20 20 7d 3b 0a 20 20   REAL".    };.  
23b1f 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 63    int len;.    c
23b20 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65  onst char *zType
23b21 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  ;..    sqlite3_s
23b22 6e 70 72 69 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53  nprintf(n-k, &zS
23b23 74 6d 74 5b 6b 5d 2c 20 7a 53 65 70 29 3b 0a 20  tmt[k], zSep);. 
23b24 20 20 20 6b 20 2b 3d 20 73 71 6c 69 74 65 33 53     k += sqlite3S
23b25 74 72 6c 65 6e 33 30 28 26 7a 53 74 6d 74 5b 6b  trlen30(&zStmt[k
23b26 5d 29 3b 0a 20 20 20 20 7a 53 65 70 20 3d 20 7a  ]);.    zSep = z
23b27 53 65 70 32 3b 0a 20 20 20 20 69 64 65 6e 74 50  Sep2;.    identP
23b28 75 74 28 7a 53 74 6d 74 2c 20 26 6b 2c 20 70 43  ut(zStmt, &k, pC
23b29 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ol->zName);.    
23b2a 61 73 73 65 72 74 28 20 70 43 6f 6c 2d 3e 61 66  assert( pCol->af
23b2b 66 69 6e 69 74 79 2d 53 51 4c 49 54 45 5f 41 46  finity-SQLITE_AF
23b2c 46 5f 54 45 58 54 20 3e 3d 20 30 20 29 3b 0a 20  F_TEXT >= 0 );. 
23b2d 20 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c 2d     assert( pCol-
23b2e 3e 61 66 66 69 6e 69 74 79 2d 53 51 4c 49 54 45  >affinity-SQLITE
23b2f 5f 41 46 46 5f 54 45 58 54 20 3c 20 73 69 7a 65  _AFF_TEXT < size
23b30 6f 66 28 61 7a 54 79 70 65 29 2f 73 69 7a 65 6f  of(azType)/sizeo
23b31 66 28 61 7a 54 79 70 65 5b 30 5d 29 20 29 3b 0a  f(azType[0]) );.
23b32 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 43      testcase( pC
23b33 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53 51  ol->affinity==SQ
23b34 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 29 3b  LITE_AFF_TEXT );
23b35 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70  .    testcase( p
23b36 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53  Col->affinity==S
23b37 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29  QLITE_AFF_NONE )
23b38 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
23b39 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d  pCol->affinity==
23b3a 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52  SQLITE_AFF_NUMER
23b3b 49 43 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  IC );.    testca
23b3c 73 65 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69  se( pCol->affini
23b3d 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 49  ty==SQLITE_AFF_I
23b3e 4e 54 45 47 45 52 20 29 3b 0a 20 20 20 20 74 65  NTEGER );.    te
23b3f 73 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61 66  stcase( pCol->af
23b40 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41  finity==SQLITE_A
23b41 46 46 5f 52 45 41 4c 20 29 3b 0a 20 20 20 20 0a  FF_REAL );.    .
23b42 20 20 20 20 7a 54 79 70 65 20 3d 20 61 7a 54 79      zType = azTy
23b43 70 65 5b 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74  pe[pCol->affinit
23b44 79 20 2d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54  y - SQLITE_AFF_T
23b45 45 58 54 5d 3b 0a 20 20 20 20 6c 65 6e 20 3d 20  EXT];.    len = 
23b46 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
23b47 7a 54 79 70 65 29 3b 0a 20 20 20 20 61 73 73 65  zType);.    asse
23b48 72 74 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69  rt( pCol->affini
23b49 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ty==SQLITE_AFF_N
23b4a 4f 4e 45 20 0a 20 20 20 20 20 20 20 20 20 20 20  ONE .           
23b4b 20 7c 7c 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69   || pCol->affini
23b4c 74 79 3d 3d 73 71 6c 69 74 65 33 41 66 66 69 6e  ty==sqlite3Affin
23b4d 69 74 79 54 79 70 65 28 7a 54 79 70 65 29 20 29  ityType(zType) )
23b4e 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 53  ;.    memcpy(&zS
23b4f 74 6d 74 5b 6b 5d 2c 20 7a 54 79 70 65 2c 20 6c  tmt[k], zType, l
23b50 65 6e 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 6c 65  en);.    k += le
23b51 6e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6b  n;.    assert( k
23b52 3c 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  <=n );.  }.  sql
23b53 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 2d  ite3_snprintf(n-
23b54 6b 2c 20 26 7a 53 74 6d 74 5b 6b 5d 2c 20 22 25  k, &zStmt[k], "%
23b55 73 22 2c 20 7a 45 6e 64 29 3b 0a 20 20 72 65 74  s", zEnd);.  ret
23b56 75 72 6e 20 7a 53 74 6d 74 3b 0a 7d 0a 0a 2f 2a  urn zStmt;.}../*
23b57 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
23b58 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65   is called to re
23b59 70 6f 72 74 20 74 68 65 20 66 69 6e 61 6c 20 22  port the final "
23b5a 29 22 20 74 68 61 74 20 74 65 72 6d 69 6e 61 74  )" that terminat
23b5b 65 73 0a 2a 2a 20 61 20 43 52 45 41 54 45 20 54  es.** a CREATE T
23b5c 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a  ABLE statement..
23b5d 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20  **.** The table 
23b5e 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 6f  structure that o
23b5f 74 68 65 72 20 61 63 74 69 6f 6e 20 72 6f 75 74  ther action rout
23b60 69 6e 65 73 20 68 61 76 65 20 62 65 65 6e 20 62  ines have been b
23b61 75 69 6c 64 69 6e 67 0a 2a 2a 20 69 73 20 61 64  uilding.** is ad
23b62 64 65 64 20 74 6f 20 74 68 65 20 69 6e 74 65 72  ded to the inter
23b63 6e 61 6c 20 68 61 73 68 20 74 61 62 6c 65 73 2c  nal hash tables,
23b64 20 61 73 73 75 6d 69 6e 67 20 6e 6f 20 65 72 72   assuming no err
23b65 6f 72 73 20 68 61 76 65 0a 2a 2a 20 6f 63 63 75  ors have.** occu
23b66 72 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65  rred..**.** An e
23b67 6e 74 72 79 20 66 6f 72 20 74 68 65 20 74 61 62  ntry for the tab
23b68 6c 65 20 69 73 20 6d 61 64 65 20 69 6e 20 74 68  le is made in th
23b69 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 20 6f  e master table o
23b6a 6e 20 64 69 73 6b 2c 20 75 6e 6c 65 73 73 0a 2a  n disk, unless.*
23b6b 2a 20 74 68 69 73 20 69 73 20 61 20 74 65 6d 70  * this is a temp
23b6c 6f 72 61 72 79 20 74 61 62 6c 65 20 6f 72 20 64  orary table or d
23b6d 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 31 2e  b->init.busy==1.
23b6e 20 20 57 68 65 6e 20 64 62 2d 3e 69 6e 69 74 2e    When db->init.
23b6f 62 75 73 79 3d 3d 31 0a 2a 2a 20 69 74 20 6d 65  busy==1.** it me
23b70 61 6e 73 20 77 65 20 61 72 65 20 72 65 61 64 69  ans we are readi
23b71 6e 67 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  ng the sqlite_ma
23b72 73 74 65 72 20 74 61 62 6c 65 20 62 65 63 61 75  ster table becau
23b73 73 65 20 77 65 20 6a 75 73 74 0a 2a 2a 20 63 6f  se we just.** co
23b74 6e 6e 65 63 74 65 64 20 74 6f 20 74 68 65 20 64  nnected to the d
23b75 61 74 61 62 61 73 65 20 6f 72 20 62 65 63 61 75  atabase or becau
23b76 73 65 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  se the sqlite_ma
23b77 73 74 65 72 20 74 61 62 6c 65 20 68 61 73 0a 2a  ster table has.*
23b78 2a 20 72 65 63 65 6e 74 6c 79 20 63 68 61 6e 67  * recently chang
23b79 65 64 2c 20 73 6f 20 74 68 65 20 65 6e 74 72 79  ed, so the entry
23b7a 20 66 6f 72 20 74 68 69 73 20 74 61 62 6c 65 20   for this table 
23b7b 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 69  already exists i
23b7c 6e 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 5f  n.** the sqlite_
23b7d 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20 20 57  master table.  W
23b7e 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 6f  e do not want to
23b7f 20 63 72 65 61 74 65 20 69 74 20 61 67 61 69 6e   create it again
23b80 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70  ..**.** If the p
23b81 53 65 6c 65 63 74 20 61 72 67 75 6d 65 6e 74 20  Select argument 
23b82 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69 74 20  is not NULL, it 
23b83 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69 73 20  means that this 
23b84 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 61 73 20 63  routine.** was c
23b85 61 6c 6c 65 64 20 74 6f 20 63 72 65 61 74 65 20  alled to create 
23b86 61 20 74 61 62 6c 65 20 67 65 6e 65 72 61 74 65  a table generate
23b87 64 20 66 72 6f 6d 20 61 20 0a 2a 2a 20 22 43 52  d from a .** "CR
23b88 45 41 54 45 20 54 41 42 4c 45 20 2e 2e 2e 20 41  EATE TABLE ... A
23b89 53 20 53 45 4c 45 43 54 20 2e 2e 2e 22 20 73 74  S SELECT ..." st
23b8a 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 63 6f  atement.  The co
23b8b 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 66 0a 2a 2a  lumn names of.**
23b8c 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 77   the new table w
23b8d 69 6c 6c 20 6d 61 74 63 68 20 74 68 65 20 72 65  ill match the re
23b8e 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20  sult set of the 
23b8f 53 45 4c 45 43 54 2e 0a 2a 2f 0a 53 51 4c 49 54  SELECT..*/.SQLIT
23b90 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
23b91 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 0a  qlite3EndTable(.
23b92 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
23b93 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
23b94 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  se context */.  
23b95 54 6f 6b 65 6e 20 2a 70 43 6f 6e 73 2c 20 20 20  Token *pCons,   
23b96 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 27          /* The '
23b97 2c 27 20 74 6f 6b 65 6e 20 61 66 74 65 72 20 74  ,' token after t
23b98 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 64  he last column d
23b99 65 66 6e 2e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  efn. */.  Token 
23b9a 2a 70 45 6e 64 2c 20 20 20 20 20 20 20 20 20 20  *pEnd,          
23b9b 20 20 2f 2a 20 54 68 65 20 66 69 6e 61 6c 20 27    /* The final '
23b9c 29 27 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20  )' token in the 
23b9d 43 52 45 41 54 45 20 54 41 42 4c 45 20 2a 2f 0a  CREATE TABLE */.
23b9e 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
23b9f 74 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 6c  t         /* Sel
23ba0 65 63 74 20 66 72 6f 6d 20 61 20 22 43 52 45 41  ect from a "CREA
23ba1 54 45 20 2e 2e 2e 20 41 53 20 53 45 4c 45 43 54  TE ... AS SELECT
23ba2 22 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20  " */.){.  Table 
23ba3 2a 70 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  *p;.  sqlite3 *d
23ba4 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
23ba5 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20 69 66    int iDb;..  if
23ba6 28 20 28 70 45 6e 64 3d 3d 30 20 26 26 20 70 53  ( (pEnd==0 && pS
23ba7 65 6c 65 63 74 3d 3d 30 29 20 7c 7c 20 64 62 2d  elect==0) || db-
23ba8 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
23ba9 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
23baa 0a 20 20 70 20 3d 20 70 50 61 72 73 65 2d 3e 70  .  p = pParse->p
23bab 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 20  NewTable;.  if( 
23bac 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 0a  p==0 ) return;..
23bad 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 69    assert( !db->i
23bae 6e 69 74 2e 62 75 73 79 20 7c 7c 20 21 70 53 65  nit.busy || !pSe
23baf 6c 65 63 74 20 29 3b 0a 0a 20 20 69 44 62 20 3d  lect );..  iDb =
23bb0 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
23bb1 49 6e 64 65 78 28 64 62 2c 20 70 2d 3e 70 53 63  Index(db, p->pSc
23bb2 68 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66 20  hema);..#ifndef 
23bb3 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43  SQLITE_OMIT_CHEC
23bb4 4b 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 6e  K.  /* Resolve n
23bb5 61 6d 65 73 20 69 6e 20 61 6c 6c 20 43 48 45 43  ames in all CHEC
23bb6 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 78 70  K constraint exp
23bb7 72 65 73 73 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20  ressions..  */. 
23bb8 20 69 66 28 20 70 2d 3e 70 43 68 65 63 6b 20 29   if( p->pCheck )
23bb9 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 73 53  {.    SrcList sS
23bba 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
23bbb 20 20 20 20 20 20 2f 2a 20 46 61 6b 65 20 53 72        /* Fake Sr
23bbc 63 4c 69 73 74 20 66 6f 72 20 70 50 61 72 73 65  cList for pParse
23bbd 2d 3e 70 4e 65 77 54 61 62 6c 65 20 2a 2f 0a 20  ->pNewTable */. 
23bbe 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73     NameContext s
23bbf 4e 43 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  NC;             
23bc0 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65     /* Name conte
23bc1 78 74 20 66 6f 72 20 70 50 61 72 73 65 2d 3e 70  xt for pParse->p
23bc2 4e 65 77 54 61 62 6c 65 20 2a 2f 0a 0a 20 20 20  NewTable */..   
23bc3 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c   memset(&sNC, 0,
23bc4 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20   sizeof(sNC));. 
23bc5 20 20 20 6d 65 6d 73 65 74 28 26 73 53 72 63 2c     memset(&sSrc,
23bc6 20 30 2c 20 73 69 7a 65 6f 66 28 73 53 72 63 29   0, sizeof(sSrc)
23bc7 29 3b 0a 20 20 20 20 73 53 72 63 2e 6e 53 72 63  );.    sSrc.nSrc
23bc8 20 3d 20 31 3b 0a 20 20 20 20 73 53 72 63 2e 61   = 1;.    sSrc.a
23bc9 5b 30 5d 2e 7a 4e 61 6d 65 20 3d 20 70 2d 3e 7a  [0].zName = p->z
23bca 4e 61 6d 65 3b 0a 20 20 20 20 73 53 72 63 2e 61  Name;.    sSrc.a
23bcb 5b 30 5d 2e 70 54 61 62 20 3d 20 70 3b 0a 20 20  [0].pTab = p;.  
23bcc 20 20 73 53 72 63 2e 61 5b 30 5d 2e 69 43 75 72    sSrc.a[0].iCur
23bcd 73 6f 72 20 3d 20 2d 31 3b 0a 20 20 20 20 73 4e  sor = -1;.    sN
23bce 43 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73  C.pParse = pPars
23bcf 65 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72 63 4c  e;.    sNC.pSrcL
23bd0 69 73 74 20 3d 20 26 73 53 72 63 3b 0a 20 20 20  ist = &sSrc;.   
23bd1 20 73 4e 43 2e 69 73 43 68 65 63 6b 20 3d 20 31   sNC.isCheck = 1
23bd2 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
23bd3 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
23bd4 73 28 26 73 4e 43 2c 20 70 2d 3e 70 43 68 65 63  s(&sNC, p->pChec
23bd5 6b 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  k) ){.      retu
23bd6 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  rn;.    }.  }.#e
23bd7 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
23bd8 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45  (SQLITE_OMIT_CHE
23bd9 43 4b 29 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20  CK) */..  /* If 
23bda 74 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  the db->init.bus
23bdb 79 20 69 73 20 31 20 69 74 20 6d 65 61 6e 73 20  y is 1 it means 
23bdc 77 65 20 61 72 65 20 72 65 61 64 69 6e 67 20 74  we are reading t
23bdd 68 65 20 53 51 4c 20 6f 66 66 20 74 68 65 0a 20  he SQL off the. 
23bde 20 2a 2a 20 22 73 71 6c 69 74 65 5f 6d 61 73 74   ** "sqlite_mast
23bdf 65 72 22 20 6f 72 20 22 73 71 6c 69 74 65 5f 74  er" or "sqlite_t
23be0 65 6d 70 5f 6d 61 73 74 65 72 22 20 74 61 62 6c  emp_master" tabl
23be1 65 20 6f 6e 20 74 68 65 20 64 69 73 6b 2e 0a 20  e on the disk.. 
23be2 20 2a 2a 20 53 6f 20 64 6f 20 6e 6f 74 20 77 72   ** So do not wr
23be3 69 74 65 20 74 6f 20 74 68 65 20 64 69 73 6b 20  ite to the disk 
23be4 61 67 61 69 6e 2e 20 20 45 78 74 72 61 63 74 20  again.  Extract 
23be5 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75  the root page nu
23be6 6d 62 65 72 0a 20 20 2a 2a 20 66 6f 72 20 74 68  mber.  ** for th
23be7 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 74 68 65  e table from the
23be8 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75   db->init.newTnu
23be9 6d 20 66 69 65 6c 64 2e 20 20 28 54 68 65 20 70  m field.  (The p
23bea 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20  age number.  ** 
23beb 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e  should have been
23bec 20 70 75 74 20 74 68 65 72 65 20 62 79 20 74 68   put there by th
23bed 65 20 73 71 6c 69 74 65 4f 70 65 6e 43 62 20 72  e sqliteOpenCb r
23bee 6f 75 74 69 6e 65 2e 29 0a 20 20 2a 2f 0a 20 20  outine.).  */.  
23bef 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  if( db->init.bus
23bf0 79 20 29 7b 0a 20 20 20 20 70 2d 3e 74 6e 75 6d  y ){.    p->tnum
23bf1 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54   = db->init.newT
23bf2 6e 75 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  num;.  }..  /* I
23bf3 66 20 6e 6f 74 20 69 6e 69 74 69 61 6c 69 7a 69  f not initializi
23bf4 6e 67 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20  ng, then create 
23bf5 61 20 72 65 63 6f 72 64 20 66 6f 72 20 74 68 65  a record for the
23bf6 20 6e 65 77 20 74 61 62 6c 65 0a 20 20 2a 2a 20   new table.  ** 
23bf7 69 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41  in the SQLITE_MA
23bf8 53 54 45 52 20 74 61 62 6c 65 20 6f 66 20 74 68  STER table of th
23bf9 65 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a  e database..  **
23bfa 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73  .  ** If this is
23bfb 20 61 20 54 45 4d 50 4f 52 41 52 59 20 74 61 62   a TEMPORARY tab
23bfc 6c 65 2c 20 77 72 69 74 65 20 74 68 65 20 65 6e  le, write the en
23bfd 74 72 79 20 69 6e 74 6f 20 74 68 65 20 61 75 78  try into the aux
23bfe 69 6c 69 61 72 79 0a 20 20 2a 2a 20 66 69 6c 65  iliary.  ** file
23bff 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 74 6f   instead of into
23c00 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
23c01 73 65 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20  se file..  */.  
23c02 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75  if( !db->init.bu
23c03 73 79 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 3b  sy ){.    int n;
23c04 0a 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  .    Vdbe *v;.  
23c05 20 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20    char *zType;  
23c06 20 20 2f 2a 20 22 76 69 65 77 22 20 6f 72 20 22    /* "view" or "
23c07 74 61 62 6c 65 22 20 2a 2f 0a 20 20 20 20 63 68  table" */.    ch
23c08 61 72 20 2a 7a 54 79 70 65 32 3b 20 20 20 2f 2a  ar *zType2;   /*
23c09 20 22 56 49 45 57 22 20 6f 72 20 22 54 41 42 4c   "VIEW" or "TABL
23c0a 45 22 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a  E" */.    char *
23c0b 7a 53 74 6d 74 3b 20 20 20 20 2f 2a 20 54 65 78  zStmt;    /* Tex
23c0c 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20  t of the CREATE 
23c0d 54 41 42 4c 45 20 6f 72 20 43 52 45 41 54 45 20  TABLE or CREATE 
23c0e 56 49 45 57 20 73 74 61 74 65 6d 65 6e 74 20 2a  VIEW statement *
23c0f 2f 0a 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74  /..    v = sqlit
23c10 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
23c11 29 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52  );.    if( NEVER
23c12 28 76 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b  (v==0) ) return;
23c13 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
23c14 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c  eAddOp1(v, OP_Cl
23c15 6f 73 65 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a  ose, 0);..    /*
23c16 20 0a 20 20 20 20 2a 2a 20 49 6e 69 74 69 61 6c   .    ** Initial
23c17 69 7a 65 20 7a 54 79 70 65 20 66 6f 72 20 74 68  ize zType for th
23c18 65 20 6e 65 77 20 76 69 65 77 20 6f 72 20 74 61  e new view or ta
23c19 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ble..    */.    
23c1a 69 66 28 20 70 2d 3e 70 53 65 6c 65 63 74 3d 3d  if( p->pSelect==
23c1b 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20  0 ){.      /* A 
23c1c 72 65 67 75 6c 61 72 20 74 61 62 6c 65 20 2a 2f  regular table */
23c1d 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22  .      zType = "
23c1e 74 61 62 6c 65 22 3b 0a 20 20 20 20 20 20 7a 54  table";.      zT
23c1f 79 70 65 32 20 3d 20 22 54 41 42 4c 45 22 3b 0a  ype2 = "TABLE";.
23c20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
23c21 4d 49 54 5f 56 49 45 57 0a 20 20 20 20 7d 65 6c  MIT_VIEW.    }el
23c22 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 76  se{.      /* A v
23c23 69 65 77 20 2a 2f 0a 20 20 20 20 20 20 7a 54 79  iew */.      zTy
23c24 70 65 20 3d 20 22 76 69 65 77 22 3b 0a 20 20 20  pe = "view";.   
23c25 20 20 20 7a 54 79 70 65 32 20 3d 20 22 56 49 45     zType2 = "VIE
23c26 57 22 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d  W";.#endif.    }
23c27 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73  ..    /* If this
23c28 20 69 73 20 61 20 43 52 45 41 54 45 20 54 41 42   is a CREATE TAB
23c29 4c 45 20 78 78 20 41 53 20 53 45 4c 45 43 54 20  LE xx AS SELECT 
23c2a 2e 2e 2e 2c 20 65 78 65 63 75 74 65 20 74 68 65  ..., execute the
23c2b 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 73   SELECT.    ** s
23c2c 74 61 74 65 6d 65 6e 74 20 74 6f 20 70 6f 70 75  tatement to popu
23c2d 6c 61 74 65 20 74 68 65 20 6e 65 77 20 74 61 62  late the new tab
23c2e 6c 65 2e 20 54 68 65 20 72 6f 6f 74 2d 70 61 67  le. The root-pag
23c2f 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  e number for the
23c30 0a 20 20 20 20 2a 2a 20 6e 65 77 20 74 61 62 6c  .    ** new tabl
23c31 65 20 69 73 20 69 6e 20 72 65 67 69 73 74 65 72  e is in register
23c32 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74   pParse->regRoot
23c33 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
23c34 4f 6e 63 65 20 74 68 65 20 53 45 4c 45 43 54 20  Once the SELECT 
23c35 68 61 73 20 62 65 65 6e 20 63 6f 64 65 64 20 62  has been coded b
23c36 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28  y sqlite3Select(
23c37 29 2c 20 69 74 20 69 73 20 69 6e 20 61 0a 20 20  ), it is in a.  
23c38 20 20 2a 2a 20 73 75 69 74 61 62 6c 65 20 73 74    ** suitable st
23c39 61 74 65 20 74 6f 20 71 75 65 72 79 20 66 6f 72  ate to query for
23c3a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   the column name
23c3b 73 20 61 6e 64 20 74 79 70 65 73 20 74 6f 20 62  s and types to b
23c3c 65 20 75 73 65 64 0a 20 20 20 20 2a 2a 20 62 79  e used.    ** by
23c3d 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 2e 0a   the new table..
23c3e 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 20      **.    ** A 
23c3f 73 68 61 72 65 64 2d 63 61 63 68 65 20 77 72 69  shared-cache wri
23c40 74 65 2d 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 72  te-lock is not r
23c41 65 71 75 69 72 65 64 20 74 6f 20 77 72 69 74 65  equired to write
23c42 20 74 6f 20 74 68 65 20 6e 65 77 20 74 61 62 6c   to the new tabl
23c43 65 2c 0a 20 20 20 20 2a 2a 20 61 73 20 61 20 73  e,.    ** as a s
23c44 63 68 65 6d 61 2d 6c 6f 63 6b 20 6d 75 73 74 20  chema-lock must 
23c45 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65  have already bee
23c46 6e 20 6f 62 74 61 69 6e 65 64 20 74 6f 20 63 72  n obtained to cr
23c47 65 61 74 65 20 69 74 2e 20 53 69 6e 63 65 0a 20  eate it. Since. 
23c48 20 20 20 2a 2a 20 61 20 73 63 68 65 6d 61 2d 6c     ** a schema-l
23c49 6f 63 6b 20 65 78 63 6c 75 64 65 73 20 61 6c 6c  ock excludes all
23c4a 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20   other database 
23c4b 75 73 65 72 73 2c 20 74 68 65 20 77 72 69 74 65  users, the write
23c4c 2d 6c 6f 63 6b 20 77 6f 75 6c 64 0a 20 20 20 20  -lock would.    
23c4d 2a 2a 20 62 65 20 72 65 64 75 6e 64 61 6e 74 2e  ** be redundant.
23c4e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
23c4f 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20  pSelect ){.     
23c50 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74   SelectDest dest
23c51 3b 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70  ;.      Table *p
23c52 53 65 6c 54 61 62 3b 0a 0a 20 20 20 20 20 20 61  SelTab;..      a
23c53 73 73 65 72 74 28 70 50 61 72 73 65 2d 3e 6e 54  ssert(pParse->nT
23c54 61 62 3d 3d 31 29 3b 0a 20 20 20 20 20 20 73 71  ab==1);.      sq
23c55 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
23c56 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c  v, OP_OpenWrite,
23c57 20 31 2c 20 70 50 61 72 73 65 2d 3e 72 65 67 52   1, pParse->regR
23c58 6f 6f 74 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  oot, iDb);.     
23c59 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
23c5a 67 65 50 35 28 76 2c 20 31 29 3b 0a 20 20 20 20  geP5(v, 1);.    
23c5b 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d    pParse->nTab =
23c5c 20 32 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   2;.      sqlite
23c5d 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28  3SelectDestInit(
23c5e 26 64 65 73 74 2c 20 53 52 54 5f 54 61 62 6c 65  &dest, SRT_Table
23c5f 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
23c60 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
23c61 2c 20 70 53 65 6c 65 63 74 2c 20 26 64 65 73 74  , pSelect, &dest
23c62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
23c63 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
23c64 5f 43 6c 6f 73 65 2c 20 31 29 3b 0a 20 20 20 20  _Close, 1);.    
23c65 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45    if( pParse->nE
23c66 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  rr==0 ){.       
23c67 20 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74   pSelTab = sqlit
23c68 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c  e3ResultSetOfSel
23c69 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c  ect(pParse, pSel
23c6a 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ect);.        if
23c6b 28 20 70 53 65 6c 54 61 62 3d 3d 30 20 29 20 72  ( pSelTab==0 ) r
23c6c 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 61  eturn;.        a
23c6d 73 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c 3d 3d  ssert( p->aCol==
23c6e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  0 );.        p->
23c6f 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e  nCol = pSelTab->
23c70 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 2d  nCol;.        p-
23c71 3e 61 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d  >aCol = pSelTab-
23c72 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70  >aCol;.        p
23c73 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 30  SelTab->nCol = 0
23c74 3b 0a 20 20 20 20 20 20 20 20 70 53 65 6c 54 61  ;.        pSelTa
23c75 62 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20  b->aCol = 0;.   
23c76 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65       sqlite3Dele
23c77 74 65 54 61 62 6c 65 28 70 53 65 6c 54 61 62 29  teTable(pSelTab)
23c78 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
23c79 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20  .    /* Compute 
23c7a 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78  the complete tex
23c7b 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20  t of the CREATE 
23c7c 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20  statement */.   
23c7d 20 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a   if( pSelect ){.
23c7e 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 63 72        zStmt = cr
23c7f 65 61 74 65 54 61 62 6c 65 53 74 6d 74 28 64 62  eateTableStmt(db
23c80 2c 20 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  , p);.    }else{
23c81 0a 20 20 20 20 20 20 6e 20 3d 20 28 69 6e 74 29  .      n = (int)
23c82 28 70 45 6e 64 2d 3e 7a 20 2d 20 70 50 61 72 73  (pEnd->z - pPars
23c83 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 29  e->sNameToken.z)
23c84 20 2b 20 31 3b 0a 20 20 20 20 20 20 7a 53 74 6d   + 1;.      zStm
23c85 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  t = sqlite3MPrin
23c86 74 66 28 64 62 2c 20 0a 20 20 20 20 20 20 20 20  tf(db, .        
23c87 20 20 22 43 52 45 41 54 45 20 25 73 20 25 2e 2a    "CREATE %s %.*
23c88 73 22 2c 20 7a 54 79 70 65 32 2c 20 6e 2c 20 70  s", zType2, n, p
23c89 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65  Parse->sNameToke
23c8a 6e 2e 7a 0a 20 20 20 20 20 20 29 3b 0a 20 20 20  n.z.      );.   
23c8b 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 20 73 6c 6f   }..    /* A slo
23c8c 74 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64  t for the record
23c8d 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
23c8e 6e 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 74  n allocated in t
23c8f 68 65 20 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54  he .    ** SQLIT
23c90 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e 20  E_MASTER table. 
23c91 20 57 65 20 6a 75 73 74 20 6e 65 65 64 20 74 6f   We just need to
23c92 20 75 70 64 61 74 65 20 74 68 61 74 20 73 6c 6f   update that slo
23c93 74 20 77 69 74 68 20 61 6c 6c 0a 20 20 20 20 2a  t with all.    *
23c94 2a 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f  * the informatio
23c95 6e 20 77 65 27 76 65 20 63 6f 6c 6c 65 63 74 65  n we've collecte
23c96 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  d..    */.    sq
23c97 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
23c98 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 22  (pParse,.      "
23c99 55 50 44 41 54 45 20 25 51 2e 25 73 20 22 0a 20  UPDATE %Q.%s ". 
23c9a 20 20 20 20 20 20 20 20 22 53 45 54 20 74 79 70          "SET typ
23c9b 65 3d 27 25 73 27 2c 20 6e 61 6d 65 3d 25 51 2c  e='%s', name=%Q,
23c9c 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 2c 20 72 6f   tbl_name=%Q, ro
23c9d 6f 74 70 61 67 65 3d 23 25 64 2c 20 73 71 6c 3d  otpage=#%d, sql=
23c9e 25 51 20 22 0a 20 20 20 20 20 20 20 22 57 48 45  %Q ".       "WHE
23c9f 52 45 20 72 6f 77 69 64 3d 23 25 64 22 2c 0a 20  RE rowid=#%d",. 
23ca0 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62       db->aDb[iDb
23ca1 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f  ].zName, SCHEMA_
23ca2 54 41 42 4c 45 28 69 44 62 29 2c 0a 20 20 20 20  TABLE(iDb),.    
23ca3 20 20 7a 54 79 70 65 2c 0a 20 20 20 20 20 20 70    zType,.      p
23ca4 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70  ->zName,.      p
23ca5 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70  ->zName,.      p
23ca6 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2c 0a  Parse->regRoot,.
23ca7 20 20 20 20 20 20 7a 53 74 6d 74 2c 0a 20 20 20        zStmt,.   
23ca8 20 20 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f     pParse->regRo
23ca9 77 69 64 0a 20 20 20 20 29 3b 0a 20 20 20 20 73  wid.    );.    s
23caa 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
23cab 20 7a 53 74 6d 74 29 3b 0a 20 20 20 20 73 71 6c   zStmt);.    sql
23cac 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65  ite3ChangeCookie
23cad 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 0a  (pParse, iDb);..
23cae 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
23caf 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e  MIT_AUTOINCREMEN
23cb0 54 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74  T.    /* Check t
23cb1 6f 20 73 65 65 20 69 66 20 77 65 20 6e 65 65 64  o see if we need
23cb2 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20 73 71   to create an sq
23cb3 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61  lite_sequence ta
23cb4 62 6c 65 20 66 6f 72 0a 20 20 20 20 2a 2a 20 6b  ble for.    ** k
23cb5 65 65 70 69 6e 67 20 74 72 61 63 6b 20 6f 66 20  eeping track of 
23cb6 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 6b 65  autoincrement ke
23cb7 79 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ys..    */.    i
23cb8 66 28 20 70 2d 3e 74 61 62 46 6c 61 67 73 20 26  f( p->tabFlags &
23cb9 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e   TF_Autoincremen
23cba 74 20 29 7b 0a 20 20 20 20 20 20 44 62 20 2a 70  t ){.      Db *p
23cbb 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44  Db = &db->aDb[iD
23cbc 62 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44  b];.      if( pD
23cbd 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71  b->pSchema->pSeq
23cbe 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Tab==0 ){.      
23cbf 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50    sqlite3NestedP
23cc0 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20  arse(pParse,.   
23cc1 20 20 20 20 20 20 20 22 43 52 45 41 54 45 20 54         "CREATE T
23cc2 41 42 4c 45 20 25 51 2e 73 71 6c 69 74 65 5f 73  ABLE %Q.sqlite_s
23cc3 65 71 75 65 6e 63 65 28 6e 61 6d 65 2c 73 65 71  equence(name,seq
23cc4 29 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 44  )",.          pD
23cc5 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 20 20 20  b->zName.       
23cc6 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   );.      }.    
23cc7 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a  }.#endif..    /*
23cc8 20 52 65 70 61 72 73 65 20 65 76 65 72 79 74 68   Reparse everyth
23cc9 69 6e 67 20 74 6f 20 75 70 64 61 74 65 20 6f 75  ing to update ou
23cca 72 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 20  r internal data 
23ccb 73 74 72 75 63 74 75 72 65 73 20 2a 2f 0a 20 20  structures */.  
23ccc 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
23ccd 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 53  Op4(v, OP_ParseS
23cce 63 68 65 6d 61 2c 20 69 44 62 2c 20 30 2c 20 30  chema, iDb, 0, 0
23ccf 2c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ,.        sqlite
23cd0 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 74 62  3MPrintf(db, "tb
23cd1 6c 5f 6e 61 6d 65 3d 27 25 71 27 22 2c 70 2d 3e  l_name='%q'",p->
23cd2 7a 4e 61 6d 65 29 2c 20 50 34 5f 44 59 4e 41 4d  zName), P4_DYNAM
23cd3 49 43 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20  IC);.  }...  /* 
23cd4 41 64 64 20 74 68 65 20 74 61 62 6c 65 20 74 6f  Add the table to
23cd5 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72   the in-memory r
23cd6 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
23cd7 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 20   the database.. 
23cd8 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e   */.  if( db->in
23cd9 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 54  it.busy ){.    T
23cda 61 62 6c 65 20 2a 70 4f 6c 64 3b 0a 20 20 20 20  able *pOld;.    
23cdb 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20  Schema *pSchema 
23cdc 3d 20 70 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20  = p->pSchema;.  
23cdd 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33    pOld = sqlite3
23cde 48 61 73 68 49 6e 73 65 72 74 28 26 70 53 63 68  HashInsert(&pSch
23cdf 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70 2d  ema->tblHash, p-
23ce0 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  >zName,.        
23ce1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23ce2 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 6c       sqlite3Strl
23ce3 65 6e 33 30 28 70 2d 3e 7a 4e 61 6d 65 29 2c 70  en30(p->zName),p
23ce4 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c 64 20  );.    if( pOld 
23ce5 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
23ce6 20 70 3d 3d 70 4f 6c 64 20 29 3b 20 20 2f 2a 20   p==pOld );  /* 
23ce7 4d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61 76 65  Malloc must have
23ce8 20 66 61 69 6c 65 64 20 69 6e 73 69 64 65 20 48   failed inside H
23ce9 61 73 68 49 6e 73 65 72 74 28 29 20 2a 2f 0a 20  ashInsert() */. 
23cea 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46       db->mallocF
23ceb 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  ailed = 1;.     
23cec 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
23ced 20 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54     pParse->pNewT
23cee 61 62 6c 65 20 3d 20 30 3b 0a 20 20 20 20 64 62  able = 0;.    db
23cef 2d 3e 6e 54 61 62 6c 65 2b 2b 3b 0a 20 20 20 20  ->nTable++;.    
23cf0 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c  db->flags |= SQL
23cf1 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65  ITE_InternChange
23cf2 73 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  s;..#ifndef SQLI
23cf3 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42  TE_OMIT_ALTERTAB
23cf4 4c 45 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 70  LE.    if( !p->p
23cf5 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20  Select ){.      
23cf6 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
23cf7 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  e = (const char 
23cf8 2a 29 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54  *)pParse->sNameT
23cf9 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 20 20 20 69 6e  oken.z;.      in
23cfa 74 20 6e 4e 61 6d 65 3b 0a 20 20 20 20 20 20 61  t nName;.      a
23cfb 73 73 65 72 74 28 20 21 70 53 65 6c 65 63 74 20  ssert( !pSelect 
23cfc 26 26 20 70 43 6f 6e 73 20 26 26 20 70 45 6e 64  && pCons && pEnd
23cfd 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43   );.      if( pC
23cfe 6f 6e 73 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20  ons->z==0 ){.   
23cff 20 20 20 20 20 70 43 6f 6e 73 20 3d 20 70 45 6e       pCons = pEn
23d00 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  d;.      }.     
23d01 20 6e 4e 61 6d 65 20 3d 20 28 69 6e 74 29 28 28   nName = (int)((
23d02 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 43 6f  const char *)pCo
23d03 6e 73 2d 3e 7a 20 2d 20 7a 4e 61 6d 65 29 3b 0a  ns->z - zName);.
23d04 20 20 20 20 20 20 70 2d 3e 61 64 64 43 6f 6c 4f        p->addColO
23d05 66 66 73 65 74 20 3d 20 31 33 20 2b 20 73 71 6c  ffset = 13 + sql
23d06 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28  ite3Utf8CharLen(
23d07 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20  zName, nName);. 
23d08 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
23d09 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
23d0a 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f 2a 0a 2a  E_OMIT_VIEW./*.*
23d0b 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c  * The parser cal
23d0c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
23d0d 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65 61  in order to crea
23d0e 74 65 20 61 20 6e 65 77 20 56 49 45 57 0a 2a 2f  te a new VIEW.*/
23d0f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
23d10 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61  void sqlite3Crea
23d11 74 65 56 69 65 77 28 0a 20 20 50 61 72 73 65 20  teView(.  Parse 
23d12 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20  *pParse,     /* 
23d13 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74  The parsing cont
23d14 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ext */.  Token *
23d15 70 42 65 67 69 6e 2c 20 20 20 20 20 2f 2a 20 54  pBegin,     /* T
23d16 68 65 20 43 52 45 41 54 45 20 74 6f 6b 65 6e 20  he CREATE token 
23d17 74 68 61 74 20 62 65 67 69 6e 73 20 74 68 65 20  that begins the 
23d18 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 54  statement */.  T
23d19 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20  oken *pName1,   
23d1a 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 20 74    /* The token t
23d1b 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61  hat holds the na
23d1c 6d 65 20 6f 66 20 74 68 65 20 76 69 65 77 20 2a  me of the view *
23d1d 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  /.  Token *pName
23d1e 32 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f  2,     /* The to
23d1f 6b 65 6e 20 74 68 61 74 20 68 6f 6c 64 73 20 74  ken that holds t
23d20 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 76  he name of the v
23d21 69 65 77 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  iew */.  Select 
23d22 2a 70 53 65 6c 65 63 74 2c 20 20 20 2f 2a 20 41  *pSelect,   /* A
23d23 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
23d24 74 20 74 68 61 74 20 77 69 6c 6c 20 62 65 63 6f  t that will beco
23d25 6d 65 20 74 68 65 20 6e 65 77 20 76 69 65 77 20  me the new view 
23d26 2a 2f 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 2c  */.  int isTemp,
23d27 20 20 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20          /* TRUE 
23d28 66 6f 72 20 61 20 54 45 4d 50 4f 52 41 52 59 20  for a TEMPORARY 
23d29 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f  view */.  int no
23d2a 45 72 72 20 20 20 20 20 20 20 20 20 20 2f 2a 20  Err          /* 
23d2b 53 75 70 70 72 65 73 73 20 65 72 72 6f 72 20 6d  Suppress error m
23d2c 65 73 73 61 67 65 73 20 69 66 20 56 49 45 57 20  essages if VIEW 
23d2d 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 2a  already exists *
23d2e 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b  /.){.  Table *p;
23d2f 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 63 6f 6e 73  .  int n;.  cons
23d30 74 20 63 68 61 72 20 2a 7a 3b 0a 20 20 54 6f 6b  t char *z;.  Tok
23d31 65 6e 20 73 45 6e 64 3b 0a 20 20 44 62 46 69 78  en sEnd;.  DbFix
23d32 65 72 20 73 46 69 78 3b 0a 20 20 54 6f 6b 65 6e  er sFix;.  Token
23d33 20 2a 70 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 69   *pName;.  int i
23d34 44 62 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  Db;.  sqlite3 *d
23d35 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
23d36 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
23d37 56 61 72 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c  Var>0 ){.    sql
23d38 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
23d39 72 73 65 2c 20 22 70 61 72 61 6d 65 74 65 72 73  rse, "parameters
23d3a 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64   are not allowed
23d3b 20 69 6e 20 76 69 65 77 73 22 29 3b 0a 20 20 20   in views");.   
23d3c 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
23d3d 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74  lete(db, pSelect
23d3e 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
23d3f 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 74 61 72   }.  sqlite3Star
23d40 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70  tTable(pParse, p
23d41 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 69  Name1, pName2, i
23d42 73 54 65 6d 70 2c 20 31 2c 20 30 2c 20 6e 6f 45  sTemp, 1, 0, noE
23d43 72 72 29 3b 0a 20 20 70 20 3d 20 70 50 61 72 73  rr);.  p = pPars
23d44 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20  e->pNewTable;.  
23d45 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20  if( p==0 ){.    
23d46 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
23d47 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29  ete(db, pSelect)
23d48 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
23d49 7d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  }.  assert( pPar
23d4a 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 3b 20 2f  se->nErr==0 ); /
23d4b 2a 20 49 66 20 73 71 6c 69 74 65 33 53 74 61 72  * If sqlite3Star
23d4c 74 54 61 62 6c 65 20 72 65 74 75 72 6e 20 6e 6f  tTable return no
23d4d 6e 2d 4e 55 4c 4c 20 74 68 65 6e 0a 20 20 20 20  n-NULL then.    
23d4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23d4f 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 72           ** ther
23d50 65 20 63 6f 75 6c 64 20 6e 6f 74 20 68 61 76 65  e could not have
23d51 20 62 65 65 6e 20 61 6e 20 65 72 72 6f 72 20 2a   been an error *
23d52 2f 0a 20 20 73 71 6c 69 74 65 33 54 77 6f 50 61  /.  sqlite3TwoPa
23d53 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70  rtName(pParse, p
23d54 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26  Name1, pName2, &
23d55 70 4e 61 6d 65 29 3b 0a 20 20 69 44 62 20 3d 20  pName);.  iDb = 
23d56 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
23d57 6e 64 65 78 28 64 62 2c 20 70 2d 3e 70 53 63 68  ndex(db, p->pSch
23d58 65 6d 61 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  ema);.  if( sqli
23d59 74 65 33 46 69 78 49 6e 69 74 28 26 73 46 69 78  te3FixInit(&sFix
23d5a 2c 20 70 50 61 72 73 65 2c 20 69 44 62 2c 20 22  , pParse, iDb, "
23d5b 76 69 65 77 22 2c 20 70 4e 61 6d 65 29 0a 20 20  view", pName).  
23d5c 20 20 26 26 20 73 71 6c 69 74 65 33 46 69 78 53    && sqlite3FixS
23d5d 65 6c 65 63 74 28 26 73 46 69 78 2c 20 70 53 65  elect(&sFix, pSe
23d5e 6c 65 63 74 29 0a 20 20 29 7b 0a 20 20 20 20 73  lect).  ){.    s
23d5f 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
23d60 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b  te(db, pSelect);
23d61 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
23d62 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 61 20 63 6f  ..  /* Make a co
23d63 70 79 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65  py of the entire
23d64 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
23d65 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74  t that defines t
23d66 68 65 20 76 69 65 77 2e 0a 20 20 2a 2a 20 54 68  he view..  ** Th
23d67 69 73 20 77 69 6c 6c 20 66 6f 72 63 65 20 61 6c  is will force al
23d68 6c 20 74 68 65 20 45 78 70 72 2e 74 6f 6b 65 6e  l the Expr.token
23d69 2e 7a 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20  .z values to be 
23d6a 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 20 20 2a 2a  dynamically.  **
23d6b 20 61 6c 6c 6f 63 61 74 65 64 20 72 61 74 68 65   allocated rathe
23d6c 72 20 74 68 61 6e 20 70 6f 69 6e 74 20 74 6f 20  r than point to 
23d6d 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67  the input string
23d6e 20 2d 20 77 68 69 63 68 20 6d 65 61 6e 73 20 74   - which means t
23d6f 68 61 74 0a 20 20 2a 2a 20 74 68 65 79 20 77 69  hat.  ** they wi
23d70 6c 6c 20 70 65 72 73 69 73 74 20 61 66 74 65 72  ll persist after
23d71 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 71 6c   the current sql
23d72 69 74 65 33 5f 65 78 65 63 28 29 20 63 61 6c 6c  ite3_exec() call
23d73 20 72 65 74 75 72 6e 73 2e 0a 20 20 2a 2f 0a 20   returns..  */. 
23d74 20 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71   p->pSelect = sq
23d75 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64  lite3SelectDup(d
23d76 62 2c 20 70 53 65 6c 65 63 74 2c 20 45 58 50 52  b, pSelect, EXPR
23d77 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 73  DUP_REDUCE);.  s
23d78 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
23d79 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b  te(db, pSelect);
23d7a 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
23d7b 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72  cFailed ){.    r
23d7c 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28  eturn;.  }.  if(
23d7d 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20   !db->init.busy 
23d7e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 69  ){.    sqlite3Vi
23d7f 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73  ewGetColumnNames
23d80 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 7d  (pParse, p);.  }
23d81 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68  ..  /* Locate th
23d82 65 20 65 6e 64 20 6f 66 20 74 68 65 20 43 52 45  e end of the CRE
23d83 41 54 45 20 56 49 45 57 20 73 74 61 74 65 6d 65  ATE VIEW stateme
23d84 6e 74 2e 20 20 4d 61 6b 65 20 73 45 6e 64 20 70  nt.  Make sEnd p
23d85 6f 69 6e 74 20 74 6f 0a 20 20 2a 2a 20 74 68 65  oint to.  ** the
23d86 20 65 6e 64 2e 0a 20 20 2a 2f 0a 20 20 73 45 6e   end..  */.  sEn
23d87 64 20 3d 20 70 50 61 72 73 65 2d 3e 73 4c 61 73  d = pParse->sLas
23d88 74 54 6f 6b 65 6e 3b 0a 20 20 69 66 28 20 41 4c  tToken;.  if( AL
23d89 57 41 59 53 28 73 45 6e 64 2e 7a 5b 30 5d 21 3d  WAYS(sEnd.z[0]!=
23d8a 30 29 20 26 26 20 73 45 6e 64 2e 7a 5b 30 5d 21  0) && sEnd.z[0]!
23d8b 3d 27 3b 27 20 29 7b 0a 20 20 20 20 73 45 6e 64  =';' ){.    sEnd
23d8c 2e 7a 20 2b 3d 20 73 45 6e 64 2e 6e 3b 0a 20 20  .z += sEnd.n;.  
23d8d 7d 0a 20 20 73 45 6e 64 2e 6e 20 3d 20 30 3b 0a  }.  sEnd.n = 0;.
23d8e 20 20 6e 20 3d 20 28 69 6e 74 29 28 73 45 6e 64    n = (int)(sEnd
23d8f 2e 7a 20 2d 20 70 42 65 67 69 6e 2d 3e 7a 29 3b  .z - pBegin->z);
23d90 0a 20 20 7a 20 3d 20 70 42 65 67 69 6e 2d 3e 7a  .  z = pBegin->z
23d91 3b 0a 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59  ;.  while( ALWAY
23d92 53 28 6e 3e 30 29 20 26 26 20 73 71 6c 69 74 65  S(n>0) && sqlite
23d93 33 49 73 73 70 61 63 65 28 7a 5b 6e 2d 31 5d 29  3Isspace(z[n-1])
23d94 20 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 73 45 6e   ){ n--; }.  sEn
23d95 64 2e 7a 20 3d 20 26 7a 5b 6e 2d 31 5d 3b 0a 20  d.z = &z[n-1];. 
23d96 20 73 45 6e 64 2e 6e 20 3d 20 31 3b 0a 0a 20 20   sEnd.n = 1;..  
23d97 2f 2a 20 55 73 65 20 73 71 6c 69 74 65 33 45 6e  /* Use sqlite3En
23d98 64 54 61 62 6c 65 28 29 20 74 6f 20 61 64 64 20  dTable() to add 
23d99 74 68 65 20 76 69 65 77 20 74 6f 20 74 68 65 20  the view to the 
23d9a 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61  SQLITE_MASTER ta
23d9b 62 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ble */.  sqlite3
23d9c 45 6e 64 54 61 62 6c 65 28 70 50 61 72 73 65 2c  EndTable(pParse,
23d9d 20 30 2c 20 26 73 45 6e 64 2c 20 30 29 3b 0a 20   0, &sEnd, 0);. 
23d9e 20 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69   return;.}.#endi
23d9f 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
23da0 5f 56 49 45 57 20 2a 2f 0a 0a 23 69 66 20 21 64  _VIEW */..#if !d
23da1 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
23da2 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66  IT_VIEW) || !def
23da3 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
23da4 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0a 2f  _VIRTUALTABLE)./
23da5 2a 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65 20 73  *.** The Table s
23da6 74 72 75 63 74 75 72 65 20 70 54 61 62 6c 65 20  tructure pTable 
23da7 69 73 20 72 65 61 6c 6c 79 20 61 20 56 49 45 57  is really a VIEW
23da8 2e 20 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 6e  .  Fill in the n
23da9 61 6d 65 73 20 6f 66 0a 2a 2a 20 74 68 65 20 63  ames of.** the c
23daa 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 76 69  olumns of the vi
23dab 65 77 20 69 6e 20 74 68 65 20 70 54 61 62 6c 65  ew in the pTable
23dac 20 73 74 72 75 63 74 75 72 65 2e 20 20 52 65 74   structure.  Ret
23dad 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a  urn the number.*
23dae 2a 20 6f 66 20 65 72 72 6f 72 73 2e 20 20 49 66  * of errors.  If
23daf 20 61 6e 20 65 72 72 6f 72 20 69 73 20 73 65 65   an error is see
23db0 6e 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72  n leave an error
23db1 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61 72   message in pPar
23db2 73 65 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2f 0a  se->zErrMsg..*/.
23db3 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
23db4 6e 74 20 73 71 6c 69 74 65 33 56 69 65 77 47 65  nt sqlite3ViewGe
23db5 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50 61 72  tColumnNames(Par
23db6 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c  se *pParse, Tabl
23db7 65 20 2a 70 54 61 62 6c 65 29 7b 0a 20 20 54 61  e *pTable){.  Ta
23db8 62 6c 65 20 2a 70 53 65 6c 54 61 62 3b 20 20 20  ble *pSelTab;   
23db9 2f 2a 20 41 20 66 61 6b 65 20 74 61 62 6c 65 20  /* A fake table 
23dba 66 72 6f 6d 20 77 68 69 63 68 20 77 65 20 67 65  from which we ge
23dbb 74 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  t the result set
23dbc 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53   */.  Select *pS
23dbd 65 6c 3b 20 20 20 20 20 2f 2a 20 43 6f 70 79 20  el;     /* Copy 
23dbe 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 74 68  of the SELECT th
23dbf 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  at implements th
23dc0 65 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20  e view */.  int 
23dc1 6e 45 72 72 20 3d 20 30 3b 20 20 20 20 20 2f 2a  nErr = 0;     /*
23dc2 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72   Number of error
23dc3 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 2a 2f  s encountered */
23dc4 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20  .  int n;       
23dc5 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72       /* Temporar
23dc6 69 6c 79 20 68 6f 6c 64 73 20 74 68 65 20 6e 75  ily holds the nu
23dc7 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73 20  mber of cursors 
23dc8 61 73 73 69 67 6e 65 64 20 2a 2f 0a 20 20 73 71  assigned */.  sq
23dc9 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
23dca 73 65 2d 3e 64 62 3b 20 20 2f 2a 20 44 61 74 61  se->db;  /* Data
23dcb 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
23dcc 66 6f 72 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72  for malloc error
23dcd 73 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 41 75  s */.  int (*xAu
23dce 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  th)(void*,int,co
23dcf 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
23dd0 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
23dd1 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  *,const char*);.
23dd2 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c  .  assert( pTabl
23dd3 65 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  e );..#ifndef SQ
23dd4 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
23dd5 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 73 71 6c  LTABLE.  if( sql
23dd6 69 74 65 33 56 74 61 62 43 61 6c 6c 43 6f 6e 6e  ite3VtabCallConn
23dd7 65 63 74 28 70 50 61 72 73 65 2c 20 70 54 61 62  ect(pParse, pTab
23dd8 6c 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  le) ){.    retur
23dd9 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
23dda 20 20 7d 0a 20 20 69 66 28 20 49 73 56 69 72 74    }.  if( IsVirt
23ddb 75 61 6c 28 70 54 61 62 6c 65 29 20 29 20 72 65  ual(pTable) ) re
23ddc 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  turn 0;.#endif..
23ddd 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
23dde 4d 49 54 5f 56 49 45 57 0a 20 20 2f 2a 20 41 20  MIT_VIEW.  /* A 
23ddf 70 6f 73 69 74 69 76 65 20 6e 43 6f 6c 20 6d 65  positive nCol me
23de0 61 6e 73 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20  ans the columns 
23de1 6e 61 6d 65 73 20 66 6f 72 20 74 68 69 73 20 76  names for this v
23de2 69 65 77 20 61 72 65 0a 20 20 2a 2a 20 61 6c 72  iew are.  ** alr
23de3 65 61 64 79 20 6b 6e 6f 77 6e 2e 0a 20 20 2a 2f  eady known..  */
23de4 0a 20 20 69 66 28 20 70 54 61 62 6c 65 2d 3e 6e  .  if( pTable->n
23de5 43 6f 6c 3e 30 20 29 20 72 65 74 75 72 6e 20 30  Col>0 ) return 0
23de6 3b 0a 0a 20 20 2f 2a 20 41 20 6e 65 67 61 74 69  ;..  /* A negati
23de7 76 65 20 6e 43 6f 6c 20 69 73 20 61 20 73 70 65  ve nCol is a spe
23de8 63 69 61 6c 20 6d 61 72 6b 65 72 20 6d 65 61 6e  cial marker mean
23de9 69 6e 67 20 74 68 61 74 20 77 65 20 61 72 65 20  ing that we are 
23dea 63 75 72 72 65 6e 74 6c 79 0a 20 20 2a 2a 20 74  currently.  ** t
23deb 72 79 69 6e 67 20 74 6f 20 63 6f 6d 70 75 74 65  rying to compute
23dec 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   the column name
23ded 73 2e 20 20 49 66 20 77 65 20 65 6e 74 65 72 20  s.  If we enter 
23dee 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74  this routine wit
23def 68 0a 20 20 2a 2a 20 61 20 6e 65 67 61 74 69 76  h.  ** a negativ
23df0 65 20 6e 43 6f 6c 2c 20 69 74 20 6d 65 61 6e 73  e nCol, it means
23df1 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 76 69 65   two or more vie
23df2 77 73 20 66 6f 72 6d 20 61 20 6c 6f 6f 70 2c 20  ws form a loop, 
23df3 6c 69 6b 65 20 74 68 69 73 3a 0a 20 20 2a 2a 0a  like this:.  **.
23df4 20 20 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20    **     CREATE 
23df5 56 49 45 57 20 6f 6e 65 20 41 53 20 53 45 4c 45  VIEW one AS SELE
23df6 43 54 20 2a 20 46 52 4f 4d 20 74 77 6f 3b 0a 20  CT * FROM two;. 
23df7 20 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 56   **     CREATE V
23df8 49 45 57 20 74 77 6f 20 41 53 20 53 45 4c 45 43  IEW two AS SELEC
23df9 54 20 2a 20 46 52 4f 4d 20 6f 6e 65 3b 0a 20 20  T * FROM one;.  
23dfa 2a 2a 0a 20 20 2a 2a 20 41 63 74 75 61 6c 6c 79  **.  ** Actually
23dfb 2c 20 74 68 65 20 65 72 72 6f 72 20 61 62 6f 76  , the error abov
23dfc 65 20 69 73 20 6e 6f 77 20 63 61 75 67 68 74 20  e is now caught 
23dfd 70 72 69 6f 72 20 74 6f 20 72 65 61 63 68 69 6e  prior to reachin
23dfe 67 20 74 68 69 73 20 70 6f 69 6e 74 2e 0a 20 20  g this point..  
23dff 2a 2a 20 42 75 74 20 74 68 65 20 66 6f 6c 6c 6f  ** But the follo
23e00 77 69 6e 67 20 74 65 73 74 20 69 73 20 73 74 69  wing test is sti
23e01 6c 6c 20 69 6d 70 6f 72 74 61 6e 74 20 61 73 20  ll important as 
23e02 69 74 20 64 6f 65 73 20 63 6f 6d 65 20 75 70 0a  it does come up.
23e03 20 20 2a 2a 20 69 6e 20 74 68 65 20 66 6f 6c 6c    ** in the foll
23e04 6f 77 69 6e 67 3a 0a 20 20 2a 2a 20 0a 20 20 2a  owing:.  ** .  *
23e05 2a 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42  *     CREATE TAB
23e06 4c 45 20 6d 61 69 6e 2e 65 78 31 28 61 29 3b 0a  LE main.ex1(a);.
23e07 20 20 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20    **     CREATE 
23e08 54 45 4d 50 20 56 49 45 57 20 65 78 31 20 41 53  TEMP VIEW ex1 AS
23e09 20 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 65   SELECT a FROM e
23e0a 78 31 3b 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c  x1;.  **     SEL
23e0b 45 43 54 20 2a 20 46 52 4f 4d 20 74 65 6d 70 2e  ECT * FROM temp.
23e0c 65 78 31 3b 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ex1;.  */.  if( 
23e0d 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3c 30 20 29  pTable->nCol<0 )
23e0e 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
23e0f 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76  orMsg(pParse, "v
23e10 69 65 77 20 25 73 20 69 73 20 63 69 72 63 75 6c  iew %s is circul
23e11 61 72 6c 79 20 64 65 66 69 6e 65 64 22 2c 20 70  arly defined", p
23e12 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  Table->zName);. 
23e13 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
23e14 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c  .  assert( pTabl
23e15 65 2d 3e 6e 43 6f 6c 3e 3d 30 20 29 3b 0a 0a 20  e->nCol>=0 );.. 
23e16 20 2f 2a 20 49 66 20 77 65 20 67 65 74 20 74 68   /* If we get th
23e17 69 73 20 66 61 72 2c 20 69 74 20 6d 65 61 6e 73  is far, it means
23e18 20 77 65 20 6e 65 65 64 20 74 6f 20 63 6f 6d 70   we need to comp
23e19 75 74 65 20 74 68 65 20 74 61 62 6c 65 20 6e 61  ute the table na
23e1a 6d 65 73 2e 0a 20 20 2a 2a 20 4e 6f 74 65 20 74  mes..  ** Note t
23e1b 68 61 74 20 74 68 65 20 63 61 6c 6c 20 74 6f 20  hat the call to 
23e1c 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74  sqlite3ResultSet
23e1d 4f 66 53 65 6c 65 63 74 28 29 20 77 69 6c 6c 20  OfSelect() will 
23e1e 65 78 70 61 6e 64 20 61 6e 79 0a 20 20 2a 2a 20  expand any.  ** 
23e1f 22 2a 22 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20  "*" elements in 
23e20 74 68 65 20 72 65 73 75 6c 74 73 20 73 65 74 20  the results set 
23e21 6f 66 20 74 68 65 20 76 69 65 77 20 61 6e 64 20  of the view and 
23e22 77 69 6c 6c 20 61 73 73 69 67 6e 20 63 75 72 73  will assign curs
23e23 6f 72 73 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20  ors.  ** to the 
23e24 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20  elements of the 
23e25 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 42 75  FROM clause.  Bu
23e26 74 20 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74  t we do not want
23e27 20 74 68 65 73 65 20 63 68 61 6e 67 65 73 0a 20   these changes. 
23e28 20 2a 2a 20 74 6f 20 62 65 20 70 65 72 6d 61 6e   ** to be perman
23e29 65 6e 74 2e 20 20 53 6f 20 74 68 65 20 63 6f 6d  ent.  So the com
23e2a 70 75 74 61 74 69 6f 6e 20 69 73 20 64 6f 6e 65  putation is done
23e2b 20 6f 6e 20 61 20 63 6f 70 79 20 6f 66 20 74 68   on a copy of th
23e2c 65 20 53 45 4c 45 43 54 0a 20 20 2a 2a 20 73 74  e SELECT.  ** st
23e2d 61 74 65 6d 65 6e 74 20 74 68 61 74 20 64 65 66  atement that def
23e2e 69 6e 65 73 20 74 68 65 20 76 69 65 77 2e 0a 20  ines the view.. 
23e2f 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54   */.  assert( pT
23e30 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74 20 29 3b  able->pSelect );
23e31 0a 20 20 70 53 65 6c 20 3d 20 73 71 6c 69 74 65  .  pSel = sqlite
23e32 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70  3SelectDup(db, p
23e33 54 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74 2c 20  Table->pSelect, 
23e34 30 29 3b 0a 20 20 69 66 28 20 70 53 65 6c 20 29  0);.  if( pSel )
23e35 7b 0a 20 20 20 20 75 38 20 65 6e 61 62 6c 65 4c  {.    u8 enableL
23e36 6f 6f 6b 61 73 69 64 65 20 3d 20 64 62 2d 3e 6c  ookaside = db->l
23e37 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65  ookaside.bEnable
23e38 64 3b 0a 20 20 20 20 6e 20 3d 20 70 50 61 72 73  d;.    n = pPars
23e39 65 2d 3e 6e 54 61 62 3b 0a 20 20 20 20 73 71 6c  e->nTab;.    sql
23e3a 69 74 65 33 53 72 63 4c 69 73 74 41 73 73 69 67  ite3SrcListAssig
23e3b 6e 43 75 72 73 6f 72 73 28 70 50 61 72 73 65 2c  nCursors(pParse,
23e3c 20 70 53 65 6c 2d 3e 70 53 72 63 29 3b 0a 20 20   pSel->pSrc);.  
23e3d 20 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d    pTable->nCol =
23e3e 20 2d 31 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f   -1;.    db->loo
23e3f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20  kaside.bEnabled 
23e40 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  = 0;.#ifndef SQL
23e41 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
23e42 5a 41 54 49 4f 4e 0a 20 20 20 20 78 41 75 74 68  ZATION.    xAuth
23e43 20 3d 20 64 62 2d 3e 78 41 75 74 68 3b 0a 20 20   = db->xAuth;.  
23e44 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 30 3b    db->xAuth = 0;
23e45 0a 20 20 20 20 70 53 65 6c 54 61 62 20 3d 20 73  .    pSelTab = s
23e46 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f  qlite3ResultSetO
23e47 66 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  fSelect(pParse, 
23e48 70 53 65 6c 29 3b 0a 20 20 20 20 64 62 2d 3e 78  pSel);.    db->x
23e49 41 75 74 68 20 3d 20 78 41 75 74 68 3b 0a 23 65  Auth = xAuth;.#e
23e4a 6c 73 65 0a 20 20 20 20 70 53 65 6c 54 61 62 20  lse.    pSelTab 
23e4b 3d 20 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53  = sqlite3ResultS
23e4c 65 74 4f 66 53 65 6c 65 63 74 28 70 50 61 72 73  etOfSelect(pPars
23e4d 65 2c 20 70 53 65 6c 29 3b 0a 23 65 6e 64 69 66  e, pSel);.#endif
23e4e 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  .    db->lookasi
23e4f 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 65 6e  de.bEnabled = en
23e50 61 62 6c 65 4c 6f 6f 6b 61 73 69 64 65 3b 0a 20  ableLookaside;. 
23e51 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20     pParse->nTab 
23e52 3d 20 6e 3b 0a 20 20 20 20 69 66 28 20 70 53 65  = n;.    if( pSe
23e53 6c 54 61 62 20 29 7b 0a 20 20 20 20 20 20 61 73  lTab ){.      as
23e54 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 61 43  sert( pTable->aC
23e55 6f 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  ol==0 );.      p
23e56 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 70 53  Table->nCol = pS
23e57 65 6c 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 20  elTab->nCol;.   
23e58 20 20 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 20     pTable->aCol 
23e59 3d 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 3b  = pSelTab->aCol;
23e5a 0a 20 20 20 20 20 20 70 53 65 6c 54 61 62 2d 3e  .      pSelTab->
23e5b 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20  nCol = 0;.      
23e5c 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 20 3d 20  pSelTab->aCol = 
23e5d 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  0;.      sqlite3
23e5e 44 65 6c 65 74 65 54 61 62 6c 65 28 70 53 65 6c  DeleteTable(pSel
23e5f 54 61 62 29 3b 0a 20 20 20 20 20 20 70 54 61 62  Tab);.      pTab
23e60 6c 65 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 6c 61  le->pSchema->fla
23e61 67 73 20 7c 3d 20 44 42 5f 55 6e 72 65 73 65 74  gs |= DB_Unreset
23e62 56 69 65 77 73 3b 0a 20 20 20 20 7d 65 6c 73 65  Views;.    }else
23e63 7b 0a 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e  {.      pTable->
23e64 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20  nCol = 0;.      
23e65 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  nErr++;.    }.  
23e66 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
23e67 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 29 3b  elete(db, pSel);
23e68 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
23e69 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a 23 65 6e 64  nErr++;.  }.#end
23e6a 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
23e6b 54 5f 56 49 45 57 20 2a 2f 0a 20 20 72 65 74 75  T_VIEW */.  retu
23e6c 72 6e 20 6e 45 72 72 3b 20 20 0a 7d 0a 23 65 6e  rn nErr;  .}.#en
23e6d 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
23e6e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
23e6f 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51  ) || !defined(SQ
23e70 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
23e71 4c 54 41 42 4c 45 29 20 2a 2f 0a 0a 23 69 66 6e  LTABLE) */..#ifn
23e72 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
23e73 56 49 45 57 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72  VIEW./*.** Clear
23e74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   the column name
23e75 73 20 66 72 6f 6d 20 65 76 65 72 79 20 56 49 45  s from every VIE
23e76 57 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 64  W in database id
23e77 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  x..*/.static voi
23e78 64 20 73 71 6c 69 74 65 56 69 65 77 52 65 73 65  d sqliteViewRese
23e79 74 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62  tAll(sqlite3 *db
23e7a 2c 20 69 6e 74 20 69 64 78 29 7b 0a 20 20 48 61  , int idx){.  Ha
23e7b 73 68 45 6c 65 6d 20 2a 69 3b 0a 20 20 69 66 28  shElem *i;.  if(
23e7c 20 21 44 62 48 61 73 50 72 6f 70 65 72 74 79 28   !DbHasProperty(
23e7d 64 62 2c 20 69 64 78 2c 20 44 42 5f 55 6e 72 65  db, idx, DB_Unre
23e7e 73 65 74 56 69 65 77 73 29 20 29 20 72 65 74 75  setViews) ) retu
23e7f 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69  rn;.  for(i=sqli
23e80 74 65 48 61 73 68 46 69 72 73 74 28 26 64 62 2d  teHashFirst(&db-
23e81 3e 61 44 62 5b 69 64 78 5d 2e 70 53 63 68 65 6d  >aDb[idx].pSchem
23e82 61 2d 3e 74 62 6c 48 61 73 68 29 3b 20 69 3b 69  a->tblHash); i;i
23e83 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28  =sqliteHashNext(
23e84 69 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a  i)){.    Table *
23e85 70 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73  pTab = sqliteHas
23e86 68 44 61 74 61 28 69 29 3b 0a 20 20 20 20 69 66  hData(i);.    if
23e87 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20  ( pTab->pSelect 
23e88 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 52  ){.      sqliteR
23e89 65 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  esetColumnNames(
23e8a 70 54 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  pTab);.    }.  }
23e8b 0a 20 20 44 62 43 6c 65 61 72 50 72 6f 70 65 72  .  DbClearProper
23e8c 74 79 28 64 62 2c 20 69 64 78 2c 20 44 42 5f 55  ty(db, idx, DB_U
23e8d 6e 72 65 73 65 74 56 69 65 77 73 29 3b 0a 7d 0a  nresetViews);.}.
23e8e 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73  #else.# define s
23e8f 71 6c 69 74 65 56 69 65 77 52 65 73 65 74 41 6c  qliteViewResetAl
23e90 6c 28 41 2c 42 29 0a 23 65 6e 64 69 66 20 2f 2a  l(A,B).#endif /*
23e91 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45   SQLITE_OMIT_VIE
23e92 57 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  W */../*.** This
23e93 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
23e94 6c 65 64 20 62 79 20 74 68 65 20 56 44 42 45 20  led by the VDBE 
23e95 74 6f 20 61 64 6a 75 73 74 20 74 68 65 20 69 6e  to adjust the in
23e96 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 0a 2a 2a  ternal schema.**
23e97 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 20   used by SQLite 
23e98 77 68 65 6e 20 74 68 65 20 62 74 72 65 65 20 6c  when the btree l
23e99 61 79 65 72 20 6d 6f 76 65 73 20 61 20 74 61 62  ayer moves a tab
23e9a 6c 65 20 72 6f 6f 74 20 70 61 67 65 2e 20 54 68  le root page. Th
23e9b 65 0a 2a 2a 20 72 6f 6f 74 2d 70 61 67 65 20 6f  e.** root-page o
23e9c 66 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  f a table or ind
23e9d 65 78 20 69 6e 20 64 61 74 61 62 61 73 65 20 69  ex in database i
23e9e 44 62 20 68 61 73 20 63 68 61 6e 67 65 64 20 66  Db has changed f
23e9f 72 6f 6d 20 69 46 72 6f 6d 0a 2a 2a 20 74 6f 20  rom iFrom.** to 
23ea0 69 54 6f 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65  iTo..**.** Ticke
23ea1 74 20 23 31 37 32 38 3a 20 20 54 68 65 20 73 79  t #1728:  The sy
23ea2 6d 62 6f 6c 20 74 61 62 6c 65 20 6d 69 67 68 74  mbol table might
23ea3 20 73 74 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 69   still contain i
23ea4 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 6f 6e  nformation.** on
23ea5 20 74 61 62 6c 65 73 20 61 6e 64 2f 6f 72 20 69   tables and/or i
23ea6 6e 64 69 63 65 73 20 74 68 61 74 20 61 72 65 20  ndices that are 
23ea7 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66 20 62  the process of b
23ea8 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a 2a 2a  eing deleted..**
23ea9 20 49 66 20 79 6f 75 20 61 72 65 20 75 6e 6c 75   If you are unlu
23eaa 63 6b 79 2c 20 6f 6e 65 20 6f 66 20 74 68 6f 73  cky, one of thos
23eab 65 20 64 65 6c 65 74 65 64 20 69 6e 64 69 63 65  e deleted indice
23eac 73 20 6f 72 20 74 61 62 6c 65 73 20 6d 69 67 68  s or tables migh
23ead 74 0a 2a 2a 20 68 61 76 65 20 74 68 65 20 73 61  t.** have the sa
23eae 6d 65 20 72 6f 6f 74 70 61 67 65 20 6e 75 6d 62  me rootpage numb
23eaf 65 72 20 61 73 20 74 68 65 20 72 65 61 6c 20 74  er as the real t
23eb0 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 74 68  able or index th
23eb1 61 74 20 69 73 0a 2a 2a 20 62 65 69 6e 67 20 6d  at is.** being m
23eb2 6f 76 65 64 2e 20 20 53 6f 20 77 65 20 63 61 6e  oved.  So we can
23eb3 6e 6f 74 20 73 74 6f 70 20 73 65 61 72 63 68 69  not stop searchi
23eb4 6e 67 20 61 66 74 65 72 20 74 68 65 20 66 69 72  ng after the fir
23eb5 73 74 20 6d 61 74 63 68 20 0a 2a 2a 20 62 65 63  st match .** bec
23eb6 61 75 73 65 20 74 68 65 20 66 69 72 73 74 20 6d  ause the first m
23eb7 61 74 63 68 20 6d 69 67 68 74 20 62 65 20 66 6f  atch might be fo
23eb8 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 65 6c  r one of the del
23eb9 65 74 65 64 20 69 6e 64 69 63 65 73 0a 2a 2a 20  eted indices.** 
23eba 6f 72 20 74 61 62 6c 65 73 20 61 6e 64 20 6e 6f  or tables and no
23ebb 74 20 74 68 65 20 74 61 62 6c 65 2f 69 6e 64 65  t the table/inde
23ebc 78 20 74 68 61 74 20 69 73 20 61 63 74 75 61 6c  x that is actual
23ebd 6c 79 20 62 65 69 6e 67 20 6d 6f 76 65 64 2e 0a  ly being moved..
23ebe 2a 2a 20 57 65 20 6d 75 73 74 20 63 6f 6e 74 69  ** We must conti
23ebf 6e 75 65 20 6c 6f 6f 70 69 6e 67 20 75 6e 74 69  nue looping unti
23ec0 6c 20 61 6c 6c 20 74 61 62 6c 65 73 20 61 6e 64  l all tables and
23ec1 20 69 6e 64 69 63 65 73 20 77 69 74 68 0a 2a 2a   indices with.**
23ec2 20 72 6f 6f 74 70 61 67 65 3d 3d 69 46 72 6f 6d   rootpage==iFrom
23ec3 20 68 61 76 65 20 62 65 65 6e 20 63 6f 6e 76 65   have been conve
23ec4 72 74 65 64 20 74 6f 20 68 61 76 65 20 61 20 72  rted to have a r
23ec5 6f 6f 74 70 61 67 65 20 6f 66 20 69 54 6f 0a 2a  ootpage of iTo.*
23ec6 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 65  * in order to be
23ec7 20 63 65 72 74 61 69 6e 20 74 68 61 74 20 77 65   certain that we
23ec8 20 67 6f 74 20 74 68 65 20 72 69 67 68 74 20 6f   got the right o
23ec9 6e 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ne..*/.#ifndef S
23eca 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
23ecb 41 43 55 55 4d 0a 53 51 4c 49 54 45 5f 50 52 49  ACUUM.SQLITE_PRI
23ecc 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
23ecd 33 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 44  3RootPageMoved(D
23ece 62 20 2a 70 44 62 2c 20 69 6e 74 20 69 46 72 6f  b *pDb, int iFro
23ecf 6d 2c 20 69 6e 74 20 69 54 6f 29 7b 0a 20 20 48  m, int iTo){.  H
23ed0 61 73 68 45 6c 65 6d 20 2a 70 45 6c 65 6d 3b 0a  ashElem *pElem;.
23ed1 20 20 48 61 73 68 20 2a 70 48 61 73 68 3b 0a 0a    Hash *pHash;..
23ed2 20 20 70 48 61 73 68 20 3d 20 26 70 44 62 2d 3e    pHash = &pDb->
23ed3 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68  pSchema->tblHash
23ed4 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71  ;.  for(pElem=sq
23ed5 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 70 48  liteHashFirst(pH
23ed6 61 73 68 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c  ash); pElem; pEl
23ed7 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78  em=sqliteHashNex
23ed8 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20 54  t(pElem)){.    T
23ed9 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c  able *pTab = sql
23eda 69 74 65 48 61 73 68 44 61 74 61 28 70 45 6c 65  iteHashData(pEle
23edb 6d 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62  m);.    if( pTab
23edc 2d 3e 74 6e 75 6d 3d 3d 69 46 72 6f 6d 20 29 7b  ->tnum==iFrom ){
23edd 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 74 6e 75  .      pTab->tnu
23ede 6d 20 3d 20 69 54 6f 3b 0a 20 20 20 20 7d 0a 20  m = iTo;.    }. 
23edf 20 7d 0a 20 20 70 48 61 73 68 20 3d 20 26 70 44   }.  pHash = &pD
23ee0 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48  b->pSchema->idxH
23ee1 61 73 68 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d  ash;.  for(pElem
23ee2 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
23ee3 28 70 48 61 73 68 29 3b 20 70 45 6c 65 6d 3b 20  (pHash); pElem; 
23ee4 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68  pElem=sqliteHash
23ee5 4e 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20  Next(pElem)){.  
23ee6 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20    Index *pIdx = 
23ee7 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 70  sqliteHashData(p
23ee8 45 6c 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 70  Elem);.    if( p
23ee9 49 64 78 2d 3e 74 6e 75 6d 3d 3d 69 46 72 6f 6d  Idx->tnum==iFrom
23eea 20 29 7b 0a 20 20 20 20 20 20 70 49 64 78 2d 3e   ){.      pIdx->
23eeb 74 6e 75 6d 20 3d 20 69 54 6f 3b 0a 20 20 20 20  tnum = iTo;.    
23eec 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a  }.  }.}.#endif..
23eed 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 63 6f 64 65  /*.** Write code
23eee 20 74 6f 20 65 72 61 73 65 20 74 68 65 20 74 61   to erase the ta
23eef 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61  ble with root-pa
23ef0 67 65 20 69 54 61 62 6c 65 20 66 72 6f 6d 20 64  ge iTable from d
23ef1 61 74 61 62 61 73 65 20 69 44 62 2e 0a 2a 2a 20  atabase iDb..** 
23ef2 41 6c 73 6f 20 77 72 69 74 65 20 63 6f 64 65 20  Also write code 
23ef3 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 73 71  to modify the sq
23ef4 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c  lite_master tabl
23ef5 65 20 61 6e 64 20 69 6e 74 65 72 6e 61 6c 20 73  e and internal s
23ef6 63 68 65 6d 61 0a 2a 2a 20 69 66 20 61 20 72 6f  chema.** if a ro
23ef7 6f 74 2d 70 61 67 65 20 6f 66 20 61 6e 6f 74 68  ot-page of anoth
23ef8 65 72 20 74 61 62 6c 65 20 69 73 20 6d 6f 76 65  er table is move
23ef9 64 20 62 79 20 74 68 65 20 62 74 72 65 65 2d 6c  d by the btree-l
23efa 61 79 65 72 20 77 68 69 6c 73 74 0a 2a 2a 20 65  ayer whilst.** e
23efb 72 61 73 69 6e 67 20 69 54 61 62 6c 65 20 28 74  rasing iTable (t
23efc 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 20 77  his can happen w
23efd 69 74 68 20 61 6e 20 61 75 74 6f 2d 76 61 63 75  ith an auto-vacu
23efe 75 6d 20 64 61 74 61 62 61 73 65 29 2e 0a 2a 2f  um database)..*/
23eff 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65   .static void de
23f00 73 74 72 6f 79 52 6f 6f 74 50 61 67 65 28 50 61  stroyRootPage(Pa
23f01 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
23f02 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 69 44 62   iTable, int iDb
23f03 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73  ){.  Vdbe *v = s
23f04 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
23f05 61 72 73 65 29 3b 0a 20 20 69 6e 74 20 72 31 20  arse);.  int r1 
23f06 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
23f07 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 73  Reg(pParse);.  s
23f08 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
23f09 28 76 2c 20 4f 50 5f 44 65 73 74 72 6f 79 2c 20  (v, OP_Destroy, 
23f0a 69 54 61 62 6c 65 2c 20 72 31 2c 20 69 44 62 29  iTable, r1, iDb)
23f0b 3b 0a 20 20 73 71 6c 69 74 65 33 4d 61 79 41 62  ;.  sqlite3MayAb
23f0c 6f 72 74 28 70 50 61 72 73 65 29 3b 0a 23 69 66  ort(pParse);.#if
23f0d 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
23f0e 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 2f 2a  _AUTOVACUUM.  /*
23f0f 20 4f 50 5f 44 65 73 74 72 6f 79 20 73 74 6f 72   OP_Destroy stor
23f10 65 73 20 61 6e 20 69 6e 20 69 6e 74 65 67 65 72  es an in integer
23f11 20 72 31 2e 20 49 66 20 74 68 69 73 20 69 6e 74   r1. If this int
23f12 65 67 65 72 0a 20 20 2a 2a 20 69 73 20 6e 6f 6e  eger.  ** is non
23f13 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 69 74 20 69  -zero, then it i
23f14 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  s the root page 
23f15 6e 75 6d 62 65 72 20 6f 66 20 61 20 74 61 62 6c  number of a tabl
23f16 65 20 6d 6f 76 65 64 20 74 6f 0a 20 20 2a 2a 20  e moved to.  ** 
23f17 6c 6f 63 61 74 69 6f 6e 20 69 54 61 62 6c 65 2e  location iTable.
23f18 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
23f19 6f 64 65 20 6d 6f 64 69 66 69 65 73 20 74 68 65  ode modifies the
23f1a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
23f1b 61 62 6c 65 20 74 6f 0a 20 20 2a 2a 20 72 65 66  able to.  ** ref
23f1c 6c 65 63 74 20 74 68 69 73 2e 0a 20 20 2a 2a 0a  lect this..  **.
23f1d 20 20 2a 2a 20 54 68 65 20 22 23 4e 4e 4e 22 20    ** The "#NNN" 
23f1e 69 6e 20 74 68 65 20 53 51 4c 20 69 73 20 61 20  in the SQL is a 
23f1f 73 70 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74  special constant
23f20 20 74 68 61 74 20 6d 65 61 6e 73 20 77 68 61 74   that means what
23f21 65 76 65 72 20 76 61 6c 75 65 0a 20 20 2a 2a 20  ever value.  ** 
23f22 69 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 4e  is in register N
23f23 4e 4e 2e 20 20 53 65 65 20 67 72 61 6d 6d 61 72  NN.  See grammar
23f24 20 72 75 6c 65 73 20 61 73 73 6f 63 69 61 74 65   rules associate
23f25 64 20 77 69 74 68 20 74 68 65 20 54 4b 5f 52 45  d with the TK_RE
23f26 47 49 53 54 45 52 0a 20 20 2a 2a 20 74 6f 6b 65  GISTER.  ** toke
23f27 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  n for additional
23f28 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20   information..  
23f29 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4e 65 73 74  */.  sqlite3Nest
23f2a 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 20  edParse(pParse, 
23f2b 0a 20 20 20 20 20 22 55 50 44 41 54 45 20 25 51  .     "UPDATE %Q
23f2c 2e 25 73 20 53 45 54 20 72 6f 6f 74 70 61 67 65  .%s SET rootpage
23f2d 3d 25 64 20 57 48 45 52 45 20 23 25 64 20 41 4e  =%d WHERE #%d AN
23f2e 44 20 72 6f 6f 74 70 61 67 65 3d 23 25 64 22 2c  D rootpage=#%d",
23f2f 0a 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62  .     pParse->db
23f30 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
23f31 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  , SCHEMA_TABLE(i
23f32 44 62 29 2c 20 69 54 61 62 6c 65 2c 20 72 31 2c  Db), iTable, r1,
23f33 20 72 31 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73   r1);.#endif.  s
23f34 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
23f35 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29  pReg(pParse, r1)
23f36 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  ;.}../*.** Write
23f37 20 56 44 42 45 20 63 6f 64 65 20 74 6f 20 65 72   VDBE code to er
23f38 61 73 65 20 74 61 62 6c 65 20 70 54 61 62 20 61  ase table pTab a
23f39 6e 64 20 61 6c 6c 20 61 73 73 6f 63 69 61 74 65  nd all associate
23f3a 64 20 69 6e 64 69 63 65 73 20 6f 6e 20 64 69 73  d indices on dis
23f3b 6b 2e 0a 2a 2a 20 43 6f 64 65 20 74 6f 20 75 70  k..** Code to up
23f3c 64 61 74 65 20 74 68 65 20 73 71 6c 69 74 65 5f  date the sqlite_
23f3d 6d 61 73 74 65 72 20 74 61 62 6c 65 73 20 61 6e  master tables an
23f3e 64 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d  d internal schem
23f3f 61 20 64 65 66 69 6e 69 74 69 6f 6e 73 0a 2a 2a  a definitions.**
23f40 20 69 6e 20 63 61 73 65 20 61 20 72 6f 6f 74 2d   in case a root-
23f41 70 61 67 65 20 62 65 6c 6f 6e 67 69 6e 67 20 74  page belonging t
23f42 6f 20 61 6e 6f 74 68 65 72 20 74 61 62 6c 65 20  o another table 
23f43 69 73 20 6d 6f 76 65 64 20 62 79 20 74 68 65 20  is moved by the 
23f44 62 74 72 65 65 20 6c 61 79 65 72 0a 2a 2a 20 69  btree layer.** i
23f45 73 20 61 6c 73 6f 20 61 64 64 65 64 20 28 74 68  s also added (th
23f46 69 73 20 63 61 6e 20 68 61 70 70 65 6e 20 77 69  is can happen wi
23f47 74 68 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75  th an auto-vacuu
23f48 6d 20 64 61 74 61 62 61 73 65 29 2e 0a 2a 2f 0a  m database)..*/.
23f49 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 73 74  static void dest
23f4a 72 6f 79 54 61 62 6c 65 28 50 61 72 73 65 20 2a  royTable(Parse *
23f4b 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70  pParse, Table *p
23f4c 54 61 62 29 7b 0a 23 69 66 64 65 66 20 53 51 4c  Tab){.#ifdef SQL
23f4d 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
23f4e 55 55 4d 0a 20 20 49 6e 64 65 78 20 2a 70 49 64  UUM.  Index *pId
23f4f 78 3b 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 73  x;.  int iDb = s
23f50 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
23f51 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  dex(pParse->db, 
23f52 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
23f53 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 50 61 67    destroyRootPag
23f54 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e  e(pParse, pTab->
23f55 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 66 6f  tnum, iDb);.  fo
23f56 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
23f57 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
23f58 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
23f59 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 50 61 67    destroyRootPag
23f5a 65 28 70 50 61 72 73 65 2c 20 70 49 64 78 2d 3e  e(pParse, pIdx->
23f5b 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 7d 0a  tnum, iDb);.  }.
23f5c 23 65 6c 73 65 0a 20 20 2f 2a 20 49 66 20 74 68  #else.  /* If th
23f5d 65 20 64 61 74 61 62 61 73 65 20 6d 61 79 20 62  e database may b
23f5e 65 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 63 61  e auto-vacuum ca
23f5f 70 61 62 6c 65 20 28 69 66 20 53 51 4c 49 54 45  pable (if SQLITE
23f60 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
23f61 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 64 65 66  .  ** is not def
23f62 69 6e 65 64 29 2c 20 74 68 65 6e 20 69 74 20 69  ined), then it i
23f63 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 63  s important to c
23f64 61 6c 6c 20 4f 50 5f 44 65 73 74 72 6f 79 20 6f  all OP_Destroy o
23f65 6e 20 74 68 65 0a 20 20 2a 2a 20 74 61 62 6c 65  n the.  ** table
23f66 20 61 6e 64 20 69 6e 64 65 78 20 72 6f 6f 74 2d   and index root-
23f67 70 61 67 65 73 20 69 6e 20 6f 72 64 65 72 2c 20  pages in order, 
23f68 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 74 68  starting with th
23f69 65 20 6e 75 6d 65 72 69 63 61 6c 6c 79 20 0a 20  e numerically . 
23f6a 20 2a 2a 20 6c 61 72 67 65 73 74 20 72 6f 6f 74   ** largest root
23f6b 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 54 68  -page number. Th
23f6c 69 73 20 67 75 61 72 61 6e 74 65 65 73 20 74 68  is guarantees th
23f6d 61 74 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 72  at none of the r
23f6e 6f 6f 74 2d 70 61 67 65 73 0a 20 20 2a 2a 20 74  oot-pages.  ** t
23f6f 6f 20 62 65 20 64 65 73 74 72 6f 79 65 64 20 69  o be destroyed i
23f70 73 20 72 65 6c 6f 63 61 74 65 64 20 62 79 20 61  s relocated by a
23f71 6e 20 65 61 72 6c 69 65 72 20 4f 50 5f 44 65 73  n earlier OP_Des
23f72 74 72 6f 79 2e 20 69 2e 65 2e 20 69 66 20 74 68  troy. i.e. if th
23f73 65 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67  e.  ** following
23f74 20 77 65 72 65 20 63 6f 64 65 64 3a 0a 20 20 2a   were coded:.  *
23f75 2a 0a 20 20 2a 2a 20 4f 50 5f 44 65 73 74 72 6f  *.  ** OP_Destro
23f76 79 20 34 20 30 0a 20 20 2a 2a 20 2e 2e 2e 0a 20  y 4 0.  ** .... 
23f77 20 2a 2a 20 4f 50 5f 44 65 73 74 72 6f 79 20 35   ** OP_Destroy 5
23f78 20 30 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61 6e 64   0.  **.  ** and
23f79 20 72 6f 6f 74 20 70 61 67 65 20 35 20 68 61 70   root page 5 hap
23f7a 70 65 6e 65 64 20 74 6f 20 62 65 20 74 68 65 20  pened to be the 
23f7b 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67  largest root-pag
23f7c 65 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 0a  e number in the.
23f7d 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2c 20 74    ** database, t
23f7e 68 65 6e 20 72 6f 6f 74 20 70 61 67 65 20 35 20  hen root page 5 
23f7f 77 6f 75 6c 64 20 62 65 20 6d 6f 76 65 64 20 74  would be moved t
23f80 6f 20 70 61 67 65 20 34 20 62 79 20 74 68 65 20  o page 4 by the 
23f81 0a 20 20 2a 2a 20 22 4f 50 5f 44 65 73 74 72 6f  .  ** "OP_Destro
23f82 79 20 34 20 30 22 20 6f 70 63 6f 64 65 2e 20 54  y 4 0" opcode. T
23f83 68 65 20 73 75 62 73 65 71 75 65 6e 74 20 22 4f  he subsequent "O
23f84 50 5f 44 65 73 74 72 6f 79 20 35 20 30 22 20 77  P_Destroy 5 0" w
23f85 6f 75 6c 64 20 68 69 74 0a 20 20 2a 2a 20 61 20  ould hit.  ** a 
23f86 66 72 65 65 2d 6c 69 73 74 20 70 61 67 65 2e 0a  free-list page..
23f87 20 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 20    */.  int iTab 
23f88 3d 20 70 54 61 62 2d 3e 74 6e 75 6d 3b 0a 20 20  = pTab->tnum;.  
23f89 69 6e 74 20 69 44 65 73 74 72 6f 79 65 64 20 3d  int iDestroyed =
23f8a 20 30 3b 0a 0a 20 20 77 68 69 6c 65 28 20 31 20   0;..  while( 1 
23f8b 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49  ){.    Index *pI
23f8c 64 78 3b 0a 20 20 20 20 69 6e 74 20 69 4c 61 72  dx;.    int iLar
23f8d 67 65 73 74 20 3d 20 30 3b 0a 0a 20 20 20 20 69  gest = 0;..    i
23f8e 66 28 20 69 44 65 73 74 72 6f 79 65 64 3d 3d 30  f( iDestroyed==0
23f8f 20 7c 7c 20 69 54 61 62 3c 69 44 65 73 74 72 6f   || iTab<iDestro
23f90 79 65 64 20 29 7b 0a 20 20 20 20 20 20 69 4c 61  yed ){.      iLa
23f91 72 67 65 73 74 20 3d 20 69 54 61 62 3b 0a 20 20  rgest = iTab;.  
23f92 20 20 7d 0a 20 20 20 20 66 6f 72 28 70 49 64 78    }.    for(pIdx
23f93 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
23f94 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
23f95 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 6e  pNext){.      in
23f96 74 20 69 49 64 78 20 3d 20 70 49 64 78 2d 3e 74  t iIdx = pIdx->t
23f97 6e 75 6d 3b 0a 20 20 20 20 20 20 61 73 73 65 72  num;.      asser
23f98 74 28 20 70 49 64 78 2d 3e 70 53 63 68 65 6d 61  t( pIdx->pSchema
23f99 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20  ==pTab->pSchema 
23f9a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 69 44  );.      if( (iD
23f9b 65 73 74 72 6f 79 65 64 3d 3d 30 20 7c 7c 20 28  estroyed==0 || (
23f9c 69 49 64 78 3c 69 44 65 73 74 72 6f 79 65 64 29  iIdx<iDestroyed)
23f9d 29 20 26 26 20 69 49 64 78 3e 69 4c 61 72 67 65  ) && iIdx>iLarge
23f9e 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4c  st ){.        iL
23f9f 61 72 67 65 73 74 20 3d 20 69 49 64 78 3b 0a 20  argest = iIdx;. 
23fa0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
23fa1 20 69 66 28 20 69 4c 61 72 67 65 73 74 3d 3d 30   if( iLargest==0
23fa2 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
23fa3 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
23fa4 20 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c     int iDb = sql
23fa5 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
23fa6 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  x(pParse->db, pT
23fa7 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
23fa8 20 20 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 50      destroyRootP
23fa9 61 67 65 28 70 50 61 72 73 65 2c 20 69 4c 61 72  age(pParse, iLar
23faa 67 65 73 74 2c 20 69 44 62 29 3b 0a 20 20 20 20  gest, iDb);.    
23fab 20 20 69 44 65 73 74 72 6f 79 65 64 20 3d 20 69    iDestroyed = i
23fac 4c 61 72 67 65 73 74 3b 0a 20 20 20 20 7d 0a 20  Largest;.    }. 
23fad 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a   }.#endif.}../*.
23fae 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
23faf 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 64 6f 20  is called to do 
23fb0 74 68 65 20 77 6f 72 6b 20 6f 66 20 61 20 44 52  the work of a DR
23fb1 4f 50 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  OP TABLE stateme
23fb2 6e 74 2e 0a 2a 2a 20 70 4e 61 6d 65 20 69 73 20  nt..** pName is 
23fb3 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
23fb4 74 61 62 6c 65 20 74 6f 20 62 65 20 64 72 6f 70  table to be drop
23fb5 70 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ped..*/.SQLITE_P
23fb6 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
23fb7 74 65 33 44 72 6f 70 54 61 62 6c 65 28 50 61 72  te3DropTable(Par
23fb8 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c  se *pParse, SrcL
23fb9 69 73 74 20 2a 70 4e 61 6d 65 2c 20 69 6e 74 20  ist *pName, int 
23fba 69 73 56 69 65 77 2c 20 69 6e 74 20 6e 6f 45 72  isView, int noEr
23fbb 72 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  r){.  Table *pTa
23fbc 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  b;.  Vdbe *v;.  
23fbd 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
23fbe 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20  arse->db;.  int 
23fbf 69 44 62 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e  iDb;..  if( db->
23fc0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
23fc1 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
23fc2 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20  op_table;.  }.  
23fc3 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
23fc4 6e 45 72 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73  nErr==0 );.  ass
23fc5 65 72 74 28 20 70 4e 61 6d 65 2d 3e 6e 53 72 63  ert( pName->nSrc
23fc6 3d 3d 31 20 29 3b 0a 20 20 70 54 61 62 20 3d 20  ==1 );.  pTab = 
23fc7 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62  sqlite3LocateTab
23fc8 6c 65 28 70 50 61 72 73 65 2c 20 69 73 56 69 65  le(pParse, isVie
23fc9 77 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  w, .            
23fca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23fcb 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d  pName->a[0].zNam
23fcc 65 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a  e, pName->a[0].z
23fcd 44 61 74 61 62 61 73 65 29 3b 0a 0a 20 20 69 66  Database);..  if
23fce 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20  ( pTab==0 ){.   
23fcf 20 69 66 28 20 6e 6f 45 72 72 20 29 7b 0a 20 20   if( noErr ){.  
23fd0 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
23fd1 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20  Clear(pParse);. 
23fd2 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 65 78     }.    goto ex
23fd3 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20  it_drop_table;. 
23fd4 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74   }.  iDb = sqlit
23fd5 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
23fd6 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
23fd7 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44  a);.  assert( iD
23fd8 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e  b>=0 && iDb<db->
23fd9 6e 44 62 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  nDb );..  /* If 
23fda 70 54 61 62 20 69 73 20 61 20 76 69 72 74 75 61  pTab is a virtua
23fdb 6c 20 74 61 62 6c 65 2c 20 63 61 6c 6c 20 56 69  l table, call Vi
23fdc 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73  ewGetColumnNames
23fdd 28 29 20 74 6f 20 65 6e 73 75 72 65 0a 20 20 2a  () to ensure.  *
23fde 2a 20 69 74 20 69 73 20 69 6e 69 74 69 61 6c 69  * it is initiali
23fdf 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  zed..  */.  if( 
23fe0 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
23fe1 26 26 20 73 71 6c 69 74 65 33 56 69 65 77 47 65  && sqlite3ViewGe
23fe2 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61  tColumnNames(pPa
23fe3 72 73 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20  rse, pTab) ){.  
23fe4 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
23fe5 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 69 66 6e  _table;.  }.#ifn
23fe6 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
23fe7 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20  AUTHORIZATION.  
23fe8 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64 65 3b 0a  {.    int code;.
23fe9 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
23fea 7a 54 61 62 20 3d 20 53 43 48 45 4d 41 5f 54 41  zTab = SCHEMA_TA
23feb 42 4c 45 28 69 44 62 29 3b 0a 20 20 20 20 63 6f  BLE(iDb);.    co
23fec 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20  nst char *zDb = 
23fed 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
23fee 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  me;.    const ch
23fef 61 72 20 2a 7a 41 72 67 32 20 3d 20 30 3b 0a 20  ar *zArg2 = 0;. 
23ff0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75     if( sqlite3Au
23ff1 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
23ff2 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a  SQLITE_DELETE, z
23ff3 54 61 62 2c 20 30 2c 20 7a 44 62 29 29 7b 0a 20  Tab, 0, zDb)){. 
23ff4 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64       goto exit_d
23ff5 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d  rop_table;.    }
23ff6 0a 20 20 20 20 69 66 28 20 69 73 56 69 65 77 20  .    if( isView 
23ff7 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d  ){.      if( !OM
23ff8 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62  IT_TEMPDB && iDb
23ff9 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 63  ==1 ){.        c
23ffa 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f  ode = SQLITE_DRO
23ffb 50 5f 54 45 4d 50 5f 56 49 45 57 3b 0a 20 20 20  P_TEMP_VIEW;.   
23ffc 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
23ffd 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f    code = SQLITE_
23ffe 44 52 4f 50 5f 56 49 45 57 3b 0a 20 20 20 20 20  DROP_VIEW;.     
23fff 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
24000 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
24001 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  BLE.    }else if
24002 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
24003 29 20 29 7b 0a 20 20 20 20 20 20 63 6f 64 65 20  ) ){.      code 
24004 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54  = SQLITE_DROP_VT
24005 41 42 4c 45 3b 0a 20 20 20 20 20 20 7a 41 72 67  ABLE;.      zArg
24006 32 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 54  2 = sqlite3GetVT
24007 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29 2d 3e  able(db, pTab)->
24008 70 4d 6f 64 2d 3e 7a 4e 61 6d 65 3b 0a 23 65 6e  pMod->zName;.#en
24009 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  dif.    }else{. 
2400a 20 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54       if( !OMIT_T
2400b 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20  EMPDB && iDb==1 
2400c 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20  ){.        code 
2400d 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45  = SQLITE_DROP_TE
2400e 4d 50 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20 20  MP_TABLE;.      
2400f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63  }else{.        c
24010 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f  ode = SQLITE_DRO
24011 50 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d  P_TABLE;.      }
24012 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
24013 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
24014 70 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 70 54  pParse, code, pT
24015 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 41 72 67 32  ab->zName, zArg2
24016 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20  , zDb) ){.      
24017 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74  goto exit_drop_t
24018 61 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20  able;.    }.    
24019 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43  if( sqlite3AuthC
2401a 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
2401b 49 54 45 5f 44 45 4c 45 54 45 2c 20 70 54 61 62  ITE_DELETE, pTab
2401c 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 20 7a 44 62 29  ->zName, 0, zDb)
2401d 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65   ){.      goto e
2401e 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a  xit_drop_table;.
2401f 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
24020 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
24021 72 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61  rNICmp(pTab->zNa
24022 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37  me, "sqlite_", 7
24023 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  )==0 ){.    sqli
24024 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
24025 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20 6d 61  se, "table %s ma
24026 79 20 6e 6f 74 20 62 65 20 64 72 6f 70 70 65 64  y not be dropped
24027 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  ", pTab->zName);
24028 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64  .    goto exit_d
24029 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a  rop_table;.  }..
2402a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2402b 4d 49 54 5f 56 49 45 57 0a 20 20 2f 2a 20 45 6e  MIT_VIEW.  /* En
2402c 73 75 72 65 20 44 52 4f 50 20 54 41 42 4c 45 20  sure DROP TABLE 
2402d 69 73 20 6e 6f 74 20 75 73 65 64 20 6f 6e 20 61  is not used on a
2402e 20 76 69 65 77 2c 20 61 6e 64 20 44 52 4f 50 20   view, and DROP 
2402f 56 49 45 57 20 69 73 20 6e 6f 74 20 75 73 65 64  VIEW is not used
24030 0a 20 20 2a 2a 20 6f 6e 20 61 20 74 61 62 6c 65  .  ** on a table
24031 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 56  ..  */.  if( isV
24032 69 65 77 20 26 26 20 70 54 61 62 2d 3e 70 53 65  iew && pTab->pSe
24033 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73  lect==0 ){.    s
24034 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
24035 50 61 72 73 65 2c 20 22 75 73 65 20 44 52 4f 50  Parse, "use DROP
24036 20 54 41 42 4c 45 20 74 6f 20 64 65 6c 65 74 65   TABLE to delete
24037 20 74 61 62 6c 65 20 25 73 22 2c 20 70 54 61 62   table %s", pTab
24038 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f  ->zName);.    go
24039 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62  to exit_drop_tab
2403a 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 69  le;.  }.  if( !i
2403b 73 56 69 65 77 20 26 26 20 70 54 61 62 2d 3e 70  sView && pTab->p
2403c 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71  Select ){.    sq
2403d 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2403e 61 72 73 65 2c 20 22 75 73 65 20 44 52 4f 50 20  arse, "use DROP 
2403f 56 49 45 57 20 74 6f 20 64 65 6c 65 74 65 20 76  VIEW to delete v
24040 69 65 77 20 25 73 22 2c 20 70 54 61 62 2d 3e 7a  iew %s", pTab->z
24041 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20  Name);.    goto 
24042 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b  exit_drop_table;
24043 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
24044 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
24045 74 6f 20 72 65 6d 6f 76 65 20 74 68 65 20 74 61  to remove the ta
24046 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 6d 61 73  ble from the mas
24047 74 65 72 20 74 61 62 6c 65 0a 20 20 2a 2a 20 6f  ter table.  ** o
24048 6e 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 76  n disk..  */.  v
24049 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
2404a 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
2404b 20 76 20 29 7b 0a 20 20 20 20 54 72 69 67 67 65   v ){.    Trigge
2404c 72 20 2a 70 54 72 69 67 67 65 72 3b 0a 20 20 20  r *pTrigger;.   
2404d 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e   Db *pDb = &db->
2404e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 20 20 73 71  aDb[iDb];.    sq
2404f 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
24050 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c  peration(pParse,
24051 20 31 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64   1, iDb);..#ifnd
24052 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
24053 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20  IRTUALTABLE.    
24054 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
24055 61 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ab) ){.      sql
24056 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76  ite3VdbeAddOp0(v
24057 2c 20 4f 50 5f 56 42 65 67 69 6e 29 3b 0a 20 20  , OP_VBegin);.  
24058 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
24059 2f 2a 20 44 72 6f 70 20 61 6c 6c 20 74 72 69 67  /* Drop all trig
2405a 67 65 72 73 20 61 73 73 6f 63 69 61 74 65 64 20  gers associated 
2405b 77 69 74 68 20 74 68 65 20 74 61 62 6c 65 20 62  with the table b
2405c 65 69 6e 67 20 64 72 6f 70 70 65 64 2e 20 43 6f  eing dropped. Co
2405d 64 65 0a 20 20 20 20 2a 2a 20 69 73 20 67 65 6e  de.    ** is gen
2405e 65 72 61 74 65 64 20 74 6f 20 72 65 6d 6f 76 65  erated to remove
2405f 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 73 71   entries from sq
24060 6c 69 74 65 5f 6d 61 73 74 65 72 20 61 6e 64 2f  lite_master and/
24061 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  or.    ** sqlite
24062 5f 74 65 6d 70 5f 6d 61 73 74 65 72 20 69 66 20  _temp_master if 
24063 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f  required..    */
24064 0a 20 20 20 20 70 54 72 69 67 67 65 72 20 3d 20  .    pTrigger = 
24065 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69  sqlite3TriggerLi
24066 73 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 29  st(pParse, pTab)
24067 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 54 72  ;.    while( pTr
24068 69 67 67 65 72 20 29 7b 0a 20 20 20 20 20 20 61  igger ){.      a
24069 73 73 65 72 74 28 20 70 54 72 69 67 67 65 72 2d  ssert( pTrigger-
2406a 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e  >pSchema==pTab->
2406b 70 53 63 68 65 6d 61 20 7c 7c 20 0a 20 20 20 20  pSchema || .    
2406c 20 20 20 20 20 20 70 54 72 69 67 67 65 72 2d 3e        pTrigger->
2406d 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 62  pSchema==db->aDb
2406e 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 3b 0a 20  [1].pSchema );. 
2406f 20 20 20 20 20 73 71 6c 69 74 65 33 44 72 6f 70       sqlite3Drop
24070 54 72 69 67 67 65 72 50 74 72 28 70 50 61 72 73  TriggerPtr(pPars
24071 65 2c 20 70 54 72 69 67 67 65 72 29 3b 0a 20 20  e, pTrigger);.  
24072 20 20 20 20 70 54 72 69 67 67 65 72 20 3d 20 70      pTrigger = p
24073 54 72 69 67 67 65 72 2d 3e 70 4e 65 78 74 3b 0a  Trigger->pNext;.
24074 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53      }..#ifndef S
24075 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
24076 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 2f 2a 20  NCREMENT.    /* 
24077 52 65 6d 6f 76 65 20 61 6e 79 20 65 6e 74 72 69  Remove any entri
24078 65 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  es of the sqlite
24079 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20  _sequence table 
2407a 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a  associated with.
2407b 20 20 20 20 2a 2a 20 74 68 65 20 74 61 62 6c 65      ** the table
2407c 20 62 65 69 6e 67 20 64 72 6f 70 70 65 64 2e 20   being dropped. 
2407d 54 68 69 73 20 69 73 20 64 6f 6e 65 20 62 65 66  This is done bef
2407e 6f 72 65 20 74 68 65 20 74 61 62 6c 65 20 69 73  ore the table is
2407f 20 64 72 6f 70 70 65 64 0a 20 20 20 20 2a 2a 20   dropped.    ** 
24080 61 74 20 74 68 65 20 62 74 72 65 65 20 6c 65 76  at the btree lev
24081 65 6c 2c 20 69 6e 20 63 61 73 65 20 74 68 65 20  el, in case the 
24082 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20  sqlite_sequence 
24083 74 61 62 6c 65 20 6e 65 65 64 73 20 74 6f 0a 20  table needs to. 
24084 20 20 20 2a 2a 20 6d 6f 76 65 20 61 73 20 61 20     ** move as a 
24085 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 64 72  result of the dr
24086 6f 70 20 28 63 61 6e 20 68 61 70 70 65 6e 20 69  op (can happen i
24087 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f  n auto-vacuum mo
24088 64 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  de)..    */.    
24089 69 66 28 20 70 54 61 62 2d 3e 74 61 62 46 6c 61  if( pTab->tabFla
2408a 67 73 20 26 20 54 46 5f 41 75 74 6f 69 6e 63 72  gs & TF_Autoincr
2408b 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 73  ement ){.      s
2408c 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
2408d 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20  e(pParse,.      
2408e 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25    "DELETE FROM %
2408f 73 2e 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  s.sqlite_sequenc
24090 65 20 57 48 45 52 45 20 6e 61 6d 65 3d 25 51 22  e WHERE name=%Q"
24091 2c 0a 20 20 20 20 20 20 20 20 70 44 62 2d 3e 7a  ,.        pDb->z
24092 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  Name, pTab->zNam
24093 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d  e.      );.    }
24094 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20  .#endif..    /* 
24095 44 72 6f 70 20 61 6c 6c 20 53 51 4c 49 54 45 5f  Drop all SQLITE_
24096 4d 41 53 54 45 52 20 74 61 62 6c 65 20 61 6e 64  MASTER table and
24097 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20 74   index entries t
24098 68 61 74 20 72 65 66 65 72 20 74 6f 20 74 68 65  hat refer to the
24099 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65 2e 20 54  .    ** table. T
2409a 68 65 20 70 72 6f 67 72 61 6d 20 6e 61 6d 65 20  he program name 
2409b 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 74 68  loops through th
2409c 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 20 61  e master table a
2409d 6e 64 20 64 65 6c 65 74 65 73 0a 20 20 20 20 2a  nd deletes.    *
2409e 2a 20 65 76 65 72 79 20 72 6f 77 20 74 68 61 74  * every row that
2409f 20 72 65 66 65 72 73 20 74 6f 20 61 20 74 61 62   refers to a tab
240a0 6c 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e  le of the same n
240a1 61 6d 65 20 61 73 20 74 68 65 20 6f 6e 65 20 62  ame as the one b
240a2 65 69 6e 67 0a 20 20 20 20 2a 2a 20 64 72 6f 70  eing.    ** drop
240a3 70 65 64 2e 20 54 72 69 67 67 65 72 73 20 61 72  ped. Triggers ar
240a4 65 20 68 61 6e 64 6c 65 64 20 73 65 70 65 72 61  e handled sepera
240a5 74 65 6c 79 20 62 65 63 61 75 73 65 20 61 20 74  tely because a t
240a6 72 69 67 67 65 72 20 63 61 6e 20 62 65 0a 20 20  rigger can be.  
240a7 20 20 2a 2a 20 63 72 65 61 74 65 64 20 69 6e 20    ** created in 
240a8 74 68 65 20 74 65 6d 70 20 64 61 74 61 62 61 73  the temp databas
240a9 65 20 74 68 61 74 20 72 65 66 65 72 73 20 74 6f  e that refers to
240aa 20 61 20 74 61 62 6c 65 20 69 6e 20 61 6e 6f 74   a table in anot
240ab 68 65 72 0a 20 20 20 20 2a 2a 20 64 61 74 61 62  her.    ** datab
240ac 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ase..    */.    
240ad 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
240ae 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  se(pParse, .    
240af 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d      "DELETE FROM
240b0 20 25 51 2e 25 73 20 57 48 45 52 45 20 74 62 6c   %Q.%s WHERE tbl
240b1 5f 6e 61 6d 65 3d 25 51 20 61 6e 64 20 74 79 70  _name=%Q and typ
240b2 65 21 3d 27 74 72 69 67 67 65 72 27 22 2c 0a 20  e!='trigger'",. 
240b3 20 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d         pDb->zNam
240b4 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28  e, SCHEMA_TABLE(
240b5 69 44 62 29 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  iDb), pTab->zNam
240b6 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70  e);..    /* Drop
240b7 20 61 6e 79 20 73 74 61 74 69 73 74 69 63 73 20   any statistics 
240b8 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74 65 5f  from the sqlite_
240b9 73 74 61 74 31 20 74 61 62 6c 65 2c 20 69 66 20  stat1 table, if 
240ba 69 74 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20  it exists */.   
240bb 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64   if( sqlite3Find
240bc 54 61 62 6c 65 28 64 62 2c 20 22 73 71 6c 69 74  Table(db, "sqlit
240bd 65 5f 73 74 61 74 31 22 2c 20 64 62 2d 3e 61 44  e_stat1", db->aD
240be 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 20 29 7b  b[iDb].zName) ){
240bf 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e 65  .      sqlite3Ne
240c0 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
240c1 2c 0a 20 20 20 20 20 20 20 20 22 44 45 4c 45 54  ,.        "DELET
240c2 45 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69 74 65  E FROM %Q.sqlite
240c3 5f 73 74 61 74 31 20 57 48 45 52 45 20 74 62 6c  _stat1 WHERE tbl
240c4 3d 25 51 22 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65  =%Q", pDb->zName
240c5 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 0a 20 20  , pTab->zName.  
240c6 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20      );.    }..  
240c7 20 20 69 66 28 20 21 69 73 56 69 65 77 20 26 26    if( !isView &&
240c8 20 21 49 73 56 69 72 74 75 61 6c 28 70 54 61 62   !IsVirtual(pTab
240c9 29 20 29 7b 0a 20 20 20 20 20 20 64 65 73 74 72  ) ){.      destr
240ca 6f 79 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  oyTable(pParse, 
240cb 70 54 61 62 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  pTab);.    }..  
240cc 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20    /* Remove the 
240cd 74 61 62 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d  table entry from
240ce 20 53 51 4c 69 74 65 27 73 20 69 6e 74 65 72 6e   SQLite's intern
240cf 61 6c 20 73 63 68 65 6d 61 20 61 6e 64 20 6d 6f  al schema and mo
240d0 64 69 66 79 0a 20 20 20 20 2a 2a 20 74 68 65 20  dify.    ** the 
240d1 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 0a 20  schema cookie.. 
240d2 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 49 73     */.    if( Is
240d3 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
240d4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
240d5 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56  beAddOp4(v, OP_V
240d6 44 65 73 74 72 6f 79 2c 20 69 44 62 2c 20 30 2c  Destroy, iDb, 0,
240d7 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c   0, pTab->zName,
240d8 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73   0);.    }.    s
240d9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
240da 28 76 2c 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65  (v, OP_DropTable
240db 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 70 54 61  , iDb, 0, 0, pTa
240dc 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  b->zName, 0);.  
240dd 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43    sqlite3ChangeC
240de 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44  ookie(pParse, iD
240df 62 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  b);.  }.  sqlite
240e0 56 69 65 77 52 65 73 65 74 41 6c 6c 28 64 62 2c  ViewResetAll(db,
240e1 20 69 44 62 29 3b 0a 0a 65 78 69 74 5f 64 72 6f   iDb);..exit_dro
240e2 70 5f 74 61 62 6c 65 3a 0a 20 20 73 71 6c 69 74  p_table:.  sqlit
240e3 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28  e3SrcListDelete(
240e4 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f  db, pName);.}../
240e5 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
240e6 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 63  e is called to c
240e7 72 65 61 74 65 20 61 20 6e 65 77 20 66 6f 72 65  reate a new fore
240e8 69 67 6e 20 6b 65 79 20 6f 6e 20 74 68 65 20 74  ign key on the t
240e9 61 62 6c 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c  able.** currentl
240ea 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  y under construc
240eb 74 69 6f 6e 2e 20 20 70 46 72 6f 6d 43 6f 6c 20  tion.  pFromCol 
240ec 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69 63 68  determines which
240ed 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74   columns.** in t
240ee 68 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65  he current table
240ef 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 6f   point to the fo
240f0 72 65 69 67 6e 20 6b 65 79 2e 20 20 49 66 20 70  reign key.  If p
240f1 46 72 6f 6d 43 6f 6c 3d 3d 30 20 74 68 65 6e 0a  FromCol==0 then.
240f2 2a 2a 20 63 6f 6e 6e 65 63 74 20 74 68 65 20 6b  ** connect the k
240f3 65 79 20 74 6f 20 74 68 65 20 6c 61 73 74 20 63  ey to the last c
240f4 6f 6c 75 6d 6e 20 69 6e 73 65 72 74 65 64 2e 20  olumn inserted. 
240f5 20 70 54 6f 20 69 73 20 74 68 65 20 6e 61 6d 65   pTo is the name
240f6 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65   of.** the table
240f7 20 72 65 66 65 72 72 65 64 20 74 6f 2e 20 20 70   referred to.  p
240f8 54 6f 43 6f 6c 20 69 73 20 61 20 6c 69 73 74 20  ToCol is a list 
240f9 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  of tables in the
240fa 20 6f 74 68 65 72 0a 2a 2a 20 70 54 6f 20 74 61   other.** pTo ta
240fb 62 6c 65 20 74 68 61 74 20 74 68 65 20 66 6f 72  ble that the for
240fc 65 69 67 6e 20 6b 65 79 20 70 6f 69 6e 74 73 20  eign key points 
240fd 74 6f 2e 20 20 66 6c 61 67 73 20 63 6f 6e 74 61  to.  flags conta
240fe 69 6e 73 20 61 6c 6c 0a 2a 2a 20 69 6e 66 6f 72  ins all.** infor
240ff 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mation about the
24100 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75   conflict resolu
24101 74 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20  tion algorithms 
24102 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 69 6e 20  specified.** in 
24103 74 68 65 20 4f 4e 20 44 45 4c 45 54 45 2c 20 4f  the ON DELETE, O
24104 4e 20 55 50 44 41 54 45 20 61 6e 64 20 4f 4e 20  N UPDATE and ON 
24105 49 4e 53 45 52 54 20 63 6c 61 75 73 65 73 2e 0a  INSERT clauses..
24106 2a 2a 0a 2a 2a 20 41 6e 20 46 4b 65 79 20 73 74  **.** An FKey st
24107 72 75 63 74 75 72 65 20 69 73 20 63 72 65 61 74  ructure is creat
24108 65 64 20 61 6e 64 20 61 64 64 65 64 20 74 6f 20  ed and added to 
24109 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e  the table curren
2410a 74 6c 79 0a 2a 2a 20 75 6e 64 65 72 20 63 6f 6e  tly.** under con
2410b 73 74 72 75 63 74 69 6f 6e 20 69 6e 20 74 68 65  struction in the
2410c 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
2410d 6c 65 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20  le field..**.** 
2410e 54 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  The foreign key 
2410f 69 73 20 73 65 74 20 66 6f 72 20 49 4d 4d 45 44  is set for IMMED
24110 49 41 54 45 20 70 72 6f 63 65 73 73 69 6e 67 2e  IATE processing.
24111 20 20 41 20 73 75 62 73 65 71 75 65 6e 74 20 63    A subsequent c
24112 61 6c 6c 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65  all.** to sqlite
24113 33 44 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79  3DeferForeignKey
24114 28 29 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20  () might change 
24115 74 68 69 73 20 74 6f 20 44 45 46 45 52 52 45 44  this to DEFERRED
24116 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
24117 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
24118 43 72 65 61 74 65 46 6f 72 65 69 67 6e 4b 65 79  CreateForeignKey
24119 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2411a 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73  e,       /* Pars
2411b 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
2411c 20 45 78 70 72 4c 69 73 74 20 2a 70 46 72 6f 6d   ExprList *pFrom
2411d 43 6f 6c 2c 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73  Col,  /* Columns
2411e 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 74   in this table t
2411f 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 74 68  hat point to oth
24120 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 54 6f  er table */.  To
24121 6b 65 6e 20 2a 70 54 6f 2c 20 20 20 20 20 20 20  ken *pTo,       
24122 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
24123 65 20 6f 74 68 65 72 20 74 61 62 6c 65 20 2a 2f  e other table */
24124 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 54 6f  .  ExprList *pTo
24125 43 6f 6c 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 6d  Col,    /* Colum
24126 6e 73 20 69 6e 20 74 68 65 20 6f 74 68 65 72 20  ns in the other 
24127 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66  table */.  int f
24128 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20  lags            
24129 2f 2a 20 43 6f 6e 66 6c 69 63 74 20 72 65 73 6f  /* Conflict reso
2412a 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d  lution algorithm
2412b 73 2e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  s. */.){.  sqlit
2412c 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2412d 3e 64 62 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  >db;.#ifndef SQL
2412e 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e  ITE_OMIT_FOREIGN
2412f 5f 4b 45 59 0a 20 20 46 4b 65 79 20 2a 70 46 4b  _KEY.  FKey *pFK
24130 65 79 20 3d 20 30 3b 0a 20 20 54 61 62 6c 65 20  ey = 0;.  Table 
24131 2a 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65  *p = pParse->pNe
24132 77 54 61 62 6c 65 3b 0a 20 20 69 6e 74 20 6e 42  wTable;.  int nB
24133 79 74 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  yte;.  int i;.  
24134 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 63 68 61 72  int nCol;.  char
24135 20 2a 7a 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   *z;..  assert( 
24136 70 54 6f 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  pTo!=0 );.  if( 
24137 70 3d 3d 30 20 7c 7c 20 49 4e 5f 44 45 43 4c 41  p==0 || IN_DECLA
24138 52 45 5f 56 54 41 42 20 29 20 67 6f 74 6f 20 66  RE_VTAB ) goto f
24139 6b 5f 65 6e 64 3b 0a 20 20 69 66 28 20 70 46 72  k_end;.  if( pFr
2413a 6f 6d 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20  omCol==0 ){.    
2413b 69 6e 74 20 69 43 6f 6c 20 3d 20 70 2d 3e 6e 43  int iCol = p->nC
2413c 6f 6c 2d 31 3b 0a 20 20 20 20 69 66 28 20 4e 45  ol-1;.    if( NE
2413d 56 45 52 28 69 43 6f 6c 3c 30 29 20 29 20 67 6f  VER(iCol<0) ) go
2413e 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 20 20 69  to fk_end;.    i
2413f 66 28 20 70 54 6f 43 6f 6c 20 26 26 20 70 54 6f  f( pToCol && pTo
24140 43 6f 6c 2d 3e 6e 45 78 70 72 21 3d 31 20 29 7b  Col->nExpr!=1 ){
24141 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
24142 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
24143 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 25  foreign key on %
24144 73 22 0a 20 20 20 20 20 20 20 20 20 22 20 73 68  s".         " sh
24145 6f 75 6c 64 20 72 65 66 65 72 65 6e 63 65 20 6f  ould reference o
24146 6e 6c 79 20 6f 6e 65 20 63 6f 6c 75 6d 6e 20 6f  nly one column o
24147 66 20 74 61 62 6c 65 20 25 54 22 2c 0a 20 20 20  f table %T",.   
24148 20 20 20 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 43        p->aCol[iC
24149 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 70 54 6f 29 3b  ol].zName, pTo);
2414a 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65  .      goto fk_e
2414b 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 43  nd;.    }.    nC
2414c 6f 6c 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 20  ol = 1;.  }else 
2414d 69 66 28 20 70 54 6f 43 6f 6c 20 26 26 20 70 54  if( pToCol && pT
2414e 6f 43 6f 6c 2d 3e 6e 45 78 70 72 21 3d 70 46 72  oCol->nExpr!=pFr
2414f 6f 6d 43 6f 6c 2d 3e 6e 45 78 70 72 20 29 7b 0a  omCol->nExpr ){.
24150 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
24151 4d 73 67 28 70 50 61 72 73 65 2c 0a 20 20 20 20  Msg(pParse,.    
24152 20 20 20 20 22 6e 75 6d 62 65 72 20 6f 66 20 63      "number of c
24153 6f 6c 75 6d 6e 73 20 69 6e 20 66 6f 72 65 69 67  olumns in foreig
24154 6e 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74 20 6d  n key does not m
24155 61 74 63 68 20 74 68 65 20 6e 75 6d 62 65 72 20  atch the number 
24156 6f 66 20 22 0a 20 20 20 20 20 20 20 20 22 63 6f  of ".        "co
24157 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 66  lumns in the ref
24158 65 72 65 6e 63 65 64 20 74 61 62 6c 65 22 29 3b  erenced table");
24159 0a 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64  .    goto fk_end
2415a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e  ;.  }else{.    n
2415b 43 6f 6c 20 3d 20 70 46 72 6f 6d 43 6f 6c 2d 3e  Col = pFromCol->
2415c 6e 45 78 70 72 3b 0a 20 20 7d 0a 20 20 6e 42 79  nExpr;.  }.  nBy
2415d 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 46 4b  te = sizeof(*pFK
2415e 65 79 29 20 2b 20 28 6e 43 6f 6c 2d 31 29 2a 73  ey) + (nCol-1)*s
2415f 69 7a 65 6f 66 28 70 46 4b 65 79 2d 3e 61 43 6f  izeof(pFKey->aCo
24160 6c 5b 30 5d 29 20 2b 20 70 54 6f 2d 3e 6e 20 2b  l[0]) + pTo->n +
24161 20 31 3b 0a 20 20 69 66 28 20 70 54 6f 43 6f 6c   1;.  if( pToCol
24162 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
24163 20 69 3c 70 54 6f 43 6f 6c 2d 3e 6e 45 78 70 72   i<pToCol->nExpr
24164 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6e 42  ; i++){.      nB
24165 79 74 65 20 2b 3d 20 73 71 6c 69 74 65 33 53 74  yte += sqlite3St
24166 72 6c 65 6e 33 30 28 70 54 6f 43 6f 6c 2d 3e 61  rlen30(pToCol->a
24167 5b 69 5d 2e 7a 4e 61 6d 65 29 20 2b 20 31 3b 0a  [i].zName) + 1;.
24168 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 46 4b 65      }.  }.  pFKe
24169 79 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  y = sqlite3DbMal
2416a 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42 79 74  locZero(db, nByt
2416b 65 20 29 3b 0a 20 20 69 66 28 20 70 46 4b 65 79  e );.  if( pFKey
2416c 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ==0 ){.    goto 
2416d 66 6b 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 70 46  fk_end;.  }.  pF
2416e 4b 65 79 2d 3e 70 46 72 6f 6d 20 3d 20 70 3b 0a  Key->pFrom = p;.
2416f 20 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72    pFKey->pNextFr
24170 6f 6d 20 3d 20 70 2d 3e 70 46 4b 65 79 3b 0a 20  om = p->pFKey;. 
24171 20 7a 20 3d 20 28 63 68 61 72 2a 29 26 70 46 4b   z = (char*)&pFK
24172 65 79 2d 3e 61 43 6f 6c 5b 6e 43 6f 6c 5d 3b 0a  ey->aCol[nCol];.
24173 20 20 70 46 4b 65 79 2d 3e 7a 54 6f 20 3d 20 7a    pFKey->zTo = z
24174 3b 0a 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70 54  ;.  memcpy(z, pT
24175 6f 2d 3e 7a 2c 20 70 54 6f 2d 3e 6e 29 3b 0a 20  o->z, pTo->n);. 
24176 20 7a 5b 70 54 6f 2d 3e 6e 5d 20 3d 20 30 3b 0a   z[pTo->n] = 0;.
24177 20 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65    sqlite3Dequote
24178 28 7a 29 3b 0a 20 20 7a 20 2b 3d 20 70 54 6f 2d  (z);.  z += pTo-
24179 3e 6e 2b 31 3b 0a 20 20 70 46 4b 65 79 2d 3e 6e  >n+1;.  pFKey->n
2417a 43 6f 6c 20 3d 20 6e 43 6f 6c 3b 0a 20 20 69 66  Col = nCol;.  if
2417b 28 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 29 7b  ( pFromCol==0 ){
2417c 0a 20 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c  .    pFKey->aCol
2417d 5b 30 5d 2e 69 46 72 6f 6d 20 3d 20 70 2d 3e 6e  [0].iFrom = p->n
2417e 43 6f 6c 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Col-1;.  }else{.
2417f 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
24180 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
24181 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f   int j;.      fo
24182 72 28 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e 43 6f 6c  r(j=0; j<p->nCol
24183 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
24184 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
24185 6d 70 28 70 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e  mp(p->aCol[j].zN
24186 61 6d 65 2c 20 70 46 72 6f 6d 43 6f 6c 2d 3e 61  ame, pFromCol->a
24187 5b 69 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b  [i].zName)==0 ){
24188 0a 20 20 20 20 20 20 20 20 20 20 70 46 4b 65 79  .          pFKey
24189 2d 3e 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 20  ->aCol[i].iFrom 
2418a 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 62  = j;.          b
2418b 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
2418c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
2418d 28 20 6a 3e 3d 70 2d 3e 6e 43 6f 6c 20 29 7b 0a  ( j>=p->nCol ){.
2418e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2418f 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
24190 0a 20 20 20 20 20 20 20 20 20 20 22 75 6e 6b 6e  .          "unkn
24191 6f 77 6e 20 63 6f 6c 75 6d 6e 20 5c 22 25 73 5c  own column \"%s\
24192 22 20 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79  " in foreign key
24193 20 64 65 66 69 6e 69 74 69 6f 6e 22 2c 20 0a 20   definition", . 
24194 20 20 20 20 20 20 20 20 20 70 46 72 6f 6d 43 6f           pFromCo
24195 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a  l->a[i].zName);.
24196 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f          goto fk_
24197 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  end;.      }.   
24198 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 54 6f   }.  }.  if( pTo
24199 43 6f 6c 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  Col ){.    for(i
2419a 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29  =0; i<nCol; i++)
2419b 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20  {.      int n = 
2419c 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
2419d 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61  pToCol->a[i].zNa
2419e 6d 65 29 3b 0a 20 20 20 20 20 20 70 46 4b 65 79  me);.      pFKey
2419f 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 20 3d  ->aCol[i].zCol =
241a0 20 7a 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   z;.      memcpy
241a1 28 7a 2c 20 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d  (z, pToCol->a[i]
241a2 2e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20 20 20  .zName, n);.    
241a3 20 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 20 20 20    z[n] = 0;.    
241a4 20 20 7a 20 2b 3d 20 6e 2b 31 3b 0a 20 20 20 20    z += n+1;.    
241a5 7d 0a 20 20 7d 0a 20 20 70 46 4b 65 79 2d 3e 69  }.  }.  pFKey->i
241a6 73 44 65 66 65 72 72 65 64 20 3d 20 30 3b 0a 20  sDeferred = 0;. 
241a7 20 70 46 4b 65 79 2d 3e 64 65 6c 65 74 65 43 6f   pFKey->deleteCo
241a8 6e 66 20 3d 20 28 75 38 29 28 66 6c 61 67 73 20  nf = (u8)(flags 
241a9 26 20 30 78 66 66 29 3b 0a 20 20 70 46 4b 65 79  & 0xff);.  pFKey
241aa 2d 3e 75 70 64 61 74 65 43 6f 6e 66 20 3d 20 28  ->updateConf = (
241ab 75 38 29 28 28 66 6c 61 67 73 20 3e 3e 20 38 20  u8)((flags >> 8 
241ac 29 20 26 20 30 78 66 66 29 3b 0a 20 20 70 46 4b  ) & 0xff);.  pFK
241ad 65 79 2d 3e 69 6e 73 65 72 74 43 6f 6e 66 20 3d  ey->insertConf =
241ae 20 28 75 38 29 28 28 66 6c 61 67 73 20 3e 3e 20   (u8)((flags >> 
241af 31 36 20 29 20 26 20 30 78 66 66 29 3b 0a 0a 20  16 ) & 0xff);.. 
241b0 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 66 6f 72   /* Link the for
241b1 65 69 67 6e 20 6b 65 79 20 74 6f 20 74 68 65 20  eign key to the 
241b2 74 61 62 6c 65 20 61 73 20 74 68 65 20 6c 61 73  table as the las
241b3 74 20 73 74 65 70 2e 0a 20 20 2a 2f 0a 20 20 70  t step..  */.  p
241b4 2d 3e 70 46 4b 65 79 20 3d 20 70 46 4b 65 79 3b  ->pFKey = pFKey;
241b5 0a 20 20 70 46 4b 65 79 20 3d 20 30 3b 0a 0a 66  .  pFKey = 0;..f
241b6 6b 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33  k_end:.  sqlite3
241b7 44 62 46 72 65 65 28 64 62 2c 20 70 46 4b 65 79  DbFree(db, pFKey
241b8 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  );.#endif /* !de
241b9 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
241ba 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 29 20 2a  T_FOREIGN_KEY) *
241bb 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  /.  sqlite3ExprL
241bc 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 46  istDelete(db, pF
241bd 72 6f 6d 43 6f 6c 29 3b 0a 20 20 73 71 6c 69 74  romCol);.  sqlit
241be 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
241bf 28 64 62 2c 20 70 54 6f 43 6f 6c 29 3b 0a 7d 0a  (db, pToCol);.}.
241c0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
241c1 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68  ine is called wh
241c2 65 6e 20 61 6e 20 49 4e 49 54 49 41 4c 4c 59 20  en an INITIALLY 
241c3 49 4d 4d 45 44 49 41 54 45 20 6f 72 20 49 4e 49  IMMEDIATE or INI
241c4 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44 0a  TIALLY DEFERRED.
241c5 2a 2a 20 63 6c 61 75 73 65 20 69 73 20 73 65 65  ** clause is see
241c6 6e 20 61 73 20 70 61 72 74 20 6f 66 20 61 20 66  n as part of a f
241c7 6f 72 65 69 67 6e 20 6b 65 79 20 64 65 66 69 6e  oreign key defin
241c8 69 74 69 6f 6e 2e 20 20 54 68 65 20 69 73 44 65  ition.  The isDe
241c9 66 65 72 72 65 64 0a 2a 2a 20 70 61 72 61 6d 65  ferred.** parame
241ca 74 65 72 20 69 73 20 31 20 66 6f 72 20 49 4e 49  ter is 1 for INI
241cb 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44 20  TIALLY DEFERRED 
241cc 61 6e 64 20 30 20 66 6f 72 20 49 4e 49 54 49 41  and 0 for INITIA
241cd 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45 2e 0a 2a  LLY IMMEDIATE..*
241ce 2a 20 54 68 65 20 62 65 68 61 76 69 6f 72 20 6f  * The behavior o
241cf 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  f the most recen
241d0 74 6c 79 20 63 72 65 61 74 65 64 20 66 6f 72 65  tly created fore
241d1 69 67 6e 20 6b 65 79 20 69 73 20 61 64 6a 75 73  ign key is adjus
241d2 74 65 64 0a 2a 2a 20 61 63 63 6f 72 64 69 6e 67  ted.** according
241d3 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ly..*/.SQLITE_PR
241d4 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
241d5 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e 4b 65  e3DeferForeignKe
241d6 79 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  y(Parse *pParse,
241d7 20 69 6e 74 20 69 73 44 65 66 65 72 72 65 64 29   int isDeferred)
241d8 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
241d9 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45  _OMIT_FOREIGN_KE
241da 59 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  Y.  Table *pTab;
241db 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 3b 0a  .  FKey *pFKey;.
241dc 20 20 69 66 28 20 28 70 54 61 62 20 3d 20 70 50    if( (pTab = pP
241dd 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29  arse->pNewTable)
241de 3d 3d 30 20 7c 7c 20 28 70 46 4b 65 79 20 3d 20  ==0 || (pFKey = 
241df 70 54 61 62 2d 3e 70 46 4b 65 79 29 3d 3d 30 20  pTab->pFKey)==0 
241e0 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65  ) return;.  asse
241e1 72 74 28 20 69 73 44 65 66 65 72 72 65 64 3d 3d  rt( isDeferred==
241e2 30 20 7c 7c 20 69 73 44 65 66 65 72 72 65 64 3d  0 || isDeferred=
241e3 3d 31 20 29 3b 0a 20 20 70 46 4b 65 79 2d 3e 69  =1 );.  pFKey->i
241e4 73 44 65 66 65 72 72 65 64 20 3d 20 28 75 38 29  sDeferred = (u8)
241e5 69 73 44 65 66 65 72 72 65 64 3b 0a 23 65 6e 64  isDeferred;.#end
241e6 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  if.}../*.** Gene
241e7 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77  rate code that w
241e8 69 6c 6c 20 65 72 61 73 65 20 61 6e 64 20 72 65  ill erase and re
241e9 66 69 6c 6c 20 69 6e 64 65 78 20 2a 70 49 64 78  fill index *pIdx
241ea 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20 75 73  .  This is.** us
241eb 65 64 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65  ed to initialize
241ec 20 61 20 6e 65 77 6c 79 20 63 72 65 61 74 65 64   a newly created
241ed 20 69 6e 64 65 78 20 6f 72 20 74 6f 20 72 65 63   index or to rec
241ee 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a 20 63 6f  ompute the.** co
241ef 6e 74 65 6e 74 20 6f 66 20 61 6e 20 69 6e 64 65  ntent of an inde
241f0 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f  x in response to
241f1 20 61 20 52 45 49 4e 44 45 58 20 63 6f 6d 6d 61   a REINDEX comma
241f2 6e 64 2e 0a 2a 2a 0a 2a 2a 20 69 66 20 6d 65 6d  nd..**.** if mem
241f3 52 6f 6f 74 50 61 67 65 20 69 73 20 6e 6f 74 20  RootPage is not 
241f4 6e 65 67 61 74 69 76 65 2c 20 69 74 20 6d 65 61  negative, it mea
241f5 6e 73 20 74 68 61 74 20 74 68 65 20 69 6e 64 65  ns that the inde
241f6 78 20 69 73 20 6e 65 77 6c 79 0a 2a 2a 20 63 72  x is newly.** cr
241f7 65 61 74 65 64 2e 20 20 54 68 65 20 72 65 67 69  eated.  The regi
241f8 73 74 65 72 20 73 70 65 63 69 66 69 65 64 20 62  ster specified b
241f9 79 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20 63 6f  y memRootPage co
241fa 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 72 6f  ntains the.** ro
241fb 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  ot page number o
241fc 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20 49 66  f the index.  If
241fd 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20 69 73 20   memRootPage is 
241fe 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 0a 2a  negative, then.*
241ff 2a 20 74 68 65 20 69 6e 64 65 78 20 61 6c 72 65  * the index alre
24200 61 64 79 20 65 78 69 73 74 73 20 61 6e 64 20 6d  ady exists and m
24201 75 73 74 20 62 65 20 63 6c 65 61 72 65 64 20 62  ust be cleared b
24202 65 66 6f 72 65 20 62 65 69 6e 67 20 72 65 66 69  efore being refi
24203 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 20  lled and.** the 
24204 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72  root page number
24205 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 69 73   of the index is
24206 20 74 61 6b 65 6e 20 66 72 6f 6d 20 70 49 6e 64   taken from pInd
24207 65 78 2d 3e 74 6e 75 6d 2e 0a 2a 2f 0a 73 74 61  ex->tnum..*/.sta
24208 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
24209 52 65 66 69 6c 6c 49 6e 64 65 78 28 50 61 72 73  RefillIndex(Pars
2420a 65 20 2a 70 50 61 72 73 65 2c 20 49 6e 64 65 78  e *pParse, Index
2420b 20 2a 70 49 6e 64 65 78 2c 20 69 6e 74 20 6d 65   *pIndex, int me
2420c 6d 52 6f 6f 74 50 61 67 65 29 7b 0a 20 20 54 61  mRootPage){.  Ta
2420d 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 49 6e 64  ble *pTab = pInd
2420e 65 78 2d 3e 70 54 61 62 6c 65 3b 20 20 2f 2a 20  ex->pTable;  /* 
2420f 54 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 69  The table that i
24210 73 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69  s indexed */.  i
24211 6e 74 20 69 54 61 62 20 3d 20 70 50 61 72 73 65  nt iTab = pParse
24212 2d 3e 6e 54 61 62 2b 2b 3b 20 20 20 20 20 2f 2a  ->nTab++;     /*
24213 20 42 74 72 65 65 20 63 75 72 73 6f 72 20 75 73   Btree cursor us
24214 65 64 20 66 6f 72 20 70 54 61 62 20 2a 2f 0a 20  ed for pTab */. 
24215 20 69 6e 74 20 69 49 64 78 20 3d 20 70 50 61 72   int iIdx = pPar
24216 73 65 2d 3e 6e 54 61 62 2b 2b 3b 20 20 20 20 20  se->nTab++;     
24217 2f 2a 20 42 74 72 65 65 20 63 75 72 73 6f 72 20  /* Btree cursor 
24218 75 73 65 64 20 66 6f 72 20 70 49 6e 64 65 78 20  used for pIndex 
24219 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 31 3b 20  */.  int addr1; 
2421a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2421b 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
2421c 66 20 74 6f 70 20 6f 66 20 6c 6f 6f 70 20 2a 2f  f top of loop */
2421d 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20 20 20  .  int tnum;    
2421e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2421f 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f    /* Root page o
24220 66 20 69 6e 64 65 78 20 2a 2f 0a 20 20 56 64 62  f index */.  Vdb
24221 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20  e *v;           
24222 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47              /* G
24223 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e 74  enerate code int
24224 6f 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 6d  o this virtual m
24225 61 63 68 69 6e 65 20 2a 2f 0a 20 20 4b 65 79 49  achine */.  KeyI
24226 6e 66 6f 20 2a 70 4b 65 79 3b 20 20 20 20 20 20  nfo *pKey;      
24227 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65             /* Ke
24228 79 49 6e 66 6f 20 66 6f 72 20 69 6e 64 65 78 20  yInfo for index 
24229 2a 2f 0a 20 20 69 6e 74 20 72 65 67 49 64 78 4b  */.  int regIdxK
2422a 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ey;             
2422b 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 73      /* Registers
2422c 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
2422d 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 69  index key */.  i
2422e 6e 74 20 72 65 67 52 65 63 6f 72 64 3b 20 20 20  nt regRecord;   
2422f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
24230 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   Register holdin
24231 67 20 61 73 73 65 6d 62 6c 69 65 64 20 69 6e 64  g assemblied ind
24232 65 78 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 73  ex record */.  s
24233 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
24234 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 2f 2a  rse->db;      /*
24235 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
24236 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  nnection */.  in
24237 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53  t iDb = sqlite3S
24238 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
24239 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61   pIndex->pSchema
2423a 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
2423b 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
2423c 41 54 49 4f 4e 0a 20 20 69 66 28 20 73 71 6c 69  ATION.  if( sqli
2423d 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
2423e 72 73 65 2c 20 53 51 4c 49 54 45 5f 52 45 49 4e  rse, SQLITE_REIN
2423f 44 45 58 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61  DEX, pIndex->zNa
24240 6d 65 2c 20 30 2c 0a 20 20 20 20 20 20 64 62 2d  me, 0,.      db-
24241 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 20  >aDb[iDb].zName 
24242 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ) ){.    return;
24243 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
24244 2a 20 52 65 71 75 69 72 65 20 61 20 77 72 69 74  * Require a writ
24245 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61  e-lock on the ta
24246 62 6c 65 20 74 6f 20 70 65 72 66 6f 72 6d 20 74  ble to perform t
24247 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f  his operation */
24248 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c  .  sqlite3TableL
24249 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c  ock(pParse, iDb,
2424a 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 31 2c 20   pTab->tnum, 1, 
2424b 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20  pTab->zName);.. 
2424c 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
2424d 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
2424e 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( v==0 ) return
2424f 3b 0a 20 20 69 66 28 20 6d 65 6d 52 6f 6f 74 50  ;.  if( memRootP
24250 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20 20 74 6e  age>=0 ){.    tn
24251 75 6d 20 3d 20 6d 65 6d 52 6f 6f 74 50 61 67 65  um = memRootPage
24252 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 74  ;.  }else{.    t
24253 6e 75 6d 20 3d 20 70 49 6e 64 65 78 2d 3e 74 6e  num = pIndex->tn
24254 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  um;.    sqlite3V
24255 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
24256 43 6c 65 61 72 2c 20 74 6e 75 6d 2c 20 69 44 62  Clear, tnum, iDb
24257 29 3b 0a 20 20 7d 0a 20 20 70 4b 65 79 20 3d 20  );.  }.  pKey = 
24258 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69  sqlite3IndexKeyi
24259 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 6e 64  nfo(pParse, pInd
2425a 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ex);.  sqlite3Vd
2425b 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f  beAddOp4(v, OP_O
2425c 70 65 6e 57 72 69 74 65 2c 20 69 49 64 78 2c 20  penWrite, iIdx, 
2425d 74 6e 75 6d 2c 20 69 44 62 2c 20 0a 20 20 20 20  tnum, iDb, .    
2425e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2425f 28 63 68 61 72 20 2a 29 70 4b 65 79 2c 20 50 34  (char *)pKey, P4
24260 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
24261 29 3b 0a 20 20 69 66 28 20 6d 65 6d 52 6f 6f 74  );.  if( memRoot
24262 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20 20 73  Page>=0 ){.    s
24263 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
24264 50 35 28 76 2c 20 31 29 3b 0a 20 20 7d 0a 20 20  P5(v, 1);.  }.  
24265 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65  sqlite3OpenTable
24266 28 70 50 61 72 73 65 2c 20 69 54 61 62 2c 20 69  (pParse, iTab, i
24267 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65  Db, pTab, OP_Ope
24268 6e 52 65 61 64 29 3b 0a 20 20 61 64 64 72 31 20  nRead);.  addr1 
24269 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2426a 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64  Op2(v, OP_Rewind
2426b 2c 20 69 54 61 62 2c 20 30 29 3b 0a 20 20 72 65  , iTab, 0);.  re
2426c 67 52 65 63 6f 72 64 20 3d 20 73 71 6c 69 74 65  gRecord = sqlite
2426d 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
2426e 73 65 29 3b 0a 20 20 72 65 67 49 64 78 4b 65 79  se);.  regIdxKey
2426f 20 3d 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61   = sqlite3Genera
24270 74 65 49 6e 64 65 78 4b 65 79 28 70 50 61 72 73  teIndexKey(pPars
24271 65 2c 20 70 49 6e 64 65 78 2c 20 69 54 61 62 2c  e, pIndex, iTab,
24272 20 72 65 67 52 65 63 6f 72 64 2c 20 31 29 3b 0a   regRecord, 1);.
24273 20 20 69 66 28 20 70 49 6e 64 65 78 2d 3e 6f 6e    if( pIndex->on
24274 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29  Error!=OE_None )
24275 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20  {.    const int 
24276 72 65 67 52 6f 77 69 64 20 3d 20 72 65 67 49 64  regRowid = regId
24277 78 4b 65 79 20 2b 20 70 49 6e 64 65 78 2d 3e 6e  xKey + pIndex->n
24278 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 63 6f 6e 73  Column;.    cons
24279 74 20 69 6e 74 20 6a 32 20 3d 20 73 71 6c 69 74  t int j2 = sqlit
2427a 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
2427b 72 28 76 29 20 2b 20 32 3b 0a 20 20 20 20 76 6f  r(v) + 2;.    vo
2427c 69 64 20 2a 20 63 6f 6e 73 74 20 70 52 65 67 4b  id * const pRegK
2427d 65 79 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 5f  ey = SQLITE_INT_
2427e 54 4f 5f 50 54 52 28 72 65 67 49 64 78 4b 65 79  TO_PTR(regIdxKey
2427f 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 72  );..    /* The r
24280 65 67 69 73 74 65 72 73 20 61 63 63 65 73 73 65  egisters accesse
24281 64 20 62 79 20 74 68 65 20 4f 50 5f 49 73 55 6e  d by the OP_IsUn
24282 69 71 75 65 20 6f 70 63 6f 64 65 20 77 65 72 65  ique opcode were
24283 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 20 20 2a   allocated.    *
24284 2a 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 47  * using sqlite3G
24285 65 74 54 65 6d 70 52 61 6e 67 65 28 29 20 69 6e  etTempRange() in
24286 73 69 64 65 20 6f 66 20 74 68 65 20 73 71 6c 69  side of the sqli
24287 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78  te3GenerateIndex
24288 4b 65 79 28 29 0a 20 20 20 20 2a 2a 20 63 61 6c  Key().    ** cal
24289 6c 20 61 62 6f 76 65 2e 20 4a 75 73 74 20 62 65  l above. Just be
2428a 66 6f 72 65 20 74 68 61 74 20 66 75 6e 63 74 69  fore that functi
2428b 6f 6e 20 77 61 73 20 66 72 65 65 64 20 74 68 65  on was freed the
2428c 79 20 77 65 72 65 20 72 65 6c 65 61 73 65 64 0a  y were released.
2428d 20 20 20 20 2a 2a 20 28 6d 61 64 65 20 61 76 61      ** (made ava
2428e 69 6c 61 62 6c 65 20 74 6f 20 74 68 65 20 63 6f  ilable to the co
2428f 6d 70 69 6c 65 72 20 66 6f 72 20 72 65 75 73 65  mpiler for reuse
24290 29 20 75 73 69 6e 67 20 0a 20 20 20 20 2a 2a 20  ) using .    ** 
24291 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
24292 6d 70 52 61 6e 67 65 28 29 2e 20 53 6f 20 69 6e  mpRange(). So in
24293 20 73 6f 6d 65 20 77 61 79 73 20 68 61 76 69 6e   some ways havin
24294 67 20 74 68 65 20 4f 50 5f 49 73 55 6e 69 71 75  g the OP_IsUniqu
24295 65 0a 20 20 20 20 2a 2a 20 6f 70 63 6f 64 65 20  e.    ** opcode 
24296 75 73 65 20 74 68 65 20 76 61 6c 75 65 73 20 73  use the values s
24297 74 6f 72 65 64 20 77 69 74 68 69 6e 20 73 65 65  tored within see
24298 6d 73 20 64 61 6e 67 65 72 6f 75 73 2e 20 48 6f  ms dangerous. Ho
24299 77 65 76 65 72 2c 20 73 69 6e 63 65 0a 20 20 20  wever, since.   
2429a 20 2a 2a 20 77 65 20 63 61 6e 20 62 65 20 73 75   ** we can be su
2429b 72 65 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72  re that no other
2429c 20 74 65 6d 70 20 72 65 67 69 73 74 65 72 73 20   temp registers 
2429d 68 61 76 65 20 62 65 65 6e 20 61 6c 6c 6f 63 61  have been alloca
2429e 74 65 64 0a 20 20 20 20 2a 2a 20 73 69 6e 63 65  ted.    ** since
2429f 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
242a0 65 6d 70 52 61 6e 67 65 28 29 20 77 61 73 20 63  empRange() was c
242a1 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 73 61 66  alled, it is saf
242a2 65 20 74 6f 20 64 6f 20 73 6f 2e 0a 20 20 20 20  e to do so..    
242a3 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  */.    sqlite3Vd
242a4 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49  beAddOp4(v, OP_I
242a5 73 55 6e 69 71 75 65 2c 20 69 49 64 78 2c 20 6a  sUnique, iIdx, j
242a6 32 2c 20 72 65 67 52 6f 77 69 64 2c 20 70 52 65  2, regRowid, pRe
242a7 67 4b 65 79 2c 20 50 34 5f 49 4e 54 33 32 29 3b  gKey, P4_INT32);
242a8 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61 6c 74  .    sqlite3Halt
242a9 43 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20 20 20  Constraint(.    
242aa 20 20 20 20 70 50 61 72 73 65 2c 20 4f 45 5f 41      pParse, OE_A
242ab 62 6f 72 74 2c 20 22 69 6e 64 65 78 65 64 20 63  bort, "indexed c
242ac 6f 6c 75 6d 6e 73 20 61 72 65 20 6e 6f 74 20 75  olumns are not u
242ad 6e 69 71 75 65 22 2c 20 50 34 5f 53 54 41 54 49  nique", P4_STATI
242ae 43 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  C);.  }.  sqlite
242af 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
242b0 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 49 64  P_IdxInsert, iId
242b1 78 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20  x, regRecord);. 
242b2 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
242b3 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 55  geP5(v, OPFLAG_U
242b4 53 45 53 45 45 4b 52 45 53 55 4c 54 29 3b 0a 20  SESEEKRESULT);. 
242b5 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
242b6 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
242b7 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c  egRecord);.  sql
242b8 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
242b9 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 54 61 62 2c  , OP_Next, iTab,
242ba 20 61 64 64 72 31 2b 31 29 3b 0a 20 20 73 71 6c   addr1+1);.  sql
242bb 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
242bc 28 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 73 71  (v, addr1);.  sq
242bd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
242be 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 54 61  v, OP_Close, iTa
242bf 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  b);.  sqlite3Vdb
242c0 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c  eAddOp1(v, OP_Cl
242c1 6f 73 65 2c 20 69 49 64 78 29 3b 0a 7d 0a 0a 2f  ose, iIdx);.}../
242c2 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  *.** Create a ne
242c3 77 20 69 6e 64 65 78 20 66 6f 72 20 61 6e 20 53  w index for an S
242c4 51 4c 20 74 61 62 6c 65 2e 20 20 70 4e 61 6d 65  QL table.  pName
242c5 31 2e 70 4e 61 6d 65 32 20 69 73 20 74 68 65 20  1.pName2 is the 
242c6 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65  name of the inde
242c7 78 20 0a 2a 2a 20 61 6e 64 20 70 54 62 6c 4c 69  x .** and pTblLi
242c8 73 74 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  st is the name o
242c9 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74  f the table that
242ca 20 69 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65   is to be indexe
242cb 64 2e 20 20 42 6f 74 68 20 77 69 6c 6c 20 0a 2a  d.  Both will .*
242cc 2a 20 62 65 20 4e 55 4c 4c 20 66 6f 72 20 61 20  * be NULL for a 
242cd 70 72 69 6d 61 72 79 20 6b 65 79 20 6f 72 20 61  primary key or a
242ce 6e 20 69 6e 64 65 78 20 74 68 61 74 20 69 73 20  n index that is 
242cf 63 72 65 61 74 65 64 20 74 6f 20 73 61 74 69 73  created to satis
242d0 66 79 20 61 0a 2a 2a 20 55 4e 49 51 55 45 20 63  fy a.** UNIQUE c
242d1 6f 6e 73 74 72 61 69 6e 74 2e 20 20 49 66 20 70  onstraint.  If p
242d2 54 61 62 6c 65 20 61 6e 64 20 70 49 6e 64 65 78  Table and pIndex
242d3 20 61 72 65 20 4e 55 4c 4c 2c 20 75 73 65 20 70   are NULL, use p
242d4 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
242d5 0a 2a 2a 20 61 73 20 74 68 65 20 74 61 62 6c 65  .** as the table
242d6 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e 20   to be indexed. 
242d7 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
242d8 6c 65 20 69 73 20 61 20 74 61 62 6c 65 20 74 68  le is a table th
242d9 61 74 20 69 73 0a 2a 2a 20 63 75 72 72 65 6e 74  at is.** current
242da 6c 79 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75  ly being constru
242db 63 74 65 64 20 62 79 20 61 20 43 52 45 41 54 45  cted by a CREATE
242dc 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
242dd 2e 0a 2a 2a 0a 2a 2a 20 70 4c 69 73 74 20 69 73  ..**.** pList is
242de 20 61 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d   a list of colum
242df 6e 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64  ns to be indexed
242e0 2e 20 20 70 4c 69 73 74 20 77 69 6c 6c 20 62 65  .  pList will be
242e1 20 4e 55 4c 4c 20 69 66 20 74 68 69 73 0a 2a 2a   NULL if this.**
242e2 20 69 73 20 61 20 70 72 69 6d 61 72 79 20 6b 65   is a primary ke
242e3 79 20 6f 72 20 75 6e 69 71 75 65 2d 63 6f 6e 73  y or unique-cons
242e4 74 72 61 69 6e 74 20 6f 6e 20 74 68 65 20 6d 6f  traint on the mo
242e5 73 74 20 72 65 63 65 6e 74 20 63 6f 6c 75 6d 6e  st recent column
242e6 20 61 64 64 65 64 0a 2a 2a 20 74 6f 20 74 68 65   added.** to the
242e7 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79   table currently
242e8 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74   under construct
242e9 69 6f 6e 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45  ion.  .*/.SQLITE
242ea 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
242eb 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78  lite3CreateIndex
242ec 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
242ed 65 2c 20 20 20 20 20 2f 2a 20 41 6c 6c 20 69 6e  e,     /* All in
242ee 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
242ef 74 68 69 73 20 70 61 72 73 65 20 2a 2f 0a 20 20  this parse */.  
242f0 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20  Token *pName1,  
242f1 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 72 74     /* First part
242f2 20 6f 66 20 69 6e 64 65 78 20 6e 61 6d 65 2e 20   of index name. 
242f3 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20  May be NULL */. 
242f4 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20   Token *pName2, 
242f5 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61      /* Second pa
242f6 72 74 20 6f 66 20 69 6e 64 65 78 20 6e 61 6d 65  rt of index name
242f7 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f  . May be NULL */
242f8 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 62 6c  .  SrcList *pTbl
242f9 4e 61 6d 65 2c 20 2f 2a 20 54 61 62 6c 65 20 74  Name, /* Table t
242fa 6f 20 69 6e 64 65 78 2e 20 55 73 65 20 70 50 61  o index. Use pPa
242fb 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 69  rse->pNewTable i
242fc 66 20 30 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  f 0 */.  ExprLis
242fd 74 20 2a 70 4c 69 73 74 2c 20 20 20 2f 2a 20 41  t *pList,   /* A
242fe 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73   list of columns
242ff 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a   to be indexed *
24300 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c  /.  int onError,
24301 20 20 20 20 20 20 20 2f 2a 20 4f 45 5f 41 62 6f         /* OE_Abo
24302 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f  rt, OE_Ignore, O
24303 45 5f 52 65 70 6c 61 63 65 2c 20 6f 72 20 4f 45  E_Replace, or OE
24304 5f 4e 6f 6e 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e  _None */.  Token
24305 20 2a 70 53 74 61 72 74 2c 20 20 20 20 20 2f 2a   *pStart,     /*
24306 20 54 68 65 20 43 52 45 41 54 45 20 74 6f 6b 65   The CREATE toke
24307 6e 20 74 68 61 74 20 62 65 67 69 6e 73 20 74 68  n that begins th
24308 69 73 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  is statement */.
24309 20 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20    Token *pEnd,  
2430a 20 20 20 20 20 2f 2a 20 54 68 65 20 22 29 22 20       /* The ")" 
2430b 74 68 61 74 20 63 6c 6f 73 65 73 20 74 68 65 20  that closes the 
2430c 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 74 61  CREATE INDEX sta
2430d 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  tement */.  int 
2430e 73 6f 72 74 4f 72 64 65 72 2c 20 20 20 20 20 2f  sortOrder,     /
2430f 2a 20 53 6f 72 74 20 6f 72 64 65 72 20 6f 66 20  * Sort order of 
24310 70 72 69 6d 61 72 79 20 6b 65 79 20 77 68 65 6e  primary key when
24311 20 70 4c 69 73 74 3d 3d 4e 55 4c 4c 20 2a 2f 0a   pList==NULL */.
24312 20 20 69 6e 74 20 69 66 4e 6f 74 45 78 69 73 74    int ifNotExist
24313 20 20 20 20 20 2f 2a 20 4f 6d 69 74 20 65 72 72       /* Omit err
24314 6f 72 20 69 66 20 69 6e 64 65 78 20 61 6c 72 65  or if index alre
24315 61 64 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b  ady exists */.){
24316 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d  .  Table *pTab =
24317 20 30 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65   0;     /* Table
24318 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a   to be indexed *
24319 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65  /.  Index *pInde
2431a 78 20 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20  x = 0;   /* The 
2431b 69 6e 64 65 78 20 74 6f 20 62 65 20 63 72 65 61  index to be crea
2431c 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ted */.  char *z
2431d 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a  Name = 0;     /*
2431e 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64   Name of the ind
2431f 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d  ex */.  int nNam
24320 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e;           /* 
24321 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63  Number of charac
24322 74 65 72 73 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f  ters in zName */
24323 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 54  .  int i, j;.  T
24324 6f 6b 65 6e 20 6e 75 6c 6c 49 64 3b 20 20 20 20  oken nullId;    
24325 20 20 20 20 2f 2a 20 46 61 6b 65 20 74 6f 6b 65      /* Fake toke
24326 6e 20 66 6f 72 20 61 6e 20 65 6d 70 74 79 20 49  n for an empty I
24327 44 20 6c 69 73 74 20 2a 2f 0a 20 20 44 62 46 69  D list */.  DbFi
24328 78 65 72 20 73 46 69 78 3b 20 20 20 20 20 20 20  xer sFix;       
24329 20 2f 2a 20 46 6f 72 20 61 73 73 69 67 6e 69 6e   /* For assignin
2432a 67 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 73  g database names
2432b 20 74 6f 20 70 54 61 62 6c 65 20 2a 2f 0a 20 20   to pTable */.  
2432c 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 4d 61 73  int sortOrderMas
2432d 6b 3b 20 20 20 2f 2a 20 31 20 74 6f 20 68 6f 6e  k;   /* 1 to hon
2432e 6f 72 20 44 45 53 43 20 69 6e 20 69 6e 64 65 78  or DESC in index
2432f 2e 20 20 30 20 74 6f 20 69 67 6e 6f 72 65 2e 20  .  0 to ignore. 
24330 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
24331 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
24332 20 44 62 20 2a 70 44 62 3b 20 20 20 20 20 20 20   Db *pDb;       
24333 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65        /* The spe
24334 63 69 66 69 63 20 74 61 62 6c 65 20 63 6f 6e 74  cific table cont
24335 61 69 6e 69 6e 67 20 74 68 65 20 69 6e 64 65 78  aining the index
24336 65 64 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  ed database */. 
24337 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
24338 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
24339 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  f the database t
2433a 68 61 74 20 69 73 20 62 65 69 6e 67 20 77 72 69  hat is being wri
2433b 74 74 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  tten */.  Token 
2433c 2a 70 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 2f  *pName = 0;    /
2433d 2a 20 55 6e 71 75 61 6c 69 66 69 65 64 20 6e 61  * Unqualified na
2433e 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  me of the index 
2433f 74 6f 20 63 72 65 61 74 65 20 2a 2f 0a 20 20 73  to create */.  s
24340 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
24341 74 65 6d 20 2a 70 4c 69 73 74 49 74 65 6d 3b 20  tem *pListItem; 
24342 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f  /* For looping o
24343 76 65 72 20 70 4c 69 73 74 20 2a 2f 0a 20 20 69  ver pList */.  i
24344 6e 74 20 6e 43 6f 6c 3b 0a 20 20 69 6e 74 20 6e  nt nCol;.  int n
24345 45 78 74 72 61 20 3d 20 30 3b 0a 20 20 63 68 61  Extra = 0;.  cha
24346 72 20 2a 7a 45 78 74 72 61 3b 0a 0a 20 20 61 73  r *zExtra;..  as
24347 73 65 72 74 28 20 70 53 74 61 72 74 3d 3d 30 20  sert( pStart==0 
24348 7c 7c 20 70 45 6e 64 21 3d 30 20 29 3b 20 2f 2a  || pEnd!=0 ); /*
24349 20 70 45 6e 64 20 6d 75 73 74 20 62 65 20 6e 6f   pEnd must be no
2434a 6e 2d 4e 55 4c 4c 20 69 66 20 70 53 74 61 72 74  n-NULL if pStart
2434b 20 69 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28   is */.  assert(
2434c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30   pParse->nErr==0
2434d 20 29 3b 20 20 20 20 20 20 2f 2a 20 4e 65 76 65   );      /* Neve
2434e 72 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70 72  r called with pr
2434f 69 6f 72 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20  ior errors */.  
24350 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
24351 69 6c 65 64 20 7c 7c 20 49 4e 5f 44 45 43 4c 41  iled || IN_DECLA
24352 52 45 5f 56 54 41 42 20 29 7b 0a 20 20 20 20 67  RE_VTAB ){.    g
24353 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f  oto exit_create_
24354 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 69 66 28  index;.  }.  if(
24355 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69   SQLITE_OK!=sqli
24356 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
24357 61 72 73 65 29 20 29 7b 0a 20 20 20 20 67 6f 74  arse) ){.    got
24358 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
24359 64 65 78 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20  dex;.  }..  /*. 
2435a 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 74 61 62   ** Find the tab
2435b 6c 65 20 74 68 61 74 20 69 73 20 74 6f 20 62 65  le that is to be
2435c 20 69 6e 64 65 78 65 64 2e 20 20 52 65 74 75 72   indexed.  Retur
2435d 6e 20 65 61 72 6c 79 20 69 66 20 6e 6f 74 20 66  n early if not f
2435e 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ound..  */.  if(
2435f 20 70 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b 0a   pTblName!=0 ){.
24360 0a 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20  .    /* Use the 
24361 74 77 6f 2d 70 61 72 74 20 69 6e 64 65 78 20 6e  two-part index n
24362 61 6d 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  ame to determine
24363 20 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 20   the database . 
24364 20 20 20 2a 2a 20 74 6f 20 73 65 61 72 63 68 20     ** to search 
24365 66 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 20 27  for the table. '
24366 46 69 78 27 20 74 68 65 20 74 61 62 6c 65 20 6e  Fix' the table n
24367 61 6d 65 20 74 6f 20 74 68 69 73 20 64 62 0a 20  ame to this db. 
24368 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 6c 6f 6f     ** before loo
24369 6b 69 6e 67 20 75 70 20 74 68 65 20 74 61 62 6c  king up the tabl
2436a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  e..    */.    as
2436b 73 65 72 74 28 20 70 4e 61 6d 65 31 20 26 26 20  sert( pName1 && 
2436c 70 4e 61 6d 65 32 20 29 3b 0a 20 20 20 20 69 44  pName2 );.    iD
2436d 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61  b = sqlite3TwoPa
2436e 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70  rtName(pParse, p
2436f 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26  Name1, pName2, &
24370 70 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  pName);.    if( 
24371 69 44 62 3c 30 20 29 20 67 6f 74 6f 20 65 78 69  iDb<0 ) goto exi
24372 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
24373 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
24374 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 20 20 20 20  OMIT_TEMPDB.    
24375 2f 2a 20 49 66 20 74 68 65 20 69 6e 64 65 78 20  /* If the index 
24376 6e 61 6d 65 20 77 61 73 20 75 6e 71 75 61 6c 69  name was unquali
24377 66 69 65 64 2c 20 63 68 65 63 6b 20 69 66 20 74  fied, check if t
24378 68 65 20 74 68 65 20 74 61 62 6c 65 0a 20 20 20  he the table.   
24379 20 2a 2a 20 69 73 20 61 20 74 65 6d 70 20 74 61   ** is a temp ta
2437a 62 6c 65 2e 20 49 66 20 73 6f 2c 20 73 65 74 20  ble. If so, set 
2437b 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  the database to 
2437c 31 2e 20 44 6f 20 6e 6f 74 20 64 6f 20 74 68 69  1. Do not do thi
2437d 73 0a 20 20 20 20 2a 2a 20 69 66 20 69 6e 69 74  s.    ** if init
2437e 69 61 6c 69 73 69 6e 67 20 61 20 64 61 74 61 62  ialising a datab
2437f 61 73 65 20 73 63 68 65 6d 61 2e 0a 20 20 20 20  ase schema..    
24380 2a 2f 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e  */.    if( !db->
24381 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20  init.busy ){.   
24382 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65     pTab = sqlite
24383 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70  3SrcListLookup(p
24384 50 61 72 73 65 2c 20 70 54 62 6c 4e 61 6d 65 29  Parse, pTblName)
24385 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 61 6d  ;.      if( pNam
24386 65 32 2d 3e 6e 3d 3d 30 20 26 26 20 70 54 61 62  e2->n==0 && pTab
24387 20 26 26 20 70 54 61 62 2d 3e 70 53 63 68 65 6d   && pTab->pSchem
24388 61 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53  a==db->aDb[1].pS
24389 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 20  chema ){.       
2438a 20 69 44 62 20 3d 20 31 3b 0a 20 20 20 20 20 20   iDb = 1;.      
2438b 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  }.    }.#endif..
2438c 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
2438d 69 78 49 6e 69 74 28 26 73 46 69 78 2c 20 70 50  ixInit(&sFix, pP
2438e 61 72 73 65 2c 20 69 44 62 2c 20 22 69 6e 64 65  arse, iDb, "inde
2438f 78 22 2c 20 70 4e 61 6d 65 29 20 26 26 0a 20 20  x", pName) &&.  
24390 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 69 78        sqlite3Fix
24391 53 72 63 4c 69 73 74 28 26 73 46 69 78 2c 20 70  SrcList(&sFix, p
24392 54 62 6c 4e 61 6d 65 29 0a 20 20 20 20 29 7b 0a  TblName).    ){.
24393 20 20 20 20 20 20 2f 2a 20 42 65 63 61 75 73 65        /* Because
24394 20 74 68 65 20 70 61 72 73 65 72 20 63 6f 6e 73   the parser cons
24395 74 72 75 63 74 73 20 70 54 62 6c 4e 61 6d 65 20  tructs pTblName 
24396 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 69 64  from a single id
24397 65 6e 74 69 66 69 65 72 2c 0a 20 20 20 20 20 20  entifier,.      
24398 2a 2a 20 73 71 6c 69 74 65 33 46 69 78 53 72 63  ** sqlite3FixSrc
24399 4c 69 73 74 20 63 61 6e 20 6e 65 76 65 72 20 66  List can never f
2439a 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73  ail. */.      as
2439b 73 65 72 74 28 30 29 3b 0a 20 20 20 20 7d 0a 20  sert(0);.    }. 
2439c 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65     pTab = sqlite
2439d 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61  3LocateTable(pPa
2439e 72 73 65 2c 20 30 2c 20 70 54 62 6c 4e 61 6d 65  rse, 0, pTblName
2439f 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 0a 20  ->a[0].zName, . 
243a0 20 20 20 20 20 20 20 70 54 62 6c 4e 61 6d 65 2d         pTblName-
243a1 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 29  >a[0].zDatabase)
243a2 3b 0a 20 20 20 20 69 66 28 20 21 70 54 61 62 20  ;.    if( !pTab 
243a3 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
243a4 6c 65 64 20 29 20 67 6f 74 6f 20 65 78 69 74 5f  led ) goto exit_
243a5 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
243a6 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44    assert( db->aD
243a7 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3d 3d  b[iDb].pSchema==
243a8 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b  pTab->pSchema );
243a9 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
243aa 73 65 72 74 28 20 70 4e 61 6d 65 3d 3d 30 20 29  sert( pName==0 )
243ab 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 70 50 61  ;.    pTab = pPa
243ac 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a  rse->pNewTable;.
243ad 20 20 20 20 69 66 28 20 21 70 54 61 62 20 29 20      if( !pTab ) 
243ae 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65  goto exit_create
243af 5f 69 6e 64 65 78 3b 0a 20 20 20 20 69 44 62 20  _index;.    iDb 
243b0 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
243b1 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d  oIndex(db, pTab-
243b2 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 7d 0a 20  >pSchema);.  }. 
243b3 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b   pDb = &db->aDb[
243b4 69 44 62 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28  iDb];..  assert(
243b5 20 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 61 73   pTab!=0 );.  as
243b6 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 45  sert( pParse->nE
243b7 72 72 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 73  rr==0 );.  if( s
243b8 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70  qlite3StrNICmp(p
243b9 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c  Tab->zName, "sql
243ba 69 74 65 5f 22 2c 20 37 29 3d 3d 30 20 0a 20 20  ite_", 7)==0 .  
243bb 20 20 20 20 20 26 26 20 6d 65 6d 63 6d 70 28 26       && memcmp(&
243bc 70 54 61 62 2d 3e 7a 4e 61 6d 65 5b 37 5d 2c 22  pTab->zName[7],"
243bd 61 6c 74 65 72 74 61 62 5f 22 2c 39 29 21 3d 30  altertab_",9)!=0
243be 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
243bf 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
243c0 22 74 61 62 6c 65 20 25 73 20 6d 61 79 20 6e 6f  "table %s may no
243c1 74 20 62 65 20 69 6e 64 65 78 65 64 22 2c 20 70  t be indexed", p
243c2 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Tab->zName);.   
243c3 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
243c4 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 23 69 66  e_index;.  }.#if
243c5 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
243c6 5f 56 49 45 57 0a 20 20 69 66 28 20 70 54 61 62  _VIEW.  if( pTab
243c7 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ->pSelect ){.   
243c8 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
243c9 28 70 50 61 72 73 65 2c 20 22 76 69 65 77 73 20  (pParse, "views 
243ca 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e 64 65 78  may not be index
243cb 65 64 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  ed");.    goto e
243cc 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
243cd 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66  ;.  }.#endif.#if
243ce 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
243cf 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
243d0 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
243d1 61 62 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ab) ){.    sqlit
243d2 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
243d3 65 2c 20 22 76 69 72 74 75 61 6c 20 74 61 62 6c  e, "virtual tabl
243d4 65 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e  es may not be in
243d5 64 65 78 65 64 22 29 3b 0a 20 20 20 20 67 6f 74  dexed");.    got
243d6 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
243d7 64 65 78 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  dex;.  }.#endif.
243d8 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20  .  /*.  ** Find 
243d9 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
243da 69 6e 64 65 78 2e 20 20 4d 61 6b 65 20 73 75 72  index.  Make sur
243db 65 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 61  e there is not a
243dc 6c 72 65 61 64 79 20 61 6e 6f 74 68 65 72 0a 20  lready another. 
243dd 20 2a 2a 20 69 6e 64 65 78 20 6f 72 20 74 61 62   ** index or tab
243de 6c 65 20 77 69 74 68 20 74 68 65 20 73 61 6d 65  le with the same
243df 20 6e 61 6d 65 2e 20 20 0a 20 20 2a 2a 0a 20 20   name.  .  **.  
243e0 2a 2a 20 45 78 63 65 70 74 69 6f 6e 3a 20 20 49  ** Exception:  I
243e1 66 20 77 65 20 61 72 65 20 72 65 61 64 69 6e 67  f we are reading
243e2 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 70 65   the names of pe
243e3 72 6d 61 6e 65 6e 74 20 69 6e 64 69 63 65 73 20  rmanent indices 
243e4 66 72 6f 6d 20 74 68 65 0a 20 20 2a 2a 20 73 71  from the.  ** sq
243e5 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c  lite_master tabl
243e6 65 20 28 62 65 63 61 75 73 65 20 73 6f 6d 65 20  e (because some 
243e7 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 63 68  other process ch
243e8 61 6e 67 65 64 20 74 68 65 20 73 63 68 65 6d 61  anged the schema
243e9 29 20 61 6e 64 0a 20 20 2a 2a 20 6f 6e 65 20 6f  ) and.  ** one o
243ea 66 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65  f the index name
243eb 73 20 63 6f 6c 6c 69 64 65 73 20 77 69 74 68 20  s collides with 
243ec 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 74 65  the name of a te
243ed 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 6f 72  mporary table or
243ee 0a 20 20 2a 2a 20 69 6e 64 65 78 2c 20 74 68 65  .  ** index, the
243ef 6e 20 77 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e  n we will contin
243f0 75 65 20 74 6f 20 70 72 6f 63 65 73 73 20 74 68  ue to process th
243f1 69 73 20 69 6e 64 65 78 2e 0a 20 20 2a 2a 0a 20  is index..  **. 
243f2 20 2a 2a 20 49 66 20 70 4e 61 6d 65 3d 3d 30 20   ** If pName==0 
243f3 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65  it means that we
243f4 20 61 72 65 0a 20 20 2a 2a 20 64 65 61 6c 69 6e   are.  ** dealin
243f5 67 20 77 69 74 68 20 61 20 70 72 69 6d 61 72 79  g with a primary
243f6 20 6b 65 79 20 6f 72 20 55 4e 49 51 55 45 20 63   key or UNIQUE c
243f7 6f 6e 73 74 72 61 69 6e 74 2e 20 20 57 65 20 68  onstraint.  We h
243f8 61 76 65 20 74 6f 20 69 6e 76 65 6e 74 20 6f 75  ave to invent ou
243f9 72 0a 20 20 2a 2a 20 6f 77 6e 20 6e 61 6d 65 2e  r.  ** own name.
243fa 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4e 61 6d  .  */.  if( pNam
243fb 65 20 29 7b 0a 20 20 20 20 7a 4e 61 6d 65 20 3d  e ){.    zName =
243fc 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
243fd 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29  Token(db, pName)
243fe 3b 0a 20 20 20 20 69 66 28 20 7a 4e 61 6d 65 3d  ;.    if( zName=
243ff 3d 30 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63  =0 ) goto exit_c
24400 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20  reate_index;.   
24401 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
24402 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65  sqlite3CheckObje
24403 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a  ctName(pParse, z
24404 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 67  Name) ){.      g
24405 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f  oto exit_create_
24406 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20  index;.    }.   
24407 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62   if( !db->init.b
24408 75 73 79 20 29 7b 0a 20 20 20 20 20 20 69 66 28  usy ){.      if(
24409 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
2440a 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 30 29 21  e(db, zName, 0)!
2440b 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  =0 ){.        sq
2440c 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2440d 61 72 73 65 2c 20 22 74 68 65 72 65 20 69 73 20  arse, "there is 
2440e 61 6c 72 65 61 64 79 20 61 20 74 61 62 6c 65 20  already a table 
2440f 6e 61 6d 65 64 20 25 73 22 2c 20 7a 4e 61 6d 65  named %s", zName
24410 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  );.        goto 
24411 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
24412 78 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  x;.      }.    }
24413 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
24414 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 4e  FindIndex(db, zN
24415 61 6d 65 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29  ame, pDb->zName)
24416 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  !=0 ){.      if(
24417 20 21 69 66 4e 6f 74 45 78 69 73 74 20 29 7b 0a   !ifNotExist ){.
24418 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
24419 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2441a 22 69 6e 64 65 78 20 25 73 20 61 6c 72 65 61 64  "index %s alread
2441b 79 20 65 78 69 73 74 73 22 2c 20 7a 4e 61 6d 65  y exists", zName
2441c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
2441d 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
2441e 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20  e_index;.    }. 
2441f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20   }else{.    int 
24420 6e 3b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 4c  n;.    Index *pL
24421 6f 6f 70 3b 0a 20 20 20 20 66 6f 72 28 70 4c 6f  oop;.    for(pLo
24422 6f 70 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 2c  op=pTab->pIndex,
24423 20 6e 3d 31 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f   n=1; pLoop; pLo
24424 6f 70 3d 70 4c 6f 6f 70 2d 3e 70 4e 65 78 74 2c  op=pLoop->pNext,
24425 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 7a 4e 61 6d   n++){}.    zNam
24426 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  e = sqlite3MPrin
24427 74 66 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 61  tf(db, "sqlite_a
24428 75 74 6f 69 6e 64 65 78 5f 25 73 5f 25 64 22 2c  utoindex_%s_%d",
24429 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 6e 29   pTab->zName, n)
2442a 3b 0a 20 20 20 20 69 66 28 20 7a 4e 61 6d 65 3d  ;.    if( zName=
2442b 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  =0 ){.      goto
2442c 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
2442d 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ex;.    }.  }.. 
2442e 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 61 75   /* Check for au
2442f 74 68 6f 72 69 7a 61 74 69 6f 6e 20 74 6f 20 63  thorization to c
24430 72 65 61 74 65 20 61 6e 20 69 6e 64 65 78 2e 0a  reate an index..
24431 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
24432 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
24433 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 63  ZATION.  {.    c
24434 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d  onst char *zDb =
24435 20 70 44 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20   pDb->zName;.   
24436 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68   if( sqlite3Auth
24437 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51  Check(pParse, SQ
24438 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20 53 43 48  LITE_INSERT, SCH
24439 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20  EMA_TABLE(iDb), 
2443a 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20  0, zDb) ){.     
2443b 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
2443c 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20  e_index;.    }. 
2443d 20 20 20 69 20 3d 20 53 51 4c 49 54 45 5f 43 52     i = SQLITE_CR
2443e 45 41 54 45 5f 49 4e 44 45 58 3b 0a 20 20 20 20  EATE_INDEX;.    
2443f 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42  if( !OMIT_TEMPDB
24440 20 26 26 20 69 44 62 3d 3d 31 20 29 20 69 20 3d   && iDb==1 ) i =
24441 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54   SQLITE_CREATE_T
24442 45 4d 50 5f 49 4e 44 45 58 3b 0a 20 20 20 20 69  EMP_INDEX;.    i
24443 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
24444 65 63 6b 28 70 50 61 72 73 65 2c 20 69 2c 20 7a  eck(pParse, i, z
24445 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  Name, pTab->zNam
24446 65 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20  e, zDb) ){.     
24447 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
24448 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20  e_index;.    }. 
24449 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
2444a 49 66 20 70 4c 69 73 74 3d 3d 30 2c 20 69 74 20  If pList==0, it 
2444b 6d 65 61 6e 73 20 74 68 69 73 20 72 6f 75 74 69  means this routi
2444c 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 20 74 6f  ne was called to
2444d 20 6d 61 6b 65 20 61 20 70 72 69 6d 61 72 79 0a   make a primary.
2444e 20 20 2a 2a 20 6b 65 79 20 6f 75 74 20 6f 66 20    ** key out of 
2444f 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20  the last column 
24450 61 64 64 65 64 20 74 6f 20 74 68 65 20 74 61 62  added to the tab
24451 6c 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  le under constru
24452 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53 6f 20 63  ction..  ** So c
24453 72 65 61 74 65 20 61 20 66 61 6b 65 20 6c 69 73  reate a fake lis
24454 74 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 74 68  t to simulate th
24455 69 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  is..  */.  if( p
24456 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 6e  List==0 ){.    n
24457 75 6c 6c 49 64 2e 7a 20 3d 20 70 54 61 62 2d 3e  ullId.z = pTab->
24458 61 43 6f 6c 5b 70 54 61 62 2d 3e 6e 43 6f 6c 2d  aCol[pTab->nCol-
24459 31 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 75  1].zName;.    nu
2445a 6c 6c 49 64 2e 6e 20 3d 20 73 71 6c 69 74 65 33  llId.n = sqlite3
2445b 53 74 72 6c 65 6e 33 30 28 28 63 68 61 72 2a 29  Strlen30((char*)
2445c 6e 75 6c 6c 49 64 2e 7a 29 3b 0a 20 20 20 20 70  nullId.z);.    p
2445d 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78  List = sqlite3Ex
2445e 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
2445f 72 73 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  rse, 0, 0);.    
24460 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 67  if( pList==0 ) g
24461 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f  oto exit_create_
24462 69 6e 64 65 78 3b 0a 20 20 20 20 73 71 6c 69 74  index;.    sqlit
24463 65 33 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d  e3ExprListSetNam
24464 65 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c  e(pParse, pList,
24465 20 26 6e 75 6c 6c 49 64 2c 20 30 29 3b 0a 20 20   &nullId, 0);.  
24466 20 20 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 73 6f    pList->a[0].so
24467 72 74 4f 72 64 65 72 20 3d 20 28 75 38 29 73 6f  rtOrder = (u8)so
24468 72 74 4f 72 64 65 72 3b 0a 20 20 7d 0a 0a 20 20  rtOrder;.  }..  
24469 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f  /* Figure out ho
2446a 77 20 6d 61 6e 79 20 62 79 74 65 73 20 6f 66 20  w many bytes of 
2446b 73 70 61 63 65 20 61 72 65 20 72 65 71 75 69 72  space are requir
2446c 65 64 20 74 6f 20 73 74 6f 72 65 20 65 78 70 6c  ed to store expl
2446d 69 63 69 74 6c 79 0a 20 20 2a 2a 20 73 70 65 63  icitly.  ** spec
2446e 69 66 69 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20  ified collation 
2446f 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 73 2e 0a  sequence names..
24470 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20    */.  for(i=0; 
24471 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  i<pList->nExpr; 
24472 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a  i++){.    Expr *
24473 70 45 78 70 72 20 3d 20 70 4c 69 73 74 2d 3e 61  pExpr = pList->a
24474 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 69  [i].pExpr;.    i
24475 66 28 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20  f( pExpr ){.    
24476 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
24477 20 3d 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b   = pExpr->pColl;
24478 0a 20 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72  .      /* Either
24479 20 70 43 6f 6c 6c 21 3d 30 20 6f 72 20 74 68 65   pColl!=0 or the
2447a 72 65 20 77 61 73 20 61 6e 20 4f 4f 4d 20 66 61  re was an OOM fa
2447b 69 6c 75 72 65 2e 20 20 42 75 74 20 69 66 20 61  ilure.  But if a
2447c 6e 20 4f 4f 4d 0a 20 20 20 20 20 20 2a 2a 20 66  n OOM.      ** f
2447d 61 69 6c 75 72 65 20 77 65 20 68 61 76 65 20 71  ailure we have q
2447e 75 69 74 20 62 65 66 6f 72 65 20 72 65 61 63 68  uit before reach
2447f 69 6e 67 20 74 68 69 73 20 70 6f 69 6e 74 2e 20  ing this point. 
24480 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57  */.      if( ALW
24481 41 59 53 28 70 43 6f 6c 6c 29 20 29 7b 0a 20 20  AYS(pColl) ){.  
24482 20 20 20 20 20 20 6e 45 78 74 72 61 20 2b 3d 20        nExtra += 
24483 28 31 20 2b 20 73 71 6c 69 74 65 33 53 74 72 6c  (1 + sqlite3Strl
24484 65 6e 33 30 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d  en30(pColl->zNam
24485 65 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  e));.      }.   
24486 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 0a 20 20   }.  }..  /* .  
24487 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20  ** Allocate the 
24488 69 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 2e  index structure.
24489 20 0a 20 20 2a 2f 0a 20 20 6e 4e 61 6d 65 20 3d   .  */.  nName =
2448a 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2448b 28 7a 4e 61 6d 65 29 3b 0a 20 20 6e 43 6f 6c 20  (zName);.  nCol 
2448c 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a  = pList->nExpr;.
2448d 20 20 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74    pIndex = sqlit
2448e 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
2448f 62 2c 20 0a 20 20 20 20 20 20 73 69 7a 65 6f 66  b, .      sizeof
24490 28 49 6e 64 65 78 29 20 2b 20 20 20 20 20 20 20  (Index) +       
24491 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
24492 73 74 72 75 63 74 75 72 65 20 20 2a 2f 0a 20 20  structure  */.  
24493 20 20 20 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a      sizeof(int)*
24494 6e 43 6f 6c 20 2b 20 20 20 20 20 20 20 20 20 20  nCol +          
24495 20 2f 2a 20 49 6e 64 65 78 2e 61 69 43 6f 6c 75   /* Index.aiColu
24496 6d 6e 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 69  mn   */.      si
24497 7a 65 6f 66 28 69 6e 74 29 2a 28 6e 43 6f 6c 2b  zeof(int)*(nCol+
24498 31 29 20 2b 20 20 20 20 20 20 20 2f 2a 20 49 6e  1) +       /* In
24499 64 65 78 2e 61 69 52 6f 77 45 73 74 20 20 20 2a  dex.aiRowEst   *
2449a 2f 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 63  /.      sizeof(c
2449b 68 61 72 20 2a 29 2a 6e 43 6f 6c 20 2b 20 20 20  har *)*nCol +   
2449c 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 7a       /* Index.az
2449d 43 6f 6c 6c 20 20 20 20 20 2a 2f 0a 20 20 20 20  Coll     */.    
2449e 20 20 73 69 7a 65 6f 66 28 75 38 29 2a 6e 43 6f    sizeof(u8)*nCo
2449f 6c 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 2f  l +            /
244a0 2a 20 49 6e 64 65 78 2e 61 53 6f 72 74 4f 72 64  * Index.aSortOrd
244a1 65 72 20 2a 2f 0a 20 20 20 20 20 20 6e 4e 61 6d  er */.      nNam
244a2 65 20 2b 20 31 20 2b 20 20 20 20 20 20 20 20 20  e + 1 +         
244a3 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
244a4 78 2e 7a 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a  x.zName      */.
244a5 20 20 20 20 20 20 6e 45 78 74 72 61 20 20 20 20        nExtra    
244a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
244a7 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20     /* Collation 
244a8 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 73 20 2a  sequence names *
244a9 2f 0a 20 20 29 3b 0a 20 20 69 66 28 20 64 62 2d  /.  );.  if( db-
244aa 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
244ab 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63  .    goto exit_c
244ac 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d  reate_index;.  }
244ad 0a 20 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c  .  pIndex->azCol
244ae 6c 20 3d 20 28 63 68 61 72 2a 2a 29 28 26 70 49  l = (char**)(&pI
244af 6e 64 65 78 5b 31 5d 29 3b 0a 20 20 70 49 6e 64  ndex[1]);.  pInd
244b0 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 20 3d 20 28  ex->aiColumn = (
244b1 69 6e 74 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e  int *)(&pIndex->
244b2 61 7a 43 6f 6c 6c 5b 6e 43 6f 6c 5d 29 3b 0a 20  azColl[nCol]);. 
244b3 20 70 49 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73   pIndex->aiRowEs
244b4 74 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 2a 29  t = (unsigned *)
244b5 28 26 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75  (&pIndex->aiColu
244b6 6d 6e 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 70 49 6e  mn[nCol]);.  pIn
244b7 64 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20  dex->aSortOrder 
244b8 3d 20 28 75 38 20 2a 29 28 26 70 49 6e 64 65 78  = (u8 *)(&pIndex
244b9 2d 3e 61 69 52 6f 77 45 73 74 5b 6e 43 6f 6c 2b  ->aiRowEst[nCol+
244ba 31 5d 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 7a  1]);.  pIndex->z
244bb 4e 61 6d 65 20 3d 20 28 63 68 61 72 20 2a 29 28  Name = (char *)(
244bc 26 70 49 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72  &pIndex->aSortOr
244bd 64 65 72 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 7a 45  der[nCol]);.  zE
244be 78 74 72 61 20 3d 20 28 63 68 61 72 20 2a 29 28  xtra = (char *)(
244bf 26 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 5b 6e  &pIndex->zName[n
244c0 4e 61 6d 65 2b 31 5d 29 3b 0a 20 20 6d 65 6d 63  Name+1]);.  memc
244c1 70 79 28 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65  py(pIndex->zName
244c2 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31  , zName, nName+1
244c3 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 70 54 61  );.  pIndex->pTa
244c4 62 6c 65 20 3d 20 70 54 61 62 3b 0a 20 20 70 49  ble = pTab;.  pI
244c5 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20  ndex->nColumn = 
244c6 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20  pList->nExpr;.  
244c7 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 20  pIndex->onError 
244c8 3d 20 28 75 38 29 6f 6e 45 72 72 6f 72 3b 0a 20  = (u8)onError;. 
244c9 20 70 49 6e 64 65 78 2d 3e 61 75 74 6f 49 6e 64   pIndex->autoInd
244ca 65 78 20 3d 20 28 75 38 29 28 70 4e 61 6d 65 3d  ex = (u8)(pName=
244cb 3d 30 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 70  =0);.  pIndex->p
244cc 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62  Schema = db->aDb
244cd 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 0a 0a  [iDb].pSchema;..
244ce 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65    /* Check to se
244cf 65 20 69 66 20 77 65 20 73 68 6f 75 6c 64 20 68  e if we should h
244d0 6f 6e 6f 72 20 44 45 53 43 20 72 65 71 75 65 73  onor DESC reques
244d1 74 73 20 6f 6e 20 69 6e 64 65 78 20 63 6f 6c 75  ts on index colu
244d2 6d 6e 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  mns.  */.  if( p
244d3 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c  Db->pSchema->fil
244d4 65 5f 66 6f 72 6d 61 74 3e 3d 34 20 29 7b 0a 20  e_format>=4 ){. 
244d5 20 20 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b     sortOrderMask
244d6 20 3d 20 2d 31 3b 20 20 20 2f 2a 20 48 6f 6e 6f   = -1;   /* Hono
244d7 72 20 44 45 53 43 20 2a 2f 0a 20 20 7d 65 6c 73  r DESC */.  }els
244d8 65 7b 0a 20 20 20 20 73 6f 72 74 4f 72 64 65 72  e{.    sortOrder
244d9 4d 61 73 6b 20 3d 20 30 3b 20 20 20 20 2f 2a 20  Mask = 0;    /* 
244da 49 67 6e 6f 72 65 20 44 45 53 43 20 2a 2f 0a 20  Ignore DESC */. 
244db 20 7d 0a 0a 20 20 2f 2a 20 53 63 61 6e 20 74 68   }..  /* Scan th
244dc 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 63  e names of the c
244dd 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 74 61  olumns of the ta
244de 62 6c 65 20 74 6f 20 62 65 20 69 6e 64 65 78 65  ble to be indexe
244df 64 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f 61 64 20  d and.  ** load 
244e0 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 69 63  the column indic
244e1 65 73 20 69 6e 74 6f 20 74 68 65 20 49 6e 64 65  es into the Inde
244e2 78 20 73 74 72 75 63 74 75 72 65 2e 20 20 52 65  x structure.  Re
244e3 70 6f 72 74 20 61 6e 20 65 72 72 6f 72 0a 20 20  port an error.  
244e4 2a 2a 20 69 66 20 61 6e 79 20 63 6f 6c 75 6d 6e  ** if any column
244e5 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 20   is not found.. 
244e6 20 2a 2a 0a 20 20 2a 2a 20 54 4f 44 4f 3a 20 20   **.  ** TODO:  
244e7 41 64 64 20 61 20 74 65 73 74 20 74 6f 20 6d 61  Add a test to ma
244e8 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65  ke sure that the
244e9 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 20 69 73 20   same column is 
244ea 6e 6f 74 20 6e 61 6d 65 64 0a 20 20 2a 2a 20 6d  not named.  ** m
244eb 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 20 77 69  ore than once wi
244ec 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20 69 6e  thin the same in
244ed 64 65 78 2e 20 20 4f 6e 6c 79 20 74 68 65 20 66  dex.  Only the f
244ee 69 72 73 74 20 69 6e 73 74 61 6e 63 65 20 6f 66  irst instance of
244ef 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e  .  ** the column
244f0 20 77 69 6c 6c 20 65 76 65 72 20 62 65 20 75 73   will ever be us
244f1 65 64 20 62 79 20 74 68 65 20 6f 70 74 69 6d 69  ed by the optimi
244f2 7a 65 72 2e 20 20 4e 6f 74 65 20 74 68 61 74 20  zer.  Note that 
244f3 75 73 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 73  using the.  ** s
244f4 61 6d 65 20 63 6f 6c 75 6d 6e 20 6d 6f 72 65 20  ame column more 
244f5 74 68 61 6e 20 6f 6e 63 65 20 63 61 6e 6e 6f 74  than once cannot
244f6 20 62 65 20 61 6e 20 65 72 72 6f 72 20 62 65 63   be an error bec
244f7 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64 20  ause that would 
244f8 0a 20 20 2a 2a 20 62 72 65 61 6b 20 62 61 63 6b  .  ** break back
244f9 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c  wards compatibil
244fa 69 74 79 20 2d 20 69 74 20 6e 65 65 64 73 20 74  ity - it needs t
244fb 6f 20 62 65 20 61 20 77 61 72 6e 69 6e 67 2e 0a  o be a warning..
244fc 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20    */.  for(i=0, 
244fd 70 4c 69 73 74 49 74 65 6d 3d 70 4c 69 73 74 2d  pListItem=pList-
244fe 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78  >a; i<pList->nEx
244ff 70 72 3b 20 69 2b 2b 2c 20 70 4c 69 73 74 49 74  pr; i++, pListIt
24500 65 6d 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73 74  em++){.    const
24501 20 63 68 61 72 20 2a 7a 43 6f 6c 4e 61 6d 65 20   char *zColName 
24502 3d 20 70 4c 69 73 74 49 74 65 6d 2d 3e 7a 4e 61  = pListItem->zNa
24503 6d 65 3b 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a  me;.    Column *
24504 70 54 61 62 43 6f 6c 3b 0a 20 20 20 20 69 6e 74  pTabCol;.    int
24505 20 72 65 71 75 65 73 74 65 64 53 6f 72 74 4f 72   requestedSortOr
24506 64 65 72 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a  der;.    char *z
24507 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Coll;           
24508 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61          /* Colla
24509 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61  tion sequence na
2450a 6d 65 20 2a 2f 0a 0a 20 20 20 20 66 6f 72 28 6a  me */..    for(j
2450b 3d 30 2c 20 70 54 61 62 43 6f 6c 3d 70 54 61 62  =0, pTabCol=pTab
2450c 2d 3e 61 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e  ->aCol; j<pTab->
2450d 6e 43 6f 6c 3b 20 6a 2b 2b 2c 20 70 54 61 62 43  nCol; j++, pTabC
2450e 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28  ol++){.      if(
2450f 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
24510 7a 43 6f 6c 4e 61 6d 65 2c 20 70 54 61 62 43 6f  zColName, pTabCo
24511 6c 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62  l->zName)==0 ) b
24512 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
24513 69 66 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43 6f  if( j>=pTab->nCo
24514 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  l ){.      sqlit
24515 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
24516 65 2c 20 22 74 61 62 6c 65 20 25 73 20 68 61 73  e, "table %s has
24517 20 6e 6f 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64   no column named
24518 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 70 54   %s",.        pT
24519 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 4e  ab->zName, zColN
2451a 61 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f  ame);.      goto
2451b 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
2451c 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49  ex;.    }.    pI
2451d 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69  ndex->aiColumn[i
2451e 5d 20 3d 20 6a 3b 0a 20 20 20 20 2f 2a 20 4a 75  ] = j;.    /* Ju
2451f 73 74 69 66 69 63 61 74 69 6f 6e 20 6f 66 20 74  stification of t
24520 68 65 20 41 4c 57 41 59 53 28 70 4c 69 73 74 49  he ALWAYS(pListI
24521 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c  tem->pExpr->pCol
24522 6c 29 3a 20 20 42 65 63 61 75 73 65 20 6f 66 0a  l):  Because of.
24523 20 20 20 20 2a 2a 20 74 68 65 20 77 61 79 20 74      ** the way t
24524 68 65 20 22 69 64 78 6c 69 73 74 22 20 6e 6f 6e  he "idxlist" non
24525 2d 74 65 72 6d 69 6e 61 6c 20 69 73 20 63 6f 6e  -terminal is con
24526 73 74 72 75 63 74 65 64 20 62 79 20 74 68 65 20  structed by the 
24527 70 61 72 73 65 72 2c 0a 20 20 20 20 2a 2a 20 69  parser,.    ** i
24528 66 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78  f pListItem->pEx
24529 70 72 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 20 74  pr is not null t
2452a 68 65 6e 20 65 69 74 68 65 72 20 70 4c 69 73 74  hen either pList
2452b 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f  Item->pExpr->pCo
2452c 6c 6c 0a 20 20 20 20 2a 2a 20 6d 75 73 74 20 65  ll.    ** must e
2452d 78 69 73 74 20 6f 72 20 65 6c 73 65 20 74 68 65  xist or else the
2452e 72 65 20 6d 75 73 74 20 68 61 76 65 20 62 65 65  re must have bee
2452f 6e 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e 20  n an OOM error. 
24530 20 42 75 74 20 69 66 20 74 68 65 72 65 0a 20 20   But if there.  
24531 20 20 2a 2a 20 77 61 73 20 61 6e 20 4f 4f 4d 20    ** was an OOM 
24532 65 72 72 6f 72 2c 20 77 65 20 77 6f 75 6c 64 20  error, we would 
24533 6e 65 76 65 72 20 72 65 61 63 68 20 74 68 69 73  never reach this
24534 20 70 6f 69 6e 74 2e 20 2a 2f 0a 20 20 20 20 69   point. */.    i
24535 66 28 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45  f( pListItem->pE
24536 78 70 72 20 26 26 20 41 4c 57 41 59 53 28 70 4c  xpr && ALWAYS(pL
24537 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e  istItem->pExpr->
24538 70 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20  pColl) ){.      
24539 69 6e 74 20 6e 43 6f 6c 6c 3b 0a 20 20 20 20 20  int nColl;.     
2453a 20 7a 43 6f 6c 6c 20 3d 20 70 4c 69 73 74 49 74   zColl = pListIt
2453b 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c 6c  em->pExpr->pColl
2453c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 6e  ->zName;.      n
2453d 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 53 74  Coll = sqlite3St
2453e 72 6c 65 6e 33 30 28 7a 43 6f 6c 6c 29 20 2b 20  rlen30(zColl) + 
2453f 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  1;.      assert(
24540 20 6e 45 78 74 72 61 3e 3d 6e 43 6f 6c 6c 20 29   nExtra>=nColl )
24541 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a  ;.      memcpy(z
24542 45 78 74 72 61 2c 20 7a 43 6f 6c 6c 2c 20 6e 43  Extra, zColl, nC
24543 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 7a 43 6f 6c  oll);.      zCol
24544 6c 20 3d 20 7a 45 78 74 72 61 3b 0a 20 20 20 20  l = zExtra;.    
24545 20 20 7a 45 78 74 72 61 20 2b 3d 20 6e 43 6f 6c    zExtra += nCol
24546 6c 3b 0a 20 20 20 20 20 20 6e 45 78 74 72 61 20  l;.      nExtra 
24547 2d 3d 20 6e 43 6f 6c 6c 3b 0a 20 20 20 20 7d 65  -= nColl;.    }e
24548 6c 73 65 7b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c  lse{.      zColl
24549 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d   = pTab->aCol[j]
2454a 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 69 66  .zColl;.      if
2454b 28 20 21 7a 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  ( !zColl ){.    
2454c 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 64 62 2d 3e      zColl = db->
2454d 70 44 66 6c 74 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65  pDfltColl->zName
2454e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2454f 20 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69      if( !db->ini
24550 74 2e 62 75 73 79 20 26 26 20 21 73 71 6c 69 74  t.busy && !sqlit
24551 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28  e3LocateCollSeq(
24552 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 29 20 29  pParse, zColl) )
24553 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69  {.      goto exi
24554 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
24555 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 64 65 78      }.    pIndex
24556 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 20 3d 20 7a 43  ->azColl[i] = zC
24557 6f 6c 6c 3b 0a 20 20 20 20 72 65 71 75 65 73 74  oll;.    request
24558 65 64 53 6f 72 74 4f 72 64 65 72 20 3d 20 70 4c  edSortOrder = pL
24559 69 73 74 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64  istItem->sortOrd
2455a 65 72 20 26 20 73 6f 72 74 4f 72 64 65 72 4d 61  er & sortOrderMa
2455b 73 6b 3b 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e  sk;.    pIndex->
2455c 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20  aSortOrder[i] = 
2455d 28 75 38 29 72 65 71 75 65 73 74 65 64 53 6f 72  (u8)requestedSor
2455e 74 4f 72 64 65 72 3b 0a 20 20 7d 0a 20 20 73 71  tOrder;.  }.  sq
2455f 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45  lite3DefaultRowE
24560 73 74 28 70 49 6e 64 65 78 29 3b 0a 0a 20 20 69  st(pIndex);..  i
24561 66 28 20 70 54 61 62 3d 3d 70 50 61 72 73 65 2d  f( pTab==pParse-
24562 3e 70 4e 65 77 54 61 62 6c 65 20 29 7b 0a 20 20  >pNewTable ){.  
24563 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e    /* This routin
24564 65 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65  e has been calle
24565 64 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20 61  d to create an a
24566 75 74 6f 6d 61 74 69 63 20 69 6e 64 65 78 20 61  utomatic index a
24567 73 20 61 0a 20 20 20 20 2a 2a 20 72 65 73 75 6c  s a.    ** resul
24568 74 20 6f 66 20 61 20 50 52 49 4d 41 52 59 20 4b  t of a PRIMARY K
24569 45 59 20 6f 72 20 55 4e 49 51 55 45 20 63 6c 61  EY or UNIQUE cla
2456a 75 73 65 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20  use on a column 
2456b 64 65 66 69 6e 69 74 69 6f 6e 2c 20 6f 72 0a 20  definition, or. 
2456c 20 20 20 2a 2a 20 61 20 50 52 49 4d 41 52 59 20     ** a PRIMARY 
2456d 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 20 63 6c  KEY or UNIQUE cl
2456e 61 75 73 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74  ause following t
2456f 68 65 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69  he column defini
24570 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2a 20 69 2e  tions..    ** i.
24571 65 2e 20 6f 6e 65 20 6f 66 3a 0a 20 20 20 20 2a  e. one of:.    *
24572 2a 0a 20 20 20 20 2a 2a 20 43 52 45 41 54 45 20  *.    ** CREATE 
24573 54 41 42 4c 45 20 74 28 78 20 50 52 49 4d 41 52  TABLE t(x PRIMAR
24574 59 20 4b 45 59 2c 20 79 29 3b 0a 20 20 20 20 2a  Y KEY, y);.    *
24575 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74  * CREATE TABLE t
24576 28 78 2c 20 79 2c 20 55 4e 49 51 55 45 28 78 2c  (x, y, UNIQUE(x,
24577 20 79 29 29 3b 0a 20 20 20 20 2a 2a 0a 20 20 20   y));.    **.   
24578 20 2a 2a 20 45 69 74 68 65 72 20 77 61 79 2c 20   ** Either way, 
24579 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  check to see if 
2457a 74 68 65 20 74 61 62 6c 65 20 61 6c 72 65 61 64  the table alread
2457b 79 20 68 61 73 20 73 75 63 68 20 61 6e 20 69 6e  y has such an in
2457c 64 65 78 2e 20 49 66 0a 20 20 20 20 2a 2a 20 73  dex. If.    ** s
2457d 6f 2c 20 64 6f 6e 27 74 20 62 6f 74 68 65 72 20  o, don't bother 
2457e 63 72 65 61 74 69 6e 67 20 74 68 69 73 20 6f 6e  creating this on
2457f 65 2e 20 54 68 69 73 20 6f 6e 6c 79 20 61 70 70  e. This only app
24580 6c 69 65 73 20 74 6f 0a 20 20 20 20 2a 2a 20 61  lies to.    ** a
24581 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65  utomatically cre
24582 61 74 65 64 20 69 6e 64 69 63 65 73 2e 20 55 73  ated indices. Us
24583 65 72 73 20 63 61 6e 20 64 6f 20 61 73 20 74 68  ers can do as th
24584 65 79 20 77 69 73 68 20 77 69 74 68 0a 20 20 20  ey wish with.   
24585 20 2a 2a 20 65 78 70 6c 69 63 69 74 20 69 6e 64   ** explicit ind
24586 69 63 65 73 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  ices..    **.   
24587 20 2a 2a 20 54 77 6f 20 55 4e 49 51 55 45 20 6f   ** Two UNIQUE o
24588 72 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f  r PRIMARY KEY co
24589 6e 73 74 72 61 69 6e 74 73 20 61 72 65 20 63 6f  nstraints are co
2458a 6e 73 69 64 65 72 65 64 20 65 71 75 69 76 61 6c  nsidered equival
2458b 65 6e 74 0a 20 20 20 20 2a 2a 20 28 61 6e 64 20  ent.    ** (and 
2458c 74 68 75 73 20 73 75 70 70 72 65 73 73 69 6e 67  thus suppressing
2458d 20 74 68 65 20 73 65 63 6f 6e 64 20 6f 6e 65 29   the second one)
2458e 20 65 76 65 6e 20 69 66 20 74 68 65 79 20 68 61   even if they ha
2458f 76 65 20 64 69 66 66 65 72 65 6e 74 0a 20 20 20  ve different.   
24590 20 2a 2a 20 73 6f 72 74 20 6f 72 64 65 72 73 2e   ** sort orders.
24591 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49  .    **.    ** I
24592 66 20 74 68 65 72 65 20 61 72 65 20 64 69 66 66  f there are diff
24593 65 72 65 6e 74 20 63 6f 6c 6c 61 74 69 6e 67 20  erent collating 
24594 73 65 71 75 65 6e 63 65 73 20 6f 72 20 69 66 20  sequences or if 
24595 74 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 0a 20  the columns of. 
24596 20 20 20 2a 2a 20 74 68 65 20 63 6f 6e 73 74 72     ** the constr
24597 61 69 6e 74 20 6f 63 63 75 72 20 69 6e 20 64 69  aint occur in di
24598 66 66 65 72 65 6e 74 20 6f 72 64 65 72 73 2c 20  fferent orders, 
24599 74 68 65 6e 20 74 68 65 20 63 6f 6e 73 74 72 61  then the constra
2459a 69 6e 74 73 20 61 72 65 0a 20 20 20 20 2a 2a 20  ints are.    ** 
2459b 63 6f 6e 73 69 64 65 72 65 64 20 64 69 73 74 69  considered disti
2459c 6e 63 74 20 61 6e 64 20 62 6f 74 68 20 72 65 73  nct and both res
2459d 75 6c 74 20 69 6e 20 73 65 70 61 72 61 74 65 20  ult in separate 
2459e 69 6e 64 69 63 65 73 2e 0a 20 20 20 20 2a 2f 0a  indices..    */.
2459f 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b      Index *pIdx;
245a0 0a 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54  .    for(pIdx=pT
245a1 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78  ab->pIndex; pIdx
245a2 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65  ; pIdx=pIdx->pNe
245a3 78 74 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b  xt){.      int k
245a4 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
245a5 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f  pIdx->onError!=O
245a6 45 5f 4e 6f 6e 65 20 29 3b 0a 20 20 20 20 20 20  E_None );.      
245a7 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 61 75  assert( pIdx->au
245a8 74 6f 49 6e 64 65 78 20 29 3b 0a 20 20 20 20 20  toIndex );.     
245a9 20 61 73 73 65 72 74 28 20 70 49 6e 64 65 78 2d   assert( pIndex-
245aa 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e  >onError!=OE_Non
245ab 65 20 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20  e );..      if( 
245ac 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d 70  pIdx->nColumn!=p
245ad 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29  Index->nColumn )
245ae 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
245af 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 49 64 78   for(k=0; k<pIdx
245b0 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b 29 7b  ->nColumn; k++){
245b1 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63  .        const c
245b2 68 61 72 20 2a 7a 31 3b 0a 20 20 20 20 20 20 20  har *z1;.       
245b3 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 3b   const char *z2;
245b4 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64  .        if( pId
245b5 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6b 5d 21 3d  x->aiColumn[k]!=
245b6 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e  pIndex->aiColumn
245b7 5b 6b 5d 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  [k] ) break;.   
245b8 20 20 20 20 20 7a 31 20 3d 20 70 49 64 78 2d 3e       z1 = pIdx->
245b9 61 7a 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20 20 20 20  azColl[k];.     
245ba 20 20 20 7a 32 20 3d 20 70 49 6e 64 65 78 2d 3e     z2 = pIndex->
245bb 61 7a 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20 20 20 20  azColl[k];.     
245bc 20 20 20 69 66 28 20 7a 31 21 3d 7a 32 20 26 26     if( z1!=z2 &&
245bd 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
245be 7a 31 2c 20 7a 32 29 20 29 20 62 72 65 61 6b 3b  z1, z2) ) break;
245bf 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
245c0 66 28 20 6b 3d 3d 70 49 64 78 2d 3e 6e 43 6f 6c  f( k==pIdx->nCol
245c1 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 69  umn ){.        i
245c2 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72  f( pIdx->onError
245c3 21 3d 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f  !=pIndex->onErro
245c4 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  r ){.          /
245c5 2a 20 54 68 69 73 20 63 6f 6e 73 74 72 61 69 6e  * This constrain
245c6 74 20 63 72 65 61 74 65 73 20 74 68 65 20 73 61  t creates the sa
245c7 6d 65 20 69 6e 64 65 78 20 61 73 20 61 20 70 72  me index as a pr
245c8 65 76 69 6f 75 73 0a 20 20 20 20 20 20 20 20 20  evious.         
245c9 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 73   ** constraint s
245ca 70 65 63 69 66 69 65 64 20 73 6f 6d 65 77 68 65  pecified somewhe
245cb 72 65 20 69 6e 20 74 68 65 20 43 52 45 41 54 45  re in the CREATE
245cc 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
245cd 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 48  ..          ** H
245ce 6f 77 65 76 65 72 20 74 68 65 20 4f 4e 20 43 4f  owever the ON CO
245cf 4e 46 4c 49 43 54 20 63 6c 61 75 73 65 73 20 61  NFLICT clauses a
245d0 72 65 20 64 69 66 66 65 72 65 6e 74 2e 20 49 66  re different. If
245d1 20 62 6f 74 68 20 74 68 69 73 20 0a 20 20 20 20   both this .    
245d2 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72 61        ** constra
245d3 69 6e 74 20 61 6e 64 20 74 68 65 20 70 72 65 76  int and the prev
245d4 69 6f 75 73 20 65 71 75 69 76 61 6c 65 6e 74 20  ious equivalent 
245d5 63 6f 6e 73 74 72 61 69 6e 74 20 68 61 76 65 20  constraint have 
245d6 65 78 70 6c 69 63 69 74 0a 20 20 20 20 20 20 20  explicit.       
245d7 20 20 20 2a 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43     ** ON CONFLIC
245d8 54 20 63 6c 61 75 73 65 73 20 74 68 69 73 20 69  T clauses this i
245d9 73 20 61 6e 20 65 72 72 6f 72 2e 20 4f 74 68 65  s an error. Othe
245da 72 77 69 73 65 2c 20 75 73 65 20 74 68 65 0a 20  rwise, use the. 
245db 20 20 20 20 20 20 20 20 20 2a 2a 20 65 78 70 6c           ** expl
245dc 69 63 69 74 6c 79 20 73 70 65 63 69 66 69 65 64  icitly specified
245dd 20 62 65 68 61 76 69 6f 75 72 20 66 6f 72 20 74   behaviour for t
245de 68 65 20 69 6e 64 65 78 2e 0a 20 20 20 20 20 20  he index..      
245df 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20      */.         
245e0 20 69 66 28 20 21 28 70 49 64 78 2d 3e 6f 6e 45   if( !(pIdx->onE
245e1 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74  rror==OE_Default
245e2 20 7c 7c 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72   || pIndex->onEr
245e3 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 29  ror==OE_Default)
245e4 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
245e5 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
245e6 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
245e7 20 20 20 20 20 20 20 20 20 22 63 6f 6e 66 6c 69           "confli
245e8 63 74 69 6e 67 20 4f 4e 20 43 4f 4e 46 4c 49 43  cting ON CONFLIC
245e9 54 20 63 6c 61 75 73 65 73 20 73 70 65 63 69 66  T clauses specif
245ea 69 65 64 22 2c 20 30 29 3b 0a 20 20 20 20 20 20  ied", 0);.      
245eb 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
245ec 69 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f  if( pIdx->onErro
245ed 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b  r==OE_Default ){
245ee 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 49 64  .            pId
245ef 78 2d 3e 6f 6e 45 72 72 6f 72 20 3d 20 70 49 6e  x->onError = pIn
245f0 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 3b 0a 20 20  dex->onError;.  
245f1 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
245f2 20 20 7d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f    }.        goto
245f3 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
245f4 65 78 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ex;.      }.    
245f5 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 69 6e 6b  }.  }..  /* Link
245f6 20 74 68 65 20 6e 65 77 20 49 6e 64 65 78 20 73   the new Index s
245f7 74 72 75 63 74 75 72 65 20 74 6f 20 69 74 73 20  tructure to its 
245f8 74 61 62 6c 65 20 61 6e 64 20 74 6f 20 74 68 65  table and to the
245f9 20 6f 74 68 65 72 0a 20 20 2a 2a 20 69 6e 2d 6d   other.  ** in-m
245fa 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 73  emory database s
245fb 74 72 75 63 74 75 72 65 73 2e 20 0a 20 20 2a 2f  tructures. .  */
245fc 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e  .  if( db->init.
245fd 62 75 73 79 20 29 7b 0a 20 20 20 20 49 6e 64 65  busy ){.    Inde
245fe 78 20 2a 70 3b 0a 20 20 20 20 70 20 3d 20 73 71  x *p;.    p = sq
245ff 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
24600 26 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61  &pIndex->pSchema
24601 2d 3e 69 64 78 48 61 73 68 2c 20 0a 20 20 20 20  ->idxHash, .    
24602 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24603 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 7a 4e        pIndex->zN
24604 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c  ame, sqlite3Strl
24605 65 6e 33 30 28 70 49 6e 64 65 78 2d 3e 7a 4e 61  en30(pIndex->zNa
24606 6d 65 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  me),.           
24607 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
24608 49 6e 64 65 78 29 3b 0a 20 20 20 20 69 66 28 20  Index);.    if( 
24609 70 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  p ){.      asser
2460a 74 28 20 70 3d 3d 70 49 6e 64 65 78 20 29 3b 20  t( p==pIndex ); 
2460b 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75 73 74 20   /* Malloc must 
2460c 68 61 76 65 20 66 61 69 6c 65 64 20 2a 2f 0a 20  have failed */. 
2460d 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46       db->mallocF
2460e 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  ailed = 1;.     
2460f 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
24610 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20  e_index;.    }. 
24611 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20     db->flags |= 
24612 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61  SQLITE_InternCha
24613 6e 67 65 73 3b 0a 20 20 20 20 69 66 28 20 70 54  nges;.    if( pT
24614 62 6c 4e 61 6d 65 21 3d 30 20 29 7b 0a 20 20 20  blName!=0 ){.   
24615 20 20 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 20     pIndex->tnum 
24616 3d 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e  = db->init.newTn
24617 75 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  um;.    }.  }.. 
24618 20 2f 2a 20 49 66 20 74 68 65 20 64 62 2d 3e 69   /* If the db->i
24619 6e 69 74 2e 62 75 73 79 20 69 73 20 30 20 74 68  nit.busy is 0 th
2461a 65 6e 20 63 72 65 61 74 65 20 74 68 65 20 69 6e  en create the in
2461b 64 65 78 20 6f 6e 20 64 69 73 6b 2e 20 20 54 68  dex on disk.  Th
2461c 69 73 0a 20 20 2a 2a 20 69 6e 76 6f 6c 76 65 73  is.  ** involves
2461d 20 77 72 69 74 69 6e 67 20 74 68 65 20 69 6e 64   writing the ind
2461e 65 78 20 69 6e 74 6f 20 74 68 65 20 6d 61 73 74  ex into the mast
2461f 65 72 20 74 61 62 6c 65 20 61 6e 64 20 66 69 6c  er table and fil
24620 6c 69 6e 67 20 69 6e 20 74 68 65 0a 20 20 2a 2a  ling in the.  **
24621 20 69 6e 64 65 78 20 77 69 74 68 20 74 68 65 20   index with the 
24622 63 75 72 72 65 6e 74 20 74 61 62 6c 65 20 63 6f  current table co
24623 6e 74 65 6e 74 73 2e 0a 20 20 2a 2a 0a 20 20 2a  ntents..  **.  *
24624 2a 20 54 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62  * The db->init.b
24625 75 73 79 20 69 73 20 30 20 77 68 65 6e 20 74 68  usy is 0 when th
24626 65 20 75 73 65 72 20 66 69 72 73 74 20 65 6e 74  e user first ent
24627 65 72 73 20 61 20 43 52 45 41 54 45 20 49 4e 44  ers a CREATE IND
24628 45 58 20 0a 20 20 2a 2a 20 63 6f 6d 6d 61 6e 64  EX .  ** command
24629 2e 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  .  db->init.busy
2462a 20 69 73 20 31 20 77 68 65 6e 20 61 20 64 61 74   is 1 when a dat
2462b 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20  abase is opened 
2462c 61 6e 64 20 0a 20 20 2a 2a 20 43 52 45 41 54 45  and .  ** CREATE
2462d 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74   INDEX statement
2462e 73 20 61 72 65 20 72 65 61 64 20 6f 75 74 20 6f  s are read out o
2462f 66 20 74 68 65 20 6d 61 73 74 65 72 20 74 61 62  f the master tab
24630 6c 65 2e 20 20 49 6e 0a 20 20 2a 2a 20 74 68 65  le.  In.  ** the
24631 20 6c 61 74 74 65 72 20 63 61 73 65 20 74 68 65   latter case the
24632 20 69 6e 64 65 78 20 61 6c 72 65 61 64 79 20 65   index already e
24633 78 69 73 74 73 20 6f 6e 20 64 69 73 6b 2c 20 77  xists on disk, w
24634 68 69 63 68 20 69 73 20 77 68 79 0a 20 20 2a 2a  hich is why.  **
24635 20 77 65 20 64 6f 6e 27 74 20 77 61 6e 74 20 74   we don't want t
24636 6f 20 72 65 63 72 65 61 74 65 20 69 74 2e 0a 20  o recreate it.. 
24637 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 54 62 6c   **.  ** If pTbl
24638 4e 61 6d 65 3d 3d 30 20 69 74 20 6d 65 61 6e 73  Name==0 it means
24639 20 74 68 69 73 20 69 6e 64 65 78 20 69 73 20 67   this index is g
2463a 65 6e 65 72 61 74 65 64 20 61 73 20 61 20 70 72  enerated as a pr
2463b 69 6d 61 72 79 20 6b 65 79 0a 20 20 2a 2a 20 6f  imary key.  ** o
2463c 72 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61  r UNIQUE constra
2463d 69 6e 74 20 6f 66 20 61 20 43 52 45 41 54 45 20  int of a CREATE 
2463e 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e  TABLE statement.
2463f 20 20 53 69 6e 63 65 20 74 68 65 20 74 61 62 6c    Since the tabl
24640 65 0a 20 20 2a 2a 20 68 61 73 20 6a 75 73 74 20  e.  ** has just 
24641 62 65 65 6e 20 63 72 65 61 74 65 64 2c 20 69 74  been created, it
24642 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 64 61 74   contains no dat
24643 61 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78 20  a and the index 
24644 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 20  initialization. 
24645 20 2a 2a 20 73 74 65 70 20 63 61 6e 20 62 65 20   ** step can be 
24646 73 6b 69 70 70 65 64 2e 0a 20 20 2a 2f 0a 20 20  skipped..  */.  
24647 65 6c 73 65 7b 20 2f 2a 20 69 66 28 20 64 62 2d  else{ /* if( db-
24648 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29 20  >init.busy==0 ) 
24649 2a 2f 0a 20 20 20 20 56 64 62 65 20 2a 76 3b 0a  */.    Vdbe *v;.
2464a 20 20 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b      char *zStmt;
2464b 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d 20  .    int iMem = 
2464c 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2464d 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33  .    v = sqlite3
2464e 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
2464f 0a 20 20 20 20 69 66 28 20 76 3d 3d 30 20 29 20  .    if( v==0 ) 
24650 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65  goto exit_create
24651 5f 69 6e 64 65 78 3b 0a 0a 0a 20 20 20 20 2f 2a  _index;...    /*
24652 20 43 72 65 61 74 65 20 74 68 65 20 72 6f 6f 74   Create the root
24653 70 61 67 65 20 66 6f 72 20 74 68 65 20 69 6e 64  page for the ind
24654 65 78 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  ex.    */.    sq
24655 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
24656 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c  peration(pParse,
24657 20 31 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71   1, iDb);.    sq
24658 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
24659 76 2c 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65  v, OP_CreateInde
2465a 78 2c 20 69 44 62 2c 20 69 4d 65 6d 29 3b 0a 0a  x, iDb, iMem);..
2465b 20 20 20 20 2f 2a 20 47 61 74 68 65 72 20 74 68      /* Gather th
2465c 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20  e complete text 
2465d 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 49 4e  of the CREATE IN
2465e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 20 69 6e  DEX statement in
2465f 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 7a 53  to.    ** the zS
24660 74 6d 74 20 76 61 72 69 61 62 6c 65 0a 20 20 20  tmt variable.   
24661 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 53 74 61   */.    if( pSta
24662 72 74 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  rt ){.      asse
24663 72 74 28 20 70 45 6e 64 21 3d 30 20 29 3b 0a 20  rt( pEnd!=0 );. 
24664 20 20 20 20 20 2f 2a 20 41 20 6e 61 6d 65 64 20       /* A named 
24665 69 6e 64 65 78 20 77 69 74 68 20 61 6e 20 65 78  index with an ex
24666 70 6c 69 63 69 74 20 43 52 45 41 54 45 20 49 4e  plicit CREATE IN
24667 44 45 58 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  DEX statement */
24668 0a 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 73  .      zStmt = s
24669 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
2466a 2c 20 22 43 52 45 41 54 45 25 73 20 49 4e 44 45  , "CREATE%s INDE
2466b 58 20 25 2e 2a 73 22 2c 0a 20 20 20 20 20 20 20  X %.*s",.       
2466c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e   onError==OE_Non
2466d 65 20 3f 20 22 22 20 3a 20 22 20 55 4e 49 51 55  e ? "" : " UNIQU
2466e 45 22 2c 0a 20 20 20 20 20 20 20 20 70 45 6e 64  E",.        pEnd
2466f 2d 3e 7a 20 2d 20 70 4e 61 6d 65 2d 3e 7a 20 2b  ->z - pName->z +
24670 20 31 2c 0a 20 20 20 20 20 20 20 20 70 4e 61 6d   1,.        pNam
24671 65 2d 3e 7a 29 3b 0a 20 20 20 20 7d 65 6c 73 65  e->z);.    }else
24672 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6e 20 61 75  {.      /* An au
24673 74 6f 6d 61 74 69 63 20 69 6e 64 65 78 20 63 72  tomatic index cr
24674 65 61 74 65 64 20 62 79 20 61 20 50 52 49 4d 41  eated by a PRIMA
24675 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45  RY KEY or UNIQUE
24676 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20   constraint */. 
24677 20 20 20 20 20 2f 2a 20 7a 53 74 6d 74 20 3d 20       /* zStmt = 
24678 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 22  sqlite3MPrintf("
24679 22 29 3b 20 2a 2f 0a 20 20 20 20 20 20 7a 53 74  "); */.      zSt
2467a 6d 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20  mt = 0;.    }.. 
2467b 20 20 20 2f 2a 20 41 64 64 20 61 6e 20 65 6e 74     /* Add an ent
2467c 72 79 20 69 6e 20 73 71 6c 69 74 65 5f 6d 61 73  ry in sqlite_mas
2467d 74 65 72 20 66 6f 72 20 74 68 69 73 20 69 6e 64  ter for this ind
2467e 65 78 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  ex.    */.    sq
2467f 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
24680 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20  (pParse, .      
24681 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 25    "INSERT INTO %
24682 51 2e 25 73 20 56 41 4c 55 45 53 28 27 69 6e 64  Q.%s VALUES('ind
24683 65 78 27 2c 25 51 2c 25 51 2c 23 25 64 2c 25 51  ex',%Q,%Q,#%d,%Q
24684 29 3b 22 2c 0a 20 20 20 20 20 20 20 20 64 62 2d  );",.        db-
24685 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c  >aDb[iDb].zName,
24686 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44   SCHEMA_TABLE(iD
24687 62 29 2c 0a 20 20 20 20 20 20 20 20 70 49 6e 64  b),.        pInd
24688 65 78 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20  ex->zName,.     
24689 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0a     pTab->zName,.
2468a 20 20 20 20 20 20 20 20 69 4d 65 6d 2c 0a 20 20          iMem,.  
2468b 20 20 20 20 20 20 7a 53 74 6d 74 0a 20 20 20 20        zStmt.    
2468c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
2468d 46 72 65 65 28 64 62 2c 20 7a 53 74 6d 74 29 3b  Free(db, zStmt);
2468e 0a 0a 20 20 20 20 2f 2a 20 46 69 6c 6c 20 74 68  ..    /* Fill th
2468f 65 20 69 6e 64 65 78 20 77 69 74 68 20 64 61 74  e index with dat
24690 61 20 61 6e 64 20 72 65 70 61 72 73 65 20 74 68  a and reparse th
24691 65 20 73 63 68 65 6d 61 2e 20 43 6f 64 65 20 61  e schema. Code a
24692 6e 20 4f 50 5f 45 78 70 69 72 65 0a 20 20 20 20  n OP_Expire.    
24693 2a 2a 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65  ** to invalidate
24694 20 61 6c 6c 20 70 72 65 2d 63 6f 6d 70 69 6c 65   all pre-compile
24695 64 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20  d statements..  
24696 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 54 62    */.    if( pTb
24697 6c 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 73  lName ){.      s
24698 71 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 65  qlite3RefillInde
24699 78 28 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78  x(pParse, pIndex
2469a 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 73  , iMem);.      s
2469b 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b  qlite3ChangeCook
2469c 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b  ie(pParse, iDb);
2469d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2469e 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50  beAddOp4(v, OP_P
2469f 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44 62 2c  arseSchema, iDb,
246a0 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20   0, 0,.         
246a1 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
246a2 62 2c 20 22 6e 61 6d 65 3d 27 25 71 27 22 2c 20  b, "name='%q'", 
246a3 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 29 2c 20  pIndex->zName), 
246a4 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20  P4_DYNAMIC);.   
246a5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
246a6 64 4f 70 31 28 76 2c 20 4f 50 5f 45 78 70 69 72  dOp1(v, OP_Expir
246a7 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  e, 0);.    }.  }
246a8 0a 0a 20 20 2f 2a 20 57 68 65 6e 20 61 64 64 69  ..  /* When addi
246a9 6e 67 20 61 6e 20 69 6e 64 65 78 20 74 6f 20 74  ng an index to t
246aa 68 65 20 6c 69 73 74 20 6f 66 20 69 6e 64 69 63  he list of indic
246ab 65 73 20 66 6f 72 20 61 20 74 61 62 6c 65 2c 20  es for a table, 
246ac 6d 61 6b 65 0a 20 20 2a 2a 20 73 75 72 65 20 61  make.  ** sure a
246ad 6c 6c 20 69 6e 64 69 63 65 73 20 6c 61 62 65 6c  ll indices label
246ae 65 64 20 4f 45 5f 52 65 70 6c 61 63 65 20 63 6f  ed OE_Replace co
246af 6d 65 20 61 66 74 65 72 20 61 6c 6c 20 74 68 6f  me after all tho
246b0 73 65 20 6c 61 62 65 6c 65 64 0a 20 20 2a 2a 20  se labeled.  ** 
246b1 4f 45 5f 49 67 6e 6f 72 65 2e 20 20 54 68 69 73  OE_Ignore.  This
246b2 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 66 6f   is necessary fo
246b3 72 20 74 68 65 20 63 6f 72 72 65 63 74 20 63 6f  r the correct co
246b4 6e 73 74 72 61 69 6e 74 20 63 68 65 63 6b 0a 20  nstraint check. 
246b5 20 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 28   ** processing (
246b6 69 6e 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61  in sqlite3Genera
246b7 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63  teConstraintChec
246b8 6b 73 28 29 29 20 61 73 20 70 61 72 74 20 6f 66  ks()) as part of
246b9 0a 20 20 2a 2a 20 55 50 44 41 54 45 20 61 6e 64  .  ** UPDATE and
246ba 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e   INSERT statemen
246bb 74 73 2e 20 20 0a 20 20 2a 2f 0a 20 20 69 66 28  ts.  .  */.  if(
246bc 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c   db->init.busy |
246bd 7c 20 70 54 62 6c 4e 61 6d 65 3d 3d 30 20 29 7b  | pTblName==0 ){
246be 0a 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72  .    if( onError
246bf 21 3d 4f 45 5f 52 65 70 6c 61 63 65 20 7c 7c 20  !=OE_Replace || 
246c0 70 54 61 62 2d 3e 70 49 6e 64 65 78 3d 3d 30 0a  pTab->pIndex==0.
246c1 20 20 20 20 20 20 20 20 20 7c 7c 20 70 54 61 62           || pTab
246c2 2d 3e 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f  ->pIndex->onErro
246c3 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 29 7b 0a  r==OE_Replace){.
246c4 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 4e        pIndex->pN
246c5 65 78 74 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64  ext = pTab->pInd
246c6 65 78 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e  ex;.      pTab->
246c7 70 49 6e 64 65 78 20 3d 20 70 49 6e 64 65 78 3b  pIndex = pIndex;
246c8 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
246c9 20 20 49 6e 64 65 78 20 2a 70 4f 74 68 65 72 20    Index *pOther 
246ca 3d 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 0a  = pTab->pIndex;.
246cb 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 4f 74        while( pOt
246cc 68 65 72 2d 3e 70 4e 65 78 74 20 26 26 20 70 4f  her->pNext && pO
246cd 74 68 65 72 2d 3e 70 4e 65 78 74 2d 3e 6f 6e 45  ther->pNext->onE
246ce 72 72 6f 72 21 3d 4f 45 5f 52 65 70 6c 61 63 65  rror!=OE_Replace
246cf 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4f 74 68   ){.        pOth
246d0 65 72 20 3d 20 70 4f 74 68 65 72 2d 3e 70 4e 65  er = pOther->pNe
246d1 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  xt;.      }.    
246d2 20 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 20    pIndex->pNext 
246d3 3d 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 3b  = pOther->pNext;
246d4 0a 20 20 20 20 20 20 70 4f 74 68 65 72 2d 3e 70  .      pOther->p
246d5 4e 65 78 74 20 3d 20 70 49 6e 64 65 78 3b 0a 20  Next = pIndex;. 
246d6 20 20 20 7d 0a 20 20 20 20 70 49 6e 64 65 78 20     }.    pIndex 
246d7 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  = 0;.  }..  /* C
246d8 6c 65 61 6e 20 75 70 20 62 65 66 6f 72 65 20 65  lean up before e
246d9 78 69 74 69 6e 67 20 2a 2f 0a 65 78 69 74 5f 63  xiting */.exit_c
246da 72 65 61 74 65 5f 69 6e 64 65 78 3a 0a 20 20 69  reate_index:.  i
246db 66 28 20 70 49 6e 64 65 78 20 29 7b 0a 20 20 20  f( pIndex ){.   
246dc 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 49   sqlite3_free(pI
246dd 6e 64 65 78 2d 3e 7a 43 6f 6c 41 66 66 29 3b 0a  ndex->zColAff);.
246de 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
246df 65 28 64 62 2c 20 70 49 6e 64 65 78 29 3b 0a 20  e(db, pIndex);. 
246e0 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72   }.  sqlite3Expr
246e1 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
246e2 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  List);.  sqlite3
246e3 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62  SrcListDelete(db
246e4 2c 20 70 54 62 6c 4e 61 6d 65 29 3b 0a 20 20 73  , pTblName);.  s
246e5 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
246e6 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72   zName);.  retur
246e7 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c 6c  n;.}../*.** Fill
246e8 20 74 68 65 20 49 6e 64 65 78 2e 61 69 52 6f 77   the Index.aiRow
246e9 45 73 74 5b 5d 20 61 72 72 61 79 20 77 69 74 68  Est[] array with
246ea 20 64 65 66 61 75 6c 74 20 69 6e 66 6f 72 6d 61   default informa
246eb 74 69 6f 6e 20 2d 20 69 6e 66 6f 72 6d 61 74 69  tion - informati
246ec 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 75 73 65 64  on.** to be used
246ed 20 77 68 65 6e 20 77 65 20 68 61 76 65 20 6e 6f   when we have no
246ee 74 20 72 75 6e 20 74 68 65 20 41 4e 41 4c 59 5a  t run the ANALYZ
246ef 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a  E command..**.**
246f0 20 61 69 52 6f 77 45 73 74 5b 30 5d 20 69 73 20   aiRowEst[0] is 
246f1 73 75 70 70 6f 73 65 20 74 6f 20 63 6f 6e 74 61  suppose to conta
246f2 69 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  in the number of
246f3 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65   elements in the
246f4 20 69 6e 64 65 78 2e 0a 2a 2a 20 53 69 6e 63 65   index..** Since
246f5 20 77 65 20 64 6f 20 6e 6f 74 20 6b 6e 6f 77 2c   we do not know,
246f6 20 67 75 65 73 73 20 31 20 6d 69 6c 6c 69 6f 6e   guess 1 million
246f7 2e 20 20 61 69 52 6f 77 45 73 74 5b 31 5d 20 69  .  aiRowEst[1] i
246f8 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66  s an estimate of
246f9 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f   the.** number o
246fa 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61  f rows in the ta
246fb 62 6c 65 20 74 68 61 74 20 6d 61 74 63 68 20 61  ble that match a
246fc 6e 79 20 70 61 72 74 69 63 75 6c 61 72 20 76 61  ny particular va
246fd 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69  lue of the.** fi
246fe 72 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  rst column of th
246ff 65 20 69 6e 64 65 78 2e 20 20 61 69 52 6f 77 45  e index.  aiRowE
24700 73 74 5b 32 5d 20 69 73 20 61 6e 20 65 73 74 69  st[2] is an esti
24701 6d 61 74 65 20 6f 66 20 74 68 65 20 6e 75 6d 62  mate of the numb
24702 65 72 0a 2a 2a 20 6f 66 20 72 6f 77 73 20 74 68  er.** of rows th
24703 61 74 20 6d 61 74 63 68 20 61 6e 79 20 70 61 72  at match any par
24704 74 69 63 75 6c 61 72 20 63 6f 6d 62 69 6e 69 61  ticular combinia
24705 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 69 72 73  tion of the firs
24706 74 20 32 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 6f  t 2 columns.** o
24707 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20 41 6e  f the index.  An
24708 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 49 74 20  d so forth.  It 
24709 6d 75 73 74 20 61 6c 77 61 79 73 20 62 65 20 74  must always be t
2470a 68 65 20 63 61 73 65 20 74 68 61 74 0a 2a 0a 2a  he case that.*.*
2470b 2a 20 20 20 20 20 20 20 20 20 20 20 61 69 52 6f  *           aiRo
2470c 77 45 73 74 5b 4e 5d 3c 3d 61 69 52 6f 77 45 73  wEst[N]<=aiRowEs
2470d 74 5b 4e 2d 31 5d 0a 2a 2a 20 20 20 20 20 20 20  t[N-1].**       
2470e 20 20 20 20 61 69 52 6f 77 45 73 74 5b 4e 5d 3e      aiRowEst[N]>
2470f 3d 31 0a 2a 2a 0a 2a 2a 20 41 70 61 72 74 20 66  =1.**.** Apart f
24710 72 6f 6d 20 74 68 61 74 2c 20 77 65 20 68 61 76  rom that, we hav
24711 65 20 6c 69 74 74 6c 65 20 74 6f 20 67 6f 20 6f  e little to go o
24712 6e 20 62 65 73 69 64 65 73 20 69 6e 74 75 69 74  n besides intuit
24713 69 6f 6e 20 61 73 20 74 6f 0a 2a 2a 20 68 6f 77  ion as to.** how
24714 20 61 69 52 6f 77 45 73 74 5b 5d 20 73 68 6f 75   aiRowEst[] shou
24715 6c 64 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65  ld be initialize
24716 64 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 73 20  d.  The numbers 
24717 67 65 6e 65 72 61 74 65 64 20 68 65 72 65 0a 2a  generated here.*
24718 2a 20 61 72 65 20 62 61 73 65 64 20 6f 6e 20 74  * are based on t
24719 79 70 69 63 61 6c 20 76 61 6c 75 65 73 20 66 6f  ypical values fo
2471a 75 6e 64 20 69 6e 20 61 63 74 75 61 6c 20 69 6e  und in actual in
2471b 64 69 63 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  dices..*/.SQLITE
2471c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2471d 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45  lite3DefaultRowE
2471e 73 74 28 49 6e 64 65 78 20 2a 70 49 64 78 29 7b  st(Index *pIdx){
2471f 0a 20 20 75 6e 73 69 67 6e 65 64 20 2a 61 20 3d  .  unsigned *a =
24720 20 70 49 64 78 2d 3e 61 69 52 6f 77 45 73 74 3b   pIdx->aiRowEst;
24721 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65  .  int i;.  asse
24722 72 74 28 20 61 21 3d 30 20 29 3b 0a 20 20 61 5b  rt( a!=0 );.  a[
24723 30 5d 20 3d 20 31 30 30 30 30 30 30 3b 0a 20 20  0] = 1000000;.  
24724 66 6f 72 28 69 3d 70 49 64 78 2d 3e 6e 43 6f 6c  for(i=pIdx->nCol
24725 75 6d 6e 3b 20 69 3e 3d 35 3b 20 69 2d 2d 29 7b  umn; i>=5; i--){
24726 0a 20 20 20 20 61 5b 69 5d 20 3d 20 35 3b 0a 20  .    a[i] = 5;. 
24727 20 7d 0a 20 20 77 68 69 6c 65 28 20 69 3e 3d 31   }.  while( i>=1
24728 20 29 7b 0a 20 20 20 20 61 5b 69 5d 20 3d 20 31   ){.    a[i] = 1
24729 31 20 2d 20 69 3b 0a 20 20 20 20 69 2d 2d 3b 0a  1 - i;.    i--;.
2472a 20 20 7d 0a 20 20 69 66 28 20 70 49 64 78 2d 3e    }.  if( pIdx->
2472b 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65  onError!=OE_None
2472c 20 29 7b 0a 20 20 20 20 61 5b 70 49 64 78 2d 3e   ){.    a[pIdx->
2472d 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 31 3b 0a 20 20  nColumn] = 1;.  
2472e 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
2472f 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64 72 6f  routine will dro
24730 70 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6e 61  p an existing na
24731 6d 65 64 20 69 6e 64 65 78 2e 20 20 54 68 69 73  med index.  This
24732 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 6d 70 6c   routine.** impl
24733 65 6d 65 6e 74 73 20 74 68 65 20 44 52 4f 50 20  ements the DROP 
24734 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 2e  INDEX statement.
24735 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
24736 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
24737 72 6f 70 49 6e 64 65 78 28 50 61 72 73 65 20 2a  ropIndex(Parse *
24738 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20  pParse, SrcList 
24739 2a 70 4e 61 6d 65 2c 20 69 6e 74 20 69 66 45 78  *pName, int ifEx
2473a 69 73 74 73 29 7b 0a 20 20 49 6e 64 65 78 20 2a  ists){.  Index *
2473b 70 49 6e 64 65 78 3b 0a 20 20 56 64 62 65 20 2a  pIndex;.  Vdbe *
2473c 76 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  v;.  sqlite3 *db
2473d 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
2473e 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20 61 73 73   int iDb;..  ass
2473f 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 45 72  ert( pParse->nEr
24740 72 3d 3d 30 20 29 3b 20 20 20 2f 2a 20 4e 65 76  r==0 );   /* Nev
24741 65 72 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70  er called with p
24742 72 69 6f 72 20 65 72 72 6f 72 73 20 2a 2f 0a 20  rior errors */. 
24743 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
24744 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74  ailed ){.    got
24745 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65  o exit_drop_inde
24746 78 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  x;.  }.  assert(
24747 20 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20   pName->nSrc==1 
24748 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  );.  if( SQLITE_
24749 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53  OK!=sqlite3ReadS
2474a 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b  chema(pParse) ){
2474b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64  .    goto exit_d
2474c 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20  rop_index;.  }. 
2474d 20 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65   pIndex = sqlite
2474e 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 70  3FindIndex(db, p
2474f 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65  Name->a[0].zName
24750 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44  , pName->a[0].zD
24751 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20  atabase);.  if( 
24752 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20  pIndex==0 ){.   
24753 20 69 66 28 20 21 69 66 45 78 69 73 74 73 20 29   if( !ifExists )
24754 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
24755 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
24756 22 6e 6f 20 73 75 63 68 20 69 6e 64 65 78 3a 20  "no such index: 
24757 25 53 22 2c 20 70 4e 61 6d 65 2c 20 30 29 3b 0a  %S", pName, 0);.
24758 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65      }.    pParse
24759 2d 3e 63 68 65 63 6b 53 63 68 65 6d 61 20 3d 20  ->checkSchema = 
2475a 31 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  1;.    goto exit
2475b 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d  _drop_index;.  }
2475c 0a 20 20 69 66 28 20 70 49 6e 64 65 78 2d 3e 61  .  if( pIndex->a
2475d 75 74 6f 49 6e 64 65 78 20 29 7b 0a 20 20 20 20  utoIndex ){.    
2475e 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2475f 70 50 61 72 73 65 2c 20 22 69 6e 64 65 78 20 61  pParse, "index a
24760 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 55  ssociated with U
24761 4e 49 51 55 45 20 22 0a 20 20 20 20 20 20 22 6f  NIQUE ".      "o
24762 72 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f  r PRIMARY KEY co
24763 6e 73 74 72 61 69 6e 74 20 63 61 6e 6e 6f 74 20  nstraint cannot 
24764 62 65 20 64 72 6f 70 70 65 64 22 2c 20 30 29 3b  be dropped", 0);
24765 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64  .    goto exit_d
24766 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20  rop_index;.  }. 
24767 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
24768 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
24769 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 29  pIndex->pSchema)
2476a 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
2476b 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
2476c 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20  ION.  {.    int 
2476d 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52  code = SQLITE_DR
2476e 4f 50 5f 49 4e 44 45 58 3b 0a 20 20 20 20 54 61  OP_INDEX;.    Ta
2476f 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 49 6e 64  ble *pTab = pInd
24770 65 78 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 20 20  ex->pTable;.    
24771 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20  const char *zDb 
24772 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
24773 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20  Name;.    const 
24774 63 68 61 72 20 2a 7a 54 61 62 20 3d 20 53 43 48  char *zTab = SCH
24775 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a  EMA_TABLE(iDb);.
24776 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41      if( sqlite3A
24777 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
24778 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20   SQLITE_DELETE, 
24779 7a 54 61 62 2c 20 30 2c 20 7a 44 62 29 20 29 7b  zTab, 0, zDb) ){
2477a 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
2477b 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 20  _drop_index;.   
2477c 20 7d 0a 20 20 20 20 69 66 28 20 21 4f 4d 49 54   }.    if( !OMIT
2477d 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 20 29  _TEMPDB && iDb )
2477e 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44   code = SQLITE_D
2477f 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 3b 0a  ROP_TEMP_INDEX;.
24780 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41      if( sqlite3A
24781 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
24782 20 63 6f 64 65 2c 20 70 49 6e 64 65 78 2d 3e 7a   code, pIndex->z
24783 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  Name, pTab->zNam
24784 65 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20  e, zDb) ){.     
24785 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f   goto exit_drop_
24786 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d  index;.    }.  }
24787 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 65  .#endif..  /* Ge
24788 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 72  nerate code to r
24789 65 6d 6f 76 65 20 74 68 65 20 69 6e 64 65 78 20  emove the index 
2478a 61 6e 64 20 66 72 6f 6d 20 74 68 65 20 6d 61 73  and from the mas
2478b 74 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76  ter table */.  v
2478c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
2478d 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
2478e 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65   v ){.    sqlite
2478f 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
24790 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20  tion(pParse, 1, 
24791 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  iDb);.    sqlite
24792 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61  3NestedParse(pPa
24793 72 73 65 2c 0a 20 20 20 20 20 20 20 22 44 45 4c  rse,.       "DEL
24794 45 54 45 20 46 52 4f 4d 20 25 51 2e 25 73 20 57  ETE FROM %Q.%s W
24795 48 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c 0a 20  HERE name=%Q",. 
24796 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44        db->aDb[iD
24797 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41  b].zName, SCHEMA
24798 5f 54 41 42 4c 45 28 69 44 62 29 2c 0a 20 20 20  _TABLE(iDb),.   
24799 20 20 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d      pIndex->zNam
2479a 65 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28  e.    );.    if(
2479b 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
2479c 65 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 74  e(db, "sqlite_st
2479d 61 74 31 22 2c 20 64 62 2d 3e 61 44 62 5b 69 44  at1", db->aDb[iD
2479e 62 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20  b].zName) ){.   
2479f 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64     sqlite3Nested
247a0 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20  Parse(pParse,.  
247a1 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 52        "DELETE FR
247a2 4f 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61  OM %Q.sqlite_sta
247a3 74 31 20 57 48 45 52 45 20 69 64 78 3d 25 51 22  t1 WHERE idx=%Q"
247a4 2c 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 44  ,.        db->aD
247a5 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 70 49  b[iDb].zName, pI
247a6 6e 64 65 78 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20  ndex->zName.    
247a7 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73    );.    }.    s
247a8 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b  qlite3ChangeCook
247a9 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b  ie(pParse, iDb);
247aa 0a 20 20 20 20 64 65 73 74 72 6f 79 52 6f 6f 74  .    destroyRoot
247ab 50 61 67 65 28 70 50 61 72 73 65 2c 20 70 49 6e  Page(pParse, pIn
247ac 64 65 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b  dex->tnum, iDb);
247ad 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
247ae 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f  AddOp4(v, OP_Dro
247af 70 49 6e 64 65 78 2c 20 69 44 62 2c 20 30 2c 20  pIndex, iDb, 0, 
247b0 30 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65  0, pIndex->zName
247b1 2c 20 30 29 3b 0a 20 20 7d 0a 0a 65 78 69 74 5f  , 0);.  }..exit_
247b2 64 72 6f 70 5f 69 6e 64 65 78 3a 0a 20 20 73 71  drop_index:.  sq
247b3 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65  lite3SrcListDele
247b4 74 65 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 7d  te(db, pName);.}
247b5 0a 0a 2f 2a 0a 2a 2a 20 70 41 72 72 61 79 20 69  ../*.** pArray i
247b6 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
247b7 6e 20 61 72 72 61 79 20 6f 66 20 6f 62 6a 65 63  n array of objec
247b8 74 73 2e 20 20 45 61 63 68 20 6f 62 6a 65 63 74  ts.  Each object
247b9 20 69 6e 20 74 68 65 0a 2a 2a 20 61 72 72 61 79   in the.** array
247ba 20 69 73 20 73 7a 45 6e 74 72 79 20 62 79 74 65   is szEntry byte
247bb 73 20 69 6e 20 73 69 7a 65 2e 20 20 54 68 69 73  s in size.  This
247bc 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74   routine allocat
247bd 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6f 62 6a 65  es a new.** obje
247be 63 74 20 6f 6e 20 74 68 65 20 65 6e 64 20 6f 66  ct on the end of
247bf 20 74 68 65 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a   the array..**.*
247c0 2a 20 2a 70 6e 45 6e 74 72 79 20 69 73 20 74 68  * *pnEntry is th
247c1 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  e number of entr
247c2 69 65 73 20 61 6c 72 65 61 64 79 20 69 6e 20 75  ies already in u
247c3 73 65 2e 20 20 2a 70 6e 41 6c 6c 6f 63 20 69 73  se.  *pnAlloc is
247c4 0a 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73  .** the previous
247c5 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a  ly allocated siz
247c6 65 20 6f 66 20 74 68 65 20 61 72 72 61 79 2e 20  e of the array. 
247c7 20 69 6e 69 74 53 69 7a 65 20 69 73 20 74 68 65   initSize is the
247c8 0a 2a 2a 20 73 75 67 67 65 73 74 65 64 20 69 6e  .** suggested in
247c9 69 74 69 61 6c 20 61 72 72 61 79 20 73 69 7a 65  itial array size
247ca 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a   allocation..**.
247cb 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20  ** The index of 
247cc 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 69 73  the new entry is
247cd 20 72 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 49   returned in *pI
247ce 64 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  dx..**.** This r
247cf 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61  outine returns a
247d0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
247d1 61 72 72 61 79 20 6f 66 20 6f 62 6a 65 63 74 73  array of objects
247d2 2e 20 20 54 68 69 73 0a 2a 2a 20 6d 69 67 68 74  .  This.** might
247d3 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 20   be the same as 
247d4 74 68 65 20 70 41 72 72 61 79 20 70 61 72 61 6d  the pArray param
247d5 65 74 65 72 20 6f 72 20 69 74 20 6d 69 67 68 74  eter or it might
247d6 20 62 65 20 61 20 64 69 66 66 65 72 65 6e 74 0a   be a different.
247d7 2a 2a 20 70 6f 69 6e 74 65 72 20 69 66 20 74 68  ** pointer if th
247d8 65 20 61 72 72 61 79 20 77 61 73 20 72 65 73 69  e array was resi
247d9 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  zed..*/.SQLITE_P
247da 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
247db 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74  ite3ArrayAllocat
247dc 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e(.  sqlite3 *db
247dd 2c 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63  ,      /* Connec
247de 74 69 6f 6e 20 74 6f 20 6e 6f 74 69 66 79 20 6f  tion to notify o
247df 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  f malloc failure
247e0 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72  s */.  void *pAr
247e1 72 61 79 2c 20 20 20 20 20 2f 2a 20 41 72 72 61  ray,     /* Arra
247e2 79 20 6f 66 20 6f 62 6a 65 63 74 73 2e 20 20 4d  y of objects.  M
247e3 69 67 68 74 20 62 65 20 72 65 61 6c 6c 6f 63 61  ight be realloca
247e4 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45  ted */.  int szE
247e5 6e 74 72 79 2c 20 20 20 20 20 20 2f 2a 20 53 69  ntry,      /* Si
247e6 7a 65 20 6f 66 20 65 61 63 68 20 6f 62 6a 65 63  ze of each objec
247e7 74 20 69 6e 20 74 68 65 20 61 72 72 61 79 20 2a  t in the array *
247e8 2f 0a 20 20 69 6e 74 20 69 6e 69 74 53 69 7a 65  /.  int initSize
247e9 2c 20 20 20 20 20 2f 2a 20 53 75 67 67 65 73 74  ,     /* Suggest
247ea 65 64 20 69 6e 69 74 69 61 6c 20 61 6c 6c 6f 63  ed initial alloc
247eb 61 74 69 6f 6e 2c 20 69 6e 20 65 6c 65 6d 65 6e  ation, in elemen
247ec 74 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 45  ts */.  int *pnE
247ed 6e 74 72 79 2c 20 20 20 20 20 2f 2a 20 4e 75 6d  ntry,     /* Num
247ee 62 65 72 20 6f 66 20 6f 62 6a 65 63 74 73 20 63  ber of objects c
247ef 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 20  urrently in use 
247f0 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 41 6c 6c 6f  */.  int *pnAllo
247f1 63 2c 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e  c,     /* Curren
247f2 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 6c  t size of the al
247f3 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 65 6c 65  location, in ele
247f4 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 2a  ments */.  int *
247f5 70 49 64 78 20 20 20 20 20 20 20 20 20 2f 2a 20  pIdx         /* 
247f6 57 72 69 74 65 20 74 68 65 20 69 6e 64 65 78 20  Write the index 
247f7 6f 66 20 61 20 6e 65 77 20 73 6c 6f 74 20 68 65  of a new slot he
247f8 72 65 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20  re */.){.  char 
247f9 2a 7a 3b 0a 20 20 69 66 28 20 2a 70 6e 45 6e 74  *z;.  if( *pnEnt
247fa 72 79 20 3e 3d 20 2a 70 6e 41 6c 6c 6f 63 20 29  ry >= *pnAlloc )
247fb 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 4e 65 77  {.    void *pNew
247fc 3b 0a 20 20 20 20 69 6e 74 20 6e 65 77 53 69 7a  ;.    int newSiz
247fd 65 3b 0a 20 20 20 20 6e 65 77 53 69 7a 65 20 3d  e;.    newSize =
247fe 20 28 2a 70 6e 41 6c 6c 6f 63 29 2a 32 20 2b 20   (*pnAlloc)*2 + 
247ff 69 6e 69 74 53 69 7a 65 3b 0a 20 20 20 20 70 4e  initSize;.    pN
24800 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65  ew = sqlite3DbRe
24801 61 6c 6c 6f 63 28 64 62 2c 20 70 41 72 72 61 79  alloc(db, pArray
24802 2c 20 6e 65 77 53 69 7a 65 2a 73 7a 45 6e 74 72  , newSize*szEntr
24803 79 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77  y);.    if( pNew
24804 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 49  ==0 ){.      *pI
24805 64 78 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 72  dx = -1;.      r
24806 65 74 75 72 6e 20 70 41 72 72 61 79 3b 0a 20 20  eturn pArray;.  
24807 20 20 7d 0a 20 20 20 20 2a 70 6e 41 6c 6c 6f 63    }.    *pnAlloc
24808 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
24809 6f 63 53 69 7a 65 28 64 62 2c 20 70 4e 65 77 29  ocSize(db, pNew)
2480a 2f 73 7a 45 6e 74 72 79 3b 0a 20 20 20 20 70 41  /szEntry;.    pA
2480b 72 72 61 79 20 3d 20 70 4e 65 77 3b 0a 20 20 7d  rray = pNew;.  }
2480c 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 70 41  .  z = (char*)pA
2480d 72 72 61 79 3b 0a 20 20 6d 65 6d 73 65 74 28 26  rray;.  memset(&
2480e 7a 5b 2a 70 6e 45 6e 74 72 79 20 2a 20 73 7a 45  z[*pnEntry * szE
2480f 6e 74 72 79 5d 2c 20 30 2c 20 73 7a 45 6e 74 72  ntry], 0, szEntr
24810 79 29 3b 0a 20 20 2a 70 49 64 78 20 3d 20 2a 70  y);.  *pIdx = *p
24811 6e 45 6e 74 72 79 3b 0a 20 20 2b 2b 2a 70 6e 45  nEntry;.  ++*pnE
24812 6e 74 72 79 3b 0a 20 20 72 65 74 75 72 6e 20 70  ntry;.  return p
24813 41 72 72 61 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Array;.}../*.** 
24814 41 70 70 65 6e 64 20 61 20 6e 65 77 20 65 6c 65  Append a new ele
24815 6d 65 6e 74 20 74 6f 20 74 68 65 20 67 69 76 65  ment to the give
24816 6e 20 49 64 4c 69 73 74 2e 20 20 43 72 65 61 74  n IdList.  Creat
24817 65 20 61 20 6e 65 77 20 49 64 4c 69 73 74 20 69  e a new IdList i
24818 66 0a 2a 2a 20 6e 65 65 64 20 62 65 2e 0a 2a 2a  f.** need be..**
24819 0a 2a 2a 20 41 20 6e 65 77 20 49 64 4c 69 73 74  .** A new IdList
2481a 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 72   is returned, or
2481b 20 4e 55 4c 4c 20 69 66 20 6d 61 6c 6c 6f 63 28   NULL if malloc(
2481c 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49  ) fails..*/.SQLI
2481d 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73  TE_PRIVATE IdLis
2481e 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74  t *sqlite3IdList
2481f 41 70 70 65 6e 64 28 73 71 6c 69 74 65 33 20 2a  Append(sqlite3 *
24820 64 62 2c 20 49 64 4c 69 73 74 20 2a 70 4c 69 73  db, IdList *pLis
24821 74 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e  t, Token *pToken
24822 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66  ){.  int i;.  if
24823 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20  ( pList==0 ){.  
24824 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65    pList = sqlite
24825 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
24826 2c 20 73 69 7a 65 6f 66 28 49 64 4c 69 73 74 29  , sizeof(IdList)
24827 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73   );.    if( pLis
24828 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  t==0 ) return 0;
24829 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c  .    pList->nAll
2482a 6f 63 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4c  oc = 0;.  }.  pL
2482b 69 73 74 2d 3e 61 20 3d 20 73 71 6c 69 74 65 33  ist->a = sqlite3
2482c 41 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20  ArrayAllocate(. 
2482d 20 20 20 20 20 64 62 2c 0a 20 20 20 20 20 20 70       db,.      p
2482e 4c 69 73 74 2d 3e 61 2c 0a 20 20 20 20 20 20 73  List->a,.      s
2482f 69 7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30  izeof(pList->a[0
24830 5d 29 2c 0a 20 20 20 20 20 20 35 2c 0a 20 20 20  ]),.      5,.   
24831 20 20 20 26 70 4c 69 73 74 2d 3e 6e 49 64 2c 0a     &pList->nId,.
24832 20 20 20 20 20 20 26 70 4c 69 73 74 2d 3e 6e 41        &pList->nA
24833 6c 6c 6f 63 2c 0a 20 20 20 20 20 20 26 69 0a 20  lloc,.      &i. 
24834 20 29 3b 0a 20 20 69 66 28 20 69 3c 30 20 29 7b   );.  if( i<0 ){
24835 0a 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69  .    sqlite3IdLi
24836 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69  stDelete(db, pLi
24837 73 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  st);.    return 
24838 30 3b 0a 20 20 7d 0a 20 20 70 4c 69 73 74 2d 3e  0;.  }.  pList->
24839 61 5b 69 5d 2e 7a 4e 61 6d 65 20 3d 20 73 71 6c  a[i].zName = sql
2483a 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
2483b 6e 28 64 62 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20  n(db, pToken);. 
2483c 20 72 65 74 75 72 6e 20 70 4c 69 73 74 3b 0a 7d   return pList;.}
2483d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61  ../*.** Delete a
2483e 6e 20 49 64 4c 69 73 74 2e 0a 2a 2f 0a 53 51 4c  n IdList..*/.SQL
2483f 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
24840 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65   sqlite3IdListDe
24841 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62  lete(sqlite3 *db
24842 2c 20 49 64 4c 69 73 74 20 2a 70 4c 69 73 74 29  , IdList *pList)
24843 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28  {.  int i;.  if(
24844 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75   pList==0 ) retu
24845 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  rn;.  for(i=0; i
24846 3c 70 4c 69 73 74 2d 3e 6e 49 64 3b 20 69 2b 2b  <pList->nId; i++
24847 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  ){.    sqlite3Db
24848 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74 2d 3e  Free(db, pList->
24849 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 7d  a[i].zName);.  }
2484a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
2484b 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 29 3b 0a  (db, pList->a);.
2484c 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2484d 64 62 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f  db, pList);.}../
2484e 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
2484f 69 6e 64 65 78 20 69 6e 20 70 4c 69 73 74 20 6f  index in pList o
24850 66 20 74 68 65 20 69 64 65 6e 74 69 66 69 65 72  f the identifier
24851 20 6e 61 6d 65 64 20 7a 49 64 2e 20 20 52 65 74   named zId.  Ret
24852 75 72 6e 20 2d 31 0a 2a 2a 20 69 66 20 6e 6f 74  urn -1.** if not
24853 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54   found..*/.SQLIT
24854 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
24855 6c 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78  lite3IdListIndex
24856 28 49 64 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20  (IdList *pList, 
24857 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
24858 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  e){.  int i;.  i
24859 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65  f( pList==0 ) re
2485a 74 75 72 6e 20 2d 31 3b 0a 20 20 66 6f 72 28 69  turn -1;.  for(i
2485b 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 49 64  =0; i<pList->nId
2485c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
2485d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
2485e 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  List->a[i].zName
2485f 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 72 65  , zName)==0 ) re
24860 74 75 72 6e 20 69 3b 0a 20 20 7d 0a 20 20 72 65  turn i;.  }.  re
24861 74 75 72 6e 20 2d 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn -1;.}../*.*
24862 2a 20 45 78 70 61 6e 64 20 74 68 65 20 73 70 61  * Expand the spa
24863 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72  ce allocated for
24864 20 74 68 65 20 67 69 76 65 6e 20 53 72 63 4c 69   the given SrcLi
24865 73 74 20 6f 62 6a 65 63 74 20 62 79 0a 2a 2a 20  st object by.** 
24866 63 72 65 61 74 69 6e 67 20 6e 45 78 74 72 61 20  creating nExtra 
24867 6e 65 77 20 73 6c 6f 74 73 20 62 65 67 69 6e 6e  new slots beginn
24868 69 6e 67 20 61 74 20 69 53 74 61 72 74 2e 20 20  ing at iStart.  
24869 69 53 74 61 72 74 20 69 73 20 7a 65 72 6f 20 62  iStart is zero b
2486a 61 73 65 64 2e 0a 2a 2a 20 4e 65 77 20 73 6c 6f  ased..** New slo
2486b 74 73 20 61 72 65 20 7a 65 72 6f 65 64 2e 0a 2a  ts are zeroed..*
2486c 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65  *.** For example
2486d 2c 20 73 75 70 70 6f 73 65 20 61 20 53 72 63 4c  , suppose a SrcL
2486e 69 73 74 20 69 6e 69 74 69 61 6c 6c 79 20 63 6f  ist initially co
2486f 6e 74 61 69 6e 73 20 74 77 6f 20 65 6e 74 72 69  ntains two entri
24870 65 73 3a 20 41 2c 42 2e 0a 2a 2a 20 54 6f 20 61  es: A,B..** To a
24871 70 70 65 6e 64 20 33 20 6e 65 77 20 65 6e 74 72  ppend 3 new entr
24872 69 65 73 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64  ies onto the end
24873 2c 20 64 6f 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a  , do this:.**.**
24874 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69      sqlite3SrcLi
24875 73 74 45 6e 6c 61 72 67 65 28 64 62 2c 20 70 53  stEnlarge(db, pS
24876 72 63 6c 69 73 74 2c 20 33 2c 20 32 29 3b 0a 2a  rclist, 3, 2);.*
24877 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 20 63  *.** After the c
24878 61 6c 6c 20 61 62 6f 76 65 20 69 74 20 77 6f 75  all above it wou
24879 6c 64 20 63 6f 6e 74 61 69 6e 3a 20 20 41 2c 20  ld contain:  A, 
2487a 42 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 6e 69 6c  B, nil, nil, nil
2487b 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69 53 74 61  ..** If the iSta
2487c 72 74 20 61 72 67 75 6d 65 6e 74 20 68 61 64 20  rt argument had 
2487d 62 65 65 6e 20 31 20 69 6e 73 74 65 61 64 20 6f  been 1 instead o
2487e 66 20 32 2c 20 74 68 65 6e 20 74 68 65 20 72 65  f 2, then the re
2487f 73 75 6c 74 0a 2a 2a 20 77 6f 75 6c 64 20 68 61  sult.** would ha
24880 76 65 20 62 65 65 6e 3a 20 20 41 2c 20 6e 69 6c  ve been:  A, nil
24881 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 42 2e 20 20  , nil, nil, B.  
24882 54 6f 20 70 72 65 70 65 6e 64 20 74 68 65 20 6e  To prepend the n
24883 65 77 20 73 6c 6f 74 73 2c 0a 2a 2a 20 74 68 65  ew slots,.** the
24884 20 69 53 74 61 72 74 20 76 61 6c 75 65 20 77 6f   iStart value wo
24885 75 6c 64 20 62 65 20 30 2e 20 20 54 68 65 20 72  uld be 0.  The r
24886 65 73 75 6c 74 20 74 68 65 6e 20 77 6f 75 6c 64  esult then would
24887 0a 2a 2a 20 62 65 3a 20 6e 69 6c 2c 20 6e 69 6c  .** be: nil, nil
24888 2c 20 6e 69 6c 2c 20 41 2c 20 42 2e 0a 2a 2a 0a  , nil, A, B..**.
24889 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61  ** If a memory a
2488a 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 20  llocation fails 
2488b 74 68 65 20 53 72 63 4c 69 73 74 20 69 73 20 75  the SrcList is u
2488c 6e 63 68 61 6e 67 65 64 2e 20 20 54 68 65 0a 2a  nchanged.  The.*
2488d 2a 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  * db->mallocFail
2488e 65 64 20 66 6c 61 67 20 77 69 6c 6c 20 62 65 20  ed flag will be 
2488f 73 65 74 20 74 6f 20 74 72 75 65 2e 0a 2a 2f 0a  set to true..*/.
24890 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53  SQLITE_PRIVATE S
24891 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53  rcList *sqlite3S
24892 72 63 4c 69 73 74 45 6e 6c 61 72 67 65 28 0a 20  rcListEnlarge(. 
24893 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
24894 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
24895 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f  connection to no
24896 74 69 66 79 20 6f 66 20 4f 4f 4d 20 65 72 72 6f  tify of OOM erro
24897 72 73 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  rs */.  SrcList 
24898 2a 70 53 72 63 2c 20 20 20 20 20 2f 2a 20 54 68  *pSrc,     /* Th
24899 65 20 53 72 63 4c 69 73 74 20 74 6f 20 62 65 20  e SrcList to be 
2489a 65 6e 6c 61 72 67 65 64 20 2a 2f 0a 20 20 69 6e  enlarged */.  in
2489b 74 20 6e 45 78 74 72 61 2c 20 20 20 20 20 20 20  t nExtra,       
2489c 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65   /* Number of ne
2489d 77 20 73 6c 6f 74 73 20 74 6f 20 61 64 64 20 74  w slots to add t
2489e 6f 20 70 53 72 63 2d 3e 61 5b 5d 20 2a 2f 0a 20  o pSrc->a[] */. 
2489f 20 69 6e 74 20 69 53 74 61 72 74 20 20 20 20 20   int iStart     
248a0 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20      /* Index in 
248a1 70 53 72 63 2d 3e 61 5b 5d 20 6f 66 20 66 69 72  pSrc->a[] of fir
248a2 73 74 20 6e 65 77 20 73 6c 6f 74 20 2a 2f 0a 29  st new slot */.)
248a3 7b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 2f 2a  {.  int i;..  /*
248a4 20 53 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67   Sanity checking
248a5 20 6f 6e 20 63 61 6c 6c 69 6e 67 20 70 61 72 61   on calling para
248a6 6d 65 74 65 72 73 20 2a 2f 0a 20 20 61 73 73 65  meters */.  asse
248a7 72 74 28 20 69 53 74 61 72 74 3e 3d 30 20 29 3b  rt( iStart>=0 );
248a8 0a 20 20 61 73 73 65 72 74 28 20 6e 45 78 74 72  .  assert( nExtr
248a9 61 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74  a>=1 );.  assert
248aa 28 20 70 53 72 63 21 3d 30 20 29 3b 0a 20 20 61  ( pSrc!=0 );.  a
248ab 73 73 65 72 74 28 20 69 53 74 61 72 74 3c 3d 70  ssert( iStart<=p
248ac 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 0a 20 20  Src->nSrc );..  
248ad 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 64 64 69  /* Allocate addi
248ae 74 69 6f 6e 61 6c 20 73 70 61 63 65 20 69 66 20  tional space if 
248af 6e 65 65 64 65 64 20 2a 2f 0a 20 20 69 66 28 20  needed */.  if( 
248b0 70 53 72 63 2d 3e 6e 53 72 63 2b 6e 45 78 74 72  pSrc->nSrc+nExtr
248b1 61 3e 70 53 72 63 2d 3e 6e 41 6c 6c 6f 63 20 29  a>pSrc->nAlloc )
248b2 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70  {.    SrcList *p
248b3 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20 6e 41 6c  New;.    int nAl
248b4 6c 6f 63 20 3d 20 70 53 72 63 2d 3e 6e 53 72 63  loc = pSrc->nSrc
248b5 2b 6e 45 78 74 72 61 3b 0a 20 20 20 20 69 6e 74  +nExtra;.    int
248b6 20 6e 47 6f 74 3b 0a 20 20 20 20 70 4e 65 77 20   nGot;.    pNew 
248b7 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c  = sqlite3DbReall
248b8 6f 63 28 64 62 2c 20 70 53 72 63 2c 0a 20 20 20  oc(db, pSrc,.   
248b9 20 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65              size
248ba 6f 66 28 2a 70 53 72 63 29 20 2b 20 28 6e 41 6c  of(*pSrc) + (nAl
248bb 6c 6f 63 2d 31 29 2a 73 69 7a 65 6f 66 28 70 53  loc-1)*sizeof(pS
248bc 72 63 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 20  rc->a[0]) );.   
248bd 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a   if( pNew==0 ){.
248be 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 62        assert( db
248bf 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
248c0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70  ;.      return p
248c1 53 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  Src;.    }.    p
248c2 53 72 63 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20  Src = pNew;.    
248c3 6e 47 6f 74 20 3d 20 28 73 71 6c 69 74 65 33 44  nGot = (sqlite3D
248c4 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20  bMallocSize(db, 
248c5 70 4e 65 77 29 20 2d 20 73 69 7a 65 6f 66 28 2a  pNew) - sizeof(*
248c6 70 53 72 63 29 29 2f 73 69 7a 65 6f 66 28 70 53  pSrc))/sizeof(pS
248c7 72 63 2d 3e 61 5b 30 5d 29 2b 31 3b 0a 20 20 20  rc->a[0])+1;.   
248c8 20 70 53 72 63 2d 3e 6e 41 6c 6c 6f 63 20 3d 20   pSrc->nAlloc = 
248c9 28 75 31 36 29 6e 47 6f 74 3b 0a 20 20 7d 0a 0a  (u16)nGot;.  }..
248ca 20 20 2f 2a 20 4d 6f 76 65 20 65 78 69 73 74 69    /* Move existi
248cb 6e 67 20 73 6c 6f 74 73 20 74 68 61 74 20 63 6f  ng slots that co
248cc 6d 65 20 61 66 74 65 72 20 74 68 65 20 6e 65 77  me after the new
248cd 6c 79 20 69 6e 73 65 72 74 65 64 20 73 6c 6f 74  ly inserted slot
248ce 73 0a 20 20 2a 2a 20 6f 75 74 20 6f 66 20 74 68  s.  ** out of th
248cf 65 20 77 61 79 20 2a 2f 0a 20 20 66 6f 72 28 69  e way */.  for(i
248d0 3d 70 53 72 63 2d 3e 6e 53 72 63 2d 31 3b 20 69  =pSrc->nSrc-1; i
248d1 3e 3d 69 53 74 61 72 74 3b 20 69 2d 2d 29 7b 0a  >=iStart; i--){.
248d2 20 20 20 20 70 53 72 63 2d 3e 61 5b 69 2b 6e 45      pSrc->a[i+nE
248d3 78 74 72 61 5d 20 3d 20 70 53 72 63 2d 3e 61 5b  xtra] = pSrc->a[
248d4 69 5d 3b 0a 20 20 7d 0a 20 20 70 53 72 63 2d 3e  i];.  }.  pSrc->
248d5 6e 53 72 63 20 2b 3d 20 28 69 31 36 29 6e 45 78  nSrc += (i16)nEx
248d6 74 72 61 3b 0a 0a 20 20 2f 2a 20 5a 65 72 6f 20  tra;..  /* Zero 
248d7 74 68 65 20 6e 65 77 6c 79 20 61 6c 6c 6f 63 61  the newly alloca
248d8 74 65 64 20 73 6c 6f 74 73 20 2a 2f 0a 20 20 6d  ted slots */.  m
248d9 65 6d 73 65 74 28 26 70 53 72 63 2d 3e 61 5b 69  emset(&pSrc->a[i
248da 53 74 61 72 74 5d 2c 20 30 2c 20 73 69 7a 65 6f  Start], 0, sizeo
248db 66 28 70 53 72 63 2d 3e 61 5b 30 5d 29 2a 6e 45  f(pSrc->a[0])*nE
248dc 78 74 72 61 29 3b 0a 20 20 66 6f 72 28 69 3d 69  xtra);.  for(i=i
248dd 53 74 61 72 74 3b 20 69 3c 69 53 74 61 72 74 2b  Start; i<iStart+
248de 6e 45 78 74 72 61 3b 20 69 2b 2b 29 7b 0a 20 20  nExtra; i++){.  
248df 20 20 70 53 72 63 2d 3e 61 5b 69 5d 2e 69 43 75    pSrc->a[i].iCu
248e0 72 73 6f 72 20 3d 20 2d 31 3b 0a 20 20 7d 0a 0a  rsor = -1;.  }..
248e1 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 20 70 6f    /* Return a po
248e2 69 6e 74 65 72 20 74 6f 20 74 68 65 20 65 6e 6c  inter to the enl
248e3 61 72 67 65 64 20 53 72 63 4c 69 73 74 20 2a 2f  arged SrcList */
248e4 0a 20 20 72 65 74 75 72 6e 20 70 53 72 63 3b 0a  .  return pSrc;.
248e5 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64  }.../*.** Append
248e6 20 61 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d   a new table nam
248e7 65 20 74 6f 20 74 68 65 20 67 69 76 65 6e 20 53  e to the given S
248e8 72 63 4c 69 73 74 2e 20 20 43 72 65 61 74 65 20  rcList.  Create 
248e9 61 20 6e 65 77 20 53 72 63 4c 69 73 74 20 69 66  a new SrcList if
248ea 0a 2a 2a 20 6e 65 65 64 20 62 65 2e 20 20 41 20  .** need be.  A 
248eb 6e 65 77 20 65 6e 74 72 79 20 69 73 20 63 72 65  new entry is cre
248ec 61 74 65 64 20 69 6e 20 74 68 65 20 53 72 63 4c  ated in the SrcL
248ed 69 73 74 20 65 76 65 6e 20 69 66 20 70 54 61 62  ist even if pTab
248ee 6c 65 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  le is NULL..**.*
248ef 2a 20 41 20 53 72 63 4c 69 73 74 20 69 73 20 72  * A SrcList is r
248f0 65 74 75 72 6e 65 64 2c 20 6f 72 20 4e 55 4c 4c  eturned, or NULL
248f1 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20   if there is an 
248f2 4f 4f 4d 20 65 72 72 6f 72 2e 20 20 54 68 65 20  OOM error.  The 
248f3 72 65 74 75 72 6e 65 64 0a 2a 2a 20 53 72 63 4c  returned.** SrcL
248f4 69 73 74 20 6d 69 67 68 74 20 62 65 20 74 68 65  ist might be the
248f5 20 73 61 6d 65 20 61 73 20 74 68 65 20 53 72 63   same as the Src
248f6 4c 69 73 74 20 74 68 61 74 20 77 61 73 20 69 6e  List that was in
248f7 70 75 74 20 6f 72 20 69 74 20 6d 69 67 68 74 20  put or it might 
248f8 62 65 0a 2a 2a 20 61 20 6e 65 77 20 6f 6e 65 2e  be.** a new one.
248f9 20 20 49 66 20 61 6e 20 4f 4f 4d 20 65 72 72 6f    If an OOM erro
248fa 72 20 64 6f 65 73 20 6f 63 63 75 72 73 2c 20 74  r does occurs, t
248fb 68 65 6e 20 74 68 65 20 70 72 69 6f 72 20 76 61  hen the prior va
248fc 6c 75 65 20 6f 66 20 70 4c 69 73 74 0a 2a 2a 20  lue of pList.** 
248fd 74 68 61 74 20 69 73 20 69 6e 70 75 74 20 74 6f  that is input to
248fe 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
248ff 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 66   automatically f
24900 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70  reed..**.** If p
24901 44 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20  Database is not 
24902 6e 75 6c 6c 2c 20 69 74 20 6d 65 61 6e 73 20 74  null, it means t
24903 68 61 74 20 74 68 65 20 74 61 62 6c 65 20 68 61  hat the table ha
24904 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 0a 2a 2a  s an optional.**
24905 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 70   database name p
24906 72 65 66 69 78 2e 20 20 4c 69 6b 65 20 74 68 69  refix.  Like thi
24907 73 3a 20 20 22 64 61 74 61 62 61 73 65 2e 74 61  s:  "database.ta
24908 62 6c 65 22 2e 20 20 54 68 65 20 70 44 61 74 61  ble".  The pData
24909 62 61 73 65 0a 2a 2a 20 70 6f 69 6e 74 73 20 74  base.** points t
2490a 6f 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  o the table name
2490b 20 61 6e 64 20 74 68 65 20 70 54 61 62 6c 65 20   and the pTable 
2490c 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 64 61  points to the da
2490d 74 61 62 61 73 65 20 6e 61 6d 65 2e 0a 2a 2a 20  tabase name..** 
2490e 54 68 65 20 53 72 63 4c 69 73 74 2e 61 5b 5d 2e  The SrcList.a[].
2490f 7a 4e 61 6d 65 20 66 69 65 6c 64 20 69 73 20 66  zName field is f
24910 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 74  illed with the t
24911 61 62 6c 65 20 6e 61 6d 65 20 77 68 69 63 68 20  able name which 
24912 6d 69 67 68 74 0a 2a 2a 20 63 6f 6d 65 20 66 72  might.** come fr
24913 6f 6d 20 70 54 61 62 6c 65 20 28 69 66 20 70 44  om pTable (if pD
24914 61 74 61 62 61 73 65 20 69 73 20 4e 55 4c 4c 29  atabase is NULL)
24915 20 6f 72 20 66 72 6f 6d 20 70 44 61 74 61 62 61   or from pDataba
24916 73 65 2e 20 20 0a 2a 2a 20 53 72 63 4c 69 73 74  se.  .** SrcList
24917 2e 61 5b 5d 2e 7a 44 61 74 61 62 61 73 65 20 69  .a[].zDatabase i
24918 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68  s filled with th
24919 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  e database name 
2491a 66 72 6f 6d 20 70 54 61 62 6c 65 2c 0a 2a 2a 20  from pTable,.** 
2491b 6f 72 20 77 69 74 68 20 4e 55 4c 4c 20 69 66 20  or with NULL if 
2491c 6e 6f 20 64 61 74 61 62 61 73 65 20 69 73 20 73  no database is s
2491d 70 65 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20  pecified..**.** 
2491e 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
2491f 69 66 20 63 61 6c 6c 20 6c 69 6b 65 20 74 68 69  if call like thi
24920 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  s:.**.**        
24921 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
24922 70 70 65 6e 64 28 44 2c 41 2c 42 2c 30 29 3b 0a  ppend(D,A,B,0);.
24923 2a 2a 0a 2a 2a 20 54 68 65 6e 20 42 20 69 73 20  **.** Then B is 
24924 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64  a table name and
24925 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61   the database na
24926 6d 65 20 69 73 20 75 6e 73 70 65 63 69 66 69 65  me is unspecifie
24927 64 2e 20 20 49 66 20 63 61 6c 6c 65 64 0a 2a 2a  d.  If called.**
24928 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   like this:.**.*
24929 2a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  *         sqlite
2492a 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 44  3SrcListAppend(D
2492b 2c 41 2c 42 2c 43 29 3b 0a 2a 2a 0a 2a 2a 20 54  ,A,B,C);.**.** T
2492c 68 65 6e 20 43 20 69 73 20 74 68 65 20 74 61 62  hen C is the tab
2492d 6c 65 20 6e 61 6d 65 20 61 6e 64 20 42 20 69 73  le name and B is
2492e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61   the database na
2492f 6d 65 2e 20 20 49 66 20 43 20 69 73 20 64 65 66  me.  If C is def
24930 69 6e 65 64 0a 2a 2a 20 74 68 65 6e 20 73 6f 20  ined.** then so 
24931 69 73 20 42 2e 20 20 49 6e 20 6f 74 68 65 72 20  is B.  In other 
24932 77 6f 72 64 73 2c 20 77 65 20 6e 65 76 65 72 20  words, we never 
24933 68 61 76 65 20 61 20 63 61 73 65 20 77 68 65 72  have a case wher
24934 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  e:.**.**        
24935 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
24936 70 70 65 6e 64 28 44 2c 41 2c 30 2c 43 29 3b 0a  ppend(D,A,0,C);.
24937 2a 2a 0a 2a 2a 20 42 6f 74 68 20 70 54 61 62 6c  **.** Both pTabl
24938 65 20 61 6e 64 20 70 44 61 74 61 62 61 73 65 20  e and pDatabase 
24939 61 72 65 20 61 73 73 75 6d 65 64 20 74 6f 20 62  are assumed to b
2493a 65 20 71 75 6f 74 65 64 2e 20 20 54 68 65 79 20  e quoted.  They 
2493b 61 72 65 20 64 65 71 75 6f 74 65 64 0a 2a 2a 20  are dequoted.** 
2493c 62 65 66 6f 72 65 20 62 65 69 6e 67 20 61 64 64  before being add
2493d 65 64 20 74 6f 20 74 68 65 20 53 72 63 4c 69 73  ed to the SrcLis
2493e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
2493f 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71  VATE SrcList *sq
24940 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
24941 6e 64 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  nd(.  sqlite3 *d
24942 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  b,        /* Con
24943 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 69 66  nection to notif
24944 79 20 6f 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  y of malloc fail
24945 75 72 65 73 20 2a 2f 0a 20 20 53 72 63 4c 69 73  ures */.  SrcLis
24946 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 2f 2a  t *pList,     /*
24947 20 41 70 70 65 6e 64 20 74 6f 20 74 68 69 73 20   Append to this 
24948 53 72 63 4c 69 73 74 2e 20 4e 55 4c 4c 20 63 72  SrcList. NULL cr
24949 65 61 74 65 73 20 61 20 6e 65 77 20 53 72 63 4c  eates a new SrcL
2494a 69 73 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ist */.  Token *
2494b 70 54 61 62 6c 65 2c 20 20 20 20 20 20 2f 2a 20  pTable,      /* 
2494c 54 61 62 6c 65 20 74 6f 20 61 70 70 65 6e 64 20  Table to append 
2494d 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 44 61 74  */.  Token *pDat
2494e 61 62 61 73 65 20 20 20 20 2f 2a 20 44 61 74 61  abase    /* Data
2494f 62 61 73 65 20 6f 66 20 74 68 65 20 74 61 62 6c  base of the tabl
24950 65 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74  e */.){.  struct
24951 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
24952 49 74 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 20  Item;.  assert( 
24953 70 44 61 74 61 62 61 73 65 3d 3d 30 20 7c 7c 20  pDatabase==0 || 
24954 70 54 61 62 6c 65 21 3d 30 20 29 3b 20 20 2f 2a  pTable!=0 );  /*
24955 20 43 61 6e 6e 6f 74 20 68 61 76 65 20 43 20 77   Cannot have C w
24956 69 74 68 6f 75 74 20 42 20 2a 2f 0a 20 20 69 66  ithout B */.  if
24957 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20  ( pList==0 ){.  
24958 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65    pList = sqlite
24959 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
2495a 2c 20 73 69 7a 65 6f 66 28 53 72 63 4c 69 73 74  , sizeof(SrcList
2495b 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69  ) );.    if( pLi
2495c 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  st==0 ) return 0
2495d 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c  ;.    pList->nAl
2495e 6c 6f 63 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 70  loc = 1;.  }.  p
2495f 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 53 72  List = sqlite3Sr
24960 63 4c 69 73 74 45 6e 6c 61 72 67 65 28 64 62 2c  cListEnlarge(db,
24961 20 70 4c 69 73 74 2c 20 31 2c 20 70 4c 69 73 74   pList, 1, pList
24962 2d 3e 6e 53 72 63 29 3b 0a 20 20 69 66 28 20 64  ->nSrc);.  if( d
24963 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
24964 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 72  ){.    sqlite3Sr
24965 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  cListDelete(db, 
24966 70 4c 69 73 74 29 3b 0a 20 20 20 20 72 65 74 75  pList);.    retu
24967 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 49 74 65  rn 0;.  }.  pIte
24968 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c  m = &pList->a[pL
24969 69 73 74 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20  ist->nSrc-1];.  
2496a 69 66 28 20 70 44 61 74 61 62 61 73 65 20 26 26  if( pDatabase &&
2496b 20 70 44 61 74 61 62 61 73 65 2d 3e 7a 3d 3d 30   pDatabase->z==0
2496c 20 29 7b 0a 20 20 20 20 70 44 61 74 61 62 61 73   ){.    pDatabas
2496d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  e = 0;.  }.  if(
2496e 20 70 44 61 74 61 62 61 73 65 20 29 7b 0a 20 20   pDatabase ){.  
2496f 20 20 54 6f 6b 65 6e 20 2a 70 54 65 6d 70 20 3d    Token *pTemp =
24970 20 70 44 61 74 61 62 61 73 65 3b 0a 20 20 20 20   pDatabase;.    
24971 70 44 61 74 61 62 61 73 65 20 3d 20 70 54 61 62  pDatabase = pTab
24972 6c 65 3b 0a 20 20 20 20 70 54 61 62 6c 65 20 3d  le;.    pTable =
24973 20 70 54 65 6d 70 3b 0a 20 20 7d 0a 20 20 70 49   pTemp;.  }.  pI
24974 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c  tem->zName = sql
24975 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
24976 6e 28 64 62 2c 20 70 54 61 62 6c 65 29 3b 0a 20  n(db, pTable);. 
24977 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73   pItem->zDatabas
24978 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  e = sqlite3NameF
24979 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 44 61  romToken(db, pDa
2497a 74 61 62 61 73 65 29 3b 0a 20 20 72 65 74 75 72  tabase);.  retur
2497b 6e 20 70 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  n pList;.}../*.*
2497c 2a 20 41 73 73 69 67 6e 20 56 64 62 65 43 75 72  * Assign VdbeCur
2497d 73 6f 72 20 69 6e 64 65 78 20 6e 75 6d 62 65 72  sor index number
2497e 73 20 74 6f 20 61 6c 6c 20 74 61 62 6c 65 73 20  s to all tables 
2497f 69 6e 20 61 20 53 72 63 4c 69 73 74 0a 2a 2f 0a  in a SrcList.*/.
24980 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
24981 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69  oid sqlite3SrcLi
24982 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28  stAssignCursors(
24983 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53  Parse *pParse, S
24984 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a  rcList *pList){.
24985 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63    int i;.  struc
24986 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
24987 70 49 74 65 6d 3b 0a 20 20 61 73 73 65 72 74 28  pItem;.  assert(
24988 70 4c 69 73 74 20 7c 7c 20 70 50 61 72 73 65 2d  pList || pParse-
24989 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
2498a 64 20 29 3b 0a 20 20 69 66 28 20 70 4c 69 73 74  d );.  if( pList
2498b 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c   ){.    for(i=0,
2498c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b   pItem=pList->a;
2498d 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20   i<pList->nSrc; 
2498e 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i++, pItem++){. 
2498f 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e       if( pItem->
24990 69 43 75 72 73 6f 72 3e 3d 30 20 29 20 62 72 65  iCursor>=0 ) bre
24991 61 6b 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d  ak;.      pItem-
24992 3e 69 43 75 72 73 6f 72 20 3d 20 70 50 61 72 73  >iCursor = pPars
24993 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20  e->nTab++;.     
24994 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c   if( pItem->pSel
24995 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ect ){.        s
24996 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 73 73  qlite3SrcListAss
24997 69 67 6e 43 75 72 73 6f 72 73 28 70 50 61 72 73  ignCursors(pPars
24998 65 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63  e, pItem->pSelec
24999 74 2d 3e 70 53 72 63 29 3b 0a 20 20 20 20 20 20  t->pSrc);.      
2499a 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  }.    }.  }.}../
2499b 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65  *.** Delete an e
2499c 6e 74 69 72 65 20 53 72 63 4c 69 73 74 20 69 6e  ntire SrcList in
2499d 63 6c 75 64 69 6e 67 20 61 6c 6c 20 69 74 73 20  cluding all its 
2499e 73 75 62 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  substructure..*/
2499f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
249a0 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c  void sqlite3SrcL
249a1 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65  istDelete(sqlite
249a2 33 20 2a 64 62 2c 20 53 72 63 4c 69 73 74 20 2a  3 *db, SrcList *
249a3 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b  pList){.  int i;
249a4 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
249a5 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20  t_item *pItem;. 
249a6 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20   if( pList==0 ) 
249a7 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 70 49  return;.  for(pI
249a8 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d  tem=pList->a, i=
249a9 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 63  0; i<pList->nSrc
249aa 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
249ab 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
249ac 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 44  ee(db, pItem->zD
249ad 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 73 71  atabase);.    sq
249ae 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
249af 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  pItem->zName);. 
249b0 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
249b1 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69  (db, pItem->zAli
249b2 61 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  as);.    sqlite3
249b3 44 62 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d  DbFree(db, pItem
249b4 2d 3e 7a 49 6e 64 65 78 29 3b 0a 20 20 20 20 73  ->zIndex);.    s
249b5 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c  qlite3DeleteTabl
249b6 65 28 70 49 74 65 6d 2d 3e 70 54 61 62 29 3b 0a  e(pItem->pTab);.
249b7 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
249b8 74 44 65 6c 65 74 65 28 64 62 2c 20 70 49 74 65  tDelete(db, pIte
249b9 6d 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20  m->pSelect);.   
249ba 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
249bb 74 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 4f  te(db, pItem->pO
249bc 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49  n);.    sqlite3I
249bd 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  dListDelete(db, 
249be 70 49 74 65 6d 2d 3e 70 55 73 69 6e 67 29 3b 0a  pItem->pUsing);.
249bf 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46    }.  sqlite3DbF
249c0 72 65 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a  ree(db, pList);.
249c1 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
249c2 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
249c3 62 79 20 74 68 65 20 70 61 72 73 65 72 20 74 6f  by the parser to
249c4 20 61 64 64 20 61 20 6e 65 77 20 74 65 72 6d 20   add a new term 
249c5 74 6f 20 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66  to the.** end of
249c6 20 61 20 67 72 6f 77 69 6e 67 20 46 52 4f 4d 20   a growing FROM 
249c7 63 6c 61 75 73 65 2e 20 20 54 68 65 20 22 70 22  clause.  The "p"
249c8 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68   parameter is th
249c9 65 20 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65  e part of.** the
249ca 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 68 61   FROM clause tha
249cb 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  t has already be
249cc 65 6e 20 63 6f 6e 73 74 72 75 63 74 65 64 2e 20  en constructed. 
249cd 20 22 70 22 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20   "p" is NULL.** 
249ce 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66  if this is the f
249cf 69 72 73 74 20 74 65 72 6d 20 6f 66 20 74 68 65  irst term of the
249d0 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 70   FROM clause.  p
249d1 54 61 62 6c 65 20 61 6e 64 20 70 44 61 74 61 62  Table and pDatab
249d2 61 73 65 0a 2a 2a 20 61 72 65 20 74 68 65 20 6e  ase.** are the n
249d3 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
249d4 20 61 6e 64 20 64 61 74 61 62 61 73 65 20 6e 61   and database na
249d5 6d 65 64 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  med in the FROM 
249d6 63 6c 61 75 73 65 20 74 65 72 6d 2e 0a 2a 2a 20  clause term..** 
249d7 70 44 61 74 61 62 61 73 65 20 69 73 20 4e 55 4c  pDatabase is NUL
249d8 4c 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73  L if the databas
249d9 65 20 6e 61 6d 65 20 71 75 61 6c 69 66 69 65 72  e name qualifier
249da 20 69 73 20 6d 69 73 73 69 6e 67 20 2d 20 74 68   is missing - th
249db 65 0a 2a 2a 20 75 73 75 61 6c 20 63 61 73 65 2e  e.** usual case.
249dc 20 20 49 66 20 74 68 65 20 74 65 72 6d 20 68 61    If the term ha
249dd 73 20 61 20 61 6c 69 61 73 2c 20 74 68 65 6e 20  s a alias, then 
249de 70 41 6c 69 61 73 20 70 6f 69 6e 74 73 20 74 6f  pAlias points to
249df 20 74 68 65 0a 2a 2a 20 61 6c 69 61 73 20 74 6f   the.** alias to
249e0 6b 65 6e 2e 20 20 49 66 20 74 68 65 20 74 65 72  ken.  If the ter
249e1 6d 20 69 73 20 61 20 73 75 62 71 75 65 72 79 2c  m is a subquery,
249e2 20 74 68 65 6e 20 70 53 75 62 71 75 65 72 79 20   then pSubquery 
249e3 69 73 20 74 68 65 0a 2a 2a 20 53 45 4c 45 43 54  is the.** SELECT
249e4 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
249e5 74 68 65 20 73 75 62 71 75 65 72 79 20 65 6e 63  the subquery enc
249e6 6f 64 65 73 2e 20 20 54 68 65 20 70 54 61 62 6c  odes.  The pTabl
249e7 65 20 61 6e 64 0a 2a 2a 20 70 44 61 74 61 62 61  e and.** pDataba
249e8 73 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 72  se parameters ar
249e9 65 20 4e 55 4c 4c 20 66 6f 72 20 73 75 62 71 75  e NULL for subqu
249ea 65 72 69 65 73 2e 20 20 54 68 65 20 70 4f 6e 20  eries.  The pOn 
249eb 61 6e 64 20 70 55 73 69 6e 67 0a 2a 2a 20 70 61  and pUsing.** pa
249ec 72 61 6d 65 74 65 72 73 20 61 72 65 20 74 68 65  rameters are the
249ed 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20   content of the 
249ee 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61  ON and USING cla
249ef 75 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  uses..**.** Retu
249f0 72 6e 20 61 20 6e 65 77 20 53 72 63 4c 69 73 74  rn a new SrcList
249f1 20 77 68 69 63 68 20 65 6e 63 6f 64 65 73 20 69   which encodes i
249f2 73 20 74 68 65 20 46 52 4f 4d 20 77 69 74 68 20  s the FROM with 
249f3 74 68 65 20 6e 65 77 0a 2a 2a 20 74 65 72 6d 20  the new.** term 
249f4 61 64 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  added..*/.SQLITE
249f5 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 74  _PRIVATE SrcList
249f6 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74   *sqlite3SrcList
249f7 41 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28 0a  AppendFromTerm(.
249f8 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
249f9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
249fa 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
249fb 20 20 53 72 63 4c 69 73 74 20 2a 70 2c 20 20 20    SrcList *p,   
249fc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
249fd 20 6c 65 66 74 20 70 61 72 74 20 6f 66 20 74 68   left part of th
249fe 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 6c  e FROM clause al
249ff 72 65 61 64 79 20 73 65 65 6e 20 2a 2f 0a 20 20  ready seen */.  
24a00 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 2c 20 20  Token *pTable,  
24a01 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
24a02 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  of the table to 
24a03 61 64 64 20 74 6f 20 74 68 65 20 46 52 4f 4d 20  add to the FROM 
24a04 63 6c 61 75 73 65 20 2a 2f 0a 20 20 54 6f 6b 65  clause */.  Toke
24a05 6e 20 2a 70 44 61 74 61 62 61 73 65 2c 20 20 20  n *pDatabase,   
24a06 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
24a07 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74  he database cont
24a08 61 69 6e 69 6e 67 20 70 54 61 62 6c 65 20 2a 2f  aining pTable */
24a09 0a 20 20 54 6f 6b 65 6e 20 2a 70 41 6c 69 61 73  .  Token *pAlias
24a0a 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
24a0b 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64  e right-hand sid
24a0c 65 20 6f 66 20 74 68 65 20 41 53 20 73 75 62 65  e of the AS sube
24a0d 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 53  xpression */.  S
24a0e 65 6c 65 63 74 20 2a 70 53 75 62 71 75 65 72 79  elect *pSubquery
24a0f 2c 20 20 20 20 20 20 2f 2a 20 41 20 73 75 62 71  ,      /* A subq
24a10 75 65 72 79 20 75 73 65 64 20 69 6e 20 70 6c 61  uery used in pla
24a11 63 65 20 6f 66 20 61 20 74 61 62 6c 65 20 6e 61  ce of a table na
24a12 6d 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f  me */.  Expr *pO
24a13 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
24a14 2f 2a 20 54 68 65 20 4f 4e 20 63 6c 61 75 73 65  /* The ON clause
24a15 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20   of a join */.  
24a16 49 64 4c 69 73 74 20 2a 70 55 73 69 6e 67 20 20  IdList *pUsing  
24a17 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 55          /* The U
24a18 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 20 61  SING clause of a
24a19 20 6a 6f 69 6e 20 2a 2f 0a 29 7b 0a 20 20 73 74   join */.){.  st
24a1a 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
24a1b 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 73 71 6c 69  m *pItem;.  sqli
24a1c 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
24a1d 2d 3e 64 62 3b 0a 20 20 69 66 28 20 21 70 20 26  ->db;.  if( !p &
24a1e 26 20 28 70 4f 6e 20 7c 7c 20 70 55 73 69 6e 67  & (pOn || pUsing
24a1f 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
24a20 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
24a21 20 22 61 20 4a 4f 49 4e 20 63 6c 61 75 73 65 20   "a JOIN clause 
24a22 69 73 20 72 65 71 75 69 72 65 64 20 62 65 66 6f  is required befo
24a23 72 65 20 25 73 22 2c 20 0a 20 20 20 20 20 20 28  re %s", .      (
24a24 70 4f 6e 20 3f 20 22 4f 4e 22 20 3a 20 22 55 53  pOn ? "ON" : "US
24a25 49 4e 47 22 29 0a 20 20 20 20 29 3b 0a 20 20 20  ING").    );.   
24a26 20 67 6f 74 6f 20 61 70 70 65 6e 64 5f 66 72 6f   goto append_fro
24a27 6d 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 70  m_error;.  }.  p
24a28 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
24a29 74 41 70 70 65 6e 64 28 64 62 2c 20 70 2c 20 70  tAppend(db, p, p
24a2a 54 61 62 6c 65 2c 20 70 44 61 74 61 62 61 73 65  Table, pDatabase
24a2b 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c  );.  if( p==0 ||
24a2c 20 4e 45 56 45 52 28 70 2d 3e 6e 53 72 63 3d 3d   NEVER(p->nSrc==
24a2d 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 61  0) ){.    goto a
24a2e 70 70 65 6e 64 5f 66 72 6f 6d 5f 65 72 72 6f 72  ppend_from_error
24a2f 3b 0a 20 20 7d 0a 20 20 70 49 74 65 6d 20 3d 20  ;.  }.  pItem = 
24a30 26 70 2d 3e 61 5b 70 2d 3e 6e 53 72 63 2d 31 5d  &p->a[p->nSrc-1]
24a31 3b 0a 20 20 61 73 73 65 72 74 28 20 70 41 6c 69  ;.  assert( pAli
24a32 61 73 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  as!=0 );.  if( p
24a33 41 6c 69 61 73 2d 3e 6e 20 29 7b 0a 20 20 20 20  Alias->n ){.    
24a34 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20  pItem->zAlias = 
24a35 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
24a36 6f 6b 65 6e 28 64 62 2c 20 70 41 6c 69 61 73 29  oken(db, pAlias)
24a37 3b 0a 20 20 7d 0a 20 20 70 49 74 65 6d 2d 3e 70  ;.  }.  pItem->p
24a38 53 65 6c 65 63 74 20 3d 20 70 53 75 62 71 75 65  Select = pSubque
24a39 72 79 3b 0a 20 20 70 49 74 65 6d 2d 3e 70 4f 6e  ry;.  pItem->pOn
24a3a 20 3d 20 70 4f 6e 3b 0a 20 20 70 49 74 65 6d 2d   = pOn;.  pItem-
24a3b 3e 70 55 73 69 6e 67 20 3d 20 70 55 73 69 6e 67  >pUsing = pUsing
24a3c 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 0a 20  ;.  return p;.. 
24a3d 61 70 70 65 6e 64 5f 66 72 6f 6d 5f 65 72 72 6f  append_from_erro
24a3e 72 3a 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d  r:.  assert( p==
24a3f 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  0 );.  sqlite3Ex
24a40 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 4f 6e  prDelete(db, pOn
24a41 29 3b 0a 20 20 73 71 6c 69 74 65 33 49 64 4c 69  );.  sqlite3IdLi
24a42 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 55 73  stDelete(db, pUs
24a43 69 6e 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  ing);.  sqlite3S
24a44 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
24a45 70 53 75 62 71 75 65 72 79 29 3b 0a 20 20 72 65  pSubquery);.  re
24a46 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
24a47 20 41 64 64 20 61 6e 20 49 4e 44 45 58 45 44 20   Add an INDEXED 
24a48 42 59 20 6f 72 20 4e 4f 54 20 49 4e 44 45 58 45  BY or NOT INDEXE
24a49 44 20 63 6c 61 75 73 65 20 74 6f 20 74 68 65 20  D clause to the 
24a4a 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64  most recently ad
24a4b 64 65 64 20 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20  ded .** element 
24a4c 6f 66 20 74 68 65 20 73 6f 75 72 63 65 2d 6c 69  of the source-li
24a4d 73 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65  st passed as the
24a4e 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
24a4f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
24a50 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
24a51 53 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79  SrcListIndexedBy
24a52 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
24a53 53 72 63 4c 69 73 74 20 2a 70 2c 20 54 6f 6b 65  SrcList *p, Toke
24a54 6e 20 2a 70 49 6e 64 65 78 65 64 42 79 29 7b 0a  n *pIndexedBy){.
24a55 20 20 61 73 73 65 72 74 28 20 70 49 6e 64 65 78    assert( pIndex
24a56 65 64 42 79 21 3d 30 20 29 3b 0a 20 20 69 66 28  edBy!=0 );.  if(
24a57 20 70 20 26 26 20 41 4c 57 41 59 53 28 70 2d 3e   p && ALWAYS(p->
24a58 6e 53 72 63 3e 30 29 20 29 7b 0a 20 20 20 20 73  nSrc>0) ){.    s
24a59 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
24a5a 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e  em *pItem = &p->
24a5b 61 5b 70 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20  a[p->nSrc-1];.  
24a5c 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d    assert( pItem-
24a5d 3e 6e 6f 74 49 6e 64 65 78 65 64 3d 3d 30 20 26  >notIndexed==0 &
24a5e 26 20 70 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 3d  & pItem->zIndex=
24a5f 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 49  =0 );.    if( pI
24a60 6e 64 65 78 65 64 42 79 2d 3e 6e 3d 3d 31 20 26  ndexedBy->n==1 &
24a61 26 20 21 70 49 6e 64 65 78 65 64 42 79 2d 3e 7a  & !pIndexedBy->z
24a62 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 22   ){.      /* A "
24a63 4e 4f 54 20 49 4e 44 45 58 45 44 22 20 63 6c 61  NOT INDEXED" cla
24a64 75 73 65 20 77 61 73 20 73 75 70 70 6c 69 65 64  use was supplied
24a65 2e 20 53 65 65 20 70 61 72 73 65 2e 79 20 0a 20  . See parse.y . 
24a66 20 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72 75 63       ** construc
24a67 74 20 22 69 6e 64 65 78 65 64 5f 6f 70 74 22 20  t "indexed_opt" 
24a68 66 6f 72 20 64 65 74 61 69 6c 73 2e 20 2a 2f 0a  for details. */.
24a69 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 6e 6f 74        pItem->not
24a6a 49 6e 64 65 78 65 64 20 3d 20 31 3b 0a 20 20 20  Indexed = 1;.   
24a6b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49   }else{.      pI
24a6c 74 65 6d 2d 3e 7a 49 6e 64 65 78 20 3d 20 73 71  tem->zIndex = sq
24a6d 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
24a6e 65 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  en(pParse->db, p
24a6f 49 6e 64 65 78 65 64 42 79 29 3b 0a 20 20 20 20  IndexedBy);.    
24a70 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  }.  }.}../*.** W
24a71 68 65 6e 20 62 75 69 6c 64 69 6e 67 20 75 70 20  hen building up 
24a72 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 6e  a FROM clause in
24a73 20 74 68 65 20 70 61 72 73 65 72 2c 20 74 68 65   the parser, the
24a74 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 0a 2a   join operator.*
24a75 2a 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 61  * is initially a
24a76 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 6c  ttached to the l
24a77 65 66 74 20 6f 70 65 72 61 6e 64 2e 20 20 42 75  eft operand.  Bu
24a78 74 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72  t the code gener
24a79 61 74 6f 72 0a 2a 2a 20 65 78 70 65 63 74 73 20  ator.** expects 
24a7a 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f  the join operato
24a7b 72 20 74 6f 20 62 65 20 6f 6e 20 74 68 65 20 72  r to be on the r
24a7c 69 67 68 74 20 6f 70 65 72 61 6e 64 2e 20 20 54  ight operand.  T
24a7d 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 53  his routine.** S
24a7e 68 69 66 74 73 20 61 6c 6c 20 6a 6f 69 6e 20 6f  hifts all join o
24a7f 70 65 72 61 74 6f 72 73 20 66 72 6f 6d 20 6c 65  perators from le
24a80 66 74 20 74 6f 20 72 69 67 68 74 20 66 6f 72 20  ft to right for 
24a81 61 6e 20 65 6e 74 69 72 65 20 46 52 4f 4d 0a 2a  an entire FROM.*
24a82 2a 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20  * clause..**.** 
24a83 45 78 61 6d 70 6c 65 3a 20 53 75 70 70 6f 73 65  Example: Suppose
24a84 20 74 68 65 20 6a 6f 69 6e 20 69 73 20 6c 69 6b   the join is lik
24a85 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
24a86 20 20 20 20 20 20 20 20 41 20 6e 61 74 75 72 61          A natura
24a87 6c 20 63 72 6f 73 73 20 6a 6f 69 6e 20 42 0a 2a  l cross join B.*
24a88 2a 0a 2a 2a 20 54 68 65 20 6f 70 65 72 61 74 6f  *.** The operato
24a89 72 20 69 73 20 22 6e 61 74 75 72 61 6c 20 63 72  r is "natural cr
24a8a 6f 73 73 20 6a 6f 69 6e 22 2e 20 20 54 68 65 20  oss join".  The 
24a8b 41 20 61 6e 64 20 42 20 6f 70 65 72 61 6e 64 73  A and B operands
24a8c 20 61 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69   are stored.** i
24a8d 6e 20 70 2d 3e 61 5b 30 5d 20 61 6e 64 20 70 2d  n p->a[0] and p-
24a8e 3e 61 5b 31 5d 2c 20 72 65 73 70 65 63 74 69 76  >a[1], respectiv
24a8f 65 6c 79 2e 20 20 54 68 65 20 70 61 72 73 65 72  ely.  The parser
24a90 20 69 6e 69 74 69 61 6c 6c 79 20 73 74 6f 72 65   initially store
24a91 73 20 74 68 65 0a 2a 2a 20 6f 70 65 72 61 74 6f  s the.** operato
24a92 72 20 77 69 74 68 20 41 2e 20 20 54 68 69 73 20  r with A.  This 
24a93 72 6f 75 74 69 6e 65 20 73 68 69 66 74 73 20 74  routine shifts t
24a94 68 61 74 20 6f 70 65 72 61 74 6f 72 20 6f 76 65  hat operator ove
24a95 72 20 74 6f 20 42 2e 0a 2a 2f 0a 53 51 4c 49 54  r to B..*/.SQLIT
24a96 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
24a97 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68 69  qlite3SrcListShi
24a98 66 74 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c 69  ftJoinType(SrcLi
24a99 73 74 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20  st *p){.  if( p 
24a9a 26 26 20 70 2d 3e 61 20 29 7b 0a 20 20 20 20 69  && p->a ){.    i
24a9b 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nt i;.    for(i=
24a9c 70 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 30 3b 20  p->nSrc-1; i>0; 
24a9d 69 2d 2d 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61  i--){.      p->a
24a9e 5b 69 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 70  [i].jointype = p
24a9f 2d 3e 61 5b 69 2d 31 5d 2e 6a 6f 69 6e 74 79 70  ->a[i-1].jointyp
24aa0 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e  e;.    }.    p->
24aa1 61 5b 30 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20  a[0].jointype = 
24aa2 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0;.  }.}../*.** 
24aa3 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74  Begin a transact
24aa4 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ion.*/.SQLITE_PR
24aa5 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
24aa6 65 33 42 65 67 69 6e 54 72 61 6e 73 61 63 74 69  e3BeginTransacti
24aa7 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  on(Parse *pParse
24aa8 2c 20 69 6e 74 20 74 79 70 65 29 7b 0a 20 20 73  , int type){.  s
24aa9 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64  qlite3 *db;.  Vd
24aaa 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69 3b 0a  be *v;.  int i;.
24aab 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73  .  assert( pPars
24aac 65 21 3d 30 20 29 3b 0a 20 20 64 62 20 3d 20 70  e!=0 );.  db = p
24aad 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73  Parse->db;.  ass
24aae 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 2f 2a  ert( db!=0 );./*
24aaf 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 30 5d    if( db->aDb[0]
24ab0 2e 70 42 74 3d 3d 30 20 29 20 72 65 74 75 72 6e  .pBt==0 ) return
24ab1 3b 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74  ; */.  if( sqlit
24ab2 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
24ab3 73 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  se, SQLITE_TRANS
24ab4 41 43 54 49 4f 4e 2c 20 22 42 45 47 49 4e 22 2c  ACTION, "BEGIN",
24ab5 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65   0, 0) ){.    re
24ab6 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 76 20 3d 20  turn;.  }.  v = 
24ab7 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
24ab8 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 21 76  Parse);.  if( !v
24ab9 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
24aba 20 74 79 70 65 21 3d 54 4b 5f 44 45 46 45 52 52   type!=TK_DEFERR
24abb 45 44 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  ED ){.    for(i=
24abc 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
24abd 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
24abe 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
24abf 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69  P_Transaction, i
24ac0 2c 20 28 74 79 70 65 3d 3d 54 4b 5f 45 58 43 4c  , (type==TK_EXCL
24ac1 55 53 49 56 45 29 2b 31 29 3b 0a 20 20 20 20 20  USIVE)+1);.     
24ac2 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73   sqlite3VdbeUses
24ac3 42 74 72 65 65 28 76 2c 20 69 29 3b 0a 20 20 20  Btree(v, i);.   
24ac4 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
24ac5 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
24ac6 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c 20 30 2c 20  _AutoCommit, 0, 
24ac7 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d  0);.}../*.** Com
24ac8 6d 69 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f  mit a transactio
24ac9 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  n.*/.SQLITE_PRIV
24aca 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
24acb 43 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 6f  CommitTransactio
24acc 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29  n(Parse *pParse)
24acd 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
24ace 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 61  .  Vdbe *v;..  a
24acf 73 73 65 72 74 28 20 70 50 61 72 73 65 21 3d 30  ssert( pParse!=0
24ad0 20 29 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73   );.  db = pPars
24ad1 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28  e->db;.  assert(
24ad2 20 64 62 21 3d 30 20 29 3b 0a 2f 2a 20 20 69 66   db!=0 );./*  if
24ad3 28 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74  ( db->aDb[0].pBt
24ad4 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 20 2a 2f  ==0 ) return; */
24ad5 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75  .  if( sqlite3Au
24ad6 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
24ad7 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49  SQLITE_TRANSACTI
24ad8 4f 4e 2c 20 22 43 4f 4d 4d 49 54 22 2c 20 30 2c  ON, "COMMIT", 0,
24ad9 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72   0) ){.    retur
24ada 6e 3b 0a 20 20 7d 0a 20 20 76 20 3d 20 73 71 6c  n;.  }.  v = sql
24adb 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
24adc 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a  se);.  if( v ){.
24add 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
24ade 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 75 74 6f  ddOp2(v, OP_Auto
24adf 43 6f 6d 6d 69 74 2c 20 31 2c 20 30 29 3b 0a 20  Commit, 1, 0);. 
24ae0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c   }.}../*.** Roll
24ae1 62 61 63 6b 20 61 20 74 72 61 6e 73 61 63 74 69  back a transacti
24ae2 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  on.*/.SQLITE_PRI
24ae3 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
24ae4 33 52 6f 6c 6c 62 61 63 6b 54 72 61 6e 73 61 63  3RollbackTransac
24ae5 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72  tion(Parse *pPar
24ae6 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  se){.  sqlite3 *
24ae7 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a  db;.  Vdbe *v;..
24ae8 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
24ae9 21 3d 30 20 29 3b 0a 20 20 64 62 20 3d 20 70 50  !=0 );.  db = pP
24aea 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65  arse->db;.  asse
24aeb 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 2f 2a 20  rt( db!=0 );./* 
24aec 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 30 5d 2e   if( db->aDb[0].
24aed 70 42 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  pBt==0 ) return;
24aee 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
24aef 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
24af0 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41  e, SQLITE_TRANSA
24af1 43 54 49 4f 4e 2c 20 22 52 4f 4c 4c 42 41 43 4b  CTION, "ROLLBACK
24af2 22 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20  ", 0, 0) ){.    
24af3 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 76 20  return;.  }.  v 
24af4 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
24af5 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
24af6 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  v ){.    sqlite3
24af7 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
24af8 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c 20 31 2c 20  _AutoCommit, 1, 
24af9 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  1);.  }.}../*.**
24afa 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
24afb 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  s called by the 
24afc 70 61 72 73 65 72 20 77 68 65 6e 20 69 74 20 70  parser when it p
24afd 61 72 73 65 73 20 61 20 63 6f 6d 6d 61 6e 64 20  arses a command 
24afe 74 6f 20 63 72 65 61 74 65 2c 0a 2a 2a 20 72 65  to create,.** re
24aff 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c 62 61 63  lease or rollbac
24b00 6b 20 61 6e 20 53 51 4c 20 73 61 76 65 70 6f 69  k an SQL savepoi
24b01 6e 74 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nt. .*/.SQLITE_P
24b02 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
24b03 74 65 33 53 61 76 65 70 6f 69 6e 74 28 50 61 72  te3Savepoint(Par
24b04 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
24b05 6f 70 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  op, Token *pName
24b06 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  ){.  char *zName
24b07 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
24b08 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e  omToken(pParse->
24b09 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66  db, pName);.  if
24b0a 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 56  ( zName ){.    V
24b0b 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33  dbe *v = sqlite3
24b0c 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
24b0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
24b0e 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
24b0f 4f 4e 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f  ON.    static co
24b10 6e 73 74 20 63 68 61 72 20 2a 61 7a 5b 5d 20 3d  nst char *az[] =
24b11 20 7b 20 22 42 45 47 49 4e 22 2c 20 22 52 45 4c   { "BEGIN", "REL
24b12 45 41 53 45 22 2c 20 22 52 4f 4c 4c 42 41 43 4b  EASE", "ROLLBACK
24b13 22 20 7d 3b 0a 20 20 20 20 61 73 73 65 72 74 28  " };.    assert(
24b14 20 21 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49   !SAVEPOINT_BEGI
24b15 4e 20 26 26 20 53 41 56 45 50 4f 49 4e 54 5f 52  N && SAVEPOINT_R
24b16 45 4c 45 41 53 45 3d 3d 31 20 26 26 20 53 41 56  ELEASE==1 && SAV
24b17 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3d  EPOINT_ROLLBACK=
24b18 3d 32 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20  =2 );.#endif.   
24b19 20 69 66 28 20 21 76 20 7c 7c 20 73 71 6c 69 74   if( !v || sqlit
24b1a 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
24b1b 73 65 2c 20 53 51 4c 49 54 45 5f 53 41 56 45 50  se, SQLITE_SAVEP
24b1c 4f 49 4e 54 2c 20 61 7a 5b 6f 70 5d 2c 20 7a 4e  OINT, az[op], zN
24b1d 61 6d 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 20  ame, 0) ){.     
24b1e 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
24b1f 50 61 72 73 65 2d 3e 64 62 2c 20 7a 4e 61 6d 65  Parse->db, zName
24b20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  );.      return;
24b21 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
24b22 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
24b23 4f 50 5f 53 61 76 65 70 6f 69 6e 74 2c 20 6f 70  OP_Savepoint, op
24b24 2c 20 30 2c 20 30 2c 20 7a 4e 61 6d 65 2c 20 50  , 0, 0, zName, P
24b25 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a  4_DYNAMIC);.  }.
24b26 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75  }../*.** Make su
24b27 72 65 20 74 68 65 20 54 45 4d 50 20 64 61 74 61  re the TEMP data
24b28 62 61 73 65 20 69 73 20 6f 70 65 6e 20 61 6e 64  base is open and
24b29 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 75   available for u
24b2a 73 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74  se.  Return.** t
24b2b 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72  he number of err
24b2c 6f 72 73 2e 20 20 4c 65 61 76 65 20 61 6e 79 20  ors.  Leave any 
24b2d 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69  error messages i
24b2e 6e 20 74 68 65 20 70 50 61 72 73 65 20 73 74 72  n the pParse str
24b2f 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54  ucture..*/.SQLIT
24b30 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
24b31 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74  lite3OpenTempDat
24b32 61 62 61 73 65 28 50 61 72 73 65 20 2a 70 50 61  abase(Parse *pPa
24b33 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  rse){.  sqlite3 
24b34 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
24b35 3b 0a 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b  ;.  if( db->aDb[
24b36 31 5d 2e 70 42 74 3d 3d 30 20 26 26 20 21 70 50  1].pBt==0 && !pP
24b37 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e 20 29 7b  arse->explain ){
24b38 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20  .    int rc;.   
24b39 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 6e   static const in
24b3a 74 20 66 6c 61 67 73 20 3d 20 0a 20 20 20 20 20  t flags = .     
24b3b 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
24b3c 5f 52 45 41 44 57 52 49 54 45 20 7c 0a 20 20 20  _READWRITE |.   
24b3d 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
24b3e 45 4e 5f 43 52 45 41 54 45 20 7c 0a 20 20 20 20  EN_CREATE |.    
24b3f 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
24b40 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c 0a 20 20  N_EXCLUSIVE |.  
24b41 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f          SQLITE_O
24b42 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53  PEN_DELETEONCLOS
24b43 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51  E |.          SQ
24b44 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44  LITE_OPEN_TEMP_D
24b45 42 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  B;..    rc = sql
24b46 69 74 65 33 42 74 72 65 65 46 61 63 74 6f 72 79  ite3BtreeFactory
24b47 28 64 62 2c 20 30 2c 20 30 2c 20 53 51 4c 49 54  (db, 0, 0, SQLIT
24b48 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f  E_DEFAULT_CACHE_
24b49 53 49 5a 45 2c 20 66 6c 61 67 73 2c 0a 20 20 20  SIZE, flags,.   
24b4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24b4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 64                &d
24b4c 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29 3b 0a  b->aDb[1].pBt);.
24b4d 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
24b4e 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73  TE_OK ){.      s
24b4f 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
24b50 50 61 72 73 65 2c 20 22 75 6e 61 62 6c 65 20 74  Parse, "unable t
24b51 6f 20 6f 70 65 6e 20 61 20 74 65 6d 70 6f 72 61  o open a tempora
24b52 72 79 20 64 61 74 61 62 61 73 65 20 22 0a 20 20  ry database ".  
24b53 20 20 20 20 20 20 22 66 69 6c 65 20 66 6f 72 20        "file for 
24b54 73 74 6f 72 69 6e 67 20 74 65 6d 70 6f 72 61 72  storing temporar
24b55 79 20 74 61 62 6c 65 73 22 29 3b 0a 20 20 20 20  y tables");.    
24b56 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 72    pParse->rc = r
24b57 63 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  c;.      return 
24b58 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  1;.    }.    ass
24b59 65 72 74 28 20 28 64 62 2d 3e 66 6c 61 67 73 20  ert( (db->flags 
24b5a 26 20 53 51 4c 49 54 45 5f 49 6e 54 72 61 6e 73  & SQLITE_InTrans
24b5b 29 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 75 74 6f  )==0 || db->auto
24b5c 43 6f 6d 6d 69 74 20 29 3b 0a 20 20 20 20 61 73  Commit );.    as
24b5d 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b 31 5d  sert( db->aDb[1]
24b5e 2e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20  .pSchema );.    
24b5f 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72  sqlite3PagerJour
24b60 6e 61 6c 4d 6f 64 65 28 73 71 6c 69 74 65 33 42  nalMode(sqlite3B
24b61 74 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61 44  treePager(db->aD
24b62 62 5b 31 5d 2e 70 42 74 29 2c 0a 20 20 20 20 20  b[1].pBt),.     
24b63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24b64 20 20 20 20 20 20 20 64 62 2d 3e 64 66 6c 74 4a         db->dfltJ
24b65 6f 75 72 6e 61 6c 4d 6f 64 65 29 3b 0a 20 20 7d  ournalMode);.  }
24b66 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
24b67 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 56  /*.** Generate V
24b68 44 42 45 20 63 6f 64 65 20 74 68 61 74 20 77 69  DBE code that wi
24b69 6c 6c 20 76 65 72 69 66 79 20 74 68 65 20 73 63  ll verify the sc
24b6a 68 65 6d 61 20 63 6f 6f 6b 69 65 20 61 6e 64 20  hema cookie and 
24b6b 73 74 61 72 74 0a 2a 2a 20 61 20 72 65 61 64 2d  start.** a read-
24b6c 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20  transaction for 
24b6d 61 6c 6c 20 6e 61 6d 65 64 20 64 61 74 61 62 61  all named databa
24b6e 73 65 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  se files..**.** 
24b6f 49 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20  It is important 
24b70 74 68 61 74 20 61 6c 6c 20 73 63 68 65 6d 61 20  that all schema 
24b71 63 6f 6f 6b 69 65 73 20 62 65 20 76 65 72 69 66  cookies be verif
24b72 69 65 64 20 61 6e 64 20 61 6c 6c 0a 2a 2a 20 72  ied and all.** r
24b73 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 73  ead transactions
24b74 20 62 65 20 73 74 61 72 74 65 64 20 62 65 66 6f   be started befo
24b75 72 65 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65  re anything else
24b76 20 68 61 70 70 65 6e 73 20 69 6e 0a 2a 2a 20 74   happens in.** t
24b77 68 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d 2e  he VDBE program.
24b78 20 20 42 75 74 20 74 68 69 73 20 72 6f 75 74 69    But this routi
24b79 6e 65 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64  ne can be called
24b7a 20 61 66 74 65 72 20 6d 75 63 68 20 6f 74 68 65   after much othe
24b7b 72 0a 2a 2a 20 63 6f 64 65 20 68 61 73 20 62 65  r.** code has be
24b7c 65 6e 20 67 65 6e 65 72 61 74 65 64 2e 20 20 53  en generated.  S
24b7d 6f 20 68 65 72 65 20 69 73 20 77 68 61 74 20 77  o here is what w
24b7e 65 20 64 6f 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20  e do:.**.** The 
24b7f 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20  first time this 
24b80 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
24b81 64 2c 20 77 65 20 63 6f 64 65 20 61 6e 20 4f 50  d, we code an OP
24b82 5f 47 6f 74 6f 20 74 68 61 74 0a 2a 2a 20 77 69  _Goto that.** wi
24b83 6c 6c 20 6a 75 6d 70 20 74 6f 20 61 20 73 75 62  ll jump to a sub
24b84 72 6f 75 74 69 6e 65 20 61 74 20 74 68 65 20 65  routine at the e
24b85 6e 64 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61  nd of the progra
24b86 6d 2e 20 20 54 68 65 6e 20 77 65 0a 2a 2a 20 72  m.  Then we.** r
24b87 65 63 6f 72 64 20 65 76 65 72 79 20 64 61 74 61  ecord every data
24b88 62 61 73 65 20 74 68 61 74 20 6e 65 65 64 73 20  base that needs 
24b89 69 74 73 20 73 63 68 65 6d 61 20 76 65 72 69 66  its schema verif
24b8a 69 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 70 50  ied in the.** pP
24b8b 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b  arse->cookieMask
24b8c 20 66 69 65 6c 64 2e 20 20 4c 61 74 65 72 2c 20   field.  Later, 
24b8d 61 66 74 65 72 20 61 6c 6c 20 6f 74 68 65 72 20  after all other 
24b8e 63 6f 64 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a  code has been.**
24b8f 20 67 65 6e 65 72 61 74 65 64 2c 20 74 68 65 20   generated, the 
24b90 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20  subroutine that 
24b91 64 6f 65 73 20 74 68 65 20 63 6f 6f 6b 69 65 20  does the cookie 
24b92 76 65 72 69 66 69 63 61 74 69 6f 6e 73 20 61 6e  verifications an
24b93 64 0a 2a 2a 20 73 74 61 72 74 73 20 74 68 65 20  d.** starts the 
24b94 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 77 69 6c  transactions wil
24b95 6c 20 62 65 20 63 6f 64 65 64 20 61 6e 64 20 74  l be coded and t
24b96 68 65 20 4f 50 5f 47 6f 74 6f 20 50 32 20 76 61  he OP_Goto P2 va
24b97 6c 75 65 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 6d  lue.** will be m
24b98 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  ade to point to 
24b99 74 68 61 74 20 73 75 62 72 6f 75 74 69 6e 65 2e  that subroutine.
24b9a 20 20 54 68 65 20 67 65 6e 65 72 61 74 69 6f 6e    The generation
24b9b 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6f 6b 69   of the.** cooki
24b9c 65 20 76 65 72 69 66 69 63 61 74 69 6f 6e 20 73  e verification s
24b9d 75 62 72 6f 75 74 69 6e 65 20 63 6f 64 65 20 68  ubroutine code h
24b9e 61 70 70 65 6e 73 20 69 6e 20 73 71 6c 69 74 65  appens in sqlite
24b9f 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 29 2e  3FinishCoding().
24ba0 0a 2a 2a 0a 2a 2a 20 49 66 20 69 44 62 3c 30 20  .**.** If iDb<0 
24ba1 74 68 65 6e 20 63 6f 64 65 20 74 68 65 20 4f 50  then code the OP
24ba2 5f 47 6f 74 6f 20 6f 6e 6c 79 20 2d 20 64 6f 6e  _Goto only - don
24ba3 27 74 20 73 65 74 20 66 6c 61 67 20 74 6f 20 76  't set flag to v
24ba4 65 72 69 66 79 20 74 68 65 0a 2a 2a 20 73 63 68  erify the.** sch
24ba5 65 6d 61 20 6f 6e 20 61 6e 79 20 64 61 74 61 62  ema on any datab
24ba6 61 73 65 73 2e 20 20 54 68 69 73 20 63 61 6e 20  ases.  This can 
24ba7 62 65 20 75 73 65 64 20 74 6f 20 70 6f 73 69 74  be used to posit
24ba8 69 6f 6e 20 74 68 65 20 4f 50 5f 47 6f 74 6f 0a  ion the OP_Goto.
24ba9 2a 2a 20 65 61 72 6c 79 20 69 6e 20 74 68 65 20  ** early in the 
24baa 63 6f 64 65 2c 20 62 65 66 6f 72 65 20 77 65 20  code, before we 
24bab 6b 6e 6f 77 20 69 66 20 61 6e 79 20 64 61 74 61  know if any data
24bac 62 61 73 65 20 74 61 62 6c 65 73 20 77 69 6c 6c  base tables will
24bad 20 62 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c   be used..*/.SQL
24bae 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
24baf 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69   sqlite3CodeVeri
24bb0 66 79 53 63 68 65 6d 61 28 50 61 72 73 65 20 2a  fySchema(Parse *
24bb1 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44 62 29  pParse, int iDb)
24bb2 7b 0a 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c  {.  Parse *pTopl
24bb3 65 76 65 6c 20 3d 20 73 71 6c 69 74 65 33 50 61  evel = sqlite3Pa
24bb4 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72  rseToplevel(pPar
24bb5 73 65 29 3b 0a 0a 20 20 69 66 28 20 70 54 6f 70  se);..  if( pTop
24bb6 6c 65 76 65 6c 2d 3e 63 6f 6f 6b 69 65 47 6f 74  level->cookieGot
24bb7 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 56 64 62 65  o==0 ){.    Vdbe
24bb8 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   *v = sqlite3Get
24bb9 56 64 62 65 28 70 54 6f 70 6c 65 76 65 6c 29 3b  Vdbe(pToplevel);
24bba 0a 20 20 20 20 69 66 28 20 76 3d 3d 30 20 29 20  .    if( v==0 ) 
24bbb 72 65 74 75 72 6e 3b 20 20 2f 2a 20 54 68 69 73  return;  /* This
24bbc 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 69 66   only happens if
24bbd 20 74 68 65 72 65 20 77 61 73 20 61 20 70 72 69   there was a pri
24bbe 6f 72 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20  or error */.    
24bbf 70 54 6f 70 6c 65 76 65 6c 2d 3e 63 6f 6f 6b 69  pToplevel->cooki
24bc0 65 47 6f 74 6f 20 3d 20 73 71 6c 69 74 65 33 56  eGoto = sqlite3V
24bc1 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
24bc2 47 6f 74 6f 2c 20 30 2c 20 30 29 2b 31 3b 0a 20  Goto, 0, 0)+1;. 
24bc3 20 7d 0a 20 20 69 66 28 20 69 44 62 3e 3d 30 20   }.  if( iDb>=0 
24bc4 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a  ){.    sqlite3 *
24bc5 64 62 20 3d 20 70 54 6f 70 6c 65 76 65 6c 2d 3e  db = pToplevel->
24bc6 64 62 3b 0a 20 20 20 20 69 6e 74 20 6d 61 73 6b  db;.    int mask
24bc7 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  ;..    assert( i
24bc8 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  Db<db->nDb );.  
24bc9 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44    assert( db->aD
24bca 62 5b 69 44 62 5d 2e 70 42 74 21 3d 30 20 7c 7c  b[iDb].pBt!=0 ||
24bcb 20 69 44 62 3d 3d 31 20 29 3b 0a 20 20 20 20 61   iDb==1 );.    a
24bcc 73 73 65 72 74 28 20 69 44 62 3c 53 51 4c 49 54  ssert( iDb<SQLIT
24bcd 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 2b 32  E_MAX_ATTACHED+2
24bce 20 29 3b 0a 20 20 20 20 6d 61 73 6b 20 3d 20 31   );.    mask = 1
24bcf 3c 3c 69 44 62 3b 0a 20 20 20 20 69 66 28 20 28  <<iDb;.    if( (
24bd0 70 54 6f 70 6c 65 76 65 6c 2d 3e 63 6f 6f 6b 69  pToplevel->cooki
24bd1 65 4d 61 73 6b 20 26 20 6d 61 73 6b 29 3d 3d 30  eMask & mask)==0
24bd2 20 29 7b 0a 20 20 20 20 20 20 70 54 6f 70 6c 65   ){.      pTople
24bd3 76 65 6c 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20  vel->cookieMask 
24bd4 7c 3d 20 6d 61 73 6b 3b 0a 20 20 20 20 20 20 70  |= mask;.      p
24bd5 54 6f 70 6c 65 76 65 6c 2d 3e 63 6f 6f 6b 69 65  Toplevel->cookie
24bd6 56 61 6c 75 65 5b 69 44 62 5d 20 3d 20 64 62 2d  Value[iDb] = db-
24bd7 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d  >aDb[iDb].pSchem
24bd8 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65  a->schema_cookie
24bd9 3b 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49  ;.      if( !OMI
24bda 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d  T_TEMPDB && iDb=
24bdb 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  =1 ){.        sq
24bdc 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74  lite3OpenTempDat
24bdd 61 62 61 73 65 28 70 54 6f 70 6c 65 76 65 6c 29  abase(pToplevel)
24bde 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
24bdf 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e    }.}../*.** Gen
24be0 65 72 61 74 65 20 56 44 42 45 20 63 6f 64 65 20  erate VDBE code 
24be1 74 68 61 74 20 70 72 65 70 61 72 65 73 20 66 6f  that prepares fo
24be2 72 20 64 6f 69 6e 67 20 61 6e 20 6f 70 65 72 61  r doing an opera
24be3 74 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 6d 69 67  tion that.** mig
24be4 68 74 20 63 68 61 6e 67 65 20 74 68 65 20 64 61  ht change the da
24be5 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  tabase..**.** Th
24be6 69 73 20 72 6f 75 74 69 6e 65 20 73 74 61 72 74  is routine start
24be7 73 20 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74  s a new transact
24be8 69 6f 6e 20 69 66 20 77 65 20 61 72 65 20 6e 6f  ion if we are no
24be9 74 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e  t already within
24bea 0a 2a 2a 20 61 20 74 72 61 6e 73 61 63 74 69 6f  .** a transactio
24beb 6e 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 6c  n.  If we are al
24bec 72 65 61 64 79 20 77 69 74 68 69 6e 20 61 20 74  ready within a t
24bed 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e  ransaction, then
24bee 20 61 20 63 68 65 63 6b 70 6f 69 6e 74 0a 2a 2a   a checkpoint.**
24bef 20 69 73 20 73 65 74 20 69 66 20 74 68 65 20 73   is set if the s
24bf0 65 74 53 74 61 74 65 6d 65 6e 74 20 70 61 72 61  etStatement para
24bf1 6d 65 74 65 72 20 69 73 20 74 72 75 65 2e 20 20  meter is true.  
24bf2 41 20 63 68 65 63 6b 70 6f 69 6e 74 20 73 68 6f  A checkpoint sho
24bf3 75 6c 64 0a 2a 2a 20 62 65 20 73 65 74 20 66 6f  uld.** be set fo
24bf4 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 68 61  r operations tha
24bf5 74 20 6d 69 67 68 74 20 66 61 69 6c 20 28 64 75  t might fail (du
24bf6 65 20 74 6f 20 61 20 63 6f 6e 73 74 72 61 69 6e  e to a constrain
24bf7 74 29 20 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68  t) part of.** th
24bf8 65 20 77 61 79 20 74 68 72 6f 75 67 68 20 61 6e  e way through an
24bf9 64 20 77 68 69 63 68 20 77 69 6c 6c 20 6e 65 65  d which will nee
24bfa 64 20 74 6f 20 75 6e 64 6f 20 73 6f 6d 65 20 77  d to undo some w
24bfb 72 69 74 65 73 20 77 69 74 68 6f 75 74 20 68 61  rites without ha
24bfc 76 69 6e 67 20 74 6f 0a 2a 2a 20 72 6f 6c 6c 62  ving to.** rollb
24bfd 61 63 6b 20 74 68 65 20 77 68 6f 6c 65 20 74 72  ack the whole tr
24bfe 61 6e 73 61 63 74 69 6f 6e 2e 20 20 46 6f 72 20  ansaction.  For 
24bff 6f 70 65 72 61 74 69 6f 6e 73 20 77 68 65 72 65  operations where
24c00 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 73   all constraints
24c01 0a 2a 2a 20 63 61 6e 20 62 65 20 63 68 65 63 6b  .** can be check
24c02 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 63 68  ed before any ch
24c03 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20 74  anges are made t
24c04 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20  o the database, 
24c05 69 74 20 69 73 20 6e 65 76 65 72 0a 2a 2a 20 6e  it is never.** n
24c06 65 63 65 73 73 61 72 79 20 74 6f 20 75 6e 64 6f  ecessary to undo
24c07 20 61 20 77 72 69 74 65 20 61 6e 64 20 74 68 65   a write and the
24c08 20 63 68 65 63 6b 70 6f 69 6e 74 20 73 68 6f 75   checkpoint shou
24c09 6c 64 20 6e 6f 74 20 62 65 20 73 65 74 2e 0a 2a  ld not be set..*
24c0a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
24c0b 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67   void sqlite3Beg
24c0c 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e  inWriteOperation
24c0d 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
24c0e 69 6e 74 20 73 65 74 53 74 61 74 65 6d 65 6e 74  int setStatement
24c0f 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 50 61  , int iDb){.  Pa
24c10 72 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20 3d  rse *pToplevel =
24c11 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70   sqlite3ParseTop
24c12 6c 65 76 65 6c 28 70 50 61 72 73 65 29 3b 0a 20  level(pParse);. 
24c13 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69   sqlite3CodeVeri
24c14 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c  fySchema(pParse,
24c15 20 69 44 62 29 3b 0a 20 20 70 54 6f 70 6c 65 76   iDb);.  pToplev
24c16 65 6c 2d 3e 77 72 69 74 65 4d 61 73 6b 20 7c 3d  el->writeMask |=
24c17 20 31 3c 3c 69 44 62 3b 0a 20 20 70 54 6f 70 6c   1<<iDb;.  pTopl
24c18 65 76 65 6c 2d 3e 69 73 4d 75 6c 74 69 57 72 69  evel->isMultiWri
24c19 74 65 20 7c 3d 20 73 65 74 53 74 61 74 65 6d 65  te |= setStateme
24c1a 6e 74 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 53 65  nt;.}../* .** Se
24c1b 74 20 74 68 65 20 22 6d 61 79 20 74 68 72 6f 77  t the "may throw
24c1c 20 61 62 6f 72 74 20 65 78 63 65 70 74 69 6f 6e   abort exception
24c1d 22 20 66 6c 61 67 20 66 6f 72 20 74 68 65 20 73  " flag for the s
24c1e 74 61 74 65 6d 65 6e 74 20 63 75 72 72 65 6e 74  tatement current
24c1f 6c 79 20 0a 2a 2a 20 62 65 69 6e 67 20 63 6f 64  ly .** being cod
24c20 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
24c21 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
24c22 65 33 4d 61 79 41 62 6f 72 74 28 50 61 72 73 65  e3MayAbort(Parse
24c23 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 50 61 72   *pParse){.  Par
24c24 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20  se *pToplevel = 
24c25 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c  sqlite3ParseTopl
24c26 65 76 65 6c 28 70 50 61 72 73 65 29 3b 0a 20 20  evel(pParse);.  
24c27 70 54 6f 70 6c 65 76 65 6c 2d 3e 6d 61 79 41 62  pToplevel->mayAb
24c28 6f 72 74 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  ort = 1;.}../*.*
24c29 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f 48 61 6c  * Code an OP_Hal
24c2a 74 20 74 68 61 74 20 63 61 75 73 65 73 20 74 68  t that causes th
24c2b 65 20 76 64 62 65 20 74 6f 20 72 65 74 75 72 6e  e vdbe to return
24c2c 20 61 6e 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54   an SQLITE_CONST
24c2d 52 41 49 4e 54 0a 2a 2a 20 65 72 72 6f 72 2e 20  RAINT.** error. 
24c2e 54 68 65 20 6f 6e 45 72 72 6f 72 20 70 61 72 61  The onError para
24c2f 6d 65 74 65 72 20 64 65 74 65 72 6d 69 6e 65 73  meter determines
24c30 20 77 68 69 63 68 20 28 69 66 20 61 6e 79 29 20   which (if any) 
24c31 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  of the statement
24c32 0a 2a 2a 20 61 6e 64 2f 6f 72 20 63 75 72 72 65  .** and/or curre
24c33 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  nt transaction i
24c34 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a  s rolled back..*
24c35 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
24c36 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 6c   void sqlite3Hal
24c37 74 43 6f 6e 73 74 72 61 69 6e 74 28 50 61 72 73  tConstraint(Pars
24c38 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6f  e *pParse, int o
24c39 6e 45 72 72 6f 72 2c 20 63 68 61 72 20 2a 70 34  nError, char *p4
24c3a 2c 20 69 6e 74 20 70 34 74 79 70 65 29 7b 0a 20  , int p4type){. 
24c3b 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74   Vdbe *v = sqlit
24c3c 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
24c3d 29 3b 0a 20 20 69 66 28 20 6f 6e 45 72 72 6f 72  );.  if( onError
24c3e 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b 0a 20 20  ==OE_Abort ){.  
24c3f 20 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72    sqlite3MayAbor
24c40 74 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 20  t(pParse);.  }. 
24c41 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24c42 70 34 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53  p4(v, OP_Halt, S
24c43 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
24c44 2c 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 20 70 34  , onError, 0, p4
24c45 2c 20 70 34 74 79 70 65 29 3b 0a 7d 0a 0a 2f 2a  , p4type);.}../*
24c46 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65  .** Check to see
24c47 20 69 66 20 70 49 6e 64 65 78 20 75 73 65 73 20   if pIndex uses 
24c48 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  the collating se
24c49 71 75 65 6e 63 65 20 70 43 6f 6c 6c 2e 20 20 52  quence pColl.  R
24c4a 65 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69 66  eturn.** true if
24c4b 20 69 74 20 64 6f 65 73 20 61 6e 64 20 66 61 6c   it does and fal
24c4c 73 65 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f  se if it does no
24c4d 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  t..*/.#ifndef SQ
24c4e 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45  LITE_OMIT_REINDE
24c4f 58 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6c  X.static int col
24c50 6c 61 74 69 6f 6e 4d 61 74 63 68 28 63 6f 6e 73  lationMatch(cons
24c51 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 2c 20 49  t char *zColl, I
24c52 6e 64 65 78 20 2a 70 49 6e 64 65 78 29 7b 0a 20  ndex *pIndex){. 
24c53 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74   int i;.  assert
24c54 28 20 7a 43 6f 6c 6c 21 3d 30 20 29 3b 0a 20 20  ( zColl!=0 );.  
24c55 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 6e 64 65  for(i=0; i<pInde
24c56 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29  x->nColumn; i++)
24c57 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
24c58 20 2a 7a 20 3d 20 70 49 6e 64 65 78 2d 3e 61 7a   *z = pIndex->az
24c59 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 61 73 73  Coll[i];.    ass
24c5a 65 72 74 28 20 7a 21 3d 30 20 29 3b 0a 20 20 20  ert( z!=0 );.   
24c5b 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53   if( 0==sqlite3S
24c5c 74 72 49 43 6d 70 28 7a 2c 20 7a 43 6f 6c 6c 29  trICmp(z, zColl)
24c5d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
24c5e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
24c5f 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64  return 0;.}.#end
24c60 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 6d 70  if../*.** Recomp
24c61 75 74 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20  ute all indices 
24c62 6f 66 20 70 54 61 62 20 74 68 61 74 20 75 73 65  of pTab that use
24c63 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73   the collating s
24c64 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 2e 0a 2a  equence pColl..*
24c65 2a 20 49 66 20 70 43 6f 6c 6c 3d 3d 30 20 74 68  * If pColl==0 th
24c66 65 6e 20 72 65 63 6f 6d 70 75 74 65 20 61 6c 6c  en recompute all
24c67 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54 61 62   indices of pTab
24c68 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
24c69 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58  ITE_OMIT_REINDEX
24c6a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 69  .static void rei
24c6b 6e 64 65 78 54 61 62 6c 65 28 50 61 72 73 65 20  ndexTable(Parse 
24c6c 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a  *pParse, Table *
24c6d 70 54 61 62 2c 20 63 68 61 72 20 63 6f 6e 73 74  pTab, char const
24c6e 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 49 6e 64 65   *zColl){.  Inde
24c6f 78 20 2a 70 49 6e 64 65 78 3b 20 20 20 20 20 20  x *pIndex;      
24c70 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e          /* An in
24c71 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77  dex associated w
24c72 69 74 68 20 70 54 61 62 20 2a 2f 0a 0a 20 20 66  ith pTab */..  f
24c73 6f 72 28 70 49 6e 64 65 78 3d 70 54 61 62 2d 3e  or(pIndex=pTab->
24c74 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 3b 20  pIndex; pIndex; 
24c75 70 49 6e 64 65 78 3d 70 49 6e 64 65 78 2d 3e 70  pIndex=pIndex->p
24c76 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 7a  Next){.    if( z
24c77 43 6f 6c 6c 3d 3d 30 20 7c 7c 20 63 6f 6c 6c 61  Coll==0 || colla
24c78 74 69 6f 6e 4d 61 74 63 68 28 7a 43 6f 6c 6c 2c  tionMatch(zColl,
24c79 20 70 49 6e 64 65 78 29 20 29 7b 0a 20 20 20 20   pIndex) ){.    
24c7a 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69    int iDb = sqli
24c7b 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
24c7c 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61  (pParse->db, pTa
24c7d 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20  b->pSchema);.   
24c7e 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57     sqlite3BeginW
24c7f 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50  riteOperation(pP
24c80 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20  arse, 0, iDb);. 
24c81 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 66 69       sqlite3Refi
24c82 6c 6c 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  llIndex(pParse, 
24c83 70 49 6e 64 65 78 2c 20 2d 31 29 3b 0a 20 20 20  pIndex, -1);.   
24c84 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a   }.  }.}.#endif.
24c85 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 6d 70 75 74 65  ./*.** Recompute
24c86 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20   all indices of 
24c87 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 61 6c  all tables in al
24c88 6c 20 64 61 74 61 62 61 73 65 73 20 77 68 65 72  l databases wher
24c89 65 20 74 68 65 0a 2a 2a 20 69 6e 64 69 63 65 73  e the.** indices
24c8a 20 75 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69   use the collati
24c8b 6e 67 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c  ng sequence pCol
24c8c 6c 2e 20 20 49 66 20 70 43 6f 6c 6c 3d 3d 30 20  l.  If pColl==0 
24c8d 74 68 65 6e 20 72 65 63 6f 6d 70 75 74 65 0a 2a  then recompute.*
24c8e 2a 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 65 76  * all indices ev
24c8f 65 72 79 77 68 65 72 65 2e 0a 2a 2f 0a 23 69 66  erywhere..*/.#if
24c90 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
24c91 5f 52 45 49 4e 44 45 58 0a 73 74 61 74 69 63 20  _REINDEX.static 
24c92 76 6f 69 64 20 72 65 69 6e 64 65 78 44 61 74 61  void reindexData
24c93 62 61 73 65 73 28 50 61 72 73 65 20 2a 70 50 61  bases(Parse *pPa
24c94 72 73 65 2c 20 63 68 61 72 20 63 6f 6e 73 74 20  rse, char const 
24c95 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 44 62 20 2a 70  *zColl){.  Db *p
24c96 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
24c97 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67         /* A sing
24c98 6c 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  le database */. 
24c99 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
24c9a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
24c9b 54 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 64  The database ind
24c9c 65 78 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73  ex number */.  s
24c9d 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
24c9e 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 54 68  rse->db;   /* Th
24c9f 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
24ca0 63 74 69 6f 6e 20 2a 2f 0a 20 20 48 61 73 68 45  ction */.  HashE
24ca1 6c 65 6d 20 2a 6b 3b 20 20 20 20 20 20 20 20 20  lem *k;         
24ca2 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f         /* For lo
24ca3 6f 70 69 6e 67 20 6f 76 65 72 20 74 61 62 6c 65  oping over table
24ca4 73 20 69 6e 20 70 44 62 20 2a 2f 0a 20 20 54 61  s in pDb */.  Ta
24ca5 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20  ble *pTab;      
24ca6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 74            /* A t
24ca7 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61  able in the data
24ca8 62 61 73 65 20 2a 2f 0a 0a 20 20 66 6f 72 28 69  base */..  for(i
24ca9 44 62 3d 30 2c 20 70 44 62 3d 64 62 2d 3e 61 44  Db=0, pDb=db->aD
24caa 62 3b 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20  b; iDb<db->nDb; 
24cab 69 44 62 2b 2b 2c 20 70 44 62 2b 2b 29 7b 0a 20  iDb++, pDb++){. 
24cac 20 20 20 61 73 73 65 72 74 28 20 70 44 62 21 3d     assert( pDb!=
24cad 30 20 29 3b 0a 20 20 20 20 66 6f 72 28 6b 3d 73  0 );.    for(k=s
24cae 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26  qliteHashFirst(&
24caf 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 74 62  pDb->pSchema->tb
24cb0 6c 48 61 73 68 29 3b 20 20 6b 3b 20 6b 3d 73 71  lHash);  k; k=sq
24cb1 6c 69 74 65 48 61 73 68 4e 65 78 74 28 6b 29 29  liteHashNext(k))
24cb2 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 28  {.      pTab = (
24cb3 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 48 61 73  Table*)sqliteHas
24cb4 68 44 61 74 61 28 6b 29 3b 0a 20 20 20 20 20 20  hData(k);.      
24cb5 72 65 69 6e 64 65 78 54 61 62 6c 65 28 70 50 61  reindexTable(pPa
24cb6 72 73 65 2c 20 70 54 61 62 2c 20 7a 43 6f 6c 6c  rse, pTab, zColl
24cb7 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23  );.    }.  }.}.#
24cb8 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  endif../*.** Gen
24cb9 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74  erate code for t
24cba 68 65 20 52 45 49 4e 44 45 58 20 63 6f 6d 6d 61  he REINDEX comma
24cbb 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  nd..**.**       
24cbc 20 52 45 49 4e 44 45 58 20 20 20 20 20 20 20 20   REINDEX        
24cbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24cbe 20 20 20 20 2d 2d 20 31 0a 2a 2a 20 20 20 20 20      -- 1.**     
24cbf 20 20 20 52 45 49 4e 44 45 58 20 20 3c 63 6f 6c     REINDEX  <col
24cc0 6c 61 74 69 6f 6e 3e 20 20 20 20 20 20 20 20 20  lation>         
24cc1 20 20 20 20 20 20 2d 2d 20 32 0a 2a 2a 20 20 20        -- 2.**   
24cc2 20 20 20 20 20 52 45 49 4e 44 45 58 20 20 3f 3c       REINDEX  ?<
24cc3 64 61 74 61 62 61 73 65 3e 2e 3f 3c 74 61 62 6c  database>.?<tabl
24cc4 65 6e 61 6d 65 3e 20 20 2d 2d 20 33 0a 2a 2a 20  ename>  -- 3.** 
24cc5 20 20 20 20 20 20 20 52 45 49 4e 44 45 58 20 20         REINDEX  
24cc6 3f 3c 64 61 74 61 62 61 73 65 3e 2e 3f 3c 69 6e  ?<database>.?<in
24cc7 64 65 78 6e 61 6d 65 3e 20 20 2d 2d 20 34 0a 2a  dexname>  -- 4.*
24cc8 2a 0a 2a 2a 20 46 6f 72 6d 20 31 20 63 61 75 73  *.** Form 1 caus
24cc9 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 69  es all indices i
24cca 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64  n all attached d
24ccb 61 74 61 62 61 73 65 73 20 74 6f 20 62 65 20 72  atabases to be r
24ccc 65 62 75 69 6c 74 2e 0a 2a 2a 20 46 6f 72 6d 20  ebuilt..** Form 
24ccd 32 20 72 65 62 75 69 6c 64 73 20 61 6c 6c 20 69  2 rebuilds all i
24cce 6e 64 69 63 65 73 20 69 6e 20 61 6c 6c 20 64 61  ndices in all da
24ccf 74 61 62 61 73 65 73 20 74 68 61 74 20 75 73 65  tabases that use
24cd0 20 74 68 65 20 6e 61 6d 65 64 0a 2a 2a 20 63 6f   the named.** co
24cd1 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llating function
24cd2 2e 20 20 46 6f 72 6d 73 20 33 20 61 6e 64 20 34  .  Forms 3 and 4
24cd3 20 72 65 62 75 69 6c 64 20 74 68 65 20 6e 61 6d   rebuild the nam
24cd4 65 64 20 69 6e 64 65 78 20 6f 72 20 61 6c 6c 0a  ed index or all.
24cd5 2a 2a 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63  ** indices assoc
24cd6 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 6e  iated with the n
24cd7 61 6d 65 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23  amed table..*/.#
24cd8 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
24cd9 49 54 5f 52 45 49 4e 44 45 58 0a 53 51 4c 49 54  IT_REINDEX.SQLIT
24cda 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
24cdb 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28 50 61  qlite3Reindex(Pa
24cdc 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b  rse *pParse, Tok
24cdd 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 54 6f 6b 65  en *pName1, Toke
24cde 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a 20 20 43 6f  n *pName2){.  Co
24cdf 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20  llSeq *pColl;   
24ce0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
24ce1 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
24ce2 74 6f 20 62 65 20 72 65 69 6e 64 65 78 65 64 2c  to be reindexed,
24ce3 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 68   or NULL */.  ch
24ce4 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20  ar *z;          
24ce5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
24ce6 65 20 6f 66 20 61 20 74 61 62 6c 65 20 6f 72 20  e of a table or 
24ce7 69 6e 64 65 78 20 2a 2f 0a 20 20 63 6f 6e 73 74  index */.  const
24ce8 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20   char *zDb;     
24ce9 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
24cea 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a  f the database *
24ceb 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  /.  Table *pTab;
24cec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24ced 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e 20 74 68  /* A table in th
24cee 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  e database */.  
24cef 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 20  Index *pIndex;  
24cf0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
24cf1 6e 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61 74  n index associat
24cf2 65 64 20 77 69 74 68 20 70 54 61 62 20 2a 2f 0a  ed with pTab */.
24cf3 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
24cf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
24cf5 20 54 68 65 20 64 61 74 61 62 61 73 65 20 69 6e   The database in
24cf6 64 65 78 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  dex number */.  
24cf7 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
24cf8 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 54  arse->db;   /* T
24cf9 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
24cfa 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65  ection */.  Toke
24cfb 6e 20 2a 70 4f 62 6a 4e 61 6d 65 3b 20 20 20 20  n *pObjName;    
24cfc 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
24cfd 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20  of the table or 
24cfe 69 6e 64 65 78 20 74 6f 20 62 65 20 72 65 69 6e  index to be rein
24cff 64 65 78 65 64 20 2a 2f 0a 0a 20 20 2f 2a 20 52  dexed */..  /* R
24d00 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73 65  ead the database
24d01 20 73 63 68 65 6d 61 2e 20 49 66 20 61 6e 20 65   schema. If an e
24d02 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65 61  rror occurs, lea
24d03 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  ve an error mess
24d04 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f 64  age.  ** and cod
24d05 65 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20  e in pParse and 
24d06 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a  return NULL. */.
24d07 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
24d08 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65  =sqlite3ReadSche
24d09 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20  ma(pParse) ){.  
24d0a 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
24d0b 20 69 66 28 20 70 4e 61 6d 65 31 3d 3d 30 20 29   if( pName1==0 )
24d0c 7b 0a 20 20 20 20 72 65 69 6e 64 65 78 44 61 74  {.    reindexDat
24d0d 61 62 61 73 65 73 28 70 50 61 72 73 65 2c 20 30  abases(pParse, 0
24d0e 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
24d0f 20 7d 65 6c 73 65 20 69 66 28 20 4e 45 56 45 52   }else if( NEVER
24d10 28 70 4e 61 6d 65 32 3d 3d 30 29 20 7c 7c 20 70  (pName2==0) || p
24d11 4e 61 6d 65 32 2d 3e 7a 3d 3d 30 20 29 7b 0a 20  Name2->z==0 ){. 
24d12 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 0a     char *zColl;.
24d13 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d      assert( pNam
24d14 65 31 2d 3e 7a 20 29 3b 0a 20 20 20 20 7a 43 6f  e1->z );.    zCo
24d15 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  ll = sqlite3Name
24d16 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65  FromToken(pParse
24d17 2d 3e 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20  ->db, pName1);. 
24d18 20 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20 29 20     if( !zColl ) 
24d19 72 65 74 75 72 6e 3b 0a 20 20 20 20 70 43 6f 6c  return;.    pCol
24d1a 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43  l = sqlite3FindC
24d1b 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64  ollSeq(db, ENC(d
24d1c 62 29 2c 20 7a 43 6f 6c 6c 2c 20 30 29 3b 0a 20  b), zColl, 0);. 
24d1d 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a     if( pColl ){.
24d1e 20 20 20 20 20 20 72 65 69 6e 64 65 78 44 61 74        reindexDat
24d1f 61 62 61 73 65 73 28 70 50 61 72 73 65 2c 20 7a  abases(pParse, z
24d20 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c  Coll);.      sql
24d21 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
24d22 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 72 65 74  Coll);.      ret
24d23 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  urn;.    }.    s
24d24 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
24d25 20 7a 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 20 20 69   zColl);.  }.  i
24d26 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50  Db = sqlite3TwoP
24d27 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20  artName(pParse, 
24d28 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20  pName1, pName2, 
24d29 26 70 4f 62 6a 4e 61 6d 65 29 3b 0a 20 20 69 66  &pObjName);.  if
24d2a 28 20 69 44 62 3c 30 20 29 20 72 65 74 75 72 6e  ( iDb<0 ) return
24d2b 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e  ;.  z = sqlite3N
24d2c 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
24d2d 20 70 4f 62 6a 4e 61 6d 65 29 3b 0a 20 20 69 66   pObjName);.  if
24d2e 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( z==0 ) return;
24d2f 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62  .  zDb = db->aDb
24d30 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 70  [iDb].zName;.  p
24d31 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  Tab = sqlite3Fin
24d32 64 54 61 62 6c 65 28 64 62 2c 20 7a 2c 20 7a 44  dTable(db, z, zD
24d33 62 29 3b 0a 20 20 69 66 28 20 70 54 61 62 20 29  b);.  if( pTab )
24d34 7b 0a 20 20 20 20 72 65 69 6e 64 65 78 54 61 62  {.    reindexTab
24d35 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  le(pParse, pTab,
24d36 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
24d37 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20  DbFree(db, z);. 
24d38 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
24d39 20 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65   pIndex = sqlite
24d3a 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a  3FindIndex(db, z
24d3b 2c 20 7a 44 62 29 3b 0a 20 20 73 71 6c 69 74 65  , zDb);.  sqlite
24d3c 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a  3DbFree(db, z);.
24d3d 20 20 69 66 28 20 70 49 6e 64 65 78 20 29 7b 0a    if( pIndex ){.
24d3e 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e      sqlite3Begin
24d3f 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70  WriteOperation(p
24d40 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a  Parse, 0, iDb);.
24d41 20 20 20 20 73 71 6c 69 74 65 33 52 65 66 69 6c      sqlite3Refil
24d42 6c 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70  lIndex(pParse, p
24d43 49 6e 64 65 78 2c 20 2d 31 29 3b 0a 20 20 20 20  Index, -1);.    
24d44 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 73 71  return;.  }.  sq
24d45 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
24d46 61 72 73 65 2c 20 22 75 6e 61 62 6c 65 20 74 6f  arse, "unable to
24d47 20 69 64 65 6e 74 69 66 79 20 74 68 65 20 6f 62   identify the ob
24d48 6a 65 63 74 20 74 6f 20 62 65 20 72 65 69 6e 64  ject to be reind
24d49 65 78 65 64 22 29 3b 0a 7d 0a 23 65 6e 64 69 66  exed");.}.#endif
24d4a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
24d4b 20 64 79 6e 61 6d 69 63 6c 79 20 61 6c 6c 6f 63   dynamicly alloc
24d4c 61 74 65 64 20 4b 65 79 49 6e 66 6f 20 73 74 72  ated KeyInfo str
24d4d 75 63 74 75 72 65 20 74 68 61 74 20 63 61 6e 20  ucture that can 
24d4e 62 65 20 75 73 65 64 0a 2a 2a 20 77 69 74 68 20  be used.** with 
24d4f 4f 50 5f 4f 70 65 6e 52 65 61 64 20 6f 72 20 4f  OP_OpenRead or O
24d50 50 5f 4f 70 65 6e 57 72 69 74 65 20 74 6f 20 61  P_OpenWrite to a
24d51 63 63 65 73 73 20 64 61 74 61 62 61 73 65 20 69  ccess database i
24d52 6e 64 65 78 20 70 49 64 78 2e 0a 2a 2a 0a 2a 2a  ndex pIdx..**.**
24d53 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
24d54 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
24d55 20 6e 65 77 20 73 74 72 75 63 74 75 72 65 20 69   new structure i
24d56 73 20 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74  s returned. In t
24d57 68 69 73 20 63 61 73 65 0a 2a 2a 20 74 68 65 20  his case.** the 
24d58 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e  caller is respon
24d59 73 69 62 6c 65 20 66 6f 72 20 63 61 6c 6c 69 6e  sible for callin
24d5a 67 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28  g sqlite3DbFree(
24d5b 64 62 2c 20 29 20 6f 6e 20 74 68 65 20 72 65 74  db, ) on the ret
24d5c 75 72 6e 65 64 20 0a 2a 2a 20 70 6f 69 6e 74 65  urned .** pointe
24d5d 72 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  r. If an error o
24d5e 63 63 75 72 73 20 28 6f 75 74 20 6f 66 20 6d 65  ccurs (out of me
24d5f 6d 6f 72 79 20 6f 72 20 6d 69 73 73 69 6e 67 20  mory or missing 
24d60 63 6f 6c 6c 61 74 69 6f 6e 20 0a 2a 2a 20 73 65  collation .** se
24d61 71 75 65 6e 63 65 29 2c 20 4e 55 4c 4c 20 69 73  quence), NULL is
24d62 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68   returned and th
24d63 65 20 73 74 61 74 65 20 6f 66 20 70 50 61 72 73  e state of pPars
24d64 65 20 75 70 64 61 74 65 64 20 74 6f 20 72 65 66  e updated to ref
24d65 6c 65 63 74 0a 2a 2a 20 74 68 65 20 65 72 72 6f  lect.** the erro
24d66 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
24d67 56 41 54 45 20 4b 65 79 49 6e 66 6f 20 2a 73 71  VATE KeyInfo *sq
24d68 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66  lite3IndexKeyinf
24d69 6f 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  o(Parse *pParse,
24d6a 20 49 6e 64 65 78 20 2a 70 49 64 78 29 7b 0a 20   Index *pIdx){. 
24d6b 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 43   int i;.  int nC
24d6c 6f 6c 20 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75  ol = pIdx->nColu
24d6d 6d 6e 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 73  mn;.  int nBytes
24d6e 20 3d 20 73 69 7a 65 6f 66 28 4b 65 79 49 6e 66   = sizeof(KeyInf
24d6f 6f 29 20 2b 20 28 6e 43 6f 6c 2d 31 29 2a 73 69  o) + (nCol-1)*si
24d70 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 20 2b  zeof(CollSeq*) +
24d71 20 6e 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33   nCol;.  sqlite3
24d72 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
24d73 62 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  b;.  KeyInfo *pK
24d74 65 79 20 3d 20 28 4b 65 79 49 6e 66 6f 20 2a 29  ey = (KeyInfo *)
24d75 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
24d76 65 72 6f 28 64 62 2c 20 6e 42 79 74 65 73 29 3b  ero(db, nBytes);
24d77 0a 0a 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a  ..  if( pKey ){.
24d78 20 20 20 20 70 4b 65 79 2d 3e 64 62 20 3d 20 70      pKey->db = p
24d79 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 70  Parse->db;.    p
24d7a 4b 65 79 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20  Key->aSortOrder 
24d7b 3d 20 28 75 38 20 2a 29 26 28 70 4b 65 79 2d 3e  = (u8 *)&(pKey->
24d7c 61 43 6f 6c 6c 5b 6e 43 6f 6c 5d 29 3b 0a 20 20  aColl[nCol]);.  
24d7d 20 20 61 73 73 65 72 74 28 20 26 70 4b 65 79 2d    assert( &pKey-
24d7e 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6e 43 6f 6c  >aSortOrder[nCol
24d7f 5d 3d 3d 26 28 28 28 75 38 20 2a 29 70 4b 65 79  ]==&(((u8 *)pKey
24d80 29 5b 6e 42 79 74 65 73 5d 29 20 29 3b 0a 20 20  )[nBytes]) );.  
24d81 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f    for(i=0; i<nCo
24d82 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63  l; i++){.      c
24d83 68 61 72 20 2a 7a 43 6f 6c 6c 20 3d 20 70 49 64  har *zColl = pId
24d84 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a 20 20  x->azColl[i];.  
24d85 20 20 20 20 61 73 73 65 72 74 28 20 7a 43 6f 6c      assert( zCol
24d86 6c 20 29 3b 0a 20 20 20 20 20 20 70 4b 65 79 2d  l );.      pKey-
24d87 3e 61 43 6f 6c 6c 5b 69 5d 20 3d 20 73 71 6c 69  >aColl[i] = sqli
24d88 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71  te3LocateCollSeq
24d89 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 29 3b  (pParse, zColl);
24d8a 0a 20 20 20 20 20 20 70 4b 65 79 2d 3e 61 53 6f  .      pKey->aSo
24d8b 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 70 49 64  rtOrder[i] = pId
24d8c 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d  x->aSortOrder[i]
24d8d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4b 65 79  ;.    }.    pKey
24d8e 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29  ->nField = (u16)
24d8f 6e 43 6f 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  nCol;.  }..  if(
24d90 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b   pParse->nErr ){
24d91 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
24d92 65 65 28 64 62 2c 20 70 4b 65 79 29 3b 0a 20 20  ee(db, pKey);.  
24d93 20 20 70 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 0a    pKey = 0;.  }.
24d94 20 20 72 65 74 75 72 6e 20 70 4b 65 79 3b 0a 7d    return pKey;.}
24d95 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
24d96 2a 20 45 6e 64 20 6f 66 20 62 75 69 6c 64 2e 63  * End of build.c
24d97 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
24d98 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24d99 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24d9a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
24d9b 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 63 61 6c  * Begin file cal
24d9c 6c 62 61 63 6b 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  lback.c ********
24d9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24d9e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24d9f 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4d 61 79  /./*.** 2005 May
24da0 20 32 33 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61   23 .**.** The a
24da1 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
24da2 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
24da3 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
24da4 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
24da5 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
24da6 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
24da7 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
24da8 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
24da9 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
24daa 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
24dab 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
24dac 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
24dad 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
24dae 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
24daf 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
24db0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
24db1 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
24db2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24db3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24db4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24db5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24db6 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ***.**.** This f
24db7 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 66 75 6e  ile contains fun
24db8 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 61  ctions used to a
24db9 63 63 65 73 73 20 74 68 65 20 69 6e 74 65 72 6e  ccess the intern
24dba 61 6c 20 68 61 73 68 20 74 61 62 6c 65 73 0a 2a  al hash tables.*
24dbb 2a 20 6f 66 20 75 73 65 72 20 64 65 66 69 6e 65  * of user define
24dbc 64 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20  d functions and 
24dbd 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
24dbe 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  ces..**.** $Id: 
24dbf 63 61 6c 6c 62 61 63 6b 2e 63 2c 76 20 31 2e 34  callback.c,v 1.4
24dc0 32 20 32 30 30 39 2f 30 36 2f 31 37 20 30 30 3a  2 2009/06/17 00:
24dc1 33 35 3a 33 31 20 64 72 68 20 45 78 70 20 24 0a  35:31 drh Exp $.
24dc2 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b  */.../*.** Invok
24dc3 65 20 74 68 65 20 27 63 6f 6c 6c 61 74 69 6f 6e  e the 'collation
24dc4 20 6e 65 65 64 65 64 27 20 63 61 6c 6c 62 61 63   needed' callbac
24dc5 6b 20 74 6f 20 72 65 71 75 65 73 74 20 61 20 63  k to request a c
24dc6 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
24dc7 65 0a 2a 2a 20 69 6e 20 74 68 65 20 65 6e 63 6f  e.** in the enco
24dc8 64 69 6e 67 20 65 6e 63 20 6f 66 20 6e 61 6d 65  ding enc of name
24dc9 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e   zName, length n
24dca 4e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  Name..*/.static 
24dcb 76 6f 69 64 20 63 61 6c 6c 43 6f 6c 6c 4e 65 65  void callCollNee
24dcc 64 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ded(sqlite3 *db,
24dcd 20 69 6e 74 20 65 6e 63 2c 20 63 6f 6e 73 74 20   int enc, const 
24dce 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20  char *zName){.  
24dcf 61 73 73 65 72 74 28 20 21 64 62 2d 3e 78 43 6f  assert( !db->xCo
24dd0 6c 6c 4e 65 65 64 65 64 20 7c 7c 20 21 64 62 2d  llNeeded || !db-
24dd1 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 20 29  >xCollNeeded16 )
24dd2 3b 0a 20 20 69 66 28 20 64 62 2d 3e 78 43 6f 6c  ;.  if( db->xCol
24dd3 6c 4e 65 65 64 65 64 20 29 7b 0a 20 20 20 20 63  lNeeded ){.    c
24dd4 68 61 72 20 2a 7a 45 78 74 65 72 6e 61 6c 20 3d  har *zExternal =
24dd5 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
24dd6 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  (db, zName);.   
24dd7 20 69 66 28 20 21 7a 45 78 74 65 72 6e 61 6c 20   if( !zExternal 
24dd8 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 64 62  ) return;.    db
24dd9 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 28 64 62  ->xCollNeeded(db
24dda 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67  ->pCollNeededArg
24ddb 2c 20 64 62 2c 20 65 6e 63 2c 20 7a 45 78 74 65  , db, enc, zExte
24ddc 72 6e 61 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74  rnal);.    sqlit
24ddd 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 78  e3DbFree(db, zEx
24dde 74 65 72 6e 61 6c 29 3b 0a 20 20 7d 0a 23 69 66  ternal);.  }.#if
24ddf 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
24de0 5f 55 54 46 31 36 0a 20 20 69 66 28 20 64 62 2d  _UTF16.  if( db-
24de1 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 20 29  >xCollNeeded16 )
24de2 7b 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74  {.    char const
24de3 20 2a 7a 45 78 74 65 72 6e 61 6c 3b 0a 20 20 20   *zExternal;.   
24de4 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
24de5 70 54 6d 70 20 3d 20 73 71 6c 69 74 65 33 56 61  pTmp = sqlite3Va
24de6 6c 75 65 4e 65 77 28 64 62 29 3b 0a 20 20 20 20  lueNew(db);.    
24de7 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53  sqlite3ValueSetS
24de8 74 72 28 70 54 6d 70 2c 20 2d 31 2c 20 7a 4e 61  tr(pTmp, -1, zNa
24de9 6d 65 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  me, SQLITE_UTF8,
24dea 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
24deb 0a 20 20 20 20 7a 45 78 74 65 72 6e 61 6c 20 3d  .    zExternal =
24dec 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78   sqlite3ValueTex
24ded 74 28 70 54 6d 70 2c 20 53 51 4c 49 54 45 5f 55  t(pTmp, SQLITE_U
24dee 54 46 31 36 4e 41 54 49 56 45 29 3b 0a 20 20 20  TF16NATIVE);.   
24def 20 69 66 28 20 7a 45 78 74 65 72 6e 61 6c 20 29   if( zExternal )
24df0 7b 0a 20 20 20 20 20 20 64 62 2d 3e 78 43 6f 6c  {.      db->xCol
24df1 6c 4e 65 65 64 65 64 31 36 28 64 62 2d 3e 70 43  lNeeded16(db->pC
24df2 6f 6c 6c 4e 65 65 64 65 64 41 72 67 2c 20 64 62  ollNeededArg, db
24df3 2c 20 28 69 6e 74 29 45 4e 43 28 64 62 29 2c 20  , (int)ENC(db), 
24df4 7a 45 78 74 65 72 6e 61 6c 29 3b 0a 20 20 20 20  zExternal);.    
24df5 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c  }.    sqlite3Val
24df6 75 65 46 72 65 65 28 70 54 6d 70 29 3b 0a 20 20  ueFree(pTmp);.  
24df7 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  }.#endif.}../*.*
24df8 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
24df9 73 20 63 61 6c 6c 65 64 20 69 66 20 74 68 65 20  s called if the 
24dfa 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72  collation factor
24dfb 79 20 66 61 69 6c 73 20 74 6f 20 64 65 6c 69 76  y fails to deliv
24dfc 65 72 20 61 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f  er a.** collatio
24dfd 6e 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20 74 68  n function in th
24dfe 65 20 62 65 73 74 20 65 6e 63 6f 64 69 6e 67 20  e best encoding 
24dff 62 75 74 20 74 68 65 72 65 20 6d 61 79 20 62 65  but there may be
24e00 20 6f 74 68 65 72 20 76 65 72 73 69 6f 6e 73 0a   other versions.
24e01 2a 2a 20 6f 66 20 74 68 69 73 20 63 6f 6c 6c 61  ** of this colla
24e02 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 28 66  tion function (f
24e03 6f 72 20 6f 74 68 65 72 20 74 65 78 74 20 65 6e  or other text en
24e04 63 6f 64 69 6e 67 73 29 20 61 76 61 69 6c 61 62  codings) availab
24e05 6c 65 2e 20 55 73 65 20 6f 6e 65 0a 2a 2a 20 6f  le. Use one.** o
24e06 66 20 74 68 65 73 65 20 69 6e 73 74 65 61 64 20  f these instead 
24e07 69 66 20 74 68 65 79 20 65 78 69 73 74 2e 20 41  if they exist. A
24e08 76 6f 69 64 20 61 20 55 54 46 2d 38 20 3c 2d 3e  void a UTF-8 <->
24e09 20 55 54 46 2d 31 36 20 63 6f 6e 76 65 72 73 69   UTF-16 conversi
24e0a 6f 6e 20 69 66 0a 2a 2a 20 70 6f 73 73 69 62 6c  on if.** possibl
24e0b 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
24e0c 20 73 79 6e 74 68 43 6f 6c 6c 53 65 71 28 73 71   synthCollSeq(sq
24e0d 6c 69 74 65 33 20 2a 64 62 2c 20 43 6f 6c 6c 53  lite3 *db, CollS
24e0e 65 71 20 2a 70 43 6f 6c 6c 29 7b 0a 20 20 43 6f  eq *pColl){.  Co
24e0f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 32 3b 0a 20  llSeq *pColl2;. 
24e10 20 63 68 61 72 20 2a 7a 20 3d 20 70 43 6f 6c 6c   char *z = pColl
24e11 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 69  ->zName;.  int i
24e12 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.  static const
24e13 20 75 38 20 61 45 6e 63 5b 5d 20 3d 20 7b 20 53   u8 aEnc[] = { S
24e14 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53  QLITE_UTF16BE, S
24e15 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c 20 53  QLITE_UTF16LE, S
24e16 51 4c 49 54 45 5f 55 54 46 38 20 7d 3b 0a 20 20  QLITE_UTF8 };.  
24e17 66 6f 72 28 69 3d 30 3b 20 69 3c 33 3b 20 69 2b  for(i=0; i<3; i+
24e18 2b 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 32 20 3d  +){.    pColl2 =
24e19 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c   sqlite3FindColl
24e1a 53 65 71 28 64 62 2c 20 61 45 6e 63 5b 69 5d 2c  Seq(db, aEnc[i],
24e1b 20 7a 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20   z, 0);.    if( 
24e1c 70 43 6f 6c 6c 32 2d 3e 78 43 6d 70 21 3d 30 20  pColl2->xCmp!=0 
24e1d 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
24e1e 70 43 6f 6c 6c 2c 20 70 43 6f 6c 6c 32 2c 20 73  pColl, pColl2, s
24e1f 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 29 29 3b  izeof(CollSeq));
24e20 0a 20 20 20 20 20 20 70 43 6f 6c 6c 2d 3e 78 44  .      pColl->xD
24e21 65 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  el = 0;         
24e22 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f 70 79 20 74  /* Do not copy t
24e23 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 2a 2f  he destructor */
24e24 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
24e25 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
24e26 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
24e27 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a  TE_ERROR;.}../*.
24e28 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
24e29 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
24e2a 66 6f 72 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65  for invoking the
24e2b 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f   collation facto
24e2c 72 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 6f  ry callback.** o
24e2d 72 20 73 75 62 73 74 69 74 75 74 69 6e 67 20 61  r substituting a
24e2e 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
24e2f 6e 63 65 20 6f 66 20 61 20 64 69 66 66 65 72 65  nce of a differe
24e30 6e 74 20 65 6e 63 6f 64 69 6e 67 20 77 68 65 6e  nt encoding when
24e31 20 74 68 65 0a 2a 2a 20 72 65 71 75 65 73 74 65   the.** requeste
24e32 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  d collation sequ
24e33 65 6e 63 65 20 69 73 20 6e 6f 74 20 61 76 61 69  ence is not avai
24e34 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 64 65 73  lable in the des
24e35 69 72 65 64 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a  ired encoding..*
24e36 2a 20 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e  * .** If it is n
24e37 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 43  ot NULL, then pC
24e38 6f 6c 6c 20 6d 75 73 74 20 70 6f 69 6e 74 20 74  oll must point t
24e39 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e  o the database n
24e3a 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 20 0a  ative encoding .
24e3b 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ** collation seq
24e3c 75 65 6e 63 65 20 77 69 74 68 20 6e 61 6d 65 20  uence with name 
24e3d 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 4e  zName, length nN
24e3e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  ame..**.** The r
24e3f 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 65  eturn value is e
24e40 69 74 68 65 72 20 74 68 65 20 63 6f 6c 6c 61 74  ither the collat
24e41 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 74 6f 20  ion sequence to 
24e42 62 65 20 75 73 65 64 20 69 6e 20 64 61 74 61 62  be used in datab
24e43 61 73 65 0a 2a 2a 20 64 62 20 66 6f 72 20 63 6f  ase.** db for co
24e44 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6e 61 6d  llation type nam
24e45 65 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 68 20  e zName, length 
24e46 6e 4e 61 6d 65 2c 20 6f 72 20 4e 55 4c 4c 2c 20  nName, or NULL, 
24e47 69 66 20 6e 6f 20 63 6f 6c 6c 61 74 69 6f 6e 0a  if no collation.
24e48 2a 2a 20 73 65 71 75 65 6e 63 65 20 63 61 6e 20  ** sequence can 
24e49 62 65 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20  be found..**.** 
24e4a 53 65 65 20 61 6c 73 6f 3a 20 73 71 6c 69 74 65  See also: sqlite
24e4b 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 29  3LocateCollSeq()
24e4c 2c 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c  , sqlite3FindCol
24e4d 6c 53 65 71 28 29 0a 2a 2f 0a 53 51 4c 49 54 45  lSeq().*/.SQLITE
24e4e 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71  _PRIVATE CollSeq
24e4f 20 2a 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c   *sqlite3GetColl
24e50 53 65 71 28 0a 20 20 73 71 6c 69 74 65 33 2a 20  Seq(.  sqlite3* 
24e51 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  db,          /* 
24e52 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
24e53 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20  nection */.  u8 
24e54 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  enc,            
24e55 20 20 20 2f 2a 20 54 68 65 20 64 65 73 69 72 65     /* The desire
24e56 64 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74  d encoding for t
24e57 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  he collating seq
24e58 75 65 6e 63 65 20 2a 2f 0a 20 20 43 6f 6c 6c 53  uence */.  CollS
24e59 65 71 20 2a 70 43 6f 6c 6c 2c 20 20 20 20 20 20  eq *pColl,      
24e5a 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65   /* Collating se
24e5b 71 75 65 6e 63 65 20 77 69 74 68 20 6e 61 74 69  quence with nati
24e5c 76 65 20 65 6e 63 6f 64 69 6e 67 2c 20 6f 72 20  ve encoding, or 
24e5d 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  NULL */.  const 
24e5e 63 68 61 72 20 2a 7a 4e 61 6d 65 20 20 20 20 20  char *zName     
24e5f 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71  /* Collating seq
24e60 75 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a 29 7b  uence name */.){
24e61 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 3b 0a 0a  .  CollSeq *p;..
24e62 20 20 70 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 69    p = pColl;.  i
24e63 66 28 20 21 70 20 29 7b 0a 20 20 20 20 70 20 3d  f( !p ){.    p =
24e64 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c   sqlite3FindColl
24e65 53 65 71 28 64 62 2c 20 65 6e 63 2c 20 7a 4e 61  Seq(db, enc, zNa
24e66 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66  me, 0);.  }.  if
24e67 28 20 21 70 20 7c 7c 20 21 70 2d 3e 78 43 6d 70  ( !p || !p->xCmp
24e68 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 20 63 6f   ){.    /* No co
24e69 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
24e6a 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 66 6f   of this type fo
24e6b 72 20 74 68 69 73 20 65 6e 63 6f 64 69 6e 67 20  r this encoding 
24e6c 69 73 20 72 65 67 69 73 74 65 72 65 64 2e 0a 20  is registered.. 
24e6d 20 20 20 2a 2a 20 43 61 6c 6c 20 74 68 65 20 63     ** Call the c
24e6e 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79  ollation factory
24e6f 20 74 6f 20 73 65 65 20 69 66 20 69 74 20 63 61   to see if it ca
24e70 6e 20 73 75 70 70 6c 79 20 75 73 20 77 69 74 68  n supply us with
24e71 20 6f 6e 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20   one..    */.   
24e72 20 63 61 6c 6c 43 6f 6c 6c 4e 65 65 64 65 64 28   callCollNeeded(
24e73 64 62 2c 20 65 6e 63 2c 20 7a 4e 61 6d 65 29 3b  db, enc, zName);
24e74 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
24e75 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20  FindCollSeq(db, 
24e76 65 6e 63 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a  enc, zName, 0);.
24e77 20 20 7d 0a 20 20 69 66 28 20 70 20 26 26 20 21    }.  if( p && !
24e78 70 2d 3e 78 43 6d 70 20 26 26 20 73 79 6e 74 68  p->xCmp && synth
24e79 43 6f 6c 6c 53 65 71 28 64 62 2c 20 70 29 20 29  CollSeq(db, p) )
24e7a 7b 0a 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 7d  {.    p = 0;.  }
24e7b 0a 20 20 61 73 73 65 72 74 28 20 21 70 20 7c 7c  .  assert( !p ||
24e7c 20 70 2d 3e 78 43 6d 70 20 29 3b 0a 20 20 72 65   p->xCmp );.  re
24e7d 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
24e7e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
24e7f 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 63 6f 6c   called on a col
24e80 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
24e81 62 65 66 6f 72 65 20 69 74 20 69 73 20 75 73 65  before it is use
24e82 64 20 74 6f 0a 2a 2a 20 63 68 65 63 6b 20 74 68  d to.** check th
24e83 61 74 20 69 74 20 69 73 20 64 65 66 69 6e 65 64  at it is defined
24e84 2e 20 41 6e 20 75 6e 64 65 66 69 6e 65 64 20 63  . An undefined c
24e85 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
24e86 65 20 65 78 69 73 74 73 20 77 68 65 6e 0a 2a 2a  e exists when.**
24e87 20 61 20 64 61 74 61 62 61 73 65 20 69 73 20 6c   a database is l
24e88 6f 61 64 65 64 20 74 68 61 74 20 63 6f 6e 74 61  oaded that conta
24e89 69 6e 73 20 72 65 66 65 72 65 6e 63 65 73 20 74  ins references t
24e8a 6f 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  o collation sequ
24e8b 65 6e 63 65 73 0a 2a 2a 20 74 68 61 74 20 68 61  ences.** that ha
24e8c 76 65 20 6e 6f 74 20 62 65 65 6e 20 64 65 66 69  ve not been defi
24e8d 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63  ned by sqlite3_c
24e8e 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28  reate_collation(
24e8f 29 20 65 74 63 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ) etc..**.** If 
24e90 72 65 71 75 69 72 65 64 2c 20 74 68 69 73 20 72  required, this r
24e91 6f 75 74 69 6e 65 20 63 61 6c 6c 73 20 74 68 65  outine calls the
24e92 20 27 63 6f 6c 6c 61 74 69 6f 6e 20 6e 65 65 64   'collation need
24e93 65 64 27 20 63 61 6c 6c 62 61 63 6b 20 74 6f 0a  ed' callback to.
24e94 2a 2a 20 72 65 71 75 65 73 74 20 61 20 64 65 66  ** request a def
24e95 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 63  inition of the c
24e96 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
24e97 65 2e 20 49 66 20 74 68 69 73 20 64 6f 65 73 6e  e. If this doesn
24e98 27 74 20 77 6f 72 6b 2c 20 0a 2a 2a 20 61 6e 20  't work, .** an 
24e99 65 71 75 69 76 61 6c 65 6e 74 20 63 6f 6c 6c 61  equivalent colla
24e9a 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 74 68  ting sequence th
24e9b 61 74 20 75 73 65 73 20 61 20 74 65 78 74 20 65  at uses a text e
24e9c 6e 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e  ncoding differen
24e9d 74 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 6d 61  t.** from the ma
24e9e 69 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 73  in database is s
24e9f 75 62 73 74 69 74 75 74 65 64 2c 20 69 66 20 6f  ubstituted, if o
24ea0 6e 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e  ne is available.
24ea1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
24ea2 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68  TE int sqlite3Ch
24ea3 65 63 6b 43 6f 6c 6c 53 65 71 28 50 61 72 73 65  eckCollSeq(Parse
24ea4 20 2a 70 50 61 72 73 65 2c 20 43 6f 6c 6c 53 65   *pParse, CollSe
24ea5 71 20 2a 70 43 6f 6c 6c 29 7b 0a 20 20 69 66 28  q *pColl){.  if(
24ea6 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 63 6f   pColl ){.    co
24ea7 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20  nst char *zName 
24ea8 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a  = pColl->zName;.
24ea9 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20      sqlite3 *db 
24eaa 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
24eab 20 20 43 6f 6c 6c 53 65 71 20 2a 70 20 3d 20 73    CollSeq *p = s
24eac 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71  qlite3GetCollSeq
24ead 28 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 70 43  (db, ENC(db), pC
24eae 6f 6c 6c 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  oll, zName);.   
24eaf 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 20   if( !p ){.     
24eb0 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
24eb1 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63  (pParse, "no suc
24eb2 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  h collation sequ
24eb3 65 6e 63 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65  ence: %s", zName
24eb4 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  );.      pParse-
24eb5 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 72  >nErr++;.      r
24eb6 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
24eb7 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  OR;.    }.    as
24eb8 73 65 72 74 28 20 70 3d 3d 70 43 6f 6c 6c 20 29  sert( p==pColl )
24eb9 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
24eba 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 2f  QLITE_OK;.}..../
24ebb 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 61 6e 64 20  *.** Locate and 
24ebc 72 65 74 75 72 6e 20 61 6e 20 65 6e 74 72 79 20  return an entry 
24ebd 66 72 6f 6d 20 74 68 65 20 64 62 2e 61 43 6f 6c  from the db.aCol
24ebe 6c 53 65 71 20 68 61 73 68 20 74 61 62 6c 65 2e  lSeq hash table.
24ebf 20 49 66 20 74 68 65 20 65 6e 74 72 79 0a 2a 2a   If the entry.**
24ec0 20 73 70 65 63 69 66 69 65 64 20 62 79 20 7a 4e   specified by zN
24ec1 61 6d 65 20 61 6e 64 20 6e 4e 61 6d 65 20 69 73  ame and nName is
24ec2 20 6e 6f 74 20 66 6f 75 6e 64 20 61 6e 64 20 70   not found and p
24ec3 61 72 61 6d 65 74 65 72 20 27 63 72 65 61 74 65  arameter 'create
24ec4 27 20 69 73 0a 2a 2a 20 74 72 75 65 2c 20 74 68  ' is.** true, th
24ec5 65 6e 20 63 72 65 61 74 65 20 61 20 6e 65 77 20  en create a new 
24ec6 65 6e 74 72 79 2e 20 4f 74 68 65 72 77 69 73 65  entry. Otherwise
24ec7 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a   return NULL..**
24ec8 0a 2a 2a 20 45 61 63 68 20 70 6f 69 6e 74 65 72  .** Each pointer
24ec9 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73   stored in the s
24eca 71 6c 69 74 65 33 2e 61 43 6f 6c 6c 53 65 71 20  qlite3.aCollSeq 
24ecb 68 61 73 68 20 74 61 62 6c 65 20 63 6f 6e 74 61  hash table conta
24ecc 69 6e 73 20 61 6e 0a 2a 2a 20 61 72 72 61 79 20  ins an.** array 
24ecd 6f 66 20 74 68 72 65 65 20 43 6f 6c 6c 53 65 71  of three CollSeq
24ece 20 73 74 72 75 63 74 75 72 65 73 2e 20 54 68 65   structures. The
24ecf 20 66 69 72 73 74 20 69 73 20 74 68 65 20 63 6f   first is the co
24ed0 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
24ed1 0a 2a 2a 20 70 72 65 66 66 65 72 72 65 64 20 66  .** prefferred f
24ed2 6f 72 20 55 54 46 2d 38 2c 20 74 68 65 20 73 65  or UTF-8, the se
24ed3 63 6f 6e 64 20 55 54 46 2d 31 36 6c 65 2c 20 61  cond UTF-16le, a
24ed4 6e 64 20 74 68 65 20 74 68 69 72 64 20 55 54 46  nd the third UTF
24ed5 2d 31 36 62 65 2e 0a 2a 2a 0a 2a 2a 20 53 74 6f  -16be..**.** Sto
24ed6 72 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  red immediately 
24ed7 61 66 74 65 72 20 74 68 65 20 74 68 72 65 65 20  after the three 
24ed8 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
24ed9 63 65 73 20 69 73 20 61 20 63 6f 70 79 20 6f 66  ces is a copy of
24eda 0a 2a 2a 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f  .** the collatio
24edb 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 2e  n sequence name.
24edc 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
24edd 69 73 20 73 74 72 69 6e 67 20 69 73 20 73 74 6f  is string is sto
24ede 72 65 64 20 69 6e 0a 2a 2a 20 65 61 63 68 20 63  red in.** each c
24edf 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
24ee0 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  e structure..*/.
24ee1 73 74 61 74 69 63 20 43 6f 6c 6c 53 65 71 20 2a  static CollSeq *
24ee2 66 69 6e 64 43 6f 6c 6c 53 65 71 45 6e 74 72 79  findCollSeqEntry
24ee3 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
24ee4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
24ee5 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
24ee6 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
24ee7 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 2f 2a 20 4e   *zName,    /* N
24ee8 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61  ame of the colla
24ee9 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f  ting sequence */
24eea 0a 20 20 69 6e 74 20 63 72 65 61 74 65 20 20 20  .  int create   
24eeb 20 20 20 20 20 20 20 20 20 2f 2a 20 43 72 65 61           /* Crea
24eec 74 65 20 61 20 6e 65 77 20 65 6e 74 72 79 20 69  te a new entry i
24eed 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 43  f true */.){.  C
24eee 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20  ollSeq *pColl;. 
24eef 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c   int nName = sql
24ef0 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61  ite3Strlen30(zNa
24ef1 6d 65 29 3b 0a 20 20 70 43 6f 6c 6c 20 3d 20 73  me);.  pColl = s
24ef2 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26  qlite3HashFind(&
24ef3 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 2c 20 7a 4e  db->aCollSeq, zN
24ef4 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 0a 20 20  ame, nName);..  
24ef5 69 66 28 20 30 3d 3d 70 43 6f 6c 6c 20 26 26 20  if( 0==pColl && 
24ef6 63 72 65 61 74 65 20 29 7b 0a 20 20 20 20 70 43  create ){.    pC
24ef7 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  oll = sqlite3DbM
24ef8 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 33 2a  allocZero(db, 3*
24ef9 73 69 7a 65 6f 66 28 2a 70 43 6f 6c 6c 29 20 2b  sizeof(*pColl) +
24efa 20 6e 4e 61 6d 65 20 2b 20 31 20 29 3b 0a 20 20   nName + 1 );.  
24efb 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20    if( pColl ){. 
24efc 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 44       CollSeq *pD
24efd 65 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 43  el = 0;.      pC
24efe 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 20 3d 20 28  oll[0].zName = (
24eff 63 68 61 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d 3b  char*)&pColl[3];
24f00 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e  .      pColl[0].
24f01 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46  enc = SQLITE_UTF
24f02 38 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 31  8;.      pColl[1
24f03 5d 2e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a  ].zName = (char*
24f04 29 26 70 43 6f 6c 6c 5b 33 5d 3b 0a 20 20 20 20  )&pColl[3];.    
24f05 20 20 70 43 6f 6c 6c 5b 31 5d 2e 65 6e 63 20 3d    pColl[1].enc =
24f06 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 3b   SQLITE_UTF16LE;
24f07 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 32 5d 2e  .      pColl[2].
24f08 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29 26  zName = (char*)&
24f09 70 43 6f 6c 6c 5b 33 5d 3b 0a 20 20 20 20 20 20  pColl[3];.      
24f0a 70 43 6f 6c 6c 5b 32 5d 2e 65 6e 63 20 3d 20 53  pColl[2].enc = S
24f0b 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3b 0a 20  QLITE_UTF16BE;. 
24f0c 20 20 20 20 20 6d 65 6d 63 70 79 28 70 43 6f 6c       memcpy(pCol
24f0d 6c 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d  l[0].zName, zNam
24f0e 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 20  e, nName);.     
24f0f 20 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 5b   pColl[0].zName[
24f10 6e 4e 61 6d 65 5d 20 3d 20 30 3b 0a 20 20 20 20  nName] = 0;.    
24f11 20 20 70 44 65 6c 20 3d 20 73 71 6c 69 74 65 33    pDel = sqlite3
24f12 48 61 73 68 49 6e 73 65 72 74 28 26 64 62 2d 3e  HashInsert(&db->
24f13 61 43 6f 6c 6c 53 65 71 2c 20 70 43 6f 6c 6c 5b  aCollSeq, pColl[
24f14 30 5d 2e 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c  0].zName, nName,
24f15 20 70 43 6f 6c 6c 29 3b 0a 0a 20 20 20 20 20 20   pColl);..      
24f16 2f 2a 20 49 66 20 61 20 6d 61 6c 6c 6f 63 28 29  /* If a malloc()
24f17 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 72 65   failure occurre
24f18 64 20 69 6e 20 73 71 6c 69 74 65 33 48 61 73 68  d in sqlite3Hash
24f19 49 6e 73 65 72 74 28 29 2c 20 69 74 20 77 69 6c  Insert(), it wil
24f1a 6c 20 0a 20 20 20 20 20 20 2a 2a 20 72 65 74 75  l .      ** retu
24f1b 72 6e 20 74 68 65 20 70 43 6f 6c 6c 20 70 6f 69  rn the pColl poi
24f1c 6e 74 65 72 20 74 6f 20 62 65 20 64 65 6c 65 74  nter to be delet
24f1d 65 64 20 28 62 65 63 61 75 73 65 20 69 74 20 77  ed (because it w
24f1e 61 73 6e 27 74 20 61 64 64 65 64 0a 20 20 20 20  asn't added.    
24f1f 20 20 2a 2a 20 74 6f 20 74 68 65 20 68 61 73 68    ** to the hash
24f20 20 74 61 62 6c 65 29 2e 0a 20 20 20 20 20 20 2a   table)..      *
24f21 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  /.      assert( 
24f22 70 44 65 6c 3d 3d 30 20 7c 7c 20 70 44 65 6c 3d  pDel==0 || pDel=
24f23 3d 70 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 20 20  =pColl );.      
24f24 69 66 28 20 70 44 65 6c 21 3d 30 20 29 7b 0a 20  if( pDel!=0 ){. 
24f25 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f         db->mallo
24f26 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
24f27 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
24f28 65 65 28 64 62 2c 20 70 44 65 6c 29 3b 0a 20 20  ee(db, pDel);.  
24f29 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 30 3b        pColl = 0;
24f2a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
24f2b 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c   }.  return pCol
24f2c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61  l;.}../*.** Para
24f2d 6d 65 74 65 72 20 7a 4e 61 6d 65 20 70 6f 69 6e  meter zName poin
24f2e 74 73 20 74 6f 20 61 20 55 54 46 2d 38 20 65 6e  ts to a UTF-8 en
24f2f 63 6f 64 65 64 20 73 74 72 69 6e 67 20 6e 4e 61  coded string nNa
24f30 6d 65 20 62 79 74 65 73 20 6c 6f 6e 67 2e 0a 2a  me bytes long..*
24f31 2a 20 52 65 74 75 72 6e 20 74 68 65 20 43 6f 6c  * Return the Col
24f32 6c 53 65 71 2a 20 70 6f 69 6e 74 65 72 20 66 6f  lSeq* pointer fo
24f33 72 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  r the collation 
24f34 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 64 20 7a  sequence named z
24f35 4e 61 6d 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20  Name.** for the 
24f36 65 6e 63 6f 64 69 6e 67 20 27 65 6e 63 27 20 66  encoding 'enc' f
24f37 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
24f38 20 27 64 62 27 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   'db'..**.** If 
24f39 74 68 65 20 65 6e 74 72 79 20 73 70 65 63 69 66  the entry specif
24f3a 69 65 64 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64  ied is not found
24f3b 20 61 6e 64 20 27 63 72 65 61 74 65 27 20 69 73   and 'create' is
24f3c 20 74 72 75 65 2c 20 74 68 65 6e 20 63 72 65 61   true, then crea
24f3d 74 65 20 61 0a 2a 2a 20 6e 65 77 20 65 6e 74 72  te a.** new entr
24f3e 79 2e 20 20 4f 74 68 65 72 77 69 73 65 20 72 65  y.  Otherwise re
24f3f 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  turn NULL..**.**
24f40 20 41 20 73 65 70 61 72 61 74 65 20 66 75 6e 63   A separate func
24f41 74 69 6f 6e 20 73 71 6c 69 74 65 33 4c 6f 63 61  tion sqlite3Loca
24f42 74 65 43 6f 6c 6c 53 65 71 28 29 20 69 73 20 61  teCollSeq() is a
24f43 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 0a   wrapper around.
24f44 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  ** this routine.
24f45 20 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43    sqlite3LocateC
24f46 6f 6c 6c 53 65 71 28 29 20 69 6e 76 6f 6b 65 73  ollSeq() invokes
24f47 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66   the collation f
24f48 61 63 74 6f 72 79 0a 2a 2a 20 69 66 20 6e 65 63  actory.** if nec
24f49 65 73 73 61 72 79 20 61 6e 64 20 67 65 6e 65 72  essary and gener
24f4a 61 74 65 73 20 61 6e 20 65 72 72 6f 72 20 6d 65  ates an error me
24f4b 73 73 61 67 65 20 69 66 20 74 68 65 20 63 6f 6c  ssage if the col
24f4c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 0a  lating sequence.
24f4d 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75  ** cannot be fou
24f4e 6e 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  nd..**.** See al
24f4f 73 6f 3a 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  so: sqlite3Locat
24f50 65 43 6f 6c 6c 53 65 71 28 29 2c 20 73 71 6c 69  eCollSeq(), sqli
24f51 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 29 0a  te3GetCollSeq().
24f52 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
24f53 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74  E CollSeq *sqlit
24f54 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 0a 20  e3FindCollSeq(. 
24f55 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20   sqlite3 *db,.  
24f56 75 38 20 65 6e 63 2c 0a 20 20 63 6f 6e 73 74 20  u8 enc,.  const 
24f57 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69  char *zName,.  i
24f58 6e 74 20 63 72 65 61 74 65 0a 29 7b 0a 20 20 43  nt create.){.  C
24f59 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20  ollSeq *pColl;. 
24f5a 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20   if( zName ){.  
24f5b 20 20 70 43 6f 6c 6c 20 3d 20 66 69 6e 64 43 6f    pColl = findCo
24f5c 6c 6c 53 65 71 45 6e 74 72 79 28 64 62 2c 20 7a  llSeqEntry(db, z
24f5d 4e 61 6d 65 2c 20 63 72 65 61 74 65 29 3b 0a 20  Name, create);. 
24f5e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 6f 6c   }else{.    pCol
24f5f 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c  l = db->pDfltCol
24f60 6c 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  l;.  }.  assert(
24f61 20 53 51 4c 49 54 45 5f 55 54 46 38 3d 3d 31 20   SQLITE_UTF8==1 
24f62 26 26 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  && SQLITE_UTF16L
24f63 45 3d 3d 32 20 26 26 20 53 51 4c 49 54 45 5f 55  E==2 && SQLITE_U
24f64 54 46 31 36 42 45 3d 3d 33 20 29 3b 0a 20 20 61  TF16BE==3 );.  a
24f65 73 73 65 72 74 28 20 65 6e 63 3e 3d 53 51 4c 49  ssert( enc>=SQLI
24f66 54 45 5f 55 54 46 38 20 26 26 20 65 6e 63 3c 3d  TE_UTF8 && enc<=
24f67 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29  SQLITE_UTF16BE )
24f68 3b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 20  ;.  if( pColl ) 
24f69 70 43 6f 6c 6c 20 2b 3d 20 65 6e 63 2d 31 3b 0a  pColl += enc-1;.
24f6a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a    return pColl;.
24f6b 7d 0a 0a 2f 2a 20 44 75 72 69 6e 67 20 74 68 65  }../* During the
24f6c 20 73 65 61 72 63 68 20 66 6f 72 20 74 68 65 20   search for the 
24f6d 62 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 64 65  best function de
24f6e 66 69 6e 69 74 69 6f 6e 2c 20 74 68 69 73 20 70  finition, this p
24f6f 72 6f 63 65 64 75 72 65 0a 2a 2a 20 69 73 20 63  rocedure.** is c
24f70 61 6c 6c 65 64 20 74 6f 20 74 65 73 74 20 68 6f  alled to test ho
24f71 77 20 77 65 6c 6c 20 74 68 65 20 66 75 6e 63 74  w well the funct
24f72 69 6f 6e 20 70 61 73 73 65 64 20 61 73 20 74 68  ion passed as th
24f73 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
24f74 0a 2a 2a 20 6d 61 74 63 68 65 73 20 74 68 65 20  .** matches the 
24f75 72 65 71 75 65 73 74 20 66 6f 72 20 61 20 66 75  request for a fu
24f76 6e 63 74 69 6f 6e 20 77 69 74 68 20 6e 41 72 67  nction with nArg
24f77 20 61 72 67 75 6d 65 6e 74 73 20 69 6e 20 61 20   arguments in a 
24f78 73 79 73 74 65 6d 0a 2a 2a 20 74 68 61 74 20 75  system.** that u
24f79 73 65 73 20 65 6e 63 6f 64 69 6e 67 20 65 6e 63  ses encoding enc
24f7a 2e 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75  . The value retu
24f7b 72 6e 65 64 20 69 6e 64 69 63 61 74 65 73 20 68  rned indicates h
24f7c 6f 77 20 77 65 6c 6c 20 74 68 65 0a 2a 2a 20 72  ow well the.** r
24f7d 65 71 75 65 73 74 20 69 73 20 6d 61 74 63 68 65  equest is matche
24f7e 64 2e 20 41 20 68 69 67 68 65 72 20 76 61 6c 75  d. A higher valu
24f7f 65 20 69 6e 64 69 63 61 74 65 73 20 61 20 62 65  e indicates a be
24f80 74 74 65 72 20 6d 61 74 63 68 2e 0a 2a 2a 0a 2a  tter match..**.*
24f81 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76  * The returned v
24f82 61 6c 75 65 20 69 73 20 61 6c 77 61 79 73 20 62  alue is always b
24f83 65 74 77 65 65 6e 20 30 20 61 6e 64 20 36 2c 20  etween 0 and 6, 
24f84 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
24f85 2a 20 30 3a 20 4e 6f 74 20 61 20 6d 61 74 63 68  * 0: Not a match
24f86 2c 20 6f 72 20 69 66 20 6e 41 72 67 3c 30 20 61  , or if nArg<0 a
24f87 6e 64 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  nd the function 
24f88 69 73 20 68 61 73 20 6e 6f 20 69 6d 70 6c 65 6d  is has no implem
24f89 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 31 3a 20  entation..** 1: 
24f8a 41 20 76 61 72 69 61 62 6c 65 20 61 72 67 75 6d  A variable argum
24f8b 65 6e 74 73 20 66 75 6e 63 74 69 6f 6e 20 74 68  ents function th
24f8c 61 74 20 70 72 65 66 65 72 73 20 55 54 46 2d 38  at prefers UTF-8
24f8d 20 77 68 65 6e 20 61 20 55 54 46 2d 31 36 0a 2a   when a UTF-16.*
24f8e 2a 20 20 20 20 65 6e 63 6f 64 69 6e 67 20 69 73  *    encoding is
24f8f 20 72 65 71 75 65 73 74 65 64 2c 20 6f 72 20 76   requested, or v
24f90 69 63 65 20 76 65 72 73 61 2e 0a 2a 2a 20 32 3a  ice versa..** 2:
24f91 20 41 20 76 61 72 69 61 62 6c 65 20 61 72 67 75   A variable argu
24f92 6d 65 6e 74 73 20 66 75 6e 63 74 69 6f 6e 20 74  ments function t
24f93 68 61 74 20 75 73 65 73 20 55 54 46 2d 31 36 42  hat uses UTF-16B
24f94 45 20 77 68 65 6e 20 55 54 46 2d 31 36 4c 45 20  E when UTF-16LE 
24f95 69 73 0a 2a 2a 20 20 20 20 72 65 71 75 65 73 74  is.**    request
24f96 65 64 2c 20 6f 72 20 76 69 63 65 20 76 65 72 73  ed, or vice vers
24f97 61 2e 0a 2a 2a 20 33 3a 20 41 20 76 61 72 69 61  a..** 3: A varia
24f98 62 6c 65 20 61 72 67 75 6d 65 6e 74 73 20 66 75  ble arguments fu
24f99 6e 63 74 69 6f 6e 20 75 73 69 6e 67 20 74 68 65  nction using the
24f9a 20 73 61 6d 65 20 74 65 78 74 20 65 6e 63 6f 64   same text encod
24f9b 69 6e 67 2e 0a 2a 2a 20 34 3a 20 41 20 66 75 6e  ing..** 4: A fun
24f9c 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 65  ction with the e
24f9d 78 61 63 74 20 6e 75 6d 62 65 72 20 6f 66 20 61  xact number of a
24f9e 72 67 75 6d 65 6e 74 73 20 72 65 71 75 65 73 74  rguments request
24f9f 65 64 20 74 68 61 74 0a 2a 2a 20 20 20 20 70 72  ed that.**    pr
24fa0 65 66 65 72 73 20 55 54 46 2d 38 20 77 68 65 6e  efers UTF-8 when
24fa1 20 61 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 69   a UTF-16 encodi
24fa2 6e 67 20 69 73 20 72 65 71 75 65 73 74 65 64 2c  ng is requested,
24fa3 20 6f 72 20 76 69 63 65 20 76 65 72 73 61 2e 0a   or vice versa..
24fa4 2a 2a 20 35 3a 20 41 20 66 75 6e 63 74 69 6f 6e  ** 5: A function
24fa5 20 77 69 74 68 20 74 68 65 20 65 78 61 63 74 20   with the exact 
24fa6 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
24fa7 6e 74 73 20 72 65 71 75 65 73 74 65 64 20 74 68  nts requested th
24fa8 61 74 0a 2a 2a 20 20 20 20 70 72 65 66 65 72 73  at.**    prefers
24fa9 20 55 54 46 2d 31 36 4c 45 20 77 68 65 6e 20 55   UTF-16LE when U
24faa 54 46 2d 31 36 42 45 20 69 73 20 72 65 71 75 65  TF-16BE is reque
24fab 73 74 65 64 2c 20 6f 72 20 76 69 63 65 20 76 65  sted, or vice ve
24fac 72 73 61 2e 0a 2a 2a 20 36 3a 20 41 6e 20 65 78  rsa..** 6: An ex
24fad 61 63 74 20 6d 61 74 63 68 2e 0a 2a 2a 0a 2a 2f  act match..**.*/
24fae 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 61 74 63  .static int matc
24faf 68 51 75 61 6c 69 74 79 28 46 75 6e 63 44 65 66  hQuality(FuncDef
24fb0 20 2a 70 2c 20 69 6e 74 20 6e 41 72 67 2c 20 75   *p, int nArg, u
24fb1 38 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20 6d 61  8 enc){.  int ma
24fb2 74 63 68 20 3d 20 30 3b 0a 20 20 69 66 28 20 70  tch = 0;.  if( p
24fb3 2d 3e 6e 41 72 67 3d 3d 2d 31 20 7c 7c 20 70 2d  ->nArg==-1 || p-
24fb4 3e 6e 41 72 67 3d 3d 6e 41 72 67 20 0a 20 20 20  >nArg==nArg .   
24fb5 7c 7c 20 28 6e 41 72 67 3d 3d 2d 31 20 26 26 20  || (nArg==-1 && 
24fb6 28 70 2d 3e 78 46 75 6e 63 21 3d 30 20 7c 7c 20  (p->xFunc!=0 || 
24fb7 70 2d 3e 78 53 74 65 70 21 3d 30 29 29 0a 20 20  p->xStep!=0)).  
24fb8 29 7b 0a 20 20 20 20 6d 61 74 63 68 20 3d 20 31  ){.    match = 1
24fb9 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 41 72  ;.    if( p->nAr
24fba 67 3d 3d 6e 41 72 67 20 7c 7c 20 6e 41 72 67 3d  g==nArg || nArg=
24fbb 3d 2d 31 20 29 7b 0a 20 20 20 20 20 20 6d 61 74  =-1 ){.      mat
24fbc 63 68 20 3d 20 34 3b 0a 20 20 20 20 7d 0a 20 20  ch = 4;.    }.  
24fbd 20 20 69 66 28 20 65 6e 63 3d 3d 70 2d 3e 69 50    if( enc==p->iP
24fbe 72 65 66 45 6e 63 20 29 7b 0a 20 20 20 20 20 20  refEnc ){.      
24fbf 6d 61 74 63 68 20 2b 3d 20 32 3b 0a 20 20 20 20  match += 2;.    
24fc0 7d 0a 20 20 20 20 65 6c 73 65 20 69 66 28 20 28  }.    else if( (
24fc1 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31  enc==SQLITE_UTF1
24fc2 36 4c 45 20 26 26 20 70 2d 3e 69 50 72 65 66 45  6LE && p->iPrefE
24fc3 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  nc==SQLITE_UTF16
24fc4 42 45 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 20  BE) ||.         
24fc5 20 20 20 20 28 65 6e 63 3d 3d 53 51 4c 49 54 45      (enc==SQLITE
24fc6 5f 55 54 46 31 36 42 45 20 26 26 20 70 2d 3e 69  _UTF16BE && p->i
24fc7 50 72 65 66 45 6e 63 3d 3d 53 51 4c 49 54 45 5f  PrefEnc==SQLITE_
24fc8 55 54 46 31 36 4c 45 29 20 29 7b 0a 20 20 20 20  UTF16LE) ){.    
24fc9 20 20 6d 61 74 63 68 20 2b 3d 20 31 3b 0a 20 20    match += 1;.  
24fca 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
24fcb 20 6d 61 74 63 68 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   match;.}../*.**
24fcc 20 53 65 61 72 63 68 20 61 20 46 75 6e 63 44 65   Search a FuncDe
24fcd 66 48 61 73 68 20 66 6f 72 20 61 20 66 75 6e 63  fHash for a func
24fce 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 67 69  tion with the gi
24fcf 76 65 6e 20 6e 61 6d 65 2e 20 20 52 65 74 75 72  ven name.  Retur
24fd0 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74  n.** a pointer t
24fd1 6f 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 46  o the matching F
24fd2 75 6e 63 44 65 66 20 69 66 20 66 6f 75 6e 64 2c  uncDef if found,
24fd3 20 6f 72 20 30 20 69 66 20 74 68 65 72 65 20 69   or 0 if there i
24fd4 73 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73  s no match..*/.s
24fd5 74 61 74 69 63 20 46 75 6e 63 44 65 66 20 2a 66  tatic FuncDef *f
24fd6 75 6e 63 74 69 6f 6e 53 65 61 72 63 68 28 0a 20  unctionSearch(. 
24fd7 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70 48   FuncDefHash *pH
24fd8 61 73 68 2c 20 20 2f 2a 20 48 61 73 68 20 74 61  ash,  /* Hash ta
24fd9 62 6c 65 20 74 6f 20 73 65 61 72 63 68 20 2a 2f  ble to search */
24fda 0a 20 20 69 6e 74 20 68 2c 20 20 20 20 20 20 20  .  int h,       
24fdb 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20          /* Hash 
24fdc 6f 66 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 20  of the name */. 
24fdd 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75   const char *zFu
24fde 6e 63 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  nc,   /* Name of
24fdf 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 69   function */.  i
24fe0 6e 74 20 6e 46 75 6e 63 20 20 20 20 20 20 20 20  nt nFunc        
24fe1 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
24fe2 20 62 79 74 65 73 20 69 6e 20 7a 46 75 6e 63 20   bytes in zFunc 
24fe3 2a 2f 0a 29 7b 0a 20 20 46 75 6e 63 44 65 66 20  */.){.  FuncDef 
24fe4 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 48 61 73  *p;.  for(p=pHas
24fe5 68 2d 3e 61 5b 68 5d 3b 20 70 3b 20 70 3d 70 2d  h->a[h]; p; p=p-
24fe6 3e 70 48 61 73 68 29 7b 0a 20 20 20 20 69 66 28  >pHash){.    if(
24fe7 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
24fe8 28 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 46 75 6e 63  (p->zName, zFunc
24fe9 2c 20 6e 46 75 6e 63 29 3d 3d 30 20 26 26 20 70  , nFunc)==0 && p
24fea 2d 3e 7a 4e 61 6d 65 5b 6e 46 75 6e 63 5d 3d 3d  ->zName[nFunc]==
24feb 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
24fec 6e 20 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n p;.    }.  }. 
24fed 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
24fee 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 6e 65 77  .** Insert a new
24fef 20 46 75 6e 63 44 65 66 20 69 6e 74 6f 20 61 20   FuncDef into a 
24ff0 46 75 6e 63 44 65 66 48 61 73 68 20 68 61 73 68  FuncDefHash hash
24ff1 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54   table..*/.SQLIT
24ff2 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
24ff3 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73  qlite3FuncDefIns
24ff4 65 72 74 28 0a 20 20 46 75 6e 63 44 65 66 48 61  ert(.  FuncDefHa
24ff5 73 68 20 2a 70 48 61 73 68 2c 20 20 2f 2a 20 54  sh *pHash,  /* T
24ff6 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 6e  he hash table in
24ff7 74 6f 20 77 68 69 63 68 20 74 6f 20 69 6e 73 65  to which to inse
24ff8 72 74 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20  rt */.  FuncDef 
24ff9 2a 70 44 65 66 20 20 20 20 20 20 20 20 2f 2a 20  *pDef        /* 
24ffa 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 65 66  The function def
24ffb 69 6e 69 74 69 6f 6e 20 74 6f 20 69 6e 73 65 72  inition to inser
24ffc 74 20 2a 2f 0a 29 7b 0a 20 20 46 75 6e 63 44 65  t */.){.  FuncDe
24ffd 66 20 2a 70 4f 74 68 65 72 3b 0a 20 20 69 6e 74  f *pOther;.  int
24ffe 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   nName = sqlite3
24fff 53 74 72 6c 65 6e 33 30 28 70 44 65 66 2d 3e 7a  Strlen30(pDef->z
25000 4e 61 6d 65 29 3b 0a 20 20 75 38 20 63 31 20 3d  Name);.  u8 c1 =
25001 20 28 75 38 29 70 44 65 66 2d 3e 7a 4e 61 6d 65   (u8)pDef->zName
25002 5b 30 5d 3b 0a 20 20 69 6e 74 20 68 20 3d 20 28  [0];.  int h = (
25003 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f  sqlite3UpperToLo
25004 77 65 72 5b 63 31 5d 20 2b 20 6e 4e 61 6d 65 29  wer[c1] + nName)
25005 20 25 20 41 72 72 61 79 53 69 7a 65 28 70 48 61   % ArraySize(pHa
25006 73 68 2d 3e 61 29 3b 0a 20 20 70 4f 74 68 65 72  sh->a);.  pOther
25007 20 3d 20 66 75 6e 63 74 69 6f 6e 53 65 61 72 63   = functionSearc
25008 68 28 70 48 61 73 68 2c 20 68 2c 20 70 44 65 66  h(pHash, h, pDef
25009 2d 3e 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b  ->zName, nName);
2500a 0a 20 20 69 66 28 20 70 4f 74 68 65 72 20 29 7b  .  if( pOther ){
2500b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 74  .    assert( pOt
2500c 68 65 72 21 3d 70 44 65 66 20 26 26 20 70 4f 74  her!=pDef && pOt
2500d 68 65 72 2d 3e 70 4e 65 78 74 21 3d 70 44 65 66  her->pNext!=pDef
2500e 20 29 3b 0a 20 20 20 20 70 44 65 66 2d 3e 70 4e   );.    pDef->pN
2500f 65 78 74 20 3d 20 70 4f 74 68 65 72 2d 3e 70 4e  ext = pOther->pN
25010 65 78 74 3b 0a 20 20 20 20 70 4f 74 68 65 72 2d  ext;.    pOther-
25011 3e 70 4e 65 78 74 20 3d 20 70 44 65 66 3b 0a 20  >pNext = pDef;. 
25012 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 44 65 66   }else{.    pDef
25013 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20  ->pNext = 0;.   
25014 20 70 44 65 66 2d 3e 70 48 61 73 68 20 3d 20 70   pDef->pHash = p
25015 48 61 73 68 2d 3e 61 5b 68 5d 3b 0a 20 20 20 20  Hash->a[h];.    
25016 70 48 61 73 68 2d 3e 61 5b 68 5d 20 3d 20 70 44  pHash->a[h] = pD
25017 65 66 3b 0a 20 20 7d 0a 7d 0a 20 20 0a 20 20 0a  ef;.  }.}.  .  .
25018 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 61 20  ./*.** Locate a 
25019 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 67 69  user function gi
2501a 76 65 6e 20 61 20 6e 61 6d 65 2c 20 61 20 6e 75  ven a name, a nu
2501b 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
2501c 73 20 61 6e 64 20 61 20 66 6c 61 67 0a 2a 2a 20  s and a flag.** 
2501d 69 6e 64 69 63 61 74 69 6e 67 20 77 68 65 74 68  indicating wheth
2501e 65 72 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  er the function 
2501f 70 72 65 66 65 72 73 20 55 54 46 2d 31 36 20 6f  prefers UTF-16 o
25020 76 65 72 20 55 54 46 2d 38 2e 20 20 52 65 74 75  ver UTF-8.  Retu
25021 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20  rn a.** pointer 
25022 74 6f 20 74 68 65 20 46 75 6e 63 44 65 66 20 73  to the FuncDef s
25023 74 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65  tructure that de
25024 66 69 6e 65 73 20 74 68 61 74 20 66 75 6e 63 74  fines that funct
25025 69 6f 6e 2c 20 6f 72 20 72 65 74 75 72 6e 0a 2a  ion, or return.*
25026 2a 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 66 75  * NULL if the fu
25027 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20  nction does not 
25028 65 78 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  exist..**.** If 
25029 74 68 65 20 63 72 65 61 74 65 46 6c 61 67 20 61  the createFlag a
2502a 72 67 75 6d 65 6e 74 20 69 73 20 74 72 75 65 2c  rgument is true,
2502b 20 74 68 65 6e 20 61 20 6e 65 77 20 28 62 6c 61   then a new (bla
2502c 6e 6b 29 20 46 75 6e 63 44 65 66 0a 2a 2a 20 73  nk) FuncDef.** s
2502d 74 72 75 63 74 75 72 65 20 69 73 20 63 72 65 61  tructure is crea
2502e 74 65 64 20 61 6e 64 20 6c 69 6b 65 64 20 69 6e  ted and liked in
2502f 74 6f 20 74 68 65 20 22 64 62 22 20 73 74 72 75  to the "db" stru
25030 63 74 75 72 65 20 69 66 20 61 0a 2a 2a 20 6e 6f  cture if a.** no
25031 20 6d 61 74 63 68 69 6e 67 20 66 75 6e 63 74 69   matching functi
25032 6f 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78  on previously ex
25033 69 73 74 65 64 2e 20 20 57 68 65 6e 20 63 72 65  isted.  When cre
25034 61 74 65 46 6c 61 67 20 69 73 20 74 72 75 65 0a  ateFlag is true.
25035 2a 2a 20 61 6e 64 20 74 68 65 20 6e 41 72 67 20  ** and the nArg 
25036 70 61 72 61 6d 65 74 65 72 20 69 73 20 2d 31 2c  parameter is -1,
25037 20 74 68 65 6e 20 6f 6e 6c 79 20 61 20 66 75 6e   then only a fun
25038 63 74 69 6f 6e 20 74 68 61 74 20 61 63 63 65 70  ction that accep
25039 74 73 0a 2a 2a 20 61 6e 79 20 6e 75 6d 62 65 72  ts.** any number
2503a 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 77 69   of arguments wi
2503b 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 2e 0a  ll be returned..
2503c 2a 2a 0a 2a 2a 20 49 66 20 63 72 65 61 74 65 46  **.** If createF
2503d 6c 61 67 20 69 73 20 66 61 6c 73 65 20 61 6e 64  lag is false and
2503e 20 6e 41 72 67 20 69 73 20 2d 31 2c 20 74 68 65   nArg is -1, the
2503f 6e 20 74 68 65 20 66 69 72 73 74 20 76 61 6c 69  n the first vali
25040 64 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 66 6f  d.** function fo
25041 75 6e 64 20 69 73 20 72 65 74 75 72 6e 65 64 2e  und is returned.
25042 20 20 41 20 66 75 6e 63 74 69 6f 6e 20 69 73 20    A function is 
25043 76 61 6c 69 64 20 69 66 20 65 69 74 68 65 72 20  valid if either 
25044 78 46 75 6e 63 0a 2a 2a 20 6f 72 20 78 53 74 65  xFunc.** or xSte
25045 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a  p is non-zero..*
25046 2a 0a 2a 2a 20 49 66 20 63 72 65 61 74 65 46 6c  *.** If createFl
25047 61 67 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65  ag is false, the
25048 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  n a function wit
25049 68 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6e  h the required n
2504a 61 6d 65 20 61 6e 64 0a 2a 2a 20 6e 75 6d 62 65  ame and.** numbe
2504b 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 6d  r of arguments m
2504c 61 79 20 62 65 20 72 65 74 75 72 6e 65 64 20 65  ay be returned e
2504d 76 65 6e 20 69 66 20 74 68 65 20 65 54 65 78 74  ven if the eText
2504e 52 65 70 20 66 6c 61 67 20 64 6f 65 73 20 6e 6f  Rep flag does no
2504f 74 0a 2a 2a 20 6d 61 74 63 68 20 74 68 61 74 20  t.** match that 
25050 72 65 71 75 65 73 74 65 64 2e 0a 2a 2f 0a 53 51  requested..*/.SQ
25051 4c 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e  LITE_PRIVATE Fun
25052 63 44 65 66 20 2a 73 71 6c 69 74 65 33 46 69 6e  cDef *sqlite3Fin
25053 64 46 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c  dFunction(.  sql
25054 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
25055 2f 2a 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 62  /* An open datab
25056 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ase */.  const c
25057 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 2f 2a 20 4e  har *zName, /* N
25058 61 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74  ame of the funct
25059 69 6f 6e 2e 20 20 4e 6f 74 20 6e 75 6c 6c 2d 74  ion.  Not null-t
2505a 65 72 6d 69 6e 61 74 65 64 20 2a 2f 0a 20 20 69  erminated */.  i
2505b 6e 74 20 6e 4e 61 6d 65 2c 20 20 20 20 20 20 20  nt nName,       
2505c 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
2505d 68 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 65  haracters in the
2505e 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e   name */.  int n
2505f 41 72 67 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Arg,          /*
25060 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   Number of argum
25061 65 6e 74 73 2e 20 20 2d 31 20 6d 65 61 6e 73 20  ents.  -1 means 
25062 61 6e 79 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  any number */.  
25063 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20  u8 enc,         
25064 20 20 20 2f 2a 20 50 72 65 66 65 72 72 65 64 20     /* Preferred 
25065 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f  text encoding */
25066 0a 20 20 69 6e 74 20 63 72 65 61 74 65 46 6c 61  .  int createFla
25067 67 20 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20  g     /* Create 
25068 6e 65 77 20 65 6e 74 72 79 20 69 66 20 74 72 75  new entry if tru
25069 65 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6f  e and does not o
2506a 74 68 65 72 77 69 73 65 20 65 78 69 73 74 20 2a  therwise exist *
2506b 2f 0a 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a  /.){.  FuncDef *
2506c 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74  p;         /* It
2506d 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20  erator variable 
2506e 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 42  */.  FuncDef *pB
2506f 65 73 74 20 3d 20 30 3b 20 2f 2a 20 42 65 73 74  est = 0; /* Best
25070 20 6d 61 74 63 68 20 66 6f 75 6e 64 20 73 6f 20   match found so 
25071 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 62 65 73  far */.  int bes
25072 74 53 63 6f 72 65 20 3d 20 30 3b 20 20 2f 2a 20  tScore = 0;  /* 
25073 53 63 6f 72 65 20 6f 66 20 62 65 73 74 20 6d 61  Score of best ma
25074 74 63 68 20 2a 2f 0a 20 20 69 6e 74 20 68 3b 20  tch */.  int h; 
25075 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
25076 48 61 73 68 20 76 61 6c 75 65 20 2a 2f 0a 0a 0a  Hash value */...
25077 20 20 61 73 73 65 72 74 28 20 65 6e 63 3d 3d 53    assert( enc==S
25078 51 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 65 6e  QLITE_UTF8 || en
25079 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  c==SQLITE_UTF16L
2507a 45 20 7c 7c 20 65 6e 63 3d 3d 53 51 4c 49 54 45  E || enc==SQLITE
2507b 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 68 20  _UTF16BE );.  h 
2507c 3d 20 28 73 71 6c 69 74 65 33 55 70 70 65 72 54  = (sqlite3UpperT
2507d 6f 4c 6f 77 65 72 5b 28 75 38 29 7a 4e 61 6d 65  oLower[(u8)zName
2507e 5b 30 5d 5d 20 2b 20 6e 4e 61 6d 65 29 20 25 20  [0]] + nName) % 
2507f 41 72 72 61 79 53 69 7a 65 28 64 62 2d 3e 61 46  ArraySize(db->aF
25080 75 6e 63 2e 61 29 3b 0a 0a 20 20 2f 2a 20 46 69  unc.a);..  /* Fi
25081 72 73 74 20 73 65 61 72 63 68 20 66 6f 72 20 61  rst search for a
25082 20 6d 61 74 63 68 20 61 6d 6f 6e 67 73 74 20 74   match amongst t
25083 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  he application-d
25084 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73  efined functions
25085 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d 20 66 75 6e  ..  */.  p = fun
25086 63 74 69 6f 6e 53 65 61 72 63 68 28 26 64 62 2d  ctionSearch(&db-
25087 3e 61 46 75 6e 63 2c 20 68 2c 20 7a 4e 61 6d 65  >aFunc, h, zName
25088 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 77 68 69 6c  , nName);.  whil
25089 65 28 20 70 20 29 7b 0a 20 20 20 20 69 6e 74 20  e( p ){.    int 
2508a 73 63 6f 72 65 20 3d 20 6d 61 74 63 68 51 75 61  score = matchQua
2508b 6c 69 74 79 28 70 2c 20 6e 41 72 67 2c 20 65 6e  lity(p, nArg, en
2508c 63 29 3b 0a 20 20 20 20 69 66 28 20 73 63 6f 72  c);.    if( scor
2508d 65 3e 62 65 73 74 53 63 6f 72 65 20 29 7b 0a 20  e>bestScore ){. 
2508e 20 20 20 20 20 70 42 65 73 74 20 3d 20 70 3b 0a       pBest = p;.
2508f 20 20 20 20 20 20 62 65 73 74 53 63 6f 72 65 20        bestScore 
25090 3d 20 73 63 6f 72 65 3b 0a 20 20 20 20 7d 0a 20  = score;.    }. 
25091 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b     p = p->pNext;
25092 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f  .  }..  /* If no
25093 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 2c   match is found,
25094 20 73 65 61 72 63 68 20 74 68 65 20 62 75 69 6c   search the buil
25095 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  t-in functions..
25096 20 20 2a 2a 0a 20 20 2a 2a 20 45 78 63 65 70 74    **.  ** Except
25097 2c 20 69 66 20 63 72 65 61 74 65 46 6c 61 67 20  , if createFlag 
25098 69 73 20 74 72 75 65 2c 20 74 68 61 74 20 6d 65  is true, that me
25099 61 6e 73 20 74 68 61 74 20 77 65 20 61 72 65 20  ans that we are 
2509a 74 72 79 69 6e 67 20 74 6f 0a 20 20 2a 2a 20 69  trying to.  ** i
2509b 6e 73 74 61 6c 6c 20 61 20 6e 65 77 20 66 75 6e  nstall a new fun
2509c 63 74 69 6f 6e 2e 20 20 57 68 61 74 65 76 65 72  ction.  Whatever
2509d 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75   FuncDef structu
2509e 72 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 77  re is returned w
2509f 69 6c 6c 0a 20 20 2a 2a 20 68 61 76 65 20 66 69  ill.  ** have fi
250a0 65 6c 64 73 20 6f 76 65 72 77 72 69 74 74 65 6e  elds overwritten
250a1 20 77 69 74 68 20 6e 65 77 20 69 6e 66 6f 72 6d   with new inform
250a2 61 74 69 6f 6e 20 61 70 70 72 6f 70 72 69 61 74  ation appropriat
250a3 65 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6e  e for the.  ** n
250a4 65 77 20 66 75 6e 63 74 69 6f 6e 2e 20 20 42 75  ew function.  Bu
250a5 74 20 74 68 65 20 46 75 6e 63 44 65 66 73 20 66  t the FuncDefs f
250a6 6f 72 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63  or built-in func
250a7 74 69 6f 6e 73 20 61 72 65 20 72 65 61 64 2d 6f  tions are read-o
250a8 6e 6c 79 2e 0a 20 20 2a 2a 20 53 6f 20 77 65 20  nly..  ** So we 
250a9 6d 75 73 74 20 6e 6f 74 20 73 65 61 72 63 68 20  must not search 
250aa 66 6f 72 20 62 75 69 6c 74 2d 69 6e 73 20 77 68  for built-ins wh
250ab 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e 65  en creating a ne
250ac 77 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f  w function..  */
250ad 20 0a 20 20 69 66 28 20 21 63 72 65 61 74 65 46   .  if( !createF
250ae 6c 61 67 20 26 26 20 21 70 42 65 73 74 20 29 7b  lag && !pBest ){
250af 0a 20 20 20 20 46 75 6e 63 44 65 66 48 61 73 68  .    FuncDefHash
250b0 20 2a 70 48 61 73 68 20 3d 20 26 47 4c 4f 42 41   *pHash = &GLOBA
250b1 4c 28 46 75 6e 63 44 65 66 48 61 73 68 2c 20 73  L(FuncDefHash, s
250b2 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63  qlite3GlobalFunc
250b3 74 69 6f 6e 73 29 3b 0a 20 20 20 20 70 20 3d 20  tions);.    p = 
250b4 66 75 6e 63 74 69 6f 6e 53 65 61 72 63 68 28 70  functionSearch(p
250b5 48 61 73 68 2c 20 68 2c 20 7a 4e 61 6d 65 2c 20  Hash, h, zName, 
250b6 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 77 68 69 6c  nName);.    whil
250b7 65 28 20 70 20 29 7b 0a 20 20 20 20 20 20 69 6e  e( p ){.      in
250b8 74 20 73 63 6f 72 65 20 3d 20 6d 61 74 63 68 51  t score = matchQ
250b9 75 61 6c 69 74 79 28 70 2c 20 6e 41 72 67 2c 20  uality(p, nArg, 
250ba 65 6e 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20  enc);.      if( 
250bb 73 63 6f 72 65 3e 62 65 73 74 53 63 6f 72 65 20  score>bestScore 
250bc 29 7b 0a 20 20 20 20 20 20 20 20 70 42 65 73 74  ){.        pBest
250bd 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 62 65   = p;.        be
250be 73 74 53 63 6f 72 65 20 3d 20 73 63 6f 72 65 3b  stScore = score;
250bf 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
250c0 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20   = p->pNext;.   
250c1 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
250c2 74 68 65 20 63 72 65 61 74 65 46 6c 61 67 20 70  the createFlag p
250c3 61 72 61 6d 65 74 65 72 20 69 73 20 74 72 75 65  arameter is true
250c4 20 61 6e 64 20 74 68 65 20 73 65 61 72 63 68 20   and the search 
250c5 64 69 64 20 6e 6f 74 20 72 65 76 65 61 6c 20 61  did not reveal a
250c6 6e 0a 20 20 2a 2a 20 65 78 61 63 74 20 6d 61 74  n.  ** exact mat
250c7 63 68 20 66 6f 72 20 74 68 65 20 6e 61 6d 65 2c  ch for the name,
250c8 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   number of argum
250c9 65 6e 74 73 20 61 6e 64 20 65 6e 63 6f 64 69 6e  ents and encodin
250ca 67 2c 20 74 68 65 6e 20 61 64 64 20 61 0a 20 20  g, then add a.  
250cb 2a 2a 20 6e 65 77 20 65 6e 74 72 79 20 74 6f 20  ** new entry to 
250cc 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 61  the hash table a
250cd 6e 64 20 72 65 74 75 72 6e 20 69 74 2e 0a 20 20  nd return it..  
250ce 2a 2f 0a 20 20 69 66 28 20 63 72 65 61 74 65 46  */.  if( createF
250cf 6c 61 67 20 26 26 20 28 62 65 73 74 53 63 6f 72  lag && (bestScor
250d0 65 3c 36 20 7c 7c 20 70 42 65 73 74 2d 3e 6e 41  e<6 || pBest->nA
250d1 72 67 21 3d 6e 41 72 67 29 20 26 26 20 0a 20 20  rg!=nArg) && .  
250d2 20 20 20 20 28 70 42 65 73 74 20 3d 20 73 71 6c      (pBest = sql
250d3 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
250d4 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 42 65  (db, sizeof(*pBe
250d5 73 74 29 2b 6e 4e 61 6d 65 2b 31 29 29 21 3d 30  st)+nName+1))!=0
250d6 20 29 7b 0a 20 20 20 20 70 42 65 73 74 2d 3e 7a   ){.    pBest->z
250d7 4e 61 6d 65 20 3d 20 28 63 68 61 72 20 2a 29 26  Name = (char *)&
250d8 70 42 65 73 74 5b 31 5d 3b 0a 20 20 20 20 70 42  pBest[1];.    pB
250d9 65 73 74 2d 3e 6e 41 72 67 20 3d 20 28 75 31 36  est->nArg = (u16
250da 29 6e 41 72 67 3b 0a 20 20 20 20 70 42 65 73 74  )nArg;.    pBest
250db 2d 3e 69 50 72 65 66 45 6e 63 20 3d 20 65 6e 63  ->iPrefEnc = enc
250dc 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 42 65  ;.    memcpy(pBe
250dd 73 74 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65  st->zName, zName
250de 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 70 42  , nName);.    pB
250df 65 73 74 2d 3e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65  est->zName[nName
250e0 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74  ] = 0;.    sqlit
250e1 65 33 46 75 6e 63 44 65 66 49 6e 73 65 72 74 28  e3FuncDefInsert(
250e2 26 64 62 2d 3e 61 46 75 6e 63 2c 20 70 42 65 73  &db->aFunc, pBes
250e3 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70  t);.  }..  if( p
250e4 42 65 73 74 20 26 26 20 28 70 42 65 73 74 2d 3e  Best && (pBest->
250e5 78 53 74 65 70 20 7c 7c 20 70 42 65 73 74 2d 3e  xStep || pBest->
250e6 78 46 75 6e 63 20 7c 7c 20 63 72 65 61 74 65 46  xFunc || createF
250e7 6c 61 67 29 20 29 7b 0a 20 20 20 20 72 65 74 75  lag) ){.    retu
250e8 72 6e 20 70 42 65 73 74 3b 0a 20 20 7d 0a 20 20  rn pBest;.  }.  
250e9 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
250ea 2a 2a 20 46 72 65 65 20 61 6c 6c 20 72 65 73 6f  ** Free all reso
250eb 75 72 63 65 73 20 68 65 6c 64 20 62 79 20 74 68  urces held by th
250ec 65 20 73 63 68 65 6d 61 20 73 74 72 75 63 74 75  e schema structu
250ed 72 65 2e 20 54 68 65 20 76 6f 69 64 2a 20 61 72  re. The void* ar
250ee 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 0a 2a 2a  gument points.**
250ef 20 61 74 20 61 20 53 63 68 65 6d 61 20 73 74 72   at a Schema str
250f0 75 63 74 2e 20 54 68 69 73 20 66 75 6e 63 74 69  uct. This functi
250f1 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 63 61 6c 6c  on does not call
250f2 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
250f3 62 2c 20 29 20 6f 6e 20 74 68 65 20 0a 2a 2a 20  b, ) on the .** 
250f4 70 6f 69 6e 74 65 72 20 69 74 73 65 6c 66 2c 20  pointer itself, 
250f5 69 74 20 6a 75 73 74 20 63 6c 65 61 6e 73 20 75  it just cleans u
250f6 70 20 73 75 62 73 69 64 75 61 72 79 20 72 65 73  p subsiduary res
250f7 6f 75 72 63 65 73 20 28 69 2e 65 2e 20 74 68 65  ources (i.e. the
250f8 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20   contents.** of 
250f9 74 68 65 20 73 63 68 65 6d 61 20 68 61 73 68 20  the schema hash 
250fa 74 61 62 6c 65 73 29 2e 0a 2a 2a 0a 2a 2a 20 54  tables)..**.** T
250fb 68 65 20 53 63 68 65 6d 61 2e 63 61 63 68 65 5f  he Schema.cache_
250fc 73 69 7a 65 20 76 61 72 69 61 62 6c 65 20 69 73  size variable is
250fd 20 6e 6f 74 20 63 6c 65 61 72 65 64 2e 0a 2a 2f   not cleared..*/
250fe 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
250ff 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63 68 65  void sqlite3Sche
25100 6d 61 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b  maFree(void *p){
25101 0a 20 20 48 61 73 68 20 74 65 6d 70 31 3b 0a 20  .  Hash temp1;. 
25102 20 48 61 73 68 20 74 65 6d 70 32 3b 0a 20 20 48   Hash temp2;.  H
25103 61 73 68 45 6c 65 6d 20 2a 70 45 6c 65 6d 3b 0a  ashElem *pElem;.
25104 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
25105 61 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 70 3b  a = (Schema *)p;
25106 0a 0a 20 20 74 65 6d 70 31 20 3d 20 70 53 63 68  ..  temp1 = pSch
25107 65 6d 61 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20  ema->tblHash;.  
25108 74 65 6d 70 32 20 3d 20 70 53 63 68 65 6d 61 2d  temp2 = pSchema-
25109 3e 74 72 69 67 48 61 73 68 3b 0a 20 20 73 71 6c  >trigHash;.  sql
2510a 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 53  ite3HashInit(&pS
2510b 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 68 29  chema->trigHash)
2510c 3b 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68 43  ;.  sqlite3HashC
2510d 6c 65 61 72 28 26 70 53 63 68 65 6d 61 2d 3e 69  lear(&pSchema->i
2510e 64 78 48 61 73 68 29 3b 0a 20 20 66 6f 72 28 70  dxHash);.  for(p
2510f 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 46  Elem=sqliteHashF
25110 69 72 73 74 28 26 74 65 6d 70 32 29 3b 20 70 45  irst(&temp2); pE
25111 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c 69 74  lem; pElem=sqlit
25112 65 48 61 73 68 4e 65 78 74 28 70 45 6c 65 6d 29  eHashNext(pElem)
25113 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  ){.    sqlite3De
25114 6c 65 74 65 54 72 69 67 67 65 72 28 30 2c 20 28  leteTrigger(0, (
25115 54 72 69 67 67 65 72 2a 29 73 71 6c 69 74 65 48  Trigger*)sqliteH
25116 61 73 68 44 61 74 61 28 70 45 6c 65 6d 29 29 3b  ashData(pElem));
25117 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 48 61  .  }.  sqlite3Ha
25118 73 68 43 6c 65 61 72 28 26 74 65 6d 70 32 29 3b  shClear(&temp2);
25119 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e  .  sqlite3HashIn
2511a 69 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c  it(&pSchema->tbl
2511b 48 61 73 68 29 3b 0a 20 20 66 6f 72 28 70 45 6c  Hash);.  for(pEl
2511c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72  em=sqliteHashFir
2511d 73 74 28 26 74 65 6d 70 31 29 3b 20 70 45 6c 65  st(&temp1); pEle
2511e 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48  m; pElem=sqliteH
2511f 61 73 68 4e 65 78 74 28 70 45 6c 65 6d 29 29 7b  ashNext(pElem)){
25120 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
25121 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 74   = sqliteHashDat
25122 61 28 70 45 6c 65 6d 29 3b 0a 20 20 20 20 61 73  a(pElem);.    as
25123 73 65 72 74 28 20 70 54 61 62 2d 3e 64 62 4d 65  sert( pTab->dbMe
25124 6d 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69  m==0 );.    sqli
25125 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70  te3DeleteTable(p
25126 54 61 62 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  Tab);.  }.  sqli
25127 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 74 65  te3HashClear(&te
25128 6d 70 31 29 3b 0a 20 20 70 53 63 68 65 6d 61 2d  mp1);.  pSchema-
25129 3e 70 53 65 71 54 61 62 20 3d 20 30 3b 0a 20 20  >pSeqTab = 0;.  
2512a 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 20 26  pSchema->flags &
2512b 3d 20 7e 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64  = ~DB_SchemaLoad
2512c 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e  ed;.}../*.** Fin
2512d 64 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65  d and return the
2512e 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74   schema associat
2512f 65 64 20 77 69 74 68 20 61 20 42 54 72 65 65 2e  ed with a BTree.
25130 20 20 43 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65    Create.** a ne
25131 77 20 6f 6e 65 20 69 66 20 6e 65 63 65 73 73 61  w one if necessa
25132 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ry..*/.SQLITE_PR
25133 49 56 41 54 45 20 53 63 68 65 6d 61 20 2a 73 71  IVATE Schema *sq
25134 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74 28 73  lite3SchemaGet(s
25135 71 6c 69 74 65 33 20 2a 64 62 2c 20 42 74 72 65  qlite3 *db, Btre
25136 65 20 2a 70 42 74 29 7b 0a 20 20 53 63 68 65 6d  e *pBt){.  Schem
25137 61 20 2a 20 70 3b 0a 20 20 69 66 28 20 70 42 74  a * p;.  if( pBt
25138 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 53 63 68   ){.    p = (Sch
25139 65 6d 61 20 2a 29 73 71 6c 69 74 65 33 42 74 72  ema *)sqlite3Btr
2513a 65 65 53 63 68 65 6d 61 28 70 42 74 2c 20 73 69  eeSchema(pBt, si
2513b 7a 65 6f 66 28 53 63 68 65 6d 61 29 2c 20 73 71  zeof(Schema), sq
2513c 6c 69 74 65 33 53 63 68 65 6d 61 46 72 65 65 29  lite3SchemaFree)
2513d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
2513e 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 73 71 6c   = (Schema *)sql
2513f 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73  ite3MallocZero(s
25140 69 7a 65 6f 66 28 53 63 68 65 6d 61 29 29 3b 0a  izeof(Schema));.
25141 20 20 7d 0a 20 20 69 66 28 20 21 70 20 29 7b 0a    }.  if( !p ){.
25142 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
25143 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73  iled = 1;.  }els
25144 65 20 69 66 20 28 20 30 3d 3d 70 2d 3e 66 69 6c  e if ( 0==p->fil
25145 65 5f 66 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20  e_format ){.    
25146 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28  sqlite3HashInit(
25147 26 70 2d 3e 74 62 6c 48 61 73 68 29 3b 0a 20 20  &p->tblHash);.  
25148 20 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69    sqlite3HashIni
25149 74 28 26 70 2d 3e 69 64 78 48 61 73 68 29 3b 0a  t(&p->idxHash);.
2514a 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 49      sqlite3HashI
2514b 6e 69 74 28 26 70 2d 3e 74 72 69 67 48 61 73 68  nit(&p->trigHash
2514c 29 3b 0a 20 20 20 20 70 2d 3e 65 6e 63 20 3d 20  );.    p->enc = 
2514d 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 7d  SQLITE_UTF8;.  }
2514e 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
2514f 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
25150 45 6e 64 20 6f 66 20 63 61 6c 6c 62 61 63 6b 2e  End of callback.
25151 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
25152 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25153 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
25154 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
25155 42 65 67 69 6e 20 66 69 6c 65 20 64 65 6c 65 74  Begin file delet
25156 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
25157 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25158 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
25159 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
2515a 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
2515b 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
2515c 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
2515d 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
2515e 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
2515f 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
25160 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
25161 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
25162 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
25163 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
25164 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
25165 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
25166 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
25167 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
25168 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
25169 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
2516a 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
2516b 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
2516c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2516d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2516e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2516f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25170 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
25171 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63  ile contains C c
25172 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61  ode routines tha
25173 74 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20  t are called by 
25174 74 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 69 6e  the parser.** in
25175 20 6f 72 64 65 72 20 74 6f 20 67 65 6e 65 72 61   order to genera
25176 74 65 20 63 6f 64 65 20 66 6f 72 20 44 45 4c 45  te code for DELE
25177 54 45 20 46 52 4f 4d 20 73 74 61 74 65 6d 65 6e  TE FROM statemen
25178 74 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 64  ts..**.** $Id: d
25179 65 6c 65 74 65 2e 63 2c 76 20 31 2e 32 30 37 20  elete.c,v 1.207 
2517a 32 30 30 39 2f 30 38 2f 30 38 20 31 38 3a 30 31  2009/08/08 18:01
2517b 3a 30 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :08 drh Exp $.*/
2517c 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b 20 75 70 20  ../*.** Look up 
2517d 65 76 65 72 79 20 74 61 62 6c 65 20 74 68 61 74  every table that
2517e 20 69 73 20 6e 61 6d 65 64 20 69 6e 20 70 53 72   is named in pSr
2517f 63 2e 20 20 49 66 20 61 6e 79 20 74 61 62 6c 65  c.  If any table
25180 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 0a 2a   is not found,.*
25181 2a 20 61 64 64 20 61 6e 20 65 72 72 6f 72 20 6d  * add an error m
25182 65 73 73 61 67 65 20 74 6f 20 70 50 61 72 73 65  essage to pParse
25183 2d 3e 7a 45 72 72 4d 73 67 20 61 6e 64 20 72 65  ->zErrMsg and re
25184 74 75 72 6e 20 4e 55 4c 4c 2e 20 20 49 66 20 61  turn NULL.  If a
25185 6c 6c 20 74 61 62 6c 65 73 0a 2a 2a 20 61 72 65  ll tables.** are
25186 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20 61   found, return a
25187 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
25188 6c 61 73 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53  last table..*/.S
25189 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 61  QLITE_PRIVATE Ta
2518a 62 6c 65 20 2a 73 71 6c 69 74 65 33 53 72 63 4c  ble *sqlite3SrcL
2518b 69 73 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20  istLookup(Parse 
2518c 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74  *pParse, SrcList
2518d 20 2a 70 53 72 63 29 7b 0a 20 20 73 74 72 75 63   *pSrc){.  struc
2518e 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
2518f 70 49 74 65 6d 20 3d 20 70 53 72 63 2d 3e 61 3b  pItem = pSrc->a;
25190 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a  .  Table *pTab;.
25191 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d 20    assert( pItem 
25192 26 26 20 70 53 72 63 2d 3e 6e 53 72 63 3d 3d 31  && pSrc->nSrc==1
25193 20 29 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c   );.  pTab = sql
25194 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
25195 70 50 61 72 73 65 2c 20 30 2c 20 70 49 74 65 6d  pParse, 0, pItem
25196 2d 3e 7a 4e 61 6d 65 2c 20 70 49 74 65 6d 2d 3e  ->zName, pItem->
25197 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 73 71  zDatabase);.  sq
25198 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65  lite3DeleteTable
25199 28 70 49 74 65 6d 2d 3e 70 54 61 62 29 3b 0a 20  (pItem->pTab);. 
2519a 20 70 49 74 65 6d 2d 3e 70 54 61 62 20 3d 20 70   pItem->pTab = p
2519b 54 61 62 3b 0a 20 20 69 66 28 20 70 54 61 62 20  Tab;.  if( pTab 
2519c 29 7b 0a 20 20 20 20 70 54 61 62 2d 3e 6e 52 65  ){.    pTab->nRe
2519d 66 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  f++;.  }.  if( s
2519e 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79 4c  qlite3IndexedByL
2519f 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 49  ookup(pParse, pI
251a0 74 65 6d 29 20 29 7b 0a 20 20 20 20 70 54 61 62  tem) ){.    pTab
251a1 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 0;.  }.  retu
251a2 72 6e 20 70 54 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pTab;.}../*.*
251a3 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20  * Check to make 
251a4 73 75 72 65 20 74 68 65 20 67 69 76 65 6e 20 74  sure the given t
251a5 61 62 6c 65 20 69 73 20 77 72 69 74 61 62 6c 65  able is writable
251a6 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74 0a  .  If it is not.
251a7 2a 2a 20 77 72 69 74 61 62 6c 65 2c 20 67 65 6e  ** writable, gen
251a8 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20 6d  erate an error m
251a9 65 73 73 61 67 65 20 61 6e 64 20 72 65 74 75 72  essage and retur
251aa 6e 20 31 2e 20 20 49 66 20 69 74 20 69 73 0a 2a  n 1.  If it is.*
251ab 2a 20 77 72 69 74 61 62 6c 65 20 72 65 74 75 72  * writable retur
251ac 6e 20 30 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  n 0;.*/.SQLITE_P
251ad 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
251ae 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 50 61 72  e3IsReadOnly(Par
251af 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c  se *pParse, Tabl
251b0 65 20 2a 70 54 61 62 2c 20 69 6e 74 20 76 69 65  e *pTab, int vie
251b1 77 4f 6b 29 7b 0a 20 20 2f 2a 20 41 20 74 61 62  wOk){.  /* A tab
251b2 6c 65 20 69 73 20 6e 6f 74 20 77 72 69 74 61 62  le is not writab
251b3 6c 65 20 75 6e 64 65 72 20 74 68 65 20 66 6f 6c  le under the fol
251b4 6c 6f 77 69 6e 67 20 63 69 72 63 75 6d 73 74 61  lowing circumsta
251b5 6e 63 65 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  nces:.  **.  ** 
251b6 20 20 31 29 20 49 74 20 69 73 20 61 20 76 69 72    1) It is a vir
251b7 74 75 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 6e  tual table and n
251b8 6f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  o implementation
251b9 20 6f 66 20 74 68 65 20 78 55 70 64 61 74 65 20   of the xUpdate 
251ba 6d 65 74 68 6f 64 0a 20 20 2a 2a 20 20 20 20 20  method.  **     
251bb 20 68 61 73 20 62 65 65 6e 20 70 72 6f 76 69 64   has been provid
251bc 65 64 2c 20 6f 72 0a 20 20 2a 2a 20 20 20 32 29  ed, or.  **   2)
251bd 20 49 74 20 69 73 20 61 20 73 79 73 74 65 6d 20   It is a system 
251be 74 61 62 6c 65 20 28 69 2e 65 2e 20 73 71 6c 69  table (i.e. sqli
251bf 74 65 5f 6d 61 73 74 65 72 29 2c 20 74 68 69 73  te_master), this
251c0 20 63 61 6c 6c 20 69 73 20 6e 6f 74 0a 20 20 2a   call is not.  *
251c1 2a 20 20 20 20 20 20 70 61 72 74 20 6f 66 20 61  *      part of a
251c2 20 6e 65 73 74 65 64 20 70 61 72 73 65 20 61 6e   nested parse an
251c3 64 20 77 72 69 74 61 62 6c 65 5f 73 63 68 65 6d  d writable_schem
251c4 61 20 70 72 61 67 6d 61 20 68 61 73 20 6e 6f 74  a pragma has not
251c5 20 0a 20 20 2a 2a 20 20 20 20 20 20 62 65 65 6e   .  **      been
251c6 20 73 70 65 63 69 66 69 65 64 2e 0a 20 20 2a 2a   specified..  **
251c7 0a 20 20 2a 2a 20 49 6e 20 65 69 74 68 65 72 20  .  ** In either 
251c8 63 61 73 65 20 6c 65 61 76 65 20 61 6e 20 65 72  case leave an er
251c9 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70  ror message in p
251ca 50 61 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e  Parse and return
251cb 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 20 20 2a 2f 0a   non-zero..  */.
251cc 20 20 69 66 28 20 28 20 49 73 56 69 72 74 75 61    if( ( IsVirtua
251cd 6c 28 70 54 61 62 29 20 0a 20 20 20 20 20 26 26  l(pTab) .     &&
251ce 20 73 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c   sqlite3GetVTabl
251cf 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  e(pParse->db, pT
251d0 61 62 29 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f 64 75  ab)->pMod->pModu
251d1 6c 65 2d 3e 78 55 70 64 61 74 65 3d 3d 30 20 29  le->xUpdate==0 )
251d2 0a 20 20 20 7c 7c 20 28 20 28 70 54 61 62 2d 3e  .   || ( (pTab->
251d3 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 52 65  tabFlags & TF_Re
251d4 61 64 6f 6e 6c 79 29 21 3d 30 0a 20 20 20 20 20  adonly)!=0.     
251d5 26 26 20 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e  && (pParse->db->
251d6 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 57  flags & SQLITE_W
251d7 72 69 74 65 53 63 68 65 6d 61 29 3d 3d 30 0a 20  riteSchema)==0. 
251d8 20 20 20 20 26 26 20 70 50 61 72 73 65 2d 3e 6e      && pParse->n
251d9 65 73 74 65 64 3d 3d 30 20 29 0a 20 20 29 7b 0a  ested==0 ).  ){.
251da 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
251db 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62  Msg(pParse, "tab
251dc 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65  le %s may not be
251dd 20 6d 6f 64 69 66 69 65 64 22 2c 20 70 54 61 62   modified", pTab
251de 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65  ->zName);.    re
251df 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 23 69 66  turn 1;.  }..#if
251e0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
251e1 5f 56 49 45 57 0a 20 20 69 66 28 20 21 76 69 65  _VIEW.  if( !vie
251e2 77 4f 6b 20 26 26 20 70 54 61 62 2d 3e 70 53 65  wOk && pTab->pSe
251e3 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  lect ){.    sqli
251e4 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
251e5 73 65 2c 22 63 61 6e 6e 6f 74 20 6d 6f 64 69 66  se,"cannot modif
251e6 79 20 25 73 20 62 65 63 61 75 73 65 20 69 74 20  y %s because it 
251e7 69 73 20 61 20 76 69 65 77 22 2c 70 54 61 62 2d  is a view",pTab-
251e8 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74  >zName);.    ret
251e9 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69  urn 1;.  }.#endi
251ea 66 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  f.  return 0;.}.
251eb 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
251ec 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
251ed 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
251ee 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
251ef 29 0a 2f 2a 0a 2a 2a 20 45 76 61 6c 75 61 74 65  )./*.** Evaluate
251f0 20 61 20 76 69 65 77 20 61 6e 64 20 73 74 6f 72   a view and stor
251f1 65 20 69 74 73 20 72 65 73 75 6c 74 20 69 6e 20  e its result in 
251f2 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62  an ephemeral tab
251f3 6c 65 2e 20 20 54 68 65 0a 2a 2a 20 70 57 68 65  le.  The.** pWhe
251f4 72 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61  re argument is a
251f5 6e 20 6f 70 74 69 6f 6e 61 6c 20 57 48 45 52 45  n optional WHERE
251f6 20 63 6c 61 75 73 65 20 74 68 61 74 20 72 65 73   clause that res
251f7 74 72 69 63 74 73 20 74 68 65 0a 2a 2a 20 73 65  tricts the.** se
251f8 74 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65  t of rows in the
251f9 20 76 69 65 77 20 74 68 61 74 20 61 72 65 20 74   view that are t
251fa 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20 74 68  o be added to th
251fb 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  e ephemeral tabl
251fc 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
251fd 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
251fe 33 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65 77  3MaterializeView
251ff 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
25200 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73  e,       /* Pars
25201 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
25202 20 54 61 62 6c 65 20 2a 70 56 69 65 77 2c 20 20   Table *pView,  
25203 20 20 20 20 20 20 2f 2a 20 56 69 65 77 20 64 65        /* View de
25204 66 69 6e 69 74 69 6f 6e 20 2a 2f 0a 20 20 45 78  finition */.  Ex
25205 70 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20 20  pr *pWhere,     
25206 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 57     /* Optional W
25207 48 45 52 45 20 63 6c 61 75 73 65 20 74 6f 20 62  HERE clause to b
25208 65 20 61 64 64 65 64 20 2a 2f 0a 20 20 69 6e 74  e added */.  int
25209 20 69 43 75 72 20 20 20 20 20 20 20 20 20 20 20   iCur           
2520a 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62    /* Cursor numb
2520b 65 72 20 66 6f 72 20 65 70 68 65 6d 65 72 69 61  er for ephemeria
2520c 6c 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20  l table */.){.  
2520d 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b  SelectDest dest;
2520e 0a 20 20 53 65 6c 65 63 74 20 2a 70 44 75 70 3b  .  Select *pDup;
2520f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
25210 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20   pParse->db;..  
25211 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 53 65  pDup = sqlite3Se
25212 6c 65 63 74 44 75 70 28 64 62 2c 20 70 56 69 65  lectDup(db, pVie
25213 77 2d 3e 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a  w->pSelect, 0);.
25214 20 20 69 66 28 20 70 57 68 65 72 65 20 29 7b 0a    if( pWhere ){.
25215 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 46 72      SrcList *pFr
25216 6f 6d 3b 0a 20 20 20 20 0a 20 20 20 20 70 57 68  om;.    .    pWh
25217 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ere = sqlite3Exp
25218 72 44 75 70 28 64 62 2c 20 70 57 68 65 72 65 2c  rDup(db, pWhere,
25219 20 30 29 3b 0a 20 20 20 20 70 46 72 6f 6d 20 3d   0);.    pFrom =
2521a 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
2521b 70 70 65 6e 64 28 64 62 2c 20 30 2c 20 30 2c 20  ppend(db, 0, 0, 
2521c 30 29 3b 0a 20 20 20 20 69 66 28 20 70 46 72 6f  0);.    if( pFro
2521d 6d 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  m ){.      asser
2521e 74 28 20 70 46 72 6f 6d 2d 3e 6e 53 72 63 3d 3d  t( pFrom->nSrc==
2521f 31 20 29 3b 0a 20 20 20 20 20 20 70 46 72 6f 6d  1 );.      pFrom
25220 2d 3e 61 5b 30 5d 2e 7a 41 6c 69 61 73 20 3d 20  ->a[0].zAlias = 
25221 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
25222 64 62 2c 20 70 56 69 65 77 2d 3e 7a 4e 61 6d 65  db, pView->zName
25223 29 3b 0a 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e  );.      pFrom->
25224 61 5b 30 5d 2e 70 53 65 6c 65 63 74 20 3d 20 70  a[0].pSelect = p
25225 44 75 70 3b 0a 20 20 20 20 20 20 61 73 73 65 72  Dup;.      asser
25226 74 28 20 70 46 72 6f 6d 2d 3e 61 5b 30 5d 2e 70  t( pFrom->a[0].p
25227 4f 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 61  On==0 );.      a
25228 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 61 5b  ssert( pFrom->a[
25229 30 5d 2e 70 55 73 69 6e 67 3d 3d 30 20 29 3b 0a  0].pUsing==0 );.
2522a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2522b 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
2522c 6c 65 74 65 28 64 62 2c 20 70 44 75 70 29 3b 0a  lete(db, pDup);.
2522d 20 20 20 20 7d 0a 20 20 20 20 70 44 75 70 20 3d      }.    pDup =
2522e 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65   sqlite3SelectNe
2522f 77 28 70 50 61 72 73 65 2c 20 30 2c 20 70 46 72  w(pParse, 0, pFr
25230 6f 6d 2c 20 70 57 68 65 72 65 2c 20 30 2c 20 30  om, pWhere, 0, 0
25231 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  , 0, 0, 0, 0);. 
25232 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65   }.  sqlite3Sele
25233 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74  ctDestInit(&dest
25234 2c 20 53 52 54 5f 45 70 68 65 6d 54 61 62 2c 20  , SRT_EphemTab, 
25235 69 43 75 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  iCur);.  sqlite3
25236 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
25237 44 75 70 2c 20 26 64 65 73 74 29 3b 0a 20 20 73  Dup, &dest);.  s
25238 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
25239 74 65 28 64 62 2c 20 70 44 75 70 29 3b 0a 7d 0a  te(db, pDup);.}.
2523a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
2523b 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
2523c 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65 64  IEW) && !defined
2523d 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49  (SQLITE_OMIT_TRI
2523e 47 47 45 52 29 20 2a 2f 0a 0a 23 69 66 20 64 65  GGER) */..#if de
2523f 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
25240 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54  BLE_UPDATE_DELET
25241 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66  E_LIMIT) && !def
25242 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
25243 5f 53 55 42 51 55 45 52 59 29 0a 2f 2a 0a 2a 2a  _SUBQUERY)./*.**
25244 20 47 65 6e 65 72 61 74 65 20 61 6e 20 65 78 70   Generate an exp
25245 72 65 73 73 69 6f 6e 20 74 72 65 65 20 74 6f 20  ression tree to 
25246 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 57 48  implement the WH
25247 45 52 45 2c 20 4f 52 44 45 52 20 42 59 2c 0a 2a  ERE, ORDER BY,.*
25248 2a 20 61 6e 64 20 4c 49 4d 49 54 2f 4f 46 46 53  * and LIMIT/OFFS
25249 45 54 20 70 6f 72 74 69 6f 6e 20 6f 66 20 44 45  ET portion of DE
2524a 4c 45 54 45 20 61 6e 64 20 55 50 44 41 54 45 20  LETE and UPDATE 
2524b 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a  statements..**.*
2524c 2a 20 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f  *     DELETE FRO
2524d 4d 20 74 61 62 6c 65 5f 77 78 79 7a 20 57 48 45  M table_wxyz WHE
2524e 52 45 20 61 3c 35 20 4f 52 44 45 52 20 42 59 20  RE a<5 ORDER BY 
2524f 61 20 4c 49 4d 49 54 20 31 3b 0a 2a 2a 20 20 20  a LIMIT 1;.**   
25250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25251 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f           \______
25252 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
25253 5f 5f 5f 5f 2f 0a 2a 2a 20 20 20 20 20 20 20 20  ____/.**        
25254 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25255 20 20 20 20 20 20 20 70 4c 69 6d 69 74 57 68 65         pLimitWhe
25256 72 65 20 28 70 49 6e 43 6c 61 75 73 65 29 0a 2a  re (pInClause).*
25257 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
25258 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 4c 69   Expr *sqlite3Li
25259 6d 69 74 57 68 65 72 65 28 0a 20 20 50 61 72 73  mitWhere(.  Pars
2525a 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2525b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2525c 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  parser context *
2525d 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  /.  SrcList *pSr
2525e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c,              
2525f 20 2f 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   /* the FROM cla
25260 75 73 65 20 2d 2d 20 77 68 69 63 68 20 74 61 62  use -- which tab
25261 6c 65 73 20 74 6f 20 73 63 61 6e 20 2a 2f 0a 20  les to scan */. 
25262 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20   Expr *pWhere,  
25263 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
25264 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
25265 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20  e.  May be null 
25266 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
25267 4f 72 64 65 72 42 79 2c 20 20 20 20 20 20 20 20  OrderBy,        
25268 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42    /* The ORDER B
25269 59 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62  Y clause.  May b
2526a 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 45 78 70 72  e null */.  Expr
2526b 20 2a 70 4c 69 6d 69 74 2c 20 20 20 20 20 20 20   *pLimit,       
2526c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2526d 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 20 20 4d  LIMIT clause.  M
2526e 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20  ay be null */.  
2526f 45 78 70 72 20 2a 70 4f 66 66 73 65 74 2c 20 20  Expr *pOffset,  
25270 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
25271 54 68 65 20 4f 46 46 53 45 54 20 63 6c 61 75 73  The OFFSET claus
25272 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20  e.  May be null 
25273 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53 74 6d 74  */.  char *zStmt
25274 54 79 70 65 20 20 20 20 20 20 20 20 20 20 20 20  Type            
25275 20 20 2f 2a 20 45 69 74 68 65 72 20 44 45 4c 45    /* Either DELE
25276 54 45 20 6f 72 20 55 50 44 41 54 45 2e 20 20 46  TE or UPDATE.  F
25277 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  or error message
25278 73 2e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20  s. */.){.  Expr 
25279 2a 70 57 68 65 72 65 52 6f 77 69 64 20 3d 20 4e  *pWhereRowid = N
2527a 55 4c 4c 3b 20 20 20 20 2f 2a 20 57 48 45 52 45  ULL;    /* WHERE
2527b 20 72 6f 77 69 64 20 2e 2e 20 2a 2f 0a 20 20 45   rowid .. */.  E
2527c 78 70 72 20 2a 70 49 6e 43 6c 61 75 73 65 20 3d  xpr *pInClause =
2527d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 2f 2a 20 57   NULL;      /* W
2527e 48 45 52 45 20 72 6f 77 69 64 20 49 4e 20 28 20  HERE rowid IN ( 
2527f 73 65 6c 65 63 74 20 29 20 2a 2f 0a 20 20 45 78  select ) */.  Ex
25280 70 72 20 2a 70 53 65 6c 65 63 74 52 6f 77 69 64  pr *pSelectRowid
25281 20 3d 20 4e 55 4c 4c 3b 20 20 20 2f 2a 20 53 45   = NULL;   /* SE
25282 4c 45 43 54 20 72 6f 77 69 64 20 2e 2e 2e 20 2a  LECT rowid ... *
25283 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
25284 4c 69 73 74 20 3d 20 4e 55 4c 4c 3b 20 20 20 20  List = NULL;    
25285 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6c   /* Expression l
25286 69 73 74 20 63 6f 6e 74 61 6e 69 6e 67 20 6f 6e  ist contaning on
25287 6c 79 20 70 53 65 6c 65 63 74 52 6f 77 69 64 20  ly pSelectRowid 
25288 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53  */.  SrcList *pS
25289 65 6c 65 63 74 53 72 63 20 3d 20 4e 55 4c 4c 3b  electSrc = NULL;
2528a 20 20 2f 2a 20 53 45 4c 45 43 54 20 72 6f 77 69    /* SELECT rowi
2528b 64 20 46 52 4f 4d 20 78 20 2e 2e 2e 20 28 64 75  d FROM x ... (du
2528c 70 20 6f 66 20 70 53 72 63 29 20 2a 2f 0a 20 20  p of pSrc) */.  
2528d 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20  Select *pSelect 
2528e 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 2f 2a 20  = NULL;      /* 
2528f 43 6f 6d 70 6c 65 74 65 20 53 45 4c 45 43 54 20  Complete SELECT 
25290 74 72 65 65 20 2a 2f 0a 0a 20 20 2f 2a 20 43 68  tree */..  /* Ch
25291 65 63 6b 20 74 68 61 74 20 74 68 65 72 65 20 69  eck that there i
25292 73 6e 27 74 20 61 6e 20 4f 52 44 45 52 20 42 59  sn't an ORDER BY
25293 20 77 69 74 68 6f 75 74 20 61 20 4c 49 4d 49 54   without a LIMIT
25294 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20   clause..  */.  
25295 69 66 28 20 70 4f 72 64 65 72 42 79 20 26 26 20  if( pOrderBy && 
25296 28 70 4c 69 6d 69 74 20 3d 3d 20 30 29 20 29 20  (pLimit == 0) ) 
25297 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
25298 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 4f  orMsg(pParse, "O
25299 52 44 45 52 20 42 59 20 77 69 74 68 6f 75 74 20  RDER BY without 
2529a 4c 49 4d 49 54 20 6f 6e 20 25 73 22 2c 20 7a 53  LIMIT on %s", zS
2529b 74 6d 74 54 79 70 65 29 3b 0a 20 20 20 20 70 50  tmtType);.    pP
2529c 61 72 73 65 2d 3e 70 61 72 73 65 45 72 72 6f 72  arse->parseError
2529d 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6c   = 1;.    goto l
2529e 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e  imit_where_clean
2529f 75 70 5f 32 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  up_2;.  }..  /* 
252a0 57 65 20 6f 6e 6c 79 20 6e 65 65 64 20 74 6f 20  We only need to 
252a1 67 65 6e 65 72 61 74 65 20 61 20 73 65 6c 65 63  generate a selec
252a2 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 66 20  t expression if 
252a3 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 61 20  there.  ** is a 
252a4 6c 69 6d 69 74 2f 6f 66 66 73 65 74 20 74 65 72  limit/offset ter
252a5 6d 20 74 6f 20 65 6e 66 6f 72 63 65 2e 0a 20 20  m to enforce..  
252a6 2a 2f 0a 20 20 69 66 28 20 70 4c 69 6d 69 74 20  */.  if( pLimit 
252a7 3d 3d 20 30 20 29 20 7b 0a 20 20 20 20 2f 2a 20  == 0 ) {.    /* 
252a8 69 66 20 70 4c 69 6d 69 74 20 69 73 20 6e 75 6c  if pLimit is nul
252a9 6c 2c 20 70 4f 66 66 73 65 74 20 77 69 6c 6c 20  l, pOffset will 
252aa 61 6c 77 61 79 73 20 62 65 20 6e 75 6c 6c 20 61  always be null a
252ab 73 20 77 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 61  s well. */.    a
252ac 73 73 65 72 74 28 20 70 4f 66 66 73 65 74 20 3d  ssert( pOffset =
252ad 3d 20 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72  = 0 );.    retur
252ae 6e 20 70 57 68 65 72 65 3b 0a 20 20 7d 0a 0a 20  n pWhere;.  }.. 
252af 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73   /* Generate a s
252b0 65 6c 65 63 74 20 65 78 70 72 65 73 73 69 6f 6e  elect expression
252b1 20 74 72 65 65 20 74 6f 20 65 6e 66 6f 72 63 65   tree to enforce
252b2 20 74 68 65 20 6c 69 6d 69 74 2f 6f 66 66 73 65   the limit/offse
252b3 74 20 0a 20 20 2a 2a 20 74 65 72 6d 20 66 6f 72  t .  ** term for
252b4 20 74 68 65 20 44 45 4c 45 54 45 20 6f 72 20 55   the DELETE or U
252b5 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e  PDATE statement.
252b6 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 20    For example:. 
252b7 20 2a 2a 20 20 20 44 45 4c 45 54 45 20 46 52 4f   **   DELETE FRO
252b8 4d 20 74 61 62 6c 65 5f 61 20 57 48 45 52 45 20  M table_a WHERE 
252b9 63 6f 6c 31 3d 31 20 4f 52 44 45 52 20 42 59 20  col1=1 ORDER BY 
252ba 63 6f 6c 32 20 4c 49 4d 49 54 20 31 20 4f 46 46  col2 LIMIT 1 OFF
252bb 53 45 54 20 31 0a 20 20 2a 2a 20 62 65 63 6f 6d  SET 1.  ** becom
252bc 65 73 3a 0a 20 20 2a 2a 20 20 20 44 45 4c 45 54  es:.  **   DELET
252bd 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f 61 20 57  E FROM table_a W
252be 48 45 52 45 20 72 6f 77 69 64 20 49 4e 20 28 20  HERE rowid IN ( 
252bf 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54  .  **     SELECT
252c0 20 72 6f 77 69 64 20 46 52 4f 4d 20 74 61 62 6c   rowid FROM tabl
252c1 65 5f 61 20 57 48 45 52 45 20 63 6f 6c 31 3d 31  e_a WHERE col1=1
252c2 20 4f 52 44 45 52 20 42 59 20 63 6f 6c 32 20 4c   ORDER BY col2 L
252c3 49 4d 49 54 20 31 20 4f 46 46 53 45 54 20 31 0a  IMIT 1 OFFSET 1.
252c4 20 20 2a 2a 20 20 20 29 3b 0a 20 20 2a 2f 0a 0a    **   );.  */..
252c5 20 20 70 53 65 6c 65 63 74 52 6f 77 69 64 20 3d    pSelectRowid =
252c6 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
252c7 61 72 73 65 2c 20 54 4b 5f 52 4f 57 2c 20 30 2c  arse, TK_ROW, 0,
252c8 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 53   0, 0);.  if( pS
252c9 65 6c 65 63 74 52 6f 77 69 64 20 3d 3d 20 30 20  electRowid == 0 
252ca 29 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77 68 65  ) goto limit_whe
252cb 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a 20 20  re_cleanup_2;.  
252cc 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33  pEList = sqlite3
252cd 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
252ce 50 61 72 73 65 2c 20 30 2c 20 70 53 65 6c 65 63  Parse, 0, pSelec
252cf 74 52 6f 77 69 64 29 3b 0a 20 20 69 66 28 20 70  tRowid);.  if( p
252d0 45 4c 69 73 74 20 3d 3d 20 30 20 29 20 67 6f 74  EList == 0 ) got
252d1 6f 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c  o limit_where_cl
252d2 65 61 6e 75 70 5f 32 3b 0a 0a 20 20 2f 2a 20 64  eanup_2;..  /* d
252d3 75 70 6c 69 63 61 74 65 20 74 68 65 20 46 52 4f  uplicate the FRO
252d4 4d 20 63 6c 61 75 73 65 20 61 73 20 69 74 20 69  M clause as it i
252d5 73 20 6e 65 65 64 65 64 20 62 79 20 62 6f 74 68  s needed by both
252d6 20 74 68 65 20 44 45 4c 45 54 45 2f 55 50 44 41   the DELETE/UPDA
252d7 54 45 20 74 72 65 65 0a 20 20 2a 2a 20 61 6e 64  TE tree.  ** and
252d8 20 74 68 65 20 53 45 4c 45 43 54 20 73 75 62 74   the SELECT subt
252d9 72 65 65 2e 20 2a 2f 0a 20 20 70 53 65 6c 65 63  ree. */.  pSelec
252da 74 53 72 63 20 3d 20 73 71 6c 69 74 65 33 53 72  tSrc = sqlite3Sr
252db 63 4c 69 73 74 44 75 70 28 70 50 61 72 73 65 2d  cListDup(pParse-
252dc 3e 64 62 2c 20 70 53 72 63 2c 20 30 29 3b 0a 20  >db, pSrc, 0);. 
252dd 20 69 66 28 20 70 53 65 6c 65 63 74 53 72 63 20   if( pSelectSrc 
252de 3d 3d 20 30 20 29 20 7b 0a 20 20 20 20 73 71 6c  == 0 ) {.    sql
252df 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
252e0 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  te(pParse->db, p
252e1 45 4c 69 73 74 29 3b 0a 20 20 20 20 67 6f 74 6f  EList);.    goto
252e2 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65   limit_where_cle
252e3 61 6e 75 70 5f 32 3b 0a 20 20 7d 0a 0a 20 20 2f  anup_2;.  }..  /
252e4 2a 20 67 65 6e 65 72 61 74 65 20 74 68 65 20 53  * generate the S
252e5 45 4c 45 43 54 20 65 78 70 72 65 73 73 69 6f 6e  ELECT expression
252e6 20 74 72 65 65 2e 20 2a 2f 0a 20 20 70 53 65 6c   tree. */.  pSel
252e7 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c  ect = sqlite3Sel
252e8 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c 70 45  ectNew(pParse,pE
252e9 4c 69 73 74 2c 70 53 65 6c 65 63 74 53 72 63 2c  List,pSelectSrc,
252ea 70 57 68 65 72 65 2c 30 2c 30 2c 0a 20 20 20 20  pWhere,0,0,.    
252eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
252ec 20 20 20 20 20 20 20 20 20 70 4f 72 64 65 72 42           pOrderB
252ed 79 2c 30 2c 70 4c 69 6d 69 74 2c 70 4f 66 66 73  y,0,pLimit,pOffs
252ee 65 74 29 3b 0a 20 20 69 66 28 20 70 53 65 6c 65  et);.  if( pSele
252ef 63 74 20 3d 3d 20 30 20 29 20 72 65 74 75 72 6e  ct == 0 ) return
252f0 20 30 3b 0a 0a 20 20 2f 2a 20 6e 6f 77 20 67 65   0;..  /* now ge
252f1 6e 65 72 61 74 65 20 74 68 65 20 6e 65 77 20 57  nerate the new W
252f2 48 45 52 45 20 72 6f 77 69 64 20 49 4e 20 63 6c  HERE rowid IN cl
252f3 61 75 73 65 20 66 6f 72 20 74 68 65 20 44 45 4c  ause for the DEL
252f4 45 54 45 2f 55 44 50 41 54 45 20 2a 2f 0a 20 20  ETE/UDPATE */.  
252f5 70 57 68 65 72 65 52 6f 77 69 64 20 3d 20 73 71  pWhereRowid = sq
252f6 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
252f7 65 2c 20 54 4b 5f 52 4f 57 2c 20 30 2c 20 30 2c  e, TK_ROW, 0, 0,
252f8 20 30 29 3b 0a 20 20 69 66 28 20 70 57 68 65 72   0);.  if( pWher
252f9 65 52 6f 77 69 64 20 3d 3d 20 30 20 29 20 67 6f  eRowid == 0 ) go
252fa 74 6f 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63  to limit_where_c
252fb 6c 65 61 6e 75 70 5f 31 3b 0a 20 20 70 49 6e 43  leanup_1;.  pInC
252fc 6c 61 75 73 65 20 3d 20 73 71 6c 69 74 65 33 50  lause = sqlite3P
252fd 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
252fe 49 4e 2c 20 70 57 68 65 72 65 52 6f 77 69 64 2c  IN, pWhereRowid,
252ff 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 49   0, 0);.  if( pI
25300 6e 43 6c 61 75 73 65 20 3d 3d 20 30 20 29 20 67  nClause == 0 ) g
25301 6f 74 6f 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f  oto limit_where_
25302 63 6c 65 61 6e 75 70 5f 31 3b 0a 0a 20 20 70 49  cleanup_1;..  pI
25303 6e 43 6c 61 75 73 65 2d 3e 78 2e 70 53 65 6c 65  nClause->x.pSele
25304 63 74 20 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20  ct = pSelect;.  
25305 70 49 6e 43 6c 61 75 73 65 2d 3e 66 6c 61 67 73  pInClause->flags
25306 20 7c 3d 20 45 50 5f 78 49 73 53 65 6c 65 63 74   |= EP_xIsSelect
25307 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53  ;.  sqlite3ExprS
25308 65 74 48 65 69 67 68 74 28 70 50 61 72 73 65 2c  etHeight(pParse,
25309 20 70 49 6e 43 6c 61 75 73 65 29 3b 0a 20 20 72   pInClause);.  r
2530a 65 74 75 72 6e 20 70 49 6e 43 6c 61 75 73 65 3b  eturn pInClause;
2530b 0a 0a 20 20 2f 2a 20 73 6f 6d 65 74 68 69 6e 67  ..  /* something
2530c 20 77 65 6e 74 20 77 72 6f 6e 67 2e 20 63 6c 65   went wrong. cle
2530d 61 6e 20 75 70 20 61 6e 79 74 68 69 6e 67 20 61  an up anything a
2530e 6c 6c 6f 63 61 74 65 64 2e 20 2a 2f 0a 6c 69 6d  llocated. */.lim
2530f 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70  it_where_cleanup
25310 5f 31 3a 0a 20 20 73 71 6c 69 74 65 33 53 65 6c  _1:.  sqlite3Sel
25311 65 63 74 44 65 6c 65 74 65 28 70 50 61 72 73 65  ectDelete(pParse
25312 2d 3e 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a  ->db, pSelect);.
25313 20 20 72 65 74 75 72 6e 20 30 3b 0a 0a 6c 69 6d    return 0;..lim
25314 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70  it_where_cleanup
25315 5f 32 3a 0a 20 20 73 71 6c 69 74 65 33 45 78 70  _2:.  sqlite3Exp
25316 72 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  rDelete(pParse->
25317 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 73  db, pWhere);.  s
25318 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
25319 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
2531a 20 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 73 71   pOrderBy);.  sq
2531b 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
2531c 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4c 69 6d  pParse->db, pLim
2531d 69 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  it);.  sqlite3Ex
2531e 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  prDelete(pParse-
2531f 3e 64 62 2c 20 70 4f 66 66 73 65 74 29 3b 0a 20  >db, pOffset);. 
25320 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e   return 0;.}.#en
25321 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53  dif /* defined(S
25322 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44  QLITE_ENABLE_UPD
25323 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54  ATE_DELETE_LIMIT
25324 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
25325 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
25326 52 59 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 65  RY) */../*.** Ge
25327 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
25328 61 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 73 74  a DELETE FROM st
25329 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20  atement..**.**  
2532a 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 74     DELETE FROM t
2532b 61 62 6c 65 5f 77 78 79 7a 20 57 48 45 52 45 20  able_wxyz WHERE 
2532c 61 3c 35 20 41 4e 44 20 62 20 4e 4f 54 20 4e 55  a<5 AND b NOT NU
2532d 4c 4c 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  LL;.**          
2532e 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f         \________
2532f 2f 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f  /       \_______
25330 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a 20 20 20  _________/.**   
25331 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
25332 54 61 62 4c 69 73 74 20 20 20 20 20 20 20 20 20  TabList         
25333 20 20 20 20 20 70 57 68 65 72 65 0a 2a 2f 0a 53       pWhere.*/.S
25334 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
25335 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  id sqlite3Delete
25336 46 72 6f 6d 28 0a 20 20 50 61 72 73 65 20 2a 70  From(.  Parse *p
25337 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f  Parse,         /
25338 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e  * The parser con
25339 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73  text */.  SrcLis
2533a 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 20 20  t *pTabList,    
2533b 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 66 72   /* The table fr
2533c 6f 6d 20 77 68 69 63 68 20 77 65 20 73 68 6f 75  om which we shou
2533d 6c 64 20 64 65 6c 65 74 65 20 74 68 69 6e 67 73  ld delete things
2533e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65   */.  Expr *pWhe
2533f 72 65 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  re           /* 
25340 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  The WHERE clause
25341 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a  .  May be null *
25342 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 20  /.){.  Vdbe *v; 
25343 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
25344 20 54 68 65 20 76 69 72 74 75 61 6c 20 64 61 74   The virtual dat
25345 61 62 61 73 65 20 65 6e 67 69 6e 65 20 2a 2f 0a  abase engine */.
25346 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20    Table *pTab;  
25347 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
25348 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68  table from which
25349 20 72 65 63 6f 72 64 73 20 77 69 6c 6c 20 62 65   records will be
2534a 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 63 6f   deleted */.  co
2534b 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20  nst char *zDb;  
2534c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
2534d 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67  database holding
2534e 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 65   pTab */.  int e
2534f 6e 64 2c 20 61 64 64 72 20 3d 20 30 3b 20 20 20  nd, addr = 0;   
25350 20 20 2f 2a 20 41 20 63 6f 75 70 6c 65 20 61 64    /* A couple ad
25351 64 72 65 73 73 65 73 20 6f 66 20 67 65 6e 65 72  dresses of gener
25352 61 74 65 64 20 63 6f 64 65 20 2a 2f 0a 20 20 69  ated code */.  i
25353 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
25354 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
25355 75 6e 74 65 72 20 2a 2f 0a 20 20 57 68 65 72 65  unter */.  Where
25356 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20 20  Info *pWInfo;   
25357 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e    /* Information
25358 20 61 62 6f 75 74 20 74 68 65 20 57 48 45 52 45   about the WHERE
25359 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 49 6e 64   clause */.  Ind
2535a 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20  ex *pIdx;       
2535b 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69      /* For loopi
2535c 6e 67 20 6f 76 65 72 20 69 6e 64 69 63 65 73 20  ng over indices 
2535d 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  of the table */.
2535e 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20    int iCur;     
2535f 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45           /* VDBE
25360 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66   Cursor number f
25361 6f 72 20 70 54 61 62 20 2a 2f 0a 20 20 73 71 6c  or pTab */.  sql
25362 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
25363 20 20 20 20 2f 2a 20 4d 61 69 6e 20 64 61 74 61      /* Main data
25364 62 61 73 65 20 73 74 72 75 63 74 75 72 65 20 2a  base structure *
25365 2f 0a 20 20 41 75 74 68 43 6f 6e 74 65 78 74 20  /.  AuthContext 
25366 73 43 6f 6e 74 65 78 74 3b 20 20 2f 2a 20 41 75  sContext;  /* Au
25367 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74  thorization cont
25368 65 78 74 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e  ext */.  NameCon
25369 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20 20  text sNC;       
2536a 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20  /* Name context 
2536b 74 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72 65  to resolve expre
2536c 73 73 69 6f 6e 73 20 69 6e 20 2a 2f 0a 20 20 69  ssions in */.  i
2536d 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
2536e 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
2536f 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e  e number */.  in
25370 74 20 6d 65 6d 43 6e 74 20 3d 20 2d 31 3b 20 20  t memCnt = -1;  
25371 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63       /* Memory c
25372 65 6c 6c 20 75 73 65 64 20 66 6f 72 20 63 68 61  ell used for cha
25373 6e 67 65 20 63 6f 75 6e 74 69 6e 67 20 2a 2f 0a  nge counting */.
25374 20 20 69 6e 74 20 72 63 61 75 74 68 3b 20 20 20    int rcauth;   
25375 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
25376 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 75  e returned by au
25377 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c  thorization call
25378 62 61 63 6b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  back */..#ifndef
25379 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
2537a 47 47 45 52 0a 20 20 69 6e 74 20 69 73 56 69 65  GGER.  int isVie
2537b 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  w;              
2537c 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
2537d 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 64 65 6c  ttempting to del
2537e 65 74 65 20 66 72 6f 6d 20 61 20 76 69 65 77 20  ete from a view 
2537f 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54  */.  Trigger *pT
25380 72 69 67 67 65 72 3b 20 20 20 20 20 20 20 20 20  rigger;         
25381 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61 62    /* List of tab
25382 6c 65 20 74 72 69 67 67 65 72 73 2c 20 69 66 20  le triggers, if 
25383 72 65 71 75 69 72 65 64 20 2a 2f 0a 23 65 6e 64  required */.#end
25384 69 66 0a 0a 20 20 6d 65 6d 73 65 74 28 26 73 43  if..  memset(&sC
25385 6f 6e 74 65 78 74 2c 20 30 2c 20 73 69 7a 65 6f  ontext, 0, sizeo
25386 66 28 73 43 6f 6e 74 65 78 74 29 29 3b 0a 20 20  f(sContext));.  
25387 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
25388 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
25389 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  Err || db->mallo
2538a 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67  cFailed ){.    g
2538b 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f  oto delete_from_
2538c 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61  cleanup;.  }.  a
2538d 73 73 65 72 74 28 20 70 54 61 62 4c 69 73 74 2d  ssert( pTabList-
2538e 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 0a 20 20 2f  >nSrc==1 );..  /
2538f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61 62  * Locate the tab
25390 6c 65 20 77 68 69 63 68 20 77 65 20 77 61 6e 74  le which we want
25391 20 74 6f 20 64 65 6c 65 74 65 2e 20 20 54 68 69   to delete.  Thi
25392 73 20 74 61 62 6c 65 20 68 61 73 20 74 6f 20 62  s table has to b
25393 65 0a 20 20 2a 2a 20 70 75 74 20 69 6e 20 61 6e  e.  ** put in an
25394 20 53 72 63 4c 69 73 74 20 73 74 72 75 63 74 75   SrcList structu
25395 72 65 20 62 65 63 61 75 73 65 20 73 6f 6d 65 20  re because some 
25396 6f 66 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e  of the subroutin
25397 65 73 20 77 65 0a 20 20 2a 2a 20 77 69 6c 6c 20  es we.  ** will 
25398 62 65 20 63 61 6c 6c 69 6e 67 20 61 72 65 20 64  be calling are d
25399 65 73 69 67 6e 65 64 20 74 6f 20 77 6f 72 6b 20  esigned to work 
2539a 77 69 74 68 20 6d 75 6c 74 69 70 6c 65 20 74 61  with multiple ta
2539b 62 6c 65 73 20 61 6e 64 20 65 78 70 65 63 74 0a  bles and expect.
2539c 20 20 2a 2a 20 61 6e 20 53 72 63 4c 69 73 74 2a    ** an SrcList*
2539d 20 70 61 72 61 6d 65 74 65 72 20 69 6e 73 74 65   parameter inste
2539e 61 64 20 6f 66 20 6a 75 73 74 20 61 20 54 61 62  ad of just a Tab
2539f 6c 65 2a 20 70 61 72 61 6d 65 74 65 72 2e 0a 20  le* parameter.. 
253a0 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 73 71 6c   */.  pTab = sql
253a1 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75  ite3SrcListLooku
253a2 70 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69  p(pParse, pTabLi
253a3 73 74 29 3b 0a 20 20 69 66 28 20 70 54 61 62 3d  st);.  if( pTab=
253a4 3d 30 20 29 20 20 67 6f 74 6f 20 64 65 6c 65 74  =0 )  goto delet
253a5 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a  e_from_cleanup;.
253a6 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74  .  /* Figure out
253a7 20 69 66 20 77 65 20 68 61 76 65 20 61 6e 79 20   if we have any 
253a8 74 72 69 67 67 65 72 73 20 61 6e 64 20 69 66 20  triggers and if 
253a9 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 0a  the table being.
253aa 20 20 2a 2a 20 64 65 6c 65 74 65 64 20 66 72 6f    ** deleted fro
253ab 6d 20 69 73 20 61 20 76 69 65 77 0a 20 20 2a 2f  m is a view.  */
253ac 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
253ad 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 70  OMIT_TRIGGER.  p
253ae 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65  Trigger = sqlite
253af 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28 70  3TriggersExist(p
253b0 50 61 72 73 65 2c 20 70 54 61 62 2c 20 54 4b 5f  Parse, pTab, TK_
253b1 44 45 4c 45 54 45 2c 20 30 2c 20 30 29 3b 0a 20  DELETE, 0, 0);. 
253b2 20 69 73 56 69 65 77 20 3d 20 70 54 61 62 2d 3e   isView = pTab->
253b3 70 53 65 6c 65 63 74 21 3d 30 3b 0a 23 65 6c 73  pSelect!=0;.#els
253b4 65 0a 23 20 64 65 66 69 6e 65 20 70 54 72 69 67  e.# define pTrig
253b5 67 65 72 20 30 0a 23 20 64 65 66 69 6e 65 20 69  ger 0.# define i
253b6 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 23  sView 0.#endif.#
253b7 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
253b8 54 5f 56 49 45 57 0a 23 20 75 6e 64 65 66 20 69  T_VIEW.# undef i
253b9 73 56 69 65 77 0a 23 20 64 65 66 69 6e 65 20 69  sView.# define i
253ba 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 0a  sView 0.#endif..
253bb 20 20 2f 2a 20 49 66 20 70 54 61 62 20 69 73 20    /* If pTab is 
253bc 72 65 61 6c 6c 79 20 61 20 76 69 65 77 2c 20 6d  really a view, m
253bd 61 6b 65 20 73 75 72 65 20 69 74 20 68 61 73 20  ake sure it has 
253be 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64  been initialized
253bf 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
253c0 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d  ite3ViewGetColum
253c1 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70  nNames(pParse, p
253c2 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  Tab) ){.    goto
253c3 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65   delete_from_cle
253c4 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  anup;.  }..  if(
253c5 20 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e   sqlite3IsReadOn
253c6 6c 79 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ly(pParse, pTab,
253c7 20 28 70 54 72 69 67 67 65 72 3f 31 3a 30 29 29   (pTrigger?1:0))
253c8 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c   ){.    goto del
253c9 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70  ete_from_cleanup
253ca 3b 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71  ;.  }.  iDb = sq
253cb 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
253cc 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  ex(db, pTab->pSc
253cd 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28  hema);.  assert(
253ce 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a   iDb<db->nDb );.
253cf 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b    zDb = db->aDb[
253d0 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 72 63  iDb].zName;.  rc
253d1 61 75 74 68 20 3d 20 73 71 6c 69 74 65 33 41 75  auth = sqlite3Au
253d2 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
253d3 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 70  SQLITE_DELETE, p
253d4 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 20 7a  Tab->zName, 0, z
253d5 44 62 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72  Db);.  assert( r
253d6 63 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f 4f 4b  cauth==SQLITE_OK
253d7 20 7c 7c 20 72 63 61 75 74 68 3d 3d 53 51 4c 49   || rcauth==SQLI
253d8 54 45 5f 44 45 4e 59 20 7c 7c 20 72 63 61 75 74  TE_DENY || rcaut
253d9 68 3d 3d 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  h==SQLITE_IGNORE
253da 20 29 3b 0a 20 20 69 66 28 20 72 63 61 75 74 68   );.  if( rcauth
253db 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b  ==SQLITE_DENY ){
253dc 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65  .    goto delete
253dd 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20  _from_cleanup;. 
253de 20 7d 0a 20 20 61 73 73 65 72 74 28 21 69 73 56   }.  assert(!isV
253df 69 65 77 20 7c 7c 20 70 54 72 69 67 67 65 72 29  iew || pTrigger)
253e0 3b 0a 0a 20 20 2f 2a 20 41 73 73 69 67 6e 20 20  ;..  /* Assign  
253e1 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 74 6f  cursor number to
253e2 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 61   the table and a
253e3 6c 6c 20 69 74 73 20 69 6e 64 69 63 65 73 2e 0a  ll its indices..
253e4 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
253e5 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31  TabList->nSrc==1
253e6 20 29 3b 0a 20 20 69 43 75 72 20 3d 20 70 54 61   );.  iCur = pTa
253e7 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69 43 75 72  bList->a[0].iCur
253e8 73 6f 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  sor = pParse->nT
253e9 61 62 2b 2b 3b 0a 20 20 66 6f 72 28 70 49 64 78  ab++;.  for(pIdx
253ea 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
253eb 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
253ec 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 50 61 72  pNext){.    pPar
253ed 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a  se->nTab++;.  }.
253ee 0a 20 20 2f 2a 20 53 74 61 72 74 20 74 68 65 20  .  /* Start the 
253ef 76 69 65 77 20 63 6f 6e 74 65 78 74 0a 20 20 2a  view context.  *
253f0 2f 0a 20 20 69 66 28 20 69 73 56 69 65 77 20 29  /.  if( isView )
253f1 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75 74  {.    sqlite3Aut
253f2 68 43 6f 6e 74 65 78 74 50 75 73 68 28 70 50 61  hContextPush(pPa
253f3 72 73 65 2c 20 26 73 43 6f 6e 74 65 78 74 2c 20  rse, &sContext, 
253f4 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pTab->zName);.  
253f5 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67 65  }..  /* Begin ge
253f6 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 2e 0a 20  nerating code.. 
253f7 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65   */.  v = sqlite
253f8 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
253f9 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a  ;.  if( v==0 ){.
253fa 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f      goto delete_
253fb 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  from_cleanup;.  
253fc 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  }.  if( pParse->
253fd 6e 65 73 74 65 64 3d 3d 30 20 29 20 73 71 6c 69  nested==0 ) sqli
253fe 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e  te3VdbeCountChan
253ff 67 65 73 28 76 29 3b 0a 20 20 73 71 6c 69 74 65  ges(v);.  sqlite
25400 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
25401 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 28 70 54  tion(pParse, (pT
25402 72 69 67 67 65 72 3f 31 3a 30 29 2c 20 69 44 62  rigger?1:0), iDb
25403 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 61  );..  /* If we a
25404 72 65 20 74 72 79 69 6e 67 20 74 6f 20 64 65 6c  re trying to del
25405 65 74 65 20 66 72 6f 6d 20 61 20 76 69 65 77 2c  ete from a view,
25406 20 72 65 61 6c 69 7a 65 20 74 68 61 74 20 76 69   realize that vi
25407 65 77 20 69 6e 74 6f 0a 20 20 2a 2a 20 61 20 65  ew into.  ** a e
25408 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e 0a  phemeral table..
25409 20 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65    */.#if !define
2540a 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  d(SQLITE_OMIT_VI
2540b 45 57 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  EW) && !defined(
2540c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
2540d 47 45 52 29 0a 20 20 69 66 28 20 69 73 56 69 65  GER).  if( isVie
2540e 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  w ){.    sqlite3
2540f 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65 77 28  MaterializeView(
25410 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70 57  pParse, pTab, pW
25411 68 65 72 65 2c 20 69 43 75 72 29 3b 0a 20 20 7d  here, iCur);.  }
25412 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65  .#endif..  /* Re
25413 73 6f 6c 76 65 20 74 68 65 20 63 6f 6c 75 6d 6e  solve the column
25414 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 57 48   names in the WH
25415 45 52 45 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f  ERE clause..  */
25416 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20  .  memset(&sNC, 
25417 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b  0, sizeof(sNC));
25418 0a 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20  .  sNC.pParse = 
25419 70 50 61 72 73 65 3b 0a 20 20 73 4e 43 2e 70 53  pParse;.  sNC.pS
2541a 72 63 4c 69 73 74 20 3d 20 70 54 61 62 4c 69 73  rcList = pTabLis
2541b 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  t;.  if( sqlite3
2541c 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
2541d 28 26 73 4e 43 2c 20 70 57 68 65 72 65 29 20 29  (&sNC, pWhere) )
2541e 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74  {.    goto delet
2541f 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a  e_from_cleanup;.
25420 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61    }..  /* Initia
25421 6c 69 7a 65 20 74 68 65 20 63 6f 75 6e 74 65 72  lize the counter
25422 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   of the number o
25423 66 20 72 6f 77 73 20 64 65 6c 65 74 65 64 2c 20  f rows deleted, 
25424 69 66 0a 20 20 2a 2a 20 77 65 20 61 72 65 20 63  if.  ** we are c
25425 6f 75 6e 74 69 6e 67 20 72 6f 77 73 2e 0a 20 20  ounting rows..  
25426 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61  */.  if( db->fla
25427 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e  gs & SQLITE_Coun
25428 74 52 6f 77 73 20 29 7b 0a 20 20 20 20 6d 65 6d  tRows ){.    mem
25429 43 6e 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  Cnt = ++pParse->
2542a 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65  nMem;.    sqlite
2542b 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2542c 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 6d 65  P_Integer, 0, me
2542d 6d 43 6e 74 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e  mCnt);.  }..#ifn
2542e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2542f 54 52 55 4e 43 41 54 45 5f 4f 50 54 49 4d 49 5a  TRUNCATE_OPTIMIZ
25430 41 54 49 4f 4e 0a 20 20 2f 2a 20 53 70 65 63 69  ATION.  /* Speci
25431 61 6c 20 63 61 73 65 3a 20 41 20 44 45 4c 45 54  al case: A DELET
25432 45 20 77 69 74 68 6f 75 74 20 61 20 57 48 45 52  E without a WHER
25433 45 20 63 6c 61 75 73 65 20 64 65 6c 65 74 65 73  E clause deletes
25434 20 65 76 65 72 79 74 68 69 6e 67 2e 0a 20 20 2a   everything..  *
25435 2a 20 49 74 20 69 73 20 65 61 73 69 65 72 20 6a  * It is easier j
25436 75 73 74 20 74 6f 20 65 72 61 73 65 20 74 68 65  ust to erase the
25437 20 77 68 6f 6c 65 20 74 61 62 6c 65 2e 20 50 72   whole table. Pr
25438 69 6f 72 20 74 6f 20 76 65 72 73 69 6f 6e 20 33  ior to version 3
25439 2e 36 2e 35 2c 0a 20 20 2a 2a 20 74 68 69 73 20  .6.5,.  ** this 
2543a 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 63 61 75  optimization cau
2543b 73 65 64 20 74 68 65 20 72 6f 77 20 63 68 61 6e  sed the row chan
2543c 67 65 20 63 6f 75 6e 74 20 28 74 68 65 20 76 61  ge count (the va
2543d 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20  lue returned by 
2543e 0a 20 20 2a 2a 20 41 50 49 20 66 75 6e 63 74 69  .  ** API functi
2543f 6f 6e 20 73 71 6c 69 74 65 33 5f 63 6f 75 6e 74  on sqlite3_count
25440 5f 63 68 61 6e 67 65 73 29 20 74 6f 20 62 65 20  _changes) to be 
25441 73 65 74 20 69 6e 63 6f 72 72 65 63 74 6c 79 2e  set incorrectly.
25442 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 61 75 74    */.  if( rcaut
25443 68 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  h==SQLITE_OK && 
25444 70 57 68 65 72 65 3d 3d 30 20 26 26 20 21 70 54  pWhere==0 && !pT
25445 72 69 67 67 65 72 20 26 26 20 21 49 73 56 69 72  rigger && !IsVir
25446 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
25447 20 20 61 73 73 65 72 74 28 20 21 69 73 56 69 65    assert( !isVie
25448 77 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  w );.    sqlite3
25449 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2544a 5f 43 6c 65 61 72 2c 20 70 54 61 62 2d 3e 74 6e  _Clear, pTab->tn
2544b 75 6d 2c 20 69 44 62 2c 20 6d 65 6d 43 6e 74 2c  um, iDb, memCnt,
2544c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2544d 20 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61         pTab->zNa
2544e 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a  me, P4_STATIC);.
2544f 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61      for(pIdx=pTa
25450 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
25451 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
25452 74 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  t){.      assert
25453 28 20 70 49 64 78 2d 3e 70 53 63 68 65 6d 61 3d  ( pIdx->pSchema=
25454 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29  =pTab->pSchema )
25455 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
25456 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
25457 43 6c 65 61 72 2c 20 70 49 64 78 2d 3e 74 6e 75  Clear, pIdx->tnu
25458 6d 2c 20 69 44 62 29 3b 0a 20 20 20 20 7d 0a 20  m, iDb);.    }. 
25459 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a   }else.#endif /*
2545a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 55   SQLITE_OMIT_TRU
2545b 4e 43 41 54 45 5f 4f 50 54 49 4d 49 5a 41 54 49  NCATE_OPTIMIZATI
2545c 4f 4e 20 2a 2f 0a 20 20 2f 2a 20 54 68 65 20 75  ON */.  /* The u
2545d 73 75 61 6c 20 63 61 73 65 3a 20 54 68 65 72 65  sual case: There
2545e 20 69 73 20 61 20 57 48 45 52 45 20 63 6c 61 75   is a WHERE clau
2545f 73 65 20 73 6f 20 77 65 20 68 61 76 65 20 74 6f  se so we have to
25460 20 73 63 61 6e 20 74 68 72 6f 75 67 68 0a 20 20   scan through.  
25461 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64  ** the table and
25462 20 70 69 63 6b 20 77 68 69 63 68 20 72 65 63 6f   pick which reco
25463 72 64 73 20 74 6f 20 64 65 6c 65 74 65 2e 0a 20  rds to delete.. 
25464 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20   */.  {.    int 
25465 69 52 6f 77 53 65 74 20 3d 20 2b 2b 70 50 61 72  iRowSet = ++pPar
25466 73 65 2d 3e 6e 4d 65 6d 3b 20 20 20 2f 2a 20 52  se->nMem;   /* R
25467 65 67 69 73 74 65 72 20 66 6f 72 20 72 6f 77 73  egister for rows
25468 65 74 20 6f 66 20 72 6f 77 73 20 74 6f 20 64 65  et of rows to de
25469 6c 65 74 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20  lete */.    int 
2546a 69 52 6f 77 69 64 20 3d 20 2b 2b 70 50 61 72 73  iRowid = ++pPars
2546b 65 2d 3e 6e 4d 65 6d 3b 20 20 20 20 2f 2a 20 55  e->nMem;    /* U
2546c 73 65 64 20 66 6f 72 20 73 74 6f 72 69 6e 67 20  sed for storing 
2546d 72 6f 77 69 64 20 76 61 6c 75 65 73 2e 20 2a 2f  rowid values. */
2546e 0a 20 20 20 20 69 6e 74 20 72 65 67 52 6f 77 69  .    int regRowi
2546f 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
25470 20 20 20 20 20 2f 2a 20 41 63 74 75 61 6c 20 72       /* Actual r
25471 65 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e 69  egister containi
25472 6e 67 20 72 6f 77 69 64 73 20 2a 2f 0a 0a 20 20  ng rowids */..  
25473 20 20 2f 2a 20 43 6f 6c 6c 65 63 74 20 72 6f 77    /* Collect row
25474 69 64 73 20 6f 66 20 65 76 65 72 79 20 72 6f 77  ids of every row
25475 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e 0a   to be deleted..
25476 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
25477 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
25478 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 52 6f 77  OP_Null, 0, iRow
25479 53 65 74 29 3b 0a 20 20 20 20 70 57 49 6e 66 6f  Set);.    pWInfo
2547a 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42   = sqlite3WhereB
2547b 65 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61  egin(pParse, pTa
2547c 62 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 30 2c  bList, pWhere,0,
2547d 57 48 45 52 45 5f 44 55 50 4c 49 43 41 54 45 53  WHERE_DUPLICATES
2547e 5f 4f 4b 29 3b 0a 20 20 20 20 69 66 28 20 70 57  _OK);.    if( pW
2547f 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 64  Info==0 ) goto d
25480 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e  elete_from_clean
25481 75 70 3b 0a 20 20 20 20 72 65 67 52 6f 77 69 64  up;.    regRowid
25482 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
25483 64 65 47 65 74 43 6f 6c 75 6d 6e 28 70 50 61 72  deGetColumn(pPar
25484 73 65 2c 20 70 54 61 62 2c 20 2d 31 2c 20 69 43  se, pTab, -1, iC
25485 75 72 2c 20 69 52 6f 77 69 64 2c 20 30 29 3b 0a  ur, iRowid, 0);.
25486 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
25487 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 53  ddOp2(v, OP_RowS
25488 65 74 41 64 64 2c 20 69 52 6f 77 53 65 74 2c 20  etAdd, iRowSet, 
25489 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 69  regRowid);.    i
2548a 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  f( db->flags & S
2548b 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20  QLITE_CountRows 
2548c 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2548d 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2548e 5f 41 64 64 49 6d 6d 2c 20 6d 65 6d 43 6e 74 2c  _AddImm, memCnt,
2548f 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73   1);.    }.    s
25490 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 70  qlite3WhereEnd(p
25491 57 49 6e 66 6f 29 3b 0a 0a 20 20 20 20 2f 2a 20  WInfo);..    /* 
25492 44 65 6c 65 74 65 20 65 76 65 72 79 20 69 74 65  Delete every ite
25493 6d 20 77 68 6f 73 65 20 6b 65 79 20 77 61 73 20  m whose key was 
25494 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6c  written to the l
25495 69 73 74 20 64 75 72 69 6e 67 20 74 68 65 0a 20  ist during the. 
25496 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 73     ** database s
25497 63 61 6e 2e 20 20 57 65 20 68 61 76 65 20 74 6f  can.  We have to
25498 20 64 65 6c 65 74 65 20 69 74 65 6d 73 20 61 66   delete items af
25499 74 65 72 20 74 68 65 20 73 63 61 6e 20 69 73 20  ter the scan is 
2549a 63 6f 6d 70 6c 65 74 65 0a 20 20 20 20 2a 2a 20  complete.    ** 
2549b 62 65 63 61 75 73 65 20 64 65 6c 65 74 69 6e 67  because deleting
2549c 20 61 6e 20 69 74 65 6d 20 63 61 6e 20 63 68 61   an item can cha
2549d 6e 67 65 20 74 68 65 20 73 63 61 6e 20 6f 72 64  nge the scan ord
2549e 65 72 2e 20 20 2a 2f 0a 20 20 20 20 65 6e 64 20  er.  */.    end 
2549f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
254a0 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 20 20 20  eLabel(v);..    
254a1 2f 2a 20 55 6e 6c 65 73 73 20 74 68 69 73 20 69  /* Unless this i
254a2 73 20 61 20 76 69 65 77 2c 20 6f 70 65 6e 20 63  s a view, open c
254a3 75 72 73 6f 72 73 20 66 6f 72 20 74 68 65 20 74  ursors for the t
254a4 61 62 6c 65 20 77 65 20 61 72 65 20 0a 20 20 20  able we are .   
254a5 20 2a 2a 20 64 65 6c 65 74 69 6e 67 20 66 72 6f   ** deleting fro
254a6 6d 20 61 6e 64 20 61 6c 6c 20 69 74 73 20 69 6e  m and all its in
254a7 64 69 63 65 73 2e 20 49 66 20 74 68 69 73 20 69  dices. If this i
254a8 73 20 61 20 76 69 65 77 2c 20 74 68 65 6e 20 74  s a view, then t
254a9 68 65 0a 20 20 20 20 2a 2a 20 6f 6e 6c 79 20 65  he.    ** only e
254aa 66 66 65 63 74 20 74 68 69 73 20 73 74 61 74 65  ffect this state
254ab 6d 65 6e 74 20 68 61 73 20 69 73 20 74 6f 20 66  ment has is to f
254ac 69 72 65 20 74 68 65 20 49 4e 53 54 45 41 44 20  ire the INSTEAD 
254ad 4f 46 20 0a 20 20 20 20 2a 2a 20 74 72 69 67 67  OF .    ** trigg
254ae 65 72 73 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28  ers.  */.    if(
254af 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20   !isView ){.    
254b0 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62    sqlite3OpenTab
254b1 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 70 50 61  leAndIndices(pPa
254b2 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c  rse, pTab, iCur,
254b3 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b 0a   OP_OpenWrite);.
254b4 20 20 20 20 7d 0a 0a 20 20 20 20 61 64 64 72 20      }..    addr 
254b5 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
254b6 4f 70 33 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74  Op3(v, OP_RowSet
254b7 52 65 61 64 2c 20 69 52 6f 77 53 65 74 2c 20 65  Read, iRowSet, e
254b8 6e 64 2c 20 69 52 6f 77 69 64 29 3b 0a 0a 20 20  nd, iRowid);..  
254b9 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20    /* Delete the 
254ba 72 6f 77 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  row */.#ifndef S
254bb 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
254bc 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20  ALTABLE.    if( 
254bd 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
254be 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63  ){.      const c
254bf 68 61 72 20 2a 70 56 54 61 62 20 3d 20 28 63 6f  har *pVTab = (co
254c0 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74  nst char *)sqlit
254c1 65 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20  e3GetVTable(db, 
254c2 70 54 61 62 29 3b 0a 20 20 20 20 20 20 73 71 6c  pTab);.      sql
254c3 69 74 65 33 56 74 61 62 4d 61 6b 65 57 72 69 74  ite3VtabMakeWrit
254c4 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61  able(pParse, pTa
254c5 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
254c6 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
254c7 50 5f 56 55 70 64 61 74 65 2c 20 30 2c 20 31 2c  P_VUpdate, 0, 1,
254c8 20 69 52 6f 77 69 64 2c 20 70 56 54 61 62 2c 20   iRowid, pVTab, 
254c9 50 34 5f 56 54 41 42 29 3b 0a 20 20 20 20 20 20  P4_VTAB);.      
254ca 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28  sqlite3MayAbort(
254cb 70 50 61 72 73 65 29 3b 0a 20 20 20 20 7d 65 6c  pParse);.    }el
254cc 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a  se.#endif.    {.
254cd 20 20 20 20 20 20 69 6e 74 20 63 6f 75 6e 74 20        int count 
254ce 3d 20 28 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  = (pParse->neste
254cf 64 3d 3d 30 29 3b 20 20 20 20 2f 2a 20 54 72 75  d==0);    /* Tru
254d0 65 20 74 6f 20 63 6f 75 6e 74 20 63 68 61 6e 67  e to count chang
254d1 65 73 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  es */.      sqli
254d2 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65  te3GenerateRowDe
254d3 6c 65 74 65 28 70 50 61 72 73 65 2c 20 70 54 61  lete(pParse, pTa
254d4 62 2c 20 69 43 75 72 2c 20 69 52 6f 77 69 64 2c  b, iCur, iRowid,
254d5 20 63 6f 75 6e 74 2c 20 70 54 72 69 67 67 65 72   count, pTrigger
254d6 2c 20 4f 45 5f 44 65 66 61 75 6c 74 29 3b 0a 20  , OE_Default);. 
254d7 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 45 6e 64     }..    /* End
254d8 20 6f 66 20 74 68 65 20 64 65 6c 65 74 65 20 6c   of the delete l
254d9 6f 6f 70 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  oop */.    sqlit
254da 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
254db 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72  OP_Goto, 0, addr
254dc 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
254dd 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
254de 2c 20 65 6e 64 29 3b 0a 0a 20 20 20 20 2f 2a 20  , end);..    /* 
254df 43 6c 6f 73 65 20 74 68 65 20 63 75 72 73 6f 72  Close the cursor
254e0 73 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 74 61  s open on the ta
254e1 62 6c 65 20 61 6e 64 20 69 74 73 20 69 6e 64 65  ble and its inde
254e2 78 65 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  xes. */.    if( 
254e3 21 69 73 56 69 65 77 20 26 26 20 21 49 73 56 69  !isView && !IsVi
254e4 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20  rtual(pTab) ){. 
254e5 20 20 20 20 20 66 6f 72 28 69 3d 31 2c 20 70 49       for(i=1, pI
254e6 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
254e7 20 70 49 64 78 3b 20 69 2b 2b 2c 20 70 49 64 78   pIdx; i++, pIdx
254e8 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20  =pIdx->pNext){. 
254e9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
254ea 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
254eb 6c 6f 73 65 2c 20 69 43 75 72 20 2b 20 69 2c 20  lose, iCur + i, 
254ec 70 49 64 78 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20  pIdx->tnum);.   
254ed 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
254ee 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
254ef 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 29 3b  OP_Close, iCur);
254f0 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
254f1 20 55 70 64 61 74 65 20 74 68 65 20 73 71 6c 69   Update the sqli
254f2 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c  te_sequence tabl
254f3 65 20 62 79 20 73 74 6f 72 69 6e 67 20 74 68 65  e by storing the
254f4 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 0a   content of the.
254f5 20 20 2a 2a 20 6d 61 78 69 6d 75 6d 20 72 6f 77    ** maximum row
254f6 69 64 20 63 6f 75 6e 74 65 72 20 76 61 6c 75 65  id counter value
254f7 73 20 72 65 63 6f 72 64 65 64 20 77 68 69 6c 65  s recorded while
254f8 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 74 6f 0a   inserting into.
254f9 20 20 2a 2a 20 61 75 74 6f 69 6e 63 72 65 6d 65    ** autoincreme
254fa 6e 74 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a  nt tables..  */.
254fb 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65    if( pParse->ne
254fc 73 74 65 64 3d 3d 30 20 26 26 20 70 50 61 72 73  sted==0 && pPars
254fd 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 3d 3d  e->pTriggerTab==
254fe 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
254ff 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64  AutoincrementEnd
25500 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 0a 20  (pParse);.  }.. 
25501 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e   /* Return the n
25502 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68  umber of rows th
25503 61 74 20 77 65 72 65 20 64 65 6c 65 74 65 64 2e  at were deleted.
25504 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   If this routine
25505 20 69 73 20 0a 20 20 2a 2a 20 67 65 6e 65 72 61   is .  ** genera
25506 74 69 6e 67 20 63 6f 64 65 20 62 65 63 61 75 73  ting code becaus
25507 65 20 6f 66 20 61 20 63 61 6c 6c 20 74 6f 20 73  e of a call to s
25508 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
25509 65 28 29 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a  e(), do not.  **
2550a 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c   invoke the call
2550b 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 20  back function.. 
2550c 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e 66   */.  if( (db->f
2550d 6c 61 67 73 26 53 51 4c 49 54 45 5f 43 6f 75 6e  lags&SQLITE_Coun
2550e 74 52 6f 77 73 29 20 26 26 20 21 70 50 61 72 73  tRows) && !pPars
2550f 65 2d 3e 6e 65 73 74 65 64 20 26 26 20 21 70 50  e->nested && !pP
25510 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61  arse->pTriggerTa
25511 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  b ){.    sqlite3
25512 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
25513 5f 52 65 73 75 6c 74 52 6f 77 2c 20 6d 65 6d 43  _ResultRow, memC
25514 6e 74 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  nt, 1);.    sqli
25515 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
25516 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c  s(v, 1);.    sql
25517 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
25518 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45  me(v, 0, COLNAME
25519 5f 4e 41 4d 45 2c 20 22 72 6f 77 73 20 64 65 6c  _NAME, "rows del
2551a 65 74 65 64 22 2c 20 53 51 4c 49 54 45 5f 53 54  eted", SQLITE_ST
2551b 41 54 49 43 29 3b 0a 20 20 7d 0a 0a 64 65 6c 65  ATIC);.  }..dele
2551c 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3a  te_from_cleanup:
2551d 0a 20 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f  .  sqlite3AuthCo
2551e 6e 74 65 78 74 50 6f 70 28 26 73 43 6f 6e 74 65  ntextPop(&sConte
2551f 78 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 72  xt);.  sqlite3Sr
25520 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  cListDelete(db, 
25521 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 73 71 6c  pTabList);.  sql
25522 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
25523 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 72 65  b, pWhere);.  re
25524 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  turn;.}../*.** T
25525 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65  his routine gene
25526 72 61 74 65 73 20 56 44 42 45 20 63 6f 64 65 20  rates VDBE code 
25527 74 68 61 74 20 63 61 75 73 65 73 20 61 20 73 69  that causes a si
25528 6e 67 6c 65 20 72 6f 77 20 6f 66 20 61 0a 2a 2a  ngle row of a.**
25529 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 74 6f   single table to
2552a 20 62 65 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a   be deleted..**.
2552b 2a 2a 20 54 68 65 20 56 44 42 45 20 6d 75 73 74  ** The VDBE must
2552c 20 62 65 20 69 6e 20 61 20 70 61 72 74 69 63 75   be in a particu
2552d 6c 61 72 20 73 74 61 74 65 20 77 68 65 6e 20 74  lar state when t
2552e 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
2552f 61 6c 6c 65 64 2e 0a 2a 2a 20 54 68 65 73 65 20  alled..** These 
25530 61 72 65 20 74 68 65 20 72 65 71 75 69 72 65 6d  are the requirem
25531 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e  ents:.**.**   1.
25532 20 20 41 20 72 65 61 64 2f 77 72 69 74 65 20 63    A read/write c
25533 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74  ursor pointing t
25534 6f 20 70 54 61 62 2c 20 74 68 65 20 74 61 62 6c  o pTab, the tabl
25535 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
25536 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 74 6f   row.**       to
25537 20 62 65 20 64 65 6c 65 74 65 64 2c 20 6d 75 73   be deleted, mus
25538 74 20 62 65 20 6f 70 65 6e 65 64 20 61 73 20 63  t be opened as c
25539 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 22 62 61  ursor number "ba
2553a 73 65 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20  se"..**.**   2. 
2553b 20 52 65 61 64 2f 77 72 69 74 65 20 63 75 72 73   Read/write curs
2553c 6f 72 73 20 66 6f 72 20 61 6c 6c 20 69 6e 64 69  ors for all indi
2553d 63 65 73 20 6f 66 20 70 54 61 62 20 6d 75 73 74  ces of pTab must
2553e 20 62 65 20 6f 70 65 6e 20 61 73 0a 2a 2a 20 20   be open as.**  
2553f 20 20 20 20 20 63 75 72 73 6f 72 20 6e 75 6d 62       cursor numb
25540 65 72 20 62 61 73 65 2b 69 20 66 6f 72 20 74 68  er base+i for th
25541 65 20 69 2d 74 68 20 69 6e 64 65 78 2e 0a 2a 2a  e i-th index..**
25542 0a 2a 2a 20 20 20 33 2e 20 20 54 68 65 20 72 65  .**   3.  The re
25543 63 6f 72 64 20 6e 75 6d 62 65 72 20 6f 66 20 74  cord number of t
25544 68 65 20 72 6f 77 20 74 6f 20 62 65 20 64 65 6c  he row to be del
25545 65 74 65 64 20 6d 75 73 74 20 62 65 20 73 74 6f  eted must be sto
25546 72 65 64 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20  red in.**       
25547 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 52 6f 77  memory cell iRow
25548 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  id..**.** This r
25549 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73  outine generates
2554a 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65 20   code to remove 
2554b 62 6f 74 68 20 74 68 65 20 74 61 62 6c 65 20 72  both the table r
2554c 65 63 6f 72 64 20 61 6e 64 20 61 6c 6c 20 0a 2a  ecord and all .*
2554d 2a 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20  * index entries 
2554e 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 74 68  that point to th
2554f 61 74 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 53 51  at record..*/.SQ
25550 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
25551 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74  d sqlite3Generat
25552 65 52 6f 77 44 65 6c 65 74 65 28 0a 20 20 50 61  eRowDelete(.  Pa
25553 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
25554 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
25555 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ext */.  Table *
25556 70 54 61 62 2c 20 20 20 20 20 20 20 2f 2a 20 54  pTab,       /* T
25557 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  able containing 
25558 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20 64 65  the row to be de
25559 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  leted */.  int i
2555a 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Cur,          /*
2555b 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66   Cursor number f
2555c 6f 72 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  or the table */.
2555d 20 20 69 6e 74 20 69 52 6f 77 69 64 2c 20 20 20    int iRowid,   
2555e 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63       /* Memory c
2555f 65 6c 6c 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ell that contain
25560 73 20 74 68 65 20 72 6f 77 69 64 20 74 6f 20 64  s the rowid to d
25561 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 63  elete */.  int c
25562 6f 75 6e 74 2c 20 20 20 20 20 20 20 20 20 2f 2a  ount,         /*
25563 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 69 6e   If non-zero, in
25564 63 72 65 6d 65 6e 74 20 74 68 65 20 72 6f 77 20  crement the row 
25565 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 2a  change counter *
25566 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72  /.  Trigger *pTr
25567 69 67 67 65 72 2c 20 2f 2a 20 4c 69 73 74 20 6f  igger, /* List o
25568 66 20 74 72 69 67 67 65 72 73 20 74 6f 20 28 70  f triggers to (p
25569 6f 74 65 6e 74 69 61 6c 6c 79 29 20 66 69 72 65  otentially) fire
2556a 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 63 6f 6e 66   */.  int onconf
2556b 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61           /* Defa
2556c 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20  ult ON CONFLICT 
2556d 70 6f 6c 69 63 79 20 66 6f 72 20 74 72 69 67 67  policy for trigg
2556e 65 72 73 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65  ers */.){.  Vdbe
2556f 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
25570 64 62 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 56  dbe;        /* V
25571 64 62 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 6c  dbe */.  int iOl
25572 64 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  d = 0;          
25573 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
25574 74 20 72 65 67 69 73 74 65 72 20 69 6e 20 4f 4c  t register in OL
25575 44 2e 2a 20 61 72 72 61 79 20 2a 2f 0a 20 20 69  D.* array */.  i
25576 6e 74 20 69 4c 61 62 65 6c 3b 20 20 20 20 20 20  nt iLabel;      
25577 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
25578 2a 20 4c 61 62 65 6c 20 72 65 73 6f 6c 76 65 64  * Label resolved
25579 20 74 6f 20 65 6e 64 20 6f 66 20 67 65 6e 65 72   to end of gener
2557a 61 74 65 64 20 63 6f 64 65 20 2a 2f 0a 0a 20 20  ated code */..  
2557b 2f 2a 20 56 64 62 65 20 69 73 20 67 75 61 72 61  /* Vdbe is guara
2557c 6e 74 65 65 64 20 74 6f 20 68 61 76 65 20 62 65  nteed to have be
2557d 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  en allocated by 
2557e 74 68 69 73 20 73 74 61 67 65 2e 20 2a 2f 0a 20  this stage. */. 
2557f 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a 0a 20   assert( v );.. 
25580 20 2f 2a 20 53 65 65 6b 20 63 75 72 73 6f 72 20   /* Seek cursor 
25581 69 43 75 72 20 74 6f 20 74 68 65 20 72 6f 77 20  iCur to the row 
25582 74 6f 20 64 65 6c 65 74 65 2e 20 49 66 20 74 68  to delete. If th
25583 69 73 20 72 6f 77 20 6e 6f 20 6c 6f 6e 67 65 72  is row no longer
25584 20 65 78 69 73 74 73 20 0a 20 20 2a 2a 20 28 74   exists .  ** (t
25585 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 20 69  his can happen i
25586 66 20 61 20 74 72 69 67 67 65 72 20 70 72 6f 67  f a trigger prog
25587 72 61 6d 20 68 61 73 20 61 6c 72 65 61 64 79 20  ram has already 
25588 64 65 6c 65 74 65 64 20 69 74 29 2c 20 64 6f 0a  deleted it), do.
25589 20 20 2a 2a 20 6e 6f 74 20 61 74 74 65 6d 70 74    ** not attempt
2558a 20 74 6f 20 64 65 6c 65 74 65 20 69 74 20 6f 72   to delete it or
2558b 20 66 69 72 65 20 61 6e 79 20 44 45 4c 45 54 45   fire any DELETE
2558c 20 74 72 69 67 67 65 72 73 2e 20 20 2a 2f 0a 20   triggers.  */. 
2558d 20 69 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74 65   iLabel = sqlite
2558e 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
2558f 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
25590 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74  AddOp3(v, OP_Not
25591 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20 69 4c  Exists, iCur, iL
25592 61 62 65 6c 2c 20 69 52 6f 77 69 64 29 3b 0a 20  abel, iRowid);. 
25593 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61  .  /* If there a
25594 72 65 20 61 6e 79 20 74 72 69 67 67 65 72 73 20  re any triggers 
25595 74 6f 20 66 69 72 65 2c 20 61 6c 6c 6f 63 61 74  to fire, allocat
25596 65 20 61 20 72 61 6e 67 65 20 6f 66 20 72 65 67  e a range of reg
25597 69 73 74 65 72 73 20 74 6f 0a 20 20 2a 2a 20 75  isters to.  ** u
25598 73 65 20 66 6f 72 20 74 68 65 20 6f 6c 64 2e 2a  se for the old.*
25599 20 72 65 66 65 72 65 6e 63 65 73 20 69 6e 20 74   references in t
2559a 68 65 20 74 72 69 67 67 65 72 73 2e 20 20 2a 2f  he triggers.  */
2559b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72 20  .  if( pTrigger 
2559c 29 7b 0a 20 20 20 20 75 33 32 20 6d 61 73 6b 3b  ){.    u32 mask;
2559d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2559e 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20       /* Mask of 
2559f 4f 4c 44 2e 2a 20 63 6f 6c 75 6d 6e 73 20 69 6e  OLD.* columns in
255a0 20 75 73 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20   use */.    int 
255a1 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  iCol;           
255a2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65            /* Ite
255a3 72 61 74 6f 72 20 75 73 65 64 20 77 68 69 6c 65  rator used while
255a4 20 70 6f 70 75 6c 61 74 69 6e 67 20 4f 4c 44 2e   populating OLD.
255a5 2a 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 54 4f 44  * */..    /* TOD
255a6 4f 3a 20 43 6f 75 6c 64 20 75 73 65 20 74 65 6d  O: Could use tem
255a7 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 73  porary registers
255a8 20 68 65 72 65 2e 20 41 6c 73 6f 20 63 6f 75 6c   here. Also coul
255a9 64 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 20 20  d attempt to.   
255aa 20 2a 2a 20 61 76 6f 69 64 20 63 6f 70 79 69 6e   ** avoid copyin
255ab 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  g the contents o
255ac 66 20 74 68 65 20 72 6f 77 69 64 20 72 65 67 69  f the rowid regi
255ad 73 74 65 72 2e 20 20 2a 2f 0a 20 20 20 20 6d 61  ster.  */.    ma
255ae 73 6b 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67  sk = sqlite3Trig
255af 67 65 72 4f 6c 64 6d 61 73 6b 28 70 50 61 72 73  gerOldmask(pPars
255b0 65 2c 20 70 54 72 69 67 67 65 72 2c 20 54 4b 5f  e, pTrigger, TK_
255b1 44 45 4c 45 54 45 2c 20 30 2c 20 70 54 61 62 2c  DELETE, 0, pTab,
255b2 20 6f 6e 63 6f 6e 66 29 3b 0a 20 20 20 20 69 4f   onconf);.    iO
255b3 6c 64 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65  ld = pParse->nMe
255b4 6d 2b 31 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  m+1;.    pParse-
255b5 3e 6e 4d 65 6d 20 2b 3d 20 28 31 20 2b 20 70 54  >nMem += (1 + pT
255b6 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 0a 20 20 20 20  ab->nCol);..    
255b7 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20  /* Populate the 
255b8 4f 4c 44 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62  OLD.* pseudo-tab
255b9 6c 65 20 72 65 67 69 73 74 65 72 20 61 72 72 61  le register arra
255ba 79 2e 20 54 68 65 73 65 20 76 61 6c 75 65 73 20  y. These values 
255bb 77 69 6c 6c 20 62 65 20 0a 20 20 20 20 2a 2a 20  will be .    ** 
255bc 75 73 65 64 20 62 79 20 61 6e 79 20 42 45 46 4f  used by any BEFO
255bd 52 45 20 61 6e 64 20 41 46 54 45 52 20 74 72 69  RE and AFTER tri
255be 67 67 65 72 73 20 74 68 61 74 20 65 78 69 73 74  ggers that exist
255bf 2e 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  .  */.    sqlite
255c0 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
255c1 50 5f 43 6f 70 79 2c 20 69 52 6f 77 69 64 2c 20  P_Copy, iRowid, 
255c2 69 4f 6c 64 29 3b 0a 20 20 20 20 66 6f 72 28 69  iOld);.    for(i
255c3 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62  Col=0; iCol<pTab
255c4 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b  ->nCol; iCol++){
255c5 0a 20 20 20 20 20 20 69 66 28 20 6d 61 73 6b 3d  .      if( mask=
255c6 3d 30 78 66 66 66 66 66 66 66 66 20 7c 7c 20 6d  =0xffffffff || m
255c7 61 73 6b 26 28 31 3c 3c 69 43 6f 6c 29 20 29 7b  ask&(1<<iCol) ){
255c8 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 54 61  .        int iTa
255c9 72 67 65 74 20 3d 20 69 4f 6c 64 20 2b 20 69 43  rget = iOld + iC
255ca 6f 6c 20 2b 20 31 3b 0a 20 20 20 20 20 20 20 20  ol + 1;.        
255cb 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
255cc 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
255cd 69 43 75 72 2c 20 69 43 6f 6c 2c 20 69 54 61 72  iCur, iCol, iTar
255ce 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71  get);.        sq
255cf 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75  lite3ColumnDefau
255d0 6c 74 28 76 2c 20 70 54 61 62 2c 20 69 43 6f 6c  lt(v, pTab, iCol
255d1 2c 20 69 54 61 72 67 65 74 29 3b 0a 20 20 20 20  , iTarget);.    
255d2 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
255d3 2a 20 49 6e 76 6f 6b 65 20 61 6e 79 20 42 45 46  * Invoke any BEF
255d4 4f 52 45 20 74 72 69 67 67 65 72 20 70 72 6f 67  ORE trigger prog
255d5 72 61 6d 73 20 2a 2f 0a 20 20 20 20 73 71 6c 69  rams */.    sqli
255d6 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65  te3CodeRowTrigge
255d7 72 28 70 50 61 72 73 65 2c 20 70 54 72 69 67 67  r(pParse, pTrigg
255d8 65 72 2c 20 0a 20 20 20 20 20 20 20 20 54 4b 5f  er, .        TK_
255d9 44 45 4c 45 54 45 2c 20 30 2c 20 54 52 49 47 47  DELETE, 0, TRIGG
255da 45 52 5f 42 45 46 4f 52 45 2c 20 70 54 61 62 2c  ER_BEFORE, pTab,
255db 20 2d 31 2c 20 69 4f 6c 64 2c 20 6f 6e 63 6f 6e   -1, iOld, oncon
255dc 66 2c 20 69 4c 61 62 65 6c 0a 20 20 20 20 29 3b  f, iLabel.    );
255dd 0a 0a 20 20 20 20 2f 2a 20 53 65 65 6b 20 74 68  ..    /* Seek th
255de 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20  e cursor to the 
255df 72 6f 77 20 74 6f 20 62 65 20 64 65 6c 65 74 65  row to be delete
255e0 64 20 61 67 61 69 6e 2e 20 49 74 20 6d 61 79 20  d again. It may 
255e1 62 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 74  be that.    ** t
255e2 68 65 20 42 45 46 4f 52 45 20 74 72 69 67 67 65  he BEFORE trigge
255e3 72 73 20 63 6f 64 65 64 20 61 62 6f 76 65 20 68  rs coded above h
255e4 61 76 65 20 61 6c 72 65 61 64 79 20 72 65 6d 6f  ave already remo
255e5 76 65 64 20 74 68 65 20 72 6f 77 0a 20 20 20 20  ved the row.    
255e6 2a 2a 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64  ** being deleted
255e7 2e 20 44 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74  . Do not attempt
255e8 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20 72   to delete the r
255e9 6f 77 20 61 20 73 65 63 6f 6e 64 20 74 69 6d 65  ow a second time
255ea 2c 20 61 6e 64 20 0a 20 20 20 20 2a 2a 20 64 6f  , and .    ** do
255eb 20 6e 6f 74 20 66 69 72 65 20 41 46 54 45 52 20   not fire AFTER 
255ec 74 72 69 67 67 65 72 73 2e 20 20 2a 2f 0a 20 20  triggers.  */.  
255ed 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
255ee 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69  Op3(v, OP_NotExi
255ef 73 74 73 2c 20 69 43 75 72 2c 20 69 4c 61 62 65  sts, iCur, iLabe
255f0 6c 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20 7d 0a  l, iRowid);.  }.
255f1 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65  .  /* Delete the
255f2 20 69 6e 64 65 78 20 61 6e 64 20 74 61 62 6c 65   index and table
255f3 20 65 6e 74 72 69 65 73 2e 20 53 6b 69 70 20 74   entries. Skip t
255f4 68 69 73 20 73 74 65 70 20 69 66 20 70 54 61 62  his step if pTab
255f5 20 69 73 20 72 65 61 6c 6c 79 0a 20 20 2a 2a 20   is really.  ** 
255f6 61 20 76 69 65 77 20 28 69 6e 20 77 68 69 63 68  a view (in which
255f7 20 63 61 73 65 20 74 68 65 20 6f 6e 6c 79 20 65   case the only e
255f8 66 66 65 63 74 20 6f 66 20 74 68 65 20 44 45 4c  ffect of the DEL
255f9 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73  ETE statement is
255fa 20 74 6f 0a 20 20 2a 2a 20 66 69 72 65 20 74 68   to.  ** fire th
255fb 65 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69  e INSTEAD OF tri
255fc 67 67 65 72 73 29 2e 20 20 2a 2f 20 0a 20 20 69  ggers).  */ .  i
255fd 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  f( pTab->pSelect
255fe 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
255ff 65 33 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64  e3GenerateRowInd
25600 65 78 44 65 6c 65 74 65 28 70 50 61 72 73 65 2c  exDelete(pParse,
25601 20 70 54 61 62 2c 20 69 43 75 72 2c 20 30 29 3b   pTab, iCur, 0);
25602 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
25603 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 44 65 6c  AddOp2(v, OP_Del
25604 65 74 65 2c 20 69 43 75 72 2c 20 28 63 6f 75 6e  ete, iCur, (coun
25605 74 3f 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45  t?OPFLAG_NCHANGE
25606 3a 30 29 29 3b 0a 20 20 20 20 69 66 28 20 63 6f  :0));.    if( co
25607 75 6e 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  unt ){.      sql
25608 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
25609 28 76 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 4e  (v, -1, pTab->zN
2560a 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b  ame, P4_STATIC);
2560b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
2560c 20 49 6e 76 6f 6b 65 20 41 46 54 45 52 20 74 72   Invoke AFTER tr
2560d 69 67 67 65 72 73 2e 20 2a 2f 0a 20 20 69 66 28  iggers. */.  if(
2560e 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20   pTrigger ){.   
2560f 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54   sqlite3CodeRowT
25610 72 69 67 67 65 72 28 70 50 61 72 73 65 2c 20 70  rigger(pParse, p
25611 54 72 69 67 67 65 72 2c 20 0a 20 20 20 20 20 20  Trigger, .      
25612 20 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 2c 20    TK_DELETE, 0, 
25613 54 52 49 47 47 45 52 5f 41 46 54 45 52 2c 20 70  TRIGGER_AFTER, p
25614 54 61 62 2c 20 2d 31 2c 20 69 4f 6c 64 2c 20 6f  Tab, -1, iOld, o
25615 6e 63 6f 6e 66 2c 20 69 4c 61 62 65 6c 0a 20 20  nconf, iLabel.  
25616 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a    );.  }..  /* J
25617 75 6d 70 20 68 65 72 65 20 69 66 20 74 68 65 20  ump here if the 
25618 72 6f 77 20 68 61 64 20 61 6c 72 65 61 64 79 20  row had already 
25619 62 65 65 6e 20 64 65 6c 65 74 65 64 20 62 65 66  been deleted bef
2561a 6f 72 65 20 61 6e 79 20 42 45 46 4f 52 45 0a 20  ore any BEFORE. 
2561b 20 2a 2a 20 74 72 69 67 67 65 72 20 70 72 6f 67   ** trigger prog
2561c 72 61 6d 73 20 77 65 72 65 20 69 6e 76 6f 6b 65  rams were invoke
2561d 64 2e 20 4f 72 20 69 66 20 61 20 74 72 69 67 67  d. Or if a trigg
2561e 65 72 20 70 72 6f 67 72 61 6d 20 74 68 72 6f 77  er program throw
2561f 73 20 61 20 0a 20 20 2a 2a 20 52 41 49 53 45 28  s a .  ** RAISE(
25620 49 47 4e 4f 52 45 29 20 65 78 63 65 70 74 69 6f  IGNORE) exceptio
25621 6e 2e 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  n.  */.  sqlite3
25622 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
25623 28 76 2c 20 69 4c 61 62 65 6c 29 3b 0a 7d 0a 0a  (v, iLabel);.}..
25624 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
25625 6e 65 20 67 65 6e 65 72 61 74 65 73 20 56 44 42  ne generates VDB
25626 45 20 63 6f 64 65 20 74 68 61 74 20 63 61 75 73  E code that caus
25627 65 73 20 74 68 65 20 64 65 6c 65 74 69 6f 6e 20  es the deletion 
25628 6f 66 20 61 6c 6c 0a 2a 2a 20 69 6e 64 65 78 20  of all.** index 
25629 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74  entries associat
2562a 65 64 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65  ed with a single
2562b 20 72 6f 77 20 6f 66 20 61 20 73 69 6e 67 6c 65   row of a single
2562c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   table..**.** Th
2562d 65 20 56 44 42 45 20 6d 75 73 74 20 62 65 20 69  e VDBE must be i
2562e 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 73  n a particular s
2562f 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 20 72  tate when this r
25630 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
25631 2e 0a 2a 2a 20 54 68 65 73 65 20 61 72 65 20 74  ..** These are t
25632 68 65 20 72 65 71 75 69 72 65 6d 65 6e 74 73 3a  he requirements:
25633 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 20 41 20 72  .**.**   1.  A r
25634 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72  ead/write cursor
25635 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 70 54 61   pointing to pTa
25636 62 2c 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6e  b, the table con
25637 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 0a  taining the row.
25638 2a 2a 20 20 20 20 20 20 20 74 6f 20 62 65 20 64  **       to be d
25639 65 6c 65 74 65 64 2c 20 6d 75 73 74 20 62 65 20  eleted, must be 
2563a 6f 70 65 6e 65 64 20 61 73 20 63 75 72 73 6f 72  opened as cursor
2563b 20 6e 75 6d 62 65 72 20 22 69 43 75 72 22 2e 0a   number "iCur"..
2563c 2a 2a 0a 2a 2a 20 20 20 32 2e 20 20 52 65 61 64  **.**   2.  Read
2563d 2f 77 72 69 74 65 20 63 75 72 73 6f 72 73 20 66  /write cursors f
2563e 6f 72 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f  or all indices o
2563f 66 20 70 54 61 62 20 6d 75 73 74 20 62 65 20 6f  f pTab must be o
25640 70 65 6e 20 61 73 0a 2a 2a 20 20 20 20 20 20 20  pen as.**       
25641 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 43  cursor number iC
25642 75 72 2b 69 20 66 6f 72 20 74 68 65 20 69 2d 74  ur+i for the i-t
25643 68 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 20  h index..**.**  
25644 20 33 2e 20 20 54 68 65 20 22 69 43 75 72 22 20   3.  The "iCur" 
25645 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20 70  cursor must be p
25646 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 72  ointing to the r
25647 6f 77 20 74 68 61 74 20 69 73 20 74 6f 20 62 65  ow that is to be
25648 0a 2a 2a 20 20 20 20 20 20 20 64 65 6c 65 74 65  .**       delete
25649 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
2564a 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2564b 33 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65  3GenerateRowInde
2564c 78 44 65 6c 65 74 65 28 0a 20 20 50 61 72 73 65  xDelete(.  Parse
2564d 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a   *pParse,     /*
2564e 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64   Parsing and cod
2564f 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e  e generating con
25650 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20  text */.  Table 
25651 2a 70 54 61 62 2c 20 20 20 20 20 20 20 2f 2a 20  *pTab,       /* 
25652 54 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67  Table containing
25653 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20 64   the row to be d
25654 65 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20  eleted */.  int 
25655 69 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 2f  iCur,          /
25656 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  * Cursor number 
25657 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 2a 2f  for the table */
25658 0a 20 20 69 6e 74 20 2a 61 52 65 67 49 64 78 20  .  int *aRegIdx 
25659 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 64 65        /* Only de
2565a 6c 65 74 65 20 69 66 20 61 52 65 67 49 64 78 21  lete if aRegIdx!
2565b 3d 30 20 26 26 20 61 52 65 67 49 64 78 5b 69 5d  =0 && aRegIdx[i]
2565c 3e 30 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  >0 */.){.  int i
2565d 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b  ;.  Index *pIdx;
2565e 0a 20 20 69 6e 74 20 72 31 3b 0a 0a 20 20 66 6f  .  int r1;..  fo
2565f 72 28 69 3d 31 2c 20 70 49 64 78 3d 70 54 61 62  r(i=1, pIdx=pTab
25660 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
25661 69 2b 2b 2c 20 70 49 64 78 3d 70 49 64 78 2d 3e  i++, pIdx=pIdx->
25662 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20  pNext){.    if( 
25663 61 52 65 67 49 64 78 21 3d 30 20 26 26 20 61 52  aRegIdx!=0 && aR
25664 65 67 49 64 78 5b 69 2d 31 5d 3d 3d 30 20 29 20  egIdx[i-1]==0 ) 
25665 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 72 31  continue;.    r1
25666 20 3d 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61   = sqlite3Genera
25667 74 65 49 6e 64 65 78 4b 65 79 28 70 50 61 72 73  teIndexKey(pPars
25668 65 2c 20 70 49 64 78 2c 20 69 43 75 72 2c 20 30  e, pIdx, iCur, 0
25669 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
2566a 33 56 64 62 65 41 64 64 4f 70 33 28 70 50 61 72  3VdbeAddOp3(pPar
2566b 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 49 64  se->pVdbe, OP_Id
2566c 78 44 65 6c 65 74 65 2c 20 69 43 75 72 2b 69 2c  xDelete, iCur+i,
2566d 20 72 31 2c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d   r1,pIdx->nColum
2566e 6e 2b 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  n+1);.  }.}../*.
2566f 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
25670 20 74 68 61 74 20 77 69 6c 6c 20 61 73 73 65 6d   that will assem
25671 62 6c 65 20 61 6e 20 69 6e 64 65 78 20 6b 65 79  ble an index key
25672 20 61 6e 64 20 70 75 74 20 69 74 20 69 6e 20 72   and put it in r
25673 65 67 69 73 74 65 72 0a 2a 2a 20 72 65 67 4f 75  egister.** regOu
25674 74 2e 20 20 54 68 65 20 6b 65 79 20 77 69 74 68  t.  The key with
25675 20 62 65 20 66 6f 72 20 69 6e 64 65 78 20 70 49   be for index pI
25676 64 78 20 77 68 69 63 68 20 69 73 20 61 6e 20 69  dx which is an i
25677 6e 64 65 78 20 6f 6e 20 70 54 61 62 2e 0a 2a 2a  ndex on pTab..**
25678 20 69 43 75 72 20 69 73 20 74 68 65 20 69 6e 64   iCur is the ind
25679 65 78 20 6f 66 20 61 20 63 75 72 73 6f 72 20 6f  ex of a cursor o
2567a 70 65 6e 20 6f 6e 20 74 68 65 20 70 54 61 62 20  pen on the pTab 
2567b 74 61 62 6c 65 20 61 6e 64 20 70 6f 69 6e 74 69  table and pointi
2567c 6e 67 20 74 6f 0a 2a 2a 20 74 68 65 20 65 6e 74  ng to.** the ent
2567d 72 79 20 74 68 61 74 20 6e 65 65 64 73 20 69 6e  ry that needs in
2567e 64 65 78 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65  dexing..**.** Re
2567f 74 75 72 6e 20 61 20 72 65 67 69 73 74 65 72 20  turn a register 
25680 6e 75 6d 62 65 72 20 77 68 69 63 68 20 69 73 20  number which is 
25681 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 20 62  the first in a b
25682 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 72 65 67 69 73  lock of.** regis
25683 74 65 72 73 20 74 68 61 74 20 68 6f 6c 64 73 20  ters that holds 
25684 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20  the elements of 
25685 74 68 65 20 69 6e 64 65 78 20 6b 65 79 2e 20 20  the index key.  
25686 54 68 65 0a 2a 2a 20 62 6c 6f 63 6b 20 6f 66 20  The.** block of 
25687 72 65 67 69 73 74 65 72 73 20 68 61 73 20 61 6c  registers has al
25688 72 65 61 64 79 20 62 65 65 6e 20 64 65 61 6c 6c  ready been deall
25689 6f 63 61 74 65 64 20 62 79 20 74 68 65 20 74 69  ocated by the ti
2568a 6d 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69  me.** this routi
2568b 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2f 0a 53  ne returns..*/.S
2568c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2568d 74 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74  t sqlite3Generat
2568e 65 49 6e 64 65 78 4b 65 79 28 0a 20 20 50 61 72  eIndexKey(.  Par
2568f 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
25690 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
25691 78 74 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70  xt */.  Index *p
25692 49 64 78 2c 20 20 20 20 20 20 20 2f 2a 20 54 68  Idx,       /* Th
25693 65 20 69 6e 64 65 78 20 66 6f 72 20 77 68 69 63  e index for whic
25694 68 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20  h to generate a 
25695 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75  key */.  int iCu
25696 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43  r,          /* C
25697 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72  ursor number for
25698 20 74 68 65 20 70 49 64 78 2d 3e 70 54 61 62 6c   the pIdx->pTabl
25699 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  e table */.  int
2569a 20 72 65 67 4f 75 74 2c 20 20 20 20 20 20 20 20   regOut,        
2569b 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65 77  /* Write the new
2569c 20 69 6e 64 65 78 20 6b 65 79 20 74 6f 20 74 68   index key to th
2569d 69 73 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20  is register */. 
2569e 20 69 6e 74 20 64 6f 4d 61 6b 65 52 65 63 20 20   int doMakeRec  
2569f 20 20 20 20 2f 2a 20 52 75 6e 20 74 68 65 20 4f      /* Run the O
256a0 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e 73  P_MakeRecord ins
256a1 74 72 75 63 74 69 6f 6e 20 69 66 20 74 72 75 65  truction if true
256a2 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76   */.){.  Vdbe *v
256a3 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
256a4 3b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 54 61 62  ;.  int j;.  Tab
256a5 6c 65 20 2a 70 54 61 62 20 3d 20 70 49 64 78 2d  le *pTab = pIdx-
256a6 3e 70 54 61 62 6c 65 3b 0a 20 20 69 6e 74 20 72  >pTable;.  int r
256a7 65 67 42 61 73 65 3b 0a 20 20 69 6e 74 20 6e 43  egBase;.  int nC
256a8 6f 6c 3b 0a 0a 20 20 6e 43 6f 6c 20 3d 20 70 49  ol;..  nCol = pI
256a9 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 72  dx->nColumn;.  r
256aa 65 67 42 61 73 65 20 3d 20 73 71 6c 69 74 65 33  egBase = sqlite3
256ab 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61  GetTempRange(pPa
256ac 72 73 65 2c 20 6e 43 6f 6c 2b 31 29 3b 0a 20 20  rse, nCol+1);.  
256ad 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
256ae 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69  2(v, OP_Rowid, i
256af 43 75 72 2c 20 72 65 67 42 61 73 65 2b 6e 43 6f  Cur, regBase+nCo
256b0 6c 29 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a  l);.  for(j=0; j
256b1 3c 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20  <nCol; j++){.   
256b2 20 69 6e 74 20 69 64 78 20 3d 20 70 49 64 78 2d   int idx = pIdx-
256b3 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20  >aiColumn[j];.  
256b4 20 20 69 66 28 20 69 64 78 3d 3d 70 54 61 62 2d    if( idx==pTab-
256b5 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  >iPKey ){.      
256b6 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
256b7 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72  2(v, OP_SCopy, r
256b8 65 67 42 61 73 65 2b 6e 43 6f 6c 2c 20 72 65 67  egBase+nCol, reg
256b9 42 61 73 65 2b 6a 29 3b 0a 20 20 20 20 7d 65 6c  Base+j);.    }el
256ba 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
256bb 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
256bc 50 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20  P_Column, iCur, 
256bd 69 64 78 2c 20 72 65 67 42 61 73 65 2b 6a 29 3b  idx, regBase+j);
256be 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f  .      sqlite3Co
256bf 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20 70  lumnDefault(v, p
256c0 54 61 62 2c 20 69 64 78 2c 20 2d 31 29 3b 0a 20  Tab, idx, -1);. 
256c1 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 64     }.  }.  if( d
256c2 6f 4d 61 6b 65 52 65 63 20 29 7b 0a 20 20 20 20  oMakeRec ){.    
256c3 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
256c4 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  3(v, OP_MakeReco
256c5 72 64 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f  rd, regBase, nCo
256c6 6c 2b 31 2c 20 72 65 67 4f 75 74 29 3b 0a 20 20  l+1, regOut);.  
256c7 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
256c8 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 73 71 6c  ngeP4(v, -1, sql
256c9 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74  ite3IndexAffinit
256ca 79 53 74 72 28 76 2c 20 70 49 64 78 29 2c 20 30  yStr(v, pIdx), 0
256cb 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
256cc 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43  prCacheAffinityC
256cd 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65  hange(pParse, re
256ce 67 42 61 73 65 2c 20 6e 43 6f 6c 2b 31 29 3b 0a  gBase, nCol+1);.
256cf 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 6c    }.  sqlite3Rel
256d0 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50  easeTempRange(pP
256d1 61 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e  arse, regBase, n
256d2 43 6f 6c 2b 31 29 3b 0a 20 20 72 65 74 75 72 6e  Col+1);.  return
256d3 20 72 65 67 42 61 73 65 3b 0a 7d 0a 0a 2f 2a 20   regBase;.}../* 
256d4 4d 61 6b 65 20 73 75 72 65 20 22 69 73 56 69 65  Make sure "isVie
256d5 77 22 20 67 65 74 73 20 75 6e 64 65 66 69 6e 65  w" gets undefine
256d6 64 20 69 6e 20 63 61 73 65 20 74 68 69 73 20 66  d in case this f
256d7 69 6c 65 20 62 65 63 6f 6d 65 73 20 70 61 72 74  ile becomes part
256d8 20 6f 66 0a 2a 2a 20 74 68 65 20 61 6d 61 6c 67   of.** the amalg
256d9 61 6d 61 74 69 6f 6e 20 2d 20 73 6f 20 74 68 61  amation - so tha
256da 74 20 73 75 62 73 65 71 75 65 6e 74 20 66 69 6c  t subsequent fil
256db 65 73 20 64 6f 20 6e 6f 74 20 73 65 65 20 69 73  es do not see is
256dc 56 69 65 77 20 61 73 20 61 0a 2a 2a 20 6d 61 63  View as a.** mac
256dd 72 6f 2e 20 2a 2f 0a 23 75 6e 64 65 66 20 69 73  ro. */.#undef is
256de 56 69 65 77 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  View../*********
256df 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 65 6c  ***** End of del
256e0 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ete.c **********
256e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256e3 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
256e4 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
256e5 20 66 75 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   func.c ********
256e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256e8 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 32  ****/./*.** 2002
256e9 20 46 65 62 72 75 61 72 79 20 32 33 0a 2a 2a 0a   February 23.**.
256ea 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
256eb 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
256ec 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
256ed 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
256ee 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
256ef 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
256f0 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
256f1 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
256f2 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
256f3 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
256f4 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
256f5 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
256f6 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
256f7 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
256f8 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
256f9 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
256fa 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
256fb 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
256fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
25700 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
25701 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e  s the C function
25702 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  s that implement
25703 20 76 61 72 69 6f 75 73 20 53 51 4c 0a 2a 2a 20   various SQL.** 
25704 66 75 6e 63 74 69 6f 6e 73 20 6f 66 20 53 51 4c  functions of SQL
25705 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65  ite.  .**.** The
25706 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e 65 20 65  re is only one e
25707 78 70 6f 72 74 65 64 20 73 79 6d 62 6f 6c 20 69  xported symbol i
25708 6e 20 74 68 69 73 20 66 69 6c 65 20 2d 20 74 68  n this file - th
25709 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71  e function.** sq
2570a 6c 69 74 65 52 65 67 69 73 74 65 72 42 75 69 6c  liteRegisterBuil
2570b 64 69 6e 46 75 6e 63 74 69 6f 6e 73 28 29 20 66  dinFunctions() f
2570c 6f 75 6e 64 20 61 74 20 74 68 65 20 62 6f 74 74  ound at the bott
2570d 6f 6d 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a  om of the file..
2570e 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 63 6f 64  ** All other cod
2570f 65 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65  e has file scope
25710 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ..*/../*.** Retu
25711 72 6e 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67  rn the collating
25712 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 6f 63 69   function associ
25713 61 74 65 64 20 77 69 74 68 20 61 20 66 75 6e 63  ated with a func
25714 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
25715 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33  CollSeq *sqlite3
25716 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 73  GetFuncCollSeq(s
25717 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
25718 63 6f 6e 74 65 78 74 29 7b 0a 20 20 72 65 74 75  context){.  retu
25719 72 6e 20 63 6f 6e 74 65 78 74 2d 3e 70 43 6f 6c  rn context->pCol
2571a 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  l;.}../*.** Impl
2571b 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
2571c 65 20 6e 6f 6e 2d 61 67 67 72 65 67 61 74 65 20  e non-aggregate 
2571d 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 28 29 20  min() and max() 
2571e 66 75 6e 63 74 69 6f 6e 73 0a 2a 2f 0a 73 74 61  functions.*/.sta
2571f 74 69 63 20 76 6f 69 64 20 6d 69 6e 6d 61 78 46  tic void minmaxF
25720 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
25721 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
25722 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
25723 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
25724 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  rgv.){.  int i;.
25725 20 20 69 6e 74 20 6d 61 73 6b 3b 20 20 20 20 2f    int mask;    /
25726 2a 20 30 20 66 6f 72 20 6d 69 6e 28 29 20 6f 72  * 0 for min() or
25727 20 30 78 66 66 66 66 66 66 66 66 20 66 6f 72 20   0xffffffff for 
25728 6d 61 78 28 29 20 2a 2f 0a 20 20 69 6e 74 20 69  max() */.  int i
25729 42 65 73 74 3b 0a 20 20 43 6f 6c 6c 53 65 71 20  Best;.  CollSeq 
2572a 2a 70 43 6f 6c 6c 3b 0a 0a 20 20 61 73 73 65 72  *pColl;..  asser
2572b 74 28 20 61 72 67 63 3e 31 20 29 3b 0a 20 20 6d  t( argc>1 );.  m
2572c 61 73 6b 20 3d 20 73 71 6c 69 74 65 33 5f 75 73  ask = sqlite3_us
2572d 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29  er_data(context)
2572e 3d 3d 30 20 3f 20 30 20 3a 20 2d 31 3b 0a 20 20  ==0 ? 0 : -1;.  
2572f 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47  pColl = sqlite3G
25730 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 63 6f  etFuncCollSeq(co
25731 6e 74 65 78 74 29 3b 0a 20 20 61 73 73 65 72 74  ntext);.  assert
25732 28 20 70 43 6f 6c 6c 20 29 3b 0a 20 20 61 73 73  ( pColl );.  ass
25733 65 72 74 28 20 6d 61 73 6b 3d 3d 2d 31 20 7c 7c  ert( mask==-1 ||
25734 20 6d 61 73 6b 3d 3d 30 20 29 3b 0a 20 20 69 42   mask==0 );.  iB
25735 65 73 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 73  est = 0;.  if( s
25736 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
25737 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49  e(argv[0])==SQLI
25738 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e  TE_NULL ) return
25739 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 61  ;.  for(i=1; i<a
2573a 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  rgc; i++){.    i
2573b 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f( sqlite3_value
2573c 5f 74 79 70 65 28 61 72 67 76 5b 69 5d 29 3d 3d  _type(argv[i])==
2573d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65  SQLITE_NULL ) re
2573e 74 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 28 73  turn;.    if( (s
2573f 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65  qlite3MemCompare
25740 28 61 72 67 76 5b 69 42 65 73 74 5d 2c 20 61 72  (argv[iBest], ar
25741 67 76 5b 69 5d 2c 20 70 43 6f 6c 6c 29 5e 6d 61  gv[i], pColl)^ma
25742 73 6b 29 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  sk)>=0 ){.      
25743 74 65 73 74 63 61 73 65 28 20 6d 61 73 6b 3d 3d  testcase( mask==
25744 30 20 29 3b 0a 20 20 20 20 20 20 69 42 65 73 74  0 );.      iBest
25745 20 3d 20 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = i;.    }.  }.
25746 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
25747 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20  _value(context, 
25748 61 72 67 76 5b 69 42 65 73 74 5d 29 3b 0a 7d 0a  argv[iBest]);.}.
25749 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
2574a 65 20 74 79 70 65 20 6f 66 20 74 68 65 20 61 72  e type of the ar
2574b 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69  gument..*/.stati
2574c 63 20 76 6f 69 64 20 74 79 70 65 6f 66 46 75 6e  c void typeofFun
2574d 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
2574e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
2574f 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20   int NotUsed,.  
25750 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
25751 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  argv.){.  const 
25752 63 68 61 72 20 2a 7a 20 3d 20 30 3b 0a 20 20 55  char *z = 0;.  U
25753 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
25754 4e 6f 74 55 73 65 64 29 3b 0a 20 20 73 77 69 74  NotUsed);.  swit
25755 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ch( sqlite3_valu
25756 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 20  e_type(argv[0]) 
25757 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  ){.    case SQLI
25758 54 45 5f 49 4e 54 45 47 45 52 3a 20 7a 20 3d 20  TE_INTEGER: z = 
25759 22 69 6e 74 65 67 65 72 22 3b 20 62 72 65 61 6b  "integer"; break
2575a 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2575b 45 5f 54 45 58 54 3a 20 20 20 20 7a 20 3d 20 22  E_TEXT:    z = "
2575c 74 65 78 74 22 3b 20 20 20 20 62 72 65 61 6b 3b  text";    break;
2575d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
2575e 5f 46 4c 4f 41 54 3a 20 20 20 7a 20 3d 20 22 72  _FLOAT:   z = "r
2575f 65 61 6c 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a  eal";    break;.
25760 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
25761 42 4c 4f 42 3a 20 20 20 20 7a 20 3d 20 22 62 6c  BLOB:    z = "bl
25762 6f 62 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20  ob";    break;. 
25763 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 20     default:     
25764 20 20 20 20 20 20 20 20 7a 20 3d 20 22 6e 75 6c          z = "nul
25765 6c 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  l";    break;.  
25766 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  }.  sqlite3_resu
25767 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
25768 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53   z, -1, SQLITE_S
25769 54 41 54 49 43 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  TATIC);.}.../*.*
2576a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
2576b 20 6f 66 20 74 68 65 20 6c 65 6e 67 74 68 28 29   of the length()
2576c 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a 73 74 61   function.*/.sta
2576d 74 69 63 20 76 6f 69 64 20 6c 65 6e 67 74 68 46  tic void lengthF
2576e 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
2576f 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
25770 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
25771 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
25772 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 6c 65 6e  rgv.){.  int len
25773 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67  ;..  assert( arg
25774 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44  c==1 );.  UNUSED
25775 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29  _PARAMETER(argc)
25776 3b 0a 20 20 73 77 69 74 63 68 28 20 73 71 6c 69  ;.  switch( sqli
25777 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
25778 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20 63  rgv[0]) ){.    c
25779 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a  ase SQLITE_BLOB:
2577a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
2577b 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 63 61  _INTEGER:.    ca
2577c 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a  se SQLITE_FLOAT:
2577d 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
2577e 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74  _result_int(cont
2577f 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  ext, sqlite3_val
25780 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
25781 29 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ));.      break;
25782 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
25783 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a 20  SQLITE_TEXT: {. 
25784 20 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67       const unsig
25785 6e 65 64 20 63 68 61 72 20 2a 7a 20 3d 20 73 71  ned char *z = sq
25786 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
25787 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20  (argv[0]);.     
25788 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75   if( z==0 ) retu
25789 72 6e 3b 0a 20 20 20 20 20 20 6c 65 6e 20 3d 20  rn;.      len = 
2578a 30 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  0;.      while( 
2578b 2a 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 6c 65  *z ){.        le
2578c 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 53 51 4c  n++;.        SQL
2578d 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29  ITE_SKIP_UTF8(z)
2578e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2578f 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
25790 6e 74 28 63 6f 6e 74 65 78 74 2c 20 6c 65 6e 29  nt(context, len)
25791 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
25792 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74     }.    default
25793 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
25794 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 63 6f  3_result_null(co
25795 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 62 72  ntext);.      br
25796 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  eak;.    }.  }.}
25797 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
25798 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 62  tation of the ab
25799 73 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a  s() function.*/.
2579a 73 74 61 74 69 63 20 76 6f 69 64 20 61 62 73 46  static void absF
2579b 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  unc(sqlite3_cont
2579c 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e  ext *context, in
2579d 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
2579e 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20  value **argv){. 
2579f 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31   assert( argc==1
257a0 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52   );.  UNUSED_PAR
257a1 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20  AMETER(argc);.  
257a2 73 77 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f  switch( sqlite3_
257a3 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
257a4 30 5d 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20  0]) ){.    case 
257a5 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20  SQLITE_INTEGER: 
257a6 7b 0a 20 20 20 20 20 20 69 36 34 20 69 56 61 6c  {.      i64 iVal
257a7 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
257a8 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 5d 29 3b  _int64(argv[0]);
257a9 0a 20 20 20 20 20 20 69 66 28 20 69 56 61 6c 3c  .      if( iVal<
257aa 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  0 ){.        if(
257ab 20 28 69 56 61 6c 3c 3c 31 29 3d 3d 30 20 29 7b   (iVal<<1)==0 ){
257ac 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
257ad 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
257ae 63 6f 6e 74 65 78 74 2c 20 22 69 6e 74 65 67 65  context, "intege
257af 72 20 6f 76 65 72 66 6c 6f 77 22 2c 20 2d 31 29  r overflow", -1)
257b0 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  ;.          retu
257b1 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  rn;.        }.  
257b2 20 20 20 20 20 20 69 56 61 6c 20 3d 20 2d 69 56        iVal = -iV
257b3 61 6c 3b 0a 20 20 20 20 20 20 7d 20 0a 20 20 20  al;.      } .   
257b4 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
257b5 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c  t_int64(context,
257b6 20 69 56 61 6c 29 3b 0a 20 20 20 20 20 20 62 72   iVal);.      br
257b7 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
257b8 61 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3a  ase SQLITE_NULL:
257b9 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
257ba 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 63 6f 6e  _result_null(con
257bb 74 65 78 74 29 3b 0a 20 20 20 20 20 20 62 72 65  text);.      bre
257bc 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65  ak;.    }.    de
257bd 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 64  fault: {.      d
257be 6f 75 62 6c 65 20 72 56 61 6c 20 3d 20 73 71 6c  ouble rVal = sql
257bf 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
257c0 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20  e(argv[0]);.    
257c1 20 20 69 66 28 20 72 56 61 6c 3c 30 20 29 20 72    if( rVal<0 ) r
257c2 56 61 6c 20 3d 20 2d 72 56 61 6c 3b 0a 20 20 20  Val = -rVal;.   
257c3 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
257c4 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74  t_double(context
257c5 2c 20 72 56 61 6c 29 3b 0a 20 20 20 20 20 20 62  , rVal);.      b
257c6 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
257c7 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
257c8 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ntation of the s
257c9 75 62 73 74 72 28 29 20 66 75 6e 63 74 69 6f 6e  ubstr() function
257ca 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 73 74 72 28 78  ..**.** substr(x
257cb 2c 70 31 2c 70 32 29 20 20 72 65 74 75 72 6e 73  ,p1,p2)  returns
257cc 20 70 32 20 63 68 61 72 61 63 74 65 72 73 20 6f   p2 characters o
257cd 66 20 78 5b 5d 20 62 65 67 69 6e 6e 69 6e 67 20  f x[] beginning 
257ce 77 69 74 68 20 70 31 2e 0a 2a 2a 20 70 31 20 69  with p1..** p1 i
257cf 73 20 31 2d 69 6e 64 65 78 65 64 2e 20 20 53 6f  s 1-indexed.  So
257d0 20 73 75 62 73 74 72 28 78 2c 31 2c 31 29 20 72   substr(x,1,1) r
257d1 65 74 75 72 6e 73 20 74 68 65 20 66 69 72 73 74  eturns the first
257d2 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 6f 66   character.** of
257d3 20 78 2e 20 20 49 66 20 78 20 69 73 20 74 65 78   x.  If x is tex
257d4 74 2c 20 74 68 65 6e 20 77 65 20 61 63 74 75 61  t, then we actua
257d5 6c 6c 79 20 63 6f 75 6e 74 20 55 54 46 2d 38 20  lly count UTF-8 
257d6 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 20 49  characters..** I
257d7 66 20 78 20 69 73 20 61 20 62 6c 6f 62 2c 20 74  f x is a blob, t
257d8 68 65 6e 20 77 65 20 63 6f 75 6e 74 20 62 79 74  hen we count byt
257d9 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 31 20  es..**.** If p1 
257da 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65  is negative, the
257db 6e 20 77 65 20 62 65 67 69 6e 20 61 62 73 28 70  n we begin abs(p
257dc 31 29 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20  1) from the end 
257dd 6f 66 20 78 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69  of x[]..*/.stati
257de 63 20 76 6f 69 64 20 73 75 62 73 74 72 46 75 6e  c void substrFun
257df 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
257e0 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
257e1 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
257e2 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
257e3 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73  v.){.  const uns
257e4 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20  igned char *z;. 
257e5 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
257e6 63 68 61 72 20 2a 7a 32 3b 0a 20 20 69 6e 74 20  char *z2;.  int 
257e7 6c 65 6e 3b 0a 20 20 69 6e 74 20 70 30 74 79 70  len;.  int p0typ
257e8 65 3b 0a 20 20 69 36 34 20 70 31 2c 20 70 32 3b  e;.  i64 p1, p2;
257e9 0a 20 20 69 6e 74 20 6e 65 67 50 32 20 3d 20 30  .  int negP2 = 0
257ea 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67  ;..  assert( arg
257eb 63 3d 3d 33 20 7c 7c 20 61 72 67 63 3d 3d 32 20  c==3 || argc==2 
257ec 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
257ed 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
257ee 5b 31 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c  [1])==SQLITE_NUL
257ef 4c 0a 20 20 20 7c 7c 20 28 61 72 67 63 3d 3d 33  L.   || (argc==3
257f0 20 26 26 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   && sqlite3_valu
257f1 65 5f 74 79 70 65 28 61 72 67 76 5b 32 5d 29 3d  e_type(argv[2])=
257f2 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 29 0a 20 20  =SQLITE_NULL).  
257f3 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
257f4 20 7d 0a 20 20 70 30 74 79 70 65 20 3d 20 73 71   }.  p0type = sq
257f5 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
257f6 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28  (argv[0]);.  if(
257f7 20 70 30 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f   p0type==SQLITE_
257f8 42 4c 4f 42 20 29 7b 0a 20 20 20 20 6c 65 6e 20  BLOB ){.    len 
257f9 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
257fa 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a  bytes(argv[0]);.
257fb 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f      z = sqlite3_
257fc 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b  value_blob(argv[
257fd 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d  0]);.    if( z==
257fe 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  0 ) return;.    
257ff 61 73 73 65 72 74 28 20 6c 65 6e 3d 3d 73 71 6c  assert( len==sql
25800 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
25801 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 7d  (argv[0]) );.  }
25802 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71  else{.    z = sq
25803 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
25804 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69  (argv[0]);.    i
25805 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( z==0 ) return
25806 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 30 3b 0a 20  ;.    len = 0;. 
25807 20 20 20 66 6f 72 28 7a 32 3d 7a 3b 20 2a 7a 32     for(z2=z; *z2
25808 3b 20 6c 65 6e 2b 2b 29 7b 0a 20 20 20 20 20 20  ; len++){.      
25809 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38  SQLITE_SKIP_UTF8
2580a 28 7a 32 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  (z2);.    }.  }.
2580b 20 20 70 31 20 3d 20 73 71 6c 69 74 65 33 5f 76    p1 = sqlite3_v
2580c 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d  alue_int(argv[1]
2580d 29 3b 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 33  );.  if( argc==3
2580e 20 29 7b 0a 20 20 20 20 70 32 20 3d 20 73 71 6c   ){.    p2 = sql
2580f 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61  ite3_value_int(a
25810 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 69 66 28  rgv[2]);.    if(
25811 20 70 32 3c 30 20 29 7b 0a 20 20 20 20 20 20 70   p2<0 ){.      p
25812 32 20 3d 20 2d 70 32 3b 0a 20 20 20 20 20 20 6e  2 = -p2;.      n
25813 65 67 50 32 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  egP2 = 1;.    }.
25814 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 32 20    }else{.    p2 
25815 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  = sqlite3_contex
25816 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74  t_db_handle(cont
25817 65 78 74 29 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  ext)->aLimit[SQL
25818 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
25819 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 31 3c  ];.  }.  if( p1<
2581a 30 20 29 7b 0a 20 20 20 20 70 31 20 2b 3d 20 6c  0 ){.    p1 += l
2581b 65 6e 3b 0a 20 20 20 20 69 66 28 20 70 31 3c 30  en;.    if( p1<0
2581c 20 29 7b 0a 20 20 20 20 20 20 70 32 20 2b 3d 20   ){.      p2 += 
2581d 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20 70 32  p1;.      if( p2
2581e 3c 30 20 29 20 70 32 20 3d 20 30 3b 0a 20 20 20  <0 ) p2 = 0;.   
2581f 20 20 20 70 31 20 3d 20 30 3b 0a 20 20 20 20 7d     p1 = 0;.    }
25820 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 31 3e  .  }else if( p1>
25821 30 20 29 7b 0a 20 20 20 20 70 31 2d 2d 3b 0a 20  0 ){.    p1--;. 
25822 20 7d 65 6c 73 65 20 69 66 28 20 70 32 3e 30 20   }else if( p2>0 
25823 29 7b 0a 20 20 20 20 70 32 2d 2d 3b 0a 20 20 7d  ){.    p2--;.  }
25824 0a 20 20 69 66 28 20 6e 65 67 50 32 20 29 7b 0a  .  if( negP2 ){.
25825 20 20 20 20 70 31 20 2d 3d 20 70 32 3b 0a 20 20      p1 -= p2;.  
25826 20 20 69 66 28 20 70 31 3c 30 20 29 7b 0a 20 20    if( p1<0 ){.  
25827 20 20 20 20 70 32 20 2b 3d 20 70 31 3b 0a 20 20      p2 += p1;.  
25828 20 20 20 20 70 31 20 3d 20 30 3b 0a 20 20 20 20      p1 = 0;.    
25829 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  }.  }.  assert( 
2582a 70 31 3e 3d 30 20 26 26 20 70 32 3e 3d 30 20 29  p1>=0 && p2>=0 )
2582b 3b 0a 20 20 69 66 28 20 70 31 2b 70 32 3e 6c 65  ;.  if( p1+p2>le
2582c 6e 20 29 7b 0a 20 20 20 20 70 32 20 3d 20 6c 65  n ){.    p2 = le
2582d 6e 2d 70 31 3b 0a 20 20 20 20 69 66 28 20 70 32  n-p1;.    if( p2
2582e 3c 30 20 29 20 70 32 20 3d 20 30 3b 0a 20 20 7d  <0 ) p2 = 0;.  }
2582f 0a 20 20 69 66 28 20 70 30 74 79 70 65 21 3d 53  .  if( p0type!=S
25830 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20  QLITE_BLOB ){.  
25831 20 20 77 68 69 6c 65 28 20 2a 7a 20 26 26 20 70    while( *z && p
25832 31 20 29 7b 0a 20 20 20 20 20 20 53 51 4c 49 54  1 ){.      SQLIT
25833 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a  E_SKIP_UTF8(z);.
25834 20 20 20 20 20 20 70 31 2d 2d 3b 0a 20 20 20 20        p1--;.    
25835 7d 0a 20 20 20 20 66 6f 72 28 7a 32 3d 7a 3b 20  }.    for(z2=z; 
25836 2a 7a 32 20 26 26 20 70 32 3b 20 70 32 2d 2d 29  *z2 && p2; p2--)
25837 7b 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53  {.      SQLITE_S
25838 4b 49 50 5f 55 54 46 38 28 7a 32 29 3b 0a 20 20  KIP_UTF8(z2);.  
25839 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
2583a 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
2583b 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 2c 20 28  ext, (char*)z, (
2583c 69 6e 74 29 28 7a 32 2d 7a 29 2c 20 53 51 4c 49  int)(z2-z), SQLI
2583d 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
2583e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
2583f 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28  te3_result_blob(
25840 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29  context, (char*)
25841 26 7a 5b 70 31 5d 2c 20 28 69 6e 74 29 70 32 2c  &z[p1], (int)p2,
25842 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
25843 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  T);.  }.}../*.**
25844 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
25845 6f 66 20 74 68 65 20 72 6f 75 6e 64 28 29 20 66  of the round() f
25846 75 6e 63 74 69 6f 6e 0a 2a 2f 0a 23 69 66 6e 64  unction.*/.#ifnd
25847 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
25848 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 73 74  LOATING_POINT.st
25849 61 74 69 63 20 76 6f 69 64 20 72 6f 75 6e 64 46  atic void roundF
2584a 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  unc(sqlite3_cont
2584b 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e  ext *context, in
2584c 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
2584d 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20  value **argv){. 
2584e 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 64 6f   int n = 0;.  do
2584f 75 62 6c 65 20 72 3b 0a 20 20 63 68 61 72 20 2a  uble r;.  char *
25850 7a 42 75 66 3b 0a 20 20 61 73 73 65 72 74 28 20  zBuf;.  assert( 
25851 61 72 67 63 3d 3d 31 20 7c 7c 20 61 72 67 63 3d  argc==1 || argc=
25852 3d 32 20 29 3b 0a 20 20 69 66 28 20 61 72 67 63  =2 );.  if( argc
25853 3d 3d 32 20 29 7b 0a 20 20 20 20 69 66 28 20 53  ==2 ){.    if( S
25854 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73 71 6c 69  QLITE_NULL==sqli
25855 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
25856 72 67 76 5b 31 5d 29 20 29 20 72 65 74 75 72 6e  rgv[1]) ) return
25857 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65  ;.    n = sqlite
25858 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76  3_value_int(argv
25859 5b 31 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e 3e  [1]);.    if( n>
2585a 33 30 20 29 20 6e 20 3d 20 33 30 3b 0a 20 20 20  30 ) n = 30;.   
2585b 20 69 66 28 20 6e 3c 30 20 29 20 6e 20 3d 20 30   if( n<0 ) n = 0
2585c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69  ;.  }.  if( sqli
2585d 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
2585e 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f  rgv[0])==SQLITE_
2585f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20  NULL ) return;. 
25860 20 72 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c   r = sqlite3_val
25861 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30  ue_double(argv[0
25862 5d 29 3b 0a 20 20 7a 42 75 66 20 3d 20 73 71 6c  ]);.  zBuf = sql
25863 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 2e  ite3_mprintf("%.
25864 2a 66 22 2c 6e 2c 72 29 3b 0a 20 20 69 66 28 20  *f",n,r);.  if( 
25865 7a 42 75 66 3d 3d 30 20 29 7b 0a 20 20 20 20 73  zBuf==0 ){.    s
25866 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
25867 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78  ror_nomem(contex
25868 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  t);.  }else{.   
25869 20 73 71 6c 69 74 65 33 41 74 6f 46 28 7a 42 75   sqlite3AtoF(zBu
2586a 66 2c 20 26 72 29 3b 0a 20 20 20 20 73 71 6c 69  f, &r);.    sqli
2586b 74 65 33 5f 66 72 65 65 28 7a 42 75 66 29 3b 0a  te3_free(zBuf);.
2586c 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
2586d 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78  lt_double(contex
2586e 74 2c 20 72 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  t, r);.  }.}.#en
2586f 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  dif../*.** Alloc
25870 61 74 65 20 6e 42 79 74 65 20 62 79 74 65 73 20  ate nByte bytes 
25871 6f 66 20 73 70 61 63 65 20 75 73 69 6e 67 20 73  of space using s
25872 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e  qlite3_malloc().
25873 20 49 66 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63   If the.** alloc
25874 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 63 61 6c  ation fails, cal
25875 6c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  l sqlite3_result
25876 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 29 20 74  _error_nomem() t
25877 6f 20 6e 6f 74 69 66 79 0a 2a 2a 20 74 68 65 20  o notify.** the 
25878 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
25879 74 68 61 74 20 6d 61 6c 6c 6f 63 28 29 20 68 61  that malloc() ha
2587a 73 20 66 61 69 6c 65 64 20 61 6e 64 20 72 65 74  s failed and ret
2587b 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 20  urn NULL..** If 
2587c 6e 42 79 74 65 20 69 73 20 6c 61 72 67 65 72 20  nByte is larger 
2587d 74 68 61 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d  than the maximum
2587e 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20   string or blob 
2587f 6c 65 6e 67 74 68 2c 20 74 68 65 6e 0a 2a 2a 20  length, then.** 
25880 72 61 69 73 65 20 61 6e 20 53 51 4c 49 54 45 5f  raise an SQLITE_
25881 54 4f 4f 42 49 47 20 65 78 63 65 70 74 69 6f 6e  TOOBIG exception
25882 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c   and return NULL
25883 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
25884 20 2a 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28   *contextMalloc(
25885 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
25886 2a 63 6f 6e 74 65 78 74 2c 20 69 36 34 20 6e 42  *context, i64 nB
25887 79 74 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b  yte){.  char *z;
25888 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
25889 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
2588a 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65  _db_handle(conte
2588b 78 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  xt);.  assert( n
2588c 42 79 74 65 3e 30 20 29 3b 0a 20 20 74 65 73 74  Byte>0 );.  test
2588d 63 61 73 65 28 20 6e 42 79 74 65 3d 3d 64 62 2d  case( nByte==db-
2588e 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
2588f 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a  IMIT_LENGTH] );.
25890 20 20 74 65 73 74 63 61 73 65 28 20 6e 42 79 74    testcase( nByt
25891 65 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  e==db->aLimit[SQ
25892 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
25893 48 5d 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e 42  H]+1 );.  if( nB
25894 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  yte>db->aLimit[S
25895 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
25896 54 48 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  TH] ){.    sqlit
25897 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
25898 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b  toobig(context);
25899 0a 20 20 20 20 7a 20 3d 20 30 3b 0a 20 20 7d 65  .    z = 0;.  }e
2589a 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c  lse{.    z = sql
2589b 69 74 65 33 4d 61 6c 6c 6f 63 28 28 69 6e 74 29  ite3Malloc((int)
2589c 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20  nByte);.    if( 
2589d 21 7a 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  !z ){.      sqli
2589e 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2589f 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b  _nomem(context);
258a0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
258a1 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn z;.}../*.** 
258a2 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
258a3 66 20 74 68 65 20 75 70 70 65 72 28 29 20 61 6e  f the upper() an
258a4 64 20 6c 6f 77 65 72 28 29 20 53 51 4c 20 66 75  d lower() SQL fu
258a5 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74  nctions..*/.stat
258a6 69 63 20 76 6f 69 64 20 75 70 70 65 72 46 75 6e  ic void upperFun
258a7 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  c(sqlite3_contex
258a8 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20  t *context, int 
258a9 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
258aa 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 63  lue **argv){.  c
258ab 68 61 72 20 2a 7a 31 3b 0a 20 20 63 6f 6e 73 74  har *z1;.  const
258ac 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 69 6e 74   char *z2;.  int
258ad 20 69 2c 20 6e 3b 0a 20 20 55 4e 55 53 45 44 5f   i, n;.  UNUSED_
258ae 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b  PARAMETER(argc);
258af 0a 20 20 7a 32 20 3d 20 28 63 68 61 72 2a 29 73  .  z2 = (char*)s
258b0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
258b1 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 20  t(argv[0]);.  n 
258b2 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
258b3 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a  bytes(argv[0]);.
258b4 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74    /* Verify that
258b5 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5f 62 79   the call to _by
258b6 74 65 73 28 29 20 64 6f 65 73 20 6e 6f 74 20 69  tes() does not i
258b7 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 5f 74  nvalidate the _t
258b8 65 78 74 28 29 20 70 6f 69 6e 74 65 72 20 2a 2f  ext() pointer */
258b9 0a 20 20 61 73 73 65 72 74 28 20 7a 32 3d 3d 28  .  assert( z2==(
258ba 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61  char*)sqlite3_va
258bb 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
258bc 29 20 29 3b 0a 20 20 69 66 28 20 7a 32 20 29 7b  ) );.  if( z2 ){
258bd 0a 20 20 20 20 7a 31 20 3d 20 63 6f 6e 74 65 78  .    z1 = contex
258be 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c  tMalloc(context,
258bf 20 28 28 69 36 34 29 6e 29 2b 31 29 3b 0a 20 20   ((i64)n)+1);.  
258c0 20 20 69 66 28 20 7a 31 20 29 7b 0a 20 20 20 20    if( z1 ){.    
258c1 20 20 6d 65 6d 63 70 79 28 7a 31 2c 20 7a 32 2c    memcpy(z1, z2,
258c2 20 6e 2b 31 29 3b 0a 20 20 20 20 20 20 66 6f 72   n+1);.      for
258c3 28 69 3d 30 3b 20 7a 31 5b 69 5d 3b 20 69 2b 2b  (i=0; z1[i]; i++
258c4 29 7b 0a 20 20 20 20 20 20 20 20 7a 31 5b 69 5d  ){.        z1[i]
258c5 20 3d 20 28 63 68 61 72 29 73 71 6c 69 74 65 33   = (char)sqlite3
258c6 54 6f 75 70 70 65 72 28 7a 31 5b 69 5d 29 3b 0a  Toupper(z1[i]);.
258c7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
258c8 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
258c9 74 28 63 6f 6e 74 65 78 74 2c 20 7a 31 2c 20 2d  t(context, z1, -
258ca 31 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29  1, sqlite3_free)
258cb 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74  ;.    }.  }.}.st
258cc 61 74 69 63 20 76 6f 69 64 20 6c 6f 77 65 72 46  atic void lowerF
258cd 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  unc(sqlite3_cont
258ce 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e  ext *context, in
258cf 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
258d0 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20  value **argv){. 
258d1 20 75 38 20 2a 7a 31 3b 0a 20 20 63 6f 6e 73 74   u8 *z1;.  const
258d2 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 69 6e 74   char *z2;.  int
258d3 20 69 2c 20 6e 3b 0a 20 20 55 4e 55 53 45 44 5f   i, n;.  UNUSED_
258d4 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b  PARAMETER(argc);
258d5 0a 20 20 7a 32 20 3d 20 28 63 68 61 72 2a 29 73  .  z2 = (char*)s
258d6 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
258d7 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 20  t(argv[0]);.  n 
258d8 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
258d9 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a  bytes(argv[0]);.
258da 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74    /* Verify that
258db 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5f 62 79   the call to _by
258dc 74 65 73 28 29 20 64 6f 65 73 20 6e 6f 74 20 69  tes() does not i
258dd 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 5f 74  nvalidate the _t
258de 65 78 74 28 29 20 70 6f 69 6e 74 65 72 20 2a 2f  ext() pointer */
258df 0a 20 20 61 73 73 65 72 74 28 20 7a 32 3d 3d 28  .  assert( z2==(
258e0 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61  char*)sqlite3_va
258e1 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
258e2 29 20 29 3b 0a 20 20 69 66 28 20 7a 32 20 29 7b  ) );.  if( z2 ){
258e3 0a 20 20 20 20 7a 31 20 3d 20 63 6f 6e 74 65 78  .    z1 = contex
258e4 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c  tMalloc(context,
258e5 20 28 28 69 36 34 29 6e 29 2b 31 29 3b 0a 20 20   ((i64)n)+1);.  
258e6 20 20 69 66 28 20 7a 31 20 29 7b 0a 20 20 20 20    if( z1 ){.    
258e7 20 20 6d 65 6d 63 70 79 28 7a 31 2c 20 7a 32 2c    memcpy(z1, z2,
258e8 20 6e 2b 31 29 3b 0a 20 20 20 20 20 20 66 6f 72   n+1);.      for
258e9 28 69 3d 30 3b 20 7a 31 5b 69 5d 3b 20 69 2b 2b  (i=0; z1[i]; i++
258ea 29 7b 0a 20 20 20 20 20 20 20 20 7a 31 5b 69 5d  ){.        z1[i]
258eb 20 3d 20 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65   = sqlite3Tolowe
258ec 72 28 7a 31 5b 69 5d 29 3b 0a 20 20 20 20 20 20  r(z1[i]);.      
258ed 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  }.      sqlite3_
258ee 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
258ef 65 78 74 2c 20 28 63 68 61 72 20 2a 29 7a 31 2c  ext, (char *)z1,
258f0 20 2d 31 2c 20 73 71 6c 69 74 65 33 5f 66 72 65   -1, sqlite3_fre
258f1 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  e);.    }.  }.}.
258f2 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
258f3 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 49 46 4e  ation of the IFN
258f4 55 4c 4c 28 29 2c 20 4e 56 4c 28 29 2c 20 61 6e  ULL(), NVL(), an
258f5 64 20 43 4f 41 4c 45 53 43 45 28 29 20 66 75 6e  d COALESCE() fun
258f6 63 74 69 6f 6e 73 2e 20 20 0a 2a 2a 20 41 6c 6c  ctions.  .** All
258f7 20 74 68 72 65 65 20 64 6f 20 74 68 65 20 73 61   three do the sa
258f8 6d 65 20 74 68 69 6e 67 2e 20 20 54 68 65 79 20  me thing.  They 
258f9 72 65 74 75 72 6e 20 74 68 65 20 66 69 72 73 74  return the first
258fa 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 61 72 67   non-NULL.** arg
258fb 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  ument..*/.static
258fc 20 76 6f 69 64 20 69 66 6e 75 6c 6c 46 75 6e 63   void ifnullFunc
258fd 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
258fe 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
258ff 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
25900 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
25901 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66  .){.  int i;.  f
25902 6f 72 28 69 3d 30 3b 20 69 3c 61 72 67 63 3b 20  or(i=0; i<argc; 
25903 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 53 51  i++){.    if( SQ
25904 4c 49 54 45 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74  LITE_NULL!=sqlit
25905 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
25906 67 76 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20  gv[i]) ){.      
25907 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76  sqlite3_result_v
25908 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20 61 72  alue(context, ar
25909 67 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 62 72  gv[i]);.      br
2590a 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  eak;.    }.  }.}
2590b 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
2590c 74 61 74 69 6f 6e 20 6f 66 20 72 61 6e 64 6f 6d  tation of random
2590d 28 29 2e 20 20 52 65 74 75 72 6e 20 61 20 72 61  ().  Return a ra
2590e 6e 64 6f 6d 20 69 6e 74 65 67 65 72 2e 20 20 0a  ndom integer.  .
2590f 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  */.static void r
25910 61 6e 64 6f 6d 46 75 6e 63 28 0a 20 20 73 71 6c  andomFunc(.  sql
25911 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
25912 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74  ntext,.  int Not
25913 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f  Used,.  sqlite3_
25914 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32  value **NotUsed2
25915 0a 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  .){.  sqlite_int
25916 36 34 20 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50  64 r;.  UNUSED_P
25917 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65  ARAMETER2(NotUse
25918 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20  d, NotUsed2);.  
25919 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65  sqlite3_randomne
2591a 73 73 28 73 69 7a 65 6f 66 28 72 29 2c 20 26 72  ss(sizeof(r), &r
2591b 29 3b 0a 20 20 69 66 28 20 72 3c 30 20 29 7b 0a  );.  if( r<0 ){.
2591c 20 20 20 20 2f 2a 20 57 65 20 6e 65 65 64 20 74      /* We need t
2591d 6f 20 70 72 65 76 65 6e 74 20 61 20 72 61 6e 64  o prevent a rand
2591e 6f 6d 20 6e 75 6d 62 65 72 20 6f 66 20 30 78 38  om number of 0x8
2591f 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 20  000000000000000 
25920 0a 20 20 20 20 2a 2a 20 28 6f 72 20 2d 39 32 32  .    ** (or -922
25921 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 38  3372036854775808
25922 29 20 73 69 6e 63 65 20 77 68 65 6e 20 79 6f 75  ) since when you
25923 20 64 6f 20 61 62 73 28 29 20 6f 66 20 74 68 61   do abs() of tha
25924 74 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20  t.    ** number 
25925 6f 66 20 79 6f 75 20 67 65 74 20 74 68 65 20 73  of you get the s
25926 61 6d 65 20 76 61 6c 75 65 20 62 61 63 6b 20 61  ame value back a
25927 67 61 69 6e 2e 20 20 54 6f 20 64 6f 20 74 68 69  gain.  To do thi
25928 73 0a 20 20 20 20 2a 2a 20 69 6e 20 61 20 77 61  s.    ** in a wa
25929 79 20 74 68 61 74 20 69 73 20 74 65 73 74 61 62  y that is testab
2592a 6c 65 2c 20 6d 61 73 6b 20 74 68 65 20 73 69 67  le, mask the sig
2592b 6e 20 62 69 74 20 6f 66 66 20 6f 66 20 6e 65 67  n bit off of neg
2592c 61 74 69 76 65 0a 20 20 20 20 2a 2a 20 76 61 6c  ative.    ** val
2592d 75 65 73 2c 20 72 65 73 75 6c 74 69 6e 67 20 69  ues, resulting i
2592e 6e 20 61 20 70 6f 73 69 74 69 76 65 20 76 61 6c  n a positive val
2592f 75 65 2e 20 20 54 68 65 6e 20 74 61 6b 65 20 74  ue.  Then take t
25930 68 65 20 0a 20 20 20 20 2a 2a 20 32 73 20 63 6f  he .    ** 2s co
25931 6d 70 6c 65 6d 65 6e 74 20 6f 66 20 74 68 61 74  mplement of that
25932 20 70 6f 73 69 74 69 76 65 20 76 61 6c 75 65 2e   positive value.
25933 20 20 54 68 65 20 65 6e 64 20 72 65 73 75 6c 74    The end result
25934 20 63 61 6e 0a 20 20 20 20 2a 2a 20 74 68 65 72   can.    ** ther
25935 65 66 6f 72 65 20 62 65 20 6e 6f 20 6c 65 73 73  efore be no less
25936 20 74 68 61 6e 20 2d 39 32 32 33 33 37 32 30 33   than -922337203
25937 36 38 35 34 37 37 35 38 30 37 2e 0a 20 20 20 20  6854775807..    
25938 2a 2f 0a 20 20 20 20 72 20 3d 20 2d 28 72 20 5e  */.    r = -(r ^
25939 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e 74 36   (((sqlite3_int6
2593a 34 29 31 29 3c 3c 36 33 29 29 3b 0a 20 20 7d 0a  4)1)<<63));.  }.
2593b 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
2593c 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c 20  _int64(context, 
2593d 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70  r);.}../*.** Imp
2593e 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 72  lementation of r
2593f 61 6e 64 6f 6d 62 6c 6f 62 28 4e 29 2e 20 20 52  andomblob(N).  R
25940 65 74 75 72 6e 20 61 20 72 61 6e 64 6f 6d 20 62  eturn a random b
25941 6c 6f 62 0a 2a 2a 20 74 68 61 74 20 69 73 20 4e  lob.** that is N
25942 20 62 79 74 65 73 20 6c 6f 6e 67 2e 0a 2a 2f 0a   bytes long..*/.
25943 73 74 61 74 69 63 20 76 6f 69 64 20 72 61 6e 64  static void rand
25944 6f 6d 42 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65  omBlob(.  sqlite
25945 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
25946 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
25947 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
25948 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20  **argv.){.  int 
25949 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  n;.  unsigned ch
2594a 61 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28  ar *p;.  assert(
2594b 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e   argc==1 );.  UN
2594c 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61  USED_PARAMETER(a
2594d 72 67 63 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69  rgc);.  n = sqli
2594e 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72  te3_value_int(ar
2594f 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 6e 3c  gv[0]);.  if( n<
25950 31 20 29 7b 0a 20 20 20 20 6e 20 3d 20 31 3b 0a  1 ){.    n = 1;.
25951 20 20 7d 0a 20 20 70 20 3d 20 63 6f 6e 74 65 78    }.  p = contex
25952 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c  tMalloc(context,
25953 20 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a   n);.  if( p ){.
25954 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64      sqlite3_rand
25955 6f 6d 6e 65 73 73 28 6e 2c 20 70 29 3b 0a 20 20  omness(n, p);.  
25956 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
25957 5f 62 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28  _blob(context, (
25958 63 68 61 72 2a 29 70 2c 20 6e 2c 20 73 71 6c 69  char*)p, n, sqli
25959 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 7d 0a 7d  te3_free);.  }.}
2595a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
2595b 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 61  tation of the la
2595c 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28  st_insert_rowid(
2595d 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20  ) SQL function. 
2595e 20 54 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76   The return.** v
2595f 61 6c 75 65 20 69 73 20 74 68 65 20 73 61 6d 65  alue is the same
25960 20 61 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f   as the sqlite3_
25961 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
25962 64 28 29 20 41 50 49 20 66 75 6e 63 74 69 6f 6e  d() API function
25963 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
25964 20 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77   last_insert_row
25965 69 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  id(.  sqlite3_co
25966 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20  ntext *context, 
25967 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 20  .  int NotUsed, 
25968 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
25969 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20   **NotUsed2.){. 
2596a 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
2596b 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
2596c 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
2596d 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
2596e 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20  METER2(NotUsed, 
2596f 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 73 71 6c  NotUsed2);.  sql
25970 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36  ite3_result_int6
25971 34 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74  4(context, sqlit
25972 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  e3_last_insert_r
25973 6f 77 69 64 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a  owid(db));.}../*
25974 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
25975 6f 6e 20 6f 66 20 74 68 65 20 63 68 61 6e 67 65  on of the change
25976 73 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  s() SQL function
25977 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  .  The return va
25978 6c 75 65 20 69 73 20 74 68 65 0a 2a 2a 20 73 61  lue is the.** sa
25979 6d 65 20 61 73 20 74 68 65 20 73 71 6c 69 74 65  me as the sqlite
2597a 33 5f 63 68 61 6e 67 65 73 28 29 20 41 50 49 20  3_changes() API 
2597b 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  function..*/.sta
2597c 74 69 63 20 76 6f 69 64 20 63 68 61 6e 67 65 73  tic void changes
2597d 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
2597e 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
2597f 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73  int NotUsed,.  s
25980 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e  qlite3_value **N
25981 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 73 71 6c  otUsed2.){.  sql
25982 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74  ite3 *db = sqlit
25983 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
25984 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20  ndle(context);. 
25985 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
25986 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55  R2(NotUsed, NotU
25987 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33  sed2);.  sqlite3
25988 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74  _result_int(cont
25989 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 63 68 61  ext, sqlite3_cha
2598a 6e 67 65 73 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a  nges(db));.}../*
2598b 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
2598c 6f 6e 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 5f  on of the total_
2598d 63 68 61 6e 67 65 73 28 29 20 53 51 4c 20 66 75  changes() SQL fu
2598e 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 74  nction.  The ret
2598f 75 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20  urn value is.** 
25990 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20  the same as the 
25991 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68  sqlite3_total_ch
25992 61 6e 67 65 73 28 29 20 41 50 49 20 66 75 6e 63  anges() API func
25993 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
25994 76 6f 69 64 20 74 6f 74 61 6c 5f 63 68 61 6e 67  void total_chang
25995 65 73 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  es(.  sqlite3_co
25996 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
25997 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20    int NotUsed,. 
25998 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
25999 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 73  *NotUsed2.){.  s
2599a 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c  qlite3 *db = sql
2599b 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
2599c 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b  handle(context);
2599d 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
2599e 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
2599f 74 55 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74  tUsed2);.  sqlit
259a0 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f  e3_result_int(co
259a1 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 74  ntext, sqlite3_t
259a2 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 64 62 29  otal_changes(db)
259a3 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74  );.}../*.** A st
259a4 72 75 63 74 75 72 65 20 64 65 66 69 6e 69 6e 67  ructure defining
259a5 20 68 6f 77 20 74 6f 20 64 6f 20 47 4c 4f 42 2d   how to do GLOB-
259a6 73 74 79 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e  style comparison
259a7 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 63 6f 6d  s..*/.struct com
259a8 70 61 72 65 49 6e 66 6f 20 7b 0a 20 20 75 38 20  pareInfo {.  u8 
259a9 6d 61 74 63 68 41 6c 6c 3b 0a 20 20 75 38 20 6d  matchAll;.  u8 m
259aa 61 74 63 68 4f 6e 65 3b 0a 20 20 75 38 20 6d 61  atchOne;.  u8 ma
259ab 74 63 68 53 65 74 3b 0a 20 20 75 38 20 6e 6f 43  tchSet;.  u8 noC
259ac 61 73 65 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46  ase;.};../*.** F
259ad 6f 72 20 4c 49 4b 45 20 61 6e 64 20 47 4c 4f 42  or LIKE and GLOB
259ae 20 6d 61 74 63 68 69 6e 67 20 6f 6e 20 45 42 43   matching on EBC
259af 44 49 43 20 6d 61 63 68 69 6e 65 73 2c 20 61 73  DIC machines, as
259b0 73 75 6d 65 20 74 68 61 74 20 65 76 65 72 79 0a  sume that every.
259b1 2a 2a 20 63 68 61 72 61 63 74 65 72 20 69 73 20  ** character is 
259b2 65 78 61 63 74 6c 79 20 6f 6e 65 20 62 79 74 65  exactly one byte
259b3 20 69 6e 20 73 69 7a 65 2e 20 20 41 6c 73 6f 2c   in size.  Also,
259b4 20 61 6c 6c 20 63 68 61 72 61 63 74 65 72 73 20   all characters 
259b5 61 72 65 0a 2a 2a 20 61 62 6c 65 20 74 6f 20 70  are.** able to p
259b6 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 75 70  articipate in up
259b7 70 65 72 2d 63 61 73 65 2d 74 6f 2d 6c 6f 77 65  per-case-to-lowe
259b8 72 2d 63 61 73 65 20 6d 61 70 70 69 6e 67 73 20  r-case mappings 
259b9 69 6e 20 45 42 43 44 49 43 0a 2a 2a 20 77 68 65  in EBCDIC.** whe
259ba 72 65 61 73 20 6f 6e 6c 79 20 63 68 61 72 61 63  reas only charac
259bb 74 65 72 73 20 6c 65 73 73 20 74 68 61 6e 20 30  ters less than 0
259bc 78 38 30 20 64 6f 20 69 6e 20 41 53 43 49 49 2e  x80 do in ASCII.
259bd 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .*/.#if defined(
259be 53 51 4c 49 54 45 5f 45 42 43 44 49 43 29 0a 23  SQLITE_EBCDIC).#
259bf 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 55   define sqlite3U
259c0 74 66 38 52 65 61 64 28 41 2c 43 29 20 20 20 20  tf8Read(A,C)    
259c1 28 2a 28 41 2b 2b 29 29 0a 23 20 64 65 66 69 6e  (*(A++)).# defin
259c2 65 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77  e GlogUpperToLow
259c3 65 72 28 41 29 20 20 20 20 20 41 20 3d 20 73 71  er(A)     A = sq
259c4 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65  lite3UpperToLowe
259c5 72 5b 41 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66  r[A].#else.# def
259c6 69 6e 65 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c  ine GlogUpperToL
259c7 6f 77 65 72 28 41 29 20 20 20 20 20 69 66 28 20  ower(A)     if( 
259c8 41 3c 30 78 38 30 20 29 7b 20 41 20 3d 20 73 71  A<0x80 ){ A = sq
259c9 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65  lite3UpperToLowe
259ca 72 5b 41 5d 3b 20 7d 0a 23 65 6e 64 69 66 0a 0a  r[A]; }.#endif..
259cb 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72  static const str
259cc 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20  uct compareInfo 
259cd 67 6c 6f 62 49 6e 66 6f 20 3d 20 7b 20 27 2a 27  globInfo = { '*'
259ce 2c 20 27 3f 27 2c 20 27 5b 27 2c 20 30 20 7d 3b  , '?', '[', 0 };
259cf 0a 2f 2a 20 54 68 65 20 63 6f 72 72 65 63 74 20  ./* The correct 
259d0 53 51 4c 2d 39 32 20 62 65 68 61 76 69 6f 72 20  SQL-92 behavior 
259d1 69 73 20 66 6f 72 20 74 68 65 20 4c 49 4b 45 20  is for the LIKE 
259d2 6f 70 65 72 61 74 6f 72 20 74 6f 20 69 67 6e 6f  operator to igno
259d3 72 65 0a 2a 2a 20 63 61 73 65 2e 20 20 54 68 75  re.** case.  Thu
259d4 73 20 20 27 61 27 20 4c 49 4b 45 20 27 41 27 20  s  'a' LIKE 'A' 
259d5 77 6f 75 6c 64 20 62 65 20 74 72 75 65 2e 20 2a  would be true. *
259d6 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73  /.static const s
259d7 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66  truct compareInf
259d8 6f 20 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 20 3d  o likeInfoNorm =
259d9 20 7b 20 27 25 27 2c 20 27 5f 27 2c 20 20 20 30   { '%', '_',   0
259da 2c 20 31 20 7d 3b 0a 2f 2a 20 49 66 20 53 51 4c  , 1 };./* If SQL
259db 49 54 45 5f 43 41 53 45 5f 53 45 4e 53 49 54 49  ITE_CASE_SENSITI
259dc 56 45 5f 4c 49 4b 45 20 69 73 20 64 65 66 69 6e  VE_LIKE is defin
259dd 65 64 2c 20 74 68 65 6e 20 74 68 65 20 4c 49 4b  ed, then the LIK
259de 45 20 6f 70 65 72 61 74 6f 72 0a 2a 2a 20 69 73  E operator.** is
259df 20 63 61 73 65 20 73 65 6e 73 69 74 69 76 65 20   case sensitive 
259e0 63 61 75 73 69 6e 67 20 27 61 27 20 4c 49 4b 45  causing 'a' LIKE
259e1 20 27 41 27 20 74 6f 20 62 65 20 66 61 6c 73 65   'A' to be false
259e2 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74   */.static const
259e3 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49   struct compareI
259e4 6e 66 6f 20 6c 69 6b 65 49 6e 66 6f 41 6c 74 20  nfo likeInfoAlt 
259e5 3d 20 7b 20 27 25 27 2c 20 27 5f 27 2c 20 20 20  = { '%', '_',   
259e6 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43  0, 0 };../*.** C
259e7 6f 6d 70 61 72 65 20 74 77 6f 20 55 54 46 2d 38  ompare two UTF-8
259e8 20 73 74 72 69 6e 67 73 20 66 6f 72 20 65 71 75   strings for equ
259e9 61 6c 69 74 79 20 77 68 65 72 65 20 74 68 65 20  ality where the 
259ea 66 69 72 73 74 20 73 74 72 69 6e 67 20 63 61 6e  first string can
259eb 0a 2a 2a 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20  .** potentially 
259ec 62 65 20 61 20 22 67 6c 6f 62 22 20 65 78 70 72  be a "glob" expr
259ed 65 73 73 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20  ession.  Return 
259ee 74 72 75 65 20 28 31 29 20 69 66 20 74 68 65 79  true (1) if they
259ef 0a 2a 2a 20 61 72 65 20 74 68 65 20 73 61 6d 65  .** are the same
259f0 20 61 6e 64 20 66 61 6c 73 65 20 28 30 29 20 69   and false (0) i
259f1 66 20 74 68 65 79 20 61 72 65 20 64 69 66 66 65  f they are diffe
259f2 72 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 47 6c 6f 62  rent..**.** Glob
259f3 62 69 6e 67 20 72 75 6c 65 73 3a 0a 2a 2a 0a 2a  bing rules:.**.*
259f4 2a 20 20 20 20 20 20 27 2a 27 20 20 20 20 20 20  *      '*'      
259f5 20 4d 61 74 63 68 65 73 20 61 6e 79 20 73 65 71   Matches any seq
259f6 75 65 6e 63 65 20 6f 66 20 7a 65 72 6f 20 6f 72  uence of zero or
259f7 20 6d 6f 72 65 20 63 68 61 72 61 63 74 65 72 73   more characters
259f8 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 27 3f 27  ..**.**      '?'
259f9 20 20 20 20 20 20 20 4d 61 74 63 68 65 73 20 65         Matches e
259fa 78 61 63 74 6c 79 20 6f 6e 65 20 63 68 61 72 61  xactly one chara
259fb 63 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  cter..**.**     
259fc 5b 2e 2e 2e 5d 20 20 20 20 20 20 4d 61 74 63 68  [...]      Match
259fd 65 73 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72  es one character
259fe 20 66 72 6f 6d 20 74 68 65 20 65 6e 63 6c 6f 73   from the enclos
259ff 65 64 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 20 20  ed list of.**   
25a00 20 20 20 20 20 20 20 20 20 20 20 20 20 63 68 61               cha
25a01 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20  racters..**.**  
25a02 20 20 20 5b 5e 2e 2e 2e 5d 20 20 20 20 20 4d 61     [^...]     Ma
25a03 74 63 68 65 73 20 6f 6e 65 20 63 68 61 72 61 63  tches one charac
25a04 74 65 72 20 6e 6f 74 20 69 6e 20 74 68 65 20 65  ter not in the e
25a05 6e 63 6c 6f 73 65 64 20 6c 69 73 74 2e 0a 2a 2a  nclosed list..**
25a06 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 5b 2e 2e  .** With the [..
25a07 2e 5d 20 61 6e 64 20 5b 5e 2e 2e 2e 5d 20 6d 61  .] and [^...] ma
25a08 74 63 68 69 6e 67 2c 20 61 20 27 5d 27 20 63 68  tching, a ']' ch
25a09 61 72 61 63 74 65 72 20 63 61 6e 20 62 65 20 69  aracter can be i
25a0a 6e 63 6c 75 64 65 64 0a 2a 2a 20 69 6e 20 74 68  ncluded.** in th
25a0b 65 20 6c 69 73 74 20 62 79 20 6d 61 6b 69 6e 67  e list by making
25a0c 20 69 74 20 74 68 65 20 66 69 72 73 74 20 63 68   it the first ch
25a0d 61 72 61 63 74 65 72 20 61 66 74 65 72 20 27 5b  aracter after '[
25a0e 27 20 6f 72 20 27 5e 27 2e 20 20 41 0a 2a 2a 20  ' or '^'.  A.** 
25a0f 72 61 6e 67 65 20 6f 66 20 63 68 61 72 61 63 74  range of charact
25a10 65 72 73 20 63 61 6e 20 62 65 20 73 70 65 63 69  ers can be speci
25a11 66 69 65 64 20 75 73 69 6e 67 20 27 2d 27 2e 20  fied using '-'. 
25a12 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 20 22 5b 61   Example:.** "[a
25a13 2d 7a 5d 22 20 6d 61 74 63 68 65 73 20 61 6e 79  -z]" matches any
25a14 20 73 69 6e 67 6c 65 20 6c 6f 77 65 72 2d 63 61   single lower-ca
25a15 73 65 20 6c 65 74 74 65 72 2e 20 20 54 6f 20 6d  se letter.  To m
25a16 61 74 63 68 20 61 20 27 2d 27 2c 20 6d 61 6b 65  atch a '-', make
25a17 0a 2a 2a 20 69 74 20 74 68 65 20 6c 61 73 74 20  .** it the last 
25a18 63 68 61 72 61 63 74 65 72 20 69 6e 20 74 68 65  character in the
25a19 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   list..**.** Thi
25a1a 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 75  s routine is usu
25a1b 61 6c 6c 79 20 71 75 69 63 6b 2c 20 62 75 74 20  ally quick, but 
25a1c 63 61 6e 20 62 65 20 4e 2a 2a 32 20 69 6e 20 74  can be N**2 in t
25a1d 68 65 20 77 6f 72 73 74 20 63 61 73 65 2e 0a 2a  he worst case..*
25a1e 2a 0a 2a 2a 20 48 69 6e 74 73 3a 20 74 6f 20 6d  *.** Hints: to m
25a1f 61 74 63 68 20 27 2a 27 20 6f 72 20 27 3f 27 2c  atch '*' or '?',
25a20 20 70 75 74 20 74 68 65 6d 20 69 6e 20 22 5b 5d   put them in "[]
25a21 22 2e 20 20 4c 69 6b 65 20 74 68 69 73 3a 0a 2a  ".  Like this:.*
25a22 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 61 62 63  *.**         abc
25a23 5b 2a 5d 78 79 7a 20 20 20 20 20 20 20 20 4d 61  [*]xyz        Ma
25a24 74 63 68 65 73 20 22 61 62 63 2a 78 79 7a 22 20  tches "abc*xyz" 
25a25 6f 6e 6c 79 0a 2a 2f 0a 73 74 61 74 69 63 20 69  only.*/.static i
25a26 6e 74 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72  nt patternCompar
25a27 65 28 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a  e(.  const u8 *z
25a28 50 61 74 74 65 72 6e 2c 20 20 20 20 20 20 20 20  Pattern,        
25a29 20 20 20 20 20 20 2f 2a 20 54 68 65 20 67 6c 6f        /* The glo
25a2a 62 20 70 61 74 74 65 72 6e 20 2a 2f 0a 20 20 63  b pattern */.  c
25a2b 6f 6e 73 74 20 75 38 20 2a 7a 53 74 72 69 6e 67  onst u8 *zString
25a2c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
25a2d 2f 2a 20 54 68 65 20 73 74 72 69 6e 67 20 74 6f  /* The string to
25a2e 20 63 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74   compare against
25a2f 20 74 68 65 20 67 6c 6f 62 20 2a 2f 0a 20 20 63   the glob */.  c
25a30 6f 6e 73 74 20 73 74 72 75 63 74 20 63 6f 6d 70  onst struct comp
25a31 61 72 65 49 6e 66 6f 20 2a 70 49 6e 66 6f 2c 20  areInfo *pInfo, 
25a32 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  /* Information a
25a33 62 6f 75 74 20 68 6f 77 20 74 6f 20 64 6f 20 74  bout how to do t
25a34 68 65 20 63 6f 6d 70 61 72 65 20 2a 2f 0a 20 20  he compare */.  
25a35 63 6f 6e 73 74 20 69 6e 74 20 65 73 63 20 20 20  const int esc   
25a36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25a37 20 2f 2a 20 54 68 65 20 65 73 63 61 70 65 20 63   /* The escape c
25a38 68 61 72 61 63 74 65 72 20 2a 2f 0a 29 7b 0a 20  haracter */.){. 
25a39 20 69 6e 74 20 63 2c 20 63 32 3b 0a 20 20 69 6e   int c, c2;.  in
25a3a 74 20 69 6e 76 65 72 74 3b 0a 20 20 69 6e 74 20  t invert;.  int 
25a3b 73 65 65 6e 3b 0a 20 20 75 38 20 6d 61 74 63 68  seen;.  u8 match
25a3c 4f 6e 65 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74  One = pInfo->mat
25a3d 63 68 4f 6e 65 3b 0a 20 20 75 38 20 6d 61 74 63  chOne;.  u8 matc
25a3e 68 41 6c 6c 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61  hAll = pInfo->ma
25a3f 74 63 68 41 6c 6c 3b 0a 20 20 75 38 20 6d 61 74  tchAll;.  u8 mat
25a40 63 68 53 65 74 20 3d 20 70 49 6e 66 6f 2d 3e 6d  chSet = pInfo->m
25a41 61 74 63 68 53 65 74 3b 0a 20 20 75 38 20 6e 6f  atchSet;.  u8 no
25a42 43 61 73 65 20 3d 20 70 49 6e 66 6f 2d 3e 6e 6f  Case = pInfo->no
25a43 43 61 73 65 3b 20 0a 20 20 69 6e 74 20 70 72 65  Case; .  int pre
25a44 76 45 73 63 61 70 65 20 3d 20 30 3b 20 20 20 20  vEscape = 0;    
25a45 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
25a46 70 72 65 76 69 6f 75 73 20 63 68 61 72 61 63 74  previous charact
25a47 65 72 20 77 61 73 20 27 65 73 63 61 70 65 27 20  er was 'escape' 
25a48 2a 2f 0a 0a 20 20 77 68 69 6c 65 28 20 28 63 20  */..  while( (c 
25a49 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
25a4a 64 28 7a 50 61 74 74 65 72 6e 2c 26 7a 50 61 74  d(zPattern,&zPat
25a4b 74 65 72 6e 29 29 21 3d 30 20 29 7b 0a 20 20 20  tern))!=0 ){.   
25a4c 20 69 66 28 20 21 70 72 65 76 45 73 63 61 70 65   if( !prevEscape
25a4d 20 26 26 20 63 3d 3d 6d 61 74 63 68 41 6c 6c 20   && c==matchAll 
25a4e 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  ){.      while( 
25a4f 28 63 3d 73 71 6c 69 74 65 33 55 74 66 38 52 65  (c=sqlite3Utf8Re
25a50 61 64 28 7a 50 61 74 74 65 72 6e 2c 26 7a 50 61  ad(zPattern,&zPa
25a51 74 74 65 72 6e 29 29 20 3d 3d 20 6d 61 74 63 68  ttern)) == match
25a52 41 6c 6c 0a 20 20 20 20 20 20 20 20 20 20 20 20  All.            
25a53 20 20 20 7c 7c 20 63 20 3d 3d 20 6d 61 74 63 68     || c == match
25a54 4f 6e 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69  One ){.        i
25a55 66 28 20 63 3d 3d 6d 61 74 63 68 4f 6e 65 20 26  f( c==matchOne &
25a56 26 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  & sqlite3Utf8Rea
25a57 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a 53 74 72  d(zString, &zStr
25a58 69 6e 67 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ing)==0 ){.     
25a59 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
25a5a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
25a5b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20  .      if( c==0 
25a5c 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
25a5d 6e 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  n 1;.      }else
25a5e 20 69 66 28 20 63 3d 3d 65 73 63 20 29 7b 0a 20   if( c==esc ){. 
25a5f 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74         c = sqlit
25a60 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74  e3Utf8Read(zPatt
25a61 65 72 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b  ern, &zPattern);
25a62 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d  .        if( c==
25a63 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  0 ){.          r
25a64 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20  eturn 0;.       
25a65 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69   }.      }else i
25a66 66 28 20 63 3d 3d 6d 61 74 63 68 53 65 74 20 29  f( c==matchSet )
25a67 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
25a68 28 20 65 73 63 3d 3d 30 20 29 3b 20 20 20 20 20  ( esc==0 );     
25a69 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 47      /* This is G
25a6a 4c 4f 42 2c 20 6e 6f 74 20 4c 49 4b 45 20 2a 2f  LOB, not LIKE */
25a6b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
25a6c 20 6d 61 74 63 68 53 65 74 3c 30 78 38 30 20 29   matchSet<0x80 )
25a6d 3b 20 20 2f 2a 20 27 5b 27 20 69 73 20 61 20 73  ;  /* '[' is a s
25a6e 69 6e 67 6c 65 2d 62 79 74 65 20 63 68 61 72 61  ingle-byte chara
25a6f 63 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 20  cter */.        
25a70 77 68 69 6c 65 28 20 2a 7a 53 74 72 69 6e 67 20  while( *zString 
25a71 26 26 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72  && patternCompar
25a72 65 28 26 7a 50 61 74 74 65 72 6e 5b 2d 31 5d 2c  e(&zPattern[-1],
25a73 7a 53 74 72 69 6e 67 2c 70 49 6e 66 6f 2c 65 73  zString,pInfo,es
25a74 63 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  c)==0 ){.       
25a75 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55     SQLITE_SKIP_U
25a76 54 46 38 28 7a 53 74 72 69 6e 67 29 3b 0a 20 20  TF8(zString);.  
25a77 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
25a78 72 65 74 75 72 6e 20 2a 7a 53 74 72 69 6e 67 21  return *zString!
25a79 3d 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  =0;.      }.    
25a7a 20 20 77 68 69 6c 65 28 20 28 63 32 20 3d 20 73    while( (c2 = s
25a7b 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
25a7c 53 74 72 69 6e 67 2c 26 7a 53 74 72 69 6e 67 29  String,&zString)
25a7d 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
25a7e 69 66 28 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20  if( noCase ){.  
25a7f 20 20 20 20 20 20 20 20 47 6c 6f 67 55 70 70 65          GlogUppe
25a80 72 54 6f 4c 6f 77 65 72 28 63 32 29 3b 0a 20 20  rToLower(c2);.  
25a81 20 20 20 20 20 20 20 20 47 6c 6f 67 55 70 70 65          GlogUppe
25a82 72 54 6f 4c 6f 77 65 72 28 63 29 3b 0a 20 20 20  rToLower(c);.   
25a83 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 32         while( c2
25a84 20 21 3d 20 30 20 26 26 20 63 32 20 21 3d 20 63   != 0 && c2 != c
25a85 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
25a86 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38  c2 = sqlite3Utf8
25a87 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a  Read(zString, &z
25a88 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 20  String);.       
25a89 20 20 20 20 20 47 6c 6f 67 55 70 70 65 72 54 6f       GlogUpperTo
25a8a 4c 6f 77 65 72 28 63 32 29 3b 0a 20 20 20 20 20  Lower(c2);.     
25a8b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
25a8c 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
25a8d 77 68 69 6c 65 28 20 63 32 20 21 3d 20 30 20 26  while( c2 != 0 &
25a8e 26 20 63 32 20 21 3d 20 63 20 29 7b 0a 20 20 20  & c2 != c ){.   
25a8f 20 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71           c2 = sq
25a90 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 53  lite3Utf8Read(zS
25a91 74 72 69 6e 67 2c 20 26 7a 53 74 72 69 6e 67 29  tring, &zString)
25a92 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
25a93 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
25a94 69 66 28 20 63 32 3d 3d 30 20 29 20 72 65 74 75  if( c2==0 ) retu
25a95 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66  rn 0;.        if
25a96 28 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65  ( patternCompare
25a97 28 7a 50 61 74 74 65 72 6e 2c 7a 53 74 72 69 6e  (zPattern,zStrin
25a98 67 2c 70 49 6e 66 6f 2c 65 73 63 29 20 29 20 72  g,pInfo,esc) ) r
25a99 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d  eturn 1;.      }
25a9a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
25a9b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21  .    }else if( !
25a9c 70 72 65 76 45 73 63 61 70 65 20 26 26 20 63 3d  prevEscape && c=
25a9d 3d 6d 61 74 63 68 4f 6e 65 20 29 7b 0a 20 20 20  =matchOne ){.   
25a9e 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 55 74     if( sqlite3Ut
25a9f 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20  f8Read(zString, 
25aa0 26 7a 53 74 72 69 6e 67 29 3d 3d 30 20 29 7b 0a  &zString)==0 ){.
25aa1 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30          return 0
25aa2 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
25aa3 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74 63 68  lse if( c==match
25aa4 53 65 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  Set ){.      int
25aa5 20 70 72 69 6f 72 5f 63 20 3d 20 30 3b 0a 20 20   prior_c = 0;.  
25aa6 20 20 20 20 61 73 73 65 72 74 28 20 65 73 63 3d      assert( esc=
25aa7 3d 30 20 29 3b 20 20 20 20 2f 2a 20 54 68 69 73  =0 );    /* This
25aa8 20 6f 6e 6c 79 20 6f 63 63 75 72 73 20 66 6f 72   only occurs for
25aa9 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c 49 4b 45 20   GLOB, not LIKE 
25aaa 2a 2f 0a 20 20 20 20 20 20 73 65 65 6e 20 3d 20  */.      seen = 
25aab 30 3b 0a 20 20 20 20 20 20 69 6e 76 65 72 74 20  0;.      invert 
25aac 3d 20 30 3b 0a 20 20 20 20 20 20 63 20 3d 20 73  = 0;.      c = s
25aad 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
25aae 53 74 72 69 6e 67 2c 20 26 7a 53 74 72 69 6e 67  String, &zString
25aaf 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d  );.      if( c==
25ab0 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
25ab1 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33      c2 = sqlite3
25ab2 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 72  Utf8Read(zPatter
25ab3 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20  n, &zPattern);. 
25ab4 20 20 20 20 20 69 66 28 20 63 32 3d 3d 27 5e 27       if( c2=='^'
25ab5 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 76 65   ){.        inve
25ab6 72 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  rt = 1;.        
25ab7 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38  c2 = sqlite3Utf8
25ab8 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20 26  Read(zPattern, &
25ab9 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 20  zPattern);.     
25aba 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 32 3d   }.      if( c2=
25abb 3d 27 5d 27 20 29 7b 0a 20 20 20 20 20 20 20 20  =']' ){.        
25abc 69 66 28 20 63 3d 3d 27 5d 27 20 29 20 73 65 65  if( c==']' ) see
25abd 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 63  n = 1;.        c
25abe 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  2 = sqlite3Utf8R
25abf 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20 26 7a  ead(zPattern, &z
25ac0 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20  Pattern);.      
25ac1 7d 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 63  }.      while( c
25ac2 32 20 26 26 20 63 32 21 3d 27 5d 27 20 29 7b 0a  2 && c2!=']' ){.
25ac3 20 20 20 20 20 20 20 20 69 66 28 20 63 32 3d 3d          if( c2==
25ac4 27 2d 27 20 26 26 20 7a 50 61 74 74 65 72 6e 5b  '-' && zPattern[
25ac5 30 5d 21 3d 27 5d 27 20 26 26 20 7a 50 61 74 74  0]!=']' && zPatt
25ac6 65 72 6e 5b 30 5d 21 3d 30 20 26 26 20 70 72 69  ern[0]!=0 && pri
25ac7 6f 72 5f 63 3e 30 20 29 7b 0a 20 20 20 20 20 20  or_c>0 ){.      
25ac8 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33      c2 = sqlite3
25ac9 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 72  Utf8Read(zPatter
25aca 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20  n, &zPattern);. 
25acb 20 20 20 20 20 20 20 20 20 69 66 28 20 63 3e 3d           if( c>=
25acc 70 72 69 6f 72 5f 63 20 26 26 20 63 3c 3d 63 32  prior_c && c<=c2
25acd 20 29 20 73 65 65 6e 20 3d 20 31 3b 0a 20 20 20   ) seen = 1;.   
25ace 20 20 20 20 20 20 20 70 72 69 6f 72 5f 63 20 3d         prior_c =
25acf 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73   0;.        }els
25ad0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  e{.          if(
25ad1 20 63 3d 3d 63 32 20 29 7b 0a 20 20 20 20 20 20   c==c2 ){.      
25ad2 20 20 20 20 20 20 73 65 65 6e 20 3d 20 31 3b 0a        seen = 1;.
25ad3 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
25ad4 20 20 20 20 20 20 70 72 69 6f 72 5f 63 20 3d 20        prior_c = 
25ad5 63 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  c2;.        }.  
25ad6 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74        c2 = sqlit
25ad7 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74  e3Utf8Read(zPatt
25ad8 65 72 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b  ern, &zPattern);
25ad9 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
25ada 66 28 20 63 32 3d 3d 30 20 7c 7c 20 28 73 65 65  f( c2==0 || (see
25adb 6e 20 5e 20 69 6e 76 65 72 74 29 3d 3d 30 20 29  n ^ invert)==0 )
25adc 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
25add 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
25ade 7d 65 6c 73 65 20 69 66 28 20 65 73 63 3d 3d 63  }else if( esc==c
25adf 20 26 26 20 21 70 72 65 76 45 73 63 61 70 65 20   && !prevEscape 
25ae0 29 7b 0a 20 20 20 20 20 20 70 72 65 76 45 73 63  ){.      prevEsc
25ae1 61 70 65 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c  ape = 1;.    }el
25ae2 73 65 7b 0a 20 20 20 20 20 20 63 32 20 3d 20 73  se{.      c2 = s
25ae3 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
25ae4 53 74 72 69 6e 67 2c 20 26 7a 53 74 72 69 6e 67  String, &zString
25ae5 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 6f 43  );.      if( noC
25ae6 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 47  ase ){.        G
25ae7 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28  logUpperToLower(
25ae8 63 29 3b 0a 20 20 20 20 20 20 20 20 47 6c 6f 67  c);.        Glog
25ae9 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 63 32 29  UpperToLower(c2)
25aea 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
25aeb 69 66 28 20 63 21 3d 63 32 20 29 7b 0a 20 20 20  if( c!=c2 ){.   
25aec 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
25aed 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 72 65       }.      pre
25aee 76 45 73 63 61 70 65 20 3d 20 30 3b 0a 20 20 20  vEscape = 0;.   
25aef 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
25af0 2a 7a 53 74 72 69 6e 67 3d 3d 30 3b 0a 7d 0a 0a  *zString==0;.}..
25af1 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 20  /*.** Count the 
25af2 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20  number of times 
25af3 74 68 61 74 20 74 68 65 20 4c 49 4b 45 20 6f 70  that the LIKE op
25af4 65 72 61 74 6f 72 20 28 6f 72 20 47 4c 4f 42 20  erator (or GLOB 
25af5 77 68 69 63 68 20 69 73 0a 2a 2a 20 6a 75 73 74  which is.** just
25af6 20 61 20 76 61 72 69 61 74 69 6f 6e 20 6f 66 20   a variation of 
25af7 4c 49 4b 45 29 20 67 65 74 73 20 63 61 6c 6c 65  LIKE) gets calle
25af8 64 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64  d.  This is used
25af9 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20   for testing.** 
25afa 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  only..*/.#ifdef 
25afb 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49  SQLITE_TEST.SQLI
25afc 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
25afd 65 33 5f 6c 69 6b 65 5f 63 6f 75 6e 74 20 3d 20  e3_like_count = 
25afe 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  0;.#endif.../*.*
25aff 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
25b00 20 6f 66 20 74 68 65 20 6c 69 6b 65 28 29 20 53   of the like() S
25b01 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  QL function.  Th
25b02 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c  is function impl
25b03 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 65 20 62 75  ements.** the bu
25b04 69 6c 64 2d 69 6e 20 4c 49 4b 45 20 6f 70 65 72  ild-in LIKE oper
25b05 61 74 6f 72 2e 20 20 54 68 65 20 66 69 72 73 74  ator.  The first
25b06 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
25b07 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 68 65   function is the
25b08 0a 2a 2a 20 70 61 74 74 65 72 6e 20 61 6e 64 20  .** pattern and 
25b09 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
25b0a 65 6e 74 20 69 73 20 74 68 65 20 73 74 72 69 6e  ent is the strin
25b0b 67 2e 20 20 53 6f 2c 20 74 68 65 20 53 51 4c 20  g.  So, the SQL 
25b0c 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a  statements:.**.*
25b0d 2a 20 20 20 20 20 20 20 41 20 4c 49 4b 45 20 42  *       A LIKE B
25b0e 0a 2a 2a 0a 2a 2a 20 69 73 20 69 6d 70 6c 65 6d  .**.** is implem
25b0f 65 6e 74 65 64 20 61 73 20 6c 69 6b 65 28 42 2c  ented as like(B,
25b10 41 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73  A)..**.** This s
25b11 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 28 77 69  ame function (wi
25b12 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 20 63  th a different c
25b13 6f 6d 70 61 72 65 49 6e 66 6f 20 73 74 72 75 63  ompareInfo struc
25b14 74 75 72 65 29 20 63 6f 6d 70 75 74 65 73 0a 2a  ture) computes.*
25b15 2a 20 74 68 65 20 47 4c 4f 42 20 6f 70 65 72 61  * the GLOB opera
25b16 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  tor..*/.static v
25b17 6f 69 64 20 6c 69 6b 65 46 75 6e 63 28 0a 20 20  oid likeFunc(.  
25b18 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
25b19 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74  *context, .  int
25b1a 20 61 72 67 63 2c 20 0a 20 20 73 71 6c 69 74 65   argc, .  sqlite
25b1b 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
25b1c 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  {.  const unsign
25b1d 65 64 20 63 68 61 72 20 2a 7a 41 2c 20 2a 7a 42  ed char *zA, *zB
25b1e 3b 0a 20 20 69 6e 74 20 65 73 63 61 70 65 20 3d  ;.  int escape =
25b1f 20 30 3b 0a 20 20 69 6e 74 20 6e 50 61 74 3b 0a   0;.  int nPat;.
25b20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
25b21 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
25b22 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78  db_handle(contex
25b23 74 29 3b 0a 0a 20 20 7a 42 20 3d 20 73 71 6c 69  t);..  zB = sqli
25b24 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
25b25 72 67 76 5b 30 5d 29 3b 0a 20 20 7a 41 20 3d 20  rgv[0]);.  zA = 
25b26 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
25b27 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 0a 20 20  xt(argv[1]);..  
25b28 2f 2a 20 4c 69 6d 69 74 20 74 68 65 20 6c 65 6e  /* Limit the len
25b29 67 74 68 20 6f 66 20 74 68 65 20 4c 49 4b 45 20  gth of the LIKE 
25b2a 6f 72 20 47 4c 4f 42 20 70 61 74 74 65 72 6e 20  or GLOB pattern 
25b2b 74 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d  to avoid problem
25b2c 73 0a 20 20 2a 2a 20 6f 66 20 64 65 65 70 20 72  s.  ** of deep r
25b2d 65 63 75 72 73 69 6f 6e 20 61 6e 64 20 4e 2a 4e  ecursion and N*N
25b2e 20 62 65 68 61 76 69 6f 72 20 69 6e 20 70 61 74   behavior in pat
25b2f 74 65 72 6e 43 6f 6d 70 61 72 65 28 29 2e 0a 20  ternCompare().. 
25b30 20 2a 2f 0a 20 20 6e 50 61 74 20 3d 20 73 71 6c   */.  nPat = sql
25b31 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
25b32 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 74 65 73  (argv[0]);.  tes
25b33 74 63 61 73 65 28 20 6e 50 61 74 3d 3d 64 62 2d  tcase( nPat==db-
25b34 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
25b35 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52  IMIT_LIKE_PATTER
25b36 4e 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 74  N_LENGTH] );.  t
25b37 65 73 74 63 61 73 65 28 20 6e 50 61 74 3d 3d 64  estcase( nPat==d
25b38 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
25b39 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54  _LIMIT_LIKE_PATT
25b3a 45 52 4e 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b  ERN_LENGTH]+1 );
25b3b 0a 20 20 69 66 28 20 6e 50 61 74 20 3e 20 64 62  .  if( nPat > db
25b3c 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
25b3d 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45  LIMIT_LIKE_PATTE
25b3e 52 4e 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20  RN_LENGTH] ){.  
25b3f 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
25b40 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
25b41 22 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61  "LIKE or GLOB pa
25b42 74 74 65 72 6e 20 74 6f 6f 20 63 6f 6d 70 6c 65  ttern too comple
25b43 78 22 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65 74  x", -1);.    ret
25b44 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  urn;.  }.  asser
25b45 74 28 20 7a 42 3d 3d 73 71 6c 69 74 65 33 5f 76  t( zB==sqlite3_v
25b46 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
25b47 5d 29 20 29 3b 20 20 2f 2a 20 45 6e 63 6f 64 69  ]) );  /* Encodi
25b48 6e 67 20 64 69 64 20 6e 6f 74 20 63 68 61 6e 67  ng did not chang
25b49 65 20 2a 2f 0a 0a 20 20 69 66 28 20 61 72 67 63  e */..  if( argc
25b4a 3d 3d 33 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  ==3 ){.    /* Th
25b4b 65 20 65 73 63 61 70 65 20 63 68 61 72 61 63 74  e escape charact
25b4c 65 72 20 73 74 72 69 6e 67 20 6d 75 73 74 20 63  er string must c
25b4d 6f 6e 73 69 73 74 20 6f 66 20 61 20 73 69 6e 67  onsist of a sing
25b4e 6c 65 20 55 54 46 2d 38 20 63 68 61 72 61 63 74  le UTF-8 charact
25b4f 65 72 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72  er..    ** Other
25b50 77 69 73 65 2c 20 72 65 74 75 72 6e 20 61 6e 20  wise, return an 
25b51 65 72 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20  error..    */.  
25b52 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
25b53 20 63 68 61 72 20 2a 7a 45 73 63 20 3d 20 73 71   char *zEsc = sq
25b54 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
25b55 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 69  (argv[2]);.    i
25b56 66 28 20 7a 45 73 63 3d 3d 30 20 29 20 72 65 74  f( zEsc==0 ) ret
25b57 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  urn;.    if( sql
25b58 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28  ite3Utf8CharLen(
25b59 28 63 68 61 72 2a 29 7a 45 73 63 2c 20 2d 31 29  (char*)zEsc, -1)
25b5a 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  !=1 ){.      sql
25b5b 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
25b5c 72 28 63 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20  r(context, .    
25b5d 20 20 20 20 20 20 22 45 53 43 41 50 45 20 65 78        "ESCAPE ex
25b5e 70 72 65 73 73 69 6f 6e 20 6d 75 73 74 20 62 65  pression must be
25b5f 20 61 20 73 69 6e 67 6c 65 20 63 68 61 72 61 63   a single charac
25b60 74 65 72 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20  ter", -1);.     
25b61 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
25b62 20 20 20 65 73 63 61 70 65 20 3d 20 73 71 6c 69     escape = sqli
25b63 74 65 33 55 74 66 38 52 65 61 64 28 7a 45 73 63  te3Utf8Read(zEsc
25b64 2c 20 26 7a 45 73 63 29 3b 0a 20 20 7d 0a 20 20  , &zEsc);.  }.  
25b65 69 66 28 20 7a 41 20 26 26 20 7a 42 20 29 7b 0a  if( zA && zB ){.
25b66 20 20 20 20 73 74 72 75 63 74 20 63 6f 6d 70 61      struct compa
25b67 72 65 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20  reInfo *pInfo = 
25b68 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74  sqlite3_user_dat
25b69 61 28 63 6f 6e 74 65 78 74 29 3b 0a 23 69 66 64  a(context);.#ifd
25b6a 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
25b6b 20 20 20 73 71 6c 69 74 65 33 5f 6c 69 6b 65 5f     sqlite3_like_
25b6c 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a  count++;.#endif.
25b6d 20 20 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33      .    sqlite3
25b6e 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74  _result_int(cont
25b6f 65 78 74 2c 20 70 61 74 74 65 72 6e 43 6f 6d 70  ext, patternComp
25b70 61 72 65 28 7a 42 2c 20 7a 41 2c 20 70 49 6e 66  are(zB, zA, pInf
25b71 6f 2c 20 65 73 63 61 70 65 29 29 3b 0a 20 20 7d  o, escape));.  }
25b72 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
25b73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
25b74 4e 55 4c 4c 49 46 28 78 2c 79 29 20 66 75 6e 63  NULLIF(x,y) func
25b75 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 73 75 6c  tion.  The resul
25b76 74 20 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a  t is the first.*
25b77 2a 20 61 72 67 75 6d 65 6e 74 20 69 66 20 74 68  * argument if th
25b78 65 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20  e arguments are 
25b79 64 69 66 66 65 72 65 6e 74 2e 20 20 54 68 65 20  different.  The 
25b7a 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 69  result is NULL i
25b7b 66 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e  f the.** argumen
25b7c 74 73 20 61 72 65 20 65 71 75 61 6c 20 74 6f 20  ts are equal to 
25b7d 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a 2f 0a 73  each other..*/.s
25b7e 74 61 74 69 63 20 76 6f 69 64 20 6e 75 6c 6c 69  tatic void nulli
25b7f 66 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  fFunc(.  sqlite3
25b80 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
25b81 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64  t,.  int NotUsed
25b82 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
25b83 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 43 6f  e **argv.){.  Co
25b84 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 73  llSeq *pColl = s
25b85 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c  qlite3GetFuncCol
25b86 6c 53 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20  lSeq(context);. 
25b87 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
25b88 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69 66  R(NotUsed);.  if
25b89 28 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70  ( sqlite3MemComp
25b8a 61 72 65 28 61 72 67 76 5b 30 5d 2c 20 61 72 67  are(argv[0], arg
25b8b 76 5b 31 5d 2c 20 70 43 6f 6c 6c 29 21 3d 30 20  v[1], pColl)!=0 
25b8c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
25b8d 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74  esult_value(cont
25b8e 65 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a 20  ext, argv[0]);. 
25b8f 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c   }.}../*.** Impl
25b90 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
25b91 65 20 73 71 6c 69 74 65 5f 76 65 72 73 69 6f 6e  e sqlite_version
25b92 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  () function.  Th
25b93 65 20 72 65 73 75 6c 74 20 69 73 20 74 68 65 20  e result is the 
25b94 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68  version.** of th
25b95 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79  e SQLite library
25b96 20 74 68 61 74 20 69 73 20 72 75 6e 6e 69 6e 67   that is running
25b97 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
25b98 20 76 65 72 73 69 6f 6e 46 75 6e 63 28 0a 20 20   versionFunc(.  
25b99 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
25b9a 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
25b9b 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74  NotUsed,.  sqlit
25b9c 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73  e3_value **NotUs
25b9d 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f  ed2.){.  UNUSED_
25b9e 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73  PARAMETER2(NotUs
25b9f 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20  ed, NotUsed2);. 
25ba0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
25ba1 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 71  text(context, sq
25ba2 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 2c 20 2d  lite3_version, -
25ba3 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  1, SQLITE_STATIC
25ba4 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  );.}../*.** Impl
25ba5 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
25ba6 65 20 73 71 6c 69 74 65 5f 73 6f 75 72 63 65 5f  e sqlite_source_
25ba7 69 64 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 54  id() function. T
25ba8 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 73  he result is a s
25ba9 74 72 69 6e 67 0a 2a 2a 20 74 68 61 74 20 69 64  tring.** that id
25baa 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 61 72  entifies the par
25bab 74 69 63 75 6c 61 72 20 76 65 72 73 69 6f 6e 20  ticular version 
25bac 6f 66 20 74 68 65 20 73 6f 75 72 63 65 20 63 6f  of the source co
25bad 64 65 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64  de used to build
25bae 0a 2a 2a 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 73  .** SQLite..*/.s
25baf 74 61 74 69 63 20 76 6f 69 64 20 73 6f 75 72 63  tatic void sourc
25bb0 65 69 64 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  eidFunc(.  sqlit
25bb1 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
25bb2 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73  ext,.  int NotUs
25bb3 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  ed,.  sqlite3_va
25bb4 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29  lue **NotUsed2.)
25bb5 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
25bb6 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e  ETER2(NotUsed, N
25bb7 6f 74 55 73 65 64 32 29 3b 0a 20 20 73 71 6c 69  otUsed2);.  sqli
25bb8 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
25bb9 63 6f 6e 74 65 78 74 2c 20 53 51 4c 49 54 45 5f  context, SQLITE_
25bba 53 4f 55 52 43 45 5f 49 44 2c 20 2d 31 2c 20 53  SOURCE_ID, -1, S
25bbb 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d  QLITE_STATIC);.}
25bbc 0a 0a 2f 2a 20 41 72 72 61 79 20 66 6f 72 20 63  ../* Array for c
25bbd 6f 6e 76 65 72 74 69 6e 67 20 66 72 6f 6d 20 68  onverting from h
25bbe 61 6c 66 2d 62 79 74 65 73 20 28 6e 79 62 62 6c  alf-bytes (nybbl
25bbf 65 73 29 20 69 6e 74 6f 20 41 53 43 49 49 20 68  es) into ASCII h
25bc0 65 78 0a 2a 2a 20 64 69 67 69 74 73 2e 20 2a 2f  ex.** digits. */
25bc1 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68  .static const ch
25bc2 61 72 20 68 65 78 64 69 67 69 74 73 5b 5d 20 3d  ar hexdigits[] =
25bc3 20 7b 0a 20 20 27 30 27 2c 20 27 31 27 2c 20 27   {.  '0', '1', '
25bc4 32 27 2c 20 27 33 27 2c 20 27 34 27 2c 20 27 35  2', '3', '4', '5
25bc5 27 2c 20 27 36 27 2c 20 27 37 27 2c 0a 20 20 27  ', '6', '7',.  '
25bc6 38 27 2c 20 27 39 27 2c 20 27 41 27 2c 20 27 42  8', '9', 'A', 'B
25bc7 27 2c 20 27 43 27 2c 20 27 44 27 2c 20 27 45 27  ', 'C', 'D', 'E'
25bc8 2c 20 27 46 27 20 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  , 'F' .};../*.**
25bc9 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 2d 20   EXPERIMENTAL - 
25bca 54 68 69 73 20 69 73 20 6e 6f 74 20 61 6e 20 6f  This is not an o
25bcb 66 66 69 63 69 61 6c 20 66 75 6e 63 74 69 6f 6e  fficial function
25bcc 2e 20 20 54 68 65 20 69 6e 74 65 72 66 61 63 65  .  The interface
25bcd 20 6d 61 79 0a 2a 2a 20 63 68 61 6e 67 65 2e 20   may.** change. 
25bce 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d   This function m
25bcf 61 79 20 64 69 73 61 70 70 65 61 72 2e 20 20 44  ay disappear.  D
25bd0 6f 20 6e 6f 74 20 77 72 69 74 65 20 63 6f 64 65  o not write code
25bd1 20 74 68 61 74 20 64 65 70 65 6e 64 73 0a 2a 2a   that depends.**
25bd2 20 6f 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f   on this functio
25bd3 6e 2e 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  n..**.** Impleme
25bd4 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 51  ntation of the Q
25bd5 55 4f 54 45 28 29 20 66 75 6e 63 74 69 6f 6e 2e  UOTE() function.
25bd6 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20    This function 
25bd7 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 0a 2a  takes a single.*
25bd8 2a 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20  * argument.  If 
25bd9 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20  the argument is 
25bda 6e 75 6d 65 72 69 63 2c 20 74 68 65 20 72 65 74  numeric, the ret
25bdb 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65  urn value is the
25bdc 20 73 61 6d 65 20 61 73 0a 2a 2a 20 74 68 65 20   same as.** the 
25bdd 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 74 68  argument.  If th
25bde 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 4e 55  e argument is NU
25bdf 4c 4c 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76  LL, the return v
25be0 61 6c 75 65 20 69 73 20 74 68 65 20 73 74 72 69  alue is the stri
25be1 6e 67 0a 2a 2a 20 22 4e 55 4c 4c 22 2e 20 20 4f  ng.** "NULL".  O
25be2 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 61 72  therwise, the ar
25be3 67 75 6d 65 6e 74 20 69 73 20 65 6e 63 6c 6f 73  gument is enclos
25be4 65 64 20 69 6e 20 73 69 6e 67 6c 65 20 71 75 6f  ed in single quo
25be5 74 65 73 20 77 69 74 68 0a 2a 2a 20 73 69 6e 67  tes with.** sing
25be6 6c 65 2d 71 75 6f 74 65 20 65 73 63 61 70 65 73  le-quote escapes
25be7 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
25be8 20 71 75 6f 74 65 46 75 6e 63 28 73 71 6c 69 74   quoteFunc(sqlit
25be9 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
25bea 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
25beb 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
25bec 72 67 76 29 7b 0a 20 20 61 73 73 65 72 74 28 20  rgv){.  assert( 
25bed 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55  argc==1 );.  UNU
25bee 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72  SED_PARAMETER(ar
25bef 67 63 29 3b 0a 20 20 73 77 69 74 63 68 28 20 73  gc);.  switch( s
25bf0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
25bf1 65 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20  e(argv[0]) ){.  
25bf2 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e    case SQLITE_IN
25bf3 54 45 47 45 52 3a 0a 20 20 20 20 63 61 73 65 20  TEGER:.    case 
25bf4 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 7b 0a  SQLITE_FLOAT: {.
25bf5 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
25bf6 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65  sult_value(conte
25bf7 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a 20 20  xt, argv[0]);.  
25bf8 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
25bf9 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
25bfa 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 63  _BLOB: {.      c
25bfb 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 30 3b 0a  har *zText = 0;.
25bfc 20 20 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74        char const
25bfd 20 2a 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65   *zBlob = sqlite
25bfe 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67  3_value_blob(arg
25bff 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 6e 74  v[0]);.      int
25c00 20 6e 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33   nBlob = sqlite3
25c01 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
25c02 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 61 73 73  v[0]);.      ass
25c03 65 72 74 28 20 7a 42 6c 6f 62 3d 3d 73 71 6c 69  ert( zBlob==sqli
25c04 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61  te3_value_blob(a
25c05 72 67 76 5b 30 5d 29 20 29 3b 20 2f 2a 20 4e 6f  rgv[0]) ); /* No
25c06 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65   encoding change
25c07 20 2a 2f 0a 20 20 20 20 20 20 7a 54 65 78 74 20   */.      zText 
25c08 3d 20 28 63 68 61 72 20 2a 29 63 6f 6e 74 65 78  = (char *)contex
25c09 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c  tMalloc(context,
25c0a 20 28 32 2a 28 69 36 34 29 6e 42 6c 6f 62 29 2b   (2*(i64)nBlob)+
25c0b 34 29 3b 20 0a 20 20 20 20 20 20 69 66 28 20 7a  4); .      if( z
25c0c 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Text ){.        
25c0d 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 66  int i;.        f
25c0e 6f 72 28 69 3d 30 3b 20 69 3c 6e 42 6c 6f 62 3b  or(i=0; i<nBlob;
25c0f 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
25c10 20 7a 54 65 78 74 5b 28 69 2a 32 29 2b 32 5d 20   zText[(i*2)+2] 
25c11 3d 20 68 65 78 64 69 67 69 74 73 5b 28 7a 42 6c  = hexdigits[(zBl
25c12 6f 62 5b 69 5d 3e 3e 34 29 26 30 78 30 46 5d 3b  ob[i]>>4)&0x0F];
25c13 0a 20 20 20 20 20 20 20 20 20 20 7a 54 65 78 74  .          zText
25c14 5b 28 69 2a 32 29 2b 33 5d 20 3d 20 68 65 78 64  [(i*2)+3] = hexd
25c15 69 67 69 74 73 5b 28 7a 42 6c 6f 62 5b 69 5d 29  igits[(zBlob[i])
25c16 26 30 78 30 46 5d 3b 0a 20 20 20 20 20 20 20 20  &0x0F];.        
25c17 7d 0a 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b  }.        zText[
25c18 28 6e 42 6c 6f 62 2a 32 29 2b 32 5d 20 3d 20 27  (nBlob*2)+2] = '
25c19 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 7a 54 65  \'';.        zTe
25c1a 78 74 5b 28 6e 42 6c 6f 62 2a 32 29 2b 33 5d 20  xt[(nBlob*2)+3] 
25c1b 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20  = '\0';.        
25c1c 7a 54 65 78 74 5b 30 5d 20 3d 20 27 58 27 3b 0a  zText[0] = 'X';.
25c1d 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 31 5d          zText[1]
25c1e 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20   = '\'';.       
25c1f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
25c20 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 54  text(context, zT
25c21 65 78 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  ext, -1, SQLITE_
25c22 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20  TRANSIENT);.    
25c23 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
25c24 28 7a 54 65 78 74 29 3b 0a 20 20 20 20 20 20 7d  (zText);.      }
25c25 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
25c26 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c    }.    case SQL
25c27 49 54 45 5f 54 45 58 54 3a 20 7b 0a 20 20 20 20  ITE_TEXT: {.    
25c28 20 20 69 6e 74 20 69 2c 6a 3b 0a 20 20 20 20 20    int i,j;.     
25c29 20 75 36 34 20 6e 3b 0a 20 20 20 20 20 20 63 6f   u64 n;.      co
25c2a 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
25c2b 72 20 2a 7a 41 72 67 20 3d 20 73 71 6c 69 74 65  r *zArg = sqlite
25c2c 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
25c2d 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 63 68 61  v[0]);.      cha
25c2e 72 20 2a 7a 3b 0a 0a 20 20 20 20 20 20 69 66 28  r *z;..      if(
25c2f 20 7a 41 72 67 3d 3d 30 20 29 20 72 65 74 75 72   zArg==0 ) retur
25c30 6e 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  n;.      for(i=0
25c31 2c 20 6e 3d 30 3b 20 7a 41 72 67 5b 69 5d 3b 20  , n=0; zArg[i]; 
25c32 69 2b 2b 29 7b 20 69 66 28 20 7a 41 72 67 5b 69  i++){ if( zArg[i
25c33 5d 3d 3d 27 5c 27 27 20 29 20 6e 2b 2b 3b 20 7d  ]=='\'' ) n++; }
25c34 0a 20 20 20 20 20 20 7a 20 3d 20 63 6f 6e 74 65  .      z = conte
25c35 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74  xtMalloc(context
25c36 2c 20 28 28 69 36 34 29 69 29 2b 28 28 69 36 34  , ((i64)i)+((i64
25c37 29 6e 29 2b 33 29 3b 0a 20 20 20 20 20 20 69 66  )n)+3);.      if
25c38 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  ( z ){.        z
25c39 5b 30 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20  [0] = '\'';.    
25c3a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 31      for(i=0, j=1
25c3b 3b 20 7a 41 72 67 5b 69 5d 3b 20 69 2b 2b 29 7b  ; zArg[i]; i++){
25c3c 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b  .          z[j++
25c3d 5d 20 3d 20 7a 41 72 67 5b 69 5d 3b 0a 20 20 20  ] = zArg[i];.   
25c3e 20 20 20 20 20 20 20 69 66 28 20 7a 41 72 67 5b         if( zArg[
25c3f 69 5d 3d 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20  i]=='\'' ){.    
25c40 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d          z[j++] =
25c41 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 20   '\'';.         
25c42 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
25c43 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 5c       z[j++] = '\
25c44 27 27 3b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 5d  '';.        z[j]
25c45 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 73 71   = 0;.        sq
25c46 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
25c47 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 6a 2c  t(context, z, j,
25c48 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
25c49 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
25c4a 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  eak;.    }.    d
25c4b 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
25c4c 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
25c4d 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
25c4e 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  0])==SQLITE_NULL
25c4f 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
25c50 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
25c51 6e 74 65 78 74 2c 20 22 4e 55 4c 4c 22 2c 20 34  ntext, "NULL", 4
25c52 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
25c53 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
25c54 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
25c55 2a 20 54 68 65 20 68 65 78 28 29 20 66 75 6e 63  * The hex() func
25c56 74 69 6f 6e 2e 20 20 49 6e 74 65 72 70 72 65 74  tion.  Interpret
25c57 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 61 73   the argument as
25c58 20 61 20 62 6c 6f 62 2e 20 20 52 65 74 75 72 6e   a blob.  Return
25c59 0a 2a 2a 20 61 20 68 65 78 61 64 65 63 69 6d 61  .** a hexadecima
25c5a 6c 20 72 65 6e 64 65 72 69 6e 67 20 61 73 20 74  l rendering as t
25c5b 65 78 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ext..*/.static v
25c5c 6f 69 64 20 68 65 78 46 75 6e 63 28 0a 20 20 73  oid hexFunc(.  s
25c5d 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
25c5e 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
25c5f 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
25c60 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
25c61 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 63 6f 6e   int i, n;.  con
25c62 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
25c63 20 2a 70 42 6c 6f 62 3b 0a 20 20 63 68 61 72 20   *pBlob;.  char 
25c64 2a 7a 48 65 78 2c 20 2a 7a 3b 0a 20 20 61 73 73  *zHex, *z;.  ass
25c65 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a  ert( argc==1 );.
25c66 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
25c67 45 52 28 61 72 67 63 29 3b 0a 20 20 70 42 6c 6f  ER(argc);.  pBlo
25c68 62 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  b = sqlite3_valu
25c69 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b  e_blob(argv[0]);
25c6a 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76  .  n = sqlite3_v
25c6b 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
25c6c 30 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0]);.  assert( p
25c6d 42 6c 6f 62 3d 3d 73 71 6c 69 74 65 33 5f 76 61  Blob==sqlite3_va
25c6e 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d  lue_blob(argv[0]
25c6f 29 20 29 3b 20 20 2f 2a 20 4e 6f 20 65 6e 63 6f  ) );  /* No enco
25c70 64 69 6e 67 20 63 68 61 6e 67 65 20 2a 2f 0a 20  ding change */. 
25c71 20 7a 20 3d 20 7a 48 65 78 20 3d 20 63 6f 6e 74   z = zHex = cont
25c72 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78  extMalloc(contex
25c73 74 2c 20 28 28 69 36 34 29 6e 29 2a 32 20 2b 20  t, ((i64)n)*2 + 
25c74 31 29 3b 0a 20 20 69 66 28 20 7a 48 65 78 20 29  1);.  if( zHex )
25c75 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  {.    for(i=0; i
25c76 3c 6e 3b 20 69 2b 2b 2c 20 70 42 6c 6f 62 2b 2b  <n; i++, pBlob++
25c77 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65  ){.      unsigne
25c78 64 20 63 68 61 72 20 63 20 3d 20 2a 70 42 6c 6f  d char c = *pBlo
25c79 62 3b 0a 20 20 20 20 20 20 2a 28 7a 2b 2b 29 20  b;.      *(z++) 
25c7a 3d 20 68 65 78 64 69 67 69 74 73 5b 28 63 3e 3e  = hexdigits[(c>>
25c7b 34 29 26 30 78 66 5d 3b 0a 20 20 20 20 20 20 2a  4)&0xf];.      *
25c7c 28 7a 2b 2b 29 20 3d 20 68 65 78 64 69 67 69 74  (z++) = hexdigit
25c7d 73 5b 63 26 30 78 66 5d 3b 0a 20 20 20 20 7d 0a  s[c&0xf];.    }.
25c7e 20 20 20 20 2a 7a 20 3d 20 30 3b 0a 20 20 20 20      *z = 0;.    
25c7f 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
25c80 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 48 65  ext(context, zHe
25c81 78 2c 20 6e 2a 32 2c 20 73 71 6c 69 74 65 33 5f  x, n*2, sqlite3_
25c82 66 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  free);.  }.}../*
25c83 0a 2a 2a 20 54 68 65 20 7a 65 72 6f 62 6c 6f 62  .** The zeroblob
25c84 28 4e 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  (N) function ret
25c85 75 72 6e 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c  urns a zero-fill
25c86 65 64 20 62 6c 6f 62 20 6f 66 20 73 69 7a 65 20  ed blob of size 
25c87 4e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74  N bytes..*/.stat
25c88 69 63 20 76 6f 69 64 20 7a 65 72 6f 62 6c 6f 62  ic void zeroblob
25c89 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
25c8a 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
25c8b 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
25c8c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
25c8d 61 72 67 76 0a 29 7b 0a 20 20 69 36 34 20 6e 3b  argv.){.  i64 n;
25c8e 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
25c8f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
25c90 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65  _db_handle(conte
25c91 78 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61  xt);.  assert( a
25c92 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53  rgc==1 );.  UNUS
25c93 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67  ED_PARAMETER(arg
25c94 63 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65  c);.  n = sqlite
25c95 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72  3_value_int64(ar
25c96 67 76 5b 30 5d 29 3b 0a 20 20 74 65 73 74 63 61  gv[0]);.  testca
25c97 73 65 28 20 6e 3d 3d 64 62 2d 3e 61 4c 69 6d 69  se( n==db->aLimi
25c98 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
25c99 45 4e 47 54 48 5d 20 29 3b 0a 20 20 74 65 73 74  ENGTH] );.  test
25c9a 63 61 73 65 28 20 6e 3d 3d 64 62 2d 3e 61 4c 69  case( n==db->aLi
25c9b 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
25c9c 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b 0a 20 20  _LENGTH]+1 );.  
25c9d 69 66 28 20 6e 3e 64 62 2d 3e 61 4c 69 6d 69 74  if( n>db->aLimit
25c9e 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45  [SQLITE_LIMIT_LE
25c9f 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 73 71 6c  NGTH] ){.    sql
25ca0 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
25ca1 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74  r_toobig(context
25ca2 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
25ca3 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a  sqlite3_result_z
25ca4 65 72 6f 62 6c 6f 62 28 63 6f 6e 74 65 78 74 2c  eroblob(context,
25ca5 20 28 69 6e 74 29 6e 29 3b 0a 20 20 7d 0a 7d 0a   (int)n);.  }.}.
25ca6 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 70 6c 61  ./*.** The repla
25ca7 63 65 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20  ce() function.  
25ca8 54 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 20  Three arguments 
25ca9 61 72 65 20 61 6c 6c 20 73 74 72 69 6e 67 73 3a  are all strings:
25caa 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 6d 20 41 2c   call.** them A,
25cab 20 42 2c 20 61 6e 64 20 43 2e 20 54 68 65 20 72   B, and C. The r
25cac 65 73 75 6c 74 20 69 73 20 61 6c 73 6f 20 61 20  esult is also a 
25cad 73 74 72 69 6e 67 20 77 68 69 63 68 20 69 73 20  string which is 
25cae 64 65 72 69 76 65 64 0a 2a 2a 20 66 72 6f 6d 20  derived.** from 
25caf 41 20 62 79 20 72 65 70 6c 61 63 69 6e 67 20 65  A by replacing e
25cb0 76 65 72 79 20 6f 63 63 75 72 61 6e 63 65 20 6f  very occurance o
25cb1 66 20 42 20 77 69 74 68 20 43 2e 20 20 54 68 65  f B with C.  The
25cb2 20 6d 61 74 63 68 0a 2a 2a 20 6d 75 73 74 20 62   match.** must b
25cb3 65 20 65 78 61 63 74 2e 20 20 43 6f 6c 6c 61 74  e exact.  Collat
25cb4 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 61 72  ing sequences ar
25cb5 65 20 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 73  e not used..*/.s
25cb6 74 61 74 69 63 20 76 6f 69 64 20 72 65 70 6c 61  tatic void repla
25cb7 63 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  ceFunc(.  sqlite
25cb8 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
25cb9 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
25cba 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
25cbb 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73  **argv.){.  cons
25cbc 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
25cbd 2a 7a 53 74 72 3b 20 20 20 20 20 20 20 20 2f 2a  *zStr;        /*
25cbe 20 54 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e   The input strin
25cbf 67 20 41 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75  g A */.  const u
25cc0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 50  nsigned char *zP
25cc1 61 74 74 65 72 6e 3b 20 20 20 20 2f 2a 20 54 68  attern;    /* Th
25cc2 65 20 70 61 74 74 65 72 6e 20 73 74 72 69 6e 67  e pattern string
25cc3 20 42 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e   B */.  const un
25cc4 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 52 65  signed char *zRe
25cc5 70 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  p;        /* The
25cc6 20 72 65 70 6c 61 63 65 6d 65 6e 74 20 73 74 72   replacement str
25cc7 69 6e 67 20 43 20 2a 2f 0a 20 20 75 6e 73 69 67  ing C */.  unsig
25cc8 6e 65 64 20 63 68 61 72 20 2a 7a 4f 75 74 3b 20  ned char *zOut; 
25cc9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
25cca 54 68 65 20 6f 75 74 70 75 74 20 2a 2f 0a 20 20  The output */.  
25ccb 69 6e 74 20 6e 53 74 72 3b 20 20 20 20 20 20 20  int nStr;       
25ccc 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
25ccd 20 6f 66 20 7a 53 74 72 20 2a 2f 0a 20 20 69 6e   of zStr */.  in
25cce 74 20 6e 50 61 74 74 65 72 6e 3b 20 20 20 20 20  t nPattern;     
25ccf 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
25cd0 66 20 7a 50 61 74 74 65 72 6e 20 2a 2f 0a 20 20  f zPattern */.  
25cd1 69 6e 74 20 6e 52 65 70 3b 20 20 20 20 20 20 20  int nRep;       
25cd2 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
25cd3 20 6f 66 20 7a 52 65 70 20 2a 2f 0a 20 20 69 36   of zRep */.  i6
25cd4 34 20 6e 4f 75 74 3b 20 20 20 20 20 20 20 20 20  4 nOut;         
25cd5 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
25cd6 6d 20 73 69 7a 65 20 6f 66 20 7a 4f 75 74 20 2a  m size of zOut *
25cd7 2f 0a 20 20 69 6e 74 20 6c 6f 6f 70 4c 69 6d 69  /.  int loopLimi
25cd8 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t;           /* 
25cd9 4c 61 73 74 20 7a 53 74 72 5b 5d 20 74 68 61 74  Last zStr[] that
25cda 20 6d 69 67 68 74 20 6d 61 74 63 68 20 7a 50 61   might match zPa
25cdb 74 74 65 72 6e 5b 5d 20 2a 2f 0a 20 20 69 6e 74  ttern[] */.  int
25cdc 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20   i, j;          
25cdd 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
25cde 75 6e 74 65 72 73 20 2a 2f 0a 0a 20 20 61 73 73  unters */..  ass
25cdf 65 72 74 28 20 61 72 67 63 3d 3d 33 20 29 3b 0a  ert( argc==3 );.
25ce0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
25ce1 45 52 28 61 72 67 63 29 3b 0a 20 20 7a 53 74 72  ER(argc);.  zStr
25ce2 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
25ce3 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
25ce4 20 20 69 66 28 20 7a 53 74 72 3d 3d 30 20 29 20    if( zStr==0 ) 
25ce5 72 65 74 75 72 6e 3b 0a 20 20 6e 53 74 72 20 3d  return;.  nStr =
25ce6 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
25ce7 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20  ytes(argv[0]);. 
25ce8 20 61 73 73 65 72 74 28 20 7a 53 74 72 3d 3d 73   assert( zStr==s
25ce9 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
25cea 74 28 61 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f  t(argv[0]) );  /
25ceb 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68  * No encoding ch
25cec 61 6e 67 65 20 2a 2f 0a 20 20 7a 50 61 74 74 65  ange */.  zPatte
25ced 72 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  rn = sqlite3_val
25cee 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29  ue_text(argv[1])
25cef 3b 0a 20 20 69 66 28 20 7a 50 61 74 74 65 72 6e  ;.  if( zPattern
25cf0 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ==0 ){.    asser
25cf1 74 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  t( sqlite3_value
25cf2 5f 74 79 70 65 28 61 72 67 76 5b 31 5d 29 3d 3d  _type(argv[1])==
25cf3 53 51 4c 49 54 45 5f 4e 55 4c 4c 0a 20 20 20 20  SQLITE_NULL.    
25cf4 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74          || sqlit
25cf5 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
25cf6 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 2d 3e 6d  ndle(context)->m
25cf7 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
25cf8 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
25cf9 20 69 66 28 20 7a 50 61 74 74 65 72 6e 5b 30 5d   if( zPattern[0]
25cfa 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ==0 ){.    asser
25cfb 74 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  t( sqlite3_value
25cfc 5f 74 79 70 65 28 61 72 67 76 5b 31 5d 29 21 3d  _type(argv[1])!=
25cfd 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 3b 0a 20  SQLITE_NULL );. 
25cfe 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
25cff 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c  t_value(context,
25d00 20 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 72   argv[0]);.    r
25d01 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6e 50 61  eturn;.  }.  nPa
25d02 74 74 65 72 6e 20 3d 20 73 71 6c 69 74 65 33 5f  ttern = sqlite3_
25d03 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
25d04 5b 31 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20  [1]);.  assert( 
25d05 7a 50 61 74 74 65 72 6e 3d 3d 73 71 6c 69 74 65  zPattern==sqlite
25d06 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
25d07 76 5b 31 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f 20  v[1]) );  /* No 
25d08 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 20  encoding change 
25d09 2a 2f 0a 20 20 7a 52 65 70 20 3d 20 73 71 6c 69  */.  zRep = sqli
25d0a 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
25d0b 72 67 76 5b 32 5d 29 3b 0a 20 20 69 66 28 20 7a  rgv[2]);.  if( z
25d0c 52 65 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  Rep==0 ) return;
25d0d 0a 20 20 6e 52 65 70 20 3d 20 73 71 6c 69 74 65  .  nRep = sqlite
25d0e 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
25d0f 67 76 5b 32 5d 29 3b 0a 20 20 61 73 73 65 72 74  gv[2]);.  assert
25d10 28 20 7a 52 65 70 3d 3d 73 71 6c 69 74 65 33 5f  ( zRep==sqlite3_
25d11 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
25d12 32 5d 29 20 29 3b 0a 20 20 6e 4f 75 74 20 3d 20  2]) );.  nOut = 
25d13 6e 53 74 72 20 2b 20 31 3b 0a 20 20 61 73 73 65  nStr + 1;.  asse
25d14 72 74 28 20 6e 4f 75 74 3c 53 51 4c 49 54 45 5f  rt( nOut<SQLITE_
25d15 4d 41 58 5f 4c 45 4e 47 54 48 20 29 3b 0a 20 20  MAX_LENGTH );.  
25d16 7a 4f 75 74 20 3d 20 63 6f 6e 74 65 78 74 4d 61  zOut = contextMa
25d17 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 69  lloc(context, (i
25d18 36 34 29 6e 4f 75 74 29 3b 0a 20 20 69 66 28 20  64)nOut);.  if( 
25d19 7a 4f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72  zOut==0 ){.    r
25d1a 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6c 6f 6f  eturn;.  }.  loo
25d1b 70 4c 69 6d 69 74 20 3d 20 6e 53 74 72 20 2d 20  pLimit = nStr - 
25d1c 6e 50 61 74 74 65 72 6e 3b 20 20 0a 20 20 66 6f  nPattern;  .  fo
25d1d 72 28 69 3d 6a 3d 30 3b 20 69 3c 3d 6c 6f 6f 70  r(i=j=0; i<=loop
25d1e 4c 69 6d 69 74 3b 20 69 2b 2b 29 7b 0a 20 20 20  Limit; i++){.   
25d1f 20 69 66 28 20 7a 53 74 72 5b 69 5d 21 3d 7a 50   if( zStr[i]!=zP
25d20 61 74 74 65 72 6e 5b 30 5d 20 7c 7c 20 6d 65 6d  attern[0] || mem
25d21 63 6d 70 28 26 7a 53 74 72 5b 69 5d 2c 20 7a 50  cmp(&zStr[i], zP
25d22 61 74 74 65 72 6e 2c 20 6e 50 61 74 74 65 72 6e  attern, nPattern
25d23 29 20 29 7b 0a 20 20 20 20 20 20 7a 4f 75 74 5b  ) ){.      zOut[
25d24 6a 2b 2b 5d 20 3d 20 7a 53 74 72 5b 69 5d 3b 0a  j++] = zStr[i];.
25d25 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
25d26 20 75 38 20 2a 7a 4f 6c 64 3b 0a 20 20 20 20 20   u8 *zOld;.     
25d27 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
25d28 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
25d29 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
25d2a 29 3b 0a 20 20 20 20 20 20 6e 4f 75 74 20 2b 3d  );.      nOut +=
25d2b 20 6e 52 65 70 20 2d 20 6e 50 61 74 74 65 72 6e   nRep - nPattern
25d2c 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
25d2d 28 20 6e 4f 75 74 2d 31 3d 3d 64 62 2d 3e 61 4c  ( nOut-1==db->aL
25d2e 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
25d2f 54 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 20  T_LENGTH] );.   
25d30 20 20 20 74 65 73 74 63 61 73 65 28 20 6e 4f 75     testcase( nOu
25d31 74 2d 32 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b  t-2==db->aLimit[
25d32 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e  SQLITE_LIMIT_LEN
25d33 47 54 48 5d 20 29 3b 0a 20 20 20 20 20 20 69 66  GTH] );.      if
25d34 28 20 6e 4f 75 74 2d 31 3e 64 62 2d 3e 61 4c 69  ( nOut-1>db->aLi
25d35 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
25d36 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20  _LENGTH] ){.    
25d37 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
25d38 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28  lt_error_toobig(
25d39 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  context);.      
25d3a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
25d3b 64 62 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 20 20  db, zOut);.     
25d3c 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20     return;.     
25d3d 20 7d 0a 20 20 20 20 20 20 7a 4f 6c 64 20 3d 20   }.      zOld = 
25d3e 7a 4f 75 74 3b 0a 20 20 20 20 20 20 7a 4f 75 74  zOut;.      zOut
25d3f 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c   = sqlite3_reall
25d40 6f 63 28 7a 4f 75 74 2c 20 28 69 6e 74 29 6e 4f  oc(zOut, (int)nO
25d41 75 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a  ut);.      if( z
25d42 4f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Out==0 ){.      
25d43 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
25d44 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e  _error_nomem(con
25d45 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 73  text);.        s
25d46 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
25d47 20 7a 4f 6c 64 29 3b 0a 20 20 20 20 20 20 20 20   zOld);.        
25d48 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
25d49 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 4f        memcpy(&zO
25d4a 75 74 5b 6a 5d 2c 20 7a 52 65 70 2c 20 6e 52 65  ut[j], zRep, nRe
25d4b 70 29 3b 0a 20 20 20 20 20 20 6a 20 2b 3d 20 6e  p);.      j += n
25d4c 52 65 70 3b 0a 20 20 20 20 20 20 69 20 2b 3d 20  Rep;.      i += 
25d4d 6e 50 61 74 74 65 72 6e 2d 31 3b 0a 20 20 20 20  nPattern-1;.    
25d4e 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  }.  }.  assert( 
25d4f 6a 2b 6e 53 74 72 2d 69 2b 31 3d 3d 6e 4f 75 74  j+nStr-i+1==nOut
25d50 20 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26 7a 4f   );.  memcpy(&zO
25d51 75 74 5b 6a 5d 2c 20 26 7a 53 74 72 5b 69 5d 2c  ut[j], &zStr[i],
25d52 20 6e 53 74 72 2d 69 29 3b 0a 20 20 6a 20 2b 3d   nStr-i);.  j +=
25d53 20 6e 53 74 72 20 2d 20 69 3b 0a 20 20 61 73 73   nStr - i;.  ass
25d54 65 72 74 28 20 6a 3c 3d 6e 4f 75 74 20 29 3b 0a  ert( j<=nOut );.
25d55 20 20 7a 4f 75 74 5b 6a 5d 20 3d 20 30 3b 0a 20    zOut[j] = 0;. 
25d56 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
25d57 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 63  text(context, (c
25d58 68 61 72 2a 29 7a 4f 75 74 2c 20 6a 2c 20 73 71  har*)zOut, j, sq
25d59 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 7d 0a 0a  lite3_free);.}..
25d5a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
25d5b 74 69 6f 6e 20 6f 66 20 74 68 65 20 54 52 49 4d  tion of the TRIM
25d5c 28 29 2c 20 4c 54 52 49 4d 28 29 2c 20 61 6e 64  (), LTRIM(), and
25d5d 20 52 54 52 49 4d 28 29 20 66 75 6e 63 74 69 6f   RTRIM() functio
25d5e 6e 73 2e 0a 2a 2a 20 54 68 65 20 75 73 65 72 64  ns..** The userd
25d5f 61 74 61 20 69 73 20 30 78 31 20 66 6f 72 20 6c  ata is 0x1 for l
25d60 65 66 74 20 74 72 69 6d 2c 20 30 78 32 20 66 6f  eft trim, 0x2 fo
25d61 72 20 72 69 67 68 74 20 74 72 69 6d 2c 20 30 78  r right trim, 0x
25d62 33 20 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73  3 for both..*/.s
25d63 74 61 74 69 63 20 76 6f 69 64 20 74 72 69 6d 46  tatic void trimF
25d64 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
25d65 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
25d66 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
25d67 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
25d68 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75  rgv.){.  const u
25d69 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49  nsigned char *zI
25d6a 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  n;         /* In
25d6b 70 75 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  put string */.  
25d6c 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
25d6d 68 61 72 20 2a 7a 43 68 61 72 53 65 74 3b 20 20  har *zCharSet;  
25d6e 20 20 2f 2a 20 53 65 74 20 6f 66 20 63 68 61 72    /* Set of char
25d6f 61 63 74 65 72 73 20 74 6f 20 74 72 69 6d 20 2a  acters to trim *
25d70 2f 0a 20 20 69 6e 74 20 6e 49 6e 3b 20 20 20 20  /.  int nIn;    
25d71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25d72 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
25d73 6f 66 20 62 79 74 65 73 20 69 6e 20 69 6e 70 75  of bytes in inpu
25d74 74 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  t */.  int flags
25d75 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
25d76 20 20 20 20 20 20 20 20 20 2f 2a 20 31 3a 20 74           /* 1: t
25d77 72 69 6d 6c 65 66 74 20 20 32 3a 20 74 72 69 6d  rimleft  2: trim
25d78 72 69 67 68 74 20 20 33 3a 20 74 72 69 6d 20 2a  right  3: trim *
25d79 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  /.  int i;      
25d7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25d7b 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
25d7c 75 6e 74 65 72 20 2a 2f 0a 20 20 75 6e 73 69 67  unter */.  unsig
25d7d 6e 65 64 20 63 68 61 72 20 2a 61 4c 65 6e 20 3d  ned char *aLen =
25d7e 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20   0;          /* 
25d7f 4c 65 6e 67 74 68 20 6f 66 20 65 61 63 68 20 63  Length of each c
25d80 68 61 72 61 63 74 65 72 20 69 6e 20 7a 43 68 61  haracter in zCha
25d81 72 53 65 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  rSet */.  unsign
25d82 65 64 20 63 68 61 72 20 2a 2a 61 7a 43 68 61 72  ed char **azChar
25d83 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 49   = 0;       /* I
25d84 6e 64 69 76 69 64 75 61 6c 20 63 68 61 72 61 63  ndividual charac
25d85 74 65 72 73 20 69 6e 20 7a 43 68 61 72 53 65 74  ters in zCharSet
25d86 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 72 3b   */.  int nChar;
25d87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25d88 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
25d89 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  r of characters 
25d8a 69 6e 20 7a 43 68 61 72 53 65 74 20 2a 2f 0a 0a  in zCharSet */..
25d8b 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61    if( sqlite3_va
25d8c 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
25d8d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29  )==SQLITE_NULL )
25d8e 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
25d8f 7d 0a 20 20 7a 49 6e 20 3d 20 73 71 6c 69 74 65  }.  zIn = sqlite
25d90 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
25d91 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 7a 49 6e  v[0]);.  if( zIn
25d92 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
25d93 6e 49 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  nIn = sqlite3_va
25d94 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30  lue_bytes(argv[0
25d95 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 49  ]);.  assert( zI
25d96 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  n==sqlite3_value
25d97 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29  _text(argv[0]) )
25d98 3b 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 31 20  ;.  if( argc==1 
25d99 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f  ){.    static co
25d9a 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
25d9b 72 20 6c 65 6e 4f 6e 65 5b 5d 20 3d 20 7b 20 31  r lenOne[] = { 1
25d9c 20 7d 3b 0a 20 20 20 20 73 74 61 74 69 63 20 75   };.    static u
25d9d 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 20 63  nsigned char * c
25d9e 6f 6e 73 74 20 61 7a 4f 6e 65 5b 5d 20 3d 20 7b  onst azOne[] = {
25d9f 20 28 75 38 2a 29 22 20 22 20 7d 3b 0a 20 20 20   (u8*)" " };.   
25da0 20 6e 43 68 61 72 20 3d 20 31 3b 0a 20 20 20 20   nChar = 1;.    
25da1 61 4c 65 6e 20 3d 20 28 75 38 2a 29 6c 65 6e 4f  aLen = (u8*)lenO
25da2 6e 65 3b 0a 20 20 20 20 61 7a 43 68 61 72 20 3d  ne;.    azChar =
25da3 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
25da4 2a 2a 29 61 7a 4f 6e 65 3b 0a 20 20 20 20 7a 43  **)azOne;.    zC
25da5 68 61 72 53 65 74 20 3d 20 30 3b 0a 20 20 7d 65  harSet = 0;.  }e
25da6 6c 73 65 20 69 66 28 20 28 7a 43 68 61 72 53 65  lse if( (zCharSe
25da7 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  t = sqlite3_valu
25da8 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 29  e_text(argv[1]))
25da9 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
25daa 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  n;.  }else{.    
25dab 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
25dac 68 61 72 20 2a 7a 3b 0a 20 20 20 20 66 6f 72 28  har *z;.    for(
25dad 7a 3d 7a 43 68 61 72 53 65 74 2c 20 6e 43 68 61  z=zCharSet, nCha
25dae 72 3d 30 3b 20 2a 7a 3b 20 6e 43 68 61 72 2b 2b  r=0; *z; nChar++
25daf 29 7b 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f  ){.      SQLITE_
25db0 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20  SKIP_UTF8(z);.  
25db1 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 43 68 61    }.    if( nCha
25db2 72 3e 30 20 29 7b 0a 20 20 20 20 20 20 61 7a 43  r>0 ){.      azC
25db3 68 61 72 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c  har = contextMal
25db4 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69  loc(context, ((i
25db5 36 34 29 6e 43 68 61 72 29 2a 28 73 69 7a 65 6f  64)nChar)*(sizeo
25db6 66 28 63 68 61 72 2a 29 2b 31 29 29 3b 0a 20 20  f(char*)+1));.  
25db7 20 20 20 20 69 66 28 20 61 7a 43 68 61 72 3d 3d      if( azChar==
25db8 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  0 ){.        ret
25db9 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
25dba 20 20 20 61 4c 65 6e 20 3d 20 28 75 6e 73 69 67     aLen = (unsig
25dbb 6e 65 64 20 63 68 61 72 2a 29 26 61 7a 43 68 61  ned char*)&azCha
25dbc 72 5b 6e 43 68 61 72 5d 3b 0a 20 20 20 20 20 20  r[nChar];.      
25dbd 66 6f 72 28 7a 3d 7a 43 68 61 72 53 65 74 2c 20  for(z=zCharSet, 
25dbe 6e 43 68 61 72 3d 30 3b 20 2a 7a 3b 20 6e 43 68  nChar=0; *z; nCh
25dbf 61 72 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61  ar++){.        a
25dc0 7a 43 68 61 72 5b 6e 43 68 61 72 5d 20 3d 20 28  zChar[nChar] = (
25dc1 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
25dc2 7a 3b 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54  z;.        SQLIT
25dc3 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a  E_SKIP_UTF8(z);.
25dc4 20 20 20 20 20 20 20 20 61 4c 65 6e 5b 6e 43 68          aLen[nCh
25dc5 61 72 5d 20 3d 20 28 75 38 29 28 7a 20 2d 20 61  ar] = (u8)(z - a
25dc6 7a 43 68 61 72 5b 6e 43 68 61 72 5d 29 3b 0a 20  zChar[nChar]);. 
25dc7 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
25dc8 0a 20 20 69 66 28 20 6e 43 68 61 72 3e 30 20 29  .  if( nChar>0 )
25dc9 7b 0a 20 20 20 20 66 6c 61 67 73 20 3d 20 53 51  {.    flags = SQ
25dca 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28  LITE_PTR_TO_INT(
25dcb 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74  sqlite3_user_dat
25dcc 61 28 63 6f 6e 74 65 78 74 29 29 3b 0a 20 20 20  a(context));.   
25dcd 20 69 66 28 20 66 6c 61 67 73 20 26 20 31 20 29   if( flags & 1 )
25dce 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 6e  {.      while( n
25dcf 49 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  In>0 ){.        
25dd0 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20  int len = 0;.   
25dd1 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
25dd2 6e 43 68 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nChar; i++){.   
25dd3 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 61 4c 65         len = aLe
25dd4 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  n[i];.          
25dd5 69 66 28 20 6c 65 6e 3c 3d 6e 49 6e 20 26 26 20  if( len<=nIn && 
25dd6 6d 65 6d 63 6d 70 28 7a 49 6e 2c 20 61 7a 43 68  memcmp(zIn, azCh
25dd7 61 72 5b 69 5d 2c 20 6c 65 6e 29 3d 3d 30 20 29  ar[i], len)==0 )
25dd8 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
25dd9 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 3e  }.        if( i>
25dda 3d 6e 43 68 61 72 20 29 20 62 72 65 61 6b 3b 0a  =nChar ) break;.
25ddb 20 20 20 20 20 20 20 20 7a 49 6e 20 2b 3d 20 6c          zIn += l
25ddc 65 6e 3b 0a 20 20 20 20 20 20 20 20 6e 49 6e 20  en;.        nIn 
25ddd 2d 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 7d 0a  -= len;.      }.
25dde 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 66 6c      }.    if( fl
25ddf 61 67 73 20 26 20 32 20 29 7b 0a 20 20 20 20 20  ags & 2 ){.     
25de0 20 77 68 69 6c 65 28 20 6e 49 6e 3e 30 20 29 7b   while( nIn>0 ){
25de1 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6c 65 6e  .        int len
25de2 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66 6f   = 0;.        fo
25de3 72 28 69 3d 30 3b 20 69 3c 6e 43 68 61 72 3b 20  r(i=0; i<nChar; 
25de4 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
25de5 6c 65 6e 20 3d 20 61 4c 65 6e 5b 69 5d 3b 0a 20  len = aLen[i];. 
25de6 20 20 20 20 20 20 20 20 20 69 66 28 20 6c 65 6e           if( len
25de7 3c 3d 6e 49 6e 20 26 26 20 6d 65 6d 63 6d 70 28  <=nIn && memcmp(
25de8 26 7a 49 6e 5b 6e 49 6e 2d 6c 65 6e 5d 2c 61 7a  &zIn[nIn-len],az
25de9 43 68 61 72 5b 69 5d 2c 6c 65 6e 29 3d 3d 30 20  Char[i],len)==0 
25dea 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ) break;.       
25deb 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 69   }.        if( i
25dec 3e 3d 6e 43 68 61 72 20 29 20 62 72 65 61 6b 3b  >=nChar ) break;
25ded 0a 20 20 20 20 20 20 20 20 6e 49 6e 20 2d 3d 20  .        nIn -= 
25dee 6c 65 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  len;.      }.   
25def 20 7d 0a 20 20 20 20 69 66 28 20 7a 43 68 61 72   }.    if( zChar
25df0 53 65 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Set ){.      sql
25df1 69 74 65 33 5f 66 72 65 65 28 61 7a 43 68 61 72  ite3_free(azChar
25df2 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  );.    }.  }.  s
25df3 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
25df4 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61  xt(context, (cha
25df5 72 2a 29 7a 49 6e 2c 20 6e 49 6e 2c 20 53 51 4c  r*)zIn, nIn, SQL
25df6 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
25df7 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  }...#ifdef SQLIT
25df8 45 5f 53 4f 55 4e 44 45 58 0a 2f 2a 0a 2a 2a 20  E_SOUNDEX./*.** 
25df9 43 6f 6d 70 75 74 65 20 74 68 65 20 73 6f 75 6e  Compute the soun
25dfa 64 65 78 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20  dex encoding of 
25dfb 61 20 77 6f 72 64 2e 0a 2a 2f 0a 73 74 61 74 69  a word..*/.stati
25dfc 63 20 76 6f 69 64 20 73 6f 75 6e 64 65 78 46 75  c void soundexFu
25dfd 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
25dfe 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
25dff 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
25e00 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
25e01 67 76 0a 29 7b 0a 20 20 63 68 61 72 20 7a 52 65  gv.){.  char zRe
25e02 73 75 6c 74 5b 38 5d 3b 0a 20 20 63 6f 6e 73 74  sult[8];.  const
25e03 20 75 38 20 2a 7a 49 6e 3b 0a 20 20 69 6e 74 20   u8 *zIn;.  int 
25e04 69 2c 20 6a 3b 0a 20 20 73 74 61 74 69 63 20 63  i, j;.  static c
25e05 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
25e06 61 72 20 69 43 6f 64 65 5b 5d 20 3d 20 7b 0a 20  ar iCode[] = {. 
25e07 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30     0, 0, 0, 0, 0
25e08 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
25e09 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
25e0a 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20  0,.    0, 0, 0, 
25e0b 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
25e0c 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
25e0d 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c   0, 0,.    0, 0,
25e0e 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
25e0f 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
25e10 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30  , 0, 0, 0,.    0
25e11 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
25e12 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
25e13 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20  0, 0, 0, 0, 0,. 
25e14 20 20 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20 33     0, 0, 1, 2, 3
25e15 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20 30 2c  , 0, 1, 2, 0, 0,
25e16 20 32 2c 20 32 2c 20 34 2c 20 35 2c 20 35 2c 20   2, 2, 4, 5, 5, 
25e17 30 2c 0a 20 20 20 20 31 2c 20 32 2c 20 36 2c 20  0,.    1, 2, 6, 
25e18 32 2c 20 33 2c 20 30 2c 20 31 2c 20 30 2c 20 32  2, 3, 0, 1, 0, 2
25e19 2c 20 30 2c 20 32 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 2, 0, 0, 0,
25e1a 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c   0, 0,.    0, 0,
25e1b 20 31 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20   1, 2, 3, 0, 1, 
25e1c 32 2c 20 30 2c 20 30 2c 20 32 2c 20 32 2c 20 34  2, 0, 0, 2, 2, 4
25e1d 2c 20 35 2c 20 35 2c 20 30 2c 0a 20 20 20 20 31  , 5, 5, 0,.    1
25e1e 2c 20 32 2c 20 36 2c 20 32 2c 20 33 2c 20 30 2c  , 2, 6, 2, 3, 0,
25e1f 20 31 2c 20 30 2c 20 32 2c 20 30 2c 20 32 2c 20   1, 0, 2, 0, 2, 
25e20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20  0, 0, 0, 0, 0,. 
25e21 20 7d 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72   };.  assert( ar
25e22 67 63 3d 3d 31 20 29 3b 0a 20 20 7a 49 6e 20 3d  gc==1 );.  zIn =
25e23 20 28 75 38 2a 29 73 71 6c 69 74 65 33 5f 76 61   (u8*)sqlite3_va
25e24 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
25e25 29 3b 0a 20 20 69 66 28 20 7a 49 6e 3d 3d 30 20  );.  if( zIn==0 
25e26 29 20 7a 49 6e 20 3d 20 28 75 38 2a 29 22 22 3b  ) zIn = (u8*)"";
25e27 0a 20 20 66 6f 72 28 69 3d 30 3b 20 7a 49 6e 5b  .  for(i=0; zIn[
25e28 69 5d 20 26 26 20 21 73 71 6c 69 74 65 33 49 73  i] && !sqlite3Is
25e29 61 6c 70 68 61 28 7a 49 6e 5b 69 5d 29 3b 20 69  alpha(zIn[i]); i
25e2a 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 7a 49 6e 5b  ++){}.  if( zIn[
25e2b 69 5d 20 29 7b 0a 20 20 20 20 75 38 20 70 72 65  i] ){.    u8 pre
25e2c 76 63 6f 64 65 20 3d 20 69 43 6f 64 65 5b 7a 49  vcode = iCode[zI
25e2d 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a 20 20 20 20  n[i]&0x7f];.    
25e2e 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20 73 71 6c  zResult[0] = sql
25e2f 69 74 65 33 54 6f 75 70 70 65 72 28 7a 49 6e 5b  ite3Toupper(zIn[
25e30 69 5d 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 31  i]);.    for(j=1
25e31 3b 20 6a 3c 34 20 26 26 20 7a 49 6e 5b 69 5d 3b  ; j<4 && zIn[i];
25e32 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74   i++){.      int
25e33 20 63 6f 64 65 20 3d 20 69 43 6f 64 65 5b 7a 49   code = iCode[zI
25e34 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a 20 20 20 20  n[i]&0x7f];.    
25e35 20 20 69 66 28 20 63 6f 64 65 3e 30 20 29 7b 0a    if( code>0 ){.
25e36 20 20 20 20 20 20 20 20 69 66 28 20 63 6f 64 65          if( code
25e37 21 3d 70 72 65 76 63 6f 64 65 20 29 7b 0a 20 20  !=prevcode ){.  
25e38 20 20 20 20 20 20 20 20 70 72 65 76 63 6f 64 65          prevcode
25e39 20 3d 20 63 6f 64 65 3b 0a 20 20 20 20 20 20 20   = code;.       
25e3a 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b 5d 20     zResult[j++] 
25e3b 3d 20 63 6f 64 65 20 2b 20 27 30 27 3b 0a 20 20  = code + '0';.  
25e3c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
25e3d 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 72 65  lse{.        pre
25e3e 76 63 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20 20  vcode = 0;.     
25e3f 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 69   }.    }.    whi
25e40 6c 65 28 20 6a 3c 34 20 29 7b 0a 20 20 20 20 20  le( j<4 ){.     
25e41 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b 5d 20 3d 20   zResult[j++] = 
25e42 27 30 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a  '0';.    }.    z
25e43 52 65 73 75 6c 74 5b 6a 5d 20 3d 20 30 3b 0a 20  Result[j] = 0;. 
25e44 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
25e45 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
25e46 7a 52 65 73 75 6c 74 2c 20 34 2c 20 53 51 4c 49  zResult, 4, SQLI
25e47 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
25e48 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
25e49 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
25e4a 63 6f 6e 74 65 78 74 2c 20 22 3f 30 30 30 22 2c  context, "?000",
25e4b 20 34 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49   4, SQLITE_STATI
25e4c 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66  C);.  }.}.#endif
25e4d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
25e4e 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e  _OMIT_LOAD_EXTEN
25e4f 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 41 20 66 75 6e  SION./*.** A fun
25e50 63 74 69 6f 6e 20 74 68 61 74 20 6c 6f 61 64 73  ction that loads
25e51 20 61 20 73 68 61 72 65 64 2d 6c 69 62 72 61 72   a shared-librar
25e52 79 20 65 78 74 65 6e 73 69 6f 6e 20 74 68 65 6e  y extension then
25e53 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a   returns NULL..*
25e54 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f  /.static void lo
25e55 61 64 45 78 74 28 73 71 6c 69 74 65 33 5f 63 6f  adExt(sqlite3_co
25e56 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20  ntext *context, 
25e57 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
25e58 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b  3_value **argv){
25e59 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
25e5a 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68  File = (const ch
25e5b 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
25e5c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
25e5d 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
25e5e 7a 50 72 6f 63 3b 0a 20 20 73 71 6c 69 74 65 33  zProc;.  sqlite3
25e5f 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63   *db = sqlite3_c
25e60 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
25e61 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 63 68 61  (context);.  cha
25e62 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a  r *zErrMsg = 0;.
25e63 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 32 20 29  .  if( argc==2 )
25e64 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d 20 28 63  {.    zProc = (c
25e65 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69  onst char *)sqli
25e66 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
25e67 72 67 76 5b 31 5d 29 3b 0a 20 20 7d 65 6c 73 65  rgv[1]);.  }else
25e68 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d 20 30 3b  {.    zProc = 0;
25e69 0a 20 20 7d 0a 20 20 69 66 28 20 7a 46 69 6c 65  .  }.  if( zFile
25e6a 20 26 26 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64   && sqlite3_load
25e6b 5f 65 78 74 65 6e 73 69 6f 6e 28 64 62 2c 20 7a  _extension(db, z
25e6c 46 69 6c 65 2c 20 7a 50 72 6f 63 2c 20 26 7a 45  File, zProc, &zE
25e6d 72 72 4d 73 67 29 20 29 7b 0a 20 20 20 20 73 71  rrMsg) ){.    sq
25e6e 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
25e6f 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72  or(context, zErr
25e70 4d 73 67 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71  Msg, -1);.    sq
25e71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72 72 4d  lite3_free(zErrM
25e72 73 67 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69  sg);.  }.}.#endi
25e73 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  f.../*.** An ins
25e74 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
25e75 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
25e76 20 68 6f 6c 64 73 20 74 68 65 20 63 6f 6e 74 65   holds the conte
25e77 78 74 20 6f 66 20 61 0a 2a 2a 20 73 75 6d 28 29  xt of a.** sum()
25e78 20 6f 72 20 61 76 67 28 29 20 61 67 67 72 65 67   or avg() aggreg
25e79 61 74 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e 2e  ate computation.
25e7a 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
25e7b 63 74 20 53 75 6d 43 74 78 20 53 75 6d 43 74 78  ct SumCtx SumCtx
25e7c 3b 0a 73 74 72 75 63 74 20 53 75 6d 43 74 78 20  ;.struct SumCtx 
25e7d 7b 0a 20 20 64 6f 75 62 6c 65 20 72 53 75 6d 3b  {.  double rSum;
25e7e 20 20 20 20 20 20 2f 2a 20 46 6c 6f 61 74 69 6e        /* Floatin
25e7f 67 20 70 6f 69 6e 74 20 73 75 6d 20 2a 2f 0a 20  g point sum */. 
25e80 20 69 36 34 20 69 53 75 6d 3b 20 20 20 20 20 20   i64 iSum;      
25e81 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 73 75     /* Integer su
25e82 6d 20 2a 2f 20 20 20 0a 20 20 69 36 34 20 63 6e  m */   .  i64 cn
25e83 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  t;          /* N
25e84 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74  umber of element
25e85 73 20 73 75 6d 6d 65 64 20 2a 2f 0a 20 20 75 38  s summed */.  u8
25e86 20 6f 76 65 72 66 6c 6f 77 3b 20 20 20 20 20 20   overflow;      
25e87 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 74 65 67  /* True if integ
25e88 65 72 20 6f 76 65 72 66 6c 6f 77 20 73 65 65 6e  er overflow seen
25e89 20 2a 2f 0a 20 20 75 38 20 61 70 70 72 6f 78 3b   */.  u8 approx;
25e8a 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
25e8b 69 66 20 6e 6f 6e 2d 69 6e 74 65 67 65 72 20 76  if non-integer v
25e8c 61 6c 75 65 20 77 61 73 20 69 6e 70 75 74 20 74  alue was input t
25e8d 6f 20 74 68 65 20 73 75 6d 20 2a 2f 0a 7d 3b 0a  o the sum */.};.
25e8e 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20  ./*.** Routines 
25e8f 75 73 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20  used to compute 
25e90 74 68 65 20 73 75 6d 2c 20 61 76 65 72 61 67 65  the sum, average
25e91 2c 20 61 6e 64 20 74 6f 74 61 6c 2e 0a 2a 2a 0a  , and total..**.
25e92 2a 2a 20 54 68 65 20 53 55 4d 28 29 20 66 75 6e  ** The SUM() fun
25e93 63 74 69 6f 6e 20 66 6f 6c 6c 6f 77 73 20 74 68  ction follows th
25e94 65 20 28 62 72 6f 6b 65 6e 29 20 53 51 4c 20 73  e (broken) SQL s
25e95 74 61 6e 64 61 72 64 20 77 68 69 63 68 20 6d 65  tandard which me
25e96 61 6e 73 0a 2a 2a 20 74 68 61 74 20 69 74 20 72  ans.** that it r
25e97 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 66 20 69  eturns NULL if i
25e98 74 20 73 75 6d 73 20 6f 76 65 72 20 6e 6f 20 69  t sums over no i
25e99 6e 70 75 74 73 2e 20 20 54 4f 54 41 4c 20 72 65  nputs.  TOTAL re
25e9a 74 75 72 6e 73 0a 2a 2a 20 30 2e 30 20 69 6e 20  turns.** 0.0 in 
25e9b 74 68 61 74 20 63 61 73 65 2e 20 20 49 6e 20 61  that case.  In a
25e9c 64 64 69 74 69 6f 6e 2c 20 54 4f 54 41 4c 20 61  ddition, TOTAL a
25e9d 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 61 20  lways returns a 
25e9e 66 6c 6f 61 74 20 77 68 65 72 65 0a 2a 2a 20 53  float where.** S
25e9f 55 4d 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20  UM might return 
25ea0 61 6e 20 69 6e 74 65 67 65 72 20 69 66 20 69 74  an integer if it
25ea1 20 6e 65 76 65 72 20 65 6e 63 6f 75 6e 74 65 72   never encounter
25ea2 73 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  s a floating poi
25ea3 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 54 4f  nt.** value.  TO
25ea4 54 41 4c 20 6e 65 76 65 72 20 66 61 69 6c 73 2c  TAL never fails,
25ea5 20 62 75 74 20 53 55 4d 20 6d 69 67 68 74 20 74   but SUM might t
25ea6 68 72 6f 75 67 68 20 61 6e 20 65 78 63 65 70 74  hrough an except
25ea7 69 6f 6e 20 69 66 0a 2a 2a 20 69 74 20 6f 76 65  ion if.** it ove
25ea8 72 66 6c 6f 77 73 20 61 6e 20 69 6e 74 65 67 65  rflows an intege
25ea9 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  r..*/.static voi
25eaa 64 20 73 75 6d 53 74 65 70 28 73 71 6c 69 74 65  d sumStep(sqlite
25eab 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
25eac 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
25ead 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
25eae 67 76 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70  gv){.  SumCtx *p
25eaf 3b 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20  ;.  int type;.  
25eb0 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20  assert( argc==1 
25eb1 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
25eb2 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 70  METER(argc);.  p
25eb3 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65   = sqlite3_aggre
25eb4 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e  gate_context(con
25eb5 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 29  text, sizeof(*p)
25eb6 29 3b 0a 20 20 74 79 70 65 20 3d 20 73 71 6c 69  );.  type = sqli
25eb7 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69  te3_value_numeri
25eb8 63 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3b  c_type(argv[0]);
25eb9 0a 20 20 69 66 28 20 70 20 26 26 20 74 79 70 65  .  if( p && type
25eba 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b  !=SQLITE_NULL ){
25ebb 0a 20 20 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20  .    p->cnt++;. 
25ebc 20 20 20 69 66 28 20 74 79 70 65 3d 3d 53 51 4c     if( type==SQL
25ebd 49 54 45 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20  ITE_INTEGER ){. 
25ebe 20 20 20 20 20 69 36 34 20 76 20 3d 20 73 71 6c       i64 v = sql
25ebf 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34  ite3_value_int64
25ec0 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20  (argv[0]);.     
25ec1 20 70 2d 3e 72 53 75 6d 20 2b 3d 20 76 3b 0a 20   p->rSum += v;. 
25ec2 20 20 20 20 20 69 66 28 20 28 70 2d 3e 61 70 70       if( (p->app
25ec3 72 6f 78 7c 70 2d 3e 6f 76 65 72 66 6c 6f 77 29  rox|p->overflow)
25ec4 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ==0 ){.        i
25ec5 36 34 20 69 4e 65 77 53 75 6d 20 3d 20 70 2d 3e  64 iNewSum = p->
25ec6 69 53 75 6d 20 2b 20 76 3b 0a 20 20 20 20 20 20  iSum + v;.      
25ec7 20 20 69 6e 74 20 73 31 20 3d 20 28 69 6e 74 29    int s1 = (int)
25ec8 28 70 2d 3e 69 53 75 6d 20 3e 3e 20 28 73 69 7a  (p->iSum >> (siz
25ec9 65 6f 66 28 69 36 34 29 2a 38 2d 31 29 29 3b 0a  eof(i64)*8-1));.
25eca 20 20 20 20 20 20 20 20 69 6e 74 20 73 32 20 3d          int s2 =
25ecb 20 28 69 6e 74 29 28 76 20 20 20 20 20 20 20 3e   (int)(v       >
25ecc 3e 20 28 73 69 7a 65 6f 66 28 69 36 34 29 2a 38  > (sizeof(i64)*8
25ecd 2d 31 29 29 3b 0a 20 20 20 20 20 20 20 20 69 6e  -1));.        in
25ece 74 20 73 33 20 3d 20 28 69 6e 74 29 28 69 4e 65  t s3 = (int)(iNe
25ecf 77 53 75 6d 20 3e 3e 20 28 73 69 7a 65 6f 66 28  wSum >> (sizeof(
25ed0 69 36 34 29 2a 38 2d 31 29 29 3b 0a 20 20 20 20  i64)*8-1));.    
25ed1 20 20 20 20 70 2d 3e 6f 76 65 72 66 6c 6f 77 20      p->overflow 
25ed2 3d 20 28 28 73 31 26 73 32 26 7e 73 33 29 20 7c  = ((s1&s2&~s3) |
25ed3 20 28 7e 73 31 26 7e 73 32 26 73 33 29 29 3f 31   (~s1&~s2&s3))?1
25ed4 3a 30 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69  :0;.        p->i
25ed5 53 75 6d 20 3d 20 69 4e 65 77 53 75 6d 3b 0a 20  Sum = iNewSum;. 
25ed6 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
25ed7 7b 0a 20 20 20 20 20 20 70 2d 3e 72 53 75 6d 20  {.      p->rSum 
25ed8 2b 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  += sqlite3_value
25ed9 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29  _double(argv[0])
25eda 3b 0a 20 20 20 20 20 20 70 2d 3e 61 70 70 72 6f  ;.      p->appro
25edb 78 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  x = 1;.    }.  }
25edc 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  .}.static void s
25edd 75 6d 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74  umFinalize(sqlit
25ede 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
25edf 65 78 74 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a  ext){.  SumCtx *
25ee0 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33  p;.  p = sqlite3
25ee1 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
25ee2 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a  xt(context, 0);.
25ee3 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 63 6e    if( p && p->cn
25ee4 74 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70  t>0 ){.    if( p
25ee5 2d 3e 6f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20  ->overflow ){.  
25ee6 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
25ee7 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74  lt_error(context
25ee8 2c 22 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c  ,"integer overfl
25ee9 6f 77 22 2c 2d 31 29 3b 0a 20 20 20 20 7d 65 6c  ow",-1);.    }el
25eea 73 65 20 69 66 28 20 70 2d 3e 61 70 70 72 6f 78  se if( p->approx
25eeb 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
25eec 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28  3_result_double(
25eed 63 6f 6e 74 65 78 74 2c 20 70 2d 3e 72 53 75 6d  context, p->rSum
25eee 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
25eef 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
25ef0 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74  lt_int64(context
25ef1 2c 20 70 2d 3e 69 53 75 6d 29 3b 0a 20 20 20 20  , p->iSum);.    
25ef2 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76  }.  }.}.static v
25ef3 6f 69 64 20 61 76 67 46 69 6e 61 6c 69 7a 65 28  oid avgFinalize(
25ef4 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
25ef5 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75 6d  *context){.  Sum
25ef6 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71  Ctx *p;.  p = sq
25ef7 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f  lite3_aggregate_
25ef8 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  context(context,
25ef9 20 30 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20   0);.  if( p && 
25efa 70 2d 3e 63 6e 74 3e 30 20 29 7b 0a 20 20 20 20  p->cnt>0 ){.    
25efb 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64  sqlite3_result_d
25efc 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70  ouble(context, p
25efd 2d 3e 72 53 75 6d 2f 28 64 6f 75 62 6c 65 29 70  ->rSum/(double)p
25efe 2d 3e 63 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 73 74  ->cnt);.  }.}.st
25eff 61 74 69 63 20 76 6f 69 64 20 74 6f 74 61 6c 46  atic void totalF
25f00 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f  inalize(sqlite3_
25f01 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
25f02 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a  ){.  SumCtx *p;.
25f03 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67    p = sqlite3_ag
25f04 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28  gregate_context(
25f05 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 2f  context, 0);.  /
25f06 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63  * (double)0 In c
25f07 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d  ase of SQLITE_OM
25f08 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
25f09 54 2e 2e 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  T... */.  sqlite
25f0a 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28  3_result_double(
25f0b 63 6f 6e 74 65 78 74 2c 20 70 20 3f 20 70 2d 3e  context, p ? p->
25f0c 72 53 75 6d 20 3a 20 28 64 6f 75 62 6c 65 29 30  rSum : (double)0
25f0d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  );.}../*.** The 
25f0e 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
25f0f 75 72 65 20 6b 65 65 70 73 20 74 72 61 63 6b 20  ure keeps track 
25f10 6f 66 20 73 74 61 74 65 20 69 6e 66 6f 72 6d 61  of state informa
25f11 74 69 6f 6e 20 66 6f 72 20 74 68 65 0a 2a 2a 20  tion for the.** 
25f12 63 6f 75 6e 74 28 29 20 61 67 67 72 65 67 61 74  count() aggregat
25f13 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 74  e function..*/.t
25f14 79 70 65 64 65 66 20 73 74 72 75 63 74 20 43 6f  ypedef struct Co
25f15 75 6e 74 43 74 78 20 43 6f 75 6e 74 43 74 78 3b  untCtx CountCtx;
25f16 0a 73 74 72 75 63 74 20 43 6f 75 6e 74 43 74 78  .struct CountCtx
25f17 20 7b 0a 20 20 69 36 34 20 6e 3b 0a 7d 3b 0a 0a   {.  i64 n;.};..
25f18 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74  /*.** Routines t
25f19 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
25f1a 63 6f 75 6e 74 28 29 20 61 67 67 72 65 67 61 74  count() aggregat
25f1b 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  e function..*/.s
25f1c 74 61 74 69 63 20 76 6f 69 64 20 63 6f 75 6e 74  tatic void count
25f1d 53 74 65 70 28 73 71 6c 69 74 65 33 5f 63 6f 6e  Step(sqlite3_con
25f1e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69  text *context, i
25f1f 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
25f20 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a  _value **argv){.
25f21 20 20 43 6f 75 6e 74 43 74 78 20 2a 70 3b 0a 20    CountCtx *p;. 
25f22 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67   p = sqlite3_agg
25f23 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63  regate_context(c
25f24 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a  ontext, sizeof(*
25f25 70 29 29 3b 0a 20 20 69 66 28 20 28 61 72 67 63  p));.  if( (argc
25f26 3d 3d 30 20 7c 7c 20 53 51 4c 49 54 45 5f 4e 55  ==0 || SQLITE_NU
25f27 4c 4c 21 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75  LL!=sqlite3_valu
25f28 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 29  e_type(argv[0]))
25f29 20 26 26 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e   && p ){.    p->
25f2a 6e 2b 2b 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65  n++;.  }..#ifnde
25f2b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
25f2c 50 52 45 43 41 54 45 44 0a 20 20 2f 2a 20 54 68  PRECATED.  /* Th
25f2d 65 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67  e sqlite3_aggreg
25f2e 61 74 65 5f 63 6f 75 6e 74 28 29 20 66 75 6e 63  ate_count() func
25f2f 74 69 6f 6e 20 69 73 20 64 65 70 72 65 63 61 74  tion is deprecat
25f30 65 64 2e 20 20 42 75 74 20 6a 75 73 74 20 74 6f  ed.  But just to
25f31 20 6d 61 6b 65 0a 20 20 2a 2a 20 73 75 72 65 20   make.  ** sure 
25f32 69 74 20 73 74 69 6c 6c 20 6f 70 65 72 61 74 65  it still operate
25f33 73 20 63 6f 72 72 65 63 74 6c 79 2c 20 76 65 72  s correctly, ver
25f34 69 66 79 20 74 68 61 74 20 69 74 73 20 63 6f 75  ify that its cou
25f35 6e 74 20 61 67 72 65 65 73 20 77 69 74 68 20 6f  nt agrees with o
25f36 75 72 20 0a 20 20 2a 2a 20 69 6e 74 65 72 6e 61  ur .  ** interna
25f37 6c 20 63 6f 75 6e 74 20 77 68 65 6e 20 75 73 69  l count when usi
25f38 6e 67 20 63 6f 75 6e 74 28 2a 29 20 61 6e 64 20  ng count(*) and 
25f39 77 68 65 6e 20 74 68 65 20 74 6f 74 61 6c 20 63  when the total c
25f3a 6f 75 6e 74 20 63 61 6e 20 62 65 0a 20 20 2a 2a  ount can be.  **
25f3b 20 65 78 70 72 65 73 73 65 64 20 61 73 20 61 20   expressed as a 
25f3c 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 20  32-bit integer. 
25f3d 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 61 72 67  */.  assert( arg
25f3e 63 3d 3d 31 20 7c 7c 20 70 3d 3d 30 20 7c 7c 20  c==1 || p==0 || 
25f3f 70 2d 3e 6e 3e 30 78 37 66 66 66 66 66 66 66 0a  p->n>0x7fffffff.
25f40 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e            || p->
25f41 6e 3d 3d 73 71 6c 69 74 65 33 5f 61 67 67 72 65  n==sqlite3_aggre
25f42 67 61 74 65 5f 63 6f 75 6e 74 28 63 6f 6e 74 65  gate_count(conte
25f43 78 74 29 20 29 3b 0a 23 65 6e 64 69 66 0a 7d 20  xt) );.#endif.} 
25f44 20 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63    .static void c
25f45 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 28 73 71 6c  ountFinalize(sql
25f46 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
25f47 6e 74 65 78 74 29 7b 0a 20 20 43 6f 75 6e 74 43  ntext){.  CountC
25f48 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c  tx *p;.  p = sql
25f49 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
25f4a 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  ontext(context, 
25f4b 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  0);.  sqlite3_re
25f4c 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65  sult_int64(conte
25f4d 78 74 2c 20 70 20 3f 20 70 2d 3e 6e 20 3a 20 30  xt, p ? p->n : 0
25f4e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74  );.}../*.** Rout
25f4f 69 6e 65 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  ines to implemen
25f50 74 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 28  t min() and max(
25f51 29 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  ) aggregate func
25f52 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  tions..*/.static
25f53 20 76 6f 69 64 20 6d 69 6e 6d 61 78 53 74 65 70   void minmaxStep
25f54 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
25f55 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20  ext *context, . 
25f56 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 20 0a 20   int NotUsed, . 
25f57 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
25f58 2a 61 72 67 76 0a 29 7b 0a 20 20 4d 65 6d 20 2a  *argv.){.  Mem *
25f59 70 41 72 67 20 20 3d 20 28 4d 65 6d 20 2a 29 61  pArg  = (Mem *)a
25f5a 72 67 76 5b 30 5d 3b 0a 20 20 4d 65 6d 20 2a 70  rgv[0];.  Mem *p
25f5b 42 65 73 74 3b 0a 20 20 55 4e 55 53 45 44 5f 50  Best;.  UNUSED_P
25f5c 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
25f5d 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65  );..  if( sqlite
25f5e 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
25f5f 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55  v[0])==SQLITE_NU
25f60 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70  LL ) return;.  p
25f61 42 65 73 74 20 3d 20 28 4d 65 6d 20 2a 29 73 71  Best = (Mem *)sq
25f62 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f  lite3_aggregate_
25f63 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  context(context,
25f64 20 73 69 7a 65 6f 66 28 2a 70 42 65 73 74 29 29   sizeof(*pBest))
25f65 3b 0a 20 20 69 66 28 20 21 70 42 65 73 74 20 29  ;.  if( !pBest )
25f66 20 72 65 74 75 72 6e 3b 0a 0a 20 20 69 66 28 20   return;..  if( 
25f67 70 42 65 73 74 2d 3e 66 6c 61 67 73 20 29 7b 0a  pBest->flags ){.
25f68 20 20 20 20 69 6e 74 20 6d 61 78 3b 0a 20 20 20      int max;.   
25f69 20 69 6e 74 20 63 6d 70 3b 0a 20 20 20 20 43 6f   int cmp;.    Co
25f6a 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 73  llSeq *pColl = s
25f6b 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c  qlite3GetFuncCol
25f6c 6c 53 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20  lSeq(context);. 
25f6d 20 20 20 2f 2a 20 54 68 69 73 20 73 74 65 70 20     /* This step 
25f6e 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
25f6f 20 66 6f 72 20 62 6f 74 68 20 74 68 65 20 6d 69   for both the mi
25f70 6e 28 29 20 61 6e 64 20 6d 61 78 28 29 20 61 67  n() and max() ag
25f71 67 72 65 67 61 74 65 73 2c 0a 20 20 20 20 2a 2a  gregates,.    **
25f72 20 74 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72   the only differ
25f73 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 65  ence between the
25f74 20 74 77 6f 20 62 65 69 6e 67 20 74 68 61 74 20   two being that 
25f75 74 68 65 20 73 65 6e 73 65 20 6f 66 20 74 68 65  the sense of the
25f76 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73  .    ** comparis
25f77 6f 6e 20 69 73 20 69 6e 76 65 72 74 65 64 2e 20  on is inverted. 
25f78 46 6f 72 20 74 68 65 20 6d 61 78 28 29 20 61 67  For the max() ag
25f79 67 72 65 67 61 74 65 2c 20 74 68 65 0a 20 20 20  gregate, the.   
25f7a 20 2a 2a 20 73 71 6c 69 74 65 33 5f 75 73 65 72   ** sqlite3_user
25f7b 5f 64 61 74 61 28 29 20 66 75 6e 63 74 69 6f 6e  _data() function
25f7c 20 72 65 74 75 72 6e 73 20 28 76 6f 69 64 20 2a   returns (void *
25f7d 29 2d 31 2e 20 46 6f 72 20 6d 69 6e 28 29 20 69  )-1. For min() i
25f7e 74 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 73  t.    ** returns
25f7f 20 28 76 6f 69 64 20 2a 29 64 62 2c 20 77 68 65   (void *)db, whe
25f80 72 65 20 64 62 20 69 73 20 74 68 65 20 73 71 6c  re db is the sql
25f81 69 74 65 33 2a 20 64 61 74 61 62 61 73 65 20 70  ite3* database p
25f82 6f 69 6e 74 65 72 2e 0a 20 20 20 20 2a 2a 20 54  ointer..    ** T
25f83 68 65 72 65 66 6f 72 65 20 74 68 65 20 6e 65 78  herefore the nex
25f84 74 20 73 74 61 74 65 6d 65 6e 74 20 73 65 74 73  t statement sets
25f85 20 76 61 72 69 61 62 6c 65 20 27 6d 61 78 27 20   variable 'max' 
25f86 74 6f 20 31 20 66 6f 72 20 74 68 65 20 6d 61 78  to 1 for the max
25f87 28 29 0a 20 20 20 20 2a 2a 20 61 67 67 72 65 67  ().    ** aggreg
25f88 61 74 65 2c 20 6f 72 20 30 20 66 6f 72 20 6d 69  ate, or 0 for mi
25f89 6e 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  n()..    */.    
25f8a 6d 61 78 20 3d 20 73 71 6c 69 74 65 33 5f 75 73  max = sqlite3_us
25f8b 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29  er_data(context)
25f8c 21 3d 30 3b 0a 20 20 20 20 63 6d 70 20 3d 20 73  !=0;.    cmp = s
25f8d 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65  qlite3MemCompare
25f8e 28 70 42 65 73 74 2c 20 70 41 72 67 2c 20 70 43  (pBest, pArg, pC
25f8f 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 28 6d  oll);.    if( (m
25f90 61 78 20 26 26 20 63 6d 70 3c 30 29 20 7c 7c 20  ax && cmp<0) || 
25f91 28 21 6d 61 78 20 26 26 20 63 6d 70 3e 30 29 20  (!max && cmp>0) 
25f92 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
25f93 56 64 62 65 4d 65 6d 43 6f 70 79 28 70 42 65 73  VdbeMemCopy(pBes
25f94 74 2c 20 70 41 72 67 29 3b 0a 20 20 20 20 7d 0a  t, pArg);.    }.
25f95 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
25f96 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28  ite3VdbeMemCopy(
25f97 70 42 65 73 74 2c 20 70 41 72 67 29 3b 0a 20 20  pBest, pArg);.  
25f98 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  }.}.static void 
25f99 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 28 73  minMaxFinalize(s
25f9a 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
25f9b 63 6f 6e 74 65 78 74 29 7b 0a 20 20 73 71 6c 69  context){.  sqli
25f9c 74 65 33 5f 76 61 6c 75 65 20 2a 70 52 65 73 3b  te3_value *pRes;
25f9d 0a 20 20 70 52 65 73 20 3d 20 28 73 71 6c 69 74  .  pRes = (sqlit
25f9e 65 33 5f 76 61 6c 75 65 20 2a 29 73 71 6c 69 74  e3_value *)sqlit
25f9f 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
25fa0 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29  text(context, 0)
25fa1 3b 0a 20 20 69 66 28 20 70 52 65 73 20 29 7b 0a  ;.  if( pRes ){.
25fa2 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70      if( ALWAYS(p
25fa3 52 65 73 2d 3e 66 6c 61 67 73 29 20 29 7b 0a 20  Res->flags) ){. 
25fa4 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
25fa5 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78  ult_value(contex
25fa6 74 2c 20 70 52 65 73 29 3b 0a 20 20 20 20 7d 0a  t, pRes);.    }.
25fa7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
25fa8 65 6d 52 65 6c 65 61 73 65 28 70 52 65 73 29 3b  emRelease(pRes);
25fa9 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 67 72  .  }.}../*.** gr
25faa 6f 75 70 5f 63 6f 6e 63 61 74 28 45 58 50 52 2c  oup_concat(EXPR,
25fab 20 3f 53 45 50 41 52 41 54 4f 52 3f 29 0a 2a 2f   ?SEPARATOR?).*/
25fac 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67 72 6f  .static void gro
25fad 75 70 43 6f 6e 63 61 74 53 74 65 70 28 0a 20 20  upConcatStep(.  
25fae 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
25faf 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
25fb0 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
25fb1 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
25fb2 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56    const char *zV
25fb3 61 6c 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 2a  al;.  StrAccum *
25fb4 70 41 63 63 75 6d 3b 0a 20 20 63 6f 6e 73 74 20  pAccum;.  const 
25fb5 63 68 61 72 20 2a 7a 53 65 70 3b 0a 20 20 69 6e  char *zSep;.  in
25fb6 74 20 6e 56 61 6c 2c 20 6e 53 65 70 3b 0a 20 20  t nVal, nSep;.  
25fb7 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20  assert( argc==1 
25fb8 7c 7c 20 61 72 67 63 3d 3d 32 20 29 3b 0a 20 20  || argc==2 );.  
25fb9 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  if( sqlite3_valu
25fba 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d  e_type(argv[0])=
25fbb 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72  =SQLITE_NULL ) r
25fbc 65 74 75 72 6e 3b 0a 20 20 70 41 63 63 75 6d 20  eturn;.  pAccum 
25fbd 3d 20 28 53 74 72 41 63 63 75 6d 2a 29 73 71 6c  = (StrAccum*)sql
25fbe 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
25fbf 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  ontext(context, 
25fc0 73 69 7a 65 6f 66 28 2a 70 41 63 63 75 6d 29 29  sizeof(*pAccum))
25fc1 3b 0a 0a 20 20 69 66 28 20 70 41 63 63 75 6d 20  ;..  if( pAccum 
25fc2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a  ){.    sqlite3 *
25fc3 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  db = sqlite3_con
25fc4 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63  text_db_handle(c
25fc5 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 69 6e 74  ontext);.    int
25fc6 20 66 69 72 73 74 54 65 72 6d 20 3d 20 70 41 63   firstTerm = pAc
25fc7 63 75 6d 2d 3e 75 73 65 4d 61 6c 6c 6f 63 3d 3d  cum->useMalloc==
25fc8 30 3b 0a 20 20 20 20 70 41 63 63 75 6d 2d 3e 75  0;.    pAccum->u
25fc9 73 65 4d 61 6c 6c 6f 63 20 3d 20 31 3b 0a 20 20  seMalloc = 1;.  
25fca 20 20 70 41 63 63 75 6d 2d 3e 6d 78 41 6c 6c 6f    pAccum->mxAllo
25fcb 63 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  c = db->aLimit[S
25fcc 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
25fcd 54 48 5d 3b 0a 20 20 20 20 69 66 28 20 21 66 69  TH];.    if( !fi
25fce 72 73 74 54 65 72 6d 20 29 7b 0a 20 20 20 20 20  rstTerm ){.     
25fcf 20 69 66 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a   if( argc==2 ){.
25fd0 20 20 20 20 20 20 20 20 7a 53 65 70 20 3d 20 28          zSep = (
25fd1 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61  char*)sqlite3_va
25fd2 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d  lue_text(argv[1]
25fd3 29 3b 0a 20 20 20 20 20 20 20 20 6e 53 65 70 20  );.        nSep 
25fd4 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
25fd5 62 79 74 65 73 28 61 72 67 76 5b 31 5d 29 3b 0a  bytes(argv[1]);.
25fd6 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
25fd7 20 20 20 20 20 7a 53 65 70 20 3d 20 22 2c 22 3b       zSep = ",";
25fd8 0a 20 20 20 20 20 20 20 20 6e 53 65 70 20 3d 20  .        nSep = 
25fd9 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
25fda 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
25fdb 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a  Append(pAccum, z
25fdc 53 65 70 2c 20 6e 53 65 70 29 3b 0a 20 20 20 20  Sep, nSep);.    
25fdd 7d 0a 20 20 20 20 7a 56 61 6c 20 3d 20 28 63 68  }.    zVal = (ch
25fde 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar*)sqlite3_valu
25fdf 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
25fe0 0a 20 20 20 20 6e 56 61 6c 20 3d 20 73 71 6c 69  .    nVal = sqli
25fe1 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
25fe2 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 73 71  argv[0]);.    sq
25fe3 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
25fe4 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a 56 61 6c  end(pAccum, zVal
25fe5 2c 20 6e 56 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 73  , nVal);.  }.}.s
25fe6 74 61 74 69 63 20 76 6f 69 64 20 67 72 6f 75 70  tatic void group
25fe7 43 6f 6e 63 61 74 46 69 6e 61 6c 69 7a 65 28 73  ConcatFinalize(s
25fe8 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
25fe9 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 74 72 41  context){.  StrA
25fea 63 63 75 6d 20 2a 70 41 63 63 75 6d 3b 0a 20 20  ccum *pAccum;.  
25feb 70 41 63 63 75 6d 20 3d 20 73 71 6c 69 74 65 33  pAccum = sqlite3
25fec 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
25fed 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a  xt(context, 0);.
25fee 20 20 69 66 28 20 70 41 63 63 75 6d 20 29 7b 0a    if( pAccum ){.
25fef 20 20 20 20 69 66 28 20 70 41 63 63 75 6d 2d 3e      if( pAccum->
25ff0 74 6f 6f 42 69 67 20 29 7b 0a 20 20 20 20 20 20  tooBig ){.      
25ff1 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
25ff2 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74  rror_toobig(cont
25ff3 65 78 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  ext);.    }else 
25ff4 69 66 28 20 70 41 63 63 75 6d 2d 3e 6d 61 6c 6c  if( pAccum->mall
25ff5 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
25ff6 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
25ff7 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e  _error_nomem(con
25ff8 74 65 78 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  text);.    }else
25ff9 7b 20 20 20 20 0a 20 20 20 20 20 20 73 71 6c 69  {    .      sqli
25ffa 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
25ffb 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33  context, sqlite3
25ffc 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 70  StrAccumFinish(p
25ffd 41 63 63 75 6d 29 2c 20 2d 31 2c 20 0a 20 20 20  Accum), -1, .   
25ffe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25fff 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
26000 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ree);.    }.  }.
26001 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
26002 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65  nction registere
26003 64 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f  d all of the abo
26004 76 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 61  ve C functions a
26005 73 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f  s SQL.** functio
26006 6e 73 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64  ns.  This should
26007 20 62 65 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75   be the only rou
26008 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c  tine in this fil
26009 65 20 77 69 74 68 0a 2a 2a 20 65 78 74 65 72 6e  e with.** extern
2600a 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2f 0a 53  al linkage..*/.S
2600b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2600c 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74  id sqlite3Regist
2600d 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 69 6f  erBuiltinFunctio
2600e 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  ns(sqlite3 *db){
2600f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
26010 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a  OMIT_ALTERTABLE.
26011 20 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 75    sqlite3AlterFu
26012 6e 63 74 69 6f 6e 73 28 64 62 29 3b 0a 23 65 6e  nctions(db);.#en
26013 64 69 66 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d  dif.  if( !db->m
26014 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
26015 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69     int rc = sqli
26016 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e  te3_overload_fun
26017 63 74 69 6f 6e 28 64 62 2c 20 22 4d 41 54 43 48  ction(db, "MATCH
26018 22 2c 20 32 29 3b 0a 20 20 20 20 61 73 73 65 72  ", 2);.    asser
26019 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  t( rc==SQLITE_NO
2601a 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  MEM || rc==SQLIT
2601b 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69 66 28 20  E_OK );.    if( 
2601c 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
2601d 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61   ){.      db->ma
2601e 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
2601f 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
26020 2a 2a 20 53 65 74 20 74 68 65 20 4c 49 4b 45 4f  ** Set the LIKEO
26021 50 54 20 66 6c 61 67 20 6f 6e 20 74 68 65 20 32  PT flag on the 2
26022 2d 61 72 67 75 6d 65 6e 74 20 66 75 6e 63 74 69  -argument functi
26023 6f 6e 20 77 69 74 68 20 74 68 65 20 67 69 76 65  on with the give
26024 6e 20 6e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69  n name..*/.stati
26025 63 20 76 6f 69 64 20 73 65 74 4c 69 6b 65 4f 70  c void setLikeOp
26026 74 46 6c 61 67 28 73 71 6c 69 74 65 33 20 2a 64  tFlag(sqlite3 *d
26027 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
26028 4e 61 6d 65 2c 20 75 38 20 66 6c 61 67 56 61 6c  Name, u8 flagVal
26029 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 44  ){.  FuncDef *pD
2602a 65 66 3b 0a 20 20 70 44 65 66 20 3d 20 73 71 6c  ef;.  pDef = sql
2602b 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e  ite3FindFunction
2602c 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 73 71 6c 69  (db, zName, sqli
2602d 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d  te3Strlen30(zNam
2602e 65 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  e),.            
2602f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26030 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   2, SQLITE_UTF8,
26031 20 30 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 59   0);.  if( ALWAY
26032 53 28 70 44 65 66 29 20 29 7b 0a 20 20 20 20 70  S(pDef) ){.    p
26033 44 65 66 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61  Def->flags = fla
26034 67 56 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  gVal;.  }.}../*.
26035 2a 2a 20 52 65 67 69 73 74 65 72 20 74 68 65 20  ** Register the 
26036 62 75 69 6c 74 2d 69 6e 20 4c 49 4b 45 20 61 6e  built-in LIKE an
26037 64 20 47 4c 4f 42 20 66 75 6e 63 74 69 6f 6e 73  d GLOB functions
26038 2e 20 20 54 68 65 20 63 61 73 65 53 65 6e 73 69  .  The caseSensi
26039 74 69 76 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65  tive.** paramete
2603a 72 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65  r determines whe
2603b 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
2603c 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 69 73  LIKE operator is
2603d 20 63 61 73 65 0a 2a 2a 20 73 65 6e 73 69 74 69   case.** sensiti
2603e 76 65 2e 20 20 47 4c 4f 42 20 69 73 20 61 6c 77  ve.  GLOB is alw
2603f 61 79 73 20 63 61 73 65 20 73 65 6e 73 69 74 69  ays case sensiti
26040 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ve..*/.SQLITE_PR
26041 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
26042 65 33 52 65 67 69 73 74 65 72 4c 69 6b 65 46 75  e3RegisterLikeFu
26043 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20  nctions(sqlite3 
26044 2a 64 62 2c 20 69 6e 74 20 63 61 73 65 53 65 6e  *db, int caseSen
26045 73 69 74 69 76 65 29 7b 0a 20 20 73 74 72 75 63  sitive){.  struc
26046 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70  t compareInfo *p
26047 49 6e 66 6f 3b 0a 20 20 69 66 28 20 63 61 73 65  Info;.  if( case
26048 53 65 6e 73 69 74 69 76 65 20 29 7b 0a 20 20 20  Sensitive ){.   
26049 20 70 49 6e 66 6f 20 3d 20 28 73 74 72 75 63 74   pInfo = (struct
2604a 20 63 6f 6d 70 61 72 65 49 6e 66 6f 2a 29 26 6c   compareInfo*)&l
2604b 69 6b 65 49 6e 66 6f 41 6c 74 3b 0a 20 20 7d 65  ikeInfoAlt;.  }e
2604c 6c 73 65 7b 0a 20 20 20 20 70 49 6e 66 6f 20 3d  lse{.    pInfo =
2604d 20 28 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65   (struct compare
2604e 49 6e 66 6f 2a 29 26 6c 69 6b 65 49 6e 66 6f 4e  Info*)&likeInfoN
2604f 6f 72 6d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  orm;.  }.  sqlit
26050 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c  e3CreateFunc(db,
26051 20 22 6c 69 6b 65 22 2c 20 32 2c 20 53 51 4c 49   "like", 2, SQLI
26052 54 45 5f 41 4e 59 2c 20 70 49 6e 66 6f 2c 20 6c  TE_ANY, pInfo, l
26053 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a  ikeFunc, 0, 0);.
26054 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46    sqlite3CreateF
26055 75 6e 63 28 64 62 2c 20 22 6c 69 6b 65 22 2c 20  unc(db, "like", 
26056 33 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 70  3, SQLITE_ANY, p
26057 49 6e 66 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c 20  Info, likeFunc, 
26058 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  0, 0);.  sqlite3
26059 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22  CreateFunc(db, "
2605a 67 6c 6f 62 22 2c 20 32 2c 20 53 51 4c 49 54 45  glob", 2, SQLITE
2605b 5f 41 4e 59 2c 20 0a 20 20 20 20 20 20 28 73 74  _ANY, .      (st
2605c 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f  ruct compareInfo
2605d 2a 29 26 67 6c 6f 62 49 6e 66 6f 2c 20 6c 69 6b  *)&globInfo, lik
2605e 65 46 75 6e 63 2c 20 30 2c 30 29 3b 0a 20 20 73  eFunc, 0,0);.  s
2605f 65 74 4c 69 6b 65 4f 70 74 46 6c 61 67 28 64 62  etLikeOptFlag(db
26060 2c 20 22 67 6c 6f 62 22 2c 20 53 51 4c 49 54 45  , "glob", SQLITE
26061 5f 46 55 4e 43 5f 4c 49 4b 45 20 7c 20 53 51 4c  _FUNC_LIKE | SQL
26062 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 3b 0a  ITE_FUNC_CASE);.
26063 20 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61 67    setLikeOptFlag
26064 28 64 62 2c 20 22 6c 69 6b 65 22 2c 20 0a 20 20  (db, "like", .  
26065 20 20 20 20 63 61 73 65 53 65 6e 73 69 74 69 76      caseSensitiv
26066 65 20 3f 20 28 53 51 4c 49 54 45 5f 46 55 4e 43  e ? (SQLITE_FUNC
26067 5f 4c 49 4b 45 20 7c 20 53 51 4c 49 54 45 5f 46  _LIKE | SQLITE_F
26068 55 4e 43 5f 43 41 53 45 29 20 3a 20 53 51 4c 49  UNC_CASE) : SQLI
26069 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 3b 0a 7d  TE_FUNC_LIKE);.}
2606a 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 70 6f  ../*.** pExpr po
2606b 69 6e 74 73 20 74 6f 20 61 6e 20 65 78 70 72 65  ints to an expre
2606c 73 73 69 6f 6e 20 77 68 69 63 68 20 69 6d 70 6c  ssion which impl
2606d 65 6d 65 6e 74 73 20 61 20 66 75 6e 63 74 69 6f  ements a functio
2606e 6e 2e 20 20 49 66 0a 2a 2a 20 69 74 20 69 73 20  n.  If.** it is 
2606f 61 70 70 72 6f 70 72 69 61 74 65 20 74 6f 20 61  appropriate to a
26070 70 70 6c 79 20 74 68 65 20 4c 49 4b 45 20 6f 70  pply the LIKE op
26071 74 69 6d 69 7a 61 74 69 6f 6e 20 74 6f 20 74 68  timization to th
26072 61 74 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74  at function.** t
26073 68 65 6e 20 73 65 74 20 61 57 63 5b 30 5d 20 74  hen set aWc[0] t
26074 68 72 6f 75 67 68 20 61 57 63 5b 32 5d 20 74 6f  hrough aWc[2] to
26075 20 74 68 65 20 77 69 6c 64 63 61 72 64 20 63 68   the wildcard ch
26076 61 72 61 63 74 65 72 73 20 61 6e 64 0a 2a 2a 20  aracters and.** 
26077 72 65 74 75 72 6e 20 54 52 55 45 2e 20 20 49 66  return TRUE.  If
26078 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73   the function is
26079 20 6e 6f 74 20 61 20 4c 49 4b 45 2d 73 74 79 6c   not a LIKE-styl
2607a 65 20 66 75 6e 63 74 69 6f 6e 20 74 68 65 6e 0a  e function then.
2607b 2a 2a 20 72 65 74 75 72 6e 20 46 41 4c 53 45 2e  ** return FALSE.
2607c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2607d 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73  TE int sqlite3Is
2607e 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 28 73 71 6c  LikeFunction(sql
2607f 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a  ite3 *db, Expr *
26080 70 45 78 70 72 2c 20 69 6e 74 20 2a 70 49 73 4e  pExpr, int *pIsN
26081 6f 63 61 73 65 2c 20 63 68 61 72 20 2a 61 57 63  ocase, char *aWc
26082 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 44  ){.  FuncDef *pD
26083 65 66 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d  ef;.  if( pExpr-
26084 3e 6f 70 21 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e  >op!=TK_FUNCTION
26085 20 0a 20 20 20 7c 7c 20 21 70 45 78 70 72 2d 3e   .   || !pExpr->
26086 78 2e 70 4c 69 73 74 20 0a 20 20 20 7c 7c 20 70  x.pList .   || p
26087 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e  Expr->x.pList->n
26088 45 78 70 72 21 3d 32 0a 20 20 29 7b 0a 20 20 20  Expr!=2.  ){.   
26089 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
2608a 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
2608b 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
2608c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
2608d 3b 0a 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74  ;.  pDef = sqlit
2608e 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64  e3FindFunction(d
2608f 62 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b  b, pExpr->u.zTok
26090 65 6e 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  en, .           
26091 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26092 20 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33    sqlite3Strlen3
26093 30 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  0(pExpr->u.zToke
26094 6e 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n),.            
26095 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26096 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   2, SQLITE_UTF8,
26097 20 30 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52   0);.  if( NEVER
26098 28 70 44 65 66 3d 3d 30 29 20 7c 7c 20 28 70 44  (pDef==0) || (pD
26099 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  ef->flags & SQLI
2609a 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 3d 3d 30  TE_FUNC_LIKE)==0
2609b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
2609c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  ;.  }..  /* The 
2609d 6d 65 6d 63 70 79 28 29 20 73 74 61 74 65 6d 65  memcpy() stateme
2609e 6e 74 20 61 73 73 75 6d 65 73 20 74 68 61 74 20  nt assumes that 
2609f 74 68 65 20 77 69 6c 64 63 61 72 64 20 63 68 61  the wildcard cha
260a0 72 61 63 74 65 72 73 20 61 72 65 0a 20 20 2a 2a  racters are.  **
260a1 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65 65   the first three
260a2 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74   statements in t
260a3 68 65 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 73  he compareInfo s
260a4 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 0a 20  tructure.  The. 
260a5 20 2a 2a 20 61 73 73 65 72 74 73 28 29 20 74 68   ** asserts() th
260a6 61 74 20 66 6f 6c 6c 6f 77 20 76 65 72 69 66 79  at follow verify
260a7 20 74 68 61 74 20 61 73 73 75 6d 70 74 69 6f 6e   that assumption
260a8 0a 20 20 2a 2f 0a 20 20 6d 65 6d 63 70 79 28 61  .  */.  memcpy(a
260a9 57 63 2c 20 70 44 65 66 2d 3e 70 55 73 65 72 44  Wc, pDef->pUserD
260aa 61 74 61 2c 20 33 29 3b 0a 20 20 61 73 73 65 72  ata, 3);.  asser
260ab 74 28 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49  t( (char*)&likeI
260ac 6e 66 6f 41 6c 74 20 3d 3d 20 28 63 68 61 72 2a  nfoAlt == (char*
260ad 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e 6d 61  )&likeInfoAlt.ma
260ae 74 63 68 41 6c 6c 20 29 3b 0a 20 20 61 73 73 65  tchAll );.  asse
260af 72 74 28 20 26 28 28 63 68 61 72 2a 29 26 6c 69  rt( &((char*)&li
260b0 6b 65 49 6e 66 6f 41 6c 74 29 5b 31 5d 20 3d 3d  keInfoAlt)[1] ==
260b1 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66   (char*)&likeInf
260b2 6f 41 6c 74 2e 6d 61 74 63 68 4f 6e 65 20 29 3b  oAlt.matchOne );
260b3 0a 20 20 61 73 73 65 72 74 28 20 26 28 28 63 68  .  assert( &((ch
260b4 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74  ar*)&likeInfoAlt
260b5 29 5b 32 5d 20 3d 3d 20 28 63 68 61 72 2a 29 26  )[2] == (char*)&
260b6 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e 6d 61 74 63  likeInfoAlt.matc
260b7 68 53 65 74 20 29 3b 0a 20 20 2a 70 49 73 4e 6f  hSet );.  *pIsNo
260b8 63 61 73 65 20 3d 20 28 70 44 65 66 2d 3e 66 6c  case = (pDef->fl
260b9 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e  ags & SQLITE_FUN
260ba 43 5f 43 41 53 45 29 3d 3d 30 3b 0a 20 20 72 65  C_CASE)==0;.  re
260bb 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 1;.}../*.**
260bc 20 41 6c 6c 20 61 6c 6c 20 6f 66 20 74 68 65 20   All all of the 
260bd 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72  FuncDef structur
260be 65 73 20 69 6e 20 74 68 65 20 61 42 75 69 6c 74  es in the aBuilt
260bf 69 6e 46 75 6e 63 5b 5d 20 61 72 72 61 79 20 61  inFunc[] array a
260c0 62 6f 76 65 0a 2a 2a 20 74 6f 20 74 68 65 20 67  bove.** to the g
260c1 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e 20 68  lobal function h
260c2 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 69 73  ash table.  This
260c3 20 6f 63 63 75 72 73 20 61 74 20 73 74 61 72 74   occurs at start
260c4 2d 74 69 6d 65 20 28 61 73 0a 2a 2a 20 61 20 63  -time (as.** a c
260c5 6f 6e 73 65 71 75 65 6e 63 65 20 6f 66 20 63 61  onsequence of ca
260c6 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e  lling sqlite3_in
260c7 69 74 69 61 6c 69 7a 65 28 29 29 2e 0a 2a 2a 0a  itialize())..**.
260c8 2a 2a 20 41 66 74 65 72 20 74 68 69 73 20 72 6f  ** After this ro
260c9 75 74 69 6e 65 20 72 75 6e 73 0a 2a 2f 0a 53 51  utine runs.*/.SQ
260ca 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
260cb 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65  d sqlite3Registe
260cc 72 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73  rGlobalFunctions
260cd 28 76 6f 69 64 29 7b 0a 20 20 2f 2a 0a 20 20 2a  (void){.  /*.  *
260ce 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
260cf 61 72 72 61 79 20 68 6f 6c 64 73 20 46 75 6e 63  array holds Func
260d0 44 65 66 20 73 74 72 75 63 74 75 72 65 73 20 66  Def structures f
260d1 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 75  or all of the fu
260d2 6e 63 74 69 6f 6e 73 0a 20 20 2a 2a 20 64 65 66  nctions.  ** def
260d3 69 6e 65 64 20 69 6e 20 74 68 69 73 20 66 69 6c  ined in this fil
260d4 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  e..  **.  ** The
260d5 20 61 72 72 61 79 20 63 61 6e 6e 6f 74 20 62 65   array cannot be
260d6 20 63 6f 6e 73 74 61 6e 74 20 73 69 6e 63 65 20   constant since 
260d7 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65  changes are made
260d8 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 46 75 6e   to the.  ** Fun
260d9 63 44 65 66 2e 70 48 61 73 68 20 65 6c 65 6d 65  cDef.pHash eleme
260da 6e 74 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d  nts at start-tim
260db 65 2e 20 20 54 68 65 20 65 6c 65 6d 65 6e 74 73  e.  The elements
260dc 20 6f 66 20 74 68 69 73 20 61 72 72 61 79 0a 20   of this array. 
260dd 20 2a 2a 20 61 72 65 20 72 65 61 64 2d 6f 6e 6c   ** are read-onl
260de 79 20 61 66 74 65 72 20 69 6e 69 74 69 61 6c 69  y after initiali
260df 7a 61 74 69 6f 6e 20 69 73 20 63 6f 6d 70 6c 65  zation is comple
260e0 74 65 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69  te..  */.  stati
260e1 63 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e  c SQLITE_WSD Fun
260e2 63 44 65 66 20 61 42 75 69 6c 74 69 6e 46 75 6e  cDef aBuiltinFun
260e3 63 5b 5d 20 3d 20 7b 0a 20 20 20 20 46 55 4e 43  c[] = {.    FUNC
260e4 54 49 4f 4e 28 6c 74 72 69 6d 2c 20 20 20 20 20  TION(ltrim,     
260e5 20 20 20 20 20 20 20 20 20 31 2c 20 31 2c 20 30           1, 1, 0
260e6 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20  , trimFunc      
260e7 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
260e8 4f 4e 28 6c 74 72 69 6d 2c 20 20 20 20 20 20 20  ON(ltrim,       
260e9 20 20 20 20 20 20 20 32 2c 20 31 2c 20 30 2c 20         2, 1, 0, 
260ea 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20  trimFunc        
260eb 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
260ec 28 72 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20  (rtrim,         
260ed 20 20 20 20 20 31 2c 20 32 2c 20 30 2c 20 74 72       1, 2, 0, tr
260ee 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20 29  imFunc         )
260ef 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 72  ,.    FUNCTION(r
260f0 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20  trim,           
260f1 20 20 20 32 2c 20 32 2c 20 30 2c 20 74 72 69 6d     2, 2, 0, trim
260f2 46 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a  Func         ),.
260f3 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 74 72 69      FUNCTION(tri
260f4 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m,              
260f5 20 31 2c 20 33 2c 20 30 2c 20 74 72 69 6d 46 75   1, 3, 0, trimFu
260f6 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20  nc         ),.  
260f7 20 20 46 55 4e 43 54 49 4f 4e 28 74 72 69 6d 2c    FUNCTION(trim,
260f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
260f9 2c 20 33 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63  , 3, 0, trimFunc
260fa 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20           ),.    
260fb 46 55 4e 43 54 49 4f 4e 28 6d 69 6e 2c 20 20 20  FUNCTION(min,   
260fc 20 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20              -1, 
260fd 30 2c 20 31 2c 20 6d 69 6e 6d 61 78 46 75 6e 63  0, 1, minmaxFunc
260fe 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
260ff 4e 43 54 49 4f 4e 28 6d 69 6e 2c 20 20 20 20 20  NCTION(min,     
26100 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c             0, 0,
26101 20 31 2c 20 30 20 20 20 20 20 20 20 20 20 20 20   1, 0           
26102 20 20 20 20 20 29 2c 0a 20 20 20 20 41 47 47 52       ),.    AGGR
26103 45 47 41 54 45 28 6d 69 6e 2c 20 20 20 20 20 20  EGATE(min,      
26104 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 31           1, 0, 1
26105 2c 20 6d 69 6e 6d 61 78 53 74 65 70 2c 20 20 20  , minmaxStep,   
26106 20 20 20 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a     minMaxFinaliz
26107 65 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f  e ),.    FUNCTIO
26108 4e 28 6d 61 78 2c 20 20 20 20 20 20 20 20 20 20  N(max,          
26109 20 20 20 20 20 2d 31 2c 20 31 2c 20 31 2c 20 6d       -1, 1, 1, m
2610a 69 6e 6d 61 78 46 75 6e 63 20 20 20 20 20 20 20  inmaxFunc       
2610b 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
2610c 6d 61 78 2c 20 20 20 20 20 20 20 20 20 20 20 20  max,            
2610d 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 30 20 20      0, 1, 1, 0  
2610e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 29 2c                ),
2610f 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 28 6d  .    AGGREGATE(m
26110 61 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ax,             
26111 20 20 31 2c 20 31 2c 20 31 2c 20 6d 69 6e 6d 61    1, 1, 1, minma
26112 78 53 74 65 70 2c 20 20 20 20 20 20 6d 69 6e 4d  xStep,      minM
26113 61 78 46 69 6e 61 6c 69 7a 65 20 29 2c 0a 20 20  axFinalize ),.  
26114 20 20 46 55 4e 43 54 49 4f 4e 28 74 79 70 65 6f    FUNCTION(typeo
26115 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 31  f,             1
26116 2c 20 30 2c 20 30 2c 20 74 79 70 65 6f 66 46 75  , 0, 0, typeofFu
26117 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20  nc       ),.    
26118 46 55 4e 43 54 49 4f 4e 28 6c 65 6e 67 74 68 2c  FUNCTION(length,
26119 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
2611a 30 2c 20 30 2c 20 6c 65 6e 67 74 68 46 75 6e 63  0, 0, lengthFunc
2611b 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
2611c 4e 43 54 49 4f 4e 28 73 75 62 73 74 72 2c 20 20  NCTION(substr,  
2611d 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c             2, 0,
2611e 20 30 2c 20 73 75 62 73 74 72 46 75 6e 63 20 20   0, substrFunc  
2611f 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
26120 54 49 4f 4e 28 73 75 62 73 74 72 2c 20 20 20 20  TION(substr,    
26121 20 20 20 20 20 20 20 20 20 33 2c 20 30 2c 20 30           3, 0, 0
26122 2c 20 73 75 62 73 74 72 46 75 6e 63 20 20 20 20  , substrFunc    
26123 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
26124 4f 4e 28 61 62 73 2c 20 20 20 20 20 20 20 20 20  ON(abs,         
26125 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20         1, 0, 0, 
26126 61 62 73 46 75 6e 63 20 20 20 20 20 20 20 20 20  absFunc         
26127 20 29 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   ),.#ifndef SQLI
26128 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
26129 5f 50 4f 49 4e 54 0a 20 20 20 20 46 55 4e 43 54  _POINT.    FUNCT
2612a 49 4f 4e 28 72 6f 75 6e 64 2c 20 20 20 20 20 20  ION(round,      
2612b 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c          1, 0, 0,
2612c 20 72 6f 75 6e 64 46 75 6e 63 20 20 20 20 20 20   roundFunc      
2612d 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
2612e 4e 28 72 6f 75 6e 64 2c 20 20 20 20 20 20 20 20  N(round,        
2612f 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 72        2, 0, 0, r
26130 6f 75 6e 64 46 75 6e 63 20 20 20 20 20 20 20 20  oundFunc        
26131 29 2c 0a 23 65 6e 64 69 66 0a 20 20 20 20 46 55  ),.#endif.    FU
26132 4e 43 54 49 4f 4e 28 75 70 70 65 72 2c 20 20 20  NCTION(upper,   
26133 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c             1, 0,
26134 20 30 2c 20 75 70 70 65 72 46 75 6e 63 20 20 20   0, upperFunc   
26135 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
26136 54 49 4f 4e 28 6c 6f 77 65 72 2c 20 20 20 20 20  TION(lower,     
26137 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30           1, 0, 0
26138 2c 20 6c 6f 77 65 72 46 75 6e 63 20 20 20 20 20  , lowerFunc     
26139 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
2613a 4f 4e 28 63 6f 61 6c 65 73 63 65 2c 20 20 20 20  ON(coalesce,    
2613b 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20         1, 0, 0, 
2613c 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
2613d 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
2613e 28 63 6f 61 6c 65 73 63 65 2c 20 20 20 20 20 20  (coalesce,      
2613f 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 69 66      -1, 0, 0, if
26140 6e 75 6c 6c 46 75 6e 63 20 20 20 20 20 20 20 29  nullFunc       )
26141 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63  ,.    FUNCTION(c
26142 6f 61 6c 65 73 63 65 2c 20 20 20 20 20 20 20 20  oalesce,        
26143 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 20 20 20     0, 0, 0, 0   
26144 20 20 20 20 20 20 20 20 20 20 20 20 20 29 2c 0a               ),.
26145 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 68 65 78      FUNCTION(hex
26146 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
26147 20 31 2c 20 30 2c 20 30 2c 20 68 65 78 46 75 6e   1, 0, 0, hexFun
26148 63 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20  c          ),.  
26149 20 20 46 55 4e 43 54 49 4f 4e 28 69 66 6e 75 6c    FUNCTION(ifnul
2614a 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 32  l,             2
2614b 2c 20 30 2c 20 31 2c 20 69 66 6e 75 6c 6c 46 75  , 0, 1, ifnullFu
2614c 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20  nc       ),.    
2614d 46 55 4e 43 54 49 4f 4e 28 72 61 6e 64 6f 6d 2c  FUNCTION(random,
2614e 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20               0, 
2614f 30 2c 20 30 2c 20 72 61 6e 64 6f 6d 46 75 6e 63  0, 0, randomFunc
26150 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
26151 4e 43 54 49 4f 4e 28 72 61 6e 64 6f 6d 62 6c 6f  NCTION(randomblo
26152 62 2c 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c  b,         1, 0,
26153 20 30 2c 20 72 61 6e 64 6f 6d 42 6c 6f 62 20 20   0, randomBlob  
26154 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
26155 54 49 4f 4e 28 6e 75 6c 6c 69 66 2c 20 20 20 20  TION(nullif,    
26156 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20 31           2, 0, 1
26157 2c 20 6e 75 6c 6c 69 66 46 75 6e 63 20 20 20 20  , nullifFunc    
26158 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
26159 4f 4e 28 73 71 6c 69 74 65 5f 76 65 72 73 69 6f  ON(sqlite_versio
2615a 6e 2c 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20  n,     0, 0, 0, 
2615b 76 65 72 73 69 6f 6e 46 75 6e 63 20 20 20 20 20  versionFunc     
2615c 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
2615d 28 73 71 6c 69 74 65 5f 73 6f 75 72 63 65 5f 69  (sqlite_source_i
2615e 64 2c 20 20 20 30 2c 20 30 2c 20 30 2c 20 73 6f  d,   0, 0, 0, so
2615f 75 72 63 65 69 64 46 75 6e 63 20 20 20 20 20 29  urceidFunc     )
26160 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 71  ,.    FUNCTION(q
26161 75 6f 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  uote,           
26162 20 20 20 31 2c 20 30 2c 20 30 2c 20 71 75 6f 74     1, 0, 0, quot
26163 65 46 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a  eFunc        ),.
26164 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 61 73      FUNCTION(las
26165 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 2c 20  t_insert_rowid, 
26166 20 30 2c 20 30 2c 20 30 2c 20 6c 61 73 74 5f 69   0, 0, 0, last_i
26167 6e 73 65 72 74 5f 72 6f 77 69 64 29 2c 0a 20 20  nsert_rowid),.  
26168 20 20 46 55 4e 43 54 49 4f 4e 28 63 68 61 6e 67    FUNCTION(chang
26169 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 30  es,            0
2616a 2c 20 30 2c 20 30 2c 20 63 68 61 6e 67 65 73 20  , 0, 0, changes 
2616b 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20           ),.    
2616c 46 55 4e 43 54 49 4f 4e 28 74 6f 74 61 6c 5f 63  FUNCTION(total_c
2616d 68 61 6e 67 65 73 2c 20 20 20 20 20 20 30 2c 20  hanges,      0, 
2616e 30 2c 20 30 2c 20 74 6f 74 61 6c 5f 63 68 61 6e  0, 0, total_chan
2616f 67 65 73 20 20 20 20 29 2c 0a 20 20 20 20 46 55  ges    ),.    FU
26170 4e 43 54 49 4f 4e 28 72 65 70 6c 61 63 65 2c 20  NCTION(replace, 
26171 20 20 20 20 20 20 20 20 20 20 20 33 2c 20 30 2c             3, 0,
26172 20 30 2c 20 72 65 70 6c 61 63 65 46 75 6e 63 20   0, replaceFunc 
26173 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
26174 54 49 4f 4e 28 7a 65 72 6f 62 6c 6f 62 2c 20 20  TION(zeroblob,  
26175 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30           1, 0, 0
26176 2c 20 7a 65 72 6f 62 6c 6f 62 46 75 6e 63 20 20  , zeroblobFunc  
26177 20 20 20 29 2c 0a 20 20 23 69 66 64 65 66 20 53     ),.  #ifdef S
26178 51 4c 49 54 45 5f 53 4f 55 4e 44 45 58 0a 20 20  QLITE_SOUNDEX.  
26179 20 20 46 55 4e 43 54 49 4f 4e 28 73 6f 75 6e 64    FUNCTION(sound
2617a 65 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 31  ex,            1
2617b 2c 20 30 2c 20 30 2c 20 73 6f 75 6e 64 65 78 46  , 0, 0, soundexF
2617c 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 23 65  unc      ),.  #e
2617d 6e 64 69 66 0a 20 20 23 69 66 6e 64 65 66 20 53  ndif.  #ifndef S
2617e 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f  QLITE_OMIT_LOAD_
2617f 45 58 54 45 4e 53 49 4f 4e 0a 20 20 20 20 46 55  EXTENSION.    FU
26180 4e 43 54 49 4f 4e 28 6c 6f 61 64 5f 65 78 74 65  NCTION(load_exte
26181 6e 73 69 6f 6e 2c 20 20 20 20 20 31 2c 20 30 2c  nsion,     1, 0,
26182 20 30 2c 20 6c 6f 61 64 45 78 74 20 20 20 20 20   0, loadExt     
26183 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
26184 54 49 4f 4e 28 6c 6f 61 64 5f 65 78 74 65 6e 73  TION(load_extens
26185 69 6f 6e 2c 20 20 20 20 20 32 2c 20 30 2c 20 30  ion,     2, 0, 0
26186 2c 20 6c 6f 61 64 45 78 74 20 20 20 20 20 20 20  , loadExt       
26187 20 20 20 29 2c 0a 20 20 23 65 6e 64 69 66 0a 20     ),.  #endif. 
26188 20 20 20 41 47 47 52 45 47 41 54 45 28 73 75 6d     AGGREGATE(sum
26189 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2618a 31 2c 20 30 2c 20 30 2c 20 73 75 6d 53 74 65 70  1, 0, 0, sumStep
2618b 2c 20 20 20 20 20 20 20 20 20 73 75 6d 46 69 6e  ,         sumFin
2618c 61 6c 69 7a 65 20 20 20 20 29 2c 0a 20 20 20 20  alize    ),.    
2618d 41 47 47 52 45 47 41 54 45 28 74 6f 74 61 6c 2c  AGGREGATE(total,
2618e 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
2618f 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c 20 20  0, 0, sumStep,  
26190 20 20 20 20 20 20 20 74 6f 74 61 6c 46 69 6e 61         totalFina
26191 6c 69 7a 65 20 20 20 20 29 2c 0a 20 20 20 20 41  lize    ),.    A
26192 47 47 52 45 47 41 54 45 28 61 76 67 2c 20 20 20  GGREGATE(avg,   
26193 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30              1, 0
26194 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c 20 20 20  , 0, sumStep,   
26195 20 20 20 20 20 20 61 76 67 46 69 6e 61 6c 69 7a        avgFinaliz
26196 65 20 20 20 20 29 2c 0a 20 2f 2a 20 41 47 47 52  e    ),. /* AGGR
26197 45 47 41 54 45 28 63 6f 75 6e 74 2c 20 20 20 20  EGATE(count,    
26198 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20 30           0, 0, 0
26199 2c 20 63 6f 75 6e 74 53 74 65 70 2c 20 20 20 20  , countStep,    
2619a 20 20 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65     countFinalize
2619b 20 20 29 2c 20 2a 2f 0a 20 20 20 20 7b 30 2c 53    ), */.    {0,S
2619c 51 4c 49 54 45 5f 55 54 46 38 2c 53 51 4c 49 54  QLITE_UTF8,SQLIT
2619d 45 5f 46 55 4e 43 5f 43 4f 55 4e 54 2c 30 2c 30  E_FUNC_COUNT,0,0
2619e 2c 30 2c 63 6f 75 6e 74 53 74 65 70 2c 63 6f 75  ,0,countStep,cou
2619f 6e 74 46 69 6e 61 6c 69 7a 65 2c 22 63 6f 75 6e  ntFinalize,"coun
261a0 74 22 2c 30 7d 2c 0a 20 20 20 20 41 47 47 52 45  t",0},.    AGGRE
261a1 47 41 54 45 28 63 6f 75 6e 74 2c 20 20 20 20 20  GATE(count,     
261a2 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c          1, 0, 0,
261a3 20 63 6f 75 6e 74 53 74 65 70 2c 20 20 20 20 20   countStep,     
261a4 20 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 20    countFinalize 
261a5 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54   ),.    AGGREGAT
261a6 45 28 67 72 6f 75 70 5f 63 6f 6e 63 61 74 2c 20  E(group_concat, 
261a7 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 67 72       1, 0, 0, gr
261a8 6f 75 70 43 6f 6e 63 61 74 53 74 65 70 2c 20 67  oupConcatStep, g
261a9 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c 69  roupConcatFinali
261aa 7a 65 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41  ze),.    AGGREGA
261ab 54 45 28 67 72 6f 75 70 5f 63 6f 6e 63 61 74 2c  TE(group_concat,
261ac 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 67        2, 0, 0, g
261ad 72 6f 75 70 43 6f 6e 63 61 74 53 74 65 70 2c 20  roupConcatStep, 
261ae 67 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c  groupConcatFinal
261af 69 7a 65 29 2c 0a 20 20 0a 20 20 20 20 4c 49 4b  ize),.  .    LIK
261b0 45 46 55 4e 43 28 67 6c 6f 62 2c 20 32 2c 20 26  EFUNC(glob, 2, &
261b1 67 6c 6f 62 49 6e 66 6f 2c 20 53 51 4c 49 54 45  globInfo, SQLITE
261b2 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49 54  _FUNC_LIKE|SQLIT
261b3 45 5f 46 55 4e 43 5f 43 41 53 45 29 2c 0a 20 20  E_FUNC_CASE),.  
261b4 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 41  #ifdef SQLITE_CA
261b5 53 45 5f 53 45 4e 53 49 54 49 56 45 5f 4c 49 4b  SE_SENSITIVE_LIK
261b6 45 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c  E.    LIKEFUNC(l
261b7 69 6b 65 2c 20 32 2c 20 26 6c 69 6b 65 49 6e 66  ike, 2, &likeInf
261b8 6f 41 6c 74 2c 20 53 51 4c 49 54 45 5f 46 55 4e  oAlt, SQLITE_FUN
261b9 43 5f 4c 49 4b 45 7c 53 51 4c 49 54 45 5f 46 55  C_LIKE|SQLITE_FU
261ba 4e 43 5f 43 41 53 45 29 2c 0a 20 20 20 20 4c 49  NC_CASE),.    LI
261bb 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 33 2c 20  KEFUNC(like, 3, 
261bc 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2c 20 53 51  &likeInfoAlt, SQ
261bd 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53  LITE_FUNC_LIKE|S
261be 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29  QLITE_FUNC_CASE)
261bf 2c 0a 20 20 23 65 6c 73 65 0a 20 20 20 20 4c 49  ,.  #else.    LI
261c0 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 32 2c 20  KEFUNC(like, 2, 
261c1 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 2c 20 53  &likeInfoNorm, S
261c2 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29  QLITE_FUNC_LIKE)
261c3 2c 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c  ,.    LIKEFUNC(l
261c4 69 6b 65 2c 20 33 2c 20 26 6c 69 6b 65 49 6e 66  ike, 3, &likeInf
261c5 6f 4e 6f 72 6d 2c 20 53 51 4c 49 54 45 5f 46 55  oNorm, SQLITE_FU
261c6 4e 43 5f 4c 49 4b 45 29 2c 0a 20 20 23 65 6e 64  NC_LIKE),.  #end
261c7 69 66 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 69  if.  };..  int i
261c8 3b 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68 20  ;.  FuncDefHash 
261c9 2a 70 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c  *pHash = &GLOBAL
261ca 28 46 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71  (FuncDefHash, sq
261cb 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74  lite3GlobalFunct
261cc 69 6f 6e 73 29 3b 0a 20 20 46 75 6e 63 44 65 66  ions);.  FuncDef
261cd 20 2a 61 46 75 6e 63 20 3d 20 28 46 75 6e 63 44   *aFunc = (FuncD
261ce 65 66 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63  ef*)&GLOBAL(Func
261cf 44 65 66 2c 20 61 42 75 69 6c 74 69 6e 46 75 6e  Def, aBuiltinFun
261d0 63 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20  c);..  for(i=0; 
261d1 69 3c 41 72 72 61 79 53 69 7a 65 28 61 42 75 69  i<ArraySize(aBui
261d2 6c 74 69 6e 46 75 6e 63 29 3b 20 69 2b 2b 29 7b  ltinFunc); i++){
261d3 0a 20 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63  .    sqlite3Func
261d4 44 65 66 49 6e 73 65 72 74 28 70 48 61 73 68 2c  DefInsert(pHash,
261d5 20 26 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d   &aFunc[i]);.  }
261d6 0a 20 20 73 71 6c 69 74 65 33 52 65 67 69 73 74  .  sqlite3Regist
261d7 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69  erDateTimeFuncti
261d8 6f 6e 73 28 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  ons();.}../*****
261d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
261da 20 66 75 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   func.c ********
261db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
261dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
261dd 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
261de 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
261df 66 69 6c 65 20 69 6e 73 65 72 74 2e 63 20 2a 2a  file insert.c **
261e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
261e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
261e2 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
261e3 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
261e4 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
261e5 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
261e6 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
261e7 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
261e8 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
261e9 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
261ea 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
261eb 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
261ec 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
261ed 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
261ee 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
261ef 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
261f0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
261f1 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
261f2 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
261f3 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
261f4 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
261f5 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
261f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
261f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
261f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
261f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
261fa 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
261fb 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f  ntains C code ro
261fc 75 74 69 6e 65 73 20 74 68 61 74 20 61 72 65 20  utines that are 
261fd 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61  called by the pa
261fe 72 73 65 72 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c  rser.** to handl
261ff 65 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65  e INSERT stateme
26200 6e 74 73 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a  nts in SQLite..*
26201 2a 0a 2a 2a 20 24 49 64 3a 20 69 6e 73 65 72 74  *.** $Id: insert
26202 2e 63 2c 76 20 31 2e 32 37 30 20 32 30 30 39 2f  .c,v 1.270 2009/
26203 30 37 2f 32 34 20 31 37 3a 35 38 3a 35 33 20 64  07/24 17:58:53 d
26204 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
26205 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  .*/../*.** Gener
26206 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69  ate code that wi
26207 6c 6c 20 6f 70 65 6e 20 61 20 74 61 62 6c 65 20  ll open a table 
26208 66 6f 72 20 72 65 61 64 69 6e 67 2e 0a 2a 2f 0a  for reading..*/.
26209 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2620a 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65 6e 54  oid sqlite3OpenT
2620b 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70  able(.  Parse *p
2620c 2c 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72  ,       /* Gener
2620d 61 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68  ate code into th
2620e 69 73 20 56 44 42 45 20 2a 2f 0a 20 20 69 6e 74  is VDBE */.  int
2620f 20 69 43 75 72 2c 20 20 20 20 20 20 20 2f 2a 20   iCur,       /* 
26210 54 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  The cursor numbe
26211 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a  r of the table *
26212 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20  /.  int iDb,    
26213 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
26214 61 73 65 20 69 6e 64 65 78 20 69 6e 20 73 71 6c  ase index in sql
26215 69 74 65 33 2e 61 44 62 5b 5d 20 2a 2f 0a 20 20  ite3.aDb[] */.  
26216 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20  Table *pTab,    
26217 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20  /* The table to 
26218 62 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69  be opened */.  i
26219 6e 74 20 6f 70 63 6f 64 65 20 20 20 20 20 20 2f  nt opcode      /
2621a 2a 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 6f 72  * OP_OpenRead or
2621b 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 2a 2f   OP_OpenWrite */
2621c 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20  .){.  Vdbe *v;. 
2621d 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70   if( IsVirtual(p
2621e 54 61 62 29 20 29 20 72 65 74 75 72 6e 3b 0a 20  Tab) ) return;. 
2621f 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
26220 64 62 65 28 70 29 3b 0a 20 20 61 73 73 65 72 74  dbe(p);.  assert
26221 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65  ( opcode==OP_Ope
26222 6e 57 72 69 74 65 20 7c 7c 20 6f 70 63 6f 64 65  nWrite || opcode
26223 3d 3d 4f 50 5f 4f 70 65 6e 52 65 61 64 20 29 3b  ==OP_OpenRead );
26224 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c  .  sqlite3TableL
26225 6f 63 6b 28 70 2c 20 69 44 62 2c 20 70 54 61 62  ock(p, iDb, pTab
26226 2d 3e 74 6e 75 6d 2c 20 28 6f 70 63 6f 64 65 3d  ->tnum, (opcode=
26227 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3f 31  =OP_OpenWrite)?1
26228 3a 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  :0, pTab->zName)
26229 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
2622a 64 64 4f 70 33 28 76 2c 20 6f 70 63 6f 64 65 2c  ddOp3(v, opcode,
2622b 20 69 43 75 72 2c 20 70 54 61 62 2d 3e 74 6e 75   iCur, pTab->tnu
2622c 6d 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c 69 74  m, iDb);.  sqlit
2622d 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
2622e 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 49 4e 54  , -1, SQLITE_INT
2622f 5f 54 4f 5f 50 54 52 28 70 54 61 62 2d 3e 6e 43  _TO_PTR(pTab->nC
26230 6f 6c 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a  ol), P4_INT32);.
26231 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
26232 2c 20 22 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e  , "%s", pTab->zN
26233 61 6d 65 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ame));.}../*.** 
26234 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
26235 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61   to the column a
26236 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 61  ffinity string a
26237 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 69  ssociated with i
26238 6e 64 65 78 0a 2a 2a 20 70 49 64 78 2e 20 41 20  ndex.** pIdx. A 
26239 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20  column affinity 
2623a 73 74 72 69 6e 67 20 68 61 73 20 6f 6e 65 20 63  string has one c
2623b 68 61 72 61 63 74 65 72 20 66 6f 72 20 65 61 63  haracter for eac
2623c 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 0a 2a 2a 20  h column in .** 
2623d 74 68 65 20 74 61 62 6c 65 2c 20 61 63 63 6f 72  the table, accor
2623e 64 69 6e 67 20 74 6f 20 74 68 65 20 61 66 66 69  ding to the affi
2623f 6e 69 74 79 20 6f 66 20 74 68 65 20 63 6f 6c 75  nity of the colu
26240 6d 6e 3a 0a 2a 2a 0a 2a 2a 20 20 43 68 61 72 61  mn:.**.**  Chara
26241 63 74 65 72 20 20 20 20 20 20 43 6f 6c 75 6d 6e  cter      Column
26242 20 61 66 66 69 6e 69 74 79 0a 2a 2a 20 20 2d 2d   affinity.**  --
26243 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26244 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20  ------------.** 
26245 20 27 61 27 20 20 20 20 20 20 20 20 20 20 20 20   'a'            
26246 54 45 58 54 0a 2a 2a 20 20 27 62 27 20 20 20 20  TEXT.**  'b'    
26247 20 20 20 20 20 20 20 20 4e 4f 4e 45 0a 2a 2a 20          NONE.** 
26248 20 27 63 27 20 20 20 20 20 20 20 20 20 20 20 20   'c'            
26249 4e 55 4d 45 52 49 43 0a 2a 2a 20 20 27 64 27 20  NUMERIC.**  'd' 
2624a 20 20 20 20 20 20 20 20 20 20 20 49 4e 54 45 47             INTEG
2624b 45 52 0a 2a 2a 20 20 27 65 27 20 20 20 20 20 20  ER.**  'e'      
2624c 20 20 20 20 20 20 52 45 41 4c 0a 2a 2a 0a 2a 2a        REAL.**.**
2624d 20 41 6e 20 65 78 74 72 61 20 27 62 27 20 69 73   An extra 'b' is
2624e 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65   appended to the
2624f 20 65 6e 64 20 6f 66 20 74 68 65 20 73 74 72 69   end of the stri
26250 6e 67 20 74 6f 20 63 6f 76 65 72 20 74 68 65 0a  ng to cover the.
26251 2a 2a 20 72 6f 77 69 64 20 74 68 61 74 20 61 70  ** rowid that ap
26252 70 65 61 72 73 20 61 73 20 74 68 65 20 6c 61 73  pears as the las
26253 74 20 63 6f 6c 75 6d 6e 20 69 6e 20 65 76 65 72  t column in ever
26254 79 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 4d  y index..**.** M
26255 65 6d 6f 72 79 20 66 6f 72 20 74 68 65 20 62 75  emory for the bu
26256 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ffer containing 
26257 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78  the column index
26258 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67   affinity string
26259 0a 2a 2a 20 69 73 20 6d 61 6e 61 67 65 64 20 61  .** is managed a
2625a 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 72 65  long with the re
2625b 73 74 20 6f 66 20 74 68 65 20 49 6e 64 65 78 20  st of the Index 
2625c 73 74 72 75 63 74 75 72 65 2e 20 49 74 20 77 69  structure. It wi
2625d 6c 6c 20 62 65 0a 2a 2a 20 72 65 6c 65 61 73 65  ll be.** release
2625e 64 20 77 68 65 6e 20 73 71 6c 69 74 65 33 44 65  d when sqlite3De
2625f 6c 65 74 65 49 6e 64 65 78 28 29 20 69 73 20 63  leteIndex() is c
26260 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  alled..*/.SQLITE
26261 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
26262 68 61 72 20 2a 73 71 6c 69 74 65 33 49 6e 64 65  har *sqlite3Inde
26263 78 41 66 66 69 6e 69 74 79 53 74 72 28 56 64 62  xAffinityStr(Vdb
26264 65 20 2a 76 2c 20 49 6e 64 65 78 20 2a 70 49 64  e *v, Index *pId
26265 78 29 7b 0a 20 20 69 66 28 20 21 70 49 64 78 2d  x){.  if( !pIdx-
26266 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20  >zColAff ){.    
26267 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74 69 6d  /* The first tim
26268 65 20 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e  e a column affin
26269 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20 61  ity string for a
2626a 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e 64 65   particular inde
2626b 78 20 69 73 0a 20 20 20 20 2a 2a 20 72 65 71 75  x is.    ** requ
2626c 69 72 65 64 2c 20 69 74 20 69 73 20 61 6c 6c 6f  ired, it is allo
2626d 63 61 74 65 64 20 61 6e 64 20 70 6f 70 75 6c 61  cated and popula
2626e 74 65 64 20 68 65 72 65 2e 20 49 74 20 69 73 20  ted here. It is 
2626f 74 68 65 6e 20 73 74 6f 72 65 64 20 61 73 0a 20  then stored as. 
26270 20 20 20 2a 2a 20 61 20 6d 65 6d 62 65 72 20 6f     ** a member o
26271 66 20 74 68 65 20 49 6e 64 65 78 20 73 74 72 75  f the Index stru
26272 63 74 75 72 65 20 66 6f 72 20 73 75 62 73 65 71  cture for subseq
26273 75 65 6e 74 20 75 73 65 2e 0a 20 20 20 20 2a 2a  uent use..    **
26274 0a 20 20 20 20 2a 2a 20 54 68 65 20 63 6f 6c 75  .    ** The colu
26275 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69  mn affinity stri
26276 6e 67 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c  ng will eventual
26277 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20 62 79  ly be deleted by
26278 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 44 65  .    ** sqliteDe
26279 6c 65 74 65 49 6e 64 65 78 28 29 20 77 68 65 6e  leteIndex() when
2627a 20 74 68 65 20 49 6e 64 65 78 20 73 74 72 75 63   the Index struc
2627b 74 75 72 65 20 69 74 73 65 6c 66 20 69 73 20 63  ture itself is c
2627c 6c 65 61 6e 65 64 0a 20 20 20 20 2a 2a 20 75 70  leaned.    ** up
2627d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74  ..    */.    int
2627e 20 6e 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70   n;.    Table *p
2627f 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54 61 62  Tab = pIdx->pTab
26280 6c 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20  le;.    sqlite3 
26281 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 56 64 62  *db = sqlite3Vdb
26282 65 44 62 28 76 29 3b 0a 20 20 20 20 70 49 64 78  eDb(v);.    pIdx
26283 2d 3e 7a 43 6f 6c 41 66 66 20 3d 20 28 63 68 61  ->zColAff = (cha
26284 72 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  r *)sqlite3Mallo
26285 63 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b  c(pIdx->nColumn+
26286 32 29 3b 0a 20 20 20 20 69 66 28 20 21 70 49 64  2);.    if( !pId
26287 78 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20  x->zColAff ){.  
26288 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
26289 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
2628a 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a  return 0;.    }.
2628b 20 20 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 70      for(n=0; n<p
2628c 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6e 2b  Idx->nColumn; n+
2628d 2b 29 7b 0a 20 20 20 20 20 20 70 49 64 78 2d 3e  +){.      pIdx->
2628e 7a 43 6f 6c 41 66 66 5b 6e 5d 20 3d 20 70 54 61  zColAff[n] = pTa
2628f 62 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d 3e 61 69  b->aCol[pIdx->ai
26290 43 6f 6c 75 6d 6e 5b 6e 5d 5d 2e 61 66 66 69 6e  Column[n]].affin
26291 69 74 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ity;.    }.    p
26292 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 5b 6e 2b 2b  Idx->zColAff[n++
26293 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ] = SQLITE_AFF_N
26294 4f 4e 45 3b 0a 20 20 20 20 70 49 64 78 2d 3e 7a  ONE;.    pIdx->z
26295 43 6f 6c 41 66 66 5b 6e 5d 20 3d 20 30 3b 0a 20  ColAff[n] = 0;. 
26296 20 7d 0a 20 0a 20 20 72 65 74 75 72 6e 20 70 49   }. .  return pI
26297 64 78 2d 3e 7a 43 6f 6c 41 66 66 3b 0a 7d 0a 0a  dx->zColAff;.}..
26298 2f 2a 0a 2a 2a 20 53 65 74 20 50 34 20 6f 66 20  /*.** Set P4 of 
26299 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  the most recentl
2629a 79 20 69 6e 73 65 72 74 65 64 20 6f 70 63 6f 64  y inserted opcod
2629b 65 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 61 66  e to a column af
2629c 66 69 6e 69 74 79 0a 2a 2a 20 73 74 72 69 6e 67  finity.** string
2629d 20 66 6f 72 20 74 61 62 6c 65 20 70 54 61 62 2e   for table pTab.
2629e 20 41 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69   A column affini
2629f 74 79 20 73 74 72 69 6e 67 20 68 61 73 20 6f 6e  ty string has on
262a0 65 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 66  e character.** f
262a1 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 69  or each column i
262a2 6e 64 65 78 65 64 20 62 79 20 74 68 65 20 69 6e  ndexed by the in
262a3 64 65 78 2c 20 61 63 63 6f 72 64 69 6e 67 20 74  dex, according t
262a4 6f 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f  o the affinity o
262a5 66 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 3a  f the.** column:
262a6 0a 2a 2a 0a 2a 2a 20 20 43 68 61 72 61 63 74 65  .**.**  Characte
262a7 72 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 61 66  r      Column af
262a8 66 69 6e 69 74 79 0a 2a 2a 20 20 2d 2d 2d 2d 2d  finity.**  -----
262a9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
262aa 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 27 61  ---------.**  'a
262ab 27 20 20 20 20 20 20 20 20 20 20 20 20 54 45 58  '            TEX
262ac 54 0a 2a 2a 20 20 27 62 27 20 20 20 20 20 20 20  T.**  'b'       
262ad 20 20 20 20 20 4e 4f 4e 45 0a 2a 2a 20 20 27 63       NONE.**  'c
262ae 27 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4d  '            NUM
262af 45 52 49 43 0a 2a 2a 20 20 27 64 27 20 20 20 20  ERIC.**  'd'    
262b0 20 20 20 20 20 20 20 20 49 4e 54 45 47 45 52 0a          INTEGER.
262b1 2a 2a 20 20 27 65 27 20 20 20 20 20 20 20 20 20  **  'e'         
262b2 20 20 20 52 45 41 4c 0a 2a 2f 0a 53 51 4c 49 54     REAL.*/.SQLIT
262b3 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
262b4 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69 6e  qlite3TableAffin
262b5 69 74 79 53 74 72 28 56 64 62 65 20 2a 76 2c 20  ityStr(Vdbe *v, 
262b6 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20  Table *pTab){.  
262b7 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74 69 6d  /* The first tim
262b8 65 20 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e  e a column affin
262b9 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20 61  ity string for a
262ba 20 70 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c   particular tabl
262bb 65 0a 20 20 2a 2a 20 69 73 20 72 65 71 75 69 72  e.  ** is requir
262bc 65 64 2c 20 69 74 20 69 73 20 61 6c 6c 6f 63 61  ed, it is alloca
262bd 74 65 64 20 61 6e 64 20 70 6f 70 75 6c 61 74 65  ted and populate
262be 64 20 68 65 72 65 2e 20 49 74 20 69 73 20 74 68  d here. It is th
262bf 65 6e 20 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20  en .  ** stored 
262c0 61 73 20 61 20 6d 65 6d 62 65 72 20 6f 66 20 74  as a member of t
262c1 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  he Table structu
262c2 72 65 20 66 6f 72 20 73 75 62 73 65 71 75 65 6e  re for subsequen
262c3 74 20 75 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  t use..  **.  **
262c4 20 54 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69   The column affi
262c5 6e 69 74 79 20 73 74 72 69 6e 67 20 77 69 6c 6c  nity string will
262c6 20 65 76 65 6e 74 75 61 6c 6c 79 20 62 65 20 64   eventually be d
262c7 65 6c 65 74 65 64 20 62 79 0a 20 20 2a 2a 20 73  eleted by.  ** s
262c8 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c  qlite3DeleteTabl
262c9 65 28 29 20 77 68 65 6e 20 74 68 65 20 54 61 62  e() when the Tab
262ca 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 74 73  le structure its
262cb 65 6c 66 20 69 73 20 63 6c 65 61 6e 65 64 20 75  elf is cleaned u
262cc 70 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70  p..  */.  if( !p
262cd 54 61 62 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a  Tab->zColAff ){.
262ce 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66      char *zColAf
262cf 66 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  f;.    int i;.  
262d0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
262d1 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29  sqlite3VdbeDb(v)
262d2 3b 0a 0a 20 20 20 20 7a 43 6f 6c 41 66 66 20 3d  ;..    zColAff =
262d3 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33   (char *)sqlite3
262d4 4d 61 6c 6c 6f 63 28 70 54 61 62 2d 3e 6e 43 6f  Malloc(pTab->nCo
262d5 6c 2b 31 29 3b 0a 20 20 20 20 69 66 28 20 21 7a  l+1);.    if( !z
262d6 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20 20 20  ColAff ){.      
262d7 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
262d8 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75   = 1;.      retu
262d9 72 6e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 66  rn;.    }..    f
262da 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e  or(i=0; i<pTab->
262db 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
262dc 20 20 7a 43 6f 6c 41 66 66 5b 69 5d 20 3d 20 70    zColAff[i] = p
262dd 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 61 66 66  Tab->aCol[i].aff
262de 69 6e 69 74 79 3b 0a 20 20 20 20 7d 0a 20 20 20  inity;.    }.   
262df 20 7a 43 6f 6c 41 66 66 5b 70 54 61 62 2d 3e 6e   zColAff[pTab->n
262e0 43 6f 6c 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20  Col] = '\0';..  
262e1 20 20 70 54 61 62 2d 3e 7a 43 6f 6c 41 66 66 20    pTab->zColAff 
262e2 3d 20 7a 43 6f 6c 41 66 66 3b 0a 20 20 7d 0a 0a  = zColAff;.  }..
262e3 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
262e4 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70 54 61  ngeP4(v, -1, pTa
262e5 62 2d 3e 7a 43 6f 6c 41 66 66 2c 20 30 29 3b 0a  b->zColAff, 0);.
262e6 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
262e7 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68 65 20  non-zero if the 
262e8 74 61 62 6c 65 20 70 54 61 62 20 69 6e 20 64 61  table pTab in da
262e9 74 61 62 61 73 65 20 69 44 62 20 6f 72 20 61 6e  tabase iDb or an
262ea 79 20 6f 66 20 69 74 73 20 69 6e 64 69 63 65 73  y of its indices
262eb 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 6f 70  .** have been op
262ec 65 6e 65 64 20 61 74 20 61 6e 79 20 70 6f 69 6e  ened at any poin
262ed 74 20 69 6e 20 74 68 65 20 56 44 42 45 20 70 72  t in the VDBE pr
262ee 6f 67 72 61 6d 20 62 65 67 69 6e 6e 69 6e 67 20  ogram beginning 
262ef 61 74 20 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69  at location.** i
262f0 53 74 61 72 74 41 64 64 72 20 74 68 72 6f 75 67  StartAddr throug
262f1 68 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  ht the end of th
262f2 65 20 70 72 6f 67 72 61 6d 2e 20 20 54 68 69 73  e program.  This
262f3 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 65 20   is used to see 
262f4 69 66 20 0a 2a 2a 20 61 20 73 74 61 74 65 6d 65  if .** a stateme
262f5 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 20  nt of the form  
262f6 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 3c 69 44  "INSERT INTO <iD
262f7 62 2c 20 70 54 61 62 3e 20 53 45 4c 45 43 54 20  b, pTab> SELECT 
262f8 2e 2e 2e 22 20 63 61 6e 20 0a 2a 2a 20 72 75 6e  ..." can .** run
262f9 20 77 69 74 68 6f 75 74 20 75 73 69 6e 67 20 74   without using t
262fa 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 66  emporary table f
262fb 6f 72 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f  or the results o
262fc 66 20 74 68 65 20 53 45 4c 45 43 54 2e 20 0a 2a  f the SELECT. .*
262fd 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61  /.static int rea
262fe 64 73 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70  dsTable(Parse *p
262ff 2c 20 69 6e 74 20 69 53 74 61 72 74 41 64 64 72  , int iStartAddr
26300 2c 20 69 6e 74 20 69 44 62 2c 20 54 61 62 6c 65  , int iDb, Table
26301 20 2a 70 54 61 62 29 7b 0a 20 20 56 64 62 65 20   *pTab){.  Vdbe 
26302 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56  *v = sqlite3GetV
26303 64 62 65 28 70 29 3b 0a 20 20 69 6e 74 20 69 3b  dbe(p);.  int i;
26304 0a 20 20 69 6e 74 20 69 45 6e 64 20 3d 20 73 71  .  int iEnd = sq
26305 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
26306 41 64 64 72 28 76 29 3b 0a 23 69 66 6e 64 65 66  Addr(v);.#ifndef
26307 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
26308 54 55 41 4c 54 41 42 4c 45 0a 20 20 56 54 61 62  TUALTABLE.  VTab
26309 6c 65 20 2a 70 56 54 61 62 20 3d 20 49 73 56 69  le *pVTab = IsVi
2630a 72 74 75 61 6c 28 70 54 61 62 29 20 3f 20 73 71  rtual(pTab) ? sq
2630b 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 70  lite3GetVTable(p
2630c 2d 3e 64 62 2c 20 70 54 61 62 29 20 3a 20 30 3b  ->db, pTab) : 0;
2630d 0a 23 65 6e 64 69 66 0a 0a 20 20 66 6f 72 28 69  .#endif..  for(i
2630e 3d 69 53 74 61 72 74 41 64 64 72 3b 20 69 3c 69  =iStartAddr; i<i
2630f 45 6e 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 56  End; i++){.    V
26310 64 62 65 4f 70 20 2a 70 4f 70 20 3d 20 73 71 6c  dbeOp *pOp = sql
26311 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c  ite3VdbeGetOp(v,
26312 20 69 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28   i);.    assert(
26313 20 70 4f 70 21 3d 30 20 29 3b 0a 20 20 20 20 69   pOp!=0 );.    i
26314 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  f( pOp->opcode==
26315 4f 50 5f 4f 70 65 6e 52 65 61 64 20 26 26 20 70  OP_OpenRead && p
26316 4f 70 2d 3e 70 33 3d 3d 69 44 62 20 29 7b 0a 20  Op->p3==iDb ){. 
26317 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e 64       Index *pInd
26318 65 78 3b 0a 20 20 20 20 20 20 69 6e 74 20 74 6e  ex;.      int tn
26319 75 6d 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20  um = pOp->p2;.  
2631a 20 20 20 20 69 66 28 20 74 6e 75 6d 3d 3d 70 54      if( tnum==pT
2631b 61 62 2d 3e 74 6e 75 6d 20 29 7b 0a 20 20 20 20  ab->tnum ){.    
2631c 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2631d 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28      }.      for(
2631e 70 49 6e 64 65 78 3d 70 54 61 62 2d 3e 70 49 6e  pIndex=pTab->pIn
2631f 64 65 78 3b 20 70 49 6e 64 65 78 3b 20 70 49 6e  dex; pIndex; pIn
26320 64 65 78 3d 70 49 6e 64 65 78 2d 3e 70 4e 65 78  dex=pIndex->pNex
26321 74 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  t){.        if( 
26322 74 6e 75 6d 3d 3d 70 49 6e 64 65 78 2d 3e 74 6e  tnum==pIndex->tn
26323 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  um ){.          
26324 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20  return 1;.      
26325 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
26326 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
26327 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
26328 4c 45 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  LE.    if( pOp->
26329 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 4f 70 65 6e  opcode==OP_VOpen
2632a 20 26 26 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61   && pOp->p4.pVta
2632b 62 3d 3d 70 56 54 61 62 20 29 7b 0a 20 20 20 20  b==pVTab ){.    
2632c 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
2632d 34 2e 70 56 74 61 62 21 3d 30 20 29 3b 0a 20 20  4.pVtab!=0 );.  
2632e 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
2632f 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 54 41 42  >p4type==P4_VTAB
26330 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   );.      return
26331 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66   1;.    }.#endif
26332 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
26333 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
26334 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52  TE_OMIT_AUTOINCR
26335 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61  EMENT./*.** Loca
26336 74 65 20 6f 72 20 63 72 65 61 74 65 20 61 6e 20  te or create an 
26337 41 75 74 6f 69 6e 63 49 6e 66 6f 20 73 74 72 75  AutoincInfo stru
26338 63 74 75 72 65 20 61 73 73 6f 63 69 61 74 65 64  cture associated
26339 20 77 69 74 68 20 74 61 62 6c 65 20 70 54 61 62   with table pTab
2633a 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 69 6e 20  .** which is in 
2633b 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 20 52  database iDb.  R
2633c 65 74 75 72 6e 20 74 68 65 20 72 65 67 69 73 74  eturn the regist
2633d 65 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  er number for th
2633e 65 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 74 68  e register.** th
2633f 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6d 61 78  at holds the max
26340 69 6d 75 6d 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a  imum rowid..**.*
26341 2a 20 54 68 65 72 65 20 69 73 20 61 74 20 6d 6f  * There is at mo
26342 73 74 20 6f 6e 65 20 41 75 74 6f 69 6e 63 49 6e  st one AutoincIn
26343 66 6f 20 73 74 72 75 63 74 75 72 65 20 70 65 72  fo structure per
26344 20 74 61 62 6c 65 20 65 76 65 6e 20 69 66 20 74   table even if t
26345 68 65 0a 2a 2a 20 73 61 6d 65 20 74 61 62 6c 65  he.** same table
26346 20 69 73 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e   is autoincremen
26347 74 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d  ted multiple tim
26348 65 73 20 64 75 65 20 74 6f 20 69 6e 73 65 72 74  es due to insert
26349 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 72 69 67  s within.** trig
2634a 67 65 72 73 2e 20 20 41 20 6e 65 77 20 41 75 74  gers.  A new Aut
2634b 6f 69 6e 63 49 6e 66 6f 20 73 74 72 75 63 74 75  oincInfo structu
2634c 72 65 20 69 73 20 63 72 65 61 74 65 64 20 69 66  re is created if
2634d 20 74 68 69 73 20 69 73 20 74 68 65 0a 2a 2a 20   this is the.** 
2634e 66 69 72 73 74 20 75 73 65 20 6f 66 20 74 61 62  first use of tab
2634f 6c 65 20 70 54 61 62 2e 20 20 4f 6e 20 32 6e 64  le pTab.  On 2nd
26350 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20   and subsequent 
26351 75 73 65 73 2c 20 74 68 65 20 6f 72 69 67 69 6e  uses, the origin
26352 61 6c 0a 2a 2a 20 41 75 74 6f 69 6e 63 49 6e 66  al.** AutoincInf
26353 6f 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75  o structure is u
26354 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 72 65 65  sed..**.** Three
26355 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e   memory location
26356 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 3a  s are allocated:
26357 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 20 52 65  .**.**   (1)  Re
26358 67 69 73 74 65 72 20 74 6f 20 68 6f 6c 64 20 74  gister to hold t
26359 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 70  he name of the p
2635a 54 61 62 20 74 61 62 6c 65 2e 0a 2a 2a 20 20 20  Tab table..**   
2635b 28 32 29 20 20 52 65 67 69 73 74 65 72 20 74 6f  (2)  Register to
2635c 20 68 6f 6c 64 20 74 68 65 20 6d 61 78 69 6d 75   hold the maximu
2635d 6d 20 52 4f 57 49 44 20 6f 66 20 70 54 61 62 2e  m ROWID of pTab.
2635e 0a 2a 2a 20 20 20 28 33 29 20 20 52 65 67 69 73  .**   (3)  Regis
2635f 74 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ter to hold the 
26360 72 6f 77 69 64 20 69 6e 20 73 71 6c 69 74 65 5f  rowid in sqlite_
26361 73 65 71 75 65 6e 63 65 20 6f 66 20 70 54 61 62  sequence of pTab
26362 0a 2a 2a 0a 2a 2a 20 54 68 65 20 32 6e 64 20 72  .**.** The 2nd r
26363 65 67 69 73 74 65 72 20 69 73 20 74 68 65 20 6f  egister is the o
26364 6e 65 20 74 68 61 74 20 69 73 20 72 65 74 75 72  ne that is retur
26365 6e 65 64 2e 20 20 54 68 61 74 20 69 73 20 61 6c  ned.  That is al
26366 6c 20 74 68 65 0a 2a 2a 20 69 6e 73 65 72 74 20  l the.** insert 
26367 72 6f 75 74 69 6e 65 20 6e 65 65 64 73 20 74 6f  routine needs to
26368 20 6b 6e 6f 77 20 61 62 6f 75 74 2e 0a 2a 2f 0a   know about..*/.
26369 73 74 61 74 69 63 20 69 6e 74 20 61 75 74 6f 49  static int autoI
2636a 6e 63 42 65 67 69 6e 28 0a 20 20 50 61 72 73 65  ncBegin(.  Parse
2636b 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f   *pParse,      /
2636c 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
2636d 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20  t */.  int iDb, 
2636e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
2636f 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62  dex of the datab
26370 61 73 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62  ase holding pTab
26371 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
26372 62 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  b         /* The
26373 20 74 61 62 6c 65 20 77 65 20 61 72 65 20 77 72   table we are wr
26374 69 74 69 6e 67 20 74 6f 20 2a 2f 0a 29 7b 0a 20  iting to */.){. 
26375 20 69 6e 74 20 6d 65 6d 49 64 20 3d 20 30 3b 20   int memId = 0; 
26376 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
26377 20 68 6f 6c 64 69 6e 67 20 6d 61 78 69 6d 75 6d   holding maximum
26378 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 66 28 20   rowid */.  if( 
26379 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26  pTab->tabFlags &
2637a 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e   TF_Autoincremen
2637b 74 20 29 7b 0a 20 20 20 20 50 61 72 73 65 20 2a  t ){.    Parse *
2637c 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69  pToplevel = sqli
2637d 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c  te3ParseToplevel
2637e 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 41 75  (pParse);.    Au
2637f 74 6f 69 6e 63 49 6e 66 6f 20 2a 70 49 6e 66 6f  toincInfo *pInfo
26380 3b 0a 0a 20 20 20 20 70 49 6e 66 6f 20 3d 20 70  ;..    pInfo = p
26381 54 6f 70 6c 65 76 65 6c 2d 3e 70 41 69 6e 63 3b  Toplevel->pAinc;
26382 0a 20 20 20 20 77 68 69 6c 65 28 20 70 49 6e 66  .    while( pInf
26383 6f 20 26 26 20 70 49 6e 66 6f 2d 3e 70 54 61 62  o && pInfo->pTab
26384 21 3d 70 54 61 62 20 29 7b 20 70 49 6e 66 6f 20  !=pTab ){ pInfo 
26385 3d 20 70 49 6e 66 6f 2d 3e 70 4e 65 78 74 3b 20  = pInfo->pNext; 
26386 7d 0a 20 20 20 20 69 66 28 20 70 49 6e 66 6f 3d  }.    if( pInfo=
26387 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 66  =0 ){.      pInf
26388 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  o = sqlite3DbMal
26389 6c 6f 63 52 61 77 28 70 50 61 72 73 65 2d 3e 64  locRaw(pParse->d
2638a 62 2c 20 73 69 7a 65 6f 66 28 2a 70 49 6e 66 6f  b, sizeof(*pInfo
2638b 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49  ));.      if( pI
2638c 6e 66 6f 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  nfo==0 ) return 
2638d 30 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e  0;.      pInfo->
2638e 70 4e 65 78 74 20 3d 20 70 54 6f 70 6c 65 76 65  pNext = pTopleve
2638f 6c 2d 3e 70 41 69 6e 63 3b 0a 20 20 20 20 20 20  l->pAinc;.      
26390 70 54 6f 70 6c 65 76 65 6c 2d 3e 70 41 69 6e 63  pToplevel->pAinc
26391 20 3d 20 70 49 6e 66 6f 3b 0a 20 20 20 20 20 20   = pInfo;.      
26392 70 49 6e 66 6f 2d 3e 70 54 61 62 20 3d 20 70 54  pInfo->pTab = pT
26393 61 62 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d  ab;.      pInfo-
26394 3e 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20  >iDb = iDb;.    
26395 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 4d 65    pToplevel->nMe
26396 6d 2b 2b 3b 20 20 20 20 20 20 20 20 20 20 20 20  m++;            
26397 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
26398 72 20 74 6f 20 68 6f 6c 64 20 6e 61 6d 65 20 6f  r to hold name o
26399 66 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20  f table */.     
2639a 20 70 49 6e 66 6f 2d 3e 72 65 67 43 74 72 20 3d   pInfo->regCtr =
2639b 20 2b 2b 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 4d   ++pToplevel->nM
2639c 65 6d 3b 20 20 2f 2a 20 4d 61 78 20 72 6f 77 69  em;  /* Max rowi
2639d 64 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20  d register */.  
2639e 20 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e      pToplevel->n
2639f 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20 20 20  Mem++;          
263a0 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64          /* Rowid
263a1 20 69 6e 20 73 71 6c 69 74 65 5f 73 65 71 75 65   in sqlite_seque
263a2 6e 63 65 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20  nce */.    }.   
263a3 20 6d 65 6d 49 64 20 3d 20 70 49 6e 66 6f 2d 3e   memId = pInfo->
263a4 72 65 67 43 74 72 3b 0a 20 20 7d 0a 20 20 72 65  regCtr;.  }.  re
263a5 74 75 72 6e 20 6d 65 6d 49 64 3b 0a 7d 0a 0a 2f  turn memId;.}../
263a6 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
263a7 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65  e generates code
263a8 20 74 68 61 74 20 77 69 6c 6c 20 69 6e 69 74 69   that will initi
263a9 61 6c 69 7a 65 20 61 6c 6c 20 6f 66 20 74 68 65  alize all of the
263aa 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 75 73 65  .** register use
263ab 64 20 62 79 20 74 68 65 20 61 75 74 6f 69 6e 63  d by the autoinc
263ac 72 65 6d 65 6e 74 20 74 72 61 63 6b 65 72 2e 20  rement tracker. 
263ad 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
263ae 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
263af 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65 67  AutoincrementBeg
263b0 69 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  in(Parse *pParse
263b1 29 7b 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66 6f  ){.  AutoincInfo
263b2 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20   *p;            
263b3 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  /* Information a
263b4 62 6f 75 74 20 61 6e 20 41 55 54 4f 49 4e 43 52  bout an AUTOINCR
263b5 45 4d 45 4e 54 20 2a 2f 0a 20 20 73 71 6c 69 74  EMENT */.  sqlit
263b6 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
263b7 3e 64 62 3b 20 20 2f 2a 20 54 68 65 20 64 61 74  >db;  /* The dat
263b8 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
263b9 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62 3b 20 20   */.  Db *pDb;  
263ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
263bb 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6f 6e 6c   /* Database onl
263bc 79 20 61 75 74 6f 69 6e 63 20 74 61 62 6c 65 20  y autoinc table 
263bd 2a 2f 0a 20 20 69 6e 74 20 6d 65 6d 49 64 3b 20  */.  int memId; 
263be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
263bf 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64  /* Register hold
263c0 69 6e 67 20 6d 61 78 20 72 6f 77 69 64 20 2a 2f  ing max rowid */
263c1 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20  .  int addr;    
263c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
263c3 20 41 20 56 44 42 45 20 61 64 64 72 65 73 73 20   A VDBE address 
263c4 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  */.  Vdbe *v = p
263c5 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 20  Parse->pVdbe;   
263c6 2f 2a 20 56 44 42 45 20 75 6e 64 65 72 20 63 6f  /* VDBE under co
263c7 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 0a 20  nstruction */.. 
263c8 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65   /* This routine
263c9 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64   is never called
263ca 20 64 75 72 69 6e 67 20 74 72 69 67 67 65 72 2d   during trigger-
263cb 67 65 6e 65 72 61 74 69 6f 6e 2e 20 20 49 74 20  generation.  It 
263cc 69 73 0a 20 20 2a 2a 20 6f 6e 6c 79 20 63 61 6c  is.  ** only cal
263cd 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 74 6f 70  led from the top
263ce 2d 6c 65 76 65 6c 20 2a 2f 0a 20 20 61 73 73 65  -level */.  asse
263cf 72 74 28 20 70 50 61 72 73 65 2d 3e 70 54 72 69  rt( pParse->pTri
263d0 67 67 65 72 54 61 62 3d 3d 30 20 29 3b 0a 20 20  ggerTab==0 );.  
263d1 61 73 73 65 72 74 28 20 70 50 61 72 73 65 3d 3d  assert( pParse==
263d2 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c  sqlite3ParseTopl
263d3 65 76 65 6c 28 70 50 61 72 73 65 29 20 29 3b 0a  evel(pParse) );.
263d4 0a 20 20 61 73 73 65 72 74 28 20 76 20 29 3b 20  .  assert( v ); 
263d5 20 20 2f 2a 20 57 65 20 66 61 69 6c 65 64 20 6c    /* We failed l
263d6 6f 6e 67 20 61 67 6f 20 69 66 20 74 68 69 73 20  ong ago if this 
263d7 69 73 20 6e 6f 74 20 73 6f 20 2a 2f 0a 20 20 66  is not so */.  f
263d8 6f 72 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70  or(p = pParse->p
263d9 41 69 6e 63 3b 20 70 3b 20 70 20 3d 20 70 2d 3e  Ainc; p; p = p->
263da 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 44 62 20  pNext){.    pDb 
263db 3d 20 26 64 62 2d 3e 61 44 62 5b 70 2d 3e 69 44  = &db->aDb[p->iD
263dc 62 5d 3b 0a 20 20 20 20 6d 65 6d 49 64 20 3d 20  b];.    memId = 
263dd 70 2d 3e 72 65 67 43 74 72 3b 0a 20 20 20 20 73  p->regCtr;.    s
263de 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28  qlite3OpenTable(
263df 70 50 61 72 73 65 2c 20 30 2c 20 70 2d 3e 69 44  pParse, 0, p->iD
263e0 62 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  b, pDb->pSchema-
263e1 3e 70 53 65 71 54 61 62 2c 20 4f 50 5f 4f 70 65  >pSeqTab, OP_Ope
263e2 6e 52 65 61 64 29 3b 0a 20 20 20 20 61 64 64 72  nRead);.    addr
263e3 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
263e4 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20  rrentAddr(v);.  
263e5 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
263e6 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
263e7 38 2c 20 30 2c 20 6d 65 6d 49 64 2d 31 2c 20 30  8, 0, memId-1, 0
263e8 2c 20 70 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65  , p->pTab->zName
263e9 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
263ea 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
263eb 50 5f 52 65 77 69 6e 64 2c 20 30 2c 20 61 64 64  P_Rewind, 0, add
263ec 72 2b 39 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  r+9);.    sqlite
263ed 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
263ee 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c 20  P_Column, 0, 0, 
263ef 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73 71 6c 69  memId);.    sqli
263f0 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
263f1 20 4f 50 5f 4e 65 2c 20 6d 65 6d 49 64 2d 31 2c   OP_Ne, memId-1,
263f2 20 61 64 64 72 2b 37 2c 20 6d 65 6d 49 64 29 3b   addr+7, memId);
263f3 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
263f4 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49  ChangeP5(v, SQLI
263f5 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a  TE_JUMPIFNULL);.
263f6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
263f7 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69  ddOp2(v, OP_Rowi
263f8 64 2c 20 30 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a  d, 0, memId+1);.
263f9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
263fa 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
263fb 6d 6e 2c 20 30 2c 20 31 2c 20 6d 65 6d 49 64 29  mn, 0, 1, memId)
263fc 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
263fd 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
263fe 74 6f 2c 20 30 2c 20 61 64 64 72 2b 39 29 3b 0a  to, 0, addr+9);.
263ff 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26400 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74  ddOp2(v, OP_Next
26401 2c 20 30 2c 20 61 64 64 72 2b 32 29 3b 0a 20 20  , 0, addr+2);.  
26402 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26403 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
26404 72 2c 20 30 2c 20 6d 65 6d 49 64 29 3b 0a 20 20  r, 0, memId);.  
26405 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26406 4f 70 30 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 29  Op0(v, OP_Close)
26407 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55  ;.  }.}../*.** U
26408 70 64 61 74 65 20 74 68 65 20 6d 61 78 69 6d 75  pdate the maximu
26409 6d 20 72 6f 77 69 64 20 66 6f 72 20 61 6e 20 61  m rowid for an a
2640a 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 63 61 6c  utoincrement cal
2640b 63 75 6c 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  culation..**.** 
2640c 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f  This routine sho
2640d 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77 68  uld be called wh
2640e 65 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  en the top of th
2640f 65 20 73 74 61 63 6b 20 68 6f 6c 64 73 20 61 0a  e stack holds a.
26410 2a 2a 20 6e 65 77 20 72 6f 77 69 64 20 74 68 61  ** new rowid tha
26411 74 20 69 73 20 61 62 6f 75 74 20 74 6f 20 62 65  t is about to be
26412 20 69 6e 73 65 72 74 65 64 2e 20 20 49 66 20 74   inserted.  If t
26413 68 61 74 20 6e 65 77 20 72 6f 77 69 64 20 69 73  hat new rowid is
26414 0a 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20  .** larger than 
26415 74 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69  the maximum rowi
26416 64 20 69 6e 20 74 68 65 20 6d 65 6d 49 64 20 6d  d in the memId m
26417 65 6d 6f 72 79 20 63 65 6c 6c 2c 20 74 68 65 6e  emory cell, then
26418 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63   the.** memory c
26419 65 6c 6c 20 69 73 20 75 70 64 61 74 65 64 2e 20  ell is updated. 
2641a 20 54 68 65 20 73 74 61 63 6b 20 69 73 20 75 6e   The stack is un
2641b 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74 61 74  changed..*/.stat
2641c 69 63 20 76 6f 69 64 20 61 75 74 6f 49 6e 63 53  ic void autoIncS
2641d 74 65 70 28 50 61 72 73 65 20 2a 70 50 61 72 73  tep(Parse *pPars
2641e 65 2c 20 69 6e 74 20 6d 65 6d 49 64 2c 20 69 6e  e, int memId, in
2641f 74 20 72 65 67 52 6f 77 69 64 29 7b 0a 20 20 69  t regRowid){.  i
26420 66 28 20 6d 65 6d 49 64 3e 30 20 29 7b 0a 20 20  f( memId>0 ){.  
26421 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26422 4f 70 32 28 70 50 61 72 73 65 2d 3e 70 56 64 62  Op2(pParse->pVdb
26423 65 2c 20 4f 50 5f 4d 65 6d 4d 61 78 2c 20 6d 65  e, OP_MemMax, me
26424 6d 49 64 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  mId, regRowid);.
26425 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
26426 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61  s routine genera
26427 74 65 73 20 74 68 65 20 63 6f 64 65 20 6e 65 65  tes the code nee
26428 64 65 64 20 74 6f 20 77 72 69 74 65 20 61 75 74  ded to write aut
26429 6f 69 6e 63 72 65 6d 65 6e 74 0a 2a 2a 20 6d 61  oincrement.** ma
2642a 78 69 6d 75 6d 20 72 6f 77 69 64 20 76 61 6c 75  ximum rowid valu
2642b 65 73 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65  es back into the
2642c 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65   sqlite_sequence
2642d 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a 20 45 76   register..** Ev
2642e 65 72 79 20 73 74 61 74 65 6d 65 6e 74 20 74 68  ery statement th
2642f 61 74 20 6d 69 67 68 74 20 64 6f 20 61 6e 20 49  at might do an I
26430 4e 53 45 52 54 20 69 6e 74 6f 20 61 6e 20 61 75  NSERT into an au
26431 74 6f 69 6e 63 72 65 6d 65 6e 74 0a 2a 2a 20 74  toincrement.** t
26432 61 62 6c 65 20 28 65 69 74 68 65 72 20 64 69 72  able (either dir
26433 65 63 74 6c 79 20 6f 72 20 74 68 72 6f 75 67 68  ectly or through
26434 20 74 72 69 67 67 65 72 73 29 20 6e 65 65 64 73   triggers) needs
26435 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 0a 2a 2a   to call this.**
26436 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 20 62 65   routine just be
26437 66 6f 72 65 20 74 68 65 20 22 65 78 69 74 22 20  fore the "exit" 
26438 63 6f 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  code..*/.SQLITE_
26439 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2643a 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e  ite3Autoincremen
2643b 74 45 6e 64 28 50 61 72 73 65 20 2a 70 50 61 72  tEnd(Parse *pPar
2643c 73 65 29 7b 0a 20 20 41 75 74 6f 69 6e 63 49 6e  se){.  AutoincIn
2643d 66 6f 20 2a 70 3b 0a 20 20 56 64 62 65 20 2a 76  fo *p;.  Vdbe *v
2643e 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
2643f 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
26440 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20  = pParse->db;.. 
26441 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a 20 20   assert( v );.  
26442 66 6f 72 28 70 20 3d 20 70 50 61 72 73 65 2d 3e  for(p = pParse->
26443 70 41 69 6e 63 3b 20 70 3b 20 70 20 3d 20 70 2d  pAinc; p; p = p-
26444 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 44 62 20  >pNext){.    Db 
26445 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b  *pDb = &db->aDb[
26446 70 2d 3e 69 44 62 5d 3b 0a 20 20 20 20 69 6e 74  p->iDb];.    int
26447 20 6a 31 2c 20 6a 32 2c 20 6a 33 2c 20 6a 34 2c   j1, j2, j3, j4,
26448 20 6a 35 3b 0a 20 20 20 20 69 6e 74 20 69 52 65   j5;.    int iRe
26449 63 3b 0a 20 20 20 20 69 6e 74 20 6d 65 6d 49 64  c;.    int memId
2644a 20 3d 20 70 2d 3e 72 65 67 43 74 72 3b 0a 0a 20   = p->regCtr;.. 
2644b 20 20 20 69 52 65 63 20 3d 20 73 71 6c 69 74 65     iRec = sqlite
2644c 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
2644d 73 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  se);.    sqlite3
2644e 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65  OpenTable(pParse
2644f 2c 20 30 2c 20 70 2d 3e 69 44 62 2c 20 70 44 62  , 0, p->iDb, pDb
26450 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 54  ->pSchema->pSeqT
26451 61 62 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65  ab, OP_OpenWrite
26452 29 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69  );.    j1 = sqli
26453 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
26454 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 6d 65 6d   OP_NotNull, mem
26455 49 64 2b 31 29 3b 0a 20 20 20 20 6a 32 20 3d 20  Id+1);.    j2 = 
26456 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26457 30 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 29 3b  0(v, OP_Rewind);
26458 0a 20 20 20 20 6a 33 20 3d 20 73 71 6c 69 74 65  .    j3 = sqlite
26459 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2645a 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c 20  P_Column, 0, 0, 
2645b 69 52 65 63 29 3b 0a 20 20 20 20 6a 34 20 3d 20  iRec);.    j4 = 
2645c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2645d 33 28 76 2c 20 4f 50 5f 45 71 2c 20 6d 65 6d 49  3(v, OP_Eq, memI
2645e 64 2d 31 2c 20 30 2c 20 69 52 65 63 29 3b 0a 20  d-1, 0, iRec);. 
2645f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26460 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c  dOp2(v, OP_Next,
26461 20 30 2c 20 6a 33 29 3b 0a 20 20 20 20 73 71 6c   0, j3);.    sql
26462 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
26463 28 76 2c 20 6a 32 29 3b 0a 20 20 20 20 73 71 6c  (v, j2);.    sql
26464 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
26465 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 30  , OP_NewRowid, 0
26466 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a 20 20 20 20  , memId+1);.    
26467 6a 35 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j5 = sqlite3Vdbe
26468 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74  AddOp0(v, OP_Got
26469 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  o);.    sqlite3V
2646a 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
2646b 34 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  4);.    sqlite3V
2646c 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2646d 52 6f 77 69 64 2c 20 30 2c 20 6d 65 6d 49 64 2b  Rowid, 0, memId+
2646e 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
2646f 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
26470 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
26471 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
26472 35 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  5);.    sqlite3V
26473 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
26474 4d 61 6b 65 52 65 63 6f 72 64 2c 20 6d 65 6d 49  MakeRecord, memI
26475 64 2d 31 2c 20 32 2c 20 69 52 65 63 29 3b 0a 20  d-1, 2, iRec);. 
26476 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26477 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72  dOp3(v, OP_Inser
26478 74 2c 20 30 2c 20 69 52 65 63 2c 20 6d 65 6d 49  t, 0, iRec, memI
26479 64 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  d+1);.    sqlite
2647a 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
2647b 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b   OPFLAG_APPEND);
2647c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2647d 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp0(v, OP_Clo
2647e 73 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  se);.    sqlite3
2647f 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
26480 50 61 72 73 65 2c 20 69 52 65 63 29 3b 0a 20 20  Parse, iRec);.  
26481 7d 0a 7d 0a 23 65 6c 73 65 0a 2f 2a 0a 2a 2a 20  }.}.#else./*.** 
26482 49 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  If SQLITE_OMIT_A
26483 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 69 73 20  UTOINCREMENT is 
26484 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68  defined, then th
26485 65 20 74 68 72 65 65 20 72 6f 75 74 69 6e 65 73  e three routines
26486 0a 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 61 6c  .** above are al
26487 6c 20 6e 6f 2d 6f 70 73 0a 2a 2f 0a 23 20 64 65  l no-ops.*/.# de
26488 66 69 6e 65 20 61 75 74 6f 49 6e 63 42 65 67 69  fine autoIncBegi
26489 6e 28 41 2c 42 2c 43 29 20 28 30 29 0a 23 20 64  n(A,B,C) (0).# d
2648a 65 66 69 6e 65 20 61 75 74 6f 49 6e 63 53 74 65  efine autoIncSte
2648b 70 28 41 2c 42 2c 43 29 0a 23 65 6e 64 69 66 20  p(A,B,C).#endif 
2648c 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  /* SQLITE_OMIT_A
2648d 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a  UTOINCREMENT */.
2648e 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63  ../* Forward dec
2648f 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74  laration */.stat
26490 69 63 20 69 6e 74 20 78 66 65 72 4f 70 74 69 6d  ic int xferOptim
26491 69 7a 61 74 69 6f 6e 28 0a 20 20 50 61 72 73 65  ization(.  Parse
26492 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
26493 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65   /* Parser conte
26494 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  xt */.  Table *p
26495 44 65 73 74 2c 20 20 20 20 20 20 20 20 20 2f 2a  Dest,         /*
26496 20 54 68 65 20 74 61 62 6c 65 20 77 65 20 61 72   The table we ar
26497 65 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 74 6f  e inserting into
26498 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53   */.  Select *pS
26499 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 41  elect,      /* A
2649a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2649b 74 20 74 6f 20 75 73 65 20 61 73 20 74 68 65 20  t to use as the 
2649c 64 61 74 61 20 73 6f 75 72 63 65 20 2a 2f 0a 20  data source */. 
2649d 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20 20   int onError,   
2649e 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f         /* How to
2649f 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61 69   handle constrai
264a0 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69  nt errors */.  i
264a1 6e 74 20 69 44 62 44 65 73 74 20 20 20 20 20 20  nt iDbDest      
264a2 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
264a3 62 61 73 65 20 6f 66 20 70 44 65 73 74 20 2a 2f  base of pDest */
264a4 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  .);../*.** This 
264a5 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 20  routine is call 
264a6 74 6f 20 68 61 6e 64 6c 65 20 53 51 4c 20 6f 66  to handle SQL of
264a7 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   the following f
264a8 6f 72 6d 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 69  orms:.**.**    i
264a9 6e 73 65 72 74 20 69 6e 74 6f 20 54 41 42 4c 45  nsert into TABLE
264aa 20 28 49 44 4c 49 53 54 29 20 76 61 6c 75 65 73   (IDLIST) values
264ab 28 45 58 50 52 4c 49 53 54 29 0a 2a 2a 20 20 20  (EXPRLIST).**   
264ac 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 54 41 42   insert into TAB
264ad 4c 45 20 28 49 44 4c 49 53 54 29 20 73 65 6c 65  LE (IDLIST) sele
264ae 63 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 44 4c  ct.**.** The IDL
264af 49 53 54 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  IST following th
264b0 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20  e table name is 
264b1 61 6c 77 61 79 73 20 6f 70 74 69 6f 6e 61 6c 2e  always optional.
264b2 20 20 49 66 20 6f 6d 69 74 74 65 64 2c 0a 2a 2a    If omitted,.**
264b3 20 74 68 65 6e 20 61 20 6c 69 73 74 20 6f 66 20   then a list of 
264b4 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20  all columns for 
264b5 74 68 65 20 74 61 62 6c 65 20 69 73 20 73 75 62  the table is sub
264b6 73 74 69 74 75 74 65 64 2e 20 20 54 68 65 20 49  stituted.  The I
264b7 44 4c 49 53 54 0a 2a 2a 20 61 70 70 65 61 72 73  DLIST.** appears
264b8 20 69 6e 20 74 68 65 20 70 43 6f 6c 75 6d 6e 20   in the pColumn 
264b9 70 61 72 61 6d 65 74 65 72 2e 20 20 70 43 6f 6c  parameter.  pCol
264ba 75 6d 6e 20 69 73 20 4e 55 4c 4c 20 69 66 20 49  umn is NULL if I
264bb 44 4c 49 53 54 20 69 73 20 6f 6d 69 74 74 65 64  DLIST is omitted
264bc 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4c 69 73  ..**.** The pLis
264bd 74 20 70 61 72 61 6d 65 74 65 72 20 68 6f 6c 64  t parameter hold
264be 73 20 45 58 50 52 4c 49 53 54 20 69 6e 20 74 68  s EXPRLIST in th
264bf 65 20 66 69 72 73 74 20 66 6f 72 6d 20 6f 66 20  e first form of 
264c0 74 68 65 20 49 4e 53 45 52 54 0a 2a 2a 20 73 74  the INSERT.** st
264c1 61 74 65 6d 65 6e 74 20 61 62 6f 76 65 2c 20 61  atement above, a
264c2 6e 64 20 70 53 65 6c 65 63 74 20 69 73 20 4e 55  nd pSelect is NU
264c3 4c 4c 2e 20 20 46 6f 72 20 74 68 65 20 73 65 63  LL.  For the sec
264c4 6f 6e 64 20 66 6f 72 6d 2c 20 70 4c 69 73 74 20  ond form, pList 
264c5 69 73 0a 2a 2a 20 4e 55 4c 4c 20 61 6e 64 20 70  is.** NULL and p
264c6 53 65 6c 65 63 74 20 69 73 20 61 20 70 6f 69 6e  Select is a poin
264c7 74 65 72 20 74 6f 20 74 68 65 20 73 65 6c 65 63  ter to the selec
264c8 74 20 73 74 61 74 65 6d 65 6e 74 20 75 73 65 64  t statement used
264c9 20 74 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20   to generate.** 
264ca 64 61 74 61 20 66 6f 72 20 74 68 65 20 69 6e 73  data for the ins
264cb 65 72 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ert..**.** The c
264cc 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 66 6f  ode generated fo
264cd 6c 6c 6f 77 73 20 6f 6e 65 20 6f 66 20 66 6f 75  llows one of fou
264ce 72 20 74 65 6d 70 6c 61 74 65 73 2e 20 20 46 6f  r templates.  Fo
264cf 72 20 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 73 65  r a simple.** se
264d0 6c 65 63 74 20 77 69 74 68 20 64 61 74 61 20 63  lect with data c
264d1 6f 6d 69 6e 67 20 66 72 6f 6d 20 61 20 56 41 4c  oming from a VAL
264d2 55 45 53 20 63 6c 61 75 73 65 2c 20 74 68 65 20  UES clause, the 
264d3 63 6f 64 65 20 65 78 65 63 75 74 65 73 0a 2a 2a  code executes.**
264d4 20 6f 6e 63 65 20 73 74 72 61 69 67 68 74 20 64   once straight d
264d5 6f 77 6e 20 74 68 72 6f 75 67 68 2e 20 20 50 73  own through.  Ps
264d6 65 75 64 6f 2d 63 6f 64 65 20 66 6f 6c 6c 6f 77  eudo-code follow
264d7 73 20 28 77 65 20 63 61 6c 6c 20 74 68 69 73 0a  s (we call this.
264d8 2a 2a 20 74 68 65 20 22 31 73 74 20 74 65 6d 70  ** the "1st temp
264d9 6c 61 74 65 22 29 3a 0a 2a 2a 0a 2a 2a 20 20 20  late"):.**.**   
264da 20 20 20 20 20 20 6f 70 65 6e 20 77 72 69 74 65        open write
264db 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c   cursor to <tabl
264dc 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69 63  e> and its indic
264dd 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 70 75  es.**         pu
264de 74 73 20 56 41 4c 55 45 53 20 63 6c 61 75 73 65  ts VALUES clause
264df 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e 74   expressions ont
264e0 6f 20 74 68 65 20 73 74 61 63 6b 0a 2a 2a 20 20  o the stack.**  
264e1 20 20 20 20 20 20 20 77 72 69 74 65 20 74 68 65         write the
264e2 20 72 65 73 75 6c 74 69 6e 67 20 72 65 63 6f 72   resulting recor
264e3 64 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a  d into <table>.*
264e4 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75  *         cleanu
264e5 70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 72 65  p.**.** The thre
264e6 65 20 72 65 6d 61 69 6e 69 6e 67 20 74 65 6d 70  e remaining temp
264e7 6c 61 74 65 73 20 61 73 73 75 6d 65 20 74 68 65  lates assume the
264e8 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6f 66   statement is of
264e9 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20   the form.**.** 
264ea 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 3c 74    INSERT INTO <t
264eb 61 62 6c 65 3e 20 53 45 4c 45 43 54 20 2e 2e 2e  able> SELECT ...
264ec 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 45  .**.** If the SE
264ed 4c 45 43 54 20 63 6c 61 75 73 65 20 69 73 20 6f  LECT clause is o
264ee 66 20 74 68 65 20 72 65 73 74 72 69 63 74 65 64  f the restricted
264ef 20 66 6f 72 6d 20 22 53 45 4c 45 43 54 20 2a 20   form "SELECT * 
264f0 46 52 4f 4d 20 3c 74 61 62 6c 65 32 3e 22 20 2d  FROM <table2>" -
264f1 0a 2a 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f 72  .** in other wor
264f2 64 73 20 69 66 20 74 68 65 20 53 45 4c 45 43 54  ds if the SELECT
264f3 20 70 75 6c 6c 73 20 61 6c 6c 20 63 6f 6c 75 6d   pulls all colum
264f4 6e 73 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65  ns from a single
264f5 20 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68   table.** and th
264f6 65 72 65 20 69 73 20 6e 6f 20 57 48 45 52 45 20  ere is no WHERE 
264f7 6f 72 20 4c 49 4d 49 54 20 6f 72 20 47 52 4f 55  or LIMIT or GROU
264f8 50 20 42 59 20 6f 72 20 4f 52 44 45 52 20 42 59  P BY or ORDER BY
264f9 20 63 6c 61 75 73 65 73 2c 20 61 6e 64 0a 2a 2a   clauses, and.**
264fa 20 69 66 20 3c 74 61 62 6c 65 32 3e 20 61 6e 64   if <table2> and
264fb 20 3c 74 61 62 6c 65 31 3e 20 61 72 65 20 64 69   <table1> are di
264fc 73 74 69 6e 63 74 20 74 61 62 6c 65 73 20 62 75  stinct tables bu
264fd 74 20 68 61 76 65 20 69 64 65 6e 74 69 63 61 6c  t have identical
264fe 0a 2a 2a 20 73 63 68 65 6d 61 73 2c 20 69 6e 63  .** schemas, inc
264ff 6c 75 64 69 6e 67 20 61 6c 6c 20 74 68 65 20 73  luding all the s
26500 61 6d 65 20 69 6e 64 69 63 65 73 2c 20 74 68 65  ame indices, the
26501 6e 20 61 20 73 70 65 63 69 61 6c 20 6f 70 74 69  n a special opti
26502 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 69  mization.** is i
26503 6e 76 6f 6b 65 64 20 74 68 61 74 20 63 6f 70 69  nvoked that copi
26504 65 73 20 72 61 77 20 72 65 63 6f 72 64 73 20 66  es raw records f
26505 72 6f 6d 20 3c 74 61 62 6c 65 32 3e 20 6f 76 65  rom <table2> ove
26506 72 20 74 6f 20 3c 74 61 62 6c 65 31 3e 2e 0a 2a  r to <table1>..*
26507 2a 20 53 65 65 20 74 68 65 20 78 66 65 72 4f 70  * See the xferOp
26508 74 69 6d 69 7a 61 74 69 6f 6e 28 29 20 66 75 6e  timization() fun
26509 63 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 69 6d  ction for the im
2650a 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
2650b 74 68 69 73 0a 2a 2a 20 74 65 6d 70 6c 61 74 65  this.** template
2650c 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 32  .  This is the 2
2650d 6e 64 20 74 65 6d 70 6c 61 74 65 2e 0a 2a 2a 0a  nd template..**.
2650e 2a 2a 20 20 20 20 20 20 20 20 20 6f 70 65 6e 20  **         open 
2650f 61 20 77 72 69 74 65 20 63 75 72 73 6f 72 20 74  a write cursor t
26510 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20  o <table>.**    
26511 20 20 20 20 20 6f 70 65 6e 20 72 65 61 64 20 63       open read c
26512 75 72 73 6f 72 20 6f 6e 20 3c 74 61 62 6c 65 32  ursor on <table2
26513 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 74 72 61  >.**         tra
26514 6e 73 66 65 72 20 61 6c 6c 20 72 65 63 6f 72 64  nsfer all record
26515 73 20 69 6e 20 3c 74 61 62 6c 65 32 3e 20 6f 76  s in <table2> ov
26516 65 72 20 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a  er to <table>.**
26517 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 20 63           close c
26518 75 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20  ursors.**       
26519 20 20 66 6f 72 65 61 63 68 20 69 6e 64 65 78 20    foreach index 
2651a 6f 6e 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20  on <table>.**   
2651b 20 20 20 20 20 20 20 20 6f 70 65 6e 20 61 20 77          open a w
2651c 72 69 74 65 20 63 75 72 73 6f 72 20 6f 6e 20 74  rite cursor on t
2651d 68 65 20 3c 74 61 62 6c 65 3e 20 69 6e 64 65 78  he <table> index
2651e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6f 70  .**           op
2651f 65 6e 20 61 20 72 65 61 64 20 63 75 72 73 6f 72  en a read cursor
26520 20 6f 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f   on the correspo
26521 6e 64 69 6e 67 20 3c 74 61 62 6c 65 32 3e 20 69  nding <table2> i
26522 6e 64 65 78 0a 2a 2a 20 20 20 20 20 20 20 20 20  ndex.**         
26523 20 20 74 72 61 6e 73 66 65 72 20 61 6c 6c 20 72    transfer all r
26524 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 65 20  ecords from the 
26525 72 65 61 64 20 74 6f 20 74 68 65 20 77 72 69 74  read to the writ
26526 65 20 63 75 72 73 6f 72 73 0a 2a 2a 20 20 20 20  e cursors.**    
26527 20 20 20 20 20 20 20 63 6c 6f 73 65 20 63 75 72         close cur
26528 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  sors.**         
26529 65 6e 64 20 66 6f 72 65 61 63 68 0a 2a 2a 0a 2a  end foreach.**.*
2652a 2a 20 54 68 65 20 33 72 64 20 74 65 6d 70 6c 61  * The 3rd templa
2652b 74 65 20 69 73 20 66 6f 72 20 77 68 65 6e 20 74  te is for when t
2652c 68 65 20 73 65 63 6f 6e 64 20 74 65 6d 70 6c 61  he second templa
2652d 74 65 20 64 6f 65 73 20 6e 6f 74 20 61 70 70 6c  te does not appl
2652e 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 53 45 4c  y.** and the SEL
2652f 45 43 54 20 63 6c 61 75 73 65 20 64 6f 65 73 20  ECT clause does 
26530 6e 6f 74 20 72 65 61 64 20 66 72 6f 6d 20 3c 74  not read from <t
26531 61 62 6c 65 3e 20 61 74 20 61 6e 79 20 74 69 6d  able> at any tim
26532 65 2e 0a 2a 2a 20 54 68 65 20 67 65 6e 65 72 61  e..** The genera
26533 74 65 64 20 63 6f 64 65 20 66 6f 6c 6c 6f 77 73  ted code follows
26534 20 74 68 69 73 20 74 65 6d 70 6c 61 74 65 3a 0a   this template:.
26535 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f  **.**         EO
26536 46 20 3c 2d 20 30 0a 2a 2a 20 20 20 20 20 20 20  F <- 0.**       
26537 20 20 58 20 3c 2d 20 41 0a 2a 2a 20 20 20 20 20    X <- A.**     
26538 20 20 20 20 67 6f 74 6f 20 42 0a 2a 2a 20 20 20      goto B.**   
26539 20 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72 20     A: setup for 
2653a 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20  the SELECT.**   
2653b 20 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72 20        loop over 
2653c 74 68 65 20 72 6f 77 73 20 69 6e 20 74 68 65 20  the rows in the 
2653d 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20  SELECT.**       
2653e 20 20 20 20 6c 6f 61 64 20 76 61 6c 75 65 73 20      load values 
2653f 69 6e 74 6f 20 72 65 67 69 73 74 65 72 73 20 52  into registers R
26540 2e 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20 20 20 20  ..R+n.**        
26541 20 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20     yield X.**   
26542 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 2a        end loop.*
26543 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75  *         cleanu
26544 70 20 61 66 74 65 72 20 74 68 65 20 53 45 4c 45  p after the SELE
26545 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f  CT.**         EO
26546 46 20 3c 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20  F <- 1.**       
26547 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20    yield X.**    
26548 20 20 20 20 20 67 6f 74 6f 20 41 0a 2a 2a 20 20       goto A.**  
26549 20 20 20 20 42 3a 20 6f 70 65 6e 20 77 72 69 74      B: open writ
2654a 65 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62  e cursor to <tab
2654b 6c 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69  le> and its indi
2654c 63 65 73 0a 2a 2a 20 20 20 20 20 20 43 3a 20 79  ces.**      C: y
2654d 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20  ield X.**       
2654e 20 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 44 0a    if EOF goto D.
2654f 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 73 65 72  **         inser
26550 74 20 74 68 65 20 73 65 6c 65 63 74 20 72 65 73  t the select res
26551 75 6c 74 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e  ult into <table>
26552 20 66 72 6f 6d 20 52 2e 2e 52 2b 6e 0a 2a 2a 20   from R..R+n.** 
26553 20 20 20 20 20 20 20 20 67 6f 74 6f 20 43 0a 2a          goto C.*
26554 2a 20 20 20 20 20 20 44 3a 20 63 6c 65 61 6e 75  *      D: cleanu
26555 70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 34 74 68 20  p.**.** The 4th 
26556 74 65 6d 70 6c 61 74 65 20 69 73 20 75 73 65 64  template is used
26557 20 69 66 20 74 68 65 20 69 6e 73 65 72 74 20 73   if the insert s
26558 74 61 74 65 6d 65 6e 74 20 74 61 6b 65 73 20 69  tatement takes i
26559 74 73 0a 2a 2a 20 76 61 6c 75 65 73 20 66 72 6f  ts.** values fro
2655a 6d 20 61 20 53 45 4c 45 43 54 20 62 75 74 20 74  m a SELECT but t
2655b 68 65 20 64 61 74 61 20 69 73 20 62 65 69 6e 67  he data is being
2655c 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 61   inserted into a
2655d 20 74 61 62 6c 65 0a 2a 2a 20 74 68 61 74 20 69   table.** that i
2655e 73 20 61 6c 73 6f 20 72 65 61 64 20 61 73 20 70  s also read as p
2655f 61 72 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43  art of the SELEC
26560 54 2e 20 20 49 6e 20 74 68 65 20 74 68 69 72 64  T.  In the third
26561 20 66 6f 72 6d 2c 0a 2a 2a 20 77 65 20 68 61 76   form,.** we hav
26562 65 20 74 6f 20 75 73 65 20 61 20 69 6e 74 65 72  e to use a inter
26563 6d 65 64 69 61 74 65 20 74 61 62 6c 65 20 74 6f  mediate table to
26564 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   store the resul
26565 74 73 20 6f 66 0a 2a 2a 20 74 68 65 20 73 65 6c  ts of.** the sel
26566 65 63 74 2e 20 20 54 68 65 20 74 65 6d 70 6c 61  ect.  The templa
26567 74 65 20 69 73 20 6c 69 6b 65 20 74 68 69 73 3a  te is like this:
26568 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 45  .**.**         E
26569 4f 46 20 3c 2d 20 30 0a 2a 2a 20 20 20 20 20 20  OF <- 0.**      
2656a 20 20 20 58 20 3c 2d 20 41 0a 2a 2a 20 20 20 20     X <- A.**    
2656b 20 20 20 20 20 67 6f 74 6f 20 42 0a 2a 2a 20 20       goto B.**  
2656c 20 20 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72      A: setup for
2656d 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20   the SELECT.**  
2656e 20 20 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72         loop over
2656f 20 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74   the tables in t
26570 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20  he SELECT.**    
26571 20 20 20 20 20 20 20 6c 6f 61 64 20 76 61 6c 75         load valu
26572 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20  e into register 
26573 52 2e 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20 20 20  R..R+n.**       
26574 20 20 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20      yield X.**  
26575 20 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a         end loop.
26576 2a 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e  **         clean
26577 75 70 20 61 66 74 65 72 20 74 68 65 20 53 45 4c  up after the SEL
26578 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 45  ECT.**         E
26579 4f 46 20 3c 2d 20 31 0a 2a 2a 20 20 20 20 20 20  OF <- 1.**      
2657a 20 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20     yield X.**   
2657b 20 20 20 20 20 20 68 61 6c 74 2d 65 72 72 6f 72        halt-error
2657c 0a 2a 2a 20 20 20 20 20 20 42 3a 20 6f 70 65 6e  .**      B: open
2657d 20 74 65 6d 70 20 74 61 62 6c 65 0a 2a 2a 20 20   temp table.**  
2657e 20 20 20 20 4c 3a 20 79 69 65 6c 64 20 58 0a 2a      L: yield X.*
2657f 2a 20 20 20 20 20 20 20 20 20 69 66 20 45 4f 46  *         if EOF
26580 20 67 6f 74 6f 20 4d 0a 2a 2a 20 20 20 20 20 20   goto M.**      
26581 20 20 20 69 6e 73 65 72 74 20 72 6f 77 20 66 72     insert row fr
26582 6f 6d 20 52 2e 2e 52 2b 6e 20 69 6e 74 6f 20 74  om R..R+n into t
26583 65 6d 70 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20  emp table.**    
26584 20 20 20 20 20 67 6f 74 6f 20 4c 0a 2a 2a 20 20       goto L.**  
26585 20 20 20 20 4d 3a 20 6f 70 65 6e 20 77 72 69 74      M: open writ
26586 65 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62  e cursor to <tab
26587 6c 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69  le> and its indi
26588 63 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 72  ces.**         r
26589 65 77 69 6e 64 20 74 65 6d 70 20 74 61 62 6c 65  ewind temp table
2658a 0a 2a 2a 20 20 20 20 20 20 43 3a 20 6c 6f 6f 70  .**      C: loop
2658b 20 6f 76 65 72 20 72 6f 77 73 20 6f 66 20 69 6e   over rows of in
2658c 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65  termediate table
2658d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 72  .**           tr
2658e 61 6e 73 66 65 72 20 76 61 6c 75 65 73 20 66 6f  ansfer values fo
2658f 72 6d 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  rm intermediate 
26590 74 61 62 6c 65 20 69 6e 74 6f 20 3c 74 61 62 6c  table into <tabl
26591 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 65 6e  e>.**         en
26592 64 20 6c 6f 6f 70 0a 2a 2a 20 20 20 20 20 20 44  d loop.**      D
26593 3a 20 63 6c 65 61 6e 75 70 0a 2a 2f 0a 53 51 4c  : cleanup.*/.SQL
26594 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
26595 20 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 0a   sqlite3Insert(.
26596 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
26597 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65          /* Parse
26598 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  r context */.  S
26599 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74  rcList *pTabList
2659a 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  ,    /* Name of 
2659b 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68  table into which
2659c 20 77 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e   we are insertin
2659d 67 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  g */.  ExprList 
2659e 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 2f 2a 20  *pList,      /* 
2659f 4c 69 73 74 20 6f 66 20 76 61 6c 75 65 73 20 74  List of values t
265a0 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f  o be inserted */
265a1 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  .  Select *pSele
265a2 63 74 2c 20 20 20 20 20 20 2f 2a 20 41 20 53 45  ct,      /* A SE
265a3 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74  LECT statement t
265a4 6f 20 75 73 65 20 61 73 20 74 68 65 20 64 61 74  o use as the dat
265a5 61 20 73 6f 75 72 63 65 20 2a 2f 0a 20 20 49 64  a source */.  Id
265a6 4c 69 73 74 20 2a 70 43 6f 6c 75 6d 6e 2c 20 20  List *pColumn,  
265a7 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61      /* Column na
265a8 6d 65 73 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  mes correspondin
265a9 67 20 74 6f 20 49 44 4c 49 53 54 2e 20 2a 2f 0a  g to IDLIST. */.
265aa 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 20 20 20    int onError   
265ab 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74          /* How t
265ac 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61  o handle constra
265ad 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a 29 7b  int errors */.){
265ae 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
265af 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
265b0 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 74  main database st
265b1 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 54 61 62  ructure */.  Tab
265b2 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20  le *pTab;       
265b3 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
265b4 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 2e 20  to insert into. 
265b5 20 61 6b 61 20 54 41 42 4c 45 20 2a 2f 0a 20 20   aka TABLE */.  
265b6 63 68 61 72 20 2a 7a 54 61 62 3b 20 20 20 20 20  char *zTab;     
265b7 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
265b8 20 74 68 65 20 74 61 62 6c 65 20 69 6e 74 6f 20   the table into 
265b9 77 68 69 63 68 20 77 65 20 61 72 65 20 69 6e 73  which we are ins
265ba 65 72 74 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73  erting */.  cons
265bb 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20  t char *zDb;    
265bc 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
265bd 20 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e   database holdin
265be 67 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a  g this table */.
265bf 20 20 69 6e 74 20 69 2c 20 6a 2c 20 69 64 78 3b    int i, j, idx;
265c0 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
265c1 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 56 64  counters */.  Vd
265c2 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20  be *v;          
265c3 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20      /* Generate 
265c4 63 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20 76  code into this v
265c5 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 2a  irtual machine *
265c6 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b  /.  Index *pIdx;
265c7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
265c8 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69 6e   looping over in
265c9 64 69 63 65 73 20 6f 66 20 74 68 65 20 74 61 62  dices of the tab
265ca 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c  le */.  int nCol
265cb 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  umn;          /*
265cc 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   Number of colum
265cd 6e 73 20 69 6e 20 74 68 65 20 64 61 74 61 20 2a  ns in the data *
265ce 2f 0a 20 20 69 6e 74 20 6e 48 69 64 64 65 6e 20  /.  int nHidden 
265cf 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d  = 0;      /* Num
265d0 62 65 72 20 6f 66 20 68 69 64 64 65 6e 20 63 6f  ber of hidden co
265d1 6c 75 6d 6e 73 20 69 66 20 54 41 42 4c 45 20 69  lumns if TABLE i
265d2 73 20 76 69 72 74 75 61 6c 20 2a 2f 0a 20 20 69  s virtual */.  i
265d3 6e 74 20 62 61 73 65 43 75 72 20 3d 20 30 3b 20  nt baseCur = 0; 
265d4 20 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75 72       /* VDBE Cur
265d5 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 70  sor number for p
265d6 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 6b 65 79  Tab */.  int key
265d7 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 20 20 20 2f  Column = -1;   /
265d8 2a 20 43 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73  * Column that is
265d9 20 74 68 65 20 49 4e 54 45 47 45 52 20 50 52 49   the INTEGER PRI
265da 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 69 6e  MARY KEY */.  in
265db 74 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20 20 20 20  t endOfLoop;    
265dc 20 20 20 20 2f 2a 20 4c 61 62 65 6c 20 66 6f 72      /* Label for
265dd 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
265de 69 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f 70 20 2a  insertion loop *
265df 2f 0a 20 20 69 6e 74 20 75 73 65 54 65 6d 70 54  /.  int useTempT
265e0 61 62 6c 65 20 3d 20 30 3b 20 2f 2a 20 53 74 6f  able = 0; /* Sto
265e1 72 65 20 53 45 4c 45 43 54 20 72 65 73 75 6c 74  re SELECT result
265e2 73 20 69 6e 20 69 6e 74 65 72 6d 65 64 69 61 74  s in intermediat
265e3 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  e table */.  int
265e4 20 73 72 63 54 61 62 20 3d 20 30 3b 20 20 20 20   srcTab = 0;    
265e5 20 20 20 2f 2a 20 44 61 74 61 20 63 6f 6d 65 73     /* Data comes
265e6 20 66 72 6f 6d 20 74 68 69 73 20 74 65 6d 70 6f   from this tempo
265e7 72 61 72 79 20 63 75 72 73 6f 72 20 69 66 20 3e  rary cursor if >
265e8 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  =0 */.  int addr
265e9 49 6e 73 54 6f 70 20 3d 20 30 3b 20 20 20 2f 2a  InsTop = 0;   /*
265ea 20 4a 75 6d 70 20 74 6f 20 6c 61 62 65 6c 20 22   Jump to label "
265eb 44 22 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  D" */.  int addr
265ec 43 6f 6e 74 20 3d 20 30 3b 20 20 20 20 20 2f 2a  Cont = 0;     /*
265ed 20 54 6f 70 20 6f 66 20 69 6e 73 65 72 74 20 6c   Top of insert l
265ee 6f 6f 70 2e 20 4c 61 62 65 6c 20 22 43 22 20 69  oop. Label "C" i
265ef 6e 20 74 65 6d 70 6c 61 74 65 73 20 33 20 61 6e  n templates 3 an
265f0 64 20 34 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  d 4 */.  int add
265f1 72 53 65 6c 65 63 74 20 3d 20 30 3b 20 20 20 2f  rSelect = 0;   /
265f2 2a 20 41 64 64 72 65 73 73 20 6f 66 20 63 6f 72  * Address of cor
265f3 6f 75 74 69 6e 65 20 74 68 61 74 20 69 6d 70 6c  outine that impl
265f4 65 6d 65 6e 74 73 20 74 68 65 20 53 45 4c 45 43  ements the SELEC
265f5 54 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73  T */.  SelectDes
265f6 74 20 64 65 73 74 3b 20 20 20 20 20 20 2f 2a 20  t dest;      /* 
265f7 44 65 73 74 69 6e 61 74 69 6f 6e 20 66 6f 72 20  Destination for 
265f8 53 45 4c 45 43 54 20 6f 6e 20 72 68 73 20 6f 66  SELECT on rhs of
265f9 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69 6e 74   INSERT */.  int
265fa 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   iDb;           
265fb 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64     /* Index of d
265fc 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20  atabase holding 
265fd 54 41 42 4c 45 20 2a 2f 0a 20 20 44 62 20 2a 70  TABLE */.  Db *p
265fe 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
265ff 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
26600 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c   containing tabl
26601 65 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64  e being inserted
26602 20 69 6e 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 61   into */.  int a
26603 70 70 65 6e 64 46 6c 61 67 20 3d 20 30 3b 20 20  ppendFlag = 0;  
26604 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
26605 69 6e 73 65 72 74 20 69 73 20 6c 69 6b 65 6c 79  insert is likely
26606 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e 64   to be an append
26607 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74   */..  /* Regist
26608 65 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a  er allocations *
26609 2f 0a 20 20 69 6e 74 20 72 65 67 46 72 6f 6d 53  /.  int regFromS
2660a 65 6c 65 63 74 20 3d 20 30 3b 2f 2a 20 42 61 73  elect = 0;/* Bas
2660b 65 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 64  e register for d
2660c 61 74 61 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20  ata coming from 
2660d 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20  SELECT */.  int 
2660e 72 65 67 41 75 74 6f 69 6e 63 20 3d 20 30 3b 20  regAutoinc = 0; 
2660f 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f    /* Register ho
26610 6c 64 69 6e 67 20 74 68 65 20 41 55 54 4f 49 4e  lding the AUTOIN
26611 43 52 45 4d 45 4e 54 20 63 6f 75 6e 74 65 72 20  CREMENT counter 
26612 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 43  */.  int regRowC
26613 6f 75 6e 74 20 3d 20 30 3b 20 20 2f 2a 20 4d 65  ount = 0;  /* Me
26614 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 66  mory cell used f
26615 6f 72 20 74 68 65 20 72 6f 77 20 63 6f 75 6e 74  or the row count
26616 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 49  er */.  int regI
26617 6e 73 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ns;           /*
26618 20 42 6c 6f 63 6b 20 6f 66 20 72 65 67 73 20 68   Block of regs h
26619 6f 6c 64 69 6e 67 20 72 6f 77 69 64 2b 64 61 74  olding rowid+dat
2661a 61 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64  a being inserted
2661b 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77   */.  int regRow
2661c 69 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 72  id;         /* r
2661d 65 67 69 73 74 65 72 73 20 68 6f 6c 64 69 6e 67  egisters holding
2661e 20 69 6e 73 65 72 74 20 72 6f 77 69 64 20 2a 2f   insert rowid */
2661f 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61 3b 20  .  int regData; 
26620 20 20 20 20 20 20 20 20 20 2f 2a 20 72 65 67 69           /* regi
26621 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 66 69 72  ster holding fir
26622 73 74 20 63 6f 6c 75 6d 6e 20 74 6f 20 69 6e 73  st column to ins
26623 65 72 74 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ert */.  int reg
26624 52 65 63 6f 72 64 3b 20 20 20 20 20 20 20 20 2f  Record;        /
26625 2a 20 48 6f 6c 64 73 20 74 68 65 20 61 73 73 65  * Holds the asse
26626 6d 62 6c 69 65 64 20 72 6f 77 20 72 65 63 6f 72  mblied row recor
26627 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 45 6f  d */.  int regEo
26628 66 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20  f = 0;       /* 
26629 52 65 67 69 73 74 65 72 20 72 65 63 6f 72 64 69  Register recordi
2662a 6e 67 20 65 6e 64 20 6f 66 20 53 45 4c 45 43 54  ng end of SELECT
2662b 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 2a   data */.  int *
2662c 61 52 65 67 49 64 78 20 3d 20 30 3b 20 20 20 20  aRegIdx = 0;    
2662d 20 2f 2a 20 4f 6e 65 20 72 65 67 69 73 74 65 72   /* One register
2662e 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 65 61   allocated to ea
2662f 63 68 20 69 6e 64 65 78 20 2a 2f 0a 0a 23 69 66  ch index */..#if
26630 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
26631 5f 54 52 49 47 47 45 52 0a 20 20 69 6e 74 20 69  _TRIGGER.  int i
26632 73 56 69 65 77 3b 20 20 20 20 20 20 20 20 20 20  sView;          
26633 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
26634 66 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20  f attempting to 
26635 69 6e 73 65 72 74 20 69 6e 74 6f 20 61 20 76 69  insert into a vi
26636 65 77 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20  ew */.  Trigger 
26637 2a 70 54 72 69 67 67 65 72 3b 20 20 20 20 20 20  *pTrigger;      
26638 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74      /* List of t
26639 72 69 67 67 65 72 73 20 6f 6e 20 70 54 61 62 2c  riggers on pTab,
2663a 20 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a   if required */.
2663b 20 20 69 6e 74 20 74 6d 61 73 6b 3b 20 20 20 20    int tmask;    
2663c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2663d 20 4d 61 73 6b 20 6f 66 20 74 72 69 67 67 65 72   Mask of trigger
2663e 20 74 69 6d 65 73 20 2a 2f 0a 23 65 6e 64 69 66   times */.#endif
2663f 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  ..  db = pParse-
26640 3e 64 62 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64  >db;.  memset(&d
26641 65 73 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64  est, 0, sizeof(d
26642 65 73 74 29 29 3b 0a 20 20 69 66 28 20 70 50 61  est));.  if( pPa
26643 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d  rse->nErr || db-
26644 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
26645 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74  .    goto insert
26646 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20  _cleanup;.  }.. 
26647 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74   /* Locate the t
26648 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20  able into which 
26649 77 65 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72  we will be inser
2664a 74 69 6e 67 20 6e 65 77 20 69 6e 66 6f 72 6d 61  ting new informa
2664b 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  tion..  */.  ass
2664c 65 72 74 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e  ert( pTabList->n
2664d 53 72 63 3d 3d 31 20 29 3b 0a 20 20 7a 54 61 62  Src==1 );.  zTab
2664e 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30   = pTabList->a[0
2664f 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 4e  ].zName;.  if( N
26650 45 56 45 52 28 7a 54 61 62 3d 3d 30 29 20 29 20  EVER(zTab==0) ) 
26651 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61  goto insert_clea
26652 6e 75 70 3b 0a 20 20 70 54 61 62 20 3d 20 73 71  nup;.  pTab = sq
26653 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b  lite3SrcListLook
26654 75 70 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c  up(pParse, pTabL
26655 69 73 74 29 3b 0a 20 20 69 66 28 20 70 54 61 62  ist);.  if( pTab
26656 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ==0 ){.    goto 
26657 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
26658 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69    }.  iDb = sqli
26659 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
2665a 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  (db, pTab->pSche
2665b 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  ma);.  assert( i
2665c 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  Db<db->nDb );.  
2665d 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69  pDb = &db->aDb[i
2665e 44 62 5d 3b 0a 20 20 7a 44 62 20 3d 20 70 44 62  Db];.  zDb = pDb
2665f 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 73  ->zName;.  if( s
26660 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
26661 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49  pParse, SQLITE_I
26662 4e 53 45 52 54 2c 20 70 54 61 62 2d 3e 7a 4e 61  NSERT, pTab->zNa
26663 6d 65 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20  me, 0, zDb) ){. 
26664 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63     goto insert_c
26665 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f  leanup;.  }..  /
26666 2a 20 46 69 67 75 72 65 20 6f 75 74 20 69 66 20  * Figure out if 
26667 77 65 20 68 61 76 65 20 61 6e 79 20 74 72 69 67  we have any trig
26668 67 65 72 73 20 61 6e 64 20 69 66 20 74 68 65 20  gers and if the 
26669 74 61 62 6c 65 20 62 65 69 6e 67 0a 20 20 2a 2a  table being.  **
2666a 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 69   inserted into i
2666b 73 20 61 20 76 69 65 77 0a 20 20 2a 2f 0a 23 69  s a view.  */.#i
2666c 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2666d 54 5f 54 52 49 47 47 45 52 0a 20 20 70 54 72 69  T_TRIGGER.  pTri
2666e 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 54 72  gger = sqlite3Tr
2666f 69 67 67 65 72 73 45 78 69 73 74 28 70 50 61 72  iggersExist(pPar
26670 73 65 2c 20 70 54 61 62 2c 20 54 4b 5f 49 4e 53  se, pTab, TK_INS
26671 45 52 54 2c 20 30 2c 20 26 74 6d 61 73 6b 29 3b  ERT, 0, &tmask);
26672 0a 20 20 69 73 56 69 65 77 20 3d 20 70 54 61 62  .  isView = pTab
26673 2d 3e 70 53 65 6c 65 63 74 21 3d 30 3b 0a 23 65  ->pSelect!=0;.#e
26674 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 70 54 72  lse.# define pTr
26675 69 67 67 65 72 20 30 0a 23 20 64 65 66 69 6e 65  igger 0.# define
26676 20 74 6d 61 73 6b 20 30 0a 23 20 64 65 66 69 6e   tmask 0.# defin
26677 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69  e isView 0.#endi
26678 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
26679 4f 4d 49 54 5f 56 49 45 57 0a 23 20 75 6e 64 65  OMIT_VIEW.# unde
2667a 66 20 69 73 56 69 65 77 0a 23 20 64 65 66 69 6e  f isView.# defin
2667b 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69  e isView 0.#endi
2667c 66 0a 20 20 61 73 73 65 72 74 28 20 28 70 54 72  f.  assert( (pTr
2667d 69 67 67 65 72 20 26 26 20 74 6d 61 73 6b 29 20  igger && tmask) 
2667e 7c 7c 20 28 70 54 72 69 67 67 65 72 3d 3d 30 20  || (pTrigger==0 
2667f 26 26 20 74 6d 61 73 6b 3d 3d 30 29 20 29 3b 0a  && tmask==0) );.
26680 0a 20 20 2f 2a 20 49 66 20 70 54 61 62 20 69 73  .  /* If pTab is
26681 20 72 65 61 6c 6c 79 20 61 20 76 69 65 77 2c 20   really a view, 
26682 6d 61 6b 65 20 73 75 72 65 20 69 74 20 68 61 73  make sure it has
26683 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65   been initialize
26684 64 2e 0a 20 20 2a 2a 20 56 69 65 77 47 65 74 43  d..  ** ViewGetC
26685 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 69 73 20  olumnNames() is 
26686 61 20 6e 6f 2d 6f 70 20 69 66 20 70 54 61 62 20  a no-op if pTab 
26687 69 73 20 6e 6f 74 20 61 20 76 69 65 77 20 28 6f  is not a view (o
26688 72 20 76 69 72 74 75 61 6c 20 0a 20 20 2a 2a 20  r virtual .  ** 
26689 6d 6f 64 75 6c 65 20 74 61 62 6c 65 29 2e 0a 20  module table).. 
2668a 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
2668b 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61  3ViewGetColumnNa
2668c 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62  mes(pParse, pTab
2668d 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e  ) ){.    goto in
2668e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  sert_cleanup;.  
2668f 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 74  }..  /* Ensure t
26690 68 61 74 3a 0a 20 20 2a 20 20 28 61 29 20 74 68  hat:.  *  (a) th
26691 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 72  e table is not r
26692 65 61 64 2d 6f 6e 6c 79 2c 20 0a 20 20 2a 20 20  ead-only, .  *  
26693 28 62 29 20 74 68 61 74 20 69 66 20 69 74 20 69  (b) that if it i
26694 73 20 61 20 76 69 65 77 20 74 68 65 6e 20 4f 4e  s a view then ON
26695 20 49 4e 53 45 52 54 20 74 72 69 67 67 65 72 73   INSERT triggers
26696 20 65 78 69 73 74 0a 20 20 2a 2f 0a 20 20 69 66   exist.  */.  if
26697 28 20 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f  ( sqlite3IsReadO
26698 6e 6c 79 28 70 50 61 72 73 65 2c 20 70 54 61 62  nly(pParse, pTab
26699 2c 20 74 6d 61 73 6b 29 20 29 7b 0a 20 20 20 20  , tmask) ){.    
2669a 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61  goto insert_clea
2669b 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  nup;.  }..  /* A
2669c 6c 6c 6f 63 61 74 65 20 61 20 56 44 42 45 0a 20  llocate a VDBE. 
2669d 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65   */.  v = sqlite
2669e 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
2669f 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67  ;.  if( v==0 ) g
266a0 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e  oto insert_clean
266a1 75 70 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  up;.  if( pParse
266a2 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 20 73 71  ->nested==0 ) sq
266a3 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68  lite3VdbeCountCh
266a4 61 6e 67 65 73 28 76 29 3b 0a 20 20 73 71 6c 69  anges(v);.  sqli
266a5 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
266a6 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70  ration(pParse, p
266a7 53 65 6c 65 63 74 20 7c 7c 20 70 54 72 69 67 67  Select || pTrigg
266a8 65 72 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64  er, iDb);..#ifnd
266a9 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58  ef SQLITE_OMIT_X
266aa 46 45 52 5f 4f 50 54 0a 20 20 2f 2a 20 49 66 20  FER_OPT.  /* If 
266ab 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73  the statement is
266ac 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 20 20 2a   of the form.  *
266ad 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 49 4e 53  *.  **       INS
266ae 45 52 54 20 49 4e 54 4f 20 3c 74 61 62 6c 65 31  ERT INTO <table1
266af 3e 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  > SELECT * FROM 
266b0 3c 74 61 62 6c 65 32 3e 3b 0a 20 20 2a 2a 0a 20  <table2>;.  **. 
266b1 20 2a 2a 20 54 68 65 6e 20 73 70 65 63 69 61 6c   ** Then special
266b2 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 63   optimizations c
266b3 61 6e 20 62 65 20 61 70 70 6c 69 65 64 20 74 68  an be applied th
266b4 61 74 20 6d 61 6b 65 20 74 68 65 20 74 72 61 6e  at make the tran
266b5 73 66 65 72 0a 20 20 2a 2a 20 76 65 72 79 20 66  sfer.  ** very f
266b6 61 73 74 20 61 6e 64 20 77 68 69 63 68 20 72 65  ast and which re
266b7 64 75 63 65 20 66 72 61 67 6d 65 6e 74 61 74 69  duce fragmentati
266b8 6f 6e 20 6f 66 20 69 6e 64 69 63 65 73 2e 0a 20  on of indices.. 
266b9 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 69 73   **.  ** This is
266ba 20 74 68 65 20 32 6e 64 20 74 65 6d 70 6c 61 74   the 2nd templat
266bb 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 43  e..  */.  if( pC
266bc 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 78 66 65 72  olumn==0 && xfer
266bd 4f 70 74 69 6d 69 7a 61 74 69 6f 6e 28 70 50 61  Optimization(pPa
266be 72 73 65 2c 20 70 54 61 62 2c 20 70 53 65 6c 65  rse, pTab, pSele
266bf 63 74 2c 20 6f 6e 45 72 72 6f 72 2c 20 69 44 62  ct, onError, iDb
266c0 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ) ){.    assert(
266c1 20 21 70 54 72 69 67 67 65 72 20 29 3b 0a 20 20   !pTrigger );.  
266c2 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 3d    assert( pList=
266c3 3d 30 20 29 3b 0a 20 20 20 20 67 6f 74 6f 20 69  =0 );.    goto i
266c4 6e 73 65 72 74 5f 65 6e 64 3b 0a 20 20 7d 0a 23  nsert_end;.  }.#
266c5 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
266c6 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54 20 2a 2f  OMIT_XFER_OPT */
266c7 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69  ..  /* If this i
266c8 73 20 61 6e 20 41 55 54 4f 49 4e 43 52 45 4d 45  s an AUTOINCREME
266c9 4e 54 20 74 61 62 6c 65 2c 20 6c 6f 6f 6b 20 75  NT table, look u
266ca 70 20 74 68 65 20 73 65 71 75 65 6e 63 65 20 6e  p the sequence n
266cb 75 6d 62 65 72 20 69 6e 20 74 68 65 0a 20 20 2a  umber in the.  *
266cc 2a 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  * sqlite_sequenc
266cd 65 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f 72  e table and stor
266ce 65 20 69 74 20 69 6e 20 6d 65 6d 6f 72 79 20 63  e it in memory c
266cf 65 6c 6c 20 72 65 67 41 75 74 6f 69 6e 63 2e 0a  ell regAutoinc..
266d0 20 20 2a 2f 0a 20 20 72 65 67 41 75 74 6f 69 6e    */.  regAutoin
266d1 63 20 3d 20 61 75 74 6f 49 6e 63 42 65 67 69 6e  c = autoIncBegin
266d2 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54  (pParse, iDb, pT
266d3 61 62 29 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72  ab);..  /* Figur
266d4 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 63  e out how many c
266d5 6f 6c 75 6d 6e 73 20 6f 66 20 64 61 74 61 20 61  olumns of data a
266d6 72 65 20 73 75 70 70 6c 69 65 64 2e 20 20 49 66  re supplied.  If
266d7 20 74 68 65 20 64 61 74 61 0a 20 20 2a 2a 20 69   the data.  ** i
266d8 73 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 61 20  s coming from a 
266d9 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
266da 2c 20 74 68 65 6e 20 67 65 6e 65 72 61 74 65 20  , then generate 
266db 61 20 63 6f 2d 72 6f 75 74 69 6e 65 20 74 68 61  a co-routine tha
266dc 74 0a 20 20 2a 2a 20 70 72 6f 64 75 63 65 73 20  t.  ** produces 
266dd 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20  a single row of 
266de 74 68 65 20 53 45 4c 45 43 54 20 6f 6e 20 65 61  the SELECT on ea
266df 63 68 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 20 20  ch invocation.  
266e0 54 68 65 0a 20 20 2a 2a 20 63 6f 2d 72 6f 75 74  The.  ** co-rout
266e1 69 6e 65 20 69 73 20 74 68 65 20 63 6f 6d 6d 6f  ine is the commo
266e2 6e 20 68 65 61 64 65 72 20 74 6f 20 74 68 65 20  n header to the 
266e3 33 72 64 20 61 6e 64 20 34 74 68 20 74 65 6d 70  3rd and 4th temp
266e4 6c 61 74 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  lates..  */.  if
266e5 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ( pSelect ){.   
266e6 20 2f 2a 20 44 61 74 61 20 69 73 20 63 6f 6d 69   /* Data is comi
266e7 6e 67 20 66 72 6f 6d 20 61 20 53 45 4c 45 43 54  ng from a SELECT
266e8 2e 20 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  .  Generate code
266e9 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
266ea 61 74 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a  at SELECT.    **
266eb 20 61 73 20 61 20 63 6f 2d 72 6f 75 74 69 6e 65   as a co-routine
266ec 2e 20 20 54 68 65 20 63 6f 64 65 20 69 73 20 63  .  The code is c
266ed 6f 6d 6d 6f 6e 20 74 6f 20 62 6f 74 68 20 74 68  ommon to both th
266ee 65 20 33 72 64 20 61 6e 64 20 34 74 68 0a 20 20  e 3rd and 4th.  
266ef 20 20 2a 2a 20 74 65 6d 70 6c 61 74 65 73 3a 0a    ** templates:.
266f0 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
266f1 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 30 0a 20        EOF <- 0. 
266f2 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 58 20     **         X 
266f3 3c 2d 20 41 0a 20 20 20 20 2a 2a 20 20 20 20 20  <- A.    **     
266f4 20 20 20 20 67 6f 74 6f 20 42 0a 20 20 20 20 2a      goto B.    *
266f5 2a 20 20 20 20 20 20 41 3a 20 73 65 74 75 70 20  *      A: setup 
266f6 66 6f 72 20 74 68 65 20 53 45 4c 45 43 54 0a 20  for the SELECT. 
266f7 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 6c 6f     **         lo
266f8 6f 70 20 6f 76 65 72 20 74 68 65 20 74 61 62 6c  op over the tabl
266f9 65 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54  es in the SELECT
266fa 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
266fb 20 20 6c 6f 61 64 20 76 61 6c 75 65 20 69 6e 74    load value int
266fc 6f 20 72 65 67 69 73 74 65 72 20 52 2e 2e 52 2b  o register R..R+
266fd 6e 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  n.    **        
266fe 20 20 20 79 69 65 6c 64 20 58 0a 20 20 20 20 2a     yield X.    *
266ff 2a 20 20 20 20 20 20 20 20 20 65 6e 64 20 6c 6f  *         end lo
26700 6f 70 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  op.    **       
26701 20 20 63 6c 65 61 6e 75 70 20 61 66 74 65 72 20    cleanup after 
26702 74 68 65 20 53 45 4c 45 43 54 0a 20 20 20 20 2a  the SELECT.    *
26703 2a 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d  *         EOF <-
26704 20 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20   1.    **       
26705 20 20 79 69 65 6c 64 20 58 0a 20 20 20 20 2a 2a    yield X.    **
26706 20 20 20 20 20 20 20 20 20 68 61 6c 74 2d 65 72           halt-er
26707 72 6f 72 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ror.    **.    *
26708 2a 20 4f 6e 20 65 61 63 68 20 69 6e 76 6f 63 61  * On each invoca
26709 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 2d 72  tion of the co-r
2670a 6f 75 74 69 6e 65 2c 20 69 74 20 70 75 74 73 20  outine, it puts 
2670b 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20  a single row of 
2670c 74 68 65 0a 20 20 20 20 2a 2a 20 53 45 4c 45 43  the.    ** SELEC
2670d 54 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 72 65  T result into re
2670e 67 69 73 74 65 72 73 20 64 65 73 74 2e 69 4d 65  gisters dest.iMe
2670f 6d 2e 2e 2e 64 65 73 74 2e 69 4d 65 6d 2b 64 65  m...dest.iMem+de
26710 73 74 2e 6e 4d 65 6d 2d 31 2e 0a 20 20 20 20 2a  st.nMem-1..    *
26711 2a 20 28 54 68 65 73 65 20 6f 75 74 70 75 74 20  * (These output 
26712 72 65 67 69 73 74 65 72 73 20 61 72 65 20 61 6c  registers are al
26713 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74  located by sqlit
26714 65 33 53 65 6c 65 63 74 28 29 2e 29 20 20 57 68  e3Select().)  Wh
26715 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 53 45  en.    ** the SE
26716 4c 45 43 54 20 63 6f 6d 70 6c 65 74 65 73 2c 20  LECT completes, 
26717 69 74 20 73 65 74 73 20 74 68 65 20 45 4f 46 20  it sets the EOF 
26718 66 6c 61 67 20 73 74 6f 72 65 64 20 69 6e 20 72  flag stored in r
26719 65 67 45 6f 66 2e 0a 20 20 20 20 2a 2f 0a 20 20  egEof..    */.  
2671a 20 20 69 6e 74 20 72 63 2c 20 6a 31 3b 0a 0a 20    int rc, j1;.. 
2671b 20 20 20 72 65 67 45 6f 66 20 3d 20 2b 2b 70 50     regEof = ++pP
2671c 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
2671d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2671e 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2671f 20 30 2c 20 72 65 67 45 6f 66 29 3b 20 20 20 20   0, regEof);    
26720 20 20 2f 2a 20 45 4f 46 20 3c 2d 20 30 20 2a 2f    /* EOF <- 0 */
26721 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  .    VdbeComment
26722 28 28 76 2c 20 22 53 45 4c 45 43 54 20 65 6f 66  ((v, "SELECT eof
26723 20 66 6c 61 67 22 29 29 3b 0a 20 20 20 20 73 71   flag"));.    sq
26724 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49  lite3SelectDestI
26725 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54 5f 43  nit(&dest, SRT_C
26726 6f 72 6f 75 74 69 6e 65 2c 20 2b 2b 70 50 61 72  oroutine, ++pPar
26727 73 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 61  se->nMem);.    a
26728 64 64 72 53 65 6c 65 63 74 20 3d 20 73 71 6c 69  ddrSelect = sqli
26729 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
2672a 64 72 28 76 29 2b 32 3b 0a 20 20 20 20 73 71 6c  dr(v)+2;.    sql
2672b 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2672c 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 61 64  , OP_Integer, ad
2672d 64 72 53 65 6c 65 63 74 2d 31 2c 20 64 65 73 74  drSelect-1, dest
2672e 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 6a 31 20  .iParm);.    j1 
2672f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
26730 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
26731 30 2c 20 30 29 3b 0a 20 20 20 20 56 64 62 65 43  0, 0);.    VdbeC
26732 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4a 75 6d 70  omment((v, "Jump
26733 20 6f 76 65 72 20 53 45 4c 45 43 54 20 63 6f 72   over SELECT cor
26734 6f 75 74 69 6e 65 22 29 29 3b 0a 0a 20 20 20 20  outine"));..    
26735 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 65  /* Resolve the e
26736 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68  xpressions in th
26737 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
26738 6e 74 20 61 6e 64 20 65 78 65 63 75 74 65 20 69  nt and execute i
26739 74 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73  t. */.    rc = s
2673a 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61  qlite3Select(pPa
2673b 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 26 64  rse, pSelect, &d
2673c 65 73 74 29 3b 0a 20 20 20 20 61 73 73 65 72 74  est);.    assert
2673d 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d  ( pParse->nErr==
2673e 30 20 7c 7c 20 72 63 20 29 3b 0a 20 20 20 20 69  0 || rc );.    i
2673f 66 28 20 72 63 20 7c 7c 20 4e 45 56 45 52 28 70  f( rc || NEVER(p
26740 50 61 72 73 65 2d 3e 6e 45 72 72 29 20 7c 7c 20  Parse->nErr) || 
26741 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
26742 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 69   ){.      goto i
26743 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20  nsert_cleanup;. 
26744 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
26745 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
26746 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 72 65 67  _Integer, 1, reg
26747 45 6f 66 29 3b 20 20 20 20 20 20 20 20 20 2f 2a  Eof);         /*
26748 20 45 4f 46 20 3c 2d 20 31 20 2a 2f 0a 20 20 20   EOF <- 1 */.   
26749 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2674a 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20  p1(v, OP_Yield, 
2674b 64 65 73 74 2e 69 50 61 72 6d 29 3b 20 20 20 2f  dest.iParm);   /
2674c 2a 20 79 69 65 6c 64 20 58 20 2a 2f 0a 20 20 20  * yield X */.   
2674d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2674e 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53  p2(v, OP_Halt, S
2674f 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c 2c 20  QLITE_INTERNAL, 
26750 4f 45 5f 41 62 6f 72 74 29 3b 0a 20 20 20 20 56  OE_Abort);.    V
26751 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
26752 45 6e 64 20 6f 66 20 53 45 4c 45 43 54 20 63 6f  End of SELECT co
26753 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20 20 20  routine"));.    
26754 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
26755 65 72 65 28 76 2c 20 6a 31 29 3b 20 20 20 20 20  ere(v, j1);     
26756 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26757 20 20 20 20 20 2f 2a 20 6c 61 62 65 6c 20 42 3a       /* label B:
26758 20 2a 2f 0a 0a 20 20 20 20 72 65 67 46 72 6f 6d   */..    regFrom
26759 53 65 6c 65 63 74 20 3d 20 64 65 73 74 2e 69 4d  Select = dest.iM
2675a 65 6d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  em;.    assert( 
2675b 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 20  pSelect->pEList 
2675c 29 3b 0a 20 20 20 20 6e 43 6f 6c 75 6d 6e 20 3d  );.    nColumn =
2675d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74   pSelect->pEList
2675e 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 61 73 73  ->nExpr;.    ass
2675f 65 72 74 28 20 64 65 73 74 2e 6e 4d 65 6d 3d 3d  ert( dest.nMem==
26760 6e 43 6f 6c 75 6d 6e 20 29 3b 0a 0a 20 20 20 20  nColumn );..    
26761 2f 2a 20 53 65 74 20 75 73 65 54 65 6d 70 54 61  /* Set useTempTa
26762 62 6c 65 20 74 6f 20 54 52 55 45 20 69 66 20 74  ble to TRUE if t
26763 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65  he result of the
26764 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
26765 74 0a 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20  t.    ** should 
26766 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  be written into 
26767 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  a temporary tabl
26768 65 20 28 74 65 6d 70 6c 61 74 65 20 34 29 2e 20  e (template 4). 
26769 20 53 65 74 20 74 6f 0a 20 20 20 20 2a 2a 20 46   Set to.    ** F
2676a 41 4c 53 45 20 69 66 20 65 61 63 68 2a 20 72 6f  ALSE if each* ro
2676b 77 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20  w of the SELECT 
2676c 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 64  can be written d
2676d 69 72 65 63 74 6c 79 20 69 6e 74 6f 0a 20 20 20  irectly into.   
2676e 20 2a 2a 20 74 68 65 20 64 65 73 74 69 6e 61 74   ** the destinat
2676f 69 6f 6e 20 74 61 62 6c 65 20 28 74 65 6d 70 6c  ion table (templ
26770 61 74 65 20 33 29 2e 0a 20 20 20 20 2a 2a 0a 20  ate 3)..    **. 
26771 20 20 20 2a 2a 20 41 20 74 65 6d 70 20 74 61 62     ** A temp tab
26772 6c 65 20 6d 75 73 74 20 62 65 20 75 73 65 64 20  le must be used 
26773 69 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  if the table bei
26774 6e 67 20 75 70 64 61 74 65 64 20 69 73 20 61 6c  ng updated is al
26775 73 6f 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 6f 66  so one.    ** of
26776 20 74 68 65 20 74 61 62 6c 65 73 20 62 65 69 6e   the tables bein
26777 67 20 72 65 61 64 20 62 79 20 74 68 65 20 53 45  g read by the SE
26778 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20  LECT statement. 
26779 20 41 6c 73 6f 20 75 73 65 20 61 20 0a 20 20 20   Also use a .   
2677a 20 2a 2a 20 74 65 6d 70 20 74 61 62 6c 65 20 69   ** temp table i
2677b 6e 20 74 68 65 20 63 61 73 65 20 6f 66 20 72 6f  n the case of ro
2677c 77 20 74 72 69 67 67 65 72 73 2e 0a 20 20 20 20  w triggers..    
2677d 2a 2f 0a 20 20 20 20 69 66 28 20 70 54 72 69 67  */.    if( pTrig
2677e 67 65 72 20 7c 7c 20 72 65 61 64 73 54 61 62 6c  ger || readsTabl
2677f 65 28 70 50 61 72 73 65 2c 20 61 64 64 72 53 65  e(pParse, addrSe
26780 6c 65 63 74 2c 20 69 44 62 2c 20 70 54 61 62 29  lect, iDb, pTab)
26781 20 29 7b 0a 20 20 20 20 20 20 75 73 65 54 65 6d   ){.      useTem
26782 70 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20  pTable = 1;.    
26783 7d 0a 0a 20 20 20 20 69 66 28 20 75 73 65 54 65  }..    if( useTe
26784 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  mpTable ){.     
26785 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 63   /* Invoke the c
26786 6f 72 6f 75 74 69 6e 65 20 74 6f 20 65 78 74 72  oroutine to extr
26787 61 63 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  act information 
26788 66 72 6f 6d 20 74 68 65 20 53 45 4c 45 43 54 0a  from the SELECT.
26789 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 61 64 64        ** and add
2678a 20 69 74 20 74 6f 20 61 20 74 72 61 6e 73 69 65   it to a transie
2678b 6e 74 20 74 61 62 6c 65 20 73 72 63 54 61 62 2e  nt table srcTab.
2678c 20 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65 72    The code gener
2678d 61 74 65 64 0a 20 20 20 20 20 20 2a 2a 20 68 65  ated.      ** he
2678e 72 65 20 69 73 20 66 72 6f 6d 20 74 68 65 20 34  re is from the 4
2678f 74 68 20 74 65 6d 70 6c 61 74 65 3a 0a 20 20 20  th template:.   
26790 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20     **.      **  
26791 20 20 20 20 42 3a 20 6f 70 65 6e 20 74 65 6d 70      B: open temp
26792 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20   table.      ** 
26793 20 20 20 20 20 4c 3a 20 79 69 65 6c 64 20 58 0a       L: yield X.
26794 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20        **        
26795 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 4d 0a 20   if EOF goto M. 
26796 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20       **         
26797 69 6e 73 65 72 74 20 72 6f 77 20 66 72 6f 6d 20  insert row from 
26798 52 2e 2e 52 2b 6e 20 69 6e 74 6f 20 74 65 6d 70  R..R+n into temp
26799 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20   table.      ** 
2679a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 4c 0a 20          goto L. 
2679b 20 20 20 20 20 2a 2a 20 20 20 20 20 20 4d 3a 20       **      M: 
2679c 2e 2e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ....      */.   
2679d 20 20 20 69 6e 74 20 72 65 67 52 65 63 3b 20 20     int regRec;  
2679e 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
2679f 74 65 72 20 74 6f 20 68 6f 6c 64 20 70 61 63 6b  ter to hold pack
267a0 65 64 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20  ed record */.   
267a1 20 20 20 69 6e 74 20 72 65 67 54 65 6d 70 52 6f     int regTempRo
267a2 77 69 64 3b 20 20 20 20 2f 2a 20 52 65 67 69 73  wid;    /* Regis
267a3 74 65 72 20 74 6f 20 68 6f 6c 64 20 74 65 6d 70  ter to hold temp
267a4 20 74 61 62 6c 65 20 52 4f 57 49 44 20 2a 2f 0a   table ROWID */.
267a5 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 54 6f        int addrTo
267a6 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61  p;         /* La
267a7 62 65 6c 20 22 4c 22 20 2a 2f 0a 20 20 20 20 20  bel "L" */.     
267a8 20 69 6e 74 20 61 64 64 72 49 66 3b 20 20 20 20   int addrIf;    
267a9 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
267aa 20 6f 66 20 6a 75 6d 70 20 74 6f 20 4d 20 2a 2f   of jump to M */
267ab 0a 0a 20 20 20 20 20 20 73 72 63 54 61 62 20 3d  ..      srcTab =
267ac 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
267ad 0a 20 20 20 20 20 20 72 65 67 52 65 63 20 3d 20  .      regRec = 
267ae 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
267af 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  g(pParse);.     
267b0 20 72 65 67 54 65 6d 70 52 6f 77 69 64 20 3d 20   regTempRowid = 
267b1 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
267b2 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  g(pParse);.     
267b3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
267b4 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68  p2(v, OP_OpenEph
267b5 65 6d 65 72 61 6c 2c 20 73 72 63 54 61 62 2c 20  emeral, srcTab, 
267b6 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20  nColumn);.      
267b7 61 64 64 72 54 6f 70 20 3d 20 73 71 6c 69 74 65  addrTop = sqlite
267b8 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
267b9 50 5f 59 69 65 6c 64 2c 20 64 65 73 74 2e 69 50  P_Yield, dest.iP
267ba 61 72 6d 29 3b 0a 20 20 20 20 20 20 61 64 64 72  arm);.      addr
267bb 49 66 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  If = sqlite3Vdbe
267bc 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c  AddOp1(v, OP_If,
267bd 20 72 65 67 45 6f 66 29 3b 0a 20 20 20 20 20 20   regEof);.      
267be 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
267bf 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  3(v, OP_MakeReco
267c0 72 64 2c 20 72 65 67 46 72 6f 6d 53 65 6c 65 63  rd, regFromSelec
267c1 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 65 67 52  t, nColumn, regR
267c2 65 63 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ec);.      sqlit
267c3 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
267c4 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 73 72 63  OP_NewRowid, src
267c5 54 61 62 2c 20 72 65 67 54 65 6d 70 52 6f 77 69  Tab, regTempRowi
267c6 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  d);.      sqlite
267c7 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
267c8 50 5f 49 6e 73 65 72 74 2c 20 73 72 63 54 61 62  P_Insert, srcTab
267c9 2c 20 72 65 67 52 65 63 2c 20 72 65 67 54 65 6d  , regRec, regTem
267ca 70 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 73  pRowid);.      s
267cb 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
267cc 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
267cd 61 64 64 72 54 6f 70 29 3b 0a 20 20 20 20 20 20  addrTop);.      
267ce 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
267cf 65 72 65 28 76 2c 20 61 64 64 72 49 66 29 3b 0a  ere(v, addrIf);.
267d0 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c        sqlite3Rel
267d1 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
267d2 73 65 2c 20 72 65 67 52 65 63 29 3b 0a 20 20 20  se, regRec);.   
267d3 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
267d4 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
267d5 20 72 65 67 54 65 6d 70 52 6f 77 69 64 29 3b 0a   regTempRowid);.
267d6 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
267d7 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68     /* This is th
267d8 65 20 63 61 73 65 20 69 66 20 74 68 65 20 64 61  e case if the da
267d9 74 61 20 66 6f 72 20 74 68 65 20 49 4e 53 45 52  ta for the INSER
267da 54 20 69 73 20 63 6f 6d 69 6e 67 20 66 72 6f 6d  T is coming from
267db 20 61 20 56 41 4c 55 45 53 0a 20 20 20 20 2a 2a   a VALUES.    **
267dc 20 63 6c 61 75 73 65 0a 20 20 20 20 2a 2f 0a 20   clause.    */. 
267dd 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73     NameContext s
267de 4e 43 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26  NC;.    memset(&
267df 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  sNC, 0, sizeof(s
267e0 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70 50  NC));.    sNC.pP
267e1 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20  arse = pParse;. 
267e2 20 20 20 73 72 63 54 61 62 20 3d 20 2d 31 3b 0a     srcTab = -1;.
267e3 20 20 20 20 61 73 73 65 72 74 28 20 75 73 65 54      assert( useT
267e4 65 6d 70 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20  empTable==0 );. 
267e5 20 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 4c 69     nColumn = pLi
267e6 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70  st ? pList->nExp
267e7 72 20 3a 20 30 3b 0a 20 20 20 20 66 6f 72 28 69  r : 0;.    for(i
267e8 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69  =0; i<nColumn; i
267e9 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73  ++){.      if( s
267ea 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
267eb 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 4c 69  rNames(&sNC, pLi
267ec 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20  st->a[i].pExpr) 
267ed 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  ){.        goto 
267ee 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
267ef 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
267f0 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72  }..  /* Make sur
267f1 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
267f2 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 73  columns in the s
267f3 6f 75 72 63 65 20 64 61 74 61 20 6d 61 74 63 68  ource data match
267f4 65 73 20 74 68 65 20 6e 75 6d 62 65 72 0a 20 20  es the number.  
267f5 2a 2a 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f  ** of columns to
267f6 20 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74   be inserted int
267f7 6f 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20 2a  o the table..  *
267f8 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61  /.  if( IsVirtua
267f9 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 66  l(pTab) ){.    f
267fa 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e  or(i=0; i<pTab->
267fb 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
267fc 20 20 6e 48 69 64 64 65 6e 20 2b 3d 20 28 49 73    nHidden += (Is
267fd 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 26 70 54  HiddenColumn(&pT
267fe 61 62 2d 3e 61 43 6f 6c 5b 69 5d 29 20 3f 20 31  ab->aCol[i]) ? 1
267ff 20 3a 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d   : 0);.    }.  }
26800 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d  .  if( pColumn==
26801 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 20 26 26 20  0 && nColumn && 
26802 6e 43 6f 6c 75 6d 6e 21 3d 28 70 54 61 62 2d 3e  nColumn!=(pTab->
26803 6e 43 6f 6c 2d 6e 48 69 64 64 65 6e 29 20 29 7b  nCol-nHidden) ){
26804 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
26805 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20  rMsg(pParse, .  
26806 20 20 20 20 20 22 74 61 62 6c 65 20 25 53 20 68       "table %S h
26807 61 73 20 25 64 20 63 6f 6c 75 6d 6e 73 20 62 75  as %d columns bu
26808 74 20 25 64 20 76 61 6c 75 65 73 20 77 65 72 65  t %d values were
26809 20 73 75 70 70 6c 69 65 64 22 2c 0a 20 20 20 20   supplied",.    
2680a 20 20 20 70 54 61 62 4c 69 73 74 2c 20 30 2c 20     pTabList, 0, 
2680b 70 54 61 62 2d 3e 6e 43 6f 6c 2d 6e 48 69 64 64  pTab->nCol-nHidd
2680c 65 6e 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20  en, nColumn);.  
2680d 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c    goto insert_cl
2680e 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28  eanup;.  }.  if(
2680f 20 70 43 6f 6c 75 6d 6e 21 3d 30 20 26 26 20 6e   pColumn!=0 && n
26810 43 6f 6c 75 6d 6e 21 3d 70 43 6f 6c 75 6d 6e 2d  Column!=pColumn-
26811 3e 6e 49 64 20 29 7b 0a 20 20 20 20 73 71 6c 69  >nId ){.    sqli
26812 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
26813 73 65 2c 20 22 25 64 20 76 61 6c 75 65 73 20 66  se, "%d values f
26814 6f 72 20 25 64 20 63 6f 6c 75 6d 6e 73 22 2c 20  or %d columns", 
26815 6e 43 6f 6c 75 6d 6e 2c 20 70 43 6f 6c 75 6d 6e  nColumn, pColumn
26816 2d 3e 6e 49 64 29 3b 0a 20 20 20 20 67 6f 74 6f  ->nId);.    goto
26817 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b   insert_cleanup;
26818 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68  .  }..  /* If th
26819 65 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65  e INSERT stateme
2681a 6e 74 20 69 6e 63 6c 75 64 65 64 20 61 6e 20 49  nt included an I
2681b 44 4c 49 53 54 20 74 65 72 6d 2c 20 74 68 65 6e  DLIST term, then
2681c 20 6d 61 6b 65 20 73 75 72 65 0a 20 20 2a 2a 20   make sure.  ** 
2681d 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20  all elements of 
2681e 74 68 65 20 49 44 4c 49 53 54 20 72 65 61 6c 6c  the IDLIST reall
2681f 79 20 61 72 65 20 63 6f 6c 75 6d 6e 73 20 6f 66  y are columns of
26820 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 0a   the table and .
26821 20 20 2a 2a 20 72 65 6d 65 6d 62 65 72 20 74 68    ** remember th
26822 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 69 63 65 73  e column indices
26823 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74  ..  **.  ** If t
26824 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20  he table has an 
26825 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
26826 4b 45 59 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 74  KEY column and t
26827 68 61 74 20 63 6f 6c 75 6d 6e 0a 20 20 2a 2a 20  hat column.  ** 
26828 69 73 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20  is named in the 
26829 49 44 4c 49 53 54 2c 20 74 68 65 6e 20 72 65 63  IDLIST, then rec
2682a 6f 72 64 20 69 6e 20 74 68 65 20 6b 65 79 43 6f  ord in the keyCo
2682b 6c 75 6d 6e 20 76 61 72 69 61 62 6c 65 0a 20 20  lumn variable.  
2682c 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 69 6e 74  ** the index int
2682d 6f 20 49 44 4c 49 53 54 20 6f 66 20 74 68 65 20  o IDLIST of the 
2682e 70 72 69 6d 61 72 79 20 6b 65 79 20 63 6f 6c 75  primary key colu
2682f 6d 6e 2e 20 20 6b 65 79 43 6f 6c 75 6d 6e 20 69  mn.  keyColumn i
26830 73 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 78  s.  ** the index
26831 20 6f 66 20 74 68 65 20 70 72 69 6d 61 72 79 20   of the primary 
26832 6b 65 79 20 61 73 20 69 74 20 61 70 70 65 61 72  key as it appear
26833 73 20 69 6e 20 49 44 4c 49 53 54 2c 20 6e 6f 74  s in IDLIST, not
26834 20 61 73 0a 20 20 2a 2a 20 69 73 20 61 70 70 65   as.  ** is appe
26835 61 72 73 20 69 6e 20 74 68 65 20 6f 72 69 67 69  ars in the origi
26836 6e 61 6c 20 74 61 62 6c 65 2e 20 20 28 54 68 65  nal table.  (The
26837 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 70 72   index of the pr
26838 69 6d 61 72 79 0a 20 20 2a 2a 20 6b 65 79 20 69  imary.  ** key i
26839 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74  n the original t
2683a 61 62 6c 65 20 69 73 20 70 54 61 62 2d 3e 69 50  able is pTab->iP
2683b 4b 65 79 2e 29 0a 20 20 2a 2f 0a 20 20 69 66 28  Key.).  */.  if(
2683c 20 70 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20   pColumn ){.    
2683d 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 6f 6c 75  for(i=0; i<pColu
2683e 6d 6e 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20  mn->nId; i++){. 
2683f 20 20 20 20 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b       pColumn->a[
26840 69 5d 2e 69 64 78 20 3d 20 2d 31 3b 0a 20 20 20  i].idx = -1;.   
26841 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20   }.    for(i=0; 
26842 69 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20  i<pColumn->nId; 
26843 69 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6f 72 28  i++){.      for(
26844 6a 3d 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f  j=0; j<pTab->nCo
26845 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  l; j++){.       
26846 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
26847 43 6d 70 28 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69  Cmp(pColumn->a[i
26848 5d 2e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 61  ].zName, pTab->a
26849 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 29 3d 3d 30  Col[j].zName)==0
2684a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43   ){.          pC
2684b 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 69 64 78 20  olumn->a[i].idx 
2684c 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 69  = j;.          i
2684d 66 28 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65  f( j==pTab->iPKe
2684e 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  y ){.           
2684f 20 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 69 3b 0a   keyColumn = i;.
26850 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
26851 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
26852 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
26853 20 20 20 20 20 69 66 28 20 6a 3e 3d 70 54 61 62       if( j>=pTab
26854 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20  ->nCol ){.      
26855 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 52    if( sqlite3IsR
26856 6f 77 69 64 28 70 43 6f 6c 75 6d 6e 2d 3e 61 5b  owid(pColumn->a[
26857 69 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20  i].zName) ){.   
26858 20 20 20 20 20 20 20 6b 65 79 43 6f 6c 75 6d 6e         keyColumn
26859 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 7d 65   = i;.        }e
2685a 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73  lse{.          s
2685b 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2685c 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 53  Parse, "table %S
2685d 20 68 61 73 20 6e 6f 20 63 6f 6c 75 6d 6e 20 6e   has no column n
2685e 61 6d 65 64 20 25 73 22 2c 0a 20 20 20 20 20 20  amed %s",.      
2685f 20 20 20 20 20 20 20 20 70 54 61 62 4c 69 73 74          pTabList
26860 2c 20 30 2c 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b  , 0, pColumn->a[
26861 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  i].zName);.     
26862 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72       pParse->nEr
26863 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 67  r++;.          g
26864 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e  oto insert_clean
26865 75 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  up;.        }.  
26866 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
26867 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69  .  /* If there i
26868 73 20 6e 6f 20 49 44 4c 49 53 54 20 74 65 72 6d  s no IDLIST term
26869 20 62 75 74 20 74 68 65 20 74 61 62 6c 65 20 68   but the table h
2686a 61 73 20 61 6e 20 69 6e 74 65 67 65 72 20 70 72  as an integer pr
2686b 69 6d 61 72 79 0a 20 20 2a 2a 20 6b 65 79 2c 20  imary.  ** key, 
2686c 74 68 65 20 73 65 74 20 74 68 65 20 6b 65 79 43  the set the keyC
2686d 6f 6c 75 6d 6e 20 76 61 72 69 61 62 6c 65 20 74  olumn variable t
2686e 6f 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65  o the primary ke
2686f 79 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 0a 20  y column index. 
26870 20 2a 2a 20 69 6e 20 74 68 65 20 6f 72 69 67 69   ** in the origi
26871 6e 61 6c 20 74 61 62 6c 65 20 64 65 66 69 6e 69  nal table defini
26872 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  tion..  */.  if(
26873 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 6e   pColumn==0 && n
26874 43 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20 20  Column>0 ){.    
26875 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 70 54 61 62  keyColumn = pTab
26876 2d 3e 69 50 4b 65 79 3b 0a 20 20 7d 0a 20 20 20  ->iPKey;.  }.   
26877 20 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a   .  /* Initializ
26878 65 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 72  e the count of r
26879 6f 77 73 20 74 6f 20 62 65 20 69 6e 73 65 72 74  ows to be insert
2687a 65 64 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62  ed.  */.  if( db
2687b 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
2687c 5f 43 6f 75 6e 74 52 6f 77 73 20 29 7b 0a 20 20  _CountRows ){.  
2687d 20 20 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20    regRowCount = 
2687e 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2687f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26880 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
26881 67 65 72 2c 20 30 2c 20 72 65 67 52 6f 77 43 6f  ger, 0, regRowCo
26882 75 6e 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  unt);.  }..  /* 
26883 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61  If this is not a
26884 20 76 69 65 77 2c 20 6f 70 65 6e 20 74 68 65 20   view, open the 
26885 74 61 62 6c 65 20 61 6e 64 20 61 6e 64 20 61 6c  table and and al
26886 6c 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69  l indices */.  i
26887 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20  f( !isView ){.  
26888 20 20 69 6e 74 20 6e 49 64 78 3b 0a 0a 20 20 20    int nIdx;..   
26889 20 62 61 73 65 43 75 72 20 3d 20 70 50 61 72 73   baseCur = pPars
2688a 65 2d 3e 6e 54 61 62 3b 0a 20 20 20 20 6e 49 64  e->nTab;.    nId
2688b 78 20 3d 20 73 71 6c 69 74 65 33 4f 70 65 6e 54  x = sqlite3OpenT
2688c 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 70  ableAndIndices(p
2688d 50 61 72 73 65 2c 20 70 54 61 62 2c 20 62 61 73  Parse, pTab, bas
2688e 65 43 75 72 2c 20 4f 50 5f 4f 70 65 6e 57 72 69  eCur, OP_OpenWri
2688f 74 65 29 3b 0a 20 20 20 20 61 52 65 67 49 64 78  te);.    aRegIdx
26890 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
26891 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66  ocRaw(db, sizeof
26892 28 69 6e 74 29 2a 28 6e 49 64 78 2b 31 29 29 3b  (int)*(nIdx+1));
26893 0a 20 20 20 20 69 66 28 20 61 52 65 67 49 64 78  .    if( aRegIdx
26894 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  ==0 ){.      got
26895 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70  o insert_cleanup
26896 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28  ;.    }.    for(
26897 69 3d 30 3b 20 69 3c 6e 49 64 78 3b 20 69 2b 2b  i=0; i<nIdx; i++
26898 29 7b 0a 20 20 20 20 20 20 61 52 65 67 49 64 78  ){.      aRegIdx
26899 5b 69 5d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  [i] = ++pParse->
2689a 6e 4d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  nMem;.    }.  }.
2689b 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68  .  /* This is th
2689c 65 20 74 6f 70 20 6f 66 20 74 68 65 20 6d 61 69  e top of the mai
2689d 6e 20 69 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f 70  n insertion loop
2689e 20 2a 2f 0a 20 20 69 66 28 20 75 73 65 54 65 6d   */.  if( useTem
2689f 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a  pTable ){.    /*
268a0 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 64 65   This block code
268a1 73 20 74 68 65 20 74 6f 70 20 6f 66 20 6c 6f 6f  s the top of loo
268a2 70 20 6f 6e 6c 79 2e 20 20 54 68 65 20 63 6f 6d  p only.  The com
268a3 70 6c 65 74 65 20 6c 6f 6f 70 20 69 73 20 74 68  plete loop is th
268a4 65 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69  e.    ** followi
268a5 6e 67 20 70 73 65 75 64 6f 63 6f 64 65 20 28 74  ng pseudocode (t
268a6 65 6d 70 6c 61 74 65 20 34 29 3a 0a 20 20 20 20  emplate 4):.    
268a7 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  **.    **       
268a8 20 20 72 65 77 69 6e 64 20 74 65 6d 70 20 74 61    rewind temp ta
268a9 62 6c 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  ble.    **      
268aa 43 3a 20 6c 6f 6f 70 20 6f 76 65 72 20 72 6f 77  C: loop over row
268ab 73 20 6f 66 20 69 6e 74 65 72 6d 65 64 69 61 74  s of intermediat
268ac 65 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 20  e table.    **  
268ad 20 20 20 20 20 20 20 20 20 74 72 61 6e 73 66 65           transfe
268ae 72 20 76 61 6c 75 65 73 20 66 6f 72 6d 20 69 6e  r values form in
268af 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65  termediate table
268b0 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 20 20   into <table>.  
268b1 20 20 2a 2a 20 20 20 20 20 20 20 20 20 65 6e 64    **         end
268b2 20 6c 6f 6f 70 0a 20 20 20 20 2a 2a 20 20 20 20   loop.    **    
268b3 20 20 44 3a 20 2e 2e 2e 0a 20 20 20 20 2a 2f 0a    D: ....    */.
268b4 20 20 20 20 61 64 64 72 49 6e 73 54 6f 70 20 3d      addrInsTop =
268b5 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
268b6 70 31 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c  p1(v, OP_Rewind,
268b7 20 73 72 63 54 61 62 29 3b 0a 20 20 20 20 61 64   srcTab);.    ad
268b8 64 72 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33  drCont = sqlite3
268b9 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
268ba 76 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  v);.  }else if( 
268bb 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 2f  pSelect ){.    /
268bc 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 64  * This block cod
268bd 65 73 20 74 68 65 20 74 6f 70 20 6f 66 20 6c 6f  es the top of lo
268be 6f 70 20 6f 6e 6c 79 2e 20 20 54 68 65 20 63 6f  op only.  The co
268bf 6d 70 6c 65 74 65 20 6c 6f 6f 70 20 69 73 20 74  mplete loop is t
268c0 68 65 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77  he.    ** follow
268c1 69 6e 67 20 70 73 65 75 64 6f 63 6f 64 65 20 28  ing pseudocode (
268c2 74 65 6d 70 6c 61 74 65 20 33 29 3a 0a 20 20 20  template 3):.   
268c3 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   **.    **      
268c4 43 3a 20 79 69 65 6c 64 20 58 0a 20 20 20 20 2a  C: yield X.    *
268c5 2a 20 20 20 20 20 20 20 20 20 69 66 20 45 4f 46  *         if EOF
268c6 20 67 6f 74 6f 20 44 0a 20 20 20 20 2a 2a 20 20   goto D.    **  
268c7 20 20 20 20 20 20 20 69 6e 73 65 72 74 20 74 68         insert th
268c8 65 20 73 65 6c 65 63 74 20 72 65 73 75 6c 74 20  e select result 
268c9 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 20 66 72 6f  into <table> fro
268ca 6d 20 52 2e 2e 52 2b 6e 0a 20 20 20 20 2a 2a 20  m R..R+n.    ** 
268cb 20 20 20 20 20 20 20 20 67 6f 74 6f 20 43 0a 20          goto C. 
268cc 20 20 20 2a 2a 20 20 20 20 20 20 44 3a 20 2e 2e     **      D: ..
268cd 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 64 64  ..    */.    add
268ce 72 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33 56  rCont = sqlite3V
268cf 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
268d0 59 69 65 6c 64 2c 20 64 65 73 74 2e 69 50 61 72  Yield, dest.iPar
268d1 6d 29 3b 0a 20 20 20 20 61 64 64 72 49 6e 73 54  m);.    addrInsT
268d2 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  op = sqlite3Vdbe
268d3 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c  AddOp1(v, OP_If,
268d4 20 72 65 67 45 6f 66 29 3b 0a 20 20 7d 0a 0a 20   regEof);.  }.. 
268d5 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 72 65 67   /* Allocate reg
268d6 69 73 74 65 72 73 20 66 6f 72 20 68 6f 6c 64 69  isters for holdi
268d7 6e 67 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20  ng the rowid of 
268d8 74 68 65 20 6e 65 77 20 72 6f 77 2c 0a 20 20 2a  the new row,.  *
268d9 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  * the content of
268da 20 74 68 65 20 6e 65 77 20 72 6f 77 2c 20 61 6e   the new row, an
268db 64 20 74 68 65 20 61 73 73 65 6d 62 6c 69 65 64  d the assemblied
268dc 20 72 6f 77 20 72 65 63 6f 72 64 2e 0a 20 20 2a   row record..  *
268dd 2f 0a 20 20 72 65 67 52 65 63 6f 72 64 20 3d 20  /.  regRecord = 
268de 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
268df 20 20 72 65 67 52 6f 77 69 64 20 3d 20 72 65 67    regRowid = reg
268e0 49 6e 73 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d  Ins = pParse->nM
268e1 65 6d 2b 31 3b 0a 20 20 70 50 61 72 73 65 2d 3e  em+1;.  pParse->
268e2 6e 4d 65 6d 20 2b 3d 20 70 54 61 62 2d 3e 6e 43  nMem += pTab->nC
268e3 6f 6c 20 2b 20 31 3b 0a 20 20 69 66 28 20 49 73  ol + 1;.  if( Is
268e4 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
268e5 0a 20 20 20 20 72 65 67 52 6f 77 69 64 2b 2b 3b  .    regRowid++;
268e6 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65  .    pParse->nMe
268e7 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 67 44 61  m++;.  }.  regDa
268e8 74 61 20 3d 20 72 65 67 52 6f 77 69 64 2b 31 3b  ta = regRowid+1;
268e9 0a 0a 20 20 2f 2a 20 52 75 6e 20 74 68 65 20 42  ..  /* Run the B
268ea 45 46 4f 52 45 20 61 6e 64 20 49 4e 53 54 45 41  EFORE and INSTEA
268eb 44 20 4f 46 20 74 72 69 67 67 65 72 73 2c 20 69  D OF triggers, i
268ec 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 0a  f there are any.
268ed 20 20 2a 2f 0a 20 20 65 6e 64 4f 66 4c 6f 6f 70    */.  endOfLoop
268ee 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
268ef 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 69 66  keLabel(v);.  if
268f0 28 20 74 6d 61 73 6b 20 26 20 54 52 49 47 47 45  ( tmask & TRIGGE
268f1 52 5f 42 45 46 4f 52 45 20 29 7b 0a 20 20 20 20  R_BEFORE ){.    
268f2 69 6e 74 20 72 65 67 43 6f 6c 73 20 3d 20 73 71  int regCols = sq
268f3 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67  lite3GetTempRang
268f4 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e  e(pParse, pTab->
268f5 6e 43 6f 6c 2b 31 29 3b 0a 0a 20 20 20 20 2f 2a  nCol+1);..    /*
268f6 20 62 75 69 6c 64 20 74 68 65 20 4e 45 57 2e 2a   build the NEW.*
268f7 20 72 65 66 65 72 65 6e 63 65 20 72 6f 77 2e 20   reference row. 
268f8 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 74 68   Note that if th
268f9 65 72 65 20 69 73 20 61 6e 20 49 4e 54 45 47 45  ere is an INTEGE
268fa 52 0a 20 20 20 20 2a 2a 20 50 52 49 4d 41 52 59  R.    ** PRIMARY
268fb 20 4b 45 59 20 69 6e 74 6f 20 77 68 69 63 68 20   KEY into which 
268fc 61 20 4e 55 4c 4c 20 69 73 20 62 65 69 6e 67 20  a NULL is being 
268fd 69 6e 73 65 72 74 65 64 2c 20 74 68 61 74 20 4e  inserted, that N
268fe 55 4c 4c 20 77 69 6c 6c 20 62 65 0a 20 20 20 20  ULL will be.    
268ff 2a 2a 20 74 72 61 6e 73 6c 61 74 65 64 20 69 6e  ** translated in
26900 74 6f 20 61 20 75 6e 69 71 75 65 20 49 44 20 66  to a unique ID f
26901 6f 72 20 74 68 65 20 72 6f 77 2e 20 20 42 75 74  or the row.  But
26902 20 6f 6e 20 61 20 42 45 46 4f 52 45 20 74 72 69   on a BEFORE tri
26903 67 67 65 72 2c 0a 20 20 20 20 2a 2a 20 77 65 20  gger,.    ** we 
26904 64 6f 20 6e 6f 74 20 6b 6e 6f 77 20 77 68 61 74  do not know what
26905 20 74 68 65 20 75 6e 69 71 75 65 20 49 44 20 77   the unique ID w
26906 69 6c 6c 20 62 65 20 28 62 65 63 61 75 73 65 20  ill be (because 
26907 74 68 65 20 69 6e 73 65 72 74 20 68 61 73 0a 20  the insert has. 
26908 20 20 20 2a 2a 20 6e 6f 74 20 68 61 70 70 65 6e     ** not happen
26909 65 64 20 79 65 74 29 20 73 6f 20 77 65 20 73 75  ed yet) so we su
2690a 62 73 74 69 74 75 74 65 20 61 20 72 6f 77 69 64  bstitute a rowid
2690b 20 6f 66 20 2d 31 0a 20 20 20 20 2a 2f 0a 20 20   of -1.    */.  
2690c 20 20 69 66 28 20 6b 65 79 43 6f 6c 75 6d 6e 3c    if( keyColumn<
2690d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
2690e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2690f 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20  OP_Integer, -1, 
26910 72 65 67 43 6f 6c 73 29 3b 0a 20 20 20 20 7d 65  regCols);.    }e
26911 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a  lse{.      int j
26912 31 3b 0a 20 20 20 20 20 20 69 66 28 20 75 73 65  1;.      if( use
26913 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20  TempTable ){.   
26914 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26915 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
26916 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6b 65 79  umn, srcTab, key
26917 43 6f 6c 75 6d 6e 2c 20 72 65 67 43 6f 6c 73 29  Column, regCols)
26918 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
26919 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
2691a 53 65 6c 65 63 74 3d 3d 30 20 29 3b 20 20 2f 2a  Select==0 );  /*
2691b 20 4f 74 68 65 72 77 69 73 65 20 75 73 65 54 65   Otherwise useTe
2691c 6d 70 54 61 62 6c 65 20 69 73 20 74 72 75 65 20  mpTable is true 
2691d 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  */.        sqlit
2691e 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
2691f 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6b 65 79 43  e, pList->a[keyC
26920 6f 6c 75 6d 6e 5d 2e 70 45 78 70 72 2c 20 72 65  olumn].pExpr, re
26921 67 43 6f 6c 73 29 3b 0a 20 20 20 20 20 20 7d 0a  gCols);.      }.
26922 20 20 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74        j1 = sqlit
26923 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
26924 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 43  OP_NotNull, regC
26925 6f 6c 73 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ols);.      sqli
26926 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
26927 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c   OP_Integer, -1,
26928 20 72 65 67 43 6f 6c 73 29 3b 0a 20 20 20 20 20   regCols);.     
26929 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
2692a 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20  Here(v, j1);.   
2692b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2692c 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42  dOp1(v, OP_MustB
2692d 65 49 6e 74 2c 20 72 65 67 43 6f 6c 73 29 3b 0a  eInt, regCols);.
2692e 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 61      }..    /* Ca
2692f 6e 6e 6f 74 20 68 61 76 65 20 74 72 69 67 67 65  nnot have trigge
26930 72 73 20 6f 6e 20 61 20 76 69 72 74 75 61 6c 20  rs on a virtual 
26931 74 61 62 6c 65 2e 20 49 66 20 69 74 20 77 65 72  table. If it wer
26932 65 20 70 6f 73 73 69 62 6c 65 2c 0a 20 20 20 20  e possible,.    
26933 2a 2a 20 74 68 69 73 20 62 6c 6f 63 6b 20 77 6f  ** this block wo
26934 75 6c 64 20 68 61 76 65 20 74 6f 20 61 63 63 6f  uld have to acco
26935 75 6e 74 20 66 6f 72 20 68 69 64 64 65 6e 20 63  unt for hidden c
26936 6f 6c 75 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20  olumn..    */.  
26937 20 20 61 73 73 65 72 74 28 20 21 49 73 56 69 72    assert( !IsVir
26938 74 75 61 6c 28 70 54 61 62 29 20 29 3b 0a 0a 20  tual(pTab) );.. 
26939 20 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65     /* Create the
2693a 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 64 61 74 61   new column data
2693b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28  .    */.    for(
2693c 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f  i=0; i<pTab->nCo
2693d 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  l; i++){.      i
2693e 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b  f( pColumn==0 ){
2693f 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 69 3b 0a  .        j = i;.
26940 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
26941 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
26942 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 6a 2b  pColumn->nId; j+
26943 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
26944 28 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 6a 5d 2e  ( pColumn->a[j].
26945 69 64 78 3d 3d 69 20 29 20 62 72 65 61 6b 3b 0a  idx==i ) break;.
26946 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
26947 7d 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c  }.      if( pCol
26948 75 6d 6e 20 26 26 20 6a 3e 3d 70 43 6f 6c 75 6d  umn && j>=pColum
26949 6e 2d 3e 6e 49 64 20 29 7b 0a 20 20 20 20 20 20  n->nId ){.      
2694a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
2694b 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e  e(pParse, pTab->
2694c 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c 20 72  aCol[i].pDflt, r
2694d 65 67 43 6f 6c 73 2b 69 2b 31 29 3b 0a 20 20 20  egCols+i+1);.   
2694e 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 73 65     }else if( use
2694f 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20  TempTable ){.   
26950 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26951 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
26952 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6a 2c 20  umn, srcTab, j, 
26953 72 65 67 43 6f 6c 73 2b 69 2b 31 29 3b 20 0a 20  regCols+i+1); . 
26954 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
26955 20 20 20 20 61 73 73 65 72 74 28 20 70 53 65 6c      assert( pSel
26956 65 63 74 3d 3d 30 20 29 3b 20 2f 2a 20 4f 74 68  ect==0 ); /* Oth
26957 65 72 77 69 73 65 20 75 73 65 54 65 6d 70 54 61  erwise useTempTa
26958 62 6c 65 20 69 73 20 74 72 75 65 20 2a 2f 0a 20  ble is true */. 
26959 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
2695a 70 72 43 6f 64 65 41 6e 64 43 61 63 68 65 28 70  prCodeAndCache(p
2695b 50 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b  Parse, pList->a[
2695c 6a 5d 2e 70 45 78 70 72 2c 20 72 65 67 43 6f 6c  j].pExpr, regCol
2695d 73 2b 69 2b 31 29 3b 0a 20 20 20 20 20 20 7d 0a  s+i+1);.      }.
2695e 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66      }..    /* If
2695f 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45   this is an INSE
26960 52 54 20 6f 6e 20 61 20 76 69 65 77 20 77 69 74  RT on a view wit
26961 68 20 61 6e 20 49 4e 53 54 45 41 44 20 4f 46 20  h an INSTEAD OF 
26962 49 4e 53 45 52 54 20 74 72 69 67 67 65 72 2c 0a  INSERT trigger,.
26963 20 20 20 20 2a 2a 20 64 6f 20 6e 6f 74 20 61 74      ** do not at
26964 74 65 6d 70 74 20 61 6e 79 20 63 6f 6e 76 65 72  tempt any conver
26965 73 69 6f 6e 73 20 62 65 66 6f 72 65 20 61 73 73  sions before ass
26966 65 6d 62 6c 69 6e 67 20 74 68 65 20 72 65 63 6f  embling the reco
26967 72 64 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74 68  rd..    ** If th
26968 69 73 20 69 73 20 61 20 72 65 61 6c 20 74 61 62  is is a real tab
26969 6c 65 2c 20 61 74 74 65 6d 70 74 20 63 6f 6e 76  le, attempt conv
2696a 65 72 73 69 6f 6e 73 20 61 73 20 72 65 71 75 69  ersions as requi
2696b 72 65 64 20 62 79 20 74 68 65 0a 20 20 20 20 2a  red by the.    *
2696c 2a 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 61  * table column a
2696d 66 66 69 6e 69 74 69 65 73 2e 0a 20 20 20 20 2a  ffinities..    *
2696e 2f 0a 20 20 20 20 69 66 28 20 21 69 73 56 69 65  /.    if( !isVie
2696f 77 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  w ){.      sqlit
26970 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
26971 4f 50 5f 41 66 66 69 6e 69 74 79 2c 20 72 65 67  OP_Affinity, reg
26972 43 6f 6c 73 2b 31 2c 20 70 54 61 62 2d 3e 6e 43  Cols+1, pTab->nC
26973 6f 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ol);.      sqlit
26974 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79 53  e3TableAffinityS
26975 74 72 28 76 2c 20 70 54 61 62 29 3b 0a 20 20 20  tr(v, pTab);.   
26976 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69 72 65 20   }..    /* Fire 
26977 42 45 46 4f 52 45 20 6f 72 20 49 4e 53 54 45 41  BEFORE or INSTEA
26978 44 20 4f 46 20 74 72 69 67 67 65 72 73 20 2a 2f  D OF triggers */
26979 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65  .    sqlite3Code
2697a 52 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73  RowTrigger(pPars
2697b 65 2c 20 70 54 72 69 67 67 65 72 2c 20 54 4b 5f  e, pTrigger, TK_
2697c 49 4e 53 45 52 54 2c 20 30 2c 20 54 52 49 47 47  INSERT, 0, TRIGG
2697d 45 52 5f 42 45 46 4f 52 45 2c 20 0a 20 20 20 20  ER_BEFORE, .    
2697e 20 20 20 20 70 54 61 62 2c 20 2d 31 2c 20 72 65      pTab, -1, re
2697f 67 43 6f 6c 73 2d 70 54 61 62 2d 3e 6e 43 6f 6c  gCols-pTab->nCol
26980 2d 31 2c 20 6f 6e 45 72 72 6f 72 2c 20 65 6e 64  -1, onError, end
26981 4f 66 4c 6f 6f 70 29 3b 0a 0a 20 20 20 20 73 71  OfLoop);..    sq
26982 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
26983 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65  Range(pParse, re
26984 67 43 6f 6c 73 2c 20 70 54 61 62 2d 3e 6e 43 6f  gCols, pTab->nCo
26985 6c 2b 31 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  l+1);.  }..  /* 
26986 50 75 73 68 20 74 68 65 20 72 65 63 6f 72 64 20  Push the record 
26987 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 6e  number for the n
26988 65 77 20 65 6e 74 72 79 20 6f 6e 74 6f 20 74 68  ew entry onto th
26989 65 20 73 74 61 63 6b 2e 20 20 54 68 65 0a 20 20  e stack.  The.  
2698a 2a 2a 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  ** record number
2698b 20 69 73 20 61 20 72 61 6e 64 6f 6d 6c 79 20 67   is a randomly g
2698c 65 6e 65 72 61 74 65 20 69 6e 74 65 67 65 72 20  enerate integer 
2698d 63 72 65 61 74 65 64 20 62 79 20 4e 65 77 52 6f  created by NewRo
2698e 77 69 64 0a 20 20 2a 2a 20 65 78 63 65 70 74 20  wid.  ** except 
2698f 77 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 68  when the table h
26990 61 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52  as an INTEGER PR
26991 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e  IMARY KEY column
26992 2c 20 69 6e 20 77 68 69 63 68 0a 20 20 2a 2a 20  , in which.  ** 
26993 63 61 73 65 20 74 68 65 20 72 65 63 6f 72 64 20  case the record 
26994 6e 75 6d 62 65 72 20 69 73 20 74 68 65 20 73 61  number is the sa
26995 6d 65 20 61 73 20 74 68 61 74 20 63 6f 6c 75 6d  me as that colum
26996 6e 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  n. .  */.  if( !
26997 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 69 66  isView ){.    if
26998 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
26999 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  ) ){.      /* Th
2699a 65 20 72 6f 77 20 74 68 61 74 20 74 68 65 20 56  e row that the V
2699b 55 70 64 61 74 65 20 6f 70 63 6f 64 65 20 77 69  Update opcode wi
2699c 6c 6c 20 64 65 6c 65 74 65 3a 20 6e 6f 6e 65 20  ll delete: none 
2699d 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
2699e 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2699f 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 49 6e 73  _Null, 0, regIns
269a0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
269a1 20 6b 65 79 43 6f 6c 75 6d 6e 3e 3d 30 20 29 7b   keyColumn>=0 ){
269a2 0a 20 20 20 20 20 20 69 66 28 20 75 73 65 54 65  .      if( useTe
269a3 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  mpTable ){.     
269a4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
269a5 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
269a6 6e 2c 20 73 72 63 54 61 62 2c 20 6b 65 79 43 6f  n, srcTab, keyCo
269a7 6c 75 6d 6e 2c 20 72 65 67 52 6f 77 69 64 29 3b  lumn, regRowid);
269a8 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
269a9 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20   pSelect ){.    
269aa 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
269ab 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70  ddOp2(v, OP_SCop
269ac 79 2c 20 72 65 67 46 72 6f 6d 53 65 6c 65 63 74  y, regFromSelect
269ad 2b 6b 65 79 43 6f 6c 75 6d 6e 2c 20 72 65 67 52  +keyColumn, regR
269ae 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 7d 65 6c  owid);.      }el
269af 73 65 7b 0a 20 20 20 20 20 20 20 20 56 64 62 65  se{.        Vdbe
269b0 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 20 20 20  Op *pOp;.       
269b1 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
269b2 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e  (pParse, pList->
269b3 61 5b 6b 65 79 43 6f 6c 75 6d 6e 5d 2e 70 45 78  a[keyColumn].pEx
269b4 70 72 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  pr, regRowid);. 
269b5 20 20 20 20 20 20 20 70 4f 70 20 3d 20 73 71 6c         pOp = sql
269b6 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c  ite3VdbeGetOp(v,
269b7 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66   -1);.        if
269b8 28 20 41 4c 57 41 59 53 28 70 4f 70 29 20 26 26  ( ALWAYS(pOp) &&
269b9 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
269ba 5f 4e 75 6c 6c 20 26 26 20 21 49 73 56 69 72 74  _Null && !IsVirt
269bb 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
269bc 20 20 20 20 20 20 20 61 70 70 65 6e 64 46 6c 61         appendFla
269bd 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  g = 1;.         
269be 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f   pOp->opcode = O
269bf 50 5f 4e 65 77 52 6f 77 69 64 3b 0a 20 20 20 20  P_NewRowid;.    
269c0 20 20 20 20 20 20 70 4f 70 2d 3e 70 31 20 3d 20        pOp->p1 = 
269c1 62 61 73 65 43 75 72 3b 0a 20 20 20 20 20 20 20  baseCur;.       
269c2 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20 72 65 67     pOp->p2 = reg
269c3 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 20 20 20  Rowid;.         
269c4 20 70 4f 70 2d 3e 70 33 20 3d 20 72 65 67 41 75   pOp->p3 = regAu
269c5 74 6f 69 6e 63 3b 0a 20 20 20 20 20 20 20 20 7d  toinc;.        }
269c6 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f  .      }.      /
269c7 2a 20 49 66 20 74 68 65 20 50 52 49 4d 41 52 59  * If the PRIMARY
269c8 20 4b 45 59 20 65 78 70 72 65 73 73 69 6f 6e 20   KEY expression 
269c9 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 75 73  is NULL, then us
269ca 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 0a 20 20  e OP_NewRowid.  
269cb 20 20 20 20 2a 2a 20 74 6f 20 67 65 6e 65 72 61      ** to genera
269cc 74 65 20 61 20 75 6e 69 71 75 65 20 70 72 69 6d  te a unique prim
269cd 61 72 79 20 6b 65 79 20 76 61 6c 75 65 2e 0a 20  ary key value.. 
269ce 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
269cf 28 20 21 61 70 70 65 6e 64 46 6c 61 67 20 29 7b  ( !appendFlag ){
269d0 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 31 3b  .        int j1;
269d1 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 49 73  .        if( !Is
269d2 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
269d3 0a 20 20 20 20 20 20 20 20 20 20 6a 31 20 3d 20  .          j1 = 
269d4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
269d5 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c  1(v, OP_NotNull,
269d6 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
269d7 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
269d8 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65  eAddOp3(v, OP_Ne
269d9 77 52 6f 77 69 64 2c 20 62 61 73 65 43 75 72 2c  wRowid, baseCur,
269da 20 72 65 67 52 6f 77 69 64 2c 20 72 65 67 41 75   regRowid, regAu
269db 74 6f 69 6e 63 29 3b 0a 20 20 20 20 20 20 20 20  toinc);.        
269dc 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
269dd 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20  pHere(v, j1);.  
269de 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
269df 20 20 20 20 20 20 20 6a 31 20 3d 20 73 71 6c 69         j1 = sqli
269e0 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
269e1 64 72 28 76 29 3b 0a 20 20 20 20 20 20 20 20 20  dr(v);.         
269e2 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
269e3 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c  p2(v, OP_IsNull,
269e4 20 72 65 67 52 6f 77 69 64 2c 20 6a 31 2b 32 29   regRowid, j1+2)
269e5 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
269e6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
269e7 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74  ddOp1(v, OP_Must
269e8 42 65 49 6e 74 2c 20 72 65 67 52 6f 77 69 64 29  BeInt, regRowid)
269e9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
269ea 6c 73 65 20 69 66 28 20 49 73 56 69 72 74 75 61  lse if( IsVirtua
269eb 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20  l(pTab) ){.     
269ec 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
269ed 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30  p2(v, OP_Null, 0
269ee 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
269ef 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
269f0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
269f1 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20  v, OP_NewRowid, 
269f2 62 61 73 65 43 75 72 2c 20 72 65 67 52 6f 77 69  baseCur, regRowi
269f3 64 2c 20 72 65 67 41 75 74 6f 69 6e 63 29 3b 0a  d, regAutoinc);.
269f4 20 20 20 20 20 20 61 70 70 65 6e 64 46 6c 61 67        appendFlag
269f5 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 1;.    }.    
269f6 61 75 74 6f 49 6e 63 53 74 65 70 28 70 50 61 72  autoIncStep(pPar
269f7 73 65 2c 20 72 65 67 41 75 74 6f 69 6e 63 2c 20  se, regAutoinc, 
269f8 72 65 67 52 6f 77 69 64 29 3b 0a 0a 20 20 20 20  regRowid);..    
269f9 2f 2a 20 50 75 73 68 20 6f 6e 74 6f 20 74 68 65  /* Push onto the
269fa 20 73 74 61 63 6b 2c 20 64 61 74 61 20 66 6f 72   stack, data for
269fb 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 6f 66 20   all columns of 
269fc 74 68 65 20 6e 65 77 20 65 6e 74 72 79 2c 20 62  the new entry, b
269fd 65 67 69 6e 6e 69 6e 67 0a 20 20 20 20 2a 2a 20  eginning.    ** 
269fe 77 69 74 68 20 74 68 65 20 66 69 72 73 74 20 63  with the first c
269ff 6f 6c 75 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20  olumn..    */.  
26a00 20 20 6e 48 69 64 64 65 6e 20 3d 20 30 3b 0a 20    nHidden = 0;. 
26a01 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54     for(i=0; i<pT
26a02 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  ab->nCol; i++){.
26a03 20 20 20 20 20 20 69 6e 74 20 69 52 65 67 53 74        int iRegSt
26a04 6f 72 65 20 3d 20 72 65 67 52 6f 77 69 64 2b 31  ore = regRowid+1
26a05 2b 69 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3d  +i;.      if( i=
26a06 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a  =pTab->iPKey ){.
26a07 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76          /* The v
26a08 61 6c 75 65 20 6f 66 20 74 68 65 20 49 4e 54 45  alue of the INTE
26a09 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20  GER PRIMARY KEY 
26a0a 63 6f 6c 75 6d 6e 20 69 73 20 61 6c 77 61 79 73  column is always
26a0b 20 61 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 20   a NULL..       
26a0c 20 2a 2a 20 57 68 65 6e 65 76 65 72 20 74 68 69   ** Whenever thi
26a0d 73 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65 61 64  s column is read
26a0e 2c 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d  , the record num
26a0f 62 65 72 20 77 69 6c 6c 20 62 65 20 73 75 62 73  ber will be subs
26a10 74 69 74 75 74 65 64 0a 20 20 20 20 20 20 20 20  tituted.        
26a11 2a 2a 20 69 6e 20 69 74 73 20 70 6c 61 63 65 2e  ** in its place.
26a12 20 20 53 6f 20 77 69 6c 6c 20 66 69 6c 6c 20 74    So will fill t
26a13 68 69 73 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20  his column with 
26a14 61 20 4e 55 4c 4c 20 74 6f 20 61 76 6f 69 64 0a  a NULL to avoid.
26a15 20 20 20 20 20 20 20 20 2a 2a 20 74 61 6b 69 6e          ** takin
26a16 67 20 75 70 20 64 61 74 61 20 73 70 61 63 65 20  g up data space 
26a17 77 69 74 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  with information
26a18 20 74 68 61 74 20 77 69 6c 6c 20 6e 65 76 65 72   that will never
26a19 20 62 65 20 75 73 65 64 2e 20 2a 2f 0a 20 20 20   be used. */.   
26a1a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26a1b 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
26a1c 6c 2c 20 30 2c 20 69 52 65 67 53 74 6f 72 65 29  l, 0, iRegStore)
26a1d 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e  ;.        contin
26a1e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ue;.      }.    
26a1f 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30    if( pColumn==0
26a20 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
26a21 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 26  IsHiddenColumn(&
26a22 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 29 20 29  pTab->aCol[i]) )
26a23 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
26a24 72 74 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  rt( IsVirtual(pT
26a25 61 62 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20  ab) );.         
26a26 20 6a 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20   j = -1;.       
26a27 20 20 20 6e 48 69 64 64 65 6e 2b 2b 3b 0a 20 20     nHidden++;.  
26a28 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
26a29 20 20 20 20 20 20 20 6a 20 3d 20 69 20 2d 20 6e         j = i - n
26a2a 48 69 64 64 65 6e 3b 0a 20 20 20 20 20 20 20 20  Hidden;.        
26a2b 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
26a2c 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20         for(j=0; 
26a2d 6a 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20  j<pColumn->nId; 
26a2e 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  j++){.          
26a2f 69 66 28 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 6a  if( pColumn->a[j
26a30 5d 2e 69 64 78 3d 3d 69 20 29 20 62 72 65 61 6b  ].idx==i ) break
26a31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
26a32 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6a 3c    }.      if( j<
26a33 30 20 7c 7c 20 6e 43 6f 6c 75 6d 6e 3d 3d 30 20  0 || nColumn==0 
26a34 7c 7c 20 28 70 43 6f 6c 75 6d 6e 20 26 26 20 6a  || (pColumn && j
26a35 3e 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 29 20  >=pColumn->nId) 
26a36 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
26a37 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
26a38 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d  e, pTab->aCol[i]
26a39 2e 70 44 66 6c 74 2c 20 69 52 65 67 53 74 6f 72  .pDflt, iRegStor
26a3a 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  e);.      }else 
26a3b 69 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65  if( useTempTable
26a3c 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
26a3d 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
26a3e 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63 54   OP_Column, srcT
26a3f 61 62 2c 20 6a 2c 20 69 52 65 67 53 74 6f 72 65  ab, j, iRegStore
26a40 29 3b 20 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  ); .      }else 
26a41 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20  if( pSelect ){. 
26a42 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
26a43 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
26a44 43 6f 70 79 2c 20 72 65 67 46 72 6f 6d 53 65 6c  Copy, regFromSel
26a45 65 63 74 2b 6a 2c 20 69 52 65 67 53 74 6f 72 65  ect+j, iRegStore
26a46 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
26a47 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
26a48 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
26a49 70 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70  pList->a[j].pExp
26a4a 72 2c 20 69 52 65 67 53 74 6f 72 65 29 3b 0a 20  r, iRegStore);. 
26a4b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
26a4c 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f    /* Generate co
26a4d 64 65 20 74 6f 20 63 68 65 63 6b 20 63 6f 6e 73  de to check cons
26a4e 74 72 61 69 6e 74 73 20 61 6e 64 20 67 65 6e 65  traints and gene
26a4f 72 61 74 65 20 69 6e 64 65 78 20 6b 65 79 73 20  rate index keys 
26a50 61 6e 64 0a 20 20 20 20 2a 2a 20 64 6f 20 74 68  and.    ** do th
26a51 65 20 69 6e 73 65 72 74 69 6f 6e 2e 0a 20 20 20  e insertion..   
26a52 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
26a53 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
26a54 41 42 4c 45 0a 20 20 20 20 69 66 28 20 49 73 56  ABLE.    if( IsV
26a55 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
26a56 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
26a57 20 2a 70 56 54 61 62 20 3d 20 28 63 6f 6e 73 74   *pVTab = (const
26a58 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 47   char *)sqlite3G
26a59 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61  etVTable(db, pTa
26a5a 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
26a5b 33 56 74 61 62 4d 61 6b 65 57 72 69 74 61 62 6c  3VtabMakeWritabl
26a5c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b  e(pParse, pTab);
26a5d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
26a5e 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56  beAddOp4(v, OP_V
26a5f 55 70 64 61 74 65 2c 20 31 2c 20 70 54 61 62 2d  Update, 1, pTab-
26a60 3e 6e 43 6f 6c 2b 32 2c 20 72 65 67 49 6e 73 2c  >nCol+2, regIns,
26a61 20 70 56 54 61 62 2c 20 50 34 5f 56 54 41 42 29   pVTab, P4_VTAB)
26a62 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d  ;.      sqlite3M
26a63 61 79 41 62 6f 72 74 28 70 50 61 72 73 65 29 3b  ayAbort(pParse);
26a64 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69  .    }else.#endi
26a65 66 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69 6e  f.    {.      in
26a66 74 20 69 73 52 65 70 6c 61 63 65 3b 20 20 20 20  t isReplace;    
26a67 2f 2a 20 53 65 74 20 74 6f 20 74 72 75 65 20 69  /* Set to true i
26a68 66 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6d 61  f constraints ma
26a69 79 20 63 61 75 73 65 20 61 20 72 65 70 6c 61 63  y cause a replac
26a6a 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74  e */.      sqlit
26a6b 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72  e3GenerateConstr
26a6c 61 69 6e 74 43 68 65 63 6b 73 28 70 50 61 72 73  aintChecks(pPars
26a6d 65 2c 20 70 54 61 62 2c 20 62 61 73 65 43 75 72  e, pTab, baseCur
26a6e 2c 20 72 65 67 49 6e 73 2c 20 61 52 65 67 49 64  , regIns, aRegId
26a6f 78 2c 0a 20 20 20 20 20 20 20 20 20 20 6b 65 79  x,.          key
26a70 43 6f 6c 75 6d 6e 3e 3d 30 2c 20 30 2c 20 6f 6e  Column>=0, 0, on
26a71 45 72 72 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70  Error, endOfLoop
26a72 2c 20 26 69 73 52 65 70 6c 61 63 65 0a 20 20 20  , &isReplace.   
26a73 20 20 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69     );.      sqli
26a74 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65 72  te3CompleteInser
26a75 74 69 6f 6e 28 0a 20 20 20 20 20 20 20 20 20 20  tion(.          
26a76 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 62 61  pParse, pTab, ba
26a77 73 65 43 75 72 2c 20 72 65 67 49 6e 73 2c 20 61  seCur, regIns, a
26a78 52 65 67 49 64 78 2c 20 30 2c 20 61 70 70 65 6e  RegIdx, 0, appen
26a79 64 46 6c 61 67 2c 20 69 73 52 65 70 6c 61 63 65  dFlag, isReplace
26a7a 3d 3d 30 0a 20 20 20 20 20 20 29 3b 0a 20 20 20  ==0.      );.   
26a7b 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64   }.  }..  /* Upd
26a7c 61 74 65 20 74 68 65 20 63 6f 75 6e 74 20 6f 66  ate the count of
26a7d 20 72 6f 77 73 20 74 68 61 74 20 61 72 65 20 69   rows that are i
26a7e 6e 73 65 72 74 65 64 0a 20 20 2a 2f 0a 20 20 69  nserted.  */.  i
26a7f 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20  f( (db->flags & 
26a80 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73  SQLITE_CountRows
26a81 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  )!=0 ){.    sqli
26a82 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
26a83 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67 52   OP_AddImm, regR
26a84 6f 77 43 6f 75 6e 74 2c 20 31 29 3b 0a 20 20 7d  owCount, 1);.  }
26a85 0a 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72  ..  if( pTrigger
26a86 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20   ){.    /* Code 
26a87 41 46 54 45 52 20 74 72 69 67 67 65 72 73 20 2a  AFTER triggers *
26a88 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64  /.    sqlite3Cod
26a89 65 52 6f 77 54 72 69 67 67 65 72 28 70 50 61 72  eRowTrigger(pPar
26a8a 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20 54 4b  se, pTrigger, TK
26a8b 5f 49 4e 53 45 52 54 2c 20 30 2c 20 54 52 49 47  _INSERT, 0, TRIG
26a8c 47 45 52 5f 41 46 54 45 52 2c 20 0a 20 20 20 20  GER_AFTER, .    
26a8d 20 20 20 20 70 54 61 62 2c 20 2d 31 2c 20 72 65      pTab, -1, re
26a8e 67 44 61 74 61 2d 32 2d 70 54 61 62 2d 3e 6e 43  gData-2-pTab->nC
26a8f 6f 6c 2c 20 6f 6e 45 72 72 6f 72 2c 20 65 6e 64  ol, onError, end
26a90 4f 66 4c 6f 6f 70 29 3b 0a 20 20 7d 0a 0a 20 20  OfLoop);.  }..  
26a91 2f 2a 20 54 68 65 20 62 6f 74 74 6f 6d 20 6f 66  /* The bottom of
26a92 20 74 68 65 20 6d 61 69 6e 20 69 6e 73 65 72 74   the main insert
26a93 69 6f 6e 20 6c 6f 6f 70 2c 20 69 66 20 74 68 65  ion loop, if the
26a94 20 64 61 74 61 20 73 6f 75 72 63 65 0a 20 20 2a   data source.  *
26a95 2a 20 69 73 20 61 20 53 45 4c 45 43 54 20 73 74  * is a SELECT st
26a96 61 74 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20  atement..  */.  
26a97 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
26a98 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4f 66  veLabel(v, endOf
26a99 4c 6f 6f 70 29 3b 0a 20 20 69 66 28 20 75 73 65  Loop);.  if( use
26a9a 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20  TempTable ){.   
26a9b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26a9c 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 73  p2(v, OP_Next, s
26a9d 72 63 54 61 62 2c 20 61 64 64 72 43 6f 6e 74 29  rcTab, addrCont)
26a9e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
26a9f 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
26aa0 72 49 6e 73 54 6f 70 29 3b 0a 20 20 20 20 73 71  rInsTop);.    sq
26aa1 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
26aa2 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 73 72 63  v, OP_Close, src
26aa3 54 61 62 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  Tab);.  }else if
26aa4 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ( pSelect ){.   
26aa5 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26aa6 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
26aa7 2c 20 61 64 64 72 43 6f 6e 74 29 3b 0a 20 20 20  , addrCont);.   
26aa8 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
26aa9 48 65 72 65 28 76 2c 20 61 64 64 72 49 6e 73 54  Here(v, addrInsT
26aaa 6f 70 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  op);.  }..  if( 
26aab 21 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29  !IsVirtual(pTab)
26aac 20 26 26 20 21 69 73 56 69 65 77 20 29 7b 0a 20   && !isView ){. 
26aad 20 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20     /* Close all 
26aae 74 61 62 6c 65 73 20 6f 70 65 6e 65 64 20 2a 2f  tables opened */
26aaf 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
26ab0 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp1(v, OP_Clo
26ab1 73 65 2c 20 62 61 73 65 43 75 72 29 3b 0a 20 20  se, baseCur);.  
26ab2 20 20 66 6f 72 28 69 64 78 3d 31 2c 20 70 49 64    for(idx=1, pId
26ab3 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
26ab4 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
26ab5 3e 70 4e 65 78 74 2c 20 69 64 78 2b 2b 29 7b 0a  >pNext, idx++){.
26ab6 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26ab7 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c  eAddOp1(v, OP_Cl
26ab8 6f 73 65 2c 20 69 64 78 2b 62 61 73 65 43 75 72  ose, idx+baseCur
26ab9 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 69 6e  );.    }.  }..in
26aba 73 65 72 74 5f 65 6e 64 3a 0a 20 20 2f 2a 20 55  sert_end:.  /* U
26abb 70 64 61 74 65 20 74 68 65 20 73 71 6c 69 74 65  pdate the sqlite
26abc 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20  _sequence table 
26abd 62 79 20 73 74 6f 72 69 6e 67 20 74 68 65 20 63  by storing the c
26abe 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 0a 20 20  ontent of the.  
26abf 2a 2a 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64  ** maximum rowid
26ac0 20 63 6f 75 6e 74 65 72 20 76 61 6c 75 65 73 20   counter values 
26ac1 72 65 63 6f 72 64 65 64 20 77 68 69 6c 65 20 69  recorded while i
26ac2 6e 73 65 72 74 69 6e 67 20 69 6e 74 6f 0a 20 20  nserting into.  
26ac3 2a 2a 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74  ** autoincrement
26ac4 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20   tables..  */.  
26ac5 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74  if( pParse->nest
26ac6 65 64 3d 3d 30 20 26 26 20 70 50 61 72 73 65 2d  ed==0 && pParse-
26ac7 3e 70 54 72 69 67 67 65 72 54 61 62 3d 3d 30 20  >pTriggerTab==0 
26ac8 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75  ){.    sqlite3Au
26ac9 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 70  toincrementEnd(p
26aca 50 61 72 73 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f  Parse);.  }..  /
26acb 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 74 68  *.  ** Return th
26acc 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
26acd 20 69 6e 73 65 72 74 65 64 2e 20 49 66 20 74 68   inserted. If th
26ace 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 0a 20  is routine is . 
26acf 20 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67 20 63   ** generating c
26ad0 6f 64 65 20 62 65 63 61 75 73 65 20 6f 66 20 61  ode because of a
26ad1 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
26ad2 4e 65 73 74 65 64 50 61 72 73 65 28 29 2c 20 64  NestedParse(), d
26ad3 6f 20 6e 6f 74 0a 20 20 2a 2a 20 69 6e 76 6f 6b  o not.  ** invok
26ad4 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66  e the callback f
26ad5 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20  unction..  */.  
26ad6 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 26 53  if( (db->flags&S
26ad7 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 29  QLITE_CountRows)
26ad8 20 26 26 20 21 70 50 61 72 73 65 2d 3e 6e 65 73   && !pParse->nes
26ad9 74 65 64 20 26 26 20 21 70 50 61 72 73 65 2d 3e  ted && !pParse->
26ada 70 54 72 69 67 67 65 72 54 61 62 20 29 7b 0a 20  pTriggerTab ){. 
26adb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26adc 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
26add 74 52 6f 77 2c 20 72 65 67 52 6f 77 43 6f 75 6e  tRow, regRowCoun
26ade 74 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  t, 1);.    sqlit
26adf 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
26ae0 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  (v, 1);.    sqli
26ae1 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
26ae2 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
26ae3 4e 41 4d 45 2c 20 22 72 6f 77 73 20 69 6e 73 65  NAME, "rows inse
26ae4 72 74 65 64 22 2c 20 53 51 4c 49 54 45 5f 53 54  rted", SQLITE_ST
26ae5 41 54 49 43 29 3b 0a 20 20 7d 0a 0a 69 6e 73 65  ATIC);.  }..inse
26ae6 72 74 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71  rt_cleanup:.  sq
26ae7 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65  lite3SrcListDele
26ae8 74 65 28 64 62 2c 20 70 54 61 62 4c 69 73 74 29  te(db, pTabList)
26ae9 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  ;.  sqlite3ExprL
26aea 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4c  istDelete(db, pL
26aeb 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  ist);.  sqlite3S
26aec 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
26aed 70 53 65 6c 65 63 74 29 3b 0a 20 20 73 71 6c 69  pSelect);.  sqli
26aee 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28  te3IdListDelete(
26aef 64 62 2c 20 70 43 6f 6c 75 6d 6e 29 3b 0a 20 20  db, pColumn);.  
26af0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
26af1 2c 20 61 52 65 67 49 64 78 29 3b 0a 7d 0a 0a 2f  , aRegIdx);.}../
26af2 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
26af3 64 65 20 74 6f 20 64 6f 20 63 6f 6e 73 74 72 61  de to do constra
26af4 69 6e 74 20 63 68 65 63 6b 73 20 70 72 69 6f 72  int checks prior
26af5 20 74 6f 20 61 6e 20 49 4e 53 45 52 54 20 6f 72   to an INSERT or
26af6 20 61 6e 20 55 50 44 41 54 45 2e 0a 2a 2a 0a 2a   an UPDATE..**.*
26af7 2a 20 54 68 65 20 69 6e 70 75 74 20 69 73 20 61  * The input is a
26af8 20 72 61 6e 67 65 20 6f 66 20 63 6f 6e 73 65 63   range of consec
26af9 75 74 69 76 65 20 72 65 67 69 73 74 65 72 73 20  utive registers 
26afa 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
26afb 2a 20 20 20 20 31 2e 20 20 54 68 65 20 72 6f 77  *    1.  The row
26afc 69 64 20 6f 66 20 74 68 65 20 72 6f 77 20 61 66  id of the row af
26afd 74 65 72 20 74 68 65 20 75 70 64 61 74 65 2e 0a  ter the update..
26afe 2a 2a 0a 2a 2a 20 20 20 20 32 2e 20 20 54 68 65  **.**    2.  The
26aff 20 64 61 74 61 20 69 6e 20 74 68 65 20 66 69 72   data in the fir
26b00 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  st column of the
26b01 20 65 6e 74 72 79 20 61 66 74 65 72 20 74 68 65   entry after the
26b02 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20   update..**.**  
26b03 20 20 69 2e 20 20 44 61 74 61 20 66 72 6f 6d 20    i.  Data from 
26b04 6d 69 64 64 6c 65 20 63 6f 6c 75 6d 6e 73 2e 2e  middle columns..
26b05 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 4e 2e 20 20 54  ..**.**    N.  T
26b06 68 65 20 64 61 74 61 20 69 6e 20 74 68 65 20 6c  he data in the l
26b07 61 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  ast column of th
26b08 65 20 65 6e 74 72 79 20 61 66 74 65 72 20 74 68  e entry after th
26b09 65 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20  e update..**.** 
26b0a 54 68 65 20 72 65 67 52 6f 77 69 64 20 70 61 72  The regRowid par
26b0b 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 69 6e  ameter is the in
26b0c 64 65 78 20 6f 66 20 74 68 65 20 72 65 67 69 73  dex of the regis
26b0d 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 28  ter containing (
26b0e 31 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 73 55  1)..**.** If isU
26b0f 70 64 61 74 65 20 69 73 20 74 72 75 65 20 61 6e  pdate is true an
26b10 64 20 72 6f 77 69 64 43 68 6e 67 20 69 73 20 6e  d rowidChng is n
26b11 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 72 6f  on-zero, then ro
26b12 77 69 64 43 68 6e 67 20 63 6f 6e 74 61 69 6e 73  widChng contains
26b13 0a 2a 2a 20 74 68 65 20 61 64 64 72 65 73 73 20  .** the address 
26b14 6f 66 20 61 20 72 65 67 69 73 74 65 72 20 63 6f  of a register co
26b15 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77  ntaining the row
26b16 69 64 20 62 65 66 6f 72 65 20 74 68 65 20 75 70  id before the up
26b17 64 61 74 65 20 74 61 6b 65 73 0a 2a 2a 20 70 6c  date takes.** pl
26b18 61 63 65 2e 20 69 73 55 70 64 61 74 65 20 69 73  ace. isUpdate is
26b19 20 74 72 75 65 20 66 6f 72 20 55 50 44 41 54 45   true for UPDATE
26b1a 73 20 61 6e 64 20 66 61 6c 73 65 20 66 6f 72 20  s and false for 
26b1b 49 4e 53 45 52 54 73 2e 20 49 66 20 69 73 55 70  INSERTs. If isUp
26b1c 64 61 74 65 0a 2a 2a 20 69 73 20 66 61 6c 73 65  date.** is false
26b1d 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 61 6e 20  , indicating an 
26b1e 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74  INSERT statement
26b1f 2c 20 74 68 65 6e 20 61 20 6e 6f 6e 2d 7a 65 72  , then a non-zer
26b20 6f 20 72 6f 77 69 64 43 68 6e 67 20 0a 2a 2a 20  o rowidChng .** 
26b21 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 74  indicates that t
26b22 68 65 20 72 6f 77 69 64 20 77 61 73 20 65 78 70  he rowid was exp
26b23 6c 69 63 69 74 6c 79 20 73 70 65 63 69 66 69 65  licitly specifie
26b24 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65  d as part of the
26b25 0a 2a 2a 20 49 4e 53 45 52 54 20 73 74 61 74 65  .** INSERT state
26b26 6d 65 6e 74 2e 20 49 66 20 72 6f 77 69 64 43 68  ment. If rowidCh
26b27 6e 67 20 69 73 20 66 61 6c 73 65 2c 20 69 74 20  ng is false, it 
26b28 6d 65 61 6e 73 20 74 68 61 74 20 20 74 68 65 20  means that  the 
26b29 72 6f 77 69 64 20 69 73 0a 2a 2a 20 63 6f 6d 70  rowid is.** comp
26b2a 75 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c  uted automatical
26b2b 6c 79 20 69 6e 20 61 6e 20 69 6e 73 65 72 74 20  ly in an insert 
26b2c 6f 72 20 74 68 61 74 20 74 68 65 20 72 6f 77 69  or that the rowi
26b2d 64 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 0a  d value is not .
26b2e 2a 2a 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61  ** modified by a
26b2f 6e 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20  n update..**.** 
26b30 54 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74  The code generat
26b31 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69  ed by this routi
26b32 6e 65 20 73 74 6f 72 65 20 6e 65 77 20 69 6e 64  ne store new ind
26b33 65 78 20 65 6e 74 72 69 65 73 20 69 6e 74 6f 0a  ex entries into.
26b34 2a 2a 20 72 65 67 69 73 74 65 72 73 20 69 64 65  ** registers ide
26b35 6e 74 69 66 69 65 64 20 62 79 20 61 52 65 67 49  ntified by aRegI
26b36 64 78 5b 5d 2e 20 20 4e 6f 20 69 6e 64 65 78 20  dx[].  No index 
26b37 65 6e 74 72 79 20 69 73 20 63 72 65 61 74 65 64  entry is created
26b38 20 66 6f 72 0a 2a 2a 20 69 6e 64 69 63 65 73 20   for.** indices 
26b39 77 68 65 72 65 20 61 52 65 67 49 64 78 5b 69 5d  where aRegIdx[i]
26b3a 3d 3d 30 2e 20 20 54 68 65 20 6f 72 64 65 72 20  ==0.  The order 
26b3b 6f 66 20 69 6e 64 69 63 65 73 20 69 6e 20 61 52  of indices in aR
26b3c 65 67 49 64 78 5b 5d 20 69 73 0a 2a 2a 20 74 68  egIdx[] is.** th
26b3d 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 6f 72  e same as the or
26b3e 64 65 72 20 6f 66 20 69 6e 64 69 63 65 73 20 6f  der of indices o
26b3f 6e 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73  n the linked lis
26b40 74 20 6f 66 20 69 6e 64 69 63 65 73 0a 2a 2a 20  t of indices.** 
26b41 61 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20  attached to the 
26b42 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  table..**.** Thi
26b43 73 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 67  s routine also g
26b44 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74 6f  enerates code to
26b45 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e   check constrain
26b46 74 73 2e 20 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 2a  ts.  NOT NULL,.*
26b47 2a 20 43 48 45 43 4b 2c 20 61 6e 64 20 55 4e 49  * CHECK, and UNI
26b48 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  QUE constraints 
26b49 61 72 65 20 61 6c 6c 20 63 68 65 63 6b 65 64 2e  are all checked.
26b4a 20 20 49 66 20 61 20 63 6f 6e 73 74 72 61 69 6e    If a constrain
26b4b 74 20 66 61 69 6c 73 2c 0a 2a 2a 20 74 68 65 6e  t fails,.** then
26b4c 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
26b4d 20 61 63 74 69 6f 6e 20 69 73 20 70 65 72 66 6f   action is perfo
26b4e 72 6d 65 64 2e 20 20 54 68 65 72 65 20 61 72 65  rmed.  There are
26b4f 20 66 69 76 65 20 70 6f 73 73 69 62 6c 65 0a 2a   five possible.*
26b50 2a 20 61 63 74 69 6f 6e 73 3a 20 52 4f 4c 4c 42  * actions: ROLLB
26b51 41 43 4b 2c 20 41 42 4f 52 54 2c 20 46 41 49 4c  ACK, ABORT, FAIL
26b52 2c 20 52 45 50 4c 41 43 45 2c 20 61 6e 64 20 49  , REPLACE, and I
26b53 47 4e 4f 52 45 2e 0a 2a 2a 0a 2a 2a 20 20 43 6f  GNORE..**.**  Co
26b54 6e 73 74 72 61 69 6e 74 20 74 79 70 65 20 20 41  nstraint type  A
26b55 63 74 69 6f 6e 20 20 20 20 20 20 20 57 68 61 74  ction       What
26b56 20 48 61 70 70 65 6e 73 0a 2a 2a 20 20 2d 2d 2d   Happens.**  ---
26b57 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d  ------------  --
26b58 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 2d 2d 2d 2d 2d  --------   -----
26b59 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26b5a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26b5b 2d 2d 2d 0a 2a 2a 20 20 61 6e 79 20 20 20 20 20  ---.**  any     
26b5c 20 20 20 20 20 20 20 20 20 52 4f 4c 4c 42 41 43           ROLLBAC
26b5d 4b 20 20 20 20 20 54 68 65 20 63 75 72 72 65 6e  K     The curren
26b5e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  t transaction is
26b5f 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e 64   rolled back and
26b60 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
26b61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26b62 20 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28     sqlite3_exec(
26b63 29 20 72 65 74 75 72 6e 73 20 69 6d 6d 65 64 69  ) returns immedi
26b64 61 74 65 6c 79 20 77 69 74 68 20 61 0a 2a 2a 20  ately with a.** 
26b65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26b66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
26b67 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 53 51  eturn code of SQ
26b68 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2e  LITE_CONSTRAINT.
26b69 0a 2a 2a 0a 2a 2a 20 20 61 6e 79 20 20 20 20 20  .**.**  any     
26b6a 20 20 20 20 20 20 20 20 20 41 42 4f 52 54 20 20           ABORT  
26b6b 20 20 20 20 20 20 42 61 63 6b 20 6f 75 74 20 63        Back out c
26b6c 68 61 6e 67 65 73 20 66 72 6f 6d 20 74 68 65 20  hanges from the 
26b6d 63 75 72 72 65 6e 74 20 63 6f 6d 6d 61 6e 64 0a  current command.
26b6e 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
26b6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26b70 20 20 6f 6e 6c 79 20 28 64 6f 20 6e 6f 74 20 64    only (do not d
26b71 6f 20 61 20 63 6f 6d 70 6c 65 74 65 20 72 6f 6c  o a complete rol
26b72 6c 62 61 63 6b 29 20 74 68 65 6e 0a 2a 2a 20 20  lback) then.**  
26b73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26b74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 61                ca
26b75 75 73 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63  use sqlite3_exec
26b76 28 29 20 74 6f 20 72 65 74 75 72 6e 20 69 6d 6d  () to return imm
26b77 65 64 69 61 74 65 6c 79 0a 2a 2a 20 20 20 20 20  ediately.**     
26b78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26b79 20 20 20 20 20 20 20 20 20 20 20 77 69 74 68 20             with 
26b7a 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
26b7b 54 2e 0a 2a 2a 0a 2a 2a 20 20 61 6e 79 20 20 20  T..**.**  any   
26b7c 20 20 20 20 20 20 20 20 20 20 20 46 41 49 4c 20             FAIL 
26b7d 20 20 20 20 20 20 20 20 53 71 6c 69 74 65 5f 65          Sqlite_e
26b7e 78 65 63 28 29 20 72 65 74 75 72 6e 73 20 69 6d  xec() returns im
26b7f 6d 65 64 69 61 74 65 6c 79 20 77 69 74 68 20 61  mediately with a
26b80 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
26b81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26b82 20 20 20 72 65 74 75 72 6e 20 63 6f 64 65 20 6f     return code o
26b83 66 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  f SQLITE_CONSTRA
26b84 49 4e 54 2e 20 20 54 68 65 0a 2a 2a 20 20 20 20  INT.  The.**    
26b85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26b86 20 20 20 20 20 20 20 20 20 20 20 20 74 72 61 6e              tran
26b87 73 61 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72  saction is not r
26b88 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e 64 20 61  olled back and a
26b89 6e 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ny.**           
26b8a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26b8b 20 20 20 20 20 70 72 69 6f 72 20 63 68 61 6e 67       prior chang
26b8c 65 73 20 61 72 65 20 72 65 74 61 69 6e 65 64 2e  es are retained.
26b8d 0a 2a 2a 0a 2a 2a 20 20 61 6e 79 20 20 20 20 20  .**.**  any     
26b8e 20 20 20 20 20 20 20 20 20 49 47 4e 4f 52 45 20           IGNORE 
26b8f 20 20 20 20 20 20 54 68 65 20 72 65 63 6f 72 64        The record
26b90 20 6e 75 6d 62 65 72 20 61 6e 64 20 64 61 74 61   number and data
26b91 20 69 73 20 70 6f 70 70 65 64 20 66 72 6f 6d 0a   is popped from.
26b92 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
26b93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26b94 20 20 74 68 65 20 73 74 61 63 6b 20 61 6e 64 20    the stack and 
26b95 74 68 65 72 65 20 69 73 20 61 6e 20 69 6d 6d 65  there is an imme
26b96 64 69 61 74 65 20 6a 75 6d 70 0a 2a 2a 20 20 20  diate jump.**   
26b97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26b98 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 20               to 
26b99 6c 61 62 65 6c 20 69 67 6e 6f 72 65 44 65 73 74  label ignoreDest
26b9a 2e 0a 2a 2a 0a 2a 2a 20 20 4e 4f 54 20 4e 55 4c  ..**.**  NOT NUL
26b9b 4c 20 20 20 20 20 20 20 20 20 52 45 50 4c 41 43  L         REPLAC
26b9c 45 20 20 20 20 20 20 54 68 65 20 4e 55 4c 4c 20  E      The NULL 
26b9d 76 61 6c 75 65 20 69 73 20 72 65 70 6c 61 63 65  value is replace
26b9e 20 62 79 20 74 68 65 20 64 65 66 61 75 6c 74 0a   by the default.
26b9f 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
26ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26ba1 20 20 76 61 6c 75 65 20 66 6f 72 20 74 68 61 74    value for that
26ba2 20 63 6f 6c 75 6d 6e 2e 20 20 49 66 20 74 68 65   column.  If the
26ba3 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 0a 2a   default value.*
26ba4 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
26ba5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26ba6 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 61 63   is NULL, the ac
26ba7 74 69 6f 6e 20 69 73 20 74 68 65 20 73 61 6d 65  tion is the same
26ba8 20 61 73 20 41 42 4f 52 54 2e 0a 2a 2a 0a 2a 2a   as ABORT..**.**
26ba9 20 20 55 4e 49 51 55 45 20 20 20 20 20 20 20 20    UNIQUE        
26baa 20 20 20 52 45 50 4c 41 43 45 20 20 20 20 20 20     REPLACE      
26bab 54 68 65 20 6f 74 68 65 72 20 72 6f 77 20 74 68  The other row th
26bac 61 74 20 63 6f 6e 66 6c 69 63 74 73 20 77 69 74  at conflicts wit
26bad 68 20 74 68 65 20 72 6f 77 0a 2a 2a 20 20 20 20  h the row.**    
26bae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26baf 20 20 20 20 20 20 20 20 20 20 20 20 62 65 69 6e              bein
26bb0 67 20 69 6e 73 65 72 74 65 64 20 69 73 20 72 65  g inserted is re
26bb1 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 43 48  moved..**.**  CH
26bb2 45 43 4b 20 20 20 20 20 20 20 20 20 20 20 20 52  ECK            R
26bb3 45 50 4c 41 43 45 20 20 20 20 20 20 49 6c 6c 65  EPLACE      Ille
26bb4 67 61 6c 2e 20 20 54 68 65 20 72 65 73 75 6c 74  gal.  The result
26bb5 73 20 69 6e 20 61 6e 20 65 78 63 65 70 74 69 6f  s in an exceptio
26bb6 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 69 63 68 20 61  n..**.** Which a
26bb7 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 20 69 73  ction to take is
26bb8 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74   determined by t
26bb9 68 65 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72  he overrideError
26bba 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 4f   parameter..** O
26bbb 72 20 69 66 20 6f 76 65 72 72 69 64 65 45 72 72  r if overrideErr
26bbc 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 2c 20  or==OE_Default, 
26bbd 74 68 65 6e 20 74 68 65 20 70 50 61 72 73 65 2d  then the pParse-
26bbe 3e 6f 6e 45 72 72 6f 72 20 70 61 72 61 6d 65 74  >onError paramet
26bbf 65 72 0a 2a 2a 20 69 73 20 75 73 65 64 2e 20 20  er.** is used.  
26bc0 4f 72 20 69 66 20 70 50 61 72 73 65 2d 3e 6f 6e  Or if pParse->on
26bc1 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c  Error==OE_Defaul
26bc2 74 20 74 68 65 6e 20 74 68 65 20 6f 6e 45 72 72  t then the onErr
26bc3 6f 72 20 76 61 6c 75 65 0a 2a 2a 20 66 6f 72 20  or value.** for 
26bc4 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 69  the constraint i
26bc5 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  s used..**.** Th
26bc6 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e  e calling routin
26bc7 65 20 6d 75 73 74 20 6f 70 65 6e 20 61 20 72 65  e must open a re
26bc8 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20  ad/write cursor 
26bc9 66 6f 72 20 70 54 61 62 20 77 69 74 68 0a 2a 2a  for pTab with.**
26bca 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 22   cursor number "
26bcb 62 61 73 65 43 75 72 22 2e 20 20 41 6c 6c 20 69  baseCur".  All i
26bcc 6e 64 69 63 65 73 20 6f 66 20 70 54 61 62 20 6d  ndices of pTab m
26bcd 75 73 74 20 61 6c 73 6f 20 68 61 76 65 20 6f 70  ust also have op
26bce 65 6e 0a 2a 2a 20 72 65 61 64 2f 77 72 69 74 65  en.** read/write
26bcf 20 63 75 72 73 6f 72 73 20 77 69 74 68 20 63 75   cursors with cu
26bd0 72 73 6f 72 20 6e 75 6d 62 65 72 20 62 61 73 65  rsor number base
26bd1 43 75 72 2b 69 20 66 6f 72 20 74 68 65 20 69 2d  Cur+i for the i-
26bd2 74 68 20 63 75 72 73 6f 72 2e 0a 2a 2a 20 45 78  th cursor..** Ex
26bd3 63 65 70 74 2c 20 69 66 20 74 68 65 72 65 20 69  cept, if there i
26bd4 73 20 6e 6f 20 70 6f 73 73 69 62 69 6c 69 74 79  s no possibility
26bd5 20 6f 66 20 61 20 52 45 50 4c 41 43 45 20 61 63   of a REPLACE ac
26bd6 74 69 6f 6e 20 74 68 65 6e 0a 2a 2a 20 63 75 72  tion then.** cur
26bd7 73 6f 72 73 20 64 6f 20 6e 6f 74 20 6e 65 65 64  sors do not need
26bd8 20 74 6f 20 62 65 20 6f 70 65 6e 20 66 6f 72 20   to be open for 
26bd9 69 6e 64 69 63 65 73 20 77 68 65 72 65 20 61 52  indices where aR
26bda 65 67 49 64 78 5b 69 5d 3d 3d 30 2e 0a 2a 2f 0a  egIdx[i]==0..*/.
26bdb 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
26bdc 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72  oid sqlite3Gener
26bdd 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65  ateConstraintChe
26bde 63 6b 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50  cks(.  Parse *pP
26bdf 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 54 68  arse,      /* Th
26be0 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
26be1 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
26be2 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65  b,        /* the
26be3 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63   table into whic
26be4 68 20 77 65 20 61 72 65 20 69 6e 73 65 72 74 69  h we are inserti
26be5 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65  ng */.  int base
26be6 43 75 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 49  Cur,        /* I
26be7 6e 64 65 78 20 6f 66 20 61 20 72 65 61 64 2f 77  ndex of a read/w
26be8 72 69 74 65 20 63 75 72 73 6f 72 20 70 6f 69 6e  rite cursor poin
26be9 74 69 6e 67 20 61 74 20 70 54 61 62 20 2a 2f 0a  ting at pTab */.
26bea 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 2c 20    int regRowid, 
26beb 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
26bec 66 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20 69  f the range of i
26bed 6e 70 75 74 20 72 65 67 69 73 74 65 72 73 20 2a  nput registers *
26bee 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67 49 64 78  /.  int *aRegIdx
26bef 2c 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73  ,       /* Regis
26bf0 74 65 72 20 75 73 65 64 20 62 79 20 65 61 63 68  ter used by each
26bf1 20 69 6e 64 65 78 2e 20 20 30 20 66 6f 72 20 75   index.  0 for u
26bf2 6e 75 73 65 64 20 69 6e 64 69 63 65 73 20 2a 2f  nused indices */
26bf3 0a 20 20 69 6e 74 20 72 6f 77 69 64 43 68 6e 67  .  int rowidChng
26bf4 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  ,      /* True i
26bf5 66 20 74 68 65 20 72 6f 77 69 64 20 6d 69 67 68  f the rowid migh
26bf6 74 20 63 6f 6c 6c 69 64 65 20 77 69 74 68 20 65  t collide with e
26bf7 78 69 73 74 69 6e 67 20 65 6e 74 72 79 20 2a 2f  xisting entry */
26bf8 0a 20 20 69 6e 74 20 69 73 55 70 64 61 74 65 2c  .  int isUpdate,
26bf9 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66         /* True f
26bfa 6f 72 20 55 50 44 41 54 45 2c 20 46 61 6c 73 65  or UPDATE, False
26bfb 20 66 6f 72 20 49 4e 53 45 52 54 20 2a 2f 0a 20   for INSERT */. 
26bfc 20 69 6e 74 20 6f 76 65 72 72 69 64 65 45 72 72   int overrideErr
26bfd 6f 72 2c 20 20 2f 2a 20 4f 76 65 72 72 69 64 65  or,  /* Override
26bfe 20 6f 6e 45 72 72 6f 72 20 74 6f 20 74 68 69 73   onError to this
26bff 20 69 66 20 6e 6f 74 20 4f 45 5f 44 65 66 61 75   if not OE_Defau
26c00 6c 74 20 2a 2f 0a 20 20 69 6e 74 20 69 67 6e 6f  lt */.  int igno
26c01 72 65 44 65 73 74 2c 20 20 20 20 20 2f 2a 20 4a  reDest,     /* J
26c02 75 6d 70 20 74 6f 20 74 68 69 73 20 6c 61 62 65  ump to this labe
26c03 6c 20 6f 6e 20 61 6e 20 4f 45 5f 49 67 6e 6f 72  l on an OE_Ignor
26c04 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 2a 2f 0a  e resolution */.
26c05 20 20 69 6e 74 20 2a 70 62 4d 61 79 52 65 70 6c    int *pbMayRepl
26c06 61 63 65 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65  ace   /* OUT: Se
26c07 74 20 74 6f 20 74 72 75 65 20 69 66 20 63 6f 6e  t to true if con
26c08 73 74 72 61 69 6e 74 20 6d 61 79 20 63 61 75 73  straint may caus
26c09 65 20 61 20 72 65 70 6c 61 63 65 20 2a 2f 0a 29  e a replace */.)
26c0a 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  {.  int i;      
26c0b 20 20 20 20 20 20 20 20 2f 2a 20 6c 6f 6f 70 20          /* loop 
26c0c 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 56 64 62  counter */.  Vdb
26c0d 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20  e *v;           
26c0e 20 2f 2a 20 56 44 42 45 20 75 6e 64 65 72 20 63   /* VDBE under c
26c0f 6f 6e 73 74 72 75 74 69 6f 6e 20 2a 2f 0a 20 20  onstrution */.  
26c10 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20  int nCol;       
26c11 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
26c12 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e   columns */.  in
26c13 74 20 6f 6e 45 72 72 6f 72 3b 20 20 20 20 20 20  t onError;      
26c14 20 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 20 72 65    /* Conflict re
26c15 73 6f 6c 75 74 69 6f 6e 20 73 74 72 61 74 65 67  solution strateg
26c16 79 20 2a 2f 0a 20 20 69 6e 74 20 6a 31 3b 20 20  y */.  int j1;  
26c17 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
26c18 64 72 65 73 73 73 20 6f 66 20 6a 75 6d 70 20 69  dresss of jump i
26c19 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20  nstruction */.  
26c1a 69 6e 74 20 6a 32 20 3d 20 30 2c 20 6a 33 3b 20  int j2 = 0, j3; 
26c1b 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 65 73      /* Addresses
26c1c 20 6f 66 20 6a 75 6d 70 20 69 6e 73 74 72 75 63   of jump instruc
26c1d 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 72  tions */.  int r
26c1e 65 67 44 61 74 61 3b 20 20 20 20 20 20 20 20 2f  egData;        /
26c1f 2a 20 52 65 67 69 73 74 65 72 20 63 6f 6e 74 61  * Register conta
26c20 69 6e 69 6e 67 20 66 69 72 73 74 20 64 61 74 61  ining first data
26c21 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74   column */.  int
26c22 20 69 43 75 72 3b 20 20 20 20 20 20 20 20 20 20   iCur;          
26c23 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 73 6f 72   /* Table cursor
26c24 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 49 6e 64   number */.  Ind
26c25 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20  ex *pIdx;       
26c26 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
26c27 6f 6e 65 20 6f 66 20 74 68 65 20 69 6e 64 69 63  one of the indic
26c28 65 73 20 2a 2f 0a 20 20 69 6e 74 20 73 65 65 6e  es */.  int seen
26c29 52 65 70 6c 61 63 65 20 3d 20 30 3b 20 2f 2a 20  Replace = 0; /* 
26c2a 54 72 75 65 20 69 66 20 52 45 50 4c 41 43 45 20  True if REPLACE 
26c2b 69 73 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c  is used to resol
26c2c 76 65 20 49 4e 54 20 50 4b 20 63 6f 6e 66 6c 69  ve INT PK confli
26c2d 63 74 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4f  ct */.  int regO
26c2e 6c 64 52 6f 77 69 64 20 3d 20 28 72 6f 77 69 64  ldRowid = (rowid
26c2f 43 68 6e 67 20 26 26 20 69 73 55 70 64 61 74 65  Chng && isUpdate
26c30 29 20 3f 20 72 6f 77 69 64 43 68 6e 67 20 3a 20  ) ? rowidChng : 
26c31 72 65 67 52 6f 77 69 64 3b 0a 0a 20 20 76 20 3d  regRowid;..  v =
26c32 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
26c33 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73 65 72  pParse);.  asser
26c34 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 61 73 73  t( v!=0 );.  ass
26c35 65 72 74 28 20 70 54 61 62 2d 3e 70 53 65 6c 65  ert( pTab->pSele
26c36 63 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 54 68 69  ct==0 );  /* Thi
26c37 73 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 61  s table is not a
26c38 20 56 49 45 57 20 2a 2f 0a 20 20 6e 43 6f 6c 20   VIEW */.  nCol 
26c39 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20  = pTab->nCol;.  
26c3a 72 65 67 44 61 74 61 20 3d 20 72 65 67 52 6f 77  regData = regRow
26c3b 69 64 20 2b 20 31 3b 0a 0a 20 20 2f 2a 20 54 65  id + 1;..  /* Te
26c3c 73 74 20 61 6c 6c 20 4e 4f 54 20 4e 55 4c 4c 20  st all NOT NULL 
26c3d 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 20 20 2a  constraints..  *
26c3e 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  /.  for(i=0; i<n
26c3f 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  Col; i++){.    i
26c40 66 28 20 69 3d 3d 70 54 61 62 2d 3e 69 50 4b 65  f( i==pTab->iPKe
26c41 79 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74 69  y ){.      conti
26c42 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6f  nue;.    }.    o
26c43 6e 45 72 72 6f 72 20 3d 20 70 54 61 62 2d 3e 61  nError = pTab->a
26c44 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c 3b 0a  Col[i].notNull;.
26c45 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d      if( onError=
26c46 3d 4f 45 5f 4e 6f 6e 65 20 29 20 63 6f 6e 74 69  =OE_None ) conti
26c47 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 6f 76 65  nue;.    if( ove
26c48 72 72 69 64 65 45 72 72 6f 72 21 3d 4f 45 5f 44  rrideError!=OE_D
26c49 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20  efault ){.      
26c4a 6f 6e 45 72 72 6f 72 20 3d 20 6f 76 65 72 72 69  onError = overri
26c4b 64 65 45 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c  deError;.    }el
26c4c 73 65 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d  se if( onError==
26c4d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20  OE_Default ){.  
26c4e 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45      onError = OE
26c4f 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20  _Abort;.    }.  
26c50 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f    if( onError==O
26c51 45 5f 52 65 70 6c 61 63 65 20 26 26 20 70 54 61  E_Replace && pTa
26c52 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74  b->aCol[i].pDflt
26c53 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45  ==0 ){.      onE
26c54 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b  rror = OE_Abort;
26c55 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
26c56 74 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52  t( onError==OE_R
26c57 6f 6c 6c 62 61 63 6b 20 7c 7c 20 6f 6e 45 72 72  ollback || onErr
26c58 6f 72 3d 3d 4f 45 5f 41 62 6f 72 74 20 7c 7c 20  or==OE_Abort || 
26c59 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 46 61 69 6c  onError==OE_Fail
26c5a 0a 20 20 20 20 20 20 20 20 7c 7c 20 6f 6e 45 72  .        || onEr
26c5b 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 7c  ror==OE_Ignore |
26c5c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65  | onError==OE_Re
26c5d 70 6c 61 63 65 20 29 3b 0a 20 20 20 20 73 77 69  place );.    swi
26c5e 74 63 68 28 20 6f 6e 45 72 72 6f 72 20 29 7b 0a  tch( onError ){.
26c5f 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 41 62        case OE_Ab
26c60 6f 72 74 3a 0a 20 20 20 20 20 20 20 20 73 71 6c  ort:.        sql
26c61 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61  ite3MayAbort(pPa
26c62 72 73 65 29 3b 0a 20 20 20 20 20 20 63 61 73 65  rse);.      case
26c63 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3a 0a 20 20   OE_Rollback:.  
26c64 20 20 20 20 63 61 73 65 20 4f 45 5f 46 61 69 6c      case OE_Fail
26c65 3a 20 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72  : {.        char
26c66 20 2a 7a 4d 73 67 3b 0a 20 20 20 20 20 20 20 20   *zMsg;.        
26c67 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j1 = sqlite3Vdbe
26c68 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 48 61 6c  AddOp3(v, OP_Hal
26c69 74 49 66 4e 75 6c 6c 2c 0a 20 20 20 20 20 20 20  tIfNull,.       
26c6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26c6b 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
26c6c 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 6f 6e  E_CONSTRAINT, on
26c6d 45 72 72 6f 72 2c 20 72 65 67 44 61 74 61 2b 69  Error, regData+i
26c6e 29 3b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20  );.        zMsg 
26c6f 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
26c70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 22 25 73  (pParse->db, "%s
26c71 2e 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 4e  .%s may not be N
26c72 55 4c 4c 22 2c 0a 20 20 20 20 20 20 20 20 20 20  ULL",.          
26c73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26c74 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c      pTab->zName,
26c75 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a   pTab->aCol[i].z
26c76 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 73  Name);.        s
26c77 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
26c78 50 34 28 76 2c 20 2d 31 2c 20 7a 4d 73 67 2c 20  P4(v, -1, zMsg, 
26c79 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20  P4_DYNAMIC);.   
26c7a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
26c7b 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 4f    }.      case O
26c7c 45 5f 49 67 6e 6f 72 65 3a 20 7b 0a 20 20 20 20  E_Ignore: {.    
26c7d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26c7e 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75  ddOp2(v, OP_IsNu
26c7f 6c 6c 2c 20 72 65 67 44 61 74 61 2b 69 2c 20 69  ll, regData+i, i
26c80 67 6e 6f 72 65 44 65 73 74 29 3b 0a 20 20 20 20  gnoreDest);.    
26c81 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
26c82 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74   }.      default
26c83 3a 20 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65  : {.        asse
26c84 72 74 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  rt( onError==OE_
26c85 52 65 70 6c 61 63 65 20 29 3b 0a 20 20 20 20 20  Replace );.     
26c86 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56     j1 = sqlite3V
26c87 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
26c88 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 44 61 74 61  NotNull, regData
26c89 2b 69 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  +i);.        sql
26c8a 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
26c8b 72 73 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  rse, pTab->aCol[
26c8c 69 5d 2e 70 44 66 6c 74 2c 20 72 65 67 44 61 74  i].pDflt, regDat
26c8d 61 2b 69 29 3b 0a 20 20 20 20 20 20 20 20 73 71  a+i);.        sq
26c8e 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
26c8f 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 20 20  e(v, j1);.      
26c90 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
26c91 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
26c92 20 54 65 73 74 20 61 6c 6c 20 43 48 45 43 4b 20   Test all CHECK 
26c93 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a 2f  constraints.  */
26c94 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
26c95 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 69 66 28  OMIT_CHECK.  if(
26c96 20 70 54 61 62 2d 3e 70 43 68 65 63 6b 20 26 26   pTab->pCheck &&
26c97 20 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c   (pParse->db->fl
26c98 61 67 73 20 26 20 53 51 4c 49 54 45 5f 49 67 6e  ags & SQLITE_Ign
26c99 6f 72 65 43 68 65 63 6b 73 29 3d 3d 30 20 29 7b  oreChecks)==0 ){
26c9a 0a 20 20 20 20 69 6e 74 20 61 6c 6c 4f 6b 20 3d  .    int allOk =
26c9b 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
26c9c 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 70 50  Label(v);.    pP
26c9d 61 72 73 65 2d 3e 63 6b 42 61 73 65 20 3d 20 72  arse->ckBase = r
26c9e 65 67 44 61 74 61 3b 0a 20 20 20 20 73 71 6c 69  egData;.    sqli
26c9f 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70 50  te3ExprIfTrue(pP
26ca0 61 72 73 65 2c 20 70 54 61 62 2d 3e 70 43 68 65  arse, pTab->pChe
26ca1 63 6b 2c 20 61 6c 6c 4f 6b 2c 20 53 51 4c 49 54  ck, allOk, SQLIT
26ca2 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20  E_JUMPIFNULL);. 
26ca3 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 6f 76 65     onError = ove
26ca4 72 72 69 64 65 45 72 72 6f 72 21 3d 4f 45 5f 44  rrideError!=OE_D
26ca5 65 66 61 75 6c 74 20 3f 20 6f 76 65 72 72 69 64  efault ? overrid
26ca6 65 45 72 72 6f 72 20 3a 20 4f 45 5f 41 62 6f 72  eError : OE_Abor
26ca7 74 3b 0a 20 20 20 20 69 66 28 20 6f 6e 45 72 72  t;.    if( onErr
26ca8 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 29 7b  or==OE_Ignore ){
26ca9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
26caa 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
26cab 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f 72 65 44 65  oto, 0, ignoreDe
26cac 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  st);.    }else{.
26cad 20 20 20 20 20 20 73 71 6c 69 74 65 33 48 61 6c        sqlite3Hal
26cae 74 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 72  tConstraint(pPar
26caf 73 65 2c 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 20  se, onError, 0, 
26cb0 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  0);.    }.    sq
26cb1 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
26cb2 4c 61 62 65 6c 28 76 2c 20 61 6c 6c 4f 6b 29 3b  Label(v, allOk);
26cb3 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21  .  }.#endif /* !
26cb4 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
26cb5 4d 49 54 5f 43 48 45 43 4b 29 20 2a 2f 0a 0a 20  MIT_CHECK) */.. 
26cb6 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 61   /* If we have a
26cb7 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  n INTEGER PRIMAR
26cb8 59 20 4b 45 59 2c 20 6d 61 6b 65 20 73 75 72 65  Y KEY, make sure
26cb9 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79   the primary key
26cba 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77  .  ** of the new
26cbb 20 72 65 63 6f 72 64 20 64 6f 65 73 20 6e 6f 74   record does not
26cbc 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73   previously exis
26cbd 74 2e 20 20 45 78 63 65 70 74 2c 20 69 66 20 74  t.  Except, if t
26cbe 68 69 73 0a 20 20 2a 2a 20 69 73 20 61 6e 20 55  his.  ** is an U
26cbf 50 44 41 54 45 20 61 6e 64 20 74 68 65 20 70 72  PDATE and the pr
26cc0 69 6d 61 72 79 20 6b 65 79 20 69 73 20 6e 6f 74  imary key is not
26cc1 20 63 68 61 6e 67 69 6e 67 2c 20 74 68 61 74 20   changing, that 
26cc2 69 73 20 4f 4b 2e 0a 20 20 2a 2f 0a 20 20 69 66  is OK..  */.  if
26cc3 28 20 72 6f 77 69 64 43 68 6e 67 20 29 7b 0a 20  ( rowidChng ){. 
26cc4 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 70 54 61     onError = pTa
26cc5 62 2d 3e 6b 65 79 43 6f 6e 66 3b 0a 20 20 20 20  b->keyConf;.    
26cc6 69 66 28 20 6f 76 65 72 72 69 64 65 45 72 72 6f  if( overrideErro
26cc7 72 21 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b  r!=OE_Default ){
26cc8 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d  .      onError =
26cc9 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 3b 0a   overrideError;.
26cca 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 6e      }else if( on
26ccb 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c  Error==OE_Defaul
26ccc 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72  t ){.      onErr
26ccd 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20  or = OE_Abort;. 
26cce 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 20 69 66     }.    .    if
26ccf 28 20 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 52 65  ( onError!=OE_Re
26cd0 70 6c 61 63 65 20 7c 7c 20 70 54 61 62 2d 3e 70  place || pTab->p
26cd1 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 20 69  Index ){.      i
26cd2 66 28 20 69 73 55 70 64 61 74 65 20 29 7b 0a 20  f( isUpdate ){. 
26cd3 20 20 20 20 20 20 20 6a 32 20 3d 20 73 71 6c 69         j2 = sqli
26cd4 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
26cd5 20 4f 50 5f 45 71 2c 20 72 65 67 52 6f 77 69 64   OP_Eq, regRowid
26cd6 2c 20 30 2c 20 72 6f 77 69 64 43 68 6e 67 29 3b  , 0, rowidChng);
26cd7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6a  .      }.      j
26cd8 33 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  3 = sqlite3VdbeA
26cd9 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45  ddOp3(v, OP_NotE
26cda 78 69 73 74 73 2c 20 62 61 73 65 43 75 72 2c 20  xists, baseCur, 
26cdb 30 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  0, regRowid);.  
26cdc 20 20 20 20 73 77 69 74 63 68 28 20 6f 6e 45 72      switch( onEr
26cdd 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 20 20 64  ror ){.        d
26cde 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
26cdf 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45      onError = OE
26ce0 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 20 20  _Abort;.        
26ce1 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 69    /* Fall thru i
26ce2 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73  nto the next cas
26ce3 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20  e */.        }. 
26ce4 20 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52         case OE_R
26ce5 6f 6c 6c 62 61 63 6b 3a 0a 20 20 20 20 20 20 20  ollback:.       
26ce6 20 63 61 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a   case OE_Abort:.
26ce7 20 20 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f          case OE_
26ce8 46 61 69 6c 3a 20 7b 0a 20 20 20 20 20 20 20 20  Fail: {.        
26ce9 20 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e    sqlite3HaltCon
26cea 73 74 72 61 69 6e 74 28 0a 20 20 20 20 20 20 20  straint(.       
26ceb 20 20 20 20 20 70 50 61 72 73 65 2c 20 6f 6e 45       pParse, onE
26cec 72 72 6f 72 2c 20 22 50 52 49 4d 41 52 59 20 4b  rror, "PRIMARY K
26ced 45 59 20 6d 75 73 74 20 62 65 20 75 6e 69 71 75  EY must be uniqu
26cee 65 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a  e", P4_STATIC);.
26cef 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
26cf0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
26cf1 20 20 20 63 61 73 65 20 4f 45 5f 52 65 70 6c 61     case OE_Repla
26cf2 63 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20  ce: {.          
26cf3 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  /* If there are 
26cf4 44 45 4c 45 54 45 20 74 72 69 67 67 65 72 73 20  DELETE triggers 
26cf5 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 20 61 6e  on this table an
26cf6 64 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20  d the.          
26cf7 2a 2a 20 72 65 63 75 72 73 69 76 65 2d 74 72 69  ** recursive-tri
26cf8 67 67 65 72 73 20 66 6c 61 67 20 69 73 20 73 65  ggers flag is se
26cf9 74 2c 20 63 61 6c 6c 20 47 65 6e 65 72 61 74 65  t, call Generate
26cfa 52 6f 77 44 65 6c 65 74 65 28 29 20 74 6f 0a 20  RowDelete() to. 
26cfb 20 20 20 20 20 20 20 20 20 2a 2a 20 72 65 6d 6f           ** remo
26cfc 76 65 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 69  ve the conflicti
26cfd 6e 67 20 72 6f 77 20 66 72 6f 6d 20 74 68 65 20  ng row from the 
26cfe 74 68 65 20 74 61 62 6c 65 2e 20 54 68 69 73 20  the table. This 
26cff 77 69 6c 6c 20 66 69 72 65 0a 20 20 20 20 20 20  will fire.      
26d00 20 20 20 20 2a 2a 20 74 68 65 20 74 72 69 67 67      ** the trigg
26d01 65 72 73 20 61 6e 64 20 72 65 6d 6f 76 65 20 62  ers and remove b
26d02 6f 74 68 20 74 68 65 20 74 61 62 6c 65 20 61 6e  oth the table an
26d03 64 20 69 6e 64 65 78 20 62 2d 74 72 65 65 20 65  d index b-tree e
26d04 6e 74 72 69 65 73 2e 0a 20 20 20 20 20 20 20 20  ntries..        
26d05 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 2a    **.          *
26d06 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20  * Otherwise, if 
26d07 74 68 65 72 65 20 61 72 65 20 6e 6f 20 74 72 69  there are no tri
26d08 67 67 65 72 73 20 6f 72 20 74 68 65 20 72 65 63  ggers or the rec
26d09 75 72 73 69 76 65 2d 74 72 69 67 67 65 72 73 0a  ursive-triggers.
26d0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 6c 61            ** fla
26d0b 67 20 69 73 20 6e 6f 74 20 73 65 74 2c 20 63 61  g is not set, ca
26d0c 6c 6c 20 47 65 6e 65 72 61 74 65 52 6f 77 49 6e  ll GenerateRowIn
26d0d 64 65 78 44 65 6c 65 74 65 28 29 2e 20 54 68 69  dexDelete(). Thi
26d0e 73 20 72 65 6d 6f 76 65 73 0a 20 20 20 20 20 20  s removes.      
26d0f 20 20 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 78      ** the index
26d10 20 62 2d 74 72 65 65 20 65 6e 74 72 69 65 73 20   b-tree entries 
26d11 6f 6e 6c 79 2e 20 54 68 65 20 74 61 62 6c 65 20  only. The table 
26d12 62 2d 74 72 65 65 20 65 6e 74 72 79 20 77 69 6c  b-tree entry wil
26d13 6c 20 62 65 20 0a 20 20 20 20 20 20 20 20 20 20  l be .          
26d14 2a 2a 20 72 65 70 6c 61 63 65 64 20 62 79 20 74  ** replaced by t
26d15 68 65 20 6e 65 77 20 65 6e 74 72 79 20 77 68 65  he new entry whe
26d16 6e 20 69 74 20 69 73 20 69 6e 73 65 72 74 65 64  n it is inserted
26d17 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  .  */.          
26d18 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
26d19 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  r = 0;.         
26d1a 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d   if( pParse->db-
26d1b 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65  >flags&SQLITE_Re
26d1c 63 54 72 69 67 67 65 72 73 20 29 7b 0a 20 20 20  cTriggers ){.   
26d1d 20 20 20 20 20 20 20 20 20 70 54 72 69 67 67 65           pTrigge
26d1e 72 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67  r = sqlite3Trigg
26d1f 65 72 73 45 78 69 73 74 28 70 50 61 72 73 65 2c  ersExist(pParse,
26d20 20 70 54 61 62 2c 20 54 4b 5f 44 45 4c 45 54 45   pTab, TK_DELETE
26d21 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  , 0, 0);.       
26d22 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69     }.          i
26d23 66 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20  f( pTrigger ){. 
26d24 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
26d25 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c  e3GenerateRowDel
26d26 65 74 65 28 0a 20 20 20 20 20 20 20 20 20 20 20  ete(.           
26d27 20 20 20 20 20 70 50 61 72 73 65 2c 20 70 54 61       pParse, pTa
26d28 62 2c 20 62 61 73 65 43 75 72 2c 20 72 65 67 52  b, baseCur, regR
26d29 6f 77 69 64 2c 20 30 2c 20 70 54 72 69 67 67 65  owid, 0, pTrigge
26d2a 72 2c 20 4f 45 5f 52 65 70 6c 61 63 65 0a 20 20  r, OE_Replace.  
26d2b 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20            );.   
26d2c 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
26d2d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
26d2e 33 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65  3GenerateRowInde
26d2f 78 44 65 6c 65 74 65 28 70 50 61 72 73 65 2c 20  xDelete(pParse, 
26d30 70 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20 30  pTab, baseCur, 0
26d31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
26d32 20 20 20 20 20 20 20 20 20 73 65 65 6e 52 65 70           seenRep
26d33 6c 61 63 65 20 3d 20 31 3b 0a 20 20 20 20 20 20  lace = 1;.      
26d34 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
26d35 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73     }.        cas
26d36 65 20 4f 45 5f 49 67 6e 6f 72 65 3a 20 7b 0a 20  e OE_Ignore: {. 
26d37 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
26d38 20 73 65 65 6e 52 65 70 6c 61 63 65 3d 3d 30 20   seenReplace==0 
26d39 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
26d3a 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
26d3b 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 67  , OP_Goto, 0, ig
26d3c 6e 6f 72 65 44 65 73 74 29 3b 0a 20 20 20 20 20  noreDest);.     
26d3d 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
26d3e 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
26d3f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
26d40 75 6d 70 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a  umpHere(v, j3);.
26d41 20 20 20 20 20 20 69 66 28 20 69 73 55 70 64 61        if( isUpda
26d42 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  te ){.        sq
26d43 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
26d44 65 28 76 2c 20 6a 32 29 3b 0a 20 20 20 20 20 20  e(v, j2);.      
26d45 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  }.    }.  }..  /
26d46 2a 20 54 65 73 74 20 61 6c 6c 20 55 4e 49 51 55  * Test all UNIQU
26d47 45 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 62 79  E constraints by
26d48 20 63 72 65 61 74 69 6e 67 20 65 6e 74 72 69 65   creating entrie
26d49 73 20 66 6f 72 20 65 61 63 68 20 55 4e 49 51 55  s for each UNIQU
26d4a 45 0a 20 20 2a 2a 20 69 6e 64 65 78 20 61 6e 64  E.  ** index and
26d4b 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 74 68 61   making sure tha
26d4c 74 20 64 75 70 6c 69 63 61 74 65 20 65 6e 74 72  t duplicate entr
26d4d 69 65 73 20 64 6f 20 6e 6f 74 20 61 6c 72 65 61  ies do not alrea
26d4e 64 79 20 65 78 69 73 74 2e 0a 20 20 2a 2a 20 41  dy exist..  ** A
26d4f 64 64 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72  dd the new recor
26d50 64 73 20 74 6f 20 74 68 65 20 69 6e 64 69 63 65  ds to the indice
26d51 73 20 61 73 20 77 65 20 67 6f 2e 0a 20 20 2a 2f  s as we go..  */
26d52 0a 20 20 66 6f 72 28 69 43 75 72 3d 30 2c 20 70  .  for(iCur=0, p
26d53 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  Idx=pTab->pIndex
26d54 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64  ; pIdx; pIdx=pId
26d55 78 2d 3e 70 4e 65 78 74 2c 20 69 43 75 72 2b 2b  x->pNext, iCur++
26d56 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 67 49 64  ){.    int regId
26d57 78 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 52 3b  x;.    int regR;
26d58 0a 0a 20 20 20 20 69 66 28 20 61 52 65 67 49 64  ..    if( aRegId
26d59 78 5b 69 43 75 72 5d 3d 3d 30 20 29 20 63 6f 6e  x[iCur]==0 ) con
26d5a 74 69 6e 75 65 3b 20 20 2f 2a 20 53 6b 69 70 20  tinue;  /* Skip 
26d5b 75 6e 75 73 65 64 20 69 6e 64 69 63 65 73 20 2a  unused indices *
26d5c 2f 0a 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65  /..    /* Create
26d5d 20 61 20 6b 65 79 20 66 6f 72 20 61 63 63 65 73   a key for acces
26d5e 73 69 6e 67 20 74 68 65 20 69 6e 64 65 78 20 65  sing the index e
26d5f 6e 74 72 79 20 2a 2f 0a 20 20 20 20 72 65 67 49  ntry */.    regI
26d60 64 78 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  dx = sqlite3GetT
26d61 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
26d62 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31   pIdx->nColumn+1
26d63 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  );.    for(i=0; 
26d64 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  i<pIdx->nColumn;
26d65 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74   i++){.      int
26d66 20 69 64 78 20 3d 20 70 49 64 78 2d 3e 61 69 43   idx = pIdx->aiC
26d67 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20  olumn[i];.      
26d68 69 66 28 20 69 64 78 3d 3d 70 54 61 62 2d 3e 69  if( idx==pTab->i
26d69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20  PKey ){.        
26d6a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26d6b 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72  2(v, OP_SCopy, r
26d6c 65 67 52 6f 77 69 64 2c 20 72 65 67 49 64 78 2b  egRowid, regIdx+
26d6d 69 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  i);.      }else{
26d6e 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
26d6f 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
26d70 5f 53 43 6f 70 79 2c 20 72 65 67 44 61 74 61 2b  _SCopy, regData+
26d71 69 64 78 2c 20 72 65 67 49 64 78 2b 69 29 3b 0a  idx, regIdx+i);.
26d72 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
26d73 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26d74 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c  Op2(v, OP_SCopy,
26d75 20 72 65 67 52 6f 77 69 64 2c 20 72 65 67 49 64   regRowid, regId
26d76 78 2b 69 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  x+i);.    sqlite
26d77 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
26d78 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65  P_MakeRecord, re
26d79 67 49 64 78 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c  gIdx, pIdx->nCol
26d7a 75 6d 6e 2b 31 2c 20 61 52 65 67 49 64 78 5b 69  umn+1, aRegIdx[i
26d7b 43 75 72 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74  Cur]);.    sqlit
26d7c 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
26d7d 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 49 6e 64  , -1, sqlite3Ind
26d7e 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c  exAffinityStr(v,
26d7f 20 70 49 64 78 29 2c 20 30 29 3b 0a 20 20 20 20   pIdx), 0);.    
26d80 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
26d81 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70  AffinityChange(p
26d82 50 61 72 73 65 2c 20 72 65 67 49 64 78 2c 20 70  Parse, regIdx, p
26d83 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b  Idx->nColumn+1);
26d84 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 6f 75  ..    /* Find ou
26d85 74 20 77 68 61 74 20 61 63 74 69 6f 6e 20 74 6f  t what action to
26d86 20 74 61 6b 65 20 69 6e 20 63 61 73 65 20 74 68   take in case th
26d87 65 72 65 20 69 73 20 61 6e 20 69 6e 64 65 78 69  ere is an indexi
26d88 6e 67 20 63 6f 6e 66 6c 69 63 74 20 2a 2f 0a 20  ng conflict */. 
26d89 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 70 49 64     onError = pId
26d8a 78 2d 3e 6f 6e 45 72 72 6f 72 3b 0a 20 20 20 20  x->onError;.    
26d8b 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  if( onError==OE_
26d8c 4e 6f 6e 65 20 29 7b 20 0a 20 20 20 20 20 20 73  None ){ .      s
26d8d 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
26d8e 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  pRange(pParse, r
26d8f 65 67 49 64 78 2c 20 70 49 64 78 2d 3e 6e 43 6f  egIdx, pIdx->nCo
26d90 6c 75 6d 6e 2b 31 29 3b 0a 20 20 20 20 20 20 63  lumn+1);.      c
26d91 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 70 49 64  ontinue;  /* pId
26d92 78 20 69 73 20 6e 6f 74 20 61 20 55 4e 49 51 55  x is not a UNIQU
26d93 45 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 7d  E index */.    }
26d94 0a 20 20 20 20 69 66 28 20 6f 76 65 72 72 69 64  .    if( overrid
26d95 65 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75  eError!=OE_Defau
26d96 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72  lt ){.      onEr
26d97 72 6f 72 20 3d 20 6f 76 65 72 72 69 64 65 45 72  ror = overrideEr
26d98 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ror;.    }else i
26d99 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44  f( onError==OE_D
26d9a 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20  efault ){.      
26d9b 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f  onError = OE_Abo
26d9c 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  rt;.    }.    if
26d9d 28 20 73 65 65 6e 52 65 70 6c 61 63 65 20 29 7b  ( seenReplace ){
26d9e 0a 20 20 20 20 20 20 69 66 28 20 6f 6e 45 72 72  .      if( onErr
26d9f 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 29 20  or==OE_Ignore ) 
26da0 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 52 65 70  onError = OE_Rep
26da1 6c 61 63 65 3b 0a 20 20 20 20 20 20 65 6c 73 65  lace;.      else
26da2 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45   if( onError==OE
26da3 5f 46 61 69 6c 20 29 20 6f 6e 45 72 72 6f 72 20  _Fail ) onError 
26da4 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20  = OE_Abort;.    
26da5 7d 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20 43 68  }.    .    /* Ch
26da6 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68  eck to see if th
26da7 65 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72  e new index entr
26da8 79 20 77 69 6c 6c 20 62 65 20 75 6e 69 71 75 65  y will be unique
26da9 20 2a 2f 0a 20 20 20 20 72 65 67 52 20 3d 20 73   */.    regR = s
26daa 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
26dab 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 73 71  (pParse);.    sq
26dac 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
26dad 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67  v, OP_SCopy, reg
26dae 4f 6c 64 52 6f 77 69 64 2c 20 72 65 67 52 29 3b  OldRowid, regR);
26daf 0a 20 20 20 20 6a 33 20 3d 20 73 71 6c 69 74 65  .    j3 = sqlite
26db0 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
26db1 50 5f 49 73 55 6e 69 71 75 65 2c 20 62 61 73 65  P_IsUnique, base
26db2 43 75 72 2b 69 43 75 72 2b 31 2c 20 30 2c 0a 20  Cur+iCur+1, 0,. 
26db3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26db4 20 20 20 20 20 20 20 20 20 20 72 65 67 52 2c 20            regR, 
26db5 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54  SQLITE_INT_TO_PT
26db6 52 28 72 65 67 49 64 78 29 2c 0a 20 20 20 20 20  R(regIdx),.     
26db7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26db8 20 20 20 20 20 20 50 34 5f 49 4e 54 33 32 29 3b        P4_INT32);
26db9 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65  .    sqlite3Rele
26dba 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61  aseTempRange(pPa
26dbb 72 73 65 2c 20 72 65 67 49 64 78 2c 20 70 49 64  rse, regIdx, pId
26dbc 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 0a  x->nColumn+1);..
26dbd 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20      /* Generate 
26dbe 63 6f 64 65 20 74 68 61 74 20 65 78 65 63 75 74  code that execut
26dbf 65 73 20 69 66 20 74 68 65 20 6e 65 77 20 69 6e  es if the new in
26dc0 64 65 78 20 65 6e 74 72 79 20 69 73 20 6e 6f 74  dex entry is not
26dc1 20 75 6e 69 71 75 65 20 2a 2f 0a 20 20 20 20 61   unique */.    a
26dc2 73 73 65 72 74 28 20 6f 6e 45 72 72 6f 72 3d 3d  ssert( onError==
26dc3 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 7c 7c 20 6f  OE_Rollback || o
26dc4 6e 45 72 72 6f 72 3d 3d 4f 45 5f 41 62 6f 72 74  nError==OE_Abort
26dc5 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f   || onError==OE_
26dc6 46 61 69 6c 0a 20 20 20 20 20 20 20 20 7c 7c 20  Fail.        || 
26dc7 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f  onError==OE_Igno
26dc8 72 65 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f  re || onError==O
26dc9 45 5f 52 65 70 6c 61 63 65 20 29 3b 0a 20 20 20  E_Replace );.   
26dca 20 73 77 69 74 63 68 28 20 6f 6e 45 72 72 6f 72   switch( onError
26dcb 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f   ){.      case O
26dcc 45 5f 52 6f 6c 6c 62 61 63 6b 3a 0a 20 20 20 20  E_Rollback:.    
26dcd 20 20 63 61 73 65 20 4f 45 5f 41 62 6f 72 74 3a    case OE_Abort:
26dce 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 46  .      case OE_F
26dcf 61 69 6c 3a 20 7b 0a 20 20 20 20 20 20 20 20 69  ail: {.        i
26dd0 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 53 74  nt j;.        St
26dd1 72 41 63 63 75 6d 20 65 72 72 4d 73 67 3b 0a 20  rAccum errMsg;. 
26dd2 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
26dd3 72 20 2a 7a 53 65 70 3b 0a 20 20 20 20 20 20 20  r *zSep;.       
26dd4 20 63 68 61 72 20 2a 7a 45 72 72 3b 0a 0a 20 20   char *zErr;..  
26dd5 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72        sqlite3Str
26dd6 41 63 63 75 6d 49 6e 69 74 28 26 65 72 72 4d 73  AccumInit(&errMs
26dd7 67 2c 20 30 2c 20 30 2c 20 32 30 30 29 3b 0a 20  g, 0, 0, 200);. 
26dd8 20 20 20 20 20 20 20 65 72 72 4d 73 67 2e 64 62         errMsg.db
26dd9 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
26dda 20 20 20 20 20 20 20 7a 53 65 70 20 3d 20 70 49         zSep = pI
26ddb 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 31 20 3f 20  dx->nColumn>1 ? 
26ddc 22 63 6f 6c 75 6d 6e 73 20 22 20 3a 20 22 63 6f  "columns " : "co
26ddd 6c 75 6d 6e 20 22 3b 0a 20 20 20 20 20 20 20 20  lumn ";.        
26dde 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d  for(j=0; j<pIdx-
26ddf 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a  >nColumn; j++){.
26de0 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
26de1 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f  zCol = pTab->aCo
26de2 6c 5b 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  l[pIdx->aiColumn
26de3 5b 6a 5d 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  [j]].zName;.    
26de4 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72        sqlite3Str
26de5 41 63 63 75 6d 41 70 70 65 6e 64 28 26 65 72 72  AccumAppend(&err
26de6 4d 73 67 2c 20 7a 53 65 70 2c 20 2d 31 29 3b 0a  Msg, zSep, -1);.
26de7 20 20 20 20 20 20 20 20 20 20 7a 53 65 70 20 3d            zSep =
26de8 20 22 2c 20 22 3b 0a 20 20 20 20 20 20 20 20 20   ", ";.         
26de9 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
26dea 41 70 70 65 6e 64 28 26 65 72 72 4d 73 67 2c 20  Append(&errMsg, 
26deb 7a 43 6f 6c 2c 20 2d 31 29 3b 0a 20 20 20 20 20  zCol, -1);.     
26dec 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c     }.        sql
26ded 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65  ite3StrAccumAppe
26dee 6e 64 28 26 65 72 72 4d 73 67 2c 0a 20 20 20 20  nd(&errMsg,.    
26def 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e 6e 43          pIdx->nC
26df0 6f 6c 75 6d 6e 3e 31 20 3f 20 22 20 61 72 65 20  olumn>1 ? " are 
26df1 6e 6f 74 20 75 6e 69 71 75 65 22 20 3a 20 22 20  not unique" : " 
26df2 69 73 20 6e 6f 74 20 75 6e 69 71 75 65 22 2c 20  is not unique", 
26df3 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 7a 45 72  -1);.        zEr
26df4 72 20 3d 20 73 71 6c 69 74 65 33 53 74 72 41 63  r = sqlite3StrAc
26df5 63 75 6d 46 69 6e 69 73 68 28 26 65 72 72 4d 73  cumFinish(&errMs
26df6 67 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  g);.        sqli
26df7 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e  te3HaltConstrain
26df8 74 28 70 50 61 72 73 65 2c 20 6f 6e 45 72 72 6f  t(pParse, onErro
26df9 72 2c 20 7a 45 72 72 2c 20 30 29 3b 0a 20 20 20  r, zErr, 0);.   
26dfa 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
26dfb 65 65 28 65 72 72 4d 73 67 2e 64 62 2c 20 7a 45  ee(errMsg.db, zE
26dfc 72 72 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  rr);.        bre
26dfd 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
26dfe 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72 65    case OE_Ignore
26dff 3a 20 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65  : {.        asse
26e00 72 74 28 20 73 65 65 6e 52 65 70 6c 61 63 65 3d  rt( seenReplace=
26e01 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71  =0 );.        sq
26e02 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
26e03 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69  v, OP_Goto, 0, i
26e04 67 6e 6f 72 65 44 65 73 74 29 3b 0a 20 20 20 20  gnoreDest);.    
26e05 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
26e06 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74   }.      default
26e07 3a 20 7b 0a 20 20 20 20 20 20 20 20 54 72 69 67  : {.        Trig
26e08 67 65 72 20 2a 70 54 72 69 67 67 65 72 20 3d 20  ger *pTrigger = 
26e09 30 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  0;.        asser
26e0a 74 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52  t( onError==OE_R
26e0b 65 70 6c 61 63 65 20 29 3b 0a 20 20 20 20 20 20  eplace );.      
26e0c 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62    if( pParse->db
26e0d 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52  ->flags&SQLITE_R
26e0e 65 63 54 72 69 67 67 65 72 73 20 29 7b 0a 20 20  ecTriggers ){.  
26e0f 20 20 20 20 20 20 20 20 70 54 72 69 67 67 65 72          pTrigger
26e10 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65   = sqlite3Trigge
26e11 72 73 45 78 69 73 74 28 70 50 61 72 73 65 2c 20  rsExist(pParse, 
26e12 70 54 61 62 2c 20 54 4b 5f 44 45 4c 45 54 45 2c  pTab, TK_DELETE,
26e13 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   0, 0);.        
26e14 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
26e15 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c 65  3GenerateRowDele
26e16 74 65 28 0a 20 20 20 20 20 20 20 20 20 20 20 20  te(.            
26e17 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 62 61  pParse, pTab, ba
26e18 73 65 43 75 72 2c 20 72 65 67 52 2c 20 30 2c 20  seCur, regR, 0, 
26e19 70 54 72 69 67 67 65 72 2c 20 4f 45 5f 52 65 70  pTrigger, OE_Rep
26e1a 6c 61 63 65 0a 20 20 20 20 20 20 20 20 29 3b 0a  lace.        );.
26e1b 20 20 20 20 20 20 20 20 73 65 65 6e 52 65 70 6c          seenRepl
26e1c 61 63 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ace = 1;.       
26e1d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
26e1e 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
26e1f 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
26e20 20 6a 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   j3);.    sqlite
26e21 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
26e22 70 50 61 72 73 65 2c 20 72 65 67 52 29 3b 0a 20  pParse, regR);. 
26e23 20 7d 0a 0a 20 20 69 66 28 20 70 62 4d 61 79 52   }..  if( pbMayR
26e24 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 2a 70  eplace ){.    *p
26e25 62 4d 61 79 52 65 70 6c 61 63 65 20 3d 20 73 65  bMayReplace = se
26e26 65 6e 52 65 70 6c 61 63 65 3b 0a 20 20 7d 0a 7d  enReplace;.  }.}
26e27 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
26e28 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63  tine generates c
26e29 6f 64 65 20 74 6f 20 66 69 6e 69 73 68 20 74 68  ode to finish th
26e2a 65 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41  e INSERT or UPDA
26e2b 54 45 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20  TE operation.** 
26e2c 74 68 61 74 20 77 61 73 20 73 74 61 72 74 65 64  that was started
26e2d 20 62 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c   by a prior call
26e2e 20 74 6f 20 73 71 6c 69 74 65 33 47 65 6e 65 72   to sqlite3Gener
26e2f 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65  ateConstraintChe
26e30 63 6b 73 2e 0a 2a 2a 20 41 20 63 6f 6e 73 65 63  cks..** A consec
26e31 75 74 69 76 65 20 72 61 6e 67 65 20 6f 66 20 72  utive range of r
26e32 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e  egisters startin
26e33 67 20 61 74 20 72 65 67 52 6f 77 69 64 20 63 6f  g at regRowid co
26e34 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 72 6f  ntains the.** ro
26e35 77 69 64 20 61 6e 64 20 74 68 65 20 63 6f 6e 74  wid and the cont
26e36 65 6e 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74  ent to be insert
26e37 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 72  ed..**.** The ar
26e38 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20  guments to this 
26e39 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 62  routine should b
26e3a 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68  e the same as th
26e3b 65 20 66 69 72 73 74 20 73 69 78 0a 2a 2a 20 61  e first six.** a
26e3c 72 67 75 6d 65 6e 74 73 20 74 6f 20 73 71 6c 69  rguments to sqli
26e3d 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74  te3GenerateConst
26e3e 72 61 69 6e 74 43 68 65 63 6b 73 2e 0a 2a 2f 0a  raintChecks..*/.
26e3f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
26e40 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c  oid sqlite3Compl
26e41 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 0a 20 20  eteInsertion(.  
26e42 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
26e43 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
26e44 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  r context */.  T
26e45 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20  able *pTab,     
26e46 20 20 20 2f 2a 20 74 68 65 20 74 61 62 6c 65 20     /* the table 
26e47 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20 61 72  into which we ar
26e48 65 20 69 6e 73 65 72 74 69 6e 67 20 2a 2f 0a 20  e inserting */. 
26e49 20 69 6e 74 20 62 61 73 65 43 75 72 2c 20 20 20   int baseCur,   
26e4a 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
26e4b 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 63 75   a read/write cu
26e4c 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 74  rsor pointing at
26e4d 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 72   pTab */.  int r
26e4e 65 67 52 6f 77 69 64 2c 20 20 20 20 20 20 20 2f  egRowid,       /
26e4f 2a 20 52 61 6e 67 65 20 6f 66 20 63 6f 6e 74 65  * Range of conte
26e50 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65  nt */.  int *aRe
26e51 67 49 64 78 2c 20 20 20 20 20 20 20 2f 2a 20 52  gIdx,       /* R
26e52 65 67 69 73 74 65 72 20 75 73 65 64 20 62 79 20  egister used by 
26e53 65 61 63 68 20 69 6e 64 65 78 2e 20 20 30 20 66  each index.  0 f
26e54 6f 72 20 75 6e 75 73 65 64 20 69 6e 64 69 63 65  or unused indice
26e55 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73 55 70 64  s */.  int isUpd
26e56 61 74 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 72  ate,       /* Tr
26e57 75 65 20 66 6f 72 20 55 50 44 41 54 45 2c 20 46  ue for UPDATE, F
26e58 61 6c 73 65 20 66 6f 72 20 49 4e 53 45 52 54 20  alse for INSERT 
26e59 2a 2f 0a 20 20 69 6e 74 20 61 70 70 65 6e 64 42  */.  int appendB
26e5a 69 61 73 2c 20 20 20 20 20 2f 2a 20 54 72 75 65  ias,     /* True
26e5b 20 69 66 20 74 68 69 73 20 69 73 20 6c 69 6b 65   if this is like
26e5c 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65  ly to be an appe
26e5d 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 75 73 65 53  nd */.  int useS
26e5e 65 65 6b 52 65 73 75 6c 74 20 20 20 2f 2a 20 54  eekResult   /* T
26e5f 72 75 65 20 74 6f 20 73 65 74 20 74 68 65 20 55  rue to set the U
26e60 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66 6c 61  SESEEKRESULT fla
26e61 67 20 6f 6e 20 4f 50 5f 5b 49 64 78 5d 49 6e 73  g on OP_[Idx]Ins
26e62 65 72 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ert */.){.  int 
26e63 69 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  i;.  Vdbe *v;.  
26e64 69 6e 74 20 6e 49 64 78 3b 0a 20 20 49 6e 64 65  int nIdx;.  Inde
26e65 78 20 2a 70 49 64 78 3b 0a 20 20 75 38 20 70 69  x *pIdx;.  u8 pi
26e66 6b 5f 66 6c 61 67 73 3b 0a 20 20 69 6e 74 20 72  k_flags;.  int r
26e67 65 67 44 61 74 61 3b 0a 20 20 69 6e 74 20 72 65  egData;.  int re
26e68 67 52 65 63 3b 0a 0a 20 20 76 20 3d 20 73 71 6c  gRec;..  v = sql
26e69 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
26e6a 73 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76  se);.  assert( v
26e6b 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
26e6c 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d   pTab->pSelect==
26e6d 30 20 29 3b 20 20 2f 2a 20 54 68 69 73 20 74 61  0 );  /* This ta
26e6e 62 6c 65 20 69 73 20 6e 6f 74 20 61 20 56 49 45  ble is not a VIE
26e6f 57 20 2a 2f 0a 20 20 66 6f 72 28 6e 49 64 78 3d  W */.  for(nIdx=
26e70 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  0, pIdx=pTab->pI
26e71 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
26e72 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6e 49  =pIdx->pNext, nI
26e73 64 78 2b 2b 29 7b 7d 0a 20 20 66 6f 72 28 69 3d  dx++){}.  for(i=
26e74 6e 49 64 78 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d  nIdx-1; i>=0; i-
26e75 2d 29 7b 0a 20 20 20 20 69 66 28 20 61 52 65 67  -){.    if( aReg
26e76 49 64 78 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74  Idx[i]==0 ) cont
26e77 69 6e 75 65 3b 0a 20 20 20 20 73 71 6c 69 74 65  inue;.    sqlite
26e78 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26e79 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 62 61 73  P_IdxInsert, bas
26e7a 65 43 75 72 2b 69 2b 31 2c 20 61 52 65 67 49 64  eCur+i+1, aRegId
26e7b 78 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 75  x[i]);.    if( u
26e7c 73 65 53 65 65 6b 52 65 73 75 6c 74 20 29 7b 0a  seSeekResult ){.
26e7d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26e7e 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46  eChangeP5(v, OPF
26e7f 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c  LAG_USESEEKRESUL
26e80 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  T);.    }.  }.  
26e81 72 65 67 44 61 74 61 20 3d 20 72 65 67 52 6f 77  regData = regRow
26e82 69 64 20 2b 20 31 3b 0a 20 20 72 65 67 52 65 63  id + 1;.  regRec
26e83 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
26e84 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
26e85 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26e86 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  3(v, OP_MakeReco
26e87 72 64 2c 20 72 65 67 44 61 74 61 2c 20 70 54 61  rd, regData, pTa
26e88 62 2d 3e 6e 43 6f 6c 2c 20 72 65 67 52 65 63 29  b->nCol, regRec)
26e89 3b 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65  ;.  sqlite3Table
26e8a 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70  AffinityStr(v, p
26e8b 54 61 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  Tab);.  sqlite3E
26e8c 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79  xprCacheAffinity
26e8d 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  Change(pParse, r
26e8e 65 67 44 61 74 61 2c 20 70 54 61 62 2d 3e 6e 43  egData, pTab->nC
26e8f 6f 6c 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73  ol);.  if( pPars
26e90 65 2d 3e 6e 65 73 74 65 64 20 29 7b 0a 20 20 20  e->nested ){.   
26e91 20 70 69 6b 5f 66 6c 61 67 73 20 3d 20 30 3b 0a   pik_flags = 0;.
26e92 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 69 6b    }else{.    pik
26e93 5f 66 6c 61 67 73 20 3d 20 4f 50 46 4c 41 47 5f  _flags = OPFLAG_
26e94 4e 43 48 41 4e 47 45 3b 0a 20 20 20 20 70 69 6b  NCHANGE;.    pik
26e95 5f 66 6c 61 67 73 20 7c 3d 20 28 69 73 55 70 64  _flags |= (isUpd
26e96 61 74 65 3f 4f 50 46 4c 41 47 5f 49 53 55 50 44  ate?OPFLAG_ISUPD
26e97 41 54 45 3a 4f 50 46 4c 41 47 5f 4c 41 53 54 52  ATE:OPFLAG_LASTR
26e98 4f 57 49 44 29 3b 0a 20 20 7d 0a 20 20 69 66 28  OWID);.  }.  if(
26e99 20 61 70 70 65 6e 64 42 69 61 73 20 29 7b 0a 20   appendBias ){. 
26e9a 20 20 20 70 69 6b 5f 66 6c 61 67 73 20 7c 3d 20     pik_flags |= 
26e9b 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 3b 0a 20  OPFLAG_APPEND;. 
26e9c 20 7d 0a 20 20 69 66 28 20 75 73 65 53 65 65 6b   }.  if( useSeek
26e9d 52 65 73 75 6c 74 20 29 7b 0a 20 20 20 20 70 69  Result ){.    pi
26e9e 6b 5f 66 6c 61 67 73 20 7c 3d 20 4f 50 46 4c 41  k_flags |= OPFLA
26e9f 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 3b  G_USESEEKRESULT;
26ea0 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64  .  }.  sqlite3Vd
26ea1 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49  beAddOp3(v, OP_I
26ea2 6e 73 65 72 74 2c 20 62 61 73 65 43 75 72 2c 20  nsert, baseCur, 
26ea3 72 65 67 52 65 63 2c 20 72 65 67 52 6f 77 69 64  regRec, regRowid
26ea4 29 3b 0a 20 20 69 66 28 20 21 70 50 61 72 73 65  );.  if( !pParse
26ea5 2d 3e 6e 65 73 74 65 64 20 29 7b 0a 20 20 20 20  ->nested ){.    
26ea6 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
26ea7 65 50 34 28 76 2c 20 2d 31 2c 20 70 54 61 62 2d  eP4(v, -1, pTab-
26ea8 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49  >zName, P4_STATI
26ea9 43 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  C);.  }.  sqlite
26eaa 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
26eab 20 70 69 6b 5f 66 6c 61 67 73 29 3b 0a 7d 0a 0a   pik_flags);.}..
26eac 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
26ead 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 6f 70  ode that will op
26eae 65 6e 20 63 75 72 73 6f 72 73 20 66 6f 72 20 61  en cursors for a
26eaf 20 74 61 62 6c 65 20 61 6e 64 20 66 6f 72 20 61   table and for a
26eb0 6c 6c 0a 2a 2a 20 69 6e 64 69 63 65 73 20 6f 66  ll.** indices of
26eb1 20 74 68 61 74 20 74 61 62 6c 65 2e 20 20 54 68   that table.  Th
26eb2 65 20 22 62 61 73 65 43 75 72 22 20 70 61 72 61  e "baseCur" para
26eb3 6d 65 74 65 72 20 69 73 20 74 68 65 20 63 75 72  meter is the cur
26eb4 73 6f 72 20 6e 75 6d 62 65 72 20 75 73 65 64 0a  sor number used.
26eb5 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  ** for the table
26eb6 2e 20 20 49 6e 64 69 63 65 73 20 61 72 65 20 6f  .  Indices are o
26eb7 70 65 6e 65 64 20 6f 6e 20 73 75 62 73 65 71 75  pened on subsequ
26eb8 65 6e 74 20 63 75 72 73 6f 72 73 2e 0a 2a 2a 0a  ent cursors..**.
26eb9 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
26eba 6d 62 65 72 20 6f 66 20 69 6e 64 69 63 65 73 20  mber of indices 
26ebb 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f  on the table..*/
26ebc 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
26ebd 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54  int sqlite3OpenT
26ebe 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 0a  ableAndIndices(.
26ebf 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
26ec0 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
26ec1 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65  ntext */.  Table
26ec2 20 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a 20 54   *pTab,     /* T
26ec3 61 62 6c 65 20 74 6f 20 62 65 20 6f 70 65 6e 65  able to be opene
26ec4 64 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65 43  d */.  int baseC
26ec5 75 72 2c 20 20 20 20 20 2f 2a 20 43 75 72 73 6f  ur,     /* Curso
26ec6 72 20 6e 75 6d 62 65 72 20 61 73 73 69 67 6e 65  r number assigne
26ec7 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 2a  d to the table *
26ec8 2f 0a 20 20 69 6e 74 20 6f 70 20 20 20 20 20 20  /.  int op      
26ec9 20 20 20 20 20 2f 2a 20 4f 50 5f 4f 70 65 6e 52       /* OP_OpenR
26eca 65 61 64 20 6f 72 20 4f 50 5f 4f 70 65 6e 57 72  ead or OP_OpenWr
26ecb 69 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ite */.){.  int 
26ecc 69 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20  i;.  int iDb;.  
26ecd 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 56  Index *pIdx;.  V
26ece 64 62 65 20 2a 76 3b 0a 0a 20 20 69 66 28 20 49  dbe *v;..  if( I
26ecf 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
26ed0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 44 62   return 0;.  iDb
26ed1 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
26ed2 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e  ToIndex(pParse->
26ed3 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
26ed4 61 29 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  a);.  v = sqlite
26ed5 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
26ed6 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30  ;.  assert( v!=0
26ed7 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 70 65   );.  sqlite3Ope
26ed8 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 62  nTable(pParse, b
26ed9 61 73 65 43 75 72 2c 20 69 44 62 2c 20 70 54 61  aseCur, iDb, pTa
26eda 62 2c 20 6f 70 29 3b 0a 20 20 66 6f 72 28 69 3d  b, op);.  for(i=
26edb 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  1, pIdx=pTab->pI
26edc 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
26edd 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 2b  =pIdx->pNext, i+
26ede 2b 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20  +){.    KeyInfo 
26edf 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49  *pKey = sqlite3I
26ee0 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72  ndexKeyinfo(pPar
26ee1 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 61  se, pIdx);.    a
26ee2 73 73 65 72 74 28 20 70 49 64 78 2d 3e 70 53 63  ssert( pIdx->pSc
26ee3 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68  hema==pTab->pSch
26ee4 65 6d 61 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ema );.    sqlit
26ee5 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
26ee6 6f 70 2c 20 69 2b 62 61 73 65 43 75 72 2c 20 70  op, i+baseCur, p
26ee7 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a  Idx->tnum, iDb,.
26ee8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26ee9 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65        (char*)pKe
26eea 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41  y, P4_KEYINFO_HA
26eeb 4e 44 4f 46 46 29 3b 0a 20 20 20 20 56 64 62 65  NDOFF);.    Vdbe
26eec 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22  Comment((v, "%s"
26eed 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b  , pIdx->zName));
26eee 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73  .  }.  if( pPars
26eef 65 2d 3e 6e 54 61 62 3c 62 61 73 65 43 75 72 2b  e->nTab<baseCur+
26ef0 69 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d  i ){.    pParse-
26ef1 3e 6e 54 61 62 20 3d 20 62 61 73 65 43 75 72 2b  >nTab = baseCur+
26ef2 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  i;.  }.  return 
26ef3 69 2d 31 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66 20  i-1;.}...#ifdef 
26ef4 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a  SQLITE_TEST./*.*
26ef5 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
26ef6 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
26ef7 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77  is incremented w
26ef8 68 65 6e 65 76 65 72 20 74 68 65 0a 2a 2a 20 74  henever the.** t
26ef9 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a 61  ransfer optimiza
26efa 74 69 6f 6e 20 69 73 20 75 73 65 64 2e 20 20 54  tion is used.  T
26efb 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20  his is used for 
26efc 74 65 73 74 69 6e 67 0a 2a 2a 20 70 75 72 70 6f  testing.** purpo
26efd 73 65 73 20 6f 6e 6c 79 20 2d 20 74 6f 20 6d 61  ses only - to ma
26efe 6b 65 20 73 75 72 65 20 74 68 65 20 74 72 61 6e  ke sure the tran
26eff 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f  sfer optimizatio
26f00 6e 20 72 65 61 6c 6c 79 0a 2a 2a 20 69 73 20 68  n really.** is h
26f01 61 70 70 65 6e 69 6e 67 20 77 68 65 6e 20 69 74  appening when it
26f02 20 69 73 20 73 75 70 70 6f 73 65 20 74 6f 2e 0a   is suppose to..
26f03 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
26f04 74 20 73 71 6c 69 74 65 33 5f 78 66 65 72 6f 70  t sqlite3_xferop
26f05 74 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 69 66 20  t_count;.#endif 
26f06 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2a  /* SQLITE_TEST *
26f07 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  /...#ifndef SQLI
26f08 54 45 5f 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54  TE_OMIT_XFER_OPT
26f09 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
26f0a 63 6f 6c 6c 61 74 69 6f 6e 20 6e 61 6d 65 73 20  collation names 
26f0b 74 6f 20 73 65 65 20 69 66 20 74 68 65 79 20 61  to see if they a
26f0c 72 65 20 63 6f 6d 70 61 74 69 62 6c 65 2e 0a 2a  re compatible..*
26f0d 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 78 66 65  /.static int xfe
26f0e 72 43 6f 6d 70 61 74 69 62 6c 65 43 6f 6c 6c 61  rCompatibleColla
26f0f 74 69 6f 6e 28 63 6f 6e 73 74 20 63 68 61 72 20  tion(const char 
26f10 2a 7a 31 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *z1, const char 
26f11 2a 7a 32 29 7b 0a 20 20 69 66 28 20 7a 31 3d 3d  *z2){.  if( z1==
26f12 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
26f13 7a 32 3d 3d 30 3b 0a 20 20 7d 0a 20 20 69 66 28  z2==0;.  }.  if(
26f14 20 7a 32 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65   z2==0 ){.    re
26f15 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65  turn 0;.  }.  re
26f16 74 75 72 6e 20 73 71 6c 69 74 65 33 53 74 72 49  turn sqlite3StrI
26f17 43 6d 70 28 7a 31 2c 20 7a 32 29 3d 3d 30 3b 0a  Cmp(z1, z2)==0;.
26f18 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  }.../*.** Check 
26f19 74 6f 20 73 65 65 20 69 66 20 69 6e 64 65 78 20  to see if index 
26f1a 70 53 72 63 20 69 73 20 63 6f 6d 70 61 74 69 62  pSrc is compatib
26f1b 6c 65 20 61 73 20 61 20 73 6f 75 72 63 65 20 6f  le as a source o
26f1c 66 20 64 61 74 61 0a 2a 2a 20 66 6f 72 20 69 6e  f data.** for in
26f1d 64 65 78 20 70 44 65 73 74 20 69 6e 20 61 6e 20  dex pDest in an 
26f1e 69 6e 73 65 72 74 20 74 72 61 6e 73 66 65 72 20  insert transfer 
26f1f 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 54  optimization.  T
26f20 68 65 20 72 75 6c 65 73 0a 2a 2a 20 66 6f 72 20  he rules.** for 
26f21 61 20 63 6f 6d 70 61 74 69 62 6c 65 20 69 6e 64  a compatible ind
26f22 65 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20  ex:.**.**    *  
26f23 20 54 68 65 20 69 6e 64 65 78 20 69 73 20 6f 76   The index is ov
26f24 65 72 20 74 68 65 20 73 61 6d 65 20 73 65 74 20  er the same set 
26f25 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 20 20  of columns.**   
26f26 20 2a 20 20 20 54 68 65 20 73 61 6d 65 20 44 45   *   The same DE
26f27 53 43 20 61 6e 64 20 41 53 43 20 6d 61 72 6b 69  SC and ASC marki
26f28 6e 67 73 20 6f 63 63 75 72 73 20 6f 6e 20 61 6c  ngs occurs on al
26f29 6c 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 20 20 20  l columns.**    
26f2a 2a 20 20 20 54 68 65 20 73 61 6d 65 20 6f 6e 45  *   The same onE
26f2b 72 72 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 20  rror processing 
26f2c 28 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67  (OE_Abort, OE_Ig
26f2d 6e 6f 72 65 2c 20 65 74 63 29 0a 2a 2a 20 20 20  nore, etc).**   
26f2e 20 2a 20 20 20 54 68 65 20 73 61 6d 65 20 63 6f   *   The same co
26f2f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
26f30 20 6f 6e 20 65 61 63 68 20 63 6f 6c 75 6d 6e 0a   on each column.
26f31 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 78 66  */.static int xf
26f32 65 72 43 6f 6d 70 61 74 69 62 6c 65 49 6e 64 65  erCompatibleInde
26f33 78 28 49 6e 64 65 78 20 2a 70 44 65 73 74 2c 20  x(Index *pDest, 
26f34 49 6e 64 65 78 20 2a 70 53 72 63 29 7b 0a 20 20  Index *pSrc){.  
26f35 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28  int i;.  assert(
26f36 20 70 44 65 73 74 20 26 26 20 70 53 72 63 20 29   pDest && pSrc )
26f37 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 65 73  ;.  assert( pDes
26f38 74 2d 3e 70 54 61 62 6c 65 21 3d 70 53 72 63 2d  t->pTable!=pSrc-
26f39 3e 70 54 61 62 6c 65 20 29 3b 0a 20 20 69 66 28  >pTable );.  if(
26f3a 20 70 44 65 73 74 2d 3e 6e 43 6f 6c 75 6d 6e 21   pDest->nColumn!
26f3b 3d 70 53 72 63 2d 3e 6e 43 6f 6c 75 6d 6e 20 29  =pSrc->nColumn )
26f3c 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
26f3d 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 6e    /* Different n
26f3e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
26f3f 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 44   */.  }.  if( pD
26f40 65 73 74 2d 3e 6f 6e 45 72 72 6f 72 21 3d 70 53  est->onError!=pS
26f41 72 63 2d 3e 6f 6e 45 72 72 6f 72 20 29 7b 0a 20  rc->onError ){. 
26f42 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
26f43 2a 20 44 69 66 66 65 72 65 6e 74 20 63 6f 6e 66  * Different conf
26f44 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20  lict resolution 
26f45 73 74 72 61 74 65 67 69 65 73 20 2a 2f 0a 20 20  strategies */.  
26f46 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  }.  for(i=0; i<p
26f47 53 72 63 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  Src->nColumn; i+
26f48 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 53 72 63  +){.    if( pSrc
26f49 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 21 3d 70  ->aiColumn[i]!=p
26f4a 44 65 73 74 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69  Dest->aiColumn[i
26f4b 5d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ] ){.      retur
26f4c 6e 20 30 3b 20 20 20 2f 2a 20 44 69 66 66 65 72  n 0;   /* Differ
26f4d 65 6e 74 20 63 6f 6c 75 6d 6e 73 20 69 6e 64 65  ent columns inde
26f4e 78 65 64 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20  xed */.    }.   
26f4f 20 69 66 28 20 70 53 72 63 2d 3e 61 53 6f 72 74   if( pSrc->aSort
26f50 4f 72 64 65 72 5b 69 5d 21 3d 70 44 65 73 74 2d  Order[i]!=pDest-
26f51 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 29  >aSortOrder[i] )
26f52 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
26f53 3b 20 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74  ;   /* Different
26f54 20 73 6f 72 74 20 6f 72 64 65 72 73 20 2a 2f 0a   sort orders */.
26f55 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 78      }.    if( !x
26f56 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 43 6f 6c  ferCompatibleCol
26f57 6c 61 74 69 6f 6e 28 70 53 72 63 2d 3e 61 7a 43  lation(pSrc->azC
26f58 6f 6c 6c 5b 69 5d 2c 70 44 65 73 74 2d 3e 61 7a  oll[i],pDest->az
26f59 43 6f 6c 6c 5b 69 5d 29 20 29 7b 0a 20 20 20 20  Coll[i]) ){.    
26f5a 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
26f5b 20 44 69 66 66 65 72 65 6e 74 20 63 6f 6c 6c 61   Different colla
26f5c 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 2a  ting sequences *
26f5d 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  /.    }.  }..  /
26f5e 2a 20 49 66 20 6e 6f 20 74 65 73 74 20 61 62 6f  * If no test abo
26f5f 76 65 20 66 61 69 6c 73 20 74 68 65 6e 20 74 68  ve fails then th
26f60 65 20 69 6e 64 69 63 65 73 20 6d 75 73 74 20 62  e indices must b
26f61 65 20 63 6f 6d 70 61 74 69 62 6c 65 20 2a 2f 0a  e compatible */.
26f62 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f    return 1;.}../
26f63 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 68 65  *.** Attempt the
26f64 20 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69   transfer optimi
26f65 7a 61 74 69 6f 6e 20 6f 6e 20 49 4e 53 45 52 54  zation on INSERT
26f66 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a  s of the form.**
26f67 0a 2a 2a 20 20 20 20 20 49 4e 53 45 52 54 20 49  .**     INSERT I
26f68 4e 54 4f 20 74 61 62 31 20 53 45 4c 45 43 54 20  NTO tab1 SELECT 
26f69 2a 20 46 52 4f 4d 20 74 61 62 32 3b 0a 2a 2a 0a  * FROM tab2;.**.
26f6a 2a 2a 20 54 68 69 73 20 6f 70 74 69 6d 69 7a 61  ** This optimiza
26f6b 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 61 74 74  tion is only att
26f6c 65 6d 70 74 65 64 20 69 66 0a 2a 2a 0a 2a 2a 20  empted if.**.** 
26f6d 20 20 20 28 31 29 20 20 74 61 62 31 20 61 6e 64     (1)  tab1 and
26f6e 20 74 61 62 32 20 68 61 76 65 20 69 64 65 6e 74   tab2 have ident
26f6f 69 63 61 6c 20 73 63 68 65 6d 61 73 20 69 6e 63  ical schemas inc
26f70 6c 75 64 69 6e 67 20 61 6c 6c 20 74 68 65 0a 2a  luding all the.*
26f71 2a 20 20 20 20 20 20 20 20 20 73 61 6d 65 20 69  *         same i
26f72 6e 64 69 63 65 73 20 61 6e 64 20 63 6f 6e 73 74  ndices and const
26f73 72 61 69 6e 74 73 0a 2a 2a 0a 2a 2a 20 20 20 20  raints.**.**    
26f74 28 32 29 20 20 74 61 62 31 20 61 6e 64 20 74 61  (2)  tab1 and ta
26f75 62 32 20 61 72 65 20 64 69 66 66 65 72 65 6e 74  b2 are different
26f76 20 74 61 62 6c 65 73 0a 2a 2a 0a 2a 2a 20 20 20   tables.**.**   
26f77 20 28 33 29 20 20 54 68 65 72 65 20 6d 75 73 74   (3)  There must
26f78 20 62 65 20 6e 6f 20 74 72 69 67 67 65 72 73 20   be no triggers 
26f79 6f 6e 20 74 61 62 31 0a 2a 2a 0a 2a 2a 20 20 20  on tab1.**.**   
26f7a 20 28 34 29 20 20 54 68 65 20 72 65 73 75 6c 74   (4)  The result
26f7b 20 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45   set of the SELE
26f7c 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  CT statement is 
26f7d 22 2a 22 0a 2a 2a 0a 2a 2a 20 20 20 20 28 35 29  "*".**.**    (5)
26f7e 20 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61    The SELECT sta
26f7f 74 65 6d 65 6e 74 20 68 61 73 20 6e 6f 20 57 48  tement has no WH
26f80 45 52 45 2c 20 48 41 56 49 4e 47 2c 20 4f 52 44  ERE, HAVING, ORD
26f81 45 52 20 42 59 2c 20 47 52 4f 55 50 20 42 59 2c  ER BY, GROUP BY,
26f82 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 72 20 4c  .**         or L
26f83 49 4d 49 54 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a  IMIT clause..**.
26f84 2a 2a 20 20 20 20 28 36 29 20 20 54 68 65 20 53  **    (6)  The S
26f85 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
26f86 69 73 20 61 20 73 69 6d 70 6c 65 20 28 6e 6f 74  is a simple (not
26f87 20 61 20 63 6f 6d 70 6f 75 6e 64 29 20 73 65 6c   a compound) sel
26f88 65 63 74 20 74 68 61 74 0a 2a 2a 20 20 20 20 20  ect that.**     
26f89 20 20 20 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 6c      contains onl
26f8a 79 20 74 61 62 32 20 69 6e 20 69 74 73 20 46 52  y tab2 in its FR
26f8b 4f 4d 20 63 6c 61 75 73 65 0a 2a 2a 0a 2a 2a 20  OM clause.**.** 
26f8c 54 68 69 73 20 6d 65 74 68 6f 64 20 66 6f 72 20  This method for 
26f8d 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65  implementing the
26f8e 20 49 4e 53 45 52 54 20 74 72 61 6e 73 66 65 72   INSERT transfer
26f8f 73 20 72 61 77 20 72 65 63 6f 72 64 73 20 66 72  s raw records fr
26f90 6f 6d 0a 2a 2a 20 74 61 62 32 20 6f 76 65 72 20  om.** tab2 over 
26f91 74 6f 20 74 61 62 31 2e 20 20 54 68 65 20 63 6f  to tab1.  The co
26f92 6c 75 6d 6e 73 20 61 72 65 20 6e 6f 74 20 64 65  lumns are not de
26f93 63 6f 64 65 64 2e 20 20 52 61 77 20 72 65 63 6f  coded.  Raw reco
26f94 72 64 73 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20  rds from.** the 
26f95 69 6e 64 69 63 65 73 20 6f 66 20 74 61 62 32 20  indices of tab2 
26f96 61 72 65 20 74 72 61 6e 73 66 65 72 65 64 20 74  are transfered t
26f97 6f 20 74 61 62 31 20 61 73 20 77 65 6c 6c 2e 20  o tab1 as well. 
26f98 20 49 6e 20 73 6f 20 64 6f 69 6e 67 2c 0a 2a 2a   In so doing,.**
26f99 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 74   the resulting t
26f9a 61 62 31 20 68 61 73 20 6d 75 63 68 20 6c 65 73  ab1 has much les
26f9b 73 20 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 2e  s fragmentation.
26f9c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
26f9d 69 6e 65 20 72 65 74 75 72 6e 73 20 54 52 55 45  ine returns TRUE
26f9e 20 69 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61   if the optimiza
26f9f 74 69 6f 6e 20 69 73 20 61 74 74 65 6d 70 74 65  tion is attempte
26fa0 64 2e 20 20 49 66 20 61 6e 79 0a 2a 2a 20 6f 66  d.  If any.** of
26fa1 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20   the conditions 
26fa2 61 62 6f 76 65 20 66 61 69 6c 20 73 6f 20 74 68  above fail so th
26fa3 61 74 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74  at the optimizat
26fa4 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 0a 2a  ion should not.*
26fa5 2a 20 62 65 20 61 74 74 65 6d 70 74 65 64 2c 20  * be attempted, 
26fa6 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  then this routin
26fa7 65 20 72 65 74 75 72 6e 73 20 46 41 4c 53 45 2e  e returns FALSE.
26fa8 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 78  .*/.static int x
26fa9 66 65 72 4f 70 74 69 6d 69 7a 61 74 69 6f 6e 28  ferOptimization(
26faa 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
26fab 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73  ,        /* Pars
26fac 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
26fad 54 61 62 6c 65 20 2a 70 44 65 73 74 2c 20 20 20  Table *pDest,   
26fae 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62        /* The tab
26faf 6c 65 20 77 65 20 61 72 65 20 69 6e 73 65 72 74  le we are insert
26fb0 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20 20 53 65  ing into */.  Se
26fb1 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20  lect *pSelect,  
26fb2 20 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20      /* A SELECT 
26fb3 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 75 73 65  statement to use
26fb4 20 61 73 20 74 68 65 20 64 61 74 61 20 73 6f 75   as the data sou
26fb5 72 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45  rce */.  int onE
26fb6 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20 20 2f  rror,          /
26fb7 2a 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c 65 20  * How to handle 
26fb8 63 6f 6e 73 74 72 61 69 6e 74 20 65 72 72 6f 72  constraint error
26fb9 73 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 44 65  s */.  int iDbDe
26fba 73 74 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  st           /* 
26fbb 54 68 65 20 64 61 74 61 62 61 73 65 20 6f 66 20  The database of 
26fbc 70 44 65 73 74 20 2a 2f 0a 29 7b 0a 20 20 45 78  pDest */.){.  Ex
26fbd 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20  prList *pEList; 
26fbe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26fbf 2a 20 54 68 65 20 72 65 73 75 6c 74 20 73 65 74  * The result set
26fc0 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 2a   of the SELECT *
26fc1 2f 0a 20 20 54 61 62 6c 65 20 2a 70 53 72 63 3b  /.  Table *pSrc;
26fc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26fc3 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
26fc4 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  e in the FROM cl
26fc5 61 75 73 65 20 6f 66 20 53 45 4c 45 43 54 20 2a  ause of SELECT *
26fc6 2f 0a 20 20 49 6e 64 65 78 20 2a 70 53 72 63 49  /.  Index *pSrcI
26fc7 64 78 2c 20 2a 70 44 65 73 74 49 64 78 3b 20 20  dx, *pDestIdx;  
26fc8 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 61       /* Source a
26fc9 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69  nd destination i
26fca 6e 64 69 63 65 73 20 2a 2f 0a 20 20 73 74 72 75  ndices */.  stru
26fcb 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
26fcc 2a 70 49 74 65 6d 3b 20 20 20 20 20 20 2f 2a 20  *pItem;      /* 
26fcd 41 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 70 53  An element of pS
26fce 65 6c 65 63 74 2d 3e 70 53 72 63 20 2a 2f 0a 20  elect->pSrc */. 
26fcf 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
26fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26fd1 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
26fd2 72 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 53 72  r */.  int iDbSr
26fd3 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
26fd4 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
26fd5 61 74 61 62 61 73 65 20 6f 66 20 70 53 72 63 20  atabase of pSrc 
26fd6 2a 2f 0a 20 20 69 6e 74 20 69 53 72 63 2c 20 69  */.  int iSrc, i
26fd7 44 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  Dest;           
26fd8 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 73        /* Cursors
26fd9 20 66 72 6f 6d 20 73 6f 75 72 63 65 20 61 6e 64   from source and
26fda 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a   destination */.
26fdb 20 20 69 6e 74 20 61 64 64 72 31 2c 20 61 64 64    int addr1, add
26fdc 72 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  r2;             
26fdd 20 20 20 2f 2a 20 4c 6f 6f 70 20 61 64 64 72 65     /* Loop addre
26fde 73 73 65 73 20 2a 2f 0a 20 20 69 6e 74 20 65 6d  sses */.  int em
26fdf 70 74 79 44 65 73 74 54 65 73 74 3b 20 20 20 20  ptyDestTest;    
26fe0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
26fe1 64 72 65 73 73 20 6f 66 20 74 65 73 74 20 66 6f  dress of test fo
26fe2 72 20 65 6d 70 74 79 20 70 44 65 73 74 20 2a 2f  r empty pDest */
26fe3 0a 20 20 69 6e 74 20 65 6d 70 74 79 53 72 63 54  .  int emptySrcT
26fe4 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  est;            
26fe5 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
26fe6 66 20 74 65 73 74 20 66 6f 72 20 65 6d 70 74 79  f test for empty
26fe7 20 70 53 72 63 20 2a 2f 0a 20 20 56 64 62 65 20   pSrc */.  Vdbe 
26fe8 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *v;             
26fe9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
26fea 68 65 20 56 44 42 45 20 77 65 20 61 72 65 20 62  he VDBE we are b
26feb 75 69 6c 64 69 6e 67 20 2a 2f 0a 20 20 4b 65 79  uilding */.  Key
26fec 49 6e 66 6f 20 2a 70 4b 65 79 3b 20 20 20 20 20  Info *pKey;     
26fed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
26fee 20 4b 65 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   Key information
26fef 20 66 6f 72 20 61 6e 20 69 6e 64 65 78 20 2a 2f   for an index */
26ff0 0a 20 20 69 6e 74 20 72 65 67 41 75 74 6f 69 6e  .  int regAutoin
26ff1 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
26ff2 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 72 65      /* Memory re
26ff3 67 69 73 74 65 72 20 75 73 65 64 20 62 79 20 41  gister used by A
26ff4 55 54 4f 49 4e 43 20 2a 2f 0a 20 20 69 6e 74 20  UTOINC */.  int 
26ff5 64 65 73 74 48 61 73 55 6e 69 71 75 65 49 64 78  destHasUniqueIdx
26ff6 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
26ff7 54 72 75 65 20 69 66 20 70 44 65 73 74 20 68 61  True if pDest ha
26ff8 73 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78  s a UNIQUE index
26ff9 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74   */.  int regDat
26ffa 61 2c 20 72 65 67 52 6f 77 69 64 3b 20 20 20 20  a, regRowid;    
26ffb 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
26ffc 65 72 73 20 68 6f 6c 64 69 6e 67 20 64 61 74 61  ers holding data
26ffd 20 61 6e 64 20 72 6f 77 69 64 20 2a 2f 0a 0a 20   and rowid */.. 
26ffe 20 69 66 28 20 70 53 65 6c 65 63 74 3d 3d 30 20   if( pSelect==0 
26fff 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
27000 20 20 20 2f 2a 20 4d 75 73 74 20 62 65 20 6f 66     /* Must be of
27001 20 74 68 65 20 66 6f 72 6d 20 20 49 4e 53 45 52   the form  INSER
27002 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43  T INTO ... SELEC
27003 54 20 2e 2e 2e 20 2a 2f 0a 20 20 7d 0a 20 20 69  T ... */.  }.  i
27004 66 28 20 73 71 6c 69 74 65 33 54 72 69 67 67 65  f( sqlite3Trigge
27005 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 44  rList(pParse, pD
27006 65 73 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75  est) ){.    retu
27007 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 31 20  rn 0;   /* tab1 
27008 6d 75 73 74 20 6e 6f 74 20 68 61 76 65 20 74 72  must not have tr
27009 69 67 67 65 72 73 20 2a 2f 0a 20 20 7d 0a 23 69  iggers */.  }.#i
2700a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2700b 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
2700c 20 69 66 28 20 70 44 65 73 74 2d 3e 74 61 62 46   if( pDest->tabF
2700d 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74 75 61  lags & TF_Virtua
2700e 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  l ){.    return 
2700f 30 3b 20 20 20 2f 2a 20 74 61 62 31 20 6d 75 73  0;   /* tab1 mus
27010 74 20 6e 6f 74 20 62 65 20 61 20 76 69 72 74 75  t not be a virtu
27011 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7d 0a  al table */.  }.
27012 23 65 6e 64 69 66 0a 20 20 69 66 28 20 6f 6e 45  #endif.  if( onE
27013 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74  rror==OE_Default
27014 20 29 7b 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20   ){.    onError 
27015 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a  = OE_Abort;.  }.
27016 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 21 3d 4f    if( onError!=O
27017 45 5f 41 62 6f 72 74 20 26 26 20 6f 6e 45 72 72  E_Abort && onErr
27018 6f 72 21 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20  or!=OE_Rollback 
27019 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
2701a 20 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 64 6f 20     /* Cannot do 
2701b 4f 52 20 52 45 50 4c 41 43 45 20 6f 72 20 4f 52  OR REPLACE or OR
2701c 20 49 47 4e 4f 52 45 20 6f 72 20 4f 52 20 46 41   IGNORE or OR FA
2701d 49 4c 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 65  IL */.  }.  asse
2701e 72 74 28 70 53 65 6c 65 63 74 2d 3e 70 53 72 63  rt(pSelect->pSrc
2701f 29 3b 20 20 20 2f 2a 20 61 6c 6c 6f 63 61 74 65  );   /* allocate
27020 64 20 65 76 65 6e 20 69 66 20 74 68 65 72 65 20  d even if there 
27021 69 73 20 6e 6f 20 46 52 4f 4d 20 63 6c 61 75 73  is no FROM claus
27022 65 20 2a 2f 0a 20 20 69 66 28 20 70 53 65 6c 65  e */.  if( pSele
27023 63 74 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 21 3d  ct->pSrc->nSrc!=
27024 31 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  1 ){.    return 
27025 30 3b 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61  0;   /* FROM cla
27026 75 73 65 20 6d 75 73 74 20 68 61 76 65 20 65 78  use must have ex
27027 61 63 74 6c 79 20 6f 6e 65 20 74 65 72 6d 20 2a  actly one term *
27028 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 65 6c  /.  }.  if( pSel
27029 65 63 74 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e  ect->pSrc->a[0].
2702a 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 72  pSelect ){.    r
2702b 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 46 52  eturn 0;   /* FR
2702c 4f 4d 20 63 6c 61 75 73 65 20 63 61 6e 6e 6f 74  OM clause cannot
2702d 20 63 6f 6e 74 61 69 6e 20 61 20 73 75 62 71 75   contain a subqu
2702e 65 72 79 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28  ery */.  }.  if(
2702f 20 70 53 65 6c 65 63 74 2d 3e 70 57 68 65 72 65   pSelect->pWhere
27030 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
27031 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61  ;   /* SELECT ma
27032 79 20 6e 6f 74 20 68 61 76 65 20 61 20 57 48 45  y not have a WHE
27033 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 7d  RE clause */.  }
27034 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e  .  if( pSelect->
27035 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
27036 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 53  return 0;   /* S
27037 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 68 61  ELECT may not ha
27038 76 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63  ve an ORDER BY c
27039 6c 61 75 73 65 20 2a 2f 0a 20 20 7d 0a 20 20 2f  lause */.  }.  /
2703a 2a 20 44 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f  * Do not need to
2703b 20 74 65 73 74 20 66 6f 72 20 61 20 48 41 56 49   test for a HAVI
2703c 4e 47 20 63 6c 61 75 73 65 2e 20 20 49 66 20 48  NG clause.  If H
2703d 41 56 49 4e 47 20 69 73 20 70 72 65 73 65 6e 74  AVING is present
2703e 20 62 75 74 0a 20 20 2a 2a 20 74 68 65 72 65 20   but.  ** there 
2703f 69 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 2c 20  is no ORDER BY, 
27040 77 65 20 77 69 6c 6c 20 67 65 74 20 61 6e 20 65  we will get an e
27041 72 72 6f 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70  rror. */.  if( p
27042 53 65 6c 65 63 74 2d 3e 70 47 72 6f 75 70 42 79  Select->pGroupBy
27043 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
27044 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61  ;   /* SELECT ma
27045 79 20 6e 6f 74 20 68 61 76 65 20 61 20 47 52 4f  y not have a GRO
27046 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a  UP BY clause */.
27047 20 20 7d 0a 20 20 69 66 28 20 70 53 65 6c 65 63    }.  if( pSelec
27048 74 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20  t->pLimit ){.   
27049 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
2704a 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 68  SELECT may not h
2704b 61 76 65 20 61 20 4c 49 4d 49 54 20 63 6c 61 75  ave a LIMIT clau
2704c 73 65 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 65  se */.  }.  asse
2704d 72 74 28 20 70 53 65 6c 65 63 74 2d 3e 70 4f 66  rt( pSelect->pOf
2704e 66 73 65 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 4d  fset==0 );  /* M
2704f 75 73 74 20 62 65 20 73 6f 20 69 66 20 70 4c 69  ust be so if pLi
27050 6d 69 74 3d 3d 30 20 2a 2f 0a 20 20 69 66 28 20  mit==0 */.  if( 
27051 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 20  pSelect->pPrior 
27052 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
27053 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79     /* SELECT may
27054 20 6e 6f 74 20 62 65 20 61 20 63 6f 6d 70 6f 75   not be a compou
27055 6e 64 20 71 75 65 72 79 20 2a 2f 0a 20 20 7d 0a  nd query */.  }.
27056 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 73    if( pSelect->s
27057 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73  elFlags & SF_Dis
27058 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 72 65 74  tinct ){.    ret
27059 75 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45  urn 0;   /* SELE
2705a 43 54 20 6d 61 79 20 6e 6f 74 20 62 65 20 44 49  CT may not be DI
2705b 53 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 0a 20 20  STINCT */.  }.  
2705c 70 45 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74  pEList = pSelect
2705d 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 61 73 73 65  ->pEList;.  asse
2705e 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b  rt( pEList!=0 );
2705f 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 6e  .  if( pEList->n
27060 45 78 70 72 21 3d 31 20 29 7b 0a 20 20 20 20 72  Expr!=1 ){.    r
27061 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 54 68  eturn 0;   /* Th
27062 65 20 72 65 73 75 6c 74 20 73 65 74 20 6d 75 73  e result set mus
27063 74 20 68 61 76 65 20 65 78 61 63 74 6c 79 20 6f  t have exactly o
27064 6e 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 7d  ne column */.  }
27065 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73  .  assert( pELis
27066 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 20 29 3b  t->a[0].pExpr );
27067 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 61  .  if( pEList->a
27068 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 21 3d 54  [0].pExpr->op!=T
27069 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 72 65 74  K_ALL ){.    ret
2706a 75 72 6e 20 30 3b 20 20 20 2f 2a 20 54 68 65 20  urn 0;   /* The 
2706b 72 65 73 75 6c 74 20 73 65 74 20 6d 75 73 74 20  result set must 
2706c 62 65 20 74 68 65 20 73 70 65 63 69 61 6c 20 6f  be the special o
2706d 70 65 72 61 74 6f 72 20 22 2a 22 20 2a 2f 0a 20  perator "*" */. 
2706e 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73   }..  /* At this
2706f 20 70 6f 69 6e 74 20 77 65 20 68 61 76 65 20 65   point we have e
27070 73 74 61 62 6c 69 73 68 65 64 20 74 68 61 74 20  stablished that 
27071 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73  the statement is
27072 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 72   of the.  ** cor
27073 72 65 63 74 20 73 79 6e 74 61 63 74 69 63 20 66  rect syntactic f
27074 6f 72 6d 20 74 6f 20 70 61 72 74 69 63 69 70 61  orm to participa
27075 74 65 20 69 6e 20 74 68 69 73 20 6f 70 74 69 6d  te in this optim
27076 69 7a 61 74 69 6f 6e 2e 20 20 4e 6f 77 0a 20 20  ization.  Now.  
27077 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 63 68  ** we have to ch
27078 65 63 6b 20 74 68 65 20 73 65 6d 61 6e 74 69 63  eck the semantic
27079 73 2e 0a 20 20 2a 2f 0a 20 20 70 49 74 65 6d 20  s..  */.  pItem 
2707a 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 2d  = pSelect->pSrc-
2707b 3e 61 3b 0a 20 20 70 53 72 63 20 3d 20 73 71 6c  >a;.  pSrc = sql
2707c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
2707d 70 50 61 72 73 65 2c 20 30 2c 20 70 49 74 65 6d  pParse, 0, pItem
2707e 2d 3e 7a 4e 61 6d 65 2c 20 70 49 74 65 6d 2d 3e  ->zName, pItem->
2707f 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66  zDatabase);.  if
27080 28 20 70 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20  ( pSrc==0 ){.   
27081 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
27082 46 52 4f 4d 20 63 6c 61 75 73 65 20 64 6f 65 73  FROM clause does
27083 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 72   not contain a r
27084 65 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7d  eal table */.  }
27085 0a 20 20 69 66 28 20 70 53 72 63 3d 3d 70 44 65  .  if( pSrc==pDe
27086 73 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  st ){.    return
27087 20 30 3b 20 20 20 2f 2a 20 74 61 62 31 20 61 6e   0;   /* tab1 an
27088 64 20 74 61 62 32 20 6d 61 79 20 6e 6f 74 20 62  d tab2 may not b
27089 65 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65  e the same table
2708a 20 2a 2f 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20   */.  }.#ifndef 
2708b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2708c 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 70  UALTABLE.  if( p
2708d 53 72 63 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  Src->tabFlags & 
2708e 54 46 5f 56 69 72 74 75 61 6c 20 29 7b 0a 20 20  TF_Virtual ){.  
2708f 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
27090 20 74 61 62 32 20 6d 75 73 74 20 6e 6f 74 20 62   tab2 must not b
27091 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  e a virtual tabl
27092 65 20 2a 2f 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  e */.  }.#endif.
27093 20 20 69 66 28 20 70 53 72 63 2d 3e 70 53 65 6c    if( pSrc->pSel
27094 65 63 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ect ){.    retur
27095 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 32 20 6d  n 0;   /* tab2 m
27096 61 79 20 6e 6f 74 20 62 65 20 61 20 76 69 65 77  ay not be a view
27097 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 44   */.  }.  if( pD
27098 65 73 74 2d 3e 6e 43 6f 6c 21 3d 70 53 72 63 2d  est->nCol!=pSrc-
27099 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 72 65 74  >nCol ){.    ret
2709a 75 72 6e 20 30 3b 20 20 20 2f 2a 20 4e 75 6d 62  urn 0;   /* Numb
2709b 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6d 75  er of columns mu
2709c 73 74 20 62 65 20 74 68 65 20 73 61 6d 65 20 69  st be the same i
2709d 6e 20 74 61 62 31 20 61 6e 64 20 74 61 62 32 20  n tab1 and tab2 
2709e 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 65  */.  }.  if( pDe
2709f 73 74 2d 3e 69 50 4b 65 79 21 3d 70 53 72 63 2d  st->iPKey!=pSrc-
270a0 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 72 65  >iPKey ){.    re
270a1 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 42 6f 74  turn 0;   /* Bot
270a2 68 20 74 61 62 6c 65 73 20 6d 75 73 74 20 68 61  h tables must ha
270a3 76 65 20 74 68 65 20 73 61 6d 65 20 49 4e 54 45  ve the same INTE
270a4 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20  GER PRIMARY KEY 
270a5 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  */.  }.  for(i=0
270a6 3b 20 69 3c 70 44 65 73 74 2d 3e 6e 43 6f 6c 3b  ; i<pDest->nCol;
270a7 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70   i++){.    if( p
270a8 44 65 73 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 61 66  Dest->aCol[i].af
270a9 66 69 6e 69 74 79 21 3d 70 53 72 63 2d 3e 61 43  finity!=pSrc->aC
270aa 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 20 29  ol[i].affinity )
270ab 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
270ac 3b 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 79  ;    /* Affinity
270ad 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61 6d   must be the sam
270ae 65 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73  e on all columns
270af 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   */.    }.    if
270b0 28 20 21 78 66 65 72 43 6f 6d 70 61 74 69 62 6c  ( !xferCompatibl
270b1 65 43 6f 6c 6c 61 74 69 6f 6e 28 70 44 65 73 74  eCollation(pDest
270b2 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 2c  ->aCol[i].zColl,
270b3 20 70 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a   pSrc->aCol[i].z
270b4 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 72  Coll) ){.      r
270b5 65 74 75 72 6e 20 30 3b 20 20 20 20 2f 2a 20 43  eturn 0;    /* C
270b6 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
270b7 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61  e must be the sa
270b8 6d 65 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d 6e  me on all column
270b9 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69  s */.    }.    i
270ba 66 28 20 70 44 65 73 74 2d 3e 61 43 6f 6c 5b 69  f( pDest->aCol[i
270bb 5d 2e 6e 6f 74 4e 75 6c 6c 20 26 26 20 21 70 53  ].notNull && !pS
270bc 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e  rc->aCol[i].notN
270bd 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ull ){.      ret
270be 75 72 6e 20 30 3b 20 20 20 20 2f 2a 20 74 61 62  urn 0;    /* tab
270bf 32 20 6d 75 73 74 20 62 65 20 4e 4f 54 20 4e 55  2 must be NOT NU
270c0 4c 4c 20 69 66 20 74 61 62 31 20 69 73 20 2a 2f  LL if tab1 is */
270c1 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72  .    }.  }.  for
270c2 28 70 44 65 73 74 49 64 78 3d 70 44 65 73 74 2d  (pDestIdx=pDest-
270c3 3e 70 49 6e 64 65 78 3b 20 70 44 65 73 74 49 64  >pIndex; pDestId
270c4 78 3b 20 70 44 65 73 74 49 64 78 3d 70 44 65 73  x; pDestIdx=pDes
270c5 74 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  tIdx->pNext){.  
270c6 20 20 69 66 28 20 70 44 65 73 74 49 64 78 2d 3e    if( pDestIdx->
270c7 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65  onError!=OE_None
270c8 20 29 7b 0a 20 20 20 20 20 20 64 65 73 74 48 61   ){.      destHa
270c9 73 55 6e 69 71 75 65 49 64 78 20 3d 20 31 3b 0a  sUniqueIdx = 1;.
270ca 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 70 53      }.    for(pS
270cb 72 63 49 64 78 3d 70 53 72 63 2d 3e 70 49 6e 64  rcIdx=pSrc->pInd
270cc 65 78 3b 20 70 53 72 63 49 64 78 3b 20 70 53 72  ex; pSrcIdx; pSr
270cd 63 49 64 78 3d 70 53 72 63 49 64 78 2d 3e 70 4e  cIdx=pSrcIdx->pN
270ce 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ext){.      if( 
270cf 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 49 6e  xferCompatibleIn
270d0 64 65 78 28 70 44 65 73 74 49 64 78 2c 20 70 53  dex(pDestIdx, pS
270d1 72 63 49 64 78 29 20 29 20 62 72 65 61 6b 3b 0a  rcIdx) ) break;.
270d2 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 53      }.    if( pS
270d3 72 63 49 64 78 3d 3d 30 20 29 7b 0a 20 20 20 20  rcIdx==0 ){.    
270d4 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 2f    return 0;    /
270d5 2a 20 70 44 65 73 74 49 64 78 20 68 61 73 20 6e  * pDestIdx has n
270d6 6f 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  o corresponding 
270d7 69 6e 64 65 78 20 69 6e 20 70 53 72 63 20 2a 2f  index in pSrc */
270d8 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 6e 64  .    }.  }.#ifnd
270d9 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
270da 48 45 43 4b 0a 20 20 69 66 28 20 70 44 65 73 74  HECK.  if( pDest
270db 2d 3e 70 43 68 65 63 6b 20 26 26 20 21 73 71 6c  ->pCheck && !sql
270dc 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28  ite3ExprCompare(
270dd 70 53 72 63 2d 3e 70 43 68 65 63 6b 2c 20 70 44  pSrc->pCheck, pD
270de 65 73 74 2d 3e 70 43 68 65 63 6b 29 20 29 7b 0a  est->pCheck) ){.
270df 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
270e0 2f 2a 20 54 61 62 6c 65 73 20 68 61 76 65 20 64  /* Tables have d
270e1 69 66 66 65 72 65 6e 74 20 43 48 45 43 4b 20 63  ifferent CHECK c
270e2 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 54 69 63  onstraints.  Tic
270e3 6b 65 74 20 23 32 32 35 32 20 2a 2f 0a 20 20 7d  ket #2252 */.  }
270e4 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66  .#endif..  /* If
270e5 20 77 65 20 67 65 74 20 74 68 69 73 20 66 61 72   we get this far
270e6 2c 20 69 74 20 6d 65 61 6e 73 20 65 69 74 68 65  , it means eithe
270e7 72 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  r:.  **.  **    
270e8 2a 20 20 20 57 65 20 63 61 6e 20 61 6c 77 61 79  *   We can alway
270e9 73 20 64 6f 20 74 68 65 20 74 72 61 6e 73 66 65  s do the transfe
270ea 72 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 63  r if the table c
270eb 6f 6e 74 61 69 6e 73 20 61 6e 0a 20 20 2a 2a 20  ontains an.  ** 
270ec 20 20 20 20 20 20 20 61 6e 20 69 6e 74 65 67 65         an intege
270ed 72 20 70 72 69 6d 61 72 79 20 6b 65 79 0a 20 20  r primary key.  
270ee 2a 2a 0a 20 20 2a 2a 20 20 20 20 2a 20 20 20 57  **.  **    *   W
270ef 65 20 63 61 6e 20 63 6f 6e 64 69 74 69 6f 6e 61  e can conditiona
270f0 6c 6c 79 20 64 6f 20 74 68 65 20 74 72 61 6e 73  lly do the trans
270f1 66 65 72 20 69 66 20 74 68 65 20 64 65 73 74 69  fer if the desti
270f2 6e 61 74 69 6f 6e 0a 20 20 2a 2a 20 20 20 20 20  nation.  **     
270f3 20 20 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74     table is empt
270f4 79 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53  y..  */.#ifdef S
270f5 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71 6c  QLITE_TEST.  sql
270f6 69 74 65 33 5f 78 66 65 72 6f 70 74 5f 63 6f 75  ite3_xferopt_cou
270f7 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 69  nt++;.#endif.  i
270f8 44 62 53 72 63 20 3d 20 73 71 6c 69 74 65 33 53  DbSrc = sqlite3S
270f9 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61  chemaToIndex(pPa
270fa 72 73 65 2d 3e 64 62 2c 20 70 53 72 63 2d 3e 70  rse->db, pSrc->p
270fb 53 63 68 65 6d 61 29 3b 0a 20 20 76 20 3d 20 73  Schema);.  v = s
270fc 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
270fd 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  arse);.  sqlite3
270fe 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61  CodeVerifySchema
270ff 28 70 50 61 72 73 65 2c 20 69 44 62 53 72 63 29  (pParse, iDbSrc)
27100 3b 0a 20 20 69 53 72 63 20 3d 20 70 50 61 72 73  ;.  iSrc = pPars
27101 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 69 44 65  e->nTab++;.  iDe
27102 73 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  st = pParse->nTa
27103 62 2b 2b 3b 0a 20 20 72 65 67 41 75 74 6f 69 6e  b++;.  regAutoin
27104 63 20 3d 20 61 75 74 6f 49 6e 63 42 65 67 69 6e  c = autoIncBegin
27105 28 70 50 61 72 73 65 2c 20 69 44 62 44 65 73 74  (pParse, iDbDest
27106 2c 20 70 44 65 73 74 29 3b 0a 20 20 73 71 6c 69  , pDest);.  sqli
27107 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61  te3OpenTable(pPa
27108 72 73 65 2c 20 69 44 65 73 74 2c 20 69 44 62 44  rse, iDest, iDbD
27109 65 73 74 2c 20 70 44 65 73 74 2c 20 4f 50 5f 4f  est, pDest, OP_O
2710a 70 65 6e 57 72 69 74 65 29 3b 0a 20 20 69 66 28  penWrite);.  if(
2710b 20 28 70 44 65 73 74 2d 3e 69 50 4b 65 79 3c 30   (pDest->iPKey<0
2710c 20 26 26 20 70 44 65 73 74 2d 3e 70 49 6e 64 65   && pDest->pInde
2710d 78 21 3d 30 29 20 7c 7c 20 64 65 73 74 48 61 73  x!=0) || destHas
2710e 55 6e 69 71 75 65 49 64 78 20 29 7b 0a 20 20 20  UniqueIdx ){.   
2710f 20 2f 2a 20 49 66 20 74 61 62 6c 65 73 20 64 6f   /* If tables do
27110 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 49 4e 54   not have an INT
27111 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
27112 20 61 6e 64 20 74 68 65 72 65 0a 20 20 20 20 2a   and there.    *
27113 2a 20 61 72 65 20 69 6e 64 69 63 65 73 20 74 6f  * are indices to
27114 20 62 65 20 63 6f 70 69 65 64 20 61 6e 64 20 74   be copied and t
27115 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69  he destination i
27116 73 20 6e 6f 74 20 65 6d 70 74 79 2c 0a 20 20 20  s not empty,.   
27117 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 64   ** we have to d
27118 69 73 61 6c 6c 6f 77 20 74 68 65 20 74 72 61 6e  isallow the tran
27119 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f  sfer optimizatio
2711a 6e 20 62 65 63 61 75 73 65 20 74 68 65 0a 20 20  n because the.  
2711b 20 20 2a 2a 20 74 68 65 20 72 6f 77 69 64 73 20    ** the rowids 
2711c 6d 69 67 68 74 20 63 68 61 6e 67 65 20 77 68 69  might change whi
2711d 63 68 20 77 69 6c 6c 20 6d 65 73 73 20 75 70 20  ch will mess up 
2711e 69 6e 64 65 78 69 6e 67 2e 0a 20 20 20 20 2a 2a  indexing..    **
2711f 0a 20 20 20 20 2a 2a 20 4f 72 20 69 66 20 74 68  .    ** Or if th
27120 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 68 61  e destination ha
27121 73 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78  s a UNIQUE index
27122 20 61 6e 64 20 69 73 20 6e 6f 74 20 65 6d 70 74   and is not empt
27123 79 2c 0a 20 20 20 20 2a 2a 20 77 65 20 61 6c 73  y,.    ** we als
27124 6f 20 64 69 73 61 6c 6c 6f 77 20 74 68 65 20 74  o disallow the t
27125 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a 61  ransfer optimiza
27126 74 69 6f 6e 20 62 65 63 61 75 73 65 20 77 65 20  tion because we 
27127 63 61 6e 6e 6f 74 0a 20 20 20 20 2a 2a 20 69 6e  cannot.    ** in
27128 73 75 72 65 20 74 68 61 74 20 61 6c 6c 20 65 6e  sure that all en
27129 74 72 69 65 73 20 69 6e 20 74 68 65 20 75 6e 69  tries in the uni
2712a 6f 6e 20 6f 66 20 44 45 53 54 20 61 6e 64 20 53  on of DEST and S
2712b 52 43 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 2a  RC will be.    *
2712c 2a 20 75 6e 69 71 75 65 2e 0a 20 20 20 20 2a 2f  * unique..    */
2712d 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c  .    addr1 = sql
2712e 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2712f 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 44 65  , OP_Rewind, iDe
27130 73 74 2c 20 30 29 3b 0a 20 20 20 20 65 6d 70 74  st, 0);.    empt
27131 79 44 65 73 74 54 65 73 74 20 3d 20 73 71 6c 69  yDestTest = sqli
27132 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27133 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30 29 3b   OP_Goto, 0, 0);
27134 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
27135 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
27136 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  1);.  }else{.   
27137 20 65 6d 70 74 79 44 65 73 74 54 65 73 74 20 3d   emptyDestTest =
27138 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   0;.  }.  sqlite
27139 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73  3OpenTable(pPars
2713a 65 2c 20 69 53 72 63 2c 20 69 44 62 53 72 63 2c  e, iSrc, iDbSrc,
2713b 20 70 53 72 63 2c 20 4f 50 5f 4f 70 65 6e 52 65   pSrc, OP_OpenRe
2713c 61 64 29 3b 0a 20 20 65 6d 70 74 79 53 72 63 54  ad);.  emptySrcT
2713d 65 73 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62  est = sqlite3Vdb
2713e 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
2713f 77 69 6e 64 2c 20 69 53 72 63 2c 20 30 29 3b 0a  wind, iSrc, 0);.
27140 20 20 72 65 67 44 61 74 61 20 3d 20 73 71 6c 69    regData = sqli
27141 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
27142 61 72 73 65 29 3b 0a 20 20 72 65 67 52 6f 77 69  arse);.  regRowi
27143 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  d = sqlite3GetTe
27144 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
27145 20 69 66 28 20 70 44 65 73 74 2d 3e 69 50 4b 65   if( pDest->iPKe
27146 79 3e 3d 30 20 29 7b 0a 20 20 20 20 61 64 64 72  y>=0 ){.    addr
27147 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
27148 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69  ddOp2(v, OP_Rowi
27149 64 2c 20 69 53 72 63 2c 20 72 65 67 52 6f 77 69  d, iSrc, regRowi
2714a 64 29 3b 0a 20 20 20 20 61 64 64 72 32 20 3d 20  d);.    addr2 = 
2714b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2714c 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74  3(v, OP_NotExist
2714d 73 2c 20 69 44 65 73 74 2c 20 30 2c 20 72 65 67  s, iDest, 0, reg
2714e 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69  Rowid);.    sqli
2714f 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e  te3HaltConstrain
27150 74 28 0a 20 20 20 20 20 20 20 20 70 50 61 72 73  t(.        pPars
27151 65 2c 20 6f 6e 45 72 72 6f 72 2c 20 22 50 52 49  e, onError, "PRI
27152 4d 41 52 59 20 4b 45 59 20 6d 75 73 74 20 62 65  MARY KEY must be
27153 20 75 6e 69 71 75 65 22 2c 20 50 34 5f 53 54 41   unique", P4_STA
27154 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  TIC);.    sqlite
27155 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
27156 20 61 64 64 72 32 29 3b 0a 20 20 20 20 61 75 74   addr2);.    aut
27157 6f 49 6e 63 53 74 65 70 28 70 50 61 72 73 65 2c  oIncStep(pParse,
27158 20 72 65 67 41 75 74 6f 69 6e 63 2c 20 72 65 67   regAutoinc, reg
27159 52 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73 65 20  Rowid);.  }else 
2715a 69 66 28 20 70 44 65 73 74 2d 3e 70 49 6e 64 65  if( pDest->pInde
2715b 78 3d 3d 30 20 29 7b 0a 20 20 20 20 61 64 64 72  x==0 ){.    addr
2715c 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
2715d 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52  ddOp2(v, OP_NewR
2715e 6f 77 69 64 2c 20 69 44 65 73 74 2c 20 72 65 67  owid, iDest, reg
2715f 52 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73 65 7b  Rowid);.  }else{
27160 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c  .    addr1 = sql
27161 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
27162 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 53 72 63  , OP_Rowid, iSrc
27163 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
27164 20 61 73 73 65 72 74 28 20 28 70 44 65 73 74 2d   assert( (pDest-
27165 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41  >tabFlags & TF_A
27166 75 74 6f 69 6e 63 72 65 6d 65 6e 74 29 3d 3d 30  utoincrement)==0
27167 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   );.  }.  sqlite
27168 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
27169 50 5f 52 6f 77 44 61 74 61 2c 20 69 53 72 63 2c  P_RowData, iSrc,
2716a 20 72 65 67 44 61 74 61 29 3b 0a 20 20 73 71 6c   regData);.  sql
2716b 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2716c 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69 44 65  , OP_Insert, iDe
2716d 73 74 2c 20 72 65 67 44 61 74 61 2c 20 72 65 67  st, regData, reg
2716e 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c 69 74 65  Rowid);.  sqlite
2716f 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
27170 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 7c   OPFLAG_NCHANGE|
27171 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44  OPFLAG_LASTROWID
27172 7c 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b  |OPFLAG_APPEND);
27173 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
27174 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70 44  angeP4(v, -1, pD
27175 65 73 74 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a  est->zName, 0);.
27176 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27177 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  Op2(v, OP_Next, 
27178 69 53 72 63 2c 20 61 64 64 72 31 29 3b 0a 20 20  iSrc, addr1);.  
27179 66 6f 72 28 70 44 65 73 74 49 64 78 3d 70 44 65  for(pDestIdx=pDe
2717a 73 74 2d 3e 70 49 6e 64 65 78 3b 20 70 44 65 73  st->pIndex; pDes
2717b 74 49 64 78 3b 20 70 44 65 73 74 49 64 78 3d 70  tIdx; pDestIdx=p
2717c 44 65 73 74 49 64 78 2d 3e 70 4e 65 78 74 29 7b  DestIdx->pNext){
2717d 0a 20 20 20 20 66 6f 72 28 70 53 72 63 49 64 78  .    for(pSrcIdx
2717e 3d 70 53 72 63 2d 3e 70 49 6e 64 65 78 3b 20 41  =pSrc->pIndex; A
2717f 4c 57 41 59 53 28 70 53 72 63 49 64 78 29 3b 20  LWAYS(pSrcIdx); 
27180 70 53 72 63 49 64 78 3d 70 53 72 63 49 64 78 2d  pSrcIdx=pSrcIdx-
27181 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69  >pNext){.      i
27182 66 28 20 78 66 65 72 43 6f 6d 70 61 74 69 62 6c  f( xferCompatibl
27183 65 49 6e 64 65 78 28 70 44 65 73 74 49 64 78 2c  eIndex(pDestIdx,
27184 20 70 53 72 63 49 64 78 29 20 29 20 62 72 65 61   pSrcIdx) ) brea
27185 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  k;.    }.    ass
27186 65 72 74 28 20 70 53 72 63 49 64 78 20 29 3b 0a  ert( pSrcIdx );.
27187 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27188 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp2(v, OP_Clos
27189 65 2c 20 69 53 72 63 2c 20 30 29 3b 0a 20 20 20  e, iSrc, 0);.   
2718a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2718b 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p2(v, OP_Close, 
2718c 69 44 65 73 74 2c 20 30 29 3b 0a 20 20 20 20 70  iDest, 0);.    p
2718d 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64  Key = sqlite3Ind
2718e 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65  exKeyinfo(pParse
2718f 2c 20 70 53 72 63 49 64 78 29 3b 0a 20 20 20 20  , pSrcIdx);.    
27190 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27191 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64  4(v, OP_OpenRead
27192 2c 20 69 53 72 63 2c 20 70 53 72 63 49 64 78 2d  , iSrc, pSrcIdx-
27193 3e 74 6e 75 6d 2c 20 69 44 62 53 72 63 2c 0a 20  >tnum, iDbSrc,. 
27194 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27195 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79       (char*)pKey
27196 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e  , P4_KEYINFO_HAN
27197 44 4f 46 46 29 3b 0a 20 20 20 20 56 64 62 65 43  DOFF);.    VdbeC
27198 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c  omment((v, "%s",
27199 20 70 53 72 63 49 64 78 2d 3e 7a 4e 61 6d 65 29   pSrcIdx->zName)
2719a 29 3b 0a 20 20 20 20 70 4b 65 79 20 3d 20 73 71  );.    pKey = sq
2719b 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66  lite3IndexKeyinf
2719c 6f 28 70 50 61 72 73 65 2c 20 70 44 65 73 74 49  o(pParse, pDestI
2719d 64 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  dx);.    sqlite3
2719e 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2719f 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 44 65 73  _OpenWrite, iDes
271a0 74 2c 20 70 44 65 73 74 49 64 78 2d 3e 74 6e 75  t, pDestIdx->tnu
271a1 6d 2c 20 69 44 62 44 65 73 74 2c 0a 20 20 20 20  m, iDbDest,.    
271a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
271a3 20 20 28 63 68 61 72 2a 29 70 4b 65 79 2c 20 50    (char*)pKey, P
271a4 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46  4_KEYINFO_HANDOF
271a5 46 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d  F);.    VdbeComm
271a6 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70 44  ent((v, "%s", pD
271a7 65 73 74 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b  estIdx->zName));
271a8 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c  .    addr1 = sql
271a9 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
271aa 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 53 72  , OP_Rewind, iSr
271ab 63 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  c, 0);.    sqlit
271ac 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
271ad 4f 50 5f 52 6f 77 4b 65 79 2c 20 69 53 72 63 2c  OP_RowKey, iSrc,
271ae 20 72 65 67 44 61 74 61 29 3b 0a 20 20 20 20 73   regData);.    s
271af 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
271b0 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74  (v, OP_IdxInsert
271b1 2c 20 69 44 65 73 74 2c 20 72 65 67 44 61 74 61  , iDest, regData
271b2 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 1);.    sqlite
271b3 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
271b4 50 5f 4e 65 78 74 2c 20 69 53 72 63 2c 20 61 64  P_Next, iSrc, ad
271b5 64 72 31 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69  dr1+1);.    sqli
271b6 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
271b7 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 7d 0a 20  v, addr1);.  }. 
271b8 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
271b9 48 65 72 65 28 76 2c 20 65 6d 70 74 79 53 72 63  Here(v, emptySrc
271ba 54 65 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  Test);.  sqlite3
271bb 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
271bc 50 61 72 73 65 2c 20 72 65 67 52 6f 77 69 64 29  Parse, regRowid)
271bd 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61  ;.  sqlite3Relea
271be 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
271bf 2c 20 72 65 67 44 61 74 61 29 3b 0a 20 20 73 71  , regData);.  sq
271c0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
271c1 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 53 72  v, OP_Close, iSr
271c2 63 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  c, 0);.  sqlite3
271c3 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
271c4 5f 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c 20 30  _Close, iDest, 0
271c5 29 3b 0a 20 20 69 66 28 20 65 6d 70 74 79 44 65  );.  if( emptyDe
271c6 73 74 54 65 73 74 20 29 7b 0a 20 20 20 20 73 71  stTest ){.    sq
271c7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
271c8 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49  v, OP_Halt, SQLI
271c9 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 73  TE_OK, 0);.    s
271ca 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
271cb 72 65 28 76 2c 20 65 6d 70 74 79 44 65 73 74 54  re(v, emptyDestT
271cc 65 73 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  est);.    sqlite
271cd 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
271ce 50 5f 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c 20  P_Close, iDest, 
271cf 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  0);.    return 0
271d0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
271d1 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 23  eturn 1;.  }.}.#
271d2 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
271d3 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54 20 2a 2f  OMIT_XFER_OPT */
271d4 0a 0a 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 22  ../* Make sure "
271d5 69 73 56 69 65 77 22 20 67 65 74 73 20 75 6e 64  isView" gets und
271d6 65 66 69 6e 65 64 20 69 6e 20 63 61 73 65 20 74  efined in case t
271d7 68 69 73 20 66 69 6c 65 20 62 65 63 6f 6d 65 73  his file becomes
271d8 20 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20   part of.** the 
271d9 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20 2d 20 73  amalgamation - s
271da 6f 20 74 68 61 74 20 73 75 62 73 65 71 75 65 6e  o that subsequen
271db 74 20 66 69 6c 65 73 20 64 6f 20 6e 6f 74 20 73  t files do not s
271dc 65 65 20 69 73 56 69 65 77 20 61 73 20 61 0a 2a  ee isView as a.*
271dd 2a 20 6d 61 63 72 6f 2e 20 2a 2f 0a 23 75 6e 64  * macro. */.#und
271de 65 66 20 69 73 56 69 65 77 0a 0a 2f 2a 2a 2a 2a  ef isView../****
271df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
271e0 66 20 69 6e 73 65 72 74 2e 63 20 2a 2a 2a 2a 2a  f insert.c *****
271e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
271e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
271e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
271e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
271e5 20 66 69 6c 65 20 6c 65 67 61 63 79 2e 63 20 2a   file legacy.c *
271e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
271e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
271e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
271e9 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
271ea 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
271eb 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
271ec 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
271ed 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
271ee 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
271ef 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
271f0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
271f1 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
271f2 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
271f3 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
271f4 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
271f5 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
271f6 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
271f7 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
271f8 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
271f9 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
271fa 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
271fb 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
271fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
271fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
271fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
271ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27200 2a 0a 2a 2a 20 4d 61 69 6e 20 66 69 6c 65 20 66  *.** Main file f
27201 6f 72 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69  or the SQLite li
27202 62 72 61 72 79 2e 20 20 54 68 65 20 72 6f 75 74  brary.  The rout
27203 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c  ines in this fil
27204 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 74  e.** implement t
27205 68 65 20 70 72 6f 67 72 61 6d 6d 65 72 20 69 6e  he programmer in
27206 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 6c  terface to the l
27207 69 62 72 61 72 79 2e 20 20 52 6f 75 74 69 6e 65  ibrary.  Routine
27208 73 20 69 6e 0a 2a 2a 20 6f 74 68 65 72 20 66 69  s in.** other fi
27209 6c 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65  les are for inte
2720a 72 6e 61 6c 20 75 73 65 20 62 79 20 53 51 4c 69  rnal use by SQLi
2720b 74 65 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f  te and should no
2720c 74 20 62 65 0a 2a 2a 20 61 63 63 65 73 73 65 64  t be.** accessed
2720d 20 62 79 20 75 73 65 72 73 20 6f 66 20 74 68 65   by users of the
2720e 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20   library..**.** 
2720f 24 49 64 3a 20 6c 65 67 61 63 79 2e 63 2c 76 20  $Id: legacy.c,v 
27210 31 2e 33 35 20 32 30 30 39 2f 30 38 2f 30 37 20  1.35 2009/08/07 
27211 31 36 3a 35 36 3a 30 30 20 64 61 6e 69 65 6c 6b  16:56:00 danielk
27212 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a  1977 Exp $.*/...
27213 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 53 51  /*.** Execute SQ
27214 4c 20 63 6f 64 65 2e 20 20 52 65 74 75 72 6e 20  L code.  Return 
27215 6f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54  one of the SQLIT
27216 45 5f 20 73 75 63 63 65 73 73 2f 66 61 69 6c 75  E_ success/failu
27217 72 65 0a 2a 2a 20 63 6f 64 65 73 2e 20 20 41 6c  re.** codes.  Al
27218 73 6f 20 77 72 69 74 65 20 61 6e 20 65 72 72 6f  so write an erro
27219 72 20 6d 65 73 73 61 67 65 20 69 6e 74 6f 20 6d  r message into m
2721a 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
2721b 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20  rom.** malloc() 
2721c 61 6e 64 20 6d 61 6b 65 20 2a 70 7a 45 72 72 4d  and make *pzErrM
2721d 73 67 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74  sg point to that
2721e 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20   message..**.** 
2721f 49 66 20 74 68 65 20 53 51 4c 20 69 73 20 61 20  If the SQL is a 
27220 71 75 65 72 79 2c 20 74 68 65 6e 20 66 6f 72 20  query, then for 
27221 65 61 63 68 20 72 6f 77 20 69 6e 20 74 68 65 20  each row in the 
27222 71 75 65 72 79 20 72 65 73 75 6c 74 0a 2a 2a 20  query result.** 
27223 74 68 65 20 78 43 61 6c 6c 62 61 63 6b 28 29 20  the xCallback() 
27224 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
27225 65 64 2e 20 20 70 41 72 67 20 62 65 63 6f 6d 65  ed.  pArg become
27226 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61  s the first.** a
27227 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 61 6c 6c  rgument to xCall
27228 62 61 63 6b 28 29 2e 20 20 49 66 20 78 43 61 6c  back().  If xCal
27229 6c 62 61 63 6b 3d 4e 55 4c 4c 20 74 68 65 6e 20  lback=NULL then 
2722a 6e 6f 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69  no callback.** i
2722b 73 20 69 6e 76 6f 6b 65 64 2c 20 65 76 65 6e 20  s invoked, even 
2722c 66 6f 72 20 71 75 65 72 69 65 73 2e 0a 2a 2f 0a  for queries..*/.
2722d 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2722e 71 6c 69 74 65 33 5f 65 78 65 63 28 0a 20 20 73  qlite3_exec(.  s
2722f 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
27230 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
27231 65 20 64 61 74 61 62 61 73 65 20 6f 6e 20 77 68  e database on wh
27232 69 63 68 20 74 68 65 20 53 51 4c 20 65 78 65 63  ich the SQL exec
27233 75 74 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  utes */.  const 
27234 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20  char *zSql,     
27235 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 51 4c        /* The SQL
27236 20 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 20   to be executed 
27237 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 63 61 6c  */.  sqlite3_cal
27238 6c 62 61 63 6b 20 78 43 61 6c 6c 62 61 63 6b 2c  lback xCallback,
27239 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 69 73 20   /* Invoke this 
2723a 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65  callback routine
2723b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 67   */.  void *pArg
2723c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2723d 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d    /* First argum
2723e 65 6e 74 20 74 6f 20 78 43 61 6c 6c 62 61 63 6b  ent to xCallback
2723f 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70  () */.  char **p
27240 7a 45 72 72 4d 73 67 20 20 20 20 20 20 20 20 20  zErrMsg         
27241 20 20 20 20 2f 2a 20 57 72 69 74 65 20 65 72 72      /* Write err
27242 6f 72 20 6d 65 73 73 61 67 65 73 20 68 65 72 65  or messages here
27243 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20   */.){.  int rc 
27244 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
27245 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
27246 6f 64 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ode */.  const c
27247 68 61 72 20 2a 7a 4c 65 66 74 6f 76 65 72 3b 20  har *zLeftover; 
27248 20 20 20 20 20 2f 2a 20 54 61 69 6c 20 6f 66 20       /* Tail of 
27249 75 6e 70 72 6f 63 65 73 73 65 64 20 53 51 4c 20  unprocessed SQL 
2724a 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
2724b 74 20 2a 70 53 74 6d 74 20 3d 20 30 3b 20 20 20  t *pStmt = 0;   
2724c 20 2f 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20   /* The current 
2724d 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  SQL statement */
2724e 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 73  .  char **azCols
2724f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f   = 0;          /
27250 2a 20 4e 61 6d 65 73 20 6f 66 20 72 65 73 75 6c  * Names of resul
27251 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69  t columns */.  i
27252 6e 74 20 6e 52 65 74 72 79 20 3d 20 30 3b 20 20  nt nRetry = 0;  
27253 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
27254 6d 62 65 72 20 6f 66 20 72 65 74 72 79 20 61 74  mber of retry at
27255 74 65 6d 70 74 73 20 2a 2f 0a 20 20 69 6e 74 20  tempts */.  int 
27256 63 61 6c 6c 62 61 63 6b 49 73 49 6e 69 74 3b 20  callbackIsInit; 
27257 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
27258 69 66 20 63 61 6c 6c 62 61 63 6b 20 64 61 74 61  if callback data
27259 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   is initialized 
2725a 2a 2f 0a 0a 20 20 69 66 28 20 7a 53 71 6c 3d 3d  */..  if( zSql==
2725b 30 20 29 20 7a 53 71 6c 20 3d 20 22 22 3b 0a 0a  0 ) zSql = "";..
2725c 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2725d 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
2725e 3b 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72  ;.  sqlite3Error
2725f 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20  (db, SQLITE_OK, 
27260 30 29 3b 0a 20 20 77 68 69 6c 65 28 20 28 72 63  0);.  while( (rc
27261 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28  ==SQLITE_OK || (
27262 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d  rc==SQLITE_SCHEM
27263 41 20 26 26 20 28 2b 2b 6e 52 65 74 72 79 29 3c  A && (++nRetry)<
27264 32 29 29 20 26 26 20 7a 53 71 6c 5b 30 5d 20 29  2)) && zSql[0] )
27265 7b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a  {.    int nCol;.
27266 20 20 20 20 63 68 61 72 20 2a 2a 61 7a 56 61 6c      char **azVal
27267 73 20 3d 20 30 3b 0a 0a 20 20 20 20 70 53 74 6d  s = 0;..    pStm
27268 74 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20  t = 0;.    rc = 
27269 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
2726a 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70  db, zSql, -1, &p
2726b 53 74 6d 74 2c 20 26 7a 4c 65 66 74 6f 76 65 72  Stmt, &zLeftover
2726c 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72  );.    assert( r
2726d 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
2726e 70 53 74 6d 74 3d 3d 30 20 29 3b 0a 20 20 20 20  pStmt==0 );.    
2726f 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
27270 4b 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74 69  K ){.      conti
27271 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  nue;.    }.    i
27272 66 28 20 21 70 53 74 6d 74 20 29 7b 0a 20 20 20  f( !pStmt ){.   
27273 20 20 20 2f 2a 20 74 68 69 73 20 68 61 70 70 65     /* this happe
27274 6e 73 20 66 6f 72 20 61 20 63 6f 6d 6d 65 6e 74  ns for a comment
27275 20 6f 72 20 77 68 69 74 65 2d 73 70 61 63 65 20   or white-space 
27276 2a 2f 0a 20 20 20 20 20 20 7a 53 71 6c 20 3d 20  */.      zSql = 
27277 7a 4c 65 66 74 6f 76 65 72 3b 0a 20 20 20 20 20  zLeftover;.     
27278 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d   continue;.    }
27279 0a 0a 20 20 20 20 63 61 6c 6c 62 61 63 6b 49 73  ..    callbackIs
2727a 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 6e 43  Init = 0;.    nC
2727b 6f 6c 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ol = sqlite3_col
2727c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29  umn_count(pStmt)
2727d 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 31 20  ;..    while( 1 
2727e 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a  ){.      int i;.
2727f 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
27280 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 3b 0a  e3_step(pStmt);.
27281 0a 20 20 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65  .      /* Invoke
27282 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75   the callback fu
27283 6e 63 74 69 6f 6e 20 69 66 20 72 65 71 75 69 72  nction if requir
27284 65 64 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  ed */.      if( 
27285 78 43 61 6c 6c 62 61 63 6b 20 26 26 20 28 53 51  xCallback && (SQ
27286 4c 49 54 45 5f 52 4f 57 3d 3d 72 63 20 7c 7c 20  LITE_ROW==rc || 
27287 0a 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49  .          (SQLI
27288 54 45 5f 44 4f 4e 45 3d 3d 72 63 20 26 26 20 21  TE_DONE==rc && !
27289 63 61 6c 6c 62 61 63 6b 49 73 49 6e 69 74 0a 20  callbackIsInit. 
2728a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2728b 20 20 20 20 20 20 20 20 20 20 26 26 20 64 62 2d            && db-
2728c 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 4e 75  >flags&SQLITE_Nu
2728d 6c 6c 43 61 6c 6c 62 61 63 6b 29 29 20 29 7b 0a  llCallback)) ){.
2728e 20 20 20 20 20 20 20 20 69 66 28 20 21 63 61 6c          if( !cal
2728f 6c 62 61 63 6b 49 73 49 6e 69 74 20 29 7b 0a 20  lbackIsInit ){. 
27290 20 20 20 20 20 20 20 20 20 61 7a 43 6f 6c 73 20           azCols 
27291 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
27292 63 5a 65 72 6f 28 64 62 2c 20 32 2a 6e 43 6f 6c  cZero(db, 2*nCol
27293 2a 73 69 7a 65 6f 66 28 63 6f 6e 73 74 20 63 68  *sizeof(const ch
27294 61 72 2a 29 20 2b 20 31 29 3b 0a 20 20 20 20 20  ar*) + 1);.     
27295 20 20 20 20 20 69 66 28 20 61 7a 43 6f 6c 73 3d       if( azCols=
27296 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
27297 20 20 67 6f 74 6f 20 65 78 65 63 5f 6f 75 74 3b    goto exec_out;
27298 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
27299 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20         for(i=0; 
2729a 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  i<nCol; i++){.  
2729b 20 20 20 20 20 20 20 20 20 20 61 7a 43 6f 6c 73            azCols
2729c 5b 69 5d 20 3d 20 28 63 68 61 72 20 2a 29 73 71  [i] = (char *)sq
2729d 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d  lite3_column_nam
2729e 65 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20 20  e(pStmt, i);.   
2729f 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69           /* sqli
272a0 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
272a1 65 28 29 20 69 6e 73 74 61 6c 6c 73 20 63 6f 6c  e() installs col
272a2 75 6d 6e 20 6e 61 6d 65 73 20 61 73 20 55 54 46  umn names as UTF
272a3 38 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  8.            **
272a4 20 73 74 72 69 6e 67 73 20 73 6f 20 74 68 65 72   strings so ther
272a5 65 20 69 73 20 6e 6f 20 77 61 79 20 66 6f 72 20  e is no way for 
272a6 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
272a7 61 6d 65 28 29 20 74 6f 20 66 61 69 6c 2e 20 2a  ame() to fail. *
272a8 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73  /.            as
272a9 73 65 72 74 28 20 61 7a 43 6f 6c 73 5b 69 5d 21  sert( azCols[i]!
272aa 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  =0 );.          
272ab 7d 0a 20 20 20 20 20 20 20 20 20 20 63 61 6c 6c  }.          call
272ac 62 61 63 6b 49 73 49 6e 69 74 20 3d 20 31 3b 0a  backIsInit = 1;.
272ad 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
272ae 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
272af 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 20 20  _ROW ){.        
272b0 20 20 61 7a 56 61 6c 73 20 3d 20 26 61 7a 43 6f    azVals = &azCo
272b1 6c 73 5b 6e 43 6f 6c 5d 3b 0a 20 20 20 20 20 20  ls[nCol];.      
272b2 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
272b3 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
272b4 20 20 20 20 20 20 20 61 7a 56 61 6c 73 5b 69 5d         azVals[i]
272b5 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74   = (char *)sqlit
272b6 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70  e3_column_text(p
272b7 53 74 6d 74 2c 20 69 29 3b 0a 20 20 20 20 20 20  Stmt, i);.      
272b8 20 20 20 20 20 20 69 66 28 20 21 61 7a 56 61 6c        if( !azVal
272b9 73 5b 69 5d 20 26 26 20 73 71 6c 69 74 65 33 5f  s[i] && sqlite3_
272ba 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 70 53 74 6d  column_type(pStm
272bb 74 2c 20 69 29 21 3d 53 51 4c 49 54 45 5f 4e 55  t, i)!=SQLITE_NU
272bc 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  LL ){.          
272bd 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
272be 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
272bf 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 65          goto exe
272c0 63 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 20  c_out;.         
272c1 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d     }.          }
272c2 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
272c3 20 20 20 69 66 28 20 78 43 61 6c 6c 62 61 63 6b     if( xCallback
272c4 28 70 41 72 67 2c 20 6e 43 6f 6c 2c 20 61 7a 56  (pArg, nCol, azV
272c5 61 6c 73 2c 20 61 7a 43 6f 6c 73 29 20 29 7b 0a  als, azCols) ){.
272c6 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
272c7 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 20  QLITE_ABORT;.   
272c8 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
272c9 62 65 46 69 6e 61 6c 69 7a 65 28 28 56 64 62 65  beFinalize((Vdbe
272ca 20 2a 29 70 53 74 6d 74 29 3b 0a 20 20 20 20 20   *)pStmt);.     
272cb 20 20 20 20 20 70 53 74 6d 74 20 3d 20 30 3b 0a       pStmt = 0;.
272cc 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
272cd 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
272ce 45 5f 41 42 4f 52 54 2c 20 30 29 3b 0a 20 20 20  E_ABORT, 0);.   
272cf 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 65 63         goto exec
272d0 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  _out;.        }.
272d1 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69        }..      i
272d2 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f  f( rc!=SQLITE_RO
272d3 57 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  W ){.        rc 
272d4 3d 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e  = sqlite3VdbeFin
272d5 61 6c 69 7a 65 28 28 56 64 62 65 20 2a 29 70 53  alize((Vdbe *)pS
272d6 74 6d 74 29 3b 0a 20 20 20 20 20 20 20 20 70 53  tmt);.        pS
272d7 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  tmt = 0;.       
272d8 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
272d9 53 43 48 45 4d 41 20 29 7b 0a 20 20 20 20 20 20  SCHEMA ){.      
272da 20 20 20 20 6e 52 65 74 72 79 20 3d 20 30 3b 0a      nRetry = 0;.
272db 20 20 20 20 20 20 20 20 20 20 7a 53 71 6c 20 3d            zSql =
272dc 20 7a 4c 65 66 74 6f 76 65 72 3b 0a 20 20 20 20   zLeftover;.    
272dd 20 20 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c        while( sql
272de 69 74 65 33 49 73 73 70 61 63 65 28 7a 53 71 6c  ite3Isspace(zSql
272df 5b 30 5d 29 20 29 20 7a 53 71 6c 2b 2b 3b 0a 20  [0]) ) zSql++;. 
272e0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
272e1 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
272e2 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
272e3 65 33 44 62 46 72 65 65 28 64 62 2c 20 61 7a 43  e3DbFree(db, azC
272e4 6f 6c 73 29 3b 0a 20 20 20 20 61 7a 43 6f 6c 73  ols);.    azCols
272e5 20 3d 20 30 3b 0a 20 20 7d 0a 0a 65 78 65 63 5f   = 0;.  }..exec_
272e6 6f 75 74 3a 0a 20 20 69 66 28 20 70 53 74 6d 74  out:.  if( pStmt
272e7 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 46 69   ) sqlite3VdbeFi
272e8 6e 61 6c 69 7a 65 28 28 56 64 62 65 20 2a 29 70  nalize((Vdbe *)p
272e9 53 74 6d 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  Stmt);.  sqlite3
272ea 44 62 46 72 65 65 28 64 62 2c 20 61 7a 43 6f 6c  DbFree(db, azCol
272eb 73 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  s);..  rc = sqli
272ec 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72  te3ApiExit(db, r
272ed 63 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  c);.  if( rc!=SQ
272ee 4c 49 54 45 5f 4f 4b 20 26 26 20 41 4c 57 41 59  LITE_OK && ALWAY
272ef 53 28 72 63 3d 3d 73 71 6c 69 74 65 33 5f 65 72  S(rc==sqlite3_er
272f0 72 63 6f 64 65 28 64 62 29 29 20 26 26 20 70 7a  rcode(db)) && pz
272f1 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 69 6e  ErrMsg ){.    in
272f2 74 20 6e 45 72 72 4d 73 67 20 3d 20 31 20 2b 20  t nErrMsg = 1 + 
272f3 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
272f4 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64  sqlite3_errmsg(d
272f5 62 29 29 3b 0a 20 20 20 20 2a 70 7a 45 72 72 4d  b));.    *pzErrM
272f6 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  sg = sqlite3Mall
272f7 6f 63 28 6e 45 72 72 4d 73 67 29 3b 0a 20 20 20  oc(nErrMsg);.   
272f8 20 69 66 28 20 2a 70 7a 45 72 72 4d 73 67 20 29   if( *pzErrMsg )
272f9 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 2a  {.      memcpy(*
272fa 70 7a 45 72 72 4d 73 67 2c 20 73 71 6c 69 74 65  pzErrMsg, sqlite
272fb 33 5f 65 72 72 6d 73 67 28 64 62 29 2c 20 6e 45  3_errmsg(db), nE
272fc 72 72 4d 73 67 29 3b 0a 20 20 20 20 7d 65 6c 73  rrMsg);.    }els
272fd 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  e{.      rc = SQ
272fe 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
272ff 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
27300 62 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c  b, SQLITE_NOMEM,
27301 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c   0);.    }.  }el
27302 73 65 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20  se if( pzErrMsg 
27303 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72 4d 73 67  ){.    *pzErrMsg
27304 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 61 73 73   = 0;.  }..  ass
27305 65 72 74 28 20 28 72 63 26 64 62 2d 3e 65 72 72  ert( (rc&db->err
27306 4d 61 73 6b 29 3d 3d 72 63 20 29 3b 0a 20 20 73  Mask)==rc );.  s
27307 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
27308 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
27309 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
2730a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
2730b 6e 64 20 6f 66 20 6c 65 67 61 63 79 2e 63 20 2a  nd of legacy.c *
2730c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2730d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2730e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
2730f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
27310 65 67 69 6e 20 66 69 6c 65 20 6c 6f 61 64 65 78  egin file loadex
27311 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.c ************
27312 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27313 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
27314 2a 0a 2a 2a 20 32 30 30 36 20 4a 75 6e 65 20 37  *.** 2006 June 7
27315 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
27316 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
27317 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
27318 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
27319 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
2731a 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
2731b 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
2731c 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
2731d 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
2731e 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
2731f 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
27320 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
27321 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
27322 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
27323 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
27324 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
27325 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
27326 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
27327 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27328 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27329 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2732a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
2732b 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
2732c 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20  tains code used 
2732d 74 6f 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 6c  to dynamically l
2732e 6f 61 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 69  oad extensions i
2732f 6e 74 6f 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74  nto.** the SQLit
27330 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a  e library..**.**
27331 20 24 49 64 3a 20 6c 6f 61 64 65 78 74 2e 63 2c   $Id: loadext.c,
27332 76 20 31 2e 36 30 20 32 30 30 39 2f 30 36 2f 30  v 1.60 2009/06/0
27333 33 20 30 31 3a 32 34 3a 35 34 20 64 72 68 20 45  3 01:24:54 drh E
27334 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66  xp $.*/..#ifndef
27335 20 53 51 4c 49 54 45 5f 43 4f 52 45 0a 20 20 23   SQLITE_CORE.  #
27336 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
27337 52 45 20 31 20 20 2f 2a 20 44 69 73 61 62 6c 65  RE 1  /* Disable
27338 20 74 68 65 20 41 50 49 20 72 65 64 65 66 69 6e   the API redefin
27339 69 74 69 6f 6e 20 69 6e 20 73 71 6c 69 74 65 33  ition in sqlite3
2733a 65 78 74 2e 68 20 2a 2f 0a 23 65 6e 64 69 66 0a  ext.h */.#endif.
2733b 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2733c 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 33 65  Include sqlite3e
2733d 78 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  xt.h in the midd
2733e 6c 65 20 6f 66 20 6c 6f 61 64 65 78 74 2e 63 20  le of loadext.c 
2733f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
27340 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
27341 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74  Begin file sqlit
27342 65 33 65 78 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  e3ext.h ********
27343 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27344 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
27345 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a 75 6e 65 20  /*.** 2006 June 
27346 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  7.**.** The auth
27347 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
27348 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
27349 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
2734a 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
2734b 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
2734c 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
2734d 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
2734e 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
2734f 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
27350 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
27351 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
27352 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
27353 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
27354 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
27355 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
27356 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
27357 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
27358 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27359 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2735a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2735b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2735c 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20  .** This header 
2735d 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65  file defines the
2735e 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63   SQLite interfac
2735f 65 20 66 6f 72 20 75 73 65 20 62 79 0a 2a 2a 20  e for use by.** 
27360 73 68 61 72 65 64 20 6c 69 62 72 61 72 69 65 73  shared libraries
27361 20 74 68 61 74 20 77 61 6e 74 20 74 6f 20 62 65   that want to be
27362 20 69 6d 70 6f 72 74 65 64 20 61 73 20 65 78 74   imported as ext
27363 65 6e 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a 20  ensions into.** 
27364 61 6e 20 53 51 4c 69 74 65 20 69 6e 73 74 61 6e  an SQLite instan
27365 63 65 2e 20 20 53 68 61 72 65 64 20 6c 69 62 72  ce.  Shared libr
27366 61 72 69 65 73 20 74 68 61 74 20 69 6e 74 65 6e  aries that inten
27367 64 20 74 6f 20 62 65 20 6c 6f 61 64 65 64 0a 2a  d to be loaded.*
27368 2a 20 61 73 20 65 78 74 65 6e 73 69 6f 6e 73 20  * as extensions 
27369 62 79 20 53 51 4c 69 74 65 20 73 68 6f 75 6c 64  by SQLite should
2736a 20 23 69 6e 63 6c 75 64 65 20 74 68 69 73 20 66   #include this f
2736b 69 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20 0a  ile instead of .
2736c 2a 2a 20 73 71 6c 69 74 65 33 2e 68 2e 0a 2a 2a  ** sqlite3.h..**
2736d 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 73 71  .** @(#) $Id: sq
2736e 6c 69 74 65 33 65 78 74 2e 68 2c 76 20 31 2e 32  lite3ext.h,v 1.2
2736f 35 20 32 30 30 38 2f 31 30 2f 31 32 20 30 30 3a  5 2008/10/12 00:
27370 32 37 3a 35 34 20 73 68 61 6e 65 20 45 78 70 20  27:54 shane Exp 
27371 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51  $.*/.#ifndef _SQ
27372 4c 49 54 45 33 45 58 54 5f 48 5f 0a 23 64 65 66  LITE3EXT_H_.#def
27373 69 6e 65 20 5f 53 51 4c 49 54 45 33 45 58 54 5f  ine _SQLITE3EXT_
27374 48 5f 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75  H_..typedef stru
27375 63 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  ct sqlite3_api_r
27376 6f 75 74 69 6e 65 73 20 73 71 6c 69 74 65 33 5f  outines sqlite3_
27377 61 70 69 5f 72 6f 75 74 69 6e 65 73 3b 0a 0a 2f  api_routines;../
27378 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
27379 6e 67 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c  ng structure hol
2737a 64 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 61  ds pointers to a
2737b 6c 6c 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65  ll of the SQLite
2737c 20 41 50 49 0a 2a 2a 20 72 6f 75 74 69 6e 65 73   API.** routines
2737d 2e 0a 2a 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a  ..**.** WARNING:
2737e 20 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61    In order to ma
2737f 69 6e 74 61 69 6e 20 62 61 63 6b 77 61 72 64 73  intain backwards
27380 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2c 20   compatibility, 
27381 61 64 64 20 6e 65 77 0a 2a 2a 20 69 6e 74 65 72  add new.** inter
27382 66 61 63 65 73 20 74 6f 20 74 68 65 20 65 6e 64  faces to the end
27383 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
27384 72 65 20 6f 6e 6c 79 2e 20 20 49 66 20 79 6f 75  re only.  If you
27385 20 69 6e 73 65 72 74 20 6e 65 77 0a 2a 2a 20 69   insert new.** i
27386 6e 74 65 72 66 61 63 65 73 20 69 6e 20 74 68 65  nterfaces in the
27387 20 6d 69 64 64 6c 65 20 6f 66 20 74 68 69 73 20   middle of this 
27388 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 6e 20  structure, then 
27389 6f 6c 64 65 72 20 64 69 66 66 65 72 65 6e 74 0a  older different.
2738a 2a 2a 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53  ** versions of S
2738b 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 62  QLite will not b
2738c 65 20 61 62 6c 65 20 74 6f 20 6c 6f 61 64 20 65  e able to load e
2738d 61 63 68 20 6f 74 68 65 72 73 27 20 73 68 61 72  ach others' shar
2738e 65 64 0a 2a 2a 20 6c 69 62 72 61 72 69 65 73 21  ed.** libraries!
2738f 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74  .*/.struct sqlit
27390 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20  e3_api_routines 
27391 7b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 61 67 67  {.  void * (*agg
27392 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 29 28  regate_context)(
27393 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
27394 2c 69 6e 74 20 6e 42 79 74 65 73 29 3b 0a 20 20  ,int nBytes);.  
27395 69 6e 74 20 20 28 2a 61 67 67 72 65 67 61 74 65  int  (*aggregate
27396 5f 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f  _count)(sqlite3_
27397 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 69 6e 74  context*);.  int
27398 20 20 28 2a 62 69 6e 64 5f 62 6c 6f 62 29 28 73    (*bind_blob)(s
27399 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
2739a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
2739b 20 6e 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a   n,void(*)(void*
2739c 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e  ));.  int  (*bin
2739d 64 5f 64 6f 75 62 6c 65 29 28 73 71 6c 69 74 65  d_double)(sqlite
2739e 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 64 6f 75 62  3_stmt*,int,doub
2739f 6c 65 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69  le);.  int  (*bi
273a0 6e 64 5f 69 6e 74 29 28 73 71 6c 69 74 65 33 5f  nd_int)(sqlite3_
273a1 73 74 6d 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a  stmt*,int,int);.
273a2 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 69 6e    int  (*bind_in
273a3 74 36 34 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  t64)(sqlite3_stm
273a4 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 5f 69 6e  t*,int,sqlite_in
273a5 74 36 34 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62  t64);.  int  (*b
273a6 69 6e 64 5f 6e 75 6c 6c 29 28 73 71 6c 69 74 65  ind_null)(sqlite
273a7 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20  3_stmt*,int);.  
273a8 69 6e 74 20 20 28 2a 62 69 6e 64 5f 70 61 72 61  int  (*bind_para
273a9 6d 65 74 65 72 5f 63 6f 75 6e 74 29 28 73 71 6c  meter_count)(sql
273aa 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 69  ite3_stmt*);.  i
273ab 6e 74 20 20 28 2a 62 69 6e 64 5f 70 61 72 61 6d  nt  (*bind_param
273ac 65 74 65 72 5f 69 6e 64 65 78 29 28 73 71 6c 69  eter_index)(sqli
273ad 74 65 33 5f 73 74 6d 74 2a 2c 63 6f 6e 73 74 20  te3_stmt*,const 
273ae 63 68 61 72 2a 7a 4e 61 6d 65 29 3b 0a 20 20 63  char*zName);.  c
273af 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 62 69  onst char * (*bi
273b0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d  nd_parameter_nam
273b1 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  e)(sqlite3_stmt*
273b2 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a  ,int);.  int  (*
273b3 62 69 6e 64 5f 74 65 78 74 29 28 73 71 6c 69 74  bind_text)(sqlit
273b4 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e  e3_stmt*,int,con
273b5 73 74 20 63 68 61 72 2a 2c 69 6e 74 20 6e 2c 76  st char*,int n,v
273b6 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
273b7 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 74 65    int  (*bind_te
273b8 78 74 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74  xt16)(sqlite3_st
273b9 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  mt*,int,const vo
273ba 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28  id*,int,void(*)(
273bb 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20 20  void*));.  int  
273bc 28 2a 62 69 6e 64 5f 76 61 6c 75 65 29 28 73 71  (*bind_value)(sq
273bd 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c  lite3_stmt*,int,
273be 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 61  const sqlite3_va
273bf 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  lue*);.  int  (*
273c0 62 75 73 79 5f 68 61 6e 64 6c 65 72 29 28 73 71  busy_handler)(sq
273c1 6c 69 74 65 33 2a 2c 69 6e 74 28 2a 29 28 76 6f  lite3*,int(*)(vo
273c2 69 64 2a 2c 69 6e 74 29 2c 76 6f 69 64 2a 29 3b  id*,int),void*);
273c3 0a 20 20 69 6e 74 20 20 28 2a 62 75 73 79 5f 74  .  int  (*busy_t
273c4 69 6d 65 6f 75 74 29 28 73 71 6c 69 74 65 33 2a  imeout)(sqlite3*
273c5 2c 69 6e 74 20 6d 73 29 3b 0a 20 20 69 6e 74 20  ,int ms);.  int 
273c6 20 28 2a 63 68 61 6e 67 65 73 29 28 73 71 6c 69   (*changes)(sqli
273c7 74 65 33 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  te3*);.  int  (*
273c8 63 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 2a 29  close)(sqlite3*)
273c9 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c 6c 61  ;.  int  (*colla
273ca 74 69 6f 6e 5f 6e 65 65 64 65 64 29 28 73 71 6c  tion_needed)(sql
273cb 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 76 6f 69 64  ite3*,void*,void
273cc 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65  (*)(void*,sqlite
273cd 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c  3*,int eTextRep,
273ce 63 6f 6e 73 74 20 63 68 61 72 2a 29 29 3b 0a 20  const char*));. 
273cf 20 69 6e 74 20 20 28 2a 63 6f 6c 6c 61 74 69 6f   int  (*collatio
273d0 6e 5f 6e 65 65 64 65 64 31 36 29 28 73 71 6c 69  n_needed16)(sqli
273d1 74 65 33 2a 2c 76 6f 69 64 2a 2c 76 6f 69 64 28  te3*,void*,void(
273d2 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33  *)(void*,sqlite3
273d3 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63  *,int eTextRep,c
273d4 6f 6e 73 74 20 76 6f 69 64 2a 29 29 3b 0a 20 20  onst void*));.  
273d5 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63  const void * (*c
273d6 6f 6c 75 6d 6e 5f 62 6c 6f 62 29 28 73 71 6c 69  olumn_blob)(sqli
273d7 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43  te3_stmt*,int iC
273d8 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f  ol);.  int  (*co
273d9 6c 75 6d 6e 5f 62 79 74 65 73 29 28 73 71 6c 69  lumn_bytes)(sqli
273da 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43  te3_stmt*,int iC
273db 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f  ol);.  int  (*co
273dc 6c 75 6d 6e 5f 62 79 74 65 73 31 36 29 28 73 71  lumn_bytes16)(sq
273dd 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20  lite3_stmt*,int 
273de 69 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a  iCol);.  int  (*
273df 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 29 28 73 71  column_count)(sq
273e0 6c 69 74 65 33 5f 73 74 6d 74 2a 70 53 74 6d 74  lite3_stmt*pStmt
273e1 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  );.  const char 
273e2 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  * (*column_datab
273e3 61 73 65 5f 6e 61 6d 65 29 28 73 71 6c 69 74 65  ase_name)(sqlite
273e4 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20  3_stmt*,int);.  
273e5 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63  const void * (*c
273e6 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e  olumn_database_n
273e7 61 6d 65 31 36 29 28 73 71 6c 69 74 65 33 5f 73  ame16)(sqlite3_s
273e8 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e  tmt*,int);.  con
273e9 73 74 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75  st char * (*colu
273ea 6d 6e 5f 64 65 63 6c 74 79 70 65 29 28 73 71 6c  mn_decltype)(sql
273eb 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69  ite3_stmt*,int i
273ec 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  );.  const void 
273ed 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74  * (*column_declt
273ee 79 70 65 31 36 29 28 73 71 6c 69 74 65 33 5f 73  ype16)(sqlite3_s
273ef 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 64 6f 75  tmt*,int);.  dou
273f0 62 6c 65 20 20 28 2a 63 6f 6c 75 6d 6e 5f 64 6f  ble  (*column_do
273f1 75 62 6c 65 29 28 73 71 6c 69 74 65 33 5f 73 74  uble)(sqlite3_st
273f2 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20  mt*,int iCol);. 
273f3 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 69   int  (*column_i
273f4 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  nt)(sqlite3_stmt
273f5 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 73  *,int iCol);.  s
273f6 71 6c 69 74 65 5f 69 6e 74 36 34 20 20 28 2a 63  qlite_int64  (*c
273f7 6f 6c 75 6d 6e 5f 69 6e 74 36 34 29 28 73 71 6c  olumn_int64)(sql
273f8 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69  ite3_stmt*,int i
273f9 43 6f 6c 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68  Col);.  const ch
273fa 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6e 61  ar * (*column_na
273fb 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  me)(sqlite3_stmt
273fc 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20  *,int);.  const 
273fd 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f  void * (*column_
273fe 6e 61 6d 65 31 36 29 28 73 71 6c 69 74 65 33 5f  name16)(sqlite3_
273ff 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f  stmt*,int);.  co
27400 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c  nst char * (*col
27401 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 29  umn_origin_name)
27402 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
27403 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69  nt);.  const voi
27404 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6f 72 69  d * (*column_ori
27405 67 69 6e 5f 6e 61 6d 65 31 36 29 28 73 71 6c 69  gin_name16)(sqli
27406 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
27407 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28    const char * (
27408 2a 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  *column_table_na
27409 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  me)(sqlite3_stmt
2740a 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20  *,int);.  const 
2740b 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f  void * (*column_
2740c 74 61 62 6c 65 5f 6e 61 6d 65 31 36 29 28 73 71  table_name16)(sq
2740d 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29  lite3_stmt*,int)
2740e 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ;.  const unsign
2740f 65 64 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75  ed char * (*colu
27410 6d 6e 5f 74 65 78 74 29 28 73 71 6c 69 74 65 33  mn_text)(sqlite3
27411 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29  _stmt*,int iCol)
27412 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  ;.  const void *
27413 20 28 2a 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36   (*column_text16
27414 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
27415 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69 6e 74  int iCol);.  int
27416 20 20 28 2a 63 6f 6c 75 6d 6e 5f 74 79 70 65 29    (*column_type)
27417 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
27418 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 73 71 6c 69  nt iCol);.  sqli
27419 74 65 33 5f 76 61 6c 75 65 2a 20 28 2a 63 6f 6c  te3_value* (*col
2741a 75 6d 6e 5f 76 61 6c 75 65 29 28 73 71 6c 69 74  umn_value)(sqlit
2741b 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f  e3_stmt*,int iCo
2741c 6c 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 63  l);.  void * (*c
2741d 6f 6d 6d 69 74 5f 68 6f 6f 6b 29 28 73 71 6c 69  ommit_hook)(sqli
2741e 74 65 33 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64  te3*,int(*)(void
2741f 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74  *),void*);.  int
27420 20 20 28 2a 63 6f 6d 70 6c 65 74 65 29 28 63 6f    (*complete)(co
27421 6e 73 74 20 63 68 61 72 2a 73 71 6c 29 3b 0a 20  nst char*sql);. 
27422 20 69 6e 74 20 20 28 2a 63 6f 6d 70 6c 65 74 65   int  (*complete
27423 31 36 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 73  16)(const void*s
27424 71 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 72  ql);.  int  (*cr
27425 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 29 28  eate_collation)(
27426 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
27427 68 61 72 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 69  har*,int,void*,i
27428 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  nt(*)(void*,int,
27429 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
2742a 63 6f 6e 73 74 20 76 6f 69 64 2a 29 29 3b 0a 20  const void*));. 
2742b 20 69 6e 74 20 20 28 2a 63 72 65 61 74 65 5f 63   int  (*create_c
2742c 6f 6c 6c 61 74 69 6f 6e 31 36 29 28 73 71 6c 69  ollation16)(sqli
2742d 74 65 33 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  te3*,const void*
2742e 2c 69 6e 74 2c 76 6f 69 64 2a 2c 69 6e 74 28 2a  ,int,void*,int(*
2742f 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  )(void*,int,cons
27430 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  t void*,int,cons
27431 74 20 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74  t void*));.  int
27432 20 20 28 2a 63 72 65 61 74 65 5f 66 75 6e 63 74    (*create_funct
27433 69 6f 6e 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f  ion)(sqlite3*,co
27434 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 69 6e  nst char*,int,in
27435 74 2c 76 6f 69 64 2a 2c 76 6f 69 64 20 28 2a 78  t,void*,void (*x
27436 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f  Func)(sqlite3_co
27437 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
27438 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 76 6f 69 64  e3_value**),void
27439 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65   (*xStep)(sqlite
2743a 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
2743b 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c  qlite3_value**),
2743c 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73  void (*xFinal)(s
2743d 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
2743e 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 72 65 61  );.  int  (*crea
2743f 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 29 28 73  te_function16)(s
27440 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 76 6f  qlite3*,const vo
27441 69 64 2a 2c 69 6e 74 2c 69 6e 74 2c 76 6f 69 64  id*,int,int,void
27442 2a 2c 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28  *,void (*xFunc)(
27443 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
27444 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
27445 75 65 2a 2a 29 2c 76 6f 69 64 20 28 2a 78 53 74  ue**),void (*xSt
27446 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ep)(sqlite3_cont
27447 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
27448 5f 76 61 6c 75 65 2a 2a 29 2c 76 6f 69 64 20 28  _value**),void (
27449 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33  *xFinal)(sqlite3
2744a 5f 63 6f 6e 74 65 78 74 2a 29 29 3b 0a 20 20 69  _context*));.  i
2744b 6e 74 20 28 2a 63 72 65 61 74 65 5f 6d 6f 64 75  nt (*create_modu
2744c 6c 65 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  le)(sqlite3*,con
2744d 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 73  st char*,const s
2744e 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2a 2c 76  qlite3_module*,v
2744f 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  oid*);.  int  (*
27450 64 61 74 61 5f 63 6f 75 6e 74 29 28 73 71 6c 69  data_count)(sqli
27451 74 65 33 5f 73 74 6d 74 2a 70 53 74 6d 74 29 3b  te3_stmt*pStmt);
27452 0a 20 20 73 71 6c 69 74 65 33 20 2a 20 28 2a 64  .  sqlite3 * (*d
27453 62 5f 68 61 6e 64 6c 65 29 28 73 71 6c 69 74 65  b_handle)(sqlite
27454 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74 20  3_stmt*);.  int 
27455 28 2a 64 65 63 6c 61 72 65 5f 76 74 61 62 29 28  (*declare_vtab)(
27456 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
27457 68 61 72 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  har*);.  int  (*
27458 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61  enable_shared_ca
27459 63 68 65 29 28 69 6e 74 29 3b 0a 20 20 69 6e 74  che)(int);.  int
2745a 20 20 28 2a 65 72 72 63 6f 64 65 29 28 73 71 6c    (*errcode)(sql
2745b 69 74 65 33 2a 64 62 29 3b 0a 20 20 63 6f 6e 73  ite3*db);.  cons
2745c 74 20 63 68 61 72 20 2a 20 28 2a 65 72 72 6d 73  t char * (*errms
2745d 67 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20  g)(sqlite3*);.  
2745e 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 65  const void * (*e
2745f 72 72 6d 73 67 31 36 29 28 73 71 6c 69 74 65 33  rrmsg16)(sqlite3
27460 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 65 78 65  *);.  int  (*exe
27461 63 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  c)(sqlite3*,cons
27462 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f  t char*,sqlite3_
27463 63 61 6c 6c 62 61 63 6b 2c 76 6f 69 64 2a 2c 63  callback,void*,c
27464 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 20 28  har**);.  int  (
27465 2a 65 78 70 69 72 65 64 29 28 73 71 6c 69 74 65  *expired)(sqlite
27466 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74 20  3_stmt*);.  int 
27467 20 28 2a 66 69 6e 61 6c 69 7a 65 29 28 73 71 6c   (*finalize)(sql
27468 69 74 65 33 5f 73 74 6d 74 2a 70 53 74 6d 74 29  ite3_stmt*pStmt)
27469 3b 0a 20 20 76 6f 69 64 20 20 28 2a 66 72 65 65  ;.  void  (*free
2746a 29 28 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64  )(void*);.  void
2746b 20 20 28 2a 66 72 65 65 5f 74 61 62 6c 65 29 28    (*free_table)(
2746c 63 68 61 72 2a 2a 72 65 73 75 6c 74 29 3b 0a 20  char**result);. 
2746d 20 69 6e 74 20 20 28 2a 67 65 74 5f 61 75 74 6f   int  (*get_auto
2746e 63 6f 6d 6d 69 74 29 28 73 71 6c 69 74 65 33 2a  commit)(sqlite3*
2746f 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 67 65  );.  void * (*ge
27470 74 5f 61 75 78 64 61 74 61 29 28 73 71 6c 69 74  t_auxdata)(sqlit
27471 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 29  e3_context*,int)
27472 3b 0a 20 20 69 6e 74 20 20 28 2a 67 65 74 5f 74  ;.  int  (*get_t
27473 61 62 6c 65 29 28 73 71 6c 69 74 65 33 2a 2c 63  able)(sqlite3*,c
27474 6f 6e 73 74 20 63 68 61 72 2a 2c 63 68 61 72 2a  onst char*,char*
27475 2a 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 63 68 61  **,int*,int*,cha
27476 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 67  r**);.  int  (*g
27477 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 29 28 76  lobal_recover)(v
27478 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  oid);.  void  (*
27479 69 6e 74 65 72 72 75 70 74 78 29 28 73 71 6c 69  interruptx)(sqli
2747a 74 65 33 2a 29 3b 0a 20 20 73 71 6c 69 74 65 5f  te3*);.  sqlite_
2747b 69 6e 74 36 34 20 20 28 2a 6c 61 73 74 5f 69 6e  int64  (*last_in
2747c 73 65 72 74 5f 72 6f 77 69 64 29 28 73 71 6c 69  sert_rowid)(sqli
2747d 74 65 33 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 63  te3*);.  const c
2747e 68 61 72 20 2a 20 28 2a 6c 69 62 76 65 72 73 69  har * (*libversi
2747f 6f 6e 29 28 76 6f 69 64 29 3b 0a 20 20 69 6e 74  on)(void);.  int
27480 20 20 28 2a 6c 69 62 76 65 72 73 69 6f 6e 5f 6e    (*libversion_n
27481 75 6d 62 65 72 29 28 76 6f 69 64 29 3b 0a 20 20  umber)(void);.  
27482 76 6f 69 64 20 2a 28 2a 6d 61 6c 6c 6f 63 29 28  void *(*malloc)(
27483 69 6e 74 29 3b 0a 20 20 63 68 61 72 20 2a 20 28  int);.  char * (
27484 2a 6d 70 72 69 6e 74 66 29 28 63 6f 6e 73 74 20  *mprintf)(const 
27485 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 20 20 69 6e  char*,...);.  in
27486 74 20 20 28 2a 6f 70 65 6e 29 28 63 6f 6e 73 74  t  (*open)(const
27487 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 2a 2a   char*,sqlite3**
27488 29 3b 0a 20 20 69 6e 74 20 20 28 2a 6f 70 65 6e  );.  int  (*open
27489 31 36 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  16)(const void*,
2748a 73 71 6c 69 74 65 33 2a 2a 29 3b 0a 20 20 69 6e  sqlite3**);.  in
2748b 74 20 20 28 2a 70 72 65 70 61 72 65 29 28 73 71  t  (*prepare)(sq
2748c 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
2748d 72 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 73  r*,int,sqlite3_s
2748e 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 63 68 61 72  tmt**,const char
2748f 2a 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 70 72  **);.  int  (*pr
27490 65 70 61 72 65 31 36 29 28 73 71 6c 69 74 65 33  epare16)(sqlite3
27491 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  *,const void*,in
27492 74 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2a  t,sqlite3_stmt**
27493 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2a 29 3b 0a  ,const void**);.
27494 20 20 76 6f 69 64 20 2a 20 28 2a 70 72 6f 66 69    void * (*profi
27495 6c 65 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69  le)(sqlite3*,voi
27496 64 28 2a 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74  d(*)(void*,const
27497 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 5f 75 69   char*,sqlite_ui
27498 6e 74 36 34 29 2c 76 6f 69 64 2a 29 3b 0a 20 20  nt64),void*);.  
27499 76 6f 69 64 20 20 28 2a 70 72 6f 67 72 65 73 73  void  (*progress
2749a 5f 68 61 6e 64 6c 65 72 29 28 73 71 6c 69 74 65  _handler)(sqlite
2749b 33 2a 2c 69 6e 74 2c 69 6e 74 28 2a 29 28 76 6f  3*,int,int(*)(vo
2749c 69 64 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 76  id*),void*);.  v
2749d 6f 69 64 20 2a 28 2a 72 65 61 6c 6c 6f 63 29 28  oid *(*realloc)(
2749e 76 6f 69 64 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e  void*,int);.  in
2749f 74 20 20 28 2a 72 65 73 65 74 29 28 73 71 6c 69  t  (*reset)(sqli
274a0 74 65 33 5f 73 74 6d 74 2a 70 53 74 6d 74 29 3b  te3_stmt*pStmt);
274a1 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c  .  void  (*resul
274a2 74 5f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f  t_blob)(sqlite3_
274a3 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 76  context*,const v
274a4 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29  oid*,int,void(*)
274a5 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69 64  (void*));.  void
274a6 20 20 28 2a 72 65 73 75 6c 74 5f 64 6f 75 62 6c    (*result_doubl
274a7 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  e)(sqlite3_conte
274a8 78 74 2a 2c 64 6f 75 62 6c 65 29 3b 0a 20 20 76  xt*,double);.  v
274a9 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 65 72  oid  (*result_er
274aa 72 6f 72 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  ror)(sqlite3_con
274ab 74 65 78 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72  text*,const char
274ac 2a 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 20  *,int);.  void  
274ad 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36  (*result_error16
274ae 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
274af 74 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  t*,const void*,i
274b0 6e 74 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72  nt);.  void  (*r
274b1 65 73 75 6c 74 5f 69 6e 74 29 28 73 71 6c 69 74  esult_int)(sqlit
274b2 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 29  e3_context*,int)
274b3 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75  ;.  void  (*resu
274b4 6c 74 5f 69 6e 74 36 34 29 28 73 71 6c 69 74 65  lt_int64)(sqlite
274b5 33 5f 63 6f 6e 74 65 78 74 2a 2c 73 71 6c 69 74  3_context*,sqlit
274b6 65 5f 69 6e 74 36 34 29 3b 0a 20 20 76 6f 69 64  e_int64);.  void
274b7 20 20 28 2a 72 65 73 75 6c 74 5f 6e 75 6c 6c 29    (*result_null)
274b8 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
274b9 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65  *);.  void  (*re
274ba 73 75 6c 74 5f 74 65 78 74 29 28 73 71 6c 69 74  sult_text)(sqlit
274bb 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73  e3_context*,cons
274bc 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76 6f 69 64  t char*,int,void
274bd 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76  (*)(void*));.  v
274be 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 74 65  oid  (*result_te
274bf 78 74 31 36 29 28 73 71 6c 69 74 65 33 5f 63 6f  xt16)(sqlite3_co
274c0 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 76 6f 69  ntext*,const voi
274c1 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76  d*,int,void(*)(v
274c2 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20  oid*));.  void  
274c3 28 2a 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62  (*result_text16b
274c4 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  e)(sqlite3_conte
274c5 78 74 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  xt*,const void*,
274c6 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  int,void(*)(void
274c7 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72  *));.  void  (*r
274c8 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 29 28  esult_text16le)(
274c9 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
274ca 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
274cb 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29  ,void(*)(void*))
274cc 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75  ;.  void  (*resu
274cd 6c 74 5f 76 61 6c 75 65 29 28 73 71 6c 69 74 65  lt_value)(sqlite
274ce 33 5f 63 6f 6e 74 65 78 74 2a 2c 73 71 6c 69 74  3_context*,sqlit
274cf 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 76 6f  e3_value*);.  vo
274d0 69 64 20 2a 20 28 2a 72 6f 6c 6c 62 61 63 6b 5f  id * (*rollback_
274d1 68 6f 6f 6b 29 28 73 71 6c 69 74 65 33 2a 2c 76  hook)(sqlite3*,v
274d2 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 2c 76 6f  oid(*)(void*),vo
274d3 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 73  id*);.  int  (*s
274d4 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 29 28 73  et_authorizer)(s
274d5 71 6c 69 74 65 33 2a 2c 69 6e 74 28 2a 29 28 76  qlite3*,int(*)(v
274d6 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63  oid*,int,const c
274d7 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
274d8 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
274d9 73 74 20 63 68 61 72 2a 29 2c 76 6f 69 64 2a 29  st char*),void*)
274da 3b 0a 20 20 76 6f 69 64 20 20 28 2a 73 65 74 5f  ;.  void  (*set_
274db 61 75 78 64 61 74 61 29 28 73 71 6c 69 74 65 33  auxdata)(sqlite3
274dc 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 76 6f  _context*,int,vo
274dd 69 64 2a 2c 76 6f 69 64 20 28 2a 29 28 76 6f 69  id*,void (*)(voi
274de 64 2a 29 29 3b 0a 20 20 63 68 61 72 20 2a 20 28  d*));.  char * (
274df 2a 73 6e 70 72 69 6e 74 66 29 28 69 6e 74 2c 63  *snprintf)(int,c
274e0 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
274e1 2c 2e 2e 2e 29 3b 0a 20 20 69 6e 74 20 20 28 2a  ,...);.  int  (*
274e2 73 74 65 70 29 28 73 71 6c 69 74 65 33 5f 73 74  step)(sqlite3_st
274e3 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 74  mt*);.  int  (*t
274e4 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61  able_column_meta
274e5 64 61 74 61 29 28 73 71 6c 69 74 65 33 2a 2c 63  data)(sqlite3*,c
274e6 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
274e7 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
274e8 72 2a 2c 63 68 61 72 20 63 6f 6e 73 74 2a 2a 2c  r*,char const**,
274e9 63 68 61 72 20 63 6f 6e 73 74 2a 2a 2c 69 6e 74  char const**,int
274ea 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a 20 20  *,int*,int*);.  
274eb 76 6f 69 64 20 20 28 2a 74 68 72 65 61 64 5f 63  void  (*thread_c
274ec 6c 65 61 6e 75 70 29 28 76 6f 69 64 29 3b 0a 20  leanup)(void);. 
274ed 20 69 6e 74 20 20 28 2a 74 6f 74 61 6c 5f 63 68   int  (*total_ch
274ee 61 6e 67 65 73 29 28 73 71 6c 69 74 65 33 2a 29  anges)(sqlite3*)
274ef 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 74 72 61  ;.  void * (*tra
274f0 63 65 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69  ce)(sqlite3*,voi
274f1 64 28 2a 78 54 72 61 63 65 29 28 76 6f 69 64 2a  d(*xTrace)(void*
274f2 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c 76 6f  ,const char*),vo
274f3 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 74  id*);.  int  (*t
274f4 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67 73  ransfer_bindings
274f5 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
274f6 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a  sqlite3_stmt*);.
274f7 20 20 76 6f 69 64 20 2a 20 28 2a 75 70 64 61 74    void * (*updat
274f8 65 5f 68 6f 6f 6b 29 28 73 71 6c 69 74 65 33 2a  e_hook)(sqlite3*
274f9 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 69  ,void(*)(void*,i
274fa 6e 74 20 2c 63 68 61 72 20 63 6f 6e 73 74 2a 2c  nt ,char const*,
274fb 63 68 61 72 20 63 6f 6e 73 74 2a 2c 73 71 6c 69  char const*,sqli
274fc 74 65 5f 69 6e 74 36 34 29 2c 76 6f 69 64 2a 29  te_int64),void*)
274fd 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 75 73 65  ;.  void * (*use
274fe 72 5f 64 61 74 61 29 28 73 71 6c 69 74 65 33 5f  r_data)(sqlite3_
274ff 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 63 6f 6e  context*);.  con
27500 73 74 20 76 6f 69 64 20 2a 20 28 2a 76 61 6c 75  st void * (*valu
27501 65 5f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f  e_blob)(sqlite3_
27502 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20 20  value*);.  int  
27503 28 2a 76 61 6c 75 65 5f 62 79 74 65 73 29 28 73  (*value_bytes)(s
27504 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
27505 20 20 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f 62    int  (*value_b
27506 79 74 65 73 31 36 29 28 73 71 6c 69 74 65 33 5f  ytes16)(sqlite3_
27507 76 61 6c 75 65 2a 29 3b 0a 20 20 64 6f 75 62 6c  value*);.  doubl
27508 65 20 20 28 2a 76 61 6c 75 65 5f 64 6f 75 62 6c  e  (*value_doubl
27509 65 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  e)(sqlite3_value
2750a 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c  *);.  int  (*val
2750b 75 65 5f 69 6e 74 29 28 73 71 6c 69 74 65 33 5f  ue_int)(sqlite3_
2750c 76 61 6c 75 65 2a 29 3b 0a 20 20 73 71 6c 69 74  value*);.  sqlit
2750d 65 5f 69 6e 74 36 34 20 20 28 2a 76 61 6c 75 65  e_int64  (*value
2750e 5f 69 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f  _int64)(sqlite3_
2750f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20 20  value*);.  int  
27510 28 2a 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f  (*value_numeric_
27511 74 79 70 65 29 28 73 71 6c 69 74 65 33 5f 76 61  type)(sqlite3_va
27512 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 75  lue*);.  const u
27513 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 20 28  nsigned char * (
27514 2a 76 61 6c 75 65 5f 74 65 78 74 29 28 73 71 6c  *value_text)(sql
27515 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20  ite3_value*);.  
27516 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 76  const void * (*v
27517 61 6c 75 65 5f 74 65 78 74 31 36 29 28 73 71 6c  alue_text16)(sql
27518 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20  ite3_value*);.  
27519 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 76  const void * (*v
2751a 61 6c 75 65 5f 74 65 78 74 31 36 62 65 29 28 73  alue_text16be)(s
2751b 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
2751c 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28    const void * (
2751d 2a 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 29  *value_text16le)
2751e 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
2751f 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75 65  ;.  int  (*value
27520 5f 74 79 70 65 29 28 73 71 6c 69 74 65 33 5f 76  _type)(sqlite3_v
27521 61 6c 75 65 2a 29 3b 0a 20 20 63 68 61 72 20 2a  alue*);.  char *
27522 28 2a 76 6d 70 72 69 6e 74 66 29 28 63 6f 6e 73  (*vmprintf)(cons
27523 74 20 63 68 61 72 2a 2c 76 61 5f 6c 69 73 74 29  t char*,va_list)
27524 3b 0a 20 20 2f 2a 20 41 64 64 65 64 20 3f 3f 3f  ;.  /* Added ???
27525 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 6f 76 65 72   */.  int (*over
27526 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 29 28 73  load_function)(s
27527 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63  qlite3*, const c
27528 68 61 72 20 2a 7a 46 75 6e 63 4e 61 6d 65 2c 20  har *zFuncName, 
27529 69 6e 74 20 6e 41 72 67 29 3b 0a 20 20 2f 2a 20  int nArg);.  /* 
2752a 41 64 64 65 64 20 62 79 20 33 2e 33 2e 31 33 20  Added by 3.3.13 
2752b 2a 2f 0a 20 20 69 6e 74 20 28 2a 70 72 65 70 61  */.  int (*prepa
2752c 72 65 5f 76 32 29 28 73 71 6c 69 74 65 33 2a 2c  re_v2)(sqlite3*,
2752d 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c  const char*,int,
2752e 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2a 2c 63  sqlite3_stmt**,c
2752f 6f 6e 73 74 20 63 68 61 72 2a 2a 29 3b 0a 20 20  onst char**);.  
27530 69 6e 74 20 28 2a 70 72 65 70 61 72 65 31 36 5f  int (*prepare16_
27531 76 32 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  v2)(sqlite3*,con
27532 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 73 71 6c  st void*,int,sql
27533 69 74 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73  ite3_stmt**,cons
27534 74 20 76 6f 69 64 2a 2a 29 3b 0a 20 20 69 6e 74  t void**);.  int
27535 20 28 2a 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67   (*clear_binding
27536 73 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  s)(sqlite3_stmt*
27537 29 3b 0a 20 20 2f 2a 20 41 64 64 65 64 20 62 79  );.  /* Added by
27538 20 33 2e 34 2e 31 20 2a 2f 0a 20 20 69 6e 74 20   3.4.1 */.  int 
27539 28 2a 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f  (*create_module_
2753a 76 32 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  v2)(sqlite3*,con
2753b 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 73  st char*,const s
2753c 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2a 2c 76  qlite3_module*,v
2753d 6f 69 64 2a 2c 76 6f 69 64 20 28 2a 78 44 65 73  oid*,void (*xDes
2753e 74 72 6f 79 29 28 76 6f 69 64 20 2a 29 29 3b 0a  troy)(void *));.
2753f 20 20 2f 2a 20 41 64 64 65 64 20 62 79 20 33 2e    /* Added by 3.
27540 35 2e 30 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 62  5.0 */.  int (*b
27541 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 29 28 73 71  ind_zeroblob)(sq
27542 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c  lite3_stmt*,int,
27543 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 62 6c  int);.  int (*bl
27544 6f 62 5f 62 79 74 65 73 29 28 73 71 6c 69 74 65  ob_bytes)(sqlite
27545 33 5f 62 6c 6f 62 2a 29 3b 0a 20 20 69 6e 74 20  3_blob*);.  int 
27546 28 2a 62 6c 6f 62 5f 63 6c 6f 73 65 29 28 73 71  (*blob_close)(sq
27547 6c 69 74 65 33 5f 62 6c 6f 62 2a 29 3b 0a 20 20  lite3_blob*);.  
27548 69 6e 74 20 28 2a 62 6c 6f 62 5f 6f 70 65 6e 29  int (*blob_open)
27549 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
2754a 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
2754b 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71  *,const char*,sq
2754c 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74 2c  lite3_int64,int,
2754d 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 2a 29 3b  sqlite3_blob**);
2754e 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 72 65  .  int (*blob_re
2754f 61 64 29 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ad)(sqlite3_blob
27550 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 29  *,void*,int,int)
27551 3b 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 77  ;.  int (*blob_w
27552 72 69 74 65 29 28 73 71 6c 69 74 65 33 5f 62 6c  rite)(sqlite3_bl
27553 6f 62 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  ob*,const void*,
27554 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20  int,int);.  int 
27555 28 2a 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  (*create_collati
27556 6f 6e 5f 76 32 29 28 73 71 6c 69 74 65 33 2a 2c  on_v2)(sqlite3*,
27557 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c  const char*,int,
27558 76 6f 69 64 2a 2c 69 6e 74 28 2a 29 28 76 6f 69  void*,int(*)(voi
27559 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
2755a 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
2755b 64 2a 29 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  d*),void(*)(void
2755c 2a 29 29 3b 0a 20 20 69 6e 74 20 28 2a 66 69 6c  *));.  int (*fil
2755d 65 5f 63 6f 6e 74 72 6f 6c 29 28 73 71 6c 69 74  e_control)(sqlit
2755e 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e3*,const char*,
2755f 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 20 20 73 71  int,void*);.  sq
27560 6c 69 74 65 33 5f 69 6e 74 36 34 20 28 2a 6d 65  lite3_int64 (*me
27561 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 29 28  mory_highwater)(
27562 69 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  int);.  sqlite3_
27563 69 6e 74 36 34 20 28 2a 6d 65 6d 6f 72 79 5f 75  int64 (*memory_u
27564 73 65 64 29 28 76 6f 69 64 29 3b 0a 20 20 73 71  sed)(void);.  sq
27565 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 28 2a 6d  lite3_mutex *(*m
27566 75 74 65 78 5f 61 6c 6c 6f 63 29 28 69 6e 74 29  utex_alloc)(int)
27567 3b 0a 20 20 76 6f 69 64 20 28 2a 6d 75 74 65 78  ;.  void (*mutex
27568 5f 65 6e 74 65 72 29 28 73 71 6c 69 74 65 33 5f  _enter)(sqlite3_
27569 6d 75 74 65 78 2a 29 3b 0a 20 20 76 6f 69 64 20  mutex*);.  void 
2756a 28 2a 6d 75 74 65 78 5f 66 72 65 65 29 28 73 71  (*mutex_free)(sq
2756b 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 20  lite3_mutex*);. 
2756c 20 76 6f 69 64 20 28 2a 6d 75 74 65 78 5f 6c 65   void (*mutex_le
2756d 61 76 65 29 28 73 71 6c 69 74 65 33 5f 6d 75 74  ave)(sqlite3_mut
2756e 65 78 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 6d 75  ex*);.  int (*mu
2756f 74 65 78 5f 74 72 79 29 28 73 71 6c 69 74 65 33  tex_try)(sqlite3
27570 5f 6d 75 74 65 78 2a 29 3b 0a 20 20 69 6e 74 20  _mutex*);.  int 
27571 28 2a 6f 70 65 6e 5f 76 32 29 28 63 6f 6e 73 74  (*open_v2)(const
27572 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 2a 2a   char*,sqlite3**
27573 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a  ,int,const char*
27574 29 3b 0a 20 20 69 6e 74 20 28 2a 72 65 6c 65 61  );.  int (*relea
27575 73 65 5f 6d 65 6d 6f 72 79 29 28 69 6e 74 29 3b  se_memory)(int);
27576 0a 20 20 76 6f 69 64 20 28 2a 72 65 73 75 6c 74  .  void (*result
27577 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 29 28 73 71  _error_nomem)(sq
27578 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b  lite3_context*);
27579 0a 20 20 76 6f 69 64 20 28 2a 72 65 73 75 6c 74  .  void (*result
2757a 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 29 28 73  _error_toobig)(s
2757b 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
2757c 3b 0a 20 20 69 6e 74 20 28 2a 73 6c 65 65 70 29  ;.  int (*sleep)
2757d 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a  (int);.  void (*
2757e 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 29  soft_heap_limit)
2757f 28 69 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  (int);.  sqlite3
27580 5f 76 66 73 20 2a 28 2a 76 66 73 5f 66 69 6e 64  _vfs *(*vfs_find
27581 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  )(const char*);.
27582 20 20 69 6e 74 20 28 2a 76 66 73 5f 72 65 67 69    int (*vfs_regi
27583 73 74 65 72 29 28 73 71 6c 69 74 65 33 5f 76 66  ster)(sqlite3_vf
27584 73 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28  s*,int);.  int (
27585 2a 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 29  *vfs_unregister)
27586 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 29 3b 0a  (sqlite3_vfs*);.
27587 20 20 69 6e 74 20 28 2a 78 74 68 72 65 61 64 73    int (*xthreads
27588 61 66 65 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f  afe)(void);.  vo
27589 69 64 20 28 2a 72 65 73 75 6c 74 5f 7a 65 72 6f  id (*result_zero
2758a 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 63 6f  blob)(sqlite3_co
2758b 6e 74 65 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 76  ntext*,int);.  v
2758c 6f 69 64 20 28 2a 72 65 73 75 6c 74 5f 65 72 72  oid (*result_err
2758d 6f 72 5f 63 6f 64 65 29 28 73 71 6c 69 74 65 33  or_code)(sqlite3
2758e 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 29 3b 0a  _context*,int);.
2758f 20 20 69 6e 74 20 28 2a 74 65 73 74 5f 63 6f 6e    int (*test_con
27590 74 72 6f 6c 29 28 69 6e 74 2c 20 2e 2e 2e 29 3b  trol)(int, ...);
27591 0a 20 20 76 6f 69 64 20 28 2a 72 61 6e 64 6f 6d  .  void (*random
27592 6e 65 73 73 29 28 69 6e 74 2c 76 6f 69 64 2a 29  ness)(int,void*)
27593 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 28 2a 63  ;.  sqlite3 *(*c
27594 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
27595 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
27596 74 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 65 78 74  t*);.  int (*ext
27597 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64  ended_result_cod
27598 65 73 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74  es)(sqlite3*,int
27599 29 3b 0a 20 20 69 6e 74 20 28 2a 6c 69 6d 69 74  );.  int (*limit
2759a 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 69  )(sqlite3*,int,i
2759b 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  nt);.  sqlite3_s
2759c 74 6d 74 20 2a 28 2a 6e 65 78 74 5f 73 74 6d 74  tmt *(*next_stmt
2759d 29 28 73 71 6c 69 74 65 33 2a 2c 73 71 6c 69 74  )(sqlite3*,sqlit
2759e 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 63 6f 6e  e3_stmt*);.  con
2759f 73 74 20 63 68 61 72 20 2a 28 2a 73 71 6c 29 28  st char *(*sql)(
275a0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a  sqlite3_stmt*);.
275a1 20 20 69 6e 74 20 28 2a 73 74 61 74 75 73 29 28    int (*status)(
275a2 69 6e 74 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e  int,int*,int*,in
275a3 74 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68  t);.};../*.** Th
275a4 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72  e following macr
275a5 6f 73 20 72 65 64 65 66 69 6e 65 20 74 68 65 20  os redefine the 
275a6 41 50 49 20 72 6f 75 74 69 6e 65 73 20 73 6f 20  API routines so 
275a7 74 68 61 74 20 74 68 65 79 20 61 72 65 0a 2a 2a  that they are.**
275a8 20 72 65 64 69 72 65 63 74 65 64 20 74 68 72 6f   redirected thro
275a9 75 67 68 74 20 74 68 65 20 67 6c 6f 62 61 6c 20  ught the global 
275aa 73 71 6c 69 74 65 33 5f 61 70 69 20 73 74 72 75  sqlite3_api stru
275ab 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  cture..**.** Thi
275ac 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 69 73  s header file is
275ad 20 61 6c 73 6f 20 75 73 65 64 20 62 79 20 74 68   also used by th
275ae 65 20 6c 6f 61 64 65 78 74 2e 63 20 73 6f 75 72  e loadext.c sour
275af 63 65 20 66 69 6c 65 0a 2a 2a 20 28 70 61 72 74  ce file.** (part
275b0 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 53 51 4c   of the main SQL
275b1 69 74 65 20 6c 69 62 72 61 72 79 20 2d 20 6e 6f  ite library - no
275b2 74 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e 29 20  t an extension) 
275b3 73 6f 20 74 68 61 74 0a 2a 2a 20 69 74 20 63 61  so that.** it ca
275b4 6e 20 67 65 74 20 61 63 63 65 73 73 20 74 6f 20  n get access to 
275b5 74 68 65 20 73 71 6c 69 74 65 33 5f 61 70 69 5f  the sqlite3_api_
275b6 72 6f 75 74 69 6e 65 73 20 73 74 72 75 63 74 75  routines structu
275b7 72 65 0a 2a 2a 20 64 65 66 69 6e 69 74 69 6f 6e  re.** definition
275b8 2e 20 20 42 75 74 20 74 68 65 20 6d 61 69 6e 20  .  But the main 
275b9 6c 69 62 72 61 72 79 20 64 6f 65 73 20 6e 6f 74  library does not
275ba 20 77 61 6e 74 20 74 6f 20 72 65 64 65 66 69 6e   want to redefin
275bb 65 0a 2a 2a 20 74 68 65 20 41 50 49 2e 20 20 53  e.** the API.  S
275bc 6f 20 74 68 65 20 72 65 64 65 66 69 6e 69 74 69  o the redefiniti
275bd 6f 6e 20 6d 61 63 72 6f 73 20 61 72 65 20 6f 6e  on macros are on
275be 6c 79 20 76 61 6c 69 64 20 69 66 20 74 68 65 0a  ly valid if the.
275bf 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 6d  ** SQLITE_CORE m
275c0 61 63 72 6f 73 20 69 73 20 75 6e 64 65 66 69 6e  acros is undefin
275c1 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ed..*/.#ifndef S
275c2 51 4c 49 54 45 5f 43 4f 52 45 0a 23 64 65 66 69  QLITE_CORE.#defi
275c3 6e 65 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65  ne sqlite3_aggre
275c4 67 61 74 65 5f 63 6f 6e 74 65 78 74 20 20 20 20  gate_context    
275c5 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 61    sqlite3_api->a
275c6 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74  ggregate_context
275c7 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
275c8 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a  OMIT_DEPRECATED.
275c9 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
275ca 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 20  aggregate_count 
275cb 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
275cc 70 69 2d 3e 61 67 67 72 65 67 61 74 65 5f 63 6f  pi->aggregate_co
275cd 75 6e 74 0a 23 65 6e 64 69 66 0a 23 64 65 66 69  unt.#endif.#defi
275ce 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ne sqlite3_bind_
275cf 62 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20 20  blob            
275d0 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
275d1 69 6e 64 5f 62 6c 6f 62 0a 23 64 65 66 69 6e 65  ind_blob.#define
275d2 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f   sqlite3_bind_do
275d3 75 62 6c 65 20 20 20 20 20 20 20 20 20 20 20 20  uble            
275d4 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e  sqlite3_api->bin
275d5 64 5f 64 6f 75 62 6c 65 0a 23 64 65 66 69 6e 65  d_double.#define
275d6 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
275d7 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
275d8 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e  sqlite3_api->bin
275d9 64 5f 69 6e 74 0a 23 64 65 66 69 6e 65 20 73 71  d_int.#define sq
275da 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
275db 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
275dc 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 69  ite3_api->bind_i
275dd 6e 74 36 34 0a 23 64 65 66 69 6e 65 20 73 71 6c  nt64.#define sql
275de 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 20 20  ite3_bind_null  
275df 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
275e0 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 6e 75  te3_api->bind_nu
275e1 6c 6c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ll.#define sqlit
275e2 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
275e3 72 5f 63 6f 75 6e 74 20 20 20 73 71 6c 69 74 65  r_count   sqlite
275e4 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70 61 72 61  3_api->bind_para
275e5 6d 65 74 65 72 5f 63 6f 75 6e 74 0a 23 64 65 66  meter_count.#def
275e6 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ine sqlite3_bind
275e7 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78  _parameter_index
275e8 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
275e9 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69  bind_parameter_i
275ea 6e 64 65 78 0a 23 64 65 66 69 6e 65 20 73 71 6c  ndex.#define sql
275eb 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
275ec 74 65 72 5f 6e 61 6d 65 20 20 20 20 73 71 6c 69  ter_name    sqli
275ed 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70 61  te3_api->bind_pa
275ee 72 61 6d 65 74 65 72 5f 6e 61 6d 65 0a 23 64 65  rameter_name.#de
275ef 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e  fine sqlite3_bin
275f0 64 5f 74 65 78 74 20 20 20 20 20 20 20 20 20 20  d_text          
275f1 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
275f2 3e 62 69 6e 64 5f 74 65 78 74 0a 23 64 65 66 69  >bind_text.#defi
275f3 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ne sqlite3_bind_
275f4 74 65 78 74 31 36 20 20 20 20 20 20 20 20 20 20  text16          
275f5 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
275f6 69 6e 64 5f 74 65 78 74 31 36 0a 23 64 65 66 69  ind_text16.#defi
275f7 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ne sqlite3_bind_
275f8 76 61 6c 75 65 20 20 20 20 20 20 20 20 20 20 20  value           
275f9 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
275fa 69 6e 64 5f 76 61 6c 75 65 0a 23 64 65 66 69 6e  ind_value.#defin
275fb 65 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68  e sqlite3_busy_h
275fc 61 6e 64 6c 65 72 20 20 20 20 20 20 20 20 20 20  andler          
275fd 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 75   sqlite3_api->bu
275fe 73 79 5f 68 61 6e 64 6c 65 72 0a 23 64 65 66 69  sy_handler.#defi
275ff 6e 65 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f  ne sqlite3_busy_
27600 74 69 6d 65 6f 75 74 20 20 20 20 20 20 20 20 20  timeout         
27601 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
27602 75 73 79 5f 74 69 6d 65 6f 75 74 0a 23 64 65 66  usy_timeout.#def
27603 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 68 61 6e  ine sqlite3_chan
27604 67 65 73 20 20 20 20 20 20 20 20 20 20 20 20 20  ges             
27605 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
27606 63 68 61 6e 67 65 73 0a 23 64 65 66 69 6e 65 20  changes.#define 
27607 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 20 20 20  sqlite3_close   
27608 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
27609 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6c 6f 73  qlite3_api->clos
2760a 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
2760b 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64  3_collation_need
2760c 65 64 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  ed       sqlite3
2760d 5f 61 70 69 2d 3e 63 6f 6c 6c 61 74 69 6f 6e 5f  _api->collation_
2760e 6e 65 65 64 65 64 0a 23 64 65 66 69 6e 65 20 73  needed.#define s
2760f 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
27610 5f 6e 65 65 64 65 64 31 36 20 20 20 20 20 73 71  _needed16     sq
27611 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 6c 61  lite3_api->colla
27612 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 0a 23 64  tion_needed16.#d
27613 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
27614 6c 75 6d 6e 5f 62 6c 6f 62 20 20 20 20 20 20 20  lumn_blob       
27615 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
27616 2d 3e 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 0a 23 64  ->column_blob.#d
27617 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
27618 6c 75 6d 6e 5f 62 79 74 65 73 20 20 20 20 20 20  lumn_bytes      
27619 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
2761a 2d 3e 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 0a 23  ->column_bytes.#
2761b 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
2761c 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 20 20 20  olumn_bytes16   
2761d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
2761e 69 2d 3e 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31  i->column_bytes1
2761f 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  6.#define sqlite
27620 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 20 20  3_column_count  
27621 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
27622 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 63 6f 75  _api->column_cou
27623 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  nt.#define sqlit
27624 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61  e3_column_databa
27625 73 65 5f 6e 61 6d 65 20 20 20 73 71 6c 69 74 65  se_name   sqlite
27626 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 61  3_api->column_da
27627 74 61 62 61 73 65 5f 6e 61 6d 65 0a 23 64 65 66  tabase_name.#def
27628 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
27629 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
2762a 31 36 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e  16 sqlite3_api->
2762b 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f  column_database_
2762c 6e 61 6d 65 31 36 0a 23 64 65 66 69 6e 65 20 73  name16.#define s
2762d 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65  qlite3_column_de
2762e 63 6c 74 79 70 65 20 20 20 20 20 20 20 20 73 71  cltype        sq
2762f 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d  lite3_api->colum
27630 6e 5f 64 65 63 6c 74 79 70 65 0a 23 64 65 66 69  n_decltype.#defi
27631 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
27632 6e 5f 64 65 63 6c 74 79 70 65 31 36 20 20 20 20  n_decltype16    
27633 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
27634 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36  olumn_decltype16
27635 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
27636 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20 20  _column_double  
27637 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
27638 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 6f 75 62  api->column_doub
27639 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  le.#define sqlit
2763a 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 20 20 20  e3_column_int   
2763b 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2763c 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 69 6e  3_api->column_in
2763d 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  t.#define sqlite
2763e 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 20 20  3_column_int64  
2763f 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
27640 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 69 6e 74  _api->column_int
27641 36 34 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  64.#define sqlit
27642 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 20 20  e3_column_name  
27643 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
27644 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 6e 61  3_api->column_na
27645 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  me.#define sqlit
27646 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36  e3_column_name16
27647 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
27648 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 6e 61  3_api->column_na
27649 6d 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c  me16.#define sql
2764a 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67  ite3_column_orig
2764b 69 6e 5f 6e 61 6d 65 20 20 20 20 20 73 71 6c 69  in_name     sqli
2764c 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f  te3_api->column_
2764d 6f 72 69 67 69 6e 5f 6e 61 6d 65 0a 23 64 65 66  origin_name.#def
2764e 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
2764f 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36  mn_origin_name16
27650 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
27651 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61  column_origin_na
27652 6d 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c  me16.#define sql
27653 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c  ite3_column_tabl
27654 65 5f 6e 61 6d 65 20 20 20 20 20 20 73 71 6c 69  e_name      sqli
27655 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f  te3_api->column_
27656 74 61 62 6c 65 5f 6e 61 6d 65 0a 23 64 65 66 69  table_name.#defi
27657 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
27658 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 20 20  n_table_name16  
27659 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
2765a 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65  olumn_table_name
2765b 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
2765c 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 20 20  e3_column_text  
2765d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2765e 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74 65  3_api->column_te
2765f 78 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  xt.#define sqlit
27660 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
27661 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
27662 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74 65  3_api->column_te
27663 78 74 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c  xt16.#define sql
27664 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65  ite3_column_type
27665 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
27666 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f  te3_api->column_
27667 74 79 70 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  type.#define sql
27668 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75  ite3_column_valu
27669 65 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  e           sqli
2766a 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f  te3_api->column_
2766b 76 61 6c 75 65 0a 23 64 65 66 69 6e 65 20 73 71  value.#define sq
2766c 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f  lite3_commit_hoo
2766d 6b 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  k            sql
2766e 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6d 6d 69 74  ite3_api->commit
2766f 5f 68 6f 6f 6b 0a 23 64 65 66 69 6e 65 20 73 71  _hook.#define sq
27670 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 20 20  lite3_complete  
27671 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
27672 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6d 70 6c 65  ite3_api->comple
27673 74 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  te.#define sqlit
27674 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 20 20 20  e3_complete16   
27675 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
27676 33 5f 61 70 69 2d 3e 63 6f 6d 70 6c 65 74 65 31  3_api->complete1
27677 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  6.#define sqlite
27678 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
27679 6f 6e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  on       sqlite3
2767a 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 63 6f 6c  _api->create_col
2767b 6c 61 74 69 6f 6e 0a 23 64 65 66 69 6e 65 20 73  lation.#define s
2767c 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
2767d 6c 6c 61 74 69 6f 6e 31 36 20 20 20 20 20 73 71  llation16     sq
2767e 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61 74  lite3_api->creat
2767f 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 0a 23 64  e_collation16.#d
27680 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72  efine sqlite3_cr
27681 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 20 20  eate_function   
27682 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
27683 2d 3e 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  ->create_functio
27684 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  n.#define sqlite
27685 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
27686 6e 31 36 20 20 20 20 20 20 73 71 6c 69 74 65 33  n16      sqlite3
27687 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 66 75 6e  _api->create_fun
27688 63 74 69 6f 6e 31 36 0a 23 64 65 66 69 6e 65 20  ction16.#define 
27689 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
2768a 6f 64 75 6c 65 20 20 20 20 20 20 20 20 20 20 73  odule          s
2768b 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61  qlite3_api->crea
2768c 74 65 5f 6d 6f 64 75 6c 65 0a 23 64 65 66 69 6e  te_module.#defin
2768d 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
2768e 5f 6d 6f 64 75 6c 65 5f 76 32 20 20 20 20 20 20  _module_v2      
2768f 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72   sqlite3_api->cr
27690 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 0a 23  eate_module_v2.#
27691 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 64  define sqlite3_d
27692 61 74 61 5f 63 6f 75 6e 74 20 20 20 20 20 20 20  ata_count       
27693 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
27694 69 2d 3e 64 61 74 61 5f 63 6f 75 6e 74 0a 23 64  i->data_count.#d
27695 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 64 62  efine sqlite3_db
27696 5f 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20 20  _handle         
27697 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
27698 2d 3e 64 62 5f 68 61 6e 64 6c 65 0a 23 64 65 66  ->db_handle.#def
27699 69 6e 65 20 73 71 6c 69 74 65 33 5f 64 65 63 6c  ine sqlite3_decl
2769a 61 72 65 5f 76 74 61 62 20 20 20 20 20 20 20 20  are_vtab        
2769b 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
2769c 64 65 63 6c 61 72 65 5f 76 74 61 62 0a 23 64 65  declare_vtab.#de
2769d 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 6e 61  fine sqlite3_ena
2769e 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65  ble_shared_cache
2769f 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
276a0 3e 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63  >enable_shared_c
276a1 61 63 68 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  ache.#define sql
276a2 69 74 65 33 5f 65 72 72 63 6f 64 65 20 20 20 20  ite3_errcode    
276a3 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
276a4 74 65 33 5f 61 70 69 2d 3e 65 72 72 63 6f 64 65  te3_api->errcode
276a5 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
276a6 5f 65 72 72 6d 73 67 20 20 20 20 20 20 20 20 20  _errmsg         
276a7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
276a8 61 70 69 2d 3e 65 72 72 6d 73 67 0a 23 64 65 66  api->errmsg.#def
276a9 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  ine sqlite3_errm
276aa 73 67 31 36 20 20 20 20 20 20 20 20 20 20 20 20  sg16            
276ab 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
276ac 65 72 72 6d 73 67 31 36 0a 23 64 65 66 69 6e 65  errmsg16.#define
276ad 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 20 20   sqlite3_exec   
276ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
276af 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 78 65  sqlite3_api->exe
276b0 63 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  c.#ifndef SQLITE
276b1 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
276b2 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
276b3 5f 65 78 70 69 72 65 64 20 20 20 20 20 20 20 20  _expired        
276b4 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
276b5 61 70 69 2d 3e 65 78 70 69 72 65 64 0a 23 65 6e  api->expired.#en
276b6 64 69 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  dif.#define sqli
276b7 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 20 20 20  te3_finalize    
276b8 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
276b9 65 33 5f 61 70 69 2d 3e 66 69 6e 61 6c 69 7a 65  e3_api->finalize
276ba 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
276bb 5f 66 72 65 65 20 20 20 20 20 20 20 20 20 20 20  _free           
276bc 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
276bd 61 70 69 2d 3e 66 72 65 65 0a 23 64 65 66 69 6e  api->free.#defin
276be 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74  e sqlite3_free_t
276bf 61 62 6c 65 20 20 20 20 20 20 20 20 20 20 20 20  able            
276c0 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 66 72   sqlite3_api->fr
276c1 65 65 5f 74 61 62 6c 65 0a 23 64 65 66 69 6e 65  ee_table.#define
276c2 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 74   sqlite3_get_aut
276c3 6f 63 6f 6d 6d 69 74 20 20 20 20 20 20 20 20 20  ocommit         
276c4 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 67 65 74  sqlite3_api->get
276c5 5f 61 75 74 6f 63 6f 6d 6d 69 74 0a 23 64 65 66  _autocommit.#def
276c6 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  ine sqlite3_get_
276c7 61 75 78 64 61 74 61 20 20 20 20 20 20 20 20 20  auxdata         
276c8 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
276c9 67 65 74 5f 61 75 78 64 61 74 61 0a 23 64 65 66  get_auxdata.#def
276ca 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  ine sqlite3_get_
276cb 74 61 62 6c 65 20 20 20 20 20 20 20 20 20 20 20  table           
276cc 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
276cd 67 65 74 5f 74 61 62 6c 65 0a 23 69 66 6e 64 65  get_table.#ifnde
276ce 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
276cf 50 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e 65  PRECATED.#define
276d0 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62 61 6c 5f   sqlite3_global_
276d1 72 65 63 6f 76 65 72 20 20 20 20 20 20 20 20 20  recover         
276d2 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 67 6c 6f  sqlite3_api->glo
276d3 62 61 6c 5f 72 65 63 6f 76 65 72 0a 23 65 6e 64  bal_recover.#end
276d4 69 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  if.#define sqlit
276d5 65 33 5f 69 6e 74 65 72 72 75 70 74 20 20 20 20  e3_interrupt    
276d6 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
276d7 33 5f 61 70 69 2d 3e 69 6e 74 65 72 72 75 70 74  3_api->interrupt
276d8 78 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  x.#define sqlite
276d9 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
276da 77 69 64 20 20 20 20 20 20 73 71 6c 69 74 65 33  wid      sqlite3
276db 5f 61 70 69 2d 3e 6c 61 73 74 5f 69 6e 73 65 72  _api->last_inser
276dc 74 5f 72 6f 77 69 64 0a 23 64 65 66 69 6e 65 20  t_rowid.#define 
276dd 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69  sqlite3_libversi
276de 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 73  on             s
276df 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 69 62 76  qlite3_api->libv
276e0 65 72 73 69 6f 6e 0a 23 64 65 66 69 6e 65 20 73  ersion.#define s
276e1 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
276e2 6e 5f 6e 75 6d 62 65 72 20 20 20 20 20 20 73 71  n_number      sq
276e3 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 69 62 76 65  lite3_api->libve
276e4 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 0a 23 64 65  rsion_number.#de
276e5 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 61 6c  fine sqlite3_mal
276e6 6c 6f 63 20 20 20 20 20 20 20 20 20 20 20 20 20  loc             
276e7 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
276e8 3e 6d 61 6c 6c 6f 63 0a 23 64 65 66 69 6e 65 20  >malloc.#define 
276e9 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 20  sqlite3_mprintf 
276ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
276eb 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 70 72 69  qlite3_api->mpri
276ec 6e 74 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ntf.#define sqli
276ed 74 65 33 5f 6f 70 65 6e 20 20 20 20 20 20 20 20  te3_open        
276ee 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
276ef 65 33 5f 61 70 69 2d 3e 6f 70 65 6e 0a 23 64 65  e3_api->open.#de
276f0 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f 70 65  fine sqlite3_ope
276f1 6e 31 36 20 20 20 20 20 20 20 20 20 20 20 20 20  n16             
276f2 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
276f3 3e 6f 70 65 6e 31 36 0a 23 64 65 66 69 6e 65 20  >open16.#define 
276f4 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 20  sqlite3_prepare 
276f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
276f6 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65 70  qlite3_api->prep
276f7 61 72 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  are.#define sqli
276f8 74 65 33 5f 70 72 65 70 61 72 65 31 36 20 20 20  te3_prepare16   
276f9 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
276fa 65 33 5f 61 70 69 2d 3e 70 72 65 70 61 72 65 31  e3_api->prepare1
276fb 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  6.#define sqlite
276fc 33 5f 70 72 65 70 61 72 65 5f 76 32 20 20 20 20  3_prepare_v2    
276fd 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
276fe 5f 61 70 69 2d 3e 70 72 65 70 61 72 65 5f 76 32  _api->prepare_v2
276ff 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
27700 5f 70 72 65 70 61 72 65 31 36 5f 76 32 20 20 20  _prepare16_v2   
27701 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
27702 61 70 69 2d 3e 70 72 65 70 61 72 65 31 36 5f 76  api->prepare16_v
27703 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  2.#define sqlite
27704 33 5f 70 72 6f 66 69 6c 65 20 20 20 20 20 20 20  3_profile       
27705 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
27706 5f 61 70 69 2d 3e 70 72 6f 66 69 6c 65 0a 23 64  _api->profile.#d
27707 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72  efine sqlite3_pr
27708 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72 20 20  ogress_handler  
27709 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
2770a 2d 3e 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c  ->progress_handl
2770b 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  er.#define sqlit
2770c 65 33 5f 72 65 61 6c 6c 6f 63 20 20 20 20 20 20  e3_realloc      
2770d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2770e 33 5f 61 70 69 2d 3e 72 65 61 6c 6c 6f 63 0a 23  3_api->realloc.#
2770f 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
27710 65 73 65 74 20 20 20 20 20 20 20 20 20 20 20 20  eset            
27711 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
27712 69 2d 3e 72 65 73 65 74 0a 23 64 65 66 69 6e 65  i->reset.#define
27713 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
27714 62 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20 20  blob            
27715 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
27716 75 6c 74 5f 62 6c 6f 62 0a 23 64 65 66 69 6e 65  ult_blob.#define
27717 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
27718 64 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20 20  double          
27719 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
2771a 75 6c 74 5f 64 6f 75 62 6c 65 0a 23 64 65 66 69  ult_double.#defi
2771b 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ne sqlite3_resul
2771c 74 5f 65 72 72 6f 72 20 20 20 20 20 20 20 20 20  t_error         
2771d 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
2771e 65 73 75 6c 74 5f 65 72 72 6f 72 0a 23 64 65 66  esult_error.#def
2771f 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ine sqlite3_resu
27720 6c 74 5f 65 72 72 6f 72 31 36 20 20 20 20 20 20  lt_error16      
27721 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
27722 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 0a 23  result_error16.#
27723 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
27724 65 73 75 6c 74 5f 69 6e 74 20 20 20 20 20 20 20  esult_int       
27725 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
27726 69 2d 3e 72 65 73 75 6c 74 5f 69 6e 74 0a 23 64  i->result_int.#d
27727 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
27728 73 75 6c 74 5f 69 6e 74 36 34 20 20 20 20 20 20  sult_int64      
27729 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
2772a 2d 3e 72 65 73 75 6c 74 5f 69 6e 74 36 34 0a 23  ->result_int64.#
2772b 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
2772c 65 73 75 6c 74 5f 6e 75 6c 6c 20 20 20 20 20 20  esult_null      
2772d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
2772e 69 2d 3e 72 65 73 75 6c 74 5f 6e 75 6c 6c 0a 23  i->result_null.#
2772f 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
27730 65 73 75 6c 74 5f 74 65 78 74 20 20 20 20 20 20  esult_text      
27731 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
27732 69 2d 3e 72 65 73 75 6c 74 5f 74 65 78 74 0a 23  i->result_text.#
27733 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
27734 65 73 75 6c 74 5f 74 65 78 74 31 36 20 20 20 20  esult_text16    
27735 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
27736 69 2d 3e 72 65 73 75 6c 74 5f 74 65 78 74 31 36  i->result_text16
27737 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
27738 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65  _result_text16be
27739 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
2773a 61 70 69 2d 3e 72 65 73 75 6c 74 5f 74 65 78 74  api->result_text
2773b 31 36 62 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  16be.#define sql
2773c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
2773d 31 36 6c 65 20 20 20 20 20 20 20 20 73 71 6c 69  16le        sqli
2773e 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f  te3_api->result_
2773f 74 65 78 74 31 36 6c 65 0a 23 64 65 66 69 6e 65  text16le.#define
27740 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
27741 76 61 6c 75 65 20 20 20 20 20 20 20 20 20 20 20  value           
27742 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
27743 75 6c 74 5f 76 61 6c 75 65 0a 23 64 65 66 69 6e  ult_value.#defin
27744 65 20 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61  e sqlite3_rollba
27745 63 6b 5f 68 6f 6f 6b 20 20 20 20 20 20 20 20 20  ck_hook         
27746 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 6f   sqlite3_api->ro
27747 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 0a 23 64 65 66  llback_hook.#def
27748 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f  ine sqlite3_set_
27749 61 75 74 68 6f 72 69 7a 65 72 20 20 20 20 20 20  authorizer      
2774a 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
2774b 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 0a 23  set_authorizer.#
2774c 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73  define sqlite3_s
2774d 65 74 5f 61 75 78 64 61 74 61 20 20 20 20 20 20  et_auxdata      
2774e 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
2774f 69 2d 3e 73 65 74 5f 61 75 78 64 61 74 61 0a 23  i->set_auxdata.#
27750 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73  define sqlite3_s
27751 6e 70 72 69 6e 74 66 20 20 20 20 20 20 20 20 20  nprintf         
27752 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
27753 69 2d 3e 73 6e 70 72 69 6e 74 66 0a 23 64 65 66  i->snprintf.#def
27754 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70  ine sqlite3_step
27755 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27756 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
27757 73 74 65 70 0a 23 64 65 66 69 6e 65 20 73 71 6c  step.#define sql
27758 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d  ite3_table_colum
27759 6e 5f 6d 65 74 61 64 61 74 61 20 20 73 71 6c 69  n_metadata  sqli
2775a 74 65 33 5f 61 70 69 2d 3e 74 61 62 6c 65 5f 63  te3_api->table_c
2775b 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 0a 23  olumn_metadata.#
2775c 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74  define sqlite3_t
2775d 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 20 20 20  hread_cleanup   
2775e 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
2775f 69 2d 3e 74 68 72 65 61 64 5f 63 6c 65 61 6e 75  i->thread_cleanu
27760 70 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  p.#define sqlite
27761 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 20  3_total_changes 
27762 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
27763 5f 61 70 69 2d 3e 74 6f 74 61 6c 5f 63 68 61 6e  _api->total_chan
27764 67 65 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ges.#define sqli
27765 74 65 33 5f 74 72 61 63 65 20 20 20 20 20 20 20  te3_trace       
27766 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
27767 65 33 5f 61 70 69 2d 3e 74 72 61 63 65 0a 23 69  e3_api->trace.#i
27768 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
27769 54 5f 44 45 50 52 45 43 41 54 45 44 0a 23 64 65  T_DEPRECATED.#de
2776a 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 72 61  fine sqlite3_tra
2776b 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 20 20  nsfer_bindings  
2776c 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
2776d 3e 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e  >transfer_bindin
2776e 67 73 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e  gs.#endif.#defin
2776f 65 20 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65  e sqlite3_update
27770 5f 68 6f 6f 6b 20 20 20 20 20 20 20 20 20 20 20  _hook           
27771 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 75 70   sqlite3_api->up
27772 64 61 74 65 5f 68 6f 6f 6b 0a 23 64 65 66 69 6e  date_hook.#defin
27773 65 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64  e sqlite3_user_d
27774 61 74 61 20 20 20 20 20 20 20 20 20 20 20 20 20  ata             
27775 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 75 73   sqlite3_api->us
27776 65 72 5f 64 61 74 61 0a 23 64 65 66 69 6e 65 20  er_data.#define 
27777 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
27778 6f 62 20 20 20 20 20 20 20 20 20 20 20 20 20 73  ob             s
27779 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75  qlite3_api->valu
2777a 65 5f 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73  e_blob.#define s
2777b 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
2777c 65 73 20 20 20 20 20 20 20 20 20 20 20 20 73 71  es            sq
2777d 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65  lite3_api->value
2777e 5f 62 79 74 65 73 0a 23 64 65 66 69 6e 65 20 73  _bytes.#define s
2777f 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
27780 65 73 31 36 20 20 20 20 20 20 20 20 20 20 73 71  es16          sq
27781 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65  lite3_api->value
27782 5f 62 79 74 65 73 31 36 0a 23 64 65 66 69 6e 65  _bytes16.#define
27783 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
27784 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20 20 20  ouble           
27785 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c  sqlite3_api->val
27786 75 65 5f 64 6f 75 62 6c 65 0a 23 64 65 66 69 6e  ue_double.#defin
27787 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  e sqlite3_value_
27788 69 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20  int             
27789 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61   sqlite3_api->va
2778a 6c 75 65 5f 69 6e 74 0a 23 64 65 66 69 6e 65 20  lue_int.#define 
2778b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
2778c 74 36 34 20 20 20 20 20 20 20 20 20 20 20 20 73  t64            s
2778d 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75  qlite3_api->valu
2778e 65 5f 69 6e 74 36 34 0a 23 64 65 66 69 6e 65 20  e_int64.#define 
2778f 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75  sqlite3_value_nu
27790 6d 65 72 69 63 5f 74 79 70 65 20 20 20 20 20 73  meric_type     s
27791 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75  qlite3_api->valu
27792 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 0a 23  e_numeric_type.#
27793 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
27794 61 6c 75 65 5f 74 65 78 74 20 20 20 20 20 20 20  alue_text       
27795 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
27796 69 2d 3e 76 61 6c 75 65 5f 74 65 78 74 0a 23 64  i->value_text.#d
27797 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61  efine sqlite3_va
27798 6c 75 65 5f 74 65 78 74 31 36 20 20 20 20 20 20  lue_text16      
27799 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
2779a 2d 3e 76 61 6c 75 65 5f 74 65 78 74 31 36 0a 23  ->value_text16.#
2779b 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
2779c 61 6c 75 65 5f 74 65 78 74 31 36 62 65 20 20 20  alue_text16be   
2779d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
2779e 69 2d 3e 76 61 6c 75 65 5f 74 65 78 74 31 36 62  i->value_text16b
2779f 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
277a0 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65  3_value_text16le
277a1 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
277a2 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 74 65 78 74  _api->value_text
277a3 31 36 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  16le.#define sql
277a4 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
277a5 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
277a6 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 74  te3_api->value_t
277a7 79 70 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ype.#define sqli
277a8 74 65 33 5f 76 6d 70 72 69 6e 74 66 20 20 20 20  te3_vmprintf    
277a9 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
277aa 65 33 5f 61 70 69 2d 3e 76 6d 70 72 69 6e 74 66  e3_api->vmprintf
277ab 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
277ac 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69  _overload_functi
277ad 6f 6e 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  on      sqlite3_
277ae 61 70 69 2d 3e 6f 76 65 72 6c 6f 61 64 5f 66 75  api->overload_fu
277af 6e 63 74 69 6f 6e 0a 23 64 65 66 69 6e 65 20 73  nction.#define s
277b0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
277b1 32 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  2             sq
277b2 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61  lite3_api->prepa
277b3 72 65 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71  re_v2.#define sq
277b4 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f  lite3_prepare16_
277b5 76 32 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  v2           sql
277b6 69 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61 72  ite3_api->prepar
277b7 65 31 36 5f 76 32 0a 23 64 65 66 69 6e 65 20 73  e16_v2.#define s
277b8 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e  qlite3_clear_bin
277b9 64 69 6e 67 73 20 20 20 20 20 20 20 20 20 73 71  dings         sq
277ba 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6c 65 61 72  lite3_api->clear
277bb 5f 62 69 6e 64 69 6e 67 73 0a 23 64 65 66 69 6e  _bindings.#defin
277bc 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a  e sqlite3_bind_z
277bd 65 72 6f 62 6c 6f 62 20 20 20 20 20 20 20 20 20  eroblob         
277be 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69   sqlite3_api->bi
277bf 6e 64 5f 7a 65 72 6f 62 6c 6f 62 0a 23 64 65 66  nd_zeroblob.#def
277c0 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ine sqlite3_blob
277c1 5f 62 79 74 65 73 20 20 20 20 20 20 20 20 20 20  _bytes          
277c2 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
277c3 62 6c 6f 62 5f 62 79 74 65 73 0a 23 64 65 66 69  blob_bytes.#defi
277c4 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  ne sqlite3_blob_
277c5 63 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20 20  close           
277c6 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
277c7 6c 6f 62 5f 63 6c 6f 73 65 0a 23 64 65 66 69 6e  lob_close.#defin
277c8 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f  e sqlite3_blob_o
277c9 70 65 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  pen             
277ca 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 6c   sqlite3_api->bl
277cb 6f 62 5f 6f 70 65 6e 0a 23 64 65 66 69 6e 65 20  ob_open.#define 
277cc 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61  sqlite3_blob_rea
277cd 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  d              s
277ce 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 6c 6f 62  qlite3_api->blob
277cf 5f 72 65 61 64 0a 23 64 65 66 69 6e 65 20 73 71  _read.#define sq
277d0 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65  lite3_blob_write
277d1 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
277d2 69 74 65 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 77  ite3_api->blob_w
277d3 72 69 74 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  rite.#define sql
277d4 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
277d5 61 74 69 6f 6e 5f 76 32 20 20 20 20 73 71 6c 69  ation_v2    sqli
277d6 74 65 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f  te3_api->create_
277d7 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 0a 23 64 65  collation_v2.#de
277d8 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 69 6c  fine sqlite3_fil
277d9 65 5f 63 6f 6e 74 72 6f 6c 20 20 20 20 20 20 20  e_control       
277da 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
277db 3e 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 0a 23 64  >file_control.#d
277dc 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 65  efine sqlite3_me
277dd 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 20 20  mory_highwater  
277de 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
277df 2d 3e 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74  ->memory_highwat
277e0 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  er.#define sqlit
277e1 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 20 20  e3_memory_used  
277e2 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
277e3 33 5f 61 70 69 2d 3e 6d 65 6d 6f 72 79 5f 75 73  3_api->memory_us
277e4 65 64 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ed.#define sqlit
277e5 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 20 20  e3_mutex_alloc  
277e6 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
277e7 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 61 6c 6c  3_api->mutex_all
277e8 6f 63 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  oc.#define sqlit
277e9 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 20 20  e3_mutex_enter  
277ea 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
277eb 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 65 6e 74  3_api->mutex_ent
277ec 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  er.#define sqlit
277ed 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 20 20 20  e3_mutex_free   
277ee 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
277ef 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 66 72 65  3_api->mutex_fre
277f0 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
277f1 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 20 20 20  3_mutex_leave   
277f2 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
277f3 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 6c 65 61 76  _api->mutex_leav
277f4 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
277f5 33 5f 6d 75 74 65 78 5f 74 72 79 20 20 20 20 20  3_mutex_try     
277f6 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
277f7 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 74 72 79 0a  _api->mutex_try.
277f8 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
277f9 6f 70 65 6e 5f 76 32 20 20 20 20 20 20 20 20 20  open_v2         
277fa 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
277fb 70 69 2d 3e 6f 70 65 6e 5f 76 32 0a 23 64 65 66  pi->open_v2.#def
277fc 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65  ine sqlite3_rele
277fd 61 73 65 5f 6d 65 6d 6f 72 79 20 20 20 20 20 20  ase_memory      
277fe 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
277ff 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 0a 23  release_memory.#
27800 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
27801 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65  esult_error_nome
27802 6d 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70  m     sqlite3_ap
27803 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  i->result_error_
27804 6e 6f 6d 65 6d 0a 23 64 65 66 69 6e 65 20 73 71  nomem.#define sq
27805 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
27806 6f 72 5f 74 6f 6f 62 69 67 20 20 20 20 73 71 6c  or_toobig    sql
27807 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74  ite3_api->result
27808 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 0a 23 64  _error_toobig.#d
27809 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 6c  efine sqlite3_sl
2780a 65 65 70 20 20 20 20 20 20 20 20 20 20 20 20 20  eep             
2780b 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
2780c 2d 3e 73 6c 65 65 70 0a 23 64 65 66 69 6e 65 20  ->sleep.#define 
2780d 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61  sqlite3_soft_hea
2780e 70 5f 6c 69 6d 69 74 20 20 20 20 20 20 20 20 73  p_limit        s
2780f 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 6f 66 74  qlite3_api->soft
27810 5f 68 65 61 70 5f 6c 69 6d 69 74 0a 23 64 65 66  _heap_limit.#def
27811 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f  ine sqlite3_vfs_
27812 66 69 6e 64 20 20 20 20 20 20 20 20 20 20 20 20  find            
27813 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
27814 76 66 73 5f 66 69 6e 64 0a 23 64 65 66 69 6e 65  vfs_find.#define
27815 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67   sqlite3_vfs_reg
27816 69 73 74 65 72 20 20 20 20 20 20 20 20 20 20 20  ister           
27817 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 66 73  sqlite3_api->vfs
27818 5f 72 65 67 69 73 74 65 72 0a 23 64 65 66 69 6e  _register.#defin
27819 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e  e sqlite3_vfs_un
2781a 72 65 67 69 73 74 65 72 20 20 20 20 20 20 20 20  register        
2781b 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 66   sqlite3_api->vf
2781c 73 5f 75 6e 72 65 67 69 73 74 65 72 0a 23 64 65  s_unregister.#de
2781d 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 68 72  fine sqlite3_thr
2781e 65 61 64 73 61 66 65 20 20 20 20 20 20 20 20 20  eadsafe         
2781f 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
27820 3e 78 74 68 72 65 61 64 73 61 66 65 0a 23 64 65  >xthreadsafe.#de
27821 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73  fine sqlite3_res
27822 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 20 20 20 20  ult_zeroblob    
27823 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
27824 3e 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62  >result_zeroblob
27825 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
27826 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f  _result_error_co
27827 64 65 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  de      sqlite3_
27828 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f  api->result_erro
27829 72 5f 63 6f 64 65 0a 23 64 65 66 69 6e 65 20 73  r_code.#define s
2782a 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74  qlite3_test_cont
2782b 72 6f 6c 20 20 20 20 20 20 20 20 20 20 20 73 71  rol           sq
2782c 6c 69 74 65 33 5f 61 70 69 2d 3e 74 65 73 74 5f  lite3_api->test_
2782d 63 6f 6e 74 72 6f 6c 0a 23 64 65 66 69 6e 65 20  control.#define 
2782e 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65  sqlite3_randomne
2782f 73 73 20 20 20 20 20 20 20 20 20 20 20 20 20 73  ss             s
27830 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 61 6e 64  qlite3_api->rand
27831 6f 6d 6e 65 73 73 0a 23 64 65 66 69 6e 65 20 73  omness.#define s
27832 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
27833 62 5f 68 61 6e 64 6c 65 20 20 20 20 20 20 73 71  b_handle      sq
27834 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6e 74 65  lite3_api->conte
27835 78 74 5f 64 62 5f 68 61 6e 64 6c 65 0a 23 64 65  xt_db_handle.#de
27836 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 78 74  fine sqlite3_ext
27837 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64  ended_result_cod
27838 65 73 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d  es  sqlite3_api-
27839 3e 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74  >extended_result
2783a 5f 63 6f 64 65 73 0a 23 64 65 66 69 6e 65 20 73  _codes.#define s
2783b 71 6c 69 74 65 33 5f 6c 69 6d 69 74 20 20 20 20  qlite3_limit    
2783c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
2783d 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 69 6d 69 74  lite3_api->limit
2783e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
2783f 5f 6e 65 78 74 5f 73 74 6d 74 20 20 20 20 20 20  _next_stmt      
27840 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
27841 61 70 69 2d 3e 6e 65 78 74 5f 73 74 6d 74 0a 23  api->next_stmt.#
27842 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73  define sqlite3_s
27843 71 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ql              
27844 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
27845 69 2d 3e 73 71 6c 0a 23 64 65 66 69 6e 65 20 73  i->sql.#define s
27846 71 6c 69 74 65 33 5f 73 74 61 74 75 73 20 20 20  qlite3_status   
27847 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
27848 6c 69 74 65 33 5f 61 70 69 2d 3e 73 74 61 74 75  lite3_api->statu
27849 73 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  s.#endif /* SQLI
2784a 54 45 5f 43 4f 52 45 20 2a 2f 0a 0a 23 64 65 66  TE_CORE */..#def
2784b 69 6e 65 20 53 51 4c 49 54 45 5f 45 58 54 45 4e  ine SQLITE_EXTEN
2784c 53 49 4f 4e 5f 49 4e 49 54 31 20 20 20 20 20 63  SION_INIT1     c
2784d 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69  onst sqlite3_api
2784e 5f 72 6f 75 74 69 6e 65 73 20 2a 73 71 6c 69 74  _routines *sqlit
2784f 65 33 5f 61 70 69 20 3d 20 30 3b 0a 23 64 65 66  e3_api = 0;.#def
27850 69 6e 65 20 53 51 4c 49 54 45 5f 45 58 54 45 4e  ine SQLITE_EXTEN
27851 53 49 4f 4e 5f 49 4e 49 54 32 28 76 29 20 20 73  SION_INIT2(v)  s
27852 71 6c 69 74 65 33 5f 61 70 69 20 3d 20 76 3b 0a  qlite3_api = v;.
27853 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51 4c 49  .#endif /* _SQLI
27854 54 45 33 45 58 54 5f 48 5f 20 2a 2f 0a 0a 2f 2a  TE3EXT_H_ */../*
27855 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
27856 64 20 6f 66 20 73 71 6c 69 74 65 33 65 78 74 2e  d of sqlite3ext.
27857 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
27858 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27859 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2785a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f  ************* Co
2785b 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77  ntinuing where w
2785c 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6c 6f  e left off in lo
2785d 61 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  adext.c ********
2785e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23  ************/..#
2785f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
27860 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f  IT_LOAD_EXTENSIO
27861 4e 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 41 50  N../*.** Some AP
27862 49 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 6f  I routines are o
27863 6d 69 74 74 65 64 20 77 68 65 6e 20 76 61 72 69  mitted when vari
27864 6f 75 73 20 66 65 61 74 75 72 65 73 20 61 72 65  ous features are
27865 0a 2a 2a 20 65 78 63 6c 75 64 65 64 20 66 72 6f  .** excluded fro
27866 6d 20 61 20 62 75 69 6c 64 20 6f 66 20 53 51 4c  m a build of SQL
27867 69 74 65 2e 20 20 53 75 62 73 74 69 74 75 74 65  ite.  Substitute
27868 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a   a NULL pointer.
27869 2a 2a 20 66 6f 72 20 61 6e 79 20 6d 69 73 73 69  ** for any missi
2786a 6e 67 20 41 50 49 73 2e 0a 2a 2f 0a 23 69 66 6e  ng APIs..*/.#ifn
2786b 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
2786c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54  E_COLUMN_METADAT
2786d 41 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  A.# define sqlit
2786e 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61  e3_column_databa
2786f 73 65 5f 6e 61 6d 65 20 20 20 30 0a 23 20 64 65  se_name   0.# de
27870 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
27871 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d  umn_database_nam
27872 65 31 36 20 30 0a 23 20 64 65 66 69 6e 65 20 73  e16 0.# define s
27873 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61  qlite3_column_ta
27874 62 6c 65 5f 6e 61 6d 65 20 20 20 20 20 20 30 0a  ble_name      0.
27875 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
27876 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  _column_table_na
27877 6d 65 31 36 20 20 20 20 30 0a 23 20 64 65 66 69  me16    0.# defi
27878 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
27879 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 20 20 20  n_origin_name   
2787a 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
2787b 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67  ite3_column_orig
2787c 69 6e 5f 6e 61 6d 65 31 36 20 20 20 30 0a 23 20  in_name16   0.# 
2787d 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74  define sqlite3_t
2787e 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61  able_column_meta
2787f 64 61 74 61 20 20 30 0a 23 65 6e 64 69 66 0a 0a  data  0.#endif..
27880 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
27881 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
27882 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
27883 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72  3_set_authorizer
27884 20 20 20 20 20 20 20 20 20 30 0a 23 65 6e 64 69           0.#endi
27885 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
27886 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 23 20 64 65  _OMIT_UTF16.# de
27887 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e  fine sqlite3_bin
27888 64 5f 74 65 78 74 31 36 20 20 20 20 20 20 20 20  d_text16        
27889 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
2788a 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
2788b 5f 6e 65 65 64 65 64 31 36 20 20 20 20 20 30 0a  _needed16     0.
2788c 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
2788d 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65  _column_decltype
2788e 31 36 20 20 20 20 20 20 30 0a 23 20 64 65 66 69  16      0.# defi
2788f 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
27890 6e 5f 6e 61 6d 65 31 36 20 20 20 20 20 20 20 20  n_name16        
27891 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
27892 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
27893 31 36 20 20 20 20 20 20 20 20 20 20 30 0a 23 20  16          0.# 
27894 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
27895 6f 6d 70 6c 65 74 65 31 36 20 20 20 20 20 20 20  omplete16       
27896 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
27897 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
27898 63 6f 6c 6c 61 74 69 6f 6e 31 36 20 20 20 20 20  collation16     
27899 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
2789a 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
2789b 6f 6e 31 36 20 20 20 20 20 20 30 0a 23 20 64 65  on16      0.# de
2789c 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 72 72  fine sqlite3_err
2789d 6d 73 67 31 36 20 20 20 20 20 20 20 20 20 20 20  msg16           
2789e 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
2789f 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 20 20 20  qlite3_open16   
278a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a                0.
278a1 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
278a2 5f 70 72 65 70 61 72 65 31 36 20 20 20 20 20 20  _prepare16      
278a3 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69          0.# defi
278a4 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  ne sqlite3_prepa
278a5 72 65 31 36 5f 76 32 20 20 20 20 20 20 20 20 20  re16_v2         
278a6 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
278a7 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
278a8 72 31 36 20 20 20 20 20 20 20 20 20 30 0a 23 20  r16         0.# 
278a9 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
278aa 65 73 75 6c 74 5f 74 65 78 74 31 36 20 20 20 20  esult_text16    
278ab 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
278ac 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
278ad 74 65 78 74 31 36 62 65 20 20 20 20 20 20 20 20  text16be        
278ae 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
278af 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36  e3_result_text16
278b0 6c 65 20 20 20 20 20 20 20 20 30 0a 23 20 64 65  le        0.# de
278b1 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  fine sqlite3_val
278b2 75 65 5f 74 65 78 74 31 36 20 20 20 20 20 20 20  ue_text16       
278b3 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
278b4 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
278b5 74 31 36 62 65 20 20 20 20 20 20 20 20 20 30 0a  t16be         0.
278b6 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
278b7 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 20  _value_text16le 
278b8 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69          0.# defi
278b9 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
278ba 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31  n_database_name1
278bb 36 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  6 0.# define sql
278bc 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c  ite3_column_tabl
278bd 65 5f 6e 61 6d 65 31 36 20 20 20 20 30 0a 23 20  e_name16    0.# 
278be 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
278bf 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d  olumn_origin_nam
278c0 65 31 36 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a  e16   0.#endif..
278c1 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
278c2 49 54 5f 43 4f 4d 50 4c 45 54 45 0a 23 20 64 65  IT_COMPLETE.# de
278c3 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d  fine sqlite3_com
278c4 70 6c 65 74 65 20 30 0a 23 20 64 65 66 69 6e 65  plete 0.# define
278c5 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
278c6 65 31 36 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69  e16 0.#endif..#i
278c7 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
278c8 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41  _PROGRESS_CALLBA
278c9 43 4b 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  CK.# define sqli
278ca 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e  te3_progress_han
278cb 64 6c 65 72 20 30 0a 23 65 6e 64 69 66 0a 0a 23  dler 0.#endif..#
278cc 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
278cd 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 23  T_VIRTUALTABLE.#
278ce 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
278cf 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 20 30 0a  create_module 0.
278d0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
278d1 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76  _create_module_v
278d2 32 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  2 0.# define sql
278d3 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61  ite3_declare_vta
278d4 62 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  b 0.#endif..#ifd
278d5 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
278d6 48 41 52 45 44 5f 43 41 43 48 45 0a 23 20 64 65  HARED_CACHE.# de
278d7 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 6e 61  fine sqlite3_ena
278d8 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65  ble_shared_cache
278d9 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65   0.#endif..#ifde
278da 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
278db 41 43 45 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ACE.# define sql
278dc 69 74 65 33 5f 70 72 6f 66 69 6c 65 20 20 20 20  ite3_profile    
278dd 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
278de 6c 69 74 65 33 5f 74 72 61 63 65 20 20 20 20 20  lite3_trace     
278df 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69      0.#endif..#i
278e0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
278e1 5f 47 45 54 5f 54 41 42 4c 45 0a 23 20 64 65 66  _GET_TABLE.# def
278e2 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ine sqlite3_free
278e3 5f 74 61 62 6c 65 20 20 20 20 30 0a 23 20 64 65  _table    0.# de
278e4 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 65 74  fine sqlite3_get
278e5 5f 74 61 62 6c 65 20 20 20 20 20 30 0a 23 65 6e  _table     0.#en
278e6 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
278e7 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42  TE_OMIT_INCRBLOB
278e8 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
278e9 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 20 20  _bind_zeroblob  
278ea 30 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  0.#define sqlite
278eb 33 5f 62 6c 6f 62 5f 62 79 74 65 73 20 20 20 20  3_blob_bytes    
278ec 20 30 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74   0.#define sqlit
278ed 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 20 20 20  e3_blob_close   
278ee 20 20 30 0a 23 64 65 66 69 6e 65 20 73 71 6c 69    0.#define sqli
278ef 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 20 20  te3_blob_open   
278f0 20 20 20 30 0a 23 64 65 66 69 6e 65 20 73 71 6c     0.#define sql
278f1 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 20 20  ite3_blob_read  
278f2 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 73 71      0.#define sq
278f3 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65  lite3_blob_write
278f4 20 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a 2f       0.#endif../
278f5 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
278f6 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e  ng structure con
278f7 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 73 20 74  tains pointers t
278f8 6f 20 61 6c 6c 20 53 51 4c 69 74 65 20 41 50 49  o all SQLite API
278f9 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 41 20   routines..** A 
278fa 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20  pointer to this 
278fb 73 74 72 75 63 74 75 72 65 20 69 73 20 70 61 73  structure is pas
278fc 73 65 64 20 69 6e 74 6f 20 65 78 74 65 6e 73 69  sed into extensi
278fd 6f 6e 73 20 77 68 65 6e 20 74 68 65 79 20 61 72  ons when they ar
278fe 65 0a 2a 2a 20 6c 6f 61 64 65 64 20 73 6f 20 74  e.** loaded so t
278ff 68 61 74 20 74 68 65 20 65 78 74 65 6e 73 69 6f  hat the extensio
27900 6e 20 63 61 6e 20 6d 61 6b 65 20 63 61 6c 6c 73  n can make calls
27901 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 53   back into the S
27902 51 4c 69 74 65 0a 2a 2a 20 6c 69 62 72 61 72 79  QLite.** library
27903 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 64 64  ..**.** When add
27904 69 6e 67 20 6e 65 77 20 41 50 49 73 2c 20 61 64  ing new APIs, ad
27905 64 20 74 68 65 6d 20 74 6f 20 74 68 65 20 62 6f  d them to the bo
27906 74 74 6f 6d 20 6f 66 20 74 68 69 73 20 73 74 72  ttom of this str
27907 75 63 74 75 72 65 0a 2a 2a 20 69 6e 20 6f 72 64  ucture.** in ord
27908 65 72 20 74 6f 20 70 72 65 73 65 72 76 65 20 62  er to preserve b
27909 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69  ackwards compati
2790a 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 45 78  bility..**.** Ex
2790b 74 65 6e 73 69 6f 6e 73 20 74 68 61 74 20 75 73  tensions that us
2790c 65 20 6e 65 77 65 72 20 41 50 49 73 20 73 68 6f  e newer APIs sho
2790d 75 6c 64 20 66 69 72 73 74 20 63 61 6c 6c 20 74  uld first call t
2790e 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 69  he.** sqlite3_li
2790f 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28  bversion_number(
27910 29 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  ) to make sure t
27911 68 61 74 20 74 68 65 20 41 50 49 20 74 68 65 79  hat the API they
27912 0a 2a 2a 20 69 6e 74 65 6e 64 20 74 6f 20 75 73  .** intend to us
27913 65 20 69 73 20 73 75 70 70 6f 72 74 65 64 20 62  e is supported b
27914 79 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 20  y the library.  
27915 45 78 74 65 6e 73 69 6f 6e 73 20 73 68 6f 75 6c  Extensions shoul
27916 64 0a 2a 2a 20 61 6c 73 6f 20 63 68 65 63 6b 20  d.** also check 
27917 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
27918 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f  t the pointer to
27919 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73   the function is
2791a 0a 2a 2a 20 6e 6f 74 20 4e 55 4c 4c 20 62 65 66  .** not NULL bef
2791b 6f 72 65 20 63 61 6c 6c 69 6e 67 20 69 74 2e 0a  ore calling it..
2791c 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
2791d 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74  sqlite3_api_rout
2791e 69 6e 65 73 20 73 71 6c 69 74 65 33 41 70 69 73  ines sqlite3Apis
2791f 20 3d 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 61   = {.  sqlite3_a
27920 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74  ggregate_context
27921 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ,.#ifndef SQLITE
27922 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
27923 0a 20 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65  .  sqlite3_aggre
27924 67 61 74 65 5f 63 6f 75 6e 74 2c 0a 23 65 6c 73  gate_count,.#els
27925 65 0a 20 20 30 2c 0a 23 65 6e 64 69 66 0a 20 20  e.  0,.#endif.  
27926 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f  sqlite3_bind_blo
27927 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  b,.  sqlite3_bin
27928 64 5f 64 6f 75 62 6c 65 2c 0a 20 20 73 71 6c 69  d_double,.  sqli
27929 74 65 33 5f 62 69 6e 64 5f 69 6e 74 2c 0a 20 20  te3_bind_int,.  
2792a 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
2792b 36 34 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69  64,.  sqlite3_bi
2792c 6e 64 5f 6e 75 6c 6c 2c 0a 20 20 73 71 6c 69 74  nd_null,.  sqlit
2792d 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
2792e 72 5f 63 6f 75 6e 74 2c 0a 20 20 73 71 6c 69 74  r_count,.  sqlit
2792f 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
27930 72 5f 69 6e 64 65 78 2c 0a 20 20 73 71 6c 69 74  r_index,.  sqlit
27931 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
27932 72 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69 74 65  r_name,.  sqlite
27933 33 5f 62 69 6e 64 5f 74 65 78 74 2c 0a 20 20 73  3_bind_text,.  s
27934 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
27935 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69  16,.  sqlite3_bi
27936 6e 64 5f 76 61 6c 75 65 2c 0a 20 20 73 71 6c 69  nd_value,.  sqli
27937 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72  te3_busy_handler
27938 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 75 73 79  ,.  sqlite3_busy
27939 5f 74 69 6d 65 6f 75 74 2c 0a 20 20 73 71 6c 69  _timeout,.  sqli
2793a 74 65 33 5f 63 68 61 6e 67 65 73 2c 0a 20 20 73  te3_changes,.  s
2793b 71 6c 69 74 65 33 5f 63 6c 6f 73 65 2c 0a 20 20  qlite3_close,.  
2793c 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f  sqlite3_collatio
2793d 6e 5f 6e 65 65 64 65 64 2c 0a 20 20 73 71 6c 69  n_needed,.  sqli
2793e 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65  te3_collation_ne
2793f 65 64 65 64 31 36 2c 0a 20 20 73 71 6c 69 74 65  eded16,.  sqlite
27940 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 2c 0a 20  3_column_blob,. 
27941 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
27942 62 79 74 65 73 2c 0a 20 20 73 71 6c 69 74 65 33  bytes,.  sqlite3
27943 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 2c  _column_bytes16,
27944 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
27945 6e 5f 63 6f 75 6e 74 2c 0a 20 20 73 71 6c 69 74  n_count,.  sqlit
27946 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61  e3_column_databa
27947 73 65 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69 74  se_name,.  sqlit
27948 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61  e3_column_databa
27949 73 65 5f 6e 61 6d 65 31 36 2c 0a 20 20 73 71 6c  se_name16,.  sql
2794a 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c  ite3_column_decl
2794b 74 79 70 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  type,.  sqlite3_
2794c 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31  column_decltype1
2794d 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  6,.  sqlite3_col
2794e 75 6d 6e 5f 64 6f 75 62 6c 65 2c 0a 20 20 73 71  umn_double,.  sq
2794f 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
27950 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
27951 6d 6e 5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c 69  mn_int64,.  sqli
27952 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 2c  te3_column_name,
27953 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
27954 6e 5f 6e 61 6d 65 31 36 2c 0a 20 20 73 71 6c 69  n_name16,.  sqli
27955 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69  te3_column_origi
27956 6e 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69 74 65  n_name,.  sqlite
27957 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f  3_column_origin_
27958 6e 61 6d 65 31 36 2c 0a 20 20 73 71 6c 69 74 65  name16,.  sqlite
27959 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e  3_column_table_n
2795a 61 6d 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  ame,.  sqlite3_c
2795b 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65  olumn_table_name
2795c 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  16,.  sqlite3_co
2795d 6c 75 6d 6e 5f 74 65 78 74 2c 0a 20 20 73 71 6c  lumn_text,.  sql
2795e 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
2795f 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  16,.  sqlite3_co
27960 6c 75 6d 6e 5f 74 79 70 65 2c 0a 20 20 73 71 6c  lumn_type,.  sql
27961 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75  ite3_column_valu
27962 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6d  e,.  sqlite3_com
27963 6d 69 74 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69  mit_hook,.  sqli
27964 74 65 33 5f 63 6f 6d 70 6c 65 74 65 2c 0a 20 20  te3_complete,.  
27965 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65  sqlite3_complete
27966 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 72  16,.  sqlite3_cr
27967 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 2c 0a  eate_collation,.
27968 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
27969 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 2c 0a 20 20  _collation16,.  
2796a 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
2796b 75 6e 63 74 69 6f 6e 2c 0a 20 20 73 71 6c 69 74  unction,.  sqlit
2796c 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
2796d 6f 6e 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f  on16,.  sqlite3_
2796e 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 2c 0a 20  create_module,. 
2796f 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f   sqlite3_data_co
27970 75 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 64  unt,.  sqlite3_d
27971 62 5f 68 61 6e 64 6c 65 2c 0a 20 20 73 71 6c 69  b_handle,.  sqli
27972 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62  te3_declare_vtab
27973 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65 6e 61 62  ,.  sqlite3_enab
27974 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 2c  le_shared_cache,
27975 0a 20 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  .  sqlite3_errco
27976 64 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65 72  de,.  sqlite3_er
27977 72 6d 73 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f  rmsg,.  sqlite3_
27978 65 72 72 6d 73 67 31 36 2c 0a 20 20 73 71 6c 69  errmsg16,.  sqli
27979 74 65 33 5f 65 78 65 63 2c 0a 23 69 66 6e 64 65  te3_exec,.#ifnde
2797a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
2797b 50 52 45 43 41 54 45 44 0a 20 20 73 71 6c 69 74  PRECATED.  sqlit
2797c 65 33 5f 65 78 70 69 72 65 64 2c 0a 23 65 6c 73  e3_expired,.#els
2797d 65 0a 20 20 30 2c 0a 23 65 6e 64 69 66 0a 20 20  e.  0,.#endif.  
2797e 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
2797f 2c 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ,.  sqlite3_free
27980 2c 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ,.  sqlite3_free
27981 5f 74 61 62 6c 65 2c 0a 20 20 73 71 6c 69 74 65  _table,.  sqlite
27982 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74  3_get_autocommit
27983 2c 0a 20 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  ,.  sqlite3_get_
27984 61 75 78 64 61 74 61 2c 0a 20 20 73 71 6c 69 74  auxdata,.  sqlit
27985 65 33 5f 67 65 74 5f 74 61 62 6c 65 2c 0a 20 20  e3_get_table,.  
27986 30 2c 20 20 20 20 20 2f 2a 20 57 61 73 20 73 71  0,     /* Was sq
27987 6c 69 74 65 33 5f 67 6c 6f 62 61 6c 5f 72 65 63  lite3_global_rec
27988 6f 76 65 72 28 29 2c 20 62 75 74 20 74 68 61 74  over(), but that
27989 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 65 70   function is dep
2798a 72 65 63 61 74 65 64 20 2a 2f 0a 20 20 73 71 6c  recated */.  sql
2798b 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 2c 0a  ite3_interrupt,.
2798c 20 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69    sqlite3_last_i
2798d 6e 73 65 72 74 5f 72 6f 77 69 64 2c 0a 20 20 73  nsert_rowid,.  s
2798e 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
2798f 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6c 69 62  n,.  sqlite3_lib
27990 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 2c 0a  version_number,.
27991 20 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63    sqlite3_malloc
27992 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  ,.  sqlite3_mpri
27993 6e 74 66 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6f  ntf,.  sqlite3_o
27994 70 65 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6f  pen,.  sqlite3_o
27995 70 65 6e 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  pen16,.  sqlite3
27996 5f 70 72 65 70 61 72 65 2c 0a 20 20 73 71 6c 69  _prepare,.  sqli
27997 74 65 33 5f 70 72 65 70 61 72 65 31 36 2c 0a 20  te3_prepare16,. 
27998 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65   sqlite3_profile
27999 2c 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 6f 67  ,.  sqlite3_prog
2799a 72 65 73 73 5f 68 61 6e 64 6c 65 72 2c 0a 20 20  ress_handler,.  
2799b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 2c  sqlite3_realloc,
2799c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74  .  sqlite3_reset
2799d 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
2799e 6c 74 5f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74  lt_blob,.  sqlit
2799f 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65  e3_result_double
279a0 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
279a1 6c 74 5f 65 72 72 6f 72 2c 0a 20 20 73 71 6c 69  lt_error,.  sqli
279a2 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
279a3 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  16,.  sqlite3_re
279a4 73 75 6c 74 5f 69 6e 74 2c 0a 20 20 73 71 6c 69  sult_int,.  sqli
279a5 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34  te3_result_int64
279a6 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
279a7 6c 74 5f 6e 75 6c 6c 2c 0a 20 20 73 71 6c 69 74  lt_null,.  sqlit
279a8 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2c 0a  e3_result_text,.
279a9 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
279aa 5f 74 65 78 74 31 36 2c 0a 20 20 73 71 6c 69 74  _text16,.  sqlit
279ab 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36  e3_result_text16
279ac 62 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  be,.  sqlite3_re
279ad 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 2c 0a 20  sult_text16le,. 
279ae 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
279af 76 61 6c 75 65 2c 0a 20 20 73 71 6c 69 74 65 33  value,.  sqlite3
279b0 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 2c 0a  _rollback_hook,.
279b1 20 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75    sqlite3_set_au
279b2 74 68 6f 72 69 7a 65 72 2c 0a 20 20 73 71 6c 69  thorizer,.  sqli
279b3 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 2c  te3_set_auxdata,
279b4 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  .  sqlite3_snpri
279b5 6e 74 66 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73  ntf,.  sqlite3_s
279b6 74 65 70 2c 0a 20 20 73 71 6c 69 74 65 33 5f 74  tep,.  sqlite3_t
279b7 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61  able_column_meta
279b8 64 61 74 61 2c 0a 23 69 66 6e 64 65 66 20 53 51  data,.#ifndef SQ
279b9 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43  LITE_OMIT_DEPREC
279ba 41 54 45 44 0a 20 20 73 71 6c 69 74 65 33 5f 74  ATED.  sqlite3_t
279bb 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 2c 0a 23  hread_cleanup,.#
279bc 65 6c 73 65 0a 20 20 30 2c 0a 23 65 6e 64 69 66  else.  0,.#endif
279bd 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c  .  sqlite3_total
279be 5f 63 68 61 6e 67 65 73 2c 0a 20 20 73 71 6c 69  _changes,.  sqli
279bf 74 65 33 5f 74 72 61 63 65 2c 0a 23 69 66 6e 64  te3_trace,.#ifnd
279c0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
279c1 45 50 52 45 43 41 54 45 44 0a 20 20 73 71 6c 69  EPRECATED.  sqli
279c2 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e  te3_transfer_bin
279c3 64 69 6e 67 73 2c 0a 23 65 6c 73 65 0a 20 20 30  dings,.#else.  0
279c4 2c 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74  ,.#endif.  sqlit
279c5 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 2c 0a  e3_update_hook,.
279c6 20 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64    sqlite3_user_d
279c7 61 74 61 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  ata,.  sqlite3_v
279c8 61 6c 75 65 5f 62 6c 6f 62 2c 0a 20 20 73 71 6c  alue_blob,.  sql
279c9 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
279ca 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
279cb 65 5f 62 79 74 65 73 31 36 2c 0a 20 20 73 71 6c  e_bytes16,.  sql
279cc 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
279cd 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  e,.  sqlite3_val
279ce 75 65 5f 69 6e 74 2c 0a 20 20 73 71 6c 69 74 65  ue_int,.  sqlite
279cf 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 2c 0a 20  3_value_int64,. 
279d0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e   sqlite3_value_n
279d1 75 6d 65 72 69 63 5f 74 79 70 65 2c 0a 20 20 73  umeric_type,.  s
279d2 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
279d3 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  t,.  sqlite3_val
279d4 75 65 5f 74 65 78 74 31 36 2c 0a 20 20 73 71 6c  ue_text16,.  sql
279d5 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
279d6 36 62 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  6be,.  sqlite3_v
279d7 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 2c 0a 20  alue_text16le,. 
279d8 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
279d9 79 70 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  ype,.  sqlite3_v
279da 6d 70 72 69 6e 74 66 2c 0a 20 20 2f 2a 0a 20 20  mprintf,.  /*.  
279db 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20  ** The original 
279dc 41 50 49 20 73 65 74 20 65 6e 64 73 20 68 65 72  API set ends her
279dd 65 2e 20 20 41 6c 6c 20 65 78 74 65 6e 73 69 6f  e.  All extensio
279de 6e 73 20 63 61 6e 20 63 61 6c 6c 20 61 6e 79 0a  ns can call any.
279df 20 20 2a 2a 20 6f 66 20 74 68 65 20 41 50 49 73    ** of the APIs
279e0 20 61 62 6f 76 65 20 70 72 6f 76 69 64 65 64 20   above provided 
279e1 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72  that the pointer
279e2 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e 20 20 42   is not NULL.  B
279e3 75 74 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20 63  ut.  ** before c
279e4 61 6c 6c 69 6e 67 20 41 50 49 73 20 74 68 61 74  alling APIs that
279e5 20 66 6f 6c 6c 6f 77 2c 20 65 78 74 65 6e 73 69   follow, extensi
279e6 6f 6e 20 73 68 6f 75 6c 64 20 63 68 65 63 6b 20  on should check 
279e7 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33  the.  ** sqlite3
279e8 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62  _libversion_numb
279e9 65 72 28 29 20 74 6f 20 6d 61 6b 65 20 73 75 72  er() to make sur
279ea 65 20 74 68 65 79 20 61 72 65 20 64 65 61 6c 69  e they are deali
279eb 6e 67 20 77 69 74 68 0a 20 20 2a 2a 20 61 20 6c  ng with.  ** a l
279ec 69 62 72 61 72 79 20 74 68 61 74 20 69 73 20 6e  ibrary that is n
279ed 65 77 20 65 6e 6f 75 67 68 20 74 6f 20 73 75 70  ew enough to sup
279ee 70 6f 72 74 20 74 68 61 74 20 41 50 49 2e 0a 20  port that API.. 
279ef 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
279f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
279f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
279f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
279f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2f 0a  **********.  */.
279f4 20 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f    sqlite3_overlo
279f5 61 64 5f 66 75 6e 63 74 69 6f 6e 2c 0a 0a 20 20  ad_function,..  
279f6 2f 2a 0a 20 20 2a 2a 20 41 64 64 65 64 20 61 66  /*.  ** Added af
279f7 74 65 72 20 33 2e 33 2e 31 33 0a 20 20 2a 2f 0a  ter 3.3.13.  */.
279f8 20 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72    sqlite3_prepar
279f9 65 5f 76 32 2c 0a 20 20 73 71 6c 69 74 65 33 5f  e_v2,.  sqlite3_
279fa 70 72 65 70 61 72 65 31 36 5f 76 32 2c 0a 20 20  prepare16_v2,.  
279fb 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69  sqlite3_clear_bi
279fc 6e 64 69 6e 67 73 2c 0a 0a 20 20 2f 2a 0a 20 20  ndings,..  /*.  
279fd 2a 2a 20 41 64 64 65 64 20 66 6f 72 20 33 2e 34  ** Added for 3.4
279fe 2e 31 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  .1.  */.  sqlite
279ff 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f  3_create_module_
27a00 76 32 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41  v2,..  /*.  ** A
27a01 64 64 65 64 20 66 6f 72 20 33 2e 35 2e 30 0a 20  dded for 3.5.0. 
27a02 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 69   */.  sqlite3_bi
27a03 6e 64 5f 7a 65 72 6f 62 6c 6f 62 2c 0a 20 20 73  nd_zeroblob,.  s
27a04 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65  qlite3_blob_byte
27a05 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f  s,.  sqlite3_blo
27a06 62 5f 63 6c 6f 73 65 2c 0a 20 20 73 71 6c 69 74  b_close,.  sqlit
27a07 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 2c 0a 20 20  e3_blob_open,.  
27a08 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61  sqlite3_blob_rea
27a09 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f  d,.  sqlite3_blo
27a0a 62 5f 77 72 69 74 65 2c 0a 20 20 73 71 6c 69 74  b_write,.  sqlit
27a0b 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
27a0c 69 6f 6e 5f 76 32 2c 0a 20 20 73 71 6c 69 74 65  ion_v2,.  sqlite
27a0d 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 2c 0a  3_file_control,.
27a0e 20 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79    sqlite3_memory
27a0f 5f 68 69 67 68 77 61 74 65 72 2c 0a 20 20 73 71  _highwater,.  sq
27a10 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65  lite3_memory_use
27a11 64 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  d,.#ifdef SQLITE
27a12 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 20 20 30 2c  _MUTEX_OMIT.  0,
27a13 20 0a 20 20 30 2c 20 0a 20 20 30 2c 0a 20 20 30   .  0, .  0,.  0
27a14 2c 0a 20 20 30 2c 0a 23 65 6c 73 65 0a 20 20 73  ,.  0,.#else.  s
27a15 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
27a16 6f 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  oc,.  sqlite3_mu
27a17 74 65 78 5f 65 6e 74 65 72 2c 0a 20 20 73 71 6c  tex_enter,.  sql
27a18 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 2c  ite3_mutex_free,
27a19 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
27a1a 5f 6c 65 61 76 65 2c 0a 20 20 73 71 6c 69 74 65  _leave,.  sqlite
27a1b 33 5f 6d 75 74 65 78 5f 74 72 79 2c 0a 23 65 6e  3_mutex_try,.#en
27a1c 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6f 70  dif.  sqlite3_op
27a1d 65 6e 5f 76 32 2c 0a 20 20 73 71 6c 69 74 65 33  en_v2,.  sqlite3
27a1e 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 2c  _release_memory,
27a1f 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
27a20 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 2c 0a 20  t_error_nomem,. 
27a21 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
27a22 65 72 72 6f 72 5f 74 6f 6f 62 69 67 2c 0a 20 20  error_toobig,.  
27a23 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 2c 0a 20  sqlite3_sleep,. 
27a24 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65   sqlite3_soft_he
27a25 61 70 5f 6c 69 6d 69 74 2c 0a 20 20 73 71 6c 69  ap_limit,.  sqli
27a26 74 65 33 5f 76 66 73 5f 66 69 6e 64 2c 0a 20 20  te3_vfs_find,.  
27a27 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69  sqlite3_vfs_regi
27a28 73 74 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f  ster,.  sqlite3_
27a29 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 2c 0a  vfs_unregister,.
27a2a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65 64  .  /*.  ** Added
27a2b 20 66 6f 72 20 33 2e 35 2e 38 0a 20 20 2a 2f 0a   for 3.5.8.  */.
27a2c 20 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64    sqlite3_thread
27a2d 73 61 66 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  safe,.  sqlite3_
27a2e 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 2c  result_zeroblob,
27a2f 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
27a30 74 5f 65 72 72 6f 72 5f 63 6f 64 65 2c 0a 20 20  t_error_code,.  
27a31 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
27a32 74 72 6f 6c 2c 0a 20 20 73 71 6c 69 74 65 33 5f  trol,.  sqlite3_
27a33 72 61 6e 64 6f 6d 6e 65 73 73 2c 0a 20 20 73 71  randomness,.  sq
27a34 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
27a35 5f 68 61 6e 64 6c 65 2c 0a 0a 20 20 2f 2a 0a 20  _handle,..  /*. 
27a36 20 2a 2a 20 41 64 64 65 64 20 66 6f 72 20 33 2e   ** Added for 3.
27a37 36 2e 30 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  6.0.  */.  sqlit
27a38 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75  e3_extended_resu
27a39 6c 74 5f 63 6f 64 65 73 2c 0a 20 20 73 71 6c 69  lt_codes,.  sqli
27a3a 74 65 33 5f 6c 69 6d 69 74 2c 0a 20 20 73 71 6c  te3_limit,.  sql
27a3b 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 2c 0a  ite3_next_stmt,.
27a3c 20 20 73 71 6c 69 74 65 33 5f 73 71 6c 2c 0a 20    sqlite3_sql,. 
27a3d 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 2c   sqlite3_status,
27a3e 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d  .};../*.** Attem
27a3f 70 74 20 74 6f 20 6c 6f 61 64 20 61 6e 20 53 51  pt to load an SQ
27a40 4c 69 74 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c  Lite extension l
27a41 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 65 64  ibrary contained
27a42 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20   in the file.** 
27a43 7a 46 69 6c 65 2e 20 20 54 68 65 20 65 6e 74 72  zFile.  The entr
27a44 79 20 70 6f 69 6e 74 20 69 73 20 7a 50 72 6f 63  y point is zProc
27a45 2e 20 20 7a 50 72 6f 63 20 6d 61 79 20 62 65 20  .  zProc may be 
27a46 30 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20  0 in which case 
27a47 61 0a 2a 2a 20 64 65 66 61 75 6c 74 20 65 6e 74  a.** default ent
27a48 72 79 20 70 6f 69 6e 74 20 6e 61 6d 65 20 28 73  ry point name (s
27a49 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e  qlite3_extension
27a4a 5f 69 6e 69 74 29 20 69 73 20 75 73 65 64 2e 20  _init) is used. 
27a4b 20 55 73 65 0a 2a 2a 20 6f 66 20 74 68 65 20 64   Use.** of the d
27a4c 65 66 61 75 6c 74 20 6e 61 6d 65 20 69 73 20 72  efault name is r
27a4d 65 63 6f 6d 6d 65 6e 64 65 64 2e 0a 2a 2a 0a 2a  ecommended..**.*
27a4e 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  * Return SQLITE_
27a4f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e  OK on success an
27a50 64 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69  d SQLITE_ERROR i
27a51 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73  f something goes
27a52 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66   wrong..**.** If
27a53 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
27a54 20 61 6e 64 20 70 7a 45 72 72 4d 73 67 20 69 73   and pzErrMsg is
27a55 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 66 69 6c   not 0, then fil
27a56 6c 20 2a 70 7a 45 72 72 4d 73 67 20 77 69 74 68  l *pzErrMsg with
27a57 20 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61   .** error messa
27a58 67 65 20 74 65 78 74 2e 20 20 54 68 65 20 63 61  ge text.  The ca
27a59 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73  lling function s
27a5a 68 6f 75 6c 64 20 66 72 65 65 20 74 68 69 73 20  hould free this 
27a5b 6d 65 6d 6f 72 79 0a 2a 2a 20 62 79 20 63 61 6c  memory.** by cal
27a5c 6c 69 6e 67 20 73 71 6c 69 74 65 33 44 62 46 72  ling sqlite3DbFr
27a5d 65 65 28 64 62 2c 20 29 2e 0a 2a 2f 0a 73 74 61  ee(db, )..*/.sta
27a5e 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4c  tic int sqlite3L
27a5f 6f 61 64 45 78 74 65 6e 73 69 6f 6e 28 0a 20 20  oadExtension(.  
27a60 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
27a61 20 20 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68        /* Load th
27a62 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 74 6f  e extension into
27a63 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 63   this database c
27a64 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63  onnection */.  c
27a65 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
27a66 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  ,    /* Name of 
27a67 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61  the shared libra
27a68 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 65 78  ry containing ex
27a69 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e  tension */.  con
27a6a 73 74 20 63 68 61 72 20 2a 7a 50 72 6f 63 2c 20  st char *zProc, 
27a6b 20 20 20 2f 2a 20 45 6e 74 72 79 20 70 6f 69 6e     /* Entry poin
27a6c 74 2e 20 20 55 73 65 20 22 73 71 6c 69 74 65 33  t.  Use "sqlite3
27a6d 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 22  _extension_init"
27a6e 20 69 66 20 30 20 2a 2f 0a 20 20 63 68 61 72 20   if 0 */.  char 
27a6f 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20 20  **pzErrMsg      
27a70 20 2f 2a 20 50 75 74 20 65 72 72 6f 72 20 6d 65   /* Put error me
27a71 73 73 61 67 65 20 68 65 72 65 20 69 66 20 6e 6f  ssage here if no
27a72 74 20 30 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  t 0 */.){.  sqli
27a73 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20  te3_vfs *pVfs = 
27a74 64 62 2d 3e 70 56 66 73 3b 0a 20 20 76 6f 69 64  db->pVfs;.  void
27a75 20 2a 68 61 6e 64 6c 65 3b 0a 20 20 69 6e 74 20   *handle;.  int 
27a76 28 2a 78 49 6e 69 74 29 28 73 71 6c 69 74 65 33  (*xInit)(sqlite3
27a77 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73 74 20 73  *,char**,const s
27a78 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69  qlite3_api_routi
27a79 6e 65 73 2a 29 3b 0a 20 20 63 68 61 72 20 2a 7a  nes*);.  char *z
27a7a 45 72 72 6d 73 67 20 3d 20 30 3b 0a 20 20 76 6f  Errmsg = 0;.  vo
27a7b 69 64 20 2a 2a 61 48 61 6e 64 6c 65 3b 0a 20 20  id **aHandle;.  
27a7c 63 6f 6e 73 74 20 69 6e 74 20 6e 4d 73 67 20 3d  const int nMsg =
27a7d 20 33 30 30 3b 0a 0a 20 20 69 66 28 20 70 7a 45   300;..  if( pzE
27a7e 72 72 4d 73 67 20 29 20 2a 70 7a 45 72 72 4d 73  rrMsg ) *pzErrMs
27a7f 67 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 69 63  g = 0;..  /* Tic
27a80 6b 65 74 20 23 31 38 36 33 2e 20 20 54 6f 20 61  ket #1863.  To a
27a81 76 6f 69 64 20 61 20 63 72 65 61 74 69 6e 67 20  void a creating 
27a82 73 65 63 75 72 69 74 79 20 70 72 6f 62 6c 65 6d  security problem
27a83 73 20 66 6f 72 20 6f 6c 64 65 72 0a 20 20 2a 2a  s for older.  **
27a84 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68   applications th
27a85 61 74 20 72 65 6c 69 6e 6b 20 61 67 61 69 6e 73  at relink agains
27a86 74 20 6e 65 77 65 72 20 76 65 72 73 69 6f 6e 73  t newer versions
27a87 20 6f 66 20 53 51 4c 69 74 65 2c 20 74 68 65 0a   of SQLite, the.
27a88 20 20 2a 2a 20 61 62 69 6c 69 74 79 20 74 6f 20    ** ability to 
27a89 72 75 6e 20 6c 6f 61 64 5f 65 78 74 65 6e 73 69  run load_extensi
27a8a 6f 6e 20 69 73 20 74 75 72 6e 65 64 20 6f 66 66  on is turned off
27a8b 20 62 79 20 64 65 66 61 75 6c 74 2e 20 20 4f 6e   by default.  On
27a8c 65 0a 20 20 2a 2a 20 6d 75 73 74 20 63 61 6c 6c  e.  ** must call
27a8d 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f   sqlite3_enable_
27a8e 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29  load_extension()
27a8f 20 74 6f 20 74 75 72 6e 20 6f 6e 20 65 78 74 65   to turn on exte
27a90 6e 73 69 6f 6e 0a 20 20 2a 2a 20 6c 6f 61 64 69  nsion.  ** loadi
27a91 6e 67 2e 20 20 4f 74 68 65 72 77 69 73 65 20 79  ng.  Otherwise y
27a92 6f 75 20 67 65 74 20 74 68 65 20 66 6f 6c 6c 6f  ou get the follo
27a93 77 69 6e 67 20 65 72 72 6f 72 2e 0a 20 20 2a 2f  wing error..  */
27a94 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67  .  if( (db->flag
27a95 73 20 26 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45  s & SQLITE_LoadE
27a96 78 74 65 6e 73 69 6f 6e 29 3d 3d 30 20 29 7b 0a  xtension)==0 ){.
27a97 20 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67      if( pzErrMsg
27a98 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72   ){.      *pzErr
27a99 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Msg = sqlite3_mp
27a9a 72 69 6e 74 66 28 22 6e 6f 74 20 61 75 74 68 6f  rintf("not autho
27a9b 72 69 7a 65 64 22 29 3b 0a 20 20 20 20 7d 0a 20  rized");.    }. 
27a9c 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
27a9d 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 69  _ERROR;.  }..  i
27a9e 66 28 20 7a 50 72 6f 63 3d 3d 30 20 29 7b 0a 20  f( zProc==0 ){. 
27a9f 20 20 20 7a 50 72 6f 63 20 3d 20 22 73 71 6c 69     zProc = "sqli
27aa0 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e  te3_extension_in
27aa1 69 74 22 3b 0a 20 20 7d 0a 0a 20 20 68 61 6e 64  it";.  }..  hand
27aa2 6c 65 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 6c  le = sqlite3OsDl
27aa3 4f 70 65 6e 28 70 56 66 73 2c 20 7a 46 69 6c 65  Open(pVfs, zFile
27aa4 29 3b 0a 20 20 69 66 28 20 68 61 6e 64 6c 65 3d  );.  if( handle=
27aa5 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 7a  =0 ){.    if( pz
27aa6 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20  ErrMsg ){.      
27aa7 7a 45 72 72 6d 73 67 20 3d 20 73 71 6c 69 74 65  zErrmsg = sqlite
27aa8 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f 28  3StackAllocZero(
27aa9 64 62 2c 20 6e 4d 73 67 29 3b 0a 20 20 20 20 20  db, nMsg);.     
27aaa 20 69 66 28 20 7a 45 72 72 6d 73 67 20 29 7b 0a   if( zErrmsg ){.
27aab 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
27aac 73 6e 70 72 69 6e 74 66 28 6e 4d 73 67 2c 20 7a  snprintf(nMsg, z
27aad 45 72 72 6d 73 67 2c 20 0a 20 20 20 20 20 20 20  Errmsg, .       
27aae 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20       "unable to 
27aaf 6f 70 65 6e 20 73 68 61 72 65 64 20 6c 69 62 72  open shared libr
27ab0 61 72 79 20 5b 25 73 5d 22 2c 20 7a 46 69 6c 65  ary [%s]", zFile
27ab1 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
27ab2 65 33 4f 73 44 6c 45 72 72 6f 72 28 70 56 66 73  e3OsDlError(pVfs
27ab3 2c 20 6e 4d 73 67 2d 31 2c 20 7a 45 72 72 6d 73  , nMsg-1, zErrms
27ab4 67 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 7a 45  g);.        *pzE
27ab5 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 44  rrMsg = sqlite3D
27ab6 62 53 74 72 44 75 70 28 30 2c 20 7a 45 72 72 6d  bStrDup(0, zErrm
27ab7 73 67 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  sg);.        sql
27ab8 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 64 62  ite3StackFree(db
27ab9 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20  , zErrmsg);.    
27aba 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 72 65    }.    }.    re
27abb 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
27abc 52 3b 0a 20 20 7d 0a 20 20 78 49 6e 69 74 20 3d  R;.  }.  xInit =
27abd 20 28 69 6e 74 28 2a 29 28 73 71 6c 69 74 65 33   (int(*)(sqlite3
27abe 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73 74 20 73  *,char**,const s
27abf 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69  qlite3_api_routi
27ac0 6e 65 73 2a 29 29 0a 20 20 20 20 20 20 20 20 20  nes*)).         
27ac1 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
27ac2 33 4f 73 44 6c 53 79 6d 28 70 56 66 73 2c 20 68  3OsDlSym(pVfs, h
27ac3 61 6e 64 6c 65 2c 20 7a 50 72 6f 63 29 3b 0a 20  andle, zProc);. 
27ac4 20 69 66 28 20 78 49 6e 69 74 3d 3d 30 20 29 7b   if( xInit==0 ){
27ac5 0a 20 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73  .    if( pzErrMs
27ac6 67 20 29 7b 0a 20 20 20 20 20 20 7a 45 72 72 6d  g ){.      zErrm
27ac7 73 67 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63  sg = sqlite3Stac
27ac8 6b 41 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e  kAllocZero(db, n
27ac9 4d 73 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20  Msg);.      if( 
27aca 7a 45 72 72 6d 73 67 20 29 7b 0a 20 20 20 20 20  zErrmsg ){.     
27acb 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
27acc 6e 74 66 28 6e 4d 73 67 2c 20 7a 45 72 72 6d 73  ntf(nMsg, zErrms
27acd 67 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 22  g,.            "
27ace 6e 6f 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 5b  no entry point [
27acf 25 73 5d 20 69 6e 20 73 68 61 72 65 64 20 6c 69  %s] in shared li
27ad0 62 72 61 72 79 20 5b 25 73 5d 22 2c 20 7a 50 72  brary [%s]", zPr
27ad1 6f 63 2c 7a 46 69 6c 65 29 3b 0a 20 20 20 20 20  oc,zFile);.     
27ad2 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72     sqlite3OsDlEr
27ad3 72 6f 72 28 70 56 66 73 2c 20 6e 4d 73 67 2d 31  ror(pVfs, nMsg-1
27ad4 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20  , zErrmsg);.    
27ad5 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20      *pzErrMsg = 
27ad6 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
27ad7 30 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20  0, zErrmsg);.   
27ad8 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 63       sqlite3Stac
27ad9 6b 46 72 65 65 28 64 62 2c 20 7a 45 72 72 6d 73  kFree(db, zErrms
27ada 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  g);.      }.    
27adb 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f    sqlite3OsDlClo
27adc 73 65 28 70 56 66 73 2c 20 68 61 6e 64 6c 65 29  se(pVfs, handle)
27add 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
27ade 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
27adf 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 78 49 6e  .  }else if( xIn
27ae0 69 74 28 64 62 2c 20 26 7a 45 72 72 6d 73 67 2c  it(db, &zErrmsg,
27ae1 20 26 73 71 6c 69 74 65 33 41 70 69 73 29 20 29   &sqlite3Apis) )
27ae2 7b 0a 20 20 20 20 69 66 28 20 70 7a 45 72 72 4d  {.    if( pzErrM
27ae3 73 67 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45  sg ){.      *pzE
27ae4 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 5f  rrMsg = sqlite3_
27ae5 6d 70 72 69 6e 74 66 28 22 65 72 72 6f 72 20 64  mprintf("error d
27ae6 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61  uring initializa
27ae7 74 69 6f 6e 3a 20 25 73 22 2c 20 7a 45 72 72 6d  tion: %s", zErrm
27ae8 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  sg);.    }.    s
27ae9 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72 72  qlite3_free(zErr
27aea 6d 73 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  msg);.    sqlite
27aeb 33 4f 73 44 6c 43 6c 6f 73 65 28 70 56 66 73 2c  3OsDlClose(pVfs,
27aec 20 68 61 6e 64 6c 65 29 3b 0a 20 20 20 20 72 65   handle);.    re
27aed 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
27aee 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 70 70  R;.  }..  /* App
27aef 65 6e 64 20 74 68 65 20 6e 65 77 20 73 68 61 72  end the new shar
27af0 65 64 20 6c 69 62 72 61 72 79 20 68 61 6e 64 6c  ed library handl
27af1 65 20 74 6f 20 74 68 65 20 64 62 2d 3e 61 45 78  e to the db->aEx
27af2 74 65 6e 73 69 6f 6e 20 61 72 72 61 79 2e 20 2a  tension array. *
27af3 2f 0a 20 20 61 48 61 6e 64 6c 65 20 3d 20 73 71  /.  aHandle = sq
27af4 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
27af5 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 68 61 6e  o(db, sizeof(han
27af6 64 6c 65 29 2a 28 64 62 2d 3e 6e 45 78 74 65 6e  dle)*(db->nExten
27af7 73 69 6f 6e 2b 31 29 29 3b 0a 20 20 69 66 28 20  sion+1));.  if( 
27af8 61 48 61 6e 64 6c 65 3d 3d 30 20 29 7b 0a 20 20  aHandle==0 ){.  
27af9 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
27afa 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28  NOMEM;.  }.  if(
27afb 20 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 3e   db->nExtension>
27afc 30 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28  0 ){.    memcpy(
27afd 61 48 61 6e 64 6c 65 2c 20 64 62 2d 3e 61 45 78  aHandle, db->aEx
27afe 74 65 6e 73 69 6f 6e 2c 20 73 69 7a 65 6f 66 28  tension, sizeof(
27aff 68 61 6e 64 6c 65 29 2a 64 62 2d 3e 6e 45 78 74  handle)*db->nExt
27b00 65 6e 73 69 6f 6e 29 3b 0a 20 20 7d 0a 20 20 73  ension);.  }.  s
27b01 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
27b02 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 29   db->aExtension)
27b03 3b 0a 20 20 64 62 2d 3e 61 45 78 74 65 6e 73 69  ;.  db->aExtensi
27b04 6f 6e 20 3d 20 61 48 61 6e 64 6c 65 3b 0a 0a 20  on = aHandle;.. 
27b05 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 5b   db->aExtension[
27b06 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 2b 2b  db->nExtension++
27b07 5d 20 3d 20 68 61 6e 64 6c 65 3b 0a 20 20 72 65  ] = handle;.  re
27b08 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
27b09 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
27b0a 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78   sqlite3_load_ex
27b0b 74 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c 69 74  tension(.  sqlit
27b0c 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
27b0d 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 78 74   /* Load the ext
27b0e 65 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69 73  ension into this
27b0f 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
27b10 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  tion */.  const 
27b11 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20 20  char *zFile,    
27b12 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 73  /* Name of the s
27b13 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 63 6f  hared library co
27b14 6e 74 61 69 6e 69 6e 67 20 65 78 74 65 6e 73 69  ntaining extensi
27b15 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  on */.  const ch
27b16 61 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f 2a  ar *zProc,    /*
27b17 20 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20 55   Entry point.  U
27b18 73 65 20 22 73 71 6c 69 74 65 33 5f 65 78 74 65  se "sqlite3_exte
27b19 6e 73 69 6f 6e 5f 69 6e 69 74 22 20 69 66 20 30  nsion_init" if 0
27b1a 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45   */.  char **pzE
27b1b 72 72 4d 73 67 20 20 20 20 20 20 20 2f 2a 20 50  rrMsg       /* P
27b1c 75 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  ut error message
27b1d 20 68 65 72 65 20 69 66 20 6e 6f 74 20 30 20 2a   here if not 0 *
27b1e 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  /.){.  int rc;. 
27b1f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
27b20 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
27b21 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4c  .  rc = sqlite3L
27b22 6f 61 64 45 78 74 65 6e 73 69 6f 6e 28 64 62 2c  oadExtension(db,
27b23 20 7a 46 69 6c 65 2c 20 7a 50 72 6f 63 2c 20 70   zFile, zProc, p
27b24 7a 45 72 72 4d 73 67 29 3b 0a 20 20 72 63 20 3d  zErrMsg);.  rc =
27b25 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
27b26 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74  db, rc);.  sqlit
27b27 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
27b28 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
27b29 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
27b2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69   Call this routi
27b2b 6e 65 20 77 68 65 6e 20 74 68 65 20 64 61 74 61  ne when the data
27b2c 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
27b2d 69 73 20 63 6c 6f 73 69 6e 67 20 69 6e 20 6f 72  is closing in or
27b2e 64 65 72 0a 2a 2a 20 74 6f 20 63 6c 65 61 6e 20  der.** to clean 
27b2f 75 70 20 6c 6f 61 64 65 64 20 65 78 74 65 6e 73  up loaded extens
27b30 69 6f 6e 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ions.*/.SQLITE_P
27b31 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
27b32 74 65 33 43 6c 6f 73 65 45 78 74 65 6e 73 69 6f  te3CloseExtensio
27b33 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  ns(sqlite3 *db){
27b34 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65  .  int i;.  asse
27b35 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
27b36 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
27b37 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  ) );.  for(i=0; 
27b38 69 3c 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e  i<db->nExtension
27b39 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  ; i++){.    sqli
27b3a 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28 64 62 2d  te3OsDlClose(db-
27b3b 3e 70 56 66 73 2c 20 64 62 2d 3e 61 45 78 74 65  >pVfs, db->aExte
27b3c 6e 73 69 6f 6e 5b 69 5d 29 3b 0a 20 20 7d 0a 20  nsion[i]);.  }. 
27b3d 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
27b3e 62 2c 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f  b, db->aExtensio
27b3f 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 61  n);.}../*.** Ena
27b40 62 6c 65 20 6f 72 20 64 69 73 61 62 6c 65 20 65  ble or disable e
27b41 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67  xtension loading
27b42 2e 20 20 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61  .  Extension loa
27b43 64 69 6e 67 20 69 73 20 64 69 73 61 62 6c 65 64  ding is disabled
27b44 20 62 79 0a 2a 2a 20 64 65 66 61 75 6c 74 20 73   by.** default s
27b45 6f 20 61 73 20 6e 6f 74 20 74 6f 20 6f 70 65 6e  o as not to open
27b46 20 73 65 63 75 72 69 74 79 20 68 6f 6c 65 73 20   security holes 
27b47 69 6e 20 6f 6c 64 65 72 20 61 70 70 6c 69 63 61  in older applica
27b48 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  tions..*/.SQLITE
27b49 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
27b4a 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74  _enable_load_ext
27b4b 65 6e 73 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a  ension(sqlite3 *
27b4c 64 62 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 7b 0a  db, int onoff){.
27b4d 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
27b4e 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
27b4f 3b 0a 20 20 69 66 28 20 6f 6e 6f 66 66 20 29 7b  ;.  if( onoff ){
27b50 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c  .    db->flags |
27b51 3d 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78 74  = SQLITE_LoadExt
27b52 65 6e 73 69 6f 6e 3b 0a 20 20 7d 65 6c 73 65 7b  ension;.  }else{
27b53 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 26  .    db->flags &
27b54 3d 20 7e 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78  = ~SQLITE_LoadEx
27b55 74 65 6e 73 69 6f 6e 3b 0a 20 20 7d 0a 20 20 73  tension;.  }.  s
27b56 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
27b57 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
27b58 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
27b59 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  K;.}..#endif /* 
27b5a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44  SQLITE_OMIT_LOAD
27b5b 5f 45 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a 0a 2f  _EXTENSION */../
27b5c 2a 0a 2a 2a 20 54 68 65 20 61 75 74 6f 2d 65 78  *.** The auto-ex
27b5d 74 65 6e 73 69 6f 6e 20 63 6f 64 65 20 61 64 64  tension code add
27b5e 65 64 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66  ed regardless of
27b5f 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
27b60 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 6c 6f 61  extension.** loa
27b61 64 69 6e 67 20 69 73 20 73 75 70 70 6f 72 74 65  ding is supporte
27b62 64 2e 20 20 57 65 20 6e 65 65 64 20 61 20 64 75  d.  We need a du
27b63 6d 6d 79 20 73 71 6c 69 74 65 33 41 70 69 73 20  mmy sqlite3Apis 
27b64 70 6f 69 6e 74 65 72 20 66 6f 72 20 74 68 61 74  pointer for that
27b65 0a 2a 2a 20 63 6f 64 65 20 69 66 20 72 65 67 75  .** code if regu
27b66 6c 61 72 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f  lar extension lo
27b67 61 64 69 6e 67 20 69 73 20 6e 6f 74 20 61 76 61  ading is not ava
27b68 69 6c 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73  ilable.  This is
27b69 20 74 68 61 74 0a 2a 2a 20 64 75 6d 6d 79 20 70   that.** dummy p
27b6a 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 23 69 66 64 65  ointer..*/.#ifde
27b6b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
27b6c 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 73 74 61  AD_EXTENSION.sta
27b6d 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
27b6e 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 73  3_api_routines s
27b6f 71 6c 69 74 65 33 41 70 69 73 20 3d 20 7b 20 30  qlite3Apis = { 0
27b70 20 7d 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a   };.#endif.../*.
27b71 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
27b72 20 6f 62 6a 65 63 74 20 68 6f 6c 64 73 20 74 68   object holds th
27b73 65 20 6c 69 73 74 20 6f 66 20 61 75 74 6f 6d 61  e list of automa
27b74 74 69 63 61 6c 6c 79 20 6c 6f 61 64 65 64 0a 2a  tically loaded.*
27b75 2a 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a  * extensions..**
27b76 0a 2a 2a 20 54 68 69 73 20 6c 69 73 74 20 69 73  .** This list is
27b77 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 20 74   shared across t
27b78 68 72 65 61 64 73 2e 20 20 54 68 65 20 53 51 4c  hreads.  The SQL
27b79 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
27b7a 5f 4d 41 53 54 45 52 0a 2a 2a 20 6d 75 74 65 78  _MASTER.** mutex
27b7b 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68   must be held wh
27b7c 69 6c 65 20 61 63 63 65 73 73 69 6e 67 20 74 68  ile accessing th
27b7d 69 73 20 6c 69 73 74 2e 0a 2a 2f 0a 74 79 70 65  is list..*/.type
27b7e 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
27b7f 65 33 41 75 74 6f 45 78 74 4c 69 73 74 20 73 71  e3AutoExtList sq
27b80 6c 69 74 65 33 41 75 74 6f 45 78 74 4c 69 73 74  lite3AutoExtList
27b81 3b 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f  ;.static SQLITE_
27b82 57 53 44 20 73 74 72 75 63 74 20 73 71 6c 69 74  WSD struct sqlit
27b83 65 33 41 75 74 6f 45 78 74 4c 69 73 74 20 7b 0a  e3AutoExtList {.
27b84 20 20 69 6e 74 20 6e 45 78 74 3b 20 20 20 20 20    int nExt;     
27b85 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
27b86 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  er of entries in
27b87 20 61 45 78 74 5b 5d 20 2a 2f 20 20 20 20 20 20   aExt[] */      
27b88 20 20 20 20 0a 20 20 76 6f 69 64 20 28 2a 2a 61      .  void (**a
27b89 45 78 74 29 28 76 6f 69 64 29 3b 20 20 20 2f 2a  Ext)(void);   /*
27b8a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65   Pointers to the
27b8b 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 69 74 20   extension init 
27b8c 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 7d 20 73  functions */.} s
27b8d 71 6c 69 74 65 33 41 75 74 6f 65 78 74 20 3d 20  qlite3Autoext = 
27b8e 7b 20 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 20 54 68  { 0, 0 };../* Th
27b8f 65 20 22 77 73 64 41 75 74 6f 65 78 74 22 20 6d  e "wsdAutoext" m
27b90 61 63 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76  acro will resolv
27b91 65 20 74 6f 20 74 68 65 20 61 75 74 6f 65 78 74  e to the autoext
27b92 65 6e 73 69 6f 6e 0a 2a 2a 20 73 74 61 74 65 20  ension.** state 
27b93 76 65 63 74 6f 72 2e 20 20 49 66 20 77 72 69 74  vector.  If writ
27b94 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61  able static data
27b95 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20   is unsupported 
27b96 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c 0a 2a  on the target,.*
27b97 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63  * we have to loc
27b98 61 74 65 20 74 68 65 20 73 74 61 74 65 20 76 65  ate the state ve
27b99 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65  ctor at run-time
27b9a 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63  .  In the more c
27b9b 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68  ommon.** case wh
27b9c 65 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61  ere writable sta
27b9d 74 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70  tic data is supp
27b9e 6f 72 74 65 64 2c 20 77 73 64 53 74 61 74 20 63  orted, wsdStat c
27b9f 61 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c  an refer directl
27ba0 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c  y.** to the "sql
27ba1 69 74 65 33 41 75 74 6f 65 78 74 22 20 73 74 61  ite3Autoext" sta
27ba2 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72  te vector declar
27ba3 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66  ed above..*/.#if
27ba4 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
27ba5 57 53 44 0a 23 20 64 65 66 69 6e 65 20 77 73 64  WSD.# define wsd
27ba6 41 75 74 6f 65 78 74 49 6e 69 74 20 5c 0a 20 20  AutoextInit \.  
27ba7 73 71 6c 69 74 65 33 41 75 74 6f 45 78 74 4c 69  sqlite3AutoExtLi
27ba8 73 74 20 2a 78 20 3d 20 26 47 4c 4f 42 41 4c 28  st *x = &GLOBAL(
27ba9 73 71 6c 69 74 65 33 41 75 74 6f 45 78 74 4c 69  sqlite3AutoExtLi
27baa 73 74 2c 73 71 6c 69 74 65 33 41 75 74 6f 65 78  st,sqlite3Autoex
27bab 74 29 0a 23 20 64 65 66 69 6e 65 20 77 73 64 41  t).# define wsdA
27bac 75 74 6f 65 78 74 20 78 5b 30 5d 0a 23 65 6c 73  utoext x[0].#els
27bad 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64 41 75  e.# define wsdAu
27bae 74 6f 65 78 74 49 6e 69 74 0a 23 20 64 65 66 69  toextInit.# defi
27baf 6e 65 20 77 73 64 41 75 74 6f 65 78 74 20 73 71  ne wsdAutoext sq
27bb0 6c 69 74 65 33 41 75 74 6f 65 78 74 0a 23 65 6e  lite3Autoext.#en
27bb1 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69  dif.../*.** Regi
27bb2 73 74 65 72 20 61 20 73 74 61 74 69 63 61 6c 6c  ster a staticall
27bb3 79 20 6c 69 6e 6b 65 64 20 65 78 74 65 6e 73 69  y linked extensi
27bb4 6f 6e 20 74 68 61 74 20 69 73 20 61 75 74 6f 6d  on that is autom
27bb5 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 6c 6f 61 64  atically.** load
27bb6 65 64 20 62 79 20 65 76 65 72 79 20 6e 65 77 20  ed by every new 
27bb7 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
27bb8 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ion..*/.SQLITE_A
27bb9 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61  PI int sqlite3_a
27bba 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f  uto_extension(vo
27bbb 69 64 20 28 2a 78 49 6e 69 74 29 28 76 6f 69 64  id (*xInit)(void
27bbc 29 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  )){.  int rc = S
27bbd 51 4c 49 54 45 5f 4f 4b 3b 0a 23 69 66 6e 64 65  QLITE_OK;.#ifnde
27bbe 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
27bbf 54 4f 49 4e 49 54 0a 20 20 72 63 20 3d 20 73 71  TOINIT.  rc = sq
27bc0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
27bc1 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a  ();.  if( rc ){.
27bc2 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
27bc3 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20   }else.#endif.  
27bc4 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 23 69 66  {.    int i;.#if
27bc5 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
27bc6 46 45 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  FE.    sqlite3_m
27bc7 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71  utex *mutex = sq
27bc8 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
27bc9 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
27bca 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e  TIC_MASTER);.#en
27bcb 64 69 66 0a 20 20 20 20 77 73 64 41 75 74 6f 65  dif.    wsdAutoe
27bcc 78 74 49 6e 69 74 3b 0a 20 20 20 20 73 71 6c 69  xtInit;.    sqli
27bcd 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
27bce 6d 75 74 65 78 29 3b 0a 20 20 20 20 66 6f 72 28  mutex);.    for(
27bcf 69 3d 30 3b 20 69 3c 77 73 64 41 75 74 6f 65 78  i=0; i<wsdAutoex
27bd0 74 2e 6e 45 78 74 3b 20 69 2b 2b 29 7b 0a 20 20  t.nExt; i++){.  
27bd1 20 20 20 20 69 66 28 20 77 73 64 41 75 74 6f 65      if( wsdAutoe
27bd2 78 74 2e 61 45 78 74 5b 69 5d 3d 3d 78 49 6e 69  xt.aExt[i]==xIni
27bd3 74 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d  t ) break;.    }
27bd4 0a 20 20 20 20 69 66 28 20 69 3d 3d 77 73 64 41  .    if( i==wsdA
27bd5 75 74 6f 65 78 74 2e 6e 45 78 74 20 29 7b 0a 20  utoext.nExt ){. 
27bd6 20 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d       int nByte =
27bd7 20 28 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78   (wsdAutoext.nEx
27bd8 74 2b 31 29 2a 73 69 7a 65 6f 66 28 77 73 64 41  t+1)*sizeof(wsdA
27bd9 75 74 6f 65 78 74 2e 61 45 78 74 5b 30 5d 29 3b  utoext.aExt[0]);
27bda 0a 20 20 20 20 20 20 76 6f 69 64 20 28 2a 2a 61  .      void (**a
27bdb 4e 65 77 29 28 76 6f 69 64 29 3b 0a 20 20 20 20  New)(void);.    
27bdc 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    aNew = sqlite3
27bdd 5f 72 65 61 6c 6c 6f 63 28 77 73 64 41 75 74 6f  _realloc(wsdAuto
27bde 65 78 74 2e 61 45 78 74 2c 20 6e 42 79 74 65 29  ext.aExt, nByte)
27bdf 3b 0a 20 20 20 20 20 20 69 66 28 20 61 4e 65 77  ;.      if( aNew
27be0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
27be1 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
27be2 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
27be3 20 20 20 20 20 20 20 77 73 64 41 75 74 6f 65 78         wsdAutoex
27be4 74 2e 61 45 78 74 20 3d 20 61 4e 65 77 3b 0a 20  t.aExt = aNew;. 
27be5 20 20 20 20 20 20 20 77 73 64 41 75 74 6f 65 78         wsdAutoex
27be6 74 2e 61 45 78 74 5b 77 73 64 41 75 74 6f 65 78  t.aExt[wsdAutoex
27be7 74 2e 6e 45 78 74 5d 20 3d 20 78 49 6e 69 74 3b  t.nExt] = xInit;
27be8 0a 20 20 20 20 20 20 20 20 77 73 64 41 75 74 6f  .        wsdAuto
27be9 65 78 74 2e 6e 45 78 74 2b 2b 3b 0a 20 20 20 20  ext.nExt++;.    
27bea 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71    }.    }.    sq
27beb 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
27bec 65 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 61 73  e(mutex);.    as
27bed 73 65 72 74 28 20 28 72 63 26 30 78 66 66 29 3d  sert( (rc&0xff)=
27bee 3d 72 63 20 29 3b 0a 20 20 20 20 72 65 74 75 72  =rc );.    retur
27bef 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  n rc;.  }.}../*.
27bf0 2a 2a 20 52 65 73 65 74 20 74 68 65 20 61 75 74  ** Reset the aut
27bf1 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e  omatic extension
27bf2 20 6c 6f 61 64 69 6e 67 20 6d 65 63 68 61 6e 69   loading mechani
27bf3 73 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  sm..*/.SQLITE_AP
27bf4 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
27bf5 65 73 65 74 5f 61 75 74 6f 5f 65 78 74 65 6e 73  eset_auto_extens
27bf6 69 6f 6e 28 76 6f 69 64 29 7b 0a 23 69 66 6e 64  ion(void){.#ifnd
27bf7 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
27bf8 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71  UTOINIT.  if( sq
27bf9 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
27bfa 28 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 0a  ()==SQLITE_OK ).
27bfb 23 65 6e 64 69 66 0a 20 20 7b 0a 23 69 66 20 53  #endif.  {.#if S
27bfc 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
27bfd 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
27bfe 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69  ex *mutex = sqli
27bff 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
27c00 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
27c01 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69  C_MASTER);.#endi
27c02 66 0a 20 20 20 20 77 73 64 41 75 74 6f 65 78 74  f.    wsdAutoext
27c03 49 6e 69 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  Init;.    sqlite
27c04 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75  3_mutex_enter(mu
27c05 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  tex);.    sqlite
27c06 33 5f 66 72 65 65 28 77 73 64 41 75 74 6f 65 78  3_free(wsdAutoex
27c07 74 2e 61 45 78 74 29 3b 0a 20 20 20 20 77 73 64  t.aExt);.    wsd
27c08 41 75 74 6f 65 78 74 2e 61 45 78 74 20 3d 20 30  Autoext.aExt = 0
27c09 3b 0a 20 20 20 20 77 73 64 41 75 74 6f 65 78 74  ;.    wsdAutoext
27c0a 2e 6e 45 78 74 20 3d 20 30 3b 0a 20 20 20 20 73  .nExt = 0;.    s
27c0b 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
27c0c 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 7d  ve(mutex);.  }.}
27c0d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 61 64 20 61 6c 6c  ../*.** Load all
27c0e 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e   automatic exten
27c0f 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  sions..**.** If 
27c10 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72  anything goes wr
27c11 6f 6e 67 2c 20 73 65 74 20 61 6e 20 65 72 72 6f  ong, set an erro
27c12 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  r in the databas
27c13 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f  e connection..*/
27c14 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
27c15 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 6f  void sqlite3Auto
27c16 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 73 28 73  LoadExtensions(s
27c17 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69  qlite3 *db){.  i
27c18 6e 74 20 69 3b 0a 20 20 69 6e 74 20 67 6f 20 3d  nt i;.  int go =
27c19 20 31 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69   1;.  int (*xIni
27c1a 74 29 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72  t)(sqlite3*,char
27c1b 2a 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  **,const sqlite3
27c1c 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 2a 29 3b  _api_routines*);
27c1d 0a 0a 20 20 77 73 64 41 75 74 6f 65 78 74 49 6e  ..  wsdAutoextIn
27c1e 69 74 3b 0a 20 20 69 66 28 20 77 73 64 41 75 74  it;.  if( wsdAut
27c1f 6f 65 78 74 2e 6e 45 78 74 3d 3d 30 20 29 7b 0a  oext.nExt==0 ){.
27c20 20 20 20 20 2f 2a 20 43 6f 6d 6d 6f 6e 20 63 61      /* Common ca
27c21 73 65 3a 20 65 61 72 6c 79 20 6f 75 74 20 77 69  se: early out wi
27c22 74 68 6f 75 74 20 65 76 65 72 79 20 68 61 76 69  thout every havi
27c23 6e 67 20 74 6f 20 61 63 71 75 69 72 65 20 61 20  ng to acquire a 
27c24 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 72 65 74  mutex */.    ret
27c25 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  urn;.  }.  for(i
27c26 3d 30 3b 20 67 6f 3b 20 69 2b 2b 29 7b 0a 20 20  =0; go; i++){.  
27c27 20 20 63 68 61 72 20 2a 7a 45 72 72 6d 73 67 3b    char *zErrmsg;
27c28 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
27c29 41 44 53 41 46 45 0a 20 20 20 20 73 71 6c 69 74  ADSAFE.    sqlit
27c2a 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20  e3_mutex *mutex 
27c2b 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  = sqlite3MutexAl
27c2c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
27c2d 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b  _STATIC_MASTER);
27c2e 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69  .#endif.    sqli
27c2f 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
27c30 6d 75 74 65 78 29 3b 0a 20 20 20 20 69 66 28 20  mutex);.    if( 
27c31 69 3e 3d 77 73 64 41 75 74 6f 65 78 74 2e 6e 45  i>=wsdAutoext.nE
27c32 78 74 20 29 7b 0a 20 20 20 20 20 20 78 49 6e 69  xt ){.      xIni
27c33 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 67 6f 20  t = 0;.      go 
27c34 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = 0;.    }else{.
27c35 20 20 20 20 20 20 78 49 6e 69 74 20 3d 20 28 69        xInit = (i
27c36 6e 74 28 2a 29 28 73 71 6c 69 74 65 33 2a 2c 63  nt(*)(sqlite3*,c
27c37 68 61 72 2a 2a 2c 63 6f 6e 73 74 20 73 71 6c 69  har**,const sqli
27c38 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73  te3_api_routines
27c39 2a 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  *)).            
27c3a 20 20 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78    wsdAutoext.aEx
27c3b 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  t[i];.    }.    
27c3c 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
27c3d 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 20 20  ave(mutex);.    
27c3e 7a 45 72 72 6d 73 67 20 3d 20 30 3b 0a 20 20 20  zErrmsg = 0;.   
27c3f 20 69 66 28 20 78 49 6e 69 74 20 26 26 20 78 49   if( xInit && xI
27c40 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 6d 73 67  nit(db, &zErrmsg
27c41 2c 20 26 73 71 6c 69 74 65 33 41 70 69 73 29 20  , &sqlite3Apis) 
27c42 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
27c43 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45  Error(db, SQLITE
27c44 5f 45 52 52 4f 52 2c 0a 20 20 20 20 20 20 20 20  _ERROR,.        
27c45 20 20 20 20 22 61 75 74 6f 6d 61 74 69 63 20 65      "automatic e
27c46 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67  xtension loading
27c47 20 66 61 69 6c 65 64 3a 20 25 73 22 2c 20 7a 45   failed: %s", zE
27c48 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 20 67 6f  rrmsg);.      go
27c49 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
27c4a 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72  sqlite3_free(zEr
27c4b 72 6d 73 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  rmsg);.  }.}../*
27c4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
27c4d 64 20 6f 66 20 6c 6f 61 64 65 78 74 2e 63 20 2a  d of loadext.c *
27c4e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27c4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
27c51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
27c52 67 69 6e 20 66 69 6c 65 20 70 72 61 67 6d 61 2e  gin file pragma.
27c53 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
27c54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27c55 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
27c56 0a 2a 2a 20 32 30 30 33 20 41 70 72 69 6c 20 36  .** 2003 April 6
27c57 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
27c58 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
27c59 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
27c5a 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
27c5b 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
27c5c 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
27c5d 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
27c5e 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
27c5f 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
27c60 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
27c61 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
27c62 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
27c63 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
27c64 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
27c65 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
27c66 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
27c67 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
27c68 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
27c69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27c6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27c6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27c6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
27c6d 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
27c6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20  tains code used 
27c6f 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
27c70 20 50 52 41 47 4d 41 20 63 6f 6d 6d 61 6e 64 2e   PRAGMA command.
27c71 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 70 72 61 67  .**.** $Id: prag
27c72 6d 61 2e 63 2c 76 20 31 2e 32 31 34 20 32 30 30  ma.c,v 1.214 200
27c73 39 2f 30 37 2f 30 32 20 30 37 3a 34 37 3a 33 33  9/07/02 07:47:33
27c74 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70   danielk1977 Exp
27c75 20 24 0a 2a 2f 0a 0a 2f 2a 20 49 67 6e 6f 72 65   $.*/../* Ignore
27c76 20 74 68 69 73 20 77 68 6f 6c 65 20 66 69 6c 65   this whole file
27c77 20 69 66 20 70 72 61 67 6d 61 73 20 61 72 65 20   if pragmas are 
27c78 64 69 73 61 62 6c 65 64 0a 2a 2f 0a 23 69 66 20  disabled.*/.#if 
27c79 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
27c7a 4f 4d 49 54 5f 50 52 41 47 4d 41 29 0a 0a 2f 2a  OMIT_PRAGMA)../*
27c7b 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68  .** Interpret th
27c7c 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 61  e given string a
27c7d 73 20 61 20 73 61 66 65 74 79 20 6c 65 76 65 6c  s a safety level
27c7e 2e 20 20 52 65 74 75 72 6e 20 30 20 66 6f 72 20  .  Return 0 for 
27c7f 4f 46 46 2c 0a 2a 2a 20 31 20 66 6f 72 20 4f 4e  OFF,.** 1 for ON
27c80 20 6f 72 20 4e 4f 52 4d 41 4c 20 61 6e 64 20 32   or NORMAL and 2
27c81 20 66 6f 72 20 46 55 4c 4c 2e 20 20 52 65 74 75   for FULL.  Retu
27c82 72 6e 20 31 20 66 6f 72 20 61 6e 20 65 6d 70 74  rn 1 for an empt
27c83 79 20 6f 72 20 0a 2a 2a 20 75 6e 72 65 63 6f 67  y or .** unrecog
27c84 6e 69 7a 65 64 20 73 74 72 69 6e 67 20 61 72 67  nized string arg
27c85 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74  ument..**.** Not
27c86 65 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65  e that the value
27c87 73 20 72 65 74 75 72 6e 65 64 20 61 72 65 20 6f  s returned are o
27c88 6e 65 20 6c 65 73 73 20 74 68 61 74 20 74 68 65  ne less that the
27c89 20 76 61 6c 75 65 73 20 74 68 61 74 0a 2a 2a 20   values that.** 
27c8a 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64  should be passed
27c8b 20 69 6e 74 6f 20 73 71 6c 69 74 65 33 42 74 72   into sqlite3Btr
27c8c 65 65 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c  eeSetSafetyLevel
27c8d 28 29 2e 20 20 54 68 65 20 69 73 20 64 6f 6e 65  ().  The is done
27c8e 0a 2a 2a 20 74 6f 20 73 75 70 70 6f 72 74 20 6c  .** to support l
27c8f 65 67 61 63 79 20 53 51 4c 20 63 6f 64 65 2e 20  egacy SQL code. 
27c90 20 54 68 65 20 73 61 66 65 74 79 20 6c 65 76 65   The safety leve
27c91 6c 20 75 73 65 64 20 74 6f 20 62 65 20 62 6f 6f  l used to be boo
27c92 6c 65 61 6e 0a 2a 2a 20 61 6e 64 20 6f 6c 64 65  lean.** and olde
27c93 72 20 73 63 72 69 70 74 73 20 6d 61 79 20 68 61  r scripts may ha
27c94 76 65 20 75 73 65 64 20 6e 75 6d 62 65 72 73 20  ve used numbers 
27c95 30 20 66 6f 72 20 4f 46 46 20 61 6e 64 20 31 20  0 for OFF and 1 
27c96 66 6f 72 20 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69  for ON..*/.stati
27c97 63 20 75 38 20 67 65 74 53 61 66 65 74 79 4c 65  c u8 getSafetyLe
27c98 76 65 6c 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  vel(const char *
27c99 7a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  z){.            
27c9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27c9b 20 2f 2a 20 31 32 33 34 35 36 37 38 39 20 31 32   /* 123456789 12
27c9c 33 34 35 36 37 38 39 20 2a 2f 0a 20 20 73 74 61  3456789 */.  sta
27c9d 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a  tic const char z
27c9e 54 65 78 74 5b 5d 20 3d 20 22 6f 6e 6f 66 66 61  Text[] = "onoffa
27c9f 6c 73 65 79 65 73 74 72 75 65 66 75 6c 6c 22 3b  lseyestruefull";
27ca0 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
27ca1 75 38 20 69 4f 66 66 73 65 74 5b 5d 20 3d 20 7b  u8 iOffset[] = {
27ca2 30 2c 20 31 2c 20 32 2c 20 34 2c 20 39 2c 20 31  0, 1, 2, 4, 9, 1
27ca3 32 2c 20 31 36 7d 3b 0a 20 20 73 74 61 74 69 63  2, 16};.  static
27ca4 20 63 6f 6e 73 74 20 75 38 20 69 4c 65 6e 67 74   const u8 iLengt
27ca5 68 5b 5d 20 3d 20 7b 32 2c 20 32 2c 20 33 2c 20  h[] = {2, 2, 3, 
27ca6 35 2c 20 33 2c 20 34 2c 20 34 7d 3b 0a 20 20 73  5, 3, 4, 4};.  s
27ca7 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 69  tatic const u8 i
27ca8 56 61 6c 75 65 5b 5d 20 3d 20 20 7b 31 2c 20 30  Value[] =  {1, 0
27ca9 2c 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20 32 7d  , 0, 0, 1, 1, 2}
27caa 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20  ;.  int i, n;.  
27cab 69 66 28 20 73 71 6c 69 74 65 33 49 73 64 69 67  if( sqlite3Isdig
27cac 69 74 28 2a 7a 29 20 29 7b 0a 20 20 20 20 72 65  it(*z) ){.    re
27cad 74 75 72 6e 20 28 75 38 29 61 74 6f 69 28 7a 29  turn (u8)atoi(z)
27cae 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 73 71 6c 69  ;.  }.  n = sqli
27caf 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b 0a  te3Strlen30(z);.
27cb0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72    for(i=0; i<Arr
27cb1 61 79 53 69 7a 65 28 69 4c 65 6e 67 74 68 29 3b  aySize(iLength);
27cb2 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69   i++){.    if( i
27cb3 4c 65 6e 67 74 68 5b 69 5d 3d 3d 6e 20 26 26 20  Length[i]==n && 
27cb4 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
27cb5 26 7a 54 65 78 74 5b 69 4f 66 66 73 65 74 5b 69  &zText[iOffset[i
27cb6 5d 5d 2c 7a 2c 6e 29 3d 3d 30 20 29 7b 0a 20 20  ]],z,n)==0 ){.  
27cb7 20 20 20 20 72 65 74 75 72 6e 20 69 56 61 6c 75      return iValu
27cb8 65 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  e[i];.    }.  }.
27cb9 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f    return 1;.}../
27cba 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74  *.** Interpret t
27cbb 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20  he given string 
27cbc 61 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61 6c  as a boolean val
27cbd 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38  ue..*/.static u8
27cbe 20 67 65 74 42 6f 6f 6c 65 61 6e 28 63 6f 6e 73   getBoolean(cons
27cbf 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 72 65  t char *z){.  re
27cc0 74 75 72 6e 20 67 65 74 53 61 66 65 74 79 4c 65  turn getSafetyLe
27cc1 76 65 6c 28 7a 29 26 31 3b 0a 7d 0a 0a 2f 2a 0a  vel(z)&1;.}../*.
27cc2 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68 65  ** Interpret the
27cc3 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 61 73   given string as
27cc4 20 61 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20   a locking mode 
27cc5 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  value..*/.static
27cc6 20 69 6e 74 20 67 65 74 4c 6f 63 6b 69 6e 67 4d   int getLockingM
27cc7 6f 64 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ode(const char *
27cc8 7a 29 7b 0a 20 20 69 66 28 20 7a 20 29 7b 0a 20  z){.  if( z ){. 
27cc9 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65     if( 0==sqlite
27cca 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 65 78 63  3StrICmp(z, "exc
27ccb 6c 75 73 69 76 65 22 29 20 29 20 72 65 74 75 72  lusive") ) retur
27ccc 6e 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  n PAGER_LOCKINGM
27ccd 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3b 0a 20  ODE_EXCLUSIVE;. 
27cce 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65     if( 0==sqlite
27ccf 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 6e 6f 72  3StrICmp(z, "nor
27cd0 6d 61 6c 22 29 20 29 20 72 65 74 75 72 6e 20 50  mal") ) return P
27cd1 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
27cd2 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 7d 0a 20 20 72  _NORMAL;.  }.  r
27cd3 65 74 75 72 6e 20 50 41 47 45 52 5f 4c 4f 43 4b  eturn PAGER_LOCK
27cd4 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 3b 0a 7d  INGMODE_QUERY;.}
27cd5 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
27cd6 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
27cd7 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74  ./*.** Interpret
27cd8 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e   the given strin
27cd9 67 20 61 73 20 61 6e 20 61 75 74 6f 2d 76 61 63  g as an auto-vac
27cda 75 75 6d 20 6d 6f 64 65 20 76 61 6c 75 65 2e 0a  uum mode value..
27cdb 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  **.** The follow
27cdc 69 6e 67 20 73 74 72 69 6e 67 73 2c 20 22 6e 6f  ing strings, "no
27cdd 6e 65 22 2c 20 22 66 75 6c 6c 22 20 61 6e 64 20  ne", "full" and 
27cde 22 69 6e 63 72 65 6d 65 6e 74 61 6c 22 20 61 72  "incremental" ar
27cdf 65 20 0a 2a 2a 20 61 63 63 65 70 74 61 62 6c 65  e .** acceptable
27ce0 2c 20 61 73 20 61 72 65 20 74 68 65 69 72 20 6e  , as are their n
27ce1 75 6d 65 72 69 63 20 65 71 75 69 76 61 6c 65 6e  umeric equivalen
27ce2 74 73 3a 20 30 2c 20 31 20 61 6e 64 20 32 20 72  ts: 0, 1 and 2 r
27ce3 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2f 0a  espectively..*/.
27ce4 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 41 75  static int getAu
27ce5 74 6f 56 61 63 75 75 6d 28 63 6f 6e 73 74 20 63  toVacuum(const c
27ce6 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 69  har *z){.  int i
27ce7 3b 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74  ;.  if( 0==sqlit
27ce8 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 6e 6f  e3StrICmp(z, "no
27ce9 6e 65 22 29 20 29 20 72 65 74 75 72 6e 20 42 54  ne") ) return BT
27cea 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e  REE_AUTOVACUUM_N
27ceb 4f 4e 45 3b 0a 20 20 69 66 28 20 30 3d 3d 73 71  ONE;.  if( 0==sq
27cec 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20  lite3StrICmp(z, 
27ced 22 66 75 6c 6c 22 29 20 29 20 72 65 74 75 72 6e  "full") ) return
27cee 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55   BTREE_AUTOVACUU
27cef 4d 5f 46 55 4c 4c 3b 0a 20 20 69 66 28 20 30 3d  M_FULL;.  if( 0=
27cf0 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28  =sqlite3StrICmp(
27cf1 7a 2c 20 22 69 6e 63 72 65 6d 65 6e 74 61 6c 22  z, "incremental"
27cf2 29 20 29 20 72 65 74 75 72 6e 20 42 54 52 45 45  ) ) return BTREE
27cf3 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52  _AUTOVACUUM_INCR
27cf4 3b 0a 20 20 69 20 3d 20 61 74 6f 69 28 7a 29 3b  ;.  i = atoi(z);
27cf5 0a 20 20 72 65 74 75 72 6e 20 28 75 38 29 28 28  .  return (u8)((
27cf6 69 3e 3d 30 26 26 69 3c 3d 32 29 3f 69 3a 30 29  i>=0&&i<=2)?i:0)
27cf7 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66  ;.}.#endif /* if
27cf8 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
27cf9 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 0a  _AUTOVACUUM */..
27cfa 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
27cfb 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  MIT_PAGER_PRAGMA
27cfc 53 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65  S./*.** Interpre
27cfd 74 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69  t the given stri
27cfe 6e 67 20 61 73 20 61 20 74 65 6d 70 20 64 62 20  ng as a temp db 
27cff 6c 6f 63 61 74 69 6f 6e 2e 20 52 65 74 75 72 6e  location. Return
27d00 20 31 20 66 6f 72 20 66 69 6c 65 0a 2a 2a 20 62   1 for file.** b
27d01 61 63 6b 65 64 20 74 65 6d 70 6f 72 61 72 79 20  acked temporary 
27d02 64 61 74 61 62 61 73 65 73 2c 20 32 20 66 6f 72  databases, 2 for
27d03 20 74 68 65 20 52 65 64 2d 42 6c 61 63 6b 20 74   the Red-Black t
27d04 72 65 65 20 69 6e 20 6d 65 6d 6f 72 79 20 64 61  ree in memory da
27d05 74 61 62 61 73 65 0a 2a 2a 20 61 6e 64 20 30 20  tabase.** and 0 
27d06 74 6f 20 75 73 65 20 74 68 65 20 63 6f 6d 70 69  to use the compi
27d07 6c 65 2d 74 69 6d 65 20 64 65 66 61 75 6c 74 2e  le-time default.
27d08 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67  .*/.static int g
27d09 65 74 54 65 6d 70 53 74 6f 72 65 28 63 6f 6e 73  etTempStore(cons
27d0a 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 66  t char *z){.  if
27d0b 28 20 7a 5b 30 5d 3e 3d 27 30 27 20 26 26 20 7a  ( z[0]>='0' && z
27d0c 5b 30 5d 3c 3d 27 32 27 20 29 7b 0a 20 20 20 20  [0]<='2' ){.    
27d0d 72 65 74 75 72 6e 20 7a 5b 30 5d 20 2d 20 27 30  return z[0] - '0
27d0e 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  ';.  }else if( s
27d0f 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c  qlite3StrICmp(z,
27d10 20 22 66 69 6c 65 22 29 3d 3d 30 20 29 7b 0a 20   "file")==0 ){. 
27d11 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
27d12 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33  else if( sqlite3
27d13 53 74 72 49 43 6d 70 28 7a 2c 20 22 6d 65 6d 6f  StrICmp(z, "memo
27d14 72 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72  ry")==0 ){.    r
27d15 65 74 75 72 6e 20 32 3b 0a 20 20 7d 65 6c 73 65  eturn 2;.  }else
27d16 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
27d17 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20    }.}.#endif /* 
27d18 53 51 4c 49 54 45 5f 50 41 47 45 52 5f 50 52 41  SQLITE_PAGER_PRA
27d19 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  GMAS */..#ifndef
27d1a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47   SQLITE_OMIT_PAG
27d1b 45 52 5f 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a  ER_PRAGMAS./*.**
27d1c 20 49 6e 76 61 6c 69 64 61 74 65 20 74 65 6d 70   Invalidate temp
27d1d 20 73 74 6f 72 61 67 65 2c 20 65 69 74 68 65 72   storage, either
27d1e 20 77 68 65 6e 20 74 68 65 20 74 65 6d 70 20 73   when the temp s
27d1f 74 6f 72 61 67 65 20 69 73 20 63 68 61 6e 67 65  torage is change
27d20 64 0a 2a 2a 20 66 72 6f 6d 20 64 65 66 61 75 6c  d.** from defaul
27d21 74 2c 20 6f 72 20 77 68 65 6e 20 27 66 69 6c 65  t, or when 'file
27d22 27 20 61 6e 64 20 74 68 65 20 74 65 6d 70 5f 73  ' and the temp_s
27d23 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 20 68  tore_directory h
27d24 61 73 20 63 68 61 6e 67 65 64 0a 2a 2f 0a 73 74  as changed.*/.st
27d25 61 74 69 63 20 69 6e 74 20 69 6e 76 61 6c 69 64  atic int invalid
27d26 61 74 65 54 65 6d 70 53 74 6f 72 61 67 65 28 50  ateTempStorage(P
27d27 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20  arse *pParse){. 
27d28 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
27d29 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28  Parse->db;.  if(
27d2a 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 21   db->aDb[1].pBt!
27d2b 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21 64  =0 ){.    if( !d
27d2c 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 7c 7c  b->autoCommit ||
27d2d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49   sqlite3BtreeIsI
27d2e 6e 52 65 61 64 54 72 61 6e 73 28 64 62 2d 3e 61  nReadTrans(db->a
27d2f 44 62 5b 31 5d 2e 70 42 74 29 20 29 7b 0a 20 20  Db[1].pBt) ){.  
27d30 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
27d31 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 65 6d  Msg(pParse, "tem
27d32 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65 20 63  porary storage c
27d33 61 6e 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64  annot be changed
27d34 20 22 0a 20 20 20 20 20 20 20 20 22 66 72 6f 6d   ".        "from
27d35 20 77 69 74 68 69 6e 20 61 20 74 72 61 6e 73 61   within a transa
27d36 63 74 69 6f 6e 22 29 3b 0a 20 20 20 20 20 20 72  ction");.      r
27d37 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
27d38 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  OR;.    }.    sq
27d39 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28  lite3BtreeClose(
27d3a 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29 3b  db->aDb[1].pBt);
27d3b 0a 20 20 20 20 64 62 2d 3e 61 44 62 5b 31 5d 2e  .    db->aDb[1].
27d3c 70 42 74 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c  pBt = 0;.    sql
27d3d 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
27d3e 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a  lSchema(db, 0);.
27d3f 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
27d40 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66  ITE_OK;.}.#endif
27d41 20 2f 2a 20 53 51 4c 49 54 45 5f 50 41 47 45 52   /* SQLITE_PAGER
27d42 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66  _PRAGMAS */..#if
27d43 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
27d44 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 2f  _PAGER_PRAGMAS./
27d45 2a 0a 2a 2a 20 49 66 20 74 68 65 20 54 45 4d 50  *.** If the TEMP
27d46 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65   database is ope
27d47 6e 2c 20 63 6c 6f 73 65 20 69 74 20 61 6e 64 20  n, close it and 
27d48 6d 61 72 6b 20 74 68 65 20 64 61 74 61 62 61 73  mark the databas
27d49 65 20 73 63 68 65 6d 61 0a 2a 2a 20 61 73 20 6e  e schema.** as n
27d4a 65 65 64 69 6e 67 20 72 65 6c 6f 61 64 69 6e 67  eeding reloading
27d4b 2e 20 20 54 68 69 73 20 6d 75 73 74 20 62 65 20  .  This must be 
27d4c 64 6f 6e 65 20 77 68 65 6e 20 75 73 69 6e 67 20  done when using 
27d4d 74 68 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f  the SQLITE_TEMP_
27d4e 53 54 4f 52 45 0a 2a 2a 20 6f 72 20 44 45 46 41  STORE.** or DEFA
27d4f 55 4c 54 5f 54 45 4d 50 5f 53 54 4f 52 45 20 70  ULT_TEMP_STORE p
27d50 72 61 67 6d 61 73 2e 0a 2a 2f 0a 73 74 61 74 69  ragmas..*/.stati
27d51 63 20 69 6e 74 20 63 68 61 6e 67 65 54 65 6d 70  c int changeTemp
27d52 53 74 6f 72 61 67 65 28 50 61 72 73 65 20 2a 70  Storage(Parse *p
27d53 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61  Parse, const cha
27d54 72 20 2a 7a 53 74 6f 72 61 67 65 54 79 70 65 29  r *zStorageType)
27d55 7b 0a 20 20 69 6e 74 20 74 73 20 3d 20 67 65 74  {.  int ts = get
27d56 54 65 6d 70 53 74 6f 72 65 28 7a 53 74 6f 72 61  TempStore(zStora
27d57 67 65 54 79 70 65 29 3b 0a 20 20 73 71 6c 69 74  geType);.  sqlit
27d58 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
27d59 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 74  >db;.  if( db->t
27d5a 65 6d 70 5f 73 74 6f 72 65 3d 3d 74 73 20 29 20  emp_store==ts ) 
27d5b 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
27d5c 3b 0a 20 20 69 66 28 20 69 6e 76 61 6c 69 64 61  ;.  if( invalida
27d5d 74 65 54 65 6d 70 53 74 6f 72 61 67 65 28 20 70  teTempStorage( p
27d5e 50 61 72 73 65 20 29 20 21 3d 20 53 51 4c 49 54  Parse ) != SQLIT
27d5f 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
27d60 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
27d61 0a 20 20 7d 0a 20 20 64 62 2d 3e 74 65 6d 70 5f  .  }.  db->temp_
27d62 73 74 6f 72 65 20 3d 20 28 75 38 29 74 73 3b 0a  store = (u8)ts;.
27d63 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
27d64 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  OK;.}.#endif /* 
27d65 53 51 4c 49 54 45 5f 50 41 47 45 52 5f 50 52 41  SQLITE_PAGER_PRA
27d66 47 4d 41 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47  GMAS */../*.** G
27d67 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
27d68 72 65 74 75 72 6e 20 61 20 73 69 6e 67 6c 65 20  return a single 
27d69 69 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 0a 2a  integer value..*
27d6a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  /.static void re
27d6b 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 50 61  turnSingleInt(Pa
27d6c 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e  rse *pParse, con
27d6d 73 74 20 63 68 61 72 20 2a 7a 4c 61 62 65 6c 2c  st char *zLabel,
27d6e 20 69 36 34 20 76 61 6c 75 65 29 7b 0a 20 20 56   i64 value){.  V
27d6f 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33  dbe *v = sqlite3
27d70 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
27d71 0a 20 20 69 6e 74 20 6d 65 6d 20 3d 20 2b 2b 70  .  int mem = ++p
27d72 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 69  Parse->nMem;.  i
27d73 36 34 20 2a 70 49 36 34 20 3d 20 73 71 6c 69 74  64 *pI64 = sqlit
27d74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 50  e3DbMallocRaw(pP
27d75 61 72 73 65 2d 3e 64 62 2c 20 73 69 7a 65 6f 66  arse->db, sizeof
27d76 28 76 61 6c 75 65 29 29 3b 0a 20 20 69 66 28 20  (value));.  if( 
27d77 70 49 36 34 20 29 7b 0a 20 20 20 20 6d 65 6d 63  pI64 ){.    memc
27d78 70 79 28 70 49 36 34 2c 20 26 76 61 6c 75 65 2c  py(pI64, &value,
27d79 20 73 69 7a 65 6f 66 28 76 61 6c 75 65 29 29 3b   sizeof(value));
27d7a 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64  .  }.  sqlite3Vd
27d7b 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49  beAddOp4(v, OP_I
27d7c 6e 74 36 34 2c 20 30 2c 20 6d 65 6d 2c 20 30 2c  nt64, 0, mem, 0,
27d7d 20 28 63 68 61 72 2a 29 70 49 36 34 2c 20 50 34   (char*)pI64, P4
27d7e 5f 49 4e 54 36 34 29 3b 0a 20 20 73 71 6c 69 74  _INT64);.  sqlit
27d7f 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
27d80 28 76 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74 65  (v, 1);.  sqlite
27d81 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
27d82 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 0, COLNAME_NA
27d83 4d 45 2c 20 7a 4c 61 62 65 6c 2c 20 53 51 4c 49  ME, zLabel, SQLI
27d84 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 73 71  TE_STATIC);.  sq
27d85 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27d86 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
27d87 20 6d 65 6d 2c 20 31 29 3b 0a 7d 0a 0a 23 69 66   mem, 1);.}..#if
27d88 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
27d89 5f 46 4c 41 47 5f 50 52 41 47 4d 41 53 0a 2f 2a  _FLAG_PRAGMAS./*
27d8a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65  .** Check to see
27d8b 20 69 66 20 7a 52 69 67 68 74 20 61 6e 64 20 7a   if zRight and z
27d8c 4c 65 66 74 20 72 65 66 65 72 20 74 6f 20 61 20  Left refer to a 
27d8d 70 72 61 67 6d 61 20 74 68 61 74 20 71 75 65 72  pragma that quer
27d8e 69 65 73 0a 2a 2a 20 6f 72 20 63 68 61 6e 67 65  ies.** or change
27d8f 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6c 61  s one of the fla
27d90 67 73 20 69 6e 20 64 62 2d 3e 66 6c 61 67 73 2e  gs in db->flags.
27d91 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 73 6f    Return 1 if so
27d92 20 61 6e 64 20 30 20 69 66 20 6e 6f 74 2e 0a 2a   and 0 if not..*
27d93 2a 20 41 6c 73 6f 2c 20 69 6d 70 6c 65 6d 65 6e  * Also, implemen
27d94 74 20 74 68 65 20 70 72 61 67 6d 61 2e 0a 2a 2f  t the pragma..*/
27d95 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 61 67  .static int flag
27d96 50 72 61 67 6d 61 28 50 61 72 73 65 20 2a 70 50  Pragma(Parse *pP
27d97 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  arse, const char
27d98 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63   *zLeft, const c
27d99 68 61 72 20 2a 7a 52 69 67 68 74 29 7b 0a 20 20  har *zRight){.  
27d9a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72  static const str
27d9b 75 63 74 20 73 50 72 61 67 6d 61 54 79 70 65 20  uct sPragmaType 
27d9c 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
27d9d 20 2a 7a 4e 61 6d 65 3b 20 20 2f 2a 20 4e 61 6d   *zName;  /* Nam
27d9e 65 20 6f 66 20 74 68 65 20 70 72 61 67 6d 61 20  e of the pragma 
27d9f 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 61 73 6b 3b  */.    int mask;
27da0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
27da1 73 6b 20 66 6f 72 20 74 68 65 20 64 62 2d 3e 66  sk for the db->f
27da2 6c 61 67 73 20 76 61 6c 75 65 20 2a 2f 0a 20 20  lags value */.  
27da3 7d 20 61 50 72 61 67 6d 61 5b 5d 20 3d 20 7b 0a  } aPragma[] = {.
27da4 20 20 20 20 7b 20 22 66 75 6c 6c 5f 63 6f 6c 75      { "full_colu
27da5 6d 6e 5f 6e 61 6d 65 73 22 2c 20 20 20 20 20 20  mn_names",      
27da6 20 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c    SQLITE_FullCol
27da7 4e 61 6d 65 73 20 20 7d 2c 0a 20 20 20 20 7b 20  Names  },.    { 
27da8 22 73 68 6f 72 74 5f 63 6f 6c 75 6d 6e 5f 6e 61  "short_column_na
27da9 6d 65 73 22 2c 20 20 20 20 20 20 20 53 51 4c 49  mes",       SQLI
27daa 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73  TE_ShortColNames
27dab 20 7d 2c 0a 20 20 20 20 7b 20 22 63 6f 75 6e 74   },.    { "count
27dac 5f 63 68 61 6e 67 65 73 22 2c 20 20 20 20 20 20  _changes",      
27dad 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 6f 75        SQLITE_Cou
27dae 6e 74 52 6f 77 73 20 20 20 20 20 7d 2c 0a 20 20  ntRows     },.  
27daf 20 20 7b 20 22 65 6d 70 74 79 5f 72 65 73 75 6c    { "empty_resul
27db0 74 5f 63 61 6c 6c 62 61 63 6b 73 22 2c 20 20 20  t_callbacks",   
27db1 53 51 4c 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c 62  SQLITE_NullCallb
27db2 61 63 6b 20 20 7d 2c 0a 20 20 20 20 7b 20 22 6c  ack  },.    { "l
27db3 65 67 61 63 79 5f 66 69 6c 65 5f 66 6f 72 6d 61  egacy_file_forma
27db4 74 22 2c 20 20 20 20 20 20 20 53 51 4c 49 54 45  t",       SQLITE
27db5 5f 4c 65 67 61 63 79 46 69 6c 65 46 6d 74 20 7d  _LegacyFileFmt }
27db6 2c 0a 20 20 20 20 7b 20 22 66 75 6c 6c 66 73 79  ,.    { "fullfsy
27db7 6e 63 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  nc",            
27db8 20 20 20 20 53 51 4c 49 54 45 5f 46 75 6c 6c 46      SQLITE_FullF
27db9 53 79 6e 63 20 20 20 20 20 7d 2c 0a 20 20 20 20  Sync     },.    
27dba 7b 20 22 72 65 76 65 72 73 65 5f 75 6e 6f 72 64  { "reverse_unord
27dbb 65 72 65 64 5f 73 65 6c 65 63 74 73 22 2c 20 53  ered_selects", S
27dbc 51 4c 49 54 45 5f 52 65 76 65 72 73 65 4f 72 64  QLITE_ReverseOrd
27dbd 65 72 20 20 7d 2c 0a 23 69 66 64 65 66 20 53 51  er  },.#ifdef SQ
27dbe 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 7b  LITE_DEBUG.    {
27dbf 20 22 73 71 6c 5f 74 72 61 63 65 22 2c 20 20 20   "sql_trace",   
27dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
27dc1 49 54 45 5f 53 71 6c 54 72 61 63 65 20 20 20 20  ITE_SqlTrace    
27dc2 20 20 7d 2c 0a 20 20 20 20 7b 20 22 76 64 62 65    },.    { "vdbe
27dc3 5f 6c 69 73 74 69 6e 67 22 2c 20 20 20 20 20 20  _listing",      
27dc4 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 56 64         SQLITE_Vd
27dc5 62 65 4c 69 73 74 69 6e 67 20 20 20 7d 2c 0a 20  beListing   },. 
27dc6 20 20 20 7b 20 22 76 64 62 65 5f 74 72 61 63 65     { "vdbe_trace
27dc7 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ",              
27dc8 20 53 51 4c 49 54 45 5f 56 64 62 65 54 72 61 63   SQLITE_VdbeTrac
27dc9 65 20 20 20 20 20 7d 2c 0a 23 65 6e 64 69 66 0a  e     },.#endif.
27dca 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
27dcb 4d 49 54 5f 43 48 45 43 4b 0a 20 20 20 20 7b 20  MIT_CHECK.    { 
27dcc 22 69 67 6e 6f 72 65 5f 63 68 65 63 6b 5f 63 6f  "ignore_check_co
27dcd 6e 73 74 72 61 69 6e 74 73 22 2c 20 53 51 4c 49  nstraints", SQLI
27dce 54 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73 20  TE_IgnoreChecks 
27dcf 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f   },.#endif.    /
27dd0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
27dd1 69 73 20 56 45 52 59 20 65 78 70 65 72 69 6d 65  is VERY experime
27dd2 6e 74 61 6c 20 2a 2f 0a 20 20 20 20 7b 20 22 77  ntal */.    { "w
27dd3 72 69 74 61 62 6c 65 5f 73 63 68 65 6d 61 22 2c  ritable_schema",
27dd4 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
27dd5 5f 57 72 69 74 65 53 63 68 65 6d 61 7c 53 51 4c  _WriteSchema|SQL
27dd6 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65  ITE_RecoveryMode
27dd7 20 7d 2c 0a 20 20 20 20 7b 20 22 6f 6d 69 74 5f   },.    { "omit_
27dd8 72 65 61 64 6c 6f 63 6b 22 2c 20 20 20 20 20 20  readlock",      
27dd9 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4e 6f 52        SQLITE_NoR
27dda 65 61 64 6c 6f 63 6b 20 20 20 20 7d 2c 0a 0a 20  eadlock    },.. 
27ddb 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 4d 61 79 62     /* TODO: Mayb
27ddc 65 20 69 74 20 73 68 6f 75 6c 64 6e 27 74 20 62  e it shouldn't b
27ddd 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 63 68  e possible to ch
27dde 61 6e 67 65 20 74 68 65 20 52 65 61 64 55 6e 63  ange the ReadUnc
27ddf 6f 6d 6d 69 74 74 65 64 0a 20 20 20 20 2a 2a 20  ommitted.    ** 
27de0 66 6c 61 67 20 69 66 20 74 68 65 72 65 20 61 72  flag if there ar
27de1 65 20 61 6e 79 20 61 63 74 69 76 65 20 73 74 61  e any active sta
27de2 74 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20 20 20 20  tements. */.    
27de3 7b 20 22 72 65 61 64 5f 75 6e 63 6f 6d 6d 69 74  { "read_uncommit
27de4 74 65 64 22 2c 20 20 20 20 20 20 20 20 20 53 51  ted",         SQ
27de5 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69  LITE_ReadUncommi
27de6 74 74 65 64 20 7d 2c 0a 20 20 20 20 7b 20 22 72  tted },.    { "r
27de7 65 63 75 72 73 69 76 65 5f 74 72 69 67 67 65 72  ecursive_trigger
27de8 73 22 2c 20 20 20 20 20 20 20 53 51 4c 49 54 45  s",       SQLITE
27de9 5f 52 65 63 54 72 69 67 67 65 72 73 20 7d 2c 0a  _RecTriggers },.
27dea 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20    };.  int i;.  
27deb 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73 50 72  const struct sPr
27dec 61 67 6d 61 54 79 70 65 20 2a 70 3b 0a 20 20 66  agmaType *p;.  f
27ded 6f 72 28 69 3d 30 2c 20 70 3d 61 50 72 61 67 6d  or(i=0, p=aPragm
27dee 61 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 61  a; i<ArraySize(a
27def 50 72 61 67 6d 61 29 3b 20 69 2b 2b 2c 20 70 2b  Pragma); i++, p+
27df0 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  +){.    if( sqli
27df1 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
27df2 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29  , p->zName)==0 )
27df3 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 20  {.      sqlite3 
27df4 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
27df5 3b 0a 20 20 20 20 20 20 56 64 62 65 20 2a 76 3b  ;.      Vdbe *v;
27df6 0a 20 20 20 20 20 20 76 20 3d 20 73 71 6c 69 74  .      v = sqlit
27df7 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
27df8 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
27df9 20 76 21 3d 30 20 29 3b 20 20 2f 2a 20 41 6c 72   v!=0 );  /* Alr
27dfa 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 20 62  eady allocated b
27dfb 79 20 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28  y sqlite3Pragma(
27dfc 29 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 41  ) */.      if( A
27dfd 4c 57 41 59 53 28 76 29 20 29 7b 0a 20 20 20 20  LWAYS(v) ){.    
27dfe 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 3d 3d      if( zRight==
27dff 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  0 ){.          r
27e00 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70  eturnSingleInt(p
27e01 50 61 72 73 65 2c 20 70 2d 3e 7a 4e 61 6d 65 2c  Parse, p->zName,
27e02 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 70 2d   (db->flags & p-
27e03 3e 6d 61 73 6b 29 21 3d 30 20 29 3b 0a 20 20 20  >mask)!=0 );.   
27e04 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
27e05 20 20 20 20 20 20 69 66 28 20 67 65 74 42 6f 6f        if( getBoo
27e06 6c 65 61 6e 28 7a 52 69 67 68 74 29 20 29 7b 0a  lean(zRight) ){.
27e07 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e              db->
27e08 66 6c 61 67 73 20 7c 3d 20 70 2d 3e 6d 61 73 6b  flags |= p->mask
27e09 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
27e0a 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 64  e{.            d
27e0b 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 70 2d 3e  b->flags &= ~p->
27e0c 6d 61 73 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  mask;.          
27e0d 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  }..          /* 
27e0e 4d 61 6e 79 20 6f 66 20 74 68 65 20 66 6c 61 67  Many of the flag
27e0f 2d 70 72 61 67 6d 61 73 20 6d 6f 64 69 66 79 20  -pragmas modify 
27e10 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74  the code generat
27e11 65 64 20 62 79 20 74 68 65 20 53 51 4c 20 0a 20  ed by the SQL . 
27e12 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70           ** comp
27e13 69 6c 65 72 20 28 65 67 2e 20 63 6f 75 6e 74 5f  iler (eg. count_
27e14 63 68 61 6e 67 65 73 29 2e 20 53 6f 20 61 64 64  changes). So add
27e15 20 61 6e 20 6f 70 63 6f 64 65 20 74 6f 20 65 78   an opcode to ex
27e16 70 69 72 65 20 61 6c 6c 0a 20 20 20 20 20 20 20  pire all.       
27e17 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 53     ** compiled S
27e18 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 61 66  QL statements af
27e19 74 65 72 20 6d 6f 64 69 66 79 69 6e 67 20 61 20  ter modifying a 
27e1a 70 72 61 67 6d 61 20 76 61 6c 75 65 2e 0a 20 20  pragma value..  
27e1b 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
27e1c 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
27e1d 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 45 78 70  AddOp2(v, OP_Exp
27e1e 69 72 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  ire, 0, 0);.    
27e1f 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20      }.      }.. 
27e20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
27e21 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
27e22 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  n 0;.}.#endif /*
27e23 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 41   SQLITE_OMIT_FLA
27e24 47 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 2f 2a  G_PRAGMAS */../*
27e25 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 68 75 6d  .** Return a hum
27e26 61 6e 2d 72 65 61 64 61 62 6c 65 20 6e 61 6d 65  an-readable name
27e27 20 66 6f 72 20 61 20 63 6f 6e 73 74 72 61 69 6e   for a constrain
27e28 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 61 63 74  t resolution act
27e29 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ion..*/.static c
27e2a 6f 6e 73 74 20 63 68 61 72 20 2a 61 63 74 69 6f  onst char *actio
27e2b 6e 4e 61 6d 65 28 75 38 20 61 63 74 69 6f 6e 29  nName(u8 action)
27e2c 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
27e2d 7a 4e 61 6d 65 3b 0a 20 20 73 77 69 74 63 68 28  zName;.  switch(
27e2e 20 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 63   action ){.    c
27e2f 61 73 65 20 4f 45 5f 53 65 74 4e 75 6c 6c 3a 20  ase OE_SetNull: 
27e30 20 7a 4e 61 6d 65 20 3d 20 22 53 45 54 20 4e 55   zName = "SET NU
27e31 4c 4c 22 3b 20 20 20 20 20 20 20 20 20 20 20 20  LL";            
27e32 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
27e33 4f 45 5f 53 65 74 44 66 6c 74 3a 20 20 7a 4e 61  OE_SetDflt:  zNa
27e34 6d 65 20 3d 20 22 53 45 54 20 44 45 46 41 55 4c  me = "SET DEFAUL
27e35 54 22 3b 20 20 20 20 20 20 20 20 20 62 72 65 61  T";         brea
27e36 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 43  k;.    case OE_C
27e37 61 73 63 61 64 65 3a 20 20 7a 4e 61 6d 65 20 3d  ascade:  zName =
27e38 20 22 43 41 53 43 41 44 45 22 3b 20 20 20 20 20   "CASCADE";     
27e39 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
27e3a 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 20     default:     
27e3b 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 22 52 45       zName = "RE
27e3c 53 54 52 49 43 54 22 3b 20 20 0a 20 20 20 20 20  STRICT";  .     
27e3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27e3e 20 61 73 73 65 72 74 28 20 61 63 74 69 6f 6e 3d   assert( action=
27e3f 3d 4f 45 5f 52 65 73 74 72 69 63 74 20 29 3b 20  =OE_Restrict ); 
27e40 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74  break;.  }.  ret
27e41 75 72 6e 20 7a 4e 61 6d 65 3b 0a 7d 0a 0a 2f 2a  urn zName;.}../*
27e42 0a 2a 2a 20 50 72 6f 63 65 73 73 20 61 20 70 72  .** Process a pr
27e43 61 67 6d 61 20 73 74 61 74 65 6d 65 6e 74 2e 20  agma statement. 
27e44 20 0a 2a 2a 0a 2a 2a 20 50 72 61 67 6d 61 73 20   .**.** Pragmas 
27e45 61 72 65 20 6f 66 20 74 68 69 73 20 66 6f 72 6d  are of this form
27e46 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 50 52 41  :.**.**      PRA
27e47 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 69  GMA [database.]i
27e48 64 20 5b 3d 20 76 61 6c 75 65 5d 0a 2a 2a 0a 2a  d [= value].**.*
27e49 2a 20 54 68 65 20 69 64 65 6e 74 69 66 69 65 72  * The identifier
27e4a 20 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20 61   might also be a
27e4b 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20 76 61   string.  The va
27e4c 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e 67 2c  lue is a string,
27e4d 20 61 6e 64 0a 2a 2a 20 69 64 65 6e 74 69 66 69   and.** identifi
27e4e 65 72 2c 20 6f 72 20 61 20 6e 75 6d 62 65 72 2e  er, or a number.
27e4f 20 20 49 66 20 6d 69 6e 75 73 46 6c 61 67 20 69    If minusFlag i
27e50 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65  s true, then the
27e51 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61 20 6e   value is.** a n
27e52 75 6d 62 65 72 20 74 68 61 74 20 77 61 73 20 70  umber that was p
27e53 72 65 63 65 64 65 64 20 62 79 20 61 20 6d 69 6e  receded by a min
27e54 75 73 20 73 69 67 6e 2e 0a 2a 2a 0a 2a 2a 20 49  us sign..**.** I
27e55 66 20 74 68 65 20 6c 65 66 74 20 73 69 64 65 20  f the left side 
27e56 69 73 20 22 64 61 74 61 62 61 73 65 2e 69 64 22  is "database.id"
27e57 20 74 68 65 6e 20 70 49 64 31 20 69 73 20 74 68   then pId1 is th
27e58 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a  e database name.
27e59 2a 2a 20 61 6e 64 20 70 49 64 32 20 69 73 20 74  ** and pId2 is t
27e5a 68 65 20 69 64 2e 20 20 49 66 20 74 68 65 20 6c  he id.  If the l
27e5b 65 66 74 20 73 69 64 65 20 69 73 20 6a 75 73 74  eft side is just
27e5c 20 22 69 64 22 20 74 68 65 6e 20 70 49 64 31 20   "id" then pId1 
27e5d 69 73 20 74 68 65 0a 2a 2a 20 69 64 20 61 6e 64  is the.** id and
27e5e 20 70 49 64 32 20 69 73 20 61 6e 79 20 65 6d 70   pId2 is any emp
27e5f 74 79 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 53 51  ty string..*/.SQ
27e60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
27e61 64 20 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28  d sqlite3Pragma(
27e62 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
27e63 2c 20 0a 20 20 54 6f 6b 65 6e 20 2a 70 49 64 31  , .  Token *pId1
27e64 2c 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73  ,        /* Firs
27e65 74 20 70 61 72 74 20 6f 66 20 5b 64 61 74 61 62  t part of [datab
27e66 61 73 65 2e 5d 69 64 20 66 69 65 6c 64 20 2a 2f  ase.]id field */
27e67 0a 20 20 54 6f 6b 65 6e 20 2a 70 49 64 32 2c 20  .  Token *pId2, 
27e68 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64         /* Second
27e69 20 70 61 72 74 20 6f 66 20 5b 64 61 74 61 62 61   part of [databa
27e6a 73 65 2e 5d 69 64 20 66 69 65 6c 64 2c 20 6f 72  se.]id field, or
27e6b 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e   NULL */.  Token
27e6c 20 2a 70 56 61 6c 75 65 2c 20 20 20 20 20 20 2f   *pValue,      /
27e6d 2a 20 54 6f 6b 65 6e 20 66 6f 72 20 3c 76 61 6c  * Token for <val
27e6e 75 65 3e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a  ue>, or NULL */.
27e6f 20 20 69 6e 74 20 6d 69 6e 75 73 46 6c 61 67 20    int minusFlag 
27e70 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
27e71 20 61 20 27 2d 27 20 73 69 67 6e 20 70 72 65 63   a '-' sign prec
27e72 65 64 65 64 20 3c 76 61 6c 75 65 3e 20 2a 2f 0a  eded <value> */.
27e73 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4c 65 66 74  ){.  char *zLeft
27e74 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e   = 0;       /* N
27e75 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54  ul-terminated UT
27e76 46 2d 38 20 73 74 72 69 6e 67 20 3c 69 64 3e 20  F-8 string <id> 
27e77 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 52 69 67 68  */.  char *zRigh
27e78 74 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e  t = 0;      /* N
27e79 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54  ul-terminated UT
27e7a 46 2d 38 20 73 74 72 69 6e 67 20 3c 76 61 6c 75  F-8 string <valu
27e7b 65 3e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20  e>, or NULL */. 
27e7c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
27e7d 20 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 64   = 0;   /* The d
27e7e 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a  atabase name */.
27e7f 20 20 54 6f 6b 65 6e 20 2a 70 49 64 3b 20 20 20    Token *pId;   
27e80 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
27e81 74 65 72 20 74 6f 20 3c 69 64 3e 20 74 6f 6b 65  ter to <id> toke
27e82 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  n */.  int iDb; 
27e83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
27e84 20 44 61 74 61 62 61 73 65 20 69 6e 64 65 78 20   Database index 
27e85 66 6f 72 20 3c 64 61 74 61 62 61 73 65 3e 20 2a  for <database> *
27e86 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
27e87 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
27e88 44 62 20 2a 70 44 62 3b 0a 20 20 56 64 62 65 20  Db *pDb;.  Vdbe 
27e89 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
27e8a 62 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  be = sqlite3Vdbe
27e8b 43 72 65 61 74 65 28 64 62 29 3b 0a 20 20 69 66  Create(db);.  if
27e8c 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( v==0 ) return;
27e8d 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20  .  pParse->nMem 
27e8e 3d 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65 72  = 2;..  /* Inter
27e8f 70 72 65 74 20 74 68 65 20 5b 64 61 74 61 62 61  pret the [databa
27e90 73 65 2e 5d 20 70 61 72 74 20 6f 66 20 74 68 65  se.] part of the
27e91 20 70 72 61 67 6d 61 20 73 74 61 74 65 6d 65 6e   pragma statemen
27e92 74 2e 20 69 44 62 20 69 73 20 74 68 65 0a 20 20  t. iDb is the.  
27e93 2a 2a 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  ** index of the 
27e94 64 61 74 61 62 61 73 65 20 74 68 69 73 20 70 72  database this pr
27e95 61 67 6d 61 20 69 73 20 62 65 69 6e 67 20 61 70  agma is being ap
27e96 70 6c 69 65 64 20 74 6f 20 69 6e 20 64 62 2e 61  plied to in db.a
27e97 44 62 5b 5d 2e 20 2a 2f 0a 20 20 69 44 62 20 3d  Db[]. */.  iDb =
27e98 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e   sqlite3TwoPartN
27e99 61 6d 65 28 70 50 61 72 73 65 2c 20 70 49 64 31  ame(pParse, pId1
27e9a 2c 20 70 49 64 32 2c 20 26 70 49 64 29 3b 0a 20  , pId2, &pId);. 
27e9b 20 69 66 28 20 69 44 62 3c 30 20 29 20 72 65 74   if( iDb<0 ) ret
27e9c 75 72 6e 3b 0a 20 20 70 44 62 20 3d 20 26 64 62  urn;.  pDb = &db
27e9d 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a 20 20 2f  ->aDb[iDb];..  /
27e9e 2a 20 49 66 20 74 68 65 20 74 65 6d 70 20 64 61  * If the temp da
27e9f 74 61 62 61 73 65 20 68 61 73 20 62 65 65 6e 20  tabase has been 
27ea0 65 78 70 6c 69 63 69 74 6c 79 20 6e 61 6d 65 64  explicitly named
27ea1 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   as part of the 
27ea2 0a 20 20 2a 2a 20 70 72 61 67 6d 61 2c 20 6d 61  .  ** pragma, ma
27ea3 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 6f 70  ke sure it is op
27ea4 65 6e 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20  en. .  */.  if( 
27ea5 69 44 62 3d 3d 31 20 26 26 20 73 71 6c 69 74 65  iDb==1 && sqlite
27ea6 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73  3OpenTempDatabas
27ea7 65 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20  e(pParse) ){.   
27ea8 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
27ea9 7a 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 4e  zLeft = sqlite3N
27eaa 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
27eab 20 70 49 64 29 3b 0a 20 20 69 66 28 20 21 7a 4c   pId);.  if( !zL
27eac 65 66 74 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  eft ) return;.  
27ead 69 66 28 20 6d 69 6e 75 73 46 6c 61 67 20 29 7b  if( minusFlag ){
27eae 0a 20 20 20 20 7a 52 69 67 68 74 20 3d 20 73 71  .    zRight = sq
27eaf 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
27eb0 20 22 2d 25 54 22 2c 20 70 56 61 6c 75 65 29 3b   "-%T", pValue);
27eb1 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 52  .  }else{.    zR
27eb2 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 4e 61  ight = sqlite3Na
27eb3 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
27eb4 70 56 61 6c 75 65 29 3b 0a 20 20 7d 0a 0a 20 20  pValue);.  }..  
27eb5 61 73 73 65 72 74 28 20 70 49 64 32 20 29 3b 0a  assert( pId2 );.
27eb6 20 20 7a 44 62 20 3d 20 70 49 64 32 2d 3e 6e 3e    zDb = pId2->n>
27eb7 30 20 3f 20 70 44 62 2d 3e 7a 4e 61 6d 65 20 3a  0 ? pDb->zName :
27eb8 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65   0;.  if( sqlite
27eb9 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
27eba 65 2c 20 53 51 4c 49 54 45 5f 50 52 41 47 4d 41  e, SQLITE_PRAGMA
27ebb 2c 20 7a 4c 65 66 74 2c 20 7a 52 69 67 68 74 2c  , zLeft, zRight,
27ebc 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 67 6f 74   zDb) ){.    got
27ebd 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20  o pragma_out;.  
27ebe 7d 0a 20 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  }. .#ifndef SQLI
27ebf 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52  TE_OMIT_PAGER_PR
27ec0 41 47 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20  AGMAS.  /*.  ** 
27ec1 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
27ec2 65 2e 5d 64 65 66 61 75 6c 74 5f 63 61 63 68 65  e.]default_cache
27ec3 5f 73 69 7a 65 0a 20 20 2a 2a 20 20 50 52 41 47  _size.  **  PRAG
27ec4 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 64 65  MA [database.]de
27ec5 66 61 75 6c 74 5f 63 61 63 68 65 5f 73 69 7a 65  fault_cache_size
27ec6 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  =N.  **.  ** The
27ec7 20 66 69 72 73 74 20 66 6f 72 6d 20 72 65 70 6f   first form repo
27ec8 72 74 73 20 74 68 65 20 63 75 72 72 65 6e 74 20  rts the current 
27ec9 70 65 72 73 69 73 74 65 6e 74 20 73 65 74 74 69  persistent setti
27eca 6e 67 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20  ng for the.  ** 
27ecb 70 61 67 65 20 63 61 63 68 65 20 73 69 7a 65 2e  page cache size.
27ecc 20 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75    The value retu
27ecd 72 6e 65 64 20 69 73 20 74 68 65 20 6d 61 78 69  rned is the maxi
27ece 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20  mum number of.  
27ecf 2a 2a 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  ** pages in the 
27ed0 70 61 67 65 20 63 61 63 68 65 2e 20 20 54 68 65  page cache.  The
27ed1 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20 73 65 74   second form set
27ed2 73 20 62 6f 74 68 20 74 68 65 20 63 75 72 72 65  s both the curre
27ed3 6e 74 0a 20 20 2a 2a 20 70 61 67 65 20 63 61 63  nt.  ** page cac
27ed4 68 65 20 73 69 7a 65 20 76 61 6c 75 65 20 61 6e  he size value an
27ed5 64 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 74  d the persistent
27ed6 20 70 61 67 65 20 63 61 63 68 65 20 73 69 7a 65   page cache size
27ed7 20 76 61 6c 75 65 0a 20 20 2a 2a 20 73 74 6f 72   value.  ** stor
27ed8 65 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ed in the databa
27ed9 73 65 20 66 69 6c 65 2e 0a 20 20 2a 2a 0a 20 20  se file..  **.  
27eda 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 63  ** The default c
27edb 61 63 68 65 20 73 69 7a 65 20 69 73 20 73 74 6f  ache size is sto
27edc 72 65 64 20 69 6e 20 6d 65 74 61 2d 76 61 6c 75  red in meta-valu
27edd 65 20 32 20 6f 66 20 70 61 67 65 20 31 20 6f 66  e 2 of page 1 of
27ede 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61   the.  ** databa
27edf 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 63 61  se file.  The ca
27ee0 63 68 65 20 73 69 7a 65 20 69 73 20 61 63 74 75  che size is actu
27ee1 61 6c 6c 79 20 74 68 65 20 61 62 73 6f 6c 75 74  ally the absolut
27ee2 65 20 76 61 6c 75 65 20 6f 66 0a 20 20 2a 2a 20  e value of.  ** 
27ee3 74 68 69 73 20 6d 65 6d 6f 72 79 20 6c 6f 63 61  this memory loca
27ee4 74 69 6f 6e 2e 20 20 54 68 65 20 73 69 67 6e 20  tion.  The sign 
27ee5 6f 66 20 6d 65 74 61 2d 76 61 6c 75 65 20 32 20  of meta-value 2 
27ee6 64 65 74 65 72 6d 69 6e 65 73 20 74 68 65 0a 20  determines the. 
27ee7 20 2a 2a 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20   ** synchronous 
27ee8 73 65 74 74 69 6e 67 2e 20 20 41 20 6e 65 67 61  setting.  A nega
27ee9 74 69 76 65 20 76 61 6c 75 65 20 6d 65 61 6e 73  tive value means
27eea 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20   synchronous is 
27eeb 6f 66 66 0a 20 20 2a 2a 20 61 6e 64 20 61 20 70  off.  ** and a p
27eec 6f 73 69 74 69 76 65 20 76 61 6c 75 65 20 6d 65  ositive value me
27eed 61 6e 73 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20  ans synchronous 
27eee 69 73 20 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66  is on..  */.  if
27eef 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
27ef0 28 7a 4c 65 66 74 2c 22 64 65 66 61 75 6c 74 5f  (zLeft,"default_
27ef1 63 61 63 68 65 5f 73 69 7a 65 22 29 3d 3d 30 20  cache_size")==0 
27ef2 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f  ){.    static co
27ef3 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 67  nst VdbeOpList g
27ef4 65 74 43 61 63 68 65 53 69 7a 65 5b 5d 20 3d 20  etCacheSize[] = 
27ef5 7b 0a 20 20 20 20 20 20 7b 20 4f 50 5f 54 72 61  {.      { OP_Tra
27ef6 6e 73 61 63 74 69 6f 6e 2c 20 30 2c 20 30 2c 20  nsaction, 0, 0, 
27ef7 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20 20 20         0},      
27ef8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27ef9 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20     /* 0 */.     
27efa 20 7b 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65   { OP_ReadCookie
27efb 2c 20 20 30 2c 20 31 2c 20 20 20 20 20 20 20 20  ,  0, 1,        
27efc 42 54 52 45 45 5f 44 45 46 41 55 4c 54 5f 43 41  BTREE_DEFAULT_CA
27efd 43 48 45 5f 53 49 5a 45 7d 2c 20 20 2f 2a 20 31  CHE_SIZE},  /* 1
27efe 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49   */.      { OP_I
27eff 66 50 6f 73 2c 20 20 20 20 20 20 20 31 2c 20 37  fPos,       1, 7
27f00 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20  ,        0},.   
27f01 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c     { OP_Integer,
27f02 20 20 20 20 20 30 2c 20 32 2c 20 20 20 20 20 20       0, 2,      
27f03 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50    0},.      { OP
27f04 5f 53 75 62 74 72 61 63 74 2c 20 20 20 20 31 2c  _Subtract,    1,
27f05 20 32 2c 20 20 20 20 20 20 20 20 31 7d 2c 0a 20   2,        1},. 
27f06 20 20 20 20 20 7b 20 4f 50 5f 49 66 50 6f 73 2c       { OP_IfPos,
27f07 20 20 20 20 20 20 20 31 2c 20 37 2c 20 20 20 20         1, 7,    
27f08 20 20 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20      0},.      { 
27f09 4f 50 5f 49 6e 74 65 67 65 72 2c 20 20 20 20 20  OP_Integer,     
27f0a 30 2c 20 31 2c 20 20 20 20 20 20 20 20 30 7d 2c  0, 1,        0},
27f0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27f0c 20 20 20 20 20 20 20 20 20 2f 2a 20 36 20 2a 2f           /* 6 */
27f0d 0a 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 73 75  .      { OP_Resu
27f0e 6c 74 52 6f 77 2c 20 20 20 31 2c 20 31 2c 20 20  ltRow,   1, 1,  
27f0f 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 7d 3b        0},.    };
27f10 0a 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20  .    int addr;. 
27f11 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
27f12 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
27f13 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f   ) goto pragma_o
27f14 75 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ut;.    sqlite3V
27f15 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20  dbeUsesBtree(v, 
27f16 69 44 62 29 3b 0a 20 20 20 20 69 66 28 20 21 7a  iDb);.    if( !z
27f17 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 73  Right ){.      s
27f18 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
27f19 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20  Cols(v, 1);.    
27f1a 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
27f1b 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
27f1c 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 63 61 63  LNAME_NAME, "cac
27f1d 68 65 5f 73 69 7a 65 22 2c 20 53 51 4c 49 54 45  he_size", SQLITE
27f1e 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
27f1f 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20  pParse->nMem += 
27f20 32 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20  2;.      addr = 
27f21 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27f22 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a  List(v, ArraySiz
27f23 65 28 67 65 74 43 61 63 68 65 53 69 7a 65 29 2c  e(getCacheSize),
27f24 20 67 65 74 43 61 63 68 65 53 69 7a 65 29 3b 0a   getCacheSize);.
27f25 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
27f26 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64  eChangeP1(v, add
27f27 72 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73  r, iDb);.      s
27f28 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
27f29 50 31 28 76 2c 20 61 64 64 72 2b 31 2c 20 69 44  P1(v, addr+1, iD
27f2a 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
27f2b 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c  3VdbeChangeP1(v,
27f2c 20 61 64 64 72 2b 36 2c 20 53 51 4c 49 54 45 5f   addr+6, SQLITE_
27f2d 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49  DEFAULT_CACHE_SI
27f2e 5a 45 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ZE);.    }else{.
27f2f 20 20 20 20 20 20 69 6e 74 20 73 69 7a 65 20 3d        int size =
27f30 20 61 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20   atoi(zRight);. 
27f31 20 20 20 20 20 69 66 28 20 73 69 7a 65 3c 30 20       if( size<0 
27f32 29 20 73 69 7a 65 20 3d 20 2d 73 69 7a 65 3b 0a  ) size = -size;.
27f33 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67        sqlite3Beg
27f34 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e  inWriteOperation
27f35 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29  (pParse, 0, iDb)
27f36 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
27f37 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
27f38 49 6e 74 65 67 65 72 2c 20 73 69 7a 65 2c 20 31  Integer, size, 1
27f39 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
27f3a 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
27f3b 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69 44 62  _ReadCookie, iDb
27f3c 2c 20 32 2c 20 42 54 52 45 45 5f 44 45 46 41 55  , 2, BTREE_DEFAU
27f3d 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 29 3b 0a  LT_CACHE_SIZE);.
27f3e 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
27f3f 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
27f40 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 32 2c 20 30  , OP_IfPos, 2, 0
27f41 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
27f42 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
27f43 5f 49 6e 74 65 67 65 72 2c 20 2d 73 69 7a 65 2c  _Integer, -size,
27f44 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   1);.      sqlit
27f45 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
27f46 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20 73  , addr);.      s
27f47 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
27f48 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65  (v, OP_SetCookie
27f49 2c 20 69 44 62 2c 20 42 54 52 45 45 5f 44 45 46  , iDb, BTREE_DEF
27f4a 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 2c  AULT_CACHE_SIZE,
27f4b 20 31 29 3b 0a 20 20 20 20 20 20 70 44 62 2d 3e   1);.      pDb->
27f4c 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73  pSchema->cache_s
27f4d 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20  ize = size;.    
27f4e 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65    sqlite3BtreeSe
27f4f 74 43 61 63 68 65 53 69 7a 65 28 70 44 62 2d 3e  tCacheSize(pDb->
27f50 70 42 74 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d  pBt, pDb->pSchem
27f51 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a  a->cache_size);.
27f52 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20      }.  }else.. 
27f53 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41   /*.  **  PRAGMA
27f54 20 5b 64 61 74 61 62 61 73 65 2e 5d 70 61 67 65   [database.]page
27f55 5f 73 69 7a 65 0a 20 20 2a 2a 20 20 50 52 41 47  _size.  **  PRAG
27f56 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 70 61  MA [database.]pa
27f57 67 65 5f 73 69 7a 65 3d 4e 0a 20 20 2a 2a 0a 20  ge_size=N.  **. 
27f58 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 66 6f   ** The first fo
27f59 72 6d 20 72 65 70 6f 72 74 73 20 74 68 65 20 63  rm reports the c
27f5a 75 72 72 65 6e 74 20 73 65 74 74 69 6e 67 20 66  urrent setting f
27f5b 6f 72 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61  or the.  ** data
27f5c 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 20 69  base page size i
27f5d 6e 20 62 79 74 65 73 2e 20 20 54 68 65 20 73 65  n bytes.  The se
27f5e 63 6f 6e 64 20 66 6f 72 6d 20 73 65 74 73 20 74  cond form sets t
27f5f 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65  he.  ** database
27f60 20 70 61 67 65 20 73 69 7a 65 20 76 61 6c 75 65   page size value
27f61 2e 20 20 54 68 65 20 76 61 6c 75 65 20 63 61 6e  .  The value can
27f62 20 6f 6e 6c 79 20 62 65 20 73 65 74 20 69 66 0a   only be set if.
27f63 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73    ** the databas
27f64 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65  e has not yet be
27f65 65 6e 20 63 72 65 61 74 65 64 2e 0a 20 20 2a 2f  en created..  */
27f66 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
27f67 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 70 61 67  rICmp(zLeft,"pag
27f68 65 5f 73 69 7a 65 22 29 3d 3d 30 20 29 7b 0a 20  e_size")==0 ){. 
27f69 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20     Btree *pBt = 
27f6a 70 44 62 2d 3e 70 42 74 3b 0a 20 20 20 20 61 73  pDb->pBt;.    as
27f6b 73 65 72 74 28 20 70 42 74 21 3d 30 20 29 3b 0a  sert( pBt!=0 );.
27f6c 20 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20      if( !zRight 
27f6d 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 69 7a  ){.      int siz
27f6e 65 20 3d 20 41 4c 57 41 59 53 28 70 42 74 29 20  e = ALWAYS(pBt) 
27f6f 3f 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  ? sqlite3BtreeGe
27f70 74 50 61 67 65 53 69 7a 65 28 70 42 74 29 20 3a  tPageSize(pBt) :
27f71 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   0;.      return
27f72 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65  SingleInt(pParse
27f73 2c 20 22 70 61 67 65 5f 73 69 7a 65 22 2c 20 73  , "page_size", s
27f74 69 7a 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ize);.    }else{
27f75 0a 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63  .      /* Malloc
27f76 20 6d 61 79 20 66 61 69 6c 20 77 68 65 6e 20 73   may fail when s
27f77 65 74 74 69 6e 67 20 74 68 65 20 70 61 67 65 2d  etting the page-
27f78 73 69 7a 65 2c 20 61 73 20 74 68 65 72 65 20 69  size, as there i
27f79 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 0a 20 20  s an internal.  
27f7a 20 20 20 20 2a 2a 20 62 75 66 66 65 72 20 74 68      ** buffer th
27f7b 61 74 20 74 68 65 20 70 61 67 65 72 20 6d 6f 64  at the pager mod
27f7c 75 6c 65 20 72 65 73 69 7a 65 73 20 75 73 69 6e  ule resizes usin
27f7d 67 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  g sqlite3_reallo
27f7e 63 28 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  c()..      */.  
27f7f 20 20 20 20 64 62 2d 3e 6e 65 78 74 50 61 67 65      db->nextPage
27f80 73 69 7a 65 20 3d 20 61 74 6f 69 28 7a 52 69 67  size = atoi(zRig
27f81 68 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 53  ht);.      if( S
27f82 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73 71 6c  QLITE_NOMEM==sql
27f83 69 74 65 33 42 74 72 65 65 53 65 74 50 61 67 65  ite3BtreeSetPage
27f84 53 69 7a 65 28 70 42 74 2c 20 64 62 2d 3e 6e 65  Size(pBt, db->ne
27f85 78 74 50 61 67 65 73 69 7a 65 2c 20 2d 31 2c 20  xtPagesize, -1, 
27f86 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62  0) ){.        db
27f87 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
27f88 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
27f89 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a  }.  }else..  /*.
27f8a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
27f8b 74 61 62 61 73 65 2e 5d 6d 61 78 5f 70 61 67 65  tabase.]max_page
27f8c 5f 63 6f 75 6e 74 0a 20 20 2a 2a 20 20 50 52 41  _count.  **  PRA
27f8d 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6d  GMA [database.]m
27f8e 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 3d 4e 0a  ax_page_count=N.
27f8f 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 69    **.  ** The fi
27f90 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72 74 73  rst form reports
27f91 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 65 74   the current set
27f92 74 69 6e 67 20 66 6f 72 20 74 68 65 0a 20 20 2a  ting for the.  *
27f93 2a 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  * maximum number
27f94 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65   of pages in the
27f95 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
27f96 20 54 68 65 20 0a 20 20 2a 2a 20 73 65 63 6f 6e   The .  ** secon
27f97 64 20 66 6f 72 6d 20 61 74 74 65 6d 70 74 73 20  d form attempts 
27f98 74 6f 20 63 68 61 6e 67 65 20 74 68 69 73 20 73  to change this s
27f99 65 74 74 69 6e 67 2e 20 20 42 6f 74 68 0a 20 20  etting.  Both.  
27f9a 2a 2a 20 66 6f 72 6d 73 20 72 65 74 75 72 6e 20  ** forms return 
27f9b 74 68 65 20 63 75 72 72 65 6e 74 20 73 65 74 74  the current sett
27f9c 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ing..  */.  if( 
27f9d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
27f9e 4c 65 66 74 2c 22 6d 61 78 5f 70 61 67 65 5f 63  Left,"max_page_c
27f9f 6f 75 6e 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20  ount")==0 ){.   
27fa0 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 70 44   Btree *pBt = pD
27fa1 62 2d 3e 70 42 74 3b 0a 20 20 20 20 69 6e 74 20  b->pBt;.    int 
27fa2 6e 65 77 4d 61 78 20 3d 20 30 3b 0a 20 20 20 20  newMax = 0;.    
27fa3 61 73 73 65 72 74 28 20 70 42 74 21 3d 30 20 29  assert( pBt!=0 )
27fa4 3b 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68 74  ;.    if( zRight
27fa5 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4d 61 78   ){.      newMax
27fa6 20 3d 20 61 74 6f 69 28 7a 52 69 67 68 74 29 3b   = atoi(zRight);
27fa7 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 41  .    }.    if( A
27fa8 4c 57 41 59 53 28 70 42 74 29 20 29 7b 0a 20 20  LWAYS(pBt) ){.  
27fa9 20 20 20 20 6e 65 77 4d 61 78 20 3d 20 73 71 6c      newMax = sql
27faa 69 74 65 33 42 74 72 65 65 4d 61 78 50 61 67 65  ite3BtreeMaxPage
27fab 43 6f 75 6e 74 28 70 42 74 2c 20 6e 65 77 4d 61  Count(pBt, newMa
27fac 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  x);.    }.    re
27fad 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50  turnSingleInt(pP
27fae 61 72 73 65 2c 20 22 6d 61 78 5f 70 61 67 65 5f  arse, "max_page_
27faf 63 6f 75 6e 74 22 2c 20 6e 65 77 4d 61 78 29 3b  count", newMax);
27fb0 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20  .  }else..  /*. 
27fb1 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
27fb2 61 62 61 73 65 2e 5d 70 61 67 65 5f 63 6f 75 6e  abase.]page_coun
27fb3 74 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75  t.  **.  ** Retu
27fb4 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
27fb5 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 73 70   pages in the sp
27fb6 65 63 69 66 69 65 64 20 64 61 74 61 62 61 73 65  ecified database
27fb7 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
27fb8 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
27fb9 74 2c 22 70 61 67 65 5f 63 6f 75 6e 74 22 29 3d  t,"page_count")=
27fba 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 52  =0 ){.    int iR
27fbb 65 67 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  eg;.    if( sqli
27fbc 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
27fbd 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61  arse) ) goto pra
27fbe 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 73 71 6c  gma_out;.    sql
27fbf 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63  ite3CodeVerifySc
27fc0 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62  hema(pParse, iDb
27fc1 29 3b 0a 20 20 20 20 69 52 65 67 20 3d 20 2b 2b  );.    iReg = ++
27fc2 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
27fc3 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27fc4 4f 70 32 28 76 2c 20 4f 50 5f 50 61 67 65 63 6f  Op2(v, OP_Pageco
27fc5 75 6e 74 2c 20 69 44 62 2c 20 69 52 65 67 29 3b  unt, iDb, iReg);
27fc6 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
27fc7 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73  AddOp2(v, OP_Res
27fc8 75 6c 74 52 6f 77 2c 20 69 52 65 67 2c 20 31 29  ultRow, iReg, 1)
27fc9 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
27fca 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31  eSetNumCols(v, 1
27fcb 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
27fcc 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
27fcd 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
27fce 20 22 70 61 67 65 5f 63 6f 75 6e 74 22 2c 20 53   "page_count", S
27fcf 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
27fd0 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a   }else..  /*.  *
27fd1 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62  *  PRAGMA [datab
27fd2 61 73 65 2e 5d 6c 6f 63 6b 69 6e 67 5f 6d 6f 64  ase.]locking_mod
27fd3 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  e.  **  PRAGMA [
27fd4 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 69 6e  database.]lockin
27fd5 67 5f 6d 6f 64 65 20 3d 20 28 6e 6f 72 6d 61 6c  g_mode = (normal
27fd6 7c 65 78 63 6c 75 73 69 76 65 29 0a 20 20 2a 2f  |exclusive).  */
27fd7 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
27fd8 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 6c 6f 63  rICmp(zLeft,"loc
27fd9 6b 69 6e 67 5f 6d 6f 64 65 22 29 3d 3d 30 20 29  king_mode")==0 )
27fda 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
27fdb 20 2a 7a 52 65 74 20 3d 20 22 6e 6f 72 6d 61 6c   *zRet = "normal
27fdc 22 3b 0a 20 20 20 20 69 6e 74 20 65 4d 6f 64 65  ";.    int eMode
27fdd 20 3d 20 67 65 74 4c 6f 63 6b 69 6e 67 4d 6f 64   = getLockingMod
27fde 65 28 7a 52 69 67 68 74 29 3b 0a 0a 20 20 20 20  e(zRight);..    
27fdf 69 66 28 20 70 49 64 32 2d 3e 6e 3d 3d 30 20 26  if( pId2->n==0 &
27fe0 26 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c  & eMode==PAGER_L
27fe1 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59  OCKINGMODE_QUERY
27fe2 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 69 6d   ){.      /* Sim
27fe3 70 6c 65 20 22 50 52 41 47 4d 41 20 6c 6f 63 6b  ple "PRAGMA lock
27fe4 69 6e 67 5f 6d 6f 64 65 3b 22 20 73 74 61 74 65  ing_mode;" state
27fe5 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20 61 20  ment. This is a 
27fe6 71 75 65 72 79 20 66 6f 72 0a 20 20 20 20 20 20  query for.      
27fe7 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 64  ** the current d
27fe8 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 20 6d  efault locking m
27fe9 6f 64 65 20 28 77 68 69 63 68 20 6d 61 79 20 62  ode (which may b
27fea 65 20 64 69 66 66 65 72 65 6e 74 20 74 6f 0a 20  e different to. 
27feb 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 6f 63 6b       ** the lock
27fec 69 6e 67 2d 6d 6f 64 65 20 6f 66 20 74 68 65 20  ing-mode of the 
27fed 6d 61 69 6e 20 64 61 74 61 62 61 73 65 29 2e 0a  main database)..
27fee 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 65        */.      e
27fef 4d 6f 64 65 20 3d 20 64 62 2d 3e 64 66 6c 74 4c  Mode = db->dfltL
27ff0 6f 63 6b 4d 6f 64 65 3b 0a 20 20 20 20 7d 65 6c  ockMode;.    }el
27ff1 73 65 7b 0a 20 20 20 20 20 20 50 61 67 65 72 20  se{.      Pager 
27ff2 2a 70 50 61 67 65 72 3b 0a 20 20 20 20 20 20 69  *pPager;.      i
27ff3 66 28 20 70 49 64 32 2d 3e 6e 3d 3d 30 20 29 7b  f( pId2->n==0 ){
27ff4 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73  .        /* This
27ff5 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20   indicates that 
27ff6 6e 6f 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  no database name
27ff7 20 77 61 73 20 73 70 65 63 69 66 69 65 64 20 61   was specified a
27ff8 73 20 70 61 72 74 0a 20 20 20 20 20 20 20 20 2a  s part.        *
27ff9 2a 20 6f 66 20 74 68 65 20 50 52 41 47 4d 41 20  * of the PRAGMA 
27ffa 63 6f 6d 6d 61 6e 64 2e 20 49 6e 20 74 68 69 73  command. In this
27ffb 20 63 61 73 65 20 74 68 65 20 6c 6f 63 6b 69 6e   case the lockin
27ffc 67 2d 6d 6f 64 65 20 6d 75 73 74 20 62 65 0a 20  g-mode must be. 
27ffd 20 20 20 20 20 20 20 2a 2a 20 73 65 74 20 6f 6e         ** set on
27ffe 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61   all attached da
27fff 74 61 62 61 73 65 73 2c 20 61 73 20 77 65 6c 6c  tabases, as well
28000 20 61 73 20 74 68 65 20 6d 61 69 6e 20 64 62 20   as the main db 
28001 66 69 6c 65 2e 0a 20 20 20 20 20 20 20 20 2a 2a  file..        **
28002 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 6c 73 6f  .        ** Also
28003 2c 20 74 68 65 20 73 71 6c 69 74 65 33 2e 64 66  , the sqlite3.df
28004 6c 74 4c 6f 63 6b 4d 6f 64 65 20 76 61 72 69 61  ltLockMode varia
28005 62 6c 65 20 69 73 20 73 65 74 20 73 6f 20 74 68  ble is set so th
28006 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e  at.        ** an
28007 79 20 73 75 62 73 65 71 75 65 6e 74 6c 79 20 61  y subsequently a
28008 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
28009 73 20 61 6c 73 6f 20 75 73 65 20 74 68 65 20 73  s also use the s
2800a 70 65 63 69 66 69 65 64 0a 20 20 20 20 20 20 20  pecified.       
2800b 20 2a 2a 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65   ** locking mode
2800c 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
2800d 20 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20       int ii;.   
2800e 20 20 20 20 20 61 73 73 65 72 74 28 70 44 62 3d       assert(pDb=
2800f 3d 26 64 62 2d 3e 61 44 62 5b 30 5d 29 3b 0a 20  =&db->aDb[0]);. 
28010 20 20 20 20 20 20 20 66 6f 72 28 69 69 3d 32 3b         for(ii=2;
28011 20 69 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 69 2b   ii<db->nDb; ii+
28012 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50  +){.          pP
28013 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74  ager = sqlite3Bt
28014 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61 44 62  reePager(db->aDb
28015 5b 69 69 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20  [ii].pBt);.     
28016 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
28017 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 70 50 61  rLockingMode(pPa
28018 67 65 72 2c 20 65 4d 6f 64 65 29 3b 0a 20 20 20  ger, eMode);.   
28019 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64       }.        d
2801a 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 20  b->dfltLockMode 
2801b 3d 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20 20  = (u8)eMode;.   
2801c 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65     }.      pPage
2801d 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  r = sqlite3Btree
2801e 50 61 67 65 72 28 70 44 62 2d 3e 70 42 74 29 3b  Pager(pDb->pBt);
2801f 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d 20 73  .      eMode = s
28020 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69  qlite3PagerLocki
28021 6e 67 4d 6f 64 65 28 70 50 61 67 65 72 2c 20 65  ngMode(pPager, e
28022 4d 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  Mode);.    }..  
28023 20 20 61 73 73 65 72 74 28 65 4d 6f 64 65 3d 3d    assert(eMode==
28024 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
28025 45 5f 4e 4f 52 4d 41 4c 7c 7c 65 4d 6f 64 65 3d  E_NORMAL||eMode=
28026 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f  =PAGER_LOCKINGMO
28027 44 45 5f 45 58 43 4c 55 53 49 56 45 29 3b 0a 20  DE_EXCLUSIVE);. 
28028 20 20 20 69 66 28 20 65 4d 6f 64 65 3d 3d 50 41     if( eMode==PA
28029 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
2802a 45 58 43 4c 55 53 49 56 45 20 29 7b 0a 20 20 20  EXCLUSIVE ){.   
2802b 20 20 20 7a 52 65 74 20 3d 20 22 65 78 63 6c 75     zRet = "exclu
2802c 73 69 76 65 22 3b 0a 20 20 20 20 7d 0a 20 20 20  sive";.    }.   
2802d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
2802e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20  umCols(v, 1);.  
2802f 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
28030 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
28031 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6c 6f 63  LNAME_NAME, "loc
28032 6b 69 6e 67 5f 6d 6f 64 65 22 2c 20 53 51 4c 49  king_mode", SQLI
28033 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
28034 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28035 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
28036 20 30 2c 20 31 2c 20 30 2c 20 7a 52 65 74 2c 20   0, 1, 0, zRet, 
28037 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
28038 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
28039 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31 29  ResultRow, 1, 1)
2803a 3b 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a  ;.  }else..  /*.
2803b 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
2803c 74 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c 5f  tabase.]journal_
2803d 6d 6f 64 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d  mode.  **  PRAGM
2803e 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6a 6f 75  A [database.]jou
2803f 72 6e 61 6c 5f 6d 6f 64 65 20 3d 20 28 64 65 6c  rnal_mode = (del
28040 65 74 65 7c 70 65 72 73 69 73 74 7c 6f 66 66 7c  ete|persist|off|
28041 74 72 75 6e 63 61 74 65 7c 6d 65 6d 6f 72 79 29  truncate|memory)
28042 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
28043 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
28044 2c 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 22 29  ,"journal_mode")
28045 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 65  ==0 ){.    int e
28046 4d 6f 64 65 3b 0a 20 20 20 20 73 74 61 74 69 63  Mode;.    static
28047 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 61 7a   char * const az
28048 4d 6f 64 65 4e 61 6d 65 5b 5d 20 3d 20 7b 0a 20  ModeName[] = {. 
28049 20 20 20 20 20 22 64 65 6c 65 74 65 22 2c 20 22       "delete", "
2804a 70 65 72 73 69 73 74 22 2c 20 22 6f 66 66 22 2c  persist", "off",
2804b 20 22 74 72 75 6e 63 61 74 65 22 2c 20 22 6d 65   "truncate", "me
2804c 6d 6f 72 79 22 0a 20 20 20 20 7d 3b 0a 0a 20 20  mory".    };..  
2804d 20 20 69 66 28 20 7a 52 69 67 68 74 3d 3d 30 20    if( zRight==0 
2804e 29 7b 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d  ){.      eMode =
2804f 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f   PAGER_JOURNALMO
28050 44 45 5f 51 55 45 52 59 3b 0a 20 20 20 20 7d 65  DE_QUERY;.    }e
28051 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e  lse{.      int n
28052 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
28053 33 30 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20  30(zRight);.    
28054 20 20 65 4d 6f 64 65 20 3d 20 73 69 7a 65 6f 66    eMode = sizeof
28055 28 61 7a 4d 6f 64 65 4e 61 6d 65 29 2f 73 69 7a  (azModeName)/siz
28056 65 6f 66 28 61 7a 4d 6f 64 65 4e 61 6d 65 5b 30  eof(azModeName[0
28057 5d 29 20 2d 20 31 3b 0a 20 20 20 20 20 20 77 68  ]) - 1;.      wh
28058 69 6c 65 28 20 65 4d 6f 64 65 3e 3d 30 20 26 26  ile( eMode>=0 &&
28059 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
2805a 28 7a 52 69 67 68 74 2c 20 61 7a 4d 6f 64 65 4e  (zRight, azModeN
2805b 61 6d 65 5b 65 4d 6f 64 65 5d 2c 20 6e 29 21 3d  ame[eMode], n)!=
2805c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 65 4d 6f  0 ){.        eMo
2805d 64 65 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  de--;.      }.  
2805e 20 20 7d 0a 20 20 20 20 69 66 28 20 70 49 64 32    }.    if( pId2
2805f 2d 3e 6e 3d 3d 30 20 26 26 20 65 4d 6f 64 65 3d  ->n==0 && eMode=
28060 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
28061 44 45 5f 51 55 45 52 59 20 29 7b 0a 20 20 20 20  DE_QUERY ){.    
28062 20 20 2f 2a 20 53 69 6d 70 6c 65 20 22 50 52 41    /* Simple "PRA
28063 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65  GMA journal_mode
28064 3b 22 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68  ;" statement. Th
28065 69 73 20 69 73 20 61 20 71 75 65 72 79 20 66 6f  is is a query fo
28066 72 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63  r.      ** the c
28067 75 72 72 65 6e 74 20 64 65 66 61 75 6c 74 20 6a  urrent default j
28068 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 28 77 68 69  ournal mode (whi
28069 63 68 20 6d 61 79 20 62 65 20 64 69 66 66 65 72  ch may be differ
2806a 65 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20  ent to.      ** 
2806b 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65  the journal-mode
2806c 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74   of the main dat
2806d 61 62 61 73 65 29 2e 0a 20 20 20 20 20 20 2a 2f  abase)..      */
2806e 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d 20 64  .      eMode = d
2806f 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f  b->dfltJournalMo
28070 64 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  de;.    }else{. 
28071 20 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67       Pager *pPag
28072 65 72 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49  er;.      if( pI
28073 64 32 2d 3e 6e 3d 3d 30 20 29 7b 0a 20 20 20 20  d2->n==0 ){.    
28074 20 20 20 20 2f 2a 20 54 68 69 73 20 69 6e 64 69      /* This indi
28075 63 61 74 65 73 20 74 68 61 74 20 6e 6f 20 64 61  cates that no da
28076 74 61 62 61 73 65 20 6e 61 6d 65 20 77 61 73 20  tabase name was 
28077 73 70 65 63 69 66 69 65 64 20 61 73 20 70 61 72  specified as par
28078 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20  t.        ** of 
28079 74 68 65 20 50 52 41 47 4d 41 20 63 6f 6d 6d 61  the PRAGMA comma
2807a 6e 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  nd. In this case
2807b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64   the journal-mod
2807c 65 20 6d 75 73 74 20 62 65 0a 20 20 20 20 20 20  e must be.      
2807d 20 20 2a 2a 20 73 65 74 20 6f 6e 20 61 6c 6c 20    ** set on all 
2807e 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
2807f 65 73 2c 20 61 73 20 77 65 6c 6c 20 61 73 20 74  es, as well as t
28080 68 65 20 6d 61 69 6e 20 64 62 20 66 69 6c 65 2e  he main db file.
28081 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
28082 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 74 68 65      ** Also, the
28083 20 73 71 6c 69 74 65 33 2e 64 66 6c 74 4a 6f 75   sqlite3.dfltJou
28084 72 6e 61 6c 4d 6f 64 65 20 76 61 72 69 61 62 6c  rnalMode variabl
28085 65 20 69 73 20 73 65 74 20 73 6f 20 74 68 61 74  e is set so that
28086 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 20  .        ** any 
28087 73 75 62 73 65 71 75 65 6e 74 6c 79 20 61 74 74  subsequently att
28088 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20  ached databases 
28089 61 6c 73 6f 20 75 73 65 20 74 68 65 20 73 70 65  also use the spe
2808a 63 69 66 69 65 64 0a 20 20 20 20 20 20 20 20 2a  cified.        *
2808b 2a 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 2e 0a  * journal mode..
2808c 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
2808d 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 20     int ii;.     
2808e 20 20 20 61 73 73 65 72 74 28 70 44 62 3d 3d 26     assert(pDb==&
2808f 64 62 2d 3e 61 44 62 5b 30 5d 29 3b 0a 20 20 20  db->aDb[0]);.   
28090 20 20 20 20 20 66 6f 72 28 69 69 3d 31 3b 20 69       for(ii=1; i
28091 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 69 2b 2b 29  i<db->nDb; ii++)
28092 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
28093 64 62 2d 3e 61 44 62 5b 69 69 5d 2e 70 42 74 20  db->aDb[ii].pBt 
28094 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ){.            p
28095 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42  Pager = sqlite3B
28096 74 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61 44  treePager(db->aD
28097 62 5b 69 69 5d 2e 70 42 74 29 3b 0a 20 20 20 20  b[ii].pBt);.    
28098 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
28099 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28  agerJournalMode(
2809a 70 50 61 67 65 72 2c 20 65 4d 6f 64 65 29 3b 0a  pPager, eMode);.
2809b 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2809c 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 62      }.        db
2809d 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64  ->dfltJournalMod
2809e 65 20 3d 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20  e = (u8)eMode;. 
2809f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61       }.      pPa
280a0 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72  ger = sqlite3Btr
280a1 65 65 50 61 67 65 72 28 70 44 62 2d 3e 70 42 74  eePager(pDb->pBt
280a2 29 3b 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d  );.      eMode =
280a3 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75   sqlite3PagerJou
280a4 72 6e 61 6c 4d 6f 64 65 28 70 50 61 67 65 72 2c  rnalMode(pPager,
280a5 20 65 4d 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20   eMode);.    }. 
280a6 20 20 20 61 73 73 65 72 74 28 20 65 4d 6f 64 65     assert( eMode
280a7 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
280a8 4f 44 45 5f 44 45 4c 45 54 45 0a 20 20 20 20 20  ODE_DELETE.     
280a9 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64           || eMod
280aa 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
280ab 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a 20 20  MODE_TRUNCATE.  
280ac 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65              || e
280ad 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
280ae 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a  NALMODE_PERSIST.
280af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c                ||
280b0 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f   eMode==PAGER_JO
280b1 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a 20 20  URNALMODE_OFF.  
280b2 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65              || e
280b3 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
280b4 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29  NALMODE_MEMORY )
280b5 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
280b6 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31  eSetNumCols(v, 1
280b7 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
280b8 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
280b9 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
280ba 20 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 22 2c   "journal_mode",
280bb 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
280bc 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
280bd 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
280be 69 6e 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20 0a  ing8, 0, 1, 0, .
280bf 20 20 20 20 20 20 20 20 20 20 20 61 7a 4d 6f 64             azMod
280c0 65 4e 61 6d 65 5b 65 4d 6f 64 65 5d 2c 20 50 34  eName[eMode], P4
280c1 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71  _STATIC);.    sq
280c2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
280c3 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
280c4 20 31 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65 0a   1, 1);.  }else.
280c5 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47  .  /*.  **  PRAG
280c6 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6a 6f  MA [database.]jo
280c7 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74  urnal_size_limit
280c8 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
280c9 61 74 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c  atabase.]journal
280ca 5f 73 69 7a 65 5f 6c 69 6d 69 74 3d 4e 0a 20 20  _size_limit=N.  
280cb 2a 2a 0a 20 20 2a 2a 20 47 65 74 20 6f 72 20 73  **.  ** Get or s
280cc 65 74 20 74 68 65 20 73 69 7a 65 20 6c 69 6d 69  et the size limi
280cd 74 20 6f 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f  t on rollback jo
280ce 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 20 20 2a  urnal files..  *
280cf 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
280d0 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 6a 6f  trICmp(zLeft,"jo
280d1 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74  urnal_size_limit
280d2 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 50 61 67  ")==0 ){.    Pag
280d3 65 72 20 2a 70 50 61 67 65 72 20 3d 20 73 71 6c  er *pPager = sql
280d4 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70  ite3BtreePager(p
280d5 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 69 36  Db->pBt);.    i6
280d6 34 20 69 4c 69 6d 69 74 20 3d 20 2d 32 3b 0a 20  4 iLimit = -2;. 
280d7 20 20 20 69 66 28 20 7a 52 69 67 68 74 20 29 7b     if( zRight ){
280d8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 41 74  .      sqlite3At
280d9 6f 69 36 34 28 7a 52 69 67 68 74 2c 20 26 69 4c  oi64(zRight, &iL
280da 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 69 66 28  imit);.      if(
280db 20 69 4c 69 6d 69 74 3c 2d 31 20 29 20 69 4c 69   iLimit<-1 ) iLi
280dc 6d 69 74 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a  mit = -1;.    }.
280dd 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 73 71 6c      iLimit = sql
280de 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c  ite3PagerJournal
280df 53 69 7a 65 4c 69 6d 69 74 28 70 50 61 67 65 72  SizeLimit(pPager
280e0 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 72  , iLimit);.    r
280e1 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70  eturnSingleInt(p
280e2 50 61 72 73 65 2c 20 22 6a 6f 75 72 6e 61 6c 5f  Parse, "journal_
280e3 73 69 7a 65 5f 6c 69 6d 69 74 22 2c 20 69 4c 69  size_limit", iLi
280e4 6d 69 74 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a 23  mit);.  }else..#
280e5 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
280e6 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d  OMIT_PAGER_PRAGM
280e7 41 53 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  AS */..  /*.  **
280e8 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
280e9 73 65 2e 5d 61 75 74 6f 5f 76 61 63 75 75 6d 0a  se.]auto_vacuum.
280ea 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
280eb 74 61 62 61 73 65 2e 5d 61 75 74 6f 5f 76 61 63  tabase.]auto_vac
280ec 75 75 6d 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  uum=N.  **.  ** 
280ed 47 65 74 20 6f 72 20 73 65 74 20 74 68 65 20 76  Get or set the v
280ee 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74 61  alue of the data
280ef 62 61 73 65 20 27 61 75 74 6f 2d 76 61 63 75 75  base 'auto-vacuu
280f0 6d 27 20 70 61 72 61 6d 65 74 65 72 2e 0a 20 20  m' parameter..  
280f1 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 69 73 20  ** The value is 
280f2 6f 6e 65 20 6f 66 3a 20 20 30 20 4e 4f 4e 45 20  one of:  0 NONE 
280f3 31 20 46 55 4c 4c 20 32 20 49 4e 43 52 45 4d 45  1 FULL 2 INCREME
280f4 4e 54 41 4c 0a 20 20 2a 2f 0a 23 69 66 6e 64 65  NTAL.  */.#ifnde
280f5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
280f6 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 28 20 73  TOVACUUM.  if( s
280f7 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
280f8 65 66 74 2c 22 61 75 74 6f 5f 76 61 63 75 75 6d  eft,"auto_vacuum
280f9 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 42 74 72  ")==0 ){.    Btr
280fa 65 65 20 2a 70 42 74 20 3d 20 70 44 62 2d 3e 70  ee *pBt = pDb->p
280fb 42 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  Bt;.    assert( 
280fc 70 42 74 21 3d 30 20 29 3b 0a 20 20 20 20 69 66  pBt!=0 );.    if
280fd 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  ( sqlite3ReadSch
280fe 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20  ema(pParse) ){. 
280ff 20 20 20 20 20 67 6f 74 6f 20 70 72 61 67 6d 61       goto pragma
28100 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _out;.    }.    
28101 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a 20  if( !zRight ){. 
28102 20 20 20 20 20 69 6e 74 20 61 75 74 6f 5f 76 61       int auto_va
28103 63 75 75 6d 3b 0a 20 20 20 20 20 20 69 66 28 20  cuum;.      if( 
28104 41 4c 57 41 59 53 28 70 42 74 29 20 29 7b 0a 20  ALWAYS(pBt) ){. 
28105 20 20 20 20 20 20 20 20 61 75 74 6f 5f 76 61 63          auto_vac
28106 75 75 6d 20 3d 20 73 71 6c 69 74 65 33 42 74 72  uum = sqlite3Btr
28107 65 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28  eeGetAutoVacuum(
28108 70 42 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  pBt);.      }els
28109 65 7b 0a 20 20 20 20 20 20 20 20 20 61 75 74 6f  e{.         auto
2810a 5f 76 61 63 75 75 6d 20 3d 20 53 51 4c 49 54 45  _vacuum = SQLITE
2810b 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43  _DEFAULT_AUTOVAC
2810c 55 55 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  UUM;.      }.   
2810d 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49     returnSingleI
2810e 6e 74 28 70 50 61 72 73 65 2c 20 22 61 75 74 6f  nt(pParse, "auto
2810f 5f 76 61 63 75 75 6d 22 2c 20 61 75 74 6f 5f 76  _vacuum", auto_v
28110 61 63 75 75 6d 29 3b 0a 20 20 20 20 7d 65 6c 73  acuum);.    }els
28111 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 41 75  e{.      int eAu
28112 74 6f 20 3d 20 67 65 74 41 75 74 6f 56 61 63 75  to = getAutoVacu
28113 75 6d 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20  um(zRight);.    
28114 20 20 61 73 73 65 72 74 28 20 65 41 75 74 6f 3e    assert( eAuto>
28115 3d 30 20 26 26 20 65 41 75 74 6f 3c 3d 32 20 29  =0 && eAuto<=2 )
28116 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6e 65 78 74  ;.      db->next
28117 41 75 74 6f 76 61 63 20 3d 20 28 75 38 29 65 41  Autovac = (u8)eA
28118 75 74 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 41  uto;.      if( A
28119 4c 57 41 59 53 28 65 41 75 74 6f 3e 3d 30 29 20  LWAYS(eAuto>=0) 
2811a 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 61  ){.        /* Ca
2811b 6c 6c 20 53 65 74 41 75 74 6f 56 61 63 75 75 6d  ll SetAutoVacuum
2811c 28 29 20 74 6f 20 73 65 74 20 69 6e 69 74 69 61  () to set initia
2811d 6c 69 7a 65 20 74 68 65 20 69 6e 74 65 72 6e 61  lize the interna
2811e 6c 20 61 75 74 6f 20 61 6e 64 0a 20 20 20 20 20  l auto and.     
2811f 20 20 20 2a 2a 20 69 6e 63 72 2d 76 61 63 75 75     ** incr-vacuu
28120 6d 20 66 6c 61 67 73 2e 20 54 68 69 73 20 69 73  m flags. This is
28121 20 72 65 71 75 69 72 65 64 20 69 6e 20 63 61 73   required in cas
28122 65 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f  e this connectio
28123 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 72 65  n.        ** cre
28124 61 74 65 73 20 74 68 65 20 64 61 74 61 62 61 73  ates the databas
28125 65 20 66 69 6c 65 2e 20 49 74 20 69 73 20 69 6d  e file. It is im
28126 70 6f 72 74 61 6e 74 20 74 68 61 74 20 69 74 20  portant that it 
28127 69 73 20 63 72 65 61 74 65 64 0a 20 20 20 20 20  is created.     
28128 20 20 20 2a 2a 20 61 73 20 61 6e 20 61 75 74 6f     ** as an auto
28129 2d 76 61 63 75 75 6d 20 63 61 70 61 62 6c 65 20  -vacuum capable 
2812a 64 62 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  db..        */. 
2812b 20 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20         int rc = 
2812c 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41  sqlite3BtreeSetA
2812d 75 74 6f 56 61 63 75 75 6d 28 70 42 74 2c 20 65  utoVacuum(pBt, e
2812e 41 75 74 6f 29 3b 0a 20 20 20 20 20 20 20 20 69  Auto);.        i
2812f 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
28130 20 26 26 20 28 65 41 75 74 6f 3d 3d 31 20 7c 7c   && (eAuto==1 ||
28131 20 65 41 75 74 6f 3d 3d 32 29 20 29 7b 0a 20 20   eAuto==2) ){.  
28132 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 6e 20          /* When 
28133 73 65 74 74 69 6e 67 20 74 68 65 20 61 75 74 6f  setting the auto
28134 5f 76 61 63 75 75 6d 20 6d 6f 64 65 20 74 6f 20  _vacuum mode to 
28135 65 69 74 68 65 72 20 22 66 75 6c 6c 22 20 6f 72  either "full" or
28136 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 22   .          ** "
28137 69 6e 63 72 65 6d 65 6e 74 61 6c 22 2c 20 77 72  incremental", wr
28138 69 74 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66  ite the value of
28139 20 6d 65 74 61 5b 36 5d 20 69 6e 20 74 68 65 20   meta[6] in the 
2813a 64 61 74 61 62 61 73 65 0a 20 20 20 20 20 20 20  database.       
2813b 20 20 20 2a 2a 20 66 69 6c 65 2e 20 42 65 66 6f     ** file. Befo
2813c 72 65 20 77 72 69 74 69 6e 67 20 74 6f 20 6d 65  re writing to me
2813d 74 61 5b 36 5d 2c 20 63 68 65 63 6b 20 74 68 61  ta[6], check tha
2813e 74 20 6d 65 74 61 5b 33 5d 20 69 6e 64 69 63 61  t meta[3] indica
2813f 74 65 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  tes.          **
28140 20 74 68 61 74 20 74 68 69 73 20 72 65 61 6c 6c   that this reall
28141 79 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 63  y is an auto-vac
28142 75 75 6d 20 63 61 70 61 62 6c 65 20 64 61 74 61  uum capable data
28143 62 61 73 65 2e 0a 20 20 20 20 20 20 20 20 20 20  base..          
28144 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 74 61  */.          sta
28145 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70  tic const VdbeOp
28146 4c 69 73 74 20 73 65 74 4d 65 74 61 36 5b 5d 20  List setMeta6[] 
28147 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  = {.            
28148 7b 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e  { OP_Transaction
28149 2c 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  ,    0,         
2814a 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1,              
2814b 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a     0},    /* 0 *
2814c 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  /.            { 
2814d 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 20  OP_ReadCookie,  
2814e 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 31 2c     0,         1,
2814f 20 20 20 20 20 20 20 20 20 42 54 52 45 45 5f 4c           BTREE_L
28150 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45  ARGEST_ROOT_PAGE
28151 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  },.            {
28152 20 4f 50 5f 49 66 2c 20 20 20 20 20 20 20 20 20   OP_If,         
28153 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 30      1,         0
28154 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
28155 20 20 30 7d 2c 20 20 20 20 2f 2a 20 32 20 2a 2f    0},    /* 2 */
28156 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
28157 50 5f 48 61 6c 74 2c 20 20 20 20 20 20 20 20 20  P_Halt,         
28158 20 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 4f 45 5f    SQLITE_OK, OE_
28159 41 62 6f 72 74 2c 20 20 20 20 20 20 20 20 20 20  Abort,          
2815a 30 7d 2c 20 20 20 20 2f 2a 20 33 20 2a 2f 0a 20  0},    /* 3 */. 
2815b 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
2815c 49 6e 74 65 67 65 72 2c 20 20 20 20 20 20 20 20  Integer,        
2815d 30 2c 20 20 20 20 20 20 20 20 20 31 2c 20 20 20  0,         1,   
2815e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 7d                0}
2815f 2c 20 20 20 20 2f 2a 20 34 20 2a 2f 0a 20 20 20  ,    /* 4 */.   
28160 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 65           { OP_Se
28161 74 43 6f 6f 6b 69 65 2c 20 20 20 20 20 20 30 2c  tCookie,      0,
28162 20 20 20 20 20 20 20 20 20 42 54 52 45 45 5f 49           BTREE_I
28163 4e 43 52 5f 56 41 43 55 55 4d 2c 20 31 7d 2c 20  NCR_VACUUM, 1}, 
28164 20 20 20 2f 2a 20 35 20 2a 2f 0a 20 20 20 20 20     /* 5 */.     
28165 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 20 20       };.        
28166 20 20 69 6e 74 20 69 41 64 64 72 3b 0a 20 20 20    int iAddr;.   
28167 20 20 20 20 20 20 20 69 41 64 64 72 20 3d 20 73         iAddr = s
28168 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c  qlite3VdbeAddOpL
28169 69 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65  ist(v, ArraySize
2816a 28 73 65 74 4d 65 74 61 36 29 2c 20 73 65 74 4d  (setMeta6), setM
2816b 65 74 61 36 29 3b 0a 20 20 20 20 20 20 20 20 20  eta6);.         
2816c 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
2816d 67 65 50 31 28 76 2c 20 69 41 64 64 72 2c 20 69  geP1(v, iAddr, i
2816e 44 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  Db);.          s
2816f 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
28170 50 31 28 76 2c 20 69 41 64 64 72 2b 31 2c 20 69  P1(v, iAddr+1, i
28171 44 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  Db);.          s
28172 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
28173 50 32 28 76 2c 20 69 41 64 64 72 2b 32 2c 20 69  P2(v, iAddr+2, i
28174 41 64 64 72 2b 34 29 3b 0a 20 20 20 20 20 20 20  Addr+4);.       
28175 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
28176 61 6e 67 65 50 31 28 76 2c 20 69 41 64 64 72 2b  angeP1(v, iAddr+
28177 34 2c 20 65 41 75 74 6f 2d 31 29 3b 0a 20 20 20  4, eAuto-1);.   
28178 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
28179 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 69 41  beChangeP1(v, iA
2817a 64 64 72 2b 35 2c 20 69 44 62 29 3b 0a 20 20 20  ddr+5, iDb);.   
2817b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2817c 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69  beUsesBtree(v, i
2817d 44 62 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  Db);.        }. 
2817e 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
2817f 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  else.#endif..  /
28180 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  *.  **  PRAGMA [
28181 64 61 74 61 62 61 73 65 2e 5d 69 6e 63 72 65 6d  database.]increm
28182 65 6e 74 61 6c 5f 76 61 63 75 75 6d 28 4e 29 0a  ental_vacuum(N).
28183 20 20 2a 2a 0a 20 20 2a 2a 20 44 6f 20 4e 20 73    **.  ** Do N s
28184 74 65 70 73 20 6f 66 20 69 6e 63 72 65 6d 65 6e  teps of incremen
28185 74 61 6c 20 76 61 63 75 75 6d 69 6e 67 20 6f 6e  tal vacuuming on
28186 20 61 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a   a database..  *
28187 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
28188 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
28189 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
2818a 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 69 6e 63  rICmp(zLeft,"inc
2818b 72 65 6d 65 6e 74 61 6c 5f 76 61 63 75 75 6d 22  remental_vacuum"
2818c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  )==0 ){.    int 
2818d 69 4c 69 6d 69 74 2c 20 61 64 64 72 3b 0a 20 20  iLimit, addr;.  
2818e 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61    if( sqlite3Rea
2818f 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
28190 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 72  ){.      goto pr
28191 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  agma_out;.    }.
28192 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 3d 3d      if( zRight==
28193 30 20 7c 7c 20 21 73 71 6c 69 74 65 33 47 65 74  0 || !sqlite3Get
28194 49 6e 74 33 32 28 7a 52 69 67 68 74 2c 20 26 69  Int32(zRight, &i
28195 4c 69 6d 69 74 29 20 7c 7c 20 69 4c 69 6d 69 74  Limit) || iLimit
28196 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 4c 69  <=0 ){.      iLi
28197 6d 69 74 20 3d 20 30 78 37 66 66 66 66 66 66 66  mit = 0x7fffffff
28198 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
28199 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
2819a 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30  ration(pParse, 0
2819b 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69  , iDb);.    sqli
2819c 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2819d 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 4c 69   OP_Integer, iLi
2819e 6d 69 74 2c 20 31 29 3b 0a 20 20 20 20 61 64 64  mit, 1);.    add
2819f 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
281a0 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 6e 63 72  ddOp1(v, OP_Incr
281a1 56 61 63 75 75 6d 2c 20 69 44 62 29 3b 0a 20 20  Vacuum, iDb);.  
281a2 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
281a3 4f 70 31 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op1(v, OP_Result
281a4 52 6f 77 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c  Row, 1);.    sql
281a5 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
281a6 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 31 2c 20  , OP_AddImm, 1, 
281a7 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  -1);.    sqlite3
281a8 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
281a9 5f 49 66 50 6f 73 2c 20 31 2c 20 61 64 64 72 29  _IfPos, 1, addr)
281aa 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
281ab 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
281ac 72 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64  r);.  }else.#end
281ad 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
281ae 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52  TE_OMIT_PAGER_PR
281af 41 47 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20  AGMAS.  /*.  ** 
281b0 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
281b1 65 2e 5d 63 61 63 68 65 5f 73 69 7a 65 0a 20 20  e.]cache_size.  
281b2 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  **  PRAGMA [data
281b3 62 61 73 65 2e 5d 63 61 63 68 65 5f 73 69 7a 65  base.]cache_size
281b4 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  =N.  **.  ** The
281b5 20 66 69 72 73 74 20 66 6f 72 6d 20 72 65 70 6f   first form repo
281b6 72 74 73 20 74 68 65 20 63 75 72 72 65 6e 74 20  rts the current 
281b7 6c 6f 63 61 6c 20 73 65 74 74 69 6e 67 20 66 6f  local setting fo
281b8 72 20 74 68 65 0a 20 20 2a 2a 20 70 61 67 65 20  r the.  ** page 
281b9 63 61 63 68 65 20 73 69 7a 65 2e 20 20 54 68 65  cache size.  The
281ba 20 6c 6f 63 61 6c 20 73 65 74 74 69 6e 67 20 63   local setting c
281bb 61 6e 20 62 65 20 64 69 66 66 65 72 65 6e 74 20  an be different 
281bc 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 70 65  from.  ** the pe
281bd 72 73 69 73 74 65 6e 74 20 63 61 63 68 65 20 73  rsistent cache s
281be 69 7a 65 20 76 61 6c 75 65 20 74 68 61 74 20 69  ize value that i
281bf 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  s stored in the 
281c0 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 66 69  database.  ** fi
281c1 6c 65 20 69 74 73 65 6c 66 2e 20 20 54 68 65 20  le itself.  The 
281c2 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69  value returned i
281c3 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  s the maximum nu
281c4 6d 62 65 72 20 6f 66 0a 20 20 2a 2a 20 70 61 67  mber of.  ** pag
281c5 65 73 20 69 6e 20 74 68 65 20 70 61 67 65 20 63  es in the page c
281c6 61 63 68 65 2e 20 20 54 68 65 20 73 65 63 6f 6e  ache.  The secon
281c7 64 20 66 6f 72 6d 20 73 65 74 73 20 74 68 65 20  d form sets the 
281c8 6c 6f 63 61 6c 0a 20 20 2a 2a 20 70 61 67 65 20  local.  ** page 
281c9 63 61 63 68 65 20 73 69 7a 65 20 76 61 6c 75 65  cache size value
281ca 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 63  .  It does not c
281cb 68 61 6e 67 65 20 74 68 65 20 70 65 72 73 69 73  hange the persis
281cc 74 65 6e 74 0a 20 20 2a 2a 20 63 61 63 68 65 20  tent.  ** cache 
281cd 73 69 7a 65 20 73 74 6f 72 65 64 20 6f 6e 20 74  size stored on t
281ce 68 65 20 64 69 73 6b 20 73 6f 20 74 68 65 20 63  he disk so the c
281cf 61 63 68 65 20 73 69 7a 65 20 77 69 6c 6c 20 72  ache size will r
281d0 65 76 65 72 74 0a 20 20 2a 2a 20 74 6f 20 69 74  evert.  ** to it
281d1 73 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20  s default value 
281d2 77 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73  when the databas
281d3 65 20 69 73 20 63 6c 6f 73 65 64 20 61 6e 64 20  e is closed and 
281d4 72 65 6f 70 65 6e 65 64 2e 0a 20 20 2a 2a 20 4e  reopened..  ** N
281d5 20 73 68 6f 75 6c 64 20 62 65 20 61 20 70 6f 73   should be a pos
281d6 69 74 69 76 65 20 69 6e 74 65 67 65 72 2e 0a 20  itive integer.. 
281d7 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
281d8 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22  3StrICmp(zLeft,"
281d9 63 61 63 68 65 5f 73 69 7a 65 22 29 3d 3d 30 20  cache_size")==0 
281da 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
281db 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
281dc 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67  rse) ) goto prag
281dd 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 69 66 28 20  ma_out;.    if( 
281de 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  !zRight ){.     
281df 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74   returnSingleInt
281e0 28 70 50 61 72 73 65 2c 20 22 63 61 63 68 65 5f  (pParse, "cache_
281e1 73 69 7a 65 22 2c 20 70 44 62 2d 3e 70 53 63 68  size", pDb->pSch
281e2 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 29  ema->cache_size)
281e3 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
281e4 20 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 61 74     int size = at
281e5 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20  oi(zRight);.    
281e6 20 20 69 66 28 20 73 69 7a 65 3c 30 20 29 20 73    if( size<0 ) s
281e7 69 7a 65 20 3d 20 2d 73 69 7a 65 3b 0a 20 20 20  ize = -size;.   
281e8 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d     pDb->pSchema-
281e9 3e 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 73 69  >cache_size = si
281ea 7a 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ze;.      sqlite
281eb 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69  3BtreeSetCacheSi
281ec 7a 65 28 70 44 62 2d 3e 70 42 74 2c 20 70 44 62  ze(pDb->pBt, pDb
281ed 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65  ->pSchema->cache
281ee 5f 73 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20  _size);.    }.  
281ef 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  }else..  /*.  **
281f0 20 20 20 50 52 41 47 4d 41 20 74 65 6d 70 5f 73     PRAGMA temp_s
281f1 74 6f 72 65 0a 20 20 2a 2a 20 20 20 50 52 41 47  tore.  **   PRAG
281f2 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 20 3d 20  MA temp_store = 
281f3 22 64 65 66 61 75 6c 74 22 7c 22 6d 65 6d 6f 72  "default"|"memor
281f4 79 22 7c 22 66 69 6c 65 22 0a 20 20 2a 2a 0a 20  y"|"file".  **. 
281f5 20 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20 73 65   ** Return or se
281f6 74 20 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75  t the local valu
281f7 65 20 6f 66 20 74 68 65 20 74 65 6d 70 5f 73 74  e of the temp_st
281f8 6f 72 65 20 66 6c 61 67 2e 20 20 43 68 61 6e 67  ore flag.  Chang
281f9 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6c 6f 63  ing.  ** the loc
281fa 61 6c 20 76 61 6c 75 65 20 64 6f 65 73 20 6e 6f  al value does no
281fb 74 20 6d 61 6b 65 20 63 68 61 6e 67 65 73 20 74  t make changes t
281fc 6f 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65 20  o the disk file 
281fd 61 6e 64 20 74 68 65 20 64 65 66 61 75 6c 74 0a  and the default.
281fe 20 20 2a 2a 20 76 61 6c 75 65 20 77 69 6c 6c 20    ** value will 
281ff 62 65 20 72 65 73 74 6f 72 65 64 20 74 68 65 20  be restored the 
28200 6e 65 78 74 20 74 69 6d 65 20 74 68 65 20 64 61  next time the da
28201 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64  tabase is opened
28202 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65  ..  **.  ** Note
28203 20 74 68 61 74 20 69 74 20 69 73 20 70 6f 73 73   that it is poss
28204 69 62 6c 65 20 66 6f 72 20 74 68 65 20 6c 69 62  ible for the lib
28205 72 61 72 79 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  rary compile-tim
28206 65 20 6f 70 74 69 6f 6e 73 20 74 6f 0a 20 20 2a  e options to.  *
28207 2a 20 6f 76 65 72 72 69 64 65 20 74 68 69 73 20  * override this 
28208 73 65 74 74 69 6e 67 0a 20 20 2a 2f 0a 20 20 69  setting.  */.  i
28209 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
2820a 70 28 7a 4c 65 66 74 2c 20 22 74 65 6d 70 5f 73  p(zLeft, "temp_s
2820b 74 6f 72 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20  tore")==0 ){.   
2820c 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a   if( !zRight ){.
2820d 20 20 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67        returnSing
2820e 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22 74  leInt(pParse, "t
2820f 65 6d 70 5f 73 74 6f 72 65 22 2c 20 64 62 2d 3e  emp_store", db->
28210 74 65 6d 70 5f 73 74 6f 72 65 29 3b 0a 20 20 20  temp_store);.   
28211 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 68   }else{.      ch
28212 61 6e 67 65 54 65 6d 70 53 74 6f 72 61 67 65 28  angeTempStorage(
28213 70 50 61 72 73 65 2c 20 7a 52 69 67 68 74 29 3b  pParse, zRight);
28214 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a  .    }.  }else..
28215 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47    /*.  **   PRAG
28216 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69  MA temp_store_di
28217 72 65 63 74 6f 72 79 0a 20 20 2a 2a 20 20 20 50  rectory.  **   P
28218 52 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65  RAGMA temp_store
28219 5f 64 69 72 65 63 74 6f 72 79 20 3d 20 22 22 7c  _directory = ""|
2821a 22 64 69 72 65 63 74 6f 72 79 5f 6e 61 6d 65 22  "directory_name"
2821b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72  .  **.  ** Retur
2821c 6e 20 6f 72 20 73 65 74 20 74 68 65 20 6c 6f 63  n or set the loc
2821d 61 6c 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  al value of the 
2821e 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63  temp_store_direc
2821f 74 6f 72 79 20 66 6c 61 67 2e 20 20 43 68 61 6e  tory flag.  Chan
28220 67 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 76 61  ging.  ** the va
28221 6c 75 65 20 73 65 74 73 20 61 20 73 70 65 63 69  lue sets a speci
28222 66 69 63 20 64 69 72 65 63 74 6f 72 79 20 74 6f  fic directory to
28223 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 65 6d   be used for tem
28224 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 0a 20 20  porary files..  
28225 2a 2a 20 53 65 74 74 69 6e 67 20 74 6f 20 61 20  ** Setting to a 
28226 6e 75 6c 6c 20 73 74 72 69 6e 67 20 72 65 76 65  null string reve
28227 72 74 73 20 74 6f 20 74 68 65 20 64 65 66 61 75  rts to the defau
28228 6c 74 20 74 65 6d 70 6f 72 61 72 79 20 64 69 72  lt temporary dir
28229 65 63 74 6f 72 79 20 73 65 61 72 63 68 2e 0a 20  ectory search.. 
2822a 20 2a 2a 20 49 66 20 74 65 6d 70 6f 72 61 72 79   ** If temporary
2822b 20 64 69 72 65 63 74 6f 72 79 20 69 73 20 63 68   directory is ch
2822c 61 6e 67 65 64 2c 20 74 68 65 6e 20 69 6e 76 61  anged, then inva
2822d 6c 69 64 61 74 65 54 65 6d 70 53 74 6f 72 61 67  lidateTempStorag
2822e 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2f 0a 20 20 69  e..  **.  */.  i
2822f 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
28230 70 28 7a 4c 65 66 74 2c 20 22 74 65 6d 70 5f 73  p(zLeft, "temp_s
28231 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 22 29  tore_directory")
28232 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21  ==0 ){.    if( !
28233 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  zRight ){.      
28234 69 66 28 20 73 71 6c 69 74 65 33 5f 74 65 6d 70  if( sqlite3_temp
28235 5f 64 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20  _directory ){.  
28236 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
28237 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31  eSetNumCols(v, 1
28238 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
28239 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
2823a 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
2823b 41 4d 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20  AME, .          
2823c 20 20 22 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69    "temp_store_di
2823d 72 65 63 74 6f 72 79 22 2c 20 53 51 4c 49 54 45  rectory", SQLITE
2823e 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
2823f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
28240 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
28241 38 2c 20 30 2c 20 31 2c 20 30 2c 20 73 71 6c 69  8, 0, 1, 0, sqli
28242 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f  te3_temp_directo
28243 72 79 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  ry, 0);.        
28244 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28245 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f  2(v, OP_ResultRo
28246 77 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 20 20  w, 1, 1);.      
28247 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 23 69 66  }.    }else{.#if
28248 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
28249 5f 57 53 44 0a 20 20 20 20 20 20 69 66 28 20 7a  _WSD.      if( z
2824a 52 69 67 68 74 5b 30 5d 20 29 7b 0a 20 20 20 20  Right[0] ){.    
2824b 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20      int rc;.    
2824c 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20      int res;.   
2824d 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
2824e 33 4f 73 41 63 63 65 73 73 28 64 62 2d 3e 70 56  3OsAccess(db->pV
2824f 66 73 2c 20 7a 52 69 67 68 74 2c 20 53 51 4c 49  fs, zRight, SQLI
28250 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52  TE_ACCESS_READWR
28251 49 54 45 2c 20 26 72 65 73 29 3b 0a 20 20 20 20  ITE, &res);.    
28252 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
28253 54 45 5f 4f 4b 20 7c 7c 20 72 65 73 3d 3d 30 20  TE_OK || res==0 
28254 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
28255 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
28256 72 73 65 2c 20 22 6e 6f 74 20 61 20 77 72 69 74  rse, "not a writ
28257 61 62 6c 65 20 64 69 72 65 63 74 6f 72 79 22 29  able directory")
28258 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
28259 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20   pragma_out;.   
2825a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
2825b 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f       if( SQLITE_
2825c 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 30 0a 20 20  TEMP_STORE==0.  
2825d 20 20 20 20 20 7c 7c 20 28 53 51 4c 49 54 45 5f       || (SQLITE_
2825e 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 31 20 26 26  TEMP_STORE==1 &&
2825f 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 3c   db->temp_store<
28260 3d 31 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 53  =1).       || (S
28261 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45  QLITE_TEMP_STORE
28262 3d 3d 32 20 26 26 20 64 62 2d 3e 74 65 6d 70 5f  ==2 && db->temp_
28263 73 74 6f 72 65 3d 3d 31 29 0a 20 20 20 20 20 20  store==1).      
28264 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 76 61 6c  ){.        inval
28265 69 64 61 74 65 54 65 6d 70 53 74 6f 72 61 67 65  idateTempStorage
28266 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
28267 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  }.      sqlite3_
28268 66 72 65 65 28 73 71 6c 69 74 65 33 5f 74 65 6d  free(sqlite3_tem
28269 70 5f 64 69 72 65 63 74 6f 72 79 29 3b 0a 20 20  p_directory);.  
2826a 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 5b 30      if( zRight[0
2826b 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  ] ){.        sql
2826c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74  ite3_temp_direct
2826d 6f 72 79 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ory = sqlite3DbS
2826e 74 72 44 75 70 28 30 2c 20 7a 52 69 67 68 74 29  trDup(0, zRight)
2826f 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
28270 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 74         sqlite3_t
28271 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 3d 20  emp_directory = 
28272 30 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  0;.      }.#endi
28273 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
28274 5f 57 53 44 20 2a 2f 0a 20 20 20 20 7d 0a 20 20  _WSD */.    }.  
28275 7d 65 6c 73 65 0a 0a 23 69 66 20 21 64 65 66 69  }else..#if !defi
28276 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
28277 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 29  E_LOCKING_STYLE)
28278 0a 23 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f  .#  if defined(_
28279 5f 41 50 50 4c 45 5f 5f 29 0a 23 20 20 20 20 64  _APPLE__).#    d
2827a 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41  efine SQLITE_ENA
2827b 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
2827c 45 20 31 0a 23 20 20 65 6c 73 65 0a 23 20 20 20  E 1.#  else.#   
2827d 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45   define SQLITE_E
2827e 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
2827f 59 4c 45 20 30 0a 23 20 20 65 6e 64 69 66 0a 23  YLE 0.#  endif.#
28280 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45  endif.#if SQLITE
28281 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
28282 53 54 59 4c 45 0a 20 20 2f 2a 0a 20 20 20 2a 2a  STYLE.  /*.   **
28283 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62     PRAGMA [datab
28284 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f  ase.]lock_proxy_
28285 66 69 6c 65 0a 20 20 20 2a 2a 20 20 20 50 52 41  file.   **   PRA
28286 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c  GMA [database.]l
28287 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 20 3d  ock_proxy_file =
28288 20 22 3a 61 75 74 6f 3a 22 7c 22 6c 6f 63 6b 5f   ":auto:"|"lock_
28289 66 69 6c 65 5f 70 61 74 68 22 0a 20 20 20 2a 2a  file_path".   **
2828a 0a 20 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f 72  .   ** Return or
2828b 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f   set the value o
2828c 66 20 74 68 65 20 6c 6f 63 6b 5f 70 72 6f 78 79  f the lock_proxy
2828d 5f 66 69 6c 65 20 66 6c 61 67 2e 20 20 43 68 61  _file flag.  Cha
2828e 6e 67 69 6e 67 0a 20 20 20 2a 2a 20 74 68 65 20  nging.   ** the 
2828f 76 61 6c 75 65 20 73 65 74 73 20 61 20 73 70 65  value sets a spe
28290 63 69 66 69 63 20 66 69 6c 65 20 74 6f 20 62 65  cific file to be
28291 20 75 73 65 64 20 66 6f 72 20 64 61 74 61 62 61   used for databa
28292 73 65 20 61 63 63 65 73 73 20 6c 6f 63 6b 73 2e  se access locks.
28293 0a 20 20 20 2a 2a 0a 20 20 20 2a 2f 0a 20 20 69  .   **.   */.  i
28294 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
28295 70 28 7a 4c 65 66 74 2c 20 22 6c 6f 63 6b 5f 70  p(zLeft, "lock_p
28296 72 6f 78 79 5f 66 69 6c 65 22 29 3d 3d 30 20 29  roxy_file")==0 )
28297 7b 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67 68  {.    if( !zRigh
28298 74 20 29 7b 0a 20 20 20 20 20 20 50 61 67 65 72  t ){.      Pager
28299 20 2a 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74   *pPager = sqlit
2829a 65 33 42 74 72 65 65 50 61 67 65 72 28 70 44 62  e3BtreePager(pDb
2829b 2d 3e 70 42 74 29 3b 0a 20 20 20 20 20 20 63 68  ->pBt);.      ch
2829c 61 72 20 2a 70 72 6f 78 79 5f 66 69 6c 65 5f 70  ar *proxy_file_p
2829d 61 74 68 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  ath = NULL;.    
2829e 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
2829f 70 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 50  pFile = sqlite3P
282a0 61 67 65 72 46 69 6c 65 28 70 50 61 67 65 72 29  agerFile(pPager)
282a1 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  ;.      sqlite3O
282a2 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 70 46 69  sFileControl(pFi
282a3 6c 65 2c 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c  le, SQLITE_GET_L
282a4 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c 20 0a 20  OCKPROXYFILE, . 
282a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
282a6 20 20 20 20 20 20 20 20 20 20 26 70 72 6f 78 79            &proxy
282a7 5f 66 69 6c 65 5f 70 61 74 68 29 3b 0a 20 20 20  _file_path);.   
282a8 20 20 20 0a 20 20 20 20 20 20 69 66 28 20 70 72     .      if( pr
282a9 6f 78 79 5f 66 69 6c 65 5f 70 61 74 68 20 29 7b  oxy_file_path ){
282aa 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
282ab 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
282ac 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 1);.        sq
282ad 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
282ae 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
282af 45 5f 4e 41 4d 45 2c 20 0a 20 20 20 20 20 20 20  E_NAME, .       
282b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
282b1 20 20 20 20 20 20 20 22 6c 6f 63 6b 5f 70 72 6f         "lock_pro
282b2 78 79 5f 66 69 6c 65 22 2c 20 53 51 4c 49 54 45  xy_file", SQLITE
282b3 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
282b4 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
282b5 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
282b6 38 2c 20 30 2c 20 31 2c 20 30 2c 20 70 72 6f 78  8, 0, 1, 0, prox
282b7 79 5f 66 69 6c 65 5f 70 61 74 68 2c 20 30 29 3b  y_file_path, 0);
282b8 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
282b9 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
282ba 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31  _ResultRow, 1, 1
282bb 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
282bc 65 6c 73 65 7b 0a 20 20 20 20 20 20 50 61 67 65  else{.      Page
282bd 72 20 2a 70 50 61 67 65 72 20 3d 20 73 71 6c 69  r *pPager = sqli
282be 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 44  te3BtreePager(pD
282bf 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 20 20 73  b->pBt);.      s
282c0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69  qlite3_file *pFi
282c1 6c 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  le = sqlite3Page
282c2 72 46 69 6c 65 28 70 50 61 67 65 72 29 3b 0a 20  rFile(pPager);. 
282c3 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20       int res;.  
282c4 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 5b 30      if( zRight[0
282c5 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 73  ] ){.        res
282c6 3d 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f  =sqlite3OsFileCo
282c7 6e 74 72 6f 6c 28 70 46 69 6c 65 2c 20 53 51 4c  ntrol(pFile, SQL
282c8 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58  ITE_SET_LOCKPROX
282c9 59 46 49 4c 45 2c 20 0a 20 20 20 20 20 20 20 20  YFILE, .        
282ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
282cb 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 52 69               zRi
282cc 67 68 74 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c  ght);.      } el
282cd 73 65 20 7b 0a 20 20 20 20 20 20 20 20 72 65 73  se {.        res
282ce 3d 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f  =sqlite3OsFileCo
282cf 6e 74 72 6f 6c 28 70 46 69 6c 65 2c 20 53 51 4c  ntrol(pFile, SQL
282d0 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58  ITE_SET_LOCKPROX
282d1 59 46 49 4c 45 2c 20 0a 20 20 20 20 20 20 20 20  YFILE, .        
282d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
282d3 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c               NUL
282d4 4c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  L);.      }.    
282d5 20 20 69 66 28 20 72 65 73 21 3d 53 51 4c 49 54    if( res!=SQLIT
282d6 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
282d7 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
282d8 70 50 61 72 73 65 2c 20 22 66 61 69 6c 65 64 20  pParse, "failed 
282d9 74 6f 20 73 65 74 20 6c 6f 63 6b 20 70 72 6f 78  to set lock prox
282da 79 20 66 69 6c 65 22 29 3b 0a 20 20 20 20 20 20  y file");.      
282db 20 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75    goto pragma_ou
282dc 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  t;.      }.    }
282dd 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20  .  }else.#endif 
282de 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  /* SQLITE_ENABLE
282df 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a  _LOCKING_STYLE *
282e0 2f 20 20 20 20 20 20 0a 20 20 20 20 0a 20 20 2f  /      .    .  /
282e1 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20  *.  **   PRAGMA 
282e2 5b 64 61 74 61 62 61 73 65 2e 5d 73 79 6e 63 68  [database.]synch
282e3 72 6f 6e 6f 75 73 0a 20 20 2a 2a 20 20 20 50 52  ronous.  **   PR
282e4 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
282e5 73 79 6e 63 68 72 6f 6e 6f 75 73 3d 4f 46 46 7c  synchronous=OFF|
282e6 4f 4e 7c 4e 4f 52 4d 41 4c 7c 46 55 4c 4c 0a 20  ON|NORMAL|FULL. 
282e7 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20   **.  ** Return 
282e8 6f 72 20 73 65 74 20 74 68 65 20 6c 6f 63 61 6c  or set the local
282e9 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 73 79   value of the sy
282ea 6e 63 68 72 6f 6e 6f 75 73 20 66 6c 61 67 2e 20  nchronous flag. 
282eb 20 43 68 61 6e 67 69 6e 67 0a 20 20 2a 2a 20 74   Changing.  ** t
282ec 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65 20 64  he local value d
282ed 6f 65 73 20 6e 6f 74 20 6d 61 6b 65 20 63 68 61  oes not make cha
282ee 6e 67 65 73 20 74 6f 20 74 68 65 20 64 69 73 6b  nges to the disk
282ef 20 66 69 6c 65 20 61 6e 64 20 74 68 65 0a 20 20   file and the.  
282f0 2a 2a 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  ** default value
282f1 20 77 69 6c 6c 20 62 65 20 72 65 73 74 6f 72 65   will be restore
282f2 64 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20  d the next time 
282f3 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 0a  the database is.
282f4 20 20 2a 2a 20 6f 70 65 6e 65 64 2e 0a 20 20 2a    ** opened..  *
282f5 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
282f6 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 73 79  trICmp(zLeft,"sy
282f7 6e 63 68 72 6f 6e 6f 75 73 22 29 3d 3d 30 20 29  nchronous")==0 )
282f8 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
282f9 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
282fa 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d  se) ) goto pragm
282fb 61 5f 6f 75 74 3b 0a 20 20 20 20 69 66 28 20 21  a_out;.    if( !
282fc 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  zRight ){.      
282fd 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28  returnSingleInt(
282fe 70 50 61 72 73 65 2c 20 22 73 79 6e 63 68 72 6f  pParse, "synchro
282ff 6e 6f 75 73 22 2c 20 70 44 62 2d 3e 73 61 66 65  nous", pDb->safe
28300 74 79 5f 6c 65 76 65 6c 2d 31 29 3b 0a 20 20 20  ty_level-1);.   
28301 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66   }else{.      if
28302 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  ( !db->autoCommi
28303 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  t ){.        sql
28304 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
28305 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20  rse, .          
28306 20 20 22 53 61 66 65 74 79 20 6c 65 76 65 6c 20    "Safety level 
28307 6d 61 79 20 6e 6f 74 20 62 65 20 63 68 61 6e 67  may not be chang
28308 65 64 20 69 6e 73 69 64 65 20 61 20 74 72 61 6e  ed inside a tran
28309 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20 20 20  saction");.     
2830a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2830b 70 44 62 2d 3e 73 61 66 65 74 79 5f 6c 65 76 65  pDb->safety_leve
2830c 6c 20 3d 20 67 65 74 53 61 66 65 74 79 4c 65 76  l = getSafetyLev
2830d 65 6c 28 7a 52 69 67 68 74 29 2b 31 3b 0a 20 20  el(zRight)+1;.  
2830e 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
2830f 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  lse.#endif /* SQ
28310 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
28311 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e  PRAGMAS */..#ifn
28312 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
28313 46 4c 41 47 5f 50 52 41 47 4d 41 53 0a 20 20 69  FLAG_PRAGMAS.  i
28314 66 28 20 66 6c 61 67 50 72 61 67 6d 61 28 70 50  f( flagPragma(pP
28315 61 72 73 65 2c 20 7a 4c 65 66 74 2c 20 7a 52 69  arse, zLeft, zRi
28316 67 68 74 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54  ght) ){.    /* T
28317 68 65 20 66 6c 61 67 50 72 61 67 6d 61 28 29 20  he flagPragma() 
28318 73 75 62 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20  subroutine also 
28319 67 65 6e 65 72 61 74 65 73 20 61 6e 79 20 6e 65  generates any ne
2831a 63 65 73 73 61 72 79 20 63 6f 64 65 0a 20 20 20  cessary code.   
2831b 20 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 74   ** there is not
2831c 68 69 6e 67 20 6d 6f 72 65 20 74 6f 20 64 6f 20  hing more to do 
2831d 68 65 72 65 20 2a 2f 0a 20 20 7d 65 6c 73 65 0a  here */.  }else.
2831e 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2831f 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41 47 4d  _OMIT_FLAG_PRAGM
28320 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  AS */..#ifndef S
28321 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d  QLITE_OMIT_SCHEM
28322 41 5f 50 52 41 47 4d 41 53 0a 20 20 2f 2a 0a 20  A_PRAGMAS.  /*. 
28323 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 74 61 62   **   PRAGMA tab
28324 6c 65 5f 69 6e 66 6f 28 3c 74 61 62 6c 65 3e 29  le_info(<table>)
28325 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72  .  **.  ** Retur
28326 6e 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 66  n a single row f
28327 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6f  or each column o
28328 66 20 74 68 65 20 6e 61 6d 65 64 20 74 61 62 6c  f the named tabl
28329 65 2e 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 6f  e. The columns o
2832a 66 0a 20 20 2a 2a 20 74 68 65 20 72 65 74 75 72  f.  ** the retur
2832b 6e 65 64 20 64 61 74 61 20 73 65 74 20 61 72 65  ned data set are
2832c 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 63 69 64 3a  :.  **.  ** cid:
2832d 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 69          Column i
2832e 64 20 28 6e 75 6d 62 65 72 65 64 20 66 72 6f 6d  d (numbered from
2832f 20 6c 65 66 74 20 74 6f 20 72 69 67 68 74 2c 20   left to right, 
28330 73 74 61 72 74 69 6e 67 20 61 74 20 30 29 0a 20  starting at 0). 
28331 20 2a 2a 20 6e 61 6d 65 3a 20 20 20 20 20 20 20   ** name:       
28332 43 6f 6c 75 6d 6e 20 6e 61 6d 65 0a 20 20 2a 2a  Column name.  **
28333 20 74 79 70 65 3a 20 20 20 20 20 20 20 43 6f 6c   type:       Col
28334 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  umn declaration 
28335 74 79 70 65 2e 0a 20 20 2a 2a 20 6e 6f 74 6e 75  type..  ** notnu
28336 6c 6c 3a 20 20 20 20 54 72 75 65 20 69 66 20 27  ll:    True if '
28337 4e 4f 54 20 4e 55 4c 4c 27 20 69 73 20 70 61 72  NOT NULL' is par
28338 74 20 6f 66 20 63 6f 6c 75 6d 6e 20 64 65 63 6c  t of column decl
28339 61 72 61 74 69 6f 6e 0a 20 20 2a 2a 20 64 66 6c  aration.  ** dfl
2833a 74 5f 76 61 6c 75 65 3a 20 54 68 65 20 64 65 66  t_value: The def
2833b 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74  ault value for t
2833c 68 65 20 63 6f 6c 75 6d 6e 2c 20 69 66 20 61 6e  he column, if an
2833d 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  y..  */.  if( sq
2833e 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
2833f 66 74 2c 20 22 74 61 62 6c 65 5f 69 6e 66 6f 22  ft, "table_info"
28340 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29  )==0 && zRight )
28341 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61  {.    Table *pTa
28342 62 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  b;.    if( sqlit
28343 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
28344 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67  rse) ) goto prag
28345 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70 54 61 62  ma_out;.    pTab
28346 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61   = sqlite3FindTa
28347 62 6c 65 28 64 62 2c 20 7a 52 69 67 68 74 2c 20  ble(db, zRight, 
28348 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70 54  zDb);.    if( pT
28349 61 62 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  ab ){.      int 
2834a 69 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 48 69  i;.      int nHi
2834b 64 64 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20  dden = 0;.      
2834c 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20  Column *pCol;.  
2834d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
2834e 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 36 29 3b  etNumCols(v, 6);
2834f 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  .      pParse->n
28350 4d 65 6d 20 3d 20 36 3b 0a 20 20 20 20 20 20 73  Mem = 6;.      s
28351 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
28352 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41  Name(v, 0, COLNA
28353 4d 45 5f 4e 41 4d 45 2c 20 22 63 69 64 22 2c 20  ME_NAME, "cid", 
28354 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
28355 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
28356 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31  eSetColName(v, 1
28357 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
28358 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53  "name", SQLITE_S
28359 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71  TATIC);.      sq
2835a 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
2835b 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d  ame(v, 2, COLNAM
2835c 45 5f 4e 41 4d 45 2c 20 22 74 79 70 65 22 2c 20  E_NAME, "type", 
2835d 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
2835e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2835f 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 33  eSetColName(v, 3
28360 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
28361 22 6e 6f 74 6e 75 6c 6c 22 2c 20 53 51 4c 49 54  "notnull", SQLIT
28362 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
28363 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
28364 6f 6c 4e 61 6d 65 28 76 2c 20 34 2c 20 43 4f 4c  olName(v, 4, COL
28365 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 64 66 6c 74  NAME_NAME, "dflt
28366 5f 76 61 6c 75 65 22 2c 20 53 51 4c 49 54 45 5f  _value", SQLITE_
28367 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73  STATIC);.      s
28368 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
28369 4e 61 6d 65 28 76 2c 20 35 2c 20 43 4f 4c 4e 41  Name(v, 5, COLNA
2836a 4d 45 5f 4e 41 4d 45 2c 20 22 70 6b 22 2c 20 53  ME_NAME, "pk", S
2836b 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
2836c 20 20 20 20 20 73 71 6c 69 74 65 33 56 69 65 77       sqlite3View
2836d 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70  GetColumnNames(p
2836e 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20  Parse, pTab);.  
2836f 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 43 6f      for(i=0, pCo
28370 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c 3b 20 69 3c  l=pTab->aCol; i<
28371 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c  pTab->nCol; i++,
28372 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20   pCol++){.      
28373 20 20 69 66 28 20 49 73 48 69 64 64 65 6e 43 6f    if( IsHiddenCo
28374 6c 75 6d 6e 28 70 43 6f 6c 29 20 29 7b 0a 20 20  lumn(pCol) ){.  
28375 20 20 20 20 20 20 20 20 6e 48 69 64 64 65 6e 2b          nHidden+
28376 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e  +;.          con
28377 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 7d  tinue;.        }
28378 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
28379 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2837a 5f 49 6e 74 65 67 65 72 2c 20 69 2d 6e 48 69 64  _Integer, i-nHid
2837b 64 65 6e 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  den, 1);.       
2837c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2837d 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
2837e 2c 20 30 2c 20 32 2c 20 30 2c 20 70 43 6f 6c 2d  , 0, 2, 0, pCol-
2837f 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20  >zName, 0);.    
28380 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
28381 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
28382 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c 0a 20 20  ng8, 0, 3, 0,.  
28383 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a           pCol->z
28384 54 79 70 65 20 3f 20 70 43 6f 6c 2d 3e 7a 54 79  Type ? pCol->zTy
28385 70 65 20 3a 20 22 22 2c 20 30 29 3b 0a 20 20 20  pe : "", 0);.   
28386 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
28387 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
28388 65 67 65 72 2c 20 28 70 43 6f 6c 2d 3e 6e 6f 74  eger, (pCol->not
28389 4e 75 6c 6c 20 3f 20 31 20 3a 20 30 29 2c 20 34  Null ? 1 : 0), 4
2838a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
2838b 43 6f 6c 2d 3e 7a 44 66 6c 74 20 29 7b 0a 20 20  Col->zDflt ){.  
2838c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2838d 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2838e 53 74 72 69 6e 67 38 2c 20 30 2c 20 35 2c 20 30  String8, 0, 5, 0
2838f 2c 20 28 63 68 61 72 2a 29 70 43 6f 6c 2d 3e 7a  , (char*)pCol->z
28390 44 66 6c 74 2c 20 30 29 3b 0a 20 20 20 20 20 20  Dflt, 0);.      
28391 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
28392 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
28393 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
28394 20 30 2c 20 35 29 3b 0a 20 20 20 20 20 20 20 20   0, 5);.        
28395 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
28396 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
28397 50 5f 49 6e 74 65 67 65 72 2c 20 70 43 6f 6c 2d  P_Integer, pCol-
28398 3e 69 73 50 72 69 6d 4b 65 79 2c 20 36 29 3b 0a  >isPrimKey, 6);.
28399 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2839a 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2839b 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 36 29  ResultRow, 1, 6)
2839c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2839d 20 20 7d 65 6c 73 65 0a 0a 20 20 69 66 28 20 73    }else..  if( s
2839e 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
2839f 65 66 74 2c 20 22 69 6e 64 65 78 5f 69 6e 66 6f  eft, "index_info
283a0 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20  ")==0 && zRight 
283a1 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49  ){.    Index *pI
283a2 64 78 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  dx;.    Table *p
283a3 54 61 62 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  Tab;.    if( sql
283a4 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70  ite3ReadSchema(p
283a5 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72  Parse) ) goto pr
283a6 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70 49  agma_out;.    pI
283a7 64 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  dx = sqlite3Find
283a8 49 6e 64 65 78 28 64 62 2c 20 7a 52 69 67 68 74  Index(db, zRight
283a9 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20  , zDb);.    if( 
283aa 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20 69 6e  pIdx ){.      in
283ab 74 20 69 3b 0a 20 20 20 20 20 20 70 54 61 62 20  t i;.      pTab 
283ac 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 3b 0a  = pIdx->pTable;.
283ad 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
283ae 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 33  eSetNumCols(v, 3
283af 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  );.      pParse-
283b0 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20 20  >nMem = 3;.     
283b1 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
283b2 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
283b3 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 6e  NAME_NAME, "seqn
283b4 6f 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  o", SQLITE_STATI
283b5 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  C);.      sqlite
283b6 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
283b7 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 1, COLNAME_NA
283b8 4d 45 2c 20 22 63 69 64 22 2c 20 53 51 4c 49 54  ME, "cid", SQLIT
283b9 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
283ba 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
283bb 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c  olName(v, 2, COL
283bc 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65  NAME_NAME, "name
283bd 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
283be 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  );.      for(i=0
283bf 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  ; i<pIdx->nColum
283c0 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  n; i++){.       
283c1 20 69 6e 74 20 63 6e 75 6d 20 3d 20 70 49 64 78   int cnum = pIdx
283c2 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20  ->aiColumn[i];. 
283c3 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
283c4 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
283c5 6e 74 65 67 65 72 2c 20 69 2c 20 31 29 3b 0a 20  nteger, i, 1);. 
283c6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
283c7 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
283c8 6e 74 65 67 65 72 2c 20 63 6e 75 6d 2c 20 32 29  nteger, cnum, 2)
283c9 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
283ca 28 20 70 54 61 62 2d 3e 6e 43 6f 6c 3e 63 6e 75  ( pTab->nCol>cnu
283cb 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  m );.        sql
283cc 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
283cd 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
283ce 20 33 2c 20 30 2c 20 70 54 61 62 2d 3e 61 43 6f   3, 0, pTab->aCo
283cf 6c 5b 63 6e 75 6d 5d 2e 7a 4e 61 6d 65 2c 20 30  l[cnum].zName, 0
283d0 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
283d1 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
283d2 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c  OP_ResultRow, 1,
283d3 20 33 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   3);.      }.   
283d4 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69 66   }.  }else..  if
283d5 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
283d6 28 7a 4c 65 66 74 2c 20 22 69 6e 64 65 78 5f 6c  (zLeft, "index_l
283d7 69 73 74 22 29 3d 3d 30 20 26 26 20 7a 52 69 67  ist")==0 && zRig
283d8 68 74 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20  ht ){.    Index 
283d9 2a 70 49 64 78 3b 0a 20 20 20 20 54 61 62 6c 65  *pIdx;.    Table
283da 20 2a 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20   *pTab;.    if( 
283db 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
283dc 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f  a(pParse) ) goto
283dd 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20   pragma_out;.   
283de 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46   pTab = sqlite3F
283df 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 52 69  indTable(db, zRi
283e0 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69  ght, zDb);.    i
283e1 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 20  f( pTab ){.     
283e2 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
283e3 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20  dbe(pParse);.   
283e4 20 20 20 70 49 64 78 20 3d 20 70 54 61 62 2d 3e     pIdx = pTab->
283e5 70 49 6e 64 65 78 3b 0a 20 20 20 20 20 20 69 66  pIndex;.      if
283e6 28 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20  ( pIdx ){.      
283e7 20 20 69 6e 74 20 69 20 3d 20 30 3b 20 0a 20 20    int i = 0; .  
283e8 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
283e9 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 33  eSetNumCols(v, 3
283ea 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73  );.        pPars
283eb 65 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20  e->nMem = 3;.   
283ec 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
283ed 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
283ee 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
283ef 73 65 71 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  seq", SQLITE_STA
283f0 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71  TIC);.        sq
283f1 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
283f2 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d  ame(v, 1, COLNAM
283f3 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22 2c 20  E_NAME, "name", 
283f4 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
283f5 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
283f6 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
283f7 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   2, COLNAME_NAME
283f8 2c 20 22 75 6e 69 71 75 65 22 2c 20 53 51 4c 49  , "unique", SQLI
283f9 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
283fa 20 20 20 20 77 68 69 6c 65 28 70 49 64 78 29 7b      while(pIdx){
283fb 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
283fc 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
283fd 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 31  OP_Integer, i, 1
283fe 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
283ff 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
28400 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
28401 20 32 2c 20 30 2c 20 70 49 64 78 2d 3e 7a 4e 61   2, 0, pIdx->zNa
28402 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  me, 0);.        
28403 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
28404 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
28405 72 2c 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72  r, pIdx->onError
28406 21 3d 4f 45 5f 4e 6f 6e 65 2c 20 33 29 3b 0a 20  !=OE_None, 3);. 
28407 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28408 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
28409 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 33  _ResultRow, 1, 3
2840a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2b 2b 69  );.          ++i
2840b 3b 0a 20 20 20 20 20 20 20 20 20 20 70 49 64 78  ;.          pIdx
2840c 20 3d 20 70 49 64 78 2d 3e 70 4e 65 78 74 3b 0a   = pIdx->pNext;.
2840d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2840e 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  }.    }.  }else.
2840f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
28410 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 64 61  rICmp(zLeft, "da
28411 74 61 62 61 73 65 5f 6c 69 73 74 22 29 3d 3d 30  tabase_list")==0
28412 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
28413 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
28414 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
28415 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f   ) goto pragma_o
28416 75 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ut;.    sqlite3V
28417 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
28418 20 33 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d   3);.    pParse-
28419 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20 73  >nMem = 3;.    s
2841a 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
2841b 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41  Name(v, 0, COLNA
2841c 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22 2c 20  ME_NAME, "seq", 
2841d 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
2841e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
2841f 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20  etColName(v, 1, 
28420 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e  COLNAME_NAME, "n
28421 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ame", SQLITE_STA
28422 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  TIC);.    sqlite
28423 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
28424 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 2, COLNAME_NA
28425 4d 45 2c 20 22 66 69 6c 65 22 2c 20 53 51 4c 49  ME, "file", SQLI
28426 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
28427 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
28428 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  Db; i++){.      
28429 69 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70  if( db->aDb[i].p
2842a 42 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  Bt==0 ) continue
2842b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2842c 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65  db->aDb[i].zName
2842d 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  !=0 );.      sql
2842e 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2842f 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c  , OP_Integer, i,
28430 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   1);.      sqlit
28431 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
28432 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 32  OP_String8, 0, 2
28433 2c 20 30 2c 20 64 62 2d 3e 61 44 62 5b 69 5d 2e  , 0, db->aDb[i].
28434 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20  zName, 0);.     
28435 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
28436 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
28437 2c 20 30 2c 20 33 2c 20 30 2c 0a 20 20 20 20 20  , 0, 3, 0,.     
28438 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
28439 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 64 62  eeGetFilename(db
2843a 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 2c 20 30  ->aDb[i].pBt), 0
2843b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2843c 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2843d 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 33  _ResultRow, 1, 3
2843e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
2843f 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ..  if( sqlite3S
28440 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 63  trICmp(zLeft, "c
28441 6f 6c 6c 61 74 69 6f 6e 5f 6c 69 73 74 22 29 3d  ollation_list")=
28442 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 20  =0 ){.    int i 
28443 3d 20 30 3b 0a 20 20 20 20 48 61 73 68 45 6c 65  = 0;.    HashEle
28444 6d 20 2a 70 3b 0a 20 20 20 20 73 71 6c 69 74 65  m *p;.    sqlite
28445 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
28446 76 2c 20 32 29 3b 0a 20 20 20 20 70 50 61 72 73  v, 2);.    pPars
28447 65 2d 3e 6e 4d 65 6d 20 3d 20 32 3b 0a 20 20 20  e->nMem = 2;.   
28448 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
28449 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
2844a 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22  NAME_NAME, "seq"
2844b 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
2844c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2844d 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31  eSetColName(v, 1
2844e 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
2844f 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53  "name", SQLITE_S
28450 54 41 54 49 43 29 3b 0a 20 20 20 20 66 6f 72 28  TATIC);.    for(
28451 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  p=sqliteHashFirs
28452 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 29  t(&db->aCollSeq)
28453 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 48 61 73  ; p; p=sqliteHas
28454 68 4e 65 78 74 28 70 29 29 7b 0a 20 20 20 20 20  hNext(p)){.     
28455 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20   CollSeq *pColl 
28456 3d 20 28 43 6f 6c 6c 53 65 71 20 2a 29 73 71 6c  = (CollSeq *)sql
28457 69 74 65 48 61 73 68 44 61 74 61 28 70 29 3b 0a  iteHashData(p);.
28458 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
28459 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
2845a 74 65 67 65 72 2c 20 69 2b 2b 2c 20 31 29 3b 0a  teger, i++, 1);.
2845b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2845c 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
2845d 72 69 6e 67 38 2c 20 30 2c 20 32 2c 20 30 2c 20  ring8, 0, 2, 0, 
2845e 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20 30 29  pColl->zName, 0)
2845f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
28460 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
28461 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 32 29  ResultRow, 1, 2)
28462 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  ;.    }.  }else.
28463 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
28464 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 50 52 41  _OMIT_SCHEMA_PRA
28465 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  GMAS */..#ifndef
28466 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52   SQLITE_OMIT_FOR
28467 45 49 47 4e 5f 4b 45 59 0a 20 20 69 66 28 20 73  EIGN_KEY.  if( s
28468 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
28469 65 66 74 2c 20 22 66 6f 72 65 69 67 6e 5f 6b 65  eft, "foreign_ke
2846a 79 5f 6c 69 73 74 22 29 3d 3d 30 20 26 26 20 7a  y_list")==0 && z
2846b 52 69 67 68 74 20 29 7b 0a 20 20 20 20 46 4b 65  Right ){.    FKe
2846c 79 20 2a 70 46 4b 3b 0a 20 20 20 20 54 61 62 6c  y *pFK;.    Tabl
2846d 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 69 66 28  e *pTab;.    if(
2846e 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
2846f 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74  ma(pParse) ) got
28470 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20  o pragma_out;.  
28471 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
28472 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 52  FindTable(db, zR
28473 69 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20 20  ight, zDb);.    
28474 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20  if( pTab ){.    
28475 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
28476 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
28477 20 20 20 20 70 46 4b 20 3d 20 70 54 61 62 2d 3e      pFK = pTab->
28478 70 46 4b 65 79 3b 0a 20 20 20 20 20 20 69 66 28  pFKey;.      if(
28479 20 70 46 4b 20 29 7b 0a 20 20 20 20 20 20 20 20   pFK ){.        
2847a 69 6e 74 20 69 20 3d 20 30 3b 20 0a 20 20 20 20  int i = 0; .    
2847b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
2847c 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 38 29 3b  etNumCols(v, 8);
2847d 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d  .        pParse-
2847e 3e 6e 4d 65 6d 20 3d 20 38 3b 0a 20 20 20 20 20  >nMem = 8;.     
2847f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
28480 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
28481 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 69 64  OLNAME_NAME, "id
28482 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
28483 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
28484 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
28485 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 1, COLNAME_N
28486 41 4d 45 2c 20 22 73 65 71 22 2c 20 53 51 4c 49  AME, "seq", SQLI
28487 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
28488 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
28489 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20  etColName(v, 2, 
2848a 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 74  COLNAME_NAME, "t
2848b 61 62 6c 65 22 2c 20 53 51 4c 49 54 45 5f 53 54  able", SQLITE_ST
2848c 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73  ATIC);.        s
2848d 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
2848e 4e 61 6d 65 28 76 2c 20 33 2c 20 43 4f 4c 4e 41  Name(v, 3, COLNA
2848f 4d 45 5f 4e 41 4d 45 2c 20 22 66 72 6f 6d 22 2c  ME_NAME, "from",
28490 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
28491 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
28492 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
28493 2c 20 34 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 4, COLNAME_NAM
28494 45 2c 20 22 74 6f 22 2c 20 53 51 4c 49 54 45 5f  E, "to", SQLITE_
28495 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
28496 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
28497 6f 6c 4e 61 6d 65 28 76 2c 20 35 2c 20 43 4f 4c  olName(v, 5, COL
28498 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f 6e 5f 75  NAME_NAME, "on_u
28499 70 64 61 74 65 22 2c 20 53 51 4c 49 54 45 5f 53  pdate", SQLITE_S
2849a 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
2849b 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
2849c 6c 4e 61 6d 65 28 76 2c 20 36 2c 20 43 4f 4c 4e  lName(v, 6, COLN
2849d 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f 6e 5f 64 65  AME_NAME, "on_de
2849e 6c 65 74 65 22 2c 20 53 51 4c 49 54 45 5f 53 54  lete", SQLITE_ST
2849f 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73  ATIC);.        s
284a0 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
284a1 4e 61 6d 65 28 76 2c 20 37 2c 20 43 4f 4c 4e 41  Name(v, 7, COLNA
284a2 4d 45 5f 4e 41 4d 45 2c 20 22 6d 61 74 63 68 22  ME_NAME, "match"
284a3 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
284a4 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  ;.        while(
284a5 70 46 4b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  pFK){.          
284a6 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 20  int j;.         
284a7 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 46 4b 2d   for(j=0; j<pFK-
284a8 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20  >nCol; j++){.   
284a9 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a           char *z
284aa 43 6f 6c 20 3d 20 70 46 4b 2d 3e 61 43 6f 6c 5b  Col = pFK->aCol[
284ab 6a 5d 2e 7a 43 6f 6c 3b 0a 20 20 20 20 20 20 20  j].zCol;.       
284ac 20 20 20 20 20 63 68 61 72 20 2a 7a 4f 6e 55 70       char *zOnUp
284ad 64 61 74 65 20 3d 20 28 63 68 61 72 20 2a 29 61  date = (char *)a
284ae 63 74 69 6f 6e 4e 61 6d 65 28 70 46 4b 2d 3e 75  ctionName(pFK->u
284af 70 64 61 74 65 43 6f 6e 66 29 3b 0a 20 20 20 20  pdateConf);.    
284b0 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4f          char *zO
284b1 6e 44 65 6c 65 74 65 20 3d 20 28 63 68 61 72 20  nDelete = (char 
284b2 2a 29 61 63 74 69 6f 6e 4e 61 6d 65 28 70 46 4b  *)actionName(pFK
284b3 2d 3e 64 65 6c 65 74 65 43 6f 6e 66 29 3b 0a 20  ->deleteConf);. 
284b4 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
284b5 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
284b6 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 31  OP_Integer, i, 1
284b7 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  );.            s
284b8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
284b9 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
284ba 6a 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 20  j, 2);.         
284bb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
284bc 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
284bd 67 38 2c 20 30 2c 20 33 2c 20 30 2c 20 70 46 4b  g8, 0, 3, 0, pFK
284be 2d 3e 7a 54 6f 2c 20 30 29 3b 0a 20 20 20 20 20  ->zTo, 0);.     
284bf 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
284c0 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
284c1 74 72 69 6e 67 38 2c 20 30 2c 20 34 2c 20 30 2c  tring8, 0, 4, 0,
284c2 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
284c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
284c4 54 61 62 2d 3e 61 43 6f 6c 5b 70 46 4b 2d 3e 61  Tab->aCol[pFK->a
284c5 43 6f 6c 5b 6a 5d 2e 69 46 72 6f 6d 5d 2e 7a 4e  Col[j].iFrom].zN
284c6 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ame, 0);.       
284c7 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
284c8 41 64 64 4f 70 34 28 76 2c 20 7a 43 6f 6c 20 3f  AddOp4(v, zCol ?
284c9 20 4f 50 5f 53 74 72 69 6e 67 38 20 3a 20 4f 50   OP_String8 : OP
284ca 5f 4e 75 6c 6c 2c 20 30 2c 20 35 2c 20 30 2c 20  _Null, 0, 5, 0, 
284cb 7a 43 6f 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20  zCol, 0);.      
284cc 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
284cd 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
284ce 72 69 6e 67 38 2c 20 30 2c 20 36 2c 20 30 2c 20  ring8, 0, 6, 0, 
284cf 7a 4f 6e 55 70 64 61 74 65 2c 20 30 29 3b 0a 20  zOnUpdate, 0);. 
284d0 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
284d1 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
284d2 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 37  OP_String8, 0, 7
284d3 2c 20 30 2c 20 7a 4f 6e 44 65 6c 65 74 65 2c 20  , 0, zOnDelete, 
284d4 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  0);.            
284d5 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
284d6 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
284d7 20 30 2c 20 38 2c 20 30 2c 20 22 4e 4f 4e 45 22   0, 8, 0, "NONE"
284d8 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
284d9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
284da 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
284db 52 6f 77 2c 20 31 2c 20 38 29 3b 0a 20 20 20 20  Row, 1, 8);.    
284dc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
284dd 20 20 2b 2b 69 3b 0a 20 20 20 20 20 20 20 20 20    ++i;.         
284de 20 70 46 4b 20 3d 20 70 46 4b 2d 3e 70 4e 65 78   pFK = pFK->pNex
284df 74 46 72 6f 6d 3b 0a 20 20 20 20 20 20 20 20 7d  tFrom;.        }
284e0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
284e1 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a   }else.#endif /*
284e2 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
284e3 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45  _OMIT_FOREIGN_KE
284e4 59 29 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e  Y) */..#ifndef N
284e5 44 45 42 55 47 0a 20 20 69 66 28 20 73 71 6c 69  DEBUG.  if( sqli
284e6 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
284e7 2c 20 22 70 61 72 73 65 72 5f 74 72 61 63 65 22  , "parser_trace"
284e8 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  )==0 ){.    if( 
284e9 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  zRight ){.      
284ea 69 66 28 20 67 65 74 42 6f 6f 6c 65 61 6e 28 7a  if( getBoolean(z
284eb 52 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20  Right) ){.      
284ec 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54    sqlite3ParserT
284ed 72 61 63 65 28 73 74 64 65 72 72 2c 20 22 70 61  race(stderr, "pa
284ee 72 73 65 72 3a 20 22 29 3b 0a 20 20 20 20 20 20  rser: ");.      
284ef 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
284f0 71 6c 69 74 65 33 50 61 72 73 65 72 54 72 61 63  qlite3ParserTrac
284f1 65 28 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d  e(0, 0);.      }
284f2 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23  .    }.  }else.#
284f3 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 69 6e  endif..  /* Rein
284f4 73 74 61 6c 6c 20 74 68 65 20 4c 49 4b 45 20 61  stall the LIKE a
284f5 6e 64 20 47 4c 4f 42 20 66 75 6e 63 74 69 6f 6e  nd GLOB function
284f6 73 2e 20 20 54 68 65 20 76 61 72 69 61 6e 74 20  s.  The variant 
284f7 6f 66 20 4c 49 4b 45 0a 20 20 2a 2a 20 75 73 65  of LIKE.  ** use
284f8 64 20 77 69 6c 6c 20 62 65 20 63 61 73 65 20 73  d will be case s
284f9 65 6e 73 69 74 69 76 65 20 6f 72 20 6e 6f 74 20  ensitive or not 
284fa 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65  depending on the
284fb 20 52 48 53 2e 0a 20 20 2a 2f 0a 20 20 69 66 28   RHS..  */.  if(
284fc 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
284fd 7a 4c 65 66 74 2c 20 22 63 61 73 65 5f 73 65 6e  zLeft, "case_sen
284fe 73 69 74 69 76 65 5f 6c 69 6b 65 22 29 3d 3d 30  sitive_like")==0
284ff 20 29 7b 0a 20 20 20 20 69 66 28 20 7a 52 69 67   ){.    if( zRig
28500 68 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ht ){.      sqli
28501 74 65 33 52 65 67 69 73 74 65 72 4c 69 6b 65 46  te3RegisterLikeF
28502 75 6e 63 74 69 6f 6e 73 28 64 62 2c 20 67 65 74  unctions(db, get
28503 42 6f 6f 6c 65 61 6e 28 7a 52 69 67 68 74 29 29  Boolean(zRight))
28504 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  ;.    }.  }else.
28505 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
28506 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 5f  INTEGRITY_CHECK_
28507 45 52 52 4f 52 5f 4d 41 58 0a 23 20 64 65 66 69  ERROR_MAX.# defi
28508 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 52  ne SQLITE_INTEGR
28509 49 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f 52 5f  ITY_CHECK_ERROR_
2850a 4d 41 58 20 31 30 30 0a 23 65 6e 64 69 66 0a 0a  MAX 100.#endif..
2850b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2850c 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48  MIT_INTEGRITY_CH
2850d 45 43 4b 0a 20 20 2f 2a 20 50 72 61 67 6d 61 20  ECK.  /* Pragma 
2850e 22 71 75 69 63 6b 5f 63 68 65 63 6b 22 20 69 73  "quick_check" is
2850f 20 61 6e 20 65 78 70 65 72 69 6d 65 6e 74 61 6c   an experimental
28510 20 72 65 64 75 63 65 64 20 76 65 72 73 69 6f 6e   reduced version
28511 20 6f 66 20 0a 20 20 2a 2a 20 69 6e 74 65 67 72   of .  ** integr
28512 69 74 79 5f 63 68 65 63 6b 20 64 65 73 69 67 6e  ity_check design
28513 65 64 20 74 6f 20 64 65 74 65 63 74 20 6d 6f 73  ed to detect mos
28514 74 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75  t database corru
28515 70 74 69 6f 6e 0a 20 20 2a 2a 20 77 69 74 68 6f  ption.  ** witho
28516 75 74 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 6f  ut most of the o
28517 76 65 72 68 65 61 64 20 6f 66 20 61 20 66 75 6c  verhead of a ful
28518 6c 20 69 6e 74 65 67 72 69 74 79 2d 63 68 65 63  l integrity-chec
28519 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  k..  */.  if( sq
2851a 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
2851b 66 74 2c 20 22 69 6e 74 65 67 72 69 74 79 5f 63  ft, "integrity_c
2851c 68 65 63 6b 22 29 3d 3d 30 0a 20 20 20 7c 7c 20  heck")==0.   || 
2851d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
2851e 4c 65 66 74 2c 20 22 71 75 69 63 6b 5f 63 68 65  Left, "quick_che
2851f 63 6b 22 29 3d 3d 30 20 0a 20 20 29 7b 0a 20 20  ck")==0 .  ){.  
28520 20 20 69 6e 74 20 69 2c 20 6a 2c 20 61 64 64 72    int i, j, addr
28521 2c 20 6d 78 45 72 72 3b 0a 0a 20 20 20 20 2f 2a  , mxErr;..    /*
28522 20 43 6f 64 65 20 74 68 61 74 20 61 70 70 65 61   Code that appea
28523 72 73 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  rs at the end of
28524 20 74 68 65 20 69 6e 74 65 67 72 69 74 79 20 63   the integrity c
28525 68 65 63 6b 2e 20 20 49 66 20 6e 6f 20 65 72 72  heck.  If no err
28526 6f 72 0a 20 20 20 20 2a 2a 20 6d 65 73 73 61 67  or.    ** messag
28527 65 73 20 68 61 76 65 20 62 65 65 6e 20 67 65 6e  es have been gen
28528 65 72 61 74 65 64 2c 20 6f 75 74 70 75 74 20 4f  erated, output O
28529 4b 2e 20 20 4f 74 68 65 72 77 69 73 65 20 6f 75  K.  Otherwise ou
2852a 74 70 75 74 20 74 68 65 0a 20 20 20 20 2a 2a 20  tput the.    ** 
2852b 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20  error message.  
2852c 20 20 2a 2f 0a 20 20 20 20 73 74 61 74 69 63 20    */.    static 
2852d 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74  const VdbeOpList
2852e 20 65 6e 64 43 6f 64 65 5b 5d 20 3d 20 7b 0a 20   endCode[] = {. 
2852f 20 20 20 20 20 7b 20 4f 50 5f 41 64 64 49 6d 6d       { OP_AddImm
28530 2c 20 20 20 20 20 20 31 2c 20 30 2c 20 20 20 20  ,      1, 0,    
28531 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20      0},    /* 0 
28532 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49 66  */.      { OP_If
28533 4e 65 67 2c 20 20 20 20 20 20 20 31 2c 20 30 2c  Neg,       1, 0,
28534 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20 2f          0},    /
28535 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f  * 1 */.      { O
28536 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20 30  P_String8,     0
28537 2c 20 33 2c 20 20 20 20 20 20 20 20 30 7d 2c 20  , 3,        0}, 
28538 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 20     /* 2 */.     
28539 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c   { OP_ResultRow,
2853a 20 20 20 33 2c 20 31 2c 20 20 20 20 20 20 20 20     3, 1,        
2853b 30 7d 2c 0a 20 20 20 20 7d 3b 0a 0a 20 20 20 20  0},.    };..    
2853c 69 6e 74 20 69 73 51 75 69 63 6b 20 3d 20 28 7a  int isQuick = (z
2853d 4c 65 66 74 5b 30 5d 3d 3d 27 71 27 29 3b 0a 0a  Left[0]=='q');..
2853e 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a      /* Initializ
2853f 65 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72  e the VDBE progr
28540 61 6d 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71  am */.    if( sq
28541 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
28542 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70  pParse) ) goto p
28543 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70  ragma_out;.    p
28544 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 36 3b  Parse->nMem = 6;
28545 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
28546 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29  SetNumCols(v, 1)
28547 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
28548 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
28549 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
2854a 22 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b  "integrity_check
2854b 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
2854c 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74  );..    /* Set t
2854d 68 65 20 6d 61 78 69 6d 75 6d 20 65 72 72 6f 72  he maximum error
2854e 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 6d 78   count */.    mx
2854f 45 72 72 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54  Err = SQLITE_INT
28550 45 47 52 49 54 59 5f 43 48 45 43 4b 5f 45 52 52  EGRITY_CHECK_ERR
28551 4f 52 5f 4d 41 58 3b 0a 20 20 20 20 69 66 28 20  OR_MAX;.    if( 
28552 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  zRight ){.      
28553 6d 78 45 72 72 20 3d 20 61 74 6f 69 28 7a 52 69  mxErr = atoi(zRi
28554 67 68 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ght);.      if( 
28555 6d 78 45 72 72 3c 3d 30 20 29 7b 0a 20 20 20 20  mxErr<=0 ){.    
28556 20 20 20 20 6d 78 45 72 72 20 3d 20 53 51 4c 49      mxErr = SQLI
28557 54 45 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  TE_INTEGRITY_CHE
28558 43 4b 5f 45 52 52 4f 52 5f 4d 41 58 3b 0a 20 20  CK_ERROR_MAX;.  
28559 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2855a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2855b 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2855c 20 6d 78 45 72 72 2c 20 31 29 3b 20 20 2f 2a 20   mxErr, 1);  /* 
2855d 72 65 67 5b 31 5d 20 68 6f 6c 64 73 20 65 72 72  reg[1] holds err
2855e 6f 72 73 20 6c 65 66 74 20 2a 2f 0a 0a 20 20 20  ors left */..   
2855f 20 2f 2a 20 44 6f 20 61 6e 20 69 6e 74 65 67 72   /* Do an integr
28560 69 74 79 20 63 68 65 63 6b 20 6f 6e 20 65 61 63  ity check on eac
28561 68 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  h database file 
28562 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  */.    for(i=0; 
28563 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
28564 0a 20 20 20 20 20 20 48 61 73 68 45 6c 65 6d 20  .      HashElem 
28565 2a 78 3b 0a 20 20 20 20 20 20 48 61 73 68 20 2a  *x;.      Hash *
28566 70 54 62 6c 73 3b 0a 20 20 20 20 20 20 69 6e 74  pTbls;.      int
28567 20 63 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20 20   cnt = 0;..     
28568 20 69 66 28 20 4f 4d 49 54 5f 54 45 4d 50 44 42   if( OMIT_TEMPDB
28569 20 26 26 20 69 3d 3d 31 20 29 20 63 6f 6e 74 69   && i==1 ) conti
2856a 6e 75 65 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69  nue;..      sqli
2856b 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68  te3CodeVerifySch
2856c 65 6d 61 28 70 50 61 72 73 65 2c 20 69 29 3b 0a  ema(pParse, i);.
2856d 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
2856e 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2856f 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31 29 3b 20  , OP_IfPos, 1); 
28570 2f 2a 20 48 61 6c 74 20 69 66 20 6f 75 74 20 6f  /* Halt if out o
28571 66 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20 20  f errors */.    
28572 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
28573 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20  Op2(v, OP_Halt, 
28574 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c  0, 0);.      sql
28575 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
28576 28 76 2c 20 61 64 64 72 29 3b 0a 0a 20 20 20 20  (v, addr);..    
28577 20 20 2f 2a 20 44 6f 20 61 6e 20 69 6e 74 65 67    /* Do an integ
28578 72 69 74 79 20 63 68 65 63 6b 20 6f 66 20 74 68  rity check of th
28579 65 20 42 2d 54 72 65 65 0a 20 20 20 20 20 20 2a  e B-Tree.      *
2857a 2a 0a 20 20 20 20 20 20 2a 2a 20 42 65 67 69 6e  *.      ** Begin
2857b 20 62 79 20 66 69 6c 6c 69 6e 67 20 72 65 67 69   by filling regi
2857c 73 74 65 72 73 20 32 2c 20 33 2c 20 2e 2e 2e 20  sters 2, 3, ... 
2857d 77 69 74 68 20 74 68 65 20 72 6f 6f 74 20 70 61  with the root pa
2857e 67 65 73 20 6e 75 6d 62 65 72 73 0a 20 20 20 20  ges numbers.    
2857f 20 20 2a 2a 20 66 6f 72 20 61 6c 6c 20 74 61 62    ** for all tab
28580 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73 20  les and indices 
28581 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  in the database.
28582 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
28583 70 54 62 6c 73 20 3d 20 26 64 62 2d 3e 61 44 62  pTbls = &db->aDb
28584 5b 69 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c  [i].pSchema->tbl
28585 48 61 73 68 3b 0a 20 20 20 20 20 20 66 6f 72 28  Hash;.      for(
28586 78 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  x=sqliteHashFirs
28587 74 28 70 54 62 6c 73 29 3b 20 78 3b 20 78 3d 73  t(pTbls); x; x=s
28588 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 78 29  qliteHashNext(x)
28589 29 7b 0a 20 20 20 20 20 20 20 20 54 61 62 6c 65  ){.        Table
2858a 20 2a 70 54 61 62 20 3d 20 73 71 6c 69 74 65 48   *pTab = sqliteH
2858b 61 73 68 44 61 74 61 28 78 29 3b 0a 20 20 20 20  ashData(x);.    
2858c 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b      Index *pIdx;
2858d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2858e 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2858f 5f 49 6e 74 65 67 65 72 2c 20 70 54 61 62 2d 3e  _Integer, pTab->
28590 74 6e 75 6d 2c 20 32 2b 63 6e 74 29 3b 0a 20 20  tnum, 2+cnt);.  
28591 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20        cnt++;.   
28592 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54       for(pIdx=pT
28593 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78  ab->pIndex; pIdx
28594 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65  ; pIdx=pIdx->pNe
28595 78 74 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  xt){.          s
28596 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
28597 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
28598 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 32 2b 63 6e  pIdx->tnum, 2+cn
28599 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6e  t);.          cn
2859a 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  t++;.        }. 
2859b 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
2859c 20 4d 61 6b 65 20 73 75 72 65 20 73 75 66 66 69   Make sure suffi
2859d 63 69 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20  cient number of 
2859e 72 65 67 69 73 74 65 72 73 20 68 61 76 65 20 62  registers have b
2859f 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f  een allocated */
285a0 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73  .      if( pPars
285a1 65 2d 3e 6e 4d 65 6d 20 3c 20 63 6e 74 2b 34 20  e->nMem < cnt+4 
285a2 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73  ){.        pPars
285a3 65 2d 3e 6e 4d 65 6d 20 3d 20 63 6e 74 2b 34 3b  e->nMem = cnt+4;
285a4 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
285a5 2f 2a 20 44 6f 20 74 68 65 20 62 2d 74 72 65 65  /* Do the b-tree
285a6 20 69 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b   integrity check
285a7 73 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74  s */.      sqlit
285a8 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
285a9 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b 2c 20  OP_IntegrityCk, 
285aa 32 2c 20 63 6e 74 2c 20 31 29 3b 0a 20 20 20 20  2, cnt, 1);.    
285ab 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
285ac 6e 67 65 50 35 28 76 2c 20 28 75 38 29 69 29 3b  ngeP5(v, (u8)i);
285ad 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71  .      addr = sq
285ae 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
285af 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 32 29  v, OP_IsNull, 2)
285b0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
285b1 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
285b2 53 74 72 69 6e 67 38 2c 20 30 2c 20 33 2c 20 30  String8, 0, 3, 0
285b3 2c 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  ,.         sqlit
285b4 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 2a  e3MPrintf(db, "*
285b5 2a 2a 20 69 6e 20 64 61 74 61 62 61 73 65 20 25  ** in database %
285b6 73 20 2a 2a 2a 5c 6e 22 2c 20 64 62 2d 3e 61 44  s ***\n", db->aD
285b7 62 5b 69 5d 2e 7a 4e 61 6d 65 29 2c 0a 20 20 20  b[i].zName),.   
285b8 20 20 20 20 20 20 50 34 5f 44 59 4e 41 4d 49 43        P4_DYNAMIC
285b9 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
285ba 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
285bb 5f 4d 6f 76 65 2c 20 32 2c 20 34 2c 20 31 29 3b  _Move, 2, 4, 1);
285bc 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
285bd 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
285be 6f 6e 63 61 74 2c 20 34 2c 20 33 2c 20 32 29 3b  oncat, 4, 3, 2);
285bf 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
285c0 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
285c1 65 73 75 6c 74 52 6f 77 2c 20 32 2c 20 31 29 3b  esultRow, 2, 1);
285c2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
285c3 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
285c4 64 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4d  dr);..      /* M
285c5 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 74 68 65  ake sure all the
285c6 20 69 6e 64 69 63 65 73 20 61 72 65 20 63 6f 6e   indices are con
285c7 73 74 72 75 63 74 65 64 20 63 6f 72 72 65 63 74  structed correct
285c8 6c 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ly..      */.   
285c9 20 20 20 66 6f 72 28 78 3d 73 71 6c 69 74 65 48     for(x=sqliteH
285ca 61 73 68 46 69 72 73 74 28 70 54 62 6c 73 29 3b  ashFirst(pTbls);
285cb 20 78 20 26 26 20 21 69 73 51 75 69 63 6b 3b 20   x && !isQuick; 
285cc 78 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  x=sqliteHashNext
285cd 28 78 29 29 7b 0a 20 20 20 20 20 20 20 20 54 61  (x)){.        Ta
285ce 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69  ble *pTab = sqli
285cf 74 65 48 61 73 68 44 61 74 61 28 78 29 3b 0a 20  teHashData(x);. 
285d0 20 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49         Index *pI
285d1 64 78 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  dx;.        int 
285d2 6c 6f 6f 70 54 6f 70 3b 0a 0a 20 20 20 20 20 20  loopTop;..      
285d3 20 20 69 66 28 20 70 54 61 62 2d 3e 70 49 6e 64    if( pTab->pInd
285d4 65 78 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  ex==0 ) continue
285d5 3b 0a 20 20 20 20 20 20 20 20 61 64 64 72 20 3d  ;.        addr =
285d6 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
285d7 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20  p1(v, OP_IfPos, 
285d8 31 29 3b 20 20 2f 2a 20 53 74 6f 70 20 69 66 20  1);  /* Stop if 
285d9 6f 75 74 20 6f 66 20 65 72 72 6f 72 73 20 2a 2f  out of errors */
285da 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
285db 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
285dc 5f 48 61 6c 74 2c 20 30 2c 20 30 29 3b 0a 20 20  _Halt, 0, 0);.  
285dd 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
285de 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
285df 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  r);.        sqli
285e0 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49  te3OpenTableAndI
285e1 6e 64 69 63 65 73 28 70 50 61 72 73 65 2c 20 70  ndices(pParse, p
285e2 54 61 62 2c 20 31 2c 20 4f 50 5f 4f 70 65 6e 52  Tab, 1, OP_OpenR
285e3 65 61 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ead);.        sq
285e4 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
285e5 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30  v, OP_Integer, 0
285e6 2c 20 32 29 3b 20 20 2f 2a 20 72 65 67 28 32 29  , 2);  /* reg(2)
285e7 20 77 69 6c 6c 20 63 6f 75 6e 74 20 65 6e 74 72   will count entr
285e8 69 65 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 6c  ies */.        l
285e9 6f 6f 70 54 6f 70 20 3d 20 73 71 6c 69 74 65 33  oopTop = sqlite3
285ea 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
285eb 5f 52 65 77 69 6e 64 2c 20 31 2c 20 30 29 3b 0a  _Rewind, 1, 0);.
285ec 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
285ed 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
285ee 41 64 64 49 6d 6d 2c 20 32 2c 20 31 29 3b 20 20  AddImm, 2, 1);  
285ef 20 2f 2a 20 69 6e 63 72 65 6d 65 6e 74 20 65 6e   /* increment en
285f0 74 72 79 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 20  try count */.   
285f1 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49       for(j=0, pI
285f2 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
285f3 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
285f4 2d 3e 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20  ->pNext, j++){. 
285f5 20 20 20 20 20 20 20 20 20 69 6e 74 20 6a 6d 70           int jmp
285f6 32 3b 0a 20 20 20 20 20 20 20 20 20 20 73 74 61  2;.          sta
285f7 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70  tic const VdbeOp
285f8 4c 69 73 74 20 69 64 78 45 72 72 5b 5d 20 3d 20  List idxErr[] = 
285f9 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  {.            { 
285fa 4f 50 5f 41 64 64 49 6d 6d 2c 20 20 20 20 20 20  OP_AddImm,      
285fb 31 2c 20 2d 31 2c 20 20 30 7d 2c 0a 20 20 20 20  1, -1,  0},.    
285fc 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72          { OP_Str
285fd 69 6e 67 38 2c 20 20 20 20 20 30 2c 20 20 33 2c  ing8,     0,  3,
285fe 20 20 30 7d 2c 20 20 20 20 2f 2a 20 31 20 2a 2f    0},    /* 1 */
285ff 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
28600 50 5f 52 6f 77 69 64 2c 20 20 20 20 20 20 20 31  P_Rowid,       1
28601 2c 20 20 34 2c 20 20 30 7d 2c 0a 20 20 20 20 20  ,  4,  0},.     
28602 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69         { OP_Stri
28603 6e 67 38 2c 20 20 20 20 20 30 2c 20 20 35 2c 20  ng8,     0,  5, 
28604 20 30 7d 2c 20 20 20 20 2f 2a 20 33 20 2a 2f 0a   0},    /* 3 */.
28605 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
28606 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20 30 2c  _String8,     0,
28607 20 20 36 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20    6,  0},    /* 
28608 34 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  4 */.           
28609 20 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 20 20   { OP_Concat,   
2860a 20 20 20 34 2c 20 20 33 2c 20 20 33 7d 2c 0a 20     4,  3,  3},. 
2860b 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
2860c 43 6f 6e 63 61 74 2c 20 20 20 20 20 20 35 2c 20  Concat,      5, 
2860d 20 33 2c 20 20 33 7d 2c 0a 20 20 20 20 20 20 20   3,  3},.       
2860e 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61 74       { OP_Concat
2860f 2c 20 20 20 20 20 20 36 2c 20 20 33 2c 20 20 33  ,      6,  3,  3
28610 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  },.            {
28611 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 20   OP_ResultRow,  
28612 20 33 2c 20 20 31 2c 20 20 30 7d 2c 0a 20 20 20   3,  1,  0},.   
28613 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 66           { OP_If
28614 50 6f 73 2c 20 20 20 20 20 20 20 31 2c 20 20 30  Pos,       1,  0
28615 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 39 20 2a  ,  0},    /* 9 *
28616 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  /.            { 
28617 4f 50 5f 48 61 6c 74 2c 20 20 20 20 20 20 20 20  OP_Halt,        
28618 30 2c 20 20 30 2c 20 20 30 7d 2c 0a 20 20 20 20  0,  0,  0},.    
28619 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 20        };.       
2861a 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61     sqlite3Genera
2861b 74 65 49 6e 64 65 78 4b 65 79 28 70 50 61 72 73  teIndexKey(pPars
2861c 65 2c 20 70 49 64 78 2c 20 31 2c 20 33 2c 20 31  e, pIdx, 1, 3, 1
2861d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 6d 70  );.          jmp
2861e 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  2 = sqlite3VdbeA
2861f 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75 6e  ddOp3(v, OP_Foun
28620 64 2c 20 6a 2b 32 2c 20 30 2c 20 33 29 3b 0a 20  d, j+2, 0, 3);. 
28621 20 20 20 20 20 20 20 20 20 61 64 64 72 20 3d 20           addr = 
28622 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28623 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a  List(v, ArraySiz
28624 65 28 69 64 78 45 72 72 29 2c 20 69 64 78 45 72  e(idxErr), idxEr
28625 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  r);.          sq
28626 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
28627 34 28 76 2c 20 61 64 64 72 2b 31 2c 20 22 72 6f  4(v, addr+1, "ro
28628 77 69 64 20 22 2c 20 50 34 5f 53 54 41 54 49 43  wid ", P4_STATIC
28629 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
2862a 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
2862b 28 76 2c 20 61 64 64 72 2b 33 2c 20 22 20 6d 69  (v, addr+3, " mi
2862c 73 73 69 6e 67 20 66 72 6f 6d 20 69 6e 64 65 78  ssing from index
2862d 20 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a   ", P4_STATIC);.
2862e 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2862f 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
28630 20 61 64 64 72 2b 34 2c 20 70 49 64 78 2d 3e 7a   addr+4, pIdx->z
28631 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29  Name, P4_STATIC)
28632 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
28633 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
28634 76 2c 20 61 64 64 72 2b 39 29 3b 0a 20 20 20 20  v, addr+9);.    
28635 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
28636 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 6d 70  eJumpHere(v, jmp
28637 32 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  2);.        }.  
28638 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
28639 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
2863a 78 74 2c 20 31 2c 20 6c 6f 6f 70 54 6f 70 2b 31  xt, 1, loopTop+1
2863b 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2863c 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
2863d 2c 20 6c 6f 6f 70 54 6f 70 29 3b 0a 20 20 20 20  , loopTop);.    
2863e 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 64      for(j=0, pId
2863f 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
28640 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
28641 3e 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20 20  >pNext, j++){.  
28642 20 20 20 20 20 20 20 20 73 74 61 74 69 63 20 63          static c
28643 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20  onst VdbeOpList 
28644 63 6e 74 49 64 78 5b 5d 20 3d 20 7b 0a 20 20 20  cntIdx[] = {.   
28645 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49            { OP_I
28646 6e 74 65 67 65 72 2c 20 20 20 20 20 20 30 2c 20  nteger,      0, 
28647 20 33 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20   3,  0},.       
28648 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 77 69 6e        { OP_Rewin
28649 64 2c 20 20 20 20 20 20 20 30 2c 20 20 30 2c 20  d,       0,  0, 
2864a 20 30 7d 2c 20 20 2f 2a 20 31 20 2a 2f 0a 20 20   0},  /* 1 */.  
2864b 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
2864c 41 64 64 49 6d 6d 2c 20 20 20 20 20 20 20 33 2c  AddImm,       3,
2864d 20 20 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20    1,  0},.      
2864e 20 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65 78 74         { OP_Next
2864f 2c 20 20 20 20 20 20 20 20 20 30 2c 20 20 30 2c  ,         0,  0,
28650 20 20 30 7d 2c 20 20 2f 2a 20 33 20 2a 2f 0a 20    0},  /* 3 */. 
28651 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
28652 5f 45 71 2c 20 20 20 20 20 20 20 20 20 20 20 32  _Eq,           2
28653 2c 20 20 30 2c 20 20 33 7d 2c 20 20 2f 2a 20 34  ,  0,  3},  /* 4
28654 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
28655 20 7b 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 20 20   { OP_AddImm,   
28656 20 20 20 20 31 2c 20 2d 31 2c 20 20 30 7d 2c 0a      1, -1,  0},.
28657 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f               { O
28658 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20 20  P_String8,      
28659 30 2c 20 20 32 2c 20 20 30 7d 2c 20 20 2f 2a 20  0,  2,  0},  /* 
2865a 36 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  6 */.           
2865b 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20    { OP_String8, 
2865c 20 20 20 20 20 30 2c 20 20 33 2c 20 20 30 7d 2c       0,  3,  0},
2865d 20 20 2f 2a 20 37 20 2a 2f 0a 20 20 20 20 20 20    /* 7 */.      
2865e 20 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63         { OP_Conc
2865f 61 74 2c 20 20 20 20 20 20 20 33 2c 20 20 32 2c  at,       3,  2,
28660 20 20 32 7d 2c 0a 20 20 20 20 20 20 20 20 20 20    2},.          
28661 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f     { OP_ResultRo
28662 77 2c 20 20 20 20 32 2c 20 20 31 2c 20 20 30 7d  w,    2,  1,  0}
28663 2c 0a 20 20 20 20 20 20 20 20 20 20 7d 3b 0a 20  ,.          };. 
28664 20 20 20 20 20 20 20 20 20 61 64 64 72 20 3d 20           addr = 
28665 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28666 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31  1(v, OP_IfPos, 1
28667 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
28668 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
28669 2c 20 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30 29  , OP_Halt, 0, 0)
2866a 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
2866b 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
2866c 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20  v, addr);.      
2866d 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
2866e 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28  e3VdbeAddOpList(
2866f 76 2c 20 41 72 72 61 79 53 69 7a 65 28 63 6e 74  v, ArraySize(cnt
28670 49 64 78 29 2c 20 63 6e 74 49 64 78 29 3b 0a 20  Idx), cntIdx);. 
28671 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28672 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
28673 61 64 64 72 2b 31 2c 20 6a 2b 32 29 3b 0a 20 20  addr+1, j+2);.  
28674 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
28675 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 61  dbeChangeP2(v, a
28676 64 64 72 2b 31 2c 20 61 64 64 72 2b 34 29 3b 0a  ddr+1, addr+4);.
28677 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28678 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c  3VdbeChangeP1(v,
28679 20 61 64 64 72 2b 33 2c 20 6a 2b 32 29 3b 0a 20   addr+3, j+2);. 
2867a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2867b 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20  VdbeChangeP2(v, 
2867c 61 64 64 72 2b 33 2c 20 61 64 64 72 2b 32 29 3b  addr+3, addr+2);
2867d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2867e 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
2867f 2c 20 61 64 64 72 2b 34 29 3b 0a 20 20 20 20 20  , addr+4);.     
28680 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
28681 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72  ChangeP4(v, addr
28682 2b 36 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  +6, .           
28683 20 20 20 20 20 20 20 20 20 20 22 77 72 6f 6e 67            "wrong
28684 20 23 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e   # of entries in
28685 20 69 6e 64 65 78 20 22 2c 20 50 34 5f 53 54 41   index ", P4_STA
28686 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 20 20  TIC);.          
28687 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
28688 65 50 34 28 76 2c 20 61 64 64 72 2b 37 2c 20 70  eP4(v, addr+7, p
28689 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53  Idx->zName, P4_S
2868a 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
2868b 7d 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20 7d  }.      } .    }
2868c 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69  .    addr = sqli
2868d 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
2868e 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28 65 6e  (v, ArraySize(en
2868f 64 43 6f 64 65 29 2c 20 65 6e 64 43 6f 64 65 29  dCode), endCode)
28690 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
28691 65 43 68 61 6e 67 65 50 32 28 76 2c 20 61 64 64  eChangeP2(v, add
28692 72 2c 20 2d 6d 78 45 72 72 29 3b 0a 20 20 20 20  r, -mxErr);.    
28693 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
28694 65 72 65 28 76 2c 20 61 64 64 72 2b 31 29 3b 0a  ere(v, addr+1);.
28695 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
28696 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2b  hangeP4(v, addr+
28697 32 2c 20 22 6f 6b 22 2c 20 50 34 5f 53 54 41 54  2, "ok", P4_STAT
28698 49 43 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e  IC);.  }else.#en
28699 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2869a 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  IT_INTEGRITY_CHE
2869b 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  CK */..#ifndef S
2869c 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
2869d 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41  .  /*.  **   PRA
2869e 47 4d 41 20 65 6e 63 6f 64 69 6e 67 0a 20 20 2a  GMA encoding.  *
2869f 2a 20 20 20 50 52 41 47 4d 41 20 65 6e 63 6f 64  *   PRAGMA encod
286a0 69 6e 67 20 3d 20 22 75 74 66 2d 38 22 7c 22 75  ing = "utf-8"|"u
286a1 74 66 2d 31 36 22 7c 22 75 74 66 2d 31 36 6c 65  tf-16"|"utf-16le
286a2 22 7c 22 75 74 66 2d 31 36 62 65 22 0a 20 20 2a  "|"utf-16be".  *
286a3 2a 0a 20 20 2a 2a 20 49 6e 20 69 74 73 20 66 69  *.  ** In its fi
286a4 72 73 74 20 66 6f 72 6d 2c 20 74 68 69 73 20 70  rst form, this p
286a5 72 61 67 6d 61 20 72 65 74 75 72 6e 73 20 74 68  ragma returns th
286a6 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68  e encoding of th
286a7 65 20 6d 61 69 6e 0a 20 20 2a 2a 20 64 61 74 61  e main.  ** data
286a8 62 61 73 65 2e 20 49 66 20 74 68 65 20 64 61 74  base. If the dat
286a9 61 62 61 73 65 20 69 73 20 6e 6f 74 20 69 6e 69  abase is not ini
286aa 74 69 61 6c 69 7a 65 64 2c 20 69 74 20 69 73 20  tialized, it is 
286ab 69 6e 69 74 69 61 6c 69 7a 65 64 20 6e 6f 77 2e  initialized now.
286ac 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73  .  **.  ** The s
286ad 65 63 6f 6e 64 20 66 6f 72 6d 20 6f 66 20 74 68  econd form of th
286ae 69 73 20 70 72 61 67 6d 61 20 69 73 20 61 20 6e  is pragma is a n
286af 6f 2d 6f 70 20 69 66 20 74 68 65 20 6d 61 69 6e  o-op if the main
286b0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20   database file. 
286b1 20 2a 2a 20 68 61 73 20 6e 6f 74 20 61 6c 72 65   ** has not alre
286b2 61 64 79 20 62 65 65 6e 20 69 6e 69 74 69 61 6c  ady been initial
286b3 69 7a 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61  ized. In this ca
286b4 73 65 20 69 74 20 73 65 74 73 20 74 68 65 20 64  se it sets the d
286b5 65 66 61 75 6c 74 0a 20 20 2a 2a 20 65 6e 63 6f  efault.  ** enco
286b6 64 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 62  ding that will b
286b7 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 6d  e used for the m
286b8 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c  ain database fil
286b9 65 20 69 66 20 61 20 6e 65 77 20 66 69 6c 65 0a  e if a new file.
286ba 20 20 2a 2a 20 69 73 20 63 72 65 61 74 65 64 2e    ** is created.
286bb 20 49 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20   If an existing 
286bc 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
286bd 6c 65 20 69 73 20 6f 70 65 6e 65 64 2c 20 74 68  le is opened, th
286be 65 6e 20 74 68 65 0a 20 20 2a 2a 20 64 65 66 61  en the.  ** defa
286bf 75 6c 74 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  ult text encodin
286c0 67 20 66 6f 72 20 74 68 65 20 65 78 69 73 74 69  g for the existi
286c1 6e 67 20 64 61 74 61 62 61 73 65 20 69 73 20 75  ng database is u
286c2 73 65 64 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20  sed..  ** .  ** 
286c3 49 6e 20 61 6c 6c 20 63 61 73 65 73 20 6e 65 77  In all cases new
286c4 20 64 61 74 61 62 61 73 65 73 20 63 72 65 61 74   databases creat
286c5 65 64 20 75 73 69 6e 67 20 74 68 65 20 41 54 54  ed using the ATT
286c6 41 43 48 20 63 6f 6d 6d 61 6e 64 20 61 72 65 0a  ACH command are.
286c7 20 20 2a 2a 20 63 72 65 61 74 65 64 20 74 6f 20    ** created to 
286c8 75 73 65 20 74 68 65 20 73 61 6d 65 20 64 65 66  use the same def
286c9 61 75 6c 74 20 74 65 78 74 20 65 6e 63 6f 64 69  ault text encodi
286ca 6e 67 20 61 73 20 74 68 65 20 6d 61 69 6e 20 64  ng as the main d
286cb 61 74 61 62 61 73 65 2e 20 49 66 0a 20 20 2a 2a  atabase. If.  **
286cc 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
286cd 73 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20  se has not been 
286ce 69 6e 69 74 69 61 6c 69 7a 65 64 20 61 6e 64 2f  initialized and/
286cf 6f 72 20 63 72 65 61 74 65 64 20 77 68 65 6e 20  or created when 
286d0 41 54 54 41 43 48 0a 20 20 2a 2a 20 69 73 20 65  ATTACH.  ** is e
286d1 78 65 63 75 74 65 64 2c 20 74 68 69 73 20 69 73  xecuted, this is
286d2 20 64 6f 6e 65 20 62 65 66 6f 72 65 20 74 68 65   done before the
286d3 20 41 54 54 41 43 48 20 6f 70 65 72 61 74 69 6f   ATTACH operatio
286d4 6e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20  n..  **.  ** In 
286d5 74 68 65 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20  the second form 
286d6 74 68 69 73 20 70 72 61 67 6d 61 20 73 65 74 73  this pragma sets
286d7 20 74 68 65 20 74 65 78 74 20 65 6e 63 6f 64 69   the text encodi
286d8 6e 67 20 74 6f 20 62 65 20 75 73 65 64 20 69 6e  ng to be used in
286d9 0a 20 20 2a 2a 20 6e 65 77 20 64 61 74 61 62 61  .  ** new databa
286da 73 65 20 66 69 6c 65 73 20 63 72 65 61 74 65 64  se files created
286db 20 75 73 69 6e 67 20 74 68 69 73 20 64 61 74 61   using this data
286dc 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 49 74 20  base handle. It 
286dd 69 73 20 6f 6e 6c 79 0a 20 20 2a 2a 20 75 73 65  is only.  ** use
286de 66 75 6c 20 69 66 20 69 6e 76 6f 6b 65 64 20 69  ful if invoked i
286df 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 72  mmediately after
286e0 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
286e1 73 65 20 69 0a 20 20 2a 2f 0a 20 20 69 66 28 20  se i.  */.  if( 
286e2 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
286e3 4c 65 66 74 2c 20 22 65 6e 63 6f 64 69 6e 67 22  Left, "encoding"
286e4 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 61 74  )==0 ){.    stat
286e5 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20  ic const struct 
286e6 45 6e 63 4e 61 6d 65 20 7b 0a 20 20 20 20 20 20  EncName {.      
286e7 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 20  char *zName;.   
286e8 20 20 20 75 38 20 65 6e 63 3b 0a 20 20 20 20 7d     u8 enc;.    }
286e9 20 65 6e 63 6e 61 6d 65 73 5b 5d 20 3d 20 7b 0a   encnames[] = {.
286ea 20 20 20 20 20 20 7b 20 22 55 54 46 38 22 2c 20        { "UTF8", 
286eb 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 20      SQLITE_UTF8 
286ec 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 20 20         },.      
286ed 7b 20 22 55 54 46 2d 38 22 2c 20 20 20 20 53 51  { "UTF-8",    SQ
286ee 4c 49 54 45 5f 55 54 46 38 20 20 20 20 20 20 20  LITE_UTF8       
286ef 20 7d 2c 20 20 2f 2a 20 4d 75 73 74 20 62 65 20   },  /* Must be 
286f0 65 6c 65 6d 65 6e 74 20 5b 31 5d 20 2a 2f 0a 20  element [1] */. 
286f1 20 20 20 20 20 7b 20 22 55 54 46 2d 31 36 6c 65       { "UTF-16le
286f2 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  ", SQLITE_UTF16L
286f3 45 20 20 20 20 20 7d 2c 20 20 2f 2a 20 4d 75 73  E     },  /* Mus
286f4 74 20 62 65 20 65 6c 65 6d 65 6e 74 20 5b 32 5d  t be element [2]
286f5 20 2a 2f 0a 20 20 20 20 20 20 7b 20 22 55 54 46   */.      { "UTF
286f6 2d 31 36 62 65 22 2c 20 53 51 4c 49 54 45 5f 55  -16be", SQLITE_U
286f7 54 46 31 36 42 45 20 20 20 20 20 7d 2c 20 20 2f  TF16BE     },  /
286f8 2a 20 4d 75 73 74 20 62 65 20 65 6c 65 6d 65 6e  * Must be elemen
286f9 74 20 5b 33 5d 20 2a 2f 0a 20 20 20 20 20 20 7b  t [3] */.      {
286fa 20 22 55 54 46 31 36 6c 65 22 2c 20 20 53 51 4c   "UTF16le",  SQL
286fb 49 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20 20  ITE_UTF16LE     
286fc 7d 2c 0a 20 20 20 20 20 20 7b 20 22 55 54 46 31  },.      { "UTF1
286fd 36 62 65 22 2c 20 20 53 51 4c 49 54 45 5f 55 54  6be",  SQLITE_UT
286fe 46 31 36 42 45 20 20 20 20 20 7d 2c 0a 20 20 20  F16BE     },.   
286ff 20 20 20 7b 20 22 55 54 46 2d 31 36 22 2c 20 20     { "UTF-16",  
28700 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20   0              
28701 20 20 20 20 7d 2c 20 2f 2a 20 53 51 4c 49 54 45      }, /* SQLITE
28702 5f 55 54 46 31 36 4e 41 54 49 56 45 20 2a 2f 0a  _UTF16NATIVE */.
28703 20 20 20 20 20 20 7b 20 22 55 54 46 31 36 22 2c        { "UTF16",
28704 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20      0           
28705 20 20 20 20 20 20 20 7d 2c 20 2f 2a 20 53 51 4c         }, /* SQL
28706 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20  ITE_UTF16NATIVE 
28707 2a 2f 0a 20 20 20 20 20 20 7b 20 30 2c 20 30 20  */.      { 0, 0 
28708 7d 0a 20 20 20 20 7d 3b 0a 20 20 20 20 63 6f 6e  }.    };.    con
28709 73 74 20 73 74 72 75 63 74 20 45 6e 63 4e 61 6d  st struct EncNam
2870a 65 20 2a 70 45 6e 63 3b 0a 20 20 20 20 69 66 28  e *pEnc;.    if(
2870b 20 21 7a 52 69 67 68 74 20 29 7b 20 20 20 20 2f   !zRight ){    /
2870c 2a 20 22 50 52 41 47 4d 41 20 65 6e 63 6f 64 69  * "PRAGMA encodi
2870d 6e 67 22 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  ng" */.      if(
2870e 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
2870f 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74  ma(pParse) ) got
28710 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20  o pragma_out;.  
28711 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
28712 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b  etNumCols(v, 1);
28713 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
28714 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
28715 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
28716 20 22 65 6e 63 6f 64 69 6e 67 22 2c 20 53 51 4c   "encoding", SQL
28717 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
28718 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
28719 64 4f 70 32 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp2(v, OP_Strin
2871a 67 38 2c 20 30 2c 20 31 29 3b 0a 20 20 20 20 20  g8, 0, 1);.     
2871b 20 61 73 73 65 72 74 28 20 65 6e 63 6e 61 6d 65   assert( encname
2871c 73 5b 53 51 4c 49 54 45 5f 55 54 46 38 5d 2e 65  s[SQLITE_UTF8].e
2871d 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc==SQLITE_UTF8 
2871e 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
2871f 20 65 6e 63 6e 61 6d 65 73 5b 53 51 4c 49 54 45   encnames[SQLITE
28720 5f 55 54 46 31 36 4c 45 5d 2e 65 6e 63 3d 3d 53  _UTF16LE].enc==S
28721 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 29 3b  QLITE_UTF16LE );
28722 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65  .      assert( e
28723 6e 63 6e 61 6d 65 73 5b 53 51 4c 49 54 45 5f 55  ncnames[SQLITE_U
28724 54 46 31 36 42 45 5d 2e 65 6e 63 3d 3d 53 51 4c  TF16BE].enc==SQL
28725 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20  ITE_UTF16BE );. 
28726 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
28727 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20  ChangeP4(v, -1, 
28728 65 6e 63 6e 61 6d 65 73 5b 45 4e 43 28 70 50 61  encnames[ENC(pPa
28729 72 73 65 2d 3e 64 62 29 5d 2e 7a 4e 61 6d 65 2c  rse->db)].zName,
2872a 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P4_STATIC);.   
2872b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2872c 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
2872d 74 52 6f 77 2c 20 31 2c 20 31 29 3b 0a 20 20 20  tRow, 1, 1);.   
2872e 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20   }else{         
2872f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
28730 2a 20 22 50 52 41 47 4d 41 20 65 6e 63 6f 64 69  * "PRAGMA encodi
28731 6e 67 20 3d 20 58 58 58 22 20 2a 2f 0a 20 20 20  ng = XXX" */.   
28732 20 20 20 2f 2a 20 4f 6e 6c 79 20 63 68 61 6e 67     /* Only chang
28733 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 73  e the value of s
28734 71 6c 69 74 65 2e 65 6e 63 20 69 66 20 74 68 65  qlite.enc if the
28735 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
28736 20 69 73 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a   is not.      **
28737 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 49 66   initialized. If
28738 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
28739 73 65 20 65 78 69 73 74 73 2c 20 74 68 65 20 6e  se exists, the n
2873a 65 77 20 73 71 6c 69 74 65 2e 65 6e 63 20 76 61  ew sqlite.enc va
2873b 6c 75 65 0a 20 20 20 20 20 20 2a 2a 20 77 69 6c  lue.      ** wil
2873c 6c 20 62 65 20 6f 76 65 72 77 72 69 74 74 65 6e  l be overwritten
2873d 20 77 68 65 6e 20 74 68 65 20 73 63 68 65 6d 61   when the schema
2873e 20 69 73 20 6e 65 78 74 20 6c 6f 61 64 65 64 2e   is next loaded.
2873f 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 0a   If it does not.
28740 20 20 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79        ** already
28741 20 65 78 69 73 74 73 2c 20 69 74 20 77 69 6c 6c   exists, it will
28742 20 62 65 20 63 72 65 61 74 65 64 20 74 6f 20 75   be created to u
28743 73 65 20 74 68 65 20 6e 65 77 20 65 6e 63 6f 64  se the new encod
28744 69 6e 67 20 76 61 6c 75 65 2e 0a 20 20 20 20 20  ing value..     
28745 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 0a 20   */.      if( . 
28746 20 20 20 20 20 20 20 21 28 44 62 48 61 73 50 72         !(DbHasPr
28747 6f 70 65 72 74 79 28 64 62 2c 20 30 2c 20 44 42  operty(db, 0, DB
28748 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 29 20  _SchemaLoaded)) 
28749 7c 7c 20 0a 20 20 20 20 20 20 20 20 44 62 48 61  || .        DbHa
2874a 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20 30 2c  sProperty(db, 0,
2874b 20 44 42 5f 45 6d 70 74 79 29 20 0a 20 20 20 20   DB_Empty) .    
2874c 20 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f 72    ){.        for
2874d 28 70 45 6e 63 3d 26 65 6e 63 6e 61 6d 65 73 5b  (pEnc=&encnames[
2874e 30 5d 3b 20 70 45 6e 63 2d 3e 7a 4e 61 6d 65 3b  0]; pEnc->zName;
2874f 20 70 45 6e 63 2b 2b 29 7b 0a 20 20 20 20 20 20   pEnc++){.      
28750 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74      if( 0==sqlit
28751 65 33 53 74 72 49 43 6d 70 28 7a 52 69 67 68 74  e3StrICmp(zRight
28752 2c 20 70 45 6e 63 2d 3e 7a 4e 61 6d 65 29 20 29  , pEnc->zName) )
28753 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 45 4e  {.            EN
28754 43 28 70 50 61 72 73 65 2d 3e 64 62 29 20 3d 20  C(pParse->db) = 
28755 70 45 6e 63 2d 3e 65 6e 63 20 3f 20 70 45 6e 63  pEnc->enc ? pEnc
28756 2d 3e 65 6e 63 20 3a 20 53 51 4c 49 54 45 5f 55  ->enc : SQLITE_U
28757 54 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 20 20  TF16NATIVE;.    
28758 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
28759 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2875a 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
2875b 20 21 70 45 6e 63 2d 3e 7a 4e 61 6d 65 20 29 7b   !pEnc->zName ){
2875c 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2875d 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2875e 65 2c 20 22 75 6e 73 75 70 70 6f 72 74 65 64 20  e, "unsupported 
2875f 65 6e 63 6f 64 69 6e 67 3a 20 25 73 22 2c 20 7a  encoding: %s", z
28760 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 20 20  Right);.        
28761 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
28762 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f    }else.#endif /
28763 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  * SQLITE_OMIT_UT
28764 46 31 36 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  F16 */..#ifndef 
28765 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45  SQLITE_OMIT_SCHE
28766 4d 41 5f 56 45 52 53 49 4f 4e 5f 50 52 41 47 4d  MA_VERSION_PRAGM
28767 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50  AS.  /*.  **   P
28768 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
28769 5d 73 63 68 65 6d 61 5f 76 65 72 73 69 6f 6e 0a  ]schema_version.
2876a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64    **   PRAGMA [d
2876b 61 74 61 62 61 73 65 2e 5d 73 63 68 65 6d 61 5f  atabase.]schema_
2876c 76 65 72 73 69 6f 6e 20 3d 20 3c 69 6e 74 65 67  version = <integ
2876d 65 72 3e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  er>.  **.  **   
2876e 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
2876f 2e 5d 75 73 65 72 5f 76 65 72 73 69 6f 6e 0a 20  .]user_version. 
28770 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61   **   PRAGMA [da
28771 74 61 62 61 73 65 2e 5d 75 73 65 72 5f 76 65 72  tabase.]user_ver
28772 73 69 6f 6e 20 3d 20 3c 69 6e 74 65 67 65 72 3e  sion = <integer>
28773 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 70  .  **.  ** The p
28774 72 61 67 6d 61 27 73 20 73 63 68 65 6d 61 5f 76  ragma's schema_v
28775 65 72 73 69 6f 6e 20 61 6e 64 20 75 73 65 72 5f  ersion and user_
28776 76 65 72 73 69 6f 6e 20 61 72 65 20 75 73 65 64  version are used
28777 20 74 6f 20 73 65 74 20 6f 72 20 67 65 74 0a 20   to set or get. 
28778 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66   ** the value of
28779 20 74 68 65 20 73 63 68 65 6d 61 2d 76 65 72 73   the schema-vers
2877a 69 6f 6e 20 61 6e 64 20 75 73 65 72 2d 76 65 72  ion and user-ver
2877b 73 69 6f 6e 2c 20 72 65 73 70 65 63 74 69 76 65  sion, respective
2877c 6c 79 2e 20 42 6f 74 68 0a 20 20 2a 2a 20 74 68  ly. Both.  ** th
2877d 65 20 73 63 68 65 6d 61 2d 76 65 72 73 69 6f 6e  e schema-version
2877e 20 61 6e 64 20 74 68 65 20 75 73 65 72 2d 76 65   and the user-ve
2877f 72 73 69 6f 6e 20 61 72 65 20 33 32 2d 62 69 74  rsion are 32-bit
28780 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73   signed integers
28781 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20  .  ** stored in 
28782 74 68 65 20 64 61 74 61 62 61 73 65 20 68 65 61  the database hea
28783 64 65 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  der..  **.  ** T
28784 68 65 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65  he schema-cookie
28785 20 69 73 20 75 73 75 61 6c 6c 79 20 6f 6e 6c 79   is usually only
28786 20 6d 61 6e 69 70 75 6c 61 74 65 64 20 69 6e 74   manipulated int
28787 65 72 6e 61 6c 6c 79 20 62 79 20 53 51 4c 69 74  ernally by SQLit
28788 65 2e 20 49 74 0a 20 20 2a 2a 20 69 73 20 69 6e  e. It.  ** is in
28789 63 72 65 6d 65 6e 74 65 64 20 62 79 20 53 51 4c  cremented by SQL
2878a 69 74 65 20 77 68 65 6e 65 76 65 72 20 74 68 65  ite whenever the
2878b 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
2878c 20 69 73 20 6d 6f 64 69 66 69 65 64 20 28 62 79   is modified (by
2878d 0a 20 20 2a 2a 20 63 72 65 61 74 69 6e 67 20 6f  .  ** creating o
2878e 72 20 64 72 6f 70 70 69 6e 67 20 61 20 74 61 62  r dropping a tab
2878f 6c 65 20 6f 72 20 69 6e 64 65 78 29 2e 20 54 68  le or index). Th
28790 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e  e schema version
28791 20 69 73 20 75 73 65 64 20 62 79 0a 20 20 2a 2a   is used by.  **
28792 20 53 51 4c 69 74 65 20 65 61 63 68 20 74 69 6d   SQLite each tim
28793 65 20 61 20 71 75 65 72 79 20 69 73 20 65 78 65  e a query is exe
28794 63 75 74 65 64 20 74 6f 20 65 6e 73 75 72 65 20  cuted to ensure 
28795 74 68 61 74 20 74 68 65 20 69 6e 74 65 72 6e 61  that the interna
28796 6c 20 63 61 63 68 65 0a 20 20 2a 2a 20 6f 66 20  l cache.  ** of 
28797 74 68 65 20 73 63 68 65 6d 61 20 75 73 65 64 20  the schema used 
28798 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 74  when compiling t
28799 68 65 20 53 51 4c 20 71 75 65 72 79 20 6d 61 74  he SQL query mat
2879a 63 68 65 73 20 74 68 65 20 73 63 68 65 6d 61 20  ches the schema 
2879b 6f 66 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61  of.  ** the data
2879c 62 61 73 65 20 61 67 61 69 6e 73 74 20 77 68 69  base against whi
2879d 63 68 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20  ch the compiled 
2879e 71 75 65 72 79 20 69 73 20 61 63 74 75 61 6c 6c  query is actuall
2879f 79 20 65 78 65 63 75 74 65 64 2e 0a 20 20 2a 2a  y executed..  **
287a0 20 53 75 62 76 65 72 74 69 6e 67 20 74 68 69 73   Subverting this
287a1 20 6d 65 63 68 61 6e 69 73 6d 20 62 79 20 75 73   mechanism by us
287a2 69 6e 67 20 22 50 52 41 47 4d 41 20 73 63 68 65  ing "PRAGMA sche
287a3 6d 61 5f 76 65 72 73 69 6f 6e 22 20 74 6f 20 6d  ma_version" to m
287a4 6f 64 69 66 79 0a 20 20 2a 2a 20 74 68 65 20 73  odify.  ** the s
287a5 63 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20 69 73  chema-version is
287a6 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 64 61 6e   potentially dan
287a7 67 65 72 6f 75 73 20 61 6e 64 20 6d 61 79 20 6c  gerous and may l
287a8 65 61 64 20 74 6f 20 70 72 6f 67 72 61 6d 0a 20  ead to program. 
287a9 20 2a 2a 20 63 72 61 73 68 65 73 20 6f 72 20 64   ** crashes or d
287aa 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69  atabase corrupti
287ab 6f 6e 2e 20 55 73 65 20 77 69 74 68 20 63 61 75  on. Use with cau
287ac 74 69 6f 6e 21 0a 20 20 2a 2a 0a 20 20 2a 2a 20  tion!.  **.  ** 
287ad 54 68 65 20 75 73 65 72 2d 76 65 72 73 69 6f 6e  The user-version
287ae 20 69 73 20 6e 6f 74 20 75 73 65 64 20 69 6e 74   is not used int
287af 65 72 6e 61 6c 6c 79 20 62 79 20 53 51 4c 69 74  ernally by SQLit
287b0 65 2e 20 49 74 20 6d 61 79 20 62 65 20 75 73 65  e. It may be use
287b1 64 20 62 79 0a 20 20 2a 2a 20 61 70 70 6c 69 63  d by.  ** applic
287b2 61 74 69 6f 6e 73 20 66 6f 72 20 61 6e 79 20 70  ations for any p
287b3 75 72 70 6f 73 65 2e 0a 20 20 2a 2f 0a 20 20 69  urpose..  */.  i
287b4 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
287b5 70 28 7a 4c 65 66 74 2c 20 22 73 63 68 65 6d 61  p(zLeft, "schema
287b6 5f 76 65 72 73 69 6f 6e 22 29 3d 3d 30 20 0a 20  _version")==0 . 
287b7 20 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72 49    || sqlite3StrI
287b8 43 6d 70 28 7a 4c 65 66 74 2c 20 22 75 73 65 72  Cmp(zLeft, "user
287b9 5f 76 65 72 73 69 6f 6e 22 29 3d 3d 30 20 0a 20  _version")==0 . 
287ba 20 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72 49    || sqlite3StrI
287bb 43 6d 70 28 7a 4c 65 66 74 2c 20 22 66 72 65 65  Cmp(zLeft, "free
287bc 6c 69 73 74 5f 63 6f 75 6e 74 22 29 3d 3d 30 20  list_count")==0 
287bd 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 43  .  ){.    int iC
287be 6f 6f 6b 69 65 3b 20 20 20 2f 2a 20 43 6f 6f 6b  ookie;   /* Cook
287bf 69 65 20 69 6e 64 65 78 2e 20 31 20 66 6f 72 20  ie index. 1 for 
287c0 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 2c 20 36  schema-cookie, 6
287c1 20 66 6f 72 20 75 73 65 72 2d 63 6f 6f 6b 69 65   for user-cookie
287c2 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  . */.    sqlite3
287c3 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c  VdbeUsesBtree(v,
287c4 20 69 44 62 29 3b 0a 20 20 20 20 73 77 69 74 63   iDb);.    switc
287c5 68 28 20 7a 4c 65 66 74 5b 30 5d 20 29 7b 0a 20  h( zLeft[0] ){. 
287c6 20 20 20 20 20 63 61 73 65 20 27 66 27 3a 20 63       case 'f': c
287c7 61 73 65 20 27 46 27 3a 0a 20 20 20 20 20 20 20  ase 'F':.       
287c8 20 69 43 6f 6f 6b 69 65 20 3d 20 42 54 52 45 45   iCookie = BTREE
287c9 5f 46 52 45 45 5f 50 41 47 45 5f 43 4f 55 4e 54  _FREE_PAGE_COUNT
287ca 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
287cb 0a 20 20 20 20 20 20 63 61 73 65 20 27 73 27 3a  .      case 's':
287cc 20 63 61 73 65 20 27 53 27 3a 0a 20 20 20 20 20   case 'S':.     
287cd 20 20 20 69 43 6f 6f 6b 69 65 20 3d 20 42 54 52     iCookie = BTR
287ce 45 45 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f  EE_SCHEMA_VERSIO
287cf 4e 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  N;.        break
287d0 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a  ;.      default:
287d1 0a 20 20 20 20 20 20 20 20 69 43 6f 6f 6b 69 65  .        iCookie
287d2 20 3d 20 42 54 52 45 45 5f 55 53 45 52 5f 56 45   = BTREE_USER_VE
287d3 52 53 49 4f 4e 3b 0a 20 20 20 20 20 20 20 20 62  RSION;.        b
287d4 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
287d5 20 69 66 28 20 7a 52 69 67 68 74 20 26 26 20 69   if( zRight && i
287d6 43 6f 6f 6b 69 65 21 3d 42 54 52 45 45 5f 46 52  Cookie!=BTREE_FR
287d7 45 45 5f 50 41 47 45 5f 43 4f 55 4e 54 20 29 7b  EE_PAGE_COUNT ){
287d8 0a 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20  .      /* Write 
287d9 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63 6f  the specified co
287da 6f 6b 69 65 20 76 61 6c 75 65 20 2a 2f 0a 20 20  okie value */.  
287db 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
287dc 20 56 64 62 65 4f 70 4c 69 73 74 20 73 65 74 43   VdbeOpList setC
287dd 6f 6f 6b 69 65 5b 5d 20 3d 20 7b 0a 20 20 20 20  ookie[] = {.    
287de 20 20 20 20 7b 20 4f 50 5f 54 72 61 6e 73 61 63      { OP_Transac
287df 74 69 6f 6e 2c 20 20 20 20 30 2c 20 20 31 2c 20  tion,    0,  1, 
287e0 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a 2f 0a   0},    /* 0 */.
287e1 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e 74          { OP_Int
287e2 65 67 65 72 2c 20 20 20 20 20 20 20 20 30 2c 20  eger,        0, 
287e3 20 31 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 31   1,  0},    /* 1
287e4 20 2a 2f 0a 20 20 20 20 20 20 20 20 7b 20 4f 50   */.        { OP
287e5 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 20 20 20 20  _SetCookie,     
287e6 20 30 2c 20 20 30 2c 20 20 31 7d 2c 20 20 20 20   0,  0,  1},    
287e7 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 20 20 7d 3b  /* 2 */.      };
287e8 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 20  .      int addr 
287e9 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
287ea 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53  OpList(v, ArrayS
287eb 69 7a 65 28 73 65 74 43 6f 6f 6b 69 65 29 2c 20  ize(setCookie), 
287ec 73 65 74 43 6f 6f 6b 69 65 29 3b 0a 20 20 20 20  setCookie);.    
287ed 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
287ee 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2c 20 69  ngeP1(v, addr, i
287ef 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Db);.      sqlit
287f0 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76  e3VdbeChangeP1(v
287f1 2c 20 61 64 64 72 2b 31 2c 20 61 74 6f 69 28 7a  , addr+1, atoi(z
287f2 52 69 67 68 74 29 29 3b 0a 20 20 20 20 20 20 73  Right));.      s
287f3 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
287f4 50 31 28 76 2c 20 61 64 64 72 2b 32 2c 20 69 44  P1(v, addr+2, iD
287f5 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
287f6 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c  3VdbeChangeP2(v,
287f7 20 61 64 64 72 2b 32 2c 20 69 43 6f 6f 6b 69 65   addr+2, iCookie
287f8 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
287f9 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20      /* Read the 
287fa 73 70 65 63 69 66 69 65 64 20 63 6f 6f 6b 69 65  specified cookie
287fb 20 76 61 6c 75 65 20 2a 2f 0a 20 20 20 20 20 20   value */.      
287fc 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62  static const Vdb
287fd 65 4f 70 4c 69 73 74 20 72 65 61 64 43 6f 6f 6b  eOpList readCook
287fe 69 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20  ie[] = {.       
287ff 20 7b 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f   { OP_Transactio
28800 6e 2c 20 20 20 20 20 30 2c 20 20 30 2c 20 20 30  n,     0,  0,  0
28801 7d 2c 20 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20  },    /* 0 */.  
28802 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 61 64 43        { OP_ReadC
28803 6f 6f 6b 69 65 2c 20 20 20 20 20 20 30 2c 20 20  ookie,      0,  
28804 31 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 31 20  1,  0},    /* 1 
28805 2a 2f 0a 20 20 20 20 20 20 20 20 7b 20 4f 50 5f  */.        { OP_
28806 52 65 73 75 6c 74 52 6f 77 2c 20 20 20 20 20 20  ResultRow,      
28807 20 31 2c 20 20 31 2c 20 20 30 7d 0a 20 20 20 20   1,  1,  0}.    
28808 20 20 7d 3b 0a 20 20 20 20 20 20 69 6e 74 20 61    };.      int a
28809 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
2880a 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72  eAddOpList(v, Ar
2880b 72 61 79 53 69 7a 65 28 72 65 61 64 43 6f 6f 6b  raySize(readCook
2880c 69 65 29 2c 20 72 65 61 64 43 6f 6f 6b 69 65 29  ie), readCookie)
2880d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2880e 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61  dbeChangeP1(v, a
2880f 64 64 72 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  ddr, iDb);.     
28810 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
28811 67 65 50 31 28 76 2c 20 61 64 64 72 2b 31 2c 20  geP1(v, addr+1, 
28812 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  iDb);.      sqli
28813 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28  te3VdbeChangeP3(
28814 76 2c 20 61 64 64 72 2b 31 2c 20 69 43 6f 6f 6b  v, addr+1, iCook
28815 69 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ie);.      sqlit
28816 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
28817 28 76 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71  (v, 1);.      sq
28818 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
28819 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
2881a 45 5f 4e 41 4d 45 2c 20 7a 4c 65 66 74 2c 20 53  E_NAME, zLeft, S
2881b 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
2881c 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  ;.    }.  }else.
2881d 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2881e 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 56 45 52  _OMIT_SCHEMA_VER
2881f 53 49 4f 4e 5f 50 52 41 47 4d 41 53 20 2a 2f 0a  SION_PRAGMAS */.
28820 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
28821 49 54 45 5f 44 45 42 55 47 29 20 7c 7c 20 64 65  ITE_DEBUG) || de
28822 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53  fined(SQLITE_TES
28823 54 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 70  T).  /*.  ** Rep
28824 6f 72 74 20 74 68 65 20 63 75 72 72 65 6e 74 20  ort the current 
28825 73 74 61 74 65 20 6f 66 20 66 69 6c 65 20 6c 6f  state of file lo
28826 67 73 20 66 6f 72 20 61 6c 6c 20 64 61 74 61 62  gs for all datab
28827 61 73 65 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ases.  */.  if( 
28828 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
28829 4c 65 66 74 2c 20 22 6c 6f 63 6b 5f 73 74 61 74  Left, "lock_stat
2882a 75 73 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73  us")==0 ){.    s
2882b 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
2882c 20 2a 63 6f 6e 73 74 20 61 7a 4c 6f 63 6b 4e 61   *const azLockNa
2882d 6d 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 22  me[] = {.      "
2882e 75 6e 6c 6f 63 6b 65 64 22 2c 20 22 73 68 61 72  unlocked", "shar
2882f 65 64 22 2c 20 22 72 65 73 65 72 76 65 64 22 2c  ed", "reserved",
28830 20 22 70 65 6e 64 69 6e 67 22 2c 20 22 65 78 63   "pending", "exc
28831 6c 75 73 69 76 65 22 0a 20 20 20 20 7d 3b 0a 20  lusive".    };. 
28832 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 73 71     int i;.    sq
28833 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
28834 6f 6c 73 28 76 2c 20 32 29 3b 0a 20 20 20 20 70  ols(v, 2);.    p
28835 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 32 3b  Parse->nMem = 2;
28836 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
28837 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
28838 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
28839 64 61 74 61 62 61 73 65 22 2c 20 53 51 4c 49 54  database", SQLIT
2883a 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73  E_STATIC);.    s
2883b 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
2883c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41  Name(v, 1, COLNA
2883d 4d 45 5f 4e 41 4d 45 2c 20 22 73 74 61 74 75 73  ME_NAME, "status
2883e 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
2883f 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  );.    for(i=0; 
28840 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
28841 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42  .      Btree *pB
28842 74 3b 0a 20 20 20 20 20 20 50 61 67 65 72 20 2a  t;.      Pager *
28843 70 50 61 67 65 72 3b 0a 20 20 20 20 20 20 63 6f  pPager;.      co
28844 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 61 74 65  nst char *zState
28845 20 3d 20 22 75 6e 6b 6e 6f 77 6e 22 3b 0a 20 20   = "unknown";.  
28846 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20      int j;.     
28847 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e   if( db->aDb[i].
28848 7a 4e 61 6d 65 3d 3d 30 20 29 20 63 6f 6e 74 69  zName==0 ) conti
28849 6e 75 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  nue;.      sqlit
2884a 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2884b 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 31  OP_String8, 0, 1
2884c 2c 20 30 2c 20 64 62 2d 3e 61 44 62 5b 69 5d 2e  , 0, db->aDb[i].
2884d 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43  zName, P4_STATIC
2884e 29 3b 0a 20 20 20 20 20 20 70 42 74 20 3d 20 64  );.      pBt = d
2884f 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20  b->aDb[i].pBt;. 
28850 20 20 20 20 20 69 66 28 20 70 42 74 3d 3d 30 20       if( pBt==0 
28851 7c 7c 20 28 70 50 61 67 65 72 20 3d 20 73 71 6c  || (pPager = sql
28852 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70  ite3BtreePager(p
28853 42 74 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Bt))==0 ){.     
28854 20 20 20 7a 53 74 61 74 65 20 3d 20 22 63 6c 6f     zState = "clo
28855 73 65 64 22 3b 0a 20 20 20 20 20 20 7d 65 6c 73  sed";.      }els
28856 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f 66 69  e if( sqlite3_fi
28857 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c 20 69  le_control(db, i
28858 20 3f 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e   ? db->aDb[i].zN
28859 61 6d 65 20 3a 20 30 2c 20 0a 20 20 20 20 20 20  ame : 0, .      
2885a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2885b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
2885c 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b  QLITE_FCNTL_LOCK
2885d 53 54 41 54 45 2c 20 26 6a 29 3d 3d 53 51 4c 49  STATE, &j)==SQLI
2885e 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
2885f 20 20 7a 53 74 61 74 65 20 3d 20 61 7a 4c 6f 63    zState = azLoc
28860 6b 4e 61 6d 65 5b 6a 5d 3b 0a 20 20 20 20 20 20  kName[j];.      
28861 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  }.      sqlite3V
28862 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
28863 53 74 72 69 6e 67 38 2c 20 30 2c 20 32 2c 20 30  String8, 0, 2, 0
28864 2c 20 7a 53 74 61 74 65 2c 20 50 34 5f 53 54 41  , zState, P4_STA
28865 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  TIC);.      sqli
28866 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
28867 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31   OP_ResultRow, 1
28868 2c 20 32 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d  , 2);.    }..  }
28869 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 23 69 66  else.#endif..#if
2886a 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45   SQLITE_HAS_CODE
2886b 43 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  C.  if( sqlite3S
2886c 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 6b  trICmp(zLeft, "k
2886d 65 79 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68  ey")==0 && zRigh
2886e 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
2886f 5f 6b 65 79 28 64 62 2c 20 7a 52 69 67 68 74 2c  _key(db, zRight,
28870 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
28871 28 7a 52 69 67 68 74 29 29 3b 0a 20 20 7d 65 6c  (zRight));.  }el
28872 73 65 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  se.  if( sqlite3
28873 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
28874 72 65 6b 65 79 22 29 3d 3d 30 20 26 26 20 7a 52  rekey")==0 && zR
28875 69 67 68 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  ight ){.    sqli
28876 74 65 33 5f 72 65 6b 65 79 28 64 62 2c 20 7a 52  te3_rekey(db, zR
28877 69 67 68 74 2c 20 73 71 6c 69 74 65 33 53 74 72  ight, sqlite3Str
28878 6c 65 6e 33 30 28 7a 52 69 67 68 74 29 29 3b 0a  len30(zRight));.
28879 20 20 7d 65 6c 73 65 0a 20 20 69 66 28 20 7a 52    }else.  if( zR
2887a 69 67 68 74 20 26 26 20 28 73 71 6c 69 74 65 33  ight && (sqlite3
2887b 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
2887c 68 65 78 6b 65 79 22 29 3d 3d 30 20 7c 7c 0a 20  hexkey")==0 ||. 
2887d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2887e 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
2887f 4c 65 66 74 2c 20 22 68 65 78 72 65 6b 65 79 22  Left, "hexrekey"
28880 29 3d 3d 30 29 20 29 7b 0a 20 20 20 20 69 6e 74  )==0) ){.    int
28881 20 69 2c 20 68 31 2c 20 68 32 3b 0a 20 20 20 20   i, h1, h2;.    
28882 63 68 61 72 20 7a 4b 65 79 5b 34 30 5d 3b 0a 20  char zKey[40];. 
28883 20 20 20 66 6f 72 28 69 3d 30 3b 20 28 68 31 20     for(i=0; (h1 
28884 3d 20 7a 52 69 67 68 74 5b 69 5d 29 21 3d 30 20  = zRight[i])!=0 
28885 26 26 20 28 68 32 20 3d 20 7a 52 69 67 68 74 5b  && (h2 = zRight[
28886 69 2b 31 5d 29 21 3d 30 3b 20 69 2b 3d 32 29 7b  i+1])!=0; i+=2){
28887 0a 20 20 20 20 20 20 68 31 20 2b 3d 20 39 2a 28  .      h1 += 9*(
28888 31 26 28 68 31 3e 3e 36 29 29 3b 0a 20 20 20 20  1&(h1>>6));.    
28889 20 20 68 32 20 2b 3d 20 39 2a 28 31 26 28 68 32    h2 += 9*(1&(h2
2888a 3e 3e 36 29 29 3b 0a 20 20 20 20 20 20 7a 4b 65  >>6));.      zKe
2888b 79 5b 69 2f 32 5d 20 3d 20 28 68 32 20 26 20 30  y[i/2] = (h2 & 0
2888c 78 30 66 29 20 7c 20 28 28 68 31 20 26 20 30 78  x0f) | ((h1 & 0x
2888d 66 29 3c 3c 34 29 3b 0a 20 20 20 20 7d 0a 20 20  f)<<4);.    }.  
2888e 20 20 69 66 28 20 28 7a 4c 65 66 74 5b 33 5d 20    if( (zLeft[3] 
2888f 26 20 30 78 66 29 3d 3d 30 78 62 20 29 7b 0a 20  & 0xf)==0xb ){. 
28890 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6b 65 79       sqlite3_key
28891 28 64 62 2c 20 7a 4b 65 79 2c 20 69 2f 32 29 3b  (db, zKey, i/2);
28892 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
28893 20 20 73 71 6c 69 74 65 33 5f 72 65 6b 65 79 28    sqlite3_rekey(
28894 64 62 2c 20 7a 4b 65 79 2c 20 69 2f 32 29 3b 0a  db, zKey, i/2);.
28895 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65      }.  }else.#e
28896 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
28897 48 41 53 5f 43 4f 44 45 43 20 7c 7c 20 64 65 66  HAS_CODEC || def
28898 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
28899 4c 45 5f 43 45 52 4f 44 29 0a 20 20 69 66 28 20  LE_CEROD).  if( 
2889a 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
2889b 4c 65 66 74 2c 20 22 61 63 74 69 76 61 74 65 5f  Left, "activate_
2889c 65 78 74 65 6e 73 69 6f 6e 73 22 29 3d 3d 30 20  extensions")==0 
2889d 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 48 41  ){.#if SQLITE_HA
2889e 53 5f 43 4f 44 45 43 0a 20 20 20 20 69 66 28 20  S_CODEC.    if( 
2889f 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
288a0 7a 52 69 67 68 74 2c 20 22 73 65 65 2d 22 2c 20  zRight, "see-", 
288a1 34 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 65  4)==0 ){.      e
288a2 78 74 65 72 6e 20 76 6f 69 64 20 73 71 6c 69 74  xtern void sqlit
288a3 65 33 5f 61 63 74 69 76 61 74 65 5f 73 65 65 28  e3_activate_see(
288a4 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20  const char*);.  
288a5 20 20 20 20 73 71 6c 69 74 65 33 5f 61 63 74 69      sqlite3_acti
288a6 76 61 74 65 5f 73 65 65 28 26 7a 52 69 67 68 74  vate_see(&zRight
288a7 5b 34 5d 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  [4]);.    }.#end
288a8 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  if.#ifdef SQLITE
288a9 5f 45 4e 41 42 4c 45 5f 43 45 52 4f 44 0a 20 20  _ENABLE_CEROD.  
288aa 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
288ab 4e 49 43 6d 70 28 7a 52 69 67 68 74 2c 20 22 63  NICmp(zRight, "c
288ac 65 72 6f 64 2d 22 2c 20 36 29 3d 3d 30 20 29 7b  erod-", 6)==0 ){
288ad 0a 20 20 20 20 20 20 65 78 74 65 72 6e 20 76 6f  .      extern vo
288ae 69 64 20 73 71 6c 69 74 65 33 5f 61 63 74 69 76  id sqlite3_activ
288af 61 74 65 5f 63 65 72 6f 64 28 63 6f 6e 73 74 20  ate_cerod(const 
288b0 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 73 71  char*);.      sq
288b1 6c 69 74 65 33 5f 61 63 74 69 76 61 74 65 5f 63  lite3_activate_c
288b2 65 72 6f 64 28 26 7a 52 69 67 68 74 5b 36 5d 29  erod(&zRight[6])
288b3 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
288b4 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 20   }else.#endif.. 
288b5 0a 20 20 7b 2f 2a 20 45 6d 70 74 79 20 45 4c 53  .  {/* Empty ELS
288b6 45 20 63 6c 61 75 73 65 20 2a 2f 7d 0a 0a 20 20  E clause */}..  
288b7 2f 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f 45 78  /* Code an OP_Ex
288b8 70 69 72 65 20 61 74 20 74 68 65 20 65 6e 64 20  pire at the end 
288b9 6f 66 20 65 61 63 68 20 50 52 41 47 4d 41 20 70  of each PRAGMA p
288ba 72 6f 67 72 61 6d 20 74 6f 20 63 61 75 73 65 0a  rogram to cause.
288bb 20 20 2a 2a 20 74 68 65 20 56 44 42 45 20 69 6d    ** the VDBE im
288bc 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 70  plementing the p
288bd 72 61 67 6d 61 20 74 6f 20 65 78 70 69 72 65 2e  ragma to expire.
288be 20 4d 6f 73 74 20 28 61 6c 6c 3f 29 20 70 72 61   Most (all?) pra
288bf 67 6d 61 73 0a 20 20 2a 2a 20 61 72 65 20 6f 6e  gmas.  ** are on
288c0 6c 79 20 76 61 6c 69 64 20 66 6f 72 20 61 20 73  ly valid for a s
288c1 69 6e 67 6c 65 20 65 78 65 63 75 74 69 6f 6e 2e  ingle execution.
288c2 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  .  */.  sqlite3V
288c3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
288c4 45 78 70 69 72 65 2c 20 31 2c 20 30 29 3b 0a 0a  Expire, 1, 0);..
288c5 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 73 65 74 20    /*.  ** Reset 
288c6 74 68 65 20 73 61 66 65 74 79 20 6c 65 76 65 6c  the safety level
288c7 2c 20 69 6e 20 63 61 73 65 20 74 68 65 20 66 75  , in case the fu
288c8 6c 6c 66 73 79 6e 63 20 66 6c 61 67 20 6f 72 20  llfsync flag or 
288c9 73 79 6e 63 68 72 6f 6e 6f 75 73 0a 20 20 2a 2a  synchronous.  **
288ca 20 73 65 74 74 69 6e 67 20 63 68 61 6e 67 65 64   setting changed
288cb 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ..  */.#ifndef S
288cc 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52  QLITE_OMIT_PAGER
288cd 5f 50 52 41 47 4d 41 53 0a 20 20 69 66 28 20 64  _PRAGMAS.  if( d
288ce 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b  b->autoCommit ){
288cf 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
288d0 65 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28  eSetSafetyLevel(
288d1 70 44 62 2d 3e 70 42 74 2c 20 70 44 62 2d 3e 73  pDb->pBt, pDb->s
288d2 61 66 65 74 79 5f 6c 65 76 65 6c 2c 0a 20 20 20  afety_level,.   
288d3 20 20 20 20 20 20 20 20 20 20 20 20 28 64 62 2d              (db-
288d4 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 75  >flags&SQLITE_Fu
288d5 6c 6c 46 53 79 6e 63 29 21 3d 30 29 3b 0a 20 20  llFSync)!=0);.  
288d6 7d 0a 23 65 6e 64 69 66 0a 70 72 61 67 6d 61 5f  }.#endif.pragma_
288d7 6f 75 74 3a 0a 20 20 73 71 6c 69 74 65 33 44 62  out:.  sqlite3Db
288d8 46 72 65 65 28 64 62 2c 20 7a 4c 65 66 74 29 3b  Free(db, zLeft);
288d9 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
288da 28 64 62 2c 20 7a 52 69 67 68 74 29 3b 0a 7d 0a  (db, zRight);.}.
288db 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
288dc 45 5f 4f 4d 49 54 5f 50 52 41 47 4d 41 20 2a 2f  E_OMIT_PRAGMA */
288dd 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
288de 2a 20 45 6e 64 20 6f 66 20 70 72 61 67 6d 61 2e  * End of pragma.
288df 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
288e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288e2 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
288e3 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 72 65  * Begin file pre
288e4 70 61 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  pare.c *********
288e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288e7 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4d 61 79  /./*.** 2005 May
288e8 20 32 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   25.**.** The au
288e9 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
288ea 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
288eb 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
288ec 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
288ed 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
288ee 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
288ef 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
288f0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
288f1 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
288f2 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
288f3 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
288f4 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
288f5 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
288f6 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
288f7 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
288f8 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
288f9 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
288fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288fe 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
288ff 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d 70  contains the imp
28900 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
28901 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  he sqlite3_prepa
28902 72 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63  re().** interfac
28903 65 2c 20 61 6e 64 20 72 6f 75 74 69 6e 65 73 20  e, and routines 
28904 74 68 61 74 20 63 6f 6e 74 72 69 62 75 74 65 20  that contribute 
28905 74 6f 20 6c 6f 61 64 69 6e 67 20 74 68 65 20 64  to loading the d
28906 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 0a 2a  atabase schema.*
28907 2a 20 66 72 6f 6d 20 64 69 73 6b 2e 0a 2a 2a 0a  * from disk..**.
28908 2a 2a 20 24 49 64 3a 20 70 72 65 70 61 72 65 2e  ** $Id: prepare.
28909 63 2c 76 20 31 2e 31 33 31 20 32 30 30 39 2f 30  c,v 1.131 2009/0
2890a 38 2f 30 36 20 31 37 3a 34 33 3a 33 31 20 64 72  8/06 17:43:31 dr
2890b 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  h Exp $.*/../*.*
2890c 2a 20 46 69 6c 6c 20 74 68 65 20 49 6e 69 74 44  * Fill the InitD
2890d 61 74 61 20 73 74 72 75 63 74 75 72 65 20 77 69  ata structure wi
2890e 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  th an error mess
2890f 61 67 65 20 74 68 61 74 20 69 6e 64 69 63 61 74  age that indicat
28910 65 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 64  es.** that the d
28911 61 74 61 62 61 73 65 20 69 73 20 63 6f 72 72 75  atabase is corru
28912 70 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  pt..*/.static vo
28913 69 64 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61  id corruptSchema
28914 28 0a 20 20 49 6e 69 74 44 61 74 61 20 2a 70 44  (.  InitData *pD
28915 61 74 61 2c 20 20 20 20 20 2f 2a 20 49 6e 69 74  ata,     /* Init
28916 69 61 6c 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65  ialization conte
28917 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  xt */.  const ch
28918 61 72 20 2a 7a 4f 62 6a 2c 20 20 20 20 2f 2a 20  ar *zObj,    /* 
28919 4f 62 6a 65 63 74 20 62 65 69 6e 67 20 70 61 72  Object being par
2891a 73 65 64 20 61 74 20 74 68 65 20 70 6f 69 6e 74  sed at the point
2891b 20 6f 66 20 65 72 72 6f 72 20 2a 2f 0a 20 20 63   of error */.  c
2891c 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 78 74 72  onst char *zExtr
2891d 61 20 20 20 2f 2a 20 45 72 72 6f 72 20 69 6e 66  a   /* Error inf
2891e 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20  ormation */.){. 
2891f 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
28920 44 61 74 61 2d 3e 64 62 3b 0a 20 20 69 66 28 20  Data->db;.  if( 
28921 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
28922 64 20 26 26 20 28 64 62 2d 3e 66 6c 61 67 73 20  d && (db->flags 
28923 26 20 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72  & SQLITE_Recover
28924 79 4d 6f 64 65 29 3d 3d 30 20 29 7b 0a 20 20 20  yMode)==0 ){.   
28925 20 69 66 28 20 7a 4f 62 6a 3d 3d 30 20 29 20 7a   if( zObj==0 ) z
28926 4f 62 6a 20 3d 20 22 3f 22 3b 0a 20 20 20 20 73  Obj = "?";.    s
28927 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
28928 70 44 61 74 61 2d 3e 70 7a 45 72 72 4d 73 67 2c  pData->pzErrMsg,
28929 20 64 62 2c 0a 20 20 20 20 20 20 22 6d 61 6c 66   db,.      "malf
2892a 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65 20 73  ormed database s
2892b 63 68 65 6d 61 20 28 25 73 29 22 2c 20 7a 4f 62  chema (%s)", zOb
2892c 6a 29 3b 0a 20 20 20 20 69 66 28 20 7a 45 78 74  j);.    if( zExt
2892d 72 61 20 29 7b 0a 20 20 20 20 20 20 2a 70 44 61  ra ){.      *pDa
2892e 74 61 2d 3e 70 7a 45 72 72 4d 73 67 20 3d 20 73  ta->pzErrMsg = s
2892f 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64  qlite3MAppendf(d
28930 62 2c 20 2a 70 44 61 74 61 2d 3e 70 7a 45 72 72  b, *pData->pzErr
28931 4d 73 67 2c 20 0a 20 20 20 20 20 20 20 20 20 20  Msg, .          
28932 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28933 20 20 20 20 20 20 20 22 25 73 20 2d 20 25 73 22         "%s - %s"
28934 2c 20 2a 70 44 61 74 61 2d 3e 70 7a 45 72 72 4d  , *pData->pzErrM
28935 73 67 2c 20 7a 45 78 74 72 61 29 3b 0a 20 20 20  sg, zExtra);.   
28936 20 7d 0a 20 20 7d 0a 20 20 70 44 61 74 61 2d 3e   }.  }.  pData->
28937 72 63 20 3d 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rc = db->mallocF
28938 61 69 6c 65 64 20 3f 20 53 51 4c 49 54 45 5f 4e  ailed ? SQLITE_N
28939 4f 4d 45 4d 20 3a 20 53 51 4c 49 54 45 5f 43 4f  OMEM : SQLITE_CO
2893a 52 52 55 50 54 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  RRUPT;.}../*.** 
2893b 54 68 69 73 20 69 73 20 74 68 65 20 63 61 6c 6c  This is the call
2893c 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 66 6f 72  back routine for
2893d 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 69   the code that i
2893e 6e 69 74 69 61 6c 69 7a 65 73 20 74 68 65 0a 2a  nitializes the.*
2893f 2a 20 64 61 74 61 62 61 73 65 2e 20 20 53 65 65  * database.  See
28940 20 73 71 6c 69 74 65 33 49 6e 69 74 28 29 20 62   sqlite3Init() b
28941 65 6c 6f 77 20 66 6f 72 20 61 64 64 69 74 69 6f  elow for additio
28942 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
28943 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
28944 20 69 73 20 61 6c 73 6f 20 63 61 6c 6c 65 64 20   is also called 
28945 66 72 6f 6d 20 74 68 65 20 4f 50 5f 50 61 72 73  from the OP_Pars
28946 65 53 63 68 65 6d 61 20 6f 70 63 6f 64 65 20 6f  eSchema opcode o
28947 66 20 74 68 65 20 56 44 42 45 2e 0a 2a 2a 0a 2a  f the VDBE..**.*
28948 2a 20 45 61 63 68 20 63 61 6c 6c 62 61 63 6b 20  * Each callback 
28949 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 66 6f 6c  contains the fol
2894a 6c 6f 77 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69  lowing informati
2894b 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 72  on:.**.**     ar
2894c 67 76 5b 30 5d 20 3d 20 6e 61 6d 65 20 6f 66 20  gv[0] = name of 
2894d 74 68 69 6e 67 20 62 65 69 6e 67 20 63 72 65 61  thing being crea
2894e 74 65 64 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b  ted.**     argv[
2894f 31 5d 20 3d 20 72 6f 6f 74 20 70 61 67 65 20 6e  1] = root page n
28950 75 6d 62 65 72 20 66 6f 72 20 74 61 62 6c 65 20  umber for table 
28951 6f 72 20 69 6e 64 65 78 2e 20 30 20 66 6f 72 20  or index. 0 for 
28952 74 72 69 67 67 65 72 20 6f 72 20 76 69 65 77 2e  trigger or view.
28953 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b 32 5d 20  .**     argv[2] 
28954 3d 20 53 51 4c 20 74 65 78 74 20 66 6f 72 20 74  = SQL text for t
28955 68 65 20 43 52 45 41 54 45 20 73 74 61 74 65 6d  he CREATE statem
28956 65 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54  ent..**.*/.SQLIT
28957 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
28958 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63  lite3InitCallbac
28959 6b 28 76 6f 69 64 20 2a 70 49 6e 69 74 2c 20 69  k(void *pInit, i
2895a 6e 74 20 61 72 67 63 2c 20 63 68 61 72 20 2a 2a  nt argc, char **
2895b 61 72 67 76 2c 20 63 68 61 72 20 2a 2a 4e 6f 74  argv, char **Not
2895c 55 73 65 64 29 7b 0a 20 20 49 6e 69 74 44 61 74  Used){.  InitDat
2895d 61 20 2a 70 44 61 74 61 20 3d 20 28 49 6e 69 74  a *pData = (Init
2895e 44 61 74 61 2a 29 70 49 6e 69 74 3b 0a 20 20 73  Data*)pInit;.  s
2895f 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 44 61  qlite3 *db = pDa
28960 74 61 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44  ta->db;.  int iD
28961 62 20 3d 20 70 44 61 74 61 2d 3e 69 44 62 3b 0a  b = pData->iDb;.
28962 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
28963 3d 33 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  =3 );.  UNUSED_P
28964 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65  ARAMETER2(NotUse
28965 64 2c 20 61 72 67 63 29 3b 0a 20 20 61 73 73 65  d, argc);.  asse
28966 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
28967 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
28968 29 20 29 3b 0a 20 20 44 62 43 6c 65 61 72 50 72  ) );.  DbClearPr
28969 6f 70 65 72 74 79 28 64 62 2c 20 69 44 62 2c 20  operty(db, iDb, 
2896a 44 42 5f 45 6d 70 74 79 29 3b 0a 20 20 69 66 28  DB_Empty);.  if(
2896b 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2896c 64 20 29 7b 0a 20 20 20 20 63 6f 72 72 75 70 74  d ){.    corrupt
2896d 53 63 68 65 6d 61 28 70 44 61 74 61 2c 20 61 72  Schema(pData, ar
2896e 67 76 5b 30 5d 2c 20 30 29 3b 0a 20 20 20 20 72  gv[0], 0);.    r
2896f 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20  eturn 1;.  }..  
28970 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26  assert( iDb>=0 &
28971 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b  & iDb<db->nDb );
28972 0a 20 20 69 66 28 20 61 72 67 76 3d 3d 30 20 29  .  if( argv==0 )
28973 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
28974 4d 69 67 68 74 20 68 61 70 70 65 6e 20 69 66 20  Might happen if 
28975 45 4d 50 54 59 5f 52 45 53 55 4c 54 5f 43 41 4c  EMPTY_RESULT_CAL
28976 4c 42 41 43 4b 53 20 61 72 65 20 6f 6e 20 2a 2f  LBACKS are on */
28977 0a 20 20 69 66 28 20 61 72 67 76 5b 31 5d 3d 3d  .  if( argv[1]==
28978 30 20 29 7b 0a 20 20 20 20 63 6f 72 72 75 70 74  0 ){.    corrupt
28979 53 63 68 65 6d 61 28 70 44 61 74 61 2c 20 61 72  Schema(pData, ar
2897a 67 76 5b 30 5d 2c 20 30 29 3b 0a 20 20 7d 65 6c  gv[0], 0);.  }el
2897b 73 65 20 69 66 28 20 61 72 67 76 5b 32 5d 20 26  se if( argv[2] &
2897c 26 20 61 72 67 76 5b 32 5d 5b 30 5d 20 29 7b 0a  & argv[2][0] ){.
2897d 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 68 65 20      /* Call the 
2897e 70 61 72 73 65 72 20 74 6f 20 70 72 6f 63 65 73  parser to proces
2897f 73 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45  s a CREATE TABLE
28980 2c 20 49 4e 44 45 58 20 6f 72 20 56 49 45 57 2e  , INDEX or VIEW.
28981 0a 20 20 20 20 2a 2a 20 42 75 74 20 62 65 63 61  .    ** But beca
28982 75 73 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  use db->init.bus
28983 79 20 69 73 20 73 65 74 20 74 6f 20 31 2c 20 6e  y is set to 1, n
28984 6f 20 56 44 42 45 20 63 6f 64 65 20 69 73 20 67  o VDBE code is g
28985 65 6e 65 72 61 74 65 64 0a 20 20 20 20 2a 2a 20  enerated.    ** 
28986 6f 72 20 65 78 65 63 75 74 65 64 2e 20 20 41 6c  or executed.  Al
28987 6c 20 74 68 65 20 70 61 72 73 65 72 20 64 6f 65  l the parser doe
28988 73 20 69 73 20 62 75 69 6c 64 20 74 68 65 20 69  s is build the i
28989 6e 74 65 72 6e 61 6c 20 64 61 74 61 0a 20 20 20  nternal data.   
2898a 20 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20 74   ** structures t
2898b 68 61 74 20 64 65 73 63 72 69 62 65 20 74 68 65  hat describe the
2898c 20 74 61 62 6c 65 2c 20 69 6e 64 65 78 2c 20 6f   table, index, o
2898d 72 20 76 69 65 77 2e 0a 20 20 20 20 2a 2f 0a 20  r view..    */. 
2898e 20 20 20 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20     char *zErr;. 
2898f 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 61     int rc;.    a
28990 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e  ssert( db->init.
28991 62 75 73 79 20 29 3b 0a 20 20 20 20 64 62 2d 3e  busy );.    db->
28992 69 6e 69 74 2e 69 44 62 20 3d 20 69 44 62 3b 0a  init.iDb = iDb;.
28993 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 77      db->init.new
28994 54 6e 75 6d 20 3d 20 61 74 6f 69 28 61 72 67 76  Tnum = atoi(argv
28995 5b 31 5d 29 3b 0a 20 20 20 20 64 62 2d 3e 69 6e  [1]);.    db->in
28996 69 74 2e 6f 72 70 68 61 6e 54 72 69 67 67 65 72  it.orphanTrigger
28997 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 73   = 0;.    rc = s
28998 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20  qlite3_exec(db, 
28999 61 72 67 76 5b 32 5d 2c 20 30 2c 20 30 2c 20 26  argv[2], 0, 0, &
2899a 7a 45 72 72 29 3b 0a 20 20 20 20 64 62 2d 3e 69  zErr);.    db->i
2899b 6e 69 74 2e 69 44 62 20 3d 20 30 3b 0a 20 20 20  nit.iDb = 0;.   
2899c 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
2899d 49 54 45 5f 4f 4b 20 7c 7c 20 7a 45 72 72 3d 3d  ITE_OK || zErr==
2899e 30 20 29 3b 0a 20 20 20 20 69 66 28 20 53 51 4c  0 );.    if( SQL
2899f 49 54 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20 20  ITE_OK!=rc ){.  
289a0 20 20 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74      if( db->init
289a1 2e 6f 72 70 68 61 6e 54 72 69 67 67 65 72 20 29  .orphanTrigger )
289a2 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
289a3 28 20 69 44 62 3d 3d 31 20 29 3b 0a 20 20 20 20  ( iDb==1 );.    
289a4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
289a5 20 70 44 61 74 61 2d 3e 72 63 20 3d 20 72 63 3b   pData->rc = rc;
289a6 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
289a7 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b  =SQLITE_NOMEM ){
289a8 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6d  .          db->m
289a9 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
289aa 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
289ab 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4e  f( rc!=SQLITE_IN
289ac 54 45 52 52 55 50 54 20 26 26 20 72 63 21 3d 53  TERRUPT && rc!=S
289ad 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 29 7b 0a  QLITE_LOCKED ){.
289ae 20 20 20 20 20 20 20 20 20 20 63 6f 72 72 75 70            corrup
289af 74 53 63 68 65 6d 61 28 70 44 61 74 61 2c 20 61  tSchema(pData, a
289b0 72 67 76 5b 30 5d 2c 20 7a 45 72 72 29 3b 0a 20  rgv[0], zErr);. 
289b1 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
289b2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
289b3 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a  Free(db, zErr);.
289b4 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
289b5 28 20 61 72 67 76 5b 30 5d 3d 3d 30 20 29 7b 0a  ( argv[0]==0 ){.
289b6 20 20 20 20 63 6f 72 72 75 70 74 53 63 68 65 6d      corruptSchem
289b7 61 28 70 44 61 74 61 2c 20 30 2c 20 30 29 3b 0a  a(pData, 0, 0);.
289b8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
289b9 49 66 20 74 68 65 20 53 51 4c 20 63 6f 6c 75 6d  If the SQL colum
289ba 6e 20 69 73 20 62 6c 61 6e 6b 20 69 74 20 6d 65  n is blank it me
289bb 61 6e 73 20 74 68 69 73 20 69 73 20 61 6e 20 69  ans this is an i
289bc 6e 64 65 78 20 74 68 61 74 0a 20 20 20 20 2a 2a  ndex that.    **
289bd 20 77 61 73 20 63 72 65 61 74 65 64 20 74 6f 20   was created to 
289be 62 65 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b  be the PRIMARY K
289bf 45 59 20 6f 72 20 74 6f 20 66 75 6c 66 69 6c 6c  EY or to fulfill
289c0 20 61 20 55 4e 49 51 55 45 0a 20 20 20 20 2a 2a   a UNIQUE.    **
289c1 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 6f 72 20   constraint for 
289c2 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 2e 20  a CREATE TABLE. 
289c3 20 54 68 65 20 69 6e 64 65 78 20 73 68 6f 75 6c   The index shoul
289c4 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a 20  d have already. 
289c5 20 20 20 2a 2a 20 62 65 65 6e 20 63 72 65 61 74     ** been creat
289c6 65 64 20 77 68 65 6e 20 77 65 20 70 72 6f 63 65  ed when we proce
289c7 73 73 65 64 20 74 68 65 20 43 52 45 41 54 45 20  ssed the CREATE 
289c8 54 41 42 4c 45 2e 20 20 41 6c 6c 20 77 65 20 68  TABLE.  All we h
289c9 61 76 65 0a 20 20 20 20 2a 2a 20 74 6f 20 64 6f  ave.    ** to do
289ca 20 68 65 72 65 20 69 73 20 72 65 63 6f 72 64 20   here is record 
289cb 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75  the root page nu
289cc 6d 62 65 72 20 66 6f 72 20 74 68 61 74 20 69 6e  mber for that in
289cd 64 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  dex..    */.    
289ce 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20  Index *pIndex;. 
289cf 20 20 20 70 49 6e 64 65 78 20 3d 20 73 71 6c 69     pIndex = sqli
289d0 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c  te3FindIndex(db,
289d1 20 61 72 67 76 5b 30 5d 2c 20 64 62 2d 3e 61 44   argv[0], db->aD
289d2 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  b[iDb].zName);. 
289d3 20 20 20 69 66 28 20 70 49 6e 64 65 78 3d 3d 30     if( pIndex==0
289d4 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69   ){.      /* Thi
289d5 73 20 63 61 6e 20 6f 63 63 75 72 20 69 66 20 74  s can occur if t
289d6 68 65 72 65 20 65 78 69 73 74 73 20 61 6e 20 69  here exists an i
289d7 6e 64 65 78 20 6f 6e 20 61 20 54 45 4d 50 20 74  ndex on a TEMP t
289d8 61 62 6c 65 20 77 68 69 63 68 0a 20 20 20 20 20  able which.     
289d9 20 2a 2a 20 68 61 73 20 74 68 65 20 73 61 6d 65   ** has the same
289da 20 6e 61 6d 65 20 61 73 20 61 6e 6f 74 68 65 72   name as another
289db 20 69 6e 64 65 78 20 6f 6e 20 61 20 70 65 72 6d   index on a perm
289dc 61 6e 65 6e 74 20 69 6e 64 65 78 2e 20 20 53 69  anent index.  Si
289dd 6e 63 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  nce.      ** the
289de 20 70 65 72 6d 61 6e 65 6e 74 20 74 61 62 6c 65   permanent table
289df 20 69 73 20 68 69 64 64 65 6e 20 62 79 20 74 68   is hidden by th
289e0 65 20 54 45 4d 50 20 74 61 62 6c 65 2c 20 77 65  e TEMP table, we
289e1 20 63 61 6e 20 61 6c 73 6f 0a 20 20 20 20 20 20   can also.      
289e2 2a 2a 20 73 61 66 65 6c 79 20 69 67 6e 6f 72 65  ** safely ignore
289e3 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 20 74 68   the index on th
289e4 65 20 70 65 72 6d 61 6e 65 6e 74 20 74 61 62 6c  e permanent tabl
289e5 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  e..      */.    
289e6 20 20 2f 2a 20 44 6f 20 4e 6f 74 68 69 6e 67 20    /* Do Nothing 
289e7 2a 2f 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  */;.    }else if
289e8 28 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33  ( sqlite3GetInt3
289e9 32 28 61 72 67 76 5b 31 5d 2c 20 26 70 49 6e 64  2(argv[1], &pInd
289ea 65 78 2d 3e 74 6e 75 6d 29 3d 3d 30 20 29 7b 0a  ex->tnum)==0 ){.
289eb 20 20 20 20 20 20 63 6f 72 72 75 70 74 53 63 68        corruptSch
289ec 65 6d 61 28 70 44 61 74 61 2c 20 61 72 67 76 5b  ema(pData, argv[
289ed 30 5d 2c 20 22 69 6e 76 61 6c 69 64 20 72 6f 6f  0], "invalid roo
289ee 74 70 61 67 65 22 29 3b 0a 20 20 20 20 7d 0a 20  tpage");.    }. 
289ef 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
289f0 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20  ../*.** Attempt 
289f1 74 6f 20 72 65 61 64 20 74 68 65 20 64 61 74 61  to read the data
289f2 62 61 73 65 20 73 63 68 65 6d 61 20 61 6e 64 20  base schema and 
289f3 69 6e 69 74 69 61 6c 69 7a 65 20 69 6e 74 65 72  initialize inter
289f4 6e 61 6c 0a 2a 2a 20 64 61 74 61 20 73 74 72 75  nal.** data stru
289f5 63 74 75 72 65 73 20 66 6f 72 20 61 20 73 69 6e  ctures for a sin
289f6 67 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  gle database fil
289f7 65 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f 66  e.  The index of
289f8 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
289f9 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e 20 62   file is given b
289fa 79 20 69 44 62 2e 20 20 69 44 62 3d 3d 30 20 69  y iDb.  iDb==0 i
289fb 73 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 6d  s used for the m
289fc 61 69 6e 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e  ain.** database.
289fd 20 20 69 44 62 3d 3d 31 20 73 68 6f 75 6c 64 20    iDb==1 should 
289fe 6e 65 76 65 72 20 62 65 20 75 73 65 64 2e 20 20  never be used.  
289ff 69 44 62 3e 3d 32 20 69 73 20 75 73 65 64 20 66  iDb>=2 is used f
28a00 6f 72 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20  or.** auxiliary 
28a01 64 61 74 61 62 61 73 65 73 2e 20 20 52 65 74 75  databases.  Retu
28a02 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 53 51  rn one of the SQ
28a03 4c 49 54 45 5f 20 65 72 72 6f 72 20 63 6f 64 65  LITE_ error code
28a04 73 20 74 6f 0a 2a 2a 20 69 6e 64 69 63 61 74 65  s to.** indicate
28a05 20 73 75 63 63 65 73 73 20 6f 72 20 66 61 69 6c   success or fail
28a06 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ure..*/.static i
28a07 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 4f 6e  nt sqlite3InitOn
28a08 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  e(sqlite3 *db, i
28a09 6e 74 20 69 44 62 2c 20 63 68 61 72 20 2a 2a 70  nt iDb, char **p
28a0a 7a 45 72 72 4d 73 67 29 7b 0a 20 20 69 6e 74 20  zErrMsg){.  int 
28a0b 72 63 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  rc;.  int i;.  i
28a0c 6e 74 20 73 69 7a 65 3b 0a 20 20 54 61 62 6c 65  nt size;.  Table
28a0d 20 2a 70 54 61 62 3b 0a 20 20 44 62 20 2a 70 44   *pTab;.  Db *pD
28a0e 62 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20  b;.  char const 
28a0f 2a 61 7a 41 72 67 5b 34 5d 3b 0a 20 20 69 6e 74  *azArg[4];.  int
28a10 20 6d 65 74 61 5b 35 5d 3b 0a 20 20 49 6e 69 74   meta[5];.  Init
28a11 44 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0a 20  Data initData;. 
28a12 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61   char const *zMa
28a13 73 74 65 72 53 63 68 65 6d 61 3b 0a 20 20 63 68  sterSchema;.  ch
28a14 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61 73 74 65  ar const *zMaste
28a15 72 4e 61 6d 65 20 3d 20 53 43 48 45 4d 41 5f 54  rName = SCHEMA_T
28a16 41 42 4c 45 28 69 44 62 29 3b 0a 20 20 69 6e 74  ABLE(iDb);.  int
28a17 20 6f 70 65 6e 65 64 54 72 61 6e 73 61 63 74 69   openedTransacti
28a18 6f 6e 20 3d 20 30 3b 0a 0a 20 20 2f 2a 0a 20 20  on = 0;..  /*.  
28a19 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 64 61  ** The master da
28a1a 74 61 62 61 73 65 20 74 61 62 6c 65 20 68 61 73  tabase table has
28a1b 20 61 20 73 74 72 75 63 74 75 72 65 20 6c 69 6b   a structure lik
28a1c 65 20 74 68 69 73 0a 20 20 2a 2f 0a 20 20 73 74  e this.  */.  st
28a1d 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
28a1e 6d 61 73 74 65 72 5f 73 63 68 65 6d 61 5b 5d 20  master_schema[] 
28a1f 3d 20 0a 20 20 20 20 20 22 43 52 45 41 54 45 20  = .     "CREATE 
28a20 54 41 42 4c 45 20 73 71 6c 69 74 65 5f 6d 61 73  TABLE sqlite_mas
28a21 74 65 72 28 5c 6e 22 0a 20 20 20 20 20 22 20 20  ter(\n".     "  
28a22 74 79 70 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20  type text,\n".  
28a23 20 20 20 22 20 20 6e 61 6d 65 20 74 65 78 74 2c     "  name text,
28a24 5c 6e 22 0a 20 20 20 20 20 22 20 20 74 62 6c 5f  \n".     "  tbl_
28a25 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20  name text,\n".  
28a26 20 20 20 22 20 20 72 6f 6f 74 70 61 67 65 20 69     "  rootpage i
28a27 6e 74 65 67 65 72 2c 5c 6e 22 0a 20 20 20 20 20  nteger,\n".     
28a28 22 20 20 73 71 6c 20 74 65 78 74 5c 6e 22 0a 20  "  sql text\n". 
28a29 20 20 20 20 22 29 22 0a 20 20 3b 0a 23 69 66 6e      ")".  ;.#ifn
28a2a 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
28a2b 54 45 4d 50 44 42 0a 20 20 73 74 61 74 69 63 20  TEMPDB.  static 
28a2c 63 6f 6e 73 74 20 63 68 61 72 20 74 65 6d 70 5f  const char temp_
28a2d 6d 61 73 74 65 72 5f 73 63 68 65 6d 61 5b 5d 20  master_schema[] 
28a2e 3d 20 0a 20 20 20 20 20 22 43 52 45 41 54 45 20  = .     "CREATE 
28a2f 54 45 4d 50 20 54 41 42 4c 45 20 73 71 6c 69 74  TEMP TABLE sqlit
28a30 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 28 5c 6e  e_temp_master(\n
28a31 22 0a 20 20 20 20 20 22 20 20 74 79 70 65 20 74  ".     "  type t
28a32 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20  ext,\n".     "  
28a33 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20  name text,\n".  
28a34 20 20 20 22 20 20 74 62 6c 5f 6e 61 6d 65 20 74     "  tbl_name t
28a35 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20  ext,\n".     "  
28a36 72 6f 6f 74 70 61 67 65 20 69 6e 74 65 67 65 72  rootpage integer
28a37 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 73 71 6c  ,\n".     "  sql
28a38 20 74 65 78 74 5c 6e 22 0a 20 20 20 20 20 22 29   text\n".     ")
28a39 22 0a 20 20 3b 0a 23 65 6c 73 65 0a 20 20 23 64  ".  ;.#else.  #d
28a3a 65 66 69 6e 65 20 74 65 6d 70 5f 6d 61 73 74 65  efine temp_maste
28a3b 72 5f 73 63 68 65 6d 61 20 30 0a 23 65 6e 64 69  r_schema 0.#endi
28a3c 66 0a 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  f..  assert( iDb
28a3d 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e  >=0 && iDb<db->n
28a3e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Db );.  assert( 
28a3f 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
28a40 68 65 6d 61 20 29 3b 0a 20 20 61 73 73 65 72 74  hema );.  assert
28a41 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
28a42 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
28a43 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  );.  assert( iDb
28a44 3d 3d 31 20 7c 7c 20 73 71 6c 69 74 65 33 42 74  ==1 || sqlite3Bt
28a45 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 64 62  reeHoldsMutex(db
28a46 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 29 20  ->aDb[iDb].pBt) 
28a47 29 3b 0a 0a 20 20 2f 2a 20 7a 4d 61 73 74 65 72  );..  /* zMaster
28a48 53 63 68 65 6d 61 20 61 6e 64 20 7a 49 6e 69 74  Schema and zInit
28a49 53 63 72 69 70 74 20 61 72 65 20 73 65 74 20 74  Script are set t
28a4a 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 6d  o point at the m
28a4b 61 73 74 65 72 20 73 63 68 65 6d 61 0a 20 20 2a  aster schema.  *
28a4c 2a 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 73 61  * and initialisa
28a4d 74 69 6f 6e 20 73 63 72 69 70 74 20 61 70 70 72  tion script appr
28a4e 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68 65 20  opriate for the 
28a4f 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 0a 20  database being. 
28a50 20 2a 2a 20 69 6e 69 74 69 61 6c 69 73 65 64 2e   ** initialised.
28a51 20 7a 4d 61 73 74 65 72 4e 61 6d 65 20 69 73 20   zMasterName is 
28a52 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
28a53 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 0a 20 20  master table..  
28a54 2a 2f 0a 20 20 69 66 28 20 21 4f 4d 49 54 5f 54  */.  if( !OMIT_T
28a55 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20  EMPDB && iDb==1 
28a56 29 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72 53 63  ){.    zMasterSc
28a57 68 65 6d 61 20 3d 20 74 65 6d 70 5f 6d 61 73 74  hema = temp_mast
28a58 65 72 5f 73 63 68 65 6d 61 3b 0a 20 20 7d 65 6c  er_schema;.  }el
28a59 73 65 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72 53  se{.    zMasterS
28a5a 63 68 65 6d 61 20 3d 20 6d 61 73 74 65 72 5f 73  chema = master_s
28a5b 63 68 65 6d 61 3b 0a 20 20 7d 0a 20 20 7a 4d 61  chema;.  }.  zMa
28a5c 73 74 65 72 4e 61 6d 65 20 3d 20 53 43 48 45 4d  sterName = SCHEM
28a5d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 0a 20  A_TABLE(iDb);.. 
28a5e 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20 74 68   /* Construct th
28a5f 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65 73 2e  e schema tables.
28a60 20 20 2a 2f 0a 20 20 61 7a 41 72 67 5b 30 5d 20    */.  azArg[0] 
28a61 3d 20 7a 4d 61 73 74 65 72 4e 61 6d 65 3b 0a 20  = zMasterName;. 
28a62 20 61 7a 41 72 67 5b 31 5d 20 3d 20 22 31 22 3b   azArg[1] = "1";
28a63 0a 20 20 61 7a 41 72 67 5b 32 5d 20 3d 20 7a 4d  .  azArg[2] = zM
28a64 61 73 74 65 72 53 63 68 65 6d 61 3b 0a 20 20 61  asterSchema;.  a
28a65 7a 41 72 67 5b 33 5d 20 3d 20 30 3b 0a 20 20 69  zArg[3] = 0;.  i
28a66 6e 69 74 44 61 74 61 2e 64 62 20 3d 20 64 62 3b  nitData.db = db;
28a67 0a 20 20 69 6e 69 74 44 61 74 61 2e 69 44 62 20  .  initData.iDb 
28a68 3d 20 69 44 62 3b 0a 20 20 69 6e 69 74 44 61 74  = iDb;.  initDat
28a69 61 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  a.rc = SQLITE_OK
28a6a 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 70 7a 45  ;.  initData.pzE
28a6b 72 72 4d 73 67 20 3d 20 70 7a 45 72 72 4d 73 67  rrMsg = pzErrMsg
28a6c 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65  ;.  (void)sqlite
28a6d 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
28a6e 20 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61 6c    sqlite3InitCal
28a6f 6c 62 61 63 6b 28 26 69 6e 69 74 44 61 74 61 2c  lback(&initData,
28a70 20 33 2c 20 28 63 68 61 72 20 2a 2a 29 61 7a 41   3, (char **)azA
28a71 72 67 2c 20 30 29 3b 0a 20 20 28 76 6f 69 64 29  rg, 0);.  (void)
28a72 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
28a73 64 62 29 3b 0a 20 20 69 66 28 20 69 6e 69 74 44  db);.  if( initD
28a74 61 74 61 2e 72 63 20 29 7b 0a 20 20 20 20 72 63  ata.rc ){.    rc
28a75 20 3d 20 69 6e 69 74 44 61 74 61 2e 72 63 3b 0a   = initData.rc;.
28a76 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f      goto error_o
28a77 75 74 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d  ut;.  }.  pTab =
28a78 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
28a79 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 4e 61 6d  e(db, zMasterNam
28a7a 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  e, db->aDb[iDb].
28a7b 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 41 4c  zName);.  if( AL
28a7c 57 41 59 53 28 70 54 61 62 29 20 29 7b 0a 20 20  WAYS(pTab) ){.  
28a7d 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73    pTab->tabFlags
28a7e 20 7c 3d 20 54 46 5f 52 65 61 64 6f 6e 6c 79 3b   |= TF_Readonly;
28a7f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74  .  }..  /* Creat
28a80 65 20 61 20 63 75 72 73 6f 72 20 74 6f 20 68 6f  e a cursor to ho
28a81 6c 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ld the database 
28a82 6f 70 65 6e 0a 20 20 2a 2f 0a 20 20 70 44 62 20  open.  */.  pDb 
28a83 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b  = &db->aDb[iDb];
28a84 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74 3d  .  if( pDb->pBt=
28a85 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21 4f  =0 ){.    if( !O
28a86 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 41 4c  MIT_TEMPDB && AL
28a87 57 41 59 53 28 69 44 62 3d 3d 31 29 20 29 7b 0a  WAYS(iDb==1) ){.
28a88 20 20 20 20 20 20 44 62 53 65 74 50 72 6f 70 65        DbSetPrope
28a89 72 74 79 28 64 62 2c 20 31 2c 20 44 42 5f 53 63  rty(db, 1, DB_Sc
28a8a 68 65 6d 61 4c 6f 61 64 65 64 29 3b 0a 20 20 20  hemaLoaded);.   
28a8b 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51   }.    return SQ
28a8c 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
28a8d 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  /* If there is n
28a8e 6f 74 20 61 6c 72 65 61 64 79 20 61 20 72 65 61  ot already a rea
28a8f 64 2d 6f 6e 6c 79 20 28 6f 72 20 72 65 61 64 2d  d-only (or read-
28a90 77 72 69 74 65 29 20 74 72 61 6e 73 61 63 74 69  write) transacti
28a91 6f 6e 20 6f 70 65 6e 65 64 0a 20 20 2a 2a 20 6f  on opened.  ** o
28a92 6e 20 74 68 65 20 62 2d 74 72 65 65 20 64 61 74  n the b-tree dat
28a93 61 62 61 73 65 2c 20 6f 70 65 6e 20 6f 6e 65 20  abase, open one 
28a94 6e 6f 77 2e 20 49 66 20 61 20 74 72 61 6e 73 61  now. If a transa
28a95 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 2c  ction is opened,
28a96 20 69 74 20 0a 20 20 2a 2a 20 77 69 6c 6c 20 62   it .  ** will b
28a97 65 20 63 6c 6f 73 65 64 20 62 65 66 6f 72 65 20  e closed before 
28a98 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  this function re
28a99 74 75 72 6e 73 2e 20 20 2a 2f 0a 20 20 73 71 6c  turns.  */.  sql
28a9a 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
28a9b 44 62 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28 20  Db->pBt);.  if( 
28a9c 21 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49  !sqlite3BtreeIsI
28a9d 6e 52 65 61 64 54 72 61 6e 73 28 70 44 62 2d 3e  nReadTrans(pDb->
28a9e 70 42 74 29 20 29 7b 0a 20 20 20 20 72 63 20 3d  pBt) ){.    rc =
28a9f 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67   sqlite3BtreeBeg
28aa0 69 6e 54 72 61 6e 73 28 70 44 62 2d 3e 70 42 74  inTrans(pDb->pBt
28aa1 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63  , 0);.    if( rc
28aa2 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
28aa3 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53       sqlite3SetS
28aa4 74 72 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20  tring(pzErrMsg, 
28aa5 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65  db, "%s", sqlite
28aa6 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20  3ErrStr(rc));.  
28aa7 20 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65      goto initone
28aa8 5f 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 20 20  _error_out;.    
28aa9 7d 0a 20 20 20 20 6f 70 65 6e 65 64 54 72 61 6e  }.    openedTran
28aaa 73 61 63 74 69 6f 6e 20 3d 20 31 3b 0a 20 20 7d  saction = 1;.  }
28aab 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20 64  ..  /* Get the d
28aac 61 74 61 62 61 73 65 20 6d 65 74 61 20 69 6e 66  atabase meta inf
28aad 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 2a 2a 0a 20  ormation..  **. 
28aae 20 2a 2a 20 4d 65 74 61 20 76 61 6c 75 65 73 20   ** Meta values 
28aaf 61 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  are as follows:.
28ab0 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 30 5d 20    **    meta[0] 
28ab1 20 20 53 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e    Schema cookie.
28ab2 20 20 43 68 61 6e 67 65 73 20 77 69 74 68 20 65    Changes with e
28ab3 61 63 68 20 73 63 68 65 6d 61 20 63 68 61 6e 67  ach schema chang
28ab4 65 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b  e..  **    meta[
28ab5 31 5d 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74  1]   File format
28ab6 20 6f 66 20 73 63 68 65 6d 61 20 6c 61 79 65 72   of schema layer
28ab7 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 32  ..  **    meta[2
28ab8 5d 20 20 20 53 69 7a 65 20 6f 66 20 74 68 65 20  ]   Size of the 
28ab9 70 61 67 65 20 63 61 63 68 65 2e 0a 20 20 2a 2a  page cache..  **
28aba 20 20 20 20 6d 65 74 61 5b 33 5d 20 20 20 4c 61      meta[3]   La
28abb 72 67 65 73 74 20 72 6f 6f 74 70 61 67 65 20 28  rgest rootpage (
28abc 61 75 74 6f 2f 69 6e 63 72 5f 76 61 63 75 75 6d  auto/incr_vacuum
28abd 20 6d 6f 64 65 29 0a 20 20 2a 2a 20 20 20 20 6d   mode).  **    m
28abe 65 74 61 5b 34 5d 20 20 20 44 62 20 74 65 78 74  eta[4]   Db text
28abf 20 65 6e 63 6f 64 69 6e 67 2e 20 31 3a 55 54 46   encoding. 1:UTF
28ac0 2d 38 20 32 3a 55 54 46 2d 31 36 4c 45 20 33 3a  -8 2:UTF-16LE 3:
28ac1 55 54 46 2d 31 36 42 45 0a 20 20 2a 2a 20 20 20  UTF-16BE.  **   
28ac2 20 6d 65 74 61 5b 35 5d 20 20 20 55 73 65 72 20   meta[5]   User 
28ac3 76 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 20 20 20  version.  **    
28ac4 6d 65 74 61 5b 36 5d 20 20 20 49 6e 63 72 65 6d  meta[6]   Increm
28ac5 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 6d 6f 64  ental vacuum mod
28ac6 65 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 37  e.  **    meta[7
28ac7 5d 20 20 20 75 6e 75 73 65 64 0a 20 20 2a 2a 20  ]   unused.  ** 
28ac8 20 20 20 6d 65 74 61 5b 38 5d 20 20 20 75 6e 75     meta[8]   unu
28ac9 73 65 64 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61  sed.  **    meta
28aca 5b 39 5d 20 20 20 75 6e 75 73 65 64 0a 20 20 2a  [9]   unused.  *
28acb 2a 0a 20 20 2a 2a 20 4e 6f 74 65 3a 20 54 68 65  *.  ** Note: The
28acc 20 23 64 65 66 69 6e 65 64 20 53 51 4c 49 54 45   #defined SQLITE
28acd 5f 55 54 46 2a 20 73 79 6d 62 6f 6c 73 20 69 6e  _UTF* symbols in
28ace 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 63 6f 72   sqliteInt.h cor
28acf 72 65 73 70 6f 6e 64 20 74 6f 0a 20 20 2a 2a 20  respond to.  ** 
28ad0 74 68 65 20 70 6f 73 73 69 62 6c 65 20 76 61 6c  the possible val
28ad1 75 65 73 20 6f 66 20 6d 65 74 61 5b 34 5d 2e 0a  ues of meta[4]..
28ad2 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20    */.  for(i=0; 
28ad3 69 3c 41 72 72 61 79 53 69 7a 65 28 6d 65 74 61  i<ArraySize(meta
28ad4 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  ); i++){.    sql
28ad5 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61  ite3BtreeGetMeta
28ad6 28 70 44 62 2d 3e 70 42 74 2c 20 69 2b 31 2c 20  (pDb->pBt, i+1, 
28ad7 28 75 33 32 20 2a 29 26 6d 65 74 61 5b 69 5d 29  (u32 *)&meta[i])
28ad8 3b 0a 20 20 7d 0a 20 20 70 44 62 2d 3e 70 53 63  ;.  }.  pDb->pSc
28ad9 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f  hema->schema_coo
28ada 6b 69 65 20 3d 20 6d 65 74 61 5b 42 54 52 45 45  kie = meta[BTREE
28adb 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2d  _SCHEMA_VERSION-
28adc 31 5d 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f 70 65  1];..  /* If ope
28add 6e 69 6e 67 20 61 20 6e 6f 6e 2d 65 6d 70 74 79  ning a non-empty
28ade 20 64 61 74 61 62 61 73 65 2c 20 63 68 65 63 6b   database, check
28adf 20 74 68 65 20 74 65 78 74 20 65 6e 63 6f 64 69   the text encodi
28ae0 6e 67 2e 20 46 6f 72 20 74 68 65 0a 20 20 2a 2a  ng. For the.  **
28ae1 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20   main database, 
28ae2 73 65 74 20 73 71 6c 69 74 65 33 2e 65 6e 63 20  set sqlite3.enc 
28ae3 74 6f 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  to the encoding 
28ae4 6f 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  of the main data
28ae5 62 61 73 65 2e 0a 20 20 2a 2a 20 46 6f 72 20 61  base..  ** For a
28ae6 6e 20 61 74 74 61 63 68 65 64 20 64 62 2c 20 69  n attached db, i
28ae7 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 69 66  t is an error if
28ae8 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69 73   the encoding is
28ae9 20 6e 6f 74 20 74 68 65 20 73 61 6d 65 0a 20 20   not the same.  
28aea 2a 2a 20 61 73 20 73 71 6c 69 74 65 33 2e 65 6e  ** as sqlite3.en
28aeb 63 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6d 65  c..  */.  if( me
28aec 74 61 5b 42 54 52 45 45 5f 54 45 58 54 5f 45 4e  ta[BTREE_TEXT_EN
28aed 43 4f 44 49 4e 47 2d 31 5d 20 29 7b 20 20 2f 2a  CODING-1] ){  /*
28aee 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a   text encoding *
28aef 2f 0a 20 20 20 20 69 66 28 20 69 44 62 3d 3d 30  /.    if( iDb==0
28af0 20 29 7b 0a 20 20 20 20 20 20 75 38 20 65 6e 63   ){.      u8 enc
28af1 6f 64 69 6e 67 3b 0a 20 20 20 20 20 20 2f 2a 20  oding;.      /* 
28af2 49 66 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 6d  If opening the m
28af3 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20 73 65  ain database, se
28af4 74 20 45 4e 43 28 64 62 29 2e 20 2a 2f 0a 20 20  t ENC(db). */.  
28af5 20 20 20 20 65 6e 63 6f 64 69 6e 67 20 3d 20 28      encoding = (
28af6 75 38 29 6d 65 74 61 5b 42 54 52 45 45 5f 54 45  u8)meta[BTREE_TE
28af7 58 54 5f 45 4e 43 4f 44 49 4e 47 2d 31 5d 20 26  XT_ENCODING-1] &
28af8 20 33 3b 0a 20 20 20 20 20 20 69 66 28 20 65 6e   3;.      if( en
28af9 63 6f 64 69 6e 67 3d 3d 30 20 29 20 65 6e 63 6f  coding==0 ) enco
28afa 64 69 6e 67 20 3d 20 53 51 4c 49 54 45 5f 55 54  ding = SQLITE_UT
28afb 46 38 3b 0a 20 20 20 20 20 20 45 4e 43 28 64 62  F8;.      ENC(db
28afc 29 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20  ) = encoding;.  
28afd 20 20 20 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c      db->pDfltCol
28afe 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43  l = sqlite3FindC
28aff 6f 6c 6c 53 65 71 28 64 62 2c 20 53 51 4c 49 54  ollSeq(db, SQLIT
28b00 45 5f 55 54 46 38 2c 20 22 42 49 4e 41 52 59 22  E_UTF8, "BINARY"
28b01 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  , 0);.    }else{
28b02 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 6f 70 65  .      /* If ope
28b03 6e 69 6e 67 20 61 6e 20 61 74 74 61 63 68 65 64  ning an attached
28b04 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 65   database, the e
28b05 6e 63 6f 64 69 6e 67 20 6d 75 63 68 20 6d 61 74  ncoding much mat
28b06 63 68 20 45 4e 43 28 64 62 29 20 2a 2f 0a 20 20  ch ENC(db) */.  
28b07 20 20 20 20 69 66 28 20 6d 65 74 61 5b 42 54 52      if( meta[BTR
28b08 45 45 5f 54 45 58 54 5f 45 4e 43 4f 44 49 4e 47  EE_TEXT_ENCODING
28b09 2d 31 5d 21 3d 45 4e 43 28 64 62 29 20 29 7b 0a  -1]!=ENC(db) ){.
28b0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
28b0b 65 74 53 74 72 69 6e 67 28 70 7a 45 72 72 4d 73  etString(pzErrMs
28b0c 67 2c 20 64 62 2c 20 22 61 74 74 61 63 68 65 64  g, db, "attached
28b0d 20 64 61 74 61 62 61 73 65 73 20 6d 75 73 74 20   databases must 
28b0e 75 73 65 20 74 68 65 20 73 61 6d 65 22 0a 20 20  use the same".  
28b0f 20 20 20 20 20 20 20 20 20 20 22 20 74 65 78 74            " text
28b10 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 6d 61 69   encoding as mai
28b11 6e 20 64 61 74 61 62 61 73 65 22 29 3b 0a 20 20  n database");.  
28b12 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
28b13 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20  E_ERROR;.       
28b14 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65 5f 65 72   goto initone_er
28b15 72 6f 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d  ror_out;.      }
28b16 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
28b17 20 20 20 20 44 62 53 65 74 50 72 6f 70 65 72 74      DbSetPropert
28b18 79 28 64 62 2c 20 69 44 62 2c 20 44 42 5f 45 6d  y(db, iDb, DB_Em
28b19 70 74 79 29 3b 0a 20 20 7d 0a 20 20 70 44 62 2d  pty);.  }.  pDb-
28b1a 3e 70 53 63 68 65 6d 61 2d 3e 65 6e 63 20 3d 20  >pSchema->enc = 
28b1b 45 4e 43 28 64 62 29 3b 0a 0a 20 20 69 66 28 20  ENC(db);..  if( 
28b1c 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61  pDb->pSchema->ca
28b1d 63 68 65 5f 73 69 7a 65 3d 3d 30 20 29 7b 0a 20  che_size==0 ){. 
28b1e 20 20 20 73 69 7a 65 20 3d 20 6d 65 74 61 5b 42     size = meta[B
28b1f 54 52 45 45 5f 44 45 46 41 55 4c 54 5f 43 41 43  TREE_DEFAULT_CAC
28b20 48 45 5f 53 49 5a 45 2d 31 5d 3b 0a 20 20 20 20  HE_SIZE-1];.    
28b21 69 66 28 20 73 69 7a 65 3d 3d 30 20 29 7b 20 73  if( size==0 ){ s
28b22 69 7a 65 20 3d 20 53 51 4c 49 54 45 5f 44 45 46  ize = SQLITE_DEF
28b23 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 3b  AULT_CACHE_SIZE;
28b24 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 3c   }.    if( size<
28b25 30 20 29 20 73 69 7a 65 20 3d 20 2d 73 69 7a 65  0 ) size = -size
28b26 3b 0a 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65  ;.    pDb->pSche
28b27 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 20 3d  ma->cache_size =
28b28 20 73 69 7a 65 3b 0a 20 20 20 20 73 71 6c 69 74   size;.    sqlit
28b29 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53  e3BtreeSetCacheS
28b2a 69 7a 65 28 70 44 62 2d 3e 70 42 74 2c 20 70 44  ize(pDb->pBt, pD
28b2b 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68  b->pSchema->cach
28b2c 65 5f 73 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20  e_size);.  }..  
28b2d 2f 2a 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72  /*.  ** file_for
28b2e 6d 61 74 3d 3d 31 20 20 20 20 56 65 72 73 69 6f  mat==1    Versio
28b2f 6e 20 33 2e 30 2e 30 2e 0a 20 20 2a 2a 20 66 69  n 3.0.0..  ** fi
28b30 6c 65 5f 66 6f 72 6d 61 74 3d 3d 32 20 20 20 20  le_format==2    
28b31 56 65 72 73 69 6f 6e 20 33 2e 31 2e 33 2e 20 20  Version 3.1.3.  
28b32 2f 2f 20 41 4c 54 45 52 20 54 41 42 4c 45 20 41  // ALTER TABLE A
28b33 44 44 20 43 4f 4c 55 4d 4e 0a 20 20 2a 2a 20 66  DD COLUMN.  ** f
28b34 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 33 20 20 20  ile_format==3   
28b35 20 56 65 72 73 69 6f 6e 20 33 2e 31 2e 34 2e 20   Version 3.1.4. 
28b36 20 2f 2f 20 64 69 74 74 6f 20 62 75 74 20 77 69   // ditto but wi
28b37 74 68 20 6e 6f 6e 2d 4e 55 4c 4c 20 64 65 66 61  th non-NULL defa
28b38 75 6c 74 73 0a 20 20 2a 2a 20 66 69 6c 65 5f 66  ults.  ** file_f
28b39 6f 72 6d 61 74 3d 3d 34 20 20 20 20 56 65 72 73  ormat==4    Vers
28b3a 69 6f 6e 20 33 2e 33 2e 30 2e 20 20 2f 2f 20 44  ion 3.3.0.  // D
28b3b 45 53 43 20 69 6e 64 69 63 65 73 2e 20 20 42 6f  ESC indices.  Bo
28b3c 6f 6c 65 61 6e 20 63 6f 6e 73 74 61 6e 74 73 0a  olean constants.
28b3d 20 20 2a 2f 0a 20 20 70 44 62 2d 3e 70 53 63 68    */.  pDb->pSch
28b3e 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74  ema->file_format
28b3f 20 3d 20 28 75 38 29 6d 65 74 61 5b 42 54 52 45   = (u8)meta[BTRE
28b40 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 2d 31 5d  E_FILE_FORMAT-1]
28b41 3b 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63  ;.  if( pDb->pSc
28b42 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61  hema->file_forma
28b43 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44 62 2d  t==0 ){.    pDb-
28b44 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66  >pSchema->file_f
28b45 6f 72 6d 61 74 20 3d 20 31 3b 0a 20 20 7d 0a 20  ormat = 1;.  }. 
28b46 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d   if( pDb->pSchem
28b47 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3e 53  a->file_format>S
28b48 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c 45 5f 46  QLITE_MAX_FILE_F
28b49 4f 52 4d 41 54 20 29 7b 0a 20 20 20 20 73 71 6c  ORMAT ){.    sql
28b4a 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 70 7a  ite3SetString(pz
28b4b 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 75 6e 73  ErrMsg, db, "uns
28b4c 75 70 70 6f 72 74 65 64 20 66 69 6c 65 20 66 6f  upported file fo
28b4d 72 6d 61 74 22 29 3b 0a 20 20 20 20 72 63 20 3d  rmat");.    rc =
28b4e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
28b4f 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65 5f     goto initone_
28b50 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  error_out;.  }..
28b51 20 20 2f 2a 20 54 69 63 6b 65 74 20 23 32 38 30    /* Ticket #280
28b52 34 3a 20 20 57 68 65 6e 20 77 65 20 6f 70 65 6e  4:  When we open
28b53 20 61 20 64 61 74 61 62 61 73 65 20 69 6e 20 74   a database in t
28b54 68 65 20 6e 65 77 65 72 20 66 69 6c 65 20 66 6f  he newer file fo
28b55 72 6d 61 74 2c 0a 20 20 2a 2a 20 63 6c 65 61 72  rmat,.  ** clear
28b56 20 74 68 65 20 6c 65 67 61 63 79 5f 66 69 6c 65   the legacy_file
28b57 5f 66 6f 72 6d 61 74 20 70 72 61 67 6d 61 20 66  _format pragma f
28b58 6c 61 67 20 73 6f 20 74 68 61 74 20 61 20 56 41  lag so that a VA
28b59 43 55 55 4d 20 77 69 6c 6c 0a 20 20 2a 2a 20 6e  CUUM will.  ** n
28b5a 6f 74 20 64 6f 77 6e 67 72 61 64 65 20 74 68 65  ot downgrade the
28b5b 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 68   database and th
28b5c 75 73 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e  us invalidate an
28b5d 79 20 64 65 73 63 65 6e 64 69 6e 67 0a 20 20 2a  y descending.  *
28b5e 2a 20 69 6e 64 69 63 65 73 20 74 68 61 74 20 74  * indices that t
28b5f 68 65 20 75 73 65 72 20 6d 69 67 68 74 20 68 61  he user might ha
28b60 76 65 20 63 72 65 61 74 65 64 2e 0a 20 20 2a 2f  ve created..  */
28b61 0a 20 20 69 66 28 20 69 44 62 3d 3d 30 20 26 26  .  if( iDb==0 &&
28b62 20 6d 65 74 61 5b 42 54 52 45 45 5f 46 49 4c 45   meta[BTREE_FILE
28b63 5f 46 4f 52 4d 41 54 2d 31 5d 3e 3d 34 20 29 7b  _FORMAT-1]>=4 ){
28b64 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 26  .    db->flags &
28b65 3d 20 7e 53 51 4c 49 54 45 5f 4c 65 67 61 63 79  = ~SQLITE_Legacy
28b66 46 69 6c 65 46 6d 74 3b 0a 20 20 7d 0a 0a 20 20  FileFmt;.  }..  
28b67 2f 2a 20 52 65 61 64 20 74 68 65 20 73 63 68 65  /* Read the sche
28b68 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f  ma information o
28b69 75 74 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61  ut of the schema
28b6a 20 74 61 62 6c 65 73 0a 20 20 2a 2f 0a 20 20 61   tables.  */.  a
28b6b 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e  ssert( db->init.
28b6c 62 75 73 79 20 29 3b 0a 20 20 7b 0a 20 20 20 20  busy );.  {.    
28b6d 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 20 20  char *zSql;.    
28b6e 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50  zSql = sqlite3MP
28b6f 72 69 6e 74 66 28 64 62 2c 20 0a 20 20 20 20 20  rintf(db, .     
28b70 20 20 20 22 53 45 4c 45 43 54 20 6e 61 6d 65 2c     "SELECT name,
28b71 20 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c 20 46   rootpage, sql F
28b72 52 4f 4d 20 27 25 71 27 2e 25 73 22 2c 0a 20 20  ROM '%q'.%s",.  
28b73 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44        db->aDb[iD
28b74 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 4d 61 73 74 65  b].zName, zMaste
28b75 72 4e 61 6d 65 29 3b 0a 20 20 20 20 28 76 6f 69  rName);.    (voi
28b76 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
28b77 66 66 28 64 62 29 3b 0a 23 69 66 6e 64 65 66 20  ff(db);.#ifndef 
28b78 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
28b79 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 7b 0a  ORIZATION.    {.
28b7a 20 20 20 20 20 20 69 6e 74 20 28 2a 78 41 75 74        int (*xAut
28b7b 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  h)(void*,int,con
28b7c 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
28b7d 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
28b7e 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20  ,const char*);. 
28b7f 20 20 20 20 20 78 41 75 74 68 20 3d 20 64 62 2d       xAuth = db-
28b80 3e 78 41 75 74 68 3b 0a 20 20 20 20 20 20 64 62  >xAuth;.      db
28b81 2d 3e 78 41 75 74 68 20 3d 20 30 3b 0a 23 65 6e  ->xAuth = 0;.#en
28b82 64 69 66 0a 20 20 20 20 20 20 72 63 20 3d 20 73  dif.      rc = s
28b83 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20  qlite3_exec(db, 
28b84 7a 53 71 6c 2c 20 73 71 6c 69 74 65 33 49 6e 69  zSql, sqlite3Ini
28b85 74 43 61 6c 6c 62 61 63 6b 2c 20 26 69 6e 69 74  tCallback, &init
28b86 44 61 74 61 2c 20 30 29 3b 0a 23 69 66 6e 64 65  Data, 0);.#ifnde
28b87 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
28b88 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20  THORIZATION.    
28b89 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 78 41    db->xAuth = xA
28b8a 75 74 68 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  uth;.    }.#endi
28b8b 66 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  f.    if( rc==SQ
28b8c 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 69  LITE_OK ) rc = i
28b8d 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20 20 20 20  nitData.rc;.    
28b8e 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
28b8f 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 73  etyOn(db);.    s
28b90 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
28b91 20 7a 53 71 6c 29 3b 0a 23 69 66 6e 64 65 66 20   zSql);.#ifndef 
28b92 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c  SQLITE_OMIT_ANAL
28b93 59 5a 45 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  YZE.    if( rc==
28b94 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
28b95 20 20 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 73     sqlite3Analys
28b96 69 73 4c 6f 61 64 28 64 62 2c 20 69 44 62 29 3b  isLoad(db, iDb);
28b97 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
28b98 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  }.  if( db->mall
28b99 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
28b9a 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
28b9b 4d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65  M;.    sqlite3Re
28b9c 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d  setInternalSchem
28b9d 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  a(db, 0);.  }.  
28b9e 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
28b9f 4b 20 7c 7c 20 28 64 62 2d 3e 66 6c 61 67 73 26  K || (db->flags&
28ba0 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d  SQLITE_RecoveryM
28ba1 6f 64 65 29 29 7b 0a 20 20 20 20 2f 2a 20 42 6c  ode)){.    /* Bl
28ba2 61 63 6b 20 6d 61 67 69 63 3a 20 49 66 20 74 68  ack magic: If th
28ba3 65 20 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72  e SQLITE_Recover
28ba4 79 4d 6f 64 65 20 66 6c 61 67 20 69 73 20 73 65  yMode flag is se
28ba5 74 2c 20 74 68 65 6e 20 63 6f 6e 73 69 64 65 72  t, then consider
28ba6 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 63 68 65  .    ** the sche
28ba7 6d 61 20 6c 6f 61 64 65 64 2c 20 65 76 65 6e 20  ma loaded, even 
28ba8 69 66 20 65 72 72 6f 72 73 20 6f 63 63 75 72 72  if errors occurr
28ba9 65 64 2e 20 49 6e 20 74 68 69 73 20 73 69 74 75  ed. In this situ
28baa 61 74 69 6f 6e 20 74 68 65 20 0a 20 20 20 20 2a  ation the .    *
28bab 2a 20 63 75 72 72 65 6e 74 20 73 71 6c 69 74 65  * current sqlite
28bac 33 5f 70 72 65 70 61 72 65 28 29 20 6f 70 65 72  3_prepare() oper
28bad 61 74 69 6f 6e 20 77 69 6c 6c 20 66 61 69 6c 2c  ation will fail,
28bae 20 62 75 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69   but the followi
28baf 6e 67 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 77 69  ng one.    ** wi
28bb0 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f  ll attempt to co
28bb1 6d 70 69 6c 65 20 74 68 65 20 73 75 70 70 6c 69  mpile the suppli
28bb2 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61 67 61  ed statement aga
28bb3 69 6e 73 74 20 77 68 61 74 65 76 65 72 20 73 75  inst whatever su
28bb4 62 73 65 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74  bset.    ** of t
28bb5 68 65 20 73 63 68 65 6d 61 20 77 61 73 20 6c 6f  he schema was lo
28bb6 61 64 65 64 20 62 65 66 6f 72 65 20 74 68 65 20  aded before the 
28bb7 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2e 20  error occurred. 
28bb8 54 68 65 20 70 72 69 6d 61 72 79 0a 20 20 20 20  The primary.    
28bb9 2a 2a 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68  ** purpose of th
28bba 69 73 20 69 73 20 74 6f 20 61 6c 6c 6f 77 20 61  is is to allow a
28bbb 63 63 65 73 73 20 74 6f 20 74 68 65 20 73 71 6c  ccess to the sql
28bbc 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
28bbd 0a 20 20 20 20 2a 2a 20 65 76 65 6e 20 77 68 65  .    ** even whe
28bbe 6e 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 20 68  n its contents h
28bbf 61 76 65 20 62 65 65 6e 20 63 6f 72 72 75 70 74  ave been corrupt
28bc0 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 44  ed..    */.    D
28bc1 62 53 65 74 50 72 6f 70 65 72 74 79 28 64 62 2c  bSetProperty(db,
28bc2 20 69 44 62 2c 20 44 42 5f 53 63 68 65 6d 61 4c   iDb, DB_SchemaL
28bc3 6f 61 64 65 64 29 3b 0a 20 20 20 20 72 63 20 3d  oaded);.    rc =
28bc4 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
28bc5 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20  .  /* Jump here 
28bc6 66 6f 72 20 61 6e 20 65 72 72 6f 72 20 74 68 61  for an error tha
28bc7 74 20 6f 63 63 75 72 73 20 61 66 74 65 72 20 73  t occurs after s
28bc8 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6c 6c 6f  uccessfully allo
28bc9 63 61 74 69 6e 67 0a 20 20 2a 2a 20 63 75 72 4d  cating.  ** curM
28bca 61 69 6e 20 61 6e 64 20 63 61 6c 6c 69 6e 67 20  ain and calling 
28bcb 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
28bcc 72 28 29 2e 20 46 6f 72 20 61 6e 20 65 72 72 6f  r(). For an erro
28bcd 72 20 74 68 61 74 20 6f 63 63 75 72 73 0a 20 20  r that occurs.  
28bce 2a 2a 20 62 65 66 6f 72 65 20 74 68 61 74 20 70  ** before that p
28bcf 6f 69 6e 74 2c 20 6a 75 6d 70 20 74 6f 20 65 72  oint, jump to er
28bd0 72 6f 72 5f 6f 75 74 2e 0a 20 20 2a 2f 0a 69 6e  ror_out..  */.in
28bd1 69 74 6f 6e 65 5f 65 72 72 6f 72 5f 6f 75 74 3a  itone_error_out:
28bd2 0a 20 20 69 66 28 20 6f 70 65 6e 65 64 54 72 61  .  if( openedTra
28bd3 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20  nsaction ){.    
28bd4 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
28bd5 69 74 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20  it(pDb->pBt);.  
28bd6 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  }.  sqlite3Btree
28bd7 4c 65 61 76 65 28 70 44 62 2d 3e 70 42 74 29 3b  Leave(pDb->pBt);
28bd8 0a 0a 65 72 72 6f 72 5f 6f 75 74 3a 0a 20 20 69  ..error_out:.  i
28bd9 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
28bda 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  MEM || rc==SQLIT
28bdb 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b  E_IOERR_NOMEM ){
28bdc 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  .    db->mallocF
28bdd 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20  ailed = 1;.  }. 
28bde 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
28bdf 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
28be0 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c  all database fil
28be1 65 73 20 2d 20 74 68 65 20 6d 61 69 6e 20 64 61  es - the main da
28be2 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65  tabase file, the
28be3 20 66 69 6c 65 0a 2a 2a 20 75 73 65 64 20 74 6f   file.** used to
28be4 20 73 74 6f 72 65 20 74 65 6d 70 6f 72 61 72 79   store temporary
28be5 20 74 61 62 6c 65 73 2c 20 61 6e 64 20 61 6e 79   tables, and any
28be6 20 61 64 64 69 74 69 6f 6e 61 6c 20 64 61 74 61   additional data
28be7 62 61 73 65 20 66 69 6c 65 73 0a 2a 2a 20 63 72  base files.** cr
28be8 65 61 74 65 64 20 75 73 69 6e 67 20 41 54 54 41  eated using ATTA
28be9 43 48 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20  CH statements.  
28bea 52 65 74 75 72 6e 20 61 20 73 75 63 63 65 73 73  Return a success
28beb 20 63 6f 64 65 2e 20 20 49 66 20 61 6e 0a 2a 2a   code.  If an.**
28bec 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 77   error occurs, w
28bed 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65  rite an error me
28bee 73 73 61 67 65 20 69 6e 74 6f 20 2a 70 7a 45 72  ssage into *pzEr
28bef 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65  rMsg..**.** Afte
28bf0 72 20 61 20 64 61 74 61 62 61 73 65 20 69 73 20  r a database is 
28bf1 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68 65  initialized, the
28bf2 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64   DB_SchemaLoaded
28bf3 20 62 69 74 20 69 73 20 73 65 74 0a 2a 2a 20 62   bit is set.** b
28bf4 69 74 20 69 73 20 73 65 74 20 69 6e 20 74 68 65  it is set in the
28bf5 20 66 6c 61 67 73 20 66 69 65 6c 64 20 6f 66 20   flags field of 
28bf6 74 68 65 20 44 62 20 73 74 72 75 63 74 75 72 65  the Db structure
28bf7 2e 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  . If the databas
28bf8 65 0a 2a 2a 20 66 69 6c 65 20 77 61 73 20 6f 66  e.** file was of
28bf9 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 2c 20 74 68   zero-length, th
28bfa 65 6e 20 74 68 65 20 44 42 5f 45 6d 70 74 79 20  en the DB_Empty 
28bfb 66 6c 61 67 20 69 73 20 61 6c 73 6f 20 73 65 74  flag is also set
28bfc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
28bfd 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
28bfe 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  nit(sqlite3 *db,
28bff 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67   char **pzErrMsg
28c00 29 7b 0a 20 20 69 6e 74 20 69 2c 20 72 63 3b 0a  ){.  int i, rc;.
28c01 20 20 69 6e 74 20 63 6f 6d 6d 69 74 5f 69 6e 74    int commit_int
28c02 65 72 6e 61 6c 20 3d 20 21 28 64 62 2d 3e 66 6c  ernal = !(db->fl
28c03 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72  ags&SQLITE_Inter
28c04 6e 43 68 61 6e 67 65 73 29 3b 0a 20 20 0a 20 20  nChanges);.  .  
28c05 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
28c06 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d  mutex_held(db->m
28c07 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20  utex) );.  rc = 
28c08 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 64 62 2d  SQLITE_OK;.  db-
28c09 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 31 3b 0a  >init.busy = 1;.
28c0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53    for(i=0; rc==S
28c0b 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62  QLITE_OK && i<db
28c0c 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
28c0d 20 69 66 28 20 44 62 48 61 73 50 72 6f 70 65 72   if( DbHasProper
28c0e 74 79 28 64 62 2c 20 69 2c 20 44 42 5f 53 63 68  ty(db, i, DB_Sch
28c0f 65 6d 61 4c 6f 61 64 65 64 29 20 7c 7c 20 69 3d  emaLoaded) || i=
28c10 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =1 ) continue;. 
28c11 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49     rc = sqlite3I
28c12 6e 69 74 4f 6e 65 28 64 62 2c 20 69 2c 20 70 7a  nitOne(db, i, pz
28c13 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66 28  ErrMsg);.    if(
28c14 20 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c   rc ){.      sql
28c15 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
28c16 6c 53 63 68 65 6d 61 28 64 62 2c 20 69 29 3b 0a  lSchema(db, i);.
28c17 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
28c18 4f 6e 63 65 20 61 6c 6c 20 74 68 65 20 6f 74 68  Once all the oth
28c19 65 72 20 64 61 74 61 62 61 73 65 73 20 68 61 76  er databases hav
28c1a 65 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 73  e been initialis
28c1b 65 64 2c 20 6c 6f 61 64 20 74 68 65 20 73 63 68  ed, load the sch
28c1c 65 6d 61 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65  ema.  ** for the
28c1d 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 2e 20   TEMP database. 
28c1e 54 68 69 73 20 69 73 20 6c 6f 61 64 65 64 20 6c  This is loaded l
28c1f 61 73 74 2c 20 61 73 20 74 68 65 20 54 45 4d 50  ast, as the TEMP
28c20 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 73   database.  ** s
28c21 63 68 65 6d 61 20 6d 61 79 20 63 6f 6e 74 61 69  chema may contai
28c22 6e 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  n references to 
28c23 6f 62 6a 65 63 74 73 20 69 6e 20 6f 74 68 65 72  objects in other
28c24 20 64 61 74 61 62 61 73 65 73 2e 0a 20 20 2a 2f   databases..  */
28c25 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
28c26 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 20 20 69 66  OMIT_TEMPDB.  if
28c27 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
28c28 26 26 20 41 4c 57 41 59 53 28 64 62 2d 3e 6e 44  && ALWAYS(db->nD
28c29 62 3e 31 29 0a 20 20 20 20 20 20 20 20 20 20 20  b>1).           
28c2a 20 20 20 20 20 20 20 20 20 26 26 20 21 44 62 48           && !DbH
28c2b 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20 31  asProperty(db, 1
28c2c 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65  , DB_SchemaLoade
28c2d 64 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  d) ){.    rc = s
28c2e 71 6c 69 74 65 33 49 6e 69 74 4f 6e 65 28 64 62  qlite3InitOne(db
28c2f 2c 20 31 2c 20 70 7a 45 72 72 4d 73 67 29 3b 0a  , 1, pzErrMsg);.
28c30 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
28c31 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74      sqlite3Reset
28c32 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64  InternalSchema(d
28c33 62 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  b, 1);.    }.  }
28c34 0a 23 65 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 69  .#endif..  db->i
28c35 6e 69 74 2e 62 75 73 79 20 3d 20 30 3b 0a 20 20  nit.busy = 0;.  
28c36 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
28c37 4b 20 26 26 20 63 6f 6d 6d 69 74 5f 69 6e 74 65  K && commit_inte
28c38 72 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69  rnal ){.    sqli
28c39 74 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61  te3CommitInterna
28c3a 6c 43 68 61 6e 67 65 73 28 64 62 29 3b 0a 20 20  lChanges(db);.  
28c3b 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 20  }..  return rc; 
28c3c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
28c3d 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
28c3e 70 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73  p if the databas
28c3f 65 20 73 63 68 65 6d 61 20 69 73 20 61 6c 72 65  e schema is alre
28c40 61 64 79 20 69 6e 69 74 69 61 6c 69 73 65 64 2e  ady initialised.
28c41 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74  .** Otherwise, t
28c42 68 65 20 73 63 68 65 6d 61 20 69 73 20 6c 6f 61  he schema is loa
28c43 64 65 64 2e 20 41 6e 20 65 72 72 6f 72 20 63 6f  ded. An error co
28c44 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  de is returned..
28c45 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
28c46 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 61  E int sqlite3Rea
28c47 64 53 63 68 65 6d 61 28 50 61 72 73 65 20 2a 70  dSchema(Parse *p
28c48 50 61 72 73 65 29 7b 0a 20 20 69 6e 74 20 72 63  Parse){.  int rc
28c49 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
28c4a 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
28c4b 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65  arse->db;.  asse
28c4c 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
28c4d 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
28c4e 29 20 29 3b 0a 20 20 69 66 28 20 21 64 62 2d 3e  ) );.  if( !db->
28c4f 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20  init.busy ){.   
28c50 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49 6e 69   rc = sqlite3Ini
28c51 74 28 64 62 2c 20 26 70 50 61 72 73 65 2d 3e 7a  t(db, &pParse->z
28c52 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 20 20 69  ErrMsg);.  }.  i
28c53 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
28c54 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   ){.    pParse->
28c55 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 70 50 61  rc = rc;.    pPa
28c56 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 7d  rse->nErr++;.  }
28c57 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
28c58 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 73 63  ../*.** Check sc
28c59 68 65 6d 61 20 63 6f 6f 6b 69 65 73 20 69 6e 20  hema cookies in 
28c5a 61 6c 6c 20 64 61 74 61 62 61 73 65 73 2e 20 20  all databases.  
28c5b 49 66 20 61 6e 79 20 63 6f 6f 6b 69 65 20 69 73  If any cookie is
28c5c 20 6f 75 74 0a 2a 2a 20 6f 66 20 64 61 74 65 20   out.** of date 
28c5d 73 65 74 20 70 50 61 72 73 65 2d 3e 72 63 20 74  set pParse->rc t
28c5e 6f 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 2e  o SQLITE_SCHEMA.
28c5f 20 20 49 66 20 61 6c 6c 20 73 63 68 65 6d 61 20    If all schema 
28c60 63 6f 6f 6b 69 65 73 0a 2a 2a 20 6d 61 6b 65 20  cookies.** make 
28c61 6e 6f 20 63 68 61 6e 67 65 73 20 74 6f 20 70 50  no changes to pP
28c62 61 72 73 65 2d 3e 72 63 2e 0a 2a 2f 0a 73 74 61  arse->rc..*/.sta
28c63 74 69 63 20 76 6f 69 64 20 73 63 68 65 6d 61 49  tic void schemaI
28c64 73 56 61 6c 69 64 28 50 61 72 73 65 20 2a 70 50  sValid(Parse *pP
28c65 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33  arse){.  sqlite3
28c66 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
28c67 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20  b;.  int iDb;.  
28c68 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 63 6f  int rc;.  int co
28c69 6f 6b 69 65 3b 0a 0a 20 20 61 73 73 65 72 74 28  okie;..  assert(
28c6a 20 70 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63   pParse->checkSc
28c6b 68 65 6d 61 20 29 3b 0a 20 20 61 73 73 65 72 74  hema );.  assert
28c6c 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
28c6d 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
28c6e 29 3b 0a 20 20 66 6f 72 28 69 44 62 3d 30 3b 20  );.  for(iDb=0; 
28c6f 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 69 44 62  iDb<db->nDb; iDb
28c70 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6f 70 65  ++){.    int ope
28c71 6e 65 64 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d  nedTransaction =
28c72 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54   0;         /* T
28c73 72 75 65 20 69 66 20 61 20 74 72 61 6e 73 61 63  rue if a transac
28c74 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 20 2a  tion is opened *
28c75 2f 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42 74  /.    Btree *pBt
28c76 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
28c77 70 42 74 3b 20 20 20 20 20 2f 2a 20 42 74 72 65  pBt;     /* Btre
28c78 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 72 65  e database to re
28c79 61 64 20 63 6f 6f 6b 69 65 20 66 72 6f 6d 20 2a  ad cookie from *
28c7a 2f 0a 20 20 20 20 69 66 28 20 70 42 74 3d 3d 30  /.    if( pBt==0
28c7b 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20   ) continue;..  
28c7c 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
28c7d 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 61 20 72   not already a r
28c7e 65 61 64 2d 6f 6e 6c 79 20 28 6f 72 20 72 65 61  ead-only (or rea
28c7f 64 2d 77 72 69 74 65 29 20 74 72 61 6e 73 61 63  d-write) transac
28c80 74 69 6f 6e 20 6f 70 65 6e 65 64 0a 20 20 20 20  tion opened.    
28c81 2a 2a 20 6f 6e 20 74 68 65 20 62 2d 74 72 65 65  ** on the b-tree
28c82 20 64 61 74 61 62 61 73 65 2c 20 6f 70 65 6e 20   database, open 
28c83 6f 6e 65 20 6e 6f 77 2e 20 49 66 20 61 20 74 72  one now. If a tr
28c84 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65  ansaction is ope
28c85 6e 65 64 2c 20 69 74 20 0a 20 20 20 20 2a 2a 20  ned, it .    ** 
28c86 77 69 6c 6c 20 62 65 20 63 6c 6f 73 65 64 20 69  will be closed i
28c87 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 72  mmediately after
28c88 20 72 65 61 64 69 6e 67 20 74 68 65 20 6d 65 74   reading the met
28c89 61 2d 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20  a-value. */.    
28c8a 69 66 28 20 21 73 71 6c 69 74 65 33 42 74 72 65  if( !sqlite3Btre
28c8b 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28 70  eIsInReadTrans(p
28c8c 42 74 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20  Bt) ){.      rc 
28c8d 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65  = sqlite3BtreeBe
28c8e 67 69 6e 54 72 61 6e 73 28 70 42 74 2c 20 30 29  ginTrans(pBt, 0)
28c8f 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
28c90 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20  SQLITE_NOMEM || 
28c91 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc==SQLITE_IOERR
28c92 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20  _NOMEM ){.      
28c93 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
28c94 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  ed = 1;.      }.
28c95 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
28c96 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
28c97 3b 0a 20 20 20 20 20 20 6f 70 65 6e 65 64 54 72  ;.      openedTr
28c98 61 6e 73 61 63 74 69 6f 6e 20 3d 20 31 3b 0a 20  ansaction = 1;. 
28c99 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 61     }..    /* Rea
28c9a 64 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f  d the schema coo
28c9b 6b 69 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74  kie from the dat
28c9c 61 62 61 73 65 2e 20 49 66 20 69 74 20 64 6f 65  abase. If it doe
28c9d 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20  s not match the 
28c9e 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 73 74  .    ** value st
28c9f 6f 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  ored as part of 
28ca0 74 68 65 20 69 6e 20 74 68 65 20 69 6e 2d 6d 65  the in the in-me
28ca1 6d 6f 72 79 20 73 63 68 65 6d 61 20 72 65 70 72  mory schema repr
28ca2 65 73 65 6e 74 61 74 69 6f 6e 2c 0a 20 20 20 20  esentation,.    
28ca3 2a 2a 20 73 65 74 20 50 61 72 73 65 2e 72 63 20  ** set Parse.rc 
28ca4 74 6f 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  to SQLITE_SCHEMA
28ca5 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  . */.    sqlite3
28ca6 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 42 74  BtreeGetMeta(pBt
28ca7 2c 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f 56  , BTREE_SCHEMA_V
28ca8 45 52 53 49 4f 4e 2c 20 28 75 33 32 20 2a 29 26  ERSION, (u32 *)&
28ca9 63 6f 6f 6b 69 65 29 3b 0a 20 20 20 20 69 66 28  cookie);.    if(
28caa 20 63 6f 6f 6b 69 65 21 3d 64 62 2d 3e 61 44 62   cookie!=db->aDb
28cab 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 73  [iDb].pSchema->s
28cac 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 29 7b 0a  chema_cookie ){.
28cad 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63        pParse->rc
28cae 20 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41   = SQLITE_SCHEMA
28caf 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
28cb0 43 6c 6f 73 65 20 74 68 65 20 74 72 61 6e 73 61  Close the transa
28cb1 63 74 69 6f 6e 2c 20 69 66 20 6f 6e 65 20 77 61  ction, if one wa
28cb2 73 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20 20 20  s opened. */.   
28cb3 20 69 66 28 20 6f 70 65 6e 65 64 54 72 61 6e 73   if( openedTrans
28cb4 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20  action ){.      
28cb5 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
28cb6 69 74 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a 20  it(pBt);.    }. 
28cb7 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76   }.}../*.** Conv
28cb8 65 72 74 20 61 20 73 63 68 65 6d 61 20 70 6f 69  ert a schema poi
28cb9 6e 74 65 72 20 69 6e 74 6f 20 74 68 65 20 69 44  nter into the iD
28cba 62 20 69 6e 64 65 78 20 74 68 61 74 20 69 6e 64  b index that ind
28cbb 69 63 61 74 65 73 0a 2a 2a 20 77 68 69 63 68 20  icates.** which 
28cbc 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e  database file in
28cbd 20 64 62 2d 3e 61 44 62 5b 5d 20 74 68 65 20 73   db->aDb[] the s
28cbe 63 68 65 6d 61 20 72 65 66 65 72 73 20 74 6f 2e  chema refers to.
28cbf 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 61  .**.** If the sa
28cc0 6d 65 20 64 61 74 61 62 61 73 65 20 69 73 20 61  me database is a
28cc1 74 74 61 63 68 65 64 20 6d 6f 72 65 20 74 68 61  ttached more tha
28cc2 6e 20 6f 6e 63 65 2c 20 74 68 65 20 66 69 72 73  n once, the firs
28cc3 74 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 64 61  t.** attached da
28cc4 74 61 62 61 73 65 20 69 73 20 72 65 74 75 72 6e  tabase is return
28cc5 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
28cc6 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
28cc7 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 73  3SchemaToIndex(s
28cc8 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 63 68 65  qlite3 *db, Sche
28cc9 6d 61 20 2a 70 53 63 68 65 6d 61 29 7b 0a 20 20  ma *pSchema){.  
28cca 69 6e 74 20 69 20 3d 20 2d 31 30 30 30 30 30 30  int i = -1000000
28ccb 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 53 63 68 65  ;..  /* If pSche
28ccc 6d 61 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e  ma is NULL, then
28ccd 20 72 65 74 75 72 6e 20 2d 31 30 30 30 30 30 30   return -1000000
28cce 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77  . This happens w
28ccf 68 65 6e 20 63 6f 64 65 20 69 6e 20 0a 20 20 2a  hen code in .  *
28cd0 2a 20 65 78 70 72 2e 63 20 69 73 20 74 72 79 69  * expr.c is tryi
28cd1 6e 67 20 74 6f 20 72 65 73 6f 6c 76 65 20 61 20  ng to resolve a 
28cd2 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 74  reference to a t
28cd3 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 20 28  ransient table (
28cd4 69 2e 65 2e 20 6f 6e 65 0a 20 20 2a 2a 20 63 72  i.e. one.  ** cr
28cd5 65 61 74 65 64 20 62 79 20 61 20 73 75 62 2d 73  eated by a sub-s
28cd6 65 6c 65 63 74 29 2e 20 49 6e 20 74 68 69 73 20  elect). In this 
28cd7 63 61 73 65 20 74 68 65 20 72 65 74 75 72 6e 20  case the return 
28cd8 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 0a 20  value of this . 
28cd9 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f   ** function sho
28cda 75 6c 64 20 6e 65 76 65 72 20 62 65 20 75 73 65  uld never be use
28cdb 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 65 20  d..  **.  ** We 
28cdc 72 65 74 75 72 6e 20 2d 31 30 30 30 30 30 30 20  return -1000000 
28cdd 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 6d  instead of the m
28cde 6f 72 65 20 75 73 75 61 6c 20 2d 31 20 73 69 6d  ore usual -1 sim
28cdf 70 6c 79 20 62 65 63 61 75 73 65 20 75 73 69 6e  ply because usin
28ce0 67 0a 20 20 2a 2a 20 2d 31 30 30 30 30 30 30 20  g.  ** -1000000 
28ce1 61 73 20 74 68 65 20 69 6e 63 6f 72 72 65 63 74  as the incorrect
28ce2 20 69 6e 64 65 78 20 69 6e 74 6f 20 64 62 2d 3e   index into db->
28ce3 61 44 62 5b 5d 20 69 73 20 6d 75 63 68 20 0a 20  aDb[] is much . 
28ce4 20 2a 2a 20 6d 6f 72 65 20 6c 69 6b 65 6c 79 20   ** more likely 
28ce5 74 6f 20 63 61 75 73 65 20 61 20 73 65 67 66 61  to cause a segfa
28ce6 75 6c 74 20 74 68 61 6e 20 2d 31 20 28 6f 66 20  ult than -1 (of 
28ce7 63 6f 75 72 73 65 20 74 68 65 72 65 20 61 72 65  course there are
28ce8 20 61 73 73 65 72 74 28 29 0a 20 20 2a 2a 20 73   assert().  ** s
28ce9 74 61 74 65 6d 65 6e 74 73 20 74 6f 6f 2c 20 62  tatements too, b
28cea 75 74 20 69 74 20 6e 65 76 65 72 20 68 75 72 74  ut it never hurt
28ceb 73 20 74 6f 20 70 6c 61 79 20 74 68 65 20 6f 64  s to play the od
28cec 64 73 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ds)..  */.  asse
28ced 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
28cee 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
28cef 29 20 29 3b 0a 20 20 69 66 28 20 70 53 63 68 65  ) );.  if( pSche
28cf0 6d 61 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  ma ){.    for(i=
28cf1 30 3b 20 41 4c 57 41 59 53 28 69 3c 64 62 2d 3e  0; ALWAYS(i<db->
28cf2 6e 44 62 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nDb); i++){.    
28cf3 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d    if( db->aDb[i]
28cf4 2e 70 53 63 68 65 6d 61 3d 3d 70 53 63 68 65 6d  .pSchema==pSchem
28cf5 61 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65  a ){.        bre
28cf6 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
28cf7 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3e  }.    assert( i>
28cf8 3d 30 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 20  =0 && i<db->nDb 
28cf9 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
28cfa 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  i;.}../*.** Comp
28cfb 69 6c 65 20 74 68 65 20 55 54 46 2d 38 20 65 6e  ile the UTF-8 en
28cfc 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d  coded SQL statem
28cfd 65 6e 74 20 7a 53 71 6c 20 69 6e 74 6f 20 61 20  ent zSql into a 
28cfe 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65  statement handle
28cff 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
28d00 73 71 6c 69 74 65 33 50 72 65 70 61 72 65 28 0a  sqlite3Prepare(.
28d01 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
28d02 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
28d03 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20  atabase handle. 
28d04 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
28d05 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f  *zSql,         /
28d06 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  * UTF-8 encoded 
28d07 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a  SQL statement. *
28d08 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20  /.  int nBytes, 
28d09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
28d0a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20   Length of zSql 
28d0b 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 69  in bytes. */.  i
28d0c 6e 74 20 73 61 76 65 53 71 6c 46 6c 61 67 2c 20  nt saveSqlFlag, 
28d0d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
28d0e 20 74 6f 20 63 6f 70 79 20 53 51 4c 20 74 65 78   to copy SQL tex
28d0f 74 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74  t into the sqlit
28d10 65 33 5f 73 74 6d 74 20 2a 2f 0a 20 20 73 71 6c  e3_stmt */.  sql
28d11 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74  ite3_stmt **ppSt
28d12 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41  mt,    /* OUT: A
28d13 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
28d14 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
28d15 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  nt */.  const ch
28d16 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20  ar **pzTail     
28d17 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66    /* OUT: End of
28d18 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a   parsed string *
28d19 2f 0a 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50  /.){.  Parse *pP
28d1a 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 20  arse;           
28d1b 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
28d1c 65 78 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ext */.  char *z
28d1d 45 72 72 4d 73 67 20 3d 20 30 3b 20 20 20 20 20  ErrMsg = 0;     
28d1e 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73     /* Error mess
28d1f 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20  age */.  int rc 
28d20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
28d21 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64     /* Result cod
28d22 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  e */.  int i;   
28d23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28d24 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
28d25 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61   */..  /* Alloca
28d26 74 65 20 74 68 65 20 70 61 72 73 69 6e 67 20 63  te the parsing c
28d27 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 70 50 61 72  ontext */.  pPar
28d28 73 65 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63  se = sqlite3Stac
28d29 6b 41 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  kAllocZero(db, s
28d2a 69 7a 65 6f 66 28 2a 70 50 61 72 73 65 29 29 3b  izeof(*pParse));
28d2b 0a 20 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30  .  if( pParse==0
28d2c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
28d2d 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67  ITE_NOMEM;.    g
28d2e 6f 74 6f 20 65 6e 64 5f 70 72 65 70 61 72 65 3b  oto end_prepare;
28d2f 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 71 6c 69  .  }..  if( sqli
28d30 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20  te3SafetyOn(db) 
28d31 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
28d32 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 20 20 67  TE_MISUSE;.    g
28d33 6f 74 6f 20 65 6e 64 5f 70 72 65 70 61 72 65 3b  oto end_prepare;
28d34 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
28d35 70 53 74 6d 74 20 26 26 20 2a 70 70 53 74 6d 74  pStmt && *ppStmt
28d36 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
28d37 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
28d38 65 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ed );.  assert( 
28d39 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
28d3a 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
28d3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20  ..  /* Check to 
28d3c 76 65 72 69 66 79 20 74 68 61 74 20 69 74 20 69  verify that it i
28d3d 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 67 65  s possible to ge
28d3e 74 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e  t a read lock on
28d3f 20 61 6c 6c 0a 20 20 2a 2a 20 64 61 74 61 62 61   all.  ** databa
28d40 73 65 20 73 63 68 65 6d 61 73 2e 20 20 54 68 65  se schemas.  The
28d41 20 69 6e 61 62 69 6c 69 74 79 20 74 6f 20 67 65   inability to ge
28d42 74 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 69 6e  t a read lock in
28d43 64 69 63 61 74 65 73 20 74 68 61 74 0a 20 20 2a  dicates that.  *
28d44 2a 20 73 6f 6d 65 20 6f 74 68 65 72 20 64 61 74  * some other dat
28d45 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
28d46 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 77 72   is holding a wr
28d47 69 74 65 2d 6c 6f 63 6b 2c 20 77 68 69 63 68 20  ite-lock, which 
28d48 69 6e 0a 20 20 2a 2a 20 74 75 72 6e 20 6d 65 61  in.  ** turn mea
28d49 6e 73 20 74 68 61 74 20 74 68 65 20 6f 74 68 65  ns that the othe
28d4a 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73  r connection has
28d4b 20 6d 61 64 65 20 75 6e 63 6f 6d 6d 69 74 74 65   made uncommitte
28d4c 64 20 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20 74  d changes.  ** t
28d4d 6f 20 74 68 65 20 73 63 68 65 6d 61 2e 0a 20 20  o the schema..  
28d4e 2a 2a 0a 20 20 2a 2a 20 57 65 72 65 20 77 65 20  **.  ** Were we 
28d4f 74 6f 20 70 72 6f 63 65 65 64 20 61 6e 64 20 70  to proceed and p
28d50 72 65 70 61 72 65 20 74 68 65 20 73 74 61 74 65  repare the state
28d51 6d 65 6e 74 20 61 67 61 69 6e 73 74 20 74 68 65  ment against the
28d52 20 75 6e 63 6f 6d 6d 69 74 74 65 64 0a 20 20 2a   uncommitted.  *
28d53 2a 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73  * schema changes
28d54 20 61 6e 64 20 69 66 20 74 68 6f 73 65 20 73 63   and if those sc
28d55 68 65 6d 61 20 63 68 61 6e 67 65 73 20 61 72 65  hema changes are
28d56 20 73 75 62 73 65 71 75 65 6e 74 6c 79 20 72 6f   subsequently ro
28d57 6c 6c 65 64 0a 20 20 2a 2a 20 62 61 63 6b 20 61  lled.  ** back a
28d58 6e 64 20 64 69 66 66 65 72 65 6e 74 20 63 68 61  nd different cha
28d59 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20 69 6e  nges are made in
28d5a 20 74 68 65 69 72 20 70 6c 61 63 65 2c 20 74 68   their place, th
28d5b 65 6e 20 77 68 65 6e 20 74 68 69 73 0a 20 20 2a  en when this.  *
28d5c 2a 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  * prepared state
28d5d 6d 65 6e 74 20 67 6f 65 73 20 74 6f 20 72 75 6e  ment goes to run
28d5e 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b   the schema cook
28d5f 69 65 20 77 6f 75 6c 64 20 66 61 69 6c 20 74 6f  ie would fail to
28d60 20 64 65 74 65 63 74 0a 20 20 2a 2a 20 74 68 65   detect.  ** the
28d61 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 2e 20   schema change. 
28d62 20 44 69 73 61 73 74 65 72 20 77 6f 75 6c 64 20   Disaster would 
28d63 66 6f 6c 6c 6f 77 2e 0a 20 20 2a 2a 0a 20 20 2a  follow..  **.  *
28d64 2a 20 54 68 69 73 20 74 68 72 65 61 64 20 69 73  * This thread is
28d65 20 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 69   currently holdi
28d66 6e 67 20 6d 75 74 65 78 65 73 20 6f 6e 20 61 6c  ng mutexes on al
28d67 6c 20 42 74 72 65 65 73 20 28 62 65 63 61 75 73  l Btrees (becaus
28d68 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 73 71  e.  ** of the sq
28d69 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41  lite3BtreeEnterA
28d6a 6c 6c 28 29 20 69 6e 20 73 71 6c 69 74 65 33 4c  ll() in sqlite3L
28d6b 6f 63 6b 41 6e 64 50 72 65 70 61 72 65 28 29 29  ockAndPrepare())
28d6c 20 73 6f 20 69 74 0a 20 20 2a 2a 20 69 73 20 6e   so it.  ** is n
28d6d 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20  ot possible for 
28d6e 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 74  another thread t
28d6f 6f 20 73 74 61 72 74 20 61 20 6e 65 77 20 73 63  o start a new sc
28d70 68 65 6d 61 20 63 68 61 6e 67 65 0a 20 20 2a 2a  hema change.  **
28d71 20 77 68 69 6c 65 20 74 68 69 73 20 72 6f 75 74   while this rout
28d72 69 6e 65 20 69 73 20 72 75 6e 6e 69 6e 67 2e 20  ine is running. 
28d73 20 48 65 6e 63 65 2c 20 77 65 20 64 6f 20 6e 6f   Hence, we do no
28d74 74 20 6e 65 65 64 20 74 6f 20 68 6f 6c 64 20 0a  t need to hold .
28d75 20 20 2a 2a 20 6c 6f 63 6b 73 20 6f 6e 20 74 68    ** locks on th
28d76 65 20 73 63 68 65 6d 61 2c 20 77 65 20 6a 75 73  e schema, we jus
28d77 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 73  t need to make s
28d78 75 72 65 20 6e 6f 62 6f 64 79 20 65 6c 73 65 20  ure nobody else 
28d79 69 73 20 0a 20 20 2a 2a 20 68 6f 6c 64 69 6e 67  is .  ** holding
28d7a 20 74 68 65 6d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a   them..  **.  **
28d7b 20 4e 6f 74 65 20 74 68 61 74 20 73 65 74 74 69   Note that setti
28d7c 6e 67 20 52 45 41 44 5f 55 4e 43 4f 4d 4d 49 54  ng READ_UNCOMMIT
28d7d 54 45 44 20 6f 76 65 72 72 69 64 65 73 20 6d 6f  TED overrides mo
28d7e 73 74 20 6c 6f 63 6b 20 64 65 74 65 63 74 69 6f  st lock detectio
28d7f 6e 2c 0a 20 20 2a 2a 20 62 75 74 20 69 74 20 64  n,.  ** but it d
28d80 6f 65 73 20 2a 6e 6f 74 2a 20 6f 76 65 72 72 69  oes *not* overri
28d81 64 65 20 73 63 68 65 6d 61 20 6c 6f 63 6b 20 64  de schema lock d
28d82 65 74 65 63 74 69 6f 6e 2c 20 73 6f 20 74 68 69  etection, so thi
28d83 73 20 61 6c 6c 20 73 74 69 6c 6c 0a 20 20 2a 2a  s all still.  **
28d84 20 77 6f 72 6b 73 20 65 76 65 6e 20 69 66 20 52   works even if R
28d85 45 41 44 5f 55 4e 43 4f 4d 4d 49 54 54 45 44 20  EAD_UNCOMMITTED 
28d86 69 73 20 73 65 74 2e 0a 20 20 2a 2f 0a 20 20 66  is set..  */.  f
28d87 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
28d88 62 3b 20 69 2b 2b 29 20 7b 0a 20 20 20 20 42 74  b; i++) {.    Bt
28d89 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61  ree *pBt = db->a
28d8a 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69  Db[i].pBt;.    i
28d8b 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20  f( pBt ){.      
28d8c 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
28d8d 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70  treeHoldsMutex(p
28d8e 42 74 29 20 29 3b 0a 20 20 20 20 20 20 72 63 20  Bt) );.      rc 
28d8f 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63  = sqlite3BtreeSc
28d90 68 65 6d 61 4c 6f 63 6b 65 64 28 70 42 74 29 3b  hemaLocked(pBt);
28d91 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b  .      if( rc ){
28d92 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63  .        const c
28d93 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61  har *zDb = db->a
28d94 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  Db[i].zName;.   
28d95 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
28d96 72 28 64 62 2c 20 72 63 2c 20 22 64 61 74 61 62  r(db, rc, "datab
28d97 61 73 65 20 73 63 68 65 6d 61 20 69 73 20 6c 6f  ase schema is lo
28d98 63 6b 65 64 3a 20 25 73 22 2c 20 7a 44 62 29 3b  cked: %s", zDb);
28d99 0a 20 20 20 20 20 20 20 20 28 76 6f 69 64 29 73  .        (void)s
28d9a 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
28d9b 64 62 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73  db);.        tes
28d9c 74 63 61 73 65 28 20 64 62 2d 3e 66 6c 61 67 73  tcase( db->flags
28d9d 20 26 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e   & SQLITE_ReadUn
28d9e 63 6f 6d 6d 69 74 74 65 64 20 29 3b 0a 20 20 20  committed );.   
28d9f 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 72       goto end_pr
28da0 65 70 61 72 65 3b 0a 20 20 20 20 20 20 7d 0a 20  epare;.      }. 
28da1 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69     }.  }..  sqli
28da2 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69 73  te3VtabUnlockLis
28da3 74 28 64 62 29 3b 0a 0a 20 20 70 50 61 72 73 65  t(db);..  pParse
28da4 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 69 66 28  ->db = db;.  if(
28da5 20 6e 42 79 74 65 73 3e 3d 30 20 26 26 20 28 6e   nBytes>=0 && (n
28da6 42 79 74 65 73 3d 3d 30 20 7c 7c 20 7a 53 71 6c  Bytes==0 || zSql
28da7 5b 6e 42 79 74 65 73 2d 31 5d 21 3d 30 29 20 29  [nBytes-1]!=0) )
28da8 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 71 6c  {.    char *zSql
28da9 43 6f 70 79 3b 0a 20 20 20 20 69 6e 74 20 6d 78  Copy;.    int mx
28daa 4c 65 6e 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74  Len = db->aLimit
28dab 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51  [SQLITE_LIMIT_SQ
28dac 4c 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 20 20 74  L_LENGTH];.    t
28dad 65 73 74 63 61 73 65 28 20 6e 42 79 74 65 73 3d  estcase( nBytes=
28dae 3d 6d 78 4c 65 6e 20 29 3b 0a 20 20 20 20 74 65  =mxLen );.    te
28daf 73 74 63 61 73 65 28 20 6e 42 79 74 65 73 3d 3d  stcase( nBytes==
28db0 6d 78 4c 65 6e 2b 31 20 29 3b 0a 20 20 20 20 69  mxLen+1 );.    i
28db1 66 28 20 6e 42 79 74 65 73 3e 6d 78 4c 65 6e 20  f( nBytes>mxLen 
28db2 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
28db3 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45  Error(db, SQLITE
28db4 5f 54 4f 4f 42 49 47 2c 20 22 73 74 61 74 65 6d  _TOOBIG, "statem
28db5 65 6e 74 20 74 6f 6f 20 6c 6f 6e 67 22 29 3b 0a  ent too long");.
28db6 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69        (void)sqli
28db7 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29  te3SafetyOff(db)
28db8 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
28db9 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
28dba 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 29 3b 0a  SQLITE_TOOBIG);.
28dbb 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70        goto end_p
28dbc 72 65 70 61 72 65 3b 0a 20 20 20 20 7d 0a 20 20  repare;.    }.  
28dbd 20 20 7a 53 71 6c 43 6f 70 79 20 3d 20 73 71 6c    zSqlCopy = sql
28dbe 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62  ite3DbStrNDup(db
28dbf 2c 20 7a 53 71 6c 2c 20 6e 42 79 74 65 73 29 3b  , zSql, nBytes);
28dc0 0a 20 20 20 20 69 66 28 20 7a 53 71 6c 43 6f 70  .    if( zSqlCop
28dc1 79 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  y ){.      sqlit
28dc2 65 33 52 75 6e 50 61 72 73 65 72 28 70 50 61 72  e3RunParser(pPar
28dc3 73 65 2c 20 7a 53 71 6c 43 6f 70 79 2c 20 26 7a  se, zSqlCopy, &z
28dc4 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 73  ErrMsg);.      s
28dc5 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
28dc6 20 7a 53 71 6c 43 6f 70 79 29 3b 0a 20 20 20 20   zSqlCopy);.    
28dc7 20 20 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20    pParse->zTail 
28dc8 3d 20 26 7a 53 71 6c 5b 70 50 61 72 73 65 2d 3e  = &zSql[pParse->
28dc9 7a 54 61 69 6c 2d 7a 53 71 6c 43 6f 70 79 5d 3b  zTail-zSqlCopy];
28dca 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
28dcb 20 20 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20    pParse->zTail 
28dcc 3d 20 26 7a 53 71 6c 5b 6e 42 79 74 65 73 5d 3b  = &zSql[nBytes];
28dcd 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
28dce 20 20 20 20 73 71 6c 69 74 65 33 52 75 6e 50 61      sqlite3RunPa
28dcf 72 73 65 72 28 70 50 61 72 73 65 2c 20 7a 53 71  rser(pParse, zSq
28dd0 6c 2c 20 26 7a 45 72 72 4d 73 67 29 3b 0a 20 20  l, &zErrMsg);.  
28dd1 7d 0a 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  }..  if( db->mal
28dd2 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
28dd3 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51   pParse->rc = SQ
28dd4 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
28dd5 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63    if( pParse->rc
28dd6 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20  ==SQLITE_DONE ) 
28dd7 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c  pParse->rc = SQL
28dd8 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 50  ITE_OK;.  if( pP
28dd9 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68 65 6d  arse->checkSchem
28dda 61 20 29 7b 0a 20 20 20 20 73 63 68 65 6d 61 49  a ){.    schemaI
28ddb 73 56 61 6c 69 64 28 70 50 61 72 73 65 29 3b 0a  sValid(pParse);.
28ddc 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65    }.  if( pParse
28ddd 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48  ->rc==SQLITE_SCH
28dde 45 4d 41 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  EMA ){.    sqlit
28ddf 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53  e3ResetInternalS
28de0 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20  chema(db, 0);.  
28de1 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  }.  if( db->mall
28de2 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
28de3 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c  pParse->rc = SQL
28de4 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
28de5 20 69 66 28 20 70 7a 54 61 69 6c 20 29 7b 0a 20   if( pzTail ){. 
28de6 20 20 20 2a 70 7a 54 61 69 6c 20 3d 20 70 50 61     *pzTail = pPa
28de7 72 73 65 2d 3e 7a 54 61 69 6c 3b 0a 20 20 7d 0a  rse->zTail;.  }.
28de8 20 20 72 63 20 3d 20 70 50 61 72 73 65 2d 3e 72    rc = pParse->r
28de9 63 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  c;..#ifndef SQLI
28dea 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a  TE_OMIT_EXPLAIN.
28deb 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
28dec 5f 4f 4b 20 26 26 20 70 50 61 72 73 65 2d 3e 70  _OK && pParse->p
28ded 56 64 62 65 20 26 26 20 70 50 61 72 73 65 2d 3e  Vdbe && pParse->
28dee 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 73  explain ){.    s
28def 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
28df0 20 2a 20 63 6f 6e 73 74 20 61 7a 43 6f 6c 4e 61   * const azColNa
28df1 6d 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20  me[] = {.       
28df2 22 61 64 64 72 22 2c 20 22 6f 70 63 6f 64 65 22  "addr", "opcode"
28df3 2c 20 22 70 31 22 2c 20 22 70 32 22 2c 20 22 70  , "p1", "p2", "p
28df4 33 22 2c 20 22 70 34 22 2c 20 22 70 35 22 2c 20  3", "p4", "p5", 
28df5 22 63 6f 6d 6d 65 6e 74 22 2c 0a 20 20 20 20 20  "comment",.     
28df6 20 20 22 6f 72 64 65 72 22 2c 20 22 66 72 6f 6d    "order", "from
28df7 22 2c 20 22 64 65 74 61 69 6c 22 0a 20 20 20 20  ", "detail".    
28df8 7d 3b 0a 20 20 20 20 69 6e 74 20 69 46 69 72 73  };.    int iFirs
28df9 74 2c 20 6d 78 3b 0a 20 20 20 20 69 66 28 20 70  t, mx;.    if( p
28dfa 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e 3d 3d  Parse->explain==
28dfb 32 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  2 ){.      sqlit
28dfc 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
28dfd 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20  (pParse->pVdbe, 
28dfe 33 29 3b 0a 20 20 20 20 20 20 69 46 69 72 73 74  3);.      iFirst
28dff 20 3d 20 38 3b 0a 20 20 20 20 20 20 6d 78 20 3d   = 8;.      mx =
28e00 20 31 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a   11;.    }else{.
28e01 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
28e02 65 53 65 74 4e 75 6d 43 6f 6c 73 28 70 50 61 72  eSetNumCols(pPar
28e03 73 65 2d 3e 70 56 64 62 65 2c 20 38 29 3b 0a 20  se->pVdbe, 8);. 
28e04 20 20 20 20 20 69 46 69 72 73 74 20 3d 20 30 3b       iFirst = 0;
28e05 0a 20 20 20 20 20 20 6d 78 20 3d 20 38 3b 0a 20  .      mx = 8;. 
28e06 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 69     }.    for(i=i
28e07 46 69 72 73 74 3b 20 69 3c 6d 78 3b 20 69 2b 2b  First; i<mx; i++
28e08 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
28e09 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 70  VdbeSetColName(p
28e0a 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 69 2d  Parse->pVdbe, i-
28e0b 69 46 69 72 73 74 2c 20 43 4f 4c 4e 41 4d 45 5f  iFirst, COLNAME_
28e0c 4e 41 4d 45 2c 0a 20 20 20 20 20 20 20 20 20 20  NAME,.          
28e0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28e0e 20 20 61 7a 43 6f 6c 4e 61 6d 65 5b 69 5d 2c 20    azColName[i], 
28e0f 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
28e10 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
28e11 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ..  if( sqlite3S
28e12 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 7b 0a  afetyOff(db) ){.
28e13 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
28e14 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 0a 20 20 61  MISUSE;.  }..  a
28e15 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e  ssert( db->init.
28e16 62 75 73 79 3d 3d 30 20 7c 7c 20 73 61 76 65 53  busy==0 || saveS
28e17 71 6c 46 6c 61 67 3d 3d 30 20 29 3b 0a 20 20 69  qlFlag==0 );.  i
28e18 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  f( db->init.busy
28e19 3d 3d 30 20 29 7b 0a 20 20 20 20 56 64 62 65 20  ==0 ){.    Vdbe 
28e1a 2a 70 56 64 62 65 20 3d 20 70 50 61 72 73 65 2d  *pVdbe = pParse-
28e1b 3e 70 56 64 62 65 3b 0a 20 20 20 20 73 71 6c 69  >pVdbe;.    sqli
28e1c 74 65 33 56 64 62 65 53 65 74 53 71 6c 28 70 56  te3VdbeSetSql(pV
28e1d 64 62 65 2c 20 7a 53 71 6c 2c 20 28 69 6e 74 29  dbe, zSql, (int)
28e1e 28 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 2d 7a  (pParse->zTail-z
28e1f 53 71 6c 29 2c 20 73 61 76 65 53 71 6c 46 6c 61  Sql), saveSqlFla
28e20 67 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50  g);.  }.  if( pP
28e21 61 72 73 65 2d 3e 70 56 64 62 65 20 26 26 20 28  arse->pVdbe && (
28e22 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
28e23 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
28e24 64 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  d) ){.    sqlite
28e25 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 70 50  3VdbeFinalize(pP
28e26 61 72 73 65 2d 3e 70 56 64 62 65 29 3b 0a 20 20  arse->pVdbe);.  
28e27 20 20 61 73 73 65 72 74 28 21 28 2a 70 70 53 74    assert(!(*ppSt
28e28 6d 74 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  mt));.  }else{. 
28e29 20 20 20 2a 70 70 53 74 6d 74 20 3d 20 28 73 71     *ppStmt = (sq
28e2a 6c 69 74 65 33 5f 73 74 6d 74 2a 29 70 50 61 72  lite3_stmt*)pPar
28e2b 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 7d 0a 0a  se->pVdbe;.  }..
28e2c 20 20 69 66 28 20 7a 45 72 72 4d 73 67 20 29 7b    if( zErrMsg ){
28e2d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
28e2e 72 28 64 62 2c 20 72 63 2c 20 22 25 73 22 2c 20  r(db, rc, "%s", 
28e2f 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 73 71  zErrMsg);.    sq
28e30 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
28e31 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d 65 6c 73  zErrMsg);.  }els
28e32 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  e{.    sqlite3Er
28e33 72 6f 72 28 64 62 2c 20 72 63 2c 20 30 29 3b 0a  ror(db, rc, 0);.
28e34 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65    }..  /* Delete
28e35 20 61 6e 79 20 54 72 69 67 67 65 72 50 72 67 20   any TriggerPrg 
28e36 73 74 72 75 63 74 75 72 65 73 20 61 6c 6c 6f 63  structures alloc
28e37 61 74 65 64 20 77 68 69 6c 65 20 70 61 72 73 69  ated while parsi
28e38 6e 67 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e  ng this statemen
28e39 74 2e 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 70  t. */.  while( p
28e3a 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 50  Parse->pTriggerP
28e3b 72 67 20 29 7b 0a 20 20 20 20 54 72 69 67 67 65  rg ){.    Trigge
28e3c 72 50 72 67 20 2a 70 54 20 3d 20 70 50 61 72 73  rPrg *pT = pPars
28e3d 65 2d 3e 70 54 72 69 67 67 65 72 50 72 67 3b 0a  e->pTriggerPrg;.
28e3e 20 20 20 20 70 50 61 72 73 65 2d 3e 70 54 72 69      pParse->pTri
28e3f 67 67 65 72 50 72 67 20 3d 20 70 54 2d 3e 70 4e  ggerPrg = pT->pN
28e40 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ext;.    sqlite3
28e41 56 64 62 65 50 72 6f 67 72 61 6d 44 65 6c 65 74  VdbeProgramDelet
28e42 65 28 64 62 2c 20 70 54 2d 3e 70 50 72 6f 67 72  e(db, pT->pProgr
28e43 61 6d 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69  am, 0);.    sqli
28e44 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54  te3DbFree(db, pT
28e45 29 3b 0a 20 20 7d 0a 0a 65 6e 64 5f 70 72 65 70  );.  }..end_prep
28e46 61 72 65 3a 0a 0a 20 20 73 71 6c 69 74 65 33 53  are:..  sqlite3S
28e47 74 61 63 6b 46 72 65 65 28 64 62 2c 20 70 50 61  tackFree(db, pPa
28e48 72 73 65 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  rse);.  rc = sql
28e49 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
28e4a 72 63 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  rc);.  assert( (
28e4b 72 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d  rc&db->errMask)=
28e4c 3d 72 63 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  =rc );.  return 
28e4d 72 63 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  rc;.}.static int
28e4e 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50   sqlite3LockAndP
28e4f 72 65 70 61 72 65 28 0a 20 20 73 71 6c 69 74 65  repare(.  sqlite
28e50 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
28e51 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
28e52 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f 6e  handle. */.  con
28e53 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20  st char *zSql,  
28e54 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20         /* UTF-8 
28e55 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74  encoded SQL stat
28e56 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20  ement. */.  int 
28e57 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20  nBytes,         
28e58 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
28e59 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73  of zSql in bytes
28e5a 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 53  . */.  int saveS
28e5b 71 6c 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20  qlFlag,         
28e5c 20 2f 2a 20 54 72 75 65 20 74 6f 20 63 6f 70 79   /* True to copy
28e5d 20 53 51 4c 20 74 65 78 74 20 69 6e 74 6f 20 74   SQL text into t
28e5e 68 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  he sqlite3_stmt 
28e5f 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
28e60 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f  t **ppStmt,    /
28e61 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72  * OUT: A pointer
28e62 20 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64   to the prepared
28e63 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
28e64 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54  const char **pzT
28e65 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54  ail       /* OUT
28e66 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20  : End of parsed 
28e67 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69  string */.){.  i
28e68 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28  nt rc;.  assert(
28e69 20 70 70 53 74 6d 74 21 3d 30 20 29 3b 0a 20 20   ppStmt!=0 );.  
28e6a 2a 70 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 69  *ppStmt = 0;.  i
28e6b 66 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74  f( !sqlite3Safet
28e6c 79 43 68 65 63 6b 4f 6b 28 64 62 29 20 29 7b 0a  yCheckOk(db) ){.
28e6d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
28e6e 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
28e6f 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
28e70 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
28e71 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
28e72 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 72 63  terAll(db);.  rc
28e73 20 3d 20 73 71 6c 69 74 65 33 50 72 65 70 61 72   = sqlite3Prepar
28e74 65 28 64 62 2c 20 7a 53 71 6c 2c 20 6e 42 79 74  e(db, zSql, nByt
28e75 65 73 2c 20 73 61 76 65 53 71 6c 46 6c 61 67 2c  es, saveSqlFlag,
28e76 20 70 70 53 74 6d 74 2c 20 70 7a 54 61 69 6c 29   ppStmt, pzTail)
28e77 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
28e78 54 45 5f 53 43 48 45 4d 41 20 29 7b 0a 20 20 20  TE_SCHEMA ){.   
28e79 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
28e7a 65 28 2a 70 70 53 74 6d 74 29 3b 0a 20 20 20 20  e(*ppStmt);.    
28e7b 72 63 20 3d 20 73 71 6c 69 74 65 33 50 72 65 70  rc = sqlite3Prep
28e7c 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 6e 42  are(db, zSql, nB
28e7d 79 74 65 73 2c 20 73 61 76 65 53 71 6c 46 6c 61  ytes, saveSqlFla
28e7e 67 2c 20 70 70 53 74 6d 74 2c 20 70 7a 54 61 69  g, ppStmt, pzTai
28e7f 6c 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  l);.  }.  sqlite
28e80 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64  3BtreeLeaveAll(d
28e81 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  b);.  sqlite3_mu
28e82 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
28e83 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
28e84 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 72 75  c;.}../*.** Reru
28e85 6e 20 74 68 65 20 63 6f 6d 70 69 6c 61 74 69 6f  n the compilatio
28e86 6e 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74  n of a statement
28e87 20 61 66 74 65 72 20 61 20 73 63 68 65 6d 61 20   after a schema 
28e88 63 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  change..**.** If
28e89 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69   the statement i
28e8a 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 72  s successfully r
28e8b 65 63 6f 6d 70 69 6c 65 64 2c 20 72 65 74 75 72  ecompiled, retur
28e8c 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68  n SQLITE_OK. Oth
28e8d 65 72 77 69 73 65 2c 0a 2a 2a 20 69 66 20 74 68  erwise,.** if th
28e8e 65 20 73 74 61 74 65 6d 65 6e 74 20 63 61 6e 6e  e statement cann
28e8f 6f 74 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64  ot be recompiled
28e90 20 62 65 63 61 75 73 65 20 61 6e 6f 74 68 65 72   because another
28e91 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 0a   connection has.
28e92 2a 2a 20 6c 6f 63 6b 65 64 20 74 68 65 20 73 71  ** locked the sq
28e93 6c 69 74 65 33 5f 6d 61 73 74 65 72 20 74 61 62  lite3_master tab
28e94 6c 65 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  le, return SQLIT
28e95 45 5f 4c 4f 43 4b 45 44 2e 20 49 66 20 61 6e 79  E_LOCKED. If any
28e96 20 6f 74 68 65 72 20 65 72 72 6f 72 0a 2a 2a 20   other error.** 
28e97 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 53  occurs, return S
28e98 51 4c 49 54 45 5f 53 43 48 45 4d 41 2e 0a 2a 2f  QLITE_SCHEMA..*/
28e99 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
28e9a 69 6e 74 20 73 71 6c 69 74 65 33 52 65 70 72 65  int sqlite3Repre
28e9b 70 61 72 65 28 56 64 62 65 20 2a 70 29 7b 0a 20  pare(Vdbe *p){. 
28e9c 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74   int rc;.  sqlit
28e9d 65 33 5f 73 74 6d 74 20 2a 70 4e 65 77 3b 0a 20  e3_stmt *pNew;. 
28e9e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71   const char *zSq
28e9f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  l;.  sqlite3 *db
28ea0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
28ea1 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
28ea2 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 70 29  sqlite3VdbeDb(p)
28ea3 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 7a 53  ->mutex) );.  zS
28ea4 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 73 71 6c  ql = sqlite3_sql
28ea5 28 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  ((sqlite3_stmt *
28ea6 29 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a  )p);.  assert( z
28ea7 53 71 6c 21 3d 30 20 29 3b 20 20 2f 2a 20 52 65  Sql!=0 );  /* Re
28ea8 70 72 65 70 61 72 65 20 6f 6e 6c 79 20 63 61 6c  prepare only cal
28ea9 6c 65 64 20 66 6f 72 20 70 72 65 70 61 72 65 5f  led for prepare_
28eaa 76 32 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20  v2() statements 
28eab 2a 2f 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65  */.  db = sqlite
28eac 33 56 64 62 65 44 62 28 70 29 3b 0a 20 20 61 73  3VdbeDb(p);.  as
28ead 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
28eae 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
28eaf 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  ex) );.  rc = sq
28eb0 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70  lite3LockAndPrep
28eb1 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31  are(db, zSql, -1
28eb2 2c 20 30 2c 20 26 70 4e 65 77 2c 20 30 29 3b 0a  , 0, &pNew, 0);.
28eb3 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
28eb4 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  if( rc==SQLITE_N
28eb5 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 64 62  OMEM ){.      db
28eb6 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
28eb7 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73   1;.    }.    as
28eb8 73 65 72 74 28 20 70 4e 65 77 3d 3d 30 20 29 3b  sert( pNew==0 );
28eb9 0a 20 20 20 20 72 65 74 75 72 6e 20 28 72 63 3d  .    return (rc=
28eba 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 29 20  =SQLITE_LOCKED) 
28ebb 3f 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20  ? SQLITE_LOCKED 
28ebc 3a 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b  : SQLITE_SCHEMA;
28ebd 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
28ebe 73 65 72 74 28 20 70 4e 65 77 21 3d 30 20 29 3b  sert( pNew!=0 );
28ebf 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64  .  }.  sqlite3Vd
28ec0 62 65 53 77 61 70 28 28 56 64 62 65 2a 29 70 4e  beSwap((Vdbe*)pN
28ec1 65 77 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74 65  ew, p);.  sqlite
28ec2 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67  3TransferBinding
28ec3 73 28 70 4e 65 77 2c 20 28 73 71 6c 69 74 65 33  s(pNew, (sqlite3
28ec4 5f 73 74 6d 74 2a 29 70 29 3b 0a 20 20 73 71 6c  _stmt*)p);.  sql
28ec5 69 74 65 33 56 64 62 65 52 65 73 65 74 53 74 65  ite3VdbeResetSte
28ec6 70 52 65 73 75 6c 74 28 28 56 64 62 65 2a 29 70  pResult((Vdbe*)p
28ec7 4e 65 77 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  New);.  sqlite3V
28ec8 64 62 65 46 69 6e 61 6c 69 7a 65 28 28 56 64 62  dbeFinalize((Vdb
28ec9 65 2a 29 70 4e 65 77 29 3b 0a 20 20 72 65 74 75  e*)pNew);.  retu
28eca 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
28ecb 0a 0a 2f 2a 0a 2a 2a 20 54 77 6f 20 76 65 72 73  ../*.** Two vers
28ecc 69 6f 6e 73 20 6f 66 20 74 68 65 20 6f 66 66 69  ions of the offi
28ecd 63 69 61 6c 20 41 50 49 2e 20 20 4c 65 67 61 63  cial API.  Legac
28ece 79 20 61 6e 64 20 6e 65 77 20 75 73 65 2e 20 20  y and new use.  
28ecf 49 6e 20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a  In the legacy.**
28ed0 20 76 65 72 73 69 6f 6e 2c 20 74 68 65 20 6f 72   version, the or
28ed1 69 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20  iginal SQL text 
28ed2 69 73 20 6e 6f 74 20 73 61 76 65 64 20 69 6e 20  is not saved in 
28ed3 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61  the prepared sta
28ed4 74 65 6d 65 6e 74 0a 2a 2a 20 61 6e 64 20 73 6f  tement.** and so
28ed5 20 69 66 20 61 20 73 63 68 65 6d 61 20 63 68 61   if a schema cha
28ed6 6e 67 65 20 6f 63 63 75 72 73 2c 20 53 51 4c 49  nge occurs, SQLI
28ed7 54 45 5f 53 43 48 45 4d 41 20 69 73 20 72 65 74  TE_SCHEMA is ret
28ed8 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69  urned by.** sqli
28ed9 74 65 33 5f 73 74 65 70 28 29 2e 20 20 49 6e 20  te3_step().  In 
28eda 74 68 65 20 6e 65 77 20 76 65 72 73 69 6f 6e 2c  the new version,
28edb 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51   the original SQ
28edc 4c 20 74 65 78 74 20 69 73 20 72 65 74 61 69 6e  L text is retain
28edd 65 64 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 74  ed.** and the st
28ede 61 74 65 6d 65 6e 74 20 69 73 20 61 75 74 6f 6d  atement is autom
28edf 61 74 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70 69  atically recompi
28ee0 6c 65 64 20 69 66 20 61 6e 20 73 63 68 65 6d 61  led if an schema
28ee1 20 63 68 61 6e 67 65 0a 2a 2a 20 6f 63 63 75 72   change.** occur
28ee2 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
28ee3 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65   int sqlite3_pre
28ee4 70 61 72 65 28 0a 20 20 73 71 6c 69 74 65 33 20  pare(.  sqlite3 
28ee5 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
28ee6 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61    /* Database ha
28ee7 6e 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74  ndle. */.  const
28ee8 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20   char *zSql,    
28ee9 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e       /* UTF-8 en
28eea 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d  coded SQL statem
28eeb 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  ent. */.  int nB
28eec 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20  ytes,           
28eed 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
28eee 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20   zSql in bytes. 
28eef 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
28ef0 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f  t **ppStmt,    /
28ef1 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72  * OUT: A pointer
28ef2 20 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64   to the prepared
28ef3 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
28ef4 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54  const char **pzT
28ef5 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54  ail       /* OUT
28ef6 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20  : End of parsed 
28ef7 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69  string */.){.  i
28ef8 6e 74 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71  nt rc;.  rc = sq
28ef9 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70  lite3LockAndPrep
28efa 61 72 65 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74  are(db,zSql,nByt
28efb 65 73 2c 30 2c 70 70 53 74 6d 74 2c 70 7a 54 61  es,0,ppStmt,pzTa
28efc 69 6c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72  il);.  assert( r
28efd 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
28efe 70 70 53 74 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70  ppStmt==0 || *pp
28eff 53 74 6d 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 56  Stmt==0 );  /* V
28f00 45 52 49 46 59 3a 20 46 31 33 30 32 31 20 2a 2f  ERIFY: F13021 */
28f01 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
28f02 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
28f03 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
28f04 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  2(.  sqlite3 *db
28f05 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
28f06 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c  * Database handl
28f07 65 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  e. */.  const ch
28f08 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20  ar *zSql,       
28f09 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64    /* UTF-8 encod
28f0a 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ed SQL statement
28f0b 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  . */.  int nByte
28f0c 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
28f0d 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53   /* Length of zS
28f0e 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a  ql in bytes. */.
28f0f 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
28f10 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f  *ppStmt,    /* O
28f11 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  UT: A pointer to
28f12 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74   the prepared st
28f13 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e  atement */.  con
28f14 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c  st char **pzTail
28f15 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45         /* OUT: E
28f16 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73 74 72  nd of parsed str
28f17 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ing */.){.  int 
28f18 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  rc;.  rc = sqlit
28f19 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72 65  e3LockAndPrepare
28f1a 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c  (db,zSql,nBytes,
28f1b 31 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29  1,ppStmt,pzTail)
28f1c 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  ;.  assert( rc==
28f1d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 70 53  SQLITE_OK || ppS
28f1e 74 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70 53 74 6d  tmt==0 || *ppStm
28f1f 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 56 45 52 49  t==0 );  /* VERI
28f20 46 59 3a 20 46 31 33 30 32 31 20 2a 2f 0a 20 20  FY: F13021 */.  
28f21 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23  return rc;.}...#
28f22 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
28f23 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 43  IT_UTF16./*.** C
28f24 6f 6d 70 69 6c 65 20 74 68 65 20 55 54 46 2d 31  ompile the UTF-1
28f25 36 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74  6 encoded SQL st
28f26 61 74 65 6d 65 6e 74 20 7a 53 71 6c 20 69 6e 74  atement zSql int
28f27 6f 20 61 20 73 74 61 74 65 6d 65 6e 74 20 68 61  o a statement ha
28f28 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ndle..*/.static 
28f29 69 6e 74 20 73 71 6c 69 74 65 33 50 72 65 70 61  int sqlite3Prepa
28f2a 72 65 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20  re16(.  sqlite3 
28f2b 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
28f2c 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61    /* Database ha
28f2d 6e 64 6c 65 2e 20 2a 2f 20 0a 20 20 63 6f 6e 73  ndle. */ .  cons
28f2e 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20  t void *zSql,   
28f2f 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65        /* UTF-8 e
28f30 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65  ncoded SQL state
28f31 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e  ment. */.  int n
28f32 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20  Bytes,          
28f33 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
28f34 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e  f zSql in bytes.
28f35 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 53 71   */.  int saveSq
28f36 6c 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 20  lFlag,          
28f37 2f 2a 20 54 72 75 65 20 74 6f 20 73 61 76 65 20  /* True to save 
28f38 53 51 4c 20 74 65 78 74 20 69 6e 74 6f 20 74 68  SQL text into th
28f39 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e sqlite3_stmt *
28f3a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
28f3b 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a   **ppStmt,    /*
28f3c 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20   OUT: A pointer 
28f3d 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20  to the prepared 
28f3e 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63  statement */.  c
28f3f 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61  onst void **pzTa
28f40 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  il       /* OUT:
28f41 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73   End of parsed s
28f42 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 2f 2a  tring */.){.  /*
28f43 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63   This function c
28f44 75 72 72 65 6e 74 6c 79 20 77 6f 72 6b 73 20 62  urrently works b
28f45 79 20 66 69 72 73 74 20 74 72 61 6e 73 66 6f 72  y first transfor
28f46 6d 69 6e 67 20 74 68 65 20 55 54 46 2d 31 36 0a  ming the UTF-16.
28f47 20 20 2a 2a 20 65 6e 63 6f 64 65 64 20 73 74 72    ** encoded str
28f48 69 6e 67 20 74 6f 20 55 54 46 2d 38 2c 20 74 68  ing to UTF-8, th
28f49 65 6e 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69  en invoking sqli
28f4a 74 65 33 5f 70 72 65 70 61 72 65 28 29 2e 20 54  te3_prepare(). T
28f4b 68 65 0a 20 20 2a 2a 20 74 72 69 63 6b 79 20 62  he.  ** tricky b
28f4c 69 74 20 69 73 20 66 69 67 75 72 69 6e 67 20 6f  it is figuring o
28f4d 75 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74  ut the pointer t
28f4e 6f 20 72 65 74 75 72 6e 20 69 6e 20 2a 70 7a 54  o return in *pzT
28f4f 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72  ail..  */.  char
28f50 20 2a 7a 53 71 6c 38 3b 0a 20 20 63 6f 6e 73 74   *zSql8;.  const
28f51 20 63 68 61 72 20 2a 7a 54 61 69 6c 38 20 3d 20   char *zTail8 = 
28f52 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  0;.  int rc = SQ
28f53 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65  LITE_OK;..  asse
28f54 72 74 28 20 70 70 53 74 6d 74 20 29 3b 0a 20 20  rt( ppStmt );.  
28f55 2a 70 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 69  *ppStmt = 0;.  i
28f56 66 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74  f( !sqlite3Safet
28f57 79 43 68 65 63 6b 4f 6b 28 64 62 29 20 29 7b 0a  yCheckOk(db) ){.
28f58 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
28f59 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
28f5a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
28f5b 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
28f5c 20 20 7a 53 71 6c 38 20 3d 20 73 71 6c 69 74 65    zSql8 = sqlite
28f5d 33 55 74 66 31 36 74 6f 38 28 64 62 2c 20 7a 53  3Utf16to8(db, zS
28f5e 71 6c 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 69  ql, nBytes);.  i
28f5f 66 28 20 7a 53 71 6c 38 20 29 7b 0a 20 20 20 20  f( zSql8 ){.    
28f60 72 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 6b  rc = sqlite3Lock
28f61 41 6e 64 50 72 65 70 61 72 65 28 64 62 2c 20 7a  AndPrepare(db, z
28f62 53 71 6c 38 2c 20 2d 31 2c 20 73 61 76 65 53 71  Sql8, -1, saveSq
28f63 6c 46 6c 61 67 2c 20 70 70 53 74 6d 74 2c 20 26  lFlag, ppStmt, &
28f64 7a 54 61 69 6c 38 29 3b 0a 20 20 7d 0a 0a 20 20  zTail8);.  }..  
28f65 69 66 28 20 7a 54 61 69 6c 38 20 26 26 20 70 7a  if( zTail8 && pz
28f66 54 61 69 6c 20 29 7b 0a 20 20 20 20 2f 2a 20 49  Tail ){.    /* I
28f67 66 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  f sqlite3_prepar
28f68 65 20 72 65 74 75 72 6e 73 20 61 20 74 61 69 6c  e returns a tail
28f69 20 70 6f 69 6e 74 65 72 2c 20 77 65 20 63 61 6c   pointer, we cal
28f6a 63 75 6c 61 74 65 20 74 68 65 0a 20 20 20 20 2a  culate the.    *
28f6b 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 70 6f 69  * equivalent poi
28f6c 6e 74 65 72 20 69 6e 74 6f 20 74 68 65 20 55 54  nter into the UT
28f6d 46 2d 31 36 20 73 74 72 69 6e 67 20 62 79 20 63  F-16 string by c
28f6e 6f 75 6e 74 69 6e 67 20 74 68 65 20 75 6e 69 63  ounting the unic
28f6f 6f 64 65 0a 20 20 20 20 2a 2a 20 63 68 61 72 61  ode.    ** chara
28f70 63 74 65 72 73 20 62 65 74 77 65 65 6e 20 7a 53  cters between zS
28f71 71 6c 38 20 61 6e 64 20 7a 54 61 69 6c 38 2c 20  ql8 and zTail8, 
28f72 61 6e 64 20 74 68 65 6e 20 72 65 74 75 72 6e 69  and then returni
28f73 6e 67 20 61 20 70 6f 69 6e 74 65 72 0a 20 20 20  ng a pointer.   
28f74 20 2a 2a 20 74 68 65 20 73 61 6d 65 20 6e 75 6d   ** the same num
28f75 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72  ber of character
28f76 73 20 69 6e 74 6f 20 74 68 65 20 55 54 46 2d 31  s into the UTF-1
28f77 36 20 73 74 72 69 6e 67 2e 0a 20 20 20 20 2a 2f  6 string..    */
28f78 0a 20 20 20 20 69 6e 74 20 63 68 61 72 73 5f 70  .    int chars_p
28f79 61 72 73 65 64 20 3d 20 73 71 6c 69 74 65 33 55  arsed = sqlite3U
28f7a 74 66 38 43 68 61 72 4c 65 6e 28 7a 53 71 6c 38  tf8CharLen(zSql8
28f7b 2c 20 28 69 6e 74 29 28 7a 54 61 69 6c 38 2d 7a  , (int)(zTail8-z
28f7c 53 71 6c 38 29 29 3b 0a 20 20 20 20 2a 70 7a 54  Sql8));.    *pzT
28f7d 61 69 6c 20 3d 20 28 75 38 20 2a 29 7a 53 71 6c  ail = (u8 *)zSql
28f7e 20 2b 20 73 71 6c 69 74 65 33 55 74 66 31 36 42   + sqlite3Utf16B
28f7f 79 74 65 4c 65 6e 28 7a 53 71 6c 2c 20 63 68 61  yteLen(zSql, cha
28f80 72 73 5f 70 61 72 73 65 64 29 3b 0a 20 20 7d 0a  rs_parsed);.  }.
28f81 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
28f82 64 62 2c 20 7a 53 71 6c 38 29 3b 20 0a 20 20 72  db, zSql8); .  r
28f83 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
28f84 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71  it(db, rc);.  sq
28f85 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
28f86 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
28f87 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
28f88 0a 2a 2a 20 54 77 6f 20 76 65 72 73 69 6f 6e 73  .** Two versions
28f89 20 6f 66 20 74 68 65 20 6f 66 66 69 63 69 61 6c   of the official
28f8a 20 41 50 49 2e 20 20 4c 65 67 61 63 79 20 61 6e   API.  Legacy an
28f8b 64 20 6e 65 77 20 75 73 65 2e 20 20 49 6e 20 74  d new use.  In t
28f8c 68 65 20 6c 65 67 61 63 79 0a 2a 2a 20 76 65 72  he legacy.** ver
28f8d 73 69 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69 6e  sion, the origin
28f8e 61 6c 20 53 51 4c 20 74 65 78 74 20 69 73 20 6e  al SQL text is n
28f8f 6f 74 20 73 61 76 65 64 20 69 6e 20 74 68 65 20  ot saved in the 
28f90 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
28f91 6e 74 0a 2a 2a 20 61 6e 64 20 73 6f 20 69 66 20  nt.** and so if 
28f92 61 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 20  a schema change 
28f93 6f 63 63 75 72 73 2c 20 53 51 4c 49 54 45 5f 53  occurs, SQLITE_S
28f94 43 48 45 4d 41 20 69 73 20 72 65 74 75 72 6e 65  CHEMA is returne
28f95 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  d by.** sqlite3_
28f96 73 74 65 70 28 29 2e 20 20 49 6e 20 74 68 65 20  step().  In the 
28f97 6e 65 77 20 76 65 72 73 69 6f 6e 2c 20 74 68 65  new version, the
28f98 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 65   original SQL te
28f99 78 74 20 69 73 20 72 65 74 61 69 6e 65 64 0a 2a  xt is retained.*
28f9a 2a 20 61 6e 64 20 74 68 65 20 73 74 61 74 65 6d  * and the statem
28f9b 65 6e 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63  ent is automatic
28f9c 61 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64 20  ally recompiled 
28f9d 69 66 20 61 6e 20 73 63 68 65 6d 61 20 63 68 61  if an schema cha
28f9e 6e 67 65 0a 2a 2a 20 6f 63 63 75 72 73 2e 0a 2a  nge.** occurs..*
28f9f 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
28fa0 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
28fa1 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  16(.  sqlite3 *d
28fa2 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
28fa3 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64  /* Database hand
28fa4 6c 65 2e 20 2a 2f 20 0a 20 20 63 6f 6e 73 74 20  le. */ .  const 
28fa5 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20  void *zSql,     
28fa6 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63      /* UTF-8 enc
28fa7 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65  oded SQL stateme
28fa8 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79  nt. */.  int nBy
28fa9 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  tes,            
28faa 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
28fab 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a  zSql in bytes. *
28fac 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
28fad 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a   **ppStmt,    /*
28fae 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20   OUT: A pointer 
28faf 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20  to the prepared 
28fb0 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63  statement */.  c
28fb1 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61  onst void **pzTa
28fb2 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  il       /* OUT:
28fb3 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73   End of parsed s
28fb4 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e  tring */.){.  in
28fb5 74 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c  t rc;.  rc = sql
28fb6 69 74 65 33 50 72 65 70 61 72 65 31 36 28 64 62  ite3Prepare16(db
28fb7 2c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c 30 2c 70  ,zSql,nBytes,0,p
28fb8 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29 3b 0a 20  pStmt,pzTail);. 
28fb9 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
28fba 49 54 45 5f 4f 4b 20 7c 7c 20 70 70 53 74 6d 74  ITE_OK || ppStmt
28fbb 3d 3d 30 20 7c 7c 20 2a 70 70 53 74 6d 74 3d 3d  ==0 || *ppStmt==
28fbc 30 20 29 3b 20 20 2f 2a 20 56 45 52 49 46 59 3a  0 );  /* VERIFY:
28fbd 20 46 31 33 30 32 31 20 2a 2f 0a 20 20 72 65 74   F13021 */.  ret
28fbe 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45  urn rc;.}.SQLITE
28fbf 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
28fc0 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 0a 20  _prepare16_v2(. 
28fc1 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
28fc2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
28fc3 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a  tabase handle. *
28fc4 2f 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  / .  const void 
28fc5 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f  *zSql,         /
28fc6 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  * UTF-8 encoded 
28fc7 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a  SQL statement. *
28fc8 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20  /.  int nBytes, 
28fc9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
28fca 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20   Length of zSql 
28fcb 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73  in bytes. */.  s
28fcc 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70  qlite3_stmt **pp
28fcd 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a  Stmt,    /* OUT:
28fce 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
28fcf 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
28fd0 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ment */.  const 
28fd1 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20  void **pzTail   
28fd2 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20      /* OUT: End 
28fd3 6f 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67  of parsed string
28fd4 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
28fd5 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50  .  rc = sqlite3P
28fd6 72 65 70 61 72 65 31 36 28 64 62 2c 7a 53 71 6c  repare16(db,zSql
28fd7 2c 6e 42 79 74 65 73 2c 31 2c 70 70 53 74 6d 74  ,nBytes,1,ppStmt
28fd8 2c 70 7a 54 61 69 6c 29 3b 0a 20 20 61 73 73 65  ,pzTail);.  asse
28fd9 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
28fda 4b 20 7c 7c 20 70 70 53 74 6d 74 3d 3d 30 20 7c  K || ppStmt==0 |
28fdb 7c 20 2a 70 70 53 74 6d 74 3d 3d 30 20 29 3b 20  | *ppStmt==0 ); 
28fdc 20 2f 2a 20 56 45 52 49 46 59 3a 20 46 31 33 30   /* VERIFY: F130
28fdd 32 31 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72  21 */.  return r
28fde 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  c;.}..#endif /* 
28fdf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
28fe0 36 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  6 */../*********
28fe1 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 72 65  ***** End of pre
28fe2 70 61 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  pare.c *********
28fe3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28fe4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28fe5 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
28fe6 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
28fe7 20 73 65 6c 65 63 74 2e 63 20 2a 2a 2a 2a 2a 2a   select.c ******
28fe8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28fe9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28fea 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
28feb 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
28fec 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
28fed 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
28fee 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
28fef 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
28ff0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
28ff1 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
28ff2 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
28ff3 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
28ff4 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
28ff5 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
28ff6 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
28ff7 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
28ff8 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
28ff9 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
28ffa 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
28ffb 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
28ffc 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
28ffd 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
28ffe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28fff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29001 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
29002 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
29003 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e  ns C code routin
29004 65 73 20 74 68 61 74 20 61 72 65 20 63 61 6c 6c  es that are call
29005 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72  ed by the parser
29006 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20 53 45  .** to handle SE
29007 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20  LECT statements 
29008 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a  in SQLite..**.**
29009 20 24 49 64 3a 20 73 65 6c 65 63 74 2e 63 2c 76   $Id: select.c,v
2900a 20 31 2e 35 32 36 20 32 30 30 39 2f 30 38 2f 30   1.526 2009/08/0
2900b 31 20 31 35 3a 30 39 3a 35 38 20 64 72 68 20 45  1 15:09:58 drh E
2900c 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  xp $.*/.../*.** 
2900d 44 65 6c 65 74 65 20 61 6c 6c 20 74 68 65 20 63  Delete all the c
2900e 6f 6e 74 65 6e 74 20 6f 66 20 61 20 53 65 6c 65  ontent of a Sele
2900f 63 74 20 73 74 72 75 63 74 75 72 65 20 62 75 74  ct structure but
29010 20 64 6f 20 6e 6f 74 20 64 65 61 6c 6c 6f 63 61   do not dealloca
29011 74 65 0a 2a 2a 20 74 68 65 20 73 65 6c 65 63 74  te.** the select
29012 20 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c   structure itsel
29013 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  f..*/.static voi
29014 64 20 63 6c 65 61 72 53 65 6c 65 63 74 28 73 71  d clearSelect(sq
29015 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63  lite3 *db, Selec
29016 74 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33  t *p){.  sqlite3
29017 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64  ExprListDelete(d
29018 62 2c 20 70 2d 3e 70 45 4c 69 73 74 29 3b 0a 20  b, p->pEList);. 
29019 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44   sqlite3SrcListD
2901a 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 53 72  elete(db, p->pSr
2901b 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  c);.  sqlite3Exp
2901c 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70  rDelete(db, p->p
2901d 57 68 65 72 65 29 3b 0a 20 20 73 71 6c 69 74 65  Where);.  sqlite
2901e 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
2901f 64 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 29  db, p->pGroupBy)
29020 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44  ;.  sqlite3ExprD
29021 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 48 61  elete(db, p->pHa
29022 76 69 6e 67 29 3b 0a 20 20 73 71 6c 69 74 65 33  ving);.  sqlite3
29023 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64  ExprListDelete(d
29024 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 29 3b  b, p->pOrderBy);
29025 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
29026 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 50  Delete(db, p->pP
29027 72 69 6f 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  rior);.  sqlite3
29028 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
29029 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 73 71 6c  ->pLimit);.  sql
2902a 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2902b 62 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29 3b 0a  b, p->pOffset);.
2902c 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c  }../*.** Initial
2902d 69 7a 65 20 61 20 53 65 6c 65 63 74 44 65 73 74  ize a SelectDest
2902e 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53   structure..*/.S
2902f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
29030 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  id sqlite3Select
29031 44 65 73 74 49 6e 69 74 28 53 65 6c 65 63 74 44  DestInit(SelectD
29032 65 73 74 20 2a 70 44 65 73 74 2c 20 69 6e 74 20  est *pDest, int 
29033 65 44 65 73 74 2c 20 69 6e 74 20 69 50 61 72 6d  eDest, int iParm
29034 29 7b 0a 20 20 70 44 65 73 74 2d 3e 65 44 65 73  ){.  pDest->eDes
29035 74 20 3d 20 28 75 38 29 65 44 65 73 74 3b 0a 20  t = (u8)eDest;. 
29036 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 20 3d 20   pDest->iParm = 
29037 69 50 61 72 6d 3b 0a 20 20 70 44 65 73 74 2d 3e  iParm;.  pDest->
29038 61 66 66 69 6e 69 74 79 20 3d 20 30 3b 0a 20 20  affinity = 0;.  
29039 70 44 65 73 74 2d 3e 69 4d 65 6d 20 3d 20 30 3b  pDest->iMem = 0;
2903a 0a 20 20 70 44 65 73 74 2d 3e 6e 4d 65 6d 20 3d  .  pDest->nMem =
2903b 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c   0;.}.../*.** Al
2903c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 53 65 6c  locate a new Sel
2903d 65 63 74 20 73 74 72 75 63 74 75 72 65 20 61 6e  ect structure an
2903e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  d return a point
2903f 65 72 20 74 6f 20 74 68 61 74 0a 2a 2a 20 73 74  er to that.** st
29040 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49  ructure..*/.SQLI
29041 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63  TE_PRIVATE Selec
29042 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74  t *sqlite3Select
29043 4e 65 77 28 0a 20 20 50 61 72 73 65 20 2a 70 50  New(.  Parse *pP
29044 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
29045 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
29046 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
29047 45 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20 77 68  EList,     /* wh
29048 69 63 68 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 69  ich columns to i
29049 6e 63 6c 75 64 65 20 69 6e 20 74 68 65 20 72 65  nclude in the re
2904a 73 75 6c 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73  sult */.  SrcLis
2904b 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20 20  t *pSrc,        
2904c 2f 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  /* the FROM clau
2904d 73 65 20 2d 2d 20 77 68 69 63 68 20 74 61 62 6c  se -- which tabl
2904e 65 73 20 74 6f 20 73 63 61 6e 20 2a 2f 0a 20 20  es to scan */.  
2904f 45 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20 20  Expr *pWhere,   
29050 20 20 20 20 20 20 2f 2a 20 74 68 65 20 57 48 45        /* the WHE
29051 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45  RE clause */.  E
29052 78 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42  xprList *pGroupB
29053 79 2c 20 20 20 2f 2a 20 74 68 65 20 47 52 4f 55  y,   /* the GROU
29054 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20  P BY clause */. 
29055 20 45 78 70 72 20 2a 70 48 61 76 69 6e 67 2c 20   Expr *pHaving, 
29056 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 48 41         /* the HA
29057 56 49 4e 47 20 63 6c 61 75 73 65 20 2a 2f 0a 20  VING clause */. 
29058 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
29059 72 42 79 2c 20 20 20 2f 2a 20 74 68 65 20 4f 52  rBy,   /* the OR
2905a 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  DER BY clause */
2905b 0a 20 20 69 6e 74 20 69 73 44 69 73 74 69 6e 63  .  int isDistinc
2905c 74 2c 20 20 20 20 20 20 20 2f 2a 20 74 72 75 65  t,       /* true
2905d 20 69 66 20 74 68 65 20 44 49 53 54 49 4e 43 54   if the DISTINCT
2905e 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73   keyword is pres
2905f 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ent */.  Expr *p
29060 4c 69 6d 69 74 2c 20 20 20 20 20 20 20 20 20 2f  Limit,         /
29061 2a 20 4c 49 4d 49 54 20 76 61 6c 75 65 2e 20 20  * LIMIT value.  
29062 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75  NULL means not u
29063 73 65 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  sed */.  Expr *p
29064 4f 66 66 73 65 74 20 20 20 20 20 20 20 20 20 2f  Offset         /
29065 2a 20 4f 46 46 53 45 54 20 76 61 6c 75 65 2e 20  * OFFSET value. 
29066 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 20 6f   NULL means no o
29067 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 53 65  ffset */.){.  Se
29068 6c 65 63 74 20 2a 70 4e 65 77 3b 0a 20 20 53 65  lect *pNew;.  Se
29069 6c 65 63 74 20 73 74 61 6e 64 69 6e 3b 0a 20 20  lect standin;.  
2906a 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2906b 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 4e 65 77  arse->db;.  pNew
2906c 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
2906d 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
2906e 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20 61 73  f(*pNew) );.  as
2906f 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63  sert( db->malloc
29070 46 61 69 6c 65 64 20 7c 7c 20 21 70 4f 66 66 73  Failed || !pOffs
29071 65 74 20 7c 7c 20 70 4c 69 6d 69 74 20 29 3b 20  et || pLimit ); 
29072 2f 2a 20 4f 46 46 53 45 54 20 69 6d 70 6c 69 65  /* OFFSET implie
29073 73 20 4c 49 4d 49 54 20 2a 2f 0a 20 20 69 66 28  s LIMIT */.  if(
29074 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20   pNew==0 ){.    
29075 70 4e 65 77 20 3d 20 26 73 74 61 6e 64 69 6e 3b  pNew = &standin;
29076 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 4e 65 77  .    memset(pNew
29077 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65  , 0, sizeof(*pNe
29078 77 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  w));.  }.  if( p
29079 45 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20  EList==0 ){.    
2907a 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33  pEList = sqlite3
2907b 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
2907c 50 61 72 73 65 2c 20 30 2c 20 73 71 6c 69 74 65  Parse, 0, sqlite
2907d 33 45 78 70 72 28 64 62 2c 54 4b 5f 41 4c 4c 2c  3Expr(db,TK_ALL,
2907e 30 29 29 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d  0));.  }.  pNew-
2907f 3e 70 45 4c 69 73 74 20 3d 20 70 45 4c 69 73 74  >pEList = pEList
29080 3b 0a 20 20 70 4e 65 77 2d 3e 70 53 72 63 20 3d  ;.  pNew->pSrc =
29081 20 70 53 72 63 3b 0a 20 20 70 4e 65 77 2d 3e 70   pSrc;.  pNew->p
29082 57 68 65 72 65 20 3d 20 70 57 68 65 72 65 3b 0a  Where = pWhere;.
29083 20 20 70 4e 65 77 2d 3e 70 47 72 6f 75 70 42 79    pNew->pGroupBy
29084 20 3d 20 70 47 72 6f 75 70 42 79 3b 0a 20 20 70   = pGroupBy;.  p
29085 4e 65 77 2d 3e 70 48 61 76 69 6e 67 20 3d 20 70  New->pHaving = p
29086 48 61 76 69 6e 67 3b 0a 20 20 70 4e 65 77 2d 3e  Having;.  pNew->
29087 70 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65  pOrderBy = pOrde
29088 72 42 79 3b 0a 20 20 70 4e 65 77 2d 3e 73 65 6c  rBy;.  pNew->sel
29089 46 6c 61 67 73 20 3d 20 69 73 44 69 73 74 69 6e  Flags = isDistin
2908a 63 74 20 3f 20 53 46 5f 44 69 73 74 69 6e 63 74  ct ? SF_Distinct
2908b 20 3a 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6f 70   : 0;.  pNew->op
2908c 20 3d 20 54 4b 5f 53 45 4c 45 43 54 3b 0a 20 20   = TK_SELECT;.  
2908d 70 4e 65 77 2d 3e 70 4c 69 6d 69 74 20 3d 20 70  pNew->pLimit = p
2908e 4c 69 6d 69 74 3b 0a 20 20 70 4e 65 77 2d 3e 70  Limit;.  pNew->p
2908f 4f 66 66 73 65 74 20 3d 20 70 4f 66 66 73 65 74  Offset = pOffset
29090 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 66 66  ;.  assert( pOff
29091 73 65 74 3d 3d 30 20 7c 7c 20 70 4c 69 6d 69 74  set==0 || pLimit
29092 21 3d 30 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 61  !=0 );.  pNew->a
29093 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d  ddrOpenEphm[0] =
29094 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64   -1;.  pNew->add
29095 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d  rOpenEphm[1] = -
29096 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f  1;.  pNew->addrO
29097 70 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b  penEphm[2] = -1;
29098 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
29099 63 46 61 69 6c 65 64 20 29 20 7b 0a 20 20 20 20  cFailed ) {.    
2909a 63 6c 65 61 72 53 65 6c 65 63 74 28 64 62 2c 20  clearSelect(db, 
2909b 70 4e 65 77 29 3b 0a 20 20 20 20 69 66 28 20 70  pNew);.    if( p
2909c 4e 65 77 21 3d 26 73 74 61 6e 64 69 6e 20 29 20  New!=&standin ) 
2909d 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2909e 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 70 4e 65  , pNew);.    pNe
2909f 77 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  w = 0;.  }.  ret
290a0 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a  urn pNew;.}../*.
290a1 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 67 69  ** Delete the gi
290a2 76 65 6e 20 53 65 6c 65 63 74 20 73 74 72 75 63  ven Select struc
290a3 74 75 72 65 20 61 6e 64 20 61 6c 6c 20 6f 66 20  ture and all of 
290a4 69 74 73 20 73 75 62 73 74 72 75 63 74 75 72 65  its substructure
290a5 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
290a6 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
290a7 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 73 71  3SelectDelete(sq
290a8 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63  lite3 *db, Selec
290a9 74 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29  t *p){.  if( p )
290aa 7b 0a 20 20 20 20 63 6c 65 61 72 53 65 6c 65 63  {.    clearSelec
290ab 74 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 73 71  t(db, p);.    sq
290ac 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
290ad 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  p);.  }.}../*.**
290ae 20 47 69 76 65 6e 20 31 20 74 6f 20 33 20 69 64   Given 1 to 3 id
290af 65 6e 74 69 66 69 65 72 73 20 70 72 65 63 65 65  entifiers precee
290b0 64 69 6e 67 20 74 68 65 20 4a 4f 49 4e 20 6b 65  ding the JOIN ke
290b1 79 77 6f 72 64 2c 20 64 65 74 65 72 6d 69 6e 65  yword, determine
290b2 20 74 68 65 0a 2a 2a 20 74 79 70 65 20 6f 66 20   the.** type of 
290b3 6a 6f 69 6e 2e 20 20 52 65 74 75 72 6e 20 61 6e  join.  Return an
290b4 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e   integer constan
290b5 74 20 74 68 61 74 20 65 78 70 72 65 73 73 65 73  t that expresses
290b6 20 74 68 61 74 20 74 79 70 65 0a 2a 2a 20 69 6e   that type.** in
290b7 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 66 6f   terms of the fo
290b8 6c 6c 6f 77 69 6e 67 20 62 69 74 20 76 61 6c 75  llowing bit valu
290b9 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4a 54  es:.**.**     JT
290ba 5f 49 4e 4e 45 52 0a 2a 2a 20 20 20 20 20 4a 54  _INNER.**     JT
290bb 5f 43 52 4f 53 53 0a 2a 2a 20 20 20 20 20 4a 54  _CROSS.**     JT
290bc 5f 4f 55 54 45 52 0a 2a 2a 20 20 20 20 20 4a 54  _OUTER.**     JT
290bd 5f 4e 41 54 55 52 41 4c 0a 2a 2a 20 20 20 20 20  _NATURAL.**     
290be 4a 54 5f 4c 45 46 54 0a 2a 2a 20 20 20 20 20 4a  JT_LEFT.**     J
290bf 54 5f 52 49 47 48 54 0a 2a 2a 0a 2a 2a 20 41 20  T_RIGHT.**.** A 
290c0 66 75 6c 6c 20 6f 75 74 65 72 20 6a 6f 69 6e 20  full outer join 
290c1 69 73 20 74 68 65 20 63 6f 6d 62 69 6e 61 74 69  is the combinati
290c2 6f 6e 20 6f 66 20 4a 54 5f 4c 45 46 54 20 61 6e  on of JT_LEFT an
290c3 64 20 4a 54 5f 52 49 47 48 54 2e 0a 2a 2a 0a 2a  d JT_RIGHT..**.*
290c4 2a 20 49 66 20 61 6e 20 69 6c 6c 65 67 61 6c 20  * If an illegal 
290c5 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6a  or unsupported j
290c6 6f 69 6e 20 74 79 70 65 20 69 73 20 73 65 65 6e  oin type is seen
290c7 2c 20 74 68 65 6e 20 73 74 69 6c 6c 20 72 65 74  , then still ret
290c8 75 72 6e 0a 2a 2a 20 61 20 6a 6f 69 6e 20 74 79  urn.** a join ty
290c9 70 65 2c 20 62 75 74 20 70 75 74 20 61 6e 20 65  pe, but put an e
290ca 72 72 6f 72 20 69 6e 20 74 68 65 20 70 50 61 72  rror in the pPar
290cb 73 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  se structure..*/
290cc 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
290cd 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54  int sqlite3JoinT
290ce 79 70 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ype(Parse *pPars
290cf 65 2c 20 54 6f 6b 65 6e 20 2a 70 41 2c 20 54 6f  e, Token *pA, To
290d0 6b 65 6e 20 2a 70 42 2c 20 54 6f 6b 65 6e 20 2a  ken *pB, Token *
290d1 70 43 29 7b 0a 20 20 69 6e 74 20 6a 6f 69 6e 74  pC){.  int joint
290d2 79 70 65 20 3d 20 30 3b 0a 20 20 54 6f 6b 65 6e  ype = 0;.  Token
290d3 20 2a 61 70 41 6c 6c 5b 33 5d 3b 0a 20 20 54 6f   *apAll[3];.  To
290d4 6b 65 6e 20 2a 70 3b 0a 20 20 20 20 20 20 20 20  ken *p;.        
290d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
290d6 20 20 20 20 20 2f 2a 20 20 20 30 31 32 33 34 35       /*   012345
290d7 36 37 38 39 20 31 32 33 34 35 36 37 38 39 20 31  6789 123456789 1
290d8 32 33 34 35 36 37 38 39 20 31 32 33 20 2a 2f 0a  23456789 123 */.
290d9 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
290da 68 61 72 20 7a 4b 65 79 54 65 78 74 5b 5d 20 3d  har zKeyText[] =
290db 20 22 6e 61 74 75 72 61 6c 65 66 74 6f 75 74 65   "naturaleftoute
290dc 72 69 67 68 74 66 75 6c 6c 69 6e 6e 65 72 63 72  rightfullinnercr
290dd 6f 73 73 22 3b 0a 20 20 73 74 61 74 69 63 20 63  oss";.  static c
290de 6f 6e 73 74 20 73 74 72 75 63 74 20 7b 0a 20 20  onst struct {.  
290df 20 20 75 38 20 69 3b 20 20 20 20 20 20 20 20 2f    u8 i;        /
290e0 2a 20 42 65 67 69 6e 6e 69 6e 67 20 6f 66 20 6b  * Beginning of k
290e1 65 79 77 6f 72 64 20 74 65 78 74 20 69 6e 20 7a  eyword text in z
290e2 4b 65 79 54 65 78 74 5b 5d 20 2a 2f 0a 20 20 20  KeyText[] */.   
290e3 20 75 38 20 6e 43 68 61 72 3b 20 20 20 20 2f 2a   u8 nChar;    /*
290e4 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 6b   Length of the k
290e5 65 79 77 6f 72 64 20 69 6e 20 63 68 61 72 61 63  eyword in charac
290e6 74 65 72 73 20 2a 2f 0a 20 20 20 20 75 38 20 63  ters */.    u8 c
290e7 6f 64 65 3b 20 20 20 20 20 2f 2a 20 4a 6f 69 6e  ode;     /* Join
290e8 20 74 79 70 65 20 6d 61 73 6b 20 2a 2f 0a 20 20   type mask */.  
290e9 7d 20 61 4b 65 79 77 6f 72 64 5b 5d 20 3d 20 7b  } aKeyword[] = {
290ea 0a 20 20 20 20 2f 2a 20 6e 61 74 75 72 61 6c 20  .    /* natural 
290eb 2a 2f 20 7b 20 30 2c 20 20 37 2c 20 4a 54 5f 4e  */ { 0,  7, JT_N
290ec 41 54 55 52 41 4c 20 20 20 20 20 20 20 20 20 20  ATURAL          
290ed 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 2f 2a 20        },.    /* 
290ee 6c 65 66 74 20 20 20 20 2a 2f 20 7b 20 36 2c 20  left    */ { 6, 
290ef 20 34 2c 20 4a 54 5f 4c 45 46 54 7c 4a 54 5f 4f   4, JT_LEFT|JT_O
290f0 55 54 45 52 20 20 20 20 20 20 20 20 20 20 7d 2c  UTER          },
290f1 0a 20 20 20 20 2f 2a 20 6f 75 74 65 72 20 20 20  .    /* outer   
290f2 2a 2f 20 7b 20 31 30 2c 20 35 2c 20 4a 54 5f 4f  */ { 10, 5, JT_O
290f3 55 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20  UTER            
290f4 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 2f 2a 20        },.    /* 
290f5 72 69 67 68 74 20 20 20 2a 2f 20 7b 20 31 34 2c  right   */ { 14,
290f6 20 35 2c 20 4a 54 5f 52 49 47 48 54 7c 4a 54 5f   5, JT_RIGHT|JT_
290f7 4f 55 54 45 52 20 20 20 20 20 20 20 20 20 7d 2c  OUTER         },
290f8 0a 20 20 20 20 2f 2a 20 66 75 6c 6c 20 20 20 20  .    /* full    
290f9 2a 2f 20 7b 20 31 39 2c 20 34 2c 20 4a 54 5f 4c  */ { 19, 4, JT_L
290fa 45 46 54 7c 4a 54 5f 52 49 47 48 54 7c 4a 54 5f  EFT|JT_RIGHT|JT_
290fb 4f 55 54 45 52 20 7d 2c 0a 20 20 20 20 2f 2a 20  OUTER },.    /* 
290fc 69 6e 6e 65 72 20 20 20 2a 2f 20 7b 20 32 33 2c  inner   */ { 23,
290fd 20 35 2c 20 4a 54 5f 49 4e 4e 45 52 20 20 20 20   5, JT_INNER    
290fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 2c                },
290ff 0a 20 20 20 20 2f 2a 20 63 72 6f 73 73 20 20 20  .    /* cross   
29100 2a 2f 20 7b 20 32 38 2c 20 35 2c 20 4a 54 5f 49  */ { 28, 5, JT_I
29101 4e 4e 45 52 7c 4a 54 5f 43 52 4f 53 53 20 20 20  NNER|JT_CROSS   
29102 20 20 20 20 20 20 7d 2c 0a 20 20 7d 3b 0a 20 20        },.  };.  
29103 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 61 70 41 6c  int i, j;.  apAl
29104 6c 5b 30 5d 20 3d 20 70 41 3b 0a 20 20 61 70 41  l[0] = pA;.  apA
29105 6c 6c 5b 31 5d 20 3d 20 70 42 3b 0a 20 20 61 70  ll[1] = pB;.  ap
29106 41 6c 6c 5b 32 5d 20 3d 20 70 43 3b 0a 20 20 66  All[2] = pC;.  f
29107 6f 72 28 69 3d 30 3b 20 69 3c 33 20 26 26 20 61  or(i=0; i<3 && a
29108 70 41 6c 6c 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20  pAll[i]; i++){. 
29109 20 20 20 70 20 3d 20 61 70 41 6c 6c 5b 69 5d 3b     p = apAll[i];
2910a 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  .    for(j=0; j<
2910b 41 72 72 61 79 53 69 7a 65 28 61 4b 65 79 77 6f  ArraySize(aKeywo
2910c 72 64 29 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  rd); j++){.     
2910d 20 69 66 28 20 70 2d 3e 6e 3d 3d 61 4b 65 79 77   if( p->n==aKeyw
2910e 6f 72 64 5b 6a 5d 2e 6e 43 68 61 72 20 0a 20 20  ord[j].nChar .  
2910f 20 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74          && sqlit
29110 65 33 53 74 72 4e 49 43 6d 70 28 28 63 68 61 72  e3StrNICmp((char
29111 2a 29 70 2d 3e 7a 2c 20 26 7a 4b 65 79 54 65 78  *)p->z, &zKeyTex
29112 74 5b 61 4b 65 79 77 6f 72 64 5b 6a 5d 2e 69 5d  t[aKeyword[j].i]
29113 2c 20 70 2d 3e 6e 29 3d 3d 30 20 29 7b 0a 20 20  , p->n)==0 ){.  
29114 20 20 20 20 20 20 6a 6f 69 6e 74 79 70 65 20 7c        jointype |
29115 3d 20 61 4b 65 79 77 6f 72 64 5b 6a 5d 2e 63 6f  = aKeyword[j].co
29116 64 65 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  de;.        brea
29117 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
29118 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a  .    testcase( j
29119 3d 3d 30 20 7c 7c 20 6a 3d 3d 31 20 7c 7c 20 6a  ==0 || j==1 || j
2911a 3d 3d 32 20 7c 7c 20 6a 3d 3d 33 20 7c 7c 20 6a  ==2 || j==3 || j
2911b 3d 3d 34 20 7c 7c 20 6a 3d 3d 35 20 7c 7c 20 6a  ==4 || j==5 || j
2911c 3d 3d 36 20 29 3b 0a 20 20 20 20 69 66 28 20 6a  ==6 );.    if( j
2911d 3e 3d 41 72 72 61 79 53 69 7a 65 28 61 4b 65 79  >=ArraySize(aKey
2911e 77 6f 72 64 29 20 29 7b 0a 20 20 20 20 20 20 6a  word) ){.      j
2911f 6f 69 6e 74 79 70 65 20 7c 3d 20 4a 54 5f 45 52  ointype |= JT_ER
29120 52 4f 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ROR;.      break
29121 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
29122 28 0a 20 20 20 20 20 28 6a 6f 69 6e 74 79 70 65  (.     (jointype
29123 20 26 20 28 4a 54 5f 49 4e 4e 45 52 7c 4a 54 5f   & (JT_INNER|JT_
29124 4f 55 54 45 52 29 29 3d 3d 28 4a 54 5f 49 4e 4e  OUTER))==(JT_INN
29125 45 52 7c 4a 54 5f 4f 55 54 45 52 29 20 7c 7c 0a  ER|JT_OUTER) ||.
29126 20 20 20 20 20 28 6a 6f 69 6e 74 79 70 65 20 26       (jointype &
29127 20 4a 54 5f 45 52 52 4f 52 29 21 3d 30 0a 20 20   JT_ERROR)!=0.  
29128 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
29129 72 20 2a 7a 53 70 20 3d 20 22 20 22 3b 0a 20 20  r *zSp = " ";.  
2912a 20 20 61 73 73 65 72 74 28 20 70 42 21 3d 30 20    assert( pB!=0 
2912b 29 3b 0a 20 20 20 20 69 66 28 20 70 43 3d 3d 30  );.    if( pC==0
2912c 20 29 7b 20 7a 53 70 2b 2b 3b 20 7d 0a 20 20 20   ){ zSp++; }.   
2912d 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2912e 28 70 50 61 72 73 65 2c 20 22 75 6e 6b 6e 6f 77  (pParse, "unknow
2912f 6e 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64  n or unsupported
29130 20 6a 6f 69 6e 20 74 79 70 65 3a 20 22 0a 20 20   join type: ".  
29131 20 20 20 20 20 22 25 54 20 25 54 25 73 25 54 22       "%T %T%s%T"
29132 2c 20 70 41 2c 20 70 42 2c 20 7a 53 70 2c 20 70  , pA, pB, zSp, p
29133 43 29 3b 0a 20 20 20 20 6a 6f 69 6e 74 79 70 65  C);.    jointype
29134 20 3d 20 4a 54 5f 49 4e 4e 45 52 3b 0a 20 20 7d   = JT_INNER;.  }
29135 65 6c 73 65 20 69 66 28 20 28 6a 6f 69 6e 74 79  else if( (jointy
29136 70 65 20 26 20 4a 54 5f 4f 55 54 45 52 29 21 3d  pe & JT_OUTER)!=
29137 30 20 0a 20 20 20 20 20 20 20 20 20 26 26 20 28  0 .         && (
29138 6a 6f 69 6e 74 79 70 65 20 26 20 28 4a 54 5f 4c  jointype & (JT_L
29139 45 46 54 7c 4a 54 5f 52 49 47 48 54 29 29 21 3d  EFT|JT_RIGHT))!=
2913a 4a 54 5f 4c 45 46 54 20 29 7b 0a 20 20 20 20 73  JT_LEFT ){.    s
2913b 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2913c 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 22 52  Parse, .      "R
2913d 49 47 48 54 20 61 6e 64 20 46 55 4c 4c 20 4f 55  IGHT and FULL OU
2913e 54 45 52 20 4a 4f 49 4e 73 20 61 72 65 20 6e 6f  TER JOINs are no
2913f 74 20 63 75 72 72 65 6e 74 6c 79 20 73 75 70 70  t currently supp
29140 6f 72 74 65 64 22 29 3b 0a 20 20 20 20 6a 6f 69  orted");.    joi
29141 6e 74 79 70 65 20 3d 20 4a 54 5f 49 4e 4e 45 52  ntype = JT_INNER
29142 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6a  ;.  }.  return j
29143 6f 69 6e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  ointype;.}../*.*
29144 2a 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64  * Return the ind
29145 65 78 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69  ex of a column i
29146 6e 20 61 20 74 61 62 6c 65 2e 20 20 52 65 74 75  n a table.  Retu
29147 72 6e 20 2d 31 20 69 66 20 74 68 65 20 63 6f 6c  rn -1 if the col
29148 75 6d 6e 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 6f  umn.** is not co
29149 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 74  ntained in the t
2914a 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
2914b 69 6e 74 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28  int columnIndex(
2914c 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 63 6f 6e  Table *pTab, con
2914d 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 29 7b 0a  st char *zCol){.
2914e 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69    int i;.  for(i
2914f 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c  =0; i<pTab->nCol
29150 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
29151 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
29152 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61  Tab->aCol[i].zNa
29153 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 20 72  me, zCol)==0 ) r
29154 65 74 75 72 6e 20 69 3b 0a 20 20 7d 0a 20 20 72  eturn i;.  }.  r
29155 65 74 75 72 6e 20 2d 31 3b 0a 7d 0a 0a 2f 2a 0a  eturn -1;.}../*.
29156 2a 2a 20 43 72 65 61 74 65 20 61 6e 20 65 78 70  ** Create an exp
29157 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72  ression node for
29158 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20 77   an identifier w
29159 69 74 68 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  ith the name of 
2915a 7a 4e 61 6d 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f  zName.*/.SQLITE_
2915b 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71  PRIVATE Expr *sq
2915c 6c 69 74 65 33 43 72 65 61 74 65 49 64 45 78 70  lite3CreateIdExp
2915d 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  r(Parse *pParse,
2915e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
2915f 6d 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  me){.  return sq
29160 6c 69 74 65 33 45 78 70 72 28 70 50 61 72 73 65  lite3Expr(pParse
29161 2d 3e 64 62 2c 20 54 4b 5f 49 44 2c 20 7a 4e 61  ->db, TK_ID, zNa
29162 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  me);.}../*.** Ad
29163 64 20 61 20 74 65 72 6d 20 74 6f 20 74 68 65 20  d a term to the 
29164 57 48 45 52 45 20 65 78 70 72 65 73 73 69 6f 6e  WHERE expression
29165 20 69 6e 20 2a 70 70 45 78 70 72 20 74 68 61 74   in *ppExpr that
29166 20 72 65 71 75 69 72 65 73 20 74 68 65 0a 2a 2a   requires the.**
29167 20 7a 43 6f 6c 20 63 6f 6c 75 6d 6e 20 74 6f 20   zCol column to 
29168 62 65 20 65 71 75 61 6c 20 69 6e 20 74 68 65 20  be equal in the 
29169 74 77 6f 20 74 61 62 6c 65 73 20 70 54 61 62 31  two tables pTab1
2916a 20 61 6e 64 20 70 54 61 62 32 2e 0a 2a 2f 0a 73   and pTab2..*/.s
2916b 74 61 74 69 63 20 76 6f 69 64 20 61 64 64 57 68  tatic void addWh
2916c 65 72 65 54 65 72 6d 28 0a 20 20 50 61 72 73 65  ereTerm(.  Parse
2916d 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2916e 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
2916f 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73  ontext */.  cons
29170 74 20 63 68 61 72 20 2a 7a 43 6f 6c 2c 20 20 20  t char *zCol,   
29171 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
29172 74 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20  the column */.  
29173 63 6f 6e 73 74 20 54 61 62 6c 65 20 2a 70 54 61  const Table *pTa
29174 62 31 2c 20 20 20 20 20 20 2f 2a 20 46 69 72 73  b1,      /* Firs
29175 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 6f 6e  t table */.  con
29176 73 74 20 63 68 61 72 20 2a 7a 41 6c 69 61 73 31  st char *zAlias1
29177 2c 20 20 20 20 20 2f 2a 20 41 6c 69 61 73 20 66  ,     /* Alias f
29178 6f 72 20 66 69 72 73 74 20 74 61 62 6c 65 2e 20  or first table. 
29179 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a   May be NULL */.
2917a 20 20 63 6f 6e 73 74 20 54 61 62 6c 65 20 2a 70    const Table *p
2917b 54 61 62 32 2c 20 20 20 20 20 20 2f 2a 20 53 65  Tab2,      /* Se
2917c 63 6f 6e 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20  cond table */.  
2917d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 6c 69  const char *zAli
2917e 61 73 32 2c 20 20 20 20 20 2f 2a 20 41 6c 69 61  as2,     /* Alia
2917f 73 20 66 6f 72 20 73 65 63 6f 6e 64 20 74 61 62  s for second tab
29180 6c 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c  le.  May be NULL
29181 20 2a 2f 0a 20 20 69 6e 74 20 69 52 69 67 68 74   */.  int iRight
29182 4a 6f 69 6e 54 61 62 6c 65 2c 20 20 20 20 20 2f  JoinTable,     /
29183 2a 20 56 44 42 45 20 63 75 72 73 6f 72 20 66 6f  * VDBE cursor fo
29184 72 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c  r the right tabl
29185 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 2a 70 70  e */.  Expr **pp
29186 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20  Expr,           
29187 2f 2a 20 41 64 64 20 74 68 65 20 65 71 75 61 6c  /* Add the equal
29188 69 74 79 20 74 65 72 6d 20 74 6f 20 74 68 69 73  ity term to this
29189 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20   expression */. 
2918a 20 69 6e 74 20 69 73 4f 75 74 65 72 4a 6f 69 6e   int isOuterJoin
2918b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
2918c 65 20 69 66 20 64 65 61 6c 69 6e 67 20 77 69 74  e if dealing wit
2918d 68 20 61 6e 20 4f 55 54 45 52 20 6a 6f 69 6e 20  h an OUTER join 
2918e 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 45  */.){.  Expr *pE
2918f 31 61 2c 20 2a 70 45 31 62 2c 20 2a 70 45 31 63  1a, *pE1b, *pE1c
29190 3b 0a 20 20 45 78 70 72 20 2a 70 45 32 61 2c 20  ;.  Expr *pE2a, 
29191 2a 70 45 32 62 2c 20 2a 70 45 32 63 3b 0a 20 20  *pE2b, *pE2c;.  
29192 45 78 70 72 20 2a 70 45 3b 0a 0a 20 20 70 45 31  Expr *pE;..  pE1
29193 61 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74  a = sqlite3Creat
29194 65 49 64 45 78 70 72 28 70 50 61 72 73 65 2c 20  eIdExpr(pParse, 
29195 7a 43 6f 6c 29 3b 0a 20 20 70 45 32 61 20 3d 20  zCol);.  pE2a = 
29196 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 64 45  sqlite3CreateIdE
29197 78 70 72 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c  xpr(pParse, zCol
29198 29 3b 0a 20 20 69 66 28 20 7a 41 6c 69 61 73 31  );.  if( zAlias1
29199 3d 3d 30 20 29 7b 0a 20 20 20 20 7a 41 6c 69 61  ==0 ){.    zAlia
2919a 73 31 20 3d 20 70 54 61 62 31 2d 3e 7a 4e 61 6d  s1 = pTab1->zNam
2919b 65 3b 0a 20 20 7d 0a 20 20 70 45 31 62 20 3d 20  e;.  }.  pE1b = 
2919c 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 64 45  sqlite3CreateIdE
2919d 78 70 72 28 70 50 61 72 73 65 2c 20 7a 41 6c 69  xpr(pParse, zAli
2919e 61 73 31 29 3b 0a 20 20 69 66 28 20 7a 41 6c 69  as1);.  if( zAli
2919f 61 73 32 3d 3d 30 20 29 7b 0a 20 20 20 20 7a 41  as2==0 ){.    zA
291a0 6c 69 61 73 32 20 3d 20 70 54 61 62 32 2d 3e 7a  lias2 = pTab2->z
291a1 4e 61 6d 65 3b 0a 20 20 7d 0a 20 20 70 45 32 62  Name;.  }.  pE2b
291a2 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74 65   = sqlite3Create
291a3 49 64 45 78 70 72 28 70 50 61 72 73 65 2c 20 7a  IdExpr(pParse, z
291a4 41 6c 69 61 73 32 29 3b 0a 20 20 70 45 31 63 20  Alias2);.  pE1c 
291a5 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
291a6 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 70  Parse, TK_DOT, p
291a7 45 31 62 2c 20 70 45 31 61 2c 20 30 29 3b 0a 20  E1b, pE1a, 0);. 
291a8 20 70 45 32 63 20 3d 20 73 71 6c 69 74 65 33 50   pE2c = sqlite3P
291a9 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
291aa 44 4f 54 2c 20 70 45 32 62 2c 20 70 45 32 61 2c  DOT, pE2b, pE2a,
291ab 20 30 29 3b 0a 20 20 70 45 20 3d 20 73 71 6c 69   0);.  pE = sqli
291ac 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
291ad 20 54 4b 5f 45 51 2c 20 70 45 31 63 2c 20 70 45   TK_EQ, pE1c, pE
291ae 32 63 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 45  2c, 0);.  if( pE
291af 20 26 26 20 69 73 4f 75 74 65 72 4a 6f 69 6e 20   && isOuterJoin 
291b0 29 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50 72  ){.    ExprSetPr
291b1 6f 70 65 72 74 79 28 70 45 2c 20 45 50 5f 46 72  operty(pE, EP_Fr
291b2 6f 6d 4a 6f 69 6e 29 3b 0a 20 20 20 20 61 73 73  omJoin);.    ass
291b3 65 72 74 28 20 21 45 78 70 72 48 61 73 41 6e 79  ert( !ExprHasAny
291b4 50 72 6f 70 65 72 74 79 28 70 45 2c 20 45 50 5f  Property(pE, EP_
291b5 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64  TokenOnly|EP_Red
291b6 75 63 65 64 29 20 29 3b 0a 20 20 20 20 45 78 70  uced) );.    Exp
291b7 72 53 65 74 49 72 72 65 64 75 63 69 62 6c 65 28  rSetIrreducible(
291b8 70 45 29 3b 0a 20 20 20 20 70 45 2d 3e 69 52 69  pE);.    pE->iRi
291b9 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 3d 20 28  ghtJoinTable = (
291ba 69 31 36 29 69 52 69 67 68 74 4a 6f 69 6e 54 61  i16)iRightJoinTa
291bb 62 6c 65 3b 0a 20 20 7d 0a 20 20 2a 70 70 45 78  ble;.  }.  *ppEx
291bc 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  pr = sqlite3Expr
291bd 41 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 2a  And(pParse->db,*
291be 70 70 45 78 70 72 2c 20 70 45 29 3b 0a 7d 0a 0a  ppExpr, pE);.}..
291bf 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 50  /*.** Set the EP
291c0 5f 46 72 6f 6d 4a 6f 69 6e 20 70 72 6f 70 65 72  _FromJoin proper
291c1 74 79 20 6f 6e 20 61 6c 6c 20 74 65 72 6d 73 20  ty on all terms 
291c2 6f 66 20 74 68 65 20 67 69 76 65 6e 20 65 78 70  of the given exp
291c3 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20 41 6e 64 20  ression..** And 
291c4 73 65 74 20 74 68 65 20 45 78 70 72 2e 69 52 69  set the Expr.iRi
291c5 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 74 6f 20  ghtJoinTable to 
291c6 69 54 61 62 6c 65 20 66 6f 72 20 65 76 65 72 79  iTable for every
291c7 20 74 65 72 6d 20 69 6e 20 74 68 65 0a 2a 2a 20   term in the.** 
291c8 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a  expression..**.*
291c9 2a 20 54 68 65 20 45 50 5f 46 72 6f 6d 4a 6f 69  * The EP_FromJoi
291ca 6e 20 70 72 6f 70 65 72 74 79 20 69 73 20 75 73  n property is us
291cb 65 64 20 6f 6e 20 74 65 72 6d 73 20 6f 66 20 61  ed on terms of a
291cc 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20  n expression to 
291cd 74 65 6c 6c 0a 2a 2a 20 74 68 65 20 4c 45 46 54  tell.** the LEFT
291ce 20 4f 55 54 45 52 20 4a 4f 49 4e 20 70 72 6f 63   OUTER JOIN proc
291cf 65 73 73 69 6e 67 20 6c 6f 67 69 63 20 74 68 61  essing logic tha
291d0 74 20 74 68 69 73 20 74 65 72 6d 20 69 73 20 70  t this term is p
291d1 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 6a 6f  art of the.** jo
291d2 69 6e 20 72 65 73 74 72 69 63 74 69 6f 6e 20 73  in restriction s
291d3 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20  pecified in the 
291d4 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75  ON or USING clau
291d5 73 65 20 61 6e 64 20 6e 6f 74 20 61 20 70 61 72  se and not a par
291d6 74 0a 2a 2a 20 6f 66 20 74 68 65 20 6d 6f 72 65  t.** of the more
291d7 20 67 65 6e 65 72 61 6c 20 57 48 45 52 45 20 63   general WHERE c
291d8 6c 61 75 73 65 2e 20 20 54 68 65 73 65 20 74 65  lause.  These te
291d9 72 6d 73 20 61 72 65 20 6d 6f 76 65 64 20 6f 76  rms are moved ov
291da 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 57 48 45  er to the.** WHE
291db 52 45 20 63 6c 61 75 73 65 20 64 75 72 69 6e 67  RE clause during
291dc 20 6a 6f 69 6e 20 70 72 6f 63 65 73 73 69 6e 67   join processing
291dd 20 62 75 74 20 77 65 20 6e 65 65 64 20 74 6f 20   but we need to 
291de 72 65 6d 65 6d 62 65 72 20 74 68 61 74 20 74 68  remember that th
291df 65 79 0a 2a 2a 20 6f 72 69 67 69 6e 61 74 65 64  ey.** originated
291e0 20 69 6e 20 74 68 65 20 4f 4e 20 6f 72 20 55 53   in the ON or US
291e1 49 4e 47 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a  ING clause..**.*
291e2 2a 20 54 68 65 20 45 78 70 72 2e 69 52 69 67 68  * The Expr.iRigh
291e3 74 4a 6f 69 6e 54 61 62 6c 65 20 74 65 6c 6c 73  tJoinTable tells
291e4 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
291e5 65 20 70 72 6f 63 65 73 73 69 6e 67 20 74 68 61  e processing tha
291e6 74 20 74 68 65 0a 2a 2a 20 65 78 70 72 65 73 73  t the.** express
291e7 69 6f 6e 20 64 65 70 65 6e 64 73 20 6f 6e 20 74  ion depends on t
291e8 61 62 6c 65 20 69 52 69 67 68 74 4a 6f 69 6e 54  able iRightJoinT
291e9 61 62 6c 65 20 65 76 65 6e 20 69 66 20 74 68 61  able even if tha
291ea 74 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 0a 2a  t table is not.*
291eb 2a 20 65 78 70 6c 69 63 69 74 6c 79 20 6d 65 6e  * explicitly men
291ec 74 69 6f 6e 65 64 20 69 6e 20 74 68 65 20 65 78  tioned in the ex
291ed 70 72 65 73 73 69 6f 6e 2e 20 20 54 68 61 74 20  pression.  That 
291ee 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 6e  information is n
291ef 65 65 64 65 64 0a 2a 2a 20 66 6f 72 20 63 61 73  eeded.** for cas
291f0 65 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  es like this:.**
291f1 0a 2a 2a 20 20 20 20 53 45 4c 45 43 54 20 2a 20  .**    SELECT * 
291f2 46 52 4f 4d 20 74 31 20 4c 45 46 54 20 4a 4f 49  FROM t1 LEFT JOI
291f3 4e 20 74 32 20 4f 4e 20 74 31 2e 61 3d 74 32 2e  N t2 ON t1.a=t2.
291f4 62 20 41 4e 44 20 74 31 2e 78 3d 35 0a 2a 2a 0a  b AND t1.x=5.**.
291f5 2a 2a 20 54 68 65 20 77 68 65 72 65 20 63 6c 61  ** The where cla
291f6 75 73 65 20 6e 65 65 64 73 20 74 6f 20 64 65 66  use needs to def
291f7 65 72 20 74 68 65 20 68 61 6e 64 6c 69 6e 67 20  er the handling 
291f8 6f 66 20 74 68 65 20 74 31 2e 78 3d 35 0a 2a 2a  of the t1.x=5.**
291f9 20 74 65 72 6d 20 75 6e 74 69 6c 20 61 66 74 65   term until afte
291fa 72 20 74 68 65 20 74 32 20 6c 6f 6f 70 20 6f 66  r the t2 loop of
291fb 20 74 68 65 20 6a 6f 69 6e 2e 20 20 49 6e 20 74   the join.  In t
291fc 68 61 74 20 77 61 79 2c 20 61 0a 2a 2a 20 4e 55  hat way, a.** NU
291fd 4c 4c 20 74 32 20 72 6f 77 20 77 69 6c 6c 20 62  LL t2 row will b
291fe 65 20 69 6e 73 65 72 74 65 64 20 77 68 65 6e 65  e inserted whene
291ff 76 65 72 20 74 31 2e 78 21 3d 35 2e 20 20 49 66  ver t1.x!=5.  If
29200 20 77 65 20 64 6f 20 6e 6f 74 0a 2a 2a 20 64 65   we do not.** de
29201 66 65 72 20 74 68 65 20 68 61 6e 64 6c 69 6e 67  fer the handling
29202 20 6f 66 20 74 31 2e 78 3d 35 2c 20 69 74 20 77   of t1.x=5, it w
29203 69 6c 6c 20 62 65 20 70 72 6f 63 65 73 73 65 64  ill be processed
29204 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20   immediately.** 
29205 61 66 74 65 72 20 74 68 65 20 74 31 20 6c 6f 6f  after the t1 loo
29206 70 20 61 6e 64 20 72 6f 77 73 20 77 69 74 68 20  p and rows with 
29207 74 31 2e 78 21 3d 35 20 77 69 6c 6c 20 6e 65 76  t1.x!=5 will nev
29208 65 72 20 61 70 70 65 61 72 20 69 6e 0a 2a 2a 20  er appear in.** 
29209 74 68 65 20 6f 75 74 70 75 74 2c 20 77 68 69 63  the output, whic
2920a 68 20 69 73 20 69 6e 63 6f 72 72 65 63 74 2e 0a  h is incorrect..
2920b 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
2920c 65 74 4a 6f 69 6e 45 78 70 72 28 45 78 70 72 20  etJoinExpr(Expr 
2920d 2a 70 2c 20 69 6e 74 20 69 54 61 62 6c 65 29 7b  *p, int iTable){
2920e 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20  .  while( p ){. 
2920f 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72     ExprSetProper
29210 74 79 28 70 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69  ty(p, EP_FromJoi
29211 6e 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  n);.    assert( 
29212 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65  !ExprHasAnyPrope
29213 72 74 79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f  rty(p, EP_TokenO
29214 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20  nly|EP_Reduced) 
29215 29 3b 0a 20 20 20 20 45 78 70 72 53 65 74 49 72  );.    ExprSetIr
29216 72 65 64 75 63 69 62 6c 65 28 70 29 3b 0a 20 20  reducible(p);.  
29217 20 20 70 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54    p->iRightJoinT
29218 61 62 6c 65 20 3d 20 28 69 31 36 29 69 54 61 62  able = (i16)iTab
29219 6c 65 3b 0a 20 20 20 20 73 65 74 4a 6f 69 6e 45  le;.    setJoinE
2921a 78 70 72 28 70 2d 3e 70 4c 65 66 74 2c 20 69 54  xpr(p->pLeft, iT
2921b 61 62 6c 65 29 3b 0a 20 20 20 20 70 20 3d 20 70  able);.    p = p
2921c 2d 3e 70 52 69 67 68 74 3b 0a 20 20 7d 20 0a 7d  ->pRight;.  } .}
2921d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
2921e 74 69 6e 65 20 70 72 6f 63 65 73 73 65 73 20 74  tine processes t
2921f 68 65 20 6a 6f 69 6e 20 69 6e 66 6f 72 6d 61 74  he join informat
29220 69 6f 6e 20 66 6f 72 20 61 20 53 45 4c 45 43 54  ion for a SELECT
29221 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 4f   statement..** O
29222 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75  N and USING clau
29223 73 65 73 20 61 72 65 20 63 6f 6e 76 65 72 74 65  ses are converte
29224 64 20 69 6e 74 6f 20 65 78 74 72 61 20 74 65 72  d into extra ter
29225 6d 73 20 6f 66 20 74 68 65 20 57 48 45 52 45 20  ms of the WHERE 
29226 63 6c 61 75 73 65 2e 0a 2a 2a 20 4e 41 54 55 52  clause..** NATUR
29227 41 4c 20 6a 6f 69 6e 73 20 61 6c 73 6f 20 63 72  AL joins also cr
29228 65 61 74 65 20 65 78 74 72 61 20 57 48 45 52 45  eate extra WHERE
29229 20 63 6c 61 75 73 65 20 74 65 72 6d 73 2e 0a 2a   clause terms..*
2922a 2a 0a 2a 2a 20 54 68 65 20 74 65 72 6d 73 20 6f  *.** The terms o
2922b 66 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  f a FROM clause 
2922c 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e  are contained in
2922d 20 74 68 65 20 53 65 6c 65 63 74 2e 70 53 72 63   the Select.pSrc
2922e 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54   structure..** T
2922f 68 65 20 6c 65 66 74 20 6d 6f 73 74 20 74 61 62  he left most tab
29230 6c 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20  le is the first 
29231 65 6e 74 72 79 20 69 6e 20 53 65 6c 65 63 74 2e  entry in Select.
29232 70 53 72 63 2e 20 20 54 68 65 20 72 69 67 68 74  pSrc.  The right
29233 2d 6d 6f 73 74 0a 2a 2a 20 74 61 62 6c 65 20 69  -most.** table i
29234 73 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79  s the last entry
29235 2e 20 20 54 68 65 20 6a 6f 69 6e 20 6f 70 65 72  .  The join oper
29236 61 74 6f 72 20 69 73 20 68 65 6c 64 20 69 6e 20  ator is held in 
29237 74 68 65 20 65 6e 74 72 79 20 74 6f 0a 2a 2a 20  the entry to.** 
29238 74 68 65 20 6c 65 66 74 2e 20 20 54 68 75 73 20  the left.  Thus 
29239 65 6e 74 72 79 20 30 20 63 6f 6e 74 61 69 6e 73  entry 0 contains
2923a 20 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74   the join operat
2923b 6f 72 20 66 6f 72 20 74 68 65 20 6a 6f 69 6e 20  or for the join 
2923c 62 65 74 77 65 65 6e 0a 2a 2a 20 65 6e 74 72 69  between.** entri
2923d 65 73 20 30 20 61 6e 64 20 31 2e 20 20 41 6e 79  es 0 and 1.  Any
2923e 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61   ON or USING cla
2923f 75 73 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  uses associated 
29240 77 69 74 68 20 74 68 65 20 6a 6f 69 6e 20 61 72  with the join ar
29241 65 0a 2a 2a 20 61 6c 73 6f 20 61 74 74 61 63 68  e.** also attach
29242 65 64 20 74 6f 20 74 68 65 20 6c 65 66 74 20 65  ed to the left e
29243 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ntry..**.** This
29244 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
29245 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
29246 72 72 6f 72 73 20 65 6e 63 6f 75 6e 74 65 72 65  rrors encountere
29247 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
29248 20 73 71 6c 69 74 65 50 72 6f 63 65 73 73 4a 6f   sqliteProcessJo
29249 69 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  in(Parse *pParse
2924a 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20  , Select *p){.  
2924b 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 20 20  SrcList *pSrc;  
2924c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2924d 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 73 20 69 6e  /* All tables in
2924e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2924f 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20   */.  int i, j; 
29250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29251 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
29252 75 6e 74 65 72 73 20 2a 2f 0a 20 20 73 74 72 75  unters */.  stru
29253 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
29254 2a 70 4c 65 66 74 3b 20 20 20 20 20 2f 2a 20 4c  *pLeft;     /* L
29255 65 66 74 20 74 61 62 6c 65 20 62 65 69 6e 67 20  eft table being 
29256 6a 6f 69 6e 65 64 20 2a 2f 0a 20 20 73 74 72 75  joined */.  stru
29257 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
29258 2a 70 52 69 67 68 74 3b 20 20 20 20 2f 2a 20 52  *pRight;    /* R
29259 69 67 68 74 20 74 61 62 6c 65 20 62 65 69 6e 67  ight table being
2925a 20 6a 6f 69 6e 65 64 20 2a 2f 0a 0a 20 20 70 53   joined */..  pS
2925b 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20  rc = p->pSrc;.  
2925c 70 4c 65 66 74 20 3d 20 26 70 53 72 63 2d 3e 61  pLeft = &pSrc->a
2925d 5b 30 5d 3b 0a 20 20 70 52 69 67 68 74 20 3d 20  [0];.  pRight = 
2925e 26 70 4c 65 66 74 5b 31 5d 3b 0a 20 20 66 6f 72  &pLeft[1];.  for
2925f 28 69 3d 30 3b 20 69 3c 70 53 72 63 2d 3e 6e 53  (i=0; i<pSrc->nS
29260 72 63 2d 31 3b 20 69 2b 2b 2c 20 70 52 69 67 68  rc-1; i++, pRigh
29261 74 2b 2b 2c 20 70 4c 65 66 74 2b 2b 29 7b 0a 20  t++, pLeft++){. 
29262 20 20 20 54 61 62 6c 65 20 2a 70 4c 65 66 74 54     Table *pLeftT
29263 61 62 20 3d 20 70 4c 65 66 74 2d 3e 70 54 61 62  ab = pLeft->pTab
29264 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 52 69  ;.    Table *pRi
29265 67 68 74 54 61 62 20 3d 20 70 52 69 67 68 74 2d  ghtTab = pRight-
29266 3e 70 54 61 62 3b 0a 20 20 20 20 69 6e 74 20 69  >pTab;.    int i
29267 73 4f 75 74 65 72 3b 0a 0a 20 20 20 20 69 66 28  sOuter;..    if(
29268 20 4e 45 56 45 52 28 70 4c 65 66 74 54 61 62 3d   NEVER(pLeftTab=
29269 3d 30 20 7c 7c 20 70 52 69 67 68 74 54 61 62 3d  =0 || pRightTab=
2926a 3d 30 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  =0) ) continue;.
2926b 20 20 20 20 69 73 4f 75 74 65 72 20 3d 20 28 70      isOuter = (p
2926c 52 69 67 68 74 2d 3e 6a 6f 69 6e 74 79 70 65 20  Right->jointype 
2926d 26 20 4a 54 5f 4f 55 54 45 52 29 21 3d 30 3b 0a  & JT_OUTER)!=0;.
2926e 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65  .    /* When the
2926f 20 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f 72 64   NATURAL keyword
29270 20 69 73 20 70 72 65 73 65 6e 74 2c 20 61 64 64   is present, add
29271 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65   WHERE clause te
29272 72 6d 73 20 66 6f 72 0a 20 20 20 20 2a 2a 20 65  rms for.    ** e
29273 76 65 72 79 20 63 6f 6c 75 6d 6e 20 74 68 61 74  very column that
29274 20 74 68 65 20 74 77 6f 20 74 61 62 6c 65 73 20   the two tables 
29275 68 61 76 65 20 69 6e 20 63 6f 6d 6d 6f 6e 2e 0a  have in common..
29276 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
29277 52 69 67 68 74 2d 3e 6a 6f 69 6e 74 79 70 65 20  Right->jointype 
29278 26 20 4a 54 5f 4e 41 54 55 52 41 4c 20 29 7b 0a  & JT_NATURAL ){.
29279 20 20 20 20 20 20 69 66 28 20 70 52 69 67 68 74        if( pRight
2927a 2d 3e 70 4f 6e 20 7c 7c 20 70 52 69 67 68 74 2d  ->pOn || pRight-
2927b 3e 70 55 73 69 6e 67 20 29 7b 0a 20 20 20 20 20  >pUsing ){.     
2927c 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2927d 73 67 28 70 50 61 72 73 65 2c 20 22 61 20 4e 41  sg(pParse, "a NA
2927e 54 55 52 41 4c 20 6a 6f 69 6e 20 6d 61 79 20 6e  TURAL join may n
2927f 6f 74 20 68 61 76 65 20 22 0a 20 20 20 20 20 20  ot have ".      
29280 20 20 20 20 20 22 61 6e 20 4f 4e 20 6f 72 20 55       "an ON or U
29281 53 49 4e 47 20 63 6c 61 75 73 65 22 2c 20 30 29  SING clause", 0)
29282 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
29283 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
29284 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4c 65    for(j=0; j<pLe
29285 66 74 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b  ftTab->nCol; j++
29286 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  ){.        char 
29287 2a 7a 4e 61 6d 65 20 3d 20 70 4c 65 66 74 54 61  *zName = pLeftTa
29288 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65  b->aCol[j].zName
29289 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6f  ;.        if( co
2928a 6c 75 6d 6e 49 6e 64 65 78 28 70 52 69 67 68 74  lumnIndex(pRight
2928b 54 61 62 2c 20 7a 4e 61 6d 65 29 3e 3d 30 20 29  Tab, zName)>=0 )
2928c 7b 0a 20 20 20 20 20 20 20 20 20 20 61 64 64 57  {.          addW
2928d 68 65 72 65 54 65 72 6d 28 70 50 61 72 73 65 2c  hereTerm(pParse,
2928e 20 7a 4e 61 6d 65 2c 20 70 4c 65 66 74 54 61 62   zName, pLeftTab
2928f 2c 20 70 4c 65 66 74 2d 3e 7a 41 6c 69 61 73 2c  , pLeft->zAlias,
29290 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
29291 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29292 70 52 69 67 68 74 54 61 62 2c 20 70 52 69 67 68  pRightTab, pRigh
29293 74 2d 3e 7a 41 6c 69 61 73 2c 0a 20 20 20 20 20  t->zAlias,.     
29294 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29295 20 20 20 20 20 20 20 20 20 70 52 69 67 68 74 2d           pRight-
29296 3e 69 43 75 72 73 6f 72 2c 20 26 70 2d 3e 70 57  >iCursor, &p->pW
29297 68 65 72 65 2c 20 69 73 4f 75 74 65 72 29 3b 0a  here, isOuter);.
29298 20 20 20 20 20 20 20 20 20 20 0a 20 20 20 20 20            .     
29299 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
2929a 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 69 73 61 6c   }..    /* Disal
2929b 6c 6f 77 20 62 6f 74 68 20 4f 4e 20 61 6e 64 20  low both ON and 
2929c 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 69 6e  USING clauses in
2929d 20 74 68 65 20 73 61 6d 65 20 6a 6f 69 6e 0a 20   the same join. 
2929e 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 52     */.    if( pR
2929f 69 67 68 74 2d 3e 70 4f 6e 20 26 26 20 70 52 69  ight->pOn && pRi
292a0 67 68 74 2d 3e 70 55 73 69 6e 67 20 29 7b 0a 20  ght->pUsing ){. 
292a1 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
292a2 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63 61  rMsg(pParse, "ca
292a3 6e 6e 6f 74 20 68 61 76 65 20 62 6f 74 68 20 4f  nnot have both O
292a4 4e 20 61 6e 64 20 55 53 49 4e 47 20 22 0a 20 20  N and USING ".  
292a5 20 20 20 20 20 20 22 63 6c 61 75 73 65 73 20 69        "clauses i
292a6 6e 20 74 68 65 20 73 61 6d 65 20 6a 6f 69 6e 22  n the same join"
292a7 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
292a8 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  1;.    }..    /*
292a9 20 41 64 64 20 74 68 65 20 4f 4e 20 63 6c 61 75   Add the ON clau
292aa 73 65 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  se to the end of
292ab 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
292ac 65 2c 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 0a  e, connected by.
292ad 20 20 20 20 2a 2a 20 61 6e 20 41 4e 44 20 6f 70      ** an AND op
292ae 65 72 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20  erator..    */. 
292af 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 70     if( pRight->p
292b0 4f 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  On ){.      if( 
292b1 69 73 4f 75 74 65 72 20 29 20 73 65 74 4a 6f 69  isOuter ) setJoi
292b2 6e 45 78 70 72 28 70 52 69 67 68 74 2d 3e 70 4f  nExpr(pRight->pO
292b3 6e 2c 20 70 52 69 67 68 74 2d 3e 69 43 75 72 73  n, pRight->iCurs
292b4 6f 72 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 57  or);.      p->pW
292b5 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78  here = sqlite3Ex
292b6 70 72 41 6e 64 28 70 50 61 72 73 65 2d 3e 64 62  prAnd(pParse->db
292b7 2c 20 70 2d 3e 70 57 68 65 72 65 2c 20 70 52 69  , p->pWhere, pRi
292b8 67 68 74 2d 3e 70 4f 6e 29 3b 0a 20 20 20 20 20  ght->pOn);.     
292b9 20 70 52 69 67 68 74 2d 3e 70 4f 6e 20 3d 20 30   pRight->pOn = 0
292ba 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
292bb 43 72 65 61 74 65 20 65 78 74 72 61 20 74 65 72  Create extra ter
292bc 6d 73 20 6f 6e 20 74 68 65 20 57 48 45 52 45 20  ms on the WHERE 
292bd 63 6c 61 75 73 65 20 66 6f 72 20 65 61 63 68 20  clause for each 
292be 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 0a 20 20 20  column named.   
292bf 20 2a 2a 20 69 6e 20 74 68 65 20 55 53 49 4e 47   ** in the USING
292c0 20 63 6c 61 75 73 65 2e 20 20 45 78 61 6d 70 6c   clause.  Exampl
292c1 65 3a 20 49 66 20 74 68 65 20 74 77 6f 20 74 61  e: If the two ta
292c2 62 6c 65 73 20 74 6f 20 62 65 20 6a 6f 69 6e 65  bles to be joine
292c3 64 20 61 72 65 20 0a 20 20 20 20 2a 2a 20 41 20  d are .    ** A 
292c4 61 6e 64 20 42 20 61 6e 64 20 74 68 65 20 55 53  and B and the US
292c5 49 4e 47 20 63 6c 61 75 73 65 20 6e 61 6d 65 73  ING clause names
292c6 20 58 2c 20 59 2c 20 61 6e 64 20 5a 2c 20 74 68   X, Y, and Z, th
292c7 65 6e 20 61 64 64 20 74 68 69 73 0a 20 20 20 20  en add this.    
292c8 2a 2a 20 74 6f 20 74 68 65 20 57 48 45 52 45 20  ** to the WHERE 
292c9 63 6c 61 75 73 65 3a 20 20 20 20 41 2e 58 3d 42  clause:    A.X=B
292ca 2e 58 20 41 4e 44 20 41 2e 59 3d 42 2e 59 20 41  .X AND A.Y=B.Y A
292cb 4e 44 20 41 2e 5a 3d 42 2e 5a 0a 20 20 20 20 2a  ND A.Z=B.Z.    *
292cc 2a 20 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f  * Report an erro
292cd 72 20 69 66 20 61 6e 79 20 63 6f 6c 75 6d 6e 20  r if any column 
292ce 6d 65 6e 74 69 6f 6e 65 64 20 69 6e 20 74 68 65  mentioned in the
292cf 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 69 73   USING clause is
292d0 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 63 6f 6e 74  .    ** not cont
292d1 61 69 6e 65 64 20 69 6e 20 62 6f 74 68 20 74 61  ained in both ta
292d2 62 6c 65 73 20 74 6f 20 62 65 20 6a 6f 69 6e 65  bles to be joine
292d3 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  d..    */.    if
292d4 28 20 70 52 69 67 68 74 2d 3e 70 55 73 69 6e 67  ( pRight->pUsing
292d5 20 29 7b 0a 20 20 20 20 20 20 49 64 4c 69 73 74   ){.      IdList
292d6 20 2a 70 4c 69 73 74 20 3d 20 70 52 69 67 68 74   *pList = pRight
292d7 2d 3e 70 55 73 69 6e 67 3b 0a 20 20 20 20 20 20  ->pUsing;.      
292d8 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4c 69 73 74  for(j=0; j<pList
292d9 2d 3e 6e 49 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20  ->nId; j++){.   
292da 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65       char *zName
292db 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 7a   = pList->a[j].z
292dc 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 69 66  Name;.        if
292dd 28 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 70 4c  ( columnIndex(pL
292de 65 66 74 54 61 62 2c 20 7a 4e 61 6d 65 29 3c 30  eftTab, zName)<0
292df 20 7c 7c 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28   || columnIndex(
292e0 70 52 69 67 68 74 54 61 62 2c 20 7a 4e 61 6d 65  pRightTab, zName
292e1 29 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  )<0 ){.         
292e2 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
292e3 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74  (pParse, "cannot
292e4 20 6a 6f 69 6e 20 75 73 69 6e 67 20 63 6f 6c 75   join using colu
292e5 6d 6e 20 25 73 20 2d 20 63 6f 6c 75 6d 6e 20 22  mn %s - column "
292e6 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 6e 6f  .            "no
292e7 74 20 70 72 65 73 65 6e 74 20 69 6e 20 62 6f 74  t present in bot
292e8 68 20 74 61 62 6c 65 73 22 2c 20 7a 4e 61 6d 65  h tables", zName
292e9 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  );.          ret
292ea 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  urn 1;.        }
292eb 0a 20 20 20 20 20 20 20 20 61 64 64 57 68 65 72  .        addWher
292ec 65 54 65 72 6d 28 70 50 61 72 73 65 2c 20 7a 4e  eTerm(pParse, zN
292ed 61 6d 65 2c 20 70 4c 65 66 74 54 61 62 2c 20 70  ame, pLeftTab, p
292ee 4c 65 66 74 2d 3e 7a 41 6c 69 61 73 2c 20 0a 20  Left->zAlias, . 
292ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
292f0 20 20 20 20 20 20 20 20 20 20 20 70 52 69 67 68             pRigh
292f1 74 54 61 62 2c 20 70 52 69 67 68 74 2d 3e 7a 41  tTab, pRight->zA
292f2 6c 69 61 73 2c 0a 20 20 20 20 20 20 20 20 20 20  lias,.          
292f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
292f4 20 20 70 52 69 67 68 74 2d 3e 69 43 75 72 73 6f    pRight->iCurso
292f5 72 2c 20 26 70 2d 3e 70 57 68 65 72 65 2c 20 69  r, &p->pWhere, i
292f6 73 4f 75 74 65 72 29 3b 0a 20 20 20 20 20 20 7d  sOuter);.      }
292f7 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
292f8 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
292f9 49 6e 73 65 72 74 20 63 6f 64 65 20 69 6e 74 6f  Insert code into
292fa 20 22 76 22 20 74 68 61 74 20 77 69 6c 6c 20 70   "v" that will p
292fb 75 73 68 20 74 68 65 20 72 65 63 6f 72 64 20 6f  ush the record o
292fc 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65  n the top of the
292fd 0a 2a 2a 20 73 74 61 63 6b 20 69 6e 74 6f 20 74  .** stack into t
292fe 68 65 20 73 6f 72 74 65 72 2e 0a 2a 2f 0a 73 74  he sorter..*/.st
292ff 61 74 69 63 20 76 6f 69 64 20 70 75 73 68 4f 6e  atic void pushOn
29300 74 6f 53 6f 72 74 65 72 28 0a 20 20 50 61 72 73  toSorter(.  Pars
29301 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
29302 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e     /* Parser con
29303 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69  text */.  ExprLi
29304 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20  st *pOrderBy,   
29305 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59   /* The ORDER BY
29306 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c   clause */.  Sel
29307 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20  ect *pSelect,   
29308 20 20 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c 65      /* The whole
29309 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2930a 74 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61  t */.  int regDa
2930b 74 61 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ta            /*
2930c 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   Register holdin
2930d 67 20 64 61 74 61 20 74 6f 20 62 65 20 73 6f 72  g data to be sor
2930e 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65  ted */.){.  Vdbe
2930f 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
29310 64 62 65 3b 0a 20 20 69 6e 74 20 6e 45 78 70 72  dbe;.  int nExpr
29311 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78   = pOrderBy->nEx
29312 70 72 3b 0a 20 20 69 6e 74 20 72 65 67 42 61 73  pr;.  int regBas
29313 65 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  e = sqlite3GetTe
29314 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
29315 6e 45 78 70 72 2b 32 29 3b 0a 20 20 69 6e 74 20  nExpr+2);.  int 
29316 72 65 67 52 65 63 6f 72 64 20 3d 20 73 71 6c 69  regRecord = sqli
29317 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
29318 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  arse);.  sqlite3
29319 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28 70  ExprCacheClear(p
2931a 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65  Parse);.  sqlite
2931b 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73  3ExprCodeExprLis
2931c 74 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72  t(pParse, pOrder
2931d 42 79 2c 20 72 65 67 42 61 73 65 2c 20 30 29 3b  By, regBase, 0);
2931e 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2931f 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 71 75 65  dOp2(v, OP_Seque
29320 6e 63 65 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69  nce, pOrderBy->i
29321 45 43 75 72 73 6f 72 2c 20 72 65 67 42 61 73 65  ECursor, regBase
29322 2b 6e 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74  +nExpr);.  sqlit
29323 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 70  e3ExprCodeMove(p
29324 50 61 72 73 65 2c 20 72 65 67 44 61 74 61 2c 20  Parse, regData, 
29325 72 65 67 42 61 73 65 2b 6e 45 78 70 72 2b 31 2c  regBase+nExpr+1,
29326 20 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64   1);.  sqlite3Vd
29327 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
29328 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 42 61  akeRecord, regBa
29329 73 65 2c 20 6e 45 78 70 72 20 2b 20 32 2c 20 72  se, nExpr + 2, r
2932a 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c  egRecord);.  sql
2932b 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2932c 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20  , OP_IdxInsert, 
2932d 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73  pOrderBy->iECurs
2932e 6f 72 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a  or, regRecord);.
2932f 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
29330 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
29331 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71  regRecord);.  sq
29332 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
29333 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65  Range(pParse, re
29334 67 42 61 73 65 2c 20 6e 45 78 70 72 2b 32 29 3b  gBase, nExpr+2);
29335 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e  .  if( pSelect->
29336 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 69 6e  iLimit ){.    in
29337 74 20 61 64 64 72 31 2c 20 61 64 64 72 32 3b 0a  t addr1, addr2;.
29338 20 20 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b 0a      int iLimit;.
29339 20 20 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d      if( pSelect-
2933a 3e 69 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20  >iOffset ){.    
2933b 20 20 69 4c 69 6d 69 74 20 3d 20 70 53 65 6c 65    iLimit = pSele
2933c 63 74 2d 3e 69 4f 66 66 73 65 74 2b 31 3b 0a 20  ct->iOffset+1;. 
2933d 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2933e 69 4c 69 6d 69 74 20 3d 20 70 53 65 6c 65 63 74  iLimit = pSelect
2933f 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20 20 20 7d 0a  ->iLimit;.    }.
29340 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69      addr1 = sqli
29341 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
29342 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 69 4c 69 6d   OP_IfZero, iLim
29343 69 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  it);.    sqlite3
29344 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
29345 5f 41 64 64 49 6d 6d 2c 20 69 4c 69 6d 69 74 2c  _AddImm, iLimit,
29346 20 2d 31 29 3b 0a 20 20 20 20 61 64 64 72 32 20   -1);.    addr2 
29347 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
29348 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b  Op0(v, OP_Goto);
29349 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2934a 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
2934b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
2934c 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2934d 4c 61 73 74 2c 20 70 4f 72 64 65 72 42 79 2d 3e  Last, pOrderBy->
2934e 69 45 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 73  iECursor);.    s
2934f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
29350 28 76 2c 20 4f 50 5f 44 65 6c 65 74 65 2c 20 70  (v, OP_Delete, p
29351 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f  OrderBy->iECurso
29352 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  r);.    sqlite3V
29353 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
29354 64 64 72 32 29 3b 0a 20 20 20 20 70 53 65 6c 65  ddr2);.    pSele
29355 63 74 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b 0a  ct->iLimit = 0;.
29356 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64    }.}../*.** Add
29357 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65   code to impleme
29358 6e 74 20 74 68 65 20 4f 46 46 53 45 54 0a 2a 2f  nt the OFFSET.*/
29359 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64  .static void cod
2935a 65 4f 66 66 73 65 74 28 0a 20 20 56 64 62 65 20  eOffset(.  Vdbe 
2935b 2a 76 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  *v,          /* 
2935c 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e  Generate code in
2935d 74 6f 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20  to this VM */.  
2935e 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20  Select *p,      
2935f 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20    /* The SELECT 
29360 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20  statement being 
29361 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  coded */.  int i
29362 43 6f 6e 74 69 6e 75 65 20 20 20 20 20 2f 2a 20  Continue     /* 
29363 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 73 6b 69  Jump here to ski
29364 70 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 65  p the current re
29365 63 6f 72 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28  cord */.){.  if(
29366 20 70 2d 3e 69 4f 66 66 73 65 74 20 26 26 20 69   p->iOffset && i
29367 43 6f 6e 74 69 6e 75 65 21 3d 30 20 29 7b 0a 20  Continue!=0 ){. 
29368 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20     int addr;.   
29369 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2936a 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c  p2(v, OP_AddImm,
2936b 20 70 2d 3e 69 4f 66 66 73 65 74 2c 20 2d 31 29   p->iOffset, -1)
2936c 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c  ;.    addr = sql
2936d 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2936e 2c 20 4f 50 5f 49 66 4e 65 67 2c 20 70 2d 3e 69  , OP_IfNeg, p->i
2936f 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 73 71 6c  Offset);.    sql
29370 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
29371 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 43  , OP_Goto, 0, iC
29372 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 20 20 56 64  ontinue);.    Vd
29373 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 73  beComment((v, "s
29374 6b 69 70 20 4f 46 46 53 45 54 20 72 65 63 6f 72  kip OFFSET recor
29375 64 73 22 29 29 3b 0a 20 20 20 20 73 71 6c 69 74  ds"));.    sqlit
29376 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
29377 2c 20 61 64 64 72 29 3b 0a 20 20 7d 0a 7d 0a 0a  , addr);.  }.}..
29378 2f 2a 0a 2a 2a 20 41 64 64 20 63 6f 64 65 20 74  /*.** Add code t
29379 68 61 74 20 77 69 6c 6c 20 63 68 65 63 6b 20 74  hat will check t
2937a 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20  o make sure the 
2937b 4e 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72  N registers star
2937c 74 69 6e 67 20 61 74 20 69 4d 65 6d 0a 2a 2a 20  ting at iMem.** 
2937d 66 6f 72 6d 20 61 20 64 69 73 74 69 6e 63 74 20  form a distinct 
2937e 65 6e 74 72 79 2e 20 20 69 54 61 62 20 69 73 20  entry.  iTab is 
2937f 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20  a sorting index 
29380 74 68 61 74 20 68 6f 6c 64 73 20 70 72 65 76 69  that holds previ
29381 6f 75 73 6c 79 0a 2a 2a 20 73 65 65 6e 20 63 6f  ously.** seen co
29382 6d 62 69 6e 61 74 69 6f 6e 73 20 6f 66 20 74 68  mbinations of th
29383 65 20 4e 20 76 61 6c 75 65 73 2e 20 20 41 20 6e  e N values.  A n
29384 65 77 20 65 6e 74 72 79 20 69 73 20 6d 61 64 65  ew entry is made
29385 20 69 6e 20 69 54 61 62 0a 2a 2a 20 69 66 20 74   in iTab.** if t
29386 68 65 20 63 75 72 72 65 6e 74 20 4e 20 76 61 6c  he current N val
29387 75 65 73 20 61 72 65 20 6e 65 77 2e 0a 2a 2a 0a  ues are new..**.
29388 2a 2a 20 41 20 6a 75 6d 70 20 74 6f 20 61 64 64  ** A jump to add
29389 72 52 65 70 65 61 74 20 69 73 20 6d 61 64 65 20  rRepeat is made 
2938a 61 6e 64 20 74 68 65 20 4e 2b 31 20 76 61 6c 75  and the N+1 valu
2938b 65 73 20 61 72 65 20 70 6f 70 70 65 64 20 66 72  es are popped fr
2938c 6f 6d 20 74 68 65 0a 2a 2a 20 73 74 61 63 6b 20  om the.** stack 
2938d 69 66 20 74 68 65 20 74 6f 70 20 4e 20 65 6c 65  if the top N ele
2938e 6d 65 6e 74 73 20 61 72 65 20 6e 6f 74 20 64 69  ments are not di
2938f 73 74 69 6e 63 74 2e 0a 2a 2f 0a 73 74 61 74 69  stinct..*/.stati
29390 63 20 76 6f 69 64 20 63 6f 64 65 44 69 73 74 69  c void codeDisti
29391 6e 63 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50  nct(.  Parse *pP
29392 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72  arse,     /* Par
29393 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65  sing and code ge
29394 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74  nerating context
29395 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 2c 20   */.  int iTab, 
29396 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 6f           /* A so
29397 72 74 69 6e 67 20 69 6e 64 65 78 20 75 73 65 64  rting index used
29398 20 74 6f 20 74 65 73 74 20 66 6f 72 20 64 69 73   to test for dis
29399 74 69 6e 63 74 6e 65 73 73 20 2a 2f 0a 20 20 69  tinctness */.  i
2939a 6e 74 20 61 64 64 72 52 65 70 65 61 74 2c 20 20  nt addrRepeat,  
2939b 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72    /* Jump to her
2939c 65 20 69 66 20 6e 6f 74 20 64 69 73 74 69 6e 63  e if not distinc
2939d 74 20 2a 2f 0a 20 20 69 6e 74 20 4e 2c 20 20 20  t */.  int N,   
2939e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
2939f 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
293a0 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 6d 20 20 20  */.  int iMem   
293a1 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
293a2 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20   element */.){. 
293a3 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20   Vdbe *v;.  int 
293a4 72 31 3b 0a 0a 20 20 76 20 3d 20 70 50 61 72 73  r1;..  v = pPars
293a5 65 2d 3e 70 56 64 62 65 3b 0a 20 20 72 31 20 3d  e->pVdbe;.  r1 =
293a6 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
293a7 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71  eg(pParse);.  sq
293a8 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
293a9 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  v, OP_MakeRecord
293aa 2c 20 69 4d 65 6d 2c 20 4e 2c 20 72 31 29 3b 0a  , iMem, N, r1);.
293ab 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
293ac 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c  Op3(v, OP_Found,
293ad 20 69 54 61 62 2c 20 61 64 64 72 52 65 70 65 61   iTab, addrRepea
293ae 74 2c 20 72 31 29 3b 0a 20 20 73 71 6c 69 74 65  t, r1);.  sqlite
293af 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
293b0 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 54 61  P_IdxInsert, iTa
293b1 62 2c 20 72 31 29 3b 0a 20 20 73 71 6c 69 74 65  b, r1);.  sqlite
293b2 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
293b3 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a  pParse, r1);.}..
293b4 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61  /*.** Generate a
293b5 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
293b6 77 68 65 6e 20 61 20 53 45 4c 45 43 54 20 69 73  when a SELECT is
293b7 20 75 73 65 64 20 77 69 74 68 69 6e 20 61 20 73   used within a s
293b8 75 62 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20  ubexpression.** 
293b9 28 65 78 61 6d 70 6c 65 3a 20 20 22 61 20 49 4e  (example:  "a IN
293ba 20 28 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20   (SELECT * FROM 
293bb 74 61 62 6c 65 29 22 29 20 62 75 74 20 69 74 20  table)") but it 
293bc 68 61 73 20 6d 6f 72 65 20 74 68 61 6e 20 31 20  has more than 1 
293bd 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d 6e  result.** column
293be 2e 20 20 57 65 20 64 6f 20 74 68 69 73 20 69 6e  .  We do this in
293bf 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 62 65   a subroutine be
293c0 63 61 75 73 65 20 74 68 65 20 65 72 72 6f 72 20  cause the error 
293c1 6f 63 63 75 72 73 20 69 6e 20 6d 75 6c 74 69 70  occurs in multip
293c2 6c 65 0a 2a 2a 20 70 6c 61 63 65 73 2e 0a 2a 2f  le.** places..*/
293c3 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63  .static int chec
293c4 6b 46 6f 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53  kForMultiColumnS
293c5 65 6c 65 63 74 45 72 72 6f 72 28 0a 20 20 50 61  electError(.  Pa
293c6 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
293c7 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74     /* Parse cont
293c8 65 78 74 2e 20 2a 2f 0a 20 20 53 65 6c 65 63 74  ext. */.  Select
293c9 44 65 73 74 20 2a 70 44 65 73 74 2c 20 20 20 2f  Dest *pDest,   /
293ca 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 6f 66  * Destination of
293cb 20 53 45 4c 45 43 54 20 72 65 73 75 6c 74 73 20   SELECT results 
293cc 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 70 72 20 20  */.  int nExpr  
293cd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
293ce 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f  ber of result co
293cf 6c 75 6d 6e 73 20 72 65 74 75 72 6e 65 64 20 62  lumns returned b
293d0 79 20 53 45 4c 45 43 54 20 2a 2f 0a 29 7b 0a 20  y SELECT */.){. 
293d1 20 69 6e 74 20 65 44 65 73 74 20 3d 20 70 44 65   int eDest = pDe
293d2 73 74 2d 3e 65 44 65 73 74 3b 0a 20 20 69 66 28  st->eDest;.  if(
293d3 20 6e 45 78 70 72 3e 31 20 26 26 20 28 65 44 65   nExpr>1 && (eDe
293d4 73 74 3d 3d 53 52 54 5f 4d 65 6d 20 7c 7c 20 65  st==SRT_Mem || e
293d5 44 65 73 74 3d 3d 53 52 54 5f 53 65 74 29 20 29  Dest==SRT_Set) )
293d6 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
293d7 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6f  orMsg(pParse, "o
293d8 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 72 65 73  nly a single res
293d9 75 6c 74 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20  ult allowed for 
293da 22 0a 20 20 20 20 20 20 20 22 61 20 53 45 4c 45  ".       "a SELE
293db 43 54 20 74 68 61 74 20 69 73 20 70 61 72 74 20  CT that is part 
293dc 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  of an expression
293dd 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  ");.    return 1
293de 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
293df 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a  eturn 0;.  }.}..
293e0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
293e1 6e 65 20 67 65 6e 65 72 61 74 65 73 20 74 68 65  ne generates the
293e2 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 69 6e   code for the in
293e3 73 69 64 65 20 6f 66 20 74 68 65 20 69 6e 6e 65  side of the inne
293e4 72 20 6c 6f 6f 70 0a 2a 2a 20 6f 66 20 61 20 53  r loop.** of a S
293e5 45 4c 45 43 54 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ELECT..**.** If 
293e6 73 72 63 54 61 62 20 61 6e 64 20 6e 43 6f 6c 75  srcTab and nColu
293e7 6d 6e 20 61 72 65 20 62 6f 74 68 20 7a 65 72 6f  mn are both zero
293e8 2c 20 74 68 65 6e 20 74 68 65 20 70 45 4c 69 73  , then the pELis
293e9 74 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a  t expressions.**
293ea 20 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 69   are evaluated i
293eb 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 74 20 74  n order to get t
293ec 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 69 73  he data for this
293ed 20 72 6f 77 2e 20 20 49 66 20 6e 43 6f 6c 75 6d   row.  If nColum
293ee 6e 3e 30 0a 2a 2a 20 74 68 65 6e 20 64 61 74 61  n>0.** then data
293ef 20 69 73 20 70 75 6c 6c 65 64 20 66 72 6f 6d 20   is pulled from 
293f0 73 72 63 54 61 62 20 61 6e 64 20 70 45 4c 69 73  srcTab and pELis
293f1 74 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 74  t is used only t
293f2 6f 20 67 65 74 20 74 68 65 0a 2a 2a 20 64 61 74  o get the.** dat
293f3 61 74 79 70 65 73 20 66 6f 72 20 65 61 63 68 20  atypes for each 
293f4 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69  column..*/.stati
293f5 63 20 76 6f 69 64 20 73 65 6c 65 63 74 49 6e 6e  c void selectInn
293f6 65 72 4c 6f 6f 70 28 0a 20 20 50 61 72 73 65 20  erLoop(.  Parse 
293f7 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
293f8 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
293f9 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c  context */.  Sel
293fa 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20  ect *p,         
293fb 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70       /* The comp
293fc 6c 65 74 65 20 73 65 6c 65 63 74 20 73 74 61 74  lete select stat
293fd 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65  ement being code
293fe 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  d */.  ExprList 
293ff 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 20 20 2f  *pEList,       /
29400 2a 20 4c 69 73 74 20 6f 66 20 76 61 6c 75 65 73  * List of values
29401 20 62 65 69 6e 67 20 65 78 74 72 61 63 74 65 64   being extracted
29402 20 2a 2f 0a 20 20 69 6e 74 20 73 72 63 54 61 62   */.  int srcTab
29403 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
29404 20 50 75 6c 6c 20 64 61 74 61 20 66 72 6f 6d 20   Pull data from 
29405 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20  this table */.  
29406 69 6e 74 20 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20  int nColumn,    
29407 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
29408 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
29409 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65  the source table
2940a 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
2940b 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 2f 2a  pOrderBy,     /*
2940c 20 49 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 73 6f   If not NULL, so
2940d 72 74 20 72 65 73 75 6c 74 73 20 75 73 69 6e 67  rt results using
2940e 20 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20 20 69   this key */.  i
2940f 6e 74 20 64 69 73 74 69 6e 63 74 2c 20 20 20 20  nt distinct,    
29410 20 20 20 20 20 20 20 2f 2a 20 49 66 20 3e 3d 30         /* If >=0
29411 2c 20 6d 61 6b 65 20 73 75 72 65 20 72 65 73 75  , make sure resu
29412 6c 74 73 20 61 72 65 20 64 69 73 74 69 6e 63 74  lts are distinct
29413 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74   */.  SelectDest
29414 20 2a 70 44 65 73 74 2c 20 20 20 20 20 20 2f 2a   *pDest,      /*
29415 20 48 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 20   How to dispose 
29416 6f 66 20 74 68 65 20 72 65 73 75 6c 74 73 20 2a  of the results *
29417 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75  /.  int iContinu
29418 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a  e,          /* J
29419 75 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74  ump here to cont
2941a 69 6e 75 65 20 77 69 74 68 20 6e 65 78 74 20 72  inue with next r
2941b 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65  ow */.  int iBre
2941c 61 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ak              
2941d 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20  /* Jump here to 
2941e 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65  break out of the
2941f 20 69 6e 6e 65 72 20 6c 6f 6f 70 20 2a 2f 0a 29   inner loop */.)
29420 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  {.  Vdbe *v = pP
29421 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
29422 6e 74 20 69 3b 0a 20 20 69 6e 74 20 68 61 73 44  nt i;.  int hasD
29423 69 73 74 69 6e 63 74 3b 20 20 20 20 20 20 20 20  istinct;        
29424 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 44  /* True if the D
29425 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20  ISTINCT keyword 
29426 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20  is present */.  
29427 69 6e 74 20 72 65 67 52 65 73 75 6c 74 3b 20 20  int regResult;  
29428 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
29429 74 61 72 74 20 6f 66 20 6d 65 6d 6f 72 79 20 68  tart of memory h
2942a 6f 6c 64 69 6e 67 20 72 65 73 75 6c 74 20 73 65  olding result se
2942b 74 20 2a 2f 0a 20 20 69 6e 74 20 65 44 65 73 74  t */.  int eDest
2942c 20 3d 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3b   = pDest->eDest;
2942d 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69 73     /* How to dis
2942e 70 6f 73 65 20 6f 66 20 72 65 73 75 6c 74 73 20  pose of results 
2942f 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 6d 20 3d  */.  int iParm =
29430 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 3b 20 20   pDest->iParm;  
29431 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65   /* First argume
29432 6e 74 20 74 6f 20 64 69 73 70 6f 73 61 6c 20 6d  nt to disposal m
29433 65 74 68 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  ethod */.  int n
29434 52 65 73 75 6c 74 43 6f 6c 3b 20 20 20 20 20 20  ResultCol;      
29435 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
29436 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d   of result colum
29437 6e 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  ns */..  assert(
29438 20 76 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45   v );.  if( NEVE
29439 52 28 76 3d 3d 30 29 20 29 20 72 65 74 75 72 6e  R(v==0) ) return
2943a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c 69  ;.  assert( pELi
2943b 73 74 21 3d 30 20 29 3b 0a 20 20 68 61 73 44 69  st!=0 );.  hasDi
2943c 73 74 69 6e 63 74 20 3d 20 64 69 73 74 69 6e 63  stinct = distinc
2943d 74 3e 3d 30 3b 0a 20 20 69 66 28 20 70 4f 72 64  t>=0;.  if( pOrd
2943e 65 72 42 79 3d 3d 30 20 26 26 20 21 68 61 73 44  erBy==0 && !hasD
2943f 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 63  istinct ){.    c
29440 6f 64 65 4f 66 66 73 65 74 28 76 2c 20 70 2c 20  odeOffset(v, p, 
29441 69 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 7d 0a  iContinue);.  }.
29442 0a 20 20 2f 2a 20 50 75 6c 6c 20 74 68 65 20 72  .  /* Pull the r
29443 65 71 75 65 73 74 65 64 20 63 6f 6c 75 6d 6e 73  equested columns
29444 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 43 6f  ..  */.  if( nCo
29445 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20 20 6e 52  lumn>0 ){.    nR
29446 65 73 75 6c 74 43 6f 6c 20 3d 20 6e 43 6f 6c 75  esultCol = nColu
29447 6d 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  mn;.  }else{.   
29448 20 6e 52 65 73 75 6c 74 43 6f 6c 20 3d 20 70 45   nResultCol = pE
29449 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 7d  List->nExpr;.  }
2944a 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 69 4d  .  if( pDest->iM
2944b 65 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44 65  em==0 ){.    pDe
2944c 73 74 2d 3e 69 4d 65 6d 20 3d 20 70 50 61 72 73  st->iMem = pPars
2944d 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 20 20 70  e->nMem+1;.    p
2944e 44 65 73 74 2d 3e 6e 4d 65 6d 20 3d 20 6e 52 65  Dest->nMem = nRe
2944f 73 75 6c 74 43 6f 6c 3b 0a 20 20 20 20 70 50 61  sultCol;.    pPa
29450 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e 52 65  rse->nMem += nRe
29451 73 75 6c 74 43 6f 6c 3b 0a 20 20 7d 65 6c 73 65  sultCol;.  }else
29452 7b 20 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  { .    assert( p
29453 44 65 73 74 2d 3e 6e 4d 65 6d 3d 3d 6e 52 65 73  Dest->nMem==nRes
29454 75 6c 74 43 6f 6c 20 29 3b 0a 20 20 7d 0a 20 20  ultCol );.  }.  
29455 72 65 67 52 65 73 75 6c 74 20 3d 20 70 44 65 73  regResult = pDes
29456 74 2d 3e 69 4d 65 6d 3b 0a 20 20 69 66 28 20 6e  t->iMem;.  if( n
29457 43 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20 20  Column>0 ){.    
29458 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75  for(i=0; i<nColu
29459 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  mn; i++){.      
2945a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2945b 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
2945c 73 72 63 54 61 62 2c 20 69 2c 20 72 65 67 52 65  srcTab, i, regRe
2945d 73 75 6c 74 2b 69 29 3b 0a 20 20 20 20 7d 0a 20  sult+i);.    }. 
2945e 20 7d 65 6c 73 65 20 69 66 28 20 65 44 65 73 74   }else if( eDest
2945f 21 3d 53 52 54 5f 45 78 69 73 74 73 20 29 7b 0a  !=SRT_Exists ){.
29460 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 64 65      /* If the de
29461 73 74 69 6e 61 74 69 6f 6e 20 69 73 20 61 6e 20  stination is an 
29462 45 58 49 53 54 53 28 2e 2e 2e 29 20 65 78 70 72  EXISTS(...) expr
29463 65 73 73 69 6f 6e 2c 20 74 68 65 20 61 63 74 75  ession, the actu
29464 61 6c 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 73  al.    ** values
29465 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65   returned by the
29466 20 53 45 4c 45 43 54 20 61 72 65 20 6e 6f 74 20   SELECT are not 
29467 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f  required..    */
29468 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
29469 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72 73  CacheClear(pPars
2946a 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  e);.    sqlite3E
2946b 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28  xprCodeExprList(
2946c 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20  pParse, pEList, 
2946d 72 65 67 52 65 73 75 6c 74 2c 20 65 44 65 73 74  regResult, eDest
2946e 3d 3d 53 52 54 5f 4f 75 74 70 75 74 29 3b 0a 20  ==SRT_Output);. 
2946f 20 7d 0a 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 6e   }.  nColumn = n
29470 52 65 73 75 6c 74 43 6f 6c 3b 0a 0a 20 20 2f 2a  ResultCol;..  /*
29471 20 49 66 20 74 68 65 20 44 49 53 54 49 4e 43 54   If the DISTINCT
29472 20 6b 65 79 77 6f 72 64 20 77 61 73 20 70 72 65   keyword was pre
29473 73 65 6e 74 20 6f 6e 20 74 68 65 20 53 45 4c 45  sent on the SELE
29474 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a  CT statement.  *
29475 2a 20 61 6e 64 20 74 68 69 73 20 72 6f 77 20 68  * and this row h
29476 61 73 20 62 65 65 6e 20 73 65 65 6e 20 62 65 66  as been seen bef
29477 6f 72 65 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74  ore, then do not
29478 20 6d 61 6b 65 20 74 68 69 73 20 72 6f 77 0a 20   make this row. 
29479 20 2a 2a 20 70 61 72 74 20 6f 66 20 74 68 65 20   ** part of the 
2947a 72 65 73 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 69  result..  */.  i
2947b 66 28 20 68 61 73 44 69 73 74 69 6e 63 74 20 29  f( hasDistinct )
2947c 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45  {.    assert( pE
2947d 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 61  List!=0 );.    a
2947e 73 73 65 72 74 28 20 70 45 4c 69 73 74 2d 3e 6e  ssert( pEList->n
2947f 45 78 70 72 3d 3d 6e 43 6f 6c 75 6d 6e 20 29 3b  Expr==nColumn );
29480 0a 20 20 20 20 63 6f 64 65 44 69 73 74 69 6e 63  .    codeDistinc
29481 74 28 70 50 61 72 73 65 2c 20 64 69 73 74 69 6e  t(pParse, distin
29482 63 74 2c 20 69 43 6f 6e 74 69 6e 75 65 2c 20 6e  ct, iContinue, n
29483 43 6f 6c 75 6d 6e 2c 20 72 65 67 52 65 73 75 6c  Column, regResul
29484 74 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 72 64  t);.    if( pOrd
29485 65 72 42 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20  erBy==0 ){.     
29486 20 63 6f 64 65 4f 66 66 73 65 74 28 76 2c 20 70   codeOffset(v, p
29487 2c 20 69 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20  , iContinue);.  
29488 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 63    }.  }..  if( c
29489 68 65 63 6b 46 6f 72 4d 75 6c 74 69 43 6f 6c 75  heckForMultiColu
2948a 6d 6e 53 65 6c 65 63 74 45 72 72 6f 72 28 70 50  mnSelectError(pP
2948b 61 72 73 65 2c 20 70 44 65 73 74 2c 20 70 45 4c  arse, pDest, pEL
2948c 69 73 74 2d 3e 6e 45 78 70 72 29 20 29 7b 0a 20  ist->nExpr) ){. 
2948d 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
2948e 20 20 73 77 69 74 63 68 28 20 65 44 65 73 74 20    switch( eDest 
2948f 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 69  ){.    /* In thi
29490 73 20 6d 6f 64 65 2c 20 77 72 69 74 65 20 65 61  s mode, write ea
29491 63 68 20 71 75 65 72 79 20 72 65 73 75 6c 74 20  ch query result 
29492 74 6f 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68  to the key of th
29493 65 20 74 65 6d 70 6f 72 61 72 79 0a 20 20 20 20  e temporary.    
29494 2a 2a 20 74 61 62 6c 65 20 69 50 61 72 6d 2e 0a  ** table iParm..
29495 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53      */.#ifndef S
29496 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f  QLITE_OMIT_COMPO
29497 55 4e 44 5f 53 45 4c 45 43 54 0a 20 20 20 20 63  UND_SELECT.    c
29498 61 73 65 20 53 52 54 5f 55 6e 69 6f 6e 3a 20 7b  ase SRT_Union: {
29499 0a 20 20 20 20 20 20 69 6e 74 20 72 31 3b 0a 20  .      int r1;. 
2949a 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65       r1 = sqlite
2949b 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
2949c 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  se);.      sqlit
2949d 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2949e 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72  OP_MakeRecord, r
2949f 65 67 52 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d  egResult, nColum
294a0 6e 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71  n, r1);.      sq
294a1 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
294a2 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c  v, OP_IdxInsert,
294a3 20 69 50 61 72 6d 2c 20 72 31 29 3b 0a 20 20 20   iParm, r1);.   
294a4 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
294a5 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
294a6 20 72 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61   r1);.      brea
294a7 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
294a8 20 43 6f 6e 73 74 72 75 63 74 20 61 20 72 65 63   Construct a rec
294a9 6f 72 64 20 66 72 6f 6d 20 74 68 65 20 71 75 65  ord from the que
294aa 72 79 20 72 65 73 75 6c 74 2c 20 62 75 74 20 69  ry result, but i
294ab 6e 73 74 65 61 64 20 6f 66 0a 20 20 20 20 2a 2a  nstead of.    **
294ac 20 73 61 76 69 6e 67 20 74 68 61 74 20 72 65 63   saving that rec
294ad 6f 72 64 2c 20 75 73 65 20 69 74 20 61 73 20 61  ord, use it as a
294ae 20 6b 65 79 20 74 6f 20 64 65 6c 65 74 65 20 65   key to delete e
294af 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 0a 20 20 20  lements from.   
294b0 20 2a 2a 20 74 68 65 20 74 65 6d 70 6f 72 61 72   ** the temporar
294b1 79 20 74 61 62 6c 65 20 69 50 61 72 6d 2e 0a 20  y table iParm.. 
294b2 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53     */.    case S
294b3 52 54 5f 45 78 63 65 70 74 3a 20 7b 0a 20 20 20  RT_Except: {.   
294b4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
294b5 64 4f 70 33 28 76 2c 20 4f 50 5f 49 64 78 44 65  dOp3(v, OP_IdxDe
294b6 6c 65 74 65 2c 20 69 50 61 72 6d 2c 20 72 65 67  lete, iParm, reg
294b7 52 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 29  Result, nColumn)
294b8 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
294b9 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
294ba 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 72 65   /* Store the re
294bb 73 75 6c 74 20 61 73 20 64 61 74 61 20 75 73 69  sult as data usi
294bc 6e 67 20 61 20 75 6e 69 71 75 65 20 6b 65 79 2e  ng a unique key.
294bd 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
294be 20 53 52 54 5f 54 61 62 6c 65 3a 0a 20 20 20 20   SRT_Table:.    
294bf 63 61 73 65 20 53 52 54 5f 45 70 68 65 6d 54 61  case SRT_EphemTa
294c0 62 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72  b: {.      int r
294c1 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  1 = sqlite3GetTe
294c2 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
294c3 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 65       testcase( e
294c4 44 65 73 74 3d 3d 53 52 54 5f 54 61 62 6c 65 20  Dest==SRT_Table 
294c5 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
294c6 65 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 45 70  e( eDest==SRT_Ep
294c7 68 65 6d 54 61 62 20 29 3b 0a 20 20 20 20 20 20  hemTab );.      
294c8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
294c9 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  3(v, OP_MakeReco
294ca 72 64 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 6e  rd, regResult, n
294cb 43 6f 6c 75 6d 6e 2c 20 72 31 29 3b 0a 20 20 20  Column, r1);.   
294cc 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20     if( pOrderBy 
294cd 29 7b 0a 20 20 20 20 20 20 20 20 70 75 73 68 4f  ){.        pushO
294ce 6e 74 6f 53 6f 72 74 65 72 28 70 50 61 72 73 65  ntoSorter(pParse
294cf 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 2c 20 72  , pOrderBy, p, r
294d0 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  1);.      }else{
294d1 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 32 20  .        int r2 
294d2 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
294d3 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
294d4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
294d5 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77  AddOp2(v, OP_New
294d6 52 6f 77 69 64 2c 20 69 50 61 72 6d 2c 20 72 32  Rowid, iParm, r2
294d7 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
294d8 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
294d9 4f 50 5f 49 6e 73 65 72 74 2c 20 69 50 61 72 6d  OP_Insert, iParm
294da 2c 20 72 31 2c 20 72 32 29 3b 0a 20 20 20 20 20  , r1, r2);.     
294db 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
294dc 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47  angeP5(v, OPFLAG
294dd 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 20 20  _APPEND);.      
294de 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
294df 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
294e0 72 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  r2);.      }.   
294e1 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
294e2 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
294e3 20 72 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61   r1);.      brea
294e4 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65  k;.    }..#ifnde
294e5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  f SQLITE_OMIT_SU
294e6 42 51 55 45 52 59 0a 20 20 20 20 2f 2a 20 49 66  BQUERY.    /* If
294e7 20 77 65 20 61 72 65 20 63 72 65 61 74 69 6e 67   we are creating
294e8 20 61 20 73 65 74 20 66 6f 72 20 61 6e 20 22 65   a set for an "e
294e9 78 70 72 20 49 4e 20 28 53 45 4c 45 43 54 20 2e  xpr IN (SELECT .
294ea 2e 2e 29 22 20 63 6f 6e 73 74 72 75 63 74 2c 0a  ..)" construct,.
294eb 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 72      ** then ther
294ec 65 20 73 68 6f 75 6c 64 20 62 65 20 61 20 73 69  e should be a si
294ed 6e 67 6c 65 20 69 74 65 6d 20 6f 6e 20 74 68 65  ngle item on the
294ee 20 73 74 61 63 6b 2e 20 20 57 72 69 74 65 20 74   stack.  Write t
294ef 68 69 73 0a 20 20 20 20 2a 2a 20 69 74 65 6d 20  his.    ** item 
294f0 69 6e 74 6f 20 74 68 65 20 73 65 74 20 74 61 62  into the set tab
294f1 6c 65 20 77 69 74 68 20 62 6f 67 75 73 20 64 61  le with bogus da
294f2 74 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  ta..    */.    c
294f3 61 73 65 20 53 52 54 5f 53 65 74 3a 20 7b 0a 20  ase SRT_Set: {. 
294f4 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 6f       assert( nCo
294f5 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20  lumn==1 );.     
294f6 20 70 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 73   p->affinity = s
294f7 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66  qlite3CompareAff
294f8 69 6e 69 74 79 28 70 45 4c 69 73 74 2d 3e 61 5b  inity(pEList->a[
294f9 30 5d 2e 70 45 78 70 72 2c 20 70 44 65 73 74 2d  0].pExpr, pDest-
294fa 3e 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 20 20  >affinity);.    
294fb 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29    if( pOrderBy )
294fc 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 74 20  {.        /* At 
294fd 66 69 72 73 74 20 67 6c 61 6e 63 65 20 79 6f 75  first glance you
294fe 20 77 6f 75 6c 64 20 74 68 69 6e 6b 20 77 65 20   would think we 
294ff 63 6f 75 6c 64 20 6f 70 74 69 6d 69 7a 65 20 6f  could optimize o
29500 75 74 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  ut the.        *
29501 2a 20 4f 52 44 45 52 20 42 59 20 69 6e 20 74 68  * ORDER BY in th
29502 69 73 20 63 61 73 65 20 73 69 6e 63 65 20 74 68  is case since th
29503 65 20 6f 72 64 65 72 20 6f 66 20 65 6e 74 72 69  e order of entri
29504 65 73 20 69 6e 20 74 68 65 20 73 65 74 0a 20 20  es in the set.  
29505 20 20 20 20 20 20 2a 2a 20 64 6f 65 73 20 6e 6f        ** does no
29506 74 20 6d 61 74 74 65 72 2e 20 20 42 75 74 20 74  t matter.  But t
29507 68 65 72 65 20 6d 69 67 68 74 20 62 65 20 61 20  here might be a 
29508 4c 49 4d 49 54 20 63 6c 61 75 73 65 2c 20 69 6e  LIMIT clause, in
29509 20 77 68 69 63 68 0a 20 20 20 20 20 20 20 20 2a   which.        *
2950a 2a 20 63 61 73 65 20 74 68 65 20 6f 72 64 65 72  * case the order
2950b 20 64 6f 65 73 20 6d 61 74 74 65 72 20 2a 2f 0a   does matter */.
2950c 20 20 20 20 20 20 20 20 70 75 73 68 4f 6e 74 6f          pushOnto
2950d 53 6f 72 74 65 72 28 70 50 61 72 73 65 2c 20 70  Sorter(pParse, p
2950e 4f 72 64 65 72 42 79 2c 20 70 2c 20 72 65 67 52  OrderBy, p, regR
2950f 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 7d 65  esult);.      }e
29510 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  lse{.        int
29511 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
29512 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
29513 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
29514 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
29515 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67  _MakeRecord, reg
29516 52 65 73 75 6c 74 2c 20 31 2c 20 72 31 2c 20 26  Result, 1, r1, &
29517 70 2d 3e 61 66 66 69 6e 69 74 79 2c 20 31 29 3b  p->affinity, 1);
29518 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
29519 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
2951a 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
2951b 72 65 67 52 65 73 75 6c 74 2c 20 31 29 3b 0a 20  regResult, 1);. 
2951c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2951d 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2951e 64 78 49 6e 73 65 72 74 2c 20 69 50 61 72 6d 2c  dxInsert, iParm,
2951f 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71   r1);.        sq
29520 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
29521 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
29522 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
29523 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
29524 20 2f 2a 20 49 66 20 61 6e 79 20 72 6f 77 20 65   /* If any row e
29525 78 69 73 74 20 69 6e 20 74 68 65 20 72 65 73 75  xist in the resu
29526 6c 74 20 73 65 74 2c 20 72 65 63 6f 72 64 20 74  lt set, record t
29527 68 61 74 20 66 61 63 74 20 61 6e 64 20 61 62 6f  hat fact and abo
29528 72 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  rt..    */.    c
29529 61 73 65 20 53 52 54 5f 45 78 69 73 74 73 3a 20  ase SRT_Exists: 
2952a 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
2952b 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2952c 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 50 61 72  Integer, 1, iPar
2952d 6d 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65  m);.      /* The
2952e 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 77 69   LIMIT clause wi
2952f 6c 6c 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65  ll terminate the
29530 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a   loop for us */.
29531 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
29532 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   }..    /* If th
29533 69 73 20 69 73 20 61 20 73 63 61 6c 61 72 20 73  is is a scalar s
29534 65 6c 65 63 74 20 74 68 61 74 20 69 73 20 70 61  elect that is pa
29535 72 74 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73  rt of an express
29536 69 6f 6e 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a  ion, then.    **
29537 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   store the resul
29538 74 73 20 69 6e 20 74 68 65 20 61 70 70 72 6f 70  ts in the approp
29539 72 69 61 74 65 20 6d 65 6d 6f 72 79 20 63 65 6c  riate memory cel
2953a 6c 20 61 6e 64 20 62 72 65 61 6b 20 6f 75 74 0a  l and break out.
2953b 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 73 63      ** of the sc
2953c 61 6e 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a  an loop..    */.
2953d 20 20 20 20 63 61 73 65 20 53 52 54 5f 4d 65 6d      case SRT_Mem
2953e 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  : {.      assert
2953f 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a  ( nColumn==1 );.
29540 20 20 20 20 20 20 69 66 28 20 70 4f 72 64 65 72        if( pOrder
29541 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20 70 75  By ){.        pu
29542 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 70 50 61  shOntoSorter(pPa
29543 72 73 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 70  rse, pOrderBy, p
29544 2c 20 72 65 67 52 65 73 75 6c 74 29 3b 0a 20 20  , regResult);.  
29545 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
29546 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
29547 64 65 4d 6f 76 65 28 70 50 61 72 73 65 2c 20 72  deMove(pParse, r
29548 65 67 52 65 73 75 6c 74 2c 20 69 50 61 72 6d 2c  egResult, iParm,
29549 20 31 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20   1);.        /* 
2954a 54 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65  The LIMIT clause
2954b 20 77 69 6c 6c 20 6a 75 6d 70 20 6f 75 74 20 6f   will jump out o
2954c 66 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75  f the loop for u
2954d 73 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20  s */.      }.   
2954e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2954f 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65  #endif /* #ifnde
29550 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  f SQLITE_OMIT_SU
29551 42 51 55 45 52 59 20 2a 2f 0a 0a 20 20 20 20 2f  BQUERY */..    /
29552 2a 20 53 65 6e 64 20 74 68 65 20 64 61 74 61 20  * Send the data 
29553 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  to the callback 
29554 66 75 6e 63 74 69 6f 6e 20 6f 72 20 74 6f 20 61  function or to a
29555 20 73 75 62 72 6f 75 74 69 6e 65 2e 20 20 49 6e   subroutine.  In
29556 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 73 65   the.    ** case
29557 20 6f 66 20 61 20 73 75 62 72 6f 75 74 69 6e 65   of a subroutine
29558 2c 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65  , the subroutine
29559 20 69 74 73 65 6c 66 20 69 73 20 72 65 73 70 6f   itself is respo
2955a 6e 73 69 62 6c 65 20 66 6f 72 0a 20 20 20 20 2a  nsible for.    *
2955b 2a 20 70 6f 70 70 69 6e 67 20 74 68 65 20 64 61  * popping the da
2955c 74 61 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63  ta from the stac
2955d 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  k..    */.    ca
2955e 73 65 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65  se SRT_Coroutine
2955f 3a 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 4f  :.    case SRT_O
29560 75 74 70 75 74 3a 20 7b 0a 20 20 20 20 20 20 74  utput: {.      t
29561 65 73 74 63 61 73 65 28 20 65 44 65 73 74 3d 3d  estcase( eDest==
29562 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 29 3b  SRT_Coroutine );
29563 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
29564 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70   eDest==SRT_Outp
29565 75 74 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ut );.      if( 
29566 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
29567 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c      int r1 = sql
29568 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
29569 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20  Parse);.        
2956a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2956b 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  3(v, OP_MakeReco
2956c 72 64 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 6e  rd, regResult, n
2956d 43 6f 6c 75 6d 6e 2c 20 72 31 29 3b 0a 20 20 20  Column, r1);.   
2956e 20 20 20 20 20 70 75 73 68 4f 6e 74 6f 53 6f 72       pushOntoSor
2956f 74 65 72 28 70 50 61 72 73 65 2c 20 70 4f 72 64  ter(pParse, pOrd
29570 65 72 42 79 2c 20 70 2c 20 72 31 29 3b 0a 20 20  erBy, p, r1);.  
29571 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c        sqlite3Rel
29572 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
29573 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 7d  se, r1);.      }
29574 65 6c 73 65 20 69 66 28 20 65 44 65 73 74 3d 3d  else if( eDest==
29575 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 29 7b  SRT_Coroutine ){
29576 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
29577 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
29578 5f 59 69 65 6c 64 2c 20 70 44 65 73 74 2d 3e 69  _Yield, pDest->i
29579 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 7d 65 6c  Parm);.      }el
2957a 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
2957b 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2957c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 72   OP_ResultRow, r
2957d 65 67 52 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d  egResult, nColum
2957e 6e 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  n);.        sqli
2957f 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69  te3ExprCacheAffi
29580 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73  nityChange(pPars
29581 65 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 6e 43  e, regResult, nC
29582 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 7d 0a  olumn);.      }.
29583 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
29584 20 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64   }..#if !defined
29585 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49  (SQLITE_OMIT_TRI
29586 47 47 45 52 29 0a 20 20 20 20 2f 2a 20 44 69 73  GGER).    /* Dis
29587 63 61 72 64 20 74 68 65 20 72 65 73 75 6c 74 73  card the results
29588 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20  .  This is used 
29589 66 6f 72 20 53 45 4c 45 43 54 20 73 74 61 74 65  for SELECT state
2958a 6d 65 6e 74 73 20 69 6e 73 69 64 65 0a 20 20 20  ments inside.   
2958b 20 2a 2a 20 74 68 65 20 62 6f 64 79 20 6f 66 20   ** the body of 
2958c 61 20 54 52 49 47 47 45 52 2e 20 20 54 68 65 20  a TRIGGER.  The 
2958d 70 75 72 70 6f 73 65 20 6f 66 20 73 75 63 68 20  purpose of such 
2958e 73 65 6c 65 63 74 73 20 69 73 20 74 6f 20 63 61  selects is to ca
2958f 6c 6c 0a 20 20 20 20 2a 2a 20 75 73 65 72 2d 64  ll.    ** user-d
29590 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73  efined functions
29591 20 74 68 61 74 20 68 61 76 65 20 73 69 64 65 20   that have side 
29592 65 66 66 65 63 74 73 2e 20 20 57 65 20 64 6f 20  effects.  We do 
29593 6e 6f 74 20 63 61 72 65 0a 20 20 20 20 2a 2a 20  not care.    ** 
29594 61 62 6f 75 74 20 74 68 65 20 61 63 74 75 61 6c  about the actual
29595 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20   results of the 
29596 73 65 6c 65 63 74 2e 0a 20 20 20 20 2a 2f 0a 20  select..    */. 
29597 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
29598 20 20 20 20 61 73 73 65 72 74 28 20 65 44 65 73      assert( eDes
29599 74 3d 3d 53 52 54 5f 44 69 73 63 61 72 64 20 29  t==SRT_Discard )
2959a 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
2959b 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
2959c 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74 68  .  /* Jump to th
2959d 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 6f 6f  e end of the loo
2959e 70 20 69 66 20 74 68 65 20 4c 49 4d 49 54 20 69  p if the LIMIT i
2959f 73 20 72 65 61 63 68 65 64 2e 0a 20 20 2a 2f 0a  s reached..  */.
295a0 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20    if( p->iLimit 
295a1 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
295a2 4f 72 64 65 72 42 79 3d 3d 30 20 29 3b 20 20 2f  OrderBy==0 );  /
295a3 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6e  * If there is an
295a4 20 4f 52 44 45 52 20 42 59 2c 20 74 68 65 20 63   ORDER BY, the c
295a5 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20 20 20 20  all to.         
295a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
295a7 20 20 20 2a 2a 20 70 75 73 68 4f 6e 74 6f 53 6f     ** pushOntoSo
295a8 72 74 65 72 28 29 20 77 6f 75 6c 64 20 68 61 76  rter() would hav
295a9 65 20 63 6c 65 61 72 65 64 20 70 2d 3e 69 4c 69  e cleared p->iLi
295aa 6d 69 74 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  mit */.    sqlit
295ab 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
295ac 4f 50 5f 41 64 64 49 6d 6d 2c 20 70 2d 3e 69 4c  OP_AddImm, p->iL
295ad 69 6d 69 74 2c 20 2d 31 29 3b 0a 20 20 20 20 73  imit, -1);.    s
295ae 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
295af 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 70  (v, OP_IfZero, p
295b0 2d 3e 69 4c 69 6d 69 74 2c 20 69 42 72 65 61 6b  ->iLimit, iBreak
295b1 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
295b2 47 69 76 65 6e 20 61 6e 20 65 78 70 72 65 73 73  Given an express
295b3 69 6f 6e 20 6c 69 73 74 2c 20 67 65 6e 65 72 61  ion list, genera
295b4 74 65 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72  te a KeyInfo str
295b5 75 63 74 75 72 65 20 74 68 61 74 20 72 65 63 6f  ucture that reco
295b6 72 64 73 0a 2a 2a 20 74 68 65 20 63 6f 6c 6c 61  rds.** the colla
295b7 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f  ting sequence fo
295b8 72 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f  r each expressio
295b9 6e 20 69 6e 20 74 68 61 74 20 65 78 70 72 65 73  n in that expres
295ba 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a  sion list..**.**
295bb 20 49 66 20 74 68 65 20 45 78 70 72 4c 69 73 74   If the ExprList
295bc 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20   is an ORDER BY 
295bd 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  or GROUP BY clau
295be 73 65 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  se then the resu
295bf 6c 74 69 6e 67 0a 2a 2a 20 4b 65 79 49 6e 66 6f  lting.** KeyInfo
295c0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 70   structure is ap
295c1 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 69 6e  propriate for in
295c2 69 74 69 61 6c 69 7a 69 6e 67 20 61 20 76 69 72  itializing a vir
295c3 74 75 61 6c 20 69 6e 64 65 78 20 74 6f 0a 2a 2a  tual index to.**
295c4 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 61 74 20   implement that 
295c5 63 6c 61 75 73 65 2e 20 20 49 66 20 74 68 65 20  clause.  If the 
295c6 45 78 70 72 4c 69 73 74 20 69 73 20 74 68 65 20  ExprList is the 
295c7 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20  result set of a 
295c8 53 45 4c 45 43 54 0a 2a 2a 20 74 68 65 6e 20 74  SELECT.** then t
295c9 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63  he KeyInfo struc
295ca 74 75 72 65 20 69 73 20 61 70 70 72 6f 70 72 69  ture is appropri
295cb 61 74 65 20 66 6f 72 20 69 6e 69 74 69 61 6c 69  ate for initiali
295cc 7a 69 6e 67 20 61 20 76 69 72 74 75 61 6c 0a 2a  zing a virtual.*
295cd 2a 20 69 6e 64 65 78 20 74 6f 20 69 6d 70 6c 65  * index to imple
295ce 6d 65 6e 74 20 61 20 44 49 53 54 49 4e 43 54 20  ment a DISTINCT 
295cf 74 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63  test..**.** Spac
295d0 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 4b 65  e to hold the Ke
295d1 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
295d2 69 73 20 6f 62 74 61 69 6e 20 66 72 6f 6d 20 6d  is obtain from m
295d3 61 6c 6c 6f 63 2e 20 20 54 68 65 20 63 61 6c 6c  alloc.  The call
295d4 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  ing.** function 
295d5 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
295d6 6f 72 20 73 65 65 69 6e 67 20 74 68 61 74 20 74  or seeing that t
295d7 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73  his structure is
295d8 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20 66   eventually.** f
295d9 72 65 65 64 2e 20 20 41 64 64 20 74 68 65 20 4b  reed.  Add the K
295da 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  eyInfo structure
295db 20 74 6f 20 74 68 65 20 50 34 20 66 69 65 6c 64   to the P4 field
295dc 20 6f 66 20 61 6e 20 6f 70 63 6f 64 65 20 75 73   of an opcode us
295dd 69 6e 67 0a 2a 2a 20 50 34 5f 4b 45 59 49 4e 46  ing.** P4_KEYINF
295de 4f 5f 48 41 4e 44 4f 46 46 20 69 73 20 74 68 65  O_HANDOFF is the
295df 20 75 73 75 61 6c 20 77 61 79 20 6f 66 20 64 65   usual way of de
295e0 61 6c 69 6e 67 20 77 69 74 68 20 74 68 69 73 2e  aling with this.
295e1 0a 2a 2f 0a 73 74 61 74 69 63 20 4b 65 79 49 6e  .*/.static KeyIn
295e2 66 6f 20 2a 6b 65 79 49 6e 66 6f 46 72 6f 6d 45  fo *keyInfoFromE
295e3 78 70 72 4c 69 73 74 28 50 61 72 73 65 20 2a 70  xprList(Parse *p
295e4 50 61 72 73 65 2c 20 45 78 70 72 4c 69 73 74 20  Parse, ExprList 
295e5 2a 70 4c 69 73 74 29 7b 0a 20 20 73 71 6c 69 74  *pList){.  sqlit
295e6 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
295e7 3e 64 62 3b 0a 20 20 69 6e 74 20 6e 45 78 70 72  >db;.  int nExpr
295e8 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 49 6e  ;.  KeyInfo *pIn
295e9 66 6f 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70  fo;.  struct Exp
295ea 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  rList_item *pIte
295eb 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 6e  m;.  int i;..  n
295ec 45 78 70 72 20 3d 20 70 4c 69 73 74 2d 3e 6e 45  Expr = pList->nE
295ed 78 70 72 3b 0a 20 20 70 49 6e 66 6f 20 3d 20 73  xpr;.  pInfo = s
295ee 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
295ef 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70  ro(db, sizeof(*p
295f0 49 6e 66 6f 29 20 2b 20 6e 45 78 70 72 2a 28 73  Info) + nExpr*(s
295f1 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 2b  izeof(CollSeq*)+
295f2 31 29 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 66  1) );.  if( pInf
295f3 6f 20 29 7b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e  o ){.    pInfo->
295f4 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38  aSortOrder = (u8
295f5 2a 29 26 70 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b  *)&pInfo->aColl[
295f6 6e 45 78 70 72 5d 3b 0a 20 20 20 20 70 49 6e 66  nExpr];.    pInf
295f7 6f 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36  o->nField = (u16
295f8 29 6e 45 78 70 72 3b 0a 20 20 20 20 70 49 6e 66  )nExpr;.    pInf
295f9 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 64 62 29  o->enc = ENC(db)
295fa 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 64 62 20  ;.    pInfo->db 
295fb 3d 20 64 62 3b 0a 20 20 20 20 66 6f 72 28 69 3d  = db;.    for(i=
295fc 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e  0, pItem=pList->
295fd 61 3b 20 69 3c 6e 45 78 70 72 3b 20 69 2b 2b 2c  a; i<nExpr; i++,
295fe 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
295ff 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
29600 0a 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73  .      pColl = s
29601 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65  qlite3ExprCollSe
29602 71 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d  q(pParse, pItem-
29603 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 69  >pExpr);.      i
29604 66 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  f( !pColl ){.   
29605 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 64 62 2d       pColl = db-
29606 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20 20  >pDfltColl;.    
29607 20 20 7d 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d    }.      pInfo-
29608 3e 61 43 6f 6c 6c 5b 69 5d 20 3d 20 70 43 6f 6c  >aColl[i] = pCol
29609 6c 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e  l;.      pInfo->
2960a 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20  aSortOrder[i] = 
2960b 70 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72  pItem->sortOrder
2960c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
2960d 74 75 72 6e 20 70 49 6e 66 6f 3b 0a 7d 0a 0a 0a  turn pInfo;.}...
2960e 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 6e  /*.** If the inn
2960f 65 72 20 6c 6f 6f 70 20 77 61 73 20 67 65 6e 65  er loop was gene
29610 72 61 74 65 64 20 75 73 69 6e 67 20 61 20 6e 6f  rated using a no
29611 6e 2d 6e 75 6c 6c 20 70 4f 72 64 65 72 42 79 20  n-null pOrderBy 
29612 61 72 67 75 6d 65 6e 74 2c 0a 2a 2a 20 74 68 65  argument,.** the
29613 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 77 65  n the results we
29614 72 65 20 70 6c 61 63 65 64 20 69 6e 20 61 20 73  re placed in a s
29615 6f 72 74 65 72 2e 20 20 41 66 74 65 72 20 74 68  orter.  After th
29616 65 20 6c 6f 6f 70 20 69 73 20 74 65 72 6d 69 6e  e loop is termin
29617 61 74 65 64 0a 2a 2a 20 77 65 20 6e 65 65 64 20  ated.** we need 
29618 74 6f 20 72 75 6e 20 74 68 65 20 73 6f 72 74 65  to run the sorte
29619 72 20 61 6e 64 20 6f 75 74 70 75 74 20 74 68 65  r and output the
2961a 20 72 65 73 75 6c 74 73 2e 20 20 54 68 65 20 66   results.  The f
2961b 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 72 6f 75 74  ollowing.** rout
2961c 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 74 68  ine generates th
2961d 65 20 63 6f 64 65 20 6e 65 65 64 65 64 20 74 6f  e code needed to
2961e 20 64 6f 20 74 68 61 74 2e 0a 2a 2f 0a 73 74 61   do that..*/.sta
2961f 74 69 63 20 76 6f 69 64 20 67 65 6e 65 72 61 74  tic void generat
29620 65 53 6f 72 74 54 61 69 6c 28 0a 20 20 50 61 72  eSortTail(.  Par
29621 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f  se *pParse,    /
29622 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
29623 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  t */.  Select *p
29624 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ,        /* The 
29625 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
29626 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 2c 20 20   */.  Vdbe *v,  
29627 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72          /* Gener
29628 61 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68  ate code into th
29629 69 73 20 56 44 42 45 20 2a 2f 0a 20 20 69 6e 74  is VDBE */.  int
2962a 20 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 2f   nColumn,      /
2962b 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  * Number of colu
2962c 6d 6e 73 20 6f 66 20 64 61 74 61 20 2a 2f 0a 20  mns of data */. 
2962d 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65   SelectDest *pDe
2962e 73 74 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20  st /* Write the 
2962f 73 6f 72 74 65 64 20 72 65 73 75 6c 74 73 20 68  sorted results h
29630 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ere */.){.  int 
29631 61 64 64 72 42 72 65 61 6b 20 3d 20 73 71 6c 69  addrBreak = sqli
29632 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
29633 28 76 29 3b 20 20 20 20 20 2f 2a 20 4a 75 6d 70  (v);     /* Jump
29634 20 68 65 72 65 20 74 6f 20 65 78 69 74 20 6c 6f   here to exit lo
29635 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  op */.  int addr
29636 43 6f 6e 74 69 6e 75 65 20 3d 20 73 71 6c 69 74  Continue = sqlit
29637 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
29638 76 29 3b 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72  v);  /* Jump her
29639 65 20 66 6f 72 20 6e 65 78 74 20 63 79 63 6c 65  e for next cycle
2963a 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a   */.  int addr;.
2963b 20 20 69 6e 74 20 69 54 61 62 3b 0a 20 20 69 6e    int iTab;.  in
2963c 74 20 70 73 65 75 64 6f 54 61 62 20 3d 20 30 3b  t pseudoTab = 0;
2963d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72  .  ExprList *pOr
2963e 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65  derBy = p->pOrde
2963f 72 42 79 3b 0a 0a 20 20 69 6e 74 20 65 44 65 73  rBy;..  int eDes
29640 74 20 3d 20 70 44 65 73 74 2d 3e 65 44 65 73 74  t = pDest->eDest
29641 3b 0a 20 20 69 6e 74 20 69 50 61 72 6d 20 3d 20  ;.  int iParm = 
29642 70 44 65 73 74 2d 3e 69 50 61 72 6d 3b 0a 0a 20  pDest->iParm;.. 
29643 20 69 6e 74 20 72 65 67 52 6f 77 3b 0a 20 20 69   int regRow;.  i
29644 6e 74 20 72 65 67 52 6f 77 69 64 3b 0a 0a 20 20  nt regRowid;..  
29645 69 54 61 62 20 3d 20 70 4f 72 64 65 72 42 79 2d  iTab = pOrderBy-
29646 3e 69 45 43 75 72 73 6f 72 3b 0a 20 20 72 65 67  >iECursor;.  reg
29647 52 6f 77 20 3d 20 73 71 6c 69 74 65 33 47 65 74  Row = sqlite3Get
29648 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
29649 0a 20 20 69 66 28 20 65 44 65 73 74 3d 3d 53 52  .  if( eDest==SR
2964a 54 5f 4f 75 74 70 75 74 20 7c 7c 20 65 44 65 73  T_Output || eDes
2964b 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65  t==SRT_Coroutine
2964c 20 29 7b 0a 20 20 20 20 70 73 65 75 64 6f 54 61   ){.    pseudoTa
2964d 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  b = pParse->nTab
2964e 2b 2b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ++;.    sqlite3V
2964f 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
29650 4f 70 65 6e 50 73 65 75 64 6f 2c 20 70 73 65 75  OpenPseudo, pseu
29651 64 6f 54 61 62 2c 20 72 65 67 52 6f 77 2c 20 6e  doTab, regRow, n
29652 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 72 65 67  Column);.    reg
29653 52 6f 77 69 64 20 3d 20 30 3b 0a 20 20 7d 65 6c  Rowid = 0;.  }el
29654 73 65 7b 0a 20 20 20 20 72 65 67 52 6f 77 69 64  se{.    regRowid
29655 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
29656 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
29657 7d 0a 20 20 61 64 64 72 20 3d 20 31 20 2b 20 73  }.  addr = 1 + s
29658 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
29659 28 76 2c 20 4f 50 5f 53 6f 72 74 2c 20 69 54 61  (v, OP_Sort, iTa
2965a 62 2c 20 61 64 64 72 42 72 65 61 6b 29 3b 0a 20  b, addrBreak);. 
2965b 20 63 6f 64 65 4f 66 66 73 65 74 28 76 2c 20 70   codeOffset(v, p
2965c 2c 20 61 64 64 72 43 6f 6e 74 69 6e 75 65 29 3b  , addrContinue);
2965d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2965e 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
2965f 6e 2c 20 69 54 61 62 2c 20 70 4f 72 64 65 72 42  n, iTab, pOrderB
29660 79 2d 3e 6e 45 78 70 72 20 2b 20 31 2c 20 72 65  y->nExpr + 1, re
29661 67 52 6f 77 29 3b 0a 20 20 73 77 69 74 63 68 28  gRow);.  switch(
29662 20 65 44 65 73 74 20 29 7b 0a 20 20 20 20 63 61   eDest ){.    ca
29663 73 65 20 53 52 54 5f 54 61 62 6c 65 3a 0a 20 20  se SRT_Table:.  
29664 20 20 63 61 73 65 20 53 52 54 5f 45 70 68 65 6d    case SRT_Ephem
29665 54 61 62 3a 20 7b 0a 20 20 20 20 20 20 74 65 73  Tab: {.      tes
29666 74 63 61 73 65 28 20 65 44 65 73 74 3d 3d 53 52  tcase( eDest==SR
29667 54 5f 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 20  T_Table );.     
29668 20 74 65 73 74 63 61 73 65 28 20 65 44 65 73 74   testcase( eDest
29669 3d 3d 53 52 54 5f 45 70 68 65 6d 54 61 62 20 29  ==SRT_EphemTab )
2966a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2966b 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2966c 4e 65 77 52 6f 77 69 64 2c 20 69 50 61 72 6d 2c  NewRowid, iParm,
2966d 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
2966e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2966f 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74  Op3(v, OP_Insert
29670 2c 20 69 50 61 72 6d 2c 20 72 65 67 52 6f 77 2c  , iParm, regRow,
29671 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
29672 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
29673 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f  ngeP5(v, OPFLAG_
29674 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 20 20 62  APPEND);.      b
29675 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e  reak;.    }.#ifn
29676 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
29677 53 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 73  SUBQUERY.    cas
29678 65 20 53 52 54 5f 53 65 74 3a 20 7b 0a 20 20 20  e SRT_Set: {.   
29679 20 20 20 61 73 73 65 72 74 28 20 6e 43 6f 6c 75     assert( nColu
2967a 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73  mn==1 );.      s
2967b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2967c 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  (v, OP_MakeRecor
2967d 64 2c 20 72 65 67 52 6f 77 2c 20 31 2c 20 72 65  d, regRow, 1, re
2967e 67 52 6f 77 69 64 2c 20 26 70 2d 3e 61 66 66 69  gRowid, &p->affi
2967f 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 20 20  nity, 1);.      
29680 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
29681 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70  AffinityChange(p
29682 50 61 72 73 65 2c 20 72 65 67 52 6f 77 2c 20 31  Parse, regRow, 1
29683 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29684 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
29685 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 50 61 72  _IdxInsert, iPar
29686 6d 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  m, regRowid);.  
29687 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
29688 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 4d 65  .    case SRT_Me
29689 6d 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72  m: {.      asser
2968a 74 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b  t( nColumn==1 );
2968b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2968c 70 72 43 6f 64 65 4d 6f 76 65 28 70 50 61 72 73  prCodeMove(pPars
2968d 65 2c 20 72 65 67 52 6f 77 2c 20 69 50 61 72 6d  e, regRow, iParm
2968e 2c 20 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54  , 1);.      /* T
2968f 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20  he LIMIT clause 
29690 77 69 6c 6c 20 74 65 72 6d 69 6e 61 74 65 20 74  will terminate t
29691 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20 2a  he loop for us *
29692 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  /.      break;. 
29693 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
29694 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
29695 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 61 73   int i;.      as
29696 73 65 72 74 28 20 65 44 65 73 74 3d 3d 53 52 54  sert( eDest==SRT
29697 5f 4f 75 74 70 75 74 20 7c 7c 20 65 44 65 73 74  _Output || eDest
29698 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20  ==SRT_Coroutine 
29699 29 3b 20 0a 20 20 20 20 20 20 74 65 73 74 63 61  ); .      testca
2969a 73 65 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f  se( eDest==SRT_O
2969b 75 74 70 75 74 20 29 3b 0a 20 20 20 20 20 20 74  utput );.      t
2969c 65 73 74 63 61 73 65 28 20 65 44 65 73 74 3d 3d  estcase( eDest==
2969d 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 29 3b  SRT_Coroutine );
2969e 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
2969f 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  i<nColumn; i++){
296a0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
296a1 20 72 65 67 52 6f 77 21 3d 70 44 65 73 74 2d 3e   regRow!=pDest->
296a2 69 4d 65 6d 2b 69 20 29 3b 0a 20 20 20 20 20 20  iMem+i );.      
296a3 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
296a4 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
296a5 2c 20 70 73 65 75 64 6f 54 61 62 2c 20 69 2c 20  , pseudoTab, i, 
296a6 70 44 65 73 74 2d 3e 69 4d 65 6d 2b 69 29 3b 0a  pDest->iMem+i);.
296a7 20 20 20 20 20 20 20 20 69 66 28 20 69 3d 3d 30          if( i==0
296a8 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
296a9 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
296aa 35 28 76 2c 20 4f 50 46 4c 41 47 5f 43 4c 45 41  5(v, OPFLAG_CLEA
296ab 52 43 41 43 48 45 29 3b 0a 20 20 20 20 20 20 20  RCACHE);.       
296ac 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
296ad 20 69 66 28 20 65 44 65 73 74 3d 3d 53 52 54 5f   if( eDest==SRT_
296ae 4f 75 74 70 75 74 20 29 7b 0a 20 20 20 20 20 20  Output ){.      
296af 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
296b0 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
296b1 52 6f 77 2c 20 70 44 65 73 74 2d 3e 69 4d 65 6d  Row, pDest->iMem
296b2 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  , nColumn);.    
296b3 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
296b4 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e  acheAffinityChan
296b5 67 65 28 70 50 61 72 73 65 2c 20 70 44 65 73 74  ge(pParse, pDest
296b6 2d 3e 69 4d 65 6d 2c 20 6e 43 6f 6c 75 6d 6e 29  ->iMem, nColumn)
296b7 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
296b8 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
296b9 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59  beAddOp1(v, OP_Y
296ba 69 65 6c 64 2c 20 70 44 65 73 74 2d 3e 69 50 61  ield, pDest->iPa
296bb 72 6d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rm);.      }.   
296bc 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
296bd 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 6c    }.  sqlite3Rel
296be 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
296bf 73 65 2c 20 72 65 67 52 6f 77 29 3b 0a 20 20 73  se, regRow);.  s
296c0 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
296c1 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
296c2 52 6f 77 69 64 29 3b 0a 0a 20 20 2f 2a 20 4c 49  Rowid);..  /* LI
296c3 4d 49 54 20 68 61 73 20 62 65 65 6e 20 69 6d 70  MIT has been imp
296c4 6c 65 6d 65 6e 74 65 64 20 62 79 20 74 68 65 20  lemented by the 
296c5 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 29  pushOntoSorter()
296c6 20 72 6f 75 74 69 6e 65 2e 0a 20 20 2a 2f 0a 20   routine..  */. 
296c7 20 61 73 73 65 72 74 28 20 70 2d 3e 69 4c 69 6d   assert( p->iLim
296c8 69 74 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 54  it==0 );..  /* T
296c9 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65  he bottom of the
296ca 20 6c 6f 6f 70 0a 20 20 2a 2f 0a 20 20 73 71 6c   loop.  */.  sql
296cb 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
296cc 61 62 65 6c 28 76 2c 20 61 64 64 72 43 6f 6e 74  abel(v, addrCont
296cd 69 6e 75 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  inue);.  sqlite3
296ce 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
296cf 5f 4e 65 78 74 2c 20 69 54 61 62 2c 20 61 64 64  _Next, iTab, add
296d0 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  r);.  sqlite3Vdb
296d1 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
296d2 20 61 64 64 72 42 72 65 61 6b 29 3b 0a 20 20 69   addrBreak);.  i
296d3 66 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75  f( eDest==SRT_Ou
296d4 74 70 75 74 20 7c 7c 20 65 44 65 73 74 3d 3d 53  tput || eDest==S
296d5 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 29 7b 0a  RT_Coroutine ){.
296d6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
296d7 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp2(v, OP_Clos
296d8 65 2c 20 70 73 65 75 64 6f 54 61 62 2c 20 30 29  e, pseudoTab, 0)
296d9 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ;.  }.}../*.** R
296da 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
296db 74 6f 20 61 20 73 74 72 69 6e 67 20 63 6f 6e 74  to a string cont
296dc 61 69 6e 69 6e 67 20 74 68 65 20 27 64 65 63 6c  aining the 'decl
296dd 61 72 61 74 69 6f 6e 20 74 79 70 65 27 20 6f 66  aration type' of
296de 20 74 68 65 0a 2a 2a 20 65 78 70 72 65 73 73 69   the.** expressi
296df 6f 6e 20 70 45 78 70 72 2e 20 54 68 65 20 73 74  on pExpr. The st
296e0 72 69 6e 67 20 6d 61 79 20 62 65 20 74 72 65 61  ring may be trea
296e1 74 65 64 20 61 73 20 73 74 61 74 69 63 20 62 79  ted as static by
296e2 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 0a   the caller..**.
296e3 2a 2a 20 54 68 65 20 64 65 63 6c 61 72 61 74 69  ** The declarati
296e4 6f 6e 20 74 79 70 65 20 69 73 20 74 68 65 20 65  on type is the e
296e5 78 61 63 74 20 64 61 74 61 74 79 70 65 20 64 65  xact datatype de
296e6 66 69 6e 69 74 69 6f 6e 20 65 78 74 72 61 63 74  finition extract
296e7 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 6f  ed from the.** o
296e8 72 69 67 69 6e 61 6c 20 43 52 45 41 54 45 20 54  riginal CREATE T
296e9 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69  ABLE statement i
296ea 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
296eb 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2e 20 54 68   is a column. Th
296ec 65 0a 2a 2a 20 64 65 63 6c 61 72 61 74 69 6f 6e  e.** declaration
296ed 20 74 79 70 65 20 66 6f 72 20 61 20 52 4f 57 49   type for a ROWI
296ee 44 20 66 69 65 6c 64 20 69 73 20 49 4e 54 45 47  D field is INTEG
296ef 45 52 2e 20 45 78 61 63 74 6c 79 20 77 68 65 6e  ER. Exactly when
296f0 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a   an expression.*
296f1 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  * is considered 
296f2 61 20 63 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20  a column can be 
296f3 63 6f 6d 70 6c 65 78 20 69 6e 20 74 68 65 20 70  complex in the p
296f4 72 65 73 65 6e 63 65 20 6f 66 20 73 75 62 71 75  resence of subqu
296f5 65 72 69 65 73 2e 20 54 68 65 0a 2a 2a 20 72 65  eries. The.** re
296f6 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73  sult-set express
296f7 69 6f 6e 20 69 6e 20 61 6c 6c 20 6f 66 20 74 68  ion in all of th
296f8 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 53 45 4c 45  e following SELE
296f9 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69 73  CT statements is
296fa 20 0a 2a 2a 20 63 6f 6e 73 69 64 65 72 65 64 20   .** considered 
296fb 61 20 63 6f 6c 75 6d 6e 20 62 79 20 74 68 69 73  a column by this
296fc 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
296fd 20 20 20 53 45 4c 45 43 54 20 63 6f 6c 20 46 52     SELECT col FR
296fe 4f 4d 20 74 62 6c 3b 0a 2a 2a 20 20 20 53 45 4c  OM tbl;.**   SEL
296ff 45 43 54 20 28 53 45 4c 45 43 54 20 63 6f 6c 20  ECT (SELECT col 
29700 46 52 4f 4d 20 74 62 6c 3b 0a 2a 2a 20 20 20 53  FROM tbl;.**   S
29701 45 4c 45 43 54 20 28 53 45 4c 45 43 54 20 63 6f  ELECT (SELECT co
29702 6c 20 46 52 4f 4d 20 74 62 6c 29 3b 0a 2a 2a 20  l FROM tbl);.** 
29703 20 20 53 45 4c 45 43 54 20 61 62 63 20 46 52 4f    SELECT abc FRO
29704 4d 20 28 53 45 4c 45 43 54 20 63 6f 6c 20 41 53  M (SELECT col AS
29705 20 61 62 63 20 46 52 4f 4d 20 74 62 6c 29 3b 0a   abc FROM tbl);.
29706 2a 2a 20 0a 2a 2a 20 54 68 65 20 64 65 63 6c 61  ** .** The decla
29707 72 61 74 69 6f 6e 20 74 79 70 65 20 66 6f 72 20  ration type for 
29708 61 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 6f  any expression o
29709 74 68 65 72 20 74 68 61 6e 20 61 20 63 6f 6c 75  ther than a colu
2970a 6d 6e 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73  mn is NULL..*/.s
2970b 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
2970c 20 2a 63 6f 6c 75 6d 6e 54 79 70 65 28 0a 20 20   *columnType(.  
2970d 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43  NameContext *pNC
2970e 2c 20 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  , .  Expr *pExpr
2970f 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
29710 2a 70 7a 4f 72 69 67 69 6e 44 62 2c 0a 20 20 63  *pzOriginDb,.  c
29711 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 4f 72  onst char **pzOr
29712 69 67 69 6e 54 61 62 2c 0a 20 20 63 6f 6e 73 74  iginTab,.  const
29713 20 63 68 61 72 20 2a 2a 70 7a 4f 72 69 67 69 6e   char **pzOrigin
29714 43 6f 6c 0a 29 7b 0a 20 20 63 68 61 72 20 63 6f  Col.){.  char co
29715 6e 73 74 20 2a 7a 54 79 70 65 20 3d 20 30 3b 0a  nst *zType = 0;.
29716 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4f    char const *zO
29717 72 69 67 69 6e 44 62 20 3d 20 30 3b 0a 20 20 63  riginDb = 0;.  c
29718 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4f 72 69 67  har const *zOrig
29719 69 6e 54 61 62 20 3d 20 30 3b 0a 20 20 63 68 61  inTab = 0;.  cha
2971a 72 20 63 6f 6e 73 74 20 2a 7a 4f 72 69 67 69 6e  r const *zOrigin
2971b 43 6f 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6a  Col = 0;.  int j
2971c 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 45  ;.  if( NEVER(pE
2971d 78 70 72 3d 3d 30 29 20 7c 7c 20 70 4e 43 2d 3e  xpr==0) || pNC->
2971e 70 53 72 63 4c 69 73 74 3d 3d 30 20 29 20 72 65  pSrcList==0 ) re
2971f 74 75 72 6e 20 30 3b 0a 0a 20 20 73 77 69 74 63  turn 0;..  switc
29720 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a  h( pExpr->op ){.
29721 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f      case TK_AGG_
29722 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 73 65  COLUMN:.    case
29723 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20   TK_COLUMN: {.  
29724 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65      /* The expre
29725 73 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d  ssion is a colum
29726 6e 2e 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61  n. Locate the ta
29727 62 6c 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69  ble the column i
29728 73 20 62 65 69 6e 67 0a 20 20 20 20 20 20 2a 2a  s being.      **
29729 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20   extracted from 
2972a 69 6e 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 70  in NameContext.p
2972b 53 72 63 4c 69 73 74 2e 20 54 68 69 73 20 74 61  SrcList. This ta
2972c 62 6c 65 20 6d 61 79 20 62 65 20 72 65 61 6c 0a  ble may be real.
2972d 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73        ** databas
2972e 65 20 74 61 62 6c 65 20 6f 72 20 61 20 73 75 62  e table or a sub
2972f 71 75 65 72 79 2e 0a 20 20 20 20 20 20 2a 2f 0a  query..      */.
29730 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61        Table *pTa
29731 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  b = 0;          
29732 20 20 2f 2a 20 54 61 62 6c 65 20 73 74 72 75 63    /* Table struc
29733 74 75 72 65 20 63 6f 6c 75 6d 6e 20 69 73 20 65  ture column is e
29734 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 2a 2f  xtracted from */
29735 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70  .      Select *p
29736 53 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  S = 0;          
29737 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 74 68 65     /* Select the
29738 20 63 6f 6c 75 6d 6e 20 69 73 20 65 78 74 72 61   column is extra
29739 63 74 65 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 20  cted from */.   
2973a 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 45     int iCol = pE
2973b 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 20 20 2f  xpr->iColumn;  /
2973c 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d  * Index of colum
2973d 6e 20 69 6e 20 70 54 61 62 20 2a 2f 0a 20 20 20  n in pTab */.   
2973e 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78     testcase( pEx
2973f 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43  pr->op==TK_AGG_C
29740 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 74  OLUMN );.      t
29741 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e  estcase( pExpr->
29742 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b  op==TK_COLUMN );
29743 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 41 4c  .      while( AL
29744 57 41 59 53 28 70 4e 43 29 20 26 26 20 21 70 54  WAYS(pNC) && !pT
29745 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 53 72  ab ){.        Sr
29746 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 20  cList *pTabList 
29747 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b  = pNC->pSrcList;
29748 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  .        for(j=0
29749 3b 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72  ;j<pTabList->nSr
2974a 63 20 26 26 20 70 54 61 62 4c 69 73 74 2d 3e 61  c && pTabList->a
2974b 5b 6a 5d 2e 69 43 75 72 73 6f 72 21 3d 70 45 78  [j].iCursor!=pEx
2974c 70 72 2d 3e 69 54 61 62 6c 65 3b 6a 2b 2b 29 3b  pr->iTable;j++);
2974d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a 3c 70  .        if( j<p
2974e 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 20 29 7b  TabList->nSrc ){
2974f 0a 20 20 20 20 20 20 20 20 20 20 70 54 61 62 20  .          pTab 
29750 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d  = pTabList->a[j]
29751 2e 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20  .pTab;.         
29752 20 70 53 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e   pS = pTabList->
29753 61 5b 6a 5d 2e 70 53 65 6c 65 63 74 3b 0a 20 20  a[j].pSelect;.  
29754 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
29755 20 20 20 20 20 20 20 70 4e 43 20 3d 20 70 4e 43         pNC = pNC
29756 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20  ->pNext;.       
29757 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20   }.      }..    
29758 20 20 69 66 28 20 4e 45 56 45 52 28 70 54 61 62    if( NEVER(pTab
29759 3d 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ==0) ){.        
2975a 2f 2a 20 41 74 20 6f 6e 65 20 74 69 6d 65 2c 20  /* At one time, 
2975b 63 6f 64 65 20 73 75 63 68 20 61 73 20 22 53 45  code such as "SE
2975c 4c 45 43 54 20 6e 65 77 2e 78 22 20 77 69 74 68  LECT new.x" with
2975d 69 6e 20 61 20 74 72 69 67 67 65 72 20 77 6f 75  in a trigger wou
2975e 6c 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 61  ld.        ** ca
2975f 75 73 65 20 74 68 69 73 20 63 6f 6e 64 69 74 69  use this conditi
29760 6f 6e 20 74 6f 20 72 75 6e 2e 20 20 53 69 6e 63  on to run.  Sinc
29761 65 20 74 68 65 6e 2c 20 77 65 20 68 61 76 65 20  e then, we have 
29762 72 65 73 74 72 75 63 74 75 72 65 64 20 68 6f 77  restructured how
29763 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72 69 67  .        ** trig
29764 67 65 72 20 63 6f 64 65 20 69 73 20 67 65 6e 65  ger code is gene
29765 72 61 74 65 64 20 61 6e 64 20 73 6f 20 74 68 69  rated and so thi
29766 73 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 20 6e  s condition is n
29767 6f 20 6c 6f 6e 67 65 72 20 0a 20 20 20 20 20 20  o longer .      
29768 20 20 2a 2a 20 70 6f 73 73 69 62 6c 65 2e 20 20    ** possible.  
29769 42 75 74 20 69 74 20 73 65 65 6d 73 20 70 72 75  But it seems pru
2976a 64 65 6e 74 20 74 6f 20 6b 65 65 70 20 74 68 65  dent to keep the
2976b 20 74 65 73 74 20 69 6e 20 70 6c 61 63 65 20 69   test in place i
2976c 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 61 73  n.        ** cas
2976d 65 20 73 6f 6d 65 74 68 69 6e 67 20 65 6c 73 65  e something else
2976e 20 63 68 61 6e 67 65 73 2e 0a 20 20 20 20 20 20   changes..      
2976f 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 7a 54 79    */.        zTy
29770 70 65 20 3d 20 22 54 45 58 54 22 3b 0a 20 20 20  pe = "TEXT";.   
29771 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
29772 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 72    }..      asser
29773 74 28 20 70 54 61 62 20 29 3b 0a 20 20 20 20 20  t( pTab );.     
29774 20 69 66 28 20 70 53 20 29 7b 0a 20 20 20 20 20   if( pS ){.     
29775 20 20 20 2f 2a 20 54 68 65 20 22 74 61 62 6c 65     /* The "table
29776 22 20 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20  " is actually a 
29777 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 61 20  sub-select or a 
29778 76 69 65 77 20 69 6e 20 74 68 65 20 46 52 4f 4d  view in the FROM
29779 20 63 6c 61 75 73 65 0a 20 20 20 20 20 20 20 20   clause.        
2977a 2a 2a 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  ** of the SELECT
2977b 20 73 74 61 74 65 6d 65 6e 74 2e 20 52 65 74 75   statement. Retu
2977c 72 6e 20 74 68 65 20 64 65 63 6c 61 72 61 74 69  rn the declarati
2977d 6f 6e 20 74 79 70 65 20 61 6e 64 20 6f 72 69 67  on type and orig
2977e 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 61  in.        ** da
2977f 74 61 20 66 6f 72 20 74 68 65 20 72 65 73 75 6c  ta for the resul
29780 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 20 6f 66 20  t-set column of 
29781 74 68 65 20 73 75 62 2d 73 65 6c 65 63 74 2e 0a  the sub-select..
29782 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
29783 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 69 43     if( ALWAYS(iC
29784 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 53  ol>=0 && iCol<pS
29785 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29  ->pEList->nExpr)
29786 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
29787 20 49 66 20 69 43 6f 6c 20 69 73 20 6c 65 73 73   If iCol is less
29788 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e   than zero, then
29789 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
2978a 72 65 71 75 65 73 74 73 20 74 68 65 0a 20 20 20  requests the.   
2978b 20 20 20 20 20 20 20 2a 2a 20 72 6f 77 69 64 20         ** rowid 
2978c 6f 66 20 74 68 65 20 73 75 62 2d 73 65 6c 65 63  of the sub-selec
2978d 74 20 6f 72 20 76 69 65 77 2e 20 54 68 69 73 20  t or view. This 
2978e 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6c 65  expression is le
2978f 67 61 6c 20 28 73 65 65 20 0a 20 20 20 20 20 20  gal (see .      
29790 20 20 20 20 2a 2a 20 74 65 73 74 20 63 61 73 65      ** test case
29791 20 6d 69 73 63 32 2e 32 2e 32 29 20 2d 20 69 74   misc2.2.2) - it
29792 20 61 6c 77 61 79 73 20 65 76 61 6c 75 61 74 65   always evaluate
29793 73 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 20  s to NULL..     
29794 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
29795 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e    NameContext sN
29796 43 3b 0a 20 20 20 20 20 20 20 20 20 20 45 78 70  C;.          Exp
29797 72 20 2a 70 20 3d 20 70 53 2d 3e 70 45 4c 69 73  r *p = pS->pELis
29798 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78 70 72  t->a[iCol].pExpr
29799 3b 0a 20 20 20 20 20 20 20 20 20 20 73 4e 43 2e  ;.          sNC.
2979a 70 53 72 63 4c 69 73 74 20 3d 20 70 53 2d 3e 70  pSrcList = pS->p
2979b 53 72 63 3b 0a 20 20 20 20 20 20 20 20 20 20 73  Src;.          s
2979c 4e 43 2e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20  NC.pNext = 0;.  
2979d 20 20 20 20 20 20 20 20 73 4e 43 2e 70 50 61 72          sNC.pPar
2979e 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65  se = pNC->pParse
2979f 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 54 79 70  ;.          zTyp
297a0 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26  e = columnType(&
297a1 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69 67 69 6e  sNC, p, &zOrigin
297a2 44 62 2c 20 26 7a 4f 72 69 67 69 6e 54 61 62 2c  Db, &zOriginTab,
297a3 20 26 7a 4f 72 69 67 69 6e 43 6f 6c 29 3b 20 0a   &zOriginCol); .
297a4 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
297a5 7d 65 6c 73 65 20 69 66 28 20 41 4c 57 41 59 53  }else if( ALWAYS
297a6 28 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 20  (pTab->pSchema) 
297a7 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 20  ){.        /* A 
297a8 72 65 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  real table */.  
297a9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
297aa 53 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  S );.        if(
297ab 20 69 43 6f 6c 3c 30 20 29 20 69 43 6f 6c 20 3d   iCol<0 ) iCol =
297ac 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 20   pTab->iPKey;.  
297ad 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 43        assert( iC
297ae 6f 6c 3d 3d 2d 31 20 7c 7c 20 28 69 43 6f 6c 3e  ol==-1 || (iCol>
297af 3d 30 20 26 26 20 69 43 6f 6c 3c 70 54 61 62 2d  =0 && iCol<pTab-
297b0 3e 6e 43 6f 6c 29 20 29 3b 0a 20 20 20 20 20 20  >nCol) );.      
297b1 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a    if( iCol<0 ){.
297b2 20 20 20 20 20 20 20 20 20 20 7a 54 79 70 65 20            zType 
297b3 3d 20 22 49 4e 54 45 47 45 52 22 3b 0a 20 20 20  = "INTEGER";.   
297b4 20 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 43 6f         zOriginCo
297b5 6c 20 3d 20 22 72 6f 77 69 64 22 3b 0a 20 20 20  l = "rowid";.   
297b6 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
297b7 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 70 54        zType = pT
297b8 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a  ab->aCol[iCol].z
297b9 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 20 20  Type;.          
297ba 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 70 54 61  zOriginCol = pTa
297bb 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e  b->aCol[iCol].zN
297bc 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ame;.        }. 
297bd 20 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 54 61         zOriginTa
297be 62 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b  b = pTab->zName;
297bf 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4e 43  .        if( pNC
297c0 2d 3e 70 50 61 72 73 65 20 29 7b 0a 20 20 20 20  ->pParse ){.    
297c1 20 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20        int iDb = 
297c2 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
297c3 6e 64 65 78 28 70 4e 43 2d 3e 70 50 61 72 73 65  ndex(pNC->pParse
297c4 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  ->db, pTab->pSch
297c5 65 6d 61 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ema);.          
297c6 7a 4f 72 69 67 69 6e 44 62 20 3d 20 70 4e 43 2d  zOriginDb = pNC-
297c7 3e 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62  >pParse->db->aDb
297c8 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  [iDb].zName;.   
297c9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
297ca 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
297cb 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
297cc 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20  _OMIT_SUBQUERY. 
297cd 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43     case TK_SELEC
297ce 54 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  T: {.      /* Th
297cf 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
297d0 61 20 73 75 62 2d 73 65 6c 65 63 74 2e 20 52 65  a sub-select. Re
297d1 74 75 72 6e 20 74 68 65 20 64 65 63 6c 61 72 61  turn the declara
297d2 74 69 6f 6e 20 74 79 70 65 20 61 6e 64 0a 20 20  tion type and.  
297d3 20 20 20 20 2a 2a 20 6f 72 69 67 69 6e 20 69 6e      ** origin in
297d4 66 6f 20 66 6f 72 20 74 68 65 20 73 69 6e 67 6c  fo for the singl
297d5 65 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20  e column in the 
297d6 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68  result set of th
297d7 65 20 53 45 4c 45 43 54 0a 20 20 20 20 20 20 2a  e SELECT.      *
297d8 2a 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20  * statement..   
297d9 20 20 20 2a 2f 0a 20 20 20 20 20 20 4e 61 6d 65     */.      Name
297da 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 20  Context sNC;.   
297db 20 20 20 53 65 6c 65 63 74 20 2a 70 53 20 3d 20     Select *pS = 
297dc 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74  pExpr->x.pSelect
297dd 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 20  ;.      Expr *p 
297de 3d 20 70 53 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b  = pS->pEList->a[
297df 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  0].pExpr;.      
297e0 61 73 73 65 72 74 28 20 45 78 70 72 48 61 73 50  assert( ExprHasP
297e1 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
297e2 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a  P_xIsSelect) );.
297e3 20 20 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69        sNC.pSrcLi
297e4 73 74 20 3d 20 70 53 2d 3e 70 53 72 63 3b 0a 20  st = pS->pSrc;. 
297e5 20 20 20 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d       sNC.pNext =
297e6 20 70 4e 43 3b 0a 20 20 20 20 20 20 73 4e 43 2e   pNC;.      sNC.
297e7 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50  pParse = pNC->pP
297e8 61 72 73 65 3b 0a 20 20 20 20 20 20 7a 54 79 70  arse;.      zTyp
297e9 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26  e = columnType(&
297ea 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69 67 69 6e  sNC, p, &zOrigin
297eb 44 62 2c 20 26 7a 4f 72 69 67 69 6e 54 61 62 2c  Db, &zOriginTab,
297ec 20 26 7a 4f 72 69 67 69 6e 43 6f 6c 29 3b 20 0a   &zOriginCol); .
297ed 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
297ee 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20   }.#endif.  }.  
297ef 0a 20 20 69 66 28 20 70 7a 4f 72 69 67 69 6e 44  .  if( pzOriginD
297f0 62 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  b ){.    assert(
297f1 20 70 7a 4f 72 69 67 69 6e 54 61 62 20 26 26 20   pzOriginTab && 
297f2 70 7a 4f 72 69 67 69 6e 43 6f 6c 20 29 3b 0a 20  pzOriginCol );. 
297f3 20 20 20 2a 70 7a 4f 72 69 67 69 6e 44 62 20 3d     *pzOriginDb =
297f4 20 7a 4f 72 69 67 69 6e 44 62 3b 0a 20 20 20 20   zOriginDb;.    
297f5 2a 70 7a 4f 72 69 67 69 6e 54 61 62 20 3d 20 7a  *pzOriginTab = z
297f6 4f 72 69 67 69 6e 54 61 62 3b 0a 20 20 20 20 2a  OriginTab;.    *
297f7 70 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 7a 4f  pzOriginCol = zO
297f8 72 69 67 69 6e 43 6f 6c 3b 0a 20 20 7d 0a 20 20  riginCol;.  }.  
297f9 72 65 74 75 72 6e 20 7a 54 79 70 65 3b 0a 7d 0a  return zType;.}.
297fa 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
297fb 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 74  code that will t
297fc 65 6c 6c 20 74 68 65 20 56 44 42 45 20 74 68 65  ell the VDBE the
297fd 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70   declaration typ
297fe 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a 2a  es of columns.**
297ff 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
29800 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  et..*/.static vo
29801 69 64 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d  id generateColum
29802 6e 54 79 70 65 73 28 0a 20 20 50 61 72 73 65 20  nTypes(.  Parse 
29803 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a  *pParse,      /*
29804 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   Parser context 
29805 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54  */.  SrcList *pT
29806 61 62 4c 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74  abList,  /* List
29807 20 6f 66 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20   of tables */.  
29808 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
29809 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
2980a 6e 73 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20  ns defining the 
2980b 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 29 7b  result set */.){
2980c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2980d 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 0a 20 20  OMIT_DECLTYPE.  
2980e 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
2980f 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69  ->pVdbe;.  int i
29810 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  ;.  NameContext 
29811 73 4e 43 3b 0a 20 20 73 4e 43 2e 70 53 72 63 4c  sNC;.  sNC.pSrcL
29812 69 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a  ist = pTabList;.
29813 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70    sNC.pParse = p
29814 50 61 72 73 65 3b 0a 20 20 66 6f 72 28 69 3d 30  Parse;.  for(i=0
29815 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70  ; i<pEList->nExp
29816 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70  r; i++){.    Exp
29817 72 20 2a 70 20 3d 20 70 45 4c 69 73 74 2d 3e 61  r *p = pEList->a
29818 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 63  [i].pExpr;.    c
29819 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65  onst char *zType
2981a 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
2981b 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45  ENABLE_COLUMN_ME
2981c 54 41 44 41 54 41 0a 20 20 20 20 63 6f 6e 73 74  TADATA.    const
2981d 20 63 68 61 72 20 2a 7a 4f 72 69 67 44 62 20 3d   char *zOrigDb =
2981e 20 30 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68   0;.    const ch
2981f 61 72 20 2a 7a 4f 72 69 67 54 61 62 20 3d 20 30  ar *zOrigTab = 0
29820 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
29821 20 2a 7a 4f 72 69 67 43 6f 6c 20 3d 20 30 3b 0a   *zOrigCol = 0;.
29822 20 20 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c 75      zType = colu
29823 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20  mnType(&sNC, p, 
29824 26 7a 4f 72 69 67 44 62 2c 20 26 7a 4f 72 69 67  &zOrigDb, &zOrig
29825 54 61 62 2c 20 26 7a 4f 72 69 67 43 6f 6c 29 3b  Tab, &zOrigCol);
29826 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 76 64 62  ..    /* The vdb
29827 65 20 6d 75 73 74 20 6d 61 6b 65 20 69 74 73 20  e must make its 
29828 6f 77 6e 20 63 6f 70 79 20 6f 66 20 74 68 65 20  own copy of the 
29829 63 6f 6c 75 6d 6e 2d 74 79 70 65 20 61 6e 64 20  column-type and 
2982a 6f 74 68 65 72 20 0a 20 20 20 20 2a 2a 20 63 6f  other .    ** co
2982b 6c 75 6d 6e 20 73 70 65 63 69 66 69 63 20 73 74  lumn specific st
2982c 72 69 6e 67 73 2c 20 69 6e 20 63 61 73 65 20 74  rings, in case t
2982d 68 65 20 73 63 68 65 6d 61 20 69 73 20 72 65 73  he schema is res
2982e 65 74 20 62 65 66 6f 72 65 20 74 68 69 73 0a 20  et before this. 
2982f 20 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61     ** virtual ma
29830 63 68 69 6e 65 20 69 73 20 64 65 6c 65 74 65 64  chine is deleted
29831 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  ..    */.    sql
29832 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
29833 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45  me(v, i, COLNAME
29834 5f 44 41 54 41 42 41 53 45 2c 20 7a 4f 72 69 67  _DATABASE, zOrig
29835 44 62 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  Db, SQLITE_TRANS
29836 49 45 4e 54 29 3b 0a 20 20 20 20 73 71 6c 69 74  IENT);.    sqlit
29837 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
29838 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 54  (v, i, COLNAME_T
29839 41 42 4c 45 2c 20 7a 4f 72 69 67 54 61 62 2c 20  ABLE, zOrigTab, 
2983a 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
2983b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2983c 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
2983d 69 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d  i, COLNAME_COLUM
2983e 4e 2c 20 7a 4f 72 69 67 43 6f 6c 2c 20 53 51 4c  N, zOrigCol, SQL
2983f 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
29840 23 65 6c 73 65 0a 20 20 20 20 7a 54 79 70 65 20  #else.    zType 
29841 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26 73 4e  = columnType(&sN
29842 43 2c 20 70 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  C, p, 0, 0, 0);.
29843 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74  #endif.    sqlit
29844 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
29845 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 44  (v, i, COLNAME_D
29846 45 43 4c 54 59 50 45 2c 20 7a 54 79 70 65 2c 20  ECLTYPE, zType, 
29847 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
29848 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  );.  }.#endif /*
29849 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43   SQLITE_OMIT_DEC
2984a 4c 54 59 50 45 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a  LTYPE */.}../*.*
2984b 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
2984c 74 68 61 74 20 77 69 6c 6c 20 74 65 6c 6c 20 74  that will tell t
2984d 68 65 20 56 44 42 45 20 74 68 65 20 6e 61 6d 65  he VDBE the name
2984e 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20  s of columns.** 
2984f 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
29850 74 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61  t.  This informa
29851 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
29852 70 72 6f 76 69 64 65 20 74 68 65 0a 2a 2a 20 61  provide the.** a
29853 7a 43 6f 6c 5b 5d 20 76 61 6c 75 65 73 20 69 6e  zCol[] values in
29854 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a   the callback..*
29855 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67 65  /.static void ge
29856 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65  nerateColumnName
29857 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  s(.  Parse *pPar
29858 73 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73  se,      /* Pars
29859 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
2985a 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
2985b 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74  t,  /* List of t
2985c 61 62 6c 65 73 20 2a 2f 0a 20 20 45 78 70 72 4c  ables */.  ExprL
2985d 69 73 74 20 2a 70 45 4c 69 73 74 20 20 20 20 2f  ist *pEList    /
2985e 2a 20 45 78 70 72 65 73 73 69 6f 6e 73 20 64 65  * Expressions de
2985f 66 69 6e 69 6e 67 20 74 68 65 20 72 65 73 75 6c  fining the resul
29860 74 20 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 56 64  t set */.){.  Vd
29861 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
29862 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 2c 20  pVdbe;.  int i, 
29863 6a 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  j;.  sqlite3 *db
29864 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
29865 20 69 6e 74 20 66 75 6c 6c 4e 61 6d 65 73 2c 20   int fullNames, 
29866 73 68 6f 72 74 4e 61 6d 65 73 3b 0a 0a 23 69 66  shortNames;..#if
29867 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
29868 5f 45 58 50 4c 41 49 4e 0a 20 20 2f 2a 20 49 66  _EXPLAIN.  /* If
29869 20 74 68 69 73 20 69 73 20 61 6e 20 45 58 50 4c   this is an EXPL
2986a 41 49 4e 2c 20 73 6b 69 70 20 74 68 69 73 20 73  AIN, skip this s
2986b 74 65 70 20 2a 2f 0a 20 20 69 66 28 20 70 50 61  tep */.  if( pPa
2986c 72 73 65 2d 3e 65 78 70 6c 61 69 6e 20 29 7b 0a  rse->explain ){.
2986d 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
2986e 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 70 50  #endif..  if( pP
2986f 61 72 73 65 2d 3e 63 6f 6c 4e 61 6d 65 73 53 65  arse->colNamesSe
29870 74 20 7c 7c 20 4e 45 56 45 52 28 76 3d 3d 30 29  t || NEVER(v==0)
29871 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
29872 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20  iled ) return;. 
29873 20 70 50 61 72 73 65 2d 3e 63 6f 6c 4e 61 6d 65   pParse->colName
29874 73 53 65 74 20 3d 20 31 3b 0a 20 20 66 75 6c 6c  sSet = 1;.  full
29875 4e 61 6d 65 73 20 3d 20 28 64 62 2d 3e 66 6c 61  Names = (db->fla
29876 67 73 20 26 20 53 51 4c 49 54 45 5f 46 75 6c 6c  gs & SQLITE_Full
29877 43 6f 6c 4e 61 6d 65 73 29 21 3d 30 3b 0a 20 20  ColNames)!=0;.  
29878 73 68 6f 72 74 4e 61 6d 65 73 20 3d 20 28 64 62  shortNames = (db
29879 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
2987a 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 29 21  _ShortColNames)!
2987b 3d 30 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  =0;.  sqlite3Vdb
2987c 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 70  eSetNumCols(v, p
2987d 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20  EList->nExpr);. 
2987e 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69   for(i=0; i<pELi
2987f 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  st->nExpr; i++){
29880 0a 20 20 20 20 45 78 70 72 20 2a 70 3b 0a 20 20  .    Expr *p;.  
29881 20 20 70 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b    p = pEList->a[
29882 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 69 66  i].pExpr;.    if
29883 28 20 4e 45 56 45 52 28 70 3d 3d 30 29 20 29 20  ( NEVER(p==0) ) 
29884 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66  continue;.    if
29885 28 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a  ( pEList->a[i].z
29886 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 63 68  Name ){.      ch
29887 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 45 4c 69  ar *zName = pELi
29888 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a  st->a[i].zName;.
29889 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2988a 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69  eSetColName(v, i
2988b 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
2988c 7a 4e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 54 52  zName, SQLITE_TR
2988d 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 7d 65  ANSIENT);.    }e
2988e 6c 73 65 20 69 66 28 20 28 70 2d 3e 6f 70 3d 3d  lse if( (p->op==
2988f 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 2d 3e  TK_COLUMN || p->
29890 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d  op==TK_AGG_COLUM
29891 4e 29 20 26 26 20 70 54 61 62 4c 69 73 74 20 29  N) && pTabList )
29892 7b 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70  {.      Table *p
29893 54 61 62 3b 0a 20 20 20 20 20 20 63 68 61 72 20  Tab;.      char 
29894 2a 7a 43 6f 6c 3b 0a 20 20 20 20 20 20 69 6e 74  *zCol;.      int
29895 20 69 43 6f 6c 20 3d 20 70 2d 3e 69 43 6f 6c 75   iCol = p->iColu
29896 6d 6e 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d  mn;.      for(j=
29897 30 3b 20 41 4c 57 41 59 53 28 6a 3c 70 54 61 62  0; ALWAYS(j<pTab
29898 4c 69 73 74 2d 3e 6e 53 72 63 29 3b 20 6a 2b 2b  List->nSrc); j++
29899 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ){.        if( p
2989a 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 69 43  TabList->a[j].iC
2989b 75 72 73 6f 72 3d 3d 70 2d 3e 69 54 61 62 6c 65  ursor==p->iTable
2989c 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
2989d 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  }.      assert( 
2989e 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  j<pTabList->nSrc
2989f 20 29 3b 0a 20 20 20 20 20 20 70 54 61 62 20 3d   );.      pTab =
298a0 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d 2e   pTabList->a[j].
298a1 70 54 61 62 3b 0a 20 20 20 20 20 20 69 66 28 20  pTab;.      if( 
298a2 69 43 6f 6c 3c 30 20 29 20 69 43 6f 6c 20 3d 20  iCol<0 ) iCol = 
298a3 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 20  pTab->iPKey;.   
298a4 20 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3d     assert( iCol=
298a5 3d 2d 31 20 7c 7c 20 28 69 43 6f 6c 3e 3d 30 20  =-1 || (iCol>=0 
298a6 26 26 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43  && iCol<pTab->nC
298a7 6f 6c 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28  ol) );.      if(
298a8 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20   iCol<0 ){.     
298a9 20 20 20 7a 43 6f 6c 20 3d 20 22 72 6f 77 69 64     zCol = "rowid
298aa 22 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  ";.      }else{.
298ab 20 20 20 20 20 20 20 20 7a 43 6f 6c 20 3d 20 70          zCol = p
298ac 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e  Tab->aCol[iCol].
298ad 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 7d 0a 20  zName;.      }. 
298ae 20 20 20 20 20 69 66 28 20 21 73 68 6f 72 74 4e       if( !shortN
298af 61 6d 65 73 20 26 26 20 21 66 75 6c 6c 4e 61 6d  ames && !fullNam
298b0 65 73 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  es ){.        sq
298b1 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
298b2 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d  ame(v, i, COLNAM
298b3 45 5f 4e 41 4d 45 2c 20 0a 20 20 20 20 20 20 20  E_NAME, .       
298b4 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 53 74       sqlite3DbSt
298b5 72 44 75 70 28 64 62 2c 20 70 45 4c 69 73 74 2d  rDup(db, pEList-
298b6 3e 61 5b 69 5d 2e 7a 53 70 61 6e 29 2c 20 53 51  >a[i].zSpan), SQ
298b7 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  LITE_DYNAMIC);. 
298b8 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66       }else if( f
298b9 75 6c 6c 4e 61 6d 65 73 20 29 7b 0a 20 20 20 20  ullNames ){.    
298ba 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20      char *zName 
298bb 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61  = 0;.        zNa
298bc 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  me = sqlite3MPri
298bd 6e 74 66 28 64 62 2c 20 22 25 73 2e 25 73 22 2c  ntf(db, "%s.%s",
298be 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43   pTab->zName, zC
298bf 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ol);.        sql
298c0 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
298c1 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45  me(v, i, COLNAME
298c2 5f 4e 41 4d 45 2c 20 7a 4e 61 6d 65 2c 20 53 51  _NAME, zName, SQ
298c3 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  LITE_DYNAMIC);. 
298c4 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
298c5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
298c6 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20  etColName(v, i, 
298c7 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 43  COLNAME_NAME, zC
298c8 6f 6c 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  ol, SQLITE_TRANS
298c9 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 7d 0a 20  IENT);.      }. 
298ca 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
298cb 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
298cc 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e  lName(v, i, COLN
298cd 41 4d 45 5f 4e 41 4d 45 2c 20 0a 20 20 20 20 20  AME_NAME, .     
298ce 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 53 74       sqlite3DbSt
298cf 72 44 75 70 28 64 62 2c 20 70 45 4c 69 73 74 2d  rDup(db, pEList-
298d0 3e 61 5b 69 5d 2e 7a 53 70 61 6e 29 2c 20 53 51  >a[i].zSpan), SQ
298d1 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  LITE_DYNAMIC);. 
298d2 20 20 20 7d 0a 20 20 7d 0a 20 20 67 65 6e 65 72     }.  }.  gener
298d3 61 74 65 43 6f 6c 75 6d 6e 54 79 70 65 73 28 70  ateColumnTypes(p
298d4 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c  Parse, pTabList,
298d5 20 70 45 4c 69 73 74 29 3b 0a 7d 0a 0a 23 69 66   pEList);.}..#if
298d6 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
298d7 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
298d8 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f 66 20 74  ./*.** Name of t
298d9 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 70  he connection op
298da 65 72 61 74 6f 72 2c 20 75 73 65 64 20 66 6f 72  erator, used for
298db 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 2e   error messages.
298dc 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
298dd 20 63 68 61 72 20 2a 73 65 6c 65 63 74 4f 70 4e   char *selectOpN
298de 61 6d 65 28 69 6e 74 20 69 64 29 7b 0a 20 20 63  ame(int id){.  c
298df 68 61 72 20 2a 7a 3b 0a 20 20 73 77 69 74 63 68  har *z;.  switch
298e0 28 20 69 64 20 29 7b 0a 20 20 20 20 63 61 73 65  ( id ){.    case
298e1 20 54 4b 5f 41 4c 4c 3a 20 20 20 20 20 20 20 7a   TK_ALL:       z
298e2 20 3d 20 22 55 4e 49 4f 4e 20 41 4c 4c 22 3b 20   = "UNION ALL"; 
298e3 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
298e4 65 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 3a 20  e TK_INTERSECT: 
298e5 7a 20 3d 20 22 49 4e 54 45 52 53 45 43 54 22 3b  z = "INTERSECT";
298e6 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
298e7 73 65 20 54 4b 5f 45 58 43 45 50 54 3a 20 20 20  se TK_EXCEPT:   
298e8 20 7a 20 3d 20 22 45 58 43 45 50 54 22 3b 20 20   z = "EXCEPT";  
298e9 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64      break;.    d
298ea 65 66 61 75 6c 74 3a 20 20 20 20 20 20 20 20 20  efault:         
298eb 20 20 7a 20 3d 20 22 55 4e 49 4f 4e 22 3b 20 20    z = "UNION";  
298ec 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a       break;.  }.
298ed 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 23 65    return z;.}.#e
298ee 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
298ef 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MIT_COMPOUND_SEL
298f0 45 43 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 69  ECT */../*.** Gi
298f1 76 65 6e 20 61 20 61 6e 20 65 78 70 72 65 73 73  ven a an express
298f2 69 6f 6e 20 6c 69 73 74 20 28 77 68 69 63 68 20  ion list (which 
298f3 69 73 20 72 65 61 6c 6c 79 20 74 68 65 20 6c 69  is really the li
298f4 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e  st of expression
298f5 73 0a 2a 2a 20 74 68 61 74 20 66 6f 72 6d 20 74  s.** that form t
298f6 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
298f7 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
298f8 65 6e 74 29 20 63 6f 6d 70 75 74 65 20 61 70 70  ent) compute app
298f9 72 6f 70 72 69 61 74 65 0a 2a 2a 20 63 6f 6c 75  ropriate.** colu
298fa 6d 6e 20 6e 61 6d 65 73 20 66 6f 72 20 61 20 74  mn names for a t
298fb 61 62 6c 65 20 74 68 61 74 20 77 6f 75 6c 64 20  able that would 
298fc 68 6f 6c 64 20 74 68 65 20 65 78 70 72 65 73 73  hold the express
298fd 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  ion list..**.** 
298fe 41 6c 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  All column names
298ff 20 77 69 6c 6c 20 62 65 20 75 6e 69 71 75 65 2e   will be unique.
29900 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20  .**.** Only the 
29901 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 72 65  column names are
29902 20 63 6f 6d 70 75 74 65 64 2e 20 20 43 6f 6c 75   computed.  Colu
29903 6d 6e 2e 7a 54 79 70 65 2c 20 43 6f 6c 75 6d 6e  mn.zType, Column
29904 2e 7a 43 6f 6c 6c 2c 0a 2a 2a 20 61 6e 64 20 6f  .zColl,.** and o
29905 74 68 65 72 20 66 69 65 6c 64 73 20 6f 66 20 43  ther fields of C
29906 6f 6c 75 6d 6e 20 61 72 65 20 7a 65 72 6f 65 64  olumn are zeroed
29907 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
29908 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63  QLITE_OK on succ
29909 65 73 73 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72  ess.  If a memor
2990a 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72  y allocation err
2990b 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 73 74  or occurs,.** st
2990c 6f 72 65 20 4e 55 4c 4c 20 69 6e 20 2a 70 61 43  ore NULL in *paC
2990d 6f 6c 20 61 6e 64 20 30 20 69 6e 20 2a 70 6e 43  ol and 0 in *pnC
2990e 6f 6c 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51  ol and return SQ
2990f 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 73  LITE_NOMEM..*/.s
29910 74 61 74 69 63 20 69 6e 74 20 73 65 6c 65 63 74  tatic int select
29911 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78 70 72 4c  ColumnsFromExprL
29912 69 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ist(.  Parse *pP
29913 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
29914 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
29915 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  t */.  ExprList 
29916 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 20 20 2f  *pEList,       /
29917 2a 20 45 78 70 72 20 6c 69 73 74 20 66 72 6f 6d  * Expr list from
29918 20 77 68 69 63 68 20 74 6f 20 64 65 72 69 76 65   which to derive
29919 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a 2f   column names */
2991a 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6c 2c 20 20  .  int *pnCol,  
2991b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
2991c 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ite the number o
2991d 66 20 63 6f 6c 75 6d 6e 73 20 68 65 72 65 20 2a  f columns here *
2991e 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 2a 70 61 43  /.  Column **paC
2991f 6f 6c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57  ol          /* W
29920 72 69 74 65 20 74 68 65 20 6e 65 77 20 63 6f 6c  rite the new col
29921 75 6d 6e 20 6c 69 73 74 20 68 65 72 65 20 2a 2f  umn list here */
29922 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
29923 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20  b = pParse->db; 
29924 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
29925 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  nnection */.  in
29926 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20  t i, j;         
29927 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
29928 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20  p counters */.  
29929 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 20 20  int cnt;        
2992a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
2992b 6e 64 65 78 20 61 64 64 65 64 20 74 6f 20 6d 61  ndex added to ma
2992c 6b 65 20 74 68 65 20 6e 61 6d 65 20 75 6e 69 71  ke the name uniq
2992d 75 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a  ue */.  Column *
2992e 61 43 6f 6c 2c 20 2a 70 43 6f 6c 3b 20 20 20 20  aCol, *pCol;    
2992f 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69      /* For loopi
29930 6e 67 20 6f 76 65 72 20 72 65 73 75 6c 74 20 63  ng over result c
29931 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74 20  olumns */.  int 
29932 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  nCol;           
29933 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
29934 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
29935 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a  the result set *
29936 2f 0a 20 20 45 78 70 72 20 2a 70 3b 20 20 20 20  /.  Expr *p;    
29937 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29938 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 66 6f  /* Expression fo
29939 72 20 61 20 73 69 6e 67 6c 65 20 72 65 73 75 6c  r a single resul
2993a 74 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68  t column */.  ch
2993b 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20  ar *zName;      
2993c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
2993d 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e  umn name */.  in
2993e 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20 20  t nName;        
2993f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
29940 65 20 6f 66 20 6e 61 6d 65 20 69 6e 20 7a 4e 61  e of name in zNa
29941 6d 65 5b 5d 20 2a 2f 0a 0a 20 20 2a 70 6e 43 6f  me[] */..  *pnCo
29942 6c 20 3d 20 6e 43 6f 6c 20 3d 20 70 45 4c 69 73  l = nCol = pELis
29943 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 61 43 6f 6c  t->nExpr;.  aCol
29944 20 3d 20 2a 70 61 43 6f 6c 20 3d 20 73 71 6c 69   = *paCol = sqli
29945 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
29946 64 62 2c 20 73 69 7a 65 6f 66 28 61 43 6f 6c 5b  db, sizeof(aCol[
29947 30 5d 29 2a 6e 43 6f 6c 29 3b 0a 20 20 69 66 28  0])*nCol);.  if(
29948 20 61 43 6f 6c 3d 3d 30 20 29 20 72 65 74 75 72   aCol==0 ) retur
29949 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
2994a 20 20 66 6f 72 28 69 3d 30 2c 20 70 43 6f 6c 3d    for(i=0, pCol=
2994b 61 43 6f 6c 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  aCol; i<nCol; i+
2994c 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20  +, pCol++){.    
2994d 2f 2a 20 47 65 74 20 61 6e 20 61 70 70 72 6f 70  /* Get an approp
2994e 72 69 61 74 65 20 6e 61 6d 65 20 66 6f 72 20 74  riate name for t
2994f 68 65 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2f  he column.    */
29950 0a 20 20 20 20 70 20 3d 20 70 45 4c 69 73 74 2d  .    p = pEList-
29951 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[i].pExpr;.   
29952 20 61 73 73 65 72 74 28 20 70 2d 3e 70 52 69 67   assert( p->pRig
29953 68 74 3d 3d 30 20 7c 7c 20 45 78 70 72 48 61 73  ht==0 || ExprHas
29954 50 72 6f 70 65 72 74 79 28 70 2d 3e 70 52 69 67  Property(p->pRig
29955 68 74 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29  ht, EP_IntValue)
29956 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
29957 7c 7c 20 70 2d 3e 70 52 69 67 68 74 2d 3e 75 2e  || p->pRight->u.
29958 7a 54 6f 6b 65 6e 3d 3d 30 20 7c 7c 20 70 2d 3e  zToken==0 || p->
29959 70 52 69 67 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e  pRight->u.zToken
2995a 5b 30 5d 21 3d 30 20 29 3b 0a 20 20 20 20 69 66  [0]!=0 );.    if
2995b 28 20 28 7a 4e 61 6d 65 20 3d 20 70 45 4c 69 73  ( (zName = pELis
2995c 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 21 3d  t->a[i].zName)!=
2995d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66  0 ){.      /* If
2995e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74   the column cont
2995f 61 69 6e 73 20 61 6e 20 22 41 53 20 3c 6e 61 6d  ains an "AS <nam
29960 65 3e 22 20 70 68 72 61 73 65 2c 20 75 73 65 20  e>" phrase, use 
29961 3c 6e 61 6d 65 3e 20 61 73 20 74 68 65 20 6e 61  <name> as the na
29962 6d 65 20 2a 2f 0a 20 20 20 20 20 20 7a 4e 61 6d  me */.      zNam
29963 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  e = sqlite3DbStr
29964 44 75 70 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a  Dup(db, zName);.
29965 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
29966 20 45 78 70 72 20 2a 70 43 6f 6c 45 78 70 72 20   Expr *pColExpr 
29967 3d 20 70 3b 20 20 2f 2a 20 54 68 65 20 65 78 70  = p;  /* The exp
29968 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20  ression that is 
29969 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d  the result colum
2996a 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20  n name */.      
2996b 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20  Table *pTab;    
2996c 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 61 73       /* Table as
2996d 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
2996e 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  is expression */
2996f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 43  .      while( pC
29970 6f 6c 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 44  olExpr->op==TK_D
29971 4f 54 20 29 20 70 43 6f 6c 45 78 70 72 20 3d 20  OT ) pColExpr = 
29972 70 43 6f 6c 45 78 70 72 2d 3e 70 52 69 67 68 74  pColExpr->pRight
29973 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c  ;.      if( pCol
29974 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c  Expr->op==TK_COL
29975 55 4d 4e 20 26 26 20 41 4c 57 41 59 53 28 70 43  UMN && ALWAYS(pC
29976 6f 6c 45 78 70 72 2d 3e 70 54 61 62 21 3d 30 29  olExpr->pTab!=0)
29977 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46   ){.        /* F
29978 6f 72 20 63 6f 6c 75 6d 6e 73 20 75 73 65 20 74  or columns use t
29979 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 6e  he column name n
2997a 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  ame */.        i
2997b 6e 74 20 69 43 6f 6c 20 3d 20 70 43 6f 6c 45 78  nt iCol = pColEx
2997c 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20  pr->iColumn;.   
2997d 20 20 20 20 20 70 54 61 62 20 3d 20 70 43 6f 6c       pTab = pCol
2997e 45 78 70 72 2d 3e 70 54 61 62 3b 0a 20 20 20 20  Expr->pTab;.    
2997f 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29      if( iCol<0 )
29980 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 69 50   iCol = pTab->iP
29981 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61  Key;.        zNa
29982 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  me = sqlite3MPri
29983 6e 74 66 28 64 62 2c 20 22 25 73 22 2c 0a 20 20  ntf(db, "%s",.  
29984 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
29985 43 6f 6c 3e 3d 30 20 3f 20 70 54 61 62 2d 3e 61  Col>=0 ? pTab->a
29986 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 20  Col[iCol].zName 
29987 3a 20 22 72 6f 77 69 64 22 29 3b 0a 20 20 20 20  : "rowid");.    
29988 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 6f 6c    }else if( pCol
29989 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20  Expr->op==TK_ID 
2998a 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ){.        asser
2998b 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  t( !ExprHasPrope
2998c 72 74 79 28 70 43 6f 6c 45 78 70 72 2c 20 45 50  rty(pColExpr, EP
2998d 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20  _IntValue) );.  
2998e 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71        zName = sq
2998f 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
29990 20 22 25 73 22 2c 20 70 43 6f 6c 45 78 70 72 2d   "%s", pColExpr-
29991 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20  >u.zToken);.    
29992 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
29993 20 2f 2a 20 55 73 65 20 74 68 65 20 6f 72 69 67   /* Use the orig
29994 69 6e 61 6c 20 74 65 78 74 20 6f 66 20 74 68 65  inal text of the
29995 20 63 6f 6c 75 6d 6e 20 65 78 70 72 65 73 73 69   column expressi
29996 6f 6e 20 61 73 20 69 74 73 20 6e 61 6d 65 20 2a  on as its name *
29997 2f 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 20  /.        zName 
29998 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
29999 28 64 62 2c 20 22 25 73 22 2c 20 70 45 4c 69 73  (db, "%s", pELis
2999a 74 2d 3e 61 5b 69 5d 2e 7a 53 70 61 6e 29 3b 0a  t->a[i].zSpan);.
2999b 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2999c 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
2999d 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
2999e 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2999f 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  , zName);.      
299a0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
299a1 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74    /* Make sure t
299a2 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 69  he column name i
299a3 73 20 75 6e 69 71 75 65 2e 20 20 49 66 20 74 68  s unique.  If th
299a4 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 75 6e  e name is not un
299a5 69 71 75 65 2c 0a 20 20 20 20 2a 2a 20 61 70 70  ique,.    ** app
299a6 65 6e 64 20 61 20 69 6e 74 65 67 65 72 20 74 6f  end a integer to
299a7 20 74 68 65 20 6e 61 6d 65 20 73 6f 20 74 68 61   the name so tha
299a8 74 20 69 74 20 62 65 63 6f 6d 65 73 20 75 6e 69  t it becomes uni
299a9 71 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  que..    */.    
299aa 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53  nName = sqlite3S
299ab 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a  trlen30(zName);.
299ac 20 20 20 20 66 6f 72 28 6a 3d 63 6e 74 3d 30 3b      for(j=cnt=0;
299ad 20 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20   j<i; j++){.    
299ae 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
299af 49 43 6d 70 28 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61  ICmp(aCol[j].zNa
299b0 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b  me, zName)==0 ){
299b1 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a  .        char *z
299b2 4e 65 77 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20  NewName;.       
299b3 20 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d 20   zName[nName] = 
299b4 30 3b 0a 20 20 20 20 20 20 20 20 7a 4e 65 77 4e  0;.        zNewN
299b5 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ame = sqlite3MPr
299b6 69 6e 74 66 28 64 62 2c 20 22 25 73 3a 25 64 22  intf(db, "%s:%d"
299b7 2c 20 7a 4e 61 6d 65 2c 20 2b 2b 63 6e 74 29 3b  , zName, ++cnt);
299b8 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
299b9 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65  DbFree(db, zName
299ba 29 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d 65  );.        zName
299bb 20 3d 20 7a 4e 65 77 4e 61 6d 65 3b 0a 20 20 20   = zNewName;.   
299bc 20 20 20 20 20 6a 20 3d 20 2d 31 3b 0a 20 20 20       j = -1;.   
299bd 20 20 20 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d       if( zName==
299be 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  0 ) break;.     
299bf 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 6f   }.    }.    pCo
299c0 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65  l->zName = zName
299c1 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e  ;.  }.  if( db->
299c2 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
299c3 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 69      for(j=0; j<i
299c4 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  ; j++){.      sq
299c5 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
299c6 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 29 3b 0a  aCol[j].zName);.
299c7 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
299c8 33 44 62 46 72 65 65 28 64 62 2c 20 61 43 6f 6c  3DbFree(db, aCol
299c9 29 3b 0a 20 20 20 20 2a 70 61 43 6f 6c 20 3d 20  );.    *paCol = 
299ca 30 3b 0a 20 20 20 20 2a 70 6e 43 6f 6c 20 3d 20  0;.    *pnCol = 
299cb 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  0;.    return SQ
299cc 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
299cd 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
299ce 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  OK;.}../*.** Add
299cf 20 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c 61 74   type and collat
299d0 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ion information 
299d1 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 6c 69 73 74  to a column list
299d2 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 61 20 53   based on.** a S
299d3 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
299d4 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 63 6f 6c 75  .** .** The colu
299d5 6d 6e 20 6c 69 73 74 20 70 72 65 73 75 6d 61 62  mn list presumab
299d6 6c 79 20 63 61 6d 65 20 66 72 6f 6d 20 73 65 6c  ly came from sel
299d7 65 63 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 46 72  ectColumnNamesFr
299d8 6f 6d 45 78 70 72 4c 69 73 74 28 29 2e 0a 2a 2a  omExprList()..**
299d9 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74   The column list
299da 20 68 61 73 20 6f 6e 6c 79 20 6e 61 6d 65 73 2c   has only names,
299db 20 6e 6f 74 20 74 79 70 65 73 20 6f 72 20 63 6f   not types or co
299dc 6c 6c 61 74 69 6f 6e 73 2e 20 20 54 68 69 73 0a  llations.  This.
299dd 2a 2a 20 72 6f 75 74 69 6e 65 20 67 6f 65 73 20  ** routine goes 
299de 74 68 72 6f 75 67 68 20 61 6e 64 20 61 64 64 73  through and adds
299df 20 74 68 65 20 74 79 70 65 73 20 61 6e 64 20 63   the types and c
299e0 6f 6c 6c 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  ollations..**.**
299e1 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
299e2 71 75 69 72 65 73 20 74 68 61 74 20 61 6c 6c 20  quires that all 
299e3 69 64 65 6e 74 69 66 69 65 72 73 20 69 6e 20 74  identifiers in t
299e4 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 73 74 61  he SELECT.** sta
299e5 74 65 6d 65 6e 74 20 62 65 20 72 65 73 6f 6c 76  tement be resolv
299e6 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
299e7 69 64 20 73 65 6c 65 63 74 41 64 64 43 6f 6c 75  id selectAddColu
299e8 6d 6e 54 79 70 65 41 6e 64 43 6f 6c 6c 61 74 69  mnTypeAndCollati
299e9 6f 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  on(.  Parse *pPa
299ea 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  rse,        /* P
299eb 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 73 20  arsing contexts 
299ec 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 2c 20 20  */.  int nCol,  
299ed 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
299ee 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
299ef 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61 43 6f  */.  Column *aCo
299f0 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69  l,         /* Li
299f1 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f  st of columns */
299f2 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  .  Select *pSele
299f3 63 74 20 20 20 20 20 20 20 2f 2a 20 53 45 4c 45  ct       /* SELE
299f4 43 54 20 75 73 65 64 20 74 6f 20 64 65 74 65 72  CT used to deter
299f5 6d 69 6e 65 20 74 79 70 65 73 20 61 6e 64 20 63  mine types and c
299f6 6f 6c 6c 61 74 69 6f 6e 73 20 2a 2f 0a 29 7b 0a  ollations */.){.
299f7 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
299f8 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 4e 61  pParse->db;.  Na
299f9 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20  meContext sNC;. 
299fa 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20   Column *pCol;. 
299fb 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
299fc 0a 20 20 69 6e 74 20 69 3b 0a 20 20 45 78 70 72  .  int i;.  Expr
299fd 20 2a 70 3b 0a 20 20 73 74 72 75 63 74 20 45 78   *p;.  struct Ex
299fe 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61 3b 0a  prList_item *a;.
299ff 0a 20 20 61 73 73 65 72 74 28 20 70 53 65 6c 65  .  assert( pSele
29a00 63 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ct!=0 );.  asser
29a01 74 28 20 28 70 53 65 6c 65 63 74 2d 3e 73 65 6c  t( (pSelect->sel
29a02 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73 6f 6c  Flags & SF_Resol
29a03 76 65 64 29 21 3d 30 20 29 3b 0a 20 20 61 73 73  ved)!=0 );.  ass
29a04 65 72 74 28 20 6e 43 6f 6c 3d 3d 70 53 65 6c 65  ert( nCol==pSele
29a05 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70  ct->pEList->nExp
29a06 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  r || db->mallocF
29a07 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 64  ailed );.  if( d
29a08 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
29a09 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d 73  ) return;.  mems
29a0a 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65  et(&sNC, 0, size
29a0b 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e 43 2e  of(sNC));.  sNC.
29a0c 70 53 72 63 4c 69 73 74 20 3d 20 70 53 65 6c 65  pSrcList = pSele
29a0d 63 74 2d 3e 70 53 72 63 3b 0a 20 20 61 20 3d 20  ct->pSrc;.  a = 
29a0e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d  pSelect->pEList-
29a0f 3e 61 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70  >a;.  for(i=0, p
29a10 43 6f 6c 3d 61 43 6f 6c 3b 20 69 3c 6e 43 6f 6c  Col=aCol; i<nCol
29a11 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a  ; i++, pCol++){.
29a12 20 20 20 20 70 20 3d 20 61 5b 69 5d 2e 70 45 78      p = a[i].pEx
29a13 70 72 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 54  pr;.    pCol->zT
29a14 79 70 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ype = sqlite3DbS
29a15 74 72 44 75 70 28 64 62 2c 20 63 6f 6c 75 6d 6e  trDup(db, column
29a16 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20 30 2c  Type(&sNC, p, 0,
29a17 20 30 2c 20 30 29 29 3b 0a 20 20 20 20 70 43 6f   0, 0));.    pCo
29a18 6c 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 73 71  l->affinity = sq
29a19 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74  lite3ExprAffinit
29a1a 79 28 70 29 3b 0a 20 20 20 20 69 66 28 20 70 43  y(p);.    if( pC
29a1b 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 30 20  ol->affinity==0 
29a1c 29 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79  ) pCol->affinity
29a1d 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f   = SQLITE_AFF_NO
29a1e 4e 45 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20  NE;.    pColl = 
29a1f 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53  sqlite3ExprCollS
29a20 65 71 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20  eq(pParse, p);. 
29a21 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a     if( pColl ){.
29a22 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a 43 6f 6c        pCol->zCol
29a23 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  l = sqlite3DbStr
29a24 44 75 70 28 64 62 2c 20 70 43 6f 6c 6c 2d 3e 7a  Dup(db, pColl->z
29a25 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Name);.    }.  }
29a26 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20  .}../*.** Given 
29a27 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
29a28 6e 74 2c 20 67 65 6e 65 72 61 74 65 20 61 20 54  nt, generate a T
29a29 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 74  able structure t
29a2a 68 61 74 20 64 65 73 63 72 69 62 65 73 0a 2a 2a  hat describes.**
29a2b 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
29a2c 6f 66 20 74 68 61 74 20 53 45 4c 45 43 54 2e 0a  of that SELECT..
29a2d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
29a2e 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33  E Table *sqlite3
29a2f 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63  ResultSetOfSelec
29a30 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  t(Parse *pParse,
29a31 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
29a32 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  ){.  Table *pTab
29a33 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
29a34 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
29a35 69 6e 74 20 73 61 76 65 64 46 6c 61 67 73 3b 0a  int savedFlags;.
29a36 0a 20 20 73 61 76 65 64 46 6c 61 67 73 20 3d 20  .  savedFlags = 
29a37 64 62 2d 3e 66 6c 61 67 73 3b 0a 20 20 64 62 2d  db->flags;.  db-
29a38 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 54  >flags &= ~SQLIT
29a39 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 3b 0a  E_FullColNames;.
29a3a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53    db->flags |= S
29a3b 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61  QLITE_ShortColNa
29a3c 6d 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 53 65  mes;.  sqlite3Se
29a3d 6c 65 63 74 50 72 65 70 28 70 50 61 72 73 65 2c  lectPrep(pParse,
29a3e 20 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20   pSelect, 0);.  
29a3f 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
29a40 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77   ) return 0;.  w
29a41 68 69 6c 65 28 20 70 53 65 6c 65 63 74 2d 3e 70  hile( pSelect->p
29a42 50 72 69 6f 72 20 29 20 70 53 65 6c 65 63 74 20  Prior ) pSelect 
29a43 3d 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f  = pSelect->pPrio
29a44 72 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 3d  r;.  db->flags =
29a45 20 73 61 76 65 64 46 6c 61 67 73 3b 0a 20 20 70   savedFlags;.  p
29a46 54 61 62 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  Tab = sqlite3DbM
29a47 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
29a48 7a 65 6f 66 28 54 61 62 6c 65 29 20 29 3b 0a 20  zeof(Table) );. 
29a49 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a   if( pTab==0 ){.
29a4a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
29a4b 7d 0a 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74  }.  /* The sqlit
29a4c 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c  e3ResultSetOfSel
29a4d 65 63 74 28 29 20 69 73 20 6f 6e 6c 79 20 75 73  ect() is only us
29a4e 65 64 20 6e 20 63 6f 6e 74 65 78 74 73 20 77 68  ed n contexts wh
29a4f 65 72 65 20 6c 6f 6f 6b 61 73 69 64 65 0a 20 20  ere lookaside.  
29a50 2a 2a 20 69 73 20 64 69 73 61 62 6c 65 64 2c 20  ** is disabled, 
29a51 73 6f 20 77 65 20 6d 69 67 68 74 20 61 73 20 77  so we might as w
29a52 65 6c 6c 20 68 61 72 64 2d 63 6f 64 65 20 70 54  ell hard-code pT
29a53 61 62 2d 3e 64 62 4d 65 6d 20 74 6f 20 4e 55 4c  ab->dbMem to NUL
29a54 4c 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  L. */.  assert( 
29a55 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45  db->lookaside.bE
29a56 6e 61 62 6c 65 64 3d 3d 30 20 29 3b 0a 20 20 70  nabled==0 );.  p
29a57 54 61 62 2d 3e 64 62 4d 65 6d 20 3d 20 30 3b 0a  Tab->dbMem = 0;.
29a58 20 20 70 54 61 62 2d 3e 6e 52 65 66 20 3d 20 31    pTab->nRef = 1
29a59 3b 0a 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 20  ;.  pTab->zName 
29a5a 3d 20 30 3b 0a 20 20 73 65 6c 65 63 74 43 6f 6c  = 0;.  selectCol
29a5b 75 6d 6e 73 46 72 6f 6d 45 78 70 72 4c 69 73 74  umnsFromExprList
29a5c 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74  (pParse, pSelect
29a5d 2d 3e 70 45 4c 69 73 74 2c 20 26 70 54 61 62 2d  ->pEList, &pTab-
29a5e 3e 6e 43 6f 6c 2c 20 26 70 54 61 62 2d 3e 61 43  >nCol, &pTab->aC
29a5f 6f 6c 29 3b 0a 20 20 73 65 6c 65 63 74 41 64 64  ol);.  selectAdd
29a60 43 6f 6c 75 6d 6e 54 79 70 65 41 6e 64 43 6f 6c  ColumnTypeAndCol
29a61 6c 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70  lation(pParse, p
29a62 54 61 62 2d 3e 6e 43 6f 6c 2c 20 70 54 61 62 2d  Tab->nCol, pTab-
29a63 3e 61 43 6f 6c 2c 20 70 53 65 6c 65 63 74 29 3b  >aCol, pSelect);
29a64 0a 20 20 70 54 61 62 2d 3e 69 50 4b 65 79 20 3d  .  pTab->iPKey =
29a65 20 2d 31 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d   -1;.  if( db->m
29a66 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
29a67 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65     sqlite3Delete
29a68 54 61 62 6c 65 28 70 54 61 62 29 3b 0a 20 20 20  Table(pTab);.   
29a69 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
29a6a 20 72 65 74 75 72 6e 20 70 54 61 62 3b 0a 7d 0a   return pTab;.}.
29a6b 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 56 44 42  ./*.** Get a VDB
29a6c 45 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20  E for the given 
29a6d 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 2e 20  parser context. 
29a6e 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 6f 6e   Create a new on
29a6f 65 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a  e if necessary..
29a70 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  ** If an error o
29a71 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 4e 55  ccurs, return NU
29a72 4c 4c 20 61 6e 64 20 6c 65 61 76 65 20 61 20 6d  LL and leave a m
29a73 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65  essage in pParse
29a74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
29a75 41 54 45 20 56 64 62 65 20 2a 73 71 6c 69 74 65  ATE Vdbe *sqlite
29a76 33 47 65 74 56 64 62 65 28 50 61 72 73 65 20 2a  3GetVdbe(Parse *
29a77 70 50 61 72 73 65 29 7b 0a 20 20 56 64 62 65 20  pParse){.  Vdbe 
29a78 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
29a79 62 65 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29  be;.  if( v==0 )
29a7a 7b 0a 20 20 20 20 76 20 3d 20 70 50 61 72 73 65  {.    v = pParse
29a7b 2d 3e 70 56 64 62 65 20 3d 20 73 71 6c 69 74 65  ->pVdbe = sqlite
29a7c 33 56 64 62 65 43 72 65 61 74 65 28 70 50 61 72  3VdbeCreate(pPar
29a7d 73 65 2d 3e 64 62 29 3b 0a 23 69 66 6e 64 65 66  se->db);.#ifndef
29a7e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41   SQLITE_OMIT_TRA
29a7f 43 45 0a 20 20 20 20 69 66 28 20 76 20 29 7b 0a  CE.    if( v ){.
29a80 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29a81 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 54 72  eAddOp0(v, OP_Tr
29a82 61 63 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  ace);.    }.#end
29a83 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  if.  }.  return 
29a84 76 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d  v;.}.../*.** Com
29a85 70 75 74 65 20 74 68 65 20 69 4c 69 6d 69 74 20  pute the iLimit 
29a86 61 6e 64 20 69 4f 66 66 73 65 74 20 66 69 65 6c  and iOffset fiel
29a87 64 73 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  ds of the SELECT
29a88 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a   based on the.**
29a89 20 70 4c 69 6d 69 74 20 61 6e 64 20 70 4f 66 66   pLimit and pOff
29a8a 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 73 2e  set expressions.
29a8b 20 20 70 4c 69 6d 69 74 20 61 6e 64 20 70 4f 66    pLimit and pOf
29a8c 66 73 65 74 20 68 6f 6c 64 20 74 68 65 20 65 78  fset hold the ex
29a8d 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 74 68 61  pressions.** tha
29a8e 74 20 61 70 70 65 61 72 20 69 6e 20 74 68 65 20  t appear in the 
29a8f 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61  original SQL sta
29a90 74 65 6d 65 6e 74 20 61 66 74 65 72 20 74 68 65  tement after the
29a91 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45   LIMIT and OFFSE
29a92 54 0a 2a 2a 20 6b 65 79 77 6f 72 64 73 2e 20 20  T.** keywords.  
29a93 4f 72 20 4e 55 4c 4c 20 69 66 20 74 68 6f 73 65  Or NULL if those
29a94 20 6b 65 79 77 6f 72 64 73 20 61 72 65 20 6f 6d   keywords are om
29a95 69 74 74 65 64 2e 20 69 4c 69 6d 69 74 20 61 6e  itted. iLimit an
29a96 64 20 69 4f 66 66 73 65 74 20 0a 2a 2a 20 61 72  d iOffset .** ar
29a97 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 6d 65  e the integer me
29a98 6d 6f 72 79 20 72 65 67 69 73 74 65 72 20 6e 75  mory register nu
29a99 6d 62 65 72 73 20 66 6f 72 20 63 6f 75 6e 74 65  mbers for counte
29a9a 72 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75  rs used to compu
29a9b 74 65 20 0a 2a 2a 20 74 68 65 20 6c 69 6d 69 74  te .** the limit
29a9c 20 61 6e 64 20 6f 66 66 73 65 74 2e 20 20 49 66   and offset.  If
29a9d 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6c 69 6d   there is no lim
29a9e 69 74 20 61 6e 64 2f 6f 72 20 6f 66 66 73 65 74  it and/or offset
29a9f 2c 20 74 68 65 6e 20 0a 2a 2a 20 69 4c 69 6d 69  , then .** iLimi
29aa0 74 20 61 6e 64 20 69 4f 66 66 73 65 74 20 61 72  t and iOffset ar
29aa1 65 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a 2a  e negative..**.*
29aa2 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
29aa3 68 61 6e 67 65 73 20 74 68 65 20 76 61 6c 75 65  hanges the value
29aa4 73 20 6f 66 20 69 4c 69 6d 69 74 20 61 6e 64 20  s of iLimit and 
29aa5 69 4f 66 66 73 65 74 20 6f 6e 6c 79 20 69 66 0a  iOffset only if.
29aa6 2a 2a 20 61 20 6c 69 6d 69 74 20 6f 72 20 6f 66  ** a limit or of
29aa7 66 73 65 74 20 69 73 20 64 65 66 69 6e 65 64 20  fset is defined 
29aa8 62 79 20 70 4c 69 6d 69 74 20 61 6e 64 20 70 4f  by pLimit and pO
29aa9 66 66 73 65 74 2e 20 20 69 4c 69 6d 69 74 20 61  ffset.  iLimit a
29aaa 6e 64 0a 2a 2a 20 69 4f 66 66 73 65 74 20 73 68  nd.** iOffset sh
29aab 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 70  ould have been p
29aac 72 65 73 65 74 20 74 6f 20 61 70 70 72 6f 70 72  reset to appropr
29aad 69 61 74 65 20 64 65 66 61 75 6c 74 20 76 61 6c  iate default val
29aae 75 65 73 0a 2a 2a 20 28 75 73 75 61 6c 6c 79 20  ues.** (usually 
29aaf 62 75 74 20 6e 6f 74 20 61 6c 77 61 79 73 20 2d  but not always -
29ab0 31 29 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c  1) prior to call
29ab1 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ing this routine
29ab2 2e 0a 2a 2a 20 4f 6e 6c 79 20 69 66 20 70 4c 69  ..** Only if pLi
29ab3 6d 69 74 21 3d 30 20 6f 72 20 70 4f 66 66 73 65  mit!=0 or pOffse
29ab4 74 21 3d 30 20 64 6f 20 74 68 65 20 6c 69 6d 69  t!=0 do the limi
29ab5 74 20 72 65 67 69 73 74 65 72 73 20 67 65 74 0a  t registers get.
29ab6 2a 2a 20 72 65 64 65 66 69 6e 65 64 2e 20 20 54  ** redefined.  T
29ab7 68 65 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 70 65  he UNION ALL ope
29ab8 72 61 74 6f 72 20 75 73 65 73 20 74 68 69 73 20  rator uses this 
29ab9 70 72 6f 70 65 72 74 79 20 74 6f 20 66 6f 72 63  property to forc
29aba 65 0a 2a 2a 20 74 68 65 20 72 65 75 73 65 20 6f  e.** the reuse o
29abb 66 20 74 68 65 20 73 61 6d 65 20 6c 69 6d 69 74  f the same limit
29abc 20 61 6e 64 20 6f 66 66 73 65 74 20 72 65 67 69   and offset regi
29abd 73 74 65 72 73 20 61 63 72 6f 73 73 20 6d 75 6c  sters across mul
29abe 74 69 70 6c 65 0a 2a 2a 20 53 45 4c 45 43 54 20  tiple.** SELECT 
29abf 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73  statements..*/.s
29ac0 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75  tatic void compu
29ac1 74 65 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73  teLimitRegisters
29ac2 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
29ac3 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 69  Select *p, int i
29ac4 42 72 65 61 6b 29 7b 0a 20 20 56 64 62 65 20 2a  Break){.  Vdbe *
29ac5 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 4c 69  v = 0;.  int iLi
29ac6 6d 69 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69  mit = 0;.  int i
29ac7 4f 66 66 73 65 74 3b 0a 20 20 69 6e 74 20 61 64  Offset;.  int ad
29ac8 64 72 31 3b 0a 20 20 69 66 28 20 70 2d 3e 69 4c  dr1;.  if( p->iL
29ac9 69 6d 69 74 20 29 20 72 65 74 75 72 6e 3b 0a 0a  imit ) return;..
29aca 20 20 2f 2a 20 0a 20 20 2a 2a 20 22 4c 49 4d 49    /* .  ** "LIMI
29acb 54 20 2d 31 22 20 61 6c 77 61 79 73 20 73 68 6f  T -1" always sho
29acc 77 73 20 61 6c 6c 20 72 6f 77 73 2e 20 20 54 68  ws all rows.  Th
29acd 65 72 65 20 69 73 20 73 6f 6d 65 0a 20 20 2a 2a  ere is some.  **
29ace 20 63 6f 6e 74 72 61 76 65 72 73 79 20 61 62 6f   contraversy abo
29acf 75 74 20 77 68 61 74 20 74 68 65 20 63 6f 72 72  ut what the corr
29ad0 65 63 74 20 62 65 68 61 76 69 6f 72 20 73 68 6f  ect behavior sho
29ad1 75 6c 64 20 62 65 2e 0a 20 20 2a 2a 20 54 68 65  uld be..  ** The
29ad2 20 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65   current impleme
29ad3 6e 74 61 74 69 6f 6e 20 69 6e 74 65 72 70 72 65  ntation interpre
29ad4 74 73 20 22 4c 49 4d 49 54 20 30 22 20 74 6f 20  ts "LIMIT 0" to 
29ad5 6d 65 61 6e 0a 20 20 2a 2a 20 6e 6f 20 72 6f 77  mean.  ** no row
29ad6 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  s..  */.  sqlite
29ad7 33 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28  3ExprCacheClear(
29ad8 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73 65 72  pParse);.  asser
29ad9 74 28 20 70 2d 3e 70 4f 66 66 73 65 74 3d 3d 30  t( p->pOffset==0
29ada 20 7c 7c 20 70 2d 3e 70 4c 69 6d 69 74 21 3d 30   || p->pLimit!=0
29adb 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 4c 69   );.  if( p->pLi
29adc 6d 69 74 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4c  mit ){.    p->iL
29add 69 6d 69 74 20 3d 20 69 4c 69 6d 69 74 20 3d 20  imit = iLimit = 
29ade 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
29adf 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47      v = sqlite3G
29ae0 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
29ae1 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 76 3d      if( NEVER(v=
29ae2 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 20 20 2f  =0) ) return;  /
29ae3 2a 20 56 44 42 45 20 73 68 6f 75 6c 64 20 68 61  * VDBE should ha
29ae4 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ve already been 
29ae5 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 20  allocated */.   
29ae6 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
29ae7 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 4c 69 6d  (pParse, p->pLim
29ae8 69 74 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20  it, iLimit);.   
29ae9 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29aea 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49  p1(v, OP_MustBeI
29aeb 6e 74 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20  nt, iLimit);.   
29aec 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
29aed 20 22 4c 49 4d 49 54 20 63 6f 75 6e 74 65 72 22   "LIMIT counter"
29aee 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ));.    sqlite3V
29aef 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29af0 49 66 5a 65 72 6f 2c 20 69 4c 69 6d 69 74 2c 20  IfZero, iLimit, 
29af1 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 69 66 28  iBreak);.    if(
29af2 20 70 2d 3e 70 4f 66 66 73 65 74 20 29 7b 0a 20   p->pOffset ){. 
29af3 20 20 20 20 20 70 2d 3e 69 4f 66 66 73 65 74 20       p->iOffset 
29af4 3d 20 69 4f 66 66 73 65 74 20 3d 20 2b 2b 70 50  = iOffset = ++pP
29af5 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
29af6 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b    pParse->nMem++
29af7 3b 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ;   /* Allocate 
29af8 61 6e 20 65 78 74 72 61 20 72 65 67 69 73 74 65  an extra registe
29af9 72 20 66 6f 72 20 6c 69 6d 69 74 2b 6f 66 66 73  r for limit+offs
29afa 65 74 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  et */.      sqli
29afb 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
29afc 73 65 2c 20 70 2d 3e 70 4f 66 66 73 65 74 2c 20  se, p->pOffset, 
29afd 69 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20  iOffset);.      
29afe 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29aff 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e  1(v, OP_MustBeIn
29b00 74 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 20 20  t, iOffset);.   
29b01 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
29b02 76 2c 20 22 4f 46 46 53 45 54 20 63 6f 75 6e 74  v, "OFFSET count
29b03 65 72 22 29 29 3b 0a 20 20 20 20 20 20 61 64 64  er"));.      add
29b04 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  r1 = sqlite3Vdbe
29b05 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 50  AddOp1(v, OP_IfP
29b06 6f 73 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 20  os, iOffset);.  
29b07 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29b08 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
29b09 67 65 72 2c 20 30 2c 20 69 4f 66 66 73 65 74 29  ger, 0, iOffset)
29b0a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
29b0b 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
29b0c 64 64 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  ddr1);.      sql
29b0d 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
29b0e 2c 20 4f 50 5f 41 64 64 2c 20 69 4c 69 6d 69 74  , OP_Add, iLimit
29b0f 2c 20 69 4f 66 66 73 65 74 2c 20 69 4f 66 66 73  , iOffset, iOffs
29b10 65 74 2b 31 29 3b 0a 20 20 20 20 20 20 56 64 62  et+1);.      Vdb
29b11 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4c 49  eComment((v, "LI
29b12 4d 49 54 2b 4f 46 46 53 45 54 22 29 29 3b 0a 20  MIT+OFFSET"));. 
29b13 20 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c       addr1 = sql
29b14 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
29b15 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 69 4c 69 6d  , OP_IfPos, iLim
29b16 69 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  it);.      sqlit
29b17 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
29b18 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20  OP_Integer, -1, 
29b19 69 4f 66 66 73 65 74 2b 31 29 3b 0a 20 20 20 20  iOffset+1);.    
29b1a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
29b1b 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 29 3b  pHere(v, addr1);
29b1c 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69  .    }.  }.}..#i
29b1d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
29b1e 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43  T_COMPOUND_SELEC
29b1f 54 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  T./*.** Return t
29b20 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 63  he appropriate c
29b21 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
29b22 65 20 66 6f 72 20 74 68 65 20 69 43 6f 6c 2d 74  e for the iCol-t
29b23 68 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a 20 74  h column of.** t
29b24 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 66 6f  he result set fo
29b25 72 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 2d 73  r the compound-s
29b26 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20  elect statement 
29b27 22 70 22 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c  "p".  Return NUL
29b28 4c 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75  L if.** the colu
29b29 6d 6e 20 68 61 73 20 6e 6f 20 64 65 66 61 75 6c  mn has no defaul
29b2a 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  t collating sequ
29b2b 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ence..**.** The 
29b2c 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
29b2d 63 65 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 6f  ce for the compo
29b2e 75 6e 64 20 73 65 6c 65 63 74 20 69 73 20 74 61  und select is ta
29b2f 6b 65 6e 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20  ken from the.** 
29b30 6c 65 66 74 2d 6d 6f 73 74 20 74 65 72 6d 20 6f  left-most term o
29b31 66 20 74 68 65 20 73 65 6c 65 63 74 20 74 68 61  f the select tha
29b32 74 20 68 61 73 20 61 20 63 6f 6c 6c 61 74 69 6e  t has a collatin
29b33 67 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2f 0a 73  g sequence..*/.s
29b34 74 61 74 69 63 20 43 6f 6c 6c 53 65 71 20 2a 6d  tatic CollSeq *m
29b35 75 6c 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65  ultiSelectCollSe
29b36 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  q(Parse *pParse,
29b37 20 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20   Select *p, int 
29b38 69 43 6f 6c 29 7b 0a 20 20 43 6f 6c 6c 53 65 71  iCol){.  CollSeq
29b39 20 2a 70 52 65 74 3b 0a 20 20 69 66 28 20 70 2d   *pRet;.  if( p-
29b3a 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20 70  >pPrior ){.    p
29b3b 52 65 74 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63  Ret = multiSelec
29b3c 74 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  tCollSeq(pParse,
29b3d 20 70 2d 3e 70 50 72 69 6f 72 2c 20 69 43 6f 6c   p->pPrior, iCol
29b3e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
29b3f 70 52 65 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  pRet = 0;.  }.  
29b40 61 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d 30 20  assert( iCol>=0 
29b41 29 3b 0a 20 20 69 66 28 20 70 52 65 74 3d 3d 30  );.  if( pRet==0
29b42 20 26 26 20 69 43 6f 6c 3c 70 2d 3e 70 45 4c 69   && iCol<p->pELi
29b43 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20  st->nExpr ){.   
29b44 20 70 52 65 74 20 3d 20 73 71 6c 69 74 65 33 45   pRet = sqlite3E
29b45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  xprCollSeq(pPars
29b46 65 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b  e, p->pEList->a[
29b47 69 43 6f 6c 5d 2e 70 45 78 70 72 29 3b 0a 20 20  iCol].pExpr);.  
29b48 7d 0a 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b  }.  return pRet;
29b49 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
29b4a 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e  ITE_OMIT_COMPOUN
29b4b 44 5f 53 45 4c 45 43 54 20 2a 2f 0a 0a 2f 2a 20  D_SELECT */../* 
29b4c 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e 63  Forward referenc
29b4d 65 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  e */.static int 
29b4e 6d 75 6c 74 69 53 65 6c 65 63 74 4f 72 64 65 72  multiSelectOrder
29b4f 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  By(.  Parse *pPa
29b50 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  rse,        /* P
29b51 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
29b52 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20  /.  Select *p,  
29b53 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
29b54 20 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20 53   right-most of S
29b55 45 4c 45 43 54 73 20 74 6f 20 62 65 20 63 6f 64  ELECTs to be cod
29b56 65 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65  ed */.  SelectDe
29b57 73 74 20 2a 70 44 65 73 74 20 20 20 20 20 2f 2a  st *pDest     /*
29b58 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68   What to do with
29b59 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 20 2a   query results *
29b5a 2f 0a 29 3b 0a 0a 0a 23 69 66 6e 64 65 66 20 53  /.);...#ifndef S
29b5b 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f  QLITE_OMIT_COMPO
29b5c 55 4e 44 5f 53 45 4c 45 43 54 0a 2f 2a 0a 2a 2a  UND_SELECT./*.**
29b5d 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
29b5e 20 63 61 6c 6c 65 64 20 74 6f 20 70 72 6f 63 65   called to proce
29b5f 73 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 71 75  ss a compound qu
29b60 65 72 79 20 66 6f 72 6d 20 66 72 6f 6d 0a 2a 2a  ery form from.**
29b61 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 73 65 70   two or more sep
29b62 61 72 61 74 65 20 71 75 65 72 69 65 73 20 75 73  arate queries us
29b63 69 6e 67 20 55 4e 49 4f 4e 2c 20 55 4e 49 4f 4e  ing UNION, UNION
29b64 20 41 4c 4c 2c 20 45 58 43 45 50 54 2c 20 6f 72   ALL, EXCEPT, or
29b65 0a 2a 2a 20 49 4e 54 45 52 53 45 43 54 0a 2a 2a  .** INTERSECT.**
29b66 0a 2a 2a 20 22 70 22 20 70 6f 69 6e 74 73 20 74  .** "p" points t
29b67 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  o the right-most
29b68 20 6f 66 20 74 68 65 20 74 77 6f 20 71 75 65 72   of the two quer
29b69 69 65 73 2e 20 20 74 68 65 20 71 75 65 72 79 20  ies.  the query 
29b6a 6f 6e 20 74 68 65 0a 2a 2a 20 6c 65 66 74 20 69  on the.** left i
29b6b 73 20 70 2d 3e 70 50 72 69 6f 72 2e 20 20 54 68  s p->pPrior.  Th
29b6c 65 20 6c 65 66 74 20 71 75 65 72 79 20 63 6f 75  e left query cou
29b6d 6c 64 20 61 6c 73 6f 20 62 65 20 61 20 63 6f 6d  ld also be a com
29b6e 70 6f 75 6e 64 20 71 75 65 72 79 0a 2a 2a 20 69  pound query.** i
29b6f 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 69  n which case thi
29b70 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 62  s routine will b
29b71 65 20 63 61 6c 6c 65 64 20 72 65 63 75 72 73 69  e called recursi
29b72 76 65 6c 79 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65  vely. .**.** The
29b73 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20   results of the 
29b74 74 6f 74 61 6c 20 71 75 65 72 79 20 61 72 65 20  total query are 
29b75 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e  to be written in
29b76 74 6f 20 61 20 64 65 73 74 69 6e 61 74 69 6f 6e  to a destination
29b77 0a 2a 2a 20 6f 66 20 74 79 70 65 20 65 44 65 73  .** of type eDes
29b78 74 20 77 69 74 68 20 70 61 72 61 6d 65 74 65 72  t with parameter
29b79 20 69 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20 45 78   iParm..**.** Ex
29b7a 61 6d 70 6c 65 20 31 3a 20 20 43 6f 6e 73 69 64  ample 1:  Consid
29b7b 65 72 20 61 20 74 68 72 65 65 2d 77 61 79 20 63  er a three-way c
29b7c 6f 6d 70 6f 75 6e 64 20 53 51 4c 20 73 74 61 74  ompound SQL stat
29b7d 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  ement..**.**    
29b7e 20 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 74   SELECT a FROM t
29b7f 31 20 55 4e 49 4f 4e 20 53 45 4c 45 43 54 20 62  1 UNION SELECT b
29b80 20 46 52 4f 4d 20 74 32 20 55 4e 49 4f 4e 20 53   FROM t2 UNION S
29b81 45 4c 45 43 54 20 63 20 46 52 4f 4d 20 74 33 0a  ELECT c FROM t3.
29b82 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 61 74 65  **.** This state
29b83 6d 65 6e 74 20 69 73 20 70 61 72 73 65 64 20 75  ment is parsed u
29b84 70 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  p as follows:.**
29b85 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 63  .**     SELECT c
29b86 20 46 52 4f 4d 20 74 33 0a 2a 2a 20 20 20 20 20   FROM t3.**     
29b87 20 7c 0a 2a 2a 20 20 20 20 20 20 60 2d 2d 2d 2d   |.**      `----
29b88 2d 3e 20 20 53 45 4c 45 43 54 20 62 20 46 52 4f  ->  SELECT b FRO
29b89 4d 20 74 32 0a 2a 2a 20 20 20 20 20 20 20 20 20  M t2.**         
29b8a 20 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20         |.**     
29b8b 20 20 20 20 20 20 20 20 20 20 20 60 2d 2d 2d 2d             `----
29b8c 2d 2d 3e 20 20 53 45 4c 45 43 54 20 61 20 46 52  -->  SELECT a FR
29b8d 4f 4d 20 74 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20  OM t1.**.** The 
29b8e 61 72 72 6f 77 73 20 69 6e 20 74 68 65 20 64 69  arrows in the di
29b8f 61 67 72 61 6d 20 61 62 6f 76 65 20 72 65 70 72  agram above repr
29b90 65 73 65 6e 74 20 74 68 65 20 53 65 6c 65 63 74  esent the Select
29b91 2e 70 50 72 69 6f 72 20 70 6f 69 6e 74 65 72 2e  .pPrior pointer.
29b92 0a 2a 2a 20 53 6f 20 69 66 20 74 68 69 73 20 72  .** So if this r
29b93 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
29b94 20 77 69 74 68 20 70 20 65 71 75 61 6c 20 74 6f   with p equal to
29b95 20 74 68 65 20 74 33 20 71 75 65 72 79 2c 20 74   the t3 query, t
29b96 68 65 6e 0a 2a 2a 20 70 50 72 69 6f 72 20 77 69  hen.** pPrior wi
29b97 6c 6c 20 62 65 20 74 68 65 20 74 32 20 71 75 65  ll be the t2 que
29b98 72 79 2e 20 20 70 2d 3e 6f 70 20 77 69 6c 6c 20  ry.  p->op will 
29b99 62 65 20 54 4b 5f 55 4e 49 4f 4e 20 69 6e 20 74  be TK_UNION in t
29b9a 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  his case..**.** 
29b9b 4e 6f 74 69 63 65 20 74 68 61 74 20 62 65 63 61  Notice that beca
29b9c 75 73 65 20 6f 66 20 74 68 65 20 77 61 79 20 53  use of the way S
29b9d 51 4c 69 74 65 20 70 61 72 73 65 73 20 63 6f 6d  QLite parses com
29b9e 70 6f 75 6e 64 20 53 45 4c 45 43 54 73 2c 20 74  pound SELECTs, t
29b9f 68 65 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c  he.** individual
29ba0 20 73 65 6c 65 63 74 73 20 61 6c 77 61 79 73 20   selects always 
29ba1 67 72 6f 75 70 20 66 72 6f 6d 20 6c 65 66 74 20  group from left 
29ba2 74 6f 20 72 69 67 68 74 2e 0a 2a 2f 0a 73 74 61  to right..*/.sta
29ba3 74 69 63 20 69 6e 74 20 6d 75 6c 74 69 53 65 6c  tic int multiSel
29ba4 65 63 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ect(.  Parse *pP
29ba5 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
29ba6 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
29ba7 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20  */.  Select *p, 
29ba8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
29ba9 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20  e right-most of 
29baa 53 45 4c 45 43 54 73 20 74 6f 20 62 65 20 63 6f  SELECTs to be co
29bab 64 65 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44  ded */.  SelectD
29bac 65 73 74 20 2a 70 44 65 73 74 20 20 20 20 20 2f  est *pDest     /
29bad 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74  * What to do wit
29bae 68 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 20  h query results 
29baf 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  */.){.  int rc =
29bb0 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f 2a   SQLITE_OK;   /*
29bb1 20 53 75 63 63 65 73 73 20 63 6f 64 65 20 66 72   Success code fr
29bb2 6f 6d 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20  om a subroutine 
29bb3 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 72  */.  Select *pPr
29bb4 69 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e  ior;       /* An
29bb5 6f 74 68 65 72 20 53 45 4c 45 43 54 20 69 6d 6d  other SELECT imm
29bb6 65 64 69 61 74 65 6c 79 20 74 6f 20 6f 75 72 20  ediately to our 
29bb7 6c 65 66 74 20 2a 2f 0a 20 20 56 64 62 65 20 2a  left */.  Vdbe *
29bb8 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
29bb9 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
29bba 20 74 6f 20 74 68 69 73 20 56 44 42 45 20 2a 2f   to this VDBE */
29bbb 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65  .  SelectDest de
29bbc 73 74 3b 20 20 20 20 20 20 2f 2a 20 41 6c 74 65  st;      /* Alte
29bbd 72 6e 61 74 69 76 65 20 64 61 74 61 20 64 65 73  rnative data des
29bbe 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 53 65  tination */.  Se
29bbf 6c 65 63 74 20 2a 70 44 65 6c 65 74 65 20 3d 20  lect *pDelete = 
29bc0 30 3b 20 20 2f 2a 20 43 68 61 69 6e 20 6f 66 20  0;  /* Chain of 
29bc1 73 69 6d 70 6c 65 20 73 65 6c 65 63 74 73 20 74  simple selects t
29bc2 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 73 71  o delete */.  sq
29bc3 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
29bc4 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
29bc5 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20  connection */.. 
29bc6 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68   /* Make sure th
29bc7 65 72 65 20 69 73 20 6e 6f 20 4f 52 44 45 52 20  ere is no ORDER 
29bc8 42 59 20 6f 72 20 4c 49 4d 49 54 20 63 6c 61 75  BY or LIMIT clau
29bc9 73 65 20 6f 6e 20 70 72 69 6f 72 20 53 45 4c 45  se on prior SELE
29bca 43 54 73 2e 20 20 4f 6e 6c 79 0a 20 20 2a 2a 20  CTs.  Only.  ** 
29bcb 74 68 65 20 6c 61 73 74 20 28 72 69 67 68 74 2d  the last (right-
29bcc 6d 6f 73 74 29 20 53 45 4c 45 43 54 20 69 6e 20  most) SELECT in 
29bcd 74 68 65 20 73 65 72 69 65 73 20 6d 61 79 20 68  the series may h
29bce 61 76 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20  ave an ORDER BY 
29bcf 6f 72 20 4c 49 4d 49 54 2e 0a 20 20 2a 2f 0a 20  or LIMIT..  */. 
29bd0 20 61 73 73 65 72 74 28 20 70 20 26 26 20 70 2d   assert( p && p-
29bd1 3e 70 50 72 69 6f 72 20 29 3b 20 20 2f 2a 20 43  >pPrior );  /* C
29bd2 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  alling function 
29bd3 67 75 61 72 61 6e 74 65 65 73 20 74 68 69 73 20  guarantees this 
29bd4 6d 75 63 68 20 2a 2f 0a 20 20 64 62 20 3d 20 70  much */.  db = p
29bd5 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 50 72  Parse->db;.  pPr
29bd6 69 6f 72 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b  ior = p->pPrior;
29bd7 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f  .  assert( pPrio
29bd8 72 2d 3e 70 52 69 67 68 74 6d 6f 73 74 21 3d 70  r->pRightmost!=p
29bd9 50 72 69 6f 72 20 29 3b 0a 20 20 61 73 73 65 72  Prior );.  asser
29bda 74 28 20 70 50 72 69 6f 72 2d 3e 70 52 69 67 68  t( pPrior->pRigh
29bdb 74 6d 6f 73 74 3d 3d 70 2d 3e 70 52 69 67 68 74  tmost==p->pRight
29bdc 6d 6f 73 74 20 29 3b 0a 20 20 64 65 73 74 20 3d  most );.  dest =
29bdd 20 2a 70 44 65 73 74 3b 0a 20 20 69 66 28 20 70   *pDest;.  if( p
29bde 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79 20  Prior->pOrderBy 
29bdf 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
29be0 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22 4f  rorMsg(pParse,"O
29be1 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 73  RDER BY clause s
29be2 68 6f 75 6c 64 20 63 6f 6d 65 20 61 66 74 65 72  hould come after
29be3 20 25 73 20 6e 6f 74 20 62 65 66 6f 72 65 22 2c   %s not before",
29be4 0a 20 20 20 20 20 20 73 65 6c 65 63 74 4f 70 4e  .      selectOpN
29be5 61 6d 65 28 70 2d 3e 6f 70 29 29 3b 0a 20 20 20  ame(p->op));.   
29be6 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74   rc = 1;.    got
29be7 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65  o multi_select_e
29be8 6e 64 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50  nd;.  }.  if( pP
29be9 72 69 6f 72 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a  rior->pLimit ){.
29bea 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
29beb 4d 73 67 28 70 50 61 72 73 65 2c 22 4c 49 4d 49  Msg(pParse,"LIMI
29bec 54 20 63 6c 61 75 73 65 20 73 68 6f 75 6c 64 20  T clause should 
29bed 63 6f 6d 65 20 61 66 74 65 72 20 25 73 20 6e 6f  come after %s no
29bee 74 20 62 65 66 6f 72 65 22 2c 0a 20 20 20 20 20  t before",.     
29bef 20 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28 70 2d   selectOpName(p-
29bf0 3e 6f 70 29 29 3b 0a 20 20 20 20 72 63 20 3d 20  >op));.    rc = 
29bf1 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74  1;.    goto mult
29bf2 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  i_select_end;.  
29bf3 7d 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  }..  v = sqlite3
29bf4 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
29bf5 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20  .  assert( v!=0 
29bf6 29 3b 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20  );  /* The VDBE 
29bf7 61 6c 72 65 61 64 79 20 63 72 65 61 74 65 64 20  already created 
29bf8 62 79 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  by calling funct
29bf9 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 43 72 65  ion */..  /* Cre
29bfa 61 74 65 20 74 68 65 20 64 65 73 74 69 6e 61 74  ate the destinat
29bfb 69 6f 6e 20 74 65 6d 70 6f 72 61 72 79 20 74 61  ion temporary ta
29bfc 62 6c 65 20 69 66 20 6e 65 63 65 73 73 61 72 79  ble if necessary
29bfd 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 65 73 74  .  */.  if( dest
29bfe 2e 65 44 65 73 74 3d 3d 53 52 54 5f 45 70 68 65  .eDest==SRT_Ephe
29bff 6d 54 61 62 20 29 7b 0a 20 20 20 20 61 73 73 65  mTab ){.    asse
29c00 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20 29 3b  rt( p->pEList );
29c01 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
29c02 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65  AddOp2(v, OP_Ope
29c03 6e 45 70 68 65 6d 65 72 61 6c 2c 20 64 65 73 74  nEphemeral, dest
29c04 2e 69 50 61 72 6d 2c 20 70 2d 3e 70 45 4c 69 73  .iParm, p->pELis
29c05 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 64  t->nExpr);.    d
29c06 65 73 74 2e 65 44 65 73 74 20 3d 20 53 52 54 5f  est.eDest = SRT_
29c07 54 61 62 6c 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  Table;.  }..  /*
29c08 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 53   Make sure all S
29c09 45 4c 45 43 54 73 20 69 6e 20 74 68 65 20 73 74  ELECTs in the st
29c0a 61 74 65 6d 65 6e 74 20 68 61 76 65 20 74 68 65  atement have the
29c0b 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20   same number of 
29c0c 65 6c 65 6d 65 6e 74 73 0a 20 20 2a 2a 20 69 6e  elements.  ** in
29c0d 20 74 68 65 69 72 20 72 65 73 75 6c 74 20 73 65   their result se
29c0e 74 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ts..  */.  asser
29c0f 74 28 20 70 2d 3e 70 45 4c 69 73 74 20 26 26 20  t( p->pEList && 
29c10 70 50 72 69 6f 72 2d 3e 70 45 4c 69 73 74 20 29  pPrior->pEList )
29c11 3b 0a 20 20 69 66 28 20 70 2d 3e 70 45 4c 69 73  ;.  if( p->pELis
29c12 74 2d 3e 6e 45 78 70 72 21 3d 70 50 72 69 6f 72  t->nExpr!=pPrior
29c13 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20  ->pEList->nExpr 
29c14 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
29c15 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
29c16 53 45 4c 45 43 54 73 20 74 6f 20 74 68 65 20 6c  SELECTs to the l
29c17 65 66 74 20 61 6e 64 20 72 69 67 68 74 20 6f 66  eft and right of
29c18 20 25 73 22 0a 20 20 20 20 20 20 22 20 64 6f 20   %s".      " do 
29c19 6e 6f 74 20 68 61 76 65 20 74 68 65 20 73 61 6d  not have the sam
29c1a 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 75  e number of resu
29c1b 6c 74 20 63 6f 6c 75 6d 6e 73 22 2c 20 73 65 6c  lt columns", sel
29c1c 65 63 74 4f 70 4e 61 6d 65 28 70 2d 3e 6f 70 29  ectOpName(p->op)
29c1d 29 3b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20  );.    rc = 1;. 
29c1e 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65     goto multi_se
29c1f 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 0a 20  lect_end;.  }.. 
29c20 20 2f 2a 20 43 6f 6d 70 6f 75 6e 64 20 53 45 4c   /* Compound SEL
29c21 45 43 54 73 20 74 68 61 74 20 68 61 76 65 20 61  ECTs that have a
29c22 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  n ORDER BY claus
29c23 65 20 61 72 65 20 68 61 6e 64 6c 65 64 20 73 65  e are handled se
29c24 70 61 72 61 74 65 6c 79 2e 0a 20 20 2a 2f 0a 20  parately..  */. 
29c25 20 69 66 28 20 70 2d 3e 70 4f 72 64 65 72 42 79   if( p->pOrderBy
29c26 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d   ){.    return m
29c27 75 6c 74 69 53 65 6c 65 63 74 4f 72 64 65 72 42  ultiSelectOrderB
29c28 79 28 70 50 61 72 73 65 2c 20 70 2c 20 70 44 65  y(pParse, p, pDe
29c29 73 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47  st);.  }..  /* G
29c2a 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
29c2b 20 74 68 65 20 6c 65 66 74 20 61 6e 64 20 72 69   the left and ri
29c2c 67 68 74 20 53 45 4c 45 43 54 20 73 74 61 74 65  ght SELECT state
29c2d 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 73 77  ments..  */.  sw
29c2e 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 20  itch( p->op ){. 
29c2f 20 20 20 63 61 73 65 20 54 4b 5f 41 4c 4c 3a 20     case TK_ALL: 
29c30 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72  {.      int addr
29c31 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65   = 0;.      asse
29c32 72 74 28 20 21 70 50 72 69 6f 72 2d 3e 70 4c 69  rt( !pPrior->pLi
29c33 6d 69 74 20 29 3b 0a 20 20 20 20 20 20 70 50 72  mit );.      pPr
29c34 69 6f 72 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 2d  ior->pLimit = p-
29c35 3e 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70  >pLimit;.      p
29c36 50 72 69 6f 72 2d 3e 70 4f 66 66 73 65 74 20 3d  Prior->pOffset =
29c37 20 70 2d 3e 70 4f 66 66 73 65 74 3b 0a 20 20 20   p->pOffset;.   
29c38 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53     rc = sqlite3S
29c39 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 50  elect(pParse, pP
29c3a 72 69 6f 72 2c 20 26 64 65 73 74 29 3b 0a 20 20  rior, &dest);.  
29c3b 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20      p->pLimit = 
29c3c 30 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66 66  0;.      p->pOff
29c3d 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  set = 0;.      i
29c3e 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20  f( rc ){.       
29c3f 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65   goto multi_sele
29c40 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a  ct_end;.      }.
29c41 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20        p->pPrior 
29c42 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 69 4c  = 0;.      p->iL
29c43 69 6d 69 74 20 3d 20 70 50 72 69 6f 72 2d 3e 69  imit = pPrior->i
29c44 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e  Limit;.      p->
29c45 69 4f 66 66 73 65 74 20 3d 20 70 50 72 69 6f 72  iOffset = pPrior
29c46 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 20  ->iOffset;.     
29c47 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20 29   if( p->iLimit )
29c48 7b 0a 20 20 20 20 20 20 20 20 61 64 64 72 20 3d  {.        addr =
29c49 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29c4a 70 31 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c  p1(v, OP_IfZero,
29c4b 20 70 2d 3e 69 4c 69 6d 69 74 29 3b 0a 20 20 20   p->iLimit);.   
29c4c 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
29c4d 28 28 76 2c 20 22 4a 75 6d 70 20 61 68 65 61 64  ((v, "Jump ahead
29c4e 20 69 66 20 4c 49 4d 49 54 20 72 65 61 63 68 65   if LIMIT reache
29c4f 64 22 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  d"));.      }.  
29c50 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
29c51 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
29c52 2c 20 26 64 65 73 74 29 3b 0a 20 20 20 20 20 20  , &dest);.      
29c53 74 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 51  testcase( rc!=SQ
29c54 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20  LITE_OK );.     
29c55 20 70 44 65 6c 65 74 65 20 3d 20 70 2d 3e 70 50   pDelete = p->pP
29c56 72 69 6f 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70  rior;.      p->p
29c57 50 72 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b 0a  Prior = pPrior;.
29c58 20 20 20 20 20 20 69 66 28 20 61 64 64 72 20 29        if( addr )
29c59 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
29c5a 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
29c5b 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20 7d 0a   addr);.      }.
29c5c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
29c5d 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45   }.    case TK_E
29c5e 58 43 45 50 54 3a 0a 20 20 20 20 63 61 73 65 20  XCEPT:.    case 
29c5f 54 4b 5f 55 4e 49 4f 4e 3a 20 7b 0a 20 20 20 20  TK_UNION: {.    
29c60 20 20 69 6e 74 20 75 6e 69 6f 6e 54 61 62 3b 20    int unionTab; 
29c61 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d     /* Cursor num
29c62 62 65 72 20 6f 66 20 74 68 65 20 74 65 6d 70 6f  ber of the tempo
29c63 72 61 72 79 20 74 61 62 6c 65 20 68 6f 6c 64 69  rary table holdi
29c64 6e 67 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 20  ng result */.   
29c65 20 20 20 75 38 20 6f 70 20 3d 20 30 3b 20 20 20     u8 op = 0;   
29c66 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68      /* One of th
29c67 65 20 53 52 54 5f 20 6f 70 65 72 61 74 69 6f 6e  e SRT_ operation
29c68 73 20 74 6f 20 61 70 70 6c 79 20 74 6f 20 73 65  s to apply to se
29c69 6c 66 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  lf */.      int 
29c6a 70 72 69 6f 72 4f 70 3b 20 20 20 20 20 2f 2a 20  priorOp;     /* 
29c6b 54 68 65 20 53 52 54 5f 20 6f 70 65 72 61 74 69  The SRT_ operati
29c6c 6f 6e 20 74 6f 20 61 70 70 6c 79 20 74 6f 20 70  on to apply to p
29c6d 72 69 6f 72 20 73 65 6c 65 63 74 73 20 2a 2f 0a  rior selects */.
29c6e 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 69 6d        Expr *pLim
29c6f 69 74 2c 20 2a 70 4f 66 66 73 65 74 3b 20 2f 2a  it, *pOffset; /*
29c70 20 53 61 76 65 64 20 76 61 6c 75 65 73 20 6f 66   Saved values of
29c71 20 70 2d 3e 6e 4c 69 6d 69 74 20 61 6e 64 20 70   p->nLimit and p
29c72 2d 3e 6e 4f 66 66 73 65 74 20 2a 2f 0a 20 20 20  ->nOffset */.   
29c73 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20     int addr;.   
29c74 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 75 6e     SelectDest un
29c75 69 6f 6e 64 65 73 74 3b 0a 0a 20 20 20 20 20 20  iondest;..      
29c76 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d  testcase( p->op=
29c77 3d 54 4b 5f 45 58 43 45 50 54 20 29 3b 0a 20 20  =TK_EXCEPT );.  
29c78 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d      testcase( p-
29c79 3e 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e 20 29 3b  >op==TK_UNION );
29c7a 0a 20 20 20 20 20 20 70 72 69 6f 72 4f 70 20 3d  .      priorOp =
29c7b 20 53 52 54 5f 55 6e 69 6f 6e 3b 0a 20 20 20 20   SRT_Union;.    
29c7c 20 20 69 66 28 20 64 65 73 74 2e 65 44 65 73 74    if( dest.eDest
29c7d 3d 3d 70 72 69 6f 72 4f 70 20 26 26 20 41 4c 57  ==priorOp && ALW
29c7e 41 59 53 28 21 70 2d 3e 70 4c 69 6d 69 74 20 26  AYS(!p->pLimit &
29c7f 26 21 70 2d 3e 70 4f 66 66 73 65 74 29 20 29 7b  &!p->pOffset) ){
29c80 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 63  .        /* We c
29c81 61 6e 20 72 65 75 73 65 20 61 20 74 65 6d 70 6f  an reuse a tempo
29c82 72 61 72 79 20 74 61 62 6c 65 20 67 65 6e 65 72  rary table gener
29c83 61 74 65 64 20 62 79 20 61 20 53 45 4c 45 43 54  ated by a SELECT
29c84 20 74 6f 20 6f 75 72 0a 20 20 20 20 20 20 20 20   to our.        
29c85 2a 2a 20 72 69 67 68 74 2e 0a 20 20 20 20 20 20  ** right..      
29c86 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73    */.        ass
29c87 65 72 74 28 20 70 2d 3e 70 52 69 67 68 74 6d 6f  ert( p->pRightmo
29c88 73 74 21 3d 70 20 29 3b 20 20 2f 2a 20 43 61 6e  st!=p );  /* Can
29c89 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 66 6f 72   only happen for
29c8a 20 6c 65 66 74 77 61 72 64 20 65 6c 65 6d 65 6e   leftward elemen
29c8b 74 73 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ts.             
29c8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29c8d 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 61 20          ** of a 
29c8e 33 2d 77 61 79 20 6f 72 20 6d 6f 72 65 20 63 6f  3-way or more co
29c8f 6d 70 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 20 20  mpound */.      
29c90 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4c 69    assert( p->pLi
29c91 6d 69 74 3d 3d 30 20 29 3b 20 20 20 20 20 20 2f  mit==0 );      /
29c92 2a 20 4e 6f 74 20 61 6c 6c 6f 77 65 64 20 6f 6e  * Not allowed on
29c93 20 6c 65 66 74 77 61 72 64 20 65 6c 65 6d 65 6e   leftward elemen
29c94 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73  ts */.        as
29c95 73 65 72 74 28 20 70 2d 3e 70 4f 66 66 73 65 74  sert( p->pOffset
29c96 3d 3d 30 20 29 3b 20 20 20 20 20 2f 2a 20 4e 6f  ==0 );     /* No
29c97 74 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 6c 65 66  t allowed on lef
29c98 74 77 61 72 64 20 65 6c 65 6d 65 6e 74 73 20 2a  tward elements *
29c99 2f 0a 20 20 20 20 20 20 20 20 75 6e 69 6f 6e 54  /.        unionT
29c9a 61 62 20 3d 20 64 65 73 74 2e 69 50 61 72 6d 3b  ab = dest.iParm;
29c9b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
29c9c 20 20 20 20 20 20 2f 2a 20 57 65 20 77 69 6c 6c        /* We will
29c9d 20 6e 65 65 64 20 74 6f 20 63 72 65 61 74 65 20   need to create 
29c9e 6f 75 72 20 6f 77 6e 20 74 65 6d 70 6f 72 61 72  our own temporar
29c9f 79 20 74 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20  y table to hold 
29ca0 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  the.        ** i
29ca1 6e 74 65 72 6d 65 64 69 61 74 65 20 72 65 73 75  ntermediate resu
29ca2 6c 74 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  lts..        */.
29ca3 20 20 20 20 20 20 20 20 75 6e 69 6f 6e 54 61 62          unionTab
29ca4 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
29ca5 2b 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  +;.        asser
29ca6 74 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 3d 3d  t( p->pOrderBy==
29ca7 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 64 64  0 );.        add
29ca8 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
29ca9 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp2(v, OP_Open
29caa 45 70 68 65 6d 65 72 61 6c 2c 20 75 6e 69 6f 6e  Ephemeral, union
29cab 54 61 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  Tab, 0);.       
29cac 20 61 73 73 65 72 74 28 20 70 2d 3e 61 64 64 72   assert( p->addr
29cad 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 3d 20 2d  OpenEphm[0] == -
29cae 31 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  1 );.        p->
29caf 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20  addrOpenEphm[0] 
29cb0 3d 20 61 64 64 72 3b 0a 20 20 20 20 20 20 20 20  = addr;.        
29cb1 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 2d 3e 73  p->pRightmost->s
29cb2 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 55 73  elFlags |= SF_Us
29cb3 65 73 45 70 68 65 6d 65 72 61 6c 3b 0a 20 20 20  esEphemeral;.   
29cb4 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
29cb5 70 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 20 20  pEList );.      
29cb6 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64 65  }..      /* Code
29cb7 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
29cb8 65 6d 65 6e 74 73 20 74 6f 20 6f 75 72 20 6c 65  ements to our le
29cb9 66 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  ft.      */.    
29cba 20 20 61 73 73 65 72 74 28 20 21 70 50 72 69 6f    assert( !pPrio
29cbb 72 2d 3e 70 4f 72 64 65 72 42 79 20 29 3b 0a 20  r->pOrderBy );. 
29cbc 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65       sqlite3Sele
29cbd 63 74 44 65 73 74 49 6e 69 74 28 26 75 6e 69 6f  ctDestInit(&unio
29cbe 6e 64 65 73 74 2c 20 70 72 69 6f 72 4f 70 2c 20  ndest, priorOp, 
29cbf 75 6e 69 6f 6e 54 61 62 29 3b 0a 20 20 20 20 20  unionTab);.     
29cc0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 65 6c   rc = sqlite3Sel
29cc1 65 63 74 28 70 50 61 72 73 65 2c 20 70 50 72 69  ect(pParse, pPri
29cc2 6f 72 2c 20 26 75 6e 69 6f 6e 64 65 73 74 29 3b  or, &uniondest);
29cc3 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b  .      if( rc ){
29cc4 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75  .        goto mu
29cc5 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  lti_select_end;.
29cc6 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
29cc7 2a 20 43 6f 64 65 20 74 68 65 20 63 75 72 72 65  * Code the curre
29cc8 6e 74 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  nt SELECT statem
29cc9 65 6e 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ent.      */.   
29cca 20 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 54 4b     if( p->op==TK
29ccb 5f 45 58 43 45 50 54 20 29 7b 0a 20 20 20 20 20  _EXCEPT ){.     
29ccc 20 20 20 6f 70 20 3d 20 53 52 54 5f 45 78 63 65     op = SRT_Exce
29ccd 70 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  pt;.      }else{
29cce 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
29ccf 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e   p->op==TK_UNION
29cd0 20 29 3b 0a 20 20 20 20 20 20 20 20 6f 70 20 3d   );.        op =
29cd1 20 53 52 54 5f 55 6e 69 6f 6e 3b 0a 20 20 20 20   SRT_Union;.    
29cd2 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 70 50 72    }.      p->pPr
29cd3 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  ior = 0;.      p
29cd4 4c 69 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69  Limit = p->pLimi
29cd5 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d  t;.      p->pLim
29cd6 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f  it = 0;.      pO
29cd7 66 66 73 65 74 20 3d 20 70 2d 3e 70 4f 66 66 73  ffset = p->pOffs
29cd8 65 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66  et;.      p->pOf
29cd9 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  fset = 0;.      
29cda 75 6e 69 6f 6e 64 65 73 74 2e 65 44 65 73 74 20  uniondest.eDest 
29cdb 3d 20 6f 70 3b 0a 20 20 20 20 20 20 72 63 20 3d  = op;.      rc =
29cdc 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70   sqlite3Select(p
29cdd 50 61 72 73 65 2c 20 70 2c 20 26 75 6e 69 6f 6e  Parse, p, &union
29cde 64 65 73 74 29 3b 0a 20 20 20 20 20 20 74 65 73  dest);.      tes
29cdf 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54  tcase( rc!=SQLIT
29ce0 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 2f 2a  E_OK );.      /*
29ce1 20 51 75 65 72 79 20 66 6c 61 74 74 65 6e 69 6e   Query flattenin
29ce2 67 20 69 6e 20 73 71 6c 69 74 65 33 53 65 6c 65  g in sqlite3Sele
29ce3 63 74 28 29 20 6d 69 67 68 74 20 72 65 66 69 6c  ct() might refil
29ce4 6c 20 70 2d 3e 70 4f 72 64 65 72 42 79 2e 0a 20  l p->pOrderBy.. 
29ce5 20 20 20 20 20 2a 2a 20 42 65 20 73 75 72 65 20       ** Be sure 
29ce6 74 6f 20 64 65 6c 65 74 65 20 70 2d 3e 70 4f 72  to delete p->pOr
29ce7 64 65 72 42 79 2c 20 74 68 65 72 65 66 6f 72 65  derBy, therefore
29ce8 2c 20 74 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d  , to avoid a mem
29ce9 6f 72 79 20 6c 65 61 6b 2e 20 2a 2f 0a 20 20 20  ory leak. */.   
29cea 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
29ceb 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e  stDelete(db, p->
29cec 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 20 20 20  pOrderBy);.     
29ced 20 70 44 65 6c 65 74 65 20 3d 20 70 2d 3e 70 50   pDelete = p->pP
29cee 72 69 6f 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70  rior;.      p->p
29cef 50 72 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b 0a  Prior = pPrior;.
29cf0 20 20 20 20 20 20 70 2d 3e 70 4f 72 64 65 72 42        p->pOrderB
29cf1 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c  y = 0;.      sql
29cf2 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
29cf3 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20  b, p->pLimit);. 
29cf4 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d       p->pLimit =
29cf5 20 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70   pLimit;.      p
29cf6 2d 3e 70 4f 66 66 73 65 74 20 3d 20 70 4f 66 66  ->pOffset = pOff
29cf7 73 65 74 3b 0a 20 20 20 20 20 20 70 2d 3e 69 4c  set;.      p->iL
29cf8 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  imit = 0;.      
29cf9 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a  p->iOffset = 0;.
29cfa 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72  .      /* Conver
29cfb 74 20 74 68 65 20 64 61 74 61 20 69 6e 20 74 68  t the data in th
29cfc 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  e temporary tabl
29cfd 65 20 69 6e 74 6f 20 77 68 61 74 65 76 65 72 20  e into whatever 
29cfe 66 6f 72 6d 0a 20 20 20 20 20 20 2a 2a 20 69 74  form.      ** it
29cff 20 69 73 20 74 68 61 74 20 77 65 20 63 75 72 72   is that we curr
29d00 65 6e 74 6c 79 20 6e 65 65 64 2e 0a 20 20 20 20  ently need..    
29d01 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72    */.      asser
29d02 74 28 20 75 6e 69 6f 6e 54 61 62 3d 3d 64 65 73  t( unionTab==des
29d03 74 2e 69 50 61 72 6d 20 7c 7c 20 64 65 73 74 2e  t.iParm || dest.
29d04 65 44 65 73 74 21 3d 70 72 69 6f 72 4f 70 20 29  eDest!=priorOp )
29d05 3b 0a 20 20 20 20 20 20 69 66 28 20 64 65 73 74  ;.      if( dest
29d06 2e 65 44 65 73 74 21 3d 70 72 69 6f 72 4f 70 20  .eDest!=priorOp 
29d07 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  ){.        int i
29d08 43 6f 6e 74 2c 20 69 42 72 65 61 6b 2c 20 69 53  Cont, iBreak, iS
29d09 74 61 72 74 3b 0a 20 20 20 20 20 20 20 20 61 73  tart;.        as
29d0a 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20  sert( p->pEList 
29d0b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 64  );.        if( d
29d0c 65 73 74 2e 65 44 65 73 74 3d 3d 53 52 54 5f 4f  est.eDest==SRT_O
29d0d 75 74 70 75 74 20 29 7b 0a 20 20 20 20 20 20 20  utput ){.       
29d0e 20 20 20 53 65 6c 65 63 74 20 2a 70 46 69 72 73     Select *pFirs
29d0f 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 20  t = p;.         
29d10 20 77 68 69 6c 65 28 20 70 46 69 72 73 74 2d 3e   while( pFirst->
29d11 70 50 72 69 6f 72 20 29 20 70 46 69 72 73 74 20  pPrior ) pFirst 
29d12 3d 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72  = pFirst->pPrior
29d13 3b 0a 20 20 20 20 20 20 20 20 20 20 67 65 6e 65  ;.          gene
29d14 72 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  rateColumnNames(
29d15 70 50 61 72 73 65 2c 20 30 2c 20 70 46 69 72 73  pParse, 0, pFirs
29d16 74 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  t->pEList);.    
29d17 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 42      }.        iB
29d18 72 65 61 6b 20 3d 20 73 71 6c 69 74 65 33 56 64  reak = sqlite3Vd
29d19 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
29d1a 20 20 20 20 20 20 20 20 69 43 6f 6e 74 20 3d 20          iCont = 
29d1b 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
29d1c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20  abel(v);.       
29d1d 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65 67   computeLimitReg
29d1e 69 73 74 65 72 73 28 70 50 61 72 73 65 2c 20 70  isters(pParse, p
29d1f 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20  , iBreak);.     
29d20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29d21 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp2(v, OP_Rewin
29d22 64 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 69 42 72  d, unionTab, iBr
29d23 65 61 6b 29 3b 0a 20 20 20 20 20 20 20 20 69 53  eak);.        iS
29d24 74 61 72 74 20 3d 20 73 71 6c 69 74 65 33 56 64  tart = sqlite3Vd
29d25 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
29d26 3b 0a 20 20 20 20 20 20 20 20 73 65 6c 65 63 74  ;.        select
29d27 49 6e 6e 65 72 4c 6f 6f 70 28 70 50 61 72 73 65  InnerLoop(pParse
29d28 2c 20 70 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20  , p, p->pEList, 
29d29 75 6e 69 6f 6e 54 61 62 2c 20 70 2d 3e 70 45 4c  unionTab, p->pEL
29d2a 69 73 74 2d 3e 6e 45 78 70 72 2c 0a 20 20 20 20  ist->nExpr,.    
29d2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29d2c 20 20 20 20 30 2c 20 2d 31 2c 20 26 64 65 73 74      0, -1, &dest
29d2d 2c 20 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b 29  , iCont, iBreak)
29d2e 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
29d2f 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
29d30 6c 28 76 2c 20 69 43 6f 6e 74 29 3b 0a 20 20 20  l(v, iCont);.   
29d31 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29d32 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78  AddOp2(v, OP_Nex
29d33 74 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 69 53 74  t, unionTab, iSt
29d34 61 72 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71  art);.        sq
29d35 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
29d36 4c 61 62 65 6c 28 76 2c 20 69 42 72 65 61 6b 29  Label(v, iBreak)
29d37 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
29d38 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
29d39 50 5f 43 6c 6f 73 65 2c 20 75 6e 69 6f 6e 54 61  P_Close, unionTa
29d3a 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20  b, 0);.      }. 
29d3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
29d3c 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 61  }.    default: a
29d3d 73 73 65 72 74 28 20 70 2d 3e 6f 70 3d 3d 54 4b  ssert( p->op==TK
29d3e 5f 49 4e 54 45 52 53 45 43 54 20 29 3b 20 7b 0a  _INTERSECT ); {.
29d3f 20 20 20 20 20 20 69 6e 74 20 74 61 62 31 2c 20        int tab1, 
29d40 74 61 62 32 3b 0a 20 20 20 20 20 20 69 6e 74 20  tab2;.      int 
29d41 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b 2c 20 69  iCont, iBreak, i
29d42 53 74 61 72 74 3b 0a 20 20 20 20 20 20 45 78 70  Start;.      Exp
29d43 72 20 2a 70 4c 69 6d 69 74 2c 20 2a 70 4f 66 66  r *pLimit, *pOff
29d44 73 65 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 61  set;.      int a
29d45 64 64 72 3b 0a 20 20 20 20 20 20 53 65 6c 65 63  ddr;.      Selec
29d46 74 44 65 73 74 20 69 6e 74 65 72 73 65 63 74 64  tDest intersectd
29d47 65 73 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 72  est;.      int r
29d48 31 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 4e 54  1;..      /* INT
29d49 45 52 53 45 43 54 20 69 73 20 64 69 66 66 65 72  ERSECT is differ
29d4a 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 6f 74 68  ent from the oth
29d4b 65 72 73 20 73 69 6e 63 65 20 69 74 20 72 65 71  ers since it req
29d4c 75 69 72 65 73 0a 20 20 20 20 20 20 2a 2a 20 74  uires.      ** t
29d4d 77 6f 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  wo temporary tab
29d4e 6c 65 73 2e 20 20 48 65 6e 63 65 20 69 74 20 68  les.  Hence it h
29d4f 61 73 20 69 74 73 20 6f 77 6e 20 63 61 73 65 2e  as its own case.
29d50 20 20 42 65 67 69 6e 0a 20 20 20 20 20 20 2a 2a    Begin.      **
29d51 20 62 79 20 61 6c 6c 6f 63 61 74 69 6e 67 20 74   by allocating t
29d52 68 65 20 74 61 62 6c 65 73 20 77 65 20 77 69 6c  he tables we wil
29d53 6c 20 6e 65 65 64 2e 0a 20 20 20 20 20 20 2a 2f  l need..      */
29d54 0a 20 20 20 20 20 20 74 61 62 31 20 3d 20 70 50  .      tab1 = pP
29d55 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
29d56 20 20 20 20 74 61 62 32 20 3d 20 70 50 61 72 73      tab2 = pPars
29d57 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20  e->nTab++;.     
29d58 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4f 72 64   assert( p->pOrd
29d59 65 72 42 79 3d 3d 30 20 29 3b 0a 0a 20 20 20 20  erBy==0 );..    
29d5a 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
29d5b 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
29d5c 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20  _OpenEphemeral, 
29d5d 74 61 62 31 2c 20 30 29 3b 0a 20 20 20 20 20 20  tab1, 0);.      
29d5e 61 73 73 65 72 74 28 20 70 2d 3e 61 64 64 72 4f  assert( p->addrO
29d5f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 3d 20 2d 31  penEphm[0] == -1
29d60 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 64 64   );.      p->add
29d61 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 61  rOpenEphm[0] = a
29d62 64 64 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70 52  ddr;.      p->pR
29d63 69 67 68 74 6d 6f 73 74 2d 3e 73 65 6c 46 6c 61  ightmost->selFla
29d64 67 73 20 7c 3d 20 53 46 5f 55 73 65 73 45 70 68  gs |= SF_UsesEph
29d65 65 6d 65 72 61 6c 3b 0a 20 20 20 20 20 20 61 73  emeral;.      as
29d66 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20  sert( p->pEList 
29d67 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64  );..      /* Cod
29d68 65 20 74 68 65 20 53 45 4c 45 43 54 73 20 74 6f  e the SELECTs to
29d69 20 6f 75 72 20 6c 65 66 74 20 69 6e 74 6f 20 74   our left into t
29d6a 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 22  emporary table "
29d6b 74 61 62 31 22 2e 0a 20 20 20 20 20 20 2a 2f 0a  tab1"..      */.
29d6c 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c        sqlite3Sel
29d6d 65 63 74 44 65 73 74 49 6e 69 74 28 26 69 6e 74  ectDestInit(&int
29d6e 65 72 73 65 63 74 64 65 73 74 2c 20 53 52 54 5f  ersectdest, SRT_
29d6f 55 6e 69 6f 6e 2c 20 74 61 62 31 29 3b 0a 20 20  Union, tab1);.  
29d70 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
29d71 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
29d72 50 72 69 6f 72 2c 20 26 69 6e 74 65 72 73 65 63  Prior, &intersec
29d73 74 64 65 73 74 29 3b 0a 20 20 20 20 20 20 69 66  tdest);.      if
29d74 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20  ( rc ){.        
29d75 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63  goto multi_selec
29d76 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 0a  t_end;.      }..
29d77 20 20 20 20 20 20 2f 2a 20 43 6f 64 65 20 74 68        /* Code th
29d78 65 20 63 75 72 72 65 6e 74 20 53 45 4c 45 43 54  e current SELECT
29d79 20 69 6e 74 6f 20 74 65 6d 70 6f 72 61 72 79 20   into temporary 
29d7a 74 61 62 6c 65 20 22 74 61 62 32 22 0a 20 20 20  table "tab2".   
29d7b 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 64 64 72     */.      addr
29d7c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
29d7d 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45  dOp2(v, OP_OpenE
29d7e 70 68 65 6d 65 72 61 6c 2c 20 74 61 62 32 2c 20  phemeral, tab2, 
29d7f 30 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  0);.      assert
29d80 28 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68  ( p->addrOpenEph
29d81 6d 5b 31 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20  m[1] == -1 );.  
29d82 20 20 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45      p->addrOpenE
29d83 70 68 6d 5b 31 5d 20 3d 20 61 64 64 72 3b 0a 20  phm[1] = addr;. 
29d84 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d       p->pPrior =
29d85 20 30 3b 0a 20 20 20 20 20 20 70 4c 69 6d 69 74   0;.      pLimit
29d86 20 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20   = p->pLimit;.  
29d87 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20      p->pLimit = 
29d88 30 3b 0a 20 20 20 20 20 20 70 4f 66 66 73 65 74  0;.      pOffset
29d89 20 3d 20 70 2d 3e 70 4f 66 66 73 65 74 3b 0a 20   = p->pOffset;. 
29d8a 20 20 20 20 20 70 2d 3e 70 4f 66 66 73 65 74 20       p->pOffset 
29d8b 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 74 65 72  = 0;.      inter
29d8c 73 65 63 74 64 65 73 74 2e 69 50 61 72 6d 20 3d  sectdest.iParm =
29d8d 20 74 61 62 32 3b 0a 20 20 20 20 20 20 72 63 20   tab2;.      rc 
29d8e 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28  = sqlite3Select(
29d8f 70 50 61 72 73 65 2c 20 70 2c 20 26 69 6e 74 65  pParse, p, &inte
29d90 72 73 65 63 74 64 65 73 74 29 3b 0a 20 20 20 20  rsectdest);.    
29d91 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d    testcase( rc!=
29d92 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20  SQLITE_OK );.   
29d93 20 20 20 70 44 65 6c 65 74 65 20 3d 20 70 2d 3e     pDelete = p->
29d94 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 70 2d  pPrior;.      p-
29d95 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69 6f 72  >pPrior = pPrior
29d96 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
29d97 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d  xprDelete(db, p-
29d98 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20  >pLimit);.      
29d99 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 4c 69 6d  p->pLimit = pLim
29d9a 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66  it;.      p->pOf
29d9b 66 73 65 74 20 3d 20 70 4f 66 66 73 65 74 3b 0a  fset = pOffset;.
29d9c 0a 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61  .      /* Genera
29d9d 74 65 20 63 6f 64 65 20 74 6f 20 74 61 6b 65 20  te code to take 
29d9e 74 68 65 20 69 6e 74 65 72 73 65 63 74 69 6f 6e  the intersection
29d9f 20 6f 66 20 74 68 65 20 74 77 6f 20 74 65 6d 70   of the two temp
29da0 6f 72 61 72 79 0a 20 20 20 20 20 20 2a 2a 20 74  orary.      ** t
29da1 61 62 6c 65 73 2e 0a 20 20 20 20 20 20 2a 2f 0a  ables..      */.
29da2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
29da3 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 20  >pEList );.     
29da4 20 69 66 28 20 64 65 73 74 2e 65 44 65 73 74 3d   if( dest.eDest=
29da5 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a 20  =SRT_Output ){. 
29da6 20 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70         Select *p
29da7 46 69 72 73 74 20 3d 20 70 3b 0a 20 20 20 20 20  First = p;.     
29da8 20 20 20 77 68 69 6c 65 28 20 70 46 69 72 73 74     while( pFirst
29da9 2d 3e 70 50 72 69 6f 72 20 29 20 70 46 69 72 73  ->pPrior ) pFirs
29daa 74 20 3d 20 70 46 69 72 73 74 2d 3e 70 50 72 69  t = pFirst->pPri
29dab 6f 72 3b 0a 20 20 20 20 20 20 20 20 67 65 6e 65  or;.        gene
29dac 72 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  rateColumnNames(
29dad 70 50 61 72 73 65 2c 20 30 2c 20 70 46 69 72 73  pParse, 0, pFirs
29dae 74 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  t->pEList);.    
29daf 20 20 7d 0a 20 20 20 20 20 20 69 42 72 65 61 6b    }.      iBreak
29db0 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
29db1 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20  keLabel(v);.    
29db2 20 20 69 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65    iCont = sqlite
29db3 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
29db4 29 3b 0a 20 20 20 20 20 20 63 6f 6d 70 75 74 65  );.      compute
29db5 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73 28 70  LimitRegisters(p
29db6 50 61 72 73 65 2c 20 70 2c 20 69 42 72 65 61 6b  Parse, p, iBreak
29db7 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29db8 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
29db9 5f 52 65 77 69 6e 64 2c 20 74 61 62 31 2c 20 69  _Rewind, tab1, i
29dba 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 72 31  Break);.      r1
29dbb 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
29dbc 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
29dbd 20 20 20 20 69 53 74 61 72 74 20 3d 20 73 71 6c      iStart = sql
29dbe 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
29dbf 2c 20 4f 50 5f 52 6f 77 4b 65 79 2c 20 74 61 62  , OP_RowKey, tab
29dc0 31 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71  1, r1);.      sq
29dc1 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
29dc2 76 2c 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 2c 20  v, OP_NotFound, 
29dc3 74 61 62 32 2c 20 69 43 6f 6e 74 2c 20 72 31 29  tab2, iCont, r1)
29dc4 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  ;.      sqlite3R
29dc5 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
29dc6 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20  arse, r1);.     
29dc7 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70   selectInnerLoop
29dc8 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70  (pParse, p, p->p
29dc9 45 4c 69 73 74 2c 20 74 61 62 31 2c 20 70 2d 3e  EList, tab1, p->
29dca 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 0a 20  pEList->nExpr,. 
29dcb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29dcc 20 20 20 20 20 30 2c 20 2d 31 2c 20 26 64 65 73       0, -1, &des
29dcd 74 2c 20 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b  t, iCont, iBreak
29dce 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29dcf 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
29dd0 28 76 2c 20 69 43 6f 6e 74 29 3b 0a 20 20 20 20  (v, iCont);.    
29dd1 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29dd2 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  Op2(v, OP_Next, 
29dd3 74 61 62 31 2c 20 69 53 74 61 72 74 29 3b 0a 20  tab1, iStart);. 
29dd4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29dd5 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
29dd6 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 73  iBreak);.      s
29dd7 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
29dd8 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 74 61  (v, OP_Close, ta
29dd9 62 32 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71  b2, 0);.      sq
29dda 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
29ddb 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 74 61 62  v, OP_Close, tab
29ddc 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 72 65  1, 0);.      bre
29ddd 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ak;.    }.  }.. 
29dde 20 2f 2a 20 43 6f 6d 70 75 74 65 20 63 6f 6c 6c   /* Compute coll
29ddf 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20  ating sequences 
29de0 75 73 65 64 20 62 79 20 0a 20 20 2a 2a 20 74 65  used by .  ** te
29de1 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 20 6e  mporary tables n
29de2 65 65 64 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65  eeded to impleme
29de3 6e 74 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20  nt the compound 
29de4 73 65 6c 65 63 74 2e 0a 20 20 2a 2a 20 41 74 74  select..  ** Att
29de5 61 63 68 20 74 68 65 20 4b 65 79 49 6e 66 6f 20  ach the KeyInfo 
29de6 73 74 72 75 63 74 75 72 65 20 74 6f 20 61 6c 6c  structure to all
29de7 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
29de8 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69  s..  **.  ** Thi
29de9 73 20 73 65 63 74 69 6f 6e 20 69 73 20 72 75 6e  s section is run
29dea 20 62 79 20 74 68 65 20 72 69 67 68 74 2d 6d 6f   by the right-mo
29deb 73 74 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  st SELECT statem
29dec 65 6e 74 20 6f 6e 6c 79 2e 0a 20 20 2a 2a 20 53  ent only..  ** S
29ded 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73  ELECT statements
29dee 20 74 6f 20 74 68 65 20 6c 65 66 74 20 61 6c 77   to the left alw
29def 61 79 73 20 73 6b 69 70 20 74 68 69 73 20 70 61  ays skip this pa
29df0 72 74 2e 20 20 54 68 65 20 72 69 67 68 74 2d 6d  rt.  The right-m
29df1 6f 73 74 0a 20 20 2a 2a 20 53 45 4c 45 43 54 20  ost.  ** SELECT 
29df2 6d 69 67 68 74 20 61 6c 73 6f 20 73 6b 69 70 20  might also skip 
29df3 74 68 69 73 20 70 61 72 74 20 69 66 20 69 74 20  this part if it 
29df4 68 61 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 20  has no ORDER BY 
29df5 63 6c 61 75 73 65 20 61 6e 64 0a 20 20 2a 2a 20  clause and.  ** 
29df6 6e 6f 20 74 65 6d 70 20 74 61 62 6c 65 73 20 61  no temp tables a
29df7 72 65 20 72 65 71 75 69 72 65 64 2e 0a 20 20 2a  re required..  *
29df8 2f 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c  /.  if( p->selFl
29df9 61 67 73 20 26 20 53 46 5f 55 73 65 73 45 70 68  ags & SF_UsesEph
29dfa 65 6d 65 72 61 6c 20 29 7b 0a 20 20 20 20 69 6e  emeral ){.    in
29dfb 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
29dfc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
29dfd 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
29dfe 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79     KeyInfo *pKey
29dff 49 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20  Info;           
29e00 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65   /* Collating se
29e01 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 72  quence for the r
29e02 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 20  esult set */.   
29e03 20 53 65 6c 65 63 74 20 2a 70 4c 6f 6f 70 3b 20   Select *pLoop; 
29e04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
29e05 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68  * For looping th
29e06 72 6f 75 67 68 20 53 45 4c 45 43 54 20 73 74 61  rough SELECT sta
29e07 74 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 43  tements */.    C
29e08 6f 6c 6c 53 65 71 20 2a 2a 61 70 43 6f 6c 6c 3b  ollSeq **apColl;
29e09 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
29e0a 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f  For looping thro
29e0b 75 67 68 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43  ugh pKeyInfo->aC
29e0c 6f 6c 6c 5b 5d 20 2a 2f 0a 20 20 20 20 69 6e 74  oll[] */.    int
29e0d 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20   nCol;          
29e0e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
29e0f 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
29e10 69 6e 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f  in result set */
29e11 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  ..    assert( p-
29e12 3e 70 52 69 67 68 74 6d 6f 73 74 3d 3d 70 20 29  >pRightmost==p )
29e13 3b 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 70 2d 3e  ;.    nCol = p->
29e14 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20  pEList->nExpr;. 
29e15 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 73 71     pKeyInfo = sq
29e16 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
29e17 6f 28 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20  o(db,.          
29e18 20 20 20 20 20 20 20 20 20 20 20 20 20 73 69 7a               siz
29e19 65 6f 66 28 2a 70 4b 65 79 49 6e 66 6f 29 2b 6e  eof(*pKeyInfo)+n
29e1a 43 6f 6c 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c  Col*(sizeof(Coll
29e1b 53 65 71 2a 29 20 2b 20 31 29 29 3b 0a 20 20 20  Seq*) + 1));.   
29e1c 20 69 66 28 20 21 70 4b 65 79 49 6e 66 6f 20 29   if( !pKeyInfo )
29e1d 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
29e1e 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
29e1f 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65   goto multi_sele
29e20 63 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a 0a 20  ct_end;.    }.. 
29e21 20 20 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63     pKeyInfo->enc
29e22 20 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 20 20   = ENC(db);.    
29e23 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64  pKeyInfo->nField
29e24 20 3d 20 28 75 31 36 29 6e 43 6f 6c 3b 0a 0a 20   = (u16)nCol;.. 
29e25 20 20 20 66 6f 72 28 69 3d 30 2c 20 61 70 43 6f     for(i=0, apCo
29e26 6c 6c 3d 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f  ll=pKeyInfo->aCo
29e27 6c 6c 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 2c  ll; i<nCol; i++,
29e28 20 61 70 43 6f 6c 6c 2b 2b 29 7b 0a 20 20 20 20   apColl++){.    
29e29 20 20 2a 61 70 43 6f 6c 6c 20 3d 20 6d 75 6c 74    *apColl = mult
29e2a 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28 70  iSelectCollSeq(p
29e2b 50 61 72 73 65 2c 20 70 2c 20 69 29 3b 0a 20 20  Parse, p, i);.  
29e2c 20 20 20 20 69 66 28 20 30 3d 3d 2a 61 70 43 6f      if( 0==*apCo
29e2d 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 61  ll ){.        *a
29e2e 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c  pColl = db->pDfl
29e2f 74 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20  tColl;.      }. 
29e30 20 20 20 7d 0a 0a 20 20 20 20 66 6f 72 28 70 4c     }..    for(pL
29e31 6f 6f 70 3d 70 3b 20 70 4c 6f 6f 70 3b 20 70 4c  oop=p; pLoop; pL
29e32 6f 6f 70 3d 70 4c 6f 6f 70 2d 3e 70 50 72 69 6f  oop=pLoop->pPrio
29e33 72 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  r){.      for(i=
29e34 30 3b 20 69 3c 32 3b 20 69 2b 2b 29 7b 0a 20 20  0; i<2; i++){.  
29e35 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 20 3d        int addr =
29e36 20 70 4c 6f 6f 70 2d 3e 61 64 64 72 4f 70 65 6e   pLoop->addrOpen
29e37 45 70 68 6d 5b 69 5d 3b 0a 20 20 20 20 20 20 20  Ephm[i];.       
29e38 20 69 66 28 20 61 64 64 72 3c 30 20 29 7b 0a 20   if( addr<0 ){. 
29e39 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 5b           /* If [
29e3a 30 5d 20 69 73 20 75 6e 75 73 65 64 20 74 68 65  0] is unused the
29e3b 6e 20 5b 31 5d 20 69 73 20 61 6c 73 6f 20 75 6e  n [1] is also un
29e3c 75 73 65 64 2e 20 20 53 6f 20 77 65 20 63 61 6e  used.  So we can
29e3d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 6c  .          ** al
29e3e 77 61 79 73 20 73 61 66 65 6c 79 20 61 62 6f 72  ways safely abor
29e3f 74 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 65  t as soon as the
29e40 20 66 69 72 73 74 20 75 6e 75 73 65 64 20 73 6c   first unused sl
29e41 6f 74 20 69 73 20 66 6f 75 6e 64 20 2a 2f 0a 20  ot is found */. 
29e42 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
29e43 20 70 4c 6f 6f 70 2d 3e 61 64 64 72 4f 70 65 6e   pLoop->addrOpen
29e44 45 70 68 6d 5b 31 5d 3c 30 20 29 3b 0a 20 20 20  Ephm[1]<0 );.   
29e45 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
29e46 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
29e47 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
29e48 65 50 32 28 76 2c 20 61 64 64 72 2c 20 6e 43 6f  eP2(v, addr, nCo
29e49 6c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  l);.        sqli
29e4a 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
29e4b 76 2c 20 61 64 64 72 2c 20 28 63 68 61 72 2a 29  v, addr, (char*)
29e4c 70 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59  pKeyInfo, P4_KEY
29e4d 49 4e 46 4f 29 3b 0a 20 20 20 20 20 20 20 20 70  INFO);.        p
29e4e 4c 6f 6f 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70  Loop->addrOpenEp
29e4f 68 6d 5b 69 5d 20 3d 20 2d 31 3b 0a 20 20 20 20  hm[i] = -1;.    
29e50 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71    }.    }.    sq
29e51 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
29e52 70 4b 65 79 49 6e 66 6f 29 3b 0a 20 20 7d 0a 0a  pKeyInfo);.  }..
29e53 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64  multi_select_end
29e54 3a 0a 20 20 70 44 65 73 74 2d 3e 69 4d 65 6d 20  :.  pDest->iMem 
29e55 3d 20 64 65 73 74 2e 69 4d 65 6d 3b 0a 20 20 70  = dest.iMem;.  p
29e56 44 65 73 74 2d 3e 6e 4d 65 6d 20 3d 20 64 65 73  Dest->nMem = des
29e57 74 2e 6e 4d 65 6d 3b 0a 20 20 73 71 6c 69 74 65  t.nMem;.  sqlite
29e58 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
29e59 2c 20 70 44 65 6c 65 74 65 29 3b 0a 20 20 72 65  , pDelete);.  re
29e5a 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69  turn rc;.}.#endi
29e5b 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
29e5c 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
29e5d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20   */../*.** Code 
29e5e 61 6e 20 6f 75 74 70 75 74 20 73 75 62 72 6f 75  an output subrou
29e5f 74 69 6e 65 20 66 6f 72 20 61 20 63 6f 72 6f 75  tine for a corou
29e60 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  tine implementat
29e61 69 6f 6e 20 6f 66 20 61 0a 2a 2a 20 53 45 4c 45  ion of a.** SELE
29e62 43 54 20 73 74 61 74 6d 65 6e 74 2e 0a 2a 2a 0a  CT statment..**.
29e63 2a 2a 20 54 68 65 20 64 61 74 61 20 74 6f 20 62  ** The data to b
29e64 65 20 6f 75 74 70 75 74 20 69 73 20 63 6f 6e 74  e output is cont
29e65 61 69 6e 65 64 20 69 6e 20 70 49 6e 2d 3e 69 4d  ained in pIn->iM
29e66 65 6d 2e 20 20 54 68 65 72 65 20 61 72 65 0a 2a  em.  There are.*
29e67 2a 20 70 49 6e 2d 3e 6e 4d 65 6d 20 63 6f 6c 75  * pIn->nMem colu
29e68 6d 6e 73 20 74 6f 20 62 65 20 6f 75 74 70 75 74  mns to be output
29e69 2e 20 20 70 44 65 73 74 20 69 73 20 77 68 65 72  .  pDest is wher
29e6a 65 20 74 68 65 20 6f 75 74 70 75 74 20 73 68 6f  e the output sho
29e6b 75 6c 64 0a 2a 2a 20 62 65 20 73 65 6e 74 2e 0a  uld.** be sent..
29e6c 2a 2a 0a 2a 2a 20 72 65 67 52 65 74 75 72 6e 20  **.** regReturn 
29e6d 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
29e6e 20 74 68 65 20 72 65 67 69 73 74 65 72 20 68 6f   the register ho
29e6f 6c 64 69 6e 67 20 74 68 65 20 73 75 62 72 6f 75  lding the subrou
29e70 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 20 61  tine.** return a
29e71 64 64 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  ddress..**.** If
29e72 20 72 65 67 50 72 65 76 3e 30 20 74 68 65 6e 20   regPrev>0 then 
29e73 69 74 20 69 73 20 61 20 74 68 65 20 66 69 72 73  it is a the firs
29e74 74 20 72 65 67 69 73 74 65 72 20 69 6e 20 61 20  t register in a 
29e75 76 65 63 74 6f 72 20 74 68 61 74 0a 2a 2a 20 72  vector that.** r
29e76 65 63 6f 72 64 73 20 74 68 65 20 70 72 65 76 69  ecords the previ
29e77 6f 75 73 20 6f 75 74 70 75 74 2e 20 20 6d 65 6d  ous output.  mem
29e78 5b 72 65 67 50 72 65 76 5d 20 69 73 20 61 20 66  [regPrev] is a f
29e79 6c 61 67 20 74 68 61 74 20 69 73 20 66 61 6c 73  lag that is fals
29e7a 65 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 68 61  e.** if there ha
29e7b 73 20 62 65 65 6e 20 6e 6f 20 70 72 65 76 69 6f  s been no previo
29e7c 75 73 20 6f 75 74 70 75 74 2e 20 20 49 66 20 72  us output.  If r
29e7d 65 67 50 72 65 76 3e 30 20 74 68 65 6e 20 63 6f  egPrev>0 then co
29e7e 64 65 20 69 73 0a 2a 2a 20 67 65 6e 65 72 61 74  de is.** generat
29e7f 65 64 20 74 6f 20 73 75 70 70 72 65 73 73 20 64  ed to suppress d
29e80 75 70 6c 69 63 61 74 65 73 2e 20 20 70 4b 65 79  uplicates.  pKey
29e81 49 6e 66 6f 20 69 73 20 75 73 65 64 20 66 6f 72  Info is used for
29e82 20 63 6f 6d 70 61 72 69 6e 67 0a 2a 2a 20 6b 65   comparing.** ke
29e83 79 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ys..**.** If the
29e84 20 4c 49 4d 49 54 20 66 6f 75 6e 64 20 69 6e 20   LIMIT found in 
29e85 70 2d 3e 69 4c 69 6d 69 74 20 69 73 20 72 65 61  p->iLimit is rea
29e86 63 68 65 64 2c 20 6a 75 6d 70 20 69 6d 6d 65 64  ched, jump immed
29e87 69 61 74 65 6c 79 20 74 6f 0a 2a 2a 20 69 42 72  iately to.** iBr
29e88 65 61 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  eak..*/.static i
29e89 6e 74 20 67 65 6e 65 72 61 74 65 4f 75 74 70 75  nt generateOutpu
29e8a 74 53 75 62 72 6f 75 74 69 6e 65 28 0a 20 20 50  tSubroutine(.  P
29e8b 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
29e8c 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
29e8d 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  g context */.  S
29e8e 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20  elect *p,       
29e8f 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45         /* The SE
29e90 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a  LECT statement *
29e91 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a  /.  SelectDest *
29e92 70 49 6e 2c 20 20 20 20 20 20 20 20 2f 2a 20 43  pIn,        /* C
29e93 6f 72 6f 75 74 69 6e 65 20 73 75 70 70 6c 79 69  oroutine supplyi
29e94 6e 67 20 64 61 74 61 20 2a 2f 0a 20 20 53 65 6c  ng data */.  Sel
29e95 65 63 74 44 65 73 74 20 2a 70 44 65 73 74 2c 20  ectDest *pDest, 
29e96 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f       /* Where to
29e97 20 73 65 6e 64 20 74 68 65 20 64 61 74 61 20 2a   send the data *
29e98 2f 0a 20 20 69 6e 74 20 72 65 67 52 65 74 75 72  /.  int regRetur
29e99 6e 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  n,          /* T
29e9a 68 65 20 72 65 74 75 72 6e 20 61 64 64 72 65 73  he return addres
29e9b 73 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20  s register */.  
29e9c 69 6e 74 20 72 65 67 50 72 65 76 2c 20 20 20 20  int regPrev,    
29e9d 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69          /* Previ
29e9e 6f 75 73 20 72 65 73 75 6c 74 20 72 65 67 69 73  ous result regis
29e9f 74 65 72 2e 20 20 4e 6f 20 75 6e 69 71 75 65 6e  ter.  No uniquen
29ea0 65 73 73 20 69 66 20 30 20 2a 2f 0a 20 20 4b 65  ess if 0 */.  Ke
29ea1 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c  yInfo *pKeyInfo,
29ea2 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 63 6f 6d        /* For com
29ea3 70 61 72 69 6e 67 20 77 69 74 68 20 70 72 65 76  paring with prev
29ea4 69 6f 75 73 20 65 6e 74 72 79 20 2a 2f 0a 20 20  ious entry */.  
29ea5 69 6e 74 20 70 34 74 79 70 65 2c 20 20 20 20 20  int p4type,     
29ea6 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
29ea7 34 20 74 79 70 65 20 66 6f 72 20 70 4b 65 79 49  4 type for pKeyI
29ea8 6e 66 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72  nfo */.  int iBr
29ea9 65 61 6b 20 20 20 20 20 20 20 20 20 20 20 20 20  eak             
29eaa 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66   /* Jump here if
29eab 20 77 65 20 68 69 74 20 74 68 65 20 4c 49 4d 49   we hit the LIMI
29eac 54 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a  T */.){.  Vdbe *
29ead 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
29eae 65 3b 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e  e;.  int iContin
29eaf 75 65 3b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a  ue;.  int addr;.
29eb0 0a 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65  .  addr = sqlite
29eb1 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
29eb2 28 76 29 3b 0a 20 20 69 43 6f 6e 74 69 6e 75 65  (v);.  iContinue
29eb3 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
29eb4 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 20 2f  keLabel(v);..  /
29eb5 2a 20 53 75 70 70 72 65 73 73 20 64 75 70 6c 69  * Suppress dupli
29eb6 63 61 74 65 73 20 66 6f 72 20 55 4e 49 4f 4e 2c  cates for UNION,
29eb7 20 45 58 43 45 50 54 2c 20 61 6e 64 20 49 4e 54   EXCEPT, and INT
29eb8 45 52 53 45 43 54 20 0a 20 20 2a 2f 0a 20 20 69  ERSECT .  */.  i
29eb9 66 28 20 72 65 67 50 72 65 76 20 29 7b 0a 20 20  f( regPrev ){.  
29eba 20 20 69 6e 74 20 6a 31 2c 20 6a 32 3b 0a 20 20    int j1, j2;.  
29ebb 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64    j1 = sqlite3Vd
29ebc 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
29ebd 66 4e 6f 74 2c 20 72 65 67 50 72 65 76 29 3b 0a  fNot, regPrev);.
29ebe 20 20 20 20 6a 32 20 3d 20 73 71 6c 69 74 65 33      j2 = sqlite3
29ebf 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
29ec0 5f 43 6f 6d 70 61 72 65 2c 20 70 49 6e 2d 3e 69  _Compare, pIn->i
29ec1 4d 65 6d 2c 20 72 65 67 50 72 65 76 2b 31 2c 20  Mem, regPrev+1, 
29ec2 70 49 6e 2d 3e 6e 4d 65 6d 2c 0a 20 20 20 20 20  pIn->nMem,.     
29ec3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29ec4 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29           (char*)
29ec5 70 4b 65 79 49 6e 66 6f 2c 20 70 34 74 79 70 65  pKeyInfo, p4type
29ec6 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
29ec7 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4a  beAddOp3(v, OP_J
29ec8 75 6d 70 2c 20 6a 32 2b 32 2c 20 69 43 6f 6e 74  ump, j2+2, iCont
29ec9 69 6e 75 65 2c 20 6a 32 2b 32 29 3b 0a 20 20 20  inue, j2+2);.   
29eca 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
29ecb 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20  Here(v, j1);.   
29ecc 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
29ecd 43 6f 70 79 28 70 50 61 72 73 65 2c 20 70 49 6e  Copy(pParse, pIn
29ece 2d 3e 69 4d 65 6d 2c 20 72 65 67 50 72 65 76 2b  ->iMem, regPrev+
29ecf 31 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 29 3b 0a 20  1, pIn->nMem);. 
29ed0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29ed1 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
29ed2 65 72 2c 20 31 2c 20 72 65 67 50 72 65 76 29 3b  er, 1, regPrev);
29ed3 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73  .  }.  if( pPars
29ed4 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  e->db->mallocFai
29ed5 6c 65 64 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  led ) return 0;.
29ed6 0a 20 20 2f 2a 20 53 75 70 70 72 65 73 73 20 74  .  /* Suppress t
29ed7 68 65 20 74 68 65 20 66 69 72 73 74 20 4f 46 46  he the first OFF
29ed8 53 45 54 20 65 6e 74 72 69 65 73 20 69 66 20 74  SET entries if t
29ed9 68 65 72 65 20 69 73 20 61 6e 20 4f 46 46 53 45  here is an OFFSE
29eda 54 20 63 6c 61 75 73 65 0a 20 20 2a 2f 0a 20 20  T clause.  */.  
29edb 63 6f 64 65 4f 66 66 73 65 74 28 76 2c 20 70 2c  codeOffset(v, p,
29edc 20 69 43 6f 6e 74 69 6e 75 65 29 3b 0a 0a 20 20   iContinue);..  
29edd 73 77 69 74 63 68 28 20 70 44 65 73 74 2d 3e 65  switch( pDest->e
29ede 44 65 73 74 20 29 7b 0a 20 20 20 20 2f 2a 20 53  Dest ){.    /* S
29edf 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
29ee0 61 73 20 64 61 74 61 20 75 73 69 6e 67 20 61 20  as data using a 
29ee1 75 6e 69 71 75 65 20 6b 65 79 2e 0a 20 20 20 20  unique key..    
29ee2 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  */.    case SRT_
29ee3 54 61 62 6c 65 3a 0a 20 20 20 20 63 61 73 65 20  Table:.    case 
29ee4 53 52 54 5f 45 70 68 65 6d 54 61 62 3a 20 7b 0a  SRT_EphemTab: {.
29ee5 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73        int r1 = s
29ee6 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
29ee7 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
29ee8 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74 65 33  int r2 = sqlite3
29ee9 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
29eea 65 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  e);.      testca
29eeb 73 65 28 20 70 44 65 73 74 2d 3e 65 44 65 73 74  se( pDest->eDest
29eec 3d 3d 53 52 54 5f 54 61 62 6c 65 20 29 3b 0a 20  ==SRT_Table );. 
29eed 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
29eee 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54  Dest->eDest==SRT
29eef 5f 45 70 68 65 6d 54 61 62 20 29 3b 0a 20 20 20  _EphemTab );.   
29ef0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29ef1 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52  dOp3(v, OP_MakeR
29ef2 65 63 6f 72 64 2c 20 70 49 6e 2d 3e 69 4d 65 6d  ecord, pIn->iMem
29ef3 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 2c 20 72 31 29  , pIn->nMem, r1)
29ef4 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
29ef5 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29ef6 4e 65 77 52 6f 77 69 64 2c 20 70 44 65 73 74 2d  NewRowid, pDest-
29ef7 3e 69 50 61 72 6d 2c 20 72 32 29 3b 0a 20 20 20  >iParm, r2);.   
29ef8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29ef9 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72  dOp3(v, OP_Inser
29efa 74 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2c  t, pDest->iParm,
29efb 20 72 31 2c 20 72 32 29 3b 0a 20 20 20 20 20 20   r1, r2);.      
29efc 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
29efd 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50  eP5(v, OPFLAG_AP
29efe 50 45 4e 44 29 3b 0a 20 20 20 20 20 20 73 71 6c  PEND);.      sql
29eff 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
29f00 65 67 28 70 50 61 72 73 65 2c 20 72 32 29 3b 0a  eg(pParse, r2);.
29f01 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c        sqlite3Rel
29f02 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
29f03 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 62  se, r1);.      b
29f04 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66  reak;.    }..#if
29f05 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
29f06 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20 2f 2a  _SUBQUERY.    /*
29f07 20 49 66 20 77 65 20 61 72 65 20 63 72 65 61 74   If we are creat
29f08 69 6e 67 20 61 20 73 65 74 20 66 6f 72 20 61 6e  ing a set for an
29f09 20 22 65 78 70 72 20 49 4e 20 28 53 45 4c 45 43   "expr IN (SELEC
29f0a 54 20 2e 2e 2e 29 22 20 63 6f 6e 73 74 72 75 63  T ...)" construc
29f0b 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74  t,.    ** then t
29f0c 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20 61  here should be a
29f0d 20 73 69 6e 67 6c 65 20 69 74 65 6d 20 6f 6e 20   single item on 
29f0e 74 68 65 20 73 74 61 63 6b 2e 20 20 57 72 69 74  the stack.  Writ
29f0f 65 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 74  e this.    ** it
29f10 65 6d 20 69 6e 74 6f 20 74 68 65 20 73 65 74 20  em into the set 
29f11 74 61 62 6c 65 20 77 69 74 68 20 62 6f 67 75 73  table with bogus
29f12 20 64 61 74 61 2e 0a 20 20 20 20 2a 2f 0a 20 20   data..    */.  
29f13 20 20 63 61 73 65 20 53 52 54 5f 53 65 74 3a 20    case SRT_Set: 
29f14 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 31 3b 0a  {.      int r1;.
29f15 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49        assert( pI
29f16 6e 2d 3e 6e 4d 65 6d 3d 3d 31 20 29 3b 0a 20 20  n->nMem==1 );.  
29f17 20 20 20 20 70 2d 3e 61 66 66 69 6e 69 74 79 20      p->affinity 
29f18 3d 20 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69  = .         sqli
29f19 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69  te3CompareAffini
29f1a 74 79 28 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b  ty(p->pEList->a[
29f1b 30 5d 2e 70 45 78 70 72 2c 20 70 44 65 73 74 2d  0].pExpr, pDest-
29f1c 3e 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 20 20  >affinity);.    
29f1d 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65    r1 = sqlite3Ge
29f1e 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
29f1f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
29f20 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
29f21 4d 61 6b 65 52 65 63 6f 72 64 2c 20 70 49 6e 2d  MakeRecord, pIn-
29f22 3e 69 4d 65 6d 2c 20 31 2c 20 72 31 2c 20 26 70  >iMem, 1, r1, &p
29f23 2d 3e 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a  ->affinity, 1);.
29f24 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
29f25 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68  rCacheAffinityCh
29f26 61 6e 67 65 28 70 50 61 72 73 65 2c 20 70 49 6e  ange(pParse, pIn
29f27 2d 3e 69 4d 65 6d 2c 20 31 29 3b 0a 20 20 20 20  ->iMem, 1);.    
29f28 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29f29 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73  Op2(v, OP_IdxIns
29f2a 65 72 74 2c 20 70 44 65 73 74 2d 3e 69 50 61 72  ert, pDest->iPar
29f2b 6d 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71  m, r1);.      sq
29f2c 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
29f2d 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
29f2e 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
29f2f 20 20 7d 0a 0a 23 69 66 20 30 20 20 2f 2a 20 4e    }..#if 0  /* N
29f30 65 76 65 72 20 6f 63 63 75 72 73 20 6f 6e 20 61  ever occurs on a
29f31 6e 20 4f 52 44 45 52 20 42 59 20 71 75 65 72 79  n ORDER BY query
29f32 20 2a 2f 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e   */.    /* If an
29f33 79 20 72 6f 77 20 65 78 69 73 74 20 69 6e 20 74  y row exist in t
29f34 68 65 20 72 65 73 75 6c 74 20 73 65 74 2c 20 72  he result set, r
29f35 65 63 6f 72 64 20 74 68 61 74 20 66 61 63 74 20  ecord that fact 
29f36 61 6e 64 20 61 62 6f 72 74 2e 0a 20 20 20 20 2a  and abort..    *
29f37 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 45  /.    case SRT_E
29f38 78 69 73 74 73 3a 20 7b 0a 20 20 20 20 20 20 73  xists: {.      s
29f39 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
29f3a 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
29f3b 31 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 29  1, pDest->iParm)
29f3c 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c  ;.      /* The L
29f3d 49 4d 49 54 20 63 6c 61 75 73 65 20 77 69 6c 6c  IMIT clause will
29f3e 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 6c   terminate the l
29f3f 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20  oop for us */.  
29f40 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
29f41 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20  .#endif..    /* 
29f42 49 66 20 74 68 69 73 20 69 73 20 61 20 73 63 61  If this is a sca
29f43 6c 61 72 20 73 65 6c 65 63 74 20 74 68 61 74 20  lar select that 
29f44 69 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65 78  is part of an ex
29f45 70 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e 0a 20  pression, then. 
29f46 20 20 20 2a 2a 20 73 74 6f 72 65 20 74 68 65 20     ** store the 
29f47 72 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20 61  results in the a
29f48 70 70 72 6f 70 72 69 61 74 65 20 6d 65 6d 6f 72  ppropriate memor
29f49 79 20 63 65 6c 6c 20 61 6e 64 20 62 72 65 61 6b  y cell and break
29f4a 20 6f 75 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74   out.    ** of t
29f4b 68 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20  he scan loop..  
29f4c 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52    */.    case SR
29f4d 54 5f 4d 65 6d 3a 20 7b 0a 20 20 20 20 20 20 61  T_Mem: {.      a
29f4e 73 73 65 72 74 28 20 70 49 6e 2d 3e 6e 4d 65 6d  ssert( pIn->nMem
29f4f 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==1 );.      sql
29f50 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65  ite3ExprCodeMove
29f51 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d  (pParse, pIn->iM
29f52 65 6d 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  em, pDest->iParm
29f53 2c 20 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54  , 1);.      /* T
29f54 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20  he LIMIT clause 
29f55 77 69 6c 6c 20 6a 75 6d 70 20 6f 75 74 20 6f 66  will jump out of
29f56 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73   the loop for us
29f57 20 2a 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   */.      break;
29f58 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  .    }.#endif /*
29f59 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f   #ifndef SQLITE_
29f5a 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f  OMIT_SUBQUERY */
29f5b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 73  ..    /* The res
29f5c 75 6c 74 73 20 61 72 65 20 73 74 6f 72 65 64 20  ults are stored 
29f5d 69 6e 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66  in a sequence of
29f5e 20 72 65 67 69 73 74 65 72 73 0a 20 20 20 20 2a   registers.    *
29f5f 2a 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 44  * starting at pD
29f60 65 73 74 2d 3e 69 4d 65 6d 2e 20 20 54 68 65 6e  est->iMem.  Then
29f61 20 74 68 65 20 63 6f 2d 72 6f 75 74 69 6e 65 20   the co-routine 
29f62 79 69 65 6c 64 73 2e 0a 20 20 20 20 2a 2f 0a 20  yields..    */. 
29f63 20 20 20 63 61 73 65 20 53 52 54 5f 43 6f 72 6f     case SRT_Coro
29f64 75 74 69 6e 65 3a 20 7b 0a 20 20 20 20 20 20 69  utine: {.      i
29f65 66 28 20 70 44 65 73 74 2d 3e 69 4d 65 6d 3d 3d  f( pDest->iMem==
29f66 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 65  0 ){.        pDe
29f67 73 74 2d 3e 69 4d 65 6d 20 3d 20 73 71 6c 69 74  st->iMem = sqlit
29f68 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70  e3GetTempRange(p
29f69 50 61 72 73 65 2c 20 70 49 6e 2d 3e 6e 4d 65 6d  Parse, pIn->nMem
29f6a 29 3b 0a 20 20 20 20 20 20 20 20 70 44 65 73 74  );.        pDest
29f6b 2d 3e 6e 4d 65 6d 20 3d 20 70 49 6e 2d 3e 6e 4d  ->nMem = pIn->nM
29f6c 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  em;.      }.    
29f6d 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
29f6e 65 4d 6f 76 65 28 70 50 61 72 73 65 2c 20 70 49  eMove(pParse, pI
29f6f 6e 2d 3e 69 4d 65 6d 2c 20 70 44 65 73 74 2d 3e  n->iMem, pDest->
29f70 69 4d 65 6d 2c 20 70 44 65 73 74 2d 3e 6e 4d 65  iMem, pDest->nMe
29f71 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  m);.      sqlite
29f72 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
29f73 50 5f 59 69 65 6c 64 2c 20 70 44 65 73 74 2d 3e  P_Yield, pDest->
29f74 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 62 72  iParm);.      br
29f75 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
29f76 2f 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68  /* If none of th
29f77 65 20 61 62 6f 76 65 2c 20 74 68 65 6e 20 74 68  e above, then th
29f78 65 20 72 65 73 75 6c 74 20 64 65 73 74 69 6e 61  e result destina
29f79 74 69 6f 6e 20 6d 75 73 74 20 62 65 0a 20 20 20  tion must be.   
29f7a 20 2a 2a 20 53 52 54 5f 4f 75 74 70 75 74 2e 20   ** SRT_Output. 
29f7b 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
29f7c 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 77 69   never called wi
29f7d 74 68 20 61 6e 79 20 6f 74 68 65 72 0a 20 20 20  th any other.   
29f7e 20 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20   ** destination 
29f7f 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f  other than the o
29f80 6e 65 73 20 68 61 6e 64 6c 65 64 20 61 62 6f 76  nes handled abov
29f81 65 20 6f 72 20 53 52 54 5f 4f 75 74 70 75 74 2e  e or SRT_Output.
29f82 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46  .    **.    ** F
29f83 6f 72 20 53 52 54 5f 4f 75 74 70 75 74 2c 20 72  or SRT_Output, r
29f84 65 73 75 6c 74 73 20 61 72 65 20 73 74 6f 72 65  esults are store
29f85 64 20 69 6e 20 61 20 73 65 71 75 65 6e 63 65 20  d in a sequence 
29f86 6f 66 20 72 65 67 69 73 74 65 72 73 2e 20 20 0a  of registers.  .
29f87 20 20 20 20 2a 2a 20 54 68 65 6e 20 74 68 65 20      ** Then the 
29f88 4f 50 5f 52 65 73 75 6c 74 52 6f 77 20 6f 70 63  OP_ResultRow opc
29f89 6f 64 65 20 69 73 20 75 73 65 64 20 74 6f 20 63  ode is used to c
29f8a 61 75 73 65 20 73 71 6c 69 74 65 33 5f 73 74 65  ause sqlite3_ste
29f8b 70 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65  p() to.    ** re
29f8c 74 75 72 6e 20 74 68 65 20 6e 65 78 74 20 72 6f  turn the next ro
29f8d 77 20 6f 66 20 72 65 73 75 6c 74 2e 0a 20 20 20  w of result..   
29f8e 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 3a   */.    default:
29f8f 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
29f90 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53   pDest->eDest==S
29f91 52 54 5f 4f 75 74 70 75 74 20 29 3b 0a 20 20 20  RT_Output );.   
29f92 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29f93 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
29f94 74 52 6f 77 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c  tRow, pIn->iMem,
29f95 20 70 49 6e 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20   pIn->nMem);.   
29f96 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
29f97 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
29f98 65 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69  e(pParse, pIn->i
29f99 4d 65 6d 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 29 3b  Mem, pIn->nMem);
29f9a 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
29f9b 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75    }.  }..  /* Ju
29f9c 6d 70 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  mp to the end of
29f9d 20 74 68 65 20 6c 6f 6f 70 20 69 66 20 74 68 65   the loop if the
29f9e 20 4c 49 4d 49 54 20 69 73 20 72 65 61 63 68 65   LIMIT is reache
29f9f 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  d..  */.  if( p-
29fa0 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 73  >iLimit ){.    s
29fa1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
29fa2 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 70  (v, OP_AddImm, p
29fa3 2d 3e 69 4c 69 6d 69 74 2c 20 2d 31 29 3b 0a 20  ->iLimit, -1);. 
29fa4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29fa5 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 5a 65 72  dOp2(v, OP_IfZer
29fa6 6f 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20 69 42  o, p->iLimit, iB
29fa7 72 65 61 6b 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  reak);.  }..  /*
29fa8 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 73 75   Generate the su
29fa9 62 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 0a  broutine return.
29faa 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
29fab 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
29fac 2c 20 69 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20  , iContinue);.  
29fad 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29fae 31 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c 20  1(v, OP_Return, 
29faf 72 65 67 52 65 74 75 72 6e 29 3b 0a 0a 20 20 72  regReturn);..  r
29fb0 65 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f  eturn addr;.}../
29fb1 2a 0a 2a 2a 20 41 6c 74 65 72 6e 61 74 69 76 65  *.** Alternative
29fb2 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74   compound select
29fb3 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20   code generator 
29fb4 66 6f 72 20 63 61 73 65 73 20 77 68 65 6e 20 74  for cases when t
29fb5 68 65 72 65 0a 2a 2a 20 69 73 20 61 6e 20 4f 52  here.** is an OR
29fb6 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a  DER BY clause..*
29fb7 2a 0a 2a 2a 20 57 65 20 61 73 73 75 6d 65 20 61  *.** We assume a
29fb8 20 71 75 65 72 79 20 6f 66 20 74 68 65 20 66 6f   query of the fo
29fb9 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 3a 0a 2a 2a  llowing form:.**
29fba 0a 2a 2a 20 20 20 20 20 20 3c 73 65 6c 65 63 74  .**      <select
29fbb 41 3e 20 20 3c 6f 70 65 72 61 74 6f 72 3e 20 20  A>  <operator>  
29fbc 3c 73 65 6c 65 63 74 42 3e 20 20 4f 52 44 45 52  <selectB>  ORDER
29fbd 20 42 59 20 3c 6f 72 64 65 72 62 79 6c 69 73 74   BY <orderbylist
29fbe 3e 0a 2a 2a 0a 2a 2a 20 3c 6f 70 65 72 61 74 6f  >.**.** <operato
29fbf 72 3e 20 69 73 20 6f 6e 65 20 6f 66 20 55 4e 49  r> is one of UNI
29fc0 4f 4e 20 41 4c 4c 2c 20 55 4e 49 4f 4e 2c 20 45  ON ALL, UNION, E
29fc1 58 43 45 50 54 2c 20 6f 72 20 49 4e 54 45 52 53  XCEPT, or INTERS
29fc2 45 43 54 2e 20 20 54 68 65 20 69 64 65 61 0a 2a  ECT.  The idea.*
29fc3 2a 20 69 73 20 74 6f 20 63 6f 64 65 20 62 6f 74  * is to code bot
29fc4 68 20 3c 73 65 6c 65 63 74 41 3e 20 61 6e 64 20  h <selectA> and 
29fc5 3c 73 65 6c 65 63 74 42 3e 20 77 69 74 68 20 74  <selectB> with t
29fc6 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
29fc7 73 65 20 61 73 0a 2a 2a 20 63 6f 2d 72 6f 75 74  se as.** co-rout
29fc8 69 6e 65 73 2e 20 20 54 68 65 6e 20 72 75 6e 20  ines.  Then run 
29fc9 74 68 65 20 63 6f 2d 72 6f 75 74 69 6e 65 73 20  the co-routines 
29fca 69 6e 20 70 61 72 61 6c 6c 65 6c 20 61 6e 64 20  in parallel and 
29fcb 6d 65 72 67 65 20 74 68 65 20 72 65 73 75 6c 74  merge the result
29fcc 73 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6f 75  s.** into the ou
29fcd 74 70 75 74 2e 20 20 49 6e 20 61 64 64 69 74 69  tput.  In additi
29fce 6f 6e 20 74 6f 20 74 68 65 20 74 77 6f 20 63 6f  on to the two co
29fcf 72 6f 75 74 69 6e 65 73 20 28 63 61 6c 6c 65 64  routines (called
29fd0 20 73 65 6c 65 63 74 41 20 61 6e 64 0a 2a 2a 20   selectA and.** 
29fd1 73 65 6c 65 63 74 42 29 20 74 68 65 72 65 20 61  selectB) there a
29fd2 72 65 20 37 20 73 75 62 72 6f 75 74 69 6e 65 73  re 7 subroutines
29fd3 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 6f 75 74 41 3a  :.**.**    outA:
29fd4 20 20 20 20 4d 6f 76 65 20 74 68 65 20 6f 75 74      Move the out
29fd5 70 75 74 20 6f 66 20 74 68 65 20 73 65 6c 65 63  put of the selec
29fd6 74 41 20 63 6f 72 6f 75 74 69 6e 65 20 69 6e 74  tA coroutine int
29fd7 6f 20 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20  o the output.** 
29fd8 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 74              of t
29fd9 68 65 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72  he compound quer
29fda 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 6f 75 74 42  y..**.**    outB
29fdb 3a 20 20 20 20 4d 6f 76 65 20 74 68 65 20 6f 75  :    Move the ou
29fdc 74 70 75 74 20 6f 66 20 74 68 65 20 73 65 6c 65  tput of the sele
29fdd 63 74 42 20 63 6f 72 6f 75 74 69 6e 65 20 69 6e  ctB coroutine in
29fde 74 6f 20 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a  to the output.**
29fdf 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20               of 
29fe0 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65  the compound que
29fe1 72 79 2e 20 20 28 4f 6e 6c 79 20 67 65 6e 65 72  ry.  (Only gener
29fe2 61 74 65 64 20 66 6f 72 20 55 4e 49 4f 4e 20 61  ated for UNION a
29fe3 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  nd.**           
29fe4 20 20 55 4e 49 4f 4e 20 41 4c 4c 2e 20 20 45 58    UNION ALL.  EX
29fe5 43 45 50 54 20 61 6e 64 20 49 4e 53 45 52 54 53  CEPT and INSERTS
29fe6 45 43 54 20 6e 65 76 65 72 20 6f 75 74 70 75 74  ECT never output
29fe7 20 61 20 72 6f 77 20 74 68 61 74 0a 2a 2a 20 20   a row that.**  
29fe8 20 20 20 20 20 20 20 20 20 20 20 61 70 70 65 61             appea
29fe9 72 73 20 6f 6e 6c 79 20 69 6e 20 42 2e 29 0a 2a  rs only in B.).*
29fea 2a 0a 2a 2a 20 20 20 20 41 6c 74 42 3a 20 20 20  *.**    AltB:   
29feb 20 43 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65   Called when the
29fec 72 65 20 69 73 20 64 61 74 61 20 66 72 6f 6d 20  re is data from 
29fed 62 6f 74 68 20 63 6f 72 6f 75 74 69 6e 65 73 20  both coroutines 
29fee 61 6e 64 20 41 3c 42 2e 0a 2a 2a 0a 2a 2a 20 20  and A<B..**.**  
29fef 20 20 41 65 71 42 3a 20 20 20 20 43 61 6c 6c 65    AeqB:    Calle
29ff0 64 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20  d when there is 
29ff1 64 61 74 61 20 66 72 6f 6d 20 62 6f 74 68 20 63  data from both c
29ff2 6f 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 41 3d  oroutines and A=
29ff3 3d 42 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 41 67 74  =B..**.**    Agt
29ff4 42 3a 20 20 20 20 43 61 6c 6c 65 64 20 77 68 65  B:    Called whe
29ff5 6e 20 74 68 65 72 65 20 69 73 20 64 61 74 61 20  n there is data 
29ff6 66 72 6f 6d 20 62 6f 74 68 20 63 6f 72 6f 75 74  from both corout
29ff7 69 6e 65 73 20 61 6e 64 20 41 3e 42 2e 0a 2a 2a  ines and A>B..**
29ff8 0a 2a 2a 20 20 20 20 45 6f 66 41 3a 20 20 20 20  .**    EofA:    
29ff9 43 61 6c 6c 65 64 20 77 68 65 6e 20 64 61 74 61  Called when data
29ffa 20 69 73 20 65 78 68 61 75 73 74 65 64 20 66 72   is exhausted fr
29ffb 6f 6d 20 73 65 6c 65 63 74 41 2e 0a 2a 2a 0a 2a  om selectA..**.*
29ffc 2a 20 20 20 20 45 6f 66 42 3a 20 20 20 20 43 61  *    EofB:    Ca
29ffd 6c 6c 65 64 20 77 68 65 6e 20 64 61 74 61 20 69  lled when data i
29ffe 73 20 65 78 68 61 75 73 74 65 64 20 66 72 6f 6d  s exhausted from
29fff 20 73 65 6c 65 63 74 42 2e 0a 2a 2a 0a 2a 2a 20   selectB..**.** 
2a000 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  The implementati
2a001 6f 6e 20 6f 66 20 74 68 65 20 6c 61 74 74 65 72  on of the latter
2a002 20 66 69 76 65 20 73 75 62 72 6f 75 74 69 6e 65   five subroutine
2a003 73 20 64 65 70 65 6e 64 20 6f 6e 20 77 68 69 63  s depend on whic
2a004 68 20 0a 2a 2a 20 3c 6f 70 65 72 61 74 6f 72 3e  h .** <operator>
2a005 20 69 73 20 75 73 65 64 3a 0a 2a 2a 0a 2a 2a 0a   is used:.**.**.
2a006 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 55  **             U
2a007 4e 49 4f 4e 20 41 4c 4c 20 20 20 20 20 20 20 20  NION ALL        
2a008 20 55 4e 49 4f 4e 20 20 20 20 20 20 20 20 20 20   UNION          
2a009 20 20 45 58 43 45 50 54 20 20 20 20 20 20 20 20    EXCEPT        
2a00a 20 20 49 4e 54 45 52 53 45 43 54 0a 2a 2a 20 20    INTERSECT.**  
2a00b 20 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d          --------
2a00c 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  -----  ---------
2a00d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d  --------  ------
2a00e 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d  --------  ------
2a00f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20  -----------.**  
2a010 20 41 6c 74 42 3a 20 20 20 6f 75 74 41 2c 20 6e   AltB:   outA, n
2a011 65 78 74 41 20 20 20 20 20 20 6f 75 74 41 2c 20  extA      outA, 
2a012 6e 65 78 74 41 20 20 20 20 20 20 20 6f 75 74 41  nextA       outA
2a013 2c 20 6e 65 78 74 41 20 20 20 20 20 20 20 20 20  , nextA         
2a014 6e 65 78 74 41 0a 2a 2a 0a 2a 2a 20 20 20 41 65  nextA.**.**   Ae
2a015 71 42 3a 20 20 20 6f 75 74 41 2c 20 6e 65 78 74  qB:   outA, next
2a016 41 20 20 20 20 20 20 20 20 20 6e 65 78 74 41 20  A         nextA 
2a017 20 20 20 20 20 20 20 20 20 20 20 20 6e 65 78 74              next
2a018 41 20 20 20 20 20 20 20 20 20 6f 75 74 41 2c 20  A         outA, 
2a019 6e 65 78 74 41 0a 2a 2a 0a 2a 2a 20 20 20 41 67  nextA.**.**   Ag
2a01a 74 42 3a 20 20 20 6f 75 74 42 2c 20 6e 65 78 74  tB:   outB, next
2a01b 42 20 20 20 20 20 20 6f 75 74 42 2c 20 6e 65 78  B      outB, nex
2a01c 74 42 20 20 20 20 20 20 20 20 20 20 6e 65 78 74  tB          next
2a01d 42 20 20 20 20 20 20 20 20 20 20 20 20 6e 65 78  B            nex
2a01e 74 42 0a 2a 2a 0a 2a 2a 20 20 20 45 6f 66 41 3a  tB.**.**   EofA:
2a01f 20 20 20 6f 75 74 42 2c 20 6e 65 78 74 42 20 20     outB, nextB  
2a020 20 20 20 20 6f 75 74 42 2c 20 6e 65 78 74 42 20      outB, nextB 
2a021 20 20 20 20 20 20 20 20 20 68 61 6c 74 20 20 20           halt   
2a022 20 20 20 20 20 20 20 20 20 20 68 61 6c 74 0a 2a            halt.*
2a023 2a 0a 2a 2a 20 20 20 45 6f 66 42 3a 20 20 20 6f  *.**   EofB:   o
2a024 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20  utA, nextA      
2a025 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20  outA, nextA     
2a026 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20    outA, nextA   
2a027 20 20 20 20 20 20 68 61 6c 74 0a 2a 2a 0a 2a 2a        halt.**.**
2a028 20 49 6e 20 74 68 65 20 41 6c 74 42 2c 20 41 65   In the AltB, Ae
2a029 71 42 2c 20 61 6e 64 20 41 67 74 42 20 73 75 62  qB, and AgtB sub
2a02a 72 6f 75 74 69 6e 65 73 2c 20 61 6e 20 45 4f 46  routines, an EOF
2a02b 20 6f 6e 20 41 20 66 6f 6c 6c 6f 77 69 6e 67 20   on A following 
2a02c 6e 65 78 74 41 0a 2a 2a 20 63 61 75 73 65 73 20  nextA.** causes 
2a02d 61 6e 20 69 6d 6d 65 64 69 61 74 65 20 6a 75 6d  an immediate jum
2a02e 70 20 74 6f 20 45 6f 66 41 20 61 6e 64 20 61 6e  p to EofA and an
2a02f 20 45 4f 46 20 6f 6e 20 42 20 66 6f 6c 6c 6f 77   EOF on B follow
2a030 69 6e 67 20 6e 65 78 74 42 20 63 61 75 73 65 73  ing nextB causes
2a031 0a 2a 2a 20 61 6e 20 69 6d 6d 65 64 69 61 74 65  .** an immediate
2a032 20 6a 75 6d 70 20 74 6f 20 45 6f 66 42 2e 20 20   jump to EofB.  
2a033 57 69 74 68 69 6e 20 45 6f 66 41 20 61 6e 64 20  Within EofA and 
2a034 45 6f 66 42 2c 20 61 6e 64 20 45 4f 46 20 6f 6e  EofB, and EOF on
2a035 20 65 6e 74 72 79 20 6f 72 0a 2a 2a 20 66 6f 6c   entry or.** fol
2a036 6c 6f 77 69 6e 67 20 6e 65 78 74 58 20 63 61 75  lowing nextX cau
2a037 73 65 73 20 61 20 6a 75 6d 70 20 74 6f 20 74 68  ses a jump to th
2a038 65 20 65 6e 64 20 6f 66 20 74 68 65 20 73 65 6c  e end of the sel
2a039 65 63 74 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a  ect processing..
2a03a 2a 2a 0a 2a 2a 20 44 75 70 6c 69 63 61 74 65 20  **.** Duplicate 
2a03b 72 65 6d 6f 76 61 6c 20 69 6e 20 74 68 65 20 55  removal in the U
2a03c 4e 49 4f 4e 2c 20 45 58 43 45 50 54 2c 20 61 6e  NION, EXCEPT, an
2a03d 64 20 49 4e 54 45 52 53 45 43 54 20 63 61 73 65  d INTERSECT case
2a03e 73 20 69 73 20 68 61 6e 64 6c 65 64 0a 2a 2a 20  s is handled.** 
2a03f 77 69 74 68 69 6e 20 74 68 65 20 6f 75 74 70 75  within the outpu
2a040 74 20 73 75 62 72 6f 75 74 69 6e 65 2e 20 20 54  t subroutine.  T
2a041 68 65 20 72 65 67 50 72 65 76 20 72 65 67 69 73  he regPrev regis
2a042 74 65 72 20 73 65 74 20 68 6f 6c 64 73 20 74 68  ter set holds th
2a043 65 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20  e previously.** 
2a044 6f 75 74 70 75 74 20 76 61 6c 75 65 2e 20 20 41  output value.  A
2a045 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 6d   comparison is m
2a046 61 64 65 20 61 67 61 69 6e 73 74 20 74 68 69 73  ade against this
2a047 20 76 61 6c 75 65 20 61 6e 64 20 74 68 65 20 6f   value and the o
2a048 75 74 70 75 74 0a 2a 2a 20 69 73 20 73 6b 69 70  utput.** is skip
2a049 70 65 64 20 69 66 20 74 68 65 20 6e 65 78 74 20  ped if the next 
2a04a 72 65 73 75 6c 74 73 20 77 6f 75 6c 64 20 62 65  results would be
2a04b 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
2a04c 20 70 72 65 76 69 6f 75 73 2e 0a 2a 2a 0a 2a 2a   previous..**.**
2a04d 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   The implementat
2a04e 69 6f 6e 20 70 6c 61 6e 20 69 73 20 74 6f 20 69  ion plan is to i
2a04f 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 74 77 6f  mplement the two
2a050 20 63 6f 72 6f 75 74 69 6e 65 73 20 61 6e 64 20   coroutines and 
2a051 73 65 76 65 6e 0a 2a 2a 20 73 75 62 72 6f 75 74  seven.** subrout
2a052 69 6e 65 73 20 66 69 72 73 74 2c 20 74 68 65 6e  ines first, then
2a053 20 70 75 74 20 74 68 65 20 63 6f 6e 74 72 6f 6c   put the control
2a054 20 6c 6f 67 69 63 20 61 74 20 74 68 65 20 62 6f   logic at the bo
2a055 74 74 6f 6d 2e 20 20 4c 69 6b 65 20 74 68 69 73  ttom.  Like this
2a056 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  :.**.**         
2a057 20 67 6f 74 6f 20 49 6e 69 74 0a 2a 2a 20 20 20   goto Init.**   
2a058 20 20 63 6f 41 3a 20 63 6f 72 6f 75 74 69 6e 65    coA: coroutine
2a059 20 66 6f 72 20 6c 65 66 74 20 71 75 65 72 79 20   for left query 
2a05a 28 41 29 0a 2a 2a 20 20 20 20 20 63 6f 42 3a 20  (A).**     coB: 
2a05b 63 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20 72 69  coroutine for ri
2a05c 67 68 74 20 71 75 65 72 79 20 28 42 29 0a 2a 2a  ght query (B).**
2a05d 20 20 20 20 6f 75 74 41 3a 20 6f 75 74 70 75 74      outA: output
2a05e 20 6f 6e 65 20 72 6f 77 20 6f 66 20 41 0a 2a 2a   one row of A.**
2a05f 20 20 20 20 6f 75 74 42 3a 20 6f 75 74 70 75 74      outB: output
2a060 20 6f 6e 65 20 72 6f 77 20 6f 66 20 42 20 28 55   one row of B (U
2a061 4e 49 4f 4e 20 61 6e 64 20 55 4e 49 4f 4e 20 41  NION and UNION A
2a062 4c 4c 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 20 45  LL only).**    E
2a063 6f 66 41 3a 20 2e 2e 2e 0a 2a 2a 20 20 20 20 45  ofA: ....**    E
2a064 6f 66 42 3a 20 2e 2e 2e 0a 2a 2a 20 20 20 20 41  ofB: ....**    A
2a065 6c 74 42 3a 20 2e 2e 2e 0a 2a 2a 20 20 20 20 41  ltB: ....**    A
2a066 65 71 42 3a 20 2e 2e 2e 0a 2a 2a 20 20 20 20 41  eqB: ....**    A
2a067 67 74 42 3a 20 2e 2e 2e 0a 2a 2a 20 20 20 20 49  gtB: ....**    I
2a068 6e 69 74 3a 20 69 6e 69 74 69 61 6c 69 7a 65 20  nit: initialize 
2a069 63 6f 72 6f 75 74 69 6e 65 20 72 65 67 69 73 74  coroutine regist
2a06a 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ers.**          
2a06b 79 69 65 6c 64 20 63 6f 41 0a 2a 2a 20 20 20 20  yield coA.**    
2a06c 20 20 20 20 20 20 69 66 20 65 6f 66 28 41 29 20        if eof(A) 
2a06d 67 6f 74 6f 20 45 6f 66 41 0a 2a 2a 20 20 20 20  goto EofA.**    
2a06e 20 20 20 20 20 20 79 69 65 6c 64 20 63 6f 42 0a        yield coB.
2a06f 2a 2a 20 20 20 20 20 20 20 20 20 20 69 66 20 65  **          if e
2a070 6f 66 28 42 29 20 67 6f 74 6f 20 45 6f 66 42 0a  of(B) goto EofB.
2a071 2a 2a 20 20 20 20 43 6d 70 72 3a 20 43 6f 6d 70  **    Cmpr: Comp
2a072 61 72 65 20 41 2c 20 42 0a 2a 2a 20 20 20 20 20  are A, B.**     
2a073 20 20 20 20 20 4a 75 6d 70 20 41 6c 74 42 2c 20       Jump AltB, 
2a074 41 65 71 42 2c 20 41 67 74 42 0a 2a 2a 20 20 20  AeqB, AgtB.**   
2a075 20 20 45 6e 64 3a 20 2e 2e 2e 0a 2a 2a 0a 2a 2a    End: ....**.**
2a076 20 57 65 20 63 61 6c 6c 20 41 6c 74 42 2c 20 41   We call AltB, A
2a077 65 71 42 2c 20 41 67 74 42 2c 20 45 6f 66 41 2c  eqB, AgtB, EofA,
2a078 20 61 6e 64 20 45 6f 66 42 20 22 73 75 62 72 6f   and EofB "subro
2a079 75 74 69 6e 65 73 22 20 62 75 74 20 74 68 65 79  utines" but they
2a07a 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 61 63 74 75   are not.** actu
2a07b 61 6c 6c 79 20 63 61 6c 6c 65 64 20 75 73 69 6e  ally called usin
2a07c 67 20 47 6f 73 75 62 20 61 6e 64 20 74 68 65 79  g Gosub and they
2a07d 20 64 6f 20 6e 6f 74 20 52 65 74 75 72 6e 2e 20   do not Return. 
2a07e 20 45 6f 66 41 20 61 6e 64 20 45 6f 66 42 20 6c   EofA and EofB l
2a07f 6f 6f 70 0a 2a 2a 20 75 6e 74 69 6c 20 61 6c 6c  oop.** until all
2a080 20 64 61 74 61 20 69 73 20 65 78 68 61 75 73 74   data is exhaust
2a081 65 64 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20  ed then jump to 
2a082 74 68 65 20 22 65 6e 64 22 20 6c 61 62 65 2e 20  the "end" labe. 
2a083 20 41 6c 74 42 2c 20 41 65 71 42 2c 0a 2a 2a 20   AltB, AeqB,.** 
2a084 61 6e 64 20 41 67 74 42 20 6a 75 6d 70 20 74 6f  and AgtB jump to
2a085 20 65 69 74 68 65 72 20 4c 32 20 6f 72 20 74 6f   either L2 or to
2a086 20 6f 6e 65 20 6f 66 20 45 6f 66 41 20 6f 72 20   one of EofA or 
2a087 45 6f 66 42 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  EofB..*/.#ifndef
2a088 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d   SQLITE_OMIT_COM
2a089 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 73 74 61  POUND_SELECT.sta
2a08a 74 69 63 20 69 6e 74 20 6d 75 6c 74 69 53 65 6c  tic int multiSel
2a08b 65 63 74 4f 72 64 65 72 42 79 28 0a 20 20 50 61  ectOrderBy(.  Pa
2a08c 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2a08d 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
2a08e 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65  ontext */.  Sele
2a08f 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  ct *p,          
2a090 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 6d    /* The right-m
2a091 6f 73 74 20 6f 66 20 53 45 4c 45 43 54 73 20 74  ost of SELECTs t
2a092 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20  o be coded */.  
2a093 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73  SelectDest *pDes
2a094 74 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f  t     /* What to
2a095 20 64 6f 20 77 69 74 68 20 71 75 65 72 79 20 72   do with query r
2a096 65 73 75 6c 74 73 20 2a 2f 0a 29 7b 0a 20 20 69  esults */.){.  i
2a097 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20  nt i, j;        
2a098 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
2a099 6e 74 65 72 73 20 2a 2f 0a 20 20 53 65 6c 65 63  nters */.  Selec
2a09a 74 20 2a 70 50 72 69 6f 72 3b 20 20 20 20 20 20  t *pPrior;      
2a09b 20 2f 2a 20 41 6e 6f 74 68 65 72 20 53 45 4c 45   /* Another SELE
2a09c 43 54 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74  CT immediately t
2a09d 6f 20 6f 75 72 20 6c 65 66 74 20 2a 2f 0a 20 20  o our left */.  
2a09e 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20  Vdbe *v;        
2a09f 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74        /* Generat
2a0a0 65 20 63 6f 64 65 20 74 6f 20 74 68 69 73 20 56  e code to this V
2a0a1 44 42 45 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44  DBE */.  SelectD
2a0a2 65 73 74 20 64 65 73 74 41 3b 20 20 20 20 20 2f  est destA;     /
2a0a3 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 66 6f  * Destination fo
2a0a4 72 20 63 6f 72 6f 75 74 69 6e 65 20 41 20 2a 2f  r coroutine A */
2a0a5 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65  .  SelectDest de
2a0a6 73 74 42 3b 20 20 20 20 20 2f 2a 20 44 65 73 74  stB;     /* Dest
2a0a7 69 6e 61 74 69 6f 6e 20 66 6f 72 20 63 6f 72 6f  ination for coro
2a0a8 75 74 69 6e 65 20 42 20 2a 2f 0a 20 20 69 6e 74  utine B */.  int
2a0a9 20 72 65 67 41 64 64 72 41 3b 20 20 20 20 20 20   regAddrA;      
2a0aa 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 72 65     /* Address re
2a0ab 67 69 73 74 65 72 20 66 6f 72 20 73 65 6c 65 63  gister for selec
2a0ac 74 2d 41 20 63 6f 72 6f 75 74 69 6e 65 20 2a 2f  t-A coroutine */
2a0ad 0a 20 20 69 6e 74 20 72 65 67 45 6f 66 41 3b 20  .  int regEofA; 
2a0ae 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67           /* Flag
2a0af 20 74 6f 20 69 6e 64 69 63 61 74 65 20 77 68 65   to indicate whe
2a0b0 6e 20 73 65 6c 65 63 74 2d 41 20 69 73 20 63 6f  n select-A is co
2a0b1 6d 70 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20  mplete */.  int 
2a0b2 72 65 67 41 64 64 72 42 3b 20 20 20 20 20 20 20  regAddrB;       
2a0b3 20 20 2f 2a 20 41 64 64 72 65 73 73 20 72 65 67    /* Address reg
2a0b4 69 73 74 65 72 20 66 6f 72 20 73 65 6c 65 63 74  ister for select
2a0b5 2d 42 20 63 6f 72 6f 75 74 69 6e 65 20 2a 2f 0a  -B coroutine */.
2a0b6 20 20 69 6e 74 20 72 65 67 45 6f 66 42 3b 20 20    int regEofB;  
2a0b7 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 20          /* Flag 
2a0b8 74 6f 20 69 6e 64 69 63 61 74 65 20 77 68 65 6e  to indicate when
2a0b9 20 73 65 6c 65 63 74 2d 42 20 69 73 20 63 6f 6d   select-B is com
2a0ba 70 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 61  plete */.  int a
2a0bb 64 64 72 53 65 6c 65 63 74 41 3b 20 20 20 20 20  ddrSelectA;     
2a0bc 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74   /* Address of t
2a0bd 68 65 20 73 65 6c 65 63 74 2d 41 20 63 6f 72 6f  he select-A coro
2a0be 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61  utine */.  int a
2a0bf 64 64 72 53 65 6c 65 63 74 42 3b 20 20 20 20 20  ddrSelectB;     
2a0c0 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74   /* Address of t
2a0c1 68 65 20 73 65 6c 65 63 74 2d 42 20 63 6f 72 6f  he select-B coro
2a0c2 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72  utine */.  int r
2a0c3 65 67 4f 75 74 41 3b 20 20 20 20 20 20 20 20 20  egOutA;         
2a0c4 20 2f 2a 20 41 64 64 72 65 73 73 20 72 65 67 69   /* Address regi
2a0c5 73 74 65 72 20 66 6f 72 20 74 68 65 20 6f 75 74  ster for the out
2a0c6 70 75 74 2d 41 20 73 75 62 72 6f 75 74 69 6e 65  put-A subroutine
2a0c7 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4f 75 74   */.  int regOut
2a0c8 42 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  B;          /* A
2a0c9 64 64 72 65 73 73 20 72 65 67 69 73 74 65 72 20  ddress register 
2a0ca 66 6f 72 20 74 68 65 20 6f 75 74 70 75 74 2d 42  for the output-B
2a0cb 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20   subroutine */. 
2a0cc 20 69 6e 74 20 61 64 64 72 4f 75 74 41 3b 20 20   int addrOutA;  
2a0cd 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
2a0ce 73 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 2d  s of the output-
2a0cf 41 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a  A subroutine */.
2a0d0 20 20 69 6e 74 20 61 64 64 72 4f 75 74 42 20 3d    int addrOutB =
2a0d1 20 30 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65   0;     /* Addre
2a0d2 73 73 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74  ss of the output
2a0d3 2d 42 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f  -B subroutine */
2a0d4 0a 20 20 69 6e 74 20 61 64 64 72 45 6f 66 41 3b  .  int addrEofA;
2a0d5 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
2a0d6 65 73 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63  ess of the selec
2a0d7 74 2d 41 2d 65 78 68 61 75 73 74 65 64 20 73 75  t-A-exhausted su
2a0d8 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e  broutine */.  in
2a0d9 74 20 61 64 64 72 45 6f 66 42 3b 20 20 20 20 20  t addrEofB;     
2a0da 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
2a0db 66 20 74 68 65 20 73 65 6c 65 63 74 2d 42 2d 65  f the select-B-e
2a0dc 78 68 61 75 73 74 65 64 20 73 75 62 72 6f 75 74  xhausted subrout
2a0dd 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  ine */.  int add
2a0de 72 41 6c 74 42 3b 20 20 20 20 20 20 20 20 20 2f  rAltB;         /
2a0df 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65  * Address of the
2a0e0 20 41 3c 42 20 73 75 62 72 6f 75 74 69 6e 65 20   A<B subroutine 
2a0e1 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 41 65 71  */.  int addrAeq
2a0e2 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64  B;         /* Ad
2a0e3 64 72 65 73 73 20 6f 66 20 74 68 65 20 41 3d 3d  dress of the A==
2a0e4 42 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a  B subroutine */.
2a0e5 20 20 69 6e 74 20 61 64 64 72 41 67 74 42 3b 20    int addrAgtB; 
2a0e6 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
2a0e7 73 73 20 6f 66 20 74 68 65 20 41 3e 42 20 73 75  ss of the A>B su
2a0e8 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e  broutine */.  in
2a0e9 74 20 72 65 67 4c 69 6d 69 74 41 3b 20 20 20 20  t regLimitA;    
2a0ea 20 20 20 20 2f 2a 20 4c 69 6d 69 74 20 72 65 67      /* Limit reg
2a0eb 69 73 74 65 72 20 66 6f 72 20 73 65 6c 65 63 74  ister for select
2a0ec 2d 41 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4c  -A */.  int regL
2a0ed 69 6d 69 74 42 3b 20 20 20 20 20 20 20 20 2f 2a  imitB;        /*
2a0ee 20 4c 69 6d 69 74 20 72 65 67 69 73 74 65 72 20   Limit register 
2a0ef 66 6f 72 20 73 65 6c 65 63 74 2d 41 20 2a 2f 0a  for select-A */.
2a0f0 20 20 69 6e 74 20 72 65 67 50 72 65 76 3b 20 20    int regPrev;  
2a0f1 20 20 20 20 20 20 20 20 2f 2a 20 41 20 72 61 6e          /* A ran
2a0f2 67 65 20 6f 66 20 72 65 67 69 73 74 65 72 73 20  ge of registers 
2a0f3 74 6f 20 68 6f 6c 64 20 70 72 65 76 69 6f 75 73  to hold previous
2a0f4 20 6f 75 74 70 75 74 20 2a 2f 0a 20 20 69 6e 74   output */.  int
2a0f5 20 73 61 76 65 64 4c 69 6d 69 74 3b 20 20 20 20   savedLimit;    
2a0f6 20 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75     /* Saved valu
2a0f7 65 20 6f 66 20 70 2d 3e 69 4c 69 6d 69 74 20 2a  e of p->iLimit *
2a0f8 2f 0a 20 20 69 6e 74 20 73 61 76 65 64 4f 66 66  /.  int savedOff
2a0f9 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 53 61 76  set;      /* Sav
2a0fa 65 64 20 76 61 6c 75 65 20 6f 66 20 70 2d 3e 69  ed value of p->i
2a0fb 4f 66 66 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20  Offset */.  int 
2a0fc 6c 61 62 65 6c 43 6d 70 72 3b 20 20 20 20 20 20  labelCmpr;      
2a0fd 20 20 2f 2a 20 4c 61 62 65 6c 20 66 6f 72 20 74    /* Label for t
2a0fe 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
2a0ff 6d 65 72 67 65 20 61 6c 67 6f 72 69 74 68 6d 20  merge algorithm 
2a100 2a 2f 0a 20 20 69 6e 74 20 6c 61 62 65 6c 45 6e  */.  int labelEn
2a101 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61  d;         /* La
2a102 62 65 6c 20 66 6f 72 20 74 68 65 20 65 6e 64 20  bel for the end 
2a103 6f 66 20 74 68 65 20 6f 76 65 72 61 6c 6c 20 53  of the overall S
2a104 45 4c 45 43 54 20 73 74 6d 74 20 2a 2f 0a 20 20  ELECT stmt */.  
2a105 69 6e 74 20 6a 31 3b 20 20 20 20 20 20 20 20 20  int j1;         
2a106 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 69 6e        /* Jump in
2a107 73 74 72 75 63 74 69 6f 6e 73 20 74 68 61 74 20  structions that 
2a108 67 65 74 20 72 65 74 61 72 67 65 74 74 65 64 20  get retargetted 
2a109 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20  */.  int op;    
2a10a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
2a10b 65 20 6f 66 20 54 4b 5f 41 4c 4c 2c 20 54 4b 5f  e of TK_ALL, TK_
2a10c 55 4e 49 4f 4e 2c 20 54 4b 5f 45 58 43 45 50 54  UNION, TK_EXCEPT
2a10d 2c 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 2a  , TK_INTERSECT *
2a10e 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65  /.  KeyInfo *pKe
2a10f 79 44 75 70 20 3d 20 30 3b 20 2f 2a 20 43 6f 6d  yDup = 0; /* Com
2a110 70 61 72 69 73 6f 6e 20 69 6e 66 6f 72 6d 61 74  parison informat
2a111 69 6f 6e 20 66 6f 72 20 64 75 70 6c 69 63 61 74  ion for duplicat
2a112 65 20 72 65 6d 6f 76 61 6c 20 2a 2f 0a 20 20 4b  e removal */.  K
2a113 65 79 49 6e 66 6f 20 2a 70 4b 65 79 4d 65 72 67  eyInfo *pKeyMerg
2a114 65 3b 20 20 20 2f 2a 20 43 6f 6d 70 61 72 69 73  e;   /* Comparis
2a115 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  on information f
2a116 6f 72 20 6d 65 72 67 69 6e 67 20 72 6f 77 73 20  or merging rows 
2a117 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
2a118 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61  ;          /* Da
2a119 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2a11a 6e 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  n */.  ExprList 
2a11b 2a 70 4f 72 64 65 72 42 79 3b 20 20 20 2f 2a 20  *pOrderBy;   /* 
2a11c 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  The ORDER BY cla
2a11d 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 72  use */.  int nOr
2a11e 64 65 72 42 79 3b 20 20 20 20 20 20 20 20 20 2f  derBy;         /
2a11f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d  * Number of term
2a120 73 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42  s in the ORDER B
2a121 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e  Y clause */.  in
2a122 74 20 2a 61 50 65 72 6d 75 74 65 3b 20 20 20 20  t *aPermute;    
2a123 20 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66      /* Mapping f
2a124 72 6f 6d 20 4f 52 44 45 52 20 42 59 20 74 65 72  rom ORDER BY ter
2a125 6d 73 20 74 6f 20 72 65 73 75 6c 74 20 73 65 74  ms to result set
2a126 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 0a 20 20 61   columns */..  a
2a127 73 73 65 72 74 28 20 70 2d 3e 70 4f 72 64 65 72  ssert( p->pOrder
2a128 42 79 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  By!=0 );.  asser
2a129 74 28 20 70 4b 65 79 44 75 70 3d 3d 30 20 29 3b  t( pKeyDup==0 );
2a12a 20 2f 2a 20 22 4d 61 6e 61 67 65 64 22 20 63 6f   /* "Managed" co
2a12b 64 65 20 6e 65 65 64 73 20 74 68 69 73 2e 20 20  de needs this.  
2a12c 54 69 63 6b 65 74 20 23 33 33 38 32 2e 20 2a 2f  Ticket #3382. */
2a12d 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  .  db = pParse->
2a12e 64 62 3b 0a 20 20 76 20 3d 20 70 50 61 72 73 65  db;.  v = pParse
2a12f 2d 3e 70 56 64 62 65 3b 0a 20 20 61 73 73 65 72  ->pVdbe;.  asser
2a130 74 28 20 76 21 3d 30 20 29 3b 20 20 20 20 20 20  t( v!=0 );      
2a131 20 2f 2a 20 41 6c 72 65 61 64 79 20 74 68 72 6f   /* Already thro
2a132 77 6e 20 74 68 65 20 65 72 72 6f 72 20 69 66 20  wn the error if 
2a133 56 44 42 45 20 61 6c 6c 6f 63 20 66 61 69 6c 65  VDBE alloc faile
2a134 64 20 2a 2f 0a 20 20 6c 61 62 65 6c 45 6e 64 20  d */.  labelEnd 
2a135 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
2a136 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 6c 61 62  eLabel(v);.  lab
2a137 65 6c 43 6d 70 72 20 3d 20 73 71 6c 69 74 65 33  elCmpr = sqlite3
2a138 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
2a139 3b 0a 0a 0a 20 20 2f 2a 20 50 61 74 63 68 20 75  ;...  /* Patch u
2a13a 70 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  p the ORDER BY c
2a13b 6c 61 75 73 65 0a 20 20 2a 2f 0a 20 20 6f 70 20  lause.  */.  op 
2a13c 3d 20 70 2d 3e 6f 70 3b 20 20 0a 20 20 70 50 72  = p->op;  .  pPr
2a13d 69 6f 72 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b  ior = p->pPrior;
2a13e 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f  .  assert( pPrio
2a13f 72 2d 3e 70 4f 72 64 65 72 42 79 3d 3d 30 20 29  r->pOrderBy==0 )
2a140 3b 0a 20 20 70 4f 72 64 65 72 42 79 20 3d 20 70  ;.  pOrderBy = p
2a141 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 61 73  ->pOrderBy;.  as
2a142 73 65 72 74 28 20 70 4f 72 64 65 72 42 79 20 29  sert( pOrderBy )
2a143 3b 0a 20 20 6e 4f 72 64 65 72 42 79 20 3d 20 70  ;.  nOrderBy = p
2a144 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 0a  OrderBy->nExpr;.
2a145 0a 20 20 2f 2a 20 46 6f 72 20 6f 70 65 72 61 74  .  /* For operat
2a146 6f 72 73 20 6f 74 68 65 72 20 74 68 61 6e 20 55  ors other than U
2a147 4e 49 4f 4e 20 41 4c 4c 20 77 65 20 68 61 76 65  NION ALL we have
2a148 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
2a149 61 74 0a 20 20 2a 2a 20 74 68 65 20 4f 52 44 45  at.  ** the ORDE
2a14a 52 20 42 59 20 63 6c 61 75 73 65 20 63 6f 76 65  R BY clause cove
2a14b 72 73 20 65 76 65 72 79 20 74 65 72 6d 20 6f 66  rs every term of
2a14c 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e   the result set.
2a14d 20 20 41 64 64 0a 20 20 2a 2a 20 74 65 72 6d 73    Add.  ** terms
2a14e 20 74 6f 20 74 68 65 20 4f 52 44 45 52 20 42 59   to the ORDER BY
2a14f 20 63 6c 61 75 73 65 20 61 73 20 6e 65 63 65 73   clause as neces
2a150 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  sary..  */.  if(
2a151 20 6f 70 21 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20   op!=TK_ALL ){. 
2a152 20 20 20 66 6f 72 28 69 3d 31 3b 20 64 62 2d 3e     for(i=1; db->
2a153 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20  mallocFailed==0 
2a154 26 26 20 69 3c 3d 70 2d 3e 70 45 4c 69 73 74 2d  && i<=p->pEList-
2a155 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20  >nExpr; i++){.  
2a156 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
2a157 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
2a158 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20  .      for(j=0, 
2a159 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e  pItem=pOrderBy->
2a15a 61 3b 20 6a 3c 6e 4f 72 64 65 72 42 79 3b 20 6a  a; j<nOrderBy; j
2a15b 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  ++, pItem++){.  
2a15c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49        assert( pI
2a15d 74 65 6d 2d 3e 69 43 6f 6c 3e 30 20 29 3b 0a 20  tem->iCol>0 );. 
2a15e 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d         if( pItem
2a15f 2d 3e 69 43 6f 6c 3d 3d 69 20 29 20 62 72 65 61  ->iCol==i ) brea
2a160 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
2a161 20 69 66 28 20 6a 3d 3d 6e 4f 72 64 65 72 42 79   if( j==nOrderBy
2a162 20 29 7b 0a 20 20 20 20 20 20 20 20 45 78 70 72   ){.        Expr
2a163 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33   *pNew = sqlite3
2a164 45 78 70 72 28 64 62 2c 20 54 4b 5f 49 4e 54 45  Expr(db, TK_INTE
2a165 47 45 52 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  GER, 0);.       
2a166 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72   if( pNew==0 ) r
2a167 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
2a168 45 4d 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77  EM;.        pNew
2a169 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 49 6e  ->flags |= EP_In
2a16a 74 56 61 6c 75 65 3b 0a 20 20 20 20 20 20 20 20  tValue;.        
2a16b 70 4e 65 77 2d 3e 75 2e 69 56 61 6c 75 65 20 3d  pNew->u.iValue =
2a16c 20 69 3b 0a 20 20 20 20 20 20 20 20 70 4f 72 64   i;.        pOrd
2a16d 65 72 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78  erBy = sqlite3Ex
2a16e 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
2a16f 72 73 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 70  rse, pOrderBy, p
2a170 4e 65 77 29 3b 0a 20 20 20 20 20 20 20 20 70 4f  New);.        pO
2a171 72 64 65 72 42 79 2d 3e 61 5b 6e 4f 72 64 65 72  rderBy->a[nOrder
2a172 42 79 2b 2b 5d 2e 69 43 6f 6c 20 3d 20 28 75 31  By++].iCol = (u1
2a173 36 29 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  6)i;.      }.   
2a174 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d   }.  }..  /* Com
2a175 70 75 74 65 20 74 68 65 20 63 6f 6d 70 61 72 69  pute the compari
2a176 73 6f 6e 20 70 65 72 6d 75 74 61 74 69 6f 6e 20  son permutation 
2a177 61 6e 64 20 6b 65 79 69 6e 66 6f 20 74 68 61 74  and keyinfo that
2a178 20 69 73 20 75 73 65 64 20 77 69 74 68 0a 20 20   is used with.  
2a179 2a 2a 20 74 68 65 20 70 65 72 6d 75 74 61 74 69  ** the permutati
2a17a 6f 6e 20 75 73 65 64 20 74 6f 20 64 65 74 65 72  on used to deter
2a17b 6d 69 6e 65 20 69 66 20 74 68 65 20 6e 65 78 74  mine if the next
2a17c 0a 20 20 2a 2a 20 72 6f 77 20 6f 66 20 72 65 73  .  ** row of res
2a17d 75 6c 74 73 20 63 6f 6d 65 73 20 66 72 6f 6d 20  ults comes from 
2a17e 73 65 6c 65 63 74 41 20 6f 72 20 73 65 6c 65 63  selectA or selec
2a17f 74 42 2e 20 20 41 6c 73 6f 20 61 64 64 20 65 78  tB.  Also add ex
2a180 70 6c 69 63 69 74 0a 20 20 2a 2a 20 63 6f 6c 6c  plicit.  ** coll
2a181 61 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 4f 52  ations to the OR
2a182 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 74 65  DER BY clause te
2a183 72 6d 73 20 73 6f 20 74 68 61 74 20 77 68 65 6e  rms so that when
2a184 20 74 68 65 20 73 75 62 71 75 65 72 69 65 73 0a   the subqueries.
2a185 20 20 2a 2a 20 74 6f 20 74 68 65 20 72 69 67 68    ** to the righ
2a186 74 20 61 6e 64 20 74 68 65 20 6c 65 66 74 20 61  t and the left a
2a187 72 65 20 65 76 61 6c 75 61 74 65 64 2c 20 74 68  re evaluated, th
2a188 65 79 20 75 73 65 20 74 68 65 20 63 6f 72 72 65  ey use the corre
2a189 63 74 0a 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f  ct.  ** collatio
2a18a 6e 2e 0a 20 20 2a 2f 0a 20 20 61 50 65 72 6d 75  n..  */.  aPermu
2a18b 74 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  te = sqlite3DbMa
2a18c 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65  llocRaw(db, size
2a18d 6f 66 28 69 6e 74 29 2a 6e 4f 72 64 65 72 42 79  of(int)*nOrderBy
2a18e 29 3b 0a 20 20 69 66 28 20 61 50 65 72 6d 75 74  );.  if( aPermut
2a18f 65 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  e ){.    struct 
2a190 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
2a191 49 74 65 6d 3b 0a 20 20 20 20 66 6f 72 28 69 3d  Item;.    for(i=
2a192 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42  0, pItem=pOrderB
2a193 79 2d 3e 61 3b 20 69 3c 6e 4f 72 64 65 72 42 79  y->a; i<nOrderBy
2a194 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
2a195 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2a196 49 74 65 6d 2d 3e 69 43 6f 6c 3e 30 20 20 26 26  Item->iCol>0  &&
2a197 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 3c 3d 70 2d   pItem->iCol<=p-
2a198 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29  >pEList->nExpr )
2a199 3b 0a 20 20 20 20 20 20 61 50 65 72 6d 75 74 65  ;.      aPermute
2a19a 5b 69 5d 20 3d 20 70 49 74 65 6d 2d 3e 69 43 6f  [i] = pItem->iCo
2a19b 6c 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  l - 1;.    }.   
2a19c 20 70 4b 65 79 4d 65 72 67 65 20 3d 0a 20 20 20   pKeyMerge =.   
2a19d 20 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c     sqlite3DbMall
2a19e 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66  ocRaw(db, sizeof
2a19f 28 2a 70 4b 65 79 4d 65 72 67 65 29 2b 6e 4f 72  (*pKeyMerge)+nOr
2a1a0 64 65 72 42 79 2a 28 73 69 7a 65 6f 66 28 43 6f  derBy*(sizeof(Co
2a1a1 6c 6c 53 65 71 2a 29 2b 31 29 29 3b 0a 20 20 20  llSeq*)+1));.   
2a1a2 20 69 66 28 20 70 4b 65 79 4d 65 72 67 65 20 29   if( pKeyMerge )
2a1a3 7b 0a 20 20 20 20 20 20 70 4b 65 79 4d 65 72 67  {.      pKeyMerg
2a1a4 65 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20  e->aSortOrder = 
2a1a5 28 75 38 2a 29 26 70 4b 65 79 4d 65 72 67 65 2d  (u8*)&pKeyMerge-
2a1a6 3e 61 43 6f 6c 6c 5b 6e 4f 72 64 65 72 42 79 5d  >aColl[nOrderBy]
2a1a7 3b 0a 20 20 20 20 20 20 70 4b 65 79 4d 65 72 67  ;.      pKeyMerg
2a1a8 65 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36  e->nField = (u16
2a1a9 29 6e 4f 72 64 65 72 42 79 3b 0a 20 20 20 20 20  )nOrderBy;.     
2a1aa 20 70 4b 65 79 4d 65 72 67 65 2d 3e 65 6e 63 20   pKeyMerge->enc 
2a1ab 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 20 20 20  = ENC(db);.     
2a1ac 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 72 64   for(i=0; i<nOrd
2a1ad 65 72 42 79 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  erBy; i++){.    
2a1ae 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
2a1af 6c 6c 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72  ll;.        Expr
2a1b0 20 2a 70 54 65 72 6d 20 3d 20 70 4f 72 64 65 72   *pTerm = pOrder
2a1b1 42 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a  By->a[i].pExpr;.
2a1b2 20 20 20 20 20 20 20 20 69 66 28 20 70 54 65 72          if( pTer
2a1b3 6d 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78  m->flags & EP_Ex
2a1b4 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20  pCollate ){.    
2a1b5 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 54        pColl = pT
2a1b6 65 72 6d 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20  erm->pColl;.    
2a1b7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2a1b8 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 6d 75 6c       pColl = mul
2a1b9 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28  tiSelectCollSeq(
2a1ba 70 50 61 72 73 65 2c 20 70 2c 20 61 50 65 72 6d  pParse, p, aPerm
2a1bb 75 74 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 20  ute[i]);.       
2a1bc 20 20 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20     pTerm->flags 
2a1bd 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65  |= EP_ExpCollate
2a1be 3b 0a 20 20 20 20 20 20 20 20 20 20 70 54 65 72  ;.          pTer
2a1bf 6d 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c  m->pColl = pColl
2a1c0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2a1c1 20 20 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e 61      pKeyMerge->a
2a1c2 43 6f 6c 6c 5b 69 5d 20 3d 20 70 43 6f 6c 6c 3b  Coll[i] = pColl;
2a1c3 0a 20 20 20 20 20 20 20 20 70 4b 65 79 4d 65 72  .        pKeyMer
2a1c4 67 65 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69  ge->aSortOrder[i
2a1c5 5d 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b  ] = pOrderBy->a[
2a1c6 69 5d 2e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20  i].sortOrder;.  
2a1c7 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
2a1c8 6c 73 65 7b 0a 20 20 20 20 70 4b 65 79 4d 65 72  lse{.    pKeyMer
2a1c9 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  ge = 0;.  }..  /
2a1ca 2a 20 52 65 61 74 74 61 63 68 20 74 68 65 20 4f  * Reattach the O
2a1cb 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 74  RDER BY clause t
2a1cc 6f 20 74 68 65 20 71 75 65 72 79 2e 0a 20 20 2a  o the query..  *
2a1cd 2f 0a 20 20 70 2d 3e 70 4f 72 64 65 72 42 79 20  /.  p->pOrderBy 
2a1ce 3d 20 70 4f 72 64 65 72 42 79 3b 0a 20 20 70 50  = pOrderBy;.  pP
2a1cf 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79 20 3d  rior->pOrderBy =
2a1d0 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2a1d1 44 75 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  Dup(pParse->db, 
2a1d2 70 4f 72 64 65 72 42 79 2c 20 30 29 3b 0a 0a 20  pOrderBy, 0);.. 
2a1d3 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 72   /* Allocate a r
2a1d4 61 6e 67 65 20 6f 66 20 74 65 6d 70 6f 72 61 72  ange of temporar
2a1d5 79 20 72 65 67 69 73 74 65 72 73 20 61 6e 64 20  y registers and 
2a1d6 74 68 65 20 4b 65 79 49 6e 66 6f 20 6e 65 65 64  the KeyInfo need
2a1d7 65 64 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20  ed.  ** for the 
2a1d8 6c 6f 67 69 63 20 74 68 61 74 20 72 65 6d 6f 76  logic that remov
2a1d9 65 73 20 64 75 70 6c 69 63 61 74 65 20 72 65 73  es duplicate res
2a1da 75 6c 74 20 72 6f 77 73 20 77 68 65 6e 20 74 68  ult rows when th
2a1db 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72 20  e.  ** operator 
2a1dc 69 73 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54  is UNION, EXCEPT
2a1dd 2c 20 6f 72 20 49 4e 54 45 52 53 45 43 54 20 28  , or INTERSECT (
2a1de 62 75 74 20 6e 6f 74 20 55 4e 49 4f 4e 20 41 4c  but not UNION AL
2a1df 4c 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f  L)..  */.  if( o
2a1e0 70 3d 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20  p==TK_ALL ){.   
2a1e1 20 72 65 67 50 72 65 76 20 3d 20 30 3b 0a 20 20   regPrev = 0;.  
2a1e2 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6e  }else{.    int n
2a1e3 45 78 70 72 20 3d 20 70 2d 3e 70 45 4c 69 73 74  Expr = p->pEList
2a1e4 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 61 73 73  ->nExpr;.    ass
2a1e5 65 72 74 28 20 6e 4f 72 64 65 72 42 79 3e 3d 6e  ert( nOrderBy>=n
2a1e6 45 78 70 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  Expr || db->mall
2a1e7 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20  ocFailed );.    
2a1e8 72 65 67 50 72 65 76 20 3d 20 73 71 6c 69 74 65  regPrev = sqlite
2a1e9 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50  3GetTempRange(pP
2a1ea 61 72 73 65 2c 20 6e 45 78 70 72 2b 31 29 3b 0a  arse, nExpr+1);.
2a1eb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2a1ec 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
2a1ed 67 65 72 2c 20 30 2c 20 72 65 67 50 72 65 76 29  ger, 0, regPrev)
2a1ee 3b 0a 20 20 20 20 70 4b 65 79 44 75 70 20 3d 20  ;.    pKeyDup = 
2a1ef 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
2a1f0 65 72 6f 28 64 62 2c 0a 20 20 20 20 20 20 20 20  ero(db,.        
2a1f1 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66            sizeof
2a1f2 28 2a 70 4b 65 79 44 75 70 29 20 2b 20 6e 45 78  (*pKeyDup) + nEx
2a1f3 70 72 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53  pr*(sizeof(CollS
2a1f4 65 71 2a 29 2b 31 29 20 29 3b 0a 20 20 20 20 69  eq*)+1) );.    i
2a1f5 66 28 20 70 4b 65 79 44 75 70 20 29 7b 0a 20 20  f( pKeyDup ){.  
2a1f6 20 20 20 20 70 4b 65 79 44 75 70 2d 3e 61 53 6f      pKeyDup->aSo
2a1f7 72 74 4f 72 64 65 72 20 3d 20 28 75 38 2a 29 26  rtOrder = (u8*)&
2a1f8 70 4b 65 79 44 75 70 2d 3e 61 43 6f 6c 6c 5b 6e  pKeyDup->aColl[n
2a1f9 45 78 70 72 5d 3b 0a 20 20 20 20 20 20 70 4b 65  Expr];.      pKe
2a1fa 79 44 75 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 28  yDup->nField = (
2a1fb 75 31 36 29 6e 45 78 70 72 3b 0a 20 20 20 20 20  u16)nExpr;.     
2a1fc 20 70 4b 65 79 44 75 70 2d 3e 65 6e 63 20 3d 20   pKeyDup->enc = 
2a1fd 45 4e 43 28 64 62 29 3b 0a 20 20 20 20 20 20 66  ENC(db);.      f
2a1fe 6f 72 28 69 3d 30 3b 20 69 3c 6e 45 78 70 72 3b  or(i=0; i<nExpr;
2a1ff 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70   i++){.        p
2a200 4b 65 79 44 75 70 2d 3e 61 43 6f 6c 6c 5b 69 5d  KeyDup->aColl[i]
2a201 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63 74 43 6f   = multiSelectCo
2a202 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 2c  llSeq(pParse, p,
2a203 20 69 29 3b 0a 20 20 20 20 20 20 20 20 70 4b 65   i);.        pKe
2a204 79 44 75 70 2d 3e 61 53 6f 72 74 4f 72 64 65 72  yDup->aSortOrder
2a205 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  [i] = 0;.      }
2a206 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 0a 20 20 2f  .    }.  }. .  /
2a207 2a 20 53 65 70 61 72 61 74 65 20 74 68 65 20 6c  * Separate the l
2a208 65 66 74 20 61 6e 64 20 74 68 65 20 72 69 67 68  eft and the righ
2a209 74 20 71 75 65 72 79 20 66 72 6f 6d 20 6f 6e 65  t query from one
2a20a 20 61 6e 6f 74 68 65 72 0a 20 20 2a 2f 0a 20 20   another.  */.  
2a20b 70 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20  p->pPrior = 0;. 
2a20c 20 70 50 72 69 6f 72 2d 3e 70 52 69 67 68 74 6d   pPrior->pRightm
2a20d 6f 73 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  ost = 0;.  sqlit
2a20e 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72  e3ResolveOrderGr
2a20f 6f 75 70 42 79 28 70 50 61 72 73 65 2c 20 70 2c  oupBy(pParse, p,
2a210 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 22 4f   p->pOrderBy, "O
2a211 52 44 45 52 22 29 3b 0a 20 20 69 66 28 20 70 50  RDER");.  if( pP
2a212 72 69 6f 72 2d 3e 70 50 72 69 6f 72 3d 3d 30 20  rior->pPrior==0 
2a213 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65  ){.    sqlite3Re
2a214 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42  solveOrderGroupB
2a215 79 28 70 50 61 72 73 65 2c 20 70 50 72 69 6f 72  y(pParse, pPrior
2a216 2c 20 70 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72  , pPrior->pOrder
2a217 42 79 2c 20 22 4f 52 44 45 52 22 29 3b 0a 20 20  By, "ORDER");.  
2a218 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20  }..  /* Compute 
2a219 74 68 65 20 6c 69 6d 69 74 20 72 65 67 69 73 74  the limit regist
2a21a 65 72 73 20 2a 2f 0a 20 20 63 6f 6d 70 75 74 65  ers */.  compute
2a21b 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73 28 70  LimitRegisters(p
2a21c 50 61 72 73 65 2c 20 70 2c 20 6c 61 62 65 6c 45  Parse, p, labelE
2a21d 6e 64 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69 4c  nd);.  if( p->iL
2a21e 69 6d 69 74 20 26 26 20 6f 70 3d 3d 54 4b 5f 41  imit && op==TK_A
2a21f 4c 4c 20 29 7b 0a 20 20 20 20 72 65 67 4c 69 6d  LL ){.    regLim
2a220 69 74 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  itA = ++pParse->
2a221 6e 4d 65 6d 3b 0a 20 20 20 20 72 65 67 4c 69 6d  nMem;.    regLim
2a222 69 74 42 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  itB = ++pParse->
2a223 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65  nMem;.    sqlite
2a224 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2a225 50 5f 43 6f 70 79 2c 20 70 2d 3e 69 4f 66 66 73  P_Copy, p->iOffs
2a226 65 74 20 3f 20 70 2d 3e 69 4f 66 66 73 65 74 2b  et ? p->iOffset+
2a227 31 20 3a 20 70 2d 3e 69 4c 69 6d 69 74 2c 0a 20  1 : p->iLimit,. 
2a228 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a229 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a22a 20 72 65 67 4c 69 6d 69 74 41 29 3b 0a 20 20 20   regLimitA);.   
2a22b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a22c 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 72  p2(v, OP_Copy, r
2a22d 65 67 4c 69 6d 69 74 41 2c 20 72 65 67 4c 69 6d  egLimitA, regLim
2a22e 69 74 42 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  itB);.  }else{. 
2a22f 20 20 20 72 65 67 4c 69 6d 69 74 41 20 3d 20 72     regLimitA = r
2a230 65 67 4c 69 6d 69 74 42 20 3d 20 30 3b 0a 20 20  egLimitB = 0;.  
2a231 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44  }.  sqlite3ExprD
2a232 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4c 69  elete(db, p->pLi
2a233 6d 69 74 29 3b 0a 20 20 70 2d 3e 70 4c 69 6d 69  mit);.  p->pLimi
2a234 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  t = 0;.  sqlite3
2a235 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
2a236 2d 3e 70 4f 66 66 73 65 74 29 3b 0a 20 20 70 2d  ->pOffset);.  p-
2a237 3e 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 0a 20  >pOffset = 0;.. 
2a238 20 72 65 67 41 64 64 72 41 20 3d 20 2b 2b 70 50   regAddrA = ++pP
2a239 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65  arse->nMem;.  re
2a23a 67 45 6f 66 41 20 3d 20 2b 2b 70 50 61 72 73 65  gEofA = ++pParse
2a23b 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 41 64 64  ->nMem;.  regAdd
2a23c 72 42 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  rB = ++pParse->n
2a23d 4d 65 6d 3b 0a 20 20 72 65 67 45 6f 66 42 20 3d  Mem;.  regEofB =
2a23e 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2a23f 0a 20 20 72 65 67 4f 75 74 41 20 3d 20 2b 2b 70  .  regOutA = ++p
2a240 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72  Parse->nMem;.  r
2a241 65 67 4f 75 74 42 20 3d 20 2b 2b 70 50 61 72 73  egOutB = ++pPars
2a242 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 73 71 6c 69 74  e->nMem;.  sqlit
2a243 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74  e3SelectDestInit
2a244 28 26 64 65 73 74 41 2c 20 53 52 54 5f 43 6f 72  (&destA, SRT_Cor
2a245 6f 75 74 69 6e 65 2c 20 72 65 67 41 64 64 72 41  outine, regAddrA
2a246 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  );.  sqlite3Sele
2a247 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74  ctDestInit(&dest
2a248 42 2c 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65  B, SRT_Coroutine
2a249 2c 20 72 65 67 41 64 64 72 42 29 3b 0a 0a 20 20  , regAddrB);..  
2a24a 2f 2a 20 4a 75 6d 70 20 70 61 73 74 20 74 68 65  /* Jump past the
2a24b 20 76 61 72 69 6f 75 73 20 73 75 62 72 6f 75 74   various subrout
2a24c 69 6e 65 73 20 61 6e 64 20 63 6f 72 6f 75 74 69  ines and corouti
2a24d 6e 65 73 20 74 6f 20 74 68 65 20 6d 61 69 6e 0a  nes to the main.
2a24e 20 20 2a 2a 20 6d 65 72 67 65 20 6c 6f 6f 70 0a    ** merge loop.
2a24f 20 20 2a 2f 0a 20 20 6a 31 20 3d 20 73 71 6c 69    */.  j1 = sqli
2a250 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c  te3VdbeAddOp0(v,
2a251 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20 20 61 64 64   OP_Goto);.  add
2a252 72 53 65 6c 65 63 74 41 20 3d 20 73 71 6c 69 74  rSelectA = sqlit
2a253 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
2a254 72 28 76 29 3b 0a 0a 0a 20 20 2f 2a 20 47 65 6e  r(v);...  /* Gen
2a255 65 72 61 74 65 20 61 20 63 6f 72 6f 75 74 69 6e  erate a coroutin
2a256 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74 68  e to evaluate th
2a257 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
2a258 6e 74 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 6c  nt to the.  ** l
2a259 65 66 74 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f  eft of the compo
2a25a 75 6e 64 20 6f 70 65 72 61 74 6f 72 20 2d 20 74  und operator - t
2a25b 68 65 20 22 41 22 20 73 65 6c 65 63 74 2e 0a 20  he "A" select.. 
2a25c 20 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f   */.  VdbeNoopCo
2a25d 6d 6d 65 6e 74 28 28 76 2c 20 22 42 65 67 69 6e  mment((v, "Begin
2a25e 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20 6c   coroutine for l
2a25f 65 66 74 20 53 45 4c 45 43 54 22 29 29 3b 0a 20  eft SELECT"));. 
2a260 20 70 50 72 69 6f 72 2d 3e 69 4c 69 6d 69 74 20   pPrior->iLimit 
2a261 3d 20 72 65 67 4c 69 6d 69 74 41 3b 0a 20 20 73  = regLimitA;.  s
2a262 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61  qlite3Select(pPa
2a263 72 73 65 2c 20 70 50 72 69 6f 72 2c 20 26 64 65  rse, pPrior, &de
2a264 73 74 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  stA);.  sqlite3V
2a265 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2a266 49 6e 74 65 67 65 72 2c 20 31 2c 20 72 65 67 45  Integer, 1, regE
2a267 6f 66 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  ofA);.  sqlite3V
2a268 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2a269 59 69 65 6c 64 2c 20 72 65 67 41 64 64 72 41 29  Yield, regAddrA)
2a26a 3b 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d  ;.  VdbeNoopComm
2a26b 65 6e 74 28 28 76 2c 20 22 45 6e 64 20 63 6f 72  ent((v, "End cor
2a26c 6f 75 74 69 6e 65 20 66 6f 72 20 6c 65 66 74 20  outine for left 
2a26d 53 45 4c 45 43 54 22 29 29 3b 0a 0a 20 20 2f 2a  SELECT"));..  /*
2a26e 20 47 65 6e 65 72 61 74 65 20 61 20 63 6f 72 6f   Generate a coro
2a26f 75 74 69 6e 65 20 74 6f 20 65 76 61 6c 75 61 74  utine to evaluat
2a270 65 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  e the SELECT sta
2a271 74 65 6d 65 6e 74 20 6f 6e 20 0a 20 20 2a 2a 20  tement on .  ** 
2a272 74 68 65 20 72 69 67 68 74 20 2d 20 74 68 65 20  the right - the 
2a273 22 42 22 20 73 65 6c 65 63 74 0a 20 20 2a 2f 0a  "B" select.  */.
2a274 20 20 61 64 64 72 53 65 6c 65 63 74 42 20 3d 20    addrSelectB = 
2a275 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
2a276 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 56 64 62  ntAddr(v);.  Vdb
2a277 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c  eNoopComment((v,
2a278 20 22 42 65 67 69 6e 20 63 6f 72 6f 75 74 69 6e   "Begin coroutin
2a279 65 20 66 6f 72 20 72 69 67 68 74 20 53 45 4c 45  e for right SELE
2a27a 43 54 22 29 29 3b 0a 20 20 73 61 76 65 64 4c 69  CT"));.  savedLi
2a27b 6d 69 74 20 3d 20 70 2d 3e 69 4c 69 6d 69 74 3b  mit = p->iLimit;
2a27c 0a 20 20 73 61 76 65 64 4f 66 66 73 65 74 20 3d  .  savedOffset =
2a27d 20 70 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 70   p->iOffset;.  p
2a27e 2d 3e 69 4c 69 6d 69 74 20 3d 20 72 65 67 4c 69  ->iLimit = regLi
2a27f 6d 69 74 42 3b 0a 20 20 70 2d 3e 69 4f 66 66 73  mitB;.  p->iOffs
2a280 65 74 20 3d 20 30 3b 20 20 0a 20 20 73 71 6c 69  et = 0;  .  sqli
2a281 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
2a282 2c 20 70 2c 20 26 64 65 73 74 42 29 3b 0a 20 20  , p, &destB);.  
2a283 70 2d 3e 69 4c 69 6d 69 74 20 3d 20 73 61 76 65  p->iLimit = save
2a284 64 4c 69 6d 69 74 3b 0a 20 20 70 2d 3e 69 4f 66  dLimit;.  p->iOf
2a285 66 73 65 74 20 3d 20 73 61 76 65 64 4f 66 66 73  fset = savedOffs
2a286 65 74 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  et;.  sqlite3Vdb
2a287 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
2a288 74 65 67 65 72 2c 20 31 2c 20 72 65 67 45 6f 66  teger, 1, regEof
2a289 42 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  B);.  sqlite3Vdb
2a28a 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69  eAddOp1(v, OP_Yi
2a28b 65 6c 64 2c 20 72 65 67 41 64 64 72 42 29 3b 0a  eld, regAddrB);.
2a28c 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e    VdbeNoopCommen
2a28d 74 28 28 76 2c 20 22 45 6e 64 20 63 6f 72 6f 75  t((v, "End corou
2a28e 74 69 6e 65 20 66 6f 72 20 72 69 67 68 74 20 53  tine for right S
2a28f 45 4c 45 43 54 22 29 29 3b 0a 0a 20 20 2f 2a 20  ELECT"));..  /* 
2a290 47 65 6e 65 72 61 74 65 20 61 20 73 75 62 72 6f  Generate a subro
2a291 75 74 69 6e 65 20 74 68 61 74 20 6f 75 74 70 75  utine that outpu
2a292 74 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  ts the current r
2a293 6f 77 20 6f 66 20 74 68 65 20 41 0a 20 20 2a 2a  ow of the A.  **
2a294 20 73 65 6c 65 63 74 20 61 73 20 74 68 65 20 6e   select as the n
2a295 65 78 74 20 6f 75 74 70 75 74 20 72 6f 77 20 6f  ext output row o
2a296 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73  f the compound s
2a297 65 6c 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 56 64  elect..  */.  Vd
2a298 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76  beNoopComment((v
2a299 2c 20 22 4f 75 74 70 75 74 20 72 6f 75 74 69 6e  , "Output routin
2a29a 65 20 66 6f 72 20 41 22 29 29 3b 0a 20 20 61 64  e for A"));.  ad
2a29b 64 72 4f 75 74 41 20 3d 20 67 65 6e 65 72 61 74  drOutA = generat
2a29c 65 4f 75 74 70 75 74 53 75 62 72 6f 75 74 69 6e  eOutputSubroutin
2a29d 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20  e(pParse,.      
2a29e 20 20 20 20 20 20 20 20 20 20 20 70 2c 20 26 64             p, &d
2a29f 65 73 74 41 2c 20 70 44 65 73 74 2c 20 72 65 67  estA, pDest, reg
2a2a0 4f 75 74 41 2c 0a 20 20 20 20 20 20 20 20 20 20  OutA,.          
2a2a1 20 20 20 20 20 20 20 72 65 67 50 72 65 76 2c 20         regPrev, 
2a2a2 70 4b 65 79 44 75 70 2c 20 50 34 5f 4b 45 59 49  pKeyDup, P4_KEYI
2a2a3 4e 46 4f 5f 48 41 4e 44 4f 46 46 2c 20 6c 61 62  NFO_HANDOFF, lab
2a2a4 65 6c 45 6e 64 29 3b 0a 20 20 0a 20 20 2f 2a 20  elEnd);.  .  /* 
2a2a5 47 65 6e 65 72 61 74 65 20 61 20 73 75 62 72 6f  Generate a subro
2a2a6 75 74 69 6e 65 20 74 68 61 74 20 6f 75 74 70 75  utine that outpu
2a2a7 74 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  ts the current r
2a2a8 6f 77 20 6f 66 20 74 68 65 20 42 0a 20 20 2a 2a  ow of the B.  **
2a2a9 20 73 65 6c 65 63 74 20 61 73 20 74 68 65 20 6e   select as the n
2a2aa 65 78 74 20 6f 75 74 70 75 74 20 72 6f 77 20 6f  ext output row o
2a2ab 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73  f the compound s
2a2ac 65 6c 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 69 66  elect..  */.  if
2a2ad 28 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 7c 7c 20  ( op==TK_ALL || 
2a2ae 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e 20 29 7b 0a  op==TK_UNION ){.
2a2af 20 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d      VdbeNoopComm
2a2b0 65 6e 74 28 28 76 2c 20 22 4f 75 74 70 75 74 20  ent((v, "Output 
2a2b1 72 6f 75 74 69 6e 65 20 66 6f 72 20 42 22 29 29  routine for B"))
2a2b2 3b 0a 20 20 20 20 61 64 64 72 4f 75 74 42 20 3d  ;.    addrOutB =
2a2b3 20 67 65 6e 65 72 61 74 65 4f 75 74 70 75 74 53   generateOutputS
2a2b4 75 62 72 6f 75 74 69 6e 65 28 70 50 61 72 73 65  ubroutine(pParse
2a2b5 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2a2b6 20 20 20 70 2c 20 26 64 65 73 74 42 2c 20 70 44     p, &destB, pD
2a2b7 65 73 74 2c 20 72 65 67 4f 75 74 42 2c 0a 20 20  est, regOutB,.  
2a2b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
2a2b9 65 67 50 72 65 76 2c 20 70 4b 65 79 44 75 70 2c  egPrev, pKeyDup,
2a2ba 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 41 54   P4_KEYINFO_STAT
2a2bb 49 43 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20  IC, labelEnd);. 
2a2bc 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74   }..  /* Generat
2a2bd 65 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74  e a subroutine t
2a2be 6f 20 72 75 6e 20 77 68 65 6e 20 74 68 65 20 72  o run when the r
2a2bf 65 73 75 6c 74 73 20 66 72 6f 6d 20 73 65 6c 65  esults from sele
2a2c0 63 74 20 41 0a 20 20 2a 2a 20 61 72 65 20 65 78  ct A.  ** are ex
2a2c1 68 61 75 73 74 65 64 20 61 6e 64 20 6f 6e 6c 79  hausted and only
2a2c2 20 64 61 74 61 20 69 6e 20 73 65 6c 65 63 74 20   data in select 
2a2c3 42 20 72 65 6d 61 69 6e 73 2e 0a 20 20 2a 2f 0a  B remains..  */.
2a2c4 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e    VdbeNoopCommen
2a2c5 74 28 28 76 2c 20 22 65 6f 66 2d 41 20 73 75 62  t((v, "eof-A sub
2a2c6 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20 69 66  routine"));.  if
2a2c7 28 20 6f 70 3d 3d 54 4b 5f 45 58 43 45 50 54 20  ( op==TK_EXCEPT 
2a2c8 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53  || op==TK_INTERS
2a2c9 45 43 54 20 29 7b 0a 20 20 20 20 61 64 64 72 45  ECT ){.    addrE
2a2ca 6f 66 41 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ofA = sqlite3Vdb
2a2cb 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
2a2cc 74 6f 2c 20 30 2c 20 6c 61 62 65 6c 45 6e 64 29  to, 0, labelEnd)
2a2cd 3b 0a 20 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20  ;.  }else{  .   
2a2ce 20 61 64 64 72 45 6f 66 41 20 3d 20 73 71 6c 69   addrEofA = sqli
2a2cf 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2a2d0 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 42 2c   OP_If, regEofB,
2a2d1 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20 20 20   labelEnd);.    
2a2d2 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a2d3 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72  2(v, OP_Gosub, r
2a2d4 65 67 4f 75 74 42 2c 20 61 64 64 72 4f 75 74 42  egOutB, addrOutB
2a2d5 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2a2d6 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59  beAddOp1(v, OP_Y
2a2d7 69 65 6c 64 2c 20 72 65 67 41 64 64 72 42 29 3b  ield, regAddrB);
2a2d8 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2a2d9 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
2a2da 6f 2c 20 30 2c 20 61 64 64 72 45 6f 66 41 29 3b  o, 0, addrEofA);
2a2db 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  .  }..  /* Gener
2a2dc 61 74 65 20 61 20 73 75 62 72 6f 75 74 69 6e 65  ate a subroutine
2a2dd 20 74 6f 20 72 75 6e 20 77 68 65 6e 20 74 68 65   to run when the
2a2de 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 73 65   results from se
2a2df 6c 65 63 74 20 42 0a 20 20 2a 2a 20 61 72 65 20  lect B.  ** are 
2a2e0 65 78 68 61 75 73 74 65 64 20 61 6e 64 20 6f 6e  exhausted and on
2a2e1 6c 79 20 64 61 74 61 20 69 6e 20 73 65 6c 65 63  ly data in selec
2a2e2 74 20 41 20 72 65 6d 61 69 6e 73 2e 0a 20 20 2a  t A remains..  *
2a2e3 2f 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49  /.  if( op==TK_I
2a2e4 4e 54 45 52 53 45 43 54 20 29 7b 0a 20 20 20 20  NTERSECT ){.    
2a2e5 61 64 64 72 45 6f 66 42 20 3d 20 61 64 64 72 45  addrEofB = addrE
2a2e6 6f 66 41 3b 0a 20 20 7d 65 6c 73 65 7b 20 20 0a  ofA;.  }else{  .
2a2e7 20 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d      VdbeNoopComm
2a2e8 65 6e 74 28 28 76 2c 20 22 65 6f 66 2d 42 20 73  ent((v, "eof-B s
2a2e9 75 62 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20  ubroutine"));.  
2a2ea 20 20 61 64 64 72 45 6f 66 42 20 3d 20 73 71 6c    addrEofB = sql
2a2eb 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2a2ec 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 41  , OP_If, regEofA
2a2ed 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20 20  , labelEnd);.   
2a2ee 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a2ef 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20  p2(v, OP_Gosub, 
2a2f0 72 65 67 4f 75 74 41 2c 20 61 64 64 72 4f 75 74  regOutA, addrOut
2a2f1 41 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  A);.    sqlite3V
2a2f2 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2a2f3 59 69 65 6c 64 2c 20 72 65 67 41 64 64 72 41 29  Yield, regAddrA)
2a2f4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2a2f5 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
2a2f6 74 6f 2c 20 30 2c 20 61 64 64 72 45 6f 66 42 29  to, 0, addrEofB)
2a2f7 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65  ;.  }..  /* Gene
2a2f8 72 61 74 65 20 63 6f 64 65 20 74 6f 20 68 61 6e  rate code to han
2a2f9 64 6c 65 20 74 68 65 20 63 61 73 65 20 6f 66 20  dle the case of 
2a2fa 41 3c 42 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e  A<B.  */.  VdbeN
2a2fb 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  oopComment((v, "
2a2fc 41 2d 6c 74 2d 42 20 73 75 62 72 6f 75 74 69 6e  A-lt-B subroutin
2a2fd 65 22 29 29 3b 0a 20 20 61 64 64 72 41 6c 74 42  e"));.  addrAltB
2a2fe 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2a2ff 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62  dOp2(v, OP_Gosub
2a300 2c 20 72 65 67 4f 75 74 41 2c 20 61 64 64 72 4f  , regOutA, addrO
2a301 75 74 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  utA);.  sqlite3V
2a302 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2a303 59 69 65 6c 64 2c 20 72 65 67 41 64 64 72 41 29  Yield, regAddrA)
2a304 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
2a305 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20  ddOp2(v, OP_If, 
2a306 72 65 67 45 6f 66 41 2c 20 61 64 64 72 45 6f 66  regEofA, addrEof
2a307 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  A);.  sqlite3Vdb
2a308 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
2a309 74 6f 2c 20 30 2c 20 6c 61 62 65 6c 43 6d 70 72  to, 0, labelCmpr
2a30a 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74  );..  /* Generat
2a30b 65 20 63 6f 64 65 20 74 6f 20 68 61 6e 64 6c 65  e code to handle
2a30c 20 74 68 65 20 63 61 73 65 20 6f 66 20 41 3d 3d   the case of A==
2a30d 42 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70 3d  B.  */.  if( op=
2a30e 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 61  =TK_ALL ){.    a
2a30f 64 64 72 41 65 71 42 20 3d 20 61 64 64 72 41 6c  ddrAeqB = addrAl
2a310 74 42 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  tB;.  }else if( 
2a311 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53 45 43 54  op==TK_INTERSECT
2a312 20 29 7b 0a 20 20 20 20 61 64 64 72 41 65 71 42   ){.    addrAeqB
2a313 20 3d 20 61 64 64 72 41 6c 74 42 3b 0a 20 20 20   = addrAltB;.   
2a314 20 61 64 64 72 41 6c 74 42 2b 2b 3b 0a 20 20 7d   addrAltB++;.  }
2a315 65 6c 73 65 7b 0a 20 20 20 20 56 64 62 65 4e 6f  else{.    VdbeNo
2a316 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 41  opComment((v, "A
2a317 2d 65 71 2d 42 20 73 75 62 72 6f 75 74 69 6e 65  -eq-B subroutine
2a318 22 29 29 3b 0a 20 20 20 20 61 64 64 72 41 65 71  "));.    addrAeq
2a319 42 20 3d 0a 20 20 20 20 73 71 6c 69 74 65 33 56  B =.    sqlite3V
2a31a 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2a31b 59 69 65 6c 64 2c 20 72 65 67 41 64 64 72 41 29  Yield, regAddrA)
2a31c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2a31d 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66  eAddOp2(v, OP_If
2a31e 2c 20 72 65 67 45 6f 66 41 2c 20 61 64 64 72 45  , regEofA, addrE
2a31f 6f 66 41 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ofA);.    sqlite
2a320 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2a321 50 5f 47 6f 74 6f 2c 20 30 2c 20 6c 61 62 65 6c  P_Goto, 0, label
2a322 43 6d 70 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  Cmpr);.  }..  /*
2a323 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
2a324 6f 20 68 61 6e 64 6c 65 20 74 68 65 20 63 61 73  o handle the cas
2a325 65 20 6f 66 20 41 3e 42 0a 20 20 2a 2f 0a 20 20  e of A>B.  */.  
2a326 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28  VdbeNoopComment(
2a327 28 76 2c 20 22 41 2d 67 74 2d 42 20 73 75 62 72  (v, "A-gt-B subr
2a328 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20 61 64 64  outine"));.  add
2a329 72 41 67 74 42 20 3d 20 73 71 6c 69 74 65 33 56  rAgtB = sqlite3V
2a32a 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
2a32b 29 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  );.  if( op==TK_
2a32c 41 4c 4c 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 4e  ALL || op==TK_UN
2a32d 49 4f 4e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ION ){.    sqlit
2a32e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2a32f 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74  OP_Gosub, regOut
2a330 42 2c 20 61 64 64 72 4f 75 74 42 29 3b 0a 20 20  B, addrOutB);.  
2a331 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  }.  sqlite3VdbeA
2a332 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c  ddOp1(v, OP_Yiel
2a333 64 2c 20 72 65 67 41 64 64 72 42 29 3b 0a 20 20  d, regAddrB);.  
2a334 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a335 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45  2(v, OP_If, regE
2a336 6f 66 42 2c 20 61 64 64 72 45 6f 66 42 29 3b 0a  ofB, addrEofB);.
2a337 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2a338 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
2a339 30 2c 20 6c 61 62 65 6c 43 6d 70 72 29 3b 0a 0a  0, labelCmpr);..
2a33a 20 20 2f 2a 20 54 68 69 73 20 63 6f 64 65 20 72    /* This code r
2a33b 75 6e 73 20 6f 6e 63 65 20 74 6f 20 69 6e 69 74  uns once to init
2a33c 69 61 6c 69 7a 65 20 65 76 65 72 79 74 68 69 6e  ialize everythin
2a33d 67 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  g..  */.  sqlite
2a33e 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2a33f 20 6a 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56   j1);.  sqlite3V
2a340 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2a341 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 45  Integer, 0, regE
2a342 6f 66 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  ofA);.  sqlite3V
2a343 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2a344 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 45  Integer, 0, regE
2a345 6f 66 42 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  ofB);.  sqlite3V
2a346 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2a347 47 6f 73 75 62 2c 20 72 65 67 41 64 64 72 41 2c  Gosub, regAddrA,
2a348 20 61 64 64 72 53 65 6c 65 63 74 41 29 3b 0a 20   addrSelectA);. 
2a349 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a34a 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20  p2(v, OP_Gosub, 
2a34b 72 65 67 41 64 64 72 42 2c 20 61 64 64 72 53 65  regAddrB, addrSe
2a34c 6c 65 63 74 42 29 3b 0a 20 20 73 71 6c 69 74 65  lectB);.  sqlite
2a34d 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2a34e 50 5f 49 66 2c 20 72 65 67 45 6f 66 41 2c 20 61  P_If, regEofA, a
2a34f 64 64 72 45 6f 66 41 29 3b 0a 20 20 73 71 6c 69  ddrEofA);.  sqli
2a350 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2a351 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 42 2c   OP_If, regEofB,
2a352 20 61 64 64 72 45 6f 66 42 29 3b 0a 0a 20 20 2f   addrEofB);..  /
2a353 2a 20 49 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  * Implement the 
2a354 6d 61 69 6e 20 6d 65 72 67 65 20 6c 6f 6f 70 0a  main merge loop.
2a355 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
2a356 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
2a357 2c 20 6c 61 62 65 6c 43 6d 70 72 29 3b 0a 20 20  , labelCmpr);.  
2a358 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a359 34 28 76 2c 20 4f 50 5f 50 65 72 6d 75 74 61 74  4(v, OP_Permutat
2a35a 69 6f 6e 2c 20 30 2c 20 30 2c 20 30 2c 20 28 63  ion, 0, 0, 0, (c
2a35b 68 61 72 2a 29 61 50 65 72 6d 75 74 65 2c 20 50  har*)aPermute, P
2a35c 34 5f 49 4e 54 41 52 52 41 59 29 3b 0a 20 20 73  4_INTARRAY);.  s
2a35d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2a35e 28 76 2c 20 4f 50 5f 43 6f 6d 70 61 72 65 2c 20  (v, OP_Compare, 
2a35f 64 65 73 74 41 2e 69 4d 65 6d 2c 20 64 65 73 74  destA.iMem, dest
2a360 42 2e 69 4d 65 6d 2c 20 6e 4f 72 64 65 72 42 79  B.iMem, nOrderBy
2a361 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2a362 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72             (char
2a363 2a 29 70 4b 65 79 4d 65 72 67 65 2c 20 50 34 5f  *)pKeyMerge, P4_
2a364 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29  KEYINFO_HANDOFF)
2a365 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
2a366 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4a 75 6d 70  ddOp3(v, OP_Jump
2a367 2c 20 61 64 64 72 41 6c 74 42 2c 20 61 64 64 72  , addrAltB, addr
2a368 41 65 71 42 2c 20 61 64 64 72 41 67 74 42 29 3b  AeqB, addrAgtB);
2a369 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74  ..  /* Release t
2a36a 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65  emporary registe
2a36b 72 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 65  rs.  */.  if( re
2a36c 67 50 72 65 76 20 29 7b 0a 20 20 20 20 73 71 6c  gPrev ){.    sql
2a36d 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
2a36e 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67  ange(pParse, reg
2a36f 50 72 65 76 2c 20 6e 4f 72 64 65 72 42 79 2b 31  Prev, nOrderBy+1
2a370 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d  );.  }..  /* Jum
2a371 70 20 74 6f 20 74 68 65 20 74 68 69 73 20 70 6f  p to the this po
2a372 69 6e 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  int in order to 
2a373 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 71 75  terminate the qu
2a374 65 72 79 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  ery..  */.  sqli
2a375 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
2a376 62 65 6c 28 76 2c 20 6c 61 62 65 6c 45 6e 64 29  bel(v, labelEnd)
2a377 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20  ;..  /* Set the 
2a378 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74 70 75 74  number of output
2a379 20 63 6f 6c 75 6d 6e 73 0a 20 20 2a 2f 0a 20 20   columns.  */.  
2a37a 69 66 28 20 70 44 65 73 74 2d 3e 65 44 65 73 74  if( pDest->eDest
2a37b 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a  ==SRT_Output ){.
2a37c 20 20 20 20 53 65 6c 65 63 74 20 2a 70 46 69 72      Select *pFir
2a37d 73 74 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20  st = pPrior;.   
2a37e 20 77 68 69 6c 65 28 20 70 46 69 72 73 74 2d 3e   while( pFirst->
2a37f 70 50 72 69 6f 72 20 29 20 70 46 69 72 73 74 20  pPrior ) pFirst 
2a380 3d 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72  = pFirst->pPrior
2a381 3b 0a 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f  ;.    generateCo
2a382 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65  lumnNames(pParse
2a383 2c 20 30 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c  , 0, pFirst->pEL
2a384 69 73 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ist);.  }..  /* 
2a385 52 65 61 73 73 65 6d 62 6c 79 20 74 68 65 20 63  Reassembly the c
2a386 6f 6d 70 6f 75 6e 64 20 71 75 65 72 79 20 73 6f  ompound query so
2a387 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65   that it will be
2a388 20 66 72 65 65 64 20 63 6f 72 72 65 63 74 6c 79   freed correctly
2a389 0a 20 20 2a 2a 20 62 79 20 74 68 65 20 63 61 6c  .  ** by the cal
2a38a 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  ling function */
2a38b 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72  .  if( p->pPrior
2a38c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
2a38d 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
2a38e 70 2d 3e 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a  p->pPrior);.  }.
2a38f 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50    p->pPrior = pP
2a390 72 69 6f 72 3b 0a 0a 20 20 2f 2a 2a 2a 20 54 42  rior;..  /*** TB
2a391 44 3a 20 20 49 6e 73 65 72 74 20 73 75 62 72 6f  D:  Insert subro
2a392 75 74 69 6e 65 20 63 61 6c 6c 73 20 74 6f 20 63  utine calls to c
2a393 6c 6f 73 65 20 63 75 72 73 6f 72 73 20 6f 6e 20  lose cursors on 
2a394 69 6e 63 6f 6d 70 6c 65 74 65 0a 20 20 2a 2a 2a  incomplete.  ***
2a395 2a 20 73 75 62 71 75 65 72 69 65 73 20 2a 2a 2a  * subqueries ***
2a396 2a 2f 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  */.  return SQLI
2a397 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a  TE_OK;.}.#endif.
2a398 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
2a399 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
2a39a 52 59 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28  RY) || !defined(
2a39b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
2a39c 29 0a 2f 2a 20 46 6f 72 77 61 72 64 20 44 65 63  )./* Forward Dec
2a39d 6c 61 72 61 74 69 6f 6e 73 20 2a 2f 0a 73 74 61  larations */.sta
2a39e 74 69 63 20 76 6f 69 64 20 73 75 62 73 74 45 78  tic void substEx
2a39f 70 72 4c 69 73 74 28 73 71 6c 69 74 65 33 2a 2c  prList(sqlite3*,
2a3a0 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c   ExprList*, int,
2a3a1 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 73 74 61   ExprList*);.sta
2a3a2 74 69 63 20 76 6f 69 64 20 73 75 62 73 74 53 65  tic void substSe
2a3a3 6c 65 63 74 28 73 71 6c 69 74 65 33 2a 2c 20 53  lect(sqlite3*, S
2a3a4 65 6c 65 63 74 20 2a 2c 20 69 6e 74 2c 20 45 78  elect *, int, Ex
2a3a5 70 72 4c 69 73 74 20 2a 29 3b 0a 0a 2f 2a 0a 2a  prList *);../*.*
2a3a6 2a 20 53 63 61 6e 20 74 68 72 6f 75 67 68 20 74  * Scan through t
2a3a7 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45  he expression pE
2a3a8 78 70 72 2e 20 20 52 65 70 6c 61 63 65 20 65 76  xpr.  Replace ev
2a3a9 65 72 79 20 72 65 66 65 72 65 6e 63 65 20 74 6f  ery reference to
2a3aa 0a 2a 2a 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20  .** a column in 
2a3ab 74 61 62 6c 65 20 6e 75 6d 62 65 72 20 69 54 61  table number iTa
2a3ac 62 6c 65 20 77 69 74 68 20 61 20 63 6f 70 79 20  ble with a copy 
2a3ad 6f 66 20 74 68 65 20 69 43 6f 6c 75 6d 6e 2d 74  of the iColumn-t
2a3ae 68 0a 2a 2a 20 65 6e 74 72 79 20 69 6e 20 70 45  h.** entry in pE
2a3af 4c 69 73 74 2e 20 20 28 42 75 74 20 6c 65 61 76  List.  (But leav
2a3b0 65 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  e references to 
2a3b1 74 68 65 20 52 4f 57 49 44 20 63 6f 6c 75 6d 6e  the ROWID column
2a3b2 20 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e 29   .** unchanged.)
2a3b3 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
2a3b4 69 6e 65 20 69 73 20 70 61 72 74 20 6f 66 20 74  ine is part of t
2a3b5 68 65 20 66 6c 61 74 74 65 6e 69 6e 67 20 70 72  he flattening pr
2a3b6 6f 63 65 64 75 72 65 2e 20 20 41 20 73 75 62 71  ocedure.  A subq
2a3b7 75 65 72 79 0a 2a 2a 20 77 68 6f 73 65 20 72 65  uery.** whose re
2a3b8 73 75 6c 74 20 73 65 74 20 69 73 20 64 65 66 69  sult set is defi
2a3b9 6e 65 64 20 62 79 20 70 45 4c 69 73 74 20 61 70  ned by pEList ap
2a3ba 70 65 61 72 73 20 61 73 20 65 6e 74 72 79 20 69  pears as entry i
2a3bb 6e 20 74 68 65 0a 2a 2a 20 46 52 4f 4d 20 63 6c  n the.** FROM cl
2a3bc 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54  ause of a SELECT
2a3bd 20 73 75 63 68 20 74 68 61 74 20 74 68 65 20 56   such that the V
2a3be 44 42 45 20 63 75 72 73 6f 72 20 61 73 73 69 67  DBE cursor assig
2a3bf 6e 65 64 20 74 6f 20 74 68 61 74 0a 2a 2a 20 46  ned to that.** F
2a3c0 4f 52 4d 20 63 6c 61 75 73 65 20 65 6e 74 72 79  ORM clause entry
2a3c1 20 69 73 20 69 54 61 62 6c 65 2e 20 20 54 68 69   is iTable.  Thi
2a3c2 73 20 72 6f 75 74 69 6e 65 20 6d 61 6b 65 20 74  s routine make t
2a3c3 68 65 20 6e 65 63 65 73 73 61 72 79 20 0a 2a 2a  he necessary .**
2a3c4 20 63 68 61 6e 67 65 73 20 74 6f 20 70 45 78 70   changes to pExp
2a3c5 72 20 73 6f 20 74 68 61 74 20 69 74 20 72 65 66  r so that it ref
2a3c6 65 72 73 20 64 69 72 65 63 74 6c 79 20 74 6f 20  ers directly to 
2a3c7 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65  the source table
2a3c8 0a 2a 2a 20 6f 66 20 74 68 65 20 73 75 62 71 75  .** of the subqu
2a3c9 65 72 79 20 72 61 74 68 65 72 20 74 68 65 20 72  ery rather the r
2a3ca 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65  esult set of the
2a3cb 20 73 75 62 71 75 65 72 79 2e 0a 2a 2f 0a 73 74   subquery..*/.st
2a3cc 61 74 69 63 20 45 78 70 72 20 2a 73 75 62 73 74  atic Expr *subst
2a3cd 45 78 70 72 28 0a 20 20 73 71 6c 69 74 65 33 20  Expr(.  sqlite3 
2a3ce 2a 64 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 52  *db,        /* R
2a3cf 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20 65 72 72  eport malloc err
2a3d0 6f 72 73 20 74 6f 20 74 68 69 73 20 63 6f 6e 6e  ors to this conn
2a3d1 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72  ection */.  Expr
2a3d2 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20   *pExpr,        
2a3d3 2f 2a 20 45 78 70 72 20 69 6e 20 77 68 69 63 68  /* Expr in which
2a3d4 20 73 75 62 73 74 69 74 75 74 69 6f 6e 20 6f 63   substitution oc
2a3d5 63 75 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 54  curs */.  int iT
2a3d6 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  able,         /*
2a3d7 20 54 61 62 6c 65 20 74 6f 20 62 65 20 73 75 62   Table to be sub
2a3d8 73 74 69 74 75 74 65 64 20 2a 2f 0a 20 20 45 78  stituted */.  Ex
2a3d9 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 20  prList *pEList  
2a3da 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20    /* Substitute 
2a3db 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 29  expressions */.)
2a3dc 7b 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30  {.  if( pExpr==0
2a3dd 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69   ) return 0;.  i
2a3de 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  f( pExpr->op==TK
2a3df 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 45 78 70 72  _COLUMN && pExpr
2a3e0 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65  ->iTable==iTable
2a3e1 20 29 7b 0a 20 20 20 20 69 66 28 20 70 45 78 70   ){.    if( pExp
2a3e2 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a  r->iColumn<0 ){.
2a3e3 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20        pExpr->op 
2a3e4 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 20 20 7d  = TK_NULL;.    }
2a3e5 65 6c 73 65 7b 0a 20 20 20 20 20 20 45 78 70 72  else{.      Expr
2a3e6 20 2a 70 4e 65 77 3b 0a 20 20 20 20 20 20 61 73   *pNew;.      as
2a3e7 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20  sert( pEList!=0 
2a3e8 26 26 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d  && pExpr->iColum
2a3e9 6e 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20  n<pEList->nExpr 
2a3ea 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
2a3eb 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3d 3d 30   pExpr->pLeft==0
2a3ec 20 26 26 20 70 45 78 70 72 2d 3e 70 52 69 67 68   && pExpr->pRigh
2a3ed 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4e  t==0 );.      pN
2a3ee 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ew = sqlite3Expr
2a3ef 44 75 70 28 64 62 2c 20 70 45 4c 69 73 74 2d 3e  Dup(db, pEList->
2a3f0 61 5b 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  a[pExpr->iColumn
2a3f1 5d 2e 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 20  ].pExpr, 0);.   
2a3f2 20 20 20 69 66 28 20 70 4e 65 77 20 26 26 20 70     if( pNew && p
2a3f3 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 29 7b 0a 20  Expr->pColl ){. 
2a3f4 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 43 6f         pNew->pCo
2a3f5 6c 6c 20 3d 20 70 45 78 70 72 2d 3e 70 43 6f 6c  ll = pExpr->pCol
2a3f6 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  l;.      }.     
2a3f7 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2a3f8 74 65 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 20  te(db, pExpr);. 
2a3f9 20 20 20 20 20 70 45 78 70 72 20 3d 20 70 4e 65       pExpr = pNe
2a3fa 77 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  w;.    }.  }else
2a3fb 7b 0a 20 20 20 20 70 45 78 70 72 2d 3e 70 4c 65  {.    pExpr->pLe
2a3fc 66 74 20 3d 20 73 75 62 73 74 45 78 70 72 28 64  ft = substExpr(d
2a3fd 62 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  b, pExpr->pLeft,
2a3fe 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29   iTable, pEList)
2a3ff 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69  ;.    pExpr->pRi
2a400 67 68 74 20 3d 20 73 75 62 73 74 45 78 70 72 28  ght = substExpr(
2a401 64 62 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  db, pExpr->pRigh
2a402 74 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73  t, iTable, pELis
2a403 74 29 3b 0a 20 20 20 20 69 66 28 20 45 78 70 72  t);.    if( Expr
2a404 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
2a405 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
2a406 20 29 7b 0a 20 20 20 20 20 20 73 75 62 73 74 53   ){.      substS
2a407 65 6c 65 63 74 28 64 62 2c 20 70 45 78 70 72 2d  elect(db, pExpr-
2a408 3e 78 2e 70 53 65 6c 65 63 74 2c 20 69 54 61 62  >x.pSelect, iTab
2a409 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20  le, pEList);.   
2a40a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 75   }else{.      su
2a40b 62 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20  bstExprList(db, 
2a40c 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2c 20  pExpr->x.pList, 
2a40d 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b  iTable, pEList);
2a40e 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
2a40f 75 72 6e 20 70 45 78 70 72 3b 0a 7d 0a 73 74 61  urn pExpr;.}.sta
2a410 74 69 63 20 76 6f 69 64 20 73 75 62 73 74 45 78  tic void substEx
2a411 70 72 4c 69 73 74 28 0a 20 20 73 71 6c 69 74 65  prList(.  sqlite
2a412 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 2f  3 *db,         /
2a413 2a 20 52 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20  * Report malloc 
2a414 65 72 72 6f 72 73 20 68 65 72 65 20 2a 2f 0a 20  errors here */. 
2a415 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
2a416 2c 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f  ,     /* List to
2a417 20 73 63 61 6e 20 61 6e 64 20 69 6e 20 77 68 69   scan and in whi
2a418 63 68 20 74 6f 20 6d 61 6b 65 20 73 75 62 73 74  ch to make subst
2a419 69 74 75 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20  itutes */.  int 
2a41a 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20  iTable,         
2a41b 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62 65 20   /* Table to be 
2a41c 73 75 62 73 74 69 74 75 74 65 64 20 2a 2f 0a 20  substituted */. 
2a41d 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
2a41e 74 20 20 20 20 20 2f 2a 20 53 75 62 73 74 69 74  t     /* Substit
2a41f 75 74 65 20 76 61 6c 75 65 73 20 2a 2f 0a 29 7b  ute values */.){
2a420 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
2a421 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72  pList==0 ) retur
2a422 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  n;.  for(i=0; i<
2a423 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b  pList->nExpr; i+
2a424 2b 29 7b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 61  +){.    pList->a
2a425 5b 69 5d 2e 70 45 78 70 72 20 3d 20 73 75 62 73  [i].pExpr = subs
2a426 74 45 78 70 72 28 64 62 2c 20 70 4c 69 73 74 2d  tExpr(db, pList-
2a427 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 69 54 61  >a[i].pExpr, iTa
2a428 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ble, pEList);.  
2a429 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  }.}.static void 
2a42a 73 75 62 73 74 53 65 6c 65 63 74 28 0a 20 20 73  substSelect(.  s
2a42b 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
2a42c 20 20 20 20 2f 2a 20 52 65 70 6f 72 74 20 6d 61      /* Report ma
2a42d 6c 6c 6f 63 20 65 72 72 6f 72 73 20 68 65 72 65  lloc errors here
2a42e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c   */.  Select *p,
2a42f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 45             /* SE
2a430 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69  LECT statement i
2a431 6e 20 77 68 69 63 68 20 74 6f 20 6d 61 6b 65 20  n which to make 
2a432 73 75 62 73 74 69 74 75 74 69 6f 6e 73 20 2a 2f  substitutions */
2a433 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20  .  int iTable,  
2a434 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
2a435 20 74 6f 20 62 65 20 72 65 70 6c 61 63 65 64 20   to be replaced 
2a436 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2a437 45 4c 69 73 74 20 20 20 20 20 2f 2a 20 53 75 62  EList     /* Sub
2a438 73 74 69 74 75 74 65 20 76 61 6c 75 65 73 20 2a  stitute values *
2a439 2f 0a 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a  /.){.  SrcList *
2a43a 70 53 72 63 3b 0a 20 20 73 74 72 75 63 74 20 53  pSrc;.  struct S
2a43b 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
2a43c 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  em;.  int i;.  i
2a43d 66 28 20 21 70 20 29 20 72 65 74 75 72 6e 3b 0a  f( !p ) return;.
2a43e 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28    substExprList(
2a43f 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 69  db, p->pEList, i
2a440 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a  Table, pEList);.
2a441 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28    substExprList(
2a442 64 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 2c  db, p->pGroupBy,
2a443 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29   iTable, pEList)
2a444 3b 0a 20 20 73 75 62 73 74 45 78 70 72 4c 69 73  ;.  substExprLis
2a445 74 28 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42  t(db, p->pOrderB
2a446 79 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73  y, iTable, pELis
2a447 74 29 3b 0a 20 20 70 2d 3e 70 48 61 76 69 6e 67  t);.  p->pHaving
2a448 20 3d 20 73 75 62 73 74 45 78 70 72 28 64 62 2c   = substExpr(db,
2a449 20 70 2d 3e 70 48 61 76 69 6e 67 2c 20 69 54 61   p->pHaving, iTa
2a44a 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ble, pEList);.  
2a44b 70 2d 3e 70 57 68 65 72 65 20 3d 20 73 75 62 73  p->pWhere = subs
2a44c 74 45 78 70 72 28 64 62 2c 20 70 2d 3e 70 57 68  tExpr(db, p->pWh
2a44d 65 72 65 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c  ere, iTable, pEL
2a44e 69 73 74 29 3b 0a 20 20 73 75 62 73 74 53 65 6c  ist);.  substSel
2a44f 65 63 74 28 64 62 2c 20 70 2d 3e 70 50 72 69 6f  ect(db, p->pPrio
2a450 72 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73  r, iTable, pELis
2a451 74 29 3b 0a 20 20 70 53 72 63 20 3d 20 70 2d 3e  t);.  pSrc = p->
2a452 70 53 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  pSrc;.  assert( 
2a453 70 53 72 63 20 29 3b 20 20 2f 2a 20 45 76 65 6e  pSrc );  /* Even
2a454 20 66 6f 72 20 28 53 45 4c 45 43 54 20 31 29 20   for (SELECT 1) 
2a455 77 65 20 68 61 76 65 3a 20 70 53 72 63 21 3d 30  we have: pSrc!=0
2a456 20 62 75 74 20 70 53 72 63 2d 3e 6e 53 72 63 3d   but pSrc->nSrc=
2a457 3d 30 20 2a 2f 0a 20 20 69 66 28 20 41 4c 57 41  =0 */.  if( ALWA
2a458 59 53 28 70 53 72 63 29 20 29 7b 0a 20 20 20 20  YS(pSrc) ){.    
2a459 66 6f 72 28 69 3d 70 53 72 63 2d 3e 6e 53 72 63  for(i=pSrc->nSrc
2a45a 2c 20 70 49 74 65 6d 3d 70 53 72 63 2d 3e 61 3b  , pItem=pSrc->a;
2a45b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d   i>0; i--, pItem
2a45c 2b 2b 29 7b 0a 20 20 20 20 20 20 73 75 62 73 74  ++){.      subst
2a45d 53 65 6c 65 63 74 28 64 62 2c 20 70 49 74 65 6d  Select(db, pItem
2a45e 2d 3e 70 53 65 6c 65 63 74 2c 20 69 54 61 62 6c  ->pSelect, iTabl
2a45f 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  e, pEList);.    
2a460 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f  }.  }.}.#endif /
2a461 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
2a462 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29  E_OMIT_SUBQUERY)
2a463 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
2a464 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 2a  ITE_OMIT_VIEW) *
2a465 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  /..#if !defined(
2a466 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
2a467 55 45 52 59 29 20 7c 7c 20 21 64 65 66 69 6e 65  UERY) || !define
2a468 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  d(SQLITE_OMIT_VI
2a469 45 57 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  EW)./*.** This r
2a46a 6f 75 74 69 6e 65 20 61 74 74 65 6d 70 74 73 20  outine attempts 
2a46b 74 6f 20 66 6c 61 74 74 65 6e 20 73 75 62 71 75  to flatten subqu
2a46c 65 72 69 65 73 20 69 6e 20 6f 72 64 65 72 20 74  eries in order t
2a46d 6f 20 73 70 65 65 64 0a 2a 2a 20 65 78 65 63 75  o speed.** execu
2a46e 74 69 6f 6e 2e 20 20 49 74 20 72 65 74 75 72 6e  tion.  It return
2a46f 73 20 31 20 69 66 20 69 74 20 6d 61 6b 65 73 20  s 1 if it makes 
2a470 63 68 61 6e 67 65 73 20 61 6e 64 20 30 20 69 66  changes and 0 if
2a471 20 6e 6f 20 66 6c 61 74 74 65 6e 69 6e 67 0a 2a   no flattening.*
2a472 2a 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20  * occurs..**.** 
2a473 54 6f 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68  To understand th
2a474 65 20 63 6f 6e 63 65 70 74 20 6f 66 20 66 6c 61  e concept of fla
2a475 74 74 65 6e 69 6e 67 2c 20 63 6f 6e 73 69 64 65  ttening, conside
2a476 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  r the following.
2a477 2a 2a 20 71 75 65 72 79 3a 0a 2a 2a 0a 2a 2a 20  ** query:.**.** 
2a478 20 20 20 20 53 45 4c 45 43 54 20 61 20 46 52 4f      SELECT a FRO
2a479 4d 20 28 53 45 4c 45 43 54 20 78 2b 79 20 41 53  M (SELECT x+y AS
2a47a 20 61 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45   a FROM t1 WHERE
2a47b 20 7a 3c 31 30 30 29 20 57 48 45 52 45 20 61 3e   z<100) WHERE a>
2a47c 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61  5.**.** The defa
2a47d 75 6c 74 20 77 61 79 20 6f 66 20 69 6d 70 6c 65  ult way of imple
2a47e 6d 65 6e 74 69 6e 67 20 74 68 69 73 20 71 75 65  menting this que
2a47f 72 79 20 69 73 20 74 6f 20 65 78 65 63 75 74 65  ry is to execute
2a480 20 74 68 65 0a 2a 2a 20 73 75 62 71 75 65 72 79   the.** subquery
2a481 20 66 69 72 73 74 20 61 6e 64 20 73 74 6f 72 65   first and store
2a482 20 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20   the results in 
2a483 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  a temporary tabl
2a484 65 2c 20 74 68 65 6e 0a 2a 2a 20 72 75 6e 20 74  e, then.** run t
2a485 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 6f  he outer query o
2a486 6e 20 74 68 61 74 20 74 65 6d 70 6f 72 61 72 79  n that temporary
2a487 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 72 65   table.  This re
2a488 71 75 69 72 65 73 20 74 77 6f 0a 2a 2a 20 70 61  quires two.** pa
2a489 73 73 65 73 20 6f 76 65 72 20 74 68 65 20 64 61  sses over the da
2a48a 74 61 2e 20 20 46 75 72 74 68 65 72 6d 6f 72 65  ta.  Furthermore
2a48b 2c 20 62 65 63 61 75 73 65 20 74 68 65 20 74 65  , because the te
2a48c 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 0a 2a 2a  mporary table.**
2a48d 20 68 61 73 20 6e 6f 20 69 6e 64 69 63 65 73 2c   has no indices,
2a48e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2a48f 65 20 6f 6e 20 74 68 65 20 6f 75 74 65 72 20 71  e on the outer q
2a490 75 65 72 79 20 63 61 6e 6e 6f 74 20 62 65 0a 2a  uery cannot be.*
2a491 2a 20 6f 70 74 69 6d 69 7a 65 64 2e 0a 2a 2a 0a  * optimized..**.
2a492 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2a493 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 77 72  attempts to rewr
2a494 69 74 65 20 71 75 65 72 69 65 73 20 73 75 63 68  ite queries such
2a495 20 61 73 20 74 68 65 20 61 62 6f 76 65 20 69 6e   as the above in
2a496 74 6f 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 66  to.** a single f
2a497 6c 61 74 20 73 65 6c 65 63 74 2c 20 6c 69 6b 65  lat select, like
2a498 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   this:.**.**    
2a499 20 53 45 4c 45 43 54 20 78 2b 79 20 41 53 20 61   SELECT x+y AS a
2a49a 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 7a   FROM t1 WHERE z
2a49b 3c 31 30 30 20 41 4e 44 20 61 3e 35 0a 2a 2a 0a  <100 AND a>5.**.
2a49c 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65  ** The code gene
2a49d 72 61 74 65 64 20 66 6f 72 20 74 68 69 73 20 73  rated for this s
2a49e 69 6d 70 69 66 69 63 61 74 69 6f 6e 20 67 69 76  impification giv
2a49f 65 73 20 74 68 65 20 73 61 6d 65 20 72 65 73 75  es the same resu
2a4a0 6c 74 0a 2a 2a 20 62 75 74 20 6f 6e 6c 79 20 68  lt.** but only h
2a4a1 61 73 20 74 6f 20 73 63 61 6e 20 74 68 65 20 64  as to scan the d
2a4a2 61 74 61 20 6f 6e 63 65 2e 20 20 41 6e 64 20 62  ata once.  And b
2a4a3 65 63 61 75 73 65 20 69 6e 64 69 63 65 73 20 6d  ecause indices m
2a4a4 69 67 68 74 20 0a 2a 2a 20 65 78 69 73 74 20 6f  ight .** exist o
2a4a5 6e 20 74 68 65 20 74 61 62 6c 65 20 74 31 2c 20  n the table t1, 
2a4a6 61 20 63 6f 6d 70 6c 65 74 65 20 73 63 61 6e 20  a complete scan 
2a4a7 6f 66 20 74 68 65 20 64 61 74 61 20 6d 69 67 68  of the data migh
2a4a8 74 20 62 65 0a 2a 2a 20 61 76 6f 69 64 65 64 2e  t be.** avoided.
2a4a9 0a 2a 2a 0a 2a 2a 20 46 6c 61 74 74 65 6e 69 6e  .**.** Flattenin
2a4aa 67 20 69 73 20 6f 6e 6c 79 20 61 74 74 65 6d 70  g is only attemp
2a4ab 74 65 64 20 69 66 20 61 6c 6c 20 6f 66 20 74 68  ted if all of th
2a4ac 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20  e following are 
2a4ad 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 31  true:.**.**   (1
2a4ae 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20  )  The subquery 
2a4af 61 6e 64 20 74 68 65 20 6f 75 74 65 72 20 71 75  and the outer qu
2a4b0 65 72 79 20 64 6f 20 6e 6f 74 20 62 6f 74 68 20  ery do not both 
2a4b1 75 73 65 20 61 67 67 72 65 67 61 74 65 73 2e 0a  use aggregates..
2a4b2 2a 2a 0a 2a 2a 20 20 20 28 32 29 20 20 54 68 65  **.**   (2)  The
2a4b3 20 73 75 62 71 75 65 72 79 20 69 73 20 6e 6f 74   subquery is not
2a4b4 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 6f 72   an aggregate or
2a4b5 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
2a4b6 20 69 73 20 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0a   is not a join..
2a4b7 2a 2a 0a 2a 2a 20 20 20 28 33 29 20 20 54 68 65  **.**   (3)  The
2a4b8 20 73 75 62 71 75 65 72 79 20 69 73 20 6e 6f 74   subquery is not
2a4b9 20 74 68 65 20 72 69 67 68 74 20 6f 70 65 72 61   the right opera
2a4ba 6e 64 20 6f 66 20 61 20 6c 65 66 74 20 6f 75 74  nd of a left out
2a4bb 65 72 20 6a 6f 69 6e 0a 2a 2a 20 20 20 20 20 20  er join.**      
2a4bc 20 20 28 4f 72 69 67 69 6e 61 6c 6c 79 20 74 69    (Originally ti
2a4bd 63 6b 65 74 20 23 33 30 36 2e 20 20 53 74 72 65  cket #306.  Stre
2a4be 6e 67 68 74 65 6e 65 64 20 62 79 20 74 69 63 6b  nghtened by tick
2a4bf 65 74 20 23 33 33 30 30 29 0a 2a 2a 0a 2a 2a 20  et #3300).**.** 
2a4c0 20 20 28 34 29 20 20 54 68 65 20 73 75 62 71 75    (4)  The subqu
2a4c1 65 72 79 20 69 73 20 6e 6f 74 20 44 49 53 54 49  ery is not DISTI
2a4c2 4e 43 54 20 6f 72 20 74 68 65 20 6f 75 74 65 72  NCT or the outer
2a4c3 20 71 75 65 72 79 20 69 73 20 6e 6f 74 20 61 20   query is not a 
2a4c4 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 35  join..**.**   (5
2a4c5 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20  )  The subquery 
2a4c6 69 73 20 6e 6f 74 20 44 49 53 54 49 4e 43 54 20  is not DISTINCT 
2a4c7 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  or the outer que
2a4c8 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 0a  ry does not use.
2a4c9 2a 2a 20 20 20 20 20 20 20 20 61 67 67 72 65 67  **        aggreg
2a4ca 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 36  ates..**.**   (6
2a4cb 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20  )  The subquery 
2a4cc 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 61 67 67  does not use agg
2a4cd 72 65 67 61 74 65 73 20 6f 72 20 74 68 65 20 6f  regates or the o
2a4ce 75 74 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f  uter query is no
2a4cf 74 0a 2a 2a 20 20 20 20 20 20 20 20 44 49 53 54  t.**        DIST
2a4d0 49 4e 43 54 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 37  INCT..**.**   (7
2a4d1 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20  )  The subquery 
2a4d2 68 61 73 20 61 20 46 52 4f 4d 20 63 6c 61 75 73  has a FROM claus
2a4d3 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 38 29 20 20  e..**.**   (8)  
2a4d4 54 68 65 20 73 75 62 71 75 65 72 79 20 64 6f 65  The subquery doe
2a4d5 73 20 6e 6f 74 20 75 73 65 20 4c 49 4d 49 54 20  s not use LIMIT 
2a4d6 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  or the outer que
2a4d7 72 79 20 69 73 20 6e 6f 74 20 61 20 6a 6f 69 6e  ry is not a join
2a4d8 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 39 29 20 20 54  ..**.**   (9)  T
2a4d9 68 65 20 73 75 62 71 75 65 72 79 20 64 6f 65 73  he subquery does
2a4da 20 6e 6f 74 20 75 73 65 20 4c 49 4d 49 54 20 6f   not use LIMIT o
2a4db 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  r the outer quer
2a4dc 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 0a 2a  y does not use.*
2a4dd 2a 20 20 20 20 20 20 20 20 61 67 67 72 65 67 61  *        aggrega
2a4de 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 30 29  tes..**.**  (10)
2a4df 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20 64    The subquery d
2a4e0 6f 65 73 20 6e 6f 74 20 75 73 65 20 61 67 67 72  oes not use aggr
2a4e1 65 67 61 74 65 73 20 6f 72 20 74 68 65 20 6f 75  egates or the ou
2a4e2 74 65 72 20 71 75 65 72 79 20 64 6f 65 73 20 6e  ter query does n
2a4e3 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 75 73 65  ot.**        use
2a4e4 20 4c 49 4d 49 54 2e 0a 2a 2a 0a 2a 2a 20 20 28   LIMIT..**.**  (
2a4e5 31 31 29 20 20 54 68 65 20 73 75 62 71 75 65 72  11)  The subquer
2a4e6 79 20 61 6e 64 20 74 68 65 20 6f 75 74 65 72 20  y and the outer 
2a4e7 71 75 65 72 79 20 64 6f 20 6e 6f 74 20 62 6f 74  query do not bot
2a4e8 68 20 68 61 76 65 20 4f 52 44 45 52 20 42 59 20  h have ORDER BY 
2a4e9 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 20  clauses..**.**  
2a4ea 28 31 32 29 20 20 4e 6f 74 20 69 6d 70 6c 65 6d  (12)  Not implem
2a4eb 65 6e 74 65 64 2e 20 20 53 75 62 73 75 6d 65 64  ented.  Subsumed
2a4ec 20 69 6e 74 6f 20 72 65 73 74 72 69 63 74 69 6f   into restrictio
2a4ed 6e 20 28 33 29 2e 20 20 57 61 73 20 70 72 65 76  n (3).  Was prev
2a4ee 69 6f 75 73 6c 79 0a 2a 2a 20 20 20 20 20 20 20  iously.**       
2a4ef 20 61 20 73 65 70 61 72 61 74 65 20 72 65 73 74   a separate rest
2a4f0 72 69 63 74 69 6f 6e 20 64 65 72 69 76 69 6e 67  riction deriving
2a4f1 20 66 72 6f 6d 20 74 69 63 6b 65 74 20 23 33 35   from ticket #35
2a4f2 30 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 33 29 20 20  0..**.**  (13)  
2a4f3 54 68 65 20 73 75 62 71 75 65 72 79 20 61 6e 64  The subquery and
2a4f4 20 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f 20   outer query do 
2a4f5 6e 6f 74 20 62 6f 74 68 20 75 73 65 20 4c 49 4d  not both use LIM
2a4f6 49 54 0a 2a 2a 0a 2a 2a 20 20 28 31 34 29 20 20  IT.**.**  (14)  
2a4f7 54 68 65 20 73 75 62 71 75 65 72 79 20 64 6f 65  The subquery doe
2a4f8 73 20 6e 6f 74 20 75 73 65 20 4f 46 46 53 45 54  s not use OFFSET
2a4f9 0a 2a 2a 0a 2a 2a 20 20 28 31 35 29 20 20 54 68  .**.**  (15)  Th
2a4fa 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 69 73  e outer query is
2a4fb 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61 20 63   not part of a c
2a4fc 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 6f  ompound select o
2a4fd 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  r the.**        
2a4fe 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f  subquery does no
2a4ff 74 20 68 61 76 65 20 62 6f 74 68 20 61 6e 20 4f  t have both an O
2a500 52 44 45 52 20 42 59 20 61 6e 64 20 61 20 4c 49  RDER BY and a LI
2a501 4d 49 54 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 20  MIT clause..**  
2a502 20 20 20 20 20 20 28 53 65 65 20 74 69 63 6b 65        (See ticke
2a503 74 20 23 32 33 33 39 29 0a 2a 2a 0a 2a 2a 20 20  t #2339).**.**  
2a504 28 31 36 29 20 20 54 68 65 20 6f 75 74 65 72 20  (16)  The outer 
2a505 71 75 65 72 79 20 69 73 20 6e 6f 74 20 61 6e 20  query is not an 
2a506 61 67 67 72 65 67 61 74 65 20 6f 72 20 74 68 65  aggregate or the
2a507 20 73 75 62 71 75 65 72 79 20 64 6f 65 73 0a 2a   subquery does.*
2a508 2a 20 20 20 20 20 20 20 20 6e 6f 74 20 63 6f 6e  *        not con
2a509 74 61 69 6e 20 4f 52 44 45 52 20 42 59 2e 20 20  tain ORDER BY.  
2a50a 28 54 69 63 6b 65 74 20 23 32 39 34 32 29 20 20  (Ticket #2942)  
2a50b 54 68 69 73 20 75 73 65 64 20 74 6f 20 6e 6f 74  This used to not
2a50c 20 6d 61 74 74 65 72 0a 2a 2a 20 20 20 20 20 20   matter.**      
2a50d 20 20 75 6e 74 69 6c 20 77 65 20 69 6e 74 72 6f    until we intro
2a50e 64 75 63 65 64 20 74 68 65 20 67 72 6f 75 70 5f  duced the group_
2a50f 63 6f 6e 63 61 74 28 29 20 66 75 6e 63 74 69 6f  concat() functio
2a510 6e 2e 20 20 0a 2a 2a 0a 2a 2a 20 20 28 31 37 29  n.  .**.**  (17)
2a511 20 20 54 68 65 20 73 75 62 2d 71 75 65 72 79 20    The sub-query 
2a512 69 73 20 6e 6f 74 20 61 20 63 6f 6d 70 6f 75 6e  is not a compoun
2a513 64 20 73 65 6c 65 63 74 2c 20 6f 72 20 69 74 20  d select, or it 
2a514 69 73 20 61 20 55 4e 49 4f 4e 20 41 4c 4c 20 0a  is a UNION ALL .
2a515 2a 2a 20 20 20 20 20 20 20 20 63 6f 6d 70 6f 75  **        compou
2a516 6e 64 20 63 6c 61 75 73 65 20 6d 61 64 65 20 75  nd clause made u
2a517 70 20 65 6e 74 69 72 65 6c 79 20 6f 66 20 6e 6f  p entirely of no
2a518 6e 2d 61 67 67 72 65 67 61 74 65 20 71 75 65 72  n-aggregate quer
2a519 69 65 73 2c 20 61 6e 64 20 0a 2a 2a 20 20 20 20  ies, and .**    
2a51a 20 20 20 20 74 68 65 20 70 61 72 65 6e 74 20 71      the parent q
2a51b 75 65 72 79 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  uery:.**.**     
2a51c 20 20 20 20 20 2a 20 69 73 20 6e 6f 74 20 69 74       * is not it
2a51d 73 65 6c 66 20 70 61 72 74 20 6f 66 20 61 20 63  self part of a c
2a51e 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2c 0a  ompound select,.
2a51f 2a 2a 20 20 20 20 20 20 20 20 20 20 2a 20 69 73  **          * is
2a520 20 6e 6f 74 20 61 6e 20 61 67 67 72 65 67 61 74   not an aggregat
2a521 65 20 6f 72 20 44 49 53 54 49 4e 43 54 20 71 75  e or DISTINCT qu
2a522 65 72 79 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 20  ery, and.**     
2a523 20 20 20 20 20 2a 20 68 61 73 20 6e 6f 20 6f 74       * has no ot
2a524 68 65 72 20 74 61 62 6c 65 73 20 6f 72 20 73 75  her tables or su
2a525 62 2d 73 65 6c 65 63 74 73 20 69 6e 20 74 68 65  b-selects in the
2a526 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 2a 2a   FROM clause..**
2a527 0a 2a 2a 20 20 20 20 20 20 20 20 54 68 65 20 70  .**        The p
2a528 61 72 65 6e 74 20 61 6e 64 20 73 75 62 2d 71 75  arent and sub-qu
2a529 65 72 79 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20  ery may contain 
2a52a 57 48 45 52 45 20 63 6c 61 75 73 65 73 2e 20 53  WHERE clauses. S
2a52b 75 62 6a 65 63 74 20 74 6f 0a 2a 2a 20 20 20 20  ubject to.**    
2a52c 20 20 20 20 72 75 6c 65 73 20 28 31 31 29 2c 20      rules (11), 
2a52d 28 31 33 29 20 61 6e 64 20 28 31 34 29 2c 20 74  (13) and (14), t
2a52e 68 65 79 20 6d 61 79 20 61 6c 73 6f 20 63 6f 6e  hey may also con
2a52f 74 61 69 6e 20 4f 52 44 45 52 20 42 59 2c 0a 2a  tain ORDER BY,.*
2a530 2a 20 20 20 20 20 20 20 20 4c 49 4d 49 54 20 61  *        LIMIT a
2a531 6e 64 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65  nd OFFSET clause
2a532 73 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 38 29 20 20  s..**.**  (18)  
2a533 49 66 20 74 68 65 20 73 75 62 2d 71 75 65 72 79  If the sub-query
2a534 20 69 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73   is a compound s
2a535 65 6c 65 63 74 2c 20 74 68 65 6e 20 61 6c 6c 20  elect, then all 
2a536 74 65 72 6d 73 20 6f 66 20 74 68 65 0a 2a 2a 20  terms of the.** 
2a537 20 20 20 20 20 20 20 4f 52 44 45 52 20 62 79 20         ORDER by 
2a538 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 70 61  clause of the pa
2a539 72 65 6e 74 20 6d 75 73 74 20 62 65 20 73 69 6d  rent must be sim
2a53a 70 6c 65 20 72 65 66 65 72 65 6e 63 65 73 20 74  ple references t
2a53b 6f 20 0a 2a 2a 20 20 20 20 20 20 20 20 63 6f 6c  o .**        col
2a53c 75 6d 6e 73 20 6f 66 20 74 68 65 20 73 75 62 2d  umns of the sub-
2a53d 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 20 28 31  query..**.**  (1
2a53e 39 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79  9)  The subquery
2a53f 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 4c 49   does not use LI
2a540 4d 49 54 20 6f 72 20 74 68 65 20 6f 75 74 65 72  MIT or the outer
2a541 20 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 0a   query does not.
2a542 2a 2a 20 20 20 20 20 20 20 20 68 61 76 65 20 61  **        have a
2a543 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 2a   WHERE clause..*
2a544 2a 0a 2a 2a 20 20 28 32 30 29 20 20 49 66 20 74  *.**  (20)  If t
2a545 68 65 20 73 75 62 2d 71 75 65 72 79 20 69 73 20  he sub-query is 
2a546 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63  a compound selec
2a547 74 2c 20 74 68 65 6e 20 69 74 20 6d 75 73 74 20  t, then it must 
2a548 6e 6f 74 20 75 73 65 0a 2a 2a 20 20 20 20 20 20  not use.**      
2a549 20 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c    an ORDER BY cl
2a54a 61 75 73 65 2e 20 20 54 69 63 6b 65 74 20 23 33  ause.  Ticket #3
2a54b 37 37 33 2e 20 20 57 65 20 63 6f 75 6c 64 20 72  773.  We could r
2a54c 65 6c 61 78 20 74 68 69 73 20 63 6f 6e 73 74 72  elax this constr
2a54d 61 69 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 73  aint.**        s
2a54e 6f 6d 65 77 68 61 74 20 62 79 20 73 61 79 69 6e  omewhat by sayin
2a54f 67 20 74 68 61 74 20 74 68 65 20 74 65 72 6d 73  g that the terms
2a550 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59   of the ORDER BY
2a551 20 63 6c 61 75 73 65 20 6d 75 73 74 0a 2a 2a 20   clause must.** 
2a552 20 20 20 20 20 20 20 61 70 70 65 61 72 20 61 73         appear as
2a553 20 75 6e 6d 6f 64 69 66 69 65 64 20 72 65 73 75   unmodified resu
2a554 6c 74 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  lt columns in th
2a555 65 20 6f 75 74 65 72 20 71 75 65 72 79 2e 20 20  e outer query.  
2a556 42 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 68 61  But.**        ha
2a557 76 65 20 6f 74 68 65 72 20 6f 70 74 69 6d 69 7a  ve other optimiz
2a558 61 74 69 6f 6e 73 20 69 6e 20 6d 69 6e 64 20 74  ations in mind t
2a559 6f 20 64 65 61 6c 20 77 69 74 68 20 74 68 61 74  o deal with that
2a55a 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20   case..**.** In 
2a55b 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 74 68  this routine, th
2a55c 65 20 22 70 22 20 70 61 72 61 6d 65 74 65 72 20  e "p" parameter 
2a55d 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
2a55e 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 2e  the outer query.
2a55f 0a 2a 2a 20 54 68 65 20 73 75 62 71 75 65 72 79  .** The subquery
2a560 20 69 73 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 69   is p->pSrc->a[i
2a561 46 72 6f 6d 5d 2e 20 20 69 73 41 67 67 20 69 73  From].  isAgg is
2a562 20 74 72 75 65 20 69 66 20 74 68 65 20 6f 75 74   true if the out
2a563 65 72 20 71 75 65 72 79 0a 2a 2a 20 75 73 65 73  er query.** uses
2a564 20 61 67 67 72 65 67 61 74 65 73 20 61 6e 64 20   aggregates and 
2a565 73 75 62 71 75 65 72 79 49 73 41 67 67 20 69 73  subqueryIsAgg is
2a566 20 74 72 75 65 20 69 66 20 74 68 65 20 73 75 62   true if the sub
2a567 71 75 65 72 79 20 75 73 65 73 20 61 67 67 72 65  query uses aggre
2a568 67 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  gates..**.** If 
2a569 66 6c 61 74 74 65 6e 69 6e 67 20 69 73 20 6e 6f  flattening is no
2a56a 74 20 61 74 74 65 6d 70 74 65 64 2c 20 74 68 69  t attempted, thi
2a56b 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
2a56c 6f 2d 6f 70 20 61 6e 64 20 72 65 74 75 72 6e 73  o-op and returns
2a56d 20 30 2e 0a 2a 2a 20 49 66 20 66 6c 61 74 74 65   0..** If flatte
2a56e 6e 69 6e 67 20 69 73 20 61 74 74 65 6d 70 74 65  ning is attempte
2a56f 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  d this routine r
2a570 65 74 75 72 6e 73 20 31 2e 0a 2a 2a 0a 2a 2a 20  eturns 1..**.** 
2a571 41 6c 6c 20 6f 66 20 74 68 65 20 65 78 70 72 65  All of the expre
2a572 73 73 69 6f 6e 20 61 6e 61 6c 79 73 69 73 20 6d  ssion analysis m
2a573 75 73 74 20 6f 63 63 75 72 20 6f 6e 20 62 6f 74  ust occur on bot
2a574 68 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  h the outer quer
2a575 79 20 61 6e 64 0a 2a 2a 20 74 68 65 20 73 75 62  y and.** the sub
2a576 71 75 65 72 79 20 62 65 66 6f 72 65 20 74 68 69  query before thi
2a577 73 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 2e 0a  s routine runs..
2a578 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c  */.static int fl
2a579 61 74 74 65 6e 53 75 62 71 75 65 72 79 28 0a 20  attenSubquery(. 
2a57a 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2a57b 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
2a57c 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65   context */.  Se
2a57d 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20  lect *p,        
2a57e 20 20 20 2f 2a 20 54 68 65 20 70 61 72 65 6e 74     /* The parent
2a57f 20 6f 72 20 6f 75 74 65 72 20 53 45 4c 45 43 54   or outer SELECT
2a580 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
2a581 69 6e 74 20 69 46 72 6f 6d 2c 20 20 20 20 20 20  int iFrom,      
2a582 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e       /* Index in
2a583 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 5d 20 6f 66   p->pSrc->a[] of
2a584 20 74 68 65 20 69 6e 6e 65 72 20 73 75 62 71 75   the inner subqu
2a585 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 73 41  ery */.  int isA
2a586 67 67 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  gg,           /*
2a587 20 54 72 75 65 20 69 66 20 6f 75 74 65 72 20 53   True if outer S
2a588 45 4c 45 43 54 20 75 73 65 73 20 61 67 67 72 65  ELECT uses aggre
2a589 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a  gate functions *
2a58a 2f 0a 20 20 69 6e 74 20 73 75 62 71 75 65 72 79  /.  int subquery
2a58b 49 73 41 67 67 20 20 20 20 2f 2a 20 54 72 75 65  IsAgg    /* True
2a58c 20 69 66 20 74 68 65 20 73 75 62 71 75 65 72 79   if the subquery
2a58d 20 75 73 65 73 20 61 67 67 72 65 67 61 74 65 20   uses aggregate 
2a58e 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 29 7b 0a  functions */.){.
2a58f 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
2a590 61 76 65 64 41 75 74 68 43 6f 6e 74 65 78 74 20  avedAuthContext 
2a591 3d 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43  = pParse->zAuthC
2a592 6f 6e 74 65 78 74 3b 0a 20 20 53 65 6c 65 63 74  ontext;.  Select
2a593 20 2a 70 50 61 72 65 6e 74 3b 0a 20 20 53 65 6c   *pParent;.  Sel
2a594 65 63 74 20 2a 70 53 75 62 3b 20 20 20 20 20 20  ect *pSub;      
2a595 20 2f 2a 20 54 68 65 20 69 6e 6e 65 72 20 71 75   /* The inner qu
2a596 65 72 79 20 6f 72 20 22 73 75 62 71 75 65 72 79  ery or "subquery
2a597 22 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  " */.  Select *p
2a598 53 75 62 31 3b 20 20 20 20 20 20 2f 2a 20 50 6f  Sub1;      /* Po
2a599 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 69 67  inter to the rig
2a59a 68 74 6d 6f 73 74 20 73 65 6c 65 63 74 20 69 6e  htmost select in
2a59b 20 73 75 62 2d 71 75 65 72 79 20 2a 2f 0a 20 20   sub-query */.  
2a59c 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 20 20  SrcList *pSrc;  
2a59d 20 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20      /* The FROM 
2a59e 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 6f 75  clause of the ou
2a59f 74 65 72 20 71 75 65 72 79 20 2a 2f 0a 20 20 53  ter query */.  S
2a5a0 72 63 4c 69 73 74 20 2a 70 53 75 62 53 72 63 3b  rcList *pSubSrc;
2a5a1 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63     /* The FROM c
2a5a2 6c 61 75 73 65 20 6f 66 20 74 68 65 20 73 75 62  lause of the sub
2a5a3 71 75 65 72 79 20 2a 2f 0a 20 20 45 78 70 72 4c  query */.  ExprL
2a5a4 69 73 74 20 2a 70 4c 69 73 74 3b 20 20 20 20 2f  ist *pList;    /
2a5a5 2a 20 54 68 65 20 72 65 73 75 6c 74 20 73 65 74  * The result set
2a5a6 20 6f 66 20 74 68 65 20 6f 75 74 65 72 20 71 75   of the outer qu
2a5a7 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61  ery */.  int iPa
2a5a8 72 65 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20  rent;        /* 
2a5a9 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62  VDBE cursor numb
2a5aa 65 72 20 6f 66 20 74 68 65 20 70 53 75 62 20 72  er of the pSub r
2a5ab 65 73 75 6c 74 20 73 65 74 20 74 65 6d 70 20 74  esult set temp t
2a5ac 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  able */.  int i;
2a5ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a5ae 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
2a5af 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b  .  Expr *pWhere;
2a5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a5b1 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45      /* The WHERE
2a5b2 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72   clause */.  str
2a5b3 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
2a5b4 20 2a 70 53 75 62 69 74 65 6d 3b 20 20 20 2f 2a   *pSubitem;   /*
2a5b5 20 54 68 65 20 73 75 62 71 75 65 72 79 20 2a 2f   The subquery */
2a5b6 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2a5b7 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20   pParse->db;..  
2a5b8 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  /* Check to see 
2a5b9 69 66 20 66 6c 61 74 74 65 6e 69 6e 67 20 69 73  if flattening is
2a5ba 20 70 65 72 6d 69 74 74 65 64 2e 20 20 52 65 74   permitted.  Ret
2a5bb 75 72 6e 20 30 20 69 66 20 6e 6f 74 2e 0a 20 20  urn 0 if not..  
2a5bc 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d  */.  assert( p!=
2a5bd 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
2a5be 2d 3e 70 50 72 69 6f 72 3d 3d 30 20 29 3b 20 20  ->pPrior==0 );  
2a5bf 2f 2a 20 55 6e 61 62 6c 65 20 74 6f 20 66 6c 61  /* Unable to fla
2a5c0 74 74 65 6e 20 63 6f 6d 70 6f 75 6e 64 20 71 75  tten compound qu
2a5c1 65 72 69 65 73 20 2a 2f 0a 20 20 70 53 72 63 20  eries */.  pSrc 
2a5c2 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 61 73 73  = p->pSrc;.  ass
2a5c3 65 72 74 28 20 70 53 72 63 20 26 26 20 69 46 72  ert( pSrc && iFr
2a5c4 6f 6d 3e 3d 30 20 26 26 20 69 46 72 6f 6d 3c 70  om>=0 && iFrom<p
2a5c5 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 20 20 70  Src->nSrc );.  p
2a5c6 53 75 62 69 74 65 6d 20 3d 20 26 70 53 72 63 2d  Subitem = &pSrc-
2a5c7 3e 61 5b 69 46 72 6f 6d 5d 3b 0a 20 20 69 50 61  >a[iFrom];.  iPa
2a5c8 72 65 6e 74 20 3d 20 70 53 75 62 69 74 65 6d 2d  rent = pSubitem-
2a5c9 3e 69 43 75 72 73 6f 72 3b 0a 20 20 70 53 75 62  >iCursor;.  pSub
2a5ca 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 70 53 65   = pSubitem->pSe
2a5cb 6c 65 63 74 3b 0a 20 20 61 73 73 65 72 74 28 20  lect;.  assert( 
2a5cc 70 53 75 62 21 3d 30 20 29 3b 0a 20 20 69 66 28  pSub!=0 );.  if(
2a5cd 20 69 73 41 67 67 20 26 26 20 73 75 62 71 75 65   isAgg && subque
2a5ce 72 79 49 73 41 67 67 20 29 20 72 65 74 75 72 6e  ryIsAgg ) return
2a5cf 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
2a5d0 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69      /* Restricti
2a5d1 6f 6e 20 28 31 29 20 20 2a 2f 0a 20 20 69 66 28  on (1)  */.  if(
2a5d2 20 73 75 62 71 75 65 72 79 49 73 41 67 67 20 26   subqueryIsAgg &
2a5d3 26 20 70 53 72 63 2d 3e 6e 53 72 63 3e 31 20 29  & pSrc->nSrc>1 )
2a5d4 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20   return 0;      
2a5d5 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69      /* Restricti
2a5d6 6f 6e 20 28 32 29 20 20 2a 2f 0a 20 20 70 53 75  on (2)  */.  pSu
2a5d7 62 53 72 63 20 3d 20 70 53 75 62 2d 3e 70 53 72  bSrc = pSub->pSr
2a5d8 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 75  c;.  assert( pSu
2a5d9 62 53 72 63 20 29 3b 0a 20 20 2f 2a 20 50 72 69  bSrc );.  /* Pri
2a5da 6f 72 20 74 6f 20 76 65 72 73 69 6f 6e 20 33 2e  or to version 3.
2a5db 31 2e 32 2c 20 77 68 65 6e 20 4c 49 4d 49 54 20  1.2, when LIMIT 
2a5dc 61 6e 64 20 4f 46 46 53 45 54 20 68 61 64 20 74  and OFFSET had t
2a5dd 6f 20 62 65 20 73 69 6d 70 6c 65 20 63 6f 6e 73  o be simple cons
2a5de 74 61 6e 74 73 2c 0a 20 20 2a 2a 20 6e 6f 74 20  tants,.  ** not 
2a5df 61 72 62 69 74 72 61 72 79 20 65 78 70 72 65 73  arbitrary expres
2a5e0 73 73 69 6f 6e 73 2c 20 77 65 20 61 6c 6c 6f 77  ssions, we allow
2a5e1 65 64 20 73 6f 6d 65 20 63 6f 6d 62 69 6e 69 6e  ed some combinin
2a5e2 67 20 6f 66 20 4c 49 4d 49 54 20 61 6e 64 20 4f  g of LIMIT and O
2a5e3 46 46 53 45 54 0a 20 20 2a 2a 20 62 65 63 61 75  FFSET.  ** becau
2a5e4 73 65 20 74 68 65 79 20 63 6f 75 6c 64 20 62 65  se they could be
2a5e5 20 63 6f 6d 70 75 74 65 64 20 61 74 20 63 6f 6d   computed at com
2a5e6 70 69 6c 65 2d 74 69 6d 65 2e 20 20 42 75 74 20  pile-time.  But 
2a5e7 77 68 65 6e 20 4c 49 4d 49 54 20 61 6e 64 20 4f  when LIMIT and O
2a5e8 46 46 53 45 54 0a 20 20 2a 2a 20 62 65 63 61 6d  FFSET.  ** becam
2a5e9 65 20 61 72 62 69 74 72 61 72 79 20 65 78 70 72  e arbitrary expr
2a5ea 65 73 73 69 6f 6e 73 2c 20 77 65 20 77 65 72 65  essions, we were
2a5eb 20 66 6f 72 63 65 64 20 74 6f 20 61 64 64 20 72   forced to add r
2a5ec 65 73 74 72 69 63 74 69 6f 6e 73 20 28 31 33 29  estrictions (13)
2a5ed 0a 20 20 2a 2a 20 61 6e 64 20 28 31 34 29 2e 20  .  ** and (14). 
2a5ee 2a 2f 0a 20 20 69 66 28 20 70 53 75 62 2d 3e 70  */.  if( pSub->p
2a5ef 4c 69 6d 69 74 20 26 26 20 70 2d 3e 70 4c 69 6d  Limit && p->pLim
2a5f0 69 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20  it ) return 0;  
2a5f1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
2a5f2 65 73 74 72 69 63 74 69 6f 6e 20 28 31 33 29 20  estriction (13) 
2a5f3 2a 2f 0a 20 20 69 66 28 20 70 53 75 62 2d 3e 70  */.  if( pSub->p
2a5f4 4f 66 66 73 65 74 20 29 20 72 65 74 75 72 6e 20  Offset ) return 
2a5f5 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
2a5f6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
2a5f7 65 73 74 72 69 63 74 69 6f 6e 20 28 31 34 29 20  estriction (14) 
2a5f8 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 52 69 67  */.  if( p->pRig
2a5f9 68 74 6d 6f 73 74 20 26 26 20 70 53 75 62 2d 3e  htmost && pSub->
2a5fa 70 4c 69 6d 69 74 20 26 26 20 70 53 75 62 2d 3e  pLimit && pSub->
2a5fb 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
2a5fc 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20  return 0;       
2a5fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a5fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a5ff 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74       /* Restrict
2a600 69 6f 6e 20 28 31 35 29 20 2a 2f 0a 20 20 7d 0a  ion (15) */.  }.
2a601 20 20 69 66 28 20 70 53 75 62 53 72 63 2d 3e 6e    if( pSubSrc->n
2a602 53 72 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  Src==0 ) return 
2a603 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
2a604 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74           /* Rest
2a605 72 69 63 74 69 6f 6e 20 28 37 29 20 20 2a 2f 0a  riction (7)  */.
2a606 20 20 69 66 28 20 28 28 70 53 75 62 2d 3e 73 65    if( ((pSub->se
2a607 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74  lFlags & SF_Dist
2a608 69 6e 63 74 29 21 3d 30 20 7c 7c 20 70 53 75 62  inct)!=0 || pSub
2a609 2d 3e 70 4c 69 6d 69 74 29 20 0a 20 20 20 20 20  ->pLimit) .     
2a60a 20 20 20 20 26 26 20 28 70 53 72 63 2d 3e 6e 53      && (pSrc->nS
2a60b 72 63 3e 31 20 7c 7c 20 69 73 41 67 67 29 20 29  rc>1 || isAgg) )
2a60c 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  {          /* Re
2a60d 73 74 72 69 63 74 69 6f 6e 73 20 28 34 29 28 35  strictions (4)(5
2a60e 29 28 38 29 28 39 29 20 2a 2f 0a 20 20 20 20 20  )(8)(9) */.     
2a60f 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20  return 0;       
2a610 0a 20 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e 73  .  }.  if( (p->s
2a611 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73  elFlags & SF_Dis
2a612 74 69 6e 63 74 29 21 3d 30 20 26 26 20 73 75 62  tinct)!=0 && sub
2a613 71 75 65 72 79 49 73 41 67 67 20 29 7b 0a 20 20  queryIsAgg ){.  
2a614 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20     return 0;    
2a615 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74       /* Restrict
2a616 69 6f 6e 20 28 36 29 20 20 2a 2f 0a 20 20 7d 0a  ion (6)  */.  }.
2a617 20 20 69 66 28 20 70 2d 3e 70 4f 72 64 65 72 42    if( p->pOrderB
2a618 79 20 26 26 20 70 53 75 62 2d 3e 70 4f 72 64 65  y && pSub->pOrde
2a619 72 42 79 20 29 7b 0a 20 20 20 20 20 72 65 74 75  rBy ){.     retu
2a61a 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20  rn 0;           
2a61b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a61c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a61d 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28  /* Restriction (
2a61e 31 31 29 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28  11) */.  }.  if(
2a61f 20 69 73 41 67 67 20 26 26 20 70 53 75 62 2d 3e   isAgg && pSub->
2a620 70 4f 72 64 65 72 42 79 20 29 20 72 65 74 75 72  pOrderBy ) retur
2a621 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  n 0;            
2a622 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69      /* Restricti
2a623 6f 6e 20 28 31 36 29 20 2a 2f 0a 20 20 69 66 28  on (16) */.  if(
2a624 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20 26 26   pSub->pLimit &&
2a625 20 70 2d 3e 70 57 68 65 72 65 20 29 20 72 65 74   p->pWhere ) ret
2a626 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20  urn 0;          
2a627 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69      /* Restricti
2a628 6f 6e 20 28 31 39 29 20 2a 2f 0a 0a 20 20 2f 2a  on (19) */..  /*
2a629 20 4f 42 53 4f 4c 45 54 45 20 43 4f 4d 4d 45 4e   OBSOLETE COMMEN
2a62a 54 20 31 3a 0a 20 20 2a 2a 20 52 65 73 74 72 69  T 1:.  ** Restri
2a62b 63 74 69 6f 6e 20 33 3a 20 20 49 66 20 74 68 65  ction 3:  If the
2a62c 20 73 75 62 71 75 65 72 79 20 69 73 20 61 20 6a   subquery is a j
2a62d 6f 69 6e 2c 20 6d 61 6b 65 20 73 75 72 65 20 74  oin, make sure t
2a62e 68 65 20 73 75 62 71 75 65 72 79 20 69 73 20 0a  he subquery is .
2a62f 20 20 2a 2a 20 6e 6f 74 20 75 73 65 64 20 61 73    ** not used as
2a630 20 74 68 65 20 72 69 67 68 74 20 6f 70 65 72 61   the right opera
2a631 6e 64 20 6f 66 20 61 6e 20 6f 75 74 65 72 20 6a  nd of an outer j
2a632 6f 69 6e 2e 20 20 45 78 61 6d 70 6c 65 73 20 6f  oin.  Examples o
2a633 66 20 77 68 79 20 74 68 69 73 0a 20 20 2a 2a 20  f why this.  ** 
2a634 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 3a 0a  is not allowed:.
2a635 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20    **.  **       
2a636 20 20 74 31 20 4c 45 46 54 20 4f 55 54 45 52 20    t1 LEFT OUTER 
2a637 4a 4f 49 4e 20 28 74 32 20 4a 4f 49 4e 20 74 33  JOIN (t2 JOIN t3
2a638 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 77  ).  **.  ** If w
2a639 65 20 66 6c 61 74 74 65 6e 20 74 68 65 20 61 62  e flatten the ab
2a63a 6f 76 65 2c 20 77 65 20 77 6f 75 6c 64 20 67 65  ove, we would ge
2a63b 74 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  t.  **.  **     
2a63c 20 20 20 20 28 74 31 20 4c 45 46 54 20 4f 55 54      (t1 LEFT OUT
2a63d 45 52 20 4a 4f 49 4e 20 74 32 29 20 4a 4f 49 4e  ER JOIN t2) JOIN
2a63e 20 74 33 0a 20 20 2a 2a 0a 20 20 2a 2a 20 77 68   t3.  **.  ** wh
2a63f 69 63 68 20 69 73 20 6e 6f 74 20 61 74 20 61 6c  ich is not at al
2a640 6c 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67  l the same thing
2a641 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f 42 53 4f  ..  **.  ** OBSO
2a642 4c 45 54 45 20 43 4f 4d 4d 45 4e 54 20 32 3a 0a  LETE COMMENT 2:.
2a643 20 20 2a 2a 20 52 65 73 74 72 69 63 74 69 6f 6e    ** Restriction
2a644 20 31 32 3a 20 20 49 66 20 74 68 65 20 73 75 62   12:  If the sub
2a645 71 75 65 72 79 20 69 73 20 74 68 65 20 72 69 67  query is the rig
2a646 68 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 20  ht operand of a 
2a647 6c 65 66 74 20 6f 75 74 65 72 0a 20 20 2a 2a 20  left outer.  ** 
2a648 6a 6f 69 6e 2c 20 6d 61 6b 65 20 73 75 72 65 20  join, make sure 
2a649 74 68 65 20 73 75 62 71 75 65 72 79 20 68 61 73  the subquery has
2a64a 20 6e 6f 20 57 48 45 52 45 20 63 6c 61 75 73 65   no WHERE clause
2a64b 2e 0a 20 20 2a 2a 20 41 6e 20 65 78 61 6d 70 6c  ..  ** An exampl
2a64c 65 73 20 6f 66 20 77 68 79 20 74 68 69 73 20 69  es of why this i
2a64d 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 3a 0a 20  s not allowed:. 
2a64e 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20   **.  **        
2a64f 20 74 31 20 4c 45 46 54 20 4f 55 54 45 52 20 4a   t1 LEFT OUTER J
2a650 4f 49 4e 20 28 53 45 4c 45 43 54 20 2a 20 46 52  OIN (SELECT * FR
2a651 4f 4d 20 74 32 20 57 48 45 52 45 20 74 32 2e 78  OM t2 WHERE t2.x
2a652 3e 30 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66  >0).  **.  ** If
2a653 20 77 65 20 66 6c 61 74 74 65 6e 20 74 68 65 20   we flatten the 
2a654 61 62 6f 76 65 2c 20 77 65 20 77 6f 75 6c 64 20  above, we would 
2a655 67 65 74 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  get.  **.  **   
2a656 20 20 20 20 20 20 28 74 31 20 4c 45 46 54 20 4f        (t1 LEFT O
2a657 55 54 45 52 20 4a 4f 49 4e 20 74 32 29 20 57 48  UTER JOIN t2) WH
2a658 45 52 45 20 74 32 2e 78 3e 30 0a 20 20 2a 2a 0a  ERE t2.x>0.  **.
2a659 20 20 2a 2a 20 42 75 74 20 74 68 65 20 74 32 2e    ** But the t2.
2a65a 78 3e 30 20 74 65 73 74 20 77 69 6c 6c 20 61 6c  x>0 test will al
2a65b 77 61 79 73 20 66 61 69 6c 20 6f 6e 20 61 20 4e  ways fail on a N
2a65c 55 4c 4c 20 72 6f 77 20 6f 66 20 74 32 2c 20 77  ULL row of t2, w
2a65d 68 69 63 68 0a 20 20 2a 2a 20 65 66 66 65 63 74  hich.  ** effect
2a65e 69 76 65 6c 79 20 63 6f 6e 76 65 72 74 73 20 74  ively converts t
2a65f 68 65 20 4f 55 54 45 52 20 4a 4f 49 4e 20 69 6e  he OUTER JOIN in
2a660 74 6f 20 61 6e 20 49 4e 4e 45 52 20 4a 4f 49 4e  to an INNER JOIN
2a661 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 48 49 53  ..  **.  ** THIS
2a662 20 4f 56 45 52 52 49 44 45 53 20 4f 42 53 4f 4c   OVERRIDES OBSOL
2a663 45 54 45 20 43 4f 4d 4d 45 4e 54 53 20 31 20 41  ETE COMMENTS 1 A
2a664 4e 44 20 32 20 41 42 4f 56 45 3a 0a 20 20 2a 2a  ND 2 ABOVE:.  **
2a665 20 54 69 63 6b 65 74 20 23 33 33 30 30 20 73 68   Ticket #3300 sh
2a666 6f 77 73 20 74 68 61 74 20 66 6c 61 74 74 65 6e  ows that flatten
2a667 69 6e 67 20 74 68 65 20 72 69 67 68 74 20 74 65  ing the right te
2a668 72 6d 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49  rm of a LEFT JOI
2a669 4e 0a 20 20 2a 2a 20 69 73 20 66 72 61 75 67 68  N.  ** is fraugh
2a66a 74 20 77 69 74 68 20 64 61 6e 67 65 72 2e 20 20  t with danger.  
2a66b 42 65 73 74 20 74 6f 20 61 76 6f 69 64 20 74 68  Best to avoid th
2a66c 65 20 77 68 6f 6c 65 20 74 68 69 6e 67 2e 20 20  e whole thing.  
2a66d 49 66 20 74 68 65 0a 20 20 2a 2a 20 73 75 62 71  If the.  ** subq
2a66e 75 65 72 79 20 69 73 20 74 68 65 20 72 69 67 68  uery is the righ
2a66f 74 20 74 65 72 6d 20 6f 66 20 61 20 4c 45 46 54  t term of a LEFT
2a670 20 4a 4f 49 4e 2c 20 74 68 65 6e 20 64 6f 20 6e   JOIN, then do n
2a671 6f 74 20 66 6c 61 74 74 65 6e 2e 0a 20 20 2a 2f  ot flatten..  */
2a672 0a 20 20 69 66 28 20 28 70 53 75 62 69 74 65 6d  .  if( (pSubitem
2a673 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f  ->jointype & JT_
2a674 4f 55 54 45 52 29 21 3d 30 20 29 7b 0a 20 20 20  OUTER)!=0 ){.   
2a675 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a   return 0;.  }..
2a676 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e    /* Restriction
2a677 20 31 37 3a 20 49 66 20 74 68 65 20 73 75 62 2d   17: If the sub-
2a678 71 75 65 72 79 20 69 73 20 61 20 63 6f 6d 70 6f  query is a compo
2a679 75 6e 64 20 53 45 4c 45 43 54 2c 20 74 68 65 6e  und SELECT, then
2a67a 20 69 74 20 6d 75 73 74 0a 20 20 2a 2a 20 75 73   it must.  ** us
2a67b 65 20 6f 6e 6c 79 20 74 68 65 20 55 4e 49 4f 4e  e only the UNION
2a67c 20 41 4c 4c 20 6f 70 65 72 61 74 6f 72 2e 20 41   ALL operator. A
2a67d 6e 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 73  nd none of the s
2a67e 69 6d 70 6c 65 20 73 65 6c 65 63 74 20 71 75 65  imple select que
2a67f 72 69 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 6d  ries.  ** that m
2a680 61 6b 65 20 75 70 20 74 68 65 20 63 6f 6d 70 6f  ake up the compo
2a681 75 6e 64 20 53 45 4c 45 43 54 20 61 72 65 20 61  und SELECT are a
2a682 6c 6c 6f 77 65 64 20 74 6f 20 62 65 20 61 67 67  llowed to be agg
2a683 72 65 67 61 74 65 20 6f 72 20 64 69 73 74 69 6e  regate or distin
2a684 63 74 0a 20 20 2a 2a 20 71 75 65 72 69 65 73 2e  ct.  ** queries.
2a685 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 75 62  .  */.  if( pSub
2a686 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20  ->pPrior ){.    
2a687 69 66 28 20 70 53 75 62 2d 3e 70 4f 72 64 65 72  if( pSub->pOrder
2a688 42 79 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  By ){.      retu
2a689 72 6e 20 30 3b 20 20 2f 2a 20 52 65 73 74 72 69  rn 0;  /* Restri
2a68a 63 74 69 6f 6e 20 32 30 20 2a 2f 0a 20 20 20 20  ction 20 */.    
2a68b 7d 0a 20 20 20 20 69 66 28 20 69 73 41 67 67 20  }.    if( isAgg 
2a68c 7c 7c 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20  || (p->selFlags 
2a68d 26 20 53 46 5f 44 69 73 74 69 6e 63 74 29 21 3d  & SF_Distinct)!=
2a68e 30 20 7c 7c 20 70 53 72 63 2d 3e 6e 53 72 63 21  0 || pSrc->nSrc!
2a68f 3d 31 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =1 ){.      retu
2a690 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 0;.    }.    
2a691 66 6f 72 28 70 53 75 62 31 3d 70 53 75 62 3b 20  for(pSub1=pSub; 
2a692 70 53 75 62 31 3b 20 70 53 75 62 31 3d 70 53 75  pSub1; pSub1=pSu
2a693 62 31 2d 3e 70 50 72 69 6f 72 29 7b 0a 20 20 20  b1->pPrior){.   
2a694 20 20 20 74 65 73 74 63 61 73 65 28 20 28 70 53     testcase( (pS
2a695 75 62 31 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20  ub1->selFlags & 
2a696 28 53 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f  (SF_Distinct|SF_
2a697 41 67 67 72 65 67 61 74 65 29 29 3d 3d 53 46 5f  Aggregate))==SF_
2a698 44 69 73 74 69 6e 63 74 20 29 3b 0a 20 20 20 20  Distinct );.    
2a699 20 20 74 65 73 74 63 61 73 65 28 20 28 70 53 75    testcase( (pSu
2a69a 62 31 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28  b1->selFlags & (
2a69b 53 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41  SF_Distinct|SF_A
2a69c 67 67 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 41  ggregate))==SF_A
2a69d 67 67 72 65 67 61 74 65 20 29 3b 0a 20 20 20 20  ggregate );.    
2a69e 20 20 69 66 28 20 28 70 53 75 62 31 2d 3e 73 65    if( (pSub1->se
2a69f 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73  lFlags & (SF_Dis
2a6a0 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61  tinct|SF_Aggrega
2a6a1 74 65 29 29 21 3d 30 0a 20 20 20 20 20 20 20 7c  te))!=0.       |
2a6a2 7c 20 28 70 53 75 62 31 2d 3e 70 50 72 69 6f 72  | (pSub1->pPrior
2a6a3 20 26 26 20 70 53 75 62 31 2d 3e 6f 70 21 3d 54   && pSub1->op!=T
2a6a4 4b 5f 41 4c 4c 29 20 0a 20 20 20 20 20 20 20 7c  K_ALL) .       |
2a6a5 7c 20 4e 45 56 45 52 28 70 53 75 62 31 2d 3e 70  | NEVER(pSub1->p
2a6a6 53 72 63 3d 3d 30 29 20 7c 7c 20 70 53 75 62 31  Src==0) || pSub1
2a6a7 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31 0a  ->pSrc->nSrc!=1.
2a6a8 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
2a6a9 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
2a6aa 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a   }.    }..    /*
2a6ab 20 52 65 73 74 72 69 63 74 69 6f 6e 20 31 38 2e   Restriction 18.
2a6ac 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70   */.    if( p->p
2a6ad 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20  OrderBy ){.     
2a6ae 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 66   int ii;.      f
2a6af 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 2d 3e 70  or(ii=0; ii<p->p
2a6b0 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20  OrderBy->nExpr; 
2a6b1 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69  ii++){.        i
2a6b2 66 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 2d 3e  f( p->pOrderBy->
2a6b3 61 5b 69 69 5d 2e 69 43 6f 6c 3d 3d 30 20 29 20  a[ii].iCol==0 ) 
2a6b4 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
2a6b5 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  }.    }.  }..  /
2a6b6 2a 2a 2a 2a 2a 20 49 66 20 77 65 20 72 65 61 63  ***** If we reac
2a6b7 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 66 6c  h this point, fl
2a6b8 61 74 74 65 6e 69 6e 67 20 69 73 20 70 65 72 6d  attening is perm
2a6b9 69 74 74 65 64 2e 20 2a 2a 2a 2a 2a 2f 0a 0a 20  itted. *****/.. 
2a6ba 20 2f 2a 20 41 75 74 68 6f 72 69 7a 65 20 74 68   /* Authorize th
2a6bb 65 20 73 75 62 71 75 65 72 79 20 2a 2f 0a 20 20  e subquery */.  
2a6bc 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e  pParse->zAuthCon
2a6bd 74 65 78 74 20 3d 20 70 53 75 62 69 74 65 6d 2d  text = pSubitem-
2a6be 3e 7a 4e 61 6d 65 3b 0a 20 20 73 71 6c 69 74 65  >zName;.  sqlite
2a6bf 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
2a6c0 65 2c 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54  e, SQLITE_SELECT
2a6c1 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 70 50  , 0, 0, 0);.  pP
2a6c2 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65  arse->zAuthConte
2a6c3 78 74 20 3d 20 7a 53 61 76 65 64 41 75 74 68 43  xt = zSavedAuthC
2a6c4 6f 6e 74 65 78 74 3b 0a 0a 20 20 2f 2a 20 49 66  ontext;..  /* If
2a6c5 20 74 68 65 20 73 75 62 2d 71 75 65 72 79 20 69   the sub-query i
2a6c6 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c  s a compound SEL
2a6c7 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2c 20 74  ECT statement, t
2a6c8 68 65 6e 20 28 62 79 20 72 65 73 74 72 69 63 74  hen (by restrict
2a6c9 69 6f 6e 73 0a 20 20 2a 2a 20 31 37 20 61 6e 64  ions.  ** 17 and
2a6ca 20 31 38 20 61 62 6f 76 65 29 20 69 74 20 6d 75   18 above) it mu
2a6cb 73 74 20 62 65 20 61 20 55 4e 49 4f 4e 20 41 4c  st be a UNION AL
2a6cc 4c 20 61 6e 64 20 74 68 65 20 70 61 72 65 6e 74  L and the parent
2a6cd 20 71 75 65 72 79 20 6d 75 73 74 20 0a 20 20 2a   query must .  *
2a6ce 2a 20 62 65 20 6f 66 20 74 68 65 20 66 6f 72 6d  * be of the form
2a6cf 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  :.  **.  **     
2a6d0 53 45 4c 45 43 54 20 3c 65 78 70 72 2d 6c 69 73  SELECT <expr-lis
2a6d1 74 3e 20 46 52 4f 4d 20 28 3c 73 75 62 2d 71 75  t> FROM (<sub-qu
2a6d2 65 72 79 3e 29 20 3c 77 68 65 72 65 2d 63 6c 61  ery>) <where-cla
2a6d3 75 73 65 3e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20  use> .  **.  ** 
2a6d4 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 6e 79 20  followed by any 
2a6d5 4f 52 44 45 52 20 42 59 2c 20 4c 49 4d 49 54 20  ORDER BY, LIMIT 
2a6d6 61 6e 64 2f 6f 72 20 4f 46 46 53 45 54 20 63 6c  and/or OFFSET cl
2a6d7 61 75 73 65 73 2e 20 54 68 69 73 20 62 6c 6f 63  auses. This bloc
2a6d8 6b 0a 20 20 2a 2a 20 63 72 65 61 74 65 73 20 4e  k.  ** creates N
2a6d9 2d 31 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65  -1 copies of the
2a6da 20 70 61 72 65 6e 74 20 71 75 65 72 79 20 77 69   parent query wi
2a6db 74 68 6f 75 74 20 61 6e 79 20 4f 52 44 45 52 20  thout any ORDER 
2a6dc 42 59 2c 20 4c 49 4d 49 54 20 6f 72 20 0a 20 20  BY, LIMIT or .  
2a6dd 2a 2a 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65  ** OFFSET clause
2a6de 73 20 61 6e 64 20 6a 6f 69 6e 73 20 74 68 65 6d  s and joins them
2a6df 20 74 6f 20 74 68 65 20 6c 65 66 74 2d 68 61 6e   to the left-han
2a6e0 64 2d 73 69 64 65 20 6f 66 20 74 68 65 20 6f 72  d-side of the or
2a6e1 69 67 69 6e 61 6c 0a 20 20 2a 2a 20 75 73 69 6e  iginal.  ** usin
2a6e2 67 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 70 65 72  g UNION ALL oper
2a6e3 61 74 6f 72 73 2e 20 49 6e 20 74 68 69 73 20 63  ators. In this c
2a6e4 61 73 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d  ase N is the num
2a6e5 62 65 72 20 6f 66 20 73 69 6d 70 6c 65 0a 20 20  ber of simple.  
2a6e6 2a 2a 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d  ** select statem
2a6e7 65 6e 74 73 20 69 6e 20 74 68 65 20 63 6f 6d 70  ents in the comp
2a6e8 6f 75 6e 64 20 73 75 62 2d 71 75 65 72 79 2e 0a  ound sub-query..
2a6e9 20 20 2a 2a 0a 20 20 2a 2a 20 45 78 61 6d 70 6c    **.  ** Exampl
2a6ea 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  e:.  **.  **    
2a6eb 20 53 45 4c 45 43 54 20 61 2b 31 20 46 52 4f 4d   SELECT a+1 FROM
2a6ec 20 28 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 53   (.  **        S
2a6ed 45 4c 45 43 54 20 78 20 46 52 4f 4d 20 74 61 62  ELECT x FROM tab
2a6ee 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 55 4e 49  .  **        UNI
2a6ef 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20 20 20 20 20  ON ALL.  **     
2a6f0 20 20 20 53 45 4c 45 43 54 20 79 20 46 52 4f 4d     SELECT y FROM
2a6f1 20 74 61 62 0a 20 20 2a 2a 20 20 20 20 20 20 20   tab.  **       
2a6f2 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20   UNION ALL.  ** 
2a6f3 20 20 20 20 20 20 20 53 45 4c 45 43 54 20 61 62         SELECT ab
2a6f4 73 28 7a 2a 32 29 20 46 52 4f 4d 20 74 61 62 32  s(z*2) FROM tab2
2a6f5 0a 20 20 2a 2a 20 20 20 20 20 29 20 57 48 45 52  .  **     ) WHER
2a6f6 45 20 61 21 3d 35 20 4f 52 44 45 52 20 42 59 20  E a!=5 ORDER BY 
2a6f7 31 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 72 61 6e  1.  **.  ** Tran
2a6f8 73 66 6f 72 6d 65 64 20 69 6e 74 6f 3a 0a 20 20  sformed into:.  
2a6f9 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45  **.  **     SELE
2a6fa 43 54 20 78 2b 31 20 46 52 4f 4d 20 74 61 62 20  CT x+1 FROM tab 
2a6fb 57 48 45 52 45 20 78 2b 31 21 3d 35 0a 20 20 2a  WHERE x+1!=5.  *
2a6fc 2a 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 0a  *     UNION ALL.
2a6fd 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20    **     SELECT 
2a6fe 79 2b 31 20 46 52 4f 4d 20 74 61 62 20 57 48 45  y+1 FROM tab WHE
2a6ff 52 45 20 79 2b 31 21 3d 35 0a 20 20 2a 2a 20 20  RE y+1!=5.  **  
2a700 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 2a     UNION ALL.  *
2a701 2a 20 20 20 20 20 53 45 4c 45 43 54 20 61 62 73  *     SELECT abs
2a702 28 7a 2a 32 29 2b 31 20 46 52 4f 4d 20 74 61 62  (z*2)+1 FROM tab
2a703 32 20 57 48 45 52 45 20 61 62 73 28 7a 2a 32 29  2 WHERE abs(z*2)
2a704 2b 31 21 3d 35 0a 20 20 2a 2a 20 20 20 20 20 4f  +1!=5.  **     O
2a705 52 44 45 52 20 42 59 20 31 0a 20 20 2a 2a 0a 20  RDER BY 1.  **. 
2a706 20 2a 2a 20 57 65 20 63 61 6c 6c 20 74 68 69 73   ** We call this
2a707 20 74 68 65 20 22 63 6f 6d 70 6f 75 6e 64 2d 73   the "compound-s
2a708 75 62 71 75 65 72 79 20 66 6c 61 74 74 65 6e 69  ubquery flatteni
2a709 6e 67 22 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  ng"..  */.  for(
2a70a 70 53 75 62 3d 70 53 75 62 2d 3e 70 50 72 69 6f  pSub=pSub->pPrio
2a70b 72 3b 20 70 53 75 62 3b 20 70 53 75 62 3d 70 53  r; pSub; pSub=pS
2a70c 75 62 2d 3e 70 50 72 69 6f 72 29 7b 0a 20 20 20  ub->pPrior){.   
2a70d 20 53 65 6c 65 63 74 20 2a 70 4e 65 77 3b 0a 20   Select *pNew;. 
2a70e 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72     ExprList *pOr
2a70f 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65  derBy = p->pOrde
2a710 72 42 79 3b 0a 20 20 20 20 45 78 70 72 20 2a 70  rBy;.    Expr *p
2a711 4c 69 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69  Limit = p->pLimi
2a712 74 3b 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70  t;.    Select *p
2a713 50 72 69 6f 72 20 3d 20 70 2d 3e 70 50 72 69 6f  Prior = p->pPrio
2a714 72 3b 0a 20 20 20 20 70 2d 3e 70 4f 72 64 65 72  r;.    p->pOrder
2a715 42 79 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70  By = 0;.    p->p
2a716 53 72 63 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e  Src = 0;.    p->
2a717 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20  pPrior = 0;.    
2a718 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20  p->pLimit = 0;. 
2a719 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65     pNew = sqlite
2a71a 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70  3SelectDup(db, p
2a71b 2c 20 30 29 3b 0a 20 20 20 20 70 2d 3e 70 4c 69  , 0);.    p->pLi
2a71c 6d 69 74 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20  mit = pLimit;.  
2a71d 20 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20    p->pOrderBy = 
2a71e 70 4f 72 64 65 72 42 79 3b 0a 20 20 20 20 70 2d  pOrderBy;.    p-
2a71f 3e 70 53 72 63 20 3d 20 70 53 72 63 3b 0a 20 20  >pSrc = pSrc;.  
2a720 20 20 70 2d 3e 6f 70 20 3d 20 54 4b 5f 41 4c 4c    p->op = TK_ALL
2a721 3b 0a 20 20 20 20 70 2d 3e 70 52 69 67 68 74 6d  ;.    p->pRightm
2a722 6f 73 74 20 3d 20 30 3b 0a 20 20 20 20 69 66 28  ost = 0;.    if(
2a723 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20   pNew==0 ){.    
2a724 20 20 70 4e 65 77 20 3d 20 70 50 72 69 6f 72 3b    pNew = pPrior;
2a725 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2a726 20 20 70 4e 65 77 2d 3e 70 50 72 69 6f 72 20 3d    pNew->pPrior =
2a727 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 70   pPrior;.      p
2a728 4e 65 77 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20  New->pRightmost 
2a729 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  = 0;.    }.    p
2a72a 2d 3e 70 50 72 69 6f 72 20 3d 20 70 4e 65 77 3b  ->pPrior = pNew;
2a72b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  .    if( db->mal
2a72c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75  locFailed ) retu
2a72d 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rn 1;.  }..  /* 
2a72e 42 65 67 69 6e 20 66 6c 61 74 74 65 6e 69 6e 67  Begin flattening
2a72f 20 74 68 65 20 69 46 72 6f 6d 2d 74 68 20 65 6e   the iFrom-th en
2a730 74 72 79 20 6f 66 20 74 68 65 20 46 52 4f 4d 20  try of the FROM 
2a731 63 6c 61 75 73 65 20 0a 20 20 2a 2a 20 69 6e 20  clause .  ** in 
2a732 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 2e  the outer query.
2a733 0a 20 20 2a 2f 0a 20 20 70 53 75 62 20 3d 20 70  .  */.  pSub = p
2a734 53 75 62 31 20 3d 20 70 53 75 62 69 74 65 6d 2d  Sub1 = pSubitem-
2a735 3e 70 53 65 6c 65 63 74 3b 0a 0a 20 20 2f 2a 20  >pSelect;..  /* 
2a736 44 65 6c 65 74 65 20 74 68 65 20 74 72 61 6e 73  Delete the trans
2a737 69 65 6e 74 20 74 61 62 6c 65 20 73 74 72 75 63  ient table struc
2a738 74 75 72 65 20 61 73 73 6f 63 69 61 74 65 64 20  ture associated 
2a739 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 73 75  with the.  ** su
2a73a 62 71 75 65 72 79 0a 20 20 2a 2f 0a 20 20 73 71  bquery.  */.  sq
2a73b 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2a73c 70 53 75 62 69 74 65 6d 2d 3e 7a 44 61 74 61 62  pSubitem->zDatab
2a73d 61 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ase);.  sqlite3D
2a73e 62 46 72 65 65 28 64 62 2c 20 70 53 75 62 69 74  bFree(db, pSubit
2a73f 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71  em->zName);.  sq
2a740 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2a741 70 53 75 62 69 74 65 6d 2d 3e 7a 41 6c 69 61 73  pSubitem->zAlias
2a742 29 3b 0a 20 20 70 53 75 62 69 74 65 6d 2d 3e 7a  );.  pSubitem->z
2a743 44 61 74 61 62 61 73 65 20 3d 20 30 3b 0a 20 20  Database = 0;.  
2a744 70 53 75 62 69 74 65 6d 2d 3e 7a 4e 61 6d 65 20  pSubitem->zName 
2a745 3d 20 30 3b 0a 20 20 70 53 75 62 69 74 65 6d 2d  = 0;.  pSubitem-
2a746 3e 7a 41 6c 69 61 73 20 3d 20 30 3b 0a 20 20 70  >zAlias = 0;.  p
2a747 53 75 62 69 74 65 6d 2d 3e 70 53 65 6c 65 63 74  Subitem->pSelect
2a748 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 44 65 66 65   = 0;..  /* Defe
2a749 72 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 54  r deleting the T
2a74a 61 62 6c 65 20 6f 62 6a 65 63 74 20 61 73 73 6f  able object asso
2a74b 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 0a  ciated with the.
2a74c 20 20 2a 2a 20 73 75 62 71 75 65 72 79 20 75 6e    ** subquery un
2a74d 74 69 6c 20 63 6f 64 65 20 67 65 6e 65 72 61 74  til code generat
2a74e 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 63 6f 6d 70  ion is.  ** comp
2a74f 6c 65 74 65 2c 20 73 69 6e 63 65 20 74 68 65 72  lete, since ther
2a750 65 20 6d 61 79 20 73 74 69 6c 6c 20 65 78 69 73  e may still exis
2a751 74 20 45 78 70 72 2e 70 54 61 62 20 65 6e 74 72  t Expr.pTab entr
2a752 69 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 72 65  ies that.  ** re
2a753 66 65 72 20 74 6f 20 74 68 65 20 73 75 62 71 75  fer to the subqu
2a754 65 72 79 20 65 76 65 6e 20 61 66 74 65 72 20 66  ery even after f
2a755 6c 61 74 74 65 6e 69 6e 67 2e 20 20 54 69 63 6b  lattening.  Tick
2a756 65 74 20 23 33 33 34 36 2e 0a 20 20 2a 2a 0a 20  et #3346..  **. 
2a757 20 2a 2a 20 70 53 75 62 69 74 65 6d 2d 3e 70 54   ** pSubitem->pT
2a758 61 62 20 69 73 20 61 6c 77 61 79 73 20 6e 6f 6e  ab is always non
2a759 2d 4e 55 4c 4c 20 62 79 20 74 65 73 74 20 72 65  -NULL by test re
2a75a 73 74 72 69 63 74 69 6f 6e 73 20 61 6e 64 20 74  strictions and t
2a75b 65 73 74 73 20 61 62 6f 76 65 2e 0a 20 20 2a 2f  ests above..  */
2a75c 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 53  .  if( ALWAYS(pS
2a75d 75 62 69 74 65 6d 2d 3e 70 54 61 62 21 3d 30 29  ubitem->pTab!=0)
2a75e 20 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70   ){.    Table *p
2a75f 54 61 62 54 6f 44 65 6c 20 3d 20 70 53 75 62 69  TabToDel = pSubi
2a760 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69  tem->pTab;.    i
2a761 66 28 20 70 54 61 62 54 6f 44 65 6c 2d 3e 6e 52  f( pTabToDel->nR
2a762 65 66 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 50  ef==1 ){.      P
2a763 61 72 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20  arse *pToplevel 
2a764 3d 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f  = sqlite3ParseTo
2a765 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29 3b 0a  plevel(pParse);.
2a766 20 20 20 20 20 20 70 54 61 62 54 6f 44 65 6c 2d        pTabToDel-
2a767 3e 70 4e 65 78 74 5a 6f 6d 62 69 65 20 3d 20 70  >pNextZombie = p
2a768 54 6f 70 6c 65 76 65 6c 2d 3e 70 5a 6f 6d 62 69  Toplevel->pZombi
2a769 65 54 61 62 3b 0a 20 20 20 20 20 20 70 54 6f 70  eTab;.      pTop
2a76a 6c 65 76 65 6c 2d 3e 70 5a 6f 6d 62 69 65 54 61  level->pZombieTa
2a76b 62 20 3d 20 70 54 61 62 54 6f 44 65 6c 3b 0a 20  b = pTabToDel;. 
2a76c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2a76d 70 54 61 62 54 6f 44 65 6c 2d 3e 6e 52 65 66 2d  pTabToDel->nRef-
2a76e 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 75  -;.    }.    pSu
2a76f 62 69 74 65 6d 2d 3e 70 54 61 62 20 3d 20 30 3b  bitem->pTab = 0;
2a770 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66  .  }..  /* The f
2a771 6f 6c 6c 6f 77 69 6e 67 20 6c 6f 6f 70 20 72 75  ollowing loop ru
2a772 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ns once for each
2a773 20 74 65 72 6d 20 69 6e 20 61 20 63 6f 6d 70 6f   term in a compo
2a774 75 6e 64 2d 73 75 62 71 75 65 72 79 0a 20 20 2a  und-subquery.  *
2a775 2a 20 66 6c 61 74 74 65 6e 69 6e 67 20 28 61 73  * flattening (as
2a776 20 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65   described above
2a777 29 2e 20 20 49 66 20 77 65 20 61 72 65 20 64 6f  ).  If we are do
2a778 69 6e 67 20 61 20 64 69 66 66 65 72 65 6e 74 20  ing a different 
2a779 6b 69 6e 64 0a 20 20 2a 2a 20 6f 66 20 66 6c 61  kind.  ** of fla
2a77a 74 74 65 6e 69 6e 67 20 2d 20 61 20 66 6c 61 74  ttening - a flat
2a77b 74 65 6e 69 6e 67 20 6f 74 68 65 72 20 74 68 61  tening other tha
2a77c 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 2d 73 75 62  n a compound-sub
2a77d 71 75 65 72 79 20 66 6c 61 74 74 65 6e 69 6e 67  query flattening
2a77e 20 2d 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 69   -.  ** then thi
2a77f 73 20 6c 6f 6f 70 20 6f 6e 6c 79 20 72 75 6e 73  s loop only runs
2a780 20 6f 6e 63 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a   once..  **.  **
2a781 20 54 68 69 73 20 6c 6f 6f 70 20 6d 6f 76 65 73   This loop moves
2a782 20 61 6c 6c 20 6f 66 20 74 68 65 20 46 52 4f 4d   all of the FROM
2a783 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65   elements of the
2a784 20 73 75 62 71 75 65 72 79 20 69 6e 74 6f 20 74   subquery into t
2a785 68 65 0a 20 20 2a 2a 20 74 68 65 20 46 52 4f 4d  he.  ** the FROM
2a786 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 6f   clause of the o
2a787 75 74 65 72 20 71 75 65 72 79 2e 20 20 42 65 66  uter query.  Bef
2a788 6f 72 65 20 64 6f 69 6e 67 20 74 68 69 73 2c 20  ore doing this, 
2a789 72 65 6d 65 6d 62 65 72 0a 20 20 2a 2a 20 74 68  remember.  ** th
2a78a 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  e cursor number 
2a78b 66 6f 72 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  for the original
2a78c 20 6f 75 74 65 72 20 71 75 65 72 79 20 46 52 4f   outer query FRO
2a78d 4d 20 65 6c 65 6d 65 6e 74 20 69 6e 0a 20 20 2a  M element in.  *
2a78e 2a 20 69 50 61 72 65 6e 74 2e 20 20 54 68 65 20  * iParent.  The 
2a78f 69 50 61 72 65 6e 74 20 63 75 72 73 6f 72 20 77  iParent cursor w
2a790 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 75 73 65  ill never be use
2a791 64 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20 63  d.  Subsequent c
2a792 6f 64 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 73 63  ode.  ** will sc
2a793 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6c  an expressions l
2a794 6f 6f 6b 69 6e 67 20 66 6f 72 20 69 50 61 72 65  ooking for iPare
2a795 6e 74 20 72 65 66 65 72 65 6e 63 65 73 20 61 6e  nt references an
2a796 64 20 72 65 70 6c 61 63 65 0a 20 20 2a 2a 20 74  d replace.  ** t
2a797 68 6f 73 65 20 72 65 66 65 72 65 6e 63 65 73 20  hose references 
2a798 77 69 74 68 20 65 78 70 72 65 73 73 69 6f 6e 73  with expressions
2a799 20 74 68 61 74 20 72 65 73 6f 6c 76 65 20 74 6f   that resolve to
2a79a 20 74 68 65 20 73 75 62 71 75 65 72 79 20 46 52   the subquery FR
2a79b 4f 4d 0a 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 73  OM.  ** elements
2a79c 20 77 65 20 61 72 65 20 6e 6f 77 20 63 6f 70 79   we are now copy
2a79d 69 6e 67 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 66  ing in..  */.  f
2a79e 6f 72 28 70 50 61 72 65 6e 74 3d 70 3b 20 70 50  or(pParent=p; pP
2a79f 61 72 65 6e 74 3b 20 70 50 61 72 65 6e 74 3d 70  arent; pParent=p
2a7a0 50 61 72 65 6e 74 2d 3e 70 50 72 69 6f 72 2c 20  Parent->pPrior, 
2a7a1 70 53 75 62 3d 70 53 75 62 2d 3e 70 50 72 69 6f  pSub=pSub->pPrio
2a7a2 72 29 7b 0a 20 20 20 20 69 6e 74 20 6e 53 75 62  r){.    int nSub
2a7a3 53 72 63 3b 0a 20 20 20 20 75 38 20 6a 6f 69 6e  Src;.    u8 join
2a7a4 74 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 70 53  type = 0;.    pS
2a7a5 75 62 53 72 63 20 3d 20 70 53 75 62 2d 3e 70 53  ubSrc = pSub->pS
2a7a6 72 63 3b 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20  rc;     /* FROM 
2a7a7 63 6c 61 75 73 65 20 6f 66 20 73 75 62 71 75 65  clause of subque
2a7a8 72 79 20 2a 2f 0a 20 20 20 20 6e 53 75 62 53 72  ry */.    nSubSr
2a7a9 63 20 3d 20 70 53 75 62 53 72 63 2d 3e 6e 53 72  c = pSubSrc->nSr
2a7aa 63 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66  c;  /* Number of
2a7ab 20 74 65 72 6d 73 20 69 6e 20 73 75 62 71 75 65   terms in subque
2a7ac 72 79 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a  ry FROM clause *
2a7ad 2f 0a 20 20 20 20 70 53 72 63 20 3d 20 70 50 61  /.    pSrc = pPa
2a7ae 72 65 6e 74 2d 3e 70 53 72 63 3b 20 20 20 20 20  rent->pSrc;     
2a7af 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  /* FROM clause o
2a7b0 66 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  f the outer quer
2a7b1 79 20 2a 2f 0a 0a 20 20 20 20 69 66 28 20 70 53  y */..    if( pS
2a7b2 72 63 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  rc ){.      asse
2a7b3 72 74 28 20 70 50 61 72 65 6e 74 3d 3d 70 20 29  rt( pParent==p )
2a7b4 3b 20 20 2f 2a 20 46 69 72 73 74 20 74 69 6d 65  ;  /* First time
2a7b5 20 74 68 72 6f 75 67 68 20 74 68 65 20 6c 6f 6f   through the loo
2a7b6 70 20 2a 2f 0a 20 20 20 20 20 20 6a 6f 69 6e 74  p */.      joint
2a7b7 79 70 65 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e  ype = pSubitem->
2a7b8 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 20 20 7d 65  jointype;.    }e
2a7b9 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72  lse{.      asser
2a7ba 74 28 20 70 50 61 72 65 6e 74 21 3d 70 20 29 3b  t( pParent!=p );
2a7bb 20 20 2f 2a 20 32 6e 64 20 61 6e 64 20 73 75 62    /* 2nd and sub
2a7bc 73 65 71 75 65 6e 74 20 74 69 6d 65 73 20 74 68  sequent times th
2a7bd 72 6f 75 67 68 20 74 68 65 20 6c 6f 6f 70 20 2a  rough the loop *
2a7be 2f 0a 20 20 20 20 20 20 70 53 72 63 20 3d 20 70  /.      pSrc = p
2a7bf 50 61 72 65 6e 74 2d 3e 70 53 72 63 20 3d 20 73  Parent->pSrc = s
2a7c0 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
2a7c1 65 6e 64 28 64 62 2c 20 30 2c 20 30 2c 20 30 29  end(db, 0, 0, 0)
2a7c2 3b 0a 20 20 20 20 20 20 69 66 28 20 70 53 72 63  ;.      if( pSrc
2a7c3 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61  ==0 ){.        a
2a7c4 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f  ssert( db->mallo
2a7c5 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20  cFailed );.     
2a7c6 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2a7c7 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
2a7c8 54 68 65 20 73 75 62 71 75 65 72 79 20 75 73 65  The subquery use
2a7c9 73 20 61 20 73 69 6e 67 6c 65 20 73 6c 6f 74 20  s a single slot 
2a7ca 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  of the FROM clau
2a7cb 73 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72 0a  se of the outer.
2a7cc 20 20 20 20 2a 2a 20 71 75 65 72 79 2e 20 20 49      ** query.  I
2a7cd 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20 68  f the subquery h
2a7ce 61 73 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  as more than one
2a7cf 20 65 6c 65 6d 65 6e 74 20 69 6e 20 69 74 73 20   element in its 
2a7d0 46 52 4f 4d 20 63 6c 61 75 73 65 2c 0a 20 20 20  FROM clause,.   
2a7d1 20 2a 2a 20 74 68 65 6e 20 65 78 70 61 6e 64 20   ** then expand 
2a7d2 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
2a7d3 74 6f 20 6d 61 6b 65 20 73 70 61 63 65 20 66 6f  to make space fo
2a7d4 72 20 69 74 20 74 6f 20 68 6f 6c 64 20 61 6c 6c  r it to hold all
2a7d5 20 65 6c 65 6d 65 6e 74 73 0a 20 20 20 20 2a 2a   elements.    **
2a7d6 20 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79   of the subquery
2a7d7 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
2a7d8 45 78 61 6d 70 6c 65 3a 0a 20 20 20 20 2a 2a 0a  Example:.    **.
2a7d9 20 20 20 20 2a 2a 20 20 20 20 53 45 4c 45 43 54      **    SELECT
2a7da 20 2a 20 46 52 4f 4d 20 74 61 62 41 2c 20 28 53   * FROM tabA, (S
2a7db 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 73 75 62  ELECT * FROM sub
2a7dc 31 2c 20 73 75 62 32 29 2c 20 74 61 62 42 3b 0a  1, sub2), tabB;.
2a7dd 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
2a7de 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 68 61  e outer query ha
2a7df 73 20 33 20 73 6c 6f 74 73 20 69 6e 20 69 74 73  s 3 slots in its
2a7e0 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 4f   FROM clause.  O
2a7e1 6e 65 20 73 6c 6f 74 20 6f 66 20 74 68 65 0a 20  ne slot of the. 
2a7e2 20 20 20 2a 2a 20 6f 75 74 65 72 20 71 75 65 72     ** outer quer
2a7e3 79 20 28 74 68 65 20 6d 69 64 64 6c 65 20 73 6c  y (the middle sl
2a7e4 6f 74 29 20 69 73 20 75 73 65 64 20 62 79 20 74  ot) is used by t
2a7e5 68 65 20 73 75 62 71 75 65 72 79 2e 20 20 54 68  he subquery.  Th
2a7e6 65 20 6e 65 78 74 0a 20 20 20 20 2a 2a 20 62 6c  e next.    ** bl
2a7e7 6f 63 6b 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c  ock of code will
2a7e8 20 65 78 70 61 6e 64 20 74 68 65 20 6f 75 74 20   expand the out 
2a7e9 71 75 65 72 79 20 74 6f 20 34 20 73 6c 6f 74 73  query to 4 slots
2a7ea 2e 20 20 54 68 65 20 6d 69 64 64 6c 65 0a 20 20  .  The middle.  
2a7eb 20 20 2a 2a 20 73 6c 6f 74 20 69 73 20 65 78 70    ** slot is exp
2a7ec 61 6e 64 65 64 20 74 6f 20 74 77 6f 20 73 6c 6f  anded to two slo
2a7ed 74 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d  ts in order to m
2a7ee 61 6b 65 20 73 70 61 63 65 20 66 6f 72 20 74 68  ake space for th
2a7ef 65 0a 20 20 20 20 2a 2a 20 74 77 6f 20 65 6c 65  e.    ** two ele
2a7f0 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 46 52 4f  ments in the FRO
2a7f1 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20  M clause of the 
2a7f2 73 75 62 71 75 65 72 79 2e 0a 20 20 20 20 2a 2f  subquery..    */
2a7f3 0a 20 20 20 20 69 66 28 20 6e 53 75 62 53 72 63  .    if( nSubSrc
2a7f4 3e 31 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72  >1 ){.      pPar
2a7f5 65 6e 74 2d 3e 70 53 72 63 20 3d 20 70 53 72 63  ent->pSrc = pSrc
2a7f6 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
2a7f7 74 45 6e 6c 61 72 67 65 28 64 62 2c 20 70 53 72  tEnlarge(db, pSr
2a7f8 63 2c 20 6e 53 75 62 53 72 63 2d 31 2c 69 46 72  c, nSubSrc-1,iFr
2a7f9 6f 6d 2b 31 29 3b 0a 20 20 20 20 20 20 69 66 28  om+1);.      if(
2a7fa 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2a7fb 64 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65  d ){.        bre
2a7fc 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
2a7fd 7d 0a 0a 20 20 20 20 2f 2a 20 54 72 61 6e 73 66  }..    /* Transf
2a7fe 65 72 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  er the FROM clau
2a7ff 73 65 20 74 65 72 6d 73 20 66 72 6f 6d 20 74 68  se terms from th
2a800 65 20 73 75 62 71 75 65 72 79 20 69 6e 74 6f 20  e subquery into 
2a801 74 68 65 0a 20 20 20 20 2a 2a 20 6f 75 74 65 72  the.    ** outer
2a802 20 71 75 65 72 79 2e 0a 20 20 20 20 2a 2f 0a 20   query..    */. 
2a803 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 53     for(i=0; i<nS
2a804 75 62 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20  ubSrc; i++){.   
2a805 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74     sqlite3IdList
2a806 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72 63 2d  Delete(db, pSrc-
2a807 3e 61 5b 69 2b 69 46 72 6f 6d 5d 2e 70 55 73 69  >a[i+iFrom].pUsi
2a808 6e 67 29 3b 0a 20 20 20 20 20 20 70 53 72 63 2d  ng);.      pSrc-
2a809 3e 61 5b 69 2b 69 46 72 6f 6d 5d 20 3d 20 70 53  >a[i+iFrom] = pS
2a80a 75 62 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20  ubSrc->a[i];.   
2a80b 20 20 20 6d 65 6d 73 65 74 28 26 70 53 75 62 53     memset(&pSubS
2a80c 72 63 2d 3e 61 5b 69 5d 2c 20 30 2c 20 73 69 7a  rc->a[i], 0, siz
2a80d 65 6f 66 28 70 53 75 62 53 72 63 2d 3e 61 5b 69  eof(pSubSrc->a[i
2a80e 5d 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ]));.    }.    p
2a80f 53 72 63 2d 3e 61 5b 69 46 72 6f 6d 5d 2e 6a 6f  Src->a[iFrom].jo
2a810 69 6e 74 79 70 65 20 3d 20 6a 6f 69 6e 74 79 70  intype = jointyp
2a811 65 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 4e 6f 77  e;.  .    /* Now
2a812 20 62 65 67 69 6e 20 73 75 62 73 74 69 74 75 74   begin substitut
2a813 69 6e 67 20 73 75 62 71 75 65 72 79 20 72 65 73  ing subquery res
2a814 75 6c 74 20 73 65 74 20 65 78 70 72 65 73 73 69  ult set expressi
2a815 6f 6e 73 20 66 6f 72 20 0a 20 20 20 20 2a 2a 20  ons for .    ** 
2a816 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68  references to th
2a817 65 20 69 50 61 72 65 6e 74 20 69 6e 20 74 68 65  e iParent in the
2a818 20 6f 75 74 65 72 20 71 75 65 72 79 2e 0a 20 20   outer query..  
2a819 20 20 2a 2a 20 0a 20 20 20 20 2a 2a 20 45 78 61    ** .    ** Exa
2a81a 6d 70 6c 65 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  mple:.    **.   
2a81b 20 2a 2a 20 20 20 53 45 4c 45 43 54 20 61 2b 35   **   SELECT a+5
2a81c 2c 20 62 2a 31 30 20 46 52 4f 4d 20 28 53 45 4c  , b*10 FROM (SEL
2a81d 45 43 54 20 78 2a 33 20 41 53 20 61 2c 20 79 2b  ECT x*3 AS a, y+
2a81e 31 30 20 41 53 20 62 20 46 52 4f 4d 20 74 31 29  10 AS b FROM t1)
2a81f 20 57 48 45 52 45 20 61 3e 62 3b 0a 20 20 20 20   WHERE a>b;.    
2a820 2a 2a 20 20 20 5c 20 20 20 20 20 20 20 20 20 20  **   \          
2a821 20 20 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f             \____
2a822 5f 5f 5f 5f 5f 5f 5f 5f 5f 20 73 75 62 71 75 65  _________ subque
2a823 72 79 20 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20  ry __________/  
2a824 20 20 20 20 20 20 20 20 2f 0a 20 20 20 20 2a 2a          /.    **
2a825 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f      \___________
2a826 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 20 6f 75 74 65 72  __________ outer
2a827 20 71 75 65 72 79 20 5f 5f 5f 5f 5f 5f 5f 5f 5f   query _________
2a828 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
2a829 5f 5f 5f 5f 5f 2f 0a 20 20 20 20 2a 2a 0a 20 20  _____/.    **.  
2a82a 20 20 2a 2a 20 57 65 20 6c 6f 6f 6b 20 61 74 20    ** We look at 
2a82b 65 76 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e  every expression
2a82c 20 69 6e 20 74 68 65 20 6f 75 74 65 72 20 71 75   in the outer qu
2a82d 65 72 79 20 61 6e 64 20 65 76 65 72 79 20 70 6c  ery and every pl
2a82e 61 63 65 20 77 65 20 73 65 65 0a 20 20 20 20 2a  ace we see.    *
2a82f 2a 20 22 61 22 20 77 65 20 73 75 62 73 74 69 74  * "a" we substit
2a830 75 74 65 20 22 78 2a 33 22 20 61 6e 64 20 65 76  ute "x*3" and ev
2a831 65 72 79 20 70 6c 61 63 65 20 77 65 20 73 65 65  ery place we see
2a832 20 22 62 22 20 77 65 20 73 75 62 73 74 69 74 75   "b" we substitu
2a833 74 65 20 22 79 2b 31 30 22 2e 0a 20 20 20 20 2a  te "y+10"..    *
2a834 2f 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 70 50  /.    pList = pP
2a835 61 72 65 6e 74 2d 3e 70 45 4c 69 73 74 3b 0a 20  arent->pEList;. 
2a836 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c     for(i=0; i<pL
2a837 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ist->nExpr; i++)
2a838 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4c 69 73  {.      if( pLis
2a839 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3d 3d 30  t->a[i].zName==0
2a83a 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73   ){.        cons
2a83b 74 20 63 68 61 72 20 2a 7a 53 70 61 6e 20 3d 20  t char *zSpan = 
2a83c 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 53 70 61  pList->a[i].zSpa
2a83d 6e 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 41  n;.        if( A
2a83e 4c 57 41 59 53 28 7a 53 70 61 6e 29 20 29 7b 0a  LWAYS(zSpan) ){.
2a83f 20 20 20 20 20 20 20 20 20 20 70 4c 69 73 74 2d            pList-
2a840 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20 3d 20 73 71  >a[i].zName = sq
2a841 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
2a842 2c 20 7a 53 70 61 6e 29 3b 0a 20 20 20 20 20 20  , zSpan);.      
2a843 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
2a844 7d 0a 20 20 20 20 73 75 62 73 74 45 78 70 72 4c  }.    substExprL
2a845 69 73 74 28 64 62 2c 20 70 50 61 72 65 6e 74 2d  ist(db, pParent-
2a846 3e 70 45 4c 69 73 74 2c 20 69 50 61 72 65 6e 74  >pEList, iParent
2a847 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b  , pSub->pEList);
2a848 0a 20 20 20 20 69 66 28 20 69 73 41 67 67 20 29  .    if( isAgg )
2a849 7b 0a 20 20 20 20 20 20 73 75 62 73 74 45 78 70  {.      substExp
2a84a 72 4c 69 73 74 28 64 62 2c 20 70 50 61 72 65 6e  rList(db, pParen
2a84b 74 2d 3e 70 47 72 6f 75 70 42 79 2c 20 69 50 61  t->pGroupBy, iPa
2a84c 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69  rent, pSub->pELi
2a84d 73 74 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65  st);.      pPare
2a84e 6e 74 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 75  nt->pHaving = su
2a84f 62 73 74 45 78 70 72 28 64 62 2c 20 70 50 61 72  bstExpr(db, pPar
2a850 65 6e 74 2d 3e 70 48 61 76 69 6e 67 2c 20 69 50  ent->pHaving, iP
2a851 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c  arent, pSub->pEL
2a852 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ist);.    }.    
2a853 69 66 28 20 70 53 75 62 2d 3e 70 4f 72 64 65 72  if( pSub->pOrder
2a854 42 79 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  By ){.      asse
2a855 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 70 4f 72  rt( pParent->pOr
2a856 64 65 72 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20  derBy==0 );.    
2a857 20 20 70 50 61 72 65 6e 74 2d 3e 70 4f 72 64 65    pParent->pOrde
2a858 72 42 79 20 3d 20 70 53 75 62 2d 3e 70 4f 72 64  rBy = pSub->pOrd
2a859 65 72 42 79 3b 0a 20 20 20 20 20 20 70 53 75 62  erBy;.      pSub
2a85a 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a  ->pOrderBy = 0;.
2a85b 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50      }else if( pP
2a85c 61 72 65 6e 74 2d 3e 70 4f 72 64 65 72 42 79 20  arent->pOrderBy 
2a85d 29 7b 0a 20 20 20 20 20 20 73 75 62 73 74 45 78  ){.      substEx
2a85e 70 72 4c 69 73 74 28 64 62 2c 20 70 50 61 72 65  prList(db, pPare
2a85f 6e 74 2d 3e 70 4f 72 64 65 72 42 79 2c 20 69 50  nt->pOrderBy, iP
2a860 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c  arent, pSub->pEL
2a861 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ist);.    }.    
2a862 69 66 28 20 70 53 75 62 2d 3e 70 57 68 65 72 65  if( pSub->pWhere
2a863 20 29 7b 0a 20 20 20 20 20 20 70 57 68 65 72 65   ){.      pWhere
2a864 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
2a865 70 28 64 62 2c 20 70 53 75 62 2d 3e 70 57 68 65  p(db, pSub->pWhe
2a866 72 65 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73  re, 0);.    }els
2a867 65 7b 0a 20 20 20 20 20 20 70 57 68 65 72 65 20  e{.      pWhere 
2a868 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  = 0;.    }.    i
2a869 66 28 20 73 75 62 71 75 65 72 79 49 73 41 67 67  f( subqueryIsAgg
2a86a 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
2a86b 28 20 70 50 61 72 65 6e 74 2d 3e 70 48 61 76 69  ( pParent->pHavi
2a86c 6e 67 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  ng==0 );.      p
2a86d 50 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67 20  Parent->pHaving 
2a86e 3d 20 70 50 61 72 65 6e 74 2d 3e 70 57 68 65 72  = pParent->pWher
2a86f 65 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74  e;.      pParent
2a870 2d 3e 70 57 68 65 72 65 20 3d 20 70 57 68 65 72  ->pWhere = pWher
2a871 65 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74  e;.      pParent
2a872 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 75 62 73  ->pHaving = subs
2a873 74 45 78 70 72 28 64 62 2c 20 70 50 61 72 65 6e  tExpr(db, pParen
2a874 74 2d 3e 70 48 61 76 69 6e 67 2c 20 69 50 61 72  t->pHaving, iPar
2a875 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73  ent, pSub->pELis
2a876 74 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e  t);.      pParen
2a877 74 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 71 6c  t->pHaving = sql
2a878 69 74 65 33 45 78 70 72 41 6e 64 28 64 62 2c 20  ite3ExprAnd(db, 
2a879 70 50 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67  pParent->pHaving
2a87a 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
2a87b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a87c 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2a87d 44 75 70 28 64 62 2c 20 70 53 75 62 2d 3e 70 48  Dup(db, pSub->pH
2a87e 61 76 69 6e 67 2c 20 30 29 29 3b 0a 20 20 20 20  aving, 0));.    
2a87f 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e    assert( pParen
2a880 74 2d 3e 70 47 72 6f 75 70 42 79 3d 3d 30 20 29  t->pGroupBy==0 )
2a881 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d  ;.      pParent-
2a882 3e 70 47 72 6f 75 70 42 79 20 3d 20 73 71 6c 69  >pGroupBy = sqli
2a883 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64  te3ExprListDup(d
2a884 62 2c 20 70 53 75 62 2d 3e 70 47 72 6f 75 70 42  b, pSub->pGroupB
2a885 79 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65  y, 0);.    }else
2a886 7b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d  {.      pParent-
2a887 3e 70 57 68 65 72 65 20 3d 20 73 75 62 73 74 45  >pWhere = substE
2a888 78 70 72 28 64 62 2c 20 70 50 61 72 65 6e 74 2d  xpr(db, pParent-
2a889 3e 70 57 68 65 72 65 2c 20 69 50 61 72 65 6e 74  >pWhere, iParent
2a88a 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b  , pSub->pEList);
2a88b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e  .      pParent->
2a88c 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33  pWhere = sqlite3
2a88d 45 78 70 72 41 6e 64 28 64 62 2c 20 70 50 61 72  ExprAnd(db, pPar
2a88e 65 6e 74 2d 3e 70 57 68 65 72 65 2c 20 70 57 68  ent->pWhere, pWh
2a88f 65 72 65 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20  ere);.    }.  . 
2a890 20 20 20 2f 2a 20 54 68 65 20 66 6c 61 74 74 65     /* The flatte
2a891 6e 65 64 20 71 75 65 72 79 20 69 73 20 64 69 73  ned query is dis
2a892 74 69 6e 63 74 20 69 66 20 65 69 74 68 65 72 20  tinct if either 
2a893 74 68 65 20 69 6e 6e 65 72 20 6f 72 20 74 68 65  the inner or the
2a894 0a 20 20 20 20 2a 2a 20 6f 75 74 65 72 20 71 75  .    ** outer qu
2a895 65 72 79 20 69 73 20 64 69 73 74 69 6e 63 74 2e  ery is distinct.
2a896 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61   .    */.    pPa
2a897 72 65 6e 74 2d 3e 73 65 6c 46 6c 61 67 73 20 7c  rent->selFlags |
2a898 3d 20 70 53 75 62 2d 3e 73 65 6c 46 6c 61 67 73  = pSub->selFlags
2a899 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74 3b 0a   & SF_Distinct;.
2a89a 20 20 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a    .    /*.    **
2a89b 20 53 45 4c 45 43 54 20 2e 2e 2e 20 46 52 4f 4d   SELECT ... FROM
2a89c 20 28 53 45 4c 45 43 54 20 2e 2e 2e 20 4c 49 4d   (SELECT ... LIM
2a89d 49 54 20 61 20 4f 46 46 53 45 54 20 62 29 20 4c  IT a OFFSET b) L
2a89e 49 4d 49 54 20 78 20 4f 46 46 53 45 54 20 79 3b  IMIT x OFFSET y;
2a89f 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f  .    **.    ** O
2a8a0 6e 65 20 69 73 20 74 65 6d 70 74 65 64 20 74 6f  ne is tempted to
2a8a1 20 74 72 79 20 74 6f 20 61 64 64 20 61 20 61 6e   try to add a an
2a8a2 64 20 62 20 74 6f 20 63 6f 6d 62 69 6e 65 20 74  d b to combine t
2a8a3 68 65 20 6c 69 6d 69 74 73 2e 20 20 42 75 74 20  he limits.  But 
2a8a4 74 68 69 73 0a 20 20 20 20 2a 2a 20 64 6f 65 73  this.    ** does
2a8a5 20 6e 6f 74 20 77 6f 72 6b 20 69 66 20 65 69 74   not work if eit
2a8a6 68 65 72 20 6c 69 6d 69 74 20 69 73 20 6e 65 67  her limit is neg
2a8a7 61 74 69 76 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  ative..    */.  
2a8a8 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4c 69 6d    if( pSub->pLim
2a8a9 69 74 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72  it ){.      pPar
2a8aa 65 6e 74 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 53  ent->pLimit = pS
2a8ab 75 62 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20 20 20  ub->pLimit;.    
2a8ac 20 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20 3d    pSub->pLimit =
2a8ad 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   0;.    }.  }.. 
2a8ae 20 2f 2a 20 46 69 6e 69 61 6c 6c 79 2c 20 64 65   /* Finially, de
2a8af 6c 65 74 65 20 77 68 61 74 20 69 73 20 6c 65 66  lete what is lef
2a8b0 74 20 6f 66 20 74 68 65 20 73 75 62 71 75 65 72  t of the subquer
2a8b1 79 20 61 6e 64 20 72 65 74 75 72 6e 0a 20 20 2a  y and return.  *
2a8b2 2a 20 73 75 63 63 65 73 73 2e 0a 20 20 2a 2f 0a  * success..  */.
2a8b3 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
2a8b4 65 6c 65 74 65 28 64 62 2c 20 70 53 75 62 31 29  elete(db, pSub1)
2a8b5 3b 0a 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d  ;..  return 1;.}
2a8b6 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
2a8b7 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
2a8b8 53 55 42 51 55 45 52 59 29 20 7c 7c 20 21 64 65  SUBQUERY) || !de
2a8b9 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2a8ba 54 5f 56 49 45 57 29 20 2a 2f 0a 0a 2f 2a 0a 2a  T_VIEW) */../*.*
2a8bb 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 53 45  * Analyze the SE
2a8bc 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70  LECT statement p
2a8bd 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67 75  assed as an argu
2a8be 6d 65 6e 74 20 74 6f 20 73 65 65 20 69 66 20 69  ment to see if i
2a8bf 74 0a 2a 2a 20 69 73 20 61 20 6d 69 6e 28 29 20  t.** is a min() 
2a8c0 6f 72 20 6d 61 78 28 29 20 71 75 65 72 79 2e 20  or max() query. 
2a8c1 52 65 74 75 72 6e 20 57 48 45 52 45 5f 4f 52 44  Return WHERE_ORD
2a8c2 45 52 42 59 5f 4d 49 4e 20 6f 72 20 57 48 45 52  ERBY_MIN or WHER
2a8c3 45 5f 4f 52 44 45 52 42 59 5f 4d 41 58 20 69 66  E_ORDERBY_MAX if
2a8c4 20 0a 2a 2a 20 69 74 20 69 73 2c 20 6f 72 20 30   .** it is, or 0
2a8c5 20 6f 74 68 65 72 77 69 73 65 2e 20 41 74 20 70   otherwise. At p
2a8c6 72 65 73 65 6e 74 2c 20 61 20 71 75 65 72 79 20  resent, a query 
2a8c7 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f  is considered to
2a8c8 20 62 65 0a 2a 2a 20 61 20 6d 69 6e 28 29 2f 6d   be.** a min()/m
2a8c9 61 78 28 29 20 71 75 65 72 79 20 69 66 3a 0a 2a  ax() query if:.*
2a8ca 2a 0a 2a 2a 20 20 20 31 2e 20 54 68 65 72 65 20  *.**   1. There 
2a8cb 69 73 20 61 20 73 69 6e 67 6c 65 20 6f 62 6a 65  is a single obje
2a8cc 63 74 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  ct in the FROM c
2a8cd 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 32  lause..**.**   2
2a8ce 2e 20 54 68 65 72 65 20 69 73 20 61 20 73 69 6e  . There is a sin
2a8cf 67 6c 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  gle expression i
2a8d0 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
2a8d1 2c 20 61 6e 64 20 69 74 20 69 73 0a 2a 2a 20 20  , and it is.**  
2a8d2 20 20 20 20 65 69 74 68 65 72 20 6d 69 6e 28 78      either min(x
2a8d3 29 20 6f 72 20 6d 61 78 28 78 29 2c 20 77 68 65  ) or max(x), whe
2a8d4 72 65 20 78 20 69 73 20 61 20 63 6f 6c 75 6d 6e  re x is a column
2a8d5 20 72 65 66 65 72 65 6e 63 65 2e 0a 2a 2f 0a 73   reference..*/.s
2a8d6 74 61 74 69 63 20 75 38 20 6d 69 6e 4d 61 78 51  tatic u8 minMaxQ
2a8d7 75 65 72 79 28 53 65 6c 65 63 74 20 2a 70 29 7b  uery(Select *p){
2a8d8 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 0a  .  Expr *pExpr;.
2a8d9 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
2a8da 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a  st = p->pEList;.
2a8db 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 6e  .  if( pEList->n
2a8dc 45 78 70 72 21 3d 31 20 29 20 72 65 74 75 72 6e  Expr!=1 ) return
2a8dd 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4e   WHERE_ORDERBY_N
2a8de 4f 52 4d 41 4c 3b 0a 20 20 70 45 78 70 72 20 3d  ORMAL;.  pExpr =
2a8df 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45   pEList->a[0].pE
2a8e0 78 70 72 3b 0a 20 20 69 66 28 20 70 45 78 70 72  xpr;.  if( pExpr
2a8e1 2d 3e 6f 70 21 3d 54 4b 5f 41 47 47 5f 46 55 4e  ->op!=TK_AGG_FUN
2a8e2 43 54 49 4f 4e 20 29 20 72 65 74 75 72 6e 20 30  CTION ) return 0
2a8e3 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 45 78  ;.  if( NEVER(Ex
2a8e4 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
2a8e5 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
2a8e6 74 29 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  t)) ) return 0;.
2a8e7 20 20 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72    pEList = pExpr
2a8e8 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 69 66 28  ->x.pList;.  if(
2a8e9 20 70 45 4c 69 73 74 3d 3d 30 20 7c 7c 20 70 45   pEList==0 || pE
2a8ea 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29  List->nExpr!=1 )
2a8eb 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
2a8ec 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45   pEList->a[0].pE
2a8ed 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 47 47 5f  xpr->op!=TK_AGG_
2a8ee 43 4f 4c 55 4d 4e 20 29 20 72 65 74 75 72 6e 20  COLUMN ) return 
2a8ef 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4e 4f  WHERE_ORDERBY_NO
2a8f0 52 4d 41 4c 3b 0a 20 20 61 73 73 65 72 74 28 20  RMAL;.  assert( 
2a8f1 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
2a8f2 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61  (pExpr, EP_IntVa
2a8f3 6c 75 65 29 20 29 3b 0a 20 20 69 66 28 20 73 71  lue) );.  if( sq
2a8f4 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 45 78  lite3StrICmp(pEx
2a8f5 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 22 6d 69  pr->u.zToken,"mi
2a8f6 6e 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  n")==0 ){.    re
2a8f7 74 75 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52  turn WHERE_ORDER
2a8f8 42 59 5f 4d 49 4e 3b 0a 20 20 7d 65 6c 73 65 20  BY_MIN;.  }else 
2a8f9 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
2a8fa 6d 70 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b  mp(pExpr->u.zTok
2a8fb 65 6e 2c 22 6d 61 78 22 29 3d 3d 30 20 29 7b 0a  en,"max")==0 ){.
2a8fc 20 20 20 20 72 65 74 75 72 6e 20 57 48 45 52 45      return WHERE
2a8fd 5f 4f 52 44 45 52 42 59 5f 4d 41 58 3b 0a 20 20  _ORDERBY_MAX;.  
2a8fe 7d 0a 20 20 72 65 74 75 72 6e 20 57 48 45 52 45  }.  return WHERE
2a8ff 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b  _ORDERBY_NORMAL;
2a900 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65  .}../*.** The se
2a901 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 70  lect statement p
2a902 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72  assed as the fir
2a903 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61  st argument is a
2a904 6e 20 61 67 67 72 65 67 61 74 65 20 71 75 65 72  n aggregate quer
2a905 79 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64  y..** The second
2a906 20 61 72 67 6d 65 6e 74 20 69 73 20 74 68 65 20   argment is the 
2a907 61 73 73 6f 63 69 61 74 65 64 20 61 67 67 72 65  associated aggre
2a908 67 61 74 65 2d 69 6e 66 6f 20 6f 62 6a 65 63 74  gate-info object
2a909 2e 20 54 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74  . This .** funct
2a90a 69 6f 6e 20 74 65 73 74 73 20 69 66 20 74 68 65  ion tests if the
2a90b 20 53 45 4c 45 43 54 20 69 73 20 6f 66 20 74 68   SELECT is of th
2a90c 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20  e form:.**.**   
2a90d 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a 29 20  SELECT count(*) 
2a90e 46 52 4f 4d 20 3c 74 62 6c 3e 0a 2a 2a 0a 2a 2a  FROM <tbl>.**.**
2a90f 20 77 68 65 72 65 20 74 61 62 6c 65 20 69 73 20   where table is 
2a910 61 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  a database table
2a911 2c 20 6e 6f 74 20 61 20 73 75 62 2d 73 65 6c 65  , not a sub-sele
2a912 63 74 20 6f 72 20 76 69 65 77 2e 20 49 66 20 74  ct or view. If t
2a913 68 65 20 71 75 65 72 79 0a 2a 2a 20 64 6f 65 73  he query.** does
2a914 20 6d 61 74 63 68 20 74 68 69 73 20 70 61 74 74   match this patt
2a915 65 72 6e 2c 20 74 68 65 6e 20 61 20 70 6f 69 6e  ern, then a poin
2a916 74 65 72 20 74 6f 20 74 68 65 20 54 61 62 6c 65  ter to the Table
2a917 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e   object represen
2a918 74 69 6e 67 0a 2a 2a 20 3c 74 62 6c 3e 20 69 73  ting.** <tbl> is
2a919 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72   returned. Other
2a91a 77 69 73 65 2c 20 30 20 69 73 20 72 65 74 75 72  wise, 0 is retur
2a91b 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54  ned..*/.static T
2a91c 61 62 6c 65 20 2a 69 73 53 69 6d 70 6c 65 43 6f  able *isSimpleCo
2a91d 75 6e 74 28 53 65 6c 65 63 74 20 2a 70 2c 20 41  unt(Select *p, A
2a91e 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f  ggInfo *pAggInfo
2a91f 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  ){.  Table *pTab
2a920 3b 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b  ;.  Expr *pExpr;
2a921 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 2d 3e  ..  assert( !p->
2a922 70 47 72 6f 75 70 42 79 20 29 3b 0a 0a 20 20 69  pGroupBy );..  i
2a923 66 28 20 70 2d 3e 70 57 68 65 72 65 20 7c 7c 20  f( p->pWhere || 
2a924 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  p->pEList->nExpr
2a925 21 3d 31 20 0a 20 20 20 7c 7c 20 70 2d 3e 70 53  !=1 .   || p->pS
2a926 72 63 2d 3e 6e 53 72 63 21 3d 31 20 7c 7c 20 70  rc->nSrc!=1 || p
2a927 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65  ->pSrc->a[0].pSe
2a928 6c 65 63 74 0a 20 20 29 7b 0a 20 20 20 20 72 65  lect.  ){.    re
2a929 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 54  turn 0;.  }.  pT
2a92a 61 62 20 3d 20 70 2d 3e 70 53 72 63 2d 3e 61 5b  ab = p->pSrc->a[
2a92b 30 5d 2e 70 54 61 62 3b 0a 20 20 70 45 78 70 72  0].pTab;.  pExpr
2a92c 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b   = p->pEList->a[
2a92d 30 5d 2e 70 45 78 70 72 3b 0a 20 20 61 73 73 65  0].pExpr;.  asse
2a92e 72 74 28 20 70 54 61 62 20 26 26 20 21 70 54 61  rt( pTab && !pTa
2a92f 62 2d 3e 70 53 65 6c 65 63 74 20 26 26 20 70 45  b->pSelect && pE
2a930 78 70 72 20 29 3b 0a 0a 20 20 69 66 28 20 49 73  xpr );..  if( Is
2a931 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 20  Virtual(pTab) ) 
2a932 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
2a933 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 47  pExpr->op!=TK_AG
2a934 47 5f 46 55 4e 43 54 49 4f 4e 20 29 20 72 65 74  G_FUNCTION ) ret
2a935 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 28 70 41  urn 0;.  if( (pA
2a936 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 30 5d  ggInfo->aFunc[0]
2a937 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 26 53 51  .pFunc->flags&SQ
2a938 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 55 4e 54 29  LITE_FUNC_COUNT)
2a939 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
2a93a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 66 6c 61    if( pExpr->fla
2a93b 67 73 26 45 50 5f 44 69 73 74 69 6e 63 74 20 29  gs&EP_Distinct )
2a93c 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 72 65   return 0;..  re
2a93d 74 75 72 6e 20 70 54 61 62 3b 0a 7d 0a 0a 2f 2a  turn pTab;.}../*
2a93e 0a 2a 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63  .** If the sourc
2a93f 65 2d 6c 69 73 74 20 69 74 65 6d 20 70 61 73 73  e-list item pass
2a940 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e  ed as an argumen
2a941 74 20 77 61 73 20 61 75 67 6d 65 6e 74 65 64 20  t was augmented 
2a942 77 69 74 68 20 61 6e 0a 2a 2a 20 49 4e 44 45 58  with an.** INDEX
2a943 45 44 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68  ED BY clause, th
2a944 65 6e 20 74 72 79 20 74 6f 20 6c 6f 63 61 74 65  en try to locate
2a945 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 69   the specified i
2a946 6e 64 65 78 2e 20 49 66 20 74 68 65 72 65 0a 2a  ndex. If there.*
2a947 2a 20 77 61 73 20 73 75 63 68 20 61 20 63 6c 61  * was such a cla
2a948 75 73 65 20 61 6e 64 20 74 68 65 20 6e 61 6d 65  use and the name
2a949 64 20 69 6e 64 65 78 20 63 61 6e 6e 6f 74 20 62  d index cannot b
2a94a 65 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20  e found, return 
2a94b 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  .** SQLITE_ERROR
2a94c 20 61 6e 64 20 6c 65 61 76 65 20 61 6e 20 65 72   and leave an er
2a94d 72 6f 72 20 69 6e 20 70 50 61 72 73 65 2e 20 4f  ror in pParse. O
2a94e 74 68 65 72 77 69 73 65 2c 20 70 6f 70 75 6c 61  therwise, popula
2a94f 74 65 20 0a 2a 2a 20 70 46 72 6f 6d 2d 3e 70 49  te .** pFrom->pI
2a950 6e 64 65 78 20 61 6e 64 20 72 65 74 75 72 6e 20  ndex and return 
2a951 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51  SQLITE_OK..*/.SQ
2a952 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2a953 20 73 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42   sqlite3IndexedB
2a954 79 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20 2a 70  yLookup(Parse *p
2a955 50 61 72 73 65 2c 20 73 74 72 75 63 74 20 53 72  Parse, struct Sr
2a956 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 46 72 6f  cList_item *pFro
2a957 6d 29 7b 0a 20 20 69 66 28 20 70 46 72 6f 6d 2d  m){.  if( pFrom-
2a958 3e 70 54 61 62 20 26 26 20 70 46 72 6f 6d 2d 3e  >pTab && pFrom->
2a959 7a 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 54 61  zIndex ){.    Ta
2a95a 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 46 72 6f  ble *pTab = pFro
2a95b 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 63 68 61  m->pTab;.    cha
2a95c 72 20 2a 7a 49 6e 64 65 78 20 3d 20 70 46 72 6f  r *zIndex = pFro
2a95d 6d 2d 3e 7a 49 6e 64 65 78 3b 0a 20 20 20 20 49  m->zIndex;.    I
2a95e 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20  ndex *pIdx;.    
2a95f 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70  for(pIdx=pTab->p
2a960 49 6e 64 65 78 3b 20 0a 20 20 20 20 20 20 20 20  Index; .        
2a961 70 49 64 78 20 26 26 20 73 71 6c 69 74 65 33 53  pIdx && sqlite3S
2a962 74 72 49 43 6d 70 28 70 49 64 78 2d 3e 7a 4e 61  trICmp(pIdx->zNa
2a963 6d 65 2c 20 7a 49 6e 64 65 78 29 3b 20 0a 20 20  me, zIndex); .  
2a964 20 20 20 20 20 20 70 49 64 78 3d 70 49 64 78 2d        pIdx=pIdx-
2a965 3e 70 4e 65 78 74 0a 20 20 20 20 29 3b 0a 20 20  >pNext.    );.  
2a966 20 20 69 66 28 20 21 70 49 64 78 20 29 7b 0a 20    if( !pIdx ){. 
2a967 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
2a968 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f  rMsg(pParse, "no
2a969 20 73 75 63 68 20 69 6e 64 65 78 3a 20 25 73 22   such index: %s"
2a96a 2c 20 7a 49 6e 64 65 78 2c 20 30 29 3b 0a 20 20  , zIndex, 0);.  
2a96b 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2a96c 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20  E_ERROR;.    }. 
2a96d 20 20 20 70 46 72 6f 6d 2d 3e 70 49 6e 64 65 78     pFrom->pIndex
2a96e 20 3d 20 70 49 64 78 3b 0a 20 20 7d 0a 20 20 72   = pIdx;.  }.  r
2a96f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2a970 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
2a971 6f 75 74 69 6e 65 20 69 73 20 61 20 57 61 6c 6b  outine is a Walk
2a972 65 72 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20  er callback for 
2a973 22 65 78 70 61 6e 64 69 6e 67 22 20 61 20 53 45  "expanding" a SE
2a974 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a  LECT statement..
2a975 2a 2a 20 22 45 78 70 61 6e 64 69 6e 67 22 20 6d  ** "Expanding" m
2a976 65 61 6e 73 20 74 6f 20 64 6f 20 74 68 65 20 66  eans to do the f
2a977 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  ollowing:.**.** 
2a978 20 20 20 28 31 29 20 20 4d 61 6b 65 20 73 75 72     (1)  Make sur
2a979 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75  e VDBE cursor nu
2a97a 6d 62 65 72 73 20 68 61 76 65 20 62 65 65 6e 20  mbers have been 
2a97b 61 73 73 69 67 6e 65 64 20 74 6f 20 65 76 65 72  assigned to ever
2a97c 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 65 6c 65  y.**         ele
2a97d 6d 65 6e 74 20 6f 66 20 74 68 65 20 46 52 4f 4d  ment of the FROM
2a97e 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20   clause..**.**  
2a97f 20 20 28 32 29 20 20 46 69 6c 6c 20 69 6e 20 74    (2)  Fill in t
2a980 68 65 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d  he pTabList->a[]
2a981 2e 70 54 61 62 20 66 69 65 6c 64 73 20 69 6e 20  .pTab fields in 
2a982 74 68 65 20 53 72 63 4c 69 73 74 20 74 68 61 74  the SrcList that
2a983 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 64 65 66   .**         def
2a984 69 6e 65 73 20 46 52 4f 4d 20 63 6c 61 75 73 65  ines FROM clause
2a985 2e 20 20 57 68 65 6e 20 76 69 65 77 73 20 61 70  .  When views ap
2a986 70 65 61 72 20 69 6e 20 74 68 65 20 46 52 4f 4d  pear in the FROM
2a987 20 63 6c 61 75 73 65 2c 0a 2a 2a 20 20 20 20 20   clause,.**     
2a988 20 20 20 20 66 69 6c 6c 20 70 54 61 62 4c 69 73      fill pTabLis
2a989 74 2d 3e 61 5b 5d 2e 70 53 65 6c 65 63 74 20 77  t->a[].pSelect w
2a98a 69 74 68 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ith a copy of th
2a98b 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
2a98c 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 74 68  nt.**         th
2a98d 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  at implements th
2a98e 65 20 76 69 65 77 2e 20 20 41 20 63 6f 70 79 20  e view.  A copy 
2a98f 69 73 20 6d 61 64 65 20 6f 66 20 74 68 65 20 76  is made of the v
2a990 69 65 77 27 73 20 53 45 4c 45 43 54 0a 2a 2a 20  iew's SELECT.** 
2a991 20 20 20 20 20 20 20 20 73 74 61 74 65 6d 65 6e          statemen
2a992 74 20 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e  t so that we can
2a993 20 66 72 65 65 6c 79 20 6d 6f 64 69 66 79 20 6f   freely modify o
2a994 72 20 64 65 6c 65 74 65 20 74 68 61 74 20 73 74  r delete that st
2a995 61 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20  atement.**      
2a996 20 20 20 77 69 74 68 6f 75 74 20 77 6f 72 72 79     without worry
2a997 69 6e 67 20 61 62 6f 75 74 20 6d 65 73 73 69 6e  ing about messin
2a998 67 20 75 70 20 74 68 65 20 70 72 65 73 69 73 74  g up the presist
2a999 65 6e 74 20 72 65 70 72 65 73 65 6e 74 61 74 69  ent representati
2a99a 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 66  on.**         of
2a99b 20 74 68 65 20 76 69 65 77 2e 0a 2a 2a 0a 2a 2a   the view..**.**
2a99c 20 20 20 20 28 33 29 20 20 41 64 64 20 74 65 72      (3)  Add ter
2a99d 6d 73 20 74 6f 20 74 68 65 20 57 48 45 52 45 20  ms to the WHERE 
2a99e 63 6c 61 75 73 65 20 74 6f 20 61 63 63 6f 6d 6f  clause to accomo
2a99f 64 61 74 65 20 74 68 65 20 4e 41 54 55 52 41 4c  date the NATURAL
2a9a0 20 6b 65 79 77 6f 72 64 0a 2a 2a 20 20 20 20 20   keyword.**     
2a9a1 20 20 20 20 6f 6e 20 6a 6f 69 6e 73 20 61 6e 64      on joins and
2a9a2 20 74 68 65 20 4f 4e 20 61 6e 64 20 55 53 49 4e   the ON and USIN
2a9a3 47 20 63 6c 61 75 73 65 20 6f 66 20 6a 6f 69 6e  G clause of join
2a9a4 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 34 29 20  s..**.**    (4) 
2a9a5 20 53 63 61 6e 20 74 68 65 20 6c 69 73 74 20 6f   Scan the list o
2a9a6 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
2a9a7 20 72 65 73 75 6c 74 20 73 65 74 20 28 70 45 4c   result set (pEL
2a9a8 69 73 74 29 20 6c 6f 6f 6b 69 6e 67 0a 2a 2a 20  ist) looking.** 
2a9a9 20 20 20 20 20 20 20 20 66 6f 72 20 69 6e 73 74          for inst
2a9aa 61 6e 63 65 73 20 6f 66 20 74 68 65 20 22 2a 22  ances of the "*"
2a9ab 20 6f 70 65 72 61 74 6f 72 20 6f 72 20 74 68 65   operator or the
2a9ac 20 54 41 42 4c 45 2e 2a 20 6f 70 65 72 61 74 6f   TABLE.* operato
2a9ad 72 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 49 66  r..**         If
2a9ae 20 66 6f 75 6e 64 2c 20 65 78 70 61 6e 64 20 65   found, expand e
2a9af 61 63 68 20 22 2a 22 20 74 6f 20 62 65 20 65 76  ach "*" to be ev
2a9b0 65 72 79 20 63 6f 6c 75 6d 6e 20 69 6e 20 65 76  ery column in ev
2a9b1 65 72 79 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20  ery table.**    
2a9b2 20 20 20 20 20 61 6e 64 20 54 41 42 4c 45 2e 2a       and TABLE.*
2a9b3 20 74 6f 20 62 65 20 65 76 65 72 79 20 63 6f 6c   to be every col
2a9b4 75 6d 6e 20 69 6e 20 54 41 42 4c 45 2e 0a 2a 2a  umn in TABLE..**
2a9b5 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
2a9b6 65 6c 65 63 74 45 78 70 61 6e 64 65 72 28 57 61  electExpander(Wa
2a9b7 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53  lker *pWalker, S
2a9b8 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 50 61 72  elect *p){.  Par
2a9b9 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70 57 61  se *pParse = pWa
2a9ba 6c 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20  lker->pParse;.  
2a9bb 69 6e 74 20 69 2c 20 6a 2c 20 6b 3b 0a 20 20 53  int i, j, k;.  S
2a9bc 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74  rcList *pTabList
2a9bd 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  ;.  ExprList *pE
2a9be 4c 69 73 74 3b 0a 20 20 73 74 72 75 63 74 20 53  List;.  struct S
2a9bf 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 46 72  rcList_item *pFr
2a9c0 6f 6d 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  om;.  sqlite3 *d
2a9c1 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2a9c2 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
2a9c3 63 46 61 69 6c 65 64 20 20 29 7b 0a 20 20 20 20  cFailed  ){.    
2a9c4 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
2a9c5 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 45 56 45  ;.  }.  if( NEVE
2a9c6 52 28 70 2d 3e 70 53 72 63 3d 3d 30 29 20 7c 7c  R(p->pSrc==0) ||
2a9c7 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20   (p->selFlags & 
2a9c8 53 46 5f 45 78 70 61 6e 64 65 64 29 21 3d 30 20  SF_Expanded)!=0 
2a9c9 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52  ){.    return WR
2a9ca 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20 70  C_Prune;.  }.  p
2a9cb 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46  ->selFlags |= SF
2a9cc 5f 45 78 70 61 6e 64 65 64 3b 0a 20 20 70 54 61  _Expanded;.  pTa
2a9cd 62 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b  bList = p->pSrc;
2a9ce 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70  .  pEList = p->p
2a9cf 45 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20 4d 61 6b  EList;..  /* Mak
2a9d0 65 20 73 75 72 65 20 63 75 72 73 6f 72 20 6e 75  e sure cursor nu
2a9d1 6d 62 65 72 73 20 68 61 76 65 20 62 65 65 6e 20  mbers have been 
2a9d2 61 73 73 69 67 6e 65 64 20 74 6f 20 61 6c 6c 20  assigned to all 
2a9d3 65 6e 74 72 69 65 73 20 69 6e 0a 20 20 2a 2a 20  entries in.  ** 
2a9d4 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2a9d5 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  of the SELECT st
2a9d6 61 74 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20  atement..  */.  
2a9d7 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 73  sqlite3SrcListAs
2a9d8 73 69 67 6e 43 75 72 73 6f 72 73 28 70 50 61 72  signCursors(pPar
2a9d9 73 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 0a  se, pTabList);..
2a9da 20 20 2f 2a 20 4c 6f 6f 6b 20 75 70 20 65 76 65    /* Look up eve
2a9db 72 79 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 69  ry table named i
2a9dc 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
2a9dd 65 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2e  e of the select.
2a9de 20 20 49 66 0a 20 20 2a 2a 20 61 6e 20 65 6e 74    If.  ** an ent
2a9df 72 79 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63  ry of the FROM c
2a9e0 6c 61 75 73 65 20 69 73 20 61 20 73 75 62 71 75  lause is a subqu
2a9e1 65 72 79 20 69 6e 73 74 65 61 64 20 6f 66 20 61  ery instead of a
2a9e2 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 2c 0a   table or view,.
2a9e3 20 20 2a 2a 20 74 68 65 6e 20 63 72 65 61 74 65    ** then create
2a9e4 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62   a transient tab
2a9e5 6c 65 20 73 74 72 75 63 74 75 72 65 20 74 6f 20  le structure to 
2a9e6 64 65 73 63 72 69 62 65 20 74 68 65 20 73 75 62  describe the sub
2a9e7 71 75 65 72 79 2e 0a 20 20 2a 2f 0a 20 20 66 6f  query..  */.  fo
2a9e8 72 28 69 3d 30 2c 20 70 46 72 6f 6d 3d 70 54 61  r(i=0, pFrom=pTa
2a9e9 62 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 54 61 62  bList->a; i<pTab
2a9ea 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c  List->nSrc; i++,
2a9eb 20 70 46 72 6f 6d 2b 2b 29 7b 0a 20 20 20 20 54   pFrom++){.    T
2a9ec 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20  able *pTab;.    
2a9ed 69 66 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62 21  if( pFrom->pTab!
2a9ee 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  =0 ){.      /* T
2a9ef 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 68 61  his statement ha
2a9f0 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70  s already been p
2a9f1 72 65 70 61 72 65 64 2e 20 20 54 68 65 72 65 20  repared.  There 
2a9f2 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 20 20 20  is no need.     
2a9f3 20 2a 2a 20 74 6f 20 67 6f 20 66 75 72 74 68 65   ** to go furthe
2a9f4 72 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65  r. */.      asse
2a9f5 72 74 28 20 69 3d 3d 30 20 29 3b 0a 20 20 20 20  rt( i==0 );.    
2a9f6 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75    return WRC_Pru
2a9f7 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ne;.    }.    if
2a9f8 28 20 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 3d 3d  ( pFrom->zName==
2a9f9 30 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  0 ){.#ifndef SQL
2a9fa 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
2a9fb 59 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a  Y.      Select *
2a9fc 70 53 65 6c 20 3d 20 70 46 72 6f 6d 2d 3e 70 53  pSel = pFrom->pS
2a9fd 65 6c 65 63 74 3b 0a 20 20 20 20 20 20 2f 2a 20  elect;.      /* 
2a9fe 41 20 73 75 62 2d 71 75 65 72 79 20 69 6e 20 74  A sub-query in t
2a9ff 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  he FROM clause o
2aa00 66 20 61 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20  f a SELECT */.  
2aa01 20 20 20 20 61 73 73 65 72 74 28 20 70 53 65 6c      assert( pSel
2aa02 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  !=0 );.      ass
2aa03 65 72 74 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62  ert( pFrom->pTab
2aa04 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==0 );.      sql
2aa05 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70  ite3WalkSelect(p
2aa06 57 61 6c 6b 65 72 2c 20 70 53 65 6c 29 3b 0a 20  Walker, pSel);. 
2aa07 20 20 20 20 20 70 46 72 6f 6d 2d 3e 70 54 61 62       pFrom->pTab
2aa08 20 3d 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65   = pTab = sqlite
2aa09 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
2aa0a 2c 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 29  , sizeof(Table))
2aa0b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62  ;.      if( pTab
2aa0c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43  ==0 ) return WRC
2aa0d 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 70 54  _Abort;.      pT
2aa0e 61 62 2d 3e 64 62 4d 65 6d 20 3d 20 64 62 2d 3e  ab->dbMem = db->
2aa0f 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c  lookaside.bEnabl
2aa10 65 64 20 3f 20 64 62 20 3a 20 30 3b 0a 20 20 20  ed ? db : 0;.   
2aa11 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 20 3d 20     pTab->nRef = 
2aa12 31 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 7a  1;.      pTab->z
2aa13 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50  Name = sqlite3MP
2aa14 72 69 6e 74 66 28 64 62 2c 20 22 73 71 6c 69 74  rintf(db, "sqlit
2aa15 65 5f 73 75 62 71 75 65 72 79 5f 25 70 5f 22 2c  e_subquery_%p_",
2aa16 20 28 76 6f 69 64 2a 29 70 54 61 62 29 3b 0a 20   (void*)pTab);. 
2aa17 20 20 20 20 20 77 68 69 6c 65 28 20 70 53 65 6c       while( pSel
2aa18 2d 3e 70 50 72 69 6f 72 20 29 7b 20 70 53 65 6c  ->pPrior ){ pSel
2aa19 20 3d 20 70 53 65 6c 2d 3e 70 50 72 69 6f 72 3b   = pSel->pPrior;
2aa1a 20 7d 0a 20 20 20 20 20 20 73 65 6c 65 63 74 43   }.      selectC
2aa1b 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78 70 72 4c 69  olumnsFromExprLi
2aa1c 73 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2d  st(pParse, pSel-
2aa1d 3e 70 45 4c 69 73 74 2c 20 26 70 54 61 62 2d 3e  >pEList, &pTab->
2aa1e 6e 43 6f 6c 2c 20 26 70 54 61 62 2d 3e 61 43 6f  nCol, &pTab->aCo
2aa1f 6c 29 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e  l);.      pTab->
2aa20 69 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20 20 20  iPKey = -1;.    
2aa21 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73    pTab->tabFlags
2aa22 20 7c 3d 20 54 46 5f 45 70 68 65 6d 65 72 61 6c   |= TF_Ephemeral
2aa23 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c  ;.#endif.    }el
2aa24 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6e 20  se{.      /* An 
2aa25 6f 72 64 69 6e 61 72 79 20 74 61 62 6c 65 20 6f  ordinary table o
2aa26 72 20 76 69 65 77 20 6e 61 6d 65 20 69 6e 20 74  r view name in t
2aa27 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a  he FROM clause *
2aa28 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  /.      assert( 
2aa29 70 46 72 6f 6d 2d 3e 70 54 61 62 3d 3d 30 20 29  pFrom->pTab==0 )
2aa2a 3b 0a 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e 70  ;.      pFrom->p
2aa2b 54 61 62 20 3d 20 70 54 61 62 20 3d 20 0a 20 20  Tab = pTab = .  
2aa2c 20 20 20 20 20 20 73 71 6c 69 74 65 33 4c 6f 63        sqlite3Loc
2aa2d 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c  ateTable(pParse,
2aa2e 30 2c 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 2c 70  0,pFrom->zName,p
2aa2f 46 72 6f 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29  From->zDatabase)
2aa30 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62  ;.      if( pTab
2aa31 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43  ==0 ) return WRC
2aa32 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 70 54  _Abort;.      pT
2aa33 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 23 69 66 20  ab->nRef++;.#if 
2aa34 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2aa35 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64  OMIT_VIEW) || !d
2aa36 65 66 69 6e 65 64 20 28 53 51 4c 49 54 45 5f 4f  efined (SQLITE_O
2aa37 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2aa38 29 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62  ).      if( pTab
2aa39 2d 3e 70 53 65 6c 65 63 74 20 7c 7c 20 49 73 56  ->pSelect || IsV
2aa3a 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
2aa3b 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 72 65          /* We re
2aa3c 61 63 68 20 68 65 72 65 20 69 66 20 74 68 65 20  ach here if the 
2aa3d 6e 61 6d 65 64 20 74 61 62 6c 65 20 69 73 20 61  named table is a
2aa3e 20 72 65 61 6c 6c 79 20 61 20 76 69 65 77 20 2a   really a view *
2aa3f 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71  /.        if( sq
2aa40 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75  lite3ViewGetColu
2aa41 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20  mnNames(pParse, 
2aa42 70 54 61 62 29 20 29 20 72 65 74 75 72 6e 20 57  pTab) ) return W
2aa43 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20  RC_Abort;.      
2aa44 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d    assert( pFrom-
2aa45 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 0a 20  >pSelect==0 );. 
2aa46 20 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e 70 53         pFrom->pS
2aa47 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53  elect = sqlite3S
2aa48 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 54 61  electDup(db, pTa
2aa49 62 2d 3e 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a  b->pSelect, 0);.
2aa4a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57          sqlite3W
2aa4b 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65  alkSelect(pWalke
2aa4c 72 2c 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63  r, pFrom->pSelec
2aa4d 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64  t);.      }.#end
2aa4e 69 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  if.    }..    /*
2aa4f 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 64 65   Locate the inde
2aa50 78 20 6e 61 6d 65 64 20 62 79 20 74 68 65 20 49  x named by the I
2aa51 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75 73 65  NDEXED BY clause
2aa52 2c 20 69 66 20 61 6e 79 2e 20 2a 2f 0a 20 20 20  , if any. */.   
2aa53 20 69 66 28 20 73 71 6c 69 74 65 33 49 6e 64 65   if( sqlite3Inde
2aa54 78 65 64 42 79 4c 6f 6f 6b 75 70 28 70 50 61 72  xedByLookup(pPar
2aa55 73 65 2c 20 70 46 72 6f 6d 29 20 29 7b 0a 20 20  se, pFrom) ){.  
2aa56 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41      return WRC_A
2aa57 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  bort;.    }.  }.
2aa58 0a 20 20 2f 2a 20 50 72 6f 63 65 73 73 20 4e 41  .  /* Process NA
2aa59 54 55 52 41 4c 20 6b 65 79 77 6f 72 64 73 2c 20  TURAL keywords, 
2aa5a 61 6e 64 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47  and ON and USING
2aa5b 20 63 6c 61 75 73 65 73 20 6f 66 20 6a 6f 69 6e   clauses of join
2aa5c 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62  s..  */.  if( db
2aa5d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c  ->mallocFailed |
2aa5e 7c 20 73 71 6c 69 74 65 50 72 6f 63 65 73 73 4a  | sqliteProcessJ
2aa5f 6f 69 6e 28 70 50 61 72 73 65 2c 20 70 29 20 29  oin(pParse, p) )
2aa60 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43  {.    return WRC
2aa61 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 2f  _Abort;.  }..  /
2aa62 2a 20 46 6f 72 20 65 76 65 72 79 20 22 2a 22 20  * For every "*" 
2aa63 74 68 61 74 20 6f 63 63 75 72 73 20 69 6e 20 74  that occurs in t
2aa64 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 2c 20  he column list, 
2aa65 69 6e 73 65 72 74 20 74 68 65 20 6e 61 6d 65 73  insert the names
2aa66 20 6f 66 0a 20 20 2a 2a 20 61 6c 6c 20 63 6f 6c   of.  ** all col
2aa67 75 6d 6e 73 20 69 6e 20 61 6c 6c 20 74 61 62 6c  umns in all tabl
2aa68 65 73 2e 20 20 41 6e 64 20 66 6f 72 20 65 76 65  es.  And for eve
2aa69 72 79 20 54 41 42 4c 45 2e 2a 20 69 6e 73 65 72  ry TABLE.* inser
2aa6a 74 20 74 68 65 20 6e 61 6d 65 73 0a 20 20 2a 2a  t the names.  **
2aa6b 20 6f 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20   of all columns 
2aa6c 69 6e 20 54 41 42 4c 45 2e 20 20 54 68 65 20 70  in TABLE.  The p
2aa6d 61 72 73 65 72 20 69 6e 73 65 72 74 65 64 20 61  arser inserted a
2aa6e 20 73 70 65 63 69 61 6c 20 65 78 70 72 65 73 73   special express
2aa6f 69 6f 6e 0a 20 20 2a 2a 20 77 69 74 68 20 74 68  ion.  ** with th
2aa70 65 20 54 4b 5f 41 4c 4c 20 6f 70 65 72 61 74 6f  e TK_ALL operato
2aa71 72 20 66 6f 72 20 65 61 63 68 20 22 2a 22 20 74  r for each "*" t
2aa72 68 61 74 20 69 74 20 66 6f 75 6e 64 20 69 6e 20  hat it found in 
2aa73 74 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 2e  the column list.
2aa74 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .  ** The follow
2aa75 69 6e 67 20 63 6f 64 65 20 6a 75 73 74 20 68 61  ing code just ha
2aa76 73 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20  s to locate the 
2aa77 54 4b 5f 41 4c 4c 20 65 78 70 72 65 73 73 69 6f  TK_ALL expressio
2aa78 6e 73 20 61 6e 64 20 65 78 70 61 6e 64 0a 20 20  ns and expand.  
2aa79 2a 2a 20 65 61 63 68 20 6f 6e 65 20 74 6f 20 74  ** each one to t
2aa7a 68 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63  he list of all c
2aa7b 6f 6c 75 6d 6e 73 20 69 6e 20 61 6c 6c 20 74 61  olumns in all ta
2aa7c 62 6c 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  bles..  **.  ** 
2aa7d 54 68 65 20 66 69 72 73 74 20 6c 6f 6f 70 20 6a  The first loop j
2aa7e 75 73 74 20 63 68 65 63 6b 73 20 74 6f 20 73 65  ust checks to se
2aa7f 65 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61  e if there are a
2aa80 6e 79 20 22 2a 22 20 6f 70 65 72 61 74 6f 72 73  ny "*" operators
2aa81 0a 20 20 2a 2a 20 74 68 61 74 20 6e 65 65 64 20  .  ** that need 
2aa82 65 78 70 61 6e 64 69 6e 67 2e 0a 20 20 2a 2f 0a  expanding..  */.
2aa83 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 45 4c    for(k=0; k<pEL
2aa84 69 73 74 2d 3e 6e 45 78 70 72 3b 20 6b 2b 2b 29  ist->nExpr; k++)
2aa85 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d  {.    Expr *pE =
2aa86 20 70 45 4c 69 73 74 2d 3e 61 5b 6b 5d 2e 70 45   pEList->a[k].pE
2aa87 78 70 72 3b 0a 20 20 20 20 69 66 28 20 70 45 2d  xpr;.    if( pE-
2aa88 3e 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29 20 62 72  >op==TK_ALL ) br
2aa89 65 61 6b 3b 0a 20 20 20 20 61 73 73 65 72 74 28  eak;.    assert(
2aa8a 20 70 45 2d 3e 6f 70 21 3d 54 4b 5f 44 4f 54 20   pE->op!=TK_DOT 
2aa8b 7c 7c 20 70 45 2d 3e 70 52 69 67 68 74 21 3d 30  || pE->pRight!=0
2aa8c 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
2aa8d 70 45 2d 3e 6f 70 21 3d 54 4b 5f 44 4f 54 20 7c  pE->op!=TK_DOT |
2aa8e 7c 20 28 70 45 2d 3e 70 4c 65 66 74 21 3d 30 20  | (pE->pLeft!=0 
2aa8f 26 26 20 70 45 2d 3e 70 4c 65 66 74 2d 3e 6f 70  && pE->pLeft->op
2aa90 3d 3d 54 4b 5f 49 44 29 20 29 3b 0a 20 20 20 20  ==TK_ID) );.    
2aa91 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 44  if( pE->op==TK_D
2aa92 4f 54 20 26 26 20 70 45 2d 3e 70 52 69 67 68 74  OT && pE->pRight
2aa93 2d 3e 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29 20 62  ->op==TK_ALL ) b
2aa94 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20  reak;.  }.  if( 
2aa95 6b 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20  k<pEList->nExpr 
2aa96 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a  ){.    /*.    **
2aa97 20 49 66 20 77 65 20 67 65 74 20 68 65 72 65 20   If we get here 
2aa98 69 74 20 6d 65 61 6e 73 20 74 68 65 20 72 65 73  it means the res
2aa99 75 6c 74 20 73 65 74 20 63 6f 6e 74 61 69 6e 73  ult set contains
2aa9a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 22 2a 22   one or more "*"
2aa9b 0a 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72  .    ** operator
2aa9c 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62  s that need to b
2aa9d 65 20 65 78 70 61 6e 64 65 64 2e 20 20 4c 6f 6f  e expanded.  Loo
2aa9e 70 20 74 68 72 6f 75 67 68 20 65 61 63 68 20 65  p through each e
2aa9f 78 70 72 65 73 73 69 6f 6e 0a 20 20 20 20 2a 2a  xpression.    **
2aaa0 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
2aaa1 65 74 20 61 6e 64 20 65 78 70 61 6e 64 20 74 68  et and expand th
2aaa2 65 6d 20 6f 6e 65 20 62 79 20 6f 6e 65 2e 0a 20  em one by one.. 
2aaa3 20 20 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 74     */.    struct
2aaa4 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
2aaa5 61 20 3d 20 70 45 4c 69 73 74 2d 3e 61 3b 0a 20  a = pEList->a;. 
2aaa6 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4e 65     ExprList *pNe
2aaa7 77 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 66  w = 0;.    int f
2aaa8 6c 61 67 73 20 3d 20 70 50 61 72 73 65 2d 3e 64  lags = pParse->d
2aaa9 62 2d 3e 66 6c 61 67 73 3b 0a 20 20 20 20 69 6e  b->flags;.    in
2aaaa 74 20 6c 6f 6e 67 4e 61 6d 65 73 20 3d 20 28 66  t longNames = (f
2aaab 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 75  lags & SQLITE_Fu
2aaac 6c 6c 43 6f 6c 4e 61 6d 65 73 29 21 3d 30 0a 20  llColNames)!=0. 
2aaad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2aaae 20 20 20 20 20 26 26 20 28 66 6c 61 67 73 20 26       && (flags &
2aaaf 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c   SQLITE_ShortCol
2aab0 4e 61 6d 65 73 29 3d 3d 30 3b 0a 0a 20 20 20 20  Names)==0;..    
2aab1 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 45 4c 69 73  for(k=0; k<pELis
2aab2 74 2d 3e 6e 45 78 70 72 3b 20 6b 2b 2b 29 7b 0a  t->nExpr; k++){.
2aab3 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d        Expr *pE =
2aab4 20 61 5b 6b 5d 2e 70 45 78 70 72 3b 0a 20 20 20   a[k].pExpr;.   
2aab5 20 20 20 61 73 73 65 72 74 28 20 70 45 2d 3e 6f     assert( pE->o
2aab6 70 21 3d 54 4b 5f 44 4f 54 20 7c 7c 20 70 45 2d  p!=TK_DOT || pE-
2aab7 3e 70 52 69 67 68 74 21 3d 30 20 29 3b 0a 20 20  >pRight!=0 );.  
2aab8 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 21 3d      if( pE->op!=
2aab9 54 4b 5f 41 4c 4c 20 26 26 20 28 70 45 2d 3e 6f  TK_ALL && (pE->o
2aaba 70 21 3d 54 4b 5f 44 4f 54 20 7c 7c 20 70 45 2d  p!=TK_DOT || pE-
2aabb 3e 70 52 69 67 68 74 2d 3e 6f 70 21 3d 54 4b 5f  >pRight->op!=TK_
2aabc 41 4c 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ALL) ){.        
2aabd 2f 2a 20 54 68 69 73 20 70 61 72 74 69 63 75 6c  /* This particul
2aabe 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 64 6f  ar expression do
2aabf 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62  es not need to b
2aac0 65 20 65 78 70 61 6e 64 65 64 2e 0a 20 20 20 20  e expanded..    
2aac1 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70      */.        p
2aac2 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70  New = sqlite3Exp
2aac3 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
2aac4 73 65 2c 20 70 4e 65 77 2c 20 61 5b 6b 5d 2e 70  se, pNew, a[k].p
2aac5 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 69  Expr);.        i
2aac6 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20  f( pNew ){.     
2aac7 20 20 20 20 20 70 4e 65 77 2d 3e 61 5b 70 4e 65       pNew->a[pNe
2aac8 77 2d 3e 6e 45 78 70 72 2d 31 5d 2e 7a 4e 61 6d  w->nExpr-1].zNam
2aac9 65 20 3d 20 61 5b 6b 5d 2e 7a 4e 61 6d 65 3b 0a  e = a[k].zName;.
2aaca 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e            pNew->
2aacb 61 5b 70 4e 65 77 2d 3e 6e 45 78 70 72 2d 31 5d  a[pNew->nExpr-1]
2aacc 2e 7a 53 70 61 6e 20 3d 20 61 5b 6b 5d 2e 7a 53  .zSpan = a[k].zS
2aacd 70 61 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 61  pan;.          a
2aace 5b 6b 5d 2e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20  [k].zName = 0;. 
2aacf 20 20 20 20 20 20 20 20 20 61 5b 6b 5d 2e 7a 53           a[k].zS
2aad0 70 61 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  pan = 0;.       
2aad1 20 7d 0a 20 20 20 20 20 20 20 20 61 5b 6b 5d 2e   }.        a[k].
2aad2 70 45 78 70 72 20 3d 20 30 3b 0a 20 20 20 20 20  pExpr = 0;.     
2aad3 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2aad4 2f 2a 20 54 68 69 73 20 65 78 70 72 65 73 73 69  /* This expressi
2aad5 6f 6e 20 69 73 20 61 20 22 2a 22 20 6f 72 20 61  on is a "*" or a
2aad6 20 22 54 41 42 4c 45 2e 2a 22 20 61 6e 64 20 6e   "TABLE.*" and n
2aad7 65 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20 20  eeds to be.     
2aad8 20 20 20 2a 2a 20 65 78 70 61 6e 64 65 64 2e 20     ** expanded. 
2aad9 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 74  */.        int t
2aada 61 62 6c 65 53 65 65 6e 20 3d 20 30 3b 20 20 20  ableSeen = 0;   
2aadb 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 31 20 77     /* Set to 1 w
2aadc 68 65 6e 20 54 41 42 4c 45 20 6d 61 74 63 68 65  hen TABLE matche
2aadd 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 68 61  s */.        cha
2aade 72 20 2a 7a 54 4e 61 6d 65 3b 20 20 20 20 20 20  r *zTName;      
2aadf 20 20 20 20 20 20 2f 2a 20 74 65 78 74 20 6f 66        /* text of
2aae0 20 6e 61 6d 65 20 6f 66 20 54 41 42 4c 45 20 2a   name of TABLE *
2aae1 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 45  /.        if( pE
2aae2 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 29 7b 0a  ->op==TK_DOT ){.
2aae3 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
2aae4 28 20 70 45 2d 3e 70 4c 65 66 74 21 3d 30 20 29  ( pE->pLeft!=0 )
2aae5 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  ;.          asse
2aae6 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
2aae7 65 72 74 79 28 70 45 2d 3e 70 4c 65 66 74 2c 20  erty(pE->pLeft, 
2aae8 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a  EP_IntValue) );.
2aae9 20 20 20 20 20 20 20 20 20 20 7a 54 4e 61 6d 65            zTName
2aaea 20 3d 20 70 45 2d 3e 70 4c 65 66 74 2d 3e 75 2e   = pE->pLeft->u.
2aaeb 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20  zToken;.        
2aaec 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
2aaed 20 7a 54 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20   zTName = 0;.   
2aaee 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 66       }.        f
2aaef 6f 72 28 69 3d 30 2c 20 70 46 72 6f 6d 3d 70 54  or(i=0, pFrom=pT
2aaf0 61 62 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 54 61  abList->a; i<pTa
2aaf1 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  bList->nSrc; i++
2aaf2 2c 20 70 46 72 6f 6d 2b 2b 29 7b 0a 20 20 20 20  , pFrom++){.    
2aaf3 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61        Table *pTa
2aaf4 62 20 3d 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3b  b = pFrom->pTab;
2aaf5 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20  .          char 
2aaf6 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 70 46 72 6f  *zTabName = pFro
2aaf7 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20 20 20 20  m->zAlias;.     
2aaf8 20 20 20 20 20 69 66 28 20 7a 54 61 62 4e 61 6d       if( zTabNam
2aaf9 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  e==0 ){.        
2aafa 20 20 20 20 7a 54 61 62 4e 61 6d 65 20 3d 20 70      zTabName = p
2aafb 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20  Tab->zName;.    
2aafc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2aafd 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
2aafe 46 61 69 6c 65 64 20 29 20 62 72 65 61 6b 3b 0a  Failed ) break;.
2aaff 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 54            if( zT
2ab00 4e 61 6d 65 20 26 26 20 73 71 6c 69 74 65 33 53  Name && sqlite3S
2ab01 74 72 49 43 6d 70 28 7a 54 4e 61 6d 65 2c 20 7a  trICmp(zTName, z
2ab02 54 61 62 4e 61 6d 65 29 21 3d 30 20 29 7b 0a 20  TabName)!=0 ){. 
2ab03 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69             conti
2ab04 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  nue;.          }
2ab05 0a 20 20 20 20 20 20 20 20 20 20 74 61 62 6c 65  .          table
2ab06 53 65 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20  Seen = 1;.      
2ab07 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70      for(j=0; j<p
2ab08 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b  Tab->nCol; j++){
2ab09 0a 20 20 20 20 20 20 20 20 20 20 20 20 45 78 70  .            Exp
2ab0a 72 20 2a 70 45 78 70 72 2c 20 2a 70 52 69 67 68  r *pExpr, *pRigh
2ab0b 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63  t;.            c
2ab0c 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 54 61  har *zName = pTa
2ab0d 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65  b->aCol[j].zName
2ab0e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68  ;.            ch
2ab0f 61 72 20 2a 7a 43 6f 6c 6e 61 6d 65 3b 20 20 2f  ar *zColname;  /
2ab10 2a 20 54 68 65 20 63 6f 6d 70 75 74 65 64 20 63  * The computed c
2ab11 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20  olumn name */.  
2ab12 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
2ab13 7a 54 6f 46 72 65 65 3b 20 20 20 2f 2a 20 4d 61  zToFree;   /* Ma
2ab14 6c 6c 6f 63 65 64 20 73 74 72 69 6e 67 20 74 68  lloced string th
2ab15 61 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 66  at needs to be f
2ab16 72 65 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20  reed */.        
2ab17 20 20 20 20 54 6f 6b 65 6e 20 73 43 6f 6c 6e 61      Token sColna
2ab18 6d 65 3b 20 20 2f 2a 20 43 6f 6d 70 75 74 65 64  me;  /* Computed
2ab19 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61 73 20   column name as 
2ab1a 61 20 74 6f 6b 65 6e 20 2a 2f 0a 0a 20 20 20 20  a token */..    
2ab1b 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 20          /* If a 
2ab1c 63 6f 6c 75 6d 6e 20 69 73 20 6d 61 72 6b 65 64  column is marked
2ab1d 20 61 73 20 27 68 69 64 64 65 6e 27 20 28 63 75   as 'hidden' (cu
2ab1e 72 72 65 6e 74 6c 79 20 6f 6e 6c 79 20 70 6f 73  rrently only pos
2ab1f 73 69 62 6c 65 0a 20 20 20 20 20 20 20 20 20 20  sible.          
2ab20 20 20 2a 2a 20 66 6f 72 20 76 69 72 74 75 61 6c    ** for virtual
2ab21 20 74 61 62 6c 65 73 29 2c 20 64 6f 20 6e 6f 74   tables), do not
2ab22 20 69 6e 63 6c 75 64 65 20 69 74 20 69 6e 20 74   include it in t
2ab23 68 65 20 65 78 70 61 6e 64 65 64 0a 20 20 20 20  he expanded.    
2ab24 20 20 20 20 20 20 20 20 2a 2a 20 72 65 73 75 6c          ** resul
2ab25 74 2d 73 65 74 20 6c 69 73 74 2e 0a 20 20 20 20  t-set list..    
2ab26 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
2ab27 20 20 20 20 20 20 20 69 66 28 20 49 73 48 69 64         if( IsHid
2ab28 64 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61 62 2d  denColumn(&pTab-
2ab29 3e 61 43 6f 6c 5b 6a 5d 29 20 29 7b 0a 20 20 20  >aCol[j]) ){.   
2ab2a 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72             asser
2ab2b 74 28 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  t(IsVirtual(pTab
2ab2c 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ));.            
2ab2d 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
2ab2e 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20          }..     
2ab2f 20 20 20 20 20 20 20 69 66 28 20 69 3e 30 20 26         if( i>0 &
2ab30 26 20 7a 54 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20  & zTName==0 ){. 
2ab31 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 72               str
2ab32 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
2ab33 20 2a 70 4c 65 66 74 20 3d 20 26 70 54 61 62 4c   *pLeft = &pTabL
2ab34 69 73 74 2d 3e 61 5b 69 2d 31 5d 3b 0a 20 20 20  ist->a[i-1];.   
2ab35 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 28             if( (
2ab36 70 4c 65 66 74 5b 31 5d 2e 6a 6f 69 6e 74 79 70  pLeft[1].jointyp
2ab37 65 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c 29 21  e & JT_NATURAL)!
2ab38 3d 30 20 26 26 0a 20 20 20 20 20 20 20 20 20 20  =0 &&.          
2ab39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
2ab3a 6c 75 6d 6e 49 6e 64 65 78 28 70 4c 65 66 74 2d  lumnIndex(pLeft-
2ab3b 3e 70 54 61 62 2c 20 7a 4e 61 6d 65 29 3e 3d 30  >pTab, zName)>=0
2ab3c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2ab3d 20 20 20 20 2f 2a 20 49 6e 20 61 20 4e 41 54 55      /* In a NATU
2ab3e 52 41 4c 20 6a 6f 69 6e 2c 20 6f 6d 69 74 20 74  RAL join, omit t
2ab3f 68 65 20 6a 6f 69 6e 20 63 6f 6c 75 6d 6e 73 20  he join columns 
2ab40 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 20 20 20  from the .      
2ab41 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62            ** tab
2ab42 6c 65 20 6f 6e 20 74 68 65 20 72 69 67 68 74 20  le on the right 
2ab43 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  */.             
2ab44 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
2ab45 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
2ab46 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73             if( s
2ab47 71 6c 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65  qlite3IdListInde
2ab48 78 28 70 4c 65 66 74 5b 31 5d 2e 70 55 73 69 6e  x(pLeft[1].pUsin
2ab49 67 2c 20 7a 4e 61 6d 65 29 3e 3d 30 20 29 7b 0a  g, zName)>=0 ){.
2ab4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ab4b 2f 2a 20 49 6e 20 61 20 6a 6f 69 6e 20 77 69 74  /* In a join wit
2ab4c 68 20 61 20 55 53 49 4e 47 20 63 6c 61 75 73 65  h a USING clause
2ab4d 2c 20 6f 6d 69 74 20 63 6f 6c 75 6d 6e 73 20 69  , omit columns i
2ab4e 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20  n the.          
2ab4f 20 20 20 20 20 20 2a 2a 20 75 73 69 6e 67 20 63        ** using c
2ab50 6c 61 75 73 65 20 66 72 6f 6d 20 74 68 65 20 74  lause from the t
2ab51 61 62 6c 65 20 6f 6e 20 74 68 65 20 72 69 67 68  able on the righ
2ab52 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  t. */.          
2ab53 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
2ab54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a                }.
2ab55 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
2ab56 20 20 20 20 20 20 20 20 20 20 70 52 69 67 68 74            pRight
2ab57 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64   = sqlite3Expr(d
2ab58 62 2c 20 54 4b 5f 49 44 2c 20 7a 4e 61 6d 65 29  b, TK_ID, zName)
2ab59 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a 43  ;.            zC
2ab5a 6f 6c 6e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a  olname = zName;.
2ab5b 20 20 20 20 20 20 20 20 20 20 20 20 7a 54 6f 46              zToF
2ab5c 72 65 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ree = 0;.       
2ab5d 20 20 20 20 20 69 66 28 20 6c 6f 6e 67 4e 61 6d       if( longNam
2ab5e 65 73 20 7c 7c 20 70 54 61 62 4c 69 73 74 2d 3e  es || pTabList->
2ab5f 6e 53 72 63 3e 31 20 29 7b 0a 20 20 20 20 20 20  nSrc>1 ){.      
2ab60 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c          Expr *pL
2ab61 65 66 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20  eft;.           
2ab62 20 20 20 70 4c 65 66 74 20 3d 20 73 71 6c 69 74     pLeft = sqlit
2ab63 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 49 44  e3Expr(db, TK_ID
2ab64 2c 20 7a 54 61 62 4e 61 6d 65 29 3b 0a 20 20 20  , zTabName);.   
2ab65 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
2ab66 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2ab67 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20  pParse, TK_DOT, 
2ab68 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 30  pLeft, pRight, 0
2ab69 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
2ab6a 20 69 66 28 20 6c 6f 6e 67 4e 61 6d 65 73 20 29   if( longNames )
2ab6b 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
2ab6c 20 20 7a 43 6f 6c 6e 61 6d 65 20 3d 20 73 71 6c    zColname = sql
2ab6d 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
2ab6e 22 25 73 2e 25 73 22 2c 20 7a 54 61 62 4e 61 6d  "%s.%s", zTabNam
2ab6f 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  e, zName);.     
2ab70 20 20 20 20 20 20 20 20 20 20 20 7a 54 6f 46 72             zToFr
2ab71 65 65 20 3d 20 7a 43 6f 6c 6e 61 6d 65 3b 0a 20  ee = zColname;. 
2ab72 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
2ab73 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
2ab74 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
2ab75 70 45 78 70 72 20 3d 20 70 52 69 67 68 74 3b 0a  pExpr = pRight;.
2ab76 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
2ab77 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d            pNew =
2ab78 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2ab79 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70  Append(pParse, p
2ab7a 4e 65 77 2c 20 70 45 78 70 72 29 3b 0a 20 20 20  New, pExpr);.   
2ab7b 20 20 20 20 20 20 20 20 20 73 43 6f 6c 6e 61 6d           sColnam
2ab7c 65 2e 7a 20 3d 20 7a 43 6f 6c 6e 61 6d 65 3b 0a  e.z = zColname;.
2ab7d 20 20 20 20 20 20 20 20 20 20 20 20 73 43 6f 6c              sCol
2ab7e 6e 61 6d 65 2e 6e 20 3d 20 73 71 6c 69 74 65 33  name.n = sqlite3
2ab7f 53 74 72 6c 65 6e 33 30 28 7a 43 6f 6c 6e 61 6d  Strlen30(zColnam
2ab80 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  e);.            
2ab81 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53  sqlite3ExprListS
2ab82 65 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70  etName(pParse, p
2ab83 4e 65 77 2c 20 26 73 43 6f 6c 6e 61 6d 65 2c 20  New, &sColname, 
2ab84 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  0);.            
2ab85 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2ab86 2c 20 7a 54 6f 46 72 65 65 29 3b 0a 20 20 20 20  , zToFree);.    
2ab87 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2ab88 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 74  }.        if( !t
2ab89 61 62 6c 65 53 65 65 6e 20 29 7b 0a 20 20 20 20  ableSeen ){.    
2ab8a 20 20 20 20 20 20 69 66 28 20 7a 54 4e 61 6d 65        if( zTName
2ab8b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2ab8c 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2ab8d 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68  pParse, "no such
2ab8e 20 74 61 62 6c 65 3a 20 25 73 22 2c 20 7a 54 4e   table: %s", zTN
2ab8f 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ame);.          
2ab90 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
2ab91 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2ab92 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 74  sg(pParse, "no t
2ab93 61 62 6c 65 73 20 73 70 65 63 69 66 69 65 64 22  ables specified"
2ab94 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
2ab95 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
2ab96 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
2ab97 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
2ab98 28 64 62 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  (db, pEList);.  
2ab99 20 20 70 2d 3e 70 45 4c 69 73 74 20 3d 20 70 4e    p->pEList = pN
2ab9a 65 77 3b 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49  ew;.  }.#if SQLI
2ab9b 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20  TE_MAX_COLUMN.  
2ab9c 69 66 28 20 70 2d 3e 70 45 4c 69 73 74 20 26 26  if( p->pEList &&
2ab9d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70   p->pEList->nExp
2ab9e 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  r>db->aLimit[SQL
2ab9f 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e  ITE_LIMIT_COLUMN
2aba0 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ] ){.    sqlite3
2aba1 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2aba2 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d   "too many colum
2aba3 6e 73 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74  ns in result set
2aba4 22 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ");.  }.#endif. 
2aba5 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74   return WRC_Cont
2aba6 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4e  inue;.}../*.** N
2aba7 6f 2d 6f 70 20 72 6f 75 74 69 6e 65 20 66 6f 72  o-op routine for
2aba8 20 74 68 65 20 70 61 72 73 65 2d 74 72 65 65 20   the parse-tree 
2aba9 77 61 6c 6b 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 68  walker..**.** Wh
2abaa 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
2abab 69 73 20 74 68 65 20 57 61 6c 6b 65 72 2e 78 45  is the Walker.xE
2abac 78 70 72 43 61 6c 6c 62 61 63 6b 20 74 68 65 6e  xprCallback then
2abad 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   expression tree
2abae 73 0a 2a 2a 20 61 72 65 20 77 61 6c 6b 65 64 20  s.** are walked 
2abaf 77 69 74 68 6f 75 74 20 61 6e 79 20 61 63 74 69  without any acti
2abb0 6f 6e 73 20 62 65 69 6e 67 20 74 61 6b 65 6e 20  ons being taken 
2abb1 61 74 20 65 61 63 68 20 6e 6f 64 65 2e 20 20 50  at each node.  P
2abb2 72 65 73 75 6d 61 62 6c 79 2c 0a 2a 2a 20 77 68  resumably,.** wh
2abb3 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
2abb4 69 73 20 75 73 65 64 20 66 6f 72 20 57 61 6c 6b  is used for Walk
2abb5 65 72 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b  er.xExprCallback
2abb6 20 74 68 65 6e 20 0a 2a 2a 20 57 61 6c 6b 65 72   then .** Walker
2abb7 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b  .xSelectCallback
2abb8 20 69 73 20 73 65 74 20 74 6f 20 64 6f 20 73 6f   is set to do so
2abb9 6d 65 74 68 69 6e 67 20 75 73 65 66 75 6c 20 66  mething useful f
2abba 6f 72 20 65 76 65 72 79 20 0a 2a 2a 20 73 75 62  or every .** sub
2abbb 71 75 65 72 79 20 69 6e 20 74 68 65 20 70 61 72  query in the par
2abbc 73 65 72 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 61  ser tree..*/.sta
2abbd 74 69 63 20 69 6e 74 20 65 78 70 72 57 61 6c 6b  tic int exprWalk
2abbe 4e 6f 6f 70 28 57 61 6c 6b 65 72 20 2a 4e 6f 74  Noop(Walker *Not
2abbf 55 73 65 64 2c 20 45 78 70 72 20 2a 4e 6f 74 55  Used, Expr *NotU
2abc0 73 65 64 32 29 7b 0a 20 20 55 4e 55 53 45 44 5f  sed2){.  UNUSED_
2abc1 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73  PARAMETER2(NotUs
2abc2 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20  ed, NotUsed2);. 
2abc3 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74   return WRC_Cont
2abc4 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  inue;.}../*.** T
2abc5 68 69 73 20 72 6f 75 74 69 6e 65 20 22 65 78 70  his routine "exp
2abc6 61 6e 64 73 22 20 61 20 53 45 4c 45 43 54 20 73  ands" a SELECT s
2abc7 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 61 6c 6c  tatement and all
2abc8 20 6f 66 20 69 74 73 20 73 75 62 71 75 65 72 69   of its subqueri
2abc9 65 73 2e 0a 2a 2a 20 46 6f 72 20 61 64 64 69 74  es..** For addit
2abca 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
2abcb 6e 20 6f 6e 20 77 68 61 74 20 69 74 20 6d 65 61  n on what it mea
2abcc 6e 73 20 74 6f 20 22 65 78 70 61 6e 64 22 20 61  ns to "expand" a
2abcd 20 53 45 4c 45 43 54 0a 2a 2a 20 73 74 61 74 65   SELECT.** state
2abce 6d 65 6e 74 2c 20 73 65 65 20 74 68 65 20 63 6f  ment, see the co
2abcf 6d 6d 65 6e 74 20 6f 6e 20 74 68 65 20 73 65 6c  mment on the sel
2abd0 65 63 74 45 78 70 61 6e 64 20 77 6f 72 6b 65 72  ectExpand worker
2abd1 20 63 61 6c 6c 62 61 63 6b 20 61 62 6f 76 65 2e   callback above.
2abd2 0a 2a 2a 0a 2a 2a 20 45 78 70 61 6e 64 69 6e 67  .**.** Expanding
2abd3 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
2abd4 65 6e 74 20 69 73 20 74 68 65 20 66 69 72 73 74  ent is the first
2abd5 20 73 74 65 70 20 69 6e 20 70 72 6f 63 65 73 73   step in process
2abd6 69 6e 67 20 61 0a 2a 2a 20 53 45 4c 45 43 54 20  ing a.** SELECT 
2abd7 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20  statement.  The 
2abd8 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2abd9 20 6d 75 73 74 20 62 65 20 65 78 70 61 6e 64 65   must be expande
2abda 64 20 62 65 66 6f 72 65 0a 2a 2a 20 6e 61 6d 65  d before.** name
2abdb 20 72 65 73 6f 6c 75 74 69 6f 6e 20 69 73 20 70   resolution is p
2abdc 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20  erformed..**.** 
2abdd 49 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73  If anything goes
2abde 20 77 72 6f 6e 67 2c 20 61 6e 20 65 72 72 6f 72   wrong, an error
2abdf 20 6d 65 73 73 61 67 65 20 69 73 20 77 72 69 74   message is writ
2abe0 74 65 6e 20 69 6e 74 6f 20 70 50 61 72 73 65 2e  ten into pParse.
2abe1 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20  .** The calling 
2abe2 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 64 65 74  function can det
2abe3 65 63 74 20 74 68 65 20 70 72 6f 62 6c 65 6d 20  ect the problem 
2abe4 62 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 70 50  by looking at pP
2abe5 61 72 73 65 2d 3e 6e 45 72 72 0a 2a 2a 20 61 6e  arse->nErr.** an
2abe6 64 2f 6f 72 20 70 50 61 72 73 65 2d 3e 64 62 2d  d/or pParse->db-
2abe7 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 2e 0a 2a  >mallocFailed..*
2abe8 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  /.static void sq
2abe9 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e  lite3SelectExpan
2abea 64 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  d(Parse *pParse,
2abeb 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
2abec 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20  ){.  Walker w;. 
2abed 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61   w.xSelectCallba
2abee 63 6b 20 3d 20 73 65 6c 65 63 74 45 78 70 61 6e  ck = selectExpan
2abef 64 65 72 3b 0a 20 20 77 2e 78 45 78 70 72 43 61  der;.  w.xExprCa
2abf0 6c 6c 62 61 63 6b 20 3d 20 65 78 70 72 57 61 6c  llback = exprWal
2abf1 6b 4e 6f 6f 70 3b 0a 20 20 77 2e 70 50 61 72 73  kNoop;.  w.pPars
2abf2 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 71  e = pParse;.  sq
2abf3 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28  lite3WalkSelect(
2abf4 26 77 2c 20 70 53 65 6c 65 63 74 29 3b 0a 7d 0a  &w, pSelect);.}.
2abf5 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2abf6 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 2f  _OMIT_SUBQUERY./
2abf7 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 57  *.** This is a W
2abf8 61 6c 6b 65 72 2e 78 53 65 6c 65 63 74 43 61 6c  alker.xSelectCal
2abf9 6c 62 61 63 6b 20 63 61 6c 6c 62 61 63 6b 20 66  lback callback f
2abfa 6f 72 20 74 68 65 20 73 71 6c 69 74 65 33 53 65  or the sqlite3Se
2abfb 6c 65 63 74 54 79 70 65 49 6e 66 6f 28 29 0a 2a  lectTypeInfo().*
2abfc 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a  * interface..**.
2abfd 2a 2a 20 46 6f 72 20 65 61 63 68 20 46 52 4f 4d  ** For each FROM
2abfe 2d 63 6c 61 75 73 65 20 73 75 62 71 75 65 72 79  -clause subquery
2abff 2c 20 61 64 64 20 43 6f 6c 75 6d 6e 2e 7a 54 79  , add Column.zTy
2ac00 70 65 20 61 6e 64 20 43 6f 6c 75 6d 6e 2e 7a 43  pe and Column.zC
2ac01 6f 6c 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  oll.** informati
2ac02 6f 6e 20 74 6f 20 74 68 65 20 54 61 62 6c 65 20  on to the Table 
2ac03 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 72  structure that r
2ac04 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 72 65  epresents the re
2ac05 73 75 6c 74 20 73 65 74 0a 2a 2a 20 6f 66 20 74  sult set.** of t
2ac06 68 61 74 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a  hat subquery..**
2ac07 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65 20 73 74  .** The Table st
2ac08 72 75 63 74 75 72 65 20 74 68 61 74 20 72 65 70  ructure that rep
2ac09 72 65 73 65 6e 74 73 20 74 68 65 20 72 65 73 75  resents the resu
2ac0a 6c 74 20 73 65 74 20 77 61 73 20 63 6f 6e 73 74  lt set was const
2ac0b 72 75 63 74 65 64 0a 2a 2a 20 62 79 20 73 65 6c  ructed.** by sel
2ac0c 65 63 74 45 78 70 61 6e 64 65 72 28 29 20 62 75  ectExpander() bu
2ac0d 74 20 74 68 65 20 74 79 70 65 20 61 6e 64 20 63  t the type and c
2ac0e 6f 6c 6c 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61  ollation informa
2ac0f 74 69 6f 6e 20 77 61 73 20 6f 6d 69 74 74 65 64  tion was omitted
2ac10 0a 2a 2a 20 61 74 20 74 68 61 74 20 70 6f 69 6e  .** at that poin
2ac11 74 20 62 65 63 61 75 73 65 20 69 64 65 6e 74 69  t because identi
2ac12 66 69 65 72 73 20 68 61 64 20 6e 6f 74 20 79 65  fiers had not ye
2ac13 74 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e  t been resolved.
2ac14 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e    This.** routin
2ac15 65 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65  e is called afte
2ac16 72 20 69 64 65 6e 74 69 66 69 65 72 20 72 65 73  r identifier res
2ac17 6f 6c 75 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  olution..*/.stat
2ac18 69 63 20 69 6e 74 20 73 65 6c 65 63 74 41 64 64  ic int selectAdd
2ac19 53 75 62 71 75 65 72 79 54 79 70 65 49 6e 66 6f  SubqueryTypeInfo
2ac1a 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72  (Walker *pWalker
2ac1b 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20  , Select *p){.  
2ac1c 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 20  Parse *pParse;. 
2ac1d 20 69 6e 74 20 69 3b 0a 20 20 53 72 63 4c 69 73   int i;.  SrcLis
2ac1e 74 20 2a 70 54 61 62 4c 69 73 74 3b 0a 20 20 73  t *pTabList;.  s
2ac1f 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
2ac20 65 6d 20 2a 70 46 72 6f 6d 3b 0a 0a 20 20 61 73  em *pFrom;..  as
2ac21 73 65 72 74 28 20 70 2d 3e 73 65 6c 46 6c 61 67  sert( p->selFlag
2ac22 73 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 20  s & SF_Resolved 
2ac23 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d  );.  assert( (p-
2ac24 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 48  >selFlags & SF_H
2ac25 61 73 54 79 70 65 49 6e 66 6f 29 3d 3d 30 20 29  asTypeInfo)==0 )
2ac26 3b 0a 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20  ;.  p->selFlags 
2ac27 7c 3d 20 53 46 5f 48 61 73 54 79 70 65 49 6e 66  |= SF_HasTypeInf
2ac28 6f 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 70 57  o;.  pParse = pW
2ac29 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20  alker->pParse;. 
2ac2a 20 70 54 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70   pTabList = p->p
2ac2b 53 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20  Src;.  for(i=0, 
2ac2c 70 46 72 6f 6d 3d 70 54 61 62 4c 69 73 74 2d 3e  pFrom=pTabList->
2ac2d 61 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e  a; i<pTabList->n
2ac2e 53 72 63 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d 2b  Src; i++, pFrom+
2ac2f 2b 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  +){.    Table *p
2ac30 54 61 62 20 3d 20 70 46 72 6f 6d 2d 3e 70 54 61  Tab = pFrom->pTa
2ac31 62 3b 0a 20 20 20 20 69 66 28 20 41 4c 57 41 59  b;.    if( ALWAY
2ac32 53 28 70 54 61 62 21 3d 30 29 20 26 26 20 28 70  S(pTab!=0) && (p
2ac33 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  Tab->tabFlags & 
2ac34 54 46 5f 45 70 68 65 6d 65 72 61 6c 29 21 3d 30  TF_Ephemeral)!=0
2ac35 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 73   ){.      /* A s
2ac36 75 62 2d 71 75 65 72 79 20 69 6e 20 74 68 65 20  ub-query in the 
2ac37 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61  FROM clause of a
2ac38 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 20 20 20   SELECT */.     
2ac39 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 20 3d 20   Select *pSel = 
2ac3a 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a  pFrom->pSelect;.
2ac3b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53        assert( pS
2ac3c 65 6c 20 29 3b 0a 20 20 20 20 20 20 77 68 69 6c  el );.      whil
2ac3d 65 28 20 70 53 65 6c 2d 3e 70 50 72 69 6f 72 20  e( pSel->pPrior 
2ac3e 29 20 70 53 65 6c 20 3d 20 70 53 65 6c 2d 3e 70  ) pSel = pSel->p
2ac3f 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 73 65 6c  Prior;.      sel
2ac40 65 63 74 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65  ectAddColumnType
2ac41 41 6e 64 43 6f 6c 6c 61 74 69 6f 6e 28 70 50 61  AndCollation(pPa
2ac42 72 73 65 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c  rse, pTab->nCol,
2ac43 20 70 54 61 62 2d 3e 61 43 6f 6c 2c 20 70 53 65   pTab->aCol, pSe
2ac44 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  l);.    }.  }.  
2ac45 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69  return WRC_Conti
2ac46 6e 75 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a  nue;.}.#endif...
2ac47 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
2ac48 6e 65 20 61 64 64 73 20 64 61 74 61 74 79 70 65  ne adds datatype
2ac49 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73   and collating s
2ac4a 65 71 75 65 6e 63 65 20 69 6e 66 6f 72 6d 61 74  equence informat
2ac4b 69 6f 6e 20 74 6f 0a 2a 2a 20 74 68 65 20 54 61  ion to.** the Ta
2ac4c 62 6c 65 20 73 74 72 75 63 74 75 72 65 73 20 6f  ble structures o
2ac4d 66 20 61 6c 6c 20 46 52 4f 4d 2d 63 6c 61 75 73  f all FROM-claus
2ac4e 65 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20  e subqueries in 
2ac4f 61 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74  a.** SELECT stat
2ac50 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 55 73 65  ement..**.** Use
2ac51 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 66   this routine af
2ac52 74 65 72 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74  ter name resolut
2ac53 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
2ac54 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63  oid sqlite3Selec
2ac55 74 41 64 64 54 79 70 65 49 6e 66 6f 28 50 61 72  tAddTypeInfo(Par
2ac56 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65 6c 65  se *pParse, Sele
2ac57 63 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a 23 69  ct *pSelect){.#i
2ac58 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2ac59 54 5f 53 55 42 51 55 45 52 59 0a 20 20 57 61 6c  T_SUBQUERY.  Wal
2ac5a 6b 65 72 20 77 3b 0a 20 20 77 2e 78 53 65 6c 65  ker w;.  w.xSele
2ac5b 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 73 65 6c  ctCallback = sel
2ac5c 65 63 74 41 64 64 53 75 62 71 75 65 72 79 54 79  ectAddSubqueryTy
2ac5d 70 65 49 6e 66 6f 3b 0a 20 20 77 2e 78 45 78 70  peInfo;.  w.xExp
2ac5e 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65 78 70 72  rCallback = expr
2ac5f 57 61 6c 6b 4e 6f 6f 70 3b 0a 20 20 77 2e 70 50  WalkNoop;.  w.pP
2ac60 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20  arse = pParse;. 
2ac61 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65   sqlite3WalkSele
2ac62 63 74 28 26 77 2c 20 70 53 65 6c 65 63 74 29 3b  ct(&w, pSelect);
2ac63 0a 23 65 6e 64 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a  .#endif.}.../*.*
2ac64 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
2ac65 65 74 73 20 6f 66 20 61 20 53 45 4c 45 43 54 20  ets of a SELECT 
2ac66 73 74 61 74 65 6d 65 6e 74 20 66 6f 72 20 70 72  statement for pr
2ac67 6f 63 65 73 73 69 6e 67 2e 20 20 54 68 65 0a 2a  ocessing.  The.*
2ac68 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61  * following is a
2ac69 63 63 6f 6d 70 6c 69 73 68 65 64 3a 0a 2a 2a 0a  ccomplished:.**.
2ac6a 2a 2a 20 20 20 20 20 2a 20 20 56 44 42 45 20 43  **     *  VDBE C
2ac6b 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 61 72  ursor numbers ar
2ac6c 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20 61 6c  e assigned to al
2ac6d 6c 20 46 52 4f 4d 2d 63 6c 61 75 73 65 20 74 65  l FROM-clause te
2ac6e 72 6d 73 2e 0a 2a 2a 20 20 20 20 20 2a 20 20 45  rms..**     *  E
2ac6f 70 68 65 6d 65 72 61 6c 20 54 61 62 6c 65 20 6f  phemeral Table o
2ac70 62 6a 65 63 74 73 20 61 72 65 20 63 72 65 61 74  bjects are creat
2ac71 65 64 20 66 6f 72 20 61 6c 6c 20 46 52 4f 4d 2d  ed for all FROM-
2ac72 63 6c 61 75 73 65 20 73 75 62 71 75 65 72 69 65  clause subquerie
2ac73 73 2e 0a 2a 2a 20 20 20 20 20 2a 20 20 4f 4e 20  s..**     *  ON 
2ac74 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65  and USING clause
2ac75 73 20 61 72 65 20 73 68 69 66 74 65 64 20 69 6e  s are shifted in
2ac76 74 6f 20 57 48 45 52 45 20 73 74 61 74 65 6d 65  to WHERE stateme
2ac77 6e 74 73 0a 2a 2a 20 20 20 20 20 2a 20 20 57 69  nts.**     *  Wi
2ac78 6c 64 63 61 72 64 73 20 22 2a 22 20 61 6e 64 20  ldcards "*" and 
2ac79 22 54 41 42 4c 45 2e 2a 22 20 69 6e 20 72 65 73  "TABLE.*" in res
2ac7a 75 6c 74 20 73 65 74 73 20 61 72 65 20 65 78 70  ult sets are exp
2ac7b 61 6e 64 65 64 2e 0a 2a 2a 20 20 20 20 20 2a 20  anded..**     * 
2ac7c 20 49 64 65 6e 74 69 66 69 65 72 73 20 69 6e 20   Identifiers in 
2ac7d 65 78 70 72 65 73 73 69 6f 6e 20 61 72 65 20 6d  expression are m
2ac7e 61 74 63 68 65 64 20 74 6f 20 74 61 62 6c 65 73  atched to tables
2ac7f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
2ac80 74 69 6e 65 20 61 63 74 73 20 72 65 63 75 72 73  tine acts recurs
2ac81 69 76 65 6c 79 20 6f 6e 20 61 6c 6c 20 73 75 62  ively on all sub
2ac82 71 75 65 72 69 65 73 20 77 69 74 68 69 6e 20 74  queries within t
2ac83 68 65 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a 53 51  he SELECT..*/.SQ
2ac84 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2ac85 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50  d sqlite3SelectP
2ac86 72 65 70 28 0a 20 20 50 61 72 73 65 20 2a 70 50  rep(.  Parse *pP
2ac87 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  arse,         /*
2ac88 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74   The parser cont
2ac89 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ext */.  Select 
2ac8a 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
2ac8b 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74  /* The SELECT st
2ac8c 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f  atement being co
2ac8d 64 65 64 2e 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f  ded. */.  NameCo
2ac8e 6e 74 65 78 74 20 2a 70 4f 75 74 65 72 4e 43 20  ntext *pOuterNC 
2ac8f 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74   /* Name context
2ac90 20 66 6f 72 20 63 6f 6e 74 61 69 6e 65 72 20 2a   for container *
2ac91 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  /.){.  sqlite3 *
2ac92 64 62 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28  db;.  if( NEVER(
2ac93 70 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0a  p==0) ) return;.
2ac94 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
2ac95 62 3b 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c 46  b;.  if( p->selF
2ac96 6c 61 67 73 20 26 20 53 46 5f 48 61 73 54 79 70  lags & SF_HasTyp
2ac97 65 49 6e 66 6f 20 29 20 72 65 74 75 72 6e 3b 0a  eInfo ) return;.
2ac98 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45    sqlite3SelectE
2ac99 78 70 61 6e 64 28 70 50 61 72 73 65 2c 20 70 29  xpand(pParse, p)
2ac9a 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
2ac9b 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  nErr || db->mall
2ac9c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72  ocFailed ) retur
2ac9d 6e 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 73 6f  n;.  sqlite3Reso
2ac9e 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 70  lveSelectNames(p
2ac9f 50 61 72 73 65 2c 20 70 2c 20 70 4f 75 74 65 72  Parse, p, pOuter
2aca0 4e 43 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73  NC);.  if( pPars
2aca1 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d  e->nErr || db->m
2aca2 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65  allocFailed ) re
2aca3 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 53  turn;.  sqlite3S
2aca4 65 6c 65 63 74 41 64 64 54 79 70 65 49 6e 66 6f  electAddTypeInfo
2aca5 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 7d 0a 0a  (pParse, p);.}..
2aca6 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 74 68 65 20  /*.** Reset the 
2aca7 61 67 67 72 65 67 61 74 65 20 61 63 63 75 6d 75  aggregate accumu
2aca8 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  lator..**.** The
2aca9 20 61 67 67 72 65 67 61 74 65 20 61 63 63 75 6d   aggregate accum
2acaa 75 6c 61 74 6f 72 20 69 73 20 61 20 73 65 74 20  ulator is a set 
2acab 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  of memory cells 
2acac 74 68 61 74 20 68 6f 6c 64 0a 2a 2a 20 69 6e 74  that hold.** int
2acad 65 72 6d 65 64 69 61 74 65 20 72 65 73 75 6c 74  ermediate result
2acae 73 20 77 68 69 6c 65 20 63 61 6c 63 75 6c 61 74  s while calculat
2acaf 69 6e 67 20 61 6e 20 61 67 67 72 65 67 61 74 65  ing an aggregate
2acb0 2e 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69  .  This.** routi
2acb1 6e 65 20 73 69 6d 70 6c 79 20 73 74 6f 72 65 73  ne simply stores
2acb2 20 4e 55 4c 4c 73 20 69 6e 20 61 6c 6c 20 6f 66   NULLs in all of
2acb3 20 74 68 6f 73 65 20 6d 65 6d 6f 72 79 20 63 65   those memory ce
2acb4 6c 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  lls..*/.static v
2acb5 6f 69 64 20 72 65 73 65 74 41 63 63 75 6d 75 6c  oid resetAccumul
2acb6 61 74 6f 72 28 50 61 72 73 65 20 2a 70 50 61 72  ator(Parse *pPar
2acb7 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41 67  se, AggInfo *pAg
2acb8 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20 2a  gInfo){.  Vdbe *
2acb9 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
2acba 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74  e;.  int i;.  st
2acbb 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e  ruct AggInfo_fun
2acbc 63 20 2a 70 46 75 6e 63 3b 0a 20 20 69 66 28 20  c *pFunc;.  if( 
2acbd 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 2b  pAggInfo->nFunc+
2acbe 70 41 67 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d  pAggInfo->nColum
2acbf 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  n==0 ){.    retu
2acc0 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  rn;.  }.  for(i=
2acc1 30 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e  0; i<pAggInfo->n
2acc2 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
2acc3 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2acc4 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
2acc5 30 2c 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f  0, pAggInfo->aCo
2acc6 6c 5b 69 5d 2e 69 4d 65 6d 29 3b 0a 20 20 7d 0a  l[i].iMem);.  }.
2acc7 20 20 66 6f 72 28 70 46 75 6e 63 3d 70 41 67 67    for(pFunc=pAgg
2acc8 49 6e 66 6f 2d 3e 61 46 75 6e 63 2c 20 69 3d 30  Info->aFunc, i=0
2acc9 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46  ; i<pAggInfo->nF
2acca 75 6e 63 3b 20 69 2b 2b 2c 20 70 46 75 6e 63 2b  unc; i++, pFunc+
2accb 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  +){.    sqlite3V
2accc 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2accd 4e 75 6c 6c 2c 20 30 2c 20 70 46 75 6e 63 2d 3e  Null, 0, pFunc->
2acce 69 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 70  iMem);.    if( p
2accf 46 75 6e 63 2d 3e 69 44 69 73 74 69 6e 63 74 3e  Func->iDistinct>
2acd0 3d 30 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72  =0 ){.      Expr
2acd1 20 2a 70 45 20 3d 20 70 46 75 6e 63 2d 3e 70 45   *pE = pFunc->pE
2acd2 78 70 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72  xpr;.      asser
2acd3 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  t( !ExprHasPrope
2acd4 72 74 79 28 70 45 2c 20 45 50 5f 78 49 73 53 65  rty(pE, EP_xIsSe
2acd5 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 69  lect) );.      i
2acd6 66 28 20 70 45 2d 3e 78 2e 70 4c 69 73 74 3d 3d  f( pE->x.pList==
2acd7 30 20 7c 7c 20 70 45 2d 3e 78 2e 70 4c 69 73 74  0 || pE->x.pList
2acd8 2d 3e 6e 45 78 70 72 21 3d 31 20 29 7b 0a 20 20  ->nExpr!=1 ){.  
2acd9 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
2acda 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 44  orMsg(pParse, "D
2acdb 49 53 54 49 4e 43 54 20 61 67 67 72 65 67 61 74  ISTINCT aggregat
2acdc 65 73 20 6d 75 73 74 20 68 61 76 65 20 65 78 61  es must have exa
2acdd 63 74 6c 79 20 6f 6e 65 20 22 0a 20 20 20 20 20  ctly one ".     
2acde 20 20 20 20 20 20 22 61 72 67 75 6d 65 6e 74 22        "argument"
2acdf 29 3b 0a 20 20 20 20 20 20 20 20 70 46 75 6e 63  );.        pFunc
2ace0 2d 3e 69 44 69 73 74 69 6e 63 74 20 3d 20 2d 31  ->iDistinct = -1
2ace1 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
2ace2 20 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a         KeyInfo *
2ace3 70 4b 65 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e  pKeyInfo = keyIn
2ace4 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70  foFromExprList(p
2ace5 50 61 72 73 65 2c 20 70 45 2d 3e 78 2e 70 4c 69  Parse, pE->x.pLi
2ace6 73 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  st);.        sql
2ace7 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2ace8 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  , OP_OpenEphemer
2ace9 61 6c 2c 20 70 46 75 6e 63 2d 3e 69 44 69 73 74  al, pFunc->iDist
2acea 69 6e 63 74 2c 20 30 2c 20 30 2c 0a 20 20 20 20  inct, 0, 0,.    
2aceb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2acec 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65        (char*)pKe
2aced 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46  yInfo, P4_KEYINF
2acee 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20  O_HANDOFF);.    
2acef 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a    }.    }.  }.}.
2acf0 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68  ./*.** Invoke th
2acf1 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c 69 7a 65  e OP_AggFinalize
2acf2 20 6f 70 63 6f 64 65 20 66 6f 72 20 65 76 65 72   opcode for ever
2acf3 79 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  y aggregate func
2acf4 74 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 41  tion.** in the A
2acf5 67 67 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  ggInfo structure
2acf6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2acf7 20 66 69 6e 61 6c 69 7a 65 41 67 67 46 75 6e 63   finalizeAggFunc
2acf8 74 69 6f 6e 73 28 50 61 72 73 65 20 2a 70 50 61  tions(Parse *pPa
2acf9 72 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41  rse, AggInfo *pA
2acfa 67 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20  ggInfo){.  Vdbe 
2acfb 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
2acfc 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  be;.  int i;.  s
2acfd 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75  truct AggInfo_fu
2acfe 6e 63 20 2a 70 46 3b 0a 20 20 66 6f 72 28 69 3d  nc *pF;.  for(i=
2acff 30 2c 20 70 46 3d 70 41 67 67 49 6e 66 6f 2d 3e  0, pF=pAggInfo->
2ad00 61 46 75 6e 63 3b 20 69 3c 70 41 67 67 49 6e 66  aFunc; i<pAggInf
2ad01 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20 70  o->nFunc; i++, p
2ad02 46 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 4c 69  F++){.    ExprLi
2ad03 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 46 2d 3e  st *pList = pF->
2ad04 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a  pExpr->x.pList;.
2ad05 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
2ad06 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 46 2d  rHasProperty(pF-
2ad07 3e 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65  >pExpr, EP_xIsSe
2ad08 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 73 71 6c  lect) );.    sql
2ad09 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2ad0a 2c 20 4f 50 5f 41 67 67 46 69 6e 61 6c 2c 20 70  , OP_AggFinal, p
2ad0b 46 2d 3e 69 4d 65 6d 2c 20 70 4c 69 73 74 20 3f  F->iMem, pList ?
2ad0c 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20   pList->nExpr : 
2ad0d 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20  0, 0,.          
2ad0e 20 20 20 20 20 20 20 20 20 20 20 20 28 76 6f 69              (voi
2ad0f 64 2a 29 70 46 2d 3e 70 46 75 6e 63 2c 20 50 34  d*)pF->pFunc, P4
2ad10 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20 7d 0a 7d  _FUNCDEF);.  }.}
2ad11 0a 0a 2f 2a 0a 2a 2a 20 55 70 64 61 74 65 20 74  ../*.** Update t
2ad12 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 6d  he accumulator m
2ad13 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66 6f 72 20  emory cells for 
2ad14 61 6e 20 61 67 67 72 65 67 61 74 65 20 62 61 73  an aggregate bas
2ad15 65 64 20 6f 6e 0a 2a 2a 20 74 68 65 20 63 75 72  ed on.** the cur
2ad16 72 65 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 69  rent cursor posi
2ad17 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
2ad18 76 6f 69 64 20 75 70 64 61 74 65 41 63 63 75 6d  void updateAccum
2ad19 75 6c 61 74 6f 72 28 50 61 72 73 65 20 2a 70 50  ulator(Parse *pP
2ad1a 61 72 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70  arse, AggInfo *p
2ad1b 41 67 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65  AggInfo){.  Vdbe
2ad1c 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
2ad1d 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  dbe;.  int i;.  
2ad1e 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66  struct AggInfo_f
2ad1f 75 6e 63 20 2a 70 46 3b 0a 20 20 73 74 72 75 63  unc *pF;.  struc
2ad20 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70  t AggInfo_col *p
2ad21 43 3b 0a 0a 20 20 70 41 67 67 49 6e 66 6f 2d 3e  C;..  pAggInfo->
2ad22 64 69 72 65 63 74 4d 6f 64 65 20 3d 20 31 3b 0a  directMode = 1;.
2ad23 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
2ad24 68 65 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b  heClear(pParse);
2ad25 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 3d 70  .  for(i=0, pF=p
2ad26 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b 20  AggInfo->aFunc; 
2ad27 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e  i<pAggInfo->nFun
2ad28 63 3b 20 69 2b 2b 2c 20 70 46 2b 2b 29 7b 0a 20  c; i++, pF++){. 
2ad29 20 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 20     int nArg;.   
2ad2a 20 69 6e 74 20 61 64 64 72 4e 65 78 74 20 3d 20   int addrNext = 
2ad2b 30 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 41 67  0;.    int regAg
2ad2c 67 3b 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20  g;.    ExprList 
2ad2d 2a 70 4c 69 73 74 20 3d 20 70 46 2d 3e 70 45 78  *pList = pF->pEx
2ad2e 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20  pr->x.pList;.   
2ad2f 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
2ad30 73 50 72 6f 70 65 72 74 79 28 70 46 2d 3e 70 45  sProperty(pF->pE
2ad31 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
2ad32 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c  t) );.    if( pL
2ad33 69 73 74 20 29 7b 0a 20 20 20 20 20 20 6e 41 72  ist ){.      nAr
2ad34 67 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72  g = pList->nExpr
2ad35 3b 0a 20 20 20 20 20 20 72 65 67 41 67 67 20 3d  ;.      regAgg =
2ad36 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2ad37 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 41 72  ange(pParse, nAr
2ad38 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  g);.      sqlite
2ad39 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73  3ExprCodeExprLis
2ad3a 74 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c  t(pParse, pList,
2ad3b 20 72 65 67 41 67 67 2c 20 30 29 3b 0a 20 20 20   regAgg, 0);.   
2ad3c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 41   }else{.      nA
2ad3d 72 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65  rg = 0;.      re
2ad3e 67 41 67 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  gAgg = 0;.    }.
2ad3f 20 20 20 20 69 66 28 20 70 46 2d 3e 69 44 69 73      if( pF->iDis
2ad40 74 69 6e 63 74 3e 3d 30 20 29 7b 0a 20 20 20 20  tinct>=0 ){.    
2ad41 20 20 61 64 64 72 4e 65 78 74 20 3d 20 73 71 6c    addrNext = sql
2ad42 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
2ad43 6c 28 76 29 3b 0a 20 20 20 20 20 20 61 73 73 65  l(v);.      asse
2ad44 72 74 28 20 6e 41 72 67 3d 3d 31 20 29 3b 0a 20  rt( nArg==1 );. 
2ad45 20 20 20 20 20 63 6f 64 65 44 69 73 74 69 6e 63       codeDistinc
2ad46 74 28 70 50 61 72 73 65 2c 20 70 46 2d 3e 69 44  t(pParse, pF->iD
2ad47 69 73 74 69 6e 63 74 2c 20 61 64 64 72 4e 65 78  istinct, addrNex
2ad48 74 2c 20 31 2c 20 72 65 67 41 67 67 29 3b 0a 20  t, 1, regAgg);. 
2ad49 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 46 2d     }.    if( pF-
2ad4a 3e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26 20  >pFunc->flags & 
2ad4b 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44  SQLITE_FUNC_NEED
2ad4c 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 20 20 43 6f  COLL ){.      Co
2ad4d 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30  llSeq *pColl = 0
2ad4e 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45  ;.      struct E
2ad4f 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
2ad50 74 65 6d 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a  tem;.      int j
2ad51 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2ad52 70 4c 69 73 74 21 3d 30 20 29 3b 20 20 2f 2a 20  pList!=0 );  /* 
2ad53 70 4c 69 73 74 21 3d 30 20 69 66 20 70 46 2d 3e  pList!=0 if pF->
2ad54 70 46 75 6e 63 20 68 61 73 20 4e 45 45 44 43 4f  pFunc has NEEDCO
2ad55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28  LL */.      for(
2ad56 6a 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74  j=0, pItem=pList
2ad57 2d 3e 61 3b 20 21 70 43 6f 6c 6c 20 26 26 20 6a  ->a; !pColl && j
2ad58 3c 6e 41 72 67 3b 20 6a 2b 2b 2c 20 70 49 74 65  <nArg; j++, pIte
2ad59 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 43  m++){.        pC
2ad5a 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70  oll = sqlite3Exp
2ad5b 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  rCollSeq(pParse,
2ad5c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a   pItem->pExpr);.
2ad5d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
2ad5e 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  ( !pColl ){.    
2ad5f 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 50 61 72      pColl = pPar
2ad60 73 65 2d 3e 64 62 2d 3e 70 44 66 6c 74 43 6f 6c  se->db->pDfltCol
2ad61 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  l;.      }.     
2ad62 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2ad63 70 34 28 76 2c 20 4f 50 5f 43 6f 6c 6c 53 65 71  p4(v, OP_CollSeq
2ad64 2c 20 30 2c 20 30 2c 20 30 2c 20 28 63 68 61 72  , 0, 0, 0, (char
2ad65 20 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f 4c   *)pColl, P4_COL
2ad66 4c 53 45 51 29 3b 0a 20 20 20 20 7d 0a 20 20 20  LSEQ);.    }.   
2ad67 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2ad68 70 34 28 76 2c 20 4f 50 5f 41 67 67 53 74 65 70  p4(v, OP_AggStep
2ad69 2c 20 30 2c 20 72 65 67 41 67 67 2c 20 70 46 2d  , 0, regAgg, pF-
2ad6a 3e 69 4d 65 6d 2c 0a 20 20 20 20 20 20 20 20 20  >iMem,.         
2ad6b 20 20 20 20 20 20 20 20 20 20 20 20 20 28 76 6f               (vo
2ad6c 69 64 2a 29 70 46 2d 3e 70 46 75 6e 63 2c 20 50  id*)pF->pFunc, P
2ad6d 34 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20 20 20  4_FUNCDEF);.    
2ad6e 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2ad6f 65 50 35 28 76 2c 20 28 75 38 29 6e 41 72 67 29  eP5(v, (u8)nArg)
2ad70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c  ;.    sqlite3Rel
2ad71 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50  easeTempRange(pP
2ad72 61 72 73 65 2c 20 72 65 67 41 67 67 2c 20 6e 41  arse, regAgg, nA
2ad73 72 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  rg);.    sqlite3
2ad74 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
2ad75 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
2ad76 72 65 67 41 67 67 2c 20 6e 41 72 67 29 3b 0a 20  regAgg, nArg);. 
2ad77 20 20 20 69 66 28 20 61 64 64 72 4e 65 78 74 20     if( addrNext 
2ad78 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2ad79 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
2ad7a 28 76 2c 20 61 64 64 72 4e 65 78 74 29 3b 0a 20  (v, addrNext);. 
2ad7b 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2ad7c 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72 73  CacheClear(pPars
2ad7d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e);.    }.  }.  
2ad7e 66 6f 72 28 69 3d 30 2c 20 70 43 3d 70 41 67 67  for(i=0, pC=pAgg
2ad7f 49 6e 66 6f 2d 3e 61 43 6f 6c 3b 20 69 3c 70 41  Info->aCol; i<pA
2ad80 67 67 49 6e 66 6f 2d 3e 6e 41 63 63 75 6d 75 6c  ggInfo->nAccumul
2ad81 61 74 6f 72 3b 20 69 2b 2b 2c 20 70 43 2b 2b 29  ator; i++, pC++)
2ad82 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  {.    sqlite3Exp
2ad83 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 43  rCode(pParse, pC
2ad84 2d 3e 70 45 78 70 72 2c 20 70 43 2d 3e 69 4d 65  ->pExpr, pC->iMe
2ad85 6d 29 3b 0a 20 20 7d 0a 20 20 70 41 67 67 49 6e  m);.  }.  pAggIn
2ad86 66 6f 2d 3e 64 69 72 65 63 74 4d 6f 64 65 20 3d  fo->directMode =
2ad87 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70   0;.  sqlite3Exp
2ad88 72 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72  rCacheClear(pPar
2ad89 73 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  se);.}../*.** Ge
2ad8a 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
2ad8b 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  the SELECT state
2ad8c 6d 65 6e 74 20 67 69 76 65 6e 20 69 6e 20 74 68  ment given in th
2ad8d 65 20 70 20 61 72 67 75 6d 65 6e 74 2e 20 20 0a  e p argument.  .
2ad8e 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74  **.** The result
2ad8f 73 20 61 72 65 20 64 69 73 74 72 69 62 75 74 65  s are distribute
2ad90 64 20 69 6e 20 76 61 72 69 6f 75 73 20 77 61 79  d in various way
2ad91 73 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74  s depending on t
2ad92 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f  he.** contents o
2ad93 66 20 74 68 65 20 53 65 6c 65 63 74 44 65 73 74  f the SelectDest
2ad94 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e 74   structure point
2ad95 65 64 20 74 6f 20 62 79 20 61 72 67 75 6d 65 6e  ed to by argumen
2ad96 74 20 70 44 65 73 74 0a 2a 2a 20 61 73 20 66 6f  t pDest.** as fo
2ad97 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  llows:.**.**    
2ad98 20 70 44 65 73 74 2d 3e 65 44 65 73 74 20 20 20   pDest->eDest   
2ad99 20 52 65 73 75 6c 74 0a 2a 2a 20 20 20 20 20 2d   Result.**     -
2ad9a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 2d  -----------    -
2ad9b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2ad9c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2ad9d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20  ----------.**   
2ad9e 20 20 53 52 54 5f 4f 75 74 70 75 74 20 20 20 20    SRT_Output    
2ad9f 20 20 47 65 6e 65 72 61 74 65 20 61 20 72 6f 77    Generate a row
2ada0 20 6f 66 20 6f 75 74 70 75 74 20 28 75 73 69 6e   of output (usin
2ada1 67 20 74 68 65 20 4f 50 5f 52 65 73 75 6c 74 52  g the OP_ResultR
2ada2 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ow.**           
2ada3 20 20 20 20 20 20 20 20 20 20 6f 70 63 6f 64 65            opcode
2ada4 29 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 69  ) for each row i
2ada5 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
2ada6 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f  ..**.**     SRT_
2ada7 4d 65 6d 20 20 20 20 20 20 20 20 20 4f 6e 6c 79  Mem         Only
2ada8 20 76 61 6c 69 64 20 69 66 20 74 68 65 20 72 65   valid if the re
2ada9 73 75 6c 74 20 69 73 20 61 20 73 69 6e 67 6c 65  sult is a single
2adaa 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 20 20 20 20   column..**     
2adab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2adac 53 74 6f 72 65 20 74 68 65 20 66 69 72 73 74 20  Store the first 
2adad 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 66 69  column of the fi
2adae 72 73 74 20 72 65 73 75 6c 74 20 72 6f 77 0a 2a  rst result row.*
2adaf 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2adb0 20 20 20 20 20 20 69 6e 20 72 65 67 69 73 74 65        in registe
2adb1 72 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 20 74  r pDest->iParm t
2adb2 68 65 6e 20 61 62 61 6e 64 6f 6e 20 74 68 65 20  hen abandon the 
2adb3 72 65 73 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  rest.**         
2adb4 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 74              of t
2adb5 68 65 20 71 75 65 72 79 2e 20 20 54 68 69 73 20  he query.  This 
2adb6 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 6d 70 6c  destination impl
2adb7 69 65 73 20 22 4c 49 4d 49 54 20 31 22 2e 0a 2a  ies "LIMIT 1"..*
2adb8 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 53 65 74  *.**     SRT_Set
2adb9 20 20 20 20 20 20 20 20 20 54 68 65 20 72 65 73           The res
2adba 75 6c 74 20 6d 75 73 74 20 62 65 20 61 20 73 69  ult must be a si
2adbb 6e 67 6c 65 20 63 6f 6c 75 6d 6e 2e 20 20 53 74  ngle column.  St
2adbc 6f 72 65 20 65 61 63 68 0a 2a 2a 20 20 20 20 20  ore each.**     
2adbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2adbe 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 20 61 73  row of result as
2adbf 20 74 68 65 20 6b 65 79 20 69 6e 20 74 61 62 6c   the key in tabl
2adc0 65 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2e 20  e pDest->iParm. 
2adc1 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2adc2 20 20 20 20 20 20 20 20 41 70 70 6c 79 20 74 68          Apply th
2adc3 65 20 61 66 66 69 6e 69 74 79 20 70 44 65 73 74  e affinity pDest
2adc4 2d 3e 61 66 66 69 6e 69 74 79 20 62 65 66 6f 72  ->affinity befor
2adc5 65 20 73 74 6f 72 69 6e 67 0a 2a 2a 20 20 20 20  e storing.**    
2adc6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2adc7 20 72 65 73 75 6c 74 73 2e 20 20 55 73 65 64 20   results.  Used 
2adc8 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 22 49 4e  to implement "IN
2adc9 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 2e 0a   (SELECT ...)"..
2adca 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 55 6e  **.**     SRT_Un
2adcb 69 6f 6e 20 20 20 20 20 20 20 53 74 6f 72 65 20  ion       Store 
2adcc 72 65 73 75 6c 74 73 20 61 73 20 61 20 6b 65 79  results as a key
2adcd 20 69 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20   in a temporary 
2adce 74 61 62 6c 65 20 70 44 65 73 74 2d 3e 69 50 61  table pDest->iPa
2adcf 72 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52  rm..**.**     SR
2add0 54 5f 45 78 63 65 70 74 20 20 20 20 20 20 52 65  T_Except      Re
2add1 6d 6f 76 65 20 72 65 73 75 6c 74 73 20 66 72 6f  move results fro
2add2 6d 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20  m the temporary 
2add3 74 61 62 6c 65 20 70 44 65 73 74 2d 3e 69 50 61  table pDest->iPa
2add4 72 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52  rm..**.**     SR
2add5 54 5f 54 61 62 6c 65 20 20 20 20 20 20 20 53 74  T_Table       St
2add6 6f 72 65 20 72 65 73 75 6c 74 73 20 69 6e 20 74  ore results in t
2add7 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 70  emporary table p
2add8 44 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a 20  Dest->iParm..** 
2add9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2adda 20 20 20 20 54 68 69 73 20 69 73 20 6c 69 6b 65      This is like
2addb 20 53 52 54 5f 45 70 68 65 6d 54 61 62 20 65 78   SRT_EphemTab ex
2addc 63 65 70 74 20 74 68 61 74 20 74 68 65 20 74 61  cept that the ta
2addd 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ble.**          
2adde 20 20 20 20 20 20 20 20 20 20 20 69 73 20 61 73             is as
2addf 73 75 6d 65 64 20 74 6f 20 61 6c 72 65 61 64 79  sumed to already
2ade0 20 62 65 20 6f 70 65 6e 2e 0a 2a 2a 0a 2a 2a 20   be open..**.** 
2ade1 20 20 20 20 53 52 54 5f 45 70 68 65 6d 54 61 62      SRT_EphemTab
2ade2 20 20 20 20 43 72 65 61 74 65 20 61 6e 20 74 65      Create an te
2ade3 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 70 44  mporary table pD
2ade4 65 73 74 2d 3e 69 50 61 72 6d 20 61 6e 64 20 73  est->iParm and s
2ade5 74 6f 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  tore.**         
2ade6 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20              the 
2ade7 72 65 73 75 6c 74 20 74 68 65 72 65 2e 20 54 68  result there. Th
2ade8 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74  e cursor is left
2ade9 20 6f 70 65 6e 20 61 66 74 65 72 0a 2a 2a 20 20   open after.**  
2adea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2adeb 20 20 20 72 65 74 75 72 6e 69 6e 67 2e 20 20 54     returning.  T
2adec 68 69 73 20 69 73 20 6c 69 6b 65 20 53 52 54 5f  his is like SRT_
2aded 54 61 62 6c 65 20 65 78 63 65 70 74 20 74 68 61  Table except tha
2adee 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
2adef 20 20 20 20 20 20 20 20 20 74 68 69 73 20 64 65           this de
2adf0 73 74 69 6e 61 74 69 6f 6e 20 75 73 65 73 20 4f  stination uses O
2adf1 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20  P_OpenEphemeral 
2adf2 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 20 20 20  to create.**    
2adf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2adf4 20 74 68 65 20 74 61 62 6c 65 20 66 69 72 73 74   the table first
2adf5 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f  ..**.**     SRT_
2adf6 43 6f 72 6f 75 74 69 6e 65 20 20 20 47 65 6e 65  Coroutine   Gene
2adf7 72 61 74 65 20 61 20 63 6f 2d 72 6f 75 74 69 6e  rate a co-routin
2adf8 65 20 74 68 61 74 20 72 65 74 75 72 6e 73 20 61  e that returns a
2adf9 20 6e 65 77 20 72 6f 77 20 6f 66 0a 2a 2a 20 20   new row of.**  
2adfa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2adfb 20 20 20 72 65 73 75 6c 74 73 20 65 61 63 68 20     results each 
2adfc 74 69 6d 65 20 69 74 20 69 73 20 69 6e 76 6f 6b  time it is invok
2adfd 65 64 2e 20 20 54 68 65 20 65 6e 74 72 79 20 70  ed.  The entry p
2adfe 6f 69 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  oint.**         
2adff 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 74              of t
2ae00 68 65 20 63 6f 2d 72 6f 75 74 69 6e 65 20 69 73  he co-routine is
2ae01 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
2ae02 74 65 72 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  ter pDest->iParm
2ae03 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f  ..**.**     SRT_
2ae04 45 78 69 73 74 73 20 20 20 20 20 20 53 74 6f 72  Exists      Stor
2ae05 65 20 61 20 31 20 69 6e 20 6d 65 6d 6f 72 79 20  e a 1 in memory 
2ae06 63 65 6c 6c 20 70 44 65 73 74 2d 3e 69 50 61 72  cell pDest->iPar
2ae07 6d 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 0a  m if the result.
2ae08 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2ae09 20 20 20 20 20 20 20 73 65 74 20 69 73 20 6e 6f         set is no
2ae0a 74 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 20  t empty..**.**  
2ae0b 20 20 20 53 52 54 5f 44 69 73 63 61 72 64 20 20     SRT_Discard  
2ae0c 20 20 20 54 68 72 6f 77 20 74 68 65 20 72 65 73     Throw the res
2ae0d 75 6c 74 73 20 61 77 61 79 2e 20 20 54 68 69 73  ults away.  This
2ae0e 20 69 73 20 75 73 65 64 20 62 79 20 53 45 4c 45   is used by SELE
2ae0f 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  CT.**           
2ae10 20 20 20 20 20 20 20 20 20 20 73 74 61 74 65 6d            statem
2ae11 65 6e 74 73 20 77 69 74 68 69 6e 20 74 72 69 67  ents within trig
2ae12 67 65 72 73 20 77 68 6f 73 65 20 6f 6e 6c 79 20  gers whose only 
2ae13 70 75 72 70 6f 73 65 20 69 73 0a 2a 2a 20 20 20  purpose is.**   
2ae14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ae15 20 20 74 68 65 20 73 69 64 65 2d 65 66 66 65 63    the side-effec
2ae16 74 73 20 6f 66 20 66 75 6e 63 74 69 6f 6e 73 2e  ts of functions.
2ae17 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
2ae18 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ine returns the 
2ae19 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  number of errors
2ae1a 2e 20 20 49 66 20 61 6e 79 20 65 72 72 6f 72 73  .  If any errors
2ae1b 20 61 72 65 0a 2a 2a 20 65 6e 63 6f 75 6e 74 65   are.** encounte
2ae1c 72 65 64 2c 20 74 68 65 6e 20 61 6e 20 61 70 70  red, then an app
2ae1d 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 6d  ropriate error m
2ae1e 65 73 73 61 67 65 20 69 73 20 6c 65 66 74 20 69  essage is left i
2ae1f 6e 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 7a 45 72  n.** pParse->zEr
2ae20 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rMsg..**.** This
2ae21 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 4e 4f   routine does NO
2ae22 54 20 66 72 65 65 20 74 68 65 20 53 65 6c 65 63  T free the Selec
2ae23 74 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73  t structure pass
2ae24 65 64 20 69 6e 2e 20 20 54 68 65 0a 2a 2a 20 63  ed in.  The.** c
2ae25 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  alling function 
2ae26 6e 65 65 64 73 20 74 6f 20 64 6f 20 74 68 61 74  needs to do that
2ae27 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2ae28 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53  ATE int sqlite3S
2ae29 65 6c 65 63 74 28 0a 20 20 50 61 72 73 65 20 2a  elect(.  Parse *
2ae2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
2ae2b 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f  /* The parser co
2ae2c 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63  ntext */.  Selec
2ae2d 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  t *p,           
2ae2e 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20    /* The SELECT 
2ae2f 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20  statement being 
2ae30 63 6f 64 65 64 2e 20 2a 2f 0a 20 20 53 65 6c 65  coded. */.  Sele
2ae31 63 74 44 65 73 74 20 2a 70 44 65 73 74 20 20 20  ctDest *pDest   
2ae32 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f     /* What to do
2ae33 20 77 69 74 68 20 74 68 65 20 71 75 65 72 79 20   with the query 
2ae34 72 65 73 75 6c 74 73 20 2a 2f 0a 29 7b 0a 20 20  results */.){.  
2ae35 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20  int i, j;       
2ae36 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
2ae37 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 57 68 65  ounters */.  Whe
2ae38 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20  reInfo *pWInfo; 
2ae39 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 66 72      /* Return fr
2ae3a 6f 6d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42  om sqlite3WhereB
2ae3b 65 67 69 6e 28 29 20 2a 2f 0a 20 20 56 64 62 65  egin() */.  Vdbe
2ae3c 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20   *v;            
2ae3d 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
2ae3e 6c 20 6d 61 63 68 69 6e 65 20 75 6e 64 65 72 20  l machine under 
2ae3f 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a  construction */.
2ae40 20 20 69 6e 74 20 69 73 41 67 67 3b 20 20 20 20    int isAgg;    
2ae41 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
2ae42 20 66 6f 72 20 73 65 6c 65 63 74 20 6c 69 73 74   for select list
2ae43 73 20 6c 69 6b 65 20 22 63 6f 75 6e 74 28 2a 29  s like "count(*)
2ae44 22 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  " */.  ExprList 
2ae45 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20 2f 2a  *pEList;      /*
2ae46 20 4c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73   List of columns
2ae47 20 74 6f 20 65 78 74 72 61 63 74 2e 20 2a 2f 0a   to extract. */.
2ae48 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c    SrcList *pTabL
2ae49 69 73 74 3b 20 20 20 20 20 2f 2a 20 4c 69 73 74  ist;     /* List
2ae4a 20 6f 66 20 74 61 62 6c 65 73 20 74 6f 20 73 65   of tables to se
2ae4b 6c 65 63 74 20 66 72 6f 6d 20 2a 2f 0a 20 20 45  lect from */.  E
2ae4c 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20 20 20  xpr *pWhere;    
2ae4d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45        /* The WHE
2ae4e 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20  RE clause.  May 
2ae4f 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 70  be NULL */.  Exp
2ae50 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 3b  rList *pOrderBy;
2ae51 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52      /* The ORDER
2ae52 20 42 59 20 63 6c 61 75 73 65 2e 20 20 4d 61 79   BY clause.  May
2ae53 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78   be NULL */.  Ex
2ae54 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79  prList *pGroupBy
2ae55 3b 20 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55  ;    /* The GROU
2ae56 50 20 42 59 20 63 6c 61 75 73 65 2e 20 20 4d 61  P BY clause.  Ma
2ae57 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45  y be NULL */.  E
2ae58 78 70 72 20 2a 70 48 61 76 69 6e 67 3b 20 20 20  xpr *pHaving;   
2ae59 20 20 20 20 20 20 2f 2a 20 54 68 65 20 48 41 56        /* The HAV
2ae5a 49 4e 47 20 63 6c 61 75 73 65 2e 20 20 4d 61 79  ING clause.  May
2ae5b 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e   be NULL */.  in
2ae5c 74 20 69 73 44 69 73 74 69 6e 63 74 3b 20 20 20  t isDistinct;   
2ae5d 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
2ae5e 74 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65 79  the DISTINCT key
2ae5f 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 20  word is present 
2ae60 2a 2f 0a 20 20 69 6e 74 20 64 69 73 74 69 6e 63  */.  int distinc
2ae61 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  t;          /* T
2ae62 61 62 6c 65 20 74 6f 20 75 73 65 20 66 6f 72 20  able to use for 
2ae63 74 68 65 20 64 69 73 74 69 6e 63 74 20 73 65 74  the distinct set
2ae64 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 31   */.  int rc = 1
2ae65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
2ae66 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20  Value to return 
2ae67 66 72 6f 6d 20 74 68 69 73 20 66 75 6e 63 74 69  from this functi
2ae68 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  on */.  int addr
2ae69 53 6f 72 74 49 6e 64 65 78 3b 20 20 20 20 20 2f  SortIndex;     /
2ae6a 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 6e 20  * Address of an 
2ae6b 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
2ae6c 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a   instruction */.
2ae6d 20 20 41 67 67 49 6e 66 6f 20 73 41 67 67 49 6e    AggInfo sAggIn
2ae6e 66 6f 3b 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f  fo;      /* Info
2ae6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 62 79 20  rmation used by 
2ae70 61 67 67 72 65 67 61 74 65 20 71 75 65 72 69 65  aggregate querie
2ae71 73 20 2a 2f 0a 20 20 69 6e 74 20 69 45 6e 64 3b  s */.  int iEnd;
2ae72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2ae73 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   Address of the 
2ae74 65 6e 64 20 6f 66 20 74 68 65 20 71 75 65 72 79  end of the query
2ae75 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
2ae76 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  b;           /* 
2ae77 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
2ae78 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 64 62  nection */..  db
2ae79 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
2ae7a 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 64 62 2d   if( p==0 || db-
2ae7b 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c  >mallocFailed ||
2ae7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b   pParse->nErr ){
2ae7d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
2ae7e 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
2ae7f 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
2ae80 2c 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54 2c  , SQLITE_SELECT,
2ae81 20 30 2c 20 30 2c 20 30 29 20 29 20 72 65 74 75   0, 0, 0) ) retu
2ae82 72 6e 20 31 3b 0a 20 20 6d 65 6d 73 65 74 28 26  rn 1;.  memset(&
2ae83 73 41 67 67 49 6e 66 6f 2c 20 30 2c 20 73 69 7a  sAggInfo, 0, siz
2ae84 65 6f 66 28 73 41 67 67 49 6e 66 6f 29 29 3b 0a  eof(sAggInfo));.
2ae85 0a 20 20 69 66 28 20 49 67 6e 6f 72 61 62 6c 65  .  if( Ignorable
2ae86 4f 72 64 65 72 62 79 28 70 44 65 73 74 29 20 29  Orderby(pDest) )
2ae87 7b 0a 20 20 20 20 61 73 73 65 72 74 28 70 44 65  {.    assert(pDe
2ae88 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 45  st->eDest==SRT_E
2ae89 78 69 73 74 73 20 7c 7c 20 70 44 65 73 74 2d 3e  xists || pDest->
2ae8a 65 44 65 73 74 3d 3d 53 52 54 5f 55 6e 69 6f 6e  eDest==SRT_Union
2ae8b 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20   || .           
2ae8c 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52  pDest->eDest==SR
2ae8d 54 5f 45 78 63 65 70 74 20 7c 7c 20 70 44 65 73  T_Except || pDes
2ae8e 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 44 69  t->eDest==SRT_Di
2ae8f 73 63 61 72 64 29 3b 0a 20 20 20 20 2f 2a 20 49  scard);.    /* I
2ae90 66 20 4f 52 44 45 52 20 42 59 20 6d 61 6b 65 73  f ORDER BY makes
2ae91 20 6e 6f 20 64 69 66 66 65 72 65 6e 63 65 20 69   no difference i
2ae92 6e 20 74 68 65 20 6f 75 74 70 75 74 20 74 68 65  n the output the
2ae93 6e 20 6e 65 69 74 68 65 72 20 64 6f 65 73 0a 20  n neither does. 
2ae94 20 20 20 2a 2a 20 44 49 53 54 49 4e 43 54 20 73     ** DISTINCT s
2ae95 6f 20 69 74 20 63 61 6e 20 62 65 20 72 65 6d 6f  o it can be remo
2ae96 76 65 64 20 74 6f 6f 2e 20 2a 2f 0a 20 20 20 20  ved too. */.    
2ae97 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2ae98 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4f 72  elete(db, p->pOr
2ae99 64 65 72 42 79 29 3b 0a 20 20 20 20 70 2d 3e 70  derBy);.    p->p
2ae9a 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 20  OrderBy = 0;.   
2ae9b 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 3d 20   p->selFlags &= 
2ae9c 7e 53 46 5f 44 69 73 74 69 6e 63 74 3b 0a 20 20  ~SF_Distinct;.  
2ae9d 7d 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  }.  sqlite3Selec
2ae9e 74 50 72 65 70 28 70 50 61 72 73 65 2c 20 70 2c  tPrep(pParse, p,
2ae9f 20 30 29 3b 0a 20 20 70 4f 72 64 65 72 42 79 20   0);.  pOrderBy 
2aea0 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20  = p->pOrderBy;. 
2aea1 20 70 54 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70   pTabList = p->p
2aea2 53 72 63 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20  Src;.  pEList = 
2aea3 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 69 66 28  p->pEList;.  if(
2aea4 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c   pParse->nErr ||
2aea5 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2aea6 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 65  d ){.    goto se
2aea7 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20  lect_end;.  }.  
2aea8 69 73 41 67 67 20 3d 20 28 70 2d 3e 73 65 6c 46  isAgg = (p->selF
2aea9 6c 61 67 73 20 26 20 53 46 5f 41 67 67 72 65 67  lags & SF_Aggreg
2aeaa 61 74 65 29 21 3d 30 3b 0a 20 20 61 73 73 65 72  ate)!=0;.  asser
2aeab 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a  t( pEList!=0 );.
2aeac 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67 65 6e 65  .  /* Begin gene
2aead 72 61 74 69 6e 67 20 63 6f 64 65 2e 0a 20 20 2a  rating code..  *
2aeae 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  /.  v = sqlite3G
2aeaf 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
2aeb0 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67 6f 74    if( v==0 ) got
2aeb1 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20  o select_end;.. 
2aeb2 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
2aeb3 65 20 66 6f 72 20 61 6c 6c 20 73 75 62 2d 71 75  e for all sub-qu
2aeb4 65 72 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f  eries in the FRO
2aeb5 4d 20 63 6c 61 75 73 65 0a 20 20 2a 2f 0a 23 69  M clause.  */.#i
2aeb6 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
2aeb7 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29  E_OMIT_SUBQUERY)
2aeb8 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
2aeb9 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 0a 20  ITE_OMIT_VIEW). 
2aeba 20 66 6f 72 28 69 3d 30 3b 20 21 70 2d 3e 70 50   for(i=0; !p->pP
2aebb 72 69 6f 72 20 26 26 20 69 3c 70 54 61 62 4c 69  rior && i<pTabLi
2aebc 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a  st->nSrc; i++){.
2aebd 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69      struct SrcLi
2aebe 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d  st_item *pItem =
2aebf 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d   &pTabList->a[i]
2aec0 3b 0a 20 20 20 20 53 65 6c 65 63 74 44 65 73 74  ;.    SelectDest
2aec1 20 64 65 73 74 3b 0a 20 20 20 20 53 65 6c 65 63   dest;.    Selec
2aec2 74 20 2a 70 53 75 62 20 3d 20 70 49 74 65 6d 2d  t *pSub = pItem-
2aec3 3e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 69 6e  >pSelect;.    in
2aec4 74 20 69 73 41 67 67 53 75 62 3b 0a 0a 20 20 20  t isAggSub;..   
2aec5 20 69 66 28 20 70 53 75 62 3d 3d 30 20 7c 7c 20   if( pSub==0 || 
2aec6 70 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74  pItem->isPopulat
2aec7 65 64 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a  ed ) continue;..
2aec8 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74      /* Increment
2aec9 20 50 61 72 73 65 2e 6e 48 65 69 67 68 74 20 62   Parse.nHeight b
2aeca 79 20 74 68 65 20 68 65 69 67 68 74 20 6f 66 20  y the height of 
2aecb 74 68 65 20 6c 61 72 67 65 73 74 20 65 78 70 72  the largest expr
2aecc 65 73 73 69 6f 6e 0a 20 20 20 20 2a 2a 20 74 72  ession.    ** tr
2aecd 65 65 20 72 65 66 65 72 65 64 20 74 6f 20 62 79  ee refered to by
2aece 20 74 68 69 73 2c 20 74 68 65 20 70 61 72 65 6e   this, the paren
2aecf 74 20 73 65 6c 65 63 74 2e 20 54 68 65 20 63 68  t select. The ch
2aed0 69 6c 64 20 73 65 6c 65 63 74 0a 20 20 20 20 2a  ild select.    *
2aed1 2a 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 65 78  * may contain ex
2aed2 70 72 65 73 73 69 6f 6e 20 74 72 65 65 73 20 6f  pression trees o
2aed3 66 20 61 74 20 6d 6f 73 74 0a 20 20 20 20 2a 2a  f at most.    **
2aed4 20 28 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50   (SQLITE_MAX_EXP
2aed5 52 5f 44 45 50 54 48 2d 50 61 72 73 65 2e 6e 48  R_DEPTH-Parse.nH
2aed6 65 69 67 68 74 29 20 68 65 69 67 68 74 2e 20 54  eight) height. T
2aed7 68 69 73 20 69 73 20 61 20 62 69 74 0a 20 20 20  his is a bit.   
2aed8 20 2a 2a 20 6d 6f 72 65 20 63 6f 6e 73 65 72 76   ** more conserv
2aed9 61 74 69 76 65 20 74 68 61 6e 20 6e 65 63 65 73  ative than neces
2aeda 73 61 72 79 2c 20 62 75 74 20 6d 75 63 68 20 65  sary, but much e
2aedb 61 73 69 65 72 20 74 68 61 6e 20 65 6e 66 6f 72  asier than enfor
2aedc 63 69 6e 67 0a 20 20 20 20 2a 2a 20 61 6e 20 65  cing.    ** an e
2aedd 78 61 63 74 20 6c 69 6d 69 74 2e 0a 20 20 20 20  xact limit..    
2aede 2a 2f 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  */.    pParse->n
2aedf 48 65 69 67 68 74 20 2b 3d 20 73 71 6c 69 74 65  Height += sqlite
2aee0 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 67 68  3SelectExprHeigh
2aee1 74 28 70 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 68  t(p);..    /* Ch
2aee2 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68  eck to see if th
2aee3 65 20 73 75 62 71 75 65 72 79 20 63 61 6e 20 62  e subquery can b
2aee4 65 20 61 62 73 6f 72 62 65 64 20 69 6e 74 6f 20  e absorbed into 
2aee5 74 68 65 20 70 61 72 65 6e 74 2e 20 2a 2f 0a 20  the parent. */. 
2aee6 20 20 20 69 73 41 67 67 53 75 62 20 3d 20 28 70     isAggSub = (p
2aee7 53 75 62 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20  Sub->selFlags & 
2aee8 53 46 5f 41 67 67 72 65 67 61 74 65 29 21 3d 30  SF_Aggregate)!=0
2aee9 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 74 74 65  ;.    if( flatte
2aeea 6e 53 75 62 71 75 65 72 79 28 70 50 61 72 73 65  nSubquery(pParse
2aeeb 2c 20 70 2c 20 69 2c 20 69 73 41 67 67 2c 20 69  , p, i, isAgg, i
2aeec 73 41 67 67 53 75 62 29 20 29 7b 0a 20 20 20 20  sAggSub) ){.    
2aeed 20 20 69 66 28 20 69 73 41 67 67 53 75 62 20 29    if( isAggSub )
2aeee 7b 0a 20 20 20 20 20 20 20 20 69 73 41 67 67 20  {.        isAgg 
2aeef 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  = 1;.        p->
2aef0 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 41  selFlags |= SF_A
2aef1 67 67 72 65 67 61 74 65 3b 0a 20 20 20 20 20 20  ggregate;.      
2aef2 7d 0a 20 20 20 20 20 20 69 20 3d 20 2d 31 3b 0a  }.      i = -1;.
2aef3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2aef4 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
2aef5 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52  stInit(&dest, SR
2aef6 54 5f 45 70 68 65 6d 54 61 62 2c 20 70 49 74 65  T_EphemTab, pIte
2aef7 6d 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20  m->iCursor);.   
2aef8 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d     assert( pItem
2aef9 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 3d 3d 30  ->isPopulated==0
2aefa 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
2aefb 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  3Select(pParse, 
2aefc 70 53 75 62 2c 20 26 64 65 73 74 29 3b 0a 20 20  pSub, &dest);.  
2aefd 20 20 20 20 70 49 74 65 6d 2d 3e 69 73 50 6f 70      pItem->isPop
2aefe 75 6c 61 74 65 64 20 3d 20 31 3b 0a 20 20 20 20  ulated = 1;.    
2aeff 7d 0a 20 20 20 20 69 66 28 20 2f 2a 70 50 61 72  }.    if( /*pPar
2af00 73 65 2d 3e 6e 45 72 72 20 7c 7c 2a 2f 20 64 62  se->nErr ||*/ db
2af01 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2af02 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 65 6c  {.      goto sel
2af03 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a 20  ect_end;.    }. 
2af04 20 20 20 70 50 61 72 73 65 2d 3e 6e 48 65 69 67     pParse->nHeig
2af05 68 74 20 2d 3d 20 73 71 6c 69 74 65 33 53 65 6c  ht -= sqlite3Sel
2af06 65 63 74 45 78 70 72 48 65 69 67 68 74 28 70 29  ectExprHeight(p)
2af07 3b 0a 20 20 20 20 70 54 61 62 4c 69 73 74 20 3d  ;.    pTabList =
2af08 20 70 2d 3e 70 53 72 63 3b 0a 20 20 20 20 69 66   p->pSrc;.    if
2af09 28 20 21 49 67 6e 6f 72 61 62 6c 65 4f 72 64 65  ( !IgnorableOrde
2af0a 72 62 79 28 70 44 65 73 74 29 20 29 7b 0a 20 20  rby(pDest) ){.  
2af0b 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d 20 70      pOrderBy = p
2af0c 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 20 20  ->pOrderBy;.    
2af0d 7d 0a 20 20 7d 0a 20 20 70 45 4c 69 73 74 20 3d  }.  }.  pEList =
2af0e 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 23 65 6e 64   p->pEList;.#end
2af0f 69 66 0a 20 20 70 57 68 65 72 65 20 3d 20 70 2d  if.  pWhere = p-
2af10 3e 70 57 68 65 72 65 3b 0a 20 20 70 47 72 6f 75  >pWhere;.  pGrou
2af11 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70 42  pBy = p->pGroupB
2af12 79 3b 0a 20 20 70 48 61 76 69 6e 67 20 3d 20 70  y;.  pHaving = p
2af13 2d 3e 70 48 61 76 69 6e 67 3b 0a 20 20 69 73 44  ->pHaving;.  isD
2af14 69 73 74 69 6e 63 74 20 3d 20 28 70 2d 3e 73 65  istinct = (p->se
2af15 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74  lFlags & SF_Dist
2af16 69 6e 63 74 29 21 3d 30 3b 0a 0a 23 69 66 6e 64  inct)!=0;..#ifnd
2af17 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
2af18 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 20  OMPOUND_SELECT. 
2af19 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
2af1a 61 72 65 20 61 20 73 65 71 75 65 6e 63 65 20 6f  are a sequence o
2af1b 66 20 71 75 65 72 69 65 73 2c 20 64 6f 20 74 68  f queries, do th
2af1c 65 20 65 61 72 6c 69 65 72 20 6f 6e 65 73 20 66  e earlier ones f
2af1d 69 72 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  irst..  */.  if(
2af1e 20 70 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20   p->pPrior ){.  
2af1f 20 20 69 66 28 20 70 2d 3e 70 52 69 67 68 74 6d    if( p->pRightm
2af20 6f 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ost==0 ){.      
2af21 53 65 6c 65 63 74 20 2a 70 4c 6f 6f 70 2c 20 2a  Select *pLoop, *
2af22 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 20 20  pRight = 0;.    
2af23 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20    int cnt = 0;. 
2af24 20 20 20 20 20 69 6e 74 20 6d 78 53 65 6c 65 63       int mxSelec
2af25 74 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 4c 6f  t;.      for(pLo
2af26 6f 70 3d 70 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f  op=p; pLoop; pLo
2af27 6f 70 3d 70 4c 6f 6f 70 2d 3e 70 50 72 69 6f 72  op=pLoop->pPrior
2af28 2c 20 63 6e 74 2b 2b 29 7b 0a 20 20 20 20 20 20  , cnt++){.      
2af29 20 20 70 4c 6f 6f 70 2d 3e 70 52 69 67 68 74 6d    pLoop->pRightm
2af2a 6f 73 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20  ost = p;.       
2af2b 20 70 4c 6f 6f 70 2d 3e 70 4e 65 78 74 20 3d 20   pLoop->pNext = 
2af2c 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20  pRight;.        
2af2d 70 52 69 67 68 74 20 3d 20 70 4c 6f 6f 70 3b 0a  pRight = pLoop;.
2af2e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 78        }.      mx
2af2f 53 65 6c 65 63 74 20 3d 20 64 62 2d 3e 61 4c 69  Select = db->aLi
2af30 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
2af31 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
2af32 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 78 53  ];.      if( mxS
2af33 65 6c 65 63 74 20 26 26 20 63 6e 74 3e 6d 78 53  elect && cnt>mxS
2af34 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20  elect ){.       
2af35 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2af36 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61  (pParse, "too ma
2af37 6e 79 20 74 65 72 6d 73 20 69 6e 20 63 6f 6d 70  ny terms in comp
2af38 6f 75 6e 64 20 53 45 4c 45 43 54 22 29 3b 0a 20  ound SELECT");. 
2af39 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b         return 1;
2af3a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2af3b 20 20 20 72 65 74 75 72 6e 20 6d 75 6c 74 69 53     return multiS
2af3c 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 2c  elect(pParse, p,
2af3d 20 70 44 65 73 74 29 3b 0a 20 20 7d 0a 23 65 6e   pDest);.  }.#en
2af3e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 72 69  dif..  /* If wri
2af3f 74 69 6e 67 20 74 6f 20 6d 65 6d 6f 72 79 20 6f  ting to memory o
2af40 72 20 67 65 6e 65 72 61 74 69 6e 67 20 61 20 73  r generating a s
2af41 65 74 0a 20 20 2a 2a 20 6f 6e 6c 79 20 61 20 73  et.  ** only a s
2af42 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6d 61 79  ingle column may
2af43 20 62 65 20 6f 75 74 70 75 74 2e 0a 20 20 2a 2f   be output..  */
2af44 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2af45 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20  OMIT_SUBQUERY.  
2af46 69 66 28 20 63 68 65 63 6b 46 6f 72 4d 75 6c 74  if( checkForMult
2af47 69 43 6f 6c 75 6d 6e 53 65 6c 65 63 74 45 72 72  iColumnSelectErr
2af48 6f 72 28 70 50 61 72 73 65 2c 20 70 44 65 73 74  or(pParse, pDest
2af49 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29  , pEList->nExpr)
2af4a 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6c   ){.    goto sel
2af4b 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 23 65 6e  ect_end;.  }.#en
2af4c 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 70 6f 73  dif..  /* If pos
2af4d 73 69 62 6c 65 2c 20 72 65 77 72 69 74 65 20 74  sible, rewrite t
2af4e 68 65 20 71 75 65 72 79 20 74 6f 20 75 73 65 20  he query to use 
2af4f 47 52 4f 55 50 20 42 59 20 69 6e 73 74 65 61 64  GROUP BY instead
2af50 20 6f 66 20 44 49 53 54 49 4e 43 54 2e 0a 20 20   of DISTINCT..  
2af51 2a 2a 20 47 52 4f 55 50 20 42 59 20 6d 69 67 68  ** GROUP BY migh
2af52 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78 2c 20  t use an index, 
2af53 44 49 53 54 49 4e 43 54 20 6e 65 76 65 72 20 64  DISTINCT never d
2af54 6f 65 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  oes..  */.  asse
2af55 72 74 28 20 70 2d 3e 70 47 72 6f 75 70 42 79 3d  rt( p->pGroupBy=
2af56 3d 30 20 7c 7c 20 28 70 2d 3e 73 65 6c 46 6c 61  =0 || (p->selFla
2af57 67 73 20 26 20 53 46 5f 41 67 67 72 65 67 61 74  gs & SF_Aggregat
2af58 65 29 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28  e)!=0 );.  if( (
2af59 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53  p->selFlags & (S
2af5a 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67  F_Distinct|SF_Ag
2af5b 67 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 44 69  gregate))==SF_Di
2af5c 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 70 2d  stinct ){.    p-
2af5d 3e 70 47 72 6f 75 70 42 79 20 3d 20 73 71 6c 69  >pGroupBy = sqli
2af5e 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64  te3ExprListDup(d
2af5f 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 30 29  b, p->pEList, 0)
2af60 3b 0a 20 20 20 20 70 47 72 6f 75 70 42 79 20 3d  ;.    pGroupBy =
2af61 20 70 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20   p->pGroupBy;.  
2af62 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 3d    p->selFlags &=
2af63 20 7e 53 46 5f 44 69 73 74 69 6e 63 74 3b 0a 20   ~SF_Distinct;. 
2af64 20 20 20 69 73 44 69 73 74 69 6e 63 74 20 3d 20     isDistinct = 
2af65 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  0;.  }..  /* If 
2af66 74 68 65 72 65 20 69 73 20 61 6e 20 4f 52 44 45  there is an ORDE
2af67 52 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65  R BY clause, the
2af68 6e 20 74 68 69 73 20 73 6f 72 74 69 6e 67 0a 20  n this sorting. 
2af69 20 2a 2a 20 69 6e 64 65 78 20 6d 69 67 68 74 20   ** index might 
2af6a 65 6e 64 20 75 70 20 62 65 69 6e 67 20 75 6e 75  end up being unu
2af6b 73 65 64 20 69 66 20 74 68 65 20 64 61 74 61 20  sed if the data 
2af6c 63 61 6e 20 62 65 20 0a 20 20 2a 2a 20 65 78 74  can be .  ** ext
2af6d 72 61 63 74 65 64 20 69 6e 20 70 72 65 2d 73 6f  racted in pre-so
2af6e 72 74 65 64 20 6f 72 64 65 72 2e 20 20 49 66 20  rted order.  If 
2af6f 74 68 61 74 20 69 73 20 74 68 65 20 63 61 73 65  that is the case
2af70 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 2a 2a 20  , then the.  ** 
2af71 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
2af72 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 69 6c   instruction wil
2af73 6c 20 62 65 20 63 68 61 6e 67 65 64 20 74 6f 20  l be changed to 
2af74 61 6e 20 4f 50 5f 4e 6f 6f 70 20 6f 6e 63 65 0a  an OP_Noop once.
2af75 20 20 2a 2a 20 77 65 20 66 69 67 75 72 65 20 6f    ** we figure o
2af76 75 74 20 74 68 61 74 20 74 68 65 20 73 6f 72 74  ut that the sort
2af77 69 6e 67 20 69 6e 64 65 78 20 69 73 20 6e 6f 74  ing index is not
2af78 20 6e 65 65 64 65 64 2e 20 20 54 68 65 20 61 64   needed.  The ad
2af79 64 72 53 6f 72 74 49 6e 64 65 78 0a 20 20 2a 2a  drSortIndex.  **
2af7a 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 65   variable is use
2af7b 64 20 74 6f 20 66 61 63 69 6c 69 74 61 74 65 20  d to facilitate 
2af7c 74 68 61 74 20 63 68 61 6e 67 65 2e 0a 20 20 2a  that change..  *
2af7d 2f 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79  /.  if( pOrderBy
2af7e 20 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20   ){.    KeyInfo 
2af7f 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 70  *pKeyInfo;.    p
2af80 4b 65 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66  KeyInfo = keyInf
2af81 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50  oFromExprList(pP
2af82 61 72 73 65 2c 20 70 4f 72 64 65 72 42 79 29 3b  arse, pOrderBy);
2af83 0a 20 20 20 20 70 4f 72 64 65 72 42 79 2d 3e 69  .    pOrderBy->i
2af84 45 43 75 72 73 6f 72 20 3d 20 70 50 61 72 73 65  ECursor = pParse
2af85 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 70 2d  ->nTab++;.    p-
2af86 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d  >addrOpenEphm[2]
2af87 20 3d 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78   = addrSortIndex
2af88 20 3d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   =.      sqlite3
2af89 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2af8a 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 0a  _OpenEphemeral,.
2af8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2af8c 20 20 20 20 20 20 20 20 20 20 20 70 4f 72 64 65             pOrde
2af8d 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 2c 20 70  rBy->iECursor, p
2af8e 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 2b 32  OrderBy->nExpr+2
2af8f 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
2af90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2af91 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c  (char*)pKeyInfo,
2af92 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
2af93 4f 46 46 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  OFF);.  }else{. 
2af94 20 20 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78     addrSortIndex
2af95 20 3d 20 2d 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a   = -1;.  }..  /*
2af96 20 49 66 20 74 68 65 20 6f 75 74 70 75 74 20 69   If the output i
2af97 73 20 64 65 73 74 69 6e 65 64 20 66 6f 72 20 61  s destined for a
2af98 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
2af99 2c 20 6f 70 65 6e 20 74 68 61 74 20 74 61 62 6c  , open that tabl
2af9a 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44  e..  */.  if( pD
2af9b 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f  est->eDest==SRT_
2af9c 45 70 68 65 6d 54 61 62 20 29 7b 0a 20 20 20 20  EphemTab ){.    
2af9d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2af9e 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65  2(v, OP_OpenEphe
2af9f 6d 65 72 61 6c 2c 20 70 44 65 73 74 2d 3e 69 50  meral, pDest->iP
2afa0 61 72 6d 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78  arm, pEList->nEx
2afa1 70 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53  pr);.  }..  /* S
2afa2 65 74 20 74 68 65 20 6c 69 6d 69 74 65 72 2e 0a  et the limiter..
2afa3 20 20 2a 2f 0a 20 20 69 45 6e 64 20 3d 20 73 71    */.  iEnd = sq
2afa4 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
2afa5 65 6c 28 76 29 3b 0a 20 20 63 6f 6d 70 75 74 65  el(v);.  compute
2afa6 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73 28 70  LimitRegisters(p
2afa7 50 61 72 73 65 2c 20 70 2c 20 69 45 6e 64 29 3b  Parse, p, iEnd);
2afa8 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 61 20 76 69  ..  /* Open a vi
2afa9 72 74 75 61 6c 20 69 6e 64 65 78 20 74 6f 20 75  rtual index to u
2afaa 73 65 20 66 6f 72 20 74 68 65 20 64 69 73 74 69  se for the disti
2afab 6e 63 74 20 73 65 74 2e 0a 20 20 2a 2f 0a 20 20  nct set..  */.  
2afac 69 66 28 20 69 73 44 69 73 74 69 6e 63 74 20 29  if( isDistinct )
2afad 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70  {.    KeyInfo *p
2afae 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 61 73 73  KeyInfo;.    ass
2afaf 65 72 74 28 20 69 73 41 67 67 20 7c 7c 20 70 47  ert( isAgg || pG
2afb0 72 6f 75 70 42 79 20 29 3b 0a 20 20 20 20 64 69  roupBy );.    di
2afb1 73 74 69 6e 63 74 20 3d 20 70 50 61 72 73 65 2d  stinct = pParse-
2afb2 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 70 4b 65  >nTab++;.    pKe
2afb3 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66 6f 46  yInfo = keyInfoF
2afb4 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72  romExprList(pPar
2afb5 73 65 2c 20 70 2d 3e 70 45 4c 69 73 74 29 3b 0a  se, p->pEList);.
2afb6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2afb7 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp4(v, OP_Open
2afb8 45 70 68 65 6d 65 72 61 6c 2c 20 64 69 73 74 69  Ephemeral, disti
2afb9 6e 63 74 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20  nct, 0, 0,.     
2afba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2afbb 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e     (char*)pKeyIn
2afbc 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48  fo, P4_KEYINFO_H
2afbd 41 4e 44 4f 46 46 29 3b 0a 20 20 7d 65 6c 73 65  ANDOFF);.  }else
2afbe 7b 0a 20 20 20 20 64 69 73 74 69 6e 63 74 20 3d  {.    distinct =
2afbf 20 2d 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41   -1;.  }..  /* A
2afc0 67 67 72 65 67 61 74 65 20 61 6e 64 20 6e 6f 6e  ggregate and non
2afc1 2d 61 67 67 72 65 67 61 74 65 20 71 75 65 72 69  -aggregate queri
2afc2 65 73 20 61 72 65 20 68 61 6e 64 6c 65 64 20 64  es are handled d
2afc3 69 66 66 65 72 65 6e 74 6c 79 20 2a 2f 0a 20 20  ifferently */.  
2afc4 69 66 28 20 21 69 73 41 67 67 20 26 26 20 70 47  if( !isAgg && pG
2afc5 72 6f 75 70 42 79 3d 3d 30 20 29 7b 0a 20 20 20  roupBy==0 ){.   
2afc6 20 2f 2a 20 54 68 69 73 20 63 61 73 65 20 69 73   /* This case is
2afc7 20 66 6f 72 20 6e 6f 6e 2d 61 67 67 72 65 67 61   for non-aggrega
2afc8 74 65 20 71 75 65 72 69 65 73 0a 20 20 20 20 2a  te queries.    *
2afc9 2a 20 42 65 67 69 6e 20 74 68 65 20 64 61 74 61  * Begin the data
2afca 62 61 73 65 20 73 63 61 6e 0a 20 20 20 20 2a 2f  base scan.    */
2afcb 0a 20 20 20 20 70 57 49 6e 66 6f 20 3d 20 73 71  .    pWInfo = sq
2afcc 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28  lite3WhereBegin(
2afcd 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74  pParse, pTabList
2afce 2c 20 70 57 68 65 72 65 2c 20 26 70 4f 72 64 65  , pWhere, &pOrde
2afcf 72 42 79 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  rBy, 0);.    if(
2afd0 20 70 57 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74   pWInfo==0 ) got
2afd1 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20  o select_end;.. 
2afd2 20 20 20 2f 2a 20 49 66 20 73 6f 72 74 69 6e 67     /* If sorting
2afd3 20 69 6e 64 65 78 20 74 68 61 74 20 77 61 73 20   index that was 
2afd4 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72 69  created by a pri
2afd5 6f 72 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  or OP_OpenEpheme
2afd6 72 61 6c 20 0a 20 20 20 20 2a 2a 20 69 6e 73 74  ral .    ** inst
2afd7 72 75 63 74 69 6f 6e 20 65 6e 64 65 64 20 75 70  ruction ended up
2afd8 20 6e 6f 74 20 62 65 69 6e 67 20 6e 65 65 64 65   not being neede
2afd9 64 2c 20 74 68 65 6e 20 63 68 61 6e 67 65 20 74  d, then change t
2afda 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  he OP_OpenEpheme
2afdb 72 61 6c 0a 20 20 20 20 2a 2a 20 69 6e 74 6f 20  ral.    ** into 
2afdc 61 6e 20 4f 50 5f 4e 6f 6f 70 2e 0a 20 20 20 20  an OP_Noop..    
2afdd 2a 2f 0a 20 20 20 20 69 66 28 20 61 64 64 72 53  */.    if( addrS
2afde 6f 72 74 49 6e 64 65 78 3e 3d 30 20 26 26 20 70  ortIndex>=0 && p
2afdf 4f 72 64 65 72 42 79 3d 3d 30 20 29 7b 0a 20 20  OrderBy==0 ){.  
2afe0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2afe1 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 61  hangeToNoop(v, a
2afe2 64 64 72 53 6f 72 74 49 6e 64 65 78 2c 20 31 29  ddrSortIndex, 1)
2afe3 3b 0a 20 20 20 20 20 20 70 2d 3e 61 64 64 72 4f  ;.      p->addrO
2afe4 70 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b  penEphm[2] = -1;
2afe5 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 55  .    }..    /* U
2afe6 73 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 20  se the standard 
2afe7 69 6e 6e 65 72 20 6c 6f 6f 70 0a 20 20 20 20 2a  inner loop.    *
2afe8 2f 0a 20 20 20 20 61 73 73 65 72 74 28 21 69 73  /.    assert(!is
2afe9 44 69 73 74 69 6e 63 74 29 3b 0a 20 20 20 20 73  Distinct);.    s
2afea 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70  electInnerLoop(p
2afeb 50 61 72 73 65 2c 20 70 2c 20 70 45 4c 69 73 74  Parse, p, pEList
2afec 2c 20 30 2c 20 30 2c 20 70 4f 72 64 65 72 42 79  , 0, 0, pOrderBy
2afed 2c 20 2d 31 2c 20 70 44 65 73 74 2c 0a 20 20 20  , -1, pDest,.   
2afee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2afef 20 70 57 49 6e 66 6f 2d 3e 69 43 6f 6e 74 69 6e   pWInfo->iContin
2aff0 75 65 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65  ue, pWInfo->iBre
2aff1 61 6b 29 3b 0a 0a 20 20 20 20 2f 2a 20 45 6e 64  ak);..    /* End
2aff2 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
2aff3 61 6e 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a  an loop..    */.
2aff4 20 20 20 20 73 71 6c 69 74 65 33 57 68 65 72 65      sqlite3Where
2aff5 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a 20 20 7d  End(pWInfo);.  }
2aff6 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 69  else{.    /* Thi
2aff7 73 20 69 73 20 74 68 65 20 70 72 6f 63 65 73 73  s is the process
2aff8 69 6e 67 20 66 6f 72 20 61 67 67 72 65 67 61 74  ing for aggregat
2aff9 65 20 71 75 65 72 69 65 73 20 2a 2f 0a 20 20 20  e queries */.   
2affa 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43   NameContext sNC
2affb 3b 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e  ;    /* Name con
2affc 74 65 78 74 20 66 6f 72 20 70 72 6f 63 65 73 73  text for process
2affd 69 6e 67 20 61 67 67 72 65 67 61 74 65 20 69 6e  ing aggregate in
2affe 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 20  formation */.   
2afff 20 69 6e 74 20 69 41 4d 65 6d 3b 20 20 20 20 20   int iAMem;     
2b000 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 4d 65       /* First Me
2b001 6d 20 61 64 64 72 65 73 73 20 66 6f 72 20 73 74  m address for st
2b002 6f 72 69 6e 67 20 63 75 72 72 65 6e 74 20 47 52  oring current GR
2b003 4f 55 50 20 42 59 20 2a 2f 0a 20 20 20 20 69 6e  OUP BY */.    in
2b004 74 20 69 42 4d 65 6d 3b 20 20 20 20 20 20 20 20  t iBMem;        
2b005 20 20 2f 2a 20 46 69 72 73 74 20 4d 65 6d 20 61    /* First Mem a
2b006 64 64 72 65 73 73 20 66 6f 72 20 70 72 65 76 69  ddress for previ
2b007 6f 75 73 20 47 52 4f 55 50 20 42 59 20 2a 2f 0a  ous GROUP BY */.
2b008 20 20 20 20 69 6e 74 20 69 55 73 65 46 6c 61 67      int iUseFlag
2b009 3b 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 20 61  ;       /* Mem a
2b00a 64 64 72 65 73 73 20 68 6f 6c 64 69 6e 67 20 66  ddress holding f
2b00b 6c 61 67 20 69 6e 64 69 63 61 74 69 6e 67 20 74  lag indicating t
2b00c 68 61 74 20 61 74 20 6c 65 61 73 74 0a 20 20 20  hat at least.   
2b00d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b00e 20 20 20 20 20 2a 2a 20 6f 6e 65 20 72 6f 77 20       ** one row 
2b00f 6f 66 20 74 68 65 20 69 6e 70 75 74 20 74 6f 20  of the input to 
2b010 74 68 65 20 61 67 67 72 65 67 61 74 6f 72 20 68  the aggregator h
2b011 61 73 20 62 65 65 6e 0a 20 20 20 20 20 20 20 20  as been.        
2b012 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b013 2a 2a 20 70 72 6f 63 65 73 73 65 64 20 2a 2f 0a  ** processed */.
2b014 20 20 20 20 69 6e 74 20 69 41 62 6f 72 74 46 6c      int iAbortFl
2b015 61 67 3b 20 20 20 20 20 2f 2a 20 4d 65 6d 20 61  ag;     /* Mem a
2b016 64 64 72 65 73 73 20 77 68 69 63 68 20 63 61 75  ddress which cau
2b017 73 65 73 20 71 75 65 72 79 20 61 62 6f 72 74 20  ses query abort 
2b018 69 66 20 70 6f 73 69 74 69 76 65 20 2a 2f 0a 20  if positive */. 
2b019 20 20 20 69 6e 74 20 67 72 6f 75 70 42 79 53 6f     int groupBySo
2b01a 72 74 3b 20 20 20 20 2f 2a 20 52 6f 77 73 20 63  rt;    /* Rows c
2b01b 6f 6d 65 20 66 72 6f 6d 20 73 6f 75 72 63 65 20  ome from source 
2b01c 69 6e 20 47 52 4f 55 50 20 42 59 20 6f 72 64 65  in GROUP BY orde
2b01d 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 61 64 64  r */.    int add
2b01e 72 45 6e 64 3b 20 20 20 20 20 20 20 20 2f 2a 20  rEnd;        /* 
2b01f 45 6e 64 20 6f 66 20 70 72 6f 63 65 73 73 69 6e  End of processin
2b020 67 20 66 6f 72 20 74 68 69 73 20 53 45 4c 45 43  g for this SELEC
2b021 54 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 52 65 6d  T */..    /* Rem
2b022 6f 76 65 20 61 6e 79 20 61 6e 64 20 61 6c 6c 20  ove any and all 
2b023 61 6c 69 61 73 65 73 20 62 65 74 77 65 65 6e 20  aliases between 
2b024 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 61  the result set a
2b025 6e 64 20 74 68 65 0a 20 20 20 20 2a 2a 20 47 52  nd the.    ** GR
2b026 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e 0a 20  OUP BY clause.. 
2b027 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 47     */.    if( pG
2b028 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 20 20  roupBy ){.      
2b029 69 6e 74 20 6b 3b 20 20 20 20 20 20 20 20 20 20  int k;          
2b02a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2b02b 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
2b02c 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45 78  .      struct Ex
2b02d 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  prList_item *pIt
2b02e 65 6d 3b 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70  em;  /* For loop
2b02f 69 6e 67 20 6f 76 65 72 20 65 78 70 72 65 73 73  ing over express
2b030 69 6f 6e 20 69 6e 20 61 20 6c 69 73 74 20 2a 2f  ion in a list */
2b031 0a 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d 70 2d  ..      for(k=p-
2b032 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20  >pEList->nExpr, 
2b033 70 49 74 65 6d 3d 70 2d 3e 70 45 4c 69 73 74 2d  pItem=p->pEList-
2b034 3e 61 3b 20 6b 3e 30 3b 20 6b 2d 2d 2c 20 70 49  >a; k>0; k--, pI
2b035 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  tem++){.        
2b036 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 20 3d 20  pItem->iAlias = 
2b037 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
2b038 20 66 6f 72 28 6b 3d 70 47 72 6f 75 70 42 79 2d   for(k=pGroupBy-
2b039 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d 3d 70 47  >nExpr, pItem=pG
2b03a 72 6f 75 70 42 79 2d 3e 61 3b 20 6b 3e 30 3b 20  roupBy->a; k>0; 
2b03b 6b 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  k--, pItem++){. 
2b03c 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 41         pItem->iA
2b03d 6c 69 61 73 20 3d 20 30 3b 0a 20 20 20 20 20 20  lias = 0;.      
2b03e 7d 0a 20 20 20 20 7d 0a 0a 20 0a 20 20 20 20 2f  }.    }.. .    /
2b03f 2a 20 43 72 65 61 74 65 20 61 20 6c 61 62 65 6c  * Create a label
2b040 20 74 6f 20 6a 75 6d 70 20 74 6f 20 77 68 65 6e   to jump to when
2b041 20 77 65 20 77 61 6e 74 20 74 6f 20 61 62 6f 72   we want to abor
2b042 74 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20  t the query */. 
2b043 20 20 20 61 64 64 72 45 6e 64 20 3d 20 73 71 6c     addrEnd = sql
2b044 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
2b045 6c 28 76 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f  l(v);..    /* Co
2b046 6e 76 65 72 74 20 54 4b 5f 43 4f 4c 55 4d 4e 20  nvert TK_COLUMN 
2b047 6e 6f 64 65 73 20 69 6e 74 6f 20 54 4b 5f 41 47  nodes into TK_AG
2b048 47 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 6d 61 6b  G_COLUMN and mak
2b049 65 20 65 6e 74 72 69 65 73 20 69 6e 0a 20 20 20  e entries in.   
2b04a 20 2a 2a 20 73 41 67 67 49 6e 66 6f 20 66 6f 72   ** sAggInfo for
2b04b 20 61 6c 6c 20 54 4b 5f 41 47 47 5f 46 55 4e 43   all TK_AGG_FUNC
2b04c 54 49 4f 4e 20 6e 6f 64 65 73 20 69 6e 20 65 78  TION nodes in ex
2b04d 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74 68 65  pressions of the
2b04e 0a 20 20 20 20 2a 2a 20 53 45 4c 45 43 54 20 73  .    ** SELECT s
2b04f 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f  tatement..    */
2b050 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e 43  .    memset(&sNC
2b051 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29  , 0, sizeof(sNC)
2b052 29 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 73  );.    sNC.pPars
2b053 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 20  e = pParse;.    
2b054 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70  sNC.pSrcList = p
2b055 54 61 62 4c 69 73 74 3b 0a 20 20 20 20 73 4e 43  TabList;.    sNC
2b056 2e 70 41 67 67 49 6e 66 6f 20 3d 20 26 73 41 67  .pAggInfo = &sAg
2b057 67 49 6e 66 6f 3b 0a 20 20 20 20 73 41 67 67 49  gInfo;.    sAggI
2b058 6e 66 6f 2e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75  nfo.nSortingColu
2b059 6d 6e 20 3d 20 70 47 72 6f 75 70 42 79 20 3f 20  mn = pGroupBy ? 
2b05a 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 2b  pGroupBy->nExpr+
2b05b 31 20 3a 20 30 3b 0a 20 20 20 20 73 41 67 67 49  1 : 0;.    sAggI
2b05c 6e 66 6f 2e 70 47 72 6f 75 70 42 79 20 3d 20 70  nfo.pGroupBy = p
2b05d 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 73 71 6c  GroupBy;.    sql
2b05e 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41  ite3ExprAnalyzeA
2b05f 67 67 4c 69 73 74 28 26 73 4e 43 2c 20 70 45 4c  ggList(&sNC, pEL
2b060 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ist);.    sqlite
2b061 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c  3ExprAnalyzeAggL
2b062 69 73 74 28 26 73 4e 43 2c 20 70 4f 72 64 65 72  ist(&sNC, pOrder
2b063 42 79 29 3b 0a 20 20 20 20 69 66 28 20 70 48 61  By);.    if( pHa
2b064 76 69 6e 67 20 29 7b 0a 20 20 20 20 20 20 73 71  ving ){.      sq
2b065 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65  lite3ExprAnalyze
2b066 41 67 67 72 65 67 61 74 65 73 28 26 73 4e 43 2c  Aggregates(&sNC,
2b067 20 70 48 61 76 69 6e 67 29 3b 0a 20 20 20 20 7d   pHaving);.    }
2b068 0a 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 6e 41  .    sAggInfo.nA
2b069 63 63 75 6d 75 6c 61 74 6f 72 20 3d 20 73 41 67  ccumulator = sAg
2b06a 67 49 6e 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b 0a 20  gInfo.nColumn;. 
2b06b 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 41     for(i=0; i<sA
2b06c 67 67 49 6e 66 6f 2e 6e 46 75 6e 63 3b 20 69 2b  ggInfo.nFunc; i+
2b06d 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  +){.      assert
2b06e 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
2b06f 74 79 28 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e  ty(sAggInfo.aFun
2b070 63 5b 69 5d 2e 70 45 78 70 72 2c 20 45 50 5f 78  c[i].pExpr, EP_x
2b071 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20  IsSelect) );.   
2b072 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e     sqlite3ExprAn
2b073 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 26 73 4e  alyzeAggList(&sN
2b074 43 2c 20 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e  C, sAggInfo.aFun
2b075 63 5b 69 5d 2e 70 45 78 70 72 2d 3e 78 2e 70 4c  c[i].pExpr->x.pL
2b076 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ist);.    }.    
2b077 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
2b078 69 6c 65 64 20 29 20 67 6f 74 6f 20 73 65 6c 65  iled ) goto sele
2b079 63 74 5f 65 6e 64 3b 0a 0a 20 20 20 20 2f 2a 20  ct_end;..    /* 
2b07a 50 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 61  Processing for a
2b07b 67 67 72 65 67 61 74 65 73 20 77 69 74 68 20 47  ggregates with G
2b07c 52 4f 55 50 20 42 59 20 69 73 20 76 65 72 79 20  ROUP BY is very 
2b07d 64 69 66 66 65 72 65 6e 74 20 61 6e 64 0a 20 20  different and.  
2b07e 20 20 2a 2a 20 6d 75 63 68 20 6d 6f 72 65 20 63    ** much more c
2b07f 6f 6d 70 6c 65 78 20 74 68 61 6e 20 61 67 67 72  omplex than aggr
2b080 65 67 61 74 65 73 20 77 69 74 68 6f 75 74 20 61  egates without a
2b081 20 47 52 4f 55 50 20 42 59 2e 0a 20 20 20 20 2a   GROUP BY..    *
2b082 2f 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75 70  /.    if( pGroup
2b083 42 79 20 29 7b 0a 20 20 20 20 20 20 4b 65 79 49  By ){.      KeyI
2b084 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 20  nfo *pKeyInfo;  
2b085 2f 2a 20 4b 65 79 69 6e 67 20 69 6e 66 6f 72 6d  /* Keying inform
2b086 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 67 72  ation for the gr
2b087 6f 75 70 20 62 79 20 63 6c 61 75 73 65 20 2a 2f  oup by clause */
2b088 0a 20 20 20 20 20 20 69 6e 74 20 6a 31 3b 20 20  .      int j1;  
2b089 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 2d             /* A-
2b08a 76 73 2d 42 20 63 6f 6d 70 61 72 69 73 69 6f 6e  vs-B comparision
2b08b 20 6a 75 6d 70 20 2a 2f 0a 20 20 20 20 20 20 69   jump */.      i
2b08c 6e 74 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77  nt addrOutputRow
2b08d 3b 20 20 2f 2a 20 53 74 61 72 74 20 6f 66 20 73  ;  /* Start of s
2b08e 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20 6f  ubroutine that o
2b08f 75 74 70 75 74 73 20 61 20 72 65 73 75 6c 74 20  utputs a result 
2b090 72 6f 77 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  row */.      int
2b091 20 72 65 67 4f 75 74 70 75 74 52 6f 77 3b 20 20   regOutputRow;  
2b092 20 2f 2a 20 52 65 74 75 72 6e 20 61 64 64 72 65   /* Return addre
2b093 73 73 20 72 65 67 69 73 74 65 72 20 66 6f 72 20  ss register for 
2b094 6f 75 74 70 75 74 20 73 75 62 72 6f 75 74 69 6e  output subroutin
2b095 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61  e */.      int a
2b096 64 64 72 53 65 74 41 62 6f 72 74 3b 20 20 20 2f  ddrSetAbort;   /
2b097 2a 20 53 65 74 20 74 68 65 20 61 62 6f 72 74 20  * Set the abort 
2b098 66 6c 61 67 20 61 6e 64 20 72 65 74 75 72 6e 20  flag and return 
2b099 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64  */.      int add
2b09a 72 54 6f 70 4f 66 4c 6f 6f 70 3b 20 20 2f 2a 20  rTopOfLoop;  /* 
2b09b 54 6f 70 20 6f 66 20 74 68 65 20 69 6e 70 75 74  Top of the input
2b09c 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20 20 20 69   loop */.      i
2b09d 6e 74 20 61 64 64 72 53 6f 72 74 69 6e 67 49 64  nt addrSortingId
2b09e 78 3b 20 2f 2a 20 54 68 65 20 4f 50 5f 4f 70 65  x; /* The OP_Ope
2b09f 6e 45 70 68 65 6d 65 72 61 6c 20 66 6f 72 20 74  nEphemeral for t
2b0a0 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78  he sorting index
2b0a1 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64   */.      int ad
2b0a2 64 72 52 65 73 65 74 3b 20 20 20 20 20 20 2f 2a  drReset;      /*
2b0a3 20 53 75 62 72 6f 75 74 69 6e 65 20 66 6f 72 20   Subroutine for 
2b0a4 72 65 73 65 74 74 69 6e 67 20 74 68 65 20 61 63  resetting the ac
2b0a5 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20  cumulator */.   
2b0a6 20 20 20 69 6e 74 20 72 65 67 52 65 73 65 74 3b     int regReset;
2b0a7 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
2b0a8 20 61 64 64 72 65 73 73 20 72 65 67 69 73 74 65   address registe
2b0a9 72 20 66 6f 72 20 72 65 73 65 74 20 73 75 62 72  r for reset subr
2b0aa 6f 75 74 69 6e 65 20 2a 2f 0a 0a 20 20 20 20 20  outine */..     
2b0ab 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
2b0ac 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  a GROUP BY claus
2b0ad 65 20 77 65 20 6d 69 67 68 74 20 6e 65 65 64 20  e we might need 
2b0ae 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20  a sorting index 
2b0af 74 6f 0a 20 20 20 20 20 20 2a 2a 20 69 6d 70 6c  to.      ** impl
2b0b0 65 6d 65 6e 74 20 69 74 2e 20 20 41 6c 6c 6f 63  ement it.  Alloc
2b0b1 61 74 65 20 74 68 61 74 20 73 6f 72 74 69 6e 67  ate that sorting
2b0b2 20 69 6e 64 65 78 20 6e 6f 77 2e 20 20 49 66 20   index now.  If 
2b0b3 69 74 20 74 75 72 6e 73 20 6f 75 74 0a 20 20 20  it turns out.   
2b0b4 20 20 20 2a 2a 20 74 68 61 74 20 77 65 20 64 6f     ** that we do
2b0b5 20 6e 6f 74 20 6e 65 65 64 20 69 74 20 61 66 74   not need it aft
2b0b6 65 72 20 61 6c 6c 2c 20 74 68 65 20 4f 70 65 6e  er all, the Open
2b0b7 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72 75  Ephemeral instru
2b0b8 63 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 77  ction.      ** w
2b0b9 69 6c 6c 20 62 65 20 63 6f 6e 76 65 72 74 65 64  ill be converted
2b0ba 20 69 6e 74 6f 20 61 20 4e 6f 6f 70 2e 20 20 0a   into a Noop.  .
2b0bb 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
2b0bc 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49  AggInfo.sortingI
2b0bd 64 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  dx = pParse->nTa
2b0be 62 2b 2b 3b 0a 20 20 20 20 20 20 70 4b 65 79 49  b++;.      pKeyI
2b0bf 6e 66 6f 20 3d 20 6b 65 79 49 6e 66 6f 46 72 6f  nfo = keyInfoFro
2b0c0 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 65  mExprList(pParse
2b0c1 2c 20 70 47 72 6f 75 70 42 79 29 3b 0a 20 20 20  , pGroupBy);.   
2b0c2 20 20 20 61 64 64 72 53 6f 72 74 69 6e 67 49 64     addrSortingId
2b0c3 78 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  x = sqlite3VdbeA
2b0c4 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp4(v, OP_Open
2b0c5 45 70 68 65 6d 65 72 61 6c 2c 20 0a 20 20 20 20  Ephemeral, .    
2b0c6 20 20 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 73        sAggInfo.s
2b0c7 6f 72 74 69 6e 67 49 64 78 2c 20 73 41 67 67 49  ortingIdx, sAggI
2b0c8 6e 66 6f 2e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75  nfo.nSortingColu
2b0c9 6d 6e 2c 20 0a 20 20 20 20 20 20 20 20 20 20 30  mn, .          0
2b0ca 2c 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66  , (char*)pKeyInf
2b0cb 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41  o, P4_KEYINFO_HA
2b0cc 4e 44 4f 46 46 29 3b 0a 0a 20 20 20 20 20 20 2f  NDOFF);..      /
2b0cd 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 6d 65 6d  * Initialize mem
2b0ce 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 75 73  ory locations us
2b0cf 65 64 20 62 79 20 47 52 4f 55 50 20 42 59 20 61  ed by GROUP BY a
2b0d0 67 67 72 65 67 61 74 65 20 70 72 6f 63 65 73 73  ggregate process
2b0d1 69 6e 67 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ing.      */.   
2b0d2 20 20 20 69 55 73 65 46 6c 61 67 20 3d 20 2b 2b     iUseFlag = ++
2b0d3 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
2b0d4 20 20 20 20 69 41 62 6f 72 74 46 6c 61 67 20 3d      iAbortFlag =
2b0d5 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2b0d6 0a 20 20 20 20 20 20 72 65 67 4f 75 74 70 75 74  .      regOutput
2b0d7 52 6f 77 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  Row = ++pParse->
2b0d8 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 61 64 64 72  nMem;.      addr
2b0d9 4f 75 74 70 75 74 52 6f 77 20 3d 20 73 71 6c 69  OutputRow = sqli
2b0da 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
2b0db 28 76 29 3b 0a 20 20 20 20 20 20 72 65 67 52 65  (v);.      regRe
2b0dc 73 65 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  set = ++pParse->
2b0dd 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 61 64 64 72  nMem;.      addr
2b0de 52 65 73 65 74 20 3d 20 73 71 6c 69 74 65 33 56  Reset = sqlite3V
2b0df 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
2b0e0 0a 20 20 20 20 20 20 69 41 4d 65 6d 20 3d 20 70  .      iAMem = p
2b0e1 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b  Parse->nMem + 1;
2b0e2 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  .      pParse->n
2b0e3 4d 65 6d 20 2b 3d 20 70 47 72 6f 75 70 42 79 2d  Mem += pGroupBy-
2b0e4 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 69 42  >nExpr;.      iB
2b0e5 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d  Mem = pParse->nM
2b0e6 65 6d 20 2b 20 31 3b 0a 20 20 20 20 20 20 70 50  em + 1;.      pP
2b0e7 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 47  arse->nMem += pG
2b0e8 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 3b 0a 20  roupBy->nExpr;. 
2b0e9 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2b0ea 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
2b0eb 65 67 65 72 2c 20 30 2c 20 69 41 62 6f 72 74 46  eger, 0, iAbortF
2b0ec 6c 61 67 29 3b 0a 20 20 20 20 20 20 56 64 62 65  lag);.      Vdbe
2b0ed 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 63 6c 65  Comment((v, "cle
2b0ee 61 72 20 61 62 6f 72 74 20 66 6c 61 67 22 29 29  ar abort flag"))
2b0ef 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2b0f0 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2b0f1 49 6e 74 65 67 65 72 2c 20 30 2c 20 69 55 73 65  Integer, 0, iUse
2b0f2 46 6c 61 67 29 3b 0a 20 20 20 20 20 20 56 64 62  Flag);.      Vdb
2b0f3 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 69 6e  eComment((v, "in
2b0f4 64 69 63 61 74 65 20 61 63 63 75 6d 75 6c 61 74  dicate accumulat
2b0f5 6f 72 20 65 6d 70 74 79 22 29 29 3b 0a 0a 20 20  or empty"));..  
2b0f6 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 61 20 6c      /* Begin a l
2b0f7 6f 6f 70 20 74 68 61 74 20 77 69 6c 6c 20 65 78  oop that will ex
2b0f8 74 72 61 63 74 20 61 6c 6c 20 73 6f 75 72 63 65  tract all source
2b0f9 20 72 6f 77 73 20 69 6e 20 47 52 4f 55 50 20 42   rows in GROUP B
2b0fa 59 20 6f 72 64 65 72 2e 0a 20 20 20 20 20 20 2a  Y order..      *
2b0fb 2a 20 54 68 69 73 20 6d 69 67 68 74 20 69 6e 76  * This might inv
2b0fc 6f 6c 76 65 20 74 77 6f 20 73 65 70 61 72 61 74  olve two separat
2b0fd 65 20 6c 6f 6f 70 73 20 77 69 74 68 20 61 6e 20  e loops with an 
2b0fe 4f 50 5f 53 6f 72 74 20 69 6e 20 62 65 74 77 65  OP_Sort in betwe
2b0ff 65 6e 2c 20 6f 72 0a 20 20 20 20 20 20 2a 2a 20  en, or.      ** 
2b100 69 74 20 6d 69 67 68 74 20 62 65 20 61 20 73 69  it might be a si
2b101 6e 67 6c 65 20 6c 6f 6f 70 20 74 68 61 74 20 75  ngle loop that u
2b102 73 65 73 20 61 6e 20 69 6e 64 65 78 20 74 6f 20  ses an index to 
2b103 65 78 74 72 61 63 74 20 69 6e 66 6f 72 6d 61 74  extract informat
2b104 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 69 6e 20  ion.      ** in 
2b105 74 68 65 20 72 69 67 68 74 20 6f 72 64 65 72 20  the right order 
2b106 74 6f 20 62 65 67 69 6e 20 77 69 74 68 2e 0a 20  to begin with.. 
2b107 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
2b108 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b109 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67  v, OP_Gosub, reg
2b10a 52 65 73 65 74 2c 20 61 64 64 72 52 65 73 65 74  Reset, addrReset
2b10b 29 3b 0a 20 20 20 20 20 20 70 57 49 6e 66 6f 20  );.      pWInfo 
2b10c 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  = sqlite3WhereBe
2b10d 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62  gin(pParse, pTab
2b10e 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 20 26 70  List, pWhere, &p
2b10f 47 72 6f 75 70 42 79 2c 20 30 29 3b 0a 20 20 20  GroupBy, 0);.   
2b110 20 20 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30     if( pWInfo==0
2b111 20 29 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65   ) goto select_e
2b112 6e 64 3b 0a 20 20 20 20 20 20 69 66 28 20 70 47  nd;.      if( pG
2b113 72 6f 75 70 42 79 3d 3d 30 20 29 7b 0a 20 20 20  roupBy==0 ){.   
2b114 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 74 69       /* The opti
2b115 6d 69 7a 65 72 20 69 73 20 61 62 6c 65 20 74 6f  mizer is able to
2b116 20 64 65 6c 69 76 65 72 20 72 6f 77 73 20 69 6e   deliver rows in
2b117 20 67 72 6f 75 70 20 62 79 20 6f 72 64 65 72 20   group by order 
2b118 73 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 65  so.        ** we
2b119 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 6f 20   do not have to 
2b11a 73 6f 72 74 2e 20 20 54 68 65 20 4f 50 5f 4f 70  sort.  The OP_Op
2b11b 65 6e 45 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  enEphemeral tabl
2b11c 65 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 20  e will be.      
2b11d 20 20 2a 2a 20 63 61 6e 63 65 6c 6c 65 64 20 6c    ** cancelled l
2b11e 61 74 65 72 20 62 65 63 61 75 73 65 20 77 65 20  ater because we 
2b11f 73 74 69 6c 6c 20 6e 65 65 64 20 74 6f 20 75 73  still need to us
2b120 65 20 74 68 65 20 70 4b 65 79 49 6e 66 6f 0a 20  e the pKeyInfo. 
2b121 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
2b122 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e    pGroupBy = p->
2b123 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 20 20  pGroupBy;.      
2b124 20 20 67 72 6f 75 70 42 79 53 6f 72 74 20 3d 20    groupBySort = 
2b125 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  0;.      }else{.
2b126 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 73 20          /* Rows 
2b127 61 72 65 20 63 6f 6d 69 6e 67 20 6f 75 74 20 69  are coming out i
2b128 6e 20 75 6e 64 65 74 65 72 6d 69 6e 65 64 20 6f  n undetermined o
2b129 72 64 65 72 2e 20 20 57 65 20 68 61 76 65 20 74  rder.  We have t
2b12a 6f 20 70 75 73 68 0a 20 20 20 20 20 20 20 20 2a  o push.        *
2b12b 2a 20 65 61 63 68 20 72 6f 77 20 69 6e 74 6f 20  * each row into 
2b12c 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 2c  a sorting index,
2b12d 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 66   terminate the f
2b12e 69 72 73 74 20 6c 6f 6f 70 2c 0a 20 20 20 20 20  irst loop,.     
2b12f 20 20 20 2a 2a 20 74 68 65 6e 20 6c 6f 6f 70 20     ** then loop 
2b130 6f 76 65 72 20 74 68 65 20 73 6f 72 74 69 6e 67  over the sorting
2b131 20 69 6e 64 65 78 20 69 6e 20 6f 72 64 65 72 20   index in order 
2b132 74 6f 20 67 65 74 20 74 68 65 20 6f 75 74 70 75  to get the outpu
2b133 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20  t.        ** in 
2b134 73 6f 72 74 65 64 20 6f 72 64 65 72 0a 20 20 20  sorted order.   
2b135 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
2b136 69 6e 74 20 72 65 67 42 61 73 65 3b 0a 20 20 20  int regBase;.   
2b137 20 20 20 20 20 69 6e 74 20 72 65 67 52 65 63 6f       int regReco
2b138 72 64 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  rd;.        int 
2b139 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e  nCol;.        in
2b13a 74 20 6e 47 72 6f 75 70 42 79 3b 0a 0a 20 20 20  t nGroupBy;..   
2b13b 20 20 20 20 20 67 72 6f 75 70 42 79 53 6f 72 74       groupBySort
2b13c 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 6e 47   = 1;.        nG
2b13d 72 6f 75 70 42 79 20 3d 20 70 47 72 6f 75 70 42  roupBy = pGroupB
2b13e 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20  y->nExpr;.      
2b13f 20 20 6e 43 6f 6c 20 3d 20 6e 47 72 6f 75 70 42    nCol = nGroupB
2b140 79 20 2b 20 31 3b 0a 20 20 20 20 20 20 20 20 6a  y + 1;.        j
2b141 20 3d 20 6e 47 72 6f 75 70 42 79 2b 31 3b 0a 20   = nGroupBy+1;. 
2b142 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20         for(i=0; 
2b143 69 3c 73 41 67 67 49 6e 66 6f 2e 6e 43 6f 6c 75  i<sAggInfo.nColu
2b144 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  mn; i++){.      
2b145 20 20 20 20 69 66 28 20 73 41 67 67 49 6e 66 6f      if( sAggInfo
2b146 2e 61 43 6f 6c 5b 69 5d 2e 69 53 6f 72 74 65 72  .aCol[i].iSorter
2b147 43 6f 6c 75 6d 6e 3e 3d 6a 20 29 7b 0a 20 20 20  Column>=j ){.   
2b148 20 20 20 20 20 20 20 20 20 6e 43 6f 6c 2b 2b 3b           nCol++;
2b149 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a 2b 2b  .            j++
2b14a 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
2b14b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2b14c 72 65 67 42 61 73 65 20 3d 20 73 71 6c 69 74 65  regBase = sqlite
2b14d 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50  3GetTempRange(pP
2b14e 61 72 73 65 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20  arse, nCol);.   
2b14f 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2b150 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72 73  CacheClear(pPars
2b151 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  e);.        sqli
2b152 74 65 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c  te3ExprCodeExprL
2b153 69 73 74 28 70 50 61 72 73 65 2c 20 70 47 72 6f  ist(pParse, pGro
2b154 75 70 42 79 2c 20 72 65 67 42 61 73 65 2c 20 30  upBy, regBase, 0
2b155 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2b156 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b157 4f 50 5f 53 65 71 75 65 6e 63 65 2c 20 73 41 67  OP_Sequence, sAg
2b158 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78  gInfo.sortingIdx
2b159 2c 72 65 67 42 61 73 65 2b 6e 47 72 6f 75 70 42  ,regBase+nGroupB
2b15a 79 29 3b 0a 20 20 20 20 20 20 20 20 6a 20 3d 20  y);.        j = 
2b15b 6e 47 72 6f 75 70 42 79 2b 31 3b 0a 20 20 20 20  nGroupBy+1;.    
2b15c 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73      for(i=0; i<s
2b15d 41 67 67 49 6e 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b  AggInfo.nColumn;
2b15e 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
2b15f 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f   struct AggInfo_
2b160 63 6f 6c 20 2a 70 43 6f 6c 20 3d 20 26 73 41 67  col *pCol = &sAg
2b161 67 49 6e 66 6f 2e 61 43 6f 6c 5b 69 5d 3b 0a 20  gInfo.aCol[i];. 
2b162 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43 6f           if( pCo
2b163 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e  l->iSorterColumn
2b164 3e 3d 6a 20 29 7b 0a 20 20 20 20 20 20 20 20 20  >=j ){.         
2b165 20 20 20 69 6e 74 20 72 31 20 3d 20 6a 20 2b 20     int r1 = j + 
2b166 72 65 67 42 61 73 65 3b 0a 20 20 20 20 20 20 20  regBase;.       
2b167 20 20 20 20 20 69 6e 74 20 72 32 3b 0a 0a 20 20       int r2;..  
2b168 20 20 20 20 20 20 20 20 20 20 72 32 20 3d 20 73            r2 = s
2b169 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65  qlite3ExprCodeGe
2b16a 74 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20  tColumn(pParse, 
2b16b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2b16c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b16d 70 43 6f 6c 2d 3e 70 54 61 62 2c 20 70 43 6f 6c  pCol->pTab, pCol
2b16e 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70 43 6f 6c 2d  ->iColumn, pCol-
2b16f 3e 69 54 61 62 6c 65 2c 20 72 31 2c 20 30 29 3b  >iTable, r1, 0);
2b170 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
2b171 20 72 31 21 3d 72 32 20 29 7b 0a 20 20 20 20 20   r1!=r2 ){.     
2b172 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2b173 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b174 5f 53 43 6f 70 79 2c 20 72 32 2c 20 72 31 29 3b  _SCopy, r2, r1);
2b175 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
2b176 20 20 20 20 20 20 20 20 20 20 20 6a 2b 2b 3b 0a             j++;.
2b177 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2b178 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 65      }.        re
2b179 67 52 65 63 6f 72 64 20 3d 20 73 71 6c 69 74 65  gRecord = sqlite
2b17a 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
2b17b 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  se);.        sql
2b17c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2b17d 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c  , OP_MakeRecord,
2b17e 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6c 2c 20   regBase, nCol, 
2b17f 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 20 20  regRecord);.    
2b180 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b181 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49  ddOp2(v, OP_IdxI
2b182 6e 73 65 72 74 2c 20 73 41 67 67 49 6e 66 6f 2e  nsert, sAggInfo.
2b183 73 6f 72 74 69 6e 67 49 64 78 2c 20 72 65 67 52  sortingIdx, regR
2b184 65 63 6f 72 64 29 3b 0a 20 20 20 20 20 20 20 20  ecord);.        
2b185 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2b186 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65  mpReg(pParse, re
2b187 67 52 65 63 6f 72 64 29 3b 0a 20 20 20 20 20 20  gRecord);.      
2b188 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
2b189 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65  TempRange(pParse
2b18a 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6c 29  , regBase, nCol)
2b18b 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2b18c 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f  3WhereEnd(pWInfo
2b18d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2b18e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b18f 4f 50 5f 53 6f 72 74 2c 20 73 41 67 67 49 6e 66  OP_Sort, sAggInf
2b190 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20 61 64  o.sortingIdx, ad
2b191 64 72 45 6e 64 29 3b 0a 20 20 20 20 20 20 20 20  drEnd);.        
2b192 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
2b193 22 47 52 4f 55 50 20 42 59 20 73 6f 72 74 22 29  "GROUP BY sort")
2b194 29 3b 0a 20 20 20 20 20 20 20 20 73 41 67 67 49  );.        sAggI
2b195 6e 66 6f 2e 75 73 65 53 6f 72 74 69 6e 67 49 64  nfo.useSortingId
2b196 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 73  x = 1;.        s
2b197 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43  qlite3ExprCacheC
2b198 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20  lear(pParse);.  
2b199 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
2b19a 45 76 61 6c 75 61 74 65 20 74 68 65 20 63 75 72  Evaluate the cur
2b19b 72 65 6e 74 20 47 52 4f 55 50 20 42 59 20 74 65  rent GROUP BY te
2b19c 72 6d 73 20 61 6e 64 20 73 74 6f 72 65 20 69 6e  rms and store in
2b19d 20 62 30 2c 20 62 31 2c 20 62 32 2e 2e 2e 0a 20   b0, b1, b2.... 
2b19e 20 20 20 20 20 2a 2a 20 28 62 30 20 69 73 20 6d       ** (b0 is m
2b19f 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 69  emory location i
2b1a0 42 4d 65 6d 2b 30 2c 20 62 31 20 69 73 20 69 42  BMem+0, b1 is iB
2b1a1 4d 65 6d 2b 31 2c 20 61 6e 64 20 73 6f 20 66 6f  Mem+1, and so fo
2b1a2 72 74 68 29 0a 20 20 20 20 20 20 2a 2a 20 54 68  rth).      ** Th
2b1a3 65 6e 20 63 6f 6d 70 61 72 65 20 74 68 65 20 63  en compare the c
2b1a4 75 72 72 65 6e 74 20 47 52 4f 55 50 20 42 59 20  urrent GROUP BY 
2b1a5 74 65 72 6d 73 20 61 67 61 69 6e 73 74 20 74 68  terms against th
2b1a6 65 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d 73  e GROUP BY terms
2b1a7 0a 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74  .      ** from t
2b1a8 68 65 20 70 72 65 76 69 6f 75 73 20 72 6f 77 20  he previous row 
2b1a9 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64  currently stored
2b1aa 20 69 6e 20 61 30 2c 20 61 31 2c 20 61 32 2e 2e   in a0, a1, a2..
2b1ab 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
2b1ac 20 61 64 64 72 54 6f 70 4f 66 4c 6f 6f 70 20 3d   addrTopOfLoop =
2b1ad 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
2b1ae 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20  entAddr(v);.    
2b1af 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
2b1b0 68 65 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b  heClear(pParse);
2b1b1 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  .      for(j=0; 
2b1b2 6a 3c 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70  j<pGroupBy->nExp
2b1b3 72 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  r; j++){.       
2b1b4 20 69 66 28 20 67 72 6f 75 70 42 79 53 6f 72 74   if( groupBySort
2b1b5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
2b1b6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2b1b7 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 41  v, OP_Column, sA
2b1b8 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64  ggInfo.sortingId
2b1b9 78 2c 20 6a 2c 20 69 42 4d 65 6d 2b 6a 29 3b 0a  x, j, iBMem+j);.
2b1ba 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
2b1bb 20 20 20 20 20 20 20 20 20 73 41 67 67 49 6e 66           sAggInf
2b1bc 6f 2e 64 69 72 65 63 74 4d 6f 64 65 20 3d 20 31  o.directMode = 1
2b1bd 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
2b1be 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
2b1bf 73 65 2c 20 70 47 72 6f 75 70 42 79 2d 3e 61 5b  se, pGroupBy->a[
2b1c0 6a 5d 2e 70 45 78 70 72 2c 20 69 42 4d 65 6d 2b  j].pExpr, iBMem+
2b1c1 6a 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  j);.        }.  
2b1c2 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
2b1c3 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2b1c4 20 4f 50 5f 43 6f 6d 70 61 72 65 2c 20 69 41 4d   OP_Compare, iAM
2b1c5 65 6d 2c 20 69 42 4d 65 6d 2c 20 70 47 72 6f 75  em, iBMem, pGrou
2b1c6 70 42 79 2d 3e 6e 45 78 70 72 2c 0a 20 20 20 20  pBy->nExpr,.    
2b1c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b1c8 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65        (char*)pKe
2b1c9 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46  yInfo, P4_KEYINF
2b1ca 4f 29 3b 0a 20 20 20 20 20 20 6a 31 20 3d 20 73  O);.      j1 = s
2b1cb 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
2b1cc 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20  tAddr(v);.      
2b1cd 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b1ce 33 28 76 2c 20 4f 50 5f 4a 75 6d 70 2c 20 6a 31  3(v, OP_Jump, j1
2b1cf 2b 31 2c 20 30 2c 20 6a 31 2b 31 29 3b 0a 0a 20  +1, 0, j1+1);.. 
2b1d0 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65       /* Generate
2b1d1 20 63 6f 64 65 20 74 68 61 74 20 72 75 6e 73 20   code that runs 
2b1d2 77 68 65 6e 65 76 65 72 20 74 68 65 20 47 52 4f  whenever the GRO
2b1d3 55 50 20 42 59 20 63 68 61 6e 67 65 73 2e 0a 20  UP BY changes.. 
2b1d4 20 20 20 20 20 2a 2a 20 43 68 61 6e 67 65 73 20       ** Changes 
2b1d5 69 6e 20 74 68 65 20 47 52 4f 55 50 20 42 59 20  in the GROUP BY 
2b1d6 61 72 65 20 64 65 74 65 63 74 65 64 20 62 79 20  are detected by 
2b1d7 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 6f 64  the previous cod
2b1d8 65 0a 20 20 20 20 20 20 2a 2a 20 62 6c 6f 63 6b  e.      ** block
2b1d9 2e 20 20 49 66 20 74 68 65 72 65 20 77 65 72 65  .  If there were
2b1da 20 6e 6f 20 63 68 61 6e 67 65 73 2c 20 74 68 69   no changes, thi
2b1db 73 20 62 6c 6f 63 6b 20 69 73 20 73 6b 69 70 70  s block is skipp
2b1dc 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  ed..      **.   
2b1dd 20 20 20 2a 2a 20 54 68 69 73 20 63 6f 64 65 20     ** This code 
2b1de 63 6f 70 69 65 73 20 63 75 72 72 65 6e 74 20 67  copies current g
2b1df 72 6f 75 70 20 62 79 20 74 65 72 6d 73 20 69 6e  roup by terms in
2b1e0 20 62 30 2c 62 31 2c 62 32 2c 2e 2e 2e 0a 20 20   b0,b1,b2,....  
2b1e1 20 20 20 20 2a 2a 20 6f 76 65 72 20 74 6f 20 61      ** over to a
2b1e2 30 2c 61 31 2c 61 32 2e 20 20 49 74 20 74 68 65  0,a1,a2.  It the
2b1e3 6e 20 63 61 6c 6c 73 20 74 68 65 20 6f 75 74 70  n calls the outp
2b1e4 75 74 20 73 75 62 72 6f 75 74 69 6e 65 0a 20 20  ut subroutine.  
2b1e5 20 20 20 20 2a 2a 20 61 6e 64 20 72 65 73 65 74      ** and reset
2b1e6 73 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20  s the aggregate 
2b1e7 61 63 63 75 6d 75 6c 61 74 6f 72 20 72 65 67 69  accumulator regi
2b1e8 73 74 65 72 73 20 69 6e 20 70 72 65 70 61 72 61  sters in prepara
2b1e9 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 66 6f  tion.      ** fo
2b1ea 72 20 74 68 65 20 6e 65 78 74 20 47 52 4f 55 50  r the next GROUP
2b1eb 20 42 59 20 62 61 74 63 68 2e 0a 20 20 20 20 20   BY batch..     
2b1ec 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
2b1ed 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 70 50  3ExprCodeMove(pP
2b1ee 61 72 73 65 2c 20 69 42 4d 65 6d 2c 20 69 41 4d  arse, iBMem, iAM
2b1ef 65 6d 2c 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45  em, pGroupBy->nE
2b1f0 78 70 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  xpr);.      sqli
2b1f1 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b1f2 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75   OP_Gosub, regOu
2b1f3 74 70 75 74 52 6f 77 2c 20 61 64 64 72 4f 75 74  tputRow, addrOut
2b1f4 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 56  putRow);.      V
2b1f5 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
2b1f6 6f 75 74 70 75 74 20 6f 6e 65 20 72 6f 77 22 29  output one row")
2b1f7 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2b1f8 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b1f9 5f 49 66 50 6f 73 2c 20 69 41 62 6f 72 74 46 6c  _IfPos, iAbortFl
2b1fa 61 67 2c 20 61 64 64 72 45 6e 64 29 3b 0a 20 20  ag, addrEnd);.  
2b1fb 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
2b1fc 28 76 2c 20 22 63 68 65 63 6b 20 61 62 6f 72 74  (v, "check abort
2b1fd 20 66 6c 61 67 22 29 29 3b 0a 20 20 20 20 20 20   flag"));.      
2b1fe 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b1ff 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72  2(v, OP_Gosub, r
2b200 65 67 52 65 73 65 74 2c 20 61 64 64 72 52 65 73  egReset, addrRes
2b201 65 74 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43  et);.      VdbeC
2b202 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 72 65 73 65  omment((v, "rese
2b203 74 20 61 63 63 75 6d 75 6c 61 74 6f 72 22 29 29  t accumulator"))
2b204 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 61  ;..      /* Upda
2b205 74 65 20 74 68 65 20 61 67 67 72 65 67 61 74 65  te the aggregate
2b206 20 61 63 63 75 6d 75 6c 61 74 6f 72 73 20 62 61   accumulators ba
2b207 73 65 64 20 6f 6e 20 74 68 65 20 63 6f 6e 74 65  sed on the conte
2b208 6e 74 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74  nt of.      ** t
2b209 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 0a 20  he current row. 
2b20a 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
2b20b 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
2b20c 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 20 20  e(v, j1);.      
2b20d 75 70 64 61 74 65 41 63 63 75 6d 75 6c 61 74 6f  updateAccumulato
2b20e 72 28 70 50 61 72 73 65 2c 20 26 73 41 67 67 49  r(pParse, &sAggI
2b20f 6e 66 6f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  nfo);.      sqli
2b210 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b211 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20   OP_Integer, 1, 
2b212 69 55 73 65 46 6c 61 67 29 3b 0a 20 20 20 20 20  iUseFlag);.     
2b213 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
2b214 20 22 69 6e 64 69 63 61 74 65 20 64 61 74 61 20   "indicate data 
2b215 69 6e 20 61 63 63 75 6d 75 6c 61 74 6f 72 22 29  in accumulator")
2b216 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 45 6e 64  );..      /* End
2b217 20 6f 66 20 74 68 65 20 6c 6f 6f 70 0a 20 20 20   of the loop.   
2b218 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
2b219 67 72 6f 75 70 42 79 53 6f 72 74 20 29 7b 0a 20  groupBySort ){. 
2b21a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2b21b 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
2b21c 65 78 74 2c 20 73 41 67 67 49 6e 66 6f 2e 73 6f  ext, sAggInfo.so
2b21d 72 74 69 6e 67 49 64 78 2c 20 61 64 64 72 54 6f  rtingIdx, addrTo
2b21e 70 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 20 20  pOfLoop);.      
2b21f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
2b220 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 70  qlite3WhereEnd(p
2b221 57 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20  WInfo);.        
2b222 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2b223 65 54 6f 4e 6f 6f 70 28 76 2c 20 61 64 64 72 53  eToNoop(v, addrS
2b224 6f 72 74 69 6e 67 49 64 78 2c 20 31 29 3b 0a 20  ortingIdx, 1);. 
2b225 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
2b226 20 4f 75 74 70 75 74 20 74 68 65 20 66 69 6e 61   Output the fina
2b227 6c 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 0a  l row of result.
2b228 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
2b229 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2b22a 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65  (v, OP_Gosub, re
2b22b 67 4f 75 74 70 75 74 52 6f 77 2c 20 61 64 64 72  gOutputRow, addr
2b22c 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20  OutputRow);.    
2b22d 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
2b22e 2c 20 22 6f 75 74 70 75 74 20 66 69 6e 61 6c 20  , "output final 
2b22f 72 6f 77 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f  row"));..      /
2b230 2a 20 4a 75 6d 70 20 6f 76 65 72 20 74 68 65 20  * Jump over the 
2b231 73 75 62 72 6f 75 74 69 6e 65 73 0a 20 20 20 20  subroutines.    
2b232 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74    */.      sqlit
2b233 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b234 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72  OP_Goto, 0, addr
2b235 45 6e 64 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  End);..      /* 
2b236 47 65 6e 65 72 61 74 65 20 61 20 73 75 62 72 6f  Generate a subro
2b237 75 74 69 6e 65 20 74 68 61 74 20 6f 75 74 70 75  utine that outpu
2b238 74 73 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20  ts a single row 
2b239 6f 66 20 74 68 65 20 72 65 73 75 6c 74 0a 20 20  of the result.  
2b23a 20 20 20 20 2a 2a 20 73 65 74 2e 20 20 54 68 69      ** set.  Thi
2b23b 73 20 73 75 62 72 6f 75 74 69 6e 65 20 66 69 72  s subroutine fir
2b23c 73 74 20 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20  st looks at the 
2b23d 69 55 73 65 46 6c 61 67 2e 20 20 49 66 20 69 55  iUseFlag.  If iU
2b23e 73 65 46 6c 61 67 0a 20 20 20 20 20 20 2a 2a 20  seFlag.      ** 
2b23f 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20  is less than or 
2b240 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2c 20 74  equal to zero, t
2b241 68 65 20 73 75 62 72 6f 75 74 69 6e 65 20 69 73  he subroutine is
2b242 20 61 20 6e 6f 2d 6f 70 2e 20 20 49 66 0a 20 20   a no-op.  If.  
2b243 20 20 20 20 2a 2a 20 74 68 65 20 70 72 6f 63 65      ** the proce
2b244 73 73 69 6e 67 20 63 61 6c 6c 73 20 66 6f 72 20  ssing calls for 
2b245 74 68 65 20 71 75 65 72 79 20 74 6f 20 61 62 6f  the query to abo
2b246 72 74 2c 20 74 68 69 73 20 73 75 62 72 6f 75 74  rt, this subrout
2b247 69 6e 65 0a 20 20 20 20 20 20 2a 2a 20 69 6e 63  ine.      ** inc
2b248 72 65 6d 65 6e 74 73 20 74 68 65 20 69 41 62 6f  rements the iAbo
2b249 72 74 46 6c 61 67 20 6d 65 6d 6f 72 79 20 6c 6f  rtFlag memory lo
2b24a 63 61 74 69 6f 6e 20 62 65 66 6f 72 65 20 72 65  cation before re
2b24b 74 75 72 6e 69 6e 67 20 69 6e 0a 20 20 20 20 20  turning in.     
2b24c 20 2a 2a 20 6f 72 64 65 72 20 74 6f 20 73 69 67   ** order to sig
2b24d 6e 61 6c 20 74 68 65 20 63 61 6c 6c 65 72 20 74  nal the caller t
2b24e 6f 20 61 62 6f 72 74 2e 0a 20 20 20 20 20 20 2a  o abort..      *
2b24f 2f 0a 20 20 20 20 20 20 61 64 64 72 53 65 74 41  /.      addrSetA
2b250 62 6f 72 74 20 3d 20 73 71 6c 69 74 65 33 56 64  bort = sqlite3Vd
2b251 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
2b252 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2b253 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2b254 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 41 62 6f  Integer, 1, iAbo
2b255 72 74 46 6c 61 67 29 3b 0a 20 20 20 20 20 20 56  rtFlag);.      V
2b256 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
2b257 73 65 74 20 61 62 6f 72 74 20 66 6c 61 67 22 29  set abort flag")
2b258 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2b259 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
2b25a 5f 52 65 74 75 72 6e 2c 20 72 65 67 4f 75 74 70  _Return, regOutp
2b25b 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 73 71  utRow);.      sq
2b25c 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
2b25d 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 4f 75 74  Label(v, addrOut
2b25e 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 61  putRow);.      a
2b25f 64 64 72 4f 75 74 70 75 74 52 6f 77 20 3d 20 73  ddrOutputRow = s
2b260 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
2b261 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20  tAddr(v);.      
2b262 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b263 32 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 69  2(v, OP_IfPos, i
2b264 55 73 65 46 6c 61 67 2c 20 61 64 64 72 4f 75 74  UseFlag, addrOut
2b265 70 75 74 52 6f 77 2b 32 29 3b 0a 20 20 20 20 20  putRow+2);.     
2b266 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
2b267 20 22 47 72 6f 75 70 62 79 20 72 65 73 75 6c 74   "Groupby result
2b268 20 67 65 6e 65 72 61 74 6f 72 20 65 6e 74 72 79   generator entry
2b269 20 70 6f 69 6e 74 22 29 29 3b 0a 20 20 20 20 20   point"));.     
2b26a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b26b 70 31 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c  p1(v, OP_Return,
2b26c 20 72 65 67 4f 75 74 70 75 74 52 6f 77 29 3b 0a   regOutputRow);.
2b26d 20 20 20 20 20 20 66 69 6e 61 6c 69 7a 65 41 67        finalizeAg
2b26e 67 46 75 6e 63 74 69 6f 6e 73 28 70 50 61 72 73  gFunctions(pPars
2b26f 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20  e, &sAggInfo);. 
2b270 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2b271 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20  IfFalse(pParse, 
2b272 70 48 61 76 69 6e 67 2c 20 61 64 64 72 4f 75 74  pHaving, addrOut
2b273 70 75 74 52 6f 77 2b 31 2c 20 53 51 4c 49 54 45  putRow+1, SQLITE
2b274 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20  _JUMPIFNULL);.  
2b275 20 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c      selectInnerL
2b276 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70  oop(pParse, p, p
2b277 2d 3e 70 45 4c 69 73 74 2c 20 30 2c 20 30 2c 20  ->pEList, 0, 0, 
2b278 70 4f 72 64 65 72 42 79 2c 0a 20 20 20 20 20 20  pOrderBy,.      
2b279 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b27a 64 69 73 74 69 6e 63 74 2c 20 70 44 65 73 74 2c  distinct, pDest,
2b27b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2b27c 20 20 20 20 20 20 20 61 64 64 72 4f 75 74 70 75         addrOutpu
2b27d 74 52 6f 77 2b 31 2c 20 61 64 64 72 53 65 74 41  tRow+1, addrSetA
2b27e 62 6f 72 74 29 3b 0a 20 20 20 20 20 20 73 71 6c  bort);.      sql
2b27f 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2b280 2c 20 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65 67  , OP_Return, reg
2b281 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20  OutputRow);.    
2b282 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
2b283 2c 20 22 65 6e 64 20 67 72 6f 75 70 62 79 20 72  , "end groupby r
2b284 65 73 75 6c 74 20 67 65 6e 65 72 61 74 6f 72 22  esult generator"
2b285 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 47 65  ));..      /* Ge
2b286 6e 65 72 61 74 65 20 61 20 73 75 62 72 6f 75 74  nerate a subrout
2b287 69 6e 65 20 74 68 61 74 20 77 69 6c 6c 20 72 65  ine that will re
2b288 73 65 74 20 74 68 65 20 67 72 6f 75 70 2d 62 79  set the group-by
2b289 20 61 63 63 75 6d 75 6c 61 74 6f 72 0a 20 20 20   accumulator.   
2b28a 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69     */.      sqli
2b28b 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
2b28c 62 65 6c 28 76 2c 20 61 64 64 72 52 65 73 65 74  bel(v, addrReset
2b28d 29 3b 0a 20 20 20 20 20 20 72 65 73 65 74 41 63  );.      resetAc
2b28e 63 75 6d 75 6c 61 74 6f 72 28 70 50 61 72 73 65  cumulator(pParse
2b28f 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20  , &sAggInfo);.  
2b290 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b291 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74 75  ddOp1(v, OP_Retu
2b292 72 6e 2c 20 72 65 67 52 65 73 65 74 29 3b 0a 20  rn, regReset);. 
2b293 20 20 20 20 0a 20 20 20 20 7d 20 2f 2a 20 65 6e      .    } /* en
2b294 64 69 66 20 70 47 72 6f 75 70 42 79 2e 20 20 42  dif pGroupBy.  B
2b295 65 67 69 6e 20 61 67 67 72 65 67 61 74 65 20 71  egin aggregate q
2b296 75 65 72 69 65 73 20 77 69 74 68 6f 75 74 20 47  ueries without G
2b297 52 4f 55 50 20 42 59 3a 20 2a 2f 0a 20 20 20 20  ROUP BY: */.    
2b298 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 45 78 70  else {.      Exp
2b299 72 4c 69 73 74 20 2a 70 44 65 6c 20 3d 20 30 3b  rList *pDel = 0;
2b29a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2b29b 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a  OMIT_BTREECOUNT.
2b29c 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61        Table *pTa
2b29d 62 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 54  b;.      if( (pT
2b29e 61 62 20 3d 20 69 73 53 69 6d 70 6c 65 43 6f 75  ab = isSimpleCou
2b29f 6e 74 28 70 2c 20 26 73 41 67 67 49 6e 66 6f 29  nt(p, &sAggInfo)
2b2a0 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
2b2a1 2f 2a 20 49 66 20 69 73 53 69 6d 70 6c 65 43 6f  /* If isSimpleCo
2b2a2 75 6e 74 28 29 20 72 65 74 75 72 6e 73 20 61 20  unt() returns a 
2b2a3 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54 61 62  pointer to a Tab
2b2a4 6c 65 20 73 74 72 75 63 74 75 72 65 2c 20 74 68  le structure, th
2b2a5 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  en.        ** th
2b2a6 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
2b2a7 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a  is of the form:.
2b2a8 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
2b2a9 20 20 20 2a 2a 20 20 20 53 45 4c 45 43 54 20 63     **   SELECT c
2b2aa 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 3c 74 62  ount(*) FROM <tb
2b2ab 6c 3e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20  l>.        **.  
2b2ac 20 20 20 20 20 20 2a 2a 20 77 68 65 72 65 20 74        ** where t
2b2ad 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  he Table structu
2b2ae 72 65 20 72 65 74 75 72 6e 65 64 20 72 65 70 72  re returned repr
2b2af 65 73 65 6e 74 73 20 74 61 62 6c 65 20 3c 74 62  esents table <tb
2b2b0 6c 3e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  l>..        **. 
2b2b1 20 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 73         ** This s
2b2b2 74 61 74 65 6d 65 6e 74 20 69 73 20 73 6f 20 63  tatement is so c
2b2b3 6f 6d 6d 6f 6e 20 74 68 61 74 20 69 74 20 69 73  ommon that it is
2b2b4 20 6f 70 74 69 6d 69 7a 65 64 20 73 70 65 63 69   optimized speci
2b2b5 61 6c 6c 79 2e 20 54 68 65 0a 20 20 20 20 20 20  ally. The.      
2b2b6 20 20 2a 2a 20 4f 50 5f 43 6f 75 6e 74 20 69 6e    ** OP_Count in
2b2b7 73 74 72 75 63 74 69 6f 6e 20 69 73 20 65 78 65  struction is exe
2b2b8 63 75 74 65 64 20 65 69 74 68 65 72 20 6f 6e 20  cuted either on 
2b2b9 74 68 65 20 69 6e 74 6b 65 79 20 74 61 62 6c 65  the intkey table
2b2ba 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a   that.        **
2b2bb 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 61   contains the da
2b2bc 74 61 20 66 6f 72 20 74 61 62 6c 65 20 3c 74 62  ta for table <tb
2b2bd 6c 3e 20 6f 72 20 6f 6e 20 6f 6e 65 20 6f 66 20  l> or on one of 
2b2be 69 74 73 20 69 6e 64 65 78 65 73 2e 20 49 74 0a  its indexes. It.
2b2bf 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 62 65          ** is be
2b2c0 74 74 65 72 20 74 6f 20 65 78 65 63 75 74 65 20  tter to execute 
2b2c1 74 68 65 20 6f 70 20 6f 6e 20 61 6e 20 69 6e 64  the op on an ind
2b2c2 65 78 2c 20 61 73 20 69 6e 64 65 78 65 73 20 61  ex, as indexes a
2b2c3 72 65 20 61 6c 6d 6f 73 74 0a 20 20 20 20 20 20  re almost.      
2b2c4 20 20 2a 2a 20 61 6c 77 61 79 73 20 73 70 72 65    ** always spre
2b2c5 61 64 20 61 63 72 6f 73 73 20 6c 65 73 73 20 70  ad across less p
2b2c6 61 67 65 73 20 74 68 61 6e 20 74 68 65 69 72 20  ages than their 
2b2c7 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 61  corresponding ta
2b2c8 62 6c 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f  bles..        */
2b2c9 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 69  .        const i
2b2ca 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  nt iDb = sqlite3
2b2cb 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50  SchemaToIndex(pP
2b2cc 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e  arse->db, pTab->
2b2cd 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20  pSchema);.      
2b2ce 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 43 73 72    const int iCsr
2b2cf 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
2b2d0 2b 3b 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72  +;     /* Cursor
2b2d1 20 74 6f 20 73 63 61 6e 20 62 2d 74 72 65 65 20   to scan b-tree 
2b2d2 2a 2f 0a 20 20 20 20 20 20 20 20 49 6e 64 65 78  */.        Index
2b2d3 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20   *pIdx;         
2b2d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b2d5 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69  /* Iterator vari
2b2d6 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20  able */.        
2b2d7 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66  KeyInfo *pKeyInf
2b2d8 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  o = 0;          
2b2d9 20 20 20 20 20 2f 2a 20 4b 65 79 69 6e 66 6f 20       /* Keyinfo 
2b2da 66 6f 72 20 73 63 61 6e 6e 65 64 20 69 6e 64 65  for scanned inde
2b2db 78 20 2a 2f 0a 20 20 20 20 20 20 20 20 49 6e 64  x */.        Ind
2b2dc 65 78 20 2a 70 42 65 73 74 20 3d 20 30 3b 20 20  ex *pBest = 0;  
2b2dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b2de 20 20 2f 2a 20 42 65 73 74 20 69 6e 64 65 78 20    /* Best index 
2b2df 66 6f 75 6e 64 20 73 6f 20 66 61 72 20 2a 2f 0a  found so far */.
2b2e0 20 20 20 20 20 20 20 20 69 6e 74 20 69 52 6f 6f          int iRoo
2b2e1 74 20 3d 20 70 54 61 62 2d 3e 74 6e 75 6d 3b 20  t = pTab->tnum; 
2b2e2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2b2e3 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 73 63 61  Root page of sca
2b2e4 6e 6e 65 64 20 62 2d 74 72 65 65 20 2a 2f 0a 0a  nned b-tree */..
2b2e5 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43          sqlite3C
2b2e6 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28  odeVerifySchema(
2b2e7 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20  pParse, iDb);.  
2b2e8 20 20 20 20 20 20 73 71 6c 69 74 65 33 54 61 62        sqlite3Tab
2b2e9 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69  leLock(pParse, i
2b2ea 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20  Db, pTab->tnum, 
2b2eb 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  0, pTab->zName);
2b2ec 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 65 61  ..        /* Sea
2b2ed 72 63 68 20 66 6f 72 20 74 68 65 20 69 6e 64 65  rch for the inde
2b2ee 78 20 74 68 61 74 20 68 61 73 20 74 68 65 20 6c  x that has the l
2b2ef 65 61 73 74 20 61 6d 6f 75 6e 74 20 6f 66 20 63  east amount of c
2b2f0 6f 6c 75 6d 6e 73 2e 20 49 66 0a 20 20 20 20 20  olumns. If.     
2b2f1 20 20 20 2a 2a 20 74 68 65 72 65 20 69 73 20 73     ** there is s
2b2f2 75 63 68 20 61 6e 20 69 6e 64 65 78 2c 20 61 6e  uch an index, an
2b2f3 64 20 69 74 20 68 61 73 20 6c 65 73 73 20 63 6f  d it has less co
2b2f4 6c 75 6d 6e 73 20 74 68 61 6e 20 74 68 65 20 74  lumns than the t
2b2f5 61 62 6c 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  able.        ** 
2b2f6 64 6f 65 73 2c 20 74 68 65 6e 20 77 65 20 63 61  does, then we ca
2b2f7 6e 20 61 73 73 75 6d 65 20 74 68 61 74 20 69 74  n assume that it
2b2f8 20 63 6f 6e 73 75 6d 65 73 20 6c 65 73 73 20 73   consumes less s
2b2f9 70 61 63 65 20 6f 6e 20 64 69 73 6b 20 61 6e 64  pace on disk and
2b2fa 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c  .        ** will
2b2fb 20 74 68 65 72 65 66 6f 72 65 20 62 65 20 63 68   therefore be ch
2b2fc 65 61 70 65 72 20 74 6f 20 73 63 61 6e 20 74 6f  eaper to scan to
2b2fd 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 71   determine the q
2b2fe 75 65 72 79 20 72 65 73 75 6c 74 2e 0a 20 20 20  uery result..   
2b2ff 20 20 20 20 20 2a 2a 20 49 6e 20 74 68 69 73 20       ** In this 
2b300 63 61 73 65 20 73 65 74 20 69 52 6f 6f 74 20 74  case set iRoot t
2b301 6f 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  o the root page 
2b302 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 69 6e  number of the in
2b303 64 65 78 20 62 2d 74 72 65 65 0a 20 20 20 20 20  dex b-tree.     
2b304 20 20 20 2a 2a 20 61 6e 64 20 70 4b 65 79 49 6e     ** and pKeyIn
2b305 66 6f 20 74 6f 20 74 68 65 20 4b 65 79 49 6e 66  fo to the KeyInf
2b306 6f 20 73 74 72 75 63 74 75 72 65 20 72 65 71 75  o structure requ
2b307 69 72 65 64 20 74 6f 20 6e 61 76 69 67 61 74 65  ired to navigate
2b308 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20   the.        ** 
2b309 69 6e 64 65 78 2e 0a 20 20 20 20 20 20 20 20 2a  index..        *
2b30a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 6e 20  *.        ** In 
2b30b 70 72 61 63 74 69 63 65 20 74 68 65 20 4b 65 79  practice the Key
2b30c 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 77  Info structure w
2b30d 69 6c 6c 20 6e 6f 74 20 62 65 20 75 73 65 64 2e  ill not be used.
2b30e 20 49 74 20 69 73 20 6f 6e 6c 79 20 0a 20 20 20   It is only .   
2b30f 20 20 20 20 20 2a 2a 20 70 61 73 73 65 64 20 74       ** passed t
2b310 6f 20 6b 65 65 70 20 4f 50 5f 4f 70 65 6e 52 65  o keep OP_OpenRe
2b311 61 64 20 68 61 70 70 79 2e 0a 20 20 20 20 20 20  ad happy..      
2b312 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72    */.        for
2b313 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64  (pIdx=pTab->pInd
2b314 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
2b315 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Idx->pNext){.   
2b316 20 20 20 20 20 20 20 69 66 28 20 21 70 42 65 73         if( !pBes
2b317 74 20 7c 7c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75  t || pIdx->nColu
2b318 6d 6e 3c 70 42 65 73 74 2d 3e 6e 43 6f 6c 75 6d  mn<pBest->nColum
2b319 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  n ){.           
2b31a 20 70 42 65 73 74 20 3d 20 70 49 64 78 3b 0a 20   pBest = pIdx;. 
2b31b 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2b31c 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
2b31d 20 70 42 65 73 74 20 26 26 20 70 42 65 73 74 2d   pBest && pBest-
2b31e 3e 6e 43 6f 6c 75 6d 6e 3c 70 54 61 62 2d 3e 6e  >nColumn<pTab->n
2b31f 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Col ){.         
2b320 20 69 52 6f 6f 74 20 3d 20 70 42 65 73 74 2d 3e   iRoot = pBest->
2b321 74 6e 75 6d 3b 0a 20 20 20 20 20 20 20 20 20 20  tnum;.          
2b322 70 4b 65 79 49 6e 66 6f 20 3d 20 73 71 6c 69 74  pKeyInfo = sqlit
2b323 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70  e3IndexKeyinfo(p
2b324 50 61 72 73 65 2c 20 70 42 65 73 74 29 3b 0a 20  Parse, pBest);. 
2b325 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
2b326 20 20 2f 2a 20 4f 70 65 6e 20 61 20 72 65 61 64    /* Open a read
2b327 2d 6f 6e 6c 79 20 63 75 72 73 6f 72 2c 20 65 78  -only cursor, ex
2b328 65 63 75 74 65 20 74 68 65 20 4f 50 5f 43 6f 75  ecute the OP_Cou
2b329 6e 74 2c 20 63 6c 6f 73 65 20 74 68 65 20 63 75  nt, close the cu
2b32a 72 73 6f 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20  rsor. */.       
2b32b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b32c 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61  p3(v, OP_OpenRea
2b32d 64 2c 20 69 43 73 72 2c 20 69 52 6f 6f 74 2c 20  d, iCsr, iRoot, 
2b32e 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20 69 66  iDb);.        if
2b32f 28 20 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20  ( pKeyInfo ){.  
2b330 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2b331 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d  dbeChangeP4(v, -
2b332 31 2c 20 28 63 68 61 72 20 2a 29 70 4b 65 79 49  1, (char *)pKeyI
2b333 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  nfo, P4_KEYINFO_
2b334 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 20  HANDOFF);.      
2b335 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69    }.        sqli
2b336 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b337 20 4f 50 5f 43 6f 75 6e 74 2c 20 69 43 73 72 2c   OP_Count, iCsr,
2b338 20 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 5b   sAggInfo.aFunc[
2b339 30 5d 2e 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20  0].iMem);.      
2b33a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b33b 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op1(v, OP_Close,
2b33c 20 69 43 73 72 29 3b 0a 20 20 20 20 20 20 7d 65   iCsr);.      }e
2b33d 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  lse.#endif /* SQ
2b33e 4c 49 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43  LITE_OMIT_BTREEC
2b33f 4f 55 4e 54 20 2a 2f 0a 20 20 20 20 20 20 7b 0a  OUNT */.      {.
2b340 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b          /* Check
2b341 20 69 66 20 74 68 65 20 71 75 65 72 79 20 69 73   if the query is
2b342 20 6f 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 66   of one of the f
2b343 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 73 3a 0a  ollowing forms:.
2b344 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
2b345 20 20 20 2a 2a 20 20 20 53 45 4c 45 43 54 20 6d     **   SELECT m
2b346 69 6e 28 78 29 20 46 52 4f 4d 20 2e 2e 2e 0a 20  in(x) FROM .... 
2b347 20 20 20 20 20 20 20 2a 2a 20 20 20 53 45 4c 45         **   SELE
2b348 43 54 20 6d 61 78 28 78 29 20 46 52 4f 4d 20 2e  CT max(x) FROM .
2b349 2e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20  ...        **.  
2b34a 20 20 20 20 20 20 2a 2a 20 49 66 20 69 74 20 69        ** If it i
2b34b 73 2c 20 74 68 65 6e 20 61 73 6b 20 74 68 65 20  s, then ask the 
2b34c 63 6f 64 65 20 69 6e 20 77 68 65 72 65 2e 63 20  code in where.c 
2b34d 74 6f 20 61 74 74 65 6d 70 74 20 74 6f 20 73 6f  to attempt to so
2b34e 72 74 20 72 65 73 75 6c 74 73 0a 20 20 20 20 20  rt results.     
2b34f 20 20 20 2a 2a 20 61 73 20 69 66 20 74 68 65 72     ** as if ther
2b350 65 20 77 61 73 20 61 6e 20 22 4f 52 44 45 52 20  e was an "ORDER 
2b351 4f 4e 20 78 22 20 6f 72 20 22 4f 52 44 45 52 20  ON x" or "ORDER 
2b352 4f 4e 20 78 20 44 45 53 43 22 20 63 6c 61 75 73  ON x DESC" claus
2b353 65 2e 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 49  e. .        ** I
2b354 66 20 77 68 65 72 65 2e 63 20 69 73 20 61 62 6c  f where.c is abl
2b355 65 20 74 6f 20 70 72 6f 64 75 63 65 20 72 65 73  e to produce res
2b356 75 6c 74 73 20 73 6f 72 74 65 64 20 69 6e 20 74  ults sorted in t
2b357 68 69 73 20 6f 72 64 65 72 2c 20 74 68 65 6e 0a  his order, then.
2b358 20 20 20 20 20 20 20 20 2a 2a 20 61 64 64 20 76          ** add v
2b359 64 62 65 20 63 6f 64 65 20 74 6f 20 62 72 65 61  dbe code to brea
2b35a 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 70 72 6f  k out of the pro
2b35b 63 65 73 73 69 6e 67 20 6c 6f 6f 70 20 61 66 74  cessing loop aft
2b35c 65 72 20 74 68 65 20 0a 20 20 20 20 20 20 20 20  er the .        
2b35d 2a 2a 20 66 69 72 73 74 20 69 74 65 72 61 74 69  ** first iterati
2b35e 6f 6e 20 28 73 69 6e 63 65 20 74 68 65 20 66 69  on (since the fi
2b35f 72 73 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66  rst iteration of
2b360 20 74 68 65 20 6c 6f 6f 70 20 69 73 20 0a 20 20   the loop is .  
2b361 20 20 20 20 20 20 2a 2a 20 67 75 61 72 61 6e 74        ** guarant
2b362 65 65 64 20 74 6f 20 6f 70 65 72 61 74 65 20 6f  eed to operate o
2b363 6e 20 74 68 65 20 72 6f 77 20 77 69 74 68 20 74  n the row with t
2b364 68 65 20 6d 69 6e 69 6d 75 6d 20 6f 72 20 6d 61  he minimum or ma
2b365 78 69 6d 75 6d 20 0a 20 20 20 20 20 20 20 20 2a  ximum .        *
2b366 2a 20 76 61 6c 75 65 20 6f 66 20 78 2c 20 74 68  * value of x, th
2b367 65 20 6f 6e 6c 79 20 72 6f 77 20 72 65 71 75 69  e only row requi
2b368 72 65 64 29 2e 0a 20 20 20 20 20 20 20 20 2a 2a  red)..        **
2b369 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 20 73 70  .        ** A sp
2b36a 65 63 69 61 6c 20 66 6c 61 67 20 6d 75 73 74 20  ecial flag must 
2b36b 62 65 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c  be passed to sql
2b36c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
2b36d 20 74 6f 20 73 6c 69 67 68 74 6c 79 0a 20 20 20   to slightly.   
2b36e 20 20 20 20 20 2a 2a 20 6d 6f 64 69 66 79 20 62       ** modify b
2b36f 65 68 61 76 69 6f 75 72 20 61 73 20 66 6f 6c 6c  ehaviour as foll
2b370 6f 77 73 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a  ows:.        **.
2b371 20 20 20 20 20 20 20 20 2a 2a 20 20 20 2b 20 49          **   + I
2b372 66 20 74 68 65 20 71 75 65 72 79 20 69 73 20 61  f the query is a
2b373 20 22 53 45 4c 45 43 54 20 6d 69 6e 28 78 29 22   "SELECT min(x)"
2b374 2c 20 74 68 65 6e 20 74 68 65 20 6c 6f 6f 70 20  , then the loop 
2b375 63 6f 64 65 64 20 62 79 0a 20 20 20 20 20 20 20  coded by.       
2b376 20 2a 2a 20 20 20 20 20 77 68 65 72 65 2e 63 20   **     where.c 
2b377 73 68 6f 75 6c 64 20 6e 6f 74 20 69 74 65 72 61  should not itera
2b378 74 65 20 6f 76 65 72 20 61 6e 79 20 76 61 6c 75  te over any valu
2b379 65 73 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 76  es with a NULL v
2b37a 61 6c 75 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  alue.        ** 
2b37b 20 20 20 20 66 6f 72 20 78 2e 0a 20 20 20 20 20      for x..     
2b37c 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
2b37d 20 20 20 2b 20 54 68 65 20 6f 70 74 69 6d 69 7a     + The optimiz
2b37e 65 72 20 63 6f 64 65 20 69 6e 20 77 68 65 72 65  er code in where
2b37f 2e 63 20 28 74 68 65 20 74 68 69 6e 67 20 74 68  .c (the thing th
2b380 61 74 20 64 65 63 69 64 65 73 20 77 68 69 63 68  at decides which
2b381 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20  .        **     
2b382 69 6e 64 65 78 20 6f 72 20 69 6e 64 69 63 65 73  index or indices
2b383 20 74 6f 20 75 73 65 29 20 73 68 6f 75 6c 64 20   to use) should 
2b384 70 6c 61 63 65 20 61 20 64 69 66 66 65 72 65 6e  place a differen
2b385 74 20 70 72 69 6f 72 69 74 79 20 6f 6e 20 0a 20  t priority on . 
2b386 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 73 61         **     sa
2b387 74 69 73 66 79 69 6e 67 20 74 68 65 20 27 4f 52  tisfying the 'OR
2b388 44 45 52 20 42 59 27 20 63 6c 61 75 73 65 20 74  DER BY' clause t
2b389 68 61 6e 20 69 74 20 64 6f 65 73 20 69 6e 20 6f  han it does in o
2b38a 74 68 65 72 20 63 61 73 65 73 2e 0a 20 20 20 20  ther cases..    
2b38b 20 20 20 20 2a 2a 20 20 20 20 20 52 65 66 65 72      **     Refer
2b38c 20 74 6f 20 63 6f 64 65 20 61 6e 64 20 63 6f 6d   to code and com
2b38d 6d 65 6e 74 73 20 69 6e 20 77 68 65 72 65 2e 63  ments in where.c
2b38e 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 20 20   for details..  
2b38f 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
2b390 20 45 78 70 72 4c 69 73 74 20 2a 70 4d 69 6e 4d   ExprList *pMinM
2b391 61 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ax = 0;.        
2b392 75 38 20 66 6c 61 67 20 3d 20 6d 69 6e 4d 61 78  u8 flag = minMax
2b393 51 75 65 72 79 28 70 29 3b 0a 20 20 20 20 20 20  Query(p);.      
2b394 20 20 69 66 28 20 66 6c 61 67 20 29 7b 0a 20 20    if( flag ){.  
2b395 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2b396 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
2b397 28 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d  (p->pEList->a[0]
2b398 2e 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65  .pExpr, EP_xIsSe
2b399 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 20  lect) );.       
2b39a 20 20 20 70 4d 69 6e 4d 61 78 20 3d 20 73 71 6c     pMinMax = sql
2b39b 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
2b39c 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61  db, p->pEList->a
2b39d 5b 30 5d 2e 70 45 78 70 72 2d 3e 78 2e 70 4c 69  [0].pExpr->x.pLi
2b39e 73 74 2c 30 29 3b 0a 20 20 20 20 20 20 20 20 20  st,0);.         
2b39f 20 70 44 65 6c 20 3d 20 70 4d 69 6e 4d 61 78 3b   pDel = pMinMax;
2b3a0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
2b3a1 4d 69 6e 4d 61 78 20 26 26 20 21 64 62 2d 3e 6d  MinMax && !db->m
2b3a2 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
2b3a3 20 20 20 20 20 20 20 20 20 20 20 70 4d 69 6e 4d             pMinM
2b3a4 61 78 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64  ax->a[0].sortOrd
2b3a5 65 72 20 3d 20 66 6c 61 67 21 3d 57 48 45 52 45  er = flag!=WHERE
2b3a6 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 20 3f 31 3a  _ORDERBY_MIN ?1:
2b3a7 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  0;.            p
2b3a8 4d 69 6e 4d 61 78 2d 3e 61 5b 30 5d 2e 70 45 78  MinMax->a[0].pEx
2b3a9 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4c 55  pr->op = TK_COLU
2b3aa 4d 4e 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  MN;.          }.
2b3ab 20 20 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20          }.  .   
2b3ac 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 73       /* This cas
2b3ad 65 20 72 75 6e 73 20 69 66 20 74 68 65 20 61 67  e runs if the ag
2b3ae 67 72 65 67 61 74 65 20 68 61 73 20 6e 6f 20 47  gregate has no G
2b3af 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e 20  ROUP BY clause. 
2b3b0 20 54 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20   The.        ** 
2b3b1 70 72 6f 63 65 73 73 69 6e 67 20 69 73 20 6d 75  processing is mu
2b3b2 63 68 20 73 69 6d 70 6c 65 72 20 73 69 6e 63 65  ch simpler since
2b3b3 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61   there is only a
2b3b4 20 73 69 6e 67 6c 65 20 72 6f 77 0a 20 20 20 20   single row.    
2b3b5 20 20 20 20 2a 2a 20 6f 66 20 6f 75 74 70 75 74      ** of output
2b3b6 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
2b3b7 20 20 20 20 20 72 65 73 65 74 41 63 63 75 6d 75       resetAccumu
2b3b8 6c 61 74 6f 72 28 70 50 61 72 73 65 2c 20 26 73  lator(pParse, &s
2b3b9 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  AggInfo);.      
2b3ba 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74    pWInfo = sqlit
2b3bb 65 33 57 68 65 72 65 42 65 67 69 6e 28 70 50 61  e3WhereBegin(pPa
2b3bc 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70  rse, pTabList, p
2b3bd 57 68 65 72 65 2c 20 26 70 4d 69 6e 4d 61 78 2c  Where, &pMinMax,
2b3be 20 66 6c 61 67 29 3b 0a 20 20 20 20 20 20 20 20   flag);.        
2b3bf 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29 7b  if( pWInfo==0 ){
2b3c0 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2b3c1 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
2b3c2 28 64 62 2c 20 70 44 65 6c 29 3b 0a 20 20 20 20  (db, pDel);.    
2b3c3 20 20 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63        goto selec
2b3c4 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 20 20 7d  t_end;.        }
2b3c5 0a 20 20 20 20 20 20 20 20 75 70 64 61 74 65 41  .        updateA
2b3c6 63 63 75 6d 75 6c 61 74 6f 72 28 70 50 61 72 73  ccumulator(pPars
2b3c7 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20  e, &sAggInfo);. 
2b3c8 20 20 20 20 20 20 20 69 66 28 20 21 70 4d 69 6e         if( !pMin
2b3c9 4d 61 78 20 26 26 20 66 6c 61 67 20 29 7b 0a 20  Max && flag ){. 
2b3ca 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2b3cb 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b3cc 5f 47 6f 74 6f 2c 20 30 2c 20 70 57 49 6e 66 6f  _Goto, 0, pWInfo
2b3cd 2d 3e 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20  ->iBreak);.     
2b3ce 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
2b3cf 28 28 76 2c 20 22 25 73 28 29 20 62 79 20 69 6e  ((v, "%s() by in
2b3d0 64 65 78 22 2c 0a 20 20 20 20 20 20 20 20 20 20  dex",.          
2b3d1 20 20 20 20 20 20 28 66 6c 61 67 3d 3d 57 48 45        (flag==WHE
2b3d2 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 3f 22  RE_ORDERBY_MIN?"
2b3d3 6d 69 6e 22 3a 22 6d 61 78 22 29 29 29 3b 0a 20  min":"max")));. 
2b3d4 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2b3d5 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64   sqlite3WhereEnd
2b3d6 28 70 57 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  (pWInfo);.      
2b3d7 20 20 66 69 6e 61 6c 69 7a 65 41 67 67 46 75 6e    finalizeAggFun
2b3d8 63 74 69 6f 6e 73 28 70 50 61 72 73 65 2c 20 26  ctions(pParse, &
2b3d9 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20  sAggInfo);.     
2b3da 20 7d 0a 0a 20 20 20 20 20 20 70 4f 72 64 65 72   }..      pOrder
2b3db 42 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71  By = 0;.      sq
2b3dc 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65  lite3ExprIfFalse
2b3dd 28 70 50 61 72 73 65 2c 20 70 48 61 76 69 6e 67  (pParse, pHaving
2b3de 2c 20 61 64 64 72 45 6e 64 2c 20 53 51 4c 49 54  , addrEnd, SQLIT
2b3df 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20  E_JUMPIFNULL);. 
2b3e0 20 20 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72       selectInner
2b3e1 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20  Loop(pParse, p, 
2b3e2 70 2d 3e 70 45 4c 69 73 74 2c 20 30 2c 20 30 2c  p->pEList, 0, 0,
2b3e3 20 30 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20 20   0, -1, .       
2b3e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
2b3e5 44 65 73 74 2c 20 61 64 64 72 45 6e 64 2c 20 61  Dest, addrEnd, a
2b3e6 64 64 72 45 6e 64 29 3b 0a 20 20 20 20 20 20 73  ddrEnd);.      s
2b3e7 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
2b3e8 6c 65 74 65 28 64 62 2c 20 70 44 65 6c 29 3b 0a  lete(db, pDel);.
2b3e9 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2b3ea 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
2b3eb 6c 28 76 2c 20 61 64 64 72 45 6e 64 29 3b 0a 20  l(v, addrEnd);. 
2b3ec 20 20 20 0a 20 20 7d 20 2f 2a 20 65 6e 64 69 66     .  } /* endif
2b3ed 20 61 67 67 72 65 67 61 74 65 20 71 75 65 72 79   aggregate query
2b3ee 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   */..  /* If the
2b3ef 72 65 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42  re is an ORDER B
2b3f0 59 20 63 6c 61 75 73 65 2c 20 74 68 65 6e 20 77  Y clause, then w
2b3f1 65 20 6e 65 65 64 20 74 6f 20 73 6f 72 74 20 74  e need to sort t
2b3f2 68 65 20 72 65 73 75 6c 74 73 0a 20 20 2a 2a 20  he results.  ** 
2b3f3 61 6e 64 20 73 65 6e 64 20 74 68 65 6d 20 74 6f  and send them to
2b3f4 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e   the callback on
2b3f5 65 20 62 79 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20  e by one..  */. 
2b3f6 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b   if( pOrderBy ){
2b3f7 0a 20 20 20 20 67 65 6e 65 72 61 74 65 53 6f 72  .    generateSor
2b3f8 74 54 61 69 6c 28 70 50 61 72 73 65 2c 20 70 2c  tTail(pParse, p,
2b3f9 20 76 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70   v, pEList->nExp
2b3fa 72 2c 20 70 44 65 73 74 29 3b 0a 20 20 7d 0a 0a  r, pDest);.  }..
2b3fb 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
2b3fc 6f 20 73 6b 69 70 20 74 68 69 73 20 71 75 65 72  o skip this quer
2b3fd 79 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  y.  */.  sqlite3
2b3fe 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
2b3ff 28 76 2c 20 69 45 6e 64 29 3b 0a 0a 20 20 2f 2a  (v, iEnd);..  /*
2b400 20 54 68 65 20 53 45 4c 45 43 54 20 77 61 73 20   The SELECT was 
2b401 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 64  successfully cod
2b402 65 64 2e 20 20 20 53 65 74 20 74 68 65 20 72 65  ed.   Set the re
2b403 74 75 72 6e 20 63 6f 64 65 20 74 6f 20 30 0a 20  turn code to 0. 
2b404 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74 65 20   ** to indicate 
2b405 6e 6f 20 65 72 72 6f 72 73 2e 0a 20 20 2a 2f 0a  no errors..  */.
2b406 20 20 72 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20    rc = 0;..  /* 
2b407 43 6f 6e 74 72 6f 6c 20 6a 75 6d 70 73 20 74 6f  Control jumps to
2b408 20 68 65 72 65 20 69 66 20 61 6e 20 65 72 72 6f   here if an erro
2b409 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  r is encountered
2b40a 20 61 62 6f 76 65 2c 20 6f 72 20 75 70 6f 6e 0a   above, or upon.
2b40b 20 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20    ** successful 
2b40c 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 53 45  coding of the SE
2b40d 4c 45 43 54 2e 0a 20 20 2a 2f 0a 73 65 6c 65 63  LECT..  */.selec
2b40e 74 5f 65 6e 64 3a 0a 0a 20 20 2f 2a 20 49 64 65  t_end:..  /* Ide
2b40f 6e 74 69 66 79 20 63 6f 6c 75 6d 6e 20 6e 61 6d  ntify column nam
2b410 65 73 20 69 66 20 72 65 73 75 6c 74 73 20 6f 66  es if results of
2b411 20 74 68 65 20 53 45 4c 45 43 54 20 61 72 65 20   the SELECT are 
2b412 74 6f 20 62 65 20 6f 75 74 70 75 74 2e 0a 20 20  to be output..  
2b413 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  */.  if( rc==SQL
2b414 49 54 45 5f 4f 4b 20 26 26 20 70 44 65 73 74 2d  ITE_OK && pDest-
2b415 3e 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70  >eDest==SRT_Outp
2b416 75 74 20 29 7b 0a 20 20 20 20 67 65 6e 65 72 61  ut ){.    genera
2b417 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50  teColumnNames(pP
2b418 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20  arse, pTabList, 
2b419 70 45 4c 69 73 74 29 3b 0a 20 20 7d 0a 0a 20 20  pEList);.  }..  
2b41a 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2b41b 2c 20 73 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 29  , sAggInfo.aCol)
2b41c 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
2b41d 65 28 64 62 2c 20 73 41 67 67 49 6e 66 6f 2e 61  e(db, sAggInfo.a
2b41e 46 75 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e 20  Func);.  return 
2b41f 72 63 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e  rc;.}..#if defin
2b420 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
2b421 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./*.************
2b422 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b423 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b424 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b425 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b426 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ***.** The follo
2b427 77 69 6e 67 20 63 6f 64 65 20 69 73 20 75 73 65  wing code is use
2b428 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e  d for testing an
2b429 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79  d debugging only
2b42a 2e 20 20 54 68 65 20 63 6f 64 65 0a 2a 2a 20 74  .  The code.** t
2b42b 68 61 74 20 66 6f 6c 6c 6f 77 73 20 64 6f 65 73  hat follows does
2b42c 20 6e 6f 74 20 61 70 70 65 61 72 20 69 6e 20 6e   not appear in n
2b42d 6f 72 6d 61 6c 20 62 75 69 6c 64 73 2e 0a 2a 2a  ormal builds..**
2b42e 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
2b42f 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 70  es are used to p
2b430 72 69 6e 74 20 6f 75 74 20 74 68 65 20 63 6f 6e  rint out the con
2b431 74 65 6e 74 20 6f 66 20 61 6c 6c 20 6f 72 20 70  tent of all or p
2b432 61 72 74 20 6f 66 20 61 20 0a 2a 2a 20 70 61 72  art of a .** par
2b433 73 65 20 73 74 72 75 63 74 75 72 65 73 20 73 75  se structures su
2b434 63 68 20 61 73 20 53 65 6c 65 63 74 20 6f 72 20  ch as Select or 
2b435 45 78 70 72 2e 20 20 53 75 63 68 20 70 72 69 6e  Expr.  Such prin
2b436 74 6f 75 74 73 20 61 72 65 20 75 73 65 66 75 6c  touts are useful
2b437 0a 2a 2a 20 66 6f 72 20 68 65 6c 70 69 6e 67 20  .** for helping 
2b438 74 6f 20 75 6e 64 65 72 73 74 61 6e 64 20 77 68  to understand wh
2b439 61 74 20 69 73 20 68 61 70 70 65 6e 69 6e 67 20  at is happening 
2b43a 69 6e 73 69 64 65 20 74 68 65 20 63 6f 64 65 20  inside the code 
2b43b 67 65 6e 65 72 61 74 6f 72 0a 2a 2a 20 64 75 72  generator.** dur
2b43c 69 6e 67 20 74 68 65 20 65 78 65 63 75 74 69 6f  ing the executio
2b43d 6e 20 6f 66 20 63 6f 6d 70 6c 65 78 20 53 45 4c  n of complex SEL
2b43e 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  ECT statements..
2b43f 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  **.** These rout
2b440 69 6e 65 20 61 72 65 20 6e 6f 74 20 63 61 6c 6c  ine are not call
2b441 65 64 20 61 6e 79 77 68 65 72 65 20 66 72 6f 6d  ed anywhere from
2b442 20 77 69 74 68 69 6e 20 74 68 65 20 6e 6f 72 6d   within the norm
2b443 61 6c 0a 2a 2a 20 63 6f 64 65 20 62 61 73 65 2e  al.** code base.
2b444 20 20 54 68 65 6e 20 61 72 65 20 69 6e 74 65 6e    Then are inten
2b445 64 65 64 20 74 6f 20 62 65 20 63 61 6c 6c 65 64  ded to be called
2b446 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65   from within the
2b447 20 64 65 62 75 67 67 65 72 0a 2a 2a 20 6f 72 20   debugger.** or 
2b448 66 72 6f 6d 20 74 65 6d 70 6f 72 61 72 79 20 22  from temporary "
2b449 70 72 69 6e 74 66 22 20 73 74 61 74 65 6d 65 6e  printf" statemen
2b44a 74 73 20 69 6e 73 65 72 74 65 64 20 66 6f 72 20  ts inserted for 
2b44b 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2f 0a 53 51  debugging..*/.SQ
2b44c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2b44d 64 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78  d sqlite3PrintEx
2b44e 70 72 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 69  pr(Expr *p){.  i
2b44f 66 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  f( !ExprHasPrope
2b450 72 74 79 28 70 2c 20 45 50 5f 49 6e 74 56 61 6c  rty(p, EP_IntVal
2b451 75 65 29 20 26 26 20 70 2d 3e 75 2e 7a 54 6f 6b  ue) && p->u.zTok
2b452 65 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  en ){.    sqlite
2b453 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 28 25  3DebugPrintf("(%
2b454 73 22 2c 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29  s", p->u.zToken)
2b455 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
2b456 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2b457 66 28 22 28 25 64 22 2c 20 70 2d 3e 6f 70 29 3b  f("(%d", p->op);
2b458 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4c  .  }.  if( p->pL
2b459 65 66 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  eft ){.    sqlit
2b45a 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 20  e3DebugPrintf(" 
2b45b 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50  ");.    sqlite3P
2b45c 72 69 6e 74 45 78 70 72 28 70 2d 3e 70 4c 65 66  rintExpr(p->pLef
2b45d 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  t);.  }.  if( p-
2b45e 3e 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 73  >pRight ){.    s
2b45f 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2b460 66 28 22 20 22 29 3b 0a 20 20 20 20 73 71 6c 69  f(" ");.    sqli
2b461 74 65 33 50 72 69 6e 74 45 78 70 72 28 70 2d 3e  te3PrintExpr(p->
2b462 70 52 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20 73  pRight);.  }.  s
2b463 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2b464 66 28 22 29 22 29 3b 0a 7d 0a 53 51 4c 49 54 45  f(")");.}.SQLITE
2b465 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2b466 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72 4c 69  lite3PrintExprLi
2b467 73 74 28 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  st(ExprList *pLi
2b468 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  st){.  int i;.  
2b469 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74  for(i=0; i<pList
2b46a 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20  ->nExpr; i++){. 
2b46b 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45     sqlite3PrintE
2b46c 78 70 72 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  xpr(pList->a[i].
2b46d 70 45 78 70 72 29 3b 0a 20 20 20 20 69 66 28 20  pExpr);.    if( 
2b46e 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31  i<pList->nExpr-1
2b46f 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2b470 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 2c 20  3DebugPrintf(", 
2b471 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  ");.    }.  }.}.
2b472 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2b473 6f 69 64 20 73 71 6c 69 74 65 33 50 72 69 6e 74  oid sqlite3Print
2b474 53 65 6c 65 63 74 28 53 65 6c 65 63 74 20 2a 70  Select(Select *p
2b475 2c 20 69 6e 74 20 69 6e 64 65 6e 74 29 7b 0a 20  , int indent){. 
2b476 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2b477 6e 74 66 28 22 25 2a 73 53 45 4c 45 43 54 28 25  ntf("%*sSELECT(%
2b478 70 29 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22  p) ", indent, ""
2b479 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 50  , p);.  sqlite3P
2b47a 72 69 6e 74 45 78 70 72 4c 69 73 74 28 70 2d 3e  rintExprList(p->
2b47b 70 45 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74  pEList);.  sqlit
2b47c 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 5c  e3DebugPrintf("\
2b47d 6e 22 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 53  n");.  if( p->pS
2b47e 72 63 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  rc ){.    char *
2b47f 7a 50 72 65 66 69 78 3b 0a 20 20 20 20 69 6e 74  zPrefix;.    int
2b480 20 69 3b 0a 20 20 20 20 7a 50 72 65 66 69 78 20   i;.    zPrefix 
2b481 3d 20 22 46 52 4f 4d 22 3b 0a 20 20 20 20 66 6f  = "FROM";.    fo
2b482 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 70 53 72 63  r(i=0; i<p->pSrc
2b483 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20  ->nSrc; i++){.  
2b484 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69      struct SrcLi
2b485 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d  st_item *pItem =
2b486 20 26 70 2d 3e 70 53 72 63 2d 3e 61 5b 69 5d 3b   &p->pSrc->a[i];
2b487 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65  .      sqlite3De
2b488 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73 20 22  bugPrintf("%*s "
2b489 2c 20 69 6e 64 65 6e 74 2b 36 2c 20 7a 50 72 65  , indent+6, zPre
2b48a 66 69 78 29 3b 0a 20 20 20 20 20 20 7a 50 72 65  fix);.      zPre
2b48b 66 69 78 20 3d 20 22 22 3b 0a 20 20 20 20 20 20  fix = "";.      
2b48c 69 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65  if( pItem->pSele
2b48d 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ct ){.        sq
2b48e 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
2b48f 28 22 28 5c 6e 22 29 3b 0a 20 20 20 20 20 20 20  ("(\n");.       
2b490 20 73 71 6c 69 74 65 33 50 72 69 6e 74 53 65 6c   sqlite3PrintSel
2b491 65 63 74 28 70 49 74 65 6d 2d 3e 70 53 65 6c 65  ect(pItem->pSele
2b492 63 74 2c 20 69 6e 64 65 6e 74 2b 31 30 29 3b 0a  ct, indent+10);.
2b493 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
2b494 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73 29  ebugPrintf("%*s)
2b495 22 2c 20 69 6e 64 65 6e 74 2b 38 2c 20 22 22 29  ", indent+8, "")
2b496 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
2b497 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29  ( pItem->zName )
2b498 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2b499 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 25 73  3DebugPrintf("%s
2b49a 22 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29  ", pItem->zName)
2b49b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2b49c 69 66 28 20 70 49 74 65 6d 2d 3e 70 54 61 62 20  if( pItem->pTab 
2b49d 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2b49e 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 28  e3DebugPrintf("(
2b49f 74 61 62 6c 65 3a 20 25 73 29 22 2c 20 70 49 74  table: %s)", pIt
2b4a0 65 6d 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  em->pTab->zName)
2b4a1 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2b4a2 69 66 28 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61  if( pItem->zAlia
2b4a3 73 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  s ){.        sql
2b4a4 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
2b4a5 22 20 41 53 20 25 73 22 2c 20 70 49 74 65 6d 2d  " AS %s", pItem-
2b4a6 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 20 20 20 20  >zAlias);.      
2b4a7 7d 0a 20 20 20 20 20 20 69 66 28 20 69 3c 70 2d  }.      if( i<p-
2b4a8 3e 70 53 72 63 2d 3e 6e 53 72 63 2d 31 20 29 7b  >pSrc->nSrc-1 ){
2b4a9 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2b4aa 44 65 62 75 67 50 72 69 6e 74 66 28 22 2c 22 29  DebugPrintf(",")
2b4ab 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2b4ac 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
2b4ad 74 66 28 22 5c 6e 22 29 3b 0a 20 20 20 20 7d 0a  tf("\n");.    }.
2b4ae 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 57 68    }.  if( p->pWh
2b4af 65 72 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ere ){.    sqlit
2b4b0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 25  e3DebugPrintf("%
2b4b1 2a 73 20 57 48 45 52 45 20 22 2c 20 69 6e 64 65  *s WHERE ", inde
2b4b2 6e 74 2c 20 22 22 29 3b 0a 20 20 20 20 73 71 6c  nt, "");.    sql
2b4b3 69 74 65 33 50 72 69 6e 74 45 78 70 72 28 70 2d  ite3PrintExpr(p-
2b4b4 3e 70 57 68 65 72 65 29 3b 0a 20 20 20 20 73 71  >pWhere);.    sq
2b4b5 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
2b4b6 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 20 20 69 66  ("\n");.  }.  if
2b4b7 28 20 70 2d 3e 70 47 72 6f 75 70 42 79 20 29 7b  ( p->pGroupBy ){
2b4b8 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75  .    sqlite3Debu
2b4b9 67 50 72 69 6e 74 66 28 22 25 2a 73 20 47 52 4f  gPrintf("%*s GRO
2b4ba 55 50 20 42 59 20 22 2c 20 69 6e 64 65 6e 74 2c  UP BY ", indent,
2b4bb 20 22 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   "");.    sqlite
2b4bc 33 50 72 69 6e 74 45 78 70 72 4c 69 73 74 28 70  3PrintExprList(p
2b4bd 2d 3e 70 47 72 6f 75 70 42 79 29 3b 0a 20 20 20  ->pGroupBy);.   
2b4be 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2b4bf 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 20  ntf("\n");.  }. 
2b4c0 20 69 66 28 20 70 2d 3e 70 48 61 76 69 6e 67 20   if( p->pHaving 
2b4c1 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  ){.    sqlite3De
2b4c2 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73 20 48  bugPrintf("%*s H
2b4c3 41 56 49 4e 47 20 22 2c 20 69 6e 64 65 6e 74 2c  AVING ", indent,
2b4c4 20 22 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   "");.    sqlite
2b4c5 33 50 72 69 6e 74 45 78 70 72 28 70 2d 3e 70 48  3PrintExpr(p->pH
2b4c6 61 76 69 6e 67 29 3b 0a 20 20 20 20 73 71 6c 69  aving);.    sqli
2b4c7 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
2b4c8 5c 6e 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  \n");.  }.  if( 
2b4c9 70 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  p->pOrderBy ){. 
2b4ca 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
2b4cb 72 69 6e 74 66 28 22 25 2a 73 20 4f 52 44 45 52  rintf("%*s ORDER
2b4cc 20 42 59 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22   BY ", indent, "
2b4cd 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50  ");.    sqlite3P
2b4ce 72 69 6e 74 45 78 70 72 4c 69 73 74 28 70 2d 3e  rintExprList(p->
2b4cf 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 20 20 73  pOrderBy);.    s
2b4d0 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2b4d1 66 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 7d 0a 2f  f("\n");.  }.}./
2b4d2 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 73 74 72  * End of the str
2b4d3 75 63 74 75 72 65 20 64 65 62 75 67 20 70 72 69  ucture debug pri
2b4d4 6e 74 69 6e 67 20 63 6f 64 65 0a 2a 2a 2a 2a 2a  nting code.*****
2b4d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b4d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b4d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b4d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b4d9 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66  ********/.#endif
2b4da 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49   /* defined(SQLI
2b4db 54 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 66 69  TE_TEST) || defi
2b4dc 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
2b4dd 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
2b4de 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 65 6c  ***** End of sel
2b4df 65 63 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ect.c **********
2b4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b4e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b4e2 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
2b4e3 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
2b4e4 20 74 61 62 6c 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a   table.c *******
2b4e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b4e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b4e7 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
2b4e8 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
2b4e9 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
2b4ea 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
2b4eb 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
2b4ec 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
2b4ed 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
2b4ee 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
2b4ef 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
2b4f0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
2b4f1 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
2b4f2 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
2b4f3 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
2b4f4 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
2b4f5 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
2b4f6 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
2b4f7 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
2b4f8 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
2b4f9 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
2b4fa 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
2b4fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b4fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b4fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b4fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
2b4ff 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
2b500 6e 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 67  ns the sqlite3_g
2b501 65 74 5f 74 61 62 6c 65 28 29 20 61 6e 64 20 73  et_table() and s
2b502 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c  qlite3_free_tabl
2b503 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  e().** interface
2b504 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 68 65 73   routines.  Thes
2b505 65 20 61 72 65 20 6a 75 73 74 20 77 72 61 70 70  e are just wrapp
2b506 65 72 73 20 61 72 6f 75 6e 64 20 74 68 65 20 6d  ers around the m
2b507 61 69 6e 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  ain.** interface
2b508 20 72 6f 75 74 69 6e 65 20 6f 66 20 73 71 6c 69   routine of sqli
2b509 74 65 33 5f 65 78 65 63 28 29 2e 0a 2a 2a 0a 2a  te3_exec()..**.*
2b50a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
2b50b 20 61 72 65 20 69 6e 20 61 20 73 65 70 61 72 61   are in a separa
2b50c 74 65 20 66 69 6c 65 73 20 73 6f 20 74 68 61 74  te files so that
2b50d 20 74 68 65 79 20 77 69 6c 6c 20 6e 6f 74 20 62   they will not b
2b50e 65 20 6c 69 6e 6b 65 64 0a 2a 2a 20 69 66 20 74  e linked.** if t
2b50f 68 65 79 20 61 72 65 20 6e 6f 74 20 75 73 65 64  hey are not used
2b510 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 74 61 62  ..**.** $Id: tab
2b511 6c 65 2e 63 2c 76 20 31 2e 34 30 20 32 30 30 39  le.c,v 1.40 2009
2b512 2f 30 34 2f 31 30 20 31 34 3a 32 38 3a 30 30 20  /04/10 14:28:00 
2b513 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69  drh Exp $.*/..#i
2b514 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2b515 54 5f 47 45 54 5f 54 41 42 4c 45 0a 0a 2f 2a 0a  T_GET_TABLE../*.
2b516 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72  ** This structur
2b517 65 20 69 73 20 75 73 65 64 20 74 6f 20 70 61 73  e is used to pas
2b518 73 20 64 61 74 61 20 66 72 6f 6d 20 73 71 6c 69  s data from sqli
2b519 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20  te3_get_table() 
2b51a 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68  through.** to th
2b51b 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  e callback funct
2b51c 69 6f 6e 20 69 73 20 75 73 65 73 20 74 6f 20 62  ion is uses to b
2b51d 75 69 6c 64 20 74 68 65 20 72 65 73 75 6c 74 2e  uild the result.
2b51e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
2b51f 63 74 20 54 61 62 52 65 73 75 6c 74 20 7b 0a 20  ct TabResult {. 
2b520 20 63 68 61 72 20 2a 2a 61 7a 52 65 73 75 6c 74   char **azResult
2b521 3b 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74  ;   /* Accumulat
2b522 65 64 20 6f 75 74 70 75 74 20 2a 2f 0a 20 20 63  ed output */.  c
2b523 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20  har *zErrMsg;   
2b524 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73 61    /* Error messa
2b525 67 65 20 74 65 78 74 2c 20 69 66 20 61 6e 20 65  ge text, if an e
2b526 72 72 6f 72 20 6f 63 63 75 72 73 20 2a 2f 0a 20  rror occurs */. 
2b527 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20   int nAlloc;    
2b528 20 20 20 20 2f 2a 20 53 6c 6f 74 73 20 61 6c 6c      /* Slots all
2b529 6f 63 61 74 65 64 20 66 6f 72 20 61 7a 52 65 73  ocated for azRes
2b52a 75 6c 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e  ult[] */.  int n
2b52b 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Row;          /*
2b52c 20 4e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   Number of rows 
2b52d 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 2a 2f  in the result */
2b52e 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20  .  int nColumn; 
2b52f 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2b530 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
2b531 65 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 69 6e  e result */.  in
2b532 74 20 6e 44 61 74 61 3b 20 20 20 20 20 20 20 20  t nData;        
2b533 20 2f 2a 20 53 6c 6f 74 73 20 75 73 65 64 20 69   /* Slots used i
2b534 6e 20 61 7a 52 65 73 75 6c 74 5b 5d 2e 20 20 28  n azResult[].  (
2b535 6e 52 6f 77 2b 31 29 2a 6e 43 6f 6c 75 6d 6e 20  nRow+1)*nColumn 
2b536 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
2b537 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
2b538 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 71 6c 69  n code from sqli
2b539 74 65 33 5f 65 78 65 63 28 29 20 2a 2f 0a 7d 20  te3_exec() */.} 
2b53a 54 61 62 52 65 73 75 6c 74 3b 0a 0a 2f 2a 0a 2a  TabResult;../*.*
2b53b 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
2b53c 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f  s called once fo
2b53d 72 20 65 61 63 68 20 72 6f 77 20 69 6e 20 74 68  r each row in th
2b53e 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 2e 20  e result table. 
2b53f 20 49 74 73 20 6a 6f 62 0a 2a 2a 20 69 73 20 74   Its job.** is t
2b540 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 54 61  o fill in the Ta
2b541 62 52 65 73 75 6c 74 20 73 74 72 75 63 74 75 72  bResult structur
2b542 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2c  e appropriately,
2b543 20 61 6c 6c 6f 63 61 74 69 6e 67 20 6e 65 77 0a   allocating new.
2b544 2a 2a 20 6d 65 6d 6f 72 79 20 61 73 20 6e 65 63  ** memory as nec
2b545 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69  essary..*/.stati
2b546 63 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65  c int sqlite3_ge
2b547 74 5f 74 61 62 6c 65 5f 63 62 28 76 6f 69 64 20  t_table_cb(void 
2b548 2a 70 41 72 67 2c 20 69 6e 74 20 6e 43 6f 6c 2c  *pArg, int nCol,
2b549 20 63 68 61 72 20 2a 2a 61 72 67 76 2c 20 63 68   char **argv, ch
2b54a 61 72 20 2a 2a 63 6f 6c 76 29 7b 0a 20 20 54 61  ar **colv){.  Ta
2b54b 62 52 65 73 75 6c 74 20 2a 70 20 3d 20 28 54 61  bResult *p = (Ta
2b54c 62 52 65 73 75 6c 74 2a 29 70 41 72 67 3b 20 20  bResult*)pArg;  
2b54d 2f 2a 20 52 65 73 75 6c 74 20 61 63 63 75 6d 75  /* Result accumu
2b54e 6c 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  lator */.  int n
2b54f 65 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  eed;            
2b550 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2b551 53 6c 6f 74 73 20 6e 65 65 64 65 64 20 69 6e 20  Slots needed in 
2b552 70 2d 3e 61 7a 52 65 73 75 6c 74 5b 5d 20 2a 2f  p->azResult[] */
2b553 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
2b554 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b555 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
2b556 6e 74 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a  nter */.  char *
2b557 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  z;              
2b558 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
2b559 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6f   single column o
2b55a 66 20 72 65 73 75 6c 74 20 2a 2f 0a 0a 20 20 2f  f result */..  /
2b55b 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 72  * Make sure ther
2b55c 65 20 69 73 20 65 6e 6f 75 67 68 20 73 70 61 63  e is enough spac
2b55d 65 20 69 6e 20 70 2d 3e 61 7a 52 65 73 75 6c 74  e in p->azResult
2b55e 20 74 6f 20 68 6f 6c 64 20 65 76 65 72 79 74 68   to hold everyth
2b55f 69 6e 67 0a 20 20 2a 2a 20 77 65 20 6e 65 65 64  ing.  ** we need
2b560 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 66 72 6f   to remember fro
2b561 6d 20 74 68 69 73 20 69 6e 76 6f 63 61 74 69 6f  m this invocatio
2b562 6e 20 6f 66 20 74 68 65 20 63 61 6c 6c 62 61 63  n of the callbac
2b563 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  k..  */.  if( p-
2b564 3e 6e 52 6f 77 3d 3d 30 20 26 26 20 61 72 67 76  >nRow==0 && argv
2b565 21 3d 30 20 29 7b 0a 20 20 20 20 6e 65 65 64 20  !=0 ){.    need 
2b566 3d 20 6e 43 6f 6c 2a 32 3b 0a 20 20 7d 65 6c 73  = nCol*2;.  }els
2b567 65 7b 0a 20 20 20 20 6e 65 65 64 20 3d 20 6e 43  e{.    need = nC
2b568 6f 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  ol;.  }.  if( p-
2b569 3e 6e 44 61 74 61 20 2b 20 6e 65 65 64 20 3e 20  >nData + need > 
2b56a 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20  p->nAlloc ){.   
2b56b 20 63 68 61 72 20 2a 2a 61 7a 4e 65 77 3b 0a 20   char **azNew;. 
2b56c 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70     p->nAlloc = p
2b56d 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b 20 6e 65 65  ->nAlloc*2 + nee
2b56e 64 3b 0a 20 20 20 20 61 7a 4e 65 77 20 3d 20 73  d;.    azNew = s
2b56f 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 20  qlite3_realloc( 
2b570 70 2d 3e 61 7a 52 65 73 75 6c 74 2c 20 73 69 7a  p->azResult, siz
2b571 65 6f 66 28 63 68 61 72 2a 29 2a 70 2d 3e 6e 41  eof(char*)*p->nA
2b572 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 69 66 28 20  lloc );.    if( 
2b573 61 7a 4e 65 77 3d 3d 30 20 29 20 67 6f 74 6f 20  azNew==0 ) goto 
2b574 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a 20  malloc_failed;. 
2b575 20 20 20 70 2d 3e 61 7a 52 65 73 75 6c 74 20 3d     p->azResult =
2b576 20 61 7a 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 2f   azNew;.  }..  /
2b577 2a 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65  * If this is the
2b578 20 66 69 72 73 74 20 72 6f 77 2c 20 74 68 65 6e   first row, then
2b579 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65 78 74   generate an ext
2b57a 72 61 20 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e  ra row containin
2b57b 67 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65 73  g.  ** the names
2b57c 20 6f 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 2e   of all columns.
2b57d 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 6e  .  */.  if( p->n
2b57e 52 6f 77 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d  Row==0 ){.    p-
2b57f 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 6e 43 6f 6c 3b  >nColumn = nCol;
2b580 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
2b581 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
2b582 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70    z = sqlite3_mp
2b583 72 69 6e 74 66 28 22 25 73 22 2c 20 63 6f 6c 76  rintf("%s", colv
2b584 5b 69 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  [i]);.      if( 
2b585 7a 3d 3d 30 20 29 20 67 6f 74 6f 20 6d 61 6c 6c  z==0 ) goto mall
2b586 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 20  oc_failed;.     
2b587 20 70 2d 3e 61 7a 52 65 73 75 6c 74 5b 70 2d 3e   p->azResult[p->
2b588 6e 44 61 74 61 2b 2b 5d 20 3d 20 7a 3b 0a 20 20  nData++] = z;.  
2b589 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
2b58a 70 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d 6e 43 6f 6c  p->nColumn!=nCol
2b58b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
2b58c 66 72 65 65 28 70 2d 3e 7a 45 72 72 4d 73 67 29  free(p->zErrMsg)
2b58d 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67  ;.    p->zErrMsg
2b58e 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
2b58f 74 66 28 0a 20 20 20 20 20 20 20 22 73 71 6c 69  tf(.       "sqli
2b590 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20  te3_get_table() 
2b591 63 61 6c 6c 65 64 20 77 69 74 68 20 74 77 6f 20  called with two 
2b592 6f 72 20 6d 6f 72 65 20 69 6e 63 6f 6d 70 61 74  or more incompat
2b593 69 62 6c 65 20 71 75 65 72 69 65 73 22 0a 20 20  ible queries".  
2b594 20 20 29 3b 0a 20 20 20 20 70 2d 3e 72 63 20 3d    );.    p->rc =
2b595 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
2b596 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
2b597 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 6f 76 65 72  ..  /* Copy over
2b598 20 74 68 65 20 72 6f 77 20 64 61 74 61 0a 20 20   the row data.  
2b599 2a 2f 0a 20 20 69 66 28 20 61 72 67 76 21 3d 30  */.  if( argv!=0
2b59a 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
2b59b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20   i<nCol; i++){. 
2b59c 20 20 20 20 20 69 66 28 20 61 72 67 76 5b 69 5d       if( argv[i]
2b59d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  ==0 ){.        z
2b59e 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73   = 0;.      }els
2b59f 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e  e{.        int n
2b5a0 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
2b5a1 33 30 28 61 72 67 76 5b 69 5d 29 2b 31 3b 0a 20  30(argv[i])+1;. 
2b5a2 20 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74         z = sqlit
2b5a3 65 33 5f 6d 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a  e3_malloc( n );.
2b5a4 20 20 20 20 20 20 20 20 69 66 28 20 7a 3d 3d 30          if( z==0
2b5a5 20 29 20 67 6f 74 6f 20 6d 61 6c 6c 6f 63 5f 66   ) goto malloc_f
2b5a6 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 6d  ailed;.        m
2b5a7 65 6d 63 70 79 28 7a 2c 20 61 72 67 76 5b 69 5d  emcpy(z, argv[i]
2b5a8 2c 20 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , n);.      }.  
2b5a9 20 20 20 20 70 2d 3e 61 7a 52 65 73 75 6c 74 5b      p->azResult[
2b5aa 70 2d 3e 6e 44 61 74 61 2b 2b 5d 20 3d 20 7a 3b  p->nData++] = z;
2b5ab 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 52  .    }.    p->nR
2b5ac 6f 77 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ow++;.  }.  retu
2b5ad 72 6e 20 30 3b 0a 0a 6d 61 6c 6c 6f 63 5f 66 61  rn 0;..malloc_fa
2b5ae 69 6c 65 64 3a 0a 20 20 70 2d 3e 72 63 20 3d 20  iled:.  p->rc = 
2b5af 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
2b5b0 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a  return 1;.}../*.
2b5b1 2a 2a 20 51 75 65 72 79 20 74 68 65 20 64 61 74  ** Query the dat
2b5b2 61 62 61 73 65 2e 20 20 42 75 74 20 69 6e 73 74  abase.  But inst
2b5b3 65 61 64 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 20  ead of invoking 
2b5b4 61 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 65  a callback for e
2b5b5 61 63 68 20 72 6f 77 2c 0a 2a 2a 20 6d 61 6c 6c  ach row,.** mall
2b5b6 6f 63 28 29 20 66 6f 72 20 73 70 61 63 65 20 74  oc() for space t
2b5b7 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c  o hold the resul
2b5b8 74 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65  t and return the
2b5b9 20 65 6e 74 69 72 65 20 72 65 73 75 6c 74 73 0a   entire results.
2b5ba 2a 2a 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75  ** at the conclu
2b5bb 73 69 6f 6e 20 6f 66 20 74 68 65 20 63 61 6c 6c  sion of the call
2b5bc 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75  ..**.** The resu
2b5bd 6c 74 20 74 68 61 74 20 69 73 20 77 72 69 74 74  lt that is writt
2b5be 65 6e 20 74 6f 20 2a 2a 2a 70 61 7a 52 65 73 75  en to ***pazResu
2b5bf 6c 74 20 69 73 20 68 65 6c 64 20 69 6e 20 6d 65  lt is held in me
2b5c0 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 0a 2a 2a  mory obtained.**
2b5c1 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 20   from malloc(). 
2b5c2 20 42 75 74 20 74 68 65 20 63 61 6c 6c 65 72 20   But the caller 
2b5c3 63 61 6e 6e 6f 74 20 66 72 65 65 20 74 68 69 73  cannot free this
2b5c4 20 6d 65 6d 6f 72 79 20 64 69 72 65 63 74 6c 79   memory directly
2b5c5 2e 20 20 0a 2a 2a 20 49 6e 73 74 65 61 64 2c 20  .  .** Instead, 
2b5c6 74 68 65 20 65 6e 74 69 72 65 20 74 61 62 6c 65  the entire table
2b5c7 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65   should be passe
2b5c8 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65  d to sqlite3_fre
2b5c9 65 5f 74 61 62 6c 65 28 29 20 77 68 65 6e 0a 2a  e_table() when.*
2b5ca 2a 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 70 72  * the calling pr
2b5cb 6f 63 65 64 75 72 65 20 69 73 20 66 69 6e 69 73  ocedure is finis
2b5cc 68 65 64 20 75 73 69 6e 67 20 69 74 2e 0a 2a 2f  hed using it..*/
2b5cd 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2b5ce 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
2b5cf 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e(.  sqlite3 *db
2b5d0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2b5d1 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
2b5d2 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20 53 51   on which the SQ
2b5d3 4c 20 65 78 65 63 75 74 65 73 20 2a 2f 0a 20 20  L executes */.  
2b5d4 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
2b5d5 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ,           /* T
2b5d6 68 65 20 53 51 4c 20 74 6f 20 62 65 20 65 78 65  he SQL to be exe
2b5d7 63 75 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20  cuted */.  char 
2b5d8 2a 2a 2a 70 61 7a 52 65 73 75 6c 74 2c 20 20 20  ***pazResult,   
2b5d9 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
2b5da 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65  the result table
2b5db 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a   here */.  int *
2b5dc 70 6e 52 6f 77 2c 20 20 20 20 20 20 20 20 20 20  pnRow,          
2b5dd 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
2b5de 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  the number of ro
2b5df 77 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ws in the result
2b5e0 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a   here */.  int *
2b5e1 70 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20  pnColumn,       
2b5e2 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
2b5e3 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  the number of co
2b5e4 6c 75 6d 6e 73 20 6f 66 20 72 65 73 75 6c 74 20  lumns of result 
2b5e5 68 65 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  here */.  char *
2b5e6 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20 20 20  *pzErrMsg       
2b5e7 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 65        /* Write e
2b5e8 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68 65  rror messages he
2b5e9 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  re */.){.  int r
2b5ea 63 3b 0a 20 20 54 61 62 52 65 73 75 6c 74 20 72  c;.  TabResult r
2b5eb 65 73 3b 0a 0a 20 20 2a 70 61 7a 52 65 73 75 6c  es;..  *pazResul
2b5ec 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 6e 43  t = 0;.  if( pnC
2b5ed 6f 6c 75 6d 6e 20 29 20 2a 70 6e 43 6f 6c 75 6d  olumn ) *pnColum
2b5ee 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 6e 52  n = 0;.  if( pnR
2b5ef 6f 77 20 29 20 2a 70 6e 52 6f 77 20 3d 20 30 3b  ow ) *pnRow = 0;
2b5f0 0a 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20  .  if( pzErrMsg 
2b5f1 29 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ) *pzErrMsg = 0;
2b5f2 0a 20 20 72 65 73 2e 7a 45 72 72 4d 73 67 20 3d  .  res.zErrMsg =
2b5f3 20 30 3b 0a 20 20 72 65 73 2e 6e 52 6f 77 20 3d   0;.  res.nRow =
2b5f4 20 30 3b 0a 20 20 72 65 73 2e 6e 43 6f 6c 75 6d   0;.  res.nColum
2b5f5 6e 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e 44 61  n = 0;.  res.nDa
2b5f6 74 61 20 3d 20 31 3b 0a 20 20 72 65 73 2e 6e 41  ta = 1;.  res.nA
2b5f7 6c 6c 6f 63 20 3d 20 32 30 3b 0a 20 20 72 65 73  lloc = 20;.  res
2b5f8 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  .rc = SQLITE_OK;
2b5f9 0a 20 20 72 65 73 2e 61 7a 52 65 73 75 6c 74 20  .  res.azResult 
2b5fa 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
2b5fb 28 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a 72  (sizeof(char*)*r
2b5fc 65 73 2e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 69  es.nAlloc );.  i
2b5fd 66 28 20 72 65 73 2e 61 7a 52 65 73 75 6c 74 3d  f( res.azResult=
2b5fe 3d 30 20 29 7b 0a 20 20 20 20 20 64 62 2d 3e 65  =0 ){.     db->e
2b5ff 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f  rrCode = SQLITE_
2b600 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 72 65 74 75  NOMEM;.     retu
2b601 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
2b602 0a 20 20 7d 0a 20 20 72 65 73 2e 61 7a 52 65 73  .  }.  res.azRes
2b603 75 6c 74 5b 30 5d 20 3d 20 30 3b 0a 20 20 72 63  ult[0] = 0;.  rc
2b604 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28   = sqlite3_exec(
2b605 64 62 2c 20 7a 53 71 6c 2c 20 73 71 6c 69 74 65  db, zSql, sqlite
2b606 33 5f 67 65 74 5f 74 61 62 6c 65 5f 63 62 2c 20  3_get_table_cb, 
2b607 26 72 65 73 2c 20 70 7a 45 72 72 4d 73 67 29 3b  &res, pzErrMsg);
2b608 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f  .  assert( sizeo
2b609 66 28 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 30  f(res.azResult[0
2b60a 5d 29 3e 3d 20 73 69 7a 65 6f 66 28 72 65 73 2e  ])>= sizeof(res.
2b60b 6e 44 61 74 61 29 20 29 3b 0a 20 20 72 65 73 2e  nData) );.  res.
2b60c 61 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20 53 51  azResult[0] = SQ
2b60d 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28  LITE_INT_TO_PTR(
2b60e 72 65 73 2e 6e 44 61 74 61 29 3b 0a 20 20 69 66  res.nData);.  if
2b60f 28 20 28 72 63 26 30 78 66 66 29 3d 3d 53 51 4c  ( (rc&0xff)==SQL
2b610 49 54 45 5f 41 42 4f 52 54 20 29 7b 0a 20 20 20  ITE_ABORT ){.   
2b611 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61   sqlite3_free_ta
2b612 62 6c 65 28 26 72 65 73 2e 61 7a 52 65 73 75 6c  ble(&res.azResul
2b613 74 5b 31 5d 29 3b 0a 20 20 20 20 69 66 28 20 72  t[1]);.    if( r
2b614 65 73 2e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20  es.zErrMsg ){.  
2b615 20 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67      if( pzErrMsg
2b616 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
2b617 74 65 33 5f 66 72 65 65 28 2a 70 7a 45 72 72 4d  te3_free(*pzErrM
2b618 73 67 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 7a  sg);.        *pz
2b619 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33  ErrMsg = sqlite3
2b61a 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 72 65  _mprintf("%s",re
2b61b 73 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  s.zErrMsg);.    
2b61c 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
2b61d 33 5f 66 72 65 65 28 72 65 73 2e 7a 45 72 72 4d  3_free(res.zErrM
2b61e 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  sg);.    }.    d
2b61f 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 65 73  b->errCode = res
2b620 2e 72 63 3b 20 20 2f 2a 20 41 73 73 75 6d 65 20  .rc;  /* Assume 
2b621 33 32 2d 62 69 74 20 61 73 73 69 67 6e 6d 65 6e  32-bit assignmen
2b622 74 20 69 73 20 61 74 6f 6d 69 63 20 2a 2f 0a 20  t is atomic */. 
2b623 20 20 20 72 65 74 75 72 6e 20 72 65 73 2e 72 63     return res.rc
2b624 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
2b625 66 72 65 65 28 72 65 73 2e 7a 45 72 72 4d 73 67  free(res.zErrMsg
2b626 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
2b627 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71  ITE_OK ){.    sq
2b628 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65  lite3_free_table
2b629 28 26 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31  (&res.azResult[1
2b62a 5d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72  ]);.    return r
2b62b 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 65 73  c;.  }.  if( res
2b62c 2e 6e 41 6c 6c 6f 63 3e 72 65 73 2e 6e 44 61 74  .nAlloc>res.nDat
2b62d 61 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a  a ){.    char **
2b62e 61 7a 4e 65 77 3b 0a 20 20 20 20 61 7a 4e 65 77  azNew;.    azNew
2b62f 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c   = sqlite3_reall
2b630 6f 63 28 20 72 65 73 2e 61 7a 52 65 73 75 6c 74  oc( res.azResult
2b631 2c 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a  , sizeof(char*)*
2b632 72 65 73 2e 6e 44 61 74 61 20 29 3b 0a 20 20 20  res.nData );.   
2b633 20 69 66 28 20 61 7a 4e 65 77 3d 3d 30 20 29 7b   if( azNew==0 ){
2b634 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
2b635 72 65 65 5f 74 61 62 6c 65 28 26 72 65 73 2e 61  ree_table(&res.a
2b636 7a 52 65 73 75 6c 74 5b 31 5d 29 3b 0a 20 20 20  zResult[1]);.   
2b637 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d     db->errCode =
2b638 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
2b639 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
2b63a 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
2b63b 20 20 20 20 72 65 73 2e 61 7a 52 65 73 75 6c 74      res.azResult
2b63c 20 3d 20 61 7a 4e 65 77 3b 0a 20 20 7d 0a 20 20   = azNew;.  }.  
2b63d 2a 70 61 7a 52 65 73 75 6c 74 20 3d 20 26 72 65  *pazResult = &re
2b63e 73 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 3b 0a 20  s.azResult[1];. 
2b63f 20 69 66 28 20 70 6e 43 6f 6c 75 6d 6e 20 29 20   if( pnColumn ) 
2b640 2a 70 6e 43 6f 6c 75 6d 6e 20 3d 20 72 65 73 2e  *pnColumn = res.
2b641 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69 66 28 20 70  nColumn;.  if( p
2b642 6e 52 6f 77 20 29 20 2a 70 6e 52 6f 77 20 3d 20  nRow ) *pnRow = 
2b643 72 65 73 2e 6e 52 6f 77 3b 0a 20 20 72 65 74 75  res.nRow;.  retu
2b644 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
2b645 54 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 65  This routine fre
2b646 65 73 20 74 68 65 20 73 70 61 63 65 20 74 68 65  es the space the
2b647 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62   sqlite3_get_tab
2b648 6c 65 28 29 20 6d 61 6c 6c 6f 63 65 64 2e 0a 2a  le() malloced..*
2b649 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
2b64a 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74  d sqlite3_free_t
2b64b 61 62 6c 65 28 0a 20 20 63 68 61 72 20 2a 2a 61  able(.  char **a
2b64c 7a 52 65 73 75 6c 74 20 20 20 20 20 20 20 20 20  zResult         
2b64d 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 72 65 74     /* Result ret
2b64e 75 72 6e 65 64 20 66 72 6f 6d 20 66 72 6f 6d 20  urned from from 
2b64f 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
2b650 65 28 29 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20  e() */.){.  if( 
2b651 61 7a 52 65 73 75 6c 74 20 29 7b 0a 20 20 20 20  azResult ){.    
2b652 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 20 20 61 7a  int i, n;.    az
2b653 52 65 73 75 6c 74 2d 2d 3b 0a 20 20 20 20 61 73  Result--;.    as
2b654 73 65 72 74 28 20 61 7a 52 65 73 75 6c 74 21 3d  sert( azResult!=
2b655 30 20 29 3b 0a 20 20 20 20 6e 20 3d 20 53 51 4c  0 );.    n = SQL
2b656 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 61  ITE_PTR_TO_INT(a
2b657 7a 52 65 73 75 6c 74 5b 30 5d 29 3b 0a 20 20 20  zResult[0]);.   
2b658 20 66 6f 72 28 69 3d 31 3b 20 69 3c 6e 3b 20 69   for(i=1; i<n; i
2b659 2b 2b 29 7b 20 69 66 28 20 61 7a 52 65 73 75 6c  ++){ if( azResul
2b65a 74 5b 69 5d 20 29 20 73 71 6c 69 74 65 33 5f 66  t[i] ) sqlite3_f
2b65b 72 65 65 28 61 7a 52 65 73 75 6c 74 5b 69 5d 29  ree(azResult[i])
2b65c 3b 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ; }.    sqlite3_
2b65d 66 72 65 65 28 61 7a 52 65 73 75 6c 74 29 3b 0a  free(azResult);.
2b65e 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a    }.}..#endif /*
2b65f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 45 54   SQLITE_OMIT_GET
2b660 5f 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _TABLE */../****
2b661 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
2b662 66 20 74 61 62 6c 65 2e 63 20 2a 2a 2a 2a 2a 2a  f table.c ******
2b663 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b664 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b665 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
2b666 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
2b667 20 66 69 6c 65 20 74 72 69 67 67 65 72 2e 63 20   file trigger.c 
2b668 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b669 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b66a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
2b66b 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
2b66c 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
2b66d 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
2b66e 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
2b66f 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
2b670 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
2b671 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
2b672 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
2b673 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
2b674 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
2b675 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
2b676 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
2b677 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
2b678 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
2b679 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
2b67a 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
2b67b 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
2b67c 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
2b67d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b67e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b67f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
2b681 2a 2a 0a 2a 2a 20 24 49 64 3a 20 74 72 69 67 67  **.** $Id: trigg
2b682 65 72 2e 63 2c 76 20 31 2e 31 34 33 20 32 30 30  er.c,v 1.143 200
2b683 39 2f 30 38 2f 31 30 20 30 33 3a 35 37 3a 35 38  9/08/10 03:57:58
2b684 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a   shane Exp $.*/.
2b685 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2b686 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 2f 2a 0a  OMIT_TRIGGER./*.
2b687 2a 2a 20 44 65 6c 65 74 65 20 61 20 6c 69 6e 6b  ** Delete a link
2b688 65 64 20 6c 69 73 74 20 6f 66 20 54 72 69 67 67  ed list of Trigg
2b689 65 72 53 74 65 70 20 73 74 72 75 63 74 75 72 65  erStep structure
2b68a 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
2b68b 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2b68c 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 53 74  3DeleteTriggerSt
2b68d 65 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ep(sqlite3 *db, 
2b68e 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 54 72  TriggerStep *pTr
2b68f 69 67 67 65 72 53 74 65 70 29 7b 0a 20 20 77 68  iggerStep){.  wh
2b690 69 6c 65 28 20 70 54 72 69 67 67 65 72 53 74 65  ile( pTriggerSte
2b691 70 20 29 7b 0a 20 20 20 20 54 72 69 67 67 65 72  p ){.    Trigger
2b692 53 74 65 70 20 2a 20 70 54 6d 70 20 3d 20 70 54  Step * pTmp = pT
2b693 72 69 67 67 65 72 53 74 65 70 3b 0a 20 20 20 20  riggerStep;.    
2b694 70 54 72 69 67 67 65 72 53 74 65 70 20 3d 20 70  pTriggerStep = p
2b695 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 4e 65  TriggerStep->pNe
2b696 78 74 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33  xt;..    sqlite3
2b697 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
2b698 54 6d 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20  Tmp->pWhere);.  
2b699 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
2b69a 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54 6d 70  tDelete(db, pTmp
2b69b 2d 3e 70 45 78 70 72 4c 69 73 74 29 3b 0a 20 20  ->pExprList);.  
2b69c 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
2b69d 65 6c 65 74 65 28 64 62 2c 20 70 54 6d 70 2d 3e  elete(db, pTmp->
2b69e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 73 71  pSelect);.    sq
2b69f 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74  lite3IdListDelet
2b6a0 65 28 64 62 2c 20 70 54 6d 70 2d 3e 70 49 64 4c  e(db, pTmp->pIdL
2b6a1 69 73 74 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74  ist);..    sqlit
2b6a2 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 6d  e3DbFree(db, pTm
2b6a3 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  p);.  }.}../*.**
2b6a4 20 47 69 76 65 6e 20 74 61 62 6c 65 20 70 54 61   Given table pTa
2b6a5 62 2c 20 72 65 74 75 72 6e 20 61 20 6c 69 73 74  b, return a list
2b6a6 20 6f 66 20 61 6c 6c 20 74 68 65 20 74 72 69 67   of all the trig
2b6a7 67 65 72 73 20 61 74 74 61 63 68 65 64 20 74 6f  gers attached to
2b6a8 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e 20   .** the table. 
2b6a9 54 68 65 20 6c 69 73 74 20 69 73 20 63 6f 6e 6e  The list is conn
2b6aa 65 63 74 65 64 20 62 79 20 54 72 69 67 67 65 72  ected by Trigger
2b6ab 2e 70 4e 65 78 74 20 70 6f 69 6e 74 65 72 73 2e  .pNext pointers.
2b6ac 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68  .**.** All of th
2b6ad 65 20 74 72 69 67 67 65 72 73 20 6f 6e 20 70 54  e triggers on pT
2b6ae 61 62 20 74 68 61 74 20 61 72 65 20 69 6e 20 74  ab that are in t
2b6af 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
2b6b0 20 61 73 20 70 54 61 62 0a 2a 2a 20 61 72 65 20   as pTab.** are 
2b6b1 61 6c 72 65 61 64 79 20 61 74 74 61 63 68 65 64  already attached
2b6b2 20 74 6f 20 70 54 61 62 2d 3e 70 54 72 69 67 67   to pTab->pTrigg
2b6b3 65 72 2e 20 20 42 75 74 20 74 68 65 72 65 20 6d  er.  But there m
2b6b4 69 67 68 74 20 62 65 20 61 64 64 69 74 69 6f 6e  ight be addition
2b6b5 61 6c 0a 2a 2a 20 74 72 69 67 67 65 72 73 20 6f  al.** triggers o
2b6b6 6e 20 70 54 61 62 20 69 6e 20 74 68 65 20 54 45  n pTab in the TE
2b6b7 4d 50 20 73 63 68 65 6d 61 2e 20 20 54 68 69 73  MP schema.  This
2b6b8 20 72 6f 75 74 69 6e 65 20 70 72 65 70 65 6e 64   routine prepend
2b6b9 73 20 61 6c 6c 0a 2a 2a 20 54 45 4d 50 20 74 72  s all.** TEMP tr
2b6ba 69 67 67 65 72 73 20 6f 6e 20 70 54 61 62 20 74  iggers on pTab t
2b6bb 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  o the beginning 
2b6bc 6f 66 20 74 68 65 20 70 54 61 62 2d 3e 70 54 72  of the pTab->pTr
2b6bd 69 67 67 65 72 20 6c 69 73 74 0a 2a 2a 20 61 6e  igger list.** an
2b6be 64 20 72 65 74 75 72 6e 73 20 74 68 65 20 63 6f  d returns the co
2b6bf 6d 62 69 6e 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a  mbined list..**.
2b6c0 2a 2a 20 54 6f 20 73 74 61 74 65 20 69 74 20 61  ** To state it a
2b6c1 6e 6f 74 68 65 72 20 77 61 79 3a 20 20 54 68 69  nother way:  Thi
2b6c2 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
2b6c3 73 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  s a list of all 
2b6c4 74 72 69 67 67 65 72 73 0a 2a 2a 20 74 68 61 74  triggers.** that
2b6c5 20 66 69 72 65 20 6f 66 66 20 6f 66 20 70 54 61   fire off of pTa
2b6c6 62 2e 20 20 54 68 65 20 6c 69 73 74 20 77 69 6c  b.  The list wil
2b6c7 6c 20 69 6e 63 6c 75 64 65 20 61 6e 79 20 54 45  l include any TE
2b6c8 4d 50 20 74 72 69 67 67 65 72 73 20 6f 6e 0a 2a  MP triggers on.*
2b6c9 2a 20 70 54 61 62 20 61 73 20 77 65 6c 6c 20 61  * pTab as well a
2b6ca 73 20 74 68 65 20 74 72 69 67 67 65 72 73 20 6c  s the triggers l
2b6cb 69 73 65 64 20 69 6e 20 70 54 61 62 2d 3e 70 54  ised in pTab->pT
2b6cc 72 69 67 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  rigger..*/.SQLIT
2b6cd 45 5f 50 52 49 56 41 54 45 20 54 72 69 67 67 65  E_PRIVATE Trigge
2b6ce 72 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65  r *sqlite3Trigge
2b6cf 72 4c 69 73 74 28 50 61 72 73 65 20 2a 70 50 61  rList(Parse *pPa
2b6d0 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62  rse, Table *pTab
2b6d1 29 7b 0a 20 20 53 63 68 65 6d 61 20 2a 20 63 6f  ){.  Schema * co
2b6d2 6e 73 74 20 70 54 6d 70 53 63 68 65 6d 61 20 3d  nst pTmpSchema =
2b6d3 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62   pParse->db->aDb
2b6d4 5b 31 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 54  [1].pSchema;.  T
2b6d5 72 69 67 67 65 72 20 2a 70 4c 69 73 74 20 3d 20  rigger *pList = 
2b6d6 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
2b6d7 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74      /* List of t
2b6d8 72 69 67 67 65 72 73 20 74 6f 20 72 65 74 75 72  riggers to retur
2b6d9 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 54 6d 70  n */..  if( pTmp
2b6da 53 63 68 65 6d 61 21 3d 70 54 61 62 2d 3e 70 53  Schema!=pTab->pS
2b6db 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 48 61 73  chema ){.    Has
2b6dc 68 45 6c 65 6d 20 2a 70 3b 0a 20 20 20 20 66 6f  hElem *p;.    fo
2b6dd 72 28 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69  r(p=sqliteHashFi
2b6de 72 73 74 28 26 70 54 6d 70 53 63 68 65 6d 61 2d  rst(&pTmpSchema-
2b6df 3e 74 72 69 67 48 61 73 68 29 3b 20 70 3b 20 70  >trigHash); p; p
2b6e0 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28  =sqliteHashNext(
2b6e1 70 29 29 7b 0a 20 20 20 20 20 20 54 72 69 67 67  p)){.      Trigg
2b6e2 65 72 20 2a 70 54 72 69 67 20 3d 20 28 54 72 69  er *pTrig = (Tri
2b6e3 67 67 65 72 20 2a 29 73 71 6c 69 74 65 48 61 73  gger *)sqliteHas
2b6e4 68 44 61 74 61 28 70 29 3b 0a 20 20 20 20 20 20  hData(p);.      
2b6e5 69 66 28 20 70 54 72 69 67 2d 3e 70 54 61 62 53  if( pTrig->pTabS
2b6e6 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63  chema==pTab->pSc
2b6e7 68 65 6d 61 0a 20 20 20 20 20 20 20 26 26 20 30  hema.       && 0
2b6e8 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ==sqlite3StrICmp
2b6e9 28 70 54 72 69 67 2d 3e 74 61 62 6c 65 2c 20 70  (pTrig->table, p
2b6ea 54 61 62 2d 3e 7a 4e 61 6d 65 29 20 0a 20 20 20  Tab->zName) .   
2b6eb 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54     ){.        pT
2b6ec 72 69 67 2d 3e 70 4e 65 78 74 20 3d 20 28 70 4c  rig->pNext = (pL
2b6ed 69 73 74 20 3f 20 70 4c 69 73 74 20 3a 20 70 54  ist ? pList : pT
2b6ee 61 62 2d 3e 70 54 72 69 67 67 65 72 29 3b 0a 20  ab->pTrigger);. 
2b6ef 20 20 20 20 20 20 20 70 4c 69 73 74 20 3d 20 70         pList = p
2b6f0 54 72 69 67 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Trig;.      }.  
2b6f1 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72    }.  }..  retur
2b6f2 6e 20 28 70 4c 69 73 74 20 3f 20 70 4c 69 73 74  n (pList ? pList
2b6f3 20 3a 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65   : pTab->pTrigge
2b6f4 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  r);.}../*.** Thi
2b6f5 73 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74  s is called by t
2b6f6 68 65 20 70 61 72 73 65 72 20 77 68 65 6e 20 69  he parser when i
2b6f7 74 20 73 65 65 73 20 61 20 43 52 45 41 54 45 20  t sees a CREATE 
2b6f8 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e  TRIGGER statemen
2b6f9 74 0a 2a 2a 20 75 70 20 74 6f 20 74 68 65 20 70  t.** up to the p
2b6fa 6f 69 6e 74 20 6f 66 20 74 68 65 20 42 45 47 49  oint of the BEGI
2b6fb 4e 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 69  N before the tri
2b6fc 67 67 65 72 20 61 63 74 69 6f 6e 73 2e 20 20 41  gger actions.  A
2b6fd 20 54 72 69 67 67 65 72 0a 2a 2a 20 73 74 72 75   Trigger.** stru
2b6fe 63 74 75 72 65 20 69 73 20 67 65 6e 65 72 61 74  cture is generat
2b6ff 65 64 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20  ed based on the 
2b700 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 76 61 69  information avai
2b701 6c 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 65 64  lable and stored
2b702 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65 2d 3e 70  .** in pParse->p
2b703 4e 65 77 54 72 69 67 67 65 72 2e 20 20 41 66 74  NewTrigger.  Aft
2b704 65 72 20 74 68 65 20 74 72 69 67 67 65 72 20 61  er the trigger a
2b705 63 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65 6e  ctions have been
2b706 20 70 61 72 73 65 64 2c 20 74 68 65 0a 2a 2a 20   parsed, the.** 
2b707 73 71 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69  sqlite3FinishTri
2b708 67 67 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 20  gger() function 
2b709 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 6d  is called to com
2b70a 70 6c 65 74 65 20 74 68 65 20 74 72 69 67 67 65  plete the trigge
2b70b 72 0a 2a 2a 20 63 6f 6e 73 74 72 75 63 74 69 6f  r.** constructio
2b70c 6e 20 70 72 6f 63 65 73 73 2e 0a 2a 2f 0a 53 51  n process..*/.SQ
2b70d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2b70e 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72  d sqlite3BeginTr
2b70f 69 67 67 65 72 28 0a 20 20 50 61 72 73 65 20 2a  igger(.  Parse *
2b710 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20  pParse,      /* 
2b711 54 68 65 20 70 61 72 73 65 20 63 6f 6e 74 65 78  The parse contex
2b712 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20  t of the CREATE 
2b713 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e  TRIGGER statemen
2b714 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e  t */.  Token *pN
2b715 61 6d 65 31 2c 20 20 20 20 20 20 2f 2a 20 54 68  ame1,      /* Th
2b716 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 72  e name of the tr
2b717 69 67 67 65 72 20 2a 2f 0a 20 20 54 6f 6b 65 6e  igger */.  Token
2b718 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 20 2f   *pName2,      /
2b719 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  * The name of th
2b71a 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 69  e trigger */.  i
2b71b 6e 74 20 74 72 5f 74 6d 2c 20 20 20 20 20 20 20  nt tr_tm,       
2b71c 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f     /* One of TK_
2b71d 42 45 46 4f 52 45 2c 20 54 4b 5f 41 46 54 45 52  BEFORE, TK_AFTER
2b71e 2c 20 54 4b 5f 49 4e 53 54 45 41 44 20 2a 2f 0a  , TK_INSTEAD */.
2b71f 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20    int op,       
2b720 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
2b721 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 55 50  TK_INSERT, TK_UP
2b722 44 41 54 45 2c 20 54 4b 5f 44 45 4c 45 54 45 20  DATE, TK_DELETE 
2b723 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 43 6f  */.  IdList *pCo
2b724 6c 75 6d 6e 73 2c 20 20 20 2f 2a 20 63 6f 6c 75  lumns,   /* colu
2b725 6d 6e 20 6c 69 73 74 20 69 66 20 74 68 69 73 20  mn list if this 
2b726 69 73 20 61 6e 20 55 50 44 41 54 45 20 4f 46 20  is an UPDATE OF 
2b727 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 53 72 63  trigger */.  Src
2b728 4c 69 73 74 20 2a 70 54 61 62 6c 65 4e 61 6d 65  List *pTableName
2b729 2c 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20  ,/* The name of 
2b72a 74 68 65 20 74 61 62 6c 65 2f 76 69 65 77 20 74  the table/view t
2b72b 68 65 20 74 72 69 67 67 65 72 20 61 70 70 6c 69  he trigger appli
2b72c 65 73 20 74 6f 20 2a 2f 0a 20 20 45 78 70 72 20  es to */.  Expr 
2b72d 2a 70 57 68 65 6e 2c 20 20 20 20 20 20 20 20 2f  *pWhen,        /
2b72e 2a 20 57 48 45 4e 20 63 6c 61 75 73 65 20 2a 2f  * WHEN clause */
2b72f 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 2c 20 20  .  int isTemp,  
2b730 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
2b731 66 20 74 68 65 20 54 45 4d 50 4f 52 41 52 59 20  f the TEMPORARY 
2b732 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73 65  keyword is prese
2b733 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45 72  nt */.  int noEr
2b734 72 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  r           /* S
2b735 75 70 70 72 65 73 73 20 65 72 72 6f 72 73 20 69  uppress errors i
2b736 66 20 74 68 65 20 74 72 69 67 67 65 72 20 61 6c  f the trigger al
2b737 72 65 61 64 79 20 65 78 69 73 74 73 20 2a 2f 0a  ready exists */.
2b738 29 7b 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54  ){.  Trigger *pT
2b739 72 69 67 67 65 72 20 3d 20 30 3b 20 20 2f 2a 20  rigger = 0;  /* 
2b73a 54 68 65 20 6e 65 77 20 74 72 69 67 67 65 72 20  The new trigger 
2b73b 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
2b73c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
2b73d 54 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 74  Table that the t
2b73e 72 69 67 67 65 72 20 66 69 72 65 73 20 6f 66 66  rigger fires off
2b73f 20 6f 66 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a   of */.  char *z
2b740 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20  Name = 0;       
2b741 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
2b742 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 73 71 6c  trigger */.  sql
2b743 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2b744 65 2d 3e 64 62 3b 20 20 2f 2a 20 54 68 65 20 64  e->db;  /* The d
2b745 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
2b746 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b  on */.  int iDb;
2b747 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b748 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
2b749 74 6f 20 73 74 6f 72 65 20 74 68 65 20 74 72 69  to store the tri
2b74a 67 67 65 72 20 69 6e 20 2a 2f 0a 20 20 54 6f 6b  gger in */.  Tok
2b74b 65 6e 20 2a 70 4e 61 6d 65 3b 20 20 20 20 20 20  en *pName;      
2b74c 20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 71 75       /* The unqu
2b74d 61 6c 69 66 69 65 64 20 64 62 20 6e 61 6d 65 20  alified db name 
2b74e 2a 2f 0a 20 20 44 62 46 69 78 65 72 20 73 46 69  */.  DbFixer sFi
2b74f 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  x;           /* 
2b750 53 74 61 74 65 20 76 65 63 74 6f 72 20 66 6f 72  State vector for
2b751 20 74 68 65 20 44 42 20 66 69 78 65 72 20 2a 2f   the DB fixer */
2b752 0a 20 20 69 6e 74 20 69 54 61 62 44 62 3b 20 20  .  int iTabDb;  
2b753 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
2b754 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62  dex of the datab
2b755 61 73 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62  ase holding pTab
2b756 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
2b757 4e 61 6d 65 31 21 3d 30 20 29 3b 20 20 20 2f 2a  Name1!=0 );   /*
2b758 20 70 4e 61 6d 65 31 2d 3e 7a 20 6d 69 67 68 74   pName1->z might
2b759 20 62 65 20 4e 55 4c 4c 2c 20 62 75 74 20 6e 6f   be NULL, but no
2b75a 74 20 70 4e 61 6d 65 31 20 69 74 73 65 6c 66 20  t pName1 itself 
2b75b 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4e 61  */.  assert( pNa
2b75c 6d 65 32 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  me2!=0 );.  asse
2b75d 72 74 28 20 6f 70 3d 3d 54 4b 5f 49 4e 53 45 52  rt( op==TK_INSER
2b75e 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 50 44 41  T || op==TK_UPDA
2b75f 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 44 45 4c  TE || op==TK_DEL
2b760 45 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ETE );.  assert(
2b761 20 6f 70 3e 30 20 26 26 20 6f 70 3c 30 78 66 66   op>0 && op<0xff
2b762 20 29 3b 0a 20 20 69 66 28 20 69 73 54 65 6d 70   );.  if( isTemp
2b763 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 54 45   ){.    /* If TE
2b764 4d 50 20 77 61 73 20 73 70 65 63 69 66 69 65 64  MP was specified
2b765 2c 20 74 68 65 6e 20 74 68 65 20 74 72 69 67 67  , then the trigg
2b766 65 72 20 6e 61 6d 65 20 6d 61 79 20 6e 6f 74 20  er name may not 
2b767 62 65 20 71 75 61 6c 69 66 69 65 64 2e 20 2a 2f  be qualified. */
2b768 0a 20 20 20 20 69 66 28 20 70 4e 61 6d 65 32 2d  .    if( pName2-
2b769 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71  >n>0 ){.      sq
2b76a 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2b76b 61 72 73 65 2c 20 22 74 65 6d 70 6f 72 61 72 79  arse, "temporary
2b76c 20 74 72 69 67 67 65 72 20 6d 61 79 20 6e 6f 74   trigger may not
2b76d 20 68 61 76 65 20 71 75 61 6c 69 66 69 65 64 20   have qualified 
2b76e 6e 61 6d 65 22 29 3b 0a 20 20 20 20 20 20 67 6f  name");.      go
2b76f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  to trigger_clean
2b770 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 44  up;.    }.    iD
2b771 62 20 3d 20 31 3b 0a 20 20 20 20 70 4e 61 6d 65  b = 1;.    pName
2b772 20 3d 20 70 4e 61 6d 65 31 3b 0a 20 20 7d 65 6c   = pName1;.  }el
2b773 73 65 7b 0a 20 20 20 20 2f 2a 20 46 69 67 75 72  se{.    /* Figur
2b774 65 20 6f 75 74 20 74 68 65 20 64 62 20 74 68 61  e out the db tha
2b775 74 20 74 68 65 20 74 68 65 20 74 72 69 67 67 65  t the the trigge
2b776 72 20 77 69 6c 6c 20 62 65 20 63 72 65 61 74 65  r will be create
2b777 64 20 69 6e 20 2a 2f 0a 20 20 20 20 69 44 62 20  d in */.    iDb 
2b778 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74  = sqlite3TwoPart
2b779 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61  Name(pParse, pNa
2b77a 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e  me1, pName2, &pN
2b77b 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 69 44  ame);.    if( iD
2b77c 62 3c 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  b<0 ){.      got
2b77d 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
2b77e 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  p;.    }.  }..  
2b77f 2f 2a 20 49 66 20 74 68 65 20 74 72 69 67 67 65  /* If the trigge
2b780 72 20 6e 61 6d 65 20 77 61 73 20 75 6e 71 75 61  r name was unqua
2b781 6c 69 66 69 65 64 2c 20 61 6e 64 20 74 68 65 20  lified, and the 
2b782 74 61 62 6c 65 20 69 73 20 61 20 74 65 6d 70 20  table is a temp 
2b783 74 61 62 6c 65 2c 0a 20 20 2a 2a 20 74 68 65 6e  table,.  ** then
2b784 20 73 65 74 20 69 44 62 20 74 6f 20 31 20 74 6f   set iDb to 1 to
2b785 20 63 72 65 61 74 65 20 74 68 65 20 74 72 69 67   create the trig
2b786 67 65 72 20 69 6e 20 74 68 65 20 74 65 6d 70 6f  ger in the tempo
2b787 72 61 72 79 20 64 61 74 61 62 61 73 65 2e 0a 20  rary database.. 
2b788 20 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 53 72   ** If sqlite3Sr
2b789 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 29 20 72 65  cListLookup() re
2b78a 74 75 72 6e 73 20 30 2c 20 69 6e 64 69 63 61 74  turns 0, indicat
2b78b 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 64 6f  ing the table do
2b78c 65 73 20 6e 6f 74 0a 20 20 2a 2a 20 65 78 69 73  es not.  ** exis
2b78d 74 2c 20 74 68 65 20 65 72 72 6f 72 20 69 73 20  t, the error is 
2b78e 63 61 75 67 68 74 20 62 79 20 74 68 65 20 62 6c  caught by the bl
2b78f 6f 63 6b 20 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a  ock below..  */.
2b790 20 20 69 66 28 20 21 70 54 61 62 6c 65 4e 61 6d    if( !pTableNam
2b791 65 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  e || db->mallocF
2b792 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74  ailed ){.    got
2b793 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
2b794 70 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d 20  p;.  }.  pTab = 
2b795 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f  sqlite3SrcListLo
2b796 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54 61  okup(pParse, pTa
2b797 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  bleName);.  if( 
2b798 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20 26 26 20  pName2->n==0 && 
2b799 70 54 61 62 20 26 26 20 70 54 61 62 2d 3e 70 53  pTab && pTab->pS
2b79a 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b 31  chema==db->aDb[1
2b79b 5d 2e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20  ].pSchema ){.   
2b79c 20 69 44 62 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20   iDb = 1;.  }.. 
2b79d 20 2f 2a 20 45 6e 73 75 72 65 20 74 68 65 20 74   /* Ensure the t
2b79e 61 62 6c 65 20 6e 61 6d 65 20 6d 61 74 63 68 65  able name matche
2b79f 73 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  s database name 
2b7a0 61 6e 64 20 74 68 61 74 20 74 68 65 20 74 61 62  and that the tab
2b7a1 6c 65 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 69  le exists */.  i
2b7a2 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
2b7a3 6c 65 64 20 29 20 67 6f 74 6f 20 74 72 69 67 67  led ) goto trigg
2b7a4 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 61 73  er_cleanup;.  as
2b7a5 73 65 72 74 28 20 70 54 61 62 6c 65 4e 61 6d 65  sert( pTableName
2b7a6 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 69  ->nSrc==1 );.  i
2b7a7 66 28 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69  f( sqlite3FixIni
2b7a8 74 28 26 73 46 69 78 2c 20 70 50 61 72 73 65 2c  t(&sFix, pParse,
2b7a9 20 69 44 62 2c 20 22 74 72 69 67 67 65 72 22 2c   iDb, "trigger",
2b7aa 20 70 4e 61 6d 65 29 20 26 26 20 0a 20 20 20 20   pName) && .    
2b7ab 20 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c    sqlite3FixSrcL
2b7ac 69 73 74 28 26 73 46 69 78 2c 20 70 54 61 62 6c  ist(&sFix, pTabl
2b7ad 65 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f  eName) ){.    go
2b7ae 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  to trigger_clean
2b7af 75 70 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d  up;.  }.  pTab =
2b7b0 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c   sqlite3SrcListL
2b7b1 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54  ookup(pParse, pT
2b7b2 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 69 66 28  ableName);.  if(
2b7b3 20 21 70 54 61 62 20 29 7b 0a 20 20 20 20 2f 2a   !pTab ){.    /*
2b7b4 20 54 68 65 20 74 61 62 6c 65 20 64 6f 65 73 20   The table does 
2b7b5 6e 6f 74 20 65 78 69 73 74 2e 20 2a 2f 0a 20 20  not exist. */.  
2b7b6 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 69    if( db->init.i
2b7b7 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 2f  Db==1 ){.      /
2b7b8 2a 20 54 69 63 6b 65 74 20 23 33 38 31 30 2e 0a  * Ticket #3810..
2b7b9 20 20 20 20 20 20 2a 2a 20 4e 6f 72 6d 61 6c 6c        ** Normall
2b7ba 79 2c 20 77 68 65 6e 65 76 65 72 20 61 20 74 61  y, whenever a ta
2b7bb 62 6c 65 20 69 73 20 64 72 6f 70 70 65 64 2c 20  ble is dropped, 
2b7bc 61 6c 6c 20 61 73 73 6f 63 69 61 74 65 64 20 74  all associated t
2b7bd 72 69 67 67 65 72 73 20 61 72 65 0a 20 20 20 20  riggers are.    
2b7be 20 20 2a 2a 20 64 72 6f 70 70 65 64 20 74 6f 6f    ** dropped too
2b7bf 2e 20 20 42 75 74 20 69 66 20 61 20 54 45 4d 50  .  But if a TEMP
2b7c0 20 74 72 69 67 67 65 72 20 69 73 20 63 72 65 61   trigger is crea
2b7c1 74 65 64 20 6f 6e 20 61 20 6e 6f 6e 2d 54 45 4d  ted on a non-TEM
2b7c2 50 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a  P table.      **
2b7c3 20 61 6e 64 20 74 68 65 20 74 61 62 6c 65 20 69   and the table i
2b7c4 73 20 64 72 6f 70 70 65 64 20 62 79 20 61 20 64  s dropped by a d
2b7c5 69 66 66 65 72 65 6e 74 20 64 61 74 61 62 61 73  ifferent databas
2b7c6 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 68  e connection, th
2b7c7 65 0a 20 20 20 20 20 20 2a 2a 20 74 72 69 67 67  e.      ** trigg
2b7c8 65 72 20 69 73 20 6e 6f 74 20 76 69 73 69 62 6c  er is not visibl
2b7c9 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  e to the databas
2b7ca 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61  e connection tha
2b7cb 74 20 64 6f 65 73 20 74 68 65 0a 20 20 20 20 20  t does the.     
2b7cc 20 2a 2a 20 64 72 6f 70 20 73 6f 20 74 68 65 20   ** drop so the 
2b7cd 74 72 69 67 67 65 72 20 63 61 6e 6e 6f 74 20 62  trigger cannot b
2b7ce 65 20 64 72 6f 70 70 65 64 2e 20 20 54 68 69 73  e dropped.  This
2b7cf 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 0a 20   results in an. 
2b7d0 20 20 20 20 20 2a 2a 20 22 6f 72 70 68 61 6e 65       ** "orphane
2b7d1 64 20 74 72 69 67 67 65 72 22 20 2d 20 61 20 74  d trigger" - a t
2b7d2 72 69 67 67 65 72 20 77 68 6f 73 65 20 61 73 73  rigger whose ass
2b7d3 6f 63 69 61 74 65 64 20 74 61 62 6c 65 20 69 73  ociated table is
2b7d4 20 6d 69 73 73 69 6e 67 2e 0a 20 20 20 20 20 20   missing..      
2b7d5 2a 2f 0a 20 20 20 20 20 20 64 62 2d 3e 69 6e 69  */.      db->ini
2b7d6 74 2e 6f 72 70 68 61 6e 54 72 69 67 67 65 72 20  t.orphanTrigger 
2b7d7 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67  = 1;.    }.    g
2b7d8 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61  oto trigger_clea
2b7d9 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 49  nup;.  }.  if( I
2b7da 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
2b7db 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
2b7dc 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63  orMsg(pParse, "c
2b7dd 61 6e 6e 6f 74 20 63 72 65 61 74 65 20 74 72 69  annot create tri
2b7de 67 67 65 72 73 20 6f 6e 20 76 69 72 74 75 61 6c  ggers on virtual
2b7df 20 74 61 62 6c 65 73 22 29 3b 0a 20 20 20 20 67   tables");.    g
2b7e0 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61  oto trigger_clea
2b7e1 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  nup;.  }..  /* C
2b7e2 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 74 72  heck that the tr
2b7e3 69 67 67 65 72 20 6e 61 6d 65 20 69 73 20 6e 6f  igger name is no
2b7e4 74 20 72 65 73 65 72 76 65 64 20 61 6e 64 20 74  t reserved and t
2b7e5 68 61 74 20 6e 6f 20 74 72 69 67 67 65 72 20 6f  hat no trigger o
2b7e6 66 20 74 68 65 0a 20 20 2a 2a 20 73 70 65 63 69  f the.  ** speci
2b7e7 66 69 65 64 20 6e 61 6d 65 20 65 78 69 73 74 73  fied name exists
2b7e8 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71   */.  zName = sq
2b7e9 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
2b7ea 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20  en(db, pName);. 
2b7eb 20 69 66 28 20 21 7a 4e 61 6d 65 20 7c 7c 20 53   if( !zName || S
2b7ec 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65  QLITE_OK!=sqlite
2b7ed 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65  3CheckObjectName
2b7ee 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20  (pParse, zName) 
2b7ef 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67  ){.    goto trig
2b7f0 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ger_cleanup;.  }
2b7f1 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 48 61  .  if( sqlite3Ha
2b7f2 73 68 46 69 6e 64 28 26 28 64 62 2d 3e 61 44 62  shFind(&(db->aDb
2b7f3 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 74  [iDb].pSchema->t
2b7f4 72 69 67 48 61 73 68 29 2c 0a 20 20 20 20 20 20  rigHash),.      
2b7f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b7f6 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74  zName, sqlite3St
2b7f7 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 29 20 29  rlen30(zName)) )
2b7f8 7b 0a 20 20 20 20 69 66 28 20 21 6e 6f 45 72 72  {.    if( !noErr
2b7f9 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2b7fa 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2b7fb 2c 20 22 74 72 69 67 67 65 72 20 25 54 20 61 6c  , "trigger %T al
2b7fc 72 65 61 64 79 20 65 78 69 73 74 73 22 2c 20 70  ready exists", p
2b7fd 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Name);.    }.   
2b7fe 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c   goto trigger_cl
2b7ff 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  eanup;.  }..  /*
2b800 20 44 6f 20 6e 6f 74 20 63 72 65 61 74 65 20 61   Do not create a
2b801 20 74 72 69 67 67 65 72 20 6f 6e 20 61 20 73 79   trigger on a sy
2b802 73 74 65 6d 20 74 61 62 6c 65 20 2a 2f 0a 20 20  stem table */.  
2b803 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49  if( sqlite3StrNI
2b804 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  Cmp(pTab->zName,
2b805 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29 3d 3d   "sqlite_", 7)==
2b806 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
2b807 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2b808 20 22 63 61 6e 6e 6f 74 20 63 72 65 61 74 65 20   "cannot create 
2b809 74 72 69 67 67 65 72 20 6f 6e 20 73 79 73 74 65  trigger on syste
2b80a 6d 20 74 61 62 6c 65 22 29 3b 0a 20 20 20 20 70  m table");.    p
2b80b 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20  Parse->nErr++;. 
2b80c 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f     goto trigger_
2b80d 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20  cleanup;.  }..  
2b80e 2f 2a 20 49 4e 53 54 45 41 44 20 6f 66 20 74 72  /* INSTEAD of tr
2b80f 69 67 67 65 72 73 20 61 72 65 20 6f 6e 6c 79 20  iggers are only 
2b810 66 6f 72 20 76 69 65 77 73 20 61 6e 64 20 76 69  for views and vi
2b811 65 77 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74  ews only support
2b812 20 49 4e 53 54 45 41 44 0a 20 20 2a 2a 20 6f 66   INSTEAD.  ** of
2b813 20 74 72 69 67 67 65 72 73 2e 0a 20 20 2a 2f 0a   triggers..  */.
2b814 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c    if( pTab->pSel
2b815 65 63 74 20 26 26 20 74 72 5f 74 6d 21 3d 54 4b  ect && tr_tm!=TK
2b816 5f 49 4e 53 54 45 41 44 20 29 7b 0a 20 20 20 20  _INSTEAD ){.    
2b817 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2b818 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20  pParse, "cannot 
2b819 63 72 65 61 74 65 20 25 73 20 74 72 69 67 67 65  create %s trigge
2b81a 72 20 6f 6e 20 76 69 65 77 3a 20 25 53 22 2c 20  r on view: %S", 
2b81b 0a 20 20 20 20 20 20 20 20 28 74 72 5f 74 6d 20  .        (tr_tm 
2b81c 3d 3d 20 54 4b 5f 42 45 46 4f 52 45 29 3f 22 42  == TK_BEFORE)?"B
2b81d 45 46 4f 52 45 22 3a 22 41 46 54 45 52 22 2c 20  EFORE":"AFTER", 
2b81e 70 54 61 62 6c 65 4e 61 6d 65 2c 20 30 29 3b 0a  pTableName, 0);.
2b81f 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72      goto trigger
2b820 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20  _cleanup;.  }.  
2b821 69 66 28 20 21 70 54 61 62 2d 3e 70 53 65 6c 65  if( !pTab->pSele
2b822 63 74 20 26 26 20 74 72 5f 74 6d 3d 3d 54 4b 5f  ct && tr_tm==TK_
2b823 49 4e 53 54 45 41 44 20 29 7b 0a 20 20 20 20 73  INSTEAD ){.    s
2b824 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2b825 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63  Parse, "cannot c
2b826 72 65 61 74 65 20 49 4e 53 54 45 41 44 20 4f 46  reate INSTEAD OF
2b827 22 0a 20 20 20 20 20 20 20 20 22 20 74 72 69 67  ".        " trig
2b828 67 65 72 20 6f 6e 20 74 61 62 6c 65 3a 20 25 53  ger on table: %S
2b829 22 2c 20 70 54 61 62 6c 65 4e 61 6d 65 2c 20 30  ", pTableName, 0
2b82a 29 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67  );.    goto trig
2b82b 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ger_cleanup;.  }
2b82c 0a 20 20 69 54 61 62 44 62 20 3d 20 73 71 6c 69  .  iTabDb = sqli
2b82d 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
2b82e 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  (db, pTab->pSche
2b82f 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  ma);..#ifndef SQ
2b830 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
2b831 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20  IZATION.  {.    
2b832 69 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c 49 54  int code = SQLIT
2b833 45 5f 43 52 45 41 54 45 5f 54 52 49 47 47 45 52  E_CREATE_TRIGGER
2b834 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
2b835 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b   *zDb = db->aDb[
2b836 69 54 61 62 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20  iTabDb].zName;. 
2b837 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
2b838 44 62 54 72 69 67 20 3d 20 69 73 54 65 6d 70 20  DbTrig = isTemp 
2b839 3f 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 7a 4e 61  ? db->aDb[1].zNa
2b83a 6d 65 20 3a 20 7a 44 62 3b 0a 20 20 20 20 69 66  me : zDb;.    if
2b83b 28 20 69 54 61 62 44 62 3d 3d 31 20 7c 7c 20 69  ( iTabDb==1 || i
2b83c 73 54 65 6d 70 20 29 20 63 6f 64 65 20 3d 20 53  sTemp ) code = S
2b83d 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d  QLITE_CREATE_TEM
2b83e 50 5f 54 52 49 47 47 45 52 3b 0a 20 20 20 20 69  P_TRIGGER;.    i
2b83f 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
2b840 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65  eck(pParse, code
2b841 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a  , zName, pTab->z
2b842 4e 61 6d 65 2c 20 7a 44 62 54 72 69 67 29 20 29  Name, zDbTrig) )
2b843 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 72 69  {.      goto tri
2b844 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  gger_cleanup;.  
2b845 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
2b846 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
2b847 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45  rse, SQLITE_INSE
2b848 52 54 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45  RT, SCHEMA_TABLE
2b849 28 69 54 61 62 44 62 29 2c 30 2c 7a 44 62 29 29  (iTabDb),0,zDb))
2b84a 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 72 69  {.      goto tri
2b84b 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  gger_cleanup;.  
2b84c 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a    }.  }.#endif..
2b84d 20 20 2f 2a 20 49 4e 53 54 45 41 44 20 4f 46 20    /* INSTEAD OF 
2b84e 74 72 69 67 67 65 72 73 20 63 61 6e 20 6f 6e 6c  triggers can onl
2b84f 79 20 61 70 70 65 61 72 20 6f 6e 20 76 69 65 77  y appear on view
2b850 73 20 61 6e 64 20 42 45 46 4f 52 45 20 74 72 69  s and BEFORE tri
2b851 67 67 65 72 73 0a 20 20 2a 2a 20 63 61 6e 6e 6f  ggers.  ** canno
2b852 74 20 61 70 70 65 61 72 20 6f 6e 20 76 69 65 77  t appear on view
2b853 73 2e 20 20 53 6f 20 77 65 20 6d 69 67 68 74 20  s.  So we might 
2b854 61 73 20 77 65 6c 6c 20 74 72 61 6e 73 6c 61 74  as well translat
2b855 65 20 65 76 65 72 79 0a 20 20 2a 2a 20 49 4e 53  e every.  ** INS
2b856 54 45 41 44 20 4f 46 20 74 72 69 67 67 65 72 20  TEAD OF trigger 
2b857 69 6e 74 6f 20 61 20 42 45 46 4f 52 45 20 74 72  into a BEFORE tr
2b858 69 67 67 65 72 2e 20 20 49 74 20 73 69 6d 70 6c  igger.  It simpl
2b859 69 66 69 65 73 20 63 6f 64 65 0a 20 20 2a 2a 20  ifies code.  ** 
2b85a 65 6c 73 65 77 68 65 72 65 2e 0a 20 20 2a 2f 0a  elsewhere..  */.
2b85b 20 20 69 66 20 28 74 72 5f 74 6d 20 3d 3d 20 54    if (tr_tm == T
2b85c 4b 5f 49 4e 53 54 45 41 44 29 7b 0a 20 20 20 20  K_INSTEAD){.    
2b85d 74 72 5f 74 6d 20 3d 20 54 4b 5f 42 45 46 4f 52  tr_tm = TK_BEFOR
2b85e 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 75 69  E;.  }..  /* Bui
2b85f 6c 64 20 74 68 65 20 54 72 69 67 67 65 72 20 6f  ld the Trigger o
2b860 62 6a 65 63 74 20 2a 2f 0a 20 20 70 54 72 69 67  bject */.  pTrig
2b861 67 65 72 20 3d 20 28 54 72 69 67 67 65 72 2a 29  ger = (Trigger*)
2b862 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
2b863 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54  ero(db, sizeof(T
2b864 72 69 67 67 65 72 29 29 3b 0a 20 20 69 66 28 20  rigger));.  if( 
2b865 70 54 72 69 67 67 65 72 3d 3d 30 20 29 20 67 6f  pTrigger==0 ) go
2b866 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  to trigger_clean
2b867 75 70 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e  up;.  pTrigger->
2b868 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20  zName = zName;. 
2b869 20 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 70 54   zName = 0;.  pT
2b86a 72 69 67 67 65 72 2d 3e 74 61 62 6c 65 20 3d 20  rigger->table = 
2b86b 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
2b86c 64 62 2c 20 70 54 61 62 6c 65 4e 61 6d 65 2d 3e  db, pTableName->
2b86d 61 5b 30 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 70  a[0].zName);.  p
2b86e 54 72 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61  Trigger->pSchema
2b86f 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
2b870 70 53 63 68 65 6d 61 3b 0a 20 20 70 54 72 69 67  pSchema;.  pTrig
2b871 67 65 72 2d 3e 70 54 61 62 53 63 68 65 6d 61 20  ger->pTabSchema 
2b872 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b  = pTab->pSchema;
2b873 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 6f 70 20  .  pTrigger->op 
2b874 3d 20 28 75 38 29 6f 70 3b 0a 20 20 70 54 72 69  = (u8)op;.  pTri
2b875 67 67 65 72 2d 3e 74 72 5f 74 6d 20 3d 20 74 72  gger->tr_tm = tr
2b876 5f 74 6d 3d 3d 54 4b 5f 42 45 46 4f 52 45 20 3f  _tm==TK_BEFORE ?
2b877 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20   TRIGGER_BEFORE 
2b878 3a 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 3b  : TRIGGER_AFTER;
2b879 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 70 57 68  .  pTrigger->pWh
2b87a 65 6e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  en = sqlite3Expr
2b87b 44 75 70 28 64 62 2c 20 70 57 68 65 6e 2c 20 45  Dup(db, pWhen, E
2b87c 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a  XPRDUP_REDUCE);.
2b87d 20 20 70 54 72 69 67 67 65 72 2d 3e 70 43 6f 6c    pTrigger->pCol
2b87e 75 6d 6e 73 20 3d 20 73 71 6c 69 74 65 33 49 64  umns = sqlite3Id
2b87f 4c 69 73 74 44 75 70 28 64 62 2c 20 70 43 6f 6c  ListDup(db, pCol
2b880 75 6d 6e 73 29 3b 0a 20 20 61 73 73 65 72 74 28  umns);.  assert(
2b881 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69   pParse->pNewTri
2b882 67 67 65 72 3d 3d 30 20 29 3b 0a 20 20 70 50 61  gger==0 );.  pPa
2b883 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72  rse->pNewTrigger
2b884 20 3d 20 70 54 72 69 67 67 65 72 3b 0a 0a 74 72   = pTrigger;..tr
2b885 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3a 0a 20  igger_cleanup:. 
2b886 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2b887 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c  b, zName);.  sql
2b888 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
2b889 65 28 64 62 2c 20 70 54 61 62 6c 65 4e 61 6d 65  e(db, pTableName
2b88a 29 3b 0a 20 20 73 71 6c 69 74 65 33 49 64 4c 69  );.  sqlite3IdLi
2b88b 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f  stDelete(db, pCo
2b88c 6c 75 6d 6e 73 29 3b 0a 20 20 73 71 6c 69 74 65  lumns);.  sqlite
2b88d 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
2b88e 70 57 68 65 6e 29 3b 0a 20 20 69 66 28 20 21 70  pWhen);.  if( !p
2b88f 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67  Parse->pNewTrigg
2b890 65 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  er ){.    sqlite
2b891 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 64  3DeleteTrigger(d
2b892 62 2c 20 70 54 72 69 67 67 65 72 29 3b 0a 20 20  b, pTrigger);.  
2b893 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
2b894 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  t( pParse->pNewT
2b895 72 69 67 67 65 72 3d 3d 70 54 72 69 67 67 65 72  rigger==pTrigger
2b896 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   );.  }.}../*.**
2b897 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
2b898 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61 6c   called after al
2b899 6c 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72  l of the trigger
2b89a 20 61 63 74 69 6f 6e 73 20 68 61 76 65 20 62 65   actions have be
2b89b 65 6e 20 70 61 72 73 65 64 0a 2a 2a 20 69 6e 20  en parsed.** in 
2b89c 6f 72 64 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74  order to complet
2b89d 65 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66  e the process of
2b89e 20 62 75 69 6c 64 69 6e 67 20 74 68 65 20 74 72   building the tr
2b89f 69 67 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  igger..*/.SQLITE
2b8a0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2b8a1 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67 67  lite3FinishTrigg
2b8a2 65 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  er(.  Parse *pPa
2b8a3 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  rse,          /*
2b8a4 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   Parser context 
2b8a5 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65 70  */.  TriggerStep
2b8a6 20 2a 70 53 74 65 70 4c 69 73 74 2c 20 2f 2a 20   *pStepList, /* 
2b8a7 54 68 65 20 74 72 69 67 67 65 72 65 64 20 70 72  The triggered pr
2b8a8 6f 67 72 61 6d 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ogram */.  Token
2b8a9 20 2a 70 41 6c 6c 20 20 20 20 20 20 20 20 20 20   *pAll          
2b8aa 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 74 68 61 74     /* Token that
2b8ab 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 63   describes the c
2b8ac 6f 6d 70 6c 65 74 65 20 43 52 45 41 54 45 20 54  omplete CREATE T
2b8ad 52 49 47 47 45 52 20 2a 2f 0a 29 7b 0a 20 20 54  RIGGER */.){.  T
2b8ae 72 69 67 67 65 72 20 2a 70 54 72 69 67 20 3d 20  rigger *pTrig = 
2b8af 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67  pParse->pNewTrig
2b8b0 67 65 72 3b 20 20 20 20 2f 2a 20 54 72 69 67 67  ger;    /* Trigg
2b8b1 65 72 20 62 65 69 6e 67 20 66 69 6e 69 73 68 65  er being finishe
2b8b2 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61  d */.  char *zNa
2b8b3 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  me;             
2b8b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b8b5 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 72 69 67 67  /* Name of trigg
2b8b6 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  er */.  sqlite3 
2b8b7 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2b8b8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2b8b9 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
2b8ba 20 2a 2f 0a 20 20 44 62 46 69 78 65 72 20 73 46   */.  DbFixer sF
2b8bb 69 78 3b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20  ix;.  int iDb;  
2b8bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b8bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2b8be 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 74 61  * Database conta
2b8bf 69 6e 69 6e 67 20 74 68 65 20 74 72 69 67 67 65  ining the trigge
2b8c0 72 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 6e 61 6d  r */.  Token nam
2b8c1 65 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20  eToken;         
2b8c2 20 20 2f 2a 20 54 72 69 67 67 65 72 20 6e 61 6d    /* Trigger nam
2b8c3 65 20 66 6f 72 20 65 72 72 6f 72 20 72 65 70 6f  e for error repo
2b8c4 72 74 69 6e 67 20 2a 2f 0a 0a 20 20 70 54 72 69  rting */..  pTri
2b8c5 67 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  g = pParse->pNew
2b8c6 54 72 69 67 67 65 72 3b 0a 20 20 70 50 61 72 73  Trigger;.  pPars
2b8c7 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 20 3d  e->pNewTrigger =
2b8c8 20 30 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28   0;.  if( NEVER(
2b8c9 70 50 61 72 73 65 2d 3e 6e 45 72 72 29 20 7c 7c  pParse->nErr) ||
2b8ca 20 21 70 54 72 69 67 20 29 20 67 6f 74 6f 20 74   !pTrig ) goto t
2b8cb 72 69 67 67 65 72 66 69 6e 69 73 68 5f 63 6c 65  riggerfinish_cle
2b8cc 61 6e 75 70 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20  anup;.  zName = 
2b8cd 70 54 72 69 67 2d 3e 7a 4e 61 6d 65 3b 0a 20 20  pTrig->zName;.  
2b8ce 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
2b8cf 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73  emaToIndex(pPars
2b8d0 65 2d 3e 64 62 2c 20 70 54 72 69 67 2d 3e 70 53  e->db, pTrig->pS
2b8d1 63 68 65 6d 61 29 3b 0a 20 20 70 54 72 69 67 2d  chema);.  pTrig-
2b8d2 3e 73 74 65 70 5f 6c 69 73 74 20 3d 20 70 53 74  >step_list = pSt
2b8d3 65 70 4c 69 73 74 3b 0a 20 20 77 68 69 6c 65 28  epList;.  while(
2b8d4 20 70 53 74 65 70 4c 69 73 74 20 29 7b 0a 20 20   pStepList ){.  
2b8d5 20 20 70 53 74 65 70 4c 69 73 74 2d 3e 70 54 72    pStepList->pTr
2b8d6 69 67 20 3d 20 70 54 72 69 67 3b 0a 20 20 20 20  ig = pTrig;.    
2b8d7 70 53 74 65 70 4c 69 73 74 20 3d 20 70 53 74 65  pStepList = pSte
2b8d8 70 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 20  pList->pNext;.  
2b8d9 7d 0a 20 20 6e 61 6d 65 54 6f 6b 65 6e 2e 7a 20  }.  nameToken.z 
2b8da 3d 20 70 54 72 69 67 2d 3e 7a 4e 61 6d 65 3b 0a  = pTrig->zName;.
2b8db 20 20 6e 61 6d 65 54 6f 6b 65 6e 2e 6e 20 3d 20    nameToken.n = 
2b8dc 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
2b8dd 6e 61 6d 65 54 6f 6b 65 6e 2e 7a 29 3b 0a 20 20  nameToken.z);.  
2b8de 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 49 6e  if( sqlite3FixIn
2b8df 69 74 28 26 73 46 69 78 2c 20 70 50 61 72 73 65  it(&sFix, pParse
2b8e0 2c 20 69 44 62 2c 20 22 74 72 69 67 67 65 72 22  , iDb, "trigger"
2b8e1 2c 20 26 6e 61 6d 65 54 6f 6b 65 6e 29 20 0a 20  , &nameToken) . 
2b8e2 20 20 20 20 20 20 20 20 20 26 26 20 73 71 6c 69           && sqli
2b8e3 74 65 33 46 69 78 54 72 69 67 67 65 72 53 74 65  te3FixTriggerSte
2b8e4 70 28 26 73 46 69 78 2c 20 70 54 72 69 67 2d 3e  p(&sFix, pTrig->
2b8e5 73 74 65 70 5f 6c 69 73 74 29 20 29 7b 0a 20 20  step_list) ){.  
2b8e6 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 66 69    goto triggerfi
2b8e7 6e 69 73 68 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  nish_cleanup;.  
2b8e8 7d 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 72  }..  /* if we ar
2b8e9 65 20 6e 6f 74 20 69 6e 69 74 69 61 6c 69 7a 69  e not initializi
2b8ea 6e 67 2c 20 61 6e 64 20 74 68 69 73 20 74 72 69  ng, and this tri
2b8eb 67 67 65 72 20 69 73 20 6e 6f 74 20 6f 6e 20 61  gger is not on a
2b8ec 20 54 45 4d 50 20 74 61 62 6c 65 2c 20 0a 20 20   TEMP table, .  
2b8ed 2a 2a 20 62 75 69 6c 64 20 74 68 65 20 73 71 6c  ** build the sql
2b8ee 69 74 65 5f 6d 61 73 74 65 72 20 65 6e 74 72 79  ite_master entry
2b8ef 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d  .  */.  if( !db-
2b8f0 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20  >init.busy ){.  
2b8f1 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20 63    Vdbe *v;.    c
2b8f2 68 61 72 20 2a 7a 3b 0a 0a 20 20 20 20 2f 2a 20  har *z;..    /* 
2b8f3 4d 61 6b 65 20 61 6e 20 65 6e 74 72 79 20 69 6e  Make an entry in
2b8f4 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74   the sqlite_mast
2b8f5 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  er table */.    
2b8f6 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
2b8f7 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  be(pParse);.    
2b8f8 69 66 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20  if( v==0 ) goto 
2b8f9 74 72 69 67 67 65 72 66 69 6e 69 73 68 5f 63 6c  triggerfinish_cl
2b8fa 65 61 6e 75 70 3b 0a 20 20 20 20 73 71 6c 69 74  eanup;.    sqlit
2b8fb 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
2b8fc 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c  ation(pParse, 0,
2b8fd 20 69 44 62 29 3b 0a 20 20 20 20 7a 20 3d 20 73   iDb);.    z = s
2b8fe 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
2b8ff 64 62 2c 20 28 63 68 61 72 2a 29 70 41 6c 6c 2d  db, (char*)pAll-
2b900 3e 7a 2c 20 70 41 6c 6c 2d 3e 6e 29 3b 0a 20 20  >z, pAll->n);.  
2b901 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50    sqlite3NestedP
2b902 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20  arse(pParse,.   
2b903 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f      "INSERT INTO
2b904 20 25 51 2e 25 73 20 56 41 4c 55 45 53 28 27 74   %Q.%s VALUES('t
2b905 72 69 67 67 65 72 27 2c 25 51 2c 25 51 2c 30 2c  rigger',%Q,%Q,0,
2b906 27 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20  'CREATE TRIGGER 
2b907 25 71 27 29 22 2c 0a 20 20 20 20 20 20 20 64 62  %q')",.       db
2b908 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
2b909 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  , SCHEMA_TABLE(i
2b90a 44 62 29 2c 20 7a 4e 61 6d 65 2c 0a 20 20 20 20  Db), zName,.    
2b90b 20 20 20 70 54 72 69 67 2d 3e 74 61 62 6c 65 2c     pTrig->table,
2b90c 20 7a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   z);.    sqlite3
2b90d 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20  DbFree(db, z);. 
2b90e 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65     sqlite3Change
2b90f 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69  Cookie(pParse, i
2b910 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Db);.    sqlite3
2b911 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2b912 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44  _ParseSchema, iD
2b913 62 2c 20 30 2c 20 30 2c 20 73 71 6c 69 74 65 33  b, 0, 0, sqlite3
2b914 4d 50 72 69 6e 74 66 28 0a 20 20 20 20 20 20 20  MPrintf(.       
2b915 20 64 62 2c 20 22 74 79 70 65 3d 27 74 72 69 67   db, "type='trig
2b916 67 65 72 27 20 41 4e 44 20 6e 61 6d 65 3d 27 25  ger' AND name='%
2b917 71 27 22 2c 20 7a 4e 61 6d 65 29 2c 20 50 34 5f  q'", zName), P4_
2b918 44 59 4e 41 4d 49 43 0a 20 20 20 20 29 3b 0a 20  DYNAMIC.    );. 
2b919 20 7d 0a 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e   }..  if( db->in
2b91a 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 54  it.busy ){.    T
2b91b 72 69 67 67 65 72 20 2a 70 4c 69 6e 6b 20 3d 20  rigger *pLink = 
2b91c 70 54 72 69 67 3b 0a 20 20 20 20 48 61 73 68 20  pTrig;.    Hash 
2b91d 2a 70 48 61 73 68 20 3d 20 26 64 62 2d 3e 61 44  *pHash = &db->aD
2b91e 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e  b[iDb].pSchema->
2b91f 74 72 69 67 48 61 73 68 3b 0a 20 20 20 20 70 54  trigHash;.    pT
2b920 72 69 67 20 3d 20 73 71 6c 69 74 65 33 48 61 73  rig = sqlite3Has
2b921 68 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 7a  hInsert(pHash, z
2b922 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72  Name, sqlite3Str
2b923 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 2c 20 70 54  len30(zName), pT
2b924 72 69 67 29 3b 0a 20 20 20 20 69 66 28 20 70 54  rig);.    if( pT
2b925 72 69 67 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  rig ){.      db-
2b926 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
2b927 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  1;.    }else if(
2b928 20 70 4c 69 6e 6b 2d 3e 70 53 63 68 65 6d 61 3d   pLink->pSchema=
2b929 3d 70 4c 69 6e 6b 2d 3e 70 54 61 62 53 63 68 65  =pLink->pTabSche
2b92a 6d 61 20 29 7b 0a 20 20 20 20 20 20 54 61 62 6c  ma ){.      Tabl
2b92b 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 20 20 69  e *pTab;.      i
2b92c 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74  nt n = sqlite3St
2b92d 72 6c 65 6e 33 30 28 70 4c 69 6e 6b 2d 3e 74 61  rlen30(pLink->ta
2b92e 62 6c 65 29 3b 0a 20 20 20 20 20 20 70 54 61 62  ble);.      pTab
2b92f 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 69   = sqlite3HashFi
2b930 6e 64 28 26 70 4c 69 6e 6b 2d 3e 70 54 61 62 53  nd(&pLink->pTabS
2b931 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20  chema->tblHash, 
2b932 70 4c 69 6e 6b 2d 3e 74 61 62 6c 65 2c 20 6e 29  pLink->table, n)
2b933 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2b934 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 20 20 20  pTab!=0 );.     
2b935 20 70 4c 69 6e 6b 2d 3e 70 4e 65 78 74 20 3d 20   pLink->pNext = 
2b936 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72 3b 0a  pTab->pTrigger;.
2b937 20 20 20 20 20 20 70 54 61 62 2d 3e 70 54 72 69        pTab->pTri
2b938 67 67 65 72 20 3d 20 70 4c 69 6e 6b 3b 0a 20 20  gger = pLink;.  
2b939 20 20 7d 0a 20 20 7d 0a 0a 74 72 69 67 67 65 72    }.  }..trigger
2b93a 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70 3a 0a  finish_cleanup:.
2b93b 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
2b93c 72 69 67 67 65 72 28 64 62 2c 20 70 54 72 69 67  rigger(db, pTrig
2b93d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 50  );.  assert( !pP
2b93e 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65  arse->pNewTrigge
2b93f 72 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65  r );.  sqlite3De
2b940 6c 65 74 65 54 72 69 67 67 65 72 53 74 65 70 28  leteTriggerStep(
2b941 64 62 2c 20 70 53 74 65 70 4c 69 73 74 29 3b 0a  db, pStepList);.
2b942 7d 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20  }../*.** Turn a 
2b943 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2b944 20 28 74 68 61 74 20 74 68 65 20 70 53 65 6c 65   (that the pSele
2b945 63 74 20 70 61 72 61 6d 65 74 65 72 20 70 6f 69  ct parameter poi
2b946 6e 74 73 20 74 6f 29 20 69 6e 74 6f 0a 2a 2a 20  nts to) into.** 
2b947 61 20 74 72 69 67 67 65 72 20 73 74 65 70 2e 20  a trigger step. 
2b948 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
2b949 72 20 74 6f 20 61 20 54 72 69 67 67 65 72 53 74  r to a TriggerSt
2b94a 65 70 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ep structure..**
2b94b 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63  .** The parser c
2b94c 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e  alls this routin
2b94d 65 20 77 68 65 6e 20 69 74 20 66 69 6e 64 73 20  e when it finds 
2b94e 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
2b94f 6e 74 20 69 6e 0a 2a 2a 20 62 6f 64 79 20 6f 66  nt in.** body of
2b950 20 61 20 54 52 49 47 47 45 52 2e 20 20 0a 2a 2f   a TRIGGER.  .*/
2b951 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2b952 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c  TriggerStep *sql
2b953 69 74 65 33 54 72 69 67 67 65 72 53 65 6c 65 63  ite3TriggerSelec
2b954 74 53 74 65 70 28 73 71 6c 69 74 65 33 20 2a 64  tStep(sqlite3 *d
2b955 62 2c 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  b, Select *pSele
2b956 63 74 29 7b 0a 20 20 54 72 69 67 67 65 72 53 74  ct){.  TriggerSt
2b957 65 70 20 2a 70 54 72 69 67 67 65 72 53 74 65 70  ep *pTriggerStep
2b958 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
2b959 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
2b95a 66 28 54 72 69 67 67 65 72 53 74 65 70 29 29 3b  f(TriggerStep));
2b95b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72 53  .  if( pTriggerS
2b95c 74 65 70 3d 3d 30 20 29 20 7b 0a 20 20 20 20 73  tep==0 ) {.    s
2b95d 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
2b95e 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b  te(db, pSelect);
2b95f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
2b960 20 7d 0a 20 20 70 54 72 69 67 67 65 72 53 74 65   }.  pTriggerSte
2b961 70 2d 3e 6f 70 20 3d 20 54 4b 5f 53 45 4c 45 43  p->op = TK_SELEC
2b962 54 3b 0a 20 20 70 54 72 69 67 67 65 72 53 74 65  T;.  pTriggerSte
2b963 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20 70 53 65  p->pSelect = pSe
2b964 6c 65 63 74 3b 0a 20 20 70 54 72 69 67 67 65 72  lect;.  pTrigger
2b965 53 74 65 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20 4f  Step->orconf = O
2b966 45 5f 44 65 66 61 75 6c 74 3b 0a 20 20 72 65 74  E_Default;.  ret
2b967 75 72 6e 20 70 54 72 69 67 67 65 72 53 74 65 70  urn pTriggerStep
2b968 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  ;.}../*.** Alloc
2b969 61 74 65 20 73 70 61 63 65 20 74 6f 20 68 6f 6c  ate space to hol
2b96a 64 20 61 20 6e 65 77 20 74 72 69 67 67 65 72 20  d a new trigger 
2b96b 73 74 65 70 2e 20 20 54 68 65 20 61 6c 6c 6f 63  step.  The alloc
2b96c 61 74 65 64 20 73 70 61 63 65 0a 2a 2a 20 68 6f  ated space.** ho
2b96d 6c 64 73 20 62 6f 74 68 20 74 68 65 20 54 72 69  lds both the Tri
2b96e 67 67 65 72 53 74 65 70 20 6f 62 6a 65 63 74 20  ggerStep object 
2b96f 61 6e 64 20 74 68 65 20 54 72 69 67 67 65 72 53  and the TriggerS
2b970 74 65 70 2e 74 61 72 67 65 74 2e 7a 20 73 74 72  tep.target.z str
2b971 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ing..**.** If an
2b972 20 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63 75 72   OOM error occur
2b973 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72  s, NULL is retur
2b974 6e 65 64 20 61 6e 64 20 64 62 2d 3e 6d 61 6c 6c  ned and db->mall
2b975 6f 63 46 61 69 6c 65 64 20 69 73 20 73 65 74 2e  ocFailed is set.
2b976 0a 2a 2f 0a 73 74 61 74 69 63 20 54 72 69 67 67  .*/.static Trigg
2b977 65 72 53 74 65 70 20 2a 74 72 69 67 67 65 72 53  erStep *triggerS
2b978 74 65 70 41 6c 6c 6f 63 61 74 65 28 0a 20 20 73  tepAllocate(.  s
2b979 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
2b97a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
2b97b 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2b97c 6e 20 2a 2f 0a 20 20 75 38 20 6f 70 2c 20 20 20  n */.  u8 op,   
2b97d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b97e 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 6f 70     /* Trigger op
2b97f 63 6f 64 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  code */.  Token 
2b980 2a 70 4e 61 6d 65 20 20 20 20 20 20 20 20 20 20  *pName          
2b981 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 72        /* The tar
2b982 67 65 74 20 6e 61 6d 65 20 2a 2f 0a 29 7b 0a 20  get name */.){. 
2b983 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 54   TriggerStep *pT
2b984 72 69 67 67 65 72 53 74 65 70 3b 0a 0a 20 20 70  riggerStep;..  p
2b985 54 72 69 67 67 65 72 53 74 65 70 20 3d 20 73 71  TriggerStep = sq
2b986 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
2b987 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 72 69  o(db, sizeof(Tri
2b988 67 67 65 72 53 74 65 70 29 20 2b 20 70 4e 61 6d  ggerStep) + pNam
2b989 65 2d 3e 6e 29 3b 0a 20 20 69 66 28 20 70 54 72  e->n);.  if( pTr
2b98a 69 67 67 65 72 53 74 65 70 20 29 7b 0a 20 20 20  iggerStep ){.   
2b98b 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 68 61 72   char *z = (char
2b98c 2a 29 26 70 54 72 69 67 67 65 72 53 74 65 70 5b  *)&pTriggerStep[
2b98d 31 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a  1];.    memcpy(z
2b98e 2c 20 70 4e 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d  , pName->z, pNam
2b98f 65 2d 3e 6e 29 3b 0a 20 20 20 20 70 54 72 69 67  e->n);.    pTrig
2b990 67 65 72 53 74 65 70 2d 3e 74 61 72 67 65 74 2e  gerStep->target.
2b991 7a 20 3d 20 7a 3b 0a 20 20 20 20 70 54 72 69 67  z = z;.    pTrig
2b992 67 65 72 53 74 65 70 2d 3e 74 61 72 67 65 74 2e  gerStep->target.
2b993 6e 20 3d 20 70 4e 61 6d 65 2d 3e 6e 3b 0a 20 20  n = pName->n;.  
2b994 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e    pTriggerStep->
2b995 6f 70 20 3d 20 6f 70 3b 0a 20 20 7d 0a 20 20 72  op = op;.  }.  r
2b996 65 74 75 72 6e 20 70 54 72 69 67 67 65 72 53 74  eturn pTriggerSt
2b997 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 75 69  ep;.}../*.** Bui
2b998 6c 64 20 61 20 74 72 69 67 67 65 72 20 73 74 65  ld a trigger ste
2b999 70 20 6f 75 74 20 6f 66 20 61 6e 20 49 4e 53 45  p out of an INSE
2b99a 52 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 52  RT statement.  R
2b99b 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a  eturn a pointer.
2b99c 2a 2a 20 74 6f 20 74 68 65 20 6e 65 77 20 74 72  ** to the new tr
2b99d 69 67 67 65 72 20 73 74 65 70 2e 0a 2a 2a 0a 2a  igger step..**.*
2b99e 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c  * The parser cal
2b99f 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
2b9a0 77 68 65 6e 20 69 74 20 73 65 65 73 20 61 6e 20  when it sees an 
2b9a1 49 4e 53 45 52 54 20 69 6e 73 69 64 65 20 74 68  INSERT inside th
2b9a2 65 0a 2a 2a 20 62 6f 64 79 20 6f 66 20 61 20 74  e.** body of a t
2b9a3 72 69 67 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  rigger..*/.SQLIT
2b9a4 45 5f 50 52 49 56 41 54 45 20 54 72 69 67 67 65  E_PRIVATE Trigge
2b9a5 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72  rStep *sqlite3Tr
2b9a6 69 67 67 65 72 49 6e 73 65 72 74 53 74 65 70 28  iggerInsertStep(
2b9a7 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
2b9a8 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
2b9a9 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2b9aa 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54  n */.  Token *pT
2b9ab 61 62 6c 65 4e 61 6d 65 2c 20 20 2f 2a 20 4e 61  ableName,  /* Na
2b9ac 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
2b9ad 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20 69 6e  into which we in
2b9ae 73 65 72 74 20 2a 2f 0a 20 20 49 64 4c 69 73 74  sert */.  IdList
2b9af 20 2a 70 43 6f 6c 75 6d 6e 2c 20 20 20 20 2f 2a   *pColumn,    /*
2b9b0 20 4c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73   List of columns
2b9b1 20 69 6e 20 70 54 61 62 6c 65 4e 61 6d 65 20 74   in pTableName t
2b9b2 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 2a 2f  o insert into */
2b9b3 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
2b9b4 69 73 74 2c 20 20 20 2f 2a 20 54 68 65 20 56 41  ist,   /* The VA
2b9b5 4c 55 45 20 63 6c 61 75 73 65 3a 20 61 20 6c 69  LUE clause: a li
2b9b6 73 74 20 6f 66 20 76 61 6c 75 65 73 20 74 6f 20  st of values to 
2b9b7 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20  be inserted */. 
2b9b8 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
2b9b9 2c 20 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54  ,    /* A SELECT
2b9ba 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
2b9bb 73 75 70 70 6c 69 65 73 20 76 61 6c 75 65 73 20  supplies values 
2b9bc 2a 2f 0a 20 20 75 38 20 6f 72 63 6f 6e 66 20 20  */.  u8 orconf  
2b9bd 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2b9be 63 6f 6e 66 6c 69 63 74 20 61 6c 67 6f 72 69 74  conflict algorit
2b9bf 68 6d 20 28 4f 45 5f 41 62 6f 72 74 2c 20 4f 45  hm (OE_Abort, OE
2b9c0 5f 52 65 70 6c 61 63 65 2c 20 65 74 63 2e 29 20  _Replace, etc.) 
2b9c1 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 53  */.){.  TriggerS
2b9c2 74 65 70 20 2a 70 54 72 69 67 67 65 72 53 74 65  tep *pTriggerSte
2b9c3 70 3b 0a 0a 20 20 61 73 73 65 72 74 28 70 45 4c  p;..  assert(pEL
2b9c4 69 73 74 20 3d 3d 20 30 20 7c 7c 20 70 53 65 6c  ist == 0 || pSel
2b9c5 65 63 74 20 3d 3d 20 30 29 3b 0a 20 20 61 73 73  ect == 0);.  ass
2b9c6 65 72 74 28 70 45 4c 69 73 74 20 21 3d 20 30 20  ert(pEList != 0 
2b9c7 7c 7c 20 70 53 65 6c 65 63 74 20 21 3d 20 30 20  || pSelect != 0 
2b9c8 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
2b9c9 6c 65 64 29 3b 0a 0a 20 20 70 54 72 69 67 67 65  led);..  pTrigge
2b9ca 72 53 74 65 70 20 3d 20 74 72 69 67 67 65 72 53  rStep = triggerS
2b9cb 74 65 70 41 6c 6c 6f 63 61 74 65 28 64 62 2c 20  tepAllocate(db, 
2b9cc 54 4b 5f 49 4e 53 45 52 54 2c 20 70 54 61 62 6c  TK_INSERT, pTabl
2b9cd 65 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 54  eName);.  if( pT
2b9ce 72 69 67 67 65 72 53 74 65 70 20 29 7b 0a 20 20  riggerStep ){.  
2b9cf 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e    pTriggerStep->
2b9d0 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65  pSelect = sqlite
2b9d1 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70  3SelectDup(db, p
2b9d2 53 65 6c 65 63 74 2c 20 45 58 50 52 44 55 50 5f  Select, EXPRDUP_
2b9d3 52 45 44 55 43 45 29 3b 0a 20 20 20 20 70 54 72  REDUCE);.    pTr
2b9d4 69 67 67 65 72 53 74 65 70 2d 3e 70 49 64 4c 69  iggerStep->pIdLi
2b9d5 73 74 20 3d 20 70 43 6f 6c 75 6d 6e 3b 0a 20 20  st = pColumn;.  
2b9d6 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e    pTriggerStep->
2b9d7 70 45 78 70 72 4c 69 73 74 20 3d 20 73 71 6c 69  pExprList = sqli
2b9d8 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64  te3ExprListDup(d
2b9d9 62 2c 20 70 45 4c 69 73 74 2c 20 45 58 50 52 44  b, pEList, EXPRD
2b9da 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20 20  UP_REDUCE);.    
2b9db 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 72  pTriggerStep->or
2b9dc 63 6f 6e 66 20 3d 20 6f 72 63 6f 6e 66 3b 0a 20  conf = orconf;. 
2b9dd 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
2b9de 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28  te3IdListDelete(
2b9df 64 62 2c 20 70 43 6f 6c 75 6d 6e 29 3b 0a 20 20  db, pColumn);.  
2b9e0 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  }.  sqlite3ExprL
2b9e1 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 45  istDelete(db, pE
2b9e2 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  List);.  sqlite3
2b9e3 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
2b9e4 20 70 53 65 6c 65 63 74 29 3b 0a 0a 20 20 72 65   pSelect);..  re
2b9e5 74 75 72 6e 20 70 54 72 69 67 67 65 72 53 74 65  turn pTriggerSte
2b9e6 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73  p;.}../*.** Cons
2b9e7 74 72 75 63 74 20 61 20 74 72 69 67 67 65 72 20  truct a trigger 
2b9e8 73 74 65 70 20 74 68 61 74 20 69 6d 70 6c 65 6d  step that implem
2b9e9 65 6e 74 73 20 61 6e 20 55 50 44 41 54 45 20 73  ents an UPDATE s
2b9ea 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65 74  tatement and ret
2b9eb 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72  urn.** a pointer
2b9ec 20 74 6f 20 74 68 61 74 20 74 72 69 67 67 65 72   to that trigger
2b9ed 20 73 74 65 70 2e 20 20 54 68 65 20 70 61 72 73   step.  The pars
2b9ee 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f  er calls this ro
2b9ef 75 74 69 6e 65 20 77 68 65 6e 20 69 74 0a 2a 2a  utine when it.**
2b9f0 20 73 65 65 73 20 61 6e 20 55 50 44 41 54 45 20   sees an UPDATE 
2b9f1 73 74 61 74 65 6d 65 6e 74 20 69 6e 73 69 64 65  statement inside
2b9f2 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 43   the body of a C
2b9f3 52 45 41 54 45 20 54 52 49 47 47 45 52 2e 0a 2a  REATE TRIGGER..*
2b9f4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2b9f5 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71   TriggerStep *sq
2b9f6 6c 69 74 65 33 54 72 69 67 67 65 72 55 70 64 61  lite3TriggerUpda
2b9f7 74 65 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65  teStep(.  sqlite
2b9f8 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 2f  3 *db,         /
2b9f9 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
2b9fa 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54  onnection */.  T
2b9fb 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65  oken *pTableName
2b9fc 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74  ,   /* Name of t
2b9fd 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 75  he table to be u
2b9fe 70 64 61 74 65 64 20 2a 2f 0a 20 20 45 78 70 72  pdated */.  Expr
2b9ff 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 20  List *pEList,   
2ba00 20 2f 2a 20 54 68 65 20 53 45 54 20 63 6c 61 75   /* The SET clau
2ba01 73 65 3a 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75  se: list of colu
2ba02 6d 6e 20 61 6e 64 20 6e 65 77 20 76 61 6c 75 65  mn and new value
2ba03 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  s */.  Expr *pWh
2ba04 65 72 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 54  ere,        /* T
2ba05 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2ba06 2a 2f 0a 20 20 75 38 20 6f 72 63 6f 6e 66 20 20  */.  u8 orconf  
2ba07 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2ba08 20 63 6f 6e 66 6c 69 63 74 20 61 6c 67 6f 72 69   conflict algori
2ba09 74 68 6d 2e 20 28 4f 45 5f 41 62 6f 72 74 2c 20  thm. (OE_Abort, 
2ba0a 4f 45 5f 49 67 6e 6f 72 65 2c 20 65 74 63 29 20  OE_Ignore, etc) 
2ba0b 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 53  */.){.  TriggerS
2ba0c 74 65 70 20 2a 70 54 72 69 67 67 65 72 53 74 65  tep *pTriggerSte
2ba0d 70 3b 0a 0a 20 20 70 54 72 69 67 67 65 72 53 74  p;..  pTriggerSt
2ba0e 65 70 20 3d 20 74 72 69 67 67 65 72 53 74 65 70  ep = triggerStep
2ba0f 41 6c 6c 6f 63 61 74 65 28 64 62 2c 20 54 4b 5f  Allocate(db, TK_
2ba10 55 50 44 41 54 45 2c 20 70 54 61 62 6c 65 4e 61  UPDATE, pTableNa
2ba11 6d 65 29 3b 0a 20 20 69 66 28 20 70 54 72 69 67  me);.  if( pTrig
2ba12 67 65 72 53 74 65 70 20 29 7b 0a 20 20 20 20 70  gerStep ){.    p
2ba13 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 45 78  TriggerStep->pEx
2ba14 70 72 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33  prList = sqlite3
2ba15 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20  ExprListDup(db, 
2ba16 70 45 4c 69 73 74 2c 20 45 58 50 52 44 55 50 5f  pEList, EXPRDUP_
2ba17 52 45 44 55 43 45 29 3b 0a 20 20 20 20 70 54 72  REDUCE);.    pTr
2ba18 69 67 67 65 72 53 74 65 70 2d 3e 70 57 68 65 72  iggerStep->pWher
2ba19 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  e = sqlite3ExprD
2ba1a 75 70 28 64 62 2c 20 70 57 68 65 72 65 2c 20 45  up(db, pWhere, E
2ba1b 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a  XPRDUP_REDUCE);.
2ba1c 20 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70      pTriggerStep
2ba1d 2d 3e 6f 72 63 6f 6e 66 20 3d 20 6f 72 63 6f 6e  ->orconf = orcon
2ba1e 66 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  f;.  }.  sqlite3
2ba1f 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64  ExprListDelete(d
2ba20 62 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 71  b, pEList);.  sq
2ba21 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
2ba22 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 72  db, pWhere);.  r
2ba23 65 74 75 72 6e 20 70 54 72 69 67 67 65 72 53 74  eturn pTriggerSt
2ba24 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  ep;.}../*.** Con
2ba25 73 74 72 75 63 74 20 61 20 74 72 69 67 67 65 72  struct a trigger
2ba26 20 73 74 65 70 20 74 68 61 74 20 69 6d 70 6c 65   step that imple
2ba27 6d 65 6e 74 73 20 61 20 44 45 4c 45 54 45 20 73  ments a DELETE s
2ba28 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65 74  tatement and ret
2ba29 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72  urn.** a pointer
2ba2a 20 74 6f 20 74 68 61 74 20 74 72 69 67 67 65 72   to that trigger
2ba2b 20 73 74 65 70 2e 20 20 54 68 65 20 70 61 72 73   step.  The pars
2ba2c 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f  er calls this ro
2ba2d 75 74 69 6e 65 20 77 68 65 6e 20 69 74 0a 2a 2a  utine when it.**
2ba2e 20 73 65 65 73 20 61 20 44 45 4c 45 54 45 20 73   sees a DELETE s
2ba2f 74 61 74 65 6d 65 6e 74 20 69 6e 73 69 64 65 20  tatement inside 
2ba30 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 43 52  the body of a CR
2ba31 45 41 54 45 20 54 52 49 47 47 45 52 2e 0a 2a 2f  EATE TRIGGER..*/
2ba32 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2ba33 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c  TriggerStep *sql
2ba34 69 74 65 33 54 72 69 67 67 65 72 44 65 6c 65 74  ite3TriggerDelet
2ba35 65 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65 33  eStep(.  sqlite3
2ba36 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
2ba37 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
2ba38 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b  nection */.  Tok
2ba39 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c 20  en *pTableName, 
2ba3a 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
2ba3b 65 20 66 72 6f 6d 20 77 68 69 63 68 20 72 6f 77  e from which row
2ba3c 73 20 61 72 65 20 64 65 6c 65 74 65 64 20 2a 2f  s are deleted */
2ba3d 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 20  .  Expr *pWhere 
2ba3e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2ba3f 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  e WHERE clause *
2ba40 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 53 74  /.){.  TriggerSt
2ba41 65 70 20 2a 70 54 72 69 67 67 65 72 53 74 65 70  ep *pTriggerStep
2ba42 3b 0a 0a 20 20 70 54 72 69 67 67 65 72 53 74 65  ;..  pTriggerSte
2ba43 70 20 3d 20 74 72 69 67 67 65 72 53 74 65 70 41  p = triggerStepA
2ba44 6c 6c 6f 63 61 74 65 28 64 62 2c 20 54 4b 5f 44  llocate(db, TK_D
2ba45 45 4c 45 54 45 2c 20 70 54 61 62 6c 65 4e 61 6d  ELETE, pTableNam
2ba46 65 29 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67  e);.  if( pTrigg
2ba47 65 72 53 74 65 70 20 29 7b 0a 20 20 20 20 70 54  erStep ){.    pT
2ba48 72 69 67 67 65 72 53 74 65 70 2d 3e 70 57 68 65  riggerStep->pWhe
2ba49 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  re = sqlite3Expr
2ba4a 44 75 70 28 64 62 2c 20 70 57 68 65 72 65 2c 20  Dup(db, pWhere, 
2ba4b 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b  EXPRDUP_REDUCE);
2ba4c 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74 65  .    pTriggerSte
2ba4d 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20 4f 45 5f 44  p->orconf = OE_D
2ba4e 65 66 61 75 6c 74 3b 0a 20 20 7d 0a 20 20 73 71  efault;.  }.  sq
2ba4f 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
2ba50 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 72  db, pWhere);.  r
2ba51 65 74 75 72 6e 20 70 54 72 69 67 67 65 72 53 74  eturn pTriggerSt
2ba52 65 70 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65  ep;.}../* .** Re
2ba53 63 75 72 73 69 76 65 6c 79 20 64 65 6c 65 74 65  cursively delete
2ba54 20 61 20 54 72 69 67 67 65 72 20 73 74 72 75 63   a Trigger struc
2ba55 74 75 72 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ture.*/.SQLITE_P
2ba56 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2ba57 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72  te3DeleteTrigger
2ba58 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 72  (sqlite3 *db, Tr
2ba59 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 29  igger *pTrigger)
2ba5a 7b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72  {.  if( pTrigger
2ba5b 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
2ba5c 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
2ba5d 67 67 65 72 53 74 65 70 28 64 62 2c 20 70 54 72  ggerStep(db, pTr
2ba5e 69 67 67 65 72 2d 3e 73 74 65 70 5f 6c 69 73 74  igger->step_list
2ba5f 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
2ba60 65 65 28 64 62 2c 20 70 54 72 69 67 67 65 72 2d  ee(db, pTrigger-
2ba61 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74  >zName);.  sqlit
2ba62 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 72  e3DbFree(db, pTr
2ba63 69 67 67 65 72 2d 3e 74 61 62 6c 65 29 3b 0a 20  igger->table);. 
2ba64 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2ba65 74 65 28 64 62 2c 20 70 54 72 69 67 67 65 72 2d  te(db, pTrigger-
2ba66 3e 70 57 68 65 6e 29 3b 0a 20 20 73 71 6c 69 74  >pWhen);.  sqlit
2ba67 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64  e3IdListDelete(d
2ba68 62 2c 20 70 54 72 69 67 67 65 72 2d 3e 70 43 6f  b, pTrigger->pCo
2ba69 6c 75 6d 6e 73 29 3b 0a 20 20 73 71 6c 69 74 65  lumns);.  sqlite
2ba6a 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 72 69  3DbFree(db, pTri
2ba6b 67 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  gger);.}../*.** 
2ba6c 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
2ba6d 20 63 61 6c 6c 65 64 20 74 6f 20 64 72 6f 70 20   called to drop 
2ba6e 61 20 74 72 69 67 67 65 72 20 66 72 6f 6d 20 74  a trigger from t
2ba6f 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
2ba70 6d 61 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ma. .**.** This 
2ba71 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 64 69  may be called di
2ba72 72 65 63 74 6c 79 20 66 72 6f 6d 20 74 68 65 20  rectly from the 
2ba73 70 61 72 73 65 72 20 61 6e 64 20 74 68 65 72 65  parser and there
2ba74 66 6f 72 65 20 69 64 65 6e 74 69 66 69 65 73 0a  fore identifies.
2ba75 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72 20 62  ** the trigger b
2ba76 79 20 6e 61 6d 65 2e 20 20 54 68 65 20 73 71 6c  y name.  The sql
2ba77 69 74 65 33 44 72 6f 70 54 72 69 67 67 65 72 50  ite3DropTriggerP
2ba78 74 72 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65  tr() routine doe
2ba79 73 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 6a 6f  s the.** same jo
2ba7a 62 20 61 73 20 74 68 69 73 20 72 6f 75 74 69 6e  b as this routin
2ba7b 65 20 65 78 63 65 70 74 20 69 74 20 74 61 6b 65  e except it take
2ba7c 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
2ba7d 68 65 20 74 72 69 67 67 65 72 0a 2a 2a 20 69 6e  he trigger.** in
2ba7e 73 74 65 61 64 20 6f 66 20 74 68 65 20 74 72 69  stead of the tri
2ba7f 67 67 65 72 20 6e 61 6d 65 2e 0a 2a 2a 2f 0a 53  gger name..**/.S
2ba80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2ba81 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72  id sqlite3DropTr
2ba82 69 67 67 65 72 28 50 61 72 73 65 20 2a 70 50 61  igger(Parse *pPa
2ba83 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e  rse, SrcList *pN
2ba84 61 6d 65 2c 20 69 6e 74 20 6e 6f 45 72 72 29 7b  ame, int noErr){
2ba85 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
2ba86 67 67 65 72 20 3d 20 30 3b 0a 20 20 69 6e 74 20  gger = 0;.  int 
2ba87 69 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  i;.  const char 
2ba88 2a 7a 44 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68  *zDb;.  const ch
2ba89 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69 6e 74  ar *zName;.  int
2ba8a 20 6e 4e 61 6d 65 3b 0a 20 20 73 71 6c 69 74 65   nName;.  sqlite
2ba8b 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2ba8c 64 62 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e 6d  db;..  if( db->m
2ba8d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f  allocFailed ) go
2ba8e 74 6f 20 64 72 6f 70 5f 74 72 69 67 67 65 72 5f  to drop_trigger_
2ba8f 63 6c 65 61 6e 75 70 3b 0a 20 20 69 66 28 20 53  cleanup;.  if( S
2ba90 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65  QLITE_OK!=sqlite
2ba91 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
2ba92 73 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  se) ){.    goto 
2ba93 64 72 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c 65  drop_trigger_cle
2ba94 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  anup;.  }..  ass
2ba95 65 72 74 28 20 70 4e 61 6d 65 2d 3e 6e 53 72 63  ert( pName->nSrc
2ba96 3d 3d 31 20 29 3b 0a 20 20 7a 44 62 20 3d 20 70  ==1 );.  zDb = p
2ba97 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61  Name->a[0].zData
2ba98 62 61 73 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20  base;.  zName = 
2ba99 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d  pName->a[0].zNam
2ba9a 65 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c  e;.  nName = sql
2ba9b 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61  ite3Strlen30(zNa
2ba9c 6d 65 29 3b 0a 20 20 66 6f 72 28 69 3d 4f 4d 49  me);.  for(i=OMI
2ba9d 54 5f 54 45 4d 50 44 42 3b 20 69 3c 64 62 2d 3e  T_TEMPDB; i<db->
2ba9e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  nDb; i++){.    i
2ba9f 6e 74 20 6a 20 3d 20 28 69 3c 32 29 20 3f 20 69  nt j = (i<2) ? i
2baa0 5e 31 20 3a 20 69 3b 20 20 2f 2a 20 53 65 61 72  ^1 : i;  /* Sear
2baa1 63 68 20 54 45 4d 50 20 62 65 66 6f 72 65 20 4d  ch TEMP before M
2baa2 41 49 4e 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a  AIN */.    if( z
2baa3 44 62 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  Db && sqlite3Str
2baa4 49 43 6d 70 28 64 62 2d 3e 61 44 62 5b 6a 5d 2e  ICmp(db->aDb[j].
2baa5 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 20 63 6f  zName, zDb) ) co
2baa6 6e 74 69 6e 75 65 3b 0a 20 20 20 20 70 54 72 69  ntinue;.    pTri
2baa7 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 48 61  gger = sqlite3Ha
2baa8 73 68 46 69 6e 64 28 26 28 64 62 2d 3e 61 44 62  shFind(&(db->aDb
2baa9 5b 6a 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72 69  [j].pSchema->tri
2baaa 67 48 61 73 68 29 2c 20 7a 4e 61 6d 65 2c 20 6e  gHash), zName, n
2baab 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70  Name);.    if( p
2baac 54 72 69 67 67 65 72 20 29 20 62 72 65 61 6b 3b  Trigger ) break;
2baad 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 54 72 69  .  }.  if( !pTri
2baae 67 67 65 72 20 29 7b 0a 20 20 20 20 69 66 28 20  gger ){.    if( 
2baaf 21 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20 20 20  !noErr ){.      
2bab0 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2bab1 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68  pParse, "no such
2bab2 20 74 72 69 67 67 65 72 3a 20 25 53 22 2c 20 70   trigger: %S", p
2bab3 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  Name, 0);.    }.
2bab4 20 20 20 20 67 6f 74 6f 20 64 72 6f 70 5f 74 72      goto drop_tr
2bab5 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20  igger_cleanup;. 
2bab6 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 72 6f 70   }.  sqlite3Drop
2bab7 54 72 69 67 67 65 72 50 74 72 28 70 50 61 72 73  TriggerPtr(pPars
2bab8 65 2c 20 70 54 72 69 67 67 65 72 29 3b 0a 0a 64  e, pTrigger);..d
2bab9 72 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c 65 61  rop_trigger_clea
2baba 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 53 72  nup:.  sqlite3Sr
2babb 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  cListDelete(db, 
2babc 70 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pName);.}../*.**
2babd 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
2babe 72 20 74 6f 20 74 68 65 20 54 61 62 6c 65 20 73  r to the Table s
2babf 74 72 75 63 74 75 72 65 20 66 6f 72 20 74 68 65  tructure for the
2bac0 20 74 61 62 6c 65 20 74 68 61 74 20 61 20 74 72   table that a tr
2bac1 69 67 67 65 72 0a 2a 2a 20 69 73 20 73 65 74 20  igger.** is set 
2bac2 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54 61  on..*/.static Ta
2bac3 62 6c 65 20 2a 74 61 62 6c 65 4f 66 54 72 69 67  ble *tableOfTrig
2bac4 67 65 72 28 54 72 69 67 67 65 72 20 2a 70 54 72  ger(Trigger *pTr
2bac5 69 67 67 65 72 29 7b 0a 20 20 69 6e 74 20 6e 20  igger){.  int n 
2bac6 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
2bac7 30 28 70 54 72 69 67 67 65 72 2d 3e 74 61 62 6c  0(pTrigger->tabl
2bac8 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  e);.  return sql
2bac9 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 70 54  ite3HashFind(&pT
2baca 72 69 67 67 65 72 2d 3e 70 54 61 62 53 63 68 65  rigger->pTabSche
2bacb 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70 54 72  ma->tblHash, pTr
2bacc 69 67 67 65 72 2d 3e 74 61 62 6c 65 2c 20 6e 29  igger->table, n)
2bacd 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70  ;.}.../*.** Drop
2bace 20 61 20 74 72 69 67 67 65 72 20 67 69 76 65 6e   a trigger given
2bacf 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
2bad0 61 74 20 74 72 69 67 67 65 72 2e 20 0a 2a 2f 0a  at trigger. .*/.
2bad1 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2bad2 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54  oid sqlite3DropT
2bad3 72 69 67 67 65 72 50 74 72 28 50 61 72 73 65 20  riggerPtr(Parse 
2bad4 2a 70 50 61 72 73 65 2c 20 54 72 69 67 67 65 72  *pParse, Trigger
2bad5 20 2a 70 54 72 69 67 67 65 72 29 7b 0a 20 20 54   *pTrigger){.  T
2bad6 61 62 6c 65 20 20 20 2a 70 54 61 62 6c 65 3b 0a  able   *pTable;.
2bad7 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c    Vdbe *v;.  sql
2bad8 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2bad9 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62  e->db;.  int iDb
2bada 3b 0a 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74  ;..  iDb = sqlit
2badb 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
2badc 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 72 69  pParse->db, pTri
2badd 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  gger->pSchema);.
2bade 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30    assert( iDb>=0
2badf 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20   && iDb<db->nDb 
2bae0 29 3b 0a 20 20 70 54 61 62 6c 65 20 3d 20 74 61  );.  pTable = ta
2bae1 62 6c 65 4f 66 54 72 69 67 67 65 72 28 70 54 72  bleOfTrigger(pTr
2bae2 69 67 67 65 72 29 3b 0a 20 20 61 73 73 65 72 74  igger);.  assert
2bae3 28 20 70 54 61 62 6c 65 20 29 3b 0a 20 20 61 73  ( pTable );.  as
2bae4 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 70 53  sert( pTable->pS
2bae5 63 68 65 6d 61 3d 3d 70 54 72 69 67 67 65 72 2d  chema==pTrigger-
2bae6 3e 70 53 63 68 65 6d 61 20 7c 7c 20 69 44 62 3d  >pSchema || iDb=
2bae7 3d 31 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  =1 );.#ifndef SQ
2bae8 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
2bae9 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20  IZATION.  {.    
2baea 69 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c 49 54  int code = SQLIT
2baeb 45 5f 44 52 4f 50 5f 54 52 49 47 47 45 52 3b 0a  E_DROP_TRIGGER;.
2baec 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2baed 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  zDb = db->aDb[iD
2baee 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f  b].zName;.    co
2baef 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20 3d  nst char *zTab =
2baf0 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44   SCHEMA_TABLE(iD
2baf1 62 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3d  b);.    if( iDb=
2baf2 3d 31 20 29 20 63 6f 64 65 20 3d 20 53 51 4c 49  =1 ) code = SQLI
2baf3 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 52 49  TE_DROP_TEMP_TRI
2baf4 47 47 45 52 3b 0a 20 20 20 20 69 66 28 20 73 71  GGER;.    if( sq
2baf5 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
2baf6 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 70 54 72  Parse, code, pTr
2baf7 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 2c 20 70 54  igger->zName, pT
2baf8 61 62 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62  able->zName, zDb
2baf9 29 20 7c 7c 0a 20 20 20 20 20 20 73 71 6c 69 74  ) ||.      sqlit
2bafa 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
2bafb 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54  se, SQLITE_DELET
2bafc 45 2c 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62 29  E, zTab, 0, zDb)
2bafd 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
2bafe 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
2baff 69 66 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74  if..  /* Generat
2bb00 65 20 63 6f 64 65 20 74 6f 20 64 65 73 74 72 6f  e code to destro
2bb01 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 72  y the database r
2bb02 65 63 6f 72 64 20 6f 66 20 74 68 65 20 74 72 69  ecord of the tri
2bb03 67 67 65 72 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  gger..  */.  ass
2bb04 65 72 74 28 20 70 54 61 62 6c 65 21 3d 30 20 29  ert( pTable!=0 )
2bb05 3b 0a 20 20 69 66 28 20 28 76 20 3d 20 73 71 6c  ;.  if( (v = sql
2bb06 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
2bb07 73 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 69  se))!=0 ){.    i
2bb08 6e 74 20 62 61 73 65 3b 0a 20 20 20 20 73 74 61  nt base;.    sta
2bb09 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70  tic const VdbeOp
2bb0a 4c 69 73 74 20 64 72 6f 70 54 72 69 67 67 65 72  List dropTrigger
2bb0b 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b 20 4f  [] = {.      { O
2bb0c 50 5f 52 65 77 69 6e 64 2c 20 20 20 20 20 30 2c  P_Rewind,     0,
2bb0d 20 41 44 44 52 28 39 29 2c 20 20 30 7d 2c 0a 20   ADDR(9),  0},. 
2bb0e 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67       { OP_String
2bb0f 38 2c 20 20 20 20 30 2c 20 31 2c 20 20 20 20 20  8,    0, 1,     
2bb10 20 20 20 30 7d 2c 20 2f 2a 20 31 20 2a 2f 0a 20     0}, /* 1 */. 
2bb11 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6c 75 6d 6e       { OP_Column
2bb12 2c 20 20 20 20 20 30 2c 20 31 2c 20 20 20 20 20  ,     0, 1,     
2bb13 20 20 20 32 7d 2c 0a 20 20 20 20 20 20 7b 20 4f     2},.      { O
2bb14 50 5f 4e 65 2c 20 20 20 20 20 20 20 20 20 32 2c  P_Ne,         2,
2bb15 20 41 44 44 52 28 38 29 2c 20 20 31 7d 2c 0a 20   ADDR(8),  1},. 
2bb16 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67       { OP_String
2bb17 38 2c 20 20 20 20 30 2c 20 31 2c 20 20 20 20 20  8,    0, 1,     
2bb18 20 20 20 30 7d 2c 20 2f 2a 20 34 3a 20 22 74 72     0}, /* 4: "tr
2bb19 69 67 67 65 72 22 20 2a 2f 0a 20 20 20 20 20 20  igger" */.      
2bb1a 7b 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 20 20 20  { OP_Column,    
2bb1b 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 32 7d   0, 0,        2}
2bb1c 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65 2c  ,.      { OP_Ne,
2bb1d 20 20 20 20 20 20 20 20 20 32 2c 20 41 44 44 52           2, ADDR
2bb1e 28 38 29 2c 20 20 31 7d 2c 0a 20 20 20 20 20 20  (8),  1},.      
2bb1f 7b 20 4f 50 5f 44 65 6c 65 74 65 2c 20 20 20 20  { OP_Delete,    
2bb20 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 30 7d   0, 0,        0}
2bb21 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65 78  ,.      { OP_Nex
2bb22 74 2c 20 20 20 20 20 20 20 30 2c 20 41 44 44 52  t,       0, ADDR
2bb23 28 31 29 2c 20 20 30 7d 2c 20 2f 2a 20 38 20 2a  (1),  0}, /* 8 *
2bb24 2f 0a 20 20 20 20 7d 3b 0a 0a 20 20 20 20 73 71  /.    };..    sq
2bb25 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
2bb26 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c  peration(pParse,
2bb27 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71   0, iDb);.    sq
2bb28 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54  lite3OpenMasterT
2bb29 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44 62  able(pParse, iDb
2bb2a 29 3b 0a 20 20 20 20 62 61 73 65 20 3d 20 73 71  );.    base = sq
2bb2b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69  lite3VdbeAddOpLi
2bb2c 73 74 28 76 2c 20 20 41 72 72 61 79 53 69 7a 65  st(v,  ArraySize
2bb2d 28 64 72 6f 70 54 72 69 67 67 65 72 29 2c 20 64  (dropTrigger), d
2bb2e 72 6f 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20  ropTrigger);.   
2bb2f 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
2bb30 67 65 50 34 28 76 2c 20 62 61 73 65 2b 31 2c 20  geP4(v, base+1, 
2bb31 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 2c  pTrigger->zName,
2bb32 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
2bb33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
2bb34 62 61 73 65 2b 34 2c 20 22 74 72 69 67 67 65 72  base+4, "trigger
2bb35 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  ", P4_STATIC);. 
2bb36 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65     sqlite3Change
2bb37 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69  Cookie(pParse, i
2bb38 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Db);.    sqlite3
2bb39 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2bb3a 5f 43 6c 6f 73 65 2c 20 30 2c 20 30 29 3b 0a 20  _Close, 0, 0);. 
2bb3b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2bb3c 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54  dOp4(v, OP_DropT
2bb3d 72 69 67 67 65 72 2c 20 69 44 62 2c 20 30 2c 20  rigger, iDb, 0, 
2bb3e 30 2c 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61  0, pTrigger->zNa
2bb3f 6d 65 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  me, 0);.    if( 
2bb40 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3c 33 20 29  pParse->nMem<3 )
2bb41 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  {.      pParse->
2bb42 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20 7d 0a  nMem = 3;.    }.
2bb43 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d    }.}../*.** Rem
2bb44 6f 76 65 20 61 20 74 72 69 67 67 65 72 20 66 72  ove a trigger fr
2bb45 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  om the hash tabl
2bb46 65 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  es of the sqlite
2bb47 2a 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51  * pointer..*/.SQ
2bb48 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2bb49 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41  d sqlite3UnlinkA
2bb4a 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72 28  ndDeleteTrigger(
2bb4b 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
2bb4c 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   iDb, const char
2bb4d 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 48 61 73 68   *zName){.  Hash
2bb4e 20 2a 70 48 61 73 68 20 3d 20 26 28 64 62 2d 3e   *pHash = &(db->
2bb4f 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61  aDb[iDb].pSchema
2bb50 2d 3e 74 72 69 67 48 61 73 68 29 3b 0a 20 20 54  ->trigHash);.  T
2bb51 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72  rigger *pTrigger
2bb52 3b 0a 20 20 70 54 72 69 67 67 65 72 20 3d 20 73  ;.  pTrigger = s
2bb53 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74  qlite3HashInsert
2bb54 28 70 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 73  (pHash, zName, s
2bb55 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
2bb56 4e 61 6d 65 29 2c 20 30 29 3b 0a 20 20 69 66 28  Name), 0);.  if(
2bb57 20 41 4c 57 41 59 53 28 70 54 72 69 67 67 65 72   ALWAYS(pTrigger
2bb58 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 54 72  ) ){.    if( pTr
2bb59 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d  igger->pSchema==
2bb5a 70 54 72 69 67 67 65 72 2d 3e 70 54 61 62 53 63  pTrigger->pTabSc
2bb5b 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 54 61  hema ){.      Ta
2bb5c 62 6c 65 20 2a 70 54 61 62 20 3d 20 74 61 62 6c  ble *pTab = tabl
2bb5d 65 4f 66 54 72 69 67 67 65 72 28 70 54 72 69 67  eOfTrigger(pTrig
2bb5e 67 65 72 29 3b 0a 20 20 20 20 20 20 54 72 69 67  ger);.      Trig
2bb5f 67 65 72 20 2a 2a 70 70 3b 0a 20 20 20 20 20 20  ger **pp;.      
2bb60 66 6f 72 28 70 70 3d 26 70 54 61 62 2d 3e 70 54  for(pp=&pTab->pT
2bb61 72 69 67 67 65 72 3b 20 2a 70 70 21 3d 70 54 72  rigger; *pp!=pTr
2bb62 69 67 67 65 72 3b 20 70 70 3d 26 28 28 2a 70 70  igger; pp=&((*pp
2bb63 29 2d 3e 70 4e 65 78 74 29 29 3b 0a 20 20 20 20  )->pNext));.    
2bb64 20 20 2a 70 70 20 3d 20 28 2a 70 70 29 2d 3e 70    *pp = (*pp)->p
2bb65 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Next;.    }.    
2bb66 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
2bb67 67 67 65 72 28 64 62 2c 20 70 54 72 69 67 67 65  gger(db, pTrigge
2bb68 72 29 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67  r);.    db->flag
2bb69 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65  s |= SQLITE_Inte
2bb6a 72 6e 43 68 61 6e 67 65 73 3b 0a 20 20 7d 0a 7d  rnChanges;.  }.}
2bb6b 0a 0a 2f 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 69  ../*.** pEList i
2bb6c 73 20 74 68 65 20 53 45 54 20 63 6c 61 75 73 65  s the SET clause
2bb6d 20 6f 66 20 61 6e 20 55 50 44 41 54 45 20 73 74   of an UPDATE st
2bb6e 61 74 65 6d 65 6e 74 2e 20 20 45 61 63 68 20 65  atement.  Each e
2bb6f 6e 74 72 79 0a 2a 2a 20 69 6e 20 70 45 4c 69 73  ntry.** in pELis
2bb70 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  t is of the form
2bb71 61 74 20 3c 69 64 3e 3d 3c 65 78 70 72 3e 2e 20  at <id>=<expr>. 
2bb72 20 49 66 20 61 6e 79 20 6f 66 20 74 68 65 20 65   If any of the e
2bb73 6e 74 72 69 65 73 0a 2a 2a 20 69 6e 20 70 45 4c  ntries.** in pEL
2bb74 69 73 74 20 68 61 76 65 20 61 6e 20 3c 69 64 3e  ist have an <id>
2bb75 20 77 68 69 63 68 20 6d 61 74 63 68 65 73 20 61   which matches a
2bb76 6e 20 69 64 65 6e 74 69 66 69 65 72 20 69 6e 20  n identifier in 
2bb77 70 49 64 4c 69 73 74 2c 0a 2a 2a 20 74 68 65 6e  pIdList,.** then
2bb78 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20 20 49   return TRUE.  I
2bb79 66 20 70 49 64 4c 69 73 74 3d 3d 4e 55 4c 4c 2c  f pIdList==NULL,
2bb7a 20 74 68 65 6e 20 69 74 20 69 73 20 63 6f 6e 73   then it is cons
2bb7b 69 64 65 72 65 64 20 61 0a 2a 2a 20 77 69 6c 64  idered a.** wild
2bb7c 63 61 72 64 20 74 68 61 74 20 6d 61 74 63 68 65  card that matche
2bb7d 73 20 61 6e 79 74 68 69 6e 67 2e 20 20 4c 69 6b  s anything.  Lik
2bb7e 65 77 69 73 65 20 69 66 20 70 45 4c 69 73 74 3d  ewise if pEList=
2bb7f 3d 4e 55 4c 4c 20 74 68 65 6e 0a 2a 2a 20 69 74  =NULL then.** it
2bb80 20 6d 61 74 63 68 65 73 20 61 6e 79 74 68 69 6e   matches anythin
2bb81 67 20 73 6f 20 61 6c 77 61 79 73 20 72 65 74 75  g so always retu
2bb82 72 6e 20 74 72 75 65 2e 20 20 52 65 74 75 72 6e  rn true.  Return
2bb83 20 66 61 6c 73 65 20 6f 6e 6c 79 0a 2a 2a 20 69   false only.** i
2bb84 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61  f there is no ma
2bb85 74 63 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  tch..*/.static i
2bb86 6e 74 20 63 68 65 63 6b 43 6f 6c 75 6d 6e 4f 76  nt checkColumnOv
2bb87 65 72 6c 61 70 28 49 64 4c 69 73 74 20 2a 70 49  erlap(IdList *pI
2bb88 64 4c 69 73 74 2c 20 45 78 70 72 4c 69 73 74 20  dList, ExprList 
2bb89 2a 70 45 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20  *pEList){.  int 
2bb8a 65 3b 0a 20 20 69 66 28 20 70 49 64 4c 69 73 74  e;.  if( pIdList
2bb8b 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 70 45 4c  ==0 || NEVER(pEL
2bb8c 69 73 74 3d 3d 30 29 20 29 20 72 65 74 75 72 6e  ist==0) ) return
2bb8d 20 31 3b 0a 20 20 66 6f 72 28 65 3d 30 3b 20 65   1;.  for(e=0; e
2bb8e 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  <pEList->nExpr; 
2bb8f 65 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71  e++){.    if( sq
2bb90 6c 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78  lite3IdListIndex
2bb91 28 70 49 64 4c 69 73 74 2c 20 70 45 4c 69 73 74  (pIdList, pEList
2bb92 2d 3e 61 5b 65 5d 2e 7a 4e 61 6d 65 29 3e 3d 30  ->a[e].zName)>=0
2bb93 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d   ) return 1;.  }
2bb94 0a 20 20 72 65 74 75 72 6e 20 30 3b 20 0a 7d 0a  .  return 0; .}.
2bb95 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
2bb96 6c 69 73 74 20 6f 66 20 61 6c 6c 20 74 72 69 67  list of all trig
2bb97 67 65 72 73 20 6f 6e 20 74 61 62 6c 65 20 70 54  gers on table pT
2bb98 61 62 20 69 66 20 74 68 65 72 65 20 65 78 69 73  ab if there exis
2bb99 74 73 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 6f  ts at least.** o
2bb9a 6e 65 20 74 72 69 67 67 65 72 20 74 68 61 74 20  ne trigger that 
2bb9b 6d 75 73 74 20 62 65 20 66 69 72 65 64 20 77 68  must be fired wh
2bb9c 65 6e 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 20  en an operation 
2bb9d 6f 66 20 74 79 70 65 20 27 6f 70 27 20 69 73 20  of type 'op' is 
2bb9e 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 6f 6e  .** performed on
2bb9f 20 74 68 65 20 74 61 62 6c 65 2c 20 61 6e 64 2c   the table, and,
2bba0 20 69 66 20 74 68 61 74 20 6f 70 65 72 61 74 69   if that operati
2bba1 6f 6e 20 69 73 20 61 6e 20 55 50 44 41 54 45 2c  on is an UPDATE,
2bba2 20 69 66 20 61 74 0a 2a 2a 20 6c 65 61 73 74 20   if at.** least 
2bba3 6f 6e 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  one of the colum
2bba4 6e 73 20 69 6e 20 70 43 68 61 6e 67 65 73 20 69  ns in pChanges i
2bba5 73 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64  s being modified
2bba6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2bba7 41 54 45 20 54 72 69 67 67 65 72 20 2a 73 71 6c  ATE Trigger *sql
2bba8 69 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73  ite3TriggersExis
2bba9 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  t(.  Parse *pPar
2bbaa 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  se,          /* 
2bbab 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f  Parse context */
2bbac 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  .  Table *pTab, 
2bbad 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2bbae 65 20 74 61 62 6c 65 20 74 68 65 20 63 6f 6e 74  e table the cont
2bbaf 61 69 6e 73 20 74 68 65 20 74 72 69 67 67 65 72  ains the trigger
2bbb0 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20  s */.  int op,  
2bbb1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2bbb2 2a 20 6f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45  * one of TK_DELE
2bbb3 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54  TE, TK_INSERT, T
2bbb4 4b 5f 55 50 44 41 54 45 20 2a 2f 0a 20 20 45 78  K_UPDATE */.  Ex
2bbb5 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 73  prList *pChanges
2bbb6 2c 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73  ,     /* Columns
2bbb7 20 74 68 61 74 20 63 68 61 6e 67 65 20 69 6e 20   that change in 
2bbb8 61 6e 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  an UPDATE statem
2bbb9 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4d  ent */.  int *pM
2bbba 61 73 6b 20 20 20 20 20 20 20 20 20 20 20 20 20  ask             
2bbbb 20 2f 2a 20 4f 55 54 3a 20 4d 61 73 6b 20 6f 66   /* OUT: Mask of
2bbbc 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 7c   TRIGGER_BEFORE|
2bbbd 54 52 49 47 47 45 52 5f 41 46 54 45 52 20 2a 2f  TRIGGER_AFTER */
2bbbe 0a 29 7b 0a 20 20 69 6e 74 20 6d 61 73 6b 20 3d  .){.  int mask =
2bbbf 20 30 3b 0a 20 20 54 72 69 67 67 65 72 20 2a 70   0;.  Trigger *p
2bbc0 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 54 72  List = sqlite3Tr
2bbc1 69 67 67 65 72 4c 69 73 74 28 70 50 61 72 73 65  iggerList(pParse
2bbc2 2c 20 70 54 61 62 29 3b 0a 20 20 54 72 69 67 67  , pTab);.  Trigg
2bbc3 65 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28  er *p;.  assert(
2bbc4 20 70 4c 69 73 74 3d 3d 30 20 7c 7c 20 49 73 56   pList==0 || IsV
2bbc5 69 72 74 75 61 6c 28 70 54 61 62 29 3d 3d 30 20  irtual(pTab)==0 
2bbc6 29 3b 0a 20 20 66 6f 72 28 70 3d 70 4c 69 73 74  );.  for(p=pList
2bbc7 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29  ; p; p=p->pNext)
2bbc8 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6f 70 3d  {.    if( p->op=
2bbc9 3d 6f 70 20 26 26 20 63 68 65 63 6b 43 6f 6c 75  =op && checkColu
2bbca 6d 6e 4f 76 65 72 6c 61 70 28 70 2d 3e 70 43 6f  mnOverlap(p->pCo
2bbcb 6c 75 6d 6e 73 2c 20 70 43 68 61 6e 67 65 73 29  lumns, pChanges)
2bbcc 20 29 7b 0a 20 20 20 20 20 20 6d 61 73 6b 20 7c   ){.      mask |
2bbcd 3d 20 70 2d 3e 74 72 5f 74 6d 3b 0a 20 20 20 20  = p->tr_tm;.    
2bbce 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4d 61 73  }.  }.  if( pMas
2bbcf 6b 20 29 7b 0a 20 20 20 20 2a 70 4d 61 73 6b 20  k ){.    *pMask 
2bbd0 3d 20 6d 61 73 6b 3b 0a 20 20 7d 0a 20 20 72 65  = mask;.  }.  re
2bbd1 74 75 72 6e 20 28 6d 61 73 6b 20 3f 20 70 4c 69  turn (mask ? pLi
2bbd2 73 74 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  st : 0);.}../*.*
2bbd3 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 70 53  * Convert the pS
2bbd4 74 65 70 2d 3e 74 61 72 67 65 74 20 74 6f 6b 65  tep->target toke
2bbd5 6e 20 69 6e 74 6f 20 61 20 53 72 63 4c 69 73 74  n into a SrcList
2bbd6 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f   and return a po
2bbd7 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 61 74  inter.** to that
2bbd8 20 53 72 63 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20   SrcList..**.** 
2bbd9 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 64 64  This routine add
2bbda 73 20 61 20 73 70 65 63 69 66 69 63 20 64 61 74  s a specific dat
2bbdb 61 62 61 73 65 20 6e 61 6d 65 2c 20 69 66 20 6e  abase name, if n
2bbdc 65 65 64 65 64 2c 20 74 6f 20 74 68 65 20 74 61  eeded, to the ta
2bbdd 72 67 65 74 20 77 68 65 6e 0a 2a 2a 20 66 6f 72  rget when.** for
2bbde 6d 69 6e 67 20 74 68 65 20 53 72 63 4c 69 73 74  ming the SrcList
2bbdf 2e 20 20 54 68 69 73 20 70 72 65 76 65 6e 74 73  .  This prevents
2bbe0 20 61 20 74 72 69 67 67 65 72 20 69 6e 20 6f 6e   a trigger in on
2bbe1 65 20 64 61 74 61 62 61 73 65 20 66 72 6f 6d 0a  e database from.
2bbe2 2a 2a 20 72 65 66 65 72 72 69 6e 67 20 74 6f 20  ** referring to 
2bbe3 61 20 74 61 72 67 65 74 20 69 6e 20 61 6e 6f 74  a target in anot
2bbe4 68 65 72 20 64 61 74 61 62 61 73 65 2e 20 20 41  her database.  A
2bbe5 6e 20 65 78 63 65 70 74 69 6f 6e 20 69 73 20 77  n exception is w
2bbe6 68 65 6e 20 74 68 65 0a 2a 2a 20 74 72 69 67 67  hen the.** trigg
2bbe7 65 72 20 69 73 20 69 6e 20 54 45 4d 50 20 69 6e  er is in TEMP in
2bbe8 20 77 68 69 63 68 20 63 61 73 65 20 69 74 20 63   which case it c
2bbe9 61 6e 20 72 65 66 65 72 20 74 6f 20 61 6e 79 20  an refer to any 
2bbea 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 69  other database i
2bbeb 74 0a 2a 2a 20 77 61 6e 74 73 2e 0a 2a 2f 0a 73  t.** wants..*/.s
2bbec 74 61 74 69 63 20 53 72 63 4c 69 73 74 20 2a 74  tatic SrcList *t
2bbed 61 72 67 65 74 53 72 63 4c 69 73 74 28 0a 20 20  argetSrcList(.  
2bbee 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2bbef 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
2bbf0 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
2bbf1 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53   TriggerStep *pS
2bbf2 74 65 70 20 20 20 2f 2a 20 54 68 65 20 74 72 69  tep   /* The tri
2bbf3 67 67 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  gger containing 
2bbf4 74 68 65 20 74 61 72 67 65 74 20 74 6f 6b 65 6e  the target token
2bbf5 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62   */.){.  int iDb
2bbf6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
2bbf7 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61   Index of the da
2bbf8 74 61 62 61 73 65 20 74 6f 20 75 73 65 20 2a 2f  tabase to use */
2bbf9 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  .  SrcList *pSrc
2bbfa 3b 20 20 20 20 20 20 20 2f 2a 20 53 72 63 4c 69  ;       /* SrcLi
2bbfb 73 74 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65  st to be returne
2bbfc 64 20 2a 2f 0a 0a 20 20 70 53 72 63 20 3d 20 73  d */..  pSrc = s
2bbfd 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
2bbfe 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  end(pParse->db, 
2bbff 30 2c 20 26 70 53 74 65 70 2d 3e 74 61 72 67 65  0, &pStep->targe
2bc00 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 53 72  t, 0);.  if( pSr
2bc01 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  c ){.    assert(
2bc02 20 70 53 72 63 2d 3e 6e 53 72 63 3e 30 20 29 3b   pSrc->nSrc>0 );
2bc03 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 53 72  .    assert( pSr
2bc04 63 2d 3e 61 21 3d 30 20 29 3b 0a 20 20 20 20 69  c->a!=0 );.    i
2bc05 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
2bc06 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65  maToIndex(pParse
2bc07 2d 3e 64 62 2c 20 70 53 74 65 70 2d 3e 70 54 72  ->db, pStep->pTr
2bc08 69 67 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ig->pSchema);.  
2bc09 20 20 69 66 28 20 69 44 62 3d 3d 30 20 7c 7c 20    if( iDb==0 || 
2bc0a 69 44 62 3e 3d 32 20 29 7b 0a 20 20 20 20 20 20  iDb>=2 ){.      
2bc0b 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2bc0c 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20  arse->db;.      
2bc0d 61 73 73 65 72 74 28 20 69 44 62 3c 70 50 61 72  assert( iDb<pPar
2bc0e 73 65 2d 3e 64 62 2d 3e 6e 44 62 20 29 3b 0a 20  se->db->nDb );. 
2bc0f 20 20 20 20 20 70 53 72 63 2d 3e 61 5b 70 53 72       pSrc->a[pSr
2bc10 63 2d 3e 6e 53 72 63 2d 31 5d 2e 7a 44 61 74 61  c->nSrc-1].zData
2bc11 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33 44 62  base = sqlite3Db
2bc12 53 74 72 44 75 70 28 64 62 2c 20 64 62 2d 3e 61  StrDup(db, db->a
2bc13 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a  Db[iDb].zName);.
2bc14 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
2bc15 72 6e 20 70 53 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pSrc;.}../*.*
2bc16 2a 20 47 65 6e 65 72 61 74 65 20 56 44 42 45 20  * Generate VDBE 
2bc17 63 6f 64 65 20 66 6f 72 20 74 68 65 20 73 74 61  code for the sta
2bc18 74 65 6d 65 6e 74 73 20 69 6e 73 69 64 65 20 74  tements inside t
2bc19 68 65 20 62 6f 64 79 20 6f 66 20 61 20 73 69 6e  he body of a sin
2bc1a 67 6c 65 20 0a 2a 2a 20 74 72 69 67 67 65 72 2e  gle .** trigger.
2bc1b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
2bc1c 6f 64 65 54 72 69 67 67 65 72 50 72 6f 67 72 61  odeTriggerProgra
2bc1d 6d 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  m(.  Parse *pPar
2bc1e 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  se,            /
2bc1f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e  * The parser con
2bc20 74 65 78 74 20 2a 2f 0a 20 20 54 72 69 67 67 65  text */.  Trigge
2bc21 72 53 74 65 70 20 2a 70 53 74 65 70 4c 69 73 74  rStep *pStepList
2bc22 2c 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 73  ,   /* List of s
2bc23 74 61 74 65 6d 65 6e 74 73 20 69 6e 73 69 64 65  tatements inside
2bc24 20 74 68 65 20 74 72 69 67 67 65 72 20 62 6f 64   the trigger bod
2bc25 79 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e  y */.  int orcon
2bc26 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f               
2bc27 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 20 61 6c 67   /* Conflict alg
2bc28 6f 72 69 74 68 6d 2e 20 28 4f 45 5f 41 62 6f 72  orithm. (OE_Abor
2bc29 74 2c 20 65 74 63 29 20 2a 2f 20 20 0a 29 7b 0a  t, etc) */  .){.
2bc2a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70    TriggerStep *p
2bc2b 53 74 65 70 3b 0a 20 20 56 64 62 65 20 2a 76 20  Step;.  Vdbe *v 
2bc2c 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
2bc2d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2bc2e 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20   pParse->db;..  
2bc2f 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
2bc30 70 54 72 69 67 67 65 72 54 61 62 20 26 26 20 70  pTriggerTab && p
2bc31 50 61 72 73 65 2d 3e 70 54 6f 70 6c 65 76 65 6c  Parse->pToplevel
2bc32 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53   );.  assert( pS
2bc33 74 65 70 4c 69 73 74 20 29 3b 0a 20 20 61 73 73  tepList );.  ass
2bc34 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 66  ert( v!=0 );.  f
2bc35 6f 72 28 70 53 74 65 70 3d 70 53 74 65 70 4c 69  or(pStep=pStepLi
2bc36 73 74 3b 20 70 53 74 65 70 3b 20 70 53 74 65 70  st; pStep; pStep
2bc37 3d 70 53 74 65 70 2d 3e 70 4e 65 78 74 29 7b 0a  =pStep->pNext){.
2bc38 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75      /* Figure ou
2bc39 74 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43  t the ON CONFLIC
2bc3a 54 20 70 6f 6c 69 63 79 20 74 68 61 74 20 77 69  T policy that wi
2bc3b 6c 6c 20 62 65 20 75 73 65 64 20 66 6f 72 20 74  ll be used for t
2bc3c 68 69 73 20 73 74 65 70 0a 20 20 20 20 2a 2a 20  his step.    ** 
2bc3d 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 20 70  of the trigger p
2bc3e 72 6f 67 72 61 6d 2e 20 49 66 20 74 68 65 20 73  rogram. If the s
2bc3f 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 63 61  tatement that ca
2bc40 75 73 65 64 20 74 68 69 73 20 74 72 69 67 67 65  used this trigge
2bc41 72 0a 20 20 20 20 2a 2a 20 74 6f 20 66 69 72 65  r.    ** to fire
2bc42 20 68 61 64 20 61 6e 20 65 78 70 6c 69 63 69 74   had an explicit
2bc43 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 2c 20 74 68   ON CONFLICT, th
2bc44 65 6e 20 75 73 65 20 69 74 2e 20 4f 74 68 65 72  en use it. Other
2bc45 77 69 73 65 2c 20 75 73 65 0a 20 20 20 20 2a 2a  wise, use.    **
2bc46 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54   the ON CONFLICT
2bc47 20 70 6f 6c 69 63 79 20 74 68 61 74 20 77 61 73   policy that was
2bc48 20 73 70 65 63 69 66 69 65 64 20 61 73 20 70 61   specified as pa
2bc49 72 74 20 6f 66 20 74 68 65 20 74 72 69 67 67 65  rt of the trigge
2bc4a 72 0a 20 20 20 20 2a 2a 20 73 74 65 70 20 73 74  r.    ** step st
2bc4b 61 74 65 6d 65 6e 74 2e 20 45 78 61 6d 70 6c 65  atement. Example
2bc4c 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  :.    **.    ** 
2bc4d 20 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52    CREATE TRIGGER
2bc4e 20 41 46 54 45 52 20 49 4e 53 45 52 54 20 4f 4e   AFTER INSERT ON
2bc4f 20 74 31 20 42 45 47 49 4e 3b 0a 20 20 20 20 2a   t1 BEGIN;.    *
2bc50 2a 20 20 20 20 20 49 4e 53 45 52 54 20 4f 52 20  *     INSERT OR 
2bc51 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 74 32 20  REPLACE INTO t2 
2bc52 56 41 4c 55 45 53 28 6e 65 77 2e 61 2c 20 6e 65  VALUES(new.a, ne
2bc53 77 2e 62 29 3b 0a 20 20 20 20 2a 2a 20 20 20 45  w.b);.    **   E
2bc54 4e 44 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ND;.    **.    *
2bc55 2a 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20  *   INSERT INTO 
2bc56 74 31 20 2e 2e 2e 20 3b 20 20 20 20 20 20 20 20  t1 ... ;        
2bc57 20 20 20 20 2d 2d 20 69 6e 73 65 72 74 20 69 6e      -- insert in
2bc58 74 6f 20 74 32 20 75 73 65 73 20 52 45 50 4c 41  to t2 uses REPLA
2bc59 43 45 20 70 6f 6c 69 63 79 0a 20 20 20 20 2a 2a  CE policy.    **
2bc5a 20 20 20 49 4e 53 45 52 54 20 4f 52 20 49 47 4e     INSERT OR IGN
2bc5b 4f 52 45 20 49 4e 54 4f 20 74 31 20 2e 2e 2e 20  ORE INTO t1 ... 
2bc5c 3b 20 20 2d 2d 20 69 6e 73 65 72 74 20 69 6e 74  ;  -- insert int
2bc5d 6f 20 74 32 20 75 73 65 73 20 49 47 4e 4f 52 45  o t2 uses IGNORE
2bc5e 20 70 6f 6c 69 63 79 0a 20 20 20 20 2a 2f 0a 20   policy.    */. 
2bc5f 20 20 20 70 50 61 72 73 65 2d 3e 65 4f 72 63 6f     pParse->eOrco
2bc60 6e 66 20 3d 20 28 6f 72 63 6f 6e 66 3d 3d 4f 45  nf = (orconf==OE
2bc61 5f 44 65 66 61 75 6c 74 29 3f 70 53 74 65 70 2d  _Default)?pStep-
2bc62 3e 6f 72 63 6f 6e 66 3a 28 75 38 29 6f 72 63 6f  >orconf:(u8)orco
2bc63 6e 66 3b 0a 0a 20 20 20 20 73 77 69 74 63 68 28  nf;..    switch(
2bc64 20 70 53 74 65 70 2d 3e 6f 70 20 29 7b 0a 20 20   pStep->op ){.  
2bc65 20 20 20 20 63 61 73 65 20 54 4b 5f 55 50 44 41      case TK_UPDA
2bc66 54 45 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71  TE: {.        sq
2bc67 6c 69 74 65 33 55 70 64 61 74 65 28 70 50 61 72  lite3Update(pPar
2bc68 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 74  se, .          t
2bc69 61 72 67 65 74 53 72 63 4c 69 73 74 28 70 50 61  argetSrcList(pPa
2bc6a 72 73 65 2c 20 70 53 74 65 70 29 2c 0a 20 20 20  rse, pStep),.   
2bc6b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
2bc6c 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 53  prListDup(db, pS
2bc6d 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 2c 20  tep->pExprList, 
2bc6e 30 29 2c 20 0a 20 20 20 20 20 20 20 20 20 20 73  0), .          s
2bc6f 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
2bc70 2c 20 70 53 74 65 70 2d 3e 70 57 68 65 72 65 2c  , pStep->pWhere,
2bc71 20 30 29 2c 20 0a 20 20 20 20 20 20 20 20 20 20   0), .          
2bc72 70 50 61 72 73 65 2d 3e 65 4f 72 63 6f 6e 66 0a  pParse->eOrconf.
2bc73 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
2bc74 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2bc75 7d 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f  }.      case TK_
2bc76 49 4e 53 45 52 54 3a 20 7b 0a 20 20 20 20 20 20  INSERT: {.      
2bc77 20 20 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28    sqlite3Insert(
2bc78 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
2bc79 20 20 20 74 61 72 67 65 74 53 72 63 4c 69 73 74     targetSrcList
2bc7a 28 70 50 61 72 73 65 2c 20 70 53 74 65 70 29 2c  (pParse, pStep),
2bc7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2bc7c 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62  e3ExprListDup(db
2bc7d 2c 20 70 53 74 65 70 2d 3e 70 45 78 70 72 4c 69  , pStep->pExprLi
2bc7e 73 74 2c 20 30 29 2c 20 0a 20 20 20 20 20 20 20  st, 0), .       
2bc7f 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
2bc80 44 75 70 28 64 62 2c 20 70 53 74 65 70 2d 3e 70  Dup(db, pStep->p
2bc81 53 65 6c 65 63 74 2c 20 30 29 2c 20 0a 20 20 20  Select, 0), .   
2bc82 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 49 64         sqlite3Id
2bc83 4c 69 73 74 44 75 70 28 64 62 2c 20 70 53 74 65  ListDup(db, pSte
2bc84 70 2d 3e 70 49 64 4c 69 73 74 29 2c 20 0a 20 20  p->pIdList), .  
2bc85 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
2bc86 65 4f 72 63 6f 6e 66 0a 20 20 20 20 20 20 20 20  eOrconf.        
2bc87 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
2bc88 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2bc89 63 61 73 65 20 54 4b 5f 44 45 4c 45 54 45 3a 20  case TK_DELETE: 
2bc8a 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2bc8b 33 44 65 6c 65 74 65 46 72 6f 6d 28 70 50 61 72  3DeleteFrom(pPar
2bc8c 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 74  se, .          t
2bc8d 61 72 67 65 74 53 72 63 4c 69 73 74 28 70 50 61  argetSrcList(pPa
2bc8e 72 73 65 2c 20 70 53 74 65 70 29 2c 0a 20 20 20  rse, pStep),.   
2bc8f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
2bc90 70 72 44 75 70 28 64 62 2c 20 70 53 74 65 70 2d  prDup(db, pStep-
2bc91 3e 70 57 68 65 72 65 2c 20 30 29 0a 20 20 20 20  >pWhere, 0).    
2bc92 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 62      );.        b
2bc93 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2bc94 20 20 20 20 64 65 66 61 75 6c 74 3a 20 61 73 73      default: ass
2bc95 65 72 74 28 20 70 53 74 65 70 2d 3e 6f 70 3d 3d  ert( pStep->op==
2bc96 54 4b 5f 53 45 4c 45 43 54 20 29 3b 20 7b 0a 20  TK_SELECT ); {. 
2bc97 20 20 20 20 20 20 20 53 65 6c 65 63 74 44 65 73         SelectDes
2bc98 74 20 73 44 65 73 74 3b 0a 20 20 20 20 20 20 20  t sDest;.       
2bc99 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
2bc9a 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
2bc9b 44 75 70 28 64 62 2c 20 70 53 74 65 70 2d 3e 70  Dup(db, pStep->p
2bc9c 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20 20 20  Select, 0);.    
2bc9d 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
2bc9e 74 44 65 73 74 49 6e 69 74 28 26 73 44 65 73 74  tDestInit(&sDest
2bc9f 2c 20 53 52 54 5f 44 69 73 63 61 72 64 2c 20 30  , SRT_Discard, 0
2bca0 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2bca1 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
2bca2 20 70 53 65 6c 65 63 74 2c 20 26 73 44 65 73 74   pSelect, &sDest
2bca3 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2bca4 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64  e3SelectDelete(d
2bca5 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20  b, pSelect);.   
2bca6 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2bca7 20 20 7d 0a 20 20 20 20 7d 20 0a 20 20 20 20 69    }.    } .    i
2bca8 66 28 20 70 53 74 65 70 2d 3e 6f 70 21 3d 54 4b  f( pStep->op!=TK
2bca9 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 20 20 20  _SELECT ){.     
2bcaa 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2bcab 70 30 28 76 2c 20 4f 50 5f 52 65 73 65 74 43 6f  p0(v, OP_ResetCo
2bcac 75 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  unt);.    }.  }.
2bcad 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
2bcae 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
2bcaf 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  BUG./*.** This f
2bcb0 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
2bcb1 74 6f 20 61 64 64 20 56 64 62 65 43 6f 6d 6d 65  to add VdbeComme
2bcb2 6e 74 28 29 20 61 6e 6e 6f 74 61 74 69 6f 6e 73  nt() annotations
2bcb3 20 74 6f 20 61 20 56 44 42 45 0a 2a 2a 20 70 72   to a VDBE.** pr
2bcb4 6f 67 72 61 6d 2e 20 49 74 20 69 73 20 6e 6f 74  ogram. It is not
2bcb5 20 75 73 65 64 20 69 6e 20 70 72 6f 64 75 63 74   used in product
2bcb6 69 6f 6e 20 63 6f 64 65 2c 20 6f 6e 6c 79 20 66  ion code, only f
2bcb7 6f 72 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2f  or debugging..*/
2bcb8 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68  .static const ch
2bcb9 61 72 20 2a 6f 6e 45 72 72 6f 72 54 65 78 74 28  ar *onErrorText(
2bcba 69 6e 74 20 6f 6e 45 72 72 6f 72 29 7b 0a 20 20  int onError){.  
2bcbb 73 77 69 74 63 68 28 20 6f 6e 45 72 72 6f 72 20  switch( onError 
2bcbc 29 7b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 41  ){.    case OE_A
2bcbd 62 6f 72 74 3a 20 20 20 20 72 65 74 75 72 6e 20  bort:    return 
2bcbe 22 61 62 6f 72 74 22 3b 0a 20 20 20 20 63 61 73  "abort";.    cas
2bcbf 65 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3a 20 72  e OE_Rollback: r
2bcc0 65 74 75 72 6e 20 22 72 6f 6c 6c 62 61 63 6b 22  eturn "rollback"
2bcc1 3b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 46 61  ;.    case OE_Fa
2bcc2 69 6c 3a 20 20 20 20 20 72 65 74 75 72 6e 20 22  il:     return "
2bcc3 66 61 69 6c 22 3b 0a 20 20 20 20 63 61 73 65 20  fail";.    case 
2bcc4 4f 45 5f 52 65 70 6c 61 63 65 3a 20 20 72 65 74  OE_Replace:  ret
2bcc5 75 72 6e 20 22 72 65 70 6c 61 63 65 22 3b 0a 20  urn "replace";. 
2bcc6 20 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72     case OE_Ignor
2bcc7 65 3a 20 20 20 72 65 74 75 72 6e 20 22 69 67 6e  e:   return "ign
2bcc8 6f 72 65 22 3b 0a 20 20 20 20 63 61 73 65 20 4f  ore";.    case O
2bcc9 45 5f 44 65 66 61 75 6c 74 3a 20 20 72 65 74 75  E_Default:  retu
2bcca 72 6e 20 22 64 65 66 61 75 6c 74 22 3b 0a 20 20  rn "default";.  
2bccb 7d 0a 20 20 72 65 74 75 72 6e 20 22 6e 2f 61 22  }.  return "n/a"
2bccc 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
2bccd 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20  * Parse context 
2bcce 73 74 72 75 63 74 75 72 65 20 70 46 72 6f 6d 20  structure pFrom 
2bccf 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 75 73  has just been us
2bcd0 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 73  ed to create a s
2bcd1 75 62 2d 76 64 62 65 0a 2a 2a 20 28 74 72 69 67  ub-vdbe.** (trig
2bcd2 67 65 72 20 70 72 6f 67 72 61 6d 29 2e 20 49 66  ger program). If
2bcd3 20 61 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63   an error has oc
2bcd4 63 75 72 72 65 64 2c 20 74 72 61 6e 73 66 65 72  curred, transfer
2bcd5 20 65 72 72 6f 72 20 69 6e 66 6f 72 6d 61 74 69   error informati
2bcd6 6f 6e 0a 2a 2a 20 66 72 6f 6d 20 70 46 72 6f 6d  on.** from pFrom
2bcd7 20 74 6f 20 70 54 6f 2e 0a 2a 2f 0a 73 74 61 74   to pTo..*/.stat
2bcd8 69 63 20 76 6f 69 64 20 74 72 61 6e 73 66 65 72  ic void transfer
2bcd9 50 61 72 73 65 45 72 72 6f 72 28 50 61 72 73 65  ParseError(Parse
2bcda 20 2a 70 54 6f 2c 20 50 61 72 73 65 20 2a 70 46   *pTo, Parse *pF
2bcdb 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20  rom){.  assert( 
2bcdc 70 46 72 6f 6d 2d 3e 7a 45 72 72 4d 73 67 3d 3d  pFrom->zErrMsg==
2bcdd 30 20 7c 7c 20 70 46 72 6f 6d 2d 3e 6e 45 72 72  0 || pFrom->nErr
2bcde 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54   );.  assert( pT
2bcdf 6f 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 20 7c 7c  o->zErrMsg==0 ||
2bce0 20 70 54 6f 2d 3e 6e 45 72 72 20 29 3b 0a 20 20   pTo->nErr );.  
2bce1 69 66 28 20 70 54 6f 2d 3e 6e 45 72 72 3d 3d 30  if( pTo->nErr==0
2bce2 20 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 7a 45 72   ){.    pTo->zEr
2bce3 72 4d 73 67 20 3d 20 70 46 72 6f 6d 2d 3e 7a 45  rMsg = pFrom->zE
2bce4 72 72 4d 73 67 3b 0a 20 20 20 20 70 54 6f 2d 3e  rrMsg;.    pTo->
2bce5 6e 45 72 72 20 3d 20 70 46 72 6f 6d 2d 3e 6e 45  nErr = pFrom->nE
2bce6 72 72 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  rr;.  }else{.   
2bce7 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
2bce8 46 72 6f 6d 2d 3e 64 62 2c 20 70 46 72 6f 6d 2d  From->db, pFrom-
2bce9 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 7d  >zErrMsg);.  }.}
2bcea 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
2bceb 6e 64 20 70 6f 70 75 6c 61 74 65 20 61 20 6e 65  nd populate a ne
2bcec 77 20 54 72 69 67 67 65 72 50 72 67 20 6f 62 6a  w TriggerPrg obj
2bced 65 63 74 20 77 69 74 68 20 61 20 73 75 62 2d 70  ect with a sub-p
2bcee 72 6f 67 72 61 6d 20 0a 2a 2a 20 69 6d 70 6c 65  rogram .** imple
2bcef 6d 65 6e 74 69 6e 67 20 74 72 69 67 67 65 72 20  menting trigger 
2bcf0 70 54 72 69 67 67 65 72 20 77 69 74 68 20 4f 4e  pTrigger with ON
2bcf1 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79   CONFLICT policy
2bcf2 20 6f 72 63 6f 6e 66 2e 0a 2a 2f 0a 73 74 61 74   orconf..*/.stat
2bcf3 69 63 20 54 72 69 67 67 65 72 50 72 67 20 2a 63  ic TriggerPrg *c
2bcf4 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 0a 20  odeRowTrigger(. 
2bcf5 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2bcf6 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
2bcf7 20 70 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a   parse context *
2bcf8 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72  /.  Trigger *pTr
2bcf9 69 67 67 65 72 2c 20 20 20 2f 2a 20 54 72 69 67  igger,   /* Trig
2bcfa 67 65 72 20 74 6f 20 63 6f 64 65 20 2a 2f 0a 20  ger to code */. 
2bcfb 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
2bcfc 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62        /* The tab
2bcfd 6c 65 20 70 54 72 69 67 67 65 72 20 69 73 20 61  le pTrigger is a
2bcfe 74 74 61 63 68 65 64 20 74 6f 20 2a 2f 0a 20 20  ttached to */.  
2bcff 69 6e 74 20 6f 72 63 6f 6e 66 20 20 20 20 20 20  int orconf      
2bd00 20 20 20 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c       /* ON CONFL
2bd01 49 43 54 20 70 6f 6c 69 63 79 20 74 6f 20 63 6f  ICT policy to co
2bd02 64 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  de trigger progr
2bd03 61 6d 20 77 69 74 68 20 2a 2f 0a 29 7b 0a 20 20  am with */.){.  
2bd04 50 61 72 73 65 20 2a 70 54 6f 70 20 3d 20 73 71  Parse *pTop = sq
2bd05 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76  lite3ParseToplev
2bd06 65 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71  el(pParse);.  sq
2bd07 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2bd08 73 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 44 61 74  se->db;   /* Dat
2bd09 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  abase handle */.
2bd0a 20 20 54 72 69 67 67 65 72 50 72 67 20 2a 70 50    TriggerPrg *pP
2bd0b 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  rg;           /*
2bd0c 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e   Value to return
2bd0d 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65   */.  Expr *pWhe
2bd0e 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  n = 0;          
2bd0f 20 20 2f 2a 20 44 75 70 6c 69 63 61 74 65 20 6f    /* Duplicate o
2bd10 66 20 74 72 69 67 67 65 72 20 57 48 45 4e 20 65  f trigger WHEN e
2bd11 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 56  xpression */.  V
2bd12 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20  dbe *v;         
2bd13 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65             /* Te
2bd14 6d 70 6f 72 61 72 79 20 56 4d 20 2a 2f 0a 20 20  mporary VM */.  
2bd15 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b  NameContext sNC;
2bd16 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2bd17 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20  ame context for 
2bd18 73 75 62 2d 76 64 62 65 20 2a 2f 0a 20 20 53 75  sub-vdbe */.  Su
2bd19 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67 72  bProgram *pProgr
2bd1a 61 6d 20 3d 20 30 3b 20 20 20 2f 2a 20 53 75 62  am = 0;   /* Sub
2bd1b 2d 76 64 62 65 20 66 6f 72 20 74 72 69 67 67 65  -vdbe for trigge
2bd1c 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 50  r program */.  P
2bd1d 61 72 73 65 20 2a 70 53 75 62 50 61 72 73 65 3b  arse *pSubParse;
2bd1e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
2bd1f 72 73 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20  rse context for 
2bd20 73 75 62 2d 76 64 62 65 20 2a 2f 0a 20 20 69 6e  sub-vdbe */.  in
2bd21 74 20 69 45 6e 64 54 72 69 67 67 65 72 20 3d 20  t iEndTrigger = 
2bd22 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 62  0;        /* Lab
2bd23 65 6c 20 74 6f 20 6a 75 6d 70 20 74 6f 20 69 66  el to jump to if
2bd24 20 57 48 45 4e 20 69 73 20 66 61 6c 73 65 20 2a   WHEN is false *
2bd25 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54 61  /..  assert( pTa
2bd26 62 3d 3d 74 61 62 6c 65 4f 66 54 72 69 67 67 65  b==tableOfTrigge
2bd27 72 28 70 54 72 69 67 67 65 72 29 20 29 3b 0a 0a  r(pTrigger) );..
2bd28 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68    /* Allocate th
2bd29 65 20 54 72 69 67 67 65 72 50 72 67 20 61 6e 64  e TriggerPrg and
2bd2a 20 53 75 62 50 72 6f 67 72 61 6d 20 6f 62 6a 65   SubProgram obje
2bd2b 63 74 73 2e 20 54 6f 20 65 6e 73 75 72 65 20 74  cts. To ensure t
2bd2c 68 61 74 20 74 68 65 79 0a 20 20 2a 2a 20 61 72  hat they.  ** ar
2bd2d 65 20 66 72 65 65 64 20 69 66 20 61 6e 20 65 72  e freed if an er
2bd2e 72 6f 72 20 6f 63 63 75 72 73 2c 20 6c 69 6e 6b  ror occurs, link
2bd2f 20 74 68 65 6d 20 69 6e 74 6f 20 74 68 65 20 50   them into the P
2bd30 61 72 73 65 2e 70 54 72 69 67 67 65 72 50 72 67  arse.pTriggerPrg
2bd31 20 0a 20 20 2a 2a 20 6c 69 73 74 20 6f 66 20 74   .  ** list of t
2bd32 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20 50 61 72  he top-level Par
2bd33 73 65 20 6f 62 6a 65 63 74 20 73 6f 6f 6e 65 72  se object sooner
2bd34 20 72 61 74 68 65 72 20 74 68 61 6e 20 6c 61 74   rather than lat
2bd35 65 72 2e 20 20 2a 2f 0a 20 20 70 50 72 67 20 3d  er.  */.  pPrg =
2bd36 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
2bd37 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
2bd38 54 72 69 67 67 65 72 50 72 67 29 29 3b 0a 20 20  TriggerPrg));.  
2bd39 69 66 28 20 21 70 50 72 67 20 29 20 72 65 74 75  if( !pPrg ) retu
2bd3a 72 6e 20 30 3b 0a 20 20 70 50 72 67 2d 3e 70 4e  rn 0;.  pPrg->pN
2bd3b 65 78 74 20 3d 20 70 54 6f 70 2d 3e 70 54 72 69  ext = pTop->pTri
2bd3c 67 67 65 72 50 72 67 3b 0a 20 20 70 54 6f 70 2d  ggerPrg;.  pTop-
2bd3d 3e 70 54 72 69 67 67 65 72 50 72 67 20 3d 20 70  >pTriggerPrg = p
2bd3e 50 72 67 3b 0a 20 20 70 50 72 67 2d 3e 70 50 72  Prg;.  pPrg->pPr
2bd3f 6f 67 72 61 6d 20 3d 20 70 50 72 6f 67 72 61 6d  ogram = pProgram
2bd40 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
2bd41 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
2bd42 66 28 53 75 62 50 72 6f 67 72 61 6d 29 29 3b 0a  f(SubProgram));.
2bd43 20 20 69 66 28 20 21 70 50 72 6f 67 72 61 6d 20    if( !pProgram 
2bd44 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 50  ) return 0;.  pP
2bd45 72 6f 67 72 61 6d 2d 3e 6e 52 65 66 20 3d 20 31  rogram->nRef = 1
2bd46 3b 0a 20 20 70 50 72 67 2d 3e 70 54 72 69 67 67  ;.  pPrg->pTrigg
2bd47 65 72 20 3d 20 70 54 72 69 67 67 65 72 3b 0a 20  er = pTrigger;. 
2bd48 20 70 50 72 67 2d 3e 6f 72 63 6f 6e 66 20 3d 20   pPrg->orconf = 
2bd49 6f 72 63 6f 6e 66 3b 0a 0a 20 20 2f 2a 20 41 6c  orconf;..  /* Al
2bd4a 6c 6f 63 61 74 65 20 61 6e 64 20 70 6f 70 75 6c  locate and popul
2bd4b 61 74 65 20 61 20 6e 65 77 20 50 61 72 73 65 20  ate a new Parse 
2bd4c 63 6f 6e 74 65 78 74 20 74 6f 20 75 73 65 20 66  context to use f
2bd4d 6f 72 20 63 6f 64 69 6e 67 20 74 68 65 20 0a 20  or coding the . 
2bd4e 20 2a 2a 20 74 72 69 67 67 65 72 20 73 75 62 2d   ** trigger sub-
2bd4f 70 72 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20 20 70  program.  */.  p
2bd50 53 75 62 50 61 72 73 65 20 3d 20 73 71 6c 69 74  SubParse = sqlit
2bd51 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f  e3StackAllocZero
2bd52 28 64 62 2c 20 73 69 7a 65 6f 66 28 50 61 72 73  (db, sizeof(Pars
2bd53 65 29 29 3b 0a 20 20 69 66 28 20 21 70 53 75 62  e));.  if( !pSub
2bd54 50 61 72 73 65 20 29 20 72 65 74 75 72 6e 20 30  Parse ) return 0
2bd55 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c  ;.  memset(&sNC,
2bd56 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29   0, sizeof(sNC))
2bd57 3b 0a 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d  ;.  sNC.pParse =
2bd58 20 70 53 75 62 50 61 72 73 65 3b 0a 20 20 70 53   pSubParse;.  pS
2bd59 75 62 50 61 72 73 65 2d 3e 64 62 20 3d 20 64 62  ubParse->db = db
2bd5a 3b 0a 20 20 70 53 75 62 50 61 72 73 65 2d 3e 70  ;.  pSubParse->p
2bd5b 54 72 69 67 67 65 72 54 61 62 20 3d 20 70 54 61  TriggerTab = pTa
2bd5c 62 3b 0a 20 20 70 53 75 62 50 61 72 73 65 2d 3e  b;.  pSubParse->
2bd5d 70 54 6f 70 6c 65 76 65 6c 20 3d 20 70 54 6f 70  pToplevel = pTop
2bd5e 3b 0a 20 20 70 53 75 62 50 61 72 73 65 2d 3e 7a  ;.  pSubParse->z
2bd5f 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 54  AuthContext = pT
2bd60 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 3b 0a 20  rigger->zName;. 
2bd61 20 70 53 75 62 50 61 72 73 65 2d 3e 65 54 72 69   pSubParse->eTri
2bd62 67 67 65 72 4f 70 20 3d 20 70 54 72 69 67 67 65  ggerOp = pTrigge
2bd63 72 2d 3e 6f 70 3b 0a 0a 20 20 76 20 3d 20 73 71  r->op;..  v = sq
2bd64 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 53 75  lite3GetVdbe(pSu
2bd65 62 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76  bParse);.  if( v
2bd66 20 29 7b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d   ){.    VdbeComm
2bd67 65 6e 74 28 28 76 2c 20 22 53 74 61 72 74 3a 20  ent((v, "Start: 
2bd68 25 73 2e 25 73 20 28 25 73 20 25 73 25 73 25 73  %s.%s (%s %s%s%s
2bd69 20 4f 4e 20 25 73 29 22 2c 20 0a 20 20 20 20 20   ON %s)", .     
2bd6a 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65   pTrigger->zName
2bd6b 2c 20 6f 6e 45 72 72 6f 72 54 65 78 74 28 6f 72  , onErrorText(or
2bd6c 63 6f 6e 66 29 2c 0a 20 20 20 20 20 20 28 70 54  conf),.      (pT
2bd6d 72 69 67 67 65 72 2d 3e 74 72 5f 74 6d 3d 3d 54  rigger->tr_tm==T
2bd6e 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 3f 20  RIGGER_BEFORE ? 
2bd6f 22 42 45 46 4f 52 45 22 20 3a 20 22 41 46 54 45  "BEFORE" : "AFTE
2bd70 52 22 29 2c 0a 20 20 20 20 20 20 20 20 28 70 54  R"),.        (pT
2bd71 72 69 67 67 65 72 2d 3e 6f 70 3d 3d 54 4b 5f 55  rigger->op==TK_U
2bd72 50 44 41 54 45 20 3f 20 22 55 50 44 41 54 45 22  PDATE ? "UPDATE"
2bd73 20 3a 20 22 22 29 2c 0a 20 20 20 20 20 20 20 20   : ""),.        
2bd74 28 70 54 72 69 67 67 65 72 2d 3e 6f 70 3d 3d 54  (pTrigger->op==T
2bd75 4b 5f 49 4e 53 45 52 54 20 3f 20 22 49 4e 53 45  K_INSERT ? "INSE
2bd76 52 54 22 20 3a 20 22 22 29 2c 0a 20 20 20 20 20  RT" : ""),.     
2bd77 20 20 20 28 70 54 72 69 67 67 65 72 2d 3e 6f 70     (pTrigger->op
2bd78 3d 3d 54 4b 5f 44 45 4c 45 54 45 20 3f 20 22 44  ==TK_DELETE ? "D
2bd79 45 4c 45 54 45 22 20 3a 20 22 22 29 2c 0a 20 20  ELETE" : ""),.  
2bd7a 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 0a      pTab->zName.
2bd7b 20 20 20 20 29 29 3b 0a 23 69 66 6e 64 65 66 20      ));.#ifndef 
2bd7c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
2bd7d 45 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  E.    sqlite3Vdb
2bd7e 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
2bd7f 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d   .      sqlite3M
2bd80 50 72 69 6e 74 66 28 64 62 2c 20 22 2d 2d 20 54  Printf(db, "-- T
2bd81 52 49 47 47 45 52 20 25 73 22 2c 20 70 54 72 69  RIGGER %s", pTri
2bd82 67 67 65 72 2d 3e 7a 4e 61 6d 65 29 2c 20 50 34  gger->zName), P4
2bd83 5f 44 59 4e 41 4d 49 43 0a 20 20 20 20 29 3b 0a  _DYNAMIC.    );.
2bd84 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 49  #endif..    /* I
2bd85 66 20 6f 6e 65 20 77 61 73 20 73 70 65 63 69 66  f one was specif
2bd86 69 65 64 2c 20 63 6f 64 65 20 74 68 65 20 57 48  ied, code the WH
2bd87 45 4e 20 63 6c 61 75 73 65 2e 20 49 66 20 69 74  EN clause. If it
2bd88 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20 66 61   evaluates to fa
2bd89 6c 73 65 0a 20 20 20 20 2a 2a 20 28 6f 72 20 4e  lse.    ** (or N
2bd8a 55 4c 4c 29 20 74 68 65 20 73 75 62 2d 76 64 62  ULL) the sub-vdb
2bd8b 65 20 69 73 20 69 6d 6d 65 64 69 61 74 65 6c 79  e is immediately
2bd8c 20 68 61 6c 74 65 64 20 62 79 20 6a 75 6d 70 69   halted by jumpi
2bd8d 6e 67 20 74 6f 20 74 68 65 20 0a 20 20 20 20 2a  ng to the .    *
2bd8e 2a 20 4f 50 5f 48 61 6c 74 20 69 6e 73 65 72 74  * OP_Halt insert
2bd8f 65 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  ed at the end of
2bd90 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 2a   the program.  *
2bd91 2f 0a 20 20 20 20 69 66 28 20 70 54 72 69 67 67  /.    if( pTrigg
2bd92 65 72 2d 3e 70 57 68 65 6e 20 29 7b 0a 20 20 20  er->pWhen ){.   
2bd93 20 20 20 70 57 68 65 6e 20 3d 20 73 71 6c 69 74     pWhen = sqlit
2bd94 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 54  e3ExprDup(db, pT
2bd95 72 69 67 67 65 72 2d 3e 70 57 68 65 6e 2c 20 30  rigger->pWhen, 0
2bd96 29 3b 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c  );.      if( SQL
2bd97 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 52  ITE_OK==sqlite3R
2bd98 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
2bd99 26 73 4e 43 2c 20 70 57 68 65 6e 29 20 0a 20 20  &sNC, pWhen) .  
2bd9a 20 20 20 20 20 26 26 20 64 62 2d 3e 6d 61 6c 6c       && db->mall
2bd9b 6f 63 46 61 69 6c 65 64 3d 3d 30 20 0a 20 20 20  ocFailed==0 .   
2bd9c 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 69 45     ){.        iE
2bd9d 6e 64 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69  ndTrigger = sqli
2bd9e 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
2bd9f 28 76 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  (v);.        sql
2bda0 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
2bda1 70 53 75 62 50 61 72 73 65 2c 20 70 57 68 65 6e  pSubParse, pWhen
2bda2 2c 20 69 45 6e 64 54 72 69 67 67 65 72 2c 20 53  , iEndTrigger, S
2bda3 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
2bda4 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
2bda5 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2bda6 74 65 28 64 62 2c 20 70 57 68 65 6e 29 3b 0a 20  te(db, pWhen);. 
2bda7 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 64     }..    /* Cod
2bda8 65 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72  e the trigger pr
2bda9 6f 67 72 61 6d 20 69 6e 74 6f 20 74 68 65 20 73  ogram into the s
2bdaa 75 62 2d 76 64 62 65 2e 20 2a 2f 0a 20 20 20 20  ub-vdbe. */.    
2bdab 63 6f 64 65 54 72 69 67 67 65 72 50 72 6f 67 72  codeTriggerProgr
2bdac 61 6d 28 70 53 75 62 50 61 72 73 65 2c 20 70 54  am(pSubParse, pT
2bdad 72 69 67 67 65 72 2d 3e 73 74 65 70 5f 6c 69 73  rigger->step_lis
2bdae 74 2c 20 6f 72 63 6f 6e 66 29 3b 0a 0a 20 20 20  t, orconf);..   
2bdaf 20 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20 4f 50   /* Insert an OP
2bdb0 5f 48 61 6c 74 20 61 74 20 74 68 65 20 65 6e 64  _Halt at the end
2bdb1 20 6f 66 20 74 68 65 20 73 75 62 2d 70 72 6f 67   of the sub-prog
2bdb2 72 61 6d 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ram. */.    if( 
2bdb3 69 45 6e 64 54 72 69 67 67 65 72 20 29 7b 0a 20  iEndTrigger ){. 
2bdb4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2bdb5 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
2bdb6 69 45 6e 64 54 72 69 67 67 65 72 29 3b 0a 20 20  iEndTrigger);.  
2bdb7 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
2bdb8 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f  dbeAddOp0(v, OP_
2bdb9 48 61 6c 74 29 3b 0a 20 20 20 20 56 64 62 65 43  Halt);.    VdbeC
2bdba 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 45 6e 64 3a  omment((v, "End:
2bdbb 20 25 73 2e 25 73 22 2c 20 70 54 72 69 67 67 65   %s.%s", pTrigge
2bdbc 72 2d 3e 7a 4e 61 6d 65 2c 20 6f 6e 45 72 72 6f  r->zName, onErro
2bdbd 72 54 65 78 74 28 6f 72 63 6f 6e 66 29 29 29 3b  rText(orconf)));
2bdbe 0a 0a 20 20 20 20 74 72 61 6e 73 66 65 72 50 61  ..    transferPa
2bdbf 72 73 65 45 72 72 6f 72 28 70 50 61 72 73 65 2c  rseError(pParse,
2bdc0 20 70 53 75 62 50 61 72 73 65 29 3b 0a 20 20 20   pSubParse);.   
2bdc1 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
2bdc2 61 69 6c 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20  ailed==0 ){.    
2bdc3 20 20 70 50 72 6f 67 72 61 6d 2d 3e 61 4f 70 20    pProgram->aOp 
2bdc4 3d 20 73 71 6c 69 74 65 33 56 64 62 65 54 61 6b  = sqlite3VdbeTak
2bdc5 65 4f 70 41 72 72 61 79 28 76 2c 20 26 70 50 72  eOpArray(v, &pPr
2bdc6 6f 67 72 61 6d 2d 3e 6e 4f 70 2c 20 26 70 54 6f  ogram->nOp, &pTo
2bdc7 70 2d 3e 6e 4d 61 78 41 72 67 29 3b 0a 20 20 20  p->nMaxArg);.   
2bdc8 20 7d 0a 20 20 20 20 70 50 72 6f 67 72 61 6d 2d   }.    pProgram-
2bdc9 3e 6e 4d 65 6d 20 3d 20 70 53 75 62 50 61 72 73  >nMem = pSubPars
2bdca 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 70 50 72  e->nMem;.    pPr
2bdcb 6f 67 72 61 6d 2d 3e 6e 43 73 72 20 3d 20 70 53  ogram->nCsr = pS
2bdcc 75 62 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a 20  ubParse->nTab;. 
2bdcd 20 20 20 70 50 72 6f 67 72 61 6d 2d 3e 74 6f 6b     pProgram->tok
2bdce 65 6e 20 3d 20 28 76 6f 69 64 20 2a 29 70 54 72  en = (void *)pTr
2bdcf 69 67 67 65 72 3b 0a 20 20 20 20 70 50 72 67 2d  igger;.    pPrg-
2bdd0 3e 6f 6c 64 6d 61 73 6b 20 3d 20 70 53 75 62 50  >oldmask = pSubP
2bdd1 61 72 73 65 2d 3e 6f 6c 64 6d 61 73 6b 3b 0a 20  arse->oldmask;. 
2bdd2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65     sqlite3VdbeDe
2bdd3 6c 65 74 65 28 76 29 3b 0a 20 20 7d 0a 0a 20 20  lete(v);.  }..  
2bdd4 61 73 73 65 72 74 28 20 21 70 53 75 62 50 61 72  assert( !pSubPar
2bdd5 73 65 2d 3e 70 41 69 6e 63 20 20 20 20 20 20 20  se->pAinc       
2bdd6 26 26 20 21 70 53 75 62 50 61 72 73 65 2d 3e 70  && !pSubParse->p
2bdd7 5a 6f 6d 62 69 65 54 61 62 20 29 3b 0a 20 20 61  ZombieTab );.  a
2bdd8 73 73 65 72 74 28 20 21 70 53 75 62 50 61 72 73  ssert( !pSubPars
2bdd9 65 2d 3e 70 54 72 69 67 67 65 72 50 72 67 20 26  e->pTriggerPrg &
2bdda 26 20 21 70 53 75 62 50 61 72 73 65 2d 3e 6e 4d  & !pSubParse->nM
2bddb 61 78 41 72 67 20 29 3b 0a 20 20 73 71 6c 69 74  axArg );.  sqlit
2bddc 65 33 53 74 61 63 6b 46 72 65 65 28 64 62 2c 20  e3StackFree(db, 
2bddd 70 53 75 62 50 61 72 73 65 29 3b 0a 0a 20 20 72  pSubParse);..  r
2bdde 65 74 75 72 6e 20 70 50 72 67 3b 0a 7d 0a 20 20  eturn pPrg;.}.  
2bddf 20 20 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20    ./*.** Return 
2bde0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54  a pointer to a T
2bde1 72 69 67 67 65 72 50 72 67 20 6f 62 6a 65 63 74  riggerPrg object
2bde2 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
2bde3 73 75 62 2d 70 72 6f 67 72 61 6d 20 66 6f 72 0a  sub-program for.
2bde4 2a 2a 20 74 72 69 67 67 65 72 20 70 54 72 69 67  ** trigger pTrig
2bde5 67 65 72 20 77 69 74 68 20 64 65 66 61 75 6c 74  ger with default
2bde6 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67   ON CONFLICT alg
2bde7 6f 72 69 74 68 6d 20 6f 72 63 6f 6e 66 2e 20 49  orithm orconf. I
2bde8 66 20 6e 6f 20 73 75 63 68 0a 2a 2a 20 54 72 69  f no such.** Tri
2bde9 67 67 65 72 50 72 67 20 6f 62 6a 65 63 74 20 65  ggerPrg object e
2bdea 78 69 73 74 73 2c 20 61 20 6e 65 77 20 6f 62 6a  xists, a new obj
2bdeb 65 63 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  ect is allocated
2bdec 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 64 20 62   and populated b
2bded 65 66 6f 72 65 0a 2a 2a 20 62 65 69 6e 67 20 72  efore.** being r
2bdee 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74  eturned..*/.stat
2bdef 69 63 20 54 72 69 67 67 65 72 50 72 67 20 2a 67  ic TriggerPrg *g
2bdf0 65 74 52 6f 77 54 72 69 67 67 65 72 28 0a 20 20  etRowTrigger(.  
2bdf1 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2bdf2 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
2bdf3 70 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f  parse context */
2bdf4 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
2bdf5 67 67 65 72 2c 20 20 20 2f 2a 20 54 72 69 67 67  gger,   /* Trigg
2bdf6 65 72 20 74 6f 20 63 6f 64 65 20 2a 2f 0a 20 20  er to code */.  
2bdf7 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20  Table *pTab,    
2bdf8 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
2bdf9 65 20 74 72 69 67 67 65 72 20 70 54 72 69 67 67  e trigger pTrigg
2bdfa 65 72 20 69 73 20 61 74 74 61 63 68 65 64 20 74  er is attached t
2bdfb 6f 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e  o */.  int orcon
2bdfc 66 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  f           /* O
2bdfd 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67 6f 72  N CONFLICT algor
2bdfe 69 74 68 6d 2e 20 2a 2f 0a 29 7b 0a 20 20 50 61  ithm. */.){.  Pa
2bdff 72 73 65 20 2a 70 52 6f 6f 74 20 3d 20 73 71 6c  rse *pRoot = sql
2be00 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65  ite3ParseTopleve
2be01 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 54 72 69  l(pParse);.  Tri
2be02 67 67 65 72 50 72 67 20 2a 70 50 72 67 3b 0a 0a  ggerPrg *pPrg;..
2be03 20 20 61 73 73 65 72 74 28 20 70 54 61 62 3d 3d    assert( pTab==
2be04 74 61 62 6c 65 4f 66 54 72 69 67 67 65 72 28 70  tableOfTrigger(p
2be05 54 72 69 67 67 65 72 29 20 29 3b 0a 0a 20 20 2f  Trigger) );..  /
2be06 2a 20 49 74 20 6d 61 79 20 62 65 20 74 68 61 74  * It may be that
2be07 20 74 68 69 73 20 74 72 69 67 67 65 72 20 68 61   this trigger ha
2be08 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63  s already been c
2be09 6f 64 65 64 20 28 6f 72 20 69 73 20 69 6e 20 74  oded (or is in t
2be0a 68 65 0a 20 20 2a 2a 20 70 72 6f 63 65 73 73 20  he.  ** process 
2be0b 6f 66 20 62 65 69 6e 67 20 63 6f 64 65 64 29 2e  of being coded).
2be0c 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
2be0d 63 61 73 65 2c 20 74 68 65 6e 20 61 6e 20 65 6e  case, then an en
2be0e 74 72 79 20 77 69 74 68 0a 20 20 2a 2a 20 61 20  try with.  ** a 
2be0f 6d 61 74 63 68 69 6e 67 20 54 72 69 67 67 65 72  matching Trigger
2be10 50 72 67 2e 70 54 72 69 67 67 65 72 20 66 69 65  Prg.pTrigger fie
2be11 6c 64 20 77 69 6c 6c 20 62 65 20 70 72 65 73 65  ld will be prese
2be12 6e 74 20 73 6f 6d 65 77 68 65 72 65 0a 20 20 2a  nt somewhere.  *
2be13 2a 20 69 6e 20 74 68 65 20 50 61 72 73 65 2e 70  * in the Parse.p
2be14 54 72 69 67 67 65 72 50 72 67 20 6c 69 73 74 2e  TriggerPrg list.
2be15 20 53 65 61 72 63 68 20 66 6f 72 20 73 75 63 68   Search for such
2be16 20 61 6e 20 65 6e 74 72 79 2e 20 20 2a 2f 0a 20   an entry.  */. 
2be17 20 66 6f 72 28 70 50 72 67 3d 70 52 6f 6f 74 2d   for(pPrg=pRoot-
2be18 3e 70 54 72 69 67 67 65 72 50 72 67 3b 20 0a 20  >pTriggerPrg; . 
2be19 20 20 20 20 20 70 50 72 67 20 26 26 20 28 70 50       pPrg && (pP
2be1a 72 67 2d 3e 70 54 72 69 67 67 65 72 21 3d 70 54  rg->pTrigger!=pT
2be1b 72 69 67 67 65 72 20 7c 7c 20 70 50 72 67 2d 3e  rigger || pPrg->
2be1c 6f 72 63 6f 6e 66 21 3d 6f 72 63 6f 6e 66 29 3b  orconf!=orconf);
2be1d 20 0a 20 20 20 20 20 20 70 50 72 67 3d 70 50 72   .      pPrg=pPr
2be1e 67 2d 3e 70 4e 65 78 74 0a 20 20 29 3b 0a 0a 20  g->pNext.  );.. 
2be1f 20 2f 2a 20 49 66 20 61 6e 20 65 78 69 73 74 69   /* If an existi
2be20 6e 67 20 54 72 69 67 67 65 72 50 72 67 20 63 6f  ng TriggerPrg co
2be21 75 6c 64 20 6e 6f 74 20 62 65 20 6c 6f 63 61 74  uld not be locat
2be22 65 64 2c 20 63 72 65 61 74 65 20 61 20 6e 65 77  ed, create a new
2be23 20 6f 6e 65 2e 20 2a 2f 0a 20 20 69 66 28 20 21   one. */.  if( !
2be24 70 50 72 67 20 29 7b 0a 20 20 20 20 70 50 72 67  pPrg ){.    pPrg
2be25 20 3d 20 63 6f 64 65 52 6f 77 54 72 69 67 67 65   = codeRowTrigge
2be26 72 28 70 50 61 72 73 65 2c 20 70 54 72 69 67 67  r(pParse, pTrigg
2be27 65 72 2c 20 70 54 61 62 2c 20 6f 72 63 6f 6e 66  er, pTab, orconf
2be28 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  );.  }..  return
2be29 20 70 50 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pPrg;.}../*.** 
2be2a 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 74  This is called t
2be2b 6f 20 63 6f 64 65 20 46 4f 52 20 45 41 43 48 20  o code FOR EACH 
2be2c 52 4f 57 20 74 72 69 67 67 65 72 73 2e 0a 2a 2a  ROW triggers..**
2be2d 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 6f 64  .** When the cod
2be2e 65 20 74 68 61 74 20 74 68 69 73 20 66 75 6e 63  e that this func
2be2f 74 69 6f 6e 20 67 65 6e 65 72 61 74 65 73 20 69  tion generates i
2be30 73 20 65 78 65 63 75 74 65 64 2c 20 74 68 65 20  s executed, the 
2be31 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 6d 75  following .** mu
2be32 73 74 20 62 65 20 74 72 75 65 3a 0a 2a 2a 0a 2a  st be true:.**.*
2be33 2a 20 31 2e 20 4e 6f 20 63 75 72 73 6f 72 73 20  * 1. No cursors 
2be34 6d 61 79 20 62 65 20 6f 70 65 6e 20 69 6e 20 74  may be open in t
2be35 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
2be36 2e 20 20 28 42 75 74 20 6e 65 77 49 64 78 20 61  .  (But newIdx a
2be37 6e 64 20 6f 6c 64 49 64 78 0a 2a 2a 20 20 20 20  nd oldIdx.**    
2be38 63 61 6e 20 62 65 20 69 6e 64 69 63 65 73 20 6f  can be indices o
2be39 66 20 63 75 72 73 6f 72 73 20 69 6e 20 74 65 6d  f cursors in tem
2be3a 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20  porary tables.  
2be3b 53 65 65 20 62 65 6c 6f 77 2e 29 0a 2a 2a 0a 2a  See below.).**.*
2be3c 2a 20 32 2e 20 49 66 20 74 68 65 20 74 72 69 67  * 2. If the trig
2be3d 67 65 72 73 20 62 65 69 6e 67 20 63 6f 64 65 64  gers being coded
2be3e 20 61 72 65 20 4f 4e 20 49 4e 53 45 52 54 20 6f   are ON INSERT o
2be3f 72 20 4f 4e 20 55 50 44 41 54 45 20 74 72 69 67  r ON UPDATE trig
2be40 67 65 72 73 2c 20 74 68 65 6e 0a 2a 2a 20 20 20  gers, then.**   
2be41 20 61 20 74 65 6d 70 6f 72 61 72 79 20 76 64 62   a temporary vdb
2be42 65 20 63 75 72 73 6f 72 20 28 69 6e 64 65 78 20  e cursor (index 
2be43 6e 65 77 49 64 78 29 20 6d 75 73 74 20 62 65 20  newIdx) must be 
2be44 6f 70 65 6e 20 61 6e 64 20 70 6f 69 6e 74 69 6e  open and pointin
2be45 67 20 61 74 0a 2a 2a 20 20 20 20 61 20 72 6f 77  g at.**    a row
2be46 20 63 6f 6e 74 61 69 6e 69 6e 67 20 76 61 6c 75   containing valu
2be47 65 73 20 74 6f 20 62 65 20 73 75 62 73 74 69 74  es to be substit
2be48 75 74 65 64 20 66 6f 72 20 6e 65 77 2e 2a 20 65  uted for new.* e
2be49 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68  xpressions in th
2be4a 65 0a 2a 2a 20 20 20 20 74 72 69 67 67 65 72 20  e.**    trigger 
2be4b 70 72 6f 67 72 61 6d 28 73 29 2e 0a 2a 2a 0a 2a  program(s)..**.*
2be4c 2a 20 33 2e 20 49 66 20 74 68 65 20 74 72 69 67  * 3. If the trig
2be4d 67 65 72 73 20 62 65 69 6e 67 20 63 6f 64 65 64  gers being coded
2be4e 20 61 72 65 20 4f 4e 20 44 45 4c 45 54 45 20 6f   are ON DELETE o
2be4f 72 20 4f 4e 20 55 50 44 41 54 45 20 74 72 69 67  r ON UPDATE trig
2be50 67 65 72 73 2c 20 74 68 65 6e 0a 2a 2a 20 20 20  gers, then.**   
2be51 20 61 20 74 65 6d 70 6f 72 61 72 79 20 76 64 62   a temporary vdb
2be52 65 20 63 75 72 73 6f 72 20 28 69 6e 64 65 78 20  e cursor (index 
2be53 6f 6c 64 49 64 78 29 20 6d 75 73 74 20 62 65 20  oldIdx) must be 
2be54 6f 70 65 6e 20 61 6e 64 20 70 6f 69 6e 74 69 6e  open and pointin
2be55 67 20 61 74 0a 2a 2a 20 20 20 20 61 20 72 6f 77  g at.**    a row
2be56 20 63 6f 6e 74 61 69 6e 69 6e 67 20 76 61 6c 75   containing valu
2be57 65 73 20 74 6f 20 62 65 20 73 75 62 73 74 69 74  es to be substit
2be58 75 74 65 64 20 66 6f 72 20 6f 6c 64 2e 2a 20 65  uted for old.* e
2be59 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68  xpressions in th
2be5a 65 0a 2a 2a 20 20 20 20 74 72 69 67 67 65 72 20  e.**    trigger 
2be5b 70 72 6f 67 72 61 6d 28 73 29 2e 0a 2a 2a 0a 2a  program(s)..**.*
2be5c 2a 20 49 66 20 74 68 65 79 20 61 72 65 20 6e 6f  * If they are no
2be5d 74 20 4e 55 4c 4c 2c 20 74 68 65 20 70 69 4f 6c  t NULL, the piOl
2be5e 64 43 6f 6c 4d 61 73 6b 20 61 6e 64 20 70 69 4e  dColMask and piN
2be5f 65 77 43 6f 6c 4d 61 73 6b 20 6f 75 74 70 75 74  ewColMask output
2be60 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 61 72   variables.** ar
2be61 65 20 73 65 74 20 74 6f 20 76 61 6c 75 65 73 20  e set to values 
2be62 74 68 61 74 20 64 65 73 63 72 69 62 65 20 74 68  that describe th
2be63 65 20 63 6f 6c 75 6d 6e 73 20 75 73 65 64 20 62  e columns used b
2be64 79 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72  y the trigger pr
2be65 6f 67 72 61 6d 0a 2a 2a 20 69 6e 20 74 68 65 20  ogram.** in the 
2be66 4f 4c 44 2e 2a 20 61 6e 64 20 4e 45 57 2e 2a 20  OLD.* and NEW.* 
2be67 74 61 62 6c 65 73 20 72 65 73 70 65 63 74 69 76  tables respectiv
2be68 65 6c 79 2e 20 49 66 20 63 6f 6c 75 6d 6e 20 4e  ely. If column N
2be69 20 6f 66 20 74 68 65 20 0a 2a 2a 20 70 73 65 75   of the .** pseu
2be6a 64 6f 2d 74 61 62 6c 65 20 69 73 20 72 65 61 64  do-table is read
2be6b 20 61 74 20 6c 65 61 73 74 20 6f 6e 63 65 2c 20   at least once, 
2be6c 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
2be6d 67 20 62 69 74 20 6f 66 20 74 68 65 20 6f 75 74  g bit of the out
2be6e 70 75 74 0a 2a 2a 20 6d 61 73 6b 20 69 73 20 73  put.** mask is s
2be6f 65 74 2e 20 49 66 20 61 20 63 6f 6c 75 6d 6e 20  et. If a column 
2be70 77 69 74 68 20 61 6e 20 69 6e 64 65 78 20 67 72  with an index gr
2be71 65 61 74 65 72 20 74 68 61 6e 20 33 32 20 69 73  eater than 32 is
2be72 20 72 65 61 64 2c 20 74 68 65 0a 2a 2a 20 6f 75   read, the.** ou
2be73 74 70 75 74 20 6d 61 73 6b 20 69 73 20 73 65 74  tput mask is set
2be74 20 74 6f 20 74 68 65 20 73 70 65 63 69 61 6c 20   to the special 
2be75 76 61 6c 75 65 20 30 78 66 66 66 66 66 66 66 66  value 0xffffffff
2be76 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ..**.*/.SQLITE_P
2be77 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2be78 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65  te3CodeRowTrigge
2be79 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  r(.  Parse *pPar
2be7a 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72  se,       /* Par
2be7b 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  se context */.  
2be7c 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
2be7d 72 2c 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20  r,   /* List of 
2be7e 74 72 69 67 67 65 72 73 20 6f 6e 20 74 61 62 6c  triggers on tabl
2be7f 65 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20  e pTab */.  int 
2be80 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  op,             
2be81 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 55 50   /* One of TK_UP
2be82 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c  DATE, TK_INSERT,
2be83 20 54 4b 5f 44 45 4c 45 54 45 20 2a 2f 0a 20 20   TK_DELETE */.  
2be84 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67  ExprList *pChang
2be85 65 73 2c 20 20 2f 2a 20 43 68 61 6e 67 65 73 20  es,  /* Changes 
2be86 6c 69 73 74 20 66 6f 72 20 61 6e 79 20 55 50 44  list for any UPD
2be87 41 54 45 20 4f 46 20 74 72 69 67 67 65 72 73 20  ATE OF triggers 
2be88 2a 2f 0a 20 20 69 6e 74 20 74 72 5f 74 6d 2c 20  */.  int tr_tm, 
2be89 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
2be8a 20 6f 66 20 54 52 49 47 47 45 52 5f 42 45 46 4f   of TRIGGER_BEFO
2be8b 52 45 2c 20 54 52 49 47 47 45 52 5f 41 46 54 45  RE, TRIGGER_AFTE
2be8c 52 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  R */.  Table *pT
2be8d 61 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ab,         /* T
2be8e 68 65 20 74 61 62 6c 65 20 74 6f 20 63 6f 64 65  he table to code
2be8f 20 74 72 69 67 67 65 72 73 20 66 72 6f 6d 20 2a   triggers from *
2be90 2f 0a 20 20 69 6e 74 20 6e 65 77 49 64 78 2c 20  /.  int newIdx, 
2be91 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2be92 69 6e 64 69 63 65 20 6f 66 20 74 68 65 20 22 6e  indice of the "n
2be93 65 77 22 20 72 6f 77 20 74 6f 20 61 63 63 65 73  ew" row to acces
2be94 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 6c 64 49 64  s */.  int oldId
2be95 78 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  x,          /* T
2be96 68 65 20 69 6e 64 69 63 65 20 6f 66 20 74 68 65  he indice of the
2be97 20 22 6f 6c 64 22 20 72 6f 77 20 74 6f 20 61 63   "old" row to ac
2be98 63 65 73 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 72  cess */.  int or
2be99 63 6f 6e 66 2c 20 20 20 20 20 20 20 20 20 20 2f  conf,          /
2be9a 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f  * ON CONFLICT po
2be9b 6c 69 63 79 20 2a 2f 0a 20 20 69 6e 74 20 69 67  licy */.  int ig
2be9c 6e 6f 72 65 4a 75 6d 70 20 20 20 20 20 20 20 2f  noreJump       /
2be9d 2a 20 49 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f  * Instruction to
2be9e 20 6a 75 6d 70 20 74 6f 20 66 6f 72 20 52 41 49   jump to for RAI
2be9f 53 45 28 49 47 4e 4f 52 45 29 20 2a 2f 0a 29 7b  SE(IGNORE) */.){
2bea0 0a 20 20 54 72 69 67 67 65 72 20 2a 70 3b 0a 0a  .  Trigger *p;..
2bea1 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
2bea2 45 52 28 6e 65 77 49 64 78 29 3b 0a 0a 20 20 61  ER(newIdx);..  a
2bea3 73 73 65 72 74 28 6f 70 20 3d 3d 20 54 4b 5f 55  ssert(op == TK_U
2bea4 50 44 41 54 45 20 7c 7c 20 6f 70 20 3d 3d 20 54  PDATE || op == T
2bea5 4b 5f 49 4e 53 45 52 54 20 7c 7c 20 6f 70 20 3d  K_INSERT || op =
2bea6 3d 20 54 4b 5f 44 45 4c 45 54 45 29 3b 0a 20 20  = TK_DELETE);.  
2bea7 61 73 73 65 72 74 28 74 72 5f 74 6d 20 3d 3d 20  assert(tr_tm == 
2bea8 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 7c  TRIGGER_BEFORE |
2bea9 7c 20 74 72 5f 74 6d 20 3d 3d 20 54 52 49 47 47  | tr_tm == TRIGG
2beaa 45 52 5f 41 46 54 45 52 20 29 3b 0a 0a 20 20 66  ER_AFTER );..  f
2beab 6f 72 28 70 3d 70 54 72 69 67 67 65 72 3b 20 70  or(p=pTrigger; p
2beac 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 0a  ; p=p->pNext){..
2bead 20 20 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68      /* Sanity ch
2beae 65 63 6b 69 6e 67 3a 20 20 54 68 65 20 73 63 68  ecking:  The sch
2beaf 65 6d 61 20 66 6f 72 20 74 68 65 20 74 72 69 67  ema for the trig
2beb0 67 65 72 20 61 6e 64 20 66 6f 72 20 74 68 65 20  ger and for the 
2beb1 74 61 62 6c 65 20 61 72 65 0a 20 20 20 20 2a 2a  table are.    **
2beb2 20 61 6c 77 61 79 73 20 64 65 66 69 6e 65 64 2e   always defined.
2beb3 20 20 54 68 65 20 74 72 69 67 67 65 72 20 6d 75    The trigger mu
2beb4 73 74 20 62 65 20 69 6e 20 74 68 65 20 73 61 6d  st be in the sam
2beb5 65 20 73 63 68 65 6d 61 20 61 73 20 74 68 65 20  e schema as the 
2beb6 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 6f 72 20  table.    ** or 
2beb7 65 6c 73 65 20 69 74 20 6d 75 73 74 20 62 65 20  else it must be 
2beb8 61 20 54 45 4d 50 20 74 72 69 67 67 65 72 2e 20  a TEMP trigger. 
2beb9 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  */.    assert( p
2beba 2d 3e 70 53 63 68 65 6d 61 21 3d 30 20 29 3b 0a  ->pSchema!=0 );.
2bebb 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
2bebc 54 61 62 53 63 68 65 6d 61 21 3d 30 20 29 3b 0a  TabSchema!=0 );.
2bebd 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
2bebe 53 63 68 65 6d 61 3d 3d 70 2d 3e 70 54 61 62 53  Schema==p->pTabS
2bebf 63 68 65 6d 61 20 0a 20 20 20 20 20 20 20 20 20  chema .         
2bec0 7c 7c 20 70 2d 3e 70 53 63 68 65 6d 61 3d 3d 70  || p->pSchema==p
2bec1 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 31  Parse->db->aDb[1
2bec2 5d 2e 70 53 63 68 65 6d 61 20 29 3b 0a 0a 20 20  ].pSchema );..  
2bec3 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 77    /* Determine w
2bec4 68 65 74 68 65 72 20 77 65 20 73 68 6f 75 6c 64  hether we should
2bec5 20 63 6f 64 65 20 74 68 69 73 20 74 72 69 67 67   code this trigg
2bec6 65 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d  er */.    if( p-
2bec7 3e 6f 70 3d 3d 6f 70 20 0a 20 20 20 20 20 26 26  >op==op .     &&
2bec8 20 70 2d 3e 74 72 5f 74 6d 3d 3d 74 72 5f 74 6d   p->tr_tm==tr_tm
2bec9 20 0a 20 20 20 20 20 26 26 20 63 68 65 63 6b 43   .     && checkC
2beca 6f 6c 75 6d 6e 4f 76 65 72 6c 61 70 28 70 2d 3e  olumnOverlap(p->
2becb 70 43 6f 6c 75 6d 6e 73 2c 70 43 68 61 6e 67 65  pColumns,pChange
2becc 73 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  s).    ){.      
2becd 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65  Vdbe *v = sqlite
2bece 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
2becf 3b 20 2f 2a 20 4d 61 69 6e 20 56 4d 20 2a 2f 0a  ; /* Main VM */.
2bed0 20 20 20 20 20 20 54 72 69 67 67 65 72 50 72 67        TriggerPrg
2bed1 20 2a 70 50 72 67 3b 0a 20 20 20 20 20 20 70 50   *pPrg;.      pP
2bed2 72 67 20 3d 20 67 65 74 52 6f 77 54 72 69 67 67  rg = getRowTrigg
2bed3 65 72 28 70 50 61 72 73 65 2c 20 70 2c 20 70 54  er(pParse, p, pT
2bed4 61 62 2c 20 6f 72 63 6f 6e 66 29 3b 0a 20 20 20  ab, orconf);.   
2bed5 20 20 20 61 73 73 65 72 74 28 20 70 50 72 67 20     assert( pPrg 
2bed6 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  || pParse->nErr 
2bed7 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d  || pParse->db->m
2bed8 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 0a  allocFailed );..
2bed9 20 20 20 20 20 20 2f 2a 20 43 6f 64 65 20 74 68        /* Code th
2beda 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20 6f 70 63  e OP_Program opc
2bedb 6f 64 65 20 69 6e 20 74 68 65 20 70 61 72 65 6e  ode in the paren
2bedc 74 20 56 44 42 45 2e 20 50 34 20 6f 66 20 74 68  t VDBE. P4 of th
2bedd 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20 0a 20 20  e OP_Program .  
2bede 20 20 20 20 2a 2a 20 69 73 20 61 20 70 6f 69 6e      ** is a poin
2bedf 74 65 72 20 74 6f 20 74 68 65 20 73 75 62 2d 76  ter to the sub-v
2bee0 64 62 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  dbe containing t
2bee1 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  he trigger progr
2bee2 61 6d 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66  am.  */.      if
2bee3 28 20 70 50 72 67 20 29 7b 0a 20 20 20 20 20 20  ( pPrg ){.      
2bee4 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2bee5 4f 70 33 28 76 2c 20 4f 50 5f 50 72 6f 67 72 61  Op3(v, OP_Progra
2bee6 6d 2c 20 6f 6c 64 49 64 78 2c 20 69 67 6e 6f 72  m, oldIdx, ignor
2bee7 65 4a 75 6d 70 2c 20 2b 2b 70 50 61 72 73 65 2d  eJump, ++pParse-
2bee8 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20  >nMem);.        
2bee9 70 50 72 67 2d 3e 70 50 72 6f 67 72 61 6d 2d 3e  pPrg->pProgram->
2beea 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20 20 20  nRef++;.        
2beeb 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2beec 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 6f 6e 73  eP4(v, -1, (cons
2beed 74 20 63 68 61 72 20 2a 29 70 50 72 67 2d 3e 70  t char *)pPrg->p
2beee 50 72 6f 67 72 61 6d 2c 20 50 34 5f 53 55 42 50  Program, P4_SUBP
2beef 52 4f 47 52 41 4d 29 3b 0a 20 20 20 20 20 20 20  ROGRAM);.       
2bef0 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
2bef1 20 22 43 61 6c 6c 3a 20 25 73 2e 25 73 22 2c 20   "Call: %s.%s", 
2bef2 70 2d 3e 7a 4e 61 6d 65 2c 20 6f 6e 45 72 72 6f  p->zName, onErro
2bef3 72 54 65 78 74 28 6f 72 63 6f 6e 66 29 29 29 3b  rText(orconf)));
2bef4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2bef5 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 69 67   }.}../*.** Trig
2bef6 67 65 72 73 20 66 69 72 65 64 20 62 79 20 55 50  gers fired by UP
2bef7 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20 73  DATE or DELETE s
2bef8 74 61 74 65 6d 65 6e 74 73 20 6d 61 79 20 61 63  tatements may ac
2bef9 63 65 73 73 20 76 61 6c 75 65 73 20 73 74 6f 72  cess values stor
2befa 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 6f 6c 64  ed.** in the old
2befb 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e  .* pseudo-table.
2befc 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   This function r
2befd 65 74 75 72 6e 73 20 61 20 33 32 2d 62 69 74 20  eturns a 32-bit 
2befe 62 69 74 6d 61 73 6b 0a 2a 2a 20 69 6e 64 69 63  bitmask.** indic
2beff 61 74 69 6e 67 20 77 68 69 63 68 20 63 6f 6c 75  ating which colu
2bf00 6d 6e 73 20 6f 66 20 74 68 65 20 6f 6c 64 2e 2a  mns of the old.*
2bf01 20 74 61 62 6c 65 20 61 63 74 75 61 6c 6c 79 20   table actually 
2bf02 61 72 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 74  are used by.** t
2bf03 72 69 67 67 65 72 73 2e 20 54 68 69 73 20 69 6e  riggers. This in
2bf04 66 6f 72 6d 61 74 69 6f 6e 20 6d 61 79 20 62 65  formation may be
2bf05 20 75 73 65 64 20 62 79 20 74 68 65 20 63 61 6c   used by the cal
2bf06 6c 65 72 20 74 6f 20 61 76 6f 69 64 20 68 61 76  ler to avoid hav
2bf07 69 6e 67 0a 2a 2a 20 74 6f 20 6c 6f 61 64 20 74  ing.** to load t
2bf08 68 65 20 65 6e 74 69 72 65 20 6f 6c 64 2e 2a 20  he entire old.* 
2bf09 72 65 63 6f 72 64 20 69 6e 74 6f 20 6d 65 6d 6f  record into memo
2bf0a 72 79 20 77 68 65 6e 20 65 78 65 63 75 74 69 6e  ry when executin
2bf0b 67 20 61 6e 20 55 50 44 41 54 45 0a 2a 2a 20 6f  g an UPDATE.** o
2bf0c 72 20 44 45 4c 45 54 45 20 63 6f 6d 6d 61 6e 64  r DELETE command
2bf0d 2e 0a 2a 2a 0a 2a 2a 20 42 69 74 20 30 20 6f 66  ..**.** Bit 0 of
2bf0e 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 6d 61   the returned ma
2bf0f 73 6b 20 69 73 20 73 65 74 20 69 66 20 74 68 65  sk is set if the
2bf10 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d   left-most colum
2bf11 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 74 61 62 6c  n of the.** tabl
2bf12 65 20 6d 61 79 20 62 65 20 61 63 63 65 73 73 65  e may be accesse
2bf13 64 20 75 73 69 6e 67 20 61 6e 20 6f 6c 64 2e 3c  d using an old.<
2bf14 63 6f 6c 3e 20 72 65 66 65 72 65 6e 63 65 2e 20  col> reference. 
2bf15 42 69 74 20 31 20 69 73 20 73 65 74 20 69 66 0a  Bit 1 is set if.
2bf16 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 6c 65  ** the second le
2bf17 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 76 61  ftmost column va
2bf18 6c 75 65 20 69 73 20 72 65 71 75 69 72 65 64 2c  lue is required,
2bf19 20 61 6e 64 20 73 6f 20 6f 6e 2e 20 49 66 20 74   and so on. If t
2bf1a 68 65 72 65 0a 2a 2a 20 61 72 65 20 6d 6f 72 65  here.** are more
2bf1b 20 74 68 61 6e 20 33 32 20 63 6f 6c 75 6d 6e 73   than 32 columns
2bf1c 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20 61   in the table, a
2bf1d 6e 64 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20  nd at least one 
2bf1e 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 0a 2a  of the columns.*
2bf1f 2a 20 77 69 74 68 20 61 6e 20 69 6e 64 65 78 20  * with an index 
2bf20 67 72 65 61 74 65 72 20 74 68 61 6e 20 33 32 20  greater than 32 
2bf21 6d 61 79 20 62 65 20 61 63 63 65 73 73 65 64 2c  may be accessed,
2bf22 20 30 78 66 66 66 66 66 66 66 66 20 69 73 20 72   0xffffffff is r
2bf23 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  eturned..**.** I
2bf24 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
2bf25 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69  e to determine i
2bf26 66 20 74 68 65 20 6f 6c 64 2e 72 6f 77 69 64 20  f the old.rowid 
2bf27 63 6f 6c 75 6d 6e 20 69 73 20 61 63 63 65 73 73  column is access
2bf28 65 64 0a 2a 2a 20 62 79 20 74 72 69 67 67 65 72  ed.** by trigger
2bf29 73 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75  s. The caller mu
2bf2a 73 74 20 61 6c 77 61 79 73 20 61 73 73 75 6d 65  st always assume
2bf2b 20 74 68 61 74 20 69 74 20 69 73 2e 0a 2a 2a 0a   that it is..**.
2bf2c 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 65  ** There is no e
2bf2d 71 75 69 76 61 6c 65 6e 74 20 66 75 6e 63 74 69  quivalent functi
2bf2e 6f 6e 20 66 6f 72 20 6e 65 77 2e 2a 20 72 65 66  on for new.* ref
2bf2f 65 72 65 6e 63 65 73 2e 0a 2a 2f 0a 53 51 4c 49  erences..*/.SQLI
2bf30 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73  TE_PRIVATE u32 s
2bf31 71 6c 69 74 65 33 54 72 69 67 67 65 72 4f 6c 64  qlite3TriggerOld
2bf32 6d 61 73 6b 28 0a 20 20 50 61 72 73 65 20 2a 70  mask(.  Parse *p
2bf33 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20  Parse,       /* 
2bf34 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f  Parse context */
2bf35 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
2bf36 67 67 65 72 2c 20 20 20 2f 2a 20 4c 69 73 74 20  gger,   /* List 
2bf37 6f 66 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74  of triggers on t
2bf38 61 62 6c 65 20 70 54 61 62 20 2a 2f 0a 20 20 69  able pTab */.  i
2bf39 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20  nt op,          
2bf3a 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 54 4b      /* Either TK
2bf3b 5f 55 50 44 41 54 45 20 6f 72 20 54 4b 5f 44 45  _UPDATE or TK_DE
2bf3c 4c 45 54 45 20 2a 2f 0a 20 20 45 78 70 72 4c 69  LETE */.  ExprLi
2bf3d 73 74 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 2f  st *pChanges,  /
2bf3e 2a 20 43 68 61 6e 67 65 73 20 6c 69 73 74 20 66  * Changes list f
2bf3f 6f 72 20 61 6e 79 20 55 50 44 41 54 45 20 4f 46  or any UPDATE OF
2bf40 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 54   triggers */.  T
2bf41 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20  able *pTab,     
2bf42 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
2bf43 20 74 6f 20 63 6f 64 65 20 74 72 69 67 67 65 72   to code trigger
2bf44 73 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20  s from */.  int 
2bf45 6f 72 63 6f 6e 66 20 20 20 20 20 20 20 20 20 20  orconf          
2bf46 20 2f 2a 20 44 65 66 61 75 6c 74 20 4f 4e 20 43   /* Default ON C
2bf47 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 66  ONFLICT policy f
2bf48 6f 72 20 74 72 69 67 67 65 72 20 73 74 65 70 73  or trigger steps
2bf49 20 2a 2f 0a 29 7b 0a 20 20 75 33 32 20 6d 61 73   */.){.  u32 mas
2bf4a 6b 20 3d 20 30 3b 0a 20 20 54 72 69 67 67 65 72  k = 0;.  Trigger
2bf4b 20 2a 70 3b 0a 0a 20 20 61 73 73 65 72 74 28 6f   *p;..  assert(o
2bf4c 70 3d 3d 54 4b 5f 55 50 44 41 54 45 20 7c 7c 20  p==TK_UPDATE || 
2bf4d 6f 70 3d 3d 54 4b 5f 44 45 4c 45 54 45 29 3b 0a  op==TK_DELETE);.
2bf4e 20 20 66 6f 72 28 70 3d 70 54 72 69 67 67 65 72    for(p=pTrigger
2bf4f 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29  ; p; p=p->pNext)
2bf50 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6f 70 3d  {.    if( p->op=
2bf51 3d 6f 70 20 26 26 20 63 68 65 63 6b 43 6f 6c 75  =op && checkColu
2bf52 6d 6e 4f 76 65 72 6c 61 70 28 70 2d 3e 70 43 6f  mnOverlap(p->pCo
2bf53 6c 75 6d 6e 73 2c 70 43 68 61 6e 67 65 73 29 20  lumns,pChanges) 
2bf54 29 7b 0a 20 20 20 20 20 20 54 72 69 67 67 65 72  ){.      Trigger
2bf55 50 72 67 20 2a 70 50 72 67 3b 0a 20 20 20 20 20  Prg *pPrg;.     
2bf56 20 70 50 72 67 20 3d 20 67 65 74 52 6f 77 54 72   pPrg = getRowTr
2bf57 69 67 67 65 72 28 70 50 61 72 73 65 2c 20 70 2c  igger(pParse, p,
2bf58 20 70 54 61 62 2c 20 6f 72 63 6f 6e 66 29 3b 0a   pTab, orconf);.
2bf59 20 20 20 20 20 20 69 66 28 20 70 50 72 67 20 29        if( pPrg )
2bf5a 7b 0a 20 20 20 20 20 20 20 20 6d 61 73 6b 20 7c  {.        mask |
2bf5b 3d 20 70 50 72 67 2d 3e 6f 6c 64 6d 61 73 6b 3b  = pPrg->oldmask;
2bf5c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2bf5d 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 6d 61 73   }..  return mas
2bf5e 6b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  k;.}..#endif /* 
2bf5f 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2bf60 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20 2a 2f  OMIT_TRIGGER) */
2bf61 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
2bf62 2a 20 45 6e 64 20 6f 66 20 74 72 69 67 67 65 72  * End of trigger
2bf63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
2bf64 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bf65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bf66 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
2bf67 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 75 70 64  * Begin file upd
2bf68 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ate.c **********
2bf69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bf6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bf6b 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
2bf6c 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
2bf6d 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
2bf6e 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
2bf6f 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
2bf70 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
2bf71 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
2bf72 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
2bf73 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
2bf74 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
2bf75 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
2bf76 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
2bf77 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
2bf78 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
2bf79 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
2bf7a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
2bf7b 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
2bf7c 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
2bf7d 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
2bf7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bf7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bf80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bf81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bf82 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
2bf83 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43   file contains C
2bf84 20 63 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74   code routines t
2bf85 68 61 74 20 61 72 65 20 63 61 6c 6c 65 64 20 62  hat are called b
2bf86 79 20 74 68 65 20 70 61 72 73 65 72 0a 2a 2a 20  y the parser.** 
2bf87 74 6f 20 68 61 6e 64 6c 65 20 55 50 44 41 54 45  to handle UPDATE
2bf88 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a   statements..**.
2bf89 2a 2a 20 24 49 64 3a 20 75 70 64 61 74 65 2e 63  ** $Id: update.c
2bf8a 2c 76 20 31 2e 32 30 37 20 32 30 30 39 2f 30 38  ,v 1.207 2009/08
2bf8b 2f 30 38 20 31 38 3a 30 31 3a 30 38 20 64 72 68  /08 18:01:08 drh
2bf8c 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64   Exp $.*/..#ifnd
2bf8d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
2bf8e 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 46  IRTUALTABLE./* F
2bf8f 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
2bf90 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  on */.static voi
2bf91 64 20 75 70 64 61 74 65 56 69 72 74 75 61 6c 54  d updateVirtualT
2bf92 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70  able(.  Parse *p
2bf93 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20  Parse,       /* 
2bf94 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74  The parsing cont
2bf95 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ext */.  SrcList
2bf96 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20 2f 2a   *pSrc,       /*
2bf97 20 54 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   The virtual tab
2bf98 6c 65 20 74 6f 20 62 65 20 6d 6f 64 69 66 69 65  le to be modifie
2bf99 64 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  d */.  Table *pT
2bf9a 61 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ab,         /* T
2bf9b 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
2bf9c 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
2bf9d 70 43 68 61 6e 67 65 73 2c 20 20 2f 2a 20 54 68  pChanges,  /* Th
2bf9e 65 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 63 68 61  e columns to cha
2bf9f 6e 67 65 20 69 6e 20 74 68 65 20 55 50 44 41 54  nge in the UPDAT
2bfa0 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  E statement */. 
2bfa1 20 45 78 70 72 20 2a 70 52 6f 77 69 64 45 78 70   Expr *pRowidExp
2bfa2 72 2c 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73  r,    /* Express
2bfa3 69 6f 6e 20 75 73 65 64 20 74 6f 20 72 65 63 6f  ion used to reco
2bfa4 6d 70 75 74 65 20 74 68 65 20 72 6f 77 69 64 20  mpute the rowid 
2bfa5 2a 2f 0a 20 20 69 6e 74 20 2a 61 58 52 65 66 2c  */.  int *aXRef,
2bfa6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 70            /* Map
2bfa7 70 69 6e 67 20 66 72 6f 6d 20 63 6f 6c 75 6d 6e  ping from column
2bfa8 73 20 6f 66 20 70 54 61 62 20 74 6f 20 65 6e 74  s of pTab to ent
2bfa9 72 69 65 73 20 69 6e 20 70 43 68 61 6e 67 65 73  ries in pChanges
2bfaa 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65   */.  Expr *pWhe
2bfab 72 65 20 20 20 20 20 20 20 20 20 2f 2a 20 57 48  re         /* WH
2bfac 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 68  ERE clause of th
2bfad 65 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65  e UPDATE stateme
2bfae 6e 74 20 2a 2f 0a 29 3b 0a 23 65 6e 64 69 66 20  nt */.);.#endif 
2bfaf 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
2bfb0 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a  IRTUALTABLE */..
2bfb1 2f 2a 0a 2a 2a 20 54 68 65 20 6d 6f 73 74 20 72  /*.** The most r
2bfb2 65 63 65 6e 74 6c 79 20 63 6f 64 65 64 20 69 6e  ecently coded in
2bfb3 73 74 72 75 63 74 69 6f 6e 20 77 61 73 20 61 6e  struction was an
2bfb4 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 72 65   OP_Column to re
2bfb5 74 72 69 65 76 65 20 74 68 65 0a 2a 2a 20 69 2d  trieve the.** i-
2bfb6 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62  th column of tab
2bfb7 6c 65 20 70 54 61 62 2e 20 54 68 69 73 20 72 6f  le pTab. This ro
2bfb8 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 50  utine sets the P
2bfb9 34 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74  4 parameter of t
2bfba 68 65 20 0a 2a 2a 20 4f 50 5f 43 6f 6c 75 6d 6e  he .** OP_Column
2bfbb 20 74 6f 20 74 68 65 20 64 65 66 61 75 6c 74 20   to the default 
2bfbc 76 61 6c 75 65 2c 20 69 66 20 61 6e 79 2e 0a 2a  value, if any..*
2bfbd 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74  *.** The default
2bfbe 20 76 61 6c 75 65 20 6f 66 20 61 20 63 6f 6c 75   value of a colu
2bfbf 6d 6e 20 69 73 20 73 70 65 63 69 66 69 65 64 20  mn is specified 
2bfc0 62 79 20 61 20 44 45 46 41 55 4c 54 20 63 6c 61  by a DEFAULT cla
2bfc1 75 73 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 63  use in the .** c
2bfc2 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e  olumn definition
2bfc3 2e 20 54 68 69 73 20 77 61 73 20 65 69 74 68 65  . This was eithe
2bfc4 72 20 73 75 70 70 6c 69 65 64 20 62 79 20 74 68  r supplied by th
2bfc5 65 20 75 73 65 72 20 77 68 65 6e 20 74 68 65 20  e user when the 
2bfc6 74 61 62 6c 65 0a 2a 2a 20 77 61 73 20 63 72 65  table.** was cre
2bfc7 61 74 65 64 2c 20 6f 72 20 61 64 64 65 64 20 6c  ated, or added l
2bfc8 61 74 65 72 20 74 6f 20 74 68 65 20 74 61 62 6c  ater to the tabl
2bfc9 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 62 79 20  e definition by 
2bfca 61 6e 20 41 4c 54 45 52 20 54 41 42 4c 45 0a 2a  an ALTER TABLE.*
2bfcb 2a 20 63 6f 6d 6d 61 6e 64 2e 20 49 66 20 74 68  * command. If th
2bfcc 65 20 6c 61 74 74 65 72 2c 20 74 68 65 6e 20 74  e latter, then t
2bfcd 68 65 20 72 6f 77 2d 72 65 63 6f 72 64 73 20 69  he row-records i
2bfce 6e 20 74 68 65 20 74 61 62 6c 65 20 62 74 72 65  n the table btre
2bfcf 65 20 6f 6e 20 64 69 73 6b 0a 2a 2a 20 6d 61 79  e on disk.** may
2bfd0 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 76   not contain a v
2bfd1 61 6c 75 65 20 66 6f 72 20 74 68 65 20 63 6f 6c  alue for the col
2bfd2 75 6d 6e 20 61 6e 64 20 74 68 65 20 64 65 66 61  umn and the defa
2bfd3 75 6c 74 20 76 61 6c 75 65 2c 20 74 61 6b 65 6e  ult value, taken
2bfd4 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 50 34 20  .** from the P4 
2bfd5 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68 65  parameter of the
2bfd6 20 4f 50 5f 43 6f 6c 75 6d 6e 20 69 6e 73 74 72   OP_Column instr
2bfd7 75 63 74 69 6f 6e 2c 20 69 73 20 72 65 74 75 72  uction, is retur
2bfd8 6e 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 20  ned instead..** 
2bfd9 49 66 20 74 68 65 20 66 6f 72 6d 65 72 2c 20 74  If the former, t
2bfda 68 65 6e 20 61 6c 6c 20 72 6f 77 2d 72 65 63 6f  hen all row-reco
2bfdb 72 64 73 20 61 72 65 20 67 75 61 72 61 6e 74 65  rds are guarante
2bfdc 65 64 20 74 6f 20 69 6e 63 6c 75 64 65 20 61 20  ed to include a 
2bfdd 76 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65  value.** for the
2bfde 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 74 68 65 20   column and the 
2bfdf 50 34 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20  P4 value is not 
2bfe0 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20  required..**.** 
2bfe1 43 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f  Column definitio
2bfe2 6e 73 20 63 72 65 61 74 65 64 20 62 79 20 61 6e  ns created by an
2bfe3 20 41 4c 54 45 52 20 54 41 42 4c 45 20 63 6f 6d   ALTER TABLE com
2bfe4 6d 61 6e 64 20 6d 61 79 20 6f 6e 6c 79 20 68 61  mand may only ha
2bfe5 76 65 20 0a 2a 2a 20 6c 69 74 65 72 61 6c 20 64  ve .** literal d
2bfe6 65 66 61 75 6c 74 20 76 61 6c 75 65 73 20 73 70  efault values sp
2bfe7 65 63 69 66 69 65 64 3a 20 61 20 6e 75 6d 62 65  ecified: a numbe
2bfe8 72 2c 20 6e 75 6c 6c 20 6f 72 20 61 20 73 74 72  r, null or a str
2bfe9 69 6e 67 2e 20 28 49 66 20 61 20 6d 6f 72 65 0a  ing. (If a more.
2bfea 2a 2a 20 63 6f 6d 70 6c 69 63 61 74 65 64 20 64  ** complicated d
2bfeb 65 66 61 75 6c 74 20 65 78 70 72 65 73 73 69 6f  efault expressio
2bfec 6e 20 76 61 6c 75 65 20 77 61 73 20 70 72 6f 76  n value was prov
2bfed 69 64 65 64 2c 20 69 74 20 69 73 20 65 76 61 6c  ided, it is eval
2bfee 75 61 74 65 64 20 0a 2a 2a 20 77 68 65 6e 20 74  uated .** when t
2bfef 68 65 20 41 4c 54 45 52 20 54 41 42 4c 45 20 69  he ALTER TABLE i
2bff0 73 20 65 78 65 63 75 74 65 64 20 61 6e 64 20 6f  s executed and o
2bff1 6e 65 20 6f 66 20 74 68 65 20 6c 69 74 65 72 61  ne of the litera
2bff2 6c 20 76 61 6c 75 65 73 20 77 72 69 74 74 65 6e  l values written
2bff3 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 71 6c  .** into the sql
2bff4 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
2bff5 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 66 6f  .).**.** Therefo
2bff6 72 65 2c 20 74 68 65 20 50 34 20 70 61 72 61 6d  re, the P4 param
2bff7 65 74 65 72 20 69 73 20 6f 6e 6c 79 20 72 65 71  eter is only req
2bff8 75 69 72 65 64 20 69 66 20 74 68 65 20 64 65 66  uired if the def
2bff9 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 0a 2a  ault value for.*
2bffa 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20  * the column is 
2bffb 61 20 6c 69 74 65 72 61 6c 20 6e 75 6d 62 65 72  a literal number
2bffc 2c 20 73 74 72 69 6e 67 20 6f 72 20 6e 75 6c 6c  , string or null
2bffd 2e 20 54 68 65 20 73 71 6c 69 74 65 33 56 61 6c  . The sqlite3Val
2bffe 75 65 46 72 6f 6d 45 78 70 72 28 29 0a 2a 2a 20  ueFromExpr().** 
2bfff 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 70 61  function is capa
2c000 62 6c 65 20 6f 66 20 74 72 61 6e 73 66 6f 72 6d  ble of transform
2c001 69 6e 67 20 74 68 65 73 65 20 74 79 70 65 73 20  ing these types 
2c002 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69  of expressions i
2c003 6e 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76  nto.** sqlite3_v
2c004 61 6c 75 65 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a  alue objects..**
2c005 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 65 72  .** If parameter
2c006 20 69 52 65 67 20 69 73 20 6e 6f 74 20 6e 65 67   iReg is not neg
2c007 61 74 69 76 65 2c 20 63 6f 64 65 20 61 6e 20 4f  ative, code an O
2c008 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 20 69  P_RealAffinity i
2c009 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 6f 6e  nstruction.** on
2c00a 20 72 65 67 69 73 74 65 72 20 69 52 65 67 2e 20   register iReg. 
2c00b 54 68 69 73 20 69 73 20 75 73 65 64 20 77 68 65  This is used whe
2c00c 6e 20 61 6e 20 65 71 75 69 76 61 6c 65 6e 74 20  n an equivalent 
2c00d 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 73  integer value is
2c00e 20 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 70   .** stored in p
2c00f 6c 61 63 65 20 6f 66 20 61 6e 20 38 2d 62 79 74  lace of an 8-byt
2c010 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  e floating point
2c011 20 76 61 6c 75 65 20 69 6e 20 6f 72 64 65 72 20   value in order 
2c012 74 6f 20 73 61 76 65 20 0a 2a 2a 20 73 70 61 63  to save .** spac
2c013 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
2c014 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2c015 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 56  3ColumnDefault(V
2c016 64 62 65 20 2a 76 2c 20 54 61 62 6c 65 20 2a 70  dbe *v, Table *p
2c017 54 61 62 2c 20 69 6e 74 20 69 2c 20 69 6e 74 20  Tab, int i, int 
2c018 69 52 65 67 29 7b 0a 20 20 61 73 73 65 72 74 28  iReg){.  assert(
2c019 20 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 69 66   pTab!=0 );.  if
2c01a 28 20 21 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  ( !pTab->pSelect
2c01b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
2c01c 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 3b 0a 20  value *pValue;. 
2c01d 20 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28     u8 enc = ENC(
2c01e 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29  sqlite3VdbeDb(v)
2c01f 29 3b 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70  );.    Column *p
2c020 43 6f 6c 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f  Col = &pTab->aCo
2c021 6c 5b 69 5d 3b 0a 20 20 20 20 56 64 62 65 43 6f  l[i];.    VdbeCo
2c022 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 2e 25 73  mment((v, "%s.%s
2c023 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  ", pTab->zName, 
2c024 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20  pCol->zName));. 
2c025 20 20 20 61 73 73 65 72 74 28 20 69 3c 70 54 61     assert( i<pTa
2c026 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 73  b->nCol );.    s
2c027 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45  qlite3ValueFromE
2c028 78 70 72 28 73 71 6c 69 74 65 33 56 64 62 65 44  xpr(sqlite3VdbeD
2c029 62 28 76 29 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c  b(v), pCol->pDfl
2c02a 74 2c 20 65 6e 63 2c 20 0a 20 20 20 20 20 20 20  t, enc, .       
2c02b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c02c 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79    pCol->affinity
2c02d 2c 20 26 70 56 61 6c 75 65 29 3b 0a 20 20 20 20  , &pValue);.    
2c02e 69 66 28 20 70 56 61 6c 75 65 20 29 7b 0a 20 20  if( pValue ){.  
2c02f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2c030 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28  hangeP4(v, -1, (
2c031 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 56 61  const char *)pVa
2c032 6c 75 65 2c 20 50 34 5f 4d 45 4d 29 3b 0a 20 20  lue, P4_MEM);.  
2c033 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
2c034 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
2c035 5f 50 4f 49 4e 54 0a 20 20 20 20 69 66 28 20 69  _POINT.    if( i
2c036 52 65 67 3e 3d 30 20 26 26 20 70 54 61 62 2d 3e  Reg>=0 && pTab->
2c037 61 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79  aCol[i].affinity
2c038 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41  ==SQLITE_AFF_REA
2c039 4c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  L ){.      sqlit
2c03a 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2c03b 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 2c  OP_RealAffinity,
2c03c 20 69 52 65 67 29 3b 0a 20 20 20 20 7d 0a 23 65   iReg);.    }.#e
2c03d 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ndif.  }.}../*.*
2c03e 2a 20 50 72 6f 63 65 73 73 20 61 6e 20 55 50 44  * Process an UPD
2c03f 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ATE statement..*
2c040 2a 0a 2a 2a 20 20 20 55 50 44 41 54 45 20 4f 52  *.**   UPDATE OR
2c041 20 49 47 4e 4f 52 45 20 74 61 62 6c 65 5f 77 78   IGNORE table_wx
2c042 79 7a 20 53 45 54 20 61 3d 62 2c 20 63 3d 64 20  yz SET a=b, c=d 
2c043 57 48 45 52 45 20 65 3c 35 20 41 4e 44 20 66 20  WHERE e<5 AND f 
2c044 4e 4f 54 20 4e 55 4c 4c 3b 0a 2a 2a 20 20 20 20  NOT NULL;.**    
2c045 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 2f 20        \_______/ 
2c046 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 5c  \________/     \
2c047 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 5c 5f  ______/       \_
2c048 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f  _______________/
2c049 0a 2a 20 20 20 20 20 20 20 20 20 20 20 20 6f 6e  .*            on
2c04a 45 72 72 6f 72 20 20 20 70 54 61 62 4c 69 73 74  Error   pTabList
2c04b 20 20 20 20 20 20 70 43 68 61 6e 67 65 73 20 20        pChanges  
2c04c 20 20 20 20 20 20 20 20 20 20 20 70 57 68 65 72             pWher
2c04d 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  e.*/.SQLITE_PRIV
2c04e 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2c04f 55 70 64 61 74 65 28 0a 20 20 50 61 72 73 65 20  Update(.  Parse 
2c050 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2c051 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
2c052 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c  ontext */.  SrcL
2c053 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20  ist *pTabList,  
2c054 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
2c055 69 6e 20 77 68 69 63 68 20 77 65 20 73 68 6f 75  in which we shou
2c056 6c 64 20 63 68 61 6e 67 65 20 74 68 69 6e 67 73  ld change things
2c057 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
2c058 70 43 68 61 6e 67 65 73 2c 20 20 20 20 2f 2a 20  pChanges,    /* 
2c059 54 68 69 6e 67 73 20 74 6f 20 62 65 20 63 68 61  Things to be cha
2c05a 6e 67 65 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a  nged */.  Expr *
2c05b 70 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 20  pWhere,         
2c05c 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c   /* The WHERE cl
2c05d 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75  ause.  May be nu
2c05e 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72  ll */.  int onEr
2c05f 72 6f 72 20 20 20 20 20 20 20 20 20 20 20 20 2f  ror            /
2c060 2a 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c 65 20  * How to handle 
2c061 63 6f 6e 73 74 72 61 69 6e 74 20 65 72 72 6f 72  constraint error
2c062 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c  s */.){.  int i,
2c063 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   j;             
2c064 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
2c065 73 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  s */.  Table *pT
2c066 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ab;           /*
2c067 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65   The table to be
2c068 20 75 70 64 61 74 65 64 20 2a 2f 0a 20 20 69 6e   updated */.  in
2c069 74 20 61 64 64 72 20 3d 20 30 3b 20 20 20 20 20  t addr = 0;     
2c06a 20 20 20 20 20 2f 2a 20 56 44 42 45 20 69 6e 73       /* VDBE ins
2c06b 74 72 75 63 74 69 6f 6e 20 61 64 64 72 65 73 73  truction address
2c06c 20 6f 66 20 74 68 65 20 73 74 61 72 74 20 6f 66   of the start of
2c06d 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 57   the loop */.  W
2c06e 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f  hereInfo *pWInfo
2c06f 3b 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61  ;     /* Informa
2c070 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 57  tion about the W
2c071 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
2c072 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20   Vdbe *v;       
2c073 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76          /* The v
2c074 69 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20  irtual database 
2c075 65 6e 67 69 6e 65 20 2a 2f 0a 20 20 49 6e 64 65  engine */.  Inde
2c076 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20  x *pIdx;        
2c077 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e     /* For loopin
2c078 67 20 6f 76 65 72 20 69 6e 64 69 63 65 73 20 2a  g over indices *
2c079 2f 0a 20 20 69 6e 74 20 6e 49 64 78 3b 20 20 20  /.  int nIdx;   
2c07a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
2c07b 6d 62 65 72 20 6f 66 20 69 6e 64 69 63 65 73 20  mber of indices 
2c07c 74 68 61 74 20 6e 65 65 64 20 75 70 64 61 74 69  that need updati
2c07d 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72  ng */.  int iCur
2c07e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2c07f 2a 20 56 44 42 45 20 43 75 72 73 6f 72 20 6e 75  * VDBE Cursor nu
2c080 6d 62 65 72 20 6f 66 20 70 54 61 62 20 2a 2f 0a  mber of pTab */.
2c081 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
2c082 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2c083 64 61 74 61 62 61 73 65 20 73 74 72 75 63 74 75  database structu
2c084 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65  re */.  int *aRe
2c085 67 49 64 78 20 3d 20 30 3b 20 20 20 20 20 20 2f  gIdx = 0;      /
2c086 2a 20 4f 6e 65 20 72 65 67 69 73 74 65 72 20 61  * One register a
2c087 73 73 69 67 6e 65 64 20 74 6f 20 65 61 63 68 20  ssigned to each 
2c088 69 6e 64 65 78 20 74 6f 20 62 65 20 75 70 64 61  index to be upda
2c089 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 58  ted */.  int *aX
2c08a 52 65 66 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Ref = 0;        
2c08b 2f 2a 20 61 58 52 65 66 5b 69 5d 20 69 73 20 74  /* aXRef[i] is t
2c08c 68 65 20 69 6e 64 65 78 20 69 6e 20 70 43 68 61  he index in pCha
2c08d 6e 67 65 73 2d 3e 61 5b 5d 20 6f 66 20 74 68 65  nges->a[] of the
2c08e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2c08f 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 20            ** an 
2c090 65 78 70 72 65 73 73 69 6f 6e 20 66 6f 72 20 74  expression for t
2c091 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f  he i-th column o
2c092 66 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20 20  f the table..   
2c093 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c094 20 20 20 20 20 20 2a 2a 20 61 58 52 65 66 5b 69        ** aXRef[i
2c095 5d 3d 3d 2d 31 20 69 66 20 74 68 65 20 69 2d 74  ]==-1 if the i-t
2c096 68 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20  h column is not 
2c097 63 68 61 6e 67 65 64 2e 20 2a 2f 0a 20 20 69 6e  changed. */.  in
2c098 74 20 63 68 6e 67 52 6f 77 69 64 3b 20 20 20 20  t chngRowid;    
2c099 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
2c09a 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  the record numbe
2c09b 72 20 69 73 20 62 65 69 6e 67 20 63 68 61 6e 67  r is being chang
2c09c 65 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52  ed */.  Expr *pR
2c09d 6f 77 69 64 45 78 70 72 20 3d 20 30 3b 20 20 2f  owidExpr = 0;  /
2c09e 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 64 65 66  * Expression def
2c09f 69 6e 69 6e 67 20 74 68 65 20 6e 65 77 20 72 65  ining the new re
2c0a0 63 6f 72 64 20 6e 75 6d 62 65 72 20 2a 2f 0a 20  cord number */. 
2c0a1 20 69 6e 74 20 6f 70 65 6e 41 6c 6c 20 3d 20 30   int openAll = 0
2c0a2 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  ;       /* True 
2c0a3 69 66 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6e  if all indices n
2c0a4 65 65 64 20 74 6f 20 62 65 20 6f 70 65 6e 65 64  eed to be opened
2c0a5 20 2a 2f 0a 20 20 41 75 74 68 43 6f 6e 74 65 78   */.  AuthContex
2c0a6 74 20 73 43 6f 6e 74 65 78 74 3b 20 20 2f 2a 20  t sContext;  /* 
2c0a7 54 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f  The authorizatio
2c0a8 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 4e  n context */.  N
2c0a9 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20  ameContext sNC; 
2c0aa 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d        /* The nam
2c0ab 65 2d 63 6f 6e 74 65 78 74 20 74 6f 20 72 65 73  e-context to res
2c0ac 6f 6c 76 65 20 65 78 70 72 65 73 73 69 6f 6e 73  olve expressions
2c0ad 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62   in */.  int iDb
2c0ae 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2c0af 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 74  /* Database cont
2c0b0 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65  aining the table
2c0b1 20 62 65 69 6e 67 20 75 70 64 61 74 65 64 20 2a   being updated *
2c0b2 2f 0a 20 20 69 6e 74 20 6a 31 3b 20 20 20 20 20  /.  int j1;     
2c0b3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
2c0b4 64 72 65 73 73 65 73 20 6f 66 20 6a 75 6d 70 20  dresses of jump 
2c0b5 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0a  instructions */.
2c0b6 20 20 69 6e 74 20 6f 6b 4f 6e 65 50 61 73 73 3b    int okOnePass;
2c0b7 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
2c0b8 20 66 6f 72 20 6f 6e 65 2d 70 61 73 73 20 61 6c   for one-pass al
2c0b9 67 6f 72 69 74 68 6d 20 77 69 74 68 6f 75 74 20  gorithm without 
2c0ba 74 68 65 20 46 49 46 4f 20 2a 2f 0a 0a 23 69 66  the FIFO */..#if
2c0bb 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2c0bc 5f 54 52 49 47 47 45 52 0a 20 20 69 6e 74 20 69  _TRIGGER.  int i
2c0bd 73 56 69 65 77 3b 20 20 20 20 20 20 20 20 20 20  sView;          
2c0be 20 20 20 20 20 20 20 20 2f 2a 20 54 72 79 69 6e          /* Tryin
2c0bf 67 20 74 6f 20 75 70 64 61 74 65 20 61 20 76 69  g to update a vi
2c0c0 65 77 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20  ew */.  Trigger 
2c0c1 2a 70 54 72 69 67 67 65 72 3b 20 20 20 20 20 20  *pTrigger;      
2c0c2 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
2c0c3 74 72 69 67 67 65 72 73 20 6f 6e 20 70 54 61 62  triggers on pTab
2c0c4 2c 20 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f  , if required */
2c0c5 0a 23 65 6e 64 69 66 0a 20 20 75 33 32 20 6f 6c  .#endif.  u32 ol
2c0c6 64 6d 61 73 6b 20 3d 20 30 3b 20 20 20 20 20 20  dmask = 0;      
2c0c7 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 4f 4c 44    /* Mask of OLD
2c0c8 2e 2a 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 75 73  .* columns in us
2c0c9 65 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 67 69 73  e */..  /* Regis
2c0ca 74 65 72 20 41 6c 6c 6f 63 61 74 69 6f 6e 73 20  ter Allocations 
2c0cb 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 43  */.  int regRowC
2c0cc 6f 75 6e 74 20 3d 20 30 3b 20 20 20 2f 2a 20 41  ount = 0;   /* A
2c0cd 20 63 6f 75 6e 74 20 6f 66 20 72 6f 77 73 20 63   count of rows c
2c0ce 68 61 6e 67 65 64 20 2a 2f 0a 20 20 69 6e 74 20  hanged */.  int 
2c0cf 72 65 67 4f 6c 64 52 6f 77 69 64 3b 20 20 20 20  regOldRowid;    
2c0d0 20 20 20 2f 2a 20 54 68 65 20 6f 6c 64 20 72 6f     /* The old ro
2c0d1 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  wid */.  int reg
2c0d2 4e 65 77 52 6f 77 69 64 3b 20 20 20 20 20 20 20  NewRowid;       
2c0d3 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f 77 69 64  /* The new rowid
2c0d4 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4e 65 77   */.  int regNew
2c0d5 3b 0a 20 20 69 6e 74 20 72 65 67 4f 6c 64 20 3d  ;.  int regOld =
2c0d6 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 52 6f 77   0;.  int regRow
2c0d7 53 65 74 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  Set = 0;     /* 
2c0d8 52 6f 77 73 65 74 20 6f 66 20 72 6f 77 73 20 74  Rowset of rows t
2c0d9 6f 20 62 65 20 75 70 64 61 74 65 64 20 2a 2f 0a  o be updated */.
2c0da 20 20 69 6e 74 20 72 65 67 52 65 63 3b 20 20 20    int regRec;   
2c0db 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69           /* Regi
2c0dc 73 74 65 72 20 75 73 65 64 20 66 6f 72 20 6e 65  ster used for ne
2c0dd 77 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20 74  w table record t
2c0de 6f 20 69 6e 73 65 72 74 20 2a 2f 0a 0a 20 20 6d  o insert */..  m
2c0df 65 6d 73 65 74 28 26 73 43 6f 6e 74 65 78 74 2c  emset(&sContext,
2c0e0 20 30 2c 20 73 69 7a 65 6f 66 28 73 43 6f 6e 74   0, sizeof(sCont
2c0e1 65 78 74 29 29 3b 0a 20 20 64 62 20 3d 20 70 50  ext));.  db = pP
2c0e2 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20  arse->db;.  if( 
2c0e3 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
2c0e4 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2c0e5 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 75 70 64   ){.    goto upd
2c0e6 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ate_cleanup;.  }
2c0e7 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 4c  .  assert( pTabL
2c0e8 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a  ist->nSrc==1 );.
2c0e9 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65  .  /* Locate the
2c0ea 20 74 61 62 6c 65 20 77 68 69 63 68 20 77 65 20   table which we 
2c0eb 77 61 6e 74 20 74 6f 20 75 70 64 61 74 65 2e 20  want to update. 
2c0ec 0a 20 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 73  .  */.  pTab = s
2c0ed 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f  qlite3SrcListLoo
2c0ee 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54 61 62  kup(pParse, pTab
2c0ef 4c 69 73 74 29 3b 0a 20 20 69 66 28 20 70 54 61  List);.  if( pTa
2c0f0 62 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70 64 61  b==0 ) goto upda
2c0f1 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 69 44  te_cleanup;.  iD
2c0f2 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
2c0f3 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
2c0f4 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  >db, pTab->pSche
2c0f5 6d 61 29 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72  ma);..  /* Figur
2c0f6 65 20 6f 75 74 20 69 66 20 77 65 20 68 61 76 65  e out if we have
2c0f7 20 61 6e 79 20 74 72 69 67 67 65 72 73 20 61 6e   any triggers an
2c0f8 64 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 62  d if the table b
2c0f9 65 69 6e 67 0a 20 20 2a 2a 20 75 70 64 61 74 65  eing.  ** update
2c0fa 64 20 69 73 20 61 20 76 69 65 77 2e 0a 20 20 2a  d is a view..  *
2c0fb 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
2c0fc 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20  _OMIT_TRIGGER.  
2c0fd 70 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74  pTrigger = sqlit
2c0fe 65 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28  e3TriggersExist(
2c0ff 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 54 4b  pParse, pTab, TK
2c100 5f 55 50 44 41 54 45 2c 20 70 43 68 61 6e 67 65  _UPDATE, pChange
2c101 73 2c 20 30 29 3b 0a 20 20 69 73 56 69 65 77 20  s, 0);.  isView 
2c102 3d 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 21  = pTab->pSelect!
2c103 3d 30 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  =0;.#else.# defi
2c104 6e 65 20 70 54 72 69 67 67 65 72 20 30 0a 23 20  ne pTrigger 0.# 
2c105 64 65 66 69 6e 65 20 69 73 56 69 65 77 20 30 0a  define isView 0.
2c106 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51  #endif.#ifdef SQ
2c107 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 23  LITE_OMIT_VIEW.#
2c108 20 75 6e 64 65 66 20 69 73 56 69 65 77 0a 23 20   undef isView.# 
2c109 64 65 66 69 6e 65 20 69 73 56 69 65 77 20 30 0a  define isView 0.
2c10a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 73 71  #endif..  if( sq
2c10b 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75  lite3ViewGetColu
2c10c 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20  mnNames(pParse, 
2c10d 70 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74  pTab) ){.    got
2c10e 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70  o update_cleanup
2c10f 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69  ;.  }.  if( sqli
2c110 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 70 50  te3IsReadOnly(pP
2c111 61 72 73 65 2c 20 70 54 61 62 2c 20 28 70 54 72  arse, pTab, (pTr
2c112 69 67 67 65 72 3f 31 3a 30 29 29 20 29 7b 0a 20  igger?1:0)) ){. 
2c113 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63     goto update_c
2c114 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61 58  leanup;.  }.  aX
2c115 52 65 66 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  Ref = sqlite3DbM
2c116 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a  allocRaw(db, siz
2c117 65 6f 66 28 69 6e 74 29 20 2a 20 70 54 61 62 2d  eof(int) * pTab-
2c118 3e 6e 43 6f 6c 20 29 3b 0a 20 20 69 66 28 20 61  >nCol );.  if( a
2c119 58 52 65 66 3d 3d 30 20 29 20 67 6f 74 6f 20 75  XRef==0 ) goto u
2c11a 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  pdate_cleanup;. 
2c11b 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62   for(i=0; i<pTab
2c11c 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 20 61 58 52  ->nCol; i++) aXR
2c11d 65 66 5b 69 5d 20 3d 20 2d 31 3b 0a 0a 20 20 2f  ef[i] = -1;..  /
2c11e 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 63 75 72  * Allocate a cur
2c11f 73 6f 72 73 20 66 6f 72 20 74 68 65 20 6d 61 69  sors for the mai
2c120 6e 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  n database table
2c121 20 61 6e 64 20 66 6f 72 20 61 6c 6c 20 69 6e 64   and for all ind
2c122 69 63 65 73 2e 0a 20 20 2a 2a 20 54 68 65 20 69  ices..  ** The i
2c123 6e 64 65 78 20 63 75 72 73 6f 72 73 20 6d 69 67  ndex cursors mig
2c124 68 74 20 6e 6f 74 20 62 65 20 75 73 65 64 2c 20  ht not be used, 
2c125 62 75 74 20 69 66 20 74 68 65 79 20 61 72 65 20  but if they are 
2c126 75 73 65 64 20 74 68 65 79 0a 20 20 2a 2a 20 6e  used they.  ** n
2c127 65 65 64 20 74 6f 20 6f 63 63 75 72 20 72 69 67  eed to occur rig
2c128 68 74 20 61 66 74 65 72 20 74 68 65 20 64 61 74  ht after the dat
2c129 61 62 61 73 65 20 63 75 72 73 6f 72 2e 20 20 53  abase cursor.  S
2c12a 6f 20 67 6f 20 61 68 65 61 64 20 61 6e 64 0a 20  o go ahead and. 
2c12b 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 65 6e 6f   ** allocate eno
2c12c 75 67 68 20 73 70 61 63 65 2c 20 6a 75 73 74 20  ugh space, just 
2c12d 69 6e 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20 20  in case..  */.  
2c12e 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69  pTabList->a[0].i
2c12f 43 75 72 73 6f 72 20 3d 20 69 43 75 72 20 3d 20  Cursor = iCur = 
2c130 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
2c131 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d    for(pIdx=pTab-
2c132 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
2c133 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
2c134 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54  {.    pParse->nT
2c135 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ab++;.  }..  /* 
2c136 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6e  Initialize the n
2c137 61 6d 65 2d 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ame-context */. 
2c138 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c   memset(&sNC, 0,
2c139 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20   sizeof(sNC));. 
2c13a 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50   sNC.pParse = pP
2c13b 61 72 73 65 3b 0a 20 20 73 4e 43 2e 70 53 72 63  arse;.  sNC.pSrc
2c13c 4c 69 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b  List = pTabList;
2c13d 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74  ..  /* Resolve t
2c13e 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  he column names 
2c13f 69 6e 20 61 6c 6c 20 74 68 65 20 65 78 70 72 65  in all the expre
2c140 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 0a 20 20  ssions of the.  
2c141 2a 2a 20 6f 66 20 74 68 65 20 55 50 44 41 54 45  ** of the UPDATE
2c142 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 6c 73   statement.  Als
2c143 6f 20 66 69 6e 64 20 74 68 65 20 63 6f 6c 75 6d  o find the colum
2c144 6e 20 69 6e 64 65 78 0a 20 20 2a 2a 20 66 6f 72  n index.  ** for
2c145 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 74 6f 20   each column to 
2c146 62 65 20 75 70 64 61 74 65 64 20 69 6e 20 74 68  be updated in th
2c147 65 20 70 43 68 61 6e 67 65 73 20 61 72 72 61 79  e pChanges array
2c148 2e 20 20 46 6f 72 20 65 61 63 68 0a 20 20 2a 2a  .  For each.  **
2c149 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 75 70   column to be up
2c14a 64 61 74 65 64 2c 20 6d 61 6b 65 20 73 75 72 65  dated, make sure
2c14b 20 77 65 20 68 61 76 65 20 61 75 74 68 6f 72 69   we have authori
2c14c 7a 61 74 69 6f 6e 20 74 6f 20 63 68 61 6e 67 65  zation to change
2c14d 0a 20 20 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d  .  ** that colum
2c14e 6e 2e 0a 20 20 2a 2f 0a 20 20 63 68 6e 67 52 6f  n..  */.  chngRo
2c14f 77 69 64 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69  wid = 0;.  for(i
2c150 3d 30 3b 20 69 3c 70 43 68 61 6e 67 65 73 2d 3e  =0; i<pChanges->
2c151 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nExpr; i++){.   
2c152 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f   if( sqlite3Reso
2c153 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e  lveExprNames(&sN
2c154 43 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69  C, pChanges->a[i
2c155 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20  ].pExpr) ){.    
2c156 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c    goto update_cl
2c157 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20  eanup;.    }.   
2c158 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 54 61 62   for(j=0; j<pTab
2c159 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20  ->nCol; j++){.  
2c15a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
2c15b 74 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f  trICmp(pTab->aCo
2c15c 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 70 43 68 61  l[j].zName, pCha
2c15d 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  nges->a[i].zName
2c15e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
2c15f 69 66 28 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b  if( j==pTab->iPK
2c160 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ey ){.          
2c161 63 68 6e 67 52 6f 77 69 64 20 3d 20 31 3b 0a 20  chngRowid = 1;. 
2c162 20 20 20 20 20 20 20 20 20 70 52 6f 77 69 64 45           pRowidE
2c163 78 70 72 20 3d 20 70 43 68 61 6e 67 65 73 2d 3e  xpr = pChanges->
2c164 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[i].pExpr;.    
2c165 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 58      }.        aX
2c166 52 65 66 5b 6a 5d 20 3d 20 69 3b 0a 20 20 20 20  Ref[j] = i;.    
2c167 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2c168 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
2c169 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29   j>=pTab->nCol )
2c16a 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  {.      if( sqli
2c16b 74 65 33 49 73 52 6f 77 69 64 28 70 43 68 61 6e  te3IsRowid(pChan
2c16c 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29  ges->a[i].zName)
2c16d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 6e 67   ){.        chng
2c16e 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 20  Rowid = 1;.     
2c16f 20 20 20 70 52 6f 77 69 64 45 78 70 72 20 3d 20     pRowidExpr = 
2c170 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 70  pChanges->a[i].p
2c171 45 78 70 72 3b 0a 20 20 20 20 20 20 7d 65 6c 73  Expr;.      }els
2c172 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e{.        sqlit
2c173 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2c174 65 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 75  e, "no such colu
2c175 6d 6e 3a 20 25 73 22 2c 20 70 43 68 61 6e 67 65  mn: %s", pChange
2c176 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a  s->a[i].zName);.
2c177 20 20 20 20 20 20 20 20 67 6f 74 6f 20 75 70 64          goto upd
2c178 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  ate_cleanup;.   
2c179 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 6e 64     }.    }.#ifnd
2c17a 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
2c17b 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20  UTHORIZATION.   
2c17c 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b   {.      int rc;
2c17d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
2c17e 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
2c17f 72 73 65 2c 20 53 51 4c 49 54 45 5f 55 50 44 41  rse, SQLITE_UPDA
2c180 54 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  TE, pTab->zName,
2c181 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2c182 20 20 20 20 20 20 20 20 20 20 20 20 70 54 61 62              pTab
2c183 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c  ->aCol[j].zName,
2c184 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
2c185 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ame);.      if( 
2c186 72 63 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20  rc==SQLITE_DENY 
2c187 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  ){.        goto 
2c188 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
2c189 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
2c18a 72 63 3d 3d 53 51 4c 49 54 45 5f 49 47 4e 4f 52  rc==SQLITE_IGNOR
2c18b 45 20 29 7b 0a 20 20 20 20 20 20 20 20 61 58 52  E ){.        aXR
2c18c 65 66 5b 6a 5d 20 3d 20 2d 31 3b 0a 20 20 20 20  ef[j] = -1;.    
2c18d 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66    }.    }.#endif
2c18e 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  .  }..  /* Alloc
2c18f 61 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74  ate memory for t
2c190 68 65 20 61 72 72 61 79 20 61 52 65 67 49 64 78  he array aRegIdx
2c191 5b 5d 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e  [].  There is on
2c192 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65 0a 20  e entry in the. 
2c193 20 2a 2a 20 61 72 72 61 79 20 66 6f 72 20 65 61   ** array for ea
2c194 63 68 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61  ch index associa
2c195 74 65 64 20 77 69 74 68 20 74 61 62 6c 65 20 62  ted with table b
2c196 65 69 6e 67 20 75 70 64 61 74 65 64 2e 20 20 46  eing updated.  F
2c197 69 6c 6c 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20  ill in.  ** the 
2c198 76 61 6c 75 65 20 77 69 74 68 20 61 20 72 65 67  value with a reg
2c199 69 73 74 65 72 20 6e 75 6d 62 65 72 20 66 6f 72  ister number for
2c19a 20 69 6e 64 69 63 65 73 20 74 68 61 74 20 61 72   indices that ar
2c19b 65 20 74 6f 20 62 65 20 75 73 65 64 0a 20 20 2a  e to be used.  *
2c19c 2a 20 61 6e 64 20 77 69 74 68 20 7a 65 72 6f 20  * and with zero 
2c19d 66 6f 72 20 75 6e 75 73 65 64 20 69 6e 64 69 63  for unused indic
2c19e 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 6e  es..  */.  for(n
2c19f 49 64 78 3d 30 2c 20 70 49 64 78 3d 70 54 61 62  Idx=0, pIdx=pTab
2c1a0 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
2c1a1 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
2c1a2 2c 20 6e 49 64 78 2b 2b 29 7b 7d 0a 20 20 69 66  , nIdx++){}.  if
2c1a3 28 20 6e 49 64 78 3e 30 20 29 7b 0a 20 20 20 20  ( nIdx>0 ){.    
2c1a4 61 52 65 67 49 64 78 20 3d 20 73 71 6c 69 74 65  aRegIdx = sqlite
2c1a5 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
2c1a6 20 73 69 7a 65 6f 66 28 49 6e 64 65 78 2a 29 20   sizeof(Index*) 
2c1a7 2a 20 6e 49 64 78 20 29 3b 0a 20 20 20 20 69 66  * nIdx );.    if
2c1a8 28 20 61 52 65 67 49 64 78 3d 3d 30 20 29 20 67  ( aRegIdx==0 ) g
2c1a9 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e  oto update_clean
2c1aa 75 70 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 6a 3d  up;.  }.  for(j=
2c1ab 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  0, pIdx=pTab->pI
2c1ac 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
2c1ad 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6a 2b  =pIdx->pNext, j+
2c1ae 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 67 3b  +){.    int reg;
2c1af 0a 20 20 20 20 69 66 28 20 63 68 6e 67 52 6f 77  .    if( chngRow
2c1b0 69 64 20 29 7b 0a 20 20 20 20 20 20 72 65 67 20  id ){.      reg 
2c1b1 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
2c1b2 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2c1b3 20 20 20 72 65 67 20 3d 20 30 3b 0a 20 20 20 20     reg = 0;.    
2c1b4 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64    for(i=0; i<pId
2c1b5 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29  x->nColumn; i++)
2c1b6 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 58  {.        if( aX
2c1b7 52 65 66 5b 70 49 64 78 2d 3e 61 69 43 6f 6c 75  Ref[pIdx->aiColu
2c1b8 6d 6e 5b 69 5d 5d 3e 3d 30 20 29 7b 0a 20 20 20  mn[i]]>=0 ){.   
2c1b9 20 20 20 20 20 20 20 72 65 67 20 3d 20 2b 2b 70         reg = ++p
2c1ba 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
2c1bb 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2c1bc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2c1bd 20 20 20 20 7d 0a 20 20 20 20 61 52 65 67 49 64      }.    aRegId
2c1be 78 5b 6a 5d 20 3d 20 72 65 67 3b 0a 20 20 7d 0a  x[j] = reg;.  }.
2c1bf 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67 65 6e 65  .  /* Begin gene
2c1c0 72 61 74 69 6e 67 20 63 6f 64 65 2e 20 2a 2f 0a  rating code. */.
2c1c1 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
2c1c2 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
2c1c3 69 66 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20  if( v==0 ) goto 
2c1c4 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
2c1c5 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65    if( pParse->ne
2c1c6 73 74 65 64 3d 3d 30 20 29 20 73 71 6c 69 74 65  sted==0 ) sqlite
2c1c7 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65  3VdbeCountChange
2c1c8 73 28 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  s(v);.  sqlite3B
2c1c9 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
2c1ca 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44  on(pParse, 1, iD
2c1cb 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  b);..#ifndef SQL
2c1cc 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2c1cd 54 41 42 4c 45 0a 20 20 2f 2a 20 56 69 72 74 75  TABLE.  /* Virtu
2c1ce 61 6c 20 74 61 62 6c 65 73 20 6d 75 73 74 20 62  al tables must b
2c1cf 65 20 68 61 6e 64 6c 65 64 20 73 65 70 61 72 61  e handled separa
2c1d0 74 65 6c 79 20 2a 2f 0a 20 20 69 66 28 20 49 73  tely */.  if( Is
2c1d1 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
2c1d2 0a 20 20 20 20 75 70 64 61 74 65 56 69 72 74 75  .    updateVirtu
2c1d3 61 6c 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  alTable(pParse, 
2c1d4 70 54 61 62 4c 69 73 74 2c 20 70 54 61 62 2c 20  pTabList, pTab, 
2c1d5 70 43 68 61 6e 67 65 73 2c 20 70 52 6f 77 69 64  pChanges, pRowid
2c1d6 45 78 70 72 2c 20 61 58 52 65 66 2c 0a 20 20 20  Expr, aXRef,.   
2c1d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c1d8 20 20 20 20 70 57 68 65 72 65 29 3b 0a 20 20 20      pWhere);.   
2c1d9 20 70 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 20   pWhere = 0;.   
2c1da 20 70 54 61 62 4c 69 73 74 20 3d 20 30 3b 0a 20   pTabList = 0;. 
2c1db 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63     goto update_c
2c1dc 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 23 65 6e 64  leanup;.  }.#end
2c1dd 69 66 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  if..  /* Allocat
2c1de 65 20 72 65 71 75 69 72 65 64 20 72 65 67 69 73  e required regis
2c1df 74 65 72 73 2e 20 2a 2f 0a 20 20 72 65 67 4f 6c  ters. */.  regOl
2c1e0 64 52 6f 77 69 64 20 3d 20 72 65 67 4e 65 77 52  dRowid = regNewR
2c1e1 6f 77 69 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d  owid = ++pParse-
2c1e2 3e 6e 4d 65 6d 3b 0a 20 20 69 66 28 20 70 54 72  >nMem;.  if( pTr
2c1e3 69 67 67 65 72 20 29 7b 0a 20 20 20 20 72 65 67  igger ){.    reg
2c1e4 4f 6c 64 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d  Old = pParse->nM
2c1e5 65 6d 20 2b 20 31 3b 0a 20 20 20 20 70 50 61 72  em + 1;.    pPar
2c1e6 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 54 61 62  se->nMem += pTab
2c1e7 2d 3e 6e 43 6f 6c 3b 0a 20 20 7d 0a 20 20 69 66  ->nCol;.  }.  if
2c1e8 28 20 63 68 6e 67 52 6f 77 69 64 20 7c 7c 20 70  ( chngRowid || p
2c1e9 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 72  Trigger ){.    r
2c1ea 65 67 4e 65 77 52 6f 77 69 64 20 3d 20 2b 2b 70  egNewRowid = ++p
2c1eb 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 7d  Parse->nMem;.  }
2c1ec 0a 20 20 72 65 67 4e 65 77 20 3d 20 70 50 61 72  .  regNew = pPar
2c1ed 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20  se->nMem + 1;.  
2c1ee 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20  pParse->nMem += 
2c1ef 70 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 72 65  pTab->nCol;.  re
2c1f0 67 52 65 63 20 3d 20 2b 2b 70 50 61 72 73 65 2d  gRec = ++pParse-
2c1f1 3e 6e 4d 65 6d 3b 0a 0a 20 20 2f 2a 20 53 74 61  >nMem;..  /* Sta
2c1f2 72 74 20 74 68 65 20 76 69 65 77 20 63 6f 6e 74  rt the view cont
2c1f3 65 78 74 2e 20 2a 2f 0a 20 20 69 66 28 20 69 73  ext. */.  if( is
2c1f4 56 69 65 77 20 29 7b 0a 20 20 20 20 73 71 6c 69  View ){.    sqli
2c1f5 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75  te3AuthContextPu
2c1f6 73 68 28 70 50 61 72 73 65 2c 20 26 73 43 6f 6e  sh(pParse, &sCon
2c1f7 74 65 78 74 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  text, pTab->zNam
2c1f8 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  e);.  }..  /* If
2c1f9 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 74   there are any t
2c1fa 72 69 67 67 65 72 73 2c 20 73 65 74 20 6f 6c 64  riggers, set old
2c1fb 6d 61 73 6b 20 61 6e 64 20 6e 65 77 5f 63 6f 6c  mask and new_col
2c1fc 5f 6d 61 73 6b 2e 20 2a 2f 0a 20 20 6f 6c 64 6d  _mask. */.  oldm
2c1fd 61 73 6b 20 3d 20 73 71 6c 69 74 65 33 54 72 69  ask = sqlite3Tri
2c1fe 67 67 65 72 4f 6c 64 6d 61 73 6b 28 0a 20 20 20  ggerOldmask(.   
2c1ff 20 20 20 70 50 61 72 73 65 2c 20 70 54 72 69 67     pParse, pTrig
2c200 67 65 72 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20  ger, TK_UPDATE, 
2c201 70 43 68 61 6e 67 65 73 2c 20 70 54 61 62 2c 20  pChanges, pTab, 
2c202 6f 6e 45 72 72 6f 72 29 3b 0a 0a 20 20 2f 2a 20  onError);..  /* 
2c203 49 66 20 77 65 20 61 72 65 20 74 72 79 69 6e 67  If we are trying
2c204 20 74 6f 20 75 70 64 61 74 65 20 61 20 76 69 65   to update a vie
2c205 77 2c 20 72 65 61 6c 69 7a 65 20 74 68 61 74 20  w, realize that 
2c206 76 69 65 77 20 69 6e 74 6f 0a 20 20 2a 2a 20 61  view into.  ** a
2c207 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65   ephemeral table
2c208 2e 0a 20 20 2a 2f 0a 23 69 66 20 21 64 65 66 69  ..  */.#if !defi
2c209 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
2c20a 56 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65  VIEW) && !define
2c20b 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  d(SQLITE_OMIT_TR
2c20c 49 47 47 45 52 29 0a 20 20 69 66 28 20 69 73 56  IGGER).  if( isV
2c20d 69 65 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  iew ){.    sqlit
2c20e 65 33 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65  e3MaterializeVie
2c20f 77 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  w(pParse, pTab, 
2c210 70 57 68 65 72 65 2c 20 69 43 75 72 29 3b 0a 20  pWhere, iCur);. 
2c211 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
2c212 52 65 73 6f 6c 76 65 20 74 68 65 20 63 6f 6c 75  Resolve the colu
2c213 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20  mn names in all 
2c214 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  the expressions 
2c215 69 6e 20 74 68 65 0a 20 20 2a 2a 20 57 48 45 52  in the.  ** WHER
2c216 45 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20  E clause..  */. 
2c217 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f   if( sqlite3Reso
2c218 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e  lveExprNames(&sN
2c219 43 2c 20 70 57 68 65 72 65 29 20 29 7b 0a 20 20  C, pWhere) ){.  
2c21a 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c    goto update_cl
2c21b 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  eanup;.  }..  /*
2c21c 20 42 65 67 69 6e 20 74 68 65 20 64 61 74 61 62   Begin the datab
2c21d 61 73 65 20 73 63 61 6e 0a 20 20 2a 2f 0a 20 20  ase scan.  */.  
2c21e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2c21f 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
2c220 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20   regOldRowid);. 
2c221 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65   pWInfo = sqlite
2c222 33 57 68 65 72 65 42 65 67 69 6e 28 70 50 61 72  3WhereBegin(pPar
2c223 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 57  se, pTabList, pW
2c224 68 65 72 65 2c 30 2c 20 57 48 45 52 45 5f 4f 4e  here,0, WHERE_ON
2c225 45 50 41 53 53 5f 44 45 53 49 52 45 44 29 3b 0a  EPASS_DESIRED);.
2c226 20 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20    if( pWInfo==0 
2c227 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c  ) goto update_cl
2c228 65 61 6e 75 70 3b 0a 20 20 6f 6b 4f 6e 65 50 61  eanup;.  okOnePa
2c229 73 73 20 3d 20 70 57 49 6e 66 6f 2d 3e 6f 6b 4f  ss = pWInfo->okO
2c22a 6e 65 50 61 73 73 3b 0a 0a 20 20 2f 2a 20 52 65  nePass;..  /* Re
2c22b 6d 65 6d 62 65 72 20 74 68 65 20 72 6f 77 69 64  member the rowid
2c22c 20 6f 66 20 65 76 65 72 79 20 69 74 65 6d 20 74   of every item t
2c22d 6f 20 62 65 20 75 70 64 61 74 65 64 2e 0a 20 20  o be updated..  
2c22e 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
2c22f 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77  AddOp2(v, OP_Row
2c230 69 64 2c 20 69 43 75 72 2c 20 72 65 67 4f 6c 64  id, iCur, regOld
2c231 52 6f 77 69 64 29 3b 0a 20 20 69 66 28 20 21 6f  Rowid);.  if( !o
2c232 6b 4f 6e 65 50 61 73 73 20 29 7b 0a 20 20 20 20  kOnePass ){.    
2c233 72 65 67 52 6f 77 53 65 74 20 3d 20 2b 2b 70 50  regRowSet = ++pP
2c234 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
2c235 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2c236 32 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74 41 64  2(v, OP_RowSetAd
2c237 64 2c 20 72 65 67 52 6f 77 53 65 74 2c 20 72 65  d, regRowSet, re
2c238 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20 7d 0a  gOldRowid);.  }.
2c239 0a 20 20 2f 2a 20 45 6e 64 20 74 68 65 20 64 61  .  /* End the da
2c23a 74 61 62 61 73 65 20 73 63 61 6e 20 6c 6f 6f 70  tabase scan loop
2c23b 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
2c23c 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29  WhereEnd(pWInfo)
2c23d 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  ;..  /* Initiali
2c23e 7a 65 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20  ze the count of 
2c23f 75 70 64 61 74 65 64 20 72 6f 77 73 0a 20 20 2a  updated rows.  *
2c240 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61  /.  if( (db->fla
2c241 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e  gs & SQLITE_Coun
2c242 74 52 6f 77 73 29 20 26 26 20 21 70 50 61 72 73  tRows) && !pPars
2c243 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 20 29  e->pTriggerTab )
2c244 7b 0a 20 20 20 20 72 65 67 52 6f 77 43 6f 75 6e  {.    regRowCoun
2c245 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  t = ++pParse->nM
2c246 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  em;.    sqlite3V
2c247 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2c248 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 52  Integer, 0, regR
2c249 6f 77 43 6f 75 6e 74 29 3b 0a 20 20 7d 0a 0a 20  owCount);.  }.. 
2c24a 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a   if( !isView ){.
2c24b 20 20 20 20 2f 2a 20 0a 20 20 20 20 2a 2a 20 4f      /* .    ** O
2c24c 70 65 6e 20 65 76 65 72 79 20 69 6e 64 65 78 20  pen every index 
2c24d 74 68 61 74 20 6e 65 65 64 73 20 75 70 64 61 74  that needs updat
2c24e 69 6e 67 2e 20 20 4e 6f 74 65 20 74 68 61 74 20  ing.  Note that 
2c24f 69 66 20 61 6e 79 0a 20 20 20 20 2a 2a 20 69 6e  if any.    ** in
2c250 64 65 78 20 63 6f 75 6c 64 20 70 6f 74 65 6e 74  dex could potent
2c251 69 61 6c 6c 79 20 69 6e 76 6f 6b 65 20 61 20 52  ially invoke a R
2c252 45 50 4c 41 43 45 20 63 6f 6e 66 6c 69 63 74 20  EPLACE conflict 
2c253 72 65 73 6f 6c 75 74 69 6f 6e 20 0a 20 20 20 20  resolution .    
2c254 2a 2a 20 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20  ** action, then 
2c255 77 65 20 6e 65 65 64 20 74 6f 20 6f 70 65 6e 20  we need to open 
2c256 61 6c 6c 20 69 6e 64 69 63 65 73 20 62 65 63 61  all indices beca
2c257 75 73 65 20 77 65 20 6d 69 67 68 74 20 6e 65 65  use we might nee
2c258 64 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 64  d.    ** to be d
2c259 65 6c 65 74 69 6e 67 20 73 6f 6d 65 20 72 65 63  eleting some rec
2c25a 6f 72 64 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ords..    */.   
2c25b 20 69 66 28 20 21 6f 6b 4f 6e 65 50 61 73 73 20   if( !okOnePass 
2c25c 29 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62  ) sqlite3OpenTab
2c25d 6c 65 28 70 50 61 72 73 65 2c 20 69 43 75 72 2c  le(pParse, iCur,
2c25e 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f   iDb, pTab, OP_O
2c25f 70 65 6e 57 72 69 74 65 29 3b 20 0a 20 20 20 20  penWrite); .    
2c260 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  if( onError==OE_
2c261 52 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 20  Replace ){.     
2c262 20 6f 70 65 6e 41 6c 6c 20 3d 20 31 3b 0a 20 20   openAll = 1;.  
2c263 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6f    }else{.      o
2c264 70 65 6e 41 6c 6c 20 3d 20 30 3b 0a 20 20 20 20  penAll = 0;.    
2c265 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d    for(pIdx=pTab-
2c266 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
2c267 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
2c268 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49  {.        if( pI
2c269 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  dx->onError==OE_
2c26a 52 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 20  Replace ){.     
2c26b 20 20 20 20 20 6f 70 65 6e 41 6c 6c 20 3d 20 31       openAll = 1
2c26c 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
2c26d 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
2c26e 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66     }.    }.    f
2c26f 6f 72 28 69 3d 30 2c 20 70 49 64 78 3d 70 54 61  or(i=0, pIdx=pTa
2c270 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
2c271 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
2c272 74 2c 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  t, i++){.      i
2c273 66 28 20 6f 70 65 6e 41 6c 6c 20 7c 7c 20 61 52  f( openAll || aR
2c274 65 67 49 64 78 5b 69 5d 3e 30 20 29 7b 0a 20 20  egIdx[i]>0 ){.  
2c275 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70        KeyInfo *p
2c276 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64  Key = sqlite3Ind
2c277 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65  exKeyinfo(pParse
2c278 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20 20 20  , pIdx);.       
2c279 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c27a 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69  p4(v, OP_OpenWri
2c27b 74 65 2c 20 69 43 75 72 2b 69 2b 31 2c 20 70 49  te, iCur+i+1, pI
2c27c 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20  dx->tnum, iDb,. 
2c27d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c27e 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65        (char*)pKe
2c27f 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41  y, P4_KEYINFO_HA
2c280 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 20 20 20  NDOFF);.        
2c281 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
2c282 6e 54 61 62 3e 69 43 75 72 2b 69 2b 31 20 29 3b  nTab>iCur+i+1 );
2c283 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2c284 20 7d 0a 0a 20 20 2f 2a 20 54 6f 70 20 6f 66 20   }..  /* Top of 
2c285 74 68 65 20 75 70 64 61 74 65 20 6c 6f 6f 70 20  the update loop 
2c286 2a 2f 0a 20 20 69 66 28 20 6f 6b 4f 6e 65 50 61  */.  if( okOnePa
2c287 73 73 20 29 7b 0a 20 20 20 20 69 6e 74 20 61 31  ss ){.    int a1
2c288 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2c289 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75  dOp1(v, OP_NotNu
2c28a 6c 6c 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29  ll, regOldRowid)
2c28b 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c  ;.    addr = sql
2c28c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76  ite3VdbeAddOp0(v
2c28d 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20 20 20 20  , OP_Goto);.    
2c28e 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
2c28f 65 72 65 28 76 2c 20 61 31 29 3b 0a 20 20 7d 65  ere(v, a1);.  }e
2c290 6c 73 65 7b 0a 20 20 20 20 61 64 64 72 20 3d 20  lse{.    addr = 
2c291 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2c292 33 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74 52 65  3(v, OP_RowSetRe
2c293 61 64 2c 20 72 65 67 52 6f 77 53 65 74 2c 20 30  ad, regRowSet, 0
2c294 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a  , regOldRowid);.
2c295 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 63    }..  /* Make c
2c296 75 72 73 6f 72 20 69 43 75 72 20 70 6f 69 6e 74  ursor iCur point
2c297 20 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20 74   to the record t
2c298 68 61 74 20 69 73 20 62 65 69 6e 67 20 75 70 64  hat is being upd
2c299 61 74 65 64 2e 20 49 66 0a 20 20 2a 2a 20 74 68  ated. If.  ** th
2c29a 69 73 20 72 65 63 6f 72 64 20 64 6f 65 73 20 6e  is record does n
2c29b 6f 74 20 65 78 69 73 74 20 66 6f 72 20 73 6f 6d  ot exist for som
2c29c 65 20 72 65 61 73 6f 6e 20 28 64 65 6c 65 74 65  e reason (delete
2c29d 64 20 62 79 20 61 20 74 72 69 67 67 65 72 2c 0a  d by a trigger,.
2c29e 20 20 2a 2a 20 66 6f 72 20 65 78 61 6d 70 6c 65    ** for example
2c29f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 74  , then jump to t
2c2a0 68 65 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f  he next iteratio
2c2a1 6e 20 6f 66 20 74 68 65 20 52 6f 77 53 65 74 20  n of the RowSet 
2c2a2 6c 6f 6f 70 2e 20 20 2a 2f 0a 20 20 73 71 6c 69  loop.  */.  sqli
2c2a3 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2c2a4 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69   OP_NotExists, i
2c2a5 43 75 72 2c 20 61 64 64 72 2c 20 72 65 67 4f 6c  Cur, addr, regOl
2c2a6 64 52 6f 77 69 64 29 3b 0a 0a 20 20 2f 2a 20 49  dRowid);..  /* I
2c2a7 66 20 74 68 65 72 65 20 61 72 65 20 74 72 69 67  f there are trig
2c2a8 67 65 72 73 20 6f 6e 20 74 68 69 73 20 74 61 62  gers on this tab
2c2a9 6c 65 2c 20 70 6f 70 75 6c 61 74 65 20 61 6e 20  le, populate an 
2c2aa 61 72 72 61 79 20 6f 66 20 72 65 67 69 73 74 65  array of registe
2c2ab 72 73 20 0a 20 20 2a 2a 20 77 69 74 68 20 74 68  rs .  ** with th
2c2ac 65 20 72 65 71 75 69 72 65 64 20 6f 6c 64 2e 2a  e required old.*
2c2ad 20 63 6f 6c 75 6d 6e 20 64 61 74 61 2e 20 20 2a   column data.  *
2c2ae 2f 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72  /.  if( pTrigger
2c2af 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
2c2b0 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   i<pTab->nCol; i
2c2b1 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61  ++){.      if( a
2c2b2 58 52 65 66 5b 69 5d 3c 30 20 7c 7c 20 6f 6c 64  XRef[i]<0 || old
2c2b3 6d 61 73 6b 3d 3d 30 78 66 66 66 66 66 66 66 66  mask==0xffffffff
2c2b4 20 7c 7c 20 28 6f 6c 64 6d 61 73 6b 20 26 20 28   || (oldmask & (
2c2b5 31 3c 3c 69 29 29 20 29 7b 0a 20 20 20 20 20 20  1<<i)) ){.      
2c2b6 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2c2b7 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
2c2b8 2c 20 69 43 75 72 2c 20 69 2c 20 72 65 67 4f 6c  , iCur, i, regOl
2c2b9 64 2b 69 29 3b 0a 20 20 20 20 20 20 20 20 73 71  d+i);.        sq
2c2ba 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75  lite3ColumnDefau
2c2bb 6c 74 28 76 2c 20 70 54 61 62 2c 20 69 2c 20 72  lt(v, pTab, i, r
2c2bc 65 67 4f 6c 64 2b 69 29 3b 0a 20 20 20 20 20 20  egOld+i);.      
2c2bd 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
2c2be 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2c2bf 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
2c2c0 72 65 67 4f 6c 64 2b 69 29 3b 0a 20 20 20 20 20  regOld+i);.     
2c2c1 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
2c2c2 2f 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72 64  /* If the record
2c2c3 20 6e 75 6d 62 65 72 20 77 69 6c 6c 20 63 68 61   number will cha
2c2c4 6e 67 65 2c 20 73 65 74 20 72 65 67 69 73 74 65  nge, set registe
2c2c5 72 20 72 65 67 4e 65 77 52 6f 77 69 64 20 74 6f  r regNewRowid to
2c2c6 0a 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 20 74 68  .  ** contain th
2c2c7 65 20 6e 65 77 20 76 61 6c 75 65 2e 20 49 66 20  e new value. If 
2c2c8 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  the record numbe
2c2c9 72 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 6d  r is not being m
2c2ca 6f 64 69 66 69 65 64 2c 0a 20 20 2a 2a 20 74 68  odified,.  ** th
2c2cb 65 6e 20 72 65 67 4e 65 77 52 6f 77 69 64 20 69  en regNewRowid i
2c2cc 73 20 74 68 65 20 73 61 6d 65 20 72 65 67 69 73  s the same regis
2c2cd 74 65 72 20 61 73 20 72 65 67 4f 6c 64 52 6f 77  ter as regOldRow
2c2ce 69 64 2c 20 77 68 69 63 68 20 69 73 0a 20 20 2a  id, which is.  *
2c2cf 2a 20 61 6c 72 65 61 64 79 20 70 6f 70 75 6c 61  * already popula
2c2d0 74 65 64 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72  ted.  */.  asser
2c2d1 74 28 20 63 68 6e 67 52 6f 77 69 64 20 7c 7c 20  t( chngRowid || 
2c2d2 70 54 72 69 67 67 65 72 20 7c 7c 20 72 65 67 4f  pTrigger || regO
2c2d3 6c 64 52 6f 77 69 64 3d 3d 72 65 67 4e 65 77 52  ldRowid==regNewR
2c2d4 6f 77 69 64 20 29 3b 0a 20 20 69 66 28 20 63 68  owid );.  if( ch
2c2d5 6e 67 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 73  ngRowid ){.    s
2c2d6 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
2c2d7 50 61 72 73 65 2c 20 70 52 6f 77 69 64 45 78 70  Parse, pRowidExp
2c2d8 72 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 29 3b  r, regNewRowid);
2c2d9 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2c2da 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73  AddOp1(v, OP_Mus
2c2db 74 42 65 49 6e 74 2c 20 72 65 67 4e 65 77 52 6f  tBeInt, regNewRo
2c2dc 77 69 64 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  wid);.  }else if
2c2dd 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20  ( pTrigger ){.  
2c2de 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2c2df 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20  Op2(v, OP_Copy, 
2c2e0 72 65 67 4f 6c 64 52 6f 77 69 64 2c 20 72 65 67  regOldRowid, reg
2c2e1 4e 65 77 52 6f 77 69 64 29 3b 0a 20 20 7d 0a 0a  NewRowid);.  }..
2c2e2 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68    /* Populate th
2c2e3 65 20 61 72 72 61 79 20 6f 66 20 72 65 67 69 73  e array of regis
2c2e4 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 61  ters beginning a
2c2e5 74 20 72 65 67 4e 65 77 20 77 69 74 68 20 74 68  t regNew with th
2c2e6 65 20 6e 65 77 0a 20 20 2a 2a 20 72 6f 77 20 64  e new.  ** row d
2c2e7 61 74 61 2e 20 54 68 69 73 20 61 72 72 61 79 20  ata. This array 
2c2e8 69 73 20 75 73 65 64 20 74 6f 20 63 68 65 63 6b  is used to check
2c2e9 20 63 6f 6e 73 74 61 69 6e 74 73 2c 20 63 72 65   constaints, cre
2c2ea 61 74 65 20 74 68 65 20 6e 65 77 0a 20 20 2a 2a  ate the new.  **
2c2eb 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78   table and index
2c2ec 20 72 65 63 6f 72 64 73 2c 20 61 6e 64 20 61 73   records, and as
2c2ed 20 74 68 65 20 76 61 6c 75 65 73 20 66 6f 72 20   the values for 
2c2ee 61 6e 79 20 6e 65 77 2e 2a 20 72 65 66 65 72 65  any new.* refere
2c2ef 6e 63 65 73 0a 20 20 2a 2a 20 6d 61 64 65 20 62  nces.  ** made b
2c2f0 79 20 74 72 69 67 67 65 72 73 2e 20 20 2a 2f 0a  y triggers.  */.
2c2f1 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61    for(i=0; i<pTa
2c2f2 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20  b->nCol; i++){. 
2c2f3 20 20 20 69 66 28 20 69 3d 3d 70 54 61 62 2d 3e     if( i==pTab->
2c2f4 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 73  iPKey ){.      s
2c2f5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2c2f6 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
2c2f7 72 65 67 4e 65 77 2b 69 29 3b 0a 20 20 20 20 7d  regNew+i);.    }
2c2f8 65 6c 73 65 7b 0a 20 20 20 20 20 20 6a 20 3d 20  else{.      j = 
2c2f9 61 58 52 65 66 5b 69 5d 3b 0a 20 20 20 20 20 20  aXRef[i];.      
2c2fa 69 66 28 20 6a 3c 30 20 29 7b 0a 20 20 20 20 20  if( j<0 ){.     
2c2fb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c2fc 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
2c2fd 6e 2c 20 69 43 75 72 2c 20 69 2c 20 72 65 67 4e  n, iCur, i, regN
2c2fe 65 77 2b 69 29 3b 0a 20 20 20 20 20 20 20 20 73  ew+i);.        s
2c2ff 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61  qlite3ColumnDefa
2c300 75 6c 74 28 76 2c 20 70 54 61 62 2c 20 69 2c 20  ult(v, pTab, i, 
2c301 72 65 67 4e 65 77 2b 69 29 3b 0a 20 20 20 20 20  regNew+i);.     
2c302 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2c303 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
2c304 70 50 61 72 73 65 2c 20 70 43 68 61 6e 67 65 73  pParse, pChanges
2c305 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 2c 20 72 65  ->a[j].pExpr, re
2c306 67 4e 65 77 2b 69 29 3b 0a 20 20 20 20 20 20 7d  gNew+i);.      }
2c307 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
2c308 20 46 69 72 65 20 61 6e 79 20 42 45 46 4f 52 45   Fire any BEFORE
2c309 20 55 50 44 41 54 45 20 74 72 69 67 67 65 72 73   UPDATE triggers
2c30a 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 62  . This happens b
2c30b 65 66 6f 72 65 20 63 6f 6e 73 74 72 61 69 6e 74  efore constraint
2c30c 73 20 61 72 65 0a 20 20 2a 2a 20 76 65 72 69 66  s are.  ** verif
2c30d 69 65 64 2e 20 4f 6e 65 20 63 6f 75 6c 64 20 61  ied. One could a
2c30e 72 67 75 65 20 74 68 61 74 20 74 68 69 73 20 69  rgue that this i
2c30f 73 20 77 72 6f 6e 67 2e 20 20 2a 2f 0a 20 20 69  s wrong.  */.  i
2c310 66 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20  f( pTrigger ){. 
2c311 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c312 64 4f 70 32 28 76 2c 20 4f 50 5f 41 66 66 69 6e  dOp2(v, OP_Affin
2c313 69 74 79 2c 20 72 65 67 4e 65 77 2c 20 70 54 61  ity, regNew, pTa
2c314 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 73 71  b->nCol);.    sq
2c315 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69  lite3TableAffini
2c316 74 79 53 74 72 28 76 2c 20 70 54 61 62 29 3b 0a  tyStr(v, pTab);.
2c317 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 52      sqlite3CodeR
2c318 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73 65  owTrigger(pParse
2c319 2c 20 70 54 72 69 67 67 65 72 2c 20 54 4b 5f 55  , pTrigger, TK_U
2c31a 50 44 41 54 45 2c 20 70 43 68 61 6e 67 65 73 2c  PDATE, pChanges,
2c31b 20 0a 20 20 20 20 20 20 20 20 54 52 49 47 47 45   .        TRIGGE
2c31c 52 5f 42 45 46 4f 52 45 2c 20 70 54 61 62 2c 20  R_BEFORE, pTab, 
2c31d 2d 31 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 2c  -1, regOldRowid,
2c31e 20 6f 6e 45 72 72 6f 72 2c 20 61 64 64 72 29 3b   onError, addr);
2c31f 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77  ..    /* The row
2c320 2d 74 72 69 67 67 65 72 20 6d 61 79 20 68 61 76  -trigger may hav
2c321 65 20 64 65 6c 65 74 65 64 20 74 68 65 20 72 6f  e deleted the ro
2c322 77 20 62 65 69 6e 67 20 75 70 64 61 74 65 64 2e  w being updated.
2c323 20 49 6e 20 74 68 69 73 0a 20 20 20 20 2a 2a 20   In this.    ** 
2c324 63 61 73 65 2c 20 6a 75 6d 70 20 74 6f 20 74 68  case, jump to th
2c325 65 20 6e 65 78 74 20 72 6f 77 2e 20 4e 6f 20 75  e next row. No u
2c326 70 64 61 74 65 73 20 6f 72 20 41 46 54 45 52 20  pdates or AFTER 
2c327 74 72 69 67 67 65 72 73 20 61 72 65 20 0a 20 20  triggers are .  
2c328 20 20 2a 2a 20 72 65 71 75 69 72 65 64 2e 20 54    ** required. T
2c329 68 69 73 20 62 65 68 61 76 69 6f 75 72 20 2d 20  his behaviour - 
2c32a 77 68 61 74 20 68 61 70 70 65 6e 73 20 77 68 65  what happens whe
2c32b 6e 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67 20  n the row being 
2c32c 75 70 64 61 74 65 64 0a 20 20 20 20 2a 2a 20 69  updated.    ** i
2c32d 73 20 64 65 6c 65 74 65 64 20 6f 72 20 72 65 6e  s deleted or ren
2c32e 61 6d 65 64 20 62 79 20 61 20 42 45 46 4f 52 45  amed by a BEFORE
2c32f 20 74 72 69 67 67 65 72 20 2d 20 69 73 20 6c 65   trigger - is le
2c330 66 74 20 75 6e 64 65 66 69 6e 65 64 20 69 6e 20  ft undefined in 
2c331 74 68 65 0a 20 20 20 20 2a 2a 20 64 6f 63 75 6d  the.    ** docum
2c332 65 6e 74 61 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20  entation.  */.  
2c333 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2c334 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69  Op3(v, OP_NotExi
2c335 73 74 73 2c 20 69 43 75 72 2c 20 61 64 64 72 2c  sts, iCur, addr,
2c336 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20   regOldRowid);. 
2c337 20 7d 0a 0a 20 20 69 66 28 20 21 69 73 56 69 65   }..  if( !isVie
2c338 77 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 44 6f 20  w ){..    /* Do 
2c339 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63 6b  constraint check
2c33a 73 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  s. */.    sqlite
2c33b 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61  3GenerateConstra
2c33c 69 6e 74 43 68 65 63 6b 73 28 70 50 61 72 73 65  intChecks(pParse
2c33d 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20 72 65  , pTab, iCur, re
2c33e 67 4e 65 77 52 6f 77 69 64 2c 0a 20 20 20 20 20  gNewRowid,.     
2c33f 20 20 20 61 52 65 67 49 64 78 2c 20 28 63 68 6e     aRegIdx, (chn
2c340 67 52 6f 77 69 64 3f 72 65 67 4f 6c 64 52 6f 77  gRowid?regOldRow
2c341 69 64 3a 30 29 2c 20 31 2c 20 6f 6e 45 72 72 6f  id:0), 1, onErro
2c342 72 2c 20 61 64 64 72 2c 20 30 29 3b 0a 0a 20 20  r, addr, 0);..  
2c343 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20    /* Delete the 
2c344 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20 61 73  index entries as
2c345 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
2c346 65 20 63 75 72 72 65 6e 74 20 72 65 63 6f 72 64  e current record
2c347 2e 20 20 2a 2f 0a 20 20 20 20 6a 31 20 3d 20 73  .  */.    j1 = s
2c348 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2c349 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73  (v, OP_NotExists
2c34a 2c 20 69 43 75 72 2c 20 30 2c 20 72 65 67 4f 6c  , iCur, 0, regOl
2c34b 64 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c  dRowid);.    sql
2c34c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 49  ite3GenerateRowI
2c34d 6e 64 65 78 44 65 6c 65 74 65 28 70 50 61 72 73  ndexDelete(pPars
2c34e 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20 61  e, pTab, iCur, a
2c34f 52 65 67 49 64 78 29 3b 0a 20 20 0a 20 20 20 20  RegIdx);.  .    
2c350 2f 2a 20 49 66 20 63 68 61 6e 67 69 6e 67 20 74  /* If changing t
2c351 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  he record number
2c352 2c 20 64 65 6c 65 74 65 20 74 68 65 20 6f 6c 64  , delete the old
2c353 20 72 65 63 6f 72 64 2e 20 20 2a 2f 0a 20 20 20   record.  */.   
2c354 20 69 66 28 20 63 68 6e 67 52 6f 77 69 64 20 29   if( chngRowid )
2c355 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
2c356 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2c357 44 65 6c 65 74 65 2c 20 69 43 75 72 2c 20 30 29  Delete, iCur, 0)
2c358 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
2c359 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
2c35a 76 2c 20 6a 31 29 3b 0a 20 20 0a 20 20 20 20 2f  v, j1);.  .    /
2c35b 2a 20 49 6e 73 65 72 74 20 74 68 65 20 6e 65 77  * Insert the new
2c35c 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20 61   index entries a
2c35d 6e 64 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72  nd the new recor
2c35e 64 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  d. */.    sqlite
2c35f 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65 72 74 69  3CompleteInserti
2c360 6f 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  on(pParse, pTab,
2c361 20 69 43 75 72 2c 20 72 65 67 4e 65 77 52 6f 77   iCur, regNewRow
2c362 69 64 2c 20 61 52 65 67 49 64 78 2c 20 31 2c 20  id, aRegIdx, 1, 
2c363 30 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  0, 0);.  }..  /*
2c364 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72   Increment the r
2c365 6f 77 20 63 6f 75 6e 74 65 72 20 0a 20 20 2a 2f  ow counter .  */
2c366 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67  .  if( (db->flag
2c367 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74  s & SQLITE_Count
2c368 52 6f 77 73 29 20 26 26 20 21 70 50 61 72 73 65  Rows) && !pParse
2c369 2d 3e 70 54 72 69 67 67 65 72 54 61 62 29 7b 0a  ->pTriggerTab){.
2c36a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2c36b 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
2c36c 6d 6d 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74 2c  mm, regRowCount,
2c36d 20 31 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69   1);.  }..  sqli
2c36e 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65  te3CodeRowTrigge
2c36f 72 28 70 50 61 72 73 65 2c 20 70 54 72 69 67 67  r(pParse, pTrigg
2c370 65 72 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 70  er, TK_UPDATE, p
2c371 43 68 61 6e 67 65 73 2c 20 0a 20 20 20 20 20 20  Changes, .      
2c372 54 52 49 47 47 45 52 5f 41 46 54 45 52 2c 20 70  TRIGGER_AFTER, p
2c373 54 61 62 2c 20 2d 31 2c 20 72 65 67 4f 6c 64 52  Tab, -1, regOldR
2c374 6f 77 69 64 2c 20 6f 6e 45 72 72 6f 72 2c 20 61  owid, onError, a
2c375 64 64 72 29 3b 0a 0a 20 20 2f 2a 20 52 65 70 65  ddr);..  /* Repe
2c376 61 74 20 74 68 65 20 61 62 6f 76 65 20 77 69 74  at the above wit
2c377 68 20 74 68 65 20 6e 65 78 74 20 72 65 63 6f 72  h the next recor
2c378 64 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 2c  d to be updated,
2c379 20 75 6e 74 69 6c 0a 20 20 2a 2a 20 61 6c 6c 20   until.  ** all 
2c37a 72 65 63 6f 72 64 20 73 65 6c 65 63 74 65 64 20  record selected 
2c37b 62 79 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  by the WHERE cla
2c37c 75 73 65 20 68 61 76 65 20 62 65 65 6e 20 75 70  use have been up
2c37d 64 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71  dated..  */.  sq
2c37e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2c37f 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61  v, OP_Goto, 0, a
2c380 64 64 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  ddr);.  sqlite3V
2c381 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
2c382 64 64 72 29 3b 0a 0a 20 20 2f 2a 20 43 6c 6f 73  ddr);..  /* Clos
2c383 65 20 61 6c 6c 20 74 61 62 6c 65 73 20 2a 2f 0a  e all tables */.
2c384 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 64 78 3d    for(i=0, pIdx=
2c385 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
2c386 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
2c387 4e 65 78 74 2c 20 69 2b 2b 29 7b 0a 20 20 20 20  Next, i++){.    
2c388 69 66 28 20 6f 70 65 6e 41 6c 6c 20 7c 7c 20 61  if( openAll || a
2c389 52 65 67 49 64 78 5b 69 5d 3e 30 20 29 7b 0a 20  RegIdx[i]>0 ){. 
2c38a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2c38b 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp2(v, OP_Clo
2c38c 73 65 2c 20 69 43 75 72 2b 69 2b 31 2c 20 30 29  se, iCur+i+1, 0)
2c38d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71  ;.    }.  }.  sq
2c38e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2c38f 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75  v, OP_Close, iCu
2c390 72 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 55 70 64  r, 0);..  /* Upd
2c391 61 74 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73  ate the sqlite_s
2c392 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 62 79  equence table by
2c393 20 73 74 6f 72 69 6e 67 20 74 68 65 20 63 6f 6e   storing the con
2c394 74 65 6e 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a  tent of the.  **
2c395 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20 63   maximum rowid c
2c396 6f 75 6e 74 65 72 20 76 61 6c 75 65 73 20 72 65  ounter values re
2c397 63 6f 72 64 65 64 20 77 68 69 6c 65 20 69 6e 73  corded while ins
2c398 65 72 74 69 6e 67 20 69 6e 74 6f 0a 20 20 2a 2a  erting into.  **
2c399 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 74   autoincrement t
2c39a 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  ables..  */.  if
2c39b 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64  ( pParse->nested
2c39c 3d 3d 30 20 26 26 20 70 50 61 72 73 65 2d 3e 70  ==0 && pParse->p
2c39d 54 72 69 67 67 65 72 54 61 62 3d 3d 30 20 29 7b  TriggerTab==0 ){
2c39e 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75 74 6f  .    sqlite3Auto
2c39f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 70 50 61  incrementEnd(pPa
2c3a0 72 73 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a  rse);.  }..  /*.
2c3a1 20 20 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20    ** Return the 
2c3a2 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74  number of rows t
2c3a3 68 61 74 20 77 65 72 65 20 63 68 61 6e 67 65 64  hat were changed
2c3a4 2e 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e  . If this routin
2c3a5 65 20 69 73 20 0a 20 20 2a 2a 20 67 65 6e 65 72  e is .  ** gener
2c3a6 61 74 69 6e 67 20 63 6f 64 65 20 62 65 63 61 75  ating code becau
2c3a7 73 65 20 6f 66 20 61 20 63 61 6c 6c 20 74 6f 20  se of a call to 
2c3a8 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
2c3a9 73 65 28 29 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a  se(), do not.  *
2c3aa 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c  * invoke the cal
2c3ab 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a  lback function..
2c3ac 20 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e    */.  if( (db->
2c3ad 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 43 6f 75  flags&SQLITE_Cou
2c3ae 6e 74 52 6f 77 73 29 20 26 26 20 21 70 50 61 72  ntRows) && !pPar
2c3af 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 20  se->pTriggerTab 
2c3b0 26 26 20 21 70 50 61 72 73 65 2d 3e 6e 65 73 74  && !pParse->nest
2c3b1 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ed ){.    sqlite
2c3b2 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2c3b3 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 72 65 67  P_ResultRow, reg
2c3b4 52 6f 77 43 6f 75 6e 74 2c 20 31 29 3b 0a 20 20  RowCount, 1);.  
2c3b5 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
2c3b6 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20  NumCols(v, 1);. 
2c3b7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
2c3b8 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
2c3b9 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 72 6f  OLNAME_NAME, "ro
2c3ba 77 73 20 75 70 64 61 74 65 64 22 2c 20 53 51 4c  ws updated", SQL
2c3bb 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d  ITE_STATIC);.  }
2c3bc 0a 0a 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70  ..update_cleanup
2c3bd 3a 0a 20 20 73 71 6c 69 74 65 33 41 75 74 68 43  :.  sqlite3AuthC
2c3be 6f 6e 74 65 78 74 50 6f 70 28 26 73 43 6f 6e 74  ontextPop(&sCont
2c3bf 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ext);.  sqlite3D
2c3c0 62 46 72 65 65 28 64 62 2c 20 61 52 65 67 49 64  bFree(db, aRegId
2c3c1 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  x);.  sqlite3DbF
2c3c2 72 65 65 28 64 62 2c 20 61 58 52 65 66 29 3b 0a  ree(db, aXRef);.
2c3c3 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
2c3c4 44 65 6c 65 74 65 28 64 62 2c 20 70 54 61 62 4c  Delete(db, pTabL
2c3c5 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ist);.  sqlite3E
2c3c6 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
2c3c7 2c 20 70 43 68 61 6e 67 65 73 29 3b 0a 20 20 73  , pChanges);.  s
2c3c8 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
2c3c9 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20  (db, pWhere);.  
2c3ca 72 65 74 75 72 6e 3b 0a 7d 0a 0a 23 69 66 6e 64  return;.}..#ifnd
2c3cb 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
2c3cc 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 0a 2a  IRTUALTABLE./*.*
2c3cd 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
2c3ce 66 6f 72 20 61 6e 20 55 50 44 41 54 45 20 6f 66  for an UPDATE of
2c3cf 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
2c3d0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 72 61  ..**.** The stra
2c3d1 74 65 67 79 20 69 73 20 74 68 61 74 20 77 65 20  tegy is that we 
2c3d2 63 72 65 61 74 65 20 61 6e 20 65 70 68 65 6d 65  create an epheme
2c3d3 72 69 61 6c 20 74 61 62 6c 65 20 74 68 61 74 20  rial table that 
2c3d4 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 66 6f 72 20  contains.** for 
2c3d5 65 61 63 68 20 72 6f 77 20 74 6f 20 62 65 20 63  each row to be c
2c3d6 68 61 6e 67 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20  hanged:.**.**   
2c3d7 28 41 29 20 20 54 68 65 20 6f 72 69 67 69 6e 61  (A)  The origina
2c3d8 6c 20 72 6f 77 69 64 20 6f 66 20 74 68 61 74 20  l rowid of that 
2c3d9 72 6f 77 2e 0a 2a 2a 20 20 20 28 42 29 20 20 54  row..**   (B)  T
2c3da 68 65 20 72 65 76 69 73 65 64 20 72 6f 77 69 64  he revised rowid
2c3db 20 66 6f 72 20 74 68 65 20 72 6f 77 2e 20 28 6e   for the row. (n
2c3dc 6f 74 65 31 29 0a 2a 2a 20 20 20 28 43 29 20 20  ote1).**   (C)  
2c3dd 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 65  The content of e
2c3de 76 65 72 79 20 63 6f 6c 75 6d 6e 20 69 6e 20 74  very column in t
2c3df 68 65 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 54 68  he row..**.** Th
2c3e0 65 6e 20 77 65 20 6c 6f 6f 70 20 6f 76 65 72 20  en we loop over 
2c3e1 74 68 69 73 20 65 70 68 65 6d 65 72 61 6c 20 74  this ephemeral t
2c3e2 61 62 6c 65 20 61 6e 64 20 66 6f 72 20 65 61 63  able and for eac
2c3e3 68 20 72 6f 77 20 69 6e 0a 2a 2a 20 74 68 65 20  h row in.** the 
2c3e4 65 70 68 65 72 6d 65 72 61 6c 20 74 61 62 6c 65  ephermeral table
2c3e5 20 63 61 6c 6c 20 56 55 70 64 61 74 65 2e 0a 2a   call VUpdate..*
2c3e6 2a 0a 2a 2a 20 57 68 65 6e 20 66 69 6e 69 73 68  *.** When finish
2c3e7 65 64 2c 20 64 72 6f 70 20 74 68 65 20 65 70 68  ed, drop the eph
2c3e8 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a  emeral table..**
2c3e9 0a 2a 2a 20 28 6e 6f 74 65 31 29 20 41 63 74 75  .** (note1) Actu
2c3ea 61 6c 6c 79 2c 20 69 66 20 77 65 20 6b 6e 6f 77  ally, if we know
2c3eb 20 69 6e 20 61 64 76 61 6e 63 65 20 74 68 61 74   in advance that
2c3ec 20 28 41 29 20 69 73 20 61 6c 77 61 79 73 20 74   (A) is always t
2c3ed 68 65 20 73 61 6d 65 0a 2a 2a 20 61 73 20 28 42  he same.** as (B
2c3ee 29 20 77 65 20 6f 6e 6c 79 20 73 74 6f 72 65 20  ) we only store 
2c3ef 28 41 29 2c 20 74 68 65 6e 20 64 75 70 6c 69 63  (A), then duplic
2c3f0 61 74 65 20 28 41 29 20 77 68 65 6e 20 70 75 6c  ate (A) when pul
2c3f1 6c 69 6e 67 0a 2a 2a 20 69 74 20 6f 75 74 20 6f  ling.** it out o
2c3f2 66 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20  f the ephemeral 
2c3f3 74 61 62 6c 65 20 62 65 66 6f 72 65 20 63 61 6c  table before cal
2c3f4 6c 69 6e 67 20 56 55 70 64 61 74 65 2e 0a 2a 2f  ling VUpdate..*/
2c3f5 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 70 64  .static void upd
2c3f6 61 74 65 56 69 72 74 75 61 6c 54 61 62 6c 65 28  ateVirtualTable(
2c3f7 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2c3f8 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70  ,       /* The p
2c3f9 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2c3fa 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  /.  SrcList *pSr
2c3fb 63 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  c,       /* The 
2c3fc 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 74 6f  virtual table to
2c3fd 20 62 65 20 6d 6f 64 69 66 69 65 64 20 2a 2f 0a   be modified */.
2c3fe 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20    Table *pTab,  
2c3ff 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69         /* The vi
2c400 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20  rtual table */. 
2c401 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e   ExprList *pChan
2c402 67 65 73 2c 20 20 2f 2a 20 54 68 65 20 63 6f 6c  ges,  /* The col
2c403 75 6d 6e 73 20 74 6f 20 63 68 61 6e 67 65 20 69  umns to change i
2c404 6e 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61  n the UPDATE sta
2c405 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72  tement */.  Expr
2c406 20 2a 70 52 6f 77 69 64 2c 20 20 20 20 20 20 20   *pRowid,       
2c407 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 75   /* Expression u
2c408 73 65 64 20 74 6f 20 72 65 63 6f 6d 70 75 74 65  sed to recompute
2c409 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20   the rowid */.  
2c40a 69 6e 74 20 2a 61 58 52 65 66 2c 20 20 20 20 20  int *aXRef,     
2c40b 20 20 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20       /* Mapping 
2c40c 66 72 6f 6d 20 63 6f 6c 75 6d 6e 73 20 6f 66 20  from columns of 
2c40d 70 54 61 62 20 74 6f 20 65 6e 74 72 69 65 73 20  pTab to entries 
2c40e 69 6e 20 70 43 68 61 6e 67 65 73 20 2a 2f 0a 20  in pChanges */. 
2c40f 20 45 78 70 72 20 2a 70 57 68 65 72 65 20 20 20   Expr *pWhere   
2c410 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63        /* WHERE c
2c411 6c 61 75 73 65 20 6f 66 20 74 68 65 20 55 50 44  lause of the UPD
2c412 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  ATE statement */
2c413 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  .){.  Vdbe *v = 
2c414 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20  pParse->pVdbe;  
2c415 2f 2a 20 56 69 72 74 75 61 6c 20 6d 61 63 68 69  /* Virtual machi
2c416 6e 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  ne under constru
2c417 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 4c  ction */.  ExprL
2c418 69 73 74 20 2a 70 45 4c 69 73 74 20 3d 20 30 3b  ist *pEList = 0;
2c419 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75       /* The resu
2c41a 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20 53 45  lt set of the SE
2c41b 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a  LECT statement *
2c41c 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
2c41d 65 63 74 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a  ect = 0;      /*
2c41e 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   The SELECT stat
2c41f 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20  ement */.  Expr 
2c420 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20  *pExpr;         
2c421 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72       /* Temporar
2c422 79 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  y expression */.
2c423 20 20 69 6e 74 20 65 70 68 65 6d 54 61 62 3b 20    int ephemTab; 
2c424 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2c425 61 62 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68 65  able holding the
2c426 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 53   result of the S
2c427 45 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20 69  ELECT */.  int i
2c428 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2c429 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
2c42a 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 64  nter */.  int ad
2c42b 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dr;             
2c42c 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
2c42d 66 20 74 6f 70 20 6f 66 20 6c 6f 6f 70 20 2a 2f  f top of loop */
2c42e 0a 20 20 69 6e 74 20 69 52 65 67 3b 20 20 20 20  .  int iReg;    
2c42f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c430 46 69 72 73 74 20 72 65 67 69 73 74 65 72 20 69  First register i
2c431 6e 20 73 65 74 20 70 61 73 73 65 64 20 74 6f 20  n set passed to 
2c432 4f 50 5f 56 55 70 64 61 74 65 20 2a 2f 0a 20 20  OP_VUpdate */.  
2c433 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2c434 61 72 73 65 2d 3e 64 62 3b 20 2f 2a 20 44 61 74  arse->db; /* Dat
2c435 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2c436 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
2c437 20 2a 70 56 54 61 62 20 3d 20 28 63 6f 6e 73 74   *pVTab = (const
2c438 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 47 65   char*)sqlite3Ge
2c439 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62  tVTable(db, pTab
2c43a 29 3b 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20  );.  SelectDest 
2c43b 64 65 73 74 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 73  dest;..  /* Cons
2c43c 74 72 75 63 74 20 74 68 65 20 53 45 4c 45 43 54  truct the SELECT
2c43d 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
2c43e 77 69 6c 6c 20 66 69 6e 64 20 74 68 65 20 6e 65  will find the ne
2c43f 77 20 76 61 6c 75 65 73 20 66 6f 72 0a 20 20 2a  w values for.  *
2c440 2a 20 61 6c 6c 20 75 70 64 61 74 65 64 20 72 6f  * all updated ro
2c441 77 73 2e 20 0a 20 20 2a 2f 0a 20 20 70 45 4c 69  ws. .  */.  pELi
2c442 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
2c443 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
2c444 65 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20  e, 0, .         
2c445 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c446 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43          sqlite3C
2c447 72 65 61 74 65 49 64 45 78 70 72 28 70 50 61 72  reateIdExpr(pPar
2c448 73 65 2c 20 22 5f 72 6f 77 69 64 5f 22 29 29 3b  se, "_rowid_"));
2c449 0a 20 20 69 66 28 20 70 52 6f 77 69 64 20 29 7b  .  if( pRowid ){
2c44a 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20 73 71  .    pEList = sq
2c44b 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
2c44c 65 6e 64 28 70 50 61 72 73 65 2c 20 70 45 4c 69  end(pParse, pELi
2c44d 73 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  st,.            
2c44e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c44f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
2c450 70 72 44 75 70 28 64 62 2c 20 70 52 6f 77 69 64  prDup(db, pRowid
2c451 2c 20 30 29 29 3b 0a 20 20 7d 0a 20 20 61 73 73  , 0));.  }.  ass
2c452 65 72 74 28 20 70 54 61 62 2d 3e 69 50 4b 65 79  ert( pTab->iPKey
2c453 3c 30 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  <0 );.  for(i=0;
2c454 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   i<pTab->nCol; i
2c455 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 58 52  ++){.    if( aXR
2c456 65 66 5b 69 5d 3e 3d 30 20 29 7b 0a 20 20 20 20  ef[i]>=0 ){.    
2c457 20 20 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65    pExpr = sqlite
2c458 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 43 68  3ExprDup(db, pCh
2c459 61 6e 67 65 73 2d 3e 61 5b 61 58 52 65 66 5b 69  anges->a[aXRef[i
2c45a 5d 5d 2e 70 45 78 70 72 2c 20 30 29 3b 0a 20 20  ]].pExpr, 0);.  
2c45b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
2c45c 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 43 72  Expr = sqlite3Cr
2c45d 65 61 74 65 49 64 45 78 70 72 28 70 50 61 72 73  eateIdExpr(pPars
2c45e 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d  e, pTab->aCol[i]
2c45f 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20  .zName);.    }. 
2c460 20 20 20 70 45 4c 69 73 74 20 3d 20 73 71 6c 69     pEList = sqli
2c461 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
2c462 64 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74  d(pParse, pEList
2c463 2c 20 70 45 78 70 72 29 3b 0a 20 20 7d 0a 20 20  , pExpr);.  }.  
2c464 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65  pSelect = sqlite
2c465 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73  3SelectNew(pPars
2c466 65 2c 20 70 45 4c 69 73 74 2c 20 70 53 72 63 2c  e, pEList, pSrc,
2c467 20 70 57 68 65 72 65 2c 20 30 2c 20 30 2c 20 30   pWhere, 0, 0, 0
2c468 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 0a 20  , 0, 0, 0);.  . 
2c469 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20 65   /* Create the e
2c46a 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 69  phemeral table i
2c46b 6e 74 6f 20 77 68 69 63 68 20 74 68 65 20 75 70  nto which the up
2c46c 64 61 74 65 20 72 65 73 75 6c 74 73 20 77 69 6c  date results wil
2c46d 6c 0a 20 20 2a 2a 20 62 65 20 73 74 6f 72 65 64  l.  ** be stored
2c46e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
2c46f 20 76 20 29 3b 0a 20 20 65 70 68 65 6d 54 61 62   v );.  ephemTab
2c470 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
2c471 2b 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  +;.  sqlite3Vdbe
2c472 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65  AddOp2(v, OP_Ope
2c473 6e 45 70 68 65 6d 65 72 61 6c 2c 20 65 70 68 65  nEphemeral, ephe
2c474 6d 54 61 62 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c  mTab, pTab->nCol
2c475 2b 31 2b 28 70 52 6f 77 69 64 21 3d 30 29 29 3b  +1+(pRowid!=0));
2c476 0a 0a 20 20 2f 2a 20 66 69 6c 6c 20 74 68 65 20  ..  /* fill the 
2c477 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20  ephemeral table 
2c478 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 53  .  */.  sqlite3S
2c479 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64  electDestInit(&d
2c47a 65 73 74 2c 20 53 52 54 5f 54 61 62 6c 65 2c 20  est, SRT_Table, 
2c47b 65 70 68 65 6d 54 61 62 29 3b 0a 20 20 73 71 6c  ephemTab);.  sql
2c47c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73  ite3Select(pPars
2c47d 65 2c 20 70 53 65 6c 65 63 74 2c 20 26 64 65 73  e, pSelect, &des
2c47e 74 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61  t);..  /* Genera
2c47f 74 65 20 63 6f 64 65 20 74 6f 20 73 63 61 6e 20  te code to scan 
2c480 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61  the ephemeral ta
2c481 62 6c 65 20 61 6e 64 20 63 61 6c 6c 20 56 55 70  ble and call VUp
2c482 64 61 74 65 2e 20 2a 2f 0a 20 20 69 52 65 67 20  date. */.  iReg 
2c483 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
2c484 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  ;.  pParse->nMem
2c485 20 2b 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31   += pTab->nCol+1
2c486 3b 0a 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74  ;.  addr = sqlit
2c487 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2c488 4f 50 5f 52 65 77 69 6e 64 2c 20 65 70 68 65 6d  OP_Rewind, ephem
2c489 54 61 62 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  Tab, 0);.  sqlit
2c48a 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2c48b 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 20 65 70 68 65  OP_Column,  ephe
2c48c 6d 54 61 62 2c 20 30 2c 20 69 52 65 67 29 3b 0a  mTab, 0, iReg);.
2c48d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2c48e 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
2c48f 2c 20 65 70 68 65 6d 54 61 62 2c 20 28 70 52 6f  , ephemTab, (pRo
2c490 77 69 64 3f 31 3a 30 29 2c 20 69 52 65 67 2b 31  wid?1:0), iReg+1
2c491 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
2c492 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29  pTab->nCol; i++)
2c493 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
2c494 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
2c495 6c 75 6d 6e 2c 20 65 70 68 65 6d 54 61 62 2c 20  lumn, ephemTab, 
2c496 69 2b 31 2b 28 70 52 6f 77 69 64 21 3d 30 29 2c  i+1+(pRowid!=0),
2c497 20 69 52 65 67 2b 32 2b 69 29 3b 0a 20 20 7d 0a   iReg+2+i);.  }.
2c498 20 20 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b    sqlite3VtabMak
2c499 65 57 72 69 74 61 62 6c 65 28 70 50 61 72 73 65  eWritable(pParse
2c49a 2c 20 70 54 61 62 29 3b 0a 20 20 73 71 6c 69 74  , pTab);.  sqlit
2c49b 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2c49c 4f 50 5f 56 55 70 64 61 74 65 2c 20 30 2c 20 70  OP_VUpdate, 0, p
2c49d 54 61 62 2d 3e 6e 43 6f 6c 2b 32 2c 20 69 52 65  Tab->nCol+2, iRe
2c49e 67 2c 20 70 56 54 61 62 2c 20 50 34 5f 56 54 41  g, pVTab, P4_VTA
2c49f 42 29 3b 0a 20 20 73 71 6c 69 74 65 33 4d 61 79  B);.  sqlite3May
2c4a0 41 62 6f 72 74 28 70 50 61 72 73 65 29 3b 0a 20  Abort(pParse);. 
2c4a1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c4a2 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 65  p2(v, OP_Next, e
2c4a3 70 68 65 6d 54 61 62 2c 20 61 64 64 72 2b 31 29  phemTab, addr+1)
2c4a4 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a  ;.  sqlite3VdbeJ
2c4a5 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29  umpHere(v, addr)
2c4a6 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
2c4a7 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp2(v, OP_Clos
2c4a8 65 2c 20 65 70 68 65 6d 54 61 62 2c 20 30 29 3b  e, ephemTab, 0);
2c4a9 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 75 70 20 2a  ..  /* Cleanup *
2c4aa 2f 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  /.  sqlite3Selec
2c4ab 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c  tDelete(db, pSel
2c4ac 65 63 74 29 3b 20 20 0a 7d 0a 23 65 6e 64 69 66  ect);  .}.#endif
2c4ad 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2c4ae 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a  VIRTUALTABLE */.
2c4af 0a 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 22 69  ./* Make sure "i
2c4b0 73 56 69 65 77 22 20 67 65 74 73 20 75 6e 64 65  sView" gets unde
2c4b1 66 69 6e 65 64 20 69 6e 20 63 61 73 65 20 74 68  fined in case th
2c4b2 69 73 20 66 69 6c 65 20 62 65 63 6f 6d 65 73 20  is file becomes 
2c4b3 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61  part of.** the a
2c4b4 6d 61 6c 67 61 6d 61 74 69 6f 6e 20 2d 20 73 6f  malgamation - so
2c4b5 20 74 68 61 74 20 73 75 62 73 65 71 75 65 6e 74   that subsequent
2c4b6 20 66 69 6c 65 73 20 64 6f 20 6e 6f 74 20 73 65   files do not se
2c4b7 65 20 69 73 56 69 65 77 20 61 73 20 61 0a 2a 2a  e isView as a.**
2c4b8 20 6d 61 63 72 6f 2e 20 2a 2f 0a 23 75 6e 64 65   macro. */.#unde
2c4b9 66 20 69 73 56 69 65 77 0a 0a 2f 2a 2a 2a 2a 2a  f isView../*****
2c4ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
2c4bb 20 75 70 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a   update.c ******
2c4bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c4bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c4be 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
2c4bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
2c4c0 66 69 6c 65 20 76 61 63 75 75 6d 2e 63 20 2a 2a  file vacuum.c **
2c4c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c4c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c4c3 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
2c4c4 32 30 30 33 20 41 70 72 69 6c 20 36 0a 2a 2a 0a  2003 April 6.**.
2c4c5 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
2c4c6 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
2c4c7 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
2c4c8 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
2c4c9 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
2c4ca 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
2c4cb 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
2c4cc 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
2c4cd 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
2c4ce 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2c4cf 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
2c4d0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
2c4d1 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
2c4d2 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2c4d3 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
2c4d4 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
2c4d5 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
2c4d6 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
2c4d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c4d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c4d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c4da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
2c4db 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
2c4dc 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69  s code used to i
2c4dd 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 56 41 43  mplement the VAC
2c4de 55 55 4d 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a  UUM command..**.
2c4df 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 63  ** Most of the c
2c4e0 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ode in this file
2c4e1 20 6d 61 79 20 62 65 20 6f 6d 69 74 74 65 64 20   may be omitted 
2c4e2 62 79 20 64 65 66 69 6e 69 6e 67 20 74 68 65 0a  by defining the.
2c4e3 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ** SQLITE_OMIT_V
2c4e4 41 43 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2a 0a  ACUUM macro..**.
2c4e5 2a 2a 20 24 49 64 3a 20 76 61 63 75 75 6d 2e 63  ** $Id: vacuum.c
2c4e6 2c 76 20 31 2e 39 31 20 32 30 30 39 2f 30 37 2f  ,v 1.91 2009/07/
2c4e7 30 32 20 30 37 3a 34 37 3a 33 33 20 64 61 6e 69  02 07:47:33 dani
2c4e8 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f  elk1977 Exp $.*/
2c4e9 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
2c4ea 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55  QLITE_OMIT_VACUU
2c4eb 4d 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  M) && !defined(S
2c4ec 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43  QLITE_OMIT_ATTAC
2c4ed 48 29 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65  H)./*.** Execute
2c4ee 20 7a 53 71 6c 20 6f 6e 20 64 61 74 61 62 61 73   zSql on databas
2c4ef 65 20 64 62 2e 20 52 65 74 75 72 6e 20 61 6e 20  e db. Return an 
2c4f0 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73  error code..*/.s
2c4f1 74 61 74 69 63 20 69 6e 74 20 65 78 65 63 53 71  tatic int execSq
2c4f2 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  l(sqlite3 *db, c
2c4f3 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 29  onst char *zSql)
2c4f4 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  {.  sqlite3_stmt
2c4f5 20 2a 70 53 74 6d 74 3b 0a 20 20 56 56 41 5f 4f   *pStmt;.  VVA_O
2c4f6 4e 4c 59 28 20 69 6e 74 20 72 63 3b 20 29 0a 20  NLY( int rc; ). 
2c4f7 20 69 66 28 20 21 7a 53 71 6c 20 29 7b 0a 20 20   if( !zSql ){.  
2c4f8 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2c4f9 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28  NOMEM;.  }.  if(
2c4fa 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69   SQLITE_OK!=sqli
2c4fb 74 65 33 5f 70 72 65 70 61 72 65 28 64 62 2c 20  te3_prepare(db, 
2c4fc 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74  zSql, -1, &pStmt
2c4fd 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75  , 0) ){.    retu
2c4fe 72 6e 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  rn sqlite3_errco
2c4ff 64 65 28 64 62 29 3b 0a 20 20 7d 0a 20 20 56 56  de(db);.  }.  VV
2c500 41 5f 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 73  A_ONLY( rc = ) s
2c501 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d  qlite3_step(pStm
2c502 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63  t);.  assert( rc
2c503 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 3b 0a  !=SQLITE_ROW );.
2c504 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
2c505 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29  _finalize(pStmt)
2c506 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75  ;.}../*.** Execu
2c507 74 65 20 7a 53 71 6c 20 6f 6e 20 64 61 74 61 62  te zSql on datab
2c508 61 73 65 20 64 62 2e 20 54 68 65 20 73 74 61 74  ase db. The stat
2c509 65 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 65 78  ement returns ex
2c50a 61 63 74 6c 79 0a 2a 2a 20 6f 6e 65 20 63 6f 6c  actly.** one col
2c50b 75 6d 6e 2e 20 45 78 65 63 75 74 65 20 74 68 69  umn. Execute thi
2c50c 73 20 61 73 20 53 51 4c 20 6f 6e 20 74 68 65 20  s as SQL on the 
2c50d 73 61 6d 65 20 64 61 74 61 62 61 73 65 2e 0a 2a  same database..*
2c50e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78 65  /.static int exe
2c50f 63 45 78 65 63 53 71 6c 28 73 71 6c 69 74 65 33  cExecSql(sqlite3
2c510 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   *db, const char
2c511 20 2a 7a 53 71 6c 29 7b 0a 20 20 73 71 6c 69 74   *zSql){.  sqlit
2c512 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a  e3_stmt *pStmt;.
2c513 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20    int rc;..  rc 
2c514 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  = sqlite3_prepar
2c515 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20  e(db, zSql, -1, 
2c516 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 69 66  &pStmt, 0);.  if
2c517 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2c518 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
2c519 77 68 69 6c 65 28 20 53 51 4c 49 54 45 5f 52 4f  while( SQLITE_RO
2c51a 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28  W==sqlite3_step(
2c51b 70 53 74 6d 74 29 20 29 7b 0a 20 20 20 20 72 63  pStmt) ){.    rc
2c51c 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c 20 28   = execSql(db, (
2c51d 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 63 6f  char*)sqlite3_co
2c51e 6c 75 6d 6e 5f 74 65 78 74 28 70 53 74 6d 74 2c  lumn_text(pStmt,
2c51f 20 30 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63   0));.    if( rc
2c520 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
2c521 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e       sqlite3_fin
2c522 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 20 20  alize(pStmt);.  
2c523 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
2c524 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
2c525 72 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  rn sqlite3_final
2c526 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f  ize(pStmt);.}../
2c527 2a 0a 2a 2a 20 54 68 65 20 6e 6f 6e 2d 73 74 61  *.** The non-sta
2c528 6e 64 61 72 64 20 56 41 43 55 55 4d 20 63 6f 6d  ndard VACUUM com
2c529 6d 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20  mand is used to 
2c52a 63 6c 65 61 6e 20 75 70 20 74 68 65 20 64 61 74  clean up the dat
2c52b 61 62 61 73 65 2c 0a 2a 2a 20 63 6f 6c 6c 61 70  abase,.** collap
2c52c 73 65 20 66 72 65 65 20 73 70 61 63 65 2c 20 65  se free space, e
2c52d 74 63 2e 20 20 49 74 20 69 73 20 6d 6f 64 65 6c  tc.  It is model
2c52e 6c 65 64 20 61 66 74 65 72 20 74 68 65 20 56 41  led after the VA
2c52f 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 0a 2a 2a 20  CUUM command.** 
2c530 69 6e 20 50 6f 73 74 67 72 65 53 51 4c 2e 0a 2a  in PostgreSQL..*
2c531 2a 0a 2a 2a 20 49 6e 20 76 65 72 73 69 6f 6e 20  *.** In version 
2c532 31 2e 30 2e 78 20 6f 66 20 53 51 4c 69 74 65 2c  1.0.x of SQLite,
2c533 20 74 68 65 20 56 41 43 55 55 4d 20 63 6f 6d 6d   the VACUUM comm
2c534 61 6e 64 20 77 6f 75 6c 64 20 63 61 6c 6c 0a 2a  and would call.*
2c535 2a 20 67 64 62 6d 5f 72 65 6f 72 67 61 6e 69 7a  * gdbm_reorganiz
2c536 65 28 29 20 6f 6e 20 61 6c 6c 20 74 68 65 20 64  e() on all the d
2c537 61 74 61 62 61 73 65 20 74 61 62 6c 65 73 2e 20  atabase tables. 
2c538 20 42 75 74 20 62 65 67 69 6e 6e 69 6e 67 0a 2a   But beginning.*
2c539 2a 20 77 69 74 68 20 32 2e 30 2e 30 2c 20 53 51  * with 2.0.0, SQ
2c53a 4c 69 74 65 20 6e 6f 20 6c 6f 6e 67 65 72 20 75  Lite no longer u
2c53b 73 65 73 20 47 44 42 4d 20 73 6f 20 74 68 69 73  ses GDBM so this
2c53c 20 63 6f 6d 6d 61 6e 64 20 68 61 73 0a 2a 2a 20   command has.** 
2c53d 62 65 63 6f 6d 65 20 61 20 6e 6f 2d 6f 70 2e 0a  become a no-op..
2c53e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2c53f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61  E void sqlite3Va
2c540 63 75 75 6d 28 50 61 72 73 65 20 2a 70 50 61 72  cuum(Parse *pPar
2c541 73 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  se){.  Vdbe *v =
2c542 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
2c543 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76  pParse);.  if( v
2c544 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
2c545 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2c546 56 61 63 75 75 6d 2c 20 30 2c 20 30 29 3b 0a 20  Vacuum, 0, 0);. 
2c547 20 7d 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a   }.  return;.}..
2c548 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
2c549 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  ne implements th
2c54a 65 20 4f 50 5f 56 61 63 75 75 6d 20 6f 70 63 6f  e OP_Vacuum opco
2c54b 64 65 20 6f 66 20 74 68 65 20 56 44 42 45 2e 0a  de of the VDBE..
2c54c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2c54d 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 75 6e  E int sqlite3Run
2c54e 56 61 63 75 75 6d 28 63 68 61 72 20 2a 2a 70 7a  Vacuum(char **pz
2c54f 45 72 72 4d 73 67 2c 20 73 71 6c 69 74 65 33 20  ErrMsg, sqlite3 
2c550 2a 64 62 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  *db){.  int rc =
2c551 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
2c552 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66  /* Return code f
2c553 72 6f 6d 20 73 65 72 76 69 63 65 20 72 6f 75 74  rom service rout
2c554 69 6e 65 73 20 2a 2f 0a 20 20 42 74 72 65 65 20  ines */.  Btree 
2c555 2a 70 4d 61 69 6e 3b 20 20 20 20 20 20 20 20 20  *pMain;         
2c556 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
2c557 65 20 62 65 69 6e 67 20 76 61 63 75 75 6d 65 64  e being vacuumed
2c558 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 54 65   */.  Btree *pTe
2c559 6d 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  mp;           /*
2c55a 20 54 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64   The temporary d
2c55b 61 74 61 62 61 73 65 20 77 65 20 76 61 63 75 75  atabase we vacuu
2c55c 6d 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 68 61 72  m into */.  char
2c55d 20 2a 7a 53 71 6c 20 3d 20 30 3b 20 20 20 20 20   *zSql = 0;     
2c55e 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65      /* SQL state
2c55f 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 73  ments */.  int s
2c560 61 76 65 64 5f 66 6c 61 67 73 3b 20 20 20 20 20  aved_flags;     
2c561 20 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75     /* Saved valu
2c562 65 20 6f 66 20 74 68 65 20 64 62 2d 3e 66 6c 61  e of the db->fla
2c563 67 73 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65  gs */.  int save
2c564 64 5f 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20  d_nChange;      
2c565 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65 20 6f  /* Saved value o
2c566 66 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f  f db->nChange */
2c567 0a 20 20 69 6e 74 20 73 61 76 65 64 5f 6e 54 6f  .  int saved_nTo
2c568 74 61 6c 43 68 61 6e 67 65 3b 20 2f 2a 20 53 61  talChange; /* Sa
2c569 76 65 64 20 76 61 6c 75 65 20 6f 66 20 64 62 2d  ved value of db-
2c56a 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20 2a 2f  >nTotalChange */
2c56b 0a 20 20 44 62 20 2a 70 44 62 20 3d 20 30 3b 20  .  Db *pDb = 0; 
2c56c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
2c56d 74 61 62 61 73 65 20 74 6f 20 64 65 74 61 63 68  tabase to detach
2c56e 20 61 74 20 65 6e 64 20 6f 66 20 76 61 63 75 75   at end of vacuu
2c56f 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4d 65 6d  m */.  int isMem
2c570 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  Db;            /
2c571 2a 20 54 72 75 65 20 69 66 20 76 61 63 75 75 6d  * True if vacuum
2c572 69 6e 67 20 61 20 3a 6d 65 6d 6f 72 79 3a 20 64  ing a :memory: d
2c573 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74  atabase */.  int
2c574 20 6e 52 65 73 3b 0a 0a 20 20 69 66 28 20 21 64   nRes;..  if( !d
2c575 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b  b->autoCommit ){
2c576 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
2c577 74 72 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20  tring(pzErrMsg, 
2c578 64 62 2c 20 22 63 61 6e 6e 6f 74 20 56 41 43 55  db, "cannot VACU
2c579 55 4d 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61  UM from within a
2c57a 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a   transaction");.
2c57b 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2c57c 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20  E_ERROR;.  }..  
2c57d 2f 2a 20 53 61 76 65 20 74 68 65 20 63 75 72 72  /* Save the curr
2c57e 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65  ent value of the
2c57f 20 77 72 69 74 65 2d 73 63 68 65 6d 61 20 66 6c   write-schema fl
2c580 61 67 20 62 65 66 6f 72 65 20 73 65 74 74 69 6e  ag before settin
2c581 67 20 69 74 2e 20 2a 2f 0a 20 20 73 61 76 65 64  g it. */.  saved
2c582 5f 66 6c 61 67 73 20 3d 20 64 62 2d 3e 66 6c 61  _flags = db->fla
2c583 67 73 3b 0a 20 20 73 61 76 65 64 5f 6e 43 68 61  gs;.  saved_nCha
2c584 6e 67 65 20 3d 20 64 62 2d 3e 6e 43 68 61 6e 67  nge = db->nChang
2c585 65 3b 0a 20 20 73 61 76 65 64 5f 6e 54 6f 74 61  e;.  saved_nTota
2c586 6c 43 68 61 6e 67 65 20 3d 20 64 62 2d 3e 6e 54  lChange = db->nT
2c587 6f 74 61 6c 43 68 61 6e 67 65 3b 0a 20 20 64 62  otalChange;.  db
2c588 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54  ->flags |= SQLIT
2c589 45 5f 57 72 69 74 65 53 63 68 65 6d 61 20 7c 20  E_WriteSchema | 
2c58a 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 65  SQLITE_IgnoreChe
2c58b 63 6b 73 3b 0a 0a 20 20 70 4d 61 69 6e 20 3d 20  cks;..  pMain = 
2c58c 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 3b 0a  db->aDb[0].pBt;.
2c58d 20 20 69 73 4d 65 6d 44 62 20 3d 20 73 71 6c 69    isMemDb = sqli
2c58e 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28  te3PagerIsMemdb(
2c58f 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
2c590 72 28 70 4d 61 69 6e 29 29 3b 0a 0a 20 20 2f 2a  r(pMain));..  /*
2c591 20 41 74 74 61 63 68 20 74 68 65 20 74 65 6d 70   Attach the temp
2c592 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 20 61  orary database a
2c593 73 20 27 76 61 63 75 75 6d 5f 64 62 27 2e 20 54  s 'vacuum_db'. T
2c594 68 65 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 70  he synchronous p
2c595 72 61 67 6d 61 0a 20 20 2a 2a 20 63 61 6e 20 62  ragma.  ** can b
2c596 65 20 73 65 74 20 74 6f 20 27 6f 66 66 27 20 66  e set to 'off' f
2c597 6f 72 20 74 68 69 73 20 66 69 6c 65 2c 20 61 73  or this file, as
2c598 20 69 74 20 69 73 20 6e 6f 74 20 72 65 63 6f 76   it is not recov
2c599 65 72 65 64 20 69 66 20 61 20 63 72 61 73 68 0a  ered if a crash.
2c59a 20 20 2a 2a 20 6f 63 63 75 72 73 20 61 6e 79 77    ** occurs anyw
2c59b 61 79 2e 20 54 68 65 20 69 6e 74 65 67 72 69 74  ay. The integrit
2c59c 79 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  y of the databas
2c59d 65 20 69 73 20 6d 61 69 6e 74 61 69 6e 65 64 20  e is maintained 
2c59e 62 79 20 61 0a 20 20 2a 2a 20 28 70 6f 73 73 69  by a.  ** (possi
2c59f 62 6c 79 20 73 79 6e 63 68 72 6f 6e 6f 75 73 29  bly synchronous)
2c5a0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65   transaction ope
2c5a1 6e 65 64 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20  ned on the main 
2c5a2 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a  database before.
2c5a3 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65    ** sqlite3Btre
2c5a4 65 43 6f 70 79 46 69 6c 65 28 29 20 69 73 20 63  eCopyFile() is c
2c5a5 61 6c 6c 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  alled..  **.  **
2c5a6 20 41 6e 20 6f 70 74 69 6d 69 73 61 74 69 6f 6e   An optimisation
2c5a7 20 77 6f 75 6c 64 20 62 65 20 74 6f 20 75 73 65   would be to use
2c5a8 20 61 20 6e 6f 6e 2d 6a 6f 75 72 6e 61 6c 65 64   a non-journaled
2c5a9 20 70 61 67 65 72 2e 0a 20 20 2a 2a 20 28 4c 61   pager..  ** (La
2c5aa 74 65 72 3a 29 20 49 20 74 72 69 65 64 20 73 65  ter:) I tried se
2c5ab 74 74 69 6e 67 20 22 50 52 41 47 4d 41 20 76 61  tting "PRAGMA va
2c5ac 63 75 75 6d 5f 64 62 2e 6a 6f 75 72 6e 61 6c 5f  cuum_db.journal_
2c5ad 6d 6f 64 65 3d 4f 46 46 22 20 62 75 74 0a 20 20  mode=OFF" but.  
2c5ae 2a 2a 20 74 68 61 74 20 61 63 74 75 61 6c 6c 79  ** that actually
2c5af 20 6d 61 64 65 20 74 68 65 20 56 41 43 55 55 4d   made the VACUUM
2c5b0 20 72 75 6e 20 73 6c 6f 77 65 72 2e 20 20 56 65   run slower.  Ve
2c5b1 72 79 20 6c 69 74 74 6c 65 20 6a 6f 75 72 6e 61  ry little journa
2c5b2 6c 6c 69 6e 67 0a 20 20 2a 2a 20 61 63 74 75 61  lling.  ** actua
2c5b3 6c 6c 79 20 6f 63 63 75 72 73 20 77 68 65 6e 20  lly occurs when 
2c5b4 64 6f 69 6e 67 20 61 20 76 61 63 75 75 6d 20 73  doing a vacuum s
2c5b5 69 6e 63 65 20 74 68 65 20 76 61 63 75 75 6d 5f  ince the vacuum_
2c5b6 64 62 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 0a  db is initially.
2c5b7 20 20 2a 2a 20 65 6d 70 74 79 2e 20 20 4f 6e 6c    ** empty.  Onl
2c5b8 79 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65  y the journal he
2c5b9 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e 2e  ader is written.
2c5ba 20 20 41 70 70 61 72 65 6e 74 6c 79 20 69 74 20    Apparently it 
2c5bb 74 61 6b 65 73 20 6d 6f 72 65 0a 20 20 2a 2a 20  takes more.  ** 
2c5bc 74 69 6d 65 20 74 6f 20 70 61 72 73 65 20 61 6e  time to parse an
2c5bd 64 20 72 75 6e 20 74 68 65 20 50 52 41 47 4d 41  d run the PRAGMA
2c5be 20 74 6f 20 74 75 72 6e 20 6a 6f 75 72 6e 61 6c   to turn journal
2c5bf 6c 69 6e 67 20 6f 66 66 20 74 68 61 6e 20 69 74  ling off than it
2c5c0 20 64 6f 65 73 0a 20 20 2a 2a 20 74 6f 20 77 72   does.  ** to wr
2c5c1 69 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ite the journal 
2c5c2 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 20 20 2a  header file..  *
2c5c3 2f 0a 20 20 7a 53 71 6c 20 3d 20 22 41 54 54 41  /.  zSql = "ATTA
2c5c4 43 48 20 27 27 20 41 53 20 76 61 63 75 75 6d 5f  CH '' AS vacuum_
2c5c5 64 62 3b 22 3b 0a 20 20 72 63 20 3d 20 65 78 65  db;";.  rc = exe
2c5c6 63 53 71 6c 28 64 62 2c 20 7a 53 71 6c 29 3b 0a  cSql(db, zSql);.
2c5c7 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2c5c8 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f  _OK ) goto end_o
2c5c9 66 5f 76 61 63 75 75 6d 3b 0a 20 20 70 44 62 20  f_vacuum;.  pDb 
2c5ca 3d 20 26 64 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e  = &db->aDb[db->n
2c5cb 44 62 2d 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  Db-1];.  assert(
2c5cc 20 73 74 72 63 6d 70 28 64 62 2d 3e 61 44 62 5b   strcmp(db->aDb[
2c5cd 64 62 2d 3e 6e 44 62 2d 31 5d 2e 7a 4e 61 6d 65  db->nDb-1].zName
2c5ce 2c 22 76 61 63 75 75 6d 5f 64 62 22 29 3d 3d 30  ,"vacuum_db")==0
2c5cf 20 29 3b 0a 20 20 70 54 65 6d 70 20 3d 20 64 62   );.  pTemp = db
2c5d0 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44 62 2d 31 5d  ->aDb[db->nDb-1]
2c5d1 2e 70 42 74 3b 0a 0a 20 20 6e 52 65 73 20 3d 20  .pBt;..  nRes = 
2c5d2 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 52  sqlite3BtreeGetR
2c5d3 65 73 65 72 76 65 28 70 4d 61 69 6e 29 3b 0a 0a  eserve(pMain);..
2c5d4 20 20 2f 2a 20 41 20 56 41 43 55 55 4d 20 63 61    /* A VACUUM ca
2c5d5 6e 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20  nnot change the 
2c5d6 70 61 67 65 73 69 7a 65 20 6f 66 20 61 6e 20 65  pagesize of an e
2c5d7 6e 63 72 79 70 74 65 64 20 64 61 74 61 62 61 73  ncrypted databas
2c5d8 65 2e 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  e. */.#ifdef SQL
2c5d9 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20  ITE_HAS_CODEC.  
2c5da 69 66 28 20 64 62 2d 3e 6e 65 78 74 50 61 67 65  if( db->nextPage
2c5db 73 69 7a 65 20 29 7b 0a 20 20 20 20 65 78 74 65  size ){.    exte
2c5dc 72 6e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  rn void sqlite3C
2c5dd 6f 64 65 63 47 65 74 4b 65 79 28 73 71 6c 69 74  odecGetKey(sqlit
2c5de 65 33 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 2a 2a  e3*, int, void**
2c5df 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20 69 6e 74  , int*);.    int
2c5e0 20 6e 4b 65 79 3b 0a 20 20 20 20 63 68 61 72 20   nKey;.    char 
2c5e1 2a 7a 4b 65 79 3b 0a 20 20 20 20 73 71 6c 69 74  *zKey;.    sqlit
2c5e2 65 33 43 6f 64 65 63 47 65 74 4b 65 79 28 64 62  e3CodecGetKey(db
2c5e3 2c 20 30 2c 20 28 76 6f 69 64 2a 2a 29 26 7a 4b  , 0, (void**)&zK
2c5e4 65 79 2c 20 26 6e 4b 65 79 29 3b 0a 20 20 20 20  ey, &nKey);.    
2c5e5 69 66 28 20 6e 4b 65 79 20 29 20 64 62 2d 3e 6e  if( nKey ) db->n
2c5e6 65 78 74 50 61 67 65 73 69 7a 65 20 3d 20 30 3b  extPagesize = 0;
2c5e7 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69  .  }.#endif..  i
2c5e8 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  f( sqlite3BtreeS
2c5e9 65 74 50 61 67 65 53 69 7a 65 28 70 54 65 6d 70  etPageSize(pTemp
2c5ea 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  , sqlite3BtreeGe
2c5eb 74 50 61 67 65 53 69 7a 65 28 70 4d 61 69 6e 29  tPageSize(pMain)
2c5ec 2c 20 6e 52 65 73 2c 20 30 29 0a 20 20 20 7c 7c  , nRes, 0).   ||
2c5ed 20 28 21 69 73 4d 65 6d 44 62 20 26 26 20 73 71   (!isMemDb && sq
2c5ee 6c 69 74 65 33 42 74 72 65 65 53 65 74 50 61 67  lite3BtreeSetPag
2c5ef 65 53 69 7a 65 28 70 54 65 6d 70 2c 20 64 62 2d  eSize(pTemp, db-
2c5f0 3e 6e 65 78 74 50 61 67 65 73 69 7a 65 2c 20 6e  >nextPagesize, n
2c5f1 52 65 73 2c 20 30 29 29 0a 20 20 20 7c 7c 20 4e  Res, 0)).   || N
2c5f2 45 56 45 52 28 64 62 2d 3e 6d 61 6c 6c 6f 63 46  EVER(db->mallocF
2c5f3 61 69 6c 65 64 29 0a 20 20 29 7b 0a 20 20 20 20  ailed).  ){.    
2c5f4 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
2c5f5 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  M;.    goto end_
2c5f6 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 7d 0a 20  of_vacuum;.  }. 
2c5f7 20 72 63 20 3d 20 65 78 65 63 53 71 6c 28 64 62   rc = execSql(db
2c5f8 2c 20 22 50 52 41 47 4d 41 20 76 61 63 75 75 6d  , "PRAGMA vacuum
2c5f9 5f 64 62 2e 73 79 6e 63 68 72 6f 6e 6f 75 73 3d  _db.synchronous=
2c5fa 4f 46 46 22 29 3b 0a 20 20 69 66 28 20 72 63 21  OFF");.  if( rc!
2c5fb 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
2c5fc 20 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61    goto end_of_va
2c5fd 63 75 75 6d 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64  cuum;.  }..#ifnd
2c5fe 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
2c5ff 55 54 4f 56 41 43 55 55 4d 0a 20 20 73 71 6c 69  UTOVACUUM.  sqli
2c600 74 65 33 42 74 72 65 65 53 65 74 41 75 74 6f 56  te3BtreeSetAutoV
2c601 61 63 75 75 6d 28 70 54 65 6d 70 2c 20 64 62 2d  acuum(pTemp, db-
2c602 3e 6e 65 78 74 41 75 74 6f 76 61 63 3e 3d 30 20  >nextAutovac>=0 
2c603 3f 20 64 62 2d 3e 6e 65 78 74 41 75 74 6f 76 61  ? db->nextAutova
2c604 63 20 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20  c :.            
2c605 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c606 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
2c607 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 41 75  qlite3BtreeGetAu
2c608 74 6f 56 61 63 75 75 6d 28 70 4d 61 69 6e 29 29  toVacuum(pMain))
2c609 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42  ;.#endif..  /* B
2c60a 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69  egin a transacti
2c60b 6f 6e 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65  on */.  rc = exe
2c60c 63 53 71 6c 28 64 62 2c 20 22 42 45 47 49 4e 20  cSql(db, "BEGIN 
2c60d 45 58 43 4c 55 53 49 56 45 3b 22 29 3b 0a 20 20  EXCLUSIVE;");.  
2c60e 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2c60f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f  K ) goto end_of_
2c610 76 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20 51 75  vacuum;..  /* Qu
2c611 65 72 79 20 74 68 65 20 73 63 68 65 6d 61 20 6f  ery the schema o
2c612 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  f the main datab
2c613 61 73 65 2e 20 43 72 65 61 74 65 20 61 20 6d 69  ase. Create a mi
2c614 72 72 6f 72 20 73 63 68 65 6d 61 0a 20 20 2a 2a  rror schema.  **
2c615 20 69 6e 20 74 68 65 20 74 65 6d 70 6f 72 61 72   in the temporar
2c616 79 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f  y database..  */
2c617 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78 65 63  .  rc = execExec
2c618 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20 20 22  Sql(db, .      "
2c619 53 45 4c 45 43 54 20 27 43 52 45 41 54 45 20 54  SELECT 'CREATE T
2c61a 41 42 4c 45 20 76 61 63 75 75 6d 5f 64 62 2e 27  ABLE vacuum_db.'
2c61b 20 7c 7c 20 73 75 62 73 74 72 28 73 71 6c 2c 31   || substr(sql,1
2c61c 34 29 20 22 0a 20 20 20 20 20 20 22 20 20 46 52  4) ".      "  FR
2c61d 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  OM sqlite_master
2c61e 20 57 48 45 52 45 20 74 79 70 65 3d 27 74 61 62   WHERE type='tab
2c61f 6c 65 27 20 41 4e 44 20 6e 61 6d 65 21 3d 27 73  le' AND name!='s
2c620 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 27 22  qlite_sequence'"
2c621 0a 20 20 20 20 20 20 22 20 20 20 41 4e 44 20 72  .      "   AND r
2c622 6f 6f 74 70 61 67 65 3e 30 22 0a 20 20 29 3b 0a  ootpage>0".  );.
2c623 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2c624 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f  _OK ) goto end_o
2c625 66 5f 76 61 63 75 75 6d 3b 0a 20 20 72 63 20 3d  f_vacuum;.  rc =
2c626 20 65 78 65 63 45 78 65 63 53 71 6c 28 64 62 2c   execExecSql(db,
2c627 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54 20   .      "SELECT 
2c628 27 43 52 45 41 54 45 20 49 4e 44 45 58 20 76 61  'CREATE INDEX va
2c629 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 73 75 62  cuum_db.' || sub
2c62a 73 74 72 28 73 71 6c 2c 31 34 29 22 0a 20 20 20  str(sql,14)".   
2c62b 20 20 20 22 20 20 46 52 4f 4d 20 73 71 6c 69 74     "  FROM sqlit
2c62c 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20 73  e_master WHERE s
2c62d 71 6c 20 4c 49 4b 45 20 27 43 52 45 41 54 45 20  ql LIKE 'CREATE 
2c62e 49 4e 44 45 58 20 25 27 20 22 29 3b 0a 20 20 69  INDEX %' ");.  i
2c62f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2c630 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76   ) goto end_of_v
2c631 61 63 75 75 6d 3b 0a 20 20 72 63 20 3d 20 65 78  acuum;.  rc = ex
2c632 65 63 45 78 65 63 53 71 6c 28 64 62 2c 20 0a 20  ecExecSql(db, . 
2c633 20 20 20 20 20 22 53 45 4c 45 43 54 20 27 43 52       "SELECT 'CR
2c634 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e 44 45  EATE UNIQUE INDE
2c635 58 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c  X vacuum_db.' ||
2c636 20 73 75 62 73 74 72 28 73 71 6c 2c 32 31 29 20   substr(sql,21) 
2c637 22 0a 20 20 20 20 20 20 22 20 20 46 52 4f 4d 20  ".      "  FROM 
2c638 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48  sqlite_master WH
2c639 45 52 45 20 73 71 6c 20 4c 49 4b 45 20 27 43 52  ERE sql LIKE 'CR
2c63a 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e 44 45  EATE UNIQUE INDE
2c63b 58 20 25 27 22 29 3b 0a 20 20 69 66 28 20 72 63  X %'");.  if( rc
2c63c 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
2c63d 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
2c63e 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20 74 68 72  ;..  /* Loop thr
2c63f 6f 75 67 68 20 74 68 65 20 74 61 62 6c 65 73 20  ough the tables 
2c640 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  in the main data
2c641 62 61 73 65 2e 20 46 6f 72 20 65 61 63 68 2c 20  base. For each, 
2c642 64 6f 0a 20 20 2a 2a 20 61 6e 20 22 49 4e 53 45  do.  ** an "INSE
2c643 52 54 20 49 4e 54 4f 20 76 61 63 75 75 6d 5f 64  RT INTO vacuum_d
2c644 62 2e 78 78 78 20 53 45 4c 45 43 54 20 2a 20 46  b.xxx SELECT * F
2c645 52 4f 4d 20 78 78 78 3b 22 20 74 6f 20 63 6f 70  ROM xxx;" to cop
2c646 79 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6e 74 65  y.  ** the conte
2c647 6e 74 73 20 74 6f 20 74 68 65 20 74 65 6d 70 6f  nts to the tempo
2c648 72 61 72 79 20 64 61 74 61 62 61 73 65 2e 0a 20  rary database.. 
2c649 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65 63 45   */.  rc = execE
2c64a 78 65 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20  xecSql(db, .    
2c64b 20 20 22 53 45 4c 45 43 54 20 27 49 4e 53 45 52    "SELECT 'INSER
2c64c 54 20 49 4e 54 4f 20 76 61 63 75 75 6d 5f 64 62  T INTO vacuum_db
2c64d 2e 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65  .' || quote(name
2c64e 29 20 22 0a 20 20 20 20 20 20 22 7c 7c 20 27 20  ) ".      "|| ' 
2c64f 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 27 20  SELECT * FROM ' 
2c650 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20 7c  || quote(name) |
2c651 7c 20 27 3b 27 22 0a 20 20 20 20 20 20 22 46 52  | ';'".      "FR
2c652 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  OM sqlite_master
2c653 20 22 0a 20 20 20 20 20 20 22 57 48 45 52 45 20   ".      "WHERE 
2c654 74 79 70 65 20 3d 20 27 74 61 62 6c 65 27 20 41  type = 'table' A
2c655 4e 44 20 6e 61 6d 65 21 3d 27 73 71 6c 69 74 65  ND name!='sqlite
2c656 5f 73 65 71 75 65 6e 63 65 27 20 22 0a 20 20 20  _sequence' ".   
2c657 20 20 20 22 20 20 41 4e 44 20 72 6f 6f 74 70 61     "  AND rootpa
2c658 67 65 3e 30 22 0a 0a 20 20 29 3b 0a 20 20 69 66  ge>0"..  );.  if
2c659 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2c65a 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61  ) goto end_of_va
2c65b 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20 43 6f 70 79  cuum;..  /* Copy
2c65c 20 6f 76 65 72 20 74 68 65 20 73 65 71 75 65 6e   over the sequen
2c65d 63 65 20 74 61 62 6c 65 0a 20 20 2a 2f 0a 20 20  ce table.  */.  
2c65e 72 63 20 3d 20 65 78 65 63 45 78 65 63 53 71 6c  rc = execExecSql
2c65f 28 64 62 2c 20 0a 20 20 20 20 20 20 22 53 45 4c  (db, .      "SEL
2c660 45 43 54 20 27 44 45 4c 45 54 45 20 46 52 4f 4d  ECT 'DELETE FROM
2c661 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20   vacuum_db.' || 
2c662 71 75 6f 74 65 28 6e 61 6d 65 29 20 7c 7c 20 27  quote(name) || '
2c663 3b 27 20 22 0a 20 20 20 20 20 20 22 46 52 4f 4d  ;' ".      "FROM
2c664 20 76 61 63 75 75 6d 5f 64 62 2e 73 71 6c 69 74   vacuum_db.sqlit
2c665 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20 6e  e_master WHERE n
2c666 61 6d 65 3d 27 73 71 6c 69 74 65 5f 73 65 71 75  ame='sqlite_sequ
2c667 65 6e 63 65 27 20 22 0a 20 20 29 3b 0a 20 20 69  ence' ".  );.  i
2c668 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2c669 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76   ) goto end_of_v
2c66a 61 63 75 75 6d 3b 0a 20 20 72 63 20 3d 20 65 78  acuum;.  rc = ex
2c66b 65 63 45 78 65 63 53 71 6c 28 64 62 2c 20 0a 20  ecExecSql(db, . 
2c66c 20 20 20 20 20 22 53 45 4c 45 43 54 20 27 49 4e       "SELECT 'IN
2c66d 53 45 52 54 20 49 4e 54 4f 20 76 61 63 75 75 6d  SERT INTO vacuum
2c66e 5f 64 62 2e 27 20 7c 7c 20 71 75 6f 74 65 28 6e  _db.' || quote(n
2c66f 61 6d 65 29 20 22 0a 20 20 20 20 20 20 22 7c 7c  ame) ".      "||
2c670 20 27 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d   ' SELECT * FROM
2c671 20 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65   ' || quote(name
2c672 29 20 7c 7c 20 27 3b 27 20 22 0a 20 20 20 20 20  ) || ';' ".     
2c673 20 22 46 52 4f 4d 20 76 61 63 75 75 6d 5f 64 62   "FROM vacuum_db
2c674 2e 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57  .sqlite_master W
2c675 48 45 52 45 20 6e 61 6d 65 3d 3d 27 73 71 6c 69  HERE name=='sqli
2c676 74 65 5f 73 65 71 75 65 6e 63 65 27 3b 22 0a 20  te_sequence';". 
2c677 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51   );.  if( rc!=SQ
2c678 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
2c679 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 0a  nd_of_vacuum;...
2c67a 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 74 72    /* Copy the tr
2c67b 69 67 67 65 72 73 2c 20 76 69 65 77 73 2c 20 61  iggers, views, a
2c67c 6e 64 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  nd virtual table
2c67d 73 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20  s from the main 
2c67e 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 6f 76  database.  ** ov
2c67f 65 72 20 74 6f 20 74 68 65 20 74 65 6d 70 6f 72  er to the tempor
2c680 61 72 79 20 64 61 74 61 62 61 73 65 2e 20 20 4e  ary database.  N
2c681 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 6f 62 6a  one of these obj
2c682 65 63 74 73 20 68 61 73 20 61 6e 79 0a 20 20 2a  ects has any.  *
2c683 2a 20 61 73 73 6f 63 69 61 74 65 64 20 73 74 6f  * associated sto
2c684 72 61 67 65 2c 20 73 6f 20 61 6c 6c 20 77 65 20  rage, so all we 
2c685 68 61 76 65 20 74 6f 20 64 6f 20 69 73 20 63 6f  have to do is co
2c686 70 79 20 74 68 65 69 72 20 65 6e 74 72 69 65 73  py their entries
2c687 0a 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 53  .  ** from the S
2c688 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62  QLITE_MASTER tab
2c689 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20  le..  */.  rc = 
2c68a 65 78 65 63 53 71 6c 28 64 62 2c 0a 20 20 20 20  execSql(db,.    
2c68b 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 76    "INSERT INTO v
2c68c 61 63 75 75 6d 5f 64 62 2e 73 71 6c 69 74 65 5f  acuum_db.sqlite_
2c68d 6d 61 73 74 65 72 20 22 0a 20 20 20 20 20 20 22  master ".      "
2c68e 20 20 53 45 4c 45 43 54 20 74 79 70 65 2c 20 6e    SELECT type, n
2c68f 61 6d 65 2c 20 74 62 6c 5f 6e 61 6d 65 2c 20 72  ame, tbl_name, r
2c690 6f 6f 74 70 61 67 65 2c 20 73 71 6c 22 0a 20 20  ootpage, sql".  
2c691 20 20 20 20 22 20 20 20 20 46 52 4f 4d 20 73 71      "    FROM sq
2c692 6c 69 74 65 5f 6d 61 73 74 65 72 22 0a 20 20 20  lite_master".   
2c693 20 20 20 22 20 20 20 57 48 45 52 45 20 74 79 70     "   WHERE typ
2c694 65 3d 27 76 69 65 77 27 20 4f 52 20 74 79 70 65  e='view' OR type
2c695 3d 27 74 72 69 67 67 65 72 27 22 0a 20 20 20 20  ='trigger'".    
2c696 20 20 22 20 20 20 20 20 20 4f 52 20 28 74 79 70    "      OR (typ
2c697 65 3d 27 74 61 62 6c 65 27 20 41 4e 44 20 72 6f  e='table' AND ro
2c698 6f 74 70 61 67 65 3d 30 29 22 0a 20 20 29 3b 0a  otpage=0)".  );.
2c699 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20    if( rc ) goto 
2c69a 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a  end_of_vacuum;..
2c69b 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69    /* At this poi
2c69c 6e 74 2c 20 75 6e 6c 65 73 73 20 74 68 65 20 6d  nt, unless the m
2c69d 61 69 6e 20 64 62 20 77 61 73 20 63 6f 6d 70 6c  ain db was compl
2c69e 65 74 65 6c 79 20 65 6d 70 74 79 2c 20 74 68 65  etely empty, the
2c69f 72 65 20 69 73 20 6e 6f 77 20 61 0a 20 20 2a 2a  re is now a.  **
2c6a0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65   transaction ope
2c6a1 6e 20 6f 6e 20 74 68 65 20 76 61 63 75 75 6d 20  n on the vacuum 
2c6a2 64 61 74 61 62 61 73 65 2c 20 62 75 74 20 6e 6f  database, but no
2c6a3 74 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 64 61  t on the main da
2c6a4 74 61 62 61 73 65 2e 0a 20 20 2a 2a 20 4f 70 65  tabase..  ** Ope
2c6a5 6e 20 61 20 62 74 72 65 65 20 6c 65 76 65 6c 20  n a btree level 
2c6a6 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74  transaction on t
2c6a7 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
2c6a8 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 61 0a  . This allows a.
2c6a9 20 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c    ** call to sql
2c6aa 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c  ite3BtreeCopyFil
2c6ab 65 28 29 2e 20 54 68 65 20 6d 61 69 6e 20 64 61  e(). The main da
2c6ac 74 61 62 61 73 65 20 62 74 72 65 65 20 6c 65 76  tabase btree lev
2c6ad 65 6c 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74  el.  ** transact
2c6ae 69 6f 6e 20 69 73 20 74 68 65 6e 20 63 6f 6d 6d  ion is then comm
2c6af 69 74 74 65 64 2c 20 73 6f 20 74 68 65 20 53 51  itted, so the SQ
2c6b0 4c 20 6c 65 76 65 6c 20 6e 65 76 65 72 20 6b 6e  L level never kn
2c6b1 6f 77 73 20 69 74 20 77 61 73 0a 20 20 2a 2a 20  ows it was.  ** 
2c6b2 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69  opened for writi
2c6b3 6e 67 2e 20 54 68 69 73 20 77 61 79 2c 20 74 68  ng. This way, th
2c6b4 65 20 53 51 4c 20 74 72 61 6e 73 61 63 74 69 6f  e SQL transactio
2c6b5 6e 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65  n used to create
2c6b6 20 74 68 65 0a 20 20 2a 2a 20 74 65 6d 70 6f 72   the.  ** tempor
2c6b7 61 72 79 20 64 61 74 61 62 61 73 65 20 6e 65 76  ary database nev
2c6b8 65 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20 63  er needs to be c
2c6b9 6f 6d 6d 69 74 74 65 64 2e 0a 20 20 2a 2f 0a 20  ommitted..  */. 
2c6ba 20 7b 0a 20 20 20 20 75 33 32 20 6d 65 74 61 3b   {.    u32 meta;
2c6bb 0a 20 20 20 20 69 6e 74 20 69 3b 0a 0a 20 20 20  .    int i;..   
2c6bc 20 2f 2a 20 54 68 69 73 20 61 72 72 61 79 20 64   /* This array d
2c6bd 65 74 65 72 6d 69 6e 65 73 20 77 68 69 63 68 20  etermines which 
2c6be 6d 65 74 61 20 6d 65 74 61 20 76 61 6c 75 65 73  meta meta values
2c6bf 20 61 72 65 20 70 72 65 73 65 72 76 65 64 20 69   are preserved i
2c6c0 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 76 61 63  n the.    ** vac
2c6c1 75 75 6d 2e 20 20 45 76 65 6e 20 65 6e 74 72 69  uum.  Even entri
2c6c2 65 73 20 61 72 65 20 74 68 65 20 6d 65 74 61 20  es are the meta 
2c6c3 76 61 6c 75 65 20 6e 75 6d 62 65 72 20 61 6e 64  value number and
2c6c4 20 6f 64 64 20 65 6e 74 72 69 65 73 0a 20 20 20   odd entries.   
2c6c5 20 2a 2a 20 61 72 65 20 61 6e 20 69 6e 63 72 65   ** are an incre
2c6c6 6d 65 6e 74 20 74 6f 20 61 70 70 6c 79 20 74 6f  ment to apply to
2c6c7 20 74 68 65 20 6d 65 74 61 20 76 61 6c 75 65 20   the meta value 
2c6c8 61 66 74 65 72 20 74 68 65 20 76 61 63 75 75 6d  after the vacuum
2c6c9 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 69 6e 63  ..    ** The inc
2c6ca 72 65 6d 65 6e 74 20 69 73 20 75 73 65 64 20 74  rement is used t
2c6cb 6f 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 73  o increase the s
2c6cc 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 73 6f 20  chema cookie so 
2c6cd 74 68 61 74 20 6f 74 68 65 72 0a 20 20 20 20 2a  that other.    *
2c6ce 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 6f  * connections to
2c6cf 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
2c6d0 73 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 74 6f 20  se will know to 
2c6d1 72 65 72 65 61 64 20 74 68 65 20 73 63 68 65 6d  reread the schem
2c6d2 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 74  a..    */.    st
2c6d3 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
2c6d4 6e 65 64 20 63 68 61 72 20 61 43 6f 70 79 5b 5d  ned char aCopy[]
2c6d5 20 3d 20 7b 0a 20 20 20 20 20 20 20 42 54 52 45   = {.       BTRE
2c6d6 45 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e  E_SCHEMA_VERSION
2c6d7 2c 20 20 20 20 20 31 2c 20 20 2f 2a 20 41 64 64  ,     1,  /* Add
2c6d8 20 6f 6e 65 20 74 6f 20 74 68 65 20 6f 6c 64 20   one to the old 
2c6d9 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 2a 2f  schema cookie */
2c6da 0a 20 20 20 20 20 20 20 42 54 52 45 45 5f 44 45  .       BTREE_DE
2c6db 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45  FAULT_CACHE_SIZE
2c6dc 2c 20 30 2c 20 20 2f 2a 20 50 72 65 73 65 72 76  , 0,  /* Preserv
2c6dd 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 70 61  e the default pa
2c6de 67 65 20 63 61 63 68 65 20 73 69 7a 65 20 2a 2f  ge cache size */
2c6df 0a 20 20 20 20 20 20 20 42 54 52 45 45 5f 54 45  .       BTREE_TE
2c6e0 58 54 5f 45 4e 43 4f 44 49 4e 47 2c 20 20 20 20  XT_ENCODING,    
2c6e1 20 20 30 2c 20 20 2f 2a 20 50 72 65 73 65 72 76    0,  /* Preserv
2c6e2 65 20 74 68 65 20 74 65 78 74 20 65 6e 63 6f 64  e the text encod
2c6e3 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 20 42 54  ing */.       BT
2c6e4 52 45 45 5f 55 53 45 52 5f 56 45 52 53 49 4f 4e  REE_USER_VERSION
2c6e5 2c 20 20 20 20 20 20 20 30 2c 20 20 2f 2a 20 50  ,       0,  /* P
2c6e6 72 65 73 65 72 76 65 20 74 68 65 20 75 73 65 72  reserve the user
2c6e7 20 76 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20   version */.    
2c6e8 7d 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20  };..    assert( 
2c6e9 31 3d 3d 73 71 6c 69 74 65 33 42 74 72 65 65 49  1==sqlite3BtreeI
2c6ea 73 49 6e 54 72 61 6e 73 28 70 54 65 6d 70 29 20  sInTrans(pTemp) 
2c6eb 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 31  );.    assert( 1
2c6ec 3d 3d 73 71 6c 69 74 65 33 42 74 72 65 65 49 73  ==sqlite3BtreeIs
2c6ed 49 6e 54 72 61 6e 73 28 70 4d 61 69 6e 29 20 29  InTrans(pMain) )
2c6ee 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 42  ;..    /* Copy B
2c6ef 74 72 65 65 20 6d 65 74 61 20 76 61 6c 75 65 73  tree meta values
2c6f0 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   */.    for(i=0;
2c6f1 20 69 3c 41 72 72 61 79 53 69 7a 65 28 61 43 6f   i<ArraySize(aCo
2c6f2 70 79 29 3b 20 69 2b 3d 32 29 7b 0a 20 20 20 20  py); i+=2){.    
2c6f3 20 20 2f 2a 20 47 65 74 4d 65 74 61 28 29 20 61    /* GetMeta() a
2c6f4 6e 64 20 55 70 64 61 74 65 4d 65 74 61 28 29 20  nd UpdateMeta() 
2c6f5 63 61 6e 6e 6f 74 20 66 61 69 6c 20 69 6e 20 74  cannot fail in t
2c6f6 68 69 73 20 63 6f 6e 74 65 78 74 20 62 65 63 61  his context beca
2c6f7 75 73 65 0a 20 20 20 20 20 20 2a 2a 20 77 65 20  use.      ** we 
2c6f8 61 6c 72 65 61 64 79 20 68 61 76 65 20 70 61 67  already have pag
2c6f9 65 20 31 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20  e 1 loaded into 
2c6fa 63 61 63 68 65 20 61 6e 64 20 6d 61 72 6b 65 64  cache and marked
2c6fb 20 64 69 72 74 79 2e 20 2a 2f 0a 20 20 20 20 20   dirty. */.     
2c6fc 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
2c6fd 4d 65 74 61 28 70 4d 61 69 6e 2c 20 61 43 6f 70  Meta(pMain, aCop
2c6fe 79 5b 69 5d 2c 20 26 6d 65 74 61 29 3b 0a 20 20  y[i], &meta);.  
2c6ff 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2c700 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 28  BtreeUpdateMeta(
2c701 70 54 65 6d 70 2c 20 61 43 6f 70 79 5b 69 5d 2c  pTemp, aCopy[i],
2c702 20 6d 65 74 61 2b 61 43 6f 70 79 5b 69 2b 31 5d   meta+aCopy[i+1]
2c703 29 3b 0a 20 20 20 20 20 20 69 66 28 20 4e 45 56  );.      if( NEV
2c704 45 52 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  ER(rc!=SQLITE_OK
2c705 29 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f  ) ) goto end_of_
2c706 76 61 63 75 75 6d 3b 0a 20 20 20 20 7d 0a 0a 20  vacuum;.    }.. 
2c707 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
2c708 74 72 65 65 43 6f 70 79 46 69 6c 65 28 70 4d 61  treeCopyFile(pMa
2c709 69 6e 2c 20 70 54 65 6d 70 29 3b 0a 20 20 20 20  in, pTemp);.    
2c70a 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2c70b 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f  K ) goto end_of_
2c70c 76 61 63 75 75 6d 3b 0a 20 20 20 20 72 63 20 3d  vacuum;.    rc =
2c70d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
2c70e 6d 69 74 28 70 54 65 6d 70 29 3b 0a 20 20 20 20  mit(pTemp);.    
2c70f 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2c710 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f  K ) goto end_of_
2c711 76 61 63 75 75 6d 3b 0a 23 69 66 6e 64 65 66 20  vacuum;.#ifndef 
2c712 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
2c713 56 41 43 55 55 4d 0a 20 20 20 20 73 71 6c 69 74  VACUUM.    sqlit
2c714 65 33 42 74 72 65 65 53 65 74 41 75 74 6f 56 61  e3BtreeSetAutoVa
2c715 63 75 75 6d 28 70 4d 61 69 6e 2c 20 73 71 6c 69  cuum(pMain, sqli
2c716 74 65 33 42 74 72 65 65 47 65 74 41 75 74 6f 56  te3BtreeGetAutoV
2c717 61 63 75 75 6d 28 70 54 65 6d 70 29 29 3b 0a 23  acuum(pTemp));.#
2c718 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 61 73 73  endif.  }..  ass
2c719 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
2c71a 4f 4b 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  OK );.  rc = sql
2c71b 69 74 65 33 42 74 72 65 65 53 65 74 50 61 67 65  ite3BtreeSetPage
2c71c 53 69 7a 65 28 70 4d 61 69 6e 2c 20 73 71 6c 69  Size(pMain, sqli
2c71d 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53  te3BtreeGetPageS
2c71e 69 7a 65 28 70 54 65 6d 70 29 2c 20 6e 52 65 73  ize(pTemp), nRes
2c71f 2c 31 29 3b 0a 0a 65 6e 64 5f 6f 66 5f 76 61 63  ,1);..end_of_vac
2c720 75 75 6d 3a 0a 20 20 2f 2a 20 52 65 73 74 6f 72  uum:.  /* Restor
2c721 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 76  e the original v
2c722 61 6c 75 65 20 6f 66 20 64 62 2d 3e 66 6c 61 67  alue of db->flag
2c723 73 20 2a 2f 0a 20 20 64 62 2d 3e 66 6c 61 67 73  s */.  db->flags
2c724 20 3d 20 73 61 76 65 64 5f 66 6c 61 67 73 3b 0a   = saved_flags;.
2c725 20 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 3d 20    db->nChange = 
2c726 73 61 76 65 64 5f 6e 43 68 61 6e 67 65 3b 0a 20  saved_nChange;. 
2c727 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67   db->nTotalChang
2c728 65 20 3d 20 73 61 76 65 64 5f 6e 54 6f 74 61 6c  e = saved_nTotal
2c729 43 68 61 6e 67 65 3b 0a 0a 20 20 2f 2a 20 43 75  Change;..  /* Cu
2c72a 72 72 65 6e 74 6c 79 20 74 68 65 72 65 20 69 73  rrently there is
2c72b 20 61 6e 20 53 51 4c 20 6c 65 76 65 6c 20 74 72   an SQL level tr
2c72c 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 20 6f  ansaction open o
2c72d 6e 20 74 68 65 20 76 61 63 75 75 6d 0a 20 20 2a  n the vacuum.  *
2c72e 2a 20 64 61 74 61 62 61 73 65 2e 20 4e 6f 20 6c  * database. No l
2c72f 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 20 6f 6e  ocks are held on
2c730 20 61 6e 79 20 6f 74 68 65 72 20 66 69 6c 65 73   any other files
2c731 20 28 73 69 6e 63 65 20 74 68 65 20 6d 61 69 6e   (since the main
2c732 20 66 69 6c 65 0a 20 20 2a 2a 20 77 61 73 20 63   file.  ** was c
2c733 6f 6d 6d 69 74 74 65 64 20 61 74 20 74 68 65 20  ommitted at the 
2c734 62 74 72 65 65 20 6c 65 76 65 6c 29 2e 20 53 6f  btree level). So
2c735 20 69 74 20 73 61 66 65 20 74 6f 20 65 6e 64 20   it safe to end 
2c736 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a  the transaction.
2c737 20 20 2a 2a 20 62 79 20 6d 61 6e 75 61 6c 6c 79    ** by manually
2c738 20 73 65 74 74 69 6e 67 20 74 68 65 20 61 75 74   setting the aut
2c739 6f 43 6f 6d 6d 69 74 20 66 6c 61 67 20 74 6f 20  oCommit flag to 
2c73a 74 72 75 65 20 61 6e 64 20 64 65 74 61 63 68 69  true and detachi
2c73b 6e 67 20 74 68 65 0a 20 20 2a 2a 20 76 61 63 75  ng the.  ** vacu
2c73c 75 6d 20 64 61 74 61 62 61 73 65 2e 20 54 68 65  um database. The
2c73d 20 76 61 63 75 75 6d 5f 64 62 20 6a 6f 75 72 6e   vacuum_db journ
2c73e 61 6c 20 66 69 6c 65 20 69 73 20 64 65 6c 65 74  al file is delet
2c73f 65 64 20 77 68 65 6e 20 74 68 65 20 70 61 67 65  ed when the page
2c740 72 0a 20 20 2a 2a 20 69 73 20 63 6c 6f 73 65 64  r.  ** is closed
2c741 20 62 79 20 74 68 65 20 44 45 54 41 43 48 2e 0a   by the DETACH..
2c742 20 20 2a 2f 0a 20 20 64 62 2d 3e 61 75 74 6f 43    */.  db->autoC
2c743 6f 6d 6d 69 74 20 3d 20 31 3b 0a 0a 20 20 69 66  ommit = 1;..  if
2c744 28 20 70 44 62 20 29 7b 0a 20 20 20 20 73 71 6c  ( pDb ){.    sql
2c745 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 70  ite3BtreeClose(p
2c746 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 70 44  Db->pBt);.    pD
2c747 62 2d 3e 70 42 74 20 3d 20 30 3b 0a 20 20 20 20  b->pBt = 0;.    
2c748 70 44 62 2d 3e 70 53 63 68 65 6d 61 20 3d 20 30  pDb->pSchema = 0
2c749 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33  ;.  }..  sqlite3
2c74a 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68  ResetInternalSch
2c74b 65 6d 61 28 64 62 2c 20 30 29 3b 0a 0a 20 20 72  ema(db, 0);..  r
2c74c 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64  eturn rc;.}.#end
2c74d 69 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  if  /* SQLITE_OM
2c74e 49 54 5f 56 41 43 55 55 4d 20 26 26 20 53 51 4c  IT_VACUUM && SQL
2c74f 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48 20  ITE_OMIT_ATTACH 
2c750 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
2c751 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 61 63 75 75  *** End of vacuu
2c752 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  m.c ************
2c753 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c754 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c755 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
2c756 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76  *** Begin file v
2c757 74 61 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tab.c **********
2c758 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c759 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c75a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a  **/./*.** 2006 J
2c75b 75 6e 65 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65  une 10.**.** The
2c75c 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
2c75d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
2c75e 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
2c75f 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
2c760 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
2c761 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
2c762 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
2c763 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
2c764 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
2c765 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
2c766 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
2c767 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
2c768 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
2c769 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
2c76a 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
2c76b 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
2c76c 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
2c76d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c76e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c76f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c771 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
2c772 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
2c773 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 69 6d   used to help im
2c774 70 6c 65 6d 65 6e 74 20 76 69 72 74 75 61 6c 20  plement virtual 
2c775 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49  tables..**.** $I
2c776 64 3a 20 76 74 61 62 2e 63 2c 76 20 31 2e 39 34  d: vtab.c,v 1.94
2c777 20 32 30 30 39 2f 30 38 2f 30 38 20 31 38 3a 30   2009/08/08 18:0
2c778 31 3a 30 38 20 64 72 68 20 45 78 70 20 24 0a 2a  1:08 drh Exp $.*
2c779 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
2c77a 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2c77b 4c 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 63  LE../*.** The ac
2c77c 74 75 61 6c 20 66 75 6e 63 74 69 6f 6e 20 74 68  tual function th
2c77d 61 74 20 64 6f 65 73 20 74 68 65 20 77 6f 72 6b  at does the work
2c77e 20 6f 66 20 63 72 65 61 74 69 6e 67 20 61 20 6e   of creating a n
2c77f 65 77 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 20 54 68  ew module..** Th
2c780 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c  is function impl
2c781 65 6d 65 6e 74 73 20 74 68 65 20 73 71 6c 69 74  ements the sqlit
2c782 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
2c783 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65  () and.** sqlite
2c784 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f  3_create_module_
2c785 76 32 28 29 20 69 6e 74 65 72 66 61 63 65 73 2e  v2() interfaces.
2c786 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
2c787 72 65 61 74 65 4d 6f 64 75 6c 65 28 0a 20 20 73  reateModule(.  s
2c788 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
2c789 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2c78a 2a 20 44 61 74 61 62 61 73 65 20 69 6e 20 77 68  * Database in wh
2c78b 69 63 68 20 6d 6f 64 75 6c 65 20 69 73 20 72 65  ich module is re
2c78c 67 69 73 74 65 72 65 64 20 2a 2f 0a 20 20 63 6f  gistered */.  co
2c78d 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
2c78e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2c78f 20 4e 61 6d 65 20 61 73 73 69 67 6e 65 64 20 74   Name assigned t
2c790 6f 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 2a 2f  o this module */
2c791 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
2c792 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65  _module *pModule
2c793 2c 20 20 2f 2a 20 54 68 65 20 64 65 66 69 6e 69  ,  /* The defini
2c794 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 64 75  tion of the modu
2c795 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  le */.  void *pA
2c796 75 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ux,             
2c797 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65          /* Conte
2c798 78 74 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 78  xt pointer for x
2c799 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20  Create/xConnect 
2c79a 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 73  */.  void (*xDes
2c79b 74 72 6f 79 29 28 76 6f 69 64 20 2a 29 20 20 20  troy)(void *)   
2c79c 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64       /* Module d
2c79d 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69  estructor functi
2c79e 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  on */.){.  int r
2c79f 63 2c 20 6e 4e 61 6d 65 3b 0a 20 20 4d 6f 64 75  c, nName;.  Modu
2c7a0 6c 65 20 2a 70 4d 6f 64 3b 0a 0a 20 20 73 71 6c  le *pMod;..  sql
2c7a1 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
2c7a2 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 6e  (db->mutex);.  n
2c7a3 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74  Name = sqlite3St
2c7a4 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20  rlen30(zName);. 
2c7a5 20 70 4d 6f 64 20 3d 20 28 4d 6f 64 75 6c 65 20   pMod = (Module 
2c7a6 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  *)sqlite3DbMallo
2c7a7 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28  cRaw(db, sizeof(
2c7a8 4d 6f 64 75 6c 65 29 20 2b 20 6e 4e 61 6d 65 20  Module) + nName 
2c7a9 2b 20 31 29 3b 0a 20 20 69 66 28 20 70 4d 6f 64  + 1);.  if( pMod
2c7aa 20 29 7b 0a 20 20 20 20 4d 6f 64 75 6c 65 20 2a   ){.    Module *
2c7ab 70 44 65 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a  pDel;.    char *
2c7ac 7a 43 6f 70 79 20 3d 20 28 63 68 61 72 20 2a 29  zCopy = (char *)
2c7ad 28 26 70 4d 6f 64 5b 31 5d 29 3b 0a 20 20 20 20  (&pMod[1]);.    
2c7ae 6d 65 6d 63 70 79 28 7a 43 6f 70 79 2c 20 7a 4e  memcpy(zCopy, zN
2c7af 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b 0a 20  ame, nName+1);. 
2c7b0 20 20 20 70 4d 6f 64 2d 3e 7a 4e 61 6d 65 20 3d     pMod->zName =
2c7b1 20 7a 43 6f 70 79 3b 0a 20 20 20 20 70 4d 6f 64   zCopy;.    pMod
2c7b2 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 4d 6f 64  ->pModule = pMod
2c7b3 75 6c 65 3b 0a 20 20 20 20 70 4d 6f 64 2d 3e 70  ule;.    pMod->p
2c7b4 41 75 78 20 3d 20 70 41 75 78 3b 0a 20 20 20 20  Aux = pAux;.    
2c7b5 70 4d 6f 64 2d 3e 78 44 65 73 74 72 6f 79 20 3d  pMod->xDestroy =
2c7b6 20 78 44 65 73 74 72 6f 79 3b 0a 20 20 20 20 70   xDestroy;.    p
2c7b7 44 65 6c 20 3d 20 28 4d 6f 64 75 6c 65 20 2a 29  Del = (Module *)
2c7b8 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72  sqlite3HashInser
2c7b9 74 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65 2c 20  t(&db->aModule, 
2c7ba 7a 43 6f 70 79 2c 20 6e 4e 61 6d 65 2c 20 28 76  zCopy, nName, (v
2c7bb 6f 69 64 2a 29 70 4d 6f 64 29 3b 0a 20 20 20 20  oid*)pMod);.    
2c7bc 69 66 28 20 70 44 65 6c 20 26 26 20 70 44 65 6c  if( pDel && pDel
2c7bd 2d 3e 78 44 65 73 74 72 6f 79 20 29 7b 0a 20 20  ->xDestroy ){.  
2c7be 20 20 20 20 70 44 65 6c 2d 3e 78 44 65 73 74 72      pDel->xDestr
2c7bf 6f 79 28 70 44 65 6c 2d 3e 70 41 75 78 29 3b 0a  oy(pDel->pAux);.
2c7c0 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2c7c1 33 44 62 46 72 65 65 28 64 62 2c 20 70 44 65 6c  3DbFree(db, pDel
2c7c2 29 3b 0a 20 20 20 20 69 66 28 20 70 44 65 6c 3d  );.    if( pDel=
2c7c3 3d 70 4d 6f 64 20 29 7b 0a 20 20 20 20 20 20 64  =pMod ){.      d
2c7c4 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2c7c5 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  = 1;.    }.    s
2c7c6 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
2c7c7 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29  nalSchema(db, 0)
2c7c8 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 78 44  ;.  }else if( xD
2c7c9 65 73 74 72 6f 79 20 29 7b 0a 20 20 20 20 78 44  estroy ){.    xD
2c7ca 65 73 74 72 6f 79 28 70 41 75 78 29 3b 0a 20 20  estroy(pAux);.  
2c7cb 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  }.  rc = sqlite3
2c7cc 41 70 69 45 78 69 74 28 64 62 2c 20 53 51 4c 49  ApiExit(db, SQLI
2c7cd 54 45 5f 4f 4b 29 3b 0a 20 20 73 71 6c 69 74 65  TE_OK);.  sqlite
2c7ce 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
2c7cf 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
2c7d0 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  rn rc;.}.../*.**
2c7d1 20 45 78 74 65 72 6e 61 6c 20 41 50 49 20 66 75   External API fu
2c7d2 6e 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 63  nction used to c
2c7d3 72 65 61 74 65 20 61 20 6e 65 77 20 76 69 72 74  reate a new virt
2c7d4 75 61 6c 2d 74 61 62 6c 65 20 6d 6f 64 75 6c 65  ual-table module
2c7d5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2c7d6 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
2c7d7 74 65 5f 6d 6f 64 75 6c 65 28 0a 20 20 73 71 6c  te_module(.  sql
2c7d8 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
2c7d9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c7da 44 61 74 61 62 61 73 65 20 69 6e 20 77 68 69 63  Database in whic
2c7db 68 20 6d 6f 64 75 6c 65 20 69 73 20 72 65 67 69  h module is regi
2c7dc 73 74 65 72 65 64 20 2a 2f 0a 20 20 63 6f 6e 73  stered */.  cons
2c7dd 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20  t char *zName,  
2c7de 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2c7df 61 6d 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20  ame assigned to 
2c7e0 74 68 69 73 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20  this module */. 
2c7e1 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
2c7e2 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 2c 20  odule *pModule, 
2c7e3 20 2f 2a 20 54 68 65 20 64 65 66 69 6e 69 74 69   /* The definiti
2c7e4 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65  on of the module
2c7e5 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78   */.  void *pAux
2c7e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c7e7 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74        /* Context
2c7e8 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 78 43 72   pointer for xCr
2c7e9 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f  eate/xConnect */
2c7ea 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 72 65  .){.  return cre
2c7eb 61 74 65 4d 6f 64 75 6c 65 28 64 62 2c 20 7a 4e  ateModule(db, zN
2c7ec 61 6d 65 2c 20 70 4d 6f 64 75 6c 65 2c 20 70 41  ame, pModule, pA
2c7ed 75 78 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ux, 0);.}../*.**
2c7ee 20 45 78 74 65 72 6e 61 6c 20 41 50 49 20 66 75   External API fu
2c7ef 6e 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 63  nction used to c
2c7f0 72 65 61 74 65 20 61 20 6e 65 77 20 76 69 72 74  reate a new virt
2c7f1 75 61 6c 2d 74 61 62 6c 65 20 6d 6f 64 75 6c 65  ual-table module
2c7f2 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2c7f3 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
2c7f4 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 0a 20 20  te_module_v2(.  
2c7f5 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
2c7f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c7f7 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6e 20 77  /* Database in w
2c7f8 68 69 63 68 20 6d 6f 64 75 6c 65 20 69 73 20 72  hich module is r
2c7f9 65 67 69 73 74 65 72 65 64 20 2a 2f 0a 20 20 63  egistered */.  c
2c7fa 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2c7fb 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
2c7fc 2a 20 4e 61 6d 65 20 61 73 73 69 67 6e 65 64 20  * Name assigned 
2c7fd 74 6f 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 2a  to this module *
2c7fe 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  /.  const sqlite
2c7ff 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  3_module *pModul
2c800 65 2c 20 20 2f 2a 20 54 68 65 20 64 65 66 69 6e  e,  /* The defin
2c801 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 64  ition of the mod
2c802 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ule */.  void *p
2c803 41 75 78 2c 20 20 20 20 20 20 20 20 20 20 20 20  Aux,            
2c804 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74           /* Cont
2c805 65 78 74 20 70 6f 69 6e 74 65 72 20 66 6f 72 20  ext pointer for 
2c806 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74  xCreate/xConnect
2c807 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65   */.  void (*xDe
2c808 73 74 72 6f 79 29 28 76 6f 69 64 20 2a 29 20 20  stroy)(void *)  
2c809 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20        /* Module 
2c80a 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74  destructor funct
2c80b 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 72 65 74 75  ion */.){.  retu
2c80c 72 6e 20 63 72 65 61 74 65 4d 6f 64 75 6c 65 28  rn createModule(
2c80d 64 62 2c 20 7a 4e 61 6d 65 2c 20 70 4d 6f 64 75  db, zName, pModu
2c80e 6c 65 2c 20 70 41 75 78 2c 20 78 44 65 73 74 72  le, pAux, xDestr
2c80f 6f 79 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f  oy);.}../*.** Lo
2c810 63 6b 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  ck the virtual t
2c811 61 62 6c 65 20 73 6f 20 74 68 61 74 20 69 74 20  able so that it 
2c812 63 61 6e 6e 6f 74 20 62 65 20 64 69 73 63 6f 6e  cannot be discon
2c813 6e 65 63 74 65 64 2e 0a 2a 2a 20 4c 6f 63 6b 73  nected..** Locks
2c814 20 6e 65 73 74 2e 20 20 45 76 65 72 79 20 6c 6f   nest.  Every lo
2c815 63 6b 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61  ck should have a
2c816 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 75   corresponding u
2c817 6e 6c 6f 63 6b 2e 0a 2a 2a 20 49 66 20 61 6e 20  nlock..** If an 
2c818 75 6e 6c 6f 63 6b 20 69 73 20 6f 6d 69 74 74 65  unlock is omitte
2c819 64 2c 20 72 65 73 6f 75 72 63 65 73 20 6c 65 61  d, resources lea
2c81a 6b 73 20 77 69 6c 6c 20 6f 63 63 75 72 2e 20 20  ks will occur.  
2c81b 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 64 69 73 63  .**.** If a disc
2c81c 6f 6e 6e 65 63 74 20 69 73 20 61 74 74 65 6d 70  onnect is attemp
2c81d 74 65 64 20 77 68 69 6c 65 20 61 20 76 69 72 74  ted while a virt
2c81e 75 61 6c 20 74 61 62 6c 65 20 69 73 20 6c 6f 63  ual table is loc
2c81f 6b 65 64 2c 0a 2a 2a 20 74 68 65 20 64 69 73 63  ked,.** the disc
2c820 6f 6e 6e 65 63 74 20 69 73 20 64 65 66 65 72 72  onnect is deferr
2c821 65 64 20 75 6e 74 69 6c 20 61 6c 6c 20 6c 6f 63  ed until all loc
2c822 6b 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 6d  ks have been rem
2c823 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  oved..*/.SQLITE_
2c824 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2c825 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 56 54 61  ite3VtabLock(VTa
2c826 62 6c 65 20 2a 70 56 54 61 62 29 7b 0a 20 20 70  ble *pVTab){.  p
2c827 56 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a  VTab->nRef++;.}.
2c828 0a 0a 2f 2a 0a 2a 2a 20 70 54 61 62 20 69 73 20  ../*.** pTab is 
2c829 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54  a pointer to a T
2c82a 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 72  able structure r
2c82b 65 70 72 65 73 65 6e 74 69 6e 67 20 61 20 76 69  epresenting a vi
2c82c 72 74 75 61 6c 2d 74 61 62 6c 65 2e 0a 2a 2a 20  rtual-table..** 
2c82d 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
2c82e 20 74 6f 20 74 68 65 20 56 54 61 62 6c 65 20 6f   to the VTable o
2c82f 62 6a 65 63 74 20 75 73 65 64 20 62 79 20 63 6f  bject used by co
2c830 6e 6e 65 63 74 69 6f 6e 20 64 62 20 74 6f 20 61  nnection db to a
2c831 63 63 65 73 73 20 0a 2a 2a 20 74 68 69 73 20 76  ccess .** this v
2c832 69 72 74 75 61 6c 2d 74 61 62 6c 65 2c 20 69 66  irtual-table, if
2c833 20 6f 6e 65 20 68 61 73 20 62 65 65 6e 20 63 72   one has been cr
2c834 65 61 74 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 6f  eated, or NULL o
2c835 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c  therwise..*/.SQL
2c836 49 54 45 5f 50 52 49 56 41 54 45 20 56 54 61 62  ITE_PRIVATE VTab
2c837 6c 65 20 2a 73 71 6c 69 74 65 33 47 65 74 56 54  le *sqlite3GetVT
2c838 61 62 6c 65 28 73 71 6c 69 74 65 33 20 2a 64 62  able(sqlite3 *db
2c839 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a  , Table *pTab){.
2c83a 20 20 56 54 61 62 6c 65 20 2a 70 56 74 61 62 3b    VTable *pVtab;
2c83b 0a 20 20 61 73 73 65 72 74 28 20 49 73 56 69 72  .  assert( IsVir
2c83c 74 75 61 6c 28 70 54 61 62 29 20 29 3b 0a 20 20  tual(pTab) );.  
2c83d 66 6f 72 28 70 56 74 61 62 3d 70 54 61 62 2d 3e  for(pVtab=pTab->
2c83e 70 56 54 61 62 6c 65 3b 20 70 56 74 61 62 20 26  pVTable; pVtab &
2c83f 26 20 70 56 74 61 62 2d 3e 64 62 21 3d 64 62 3b  & pVtab->db!=db;
2c840 20 70 56 74 61 62 3d 70 56 74 61 62 2d 3e 70 4e   pVtab=pVtab->pN
2c841 65 78 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  ext);.  return p
2c842 56 74 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  Vtab;.}../*.** D
2c843 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66  ecrement the ref
2c844 2d 63 6f 75 6e 74 20 6f 6e 20 61 20 76 69 72 74  -count on a virt
2c845 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74  ual table object
2c846 2e 20 57 68 65 6e 20 74 68 65 20 72 65 66 2d 63  . When the ref-c
2c847 6f 75 6e 74 0a 2a 2a 20 72 65 61 63 68 65 73 20  ount.** reaches 
2c848 7a 65 72 6f 2c 20 63 61 6c 6c 20 74 68 65 20 78  zero, call the x
2c849 44 69 73 63 6f 6e 6e 65 63 74 28 29 20 6d 65 74  Disconnect() met
2c84a 68 6f 64 20 74 6f 20 64 65 6c 65 74 65 20 74 68  hod to delete th
2c84b 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c  e object..*/.SQL
2c84c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2c84d 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f   sqlite3VtabUnlo
2c84e 63 6b 28 56 54 61 62 6c 65 20 2a 70 56 54 61 62  ck(VTable *pVTab
2c84f 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
2c850 20 3d 20 70 56 54 61 62 2d 3e 64 62 3b 0a 0a 20   = pVTab->db;.. 
2c851 20 61 73 73 65 72 74 28 20 64 62 20 29 3b 0a 20   assert( db );. 
2c852 20 61 73 73 65 72 74 28 20 70 56 54 61 62 2d 3e   assert( pVTab->
2c853 6e 52 65 66 3e 30 20 29 3b 0a 20 20 61 73 73 65  nRef>0 );.  asse
2c854 72 74 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  rt( sqlite3Safet
2c855 79 43 68 65 63 6b 4f 6b 28 64 62 29 20 29 3b 0a  yCheckOk(db) );.
2c856 0a 20 20 70 56 54 61 62 2d 3e 6e 52 65 66 2d 2d  .  pVTab->nRef--
2c857 3b 0a 20 20 69 66 28 20 70 56 54 61 62 2d 3e 6e  ;.  if( pVTab->n
2c858 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  Ref==0 ){.    sq
2c859 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 20 3d 20  lite3_vtab *p = 
2c85a 70 56 54 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20  pVTab->pVtab;.  
2c85b 20 20 69 66 28 20 70 20 29 7b 0a 23 69 66 64 65    if( p ){.#ifde
2c85c 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
2c85d 20 20 20 20 20 69 66 28 20 70 56 54 61 62 2d 3e       if( pVTab->
2c85e 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54  db->magic==SQLIT
2c85f 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 7b 0a  E_MAGIC_BUSY ){.
2c860 20 20 20 20 20 20 20 20 28 76 6f 69 64 29 73 71          (void)sq
2c861 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
2c862 62 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70  b);.        p->p
2c863 4d 6f 64 75 6c 65 2d 3e 78 44 69 73 63 6f 6e 6e  Module->xDisconn
2c864 65 63 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20  ect(p);.        
2c865 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
2c866 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 20  etyOn(db);.     
2c867 20 7d 20 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20   } else.#endif. 
2c868 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70       {.        p
2c869 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44 69 73 63  ->pModule->xDisc
2c86a 6f 6e 6e 65 63 74 28 70 29 3b 0a 20 20 20 20 20  onnect(p);.     
2c86b 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c   }.    }.    sql
2c86c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2c86d 56 54 61 62 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  VTab);.  }.}../*
2c86e 0a 2a 2a 20 54 61 62 6c 65 20 70 20 69 73 20 61  .** Table p is a
2c86f 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20   virtual table. 
2c870 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 6f  This function mo
2c871 76 65 73 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73  ves all elements
2c872 20 69 6e 20 74 68 65 0a 2a 2a 20 70 2d 3e 70 56   in the.** p->pV
2c873 54 61 62 6c 65 20 6c 69 73 74 20 74 6f 20 74 68  Table list to th
2c874 65 20 73 71 6c 69 74 65 33 2e 70 44 69 73 63 6f  e sqlite3.pDisco
2c875 6e 6e 65 63 74 20 6c 69 73 74 73 20 6f 66 20 74  nnect lists of t
2c876 68 65 69 72 20 61 73 73 6f 63 69 61 74 65 64 0a  heir associated.
2c877 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ** database conn
2c878 65 63 74 69 6f 6e 73 20 74 6f 20 62 65 20 64 69  ections to be di
2c879 73 63 6f 6e 6e 65 63 74 65 64 20 61 74 20 74 68  sconnected at th
2c87a 65 20 6e 65 78 74 20 6f 70 70 6f 72 74 75 6e 69  e next opportuni
2c87b 74 79 2e 20 0a 2a 2a 20 45 78 63 65 70 74 2c 20  ty. .** Except, 
2c87c 69 66 20 61 72 67 75 6d 65 6e 74 20 64 62 20 69  if argument db i
2c87d 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
2c87e 20 74 68 65 20 65 6e 74 72 79 20 61 73 73 6f 63   the entry assoc
2c87f 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 63 6f  iated with.** co
2c880 6e 6e 65 63 74 69 6f 6e 20 64 62 20 69 73 20 6c  nnection db is l
2c881 65 66 74 20 69 6e 20 74 68 65 20 70 2d 3e 70 56  eft in the p->pV
2c882 54 61 62 6c 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73  Table list..*/.s
2c883 74 61 74 69 63 20 56 54 61 62 6c 65 20 2a 76 74  tatic VTable *vt
2c884 61 62 44 69 73 63 6f 6e 6e 65 63 74 41 6c 6c 28  abDisconnectAll(
2c885 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 61 62  sqlite3 *db, Tab
2c886 6c 65 20 2a 70 29 7b 0a 20 20 56 54 61 62 6c 65  le *p){.  VTable
2c887 20 2a 70 52 65 74 20 3d 20 30 3b 0a 20 20 56 54   *pRet = 0;.  VT
2c888 61 62 6c 65 20 2a 70 56 54 61 62 6c 65 20 3d 20  able *pVTable = 
2c889 70 2d 3e 70 56 54 61 62 6c 65 3b 0a 20 20 70 2d  p->pVTable;.  p-
2c88a 3e 70 56 54 61 62 6c 65 20 3d 20 30 3b 0a 0a 20  >pVTable = 0;.. 
2c88b 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20   /* Assert that 
2c88c 74 68 65 20 6d 75 74 65 78 20 28 69 66 20 61 6e  the mutex (if an
2c88d 79 29 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  y) associated wi
2c88e 74 68 20 74 68 65 20 42 74 53 68 61 72 65 64 20  th the BtShared 
2c88f 64 61 74 61 62 61 73 65 20 0a 20 20 2a 2a 20 74  database .  ** t
2c890 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 61 62  hat contains tab
2c891 6c 65 20 70 20 69 73 20 68 65 6c 64 20 62 79 20  le p is held by 
2c892 74 68 65 20 63 61 6c 6c 65 72 2e 20 53 65 65 20  the caller. See 
2c893 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 73 20  header comments 
2c894 0a 20 20 2a 2a 20 61 62 6f 76 65 20 66 75 6e 63  .  ** above func
2c895 74 69 6f 6e 20 73 71 6c 69 74 65 33 56 74 61 62  tion sqlite3Vtab
2c896 55 6e 6c 6f 63 6b 4c 69 73 74 28 29 20 66 6f 72  UnlockList() for
2c897 20 61 6e 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20   an explanation 
2c898 6f 66 20 77 68 79 0a 20 20 2a 2a 20 74 68 69 73  of why.  ** this
2c899 20 6d 61 6b 65 73 20 69 74 20 73 61 66 65 20 74   makes it safe t
2c89a 6f 20 61 63 63 65 73 73 20 74 68 65 20 73 71 6c  o access the sql
2c89b 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74  ite3.pDisconnect
2c89c 20 6c 69 73 74 20 6f 66 20 61 6e 79 0a 20 20 2a   list of any.  *
2c89d 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * database conne
2c89e 63 74 69 6f 6e 20 74 68 61 74 20 6d 61 79 20 68  ction that may h
2c89f 61 76 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 20  ave an entry in 
2c8a0 74 68 65 20 70 2d 3e 70 56 54 61 62 6c 65 20 6c  the p->pVTable l
2c8a1 69 73 74 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72  ist.  */.  asser
2c8a2 74 28 20 64 62 3d 3d 30 20 7c 7c 0a 20 20 20 20  t( db==0 ||.    
2c8a3 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
2c8a4 73 4d 75 74 65 78 28 64 62 2d 3e 61 44 62 5b 73  sMutex(db->aDb[s
2c8a5 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
2c8a6 64 65 78 28 64 62 2c 20 70 2d 3e 70 53 63 68 65  dex(db, p->pSche
2c8a7 6d 61 29 5d 2e 70 42 74 29 20 0a 20 20 29 3b 0a  ma)].pBt) .  );.
2c8a8 0a 20 20 77 68 69 6c 65 28 20 70 56 54 61 62 6c  .  while( pVTabl
2c8a9 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  e ){.    sqlite3
2c8aa 20 2a 64 62 32 20 3d 20 70 56 54 61 62 6c 65 2d   *db2 = pVTable-
2c8ab 3e 64 62 3b 0a 20 20 20 20 56 54 61 62 6c 65 20  >db;.    VTable 
2c8ac 2a 70 4e 65 78 74 20 3d 20 70 56 54 61 62 6c 65  *pNext = pVTable
2c8ad 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 61 73 73  ->pNext;.    ass
2c8ae 65 72 74 28 20 64 62 32 20 29 3b 0a 20 20 20 20  ert( db2 );.    
2c8af 69 66 28 20 64 62 32 3d 3d 64 62 20 29 7b 0a 20  if( db2==db ){. 
2c8b0 20 20 20 20 20 70 52 65 74 20 3d 20 70 56 54 61       pRet = pVTa
2c8b1 62 6c 65 3b 0a 20 20 20 20 20 20 70 2d 3e 70 56  ble;.      p->pV
2c8b2 54 61 62 6c 65 20 3d 20 70 52 65 74 3b 0a 20 20  Table = pRet;.  
2c8b3 20 20 20 20 70 52 65 74 2d 3e 70 4e 65 78 74 20      pRet->pNext 
2c8b4 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = 0;.    }else{.
2c8b5 20 20 20 20 20 20 70 56 54 61 62 6c 65 2d 3e 70        pVTable->p
2c8b6 4e 65 78 74 20 3d 20 64 62 32 2d 3e 70 44 69 73  Next = db2->pDis
2c8b7 63 6f 6e 6e 65 63 74 3b 0a 20 20 20 20 20 20 64  connect;.      d
2c8b8 62 32 2d 3e 70 44 69 73 63 6f 6e 6e 65 63 74 20  b2->pDisconnect 
2c8b9 3d 20 70 56 54 61 62 6c 65 3b 0a 20 20 20 20 7d  = pVTable;.    }
2c8ba 0a 20 20 20 20 70 56 54 61 62 6c 65 20 3d 20 70  .    pVTable = p
2c8bb 4e 65 78 74 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  Next;.  }..  ass
2c8bc 65 72 74 28 20 21 64 62 20 7c 7c 20 70 52 65 74  ert( !db || pRet
2c8bd 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65   );.  return pRe
2c8be 74 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 69 73  t;.}.../*.** Dis
2c8bf 63 6f 6e 6e 65 63 74 20 61 6c 6c 20 74 68 65 20  connect all the 
2c8c0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 62  virtual table ob
2c8c1 6a 65 63 74 73 20 69 6e 20 74 68 65 20 73 71 6c  jects in the sql
2c8c2 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74  ite3.pDisconnect
2c8c3 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   list..**.** Thi
2c8c4 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f  s function may o
2c8c5 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 77 68  nly be called wh
2c8c6 65 6e 20 74 68 65 20 6d 75 74 65 78 65 73 20 61  en the mutexes a
2c8c7 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
2c8c8 6c 6c 0a 2a 2a 20 73 68 61 72 65 64 20 62 2d 74  ll.** shared b-t
2c8c9 72 65 65 20 64 61 74 61 62 61 73 65 73 20 6f 70  ree databases op
2c8ca 65 6e 65 64 20 75 73 69 6e 67 20 63 6f 6e 6e 65  ened using conne
2c8cb 63 74 69 6f 6e 20 64 62 20 61 72 65 20 68 65 6c  ction db are hel
2c8cc 64 20 62 79 20 74 68 65 20 0a 2a 2a 20 63 61 6c  d by the .** cal
2c8cd 6c 65 72 2e 20 54 68 69 73 20 69 73 20 64 6f 6e  ler. This is don
2c8ce 65 20 74 6f 20 70 72 6f 74 65 63 74 20 74 68 65  e to protect the
2c8cf 20 73 71 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e   sqlite3.pDiscon
2c8d0 6e 65 63 74 20 6c 69 73 74 2e 20 54 68 65 0a 2a  nect list. The.*
2c8d1 2a 20 73 71 6c 69 74 65 33 2e 70 44 69 73 63 6f  * sqlite3.pDisco
2c8d2 6e 6e 65 63 74 20 6c 69 73 74 20 69 73 20 61 63  nnect list is ac
2c8d3 63 65 73 73 65 64 20 6f 6e 6c 79 20 61 73 20 66  cessed only as f
2c8d4 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ollows:.**.**   
2c8d5 31 29 20 42 79 20 74 68 69 73 20 66 75 6e 63 74  1) By this funct
2c8d6 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ion. In this cas
2c8d7 65 2c 20 61 6c 6c 20 42 74 53 68 61 72 65 64 20  e, all BtShared 
2c8d8 6d 75 74 65 78 65 73 20 61 6e 64 20 74 68 65 20  mutexes and the 
2c8d9 6d 75 74 65 78 0a 2a 2a 20 20 20 20 20 20 61 73  mutex.**      as
2c8da 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
2c8db 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
2c8dc 65 20 69 74 73 65 6c 66 20 6d 75 73 74 20 62 65  e itself must be
2c8dd 20 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32   held..**.**   2
2c8de 29 20 42 79 20 66 75 6e 63 74 69 6f 6e 20 76 74  ) By function vt
2c8df 61 62 44 69 73 63 6f 6e 6e 65 63 74 41 6c 6c 28  abDisconnectAll(
2c8e0 29 2c 20 77 68 65 6e 20 69 74 20 61 64 64 73 20  ), when it adds 
2c8e1 61 20 56 54 61 62 6c 65 20 65 6e 74 72 79 20 74  a VTable entry t
2c8e2 6f 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 73 71  o.**      the sq
2c8e3 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63  lite3.pDisconnec
2c8e4 74 20 6c 69 73 74 2e 20 49 6e 20 74 68 69 73 20  t list. In this 
2c8e5 63 61 73 65 20 65 69 74 68 65 72 20 74 68 65 20  case either the 
2c8e6 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 0a 2a  BtShared mutex.*
2c8e7 2a 20 20 20 20 20 20 61 73 73 6f 63 69 61 74 65  *      associate
2c8e8 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62  d with the datab
2c8e9 61 73 65 20 74 68 65 20 76 69 72 74 75 61 6c 20  ase the virtual 
2c8ea 74 61 62 6c 65 20 69 73 20 73 74 6f 72 65 64 20  table is stored 
2c8eb 69 6e 20 69 73 20 68 65 6c 64 0a 2a 2a 20 20 20  in is held.**   
2c8ec 20 20 20 6f 72 2c 20 69 66 20 74 68 65 20 76 69     or, if the vi
2c8ed 72 74 75 61 6c 20 74 61 62 6c 65 20 69 73 20 73  rtual table is s
2c8ee 74 6f 72 65 64 20 69 6e 20 61 20 6e 6f 6e 2d 73  tored in a non-s
2c8ef 68 61 72 61 62 6c 65 20 64 61 74 61 62 61 73 65  harable database
2c8f0 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 74  , then.**      t
2c8f1 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
2c8f2 6c 65 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64  le mutex is held
2c8f3 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 61 20 72 65 73  ..**.** As a res
2c8f4 75 6c 74 2c 20 61 20 73 71 6c 69 74 65 33 2e 70  ult, a sqlite3.p
2c8f5 44 69 73 63 6f 6e 6e 65 63 74 20 63 61 6e 6e 6f  Disconnect canno
2c8f6 74 20 62 65 20 61 63 63 65 73 73 65 64 20 73 69  t be accessed si
2c8f7 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 20 0a 2a 2a  multaneously .**
2c8f8 20 62 79 20 6d 75 6c 74 69 70 6c 65 20 74 68 72   by multiple thr
2c8f9 65 61 64 73 2e 20 49 74 20 69 73 20 74 68 72 65  eads. It is thre
2c8fa 61 64 2d 73 61 66 65 2e 0a 2a 2f 0a 53 51 4c 49  ad-safe..*/.SQLI
2c8fb 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2c8fc 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63  sqlite3VtabUnloc
2c8fd 6b 4c 69 73 74 28 73 71 6c 69 74 65 33 20 2a 64  kList(sqlite3 *d
2c8fe 62 29 7b 0a 20 20 56 54 61 62 6c 65 20 2a 70 20  b){.  VTable *p 
2c8ff 3d 20 64 62 2d 3e 70 44 69 73 63 6f 6e 6e 65 63  = db->pDisconnec
2c900 74 3b 0a 20 20 64 62 2d 3e 70 44 69 73 63 6f 6e  t;.  db->pDiscon
2c901 6e 65 63 74 20 3d 20 30 3b 0a 0a 20 20 61 73 73  nect = 0;..  ass
2c902 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
2c903 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73  eHoldsAllMutexes
2c904 28 64 62 29 20 29 3b 0a 20 20 61 73 73 65 72 74  (db) );.  assert
2c905 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
2c906 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
2c907 29 3b 0a 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  );..  if( p ){. 
2c908 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65     sqlite3Expire
2c909 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
2c90a 74 73 28 64 62 29 3b 0a 20 20 20 20 64 6f 20 7b  ts(db);.    do {
2c90b 0a 20 20 20 20 20 20 56 54 61 62 6c 65 20 2a 70  .      VTable *p
2c90c 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b  Next = p->pNext;
2c90d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 74  .      sqlite3Vt
2c90e 61 62 55 6e 6c 6f 63 6b 28 70 29 3b 0a 20 20 20  abUnlock(p);.   
2c90f 20 20 20 70 20 3d 20 70 4e 65 78 74 3b 0a 20 20     p = pNext;.  
2c910 20 20 7d 77 68 69 6c 65 28 20 70 20 29 3b 0a 20    }while( p );. 
2c911 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61   }.}../*.** Clea
2c912 72 20 61 6e 79 20 61 6e 64 20 61 6c 6c 20 76 69  r any and all vi
2c913 72 74 75 61 6c 2d 74 61 62 6c 65 20 69 6e 66 6f  rtual-table info
2c914 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65  rmation from the
2c915 20 54 61 62 6c 65 20 72 65 63 6f 72 64 2e 0a 2a   Table record..*
2c916 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
2c917 73 20 63 61 6c 6c 65 64 2c 20 66 6f 72 20 65 78  s called, for ex
2c918 61 6d 70 6c 65 2c 20 6a 75 73 74 20 62 65 66 6f  ample, just befo
2c919 72 65 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20  re deleting the 
2c91a 54 61 62 6c 65 0a 2a 2a 20 72 65 63 6f 72 64 2e  Table.** record.
2c91b 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 69 74 20  .**.** Since it 
2c91c 69 73 20 61 20 76 69 72 74 75 61 6c 2d 74 61 62  is a virtual-tab
2c91d 6c 65 2c 20 74 68 65 20 54 61 62 6c 65 20 73 74  le, the Table st
2c91e 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73  ructure contains
2c91f 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f   a pointer.** to
2c920 20 74 68 65 20 68 65 61 64 20 6f 66 20 61 20 6c   the head of a l
2c921 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 56 54  inked list of VT
2c922 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 73 2e  able structures.
2c923 20 45 61 63 68 20 56 54 61 62 6c 65 20 0a 2a 2a   Each VTable .**
2c924 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 73   structure is as
2c925 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
2c926 73 69 6e 67 6c 65 20 73 71 6c 69 74 65 33 2a 20  single sqlite3* 
2c927 75 73 65 72 20 6f 66 20 74 68 65 20 73 63 68 65  user of the sche
2c928 6d 61 2e 0a 2a 2a 20 54 68 65 20 72 65 66 65 72  ma..** The refer
2c929 65 6e 63 65 20 63 6f 75 6e 74 20 6f 66 20 74 68  ence count of th
2c92a 65 20 56 54 61 62 6c 65 20 73 74 72 75 63 74 75  e VTable structu
2c92b 72 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  re associated wi
2c92c 74 68 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20  th database .** 
2c92d 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 69 73  connection db is
2c92e 20 64 65 63 72 65 6d 65 6e 74 65 64 20 69 6d 6d   decremented imm
2c92f 65 64 69 61 74 65 6c 79 20 28 77 68 69 63 68 20  ediately (which 
2c930 6d 61 79 20 6c 65 61 64 20 74 6f 20 74 68 65 20  may lead to the 
2c931 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 62 65  .** structure be
2c932 69 6e 67 20 78 44 69 73 63 6f 6e 6e 65 63 74 65  ing xDisconnecte
2c933 64 20 61 6e 64 20 66 72 65 65 29 2e 20 41 6e 79  d and free). Any
2c934 20 6f 74 68 65 72 20 56 54 61 62 6c 65 20 73 74   other VTable st
2c935 72 75 63 74 75 72 65 73 0a 2a 2a 20 69 6e 20 74  ructures.** in t
2c936 68 65 20 6c 69 73 74 20 61 72 65 20 6d 6f 76 65  he list are move
2c937 64 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  d to the sqlite3
2c938 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20 6c 69 73  .pDisconnect lis
2c939 74 20 6f 66 20 74 68 65 20 61 73 73 6f 63 69 61  t of the associa
2c93a 74 65 64 20 0a 2a 2a 20 64 61 74 61 62 61 73 65  ted .** database
2c93b 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a   connection..*/.
2c93c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2c93d 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 43  oid sqlite3VtabC
2c93e 6c 65 61 72 28 54 61 62 6c 65 20 2a 70 29 7b 0a  lear(Table *p){.
2c93f 20 20 76 74 61 62 44 69 73 63 6f 6e 6e 65 63 74    vtabDisconnect
2c940 41 6c 6c 28 30 2c 20 70 29 3b 0a 20 20 69 66 28  All(0, p);.  if(
2c941 20 70 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 20   p->azModuleArg 
2c942 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  ){.    int i;.  
2c943 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
2c944 6e 4d 6f 64 75 6c 65 41 72 67 3b 20 69 2b 2b 29  nModuleArg; i++)
2c945 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  {.      sqlite3D
2c946 62 46 72 65 65 28 70 2d 3e 64 62 4d 65 6d 2c 20  bFree(p->dbMem, 
2c947 70 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 69  p->azModuleArg[i
2c948 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  ]);.    }.    sq
2c949 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64  lite3DbFree(p->d
2c94a 62 4d 65 6d 2c 20 70 2d 3e 61 7a 4d 6f 64 75 6c  bMem, p->azModul
2c94b 65 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  eArg);.  }.}../*
2c94c 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 6d 6f  .** Add a new mo
2c94d 64 75 6c 65 20 61 72 67 75 6d 65 6e 74 20 74 6f  dule argument to
2c94e 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c   pTable->azModul
2c94f 65 41 72 67 5b 5d 2e 0a 2a 2a 20 54 68 65 20 73  eArg[]..** The s
2c950 74 72 69 6e 67 20 69 73 20 6e 6f 74 20 63 6f 70  tring is not cop
2c951 69 65 64 20 2d 20 74 68 65 20 70 6f 69 6e 74 65  ied - the pointe
2c952 72 20 69 73 20 73 74 6f 72 65 64 2e 20 20 54 68  r is stored.  Th
2c953 65 0a 2a 2a 20 73 74 72 69 6e 67 20 77 69 6c 6c  e.** string will
2c954 20 62 65 20 66 72 65 65 64 20 61 75 74 6f 6d 61   be freed automa
2c955 74 69 63 61 6c 6c 79 20 77 68 65 6e 20 74 68 65  tically when the
2c956 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 64 65 6c   table is.** del
2c957 65 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  eted..*/.static 
2c958 76 6f 69 64 20 61 64 64 4d 6f 64 75 6c 65 41 72  void addModuleAr
2c959 67 75 6d 65 6e 74 28 73 71 6c 69 74 65 33 20 2a  gument(sqlite3 *
2c95a 64 62 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 6c  db, Table *pTabl
2c95b 65 2c 20 63 68 61 72 20 2a 7a 41 72 67 29 7b 0a  e, char *zArg){.
2c95c 20 20 69 6e 74 20 69 20 3d 20 70 54 61 62 6c 65    int i = pTable
2c95d 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 2b 2b 3b 0a  ->nModuleArg++;.
2c95e 20 20 69 6e 74 20 6e 42 79 74 65 73 20 3d 20 73    int nBytes = s
2c95f 69 7a 65 6f 66 28 63 68 61 72 20 2a 29 2a 28 31  izeof(char *)*(1
2c960 2b 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65  +pTable->nModule
2c961 41 72 67 29 3b 0a 20 20 63 68 61 72 20 2a 2a 61  Arg);.  char **a
2c962 7a 4d 6f 64 75 6c 65 41 72 67 3b 0a 20 20 61 7a  zModuleArg;.  az
2c963 4d 6f 64 75 6c 65 41 72 67 20 3d 20 73 71 6c 69  ModuleArg = sqli
2c964 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c  te3DbRealloc(db,
2c965 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c   pTable->azModul
2c966 65 41 72 67 2c 20 6e 42 79 74 65 73 29 3b 0a 20  eArg, nBytes);. 
2c967 20 69 66 28 20 61 7a 4d 6f 64 75 6c 65 41 72 67   if( azModuleArg
2c968 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a  ==0 ){.    int j
2c969 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a  ;.    for(j=0; j
2c96a 3c 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  <i; j++){.      
2c96b 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2c96c 2c 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75  , pTable->azModu
2c96d 6c 65 41 72 67 5b 6a 5d 29 3b 0a 20 20 20 20 7d  leArg[j]);.    }
2c96e 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2c96f 65 65 28 64 62 2c 20 7a 41 72 67 29 3b 0a 20 20  ee(db, zArg);.  
2c970 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2c971 64 62 2c 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f  db, pTable->azMo
2c972 64 75 6c 65 41 72 67 29 3b 0a 20 20 20 20 70 54  duleArg);.    pT
2c973 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67  able->nModuleArg
2c974 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
2c975 20 20 20 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 69     azModuleArg[i
2c976 5d 20 3d 20 7a 41 72 67 3b 0a 20 20 20 20 61 7a  ] = zArg;.    az
2c977 4d 6f 64 75 6c 65 41 72 67 5b 69 2b 31 5d 20 3d  ModuleArg[i+1] =
2c978 20 30 3b 0a 20 20 7d 0a 20 20 70 54 61 62 6c 65   0;.  }.  pTable
2c979 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 20 3d 20  ->azModuleArg = 
2c97a 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 0a 7d 0a 0a  azModuleArg;.}..
2c97b 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72  /*.** The parser
2c97c 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74   calls this rout
2c97d 69 6e 65 20 77 68 65 6e 20 69 74 20 66 69 72 73  ine when it firs
2c97e 74 20 73 65 65 73 20 61 20 43 52 45 41 54 45 20  t sees a CREATE 
2c97f 56 49 52 54 55 41 4c 20 54 41 42 4c 45 0a 2a 2a  VIRTUAL TABLE.**
2c980 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65   statement.  The
2c981 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 68 61 73   module name has
2c982 20 62 65 65 6e 20 70 61 72 73 65 64 2c 20 62 75   been parsed, bu
2c983 74 20 74 68 65 20 6f 70 74 69 6f 6e 61 6c 20 6c  t the optional l
2c984 69 73 74 0a 2a 2a 20 6f 66 20 70 61 72 61 6d 65  ist.** of parame
2c985 74 65 72 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77  ters that follow
2c986 20 74 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65   the module name
2c987 20 61 72 65 20 73 74 69 6c 6c 20 70 65 6e 64 69   are still pendi
2c988 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ng..*/.SQLITE_PR
2c989 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2c98a 65 33 56 74 61 62 42 65 67 69 6e 50 61 72 73 65  e3VtabBeginParse
2c98b 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2c98c 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
2c98d 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
2c98e 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c    Token *pName1,
2c98f 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
2c990 6f 66 20 6e 65 77 20 74 61 62 6c 65 2c 20 6f 72  of new table, or
2c991 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a   database name *
2c992 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  /.  Token *pName
2c993 32 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  2,        /* Nam
2c994 65 20 6f 66 20 6e 65 77 20 74 61 62 6c 65 20 6f  e of new table o
2c995 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65  r NULL */.  Toke
2c996 6e 20 2a 70 4d 6f 64 75 6c 65 4e 61 6d 65 20 20  n *pModuleName  
2c997 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
2c998 20 6d 6f 64 75 6c 65 20 66 6f 72 20 74 68 65 20   module for the 
2c999 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f  virtual table */
2c99a 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20  .){.  int iDb;  
2c99b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2c99c 68 65 20 64 61 74 61 62 61 73 65 20 74 68 65 20  he database the 
2c99d 74 61 62 6c 65 20 69 73 20 62 65 69 6e 67 20 63  table is being c
2c99e 72 65 61 74 65 64 20 69 6e 20 2a 2f 0a 20 20 54  reated in */.  T
2c99f 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b 20 20 20  able *pTable;   
2c9a0 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20       /* The new 
2c9a1 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f  virtual table */
2c9a2 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
2c9a3 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
2c9a4 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
2c9a5 2a 2f 0a 0a 20 20 73 71 6c 69 74 65 33 53 74 61  */..  sqlite3Sta
2c9a6 72 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  rtTable(pParse, 
2c9a7 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20  pName1, pName2, 
2c9a8 30 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 70  0, 0, 1, 0);.  p
2c9a9 54 61 62 6c 65 20 3d 20 70 50 61 72 73 65 2d 3e  Table = pParse->
2c9aa 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28  pNewTable;.  if(
2c9ab 20 70 54 61 62 6c 65 3d 3d 30 20 29 20 72 65 74   pTable==0 ) ret
2c9ac 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 30  urn;.  assert( 0
2c9ad 3d 3d 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78  ==pTable->pIndex
2c9ae 20 29 3b 0a 0a 20 20 64 62 20 3d 20 70 50 61 72   );..  db = pPar
2c9af 73 65 2d 3e 64 62 3b 0a 20 20 69 44 62 20 3d 20  se->db;.  iDb = 
2c9b0 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
2c9b1 6e 64 65 78 28 64 62 2c 20 70 54 61 62 6c 65 2d  ndex(db, pTable-
2c9b2 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 73  >pSchema);.  ass
2c9b3 65 72 74 28 20 69 44 62 3e 3d 30 20 29 3b 0a 0a  ert( iDb>=0 );..
2c9b4 20 20 70 54 61 62 6c 65 2d 3e 74 61 62 46 6c 61    pTable->tabFla
2c9b5 67 73 20 7c 3d 20 54 46 5f 56 69 72 74 75 61 6c  gs |= TF_Virtual
2c9b6 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64  ;.  pTable->nMod
2c9b7 75 6c 65 41 72 67 20 3d 20 30 3b 0a 20 20 61 64  uleArg = 0;.  ad
2c9b8 64 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74 28  dModuleArgument(
2c9b9 64 62 2c 20 70 54 61 62 6c 65 2c 20 73 71 6c 69  db, pTable, sqli
2c9ba 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
2c9bb 28 64 62 2c 20 70 4d 6f 64 75 6c 65 4e 61 6d 65  (db, pModuleName
2c9bc 29 29 3b 0a 20 20 61 64 64 4d 6f 64 75 6c 65 41  ));.  addModuleA
2c9bd 72 67 75 6d 65 6e 74 28 64 62 2c 20 70 54 61 62  rgument(db, pTab
2c9be 6c 65 2c 20 73 71 6c 69 74 65 33 44 62 53 74 72  le, sqlite3DbStr
2c9bf 44 75 70 28 64 62 2c 20 64 62 2d 3e 61 44 62 5b  Dup(db, db->aDb[
2c9c0 69 44 62 5d 2e 7a 4e 61 6d 65 29 29 3b 0a 20 20  iDb].zName));.  
2c9c1 61 64 64 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e  addModuleArgumen
2c9c2 74 28 64 62 2c 20 70 54 61 62 6c 65 2c 20 73 71  t(db, pTable, sq
2c9c3 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
2c9c4 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 29  , pTable->zName)
2c9c5 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73 4e 61  );.  pParse->sNa
2c9c6 6d 65 54 6f 6b 65 6e 2e 6e 20 3d 20 28 69 6e 74  meToken.n = (int
2c9c7 29 28 26 70 4d 6f 64 75 6c 65 4e 61 6d 65 2d 3e  )(&pModuleName->
2c9c8 7a 5b 70 4d 6f 64 75 6c 65 4e 61 6d 65 2d 3e 6e  z[pModuleName->n
2c9c9 5d 20 2d 20 70 4e 61 6d 65 31 2d 3e 7a 29 3b 0a  ] - pName1->z);.
2c9ca 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2c9cb 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
2c9cc 4f 4e 0a 20 20 2f 2a 20 43 72 65 61 74 69 6e 67  ON.  /* Creating
2c9cd 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
2c9ce 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 61 75 74   invokes the aut
2c9cf 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62  horization callb
2c9d0 61 63 6b 20 74 77 69 63 65 2e 0a 20 20 2a 2a 20  ack twice..  ** 
2c9d1 54 68 65 20 66 69 72 73 74 20 69 6e 76 6f 63 61  The first invoca
2c9d2 74 69 6f 6e 2c 20 74 6f 20 6f 62 74 61 69 6e 20  tion, to obtain 
2c9d3 70 65 72 6d 69 73 73 69 6f 6e 20 74 6f 20 49 4e  permission to IN
2c9d4 53 45 52 54 20 61 20 72 6f 77 20 69 6e 74 6f 20  SERT a row into 
2c9d5 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f  the.  ** sqlite_
2c9d6 6d 61 73 74 65 72 20 74 61 62 6c 65 2c 20 68 61  master table, ha
2c9d7 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6d  s already been m
2c9d8 61 64 65 20 62 79 20 73 71 6c 69 74 65 33 53 74  ade by sqlite3St
2c9d9 61 72 74 54 61 62 6c 65 28 29 2e 0a 20 20 2a 2a  artTable()..  **
2c9da 20 54 68 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c   The second call
2c9db 2c 20 74 6f 20 6f 62 74 61 69 6e 20 70 65 72 6d  , to obtain perm
2c9dc 69 73 73 69 6f 6e 20 74 6f 20 63 72 65 61 74 65  ission to create
2c9dd 20 74 68 65 20 74 61 62 6c 65 2c 20 69 73 20 6d   the table, is m
2c9de 61 64 65 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20  ade now..  */.  
2c9df 69 66 28 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f  if( pTable->azMo
2c9e0 64 75 6c 65 41 72 67 20 29 7b 0a 20 20 20 20 73  duleArg ){.    s
2c9e1 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
2c9e2 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 43  pParse, SQLITE_C
2c9e3 52 45 41 54 45 5f 56 54 41 42 4c 45 2c 20 70 54  REATE_VTABLE, pT
2c9e4 61 62 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 0a 20 20  able->zName, .  
2c9e5 20 20 20 20 20 20 20 20 20 20 70 54 61 62 6c 65            pTable
2c9e6 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d  ->azModuleArg[0]
2c9e7 2c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44  , pParse->db->aD
2c9e8 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  b[iDb].zName);. 
2c9e9 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a   }.#endif.}../*.
2c9ea 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2c9eb 74 61 6b 65 73 20 74 68 65 20 6d 6f 64 75 6c 65  takes the module
2c9ec 20 61 72 67 75 6d 65 6e 74 20 74 68 61 74 20 68   argument that h
2c9ed 61 73 20 62 65 65 6e 20 61 63 63 75 6d 75 6c 61  as been accumula
2c9ee 74 69 6e 67 0a 2a 2a 20 69 6e 20 70 50 61 72 73  ting.** in pPars
2c9ef 65 2d 3e 7a 41 72 67 5b 5d 20 61 6e 64 20 61 70  e->zArg[] and ap
2c9f0 70 65 6e 64 73 20 69 74 20 74 6f 20 74 68 65 20  pends it to the 
2c9f1 6c 69 73 74 20 6f 66 20 61 72 67 75 6d 65 6e 74  list of argument
2c9f2 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 76 69 72 74  s on the.** virt
2c9f3 75 61 6c 20 74 61 62 6c 65 20 63 75 72 72 65 6e  ual table curren
2c9f4 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  tly under constr
2c9f5 75 63 74 69 6f 6e 20 69 6e 20 70 50 61 72 73 65  uction in pParse
2c9f6 2d 3e 70 54 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  ->pTable..*/.sta
2c9f7 74 69 63 20 76 6f 69 64 20 61 64 64 41 72 67 75  tic void addArgu
2c9f8 6d 65 6e 74 54 6f 56 74 61 62 28 50 61 72 73 65  mentToVtab(Parse
2c9f9 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 66 28   *pParse){.  if(
2c9fa 20 70 50 61 72 73 65 2d 3e 73 41 72 67 2e 7a 20   pParse->sArg.z 
2c9fb 26 26 20 41 4c 57 41 59 53 28 70 50 61 72 73 65  && ALWAYS(pParse
2c9fc 2d 3e 70 4e 65 77 54 61 62 6c 65 29 20 29 7b 0a  ->pNewTable) ){.
2c9fd 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2c9fe 7a 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a  z = (const char*
2c9ff 29 70 50 61 72 73 65 2d 3e 73 41 72 67 2e 7a 3b  )pParse->sArg.z;
2ca00 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 50 61  .    int n = pPa
2ca01 72 73 65 2d 3e 73 41 72 67 2e 6e 3b 0a 20 20 20  rse->sArg.n;.   
2ca02 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
2ca03 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 61  Parse->db;.    a
2ca04 64 64 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74  ddModuleArgument
2ca05 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 70 4e 65  (db, pParse->pNe
2ca06 77 54 61 62 6c 65 2c 20 73 71 6c 69 74 65 33 44  wTable, sqlite3D
2ca07 62 53 74 72 4e 44 75 70 28 64 62 2c 20 7a 2c 20  bStrNDup(db, z, 
2ca08 6e 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  n));.  }.}../*.*
2ca09 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c  * The parser cal
2ca0a 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
2ca0b 61 66 74 65 72 20 74 68 65 20 43 52 45 41 54 45  after the CREATE
2ca0c 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73   VIRTUAL TABLE s
2ca0d 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 68 61 73 20  tatement.** has 
2ca0e 62 65 65 6e 20 63 6f 6d 70 6c 65 74 65 6c 79 20  been completely 
2ca0f 70 61 72 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  parsed..*/.SQLIT
2ca10 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2ca11 71 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73 68  qlite3VtabFinish
2ca12 50 61 72 73 65 28 50 61 72 73 65 20 2a 70 50 61  Parse(Parse *pPa
2ca13 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 45 6e 64  rse, Token *pEnd
2ca14 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  ){.  Table *pTab
2ca15 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54   = pParse->pNewT
2ca16 61 62 6c 65 3b 20 20 2f 2a 20 54 68 65 20 74 61  able;  /* The ta
2ca17 62 6c 65 20 62 65 69 6e 67 20 63 6f 6e 73 74 72  ble being constr
2ca18 75 63 74 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74  ucted */.  sqlit
2ca19 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2ca1a 3e 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  >db;         /* 
2ca1b 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
2ca1c 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66  nection */..  if
2ca1d 28 20 70 54 61 62 3d 3d 30 20 29 20 72 65 74 75  ( pTab==0 ) retu
2ca1e 72 6e 3b 0a 20 20 61 64 64 41 72 67 75 6d 65 6e  rn;.  addArgumen
2ca1f 74 54 6f 56 74 61 62 28 70 50 61 72 73 65 29 3b  tToVtab(pParse);
2ca20 0a 20 20 70 50 61 72 73 65 2d 3e 73 41 72 67 2e  .  pParse->sArg.
2ca21 7a 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 54 61  z = 0;.  if( pTa
2ca22 62 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 3c 31 20  b->nModuleArg<1 
2ca23 29 20 72 65 74 75 72 6e 3b 0a 20 20 0a 20 20 2f  ) return;.  .  /
2ca24 2a 20 49 66 20 74 68 65 20 43 52 45 41 54 45 20  * If the CREATE 
2ca25 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74  VIRTUAL TABLE st
2ca26 61 74 65 6d 65 6e 74 20 69 73 20 62 65 69 6e 67  atement is being
2ca27 20 65 6e 74 65 72 65 64 20 66 6f 72 20 74 68 65   entered for the
2ca28 0a 20 20 2a 2a 20 66 69 72 73 74 20 74 69 6d 65  .  ** first time
2ca29 20 28 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73   (in other words
2ca2a 20 69 66 20 74 68 65 20 76 69 72 74 75 61 6c 20   if the virtual 
2ca2b 74 61 62 6c 65 20 69 73 20 61 63 74 75 61 6c 6c  table is actuall
2ca2c 79 20 62 65 69 6e 67 0a 20 20 2a 2a 20 63 72 65  y being.  ** cre
2ca2d 61 74 65 64 20 6e 6f 77 20 69 6e 73 74 65 61 64  ated now instead
2ca2e 20 6f 66 20 6a 75 73 74 20 62 65 69 6e 67 20 72   of just being r
2ca2f 65 61 64 20 6f 75 74 20 6f 66 20 73 71 6c 69 74  ead out of sqlit
2ca30 65 5f 6d 61 73 74 65 72 29 20 74 68 65 6e 0a 20  e_master) then. 
2ca31 20 2a 2a 20 64 6f 20 61 64 64 69 74 69 6f 6e 61   ** do additiona
2ca32 6c 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  l initialization
2ca33 20 77 6f 72 6b 20 61 6e 64 20 73 74 6f 72 65 20   work and store 
2ca34 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 65  the statement te
2ca35 78 74 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 73  xt.  ** in the s
2ca36 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
2ca37 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  le..  */.  if( !
2ca38 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b  db->init.busy ){
2ca39 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 74 6d 74  .    char *zStmt
2ca3a 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 57 68 65  ;.    char *zWhe
2ca3b 72 65 3b 0a 20 20 20 20 69 6e 74 20 69 44 62 3b  re;.    int iDb;
2ca3c 0a 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20  .    Vdbe *v;.. 
2ca3d 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68     /* Compute th
2ca3e 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20  e complete text 
2ca3f 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 56 49  of the CREATE VI
2ca40 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74  RTUAL TABLE stat
2ca41 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 69 66 28  ement */.    if(
2ca42 20 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 70   pEnd ){.      p
2ca43 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65  Parse->sNameToke
2ca44 6e 2e 6e 20 3d 20 28 69 6e 74 29 28 70 45 6e 64  n.n = (int)(pEnd
2ca45 2d 3e 7a 20 2d 20 70 50 61 72 73 65 2d 3e 73 4e  ->z - pParse->sN
2ca46 61 6d 65 54 6f 6b 65 6e 2e 7a 29 20 2b 20 70 45  ameToken.z) + pE
2ca47 6e 64 2d 3e 6e 3b 0a 20 20 20 20 7d 0a 20 20 20  nd->n;.    }.   
2ca48 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33   zStmt = sqlite3
2ca49 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 43 52 45  MPrintf(db, "CRE
2ca4a 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c  ATE VIRTUAL TABL
2ca4b 45 20 25 54 22 2c 20 26 70 50 61 72 73 65 2d 3e  E %T", &pParse->
2ca4c 73 4e 61 6d 65 54 6f 6b 65 6e 29 3b 0a 0a 20 20  sNameToken);..  
2ca4d 20 20 2f 2a 20 41 20 73 6c 6f 74 20 66 6f 72 20    /* A slot for 
2ca4e 74 68 65 20 72 65 63 6f 72 64 20 68 61 73 20 61  the record has a
2ca4f 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f  lready been allo
2ca50 63 61 74 65 64 20 69 6e 20 74 68 65 20 0a 20 20  cated in the .  
2ca51 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 53 54    ** SQLITE_MAST
2ca52 45 52 20 74 61 62 6c 65 2e 20 20 57 65 20 6a 75  ER table.  We ju
2ca53 73 74 20 6e 65 65 64 20 74 6f 20 75 70 64 61 74  st need to updat
2ca54 65 20 74 68 61 74 20 73 6c 6f 74 20 77 69 74 68  e that slot with
2ca55 20 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 68 65 20   all.    ** the 
2ca56 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 65 27 76  information we'v
2ca57 65 20 63 6f 6c 6c 65 63 74 65 64 2e 20 20 0a 20  e collected.  . 
2ca58 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
2ca59 20 56 4d 20 72 65 67 69 73 74 65 72 20 6e 75 6d   VM register num
2ca5a 62 65 72 20 70 50 61 72 73 65 2d 3e 72 65 67 52  ber pParse->regR
2ca5b 6f 77 69 64 20 68 6f 6c 64 73 20 74 68 65 20 72  owid holds the r
2ca5c 6f 77 69 64 20 6f 66 20 61 6e 0a 20 20 20 20 2a  owid of an.    *
2ca5d 2a 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 73  * entry in the s
2ca5e 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
2ca5f 6c 65 20 74 68 74 20 77 61 73 20 63 72 65 61 74  le tht was creat
2ca60 65 64 20 66 6f 72 20 74 68 69 73 20 76 74 61 62  ed for this vtab
2ca61 0a 20 20 20 20 2a 2a 20 62 79 20 73 71 6c 69 74  .    ** by sqlit
2ca62 65 33 53 74 61 72 74 54 61 62 6c 65 28 29 2e 0a  e3StartTable()..
2ca63 20 20 20 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d      */.    iDb =
2ca64 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
2ca65 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e  Index(db, pTab->
2ca66 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 73 71  pSchema);.    sq
2ca67 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
2ca68 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 22  (pParse,.      "
2ca69 55 50 44 41 54 45 20 25 51 2e 25 73 20 22 0a 20  UPDATE %Q.%s ". 
2ca6a 20 20 20 20 20 20 20 20 22 53 45 54 20 74 79 70          "SET typ
2ca6b 65 3d 27 74 61 62 6c 65 27 2c 20 6e 61 6d 65 3d  e='table', name=
2ca6c 25 51 2c 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 2c  %Q, tbl_name=%Q,
2ca6d 20 72 6f 6f 74 70 61 67 65 3d 30 2c 20 73 71 6c   rootpage=0, sql
2ca6e 3d 25 51 20 22 0a 20 20 20 20 20 20 20 22 57 48  =%Q ".       "WH
2ca6f 45 52 45 20 72 6f 77 69 64 3d 23 25 64 22 2c 0a  ERE rowid=#%d",.
2ca70 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44        db->aDb[iD
2ca71 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41  b].zName, SCHEMA
2ca72 5f 54 41 42 4c 45 28 69 44 62 29 2c 0a 20 20 20  _TABLE(iDb),.   
2ca73 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0a     pTab->zName,.
2ca74 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d        pTab->zNam
2ca75 65 2c 0a 20 20 20 20 20 20 7a 53 74 6d 74 2c 0a  e,.      zStmt,.
2ca76 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 65        pParse->re
2ca77 67 52 6f 77 69 64 0a 20 20 20 20 29 3b 0a 20 20  gRowid.    );.  
2ca78 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2ca79 64 62 2c 20 7a 53 74 6d 74 29 3b 0a 20 20 20 20  db, zStmt);.    
2ca7a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
2ca7b 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  be(pParse);.    
2ca7c 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f  sqlite3ChangeCoo
2ca7d 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29  kie(pParse, iDb)
2ca7e 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ;..    sqlite3Vd
2ca7f 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 45  beAddOp2(v, OP_E
2ca80 78 70 69 72 65 2c 20 30 2c 20 30 29 3b 0a 20 20  xpire, 0, 0);.  
2ca81 20 20 7a 57 68 65 72 65 20 3d 20 73 71 6c 69 74    zWhere = sqlit
2ca82 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e  e3MPrintf(db, "n
2ca83 61 6d 65 3d 27 25 71 27 22 2c 20 70 54 61 62 2d  ame='%q'", pTab-
2ca84 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c  >zName);.    sql
2ca85 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2ca86 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61  , OP_ParseSchema
2ca87 2c 20 69 44 62 2c 20 31 2c 20 30 2c 20 7a 57 68  , iDb, 1, 0, zWh
2ca88 65 72 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29  ere, P4_DYNAMIC)
2ca89 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2ca8a 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 43  eAddOp4(v, OP_VC
2ca8b 72 65 61 74 65 2c 20 69 44 62 2c 20 30 2c 20 30  reate, iDb, 0, 0
2ca8c 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
2ca8d 20 20 20 20 20 20 20 20 20 20 20 20 70 54 61 62              pTab
2ca8e 2d 3e 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33  ->zName, sqlite3
2ca8f 53 74 72 6c 65 6e 33 30 28 70 54 61 62 2d 3e 7a  Strlen30(pTab->z
2ca90 4e 61 6d 65 29 20 2b 20 31 29 3b 0a 20 20 7d 0a  Name) + 1);.  }.
2ca91 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20  .  /* If we are 
2ca92 72 65 72 65 61 64 69 6e 67 20 74 68 65 20 73 71  rereading the sq
2ca93 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c  lite_master tabl
2ca94 65 20 63 72 65 61 74 65 20 74 68 65 20 69 6e 2d  e create the in-
2ca95 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20 72 65 63 6f  memory.  ** reco
2ca96 72 64 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e  rd of the table.
2ca97 20 54 68 65 20 78 43 6f 6e 6e 65 63 74 28 29 20   The xConnect() 
2ca98 6d 65 74 68 6f 64 20 69 73 20 6e 6f 74 20 63 61  method is not ca
2ca99 6c 6c 65 64 20 75 6e 74 69 6c 0a 20 20 2a 2a 20  lled until.  ** 
2ca9a 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74  the first time t
2ca9b 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
2ca9c 20 69 73 20 75 73 65 64 20 69 6e 20 61 6e 20 53   is used in an S
2ca9d 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68  QL statement. Th
2ca9e 69 73 0a 20 20 2a 2a 20 61 6c 6c 6f 77 73 20 61  is.  ** allows a
2ca9f 20 73 63 68 65 6d 61 20 74 68 61 74 20 63 6f 6e   schema that con
2caa0 74 61 69 6e 73 20 76 69 72 74 75 61 6c 20 74 61  tains virtual ta
2caa1 62 6c 65 73 20 74 6f 20 62 65 20 6c 6f 61 64 65  bles to be loade
2caa2 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 74 68  d before.  ** th
2caa3 65 20 72 65 71 75 69 72 65 64 20 76 69 72 74 75  e required virtu
2caa4 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65  al table impleme
2caa5 6e 74 61 74 69 6f 6e 73 20 61 72 65 20 72 65 67  ntations are reg
2caa6 69 73 74 65 72 65 64 2e 20 20 2a 2f 0a 20 20 65  istered.  */.  e
2caa7 6c 73 65 20 7b 0a 20 20 20 20 54 61 62 6c 65 20  lse {.    Table 
2caa8 2a 70 4f 6c 64 3b 0a 20 20 20 20 53 63 68 65 6d  *pOld;.    Schem
2caa9 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 70 54 61  a *pSchema = pTa
2caaa 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20  b->pSchema;.    
2caab 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
2caac 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b  e = pTab->zName;
2caad 0a 20 20 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d  .    int nName =
2caae 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2caaf 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 4f 6c  (zName);.    pOl
2cab0 64 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49  d = sqlite3HashI
2cab1 6e 73 65 72 74 28 26 70 53 63 68 65 6d 61 2d 3e  nsert(&pSchema->
2cab2 74 62 6c 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20  tblHash, zName, 
2cab3 6e 4e 61 6d 65 2c 20 70 54 61 62 29 3b 0a 20 20  nName, pTab);.  
2cab4 20 20 69 66 28 20 70 4f 6c 64 20 29 7b 0a 20 20    if( pOld ){.  
2cab5 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
2cab6 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
2cab7 61 73 73 65 72 74 28 20 70 54 61 62 3d 3d 70 4f  assert( pTab==pO
2cab8 6c 64 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63  ld );  /* Malloc
2cab9 20 6d 75 73 74 20 68 61 76 65 20 66 61 69 6c 65   must have faile
2caba 64 20 69 6e 73 69 64 65 20 48 61 73 68 49 6e 73  d inside HashIns
2cabb 65 72 74 28 29 20 2a 2f 0a 20 20 20 20 20 20 72  ert() */.      r
2cabc 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20  eturn;.    }.   
2cabd 20 70 53 63 68 65 6d 61 2d 3e 64 62 20 3d 20 70   pSchema->db = p
2cabe 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 70  Parse->db;.    p
2cabf 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2cac0 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   = 0;.  }.}../*.
2cac1 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61  ** The parser ca
2cac2 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  lls this routine
2cac3 20 77 68 65 6e 20 69 74 20 73 65 65 73 20 74 68   when it sees th
2cac4 65 20 66 69 72 73 74 20 74 6f 6b 65 6e 0a 2a 2a  e first token.**
2cac5 20 6f 66 20 61 6e 20 61 72 67 75 6d 65 6e 74 20   of an argument 
2cac6 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 6e 61  to the module na
2cac7 6d 65 20 69 6e 20 61 20 43 52 45 41 54 45 20 56  me in a CREATE V
2cac8 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61  IRTUAL TABLE sta
2cac9 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  tement..*/.SQLIT
2caca 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2cacb 71 6c 69 74 65 33 56 74 61 62 41 72 67 49 6e 69  qlite3VtabArgIni
2cacc 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29  t(Parse *pParse)
2cacd 7b 0a 20 20 61 64 64 41 72 67 75 6d 65 6e 74 54  {.  addArgumentT
2cace 6f 56 74 61 62 28 70 50 61 72 73 65 29 3b 0a 20  oVtab(pParse);. 
2cacf 20 70 50 61 72 73 65 2d 3e 73 41 72 67 2e 7a 20   pParse->sArg.z 
2cad0 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73  = 0;.  pParse->s
2cad1 41 72 67 2e 6e 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  Arg.n = 0;.}../*
2cad2 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63  .** The parser c
2cad3 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e  alls this routin
2cad4 65 20 66 6f 72 20 65 61 63 68 20 74 6f 6b 65 6e  e for each token
2cad5 20 61 66 74 65 72 20 74 68 65 20 66 69 72 73 74   after the first
2cad6 20 74 6f 6b 65 6e 0a 2a 2a 20 69 6e 20 61 6e 20   token.** in an 
2cad7 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
2cad8 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 69 6e 20 61  module name in a
2cad9 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20   CREATE VIRTUAL 
2cada 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e  TABLE statement.
2cadb 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2cadc 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
2cadd 74 61 62 41 72 67 45 78 74 65 6e 64 28 50 61 72  tabArgExtend(Par
2cade 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65  se *pParse, Toke
2cadf 6e 20 2a 70 29 7b 0a 20 20 54 6f 6b 65 6e 20 2a  n *p){.  Token *
2cae0 70 41 72 67 20 3d 20 26 70 50 61 72 73 65 2d 3e  pArg = &pParse->
2cae1 73 41 72 67 3b 0a 20 20 69 66 28 20 70 41 72 67  sArg;.  if( pArg
2cae2 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 70 41  ->z==0 ){.    pA
2cae3 72 67 2d 3e 7a 20 3d 20 70 2d 3e 7a 3b 0a 20 20  rg->z = p->z;.  
2cae4 20 20 70 41 72 67 2d 3e 6e 20 3d 20 70 2d 3e 6e    pArg->n = p->n
2cae5 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
2cae6 73 73 65 72 74 28 70 41 72 67 2d 3e 7a 20 3c 20  ssert(pArg->z < 
2cae7 70 2d 3e 7a 29 3b 0a 20 20 20 20 70 41 72 67 2d  p->z);.    pArg-
2cae8 3e 6e 20 3d 20 28 69 6e 74 29 28 26 70 2d 3e 7a  >n = (int)(&p->z
2cae9 5b 70 2d 3e 6e 5d 20 2d 20 70 41 72 67 2d 3e 7a  [p->n] - pArg->z
2caea 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
2caeb 49 6e 76 6f 6b 65 20 61 20 76 69 72 74 75 61 6c  Invoke a virtual
2caec 20 74 61 62 6c 65 20 63 6f 6e 73 74 72 75 63 74   table construct
2caed 6f 72 20 28 65 69 74 68 65 72 20 78 43 72 65 61  or (either xCrea
2caee 74 65 20 6f 72 20 78 43 6f 6e 6e 65 63 74 29 2e  te or xConnect).
2caef 20 54 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20   The.** pointer 
2caf0 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  to the function 
2caf1 74 6f 20 69 6e 76 6f 6b 65 20 69 73 20 70 61 73  to invoke is pas
2caf2 73 65 64 20 61 73 20 74 68 65 20 66 6f 75 72 74  sed as the fourt
2caf3 68 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74  h parameter.** t
2caf4 6f 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65  o this procedure
2caf5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2caf6 76 74 61 62 43 61 6c 6c 43 6f 6e 73 74 72 75 63  vtabCallConstruc
2caf7 74 6f 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  tor(.  sqlite3 *
2caf8 64 62 2c 20 0a 20 20 54 61 62 6c 65 20 2a 70 54  db, .  Table *pT
2caf9 61 62 2c 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d  ab,.  Module *pM
2cafa 6f 64 2c 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e  od,.  int (*xCon
2cafb 73 74 72 75 63 74 29 28 73 71 6c 69 74 65 33 2a  struct)(sqlite3*
2cafc 2c 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74  ,void*,int,const
2cafd 20 63 68 61 72 2a 63 6f 6e 73 74 2a 2c 73 71 6c   char*const*,sql
2cafe 69 74 65 33 5f 76 74 61 62 2a 2a 2c 63 68 61 72  ite3_vtab**,char
2caff 2a 2a 29 2c 0a 20 20 63 68 61 72 20 2a 2a 70 7a  **),.  char **pz
2cb00 45 72 72 0a 29 7b 0a 20 20 56 54 61 62 6c 65 20  Err.){.  VTable 
2cb01 2a 70 56 54 61 62 6c 65 3b 0a 20 20 69 6e 74 20  *pVTable;.  int 
2cb02 72 63 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  rc;.  const char
2cb03 20 2a 63 6f 6e 73 74 2a 61 7a 41 72 67 20 3d 20   *const*azArg = 
2cb04 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e  (const char *con
2cb05 73 74 2a 29 70 54 61 62 2d 3e 61 7a 4d 6f 64 75  st*)pTab->azModu
2cb06 6c 65 41 72 67 3b 0a 20 20 69 6e 74 20 6e 41 72  leArg;.  int nAr
2cb07 67 20 3d 20 70 54 61 62 2d 3e 6e 4d 6f 64 75 6c  g = pTab->nModul
2cb08 65 41 72 67 3b 0a 20 20 63 68 61 72 20 2a 7a 45  eArg;.  char *zE
2cb09 72 72 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a  rr = 0;.  char *
2cb0a 7a 4d 6f 64 75 6c 65 4e 61 6d 65 20 3d 20 73 71  zModuleName = sq
2cb0b 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
2cb0c 20 22 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61   "%s", pTab->zNa
2cb0d 6d 65 29 3b 0a 0a 20 20 69 66 28 20 21 7a 4d 6f  me);..  if( !zMo
2cb0e 64 75 6c 65 4e 61 6d 65 20 29 7b 0a 20 20 20 20  duleName ){.    
2cb0f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
2cb10 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 70 56 54 61  MEM;.  }..  pVTa
2cb11 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  ble = sqlite3DbM
2cb12 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
2cb13 7a 65 6f 66 28 56 54 61 62 6c 65 29 29 3b 0a 20  zeof(VTable));. 
2cb14 20 69 66 28 20 21 70 56 54 61 62 6c 65 20 29 7b   if( !pVTable ){
2cb15 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2cb16 65 65 28 64 62 2c 20 7a 4d 6f 64 75 6c 65 4e 61  ee(db, zModuleNa
2cb17 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  me);.    return 
2cb18 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
2cb19 7d 0a 20 20 70 56 54 61 62 6c 65 2d 3e 64 62 20  }.  pVTable->db 
2cb1a 3d 20 64 62 3b 0a 20 20 70 56 54 61 62 6c 65 2d  = db;.  pVTable-
2cb1b 3e 70 4d 6f 64 20 3d 20 70 4d 6f 64 3b 0a 0a 20  >pMod = pMod;.. 
2cb1c 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 70 56   assert( !db->pV
2cb1d 54 61 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28  Tab );.  assert(
2cb1e 20 78 43 6f 6e 73 74 72 75 63 74 20 29 3b 0a 20   xConstruct );. 
2cb1f 20 64 62 2d 3e 70 56 54 61 62 20 3d 20 70 54 61   db->pVTab = pTa
2cb20 62 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20  b;..  /* Invoke 
2cb21 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
2cb22 65 20 63 6f 6e 73 74 72 75 63 74 6f 72 20 2a 2f  e constructor */
2cb23 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33  .  (void)sqlite3
2cb24 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20  SafetyOff(db);. 
2cb25 20 72 63 20 3d 20 78 43 6f 6e 73 74 72 75 63 74   rc = xConstruct
2cb26 28 64 62 2c 20 70 4d 6f 64 2d 3e 70 41 75 78 2c  (db, pMod->pAux,
2cb27 20 6e 41 72 67 2c 20 61 7a 41 72 67 2c 20 26 70   nArg, azArg, &p
2cb28 56 54 61 62 6c 65 2d 3e 70 56 74 61 62 2c 20 26  VTable->pVtab, &
2cb29 7a 45 72 72 29 3b 0a 20 20 28 76 6f 69 64 29 73  zErr);.  (void)s
2cb2a 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
2cb2b 62 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  b);.  if( rc==SQ
2cb2c 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 20 64 62 2d  LITE_NOMEM ) db-
2cb2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
2cb2e 31 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45  1;..  if( SQLITE
2cb2f 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 69  _OK!=rc ){.    i
2cb30 66 28 20 7a 45 72 72 3d 3d 30 20 29 7b 0a 20 20  f( zErr==0 ){.  
2cb31 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c      *pzErr = sql
2cb32 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
2cb33 22 76 74 61 62 6c 65 20 63 6f 6e 73 74 72 75 63  "vtable construc
2cb34 74 6f 72 20 66 61 69 6c 65 64 3a 20 25 73 22 2c  tor failed: %s",
2cb35 20 7a 4d 6f 64 75 6c 65 4e 61 6d 65 29 3b 0a 20   zModuleName);. 
2cb36 20 20 20 7d 65 6c 73 65 20 7b 0a 20 20 20 20 20     }else {.     
2cb37 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65   *pzErr = sqlite
2cb38 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73  3MPrintf(db, "%s
2cb39 22 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20  ", zErr);.      
2cb3a 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2cb3b 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 7d 0a 20  , zErr);.    }. 
2cb3c 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2cb3d 28 64 62 2c 20 70 56 54 61 62 6c 65 29 3b 0a 20  (db, pVTable);. 
2cb3e 20 7d 65 6c 73 65 20 69 66 28 20 41 4c 57 41 59   }else if( ALWAY
2cb3f 53 28 70 56 54 61 62 6c 65 2d 3e 70 56 74 61 62  S(pVTable->pVtab
2cb40 29 20 29 7b 0a 20 20 20 20 2f 2a 20 4a 75 73 74  ) ){.    /* Just
2cb41 69 66 69 63 61 74 69 6f 6e 20 6f 66 20 41 4c 57  ification of ALW
2cb42 41 59 53 28 29 3a 20 20 41 20 63 6f 72 72 65 63  AYS():  A correc
2cb43 74 20 76 74 61 62 20 63 6f 6e 73 74 72 75 63 74  t vtab construct
2cb44 6f 72 20 6d 75 73 74 20 61 6c 6c 6f 63 61 74 65  or must allocate
2cb45 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 71 6c 69  .    ** the sqli
2cb46 74 65 33 5f 76 74 61 62 20 6f 62 6a 65 63 74 20  te3_vtab object 
2cb47 69 66 20 73 75 63 63 65 73 73 66 75 6c 2e 20 20  if successful.  
2cb48 2a 2f 0a 20 20 20 20 70 56 54 61 62 6c 65 2d 3e  */.    pVTable->
2cb49 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 20 3d  pVtab->pModule =
2cb4a 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 3b 0a   pMod->pModule;.
2cb4b 20 20 20 20 70 56 54 61 62 6c 65 2d 3e 6e 52 65      pVTable->nRe
2cb4c 66 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20 64  f = 1;.    if( d
2cb4d 62 2d 3e 70 56 54 61 62 20 29 7b 0a 20 20 20 20  b->pVTab ){.    
2cb4e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
2cb4f 6f 72 6d 61 74 20 3d 20 22 76 74 61 62 6c 65 20  ormat = "vtable 
2cb50 63 6f 6e 73 74 72 75 63 74 6f 72 20 64 69 64 20  constructor did 
2cb51 6e 6f 74 20 64 65 63 6c 61 72 65 20 73 63 68 65  not declare sche
2cb52 6d 61 3a 20 25 73 22 3b 0a 20 20 20 20 20 20 2a  ma: %s";.      *
2cb53 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d  pzErr = sqlite3M
2cb54 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d  Printf(db, zForm
2cb55 61 74 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  at, pTab->zName)
2cb56 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2cb57 74 61 62 55 6e 6c 6f 63 6b 28 70 56 54 61 62 6c  tabUnlock(pVTabl
2cb58 65 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  e);.      rc = S
2cb59 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
2cb5a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e   }else{.      in
2cb5b 74 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 2f 2a  t iCol;.      /*
2cb5c 20 49 66 20 65 76 65 72 79 74 68 69 6e 67 20 77   If everything w
2cb5d 65 6e 74 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  ent according to
2cb5e 20 70 6c 61 6e 2c 20 6c 69 6e 6b 20 74 68 65 20   plan, link the 
2cb5f 6e 65 77 20 56 54 61 62 6c 65 20 73 74 72 75 63  new VTable struc
2cb60 74 75 72 65 0a 20 20 20 20 20 20 2a 2a 20 69 6e  ture.      ** in
2cb61 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69  to the linked li
2cb62 73 74 20 68 65 61 64 65 64 20 62 79 20 70 54 61  st headed by pTa
2cb63 62 2d 3e 70 56 54 61 62 6c 65 2e 20 54 68 65 6e  b->pVTable. Then
2cb64 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68   loop through th
2cb65 65 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6c 75  e .      ** colu
2cb66 6d 6e 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65  mns of the table
2cb67 20 74 6f 20 73 65 65 20 69 66 20 61 6e 79 20 6f   to see if any o
2cb68 66 20 74 68 65 6d 20 63 6f 6e 74 61 69 6e 20 74  f them contain t
2cb69 68 65 20 74 6f 6b 65 6e 20 22 68 69 64 64 65 6e  he token "hidden
2cb6a 22 2e 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 73  "..      ** If s
2cb6b 6f 2c 20 73 65 74 20 74 68 65 20 43 6f 6c 75 6d  o, set the Colum
2cb6c 6e 2e 69 73 48 69 64 64 65 6e 20 66 6c 61 67 20  n.isHidden flag 
2cb6d 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 65 20 74  and remove the t
2cb6e 6f 6b 65 6e 20 66 72 6f 6d 0a 20 20 20 20 20 20  oken from.      
2cb6f 2a 2a 20 74 68 65 20 74 79 70 65 20 73 74 72 69  ** the type stri
2cb70 6e 67 2e 20 20 2a 2f 0a 20 20 20 20 20 20 70 56  ng.  */.      pV
2cb71 54 61 62 6c 65 2d 3e 70 4e 65 78 74 20 3d 20 70  Table->pNext = p
2cb72 54 61 62 2d 3e 70 56 54 61 62 6c 65 3b 0a 20 20  Tab->pVTable;.  
2cb73 20 20 20 20 70 54 61 62 2d 3e 70 56 54 61 62 6c      pTab->pVTabl
2cb74 65 20 3d 20 70 56 54 61 62 6c 65 3b 0a 0a 20 20  e = pVTable;..  
2cb75 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20      for(iCol=0; 
2cb76 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  iCol<pTab->nCol;
2cb77 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20   iCol++){.      
2cb78 20 20 63 68 61 72 20 2a 7a 54 79 70 65 20 3d 20    char *zType = 
2cb79 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d  pTab->aCol[iCol]
2cb7a 2e 7a 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20  .zType;.        
2cb7b 69 6e 74 20 6e 54 79 70 65 3b 0a 20 20 20 20 20  int nType;.     
2cb7c 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20     int i = 0;.  
2cb7d 20 20 20 20 20 20 69 66 28 20 21 7a 54 79 70 65        if( !zType
2cb7e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
2cb7f 20 20 20 20 20 6e 54 79 70 65 20 3d 20 73 71 6c       nType = sql
2cb80 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 79  ite3Strlen30(zTy
2cb81 70 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  pe);.        if(
2cb82 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
2cb83 28 22 68 69 64 64 65 6e 22 2c 20 7a 54 79 70 65  ("hidden", zType
2cb84 2c 20 36 29 7c 7c 28 7a 54 79 70 65 5b 36 5d 20  , 6)||(zType[6] 
2cb85 26 26 20 7a 54 79 70 65 5b 36 5d 21 3d 27 20 27  && zType[6]!=' '
2cb86 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66  ) ){.          f
2cb87 6f 72 28 69 3d 30 3b 20 69 3c 6e 54 79 70 65 3b  or(i=0; i<nType;
2cb88 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
2cb89 20 20 20 69 66 28 20 28 30 3d 3d 73 71 6c 69 74     if( (0==sqlit
2cb8a 65 33 53 74 72 4e 49 43 6d 70 28 22 20 68 69 64  e3StrNICmp(" hid
2cb8b 64 65 6e 22 2c 20 26 7a 54 79 70 65 5b 69 5d 2c  den", &zType[i],
2cb8c 20 37 29 29 0a 20 20 20 20 20 20 20 20 20 20 20   7)).           
2cb8d 20 20 26 26 20 28 7a 54 79 70 65 5b 69 2b 37 5d    && (zType[i+7]
2cb8e 3d 3d 27 5c 30 27 20 7c 7c 20 7a 54 79 70 65 5b  =='\0' || zType[
2cb8f 69 2b 37 5d 3d 3d 27 20 27 29 0a 20 20 20 20 20  i+7]==' ').     
2cb90 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20         ){.      
2cb91 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20          i++;.   
2cb92 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
2cb93 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
2cb94 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2cb95 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
2cb96 28 20 69 3c 6e 54 79 70 65 20 29 7b 0a 20 20 20  ( i<nType ){.   
2cb97 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20         int j;.  
2cb98 20 20 20 20 20 20 20 20 69 6e 74 20 6e 44 65 6c          int nDel
2cb99 20 3d 20 36 20 2b 20 28 7a 54 79 70 65 5b 69 2b   = 6 + (zType[i+
2cb9a 36 5d 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20  6] ? 1 : 0);.   
2cb9b 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 69 3b 20         for(j=i; 
2cb9c 28 6a 2b 6e 44 65 6c 29 3c 3d 6e 54 79 70 65 3b  (j+nDel)<=nType;
2cb9d 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   j++){.         
2cb9e 20 20 20 7a 54 79 70 65 5b 6a 5d 20 3d 20 7a 54     zType[j] = zT
2cb9f 79 70 65 5b 6a 2b 6e 44 65 6c 5d 3b 0a 20 20 20  ype[j+nDel];.   
2cba0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2cba1 20 20 20 69 66 28 20 7a 54 79 70 65 5b 69 5d 3d     if( zType[i]=
2cba2 3d 27 5c 30 27 20 26 26 20 69 3e 30 20 29 7b 0a  ='\0' && i>0 ){.
2cba3 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65              asse
2cba4 72 74 28 7a 54 79 70 65 5b 69 2d 31 5d 3d 3d 27  rt(zType[i-1]=='
2cba5 20 27 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   ');.           
2cba6 20 7a 54 79 70 65 5b 69 2d 31 5d 20 3d 20 27 5c   zType[i-1] = '\
2cba7 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  0';.          }.
2cba8 20 20 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e            pTab->
2cba9 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73 48 69 64  aCol[iCol].isHid
2cbaa 64 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  den = 1;.       
2cbab 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
2cbac 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 44  .  }..  sqlite3D
2cbad 62 46 72 65 65 28 64 62 2c 20 7a 4d 6f 64 75 6c  bFree(db, zModul
2cbae 65 4e 61 6d 65 29 3b 0a 20 20 64 62 2d 3e 70 56  eName);.  db->pV
2cbaf 54 61 62 20 3d 20 30 3b 0a 20 20 72 65 74 75 72  Tab = 0;.  retur
2cbb0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
2cbb1 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
2cbb2 69 6e 76 6f 6b 65 64 20 62 79 20 74 68 65 20 70  invoked by the p
2cbb3 61 72 73 65 72 20 74 6f 20 63 61 6c 6c 20 74 68  arser to call th
2cbb4 65 20 78 43 6f 6e 6e 65 63 74 28 29 20 6d 65 74  e xConnect() met
2cbb5 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20 76 69  hod.** of the vi
2cbb6 72 74 75 61 6c 20 74 61 62 6c 65 20 70 54 61 62  rtual table pTab
2cbb7 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  . If an error oc
2cbb8 63 75 72 73 2c 20 61 6e 20 65 72 72 6f 72 20 63  curs, an error c
2cbb9 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20  ode is returned 
2cbba 0a 2a 2a 20 61 6e 64 20 61 6e 20 65 72 72 6f 72  .** and an error
2cbbb 20 6c 65 66 74 20 69 6e 20 70 50 61 72 73 65 2e   left in pParse.
2cbbc 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c  .**.** This call
2cbbd 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74   is a no-op if t
2cbbe 61 62 6c 65 20 70 54 61 62 20 69 73 20 6e 6f 74  able pTab is not
2cbbf 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
2cbc0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2cbc1 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
2cbc2 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 50  tabCallConnect(P
2cbc3 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61  arse *pParse, Ta
2cbc4 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 73 71  ble *pTab){.  sq
2cbc5 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2cbc6 73 65 2d 3e 64 62 3b 0a 20 20 63 6f 6e 73 74 20  se->db;.  const 
2cbc7 63 68 61 72 20 2a 7a 4d 6f 64 3b 0a 20 20 4d 6f  char *zMod;.  Mo
2cbc8 64 75 6c 65 20 2a 70 4d 6f 64 3b 0a 20 20 69 6e  dule *pMod;.  in
2cbc9 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
2cbca 20 70 54 61 62 20 29 3b 0a 20 20 69 66 28 20 28   pTab );.  if( (
2cbcb 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26  pTab->tabFlags &
2cbcc 20 54 46 5f 56 69 72 74 75 61 6c 29 3d 3d 30 20   TF_Virtual)==0 
2cbcd 7c 7c 20 73 71 6c 69 74 65 33 47 65 74 56 54 61  || sqlite3GetVTa
2cbce 62 6c 65 28 64 62 2c 20 70 54 61 62 29 20 29 7b  ble(db, pTab) ){
2cbcf 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
2cbd0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  TE_OK;.  }..  /*
2cbd1 20 4c 6f 63 61 74 65 20 74 68 65 20 72 65 71 75   Locate the requ
2cbd2 69 72 65 64 20 76 69 72 74 75 61 6c 20 74 61 62  ired virtual tab
2cbd3 6c 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 7a  le module */.  z
2cbd4 4d 6f 64 20 3d 20 70 54 61 62 2d 3e 61 7a 4d 6f  Mod = pTab->azMo
2cbd5 64 75 6c 65 41 72 67 5b 30 5d 3b 0a 20 20 70 4d  duleArg[0];.  pM
2cbd6 6f 64 20 3d 20 28 4d 6f 64 75 6c 65 2a 29 73 71  od = (Module*)sq
2cbd7 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 64  lite3HashFind(&d
2cbd8 62 2d 3e 61 4d 6f 64 75 6c 65 2c 20 7a 4d 6f 64  b->aModule, zMod
2cbd9 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  , sqlite3Strlen3
2cbda 30 28 7a 4d 6f 64 29 29 3b 0a 0a 20 20 69 66 28  0(zMod));..  if(
2cbdb 20 21 70 4d 6f 64 20 29 7b 0a 20 20 20 20 63 6f   !pMod ){.    co
2cbdc 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 75 6c  nst char *zModul
2cbdd 65 20 3d 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75  e = pTab->azModu
2cbde 6c 65 41 72 67 5b 30 5d 3b 0a 20 20 20 20 73 71  leArg[0];.    sq
2cbdf 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2cbe0 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 6d  arse, "no such m
2cbe1 6f 64 75 6c 65 3a 20 25 73 22 2c 20 7a 4d 6f 64  odule: %s", zMod
2cbe2 75 6c 65 29 3b 0a 20 20 20 20 72 63 20 3d 20 53  ule);.    rc = S
2cbe3 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
2cbe4 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a  else{.    char *
2cbe5 7a 45 72 72 20 3d 20 30 3b 0a 20 20 20 20 72 63  zErr = 0;.    rc
2cbe6 20 3d 20 76 74 61 62 43 61 6c 6c 43 6f 6e 73 74   = vtabCallConst
2cbe7 72 75 63 74 6f 72 28 64 62 2c 20 70 54 61 62 2c  ructor(db, pTab,
2cbe8 20 70 4d 6f 64 2c 20 70 4d 6f 64 2d 3e 70 4d 6f   pMod, pMod->pMo
2cbe9 64 75 6c 65 2d 3e 78 43 6f 6e 6e 65 63 74 2c 20  dule->xConnect, 
2cbea 26 7a 45 72 72 29 3b 0a 20 20 20 20 69 66 28 20  &zErr);.    if( 
2cbeb 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
2cbec 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
2cbed 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2cbee 25 73 22 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20  %s", zErr);.    
2cbef 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  }.    sqlite3DbF
2cbf0 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20  ree(db, zErr);. 
2cbf1 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
2cbf2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 68  .}../*.** Add th
2cbf3 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
2cbf4 70 56 54 61 62 20 74 6f 20 74 68 65 20 61 72 72  pVTab to the arr
2cbf5 61 79 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61  ay sqlite3.aVTra
2cbf6 6e 73 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ns[]..*/.static 
2cbf7 69 6e 74 20 61 64 64 54 6f 56 54 72 61 6e 73 28  int addToVTrans(
2cbf8 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 56 54 61  sqlite3 *db, VTa
2cbf9 62 6c 65 20 2a 70 56 54 61 62 29 7b 0a 20 20 63  ble *pVTab){.  c
2cbfa 6f 6e 73 74 20 69 6e 74 20 41 52 52 41 59 5f 49  onst int ARRAY_I
2cbfb 4e 43 52 20 3d 20 35 3b 0a 0a 20 20 2f 2a 20 47  NCR = 5;..  /* G
2cbfc 72 6f 77 20 74 68 65 20 73 71 6c 69 74 65 33 2e  row the sqlite3.
2cbfd 61 56 54 72 61 6e 73 20 61 72 72 61 79 20 69 66  aVTrans array if
2cbfe 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69   required */.  i
2cbff 66 28 20 28 64 62 2d 3e 6e 56 54 72 61 6e 73 25  f( (db->nVTrans%
2cc00 41 52 52 41 59 5f 49 4e 43 52 29 3d 3d 30 20 29  ARRAY_INCR)==0 )
2cc01 7b 0a 20 20 20 20 56 54 61 62 6c 65 20 2a 2a 61  {.    VTable **a
2cc02 56 54 72 61 6e 73 3b 0a 20 20 20 20 69 6e 74 20  VTrans;.    int 
2cc03 6e 42 79 74 65 73 20 3d 20 73 69 7a 65 6f 66 28  nBytes = sizeof(
2cc04 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29 20  sqlite3_vtab *) 
2cc05 2a 20 28 64 62 2d 3e 6e 56 54 72 61 6e 73 20 2b  * (db->nVTrans +
2cc06 20 41 52 52 41 59 5f 49 4e 43 52 29 3b 0a 20 20   ARRAY_INCR);.  
2cc07 20 20 61 56 54 72 61 6e 73 20 3d 20 73 71 6c 69    aVTrans = sqli
2cc08 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c  te3DbRealloc(db,
2cc09 20 28 76 6f 69 64 20 2a 29 64 62 2d 3e 61 56 54   (void *)db->aVT
2cc0a 72 61 6e 73 2c 20 6e 42 79 74 65 73 29 3b 0a 20  rans, nBytes);. 
2cc0b 20 20 20 69 66 28 20 21 61 56 54 72 61 6e 73 20     if( !aVTrans 
2cc0c 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
2cc0d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
2cc0e 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74 28 26    }.    memset(&
2cc0f 61 56 54 72 61 6e 73 5b 64 62 2d 3e 6e 56 54 72  aVTrans[db->nVTr
2cc10 61 6e 73 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28  ans], 0, sizeof(
2cc11 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29 2a  sqlite3_vtab *)*
2cc12 41 52 52 41 59 5f 49 4e 43 52 29 3b 0a 20 20 20  ARRAY_INCR);.   
2cc13 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d 20 61   db->aVTrans = a
2cc14 56 54 72 61 6e 73 3b 0a 20 20 7d 0a 0a 20 20 2f  VTrans;.  }..  /
2cc15 2a 20 41 64 64 20 70 56 74 61 62 20 74 6f 20 74  * Add pVtab to t
2cc16 68 65 20 65 6e 64 20 6f 66 20 73 71 6c 69 74 65  he end of sqlite
2cc17 33 2e 61 56 54 72 61 6e 73 20 2a 2f 0a 20 20 64  3.aVTrans */.  d
2cc18 62 2d 3e 61 56 54 72 61 6e 73 5b 64 62 2d 3e 6e  b->aVTrans[db->n
2cc19 56 54 72 61 6e 73 2b 2b 5d 20 3d 20 70 56 54 61  VTrans++] = pVTa
2cc1a 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62  b;.  sqlite3Vtab
2cc1b 4c 6f 63 6b 28 70 56 54 61 62 29 3b 0a 20 20 72  Lock(pVTab);.  r
2cc1c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2cc1d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
2cc1e 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b  unction is invok
2cc1f 65 64 20 62 79 20 74 68 65 20 76 64 62 65 20 74  ed by the vdbe t
2cc20 6f 20 63 61 6c 6c 20 74 68 65 20 78 43 72 65 61  o call the xCrea
2cc21 74 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20  te method.** of 
2cc22 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
2cc23 65 20 6e 61 6d 65 64 20 7a 54 61 62 20 69 6e 20  e named zTab in 
2cc24 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 0a 2a  database iDb. .*
2cc25 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72  *.** If an error
2cc26 20 6f 63 63 75 72 73 2c 20 2a 70 7a 45 72 72 20   occurs, *pzErr 
2cc27 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20  is set to point 
2cc28 61 6e 20 61 6e 20 45 6e 67 6c 69 73 68 20 6c 61  an an English la
2cc29 6e 67 75 61 67 65 0a 2a 2a 20 64 65 73 63 72 69  nguage.** descri
2cc2a 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 65 72 72  ption of the err
2cc2b 6f 72 20 61 6e 64 20 61 6e 20 53 51 4c 49 54 45  or and an SQLITE
2cc2c 5f 58 58 58 20 65 72 72 6f 72 20 63 6f 64 65 20  _XXX error code 
2cc2d 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20  is returned..** 
2cc2e 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
2cc2f 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63 61 6c   caller must cal
2cc30 6c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28  l sqlite3DbFree(
2cc31 64 62 2c 20 29 20 6f 6e 20 2a 70 7a 45 72 72 2e  db, ) on *pzErr.
2cc32 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2cc33 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74  TE int sqlite3Vt
2cc34 61 62 43 61 6c 6c 43 72 65 61 74 65 28 73 71 6c  abCallCreate(sql
2cc35 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44  ite3 *db, int iD
2cc36 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
2cc37 54 61 62 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72  Tab, char **pzEr
2cc38 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  r){.  int rc = S
2cc39 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 54 61 62 6c  QLITE_OK;.  Tabl
2cc3a 65 20 2a 70 54 61 62 3b 0a 20 20 4d 6f 64 75 6c  e *pTab;.  Modul
2cc3b 65 20 2a 70 4d 6f 64 3b 0a 20 20 63 6f 6e 73 74  e *pMod;.  const
2cc3c 20 63 68 61 72 20 2a 7a 4d 6f 64 3b 0a 0a 20 20   char *zMod;..  
2cc3d 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69  pTab = sqlite3Fi
2cc3e 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62  ndTable(db, zTab
2cc3f 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  , db->aDb[iDb].z
2cc40 4e 61 6d 65 29 3b 0a 20 20 61 73 73 65 72 74 28  Name);.  assert(
2cc41 20 70 54 61 62 20 26 26 20 28 70 54 61 62 2d 3e   pTab && (pTab->
2cc42 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69  tabFlags & TF_Vi
2cc43 72 74 75 61 6c 29 21 3d 30 20 26 26 20 21 70 54  rtual)!=0 && !pT
2cc44 61 62 2d 3e 70 56 54 61 62 6c 65 20 29 3b 0a 0a  ab->pVTable );..
2cc45 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20    /* Locate the 
2cc46 72 65 71 75 69 72 65 64 20 76 69 72 74 75 61 6c  required virtual
2cc47 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 2a 2f   table module */
2cc48 0a 20 20 7a 4d 6f 64 20 3d 20 70 54 61 62 2d 3e  .  zMod = pTab->
2cc49 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 3b 0a  azModuleArg[0];.
2cc4a 20 20 70 4d 6f 64 20 3d 20 28 4d 6f 64 75 6c 65    pMod = (Module
2cc4b 2a 29 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e  *)sqlite3HashFin
2cc4c 64 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65 2c 20  d(&db->aModule, 
2cc4d 7a 4d 6f 64 2c 20 73 71 6c 69 74 65 33 53 74 72  zMod, sqlite3Str
2cc4e 6c 65 6e 33 30 28 7a 4d 6f 64 29 29 3b 0a 0a 20  len30(zMod));.. 
2cc4f 20 2f 2a 20 49 66 20 74 68 65 20 6d 6f 64 75 6c   /* If the modul
2cc50 65 20 68 61 73 20 62 65 65 6e 20 72 65 67 69 73  e has been regis
2cc51 74 65 72 65 64 20 61 6e 64 20 69 6e 63 6c 75 64  tered and includ
2cc52 65 73 20 61 20 43 72 65 61 74 65 20 6d 65 74 68  es a Create meth
2cc53 6f 64 2c 20 0a 20 20 2a 2a 20 69 6e 76 6f 6b 65  od, .  ** invoke
2cc54 20 69 74 20 6e 6f 77 2e 20 49 66 20 74 68 65 20   it now. If the 
2cc55 6d 6f 64 75 6c 65 20 68 61 73 20 6e 6f 74 20 62  module has not b
2cc56 65 65 6e 20 72 65 67 69 73 74 65 72 65 64 2c 20  een registered, 
2cc57 72 65 74 75 72 6e 20 61 6e 20 0a 20 20 2a 2a 20  return an .  ** 
2cc58 65 72 72 6f 72 2e 20 4f 74 68 65 72 77 69 73 65  error. Otherwise
2cc59 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 0a 20 20  , do nothing..  
2cc5a 2a 2f 0a 20 20 69 66 28 20 21 70 4d 6f 64 20 29  */.  if( !pMod )
2cc5b 7b 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73  {.    *pzErr = s
2cc5c 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
2cc5d 2c 20 22 6e 6f 20 73 75 63 68 20 6d 6f 64 75 6c  , "no such modul
2cc5e 65 3a 20 25 73 22 2c 20 7a 4d 6f 64 29 3b 0a 20  e: %s", zMod);. 
2cc5f 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
2cc60 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  RROR;.  }else{. 
2cc61 20 20 20 72 63 20 3d 20 76 74 61 62 43 61 6c 6c     rc = vtabCall
2cc62 43 6f 6e 73 74 72 75 63 74 6f 72 28 64 62 2c 20  Constructor(db, 
2cc63 70 54 61 62 2c 20 70 4d 6f 64 2c 20 70 4d 6f 64  pTab, pMod, pMod
2cc64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 43 72 65 61  ->pModule->xCrea
2cc65 74 65 2c 20 70 7a 45 72 72 29 3b 0a 20 20 7d 0a  te, pzErr);.  }.
2cc66 0a 20 20 2f 2a 20 4a 75 73 74 69 66 69 63 61 74  .  /* Justificat
2cc67 69 6f 6e 20 6f 66 20 41 4c 57 41 59 53 28 29 3a  ion of ALWAYS():
2cc68 20 20 54 68 65 20 78 43 6f 6e 73 74 72 75 63 74    The xConstruct
2cc69 6f 72 20 6d 65 74 68 6f 64 20 69 73 20 72 65 71  or method is req
2cc6a 75 69 72 65 64 20 74 6f 0a 20 20 2a 2a 20 63 72  uired to.  ** cr
2cc6b 65 61 74 65 20 61 20 76 61 6c 69 64 20 73 71 6c  eate a valid sql
2cc6c 69 74 65 33 5f 76 74 61 62 20 69 66 20 69 74 20  ite3_vtab if it 
2cc6d 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f  returns SQLITE_O
2cc6e 4b 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d  K. */.  if( rc==
2cc6f 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 41 4c 57  SQLITE_OK && ALW
2cc70 41 59 53 28 73 71 6c 69 74 65 33 47 65 74 56 54  AYS(sqlite3GetVT
2cc71 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29 29 20  able(db, pTab)) 
2cc72 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 64  ){.      rc = ad
2cc73 64 54 6f 56 54 72 61 6e 73 28 64 62 2c 20 73 71  dToVTrans(db, sq
2cc74 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 64  lite3GetVTable(d
2cc75 62 2c 20 70 54 61 62 29 29 3b 0a 20 20 7d 0a 0a  b, pTab));.  }..
2cc76 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
2cc77 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
2cc78 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 73  ion is used to s
2cc79 65 74 20 74 68 65 20 73 63 68 65 6d 61 20 6f 66  et the schema of
2cc7a 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
2cc7b 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 0a 2a 2a  .  It is only.**
2cc7c 20 76 61 6c 69 64 20 74 6f 20 63 61 6c 6c 20 74   valid to call t
2cc7d 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 72 6f  his function fro
2cc7e 6d 20 77 69 74 68 69 6e 20 74 68 65 20 78 43 72  m within the xCr
2cc7f 65 61 74 65 28 29 20 6f 72 20 78 43 6f 6e 6e 65  eate() or xConne
2cc80 63 74 28 29 20 6f 66 20 61 0a 2a 2a 20 76 69 72  ct() of a.** vir
2cc81 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
2cc82 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
2cc83 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 65 63   int sqlite3_dec
2cc84 6c 61 72 65 5f 76 74 61 62 28 73 71 6c 69 74 65  lare_vtab(sqlite
2cc85 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
2cc86 72 20 2a 7a 43 72 65 61 74 65 54 61 62 6c 65 29  r *zCreateTable)
2cc87 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  {.  Parse *pPars
2cc88 65 3b 0a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  e;..  int rc = S
2cc89 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 54 61 62 6c  QLITE_OK;.  Tabl
2cc8a 65 20 2a 70 54 61 62 3b 0a 20 20 63 68 61 72 20  e *pTab;.  char 
2cc8b 2a 7a 45 72 72 20 3d 20 30 3b 0a 0a 20 20 73 71  *zErr = 0;..  sq
2cc8c 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
2cc8d 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
2cc8e 70 54 61 62 20 3d 20 64 62 2d 3e 70 56 54 61 62  pTab = db->pVTab
2cc8f 3b 0a 20 20 69 66 28 20 21 70 54 61 62 20 29 7b  ;.  if( !pTab ){
2cc90 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
2cc91 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4d 49 53  r(db, SQLITE_MIS
2cc92 55 53 45 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  USE, 0);.    sql
2cc93 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
2cc94 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  (db->mutex);.   
2cc95 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d   return SQLITE_M
2cc96 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 61 73 73  ISUSE;.  }.  ass
2cc97 65 72 74 28 20 28 70 54 61 62 2d 3e 74 61 62 46  ert( (pTab->tabF
2cc98 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74 75 61  lags & TF_Virtua
2cc99 6c 29 21 3d 30 20 29 3b 0a 0a 20 20 70 50 61 72  l)!=0 );..  pPar
2cc9a 73 65 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63  se = sqlite3Stac
2cc9b 6b 41 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  kAllocZero(db, s
2cc9c 69 7a 65 6f 66 28 2a 70 50 61 72 73 65 29 29 3b  izeof(*pParse));
2cc9d 0a 20 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30  .  if( pParse==0
2cc9e 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
2cc9f 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c  ITE_NOMEM;.  }el
2cca0 73 65 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  se{.    pParse->
2cca1 64 65 63 6c 61 72 65 56 74 61 62 20 3d 20 31 3b  declareVtab = 1;
2cca2 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62 20  .    pParse->db 
2cca3 3d 20 64 62 3b 0a 20 20 0a 20 20 20 20 69 66 28  = db;.  .    if(
2cca4 20 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 45   .        SQLITE
2cca5 5f 4f 4b 20 3d 3d 20 73 71 6c 69 74 65 33 52 75  _OK == sqlite3Ru
2cca6 6e 50 61 72 73 65 72 28 70 50 61 72 73 65 2c 20  nParser(pParse, 
2cca7 7a 43 72 65 61 74 65 54 61 62 6c 65 2c 20 26 7a  zCreateTable, &z
2cca8 45 72 72 29 20 26 26 20 0a 20 20 20 20 20 20 20  Err) && .       
2cca9 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
2ccaa 6c 65 20 26 26 20 0a 20 20 20 20 20 20 20 20 21  le && .        !
2ccab 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
2ccac 65 2d 3e 70 53 65 6c 65 63 74 20 26 26 20 0a 20  e->pSelect && . 
2ccad 20 20 20 20 20 20 20 28 70 50 61 72 73 65 2d 3e         (pParse->
2ccae 70 4e 65 77 54 61 62 6c 65 2d 3e 74 61 62 46 6c  pNewTable->tabFl
2ccaf 61 67 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c  ags & TF_Virtual
2ccb0 29 3d 3d 30 0a 20 20 20 20 29 7b 0a 20 20 20 20  )==0.    ){.    
2ccb1 20 20 69 66 28 20 21 70 54 61 62 2d 3e 61 43 6f    if( !pTab->aCo
2ccb2 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 61  l ){.        pTa
2ccb3 62 2d 3e 61 43 6f 6c 20 3d 20 70 50 61 72 73 65  b->aCol = pParse
2ccb4 2d 3e 70 4e 65 77 54 61 62 6c 65 2d 3e 61 43 6f  ->pNewTable->aCo
2ccb5 6c 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d  l;.        pTab-
2ccb6 3e 6e 43 6f 6c 20 3d 20 70 50 61 72 73 65 2d 3e  >nCol = pParse->
2ccb7 70 4e 65 77 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3b  pNewTable->nCol;
2ccb8 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d  .        pParse-
2ccb9 3e 70 4e 65 77 54 61 62 6c 65 2d 3e 6e 43 6f 6c  >pNewTable->nCol
2ccba 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70 50   = 0;.        pP
2ccbb 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2d  arse->pNewTable-
2ccbc 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20  >aCol = 0;.     
2ccbd 20 7d 0a 20 20 20 20 20 20 64 62 2d 3e 70 56 54   }.      db->pVT
2ccbe 61 62 20 3d 20 30 3b 0a 20 20 20 20 7d 20 65 6c  ab = 0;.    } el
2ccbf 73 65 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  se {.      sqlit
2ccc0 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49  e3Error(db, SQLI
2ccc1 54 45 5f 45 52 52 4f 52 2c 20 7a 45 72 72 29 3b  TE_ERROR, zErr);
2ccc2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
2ccc3 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a  Free(db, zErr);.
2ccc4 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
2ccc5 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20  E_ERROR;.    }. 
2ccc6 20 20 20 70 50 61 72 73 65 2d 3e 64 65 63 6c 61     pParse->decla
2ccc7 72 65 56 74 61 62 20 3d 20 30 3b 0a 20 20 0a 20  reVtab = 0;.  . 
2ccc8 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 70     if( pParse->p
2ccc9 56 64 62 65 20 29 7b 0a 20 20 20 20 20 20 73 71  Vdbe ){.      sq
2ccca 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a  lite3VdbeFinaliz
2cccb 65 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 29  e(pParse->pVdbe)
2cccc 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
2cccd 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70  te3DeleteTable(p
2ccce 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2cccf 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74  );.    sqlite3St
2ccd0 61 63 6b 46 72 65 65 28 64 62 2c 20 70 50 61 72  ackFree(db, pPar
2ccd1 73 65 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65  se);.  }..  asse
2ccd2 72 74 28 20 28 72 63 26 30 78 66 66 29 3d 3d 72  rt( (rc&0xff)==r
2ccd3 63 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  c );.  rc = sqli
2ccd4 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72  te3ApiExit(db, r
2ccd5 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  c);.  sqlite3_mu
2ccd6 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
2ccd7 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
2ccd8 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
2ccd9 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76   function is inv
2ccda 6f 6b 65 64 20 62 79 20 74 68 65 20 76 64 62 65  oked by the vdbe
2ccdb 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 78 44 65   to call the xDe
2ccdc 73 74 72 6f 79 20 6d 65 74 68 6f 64 0a 2a 2a 20  stroy method.** 
2ccdd 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  of the virtual t
2ccde 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 61 62 20  able named zTab 
2ccdf 69 6e 20 64 61 74 61 62 61 73 65 20 69 44 62 2e  in database iDb.
2cce0 20 54 68 69 73 20 6f 63 63 75 72 73 0a 2a 2a 20   This occurs.** 
2cce1 77 68 65 6e 20 61 20 44 52 4f 50 20 54 41 42 4c  when a DROP TABL
2cce2 45 20 69 73 20 6d 65 6e 74 69 6f 6e 65 64 2e 0a  E is mentioned..
2cce3 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20  **.** This call 
2cce4 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 7a 54  is a no-op if zT
2cce5 61 62 20 69 73 20 6e 6f 74 20 61 20 76 69 72 74  ab is not a virt
2cce6 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51  ual table..*/.SQ
2cce7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2cce8 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c   sqlite3VtabCall
2cce9 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33 20  Destroy(sqlite3 
2ccea 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f  *db, int iDb, co
2cceb 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 29 7b  nst char *zTab){
2ccec 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
2cced 54 45 5f 4f 4b 3b 0a 20 20 54 61 62 6c 65 20 2a  TE_OK;.  Table *
2ccee 70 54 61 62 3b 0a 0a 20 20 70 54 61 62 20 3d 20  pTab;..  pTab = 
2ccef 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
2ccf0 28 64 62 2c 20 7a 54 61 62 2c 20 64 62 2d 3e 61  (db, zTab, db->a
2ccf1 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a  Db[iDb].zName);.
2ccf2 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 54 61    if( ALWAYS(pTa
2ccf3 62 21 3d 30 20 26 26 20 70 54 61 62 2d 3e 70 56  b!=0 && pTab->pV
2ccf4 54 61 62 6c 65 21 3d 30 29 20 29 7b 0a 20 20 20  Table!=0) ){.   
2ccf5 20 56 54 61 62 6c 65 20 2a 70 20 3d 20 76 74 61   VTable *p = vta
2ccf6 62 44 69 73 63 6f 6e 6e 65 63 74 41 6c 6c 28 64  bDisconnectAll(d
2ccf7 62 2c 20 70 54 61 62 29 3b 0a 0a 20 20 20 20 72  b, pTab);..    r
2ccf8 63 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65 74  c = sqlite3Safet
2ccf9 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 61 73  yOff(db);.    as
2ccfa 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
2ccfb 5f 4f 4b 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  _OK );.    rc = 
2ccfc 70 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65  p->pMod->pModule
2ccfd 2d 3e 78 44 65 73 74 72 6f 79 28 70 2d 3e 70 56  ->xDestroy(p->pV
2ccfe 74 61 62 29 3b 0a 20 20 20 20 28 76 6f 69 64 29  tab);.    (void)
2ccff 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
2cd00 64 62 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 6d  db);..    /* Rem
2cd01 6f 76 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ove the sqlite3_
2cd02 76 74 61 62 2a 20 66 72 6f 6d 20 74 68 65 20 61  vtab* from the a
2cd03 56 54 72 61 6e 73 5b 5d 20 61 72 72 61 79 2c 20  VTrans[] array, 
2cd04 69 66 20 61 70 70 6c 69 63 61 62 6c 65 20 2a 2f  if applicable */
2cd05 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
2cd06 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
2cd07 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 70 56  assert( pTab->pV
2cd08 54 61 62 6c 65 3d 3d 70 20 26 26 20 70 2d 3e 70  Table==p && p->p
2cd09 4e 65 78 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20  Next==0 );.     
2cd0a 20 70 2d 3e 70 56 74 61 62 20 3d 20 30 3b 0a 20   p->pVtab = 0;. 
2cd0b 20 20 20 20 20 70 54 61 62 2d 3e 70 56 54 61 62       pTab->pVTab
2cd0c 6c 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71  le = 0;.      sq
2cd0d 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28  lite3VtabUnlock(
2cd0e 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  p);.    }.  }.. 
2cd0f 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
2cd10 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
2cd11 6f 6e 20 69 6e 76 6f 6b 65 73 20 65 69 74 68 65  on invokes eithe
2cd12 72 20 74 68 65 20 78 52 6f 6c 6c 62 61 63 6b 20  r the xRollback 
2cd13 6f 72 20 78 43 6f 6d 6d 69 74 20 6d 65 74 68 6f  or xCommit metho
2cd14 64 0a 2a 2a 20 6f 66 20 65 61 63 68 20 6f 66 20  d.** of each of 
2cd15 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
2cd16 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65  es in the sqlite
2cd17 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61 79 2e  3.aVTrans array.
2cd18 20 54 68 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 63   The method.** c
2cd19 61 6c 6c 65 64 20 69 73 20 69 64 65 6e 74 69 66  alled is identif
2cd1a 69 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e  ied by the secon
2cd1b 64 20 61 72 67 75 6d 65 6e 74 2c 20 22 6f 66 66  d argument, "off
2cd1c 73 65 74 22 2c 20 77 68 69 63 68 20 69 73 0a 2a  set", which is.*
2cd1d 2a 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66 20  * the offset of 
2cd1e 74 68 65 20 6d 65 74 68 6f 64 20 74 6f 20 63 61  the method to ca
2cd1f 6c 6c 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65  ll in the sqlite
2cd20 33 5f 6d 6f 64 75 6c 65 20 73 74 72 75 63 74 75  3_module structu
2cd21 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 72  re..**.** The ar
2cd22 72 61 79 20 69 73 20 63 6c 65 61 72 65 64 20 61  ray is cleared a
2cd23 66 74 65 72 20 69 6e 76 6f 6b 69 6e 67 20 74 68  fter invoking th
2cd24 65 20 63 61 6c 6c 62 61 63 6b 73 2e 20 0a 2a 2f  e callbacks. .*/
2cd25 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 61 6c  .static void cal
2cd26 6c 46 69 6e 61 6c 69 73 65 72 28 73 71 6c 69 74  lFinaliser(sqlit
2cd27 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f 66 66 73  e3 *db, int offs
2cd28 65 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  et){.  int i;.  
2cd29 69 66 28 20 64 62 2d 3e 61 56 54 72 61 6e 73 20  if( db->aVTrans 
2cd2a 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ){.    for(i=0; 
2cd2b 69 3c 64 62 2d 3e 6e 56 54 72 61 6e 73 3b 20 69  i<db->nVTrans; i
2cd2c 2b 2b 29 7b 0a 20 20 20 20 20 20 56 54 61 62 6c  ++){.      VTabl
2cd2d 65 20 2a 70 56 54 61 62 20 3d 20 64 62 2d 3e 61  e *pVTab = db->a
2cd2e 56 54 72 61 6e 73 5b 69 5d 3b 0a 20 20 20 20 20  VTrans[i];.     
2cd2f 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
2cd30 20 3d 20 70 56 54 61 62 2d 3e 70 56 74 61 62 3b   = pVTab->pVtab;
2cd31 0a 20 20 20 20 20 20 69 66 28 20 70 20 29 7b 0a  .      if( p ){.
2cd32 20 20 20 20 20 20 20 20 69 6e 74 20 28 2a 78 29          int (*x)
2cd33 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29  (sqlite3_vtab *)
2cd34 3b 0a 20 20 20 20 20 20 20 20 78 20 3d 20 2a 28  ;.        x = *(
2cd35 69 6e 74 20 28 2a 2a 29 28 73 71 6c 69 74 65 33  int (**)(sqlite3
2cd36 5f 76 74 61 62 20 2a 29 29 28 28 63 68 61 72 20  _vtab *))((char 
2cd37 2a 29 70 2d 3e 70 4d 6f 64 75 6c 65 20 2b 20 6f  *)p->pModule + o
2cd38 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20  ffset);.        
2cd39 69 66 28 20 78 20 29 20 78 28 70 29 3b 0a 20 20  if( x ) x(p);.  
2cd3a 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
2cd3b 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 70 56  te3VtabUnlock(pV
2cd3c 54 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Tab);.    }.    
2cd3d 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2cd3e 2c 20 64 62 2d 3e 61 56 54 72 61 6e 73 29 3b 0a  , db->aVTrans);.
2cd3f 20 20 20 20 64 62 2d 3e 6e 56 54 72 61 6e 73 20      db->nVTrans 
2cd40 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 61 56 54  = 0;.    db->aVT
2cd41 72 61 6e 73 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a  rans = 0;.  }.}.
2cd42 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68  ./*.** Invoke th
2cd43 65 20 78 53 79 6e 63 20 6d 65 74 68 6f 64 20 6f  e xSync method o
2cd44 66 20 61 6c 6c 20 76 69 72 74 75 61 6c 20 74 61  f all virtual ta
2cd45 62 6c 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69  bles in the sqli
2cd46 74 65 33 2e 61 56 54 72 61 6e 73 0a 2a 2a 20 61  te3.aVTrans.** a
2cd47 72 72 61 79 2e 20 52 65 74 75 72 6e 20 74 68 65  rray. Return the
2cd48 20 65 72 72 6f 72 20 63 6f 64 65 20 66 6f 72 20   error code for 
2cd49 74 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 20  the first error 
2cd4a 74 68 61 74 20 6f 63 63 75 72 73 2c 20 6f 72 0a  that occurs, or.
2cd4b 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20  ** SQLITE_OK if 
2cd4c 61 6c 6c 20 78 53 79 6e 63 20 6f 70 65 72 61 74  all xSync operat
2cd4d 69 6f 6e 73 20 61 72 65 20 73 75 63 63 65 73 73  ions are success
2cd4e 66 75 6c 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 20 2a  ful..**.** Set *
2cd4f 70 7a 45 72 72 6d 73 67 20 74 6f 20 70 6f 69 6e  pzErrmsg to poin
2cd50 74 20 74 6f 20 61 20 62 75 66 66 65 72 20 74 68  t to a buffer th
2cd51 61 74 20 73 68 6f 75 6c 64 20 62 65 20 72 65 6c  at should be rel
2cd52 65 61 73 65 64 20 75 73 69 6e 67 20 0a 2a 2a 20  eased using .** 
2cd53 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 29 20  sqlite3DbFree() 
2cd54 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6e 20 65 72  containing an er
2cd55 72 6f 72 20 6d 65 73 73 61 67 65 2c 20 69 66 20  ror message, if 
2cd56 6f 6e 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65  one is available
2cd57 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2cd58 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
2cd59 74 61 62 53 79 6e 63 28 73 71 6c 69 74 65 33 20  tabSync(sqlite3 
2cd5a 2a 64 62 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72  *db, char **pzEr
2cd5b 72 6d 73 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  rmsg){.  int i;.
2cd5c 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
2cd5d 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 63 73 61  E_OK;.  int rcsa
2cd5e 66 65 74 79 3b 0a 20 20 56 54 61 62 6c 65 20 2a  fety;.  VTable *
2cd5f 2a 61 56 54 72 61 6e 73 20 3d 20 64 62 2d 3e 61  *aVTrans = db->a
2cd60 56 54 72 61 6e 73 3b 0a 0a 20 20 72 63 20 3d 20  VTrans;..  rc = 
2cd61 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
2cd62 28 64 62 29 3b 0a 20 20 64 62 2d 3e 61 56 54 72  (db);.  db->aVTr
2cd63 61 6e 73 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69  ans = 0;.  for(i
2cd64 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
2cd65 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 56 54 72 61  K && i<db->nVTra
2cd66 6e 73 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e  ns; i++){.    in
2cd67 74 20 28 2a 78 29 28 73 71 6c 69 74 65 33 5f 76  t (*x)(sqlite3_v
2cd68 74 61 62 20 2a 29 3b 0a 20 20 20 20 73 71 6c 69  tab *);.    sqli
2cd69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 20  te3_vtab *pVtab 
2cd6a 3d 20 61 56 54 72 61 6e 73 5b 69 5d 2d 3e 70 56  = aVTrans[i]->pV
2cd6b 74 61 62 3b 0a 20 20 20 20 69 66 28 20 70 56 74  tab;.    if( pVt
2cd6c 61 62 20 26 26 20 28 78 20 3d 20 70 56 74 61 62  ab && (x = pVtab
2cd6d 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 53 79 6e 63  ->pModule->xSync
2cd6e 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63  )!=0 ){.      rc
2cd6f 20 3d 20 78 28 70 56 74 61 62 29 3b 0a 20 20 20   = x(pVtab);.   
2cd70 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2cd71 28 64 62 2c 20 2a 70 7a 45 72 72 6d 73 67 29 3b  (db, *pzErrmsg);
2cd72 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 6d 73 67  .      *pzErrmsg
2cd73 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73   = pVtab->zErrMs
2cd74 67 3b 0a 20 20 20 20 20 20 70 56 74 61 62 2d 3e  g;.      pVtab->
2cd75 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20  zErrMsg = 0;.   
2cd76 20 7d 0a 20 20 7d 0a 20 20 64 62 2d 3e 61 56 54   }.  }.  db->aVT
2cd77 72 61 6e 73 20 3d 20 61 56 54 72 61 6e 73 3b 0a  rans = aVTrans;.
2cd78 20 20 72 63 73 61 66 65 74 79 20 3d 20 73 71 6c    rcsafety = sql
2cd79 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
2cd7a 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ;..  if( rc==SQL
2cd7b 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63  ITE_OK ){.    rc
2cd7c 20 3d 20 72 63 73 61 66 65 74 79 3b 0a 20 20 7d   = rcsafety;.  }
2cd7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2cd7e 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68  ./*.** Invoke th
2cd7f 65 20 78 52 6f 6c 6c 62 61 63 6b 20 6d 65 74 68  e xRollback meth
2cd80 6f 64 20 6f 66 20 61 6c 6c 20 76 69 72 74 75 61  od of all virtua
2cd81 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  l tables in the 
2cd82 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 61 56 54 72  .** sqlite3.aVTr
2cd83 61 6e 73 20 61 72 72 61 79 2e 20 54 68 65 6e 20  ans array. Then 
2cd84 63 6c 65 61 72 20 74 68 65 20 61 72 72 61 79 20  clear the array 
2cd85 69 74 73 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49 54  itself..*/.SQLIT
2cd86 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2cd87 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63  lite3VtabRollbac
2cd88 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  k(sqlite3 *db){.
2cd89 20 20 63 61 6c 6c 46 69 6e 61 6c 69 73 65 72 28    callFinaliser(
2cd8a 64 62 2c 20 6f 66 66 73 65 74 6f 66 28 73 71 6c  db, offsetof(sql
2cd8b 69 74 65 33 5f 6d 6f 64 75 6c 65 2c 78 52 6f 6c  ite3_module,xRol
2cd8c 6c 62 61 63 6b 29 29 3b 0a 20 20 72 65 74 75 72  lback));.  retur
2cd8d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
2cd8e 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65  /*.** Invoke the
2cd8f 20 78 43 6f 6d 6d 69 74 20 6d 65 74 68 6f 64 20   xCommit method 
2cd90 6f 66 20 61 6c 6c 20 76 69 72 74 75 61 6c 20 74  of all virtual t
2cd91 61 62 6c 65 73 20 69 6e 20 74 68 65 20 0a 2a 2a  ables in the .**
2cd92 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73   sqlite3.aVTrans
2cd93 20 61 72 72 61 79 2e 20 54 68 65 6e 20 63 6c 65   array. Then cle
2cd94 61 72 20 74 68 65 20 61 72 72 61 79 20 69 74 73  ar the array its
2cd95 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  elf..*/.SQLITE_P
2cd96 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2cd97 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 73 71 6c  e3VtabCommit(sql
2cd98 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 63 61 6c  ite3 *db){.  cal
2cd99 6c 46 69 6e 61 6c 69 73 65 72 28 64 62 2c 20 6f  lFinaliser(db, o
2cd9a 66 66 73 65 74 6f 66 28 73 71 6c 69 74 65 33 5f  ffsetof(sqlite3_
2cd9b 6d 6f 64 75 6c 65 2c 78 43 6f 6d 6d 69 74 29 29  module,xCommit))
2cd9c 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
2cd9d 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  E_OK;.}../*.** I
2cd9e 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  f the virtual ta
2cd9f 62 6c 65 20 70 56 74 61 62 20 73 75 70 70 6f 72  ble pVtab suppor
2cda0 74 73 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ts the transacti
2cda1 6f 6e 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20  on interface.** 
2cda2 28 78 42 65 67 69 6e 2f 78 52 6f 6c 6c 62 61 63  (xBegin/xRollbac
2cda3 6b 2f 78 43 6f 6d 6d 69 74 20 61 6e 64 20 6f 70  k/xCommit and op
2cda4 74 69 6f 6e 61 6c 6c 79 20 78 53 79 6e 63 29 20  tionally xSync) 
2cda5 61 6e 64 20 61 20 74 72 61 6e 73 61 63 74 69 6f  and a transactio
2cda6 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20 63 75 72 72  n is.** not curr
2cda7 65 6e 74 6c 79 20 6f 70 65 6e 2c 20 69 6e 76 6f  ently open, invo
2cda8 6b 65 20 74 68 65 20 78 42 65 67 69 6e 20 6d 65  ke the xBegin me
2cda9 74 68 6f 64 20 6e 6f 77 2e 0a 2a 2a 0a 2a 2a 20  thod now..**.** 
2cdaa 49 66 20 74 68 65 20 78 42 65 67 69 6e 20 63 61  If the xBegin ca
2cdab 6c 6c 20 69 73 20 73 75 63 63 65 73 73 66 75 6c  ll is successful
2cdac 2c 20 70 6c 61 63 65 20 74 68 65 20 73 71 6c 69  , place the sqli
2cdad 74 65 33 5f 76 74 61 62 20 70 6f 69 6e 74 65 72  te3_vtab pointer
2cdae 0a 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c 69 74  .** in the sqlit
2cdaf 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61 79  e3.aVTrans array
2cdb0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2cdb1 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
2cdb2 74 61 62 42 65 67 69 6e 28 73 71 6c 69 74 65 33  tabBegin(sqlite3
2cdb3 20 2a 64 62 2c 20 56 54 61 62 6c 65 20 2a 70 56   *db, VTable *pV
2cdb4 54 61 62 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  Tab){.  int rc =
2cdb5 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 6f   SQLITE_OK;.  co
2cdb6 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
2cdb7 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20  le *pModule;..  
2cdb8 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65 3a  /* Special case:
2cdb9 20 49 66 20 64 62 2d 3e 61 56 54 72 61 6e 73 20   If db->aVTrans 
2cdba 69 73 20 4e 55 4c 4c 20 61 6e 64 20 64 62 2d 3e  is NULL and db->
2cdbb 6e 56 54 72 61 6e 73 20 69 73 20 67 72 65 61 74  nVTrans is great
2cdbc 65 72 0a 20 20 2a 2a 20 74 68 61 6e 20 7a 65 72  er.  ** than zer
2cdbd 6f 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e  o, then this fun
2cdbe 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 63  ction is being c
2cdbf 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69  alled from withi
2cdc0 6e 20 61 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c  n a.  ** virtual
2cdc1 20 6d 6f 64 75 6c 65 20 78 53 79 6e 63 28 29 20   module xSync() 
2cdc2 63 61 6c 6c 62 61 63 6b 2e 20 49 74 20 69 73 20  callback. It is 
2cdc3 69 6c 6c 65 67 61 6c 20 74 6f 20 77 72 69 74 65  illegal to write
2cdc4 20 74 6f 20 0a 20 20 2a 2a 20 76 69 72 74 75 61   to .  ** virtua
2cdc5 6c 20 6d 6f 64 75 6c 65 20 74 61 62 6c 65 73 20  l module tables 
2cdc6 69 6e 20 74 68 69 73 20 63 61 73 65 2c 20 73 6f  in this case, so
2cdc7 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c   return SQLITE_L
2cdc8 4f 43 4b 45 44 2e 0a 20 20 2a 2f 0a 20 20 69 66  OCKED..  */.  if
2cdc9 28 20 73 71 6c 69 74 65 33 56 74 61 62 49 6e 53  ( sqlite3VtabInS
2cdca 79 6e 63 28 64 62 29 20 29 7b 0a 20 20 20 20 72  ync(db) ){.    r
2cdcb 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43  eturn SQLITE_LOC
2cdcc 4b 45 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21  KED;.  }.  if( !
2cdcd 70 56 54 61 62 20 29 7b 0a 20 20 20 20 72 65 74  pVTab ){.    ret
2cdce 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
2cdcf 20 7d 20 0a 20 20 70 4d 6f 64 75 6c 65 20 3d 20   } .  pModule = 
2cdd0 70 56 54 61 62 2d 3e 70 56 74 61 62 2d 3e 70 4d  pVTab->pVtab->pM
2cdd1 6f 64 75 6c 65 3b 0a 0a 20 20 69 66 28 20 70 4d  odule;..  if( pM
2cdd2 6f 64 75 6c 65 2d 3e 78 42 65 67 69 6e 20 29 7b  odule->xBegin ){
2cdd3 0a 20 20 20 20 69 6e 74 20 69 3b 0a 0a 0a 20 20  .    int i;...  
2cdd4 20 20 2f 2a 20 49 66 20 70 56 74 61 62 20 69 73    /* If pVtab is
2cdd5 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
2cdd6 61 56 54 72 61 6e 73 20 61 72 72 61 79 2c 20 72  aVTrans array, r
2cdd7 65 74 75 72 6e 20 65 61 72 6c 79 20 2a 2f 0a 20  eturn early */. 
2cdd8 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62     for(i=0; i<db
2cdd9 2d 3e 6e 56 54 72 61 6e 73 3b 20 69 2b 2b 29 7b  ->nVTrans; i++){
2cdda 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 61  .      if( db->a
2cddb 56 54 72 61 6e 73 5b 69 5d 3d 3d 70 56 54 61 62  VTrans[i]==pVTab
2cddc 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
2cddd 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
2cdde 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
2cddf 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78   /* Invoke the x
2cde0 42 65 67 69 6e 20 6d 65 74 68 6f 64 20 2a 2f 0a  Begin method */.
2cde1 20 20 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65      rc = pModule
2cde2 2d 3e 78 42 65 67 69 6e 28 70 56 54 61 62 2d 3e  ->xBegin(pVTab->
2cde3 70 56 74 61 62 29 3b 0a 20 20 20 20 69 66 28 20  pVtab);.    if( 
2cde4 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
2cde5 0a 20 20 20 20 20 20 72 63 20 3d 20 61 64 64 54  .      rc = addT
2cde6 6f 56 54 72 61 6e 73 28 64 62 2c 20 70 56 54 61  oVTrans(db, pVTa
2cde7 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  b);.    }.  }.  
2cde8 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
2cde9 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61  .** The first pa
2cdea 72 61 6d 65 74 65 72 20 28 70 44 65 66 29 20 69  rameter (pDef) i
2cdeb 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  s a function imp
2cdec 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68  lementation.  Th
2cded 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61  e.** second para
2cdee 6d 65 74 65 72 20 28 70 45 78 70 72 29 20 69 73  meter (pExpr) is
2cdef 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
2cdf0 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63  ent to this func
2cdf1 74 69 6f 6e 2e 0a 2a 2a 20 49 66 20 70 45 78 70  tion..** If pExp
2cdf2 72 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 69 6e  r is a column in
2cdf3 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
2cdf4 2c 20 74 68 65 6e 20 6c 65 74 20 74 68 65 20 76  , then let the v
2cdf5 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20  irtual.** table 
2cdf6 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 68  implementation h
2cdf7 61 76 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69  ave an opportuni
2cdf8 74 79 20 74 6f 20 6f 76 65 72 6c 6f 61 64 20 74  ty to overload t
2cdf9 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a  he function..**.
2cdfa 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2cdfb 69 73 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 77  is used to allow
2cdfc 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69   virtual table i
2cdfd 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 74  mplementations t
2cdfe 6f 0a 2a 2a 20 6f 76 65 72 6c 6f 61 64 20 4d 41  o.** overload MA
2cdff 54 43 48 2c 20 4c 49 4b 45 2c 20 47 4c 4f 42 2c  TCH, LIKE, GLOB,
2ce00 20 61 6e 64 20 52 45 47 45 58 50 20 6f 70 65 72   and REGEXP oper
2ce01 61 74 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  ators..**.** Ret
2ce02 75 72 6e 20 65 69 74 68 65 72 20 74 68 65 20 70  urn either the p
2ce03 44 65 66 20 61 72 67 75 6d 65 6e 74 20 28 69 6e  Def argument (in
2ce04 64 69 63 61 74 69 6e 67 20 6e 6f 20 63 68 61 6e  dicating no chan
2ce05 67 65 29 20 6f 72 20 61 20 0a 2a 2a 20 6e 65 77  ge) or a .** new
2ce06 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75   FuncDef structu
2ce07 72 65 20 74 68 61 74 20 69 73 20 6d 61 72 6b 65  re that is marke
2ce08 64 20 61 73 20 65 70 68 65 6d 65 72 61 6c 20 75  d as ephemeral u
2ce09 73 69 6e 67 20 74 68 65 0a 2a 2a 20 53 51 4c 49  sing the.** SQLI
2ce0a 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 20 66 6c  TE_FUNC_EPHEM fl
2ce0b 61 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ag..*/.SQLITE_PR
2ce0c 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a 73  IVATE FuncDef *s
2ce0d 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c 6f  qlite3VtabOverlo
2ce0e 61 64 46 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71  adFunction(.  sq
2ce0f 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 2f 2a  lite3 *db,    /*
2ce10 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
2ce11 74 69 6f 6e 20 66 6f 72 20 72 65 70 6f 72 74 69  tion for reporti
2ce12 6e 67 20 6d 61 6c 6c 6f 63 20 70 72 6f 62 6c 65  ng malloc proble
2ce13 6d 73 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20  ms */.  FuncDef 
2ce14 2a 70 44 65 66 2c 20 20 2f 2a 20 46 75 6e 63 74  *pDef,  /* Funct
2ce15 69 6f 6e 20 74 6f 20 70 6f 73 73 69 62 6c 79 20  ion to possibly 
2ce16 6f 76 65 72 6c 6f 61 64 20 2a 2f 0a 20 20 69 6e  overload */.  in
2ce17 74 20 6e 41 72 67 2c 20 20 20 20 20 20 20 2f 2a  t nArg,       /*
2ce18 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   Number of argum
2ce19 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e 63  ents to the func
2ce1a 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a  tion */.  Expr *
2ce1b 70 45 78 70 72 20 20 20 20 20 2f 2a 20 46 69 72  pExpr     /* Fir
2ce1c 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  st argument to t
2ce1d 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29  he function */.)
2ce1e 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  {.  Table *pTab;
2ce1f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
2ce20 2a 70 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65  *pVtab;.  sqlite
2ce21 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 0a  3_module *pMod;.
2ce22 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28    void (*xFunc)(
2ce23 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
2ce24 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
2ce25 75 65 2a 2a 29 20 3d 20 30 3b 0a 20 20 76 6f 69  ue**) = 0;.  voi
2ce26 64 20 2a 70 41 72 67 20 3d 20 30 3b 0a 20 20 46  d *pArg = 0;.  F
2ce27 75 6e 63 44 65 66 20 2a 70 4e 65 77 3b 0a 20 20  uncDef *pNew;.  
2ce28 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 63 68  int rc = 0;.  ch
2ce29 61 72 20 2a 7a 4c 6f 77 65 72 4e 61 6d 65 3b 0a  ar *zLowerName;.
2ce2a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
2ce2b 2a 7a 3b 0a 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  *z;...  /* Check
2ce2c 20 74 6f 20 73 65 65 20 74 68 65 20 6c 65 66 74   to see the left
2ce2d 20 6f 70 65 72 61 6e 64 20 69 73 20 61 20 63 6f   operand is a co
2ce2e 6c 75 6d 6e 20 69 6e 20 61 20 76 69 72 74 75 61  lumn in a virtua
2ce2f 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 66 28  l table */.  if(
2ce30 20 4e 45 56 45 52 28 70 45 78 70 72 3d 3d 30 29   NEVER(pExpr==0)
2ce31 20 29 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a   ) return pDef;.
2ce32 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21    if( pExpr->op!
2ce33 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20 72 65 74  =TK_COLUMN ) ret
2ce34 75 72 6e 20 70 44 65 66 3b 0a 20 20 70 54 61 62  urn pDef;.  pTab
2ce35 20 3d 20 70 45 78 70 72 2d 3e 70 54 61 62 3b 0a   = pExpr->pTab;.
2ce36 20 20 69 66 28 20 4e 45 56 45 52 28 70 54 61 62    if( NEVER(pTab
2ce37 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 20 70 44  ==0) ) return pD
2ce38 65 66 3b 0a 20 20 69 66 28 20 28 70 54 61 62 2d  ef;.  if( (pTab-
2ce39 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56  >tabFlags & TF_V
2ce3a 69 72 74 75 61 6c 29 3d 3d 30 20 29 20 72 65 74  irtual)==0 ) ret
2ce3b 75 72 6e 20 70 44 65 66 3b 0a 20 20 70 56 74 61  urn pDef;.  pVta
2ce3c 62 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 54  b = sqlite3GetVT
2ce3d 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29 2d 3e  able(db, pTab)->
2ce3e 70 56 74 61 62 3b 0a 20 20 61 73 73 65 72 74 28  pVtab;.  assert(
2ce3f 20 70 56 74 61 62 21 3d 30 20 29 3b 0a 20 20 61   pVtab!=0 );.  a
2ce40 73 73 65 72 74 28 20 70 56 74 61 62 2d 3e 70 4d  ssert( pVtab->pM
2ce41 6f 64 75 6c 65 21 3d 30 20 29 3b 0a 20 20 70 4d  odule!=0 );.  pM
2ce42 6f 64 20 3d 20 28 73 71 6c 69 74 65 33 5f 6d 6f  od = (sqlite3_mo
2ce43 64 75 6c 65 20 2a 29 70 56 74 61 62 2d 3e 70 4d  dule *)pVtab->pM
2ce44 6f 64 75 6c 65 3b 0a 20 20 69 66 28 20 70 4d 6f  odule;.  if( pMo
2ce45 64 2d 3e 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e  d->xFindFunction
2ce46 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 70 44 65  ==0 ) return pDe
2ce47 66 3b 0a 20 0a 20 20 2f 2a 20 43 61 6c 6c 20 74  f;. .  /* Call t
2ce48 68 65 20 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e  he xFindFunction
2ce49 20 6d 65 74 68 6f 64 20 6f 6e 20 74 68 65 20 76   method on the v
2ce4a 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70  irtual table imp
2ce4b 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 20 20 2a 2a  lementation.  **
2ce4c 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 69   to see if the i
2ce4d 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 61  mplementation wa
2ce4e 6e 74 73 20 74 6f 20 6f 76 65 72 6c 6f 61 64 20  nts to overload 
2ce4f 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 0a 20  this function . 
2ce50 20 2a 2f 0a 20 20 7a 4c 6f 77 65 72 4e 61 6d 65   */.  zLowerName
2ce51 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
2ce52 75 70 28 64 62 2c 20 70 44 65 66 2d 3e 7a 4e 61  up(db, pDef->zNa
2ce53 6d 65 29 3b 0a 20 20 69 66 28 20 7a 4c 6f 77 65  me);.  if( zLowe
2ce54 72 4e 61 6d 65 20 29 7b 0a 20 20 20 20 66 6f 72  rName ){.    for
2ce55 28 7a 3d 28 75 6e 73 69 67 6e 65 64 20 63 68 61  (z=(unsigned cha
2ce56 72 2a 29 7a 4c 6f 77 65 72 4e 61 6d 65 3b 20 2a  r*)zLowerName; *
2ce57 7a 3b 20 7a 2b 2b 29 7b 0a 20 20 20 20 20 20 2a  z; z++){.      *
2ce58 7a 20 3d 20 73 71 6c 69 74 65 33 55 70 70 65 72  z = sqlite3Upper
2ce59 54 6f 4c 6f 77 65 72 5b 2a 7a 5d 3b 0a 20 20 20  ToLower[*z];.   
2ce5a 20 7d 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f 64   }.    rc = pMod
2ce5b 2d 3e 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28  ->xFindFunction(
2ce5c 70 56 74 61 62 2c 20 6e 41 72 67 2c 20 7a 4c 6f  pVtab, nArg, zLo
2ce5d 77 65 72 4e 61 6d 65 2c 20 26 78 46 75 6e 63 2c  werName, &xFunc,
2ce5e 20 26 70 41 72 67 29 3b 0a 20 20 20 20 73 71 6c   &pArg);.    sql
2ce5f 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2ce60 4c 6f 77 65 72 4e 61 6d 65 29 3b 0a 20 20 7d 0a  LowerName);.  }.
2ce61 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20    if( rc==0 ){. 
2ce62 20 20 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a     return pDef;.
2ce63 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65    }..  /* Create
2ce64 20 61 20 6e 65 77 20 65 70 68 65 6d 65 72 61 6c   a new ephemeral
2ce65 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69   function defini
2ce66 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6f 76 65  tion for the ove
2ce67 72 6c 6f 61 64 65 64 0a 20 20 2a 2a 20 66 75 6e  rloaded.  ** fun
2ce68 63 74 69 6f 6e 20 2a 2f 0a 20 20 70 4e 65 77 20  ction */.  pNew 
2ce69 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2ce6a 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
2ce6b 28 2a 70 4e 65 77 29 0a 20 20 20 20 20 20 20 20  (*pNew).        
2ce6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ce6d 20 20 20 20 20 2b 20 73 71 6c 69 74 65 33 53 74       + sqlite3St
2ce6e 72 6c 65 6e 33 30 28 70 44 65 66 2d 3e 7a 4e 61  rlen30(pDef->zNa
2ce6f 6d 65 29 20 2b 20 31 29 3b 0a 20 20 69 66 28 20  me) + 1);.  if( 
2ce70 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 72  pNew==0 ){.    r
2ce71 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 20 7d 0a  eturn pDef;.  }.
2ce72 20 20 2a 70 4e 65 77 20 3d 20 2a 70 44 65 66 3b    *pNew = *pDef;
2ce73 0a 20 20 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d  .  pNew->zName =
2ce74 20 28 63 68 61 72 20 2a 29 26 70 4e 65 77 5b 31   (char *)&pNew[1
2ce75 5d 3b 0a 20 20 6d 65 6d 63 70 79 28 70 4e 65 77  ];.  memcpy(pNew
2ce76 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66 2d 3e 7a  ->zName, pDef->z
2ce77 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72  Name, sqlite3Str
2ce78 6c 65 6e 33 30 28 70 44 65 66 2d 3e 7a 4e 61 6d  len30(pDef->zNam
2ce79 65 29 2b 31 29 3b 0a 20 20 70 4e 65 77 2d 3e 78  e)+1);.  pNew->x
2ce7a 46 75 6e 63 20 3d 20 78 46 75 6e 63 3b 0a 20 20  Func = xFunc;.  
2ce7b 70 4e 65 77 2d 3e 70 55 73 65 72 44 61 74 61 20  pNew->pUserData 
2ce7c 3d 20 70 41 72 67 3b 0a 20 20 70 4e 65 77 2d 3e  = pArg;.  pNew->
2ce7d 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f  flags |= SQLITE_
2ce7e 46 55 4e 43 5f 45 50 48 45 4d 3b 0a 20 20 72 65  FUNC_EPHEM;.  re
2ce7f 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a  turn pNew;.}../*
2ce80 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 76 69  .** Make sure vi
2ce81 72 74 75 61 6c 20 74 61 62 6c 65 20 70 54 61 62  rtual table pTab
2ce82 20 69 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e   is contained in
2ce83 20 74 68 65 20 70 50 61 72 73 65 2d 3e 61 70 56   the pParse->apV
2ce84 69 72 74 75 61 6c 4c 6f 63 6b 5b 5d 0a 2a 2a 20  irtualLock[].** 
2ce85 61 72 72 61 79 20 73 6f 20 74 68 61 74 20 61 6e  array so that an
2ce86 20 4f 50 5f 56 42 65 67 69 6e 20 77 69 6c 6c 20   OP_VBegin will 
2ce87 67 65 74 20 67 65 6e 65 72 61 74 65 64 20 66 6f  get generated fo
2ce88 72 20 69 74 2e 20 20 41 64 64 20 70 54 61 62 20  r it.  Add pTab 
2ce89 74 6f 20 74 68 65 0a 2a 2a 20 61 72 72 61 79 20  to the.** array 
2ce8a 69 66 20 69 74 20 69 73 20 6d 69 73 73 69 6e 67  if it is missing
2ce8b 2e 20 20 49 66 20 70 54 61 62 20 69 73 20 61 6c  .  If pTab is al
2ce8c 72 65 61 64 79 20 69 6e 20 74 68 65 20 61 72 72  ready in the arr
2ce8d 61 79 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ay, this routine
2ce8e 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  .** is a no-op..
2ce8f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2ce90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
2ce91 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 50  abMakeWritable(P
2ce92 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61  arse *pParse, Ta
2ce93 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 50 61  ble *pTab){.  Pa
2ce94 72 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20 3d  rse *pToplevel =
2ce95 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70   sqlite3ParseTop
2ce96 6c 65 76 65 6c 28 70 50 61 72 73 65 29 3b 0a 20  level(pParse);. 
2ce97 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 54 61 62   int i, n;.  Tab
2ce98 6c 65 20 2a 2a 61 70 56 74 61 62 4c 6f 63 6b 3b  le **apVtabLock;
2ce99 0a 0a 20 20 61 73 73 65 72 74 28 20 49 73 56 69  ..  assert( IsVi
2ce9a 72 74 75 61 6c 28 70 54 61 62 29 20 29 3b 0a 20  rtual(pTab) );. 
2ce9b 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 6f 70   for(i=0; i<pTop
2ce9c 6c 65 76 65 6c 2d 3e 6e 56 74 61 62 4c 6f 63 6b  level->nVtabLock
2ce9d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
2ce9e 70 54 61 62 3d 3d 70 54 6f 70 6c 65 76 65 6c 2d  pTab==pToplevel-
2ce9f 3e 61 70 56 74 61 62 4c 6f 63 6b 5b 69 5d 20 29  >apVtabLock[i] )
2cea0 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6e   return;.  }.  n
2cea1 20 3d 20 28 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e   = (pToplevel->n
2cea2 56 74 61 62 4c 6f 63 6b 2b 31 29 2a 73 69 7a 65  VtabLock+1)*size
2cea3 6f 66 28 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 70  of(pToplevel->ap
2cea4 56 74 61 62 4c 6f 63 6b 5b 30 5d 29 3b 0a 20 20  VtabLock[0]);.  
2cea5 61 70 56 74 61 62 4c 6f 63 6b 20 3d 20 73 71 6c  apVtabLock = sql
2cea6 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 54 6f  ite3_realloc(pTo
2cea7 70 6c 65 76 65 6c 2d 3e 61 70 56 74 61 62 4c 6f  plevel->apVtabLo
2cea8 63 6b 2c 20 6e 29 3b 0a 20 20 69 66 28 20 61 70  ck, n);.  if( ap
2cea9 56 74 61 62 4c 6f 63 6b 20 29 7b 0a 20 20 20 20  VtabLock ){.    
2ceaa 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 70 56 74 61  pToplevel->apVta
2ceab 62 4c 6f 63 6b 20 3d 20 61 70 56 74 61 62 4c 6f  bLock = apVtabLo
2ceac 63 6b 3b 0a 20 20 20 20 70 54 6f 70 6c 65 76 65  ck;.    pTopleve
2cead 6c 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 5b 70 54  l->apVtabLock[pT
2ceae 6f 70 6c 65 76 65 6c 2d 3e 6e 56 74 61 62 4c 6f  oplevel->nVtabLo
2ceaf 63 6b 2b 2b 5d 20 3d 20 70 54 61 62 3b 0a 20 20  ck++] = pTab;.  
2ceb0 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 6f 70 6c  }else{.    pTopl
2ceb1 65 76 65 6c 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  evel->db->malloc
2ceb2 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  Failed = 1;.  }.
2ceb3 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
2ceb4 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2ceb5 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  TABLE */../*****
2ceb6 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
2ceb7 20 76 74 61 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   vtab.c ********
2ceb8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ceb9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ceba 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
2cebb 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
2cebc 66 69 6c 65 20 77 68 65 72 65 2e 63 20 2a 2a 2a  file where.c ***
2cebd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cebe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cebf 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
2cec0 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
2cec1 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
2cec2 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
2cec3 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
2cec4 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
2cec5 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
2cec6 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
2cec7 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
2cec8 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
2cec9 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
2ceca 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
2cecb 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
2cecc 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
2cecd 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
2cece 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
2cecf 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
2ced0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
2ced1 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
2ced2 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
2ced3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ced4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ced5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ced6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ced7 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65 20  .** This module 
2ced8 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20  contains C code 
2ced9 74 68 61 74 20 67 65 6e 65 72 61 74 65 73 20 56  that generates V
2ceda 44 42 45 20 63 6f 64 65 20 75 73 65 64 20 74 6f  DBE code used to
2cedb 20 70 72 6f 63 65 73 73 0a 2a 2a 20 74 68 65 20   process.** the 
2cedc 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20  WHERE clause of 
2cedd 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 20  SQL statements. 
2cede 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20   This module is 
2cedf 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 0a  responsible for.
2cee0 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67 20 74 68  ** generating th
2cee1 65 20 63 6f 64 65 20 74 68 61 74 20 6c 6f 6f 70  e code that loop
2cee2 73 20 74 68 72 6f 75 67 68 20 61 20 74 61 62 6c  s through a tabl
2cee3 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 70  e looking for ap
2cee4 70 6c 69 63 61 62 6c 65 0a 2a 2a 20 72 6f 77 73  plicable.** rows
2cee5 2e 20 20 49 6e 64 69 63 65 73 20 61 72 65 20 73  .  Indices are s
2cee6 65 6c 65 63 74 65 64 20 61 6e 64 20 75 73 65 64  elected and used
2cee7 20 74 6f 20 73 70 65 65 64 20 74 68 65 20 73 65   to speed the se
2cee8 61 72 63 68 20 77 68 65 6e 20 64 6f 69 6e 67 0a  arch when doing.
2cee9 2a 2a 20 73 6f 20 69 73 20 61 70 70 6c 69 63 61  ** so is applica
2ceea 62 6c 65 2e 20 20 42 65 63 61 75 73 65 20 74 68  ble.  Because th
2ceeb 69 73 20 6d 6f 64 75 6c 65 20 69 73 20 72 65 73  is module is res
2ceec 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 73 65 6c  ponsible for sel
2ceed 65 63 74 69 6e 67 0a 2a 2a 20 69 6e 64 69 63 65  ecting.** indice
2ceee 73 2c 20 79 6f 75 20 6d 69 67 68 74 20 61 6c 73  s, you might als
2ceef 6f 20 74 68 69 6e 6b 20 6f 66 20 74 68 69 73 20  o think of this 
2cef0 6d 6f 64 75 6c 65 20 61 73 20 74 68 65 20 22 71  module as the "q
2cef1 75 65 72 79 20 6f 70 74 69 6d 69 7a 65 72 22 2e  uery optimizer".
2cef2 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 77 68 65 72  .**.** $Id: wher
2cef3 65 2e 63 2c 76 20 31 2e 34 31 31 20 32 30 30 39  e.c,v 1.411 2009
2cef4 2f 30 37 2f 33 31 20 30 36 3a 31 34 3a 35 32 20  /07/31 06:14:52 
2cef5 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
2cef6 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 63  $.*/../*.** Trac
2cef7 65 20 6f 75 74 70 75 74 20 6d 61 63 72 6f 73 0a  e output macros.
2cef8 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  */.#if defined(S
2cef9 51 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 64  QLITE_TEST) || d
2cefa 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
2cefb 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 56  BUG).SQLITE_PRIV
2cefc 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
2cefd 68 65 72 65 54 72 61 63 65 20 3d 20 30 3b 0a 23  hereTrace = 0;.#
2cefe 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65  endif.#if define
2ceff 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26  d(SQLITE_TEST) &
2cf00 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  & defined(SQLITE
2cf01 5f 44 45 42 55 47 29 0a 23 20 64 65 66 69 6e 65  _DEBUG).# define
2cf02 20 57 48 45 52 45 54 52 41 43 45 28 58 29 20 20   WHERETRACE(X)  
2cf03 69 66 28 73 71 6c 69 74 65 33 57 68 65 72 65 54  if(sqlite3WhereT
2cf04 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62  race) sqlite3Deb
2cf05 75 67 50 72 69 6e 74 66 20 58 0a 23 65 6c 73 65  ugPrintf X.#else
2cf06 0a 23 20 64 65 66 69 6e 65 20 57 48 45 52 45 54  .# define WHERET
2cf07 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66 0a 0a  RACE(X).#endif..
2cf08 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72  /* Forward refer
2cf09 65 6e 63 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ence.*/.typedef 
2cf0a 73 74 72 75 63 74 20 57 68 65 72 65 43 6c 61 75  struct WhereClau
2cf0b 73 65 20 57 68 65 72 65 43 6c 61 75 73 65 3b 0a  se WhereClause;.
2cf0c 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57  typedef struct W
2cf0d 68 65 72 65 4d 61 73 6b 53 65 74 20 57 68 65 72  hereMaskSet Wher
2cf0e 65 4d 61 73 6b 53 65 74 3b 0a 74 79 70 65 64 65  eMaskSet;.typede
2cf0f 66 20 73 74 72 75 63 74 20 57 68 65 72 65 4f 72  f struct WhereOr
2cf10 49 6e 66 6f 20 57 68 65 72 65 4f 72 49 6e 66 6f  Info WhereOrInfo
2cf11 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
2cf12 20 57 68 65 72 65 41 6e 64 49 6e 66 6f 20 57 68   WhereAndInfo Wh
2cf13 65 72 65 41 6e 64 49 6e 66 6f 3b 0a 74 79 70 65  ereAndInfo;.type
2cf14 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65  def struct Where
2cf15 43 6f 73 74 20 57 68 65 72 65 43 6f 73 74 3b 0a  Cost WhereCost;.
2cf16 0a 2f 2a 0a 2a 2a 20 54 68 65 20 71 75 65 72 79  ./*.** The query
2cf17 20 67 65 6e 65 72 61 74 6f 72 20 75 73 65 73 20   generator uses 
2cf18 61 6e 20 61 72 72 61 79 20 6f 66 20 69 6e 73 74  an array of inst
2cf19 61 6e 63 65 73 20 6f 66 20 74 68 69 73 20 73 74  ances of this st
2cf1a 72 75 63 74 75 72 65 20 74 6f 0a 2a 2a 20 68 65  ructure to.** he
2cf1b 6c 70 20 69 74 20 61 6e 61 6c 79 7a 65 20 74 68  lp it analyze th
2cf1c 65 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73  e subexpressions
2cf1d 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c   of the WHERE cl
2cf1e 61 75 73 65 2e 20 20 45 61 63 68 20 57 48 45 52  ause.  Each WHER
2cf1f 45 0a 2a 2a 20 63 6c 61 75 73 65 20 73 75 62 65  E.** clause sube
2cf20 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73 65 70  xpression is sep
2cf21 61 72 61 74 65 64 20 66 72 6f 6d 20 74 68 65 20  arated from the 
2cf22 6f 74 68 65 72 73 20 62 79 20 41 4e 44 20 6f 70  others by AND op
2cf23 65 72 61 74 6f 72 73 2c 0a 2a 2a 20 75 73 75 61  erators,.** usua
2cf24 6c 6c 79 2c 20 6f 72 20 73 6f 6d 65 74 69 6d 65  lly, or sometime
2cf25 73 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73  s subexpressions
2cf26 20 73 65 70 61 72 61 74 65 64 20 62 79 20 4f 52   separated by OR
2cf27 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 57 68 65 72  ..**.** All Wher
2cf28 65 54 65 72 6d 73 20 61 72 65 20 63 6f 6c 6c 65  eTerms are colle
2cf29 63 74 65 64 20 69 6e 74 6f 20 61 20 73 69 6e 67  cted into a sing
2cf2a 6c 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 73  le WhereClause s
2cf2b 74 72 75 63 74 75 72 65 2e 20 20 0a 2a 2a 20 54  tructure.  .** T
2cf2c 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 64 65  he following ide
2cf2d 6e 74 69 74 79 20 68 6f 6c 64 73 3a 0a 2a 2a 0a  ntity holds:.**.
2cf2e 2a 2a 20 20 20 20 20 20 20 20 57 68 65 72 65 54  **        WhereT
2cf2f 65 72 6d 2e 70 57 43 2d 3e 61 5b 57 68 65 72 65  erm.pWC->a[Where
2cf30 54 65 72 6d 2e 69 64 78 5d 20 3d 3d 20 57 68 65  Term.idx] == Whe
2cf31 72 65 54 65 72 6d 0a 2a 2a 0a 2a 2a 20 57 68 65  reTerm.**.** Whe
2cf32 6e 20 61 20 74 65 72 6d 20 69 73 20 6f 66 20 74  n a term is of t
2cf33 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20  he form:.**.**  
2cf34 20 20 20 20 20 20 20 20 20 20 20 20 58 20 3c 6f              X <o
2cf35 70 3e 20 3c 65 78 70 72 3e 0a 2a 2a 0a 2a 2a 20  p> <expr>.**.** 
2cf36 77 68 65 72 65 20 58 20 69 73 20 61 20 63 6f 6c  where X is a col
2cf37 75 6d 6e 20 6e 61 6d 65 20 61 6e 64 20 3c 6f 70  umn name and <op
2cf38 3e 20 69 73 20 6f 6e 65 20 6f 66 20 63 65 72 74  > is one of cert
2cf39 61 69 6e 20 6f 70 65 72 61 74 6f 72 73 2c 0a 2a  ain operators,.*
2cf3a 2a 20 74 68 65 6e 20 57 68 65 72 65 54 65 72 6d  * then WhereTerm
2cf3b 2e 6c 65 66 74 43 75 72 73 6f 72 20 61 6e 64 20  .leftCursor and 
2cf3c 57 68 65 72 65 54 65 72 6d 2e 75 2e 6c 65 66 74  WhereTerm.u.left
2cf3d 43 6f 6c 75 6d 6e 20 72 65 63 6f 72 64 20 74 68  Column record th
2cf3e 65 0a 2a 2a 20 63 75 72 73 6f 72 20 6e 75 6d 62  e.** cursor numb
2cf3f 65 72 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e 75  er and column nu
2cf40 6d 62 65 72 20 66 6f 72 20 58 2e 20 20 57 68 65  mber for X.  Whe
2cf41 72 65 54 65 72 6d 2e 65 4f 70 65 72 61 74 6f 72  reTerm.eOperator
2cf42 20 72 65 63 6f 72 64 73 0a 2a 2a 20 74 68 65 20   records.** the 
2cf43 3c 6f 70 3e 20 75 73 69 6e 67 20 61 20 62 69 74  <op> using a bit
2cf44 6d 61 73 6b 20 65 6e 63 6f 64 69 6e 67 20 64 65  mask encoding de
2cf45 66 69 6e 65 64 20 62 79 20 57 4f 5f 78 78 78 20  fined by WO_xxx 
2cf46 62 65 6c 6f 77 2e 20 20 54 68 65 0a 2a 2a 20 75  below.  The.** u
2cf47 73 65 20 6f 66 20 61 20 62 69 74 6d 61 73 6b 20  se of a bitmask 
2cf48 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65  encoding for the
2cf49 20 6f 70 65 72 61 74 6f 72 20 61 6c 6c 6f 77 73   operator allows
2cf4a 20 75 73 20 74 6f 20 73 65 61 72 63 68 0a 2a 2a   us to search.**
2cf4b 20 71 75 69 63 6b 6c 79 20 66 6f 72 20 74 65 72   quickly for ter
2cf4c 6d 73 20 74 68 61 74 20 6d 61 74 63 68 20 61 6e  ms that match an
2cf4d 79 20 6f 66 20 73 65 76 65 72 61 6c 20 64 69 66  y of several dif
2cf4e 66 65 72 65 6e 74 20 6f 70 65 72 61 74 6f 72 73  ferent operators
2cf4f 2e 0a 2a 2a 0a 2a 2a 20 41 20 57 68 65 72 65 54  ..**.** A WhereT
2cf50 65 72 6d 20 6d 69 67 68 74 20 61 6c 73 6f 20 62  erm might also b
2cf51 65 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 73 75  e two or more su
2cf52 62 74 65 72 6d 73 20 63 6f 6e 6e 65 63 74 65 64  bterms connected
2cf53 20 62 79 20 4f 52 3a 0a 2a 2a 0a 2a 2a 20 20 20   by OR:.**.**   
2cf54 20 20 20 20 20 20 28 74 31 2e 58 20 3c 6f 70 3e        (t1.X <op>
2cf55 20 3c 65 78 70 72 3e 29 20 4f 52 20 28 74 31 2e   <expr>) OR (t1.
2cf56 59 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 29 20 4f  Y <op> <expr>) O
2cf57 52 20 2e 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  R .....**.** In 
2cf58 74 68 69 73 20 73 65 63 6f 6e 64 20 63 61 73 65  this second case
2cf59 2c 20 77 74 46 6c 61 67 20 61 73 20 74 68 65 20  , wtFlag as the 
2cf5a 54 45 52 4d 5f 4f 52 49 4e 46 4f 20 73 65 74 20  TERM_ORINFO set 
2cf5b 61 6e 64 20 65 4f 70 65 72 61 74 6f 72 3d 3d 57  and eOperator==W
2cf5c 4f 5f 4f 52 0a 2a 2a 20 61 6e 64 20 74 68 65 20  O_OR.** and the 
2cf5d 57 68 65 72 65 54 65 72 6d 2e 75 2e 70 4f 72 49  WhereTerm.u.pOrI
2cf5e 6e 66 6f 20 66 69 65 6c 64 20 70 6f 69 6e 74 73  nfo field points
2cf5f 20 74 6f 20 61 75 78 69 6c 69 61 72 79 20 69 6e   to auxiliary in
2cf60 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 0a 2a  formation that.*
2cf61 2a 20 69 73 20 63 6f 6c 6c 65 63 74 65 64 20 61  * is collected a
2cf62 62 6f 75 74 20 74 68 65 0a 2a 2a 0a 2a 2a 20 49  bout the.**.** I
2cf63 66 20 61 20 74 65 72 6d 20 69 6e 20 74 68 65 20  f a term in the 
2cf64 57 48 45 52 45 20 63 6c 61 75 73 65 20 64 6f 65  WHERE clause doe
2cf65 73 20 6e 6f 74 20 6d 61 74 63 68 20 65 69 74 68  s not match eith
2cf66 65 72 20 6f 66 20 74 68 65 20 74 77 6f 20 70 72  er of the two pr
2cf67 65 76 69 6f 75 73 0a 2a 2a 20 63 61 74 65 67 6f  evious.** catego
2cf68 72 69 65 73 2c 20 74 68 65 6e 20 65 4f 70 65 72  ries, then eOper
2cf69 61 74 6f 72 3d 3d 30 2e 20 20 54 68 65 20 57 68  ator==0.  The Wh
2cf6a 65 72 65 54 65 72 6d 2e 70 45 78 70 72 20 66 69  ereTerm.pExpr fi
2cf6b 65 6c 64 20 69 73 20 73 74 69 6c 6c 20 73 65 74  eld is still set
2cf6c 0a 2a 2a 20 74 6f 20 74 68 65 20 6f 72 69 67 69  .** to the origi
2cf6d 6e 61 6c 20 73 75 62 65 78 70 72 65 73 73 69 6f  nal subexpressio
2cf6e 6e 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 77 74  n content and wt
2cf6f 46 6c 61 67 73 20 69 73 20 73 65 74 20 75 70 20  Flags is set up 
2cf70 61 70 70 72 6f 70 72 69 61 74 65 6c 79 0a 2a 2a  appropriately.**
2cf71 20 62 75 74 20 6e 6f 20 6f 74 68 65 72 20 66 69   but no other fi
2cf72 65 6c 64 73 20 69 6e 20 74 68 65 20 57 68 65 72  elds in the Wher
2cf73 65 54 65 72 6d 20 6f 62 6a 65 63 74 20 61 72 65  eTerm object are
2cf74 20 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2a 0a   meaningful..**.
2cf75 2a 2a 20 57 68 65 6e 20 65 4f 70 65 72 61 74 6f  ** When eOperato
2cf76 72 21 3d 30 2c 20 70 72 65 72 65 71 52 69 67 68  r!=0, prereqRigh
2cf77 74 20 61 6e 64 20 70 72 65 72 65 71 41 6c 6c 20  t and prereqAll 
2cf78 72 65 63 6f 72 64 20 73 65 74 73 20 6f 66 20 63  record sets of c
2cf79 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 2c 0a 2a  ursor numbers,.*
2cf7a 2a 20 62 75 74 20 74 68 65 79 20 64 6f 20 73 6f  * but they do so
2cf7b 20 69 6e 64 69 72 65 63 74 6c 79 2e 20 20 41 20   indirectly.  A 
2cf7c 73 69 6e 67 6c 65 20 57 68 65 72 65 4d 61 73 6b  single WhereMask
2cf7d 53 65 74 20 73 74 72 75 63 74 75 72 65 20 74 72  Set structure tr
2cf7e 61 6e 73 6c 61 74 65 73 0a 2a 2a 20 63 75 72 73  anslates.** curs
2cf7f 6f 72 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 62  or number into b
2cf80 69 74 73 20 61 6e 64 20 74 68 65 20 74 72 61 6e  its and the tran
2cf81 73 6c 61 74 65 64 20 62 69 74 20 69 73 20 73 74  slated bit is st
2cf82 6f 72 65 64 20 69 6e 20 74 68 65 20 70 72 65 72  ored in the prer
2cf83 65 71 0a 2a 2a 20 66 69 65 6c 64 73 2e 20 20 54  eq.** fields.  T
2cf84 68 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 69  he translation i
2cf85 73 20 75 73 65 64 20 69 6e 20 6f 72 64 65 72 20  s used in order 
2cf86 74 6f 20 6d 61 78 69 6d 69 7a 65 20 74 68 65 20  to maximize the 
2cf87 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 69 74  number of.** bit
2cf88 73 20 74 68 61 74 20 77 69 6c 6c 20 66 69 74 20  s that will fit 
2cf89 69 6e 20 61 20 42 69 74 6d 61 73 6b 2e 20 20 54  in a Bitmask.  T
2cf8a 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e  he VDBE cursor n
2cf8b 75 6d 62 65 72 73 20 6d 69 67 68 74 20 62 65 0a  umbers might be.
2cf8c 2a 2a 20 73 70 72 65 61 64 20 6f 75 74 20 6f 76  ** spread out ov
2cf8d 65 72 20 74 68 65 20 6e 6f 6e 2d 6e 65 67 61 74  er the non-negat
2cf8e 69 76 65 20 69 6e 74 65 67 65 72 73 2e 20 20 46  ive integers.  F
2cf8f 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20  or example, the 
2cf90 63 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d 62 65 72  cursor.** number
2cf91 73 20 6d 69 67 68 74 20 62 65 20 33 2c 20 38 2c  s might be 3, 8,
2cf92 20 39 2c 20 31 30 2c 20 32 30 2c 20 32 33 2c 20   9, 10, 20, 23, 
2cf93 34 31 2c 20 61 6e 64 20 34 35 2e 20 20 54 68 65  41, and 45.  The
2cf94 20 57 68 65 72 65 4d 61 73 6b 53 65 74 0a 2a 2a   WhereMaskSet.**
2cf95 20 74 72 61 6e 73 6c 61 74 65 73 20 74 68 65 73   translates thes
2cf96 65 20 73 70 61 72 73 65 20 63 75 72 73 6f 72 20  e sparse cursor 
2cf97 6e 75 6d 62 65 72 73 20 69 6e 74 6f 20 63 6f 6e  numbers into con
2cf98 73 65 63 75 74 69 76 65 20 69 6e 74 65 67 65 72  secutive integer
2cf99 73 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 77  s.** beginning w
2cf9a 69 74 68 20 30 20 69 6e 20 6f 72 64 65 72 20 74  ith 0 in order t
2cf9b 6f 20 6d 61 6b 65 20 74 68 65 20 62 65 73 74 20  o make the best 
2cf9c 70 6f 73 73 69 62 6c 65 20 75 73 65 20 6f 66 20  possible use of 
2cf9d 74 68 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a  the available.**
2cf9e 20 62 69 74 73 20 69 6e 20 74 68 65 20 42 69 74   bits in the Bit
2cf9f 6d 61 73 6b 2e 20 20 53 6f 2c 20 69 6e 20 74 68  mask.  So, in th
2cfa0 65 20 65 78 61 6d 70 6c 65 20 61 62 6f 76 65 2c  e example above,
2cfa1 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62   the cursor numb
2cfa2 65 72 73 0a 2a 2a 20 77 6f 75 6c 64 20 62 65 20  ers.** would be 
2cfa3 6d 61 70 70 65 64 20 69 6e 74 6f 20 69 6e 74 65  mapped into inte
2cfa4 67 65 72 73 20 30 20 74 68 72 6f 75 67 68 20 37  gers 0 through 7
2cfa5 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62  ..**.** The numb
2cfa6 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 61  er of terms in a
2cfa7 20 6a 6f 69 6e 20 69 73 20 6c 69 6d 69 74 65 64   join is limited
2cfa8 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   by the number o
2cfa9 66 20 62 69 74 73 0a 2a 2a 20 69 6e 20 70 72 65  f bits.** in pre
2cfaa 72 65 71 52 69 67 68 74 20 61 6e 64 20 70 72 65  reqRight and pre
2cfab 72 65 71 41 6c 6c 2e 20 20 54 68 65 20 64 65 66  reqAll.  The def
2cfac 61 75 6c 74 20 69 73 20 36 34 20 62 69 74 73 2c  ault is 64 bits,
2cfad 20 68 65 6e 63 65 20 53 51 4c 69 74 65 0a 2a 2a   hence SQLite.**
2cfae 20 69 73 20 6f 6e 6c 79 20 61 62 6c 65 20 74 6f   is only able to
2cfaf 20 70 72 6f 63 65 73 73 20 6a 6f 69 6e 73 20 77   process joins w
2cfb0 69 74 68 20 36 34 20 6f 72 20 66 65 77 65 72 20  ith 64 or fewer 
2cfb1 74 61 62 6c 65 73 2e 0a 2a 2f 0a 74 79 70 65 64  tables..*/.typed
2cfb2 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 54  ef struct WhereT
2cfb3 65 72 6d 20 57 68 65 72 65 54 65 72 6d 3b 0a 73  erm WhereTerm;.s
2cfb4 74 72 75 63 74 20 57 68 65 72 65 54 65 72 6d 20  truct WhereTerm 
2cfb5 7b 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b  {.  Expr *pExpr;
2cfb6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
2cfb7 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73 75  ointer to the su
2cfb8 62 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74  bexpression that
2cfb9 20 69 73 20 74 68 69 73 20 74 65 72 6d 20 2a 2f   is this term */
2cfba 0a 20 20 69 6e 74 20 69 50 61 72 65 6e 74 3b 20  .  int iParent; 
2cfbb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69             /* Di
2cfbc 73 61 62 6c 65 20 70 57 43 2d 3e 61 5b 69 50 61  sable pWC->a[iPa
2cfbd 72 65 6e 74 5d 20 77 68 65 6e 20 74 68 69 73 20  rent] when this 
2cfbe 74 65 72 6d 20 64 69 73 61 62 6c 65 64 20 2a 2f  term disabled */
2cfbf 0a 20 20 69 6e 74 20 6c 65 66 74 43 75 72 73 6f  .  int leftCurso
2cfc0 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75  r;         /* Cu
2cfc1 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 58  rsor number of X
2cfc2 20 69 6e 20 22 58 20 3c 6f 70 3e 20 3c 65 78 70   in "X <op> <exp
2cfc3 72 3e 22 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b  r>" */.  union {
2cfc4 0a 20 20 20 20 69 6e 74 20 6c 65 66 74 43 6f 6c  .    int leftCol
2cfc5 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  umn;         /* 
2cfc6 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 6f 66  Column number of
2cfc7 20 58 20 69 6e 20 22 58 20 3c 6f 70 3e 20 3c 65   X in "X <op> <e
2cfc8 78 70 72 3e 22 20 2a 2f 0a 20 20 20 20 57 68 65  xpr>" */.    Whe
2cfc9 72 65 4f 72 49 6e 66 6f 20 2a 70 4f 72 49 6e 66  reOrInfo *pOrInf
2cfca 6f 3b 20 20 20 2f 2a 20 45 78 74 72 61 20 69 6e  o;   /* Extra in
2cfcb 66 6f 72 6d 61 74 69 6f 6e 20 69 66 20 65 4f 70  formation if eOp
2cfcc 65 72 61 74 6f 72 3d 3d 57 4f 5f 4f 52 20 2a 2f  erator==WO_OR */
2cfcd 0a 20 20 20 20 57 68 65 72 65 41 6e 64 49 6e 66  .    WhereAndInf
2cfce 6f 20 2a 70 41 6e 64 49 6e 66 6f 3b 20 2f 2a 20  o *pAndInfo; /* 
2cfcf 45 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f  Extra informatio
2cfd0 6e 20 69 66 20 65 4f 70 65 72 61 74 6f 72 3d 3d  n if eOperator==
2cfd1 57 4f 5f 41 4e 44 20 2a 2f 0a 20 20 7d 20 75 3b  WO_AND */.  } u;
2cfd2 0a 20 20 75 31 36 20 65 4f 70 65 72 61 74 6f 72  .  u16 eOperator
2cfd3 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20  ;          /* A 
2cfd4 57 4f 5f 78 78 20 76 61 6c 75 65 20 64 65 73 63  WO_xx value desc
2cfd5 72 69 62 69 6e 67 20 3c 6f 70 3e 20 2a 2f 0a 20  ribing <op> */. 
2cfd6 20 75 38 20 77 74 46 6c 61 67 73 3b 20 20 20 20   u8 wtFlags;    
2cfd7 20 20 20 20 20 20 20 20 20 2f 2a 20 54 45 52 4d           /* TERM
2cfd8 5f 78 78 78 20 62 69 74 20 66 6c 61 67 73 2e 20  _xxx bit flags. 
2cfd9 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a 20 20   See below */.  
2cfda 75 38 20 6e 43 68 69 6c 64 3b 20 20 20 20 20 20  u8 nChild;      
2cfdb 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2cfdc 72 20 6f 66 20 63 68 69 6c 64 72 65 6e 20 74 68  r of children th
2cfdd 61 74 20 6d 75 73 74 20 64 69 73 61 62 6c 65 20  at must disable 
2cfde 75 73 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61  us */.  WhereCla
2cfdf 75 73 65 20 2a 70 57 43 3b 20 20 20 20 20 20 20  use *pWC;       
2cfe0 2f 2a 20 54 68 65 20 63 6c 61 75 73 65 20 74 68  /* The clause th
2cfe1 69 73 20 74 65 72 6d 20 69 73 20 70 61 72 74 20  is term is part 
2cfe2 6f 66 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20  of */.  Bitmask 
2cfe3 70 72 65 72 65 71 52 69 67 68 74 3b 20 20 20 20  prereqRight;    
2cfe4 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 74 61  /* Bitmask of ta
2cfe5 62 6c 65 73 20 75 73 65 64 20 62 79 20 70 45 78  bles used by pEx
2cfe6 70 72 2d 3e 70 52 69 67 68 74 20 2a 2f 0a 20 20  pr->pRight */.  
2cfe7 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71 41 6c  Bitmask prereqAl
2cfe8 6c 3b 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61  l;      /* Bitma
2cfe9 73 6b 20 6f 66 20 74 61 62 6c 65 73 20 72 65 66  sk of tables ref
2cfea 65 72 65 6e 63 65 64 20 62 79 20 70 45 78 70 72  erenced by pExpr
2cfeb 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c   */.};../*.** Al
2cfec 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66 20  lowed values of 
2cfed 57 68 65 72 65 54 65 72 6d 2e 77 74 46 6c 61 67  WhereTerm.wtFlag
2cfee 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52  s.*/.#define TER
2cfef 4d 5f 44 59 4e 41 4d 49 43 20 20 20 20 30 78 30  M_DYNAMIC    0x0
2cff0 31 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 63  1   /* Need to c
2cff1 61 6c 6c 20 73 71 6c 69 74 65 33 45 78 70 72 44  all sqlite3ExprD
2cff2 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72 29  elete(db, pExpr)
2cff3 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d   */.#define TERM
2cff4 5f 56 49 52 54 55 41 4c 20 20 20 20 30 78 30 32  _VIRTUAL    0x02
2cff5 20 20 20 2f 2a 20 41 64 64 65 64 20 62 79 20 74     /* Added by t
2cff6 68 65 20 6f 70 74 69 6d 69 7a 65 72 2e 20 20 44  he optimizer.  D
2cff7 6f 20 6e 6f 74 20 63 6f 64 65 20 2a 2f 0a 23 64  o not code */.#d
2cff8 65 66 69 6e 65 20 54 45 52 4d 5f 43 4f 44 45 44  efine TERM_CODED
2cff9 20 20 20 20 20 20 30 78 30 34 20 20 20 2f 2a 20        0x04   /* 
2cffa 54 68 69 73 20 74 65 72 6d 20 69 73 20 61 6c 72  This term is alr
2cffb 65 61 64 79 20 63 6f 64 65 64 20 2a 2f 0a 23 64  eady coded */.#d
2cffc 65 66 69 6e 65 20 54 45 52 4d 5f 43 4f 50 49 45  efine TERM_COPIE
2cffd 44 20 20 20 20 20 30 78 30 38 20 20 20 2f 2a 20  D     0x08   /* 
2cffe 48 61 73 20 61 20 63 68 69 6c 64 20 2a 2f 0a 23  Has a child */.#
2cfff 64 65 66 69 6e 65 20 54 45 52 4d 5f 4f 52 49 4e  define TERM_ORIN
2d000 46 4f 20 20 20 20 20 30 78 31 30 20 20 20 2f 2a  FO     0x10   /*
2d001 20 4e 65 65 64 20 74 6f 20 66 72 65 65 20 74 68   Need to free th
2d002 65 20 57 68 65 72 65 54 65 72 6d 2e 75 2e 70 4f  e WhereTerm.u.pO
2d003 72 49 6e 66 6f 20 6f 62 6a 65 63 74 20 2a 2f 0a  rInfo object */.
2d004 23 64 65 66 69 6e 65 20 54 45 52 4d 5f 41 4e 44  #define TERM_AND
2d005 49 4e 46 4f 20 20 20 20 30 78 32 30 20 20 20 2f  INFO    0x20   /
2d006 2a 20 4e 65 65 64 20 74 6f 20 66 72 65 65 20 74  * Need to free t
2d007 68 65 20 57 68 65 72 65 54 65 72 6d 2e 75 2e 70  he WhereTerm.u.p
2d008 41 6e 64 49 6e 66 6f 20 6f 62 6a 20 2a 2f 0a 23  AndInfo obj */.#
2d009 64 65 66 69 6e 65 20 54 45 52 4d 5f 4f 52 5f 4f  define TERM_OR_O
2d00a 4b 20 20 20 20 20 20 30 78 34 30 20 20 20 2f 2a  K      0x40   /*
2d00b 20 55 73 65 64 20 64 75 72 69 6e 67 20 4f 52 2d   Used during OR-
2d00c 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e  clause processin
2d00d 67 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  g */../*.** An i
2d00e 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
2d00f 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
2d010 72 65 20 68 6f 6c 64 73 20 61 6c 6c 20 69 6e 66  re holds all inf
2d011 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61  ormation about a
2d012 0a 2a 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65  .** WHERE clause
2d013 2e 20 20 4d 6f 73 74 6c 79 20 74 68 69 73 20 69  .  Mostly this i
2d014 73 20 61 20 63 6f 6e 74 61 69 6e 65 72 20 66 6f  s a container fo
2d015 72 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 57 68  r one or more Wh
2d016 65 72 65 54 65 72 6d 73 2e 0a 2a 2f 0a 73 74 72  ereTerms..*/.str
2d017 75 63 74 20 57 68 65 72 65 43 6c 61 75 73 65 20  uct WhereClause 
2d018 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  {.  Parse *pPars
2d019 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e;           /* 
2d01a 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65  The parser conte
2d01b 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73  xt */.  WhereMas
2d01c 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 3b 20  kSet *pMaskSet; 
2d01d 20 2f 2a 20 4d 61 70 70 69 6e 67 20 6f 66 20 74   /* Mapping of t
2d01e 61 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62  able cursor numb
2d01f 65 72 73 20 74 6f 20 62 69 74 6d 61 73 6b 73 20  ers to bitmasks 
2d020 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 76 6d 61  */.  Bitmask vma
2d021 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  sk;           /*
2d022 20 42 69 74 6d 61 73 6b 20 69 64 65 6e 74 69 66   Bitmask identif
2d023 79 69 6e 67 20 76 69 72 74 75 61 6c 20 74 61 62  ying virtual tab
2d024 6c 65 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20  le cursors */.  
2d025 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20  u8 op;          
2d026 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 6c 69           /* Spli
2d027 74 20 6f 70 65 72 61 74 6f 72 2e 20 20 54 4b 5f  t operator.  TK_
2d028 41 4e 44 20 6f 72 20 54 4b 5f 4f 52 20 2a 2f 0a  AND or TK_OR */.
2d029 20 20 69 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20    int nTerm;    
2d02a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
2d02b 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 2a 2f  mber of terms */
2d02c 0a 20 20 69 6e 74 20 6e 53 6c 6f 74 3b 20 20 20  .  int nSlot;   
2d02d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2d02e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
2d02f 20 69 6e 20 61 5b 5d 20 2a 2f 0a 20 20 57 68 65   in a[] */.  Whe
2d030 72 65 54 65 72 6d 20 2a 61 3b 20 20 20 20 20 20  reTerm *a;      
2d031 20 20 20 20 20 20 2f 2a 20 45 61 63 68 20 61 5b        /* Each a[
2d032 5d 20 64 65 73 63 72 69 62 65 73 20 61 20 74 65  ] describes a te
2d033 72 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45 20  rm of the WHERE 
2d034 63 6c 75 61 73 65 20 2a 2f 0a 23 69 66 20 64 65  cluase */.#if de
2d035 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 53 4d 41  fined(SQLITE_SMA
2d036 4c 4c 5f 53 54 41 43 4b 29 0a 20 20 57 68 65 72  LL_STACK).  Wher
2d037 65 54 65 72 6d 20 61 53 74 61 74 69 63 5b 31 5d  eTerm aStatic[1]
2d038 3b 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20  ;    /* Initial 
2d039 73 74 61 74 69 63 20 73 70 61 63 65 20 66 6f 72  static space for
2d03a 20 61 5b 5d 20 2a 2f 0a 23 65 6c 73 65 0a 20 20   a[] */.#else.  
2d03b 57 68 65 72 65 54 65 72 6d 20 61 53 74 61 74 69  WhereTerm aStati
2d03c 63 5b 38 5d 3b 20 20 20 20 2f 2a 20 49 6e 69 74  c[8];    /* Init
2d03d 69 61 6c 20 73 74 61 74 69 63 20 73 70 61 63 65  ial static space
2d03e 20 66 6f 72 20 61 5b 5d 20 2a 2f 0a 23 65 6e 64   for a[] */.#end
2d03f 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 57  if.};../*.** A W
2d040 68 65 72 65 54 65 72 6d 20 77 69 74 68 20 65 4f  hereTerm with eO
2d041 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 4f 52 20 68  perator==WO_OR h
2d042 61 73 20 69 74 73 20 75 2e 70 4f 72 49 6e 66 6f  as its u.pOrInfo
2d043 20 70 6f 69 6e 74 65 72 20 73 65 74 20 74 6f 0a   pointer set to.
2d044 2a 2a 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79  ** a dynamically
2d045 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 73 74 61   allocated insta
2d046 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
2d047 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a  wing structure..
2d048 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 4f  */.struct WhereO
2d049 72 49 6e 66 6f 20 7b 0a 20 20 57 68 65 72 65 43  rInfo {.  WhereC
2d04a 6c 61 75 73 65 20 77 63 3b 20 20 20 20 20 20 20  lause wc;       
2d04b 20 20 20 2f 2a 20 44 65 63 6f 6d 70 6f 73 69 74     /* Decomposit
2d04c 69 6f 6e 20 69 6e 74 6f 20 73 75 62 74 65 72 6d  ion into subterm
2d04d 73 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 69  s */.  Bitmask i
2d04e 6e 64 65 78 61 62 6c 65 3b 20 20 20 20 20 20 20  ndexable;       
2d04f 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 61 6c  /* Bitmask of al
2d050 6c 20 69 6e 64 65 78 61 62 6c 65 20 74 61 62 6c  l indexable tabl
2d051 65 73 20 69 6e 20 74 68 65 20 63 6c 61 75 73 65  es in the clause
2d052 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20   */.};../*.** A 
2d053 57 68 65 72 65 54 65 72 6d 20 77 69 74 68 20 65  WhereTerm with e
2d054 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 41 4e 44  Operator==WO_AND
2d055 20 68 61 73 20 69 74 73 20 75 2e 70 41 6e 64 49   has its u.pAndI
2d056 6e 66 6f 20 70 6f 69 6e 74 65 72 20 73 65 74 20  nfo pointer set 
2d057 74 6f 0a 2a 2a 20 61 20 64 79 6e 61 6d 69 63 61  to.** a dynamica
2d058 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e  lly allocated in
2d059 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
2d05a 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
2d05b 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65  e..*/.struct Whe
2d05c 72 65 41 6e 64 49 6e 66 6f 20 7b 0a 20 20 57 68  reAndInfo {.  Wh
2d05d 65 72 65 43 6c 61 75 73 65 20 77 63 3b 20 20 20  ereClause wc;   
2d05e 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 75         /* The su
2d05f 62 65 78 70 72 65 73 73 69 6f 6e 20 62 72 6f 6b  bexpression brok
2d060 65 6e 20 6f 75 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  en out */.};../*
2d061 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
2d062 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
2d063 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 73   structure keeps
2d064 20 74 72 61 63 6b 20 6f 66 20 61 20 6d 61 70 70   track of a mapp
2d065 69 6e 67 0a 2a 2a 20 62 65 74 77 65 65 6e 20 56  ing.** between V
2d066 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  DBE cursor numbe
2d067 72 73 20 61 6e 64 20 62 69 74 73 20 6f 66 20 74  rs and bits of t
2d068 68 65 20 62 69 74 6d 61 73 6b 73 20 69 6e 20 57  he bitmasks in W
2d069 68 65 72 65 54 65 72 6d 2e 0a 2a 2a 0a 2a 2a 20  hereTerm..**.** 
2d06a 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20  The VDBE cursor 
2d06b 6e 75 6d 62 65 72 73 20 61 72 65 20 73 6d 61 6c  numbers are smal
2d06c 6c 20 69 6e 74 65 67 65 72 73 20 63 6f 6e 74 61  l integers conta
2d06d 69 6e 65 64 20 69 6e 20 0a 2a 2a 20 53 72 63 4c  ined in .** SrcL
2d06e 69 73 74 5f 69 74 65 6d 2e 69 43 75 72 73 6f 72  ist_item.iCursor
2d06f 20 61 6e 64 20 45 78 70 72 2e 69 54 61 62 6c 65   and Expr.iTable
2d070 20 66 69 65 6c 64 73 2e 20 20 46 6f 72 20 61 6e   fields.  For an
2d071 79 20 67 69 76 65 6e 20 57 48 45 52 45 20 0a 2a  y given WHERE .*
2d072 2a 20 63 6c 61 75 73 65 2c 20 74 68 65 20 63 75  * clause, the cu
2d073 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 6d 69 67  rsor numbers mig
2d074 68 74 20 6e 6f 74 20 62 65 67 69 6e 20 77 69 74  ht not begin wit
2d075 68 20 30 20 61 6e 64 20 74 68 65 79 20 6d 69 67  h 0 and they mig
2d076 68 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20 67 61  ht.** contain ga
2d077 70 73 20 69 6e 20 74 68 65 20 6e 75 6d 62 65 72  ps in the number
2d078 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 20 20 42  ing sequence.  B
2d079 75 74 20 77 65 20 77 61 6e 74 20 74 6f 20 6d 61  ut we want to ma
2d07a 6b 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 75 73  ke maximum.** us
2d07b 65 20 6f 66 20 74 68 65 20 62 69 74 73 20 69 6e  e of the bits in
2d07c 20 6f 75 72 20 62 69 74 6d 61 73 6b 73 2e 20 20   our bitmasks.  
2d07d 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 70  This structure p
2d07e 72 6f 76 69 64 65 73 20 61 20 6d 61 70 70 69 6e  rovides a mappin
2d07f 67 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 73 70  g.** from the sp
2d080 61 72 73 65 20 63 75 72 73 6f 72 20 6e 75 6d 62  arse cursor numb
2d081 65 72 73 20 69 6e 74 6f 20 63 6f 6e 73 65 63 75  ers into consecu
2d082 74 69 76 65 20 69 6e 74 65 67 65 72 73 20 62 65  tive integers be
2d083 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69 74 68 20  ginning.** with 
2d084 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 57 68 65 72  0..**.** If Wher
2d085 65 4d 61 73 6b 53 65 74 2e 69 78 5b 41 5d 3d 3d  eMaskSet.ix[A]==
2d086 42 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20  B it means that 
2d087 54 68 65 20 41 2d 74 68 20 62 69 74 20 6f 66 20  The A-th bit of 
2d088 61 20 42 69 74 6d 61 73 6b 0a 2a 2a 20 63 6f 72  a Bitmask.** cor
2d089 72 65 73 70 6f 6e 64 73 20 56 44 42 45 20 63 75  responds VDBE cu
2d08a 72 73 6f 72 20 6e 75 6d 62 65 72 20 42 2e 20 20  rsor number B.  
2d08b 54 68 65 20 41 2d 74 68 20 62 69 74 20 6f 66 20  The A-th bit of 
2d08c 61 20 62 69 74 6d 61 73 6b 20 69 73 20 31 3c 3c  a bitmask is 1<<
2d08d 41 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61  A..**.** For exa
2d08e 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 57 48 45  mple, if the WHE
2d08f 52 45 20 63 6c 61 75 73 65 20 65 78 70 72 65 73  RE clause expres
2d090 73 69 6f 6e 20 75 73 65 64 20 74 68 65 73 65 20  sion used these 
2d091 56 44 42 45 0a 2a 2a 20 63 75 72 73 6f 72 73 3a  VDBE.** cursors:
2d092 20 20 34 2c 20 35 2c 20 38 2c 20 32 39 2c 20 35    4, 5, 8, 29, 5
2d093 37 2c 20 37 33 2e 20 20 54 68 65 6e 20 74 68 65  7, 73.  Then the
2d094 20 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 73    WhereMaskSet s
2d095 74 72 75 63 74 75 72 65 0a 2a 2a 20 77 6f 75 6c  tructure.** woul
2d096 64 20 6d 61 70 20 74 68 6f 73 65 20 63 75 72 73  d map those curs
2d097 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e 74 6f 20  or numbers into 
2d098 62 69 74 73 20 30 20 74 68 72 6f 75 67 68 20 35  bits 0 through 5
2d099 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
2d09a 74 20 74 68 65 20 6d 61 70 70 69 6e 67 20 69 73  t the mapping is
2d09b 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79   not necessarily
2d09c 20 6f 72 64 65 72 65 64 2e 20 20 49 6e 20 74 68   ordered.  In th
2d09d 65 20 65 78 61 6d 70 6c 65 0a 2a 2a 20 61 62 6f  e example.** abo
2d09e 76 65 2c 20 74 68 65 20 6d 61 70 70 69 6e 67 20  ve, the mapping 
2d09f 6d 69 67 68 74 20 67 6f 20 6c 69 6b 65 20 74 68  might go like th
2d0a0 69 73 3a 20 20 34 2d 3e 33 2c 20 35 2d 3e 31 2c  is:  4->3, 5->1,
2d0a1 20 38 2d 3e 32 2c 20 32 39 2d 3e 30 2c 0a 2a 2a   8->2, 29->0,.**
2d0a2 20 35 37 2d 3e 35 2c 20 37 33 2d 3e 34 2e 20 20   57->5, 73->4.  
2d0a3 4f 72 20 6f 6e 65 20 6f 66 20 37 31 39 20 6f 74  Or one of 719 ot
2d0a4 68 65 72 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73  her combinations
2d0a5 20 6d 69 67 68 74 20 62 65 20 75 73 65 64 2e 20   might be used. 
2d0a6 49 74 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 72  It.** does not r
2d0a7 65 61 6c 6c 79 20 6d 61 74 74 65 72 2e 20 20 57  eally matter.  W
2d0a8 68 61 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74  hat is important
2d0a9 20 69 73 20 74 68 61 74 20 73 70 61 72 73 65 20   is that sparse 
2d0aa 63 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d 62 65 72  cursor.** number
2d0ab 73 20 61 6c 6c 20 67 65 74 20 6d 61 70 70 65 64  s all get mapped
2d0ac 20 69 6e 74 6f 20 62 69 74 20 6e 75 6d 62 65 72   into bit number
2d0ad 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 69 74  s that begin wit
2d0ae 68 20 30 20 61 6e 64 20 63 6f 6e 74 61 69 6e 0a  h 0 and contain.
2d0af 2a 2a 20 6e 6f 20 67 61 70 73 2e 0a 2a 2f 0a 73  ** no gaps..*/.s
2d0b0 74 72 75 63 74 20 57 68 65 72 65 4d 61 73 6b 53  truct WhereMaskS
2d0b1 65 74 20 7b 0a 20 20 69 6e 74 20 6e 3b 20 20 20  et {.  int n;   
2d0b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d0b3 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2d0b4 66 20 61 73 73 69 67 6e 65 64 20 63 75 72 73 6f  f assigned curso
2d0b5 72 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 69 6e  r values */.  in
2d0b6 74 20 69 78 5b 42 4d 53 5d 3b 20 20 20 20 20 20  t ix[BMS];      
2d0b7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
2d0b8 75 72 73 6f 72 20 61 73 73 69 67 6e 65 64 20 74  ursor assigned t
2d0b9 6f 20 65 61 63 68 20 62 69 74 20 2a 2f 0a 7d 3b  o each bit */.};
2d0ba 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 65 43  ../*.** A WhereC
2d0bb 6f 73 74 20 6f 62 6a 65 63 74 20 72 65 63 6f 72  ost object recor
2d0bc 64 73 20 61 20 6c 6f 6f 6b 75 70 20 73 74 72 61  ds a lookup stra
2d0bd 74 65 67 79 20 61 6e 64 20 74 68 65 20 65 73 74  tegy and the est
2d0be 69 6d 61 74 65 64 0a 2a 2a 20 63 6f 73 74 20 6f  imated.** cost o
2d0bf 66 20 70 75 72 73 75 69 6e 67 20 74 68 61 74 20  f pursuing that 
2d0c0 73 74 72 61 74 65 67 79 2e 0a 2a 2f 0a 73 74 72  strategy..*/.str
2d0c1 75 63 74 20 57 68 65 72 65 43 6f 73 74 20 7b 0a  uct WhereCost {.
2d0c2 20 20 57 68 65 72 65 50 6c 61 6e 20 70 6c 61 6e    WherePlan plan
2d0c3 3b 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b  ;    /* The look
2d0c4 75 70 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 20  up strategy */. 
2d0c5 20 64 6f 75 62 6c 65 20 72 43 6f 73 74 3b 20 20   double rCost;  
2d0c6 20 20 20 20 2f 2a 20 4f 76 65 72 61 6c 6c 20 63      /* Overall c
2d0c7 6f 73 74 20 6f 66 20 70 75 72 73 75 69 6e 67 20  ost of pursuing 
2d0c8 74 68 69 73 20 73 65 61 72 63 68 20 73 74 72 61  this search stra
2d0c9 74 65 67 79 20 2a 2f 0a 20 20 64 6f 75 62 6c 65  tegy */.  double
2d0ca 20 6e 52 6f 77 3b 20 20 20 20 20 20 20 2f 2a 20   nRow;       /* 
2d0cb 45 73 74 69 6d 61 74 65 64 20 6e 75 6d 62 65 72  Estimated number
2d0cc 20 6f 66 20 6f 75 74 70 75 74 20 72 6f 77 73 20   of output rows 
2d0cd 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 75 73 65  */.  Bitmask use
2d0ce 64 3b 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61  d;      /* Bitma
2d0cf 73 6b 20 6f 66 20 63 75 72 73 6f 72 73 20 75 73  sk of cursors us
2d0d0 65 64 20 62 79 20 74 68 69 73 20 70 6c 61 6e 20  ed by this plan 
2d0d1 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 69 74  */.};../*.** Bit
2d0d2 6d 61 73 6b 73 20 66 6f 72 20 74 68 65 20 6f 70  masks for the op
2d0d3 65 72 61 74 6f 72 73 20 74 68 61 74 20 69 6e 64  erators that ind
2d0d4 69 63 65 73 20 61 72 65 20 61 62 6c 65 20 74 6f  ices are able to
2d0d5 20 65 78 70 6c 6f 69 74 2e 20 20 41 6e 0a 2a 2a   exploit.  An.**
2d0d6 20 4f 52 2d 65 64 20 63 6f 6d 62 69 6e 61 74 69   OR-ed combinati
2d0d7 6f 6e 20 6f 66 20 74 68 65 73 65 20 76 61 6c 75  on of these valu
2d0d8 65 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 77  es can be used w
2d0d9 68 65 6e 20 73 65 61 72 63 68 69 6e 67 20 66 6f  hen searching fo
2d0da 72 0a 2a 2a 20 74 65 72 6d 73 20 69 6e 20 74 68  r.** terms in th
2d0db 65 20 77 68 65 72 65 20 63 6c 61 75 73 65 2e 0a  e where clause..
2d0dc 2a 2f 0a 23 64 65 66 69 6e 65 20 57 4f 5f 49 4e  */.#define WO_IN
2d0dd 20 20 20 20 20 30 78 30 30 31 0a 23 64 65 66 69       0x001.#defi
2d0de 6e 65 20 57 4f 5f 45 51 20 20 20 20 20 30 78 30  ne WO_EQ     0x0
2d0df 30 32 0a 23 64 65 66 69 6e 65 20 57 4f 5f 4c 54  02.#define WO_LT
2d0e0 20 20 20 20 20 28 57 4f 5f 45 51 3c 3c 28 54 4b       (WO_EQ<<(TK
2d0e1 5f 4c 54 2d 54 4b 5f 45 51 29 29 0a 23 64 65 66  _LT-TK_EQ)).#def
2d0e2 69 6e 65 20 57 4f 5f 4c 45 20 20 20 20 20 28 57  ine WO_LE     (W
2d0e3 4f 5f 45 51 3c 3c 28 54 4b 5f 4c 45 2d 54 4b 5f  O_EQ<<(TK_LE-TK_
2d0e4 45 51 29 29 0a 23 64 65 66 69 6e 65 20 57 4f 5f  EQ)).#define WO_
2d0e5 47 54 20 20 20 20 20 28 57 4f 5f 45 51 3c 3c 28  GT     (WO_EQ<<(
2d0e6 54 4b 5f 47 54 2d 54 4b 5f 45 51 29 29 0a 23 64  TK_GT-TK_EQ)).#d
2d0e7 65 66 69 6e 65 20 57 4f 5f 47 45 20 20 20 20 20  efine WO_GE     
2d0e8 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f 47 45 2d 54  (WO_EQ<<(TK_GE-T
2d0e9 4b 5f 45 51 29 29 0a 23 64 65 66 69 6e 65 20 57  K_EQ)).#define W
2d0ea 4f 5f 4d 41 54 43 48 20 20 30 78 30 34 30 0a 23  O_MATCH  0x040.#
2d0eb 64 65 66 69 6e 65 20 57 4f 5f 49 53 4e 55 4c 4c  define WO_ISNULL
2d0ec 20 30 78 30 38 30 0a 23 64 65 66 69 6e 65 20 57   0x080.#define W
2d0ed 4f 5f 4f 52 20 20 20 20 20 30 78 31 30 30 20 20  O_OR     0x100  
2d0ee 20 20 20 20 20 2f 2a 20 54 77 6f 20 6f 72 20 6d       /* Two or m
2d0ef 6f 72 65 20 4f 52 2d 63 6f 6e 6e 65 63 74 65 64  ore OR-connected
2d0f0 20 74 65 72 6d 73 20 2a 2f 0a 23 64 65 66 69 6e   terms */.#defin
2d0f1 65 20 57 4f 5f 41 4e 44 20 20 20 20 30 78 32 30  e WO_AND    0x20
2d0f2 30 20 20 20 20 20 20 20 2f 2a 20 54 77 6f 20 6f  0       /* Two o
2d0f3 72 20 6d 6f 72 65 20 41 4e 44 2d 63 6f 6e 6e 65  r more AND-conne
2d0f4 63 74 65 64 20 74 65 72 6d 73 20 2a 2f 0a 0a 23  cted terms */..#
2d0f5 64 65 66 69 6e 65 20 57 4f 5f 41 4c 4c 20 20 20  define WO_ALL   
2d0f6 20 30 78 66 66 66 20 20 20 20 20 20 20 2f 2a 20   0xfff       /* 
2d0f7 4d 61 73 6b 20 6f 66 20 61 6c 6c 20 70 6f 73 73  Mask of all poss
2d0f8 69 62 6c 65 20 57 4f 5f 2a 20 76 61 6c 75 65 73  ible WO_* values
2d0f9 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 4f 5f 53   */.#define WO_S
2d0fa 49 4e 47 4c 45 20 30 78 30 66 66 20 20 20 20 20  INGLE 0x0ff     
2d0fb 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 61 6c 6c    /* Mask of all
2d0fc 20 6e 6f 6e 2d 63 6f 6d 70 6f 75 6e 64 20 57 4f   non-compound WO
2d0fd 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 0a 2f 2a  _* values */../*
2d0fe 0a 2a 2a 20 56 61 6c 75 65 20 66 6f 72 20 77 73  .** Value for ws
2d0ff 46 6c 61 67 73 20 72 65 74 75 72 6e 65 64 20 62  Flags returned b
2d100 79 20 62 65 73 74 49 6e 64 65 78 28 29 20 61 6e  y bestIndex() an
2d101 64 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 57  d stored in.** W
2d102 68 65 72 65 4c 65 76 65 6c 2e 77 73 46 6c 61 67  hereLevel.wsFlag
2d103 73 2e 20 20 54 68 65 73 65 20 66 6c 61 67 73 20  s.  These flags 
2d104 64 65 74 65 72 6d 69 6e 65 20 77 68 69 63 68 20  determine which 
2d105 73 65 61 72 63 68 0a 2a 2a 20 73 74 72 61 74 65  search.** strate
2d106 67 69 65 73 20 61 72 65 20 61 70 70 72 6f 70 72  gies are appropr
2d107 69 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  iate..**.** The 
2d108 6c 65 61 73 74 20 73 69 67 6e 69 66 69 63 61 6e  least significan
2d109 74 20 31 32 20 62 69 74 73 20 69 73 20 72 65 73  t 12 bits is res
2d10a 65 72 76 65 64 20 61 73 20 61 20 6d 61 73 6b 20  erved as a mask 
2d10b 66 6f 72 20 57 4f 5f 20 76 61 6c 75 65 73 20 61  for WO_ values a
2d10c 62 6f 76 65 2e 0a 2a 2a 20 54 68 65 20 57 68 65  bove..** The Whe
2d10d 72 65 4c 65 76 65 6c 2e 77 73 46 6c 61 67 73 20  reLevel.wsFlags 
2d10e 66 69 65 6c 64 20 69 73 20 75 73 75 61 6c 6c 79  field is usually
2d10f 20 73 65 74 20 74 6f 20 57 4f 5f 49 4e 7c 57 4f   set to WO_IN|WO
2d110 5f 45 51 7c 57 4f 5f 49 53 4e 55 4c 4c 2e 0a 2a  _EQ|WO_ISNULL..*
2d111 2a 20 42 75 74 20 69 66 20 74 68 65 20 74 61 62  * But if the tab
2d112 6c 65 20 69 73 20 74 68 65 20 72 69 67 68 74 20  le is the right 
2d113 74 61 62 6c 65 20 6f 66 20 61 20 6c 65 66 74 20  table of a left 
2d114 6a 6f 69 6e 2c 20 57 68 65 72 65 4c 65 76 65 6c  join, WhereLevel
2d115 2e 77 73 46 6c 61 67 73 0a 2a 2a 20 69 73 20 73  .wsFlags.** is s
2d116 65 74 20 74 6f 20 57 4f 5f 49 4e 7c 57 4f 5f 45  et to WO_IN|WO_E
2d117 51 2e 20 20 54 68 65 20 57 68 65 72 65 4c 65 76  Q.  The WhereLev
2d118 65 6c 2e 77 73 46 6c 61 67 73 20 66 69 65 6c 64  el.wsFlags field
2d119 20 63 61 6e 20 74 68 65 6e 20 62 65 20 75 73 65   can then be use
2d11a 64 20 61 73 0a 2a 2a 20 74 68 65 20 22 6f 70 22  d as.** the "op"
2d11b 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 66 69   parameter to fi
2d11c 6e 64 54 65 72 6d 20 77 68 65 6e 20 77 65 20 61  ndTerm when we a
2d11d 72 65 20 72 65 73 6f 6c 76 69 6e 67 20 65 71 75  re resolving equ
2d11e 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
2d11f 73 2e 0a 2a 2a 20 49 53 4e 55 4c 4c 20 63 6f 6e  s..** ISNULL con
2d120 73 74 72 61 69 6e 74 73 20 77 69 6c 6c 20 74 68  straints will th
2d121 65 6e 20 6e 6f 74 20 62 65 20 75 73 65 64 20 6f  en not be used o
2d122 6e 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c  n the right tabl
2d123 65 20 6f 66 20 61 20 6c 65 66 74 0a 2a 2a 20 6a  e of a left.** j
2d124 6f 69 6e 2e 20 20 54 69 63 6b 65 74 73 20 23 32  oin.  Tickets #2
2d125 31 37 37 20 61 6e 64 20 23 32 31 38 39 2e 0a 2a  177 and #2189..*
2d126 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
2d127 52 4f 57 49 44 5f 45 51 20 20 20 20 20 30 78 30  ROWID_EQ     0x0
2d128 30 30 30 31 30 30 30 20 20 2f 2a 20 72 6f 77 69  0001000  /* rowi
2d129 64 3d 45 58 50 52 20 6f 72 20 72 6f 77 69 64 20  d=EXPR or rowid 
2d12a 49 4e 20 28 2e 2e 2e 29 20 2a 2f 0a 23 64 65 66  IN (...) */.#def
2d12b 69 6e 65 20 57 48 45 52 45 5f 52 4f 57 49 44 5f  ine WHERE_ROWID_
2d12c 52 41 4e 47 45 20 20 30 78 30 30 30 30 32 30 30  RANGE  0x0000200
2d12d 30 20 20 2f 2a 20 72 6f 77 69 64 3c 45 58 50 52  0  /* rowid<EXPR
2d12e 20 61 6e 64 2f 6f 72 20 72 6f 77 69 64 3e 45 58   and/or rowid>EX
2d12f 50 52 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  PR */.#define WH
2d130 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45 51 20 20 20  ERE_COLUMN_EQ   
2d131 20 30 78 30 30 30 31 30 30 30 30 20 20 2f 2a 20   0x00010000  /* 
2d132 78 3d 45 58 50 52 20 6f 72 20 78 20 49 4e 20 28  x=EXPR or x IN (
2d133 2e 2e 2e 29 20 6f 72 20 78 20 49 53 20 4e 55 4c  ...) or x IS NUL
2d134 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  L */.#define WHE
2d135 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45 20  RE_COLUMN_RANGE 
2d136 30 78 30 30 30 32 30 30 30 30 20 20 2f 2a 20 78  0x00020000  /* x
2d137 3c 45 58 50 52 20 61 6e 64 2f 6f 72 20 78 3e 45  <EXPR and/or x>E
2d138 58 50 52 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57  XPR */.#define W
2d139 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 20 20  HERE_COLUMN_IN  
2d13a 20 20 30 78 30 30 30 34 30 30 30 30 20 20 2f 2a    0x00040000  /*
2d13b 20 78 20 49 4e 20 28 2e 2e 2e 29 20 2a 2f 0a 23   x IN (...) */.#
2d13c 64 65 66 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c  define WHERE_COL
2d13d 55 4d 4e 5f 4e 55 4c 4c 20 20 30 78 30 30 30 38  UMN_NULL  0x0008
2d13e 30 30 30 30 20 20 2f 2a 20 78 20 49 53 20 4e 55  0000  /* x IS NU
2d13f 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  LL */.#define WH
2d140 45 52 45 5f 49 4e 44 45 58 45 44 20 20 20 20 20  ERE_INDEXED     
2d141 20 30 78 30 30 30 66 30 30 30 30 20 20 2f 2a 20   0x000f0000  /* 
2d142 41 6e 79 74 68 69 6e 67 20 74 68 61 74 20 75 73  Anything that us
2d143 65 73 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 23  es an index */.#
2d144 64 65 66 69 6e 65 20 57 48 45 52 45 5f 49 4e 5f  define WHERE_IN_
2d145 41 42 4c 45 20 20 20 20 20 20 30 78 30 30 30 66  ABLE      0x000f
2d146 31 30 30 30 20 20 2f 2a 20 41 62 6c 65 20 74 6f  1000  /* Able to
2d147 20 73 75 70 70 6f 72 74 20 61 6e 20 49 4e 20 6f   support an IN o
2d148 70 65 72 61 74 6f 72 20 2a 2f 0a 23 64 65 66 69  perator */.#defi
2d149 6e 65 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d  ne WHERE_TOP_LIM
2d14a 49 54 20 20 20 20 30 78 30 30 31 30 30 30 30 30  IT    0x00100000
2d14b 20 20 2f 2a 20 78 3c 45 58 50 52 20 6f 72 20 78    /* x<EXPR or x
2d14c 3c 3d 45 58 50 52 20 63 6f 6e 73 74 72 61 69 6e  <=EXPR constrain
2d14d 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  t */.#define WHE
2d14e 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 20 20 20 20  RE_BTM_LIMIT    
2d14f 30 78 30 30 32 30 30 30 30 30 20 20 2f 2a 20 78  0x00200000  /* x
2d150 3e 45 58 50 52 20 6f 72 20 78 3e 3d 45 58 50 52  >EXPR or x>=EXPR
2d151 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 23   constraint */.#
2d152 64 65 66 69 6e 65 20 57 48 45 52 45 5f 49 44 58  define WHERE_IDX
2d153 5f 4f 4e 4c 59 20 20 20 20 20 30 78 30 30 38 30  _ONLY     0x0080
2d154 30 30 30 30 20 20 2f 2a 20 55 73 65 20 69 6e 64  0000  /* Use ind
2d155 65 78 20 6f 6e 6c 79 20 2d 20 6f 6d 69 74 20 74  ex only - omit t
2d156 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  able */.#define 
2d157 57 48 45 52 45 5f 4f 52 44 45 52 42 59 20 20 20  WHERE_ORDERBY   
2d158 20 20 20 30 78 30 31 30 30 30 30 30 30 20 20 2f     0x01000000  /
2d159 2a 20 4f 75 74 70 75 74 20 77 69 6c 6c 20 61 70  * Output will ap
2d15a 70 65 61 72 20 69 6e 20 63 6f 72 72 65 63 74 20  pear in correct 
2d15b 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65  order */.#define
2d15c 20 57 48 45 52 45 5f 52 45 56 45 52 53 45 20 20   WHERE_REVERSE  
2d15d 20 20 20 20 30 78 30 32 30 30 30 30 30 30 20 20      0x02000000  
2d15e 2f 2a 20 53 63 61 6e 20 69 6e 20 72 65 76 65 72  /* Scan in rever
2d15f 73 65 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66  se order */.#def
2d160 69 6e 65 20 57 48 45 52 45 5f 55 4e 49 51 55 45  ine WHERE_UNIQUE
2d161 20 20 20 20 20 20 20 30 78 30 34 30 30 30 30 30         0x0400000
2d162 30 20 20 2f 2a 20 53 65 6c 65 63 74 73 20 6e 6f  0  /* Selects no
2d163 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 72   more than one r
2d164 6f 77 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  ow */.#define WH
2d165 45 52 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  ERE_VIRTUALTABLE
2d166 20 30 78 30 38 30 30 30 30 30 30 20 20 2f 2a 20   0x08000000  /* 
2d167 55 73 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c  Use virtual-tabl
2d168 65 20 70 72 6f 63 65 73 73 69 6e 67 20 2a 2f 0a  e processing */.
2d169 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4d 55  #define WHERE_MU
2d16a 4c 54 49 5f 4f 52 20 20 20 20 20 30 78 31 30 30  LTI_OR     0x100
2d16b 30 30 30 30 30 20 20 2f 2a 20 4f 52 20 75 73 69  00000  /* OR usi
2d16c 6e 67 20 6d 75 6c 74 69 70 6c 65 20 69 6e 64 69  ng multiple indi
2d16d 63 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e  ces */../*.** In
2d16e 69 74 69 61 6c 69 7a 65 20 61 20 70 72 65 61 6c  itialize a preal
2d16f 6c 6f 63 61 74 65 64 20 57 68 65 72 65 43 6c 61  located WhereCla
2d170 75 73 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  use structure..*
2d171 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 68  /.static void wh
2d172 65 72 65 43 6c 61 75 73 65 49 6e 69 74 28 0a 20  ereClauseInit(. 
2d173 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57   WhereClause *pW
2d174 43 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  C,        /* The
2d175 20 57 68 65 72 65 43 6c 61 75 73 65 20 74 6f 20   WhereClause to 
2d176 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a  be initialized *
2d177 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  /.  Parse *pPars
2d178 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e,           /* 
2d179 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74  The parsing cont
2d17a 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61  ext */.  WhereMa
2d17b 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 20  skSet *pMaskSet 
2d17c 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f    /* Mapping fro
2d17d 6d 20 74 61 62 6c 65 20 63 75 72 73 6f 72 20 6e  m table cursor n
2d17e 75 6d 62 65 72 73 20 74 6f 20 62 69 74 6d 61 73  umbers to bitmas
2d17f 6b 73 20 2a 2f 0a 29 7b 0a 20 20 70 57 43 2d 3e  ks */.){.  pWC->
2d180 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b  pParse = pParse;
2d181 0a 20 20 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74  .  pWC->pMaskSet
2d182 20 3d 20 70 4d 61 73 6b 53 65 74 3b 0a 20 20 70   = pMaskSet;.  p
2d183 57 43 2d 3e 6e 54 65 72 6d 20 3d 20 30 3b 0a 20  WC->nTerm = 0;. 
2d184 20 70 57 43 2d 3e 6e 53 6c 6f 74 20 3d 20 41 72   pWC->nSlot = Ar
2d185 72 61 79 53 69 7a 65 28 70 57 43 2d 3e 61 53 74  raySize(pWC->aSt
2d186 61 74 69 63 29 3b 0a 20 20 70 57 43 2d 3e 61 20  atic);.  pWC->a 
2d187 3d 20 70 57 43 2d 3e 61 53 74 61 74 69 63 3b 0a  = pWC->aStatic;.
2d188 20 20 70 57 43 2d 3e 76 6d 61 73 6b 20 3d 20 30    pWC->vmask = 0
2d189 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20  ;.}../* Forward 
2d18a 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73 74 61  reference */.sta
2d18b 74 69 63 20 76 6f 69 64 20 77 68 65 72 65 43 6c  tic void whereCl
2d18c 61 75 73 65 43 6c 65 61 72 28 57 68 65 72 65 43  auseClear(WhereC
2d18d 6c 61 75 73 65 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  lause*);../*.** 
2d18e 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c 6c 20 6d  Deallocate all m
2d18f 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64  emory associated
2d190 20 77 69 74 68 20 61 20 57 68 65 72 65 4f 72 49   with a WhereOrI
2d191 6e 66 6f 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73  nfo object..*/.s
2d192 74 61 74 69 63 20 76 6f 69 64 20 77 68 65 72 65  tatic void where
2d193 4f 72 49 6e 66 6f 44 65 6c 65 74 65 28 73 71 6c  OrInfoDelete(sql
2d194 69 74 65 33 20 2a 64 62 2c 20 57 68 65 72 65 4f  ite3 *db, WhereO
2d195 72 49 6e 66 6f 20 2a 70 29 7b 0a 20 20 77 68 65  rInfo *p){.  whe
2d196 72 65 43 6c 61 75 73 65 43 6c 65 61 72 28 26 70  reClauseClear(&p
2d197 2d 3e 77 63 29 3b 0a 20 20 73 71 6c 69 74 65 33  ->wc);.  sqlite3
2d198 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d  DbFree(db, p);.}
2d199 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61  ../*.** Dealloca
2d19a 74 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 73  te all memory as
2d19b 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
2d19c 57 68 65 72 65 41 6e 64 49 6e 66 6f 20 6f 62 6a  WhereAndInfo obj
2d19d 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ect..*/.static v
2d19e 6f 69 64 20 77 68 65 72 65 41 6e 64 49 6e 66 6f  oid whereAndInfo
2d19f 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a  Delete(sqlite3 *
2d1a0 64 62 2c 20 57 68 65 72 65 41 6e 64 49 6e 66 6f  db, WhereAndInfo
2d1a1 20 2a 70 29 7b 0a 20 20 77 68 65 72 65 43 6c 61   *p){.  whereCla
2d1a2 75 73 65 43 6c 65 61 72 28 26 70 2d 3e 77 63 29  useClear(&p->wc)
2d1a3 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
2d1a4 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a  e(db, p);.}../*.
2d1a5 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 20  ** Deallocate a 
2d1a6 57 68 65 72 65 43 6c 61 75 73 65 20 73 74 72 75  WhereClause stru
2d1a7 63 74 75 72 65 2e 20 20 54 68 65 20 57 68 65 72  cture.  The Wher
2d1a8 65 43 6c 61 75 73 65 20 73 74 72 75 63 74 75 72  eClause structur
2d1a9 65 0a 2a 2a 20 69 74 73 65 6c 66 20 69 73 20 6e  e.** itself is n
2d1aa 6f 74 20 66 72 65 65 64 2e 20 20 54 68 69 73 20  ot freed.  This 
2d1ab 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 69  routine is the i
2d1ac 6e 76 65 72 73 65 20 6f 66 20 77 68 65 72 65 43  nverse of whereC
2d1ad 6c 61 75 73 65 49 6e 69 74 28 29 2e 0a 2a 2f 0a  lauseInit()..*/.
2d1ae 73 74 61 74 69 63 20 76 6f 69 64 20 77 68 65 72  static void wher
2d1af 65 43 6c 61 75 73 65 43 6c 65 61 72 28 57 68 65  eClauseClear(Whe
2d1b0 72 65 43 6c 61 75 73 65 20 2a 70 57 43 29 7b 0a  reClause *pWC){.
2d1b1 20 20 69 6e 74 20 69 3b 0a 20 20 57 68 65 72 65    int i;.  Where
2d1b2 54 65 72 6d 20 2a 61 3b 0a 20 20 73 71 6c 69 74  Term *a;.  sqlit
2d1b3 65 33 20 2a 64 62 20 3d 20 70 57 43 2d 3e 70 50  e3 *db = pWC->pP
2d1b4 61 72 73 65 2d 3e 64 62 3b 0a 20 20 66 6f 72 28  arse->db;.  for(
2d1b5 69 3d 70 57 43 2d 3e 6e 54 65 72 6d 2d 31 2c 20  i=pWC->nTerm-1, 
2d1b6 61 3d 70 57 43 2d 3e 61 3b 20 69 3e 3d 30 3b 20  a=pWC->a; i>=0; 
2d1b7 69 2d 2d 2c 20 61 2b 2b 29 7b 0a 20 20 20 20 69  i--, a++){.    i
2d1b8 66 28 20 61 2d 3e 77 74 46 6c 61 67 73 20 26 20  f( a->wtFlags & 
2d1b9 54 45 52 4d 5f 44 59 4e 41 4d 49 43 20 29 7b 0a  TERM_DYNAMIC ){.
2d1ba 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2d1bb 72 44 65 6c 65 74 65 28 64 62 2c 20 61 2d 3e 70  rDelete(db, a->p
2d1bc 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Expr);.    }.   
2d1bd 20 69 66 28 20 61 2d 3e 77 74 46 6c 61 67 73 20   if( a->wtFlags 
2d1be 26 20 54 45 52 4d 5f 4f 52 49 4e 46 4f 20 29 7b  & TERM_ORINFO ){
2d1bf 0a 20 20 20 20 20 20 77 68 65 72 65 4f 72 49 6e  .      whereOrIn
2d1c0 66 6f 44 65 6c 65 74 65 28 64 62 2c 20 61 2d 3e  foDelete(db, a->
2d1c1 75 2e 70 4f 72 49 6e 66 6f 29 3b 0a 20 20 20 20  u.pOrInfo);.    
2d1c2 7d 65 6c 73 65 20 69 66 28 20 61 2d 3e 77 74 46  }else if( a->wtF
2d1c3 6c 61 67 73 20 26 20 54 45 52 4d 5f 41 4e 44 49  lags & TERM_ANDI
2d1c4 4e 46 4f 20 29 7b 0a 20 20 20 20 20 20 77 68 65  NFO ){.      whe
2d1c5 72 65 41 6e 64 49 6e 66 6f 44 65 6c 65 74 65 28  reAndInfoDelete(
2d1c6 64 62 2c 20 61 2d 3e 75 2e 70 41 6e 64 49 6e 66  db, a->u.pAndInf
2d1c7 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  o);.    }.  }.  
2d1c8 69 66 28 20 70 57 43 2d 3e 61 21 3d 70 57 43 2d  if( pWC->a!=pWC-
2d1c9 3e 61 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20  >aStatic ){.    
2d1ca 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2d1cb 2c 20 70 57 43 2d 3e 61 29 3b 0a 20 20 7d 0a 7d  , pWC->a);.  }.}
2d1cc 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 73 69  ../*.** Add a si
2d1cd 6e 67 6c 65 20 6e 65 77 20 57 68 65 72 65 54 65  ngle new WhereTe
2d1ce 72 6d 20 65 6e 74 72 79 20 74 6f 20 74 68 65 20  rm entry to the 
2d1cf 57 68 65 72 65 43 6c 61 75 73 65 20 6f 62 6a 65  WhereClause obje
2d1d0 63 74 20 70 57 43 2e 0a 2a 2a 20 54 68 65 20 6e  ct pWC..** The n
2d1d1 65 77 20 57 68 65 72 65 54 65 72 6d 20 6f 62 6a  ew WhereTerm obj
2d1d2 65 63 74 20 69 73 20 63 6f 6e 73 74 72 75 63 74  ect is construct
2d1d3 65 64 20 66 72 6f 6d 20 45 78 70 72 20 70 20 61  ed from Expr p a
2d1d4 6e 64 20 77 69 74 68 20 77 74 46 6c 61 67 73 2e  nd with wtFlags.
2d1d5 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 69 6e  .** The index in
2d1d6 20 70 57 43 2d 3e 61 5b 5d 20 6f 66 20 74 68 65   pWC->a[] of the
2d1d7 20 6e 65 77 20 57 68 65 72 65 54 65 72 6d 20 69   new WhereTerm i
2d1d8 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75  s returned on su
2d1d9 63 63 65 73 73 2e 0a 2a 2a 20 30 20 69 73 20 72  ccess..** 0 is r
2d1da 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 6e  eturned if the n
2d1db 65 77 20 57 68 65 72 65 54 65 72 6d 20 63 6f 75  ew WhereTerm cou
2d1dc 6c 64 20 6e 6f 74 20 62 65 20 61 64 64 65 64 20  ld not be added 
2d1dd 64 75 65 20 74 6f 20 61 20 6d 65 6d 6f 72 79 0a  due to a memory.
2d1de 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72  ** allocation er
2d1df 72 6f 72 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79  ror.  The memory
2d1e0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c   allocation fail
2d1e1 75 72 65 20 77 69 6c 6c 20 62 65 20 72 65 63 6f  ure will be reco
2d1e2 72 64 65 64 20 69 6e 0a 2a 2a 20 74 68 65 20 64  rded in.** the d
2d1e3 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2d1e4 66 6c 61 67 20 73 6f 20 74 68 61 74 20 68 69 67  flag so that hig
2d1e5 68 65 72 2d 6c 65 76 65 6c 20 66 75 6e 63 74 69  her-level functi
2d1e6 6f 6e 73 20 63 61 6e 20 64 65 74 65 63 74 20 69  ons can detect i
2d1e7 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  t..**.** This ro
2d1e8 75 74 69 6e 65 20 77 69 6c 6c 20 69 6e 63 72 65  utine will incre
2d1e9 61 73 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20  ase the size of 
2d1ea 74 68 65 20 70 57 43 2d 3e 61 5b 5d 20 61 72 72  the pWC->a[] arr
2d1eb 61 79 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e  ay as necessary.
2d1ec 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 77 74  .**.** If the wt
2d1ed 46 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 69  Flags argument i
2d1ee 6e 63 6c 75 64 65 73 20 54 45 52 4d 5f 44 59 4e  ncludes TERM_DYN
2d1ef 41 4d 49 43 2c 20 74 68 65 6e 20 72 65 73 70 6f  AMIC, then respo
2d1f0 6e 73 69 62 69 6c 69 74 79 0a 2a 2a 20 66 6f 72  nsibility.** for
2d1f1 20 66 72 65 65 69 6e 67 20 74 68 65 20 65 78 70   freeing the exp
2d1f2 72 65 73 73 69 6f 6e 20 70 20 69 73 20 61 73 73  ression p is ass
2d1f3 75 6d 65 64 20 62 79 20 74 68 65 20 57 68 65 72  umed by the Wher
2d1f4 65 43 6c 61 75 73 65 20 6f 62 6a 65 63 74 20 70  eClause object p
2d1f5 57 43 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 74  WC..** This is t
2d1f6 72 75 65 20 65 76 65 6e 20 69 66 20 74 68 69 73  rue even if this
2d1f7 20 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 20 74   routine fails t
2d1f8 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  o allocate a new
2d1f9 20 57 68 65 72 65 54 65 72 6d 2e 0a 2a 2a 0a 2a   WhereTerm..**.*
2d1fa 2a 20 57 41 52 4e 49 4e 47 3a 20 20 54 68 69 73  * WARNING:  This
2d1fb 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 72   routine might r
2d1fc 65 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 73 70  eallocate the sp
2d1fd 61 63 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72  ace used to stor
2d1fe 65 0a 2a 2a 20 57 68 65 72 65 54 65 72 6d 73 2e  e.** WhereTerms.
2d1ff 20 20 41 6c 6c 20 70 6f 69 6e 74 65 72 73 20 74    All pointers t
2d200 6f 20 57 68 65 72 65 54 65 72 6d 73 20 73 68 6f  o WhereTerms sho
2d201 75 6c 64 20 62 65 20 69 6e 76 61 6c 69 64 61 74  uld be invalidat
2d202 65 64 20 61 66 74 65 72 0a 2a 2a 20 63 61 6c 6c  ed after.** call
2d203 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ing this routine
2d204 2e 20 20 53 75 63 68 20 70 6f 69 6e 74 65 72 73  .  Such pointers
2d205 20 6d 61 79 20 62 65 20 72 65 69 6e 69 74 69 61   may be reinitia
2d206 6c 69 7a 65 64 20 62 79 20 72 65 66 65 72 65 6e  lized by referen
2d207 63 69 6e 67 0a 2a 2a 20 74 68 65 20 70 57 43 2d  cing.** the pWC-
2d208 3e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a 2f 0a 73  >a[] array..*/.s
2d209 74 61 74 69 63 20 69 6e 74 20 77 68 65 72 65 43  tatic int whereC
2d20a 6c 61 75 73 65 49 6e 73 65 72 74 28 57 68 65 72  lauseInsert(Wher
2d20b 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 45 78  eClause *pWC, Ex
2d20c 70 72 20 2a 70 2c 20 75 38 20 77 74 46 6c 61 67  pr *p, u8 wtFlag
2d20d 73 29 7b 0a 20 20 57 68 65 72 65 54 65 72 6d 20  s){.  WhereTerm 
2d20e 2a 70 54 65 72 6d 3b 0a 20 20 69 6e 74 20 69 64  *pTerm;.  int id
2d20f 78 3b 0a 20 20 69 66 28 20 70 57 43 2d 3e 6e 54  x;.  if( pWC->nT
2d210 65 72 6d 3e 3d 70 57 43 2d 3e 6e 53 6c 6f 74 20  erm>=pWC->nSlot 
2d211 29 7b 0a 20 20 20 20 57 68 65 72 65 54 65 72 6d  ){.    WhereTerm
2d212 20 2a 70 4f 6c 64 20 3d 20 70 57 43 2d 3e 61 3b   *pOld = pWC->a;
2d213 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62  .    sqlite3 *db
2d214 20 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 2d 3e   = pWC->pParse->
2d215 64 62 3b 0a 20 20 20 20 70 57 43 2d 3e 61 20 3d  db;.    pWC->a =
2d216 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
2d217 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 70  Raw(db, sizeof(p
2d218 57 43 2d 3e 61 5b 30 5d 29 2a 70 57 43 2d 3e 6e  WC->a[0])*pWC->n
2d219 53 6c 6f 74 2a 32 20 29 3b 0a 20 20 20 20 69 66  Slot*2 );.    if
2d21a 28 20 70 57 43 2d 3e 61 3d 3d 30 20 29 7b 0a 20  ( pWC->a==0 ){. 
2d21b 20 20 20 20 20 69 66 28 20 77 74 46 6c 61 67 73       if( wtFlags
2d21c 20 26 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43 20   & TERM_DYNAMIC 
2d21d 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2d21e 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
2d21f 20 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   p);.      }.   
2d220 20 20 20 70 57 43 2d 3e 61 20 3d 20 70 4f 6c 64     pWC->a = pOld
2d221 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  ;.      return 0
2d222 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63  ;.    }.    memc
2d223 70 79 28 70 57 43 2d 3e 61 2c 20 70 4f 6c 64 2c  py(pWC->a, pOld,
2d224 20 73 69 7a 65 6f 66 28 70 57 43 2d 3e 61 5b 30   sizeof(pWC->a[0
2d225 5d 29 2a 70 57 43 2d 3e 6e 54 65 72 6d 29 3b 0a  ])*pWC->nTerm);.
2d226 20 20 20 20 69 66 28 20 70 4f 6c 64 21 3d 70 57      if( pOld!=pW
2d227 43 2d 3e 61 53 74 61 74 69 63 20 29 7b 0a 20 20  C->aStatic ){.  
2d228 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2d229 65 28 64 62 2c 20 70 4f 6c 64 29 3b 0a 20 20 20  e(db, pOld);.   
2d22a 20 7d 0a 20 20 20 20 70 57 43 2d 3e 6e 53 6c 6f   }.    pWC->nSlo
2d22b 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  t = sqlite3DbMal
2d22c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 70 57 43 2d  locSize(db, pWC-
2d22d 3e 61 29 2f 73 69 7a 65 6f 66 28 70 57 43 2d 3e  >a)/sizeof(pWC->
2d22e 61 5b 30 5d 29 3b 0a 20 20 7d 0a 20 20 70 54 65  a[0]);.  }.  pTe
2d22f 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78  rm = &pWC->a[idx
2d230 20 3d 20 70 57 43 2d 3e 6e 54 65 72 6d 2b 2b 5d   = pWC->nTerm++]
2d231 3b 0a 20 20 70 54 65 72 6d 2d 3e 70 45 78 70 72  ;.  pTerm->pExpr
2d232 20 3d 20 70 3b 0a 20 20 70 54 65 72 6d 2d 3e 77   = p;.  pTerm->w
2d233 74 46 6c 61 67 73 20 3d 20 77 74 46 6c 61 67 73  tFlags = wtFlags
2d234 3b 0a 20 20 70 54 65 72 6d 2d 3e 70 57 43 20 3d  ;.  pTerm->pWC =
2d235 20 70 57 43 3b 0a 20 20 70 54 65 72 6d 2d 3e 69   pWC;.  pTerm->i
2d236 50 61 72 65 6e 74 20 3d 20 2d 31 3b 0a 20 20 72  Parent = -1;.  r
2d237 65 74 75 72 6e 20 69 64 78 3b 0a 7d 0a 0a 2f 2a  eturn idx;.}../*
2d238 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2d239 20 69 64 65 6e 74 69 66 69 65 73 20 73 75 62 65   identifies sube
2d23a 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68  xpressions in th
2d23b 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 77  e WHERE clause w
2d23c 68 65 72 65 0a 2a 2a 20 65 61 63 68 20 73 75 62  here.** each sub
2d23d 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73 65  expression is se
2d23e 70 61 72 61 74 65 64 20 62 79 20 74 68 65 20 41  parated by the A
2d23f 4e 44 20 6f 70 65 72 61 74 6f 72 20 6f 72 20 73  ND operator or s
2d240 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 6f 70 65  ome other.** ope
2d241 72 61 74 6f 72 20 73 70 65 63 69 66 69 65 64 20  rator specified 
2d242 69 6e 20 74 68 65 20 6f 70 20 70 61 72 61 6d 65  in the op parame
2d243 74 65 72 2e 20 20 54 68 65 20 57 68 65 72 65 43  ter.  The WhereC
2d244 6c 61 75 73 65 20 73 74 72 75 63 74 75 72 65 0a  lause structure.
2d245 2a 2a 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74  ** is filled wit
2d246 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 73 75  h pointers to su
2d247 62 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 20 46  bexpressions.  F
2d248 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a  or example:.**.*
2d249 2a 20 20 20 20 57 48 45 52 45 20 20 61 3d 3d 27  *    WHERE  a=='
2d24a 68 65 6c 6c 6f 27 20 41 4e 44 20 63 6f 61 6c 65  hello' AND coale
2d24b 73 63 65 28 62 2c 31 31 29 3c 31 30 20 41 4e 44  sce(b,11)<10 AND
2d24c 20 28 63 2b 31 32 21 3d 64 20 4f 52 20 63 3d 3d   (c+12!=d OR c==
2d24d 32 32 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  22).**          
2d24e 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20   \________/     
2d24f 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  \_______________
2d250 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f  /     \_________
2d251 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a 20 20 20 20 20  _______/.**     
2d252 20 20 20 20 20 20 20 73 6c 6f 74 5b 30 5d 20 20         slot[0]  
2d253 20 20 20 20 20 20 20 20 20 20 73 6c 6f 74 5b 31            slot[1
2d254 5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ]               
2d255 73 6c 6f 74 5b 32 5d 0a 2a 2a 0a 2a 2a 20 54 68  slot[2].**.** Th
2d256 65 20 6f 72 69 67 69 6e 61 6c 20 57 48 45 52 45  e original WHERE
2d257 20 63 6c 61 75 73 65 20 69 6e 20 70 45 78 70 72   clause in pExpr
2d258 20 69 73 20 75 6e 61 6c 74 65 72 65 64 2e 20 20   is unaltered.  
2d259 41 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  All this routine
2d25a 0a 2a 2a 20 64 6f 65 73 20 69 73 20 6d 61 6b 65  .** does is make
2d25b 20 73 6c 6f 74 5b 5d 20 65 6e 74 72 69 65 73 20   slot[] entries 
2d25c 70 6f 69 6e 74 20 74 6f 20 73 75 62 73 74 72 75  point to substru
2d25d 63 74 75 72 65 20 77 69 74 68 69 6e 20 70 45 78  cture within pEx
2d25e 70 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65  pr..**.** In the
2d25f 20 70 72 65 76 69 6f 75 73 20 73 65 6e 74 65 6e   previous senten
2d260 63 65 20 61 6e 64 20 69 6e 20 74 68 65 20 64 69  ce and in the di
2d261 61 67 72 61 6d 2c 20 22 73 6c 6f 74 5b 5d 22 20  agram, "slot[]" 
2d262 72 65 66 65 72 73 20 74 6f 0a 2a 2a 20 74 68 65  refers to.** the
2d263 20 57 68 65 72 65 43 6c 61 75 73 65 2e 61 5b 5d   WhereClause.a[]
2d264 20 61 72 72 61 79 2e 20 20 54 68 65 20 73 6c 6f   array.  The slo
2d265 74 5b 5d 20 61 72 72 61 79 20 67 72 6f 77 73 20  t[] array grows 
2d266 61 73 20 6e 65 65 64 65 64 20 74 6f 20 63 6f 6e  as needed to con
2d267 74 61 69 6e 0a 2a 2a 20 61 6c 6c 20 74 65 72 6d  tain.** all term
2d268 73 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63  s of the WHERE c
2d269 6c 61 75 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  lause..*/.static
2d26a 20 76 6f 69 64 20 77 68 65 72 65 53 70 6c 69 74   void whereSplit
2d26b 28 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57  (WhereClause *pW
2d26c 43 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20  C, Expr *pExpr, 
2d26d 69 6e 74 20 6f 70 29 7b 0a 20 20 70 57 43 2d 3e  int op){.  pWC->
2d26e 6f 70 20 3d 20 28 75 38 29 6f 70 3b 0a 20 20 69  op = (u8)op;.  i
2d26f 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65  f( pExpr==0 ) re
2d270 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 45 78 70  turn;.  if( pExp
2d271 72 2d 3e 6f 70 21 3d 6f 70 20 29 7b 0a 20 20 20  r->op!=op ){.   
2d272 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65   whereClauseInse
2d273 72 74 28 70 57 43 2c 20 70 45 78 70 72 2c 20 30  rt(pWC, pExpr, 0
2d274 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
2d275 77 68 65 72 65 53 70 6c 69 74 28 70 57 43 2c 20  whereSplit(pWC, 
2d276 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 6f 70  pExpr->pLeft, op
2d277 29 3b 0a 20 20 20 20 77 68 65 72 65 53 70 6c 69  );.    whereSpli
2d278 74 28 70 57 43 2c 20 70 45 78 70 72 2d 3e 70 52  t(pWC, pExpr->pR
2d279 69 67 68 74 2c 20 6f 70 29 3b 0a 20 20 7d 0a 7d  ight, op);.  }.}
2d27a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ../*.** Initiali
2d27b 7a 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  ze an expression
2d27c 20 6d 61 73 6b 20 73 65 74 20 28 61 20 57 68 65   mask set (a Whe
2d27d 72 65 4d 61 73 6b 53 65 74 20 6f 62 6a 65 63 74  reMaskSet object
2d27e 29 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 69 6e 69  ).*/.#define ini
2d27f 74 4d 61 73 6b 53 65 74 28 50 29 20 20 6d 65 6d  tMaskSet(P)  mem
2d280 73 65 74 28 50 2c 20 30 2c 20 73 69 7a 65 6f 66  set(P, 0, sizeof
2d281 28 2a 50 29 29 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  (*P))../*.** Ret
2d282 75 72 6e 20 74 68 65 20 62 69 74 6d 61 73 6b 20  urn the bitmask 
2d283 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 63 75  for the given cu
2d284 72 73 6f 72 20 6e 75 6d 62 65 72 2e 20 20 52 65  rsor number.  Re
2d285 74 75 72 6e 20 30 20 69 66 0a 2a 2a 20 69 43 75  turn 0 if.** iCu
2d286 72 73 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 74  rsor is not in t
2d287 68 65 20 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69  he set..*/.stati
2d288 63 20 42 69 74 6d 61 73 6b 20 67 65 74 4d 61 73  c Bitmask getMas
2d289 6b 28 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a  k(WhereMaskSet *
2d28a 70 4d 61 73 6b 53 65 74 2c 20 69 6e 74 20 69 43  pMaskSet, int iC
2d28b 75 72 73 6f 72 29 7b 0a 20 20 69 6e 74 20 69 3b  ursor){.  int i;
2d28c 0a 20 20 61 73 73 65 72 74 28 20 70 4d 61 73 6b  .  assert( pMask
2d28d 53 65 74 2d 3e 6e 3c 3d 73 69 7a 65 6f 66 28 42  Set->n<=sizeof(B
2d28e 69 74 6d 61 73 6b 29 2a 38 20 29 3b 0a 20 20 66  itmask)*8 );.  f
2d28f 6f 72 28 69 3d 30 3b 20 69 3c 70 4d 61 73 6b 53  or(i=0; i<pMaskS
2d290 65 74 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  et->n; i++){.   
2d291 20 69 66 28 20 70 4d 61 73 6b 53 65 74 2d 3e 69   if( pMaskSet->i
2d292 78 5b 69 5d 3d 3d 69 43 75 72 73 6f 72 20 29 7b  x[i]==iCursor ){
2d293 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 28 28  .      return ((
2d294 42 69 74 6d 61 73 6b 29 31 29 3c 3c 69 3b 0a 20  Bitmask)1)<<i;. 
2d295 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
2d296 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72  n 0;.}../*.** Cr
2d297 65 61 74 65 20 61 20 6e 65 77 20 6d 61 73 6b 20  eate a new mask 
2d298 66 6f 72 20 63 75 72 73 6f 72 20 69 43 75 72 73  for cursor iCurs
2d299 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  or..**.** There 
2d29a 69 73 20 6f 6e 65 20 63 75 72 73 6f 72 20 70 65  is one cursor pe
2d29b 72 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46  r table in the F
2d29c 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54 68 65  ROM clause.  The
2d29d 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 61   number of.** ta
2d29e 62 6c 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d  bles in the FROM
2d29f 20 63 6c 61 75 73 65 20 69 73 20 6c 69 6d 69 74   clause is limit
2d2a0 65 64 20 62 79 20 61 20 74 65 73 74 20 65 61 72  ed by a test ear
2d2a1 6c 79 20 69 6e 20 74 68 65 0a 2a 2a 20 73 71 6c  ly in the.** sql
2d2a2 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
2d2a3 20 72 6f 75 74 69 6e 65 2e 20 20 53 6f 20 77 65   routine.  So we
2d2a4 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 70   know that the p
2d2a5 4d 61 73 6b 53 65 74 2d 3e 69 78 5b 5d 0a 2a 2a  MaskSet->ix[].**
2d2a6 20 61 72 72 61 79 20 77 69 6c 6c 20 6e 65 76 65   array will neve
2d2a7 72 20 6f 76 65 72 66 6c 6f 77 2e 0a 2a 2f 0a 73  r overflow..*/.s
2d2a8 74 61 74 69 63 20 76 6f 69 64 20 63 72 65 61 74  tatic void creat
2d2a9 65 4d 61 73 6b 28 57 68 65 72 65 4d 61 73 6b 53  eMask(WhereMaskS
2d2aa 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 69 6e  et *pMaskSet, in
2d2ab 74 20 69 43 75 72 73 6f 72 29 7b 0a 20 20 61 73  t iCursor){.  as
2d2ac 73 65 72 74 28 20 70 4d 61 73 6b 53 65 74 2d 3e  sert( pMaskSet->
2d2ad 6e 20 3c 20 41 72 72 61 79 53 69 7a 65 28 70 4d  n < ArraySize(pM
2d2ae 61 73 6b 53 65 74 2d 3e 69 78 29 20 29 3b 0a 20  askSet->ix) );. 
2d2af 20 70 4d 61 73 6b 53 65 74 2d 3e 69 78 5b 70 4d   pMaskSet->ix[pM
2d2b0 61 73 6b 53 65 74 2d 3e 6e 2b 2b 5d 20 3d 20 69  askSet->n++] = i
2d2b1 43 75 72 73 6f 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Cursor;.}../*.**
2d2b2 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61   This routine wa
2d2b3 6c 6b 73 20 28 72 65 63 75 72 73 69 76 65 6c 79  lks (recursively
2d2b4 29 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  ) an expression 
2d2b5 74 72 65 65 20 61 6e 64 20 67 65 6e 65 72 61 74  tree and generat
2d2b6 65 73 0a 2a 2a 20 61 20 62 69 74 6d 61 73 6b 20  es.** a bitmask 
2d2b7 69 6e 64 69 63 61 74 69 6e 67 20 77 68 69 63 68  indicating which
2d2b8 20 74 61 62 6c 65 73 20 61 72 65 20 75 73 65 64   tables are used
2d2b9 20 69 6e 20 74 68 61 74 20 65 78 70 72 65 73 73   in that express
2d2ba 69 6f 6e 0a 2a 2a 20 74 72 65 65 2e 0a 2a 2a 0a  ion.** tree..**.
2d2bb 2a 2a 20 49 6e 20 6f 72 64 65 72 20 66 6f 72 20  ** In order for 
2d2bc 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20  this routine to 
2d2bd 77 6f 72 6b 2c 20 74 68 65 20 63 61 6c 6c 69 6e  work, the callin
2d2be 67 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20  g function must 
2d2bf 68 61 76 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73  have.** previous
2d2c0 6c 79 20 69 6e 76 6f 6b 65 64 20 73 71 6c 69 74  ly invoked sqlit
2d2c1 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d  e3ResolveExprNam
2d2c2 65 73 28 29 20 6f 6e 20 74 68 65 20 65 78 70 72  es() on the expr
2d2c3 65 73 73 69 6f 6e 2e 20 20 53 65 65 0a 2a 2a 20  ession.  See.** 
2d2c4 74 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65  the header comme
2d2c5 6e 74 20 6f 6e 20 74 68 61 74 20 72 6f 75 74 69  nt on that routi
2d2c6 6e 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  ne for additiona
2d2c7 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  l information..*
2d2c8 2a 20 54 68 65 20 73 71 6c 69 74 65 33 52 65 73  * The sqlite3Res
2d2c9 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 20  olveExprNames() 
2d2ca 72 6f 75 74 69 6e 65 73 20 6c 6f 6f 6b 73 20 66  routines looks f
2d2cb 6f 72 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  or column names 
2d2cc 61 6e 64 0a 2a 2a 20 73 65 74 73 20 74 68 65 69  and.** sets thei
2d2cd 72 20 6f 70 63 6f 64 65 73 20 74 6f 20 54 4b 5f  r opcodes to TK_
2d2ce 43 4f 4c 55 4d 4e 20 61 6e 64 20 74 68 65 69 72  COLUMN and their
2d2cf 20 45 78 70 72 2e 69 54 61 62 6c 65 20 66 69 65   Expr.iTable fie
2d2d0 6c 64 73 20 74 6f 0a 2a 2a 20 74 68 65 20 56 44  lds to.** the VD
2d2d1 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  BE cursor number
2d2d2 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20   of the table.  
2d2d3 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 73  This routine jus
2d2d4 74 20 68 61 73 20 74 6f 0a 2a 2a 20 74 72 61 6e  t has to.** tran
2d2d5 73 6c 61 74 65 20 74 68 65 20 63 75 72 73 6f 72  slate the cursor
2d2d6 20 6e 75 6d 62 65 72 73 20 69 6e 74 6f 20 62 69   numbers into bi
2d2d7 74 6d 61 73 6b 20 76 61 6c 75 65 73 20 61 6e 64  tmask values and
2d2d8 20 4f 52 20 61 6c 6c 0a 2a 2a 20 74 68 65 20 62   OR all.** the b
2d2d9 69 74 6d 61 73 6b 73 20 74 6f 67 65 74 68 65 72  itmasks together
2d2da 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 42 69 74 6d  ..*/.static Bitm
2d2db 61 73 6b 20 65 78 70 72 4c 69 73 74 54 61 62 6c  ask exprListTabl
2d2dc 65 55 73 61 67 65 28 57 68 65 72 65 4d 61 73 6b  eUsage(WhereMask
2d2dd 53 65 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29  Set*, ExprList*)
2d2de 3b 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b  ;.static Bitmask
2d2df 20 65 78 70 72 53 65 6c 65 63 74 54 61 62 6c 65   exprSelectTable
2d2e0 55 73 61 67 65 28 57 68 65 72 65 4d 61 73 6b 53  Usage(WhereMaskS
2d2e1 65 74 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 73  et*, Select*);.s
2d2e2 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20 65 78  tatic Bitmask ex
2d2e3 70 72 54 61 62 6c 65 55 73 61 67 65 28 57 68 65  prTableUsage(Whe
2d2e4 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b  reMaskSet *pMask
2d2e5 53 65 74 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20  Set, Expr *p){. 
2d2e6 20 42 69 74 6d 61 73 6b 20 6d 61 73 6b 20 3d 20   Bitmask mask = 
2d2e7 30 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  0;.  if( p==0 ) 
2d2e8 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
2d2e9 70 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e  p->op==TK_COLUMN
2d2ea 20 29 7b 0a 20 20 20 20 6d 61 73 6b 20 3d 20 67   ){.    mask = g
2d2eb 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c  etMask(pMaskSet,
2d2ec 20 70 2d 3e 69 54 61 62 6c 65 29 3b 0a 20 20 20   p->iTable);.   
2d2ed 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 20 20   return mask;.  
2d2ee 7d 0a 20 20 6d 61 73 6b 20 3d 20 65 78 70 72 54  }.  mask = exprT
2d2ef 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
2d2f0 65 74 2c 20 70 2d 3e 70 52 69 67 68 74 29 3b 0a  et, p->pRight);.
2d2f1 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54 61    mask |= exprTa
2d2f2 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65  bleUsage(pMaskSe
2d2f3 74 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20  t, p->pLeft);.  
2d2f4 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
2d2f5 72 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65 6c  rty(p, EP_xIsSel
2d2f6 65 63 74 29 20 29 7b 0a 20 20 20 20 6d 61 73 6b  ect) ){.    mask
2d2f7 20 7c 3d 20 65 78 70 72 53 65 6c 65 63 74 54 61   |= exprSelectTa
2d2f8 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65  bleUsage(pMaskSe
2d2f9 74 2c 20 70 2d 3e 78 2e 70 53 65 6c 65 63 74 29  t, p->x.pSelect)
2d2fa 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d  ;.  }else{.    m
2d2fb 61 73 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74 54  ask |= exprListT
2d2fc 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
2d2fd 65 74 2c 20 70 2d 3e 78 2e 70 4c 69 73 74 29 3b  et, p->x.pList);
2d2fe 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d 61  .  }.  return ma
2d2ff 73 6b 3b 0a 7d 0a 73 74 61 74 69 63 20 42 69 74  sk;.}.static Bit
2d300 6d 61 73 6b 20 65 78 70 72 4c 69 73 74 54 61 62  mask exprListTab
2d301 6c 65 55 73 61 67 65 28 57 68 65 72 65 4d 61 73  leUsage(WhereMas
2d302 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20  kSet *pMaskSet, 
2d303 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29  ExprList *pList)
2d304 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42 69 74  {.  int i;.  Bit
2d305 6d 61 73 6b 20 6d 61 73 6b 20 3d 20 30 3b 0a 20  mask mask = 0;. 
2d306 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20   if( pList ){.  
2d307 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69    for(i=0; i<pLi
2d308 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  st->nExpr; i++){
2d309 0a 20 20 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65  .      mask |= e
2d30a 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d  xprTableUsage(pM
2d30b 61 73 6b 53 65 74 2c 20 70 4c 69 73 74 2d 3e 61  askSet, pList->a
2d30c 5b 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20  [i].pExpr);.    
2d30d 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d  }.  }.  return m
2d30e 61 73 6b 3b 0a 7d 0a 73 74 61 74 69 63 20 42 69  ask;.}.static Bi
2d30f 74 6d 61 73 6b 20 65 78 70 72 53 65 6c 65 63 74  tmask exprSelect
2d310 54 61 62 6c 65 55 73 61 67 65 28 57 68 65 72 65  TableUsage(Where
2d311 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65  MaskSet *pMaskSe
2d312 74 2c 20 53 65 6c 65 63 74 20 2a 70 53 29 7b 0a  t, Select *pS){.
2d313 20 20 42 69 74 6d 61 73 6b 20 6d 61 73 6b 20 3d    Bitmask mask =
2d314 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 70 53 20   0;.  while( pS 
2d315 29 7b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65  ){.    mask |= e
2d316 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67  xprListTableUsag
2d317 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e  e(pMaskSet, pS->
2d318 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 6d 61 73  pEList);.    mas
2d319 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74 54 61 62  k |= exprListTab
2d31a 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
2d31b 2c 20 70 53 2d 3e 70 47 72 6f 75 70 42 79 29 3b  , pS->pGroupBy);
2d31c 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70  .    mask |= exp
2d31d 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28  rListTableUsage(
2d31e 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 4f  pMaskSet, pS->pO
2d31f 72 64 65 72 42 79 29 3b 0a 20 20 20 20 6d 61 73  rderBy);.    mas
2d320 6b 20 7c 3d 20 65 78 70 72 54 61 62 6c 65 55 73  k |= exprTableUs
2d321 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 53  age(pMaskSet, pS
2d322 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 20 20 6d  ->pWhere);.    m
2d323 61 73 6b 20 7c 3d 20 65 78 70 72 54 61 62 6c 65  ask |= exprTable
2d324 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
2d325 70 53 2d 3e 70 48 61 76 69 6e 67 29 3b 0a 20 20  pS->pHaving);.  
2d326 20 20 70 53 20 3d 20 70 53 2d 3e 70 50 72 69 6f    pS = pS->pPrio
2d327 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  r;.  }.  return 
2d328 6d 61 73 6b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  mask;.}../*.** R
2d329 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68  eturn TRUE if th
2d32a 65 20 67 69 76 65 6e 20 6f 70 65 72 61 74 6f 72  e given operator
2d32b 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 6f   is one of the o
2d32c 70 65 72 61 74 6f 72 73 20 74 68 61 74 20 69 73  perators that is
2d32d 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20  .** allowed for 
2d32e 61 6e 20 69 6e 64 65 78 61 62 6c 65 20 57 48 45  an indexable WHE
2d32f 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 2e 20  RE clause term. 
2d330 20 54 68 65 20 61 6c 6c 6f 77 65 64 20 6f 70 65   The allowed ope
2d331 72 61 74 6f 72 73 20 61 72 65 0a 2a 2a 20 22 3d  rators are.** "=
2d332 22 2c 20 22 3c 22 2c 20 22 3e 22 2c 20 22 3c 3d  ", "<", ">", "<=
2d333 22 2c 20 22 3e 3d 22 2c 20 61 6e 64 20 22 49 4e  ", ">=", and "IN
2d334 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  "..*/.static int
2d335 20 61 6c 6c 6f 77 65 64 4f 70 28 69 6e 74 20 6f   allowedOp(int o
2d336 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 54 4b  p){.  assert( TK
2d337 5f 47 54 3e 54 4b 5f 45 51 20 26 26 20 54 4b 5f  _GT>TK_EQ && TK_
2d338 47 54 3c 54 4b 5f 47 45 20 29 3b 0a 20 20 61 73  GT<TK_GE );.  as
2d339 73 65 72 74 28 20 54 4b 5f 4c 54 3e 54 4b 5f 45  sert( TK_LT>TK_E
2d33a 51 20 26 26 20 54 4b 5f 4c 54 3c 54 4b 5f 47 45  Q && TK_LT<TK_GE
2d33b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 54 4b   );.  assert( TK
2d33c 5f 4c 45 3e 54 4b 5f 45 51 20 26 26 20 54 4b 5f  _LE>TK_EQ && TK_
2d33d 4c 45 3c 54 4b 5f 47 45 20 29 3b 0a 20 20 61 73  LE<TK_GE );.  as
2d33e 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 54 4b 5f  sert( TK_GE==TK_
2d33f 45 51 2b 34 20 29 3b 0a 20 20 72 65 74 75 72 6e  EQ+4 );.  return
2d340 20 6f 70 3d 3d 54 4b 5f 49 4e 20 7c 7c 20 28 6f   op==TK_IN || (o
2d341 70 3e 3d 54 4b 5f 45 51 20 26 26 20 6f 70 3c 3d  p>=TK_EQ && op<=
2d342 54 4b 5f 47 45 29 20 7c 7c 20 6f 70 3d 3d 54 4b  TK_GE) || op==TK
2d343 5f 49 53 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a  _ISNULL;.}../*.*
2d344 2a 20 53 77 61 70 20 74 77 6f 20 6f 62 6a 65 63  * Swap two objec
2d345 74 73 20 6f 66 20 74 79 70 65 20 54 59 50 45 2e  ts of type TYPE.
2d346 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 57 41 50  .*/.#define SWAP
2d347 28 54 59 50 45 2c 41 2c 42 29 20 7b 54 59 50 45  (TYPE,A,B) {TYPE
2d348 20 74 3d 41 3b 20 41 3d 42 3b 20 42 3d 74 3b 7d   t=A; A=B; B=t;}
2d349 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 75 74 65 20  ../*.** Commute 
2d34a 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65  a comparison ope
2d34b 72 61 74 6f 72 2e 20 20 45 78 70 72 65 73 73 69  rator.  Expressi
2d34c 6f 6e 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ons of the form 
2d34d 22 58 20 6f 70 20 59 22 0a 2a 2a 20 61 72 65 20  "X op Y".** are 
2d34e 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 22  converted into "
2d34f 59 20 6f 70 20 58 22 2e 0a 2a 2a 0a 2a 2a 20 49  Y op X"..**.** I
2d350 66 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  f a collation se
2d351 71 75 65 6e 63 65 20 69 73 20 61 73 73 6f 63 69  quence is associ
2d352 61 74 65 64 20 77 69 74 68 20 65 69 74 68 65 72  ated with either
2d353 20 74 68 65 20 6c 65 66 74 20 6f 72 20 72 69 67   the left or rig
2d354 68 74 0a 2a 2a 20 73 69 64 65 20 6f 66 20 74 68  ht.** side of th
2d355 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2c 20 69 74  e comparison, it
2d356 20 72 65 6d 61 69 6e 73 20 61 73 73 6f 63 69 61   remains associa
2d357 74 65 64 20 77 69 74 68 20 74 68 65 20 73 61 6d  ted with the sam
2d358 65 20 73 69 64 65 20 61 66 74 65 72 0a 2a 2a 20  e side after.** 
2d359 74 68 65 20 63 6f 6d 6d 75 74 61 74 69 6f 6e 2e  the commutation.
2d35a 20 53 6f 20 22 59 20 63 6f 6c 6c 61 74 65 20 4e   So "Y collate N
2d35b 4f 43 41 53 45 20 6f 70 20 58 22 20 62 65 63 6f  OCASE op X" beco
2d35c 6d 65 73 20 0a 2a 2a 20 22 58 20 63 6f 6c 6c 61  mes .** "X colla
2d35d 74 65 20 4e 4f 43 41 53 45 20 6f 70 20 59 22 2e  te NOCASE op Y".
2d35e 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65   This is because
2d35f 20 61 6e 79 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   any collation s
2d360 65 71 75 65 6e 63 65 20 6f 6e 0a 2a 2a 20 74 68  equence on.** th
2d361 65 20 6c 65 66 74 20 68 61 6e 64 20 73 69 64 65  e left hand side
2d362 20 6f 66 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e   of a comparison
2d363 20 6f 76 65 72 72 69 64 65 73 20 61 6e 79 20 63   overrides any c
2d364 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
2d365 65 20 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 74  e .** attached t
2d366 6f 20 74 68 65 20 72 69 67 68 74 2e 20 46 6f 72  o the right. For
2d367 20 74 68 65 20 73 61 6d 65 20 72 65 61 73 6f 6e   the same reason
2d368 20 74 68 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61   the EP_ExpColla
2d369 74 65 20 66 6c 61 67 0a 2a 2a 20 69 73 20 6e 6f  te flag.** is no
2d36a 74 20 63 6f 6d 6d 75 74 65 64 2e 0a 2a 2f 0a 73  t commuted..*/.s
2d36b 74 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 43  tatic void exprC
2d36c 6f 6d 6d 75 74 65 28 50 61 72 73 65 20 2a 70 50  ommute(Parse *pP
2d36d 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70  arse, Expr *pExp
2d36e 72 29 7b 0a 20 20 75 31 36 20 65 78 70 52 69 67  r){.  u16 expRig
2d36f 68 74 20 3d 20 28 70 45 78 70 72 2d 3e 70 52 69  ht = (pExpr->pRi
2d370 67 68 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  ght->flags & EP_
2d371 45 78 70 43 6f 6c 6c 61 74 65 29 3b 0a 20 20 75  ExpCollate);.  u
2d372 31 36 20 65 78 70 4c 65 66 74 20 3d 20 28 70 45  16 expLeft = (pE
2d373 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 66 6c 61 67  xpr->pLeft->flag
2d374 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74  s & EP_ExpCollat
2d375 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61 6c  e);.  assert( al
2d376 6c 6f 77 65 64 4f 70 28 70 45 78 70 72 2d 3e 6f  lowedOp(pExpr->o
2d377 70 29 20 26 26 20 70 45 78 70 72 2d 3e 6f 70 21  p) && pExpr->op!
2d378 3d 54 4b 5f 49 4e 20 29 3b 0a 20 20 70 45 78 70  =TK_IN );.  pExp
2d379 72 2d 3e 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c  r->pRight->pColl
2d37a 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
2d37b 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45  llSeq(pParse, pE
2d37c 78 70 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20  xpr->pRight);.  
2d37d 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 70 43  pExpr->pLeft->pC
2d37e 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70  oll = sqlite3Exp
2d37f 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  rCollSeq(pParse,
2d380 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a   pExpr->pLeft);.
2d381 20 20 53 57 41 50 28 43 6f 6c 6c 53 65 71 2a 2c    SWAP(CollSeq*,
2d382 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 70  pExpr->pRight->p
2d383 43 6f 6c 6c 2c 70 45 78 70 72 2d 3e 70 4c 65 66  Coll,pExpr->pLef
2d384 74 2d 3e 70 43 6f 6c 6c 29 3b 0a 20 20 70 45 78  t->pColl);.  pEx
2d385 70 72 2d 3e 70 52 69 67 68 74 2d 3e 66 6c 61 67  pr->pRight->flag
2d386 73 20 3d 20 28 70 45 78 70 72 2d 3e 70 52 69 67  s = (pExpr->pRig
2d387 68 74 2d 3e 66 6c 61 67 73 20 26 20 7e 45 50 5f  ht->flags & ~EP_
2d388 45 78 70 43 6f 6c 6c 61 74 65 29 20 7c 20 65 78  ExpCollate) | ex
2d389 70 4c 65 66 74 3b 0a 20 20 70 45 78 70 72 2d 3e  pLeft;.  pExpr->
2d38a 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 3d 20 28  pLeft->flags = (
2d38b 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 66 6c  pExpr->pLeft->fl
2d38c 61 67 73 20 26 20 7e 45 50 5f 45 78 70 43 6f 6c  ags & ~EP_ExpCol
2d38d 6c 61 74 65 29 20 7c 20 65 78 70 52 69 67 68 74  late) | expRight
2d38e 3b 0a 20 20 53 57 41 50 28 45 78 70 72 2a 2c 70  ;.  SWAP(Expr*,p
2d38f 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 70 45 78  Expr->pRight,pEx
2d390 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 69 66  pr->pLeft);.  if
2d391 28 20 70 45 78 70 72 2d 3e 6f 70 3e 3d 54 4b 5f  ( pExpr->op>=TK_
2d392 47 54 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  GT ){.    assert
2d393 28 20 54 4b 5f 4c 54 3d 3d 54 4b 5f 47 54 2b 32  ( TK_LT==TK_GT+2
2d394 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
2d395 54 4b 5f 47 45 3d 3d 54 4b 5f 4c 45 2b 32 20 29  TK_GE==TK_LE+2 )
2d396 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 54 4b  ;.    assert( TK
2d397 5f 47 54 3e 54 4b 5f 45 51 20 29 3b 0a 20 20 20  _GT>TK_EQ );.   
2d398 20 61 73 73 65 72 74 28 20 54 4b 5f 47 54 3c 54   assert( TK_GT<T
2d399 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 61 73 73 65  K_LE );.    asse
2d39a 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 3e 3d 54  rt( pExpr->op>=T
2d39b 4b 5f 47 54 20 26 26 20 70 45 78 70 72 2d 3e 6f  K_GT && pExpr->o
2d39c 70 3c 3d 54 4b 5f 47 45 20 29 3b 0a 20 20 20 20  p<=TK_GE );.    
2d39d 70 45 78 70 72 2d 3e 6f 70 20 3d 20 28 28 70 45  pExpr->op = ((pE
2d39e 78 70 72 2d 3e 6f 70 2d 54 4b 5f 47 54 29 5e 32  xpr->op-TK_GT)^2
2d39f 29 2b 54 4b 5f 47 54 3b 0a 20 20 7d 0a 7d 0a 0a  )+TK_GT;.  }.}..
2d3a0 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20  /*.** Translate 
2d3a1 66 72 6f 6d 20 54 4b 5f 78 78 20 6f 70 65 72 61  from TK_xx opera
2d3a2 74 6f 72 20 74 6f 20 57 4f 5f 78 78 20 62 69 74  tor to WO_xx bit
2d3a3 6d 61 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  mask..*/.static 
2d3a4 75 31 36 20 6f 70 65 72 61 74 6f 72 4d 61 73 6b  u16 operatorMask
2d3a5 28 69 6e 74 20 6f 70 29 7b 0a 20 20 75 31 36 20  (int op){.  u16 
2d3a6 63 3b 0a 20 20 61 73 73 65 72 74 28 20 61 6c 6c  c;.  assert( all
2d3a7 6f 77 65 64 4f 70 28 6f 70 29 20 29 3b 0a 20 20  owedOp(op) );.  
2d3a8 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 4e 20 29 7b  if( op==TK_IN ){
2d3a9 0a 20 20 20 20 63 20 3d 20 57 4f 5f 49 4e 3b 0a  .    c = WO_IN;.
2d3aa 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d    }else if( op==
2d3ab 54 4b 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20  TK_ISNULL ){.   
2d3ac 20 63 20 3d 20 57 4f 5f 49 53 4e 55 4c 4c 3b 0a   c = WO_ISNULL;.
2d3ad 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
2d3ae 65 72 74 28 20 28 57 4f 5f 45 51 3c 3c 28 6f 70  ert( (WO_EQ<<(op
2d3af 2d 54 4b 5f 45 51 29 29 20 3c 20 30 78 37 66 66  -TK_EQ)) < 0x7ff
2d3b0 66 20 29 3b 0a 20 20 20 20 63 20 3d 20 28 75 31  f );.    c = (u1
2d3b1 36 29 28 57 4f 5f 45 51 3c 3c 28 6f 70 2d 54 4b  6)(WO_EQ<<(op-TK
2d3b2 5f 45 51 29 29 3b 0a 20 20 7d 0a 20 20 61 73 73  _EQ));.  }.  ass
2d3b3 65 72 74 28 20 6f 70 21 3d 54 4b 5f 49 53 4e 55  ert( op!=TK_ISNU
2d3b4 4c 4c 20 7c 7c 20 63 3d 3d 57 4f 5f 49 53 4e 55  LL || c==WO_ISNU
2d3b5 4c 4c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  LL );.  assert( 
2d3b6 6f 70 21 3d 54 4b 5f 49 4e 20 7c 7c 20 63 3d 3d  op!=TK_IN || c==
2d3b7 57 4f 5f 49 4e 20 29 3b 0a 20 20 61 73 73 65 72  WO_IN );.  asser
2d3b8 74 28 20 6f 70 21 3d 54 4b 5f 45 51 20 7c 7c 20  t( op!=TK_EQ || 
2d3b9 63 3d 3d 57 4f 5f 45 51 20 29 3b 0a 20 20 61 73  c==WO_EQ );.  as
2d3ba 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 4c 54 20  sert( op!=TK_LT 
2d3bb 7c 7c 20 63 3d 3d 57 4f 5f 4c 54 20 29 3b 0a 20  || c==WO_LT );. 
2d3bc 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f   assert( op!=TK_
2d3bd 4c 45 20 7c 7c 20 63 3d 3d 57 4f 5f 4c 45 20 29  LE || c==WO_LE )
2d3be 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d  ;.  assert( op!=
2d3bf 54 4b 5f 47 54 20 7c 7c 20 63 3d 3d 57 4f 5f 47  TK_GT || c==WO_G
2d3c0 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f  T );.  assert( o
2d3c1 70 21 3d 54 4b 5f 47 45 20 7c 7c 20 63 3d 3d 57  p!=TK_GE || c==W
2d3c2 4f 5f 47 45 20 29 3b 0a 20 20 72 65 74 75 72 6e  O_GE );.  return
2d3c3 20 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 61   c;.}../*.** Sea
2d3c4 72 63 68 20 66 6f 72 20 61 20 74 65 72 6d 20 69  rch for a term i
2d3c5 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  n the WHERE clau
2d3c6 73 65 20 74 68 61 74 20 69 73 20 6f 66 20 74 68  se that is of th
2d3c7 65 20 66 6f 72 6d 20 22 58 20 3c 6f 70 3e 20 3c  e form "X <op> <
2d3c8 65 78 70 72 3e 22 0a 2a 2a 20 77 68 65 72 65 20  expr>".** where 
2d3c9 58 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 65  X is a reference
2d3ca 20 74 6f 20 74 68 65 20 69 43 6f 6c 75 6d 6e 20   to the iColumn 
2d3cb 6f 66 20 74 61 62 6c 65 20 69 43 75 72 20 61 6e  of table iCur an
2d3cc 64 20 3c 6f 70 3e 20 69 73 20 6f 6e 65 20 6f 66  d <op> is one of
2d3cd 0a 2a 2a 20 74 68 65 20 57 4f 5f 78 78 20 6f 70  .** the WO_xx op
2d3ce 65 72 61 74 6f 72 20 63 6f 64 65 73 20 73 70 65  erator codes spe
2d3cf 63 69 66 69 65 64 20 62 79 20 74 68 65 20 6f 70  cified by the op
2d3d0 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 52   parameter..** R
2d3d1 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
2d3d2 74 6f 20 74 68 65 20 74 65 72 6d 2e 20 20 52 65  to the term.  Re
2d3d3 74 75 72 6e 20 30 20 69 66 20 6e 6f 74 20 66 6f  turn 0 if not fo
2d3d4 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57  und..*/.static W
2d3d5 68 65 72 65 54 65 72 6d 20 2a 66 69 6e 64 54 65  hereTerm *findTe
2d3d6 72 6d 28 0a 20 20 57 68 65 72 65 43 6c 61 75 73  rm(.  WhereClaus
2d3d7 65 20 2a 70 57 43 2c 20 20 20 20 20 2f 2a 20 54  e *pWC,     /* T
2d3d8 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2d3d9 74 6f 20 62 65 20 73 65 61 72 63 68 65 64 20 2a  to be searched *
2d3da 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20  /.  int iCur,   
2d3db 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
2d3dc 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 4c 48  sor number of LH
2d3dd 53 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75  S */.  int iColu
2d3de 6d 6e 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  mn,          /* 
2d3df 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 6f 66  Column number of
2d3e0 20 4c 48 53 20 2a 2f 0a 20 20 42 69 74 6d 61 73   LHS */.  Bitmas
2d3e1 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20  k notReady,     
2d3e2 2f 2a 20 52 48 53 20 6d 75 73 74 20 6e 6f 74 20  /* RHS must not 
2d3e3 6f 76 65 72 6c 61 70 20 77 69 74 68 20 74 68 69  overlap with thi
2d3e4 73 20 6d 61 73 6b 20 2a 2f 0a 20 20 75 33 32 20  s mask */.  u32 
2d3e5 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  op,             
2d3e6 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 57 4f 5f    /* Mask of WO_
2d3e7 78 78 20 76 61 6c 75 65 73 20 64 65 73 63 72 69  xx values descri
2d3e8 62 69 6e 67 20 6f 70 65 72 61 74 6f 72 20 2a 2f  bing operator */
2d3e9 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20 20  .  Index *pIdx  
2d3ea 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 73 74           /* Must
2d3eb 20 62 65 20 63 6f 6d 70 61 74 69 62 6c 65 20 77   be compatible w
2d3ec 69 74 68 20 74 68 69 73 20 69 6e 64 65 78 2c 20  ith this index, 
2d3ed 69 66 20 6e 6f 74 20 4e 55 4c 4c 20 2a 2f 0a 29  if not NULL */.)
2d3ee 7b 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70  {.  WhereTerm *p
2d3ef 54 65 72 6d 3b 0a 20 20 69 6e 74 20 6b 3b 0a 20  Term;.  int k;. 
2d3f0 20 61 73 73 65 72 74 28 20 69 43 75 72 3e 3d 30   assert( iCur>=0
2d3f1 20 29 3b 0a 20 20 6f 70 20 26 3d 20 57 4f 5f 41   );.  op &= WO_A
2d3f2 4c 4c 3b 0a 20 20 66 6f 72 28 70 54 65 72 6d 3d  LL;.  for(pTerm=
2d3f3 70 57 43 2d 3e 61 2c 20 6b 3d 70 57 43 2d 3e 6e  pWC->a, k=pWC->n
2d3f4 54 65 72 6d 3b 20 6b 3b 20 6b 2d 2d 2c 20 70 54  Term; k; k--, pT
2d3f5 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  erm++){.    if( 
2d3f6 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  pTerm->leftCurso
2d3f7 72 3d 3d 69 43 75 72 0a 20 20 20 20 20 20 20 26  r==iCur.       &
2d3f8 26 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71  & (pTerm->prereq
2d3f9 52 69 67 68 74 20 26 20 6e 6f 74 52 65 61 64 79  Right & notReady
2d3fa 29 3d 3d 30 0a 20 20 20 20 20 20 20 26 26 20 70  )==0.       && p
2d3fb 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75  Term->u.leftColu
2d3fc 6d 6e 3d 3d 69 43 6f 6c 75 6d 6e 0a 20 20 20 20  mn==iColumn.    
2d3fd 20 20 20 26 26 20 28 70 54 65 72 6d 2d 3e 65 4f     && (pTerm->eO
2d3fe 70 65 72 61 74 6f 72 20 26 20 6f 70 29 21 3d 30  perator & op)!=0
2d3ff 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66  .    ){.      if
2d400 28 20 70 49 64 78 20 26 26 20 70 54 65 72 6d 2d  ( pIdx && pTerm-
2d401 3e 65 4f 70 65 72 61 74 6f 72 21 3d 57 4f 5f 49  >eOperator!=WO_I
2d402 53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20  SNULL ){.       
2d403 20 45 78 70 72 20 2a 70 58 20 3d 20 70 54 65 72   Expr *pX = pTer
2d404 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  m->pExpr;.      
2d405 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
2d406 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 69  ;.        char i
2d407 64 78 61 66 66 3b 0a 20 20 20 20 20 20 20 20 69  dxaff;.        i
2d408 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 50 61  nt j;.        Pa
2d409 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70 57  rse *pParse = pW
2d40a 43 2d 3e 70 50 61 72 73 65 3b 0a 0a 20 20 20 20  C->pParse;..    
2d40b 20 20 20 20 69 64 78 61 66 66 20 3d 20 70 49 64      idxaff = pId
2d40c 78 2d 3e 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 5b  x->pTable->aCol[
2d40d 69 43 6f 6c 75 6d 6e 5d 2e 61 66 66 69 6e 69 74  iColumn].affinit
2d40e 79 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  y;.        if( !
2d40f 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69  sqlite3IndexAffi
2d410 6e 69 74 79 4f 6b 28 70 58 2c 20 69 64 78 61 66  nityOk(pX, idxaf
2d411 66 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a  f) ) continue;..
2d412 20 20 20 20 20 20 20 20 2f 2a 20 46 69 67 75 72          /* Figur
2d413 65 20 6f 75 74 20 74 68 65 20 63 6f 6c 6c 61 74  e out the collat
2d414 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 72 65 71  ion sequence req
2d415 75 69 72 65 64 20 66 72 6f 6d 20 61 6e 20 69 6e  uired from an in
2d416 64 65 78 20 66 6f 72 0a 20 20 20 20 20 20 20 20  dex for.        
2d417 2a 2a 20 69 74 20 74 6f 20 62 65 20 75 73 65 66  ** it to be usef
2d418 75 6c 20 66 6f 72 20 6f 70 74 69 6d 69 73 69 6e  ul for optimisin
2d419 67 20 65 78 70 72 65 73 73 69 6f 6e 20 70 58 2e  g expression pX.
2d41a 20 53 74 6f 72 65 20 74 68 69 73 0a 20 20 20 20   Store this.    
2d41b 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 69 6e 20      ** value in 
2d41c 76 61 72 69 61 62 6c 65 20 70 43 6f 6c 6c 2e 0a  variable pColl..
2d41d 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
2d41e 20 20 20 61 73 73 65 72 74 28 70 58 2d 3e 70 4c     assert(pX->pL
2d41f 65 66 74 29 3b 0a 20 20 20 20 20 20 20 20 70 43  eft);.        pC
2d420 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 42 69 6e  oll = sqlite3Bin
2d421 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65  aryCompareCollSe
2d422 71 28 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 4c  q(pParse, pX->pL
2d423 65 66 74 2c 20 70 58 2d 3e 70 52 69 67 68 74 29  eft, pX->pRight)
2d424 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
2d425 28 70 43 6f 6c 6c 20 7c 7c 20 70 50 61 72 73 65  (pColl || pParse
2d426 2d 3e 6e 45 72 72 29 3b 0a 0a 20 20 20 20 20 20  ->nErr);..      
2d427 20 20 66 6f 72 28 6a 3d 30 3b 20 70 49 64 78 2d    for(j=0; pIdx-
2d428 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 21 3d 69 43  >aiColumn[j]!=iC
2d429 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20  olumn; j++){.   
2d42a 20 20 20 20 20 20 20 69 66 28 20 4e 45 56 45 52         if( NEVER
2d42b 28 6a 3e 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  (j>=pIdx->nColum
2d42c 6e 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  n) ) return 0;. 
2d42d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2d42e 20 69 66 28 20 70 43 6f 6c 6c 20 26 26 20 73 71   if( pColl && sq
2d42f 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f  lite3StrICmp(pCo
2d430 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20 70 49 64 78 2d  ll->zName, pIdx-
2d431 3e 61 7a 43 6f 6c 6c 5b 6a 5d 29 20 29 20 63 6f  >azColl[j]) ) co
2d432 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a  ntinue;.      }.
2d433 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 54 65        return pTe
2d434 72 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  rm;.    }.  }.  
2d435 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20  return 0;.}../* 
2d436 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e 63  Forward referenc
2d437 65 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  e */.static void
2d438 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 53 72 63   exprAnalyze(Src
2d439 4c 69 73 74 2a 2c 20 57 68 65 72 65 43 6c 61 75  List*, WhereClau
2d43a 73 65 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a  se*, int);../*.*
2d43b 2a 20 43 61 6c 6c 20 65 78 70 72 41 6e 61 6c 79  * Call exprAnaly
2d43c 7a 65 20 6f 6e 20 61 6c 6c 20 74 65 72 6d 73 20  ze on all terms 
2d43d 69 6e 20 61 20 57 48 45 52 45 20 63 6c 61 75 73  in a WHERE claus
2d43e 65 2e 20 20 0a 2a 2a 0a 2a 2a 0a 2a 2f 0a 73 74  e.  .**.**.*/.st
2d43f 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 41 6e  atic void exprAn
2d440 61 6c 79 7a 65 41 6c 6c 28 0a 20 20 53 72 63 4c  alyzeAll(.  SrcL
2d441 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20  ist *pTabList,  
2d442 20 20 20 20 20 2f 2a 20 74 68 65 20 46 52 4f 4d       /* the FROM
2d443 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65   clause */.  Whe
2d444 72 65 43 6c 61 75 73 65 20 2a 70 57 43 20 20 20  reClause *pWC   
2d445 20 20 20 20 20 20 2f 2a 20 74 68 65 20 57 48 45        /* the WHE
2d446 52 45 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20  RE clause to be 
2d447 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 29 7b 0a 20  analyzed */.){. 
2d448 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
2d449 70 57 43 2d 3e 6e 54 65 72 6d 2d 31 3b 20 69 3e  pWC->nTerm-1; i>
2d44a 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 65 78  =0; i--){.    ex
2d44b 70 72 41 6e 61 6c 79 7a 65 28 70 54 61 62 4c 69  prAnalyze(pTabLi
2d44c 73 74 2c 20 70 57 43 2c 20 69 29 3b 0a 20 20 7d  st, pWC, i);.  }
2d44d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
2d44e 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54  TE_OMIT_LIKE_OPT
2d44f 49 4d 49 5a 41 54 49 4f 4e 0a 2f 2a 0a 2a 2a 20  IMIZATION./*.** 
2d450 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
2d451 74 68 65 20 67 69 76 65 6e 20 65 78 70 72 65 73  the given expres
2d452 73 69 6f 6e 20 69 73 20 61 20 4c 49 4b 45 20 6f  sion is a LIKE o
2d453 72 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 20  r GLOB operator 
2d454 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 6f  that.** can be o
2d455 70 74 69 6d 69 7a 65 64 20 75 73 69 6e 67 20 69  ptimized using i
2d456 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72  nequality constr
2d457 61 69 6e 74 73 2e 20 20 52 65 74 75 72 6e 20 54  aints.  Return T
2d458 52 55 45 20 69 66 20 69 74 20 69 73 0a 2a 2a 20  RUE if it is.** 
2d459 73 6f 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20  so and false if 
2d45a 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 72  not..**.** In or
2d45b 64 65 72 20 66 6f 72 20 74 68 65 20 6f 70 65 72  der for the oper
2d45c 61 74 6f 72 20 74 6f 20 62 65 20 6f 70 74 69 6d  ator to be optim
2d45d 69 7a 69 62 6c 65 2c 20 74 68 65 20 52 48 53 20  izible, the RHS 
2d45e 6d 75 73 74 20 62 65 20 61 20 73 74 72 69 6e 67  must be a string
2d45f 0a 2a 2a 20 6c 69 74 65 72 61 6c 20 74 68 61 74  .** literal that
2d460 20 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e 20   does not begin 
2d461 77 69 74 68 20 61 20 77 69 6c 64 63 61 72 64 2e  with a wildcard.
2d462 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74    .*/.static int
2d463 20 69 73 4c 69 6b 65 4f 72 47 6c 6f 62 28 0a 20   isLikeOrGlob(. 
2d464 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2d465 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e     /* Parsing an
2d466 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e  d code generatin
2d467 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45  g context */.  E
2d468 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20  xpr *pExpr,     
2d469 20 2f 2a 20 54 65 73 74 20 74 68 69 73 20 65 78   /* Test this ex
2d46a 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e  pression */.  in
2d46b 74 20 2a 70 6e 50 61 74 74 65 72 6e 2c 20 20 20  t *pnPattern,   
2d46c 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e  /* Number of non
2d46d 2d 77 69 6c 64 63 61 72 64 20 70 72 65 66 69 78  -wildcard prefix
2d46e 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f 0a 20   characters */. 
2d46f 20 69 6e 74 20 2a 70 69 73 43 6f 6d 70 6c 65 74   int *pisComplet
2d470 65 2c 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68  e, /* True if th
2d471 65 20 6f 6e 6c 79 20 77 69 6c 64 63 61 72 64 20  e only wildcard 
2d472 69 73 20 25 20 69 6e 20 74 68 65 20 6c 61 73 74  is % in the last
2d473 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20   character */.  
2d474 69 6e 74 20 2a 70 6e 6f 43 61 73 65 20 20 20 20  int *pnoCase    
2d475 20 20 2f 2a 20 54 72 75 65 20 69 66 20 75 70 70    /* True if upp
2d476 65 72 63 61 73 65 20 69 73 20 65 71 75 69 76 61  ercase is equiva
2d477 6c 65 6e 74 20 74 6f 20 6c 6f 77 65 72 63 61 73  lent to lowercas
2d478 65 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  e */.){.  const 
2d479 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20  char *z;        
2d47a 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f       /* String o
2d47b 6e 20 52 48 53 20 6f 66 20 4c 49 4b 45 20 6f 70  n RHS of LIKE op
2d47c 65 72 61 74 6f 72 20 2a 2f 0a 20 20 45 78 70 72  erator */.  Expr
2d47d 20 2a 70 52 69 67 68 74 2c 20 2a 70 4c 65 66 74   *pRight, *pLeft
2d47e 3b 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20  ;      /* Right 
2d47f 61 6e 64 20 6c 65 66 74 20 73 69 7a 65 20 6f 66  and left size of
2d480 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 2a   LIKE operator *
2d481 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c  /.  ExprList *pL
2d482 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ist;           /
2d483 2a 20 4c 69 73 74 20 6f 66 20 6f 70 65 72 61 6e  * List of operan
2d484 64 73 20 74 6f 20 74 68 65 20 4c 49 4b 45 20 6f  ds to the LIKE o
2d485 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74  perator */.  int
2d486 20 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   c;             
2d487 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63          /* One c
2d488 68 61 72 61 63 74 65 72 20 69 6e 20 7a 5b 5d 20  haracter in z[] 
2d489 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20  */.  int cnt;   
2d48a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d48b 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e  /* Number of non
2d48c 2d 77 69 6c 64 63 61 72 64 20 70 72 65 66 69 78  -wildcard prefix
2d48d 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f 0a 20   characters */. 
2d48e 20 63 68 61 72 20 77 63 5b 33 5d 3b 20 20 20 20   char wc[3];    
2d48f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
2d490 69 6c 64 63 61 72 64 20 63 68 61 72 61 63 74 65  ildcard characte
2d491 72 73 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20  rs */.  CollSeq 
2d492 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20  *pColl;         
2d493 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20     /* Collating 
2d494 73 65 71 75 65 6e 63 65 20 66 6f 72 20 4c 48 53  sequence for LHS
2d495 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
2d496 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20  b = pParse->db; 
2d497 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
2d498 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66  nection */..  if
2d499 28 20 21 73 71 6c 69 74 65 33 49 73 4c 69 6b 65  ( !sqlite3IsLike
2d49a 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 45 78  Function(db, pEx
2d49b 70 72 2c 20 70 6e 6f 43 61 73 65 2c 20 77 63 29  pr, pnoCase, wc)
2d49c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
2d49d 3b 0a 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c  ;.  }.#ifdef SQL
2d49e 49 54 45 5f 45 42 43 44 49 43 0a 20 20 69 66 28  ITE_EBCDIC.  if(
2d49f 20 2a 70 6e 6f 43 61 73 65 20 29 20 72 65 74 75   *pnoCase ) retu
2d4a0 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 70  rn 0;.#endif.  p
2d4a1 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e  List = pExpr->x.
2d4a2 70 4c 69 73 74 3b 0a 20 20 70 52 69 67 68 74 20  pList;.  pRight 
2d4a3 3d 20 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  = pList->a[0].pE
2d4a4 78 70 72 3b 0a 20 20 69 66 28 20 70 52 69 67 68  xpr;.  if( pRigh
2d4a5 74 2d 3e 6f 70 21 3d 54 4b 5f 53 54 52 49 4e 47  t->op!=TK_STRING
2d4a6 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
2d4a7 3b 0a 20 20 7d 0a 20 20 70 4c 65 66 74 20 3d 20  ;.  }.  pLeft = 
2d4a8 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70  pList->a[1].pExp
2d4a9 72 3b 0a 20 20 69 66 28 20 70 4c 65 66 74 2d 3e  r;.  if( pLeft->
2d4aa 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b  op!=TK_COLUMN ){
2d4ab 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
2d4ac 20 7d 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c   }.  pColl = sql
2d4ad 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28  ite3ExprCollSeq(
2d4ae 70 50 61 72 73 65 2c 20 70 4c 65 66 74 29 3b 0a  pParse, pLeft);.
2d4af 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c 6c 21    assert( pColl!
2d4b0 3d 30 20 7c 7c 20 70 4c 65 66 74 2d 3e 69 43 6f  =0 || pLeft->iCo
2d4b1 6c 75 6d 6e 3d 3d 2d 31 20 29 3b 0a 20 20 69 66  lumn==-1 );.  if
2d4b2 28 20 70 43 6f 6c 6c 3d 3d 30 20 29 20 72 65 74  ( pColl==0 ) ret
2d4b3 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 28 70 43  urn 0;.  if( (pC
2d4b4 6f 6c 6c 2d 3e 74 79 70 65 21 3d 53 51 4c 49 54  oll->type!=SQLIT
2d4b5 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 20 7c 7c  E_COLL_BINARY ||
2d4b6 20 2a 70 6e 6f 43 61 73 65 29 20 26 26 0a 20 20   *pnoCase) &&.  
2d4b7 20 20 20 20 28 70 43 6f 6c 6c 2d 3e 74 79 70 65      (pColl->type
2d4b8 21 3d 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e 4f  !=SQLITE_COLL_NO
2d4b9 43 41 53 45 20 7c 7c 20 21 2a 70 6e 6f 43 61 73  CASE || !*pnoCas
2d4ba 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  e) ){.    return
2d4bb 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71   0;.  }.  if( sq
2d4bc 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74  lite3ExprAffinit
2d4bd 79 28 70 4c 65 66 74 29 21 3d 53 51 4c 49 54 45  y(pLeft)!=SQLITE
2d4be 5f 41 46 46 5f 54 45 58 54 20 29 20 72 65 74 75  _AFF_TEXT ) retu
2d4bf 72 6e 20 30 3b 0a 20 20 7a 20 3d 20 70 52 69 67  rn 0;.  z = pRig
2d4c0 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20  ht->u.zToken;.  
2d4c1 69 66 28 20 41 4c 57 41 59 53 28 7a 29 20 29 7b  if( ALWAYS(z) ){
2d4c2 0a 20 20 20 20 63 6e 74 20 3d 20 30 3b 0a 20 20  .    cnt = 0;.  
2d4c3 20 20 77 68 69 6c 65 28 20 28 63 3d 7a 5b 63 6e    while( (c=z[cn
2d4c4 74 5d 29 21 3d 30 20 26 26 20 63 21 3d 77 63 5b  t])!=0 && c!=wc[
2d4c5 30 5d 20 26 26 20 63 21 3d 77 63 5b 31 5d 20 26  0] && c!=wc[1] &
2d4c6 26 20 63 21 3d 77 63 5b 32 5d 20 29 7b 0a 20 20  & c!=wc[2] ){.  
2d4c7 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 7d      cnt++;.    }
2d4c8 0a 20 20 20 20 69 66 28 20 63 6e 74 21 3d 30 20  .    if( cnt!=0 
2d4c9 26 26 20 63 21 3d 30 20 26 26 20 32 35 35 21 3d  && c!=0 && 255!=
2d4ca 28 75 38 29 7a 5b 63 6e 74 2d 31 5d 20 29 7b 0a  (u8)z[cnt-1] ){.
2d4cb 20 20 20 20 20 20 2a 70 69 73 43 6f 6d 70 6c 65        *pisComple
2d4cc 74 65 20 3d 20 7a 5b 63 6e 74 5d 3d 3d 77 63 5b  te = z[cnt]==wc[
2d4cd 30 5d 20 26 26 20 7a 5b 63 6e 74 2b 31 5d 3d 3d  0] && z[cnt+1]==
2d4ce 30 3b 0a 20 20 20 20 20 20 2a 70 6e 50 61 74 74  0;.      *pnPatt
2d4cf 65 72 6e 20 3d 20 63 6e 74 3b 0a 20 20 20 20 20  ern = cnt;.     
2d4d0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
2d4d1 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
2d4d2 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
2d4d3 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50  ITE_OMIT_LIKE_OP
2d4d4 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 0a  TIMIZATION */...
2d4d5 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2d4d6 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2d4d7 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
2d4d8 73 65 65 20 69 66 20 74 68 65 20 67 69 76 65 6e  see if the given
2d4d9 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6f   expression is o
2d4da 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a  f the form.**.**
2d4db 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 20           column 
2d4dc 4d 41 54 43 48 20 65 78 70 72 0a 2a 2a 0a 2a 2a  MATCH expr.**.**
2d4dd 20 49 66 20 69 74 20 69 73 20 74 68 65 6e 20 72   If it is then r
2d4de 65 74 75 72 6e 20 54 52 55 45 2e 20 20 49 66 20  eturn TRUE.  If 
2d4df 6e 6f 74 2c 20 72 65 74 75 72 6e 20 46 41 4c 53  not, return FALS
2d4e0 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  E..*/.static int
2d4e1 20 69 73 4d 61 74 63 68 4f 66 43 6f 6c 75 6d 6e   isMatchOfColumn
2d4e2 28 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20  (.  Expr *pExpr 
2d4e3 20 20 20 20 20 2f 2a 20 54 65 73 74 20 74 68 69       /* Test thi
2d4e4 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  s expression */.
2d4e5 29 7b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  ){.  ExprList *p
2d4e6 4c 69 73 74 3b 0a 0a 20 20 69 66 28 20 70 45 78  List;..  if( pEx
2d4e7 70 72 2d 3e 6f 70 21 3d 54 4b 5f 46 55 4e 43 54  pr->op!=TK_FUNCT
2d4e8 49 4f 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ION ){.    retur
2d4e9 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  n 0;.  }.  if( s
2d4ea 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 45  qlite3StrICmp(pE
2d4eb 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 22 6d  xpr->u.zToken,"m
2d4ec 61 74 63 68 22 29 21 3d 30 20 29 7b 0a 20 20 20  atch")!=0 ){.   
2d4ed 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
2d4ee 20 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e   pList = pExpr->
2d4ef 78 2e 70 4c 69 73 74 3b 0a 20 20 69 66 28 20 70  x.pList;.  if( p
2d4f0 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 32 20 29  List->nExpr!=2 )
2d4f1 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
2d4f2 20 20 7d 0a 20 20 69 66 28 20 70 4c 69 73 74 2d    }.  if( pList-
2d4f3 3e 61 5b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 20  >a[1].pExpr->op 
2d4f4 21 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a  != TK_COLUMN ){.
2d4f5 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
2d4f6 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  }.  return 1;.}.
2d4f7 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2d4f8 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2d4f9 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  LE */../*.** If 
2d4fa 74 68 65 20 70 42 61 73 65 20 65 78 70 72 65 73  the pBase expres
2d4fb 73 69 6f 6e 20 6f 72 69 67 69 6e 61 74 65 64 20  sion originated 
2d4fc 69 6e 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49  in the ON or USI
2d4fd 4e 47 20 63 6c 61 75 73 65 20 6f 66 0a 2a 2a 20  NG clause of.** 
2d4fe 61 20 6a 6f 69 6e 2c 20 74 68 65 6e 20 74 72 61  a join, then tra
2d4ff 6e 73 66 65 72 20 74 68 65 20 61 70 70 72 6f 70  nsfer the approp
2d500 72 69 61 74 65 20 6d 61 72 6b 69 6e 67 73 20 6f  riate markings o
2d501 76 65 72 20 74 6f 20 64 65 72 69 76 65 64 2e 0a  ver to derived..
2d502 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74  */.static void t
2d503 72 61 6e 73 66 65 72 4a 6f 69 6e 4d 61 72 6b 69  ransferJoinMarki
2d504 6e 67 73 28 45 78 70 72 20 2a 70 44 65 72 69 76  ngs(Expr *pDeriv
2d505 65 64 2c 20 45 78 70 72 20 2a 70 42 61 73 65 29  ed, Expr *pBase)
2d506 7b 0a 20 20 70 44 65 72 69 76 65 64 2d 3e 66 6c  {.  pDerived->fl
2d507 61 67 73 20 7c 3d 20 70 42 61 73 65 2d 3e 66 6c  ags |= pBase->fl
2d508 61 67 73 20 26 20 45 50 5f 46 72 6f 6d 4a 6f 69  ags & EP_FromJoi
2d509 6e 3b 0a 20 20 70 44 65 72 69 76 65 64 2d 3e 69  n;.  pDerived->i
2d50a 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 3d  RightJoinTable =
2d50b 20 70 42 61 73 65 2d 3e 69 52 69 67 68 74 4a 6f   pBase->iRightJo
2d50c 69 6e 54 61 62 6c 65 3b 0a 7d 0a 0a 23 69 66 20  inTable;.}..#if 
2d50d 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2d50e 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41  OMIT_OR_OPTIMIZA
2d50f 54 49 4f 4e 29 20 26 26 20 21 64 65 66 69 6e 65  TION) && !define
2d510 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  d(SQLITE_OMIT_SU
2d511 42 51 55 45 52 59 29 0a 2f 2a 0a 2a 2a 20 41 6e  BQUERY)./*.** An
2d512 61 6c 79 7a 65 20 61 20 74 65 72 6d 20 74 68 61  alyze a term tha
2d513 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 77  t consists of tw
2d514 6f 20 6f 72 20 6d 6f 72 65 20 4f 52 2d 63 6f 6e  o or more OR-con
2d515 6e 65 63 74 65 64 0a 2a 2a 20 73 75 62 74 65 72  nected.** subter
2d516 6d 73 2e 20 20 53 6f 20 69 6e 3a 0a 2a 2a 0a 2a  ms.  So in:.**.*
2d517 2a 20 20 20 20 20 2e 2e 2e 20 57 48 45 52 45 20  *     ... WHERE 
2d518 20 28 61 3d 35 29 20 41 4e 44 20 28 62 3d 37 20   (a=5) AND (b=7 
2d519 4f 52 20 63 3d 39 20 4f 52 20 64 3d 31 33 29 20  OR c=9 OR d=13) 
2d51a 41 4e 44 20 28 64 3d 31 33 29 0a 2a 2a 20 20 20  AND (d=13).**   
2d51b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d51c 20 20 20 20 20 20 20 5e 5e 5e 5e 5e 5e 5e 5e 5e         ^^^^^^^^^
2d51d 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 0a 2a 2a 0a 2a  ^^^^^^^^^^^.**.*
2d51e 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
2d51f 6e 61 6c 79 7a 65 73 20 74 65 72 6d 73 20 73 75  nalyzes terms su
2d520 63 68 20 61 73 20 74 68 65 20 6d 69 64 64 6c 65  ch as the middle
2d521 20 74 65 72 6d 20 69 6e 20 74 68 65 20 61 62 6f   term in the abo
2d522 76 65 20 65 78 61 6d 70 6c 65 2e 0a 2a 2a 20 41  ve example..** A
2d523 20 57 68 65 72 65 4f 72 54 65 72 6d 20 6f 62 6a   WhereOrTerm obj
2d524 65 63 74 20 69 73 20 63 6f 6d 70 75 74 65 64 20  ect is computed 
2d525 61 6e 64 20 61 74 74 61 63 68 65 64 20 74 6f 20  and attached to 
2d526 74 68 65 20 74 65 72 6d 20 75 6e 64 65 72 0a 2a  the term under.*
2d527 2a 20 61 6e 61 6c 79 73 69 73 2c 20 72 65 67 61  * analysis, rega
2d528 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 6f 75  rdless of the ou
2d529 74 63 6f 6d 65 20 6f 66 20 74 68 65 20 61 6e 61  tcome of the ana
2d52a 6c 79 73 69 73 2e 20 20 48 65 6e 63 65 3a 0a 2a  lysis.  Hence:.*
2d52b 2a 0a 2a 2a 20 20 20 20 20 57 68 65 72 65 54 65  *.**     WhereTe
2d52c 72 6d 2e 77 74 46 6c 61 67 73 20 20 20 7c 3d 20  rm.wtFlags   |= 
2d52d 20 54 45 52 4d 5f 4f 52 49 4e 46 4f 0a 2a 2a 20   TERM_ORINFO.** 
2d52e 20 20 20 20 57 68 65 72 65 54 65 72 6d 2e 75 2e      WhereTerm.u.
2d52f 70 4f 72 49 6e 66 6f 20 20 3d 20 20 61 20 64 79  pOrInfo  =  a dy
2d530 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
2d531 74 65 64 20 57 68 65 72 65 4f 72 54 65 72 6d 20  ted WhereOrTerm 
2d532 6f 62 6a 65 63 74 0a 2a 2a 0a 2a 2a 20 54 68 65  object.**.** The
2d533 20 74 65 72 6d 20 62 65 69 6e 67 20 61 6e 61 6c   term being anal
2d534 79 7a 65 64 20 6d 75 73 74 20 68 61 76 65 20 74  yzed must have t
2d535 77 6f 20 6f 72 20 6d 6f 72 65 20 6f 66 20 4f 52  wo or more of OR
2d536 2d 63 6f 6e 6e 65 63 74 65 64 20 73 75 62 74 65  -connected subte
2d537 72 6d 73 2e 0a 2a 2a 20 41 20 73 69 6e 67 6c 65  rms..** A single
2d538 20 73 75 62 74 65 72 6d 20 6d 69 67 68 74 20 62   subterm might b
2d539 65 20 61 20 73 65 74 20 6f 66 20 41 4e 44 2d 63  e a set of AND-c
2d53a 6f 6e 6e 65 63 74 65 64 20 73 75 62 2d 73 75 62  onnected sub-sub
2d53b 74 65 72 6d 73 2e 0a 2a 2a 20 45 78 61 6d 70 6c  terms..** Exampl
2d53c 65 73 20 6f 66 20 74 65 72 6d 73 20 75 6e 64 65  es of terms unde
2d53d 72 20 61 6e 61 6c 79 73 69 73 3a 0a 2a 2a 0a 2a  r analysis:.**.*
2d53e 2a 20 20 20 20 20 28 41 29 20 20 20 20 20 74 31  *     (A)     t1
2d53f 2e 78 3d 74 32 2e 79 20 4f 52 20 74 31 2e 78 3d  .x=t2.y OR t1.x=
2d540 74 32 2e 7a 20 4f 52 20 74 31 2e 79 3d 31 35 20  t2.z OR t1.y=15 
2d541 4f 52 20 74 31 2e 7a 3d 74 33 2e 61 2b 35 0a 2a  OR t1.z=t3.a+5.*
2d542 2a 20 20 20 20 20 28 42 29 20 20 20 20 20 78 3d  *     (B)     x=
2d543 65 78 70 72 31 20 4f 52 20 65 78 70 72 32 3d 78  expr1 OR expr2=x
2d544 20 4f 52 20 78 3d 65 78 70 72 33 0a 2a 2a 20 20   OR x=expr3.**  
2d545 20 20 20 28 43 29 20 20 20 20 20 74 31 2e 78 3d     (C)     t1.x=
2d546 74 32 2e 79 20 4f 52 20 28 74 31 2e 78 3d 74 32  t2.y OR (t1.x=t2
2d547 2e 7a 20 41 4e 44 20 74 31 2e 79 3d 31 35 29 0a  .z AND t1.y=15).
2d548 2a 2a 20 20 20 20 20 28 44 29 20 20 20 20 20 78  **     (D)     x
2d549 3d 65 78 70 72 31 20 4f 52 20 28 79 3e 31 31 20  =expr1 OR (y>11 
2d54a 41 4e 44 20 79 3c 32 32 20 41 4e 44 20 7a 20 4c  AND y<22 AND z L
2d54b 49 4b 45 20 27 2a 68 65 6c 6c 6f 2a 27 29 0a 2a  IKE '*hello*').*
2d54c 2a 20 20 20 20 20 28 45 29 20 20 20 20 20 28 70  *     (E)     (p
2d54d 2e 61 3d 31 20 41 4e 44 20 71 2e 62 3d 32 20 41  .a=1 AND q.b=2 A
2d54e 4e 44 20 72 2e 63 3d 33 29 20 4f 52 20 28 70 2e  ND r.c=3) OR (p.
2d54f 78 3d 34 20 41 4e 44 20 71 2e 79 3d 35 20 41 4e  x=4 AND q.y=5 AN
2d550 44 20 72 2e 7a 3d 36 29 0a 2a 2a 0a 2a 2a 20 43  D r.z=6).**.** C
2d551 41 53 45 20 31 3a 0a 2a 2a 0a 2a 2a 20 49 66 20  ASE 1:.**.** If 
2d552 61 6c 6c 20 73 75 62 74 65 72 6d 73 20 61 72 65  all subterms are
2d553 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 54 2e 43   of the form T.C
2d554 3d 65 78 70 72 20 66 6f 72 20 73 6f 6d 65 20 73  =expr for some s
2d555 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6f 66 20  ingle column of 
2d556 43 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 74 61  C.** a single ta
2d557 62 6c 65 20 54 20 28 61 73 20 73 68 6f 77 6e 20  ble T (as shown 
2d558 69 6e 20 65 78 61 6d 70 6c 65 20 42 20 61 62 6f  in example B abo
2d559 76 65 29 20 74 68 65 6e 20 63 72 65 61 74 65 20  ve) then create 
2d55a 61 20 6e 65 77 20 76 69 72 74 75 61 6c 0a 2a 2a  a new virtual.**
2d55b 20 74 65 72 6d 20 74 68 61 74 20 69 73 20 61 6e   term that is an
2d55c 20 65 71 75 69 76 61 6c 65 6e 74 20 49 4e 20 65   equivalent IN e
2d55d 78 70 72 65 73 73 69 6f 6e 2e 20 20 49 6e 20 6f  xpression.  In o
2d55e 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 74  ther words, if t
2d55f 68 65 20 74 65 72 6d 0a 2a 2a 20 62 65 69 6e 67  he term.** being
2d560 20 61 6e 61 6c 79 7a 65 64 20 69 73 3a 0a 2a 2a   analyzed is:.**
2d561 0a 2a 2a 20 20 20 20 20 20 78 20 3d 20 65 78 70  .**      x = exp
2d562 72 31 20 20 4f 52 20 20 65 78 70 72 32 20 3d 20  r1  OR  expr2 = 
2d563 78 20 20 4f 52 20 20 78 20 3d 20 65 78 70 72 33  x  OR  x = expr3
2d564 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 63 72 65 61  .**.** then crea
2d565 74 65 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c  te a new virtual
2d566 20 74 65 72 6d 20 6c 69 6b 65 20 74 68 69 73 3a   term like this:
2d567 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 78 20 49 4e  .**.**      x IN
2d568 20 28 65 78 70 72 31 2c 65 78 70 72 32 2c 65 78   (expr1,expr2,ex
2d569 70 72 33 29 0a 2a 2a 0a 2a 2a 20 43 41 53 45 20  pr3).**.** CASE 
2d56a 32 3a 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6c 6c 20  2:.**.** If all 
2d56b 73 75 62 74 65 72 6d 73 20 61 72 65 20 69 6e 64  subterms are ind
2d56c 65 78 61 62 6c 65 20 62 79 20 61 20 73 69 6e 67  exable by a sing
2d56d 6c 65 20 74 61 62 6c 65 20 54 2c 20 74 68 65 6e  le table T, then
2d56e 20 73 65 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 57   set.**.**     W
2d56f 68 65 72 65 54 65 72 6d 2e 65 4f 70 65 72 61 74  hereTerm.eOperat
2d570 6f 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  or              
2d571 3d 20 20 57 4f 5f 4f 52 0a 2a 2a 20 20 20 20 20  =  WO_OR.**     
2d572 57 68 65 72 65 54 65 72 6d 2e 75 2e 70 4f 72 49  WhereTerm.u.pOrI
2d573 6e 66 6f 2d 3e 69 6e 64 65 78 61 62 6c 65 20 20  nfo->indexable  
2d574 7c 3d 20 20 74 68 65 20 63 75 72 73 6f 72 20 6e  |=  the cursor n
2d575 75 6d 62 65 72 20 66 6f 72 20 74 61 62 6c 65 20  umber for table 
2d576 54 0a 2a 2a 0a 2a 2a 20 41 20 73 75 62 74 65 72  T.**.** A subter
2d577 6d 20 69 73 20 22 69 6e 64 65 78 61 62 6c 65 22  m is "indexable"
2d578 20 69 66 20 69 74 20 69 73 20 6f 66 20 74 68 65   if it is of the
2d579 20 66 6f 72 6d 0a 2a 2a 20 22 54 2e 43 20 3c 6f   form.** "T.C <o
2d57a 70 3e 20 3c 65 78 70 72 3e 22 20 77 68 65 72 65  p> <expr>" where
2d57b 20 43 20 69 73 20 61 6e 79 20 63 6f 6c 75 6d 6e   C is any column
2d57c 20 6f 66 20 74 61 62 6c 65 20 54 20 61 6e 64 20   of table T and 
2d57d 0a 2a 2a 20 3c 6f 70 3e 20 69 73 20 6f 6e 65 20  .** <op> is one 
2d57e 6f 66 20 22 3d 22 2c 20 22 3c 22 2c 20 22 3c 3d  of "=", "<", "<=
2d57f 22 2c 20 22 3e 22 2c 20 22 3e 3d 22 2c 20 22 49  ", ">", ">=", "I
2d580 53 20 4e 55 4c 4c 22 2c 20 6f 72 20 22 49 4e 22  S NULL", or "IN"
2d581 2e 0a 2a 2a 20 41 20 73 75 62 74 65 72 6d 20 69  ..** A subterm i
2d582 73 20 61 6c 73 6f 20 69 6e 64 65 78 61 62 6c 65  s also indexable
2d583 20 69 66 20 69 74 20 69 73 20 61 6e 20 41 4e 44   if it is an AND
2d584 20 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a   of two or more.
2d585 2a 2a 20 73 75 62 73 75 62 74 65 72 6d 73 20 61  ** subsubterms a
2d586 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 66 20 77  t least one of w
2d587 68 69 63 68 20 69 73 20 69 6e 64 65 78 61 62 6c  hich is indexabl
2d588 65 2e 20 20 49 6e 64 65 78 61 62 6c 65 20 41 4e  e.  Indexable AN
2d589 44 20 0a 2a 2a 20 73 75 62 74 65 72 6d 73 20 68  D .** subterms h
2d58a 61 76 65 20 74 68 65 69 72 20 65 4f 70 65 72 61  ave their eOpera
2d58b 74 6f 72 20 73 65 74 20 74 6f 20 57 4f 5f 41 4e  tor set to WO_AN
2d58c 44 20 61 6e 64 20 74 68 65 79 20 68 61 76 65 0a  D and they have.
2d58d 2a 2a 20 75 2e 70 41 6e 64 49 6e 66 6f 20 73 65  ** u.pAndInfo se
2d58e 74 20 74 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c  t to a dynamical
2d58f 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 57 68 65  ly allocated Whe
2d590 72 65 41 6e 64 54 65 72 6d 20 6f 62 6a 65 63 74  reAndTerm object
2d591 2e 0a 2a 2a 0a 2a 2a 20 46 72 6f 6d 20 61 6e 6f  ..**.** From ano
2d592 74 68 65 72 20 70 6f 69 6e 74 20 6f 66 20 76 69  ther point of vi
2d593 65 77 2c 20 22 69 6e 64 65 78 61 62 6c 65 22 20  ew, "indexable" 
2d594 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 73  means that the s
2d595 75 62 74 65 72 6d 20 63 6f 75 6c 64 0a 2a 2a 20  ubterm could.** 
2d596 70 6f 74 65 6e 74 69 61 6c 6c 79 20 62 65 20 75  potentially be u
2d597 73 65 64 20 77 69 74 68 20 61 6e 20 69 6e 64 65  sed with an inde
2d598 78 20 69 66 20 61 6e 20 61 70 70 72 6f 70 72 69  x if an appropri
2d599 61 74 65 20 69 6e 64 65 78 20 65 78 69 73 74 73  ate index exists
2d59a 2e 0a 2a 2a 20 54 68 69 73 20 61 6e 61 6c 79 73  ..** This analys
2d59b 69 73 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 73  is does not cons
2d59c 69 64 65 72 20 77 68 65 74 68 65 72 20 6f 72 20  ider whether or 
2d59d 6e 6f 74 20 74 68 65 20 69 6e 64 65 78 20 65 78  not the index ex
2d59e 69 73 74 73 3b 20 74 68 61 74 0a 2a 2a 20 69 73  ists; that.** is
2d59f 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 65 20 62   something the b
2d5a0 65 73 74 49 6e 64 65 78 28 29 20 72 6f 75 74 69  estIndex() routi
2d5a1 6e 65 20 77 69 6c 6c 20 64 65 74 65 72 6d 69 6e  ne will determin
2d5a2 65 2e 20 20 54 68 69 73 20 61 6e 61 6c 79 73 69  e.  This analysi
2d5a3 73 0a 2a 2a 20 6f 6e 6c 79 20 6c 6f 6f 6b 73 20  s.** only looks 
2d5a4 61 74 20 77 68 65 74 68 65 72 20 73 75 62 74 65  at whether subte
2d5a5 72 6d 73 20 61 70 70 72 6f 70 72 69 61 74 65 20  rms appropriate 
2d5a6 66 6f 72 20 69 6e 64 65 78 69 6e 67 20 65 78 69  for indexing exi
2d5a7 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 65 78  st..**.** All ex
2d5a8 61 6d 70 6c 65 73 20 41 20 74 68 72 6f 75 67 68  amples A through
2d5a9 20 45 20 61 62 6f 76 65 20 61 6c 6c 20 73 61 74   E above all sat
2d5aa 69 73 66 79 20 63 61 73 65 20 32 2e 20 20 42 75  isfy case 2.  Bu
2d5ab 74 20 69 66 20 61 20 74 65 72 6d 0a 2a 2a 20 61  t if a term.** a
2d5ac 6c 73 6f 20 73 74 61 74 69 73 66 69 65 73 20 63  lso statisfies c
2d5ad 61 73 65 20 31 20 28 73 75 63 68 20 61 73 20 42  ase 1 (such as B
2d5ae 29 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74  ) we know that t
2d5af 68 65 20 6f 70 74 69 6d 69 7a 65 72 20 77 69 6c  he optimizer wil
2d5b0 6c 0a 2a 2a 20 61 6c 77 61 79 73 20 70 72 65 66  l.** always pref
2d5b1 65 72 20 63 61 73 65 20 31 2c 20 73 6f 20 69 6e  er case 1, so in
2d5b2 20 74 68 61 74 20 63 61 73 65 20 77 65 20 70 72   that case we pr
2d5b3 65 74 65 6e 64 20 74 68 61 74 20 63 61 73 65 20  etend that case 
2d5b4 32 20 69 73 20 6e 6f 74 0a 2a 2a 20 73 61 74 69  2 is not.** sati
2d5b5 73 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20  sfied..**.** It 
2d5b6 6d 69 67 68 74 20 62 65 20 74 68 65 20 63 61 73  might be the cas
2d5b7 65 20 74 68 61 74 20 6d 75 6c 74 69 70 6c 65 20  e that multiple 
2d5b8 74 61 62 6c 65 73 20 61 72 65 20 69 6e 64 65 78  tables are index
2d5b9 61 62 6c 65 2e 20 20 46 6f 72 20 65 78 61 6d 70  able.  For examp
2d5ba 6c 65 2c 0a 2a 2a 20 28 45 29 20 61 62 6f 76 65  le,.** (E) above
2d5bb 20 69 73 20 69 6e 64 65 78 61 62 6c 65 20 6f 6e   is indexable on
2d5bc 20 74 61 62 6c 65 73 20 50 2c 20 51 2c 20 61 6e   tables P, Q, an
2d5bd 64 20 52 2e 0a 2a 2a 0a 2a 2a 20 54 65 72 6d 73  d R..**.** Terms
2d5be 20 74 68 61 74 20 73 61 74 69 73 66 79 20 63 61   that satisfy ca
2d5bf 73 65 20 32 20 61 72 65 20 63 61 6e 64 69 64 61  se 2 are candida
2d5c0 74 65 73 20 66 6f 72 20 6c 6f 6f 6b 75 70 20 62  tes for lookup b
2d5c1 79 20 75 73 69 6e 67 0a 2a 2a 20 73 65 70 61 72  y using.** separ
2d5c2 61 74 65 20 69 6e 64 69 63 65 73 20 74 6f 20 66  ate indices to f
2d5c3 69 6e 64 20 72 6f 77 69 64 73 20 66 6f 72 20 65  ind rowids for e
2d5c4 61 63 68 20 73 75 62 74 65 72 6d 20 61 6e 64 20  ach subterm and 
2d5c5 63 6f 6d 70 6f 73 69 6e 67 0a 2a 2a 20 74 68 65  composing.** the
2d5c6 20 75 6e 69 6f 6e 20 6f 66 20 61 6c 6c 20 72 6f   union of all ro
2d5c7 77 69 64 73 20 75 73 69 6e 67 20 61 20 52 6f 77  wids using a Row
2d5c8 53 65 74 20 6f 62 6a 65 63 74 2e 20 20 54 68 69  Set object.  Thi
2d5c9 73 20 69 73 20 73 69 6d 69 6c 61 72 0a 2a 2a 20  s is similar.** 
2d5ca 74 6f 20 22 62 69 74 6d 61 70 20 69 6e 64 69 63  to "bitmap indic
2d5cb 65 73 22 20 69 6e 20 6f 74 68 65 72 20 64 61 74  es" in other dat
2d5cc 61 62 61 73 65 20 65 6e 67 69 6e 65 73 2e 0a 2a  abase engines..*
2d5cd 2a 0a 2a 2a 20 4f 54 48 45 52 57 49 53 45 3a 0a  *.** OTHERWISE:.
2d5ce 2a 2a 0a 2a 2a 20 49 66 20 6e 65 69 74 68 65 72  **.** If neither
2d5cf 20 63 61 73 65 20 31 20 6e 6f 72 20 63 61 73 65   case 1 nor case
2d5d0 20 32 20 61 70 70 6c 79 2c 20 74 68 65 6e 20 6c   2 apply, then l
2d5d1 65 61 76 65 20 74 68 65 20 65 4f 70 65 72 61 74  eave the eOperat
2d5d2 6f 72 20 73 65 74 20 74 6f 0a 2a 2a 20 7a 65 72  or set to.** zer
2d5d3 6f 2e 20 20 54 68 69 73 20 74 65 72 6d 20 69 73  o.  This term is
2d5d4 20 6e 6f 74 20 75 73 65 66 75 6c 20 66 6f 72 20   not useful for 
2d5d5 73 65 61 72 63 68 2e 0a 2a 2f 0a 73 74 61 74 69  search..*/.stati
2d5d6 63 20 76 6f 69 64 20 65 78 70 72 41 6e 61 6c 79  c void exprAnaly
2d5d7 7a 65 4f 72 54 65 72 6d 28 0a 20 20 53 72 63 4c  zeOrTerm(.  SrcL
2d5d8 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20  ist *pSrc,      
2d5d9 20 20 20 20 20 20 2f 2a 20 74 68 65 20 46 52 4f        /* the FRO
2d5da 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68  M clause */.  Wh
2d5db 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20  ereClause *pWC, 
2d5dc 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 63          /* the c
2d5dd 6f 6d 70 6c 65 74 65 20 57 48 45 52 45 20 63 6c  omplete WHERE cl
2d5de 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 64  ause */.  int id
2d5df 78 54 65 72 6d 20 20 20 20 20 20 20 20 20 20 20  xTerm           
2d5e0 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
2d5e1 74 68 65 20 4f 52 2d 74 65 72 6d 20 74 6f 20 62  the OR-term to b
2d5e2 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 29 7b  e analyzed */.){
2d5e3 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2d5e4 20 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 3b 20   = pWC->pParse; 
2d5e5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
2d5e6 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
2d5e7 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2d5e8 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20  pParse->db;     
2d5e9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2d5ea 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2d5eb 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20   */.  WhereTerm 
2d5ec 2a 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61  *pTerm = &pWC->a
2d5ed 5b 69 64 78 54 65 72 6d 5d 3b 20 20 20 20 2f 2a  [idxTerm];    /*
2d5ee 20 54 68 65 20 74 65 72 6d 20 74 6f 20 62 65 20   The term to be 
2d5ef 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 45 78  analyzed */.  Ex
2d5f0 70 72 20 2a 70 45 78 70 72 20 3d 20 70 54 65 72  pr *pExpr = pTer
2d5f1 6d 2d 3e 70 45 78 70 72 3b 20 20 20 20 20 20 20  m->pExpr;       
2d5f2 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70        /* The exp
2d5f3 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 74  ression of the t
2d5f4 65 72 6d 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61  erm */.  WhereMa
2d5f5 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 20  skSet *pMaskSet 
2d5f6 3d 20 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 3b  = pWC->pMaskSet;
2d5f7 20 2f 2a 20 54 61 62 6c 65 20 75 73 65 20 6d 61   /* Table use ma
2d5f8 73 6b 73 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20  sks */.  int i; 
2d5f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d5fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d5fb 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
2d5fc 73 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75  s */.  WhereClau
2d5fd 73 65 20 2a 70 4f 72 57 63 3b 20 20 20 20 20 20  se *pOrWc;      
2d5fe 20 2f 2a 20 42 72 65 61 6b 75 70 20 6f 66 20 70   /* Breakup of p
2d5ff 54 65 72 6d 20 69 6e 74 6f 20 73 75 62 74 65 72  Term into subter
2d600 6d 73 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72  ms */.  WhereTer
2d601 6d 20 2a 70 4f 72 54 65 72 6d 3b 20 20 20 20 20  m *pOrTerm;     
2d602 20 20 2f 2a 20 41 20 53 75 62 2d 74 65 72 6d 20    /* A Sub-term 
2d603 77 69 74 68 69 6e 20 74 68 65 20 70 4f 72 57 63  within the pOrWc
2d604 20 2a 2f 0a 20 20 57 68 65 72 65 4f 72 49 6e 66   */.  WhereOrInf
2d605 6f 20 2a 70 4f 72 49 6e 66 6f 3b 20 20 20 20 20  o *pOrInfo;     
2d606 2f 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 69 6e  /* Additional in
2d607 66 6f 72 6d 61 74 69 6f 6e 20 61 73 73 6f 63 69  formation associ
2d608 61 74 65 64 20 77 69 74 68 20 70 54 65 72 6d 20  ated with pTerm 
2d609 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 63 68 6e  */.  Bitmask chn
2d60a 67 54 6f 49 4e 3b 20 20 20 20 20 20 20 20 20 2f  gToIN;         /
2d60b 2a 20 54 61 62 6c 65 73 20 74 68 61 74 20 6d 69  * Tables that mi
2d60c 67 68 74 20 73 61 74 69 73 66 79 20 63 61 73 65  ght satisfy case
2d60d 20 31 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20   1 */.  Bitmask 
2d60e 69 6e 64 65 78 61 62 6c 65 3b 20 20 20 20 20 20  indexable;      
2d60f 20 20 2f 2a 20 54 61 62 6c 65 73 20 74 68 61 74    /* Tables that
2d610 20 61 72 65 20 69 6e 64 65 78 61 62 6c 65 2c 20   are indexable, 
2d611 73 61 74 69 73 66 79 69 6e 67 20 63 61 73 65 20  satisfying case 
2d612 32 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  2 */..  /*.  ** 
2d613 42 72 65 61 6b 20 74 68 65 20 4f 52 20 63 6c 61  Break the OR cla
2d614 75 73 65 20 69 6e 74 6f 20 69 74 73 20 73 65 70  use into its sep
2d615 61 72 61 74 65 20 73 75 62 74 65 72 6d 73 2e 20  arate subterms. 
2d616 20 54 68 65 20 73 75 62 74 65 72 6d 73 20 61 72   The subterms ar
2d617 65 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20 69 6e  e.  ** stored in
2d618 20 61 20 57 68 65 72 65 43 6c 61 75 73 65 20 73   a WhereClause s
2d619 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
2d61a 69 6e 67 20 77 69 74 68 69 6e 20 74 68 65 20 57  ing within the W
2d61b 68 65 72 65 4f 72 49 6e 66 6f 0a 20 20 2a 2a 20  hereOrInfo.  ** 
2d61c 6f 62 6a 65 63 74 20 74 68 61 74 20 69 73 20 61  object that is a
2d61d 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 6f  ttached to the o
2d61e 72 69 67 69 6e 61 6c 20 4f 52 20 63 6c 61 75 73  riginal OR claus
2d61f 65 20 74 65 72 6d 2e 0a 20 20 2a 2f 0a 20 20 61  e term..  */.  a
2d620 73 73 65 72 74 28 20 28 70 54 65 72 6d 2d 3e 77  ssert( (pTerm->w
2d621 74 46 6c 61 67 73 20 26 20 28 54 45 52 4d 5f 44  tFlags & (TERM_D
2d622 59 4e 41 4d 49 43 7c 54 45 52 4d 5f 4f 52 49 4e  YNAMIC|TERM_ORIN
2d623 46 4f 7c 54 45 52 4d 5f 41 4e 44 49 4e 46 4f 29  FO|TERM_ANDINFO)
2d624 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  )==0 );.  assert
2d625 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
2d626 4f 52 20 29 3b 0a 20 20 70 54 65 72 6d 2d 3e 75  OR );.  pTerm->u
2d627 2e 70 4f 72 49 6e 66 6f 20 3d 20 70 4f 72 49 6e  .pOrInfo = pOrIn
2d628 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  fo = sqlite3DbMa
2d629 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
2d62a 65 6f 66 28 2a 70 4f 72 49 6e 66 6f 29 29 3b 0a  eof(*pOrInfo));.
2d62b 20 20 69 66 28 20 70 4f 72 49 6e 66 6f 3d 3d 30    if( pOrInfo==0
2d62c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 54 65   ) return;.  pTe
2d62d 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54  rm->wtFlags |= T
2d62e 45 52 4d 5f 4f 52 49 4e 46 4f 3b 0a 20 20 70 4f  ERM_ORINFO;.  pO
2d62f 72 57 63 20 3d 20 26 70 4f 72 49 6e 66 6f 2d 3e  rWc = &pOrInfo->
2d630 77 63 3b 0a 20 20 77 68 65 72 65 43 6c 61 75 73  wc;.  whereClaus
2d631 65 49 6e 69 74 28 70 4f 72 57 63 2c 20 70 57 43  eInit(pOrWc, pWC
2d632 2d 3e 70 50 61 72 73 65 2c 20 70 4d 61 73 6b 53  ->pParse, pMaskS
2d633 65 74 29 3b 0a 20 20 77 68 65 72 65 53 70 6c 69  et);.  whereSpli
2d634 74 28 70 4f 72 57 63 2c 20 70 45 78 70 72 2c 20  t(pOrWc, pExpr, 
2d635 54 4b 5f 4f 52 29 3b 0a 20 20 65 78 70 72 41 6e  TK_OR);.  exprAn
2d636 61 6c 79 7a 65 41 6c 6c 28 70 53 72 63 2c 20 70  alyzeAll(pSrc, p
2d637 4f 72 57 63 29 3b 0a 20 20 69 66 28 20 64 62 2d  OrWc);.  if( db-
2d638 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
2d639 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74  return;.  assert
2d63a 28 20 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 3e 3d  ( pOrWc->nTerm>=
2d63b 32 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  2 );..  /*.  ** 
2d63c 43 6f 6d 70 75 74 65 20 74 68 65 20 73 65 74 20  Compute the set 
2d63d 6f 66 20 74 61 62 6c 65 73 20 74 68 61 74 20 6d  of tables that m
2d63e 69 67 68 74 20 73 61 74 69 73 66 79 20 63 61 73  ight satisfy cas
2d63f 65 73 20 31 20 6f 72 20 32 2e 0a 20 20 2a 2f 0a  es 1 or 2..  */.
2d640 20 20 69 6e 64 65 78 61 62 6c 65 20 3d 20 7e 28    indexable = ~(
2d641 42 69 74 6d 61 73 6b 29 30 3b 0a 20 20 63 68 6e  Bitmask)0;.  chn
2d642 67 54 6f 49 4e 20 3d 20 7e 28 70 57 43 2d 3e 76  gToIN = ~(pWC->v
2d643 6d 61 73 6b 29 3b 0a 20 20 66 6f 72 28 69 3d 70  mask);.  for(i=p
2d644 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d 31 2c 20 70  OrWc->nTerm-1, p
2d645 4f 72 54 65 72 6d 3d 70 4f 72 57 63 2d 3e 61 3b  OrTerm=pOrWc->a;
2d646 20 69 3e 3d 30 20 26 26 20 69 6e 64 65 78 61 62   i>=0 && indexab
2d647 6c 65 3b 20 69 2d 2d 2c 20 70 4f 72 54 65 72 6d  le; i--, pOrTerm
2d648 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 28 70 4f  ++){.    if( (pO
2d649 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  rTerm->eOperator
2d64a 20 26 20 57 4f 5f 53 49 4e 47 4c 45 29 3d 3d 30   & WO_SINGLE)==0
2d64b 20 29 7b 0a 20 20 20 20 20 20 57 68 65 72 65 41   ){.      WhereA
2d64c 6e 64 49 6e 66 6f 20 2a 70 41 6e 64 49 6e 66 6f  ndInfo *pAndInfo
2d64d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2d64e 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  pOrTerm->eOperat
2d64f 6f 72 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 61  or==0 );.      a
2d650 73 73 65 72 74 28 20 28 70 4f 72 54 65 72 6d 2d  ssert( (pOrTerm-
2d651 3e 77 74 46 6c 61 67 73 20 26 20 28 54 45 52 4d  >wtFlags & (TERM
2d652 5f 41 4e 44 49 4e 46 4f 7c 54 45 52 4d 5f 4f 52  _ANDINFO|TERM_OR
2d653 49 4e 46 4f 29 29 3d 3d 30 20 29 3b 0a 20 20 20  INFO))==0 );.   
2d654 20 20 20 63 68 6e 67 54 6f 49 4e 20 3d 20 30 3b     chngToIN = 0;
2d655 0a 20 20 20 20 20 20 70 41 6e 64 49 6e 66 6f 20  .      pAndInfo 
2d656 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2d657 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28  cRaw(db, sizeof(
2d658 2a 70 41 6e 64 49 6e 66 6f 29 29 3b 0a 20 20 20  *pAndInfo));.   
2d659 20 20 20 69 66 28 20 70 41 6e 64 49 6e 66 6f 20     if( pAndInfo 
2d65a 29 7b 0a 20 20 20 20 20 20 20 20 57 68 65 72 65  ){.        Where
2d65b 43 6c 61 75 73 65 20 2a 70 41 6e 64 57 43 3b 0a  Clause *pAndWC;.
2d65c 20 20 20 20 20 20 20 20 57 68 65 72 65 54 65 72          WhereTer
2d65d 6d 20 2a 70 41 6e 64 54 65 72 6d 3b 0a 20 20 20  m *pAndTerm;.   
2d65e 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20       int j;.    
2d65f 20 20 20 20 42 69 74 6d 61 73 6b 20 62 20 3d 20      Bitmask b = 
2d660 30 3b 0a 20 20 20 20 20 20 20 20 70 4f 72 54 65  0;.        pOrTe
2d661 72 6d 2d 3e 75 2e 70 41 6e 64 49 6e 66 6f 20 3d  rm->u.pAndInfo =
2d662 20 70 41 6e 64 49 6e 66 6f 3b 0a 20 20 20 20 20   pAndInfo;.     
2d663 20 20 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c     pOrTerm->wtFl
2d664 61 67 73 20 7c 3d 20 54 45 52 4d 5f 41 4e 44 49  ags |= TERM_ANDI
2d665 4e 46 4f 3b 0a 20 20 20 20 20 20 20 20 70 4f 72  NFO;.        pOr
2d666 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
2d667 3d 20 57 4f 5f 41 4e 44 3b 0a 20 20 20 20 20 20  = WO_AND;.      
2d668 20 20 70 41 6e 64 57 43 20 3d 20 26 70 41 6e 64    pAndWC = &pAnd
2d669 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20 20 20 20 20  Info->wc;.      
2d66a 20 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 69    whereClauseIni
2d66b 74 28 70 41 6e 64 57 43 2c 20 70 57 43 2d 3e 70  t(pAndWC, pWC->p
2d66c 50 61 72 73 65 2c 20 70 4d 61 73 6b 53 65 74 29  Parse, pMaskSet)
2d66d 3b 0a 20 20 20 20 20 20 20 20 77 68 65 72 65 53  ;.        whereS
2d66e 70 6c 69 74 28 70 41 6e 64 57 43 2c 20 70 4f 72  plit(pAndWC, pOr
2d66f 54 65 72 6d 2d 3e 70 45 78 70 72 2c 20 54 4b 5f  Term->pExpr, TK_
2d670 41 4e 44 29 3b 0a 20 20 20 20 20 20 20 20 65 78  AND);.        ex
2d671 70 72 41 6e 61 6c 79 7a 65 41 6c 6c 28 70 53 72  prAnalyzeAll(pSr
2d672 63 2c 20 70 41 6e 64 57 43 29 3b 0a 20 20 20 20  c, pAndWC);.    
2d673 20 20 20 20 74 65 73 74 63 61 73 65 28 20 64 62      testcase( db
2d674 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2d675 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 64  ;.        if( !d
2d676 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2d677 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  ){.          for
2d678 28 6a 3d 30 2c 20 70 41 6e 64 54 65 72 6d 3d 70  (j=0, pAndTerm=p
2d679 41 6e 64 57 43 2d 3e 61 3b 20 6a 3c 70 41 6e 64  AndWC->a; j<pAnd
2d67a 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a 2b 2b 2c 20  WC->nTerm; j++, 
2d67b 70 41 6e 64 54 65 72 6d 2b 2b 29 7b 0a 20 20 20  pAndTerm++){.   
2d67c 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
2d67d 20 70 41 6e 64 54 65 72 6d 2d 3e 70 45 78 70 72   pAndTerm->pExpr
2d67e 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   );.            
2d67f 69 66 28 20 61 6c 6c 6f 77 65 64 4f 70 28 70 41  if( allowedOp(pA
2d680 6e 64 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 6f  ndTerm->pExpr->o
2d681 70 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  p) ){.          
2d682 20 20 20 20 62 20 7c 3d 20 67 65 74 4d 61 73 6b      b |= getMask
2d683 28 70 4d 61 73 6b 53 65 74 2c 20 70 41 6e 64 54  (pMaskSet, pAndT
2d684 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 29  erm->leftCursor)
2d685 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
2d686 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2d687 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 6e      }.        in
2d688 64 65 78 61 62 6c 65 20 26 3d 20 62 3b 0a 20 20  dexable &= b;.  
2d689 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20      }.    }else 
2d68a 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46  if( pOrTerm->wtF
2d68b 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 50 49  lags & TERM_COPI
2d68c 45 44 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53  ED ){.      /* S
2d68d 6b 69 70 20 74 68 69 73 20 74 65 72 6d 20 66 6f  kip this term fo
2d68e 72 20 6e 6f 77 2e 20 20 57 65 20 72 65 76 69 73  r now.  We revis
2d68f 69 74 20 69 74 20 77 68 65 6e 20 77 65 20 70 72  it it when we pr
2d690 6f 63 65 73 73 20 74 68 65 0a 20 20 20 20 20 20  ocess the.      
2d691 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ** corresponding
2d692 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 74 65   TERM_VIRTUAL te
2d693 72 6d 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 7b  rm */.    }else{
2d694 0a 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20 62  .      Bitmask b
2d695 3b 0a 20 20 20 20 20 20 62 20 3d 20 67 65 74 4d  ;.      b = getM
2d696 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 4f  ask(pMaskSet, pO
2d697 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  rTerm->leftCurso
2d698 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f  r);.      if( pO
2d699 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26  rTerm->wtFlags &
2d69a 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 29 7b   TERM_VIRTUAL ){
2d69b 0a 20 20 20 20 20 20 20 20 57 68 65 72 65 54 65  .        WhereTe
2d69c 72 6d 20 2a 70 4f 74 68 65 72 20 3d 20 26 70 4f  rm *pOther = &pO
2d69d 72 57 63 2d 3e 61 5b 70 4f 72 54 65 72 6d 2d 3e  rWc->a[pOrTerm->
2d69e 69 50 61 72 65 6e 74 5d 3b 0a 20 20 20 20 20 20  iParent];.      
2d69f 20 20 62 20 7c 3d 20 67 65 74 4d 61 73 6b 28 70    b |= getMask(p
2d6a0 4d 61 73 6b 53 65 74 2c 20 70 4f 74 68 65 72 2d  MaskSet, pOther-
2d6a1 3e 6c 65 66 74 43 75 72 73 6f 72 29 3b 0a 20 20  >leftCursor);.  
2d6a2 20 20 20 20 7d 0a 20 20 20 20 20 20 69 6e 64 65      }.      inde
2d6a3 78 61 62 6c 65 20 26 3d 20 62 3b 0a 20 20 20 20  xable &= b;.    
2d6a4 20 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 65    if( pOrTerm->e
2d6a5 4f 70 65 72 61 74 6f 72 21 3d 57 4f 5f 45 51 20  Operator!=WO_EQ 
2d6a6 29 7b 0a 20 20 20 20 20 20 20 20 63 68 6e 67 54  ){.        chngT
2d6a7 6f 49 4e 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  oIN = 0;.      }
2d6a8 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 68  else{.        ch
2d6a9 6e 67 54 6f 49 4e 20 26 3d 20 62 3b 0a 20 20 20  ngToIN &= b;.   
2d6aa 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
2d6ab 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 63 6f 72 64    /*.  ** Record
2d6ac 20 74 68 65 20 73 65 74 20 6f 66 20 74 61 62 6c   the set of tabl
2d6ad 65 73 20 74 68 61 74 20 73 61 74 69 73 66 79 20  es that satisfy 
2d6ae 63 61 73 65 20 32 2e 20 20 54 68 65 20 73 65 74  case 2.  The set
2d6af 20 6d 69 67 68 74 20 62 65 0a 20 20 2a 2a 20 65   might be.  ** e
2d6b0 6d 70 74 79 2e 0a 20 20 2a 2f 0a 20 20 70 4f 72  mpty..  */.  pOr
2d6b1 49 6e 66 6f 2d 3e 69 6e 64 65 78 61 62 6c 65 20  Info->indexable 
2d6b2 3d 20 69 6e 64 65 78 61 62 6c 65 3b 0a 20 20 70  = indexable;.  p
2d6b3 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
2d6b4 3d 20 69 6e 64 65 78 61 62 6c 65 3d 3d 30 20 3f  = indexable==0 ?
2d6b5 20 30 20 3a 20 57 4f 5f 4f 52 3b 0a 0a 20 20 2f   0 : WO_OR;..  /
2d6b6 2a 0a 20 20 2a 2a 20 63 68 6e 67 54 6f 49 4e 20  *.  ** chngToIN 
2d6b7 68 6f 6c 64 73 20 61 20 73 65 74 20 6f 66 20 74  holds a set of t
2d6b8 61 62 6c 65 73 20 74 68 61 74 20 2a 6d 69 67 68  ables that *migh
2d6b9 74 2a 20 73 61 74 69 73 66 79 20 63 61 73 65 20  t* satisfy case 
2d6ba 31 2e 20 20 42 75 74 0a 20 20 2a 2a 20 77 65 20  1.  But.  ** we 
2d6bb 68 61 76 65 20 74 6f 20 64 6f 20 73 6f 6d 65 20  have to do some 
2d6bc 61 64 64 69 74 69 6f 6e 61 6c 20 63 68 65 63 6b  additional check
2d6bd 69 6e 67 20 74 6f 20 73 65 65 20 69 66 20 63 61  ing to see if ca
2d6be 73 65 20 31 20 72 65 61 6c 6c 79 0a 20 20 2a 2a  se 1 really.  **
2d6bf 20 69 73 20 73 61 74 69 73 66 69 65 64 2e 0a 20   is satisfied.. 
2d6c0 20 2a 2a 0a 20 20 2a 2a 20 63 68 6e 67 54 6f 49   **.  ** chngToI
2d6c1 4e 20 77 69 6c 6c 20 68 6f 6c 64 20 65 69 74 68  N will hold eith
2d6c2 65 72 20 30 2c 20 31 2c 20 6f 72 20 32 20 62 69  er 0, 1, or 2 bi
2d6c3 74 73 2e 20 20 54 68 65 20 30 2d 62 69 74 20 63  ts.  The 0-bit c
2d6c4 61 73 65 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 74  ase means.  ** t
2d6c5 68 61 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20  hat there is no 
2d6c6 70 6f 73 73 69 62 69 6c 69 74 79 20 6f 66 20 74  possibility of t
2d6c7 72 61 6e 73 66 6f 72 6d 69 6e 67 20 74 68 65 20  ransforming the 
2d6c8 4f 52 20 63 6c 61 75 73 65 20 69 6e 74 6f 20 61  OR clause into a
2d6c9 6e 0a 20 20 2a 2a 20 49 4e 20 6f 70 65 72 61 74  n.  ** IN operat
2d6ca 6f 72 20 62 65 63 61 75 73 65 20 6f 6e 65 20 6f  or because one o
2d6cb 72 20 6d 6f 72 65 20 74 65 72 6d 73 20 69 6e 20  r more terms in 
2d6cc 74 68 65 20 4f 52 20 63 6c 61 75 73 65 20 63 6f  the OR clause co
2d6cd 6e 74 61 69 6e 0a 20 20 2a 2a 20 73 6f 6d 65 74  ntain.  ** somet
2d6ce 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20  hing other than 
2d6cf 3d 3d 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 69  == on a column i
2d6d0 6e 20 74 68 65 20 73 69 6e 67 6c 65 20 74 61 62  n the single tab
2d6d1 6c 65 2e 20 20 54 68 65 20 31 2d 62 69 74 0a 20  le.  The 1-bit. 
2d6d2 20 2a 2a 20 63 61 73 65 20 6d 65 61 6e 73 20 74   ** case means t
2d6d3 68 61 74 20 65 76 65 72 79 20 74 65 72 6d 20 6f  hat every term o
2d6d4 66 20 74 68 65 20 4f 52 20 63 6c 61 75 73 65 20  f the OR clause 
2d6d5 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 20  is of the form. 
2d6d6 20 2a 2a 20 22 74 61 62 6c 65 2e 63 6f 6c 75 6d   ** "table.colum
2d6d7 6e 3d 65 78 70 72 22 20 66 6f 72 20 73 6f 6d 65  n=expr" for some
2d6d8 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 2e 20 20   single table.  
2d6d9 54 68 65 20 6f 6e 65 20 62 69 74 20 74 68 61 74  The one bit that
2d6da 20 69 73 20 73 65 74 0a 20 20 2a 2a 20 77 69 6c   is set.  ** wil
2d6db 6c 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20  l correspond to 
2d6dc 74 68 65 20 63 6f 6d 6d 6f 6e 20 74 61 62 6c 65  the common table
2d6dd 2e 20 20 57 65 20 73 74 69 6c 6c 20 6e 65 65 64  .  We still need
2d6de 20 74 6f 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b   to check to mak
2d6df 65 0a 20 20 2a 2a 20 73 75 72 65 20 74 68 65 20  e.  ** sure the 
2d6e0 73 61 6d 65 20 63 6f 6c 75 6d 6e 20 69 73 20 75  same column is u
2d6e1 73 65 64 20 6f 6e 20 61 6c 6c 20 74 65 72 6d 73  sed on all terms
2d6e2 2e 20 20 54 68 65 20 32 2d 62 69 74 20 63 61 73  .  The 2-bit cas
2d6e3 65 20 69 73 20 77 68 65 6e 0a 20 20 2a 2a 20 74  e is when.  ** t
2d6e4 68 65 20 61 6c 6c 20 74 65 72 6d 73 20 61 72 65  he all terms are
2d6e5 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 74 61   of the form "ta
2d6e6 62 6c 65 31 2e 63 6f 6c 75 6d 6e 3d 74 61 62 6c  ble1.column=tabl
2d6e7 65 32 2e 63 6f 6c 75 6d 6e 22 2e 20 20 49 74 0a  e2.column".  It.
2d6e8 20 20 2a 2a 20 6d 69 67 68 74 20 62 65 20 70 6f    ** might be po
2d6e9 73 73 69 62 6c 65 20 74 6f 20 66 6f 72 6d 20 61  ssible to form a
2d6ea 6e 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 77 69  n IN operator wi
2d6eb 74 68 20 65 69 74 68 65 72 20 74 61 62 6c 65 31  th either table1
2d6ec 2e 63 6f 6c 75 6d 6e 0a 20 20 2a 2a 20 6f 72 20  .column.  ** or 
2d6ed 74 61 62 6c 65 32 2e 63 6f 6c 75 6d 6e 20 61 73  table2.column as
2d6ee 20 74 68 65 20 4c 48 53 20 69 66 20 65 69 74 68   the LHS if eith
2d6ef 65 72 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20  er is common to 
2d6f0 65 76 65 72 79 20 74 65 72 6d 20 6f 66 0a 20 20  every term of.  
2d6f1 2a 2a 20 74 68 65 20 4f 52 20 63 6c 61 75 73 65  ** the OR clause
2d6f2 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65  ..  **.  ** Note
2d6f3 20 74 68 61 74 20 74 65 72 6d 73 20 6f 66 20 74   that terms of t
2d6f4 68 65 20 66 6f 72 6d 20 22 74 61 62 6c 65 2e 63  he form "table.c
2d6f5 6f 6c 75 6d 6e 31 3d 74 61 62 6c 65 2e 63 6f 6c  olumn1=table.col
2d6f6 75 6d 6e 32 22 20 28 74 68 65 0a 20 20 2a 2a 20  umn2" (the.  ** 
2d6f7 73 61 6d 65 20 74 61 62 6c 65 20 6f 6e 20 62 6f  same table on bo
2d6f8 74 68 20 73 69 7a 65 73 20 6f 66 20 74 68 65 20  th sizes of the 
2d6f9 3d 3d 29 20 63 61 6e 6e 6f 74 20 62 65 20 6f 70  ==) cannot be op
2d6fa 74 69 6d 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20  timized..  */.  
2d6fb 69 66 28 20 63 68 6e 67 54 6f 49 4e 20 29 7b 0a  if( chngToIN ){.
2d6fc 20 20 20 20 69 6e 74 20 6f 6b 54 6f 43 68 6e 67      int okToChng
2d6fd 54 6f 49 4e 20 3d 20 30 3b 20 20 20 20 20 2f 2a  ToIN = 0;     /*
2d6fe 20 54 72 75 65 20 69 66 20 74 68 65 20 63 6f 6e   True if the con
2d6ff 76 65 72 73 69 6f 6e 20 74 6f 20 49 4e 20 69 73  version to IN is
2d700 20 76 61 6c 69 64 20 2a 2f 0a 20 20 20 20 69 6e   valid */.    in
2d701 74 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 20  t iColumn = -1; 
2d702 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d          /* Colum
2d703 6e 20 69 6e 64 65 78 20 6f 6e 20 6c 68 73 20 6f  n index on lhs o
2d704 66 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f  f IN operator */
2d705 0a 20 20 20 20 69 6e 74 20 69 43 75 72 73 6f 72  .    int iCursor
2d706 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 2f   = -1;         /
2d707 2a 20 54 61 62 6c 65 20 63 75 72 73 6f 72 20 63  * Table cursor c
2d708 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 74 65 72  ommon to all ter
2d709 6d 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6a 20  ms */.    int j 
2d70a 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
2d70b 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2d70c 74 65 72 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 53  ter */..    /* S
2d70d 65 61 72 63 68 20 66 6f 72 20 61 20 74 61 62 6c  earch for a tabl
2d70e 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 74 68 61  e and column tha
2d70f 74 20 61 70 70 65 61 72 73 20 6f 6e 20 6f 6e 65  t appears on one
2d710 20 73 69 64 65 20 6f 72 20 74 68 65 0a 20 20 20   side or the.   
2d711 20 2a 2a 20 6f 74 68 65 72 20 6f 66 20 74 68 65   ** other of the
2d712 20 3d 3d 20 6f 70 65 72 61 74 6f 72 20 69 6e 20   == operator in 
2d713 65 76 65 72 79 20 73 75 62 74 65 72 6d 2e 20 20  every subterm.  
2d714 54 68 61 74 20 74 61 62 6c 65 20 61 6e 64 20 63  That table and c
2d715 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2a 20 77 69 6c  olumn.    ** wil
2d716 6c 20 62 65 20 72 65 63 6f 72 64 65 64 20 69 6e  l be recorded in
2d717 20 69 43 75 72 73 6f 72 20 61 6e 64 20 69 43 6f   iCursor and iCo
2d718 6c 75 6d 6e 2e 20 20 54 68 65 72 65 20 6d 69 67  lumn.  There mig
2d719 68 74 20 6e 6f 74 20 62 65 20 61 6e 79 0a 20 20  ht not be any.  
2d71a 20 20 2a 2a 20 73 75 63 68 20 74 61 62 6c 65 20    ** such table 
2d71b 61 6e 64 20 63 6f 6c 75 6d 6e 2e 20 20 53 65 74  and column.  Set
2d71c 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 69 66   okToChngToIN if
2d71d 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20   an appropriate 
2d71e 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 61 6e 64  table.    ** and
2d71f 20 63 6f 6c 75 6d 6e 20 69 73 20 66 6f 75 6e 64   column is found
2d720 20 62 75 74 20 6c 65 61 76 65 20 6f 6b 54 6f 43   but leave okToC
2d721 68 6e 67 54 6f 49 4e 20 66 61 6c 73 65 20 69 66  hngToIN false if
2d722 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 20 20 20 20   not found..    
2d723 2a 2f 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  */.    for(j=0; 
2d724 6a 3c 32 20 26 26 20 21 6f 6b 54 6f 43 68 6e 67  j<2 && !okToChng
2d725 54 6f 49 4e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  ToIN; j++){.    
2d726 20 20 70 4f 72 54 65 72 6d 20 3d 20 70 4f 72 57    pOrTerm = pOrW
2d727 63 2d 3e 61 3b 0a 20 20 20 20 20 20 66 6f 72 28  c->a;.      for(
2d728 69 3d 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d 31  i=pOrWc->nTerm-1
2d729 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f 72  ; i>=0; i--, pOr
2d72a 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Term++){.       
2d72b 20 61 73 73 65 72 74 28 20 70 4f 72 54 65 72 6d   assert( pOrTerm
2d72c 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f  ->eOperator==WO_
2d72d 45 51 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4f  EQ );.        pO
2d72e 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26  rTerm->wtFlags &
2d72f 3d 20 7e 54 45 52 4d 5f 4f 52 5f 4f 4b 3b 0a 20  = ~TERM_OR_OK;. 
2d730 20 20 20 20 20 20 20 69 66 28 20 70 4f 72 54 65         if( pOrTe
2d731 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d  rm->leftCursor==
2d732 69 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20  iCursor ){.     
2d733 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20       /* This is 
2d734 74 68 65 20 32 2d 62 69 74 20 63 61 73 65 20 61  the 2-bit case a
2d735 6e 64 20 77 65 20 61 72 65 20 6f 6e 20 74 68 65  nd we are on the
2d736 20 73 65 63 6f 6e 64 20 69 74 65 72 61 74 69 6f   second iteratio
2d737 6e 20 61 6e 64 0a 20 20 20 20 20 20 20 20 20 20  n and.          
2d738 2a 2a 20 63 75 72 72 65 6e 74 20 74 65 72 6d 20  ** current term 
2d739 69 73 20 66 72 6f 6d 20 74 68 65 20 66 69 72 73  is from the firs
2d73a 74 20 69 74 65 72 61 74 69 6f 6e 2e 20 20 53 6f  t iteration.  So
2d73b 20 73 6b 69 70 20 74 68 69 73 20 74 65 72 6d 2e   skip this term.
2d73c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 61 73   */.          as
2d73d 73 65 72 74 28 20 6a 3d 3d 31 20 29 3b 0a 20 20  sert( j==1 );.  
2d73e 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65          continue
2d73f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2d740 20 20 20 20 69 66 28 20 28 63 68 6e 67 54 6f 49      if( (chngToI
2d741 4e 20 26 20 67 65 74 4d 61 73 6b 28 70 4d 61 73  N & getMask(pMas
2d742 6b 53 65 74 2c 20 70 4f 72 54 65 72 6d 2d 3e 6c  kSet, pOrTerm->l
2d743 65 66 74 43 75 72 73 6f 72 29 29 3d 3d 30 20 29  eftCursor))==0 )
2d744 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  {.          /* T
2d745 68 69 73 20 74 65 72 6d 20 6d 75 73 74 20 62 65  his term must be
2d746 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 74 31 2e   of the form t1.
2d747 61 3d 3d 74 32 2e 62 20 77 68 65 72 65 20 74 32  a==t2.b where t2
2d748 20 69 73 20 69 6e 20 74 68 65 0a 20 20 20 20 20   is in the.     
2d749 20 20 20 20 20 2a 2a 20 63 68 6e 67 54 6f 49 4e       ** chngToIN
2d74a 20 73 65 74 20 62 75 74 20 74 31 20 69 73 20 6e   set but t1 is n
2d74b 6f 74 2e 20 20 54 68 69 73 20 74 65 72 6d 20 77  ot.  This term w
2d74c 69 6c 6c 20 62 65 20 65 69 74 68 65 72 20 70 72  ill be either pr
2d74d 65 63 65 65 64 65 64 0a 20 20 20 20 20 20 20 20  eceeded.        
2d74e 20 20 2a 2a 20 6f 72 20 66 6f 6c 6c 77 65 64 20    ** or follwed 
2d74f 62 79 20 61 6e 20 69 6e 76 65 72 74 65 64 20 63  by an inverted c
2d750 6f 70 79 20 28 74 32 2e 62 3d 3d 74 31 2e 61 29  opy (t2.b==t1.a)
2d751 2e 20 20 53 6b 69 70 20 74 68 69 73 20 74 65 72  .  Skip this ter
2d752 6d 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  m .          ** 
2d753 61 6e 64 20 75 73 65 20 69 74 73 20 69 6e 76 65  and use its inve
2d754 72 73 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20  rsion. */.      
2d755 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 4f      testcase( pO
2d756 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26  rTerm->wtFlags &
2d757 20 54 45 52 4d 5f 43 4f 50 49 45 44 20 29 3b 0a   TERM_COPIED );.
2d758 20 20 20 20 20 20 20 20 20 20 74 65 73 74 63 61            testca
2d759 73 65 28 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46  se( pOrTerm->wtF
2d75a 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 49 52 54  lags & TERM_VIRT
2d75b 55 41 4c 20 29 3b 0a 20 20 20 20 20 20 20 20 20  UAL );.         
2d75c 20 61 73 73 65 72 74 28 20 70 4f 72 54 65 72 6d   assert( pOrTerm
2d75d 2d 3e 77 74 46 6c 61 67 73 20 26 20 28 54 45 52  ->wtFlags & (TER
2d75e 4d 5f 43 4f 50 49 45 44 7c 54 45 52 4d 5f 56 49  M_COPIED|TERM_VI
2d75f 52 54 55 41 4c 29 20 29 3b 0a 20 20 20 20 20 20  RTUAL) );.      
2d760 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
2d761 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2d762 69 43 6f 6c 75 6d 6e 20 3d 20 70 4f 72 54 65 72  iColumn = pOrTer
2d763 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3b  m->u.leftColumn;
2d764 0a 20 20 20 20 20 20 20 20 69 43 75 72 73 6f 72  .        iCursor
2d765 20 3d 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74   = pOrTerm->left
2d766 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 20 20  Cursor;.        
2d767 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
2d768 20 20 20 20 20 69 66 28 20 69 3c 30 20 29 7b 0a       if( i<0 ){.
2d769 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20 63 61          /* No ca
2d76a 6e 64 69 64 61 74 65 20 74 61 62 6c 65 2b 63 6f  ndidate table+co
2d76b 6c 75 6d 6e 20 77 61 73 20 66 6f 75 6e 64 2e 20  lumn was found. 
2d76c 20 54 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20 6f   This can only o
2d76d 63 63 75 72 0a 20 20 20 20 20 20 20 20 2a 2a 20  ccur.        ** 
2d76e 6f 6e 20 74 68 65 20 73 65 63 6f 6e 64 20 69 74  on the second it
2d76f 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  eration */.     
2d770 20 20 20 61 73 73 65 72 74 28 20 6a 3d 3d 31 20     assert( j==1 
2d771 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
2d772 74 28 20 28 63 68 6e 67 54 6f 49 4e 26 28 63 68  t( (chngToIN&(ch
2d773 6e 67 54 6f 49 4e 2d 31 29 29 3d 3d 30 20 29 3b  ngToIN-1))==0 );
2d774 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2d775 20 63 68 6e 67 54 6f 49 4e 3d 3d 67 65 74 4d 61   chngToIN==getMa
2d776 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 69 43 75  sk(pMaskSet, iCu
2d777 72 73 6f 72 29 20 29 3b 0a 20 20 20 20 20 20 20  rsor) );.       
2d778 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
2d779 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2d77a 6a 3d 3d 31 20 29 3b 0a 0a 20 20 20 20 20 20 2f  j==1 );..      /
2d77b 2a 20 57 65 20 68 61 76 65 20 66 6f 75 6e 64 20  * We have found 
2d77c 61 20 63 61 6e 64 69 64 61 74 65 20 74 61 62 6c  a candidate tabl
2d77d 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 20 20 43  e and column.  C
2d77e 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
2d77f 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 74 61 62  hat.      ** tab
2d780 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 69 73  le and column is
2d781 20 63 6f 6d 6d 6f 6e 20 74 6f 20 65 76 65 72 79   common to every
2d782 20 74 65 72 6d 20 69 6e 20 74 68 65 20 4f 52 20   term in the OR 
2d783 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20  clause */.      
2d784 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 3d 20 31  okToChngToIN = 1
2d785 3b 0a 20 20 20 20 20 20 66 6f 72 28 3b 20 69 3e  ;.      for(; i>
2d786 3d 30 20 26 26 20 6f 6b 54 6f 43 68 6e 67 54 6f  =0 && okToChngTo
2d787 49 4e 3b 20 69 2d 2d 2c 20 70 4f 72 54 65 72 6d  IN; i--, pOrTerm
2d788 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  ++){.        ass
2d789 65 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f  ert( pOrTerm->eO
2d78a 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 45 51 20 29  perator==WO_EQ )
2d78b 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4f  ;.        if( pO
2d78c 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  rTerm->leftCurso
2d78d 72 21 3d 69 43 75 72 73 6f 72 20 29 7b 0a 20 20  r!=iCursor ){.  
2d78e 20 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d 2d          pOrTerm-
2d78f 3e 77 74 46 6c 61 67 73 20 26 3d 20 7e 54 45 52  >wtFlags &= ~TER
2d790 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 20  M_OR_OK;.       
2d791 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 72 54 65   }else if( pOrTe
2d792 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e  rm->u.leftColumn
2d793 21 3d 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  !=iColumn ){.   
2d794 20 20 20 20 20 20 20 6f 6b 54 6f 43 68 6e 67 54         okToChngT
2d795 6f 49 4e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  oIN = 0;.       
2d796 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2d797 20 20 69 6e 74 20 61 66 66 4c 65 66 74 2c 20 61    int affLeft, a
2d798 66 66 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20  ffRight;.       
2d799 20 20 20 2f 2a 20 49 66 20 74 68 65 20 72 69 67     /* If the rig
2d79a 68 74 2d 68 61 6e 64 20 73 69 64 65 20 69 73 20  ht-hand side is 
2d79b 61 6c 73 6f 20 61 20 63 6f 6c 75 6d 6e 2c 20 74  also a column, t
2d79c 68 65 6e 20 74 68 65 20 61 66 66 69 6e 69 74 69  hen the affiniti
2d79d 65 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  es.          ** 
2d79e 6f 66 20 62 6f 74 68 20 72 69 67 68 74 20 61 6e  of both right an
2d79f 64 20 6c 65 66 74 20 73 69 64 65 73 20 6d 75 73  d left sides mus
2d7a0 74 20 62 65 20 73 75 63 68 20 74 68 61 74 20 6e  t be such that n
2d7a1 6f 20 74 79 70 65 0a 20 20 20 20 20 20 20 20 20  o type.         
2d7a2 20 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20   ** conversions 
2d7a3 61 72 65 20 72 65 71 75 69 72 65 64 20 6f 6e 20  are required on 
2d7a4 74 68 65 20 72 69 67 68 74 2e 20 20 28 54 69 63  the right.  (Tic
2d7a5 6b 65 74 20 23 32 32 34 39 29 0a 20 20 20 20 20  ket #2249).     
2d7a6 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
2d7a7 20 20 61 66 66 52 69 67 68 74 20 3d 20 73 71 6c    affRight = sql
2d7a8 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79  ite3ExprAffinity
2d7a9 28 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2d  (pOrTerm->pExpr-
2d7aa 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20  >pRight);.      
2d7ab 20 20 20 20 61 66 66 4c 65 66 74 20 3d 20 73 71      affLeft = sq
2d7ac 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74  lite3ExprAffinit
2d7ad 79 28 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72  y(pOrTerm->pExpr
2d7ae 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20  ->pLeft);.      
2d7af 20 20 20 20 69 66 28 20 61 66 66 52 69 67 68 74      if( affRight
2d7b0 21 3d 30 20 26 26 20 61 66 66 52 69 67 68 74 21  !=0 && affRight!
2d7b1 3d 61 66 66 4c 65 66 74 20 29 7b 0a 20 20 20 20  =affLeft ){.    
2d7b2 20 20 20 20 20 20 20 20 6f 6b 54 6f 43 68 6e 67          okToChng
2d7b3 54 6f 49 4e 20 3d 20 30 3b 0a 20 20 20 20 20 20  ToIN = 0;.      
2d7b4 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2d7b5 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d 2d 3e         pOrTerm->
2d7b6 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f  wtFlags |= TERM_
2d7b7 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20  OR_OK;.         
2d7b8 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
2d7b9 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
2d7ba 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74  /* At this point
2d7bb 2c 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 69  , okToChngToIN i
2d7bc 73 20 74 72 75 65 20 69 66 20 6f 72 69 67 69 6e  s true if origin
2d7bd 61 6c 20 70 54 65 72 6d 20 73 61 74 69 73 66 69  al pTerm satisfi
2d7be 65 73 0a 20 20 20 20 2a 2a 20 63 61 73 65 20 31  es.    ** case 1
2d7bf 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c  .  In that case,
2d7c0 20 63 6f 6e 73 74 72 75 63 74 20 61 20 6e 65 77   construct a new
2d7c1 20 76 69 72 74 75 61 6c 20 74 65 72 6d 20 74 68   virtual term th
2d7c2 61 74 20 69 73 20 0a 20 20 20 20 2a 2a 20 70 54  at is .    ** pT
2d7c3 65 72 6d 20 63 6f 6e 76 65 72 74 65 64 20 69 6e  erm converted in
2d7c4 74 6f 20 61 6e 20 49 4e 20 6f 70 65 72 61 74 6f  to an IN operato
2d7c5 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  r..    */.    if
2d7c6 28 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 29  ( okToChngToIN )
2d7c7 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 44  {.      Expr *pD
2d7c8 75 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  up;            /
2d7c9 2a 20 41 20 74 72 61 6e 73 69 65 6e 74 20 64 75  * A transient du
2d7ca 70 6c 69 63 61 74 65 20 65 78 70 72 65 73 73 69  plicate expressi
2d7cb 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72  on */.      Expr
2d7cc 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 30 3b  List *pList = 0;
2d7cd 20 20 20 2f 2a 20 54 68 65 20 52 48 53 20 6f 66     /* The RHS of
2d7ce 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72   the IN operator
2d7cf 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a   */.      Expr *
2d7d0 70 4c 65 66 74 20 3d 20 30 3b 20 20 20 20 20 20  pLeft = 0;      
2d7d1 20 2f 2a 20 54 68 65 20 4c 48 53 20 6f 66 20 74   /* The LHS of t
2d7d2 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a  he IN operator *
2d7d3 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e  /.      Expr *pN
2d7d4 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ew;            /
2d7d5 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 49  * The complete I
2d7d6 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 0a 20  N operator */.. 
2d7d7 20 20 20 20 20 66 6f 72 28 69 3d 70 4f 72 57 63       for(i=pOrWc
2d7d8 2d 3e 6e 54 65 72 6d 2d 31 2c 20 70 4f 72 54 65  ->nTerm-1, pOrTe
2d7d9 72 6d 3d 70 4f 72 57 63 2d 3e 61 3b 20 69 3e 3d  rm=pOrWc->a; i>=
2d7da 30 3b 20 69 2d 2d 2c 20 70 4f 72 54 65 72 6d 2b  0; i--, pOrTerm+
2d7db 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
2d7dc 28 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67  (pOrTerm->wtFlag
2d7dd 73 20 26 20 54 45 52 4d 5f 4f 52 5f 4f 4b 29 3d  s & TERM_OR_OK)=
2d7de 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =0 ) continue;. 
2d7df 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
2d7e0 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f  OrTerm->eOperato
2d7e1 72 3d 3d 57 4f 5f 45 51 20 29 3b 0a 20 20 20 20  r==WO_EQ );.    
2d7e2 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 72 54      assert( pOrT
2d7e3 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d  erm->leftCursor=
2d7e4 3d 69 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 20  =iCursor );.    
2d7e5 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 72 54      assert( pOrT
2d7e6 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d  erm->u.leftColum
2d7e7 6e 3d 3d 69 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20  n==iColumn );.  
2d7e8 20 20 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c        pDup = sql
2d7e9 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
2d7ea 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e  pOrTerm->pExpr->
2d7eb 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 20 20 20  pRight, 0);.    
2d7ec 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69      pList = sqli
2d7ed 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
2d7ee 64 28 70 57 43 2d 3e 70 50 61 72 73 65 2c 20 70  d(pWC->pParse, p
2d7ef 4c 69 73 74 2c 20 70 44 75 70 29 3b 0a 20 20 20  List, pDup);.   
2d7f0 20 20 20 20 20 70 4c 65 66 74 20 3d 20 70 4f 72       pLeft = pOr
2d7f1 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 4c 65  Term->pExpr->pLe
2d7f2 66 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ft;.      }.    
2d7f3 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 74 21    assert( pLeft!
2d7f4 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 44 75 70  =0 );.      pDup
2d7f5 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
2d7f6 70 28 64 62 2c 20 70 4c 65 66 74 2c 20 30 29 3b  p(db, pLeft, 0);
2d7f7 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71  .      pNew = sq
2d7f8 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2d7f9 65 2c 20 54 4b 5f 49 4e 2c 20 70 44 75 70 2c 20  e, TK_IN, pDup, 
2d7fa 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  0, 0);.      if(
2d7fb 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20   pNew ){.       
2d7fc 20 69 6e 74 20 69 64 78 4e 65 77 3b 0a 20 20 20   int idxNew;.   
2d7fd 20 20 20 20 20 74 72 61 6e 73 66 65 72 4a 6f 69       transferJoi
2d7fe 6e 4d 61 72 6b 69 6e 67 73 28 70 4e 65 77 2c 20  nMarkings(pNew, 
2d7ff 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20  pExpr);.        
2d800 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
2d801 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20 45  Property(pNew, E
2d802 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a  P_xIsSelect) );.
2d803 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e          pNew->x.
2d804 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a 20  pList = pList;. 
2d805 20 20 20 20 20 20 20 69 64 78 4e 65 77 20 3d 20         idxNew = 
2d806 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72  whereClauseInser
2d807 74 28 70 57 43 2c 20 70 4e 65 77 2c 20 54 45 52  t(pWC, pNew, TER
2d808 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44  M_VIRTUAL|TERM_D
2d809 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 20  YNAMIC);.       
2d80a 20 74 65 73 74 63 61 73 65 28 20 69 64 78 4e 65   testcase( idxNe
2d80b 77 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  w==0 );.        
2d80c 65 78 70 72 41 6e 61 6c 79 7a 65 28 70 53 72 63  exprAnalyze(pSrc
2d80d 2c 20 70 57 43 2c 20 69 64 78 4e 65 77 29 3b 0a  , pWC, idxNew);.
2d80e 20 20 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20          pTerm = 
2d80f 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d  &pWC->a[idxTerm]
2d810 3b 0a 20 20 20 20 20 20 20 20 70 57 43 2d 3e 61  ;.        pWC->a
2d811 5b 69 64 78 4e 65 77 5d 2e 69 50 61 72 65 6e 74  [idxNew].iParent
2d812 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20   = idxTerm;.    
2d813 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c      pTerm->nChil
2d814 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c  d = 1;.      }el
2d815 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
2d816 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
2d817 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20  e(db, pList);.  
2d818 20 20 20 20 7d 0a 20 20 20 20 20 20 70 54 65 72      }.      pTer
2d819 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 30  m->eOperator = 0
2d81a 3b 20 20 2f 2a 20 63 61 73 65 20 31 20 74 72 75  ;  /* case 1 tru
2d81b 6d 70 73 20 63 61 73 65 20 32 20 2a 2f 0a 20 20  mps case 2 */.  
2d81c 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66    }.  }.}.#endif
2d81d 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54   /* !SQLITE_OMIT
2d81e 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e  _OR_OPTIMIZATION
2d81f 20 26 26 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54   && !SQLITE_OMIT
2d820 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a 0a 2f  _SUBQUERY */.../
2d821 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 74  *.** The input t
2d822 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  o this routine i
2d823 73 20 61 6e 20 57 68 65 72 65 54 65 72 6d 20 73  s an WhereTerm s
2d824 74 72 75 63 74 75 72 65 20 77 69 74 68 20 6f 6e  tructure with on
2d825 6c 79 20 74 68 65 0a 2a 2a 20 22 70 45 78 70 72  ly the.** "pExpr
2d826 22 20 66 69 65 6c 64 20 66 69 6c 6c 65 64 20 69  " field filled i
2d827 6e 2e 20 20 54 68 65 20 6a 6f 62 20 6f 66 20 74  n.  The job of t
2d828 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74  his routine is t
2d829 6f 20 61 6e 61 6c 79 7a 65 20 74 68 65 0a 2a 2a  o analyze the.**
2d82a 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 61   subexpression a
2d82b 6e 64 20 70 6f 70 75 6c 61 74 65 20 61 6c 6c 20  nd populate all 
2d82c 74 68 65 20 6f 74 68 65 72 20 66 69 65 6c 64 73  the other fields
2d82d 20 6f 66 20 74 68 65 20 57 68 65 72 65 54 65 72   of the WhereTer
2d82e 6d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a  m.** structure..
2d82f 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70  **.** If the exp
2d830 72 65 73 73 69 6f 6e 20 69 73 20 6f 66 20 74 68  ression is of th
2d831 65 20 66 6f 72 6d 20 22 3c 65 78 70 72 3e 20 3c  e form "<expr> <
2d832 6f 70 3e 20 58 22 20 69 74 20 67 65 74 73 20 63  op> X" it gets c
2d833 6f 6d 6d 75 74 65 64 0a 2a 2a 20 74 6f 20 74 68  ommuted.** to th
2d834 65 20 73 74 61 6e 64 61 72 64 20 66 6f 72 6d 20  e standard form 
2d835 6f 66 20 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72  of "X <op> <expr
2d836 3e 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  >"..**.** If the
2d837 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6f   expression is o
2d838 66 20 74 68 65 20 66 6f 72 6d 20 22 58 20 3c 6f  f the form "X <o
2d839 70 3e 20 59 22 20 77 68 65 72 65 20 62 6f 74 68  p> Y" where both
2d83a 20 58 20 61 6e 64 20 59 20 61 72 65 0a 2a 2a 20   X and Y are.** 
2d83b 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 74 68  columns, then th
2d83c 65 20 6f 72 69 67 69 6e 61 6c 20 65 78 70 72 65  e original expre
2d83d 73 73 69 6f 6e 20 69 73 20 75 6e 63 68 61 6e 67  ssion is unchang
2d83e 65 64 20 61 6e 64 20 61 20 6e 65 77 20 76 69 72  ed and a new vir
2d83f 74 75 61 6c 0a 2a 2a 20 74 65 72 6d 20 6f 66 20  tual.** term of 
2d840 74 68 65 20 66 6f 72 6d 20 22 59 20 3c 6f 70 3e  the form "Y <op>
2d841 20 58 22 20 69 73 20 61 64 64 65 64 20 74 6f 20   X" is added to 
2d842 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2d843 20 61 6e 64 0a 2a 2a 20 61 6e 61 6c 79 7a 65 64   and.** analyzed
2d844 20 73 65 70 61 72 61 74 65 6c 79 2e 20 20 54 68   separately.  Th
2d845 65 20 6f 72 69 67 69 6e 61 6c 20 74 65 72 6d 20  e original term 
2d846 69 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20 54  is marked with T
2d847 45 52 4d 5f 43 4f 50 49 45 44 0a 2a 2a 20 61 6e  ERM_COPIED.** an
2d848 64 20 74 68 65 20 6e 65 77 20 74 65 72 6d 20 69  d the new term i
2d849 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20 54 45  s marked with TE
2d84a 52 4d 5f 44 59 4e 41 4d 49 43 20 28 62 65 63 61  RM_DYNAMIC (beca
2d84b 75 73 65 20 69 74 27 73 20 70 45 78 70 72 0a 2a  use it's pExpr.*
2d84c 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 66 72  * needs to be fr
2d84d 65 65 64 20 77 69 74 68 20 74 68 65 20 57 68 65  eed with the Whe
2d84e 72 65 43 6c 61 75 73 65 29 20 61 6e 64 20 54 45  reClause) and TE
2d84f 52 4d 5f 56 49 52 54 55 41 4c 20 28 62 65 63 61  RM_VIRTUAL (beca
2d850 75 73 65 20 69 74 0a 2a 2a 20 69 73 20 61 20 63  use it.** is a c
2d851 6f 6d 6d 75 74 65 64 20 63 6f 70 79 20 6f 66 20  ommuted copy of 
2d852 61 20 70 72 69 6f 72 20 74 65 72 6d 2e 29 20 20  a prior term.)  
2d853 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 65 72  The original ter
2d854 6d 20 68 61 73 20 6e 43 68 69 6c 64 3d 31 0a 2a  m has nChild=1.*
2d855 2a 20 61 6e 64 20 74 68 65 20 63 6f 70 79 20 68  * and the copy h
2d856 61 73 20 69 64 78 50 61 72 65 6e 74 20 73 65 74  as idxParent set
2d857 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66   to the index of
2d858 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 65   the original te
2d859 72 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  rm..*/.static vo
2d85a 69 64 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 0a  id exprAnalyze(.
2d85b 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c    SrcList *pSrc,
2d85c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74              /* t
2d85d 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a  he FROM clause *
2d85e 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20  /.  WhereClause 
2d85f 2a 70 57 43 2c 20 20 20 20 20 20 20 20 20 2f 2a  *pWC,         /*
2d860 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2d861 65 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 54 65  e */.  int idxTe
2d862 72 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rm              
2d863 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65   /* Index of the
2d864 20 74 65 72 6d 20 74 6f 20 62 65 20 61 6e 61 6c   term to be anal
2d865 79 7a 65 64 20 2a 2f 0a 29 7b 0a 20 20 57 68 65  yzed */.){.  Whe
2d866 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20  reTerm *pTerm;  
2d867 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2d868 20 54 68 65 20 74 65 72 6d 20 74 6f 20 62 65 20   The term to be 
2d869 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 57 68  analyzed */.  Wh
2d86a 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73  ereMaskSet *pMas
2d86b 6b 53 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f  kSet;          /
2d86c 2a 20 53 65 74 20 6f 66 20 74 61 62 6c 65 20 69  * Set of table i
2d86d 6e 64 65 78 20 6d 61 73 6b 73 20 2a 2f 0a 20 20  ndex masks */.  
2d86e 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20  Expr *pExpr;    
2d86f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d870 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
2d871 6f 6e 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65  on to be analyze
2d872 64 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 70  d */.  Bitmask p
2d873 72 65 72 65 71 4c 65 66 74 3b 20 20 20 20 20 20  rereqLeft;      
2d874 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 72 65          /* Prere
2d875 71 75 65 73 69 74 65 73 20 6f 66 20 74 68 65 20  quesites of the 
2d876 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 2a 2f 0a  pExpr->pLeft */.
2d877 20 20 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71    Bitmask prereq
2d878 41 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  All;            
2d879 20 20 20 2f 2a 20 50 72 65 72 65 71 75 65 73 69     /* Prerequesi
2d87a 74 65 73 20 6f 66 20 70 45 78 70 72 20 2a 2f 0a  tes of pExpr */.
2d87b 20 20 42 69 74 6d 61 73 6b 20 65 78 74 72 61 52    Bitmask extraR
2d87c 69 67 68 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20  ight = 0;.  int 
2d87d 6e 50 61 74 74 65 72 6e 3b 0a 20 20 69 6e 74 20  nPattern;.  int 
2d87e 69 73 43 6f 6d 70 6c 65 74 65 3b 0a 20 20 69 6e  isComplete;.  in
2d87f 74 20 6e 6f 43 61 73 65 3b 0a 20 20 69 6e 74 20  t noCase;.  int 
2d880 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
2d881 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d882 54 6f 70 2d 6c 65 76 65 6c 20 6f 70 65 72 61 74  Top-level operat
2d883 6f 72 2e 20 20 70 45 78 70 72 2d 3e 6f 70 20 2a  or.  pExpr->op *
2d884 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  /.  Parse *pPars
2d885 65 20 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 3b  e = pWC->pParse;
2d886 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
2d887 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 73 71 6c  context */.  sql
2d888 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2d889 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 2f 2a  e->db;        /*
2d88a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
2d88b 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 64  tion */..  if( d
2d88c 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2d88d 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
2d88e 20 7d 0a 20 20 70 54 65 72 6d 20 3d 20 26 70 57   }.  pTerm = &pW
2d88f 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20  C->a[idxTerm];. 
2d890 20 70 4d 61 73 6b 53 65 74 20 3d 20 70 57 43 2d   pMaskSet = pWC-
2d891 3e 70 4d 61 73 6b 53 65 74 3b 0a 20 20 70 45 78  >pMaskSet;.  pEx
2d892 70 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70  pr = pTerm->pExp
2d893 72 3b 0a 20 20 70 72 65 72 65 71 4c 65 66 74 20  r;.  prereqLeft 
2d894 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65  = exprTableUsage
2d895 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72  (pMaskSet, pExpr
2d896 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 6f 70 20 3d  ->pLeft);.  op =
2d897 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 69 66   pExpr->op;.  if
2d898 28 20 6f 70 3d 3d 54 4b 5f 49 4e 20 29 7b 0a 20  ( op==TK_IN ){. 
2d899 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
2d89a 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 20  ->pRight==0 );. 
2d89b 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72     if( ExprHasPr
2d89c 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
2d89d 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20  _xIsSelect) ){. 
2d89e 20 20 20 20 20 70 54 65 72 6d 2d 3e 70 72 65 72       pTerm->prer
2d89f 65 71 52 69 67 68 74 20 3d 20 65 78 70 72 53 65  eqRight = exprSe
2d8a0 6c 65 63 74 54 61 62 6c 65 55 73 61 67 65 28 70  lectTableUsage(p
2d8a1 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72 2d 3e  MaskSet, pExpr->
2d8a2 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  x.pSelect);.    
2d8a3 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 54 65  }else{.      pTe
2d8a4 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20  rm->prereqRight 
2d8a5 3d 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55  = exprListTableU
2d8a6 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70  sage(pMaskSet, p
2d8a7 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a  Expr->x.pList);.
2d8a8 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
2d8a9 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20  ( op==TK_ISNULL 
2d8aa 29 7b 0a 20 20 20 20 70 54 65 72 6d 2d 3e 70 72  ){.    pTerm->pr
2d8ab 65 72 65 71 52 69 67 68 74 20 3d 20 30 3b 0a 20  ereqRight = 0;. 
2d8ac 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 65 72   }else{.    pTer
2d8ad 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d  m->prereqRight =
2d8ae 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28   exprTableUsage(
2d8af 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72 2d  pMaskSet, pExpr-
2d8b0 3e 70 52 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20  >pRight);.  }.  
2d8b1 70 72 65 72 65 71 41 6c 6c 20 3d 20 65 78 70 72  prereqAll = expr
2d8b2 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b  TableUsage(pMask
2d8b3 53 65 74 2c 20 70 45 78 70 72 29 3b 0a 20 20 69  Set, pExpr);.  i
2d8b4 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  f( ExprHasProper
2d8b5 74 79 28 70 45 78 70 72 2c 20 45 50 5f 46 72 6f  ty(pExpr, EP_Fro
2d8b6 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20 42 69  mJoin) ){.    Bi
2d8b7 74 6d 61 73 6b 20 78 20 3d 20 67 65 74 4d 61 73  tmask x = getMas
2d8b8 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70  k(pMaskSet, pExp
2d8b9 72 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62  r->iRightJoinTab
2d8ba 6c 65 29 3b 0a 20 20 20 20 70 72 65 72 65 71 41  le);.    prereqA
2d8bb 6c 6c 20 7c 3d 20 78 3b 0a 20 20 20 20 65 78 74  ll |= x;.    ext
2d8bc 72 61 52 69 67 68 74 20 3d 20 78 2d 31 3b 20 20  raRight = x-1;  
2d8bd 2f 2a 20 4f 4e 20 63 6c 61 75 73 65 20 74 65 72  /* ON clause ter
2d8be 6d 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 75 73  ms may not be us
2d8bf 65 64 20 77 69 74 68 20 61 6e 20 69 6e 64 65 78  ed with an index
2d8c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2d8c1 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20 6c 65          ** on le
2d8c2 66 74 20 74 61 62 6c 65 20 6f 66 20 61 20 4c 45  ft table of a LE
2d8c3 46 54 20 4a 4f 49 4e 2e 20 20 54 69 63 6b 65 74  FT JOIN.  Ticket
2d8c4 20 23 33 30 31 35 20 2a 2f 0a 20 20 7d 0a 20 20   #3015 */.  }.  
2d8c5 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c  pTerm->prereqAll
2d8c6 20 3d 20 70 72 65 72 65 71 41 6c 6c 3b 0a 20 20   = prereqAll;.  
2d8c7 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  pTerm->leftCurso
2d8c8 72 20 3d 20 2d 31 3b 0a 20 20 70 54 65 72 6d 2d  r = -1;.  pTerm-
2d8c9 3e 69 50 61 72 65 6e 74 20 3d 20 2d 31 3b 0a 20  >iParent = -1;. 
2d8ca 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
2d8cb 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 61 6c 6c  r = 0;.  if( all
2d8cc 6f 77 65 64 4f 70 28 6f 70 29 20 26 26 20 28 70  owedOp(op) && (p
2d8cd 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68  Term->prereqRigh
2d8ce 74 20 26 20 70 72 65 72 65 71 4c 65 66 74 29 3d  t & prereqLeft)=
2d8cf 3d 30 20 29 7b 0a 20 20 20 20 45 78 70 72 20 2a  =0 ){.    Expr *
2d8d0 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70  pLeft = pExpr->p
2d8d1 4c 65 66 74 3b 0a 20 20 20 20 45 78 70 72 20 2a  Left;.    Expr *
2d8d2 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e  pRight = pExpr->
2d8d3 70 52 69 67 68 74 3b 0a 20 20 20 20 69 66 28 20  pRight;.    if( 
2d8d4 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  pLeft->op==TK_CO
2d8d5 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20 70 54  LUMN ){.      pT
2d8d6 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20  erm->leftCursor 
2d8d7 3d 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65 3b  = pLeft->iTable;
2d8d8 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 75 2e  .      pTerm->u.
2d8d9 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65  leftColumn = pLe
2d8da 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20  ft->iColumn;.   
2d8db 20 20 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61     pTerm->eOpera
2d8dc 74 6f 72 20 3d 20 6f 70 65 72 61 74 6f 72 4d 61  tor = operatorMa
2d8dd 73 6b 28 6f 70 29 3b 0a 20 20 20 20 7d 0a 20 20  sk(op);.    }.  
2d8de 20 20 69 66 28 20 70 52 69 67 68 74 20 26 26 20    if( pRight && 
2d8df 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 43  pRight->op==TK_C
2d8e0 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20 57  OLUMN ){.      W
2d8e1 68 65 72 65 54 65 72 6d 20 2a 70 4e 65 77 3b 0a  hereTerm *pNew;.
2d8e2 20 20 20 20 20 20 45 78 70 72 20 2a 70 44 75 70        Expr *pDup
2d8e3 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 65 72  ;.      if( pTer
2d8e4 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3e 3d 30  m->leftCursor>=0
2d8e5 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
2d8e6 69 64 78 4e 65 77 3b 0a 20 20 20 20 20 20 20 20  idxNew;.        
2d8e7 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78  pDup = sqlite3Ex
2d8e8 70 72 44 75 70 28 64 62 2c 20 70 45 78 70 72 2c  prDup(db, pExpr,
2d8e9 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   0);.        if(
2d8ea 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2d8eb 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  d ){.          s
2d8ec 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
2d8ed 28 64 62 2c 20 70 44 75 70 29 3b 0a 20 20 20 20  (db, pDup);.    
2d8ee 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
2d8ef 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2d8f0 69 64 78 4e 65 77 20 3d 20 77 68 65 72 65 43 6c  idxNew = whereCl
2d8f1 61 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20  auseInsert(pWC, 
2d8f2 70 44 75 70 2c 20 54 45 52 4d 5f 56 49 52 54 55  pDup, TERM_VIRTU
2d8f3 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29  AL|TERM_DYNAMIC)
2d8f4 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 64  ;.        if( id
2d8f5 78 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e  xNew==0 ) return
2d8f6 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d  ;.        pNew =
2d8f7 20 26 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d   &pWC->a[idxNew]
2d8f8 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e  ;.        pNew->
2d8f9 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72  iParent = idxTer
2d8fa 6d 3b 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d  m;.        pTerm
2d8fb 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65   = &pWC->a[idxTe
2d8fc 72 6d 5d 3b 0a 20 20 20 20 20 20 20 20 70 54 65  rm];.        pTe
2d8fd 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 31 3b 0a  rm->nChild = 1;.
2d8fe 20 20 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 77          pTerm->w
2d8ff 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43  tFlags |= TERM_C
2d900 4f 50 49 45 44 3b 0a 20 20 20 20 20 20 7d 65 6c  OPIED;.      }el
2d901 73 65 7b 0a 20 20 20 20 20 20 20 20 70 44 75 70  se{.        pDup
2d902 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20   = pExpr;.      
2d903 20 20 70 4e 65 77 20 3d 20 70 54 65 72 6d 3b 0a    pNew = pTerm;.
2d904 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 65 78        }.      ex
2d905 70 72 43 6f 6d 6d 75 74 65 28 70 50 61 72 73 65  prCommute(pParse
2d906 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20 20 70  , pDup);.      p
2d907 4c 65 66 74 20 3d 20 70 44 75 70 2d 3e 70 4c 65  Left = pDup->pLe
2d908 66 74 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e  ft;.      pNew->
2d909 6c 65 66 74 43 75 72 73 6f 72 20 3d 20 70 4c 65  leftCursor = pLe
2d90a 66 74 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20  ft->iTable;.    
2d90b 20 20 70 4e 65 77 2d 3e 75 2e 6c 65 66 74 43 6f    pNew->u.leftCo
2d90c 6c 75 6d 6e 20 3d 20 70 4c 65 66 74 2d 3e 69 43  lumn = pLeft->iC
2d90d 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 70 4e 65  olumn;.      pNe
2d90e 77 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d  w->prereqRight =
2d90f 20 70 72 65 72 65 71 4c 65 66 74 3b 0a 20 20 20   prereqLeft;.   
2d910 20 20 20 70 4e 65 77 2d 3e 70 72 65 72 65 71 41     pNew->prereqA
2d911 6c 6c 20 3d 20 70 72 65 72 65 71 41 6c 6c 3b 0a  ll = prereqAll;.
2d912 20 20 20 20 20 20 70 4e 65 77 2d 3e 65 4f 70 65        pNew->eOpe
2d913 72 61 74 6f 72 20 3d 20 6f 70 65 72 61 74 6f 72  rator = operator
2d914 4d 61 73 6b 28 70 44 75 70 2d 3e 6f 70 29 3b 0a  Mask(pDup->op);.
2d915 20 20 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 6e 64      }.  }..#ifnd
2d916 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ef SQLITE_OMIT_B
2d917 45 54 57 45 45 4e 5f 4f 50 54 49 4d 49 5a 41 54  ETWEEN_OPTIMIZAT
2d918 49 4f 4e 0a 20 20 2f 2a 20 49 66 20 61 20 74 65  ION.  /* If a te
2d919 72 6d 20 69 73 20 74 68 65 20 42 45 54 57 45 45  rm is the BETWEE
2d91a 4e 20 6f 70 65 72 61 74 6f 72 2c 20 63 72 65 61  N operator, crea
2d91b 74 65 20 74 77 6f 20 6e 65 77 20 76 69 72 74 75  te two new virtu
2d91c 61 6c 20 74 65 72 6d 73 0a 20 20 2a 2a 20 74 68  al terms.  ** th
2d91d 61 74 20 64 65 66 69 6e 65 20 74 68 65 20 72 61  at define the ra
2d91e 6e 67 65 20 74 68 61 74 20 74 68 65 20 42 45 54  nge that the BET
2d91f 57 45 45 4e 20 69 6d 70 6c 65 6d 65 6e 74 73 2e  WEEN implements.
2d920 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 20    For example:. 
2d921 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 61 20   **.  **      a 
2d922 42 45 54 57 45 45 4e 20 62 20 41 4e 44 20 63 0a  BETWEEN b AND c.
2d923 20 20 2a 2a 0a 20 20 2a 2a 20 69 73 20 63 6f 6e    **.  ** is con
2d924 76 65 72 74 65 64 20 69 6e 74 6f 3a 0a 20 20 2a  verted into:.  *
2d925 2a 0a 20 20 2a 2a 20 20 20 20 20 20 28 61 20 42  *.  **      (a B
2d926 45 54 57 45 45 4e 20 62 20 41 4e 44 20 63 29 20  ETWEEN b AND c) 
2d927 41 4e 44 20 28 61 3e 3d 62 29 20 41 4e 44 20 28  AND (a>=b) AND (
2d928 61 3c 3d 63 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20  a<=c).  **.  ** 
2d929 54 68 65 20 74 77 6f 20 6e 65 77 20 74 65 72 6d  The two new term
2d92a 73 20 61 72 65 20 61 64 64 65 64 20 6f 6e 74 6f  s are added onto
2d92b 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
2d92c 57 68 65 72 65 43 6c 61 75 73 65 20 6f 62 6a 65  WhereClause obje
2d92d 63 74 2e 0a 20 20 2a 2a 20 54 68 65 20 6e 65 77  ct..  ** The new
2d92e 20 74 65 72 6d 73 20 61 72 65 20 22 64 79 6e 61   terms are "dyna
2d92f 6d 69 63 22 20 61 6e 64 20 61 72 65 20 63 68 69  mic" and are chi
2d930 6c 64 72 65 6e 20 6f 66 20 74 68 65 20 6f 72 69  ldren of the ori
2d931 67 69 6e 61 6c 20 42 45 54 57 45 45 4e 0a 20 20  ginal BETWEEN.  
2d932 2a 2a 20 74 65 72 6d 2e 20 20 54 68 61 74 20 6d  ** term.  That m
2d933 65 61 6e 73 20 74 68 61 74 20 69 66 20 74 68 65  eans that if the
2d934 20 42 45 54 57 45 45 4e 20 74 65 72 6d 20 69 73   BETWEEN term is
2d935 20 63 6f 64 65 64 2c 20 74 68 65 20 63 68 69 6c   coded, the chil
2d936 64 72 65 6e 20 61 72 65 0a 20 20 2a 2a 20 73 6b  dren are.  ** sk
2d937 69 70 70 65 64 2e 20 20 4f 72 2c 20 69 66 20 74  ipped.  Or, if t
2d938 68 65 20 63 68 69 6c 64 72 65 6e 20 61 72 65 20  he children are 
2d939 73 61 74 69 73 66 69 65 64 20 62 79 20 61 6e 20  satisfied by an 
2d93a 69 6e 64 65 78 2c 20 74 68 65 20 6f 72 69 67 69  index, the origi
2d93b 6e 61 6c 0a 20 20 2a 2a 20 42 45 54 57 45 45 4e  nal.  ** BETWEEN
2d93c 20 74 65 72 6d 20 69 73 20 73 6b 69 70 70 65 64   term is skipped
2d93d 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66  ..  */.  else if
2d93e 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
2d93f 42 45 54 57 45 45 4e 20 26 26 20 70 57 43 2d 3e  BETWEEN && pWC->
2d940 6f 70 3d 3d 54 4b 5f 41 4e 44 20 29 7b 0a 20 20  op==TK_AND ){.  
2d941 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
2d942 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  t = pExpr->x.pLi
2d943 73 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20  st;.    int i;. 
2d944 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
2d945 75 38 20 6f 70 73 5b 5d 20 3d 20 7b 54 4b 5f 47  u8 ops[] = {TK_G
2d946 45 2c 20 54 4b 5f 4c 45 7d 3b 0a 20 20 20 20 61  E, TK_LE};.    a
2d947 73 73 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20  ssert( pList!=0 
2d948 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
2d949 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 32 20 29  List->nExpr==2 )
2d94a 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
2d94b 3c 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  <2; i++){.      
2d94c 45 78 70 72 20 2a 70 4e 65 77 45 78 70 72 3b 0a  Expr *pNewExpr;.
2d94d 20 20 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77        int idxNew
2d94e 3b 0a 20 20 20 20 20 20 70 4e 65 77 45 78 70 72  ;.      pNewExpr
2d94f 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2d950 70 50 61 72 73 65 2c 20 6f 70 73 5b 69 5d 2c 20  pParse, ops[i], 
2d951 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2d952 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
2d953 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
2d954 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 30   pExpr->pLeft, 0
2d955 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
2d956 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d957 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
2d958 62 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70  b, pList->a[i].p
2d959 45 78 70 72 2c 20 30 29 2c 20 30 29 3b 0a 20 20  Expr, 0), 0);.  
2d95a 20 20 20 20 69 64 78 4e 65 77 20 3d 20 77 68 65      idxNew = whe
2d95b 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70  reClauseInsert(p
2d95c 57 43 2c 20 70 4e 65 77 45 78 70 72 2c 20 54 45  WC, pNewExpr, TE
2d95d 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f  RM_VIRTUAL|TERM_
2d95e 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20  DYNAMIC);.      
2d95f 74 65 73 74 63 61 73 65 28 20 69 64 78 4e 65 77  testcase( idxNew
2d960 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 65 78 70  ==0 );.      exp
2d961 72 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20 70  rAnalyze(pSrc, p
2d962 57 43 2c 20 69 64 78 4e 65 77 29 3b 0a 20 20 20  WC, idxNew);.   
2d963 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d     pTerm = &pWC-
2d964 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20  >a[idxTerm];.   
2d965 20 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77     pWC->a[idxNew
2d966 5d 2e 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54  ].iParent = idxT
2d967 65 72 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  erm;.    }.    p
2d968 54 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 32  Term->nChild = 2
2d969 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;.  }.#endif /* 
2d96a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 45 54 57  SQLITE_OMIT_BETW
2d96b 45 45 4e 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e  EEN_OPTIMIZATION
2d96c 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65   */..#if !define
2d96d 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52  d(SQLITE_OMIT_OR
2d96e 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 29 20 26  _OPTIMIZATION) &
2d96f 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
2d970 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29  E_OMIT_SUBQUERY)
2d971 0a 20 20 2f 2a 20 41 6e 61 6c 79 7a 65 20 61 20  .  /* Analyze a 
2d972 74 65 72 6d 20 74 68 61 74 20 69 73 20 63 6f 6d  term that is com
2d973 70 6f 73 65 64 20 6f 66 20 74 77 6f 20 6f 72 20  posed of two or 
2d974 6d 6f 72 65 20 73 75 62 74 65 72 6d 73 20 63 6f  more subterms co
2d975 6e 6e 65 63 74 65 64 20 62 79 0a 20 20 2a 2a 20  nnected by.  ** 
2d976 61 6e 20 4f 52 20 6f 70 65 72 61 74 6f 72 2e 0a  an OR operator..
2d977 20 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 28 20    */.  else if( 
2d978 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4f 52  pExpr->op==TK_OR
2d979 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
2d97a 70 57 43 2d 3e 6f 70 3d 3d 54 4b 5f 41 4e 44 20  pWC->op==TK_AND 
2d97b 29 3b 0a 20 20 20 20 65 78 70 72 41 6e 61 6c 79  );.    exprAnaly
2d97c 7a 65 4f 72 54 65 72 6d 28 70 53 72 63 2c 20 70  zeOrTerm(pSrc, p
2d97d 57 43 2c 20 69 64 78 54 65 72 6d 29 3b 0a 20 20  WC, idxTerm);.  
2d97e 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e    pTerm = &pWC->
2d97f 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 7d 0a  a[idxTerm];.  }.
2d980 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2d981 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a  _OMIT_OR_OPTIMIZ
2d982 41 54 49 4f 4e 20 2a 2f 0a 0a 23 69 66 6e 64 65  ATION */..#ifnde
2d983 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49  f SQLITE_OMIT_LI
2d984 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a  KE_OPTIMIZATION.
2d985 20 20 2f 2a 20 41 64 64 20 63 6f 6e 73 74 72 61    /* Add constra
2d986 69 6e 74 73 20 74 6f 20 72 65 64 75 63 65 20 74  ints to reduce t
2d987 68 65 20 73 65 61 72 63 68 20 73 70 61 63 65 20  he search space 
2d988 6f 6e 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f  on a LIKE or GLO
2d989 42 0a 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72 2e  B.  ** operator.
2d98a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 6c 69 6b  .  **.  ** A lik
2d98b 65 20 70 61 74 74 65 72 6e 20 6f 66 20 74 68 65  e pattern of the
2d98c 20 66 6f 72 6d 20 22 78 20 4c 49 4b 45 20 27 61   form "x LIKE 'a
2d98d 62 63 25 27 22 20 69 73 20 63 68 61 6e 67 65 64  bc%'" is changed
2d98e 20 69 6e 74 6f 20 63 6f 6e 73 74 72 61 69 6e 74   into constraint
2d98f 73 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  s.  **.  **     
2d990 20 20 20 20 20 78 3e 3d 27 61 62 63 27 20 41 4e       x>='abc' AN
2d991 44 20 78 3c 27 61 62 64 27 20 41 4e 44 20 78 20  D x<'abd' AND x 
2d992 4c 49 4b 45 20 27 61 62 63 25 27 0a 20 20 2a 2a  LIKE 'abc%'.  **
2d993 0a 20 20 2a 2a 20 54 68 65 20 6c 61 73 74 20 63  .  ** The last c
2d994 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 20  haracter of the 
2d995 70 72 65 66 69 78 20 22 61 62 63 22 20 69 73 20  prefix "abc" is 
2d996 69 6e 63 72 65 6d 65 6e 74 65 64 20 74 6f 20 66  incremented to f
2d997 6f 72 6d 20 74 68 65 0a 20 20 2a 2a 20 74 65 72  orm the.  ** ter
2d998 6d 69 6e 61 74 69 6f 6e 20 63 6f 6e 64 69 74 69  mination conditi
2d999 6f 6e 20 22 61 62 64 22 2e 0a 20 20 2a 2f 0a 20  on "abd"..  */. 
2d99a 20 69 66 28 20 69 73 4c 69 6b 65 4f 72 47 6c 6f   if( isLikeOrGlo
2d99b 62 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c  b(pParse, pExpr,
2d99c 20 26 6e 50 61 74 74 65 72 6e 2c 20 26 69 73 43   &nPattern, &isC
2d99d 6f 6d 70 6c 65 74 65 2c 20 26 6e 6f 43 61 73 65  omplete, &noCase
2d99e 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 57  ).         && pW
2d99f 43 2d 3e 6f 70 3d 3d 54 4b 5f 41 4e 44 20 29 7b  C->op==TK_AND ){
2d9a0 0a 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74  .    Expr *pLeft
2d9a1 2c 20 2a 70 52 69 67 68 74 3b 0a 20 20 20 20 45  , *pRight;.    E
2d9a2 78 70 72 20 2a 70 53 74 72 31 2c 20 2a 70 53 74  xpr *pStr1, *pSt
2d9a3 72 32 3b 0a 20 20 20 20 45 78 70 72 20 2a 70 4e  r2;.    Expr *pN
2d9a4 65 77 45 78 70 72 31 2c 20 2a 70 4e 65 77 45 78  ewExpr1, *pNewEx
2d9a5 70 72 32 3b 0a 20 20 20 20 69 6e 74 20 69 64 78  pr2;.    int idx
2d9a6 4e 65 77 31 2c 20 69 64 78 4e 65 77 32 3b 0a 0a  New1, idxNew2;..
2d9a7 20 20 20 20 70 4c 65 66 74 20 3d 20 70 45 78 70      pLeft = pExp
2d9a8 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d  r->x.pList->a[1]
2d9a9 2e 70 45 78 70 72 3b 0a 20 20 20 20 70 52 69 67  .pExpr;.    pRig
2d9aa 68 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c  ht = pExpr->x.pL
2d9ab 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b  ist->a[0].pExpr;
2d9ac 0a 20 20 20 20 70 53 74 72 31 20 3d 20 73 71 6c  .    pStr1 = sql
2d9ad 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f  ite3Expr(db, TK_
2d9ae 53 54 52 49 4e 47 2c 20 70 52 69 67 68 74 2d 3e  STRING, pRight->
2d9af 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 69  u.zToken);.    i
2d9b0 66 28 20 70 53 74 72 31 20 29 20 70 53 74 72 31  f( pStr1 ) pStr1
2d9b1 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 6e 50 61 74 74  ->u.zToken[nPatt
2d9b2 65 72 6e 5d 20 3d 20 30 3b 0a 20 20 20 20 70 53  ern] = 0;.    pS
2d9b3 74 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70  tr2 = sqlite3Exp
2d9b4 72 44 75 70 28 64 62 2c 20 70 53 74 72 31 2c 20  rDup(db, pStr1, 
2d9b5 30 29 3b 0a 20 20 20 20 69 66 28 20 21 64 62 2d  0);.    if( !db-
2d9b6 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
2d9b7 0a 20 20 20 20 20 20 75 38 20 63 2c 20 2a 70 43  .      u8 c, *pC
2d9b8 3b 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20  ;       /* Last 
2d9b9 63 68 61 72 61 63 74 65 72 20 62 65 66 6f 72 65  character before
2d9ba 20 74 68 65 20 66 69 72 73 74 20 77 69 6c 64 63   the first wildc
2d9bb 61 72 64 20 2a 2f 0a 20 20 20 20 20 20 70 43 20  ard */.      pC 
2d9bc 3d 20 28 75 38 2a 29 26 70 53 74 72 32 2d 3e 75  = (u8*)&pStr2->u
2d9bd 2e 7a 54 6f 6b 65 6e 5b 6e 50 61 74 74 65 72 6e  .zToken[nPattern
2d9be 2d 31 5d 3b 0a 20 20 20 20 20 20 63 20 3d 20 2a  -1];.      c = *
2d9bf 70 43 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 6f  pC;.      if( no
2d9c0 43 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Case ){.        
2d9c1 2f 2a 20 54 68 65 20 70 6f 69 6e 74 20 69 73 20  /* The point is 
2d9c2 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65  to increment the
2d9c3 20 6c 61 73 74 20 63 68 61 72 61 63 74 65 72 20   last character 
2d9c4 62 65 66 6f 72 65 20 74 68 65 20 66 69 72 73 74  before the first
2d9c5 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 69 6c 64  .        ** wild
2d9c6 63 61 72 64 2e 20 20 42 75 74 20 69 66 20 77 65  card.  But if we
2d9c7 20 69 6e 63 72 65 6d 65 6e 74 20 27 40 27 2c 20   increment '@', 
2d9c8 74 68 61 74 20 77 69 6c 6c 20 70 75 73 68 20 69  that will push i
2d9c9 74 20 69 6e 74 6f 20 74 68 65 0a 20 20 20 20 20  t into the.     
2d9ca 20 20 20 2a 2a 20 61 6c 70 68 61 62 65 74 69 63     ** alphabetic
2d9cb 20 72 61 6e 67 65 20 77 68 65 72 65 20 63 61 73   range where cas
2d9cc 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 77 69  e conversions wi
2d9cd 6c 6c 20 6d 65 73 73 20 75 70 20 74 68 65 20 0a  ll mess up the .
2d9ce 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 65 71 75          ** inequ
2d9cf 61 6c 69 74 79 2e 20 20 54 6f 20 61 76 6f 69 64  ality.  To avoid
2d9d0 20 74 68 69 73 2c 20 6d 61 6b 65 20 73 75 72 65   this, make sure
2d9d1 20 74 6f 20 61 6c 73 6f 20 72 75 6e 20 74 68 65   to also run the
2d9d2 20 66 75 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a   full.        **
2d9d3 20 4c 49 4b 45 20 6f 6e 20 61 6c 6c 20 63 61 6e   LIKE on all can
2d9d4 64 69 64 61 74 65 20 65 78 70 72 65 73 73 69 6f  didate expressio
2d9d5 6e 73 20 62 79 20 63 6c 65 61 72 69 6e 67 20 74  ns by clearing t
2d9d6 68 65 20 69 73 43 6f 6d 70 6c 65 74 65 20 66 6c  he isComplete fl
2d9d7 61 67 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  ag.        */.  
2d9d8 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 41 27        if( c=='A'
2d9d9 2d 31 20 29 20 69 73 43 6f 6d 70 6c 65 74 65 20  -1 ) isComplete 
2d9da 3d 20 30 3b 0a 0a 20 20 20 20 20 20 20 20 63 20  = 0;..        c 
2d9db 3d 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f  = sqlite3UpperTo
2d9dc 4c 6f 77 65 72 5b 63 5d 3b 0a 20 20 20 20 20 20  Lower[c];.      
2d9dd 7d 0a 20 20 20 20 20 20 2a 70 43 20 3d 20 63 20  }.      *pC = c 
2d9de 2b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  + 1;.    }.    p
2d9df 4e 65 77 45 78 70 72 31 20 3d 20 73 71 6c 69 74  NewExpr1 = sqlit
2d9e0 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
2d9e1 54 4b 5f 47 45 2c 20 73 71 6c 69 74 65 33 45 78  TK_GE, sqlite3Ex
2d9e2 70 72 44 75 70 28 64 62 2c 70 4c 65 66 74 2c 30  prDup(db,pLeft,0
2d9e3 29 2c 70 53 74 72 31 2c 30 29 3b 0a 20 20 20 20  ),pStr1,0);.    
2d9e4 69 64 78 4e 65 77 31 20 3d 20 77 68 65 72 65 43  idxNew1 = whereC
2d9e5 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c  lauseInsert(pWC,
2d9e6 20 70 4e 65 77 45 78 70 72 31 2c 20 54 45 52 4d   pNewExpr1, TERM
2d9e7 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59  _VIRTUAL|TERM_DY
2d9e8 4e 41 4d 49 43 29 3b 0a 20 20 20 20 74 65 73 74  NAMIC);.    test
2d9e9 63 61 73 65 28 20 69 64 78 4e 65 77 31 3d 3d 30  case( idxNew1==0
2d9ea 20 29 3b 0a 20 20 20 20 65 78 70 72 41 6e 61 6c   );.    exprAnal
2d9eb 79 7a 65 28 70 53 72 63 2c 20 70 57 43 2c 20 69  yze(pSrc, pWC, i
2d9ec 64 78 4e 65 77 31 29 3b 0a 20 20 20 20 70 4e 65  dxNew1);.    pNe
2d9ed 77 45 78 70 72 32 20 3d 20 73 71 6c 69 74 65 33  wExpr2 = sqlite3
2d9ee 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2d9ef 5f 4c 54 2c 20 73 71 6c 69 74 65 33 45 78 70 72  _LT, sqlite3Expr
2d9f0 44 75 70 28 64 62 2c 70 4c 65 66 74 2c 30 29 2c  Dup(db,pLeft,0),
2d9f1 70 53 74 72 32 2c 30 29 3b 0a 20 20 20 20 69 64  pStr2,0);.    id
2d9f2 78 4e 65 77 32 20 3d 20 77 68 65 72 65 43 6c 61  xNew2 = whereCla
2d9f3 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70  useInsert(pWC, p
2d9f4 4e 65 77 45 78 70 72 32 2c 20 54 45 52 4d 5f 56  NewExpr2, TERM_V
2d9f5 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41  IRTUAL|TERM_DYNA
2d9f6 4d 49 43 29 3b 0a 20 20 20 20 74 65 73 74 63 61  MIC);.    testca
2d9f7 73 65 28 20 69 64 78 4e 65 77 32 3d 3d 30 20 29  se( idxNew2==0 )
2d9f8 3b 0a 20 20 20 20 65 78 70 72 41 6e 61 6c 79 7a  ;.    exprAnalyz
2d9f9 65 28 70 53 72 63 2c 20 70 57 43 2c 20 69 64 78  e(pSrc, pWC, idx
2d9fa 4e 65 77 32 29 3b 0a 20 20 20 20 70 54 65 72 6d  New2);.    pTerm
2d9fb 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65   = &pWC->a[idxTe
2d9fc 72 6d 5d 3b 0a 20 20 20 20 69 66 28 20 69 73 43  rm];.    if( isC
2d9fd 6f 6d 70 6c 65 74 65 20 29 7b 0a 20 20 20 20 20  omplete ){.     
2d9fe 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 31 5d   pWC->a[idxNew1]
2d9ff 2e 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65  .iParent = idxTe
2da00 72 6d 3b 0a 20 20 20 20 20 20 70 57 43 2d 3e 61  rm;.      pWC->a
2da01 5b 69 64 78 4e 65 77 32 5d 2e 69 50 61 72 65 6e  [idxNew2].iParen
2da02 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20  t = idxTerm;.   
2da03 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c 64     pTerm->nChild
2da04 20 3d 20 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 2;.    }.  }.
2da05 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2da06 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d  _OMIT_LIKE_OPTIM
2da07 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 23 69 66 6e  IZATION */..#ifn
2da08 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2da09 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 2f  VIRTUALTABLE.  /
2da0a 2a 20 41 64 64 20 61 20 57 4f 5f 4d 41 54 43 48  * Add a WO_MATCH
2da0b 20 61 75 78 69 6c 69 61 72 79 20 74 65 72 6d 20   auxiliary term 
2da0c 74 6f 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  to the constrain
2da0d 74 20 73 65 74 20 69 66 20 74 68 65 0a 20 20 2a  t set if the.  *
2da0e 2a 20 63 75 72 72 65 6e 74 20 65 78 70 72 65 73  * current expres
2da0f 73 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66  sion is of the f
2da10 6f 72 6d 3a 20 20 63 6f 6c 75 6d 6e 20 4d 41 54  orm:  column MAT
2da11 43 48 20 65 78 70 72 2e 0a 20 20 2a 2a 20 54 68  CH expr..  ** Th
2da12 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  is information i
2da13 73 20 75 73 65 64 20 62 79 20 74 68 65 20 78 42  s used by the xB
2da14 65 73 74 49 6e 64 65 78 20 6d 65 74 68 6f 64 73  estIndex methods
2da15 20 6f 66 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c   of.  ** virtual
2da16 20 74 61 62 6c 65 73 2e 20 20 54 68 65 20 6e 61   tables.  The na
2da17 74 69 76 65 20 71 75 65 72 79 20 6f 70 74 69 6d  tive query optim
2da18 69 7a 65 72 20 64 6f 65 73 20 6e 6f 74 20 61 74  izer does not at
2da19 74 65 6d 70 74 0a 20 20 2a 2a 20 74 6f 20 64 6f  tempt.  ** to do
2da1a 20 61 6e 79 74 68 69 6e 67 20 77 69 74 68 20 4d   anything with M
2da1b 41 54 43 48 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  ATCH functions..
2da1c 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 4d 61 74    */.  if( isMat
2da1d 63 68 4f 66 43 6f 6c 75 6d 6e 28 70 45 78 70 72  chOfColumn(pExpr
2da1e 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 64 78  ) ){.    int idx
2da1f 4e 65 77 3b 0a 20 20 20 20 45 78 70 72 20 2a 70  New;.    Expr *p
2da20 52 69 67 68 74 2c 20 2a 70 4c 65 66 74 3b 0a 20  Right, *pLeft;. 
2da21 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4e     WhereTerm *pN
2da22 65 77 54 65 72 6d 3b 0a 20 20 20 20 42 69 74 6d  ewTerm;.    Bitm
2da23 61 73 6b 20 70 72 65 72 65 71 43 6f 6c 75 6d 6e  ask prereqColumn
2da24 2c 20 70 72 65 72 65 71 45 78 70 72 3b 0a 0a 20  , prereqExpr;.. 
2da25 20 20 20 70 52 69 67 68 74 20 3d 20 70 45 78 70     pRight = pExp
2da26 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 30 5d  r->x.pList->a[0]
2da27 2e 70 45 78 70 72 3b 0a 20 20 20 20 70 4c 65 66  .pExpr;.    pLef
2da28 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  t = pExpr->x.pLi
2da29 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a  st->a[1].pExpr;.
2da2a 20 20 20 20 70 72 65 72 65 71 45 78 70 72 20 3d      prereqExpr =
2da2b 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28   exprTableUsage(
2da2c 70 4d 61 73 6b 53 65 74 2c 20 70 52 69 67 68 74  pMaskSet, pRight
2da2d 29 3b 0a 20 20 20 20 70 72 65 72 65 71 43 6f 6c  );.    prereqCol
2da2e 75 6d 6e 20 3d 20 65 78 70 72 54 61 62 6c 65 55  umn = exprTableU
2da2f 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70  sage(pMaskSet, p
2da30 4c 65 66 74 29 3b 0a 20 20 20 20 69 66 28 20 28  Left);.    if( (
2da31 70 72 65 72 65 71 45 78 70 72 20 26 20 70 72 65  prereqExpr & pre
2da32 72 65 71 43 6f 6c 75 6d 6e 29 3d 3d 30 20 29 7b  reqColumn)==0 ){
2da33 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e 65  .      Expr *pNe
2da34 77 45 78 70 72 3b 0a 20 20 20 20 20 20 70 4e 65  wExpr;.      pNe
2da35 77 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50  wExpr = sqlite3P
2da36 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
2da37 4d 41 54 43 48 2c 20 0a 20 20 20 20 20 20 20 20  MATCH, .        
2da38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2da39 20 20 20 20 20 20 30 2c 20 73 71 6c 69 74 65 33        0, sqlite3
2da3a 45 78 70 72 44 75 70 28 64 62 2c 20 70 52 69 67  ExprDup(db, pRig
2da3b 68 74 2c 20 30 29 2c 20 30 29 3b 0a 20 20 20 20  ht, 0), 0);.    
2da3c 20 20 69 64 78 4e 65 77 20 3d 20 77 68 65 72 65    idxNew = where
2da3d 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43  ClauseInsert(pWC
2da3e 2c 20 70 4e 65 77 45 78 70 72 2c 20 54 45 52 4d  , pNewExpr, TERM
2da3f 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59  _VIRTUAL|TERM_DY
2da40 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 74 65  NAMIC);.      te
2da41 73 74 63 61 73 65 28 20 69 64 78 4e 65 77 3d 3d  stcase( idxNew==
2da42 30 20 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 54  0 );.      pNewT
2da43 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64  erm = &pWC->a[id
2da44 78 4e 65 77 5d 3b 0a 20 20 20 20 20 20 70 4e 65  xNew];.      pNe
2da45 77 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67  wTerm->prereqRig
2da46 68 74 20 3d 20 70 72 65 72 65 71 45 78 70 72 3b  ht = prereqExpr;
2da47 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d  .      pNewTerm-
2da48 3e 6c 65 66 74 43 75 72 73 6f 72 20 3d 20 70 4c  >leftCursor = pL
2da49 65 66 74 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20  eft->iTable;.   
2da4a 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 75 2e 6c     pNewTerm->u.l
2da4b 65 66 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65 66  eftColumn = pLef
2da4c 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  t->iColumn;.    
2da4d 20 20 70 4e 65 77 54 65 72 6d 2d 3e 65 4f 70 65    pNewTerm->eOpe
2da4e 72 61 74 6f 72 20 3d 20 57 4f 5f 4d 41 54 43 48  rator = WO_MATCH
2da4f 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d  ;.      pNewTerm
2da50 2d 3e 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54  ->iParent = idxT
2da51 65 72 6d 3b 0a 20 20 20 20 20 20 70 54 65 72 6d  erm;.      pTerm
2da52 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65   = &pWC->a[idxTe
2da53 72 6d 5d 3b 0a 20 20 20 20 20 20 70 54 65 72 6d  rm];.      pTerm
2da54 2d 3e 6e 43 68 69 6c 64 20 3d 20 31 3b 0a 20 20  ->nChild = 1;.  
2da55 20 20 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61      pTerm->wtFla
2da56 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 50 49 45  gs |= TERM_COPIE
2da57 44 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72  D;.      pNewTer
2da58 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 20 3d 20 70  m->prereqAll = p
2da59 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 3b  Term->prereqAll;
2da5a 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
2da5b 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2da5c 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
2da5d 0a 0a 20 20 2f 2a 20 50 72 65 76 65 6e 74 20 4f  ..  /* Prevent O
2da5e 4e 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 6f  N clause terms o
2da5f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 20 66 72  f a LEFT JOIN fr
2da60 6f 6d 20 62 65 69 6e 67 20 75 73 65 64 20 74 6f  om being used to
2da61 20 64 72 69 76 65 0a 20 20 2a 2a 20 61 6e 20 69   drive.  ** an i
2da62 6e 64 65 78 20 66 6f 72 20 74 61 62 6c 65 73 20  ndex for tables 
2da63 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20 74  to the left of t
2da64 68 65 20 6a 6f 69 6e 2e 0a 20 20 2a 2f 0a 20 20  he join..  */.  
2da65 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67  pTerm->prereqRig
2da66 68 74 20 7c 3d 20 65 78 74 72 61 52 69 67 68 74  ht |= extraRight
2da67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
2da68 6e 20 54 52 55 45 20 69 66 20 61 6e 79 20 6f 66  n TRUE if any of
2da69 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73   the expressions
2da6a 20 69 6e 20 70 4c 69 73 74 2d 3e 61 5b 69 46 69   in pList->a[iFi
2da6b 72 73 74 2e 2e 2e 5d 20 63 6f 6e 74 61 69 6e 0a  rst...] contain.
2da6c 2a 2a 20 61 20 72 65 66 65 72 65 6e 63 65 20 74  ** a reference t
2da6d 6f 20 61 6e 79 20 74 61 62 6c 65 20 6f 74 68 65  o any table othe
2da6e 72 20 74 68 61 6e 20 74 68 65 20 69 42 61 73 65  r than the iBase
2da6f 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69   table..*/.stati
2da70 63 20 69 6e 74 20 72 65 66 65 72 65 6e 63 65 73  c int references
2da71 4f 74 68 65 72 54 61 62 6c 65 73 28 0a 20 20 45  OtherTables(.  E
2da72 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20  xprList *pList, 
2da73 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 61 72           /* Sear
2da74 63 68 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69  ch expressions i
2da75 6e 20 74 68 73 20 6c 69 73 74 20 2a 2f 0a 20 20  n ths list */.  
2da76 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d  WhereMaskSet *pM
2da77 61 73 6b 53 65 74 2c 20 20 20 2f 2a 20 4d 61 70  askSet,   /* Map
2da78 70 69 6e 67 20 66 72 6f 6d 20 74 61 62 6c 65 73  ping from tables
2da79 20 74 6f 20 62 69 74 6d 61 70 73 20 2a 2f 0a 20   to bitmaps */. 
2da7a 20 69 6e 74 20 69 46 69 72 73 74 2c 20 20 20 20   int iFirst,    
2da7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65             /* Be
2da7c 20 73 65 61 72 63 68 69 6e 67 20 77 69 74 68 20   searching with 
2da7d 74 68 65 20 69 46 69 72 73 74 2d 74 68 20 65 78  the iFirst-th ex
2da7e 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e  pression */.  in
2da7f 74 20 69 42 61 73 65 20 20 20 20 20 20 20 20 20  t iBase         
2da80 20 20 20 20 20 20 20 20 2f 2a 20 49 67 6e 6f 72          /* Ignor
2da81 65 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  e references to 
2da82 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 29 7b  this table */.){
2da83 0a 20 20 42 69 74 6d 61 73 6b 20 61 6c 6c 6f 77  .  Bitmask allow
2da84 65 64 20 3d 20 7e 67 65 74 4d 61 73 6b 28 70 4d  ed = ~getMask(pM
2da85 61 73 6b 53 65 74 2c 20 69 42 61 73 65 29 3b 0a  askSet, iBase);.
2da86 20 20 77 68 69 6c 65 28 20 69 46 69 72 73 74 3c    while( iFirst<
2da87 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a  pList->nExpr ){.
2da88 20 20 20 20 69 66 28 20 28 65 78 70 72 54 61 62      if( (exprTab
2da89 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
2da8a 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 46 69 72 73  , pList->a[iFirs
2da8b 74 2b 2b 5d 2e 70 45 78 70 72 29 26 61 6c 6c 6f  t++].pExpr)&allo
2da8c 77 65 64 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  wed)!=0 ){.     
2da8d 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
2da8e 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
2da8f 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  .}.../*.** This 
2da90 72 6f 75 74 69 6e 65 20 64 65 63 69 64 65 73 20  routine decides 
2da91 69 66 20 70 49 64 78 20 63 61 6e 20 62 65 20 75  if pIdx can be u
2da92 73 65 64 20 74 6f 20 73 61 74 69 73 66 79 20 74  sed to satisfy t
2da93 68 65 20 4f 52 44 45 52 20 42 59 0a 2a 2a 20 63  he ORDER BY.** c
2da94 6c 61 75 73 65 2e 20 20 49 66 20 69 74 20 63 61  lause.  If it ca
2da95 6e 2c 20 69 74 20 72 65 74 75 72 6e 73 20 31 2e  n, it returns 1.
2da96 20 20 49 66 20 70 49 64 78 20 63 61 6e 6e 6f 74    If pIdx cannot
2da97 20 73 61 74 69 73 66 79 20 74 68 65 0a 2a 2a 20   satisfy the.** 
2da98 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c  ORDER BY clause,
2da99 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
2da9a 74 75 72 6e 73 20 30 2e 0a 2a 2a 0a 2a 2a 20 70  turns 0..**.** p
2da9b 4f 72 64 65 72 42 79 20 69 73 20 61 6e 20 4f 52  OrderBy is an OR
2da9c 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 66 72  DER BY clause fr
2da9d 6f 6d 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  om a SELECT stat
2da9e 65 6d 65 6e 74 2e 20 20 70 54 61 62 20 69 73 20  ement.  pTab is 
2da9f 74 68 65 0a 2a 2a 20 6c 65 66 74 2d 6d 6f 73 74  the.** left-most
2daa0 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52   table in the FR
2daa1 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 61  OM clause of tha
2daa2 74 20 73 61 6d 65 20 53 45 4c 45 43 54 20 73 74  t same SELECT st
2daa3 61 74 65 6d 65 6e 74 20 61 6e 64 0a 2a 2a 20 74  atement and.** t
2daa4 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 20 63  he table has a c
2daa5 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20  ursor number of 
2daa6 22 62 61 73 65 22 2e 20 20 70 49 64 78 20 69 73  "base".  pIdx is
2daa7 20 61 6e 20 69 6e 64 65 78 20 6f 6e 20 70 54 61   an index on pTa
2daa8 62 2e 0a 2a 2a 0a 2a 2a 20 6e 45 71 43 6f 6c 20  b..**.** nEqCol 
2daa9 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
2daaa 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 70 49 64 78   columns of pIdx
2daab 20 74 68 61 74 20 61 72 65 20 75 73 65 64 20 61   that are used a
2daac 73 20 65 71 75 61 6c 69 74 79 0a 2a 2a 20 63 6f  s equality.** co
2daad 6e 73 74 72 61 69 6e 74 73 2e 20 20 41 6e 79 20  nstraints.  Any 
2daae 6f 66 20 74 68 65 73 65 20 63 6f 6c 75 6d 6e 73  of these columns
2daaf 20 6d 61 79 20 62 65 20 6d 69 73 73 69 6e 67 20   may be missing 
2dab0 66 72 6f 6d 20 74 68 65 20 4f 52 44 45 52 20 42  from the ORDER B
2dab1 59 0a 2a 2a 20 63 6c 61 75 73 65 20 61 6e 64 20  Y.** clause and 
2dab2 74 68 65 20 6d 61 74 63 68 20 63 61 6e 20 73 74  the match can st
2dab3 69 6c 6c 20 62 65 20 61 20 73 75 63 63 65 73 73  ill be a success
2dab4 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 74 65 72 6d  ..**.** All term
2dab5 73 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42  s of the ORDER B
2dab6 59 20 74 68 61 74 20 6d 61 74 63 68 20 61 67 61  Y that match aga
2dab7 69 6e 73 74 20 74 68 65 20 69 6e 64 65 78 20 6d  inst the index m
2dab8 75 73 74 20 62 65 20 65 69 74 68 65 72 0a 2a 2a  ust be either.**
2dab9 20 41 53 43 20 6f 72 20 44 45 53 43 2e 20 20 28   ASC or DESC.  (
2daba 54 65 72 6d 73 20 6f 66 20 74 68 65 20 4f 52 44  Terms of the ORD
2dabb 45 52 20 42 59 20 63 6c 61 75 73 65 20 70 61 73  ER BY clause pas
2dabc 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 55  t the end of a U
2dabd 4e 49 51 55 45 0a 2a 2a 20 69 6e 64 65 78 20 64  NIQUE.** index d
2dabe 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 73 61  o not need to sa
2dabf 74 69 73 66 79 20 74 68 69 73 20 63 6f 6e 73 74  tisfy this const
2dac0 72 61 69 6e 74 2e 29 20 20 54 68 65 20 2a 70 62  raint.)  The *pb
2dac1 52 65 76 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20  Rev value is.** 
2dac2 73 65 74 20 74 6f 20 31 20 69 66 20 74 68 65 20  set to 1 if the 
2dac3 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2dac4 69 73 20 61 6c 6c 20 44 45 53 43 20 61 6e 64 20  is all DESC and 
2dac5 69 74 20 69 73 20 73 65 74 20 74 6f 20 30 20 69  it is set to 0 i
2dac6 66 0a 2a 2a 20 74 68 65 20 4f 52 44 45 52 20 42  f.** the ORDER B
2dac7 59 20 63 6c 61 75 73 65 20 69 73 20 61 6c 6c 20  Y clause is all 
2dac8 41 53 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ASC..*/.static i
2dac9 6e 74 20 69 73 53 6f 72 74 69 6e 67 49 6e 64 65  nt isSortingInde
2daca 78 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  x(.  Parse *pPar
2dacb 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  se,          /* 
2dacc 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
2dacd 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73 6b 53 65  */.  WhereMaskSe
2dace 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 2f 2a 20  t *pMaskSet, /* 
2dacf 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 74 61 62  Mapping from tab
2dad0 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  le cursor number
2dad1 73 20 74 6f 20 62 69 74 6d 61 70 73 20 2a 2f 0a  s to bitmaps */.
2dad2 20 20 49 6e 64 65 78 20 2a 70 49 64 78 2c 20 20    Index *pIdx,  
2dad3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2dad4 20 69 6e 64 65 78 20 77 65 20 61 72 65 20 74 65   index we are te
2dad5 73 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62  sting */.  int b
2dad6 61 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ase,            
2dad7 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d     /* Cursor num
2dad8 62 65 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c  ber for the tabl
2dad9 65 20 74 6f 20 62 65 20 73 6f 72 74 65 64 20 2a  e to be sorted *
2dada 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f  /.  ExprList *pO
2dadb 72 64 65 72 42 79 2c 20 20 20 20 20 2f 2a 20 54  rderBy,     /* T
2dadc 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2dadd 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 71 43  se */.  int nEqC
2dade 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ol,             
2dadf 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 6e 64  /* Number of ind
2dae0 65 78 20 63 6f 6c 75 6d 6e 73 20 77 69 74 68 20  ex columns with 
2dae1 3d 3d 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a  == constraints *
2dae2 2f 0a 20 20 69 6e 74 20 2a 70 62 52 65 76 20 20  /.  int *pbRev  
2dae3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
2dae4 65 74 20 74 6f 20 31 20 69 66 20 4f 52 44 45 52  et to 1 if ORDER
2dae5 20 42 59 20 69 73 20 44 45 53 43 20 2a 2f 0a 29   BY is DESC */.)
2dae6 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20  {.  int i, j;   
2dae7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dae8 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2dae9 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 73 6f  ters */.  int so
2daea 72 74 4f 72 64 65 72 20 3d 20 30 3b 20 20 20 20  rtOrder = 0;    
2daeb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 58 4f 52            /* XOR
2daec 20 6f 66 20 69 6e 64 65 78 20 61 6e 64 20 4f 52   of index and OR
2daed 44 45 52 20 42 59 20 73 6f 72 74 20 64 69 72 65  DER BY sort dire
2daee 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e  ction */.  int n
2daef 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20  Term;           
2daf0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
2daf1 6d 62 65 72 20 6f 66 20 4f 52 44 45 52 20 42 59  mber of ORDER BY
2daf2 20 74 65 72 6d 73 20 2a 2f 0a 20 20 73 74 72 75   terms */.  stru
2daf3 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d  ct ExprList_item
2daf4 20 2a 70 54 65 72 6d 3b 20 20 20 20 2f 2a 20 41   *pTerm;    /* A
2daf5 20 74 65 72 6d 20 6f 66 20 74 68 65 20 4f 52 44   term of the ORD
2daf6 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a  ER BY clause */.
2daf7 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2daf8 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 61  pParse->db;..  a
2daf9 73 73 65 72 74 28 20 70 4f 72 64 65 72 42 79 21  ssert( pOrderBy!
2dafa 3d 30 20 29 3b 0a 20 20 6e 54 65 72 6d 20 3d 20  =0 );.  nTerm = 
2dafb 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b  pOrderBy->nExpr;
2dafc 0a 20 20 61 73 73 65 72 74 28 20 6e 54 65 72 6d  .  assert( nTerm
2dafd 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 41 72 67 75  >0 );..  /* Argu
2dafe 6d 65 6e 74 20 70 49 64 78 20 6d 75 73 74 20 65  ment pIdx must e
2daff 69 74 68 65 72 20 70 6f 69 6e 74 20 74 6f 20 61  ither point to a
2db00 20 27 72 65 61 6c 27 20 6e 61 6d 65 64 20 69 6e   'real' named in
2db01 64 65 78 20 73 74 72 75 63 74 75 72 65 2c 20 0a  dex structure, .
2db02 20 20 2a 2a 20 6f 72 20 61 6e 20 69 6e 64 65 78    ** or an index
2db03 20 73 74 72 75 63 74 75 72 65 20 61 6c 6c 6f 63   structure alloc
2db04 61 74 65 64 20 6f 6e 20 74 68 65 20 73 74 61 63  ated on the stac
2db05 6b 20 62 79 20 62 65 73 74 42 74 72 65 65 49 6e  k by bestBtreeIn
2db06 64 65 78 28 29 20 74 6f 0a 20 20 2a 2a 20 72 65  dex() to.  ** re
2db07 70 72 65 73 65 6e 74 20 74 68 65 20 72 6f 77 69  present the rowi
2db08 64 20 69 6e 64 65 78 20 74 68 61 74 20 69 73 20  d index that is 
2db09 70 61 72 74 20 6f 66 20 65 76 65 72 79 20 74 61  part of every ta
2db0a 62 6c 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72  ble.  */.  asser
2db0b 74 28 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 20 7c  t( pIdx->zName |
2db0c 7c 20 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  | (pIdx->nColumn
2db0d 3d 3d 31 20 26 26 20 70 49 64 78 2d 3e 61 69 43  ==1 && pIdx->aiC
2db0e 6f 6c 75 6d 6e 5b 30 5d 3d 3d 2d 31 29 20 29 3b  olumn[0]==-1) );
2db0f 0a 0a 20 20 2f 2a 20 4d 61 74 63 68 20 74 65 72  ..  /* Match ter
2db10 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20  ms of the ORDER 
2db11 42 59 20 63 6c 61 75 73 65 20 61 67 61 69 6e 73  BY clause agains
2db12 74 20 63 6f 6c 75 6d 6e 73 20 6f 66 0a 20 20 2a  t columns of.  *
2db13 2a 20 74 68 65 20 69 6e 64 65 78 2e 0a 20 20 2a  * the index..  *
2db14 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74  *.  ** Note that
2db15 20 69 6e 64 69 63 65 73 20 68 61 76 65 20 70 49   indices have pI
2db16 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 72 65 67 75  dx->nColumn regu
2db17 6c 61 72 20 63 6f 6c 75 6d 6e 73 20 70 6c 75 73  lar columns plus
2db18 0a 20 20 2a 2a 20 6f 6e 65 20 61 64 64 69 74 69  .  ** one additi
2db19 6f 6e 61 6c 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74  onal column cont
2db1a 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 69 64  aining the rowid
2db1b 2e 20 20 54 68 65 20 72 6f 77 69 64 20 63 6f 6c  .  The rowid col
2db1c 75 6d 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  umn.  ** of the 
2db1d 69 6e 64 65 78 20 69 73 20 61 6c 73 6f 20 61 6c  index is also al
2db1e 6c 6f 77 65 64 20 74 6f 20 6d 61 74 63 68 20 61  lowed to match a
2db1f 67 61 69 6e 73 74 20 74 68 65 20 4f 52 44 45 52  gainst the ORDER
2db20 20 42 59 0a 20 20 2a 2a 20 63 6c 61 75 73 65 2e   BY.  ** clause.
2db21 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 6a 3d  .  */.  for(i=j=
2db22 30 2c 20 70 54 65 72 6d 3d 70 4f 72 64 65 72 42  0, pTerm=pOrderB
2db23 79 2d 3e 61 3b 20 6a 3c 6e 54 65 72 6d 20 26 26  y->a; j<nTerm &&
2db24 20 69 3c 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d   i<=pIdx->nColum
2db25 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70  n; i++){.    Exp
2db26 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20  r *pExpr;       
2db27 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f  /* The expressio
2db28 6e 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42  n of the ORDER B
2db29 59 20 70 54 65 72 6d 20 2a 2f 0a 20 20 20 20 43  Y pTerm */.    C
2db2a 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20  ollSeq *pColl;  
2db2b 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69    /* The collati
2db2c 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 66 20 70  ng sequence of p
2db2d 45 78 70 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  Expr */.    int 
2db2e 74 65 72 6d 53 6f 72 74 4f 72 64 65 72 3b 20 2f  termSortOrder; /
2db2f 2a 20 53 6f 72 74 20 6f 72 64 65 72 20 66 6f 72  * Sort order for
2db30 20 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20 20   this term */.  
2db31 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20    int iColumn;  
2db32 20 20 20 20 20 2f 2a 20 54 68 65 20 69 2d 74 68       /* The i-th
2db33 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 69   column of the i
2db34 6e 64 65 78 2e 20 20 2d 31 20 66 6f 72 20 72 6f  ndex.  -1 for ro
2db35 77 69 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  wid */.    int i
2db36 53 6f 72 74 4f 72 64 65 72 3b 20 20 20 20 2f 2a  SortOrder;    /*
2db37 20 31 20 66 6f 72 20 44 45 53 43 2c 20 30 20 66   1 for DESC, 0 f
2db38 6f 72 20 41 53 43 20 6f 6e 20 74 68 65 20 69 2d  or ASC on the i-
2db39 74 68 20 69 6e 64 65 78 20 74 65 72 6d 20 2a 2f  th index term */
2db3a 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
2db3b 2a 7a 43 6f 6c 6c 3b 20 2f 2a 20 4e 61 6d 65 20  *zColl; /* Name 
2db3c 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67  of the collating
2db3d 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 69 2d   sequence for i-
2db3e 74 68 20 69 6e 64 65 78 20 74 65 72 6d 20 2a 2f  th index term */
2db3f 0a 0a 20 20 20 20 70 45 78 70 72 20 3d 20 70 54  ..    pExpr = pT
2db40 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20  erm->pExpr;.    
2db41 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  if( pExpr->op!=T
2db42 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 45 78 70  K_COLUMN || pExp
2db43 72 2d 3e 69 54 61 62 6c 65 21 3d 62 61 73 65 20  r->iTable!=base 
2db44 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 6e 20  ){.      /* Can 
2db45 6e 6f 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78  not use an index
2db46 20 73 6f 72 74 20 6f 6e 20 61 6e 79 74 68 69 6e   sort on anythin
2db47 67 20 74 68 61 74 20 69 73 20 6e 6f 74 20 61 20  g that is not a 
2db48 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 0a 20 20  column in the.  
2db49 20 20 20 20 2a 2a 20 6c 65 66 74 2d 6d 6f 73 74      ** left-most
2db4a 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20 46 52   table of the FR
2db4b 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20  OM clause */.   
2db4c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2db4d 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69      pColl = sqli
2db4e 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70  te3ExprCollSeq(p
2db4f 50 61 72 73 65 2c 20 70 45 78 70 72 29 3b 0a 20  Parse, pExpr);. 
2db50 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29 7b     if( !pColl ){
2db51 0a 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 64  .      pColl = d
2db52 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20  b->pDfltColl;.  
2db53 20 20 7d 0a 20 20 20 20 69 66 28 20 70 49 64 78    }.    if( pIdx
2db54 2d 3e 7a 4e 61 6d 65 20 26 26 20 69 3c 70 49 64  ->zName && i<pId
2db55 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20  x->nColumn ){.  
2db56 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 70 49      iColumn = pI
2db57 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b  dx->aiColumn[i];
2db58 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 75  .      if( iColu
2db59 6d 6e 3d 3d 70 49 64 78 2d 3e 70 54 61 62 6c 65  mn==pIdx->pTable
2db5a 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20  ->iPKey ){.     
2db5b 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b     iColumn = -1;
2db5c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
2db5d 53 6f 72 74 4f 72 64 65 72 20 3d 20 70 49 64 78  SortOrder = pIdx
2db5e 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 3b  ->aSortOrder[i];
2db5f 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70  .      zColl = p
2db60 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a  Idx->azColl[i];.
2db61 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2db62 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20   iColumn = -1;. 
2db63 20 20 20 20 20 69 53 6f 72 74 4f 72 64 65 72 20       iSortOrder 
2db64 3d 20 30 3b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c  = 0;.      zColl
2db65 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b   = pColl->zName;
2db66 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
2db67 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 69  Expr->iColumn!=i
2db68 43 6f 6c 75 6d 6e 20 7c 7c 20 73 71 6c 69 74 65  Column || sqlite
2db69 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 6c 2d 3e  3StrICmp(pColl->
2db6a 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 6c 29 20 29 7b  zName, zColl) ){
2db6b 0a 20 20 20 20 20 20 2f 2a 20 54 65 72 6d 20 6a  .      /* Term j
2db6c 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59   of the ORDER BY
2db6d 20 63 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f 74   clause does not
2db6e 20 6d 61 74 63 68 20 63 6f 6c 75 6d 6e 20 69 20   match column i 
2db6f 6f 66 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a  of the index */.
2db70 20 20 20 20 20 20 69 66 28 20 69 3c 6e 45 71 43        if( i<nEqC
2db71 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  ol ){.        /*
2db72 20 49 66 20 61 6e 20 69 6e 64 65 78 20 63 6f 6c   If an index col
2db73 75 6d 6e 20 74 68 61 74 20 69 73 20 63 6f 6e 73  umn that is cons
2db74 74 72 61 69 6e 65 64 20 62 79 20 3d 3d 20 66 61  trained by == fa
2db75 69 6c 73 20 74 6f 20 6d 61 74 63 68 20 61 6e 0a  ils to match an.
2db76 20 20 20 20 20 20 20 20 2a 2a 20 4f 52 44 45 52          ** ORDER
2db77 20 42 59 20 74 65 72 6d 2c 20 74 68 61 74 20 69   BY term, that i
2db78 73 20 4f 4b 2e 20 20 4a 75 73 74 20 69 67 6e 6f  s OK.  Just igno
2db79 72 65 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 6f  re that column o
2db7a 66 20 74 68 65 20 69 6e 64 65 78 0a 20 20 20 20  f the index.    
2db7b 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 63      */.        c
2db7c 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d  ontinue;.      }
2db7d 65 6c 73 65 20 69 66 28 20 69 3d 3d 70 49 64 78  else if( i==pIdx
2db7e 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->nColumn ){.   
2db7f 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 63 6f       /* Index co
2db80 6c 75 6d 6e 20 69 20 69 73 20 74 68 65 20 72 6f  lumn i is the ro
2db81 77 69 64 2e 20 20 41 6c 6c 20 6f 74 68 65 72 20  wid.  All other 
2db82 74 65 72 6d 73 20 6d 61 74 63 68 2e 20 2a 2f 0a  terms match. */.
2db83 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2db84 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2db85 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 69 6e 64      /* If an ind
2db86 65 78 20 63 6f 6c 75 6d 6e 20 66 61 69 6c 73 20  ex column fails 
2db87 74 6f 20 6d 61 74 63 68 20 61 6e 64 20 69 73 20  to match and is 
2db88 6e 6f 74 20 63 6f 6e 73 74 72 61 69 6e 65 64 20  not constrained 
2db89 62 79 20 3d 3d 0a 20 20 20 20 20 20 20 20 2a 2a  by ==.        **
2db8a 20 74 68 65 6e 20 74 68 65 20 69 6e 64 65 78 20   then the index 
2db8b 63 61 6e 6e 6f 74 20 73 61 74 69 73 66 79 20 74  cannot satisfy t
2db8c 68 65 20 4f 52 44 45 52 20 42 59 20 63 6f 6e 73  he ORDER BY cons
2db8d 74 72 61 69 6e 74 2e 0a 20 20 20 20 20 20 20 20  traint..        
2db8e 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  */.        retur
2db8f 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 0;.      }.   
2db90 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
2db91 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 21  Idx->aSortOrder!
2db92 3d 30 20 7c 7c 20 69 43 6f 6c 75 6d 6e 3d 3d 2d  =0 || iColumn==-
2db93 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  1 );.    assert(
2db94 20 70 54 65 72 6d 2d 3e 73 6f 72 74 4f 72 64 65   pTerm->sortOrde
2db95 72 3d 3d 30 20 7c 7c 20 70 54 65 72 6d 2d 3e 73  r==0 || pTerm->s
2db96 6f 72 74 4f 72 64 65 72 3d 3d 31 20 29 3b 0a 20  ortOrder==1 );. 
2db97 20 20 20 61 73 73 65 72 74 28 20 69 53 6f 72 74     assert( iSort
2db98 4f 72 64 65 72 3d 3d 30 20 7c 7c 20 69 53 6f 72  Order==0 || iSor
2db99 74 4f 72 64 65 72 3d 3d 31 20 29 3b 0a 20 20 20  tOrder==1 );.   
2db9a 20 74 65 72 6d 53 6f 72 74 4f 72 64 65 72 20 3d   termSortOrder =
2db9b 20 69 53 6f 72 74 4f 72 64 65 72 20 5e 20 70 54   iSortOrder ^ pT
2db9c 65 72 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b 0a  erm->sortOrder;.
2db9d 20 20 20 20 69 66 28 20 69 3e 6e 45 71 43 6f 6c      if( i>nEqCol
2db9e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 74 65   ){.      if( te
2db9f 72 6d 53 6f 72 74 4f 72 64 65 72 21 3d 73 6f 72  rmSortOrder!=sor
2dba0 74 4f 72 64 65 72 20 29 7b 0a 20 20 20 20 20 20  tOrder ){.      
2dba1 20 20 2f 2a 20 49 6e 64 69 63 65 73 20 63 61 6e    /* Indices can
2dba2 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 69 66   only be used if
2dba3 20 61 6c 6c 20 4f 52 44 45 52 20 42 59 20 74 65   all ORDER BY te
2dba4 72 6d 73 20 70 61 73 74 20 74 68 65 0a 20 20 20  rms past the.   
2dba5 20 20 20 20 20 2a 2a 20 65 71 75 61 6c 69 74 79       ** equality
2dba6 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72 65   constraints are
2dba7 20 61 6c 6c 20 65 69 74 68 65 72 20 44 45 53 43   all either DESC
2dba8 20 6f 72 20 41 53 43 2e 20 2a 2f 0a 20 20 20 20   or ASC. */.    
2dba9 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
2dbaa 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
2dbab 0a 20 20 20 20 20 20 73 6f 72 74 4f 72 64 65 72  .      sortOrder
2dbac 20 3d 20 74 65 72 6d 53 6f 72 74 4f 72 64 65 72   = termSortOrder
2dbad 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6a 2b 2b 3b  ;.    }.    j++;
2dbae 0a 20 20 20 20 70 54 65 72 6d 2b 2b 3b 0a 20 20  .    pTerm++;.  
2dbaf 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3c 30 20    if( iColumn<0 
2dbb0 26 26 20 21 72 65 66 65 72 65 6e 63 65 73 4f 74  && !referencesOt
2dbb1 68 65 72 54 61 62 6c 65 73 28 70 4f 72 64 65 72  herTables(pOrder
2dbb2 42 79 2c 20 70 4d 61 73 6b 53 65 74 2c 20 6a 2c  By, pMaskSet, j,
2dbb3 20 62 61 73 65 29 20 29 7b 0a 20 20 20 20 20 20   base) ){.      
2dbb4 2f 2a 20 49 66 20 74 68 65 20 69 6e 64 65 78 65  /* If the indexe
2dbb5 64 20 63 6f 6c 75 6d 6e 20 69 73 20 74 68 65 20  d column is the 
2dbb6 70 72 69 6d 61 72 79 20 6b 65 79 20 61 6e 64 20  primary key and 
2dbb7 65 76 65 72 79 74 68 69 6e 67 20 6d 61 74 63 68  everything match
2dbb8 65 73 0a 20 20 20 20 20 20 2a 2a 20 73 6f 20 66  es.      ** so f
2dbb9 61 72 20 61 6e 64 20 6e 6f 6e 65 20 6f 66 20 74  ar and none of t
2dbba 68 65 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  he ORDER BY term
2dbbb 73 20 74 6f 20 74 68 65 20 72 69 67 68 74 20 72  s to the right r
2dbbc 65 66 65 72 65 6e 63 65 20 6f 74 68 65 72 0a 20  eference other. 
2dbbd 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 73 20 69       ** tables i
2dbbe 6e 20 74 68 65 20 6a 6f 69 6e 2c 20 74 68 65 6e  n the join, then
2dbbf 20 77 65 20 61 72 65 20 61 73 73 75 72 65 64 20   we are assured 
2dbc0 74 68 61 74 20 74 68 65 20 69 6e 64 65 78 20 63  that the index c
2dbc1 61 6e 20 62 65 20 75 73 65 64 20 0a 20 20 20 20  an be used .    
2dbc2 20 20 2a 2a 20 74 6f 20 73 6f 72 74 20 62 65 63    ** to sort bec
2dbc3 61 75 73 65 20 74 68 65 20 70 72 69 6d 61 72 79  ause the primary
2dbc4 20 6b 65 79 20 69 73 20 75 6e 69 71 75 65 20 61   key is unique a
2dbc5 6e 64 20 73 6f 20 6e 6f 6e 65 20 6f 66 20 74 68  nd so none of th
2dbc6 65 20 6f 74 68 65 72 0a 20 20 20 20 20 20 2a 2a  e other.      **
2dbc7 20 63 6f 6c 75 6d 6e 73 20 77 69 6c 6c 20 6d 61   columns will ma
2dbc8 6b 65 20 61 6e 79 20 64 69 66 66 65 72 65 6e 63  ke any differenc
2dbc9 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  e.      */.     
2dbca 20 6a 20 3d 20 6e 54 65 72 6d 3b 0a 20 20 20 20   j = nTerm;.    
2dbcb 7d 0a 20 20 7d 0a 0a 20 20 2a 70 62 52 65 76 20  }.  }..  *pbRev 
2dbcc 3d 20 73 6f 72 74 4f 72 64 65 72 21 3d 30 3b 0a  = sortOrder!=0;.
2dbcd 20 20 69 66 28 20 6a 3e 3d 6e 54 65 72 6d 20 29    if( j>=nTerm )
2dbce 7b 0a 20 20 20 20 2f 2a 20 41 6c 6c 20 74 65 72  {.    /* All ter
2dbcf 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20  ms of the ORDER 
2dbd0 42 59 20 63 6c 61 75 73 65 20 61 72 65 20 63 6f  BY clause are co
2dbd1 76 65 72 65 64 20 62 79 20 74 68 69 73 20 69 6e  vered by this in
2dbd2 64 65 78 20 73 6f 0a 20 20 20 20 2a 2a 20 74 68  dex so.    ** th
2dbd3 69 73 20 69 6e 64 65 78 20 63 61 6e 20 62 65 20  is index can be 
2dbd4 75 73 65 64 20 66 6f 72 20 73 6f 72 74 69 6e 67  used for sorting
2dbd5 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  . */.    return 
2dbd6 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 64  1;.  }.  if( pId
2dbd7 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e  x->onError!=OE_N
2dbd8 6f 6e 65 20 26 26 20 69 3d 3d 70 49 64 78 2d 3e  one && i==pIdx->
2dbd9 6e 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 26 26  nColumn.      &&
2dbda 20 21 72 65 66 65 72 65 6e 63 65 73 4f 74 68 65   !referencesOthe
2dbdb 72 54 61 62 6c 65 73 28 70 4f 72 64 65 72 42 79  rTables(pOrderBy
2dbdc 2c 20 70 4d 61 73 6b 53 65 74 2c 20 6a 2c 20 62  , pMaskSet, j, b
2dbdd 61 73 65 29 20 29 7b 0a 20 20 20 20 2f 2a 20 41  ase) ){.    /* A
2dbde 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74 68 69 73  ll terms of this
2dbdf 20 69 6e 64 65 78 20 6d 61 74 63 68 20 73 6f 6d   index match som
2dbe0 65 20 70 72 65 66 69 78 20 6f 66 20 74 68 65 20  e prefix of the 
2dbe1 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 0a  ORDER BY clause.
2dbe2 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 69      ** and the i
2dbe3 6e 64 65 78 20 69 73 20 55 4e 49 51 55 45 20 61  ndex is UNIQUE a
2dbe4 6e 64 20 6e 6f 20 74 65 72 6d 73 20 6f 6e 20 74  nd no terms on t
2dbe5 68 65 20 74 61 69 6c 20 6f 66 20 74 68 65 20 4f  he tail of the O
2dbe6 52 44 45 52 20 42 59 0a 20 20 20 20 2a 2a 20 63  RDER BY.    ** c
2dbe7 6c 61 75 73 65 20 72 65 66 65 72 65 6e 63 65 20  lause reference 
2dbe8 6f 74 68 65 72 20 74 61 62 6c 65 73 20 69 6e 20  other tables in 
2dbe9 61 20 6a 6f 69 6e 2e 20 20 49 66 20 74 68 69 73  a join.  If this
2dbea 20 69 73 20 61 6c 6c 20 74 72 75 65 20 74 68 65   is all true the
2dbeb 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 6f 72 64  n.    ** the ord
2dbec 65 72 20 62 79 20 63 6c 61 75 73 65 20 69 73 20  er by clause is 
2dbed 73 75 70 65 72 66 6c 75 6f 75 73 2e 20 2a 2f 0a  superfluous. */.
2dbee 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2dbef 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
2dbf0 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 61  ./*.** Prepare a
2dbf1 20 63 72 75 64 65 20 65 73 74 69 6d 61 74 65 20   crude estimate 
2dbf2 6f 66 20 74 68 65 20 6c 6f 67 61 72 69 74 68 6d  of the logarithm
2dbf3 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 76 61   of the input va
2dbf4 6c 75 65 2e 0a 2a 2a 20 54 68 65 20 72 65 73 75  lue..** The resu
2dbf5 6c 74 73 20 6e 65 65 64 20 6e 6f 74 20 62 65 20  lts need not be 
2dbf6 65 78 61 63 74 2e 20 20 54 68 69 73 20 69 73 20  exact.  This is 
2dbf7 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72 20 65 73  only used for es
2dbf8 74 69 6d 61 74 69 6e 67 0a 2a 2a 20 74 68 65 20  timating.** the 
2dbf9 74 6f 74 61 6c 20 63 6f 73 74 20 6f 66 20 70 65  total cost of pe
2dbfa 72 66 6f 72 6d 69 6e 67 20 6f 70 65 72 61 74 69  rforming operati
2dbfb 6f 6e 73 20 77 69 74 68 20 4f 28 6c 6f 67 4e 29  ons with O(logN)
2dbfc 20 6f 72 20 4f 28 4e 6c 6f 67 4e 29 0a 2a 2a 20   or O(NlogN).** 
2dbfd 63 6f 6d 70 6c 65 78 69 74 79 2e 20 20 42 65 63  complexity.  Bec
2dbfe 61 75 73 65 20 4e 20 69 73 20 6a 75 73 74 20 61  ause N is just a
2dbff 20 67 75 65 73 73 2c 20 69 74 20 69 73 20 6e 6f   guess, it is no
2dc00 20 67 72 65 61 74 20 74 72 61 67 65 64 79 20 69   great tragedy i
2dc01 66 0a 2a 2a 20 6c 6f 67 4e 20 69 73 20 61 20 6c  f.** logN is a l
2dc02 69 74 74 6c 65 20 6f 66 66 2e 0a 2a 2f 0a 73 74  ittle off..*/.st
2dc03 61 74 69 63 20 64 6f 75 62 6c 65 20 65 73 74 4c  atic double estL
2dc04 6f 67 28 64 6f 75 62 6c 65 20 4e 29 7b 0a 20 20  og(double N){.  
2dc05 64 6f 75 62 6c 65 20 6c 6f 67 4e 20 3d 20 31 3b  double logN = 1;
2dc06 0a 20 20 64 6f 75 62 6c 65 20 78 20 3d 20 31 30  .  double x = 10
2dc07 3b 0a 20 20 77 68 69 6c 65 28 20 4e 3e 78 20 29  ;.  while( N>x )
2dc08 7b 0a 20 20 20 20 6c 6f 67 4e 20 2b 3d 20 31 3b  {.    logN += 1;
2dc09 0a 20 20 20 20 78 20 2a 3d 20 31 30 3b 0a 20 20  .    x *= 10;.  
2dc0a 7d 0a 20 20 72 65 74 75 72 6e 20 6c 6f 67 4e 3b  }.  return logN;
2dc0b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 77 6f 20 72 6f  .}../*.** Two ro
2dc0c 75 74 69 6e 65 73 20 66 6f 72 20 70 72 69 6e 74  utines for print
2dc0d 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ing the content 
2dc0e 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 5f 69 6e  of an sqlite3_in
2dc0f 64 65 78 5f 69 6e 66 6f 0a 2a 2a 20 73 74 72 75  dex_info.** stru
2dc10 63 74 75 72 65 2e 20 20 55 73 65 64 20 66 6f 72  cture.  Used for
2dc11 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62   testing and deb
2dc12 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 20 20 49 66  ugging only.  If
2dc13 20 6e 65 69 74 68 65 72 0a 2a 2a 20 53 51 4c 49   neither.** SQLI
2dc14 54 45 5f 54 45 53 54 20 6f 72 20 53 51 4c 49 54  TE_TEST or SQLIT
2dc15 45 5f 44 45 42 55 47 20 61 72 65 20 64 65 66 69  E_DEBUG are defi
2dc16 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 73 65 20  ned, then these 
2dc17 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72 65 20  routines.** are 
2dc18 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 69 66 20 21  no-ops..*/.#if !
2dc19 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2dc1a 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2dc1b 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c  ) && defined(SQL
2dc1c 49 54 45 5f 44 45 42 55 47 29 0a 73 74 61 74 69  ITE_DEBUG).stati
2dc1d 63 20 76 6f 69 64 20 54 52 41 43 45 5f 49 44 58  c void TRACE_IDX
2dc1e 5f 49 4e 50 55 54 53 28 73 71 6c 69 74 65 33 5f  _INPUTS(sqlite3_
2dc1f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 29 7b 0a  index_info *p){.
2dc20 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21    int i;.  if( !
2dc21 73 71 6c 69 74 65 33 57 68 65 72 65 54 72 61 63  sqlite3WhereTrac
2dc22 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f  e ) return;.  fo
2dc23 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6e  r(i=0; i<p->nCon
2dc24 73 74 72 61 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20  straint; i++){. 
2dc25 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
2dc26 72 69 6e 74 66 28 22 20 20 63 6f 6e 73 74 72 61  rintf("  constra
2dc27 69 6e 74 5b 25 64 5d 3a 20 63 6f 6c 3d 25 64 20  int[%d]: col=%d 
2dc28 74 65 72 6d 69 64 3d 25 64 20 6f 70 3d 25 64 20  termid=%d op=%d 
2dc29 75 73 61 62 6c 65 64 3d 25 64 5c 6e 22 2c 0a 20  usabled=%d\n",. 
2dc2a 20 20 20 20 20 20 69 2c 0a 20 20 20 20 20 20 20        i,.       
2dc2b 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69  p->aConstraint[i
2dc2c 5d 2e 69 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20  ].iColumn,.     
2dc2d 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74    p->aConstraint
2dc2e 5b 69 5d 2e 69 54 65 72 6d 4f 66 66 73 65 74 2c  [i].iTermOffset,
2dc2f 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73  .       p->aCons
2dc30 74 72 61 69 6e 74 5b 69 5d 2e 6f 70 2c 0a 20 20  traint[i].op,.  
2dc31 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61       p->aConstra
2dc32 69 6e 74 5b 69 5d 2e 75 73 61 62 6c 65 29 3b 0a  int[i].usable);.
2dc33 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69    }.  for(i=0; i
2dc34 3c 70 2d 3e 6e 4f 72 64 65 72 42 79 3b 20 69 2b  <p->nOrderBy; i+
2dc35 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  +){.    sqlite3D
2dc36 65 62 75 67 50 72 69 6e 74 66 28 22 20 20 6f 72  ebugPrintf("  or
2dc37 64 65 72 62 79 5b 25 64 5d 3a 20 63 6f 6c 3d 25  derby[%d]: col=%
2dc38 64 20 64 65 73 63 3d 25 64 5c 6e 22 2c 0a 20 20  d desc=%d\n",.  
2dc39 20 20 20 20 20 69 2c 0a 20 20 20 20 20 20 20 70       i,.       p
2dc3a 2d 3e 61 4f 72 64 65 72 42 79 5b 69 5d 2e 69 43  ->aOrderBy[i].iC
2dc3b 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20 20 20 70 2d  olumn,.       p-
2dc3c 3e 61 4f 72 64 65 72 42 79 5b 69 5d 2e 64 65 73  >aOrderBy[i].des
2dc3d 63 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63  c);.  }.}.static
2dc3e 20 76 6f 69 64 20 54 52 41 43 45 5f 49 44 58 5f   void TRACE_IDX_
2dc3f 4f 55 54 50 55 54 53 28 73 71 6c 69 74 65 33 5f  OUTPUTS(sqlite3_
2dc40 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 29 7b 0a  index_info *p){.
2dc41 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21    int i;.  if( !
2dc42 73 71 6c 69 74 65 33 57 68 65 72 65 54 72 61 63  sqlite3WhereTrac
2dc43 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f  e ) return;.  fo
2dc44 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6e  r(i=0; i<p->nCon
2dc45 73 74 72 61 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20  straint; i++){. 
2dc46 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
2dc47 72 69 6e 74 66 28 22 20 20 75 73 61 67 65 5b 25  rintf("  usage[%
2dc48 64 5d 3a 20 61 72 67 76 49 64 78 3d 25 64 20 6f  d]: argvIdx=%d o
2dc49 6d 69 74 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20  mit=%d\n",.     
2dc4a 20 20 69 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61    i,.       p->a
2dc4b 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b  ConstraintUsage[
2dc4c 69 5d 2e 61 72 67 76 49 6e 64 65 78 2c 0a 20 20  i].argvIndex,.  
2dc4d 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61       p->aConstra
2dc4e 69 6e 74 55 73 61 67 65 5b 69 5d 2e 6f 6d 69 74  intUsage[i].omit
2dc4f 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
2dc50 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 20 69  DebugPrintf("  i
2dc51 64 78 4e 75 6d 3d 25 64 5c 6e 22 2c 20 70 2d 3e  dxNum=%d\n", p->
2dc52 69 64 78 4e 75 6d 29 3b 0a 20 20 73 71 6c 69 74  idxNum);.  sqlit
2dc53 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 20  e3DebugPrintf(" 
2dc54 20 69 64 78 53 74 72 3d 25 73 5c 6e 22 2c 20 70   idxStr=%s\n", p
2dc55 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20 73 71 6c  ->idxStr);.  sql
2dc56 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
2dc57 22 20 20 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d  "  orderByConsum
2dc58 65 64 3d 25 64 5c 6e 22 2c 20 70 2d 3e 6f 72 64  ed=%d\n", p->ord
2dc59 65 72 42 79 43 6f 6e 73 75 6d 65 64 29 3b 0a 20  erByConsumed);. 
2dc5a 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2dc5b 6e 74 66 28 22 20 20 65 73 74 69 6d 61 74 65 64  ntf("  estimated
2dc5c 43 6f 73 74 3d 25 67 5c 6e 22 2c 20 70 2d 3e 65  Cost=%g\n", p->e
2dc5d 73 74 69 6d 61 74 65 64 43 6f 73 74 29 3b 0a 7d  stimatedCost);.}
2dc5e 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 54  .#else.#define T
2dc5f 52 41 43 45 5f 49 44 58 5f 49 4e 50 55 54 53 28  RACE_IDX_INPUTS(
2dc60 41 29 0a 23 64 65 66 69 6e 65 20 54 52 41 43 45  A).#define TRACE
2dc61 5f 49 44 58 5f 4f 55 54 50 55 54 53 28 41 29 0a  _IDX_OUTPUTS(A).
2dc62 23 65 6e 64 69 66 0a 0a 2f 2a 20 0a 2a 2a 20 52  #endif../* .** R
2dc63 65 71 75 69 72 65 64 20 62 65 63 61 75 73 65 20  equired because 
2dc64 62 65 73 74 49 6e 64 65 78 28 29 20 69 73 20 63  bestIndex() is c
2dc65 61 6c 6c 65 64 20 62 79 20 62 65 73 74 4f 72 43  alled by bestOrC
2dc66 6c 61 75 73 65 49 6e 64 65 78 28 29 20 0a 2a 2f  lauseIndex() .*/
2dc67 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 65 73  .static void bes
2dc68 74 49 6e 64 65 78 28 0a 20 20 20 20 50 61 72 73  tIndex(.    Pars
2dc69 65 2a 2c 20 57 68 65 72 65 43 6c 61 75 73 65 2a  e*, WhereClause*
2dc6a 2c 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74  , struct SrcList
2dc6b 5f 69 74 65 6d 2a 2c 20 42 69 74 6d 61 73 6b 2c  _item*, Bitmask,
2dc6c 20 45 78 70 72 4c 69 73 74 2a 2c 20 57 68 65 72   ExprList*, Wher
2dc6d 65 43 6f 73 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  eCost*);../*.** 
2dc6e 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 74 74  This routine att
2dc6f 65 6d 70 74 73 20 74 6f 20 66 69 6e 64 20 61 6e  empts to find an
2dc70 20 73 63 61 6e 6e 69 6e 67 20 73 74 72 61 74 65   scanning strate
2dc71 67 79 20 74 68 61 74 20 63 61 6e 20 62 65 20 75  gy that can be u
2dc72 73 65 64 20 0a 2a 2a 20 74 6f 20 6f 70 74 69 6d  sed .** to optim
2dc73 69 7a 65 20 61 6e 20 27 4f 52 27 20 65 78 70 72  ize an 'OR' expr
2dc74 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 70  ession that is p
2dc75 61 72 74 20 6f 66 20 61 20 57 48 45 52 45 20 63  art of a WHERE c
2dc76 6c 61 75 73 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68  lause. .**.** Th
2dc77 65 20 74 61 62 6c 65 20 61 73 73 6f 63 69 61 74  e table associat
2dc78 65 64 20 77 69 74 68 20 46 52 4f 4d 20 63 6c 61  ed with FROM cla
2dc79 75 73 65 20 74 65 72 6d 20 70 53 72 63 20 6d 61  use term pSrc ma
2dc7a 79 20 62 65 20 65 69 74 68 65 72 20 61 0a 2a 2a  y be either a.**
2dc7b 20 72 65 67 75 6c 61 72 20 42 2d 54 72 65 65 20   regular B-Tree 
2dc7c 74 61 62 6c 65 20 6f 72 20 61 20 76 69 72 74 75  table or a virtu
2dc7d 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  al table..*/.sta
2dc7e 74 69 63 20 76 6f 69 64 20 62 65 73 74 4f 72 43  tic void bestOrC
2dc7f 6c 61 75 73 65 49 6e 64 65 78 28 0a 20 20 50 61  lauseIndex(.  Pa
2dc80 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2dc81 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2dc82 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   parsing context
2dc83 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73   */.  WhereClaus
2dc84 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20 20  e *pWC,         
2dc85 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63    /* The WHERE c
2dc86 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63  lause */.  struc
2dc87 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
2dc88 70 53 72 63 2c 20 20 2f 2a 20 54 68 65 20 46 52  pSrc,  /* The FR
2dc89 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 20 74  OM clause term t
2dc8a 6f 20 73 65 61 72 63 68 20 2a 2f 0a 20 20 42 69  o search */.  Bi
2dc8b 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20  tmask notReady, 
2dc8c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73            /* Mas
2dc8d 6b 20 6f 66 20 63 75 72 73 6f 72 73 20 74 68 61  k of cursors tha
2dc8e 74 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61  t are not availa
2dc8f 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ble */.  ExprLis
2dc90 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20  t *pOrderBy,    
2dc91 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45       /* The ORDE
2dc92 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20  R BY clause */. 
2dc93 20 57 68 65 72 65 43 6f 73 74 20 2a 70 43 6f 73   WhereCost *pCos
2dc94 74 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t            /* 
2dc95 4c 6f 77 65 73 74 20 63 6f 73 74 20 71 75 65 72  Lowest cost quer
2dc96 79 20 70 6c 61 6e 20 2a 2f 0a 29 7b 0a 23 69 66  y plan */.){.#if
2dc97 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2dc98 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e  _OR_OPTIMIZATION
2dc99 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 43 75  .  const int iCu
2dc9a 72 20 3d 20 70 53 72 63 2d 3e 69 43 75 72 73 6f  r = pSrc->iCurso
2dc9b 72 3b 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73  r;   /* The curs
2dc9c 6f 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  or of the table 
2dc9d 74 6f 20 62 65 20 61 63 63 65 73 73 65 64 20 2a  to be accessed *
2dc9e 2f 0a 20 20 63 6f 6e 73 74 20 42 69 74 6d 61 73  /.  const Bitmas
2dc9f 6b 20 6d 61 73 6b 53 72 63 20 3d 20 67 65 74 4d  k maskSrc = getM
2dca0 61 73 6b 28 70 57 43 2d 3e 70 4d 61 73 6b 53 65  ask(pWC->pMaskSe
2dca1 74 2c 20 69 43 75 72 29 3b 20 20 2f 2a 20 42 69  t, iCur);  /* Bi
2dca2 74 6d 61 73 6b 20 66 6f 72 20 70 53 72 63 20 2a  tmask for pSrc *
2dca3 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 20  /.  WhereTerm * 
2dca4 63 6f 6e 73 74 20 70 57 43 45 6e 64 20 3d 20 26  const pWCEnd = &
2dca5 70 57 43 2d 3e 61 5b 70 57 43 2d 3e 6e 54 65 72  pWC->a[pWC->nTer
2dca6 6d 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 45 6e  m];        /* En
2dca7 64 20 6f 66 20 70 57 43 2d 3e 61 5b 5d 20 2a 2f  d of pWC->a[] */
2dca8 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54  .  WhereTerm *pT
2dca9 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  erm;            
2dcaa 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65       /* A single
2dcab 20 74 65 72 6d 20 6f 66 20 74 68 65 20 57 48 45   term of the WHE
2dcac 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 0a 20 20  RE clause */..  
2dcad 2f 2a 20 53 65 61 72 63 68 20 74 68 65 20 57 48  /* Search the WH
2dcae 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73  ERE clause terms
2dcaf 20 66 6f 72 20 61 20 75 73 61 62 6c 65 20 57 4f   for a usable WO
2dcb0 5f 4f 52 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 66  _OR term. */.  f
2dcb1 6f 72 28 70 54 65 72 6d 3d 70 57 43 2d 3e 61 3b  or(pTerm=pWC->a;
2dcb2 20 70 54 65 72 6d 3c 70 57 43 45 6e 64 3b 20 70   pTerm<pWCEnd; p
2dcb3 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28  Term++){.    if(
2dcb4 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
2dcb5 72 3d 3d 57 4f 5f 4f 52 20 0a 20 20 20 20 20 26  r==WO_OR .     &
2dcb6 26 20 28 28 70 54 65 72 6d 2d 3e 70 72 65 72 65  & ((pTerm->prere
2dcb7 71 41 6c 6c 20 26 20 7e 6d 61 73 6b 53 72 63 29  qAll & ~maskSrc)
2dcb8 20 26 20 6e 6f 74 52 65 61 64 79 29 3d 3d 30 0a   & notReady)==0.
2dcb9 20 20 20 20 20 26 26 20 28 70 54 65 72 6d 2d 3e       && (pTerm->
2dcba 75 2e 70 4f 72 49 6e 66 6f 2d 3e 69 6e 64 65 78  u.pOrInfo->index
2dcbb 61 62 6c 65 20 26 20 6d 61 73 6b 53 72 63 29 21  able & maskSrc)!
2dcbc 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  =0 .    ){.     
2dcbd 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 20 63   WhereClause * c
2dcbe 6f 6e 73 74 20 70 4f 72 57 43 20 3d 20 26 70 54  onst pOrWC = &pT
2dcbf 65 72 6d 2d 3e 75 2e 70 4f 72 49 6e 66 6f 2d 3e  erm->u.pOrInfo->
2dcc0 77 63 3b 0a 20 20 20 20 20 20 57 68 65 72 65 54  wc;.      WhereT
2dcc1 65 72 6d 20 2a 20 63 6f 6e 73 74 20 70 4f 72 57  erm * const pOrW
2dcc2 43 45 6e 64 20 3d 20 26 70 4f 72 57 43 2d 3e 61  CEnd = &pOrWC->a
2dcc3 5b 70 4f 72 57 43 2d 3e 6e 54 65 72 6d 5d 3b 0a  [pOrWC->nTerm];.
2dcc4 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20        WhereTerm 
2dcc5 2a 70 4f 72 54 65 72 6d 3b 0a 20 20 20 20 20 20  *pOrTerm;.      
2dcc6 69 6e 74 20 66 6c 61 67 73 20 3d 20 57 48 45 52  int flags = WHER
2dcc7 45 5f 4d 55 4c 54 49 5f 4f 52 3b 0a 20 20 20 20  E_MULTI_OR;.    
2dcc8 20 20 64 6f 75 62 6c 65 20 72 54 6f 74 61 6c 20    double rTotal 
2dcc9 3d 20 30 3b 0a 20 20 20 20 20 20 64 6f 75 62 6c  = 0;.      doubl
2dcca 65 20 6e 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20  e nRow = 0;.    
2dccb 20 20 42 69 74 6d 61 73 6b 20 75 73 65 64 20 3d    Bitmask used =
2dccc 20 30 3b 0a 0a 20 20 20 20 20 20 66 6f 72 28 70   0;..      for(p
2dccd 4f 72 54 65 72 6d 3d 70 4f 72 57 43 2d 3e 61 3b  OrTerm=pOrWC->a;
2dcce 20 70 4f 72 54 65 72 6d 3c 70 4f 72 57 43 45 6e   pOrTerm<pOrWCEn
2dccf 64 3b 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20  d; pOrTerm++){. 
2dcd0 20 20 20 20 20 20 20 57 68 65 72 65 43 6f 73 74         WhereCost
2dcd1 20 73 54 65 72 6d 43 6f 73 74 3b 0a 20 20 20 20   sTermCost;.    
2dcd2 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28      WHERETRACE((
2dcd3 22 2e 2e 2e 20 4d 75 6c 74 69 2d 69 6e 64 65 78  "... Multi-index
2dcd4 20 4f 52 20 74 65 73 74 69 6e 67 20 66 6f 72 20   OR testing for 
2dcd5 74 65 72 6d 20 25 64 20 6f 66 20 25 64 2e 2e 2e  term %d of %d...
2dcd6 2e 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20  .\n", .         
2dcd7 20 28 70 4f 72 54 65 72 6d 20 2d 20 70 4f 72 57   (pOrTerm - pOrW
2dcd8 43 2d 3e 61 29 2c 20 28 70 54 65 72 6d 20 2d 20  C->a), (pTerm - 
2dcd9 70 57 43 2d 3e 61 29 0a 20 20 20 20 20 20 20 20  pWC->a).        
2dcda 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ));.        if( 
2dcdb 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  pOrTerm->eOperat
2dcdc 6f 72 3d 3d 57 4f 5f 41 4e 44 20 29 7b 0a 20 20  or==WO_AND ){.  
2dcdd 20 20 20 20 20 20 20 20 57 68 65 72 65 43 6c 61          WhereCla
2dcde 75 73 65 20 2a 70 41 6e 64 57 43 20 3d 20 26 70  use *pAndWC = &p
2dcdf 4f 72 54 65 72 6d 2d 3e 75 2e 70 41 6e 64 49 6e  OrTerm->u.pAndIn
2dce0 66 6f 2d 3e 77 63 3b 0a 20 20 20 20 20 20 20 20  fo->wc;.        
2dce1 20 20 62 65 73 74 49 6e 64 65 78 28 70 50 61 72    bestIndex(pPar
2dce2 73 65 2c 20 70 41 6e 64 57 43 2c 20 70 53 72 63  se, pAndWC, pSrc
2dce3 2c 20 6e 6f 74 52 65 61 64 79 2c 20 30 2c 20 26  , notReady, 0, &
2dce4 73 54 65 72 6d 43 6f 73 74 29 3b 0a 20 20 20 20  sTermCost);.    
2dce5 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f      }else if( pO
2dce6 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  rTerm->leftCurso
2dce7 72 3d 3d 69 43 75 72 20 29 7b 0a 20 20 20 20 20  r==iCur ){.     
2dce8 20 20 20 20 20 57 68 65 72 65 43 6c 61 75 73 65       WhereClause
2dce9 20 74 65 6d 70 57 43 3b 0a 20 20 20 20 20 20 20   tempWC;.       
2dcea 20 20 20 74 65 6d 70 57 43 2e 70 50 61 72 73 65     tempWC.pParse
2dceb 20 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 3b 0a   = pWC->pParse;.
2dcec 20 20 20 20 20 20 20 20 20 20 74 65 6d 70 57 43            tempWC
2dced 2e 70 4d 61 73 6b 53 65 74 20 3d 20 70 57 43 2d  .pMaskSet = pWC-
2dcee 3e 70 4d 61 73 6b 53 65 74 3b 0a 20 20 20 20 20  >pMaskSet;.     
2dcef 20 20 20 20 20 74 65 6d 70 57 43 2e 6f 70 20 3d       tempWC.op =
2dcf0 20 54 4b 5f 41 4e 44 3b 0a 20 20 20 20 20 20 20   TK_AND;.       
2dcf1 20 20 20 74 65 6d 70 57 43 2e 61 20 3d 20 70 4f     tempWC.a = pO
2dcf2 72 54 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 20  rTerm;.         
2dcf3 20 74 65 6d 70 57 43 2e 6e 54 65 72 6d 20 3d 20   tempWC.nTerm = 
2dcf4 31 3b 0a 20 20 20 20 20 20 20 20 20 20 62 65 73  1;.          bes
2dcf5 74 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 26  tIndex(pParse, &
2dcf6 74 65 6d 70 57 43 2c 20 70 53 72 63 2c 20 6e 6f  tempWC, pSrc, no
2dcf7 74 52 65 61 64 79 2c 20 30 2c 20 26 73 54 65 72  tReady, 0, &sTer
2dcf8 6d 43 6f 73 74 29 3b 0a 20 20 20 20 20 20 20 20  mCost);.        
2dcf9 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
2dcfa 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
2dcfb 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 54 6f     }.        rTo
2dcfc 74 61 6c 20 2b 3d 20 73 54 65 72 6d 43 6f 73 74  tal += sTermCost
2dcfd 2e 72 43 6f 73 74 3b 0a 20 20 20 20 20 20 20 20  .rCost;.        
2dcfe 6e 52 6f 77 20 2b 3d 20 73 54 65 72 6d 43 6f 73  nRow += sTermCos
2dcff 74 2e 6e 52 6f 77 3b 0a 20 20 20 20 20 20 20 20  t.nRow;.        
2dd00 75 73 65 64 20 7c 3d 20 73 54 65 72 6d 43 6f 73  used |= sTermCos
2dd01 74 2e 75 73 65 64 3b 0a 20 20 20 20 20 20 20 20  t.used;.        
2dd02 69 66 28 20 72 54 6f 74 61 6c 3e 3d 70 43 6f 73  if( rTotal>=pCos
2dd03 74 2d 3e 72 43 6f 73 74 20 29 20 62 72 65 61 6b  t->rCost ) break
2dd04 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
2dd05 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
2dd06 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  an ORDER BY clau
2dd07 73 65 2c 20 69 6e 63 72 65 61 73 65 20 74 68 65  se, increase the
2dd08 20 73 63 61 6e 20 63 6f 73 74 20 74 6f 20 61 63   scan cost to ac
2dd09 63 6f 75 6e 74 20 0a 20 20 20 20 20 20 2a 2a 20  count .      ** 
2dd0a 66 6f 72 20 74 68 65 20 63 6f 73 74 20 6f 66 20  for the cost of 
2dd0b 74 68 65 20 73 6f 72 74 2e 20 2a 2f 0a 20 20 20  the sort. */.   
2dd0c 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 21     if( pOrderBy!
2dd0d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 54  =0 ){.        rT
2dd0e 6f 74 61 6c 20 2b 3d 20 6e 52 6f 77 2a 65 73 74  otal += nRow*est
2dd0f 4c 6f 67 28 6e 52 6f 77 29 3b 0a 20 20 20 20 20  Log(nRow);.     
2dd10 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22     WHERETRACE(("
2dd11 2e 2e 2e 20 73 6f 72 74 69 6e 67 20 69 6e 63 72  ... sorting incr
2dd12 65 61 73 65 73 20 4f 52 20 63 6f 73 74 20 74 6f  eases OR cost to
2dd13 20 25 2e 39 67 5c 6e 22 2c 20 72 54 6f 74 61 6c   %.9g\n", rTotal
2dd14 29 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  ));.      }..   
2dd15 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6f 73     /* If the cos
2dd16 74 20 6f 66 20 73 63 61 6e 6e 69 6e 67 20 75 73  t of scanning us
2dd17 69 6e 67 20 74 68 69 73 20 4f 52 20 74 65 72 6d  ing this OR term
2dd18 20 66 6f 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f   for optimizatio
2dd19 6e 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 6c 65  n is.      ** le
2dd1a 73 73 20 74 68 61 6e 20 74 68 65 20 63 75 72 72  ss than the curr
2dd1b 65 6e 74 20 63 6f 73 74 20 73 74 6f 72 65 64 20  ent cost stored 
2dd1c 69 6e 20 70 43 6f 73 74 2c 20 72 65 70 6c 61 63  in pCost, replac
2dd1d 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 20  e the contents. 
2dd1e 20 20 20 20 20 2a 2a 20 6f 66 20 70 43 6f 73 74       ** of pCost
2dd1f 2e 20 2a 2f 0a 20 20 20 20 20 20 57 48 45 52 45  . */.      WHERE
2dd20 54 52 41 43 45 28 28 22 2e 2e 2e 20 6d 75 6c 74  TRACE(("... mult
2dd21 69 2d 69 6e 64 65 78 20 4f 52 20 63 6f 73 74 3d  i-index OR cost=
2dd22 25 2e 39 67 20 6e 72 6f 77 3d 25 2e 39 67 5c 6e  %.9g nrow=%.9g\n
2dd23 22 2c 20 72 54 6f 74 61 6c 2c 20 6e 52 6f 77 29  ", rTotal, nRow)
2dd24 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 54 6f  );.      if( rTo
2dd25 74 61 6c 3c 70 43 6f 73 74 2d 3e 72 43 6f 73 74  tal<pCost->rCost
2dd26 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 73   ){.        pCos
2dd27 74 2d 3e 72 43 6f 73 74 20 3d 20 72 54 6f 74 61  t->rCost = rTota
2dd28 6c 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 73 74  l;.        pCost
2dd29 2d 3e 6e 52 6f 77 20 3d 20 6e 52 6f 77 3b 0a 20  ->nRow = nRow;. 
2dd2a 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 75 73         pCost->us
2dd2b 65 64 20 3d 20 75 73 65 64 3b 0a 20 20 20 20 20  ed = used;.     
2dd2c 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77     pCost->plan.w
2dd2d 73 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a  sFlags = flags;.
2dd2e 20 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 70          pCost->p
2dd2f 6c 61 6e 2e 75 2e 70 54 65 72 6d 20 3d 20 70 54  lan.u.pTerm = pT
2dd30 65 72 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  erm;.      }.   
2dd31 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a   }.  }.#endif /*
2dd32 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f   SQLITE_OMIT_OR_
2dd33 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a  OPTIMIZATION */.
2dd34 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
2dd35 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
2dd36 42 4c 45 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  BLE./*.** Alloca
2dd37 74 65 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 20  te and populate 
2dd38 61 6e 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  an sqlite3_index
2dd39 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e  _info structure.
2dd3a 20 49 74 20 69 73 20 74 68 65 20 0a 2a 2a 20 72   It is the .** r
2dd3b 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66  esponsibility of
2dd3c 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 65   the caller to e
2dd3d 76 65 6e 74 75 61 6c 6c 79 20 72 65 6c 65 61 73  ventually releas
2dd3e 65 20 74 68 65 20 73 74 72 75 63 74 75 72 65 0a  e the structure.
2dd3f 2a 2a 20 62 79 20 70 61 73 73 69 6e 67 20 74 68  ** by passing th
2dd40 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e  e pointer return
2dd41 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74  ed by this funct
2dd42 69 6f 6e 20 74 6f 20 73 71 6c 69 74 65 33 5f 66  ion to sqlite3_f
2dd43 72 65 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  ree()..*/.static
2dd44 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
2dd45 6e 66 6f 20 2a 61 6c 6c 6f 63 61 74 65 49 6e 64  nfo *allocateInd
2dd46 65 78 49 6e 66 6f 28 0a 20 20 50 61 72 73 65 20  exInfo(.  Parse 
2dd47 2a 70 50 61 72 73 65 2c 20 0a 20 20 57 68 65 72  *pParse, .  Wher
2dd48 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 0a 20 20  eClause *pWC,.  
2dd49 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
2dd4a 74 65 6d 20 2a 70 53 72 63 2c 0a 20 20 45 78 70  tem *pSrc,.  Exp
2dd4b 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 0a  rList *pOrderBy.
2dd4c 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20  ){.  int i, j;. 
2dd4d 20 69 6e 74 20 6e 54 65 72 6d 3b 0a 20 20 73 74   int nTerm;.  st
2dd4e 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
2dd4f 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 2a 70  ex_constraint *p
2dd50 49 64 78 43 6f 6e 73 3b 0a 20 20 73 74 72 75 63  IdxCons;.  struc
2dd51 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
2dd52 6f 72 64 65 72 62 79 20 2a 70 49 64 78 4f 72 64  orderby *pIdxOrd
2dd53 65 72 42 79 3b 0a 20 20 73 74 72 75 63 74 20 73  erBy;.  struct s
2dd54 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e  qlite3_index_con
2dd55 73 74 72 61 69 6e 74 5f 75 73 61 67 65 20 2a 70  straint_usage *p
2dd56 55 73 61 67 65 3b 0a 20 20 57 68 65 72 65 54 65  Usage;.  WhereTe
2dd57 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e 74  rm *pTerm;.  int
2dd58 20 6e 4f 72 64 65 72 42 79 3b 0a 20 20 73 71 6c   nOrderBy;.  sql
2dd59 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
2dd5a 2a 70 49 64 78 49 6e 66 6f 3b 0a 0a 20 20 57 48  *pIdxInfo;..  WH
2dd5b 45 52 45 54 52 41 43 45 28 28 22 52 65 63 6f 6d  ERETRACE(("Recom
2dd5c 70 75 74 69 6e 67 20 69 6e 64 65 78 20 69 6e 66  puting index inf
2dd5d 6f 20 66 6f 72 20 25 73 2e 2e 2e 5c 6e 22 2c 20  o for %s...\n", 
2dd5e 70 53 72 63 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d  pSrc->pTab->zNam
2dd5f 65 29 29 3b 0a 0a 20 20 2f 2a 20 43 6f 75 6e 74  e));..  /* Count
2dd60 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
2dd61 6f 73 73 69 62 6c 65 20 57 48 45 52 45 20 63 6c  ossible WHERE cl
2dd62 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73  ause constraints
2dd63 20 72 65 66 65 72 72 69 6e 67 0a 20 20 2a 2a 20   referring.  ** 
2dd64 74 6f 20 74 68 69 73 20 76 69 72 74 75 61 6c 20  to this virtual 
2dd65 74 61 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 69  table */.  for(i
2dd66 3d 6e 54 65 72 6d 3d 30 2c 20 70 54 65 72 6d 3d  =nTerm=0, pTerm=
2dd67 70 57 43 2d 3e 61 3b 20 69 3c 70 57 43 2d 3e 6e  pWC->a; i<pWC->n
2dd68 54 65 72 6d 3b 20 69 2b 2b 2c 20 70 54 65 72 6d  Term; i++, pTerm
2dd69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 54 65  ++){.    if( pTe
2dd6a 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 21  rm->leftCursor !
2dd6b 3d 20 70 53 72 63 2d 3e 69 43 75 72 73 6f 72 20  = pSrc->iCursor 
2dd6c 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
2dd6d 61 73 73 65 72 74 28 20 28 70 54 65 72 6d 2d 3e  assert( (pTerm->
2dd6e 65 4f 70 65 72 61 74 6f 72 26 28 70 54 65 72 6d  eOperator&(pTerm
2dd6f 2d 3e 65 4f 70 65 72 61 74 6f 72 2d 31 29 29 3d  ->eOperator-1))=
2dd70 3d 30 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  =0 );.    testca
2dd71 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  se( pTerm->eOper
2dd72 61 74 6f 72 3d 3d 57 4f 5f 49 4e 20 29 3b 0a 20  ator==WO_IN );. 
2dd73 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65     testcase( pTe
2dd74 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57  rm->eOperator==W
2dd75 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20  O_ISNULL );.    
2dd76 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  if( pTerm->eOper
2dd77 61 74 6f 72 20 26 20 28 57 4f 5f 49 4e 7c 57 4f  ator & (WO_IN|WO
2dd78 5f 49 53 4e 55 4c 4c 29 20 29 20 63 6f 6e 74 69  _ISNULL) ) conti
2dd79 6e 75 65 3b 0a 20 20 20 20 6e 54 65 72 6d 2b 2b  nue;.    nTerm++
2dd7a 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
2dd7b 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2dd7c 73 65 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 6c 79  se contains only
2dd7d 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
2dd7e 63 75 72 72 65 6e 74 20 0a 20 20 2a 2a 20 76 69  current .  ** vi
2dd7f 72 74 75 61 6c 20 74 61 62 6c 65 20 74 68 65 6e  rtual table then
2dd80 20 61 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20   allocate space 
2dd81 66 6f 72 20 74 68 65 20 61 4f 72 64 65 72 42 79  for the aOrderBy
2dd82 20 70 61 72 74 20 6f 66 0a 20 20 2a 2a 20 74 68   part of.  ** th
2dd83 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  e sqlite3_index_
2dd84 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a  info structure..
2dd85 20 20 2a 2f 0a 20 20 6e 4f 72 64 65 72 42 79 20    */.  nOrderBy 
2dd86 3d 20 30 3b 0a 20 20 69 66 28 20 70 4f 72 64 65  = 0;.  if( pOrde
2dd87 72 42 79 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  rBy ){.    for(i
2dd88 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e  =0; i<pOrderBy->
2dd89 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nExpr; i++){.   
2dd8a 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d     Expr *pExpr =
2dd8b 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e   pOrderBy->a[i].
2dd8c 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69 66 28  pExpr;.      if(
2dd8d 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43   pExpr->op!=TK_C
2dd8e 4f 4c 55 4d 4e 20 7c 7c 20 70 45 78 70 72 2d 3e  OLUMN || pExpr->
2dd8f 69 54 61 62 6c 65 21 3d 70 53 72 63 2d 3e 69 43  iTable!=pSrc->iC
2dd90 75 72 73 6f 72 20 29 20 62 72 65 61 6b 3b 0a 20  ursor ) break;. 
2dd91 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3d 3d     }.    if( i==
2dd92 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 20  pOrderBy->nExpr 
2dd93 29 7b 0a 20 20 20 20 20 20 6e 4f 72 64 65 72 42  ){.      nOrderB
2dd94 79 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45  y = pOrderBy->nE
2dd95 78 70 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  xpr;.    }.  }..
2dd96 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68    /* Allocate th
2dd97 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  e sqlite3_index_
2dd98 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a 20  info structure. 
2dd99 20 2a 2f 0a 20 20 70 49 64 78 49 6e 66 6f 20 3d   */.  pIdxInfo =
2dd9a 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
2dd9b 5a 65 72 6f 28 70 50 61 72 73 65 2d 3e 64 62 2c  Zero(pParse->db,
2dd9c 20 73 69 7a 65 6f 66 28 2a 70 49 64 78 49 6e 66   sizeof(*pIdxInf
2dd9d 6f 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  o).             
2dd9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2b 20                + 
2dd9f 28 73 69 7a 65 6f 66 28 2a 70 49 64 78 43 6f 6e  (sizeof(*pIdxCon
2dda0 73 29 20 2b 20 73 69 7a 65 6f 66 28 2a 70 55 73  s) + sizeof(*pUs
2dda1 61 67 65 29 29 2a 6e 54 65 72 6d 0a 20 20 20 20  age))*nTerm.    
2dda2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dda3 20 20 20 20 20 20 20 2b 20 73 69 7a 65 6f 66 28         + sizeof(
2dda4 2a 70 49 64 78 4f 72 64 65 72 42 79 29 2a 6e 4f  *pIdxOrderBy)*nO
2dda5 72 64 65 72 42 79 20 29 3b 0a 20 20 69 66 28 20  rderBy );.  if( 
2dda6 70 49 64 78 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20  pIdxInfo==0 ){. 
2dda7 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2dda8 73 67 28 70 50 61 72 73 65 2c 20 22 6f 75 74 20  sg(pParse, "out 
2dda9 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 20  of memory");.   
2ddaa 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e   /* (double)0 In
2ddab 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f   case of SQLITE_
2ddac 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
2ddad 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 72 65  INT... */.    re
2ddae 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  turn 0;.  }..  /
2ddaf 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * Initialize the
2ddb0 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65   structure.  The
2ddb1 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
2ddb2 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 63 6f  nfo structure co
2ddb3 6e 74 61 69 6e 73 0a 20 20 2a 2a 20 6d 61 6e 79  ntains.  ** many
2ddb4 20 66 69 65 6c 64 73 20 74 68 61 74 20 61 72 65   fields that are
2ddb5 20 64 65 63 6c 61 72 65 64 20 22 63 6f 6e 73 74   declared "const
2ddb6 22 20 74 6f 20 70 72 65 76 65 6e 74 20 78 42 65  " to prevent xBe
2ddb7 73 74 49 6e 64 65 78 20 66 72 6f 6d 0a 20 20 2a  stIndex from.  *
2ddb8 2a 20 63 68 61 6e 67 69 6e 67 20 74 68 65 6d 2e  * changing them.
2ddb9 20 20 57 65 20 68 61 76 65 20 74 6f 20 64 6f 20    We have to do 
2ddba 73 6f 6d 65 20 66 75 6e 6b 79 20 63 61 73 74 69  some funky casti
2ddbb 6e 67 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 20  ng in order to. 
2ddbc 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20 74   ** initialize t
2ddbd 68 6f 73 65 20 66 69 65 6c 64 73 2e 0a 20 20 2a  hose fields..  *
2ddbe 2f 0a 20 20 70 49 64 78 43 6f 6e 73 20 3d 20 28  /.  pIdxCons = (
2ddbf 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
2ddc0 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 2a  ndex_constraint*
2ddc1 29 26 70 49 64 78 49 6e 66 6f 5b 31 5d 3b 0a 20  )&pIdxInfo[1];. 
2ddc2 20 70 49 64 78 4f 72 64 65 72 42 79 20 3d 20 28   pIdxOrderBy = (
2ddc3 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
2ddc4 6e 64 65 78 5f 6f 72 64 65 72 62 79 2a 29 26 70  ndex_orderby*)&p
2ddc5 49 64 78 43 6f 6e 73 5b 6e 54 65 72 6d 5d 3b 0a  IdxCons[nTerm];.
2ddc6 20 20 70 55 73 61 67 65 20 3d 20 28 73 74 72 75    pUsage = (stru
2ddc7 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  ct sqlite3_index
2ddc8 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67  _constraint_usag
2ddc9 65 2a 29 26 70 49 64 78 4f 72 64 65 72 42 79 5b  e*)&pIdxOrderBy[
2ddca 6e 4f 72 64 65 72 42 79 5d 3b 0a 20 20 2a 28 69  nOrderBy];.  *(i
2ddcb 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 6e  nt*)&pIdxInfo->n
2ddcc 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 6e 54 65  Constraint = nTe
2ddcd 72 6d 3b 0a 20 20 2a 28 69 6e 74 2a 29 26 70 49  rm;.  *(int*)&pI
2ddce 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79  dxInfo->nOrderBy
2ddcf 20 3d 20 6e 4f 72 64 65 72 42 79 3b 0a 20 20 2a   = nOrderBy;.  *
2ddd0 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  (struct sqlite3_
2ddd1 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74  index_constraint
2ddd2 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61 43  **)&pIdxInfo->aC
2ddd3 6f 6e 73 74 72 61 69 6e 74 20 3d 20 70 49 64 78  onstraint = pIdx
2ddd4 43 6f 6e 73 3b 0a 20 20 2a 28 73 74 72 75 63 74  Cons;.  *(struct
2ddd5 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f   sqlite3_index_o
2ddd6 72 64 65 72 62 79 2a 2a 29 26 70 49 64 78 49 6e  rderby**)&pIdxIn
2ddd7 66 6f 2d 3e 61 4f 72 64 65 72 42 79 20 3d 20 70  fo->aOrderBy = p
2ddd8 49 64 78 4f 72 64 65 72 42 79 3b 0a 20 20 2a 28  IdxOrderBy;.  *(
2ddd9 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
2ddda 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f  ndex_constraint_
2dddb 75 73 61 67 65 2a 2a 29 26 70 49 64 78 49 6e 66  usage**)&pIdxInf
2dddc 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73  o->aConstraintUs
2dddd 61 67 65 20 3d 0a 20 20 20 20 20 20 20 20 20 20  age =.          
2ddde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dddf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dde1 20 20 20 20 20 20 20 20 20 70 55 73 61 67 65 3b           pUsage;
2dde2 0a 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 2c 20 70  ..  for(i=j=0, p
2dde3 54 65 72 6d 3d 70 57 43 2d 3e 61 3b 20 69 3c 70  Term=pWC->a; i<p
2dde4 57 43 2d 3e 6e 54 65 72 6d 3b 20 69 2b 2b 2c 20  WC->nTerm; i++, 
2dde5 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66  pTerm++){.    if
2dde6 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72  ( pTerm->leftCur
2dde7 73 6f 72 20 21 3d 20 70 53 72 63 2d 3e 69 43 75  sor != pSrc->iCu
2dde8 72 73 6f 72 20 29 20 63 6f 6e 74 69 6e 75 65 3b  rsor ) continue;
2dde9 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 54  .    assert( (pT
2ddea 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 26 28  erm->eOperator&(
2ddeb 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
2ddec 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 74  -1))==0 );.    t
2dded 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e  estcase( pTerm->
2ddee 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 4e  eOperator==WO_IN
2ddef 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
2ddf0 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  ( pTerm->eOperat
2ddf1 6f 72 3d 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b  or==WO_ISNULL );
2ddf2 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e  .    if( pTerm->
2ddf3 65 4f 70 65 72 61 74 6f 72 20 26 20 28 57 4f 5f  eOperator & (WO_
2ddf4 49 4e 7c 57 4f 5f 49 53 4e 55 4c 4c 29 20 29 20  IN|WO_ISNULL) ) 
2ddf5 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 70 49  continue;.    pI
2ddf6 64 78 43 6f 6e 73 5b 6a 5d 2e 69 43 6f 6c 75 6d  dxCons[j].iColum
2ddf7 6e 20 3d 20 70 54 65 72 6d 2d 3e 75 2e 6c 65 66  n = pTerm->u.lef
2ddf8 74 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 70 49 64  tColumn;.    pId
2ddf9 78 43 6f 6e 73 5b 6a 5d 2e 69 54 65 72 6d 4f 66  xCons[j].iTermOf
2ddfa 66 73 65 74 20 3d 20 69 3b 0a 20 20 20 20 70 49  fset = i;.    pI
2ddfb 64 78 43 6f 6e 73 5b 6a 5d 2e 6f 70 20 3d 20 28  dxCons[j].op = (
2ddfc 75 38 29 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  u8)pTerm->eOpera
2ddfd 74 6f 72 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20  tor;.    /* The 
2ddfe 64 69 72 65 63 74 20 61 73 73 69 67 6e 6d 65 6e  direct assignmen
2ddff 74 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75  t in the previou
2de00 73 20 6c 69 6e 65 20 69 73 20 70 6f 73 73 69 62  s line is possib
2de01 6c 65 20 6f 6e 6c 79 20 62 65 63 61 75 73 65 0a  le only because.
2de02 20 20 20 20 2a 2a 20 74 68 65 20 57 4f 5f 20 61      ** the WO_ a
2de03 6e 64 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  nd SQLITE_INDEX_
2de04 43 4f 4e 53 54 52 41 49 4e 54 5f 20 63 6f 64 65  CONSTRAINT_ code
2de05 73 20 61 72 65 20 69 64 65 6e 74 69 63 61 6c 2e  s are identical.
2de06 20 20 54 68 65 0a 20 20 20 20 2a 2a 20 66 6f 6c    The.    ** fol
2de07 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 73 20 76  lowing asserts v
2de08 65 72 69 66 79 20 74 68 69 73 20 66 61 63 74 2e  erify this fact.
2de09 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
2de0a 57 4f 5f 45 51 3d 3d 53 51 4c 49 54 45 5f 49 4e  WO_EQ==SQLITE_IN
2de0b 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 45  DEX_CONSTRAINT_E
2de0c 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  Q );.    assert(
2de0d 20 57 4f 5f 4c 54 3d 3d 53 51 4c 49 54 45 5f 49   WO_LT==SQLITE_I
2de0e 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
2de0f 4c 54 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  LT );.    assert
2de10 28 20 57 4f 5f 4c 45 3d 3d 53 51 4c 49 54 45 5f  ( WO_LE==SQLITE_
2de11 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54  INDEX_CONSTRAINT
2de12 5f 4c 45 20 29 3b 0a 20 20 20 20 61 73 73 65 72  _LE );.    asser
2de13 74 28 20 57 4f 5f 47 54 3d 3d 53 51 4c 49 54 45  t( WO_GT==SQLITE
2de14 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e  _INDEX_CONSTRAIN
2de15 54 5f 47 54 20 29 3b 0a 20 20 20 20 61 73 73 65  T_GT );.    asse
2de16 72 74 28 20 57 4f 5f 47 45 3d 3d 53 51 4c 49 54  rt( WO_GE==SQLIT
2de17 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
2de18 4e 54 5f 47 45 20 29 3b 0a 20 20 20 20 61 73 73  NT_GE );.    ass
2de19 65 72 74 28 20 57 4f 5f 4d 41 54 43 48 3d 3d 53  ert( WO_MATCH==S
2de1a 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
2de1b 54 52 41 49 4e 54 5f 4d 41 54 43 48 20 29 3b 0a  TRAINT_MATCH );.
2de1c 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65 72      assert( pTer
2de1d 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28  m->eOperator & (
2de1e 57 4f 5f 45 51 7c 57 4f 5f 4c 54 7c 57 4f 5f 4c  WO_EQ|WO_LT|WO_L
2de1f 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47 45 7c 57 4f  E|WO_GT|WO_GE|WO
2de20 5f 4d 41 54 43 48 29 20 29 3b 0a 20 20 20 20 6a  _MATCH) );.    j
2de21 2b 2b 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  ++;.  }.  for(i=
2de22 30 3b 20 69 3c 6e 4f 72 64 65 72 42 79 3b 20 69  0; i<nOrderBy; i
2de23 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70  ++){.    Expr *p
2de24 45 78 70 72 20 3d 20 70 4f 72 64 65 72 42 79 2d  Expr = pOrderBy-
2de25 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[i].pExpr;.   
2de26 20 70 49 64 78 4f 72 64 65 72 42 79 5b 69 5d 2e   pIdxOrderBy[i].
2de27 69 43 6f 6c 75 6d 6e 20 3d 20 70 45 78 70 72 2d  iColumn = pExpr-
2de28 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 70 49  >iColumn;.    pI
2de29 64 78 4f 72 64 65 72 42 79 5b 69 5d 2e 64 65 73  dxOrderBy[i].des
2de2a 63 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b  c = pOrderBy->a[
2de2b 69 5d 2e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20  i].sortOrder;.  
2de2c 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 49 64 78  }..  return pIdx
2de2d 49 6e 66 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  Info;.}../*.** T
2de2e 68 65 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20  he table object 
2de2f 72 65 66 65 72 65 6e 63 65 20 70 61 73 73 65 64  reference passed
2de30 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61   as the second a
2de31 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20  rgument to this 
2de32 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74  function.** must
2de33 20 72 65 70 72 65 73 65 6e 74 20 61 20 76 69 72   represent a vir
2de34 74 75 61 6c 20 74 61 62 6c 65 2e 20 54 68 69 73  tual table. This
2de35 20 66 75 6e 63 74 69 6f 6e 20 69 6e 76 6f 6b 65   function invoke
2de36 73 20 74 68 65 20 78 42 65 73 74 49 6e 64 65 78  s the xBestIndex
2de37 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20  ().** method of 
2de38 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
2de39 65 20 77 69 74 68 20 74 68 65 20 73 71 6c 69 74  e with the sqlit
2de3a 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 70 6f  e3_index_info po
2de3b 69 6e 74 65 72 20 70 61 73 73 65 64 0a 2a 2a 20  inter passed.** 
2de3c 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e  as the argument.
2de3d 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72  .**.** If an err
2de3e 6f 72 20 6f 63 63 75 72 73 2c 20 70 50 61 72 73  or occurs, pPars
2de3f 65 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 77  e is populated w
2de40 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ith an error mes
2de41 73 61 67 65 20 61 6e 64 20 61 0a 2a 2a 20 6e 6f  sage and a.** no
2de42 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 69 73 20  n-zero value is 
2de43 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
2de44 69 73 65 2c 20 30 20 69 73 20 72 65 74 75 72 6e  ise, 0 is return
2de45 65 64 20 61 6e 64 20 74 68 65 20 6f 75 74 70 75  ed and the outpu
2de46 74 0a 2a 2a 20 70 61 72 74 20 6f 66 20 74 68 65  t.** part of the
2de47 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
2de48 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 73  nfo structure is
2de49 20 6c 65 66 74 20 70 6f 70 75 6c 61 74 65 64 2e   left populated.
2de4a 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f  .**.** Whether o
2de4b 72 20 6e 6f 74 20 61 6e 20 65 72 72 6f 72 20 69  r not an error i
2de4c 73 20 72 65 74 75 72 6e 65 64 2c 20 69 74 20 69  s returned, it i
2de4d 73 20 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69  s the responsibi
2de4e 6c 69 74 79 20 6f 66 20 74 68 65 0a 2a 2a 20 63  lity of the.** c
2de4f 61 6c 6c 65 72 20 74 6f 20 65 76 65 6e 74 75 61  aller to eventua
2de50 6c 6c 79 20 66 72 65 65 20 70 2d 3e 69 64 78 53  lly free p->idxS
2de51 74 72 20 69 66 20 70 2d 3e 6e 65 65 64 54 6f 46  tr if p->needToF
2de52 72 65 65 49 64 78 53 74 72 20 69 6e 64 69 63 61  reeIdxStr indica
2de53 74 65 73 0a 2a 2a 20 74 68 61 74 20 74 68 69 73  tes.** that this
2de54 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f   is required..*/
2de55 0a 73 74 61 74 69 63 20 69 6e 74 20 76 74 61 62  .static int vtab
2de56 42 65 73 74 49 6e 64 65 78 28 50 61 72 73 65 20  BestIndex(Parse 
2de57 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a  *pParse, Table *
2de58 70 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e  pTab, sqlite3_in
2de59 64 65 78 5f 69 6e 66 6f 20 2a 70 29 7b 0a 20 20  dex_info *p){.  
2de5a 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
2de5b 74 61 62 20 3d 20 73 71 6c 69 74 65 33 47 65 74  tab = sqlite3Get
2de5c 56 54 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 64  VTable(pParse->d
2de5d 62 2c 20 70 54 61 62 29 2d 3e 70 56 74 61 62 3b  b, pTab)->pVtab;
2de5e 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
2de5f 72 63 3b 0a 0a 20 20 28 76 6f 69 64 29 73 71 6c  rc;..  (void)sql
2de60 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 70 50  ite3SafetyOff(pP
2de61 61 72 73 65 2d 3e 64 62 29 3b 0a 20 20 57 48 45  arse->db);.  WHE
2de62 52 45 54 52 41 43 45 28 28 22 78 42 65 73 74 49  RETRACE(("xBestI
2de63 6e 64 65 78 20 66 6f 72 20 25 73 5c 6e 22 2c 20  ndex for %s\n", 
2de64 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20  pTab->zName));. 
2de65 20 54 52 41 43 45 5f 49 44 58 5f 49 4e 50 55 54   TRACE_IDX_INPUT
2de66 53 28 70 29 3b 0a 20 20 72 63 20 3d 20 70 56 74  S(p);.  rc = pVt
2de67 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 42 65  ab->pModule->xBe
2de68 73 74 49 6e 64 65 78 28 70 56 74 61 62 2c 20 70  stIndex(pVtab, p
2de69 29 3b 0a 20 20 54 52 41 43 45 5f 49 44 58 5f 4f  );.  TRACE_IDX_O
2de6a 55 54 50 55 54 53 28 70 29 3b 0a 20 20 28 76 6f  UTPUTS(p);.  (vo
2de6b 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
2de6c 4f 6e 28 70 50 61 72 73 65 2d 3e 64 62 29 3b 0a  On(pParse->db);.
2de6d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
2de6e 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20  E_OK ){.    if( 
2de6f 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
2de70 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65   ){.      pParse
2de71 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
2de72 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73  ed = 1;.    }els
2de73 65 20 69 66 28 20 21 70 56 74 61 62 2d 3e 7a 45  e if( !pVtab->zE
2de74 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 73  rrMsg ){.      s
2de75 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2de76 50 61 72 73 65 2c 20 22 25 73 22 2c 20 73 71 6c  Parse, "%s", sql
2de77 69 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b  ite3ErrStr(rc));
2de78 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2de79 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2de7a 67 28 70 50 61 72 73 65 2c 20 22 25 73 22 2c 20  g(pParse, "%s", 
2de7b 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 29 3b  pVtab->zErrMsg);
2de7c 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c  .    }.  }.  sql
2de7d 69 74 65 33 44 62 46 72 65 65 28 70 50 61 72 73  ite3DbFree(pPars
2de7e 65 2d 3e 64 62 2c 20 70 56 74 61 62 2d 3e 7a 45  e->db, pVtab->zE
2de7f 72 72 4d 73 67 29 3b 0a 20 20 70 56 74 61 62 2d  rrMsg);.  pVtab-
2de80 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 0a 20  >zErrMsg = 0;.. 
2de81 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e   for(i=0; i<p->n
2de82 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b 29  Constraint; i++)
2de83 7b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 61 43  {.    if( !p->aC
2de84 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 75 73 61  onstraint[i].usa
2de85 62 6c 65 20 26 26 20 70 2d 3e 61 43 6f 6e 73 74  ble && p->aConst
2de86 72 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e 61 72  raintUsage[i].ar
2de87 67 76 49 6e 64 65 78 3e 30 20 29 7b 0a 20 20 20  gvIndex>0 ){.   
2de88 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2de89 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  sg(pParse, .    
2de8a 20 20 20 20 20 20 22 74 61 62 6c 65 20 25 73 3a        "table %s:
2de8b 20 78 42 65 73 74 49 6e 64 65 78 20 72 65 74 75   xBestIndex retu
2de8c 72 6e 65 64 20 61 6e 20 69 6e 76 61 6c 69 64 20  rned an invalid 
2de8d 70 6c 61 6e 22 2c 20 70 54 61 62 2d 3e 7a 4e 61  plan", pTab->zNa
2de8e 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  me);.    }.  }..
2de8f 20 20 72 65 74 75 72 6e 20 70 50 61 72 73 65 2d    return pParse-
2de90 3e 6e 45 72 72 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  >nErr;.}.../*.**
2de91 20 43 6f 6d 70 75 74 65 20 74 68 65 20 62 65 73   Compute the bes
2de92 74 20 69 6e 64 65 78 20 66 6f 72 20 61 20 76 69  t index for a vi
2de93 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a  rtual table..**.
2de94 2a 2a 20 54 68 65 20 62 65 73 74 20 69 6e 64 65  ** The best inde
2de95 78 20 69 73 20 63 6f 6d 70 75 74 65 64 20 62 79  x is computed by
2de96 20 74 68 65 20 78 42 65 73 74 49 6e 64 65 78 20   the xBestIndex 
2de97 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 76 69  method of the vi
2de98 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 6d  rtual.** table m
2de99 6f 64 75 6c 65 2e 20 20 54 68 69 73 20 72 6f 75  odule.  This rou
2de9a 74 69 6e 65 20 69 73 20 72 65 61 6c 6c 79 20 6a  tine is really j
2de9b 75 73 74 20 61 20 77 72 61 70 70 65 72 20 74 68  ust a wrapper th
2de9c 61 74 20 73 65 74 73 20 75 70 0a 2a 2a 20 74 68  at sets up.** th
2de9d 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  e sqlite3_index_
2de9e 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74  info structure t
2de9f 68 61 74 20 69 73 20 75 73 65 64 20 74 6f 20 63  hat is used to c
2dea0 6f 6d 6d 75 6e 69 63 61 74 65 20 77 69 74 68 0a  ommunicate with.
2dea1 2a 2a 20 78 42 65 73 74 49 6e 64 65 78 2e 0a 2a  ** xBestIndex..*
2dea2 2a 0a 2a 2a 20 49 6e 20 61 20 6a 6f 69 6e 2c 20  *.** In a join, 
2dea3 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67  this routine mig
2dea4 68 74 20 62 65 20 63 61 6c 6c 65 64 20 6d 75 6c  ht be called mul
2dea5 74 69 70 6c 65 20 74 69 6d 65 73 20 66 6f 72 20  tiple times for 
2dea6 74 68 65 0a 2a 2a 20 73 61 6d 65 20 76 69 72 74  the.** same virt
2dea7 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20  ual table.  The 
2dea8 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
2dea9 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 73 20  fo structure is 
2deaa 63 72 65 61 74 65 64 0a 2a 2a 20 61 6e 64 20 69  created.** and i
2deab 6e 69 74 69 61 6c 69 7a 65 64 20 6f 6e 20 74 68  nitialized on th
2deac 65 20 66 69 72 73 74 20 69 6e 76 6f 63 61 74 69  e first invocati
2dead 6f 6e 20 61 6e 64 20 72 65 75 73 65 64 20 6f 6e  on and reused on
2deae 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 0a   all subsequent.
2deaf 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 20  ** invocations. 
2deb0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64   The sqlite3_ind
2deb1 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72  ex_info structur
2deb2 65 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20 77  e is also used w
2deb3 68 65 6e 0a 2a 2a 20 63 6f 64 65 20 69 73 20 67  hen.** code is g
2deb4 65 6e 65 72 61 74 65 64 20 74 6f 20 61 63 63 65  enerated to acce
2deb5 73 73 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  ss the virtual t
2deb6 61 62 6c 65 2e 20 20 54 68 65 20 77 68 65 72 65  able.  The where
2deb7 49 6e 66 6f 44 65 6c 65 74 65 28 29 20 0a 2a 2a  InfoDelete() .**
2deb8 20 72 6f 75 74 69 6e 65 20 74 61 6b 65 73 20 63   routine takes c
2deb9 61 72 65 20 6f 66 20 66 72 65 65 69 6e 67 20 74  are of freeing t
2deba 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  he sqlite3_index
2debb 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  _info structure 
2debc 61 66 74 65 72 0a 2a 2a 20 65 76 65 72 79 62 6f  after.** everybo
2debd 64 79 20 68 61 73 20 66 69 6e 69 73 68 65 64 20  dy has finished 
2debe 77 69 74 68 20 69 74 2e 0a 2a 2f 0a 73 74 61 74  with it..*/.stat
2debf 69 63 20 76 6f 69 64 20 62 65 73 74 56 69 72 74  ic void bestVirt
2dec0 75 61 6c 49 6e 64 65 78 28 0a 20 20 50 61 72 73  ualIndex(.  Pars
2dec1 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2dec2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2dec3 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65  he parsing conte
2dec4 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61  xt */.  WhereCla
2dec5 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20  use *pWC,       
2dec6 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57          /* The W
2dec7 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
2dec8 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
2dec9 69 74 65 6d 20 2a 70 53 72 63 2c 20 20 20 20 20  item *pSrc,     
2deca 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61   /* The FROM cla
2decb 75 73 65 20 74 65 72 6d 20 74 6f 20 73 65 61 72  use term to sear
2decc 63 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20  ch */.  Bitmask 
2decd 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20 20 20  notReady,       
2dece 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20          /* Mask 
2decf 6f 66 20 63 75 72 73 6f 72 73 20 74 68 61 74 20  of cursors that 
2ded0 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c  are not availabl
2ded1 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  e */.  ExprList 
2ded2 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 20  *pOrderBy,      
2ded3 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72         /* The or
2ded4 64 65 72 20 62 79 20 63 6c 61 75 73 65 20 2a 2f  der by clause */
2ded5 0a 20 20 57 68 65 72 65 43 6f 73 74 20 2a 70 43  .  WhereCost *pC
2ded6 6f 73 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  ost,            
2ded7 20 20 20 2f 2a 20 4c 6f 77 65 73 74 20 63 6f 73     /* Lowest cos
2ded8 74 20 71 75 65 72 79 20 70 6c 61 6e 20 2a 2f 0a  t query plan */.
2ded9 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f    sqlite3_index_
2deda 69 6e 66 6f 20 2a 2a 70 70 49 64 78 49 6e 66 6f  info **ppIdxInfo
2dedb 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 66 6f 72    /* Index infor
2dedc 6d 61 74 69 6f 6e 20 70 61 73 73 65 64 20 74 6f  mation passed to
2dedd 20 78 42 65 73 74 49 6e 64 65 78 20 2a 2f 0a 29   xBestIndex */.)
2dede 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20  {.  Table *pTab 
2dedf 3d 20 70 53 72 63 2d 3e 70 54 61 62 3b 0a 20 20  = pSrc->pTab;.  
2dee0 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
2dee1 66 6f 20 2a 70 49 64 78 49 6e 66 6f 3b 0a 20 20  fo *pIdxInfo;.  
2dee2 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
2dee3 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20  ndex_constraint 
2dee4 2a 70 49 64 78 43 6f 6e 73 3b 0a 20 20 73 74 72  *pIdxCons;.  str
2dee5 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
2dee6 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61  x_constraint_usa
2dee7 67 65 20 2a 70 55 73 61 67 65 3b 0a 20 20 57 68  ge *pUsage;.  Wh
2dee8 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0a  ereTerm *pTerm;.
2dee9 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 6e    int i, j;.  in
2deea 74 20 6e 4f 72 64 65 72 42 79 3b 0a 0a 20 20 2f  t nOrderBy;..  /
2deeb 2a 20 4d 61 6b 65 20 73 75 72 65 20 77 73 46 6c  * Make sure wsFl
2deec 61 67 73 20 69 73 20 69 6e 69 74 69 61 6c 69 7a  ags is initializ
2deed 65 64 20 74 6f 20 73 6f 6d 65 20 73 61 6e 65 20  ed to some sane 
2deee 76 61 6c 75 65 2e 20 4f 74 68 65 72 77 69 73 65  value. Otherwise
2deef 2c 20 69 66 20 74 68 65 20 0a 20 20 2a 2a 20 6d  , if the .  ** m
2def0 61 6c 6c 6f 63 20 69 6e 20 61 6c 6c 6f 63 61 74  alloc in allocat
2def1 65 49 6e 64 65 78 49 6e 66 6f 28 29 20 66 61 69  eIndexInfo() fai
2def2 6c 73 20 61 6e 64 20 74 68 69 73 20 66 75 6e 63  ls and this func
2def3 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 6c 65 61  tion returns lea
2def4 76 69 6e 67 0a 20 20 2a 2a 20 77 73 46 6c 61 67  ving.  ** wsFlag
2def5 73 20 69 6e 20 61 6e 20 75 6e 69 6e 69 74 69 61  s in an uninitia
2def6 6c 69 7a 65 64 20 73 74 61 74 65 2c 20 74 68 65  lized state, the
2def7 20 63 61 6c 6c 65 72 20 6d 61 79 20 62 65 68 61   caller may beha
2def8 76 65 20 75 6e 70 72 65 64 69 63 74 61 62 6c 79  ve unpredictably
2def9 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28  ..  */.  memset(
2defa 70 43 6f 73 74 2c 20 30 2c 20 73 69 7a 65 6f 66  pCost, 0, sizeof
2defb 28 2a 70 43 6f 73 74 29 29 3b 0a 20 20 70 43 6f  (*pCost));.  pCo
2defc 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  st->plan.wsFlags
2defd 20 3d 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c   = WHERE_VIRTUAL
2defe 54 41 42 4c 45 3b 0a 0a 20 20 2f 2a 20 49 66 20  TABLE;..  /* If 
2deff 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  the sqlite3_inde
2df00 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65  x_info structure
2df01 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 70 72   has not been pr
2df02 65 76 69 6f 75 73 6c 79 0a 20 20 2a 2a 20 61 6c  eviously.  ** al
2df03 6c 6f 63 61 74 65 64 20 61 6e 64 20 69 6e 69 74  located and init
2df04 69 61 6c 69 7a 65 64 2c 20 74 68 65 6e 20 61 6c  ialized, then al
2df05 6c 6f 63 61 74 65 20 61 6e 64 20 69 6e 69 74 69  locate and initi
2df06 61 6c 69 7a 65 20 69 74 20 6e 6f 77 2e 0a 20 20  alize it now..  
2df07 2a 2f 0a 20 20 70 49 64 78 49 6e 66 6f 20 3d 20  */.  pIdxInfo = 
2df08 2a 70 70 49 64 78 49 6e 66 6f 3b 0a 20 20 69 66  *ppIdxInfo;.  if
2df09 28 20 70 49 64 78 49 6e 66 6f 3d 3d 30 20 29 7b  ( pIdxInfo==0 ){
2df0a 0a 20 20 20 20 2a 70 70 49 64 78 49 6e 66 6f 20  .    *ppIdxInfo 
2df0b 3d 20 70 49 64 78 49 6e 66 6f 20 3d 20 61 6c 6c  = pIdxInfo = all
2df0c 6f 63 61 74 65 49 6e 64 65 78 49 6e 66 6f 28 70  ocateIndexInfo(p
2df0d 50 61 72 73 65 2c 20 70 57 43 2c 20 70 53 72 63  Parse, pWC, pSrc
2df0e 2c 20 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 7d  , pOrderBy);.  }
2df0f 0a 20 20 69 66 28 20 70 49 64 78 49 6e 66 6f 3d  .  if( pIdxInfo=
2df10 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
2df11 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74  ;.  }..  /* At t
2df12 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 20 73  his point, the s
2df13 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
2df14 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  o structure that
2df15 20 70 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74 73   pIdxInfo points
2df16 0a 20 20 2a 2a 20 74 6f 20 77 69 6c 6c 20 68 61  .  ** to will ha
2df17 76 65 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69  ve been initiali
2df18 7a 65 64 2c 20 65 69 74 68 65 72 20 64 75 72 69  zed, either duri
2df19 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 69  ng the current i
2df1a 6e 76 6f 63 61 74 69 6f 6e 20 6f 72 0a 20 20 2a  nvocation or.  *
2df1b 2a 20 64 75 72 69 6e 67 20 73 6f 6d 65 20 70 72  * during some pr
2df1c 69 6f 72 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 20  ior invocation. 
2df1d 20 4e 6f 77 20 77 65 20 6a 75 73 74 20 68 61 76   Now we just hav
2df1e 65 20 74 6f 20 63 75 73 74 6f 6d 69 7a 65 20 74  e to customize t
2df1f 68 65 0a 20 20 2a 2a 20 64 65 74 61 69 6c 73 20  he.  ** details 
2df20 6f 66 20 70 49 64 78 49 6e 66 6f 20 66 6f 72 20  of pIdxInfo for 
2df21 74 68 65 20 63 75 72 72 65 6e 74 20 69 6e 76 6f  the current invo
2df22 63 61 74 69 6f 6e 20 61 6e 64 20 70 61 73 73 20  cation and pass 
2df23 69 74 20 74 6f 0a 20 20 2a 2a 20 78 42 65 73 74  it to.  ** xBest
2df24 49 6e 64 65 78 2e 0a 20 20 2a 2f 0a 0a 20 20 2f  Index..  */..  /
2df25 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d  * The module nam
2df26 65 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65  e must be define
2df27 64 2e 20 41 6c 73 6f 2c 20 62 79 20 74 68 69 73  d. Also, by this
2df28 20 70 6f 69 6e 74 20 74 68 65 72 65 20 6d 75 73   point there mus
2df29 74 0a 20 20 2a 2a 20 62 65 20 61 20 70 6f 69 6e  t.  ** be a poin
2df2a 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65  ter to an sqlite
2df2b 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65  3_vtab structure
2df2c 2e 20 4f 74 68 65 72 77 69 73 65 0a 20 20 2a 2a  . Otherwise.  **
2df2d 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43   sqlite3ViewGetC
2df2e 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 77 6f 75  olumnNames() wou
2df2f 6c 64 20 68 61 76 65 20 70 69 63 6b 65 64 20 75  ld have picked u
2df30 70 20 74 68 65 20 65 72 72 6f 72 2e 20 0a 20 20  p the error. .  
2df31 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54 61  */.  assert( pTa
2df32 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 20 26  b->azModuleArg &
2df33 26 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65  & pTab->azModule
2df34 41 72 67 5b 30 5d 20 29 3b 0a 20 20 61 73 73 65  Arg[0] );.  asse
2df35 72 74 28 20 73 71 6c 69 74 65 33 47 65 74 56 54  rt( sqlite3GetVT
2df36 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  able(pParse->db,
2df37 20 70 54 61 62 29 20 29 3b 0a 0a 20 20 2f 2a 20   pTab) );..  /* 
2df38 53 65 74 20 74 68 65 20 61 43 6f 6e 73 74 72 61  Set the aConstra
2df39 69 6e 74 5b 5d 2e 75 73 61 62 6c 65 20 66 69 65  int[].usable fie
2df3a 6c 64 73 20 61 6e 64 20 69 6e 69 74 69 61 6c 69  lds and initiali
2df3b 7a 65 20 61 6c 6c 20 0a 20 20 2a 2a 20 6f 75 74  ze all .  ** out
2df3c 70 75 74 20 76 61 72 69 61 62 6c 65 73 20 74 6f  put variables to
2df3d 20 7a 65 72 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a   zero..  **.  **
2df3e 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 75   aConstraint[].u
2df3f 73 61 62 6c 65 20 69 73 20 74 72 75 65 20 66 6f  sable is true fo
2df40 72 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 77 68  r constraints wh
2df41 65 72 65 20 74 68 65 20 72 69 67 68 74 2d 68 61  ere the right-ha
2df42 6e 64 0a 20 20 2a 2a 20 73 69 64 65 20 63 6f 6e  nd.  ** side con
2df43 74 61 69 6e 73 20 6f 6e 6c 79 20 72 65 66 65 72  tains only refer
2df44 65 6e 63 65 73 20 74 6f 20 74 61 62 6c 65 73 20  ences to tables 
2df45 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20 74  to the left of t
2df46 68 65 20 63 75 72 72 65 6e 74 0a 20 20 2a 2a 20  he current.  ** 
2df47 74 61 62 6c 65 2e 20 20 49 6e 20 6f 74 68 65 72  table.  In other
2df48 20 77 6f 72 64 73 2c 20 69 66 20 74 68 65 20 63   words, if the c
2df49 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 6f 66 20  onstraint is of 
2df4a 74 68 65 20 66 6f 72 6d 3a 0a 20 20 2a 2a 0a 20  the form:.  **. 
2df4b 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 63 6f   **           co
2df4c 6c 75 6d 6e 20 3d 20 65 78 70 72 0a 20 20 2a 2a  lumn = expr.  **
2df4d 0a 20 20 2a 2a 20 61 6e 64 20 77 65 20 61 72 65  .  ** and we are
2df4e 20 65 76 61 6c 75 61 74 69 6e 67 20 61 20 6a 6f   evaluating a jo
2df4f 69 6e 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6e  in, then the con
2df50 73 74 72 61 69 6e 74 20 6f 6e 20 63 6f 6c 75 6d  straint on colum
2df51 6e 20 69 73 20 0a 20 20 2a 2a 20 6f 6e 6c 79 20  n is .  ** only 
2df52 76 61 6c 69 64 20 69 66 20 61 6c 6c 20 74 61 62  valid if all tab
2df53 6c 65 73 20 72 65 66 65 72 65 6e 63 65 64 20 69  les referenced i
2df54 6e 20 65 78 70 72 20 6f 63 63 75 72 20 74 6f 20  n expr occur to 
2df55 74 68 65 20 6c 65 66 74 0a 20 20 2a 2a 20 6f 66  the left.  ** of
2df56 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61   the table conta
2df57 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2e 0a 20 20  ining column..  
2df58 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 61 43 6f 6e  **.  ** The aCon
2df59 73 74 72 61 69 6e 74 73 5b 5d 20 61 72 72 61 79  straints[] array
2df5a 20 63 6f 6e 74 61 69 6e 73 20 65 6e 74 72 69 65   contains entrie
2df5b 73 20 66 6f 72 20 61 6c 6c 20 63 6f 6e 73 74 72  s for all constr
2df5c 61 69 6e 74 73 0a 20 20 2a 2a 20 6f 6e 20 74 68  aints.  ** on th
2df5d 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65 2e  e current table.
2df5e 20 20 54 68 61 74 20 77 61 79 20 77 65 20 6f 6e    That way we on
2df5f 6c 79 20 68 61 76 65 20 74 6f 20 63 6f 6d 70 75  ly have to compu
2df60 74 65 20 69 74 20 6f 6e 63 65 0a 20 20 2a 2a 20  te it once.  ** 
2df61 65 76 65 6e 20 74 68 6f 75 67 68 20 77 65 20 6d  even though we m
2df62 69 67 68 74 20 74 72 79 20 74 6f 20 70 69 63 6b  ight try to pick
2df63 20 74 68 65 20 62 65 73 74 20 69 6e 64 65 78 20   the best index 
2df64 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 2e 0a  multiple times..
2df65 20 20 2a 2a 20 46 6f 72 20 65 61 63 68 20 61 74    ** For each at
2df66 74 65 6d 70 74 20 61 74 20 70 69 63 6b 69 6e 67  tempt at picking
2df67 20 61 6e 20 69 6e 64 65 78 2c 20 74 68 65 20 6f   an index, the o
2df68 72 64 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69  rder of tables i
2df69 6e 20 74 68 65 0a 20 20 2a 2a 20 6a 6f 69 6e 20  n the.  ** join 
2df6a 6d 69 67 68 74 20 62 65 20 64 69 66 66 65 72 65  might be differe
2df6b 6e 74 20 73 6f 20 77 65 20 68 61 76 65 20 74 6f  nt so we have to
2df6c 20 72 65 63 6f 6d 70 75 74 65 20 74 68 65 20 75   recompute the u
2df6d 73 61 62 6c 65 20 66 6c 61 67 0a 20 20 2a 2a 20  sable flag.  ** 
2df6e 65 61 63 68 20 74 69 6d 65 2e 0a 20 20 2a 2f 0a  each time..  */.
2df6f 20 20 70 49 64 78 43 6f 6e 73 20 3d 20 2a 28 73    pIdxCons = *(s
2df70 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
2df71 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 2a 2a  dex_constraint**
2df72 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e  )&pIdxInfo->aCon
2df73 73 74 72 61 69 6e 74 3b 0a 20 20 70 55 73 61 67  straint;.  pUsag
2df74 65 20 3d 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43  e = pIdxInfo->aC
2df75 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 3b 0a  onstraintUsage;.
2df76 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64    for(i=0; i<pId
2df77 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69  xInfo->nConstrai
2df78 6e 74 3b 20 69 2b 2b 2c 20 70 49 64 78 43 6f 6e  nt; i++, pIdxCon
2df79 73 2b 2b 29 7b 0a 20 20 20 20 6a 20 3d 20 70 49  s++){.    j = pI
2df7a 64 78 43 6f 6e 73 2d 3e 69 54 65 72 6d 4f 66 66  dxCons->iTermOff
2df7b 73 65 74 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d  set;.    pTerm =
2df7c 20 26 70 57 43 2d 3e 61 5b 6a 5d 3b 0a 20 20 20   &pWC->a[j];.   
2df7d 20 70 49 64 78 43 6f 6e 73 2d 3e 75 73 61 62 6c   pIdxCons->usabl
2df7e 65 20 3d 20 28 70 54 65 72 6d 2d 3e 70 72 65 72  e = (pTerm->prer
2df7f 65 71 52 69 67 68 74 26 6e 6f 74 52 65 61 64 79  eqRight&notReady
2df80 29 20 3f 20 30 20 3a 20 31 3b 0a 20 20 7d 0a 20  ) ? 0 : 1;.  }. 
2df81 20 6d 65 6d 73 65 74 28 70 55 73 61 67 65 2c 20   memset(pUsage, 
2df82 30 2c 20 73 69 7a 65 6f 66 28 70 55 73 61 67 65  0, sizeof(pUsage
2df83 5b 30 5d 29 2a 70 49 64 78 49 6e 66 6f 2d 3e 6e  [0])*pIdxInfo->n
2df84 43 6f 6e 73 74 72 61 69 6e 74 29 3b 0a 20 20 69  Constraint);.  i
2df85 66 28 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 65 65  f( pIdxInfo->nee
2df86 64 54 6f 46 72 65 65 49 64 78 53 74 72 20 29 7b  dToFreeIdxStr ){
2df87 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
2df88 65 28 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78 53  e(pIdxInfo->idxS
2df89 74 72 29 3b 0a 20 20 7d 0a 20 20 70 49 64 78 49  tr);.  }.  pIdxI
2df8a 6e 66 6f 2d 3e 69 64 78 53 74 72 20 3d 20 30 3b  nfo->idxStr = 0;
2df8b 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78  .  pIdxInfo->idx
2df8c 4e 75 6d 20 3d 20 30 3b 0a 20 20 70 49 64 78 49  Num = 0;.  pIdxI
2df8d 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49  nfo->needToFreeI
2df8e 64 78 53 74 72 20 3d 20 30 3b 0a 20 20 70 49 64  dxStr = 0;.  pId
2df8f 78 49 6e 66 6f 2d 3e 6f 72 64 65 72 42 79 43 6f  xInfo->orderByCo
2df90 6e 73 75 6d 65 64 20 3d 20 30 3b 0a 20 20 2f 2a  nsumed = 0;.  /*
2df91 20 28 28 64 6f 75 62 6c 65 29 32 29 20 49 6e 20   ((double)2) In 
2df92 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f  case of SQLITE_O
2df93 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
2df94 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 70 49 64 78 49  NT... */.  pIdxI
2df95 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f  nfo->estimatedCo
2df96 73 74 20 3d 20 53 51 4c 49 54 45 5f 42 49 47 5f  st = SQLITE_BIG_
2df97 44 42 4c 20 2f 20 28 28 64 6f 75 62 6c 65 29 32  DBL / ((double)2
2df98 29 3b 0a 20 20 6e 4f 72 64 65 72 42 79 20 3d 20  );.  nOrderBy = 
2df99 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72  pIdxInfo->nOrder
2df9a 42 79 3b 0a 20 20 69 66 28 20 21 70 4f 72 64 65  By;.  if( !pOrde
2df9b 72 42 79 20 29 7b 0a 20 20 20 20 70 49 64 78 49  rBy ){.    pIdxI
2df9c 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79 20 3d 20  nfo->nOrderBy = 
2df9d 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 76 74  0;.  }..  if( vt
2df9e 61 62 42 65 73 74 49 6e 64 65 78 28 70 50 61 72  abBestIndex(pPar
2df9f 73 65 2c 20 70 54 61 62 2c 20 70 49 64 78 49 6e  se, pTab, pIdxIn
2dfa0 66 6f 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  fo) ){.    retur
2dfa1 6e 3b 0a 20 20 7d 0a 0a 20 20 70 49 64 78 43 6f  n;.  }..  pIdxCo
2dfa2 6e 73 20 3d 20 2a 28 73 74 72 75 63 74 20 73 71  ns = *(struct sq
2dfa3 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73  lite3_index_cons
2dfa4 74 72 61 69 6e 74 2a 2a 29 26 70 49 64 78 49 6e  traint**)&pIdxIn
2dfa5 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 3b  fo->aConstraint;
2dfa6 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49  .  for(i=0; i<pI
2dfa7 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61  dxInfo->nConstra
2dfa8 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  int; i++){.    i
2dfa9 66 28 20 70 55 73 61 67 65 5b 69 5d 2e 61 72 67  f( pUsage[i].arg
2dfaa 76 49 6e 64 65 78 3e 30 20 29 7b 0a 20 20 20 20  vIndex>0 ){.    
2dfab 20 20 70 43 6f 73 74 2d 3e 75 73 65 64 20 7c 3d    pCost->used |=
2dfac 20 70 57 43 2d 3e 61 5b 70 49 64 78 43 6f 6e 73   pWC->a[pIdxCons
2dfad 5b 69 5d 2e 69 54 65 72 6d 4f 66 66 73 65 74 5d  [i].iTermOffset]
2dfae 2e 70 72 65 72 65 71 52 69 67 68 74 3b 0a 20 20  .prereqRight;.  
2dfaf 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68    }.  }..  /* Th
2dfb0 65 20 63 6f 73 74 20 69 73 20 6e 6f 74 20 61 6c  e cost is not al
2dfb1 6c 6f 77 65 64 20 74 6f 20 62 65 20 6c 61 72 67  lowed to be larg
2dfb2 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 42  er than SQLITE_B
2dfb3 49 47 5f 44 42 4c 20 28 74 68 65 0a 20 20 2a 2a  IG_DBL (the.  **
2dfb4 20 69 6e 69 74 61 6c 20 76 61 6c 75 65 20 6f 66   inital value of
2dfb5 20 6c 6f 77 65 73 74 43 6f 73 74 20 69 6e 20 74   lowestCost in t
2dfb6 68 69 73 20 6c 6f 6f 70 2e 20 49 66 20 69 74 20  his loop. If it 
2dfb7 69 73 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 2a  is, then the.  *
2dfb8 2a 20 28 63 6f 73 74 3c 6c 6f 77 65 73 74 43 6f  * (cost<lowestCo
2dfb9 73 74 29 20 74 65 73 74 20 62 65 6c 6f 77 20 77  st) test below w
2dfba 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 74 72 75  ill never be tru
2dfbb 65 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 55 73  e..  ** .  ** Us
2dfbc 65 20 22 28 64 6f 75 62 6c 65 29 32 22 20 69 6e  e "(double)2" in
2dfbd 73 74 65 61 64 20 6f 66 20 22 32 2e 30 22 20 69  stead of "2.0" i
2dfbe 6e 20 63 61 73 65 20 4f 4d 49 54 5f 46 4c 4f 41  n case OMIT_FLOA
2dfbf 54 49 4e 47 5f 50 4f 49 4e 54 20 0a 20 20 2a 2a  TING_POINT .  **
2dfc0 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 20 20 2a   is defined..  *
2dfc1 2f 0a 20 20 69 66 28 20 28 53 51 4c 49 54 45 5f  /.  if( (SQLITE_
2dfc2 42 49 47 5f 44 42 4c 2f 28 28 64 6f 75 62 6c 65  BIG_DBL/((double
2dfc3 29 32 29 29 3c 70 49 64 78 49 6e 66 6f 2d 3e 65  )2))<pIdxInfo->e
2dfc4 73 74 69 6d 61 74 65 64 43 6f 73 74 20 29 7b 0a  stimatedCost ){.
2dfc5 20 20 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74      pCost->rCost
2dfc6 20 3d 20 28 53 51 4c 49 54 45 5f 42 49 47 5f 44   = (SQLITE_BIG_D
2dfc7 42 4c 2f 28 28 64 6f 75 62 6c 65 29 32 29 29 3b  BL/((double)2));
2dfc8 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43  .  }else{.    pC
2dfc9 6f 73 74 2d 3e 72 43 6f 73 74 20 3d 20 70 49 64  ost->rCost = pId
2dfca 78 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64  xInfo->estimated
2dfcb 43 6f 73 74 3b 0a 20 20 7d 0a 20 20 70 43 6f 73  Cost;.  }.  pCos
2dfcc 74 2d 3e 70 6c 61 6e 2e 75 2e 70 56 74 61 62 49  t->plan.u.pVtabI
2dfcd 64 78 20 3d 20 70 49 64 78 49 6e 66 6f 3b 0a 20  dx = pIdxInfo;. 
2dfce 20 69 66 28 20 70 49 64 78 49 6e 66 6f 2d 3e 6f   if( pIdxInfo->o
2dfcf 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 20 29  rderByConsumed )
2dfd0 7b 0a 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61  {.    pCost->pla
2dfd1 6e 2e 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45  n.wsFlags |= WHE
2dfd2 52 45 5f 4f 52 44 45 52 42 59 3b 0a 20 20 7d 0a  RE_ORDERBY;.  }.
2dfd3 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 6e 45    pCost->plan.nE
2dfd4 71 20 3d 20 30 3b 0a 20 20 70 49 64 78 49 6e 66  q = 0;.  pIdxInf
2dfd5 6f 2d 3e 6e 4f 72 64 65 72 42 79 20 3d 20 6e 4f  o->nOrderBy = nO
2dfd6 72 64 65 72 42 79 3b 0a 0a 20 20 2f 2a 20 54 72  rderBy;..  /* Tr
2dfd7 79 20 74 6f 20 66 69 6e 64 20 61 20 6d 6f 72 65  y to find a more
2dfd8 20 65 66 66 69 63 69 65 6e 74 20 61 63 63 65 73   efficient acces
2dfd9 73 20 70 61 74 74 65 72 6e 20 62 79 20 75 73 69  s pattern by usi
2dfda 6e 67 20 6d 75 6c 74 69 70 6c 65 20 69 6e 64 65  ng multiple inde
2dfdb 78 65 73 0a 20 20 2a 2a 20 74 6f 20 6f 70 74 69  xes.  ** to opti
2dfdc 6d 69 7a 65 20 61 6e 20 4f 52 20 65 78 70 72 65  mize an OR expre
2dfdd 73 73 69 6f 6e 20 77 69 74 68 69 6e 20 74 68 65  ssion within the
2dfde 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 0a   WHERE clause. .
2dfdf 20 20 2a 2f 0a 20 20 62 65 73 74 4f 72 43 6c 61    */.  bestOrCla
2dfe0 75 73 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c  useIndex(pParse,
2dfe1 20 70 57 43 2c 20 70 53 72 63 2c 20 6e 6f 74 52   pWC, pSrc, notR
2dfe2 65 61 64 79 2c 20 70 4f 72 64 65 72 42 79 2c 20  eady, pOrderBy, 
2dfe3 70 43 6f 73 74 29 3b 0a 7d 0a 23 65 6e 64 69 66  pCost);.}.#endif
2dfe4 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2dfe5 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a  VIRTUALTABLE */.
2dfe6 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20  ./*.** Argument 
2dfe7 70 49 64 78 20 69 73 20 61 20 70 6f 69 6e 74 65  pIdx is a pointe
2dfe8 72 20 74 6f 20 61 6e 20 69 6e 64 65 78 20 73 74  r to an index st
2dfe9 72 75 63 74 75 72 65 20 74 68 61 74 20 68 61 73  ructure that has
2dfea 20 61 6e 20 61 72 72 61 79 20 6f 66 0a 2a 2a 20   an array of.** 
2dfeb 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d  SQLITE_INDEX_SAM
2dfec 50 4c 45 53 20 65 76 65 6e 6c 79 20 73 70 61 63  PLES evenly spac
2dfed 65 64 20 73 61 6d 70 6c 65 73 20 6f 66 20 74 68  ed samples of th
2dfee 65 20 66 69 72 73 74 20 69 6e 64 65 78 65 64 20  e first indexed 
2dfef 63 6f 6c 75 6d 6e 0a 2a 2a 20 73 74 6f 72 65 64  column.** stored
2dff0 20 69 6e 20 49 6e 64 65 78 2e 61 53 61 6d 70 6c   in Index.aSampl
2dff1 65 2e 20 54 68 65 20 64 6f 6d 61 69 6e 20 6f 66  e. The domain of
2dff2 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69   values stored i
2dff3 6e 20 73 61 69 64 20 63 6f 6c 75 6d 6e 0a 2a 2a  n said column.**
2dff4 20 6d 61 79 20 62 65 20 74 68 6f 75 67 68 74 20   may be thought 
2dff5 6f 66 20 61 73 20 64 69 76 69 64 65 64 20 69 6e  of as divided in
2dff6 74 6f 20 28 53 51 4c 49 54 45 5f 49 4e 44 45 58  to (SQLITE_INDEX
2dff7 5f 53 41 4d 50 4c 45 53 2b 31 29 20 72 65 67 69  _SAMPLES+1) regi
2dff8 6f 6e 73 2e 0a 2a 2a 20 52 65 67 69 6f 6e 20 30  ons..** Region 0
2dff9 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 76 61   contains all va
2dffa 6c 75 65 73 20 73 6d 61 6c 6c 65 72 20 74 68 61  lues smaller tha
2dffb 6e 20 74 68 65 20 66 69 72 73 74 20 73 61 6d 70  n the first samp
2dffc 6c 65 20 76 61 6c 75 65 2e 20 52 65 67 69 6f 6e  le value. Region
2dffd 0a 2a 2a 20 31 20 63 6f 6e 74 61 69 6e 73 20 76  .** 1 contains v
2dffe 61 6c 75 65 73 20 6c 61 72 67 65 72 20 74 68 61  alues larger tha
2dfff 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68  n or equal to th
2e000 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 66  e value of the f
2e001 69 72 73 74 20 73 61 6d 70 6c 65 2c 0a 2a 2a 20  irst sample,.** 
2e002 62 75 74 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e  but smaller than
2e003 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
2e004 65 20 73 65 63 6f 6e 64 2e 20 41 6e 64 20 73 6f  e second. And so
2e005 20 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75   on..**.** If su
2e006 63 63 65 73 73 66 75 6c 2c 20 74 68 69 73 20 66  ccessful, this f
2e007 75 6e 63 74 69 6f 6e 20 64 65 74 65 72 6d 69 6e  unction determin
2e008 65 73 20 77 68 69 63 68 20 6f 66 20 74 68 65 20  es which of the 
2e009 72 65 67 69 6f 6e 73 20 76 61 6c 75 65 20 0a 2a  regions value .*
2e00a 2a 20 70 56 61 6c 20 6c 69 65 73 20 69 6e 2c 20  * pVal lies in, 
2e00b 73 65 74 73 20 2a 70 69 52 65 67 69 6f 6e 20 74  sets *piRegion t
2e00c 6f 20 74 68 65 20 72 65 67 69 6f 6e 20 69 6e 64  o the region ind
2e00d 65 78 20 28 61 20 76 61 6c 75 65 20 62 65 74 77  ex (a value betw
2e00e 65 65 6e 20 30 0a 2a 2a 20 61 6e 64 20 53 51 4c  een 0.** and SQL
2e00f 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45  ITE_INDEX_SAMPLE
2e010 53 2b 31 2c 20 69 6e 63 6c 75 73 69 76 65 29 20  S+1, inclusive) 
2e011 61 6e 64 20 72 65 74 75 72 6e 73 20 53 51 4c 49  and returns SQLI
2e012 54 45 5f 4f 4b 2e 0a 2a 2a 20 4f 72 2c 20 69 66  TE_OK..** Or, if
2e013 20 61 6e 20 4f 4f 4d 20 6f 63 63 75 72 73 20 77   an OOM occurs w
2e014 68 69 6c 65 20 63 6f 6e 76 65 72 74 69 6e 67 20  hile converting 
2e015 74 65 78 74 20 76 61 6c 75 65 73 20 62 65 74 77  text values betw
2e016 65 65 6e 20 65 6e 63 6f 64 69 6e 67 73 2c 0a 2a  een encodings,.*
2e017 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69  * SQLITE_NOMEM i
2e018 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a  s returned and *
2e019 70 69 52 65 67 69 6f 6e 20 69 73 20 75 6e 64 65  piRegion is unde
2e01a 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66  fined..*/.#ifdef
2e01b 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53   SQLITE_ENABLE_S
2e01c 54 41 54 32 0a 73 74 61 74 69 63 20 69 6e 74 20  TAT2.static int 
2e01d 77 68 65 72 65 52 61 6e 67 65 52 65 67 69 6f 6e  whereRangeRegion
2e01e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2e01f 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
2e020 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e  /* Database conn
2e021 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 49 6e 64 65  ection */.  Inde
2e022 78 20 2a 70 49 64 78 2c 20 20 20 20 20 20 20 20  x *pIdx,        
2e023 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
2e024 20 74 6f 20 63 6f 6e 73 69 64 65 72 20 64 6f 6d   to consider dom
2e025 61 69 6e 20 6f 66 20 2a 2f 0a 20 20 73 71 6c 69  ain of */.  sqli
2e026 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c  te3_value *pVal,
2e027 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
2e028 20 74 6f 20 63 6f 6e 73 69 64 65 72 20 2a 2f 0a   to consider */.
2e029 20 20 69 6e 74 20 2a 70 69 52 65 67 69 6f 6e 20    int *piRegion 
2e02a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2e02b 20 4f 55 54 3a 20 52 65 67 69 6f 6e 20 6f 66 20   OUT: Region of 
2e02c 64 6f 6d 61 69 6e 20 69 6e 20 77 68 69 63 68 20  domain in which 
2e02d 76 61 6c 75 65 20 6c 69 65 73 20 2a 2f 0a 29 7b  value lies */.){
2e02e 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 56  .  if( ALWAYS(pV
2e02f 61 6c 29 20 29 7b 0a 20 20 20 20 49 6e 64 65 78  al) ){.    Index
2e030 53 61 6d 70 6c 65 20 2a 61 53 61 6d 70 6c 65 20  Sample *aSample 
2e031 3d 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 3b  = pIdx->aSample;
2e032 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a  .    int i = 0;.
2e033 20 20 20 20 69 6e 74 20 65 54 79 70 65 20 3d 20      int eType = 
2e034 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
2e035 70 65 28 70 56 61 6c 29 3b 0a 0a 20 20 20 20 69  pe(pVal);..    i
2e036 66 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45  f( eType==SQLITE
2e037 5f 49 4e 54 45 47 45 52 20 7c 7c 20 65 54 79 70  _INTEGER || eTyp
2e038 65 3d 3d 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20  e==SQLITE_FLOAT 
2e039 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20  ){.      double 
2e03a 72 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  r = sqlite3_valu
2e03b 65 5f 64 6f 75 62 6c 65 28 70 56 61 6c 29 3b 0a  e_double(pVal);.
2e03c 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
2e03d 3c 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41  <SQLITE_INDEX_SA
2e03e 4d 50 4c 45 53 3b 20 69 2b 2b 29 7b 0a 20 20 20  MPLES; i++){.   
2e03f 20 20 20 20 20 69 66 28 20 61 53 61 6d 70 6c 65       if( aSample
2e040 5b 69 5d 2e 65 54 79 70 65 3d 3d 53 51 4c 49 54  [i].eType==SQLIT
2e041 45 5f 4e 55 4c 4c 20 29 20 63 6f 6e 74 69 6e 75  E_NULL ) continu
2e042 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61  e;.        if( a
2e043 53 61 6d 70 6c 65 5b 69 5d 2e 65 54 79 70 65 3e  Sample[i].eType>
2e044 3d 53 51 4c 49 54 45 5f 54 45 58 54 20 7c 7c 20  =SQLITE_TEXT || 
2e045 61 53 61 6d 70 6c 65 5b 69 5d 2e 75 2e 72 3e 72  aSample[i].u.r>r
2e046 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
2e047 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 20 0a 20 20  }.    }else{ .  
2e048 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20      sqlite3 *db 
2e049 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2e04a 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
2e04b 6c 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20  ll;.      const 
2e04c 75 38 20 2a 7a 3b 0a 20 20 20 20 20 20 69 6e 74  u8 *z;.      int
2e04d 20 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 70 56   n;..      /* pV
2e04e 61 6c 20 63 6f 6d 65 73 20 66 72 6f 6d 20 73 71  al comes from sq
2e04f 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78  lite3ValueFromEx
2e050 70 72 28 29 20 73 6f 20 74 68 65 20 74 79 70 65  pr() so the type
2e051 20 63 61 6e 6e 6f 74 20 62 65 20 4e 55 4c 4c 20   cannot be NULL 
2e052 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
2e053 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 54   eType==SQLITE_T
2e054 45 58 54 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51  EXT || eType==SQ
2e055 4c 49 54 45 5f 42 4c 4f 42 20 29 3b 0a 0a 20 20  LITE_BLOB );..  
2e056 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 53      if( eType==S
2e057 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20  QLITE_BLOB ){.  
2e058 20 20 20 20 20 20 7a 20 3d 20 28 63 6f 6e 73 74        z = (const
2e059 20 75 38 20 2a 29 73 71 6c 69 74 65 33 5f 76 61   u8 *)sqlite3_va
2e05a 6c 75 65 5f 62 6c 6f 62 28 70 56 61 6c 29 3b 0a  lue_blob(pVal);.
2e05b 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20          pColl = 
2e05c 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20  db->pDfltColl;. 
2e05d 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
2e05e 43 6f 6c 6c 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54  Coll->enc==SQLIT
2e05f 45 5f 55 54 46 38 20 29 3b 0a 20 20 20 20 20 20  E_UTF8 );.      
2e060 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
2e061 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65  Coll = sqlite3Ge
2e062 74 43 6f 6c 6c 53 65 71 28 64 62 2c 20 53 51 4c  tCollSeq(db, SQL
2e063 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 2a 70 49  ITE_UTF8, 0, *pI
2e064 64 78 2d 3e 61 7a 43 6f 6c 6c 29 3b 0a 20 20 20  dx->azColl);.   
2e065 20 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 3d 3d       if( pColl==
2e066 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  0 ){.          s
2e067 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2e068 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20  Parse, "no such 
2e069 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
2e06a 63 65 3a 20 25 73 22 2c 0a 20 20 20 20 20 20 20  ce: %s",.       
2e06b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e06c 20 20 20 2a 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c     *pIdx->azColl
2e06d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  );.          ret
2e06e 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
2e06f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2e070 20 20 20 20 7a 20 3d 20 28 63 6f 6e 73 74 20 75      z = (const u
2e071 38 20 2a 29 73 71 6c 69 74 65 33 56 61 6c 75 65  8 *)sqlite3Value
2e072 54 65 78 74 28 70 56 61 6c 2c 20 70 43 6f 6c 6c  Text(pVal, pColl
2e073 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 20 20  ->enc);.        
2e074 69 66 28 20 21 7a 20 29 7b 0a 20 20 20 20 20 20  if( !z ){.      
2e075 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2e076 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20  E_NOMEM;.       
2e077 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72   }.        asser
2e078 74 28 20 7a 20 26 26 20 70 43 6f 6c 6c 20 26 26  t( z && pColl &&
2e079 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 29 3b 0a   pColl->xCmp );.
2e07a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 20        }.      n 
2e07b 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79  = sqlite3ValueBy
2e07c 74 65 73 28 70 56 61 6c 2c 20 70 43 6f 6c 6c 2d  tes(pVal, pColl-
2e07d 3e 65 6e 63 29 3b 0a 0a 20 20 20 20 20 20 66 6f  >enc);..      fo
2e07e 72 28 69 3d 30 3b 20 69 3c 53 51 4c 49 54 45 5f  r(i=0; i<SQLITE_
2e07f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 3b 20 69  INDEX_SAMPLES; i
2e080 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  ++){.        int
2e081 20 72 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   r;.        int 
2e082 65 53 61 6d 70 6c 65 74 79 70 65 20 3d 20 61 53  eSampletype = aS
2e083 61 6d 70 6c 65 5b 69 5d 2e 65 54 79 70 65 3b 0a  ample[i].eType;.
2e084 20 20 20 20 20 20 20 20 69 66 28 20 65 53 61 6d          if( eSam
2e085 70 6c 65 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f  pletype==SQLITE_
2e086 4e 55 4c 4c 20 7c 7c 20 65 53 61 6d 70 6c 65 74  NULL || eSamplet
2e087 79 70 65 3c 65 54 79 70 65 20 29 20 63 6f 6e 74  ype<eType ) cont
2e088 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 69 66  inue;.        if
2e089 28 20 28 65 53 61 6d 70 6c 65 74 79 70 65 21 3d  ( (eSampletype!=
2e08a 65 54 79 70 65 29 20 29 20 62 72 65 61 6b 3b 0a  eType) ) break;.
2e08b 20 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c          if( pCol
2e08c 6c 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  l->enc==SQLITE_U
2e08d 54 46 38 20 29 7b 0a 20 20 20 20 20 20 20 20 20  TF8 ){.         
2e08e 20 72 20 3d 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70   r = pColl->xCmp
2e08f 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 20 61  (pColl->pUser, a
2e090 53 61 6d 70 6c 65 5b 69 5d 2e 6e 42 79 74 65 2c  Sample[i].nByte,
2e091 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 75 2e 7a 2c   aSample[i].u.z,
2e092 20 6e 2c 20 7a 29 3b 0a 20 20 20 20 20 20 20 20   n, z);.        
2e093 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
2e094 20 69 6e 74 20 6e 53 61 6d 70 6c 65 3b 0a 20 20   int nSample;.  
2e095 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 53          char *zS
2e096 61 6d 70 6c 65 20 3d 20 73 71 6c 69 74 65 33 55  ample = sqlite3U
2e097 74 66 38 74 6f 31 36 28 0a 20 20 20 20 20 20 20  tf8to16(.       
2e098 20 20 20 20 20 20 20 64 62 2c 20 70 43 6f 6c 6c         db, pColl
2e099 2d 3e 65 6e 63 2c 20 61 53 61 6d 70 6c 65 5b 69  ->enc, aSample[i
2e09a 5d 2e 75 2e 7a 2c 20 61 53 61 6d 70 6c 65 5b 69  ].u.z, aSample[i
2e09b 5d 2e 6e 42 79 74 65 2c 20 26 6e 53 61 6d 70 6c  ].nByte, &nSampl
2e09c 65 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20  e.          );. 
2e09d 20 20 20 20 20 20 20 20 20 69 66 28 20 21 7a 53           if( !zS
2e09e 61 6d 70 6c 65 20 29 7b 0a 20 20 20 20 20 20 20  ample ){.       
2e09f 20 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d       assert( db-
2e0a0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
2e0a1 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74  .            ret
2e0a2 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
2e0a3 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
2e0a4 20 20 20 20 20 20 20 20 72 20 3d 20 70 43 6f 6c          r = pCol
2e0a5 6c 2d 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70  l->xCmp(pColl->p
2e0a6 55 73 65 72 2c 20 6e 53 61 6d 70 6c 65 2c 20 7a  User, nSample, z
2e0a7 53 61 6d 70 6c 65 2c 20 6e 2c 20 7a 29 3b 0a 20  Sample, n, z);. 
2e0a8 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2e0a9 44 62 46 72 65 65 28 64 62 2c 20 7a 53 61 6d 70  DbFree(db, zSamp
2e0aa 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  le);.        }. 
2e0ab 20 20 20 20 20 20 20 69 66 28 20 72 3e 30 20 29         if( r>0 )
2e0ac 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
2e0ad 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 72      }..    asser
2e0ae 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 3d 53 51  t( i>=0 && i<=SQ
2e0af 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c  LITE_INDEX_SAMPL
2e0b0 45 53 20 29 3b 0a 20 20 20 20 2a 70 69 52 65 67  ES );.    *piReg
2e0b1 69 6f 6e 20 3d 20 69 3b 0a 20 20 7d 0a 20 20 72  ion = i;.  }.  r
2e0b2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2e0b3 0a 7d 0a 23 65 6e 64 69 66 20 20 20 2f 2a 20 23  .}.#endif   /* #
2e0b4 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
2e0b5 42 4c 45 5f 53 54 41 54 32 20 2a 2f 0a 0a 2f 2a  BLE_STAT2 */../*
2e0b6 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
2e0b7 6e 20 69 73 20 75 73 65 64 20 74 6f 20 65 73 74  n is used to est
2e0b8 69 6d 61 74 65 20 74 68 65 20 6e 75 6d 62 65 72  imate the number
2e0b9 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20 77 69   of rows that wi
2e0ba 6c 6c 20 62 65 20 76 69 73 69 74 65 64 0a 2a 2a  ll be visited.**
2e0bb 20 62 79 20 73 63 61 6e 6e 69 6e 67 20 61 6e 20   by scanning an 
2e0bc 69 6e 64 65 78 20 66 6f 72 20 61 20 72 61 6e 67  index for a rang
2e0bd 65 20 6f 66 20 76 61 6c 75 65 73 2e 20 54 68 65  e of values. The
2e0be 20 72 61 6e 67 65 20 6d 61 79 20 68 61 76 65 20   range may have 
2e0bf 61 6e 20 75 70 70 65 72 0a 2a 2a 20 62 6f 75 6e  an upper.** boun
2e0c0 64 2c 20 61 20 6c 6f 77 65 72 20 62 6f 75 6e 64  d, a lower bound
2e0c1 2c 20 6f 72 20 62 6f 74 68 2e 20 54 68 65 20 57  , or both. The W
2e0c2 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d  HERE clause term
2e0c3 73 20 74 68 61 74 20 73 65 74 20 74 68 65 20 75  s that set the u
2e0c4 70 70 65 72 0a 2a 2a 20 61 6e 64 20 6c 6f 77 65  pper.** and lowe
2e0c5 72 20 62 6f 75 6e 64 73 20 61 72 65 20 72 65 70  r bounds are rep
2e0c6 72 65 73 65 6e 74 65 64 20 62 79 20 70 4c 6f 77  resented by pLow
2e0c7 65 72 20 61 6e 64 20 70 55 70 70 65 72 20 72 65  er and pUpper re
2e0c8 73 70 65 63 74 69 76 65 6c 79 2e 20 46 6f 72 0a  spectively. For.
2e0c9 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 61 73 73 75  ** example, assu
2e0ca 6d 69 6e 67 20 74 68 61 74 20 69 6e 64 65 78 20  ming that index 
2e0cb 70 20 69 73 20 6f 6e 20 74 31 28 61 29 3a 0a 2a  p is on t1(a):.*
2e0cc 2a 0a 2a 2a 20 20 20 2e 2e 2e 20 46 52 4f 4d 20  *.**   ... FROM 
2e0cd 74 31 20 57 48 45 52 45 20 61 20 3e 20 3f 20 41  t1 WHERE a > ? A
2e0ce 4e 44 20 61 20 3c 20 3f 20 2e 2e 2e 0a 2a 2a 20  ND a < ? ....** 
2e0cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e0d0 20 20 20 7c 5f 5f 5f 5f 5f 7c 20 20 20 7c 5f 5f     |_____|   |__
2e0d1 5f 5f 5f 7c 0a 2a 2a 20 20 20 20 20 20 20 20 20  ___|.**         
2e0d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20                | 
2e0d3 20 20 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20          |.**    
2e0d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e0d5 20 70 4c 6f 77 65 72 20 20 20 20 70 55 70 70 65   pLower    pUppe
2e0d6 72 0a 2a 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65  r.**.** If eithe
2e0d7 72 20 6f 66 20 74 68 65 20 75 70 70 65 72 20 6f  r of the upper o
2e0d8 72 20 6c 6f 77 65 72 20 62 6f 75 6e 64 20 69 73  r lower bound is
2e0d9 20 6e 6f 74 20 70 72 65 73 65 6e 74 2c 20 74 68   not present, th
2e0da 65 6e 20 4e 55 4c 4c 20 69 73 20 70 61 73 73 65  en NULL is passe
2e0db 64 20 69 6e 0a 2a 2a 20 70 6c 61 63 65 20 6f 66  d in.** place of
2e0dc 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
2e0dd 6e 67 20 57 68 65 72 65 54 65 72 6d 2e 0a 2a 2a  ng WhereTerm..**
2e0de 0a 2a 2a 20 54 68 65 20 6e 45 71 20 70 61 72 61  .** The nEq para
2e0df 6d 65 74 65 72 20 69 73 20 70 61 73 73 65 64 20  meter is passed 
2e0e0 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
2e0e1 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 20 73 75   index column su
2e0e2 62 6a 65 63 74 20 74 6f 20 74 68 65 0a 2a 2a 20  bject to the.** 
2e0e3 72 61 6e 67 65 20 63 6f 6e 73 74 72 61 69 6e 74  range constraint
2e0e4 2e 20 4f 72 2c 20 65 71 75 69 76 61 6c 65 6e 74  . Or, equivalent
2e0e5 6c 79 2c 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ly, the number o
2e0e6 66 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74  f equality const
2e0e7 72 61 69 6e 74 73 0a 2a 2a 20 6f 70 74 69 6d 69  raints.** optimi
2e0e8 7a 65 64 20 62 79 20 74 68 65 20 70 72 6f 70 6f  zed by the propo
2e0e9 73 65 64 20 69 6e 64 65 78 20 73 63 61 6e 2e 20  sed index scan. 
2e0ea 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 73 73  For example, ass
2e0eb 75 6d 69 6e 67 20 69 6e 64 65 78 20 70 20 69 73  uming index p is
2e0ec 0a 2a 2a 20 6f 6e 20 74 31 28 61 2c 20 62 29 2c  .** on t1(a, b),
2e0ed 20 61 6e 64 20 74 68 65 20 53 51 4c 20 71 75 65   and the SQL que
2e0ee 72 79 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2e  ry is:.**.**   .
2e0ef 2e 2e 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45  .. FROM t1 WHERE
2e0f0 20 61 20 3d 20 3f 20 41 4e 44 20 62 20 3e 20 3f   a = ? AND b > ?
2e0f1 20 41 4e 44 20 62 20 3c 20 3f 20 2e 2e 2e 0a 2a   AND b < ? ....*
2e0f2 2a 0a 2a 2a 20 74 68 65 6e 20 6e 45 71 20 73 68  *.** then nEq sh
2e0f3 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 74  ould be passed t
2e0f4 68 65 20 76 61 6c 75 65 20 31 20 28 61 73 20 74  he value 1 (as t
2e0f5 68 65 20 72 61 6e 67 65 20 72 65 73 74 72 69 63  he range restric
2e0f6 74 65 64 20 63 6f 6c 75 6d 6e 2c 0a 2a 2a 20 62  ted column,.** b
2e0f7 2c 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20  , is the second 
2e0f8 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e  left-most column
2e0f9 20 6f 66 20 74 68 65 20 69 6e 64 65 78 29 2e 20   of the index). 
2e0fa 4f 72 2c 20 69 66 20 74 68 65 20 71 75 65 72 79  Or, if the query
2e0fb 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2e 2e 2e   is:.**.**   ...
2e0fc 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61   FROM t1 WHERE a
2e0fd 20 3e 20 3f 20 41 4e 44 20 61 20 3c 20 3f 20 2e   > ? AND a < ? .
2e0fe 2e 2e 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 6e 45  ...**.** then nE
2e0ff 71 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73  q should be pass
2e100 65 64 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ed 0..**.** The 
2e101 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69  returned value i
2e102 73 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65 74  s an integer bet
2e103 77 65 65 6e 20 31 20 61 6e 64 20 31 30 30 2c 20  ween 1 and 100, 
2e104 69 6e 63 6c 75 73 69 76 65 2e 20 41 20 72 65 74  inclusive. A ret
2e105 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20  urn.** value of 
2e106 31 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74  1 indicates that
2e107 20 74 68 65 20 70 72 6f 70 6f 73 65 64 20 72 61   the proposed ra
2e108 6e 67 65 20 73 63 61 6e 20 69 73 20 65 78 70 65  nge scan is expe
2e109 63 74 65 64 20 74 6f 20 76 69 73 69 74 0a 2a 2a  cted to visit.**
2e10a 20 61 70 70 72 6f 78 69 6d 61 74 65 6c 79 20 31   approximately 1
2e10b 2f 31 30 30 74 68 20 28 31 25 29 20 6f 66 20 74  /100th (1%) of t
2e10c 68 65 20 72 6f 77 73 20 73 65 6c 65 63 74 65 64  he rows selected
2e10d 20 62 79 20 74 68 65 20 6e 45 71 20 65 71 75 61   by the nEq equa
2e10e 6c 69 74 79 0a 2a 2a 20 63 6f 6e 73 74 72 61 69  lity.** constrai
2e10f 6e 74 73 20 28 69 66 20 61 6e 79 29 2e 20 41 20  nts (if any). A 
2e110 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20  return value of 
2e111 31 30 30 20 69 6e 64 69 63 61 74 65 73 20 74 68  100 indicates th
2e112 61 74 20 69 74 20 69 73 20 65 78 70 65 63 74 65  at it is expecte
2e113 64 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 72 61  d.** that the ra
2e114 6e 67 65 20 73 63 61 6e 20 77 69 6c 6c 20 76 69  nge scan will vi
2e115 73 69 74 20 65 76 65 72 79 20 72 6f 77 20 28 31  sit every row (1
2e116 30 30 25 29 20 73 65 6c 65 63 74 65 64 20 62 79  00%) selected by
2e117 20 74 68 65 20 65 71 75 61 6c 69 74 79 0a 2a 2a   the equality.**
2e118 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 2a 2a   constraints..**
2e119 0a 2a 2a 20 49 6e 20 74 68 65 20 61 62 73 65 6e  .** In the absen
2e11a 63 65 20 6f 66 20 73 71 6c 69 74 65 5f 73 74 61  ce of sqlite_sta
2e11b 74 32 20 41 4e 41 4c 59 5a 45 20 64 61 74 61 2c  t2 ANALYZE data,
2e11c 20 65 61 63 68 20 72 61 6e 67 65 20 69 6e 65 71   each range ineq
2e11d 75 61 6c 69 74 79 0a 2a 2a 20 72 65 64 75 63 65  uality.** reduce
2e11e 73 20 74 68 65 20 73 65 61 72 63 68 20 73 70 61  s the search spa
2e11f 63 65 20 62 79 20 32 2f 33 72 64 73 2e 20 20 48  ce by 2/3rds.  H
2e120 65 6e 63 65 20 61 20 73 69 6e 67 6c 65 20 63 6f  ence a single co
2e121 6e 73 74 72 61 69 6e 74 20 28 78 3e 3f 29 0a 2a  nstraint (x>?).*
2e122 2a 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 72  * results in a r
2e123 65 74 75 72 6e 20 6f 66 20 33 33 20 61 6e 64 20  eturn of 33 and 
2e124 61 20 72 61 6e 67 65 20 63 6f 6e 73 74 72 61 69  a range constrai
2e125 6e 74 20 28 78 3e 3f 20 41 4e 44 20 78 3c 3f 29  nt (x>? AND x<?)
2e126 20 72 65 73 75 6c 74 73 0a 2a 2a 20 69 6e 20 61   results.** in a
2e127 20 72 65 74 75 72 6e 20 6f 66 20 31 31 2e 0a 2a   return of 11..*
2e128 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 68 65  /.static int whe
2e129 72 65 52 61 6e 67 65 53 63 61 6e 45 73 74 28 0a  reRangeScanEst(.
2e12a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2e12b 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
2e12c 67 20 26 20 63 6f 64 65 20 67 65 6e 65 72 61 74  g & code generat
2e12d 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
2e12e 20 49 6e 64 65 78 20 2a 70 2c 20 20 20 20 20 20   Index *p,      
2e12f 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64        /* The ind
2e130 65 78 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ex containing th
2e131 65 20 72 61 6e 67 65 2d 63 6f 6d 70 61 72 65 64  e range-compared
2e132 20 63 6f 6c 75 6d 6e 3b 20 22 78 22 20 2a 2f 0a   column; "x" */.
2e133 20 20 69 6e 74 20 6e 45 71 2c 20 20 20 20 20 20    int nEq,      
2e134 20 20 20 20 20 20 20 2f 2a 20 69 6e 64 65 78 20         /* index 
2e135 69 6e 74 6f 20 70 2d 3e 61 43 6f 6c 5b 5d 20 6f  into p->aCol[] o
2e136 66 20 74 68 65 20 72 61 6e 67 65 2d 63 6f 6d 70  f the range-comp
2e137 61 72 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20  ared column */. 
2e138 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4c 6f 77   WhereTerm *pLow
2e139 65 72 2c 20 20 20 2f 2a 20 4c 6f 77 65 72 20 62  er,   /* Lower b
2e13a 6f 75 6e 64 20 6f 6e 20 74 68 65 20 72 61 6e 67  ound on the rang
2e13b 65 2e 20 65 78 3a 20 22 78 3e 31 32 33 22 20 4d  e. ex: "x>123" M
2e13c 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a  ight be NULL */.
2e13d 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 55 70    WhereTerm *pUp
2e13e 70 65 72 2c 20 20 20 2f 2a 20 55 70 70 65 72 20  per,   /* Upper 
2e13f 62 6f 75 6e 64 20 6f 6e 20 74 68 65 20 72 61 6e  bound on the ran
2e140 67 65 2e 20 65 78 3a 20 22 78 3c 34 35 35 22 20  ge. ex: "x<455" 
2e141 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f  Might be NULL */
2e142 0a 20 20 69 6e 74 20 2a 70 69 45 73 74 20 20 20  .  int *piEst   
2e143 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
2e144 52 65 74 75 72 6e 20 76 61 6c 75 65 20 2a 2f 0a  Return value */.
2e145 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
2e146 4c 49 54 45 5f 4f 4b 3b 0a 0a 23 69 66 64 65 66  LITE_OK;..#ifdef
2e147 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53   SQLITE_ENABLE_S
2e148 54 41 54 32 0a 20 20 73 71 6c 69 74 65 33 20 2a  TAT2.  sqlite3 *
2e149 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2e14a 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
2e14b 20 2a 70 4c 6f 77 65 72 56 61 6c 20 3d 20 30 3b   *pLowerVal = 0;
2e14c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
2e14d 20 2a 70 55 70 70 65 72 56 61 6c 20 3d 20 30 3b   *pUpperVal = 0;
2e14e 0a 0a 20 20 69 66 28 20 6e 45 71 3d 3d 30 20 26  ..  if( nEq==0 &
2e14f 26 20 70 2d 3e 61 53 61 6d 70 6c 65 20 29 7b 0a  & p->aSample ){.
2e150 20 20 20 20 69 6e 74 20 69 45 73 74 3b 0a 20 20      int iEst;.  
2e151 20 20 69 6e 74 20 69 4c 6f 77 65 72 20 3d 20 30    int iLower = 0
2e152 3b 0a 20 20 20 20 69 6e 74 20 69 55 70 70 65 72  ;.    int iUpper
2e153 20 3d 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f   = SQLITE_INDEX_
2e154 53 41 4d 50 4c 45 53 3b 0a 20 20 20 20 75 38 20  SAMPLES;.    u8 
2e155 61 66 66 20 3d 20 70 2d 3e 70 54 61 62 6c 65 2d  aff = p->pTable-
2e156 3e 61 43 6f 6c 5b 30 5d 2e 61 66 66 69 6e 69 74  >aCol[0].affinit
2e157 79 3b 0a 0a 20 20 20 20 69 66 28 20 70 4c 6f 77  y;..    if( pLow
2e158 65 72 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72  er ){.      Expr
2e159 20 2a 70 45 78 70 72 20 3d 20 70 4c 6f 77 65 72   *pExpr = pLower
2e15a 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b  ->pExpr->pRight;
2e15b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
2e15c 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72  te3ValueFromExpr
2e15d 28 64 62 2c 20 70 45 78 70 72 2c 20 53 51 4c 49  (db, pExpr, SQLI
2e15e 54 45 5f 55 54 46 38 2c 20 61 66 66 2c 20 26 70  TE_UTF8, aff, &p
2e15f 4c 6f 77 65 72 56 61 6c 29 3b 0a 20 20 20 20 7d  LowerVal);.    }
2e160 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
2e161 49 54 45 5f 4f 4b 20 26 26 20 70 55 70 70 65 72  ITE_OK && pUpper
2e162 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a   ){.      Expr *
2e163 70 45 78 70 72 20 3d 20 70 55 70 70 65 72 2d 3e  pExpr = pUpper->
2e164 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20  pExpr->pRight;. 
2e165 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
2e166 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 64  3ValueFromExpr(d
2e167 62 2c 20 70 45 78 70 72 2c 20 53 51 4c 49 54 45  b, pExpr, SQLITE
2e168 5f 55 54 46 38 2c 20 61 66 66 2c 20 26 70 55 70  _UTF8, aff, &pUp
2e169 70 65 72 56 61 6c 29 3b 0a 20 20 20 20 7d 0a 0a  perVal);.    }..
2e16a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
2e16b 54 45 5f 4f 4b 20 7c 7c 20 28 70 4c 6f 77 65 72  TE_OK || (pLower
2e16c 56 61 6c 3d 3d 30 20 26 26 20 70 55 70 70 65 72  Val==0 && pUpper
2e16d 56 61 6c 3d 3d 30 29 20 29 7b 0a 20 20 20 20 20  Val==0) ){.     
2e16e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65   sqlite3ValueFre
2e16f 65 28 70 4c 6f 77 65 72 56 61 6c 29 3b 0a 20 20  e(pLowerVal);.  
2e170 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65      sqlite3Value
2e171 46 72 65 65 28 70 55 70 70 65 72 56 61 6c 29 3b  Free(pUpperVal);
2e172 0a 20 20 20 20 20 20 67 6f 74 6f 20 72 61 6e 67  .      goto rang
2e173 65 5f 65 73 74 5f 66 61 6c 6c 62 61 63 6b 3b 0a  e_est_fallback;.
2e174 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4c      }else if( pL
2e175 6f 77 65 72 56 61 6c 3d 3d 30 20 29 7b 0a 20 20  owerVal==0 ){.  
2e176 20 20 20 20 72 63 20 3d 20 77 68 65 72 65 52 61      rc = whereRa
2e177 6e 67 65 52 65 67 69 6f 6e 28 70 50 61 72 73 65  ngeRegion(pParse
2e178 2c 20 70 2c 20 70 55 70 70 65 72 56 61 6c 2c 20  , p, pUpperVal, 
2e179 26 69 55 70 70 65 72 29 3b 0a 20 20 20 20 20 20  &iUpper);.      
2e17a 69 66 28 20 70 4c 6f 77 65 72 20 29 20 69 4c 6f  if( pLower ) iLo
2e17b 77 65 72 20 3d 20 69 55 70 70 65 72 2f 32 3b 0a  wer = iUpper/2;.
2e17c 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 55      }else if( pU
2e17d 70 70 65 72 56 61 6c 3d 3d 30 20 29 7b 0a 20 20  pperVal==0 ){.  
2e17e 20 20 20 20 72 63 20 3d 20 77 68 65 72 65 52 61      rc = whereRa
2e17f 6e 67 65 52 65 67 69 6f 6e 28 70 50 61 72 73 65  ngeRegion(pParse
2e180 2c 20 70 2c 20 70 4c 6f 77 65 72 56 61 6c 2c 20  , p, pLowerVal, 
2e181 26 69 4c 6f 77 65 72 29 3b 0a 20 20 20 20 20 20  &iLower);.      
2e182 69 66 28 20 70 55 70 70 65 72 20 29 20 69 55 70  if( pUpper ) iUp
2e183 70 65 72 20 3d 20 28 69 4c 6f 77 65 72 20 2b 20  per = (iLower + 
2e184 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d  SQLITE_INDEX_SAM
2e185 50 4c 45 53 20 2b 20 31 29 2f 32 3b 0a 20 20 20  PLES + 1)/2;.   
2e186 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
2e187 20 3d 20 77 68 65 72 65 52 61 6e 67 65 52 65 67   = whereRangeReg
2e188 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 2c 20 70  ion(pParse, p, p
2e189 55 70 70 65 72 56 61 6c 2c 20 26 69 55 70 70 65  UpperVal, &iUppe
2e18a 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  r);.      if( rc
2e18b 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
2e18c 20 20 20 20 20 20 20 72 63 20 3d 20 77 68 65 72         rc = wher
2e18d 65 52 61 6e 67 65 52 65 67 69 6f 6e 28 70 50 61  eRangeRegion(pPa
2e18e 72 73 65 2c 20 70 2c 20 70 4c 6f 77 65 72 56 61  rse, p, pLowerVa
2e18f 6c 2c 20 26 69 4c 6f 77 65 72 29 3b 0a 20 20 20  l, &iLower);.   
2e190 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
2e191 69 45 73 74 20 3d 20 69 55 70 70 65 72 20 2d 20  iEst = iUpper - 
2e192 69 4c 6f 77 65 72 3b 0a 20 20 20 20 74 65 73 74  iLower;.    test
2e193 63 61 73 65 28 20 69 45 73 74 3d 3d 53 51 4c 49  case( iEst==SQLI
2e194 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53  TE_INDEX_SAMPLES
2e195 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
2e196 69 45 73 74 3c 3d 53 51 4c 49 54 45 5f 49 4e 44  iEst<=SQLITE_IND
2e197 45 58 5f 53 41 4d 50 4c 45 53 20 29 3b 0a 20 20  EX_SAMPLES );.  
2e198 20 20 69 66 28 20 69 45 73 74 3c 31 20 29 7b 0a    if( iEst<1 ){.
2e199 20 20 20 20 20 20 69 45 73 74 20 3d 20 31 3b 0a        iEst = 1;.
2e19a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
2e19b 65 33 56 61 6c 75 65 46 72 65 65 28 70 4c 6f 77  e3ValueFree(pLow
2e19c 65 72 56 61 6c 29 3b 0a 20 20 20 20 73 71 6c 69  erVal);.    sqli
2e19d 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 55 70  te3ValueFree(pUp
2e19e 70 65 72 56 61 6c 29 3b 0a 20 20 20 20 2a 70 69  perVal);.    *pi
2e19f 45 73 74 20 3d 20 28 69 45 73 74 20 2a 20 31 30  Est = (iEst * 10
2e1a0 30 29 2f 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  0)/SQLITE_INDEX_
2e1a1 53 41 4d 50 4c 45 53 3b 0a 20 20 20 20 72 65 74  SAMPLES;.    ret
2e1a2 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 72 61 6e 67  urn rc;.  }.rang
2e1a3 65 5f 65 73 74 5f 66 61 6c 6c 62 61 63 6b 3a 0a  e_est_fallback:.
2e1a4 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50  #else.  UNUSED_P
2e1a5 41 52 41 4d 45 54 45 52 28 70 50 61 72 73 65 29  ARAMETER(pParse)
2e1a6 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
2e1a7 45 54 45 52 28 70 29 3b 0a 20 20 55 4e 55 53 45  ETER(p);.  UNUSE
2e1a8 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 45 71 29  D_PARAMETER(nEq)
2e1a9 3b 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 65 72  ;.#endif.  asser
2e1aa 74 28 20 70 4c 6f 77 65 72 20 7c 7c 20 70 55 70  t( pLower || pUp
2e1ab 70 65 72 20 29 3b 0a 20 20 69 66 28 20 70 4c 6f  per );.  if( pLo
2e1ac 77 65 72 20 26 26 20 70 55 70 70 65 72 20 29 7b  wer && pUpper ){
2e1ad 0a 20 20 20 20 2a 70 69 45 73 74 20 3d 20 31 31  .    *piEst = 11
2e1ae 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a  ;.  }else{.    *
2e1af 70 69 45 73 74 20 3d 20 33 33 3b 0a 20 20 7d 0a  piEst = 33;.  }.
2e1b0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
2e1b1 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20  ./*.** Find the 
2e1b2 71 75 65 72 79 20 70 6c 61 6e 20 66 6f 72 20 61  query plan for a
2e1b3 63 63 65 73 73 69 6e 67 20 61 20 70 61 72 74 69  ccessing a parti
2e1b4 63 75 6c 61 72 20 74 61 62 6c 65 2e 20 20 57 72  cular table.  Wr
2e1b5 69 74 65 20 74 68 65 0a 2a 2a 20 62 65 73 74 20  ite the.** best 
2e1b6 71 75 65 72 79 20 70 6c 61 6e 20 61 6e 64 20 69  query plan and i
2e1b7 74 73 20 63 6f 73 74 20 69 6e 74 6f 20 74 68 65  ts cost into the
2e1b8 20 57 68 65 72 65 43 6f 73 74 20 6f 62 6a 65 63   WhereCost objec
2e1b9 74 20 73 75 70 70 6c 69 65 64 20 61 73 20 74 68  t supplied as th
2e1ba 65 0a 2a 2a 20 6c 61 73 74 20 70 61 72 61 6d 65  e.** last parame
2e1bb 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c  ter..**.** The l
2e1bc 6f 77 65 73 74 20 63 6f 73 74 20 70 6c 61 6e 20  owest cost plan 
2e1bd 77 69 6e 73 2e 20 20 54 68 65 20 63 6f 73 74 20  wins.  The cost 
2e1be 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f  is an estimate o
2e1bf 66 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 0a  f the amount of.
2e1c0 2a 2a 20 43 50 55 20 61 6e 64 20 64 69 73 6b 20  ** CPU and disk 
2e1c1 49 2f 4f 20 6e 65 65 64 20 74 6f 20 70 72 6f 63  I/O need to proc
2e1c2 65 73 73 20 74 68 65 20 72 65 71 75 65 73 74 20  ess the request 
2e1c3 75 73 69 6e 67 20 74 68 65 20 73 65 6c 65 63 74  using the select
2e1c4 65 64 20 70 6c 61 6e 2e 0a 2a 2a 20 46 61 63 74  ed plan..** Fact
2e1c5 6f 72 73 20 74 68 61 74 20 69 6e 66 6c 75 65 6e  ors that influen
2e1c6 63 65 20 63 6f 73 74 20 69 6e 63 6c 75 64 65 3a  ce cost include:
2e1c7 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 54 68 65  .**.**    *  The
2e1c8 20 65 73 74 69 6d 61 74 65 64 20 6e 75 6d 62 65   estimated numbe
2e1c9 72 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20 77  r of rows that w
2e1ca 69 6c 6c 20 62 65 20 72 65 74 72 69 65 76 65 64  ill be retrieved
2e1cb 2e 20 20 28 54 68 65 0a 2a 2a 20 20 20 20 20 20  .  (The.**      
2e1cc 20 66 65 77 65 72 20 74 68 65 20 62 65 74 74 65   fewer the bette
2e1cd 72 2e 29 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20  r.).**.**    *  
2e1ce 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 73  Whether or not s
2e1cf 6f 72 74 69 6e 67 20 6d 75 73 74 20 6f 63 63 75  orting must occu
2e1d0 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 57  r..**.**    *  W
2e1d1 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
2e1d2 65 72 65 20 6d 75 73 74 20 62 65 20 73 65 70 61  ere must be sepa
2e1d3 72 61 74 65 20 6c 6f 6f 6b 75 70 73 20 69 6e 20  rate lookups in 
2e1d4 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 69 6e 64  the.**       ind
2e1d5 65 78 20 61 6e 64 20 69 6e 20 74 68 65 20 6d 61  ex and in the ma
2e1d6 69 6e 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  in table..**.** 
2e1d7 49 66 20 74 68 65 72 65 20 77 61 73 20 61 6e 20  If there was an 
2e1d8 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75 73  INDEXED BY claus
2e1d9 65 20 28 70 53 72 63 2d 3e 70 49 6e 64 65 78 29  e (pSrc->pIndex)
2e1da 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68 65   attached to the
2e1db 20 74 61 62 6c 65 20 69 6e 0a 2a 2a 20 74 68 65   table in.** the
2e1dc 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20   SQL statement, 
2e1dd 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  then this functi
2e1de 6f 6e 20 6f 6e 6c 79 20 63 6f 6e 73 69 64 65 72  on only consider
2e1df 73 20 70 6c 61 6e 73 20 75 73 69 6e 67 20 74 68  s plans using th
2e1e0 65 20 0a 2a 2a 20 6e 61 6d 65 64 20 69 6e 64 65  e .** named inde
2e1e1 78 2e 20 49 66 20 6e 6f 20 73 75 63 68 20 70 6c  x. If no such pl
2e1e2 61 6e 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65  an is found, the
2e1e3 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 63  n the returned c
2e1e4 6f 73 74 20 69 73 0a 2a 2a 20 53 51 4c 49 54 45  ost is.** SQLITE
2e1e5 5f 42 49 47 5f 44 42 4c 2e 20 49 66 20 61 20 70  _BIG_DBL. If a p
2e1e6 6c 61 6e 20 69 73 20 66 6f 75 6e 64 20 74 68 61  lan is found tha
2e1e7 74 20 75 73 65 73 20 74 68 65 20 6e 61 6d 65 64  t uses the named
2e1e8 20 69 6e 64 65 78 2c 20 0a 2a 2a 20 74 68 65 6e   index, .** then
2e1e9 20 74 68 65 20 63 6f 73 74 20 69 73 20 63 61 6c   the cost is cal
2e1ea 63 75 6c 61 74 65 64 20 69 6e 20 74 68 65 20 75  culated in the u
2e1eb 73 75 61 6c 20 77 61 79 2e 0a 2a 2a 0a 2a 2a 20  sual way..**.** 
2e1ec 49 66 20 61 20 4e 4f 54 20 49 4e 44 45 58 45 44  If a NOT INDEXED
2e1ed 20 63 6c 61 75 73 65 20 28 70 53 72 63 2d 3e 6e   clause (pSrc->n
2e1ee 6f 74 49 6e 64 65 78 65 64 21 3d 30 29 20 77 61  otIndexed!=0) wa
2e1ef 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68  s attached to th
2e1f0 65 20 74 61 62 6c 65 20 0a 2a 2a 20 69 6e 20 74  e table .** in t
2e1f1 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
2e1f2 65 6e 74 2c 20 74 68 65 6e 20 6e 6f 20 69 6e 64  ent, then no ind
2e1f3 65 78 65 73 20 61 72 65 20 63 6f 6e 73 69 64 65  exes are conside
2e1f4 72 65 64 2e 20 48 6f 77 65 76 65 72 2c 20 74 68  red. However, th
2e1f5 65 20 0a 2a 2a 20 73 65 6c 65 63 74 65 64 20 70  e .** selected p
2e1f6 6c 61 6e 20 6d 61 79 20 73 74 69 6c 6c 20 74 61  lan may still ta
2e1f7 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f 66 20  ke advantage of 
2e1f8 74 68 65 20 74 61 62 6c 65 73 20 62 75 69 6c 74  the tables built
2e1f9 2d 69 6e 20 72 6f 77 69 64 0a 2a 2a 20 69 6e 64  -in rowid.** ind
2e1fa 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ex..*/.static vo
2e1fb 69 64 20 62 65 73 74 42 74 72 65 65 49 6e 64 65  id bestBtreeInde
2e1fc 78 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  x(.  Parse *pPar
2e1fd 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
2e1fe 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
2e1ff 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65  context */.  Whe
2e200 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20  reClause *pWC,  
2e201 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2e202 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
2e203 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
2e204 5f 69 74 65 6d 20 2a 70 53 72 63 2c 20 20 2f 2a  _item *pSrc,  /*
2e205 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   The FROM clause
2e206 20 74 65 72 6d 20 74 6f 20 73 65 61 72 63 68 20   term to search 
2e207 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74  */.  Bitmask not
2e208 52 65 61 64 79 2c 20 20 20 20 20 20 20 20 20 20  Ready,          
2e209 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 63 75 72 73   /* Mask of curs
2e20a 6f 72 73 20 74 68 61 74 20 61 72 65 20 6e 6f 74  ors that are not
2e20b 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20 20   available */.  
2e20c 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
2e20d 42 79 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  By,         /* T
2e20e 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2e20f 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 43 6f 73  se */.  WhereCos
2e210 74 20 2a 70 43 6f 73 74 20 20 20 20 20 20 20 20  t *pCost        
2e211 20 20 20 20 2f 2a 20 4c 6f 77 65 73 74 20 63 6f      /* Lowest co
2e212 73 74 20 71 75 65 72 79 20 70 6c 61 6e 20 2a 2f  st query plan */
2e213 0a 29 7b 0a 20 20 69 6e 74 20 69 43 75 72 20 3d  .){.  int iCur =
2e214 20 70 53 72 63 2d 3e 69 43 75 72 73 6f 72 3b 20   pSrc->iCursor; 
2e215 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20    /* The cursor 
2e216 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  of the table to 
2e217 62 65 20 61 63 63 65 73 73 65 64 20 2a 2f 0a 20  be accessed */. 
2e218 20 49 6e 64 65 78 20 2a 70 50 72 6f 62 65 3b 20   Index *pProbe; 
2e219 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e21a 41 6e 20 69 6e 64 65 78 20 77 65 20 61 72 65 20  An index we are 
2e21b 65 76 61 6c 75 61 74 69 6e 67 20 2a 2f 0a 20 20  evaluating */.  
2e21c 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20  Index *pIdx;    
2e21d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
2e21e 6f 70 79 20 6f 66 20 70 50 72 6f 62 65 2c 20 6f  opy of pProbe, o
2e21f 72 20 7a 65 72 6f 20 66 6f 72 20 49 50 4b 20 69  r zero for IPK i
2e220 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 65 71  ndex */.  int eq
2e221 54 65 72 6d 4d 61 73 6b 3b 20 20 20 20 20 20 20  TermMask;       
2e222 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
2e223 20 6d 61 73 6b 20 6f 66 20 76 61 6c 69 64 20 65   mask of valid e
2e224 71 75 61 6c 69 74 79 20 6f 70 65 72 61 74 6f 72  quality operator
2e225 73 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 45 71  s */.  int idxEq
2e226 54 65 72 6d 4d 61 73 6b 3b 20 20 20 20 20 20 20  TermMask;       
2e227 20 20 20 2f 2a 20 49 6e 64 65 78 20 6d 61 73 6b     /* Index mask
2e228 20 6f 66 20 76 61 6c 69 64 20 65 71 75 61 6c 69   of valid equali
2e229 74 79 20 6f 70 65 72 61 74 6f 72 73 20 2a 2f 0a  ty operators */.
2e22a 20 20 49 6e 64 65 78 20 73 50 6b 3b 20 20 20 20    Index sPk;    
2e22b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2e22c 20 41 20 66 61 6b 65 20 69 6e 64 65 78 20 6f 62   A fake index ob
2e22d 6a 65 63 74 20 66 6f 72 20 74 68 65 20 70 72 69  ject for the pri
2e22e 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 20 20 75 6e  mary key */.  un
2e22f 73 69 67 6e 65 64 20 69 6e 74 20 61 69 52 6f 77  signed int aiRow
2e230 45 73 74 50 6b 5b 32 5d 3b 20 2f 2a 20 54 68 65  EstPk[2]; /* The
2e231 20 61 69 52 6f 77 45 73 74 5b 5d 20 76 61 6c 75   aiRowEst[] valu
2e232 65 20 66 6f 72 20 74 68 65 20 73 50 6b 20 69 6e  e for the sPk in
2e233 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 61 69 43  dex */.  int aiC
2e234 6f 6c 75 6d 6e 50 6b 20 3d 20 2d 31 3b 20 20 20  olumnPk = -1;   
2e235 20 20 20 20 20 2f 2a 20 54 68 65 20 61 43 6f 6c       /* The aCol
2e236 75 6d 6e 5b 5d 20 76 61 6c 75 65 20 66 6f 72 20  umn[] value for 
2e237 74 68 65 20 73 50 6b 20 69 6e 64 65 78 20 2a 2f  the sPk index */
2e238 0a 20 20 69 6e 74 20 77 73 46 6c 61 67 4d 61 73  .  int wsFlagMas
2e239 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  k;             /
2e23a 2a 20 41 6c 6c 6f 77 65 64 20 66 6c 61 67 73 20  * Allowed flags 
2e23b 69 6e 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77  in pCost->plan.w
2e23c 73 46 6c 61 67 20 2a 2f 0a 0a 20 20 2f 2a 20 49  sFlag */..  /* I
2e23d 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f  nitialize the co
2e23e 73 74 20 74 6f 20 61 20 77 6f 72 73 74 2d 63 61  st to a worst-ca
2e23f 73 65 20 76 61 6c 75 65 20 2a 2f 0a 20 20 6d 65  se value */.  me
2e240 6d 73 65 74 28 70 43 6f 73 74 2c 20 30 2c 20 73  mset(pCost, 0, s
2e241 69 7a 65 6f 66 28 2a 70 43 6f 73 74 29 29 3b 0a  izeof(*pCost));.
2e242 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d    pCost->rCost =
2e243 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 3b   SQLITE_BIG_DBL;
2e244 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 53  ..  /* If the pS
2e245 72 63 20 74 61 62 6c 65 20 69 73 20 74 68 65 20  rc table is the 
2e246 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20 61  right table of a
2e247 20 4c 45 46 54 20 4a 4f 49 4e 20 74 68 65 6e 20   LEFT JOIN then 
2e248 77 65 20 6d 61 79 20 6e 6f 74 0a 20 20 2a 2a 20  we may not.  ** 
2e249 75 73 65 20 61 6e 20 69 6e 64 65 78 20 74 6f 20  use an index to 
2e24a 73 61 74 69 73 66 79 20 49 53 20 4e 55 4c 4c 20  satisfy IS NULL 
2e24b 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f 6e 20 74  constraints on t
2e24c 68 61 74 20 74 61 62 6c 65 2e 20 20 54 68 69 73  hat table.  This
2e24d 20 69 73 0a 20 20 2a 2a 20 62 65 63 61 75 73 65   is.  ** because
2e24e 20 63 6f 6c 75 6d 6e 73 20 6d 69 67 68 74 20 65   columns might e
2e24f 6e 64 20 75 70 20 62 65 69 6e 67 20 4e 55 4c 4c  nd up being NULL
2e250 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 64 6f   if the table do
2e251 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 2d 0a 20  es not match -. 
2e252 20 2a 2a 20 61 20 63 69 72 63 75 6d 73 74 61 6e   ** a circumstan
2e253 63 65 20 77 68 69 63 68 20 74 68 65 20 69 6e 64  ce which the ind
2e254 65 78 20 63 61 6e 6e 6f 74 20 68 65 6c 70 20 75  ex cannot help u
2e255 73 20 64 69 73 63 6f 76 65 72 2e 20 20 54 69 63  s discover.  Tic
2e256 6b 65 74 20 23 32 31 37 37 2e 0a 20 20 2a 2f 0a  ket #2177..  */.
2e257 20 20 69 66 28 20 70 53 72 63 2d 3e 6a 6f 69 6e    if( pSrc->join
2e258 74 79 70 65 20 26 20 4a 54 5f 4c 45 46 54 20 29  type & JT_LEFT )
2e259 7b 0a 20 20 20 20 69 64 78 45 71 54 65 72 6d 4d  {.    idxEqTermM
2e25a 61 73 6b 20 3d 20 57 4f 5f 45 51 7c 57 4f 5f 49  ask = WO_EQ|WO_I
2e25b 4e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  N;.  }else{.    
2e25c 69 64 78 45 71 54 65 72 6d 4d 61 73 6b 20 3d 20  idxEqTermMask = 
2e25d 57 4f 5f 45 51 7c 57 4f 5f 49 4e 7c 57 4f 5f 49  WO_EQ|WO_IN|WO_I
2e25e 53 4e 55 4c 4c 3b 0a 20 20 7d 0a 0a 20 20 69 66  SNULL;.  }..  if
2e25f 28 20 70 53 72 63 2d 3e 70 49 6e 64 65 78 20 29  ( pSrc->pIndex )
2e260 7b 0a 20 20 20 20 2f 2a 20 41 6e 20 49 4e 44 45  {.    /* An INDE
2e261 58 45 44 20 42 59 20 63 6c 61 75 73 65 20 73 70  XED BY clause sp
2e262 65 63 69 66 69 65 73 20 61 20 70 61 72 74 69 63  ecifies a partic
2e263 75 6c 61 72 20 69 6e 64 65 78 20 74 6f 20 75 73  ular index to us
2e264 65 20 2a 2f 0a 20 20 20 20 70 49 64 78 20 3d 20  e */.    pIdx = 
2e265 70 50 72 6f 62 65 20 3d 20 70 53 72 63 2d 3e 70  pProbe = pSrc->p
2e266 49 6e 64 65 78 3b 0a 20 20 20 20 77 73 46 6c 61  Index;.    wsFla
2e267 67 4d 61 73 6b 20 3d 20 7e 28 57 48 45 52 45 5f  gMask = ~(WHERE_
2e268 52 4f 57 49 44 5f 45 51 7c 57 48 45 52 45 5f 52  ROWID_EQ|WHERE_R
2e269 4f 57 49 44 5f 52 41 4e 47 45 29 3b 0a 20 20 20  OWID_RANGE);.   
2e26a 20 65 71 54 65 72 6d 4d 61 73 6b 20 3d 20 69 64   eqTermMask = id
2e26b 78 45 71 54 65 72 6d 4d 61 73 6b 3b 0a 20 20 7d  xEqTermMask;.  }
2e26c 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65  else{.    /* The
2e26d 72 65 20 69 73 20 6e 6f 20 49 4e 44 45 58 45 44  re is no INDEXED
2e26e 20 42 59 20 63 6c 61 75 73 65 2e 20 20 43 72 65   BY clause.  Cre
2e26f 61 74 65 20 61 20 66 61 6b 65 20 49 6e 64 65 78  ate a fake Index
2e270 20 6f 62 6a 65 63 74 20 74 6f 0a 20 20 20 20 2a   object to.    *
2e271 2a 20 72 65 70 72 65 73 65 6e 74 20 74 68 65 20  * represent the 
2e272 70 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 20  primary key */. 
2e273 20 20 20 49 6e 64 65 78 20 2a 70 46 69 72 73 74     Index *pFirst
2e274 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2e275 20 2f 2a 20 41 6e 79 20 6f 74 68 65 72 20 69 6e   /* Any other in
2e276 64 65 78 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  dex on the table
2e277 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26   */.    memset(&
2e278 73 50 6b 2c 20 30 2c 20 73 69 7a 65 6f 66 28 49  sPk, 0, sizeof(I
2e279 6e 64 65 78 29 29 3b 0a 20 20 20 20 73 50 6b 2e  ndex));.    sPk.
2e27a 6e 43 6f 6c 75 6d 6e 20 3d 20 31 3b 0a 20 20 20  nColumn = 1;.   
2e27b 20 73 50 6b 2e 61 69 43 6f 6c 75 6d 6e 20 3d 20   sPk.aiColumn = 
2e27c 26 61 69 43 6f 6c 75 6d 6e 50 6b 3b 0a 20 20 20  &aiColumnPk;.   
2e27d 20 73 50 6b 2e 61 69 52 6f 77 45 73 74 20 3d 20   sPk.aiRowEst = 
2e27e 61 69 52 6f 77 45 73 74 50 6b 3b 0a 20 20 20 20  aiRowEstPk;.    
2e27f 61 69 52 6f 77 45 73 74 50 6b 5b 31 5d 20 3d 20  aiRowEstPk[1] = 
2e280 31 3b 0a 20 20 20 20 73 50 6b 2e 6f 6e 45 72 72  1;.    sPk.onErr
2e281 6f 72 20 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b  or = OE_Replace;
2e282 0a 20 20 20 20 73 50 6b 2e 70 54 61 62 6c 65 20  .    sPk.pTable 
2e283 3d 20 70 53 72 63 2d 3e 70 54 61 62 3b 0a 20 20  = pSrc->pTab;.  
2e284 20 20 70 46 69 72 73 74 20 3d 20 70 53 72 63 2d    pFirst = pSrc-
2e285 3e 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 0a 20  >pTab->pIndex;. 
2e286 20 20 20 69 66 28 20 70 53 72 63 2d 3e 6e 6f 74     if( pSrc->not
2e287 49 6e 64 65 78 65 64 3d 3d 30 20 29 7b 0a 20 20  Indexed==0 ){.  
2e288 20 20 20 20 73 50 6b 2e 70 4e 65 78 74 20 3d 20      sPk.pNext = 
2e289 70 46 69 72 73 74 3b 0a 20 20 20 20 7d 0a 20 20  pFirst;.    }.  
2e28a 20 20 2f 2a 20 54 68 65 20 61 69 52 6f 77 45 73    /* The aiRowEs
2e28b 74 50 6b 5b 30 5d 20 69 73 20 61 6e 20 65 73 74  tPk[0] is an est
2e28c 69 6d 61 74 65 20 6f 66 20 74 68 65 20 74 6f 74  imate of the tot
2e28d 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  al number of row
2e28e 73 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20  s in the.    ** 
2e28f 74 61 62 6c 65 2e 20 20 47 65 74 20 74 68 69 73  table.  Get this
2e290 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f   information fro
2e291 6d 20 74 68 65 20 41 4e 41 4c 59 5a 45 20 69 6e  m the ANALYZE in
2e292 66 6f 72 6d 61 74 69 6f 6e 20 69 66 20 69 74 20  formation if it 
2e293 69 73 0a 20 20 20 20 2a 2a 20 61 76 61 69 6c 61  is.    ** availa
2e294 62 6c 65 2e 20 20 49 66 20 6e 6f 74 20 61 76 61  ble.  If not ava
2e295 69 6c 61 62 6c 65 2c 20 61 73 73 75 6d 65 20 74  ilable, assume t
2e296 68 65 20 74 61 62 6c 65 20 31 20 6d 69 6c 6c 69  he table 1 milli
2e297 6f 6e 20 72 6f 77 73 20 69 6e 20 73 69 7a 65 2e  on rows in size.
2e298 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
2e299 70 46 69 72 73 74 20 29 7b 0a 20 20 20 20 20 20  pFirst ){.      
2e29a 61 73 73 65 72 74 28 20 70 46 69 72 73 74 2d 3e  assert( pFirst->
2e29b 61 69 52 6f 77 45 73 74 21 3d 30 20 29 3b 20 2f  aiRowEst!=0 ); /
2e29c 2a 20 41 6c 6c 6f 63 61 74 65 64 20 74 6f 67 65  * Allocated toge
2e29d 74 68 65 72 20 77 69 74 68 20 70 46 69 72 73 74  ther with pFirst
2e29e 20 2a 2f 0a 20 20 20 20 20 20 61 69 52 6f 77 45   */.      aiRowE
2e29f 73 74 50 6b 5b 30 5d 20 3d 20 70 46 69 72 73 74  stPk[0] = pFirst
2e2a0 2d 3e 61 69 52 6f 77 45 73 74 5b 30 5d 3b 0a 20  ->aiRowEst[0];. 
2e2a1 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2e2a2 61 69 52 6f 77 45 73 74 50 6b 5b 30 5d 20 3d 20  aiRowEstPk[0] = 
2e2a3 31 30 30 30 30 30 30 3b 0a 20 20 20 20 7d 0a 20  1000000;.    }. 
2e2a4 20 20 20 70 50 72 6f 62 65 20 3d 20 26 73 50 6b     pProbe = &sPk
2e2a5 3b 0a 20 20 20 20 77 73 46 6c 61 67 4d 61 73 6b  ;.    wsFlagMask
2e2a6 20 3d 20 7e 28 0a 20 20 20 20 20 20 20 20 57 48   = ~(.        WH
2e2a7 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 7c 57 48  ERE_COLUMN_IN|WH
2e2a8 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45 51 7c 57 48  ERE_COLUMN_EQ|WH
2e2a9 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c 7c  ERE_COLUMN_NULL|
2e2aa 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e  WHERE_COLUMN_RAN
2e2ab 47 45 0a 20 20 20 20 29 3b 0a 20 20 20 20 65 71  GE.    );.    eq
2e2ac 54 65 72 6d 4d 61 73 6b 20 3d 20 57 4f 5f 45 51  TermMask = WO_EQ
2e2ad 7c 57 4f 5f 49 4e 3b 0a 20 20 20 20 70 49 64 78  |WO_IN;.    pIdx
2e2ae 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   = 0;.  }..  /* 
2e2af 4c 6f 6f 70 20 6f 76 65 72 20 61 6c 6c 20 69 6e  Loop over all in
2e2b0 64 69 63 65 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f  dices looking fo
2e2b1 72 20 74 68 65 20 62 65 73 74 20 6f 6e 65 20 74  r the best one t
2e2b2 6f 20 75 73 65 0a 20 20 2a 2f 0a 20 20 66 6f 72  o use.  */.  for
2e2b3 28 3b 20 70 50 72 6f 62 65 3b 20 70 49 64 78 3d  (; pProbe; pIdx=
2e2b4 70 50 72 6f 62 65 3d 70 50 72 6f 62 65 2d 3e 70  pProbe=pProbe->p
2e2b5 4e 65 78 74 29 7b 0a 20 20 20 20 63 6f 6e 73 74  Next){.    const
2e2b6 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 2a 20   unsigned int * 
2e2b7 63 6f 6e 73 74 20 61 69 52 6f 77 45 73 74 20 3d  const aiRowEst =
2e2b8 20 70 50 72 6f 62 65 2d 3e 61 69 52 6f 77 45 73   pProbe->aiRowEs
2e2b9 74 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 63 6f  t;.    double co
2e2ba 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  st;             
2e2bb 20 20 20 2f 2a 20 43 6f 73 74 20 6f 66 20 75 73     /* Cost of us
2e2bc 69 6e 67 20 70 50 72 6f 62 65 20 2a 2f 0a 20 20  ing pProbe */.  
2e2bd 20 20 64 6f 75 62 6c 65 20 6e 52 6f 77 3b 20 20    double nRow;  
2e2be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2e2bf 20 45 73 74 69 6d 61 74 65 64 20 6e 75 6d 62 65   Estimated numbe
2e2c0 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 72 65 73  r of rows in res
2e2c1 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 20 20 69  ult set */.    i
2e2c2 6e 74 20 72 65 76 3b 20 20 20 20 20 20 20 20 20  nt rev;         
2e2c3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
2e2c4 75 65 20 74 6f 20 73 63 61 6e 20 69 6e 20 72 65  ue to scan in re
2e2c5 76 65 72 73 65 20 6f 72 64 65 72 20 2a 2f 0a 20  verse order */. 
2e2c6 20 20 20 69 6e 74 20 77 73 46 6c 61 67 73 20 3d     int wsFlags =
2e2c7 20 30 3b 0a 20 20 20 20 42 69 74 6d 61 73 6b 20   0;.    Bitmask 
2e2c8 75 73 65 64 20 3d 20 30 3b 0a 0a 20 20 20 20 2f  used = 0;..    /
2e2c9 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
2e2ca 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 70 6f  variables are po
2e2cb 70 75 6c 61 74 65 64 20 62 61 73 65 64 20 6f 6e  pulated based on
2e2cc 20 74 68 65 20 70 72 6f 70 65 72 74 69 65 73 20   the properties 
2e2cd 6f 66 0a 20 20 20 20 2a 2a 20 73 63 61 6e 20 62  of.    ** scan b
2e2ce 65 69 6e 67 20 65 76 61 6c 75 61 74 65 64 2e 20  eing evaluated. 
2e2cf 54 68 65 79 20 61 72 65 20 74 68 65 6e 20 75 73  They are then us
2e2d0 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ed to determine 
2e2d1 74 68 65 20 65 78 70 65 63 74 65 64 0a 20 20 20  the expected.   
2e2d2 20 2a 2a 20 63 6f 73 74 20 61 6e 64 20 6e 75 6d   ** cost and num
2e2d3 62 65 72 20 6f 66 20 72 6f 77 73 20 72 65 74 75  ber of rows retu
2e2d4 72 6e 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  rned..    **.   
2e2d5 20 2a 2a 20 20 6e 45 71 3a 20 0a 20 20 20 20 2a   **  nEq: .    *
2e2d6 2a 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 65  *    Number of e
2e2d7 71 75 61 6c 69 74 79 20 74 65 72 6d 73 20 74 68  quality terms th
2e2d8 61 74 20 63 61 6e 20 62 65 20 69 6d 70 6c 65 6d  at can be implem
2e2d9 65 6e 74 65 64 20 75 73 69 6e 67 20 74 68 65 20  ented using the 
2e2da 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2a 0a 20 20  index..    **.  
2e2db 20 20 2a 2a 20 20 6e 49 6e 4d 75 6c 3a 20 20 0a    **  nInMul:  .
2e2dc 20 20 20 20 2a 2a 20 20 20 20 54 68 65 20 22 69      **    The "i
2e2dd 6e 2d 6d 75 6c 74 69 70 6c 69 65 72 22 2e 20 54  n-multiplier". T
2e2de 68 69 73 20 69 73 20 61 6e 20 65 73 74 69 6d 61  his is an estima
2e2df 74 65 20 6f 66 20 68 6f 77 20 6d 61 6e 79 20 73  te of how many s
2e2e0 65 65 6b 20 6f 70 65 72 61 74 69 6f 6e 73 20 0a  eek operations .
2e2e1 20 20 20 20 2a 2a 20 20 20 20 53 51 4c 69 74 65      **    SQLite
2e2e2 20 6d 75 73 74 20 70 65 72 66 6f 72 6d 20 6f 6e   must perform on
2e2e3 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20 71 75   the index in qu
2e2e4 65 73 74 69 6f 6e 2e 20 46 6f 72 20 65 78 61 6d  estion. For exam
2e2e5 70 6c 65 2c 20 69 66 20 74 68 65 20 0a 20 20 20  ple, if the .   
2e2e6 20 2a 2a 20 20 20 20 57 48 45 52 45 20 63 6c 61   **    WHERE cla
2e2e7 75 73 65 20 69 73 3a 0a 20 20 20 20 2a 2a 0a 20  use is:.    **. 
2e2e8 20 20 20 2a 2a 20 20 20 20 20 20 57 48 45 52 45     **      WHERE
2e2e9 20 61 20 49 4e 20 28 31 2c 20 32 2c 20 33 29 20   a IN (1, 2, 3) 
2e2ea 41 4e 44 20 62 20 49 4e 20 28 34 2c 20 35 2c 20  AND b IN (4, 5, 
2e2eb 36 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  6).    **.    **
2e2ec 20 20 20 20 53 51 4c 69 74 65 20 6d 75 73 74 20      SQLite must 
2e2ed 70 65 72 66 6f 72 6d 20 39 20 6c 6f 6f 6b 75 70  perform 9 lookup
2e2ee 73 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 6f 6e  s on an index on
2e2ef 20 28 61 2c 20 62 29 2c 20 73 6f 20 6e 49 6e 4d   (a, b), so nInM
2e2f0 75 6c 20 69 73 20 0a 20 20 20 20 2a 2a 20 20 20  ul is .    **   
2e2f1 20 73 65 74 20 74 6f 20 39 2e 20 47 69 76 65 6e   set to 9. Given
2e2f2 20 74 68 65 20 73 61 6d 65 20 73 63 68 65 6d 61   the same schema
2e2f3 20 61 6e 64 20 65 69 74 68 65 72 20 6f 66 20 74   and either of t
2e2f4 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 57 48 45  he following WHE
2e2f5 52 45 20 0a 20 20 20 20 2a 2a 20 20 20 20 63 6c  RE .    **    cl
2e2f6 61 75 73 65 73 3a 0a 20 20 20 20 2a 2a 0a 20 20  auses:.    **.  
2e2f7 20 20 2a 2a 20 20 20 20 20 20 57 48 45 52 45 20    **      WHERE 
2e2f8 61 20 3d 20 20 31 0a 20 20 20 20 2a 2a 20 20 20  a =  1.    **   
2e2f9 20 20 20 57 48 45 52 45 20 61 20 3e 3d 20 32 0a     WHERE a >= 2.
2e2fa 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
2e2fb 20 6e 49 6e 4d 75 6c 20 69 73 20 73 65 74 20 74   nInMul is set t
2e2fc 6f 20 31 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  o 1..    **.    
2e2fd 2a 2a 20 20 20 20 49 66 20 74 68 65 72 65 20 65  **    If there e
2e2fe 78 69 73 74 73 20 61 20 57 48 45 52 45 20 74 65  xists a WHERE te
2e2ff 72 6d 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  rm of the form "
2e300 78 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e  x IN (SELECT ...
2e301 29 22 2c 20 74 68 65 6e 20 0a 20 20 20 20 2a 2a  )", then .    **
2e302 20 20 20 20 74 68 65 20 73 75 62 2d 73 65 6c 65      the sub-sele
2e303 63 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f  ct is assumed to
2e304 20 72 65 74 75 72 6e 20 32 35 20 72 6f 77 73 20   return 25 rows 
2e305 66 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73  for the purposes
2e306 20 6f 66 20 0a 20 20 20 20 2a 2a 20 20 20 20 64   of .    **    d
2e307 65 74 65 72 6d 69 6e 69 6e 67 20 6e 49 6e 4d 75  etermining nInMu
2e308 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  l..    **.    **
2e309 20 20 62 49 6e 45 73 74 3a 20 20 0a 20 20 20 20    bInEst:  .    
2e30a 2a 2a 20 20 20 20 53 65 74 20 74 6f 20 74 72 75  **    Set to tru
2e30b 65 20 69 66 20 74 68 65 72 65 20 77 61 73 20 61  e if there was a
2e30c 74 20 6c 65 61 73 74 20 6f 6e 65 20 22 78 20 49  t least one "x I
2e30d 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 20  N (SELECT ...)" 
2e30e 74 65 72 6d 20 75 73 65 64 20 0a 20 20 20 20 2a  term used .    *
2e30f 2a 20 20 20 20 69 6e 20 64 65 74 65 72 6d 69 6e  *    in determin
2e310 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f 66  ing the value of
2e311 20 6e 49 6e 4d 75 6c 2e 0a 20 20 20 20 2a 2a 0a   nInMul..    **.
2e312 20 20 20 20 2a 2a 20 20 6e 42 6f 75 6e 64 3a 0a      **  nBound:.
2e313 20 20 20 20 2a 2a 20 20 20 20 41 6e 20 65 73 74      **    An est
2e314 69 6d 61 74 65 20 6f 6e 20 74 68 65 20 61 6d 6f  imate on the amo
2e315 75 6e 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65  unt of the table
2e316 20 74 68 61 74 20 6d 75 73 74 20 62 65 20 73 65   that must be se
2e317 61 72 63 68 65 64 2e 20 20 41 0a 20 20 20 20 2a  arched.  A.    *
2e318 2a 20 20 20 20 76 61 6c 75 65 20 6f 66 20 31 30  *    value of 10
2e319 30 20 6d 65 61 6e 73 20 74 68 65 20 65 6e 74 69  0 means the enti
2e31a 72 65 20 74 61 62 6c 65 20 69 73 20 73 65 61 72  re table is sear
2e31b 63 68 65 64 2e 20 20 52 61 6e 67 65 20 63 6f 6e  ched.  Range con
2e31c 73 74 72 61 69 6e 74 73 0a 20 20 20 20 2a 2a 20  straints.    ** 
2e31d 20 20 20 6d 69 67 68 74 20 72 65 64 75 63 65 20     might reduce 
2e31e 74 68 69 73 20 74 6f 20 61 20 76 61 6c 75 65 20  this to a value 
2e31f 6c 65 73 73 20 74 68 61 6e 20 31 30 30 20 74 6f  less than 100 to
2e320 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 6f   indicate that o
2e321 6e 6c 79 0a 20 20 20 20 2a 2a 20 20 20 20 61 20  nly.    **    a 
2e322 66 72 61 63 74 69 6f 6e 20 6f 66 20 74 68 65 20  fraction of the 
2e323 74 61 62 6c 65 20 6e 65 65 64 73 20 73 65 61 72  table needs sear
2e324 63 68 69 6e 67 2e 20 20 49 6e 20 74 68 65 20 61  ching.  In the a
2e325 62 73 65 6e 63 65 20 6f 66 0a 20 20 20 20 2a 2a  bsence of.    **
2e326 20 20 20 20 73 71 6c 69 74 65 5f 73 74 61 74 32      sqlite_stat2
2e327 20 41 4e 41 4c 59 5a 45 20 64 61 74 61 2c 20 61   ANALYZE data, a
2e328 20 73 69 6e 67 6c 65 20 69 6e 65 71 75 61 6c 69   single inequali
2e329 74 79 20 72 65 64 75 63 65 73 20 74 68 65 20 73  ty reduces the s
2e32a 65 61 72 63 68 0a 20 20 20 20 2a 2a 20 20 20 20  earch.    **    
2e32b 73 70 61 63 65 20 74 6f 20 31 2f 33 72 64 20 69  space to 1/3rd i
2e32c 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a 65  ts original size
2e32d 2e 20 20 53 6f 20 61 6e 20 78 3e 3f 20 63 6f 6e  .  So an x>? con
2e32e 73 74 72 61 69 6e 74 20 72 65 64 75 63 65 73 0a  straint reduces.
2e32f 20 20 20 20 2a 2a 20 20 20 20 6e 42 6f 75 6e 64      **    nBound
2e330 20 74 6f 20 33 33 2e 20 20 54 77 6f 20 63 6f 6e   to 33.  Two con
2e331 73 74 72 61 69 6e 74 73 20 28 78 3e 3f 20 41 4e  straints (x>? AN
2e332 44 20 78 3c 3f 29 20 72 65 64 75 63 65 20 6e 42  D x<?) reduce nB
2e333 6f 75 6e 64 20 74 6f 20 31 31 2e 0a 20 20 20 20  ound to 11..    
2e334 2a 2a 0a 20 20 20 20 2a 2a 20 20 62 53 6f 72 74  **.    **  bSort
2e335 3a 20 20 20 0a 20 20 20 20 2a 2a 20 20 20 20 42  :   .    **    B
2e336 6f 6f 6c 65 61 6e 2e 20 54 72 75 65 20 69 66 20  oolean. True if 
2e337 74 68 65 72 65 20 69 73 20 61 6e 20 4f 52 44 45  there is an ORDE
2e338 52 20 42 59 20 63 6c 61 75 73 65 20 74 68 61 74  R BY clause that
2e339 20 77 69 6c 6c 20 72 65 71 75 69 72 65 20 61 6e   will require an
2e33a 20 0a 20 20 20 20 2a 2a 20 20 20 20 65 78 74 65   .    **    exte
2e33b 72 6e 61 6c 20 73 6f 72 74 20 28 69 2e 65 2e 20  rnal sort (i.e. 
2e33c 73 63 61 6e 6e 69 6e 67 20 74 68 65 20 69 6e 64  scanning the ind
2e33d 65 78 20 62 65 69 6e 67 20 65 76 61 6c 75 61 74  ex being evaluat
2e33e 65 64 20 77 69 6c 6c 20 6e 6f 74 20 0a 20 20 20  ed will not .   
2e33f 20 2a 2a 20 20 20 20 63 6f 72 72 65 63 74 6c 79   **    correctly
2e340 20 6f 72 64 65 72 20 72 65 63 6f 72 64 73 29 2e   order records).
2e341 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
2e342 62 4c 6f 6f 6b 75 70 3a 20 0a 20 20 20 20 2a 2a  bLookup: .    **
2e343 20 20 20 20 42 6f 6f 6c 65 61 6e 2e 20 54 72 75      Boolean. Tru
2e344 65 20 69 66 20 66 6f 72 20 65 61 63 68 20 69 6e  e if for each in
2e345 64 65 78 20 65 6e 74 72 79 20 76 69 73 69 74 65  dex entry visite
2e346 64 20 61 20 6c 6f 6f 6b 75 70 20 6f 6e 20 74 68  d a lookup on th
2e347 65 20 0a 20 20 20 20 2a 2a 20 20 20 20 63 6f 72  e .    **    cor
2e348 72 65 73 70 6f 6e 64 69 6e 67 20 74 61 62 6c 65  responding table
2e349 20 62 2d 74 72 65 65 20 69 73 20 72 65 71 75 69   b-tree is requi
2e34a 72 65 64 2e 20 54 68 69 73 20 69 73 20 61 6c 77  red. This is alw
2e34b 61 79 73 20 66 61 6c 73 65 20 0a 20 20 20 20 2a  ays false .    *
2e34c 2a 20 20 20 20 66 6f 72 20 74 68 65 20 72 6f 77  *    for the row
2e34d 69 64 20 69 6e 64 65 78 2e 20 46 6f 72 20 6f 74  id index. For ot
2e34e 68 65 72 20 69 6e 64 65 78 65 73 2c 20 69 74 20  her indexes, it 
2e34f 69 73 20 74 72 75 65 20 75 6e 6c 65 73 73 20 61  is true unless a
2e350 6c 6c 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 20  ll the .    **  
2e351 20 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65    columns of the
2e352 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20 74   table used by t
2e353 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
2e354 65 6e 74 20 61 72 65 20 70 72 65 73 65 6e 74 20  ent are present 
2e355 69 6e 20 0a 20 20 20 20 2a 2a 20 20 20 20 74 68  in .    **    th
2e356 65 20 69 6e 64 65 78 20 28 73 75 63 68 20 61 6e  e index (such an
2e357 20 69 6e 64 65 78 20 69 73 20 73 6f 6d 65 74 69   index is someti
2e358 6d 65 73 20 64 65 73 63 72 69 62 65 64 20 61 73  mes described as
2e359 20 61 20 63 6f 76 65 72 69 6e 67 20 69 6e 64 65   a covering inde
2e35a 78 29 2e 0a 20 20 20 20 2a 2a 20 20 20 20 46 6f  x)..    **    Fo
2e35b 72 20 65 78 61 6d 70 6c 65 2c 20 67 69 76 65 6e  r example, given
2e35c 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 20 28 61   the index on (a
2e35d 2c 20 62 29 2c 20 74 68 65 20 73 65 63 6f 6e 64  , b), the second
2e35e 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
2e35f 67 20 0a 20 20 20 20 2a 2a 20 20 20 20 74 77 6f  g .    **    two
2e360 20 71 75 65 72 69 65 73 20 72 65 71 75 69 72 65   queries require
2e361 73 20 74 61 62 6c 65 20 62 2d 74 72 65 65 20 6c  s table b-tree l
2e362 6f 6f 6b 75 70 73 2c 20 62 75 74 20 74 68 65 20  ookups, but the 
2e363 66 69 72 73 74 20 64 6f 65 73 20 6e 6f 74 2e 0a  first does not..
2e364 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
2e365 20 20 20 20 20 20 20 20 20 20 53 45 4c 45 43 54            SELECT
2e366 20 61 2c 20 62 20 20 20 20 46 52 4f 4d 20 74 62   a, b    FROM tb
2e367 6c 20 57 48 45 52 45 20 61 20 3d 20 31 3b 0a 20  l WHERE a = 1;. 
2e368 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20     **           
2e369 20 20 53 45 4c 45 43 54 20 61 2c 20 62 2c 20 63    SELECT a, b, c
2e36a 20 46 52 4f 4d 20 74 62 6c 20 57 48 45 52 45 20   FROM tbl WHERE 
2e36b 61 20 3d 20 31 3b 0a 20 20 20 20 2a 2f 0a 20 20  a = 1;.    */.  
2e36c 20 20 69 6e 74 20 6e 45 71 3b 0a 20 20 20 20 69    int nEq;.    i
2e36d 6e 74 20 62 49 6e 45 73 74 20 3d 20 30 3b 0a 20  nt bInEst = 0;. 
2e36e 20 20 20 69 6e 74 20 6e 49 6e 4d 75 6c 20 3d 20     int nInMul = 
2e36f 31 3b 0a 20 20 20 20 69 6e 74 20 6e 42 6f 75 6e  1;.    int nBoun
2e370 64 20 3d 20 31 30 30 3b 0a 20 20 20 20 69 6e 74  d = 100;.    int
2e371 20 62 53 6f 72 74 20 3d 20 30 3b 0a 20 20 20 20   bSort = 0;.    
2e372 69 6e 74 20 62 4c 6f 6f 6b 75 70 20 3d 20 30 3b  int bLookup = 0;
2e373 0a 0a 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69  ..    /* Determi
2e374 6e 65 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66  ne the values of
2e375 20 6e 45 71 20 61 6e 64 20 6e 49 6e 4d 75 6c 20   nEq and nInMul 
2e376 2a 2f 0a 20 20 20 20 66 6f 72 28 6e 45 71 3d 30  */.    for(nEq=0
2e377 3b 20 6e 45 71 3c 70 50 72 6f 62 65 2d 3e 6e 43  ; nEq<pProbe->nC
2e378 6f 6c 75 6d 6e 3b 20 6e 45 71 2b 2b 29 7b 0a 20  olumn; nEq++){. 
2e379 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a       WhereTerm *
2e37a 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20  pTerm;          
2e37b 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 74 65 72   /* A single ter
2e37c 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63  m of the WHERE c
2e37d 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20 69  lause */.      i
2e37e 6e 74 20 6a 20 3d 20 70 50 72 6f 62 65 2d 3e 61  nt j = pProbe->a
2e37f 69 43 6f 6c 75 6d 6e 5b 6e 45 71 5d 3b 0a 20 20  iColumn[nEq];.  
2e380 20 20 20 20 70 54 65 72 6d 20 3d 20 66 69 6e 64      pTerm = find
2e381 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20  Term(pWC, iCur, 
2e382 6a 2c 20 6e 6f 74 52 65 61 64 79 2c 20 65 71 54  j, notReady, eqT
2e383 65 72 6d 4d 61 73 6b 2c 20 70 49 64 78 29 3b 0a  ermMask, pIdx);.
2e384 20 20 20 20 20 20 69 66 28 20 70 54 65 72 6d 3d        if( pTerm=
2e385 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =0 ) break;.    
2e386 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 28 57 48    wsFlags |= (WH
2e387 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45 51 7c 57 48  ERE_COLUMN_EQ|WH
2e388 45 52 45 5f 52 4f 57 49 44 5f 45 51 29 3b 0a 20  ERE_ROWID_EQ);. 
2e389 20 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e       if( pTerm->
2e38a 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 49  eOperator & WO_I
2e38b 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 45 78 70  N ){.        Exp
2e38c 72 20 2a 70 45 78 70 72 20 3d 20 70 54 65 72 6d  r *pExpr = pTerm
2e38d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20  ->pExpr;.       
2e38e 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52   wsFlags |= WHER
2e38f 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 3b 0a 20 20 20  E_COLUMN_IN;.   
2e390 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73       if( ExprHas
2e391 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
2e392 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b  EP_xIsSelect) ){
2e393 0a 20 20 20 20 20 20 20 20 20 20 6e 49 6e 4d 75  .          nInMu
2e394 6c 20 2a 3d 20 32 35 3b 0a 20 20 20 20 20 20 20  l *= 25;.       
2e395 20 20 20 62 49 6e 45 73 74 20 3d 20 31 3b 0a 20     bInEst = 1;. 
2e396 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
2e397 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20   pExpr->x.pList 
2e398 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 49 6e  ){.          nIn
2e399 4d 75 6c 20 2a 3d 20 70 45 78 70 72 2d 3e 78 2e  Mul *= pExpr->x.
2e39a 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 2b 20 31  pList->nExpr + 1
2e39b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2e39c 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54 65 72    }else if( pTer
2e39d 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57  m->eOperator & W
2e39e 4f 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  O_ISNULL ){.    
2e39f 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57      wsFlags |= W
2e3a0 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c  HERE_COLUMN_NULL
2e3a1 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2e3a2 75 73 65 64 20 7c 3d 20 70 54 65 72 6d 2d 3e 70  used |= pTerm->p
2e3a3 72 65 72 65 71 52 69 67 68 74 3b 0a 20 20 20 20  rereqRight;.    
2e3a4 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 74 65 72 6d  }..    /* Determ
2e3a5 69 6e 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66  ine the value of
2e3a6 20 6e 42 6f 75 6e 64 2e 20 2a 2f 0a 20 20 20 20   nBound. */.    
2e3a7 69 66 28 20 6e 45 71 3c 70 50 72 6f 62 65 2d 3e  if( nEq<pProbe->
2e3a8 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20  nColumn ){.     
2e3a9 20 69 6e 74 20 6a 20 3d 20 70 50 72 6f 62 65 2d   int j = pProbe-
2e3aa 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45 71 5d 3b 0a  >aiColumn[nEq];.
2e3ab 20 20 20 20 20 20 69 66 28 20 66 69 6e 64 54 65        if( findTe
2e3ac 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c  rm(pWC, iCur, j,
2e3ad 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 4c 54   notReady, WO_LT
2e3ae 7c 57 4f 5f 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f  |WO_LE|WO_GT|WO_
2e3af 47 45 2c 20 70 49 64 78 29 20 29 7b 0a 20 20 20  GE, pIdx) ){.   
2e3b0 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a       WhereTerm *
2e3b1 70 54 6f 70 20 3d 20 66 69 6e 64 54 65 72 6d 28  pTop = findTerm(
2e3b2 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e 6f  pWC, iCur, j, no
2e3b3 74 52 65 61 64 79 2c 20 57 4f 5f 4c 54 7c 57 4f  tReady, WO_LT|WO
2e3b4 5f 4c 45 2c 20 70 49 64 78 29 3b 0a 20 20 20 20  _LE, pIdx);.    
2e3b5 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70      WhereTerm *p
2e3b6 42 74 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70  Btm = findTerm(p
2e3b7 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e 6f 74  WC, iCur, j, not
2e3b8 52 65 61 64 79 2c 20 57 4f 5f 47 54 7c 57 4f 5f  Ready, WO_GT|WO_
2e3b9 47 45 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20  GE, pIdx);.     
2e3ba 20 20 20 77 68 65 72 65 52 61 6e 67 65 53 63 61     whereRangeSca
2e3bb 6e 45 73 74 28 70 50 61 72 73 65 2c 20 70 50 72  nEst(pParse, pPr
2e3bc 6f 62 65 2c 20 6e 45 71 2c 20 70 42 74 6d 2c 20  obe, nEq, pBtm, 
2e3bd 70 54 6f 70 2c 20 26 6e 42 6f 75 6e 64 29 3b 0a  pTop, &nBound);.
2e3be 20 20 20 20 20 20 20 20 69 66 28 20 70 54 6f 70          if( pTop
2e3bf 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 77 73   ){.          ws
2e3c0 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 54  Flags |= WHERE_T
2e3c1 4f 50 5f 4c 49 4d 49 54 3b 0a 20 20 20 20 20 20  OP_LIMIT;.      
2e3c2 20 20 20 20 75 73 65 64 20 7c 3d 20 70 54 6f 70      used |= pTop
2e3c3 2d 3e 70 72 65 72 65 71 52 69 67 68 74 3b 0a 20  ->prereqRight;. 
2e3c4 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2e3c5 20 69 66 28 20 70 42 74 6d 20 29 7b 0a 20 20 20   if( pBtm ){.   
2e3c6 20 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c         wsFlags |
2e3c7 3d 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49  = WHERE_BTM_LIMI
2e3c8 54 3b 0a 20 20 20 20 20 20 20 20 20 20 75 73 65  T;.          use
2e3c9 64 20 7c 3d 20 70 42 74 6d 2d 3e 70 72 65 72 65  d |= pBtm->prere
2e3ca 71 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20  qRight;.        
2e3cb 7d 0a 20 20 20 20 20 20 20 20 77 73 46 6c 61 67  }.        wsFlag
2e3cc 73 20 7c 3d 20 28 57 48 45 52 45 5f 43 4f 4c 55  s |= (WHERE_COLU
2e3cd 4d 4e 5f 52 41 4e 47 45 7c 57 48 45 52 45 5f 52  MN_RANGE|WHERE_R
2e3ce 4f 57 49 44 5f 52 41 4e 47 45 29 3b 0a 20 20 20  OWID_RANGE);.   
2e3cf 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69     }.    }else i
2e3d0 66 28 20 70 50 72 6f 62 65 2d 3e 6f 6e 45 72 72  f( pProbe->onErr
2e3d1 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0a 20  or!=OE_None ){. 
2e3d2 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 77       testcase( w
2e3d3 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 43  sFlags & WHERE_C
2e3d4 4f 4c 55 4d 4e 5f 49 4e 20 29 3b 0a 20 20 20 20  OLUMN_IN );.    
2e3d5 20 20 74 65 73 74 63 61 73 65 28 20 77 73 46 6c    testcase( wsFl
2e3d6 61 67 73 20 26 20 57 48 45 52 45 5f 43 4f 4c 55  ags & WHERE_COLU
2e3d7 4d 4e 5f 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20  MN_NULL );.     
2e3d8 20 69 66 28 20 28 77 73 46 6c 61 67 73 20 26 20   if( (wsFlags & 
2e3d9 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e  (WHERE_COLUMN_IN
2e3da 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55  |WHERE_COLUMN_NU
2e3db 4c 4c 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  LL))==0 ){.     
2e3dc 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48     wsFlags |= WH
2e3dd 45 52 45 5f 55 4e 49 51 55 45 3b 0a 20 20 20 20  ERE_UNIQUE;.    
2e3de 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
2e3df 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6e  * If there is an
2e3e0 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2e3e1 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78 20 62   and the index b
2e3e2 65 69 6e 67 20 63 6f 6e 73 69 64 65 72 65 64 20  eing considered 
2e3e3 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 6e 61 74 75  will.    ** natu
2e3e4 72 61 6c 6c 79 20 73 63 61 6e 20 72 6f 77 73 20  rally scan rows 
2e3e5 69 6e 20 74 68 65 20 72 65 71 75 69 72 65 64 20  in the required 
2e3e6 6f 72 64 65 72 2c 20 73 65 74 20 74 68 65 20 61  order, set the a
2e3e7 70 70 72 6f 70 72 69 61 74 65 20 66 6c 61 67 73  ppropriate flags
2e3e8 0a 20 20 20 20 2a 2a 20 69 6e 20 77 73 46 6c 61  .    ** in wsFla
2e3e9 67 73 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  gs. Otherwise, i
2e3ea 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 4f 52  f there is an OR
2e3eb 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 62 75  DER BY clause bu
2e3ec 74 20 74 68 65 20 69 6e 64 65 78 0a 20 20 20 20  t the index.    
2e3ed 2a 2a 20 77 69 6c 6c 20 73 63 61 6e 20 72 6f 77  ** will scan row
2e3ee 73 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74  s in a different
2e3ef 20 6f 72 64 65 72 2c 20 73 65 74 20 74 68 65 20   order, set the 
2e3f0 62 53 6f 72 74 20 76 61 72 69 61 62 6c 65 2e 20  bSort variable. 
2e3f1 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 72 64   */.    if( pOrd
2e3f2 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 69 66  erBy ){.      if
2e3f3 28 20 28 77 73 46 6c 61 67 73 20 26 20 28 57 48  ( (wsFlags & (WH
2e3f4 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 7c 57 48  ERE_COLUMN_IN|WH
2e3f5 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c 29  ERE_COLUMN_NULL)
2e3f6 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 26 26 20  )==0.        && 
2e3f7 69 73 53 6f 72 74 69 6e 67 49 6e 64 65 78 28 70  isSortingIndex(p
2e3f8 50 61 72 73 65 2c 70 57 43 2d 3e 70 4d 61 73 6b  Parse,pWC->pMask
2e3f9 53 65 74 2c 70 50 72 6f 62 65 2c 69 43 75 72 2c  Set,pProbe,iCur,
2e3fa 70 4f 72 64 65 72 42 79 2c 6e 45 71 2c 26 72 65  pOrderBy,nEq,&re
2e3fb 76 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  v).      ){.    
2e3fc 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57      wsFlags |= W
2e3fd 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45  HERE_ROWID_RANGE
2e3fe 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41  |WHERE_COLUMN_RA
2e3ff 4e 47 45 7c 57 48 45 52 45 5f 4f 52 44 45 52 42  NGE|WHERE_ORDERB
2e400 59 3b 0a 20 20 20 20 20 20 20 20 77 73 46 6c 61  Y;.        wsFla
2e401 67 73 20 7c 3d 20 28 72 65 76 20 3f 20 57 48 45  gs |= (rev ? WHE
2e402 52 45 5f 52 45 56 45 52 53 45 20 3a 20 30 29 3b  RE_REVERSE : 0);
2e403 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2e404 20 20 20 20 20 20 62 53 6f 72 74 20 3d 20 31 3b        bSort = 1;
2e405 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
2e406 20 20 20 20 2f 2a 20 49 66 20 63 75 72 72 65 6e      /* If curren
2e407 74 6c 79 20 63 61 6c 63 75 6c 61 74 69 6e 67 20  tly calculating 
2e408 74 68 65 20 63 6f 73 74 20 6f 66 20 75 73 69 6e  the cost of usin
2e409 67 20 61 6e 20 69 6e 64 65 78 20 28 6e 6f 74 20  g an index (not 
2e40a 74 68 65 20 49 50 4b 0a 20 20 20 20 2a 2a 20 69  the IPK.    ** i
2e40b 6e 64 65 78 29 2c 20 64 65 74 65 72 6d 69 6e 65  ndex), determine
2e40c 20 69 66 20 61 6c 6c 20 72 65 71 75 69 72 65 64   if all required
2e40d 20 63 6f 6c 75 6d 6e 20 64 61 74 61 20 6d 61 79   column data may
2e40e 20 62 65 20 6f 62 74 61 69 6e 65 64 20 77 69 74   be obtained wit
2e40f 68 6f 75 74 20 0a 20 20 20 20 2a 2a 20 73 65 65  hout .    ** see
2e410 6b 69 6e 67 20 74 6f 20 65 6e 74 72 69 65 73 20  king to entries 
2e411 69 6e 20 74 68 65 20 6d 61 69 6e 20 74 61 62 6c  in the main tabl
2e412 65 20 28 69 2e 65 2e 20 69 66 20 74 68 65 20 69  e (i.e. if the i
2e413 6e 64 65 78 20 69 73 20 61 20 63 6f 76 65 72 69  ndex is a coveri
2e414 6e 67 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78 20  ng.    ** index 
2e415 66 6f 72 20 74 68 69 73 20 71 75 65 72 79 29 2e  for this query).
2e416 20 49 66 20 69 74 20 69 73 2c 20 73 65 74 20 74   If it is, set t
2e417 68 65 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c  he WHERE_IDX_ONL
2e418 59 20 66 6c 61 67 20 69 6e 0a 20 20 20 20 2a 2a  Y flag in.    **
2e419 20 77 73 46 6c 61 67 73 2e 20 4f 74 68 65 72 77   wsFlags. Otherw
2e41a 69 73 65 2c 20 73 65 74 20 74 68 65 20 62 4c 6f  ise, set the bLo
2e41b 6f 6b 75 70 20 76 61 72 69 61 62 6c 65 20 74 6f  okup variable to
2e41c 20 74 72 75 65 2e 20 20 2a 2f 0a 20 20 20 20 69   true.  */.    i
2e41d 66 28 20 70 49 64 78 20 26 26 20 77 73 46 6c 61  f( pIdx && wsFla
2e41e 67 73 20 29 7b 0a 20 20 20 20 20 20 42 69 74 6d  gs ){.      Bitm
2e41f 61 73 6b 20 6d 20 3d 20 70 53 72 63 2d 3e 63 6f  ask m = pSrc->co
2e420 6c 55 73 65 64 3b 0a 20 20 20 20 20 20 69 6e 74  lUsed;.      int
2e421 20 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d   j;.      for(j=
2e422 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75  0; j<pIdx->nColu
2e423 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  mn; j++){.      
2e424 20 20 69 6e 74 20 78 20 3d 20 70 49 64 78 2d 3e    int x = pIdx->
2e425 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20  aiColumn[j];.   
2e426 20 20 20 20 20 69 66 28 20 78 3c 42 4d 53 2d 31       if( x<BMS-1
2e427 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 20   ){.          m 
2e428 26 3d 20 7e 28 28 28 42 69 74 6d 61 73 6b 29 31  &= ~(((Bitmask)1
2e429 29 3c 3c 78 29 3b 0a 20 20 20 20 20 20 20 20 7d  )<<x);.        }
2e42a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
2e42b 66 28 20 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 20  f( m==0 ){.     
2e42c 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48     wsFlags |= WH
2e42d 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 3b 0a 20 20  ERE_IDX_ONLY;.  
2e42e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2e42f 20 20 20 62 4c 6f 6f 6b 75 70 20 3d 20 31 3b 0a     bLookup = 1;.
2e430 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
2e431 20 20 20 2f 2a 2a 2a 2a 20 42 65 67 69 6e 20 61     /**** Begin a
2e432 64 64 69 6e 67 20 75 70 20 74 68 65 20 63 6f 73  dding up the cos
2e433 74 20 6f 66 20 75 73 69 6e 67 20 74 68 69 73 20  t of using this 
2e434 69 6e 64 65 78 20 28 4e 65 65 64 73 20 69 6d 70  index (Needs imp
2e435 72 6f 76 65 6d 65 6e 74 73 29 0a 20 20 20 20 2a  rovements).    *
2e436 2a 0a 20 20 20 20 2a 2a 20 45 73 74 69 6d 61 74  *.    ** Estimat
2e437 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
2e438 72 6f 77 73 20 6f 66 20 6f 75 74 70 75 74 2e 20  rows of output. 
2e439 20 46 6f 72 20 61 6e 20 49 4e 20 6f 70 65 72 61   For an IN opera
2e43a 74 6f 72 2c 0a 20 20 20 20 2a 2a 20 64 6f 20 6e  tor,.    ** do n
2e43b 6f 74 20 6c 65 74 20 74 68 65 20 65 73 74 69 6d  ot let the estim
2e43c 61 74 65 20 65 78 63 65 65 64 20 68 61 6c 66 20  ate exceed half 
2e43d 74 68 65 20 72 6f 77 73 20 69 6e 20 74 68 65 20  the rows in the 
2e43e 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  table..    */.  
2e43f 20 20 6e 52 6f 77 20 3d 20 28 64 6f 75 62 6c 65    nRow = (double
2e440 29 28 61 69 52 6f 77 45 73 74 5b 6e 45 71 5d 20  )(aiRowEst[nEq] 
2e441 2a 20 6e 49 6e 4d 75 6c 29 3b 0a 20 20 20 20 69  * nInMul);.    i
2e442 66 28 20 62 49 6e 45 73 74 20 26 26 20 6e 52 6f  f( bInEst && nRo
2e443 77 2a 32 3e 61 69 52 6f 77 45 73 74 5b 30 5d 20  w*2>aiRowEst[0] 
2e444 29 7b 0a 20 20 20 20 20 20 6e 52 6f 77 20 3d 20  ){.      nRow = 
2e445 61 69 52 6f 77 45 73 74 5b 30 5d 2f 32 3b 0a 20  aiRowEst[0]/2;. 
2e446 20 20 20 20 20 6e 49 6e 4d 75 6c 20 3d 20 28 69       nInMul = (i
2e447 6e 74 29 28 6e 52 6f 77 20 2f 20 61 69 52 6f 77  nt)(nRow / aiRow
2e448 45 73 74 5b 6e 45 71 5d 29 3b 0a 20 20 20 20 7d  Est[nEq]);.    }
2e449 0a 0a 20 20 20 20 2f 2a 20 41 73 73 75 6d 65 20  ..    /* Assume 
2e44a 63 6f 6e 73 74 61 6e 74 20 63 6f 73 74 20 74 6f  constant cost to
2e44b 20 61 63 63 65 73 73 20 61 20 72 6f 77 20 61 6e   access a row an
2e44c 64 20 6c 6f 67 61 72 69 74 68 6d 69 63 20 63 6f  d logarithmic co
2e44d 73 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64 6f 20  st to.    ** do 
2e44e 61 20 62 69 6e 61 72 79 20 73 65 61 72 63 68 2e  a binary search.
2e44f 20 20 48 65 6e 63 65 2c 20 74 68 65 20 69 6e 69    Hence, the ini
2e450 74 69 61 6c 20 63 6f 73 74 20 69 73 20 74 68 65  tial cost is the
2e451 20 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74 70 75   number of outpu
2e452 74 0a 20 20 20 20 2a 2a 20 72 6f 77 73 20 70 6c  t.    ** rows pl
2e453 75 73 20 6c 6f 67 32 28 74 61 62 6c 65 2d 73 69  us log2(table-si
2e454 7a 65 29 20 74 69 6d 65 73 20 74 68 65 20 6e 75  ze) times the nu
2e455 6d 62 65 72 20 6f 66 20 62 69 6e 61 72 79 20 73  mber of binary s
2e456 65 61 72 63 68 65 73 2e 0a 20 20 20 20 2a 2f 0a  earches..    */.
2e457 20 20 20 20 63 6f 73 74 20 3d 20 6e 52 6f 77 20      cost = nRow 
2e458 2b 20 6e 49 6e 4d 75 6c 2a 65 73 74 4c 6f 67 28  + nInMul*estLog(
2e459 61 69 52 6f 77 45 73 74 5b 30 5d 29 3b 0a 0a 20  aiRowEst[0]);.. 
2e45a 20 20 20 2f 2a 20 41 64 6a 75 73 74 20 74 68 65     /* Adjust the
2e45b 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   number of rows 
2e45c 61 6e 64 20 74 68 65 20 63 6f 73 74 20 64 6f 77  and the cost dow
2e45d 6e 77 61 72 64 20 74 6f 20 72 65 66 6c 65 63 74  nward to reflect
2e45e 20 72 6f 77 73 0a 20 20 20 20 2a 2a 20 74 68 61   rows.    ** tha
2e45f 74 20 61 72 65 20 65 78 63 6c 75 64 65 64 20 62  t are excluded b
2e460 79 20 72 61 6e 67 65 20 63 6f 6e 73 74 72 61 69  y range constrai
2e461 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  nts..    */.    
2e462 6e 52 6f 77 20 3d 20 28 6e 52 6f 77 20 2a 20 28  nRow = (nRow * (
2e463 64 6f 75 62 6c 65 29 6e 42 6f 75 6e 64 29 20 2f  double)nBound) /
2e464 20 28 64 6f 75 62 6c 65 29 31 30 30 3b 0a 20 20   (double)100;.  
2e465 20 20 63 6f 73 74 20 3d 20 28 63 6f 73 74 20 2a    cost = (cost *
2e466 20 28 64 6f 75 62 6c 65 29 6e 42 6f 75 6e 64 29   (double)nBound)
2e467 20 2f 20 28 64 6f 75 62 6c 65 29 31 30 30 3b 0a   / (double)100;.
2e468 0a 20 20 20 20 2f 2a 20 41 64 64 20 69 6e 20 74  .    /* Add in t
2e469 68 65 20 65 73 74 69 6d 61 74 65 64 20 63 6f 73  he estimated cos
2e46a 74 20 6f 66 20 73 6f 72 74 69 6e 67 20 74 68 65  t of sorting the
2e46b 20 72 65 73 75 6c 74 0a 20 20 20 20 2a 2f 0a 20   result.    */. 
2e46c 20 20 20 69 66 28 20 62 53 6f 72 74 20 29 7b 0a     if( bSort ){.
2e46d 20 20 20 20 20 20 63 6f 73 74 20 2b 3d 20 63 6f        cost += co
2e46e 73 74 2a 65 73 74 4c 6f 67 28 63 6f 73 74 29 3b  st*estLog(cost);
2e46f 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49  .    }..    /* I
2e470 66 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  f all informatio
2e471 6e 20 63 61 6e 20 62 65 20 74 61 6b 65 6e 20 64  n can be taken d
2e472 69 72 65 63 74 6c 79 20 66 72 6f 6d 20 74 68 65  irectly from the
2e473 20 69 6e 64 65 78 2c 20 77 65 20 61 76 6f 69 64   index, we avoid
2e474 0a 20 20 20 20 2a 2a 20 64 6f 69 6e 67 20 74 61  .    ** doing ta
2e475 62 6c 65 20 6c 6f 6f 6b 75 70 73 2e 20 20 54 68  ble lookups.  Th
2e476 69 73 20 72 65 64 75 63 65 73 20 74 68 65 20 63  is reduces the c
2e477 6f 73 74 20 62 79 20 68 61 6c 66 2e 20 20 28 4e  ost by half.  (N
2e478 6f 74 20 72 65 61 6c 6c 79 20 2d 0a 20 20 20 20  ot really -.    
2e479 2a 2a 20 74 68 69 73 20 6e 65 65 64 73 20 74 6f  ** this needs to
2e47a 20 62 65 20 66 69 78 65 64 2e 29 0a 20 20 20 20   be fixed.).    
2e47b 2a 2f 0a 20 20 20 20 69 66 28 20 70 49 64 78 20  */.    if( pIdx 
2e47c 26 26 20 62 4c 6f 6f 6b 75 70 3d 3d 30 20 29 7b  && bLookup==0 ){
2e47d 0a 20 20 20 20 20 20 63 6f 73 74 20 2f 3d 20 28  .      cost /= (
2e47e 64 6f 75 62 6c 65 29 32 3b 0a 20 20 20 20 7d 0a  double)2;.    }.
2e47f 20 20 20 20 2f 2a 2a 2a 2a 20 43 6f 73 74 20 6f      /**** Cost o
2e480 66 20 75 73 69 6e 67 20 74 68 69 73 20 69 6e 64  f using this ind
2e481 65 78 20 68 61 73 20 6e 6f 77 20 62 65 65 6e 20  ex has now been 
2e482 63 6f 6d 70 75 74 65 64 20 2a 2a 2a 2a 2f 0a 0a  computed ****/..
2e483 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28      WHERETRACE((
2e484 0a 20 20 20 20 20 20 22 74 62 6c 3d 25 73 20 69  .      "tbl=%s i
2e485 64 78 3d 25 73 20 6e 45 71 3d 25 64 20 6e 49 6e  dx=%s nEq=%d nIn
2e486 4d 75 6c 3d 25 64 20 6e 42 6f 75 6e 64 3d 25 64  Mul=%d nBound=%d
2e487 20 62 53 6f 72 74 3d 25 64 20 62 4c 6f 6f 6b 75   bSort=%d bLooku
2e488 70 3d 25 64 22 0a 20 20 20 20 20 20 22 20 77 73  p=%d".      " ws
2e489 46 6c 61 67 73 3d 25 64 20 20 20 28 6e 52 6f 77  Flags=%d   (nRow
2e48a 3d 25 2e 32 66 20 63 6f 73 74 3d 25 2e 32 66 29  =%.2f cost=%.2f)
2e48b 5c 6e 22 2c 0a 20 20 20 20 20 20 70 53 72 63 2d  \n",.      pSrc-
2e48c 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 28 70  >pTab->zName, (p
2e48d 49 64 78 20 3f 20 70 49 64 78 2d 3e 7a 4e 61 6d  Idx ? pIdx->zNam
2e48e 65 20 3a 20 22 69 70 6b 22 29 2c 20 0a 20 20 20  e : "ipk"), .   
2e48f 20 20 20 6e 45 71 2c 20 6e 49 6e 4d 75 6c 2c 20     nEq, nInMul, 
2e490 6e 42 6f 75 6e 64 2c 20 62 53 6f 72 74 2c 20 62  nBound, bSort, b
2e491 4c 6f 6f 6b 75 70 2c 20 77 73 46 6c 61 67 73 2c  Lookup, wsFlags,
2e492 20 6e 52 6f 77 2c 20 63 6f 73 74 0a 20 20 20 20   nRow, cost.    
2e493 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74  ));..    /* If t
2e494 68 69 73 20 69 6e 64 65 78 20 69 73 20 74 68 65  his index is the
2e495 20 62 65 73 74 20 77 65 20 68 61 76 65 20 73 65   best we have se
2e496 65 6e 20 73 6f 20 66 61 72 2c 20 74 68 65 6e 20  en so far, then 
2e497 72 65 63 6f 72 64 20 74 68 69 73 0a 20 20 20 20  record this.    
2e498 2a 2a 20 69 6e 64 65 78 20 61 6e 64 20 69 74 73  ** index and its
2e499 20 63 6f 73 74 20 69 6e 20 74 68 65 20 70 43 6f   cost in the pCo
2e49a 73 74 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20  st structure..  
2e49b 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 21 70    */.    if( (!p
2e49c 49 64 78 20 7c 7c 20 77 73 46 6c 61 67 73 29 20  Idx || wsFlags) 
2e49d 26 26 20 63 6f 73 74 3c 70 43 6f 73 74 2d 3e 72  && cost<pCost->r
2e49e 43 6f 73 74 20 29 7b 0a 20 20 20 20 20 20 70 43  Cost ){.      pC
2e49f 6f 73 74 2d 3e 72 43 6f 73 74 20 3d 20 63 6f 73  ost->rCost = cos
2e4a0 74 3b 0a 20 20 20 20 20 20 70 43 6f 73 74 2d 3e  t;.      pCost->
2e4a1 6e 52 6f 77 20 3d 20 6e 52 6f 77 3b 0a 20 20 20  nRow = nRow;.   
2e4a2 20 20 20 70 43 6f 73 74 2d 3e 75 73 65 64 20 3d     pCost->used =
2e4a3 20 75 73 65 64 3b 0a 20 20 20 20 20 20 70 43 6f   used;.      pCo
2e4a4 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  st->plan.wsFlags
2e4a5 20 3d 20 28 77 73 46 6c 61 67 73 26 77 73 46 6c   = (wsFlags&wsFl
2e4a6 61 67 4d 61 73 6b 29 3b 0a 20 20 20 20 20 20 70  agMask);.      p
2e4a7 43 6f 73 74 2d 3e 70 6c 61 6e 2e 6e 45 71 20 3d  Cost->plan.nEq =
2e4a8 20 6e 45 71 3b 0a 20 20 20 20 20 20 70 43 6f 73   nEq;.      pCos
2e4a9 74 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 20 3d  t->plan.u.pIdx =
2e4aa 20 70 49 64 78 3b 0a 20 20 20 20 7d 0a 0a 20 20   pIdx;.    }..  
2e4ab 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 77 61    /* If there wa
2e4ac 73 20 61 6e 20 49 4e 44 45 58 45 44 20 42 59 20  s an INDEXED BY 
2e4ad 63 6c 61 75 73 65 2c 20 74 68 65 6e 20 6f 6e 6c  clause, then onl
2e4ae 79 20 74 68 61 74 20 6f 6e 65 20 69 6e 64 65 78  y that one index
2e4af 20 69 73 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 69   is.    ** consi
2e4b0 64 65 72 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66  dered. */.    if
2e4b1 28 20 70 53 72 63 2d 3e 70 49 6e 64 65 78 20 29  ( pSrc->pIndex )
2e4b2 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 2f 2a 20   break;..    /* 
2e4b3 52 65 73 65 74 20 6d 61 73 6b 73 20 66 6f 72 20  Reset masks for 
2e4b4 74 68 65 20 6e 65 78 74 20 69 6e 64 65 78 20 69  the next index i
2e4b5 6e 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20  n the loop */.  
2e4b6 20 20 77 73 46 6c 61 67 4d 61 73 6b 20 3d 20 7e    wsFlagMask = ~
2e4b7 28 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 7c  (WHERE_ROWID_EQ|
2e4b8 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47  WHERE_ROWID_RANG
2e4b9 45 29 3b 0a 20 20 20 20 65 71 54 65 72 6d 4d 61  E);.    eqTermMa
2e4ba 73 6b 20 3d 20 69 64 78 45 71 54 65 72 6d 4d 61  sk = idxEqTermMa
2e4bb 73 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  sk;.  }..  /* If
2e4bc 20 74 68 65 72 65 20 69 73 20 6e 6f 20 4f 52 44   there is no ORD
2e4bd 45 52 20 42 59 20 63 6c 61 75 73 65 20 61 6e 64  ER BY clause and
2e4be 20 74 68 65 20 53 51 4c 49 54 45 5f 52 65 76 65   the SQLITE_Reve
2e4bf 72 73 65 4f 72 64 65 72 20 66 6c 61 67 0a 20 20  rseOrder flag.  
2e4c0 2a 2a 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20  ** is set, then 
2e4c1 72 65 76 65 72 73 65 20 74 68 65 20 6f 72 64 65  reverse the orde
2e4c2 72 20 74 68 61 74 20 74 68 65 20 69 6e 64 65 78  r that the index
2e4c3 20 77 69 6c 6c 20 62 65 20 73 63 61 6e 6e 65 64   will be scanned
2e4c4 0a 20 20 2a 2a 20 69 6e 2e 20 54 68 69 73 20 69  .  ** in. This i
2e4c5 73 20 75 73 65 64 20 66 6f 72 20 61 70 70 6c 69  s used for appli
2e4c6 63 61 74 69 6f 6e 20 74 65 73 74 69 6e 67 2c 20  cation testing, 
2e4c7 74 6f 20 68 65 6c 70 20 66 69 6e 64 20 63 61 73  to help find cas
2e4c8 65 73 0a 20 20 2a 2a 20 77 68 65 72 65 20 61 70  es.  ** where ap
2e4c9 70 6c 69 63 61 74 69 6f 6e 20 62 65 68 61 76 69  plication behavi
2e4ca 6f 75 72 20 64 65 70 65 6e 64 73 20 6f 6e 20 74  our depends on t
2e4cb 68 65 20 28 75 6e 64 65 66 69 6e 65 64 29 20 6f  he (undefined) o
2e4cc 72 64 65 72 20 74 68 61 74 0a 20 20 2a 2a 20 53  rder that.  ** S
2e4cd 51 4c 69 74 65 20 6f 75 74 70 75 74 73 20 72 6f  QLite outputs ro
2e4ce 77 73 20 69 6e 20 69 6e 20 74 68 65 20 61 62 73  ws in in the abs
2e4cf 65 6e 63 65 20 6f 66 20 61 6e 20 4f 52 44 45 52  ence of an ORDER
2e4d0 20 42 59 20 63 6c 61 75 73 65 2e 20 20 2a 2f 0a   BY clause.  */.
2e4d1 20 20 69 66 28 20 21 70 4f 72 64 65 72 42 79 20    if( !pOrderBy 
2e4d2 26 26 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66  && pParse->db->f
2e4d3 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65  lags & SQLITE_Re
2e4d4 76 65 72 73 65 4f 72 64 65 72 20 29 7b 0a 20 20  verseOrder ){.  
2e4d5 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73    pCost->plan.ws
2e4d6 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 52  Flags |= WHERE_R
2e4d7 45 56 45 52 53 45 3b 0a 20 20 7d 0a 0a 20 20 61  EVERSE;.  }..  a
2e4d8 73 73 65 72 74 28 20 70 4f 72 64 65 72 42 79 20  ssert( pOrderBy 
2e4d9 7c 7c 20 28 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e  || (pCost->plan.
2e4da 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 4f 52  wsFlags&WHERE_OR
2e4db 44 45 52 42 59 29 3d 3d 30 20 29 3b 0a 20 20 61  DERBY)==0 );.  a
2e4dc 73 73 65 72 74 28 20 70 43 6f 73 74 2d 3e 70 6c  ssert( pCost->pl
2e4dd 61 6e 2e 75 2e 70 49 64 78 3d 3d 30 20 7c 7c 20  an.u.pIdx==0 || 
2e4de 28 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46  (pCost->plan.wsF
2e4df 6c 61 67 73 26 57 48 45 52 45 5f 52 4f 57 49 44  lags&WHERE_ROWID
2e4e0 5f 45 51 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73  _EQ)==0 );.  ass
2e4e1 65 72 74 28 20 70 53 72 63 2d 3e 70 49 6e 64 65  ert( pSrc->pInde
2e4e2 78 3d 3d 30 20 0a 20 20 20 20 20 20 20 7c 7c 20  x==0 .       || 
2e4e3 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70 49  pCost->plan.u.pI
2e4e4 64 78 3d 3d 30 20 0a 20 20 20 20 20 20 20 7c 7c  dx==0 .       ||
2e4e5 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70   pCost->plan.u.p
2e4e6 49 64 78 3d 3d 70 53 72 63 2d 3e 70 49 6e 64 65  Idx==pSrc->pInde
2e4e7 78 20 0a 20 20 29 3b 0a 0a 20 20 57 48 45 52 45  x .  );..  WHERE
2e4e8 54 52 41 43 45 28 28 22 62 65 73 74 20 69 6e 64  TRACE(("best ind
2e4e9 65 78 20 69 73 3a 20 25 73 5c 6e 22 2c 20 0a 20  ex is: %s\n", . 
2e4ea 20 20 20 28 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e     (pCost->plan.
2e4eb 75 2e 70 49 64 78 20 3f 20 70 43 6f 73 74 2d 3e  u.pIdx ? pCost->
2e4ec 70 6c 61 6e 2e 75 2e 70 49 64 78 2d 3e 7a 4e 61  plan.u.pIdx->zNa
2e4ed 6d 65 20 3a 20 22 69 70 6b 22 29 0a 20 20 29 29  me : "ipk").  ))
2e4ee 3b 0a 20 20 0a 20 20 62 65 73 74 4f 72 43 6c 61  ;.  .  bestOrCla
2e4ef 75 73 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c  useIndex(pParse,
2e4f0 20 70 57 43 2c 20 70 53 72 63 2c 20 6e 6f 74 52   pWC, pSrc, notR
2e4f1 65 61 64 79 2c 20 70 4f 72 64 65 72 42 79 2c 20  eady, pOrderBy, 
2e4f2 70 43 6f 73 74 29 3b 0a 20 20 70 43 6f 73 74 2d  pCost);.  pCost-
2e4f3 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 7c 3d  >plan.wsFlags |=
2e4f4 20 65 71 54 65 72 6d 4d 61 73 6b 3b 0a 7d 0a 0a   eqTermMask;.}..
2e4f5 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 71  /*.** Find the q
2e4f6 75 65 72 79 20 70 6c 61 6e 20 66 6f 72 20 61 63  uery plan for ac
2e4f7 63 65 73 73 69 6e 67 20 74 61 62 6c 65 20 70 53  cessing table pS
2e4f8 72 63 2d 3e 70 54 61 62 2e 20 57 72 69 74 65 20  rc->pTab. Write 
2e4f9 74 68 65 0a 2a 2a 20 62 65 73 74 20 71 75 65 72  the.** best quer
2e4fa 79 20 70 6c 61 6e 20 61 6e 64 20 69 74 73 20 63  y plan and its c
2e4fb 6f 73 74 20 69 6e 74 6f 20 74 68 65 20 57 68 65  ost into the Whe
2e4fc 72 65 43 6f 73 74 20 6f 62 6a 65 63 74 20 73 75  reCost object su
2e4fd 70 70 6c 69 65 64 20 0a 2a 2a 20 61 73 20 74 68  pplied .** as th
2e4fe 65 20 6c 61 73 74 20 70 61 72 61 6d 65 74 65 72  e last parameter
2e4ff 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
2e500 6d 61 79 20 63 61 6c 63 75 6c 61 74 65 20 74 68  may calculate th
2e501 65 20 63 6f 73 74 20 6f 66 0a 2a 2a 20 62 6f 74  e cost of.** bot
2e502 68 20 72 65 61 6c 20 61 6e 64 20 76 69 72 74 75  h real and virtu
2e503 61 6c 20 74 61 62 6c 65 20 73 63 61 6e 73 2e 0a  al table scans..
2e504 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62  */.static void b
2e505 65 73 74 49 6e 64 65 78 28 0a 20 20 50 61 72 73  estIndex(.  Pars
2e506 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2e507 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
2e508 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2e509 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20  /.  WhereClause 
2e50a 2a 70 57 43 2c 20 20 20 20 20 20 20 20 20 20 20  *pWC,           
2e50b 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
2e50c 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  use */.  struct 
2e50d 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53  SrcList_item *pS
2e50e 72 63 2c 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d  rc,  /* The FROM
2e50f 20 63 6c 61 75 73 65 20 74 65 72 6d 20 74 6f 20   clause term to 
2e510 73 65 61 72 63 68 20 2a 2f 0a 20 20 42 69 74 6d  search */.  Bitm
2e511 61 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20 20  ask notReady,   
2e512 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20          /* Mask 
2e513 6f 66 20 63 75 72 73 6f 72 73 20 74 68 61 74 20  of cursors that 
2e514 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c  are not availabl
2e515 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  e */.  ExprList 
2e516 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 20  *pOrderBy,      
2e517 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20     /* The ORDER 
2e518 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57  BY clause */.  W
2e519 68 65 72 65 43 6f 73 74 20 2a 70 43 6f 73 74 20  hereCost *pCost 
2e51a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
2e51b 77 65 73 74 20 63 6f 73 74 20 71 75 65 72 79 20  west cost query 
2e51c 70 6c 61 6e 20 2a 2f 0a 29 7b 0a 23 69 66 6e 64  plan */.){.#ifnd
2e51d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
2e51e 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66  IRTUALTABLE.  if
2e51f 28 20 49 73 56 69 72 74 75 61 6c 28 70 53 72 63  ( IsVirtual(pSrc
2e520 2d 3e 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73  ->pTab) ){.    s
2e521 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
2e522 6f 20 2a 70 20 3d 20 30 3b 0a 20 20 20 20 62 65  o *p = 0;.    be
2e523 73 74 56 69 72 74 75 61 6c 49 6e 64 65 78 28 70  stVirtualIndex(p
2e524 50 61 72 73 65 2c 20 70 57 43 2c 20 70 53 72 63  Parse, pWC, pSrc
2e525 2c 20 6e 6f 74 52 65 61 64 79 2c 20 70 4f 72 64  , notReady, pOrd
2e526 65 72 42 79 2c 20 70 43 6f 73 74 2c 20 26 70 29  erBy, pCost, &p)
2e527 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 65 65  ;.    if( p->nee
2e528 64 54 6f 46 72 65 65 49 64 78 53 74 72 20 29 7b  dToFreeIdxStr ){
2e529 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
2e52a 72 65 65 28 70 2d 3e 69 64 78 53 74 72 29 3b 0a  ree(p->idxStr);.
2e52b 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2e52c 33 44 62 46 72 65 65 28 70 50 61 72 73 65 2d 3e  3DbFree(pParse->
2e52d 64 62 2c 20 70 29 3b 0a 20 20 7d 65 6c 73 65 0a  db, p);.  }else.
2e52e 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20 62  #endif.  {.    b
2e52f 65 73 74 42 74 72 65 65 49 6e 64 65 78 28 70 50  estBtreeIndex(pP
2e530 61 72 73 65 2c 20 70 57 43 2c 20 70 53 72 63 2c  arse, pWC, pSrc,
2e531 20 6e 6f 74 52 65 61 64 79 2c 20 70 4f 72 64 65   notReady, pOrde
2e532 72 42 79 2c 20 70 43 6f 73 74 29 3b 0a 20 20 7d  rBy, pCost);.  }
2e533 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 69 73 61 62 6c  .}../*.** Disabl
2e534 65 20 61 20 74 65 72 6d 20 69 6e 20 74 68 65 20  e a term in the 
2e535 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 45  WHERE clause.  E
2e536 78 63 65 70 74 2c 20 64 6f 20 6e 6f 74 20 64 69  xcept, do not di
2e537 73 61 62 6c 65 20 74 68 65 20 74 65 72 6d 0a 2a  sable the term.*
2e538 2a 20 69 66 20 69 74 20 63 6f 6e 74 72 6f 6c 73  * if it controls
2e539 20 61 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f   a LEFT OUTER JO
2e53a 49 4e 20 61 6e 64 20 69 74 20 64 69 64 20 6e 6f  IN and it did no
2e53b 74 20 6f 72 69 67 69 6e 61 74 65 20 69 6e 20 74  t originate in t
2e53c 68 65 20 4f 4e 0a 2a 2a 20 6f 72 20 55 53 49 4e  he ON.** or USIN
2e53d 47 20 63 6c 61 75 73 65 20 6f 66 20 74 68 61 74  G clause of that
2e53e 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e   join..**.** Con
2e53f 73 69 64 65 72 20 74 68 65 20 74 65 72 6d 20 74  sider the term t
2e540 32 2e 7a 3d 27 6f 6b 27 20 69 6e 20 74 68 65 20  2.z='ok' in the 
2e541 66 6f 6c 6c 6f 77 69 6e 67 20 71 75 65 72 69 65  following querie
2e542 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 20  s:.**.**   (1)  
2e543 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31  SELECT * FROM t1
2e544 20 4c 45 46 54 20 4a 4f 49 4e 20 74 32 20 4f 4e   LEFT JOIN t2 ON
2e545 20 74 31 2e 61 3d 74 32 2e 78 20 57 48 45 52 45   t1.a=t2.x WHERE
2e546 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 20 20 20   t2.z='ok'.**   
2e547 28 32 29 20 20 53 45 4c 45 43 54 20 2a 20 46 52  (2)  SELECT * FR
2e548 4f 4d 20 74 31 20 4c 45 46 54 20 4a 4f 49 4e 20  OM t1 LEFT JOIN 
2e549 74 32 20 4f 4e 20 74 31 2e 61 3d 74 32 2e 78 20  t2 ON t1.a=t2.x 
2e54a 41 4e 44 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a  AND t2.z='ok'.**
2e54b 20 20 20 28 33 29 20 20 53 45 4c 45 43 54 20 2a     (3)  SELECT *
2e54c 20 46 52 4f 4d 20 74 31 2c 20 74 32 20 57 48 45   FROM t1, t2 WHE
2e54d 52 45 20 74 31 2e 61 3d 74 32 2e 78 20 41 4e 44  RE t1.a=t2.x AND
2e54e 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 0a 2a 2a   t2.z='ok'.**.**
2e54f 20 54 68 65 20 74 32 2e 7a 3d 27 6f 6b 27 20 69   The t2.z='ok' i
2e550 73 20 64 69 73 61 62 6c 65 64 20 69 6e 20 74 68  s disabled in th
2e551 65 20 69 6e 20 28 32 29 20 62 65 63 61 75 73 65  e in (2) because
2e552 20 69 74 20 6f 72 69 67 69 6e 61 74 65 73 0a 2a   it originates.*
2e553 2a 20 69 6e 20 74 68 65 20 4f 4e 20 63 6c 61 75  * in the ON clau
2e554 73 65 2e 20 20 54 68 65 20 74 65 72 6d 20 69 73  se.  The term is
2e555 20 64 69 73 61 62 6c 65 64 20 69 6e 20 28 33 29   disabled in (3)
2e556 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20 6e   because it is n
2e557 6f 74 20 70 61 72 74 0a 2a 2a 20 6f 66 20 61 20  ot part.** of a 
2e558 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 2e  LEFT OUTER JOIN.
2e559 20 20 49 6e 20 28 31 29 2c 20 74 68 65 20 74 65    In (1), the te
2e55a 72 6d 20 69 73 20 6e 6f 74 20 64 69 73 61 62 6c  rm is not disabl
2e55b 65 64 2e 0a 2a 2a 0a 2a 2a 20 44 69 73 61 62 6c  ed..**.** Disabl
2e55c 69 6e 67 20 61 20 74 65 72 6d 20 63 61 75 73 65  ing a term cause
2e55d 73 20 74 68 61 74 20 74 65 72 6d 20 74 6f 20 6e  s that term to n
2e55e 6f 74 20 62 65 20 74 65 73 74 65 64 20 69 6e 20  ot be tested in 
2e55f 74 68 65 20 69 6e 6e 65 72 20 6c 6f 6f 70 0a 2a  the inner loop.*
2e560 2a 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 20 20  * of the join.  
2e561 44 69 73 61 62 6c 69 6e 67 20 69 73 20 61 6e 20  Disabling is an 
2e562 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 57  optimization.  W
2e563 68 65 6e 20 74 65 72 6d 73 20 61 72 65 20 73 61  hen terms are sa
2e564 74 69 73 66 69 65 64 0a 2a 2a 20 62 79 20 69 6e  tisfied.** by in
2e565 64 69 63 65 73 2c 20 77 65 20 64 69 73 61 62 6c  dices, we disabl
2e566 65 20 74 68 65 6d 20 74 6f 20 70 72 65 76 65 6e  e them to preven
2e567 74 20 72 65 64 75 6e 64 61 6e 74 20 74 65 73 74  t redundant test
2e568 73 20 69 6e 20 74 68 65 20 69 6e 6e 65 72 0a 2a  s in the inner.*
2e569 2a 20 6c 6f 6f 70 2e 20 20 57 65 20 77 6f 75 6c  * loop.  We woul
2e56a 64 20 67 65 74 20 74 68 65 20 63 6f 72 72 65 63  d get the correc
2e56b 74 20 72 65 73 75 6c 74 73 20 69 66 20 6e 6f 74  t results if not
2e56c 68 69 6e 67 20 77 65 72 65 20 65 76 65 72 20 64  hing were ever d
2e56d 69 73 61 62 6c 65 64 2c 0a 2a 2a 20 62 75 74 20  isabled,.** but 
2e56e 6a 6f 69 6e 73 20 6d 69 67 68 74 20 72 75 6e 20  joins might run 
2e56f 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65 72 2e  a little slower.
2e570 20 20 54 68 65 20 74 72 69 63 6b 20 69 73 20 74    The trick is t
2e571 6f 20 64 69 73 61 62 6c 65 20 61 73 20 6d 75 63  o disable as muc
2e572 68 0a 2a 2a 20 61 73 20 77 65 20 63 61 6e 20 77  h.** as we can w
2e573 69 74 68 6f 75 74 20 64 69 73 61 62 6c 69 6e 67  ithout disabling
2e574 20 74 6f 6f 20 6d 75 63 68 2e 20 20 49 66 20 77   too much.  If w
2e575 65 20 64 69 73 61 62 6c 65 64 20 69 6e 20 28 31  e disabled in (1
2e576 29 2c 20 77 65 27 64 20 67 65 74 0a 2a 2a 20 74  ), we'd get.** t
2e577 68 65 20 77 72 6f 6e 67 20 61 6e 73 77 65 72 2e  he wrong answer.
2e578 20 20 53 65 65 20 74 69 63 6b 65 74 20 23 38 31    See ticket #81
2e579 33 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  3..*/.static voi
2e57a 64 20 64 69 73 61 62 6c 65 54 65 72 6d 28 57 68  d disableTerm(Wh
2e57b 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c  ereLevel *pLevel
2e57c 2c 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65  , WhereTerm *pTe
2e57d 72 6d 29 7b 0a 20 20 69 66 28 20 70 54 65 72 6d  rm){.  if( pTerm
2e57e 0a 20 20 20 20 20 20 26 26 20 41 4c 57 41 59 53  .      && ALWAYS
2e57f 28 28 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73  ((pTerm->wtFlags
2e580 20 26 20 54 45 52 4d 5f 43 4f 44 45 44 29 3d 3d   & TERM_CODED)==
2e581 30 29 0a 20 20 20 20 20 20 26 26 20 28 70 4c 65  0).      && (pLe
2e582 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 3d 3d  vel->iLeftJoin==
2e583 30 20 7c 7c 20 45 78 70 72 48 61 73 50 72 6f 70  0 || ExprHasProp
2e584 65 72 74 79 28 70 54 65 72 6d 2d 3e 70 45 78 70  erty(pTerm->pExp
2e585 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 29  r, EP_FromJoin))
2e586 0a 20 20 29 7b 0a 20 20 20 20 70 54 65 72 6d 2d  .  ){.    pTerm-
2e587 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d  >wtFlags |= TERM
2e588 5f 43 4f 44 45 44 3b 0a 20 20 20 20 69 66 28 20  _CODED;.    if( 
2e589 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 3e 3d  pTerm->iParent>=
2e58a 30 20 29 7b 0a 20 20 20 20 20 20 57 68 65 72 65  0 ){.      Where
2e58b 54 65 72 6d 20 2a 70 4f 74 68 65 72 20 3d 20 26  Term *pOther = &
2e58c 70 54 65 72 6d 2d 3e 70 57 43 2d 3e 61 5b 70 54  pTerm->pWC->a[pT
2e58d 65 72 6d 2d 3e 69 50 61 72 65 6e 74 5d 3b 0a 20  erm->iParent];. 
2e58e 20 20 20 20 20 69 66 28 20 28 2d 2d 70 4f 74 68       if( (--pOth
2e58f 65 72 2d 3e 6e 43 68 69 6c 64 29 3d 3d 30 20 29  er->nChild)==0 )
2e590 7b 0a 20 20 20 20 20 20 20 20 64 69 73 61 62 6c  {.        disabl
2e591 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 4f  eTerm(pLevel, pO
2e592 74 68 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ther);.      }. 
2e593 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
2e594 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f 41 66 66  * Code an OP_Aff
2e595 69 6e 69 74 79 20 6f 70 63 6f 64 65 20 74 6f 20  inity opcode to 
2e596 61 70 70 6c 79 20 74 68 65 20 63 6f 6c 75 6d 6e  apply the column
2e597 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67   affinity string
2e598 20 7a 41 66 66 0a 2a 2a 20 74 6f 20 74 68 65 20   zAff.** to the 
2e599 6e 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72  n registers star
2e59a 74 69 6e 67 20 61 74 20 62 61 73 65 2e 20 0a 2a  ting at base. .*
2e59b 2a 0a 2a 2a 20 42 75 66 66 65 72 20 7a 41 66 66  *.** Buffer zAff
2e59c 20 77 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 75   was allocated u
2e59d 73 69 6e 67 20 73 71 6c 69 74 65 33 44 62 4d 61  sing sqlite3DbMa
2e59e 6c 6c 6f 63 28 29 2e 20 49 74 20 69 73 20 74 68  lloc(). It is th
2e59f 65 20 0a 2a 2a 20 72 65 73 70 6f 6e 73 69 62 69  e .** responsibi
2e5a0 6c 69 74 79 20 6f 66 20 74 68 69 73 20 66 75 6e  lity of this fun
2e5a1 63 74 69 6f 6e 20 74 6f 20 61 72 72 61 6e 67 65  ction to arrange
2e5a2 20 66 6f 72 20 69 74 20 74 6f 20 62 65 20 65 76   for it to be ev
2e5a3 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20 66 72 65 65  entually.** free
2e5a4 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 44  d using sqlite3D
2e5a5 62 46 72 65 65 28 29 2e 0a 2a 2f 0a 73 74 61 74  bFree()..*/.stat
2e5a6 69 63 20 76 6f 69 64 20 63 6f 64 65 41 70 70 6c  ic void codeAppl
2e5a7 79 41 66 66 69 6e 69 74 79 28 50 61 72 73 65 20  yAffinity(Parse 
2e5a8 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 62 61 73  *pParse, int bas
2e5a9 65 2c 20 69 6e 74 20 6e 2c 20 63 68 61 72 20 2a  e, int n, char *
2e5aa 7a 41 66 66 29 7b 0a 20 20 56 64 62 65 20 2a 76  zAff){.  Vdbe *v
2e5ab 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
2e5ac 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30  ;.  assert( v!=0
2e5ad 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62   );.  sqlite3Vdb
2e5ae 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 66  eAddOp2(v, OP_Af
2e5af 66 69 6e 69 74 79 2c 20 62 61 73 65 2c 20 6e 29  finity, base, n)
2e5b0 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43  ;.  sqlite3VdbeC
2e5b1 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 7a  hangeP4(v, -1, z
2e5b2 41 66 66 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29  Aff, P4_DYNAMIC)
2e5b3 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43  ;.  sqlite3ExprC
2e5b4 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e  acheAffinityChan
2e5b5 67 65 28 70 50 61 72 73 65 2c 20 62 61 73 65 2c  ge(pParse, base,
2e5b6 20 6e 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47   n);.}.../*.** G
2e5b7 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
2e5b8 20 61 20 73 69 6e 67 6c 65 20 65 71 75 61 6c 69   a single equali
2e5b9 74 79 20 74 65 72 6d 20 6f 66 20 74 68 65 20 57  ty term of the W
2e5ba 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 41 6e  HERE clause.  An
2e5bb 20 65 71 75 61 6c 69 74 79 0a 2a 2a 20 74 65 72   equality.** ter
2e5bc 6d 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20  m can be either 
2e5bd 58 3d 65 78 70 72 20 6f 72 20 58 20 49 4e 20 28  X=expr or X IN (
2e5be 2e 2e 2e 29 2e 20 20 20 70 54 65 72 6d 20 69 73  ...).   pTerm is
2e5bf 20 74 68 65 20 74 65 72 6d 20 74 6f 20 62 65 20   the term to be 
2e5c0 0a 2a 2a 20 63 6f 64 65 64 2e 0a 2a 2a 0a 2a 2a  .** coded..**.**
2e5c1 20 54 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c   The current val
2e5c2 75 65 20 66 6f 72 20 74 68 65 20 63 6f 6e 73 74  ue for the const
2e5c3 72 61 69 6e 74 20 69 73 20 6c 65 66 74 20 69 6e  raint is left in
2e5c4 20 72 65 67 69 73 74 65 72 20 69 52 65 67 2e 0a   register iReg..
2e5c5 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 63 6f 6e 73  **.** For a cons
2e5c6 74 72 61 69 6e 74 20 6f 66 20 74 68 65 20 66 6f  traint of the fo
2e5c7 72 6d 20 58 3d 65 78 70 72 2c 20 74 68 65 20 65  rm X=expr, the e
2e5c8 78 70 72 65 73 73 69 6f 6e 20 69 73 20 65 76 61  xpression is eva
2e5c9 6c 75 61 74 65 64 20 61 6e 64 20 69 74 73 0a 2a  luated and its.*
2e5ca 2a 20 72 65 73 75 6c 74 20 69 73 20 6c 65 66 74  * result is left
2e5cb 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20   on the stack.  
2e5cc 46 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  For constraints 
2e5cd 6f 66 20 74 68 65 20 66 6f 72 6d 20 58 20 49 4e  of the form X IN
2e5ce 20 28 2e 2e 2e 29 0a 2a 2a 20 74 68 69 73 20 72   (...).** this r
2e5cf 6f 75 74 69 6e 65 20 73 65 74 73 20 75 70 20 61  outine sets up a
2e5d0 20 6c 6f 6f 70 20 74 68 61 74 20 77 69 6c 6c 20   loop that will 
2e5d1 69 74 65 72 61 74 65 20 6f 76 65 72 20 61 6c 6c  iterate over all
2e5d2 20 76 61 6c 75 65 73 20 6f 66 20 58 2e 0a 2a 2f   values of X..*/
2e5d3 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65  .static int code
2e5d4 45 71 75 61 6c 69 74 79 54 65 72 6d 28 0a 20 20  EqualityTerm(.  
2e5d5 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2e5d6 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69      /* The parsi
2e5d7 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
2e5d8 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d  WhereTerm *pTerm
2e5d9 2c 20 20 20 2f 2a 20 54 68 65 20 74 65 72 6d 20  ,   /* The term 
2e5da 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  of the WHERE cla
2e5db 75 73 65 20 74 6f 20 62 65 20 63 6f 64 65 64 20  use to be coded 
2e5dc 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76 65 6c 20  */.  WhereLevel 
2e5dd 2a 70 4c 65 76 65 6c 2c 20 2f 2a 20 57 68 65 6e  *pLevel, /* When
2e5de 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 46 52   level of the FR
2e5df 4f 4d 20 63 6c 61 75 73 65 20 77 65 20 61 72 65  OM clause we are
2e5e0 20 77 6f 72 6b 69 6e 67 20 6f 6e 20 2a 2f 0a 20   working on */. 
2e5e1 20 69 6e 74 20 69 54 61 72 67 65 74 20 20 20 20   int iTarget    
2e5e2 20 20 20 20 20 2f 2a 20 41 74 74 65 6d 70 74 20       /* Attempt 
2e5e3 74 6f 20 6c 65 61 76 65 20 72 65 73 75 6c 74 73  to leave results
2e5e4 20 69 6e 20 74 68 69 73 20 72 65 67 69 73 74 65   in this registe
2e5e5 72 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a  r */.){.  Expr *
2e5e6 70 58 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70  pX = pTerm->pExp
2e5e7 72 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  r;.  Vdbe *v = p
2e5e8 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
2e5e9 69 6e 74 20 69 52 65 67 3b 20 20 20 20 20 20 20  int iReg;       
2e5ea 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
2e5eb 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 72  gister holding r
2e5ec 65 73 75 6c 74 73 20 2a 2f 0a 0a 20 20 61 73 73  esults */..  ass
2e5ed 65 72 74 28 20 69 54 61 72 67 65 74 3e 30 20 29  ert( iTarget>0 )
2e5ee 3b 0a 20 20 69 66 28 20 70 58 2d 3e 6f 70 3d 3d  ;.  if( pX->op==
2e5ef 54 4b 5f 45 51 20 29 7b 0a 20 20 20 20 69 52 65  TK_EQ ){.    iRe
2e5f0 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  g = sqlite3ExprC
2e5f1 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65  odeTarget(pParse
2e5f2 2c 20 70 58 2d 3e 70 52 69 67 68 74 2c 20 69 54  , pX->pRight, iT
2e5f3 61 72 67 65 74 29 3b 0a 20 20 7d 65 6c 73 65 20  arget);.  }else 
2e5f4 69 66 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 49  if( pX->op==TK_I
2e5f5 53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 69 52 65  SNULL ){.    iRe
2e5f6 67 20 3d 20 69 54 61 72 67 65 74 3b 0a 20 20 20  g = iTarget;.   
2e5f7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2e5f8 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30  p2(v, OP_Null, 0
2e5f9 2c 20 69 52 65 67 29 3b 0a 23 69 66 6e 64 65 66  , iReg);.#ifndef
2e5fa 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42   SQLITE_OMIT_SUB
2e5fb 51 55 45 52 59 0a 20 20 7d 65 6c 73 65 7b 0a 20  QUERY.  }else{. 
2e5fc 20 20 20 69 6e 74 20 65 54 79 70 65 3b 0a 20 20     int eType;.  
2e5fd 20 20 69 6e 74 20 69 54 61 62 3b 0a 20 20 20 20    int iTab;.    
2e5fe 73 74 72 75 63 74 20 49 6e 4c 6f 6f 70 20 2a 70  struct InLoop *p
2e5ff 49 6e 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28  In;..    assert(
2e600 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 29   pX->op==TK_IN )
2e601 3b 0a 20 20 20 20 69 52 65 67 20 3d 20 69 54 61  ;.    iReg = iTa
2e602 72 67 65 74 3b 0a 20 20 20 20 65 54 79 70 65 20  rget;.    eType 
2e603 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49  = sqlite3FindInI
2e604 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 58 2c  ndex(pParse, pX,
2e605 20 30 29 3b 0a 20 20 20 20 69 54 61 62 20 3d 20   0);.    iTab = 
2e606 70 58 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20  pX->iTable;.    
2e607 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2e608 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20  2(v, OP_Rewind, 
2e609 69 54 61 62 2c 20 30 29 3b 0a 20 20 20 20 61 73  iTab, 0);.    as
2e60a 73 65 72 74 28 20 70 4c 65 76 65 6c 2d 3e 70 6c  sert( pLevel->pl
2e60b 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
2e60c 52 45 5f 49 4e 5f 41 42 4c 45 20 29 3b 0a 20 20  RE_IN_ABLE );.  
2e60d 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 75 2e    if( pLevel->u.
2e60e 69 6e 2e 6e 49 6e 3d 3d 30 20 29 7b 0a 20 20 20  in.nIn==0 ){.   
2e60f 20 20 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 4e     pLevel->addrN
2e610 78 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  xt = sqlite3Vdbe
2e611 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20  MakeLabel(v);.  
2e612 20 20 7d 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e    }.    pLevel->
2e613 75 2e 69 6e 2e 6e 49 6e 2b 2b 3b 0a 20 20 20 20  u.in.nIn++;.    
2e614 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e  pLevel->u.in.aIn
2e615 4c 6f 6f 70 20 3d 0a 20 20 20 20 20 20 20 73 71  Loop =.       sq
2e616 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72  lite3DbReallocOr
2e617 46 72 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  Free(pParse->db,
2e618 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49   pLevel->u.in.aI
2e619 6e 4c 6f 6f 70 2c 0a 20 20 20 20 20 20 20 20 20  nLoop,.         
2e61a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e61b 20 20 20 20 20 73 69 7a 65 6f 66 28 70 4c 65 76       sizeof(pLev
2e61c 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f 6f 70  el->u.in.aInLoop
2e61d 5b 30 5d 29 2a 70 4c 65 76 65 6c 2d 3e 75 2e 69  [0])*pLevel->u.i
2e61e 6e 2e 6e 49 6e 29 3b 0a 20 20 20 20 70 49 6e 20  n.nIn);.    pIn 
2e61f 3d 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61  = pLevel->u.in.a
2e620 49 6e 4c 6f 6f 70 3b 0a 20 20 20 20 69 66 28 20  InLoop;.    if( 
2e621 70 49 6e 20 29 7b 0a 20 20 20 20 20 20 70 49 6e  pIn ){.      pIn
2e622 20 2b 3d 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e   += pLevel->u.in
2e623 2e 6e 49 6e 20 2d 20 31 3b 0a 20 20 20 20 20 20  .nIn - 1;.      
2e624 70 49 6e 2d 3e 69 43 75 72 20 3d 20 69 54 61 62  pIn->iCur = iTab
2e625 3b 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 70  ;.      if( eTyp
2e626 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49  e==IN_INDEX_ROWI
2e627 44 20 29 7b 0a 20 20 20 20 20 20 20 20 70 49 6e  D ){.        pIn
2e628 2d 3e 61 64 64 72 49 6e 54 6f 70 20 3d 20 73 71  ->addrInTop = sq
2e629 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2e62a 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 54 61  v, OP_Rowid, iTa
2e62b 62 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 20 20  b, iReg);.      
2e62c 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
2e62d 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 20 3d 20  In->addrInTop = 
2e62e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2e62f 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
2e630 69 54 61 62 2c 20 30 2c 20 69 52 65 67 29 3b 0a  iTab, 0, iReg);.
2e631 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
2e632 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2e633 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 69 52  v, OP_IsNull, iR
2e634 65 67 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  eg);.    }else{.
2e635 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 75 2e        pLevel->u.
2e636 69 6e 2e 6e 49 6e 20 3d 20 30 3b 0a 20 20 20 20  in.nIn = 0;.    
2e637 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 64  }.#endif.  }.  d
2e638 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65  isableTerm(pLeve
2e639 6c 2c 20 70 54 65 72 6d 29 3b 0a 20 20 72 65 74  l, pTerm);.  ret
2e63a 75 72 6e 20 69 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a  urn iReg;.}../*.
2e63b 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
2e63c 20 74 68 61 74 20 77 69 6c 6c 20 65 76 61 6c 75   that will evalu
2e63d 61 74 65 20 61 6c 6c 20 3d 3d 20 61 6e 64 20 49  ate all == and I
2e63e 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 66 6f  N constraints fo
2e63f 72 20 61 6e 0a 2a 2a 20 69 6e 64 65 78 2e 20 20  r an.** index.  
2e640 54 68 65 20 76 61 6c 75 65 73 20 66 6f 72 20 61  The values for a
2e641 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61  ll constraints a
2e642 72 65 20 6c 65 66 74 20 6f 6e 20 74 68 65 20 73  re left on the s
2e643 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  tack..**.** For 
2e644 65 78 61 6d 70 6c 65 2c 20 63 6f 6e 73 69 64 65  example, conside
2e645 72 20 74 61 62 6c 65 20 74 31 28 61 2c 62 2c 63  r table t1(a,b,c
2e646 2c 64 2c 65 2c 66 29 20 77 69 74 68 20 69 6e 64  ,d,e,f) with ind
2e647 65 78 20 69 31 28 61 2c 62 2c 63 29 2e 0a 2a 2a  ex i1(a,b,c)..**
2e648 20 53 75 70 70 6f 73 65 20 74 68 65 20 57 48 45   Suppose the WHE
2e649 52 45 20 63 6c 61 75 73 65 20 69 73 20 74 68 69  RE clause is thi
2e64a 73 3a 20 20 61 3d 3d 35 20 41 4e 44 20 62 20 49  s:  a==5 AND b I
2e64b 4e 20 28 31 2c 32 2c 33 29 20 41 4e 44 20 63 3e  N (1,2,3) AND c>
2e64c 35 20 41 4e 44 20 63 3c 31 30 0a 2a 2a 20 54 68  5 AND c<10.** Th
2e64d 65 20 69 6e 64 65 78 20 68 61 73 20 61 73 20 6d  e index has as m
2e64e 61 6e 79 20 61 73 20 74 68 72 65 65 20 65 71 75  any as three equ
2e64f 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
2e650 73 2c 20 62 75 74 20 69 6e 20 74 68 69 73 0a 2a  s, but in this.*
2e651 2a 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 74  * example, the t
2e652 68 69 72 64 20 22 63 22 20 76 61 6c 75 65 20 69  hird "c" value i
2e653 73 20 61 6e 20 69 6e 65 71 75 61 6c 69 74 79 2e  s an inequality.
2e654 20 20 53 6f 20 6f 6e 6c 79 20 74 77 6f 20 0a 2a    So only two .*
2e655 2a 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72  * constraints ar
2e656 65 20 63 6f 64 65 64 2e 20 20 54 68 69 73 20 72  e coded.  This r
2e657 6f 75 74 69 6e 65 20 77 69 6c 6c 20 67 65 6e 65  outine will gene
2e658 72 61 74 65 20 63 6f 64 65 20 74 6f 20 65 76 61  rate code to eva
2e659 6c 75 61 74 65 0a 2a 2a 20 61 3d 3d 35 20 61 6e  luate.** a==5 an
2e65a 64 20 62 20 49 4e 20 28 31 2c 32 2c 33 29 2e 20  d b IN (1,2,3). 
2e65b 20 54 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c   The current val
2e65c 75 65 73 20 66 6f 72 20 61 20 61 6e 64 20 62 20  ues for a and b 
2e65d 77 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 0a 2a  will be stored.*
2e65e 2a 20 69 6e 20 63 6f 6e 73 65 63 75 74 69 76 65  * in consecutive
2e65f 20 72 65 67 69 73 74 65 72 73 20 61 6e 64 20 74   registers and t
2e660 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
2e661 66 69 72 73 74 20 72 65 67 69 73 74 65 72 20 69  first register i
2e662 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
2e663 2a 20 49 6e 20 74 68 65 20 65 78 61 6d 70 6c 65  * In the example
2e664 20 61 62 6f 76 65 20 6e 45 71 3d 3d 32 2e 20 20   above nEq==2.  
2e665 42 75 74 20 74 68 69 73 20 73 75 62 72 6f 75 74  But this subrout
2e666 69 6e 65 20 77 6f 72 6b 73 20 66 6f 72 20 61 6e  ine works for an
2e667 79 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 6e 45  y value.** of nE
2e668 71 20 69 6e 63 6c 75 64 69 6e 67 20 30 2e 20 20  q including 0.  
2e669 49 66 20 6e 45 71 3d 3d 30 2c 20 74 68 69 73 20  If nEq==0, this 
2e66a 72 6f 75 74 69 6e 65 20 69 73 20 6e 65 61 72 6c  routine is nearl
2e66b 79 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 20 54 68  y a no-op..** Th
2e66c 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 69 74 20  e only thing it 
2e66d 64 6f 65 73 20 69 73 20 61 6c 6c 6f 63 61 74 65  does is allocate
2e66e 20 74 68 65 20 70 4c 65 76 65 6c 2d 3e 69 4d 65   the pLevel->iMe
2e66f 6d 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 0a 2a  m memory cell..*
2e670 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2e671 65 20 61 6c 77 61 79 73 20 61 6c 6c 6f 63 61 74  e always allocat
2e672 65 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20  es at least one 
2e673 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64 20  memory cell and 
2e674 72 65 74 75 72 6e 73 0a 2a 2a 20 74 68 65 20 69  returns.** the i
2e675 6e 64 65 78 20 6f 66 20 74 68 61 74 20 6d 65 6d  ndex of that mem
2e676 6f 72 79 20 63 65 6c 6c 2e 20 54 68 65 20 63 6f  ory cell. The co
2e677 64 65 20 74 68 61 74 0a 2a 2a 20 63 61 6c 6c 73  de that.** calls
2e678 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   this routine wi
2e679 6c 6c 20 75 73 65 20 74 68 61 74 20 6d 65 6d 6f  ll use that memo
2e67a 72 79 20 63 65 6c 6c 20 74 6f 20 73 74 6f 72 65  ry cell to store
2e67b 20 74 68 65 20 74 65 72 6d 69 6e 61 74 69 6f 6e   the termination
2e67c 0a 2a 2a 20 6b 65 79 20 76 61 6c 75 65 20 6f 66  .** key value of
2e67d 20 74 68 65 20 6c 6f 6f 70 2e 20 20 49 66 20 6f   the loop.  If o
2e67e 6e 65 20 6f 72 20 6d 6f 72 65 20 49 4e 20 6f 70  ne or more IN op
2e67f 65 72 61 74 6f 72 73 20 61 70 70 65 61 72 2c 20  erators appear, 
2e680 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75  then.** this rou
2e681 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61  tine allocates a
2e682 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 6e 45 71  n additional nEq
2e683 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66 6f   memory cells fo
2e684 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73  r internal.** us
2e685 65 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f 72 65 20  e..**.** Before 
2e686 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 7a 41 66  returning, *pzAf
2e687 66 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e  f is set to poin
2e688 74 20 74 6f 20 61 20 62 75 66 66 65 72 20 63 6f  t to a buffer co
2e689 6e 74 61 69 6e 69 6e 67 20 61 0a 2a 2a 20 63 6f  ntaining a.** co
2e68a 70 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  py of the column
2e68b 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67   affinity string
2e68c 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 61 6c   of the index al
2e68d 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a  located using.**
2e68e 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
2e68f 28 29 2e 20 45 78 63 65 70 74 2c 20 65 6e 74 72  (). Except, entr
2e690 69 65 73 20 69 6e 20 74 68 65 20 63 6f 70 79 20  ies in the copy 
2e691 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 61 73  of the string as
2e692 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68  sociated.** with
2e693 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72   equality constr
2e694 61 69 6e 74 73 20 74 68 61 74 20 75 73 65 20 4e  aints that use N
2e695 4f 4e 45 20 61 66 66 69 6e 69 74 79 20 61 72 65  ONE affinity are
2e696 20 73 65 74 20 74 6f 0a 2a 2a 20 53 51 4c 49 54   set to.** SQLIT
2e697 45 5f 41 46 46 5f 4e 4f 4e 45 2e 20 54 68 69 73  E_AFF_NONE. This
2e698 20 69 73 20 74 6f 20 64 65 61 6c 20 77 69 74 68   is to deal with
2e699 20 53 51 4c 20 73 75 63 68 20 61 73 20 74 68 65   SQL such as the
2e69a 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a   following:.**.*
2e69b 2a 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45  *   CREATE TABLE
2e69c 20 74 31 28 61 20 54 45 58 54 20 50 52 49 4d 41   t1(a TEXT PRIMA
2e69d 52 59 20 4b 45 59 2c 20 62 29 3b 0a 2a 2a 20 20  RY KEY, b);.**  
2e69e 20 53 45 4c 45 43 54 20 2e 2e 2e 20 46 52 4f 4d   SELECT ... FROM
2e69f 20 74 31 20 41 53 20 74 32 2c 20 74 31 20 57 48   t1 AS t2, t1 WH
2e6a0 45 52 45 20 74 31 2e 61 20 3d 20 74 32 2e 62 3b  ERE t1.a = t2.b;
2e6a1 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 65 78  .**.** In the ex
2e6a2 61 6d 70 6c 65 20 61 62 6f 76 65 2c 20 74 68 65  ample above, the
2e6a3 20 69 6e 64 65 78 20 6f 6e 20 74 31 28 61 29 20   index on t1(a) 
2e6a4 68 61 73 20 54 45 58 54 20 61 66 66 69 6e 69 74  has TEXT affinit
2e6a5 79 2e 20 42 75 74 20 73 69 6e 63 65 0a 2a 2a 20  y. But since.** 
2e6a6 74 68 65 20 72 69 67 68 74 20 68 61 6e 64 20 73  the right hand s
2e6a7 69 64 65 20 6f 66 20 74 68 65 20 65 71 75 61 6c  ide of the equal
2e6a8 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 28  ity constraint (
2e6a9 74 32 2e 62 29 20 68 61 73 20 4e 4f 4e 45 20 61  t2.b) has NONE a
2e6aa 66 66 69 6e 69 74 79 2c 0a 2a 2a 20 6e 6f 20 63  ffinity,.** no c
2e6ab 6f 6e 76 65 72 73 69 6f 6e 20 73 68 6f 75 6c 64  onversion should
2e6ac 20 62 65 20 61 74 74 65 6d 70 74 65 64 20 62 65   be attempted be
2e6ad 66 6f 72 65 20 75 73 69 6e 67 20 61 20 74 32 2e  fore using a t2.
2e6ae 62 20 76 61 6c 75 65 20 61 73 20 70 61 72 74 20  b value as part 
2e6af 6f 66 0a 2a 2a 20 61 20 6b 65 79 20 74 6f 20 73  of.** a key to s
2e6b0 65 61 72 63 68 20 74 68 65 20 69 6e 64 65 78 2e  earch the index.
2e6b1 20 48 65 6e 63 65 20 74 68 65 20 66 69 72 73 74   Hence the first
2e6b2 20 62 79 74 65 20 69 6e 20 74 68 65 20 72 65 74   byte in the ret
2e6b3 75 72 6e 65 64 20 61 66 66 69 6e 69 74 79 0a 2a  urned affinity.*
2e6b4 2a 20 73 74 72 69 6e 67 20 69 6e 20 74 68 69 73  * string in this
2e6b5 20 65 78 61 6d 70 6c 65 20 77 6f 75 6c 64 20 62   example would b
2e6b6 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f  e set to SQLITE_
2e6b7 41 46 46 5f 4e 4f 4e 45 2e 0a 2a 2f 0a 73 74 61  AFF_NONE..*/.sta
2e6b8 74 69 63 20 69 6e 74 20 63 6f 64 65 41 6c 6c 45  tic int codeAllE
2e6b9 71 75 61 6c 69 74 79 54 65 72 6d 73 28 0a 20 20  qualityTerms(.  
2e6ba 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2e6bb 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
2e6bc 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68   context */.  Wh
2e6bd 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c  ereLevel *pLevel
2e6be 2c 20 20 20 2f 2a 20 57 68 69 63 68 20 6e 65 73  ,   /* Which nes
2e6bf 74 65 64 20 6c 6f 6f 70 20 6f 66 20 74 68 65 20  ted loop of the 
2e6c0 46 52 4f 4d 20 77 65 20 61 72 65 20 63 6f 64 69  FROM we are codi
2e6c1 6e 67 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61  ng */.  WhereCla
2e6c2 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20 2f 2a  use *pWC,     /*
2e6c3 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
2e6c4 65 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e  e */.  Bitmask n
2e6c5 6f 74 52 65 61 64 79 2c 20 20 20 20 20 2f 2a 20  otReady,     /* 
2e6c6 57 68 69 63 68 20 70 61 72 74 73 20 6f 66 20 46  Which parts of F
2e6c7 52 4f 4d 20 68 61 76 65 20 6e 6f 74 20 79 65 74  ROM have not yet
2e6c8 20 62 65 65 6e 20 63 6f 64 65 64 20 2a 2f 0a 20   been coded */. 
2e6c9 20 69 6e 74 20 6e 45 78 74 72 61 52 65 67 2c 20   int nExtraReg, 
2e6ca 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
2e6cb 20 6f 66 20 65 78 74 72 61 20 72 65 67 69 73 74   of extra regist
2e6cc 65 72 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ers to allocate 
2e6cd 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 41 66  */.  char **pzAf
2e6ce 66 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55  f          /* OU
2e6cf 54 3a 20 53 65 74 20 74 6f 20 70 6f 69 6e 74 20  T: Set to point 
2e6d0 74 6f 20 61 66 66 69 6e 69 74 79 20 73 74 72 69  to affinity stri
2e6d1 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e  ng */.){.  int n
2e6d2 45 71 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61  Eq = pLevel->pla
2e6d3 6e 2e 6e 45 71 3b 20 20 20 2f 2a 20 54 68 65 20  n.nEq;   /* The 
2e6d4 6e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 6f 72 20  number of == or 
2e6d5 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 74  IN constraints t
2e6d6 6f 20 63 6f 64 65 20 2a 2f 0a 20 20 56 64 62 65  o code */.  Vdbe
2e6d7 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
2e6d8 64 62 65 3b 20 20 20 20 20 20 2f 2a 20 54 68 65  dbe;      /* The
2e6d9 20 76 6d 20 75 6e 64 65 72 20 63 6f 6e 73 74 72   vm under constr
2e6da 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 49 6e 64 65  uction */.  Inde
2e6db 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20  x *pIdx;        
2e6dc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2e6dd 20 69 6e 64 65 78 20 62 65 69 6e 67 20 75 73 65   index being use
2e6de 64 20 66 6f 72 20 74 68 69 73 20 6c 6f 6f 70 20  d for this loop 
2e6df 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 20 3d 20  */.  int iCur = 
2e6e0 70 4c 65 76 65 6c 2d 3e 69 54 61 62 43 75 72 3b  pLevel->iTabCur;
2e6e1 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72     /* The cursor
2e6e2 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f   of the table */
2e6e3 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54  .  WhereTerm *pT
2e6e4 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  erm;            
2e6e5 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 63 6f 6e   /* A single con
2e6e6 73 74 72 61 69 6e 74 20 74 65 72 6d 20 2a 2f 0a  straint term */.
2e6e7 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20    int j;        
2e6e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e6e9 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
2e6ea 2a 2f 0a 20 20 69 6e 74 20 72 65 67 42 61 73 65  */.  int regBase
2e6eb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2e6ec 20 20 20 2f 2a 20 42 61 73 65 20 72 65 67 69 73     /* Base regis
2e6ed 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65  ter */.  int nRe
2e6ee 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
2e6ef 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
2e6f0 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 74 6f   of registers to
2e6f1 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 63   allocate */.  c
2e6f2 68 61 72 20 2a 7a 41 66 66 3b 20 20 20 20 20 20  har *zAff;      
2e6f3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e6f4 41 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20  Affinity string 
2e6f5 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a 20 20  to return */..  
2e6f6 2f 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 69  /* This module i
2e6f7 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 6f 6e  s only called on
2e6f8 20 71 75 65 72 79 20 70 6c 61 6e 73 20 74 68 61   query plans tha
2e6f9 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78 2e 20  t use an index. 
2e6fa 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65  */.  assert( pLe
2e6fb 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
2e6fc 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45 58 45  s & WHERE_INDEXE
2e6fd 44 20 29 3b 0a 20 20 70 49 64 78 20 3d 20 70 4c  D );.  pIdx = pL
2e6fe 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64  evel->plan.u.pId
2e6ff 78 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20  x;..  /* Figure 
2e700 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 6d 65 6d  out how many mem
2e701 6f 72 79 20 63 65 6c 6c 73 20 77 65 20 77 69 6c  ory cells we wil
2e702 6c 20 6e 65 65 64 20 74 68 65 6e 20 61 6c 6c 6f  l need then allo
2e703 63 61 74 65 20 74 68 65 6d 2e 0a 20 20 2a 2f 0a  cate them..  */.
2e704 20 20 72 65 67 42 61 73 65 20 3d 20 70 50 61 72    regBase = pPar
2e705 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20  se->nMem + 1;.  
2e706 6e 52 65 67 20 3d 20 70 4c 65 76 65 6c 2d 3e 70  nReg = pLevel->p
2e707 6c 61 6e 2e 6e 45 71 20 2b 20 6e 45 78 74 72 61  lan.nEq + nExtra
2e708 52 65 67 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e  Reg;.  pParse->n
2e709 4d 65 6d 20 2b 3d 20 6e 52 65 67 3b 0a 0a 20 20  Mem += nReg;..  
2e70a 7a 41 66 66 20 3d 20 73 71 6c 69 74 65 33 44 62  zAff = sqlite3Db
2e70b 53 74 72 44 75 70 28 70 50 61 72 73 65 2d 3e 64  StrDup(pParse->d
2e70c 62 2c 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41  b, sqlite3IndexA
2e70d 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70 49  ffinityStr(v, pI
2e70e 64 78 29 29 3b 0a 20 20 69 66 28 20 21 7a 41 66  dx));.  if( !zAf
2e70f 66 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d  f ){.    pParse-
2e710 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
2e711 64 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  d = 1;.  }..  /*
2e712 20 45 76 61 6c 75 61 74 65 20 74 68 65 20 65 71   Evaluate the eq
2e713 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e  uality constrain
2e714 74 73 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  ts.  */.  assert
2e715 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e  ( pIdx->nColumn>
2e716 3d 6e 45 71 20 29 3b 0a 20 20 66 6f 72 28 6a 3d  =nEq );.  for(j=
2e717 30 3b 20 6a 3c 6e 45 71 3b 20 6a 2b 2b 29 7b 0a  0; j<nEq; j++){.
2e718 20 20 20 20 69 6e 74 20 72 31 3b 0a 20 20 20 20      int r1;.    
2e719 69 6e 74 20 6b 20 3d 20 70 49 64 78 2d 3e 61 69  int k = pIdx->ai
2e71a 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 20 70  Column[j];.    p
2e71b 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28  Term = findTerm(
2e71c 70 57 43 2c 20 69 43 75 72 2c 20 6b 2c 20 6e 6f  pWC, iCur, k, no
2e71d 74 52 65 61 64 79 2c 20 70 4c 65 76 65 6c 2d 3e  tReady, pLevel->
2e71e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 2c 20 70 49  plan.wsFlags, pI
2e71f 64 78 29 3b 0a 20 20 20 20 69 66 28 20 4e 45 56  dx);.    if( NEV
2e720 45 52 28 70 54 65 72 6d 3d 3d 30 29 20 29 20 62  ER(pTerm==0) ) b
2e721 72 65 61 6b 3b 0a 20 20 20 20 61 73 73 65 72 74  reak;.    assert
2e722 28 20 28 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67  ( (pTerm->wtFlag
2e723 73 20 26 20 54 45 52 4d 5f 43 4f 44 45 44 29 3d  s & TERM_CODED)=
2e724 3d 30 20 29 3b 0a 20 20 20 20 72 31 20 3d 20 63  =0 );.    r1 = c
2e725 6f 64 65 45 71 75 61 6c 69 74 79 54 65 72 6d 28  odeEqualityTerm(
2e726 70 50 61 72 73 65 2c 20 70 54 65 72 6d 2c 20 70  pParse, pTerm, p
2e727 4c 65 76 65 6c 2c 20 72 65 67 42 61 73 65 2b 6a  Level, regBase+j
2e728 29 3b 0a 20 20 20 20 69 66 28 20 72 31 21 3d 72  );.    if( r1!=r
2e729 65 67 42 61 73 65 2b 6a 20 29 7b 0a 20 20 20 20  egBase+j ){.    
2e72a 20 20 69 66 28 20 6e 52 65 67 3d 3d 31 20 29 7b    if( nReg==1 ){
2e72b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2e72c 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
2e72d 50 61 72 73 65 2c 20 72 65 67 42 61 73 65 29 3b  Parse, regBase);
2e72e 0a 20 20 20 20 20 20 20 20 72 65 67 42 61 73 65  .        regBase
2e72f 20 3d 20 72 31 3b 0a 20 20 20 20 20 20 7d 65 6c   = r1;.      }el
2e730 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
2e731 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2e732 20 4f 50 5f 53 43 6f 70 79 2c 20 72 31 2c 20 72   OP_SCopy, r1, r
2e733 65 67 42 61 73 65 2b 6a 29 3b 0a 20 20 20 20 20  egBase+j);.     
2e734 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 74 65 73   }.    }.    tes
2e735 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f  tcase( pTerm->eO
2e736 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 49 53 4e  perator & WO_ISN
2e737 55 4c 4c 20 29 3b 0a 20 20 20 20 74 65 73 74 63  ULL );.    testc
2e738 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65  ase( pTerm->eOpe
2e739 72 61 74 6f 72 20 26 20 57 4f 5f 49 4e 20 29 3b  rator & WO_IN );
2e73a 0a 20 20 20 20 69 66 28 20 28 70 54 65 72 6d 2d  .    if( (pTerm-
2e73b 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28 57 4f  >eOperator & (WO
2e73c 5f 49 53 4e 55 4c 4c 7c 57 4f 5f 49 4e 29 29 3d  _ISNULL|WO_IN))=
2e73d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
2e73e 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2e73f 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 42   OP_IsNull, regB
2e740 61 73 65 2b 6a 2c 20 70 4c 65 76 65 6c 2d 3e 61  ase+j, pLevel->a
2e741 64 64 72 42 72 6b 29 3b 0a 20 20 20 20 20 20 69  ddrBrk);.      i
2e742 66 28 20 7a 41 66 66 20 0a 20 20 20 20 20 20 20  f( zAff .       
2e743 26 26 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72  && sqlite3Compar
2e744 65 41 66 66 69 6e 69 74 79 28 70 54 65 72 6d 2d  eAffinity(pTerm-
2e745 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20  >pExpr->pRight, 
2e746 7a 41 66 66 5b 6a 5d 29 3d 3d 53 51 4c 49 54 45  zAff[j])==SQLITE
2e747 5f 41 46 46 5f 4e 4f 4e 45 0a 20 20 20 20 20 20  _AFF_NONE.      
2e748 29 7b 0a 20 20 20 20 20 20 20 20 7a 41 66 66 5b  ){.        zAff[
2e749 6a 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  j] = SQLITE_AFF_
2e74a 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20  NONE;.      }.  
2e74b 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 7a 41 66 66    }.  }.  *pzAff
2e74c 20 3d 20 7a 41 66 66 3b 0a 20 20 72 65 74 75 72   = zAff;.  retur
2e74d 6e 20 72 65 67 42 61 73 65 3b 0a 7d 0a 0a 2f 2a  n regBase;.}../*
2e74e 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
2e74f 65 20 66 6f 72 20 74 68 65 20 73 74 61 72 74 20  e for the start 
2e750 6f 66 20 74 68 65 20 69 4c 65 76 65 6c 2d 74 68  of the iLevel-th
2e751 20 6c 6f 6f 70 20 69 6e 20 74 68 65 20 57 48 45   loop in the WHE
2e752 52 45 20 63 6c 61 75 73 65 0a 2a 2a 20 69 6d 70  RE clause.** imp
2e753 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 65 73 63  lementation desc
2e754 72 69 62 65 64 20 62 79 20 70 57 49 6e 66 6f 2e  ribed by pWInfo.
2e755 0a 2a 2f 0a 73 74 61 74 69 63 20 42 69 74 6d 61  .*/.static Bitma
2e756 73 6b 20 63 6f 64 65 4f 6e 65 4c 6f 6f 70 53 74  sk codeOneLoopSt
2e757 61 72 74 28 0a 20 20 57 68 65 72 65 49 6e 66 6f  art(.  WhereInfo
2e758 20 2a 70 57 49 6e 66 6f 2c 20 20 20 2f 2a 20 43   *pWInfo,   /* C
2e759 6f 6d 70 6c 65 74 65 20 69 6e 66 6f 72 6d 61 74  omplete informat
2e75a 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 57 48  ion about the WH
2e75b 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
2e75c 69 6e 74 20 69 4c 65 76 65 6c 2c 20 20 20 20 20  int iLevel,     
2e75d 20 20 20 20 20 2f 2a 20 57 68 69 63 68 20 6c 65       /* Which le
2e75e 76 65 6c 20 6f 66 20 70 57 49 6e 66 6f 2d 3e 61  vel of pWInfo->a
2e75f 5b 5d 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 64  [] should be cod
2e760 65 64 20 2a 2f 0a 20 20 75 31 36 20 77 63 74 72  ed */.  u16 wctr
2e761 6c 46 6c 61 67 73 2c 20 20 20 20 20 20 2f 2a 20  lFlags,      /* 
2e762 4f 6e 65 20 6f 66 20 74 68 65 20 57 48 45 52 45  One of the WHERE
2e763 5f 2a 20 66 6c 61 67 73 20 64 65 66 69 6e 65 64  _* flags defined
2e764 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   in sqliteInt.h 
2e765 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74  */.  Bitmask not
2e766 52 65 61 64 79 20 20 20 20 20 2f 2a 20 57 68 69  Ready     /* Whi
2e767 63 68 20 74 61 62 6c 65 73 20 61 72 65 20 63 75  ch tables are cu
2e768 72 72 65 6e 74 6c 79 20 61 76 61 69 6c 61 62 6c  rrently availabl
2e769 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6a 2c  e */.){.  int j,
2e76a 20 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   k;            /
2e76b 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20  * Loop counters 
2e76c 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b 20 20  */.  int iCur;  
2e76d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2e76e 20 56 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72   VDBE cursor for
2e76f 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20   the table */.  
2e770 69 6e 74 20 61 64 64 72 4e 78 74 3b 20 20 20 20  int addrNxt;    
2e771 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f       /* Where to
2e772 20 6a 75 6d 70 20 74 6f 20 63 6f 6e 74 69 6e 75   jump to continu
2e773 65 20 77 69 74 68 20 74 68 65 20 6e 65 78 74 20  e with the next 
2e774 49 4e 20 63 61 73 65 20 2a 2f 0a 20 20 69 6e 74  IN case */.  int
2e775 20 6f 6d 69 74 54 61 62 6c 65 3b 20 20 20 20 20   omitTable;     
2e776 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20    /* True if we 
2e777 75 73 65 20 74 68 65 20 69 6e 64 65 78 20 6f 6e  use the index on
2e778 6c 79 20 2a 2f 0a 20 20 69 6e 74 20 62 52 65 76  ly */.  int bRev
2e779 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
2e77a 54 72 75 65 20 69 66 20 77 65 20 6e 65 65 64 20  True if we need 
2e77b 74 6f 20 73 63 61 6e 20 69 6e 20 72 65 76 65 72  to scan in rever
2e77c 73 65 20 6f 72 64 65 72 20 2a 2f 0a 20 20 57 68  se order */.  Wh
2e77d 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c  ereLevel *pLevel
2e77e 3b 20 20 2f 2a 20 54 68 65 20 77 68 65 72 65 20  ;  /* The where 
2e77f 6c 65 76 65 6c 20 74 6f 20 62 65 20 63 6f 64 65  level to be code
2e780 64 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75  d */.  WhereClau
2e781 73 65 20 2a 70 57 43 3b 20 20 20 20 2f 2a 20 44  se *pWC;    /* D
2e782 65 63 6f 6d 70 6f 73 69 74 69 6f 6e 20 6f 66 20  ecomposition of 
2e783 74 68 65 20 65 6e 74 69 72 65 20 57 48 45 52 45  the entire WHERE
2e784 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65   clause */.  Whe
2e785 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20  reTerm *pTerm;  
2e786 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e787 41 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74  A WHERE clause t
2e788 65 72 6d 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a  erm */.  Parse *
2e789 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20  pParse;         
2e78a 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
2e78b 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
2e78c 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20   Vdbe *v;       
2e78d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e78e 20 2f 2a 20 54 68 65 20 70 72 65 70 61 72 65 64   /* The prepared
2e78f 20 73 74 6d 74 20 75 6e 64 65 72 20 63 6f 6e 73   stmt under cons
2e790 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 73  tructions */.  s
2e791 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
2e792 65 6d 20 2a 70 54 61 62 49 74 65 6d 3b 20 20 2f  em *pTabItem;  /
2e793 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65  * FROM clause te
2e794 72 6d 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a  rm being coded *
2e795 2f 0a 20 20 69 6e 74 20 61 64 64 72 42 72 6b 3b  /.  int addrBrk;
2e796 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e797 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65      /* Jump here
2e798 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66   to break out of
2e799 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69   the loop */.  i
2e79a 6e 74 20 61 64 64 72 43 6f 6e 74 3b 20 20 20 20  nt addrCont;    
2e79b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2e79c 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 63  * Jump here to c
2e79d 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 6e 65 78  ontinue with nex
2e79e 74 20 63 79 63 6c 65 20 2a 2f 0a 20 20 69 6e 74  t cycle */.  int
2e79f 20 69 52 6f 77 69 64 52 65 67 20 3d 20 30 3b 20   iRowidReg = 0; 
2e7a0 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20         /* Rowid 
2e7a1 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 69  is stored in thi
2e7a2 73 20 72 65 67 69 73 74 65 72 2c 20 69 66 20 6e  s register, if n
2e7a3 6f 74 20 7a 65 72 6f 20 2a 2f 0a 20 20 69 6e 74  ot zero */.  int
2e7a4 20 69 52 65 6c 65 61 73 65 52 65 67 20 3d 20 30   iReleaseReg = 0
2e7a5 3b 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 72  ;      /* Temp r
2e7a6 65 67 69 73 74 65 72 20 74 6f 20 66 72 65 65 20  egister to free 
2e7a7 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
2e7a8 20 2a 2f 0a 0a 20 20 70 50 61 72 73 65 20 3d 20   */..  pParse = 
2e7a9 70 57 49 6e 66 6f 2d 3e 70 50 61 72 73 65 3b 0a  pWInfo->pParse;.
2e7aa 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56    v = pParse->pV
2e7ab 64 62 65 3b 0a 20 20 70 57 43 20 3d 20 70 57 49  dbe;.  pWC = pWI
2e7ac 6e 66 6f 2d 3e 70 57 43 3b 0a 20 20 70 4c 65 76  nfo->pWC;.  pLev
2e7ad 65 6c 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 61 5b  el = &pWInfo->a[
2e7ae 69 4c 65 76 65 6c 5d 3b 0a 20 20 70 54 61 62 49  iLevel];.  pTabI
2e7af 74 65 6d 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 70  tem = &pWInfo->p
2e7b0 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76 65  TabList->a[pLeve
2e7b1 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 69 43 75  l->iFrom];.  iCu
2e7b2 72 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 69 43  r = pTabItem->iC
2e7b3 75 72 73 6f 72 3b 0a 20 20 62 52 65 76 20 3d 20  ursor;.  bRev = 
2e7b4 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73  (pLevel->plan.ws
2e7b5 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 52 45  Flags & WHERE_RE
2e7b6 56 45 52 53 45 29 21 3d 30 3b 0a 20 20 6f 6d 69  VERSE)!=0;.  omi
2e7b7 74 54 61 62 6c 65 20 3d 20 28 70 4c 65 76 65 6c  tTable = (pLevel
2e7b8 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
2e7b9 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29   WHERE_IDX_ONLY)
2e7ba 21 3d 30 20 0a 20 20 20 20 20 20 20 20 20 20 20  !=0 .           
2e7bb 26 26 20 28 77 63 74 72 6c 46 6c 61 67 73 20 26  && (wctrlFlags &
2e7bc 20 57 48 45 52 45 5f 46 4f 52 43 45 5f 54 41 42   WHERE_FORCE_TAB
2e7bd 4c 45 29 3d 3d 30 3b 0a 0a 20 20 2f 2a 20 43 72  LE)==0;..  /* Cr
2e7be 65 61 74 65 20 6c 61 62 65 6c 73 20 66 6f 72 20  eate labels for 
2e7bf 74 68 65 20 22 62 72 65 61 6b 22 20 61 6e 64 20  the "break" and 
2e7c0 22 63 6f 6e 74 69 6e 75 65 22 20 69 6e 73 74 72  "continue" instr
2e7c1 75 63 74 69 6f 6e 73 0a 20 20 2a 2a 20 66 6f 72  uctions.  ** for
2e7c2 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 6f   the current loo
2e7c3 70 2e 20 20 4a 75 6d 70 20 74 6f 20 61 64 64 72  p.  Jump to addr
2e7c4 42 72 6b 20 74 6f 20 62 72 65 61 6b 20 6f 75 74  Brk to break out
2e7c5 20 6f 66 20 61 20 6c 6f 6f 70 2e 0a 20 20 2a 2a   of a loop..  **
2e7c6 20 4a 75 6d 70 20 74 6f 20 63 6f 6e 74 20 74 6f   Jump to cont to
2e7c7 20 67 6f 20 69 6d 6d 65 64 69 61 74 65 6c 79 20   go immediately 
2e7c8 74 6f 20 74 68 65 20 6e 65 78 74 20 69 74 65 72  to the next iter
2e7c9 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20 2a  ation of the.  *
2e7ca 2a 20 6c 6f 6f 70 2e 0a 20 20 2a 2a 0a 20 20 2a  * loop..  **.  *
2e7cb 2a 20 57 68 65 6e 20 74 68 65 72 65 20 69 73 20  * When there is 
2e7cc 61 6e 20 49 4e 20 6f 70 65 72 61 74 6f 72 2c 20  an IN operator, 
2e7cd 77 65 20 61 6c 73 6f 20 68 61 76 65 20 61 20 22  we also have a "
2e7ce 61 64 64 72 4e 78 74 22 20 6c 61 62 65 6c 20 74  addrNxt" label t
2e7cf 68 61 74 0a 20 20 2a 2a 20 6d 65 61 6e 73 20 74  hat.  ** means t
2e7d0 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20  o continue with 
2e7d1 74 68 65 20 6e 65 78 74 20 49 4e 20 76 61 6c 75  the next IN valu
2e7d2 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 2e 20 20  e combination.  
2e7d3 57 68 65 6e 0a 20 20 2a 2a 20 74 68 65 72 65 20  When.  ** there 
2e7d4 61 72 65 20 6e 6f 20 49 4e 20 6f 70 65 72 61 74  are no IN operat
2e7d5 6f 72 73 20 69 6e 20 74 68 65 20 63 6f 6e 73 74  ors in the const
2e7d6 72 61 69 6e 74 73 2c 20 74 68 65 20 22 61 64 64  raints, the "add
2e7d7 72 4e 78 74 22 20 6c 61 62 65 6c 0a 20 20 2a 2a  rNxt" label.  **
2e7d8 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
2e7d9 22 61 64 64 72 42 72 6b 22 2e 0a 20 20 2a 2f 0a  "addrBrk"..  */.
2e7da 20 20 61 64 64 72 42 72 6b 20 3d 20 70 4c 65 76    addrBrk = pLev
2e7db 65 6c 2d 3e 61 64 64 72 42 72 6b 20 3d 20 70 4c  el->addrBrk = pL
2e7dc 65 76 65 6c 2d 3e 61 64 64 72 4e 78 74 20 3d 20  evel->addrNxt = 
2e7dd 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
2e7de 61 62 65 6c 28 76 29 3b 0a 20 20 61 64 64 72 43  abel(v);.  addrC
2e7df 6f 6e 74 20 3d 20 70 4c 65 76 65 6c 2d 3e 61 64  ont = pLevel->ad
2e7e0 64 72 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33  drCont = sqlite3
2e7e1 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
2e7e2 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20  ;..  /* If this 
2e7e3 69 73 20 74 68 65 20 72 69 67 68 74 20 74 61 62  is the right tab
2e7e4 6c 65 20 6f 66 20 61 20 4c 45 46 54 20 4f 55 54  le of a LEFT OUT
2e7e5 45 52 20 4a 4f 49 4e 2c 20 61 6c 6c 6f 63 61 74  ER JOIN, allocat
2e7e6 65 20 61 6e 64 0a 20 20 2a 2a 20 69 6e 69 74 69  e and.  ** initi
2e7e7 61 6c 69 7a 65 20 61 20 6d 65 6d 6f 72 79 20 63  alize a memory c
2e7e8 65 6c 6c 20 74 68 61 74 20 72 65 63 6f 72 64 73  ell that records
2e7e9 20 69 66 20 74 68 69 73 20 74 61 62 6c 65 20 6d   if this table m
2e7ea 61 74 63 68 65 73 20 61 6e 79 0a 20 20 2a 2a 20  atches any.  ** 
2e7eb 72 6f 77 20 6f 66 20 74 68 65 20 6c 65 66 74 20  row of the left 
2e7ec 74 61 62 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69  table of the joi
2e7ed 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4c  n..  */.  if( pL
2e7ee 65 76 65 6c 2d 3e 69 46 72 6f 6d 3e 30 20 26 26  evel->iFrom>0 &&
2e7ef 20 28 70 54 61 62 49 74 65 6d 5b 30 5d 2e 6a 6f   (pTabItem[0].jo
2e7f0 69 6e 74 79 70 65 20 26 20 4a 54 5f 4c 45 46 54  intype & JT_LEFT
2e7f1 29 21 3d 30 20 29 7b 0a 20 20 20 20 70 4c 65 76  )!=0 ){.    pLev
2e7f2 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20 3d 20  el->iLeftJoin = 
2e7f3 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2e7f4 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2e7f5 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
2e7f6 67 65 72 2c 20 30 2c 20 70 4c 65 76 65 6c 2d 3e  ger, 0, pLevel->
2e7f7 69 4c 65 66 74 4a 6f 69 6e 29 3b 0a 20 20 20 20  iLeftJoin);.    
2e7f8 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
2e7f9 22 69 6e 69 74 20 4c 45 46 54 20 4a 4f 49 4e 20  "init LEFT JOIN 
2e7fa 6e 6f 2d 6d 61 74 63 68 20 66 6c 61 67 22 29 29  no-match flag"))
2e7fb 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53  ;.  }..#ifndef S
2e7fc 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
2e7fd 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 20 28  ALTABLE.  if(  (
2e7fe 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
2e7ff 6c 61 67 73 20 26 20 57 48 45 52 45 5f 56 49 52  lags & WHERE_VIR
2e800 54 55 41 4c 54 41 42 4c 45 29 21 3d 30 20 29 7b  TUALTABLE)!=0 ){
2e801 0a 20 20 20 20 2f 2a 20 43 61 73 65 20 30 3a 20  .    /* Case 0: 
2e802 20 54 68 65 20 74 61 62 6c 65 20 69 73 20 61 20   The table is a 
2e803 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 2e 20 20  virtual-table.  
2e804 55 73 65 20 74 68 65 20 56 46 69 6c 74 65 72 20  Use the VFilter 
2e805 61 6e 64 20 56 4e 65 78 74 0a 20 20 20 20 2a 2a  and VNext.    **
2e806 20 20 20 20 20 20 20 20 20 20 74 6f 20 61 63 63            to acc
2e807 65 73 73 20 74 68 65 20 64 61 74 61 2e 0a 20 20  ess the data..  
2e808 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 52 65    */.    int iRe
2e809 67 3b 20 20 20 2f 2a 20 50 33 20 56 61 6c 75 65  g;   /* P3 Value
2e80a 20 66 6f 72 20 4f 50 5f 56 46 69 6c 74 65 72 20   for OP_VFilter 
2e80b 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69  */.    sqlite3_i
2e80c 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 56 74 61 62  ndex_info *pVtab
2e80d 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c  Idx = pLevel->pl
2e80e 61 6e 2e 75 2e 70 56 74 61 62 49 64 78 3b 0a 20  an.u.pVtabIdx;. 
2e80f 20 20 20 69 6e 74 20 6e 43 6f 6e 73 74 72 61 69     int nConstrai
2e810 6e 74 20 3d 20 70 56 74 61 62 49 64 78 2d 3e 6e  nt = pVtabIdx->n
2e811 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20 20 20  Constraint;.    
2e812 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
2e813 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f  ndex_constraint_
2e814 75 73 61 67 65 20 2a 61 55 73 61 67 65 20 3d 0a  usage *aUsage =.
2e815 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e816 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e817 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e818 70 56 74 61 62 49 64 78 2d 3e 61 43 6f 6e 73 74  pVtabIdx->aConst
2e819 72 61 69 6e 74 55 73 61 67 65 3b 0a 20 20 20 20  raintUsage;.    
2e81a 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73 71 6c  const struct sql
2e81b 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74  ite3_index_const
2e81c 72 61 69 6e 74 20 2a 61 43 6f 6e 73 74 72 61 69  raint *aConstrai
2e81d 6e 74 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20  nt =.           
2e81e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e81f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e820 20 20 20 20 20 70 56 74 61 62 49 64 78 2d 3e 61       pVtabIdx->a
2e821 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 0a 20 20 20  Constraint;..   
2e822 20 69 52 65 67 20 3d 20 73 71 6c 69 74 65 33 47   iReg = sqlite3G
2e823 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  etTempRange(pPar
2e824 73 65 2c 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b  se, nConstraint+
2e825 32 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 31 3b  2);.    for(j=1;
2e826 20 6a 3c 3d 6e 43 6f 6e 73 74 72 61 69 6e 74 3b   j<=nConstraint;
2e827 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6f 72   j++){.      for
2e828 28 6b 3d 30 3b 20 6b 3c 6e 43 6f 6e 73 74 72 61  (k=0; k<nConstra
2e829 69 6e 74 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20  int; k++){.     
2e82a 20 20 20 69 66 28 20 61 55 73 61 67 65 5b 6b 5d     if( aUsage[k]
2e82b 2e 61 72 67 76 49 6e 64 65 78 3d 3d 6a 20 29 7b  .argvIndex==j ){
2e82c 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69  .          int i
2e82d 54 65 72 6d 20 3d 20 61 43 6f 6e 73 74 72 61 69  Term = aConstrai
2e82e 6e 74 5b 6b 5d 2e 69 54 65 72 6d 4f 66 66 73 65  nt[k].iTermOffse
2e82f 74 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  t;.          sql
2e830 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
2e831 72 73 65 2c 20 70 57 43 2d 3e 61 5b 69 54 65 72  rse, pWC->a[iTer
2e832 6d 5d 2e 70 45 78 70 72 2d 3e 70 52 69 67 68 74  m].pExpr->pRight
2e833 2c 20 69 52 65 67 2b 6a 2b 31 29 3b 0a 20 20 20  , iReg+j+1);.   
2e834 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2e835 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2e836 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 6e 43 6f        if( k==nCo
2e837 6e 73 74 72 61 69 6e 74 20 29 20 62 72 65 61 6b  nstraint ) break
2e838 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
2e839 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2e83a 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70 56 74   OP_Integer, pVt
2e83b 61 62 49 64 78 2d 3e 69 64 78 4e 75 6d 2c 20 69  abIdx->idxNum, i
2e83c 52 65 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Reg);.    sqlite
2e83d 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2e83e 50 5f 49 6e 74 65 67 65 72 2c 20 6a 2d 31 2c 20  P_Integer, j-1, 
2e83f 69 52 65 67 2b 31 29 3b 0a 20 20 20 20 73 71 6c  iReg+1);.    sql
2e840 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2e841 2c 20 4f 50 5f 56 46 69 6c 74 65 72 2c 20 69 43  , OP_VFilter, iC
2e842 75 72 2c 20 61 64 64 72 42 72 6b 2c 20 69 52 65  ur, addrBrk, iRe
2e843 67 2c 20 70 56 74 61 62 49 64 78 2d 3e 69 64 78  g, pVtabIdx->idx
2e844 53 74 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Str,.           
2e845 20 20 20 20 20 20 20 20 20 20 20 70 56 74 61 62             pVtab
2e846 49 64 78 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49  Idx->needToFreeI
2e847 64 78 53 74 72 20 3f 20 50 34 5f 4d 50 52 49 4e  dxStr ? P4_MPRIN
2e848 54 46 20 3a 20 50 34 5f 53 54 41 54 49 43 29 3b  TF : P4_STATIC);
2e849 0a 20 20 20 20 70 56 74 61 62 49 64 78 2d 3e 6e  .    pVtabIdx->n
2e84a 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 20  eedToFreeIdxStr 
2e84b 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30  = 0;.    for(j=0
2e84c 3b 20 6a 3c 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  ; j<nConstraint;
2e84d 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   j++){.      if(
2e84e 20 61 55 73 61 67 65 5b 6a 5d 2e 6f 6d 69 74 20   aUsage[j].omit 
2e84f 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  ){.        int i
2e850 54 65 72 6d 20 3d 20 61 43 6f 6e 73 74 72 61 69  Term = aConstrai
2e851 6e 74 5b 6a 5d 2e 69 54 65 72 6d 4f 66 66 73 65  nt[j].iTermOffse
2e852 74 3b 0a 20 20 20 20 20 20 20 20 64 69 73 61 62  t;.        disab
2e853 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 26  leTerm(pLevel, &
2e854 70 57 43 2d 3e 61 5b 69 54 65 72 6d 5d 29 3b 0a  pWC->a[iTerm]);.
2e855 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2e856 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f    pLevel->op = O
2e857 50 5f 56 4e 65 78 74 3b 0a 20 20 20 20 70 4c 65  P_VNext;.    pLe
2e858 76 65 6c 2d 3e 70 31 20 3d 20 69 43 75 72 3b 0a  vel->p1 = iCur;.
2e859 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d      pLevel->p2 =
2e85a 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
2e85b 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20  entAddr(v);.    
2e85c 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2e85d 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
2e85e 69 52 65 67 2c 20 6e 43 6f 6e 73 74 72 61 69 6e  iReg, nConstrain
2e85f 74 2b 32 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65  t+2);.  }else.#e
2e860 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2e861 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2e862 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4c 65 76 65   */..  if( pLeve
2e863 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
2e864 26 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51  & WHERE_ROWID_EQ
2e865 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65 20   ){.    /* Case 
2e866 31 3a 20 20 57 65 20 63 61 6e 20 64 69 72 65 63  1:  We can direc
2e867 74 6c 79 20 72 65 66 65 72 65 6e 63 65 20 61 20  tly reference a 
2e868 73 69 6e 67 6c 65 20 72 6f 77 20 75 73 69 6e 67  single row using
2e869 20 61 6e 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   an.    **      
2e86a 20 20 20 20 65 71 75 61 6c 69 74 79 20 63 6f 6d      equality com
2e86b 70 61 72 69 73 6f 6e 20 61 67 61 69 6e 73 74 20  parison against 
2e86c 74 68 65 20 52 4f 57 49 44 20 66 69 65 6c 64 2e  the ROWID field.
2e86d 20 20 4f 72 0a 20 20 20 20 2a 2a 20 20 20 20 20    Or.    **     
2e86e 20 20 20 20 20 77 65 20 72 65 66 65 72 65 6e 63       we referenc
2e86f 65 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77 73 20  e multiple rows 
2e870 75 73 69 6e 67 20 61 20 22 72 6f 77 69 64 20 49  using a "rowid I
2e871 4e 20 28 2e 2e 2e 29 22 0a 20 20 20 20 2a 2a 20  N (...)".    ** 
2e872 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 72 75           constru
2e873 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ct..    */.    i
2e874 52 65 6c 65 61 73 65 52 65 67 20 3d 20 73 71 6c  ReleaseReg = sql
2e875 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
2e876 50 61 72 73 65 29 3b 0a 20 20 20 20 70 54 65 72  Parse);.    pTer
2e877 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43  m = findTerm(pWC
2e878 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52  , iCur, -1, notR
2e879 65 61 64 79 2c 20 57 4f 5f 45 51 7c 57 4f 5f 49  eady, WO_EQ|WO_I
2e87a 4e 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65 72  N, 0);.    asser
2e87b 74 28 20 70 54 65 72 6d 21 3d 30 20 29 3b 0a 20  t( pTerm!=0 );. 
2e87c 20 20 20 61 73 73 65 72 74 28 20 70 54 65 72 6d     assert( pTerm
2e87d 2d 3e 70 45 78 70 72 21 3d 30 20 29 3b 0a 20 20  ->pExpr!=0 );.  
2e87e 20 20 61 73 73 65 72 74 28 20 70 54 65 72 6d 2d    assert( pTerm-
2e87f 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75  >leftCursor==iCu
2e880 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  r );.    assert(
2e881 20 6f 6d 69 74 54 61 62 6c 65 3d 3d 30 20 29 3b   omitTable==0 );
2e882 0a 20 20 20 20 69 52 6f 77 69 64 52 65 67 20 3d  .    iRowidReg =
2e883 20 63 6f 64 65 45 71 75 61 6c 69 74 79 54 65 72   codeEqualityTer
2e884 6d 28 70 50 61 72 73 65 2c 20 70 54 65 72 6d 2c  m(pParse, pTerm,
2e885 20 70 4c 65 76 65 6c 2c 20 69 52 65 6c 65 61 73   pLevel, iReleas
2e886 65 52 65 67 29 3b 0a 20 20 20 20 61 64 64 72 4e  eReg);.    addrN
2e887 78 74 20 3d 20 70 4c 65 76 65 6c 2d 3e 61 64 64  xt = pLevel->add
2e888 72 4e 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  rNxt;.    sqlite
2e889 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2e88a 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 69 52 6f  P_MustBeInt, iRo
2e88b 77 69 64 52 65 67 2c 20 61 64 64 72 4e 78 74 29  widReg, addrNxt)
2e88c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2e88d 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f  eAddOp3(v, OP_No
2e88e 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20 61  tExists, iCur, a
2e88f 64 64 72 4e 78 74 2c 20 69 52 6f 77 69 64 52 65  ddrNxt, iRowidRe
2e890 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  g);.    sqlite3E
2e891 78 70 72 43 61 63 68 65 53 74 6f 72 65 28 70 50  xprCacheStore(pP
2e892 61 72 73 65 2c 20 69 43 75 72 2c 20 2d 31 2c 20  arse, iCur, -1, 
2e893 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 20  iRowidReg);.    
2e894 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
2e895 22 70 6b 22 29 29 3b 0a 20 20 20 20 70 4c 65 76  "pk"));.    pLev
2e896 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 4e 6f 6f 70  el->op = OP_Noop
2e897 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4c  ;.  }else if( pL
2e898 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
2e899 67 73 20 26 20 57 48 45 52 45 5f 52 4f 57 49 44  gs & WHERE_ROWID
2e89a 5f 52 41 4e 47 45 20 29 7b 0a 20 20 20 20 2f 2a  _RANGE ){.    /*
2e89b 20 43 61 73 65 20 32 3a 20 20 57 65 20 68 61 76   Case 2:  We hav
2e89c 65 20 61 6e 20 69 6e 65 71 75 61 6c 69 74 79 20  e an inequality 
2e89d 63 6f 6d 70 61 72 69 73 6f 6e 20 61 67 61 69 6e  comparison again
2e89e 73 74 20 74 68 65 20 52 4f 57 49 44 20 66 69 65  st the ROWID fie
2e89f 6c 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ld..    */.    i
2e8a0 6e 74 20 74 65 73 74 4f 70 20 3d 20 4f 50 5f 4e  nt testOp = OP_N
2e8a1 6f 6f 70 3b 0a 20 20 20 20 69 6e 74 20 73 74 61  oop;.    int sta
2e8a2 72 74 3b 0a 20 20 20 20 69 6e 74 20 6d 65 6d 45  rt;.    int memE
2e8a3 6e 64 56 61 6c 75 65 20 3d 20 30 3b 0a 20 20 20  ndValue = 0;.   
2e8a4 20 57 68 65 72 65 54 65 72 6d 20 2a 70 53 74 61   WhereTerm *pSta
2e8a5 72 74 2c 20 2a 70 45 6e 64 3b 0a 0a 20 20 20 20  rt, *pEnd;..    
2e8a6 61 73 73 65 72 74 28 20 6f 6d 69 74 54 61 62 6c  assert( omitTabl
2e8a7 65 3d 3d 30 20 29 3b 0a 20 20 20 20 70 53 74 61  e==0 );.    pSta
2e8a8 72 74 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57  rt = findTerm(pW
2e8a9 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74  C, iCur, -1, not
2e8aa 52 65 61 64 79 2c 20 57 4f 5f 47 54 7c 57 4f 5f  Ready, WO_GT|WO_
2e8ab 47 45 2c 20 30 29 3b 0a 20 20 20 20 70 45 6e 64  GE, 0);.    pEnd
2e8ac 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c   = findTerm(pWC,
2e8ad 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65   iCur, -1, notRe
2e8ae 61 64 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45  ady, WO_LT|WO_LE
2e8af 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 62 52  , 0);.    if( bR
2e8b0 65 76 20 29 7b 0a 20 20 20 20 20 20 70 54 65 72  ev ){.      pTer
2e8b1 6d 20 3d 20 70 53 74 61 72 74 3b 0a 20 20 20 20  m = pStart;.    
2e8b2 20 20 70 53 74 61 72 74 20 3d 20 70 45 6e 64 3b    pStart = pEnd;
2e8b3 0a 20 20 20 20 20 20 70 45 6e 64 20 3d 20 70 54  .      pEnd = pT
2e8b4 65 72 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  erm;.    }.    i
2e8b5 66 28 20 70 53 74 61 72 74 20 29 7b 0a 20 20 20  f( pStart ){.   
2e8b6 20 20 20 45 78 70 72 20 2a 70 58 3b 20 20 20 20     Expr *pX;    
2e8b7 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2e8b8 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20  expression that 
2e8b9 64 65 66 69 6e 65 73 20 74 68 65 20 73 74 61 72  defines the star
2e8ba 74 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 20  t bound */.     
2e8bb 20 69 6e 74 20 72 31 2c 20 72 54 65 6d 70 3b 20   int r1, rTemp; 
2e8bc 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
2e8bd 65 72 73 20 66 6f 72 20 68 6f 6c 64 69 6e 67 20  ers for holding 
2e8be 74 68 65 20 73 74 61 72 74 20 62 6f 75 6e 64 61  the start bounda
2e8bf 72 79 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20  ry */..      /* 
2e8c0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  The following co
2e8c1 6e 73 74 61 6e 74 20 6d 61 70 73 20 54 4b 5f 78  nstant maps TK_x
2e8c2 78 20 63 6f 64 65 73 20 69 6e 74 6f 20 63 6f 72  x codes into cor
2e8c3 72 65 73 70 6f 6e 64 69 6e 67 20 0a 20 20 20 20  responding .    
2e8c4 20 20 2a 2a 20 73 65 65 6b 20 6f 70 63 6f 64 65    ** seek opcode
2e8c5 73 2e 20 20 49 74 20 64 65 70 65 6e 64 73 20 6f  s.  It depends o
2e8c6 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 6f  n a particular o
2e8c7 72 64 65 72 69 6e 67 20 6f 66 20 54 4b 5f 78 78  rdering of TK_xx
2e8c8 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2e8c9 63 6f 6e 73 74 20 75 38 20 61 4d 6f 76 65 4f 70  const u8 aMoveOp
2e8ca 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20  [] = {.         
2e8cb 20 20 2f 2a 20 54 4b 5f 47 54 20 2a 2f 20 20 4f    /* TK_GT */  O
2e8cc 50 5f 53 65 65 6b 47 74 2c 0a 20 20 20 20 20 20  P_SeekGt,.      
2e8cd 20 20 20 20 20 2f 2a 20 54 4b 5f 4c 45 20 2a 2f       /* TK_LE */
2e8ce 20 20 4f 50 5f 53 65 65 6b 4c 65 2c 0a 20 20 20    OP_SeekLe,.   
2e8cf 20 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f 4c 54          /* TK_LT
2e8d0 20 2a 2f 20 20 4f 50 5f 53 65 65 6b 4c 74 2c 0a   */  OP_SeekLt,.
2e8d1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 4b             /* TK
2e8d2 5f 47 45 20 2a 2f 20 20 4f 50 5f 53 65 65 6b 47  _GE */  OP_SeekG
2e8d3 65 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20  e.      };.     
2e8d4 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 45 3d 3d   assert( TK_LE==
2e8d5 54 4b 5f 47 54 2b 31 20 29 3b 20 20 20 20 20 20  TK_GT+1 );      
2e8d6 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  /* Make sure the
2e8d7 20 6f 72 64 65 72 69 6e 67 2e 2e 20 2a 2f 0a 20   ordering.. */. 
2e8d8 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
2e8d9 4c 54 3d 3d 54 4b 5f 47 54 2b 32 20 29 3b 20 20  LT==TK_GT+2 );  
2e8da 20 20 20 20 2f 2a 20 20 2e 2e 2e 20 6f 66 20 74      /*  ... of t
2e8db 68 65 20 54 4b 5f 78 78 20 76 61 6c 75 65 73 2e  he TK_xx values.
2e8dc 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65  .. */.      asse
2e8dd 72 74 28 20 54 4b 5f 47 45 3d 3d 54 4b 5f 47 54  rt( TK_GE==TK_GT
2e8de 2b 33 20 29 3b 20 20 20 20 20 20 2f 2a 20 20 2e  +3 );      /*  .
2e8df 2e 2e 20 69 73 20 63 6f 72 72 65 63 63 74 2e 20  .. is correcct. 
2e8e0 2a 2f 0a 0a 20 20 20 20 20 20 70 58 20 3d 20 70  */..      pX = p
2e8e1 53 74 61 72 74 2d 3e 70 45 78 70 72 3b 0a 20 20  Start->pExpr;.  
2e8e2 20 20 20 20 61 73 73 65 72 74 28 20 70 58 21 3d      assert( pX!=
2e8e3 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  0 );.      asser
2e8e4 74 28 20 70 53 74 61 72 74 2d 3e 6c 65 66 74 43  t( pStart->leftC
2e8e5 75 72 73 6f 72 3d 3d 69 43 75 72 20 29 3b 0a 20  ursor==iCur );. 
2e8e6 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65       r1 = sqlite
2e8e7 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
2e8e8 61 72 73 65 2c 20 70 58 2d 3e 70 52 69 67 68 74  arse, pX->pRight
2e8e9 2c 20 26 72 54 65 6d 70 29 3b 0a 20 20 20 20 20  , &rTemp);.     
2e8ea 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2e8eb 70 33 28 76 2c 20 61 4d 6f 76 65 4f 70 5b 70 58  p3(v, aMoveOp[pX
2e8ec 2d 3e 6f 70 2d 54 4b 5f 47 54 5d 2c 20 69 43 75  ->op-TK_GT], iCu
2e8ed 72 2c 20 61 64 64 72 42 72 6b 2c 20 72 31 29 3b  r, addrBrk, r1);
2e8ee 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
2e8ef 6e 74 28 28 76 2c 20 22 70 6b 22 29 29 3b 0a 20  nt((v, "pk"));. 
2e8f0 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2e8f1 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
2e8f2 6e 67 65 28 70 50 61 72 73 65 2c 20 72 31 2c 20  nge(pParse, r1, 
2e8f3 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
2e8f4 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
2e8f5 70 50 61 72 73 65 2c 20 72 54 65 6d 70 29 3b 0a  pParse, rTemp);.
2e8f6 20 20 20 20 20 20 64 69 73 61 62 6c 65 54 65 72        disableTer
2e8f7 6d 28 70 4c 65 76 65 6c 2c 20 70 53 74 61 72 74  m(pLevel, pStart
2e8f8 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
2e8f9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2e8fa 64 64 4f 70 32 28 76 2c 20 62 52 65 76 20 3f 20  ddOp2(v, bRev ? 
2e8fb 4f 50 5f 4c 61 73 74 20 3a 20 4f 50 5f 52 65 77  OP_Last : OP_Rew
2e8fc 69 6e 64 2c 20 69 43 75 72 2c 20 61 64 64 72 42  ind, iCur, addrB
2e8fd 72 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  rk);.    }.    i
2e8fe 66 28 20 70 45 6e 64 20 29 7b 0a 20 20 20 20 20  f( pEnd ){.     
2e8ff 20 45 78 70 72 20 2a 70 58 3b 0a 20 20 20 20 20   Expr *pX;.     
2e900 20 70 58 20 3d 20 70 45 6e 64 2d 3e 70 45 78 70   pX = pEnd->pExp
2e901 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  r;.      assert(
2e902 20 70 58 21 3d 30 20 29 3b 0a 20 20 20 20 20 20   pX!=0 );.      
2e903 61 73 73 65 72 74 28 20 70 45 6e 64 2d 3e 6c 65  assert( pEnd->le
2e904 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 29  ftCursor==iCur )
2e905 3b 0a 20 20 20 20 20 20 6d 65 6d 45 6e 64 56 61  ;.      memEndVa
2e906 6c 75 65 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  lue = ++pParse->
2e907 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69  nMem;.      sqli
2e908 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
2e909 73 65 2c 20 70 58 2d 3e 70 52 69 67 68 74 2c 20  se, pX->pRight, 
2e90a 6d 65 6d 45 6e 64 56 61 6c 75 65 29 3b 0a 20 20  memEndValue);.  
2e90b 20 20 20 20 69 66 28 20 70 58 2d 3e 6f 70 3d 3d      if( pX->op==
2e90c 54 4b 5f 4c 54 20 7c 7c 20 70 58 2d 3e 6f 70 3d  TK_LT || pX->op=
2e90d 3d 54 4b 5f 47 54 20 29 7b 0a 20 20 20 20 20 20  =TK_GT ){.      
2e90e 20 20 74 65 73 74 4f 70 20 3d 20 62 52 65 76 20    testOp = bRev 
2e90f 3f 20 4f 50 5f 4c 65 20 3a 20 4f 50 5f 47 65 3b  ? OP_Le : OP_Ge;
2e910 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2e911 20 20 20 20 20 20 74 65 73 74 4f 70 20 3d 20 62        testOp = b
2e912 52 65 76 20 3f 20 4f 50 5f 4c 74 20 3a 20 4f 50  Rev ? OP_Lt : OP
2e913 5f 47 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  _Gt;.      }.   
2e914 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70     disableTerm(p
2e915 4c 65 76 65 6c 2c 20 70 45 6e 64 29 3b 0a 20 20  Level, pEnd);.  
2e916 20 20 7d 0a 20 20 20 20 73 74 61 72 74 20 3d 20    }.    start = 
2e917 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
2e918 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 70  ntAddr(v);.    p
2e919 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 62 52 65 76  Level->op = bRev
2e91a 20 3f 20 4f 50 5f 50 72 65 76 20 3a 20 4f 50 5f   ? OP_Prev : OP_
2e91b 4e 65 78 74 3b 0a 20 20 20 20 70 4c 65 76 65 6c  Next;.    pLevel
2e91c 2d 3e 70 31 20 3d 20 69 43 75 72 3b 0a 20 20 20  ->p1 = iCur;.   
2e91d 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 73 74   pLevel->p2 = st
2e91e 61 72 74 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  art;.    pLevel-
2e91f 3e 70 35 20 3d 20 28 70 53 74 61 72 74 3d 3d 30  >p5 = (pStart==0
2e920 20 26 26 20 70 45 6e 64 3d 3d 30 29 20 3f 31 3a   && pEnd==0) ?1:
2e921 30 3b 0a 20 20 20 20 69 66 28 20 74 65 73 74 4f  0;.    if( testO
2e922 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20 20  p!=OP_Noop ){.  
2e923 20 20 20 20 69 52 6f 77 69 64 52 65 67 20 3d 20      iRowidReg = 
2e924 69 52 65 6c 65 61 73 65 52 65 67 20 3d 20 73 71  iReleaseReg = sq
2e925 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
2e926 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73  pParse);.      s
2e927 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2e928 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43  (v, OP_Rowid, iC
2e929 75 72 2c 20 69 52 6f 77 69 64 52 65 67 29 3b 0a  ur, iRowidReg);.
2e92a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2e92b 72 43 61 63 68 65 53 74 6f 72 65 28 70 50 61 72  rCacheStore(pPar
2e92c 73 65 2c 20 69 43 75 72 2c 20 2d 31 2c 20 69 52  se, iCur, -1, iR
2e92d 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 20 20 20  owidReg);.      
2e92e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2e92f 33 28 76 2c 20 74 65 73 74 4f 70 2c 20 6d 65 6d  3(v, testOp, mem
2e930 45 6e 64 56 61 6c 75 65 2c 20 61 64 64 72 42 72  EndValue, addrBr
2e931 6b 2c 20 69 52 6f 77 69 64 52 65 67 29 3b 0a 20  k, iRowidReg);. 
2e932 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2e933 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49  ChangeP5(v, SQLI
2e934 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 7c  TE_AFF_NUMERIC |
2e935 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55   SQLITE_JUMPIFNU
2e936 4c 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  LL);.    }.  }el
2e937 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70  se if( pLevel->p
2e938 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 28 57  lan.wsFlags & (W
2e939 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47  HERE_COLUMN_RANG
2e93a 45 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45  E|WHERE_COLUMN_E
2e93b 51 29 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73  Q) ){.    /* Cas
2e93c 65 20 33 3a 20 41 20 73 63 61 6e 20 75 73 69 6e  e 3: A scan usin
2e93d 67 20 61 6e 20 69 6e 64 65 78 2e 0a 20 20 20 20  g an index..    
2e93e 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  **.    **       
2e93f 20 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75    The WHERE clau
2e940 73 65 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 7a  se may contain z
2e941 65 72 6f 20 6f 72 20 6d 6f 72 65 20 65 71 75 61  ero or more equa
2e942 6c 69 74 79 20 0a 20 20 20 20 2a 2a 20 20 20 20  lity .    **    
2e943 20 20 20 20 20 74 65 72 6d 73 20 28 22 3d 3d 22       terms ("=="
2e944 20 6f 72 20 22 49 4e 22 20 6f 70 65 72 61 74 6f   or "IN" operato
2e945 72 73 29 20 74 68 61 74 20 72 65 66 65 72 20 74  rs) that refer t
2e946 6f 20 74 68 65 20 4e 0a 20 20 20 20 2a 2a 20 20  o the N.    **  
2e947 20 20 20 20 20 20 20 6c 65 66 74 2d 6d 6f 73 74         left-most
2e948 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20   columns of the 
2e949 69 6e 64 65 78 2e 20 49 74 20 6d 61 79 20 61 6c  index. It may al
2e94a 73 6f 20 63 6f 6e 74 61 69 6e 0a 20 20 20 20 2a  so contain.    *
2e94b 2a 20 20 20 20 20 20 20 20 20 69 6e 65 71 75 61  *         inequa
2e94c 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73  lity constraints
2e94d 20 28 3e 2c 20 3c 2c 20 3e 3d 20 6f 72 20 3c 3d   (>, <, >= or <=
2e94e 29 20 6f 6e 20 74 68 65 20 69 6e 64 65 78 65 64  ) on the indexed
2e94f 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2e950 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 6d 6d 65  column that imme
2e951 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 73 20  diately follows 
2e952 74 68 65 20 4e 20 65 71 75 61 6c 69 74 69 65 73  the N equalities
2e953 2e 20 4f 6e 6c 79 20 0a 20 20 20 20 2a 2a 20 20  . Only .    **  
2e954 20 20 20 20 20 20 20 74 68 65 20 72 69 67 68 74         the right
2e955 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 63 61 6e  -most column can
2e956 20 62 65 20 61 6e 20 69 6e 65 71 75 61 6c 69 74   be an inequalit
2e957 79 20 2d 20 74 68 65 20 72 65 73 74 20 6d 75 73  y - the rest mus
2e958 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  t.    **        
2e959 20 75 73 65 20 74 68 65 20 22 3d 3d 22 20 61 6e   use the "==" an
2e95a 64 20 22 49 4e 22 20 6f 70 65 72 61 74 6f 72 73  d "IN" operators
2e95b 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69  . For example, i
2e95c 66 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 20 20  f the .    **   
2e95d 20 20 20 20 20 20 69 6e 64 65 78 20 69 73 20 6f        index is o
2e95e 6e 20 28 78 2c 79 2c 7a 29 2c 20 74 68 65 6e 20  n (x,y,z), then 
2e95f 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6c  the following cl
2e960 61 75 73 65 73 20 61 72 65 20 61 6c 6c 20 0a 20  auses are all . 
2e961 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 6f 70     **         op
2e962 74 69 6d 69 7a 65 64 3a 0a 20 20 20 20 2a 2a 0a  timized:.    **.
2e963 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
2e964 20 20 78 3d 35 0a 20 20 20 20 2a 2a 20 20 20 20    x=5.    **    
2e965 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20          x=5 AND 
2e966 79 3d 31 30 0a 20 20 20 20 2a 2a 20 20 20 20 20  y=10.    **     
2e967 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20 79         x=5 AND y
2e968 3c 31 30 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  <10.    **      
2e969 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20 79 3e        x=5 AND y>
2e96a 35 20 41 4e 44 20 79 3c 31 30 0a 20 20 20 20 2a  5 AND y<10.    *
2e96b 2a 20 20 20 20 20 20 20 20 20 20 20 20 78 3d 35  *            x=5
2e96c 20 41 4e 44 20 79 3d 35 20 41 4e 44 20 7a 3c 3d   AND y=5 AND z<=
2e96d 31 30 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  10.    **.    **
2e96e 20 20 20 20 20 20 20 20 20 54 68 65 20 7a 3c 31           The z<1
2e96f 30 20 74 65 72 6d 20 6f 66 20 74 68 65 20 66 6f  0 term of the fo
2e970 6c 6c 6f 77 69 6e 67 20 63 61 6e 6e 6f 74 20 62  llowing cannot b
2e971 65 20 75 73 65 64 2c 20 6f 6e 6c 79 0a 20 20 20  e used, only.   
2e972 20 2a 2a 20 20 20 20 20 20 20 20 20 74 68 65 20   **         the 
2e973 78 3d 35 20 74 65 72 6d 3a 0a 20 20 20 20 2a 2a  x=5 term:.    **
2e974 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2e975 20 20 20 78 3d 35 20 41 4e 44 20 7a 3c 31 30 0a     x=5 AND z<10.
2e976 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
2e977 20 20 20 20 20 20 4e 20 6d 61 79 20 62 65 20 7a        N may be z
2e978 65 72 6f 20 69 66 20 74 68 65 72 65 20 61 72 65  ero if there are
2e979 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73   inequality cons
2e97a 74 72 61 69 6e 74 73 2e 0a 20 20 20 20 2a 2a 20  traints..    ** 
2e97b 20 20 20 20 20 20 20 20 49 66 20 74 68 65 72 65          If there
2e97c 20 61 72 65 20 6e 6f 20 69 6e 65 71 75 61 6c 69   are no inequali
2e97d 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20  ty constraints, 
2e97e 74 68 65 6e 20 4e 20 69 73 20 61 74 0a 20 20 20  then N is at.   
2e97f 20 2a 2a 20 20 20 20 20 20 20 20 20 6c 65 61 73   **         leas
2e980 74 20 6f 6e 65 2e 0a 20 20 20 20 2a 2a 0a 20 20  t one..    **.  
2e981 20 20 2a 2a 20 20 20 20 20 20 20 20 20 54 68 69    **         Thi
2e982 73 20 63 61 73 65 20 69 73 20 61 6c 73 6f 20 75  s case is also u
2e983 73 65 64 20 77 68 65 6e 20 74 68 65 72 65 20 61  sed when there a
2e984 72 65 20 6e 6f 20 57 48 45 52 45 20 63 6c 61 75  re no WHERE clau
2e985 73 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  se.    **       
2e986 20 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 62 75    constraints bu
2e987 74 20 61 6e 20 69 6e 64 65 78 20 69 73 20 73 65  t an index is se
2e988 6c 65 63 74 65 64 20 61 6e 79 77 61 79 2c 20 69  lected anyway, i
2e989 6e 20 6f 72 64 65 72 0a 20 20 20 20 2a 2a 20 20  n order.    **  
2e98a 20 20 20 20 20 20 20 74 6f 20 66 6f 72 63 65 20         to force 
2e98b 74 68 65 20 6f 75 74 70 75 74 20 6f 72 64 65 72  the output order
2e98c 20 74 6f 20 63 6f 6e 66 6f 72 6d 20 74 6f 20 61   to conform to a
2e98d 6e 20 4f 52 44 45 52 20 42 59 2e 0a 20 20 20 20  n ORDER BY..    
2e98e 2a 2f 20 20 0a 20 20 20 20 69 6e 74 20 61 53 74  */  .    int aSt
2e98f 61 72 74 4f 70 5b 5d 20 3d 20 7b 0a 20 20 20 20  artOp[] = {.    
2e990 20 20 30 2c 0a 20 20 20 20 20 20 30 2c 0a 20 20    0,.      0,.  
2e991 20 20 20 20 4f 50 5f 52 65 77 69 6e 64 2c 20 20      OP_Rewind,  
2e992 20 20 20 20 20 20 20 20 20 2f 2a 20 32 3a 20 28           /* 2: (
2e993 21 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e  !start_constrain
2e994 74 73 20 26 26 20 73 74 61 72 74 45 71 20 26 26  ts && startEq &&
2e995 20 20 21 62 52 65 76 29 20 2a 2f 0a 20 20 20 20    !bRev) */.    
2e996 20 20 4f 50 5f 4c 61 73 74 2c 20 20 20 20 20 20    OP_Last,      
2e997 20 20 20 20 20 20 20 2f 2a 20 33 3a 20 28 21 73         /* 3: (!s
2e998 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73  tart_constraints
2e999 20 26 26 20 73 74 61 72 74 45 71 20 26 26 20 20   && startEq &&  
2e99a 20 62 52 65 76 29 20 2a 2f 0a 20 20 20 20 20 20   bRev) */.      
2e99b 4f 50 5f 53 65 65 6b 47 74 2c 20 20 20 20 20 20  OP_SeekGt,      
2e99c 20 20 20 20 20 2f 2a 20 34 3a 20 28 73 74 61 72       /* 4: (star
2e99d 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 26  t_constraints  &
2e99e 26 20 21 73 74 61 72 74 45 71 20 26 26 20 21 62  & !startEq && !b
2e99f 52 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50  Rev) */.      OP
2e9a0 5f 53 65 65 6b 4c 74 2c 20 20 20 20 20 20 20 20  _SeekLt,        
2e9a1 20 20 20 2f 2a 20 35 3a 20 28 73 74 61 72 74 5f     /* 5: (start_
2e9a2 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 26 26 20  constraints  && 
2e9a3 21 73 74 61 72 74 45 71 20 26 26 20 20 62 52 65  !startEq &&  bRe
2e9a4 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 53  v) */.      OP_S
2e9a5 65 65 6b 47 65 2c 20 20 20 20 20 20 20 20 20 20  eekGe,          
2e9a6 20 2f 2a 20 36 3a 20 28 73 74 61 72 74 5f 63 6f   /* 6: (start_co
2e9a7 6e 73 74 72 61 69 6e 74 73 20 20 26 26 20 20 73  nstraints  &&  s
2e9a8 74 61 72 74 45 71 20 26 26 20 21 62 52 65 76 29  tartEq && !bRev)
2e9a9 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 53 65 65   */.      OP_See
2e9aa 6b 4c 65 20 20 20 20 20 20 20 20 20 20 20 20 2f  kLe            /
2e9ab 2a 20 37 3a 20 28 73 74 61 72 74 5f 63 6f 6e 73  * 7: (start_cons
2e9ac 74 72 61 69 6e 74 73 20 20 26 26 20 20 73 74 61  traints  &&  sta
2e9ad 72 74 45 71 20 26 26 20 20 62 52 65 76 29 20 2a  rtEq &&  bRev) *
2e9ae 2f 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74  /.    };.    int
2e9af 20 61 45 6e 64 4f 70 5b 5d 20 3d 20 7b 0a 20 20   aEndOp[] = {.  
2e9b0 20 20 20 20 4f 50 5f 4e 6f 6f 70 2c 20 20 20 20      OP_Noop,    
2e9b1 20 20 20 20 20 20 20 20 20 2f 2a 20 30 3a 20 28           /* 0: (
2e9b2 21 65 6e 64 5f 63 6f 6e 73 74 72 61 69 6e 74 73  !end_constraints
2e9b3 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 49 64  ) */.      OP_Id
2e9b4 78 47 45 2c 20 20 20 20 20 20 20 20 20 20 20 20  xGE,            
2e9b5 2f 2a 20 31 3a 20 28 65 6e 64 5f 63 6f 6e 73 74  /* 1: (end_const
2e9b6 72 61 69 6e 74 73 20 26 26 20 21 62 52 65 76 29  raints && !bRev)
2e9b7 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 49 64 78   */.      OP_Idx
2e9b8 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  LT             /
2e9b9 2a 20 32 3a 20 28 65 6e 64 5f 63 6f 6e 73 74 72  * 2: (end_constr
2e9ba 61 69 6e 74 73 20 26 26 20 62 52 65 76 29 20 2a  aints && bRev) *
2e9bb 2f 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74  /.    };.    int
2e9bc 20 6e 45 71 20 3d 20 70 4c 65 76 65 6c 2d 3e 70   nEq = pLevel->p
2e9bd 6c 61 6e 2e 6e 45 71 3b 0a 20 20 20 20 69 6e 74  lan.nEq;.    int
2e9be 20 69 73 4d 69 6e 51 75 65 72 79 20 3d 20 30 3b   isMinQuery = 0;
2e9bf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20            /* If 
2e9c0 74 68 69 73 20 69 73 20 61 6e 20 6f 70 74 69 6d  this is an optim
2e9c1 69 7a 65 64 20 53 45 4c 45 43 54 20 6d 69 6e 28  ized SELECT min(
2e9c2 78 29 2e 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20  x).. */.    int 
2e9c3 72 65 67 42 61 73 65 3b 20 20 20 20 20 20 20 20  regBase;        
2e9c4 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65           /* Base
2e9c5 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   register holdin
2e9c6 67 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 61 6c  g constraint val
2e9c7 75 65 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72  ues */.    int r
2e9c8 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
2e9c9 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20          /* Temp 
2e9ca 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20  register */.    
2e9cb 57 68 65 72 65 54 65 72 6d 20 2a 70 52 61 6e 67  WhereTerm *pRang
2e9cc 65 53 74 61 72 74 20 3d 20 30 3b 20 20 2f 2a 20  eStart = 0;  /* 
2e9cd 49 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74  Inequality const
2e9ce 72 61 69 6e 74 20 61 74 20 72 61 6e 67 65 20 73  raint at range s
2e9cf 74 61 72 74 20 2a 2f 0a 20 20 20 20 57 68 65 72  tart */.    Wher
2e9d0 65 54 65 72 6d 20 2a 70 52 61 6e 67 65 45 6e 64  eTerm *pRangeEnd
2e9d1 20 3d 20 30 3b 20 20 20 20 2f 2a 20 49 6e 65 71   = 0;    /* Ineq
2e9d2 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e  uality constrain
2e9d3 74 20 61 74 20 72 61 6e 67 65 20 65 6e 64 20 2a  t at range end *
2e9d4 2f 0a 20 20 20 20 69 6e 74 20 73 74 61 72 74 45  /.    int startE
2e9d5 71 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  q;              
2e9d6 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72 61     /* True if ra
2e9d7 6e 67 65 20 73 74 61 72 74 20 75 73 65 73 20 3d  nge start uses =
2e9d8 3d 2c 20 3e 3d 20 6f 72 20 3c 3d 20 2a 2f 0a 20  =, >= or <= */. 
2e9d9 20 20 20 69 6e 74 20 65 6e 64 45 71 3b 20 20 20     int endEq;   
2e9da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e9db 2f 2a 20 54 72 75 65 20 69 66 20 72 61 6e 67 65  /* True if range
2e9dc 20 65 6e 64 20 75 73 65 73 20 3d 3d 2c 20 3e 3d   end uses ==, >=
2e9dd 20 6f 72 20 3c 3d 20 2a 2f 0a 20 20 20 20 69 6e   or <= */.    in
2e9de 74 20 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69  t start_constrai
2e9df 6e 74 73 3b 20 20 20 20 20 20 20 2f 2a 20 53 74  nts;       /* St
2e9e0 61 72 74 20 6f 66 20 72 61 6e 67 65 20 69 73 20  art of range is 
2e9e1 63 6f 6e 73 74 72 61 69 6e 65 64 20 2a 2f 0a 20  constrained */. 
2e9e2 20 20 20 69 6e 74 20 6e 43 6f 6e 73 74 72 61 69     int nConstrai
2e9e3 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nt;             
2e9e4 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6e  /* Number of con
2e9e5 73 74 72 61 69 6e 74 20 74 65 72 6d 73 20 2a 2f  straint terms */
2e9e6 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78  .    Index *pIdx
2e9e7 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ;         /* The
2e9e8 20 69 6e 64 65 78 20 77 65 20 77 69 6c 6c 20 62   index we will b
2e9e9 65 20 75 73 69 6e 67 20 2a 2f 0a 20 20 20 20 69  e using */.    i
2e9ea 6e 74 20 69 49 64 78 43 75 72 3b 20 20 20 20 20  nt iIdxCur;     
2e9eb 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20      /* The VDBE 
2e9ec 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20 69  cursor for the i
2e9ed 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ndex */.    int 
2e9ee 6e 45 78 74 72 61 52 65 67 20 3d 20 30 3b 20 20  nExtraReg = 0;  
2e9ef 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78   /* Number of ex
2e9f0 74 72 61 20 72 65 67 69 73 74 65 72 73 20 6e 65  tra registers ne
2e9f1 65 64 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20  eded */.    int 
2e9f2 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
2e9f3 20 2f 2a 20 49 6e 73 74 72 75 63 74 69 6f 6e 20   /* Instruction 
2e9f4 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 20 20 63 68  opcode */.    ch
2e9f5 61 72 20 2a 7a 41 66 66 3b 0a 0a 20 20 20 20 70  ar *zAff;..    p
2e9f6 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c  Idx = pLevel->pl
2e9f7 61 6e 2e 75 2e 70 49 64 78 3b 0a 20 20 20 20 69  an.u.pIdx;.    i
2e9f8 49 64 78 43 75 72 20 3d 20 70 4c 65 76 65 6c 2d  IdxCur = pLevel-
2e9f9 3e 69 49 64 78 43 75 72 3b 0a 20 20 20 20 6b 20  >iIdxCur;.    k 
2e9fa 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  = pIdx->aiColumn
2e9fb 5b 6e 45 71 5d 3b 20 20 20 20 20 2f 2a 20 43 6f  [nEq];     /* Co
2e9fc 6c 75 6d 6e 20 66 6f 72 20 69 6e 65 71 75 61 6c  lumn for inequal
2e9fd 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  ity constraints 
2e9fe 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  */..    /* If th
2e9ff 69 73 20 6c 6f 6f 70 20 73 61 74 69 73 66 69 65  is loop satisfie
2ea00 73 20 61 20 73 6f 72 74 20 6f 72 64 65 72 20 28  s a sort order (
2ea01 70 4f 72 64 65 72 42 79 29 20 72 65 71 75 65 73  pOrderBy) reques
2ea02 74 20 74 68 61 74 20 0a 20 20 20 20 2a 2a 20 77  t that .    ** w
2ea03 61 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 69  as passed to thi
2ea04 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6d  s function to im
2ea05 70 6c 65 6d 65 6e 74 20 61 20 22 53 45 4c 45 43  plement a "SELEC
2ea06 54 20 6d 69 6e 28 78 29 20 2e 2e 2e 22 20 0a 20  T min(x) ..." . 
2ea07 20 20 20 2a 2a 20 71 75 65 72 79 2c 20 74 68 65     ** query, the
2ea08 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 77 69 6c  n the caller wil
2ea09 6c 20 6f 6e 6c 79 20 61 6c 6c 6f 77 20 74 68 65  l only allow the
2ea0a 20 6c 6f 6f 70 20 74 6f 20 72 75 6e 20 66 6f 72   loop to run for
2ea0b 0a 20 20 20 20 2a 2a 20 61 20 73 69 6e 67 6c 65  .    ** a single
2ea0c 20 69 74 65 72 61 74 69 6f 6e 2e 20 54 68 69 73   iteration. This
2ea0d 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
2ea0e 66 69 72 73 74 20 72 6f 77 20 72 65 74 75 72 6e  first row return
2ea0f 65 64 0a 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64  ed.    ** should
2ea10 20 6e 6f 74 20 68 61 76 65 20 61 20 4e 55 4c 4c   not have a NULL
2ea11 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e   value stored in
2ea12 20 27 78 27 2e 20 49 66 20 63 6f 6c 75 6d 6e 20   'x'. If column 
2ea13 27 78 27 20 69 73 0a 20 20 20 20 2a 2a 20 74 68  'x' is.    ** th
2ea14 65 20 66 69 72 73 74 20 6f 6e 65 20 61 66 74 65  e first one afte
2ea15 72 20 74 68 65 20 6e 45 71 20 65 71 75 61 6c 69  r the nEq equali
2ea16 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 69  ty constraints i
2ea17 6e 20 74 68 65 20 69 6e 64 65 78 2c 0a 20 20 20  n the index,.   
2ea18 20 2a 2a 20 74 68 69 73 20 72 65 71 75 69 72 65   ** this require
2ea19 73 20 73 6f 6d 65 20 73 70 65 63 69 61 6c 20 68  s some special h
2ea1a 61 6e 64 6c 69 6e 67 2e 0a 20 20 20 20 2a 2f 0a  andling..    */.
2ea1b 20 20 20 20 69 66 28 20 28 77 63 74 72 6c 46 6c      if( (wctrlFl
2ea1c 61 67 73 26 57 48 45 52 45 5f 4f 52 44 45 52 42  ags&WHERE_ORDERB
2ea1d 59 5f 4d 49 4e 29 21 3d 30 0a 20 20 20 20 20 26  Y_MIN)!=0.     &
2ea1e 26 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  & (pLevel->plan.
2ea1f 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 4f 52  wsFlags&WHERE_OR
2ea20 44 45 52 42 59 29 0a 20 20 20 20 20 26 26 20 28  DERBY).     && (
2ea21 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 6e 45  pIdx->nColumn>nE
2ea22 71 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  q).    ){.      
2ea23 2f 2a 20 61 73 73 65 72 74 28 20 70 4f 72 64 65  /* assert( pOrde
2ea24 72 42 79 2d 3e 6e 45 78 70 72 3d 3d 31 20 29 3b  rBy->nExpr==1 );
2ea25 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 61 73 73   */.      /* ass
2ea26 65 72 74 28 20 70 4f 72 64 65 72 42 79 2d 3e 61  ert( pOrderBy->a
2ea27 5b 30 5d 2e 70 45 78 70 72 2d 3e 69 43 6f 6c 75  [0].pExpr->iColu
2ea28 6d 6e 3d 3d 70 49 64 78 2d 3e 61 69 43 6f 6c 75  mn==pIdx->aiColu
2ea29 6d 6e 5b 6e 45 71 5d 20 29 3b 20 2a 2f 0a 20 20  mn[nEq] ); */.  
2ea2a 20 20 20 20 69 73 4d 69 6e 51 75 65 72 79 20 3d      isMinQuery =
2ea2b 20 31 3b 0a 20 20 20 20 20 20 6e 45 78 74 72 61   1;.      nExtra
2ea2c 52 65 67 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a  Reg = 1;.    }..
2ea2d 20 20 20 20 2f 2a 20 46 69 6e 64 20 61 6e 79 20      /* Find any 
2ea2e 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74  inequality const
2ea2f 72 61 69 6e 74 20 74 65 72 6d 73 20 66 6f 72 20  raint terms for 
2ea30 74 68 65 20 73 74 61 72 74 20 61 6e 64 20 65 6e  the start and en
2ea31 64 20 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65  d .    ** of the
2ea32 20 72 61 6e 67 65 2e 20 0a 20 20 20 20 2a 2f 0a   range. .    */.
2ea33 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e      if( pLevel->
2ea34 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
2ea35 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 20 29  HERE_TOP_LIMIT )
2ea36 7b 0a 20 20 20 20 20 20 70 52 61 6e 67 65 45 6e  {.      pRangeEn
2ea37 64 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43  d = findTerm(pWC
2ea38 2c 20 69 43 75 72 2c 20 6b 2c 20 6e 6f 74 52 65  , iCur, k, notRe
2ea39 61 64 79 2c 20 28 57 4f 5f 4c 54 7c 57 4f 5f 4c  ady, (WO_LT|WO_L
2ea3a 45 29 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20  E), pIdx);.     
2ea3b 20 6e 45 78 74 72 61 52 65 67 20 3d 20 31 3b 0a   nExtraReg = 1;.
2ea3c 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c      }.    if( pL
2ea3d 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
2ea3e 67 73 20 26 20 57 48 45 52 45 5f 42 54 4d 5f 4c  gs & WHERE_BTM_L
2ea3f 49 4d 49 54 20 29 7b 0a 20 20 20 20 20 20 70 52  IMIT ){.      pR
2ea40 61 6e 67 65 53 74 61 72 74 20 3d 20 66 69 6e 64  angeStart = find
2ea41 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20  Term(pWC, iCur, 
2ea42 6b 2c 20 6e 6f 74 52 65 61 64 79 2c 20 28 57 4f  k, notReady, (WO
2ea43 5f 47 54 7c 57 4f 5f 47 45 29 2c 20 70 49 64 78  _GT|WO_GE), pIdx
2ea44 29 3b 0a 20 20 20 20 20 20 6e 45 78 74 72 61 52  );.      nExtraR
2ea45 65 67 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20  eg = 1;.    }.. 
2ea46 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63     /* Generate c
2ea47 6f 64 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20  ode to evaluate 
2ea48 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 20 74  all constraint t
2ea49 65 72 6d 73 20 75 73 69 6e 67 20 3d 3d 20 6f 72  erms using == or
2ea4a 20 49 4e 0a 20 20 20 20 2a 2a 20 61 6e 64 20 73   IN.    ** and s
2ea4b 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 73 20  tore the values 
2ea4c 6f 66 20 74 68 6f 73 65 20 74 65 72 6d 73 20 69  of those terms i
2ea4d 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 72 65  n an array of re
2ea4e 67 69 73 74 65 72 73 0a 20 20 20 20 2a 2a 20 73  gisters.    ** s
2ea4f 74 61 72 74 69 6e 67 20 61 74 20 72 65 67 42 61  tarting at regBa
2ea50 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  se..    */.    r
2ea51 65 67 42 61 73 65 20 3d 20 63 6f 64 65 41 6c 6c  egBase = codeAll
2ea52 45 71 75 61 6c 69 74 79 54 65 72 6d 73 28 0a 20  EqualityTerms(. 
2ea53 20 20 20 20 20 20 20 70 50 61 72 73 65 2c 20 70         pParse, p
2ea54 4c 65 76 65 6c 2c 20 70 57 43 2c 20 6e 6f 74 52  Level, pWC, notR
2ea55 65 61 64 79 2c 20 6e 45 78 74 72 61 52 65 67 2c  eady, nExtraReg,
2ea56 20 26 7a 41 66 66 0a 20 20 20 20 29 3b 0a 20 20   &zAff.    );.  
2ea57 20 20 61 64 64 72 4e 78 74 20 3d 20 70 4c 65 76    addrNxt = pLev
2ea58 65 6c 2d 3e 61 64 64 72 4e 78 74 3b 0a 0a 20 20  el->addrNxt;..  
2ea59 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 64    /* If we are d
2ea5a 6f 69 6e 67 20 61 20 72 65 76 65 72 73 65 20 6f  oing a reverse o
2ea5b 72 64 65 72 20 73 63 61 6e 20 6f 6e 20 61 6e 20  rder scan on an 
2ea5c 61 73 63 65 6e 64 69 6e 67 20 69 6e 64 65 78 2c  ascending index,
2ea5d 20 6f 72 0a 20 20 20 20 2a 2a 20 61 20 66 6f 72   or.    ** a for
2ea5e 77 61 72 64 20 6f 72 64 65 72 20 73 63 61 6e 20  ward order scan 
2ea5f 6f 6e 20 61 20 64 65 73 63 65 6e 64 69 6e 67 20  on a descending 
2ea60 69 6e 64 65 78 2c 20 69 6e 74 65 72 63 68 61 6e  index, interchan
2ea61 67 65 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 73  ge the .    ** s
2ea62 74 61 72 74 20 61 6e 64 20 65 6e 64 20 74 65 72  tart and end ter
2ea63 6d 73 20 28 70 52 61 6e 67 65 53 74 61 72 74 20  ms (pRangeStart 
2ea64 61 6e 64 20 70 52 61 6e 67 65 45 6e 64 29 2e 0a  and pRangeEnd)..
2ea65 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 62      */.    if( b
2ea66 52 65 76 3d 3d 28 70 49 64 78 2d 3e 61 53 6f 72  Rev==(pIdx->aSor
2ea67 74 4f 72 64 65 72 5b 6e 45 71 5d 3d 3d 53 51 4c  tOrder[nEq]==SQL
2ea68 49 54 45 5f 53 4f 5f 41 53 43 29 20 29 7b 0a 20  ITE_SO_ASC) ){. 
2ea69 20 20 20 20 20 53 57 41 50 28 57 68 65 72 65 54       SWAP(WhereT
2ea6a 65 72 6d 20 2a 2c 20 70 52 61 6e 67 65 45 6e 64  erm *, pRangeEnd
2ea6b 2c 20 70 52 61 6e 67 65 53 74 61 72 74 29 3b 0a  , pRangeStart);.
2ea6c 20 20 20 20 7d 0a 0a 20 20 20 20 74 65 73 74 63      }..    testc
2ea6d 61 73 65 28 20 70 52 61 6e 67 65 53 74 61 72 74  ase( pRangeStart
2ea6e 20 26 26 20 70 52 61 6e 67 65 53 74 61 72 74 2d   && pRangeStart-
2ea6f 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f  >eOperator & WO_
2ea70 4c 45 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  LE );.    testca
2ea71 73 65 28 20 70 52 61 6e 67 65 53 74 61 72 74 20  se( pRangeStart 
2ea72 26 26 20 70 52 61 6e 67 65 53 74 61 72 74 2d 3e  && pRangeStart->
2ea73 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 47  eOperator & WO_G
2ea74 45 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  E );.    testcas
2ea75 65 28 20 70 52 61 6e 67 65 45 6e 64 20 26 26 20  e( pRangeEnd && 
2ea76 70 52 61 6e 67 65 45 6e 64 2d 3e 65 4f 70 65 72  pRangeEnd->eOper
2ea77 61 74 6f 72 20 26 20 57 4f 5f 4c 45 20 29 3b 0a  ator & WO_LE );.
2ea78 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 52      testcase( pR
2ea79 61 6e 67 65 45 6e 64 20 26 26 20 70 52 61 6e 67  angeEnd && pRang
2ea7a 65 45 6e 64 2d 3e 65 4f 70 65 72 61 74 6f 72 20  eEnd->eOperator 
2ea7b 26 20 57 4f 5f 47 45 20 29 3b 0a 20 20 20 20 73  & WO_GE );.    s
2ea7c 74 61 72 74 45 71 20 3d 20 21 70 52 61 6e 67 65  tartEq = !pRange
2ea7d 53 74 61 72 74 20 7c 7c 20 70 52 61 6e 67 65 53  Start || pRangeS
2ea7e 74 61 72 74 2d 3e 65 4f 70 65 72 61 74 6f 72 20  tart->eOperator 
2ea7f 26 20 28 57 4f 5f 4c 45 7c 57 4f 5f 47 45 29 3b  & (WO_LE|WO_GE);
2ea80 0a 20 20 20 20 65 6e 64 45 71 20 3d 20 20 20 21  .    endEq =   !
2ea81 70 52 61 6e 67 65 45 6e 64 20 7c 7c 20 70 52 61  pRangeEnd || pRa
2ea82 6e 67 65 45 6e 64 2d 3e 65 4f 70 65 72 61 74 6f  ngeEnd->eOperato
2ea83 72 20 26 20 28 57 4f 5f 4c 45 7c 57 4f 5f 47 45  r & (WO_LE|WO_GE
2ea84 29 3b 0a 20 20 20 20 73 74 61 72 74 5f 63 6f 6e  );.    start_con
2ea85 73 74 72 61 69 6e 74 73 20 3d 20 70 52 61 6e 67  straints = pRang
2ea86 65 53 74 61 72 74 20 7c 7c 20 6e 45 71 3e 30 3b  eStart || nEq>0;
2ea87 0a 0a 20 20 20 20 2f 2a 20 53 65 65 6b 20 74 68  ..    /* Seek th
2ea88 65 20 69 6e 64 65 78 20 63 75 72 73 6f 72 20 74  e index cursor t
2ea89 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  o the start of t
2ea8a 68 65 20 72 61 6e 67 65 2e 20 2a 2f 0a 20 20 20  he range. */.   
2ea8b 20 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 6e   nConstraint = n
2ea8c 45 71 3b 0a 20 20 20 20 69 66 28 20 70 52 61 6e  Eq;.    if( pRan
2ea8d 67 65 53 74 61 72 74 20 29 7b 0a 20 20 20 20 20  geStart ){.     
2ea8e 20 45 78 70 72 20 2a 70 52 69 67 68 74 20 3d 20   Expr *pRight = 
2ea8f 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 70 45 78  pRangeStart->pEx
2ea90 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20  pr->pRight;.    
2ea91 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
2ea92 65 28 70 50 61 72 73 65 2c 20 70 52 69 67 68 74  e(pParse, pRight
2ea93 2c 20 72 65 67 42 61 73 65 2b 6e 45 71 29 3b 0a  , regBase+nEq);.
2ea94 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2ea95 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 73  eAddOp2(v, OP_Is
2ea96 4e 75 6c 6c 2c 20 72 65 67 42 61 73 65 2b 6e 45  Null, regBase+nE
2ea97 71 2c 20 61 64 64 72 4e 78 74 29 3b 0a 20 20 20  q, addrNxt);.   
2ea98 20 20 20 69 66 28 20 7a 41 66 66 20 0a 20 20 20     if( zAff .   
2ea99 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 43 6f      && sqlite3Co
2ea9a 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 52  mpareAffinity(pR
2ea9b 69 67 68 74 2c 20 7a 41 66 66 5b 6e 43 6f 6e 73  ight, zAff[nCons
2ea9c 74 72 61 69 6e 74 5d 29 3d 3d 53 51 4c 49 54 45  traint])==SQLITE
2ea9d 5f 41 46 46 5f 4e 4f 4e 45 0a 20 20 20 20 20 20  _AFF_NONE.      
2ea9e 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 69  ){.        /* Si
2ea9f 6e 63 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73  nce the comparis
2eaa0 6f 6e 20 69 73 20 74 6f 20 62 65 20 70 65 72 66  on is to be perf
2eaa1 6f 72 6d 65 64 20 77 69 74 68 20 6e 6f 20 63 6f  ormed with no co
2eaa2 6e 76 65 72 73 69 6f 6e 73 20 61 70 70 6c 69 65  nversions applie
2eaa3 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20  d.        ** to 
2eaa4 74 68 65 20 6f 70 65 72 61 6e 64 73 2c 20 73 65  the operands, se
2eaa5 74 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 74  t the affinity t
2eaa6 6f 20 61 70 70 6c 79 20 74 6f 20 70 52 69 67 68  o apply to pRigh
2eaa7 74 20 74 6f 20 0a 20 20 20 20 20 20 20 20 2a 2a  t to .        **
2eaa8 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45   SQLITE_AFF_NONE
2eaa9 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 7a 41  .  */.        zA
2eaaa 66 66 5b 6e 43 6f 6e 73 74 72 61 69 6e 74 5d 20  ff[nConstraint] 
2eaab 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  = SQLITE_AFF_NON
2eaac 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  E;.      }.     
2eaad 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b 0a   nConstraint++;.
2eaae 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73      }else if( is
2eaaf 4d 69 6e 51 75 65 72 79 20 29 7b 0a 20 20 20 20  MinQuery ){.    
2eab0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2eab1 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
2eab2 30 2c 20 72 65 67 42 61 73 65 2b 6e 45 71 29 3b  0, regBase+nEq);
2eab3 0a 20 20 20 20 20 20 6e 43 6f 6e 73 74 72 61 69  .      nConstrai
2eab4 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 73 74 61 72  nt++;.      star
2eab5 74 45 71 20 3d 20 30 3b 0a 20 20 20 20 20 20 73  tEq = 0;.      s
2eab6 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73  tart_constraints
2eab7 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 1;.    }.    
2eab8 63 6f 64 65 41 70 70 6c 79 41 66 66 69 6e 69 74  codeApplyAffinit
2eab9 79 28 70 50 61 72 73 65 2c 20 72 65 67 42 61 73  y(pParse, regBas
2eaba 65 2c 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2c 20  e, nConstraint, 
2eabb 7a 41 66 66 29 3b 0a 20 20 20 20 6f 70 20 3d 20  zAff);.    op = 
2eabc 61 53 74 61 72 74 4f 70 5b 28 73 74 61 72 74 5f  aStartOp[(start_
2eabd 63 6f 6e 73 74 72 61 69 6e 74 73 3c 3c 32 29 20  constraints<<2) 
2eabe 2b 20 28 73 74 61 72 74 45 71 3c 3c 31 29 20 2b  + (startEq<<1) +
2eabf 20 62 52 65 76 5d 3b 0a 20 20 20 20 61 73 73 65   bRev];.    asse
2eac0 72 74 28 20 6f 70 21 3d 30 20 29 3b 0a 20 20 20  rt( op!=0 );.   
2eac1 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f   testcase( op==O
2eac2 50 5f 52 65 77 69 6e 64 20 29 3b 0a 20 20 20 20  P_Rewind );.    
2eac3 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50  testcase( op==OP
2eac4 5f 4c 61 73 74 20 29 3b 0a 20 20 20 20 74 65 73  _Last );.    tes
2eac5 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 53 65  tcase( op==OP_Se
2eac6 65 6b 47 74 20 29 3b 0a 20 20 20 20 74 65 73 74  ekGt );.    test
2eac7 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 53 65 65  case( op==OP_See
2eac8 6b 47 65 20 29 3b 0a 20 20 20 20 74 65 73 74 63  kGe );.    testc
2eac9 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 53 65 65 6b  ase( op==OP_Seek
2eaca 4c 65 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  Le );.    testca
2eacb 73 65 28 20 6f 70 3d 3d 4f 50 5f 53 65 65 6b 4c  se( op==OP_SeekL
2eacc 74 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  t );.    sqlite3
2eacd 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 6f 70  VdbeAddOp4(v, op
2eace 2c 20 69 49 64 78 43 75 72 2c 20 61 64 64 72 4e  , iIdxCur, addrN
2eacf 78 74 2c 20 72 65 67 42 61 73 65 2c 20 0a 20 20  xt, regBase, .  
2ead0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ead1 20 20 20 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54      SQLITE_INT_T
2ead2 4f 5f 50 54 52 28 6e 43 6f 6e 73 74 72 61 69 6e  O_PTR(nConstrain
2ead3 74 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 0a  t), P4_INT32);..
2ead4 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20      /* Load the 
2ead5 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 69 6e  value for the in
2ead6 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
2ead7 69 6e 74 20 61 74 20 74 68 65 20 65 6e 64 20 6f  int at the end o
2ead8 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 61 6e  f the.    ** ran
2ead9 67 65 20 28 69 66 20 61 6e 79 29 2e 0a 20 20 20  ge (if any)..   
2eada 20 2a 2f 0a 20 20 20 20 6e 43 6f 6e 73 74 72 61   */.    nConstra
2eadb 69 6e 74 20 3d 20 6e 45 71 3b 0a 20 20 20 20 69  int = nEq;.    i
2eadc 66 28 20 70 52 61 6e 67 65 45 6e 64 20 29 7b 0a  f( pRangeEnd ){.
2eadd 20 20 20 20 20 20 45 78 70 72 20 2a 70 52 69 67        Expr *pRig
2eade 68 74 20 3d 20 70 52 61 6e 67 65 45 6e 64 2d 3e  ht = pRangeEnd->
2eadf 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20  pExpr->pRight;. 
2eae0 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2eae1 43 61 63 68 65 52 65 6d 6f 76 65 28 70 50 61 72  CacheRemove(pPar
2eae2 73 65 2c 20 72 65 67 42 61 73 65 2b 6e 45 71 29  se, regBase+nEq)
2eae3 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
2eae4 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
2eae5 70 52 69 67 68 74 2c 20 72 65 67 42 61 73 65 2b  pRight, regBase+
2eae6 6e 45 71 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  nEq);.      sqli
2eae7 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2eae8 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 42   OP_IsNull, regB
2eae9 61 73 65 2b 6e 45 71 2c 20 61 64 64 72 4e 78 74  ase+nEq, addrNxt
2eaea 29 3b 0a 20 20 20 20 20 20 7a 41 66 66 20 3d 20  );.      zAff = 
2eaeb 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
2eaec 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 41 66 66  pParse->db, zAff
2eaed 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 41 66  );.      if( zAf
2eaee 66 20 0a 20 20 20 20 20 20 20 26 26 20 73 71 6c  f .       && sql
2eaef 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e  ite3CompareAffin
2eaf0 69 74 79 28 70 52 69 67 68 74 2c 20 7a 41 66 66  ity(pRight, zAff
2eaf1 5b 6e 43 6f 6e 73 74 72 61 69 6e 74 5d 29 3d 3d  [nConstraint])==
2eaf2 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 0a  SQLITE_AFF_NONE.
2eaf3 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
2eaf4 20 2f 2a 20 53 69 6e 63 65 20 74 68 65 20 63 6f   /* Since the co
2eaf5 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 6f 20 62  mparison is to b
2eaf6 65 20 70 65 72 66 6f 72 6d 65 64 20 77 69 74 68  e performed with
2eaf7 20 6e 6f 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20   no conversions 
2eaf8 61 70 70 6c 69 65 64 0a 20 20 20 20 20 20 20 20  applied.        
2eaf9 2a 2a 20 74 6f 20 74 68 65 20 6f 70 65 72 61 6e  ** to the operan
2eafa 64 73 2c 20 73 65 74 20 74 68 65 20 61 66 66 69  ds, set the affi
2eafb 6e 69 74 79 20 74 6f 20 61 70 70 6c 79 20 74 6f  nity to apply to
2eafc 20 70 52 69 67 68 74 20 74 6f 20 0a 20 20 20 20   pRight to .    
2eafd 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 41 46      ** SQLITE_AF
2eafe 46 5f 4e 4f 4e 45 2e 20 20 2a 2f 0a 20 20 20 20  F_NONE.  */.    
2eaff 20 20 20 20 7a 41 66 66 5b 6e 43 6f 6e 73 74 72      zAff[nConstr
2eb00 61 69 6e 74 5d 20 3d 20 53 51 4c 49 54 45 5f 41  aint] = SQLITE_A
2eb01 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 7d  FF_NONE;.      }
2eb02 0a 20 20 20 20 20 20 63 6f 64 65 41 70 70 6c 79  .      codeApply
2eb03 41 66 66 69 6e 69 74 79 28 70 50 61 72 73 65 2c  Affinity(pParse,
2eb04 20 72 65 67 42 61 73 65 2c 20 6e 45 71 2b 31 2c   regBase, nEq+1,
2eb05 20 7a 41 66 66 29 3b 0a 20 20 20 20 20 20 6e 43   zAff);.      nC
2eb06 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b 0a 20 20 20  onstraint++;.   
2eb07 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 6f 70 20 6f   }..    /* Top o
2eb08 66 20 74 68 65 20 6c 6f 6f 70 20 62 6f 64 79 20  f the loop body 
2eb09 2a 2f 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70  */.    pLevel->p
2eb0a 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  2 = sqlite3VdbeC
2eb0b 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 0a  urrentAddr(v);..
2eb0c 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20      /* Check if 
2eb0d 74 68 65 20 69 6e 64 65 78 20 63 75 72 73 6f 72  the index cursor
2eb0e 20 69 73 20 70 61 73 74 20 74 68 65 20 65 6e 64   is past the end
2eb0f 20 6f 66 20 74 68 65 20 72 61 6e 67 65 2e 20 2a   of the range. *
2eb10 2f 0a 20 20 20 20 6f 70 20 3d 20 61 45 6e 64 4f  /.    op = aEndO
2eb11 70 5b 28 70 52 61 6e 67 65 45 6e 64 20 7c 7c 20  p[(pRangeEnd || 
2eb12 6e 45 71 29 20 2a 20 28 31 20 2b 20 62 52 65 76  nEq) * (1 + bRev
2eb13 29 5d 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65  )];.    testcase
2eb14 28 20 6f 70 3d 3d 4f 50 5f 4e 6f 6f 70 20 29 3b  ( op==OP_Noop );
2eb15 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f  .    testcase( o
2eb16 70 3d 3d 4f 50 5f 49 64 78 47 45 20 29 3b 0a 20  p==OP_IdxGE );. 
2eb17 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
2eb18 3d 4f 50 5f 49 64 78 4c 54 20 29 3b 0a 20 20 20  =OP_IdxLT );.   
2eb19 20 69 66 28 20 6f 70 21 3d 4f 50 5f 4e 6f 6f 70   if( op!=OP_Noop
2eb1a 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2eb1b 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 6f  3VdbeAddOp4(v, o
2eb1c 70 2c 20 69 49 64 78 43 75 72 2c 20 61 64 64 72  p, iIdxCur, addr
2eb1d 4e 78 74 2c 20 72 65 67 42 61 73 65 2c 0a 20 20  Nxt, regBase,.  
2eb1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eb1f 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4e 54        SQLITE_INT
2eb20 5f 54 4f 5f 50 54 52 28 6e 43 6f 6e 73 74 72 61  _TO_PTR(nConstra
2eb21 69 6e 74 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b  int), P4_INT32);
2eb22 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2eb23 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 65 6e  beChangeP5(v, en
2eb24 64 45 71 21 3d 62 52 65 76 20 3f 31 3a 30 29 3b  dEq!=bRev ?1:0);
2eb25 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49  .    }..    /* I
2eb26 66 20 74 68 65 72 65 20 61 72 65 20 69 6e 65 71  f there are ineq
2eb27 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e  uality constrain
2eb28 74 73 2c 20 63 68 65 63 6b 20 74 68 61 74 20 74  ts, check that t
2eb29 68 65 20 76 61 6c 75 65 0a 20 20 20 20 2a 2a 20  he value.    ** 
2eb2a 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c  of the table col
2eb2b 75 6d 6e 20 74 68 61 74 20 74 68 65 20 69 6e 65  umn that the ine
2eb2c 71 75 61 6c 69 74 79 20 63 6f 6e 74 72 61 69 6e  quality contrain
2eb2d 73 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e 0a 20  s is not NULL.. 
2eb2e 20 20 20 2a 2a 20 49 66 20 69 74 20 69 73 2c 20     ** If it is, 
2eb2f 6a 75 6d 70 20 74 6f 20 74 68 65 20 6e 65 78 74  jump to the next
2eb30 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68   iteration of th
2eb31 65 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20  e loop..    */. 
2eb32 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47     r1 = sqlite3G
2eb33 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
2eb34 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
2eb35 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73   pLevel->plan.ws
2eb36 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 42 54  Flags & WHERE_BT
2eb37 4d 5f 4c 49 4d 49 54 20 29 3b 0a 20 20 20 20 74  M_LIMIT );.    t
2eb38 65 73 74 63 61 73 65 28 20 70 4c 65 76 65 6c 2d  estcase( pLevel-
2eb39 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
2eb3a 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 20  WHERE_TOP_LIMIT 
2eb3b 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65  );.    if( pLeve
2eb3c 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
2eb3d 26 20 28 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d  & (WHERE_BTM_LIM
2eb3e 49 54 7c 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d  IT|WHERE_TOP_LIM
2eb3f 49 54 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  IT) ){.      sql
2eb40 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2eb41 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 49 64  , OP_Column, iId
2eb42 78 43 75 72 2c 20 6e 45 71 2c 20 72 31 29 3b 0a  xCur, nEq, r1);.
2eb43 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2eb44 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 73  eAddOp2(v, OP_Is
2eb45 4e 75 6c 6c 2c 20 72 31 2c 20 61 64 64 72 43 6f  Null, r1, addrCo
2eb46 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  nt);.    }.    s
2eb47 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
2eb48 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29  pReg(pParse, r1)
2eb49 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 65 6b 20 74  ;..    /* Seek t
2eb4a 68 65 20 74 61 62 6c 65 20 63 75 72 73 6f 72 2c  he table cursor,
2eb4b 20 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a   if required */.
2eb4c 20 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28      disableTerm(
2eb4d 70 4c 65 76 65 6c 2c 20 70 52 61 6e 67 65 53 74  pLevel, pRangeSt
2eb4e 61 72 74 29 3b 0a 20 20 20 20 64 69 73 61 62 6c  art);.    disabl
2eb4f 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 52  eTerm(pLevel, pR
2eb50 61 6e 67 65 45 6e 64 29 3b 0a 20 20 20 20 69 66  angeEnd);.    if
2eb51 28 20 21 6f 6d 69 74 54 61 62 6c 65 20 29 7b 0a  ( !omitTable ){.
2eb52 20 20 20 20 20 20 69 52 6f 77 69 64 52 65 67 20        iRowidReg 
2eb53 3d 20 69 52 65 6c 65 61 73 65 52 65 67 20 3d 20  = iReleaseReg = 
2eb54 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
2eb55 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  g(pParse);.     
2eb56 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2eb57 70 32 28 76 2c 20 4f 50 5f 49 64 78 52 6f 77 69  p2(v, OP_IdxRowi
2eb58 64 2c 20 69 49 64 78 43 75 72 2c 20 69 52 6f 77  d, iIdxCur, iRow
2eb59 69 64 52 65 67 29 3b 0a 20 20 20 20 20 20 73 71  idReg);.      sq
2eb5a 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 53 74  lite3ExprCacheSt
2eb5b 6f 72 65 28 70 50 61 72 73 65 2c 20 69 43 75 72  ore(pParse, iCur
2eb5c 2c 20 2d 31 2c 20 69 52 6f 77 69 64 52 65 67 29  , -1, iRowidReg)
2eb5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2eb5e 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2eb5f 53 65 65 6b 2c 20 69 43 75 72 2c 20 69 52 6f 77  Seek, iCur, iRow
2eb60 69 64 52 65 67 29 3b 20 20 2f 2a 20 44 65 66 65  idReg);  /* Defe
2eb61 72 72 65 64 20 73 65 65 6b 20 2a 2f 0a 20 20 20  rred seek */.   
2eb62 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 63 6f 72   }..    /* Recor
2eb63 64 20 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f  d the instructio
2eb64 6e 20 75 73 65 64 20 74 6f 20 74 65 72 6d 69 6e  n used to termin
2eb65 61 74 65 20 74 68 65 20 6c 6f 6f 70 2e 20 44 69  ate the loop. Di
2eb66 73 61 62 6c 65 20 0a 20 20 20 20 2a 2a 20 57 48  sable .    ** WH
2eb67 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73  ERE clause terms
2eb68 20 6d 61 64 65 20 72 65 64 75 6e 64 61 6e 74 20   made redundant 
2eb69 62 79 20 74 68 65 20 69 6e 64 65 78 20 72 61 6e  by the index ran
2eb6a 67 65 20 73 63 61 6e 2e 0a 20 20 20 20 2a 2f 0a  ge scan..    */.
2eb6b 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d      pLevel->op =
2eb6c 20 62 52 65 76 20 3f 20 4f 50 5f 50 72 65 76 20   bRev ? OP_Prev 
2eb6d 3a 20 4f 50 5f 4e 65 78 74 3b 0a 20 20 20 20 70  : OP_Next;.    p
2eb6e 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 69 49 64 78  Level->p1 = iIdx
2eb6f 43 75 72 3b 0a 20 20 7d 65 6c 73 65 0a 0a 23 69  Cur;.  }else..#i
2eb70 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2eb71 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  T_OR_OPTIMIZATIO
2eb72 4e 0a 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e  N.  if( pLevel->
2eb73 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
2eb74 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20 29 7b  HERE_MULTI_OR ){
2eb75 0a 20 20 20 20 2f 2a 20 43 61 73 65 20 34 3a 20  .    /* Case 4: 
2eb76 20 54 77 6f 20 6f 72 20 6d 6f 72 65 20 73 65 70   Two or more sep
2eb77 61 72 61 74 65 6c 79 20 69 6e 64 65 78 65 64 20  arately indexed 
2eb78 74 65 72 6d 73 20 63 6f 6e 6e 65 63 74 65 64 20  terms connected 
2eb79 62 79 20 4f 52 0a 20 20 20 20 2a 2a 0a 20 20 20  by OR.    **.   
2eb7a 20 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 20 20 20   ** Example:.   
2eb7b 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 43 52 45   **.    **   CRE
2eb7c 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 62  ATE TABLE t1(a,b
2eb7d 2c 63 2c 64 29 3b 0a 20 20 20 20 2a 2a 20 20 20  ,c,d);.    **   
2eb7e 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 31 20  CREATE INDEX i1 
2eb7f 4f 4e 20 74 31 28 61 29 3b 0a 20 20 20 20 2a 2a  ON t1(a);.    **
2eb80 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 20     CREATE INDEX 
2eb81 69 32 20 4f 4e 20 74 31 28 62 29 3b 0a 20 20 20  i2 ON t1(b);.   
2eb82 20 2a 2a 20 20 20 43 52 45 41 54 45 20 49 4e 44   **   CREATE IND
2eb83 45 58 20 69 33 20 4f 4e 20 74 31 28 63 29 3b 0a  EX i3 ON t1(c);.
2eb84 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
2eb85 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31  SELECT * FROM t1
2eb86 20 57 48 45 52 45 20 61 3d 35 20 4f 52 20 62 3d   WHERE a=5 OR b=
2eb87 37 20 4f 52 20 28 63 3d 31 31 20 41 4e 44 20 64  7 OR (c=11 AND d
2eb88 3d 31 33 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20  =13).    **.    
2eb89 2a 2a 20 49 6e 20 74 68 65 20 65 78 61 6d 70 6c  ** In the exampl
2eb8a 65 2c 20 74 68 65 72 65 20 61 72 65 20 74 68 72  e, there are thr
2eb8b 65 65 20 69 6e 64 65 78 65 64 20 74 65 72 6d 73  ee indexed terms
2eb8c 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 4f 52   connected by OR
2eb8d 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 74 6f 70  ..    ** The top
2eb8e 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 6c 6f 6f   of the loop loo
2eb8f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20 20  ks like this:.  
2eb90 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20    **.    **     
2eb91 20 20 20 20 20 4e 75 6c 6c 20 20 20 20 20 20 20       Null       
2eb92 31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1               
2eb93 20 23 20 5a 65 72 6f 20 74 68 65 20 72 6f 77 73   # Zero the rows
2eb94 65 74 20 69 6e 20 72 65 67 20 31 0a 20 20 20 20  et in reg 1.    
2eb95 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 6e 2c 20  **.    ** Then, 
2eb96 66 6f 72 20 65 61 63 68 20 69 6e 64 65 78 65 64  for each indexed
2eb97 20 74 65 72 6d 2c 20 74 68 65 20 66 6f 6c 6c 6f   term, the follo
2eb98 77 69 6e 67 2e 20 54 68 65 20 61 72 67 75 6d 65  wing. The argume
2eb99 6e 74 73 20 74 6f 0a 20 20 20 20 2a 2a 20 52 6f  nts to.    ** Ro
2eb9a 77 53 65 74 54 65 73 74 20 61 72 65 20 73 75 63  wSetTest are suc
2eb9b 68 20 74 68 61 74 20 74 68 65 20 72 6f 77 69 64  h that the rowid
2eb9c 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   of the current 
2eb9d 72 6f 77 20 69 73 20 69 6e 73 65 72 74 65 64 0a  row is inserted.
2eb9e 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20      ** into the 
2eb9f 52 6f 77 53 65 74 2e 20 49 66 20 69 74 20 69 73  RowSet. If it is
2eba0 20 61 6c 72 65 61 64 79 20 70 72 65 73 65 6e 74   already present
2eba1 2c 20 63 6f 6e 74 72 6f 6c 20 73 6b 69 70 73 20  , control skips 
2eba2 74 68 65 0a 20 20 20 20 2a 2a 20 47 6f 73 75 62  the.    ** Gosub
2eba3 20 6f 70 63 6f 64 65 20 61 6e 64 20 6a 75 6d 70   opcode and jump
2eba4 73 20 73 74 72 61 69 67 68 74 20 74 6f 20 74 68  s straight to th
2eba5 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64  e code generated
2eba6 20 62 79 20 57 68 65 72 65 45 6e 64 28 29 2e 0a   by WhereEnd()..
2eba7 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
2eba8 20 20 20 20 20 73 71 6c 69 74 65 33 57 68 65 72       sqlite3Wher
2eba9 65 42 65 67 69 6e 28 3c 74 65 72 6d 3e 29 0a 20  eBegin(<term>). 
2ebaa 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 52     **          R
2ebab 6f 77 53 65 74 54 65 73 74 20 20 20 20 20 20 20  owSetTest       
2ebac 20 20 20 20 20 20 20 20 20 20 20 23 20 49 6e 73             # Ins
2ebad 65 72 74 20 72 6f 77 69 64 20 69 6e 74 6f 20 72  ert rowid into r
2ebae 6f 77 73 65 74 0a 20 20 20 20 2a 2a 20 20 20 20  owset.    **    
2ebaf 20 20 20 20 20 20 47 6f 73 75 62 20 20 20 20 20        Gosub     
2ebb0 20 32 20 41 0a 20 20 20 20 2a 2a 20 20 20 20 20   2 A.    **     
2ebb1 20 20 20 73 71 6c 69 74 65 33 57 68 65 72 65 45     sqlite3WhereE
2ebb2 6e 64 28 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20  nd().    **.    
2ebb3 2a 2a 20 46 6f 6c 6c 6f 77 69 6e 67 20 74 68 65  ** Following the
2ebb4 20 61 62 6f 76 65 2c 20 63 6f 64 65 20 74 6f 20   above, code to 
2ebb5 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f  terminate the lo
2ebb6 6f 70 2e 20 4c 61 62 65 6c 20 41 2c 20 74 68 65  op. Label A, the
2ebb7 20 74 61 72 67 65 74 0a 20 20 20 20 2a 2a 20 6f   target.    ** o
2ebb8 66 20 74 68 65 20 47 6f 73 75 62 20 61 62 6f 76  f the Gosub abov
2ebb9 65 2c 20 6a 75 6d 70 73 20 74 6f 20 74 68 65 20  e, jumps to the 
2ebba 69 6e 73 74 72 75 63 74 69 6f 6e 20 72 69 67 68  instruction righ
2ebbb 74 20 61 66 74 65 72 20 74 68 65 20 47 6f 74 6f  t after the Goto
2ebbc 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
2ebbd 20 20 20 20 20 20 20 20 20 4e 75 6c 6c 20 20 20           Null   
2ebbe 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20 20      1           
2ebbf 20 20 20 20 20 23 20 5a 65 72 6f 20 74 68 65 20       # Zero the 
2ebc0 72 6f 77 73 65 74 20 69 6e 20 72 65 67 20 31 0a  rowset in reg 1.
2ebc1 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
2ebc2 47 6f 74 6f 20 20 20 20 20 20 20 42 20 20 20 20  Goto       B    
2ebc3 20 20 20 20 20 20 20 20 20 20 20 20 23 20 54 68              # Th
2ebc4 65 20 6c 6f 6f 70 20 69 73 20 66 69 6e 69 73 68  e loop is finish
2ebc5 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ed..    **.    *
2ebc6 2a 20 20 20 20 20 20 20 41 3a 20 3c 6c 6f 6f 70  *       A: <loop
2ebc7 20 62 6f 64 79 3e 20 20 20 20 20 20 20 20 20 20   body>          
2ebc8 20 20 20 20 20 20 20 23 20 52 65 74 75 72 6e 20         # Return 
2ebc9 64 61 74 61 2c 20 77 68 61 74 65 76 65 72 2e 0a  data, whatever..
2ebca 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
2ebcb 20 20 20 20 20 20 20 52 65 74 75 72 6e 20 20 20         Return   
2ebcc 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20    2             
2ebcd 20 20 20 23 20 4a 75 6d 70 20 62 61 63 6b 20 74     # Jump back t
2ebce 6f 20 74 68 65 20 47 6f 73 75 62 0a 20 20 20 20  o the Gosub.    
2ebcf 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  **.    **       
2ebd0 42 3a 20 3c 61 66 74 65 72 20 74 68 65 20 6c 6f  B: <after the lo
2ebd1 6f 70 3e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  op>.    **.    *
2ebd2 2f 0a 20 20 20 20 57 68 65 72 65 43 6c 61 75 73  /.    WhereClaus
2ebd3 65 20 2a 70 4f 72 57 63 3b 20 20 20 20 2f 2a 20  e *pOrWc;    /* 
2ebd4 54 68 65 20 4f 52 2d 63 6c 61 75 73 65 20 62 72  The OR-clause br
2ebd5 6f 6b 65 6e 20 6f 75 74 20 69 6e 74 6f 20 73 75  oken out into su
2ebd6 62 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20 57 68  bterms */.    Wh
2ebd7 65 72 65 54 65 72 6d 20 2a 70 46 69 6e 61 6c 3b  ereTerm *pFinal;
2ebd8 20 20 20 20 20 2f 2a 20 46 69 6e 61 6c 20 73 75       /* Final su
2ebd9 62 74 65 72 6d 20 77 69 74 68 69 6e 20 74 68 65  bterm within the
2ebda 20 4f 52 2d 63 6c 61 75 73 65 2e 20 2a 2f 0a 20   OR-clause. */. 
2ebdb 20 20 20 53 72 63 4c 69 73 74 20 6f 6e 65 54 61     SrcList oneTa
2ebdc 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 68 6f  b;        /* Sho
2ebdd 72 74 65 6e 65 64 20 74 61 62 6c 65 20 6c 69 73  rtened table lis
2ebde 74 20 2a 2f 0a 0a 20 20 20 20 69 6e 74 20 72 65  t */..    int re
2ebdf 67 52 65 74 75 72 6e 20 3d 20 2b 2b 70 50 61 72  gReturn = ++pPar
2ebe0 73 65 2d 3e 6e 4d 65 6d 3b 20 20 20 20 20 20 20  se->nMem;       
2ebe1 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
2ebe2 75 73 65 64 20 77 69 74 68 20 4f 50 5f 47 6f 73  used with OP_Gos
2ebe3 75 62 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65  ub */.    int re
2ebe4 67 52 6f 77 73 65 74 20 3d 20 30 3b 20 20 20 20  gRowset = 0;    
2ebe5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ebe6 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
2ebe7 66 6f 72 20 52 6f 77 53 65 74 20 6f 62 6a 65 63  for RowSet objec
2ebe8 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65 67  t */.    int reg
2ebe9 52 6f 77 69 64 20 3d 20 30 3b 20 20 20 20 20 20  Rowid = 0;      
2ebea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ebeb 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68     /* Register h
2ebec 6f 6c 64 69 6e 67 20 72 6f 77 69 64 20 2a 2f 0a  olding rowid */.
2ebed 20 20 20 20 69 6e 74 20 69 4c 6f 6f 70 42 6f 64      int iLoopBod
2ebee 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  y = sqlite3VdbeM
2ebef 61 6b 65 4c 61 62 65 6c 28 76 29 3b 20 20 2f 2a  akeLabel(v);  /*
2ebf0 20 53 74 61 72 74 20 6f 66 20 6c 6f 6f 70 20 62   Start of loop b
2ebf1 6f 64 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ody */.    int i
2ebf2 52 65 74 49 6e 69 74 3b 20 20 20 20 20 20 20 20  RetInit;        
2ebf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ebf4 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
2ebf5 6f 66 20 72 65 67 52 65 74 75 72 6e 20 69 6e 69  of regReturn ini
2ebf6 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b  t */.    int ii;
2ebf7 0a 20 20 20 0a 20 20 20 20 70 54 65 72 6d 20 3d  .   .    pTerm =
2ebf8 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e   pLevel->plan.u.
2ebf9 70 54 65 72 6d 3b 0a 20 20 20 20 61 73 73 65 72  pTerm;.    asser
2ebfa 74 28 20 70 54 65 72 6d 21 3d 30 20 29 3b 0a 20  t( pTerm!=0 );. 
2ebfb 20 20 20 61 73 73 65 72 74 28 20 70 54 65 72 6d     assert( pTerm
2ebfc 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f  ->eOperator==WO_
2ebfd 4f 52 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  OR );.    assert
2ebfe 28 20 28 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67  ( (pTerm->wtFlag
2ebff 73 20 26 20 54 45 52 4d 5f 4f 52 49 4e 46 4f 29  s & TERM_ORINFO)
2ec00 21 3d 30 20 29 3b 0a 20 20 20 20 70 4f 72 57 63  !=0 );.    pOrWc
2ec01 20 3d 20 26 70 54 65 72 6d 2d 3e 75 2e 70 4f 72   = &pTerm->u.pOr
2ec02 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20 20 20 70 46  Info->wc;.    pF
2ec03 69 6e 61 6c 20 3d 20 26 70 4f 72 57 63 2d 3e 61  inal = &pOrWc->a
2ec04 5b 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d 31 5d  [pOrWc->nTerm-1]
2ec05 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 75 70  ;..    /* Set up
2ec06 20 61 20 53 72 63 4c 69 73 74 20 63 6f 6e 74 61   a SrcList conta
2ec07 69 6e 69 6e 67 20 6a 75 73 74 20 74 68 65 20 74  ining just the t
2ec08 61 62 6c 65 20 62 65 69 6e 67 20 73 63 61 6e 6e  able being scann
2ec09 65 64 20 62 79 20 74 68 69 73 20 6c 6f 6f 70 2e  ed by this loop.
2ec0a 20 2a 2f 0a 20 20 20 20 6f 6e 65 54 61 62 2e 6e   */.    oneTab.n
2ec0b 53 72 63 20 3d 20 31 3b 0a 20 20 20 20 6f 6e 65  Src = 1;.    one
2ec0c 54 61 62 2e 6e 41 6c 6c 6f 63 20 3d 20 31 3b 0a  Tab.nAlloc = 1;.
2ec0d 20 20 20 20 6f 6e 65 54 61 62 2e 61 5b 30 5d 20      oneTab.a[0] 
2ec0e 3d 20 2a 70 54 61 62 49 74 65 6d 3b 0a 0a 20 20  = *pTabItem;..  
2ec0f 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
2ec10 74 68 65 20 72 6f 77 73 65 74 20 72 65 67 69 73  the rowset regis
2ec11 74 65 72 20 74 6f 20 63 6f 6e 74 61 69 6e 20 4e  ter to contain N
2ec12 55 4c 4c 2e 20 41 6e 20 53 51 4c 20 4e 55 4c 4c  ULL. An SQL NULL
2ec13 20 69 73 20 0a 20 20 20 20 2a 2a 20 65 71 75 69   is .    ** equi
2ec14 76 61 6c 65 6e 74 20 74 6f 20 61 6e 20 65 6d 70  valent to an emp
2ec15 74 79 20 72 6f 77 73 65 74 2e 0a 20 20 20 20 2a  ty rowset..    *
2ec16 2a 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 20 69 6e  *.    ** Also in
2ec17 69 74 69 61 6c 69 7a 65 20 72 65 67 52 65 74 75  itialize regRetu
2ec18 72 6e 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68  rn to contain th
2ec19 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65  e address of the
2ec1a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 0a 20 20   instruction .  
2ec1b 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79    ** immediately
2ec1c 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 4f   following the O
2ec1d 50 5f 52 65 74 75 72 6e 20 61 74 20 74 68 65 20  P_Return at the 
2ec1e 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20 6c 6f  bottom of the lo
2ec1f 6f 70 2e 20 54 68 69 73 0a 20 20 20 20 2a 2a 20  op. This.    ** 
2ec20 69 73 20 72 65 71 75 69 72 65 64 20 69 6e 20 61  is required in a
2ec21 20 66 65 77 20 6f 62 73 63 75 72 65 20 4c 45 46   few obscure LEF
2ec22 54 20 4a 4f 49 4e 20 63 61 73 65 73 20 77 68 65  T JOIN cases whe
2ec23 72 65 20 63 6f 6e 74 72 6f 6c 20 6a 75 6d 70 73  re control jumps
2ec24 0a 20 20 20 20 2a 2a 20 6f 76 65 72 20 74 68 65  .    ** over the
2ec25 20 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70   top of the loop
2ec26 20 69 6e 74 6f 20 74 68 65 20 62 6f 64 79 20 6f   into the body o
2ec27 66 20 69 74 2e 20 49 6e 20 74 68 69 73 20 63 61  f it. In this ca
2ec28 73 65 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63  se the .    ** c
2ec29 6f 72 72 65 63 74 20 72 65 73 70 6f 6e 73 65 20  orrect response 
2ec2a 66 6f 72 20 74 68 65 20 65 6e 64 2d 6f 66 2d 6c  for the end-of-l
2ec2b 6f 6f 70 20 63 6f 64 65 20 28 74 68 65 20 4f 50  oop code (the OP
2ec2c 5f 52 65 74 75 72 6e 29 20 69 73 20 74 6f 20 0a  _Return) is to .
2ec2d 20 20 20 20 2a 2a 20 66 61 6c 6c 20 74 68 72 6f      ** fall thro
2ec2e 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ugh to the next 
2ec2f 69 6e 73 74 72 75 63 74 69 6f 6e 2c 20 6a 75 73  instruction, jus
2ec30 74 20 61 73 20 61 6e 20 4f 50 5f 4e 65 78 74 20  t as an OP_Next 
2ec31 64 6f 65 73 20 69 66 0a 20 20 20 20 2a 2a 20 63  does if.    ** c
2ec32 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 75 6e 69 6e  alled on an unin
2ec33 69 74 69 61 6c 69 7a 65 64 20 63 75 72 73 6f 72  itialized cursor
2ec34 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
2ec35 20 28 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57   (wctrlFlags & W
2ec36 48 45 52 45 5f 44 55 50 4c 49 43 41 54 45 53 5f  HERE_DUPLICATES_
2ec37 4f 4b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  OK)==0 ){.      
2ec38 72 65 67 52 6f 77 73 65 74 20 3d 20 2b 2b 70 50  regRowset = ++pP
2ec39 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
2ec3a 20 20 72 65 67 52 6f 77 69 64 20 3d 20 2b 2b 70    regRowid = ++p
2ec3b 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
2ec3c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2ec3d 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
2ec3e 20 30 2c 20 72 65 67 52 6f 77 73 65 74 29 3b 0a   0, regRowset);.
2ec3f 20 20 20 20 7d 0a 20 20 20 20 69 52 65 74 49 6e      }.    iRetIn
2ec40 69 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  it = sqlite3Vdbe
2ec41 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
2ec42 65 67 65 72 2c 20 30 2c 20 72 65 67 52 65 74 75  eger, 0, regRetu
2ec43 72 6e 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69 69  rn);..    for(ii
2ec44 3d 30 3b 20 69 69 3c 70 4f 72 57 63 2d 3e 6e 54  =0; ii<pOrWc->nT
2ec45 65 72 6d 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  erm; ii++){.    
2ec46 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4f 72    WhereTerm *pOr
2ec47 54 65 72 6d 20 3d 20 26 70 4f 72 57 63 2d 3e 61  Term = &pOrWc->a
2ec48 5b 69 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20  [ii];.      if( 
2ec49 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72  pOrTerm->leftCur
2ec4a 73 6f 72 3d 3d 69 43 75 72 20 7c 7c 20 70 4f 72  sor==iCur || pOr
2ec4b 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d  Term->eOperator=
2ec4c 3d 57 4f 5f 41 4e 44 20 29 7b 0a 20 20 20 20 20  =WO_AND ){.     
2ec4d 20 20 20 57 68 65 72 65 49 6e 66 6f 20 2a 70 53     WhereInfo *pS
2ec4e 75 62 57 49 6e 66 6f 3b 20 20 20 20 20 20 20 20  ubWInfo;        
2ec4f 20 20 2f 2a 20 49 6e 66 6f 20 66 6f 72 20 73 69    /* Info for si
2ec50 6e 67 6c 65 20 4f 52 2d 74 65 72 6d 20 73 63 61  ngle OR-term sca
2ec51 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 2f 2a 20  n */.        /* 
2ec52 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 61 62  Loop through tab
2ec53 6c 65 20 65 6e 74 72 69 65 73 20 74 68 61 74 20  le entries that 
2ec54 6d 61 74 63 68 20 74 65 72 6d 20 70 4f 72 54 65  match term pOrTe
2ec55 72 6d 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 70  rm. */.        p
2ec56 53 75 62 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74  SubWInfo = sqlit
2ec57 65 33 57 68 65 72 65 42 65 67 69 6e 28 70 50 61  e3WhereBegin(pPa
2ec58 72 73 65 2c 20 26 6f 6e 65 54 61 62 2c 20 70 4f  rse, &oneTab, pO
2ec59 72 54 65 72 6d 2d 3e 70 45 78 70 72 2c 20 30 2c  rTerm->pExpr, 0,
2ec5a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2ec5b 20 20 20 20 20 20 20 20 20 57 48 45 52 45 5f 4f           WHERE_O
2ec5c 4d 49 54 5f 4f 50 45 4e 20 7c 20 57 48 45 52 45  MIT_OPEN | WHERE
2ec5d 5f 4f 4d 49 54 5f 43 4c 4f 53 45 20 7c 20 57 48  _OMIT_CLOSE | WH
2ec5e 45 52 45 5f 46 4f 52 43 45 5f 54 41 42 4c 45 29  ERE_FORCE_TABLE)
2ec5f 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 53  ;.        if( pS
2ec60 75 62 57 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20  ubWInfo ){.     
2ec61 20 20 20 20 20 69 66 28 20 28 77 63 74 72 6c 46       if( (wctrlF
2ec62 6c 61 67 73 20 26 20 57 48 45 52 45 5f 44 55 50  lags & WHERE_DUP
2ec63 4c 49 43 41 54 45 53 5f 4f 4b 29 3d 3d 30 20 29  LICATES_OK)==0 )
2ec64 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e  {.            in
2ec65 74 20 69 53 65 74 20 3d 20 28 28 69 69 3d 3d 70  t iSet = ((ii==p
2ec66 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d 31 29 3f 2d  OrWc->nTerm-1)?-
2ec67 31 3a 69 69 29 3b 0a 20 20 20 20 20 20 20 20 20  1:ii);.         
2ec68 20 20 20 69 6e 74 20 72 3b 0a 20 20 20 20 20 20     int r;.      
2ec69 20 20 20 20 20 20 72 20 3d 20 73 71 6c 69 74 65        r = sqlite
2ec6a 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c 75  3ExprCodeGetColu
2ec6b 6d 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 49  mn(pParse, pTabI
2ec6c 74 65 6d 2d 3e 70 54 61 62 2c 20 2d 31 2c 20 69  tem->pTab, -1, i
2ec6d 43 75 72 2c 20 0a 20 20 20 20 20 20 20 20 20 20  Cur, .          
2ec6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
2ec70 65 67 52 6f 77 69 64 2c 20 30 29 3b 0a 20 20 20  egRowid, 0);.   
2ec71 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2ec72 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2ec73 5f 52 6f 77 53 65 74 54 65 73 74 2c 20 72 65 67  _RowSetTest, reg
2ec74 52 6f 77 73 65 74 2c 0a 20 20 20 20 20 20 20 20  Rowset,.        
2ec75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec76 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2ec77 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 2b  eCurrentAddr(v)+
2ec78 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  2,.             
2ec79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec7a 20 72 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54   r, SQLITE_INT_T
2ec7b 4f 5f 50 54 52 28 69 53 65 74 29 2c 20 50 34 5f  O_PTR(iSet), P4_
2ec7c 49 4e 54 33 32 29 3b 0a 20 20 20 20 20 20 20 20  INT32);.        
2ec7d 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 71    }.          sq
2ec7e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2ec7f 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67  v, OP_Gosub, reg
2ec80 52 65 74 75 72 6e 2c 20 69 4c 6f 6f 70 42 6f 64  Return, iLoopBod
2ec81 79 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f  y);..          /
2ec82 2a 20 46 69 6e 69 73 68 20 74 68 65 20 6c 6f 6f  * Finish the loo
2ec83 70 20 74 68 72 6f 75 67 68 20 74 61 62 6c 65 20  p through table 
2ec84 65 6e 74 72 69 65 73 20 74 68 61 74 20 6d 61 74  entries that mat
2ec85 63 68 20 74 65 72 6d 20 70 4f 72 54 65 72 6d 2e  ch term pOrTerm.
2ec86 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 71   */.          sq
2ec87 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 70 53  lite3WhereEnd(pS
2ec88 75 62 57 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  ubWInfo);.      
2ec89 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
2ec8a 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
2ec8b 65 43 68 61 6e 67 65 50 31 28 76 2c 20 69 52 65  eChangeP1(v, iRe
2ec8c 74 49 6e 69 74 2c 20 73 71 6c 69 74 65 33 56 64  tInit, sqlite3Vd
2ec8d 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
2ec8e 29 3b 0a 20 20 20 20 2f 2a 20 73 71 6c 69 74 65  );.    /* sqlite
2ec8f 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2ec90 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 52 6f  P_Null, 0, regRo
2ec91 77 73 65 74 29 3b 20 2a 2f 0a 20 20 20 20 73 71  wset); */.    sq
2ec92 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2ec93 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70  v, OP_Goto, 0, p
2ec94 4c 65 76 65 6c 2d 3e 61 64 64 72 42 72 6b 29 3b  Level->addrBrk);
2ec95 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2ec96 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
2ec97 69 4c 6f 6f 70 42 6f 64 79 29 3b 0a 0a 20 20 20  iLoopBody);..   
2ec98 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50   pLevel->op = OP
2ec99 5f 52 65 74 75 72 6e 3b 0a 20 20 20 20 70 4c 65  _Return;.    pLe
2ec9a 76 65 6c 2d 3e 70 31 20 3d 20 72 65 67 52 65 74  vel->p1 = regRet
2ec9b 75 72 6e 3b 0a 20 20 20 20 64 69 73 61 62 6c 65  urn;.    disable
2ec9c 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 54 65  Term(pLevel, pTe
2ec9d 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e  rm);.  }else.#en
2ec9e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2ec9f 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49  IT_OR_OPTIMIZATI
2eca0 4f 4e 20 2a 2f 0a 0a 20 20 7b 0a 20 20 20 20 2f  ON */..  {.    /
2eca1 2a 20 43 61 73 65 20 35 3a 20 20 54 68 65 72 65  * Case 5:  There
2eca2 20 69 73 20 6e 6f 20 75 73 61 62 6c 65 20 69 6e   is no usable in
2eca3 64 65 78 2e 20 20 57 65 20 6d 75 73 74 20 64 6f  dex.  We must do
2eca4 20 61 20 63 6f 6d 70 6c 65 74 65 0a 20 20 20 20   a complete.    
2eca5 2a 2a 20 20 20 20 20 20 20 20 20 20 73 63 61 6e  **          scan
2eca6 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65 20 74   of the entire t
2eca7 61 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  able..    */.   
2eca8 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38   static const u8
2eca9 20 61 53 74 65 70 5b 5d 20 3d 20 7b 20 4f 50 5f   aStep[] = { OP_
2ecaa 4e 65 78 74 2c 20 4f 50 5f 50 72 65 76 20 7d 3b  Next, OP_Prev };
2ecab 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
2ecac 74 20 75 38 20 61 53 74 61 72 74 5b 5d 20 3d 20  t u8 aStart[] = 
2ecad 7b 20 4f 50 5f 52 65 77 69 6e 64 2c 20 4f 50 5f  { OP_Rewind, OP_
2ecae 4c 61 73 74 20 7d 3b 0a 20 20 20 20 61 73 73 65  Last };.    asse
2ecaf 72 74 28 20 62 52 65 76 3d 3d 30 20 7c 7c 20 62  rt( bRev==0 || b
2ecb0 52 65 76 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73  Rev==1 );.    as
2ecb1 73 65 72 74 28 20 6f 6d 69 74 54 61 62 6c 65 3d  sert( omitTable=
2ecb2 3d 30 20 29 3b 0a 20 20 20 20 70 4c 65 76 65 6c  =0 );.    pLevel
2ecb3 2d 3e 6f 70 20 3d 20 61 53 74 65 70 5b 62 52 65  ->op = aStep[bRe
2ecb4 76 5d 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e  v];.    pLevel->
2ecb5 70 31 20 3d 20 69 43 75 72 3b 0a 20 20 20 20 70  p1 = iCur;.    p
2ecb6 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 31 20 2b 20  Level->p2 = 1 + 
2ecb7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2ecb8 32 28 76 2c 20 61 53 74 61 72 74 5b 62 52 65 76  2(v, aStart[bRev
2ecb9 5d 2c 20 69 43 75 72 2c 20 61 64 64 72 42 72 6b  ], iCur, addrBrk
2ecba 29 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70  );.    pLevel->p
2ecbb 35 20 3d 20 53 51 4c 49 54 45 5f 53 54 4d 54 53  5 = SQLITE_STMTS
2ecbc 54 41 54 55 53 5f 46 55 4c 4c 53 43 41 4e 5f 53  TATUS_FULLSCAN_S
2ecbd 54 45 50 3b 0a 20 20 7d 0a 20 20 6e 6f 74 52 65  TEP;.  }.  notRe
2ecbe 61 64 79 20 26 3d 20 7e 67 65 74 4d 61 73 6b 28  ady &= ~getMask(
2ecbf 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 2c 20 69  pWC->pMaskSet, i
2ecc0 43 75 72 29 3b 0a 0a 20 20 2f 2a 20 49 6e 73 65  Cur);..  /* Inse
2ecc1 72 74 20 63 6f 64 65 20 74 6f 20 74 65 73 74 20  rt code to test 
2ecc2 65 76 65 72 79 20 73 75 62 65 78 70 72 65 73 73  every subexpress
2ecc3 69 6f 6e 20 74 68 61 74 20 63 61 6e 20 62 65 20  ion that can be 
2ecc4 63 6f 6d 70 6c 65 74 65 6c 79 0a 20 20 2a 2a 20  completely.  ** 
2ecc5 63 6f 6d 70 75 74 65 64 20 75 73 69 6e 67 20 74  computed using t
2ecc6 68 65 20 63 75 72 72 65 6e 74 20 73 65 74 20 6f  he current set o
2ecc7 66 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20  f tables..  */. 
2ecc8 20 6b 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 54   k = 0;.  for(pT
2ecc9 65 72 6d 3d 70 57 43 2d 3e 61 2c 20 6a 3d 70 57  erm=pWC->a, j=pW
2ecca 43 2d 3e 6e 54 65 72 6d 3b 20 6a 3e 30 3b 20 6a  C->nTerm; j>0; j
2eccb 2d 2d 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20  --, pTerm++){.  
2eccc 20 20 45 78 70 72 20 2a 70 45 3b 0a 20 20 20 20    Expr *pE;.    
2eccd 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d  testcase( pTerm-
2ecce 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f  >wtFlags & TERM_
2eccf 56 49 52 54 55 41 4c 20 29 3b 0a 20 20 20 20 74  VIRTUAL );.    t
2ecd0 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e  estcase( pTerm->
2ecd1 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 43  wtFlags & TERM_C
2ecd2 4f 44 45 44 20 29 3b 0a 20 20 20 20 69 66 28 20  ODED );.    if( 
2ecd3 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26  pTerm->wtFlags &
2ecd4 20 28 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54   (TERM_VIRTUAL|T
2ecd5 45 52 4d 5f 43 4f 44 45 44 29 20 29 20 63 6f 6e  ERM_CODED) ) con
2ecd6 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 28  tinue;.    if( (
2ecd7 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c  pTerm->prereqAll
2ecd8 20 26 20 6e 6f 74 52 65 61 64 79 29 21 3d 30 20   & notReady)!=0 
2ecd9 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
2ecda 70 45 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70  pE = pTerm->pExp
2ecdb 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  r;.    assert( p
2ecdc 45 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  E!=0 );.    if( 
2ecdd 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69  pLevel->iLeftJoi
2ecde 6e 20 26 26 20 21 45 78 70 72 48 61 73 50 72 6f  n && !ExprHasPro
2ecdf 70 65 72 74 79 28 70 45 2c 20 45 50 5f 46 72 6f  perty(pE, EP_Fro
2ece0 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20 20 20  mJoin) ){.      
2ece1 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a  continue;.    }.
2ece2 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
2ece3 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70  fFalse(pParse, p
2ece4 45 2c 20 61 64 64 72 43 6f 6e 74 2c 20 53 51 4c  E, addrCont, SQL
2ece5 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b  ITE_JUMPIFNULL);
2ece6 0a 20 20 20 20 6b 20 3d 20 31 3b 0a 20 20 20 20  .    k = 1;.    
2ece7 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c  pTerm->wtFlags |
2ece8 3d 20 54 45 52 4d 5f 43 4f 44 45 44 3b 0a 20 20  = TERM_CODED;.  
2ece9 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20 61 20 4c 45  }..  /* For a LE
2ecea 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 2c 20 67  FT OUTER JOIN, g
2eceb 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
2ecec 74 20 77 69 6c 6c 20 72 65 63 6f 72 64 20 74 68  t will record th
2eced 65 20 66 61 63 74 20 74 68 61 74 0a 20 20 2a 2a  e fact that.  **
2ecee 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 72 6f   at least one ro
2ecef 77 20 6f 66 20 74 68 65 20 72 69 67 68 74 20 74  w of the right t
2ecf0 61 62 6c 65 20 68 61 73 20 6d 61 74 63 68 65 64  able has matched
2ecf1 20 74 68 65 20 6c 65 66 74 20 74 61 62 6c 65 2e   the left table.
2ecf2 20 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4c    .  */.  if( pL
2ecf3 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20  evel->iLeftJoin 
2ecf4 29 7b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 61  ){.    pLevel->a
2ecf5 64 64 72 46 69 72 73 74 20 3d 20 73 71 6c 69 74  ddrFirst = sqlit
2ecf6 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
2ecf7 72 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  r(v);.    sqlite
2ecf8 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2ecf9 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 70 4c  P_Integer, 1, pL
2ecfa 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 29  evel->iLeftJoin)
2ecfb 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e  ;.    VdbeCommen
2ecfc 74 28 28 76 2c 20 22 72 65 63 6f 72 64 20 4c 45  t((v, "record LE
2ecfd 46 54 20 4a 4f 49 4e 20 68 69 74 22 29 29 3b 0a  FT JOIN hit"));.
2ecfe 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2ecff 61 63 68 65 43 6c 65 61 72 28 70 50 61 72 73 65  acheClear(pParse
2ed00 29 3b 0a 20 20 20 20 66 6f 72 28 70 54 65 72 6d  );.    for(pTerm
2ed01 3d 70 57 43 2d 3e 61 2c 20 6a 3d 30 3b 20 6a 3c  =pWC->a, j=0; j<
2ed02 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a 2b 2b 2c  pWC->nTerm; j++,
2ed03 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20   pTerm++){.     
2ed04 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d   testcase( pTerm
2ed05 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d  ->wtFlags & TERM
2ed06 5f 56 49 52 54 55 41 4c 20 29 3b 0a 20 20 20 20  _VIRTUAL );.    
2ed07 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72    testcase( pTer
2ed08 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52  m->wtFlags & TER
2ed09 4d 5f 43 4f 44 45 44 20 29 3b 0a 20 20 20 20 20  M_CODED );.     
2ed0a 20 69 66 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c   if( pTerm->wtFl
2ed0b 61 67 73 20 26 20 28 54 45 52 4d 5f 56 49 52 54  ags & (TERM_VIRT
2ed0c 55 41 4c 7c 54 45 52 4d 5f 43 4f 44 45 44 29 20  UAL|TERM_CODED) 
2ed0d 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
2ed0e 20 20 69 66 28 20 28 70 54 65 72 6d 2d 3e 70 72    if( (pTerm->pr
2ed0f 65 72 65 71 41 6c 6c 20 26 20 6e 6f 74 52 65 61  ereqAll & notRea
2ed10 64 79 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e 75  dy)!=0 ) continu
2ed11 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  e;.      assert(
2ed12 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 20 29 3b   pTerm->pExpr );
2ed13 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2ed14 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65  prIfFalse(pParse
2ed15 2c 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 2c 20  , pTerm->pExpr, 
2ed16 61 64 64 72 43 6f 6e 74 2c 20 53 51 4c 49 54 45  addrCont, SQLITE
2ed17 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20  _JUMPIFNULL);.  
2ed18 20 20 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61      pTerm->wtFla
2ed19 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 44 45 44  gs |= TERM_CODED
2ed1a 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71  ;.    }.  }.  sq
2ed1b 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2ed1c 52 65 67 28 70 50 61 72 73 65 2c 20 69 52 65 6c  Reg(pParse, iRel
2ed1d 65 61 73 65 52 65 67 29 3b 0a 0a 20 20 72 65 74  easeReg);..  ret
2ed1e 75 72 6e 20 6e 6f 74 52 65 61 64 79 3b 0a 7d 0a  urn notReady;.}.
2ed1f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
2ed20 49 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20  ITE_TEST)./*.** 
2ed21 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61  The following va
2ed22 72 69 61 62 6c 65 20 68 6f 6c 64 73 20 61 20 74  riable holds a t
2ed23 65 78 74 20 64 65 73 63 72 69 70 74 69 6f 6e 20  ext description 
2ed24 6f 66 20 71 75 65 72 79 20 70 6c 61 6e 20 67 65  of query plan ge
2ed25 6e 65 72 61 74 65 64 0a 2a 2a 20 62 79 20 74 68  nerated.** by th
2ed26 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61  e most recent ca
2ed27 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 57 68 65  ll to sqlite3Whe
2ed28 72 65 42 65 67 69 6e 28 29 2e 20 20 45 61 63 68  reBegin().  Each
2ed29 20 63 61 6c 6c 20 74 6f 20 57 68 65 72 65 42 65   call to WhereBe
2ed2a 67 69 6e 0a 2a 2a 20 6f 76 65 72 77 72 69 74 65  gin.** overwrite
2ed2b 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 2e 20  s the previous. 
2ed2c 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f   This informatio
2ed2d 6e 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65  n is used for te
2ed2e 73 74 69 6e 67 20 61 6e 64 0a 2a 2a 20 61 6e 61  sting and.** ana
2ed2f 6c 79 73 69 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53  lysis only..*/.S
2ed30 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 73  QLITE_API char s
2ed31 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61  qlite3_query_pla
2ed32 6e 5b 42 4d 53 2a 32 2a 34 30 5d 3b 20 20 2f 2a  n[BMS*2*40];  /*
2ed33 20 54 65 78 74 20 6f 66 20 74 68 65 20 6a 6f 69   Text of the joi
2ed34 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  n */.static int 
2ed35 6e 51 50 6c 61 6e 20 3d 20 30 3b 20 20 20 20 20  nQPlan = 0;     
2ed36 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74           /* Next
2ed37 20 66 72 65 65 20 73 6c 6f 77 20 69 6e 20 5f 71   free slow in _q
2ed38 75 65 72 79 5f 70 6c 61 6e 5b 5d 20 2a 2f 0a 0a  uery_plan[] */..
2ed39 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2ed3a 5f 54 45 53 54 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a  _TEST */.../*.**
2ed3b 20 46 72 65 65 20 61 20 57 68 65 72 65 49 6e 66   Free a WhereInf
2ed3c 6f 20 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a 73  o structure.*/.s
2ed3d 74 61 74 69 63 20 76 6f 69 64 20 77 68 65 72 65  tatic void where
2ed3e 49 6e 66 6f 46 72 65 65 28 73 71 6c 69 74 65 33  InfoFree(sqlite3
2ed3f 20 2a 64 62 2c 20 57 68 65 72 65 49 6e 66 6f 20   *db, WhereInfo 
2ed40 2a 70 57 49 6e 66 6f 29 7b 0a 20 20 69 66 28 20  *pWInfo){.  if( 
2ed41 70 57 49 6e 66 6f 20 29 7b 0a 20 20 20 20 69 6e  pWInfo ){.    in
2ed42 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  t i;.    for(i=0
2ed43 3b 20 69 3c 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76  ; i<pWInfo->nLev
2ed44 65 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  el; i++){.      
2ed45 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
2ed46 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 70 57 49 6e  fo *pInfo = pWIn
2ed47 66 6f 2d 3e 61 5b 69 5d 2e 70 49 64 78 49 6e 66  fo->a[i].pIdxInf
2ed48 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 6e  o;.      if( pIn
2ed49 66 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  fo ){.        /*
2ed4a 20 61 73 73 65 72 74 28 20 70 49 6e 66 6f 2d 3e   assert( pInfo->
2ed4b 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72  needToFreeIdxStr
2ed4c 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  ==0 || db->mallo
2ed4d 63 46 61 69 6c 65 64 20 29 3b 20 2a 2f 0a 20 20  cFailed ); */.  
2ed4e 20 20 20 20 20 20 69 66 28 20 70 49 6e 66 6f 2d        if( pInfo-
2ed4f 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74  >needToFreeIdxSt
2ed50 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  r ){.          s
2ed51 71 6c 69 74 65 33 5f 66 72 65 65 28 70 49 6e 66  qlite3_free(pInf
2ed52 6f 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20 20 20  o->idxStr);.    
2ed53 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71      }.        sq
2ed54 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2ed55 70 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 7d 0a  pInfo);.      }.
2ed56 20 20 20 20 7d 0a 20 20 20 20 77 68 65 72 65 43      }.    whereC
2ed57 6c 61 75 73 65 43 6c 65 61 72 28 70 57 49 6e 66  lauseClear(pWInf
2ed58 6f 2d 3e 70 57 43 29 3b 0a 20 20 20 20 73 71 6c  o->pWC);.    sql
2ed59 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2ed5a 57 49 6e 66 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a  WInfo);.  }.}...
2ed5b 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 74  /*.** Generate t
2ed5c 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
2ed5d 74 68 65 20 6c 6f 6f 70 20 75 73 65 64 20 66 6f  the loop used fo
2ed5e 72 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 70  r WHERE clause p
2ed5f 72 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a 20 54 68  rocessing..** Th
2ed60 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69  e return value i
2ed61 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
2ed62 6e 20 6f 70 61 71 75 65 20 73 74 72 75 63 74 75  n opaque structu
2ed63 72 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  re that contains
2ed64 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  .** information 
2ed65 6e 65 65 64 65 64 20 74 6f 20 74 65 72 6d 69 6e  needed to termin
2ed66 61 74 65 20 74 68 65 20 6c 6f 6f 70 2e 20 20 4c  ate the loop.  L
2ed67 61 74 65 72 2c 20 74 68 65 20 63 61 6c 6c 69 6e  ater, the callin
2ed68 67 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 73 68 6f  g routine.** sho
2ed69 75 6c 64 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74  uld invoke sqlit
2ed6a 65 33 57 68 65 72 65 45 6e 64 28 29 20 77 69 74  e3WhereEnd() wit
2ed6b 68 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  h the return val
2ed6c 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74  ue of this funct
2ed6d 69 6f 6e 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20  ion.** in order 
2ed6e 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20  to complete the 
2ed6f 57 48 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f  WHERE clause pro
2ed70 63 65 73 73 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49  cessing..**.** I
2ed71 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
2ed72 73 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  s, this routine 
2ed73 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a  returns NULL..**
2ed74 0a 2a 2a 20 54 68 65 20 62 61 73 69 63 20 69 64  .** The basic id
2ed75 65 61 20 69 73 20 74 6f 20 64 6f 20 61 20 6e 65  ea is to do a ne
2ed76 73 74 65 64 20 6c 6f 6f 70 2c 20 6f 6e 65 20 6c  sted loop, one l
2ed77 6f 6f 70 20 66 6f 72 20 65 61 63 68 20 74 61 62  oop for each tab
2ed78 6c 65 20 69 6e 0a 2a 2a 20 74 68 65 20 46 52 4f  le in.** the FRO
2ed79 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20 73 65  M clause of a se
2ed7a 6c 65 63 74 2e 20 20 28 49 4e 53 45 52 54 20 61  lect.  (INSERT a
2ed7b 6e 64 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  nd UPDATE statem
2ed7c 65 6e 74 73 20 61 72 65 20 74 68 65 0a 2a 2a 20  ents are the.** 
2ed7d 73 61 6d 65 20 61 73 20 61 20 53 45 4c 45 43 54  same as a SELECT
2ed7e 20 77 69 74 68 20 6f 6e 6c 79 20 61 20 73 69 6e   with only a sin
2ed7f 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65  gle table in the
2ed80 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 29 20 20   FROM clause.)  
2ed81 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20  For.** example, 
2ed82 69 66 20 74 68 65 20 53 51 4c 20 69 73 20 74 68  if the SQL is th
2ed83 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  is:.**.**       
2ed84 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31  SELECT * FROM t1
2ed85 2c 20 74 32 2c 20 74 33 20 57 48 45 52 45 20 2e  , t2, t3 WHERE .
2ed86 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 74  ..;.**.** Then t
2ed87 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65  he code generate
2ed88 64 20 69 73 20 63 6f 6e 63 65 70 74 75 61 6c 6c  d is conceptuall
2ed89 79 20 6c 69 6b 65 20 74 68 65 20 66 6f 6c 6c 6f  y like the follo
2ed8a 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  wing:.**.**     
2ed8b 20 66 6f 72 65 61 63 68 20 72 6f 77 31 20 69 6e   foreach row1 in
2ed8c 20 74 31 20 64 6f 20 20 20 20 20 20 20 5c 20 20   t1 do       \  
2ed8d 20 20 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64    Code generated
2ed8e 0a 2a 2a 20 20 20 20 20 20 20 20 66 6f 72 65 61  .**        forea
2ed8f 63 68 20 72 6f 77 32 20 69 6e 20 74 32 20 64 6f  ch row2 in t2 do
2ed90 20 20 20 20 20 20 7c 2d 2d 20 62 79 20 73 71 6c        |-- by sql
2ed91 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
2ed92 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 6f 72  .**          for
2ed93 65 61 63 68 20 72 6f 77 33 20 69 6e 20 74 33 20  each row3 in t3 
2ed94 64 6f 20 20 20 2f 0a 2a 2a 20 20 20 20 20 20 20  do   /.**       
2ed95 20 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 20 20       ....**     
2ed96 20 20 20 20 20 65 6e 64 20 20 20 20 20 20 20 20       end        
2ed97 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 20 20               \  
2ed98 20 20 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64    Code generated
2ed99 0a 2a 2a 20 20 20 20 20 20 20 20 65 6e 64 20 20  .**        end  
2ed9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ed9b 20 20 20 20 20 20 7c 2d 2d 20 62 79 20 73 71 6c        |-- by sql
2ed9c 69 74 65 33 57 68 65 72 65 45 6e 64 28 29 0a 2a  ite3WhereEnd().*
2ed9d 2a 20 20 20 20 20 20 65 6e 64 20 20 20 20 20 20  *      end      
2ed9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ed9f 20 20 20 2f 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20     /.**.** Note 
2eda0 74 68 61 74 20 74 68 65 20 6c 6f 6f 70 73 20 6d  that the loops m
2eda1 69 67 68 74 20 6e 6f 74 20 62 65 20 6e 65 73 74  ight not be nest
2eda2 65 64 20 69 6e 20 74 68 65 20 6f 72 64 65 72 20  ed in the order 
2eda3 69 6e 20 77 68 69 63 68 20 74 68 65 79 0a 2a 2a  in which they.**
2eda4 20 61 70 70 65 61 72 20 69 6e 20 74 68 65 20 46   appear in the F
2eda5 52 4f 4d 20 63 6c 61 75 73 65 20 69 66 20 61 20  ROM clause if a 
2eda6 64 69 66 66 65 72 65 6e 74 20 6f 72 64 65 72 20  different order 
2eda7 69 73 20 62 65 74 74 65 72 20 61 62 6c 65 20 74  is better able t
2eda8 6f 20 6d 61 6b 65 0a 2a 2a 20 75 73 65 20 6f 66  o make.** use of
2eda9 20 69 6e 64 69 63 65 73 2e 20 20 4e 6f 74 65 20   indices.  Note 
2edaa 61 6c 73 6f 20 74 68 61 74 20 77 68 65 6e 20 74  also that when t
2edab 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 61  he IN operator a
2edac 70 70 65 61 72 73 20 69 6e 0a 2a 2a 20 74 68 65  ppears in.** the
2edad 20 57 48 45 52 45 20 63 6c 61 75 73 65 2c 20 69   WHERE clause, i
2edae 74 20 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69  t might result i
2edaf 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 6e 65 73  n additional nes
2edb0 74 65 64 20 6c 6f 6f 70 73 20 66 6f 72 0a 2a 2a  ted loops for.**
2edb1 20 73 63 61 6e 6e 69 6e 67 20 74 68 72 6f 75 67   scanning throug
2edb2 68 20 61 6c 6c 20 76 61 6c 75 65 73 20 6f 6e 20  h all values on 
2edb3 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73  the right-hand s
2edb4 69 64 65 20 6f 66 20 74 68 65 20 49 4e 2e 0a 2a  ide of the IN..*
2edb5 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 42  *.** There are B
2edb6 74 72 65 65 20 63 75 72 73 6f 72 73 20 61 73 73  tree cursors ass
2edb7 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63  ociated with eac
2edb8 68 20 74 61 62 6c 65 2e 20 20 74 31 20 75 73 65  h table.  t1 use
2edb9 73 20 63 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d 62  s cursor.** numb
2edba 65 72 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30  er pTabList->a[0
2edbb 5d 2e 69 43 75 72 73 6f 72 2e 20 20 74 32 20 75  ].iCursor.  t2 u
2edbc 73 65 73 20 74 68 65 20 63 75 72 73 6f 72 20 70  ses the cursor p
2edbd 54 61 62 4c 69 73 74 2d 3e 61 5b 31 5d 2e 69 43  TabList->a[1].iC
2edbe 75 72 73 6f 72 2e 0a 2a 2a 20 41 6e 64 20 73 6f  ursor..** And so
2edbf 20 66 6f 72 74 68 2e 20 20 54 68 69 73 20 72 6f   forth.  This ro
2edc0 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20  utine generates 
2edc1 63 6f 64 65 20 74 6f 20 6f 70 65 6e 20 74 68 6f  code to open tho
2edc2 73 65 20 56 44 42 45 20 63 75 72 73 6f 72 73 0a  se VDBE cursors.
2edc3 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 57 68  ** and sqlite3Wh
2edc4 65 72 65 45 6e 64 28 29 20 67 65 6e 65 72 61 74  ereEnd() generat
2edc5 65 73 20 74 68 65 20 63 6f 64 65 20 74 6f 20 63  es the code to c
2edc6 6c 6f 73 65 20 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a  lose them..**.**
2edc7 20 54 68 65 20 63 6f 64 65 20 74 68 61 74 20 73   The code that s
2edc8 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
2edc9 28 29 20 67 65 6e 65 72 61 74 65 73 20 6c 65 61  () generates lea
2edca 76 65 73 20 74 68 65 20 63 75 72 73 6f 72 73 20  ves the cursors 
2edcb 6e 61 6d 65 64 0a 2a 2a 20 69 6e 20 70 54 61 62  named.** in pTab
2edcc 4c 69 73 74 20 70 6f 69 6e 74 69 6e 67 20 61 74  List pointing at
2edcd 20 74 68 65 69 72 20 61 70 70 72 6f 70 72 69 61   their appropria
2edce 74 65 20 65 6e 74 72 69 65 73 2e 20 20 54 68 65  te entries.  The
2edcf 20 5b 2e 2e 2e 5d 20 63 6f 64 65 0a 2a 2a 20 63   [...] code.** c
2edd0 61 6e 20 75 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e  an use OP_Column
2edd1 20 61 6e 64 20 4f 50 5f 52 6f 77 69 64 20 6f 70   and OP_Rowid op
2edd2 63 6f 64 65 73 20 6f 6e 20 74 68 65 73 65 20 63  codes on these c
2edd3 75 72 73 6f 72 73 20 74 6f 20 65 78 74 72 61 63  ursors to extrac
2edd4 74 0a 2a 2a 20 64 61 74 61 20 66 72 6f 6d 20 74  t.** data from t
2edd5 68 65 20 76 61 72 69 6f 75 73 20 74 61 62 6c 65  he various table
2edd6 73 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 0a 2a  s of the loop..*
2edd7 2a 0a 2a 2a 20 49 66 20 74 68 65 20 57 48 45 52  *.** If the WHER
2edd8 45 20 63 6c 61 75 73 65 20 69 73 20 65 6d 70 74  E clause is empt
2edd9 79 2c 20 74 68 65 20 66 6f 72 65 61 63 68 20 6c  y, the foreach l
2edda 6f 6f 70 73 20 6d 75 73 74 20 65 61 63 68 20 73  oops must each s
2eddb 63 61 6e 20 74 68 65 69 72 0a 2a 2a 20 65 6e 74  can their.** ent
2eddc 69 72 65 20 74 61 62 6c 65 73 2e 20 20 54 68 75  ire tables.  Thu
2eddd 73 20 61 20 74 68 72 65 65 2d 77 61 79 20 6a 6f  s a three-way jo
2edde 69 6e 20 69 73 20 61 6e 20 4f 28 4e 5e 33 29 20  in is an O(N^3) 
2eddf 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 75 74 20  operation.  But 
2ede0 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 73  if.** the tables
2ede1 20 68 61 76 65 20 69 6e 64 69 63 65 73 20 61 6e   have indices an
2ede2 64 20 74 68 65 72 65 20 61 72 65 20 74 65 72 6d  d there are term
2ede3 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63  s in the WHERE c
2ede4 6c 61 75 73 65 20 74 68 61 74 0a 2a 2a 20 72 65  lause that.** re
2ede5 66 65 72 20 74 6f 20 74 68 6f 73 65 20 69 6e 64  fer to those ind
2ede6 69 63 65 73 2c 20 61 20 63 6f 6d 70 6c 65 74 65  ices, a complete
2ede7 20 74 61 62 6c 65 20 73 63 61 6e 20 63 61 6e 20   table scan can 
2ede8 62 65 20 61 76 6f 69 64 65 64 20 61 6e 64 20 74  be avoided and t
2ede9 68 65 0a 2a 2a 20 63 6f 64 65 20 77 69 6c 6c 20  he.** code will 
2edea 72 75 6e 20 6d 75 63 68 20 66 61 73 74 65 72 2e  run much faster.
2edeb 20 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 77 6f    Most of the wo
2edec 72 6b 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69  rk of this routi
2eded 6e 65 20 69 73 20 63 68 65 63 6b 69 6e 67 0a 2a  ne is checking.*
2edee 2a 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 72  * to see if ther
2edef 65 20 61 72 65 20 69 6e 64 69 63 65 73 20 74 68  e are indices th
2edf0 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  at can be used t
2edf1 6f 20 73 70 65 65 64 20 75 70 20 74 68 65 20 6c  o speed up the l
2edf2 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 65 72 6d 73  oop..**.** Terms
2edf3 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c   of the WHERE cl
2edf4 61 75 73 65 20 61 72 65 20 61 6c 73 6f 20 75 73  ause are also us
2edf5 65 64 20 74 6f 20 6c 69 6d 69 74 20 77 68 69 63  ed to limit whic
2edf6 68 20 72 6f 77 73 20 61 63 74 75 61 6c 6c 79 0a  h rows actually.
2edf7 2a 2a 20 6d 61 6b 65 20 69 74 20 74 6f 20 74 68  ** make it to th
2edf8 65 20 22 2e 2e 2e 22 20 69 6e 20 74 68 65 20 6d  e "..." in the m
2edf9 69 64 64 6c 65 20 6f 66 20 74 68 65 20 6c 6f 6f  iddle of the loo
2edfa 70 2e 20 20 41 66 74 65 72 20 65 61 63 68 20 22  p.  After each "
2edfb 66 6f 72 65 61 63 68 22 2c 0a 2a 2a 20 74 65 72  foreach",.** ter
2edfc 6d 73 20 6f 66 20 74 68 65 20 57 48 45 52 45 20  ms of the WHERE 
2edfd 63 6c 61 75 73 65 20 74 68 61 74 20 75 73 65 20  clause that use 
2edfe 6f 6e 6c 79 20 74 65 72 6d 73 20 69 6e 20 74 68  only terms in th
2edff 61 74 20 6c 6f 6f 70 20 61 6e 64 20 6f 75 74 65  at loop and oute
2ee00 72 0a 2a 2a 20 6c 6f 6f 70 73 20 61 72 65 20 65  r.** loops are e
2ee01 76 61 6c 75 61 74 65 64 20 61 6e 64 20 69 66 20  valuated and if 
2ee02 66 61 6c 73 65 20 61 20 6a 75 6d 70 20 69 73 20  false a jump is 
2ee03 6d 61 64 65 20 61 72 6f 75 6e 64 20 61 6c 6c 20  made around all 
2ee04 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 69 6e  subsequent.** in
2ee05 6e 65 72 20 6c 6f 6f 70 73 20 28 6f 72 20 61 72  ner loops (or ar
2ee06 6f 75 6e 64 20 74 68 65 20 22 2e 2e 2e 22 20 69  ound the "..." i
2ee07 66 20 74 68 65 20 74 65 73 74 20 6f 63 63 75 72  f the test occur
2ee08 73 20 77 69 74 68 69 6e 20 74 68 65 20 69 6e 6e  s within the inn
2ee09 65 72 2d 0a 2a 2a 20 6d 6f 73 74 20 6c 6f 6f 70  er-.** most loop
2ee0a 29 0a 2a 2a 0a 2a 2a 20 4f 55 54 45 52 20 4a 4f  ).**.** OUTER JO
2ee0b 49 4e 53 0a 2a 2a 0a 2a 2a 20 41 6e 20 6f 75 74  INS.**.** An out
2ee0c 65 72 20 6a 6f 69 6e 20 6f 66 20 74 61 62 6c 65  er join of table
2ee0d 73 20 74 31 20 61 6e 64 20 74 32 20 69 73 20 63  s t1 and t2 is c
2ee0e 6f 6e 63 65 70 74 61 6c 6c 79 20 63 6f 64 65 64  onceptally coded
2ee0f 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
2ee10 2a 2a 20 20 20 20 66 6f 72 65 61 63 68 20 72 6f  **    foreach ro
2ee11 77 31 20 69 6e 20 74 31 20 64 6f 0a 2a 2a 20 20  w1 in t1 do.**  
2ee12 20 20 20 20 66 6c 61 67 20 3d 20 30 0a 2a 2a 20      flag = 0.** 
2ee13 20 20 20 20 20 66 6f 72 65 61 63 68 20 72 6f 77       foreach row
2ee14 32 20 69 6e 20 74 32 20 64 6f 0a 2a 2a 20 20 20  2 in t2 do.**   
2ee15 20 20 20 20 20 73 74 61 72 74 3a 0a 2a 2a 20 20       start:.**  
2ee16 20 20 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20          ....**  
2ee17 20 20 20 20 20 20 20 20 66 6c 61 67 20 3d 20 31          flag = 1
2ee18 0a 2a 2a 20 20 20 20 20 20 65 6e 64 0a 2a 2a 20  .**      end.** 
2ee19 20 20 20 20 20 69 66 20 66 6c 61 67 3d 3d 30 20       if flag==0 
2ee1a 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 6d  then.**        m
2ee1b 6f 76 65 20 74 68 65 20 72 6f 77 32 20 63 75 72  ove the row2 cur
2ee1c 73 6f 72 20 74 6f 20 61 20 6e 75 6c 6c 20 72 6f  sor to a null ro
2ee1d 77 0a 2a 2a 20 20 20 20 20 20 20 20 67 6f 74 6f  w.**        goto
2ee1e 20 73 74 61 72 74 0a 2a 2a 20 20 20 20 20 20 66   start.**      f
2ee1f 69 0a 2a 2a 20 20 20 20 65 6e 64 0a 2a 2a 0a 2a  i.**    end.**.*
2ee20 2a 20 4f 52 44 45 52 20 42 59 20 43 4c 41 55 53  * ORDER BY CLAUS
2ee21 45 20 50 52 4f 43 45 53 53 49 4e 47 0a 2a 2a 0a  E PROCESSING.**.
2ee22 2a 2a 20 2a 70 70 4f 72 64 65 72 42 79 20 69 73  ** *ppOrderBy is
2ee23 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
2ee24 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
2ee25 65 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74  e of a SELECT st
2ee26 61 74 65 6d 65 6e 74 2c 0a 2a 2a 20 69 66 20 74  atement,.** if t
2ee27 68 65 72 65 20 69 73 20 6f 6e 65 2e 20 20 49 66  here is one.  If
2ee28 20 74 68 65 72 65 20 69 73 20 6e 6f 20 4f 52 44   there is no ORD
2ee29 45 52 20 42 59 20 63 6c 61 75 73 65 20 6f 72 20  ER BY clause or 
2ee2a 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a  if this routine.
2ee2b 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f  ** is called fro
2ee2c 6d 20 61 6e 20 55 50 44 41 54 45 20 6f 72 20 44  m an UPDATE or D
2ee2d 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 2c  ELETE statement,
2ee2e 20 74 68 65 6e 20 70 70 4f 72 64 65 72 42 79 20   then ppOrderBy 
2ee2f 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49  is NULL..**.** I
2ee30 66 20 61 6e 20 69 6e 64 65 78 20 63 61 6e 20 62  f an index can b
2ee31 65 20 75 73 65 64 20 73 6f 20 74 68 61 74 20 74  e used so that t
2ee32 68 65 20 6e 61 74 75 72 61 6c 20 6f 75 74 70 75  he natural outpu
2ee33 74 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20 74  t order of the t
2ee34 61 62 6c 65 0a 2a 2a 20 73 63 61 6e 20 69 73 20  able.** scan is 
2ee35 63 6f 72 72 65 63 74 20 66 6f 72 20 74 68 65 20  correct for the 
2ee36 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c  ORDER BY clause,
2ee37 20 74 68 65 6e 20 74 68 61 74 20 69 6e 64 65 78   then that index
2ee38 20 69 73 20 75 73 65 64 20 61 6e 64 0a 2a 2a 20   is used and.** 
2ee39 2a 70 70 4f 72 64 65 72 42 79 20 69 73 20 73 65  *ppOrderBy is se
2ee3a 74 20 74 6f 20 4e 55 4c 4c 2e 20 20 54 68 69 73  t to NULL.  This
2ee3b 20 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74   is an optimizat
2ee3c 69 6f 6e 20 74 68 61 74 20 70 72 65 76 65 6e 74  ion that prevent
2ee3d 73 20 61 6e 0a 2a 2a 20 75 6e 6e 65 63 65 73 73  s an.** unnecess
2ee3e 61 72 79 20 73 6f 72 74 20 6f 66 20 74 68 65 20  ary sort of the 
2ee3f 72 65 73 75 6c 74 20 73 65 74 20 69 66 20 61 6e  result set if an
2ee40 20 69 6e 64 65 78 20 61 70 70 72 6f 70 72 69 61   index appropria
2ee41 74 65 20 66 6f 72 20 74 68 65 0a 2a 2a 20 4f 52  te for the.** OR
2ee42 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 61 6c  DER BY clause al
2ee43 72 65 61 64 79 20 65 78 69 73 74 73 2e 0a 2a 2a  ready exists..**
2ee44 0a 2a 2a 20 49 66 20 74 68 65 20 77 68 65 72 65  .** If the where
2ee45 20 63 6c 61 75 73 65 20 6c 6f 6f 70 73 20 63 61   clause loops ca
2ee46 6e 6e 6f 74 20 62 65 20 61 72 72 61 6e 67 65 64  nnot be arranged
2ee47 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 20   to provide the 
2ee48 63 6f 72 72 65 63 74 0a 2a 2a 20 6f 75 74 70 75  correct.** outpu
2ee49 74 20 6f 72 64 65 72 2c 20 74 68 65 6e 20 74 68  t order, then th
2ee4a 65 20 2a 70 70 4f 72 64 65 72 42 79 20 69 73 20  e *ppOrderBy is 
2ee4b 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51  unchanged..*/.SQ
2ee4c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 57 68 65  LITE_PRIVATE Whe
2ee4d 72 65 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 57  reInfo *sqlite3W
2ee4e 68 65 72 65 42 65 67 69 6e 28 0a 20 20 50 61 72  hereBegin(.  Par
2ee4f 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2ee50 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
2ee51 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72   context */.  Sr
2ee52 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c  cList *pTabList,
2ee53 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f 66      /* A list of
2ee54 20 61 6c 6c 20 74 61 62 6c 65 73 20 74 6f 20 62   all tables to b
2ee55 65 20 73 63 61 6e 6e 65 64 20 2a 2f 0a 20 20 45  e scanned */.  E
2ee56 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20  xpr *pWhere,    
2ee57 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52       /* The WHER
2ee58 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78  E clause */.  Ex
2ee59 70 72 4c 69 73 74 20 2a 2a 70 70 4f 72 64 65 72  prList **ppOrder
2ee5a 42 79 2c 20 2f 2a 20 41 6e 20 4f 52 44 45 52 20  By, /* An ORDER 
2ee5b 42 59 20 63 6c 61 75 73 65 2c 20 6f 72 20 4e 55  BY clause, or NU
2ee5c 4c 4c 20 2a 2f 0a 20 20 75 31 36 20 77 63 74 72  LL */.  u16 wctr
2ee5d 6c 46 6c 61 67 73 20 20 20 20 20 20 20 20 2f 2a  lFlags        /*
2ee5e 20 4f 6e 65 20 6f 66 20 74 68 65 20 57 48 45 52   One of the WHER
2ee5f 45 5f 2a 20 66 6c 61 67 73 20 64 65 66 69 6e 65  E_* flags define
2ee60 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68  d in sqliteInt.h
2ee61 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20   */.){.  int i; 
2ee62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ee63 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2ee64 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79  ter */.  int nBy
2ee65 74 65 57 49 6e 66 6f 3b 20 20 20 20 20 20 20 20  teWInfo;        
2ee66 20 20 20 20 2f 2a 20 4e 75 6d 2e 20 62 79 74 65      /* Num. byte
2ee67 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  s allocated for 
2ee68 57 68 65 72 65 49 6e 66 6f 20 73 74 72 75 63 74  WhereInfo struct
2ee69 20 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f 20   */.  WhereInfo 
2ee6a 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20 20 20 20  *pWInfo;        
2ee6b 20 2f 2a 20 57 69 6c 6c 20 62 65 63 6f 6d 65 20   /* Will become 
2ee6c 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
2ee6d 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   of this functio
2ee6e 6e 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d  n */.  Vdbe *v =
2ee6f 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20   pParse->pVdbe; 
2ee70 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c    /* The virtual
2ee71 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65   database engine
2ee72 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f   */.  Bitmask no
2ee73 74 52 65 61 64 79 3b 20 20 20 20 20 20 20 20 20  tReady;         
2ee74 20 2f 2a 20 43 75 72 73 6f 72 73 20 74 68 61 74   /* Cursors that
2ee75 20 61 72 65 20 6e 6f 74 20 79 65 74 20 70 6f 73   are not yet pos
2ee76 69 74 69 6f 6e 65 64 20 2a 2f 0a 20 20 57 68 65  itioned */.  Whe
2ee77 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b  reMaskSet *pMask
2ee78 53 65 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 65  Set;    /* The e
2ee79 78 70 72 65 73 73 69 6f 6e 20 6d 61 73 6b 20 73  xpression mask s
2ee7a 65 74 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61  et */.  WhereCla
2ee7b 75 73 65 20 2a 70 57 43 3b 20 20 20 20 20 20 20  use *pWC;       
2ee7c 20 20 20 20 20 20 20 20 2f 2a 20 44 65 63 6f 6d          /* Decom
2ee7d 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65 20  position of the 
2ee7e 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
2ee7f 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
2ee80 5f 69 74 65 6d 20 2a 70 54 61 62 49 74 65 6d 3b  _item *pTabItem;
2ee81 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 65 6e    /* A single en
2ee82 74 72 79 20 66 72 6f 6d 20 70 54 61 62 4c 69 73  try from pTabLis
2ee83 74 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76 65  t */.  WhereLeve
2ee84 6c 20 2a 70 4c 65 76 65 6c 3b 20 20 20 20 20 20  l *pLevel;      
2ee85 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67         /* A sing
2ee86 6c 65 20 6c 65 76 65 6c 20 69 6e 20 74 68 65 20  le level in the 
2ee87 70 57 49 6e 66 6f 20 6c 69 73 74 20 2a 2f 0a 20  pWInfo list */. 
2ee88 20 69 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20 20   int iFrom;     
2ee89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ee8a 20 2f 2a 20 46 69 72 73 74 20 75 6e 75 73 65 64   /* First unused
2ee8b 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 65 6c 65   FROM clause ele
2ee8c 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 61 6e  ment */.  int an
2ee8d 64 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20  dFlags;         
2ee8e 20 20 20 20 20 2f 2a 20 41 4e 44 2d 65 64 20 63       /* AND-ed c
2ee8f 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 61 6c  ombination of al
2ee90 6c 20 70 57 43 2d 3e 61 5b 5d 2e 77 74 46 6c 61  l pWC->a[].wtFla
2ee91 67 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  gs */.  sqlite3 
2ee92 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  *db;            
2ee93 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63     /* Database c
2ee94 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20  onnection */..  
2ee95 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66  /* The number of
2ee96 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 46   tables in the F
2ee97 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 6c 69  ROM clause is li
2ee98 6d 69 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d  mited by the num
2ee99 62 65 72 20 6f 66 0a 20 20 2a 2a 20 62 69 74 73  ber of.  ** bits
2ee9a 20 69 6e 20 61 20 42 69 74 6d 61 73 6b 20 0a 20   in a Bitmask . 
2ee9b 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 4c 69   */.  if( pTabLi
2ee9c 73 74 2d 3e 6e 53 72 63 3e 42 4d 53 20 29 7b 0a  st->nSrc>BMS ){.
2ee9d 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2ee9e 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 74 20  Msg(pParse, "at 
2ee9f 6d 6f 73 74 20 25 64 20 74 61 62 6c 65 73 20 69  most %d tables i
2eea0 6e 20 61 20 6a 6f 69 6e 22 2c 20 42 4d 53 29 3b  n a join", BMS);
2eea1 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
2eea2 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74   }..  /* Allocat
2eea3 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65  e and initialize
2eea4 20 74 68 65 20 57 68 65 72 65 49 6e 66 6f 20 73   the WhereInfo s
2eea5 74 72 75 63 74 75 72 65 20 74 68 61 74 20 77 69  tructure that wi
2eea6 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 0a 20 20  ll become the.  
2eea7 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2e  ** return value.
2eea8 20 41 20 73 69 6e 67 6c 65 20 61 6c 6c 6f 63 61   A single alloca
2eea9 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
2eeaa 73 74 6f 72 65 20 74 68 65 20 57 68 65 72 65 49  store the WhereI
2eeab 6e 66 6f 0a 20 20 2a 2a 20 73 74 72 75 63 74 2c  nfo.  ** struct,
2eeac 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
2eead 20 57 68 65 72 65 49 6e 66 6f 2e 61 5b 5d 2c 20   WhereInfo.a[], 
2eeae 74 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 20  the WhereClause 
2eeaf 73 74 72 75 63 74 75 72 65 0a 20 20 2a 2a 20 61  structure.  ** a
2eeb0 6e 64 20 74 68 65 20 57 68 65 72 65 4d 61 73 6b  nd the WhereMask
2eeb1 53 65 74 20 73 74 72 75 63 74 75 72 65 2e 20 53  Set structure. S
2eeb2 69 6e 63 65 20 57 68 65 72 65 43 6c 61 75 73 65  ince WhereClause
2eeb3 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 38 2d 62   contains an 8-b
2eeb4 79 74 65 0a 20 20 2a 2a 20 66 69 65 6c 64 20 28  yte.  ** field (
2eeb5 74 79 70 65 20 42 69 74 6d 61 73 6b 29 20 69 74  type Bitmask) it
2eeb6 20 6d 75 73 74 20 62 65 20 61 6c 69 67 6e 65 64   must be aligned
2eeb7 20 6f 6e 20 61 6e 20 38 2d 62 79 74 65 20 62 6f   on an 8-byte bo
2eeb8 75 6e 64 61 72 79 20 6f 6e 0a 20 20 2a 2a 20 73  undary on.  ** s
2eeb9 6f 6d 65 20 61 72 63 68 69 74 65 63 74 75 72 65  ome architecture
2eeba 73 2e 20 48 65 6e 63 65 20 74 68 65 20 52 4f 55  s. Hence the ROU
2eebb 4e 44 38 28 29 20 62 65 6c 6f 77 2e 0a 20 20 2a  ND8() below..  *
2eebc 2f 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  /.  db = pParse-
2eebd 3e 64 62 3b 0a 20 20 6e 42 79 74 65 57 49 6e 66  >db;.  nByteWInf
2eebe 6f 20 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f  o = ROUND8(sizeo
2eebf 66 28 57 68 65 72 65 49 6e 66 6f 29 2b 28 70 54  f(WhereInfo)+(pT
2eec0 61 62 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 29 2a  abList->nSrc-1)*
2eec1 73 69 7a 65 6f 66 28 57 68 65 72 65 4c 65 76 65  sizeof(WhereLeve
2eec2 6c 29 29 3b 0a 20 20 70 57 49 6e 66 6f 20 3d 20  l));.  pWInfo = 
2eec3 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
2eec4 65 72 6f 28 64 62 2c 20 0a 20 20 20 20 20 20 6e  ero(db, .      n
2eec5 42 79 74 65 57 49 6e 66 6f 20 2b 20 0a 20 20 20  ByteWInfo + .   
2eec6 20 20 20 73 69 7a 65 6f 66 28 57 68 65 72 65 43     sizeof(WhereC
2eec7 6c 61 75 73 65 29 20 2b 0a 20 20 20 20 20 20 73  lause) +.      s
2eec8 69 7a 65 6f 66 28 57 68 65 72 65 4d 61 73 6b 53  izeof(WhereMaskS
2eec9 65 74 29 0a 20 20 29 3b 0a 20 20 69 66 28 20 64  et).  );.  if( d
2eeca 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2eecb 29 7b 0a 20 20 20 20 67 6f 74 6f 20 77 68 65 72  ){.    goto wher
2eecc 65 42 65 67 69 6e 45 72 72 6f 72 3b 0a 20 20 7d  eBeginError;.  }
2eecd 0a 20 20 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65  .  pWInfo->nLeve
2eece 6c 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53  l = pTabList->nS
2eecf 72 63 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e 70 50  rc;.  pWInfo->pP
2eed0 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20  arse = pParse;. 
2eed1 20 70 57 49 6e 66 6f 2d 3e 70 54 61 62 4c 69 73   pWInfo->pTabLis
2eed2 74 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a 20 20  t = pTabList;.  
2eed3 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 20 3d  pWInfo->iBreak =
2eed4 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
2eed5 4c 61 62 65 6c 28 76 29 3b 0a 20 20 70 57 49 6e  Label(v);.  pWIn
2eed6 66 6f 2d 3e 70 57 43 20 3d 20 70 57 43 20 3d 20  fo->pWC = pWC = 
2eed7 28 57 68 65 72 65 43 6c 61 75 73 65 20 2a 29 26  (WhereClause *)&
2eed8 28 28 75 38 20 2a 29 70 57 49 6e 66 6f 29 5b 6e  ((u8 *)pWInfo)[n
2eed9 42 79 74 65 57 49 6e 66 6f 5d 3b 0a 20 20 70 57  ByteWInfo];.  pW
2eeda 49 6e 66 6f 2d 3e 77 63 74 72 6c 46 6c 61 67 73  Info->wctrlFlags
2eedb 20 3d 20 77 63 74 72 6c 46 6c 61 67 73 3b 0a 20   = wctrlFlags;. 
2eedc 20 70 4d 61 73 6b 53 65 74 20 3d 20 28 57 68 65   pMaskSet = (Whe
2eedd 72 65 4d 61 73 6b 53 65 74 2a 29 26 70 57 43 5b  reMaskSet*)&pWC[
2eede 31 5d 3b 0a 0a 20 20 2f 2a 20 53 70 6c 69 74 20  1];..  /* Split 
2eedf 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2eee0 20 69 6e 74 6f 20 73 65 70 61 72 61 74 65 20 73   into separate s
2eee1 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20 77 68  ubexpressions wh
2eee2 65 72 65 20 65 61 63 68 0a 20 20 2a 2a 20 73 75  ere each.  ** su
2eee3 62 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73  bexpression is s
2eee4 65 70 61 72 61 74 65 64 20 62 79 20 61 6e 20 41  eparated by an A
2eee5 4e 44 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20 2a  ND operator..  *
2eee6 2f 0a 20 20 69 6e 69 74 4d 61 73 6b 53 65 74 28  /.  initMaskSet(
2eee7 70 4d 61 73 6b 53 65 74 29 3b 0a 20 20 77 68 65  pMaskSet);.  whe
2eee8 72 65 43 6c 61 75 73 65 49 6e 69 74 28 70 57 43  reClauseInit(pWC
2eee9 2c 20 70 50 61 72 73 65 2c 20 70 4d 61 73 6b 53  , pParse, pMaskS
2eeea 65 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  et);.  sqlite3Ex
2eeeb 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28  prCodeConstants(
2eeec 70 50 61 72 73 65 2c 20 70 57 68 65 72 65 29 3b  pParse, pWhere);
2eeed 0a 20 20 77 68 65 72 65 53 70 6c 69 74 28 70 57  .  whereSplit(pW
2eeee 43 2c 20 70 57 68 65 72 65 2c 20 54 4b 5f 41 4e  C, pWhere, TK_AN
2eeef 44 29 3b 0a 20 20 20 20 0a 20 20 2f 2a 20 53 70  D);.    .  /* Sp
2eef0 65 63 69 61 6c 20 63 61 73 65 3a 20 61 20 57 48  ecial case: a WH
2eef1 45 52 45 20 63 6c 61 75 73 65 20 74 68 61 74 20  ERE clause that 
2eef2 69 73 20 63 6f 6e 73 74 61 6e 74 2e 20 20 45 76  is constant.  Ev
2eef3 61 6c 75 61 74 65 20 74 68 65 0a 20 20 2a 2a 20  aluate the.  ** 
2eef4 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 65  expression and e
2eef5 69 74 68 65 72 20 6a 75 6d 70 20 6f 76 65 72 20  ither jump over 
2eef6 61 6c 6c 20 6f 66 20 74 68 65 20 63 6f 64 65 20  all of the code 
2eef7 6f 72 20 66 61 6c 6c 20 74 68 72 75 2e 0a 20 20  or fall thru..  
2eef8 2a 2f 0a 20 20 69 66 28 20 70 57 68 65 72 65 20  */.  if( pWhere 
2eef9 26 26 20 28 70 54 61 62 4c 69 73 74 2d 3e 6e 53  && (pTabList->nS
2eefa 72 63 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  rc==0 || sqlite3
2eefb 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f  ExprIsConstantNo
2eefc 74 4a 6f 69 6e 28 70 57 68 65 72 65 29 29 20 29  tJoin(pWhere)) )
2eefd 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  {.    sqlite3Exp
2eefe 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c  rIfFalse(pParse,
2eeff 20 70 57 68 65 72 65 2c 20 70 57 49 6e 66 6f 2d   pWhere, pWInfo-
2ef00 3e 69 42 72 65 61 6b 2c 20 53 51 4c 49 54 45 5f  >iBreak, SQLITE_
2ef01 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20  JUMPIFNULL);.   
2ef02 20 70 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 7d   pWhere = 0;.  }
2ef03 0a 0a 20 20 2f 2a 20 41 73 73 69 67 6e 20 61 20  ..  /* Assign a 
2ef04 62 69 74 20 66 72 6f 6d 20 74 68 65 20 62 69 74  bit from the bit
2ef05 6d 61 73 6b 20 74 6f 20 65 76 65 72 79 20 74 65  mask to every te
2ef06 72 6d 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  rm in the FROM c
2ef07 6c 61 75 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  lause..  **.  **
2ef08 20 57 68 65 6e 20 61 73 73 69 67 6e 69 6e 67 20   When assigning 
2ef09 62 69 74 6d 61 73 6b 20 76 61 6c 75 65 73 20 74  bitmask values t
2ef0a 6f 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 63 75  o FROM clause cu
2ef0b 72 73 6f 72 73 2c 20 69 74 20 6d 75 73 74 20 62  rsors, it must b
2ef0c 65 0a 20 20 2a 2a 20 74 68 65 20 63 61 73 65 20  e.  ** the case 
2ef0d 74 68 61 74 20 69 66 20 58 20 69 73 20 74 68 65  that if X is the
2ef0e 20 62 69 74 6d 61 73 6b 20 66 6f 72 20 74 68 65   bitmask for the
2ef0f 20 4e 2d 74 68 20 46 52 4f 4d 20 63 6c 61 75 73   N-th FROM claus
2ef10 65 20 74 65 72 6d 20 74 68 65 6e 0a 20 20 2a 2a  e term then.  **
2ef11 20 74 68 65 20 62 69 74 6d 61 73 6b 20 66 6f 72   the bitmask for
2ef12 20 61 6c 6c 20 46 52 4f 4d 20 63 6c 61 75 73 65   all FROM clause
2ef13 20 74 65 72 6d 73 20 74 6f 20 74 68 65 20 6c 65   terms to the le
2ef14 66 74 20 6f 66 20 74 68 65 20 4e 2d 74 68 20 74  ft of the N-th t
2ef15 65 72 6d 0a 20 20 2a 2a 20 69 73 20 28 58 2d 31  erm.  ** is (X-1
2ef16 29 2e 20 20 20 41 6e 20 65 78 70 72 65 73 73 69  ).   An expressi
2ef17 6f 6e 20 66 72 6f 6d 20 74 68 65 20 4f 4e 20 63  on from the ON c
2ef18 6c 61 75 73 65 20 6f 66 20 61 20 4c 45 46 54 20  lause of a LEFT 
2ef19 4a 4f 49 4e 20 63 61 6e 20 75 73 65 0a 20 20 2a  JOIN can use.  *
2ef1a 2a 20 69 74 73 20 45 78 70 72 2e 69 52 69 67 68  * its Expr.iRigh
2ef1b 74 4a 6f 69 6e 54 61 62 6c 65 20 76 61 6c 75 65  tJoinTable value
2ef1c 20 74 6f 20 66 69 6e 64 20 74 68 65 20 62 69 74   to find the bit
2ef1d 6d 61 73 6b 20 6f 66 20 74 68 65 20 72 69 67 68  mask of the righ
2ef1e 74 20 74 61 62 6c 65 0a 20 20 2a 2a 20 6f 66 20  t table.  ** of 
2ef1f 74 68 65 20 6a 6f 69 6e 2e 20 20 53 75 62 74 72  the join.  Subtr
2ef20 61 63 74 69 6e 67 20 6f 6e 65 20 66 72 6f 6d 20  acting one from 
2ef21 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20  the right table 
2ef22 62 69 74 6d 61 73 6b 20 67 69 76 65 73 20 61 0a  bitmask gives a.
2ef23 20 20 2a 2a 20 62 69 74 6d 61 73 6b 20 66 6f 72    ** bitmask for
2ef24 20 61 6c 6c 20 74 61 62 6c 65 73 20 74 6f 20 74   all tables to t
2ef25 68 65 20 6c 65 66 74 20 6f 66 20 74 68 65 20 6a  he left of the j
2ef26 6f 69 6e 2e 20 20 4b 6e 6f 77 69 6e 67 20 74 68  oin.  Knowing th
2ef27 65 20 62 69 74 6d 61 73 6b 0a 20 20 2a 2a 20 66  e bitmask.  ** f
2ef28 6f 72 20 61 6c 6c 20 74 61 62 6c 65 73 20 74 6f  or all tables to
2ef29 20 74 68 65 20 6c 65 66 74 20 6f 66 20 61 20 6c   the left of a l
2ef2a 65 66 74 20 6a 6f 69 6e 20 69 73 20 69 6d 70 6f  eft join is impo
2ef2b 72 74 61 6e 74 2e 20 20 54 69 63 6b 65 74 20 23  rtant.  Ticket #
2ef2c 33 30 31 35 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  3015..  **.  ** 
2ef2d 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 57 68  Configure the Wh
2ef2e 65 72 65 43 6c 61 75 73 65 2e 76 6d 61 73 6b 20  ereClause.vmask 
2ef2f 76 61 72 69 61 62 6c 65 20 73 6f 20 74 68 61 74  variable so that
2ef30 20 62 69 74 73 20 74 68 61 74 20 63 6f 72 72 65   bits that corre
2ef31 73 70 6f 6e 64 0a 20 20 2a 2a 20 74 6f 20 76 69  spond.  ** to vi
2ef32 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75 72 73  rtual table curs
2ef33 6f 72 73 20 61 72 65 20 73 65 74 2e 20 54 68 69  ors are set. Thi
2ef34 73 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 6c  s is used to sel
2ef35 65 63 74 69 76 65 6c 79 20 64 69 73 61 62 6c 65  ectively disable
2ef36 20 0a 20 20 2a 2a 20 74 68 65 20 4f 52 2d 74 6f   .  ** the OR-to
2ef37 2d 49 4e 20 74 72 61 6e 73 66 6f 72 6d 61 74 69  -IN transformati
2ef38 6f 6e 20 69 6e 20 65 78 70 72 41 6e 61 6c 79 7a  on in exprAnalyz
2ef39 65 4f 72 54 65 72 6d 28 29 2e 20 49 74 20 69 73  eOrTerm(). It is
2ef3a 20 6e 6f 74 20 68 65 6c 70 66 75 6c 20 0a 20 20   not helpful .  
2ef3b 2a 2a 20 77 69 74 68 20 76 69 72 74 75 61 6c 20  ** with virtual 
2ef3c 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 61  tables..  */.  a
2ef3d 73 73 65 72 74 28 20 70 57 43 2d 3e 76 6d 61 73  ssert( pWC->vmas
2ef3e 6b 3d 3d 30 20 26 26 20 70 4d 61 73 6b 53 65 74  k==0 && pMaskSet
2ef3f 2d 3e 6e 3d 3d 30 20 29 3b 0a 20 20 66 6f 72 28  ->n==0 );.  for(
2ef40 69 3d 30 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d  i=0; i<pTabList-
2ef41 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nSrc; i++){.   
2ef42 20 63 72 65 61 74 65 4d 61 73 6b 28 70 4d 61 73   createMask(pMas
2ef43 6b 53 65 74 2c 20 70 54 61 62 4c 69 73 74 2d 3e  kSet, pTabList->
2ef44 61 5b 69 5d 2e 69 43 75 72 73 6f 72 29 3b 0a 23  a[i].iCursor);.#
2ef45 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2ef46 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
2ef47 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70      if( ALWAYS(p
2ef48 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 54  TabList->a[i].pT
2ef49 61 62 29 20 26 26 20 49 73 56 69 72 74 75 61 6c  ab) && IsVirtual
2ef4a 28 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e  (pTabList->a[i].
2ef4b 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 70  pTab) ){.      p
2ef4c 57 43 2d 3e 76 6d 61 73 6b 20 7c 3d 20 28 28 42  WC->vmask |= ((B
2ef4d 69 74 6d 61 73 6b 29 31 20 3c 3c 20 69 29 3b 0a  itmask)1 << i);.
2ef4e 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
2ef4f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
2ef50 20 20 7b 0a 20 20 20 20 42 69 74 6d 61 73 6b 20    {.    Bitmask 
2ef51 74 6f 54 68 65 4c 65 66 74 20 3d 20 30 3b 0a 20  toTheLeft = 0;. 
2ef52 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54     for(i=0; i<pT
2ef53 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b  abList->nSrc; i+
2ef54 2b 29 7b 0a 20 20 20 20 20 20 42 69 74 6d 61 73  +){.      Bitmas
2ef55 6b 20 6d 20 3d 20 67 65 74 4d 61 73 6b 28 70 4d  k m = getMask(pM
2ef56 61 73 6b 53 65 74 2c 20 70 54 61 62 4c 69 73 74  askSet, pTabList
2ef57 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 29 3b  ->a[i].iCursor);
2ef58 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28  .      assert( (
2ef59 6d 2d 31 29 3d 3d 74 6f 54 68 65 4c 65 66 74 20  m-1)==toTheLeft 
2ef5a 29 3b 0a 20 20 20 20 20 20 74 6f 54 68 65 4c 65  );.      toTheLe
2ef5b 66 74 20 7c 3d 20 6d 3b 0a 20 20 20 20 7d 0a 20  ft |= m;.    }. 
2ef5c 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
2ef5d 41 6e 61 6c 79 7a 65 20 61 6c 6c 20 6f 66 20 74  Analyze all of t
2ef5e 68 65 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e  he subexpression
2ef5f 73 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 65 78  s.  Note that ex
2ef60 70 72 41 6e 61 6c 79 7a 65 28 29 20 6d 69 67 68  prAnalyze() migh
2ef61 74 0a 20 20 2a 2a 20 61 64 64 20 6e 65 77 20 76  t.  ** add new v
2ef62 69 72 74 75 61 6c 20 74 65 72 6d 73 20 6f 6e 74  irtual terms ont
2ef63 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  o the end of the
2ef64 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20   WHERE clause.  
2ef65 57 65 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 77  We do not.  ** w
2ef66 61 6e 74 20 74 6f 20 61 6e 61 6c 79 7a 65 20 74  ant to analyze t
2ef67 68 65 73 65 20 76 69 72 74 75 61 6c 20 74 65 72  hese virtual ter
2ef68 6d 73 2c 20 73 6f 20 73 74 61 72 74 20 61 6e 61  ms, so start ana
2ef69 6c 79 7a 69 6e 67 20 61 74 20 74 68 65 20 65 6e  lyzing at the en
2ef6a 64 0a 20 20 2a 2a 20 61 6e 64 20 77 6f 72 6b 20  d.  ** and work 
2ef6b 66 6f 72 77 61 72 64 20 73 6f 20 74 68 61 74 20  forward so that 
2ef6c 74 68 65 20 61 64 64 65 64 20 76 69 72 74 75 61  the added virtua
2ef6d 6c 20 74 65 72 6d 73 20 61 72 65 20 6e 65 76 65  l terms are neve
2ef6e 72 20 70 72 6f 63 65 73 73 65 64 2e 0a 20 20 2a  r processed..  *
2ef6f 2f 0a 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 41  /.  exprAnalyzeA
2ef70 6c 6c 28 70 54 61 62 4c 69 73 74 2c 20 70 57 43  ll(pTabList, pWC
2ef71 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  );.  if( db->mal
2ef72 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
2ef73 20 67 6f 74 6f 20 77 68 65 72 65 42 65 67 69 6e   goto whereBegin
2ef74 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  Error;.  }..  /*
2ef75 20 43 68 6f 73 65 20 74 68 65 20 62 65 73 74 20   Chose the best 
2ef76 69 6e 64 65 78 20 74 6f 20 75 73 65 20 66 6f 72  index to use for
2ef77 20 65 61 63 68 20 74 61 62 6c 65 20 69 6e 20 74   each table in t
2ef78 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a  he FROM clause..
2ef79 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 6c    **.  ** This l
2ef7a 6f 6f 70 20 66 69 6c 6c 73 20 69 6e 20 74 68 65  oop fills in the
2ef7b 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64   following field
2ef7c 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 70  s:.  **.  **   p
2ef7d 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 70 49 64 78 20  WInfo->a[].pIdx 
2ef7e 20 20 20 20 20 54 68 65 20 69 6e 64 65 78 20 74       The index t
2ef7f 6f 20 75 73 65 20 66 6f 72 20 74 68 69 73 20 6c  o use for this l
2ef80 65 76 65 6c 20 6f 66 20 74 68 65 20 6c 6f 6f 70  evel of the loop
2ef81 2e 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d  ..  **   pWInfo-
2ef82 3e 61 5b 5d 2e 77 73 46 6c 61 67 73 20 20 20 57  >a[].wsFlags   W
2ef83 48 45 52 45 5f 78 78 78 20 66 6c 61 67 73 20 61  HERE_xxx flags a
2ef84 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70  ssociated with p
2ef85 49 64 78 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66  Idx.  **   pWInf
2ef86 6f 2d 3e 61 5b 5d 2e 6e 45 71 20 20 20 20 20 20  o->a[].nEq      
2ef87 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 3d   The number of =
2ef88 3d 20 61 6e 64 20 49 4e 20 63 6f 6e 73 74 72 61  = and IN constra
2ef89 69 6e 74 73 0a 20 20 2a 2a 20 20 20 70 57 49 6e  ints.  **   pWIn
2ef8a 66 6f 2d 3e 61 5b 5d 2e 69 46 72 6f 6d 20 20 20  fo->a[].iFrom   
2ef8b 20 20 57 68 69 63 68 20 74 65 72 6d 20 6f 66 20    Which term of 
2ef8c 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2ef8d 69 73 20 62 65 69 6e 67 20 63 6f 64 65 64 0a 20  is being coded. 
2ef8e 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b   **   pWInfo->a[
2ef8f 5d 2e 69 54 61 62 43 75 72 20 20 20 54 68 65 20  ].iTabCur   The 
2ef90 56 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72 20  VDBE cursor for 
2ef91 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62  the database tab
2ef92 6c 65 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f  le.  **   pWInfo
2ef93 2d 3e 61 5b 5d 2e 69 49 64 78 43 75 72 20 20 20  ->a[].iIdxCur   
2ef94 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20  The VDBE cursor 
2ef95 66 6f 72 20 74 68 65 20 69 6e 64 65 78 0a 20 20  for the index.  
2ef96 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d  **   pWInfo->a[]
2ef97 2e 70 54 65 72 6d 20 20 20 20 20 57 68 65 6e 20  .pTerm     When 
2ef98 77 73 46 6c 61 67 73 3d 3d 57 4f 5f 4f 52 2c 20  wsFlags==WO_OR, 
2ef99 74 68 65 20 4f 52 2d 63 6c 61 75 73 65 20 74 65  the OR-clause te
2ef9a 72 6d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69  rm.  **.  ** Thi
2ef9b 73 20 6c 6f 6f 70 20 61 6c 73 6f 20 66 69 67 75  s loop also figu
2ef9c 72 65 73 20 6f 75 74 20 74 68 65 20 6e 65 73 74  res out the nest
2ef9d 69 6e 67 20 6f 72 64 65 72 20 6f 66 20 74 61 62  ing order of tab
2ef9e 6c 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 0a  les in the FROM.
2ef9f 20 20 2a 2a 20 63 6c 61 75 73 65 2e 0a 20 20 2a    ** clause..  *
2efa0 2f 0a 20 20 6e 6f 74 52 65 61 64 79 20 3d 20 7e  /.  notReady = ~
2efa1 28 42 69 74 6d 61 73 6b 29 30 3b 0a 20 20 70 54  (Bitmask)0;.  pT
2efa2 61 62 49 74 65 6d 20 3d 20 70 54 61 62 4c 69 73  abItem = pTabLis
2efa3 74 2d 3e 61 3b 0a 20 20 70 4c 65 76 65 6c 20 3d  t->a;.  pLevel =
2efa4 20 70 57 49 6e 66 6f 2d 3e 61 3b 0a 20 20 61 6e   pWInfo->a;.  an
2efa5 64 46 6c 61 67 73 20 3d 20 7e 30 3b 0a 20 20 57  dFlags = ~0;.  W
2efa6 48 45 52 45 54 52 41 43 45 28 28 22 2a 2a 2a 20  HERETRACE(("*** 
2efa7 4f 70 74 69 6d 69 7a 65 72 20 53 74 61 72 74 20  Optimizer Start 
2efa8 2a 2a 2a 5c 6e 22 29 29 3b 0a 20 20 66 6f 72 28  ***\n"));.  for(
2efa9 69 3d 69 46 72 6f 6d 3d 30 2c 20 70 4c 65 76 65  i=iFrom=0, pLeve
2efaa 6c 3d 70 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c 70  l=pWInfo->a; i<p
2efab 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69  TabList->nSrc; i
2efac 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20  ++, pLevel++){. 
2efad 20 20 20 57 68 65 72 65 43 6f 73 74 20 62 65 73     WhereCost bes
2efae 74 50 6c 61 6e 3b 20 20 20 20 20 20 20 20 20 2f  tPlan;         /
2efaf 2a 20 4d 6f 73 74 20 65 66 66 69 63 69 65 6e 74  * Most efficient
2efb0 20 70 6c 61 6e 20 73 65 65 6e 20 73 6f 20 66 61   plan seen so fa
2efb1 72 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a  r */.    Index *
2efb2 70 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20  pIdx;           
2efb3 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 66 6f       /* Index fo
2efb4 72 20 46 52 4f 4d 20 74 61 62 6c 65 20 61 74 20  r FROM table at 
2efb5 70 54 61 62 49 74 65 6d 20 2a 2f 0a 20 20 20 20  pTabItem */.    
2efb6 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20  int j;          
2efb7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
2efb8 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
2efb9 46 52 4f 4d 20 74 61 62 6c 65 73 20 2a 2f 0a 20  FROM tables */. 
2efba 20 20 20 69 6e 74 20 62 65 73 74 4a 20 3d 20 2d     int bestJ = -
2efbb 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  1;             /
2efbc 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 6a  * The value of j
2efbd 20 2a 2f 0a 20 20 20 20 42 69 74 6d 61 73 6b 20   */.    Bitmask 
2efbe 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
2efbf 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 76      /* Bitmask v
2efc0 61 6c 75 65 20 66 6f 72 20 6a 20 6f 72 20 62 65  alue for j or be
2efc1 73 74 4a 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  stJ */.    int i
2efc2 73 4f 70 74 69 6d 61 6c 3b 20 20 20 20 20 20 20  sOptimal;       
2efc3 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74         /* Iterat
2efc4 6f 72 20 66 6f 72 20 6f 70 74 69 6d 61 6c 2f 6e  or for optimal/n
2efc5 6f 6e 2d 6f 70 74 69 6d 61 6c 20 73 65 61 72 63  on-optimal searc
2efc6 68 20 2a 2f 0a 0a 20 20 20 20 6d 65 6d 73 65 74  h */..    memset
2efc7 28 26 62 65 73 74 50 6c 61 6e 2c 20 30 2c 20 73  (&bestPlan, 0, s
2efc8 69 7a 65 6f 66 28 62 65 73 74 50 6c 61 6e 29 29  izeof(bestPlan))
2efc9 3b 0a 20 20 20 20 62 65 73 74 50 6c 61 6e 2e 72  ;.    bestPlan.r
2efca 43 6f 73 74 20 3d 20 53 51 4c 49 54 45 5f 42 49  Cost = SQLITE_BI
2efcb 47 5f 44 42 4c 3b 0a 0a 20 20 20 20 2f 2a 20 4c  G_DBL;..    /* L
2efcc 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20  oop through the 
2efcd 72 65 6d 61 69 6e 69 6e 67 20 65 6e 74 72 69 65  remaining entrie
2efce 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  s in the FROM cl
2efcf 61 75 73 65 20 74 6f 20 66 69 6e 64 20 74 68 65  ause to find the
2efd0 0a 20 20 20 20 2a 2a 20 6e 65 78 74 20 6e 65 73  .    ** next nes
2efd1 74 65 64 20 6c 6f 6f 70 2e 20 54 68 65 20 46 52  ted loop. The FR
2efd2 4f 4d 20 63 6c 61 75 73 65 20 65 6e 74 72 69 65  OM clause entrie
2efd3 73 20 6d 61 79 20 62 65 20 69 74 65 72 61 74 65  s may be iterate
2efd4 64 20 74 68 72 6f 75 67 68 0a 20 20 20 20 2a 2a  d through.    **
2efd5 20 65 69 74 68 65 72 20 6f 6e 63 65 20 6f 72 20   either once or 
2efd6 74 77 69 63 65 2e 20 0a 20 20 20 20 2a 2a 0a 20  twice. .    **. 
2efd7 20 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20     ** The first 
2efd8 69 74 65 72 61 74 69 6f 6e 2c 20 77 68 69 63 68  iteration, which
2efd9 20 69 73 20 61 6c 77 61 79 73 20 70 65 72 66 6f   is always perfo
2efda 72 6d 65 64 2c 20 73 65 61 72 63 68 65 73 20 66  rmed, searches f
2efdb 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 46 52  or the.    ** FR
2efdc 4f 4d 20 63 6c 61 75 73 65 20 65 6e 74 72 79 20  OM clause entry 
2efdd 74 68 61 74 20 70 65 72 6d 69 74 73 20 74 68 65  that permits the
2efde 20 6c 6f 77 65 73 74 2d 63 6f 73 74 2c 20 22 6f   lowest-cost, "o
2efdf 70 74 69 6d 61 6c 22 20 73 63 61 6e 2e 20 49 6e  ptimal" scan. In
2efe0 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 63 6f 6e  .    ** this con
2efe1 74 65 78 74 20 61 6e 20 6f 70 74 69 6d 61 6c 20  text an optimal 
2efe2 73 63 61 6e 20 69 73 20 6f 6e 65 20 74 68 61 74  scan is one that
2efe3 20 75 73 65 73 20 74 68 65 20 73 61 6d 65 20 73   uses the same s
2efe4 74 72 61 74 65 67 79 0a 20 20 20 20 2a 2a 20 66  trategy.    ** f
2efe5 6f 72 20 74 68 65 20 67 69 76 65 6e 20 46 52 4f  or the given FRO
2efe6 4d 20 63 6c 61 75 73 65 20 65 6e 74 72 79 20 61  M clause entry a
2efe7 73 20 77 6f 75 6c 64 20 62 65 20 73 65 6c 65 63  s would be selec
2efe8 74 65 64 20 69 66 20 74 68 65 20 65 6e 74 72 79  ted if the entry
2efe9 0a 20 20 20 20 2a 2a 20 77 65 72 65 20 75 73 65  .    ** were use
2efea 64 20 61 73 20 74 68 65 20 69 6e 6e 65 72 6d 6f  d as the innermo
2efeb 73 74 20 6e 65 73 74 65 64 20 6c 6f 6f 70 2e 20  st nested loop. 
2efec 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
2efed 20 61 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20   a table.    ** 
2efee 69 73 20 63 68 6f 73 65 6e 20 73 75 63 68 20 74  is chosen such t
2efef 68 61 74 20 74 68 65 20 63 6f 73 74 20 6f 66 20  hat the cost of 
2eff0 72 75 6e 6e 69 6e 67 20 74 68 61 74 20 74 61 62  running that tab
2eff1 6c 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 64  le cannot be red
2eff2 75 63 65 64 0a 20 20 20 20 2a 2a 20 62 79 20 77  uced.    ** by w
2eff3 61 69 74 69 6e 67 20 66 6f 72 20 6f 74 68 65 72  aiting for other
2eff4 20 74 61 62 6c 65 73 20 74 6f 20 72 75 6e 20 66   tables to run f
2eff5 69 72 73 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  irst..    **.   
2eff6 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 69   ** The second i
2eff7 74 65 72 61 74 69 6f 6e 20 69 73 20 6f 6e 6c 79  teration is only
2eff8 20 70 65 72 66 6f 72 6d 65 64 20 69 66 20 6e 6f   performed if no
2eff9 20 6f 70 74 69 6d 61 6c 20 73 63 61 6e 20 73 74   optimal scan st
2effa 72 61 74 65 67 69 65 73 0a 20 20 20 20 2a 2a 20  rategies.    ** 
2effb 77 65 72 65 20 66 6f 75 6e 64 20 62 79 20 74 68  were found by th
2effc 65 20 66 69 72 73 74 2e 20 54 68 69 73 20 69 74  e first. This it
2effd 65 72 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20  eration is used 
2effe 74 6f 20 73 65 61 72 63 68 20 66 6f 72 20 74 68  to search for th
2efff 65 0a 20 20 20 20 2a 2a 20 6c 6f 77 65 73 74 20  e.    ** lowest 
2f000 63 6f 73 74 20 73 63 61 6e 20 6f 76 65 72 61 6c  cost scan overal
2f001 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  l..    **.    **
2f002 20 50 72 65 76 69 6f 75 73 20 76 65 72 73 69 6f   Previous versio
2f003 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 70 65 72  ns of SQLite per
2f004 66 6f 72 6d 65 64 20 6f 6e 6c 79 20 74 68 65 20  formed only the 
2f005 73 65 63 6f 6e 64 20 69 74 65 72 61 74 69 6f 6e  second iteration
2f006 20 2d 0a 20 20 20 20 2a 2a 20 74 68 65 20 6e 65   -.    ** the ne
2f007 78 74 20 6f 75 74 65 72 6d 6f 73 74 20 6c 6f 6f  xt outermost loo
2f008 70 20 77 61 73 20 61 6c 77 61 79 73 20 74 68 61  p was always tha
2f009 74 20 77 69 74 68 20 74 68 65 20 6c 6f 77 65 73  t with the lowes
2f00a 74 20 6f 76 65 72 61 6c 6c 0a 20 20 20 20 2a 2a  t overall.    **
2f00b 20 63 6f 73 74 2e 20 48 6f 77 65 76 65 72 2c 20   cost. However, 
2f00c 74 68 69 73 20 6d 65 61 6e 74 20 74 68 61 74 20  this meant that 
2f00d 53 51 4c 69 74 65 20 63 6f 75 6c 64 20 73 65 6c  SQLite could sel
2f00e 65 63 74 20 74 68 65 20 77 72 6f 6e 67 20 70 6c  ect the wrong pl
2f00f 61 6e 0a 20 20 20 20 2a 2a 20 66 6f 72 20 73 63  an.    ** for sc
2f010 72 69 70 74 73 20 73 75 63 68 20 61 73 20 74 68  ripts such as th
2f011 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 20 20 20  e following:.   
2f012 20 2a 2a 20 20 20 0a 20 20 20 20 2a 2a 20 20 20   **   .    **   
2f013 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28  CREATE TABLE t1(
2f014 61 2c 20 62 29 3b 20 0a 20 20 20 20 2a 2a 20 20  a, b); .    **  
2f015 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 32   CREATE TABLE t2
2f016 28 63 2c 20 64 29 3b 0a 20 20 20 20 2a 2a 20 20  (c, d);.    **  
2f017 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74   SELECT * FROM t
2f018 32 2c 20 74 31 20 57 48 45 52 45 20 74 32 2e 72  2, t1 WHERE t2.r
2f019 6f 77 69 64 20 3d 20 74 31 2e 61 3b 0a 20 20 20  owid = t1.a;.   
2f01a 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 62   **.    ** The b
2f01b 65 73 74 20 73 74 72 61 74 65 67 79 20 69 73 20  est strategy is 
2f01c 74 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75  to iterate throu
2f01d 67 68 20 74 61 62 6c 65 20 74 31 20 66 69 72 73  gh table t1 firs
2f01e 74 2e 20 48 6f 77 65 76 65 72 20 69 74 0a 20 20  t. However it.  
2f01f 20 20 2a 2a 20 69 73 20 6e 6f 74 20 70 6f 73 73    ** is not poss
2f020 69 62 6c 65 20 74 6f 20 64 65 74 65 72 6d 69 6e  ible to determin
2f021 65 20 74 68 69 73 20 77 69 74 68 20 61 20 73 69  e this with a si
2f022 6d 70 6c 65 20 67 72 65 65 64 79 20 61 6c 67 6f  mple greedy algo
2f023 72 69 74 68 6d 2e 0a 20 20 20 20 2a 2a 20 48 6f  rithm..    ** Ho
2f024 77 65 76 65 72 2c 20 73 69 6e 63 65 20 74 68 65  wever, since the
2f025 20 63 6f 73 74 20 6f 66 20 61 20 6c 69 6e 65 61   cost of a linea
2f026 72 20 73 63 61 6e 20 74 68 72 6f 75 67 68 20 74  r scan through t
2f027 61 62 6c 65 20 74 32 20 69 73 20 74 68 65 20 73  able t2 is the s
2f028 61 6d 65 20 0a 20 20 20 20 2a 2a 20 61 73 20 74  ame .    ** as t
2f029 68 65 20 63 6f 73 74 20 6f 66 20 61 20 6c 69 6e  he cost of a lin
2f02a 65 61 72 20 73 63 61 6e 20 74 68 72 6f 75 67 68  ear scan through
2f02b 20 74 61 62 6c 65 20 74 31 2c 20 61 20 73 69 6d   table t1, a sim
2f02c 70 6c 65 20 67 72 65 65 64 79 20 0a 20 20 20 20  ple greedy .    
2f02d 2a 2a 20 61 6c 67 6f 72 69 74 68 6d 20 6d 61 79  ** algorithm may
2f02e 20 63 68 6f 6f 73 65 20 74 6f 20 75 73 65 20 74   choose to use t
2f02f 32 20 66 6f 72 20 74 68 65 20 6f 75 74 65 72 20  2 for the outer 
2f030 6c 6f 6f 70 2c 20 77 68 69 63 68 20 69 73 20 61  loop, which is a
2f031 20 6d 75 63 68 0a 20 20 20 20 2a 2a 20 63 6f 73   much.    ** cos
2f032 74 6c 69 65 72 20 61 70 70 72 6f 61 63 68 2e 0a  tlier approach..
2f033 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69      */.    for(i
2f034 73 4f 70 74 69 6d 61 6c 3d 31 3b 20 69 73 4f 70  sOptimal=1; isOp
2f035 74 69 6d 61 6c 3e 3d 30 20 26 26 20 62 65 73 74  timal>=0 && best
2f036 4a 3c 30 3b 20 69 73 4f 70 74 69 6d 61 6c 2d 2d  J<0; isOptimal--
2f037 29 7b 0a 20 20 20 20 20 20 42 69 74 6d 61 73 6b  ){.      Bitmask
2f038 20 6d 61 73 6b 20 3d 20 28 69 73 4f 70 74 69 6d   mask = (isOptim
2f039 61 6c 20 3f 20 30 20 3a 20 6e 6f 74 52 65 61 64  al ? 0 : notRead
2f03a 79 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  y);.      assert
2f03b 28 20 28 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72  ( (pTabList->nSr
2f03c 63 2d 69 46 72 6f 6d 29 3e 31 20 7c 7c 20 69 73  c-iFrom)>1 || is
2f03d 4f 70 74 69 6d 61 6c 20 29 3b 0a 20 20 20 20 20  Optimal );.     
2f03e 20 66 6f 72 28 6a 3d 69 46 72 6f 6d 2c 20 70 54   for(j=iFrom, pT
2f03f 61 62 49 74 65 6d 3d 26 70 54 61 62 4c 69 73 74  abItem=&pTabList
2f040 2d 3e 61 5b 6a 5d 3b 20 6a 3c 70 54 61 62 4c 69  ->a[j]; j<pTabLi
2f041 73 74 2d 3e 6e 53 72 63 3b 20 6a 2b 2b 2c 20 70  st->nSrc; j++, p
2f042 54 61 62 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  TabItem++){.    
2f043 20 20 20 20 69 6e 74 20 64 6f 4e 6f 74 52 65 6f      int doNotReo
2f044 72 64 65 72 3b 20 20 20 20 2f 2a 20 54 72 75 65  rder;    /* True
2f045 20 69 66 20 74 68 69 73 20 74 61 62 6c 65 20 73   if this table s
2f046 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 72 65 6f  hould not be reo
2f047 72 64 65 72 65 64 20 2a 2f 0a 20 20 20 20 20 20  rdered */.      
2f048 20 20 57 68 65 72 65 43 6f 73 74 20 73 43 6f 73    WhereCost sCos
2f049 74 3b 20 20 20 20 20 2f 2a 20 43 6f 73 74 20 69  t;     /* Cost i
2f04a 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20  nformation from 
2f04b 62 65 73 74 5b 56 69 72 74 75 61 6c 5d 49 6e 64  best[Virtual]Ind
2f04c 65 78 28 29 20 2a 2f 0a 20 20 20 20 20 20 20 20  ex() */.        
2f04d 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
2f04e 42 79 3b 20 20 2f 2a 20 4f 52 44 45 52 20 42 59  By;  /* ORDER BY
2f04f 20 63 6c 61 75 73 65 20 66 6f 72 20 69 6e 64 65   clause for inde
2f050 78 20 74 6f 20 6f 70 74 69 6d 69 7a 65 20 2a 2f  x to optimize */
2f051 0a 20 20 0a 20 20 20 20 20 20 20 20 64 6f 4e 6f  .  .        doNo
2f052 74 52 65 6f 72 64 65 72 20 3d 20 20 28 70 54 61  tReorder =  (pTa
2f053 62 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20  bItem->jointype 
2f054 26 20 28 4a 54 5f 4c 45 46 54 7c 4a 54 5f 43 52  & (JT_LEFT|JT_CR
2f055 4f 53 53 29 29 21 3d 30 3b 0a 20 20 20 20 20 20  OSS))!=0;.      
2f056 20 20 69 66 28 20 6a 21 3d 69 46 72 6f 6d 20 26    if( j!=iFrom &
2f057 26 20 64 6f 4e 6f 74 52 65 6f 72 64 65 72 20 29  & doNotReorder )
2f058 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
2f059 6d 20 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61 73  m = getMask(pMas
2f05a 6b 53 65 74 2c 20 70 54 61 62 49 74 65 6d 2d 3e  kSet, pTabItem->
2f05b 69 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20  iCursor);.      
2f05c 20 20 69 66 28 20 28 6d 20 26 20 6e 6f 74 52 65    if( (m & notRe
2f05d 61 64 79 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ady)==0 ){.     
2f05e 20 20 20 20 20 69 66 28 20 6a 3d 3d 69 46 72 6f       if( j==iFro
2f05f 6d 20 29 20 69 46 72 6f 6d 2b 2b 3b 0a 20 20 20  m ) iFrom++;.   
2f060 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
2f061 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2f062 20 20 20 70 4f 72 64 65 72 42 79 20 3d 20 28 28     pOrderBy = ((
2f063 69 3d 3d 30 20 26 26 20 70 70 4f 72 64 65 72 42  i==0 && ppOrderB
2f064 79 20 29 3f 2a 70 70 4f 72 64 65 72 42 79 3a 30  y )?*ppOrderBy:0
2f065 29 3b 0a 20 20 0a 20 20 20 20 20 20 20 20 61 73  );.  .        as
2f066 73 65 72 74 28 20 70 54 61 62 49 74 65 6d 2d 3e  sert( pTabItem->
2f067 70 54 61 62 20 29 3b 0a 23 69 66 6e 64 65 66 20  pTab );.#ifndef 
2f068 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2f069 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 20  UALTABLE.       
2f06a 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70   if( IsVirtual(p
2f06b 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 29 20 29  TabItem->pTab) )
2f06c 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
2f06d 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
2f06e 2a 70 70 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 61  *pp = &pWInfo->a
2f06f 5b 6a 5d 2e 70 49 64 78 49 6e 66 6f 3b 0a 20 20  [j].pIdxInfo;.  
2f070 20 20 20 20 20 20 20 20 62 65 73 74 56 69 72 74          bestVirt
2f071 75 61 6c 49 6e 64 65 78 28 70 50 61 72 73 65 2c  ualIndex(pParse,
2f072 20 70 57 43 2c 20 70 54 61 62 49 74 65 6d 2c 20   pWC, pTabItem, 
2f073 6d 61 73 6b 2c 20 70 4f 72 64 65 72 42 79 2c 20  mask, pOrderBy, 
2f074 26 73 43 6f 73 74 2c 20 70 70 29 3b 0a 20 20 20  &sCost, pp);.   
2f075 20 20 20 20 20 7d 65 6c 73 65 20 0a 23 65 6e 64       }else .#end
2f076 69 66 0a 20 20 20 20 20 20 20 20 7b 0a 20 20 20  if.        {.   
2f077 20 20 20 20 20 20 20 62 65 73 74 42 74 72 65 65         bestBtree
2f078 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 57  Index(pParse, pW
2f079 43 2c 20 70 54 61 62 49 74 65 6d 2c 20 6d 61 73  C, pTabItem, mas
2f07a 6b 2c 20 70 4f 72 64 65 72 42 79 2c 20 26 73 43  k, pOrderBy, &sC
2f07b 6f 73 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ost);.        }.
2f07c 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2f07d 69 73 4f 70 74 69 6d 61 6c 20 7c 7c 20 28 73 43  isOptimal || (sC
2f07e 6f 73 74 2e 75 73 65 64 26 6e 6f 74 52 65 61 64  ost.used&notRead
2f07f 79 29 3d 3d 30 20 29 3b 0a 0a 20 20 20 20 20 20  y)==0 );..      
2f080 20 20 69 66 28 20 28 73 43 6f 73 74 2e 75 73 65    if( (sCost.use
2f081 64 26 6e 6f 74 52 65 61 64 79 29 3d 3d 30 0a 20  d&notReady)==0. 
2f082 20 20 20 20 20 20 20 20 26 26 20 28 6a 3d 3d 69          && (j==i
2f083 46 72 6f 6d 20 7c 7c 20 73 43 6f 73 74 2e 72 43  From || sCost.rC
2f084 6f 73 74 3c 62 65 73 74 50 6c 61 6e 2e 72 43 6f  ost<bestPlan.rCo
2f085 73 74 29 20 0a 20 20 20 20 20 20 20 20 29 7b 0a  st) .        ){.
2f086 20 20 20 20 20 20 20 20 20 20 62 65 73 74 50 6c            bestPl
2f087 61 6e 20 3d 20 73 43 6f 73 74 3b 0a 20 20 20 20  an = sCost;.    
2f088 20 20 20 20 20 20 62 65 73 74 4a 20 3d 20 6a 3b        bestJ = j;
2f089 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2f08a 20 20 20 69 66 28 20 64 6f 4e 6f 74 52 65 6f 72     if( doNotReor
2f08b 64 65 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  der ) break;.   
2f08c 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61     }.    }.    a
2f08d 73 73 65 72 74 28 20 62 65 73 74 4a 3e 3d 30 20  ssert( bestJ>=0 
2f08e 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e  );.    assert( n
2f08f 6f 74 52 65 61 64 79 20 26 20 67 65 74 4d 61 73  otReady & getMas
2f090 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 54 61 62  k(pMaskSet, pTab
2f091 4c 69 73 74 2d 3e 61 5b 62 65 73 74 4a 5d 2e 69  List->a[bestJ].i
2f092 43 75 72 73 6f 72 29 20 29 3b 0a 20 20 20 20 57  Cursor) );.    W
2f093 48 45 52 45 54 52 41 43 45 28 28 22 2a 2a 2a 20  HERETRACE(("*** 
2f094 4f 70 74 69 6d 69 7a 65 72 20 73 65 6c 65 63 74  Optimizer select
2f095 73 20 74 61 62 6c 65 20 25 64 20 66 6f 72 20 6c  s table %d for l
2f096 6f 6f 70 20 25 64 5c 6e 22 2c 20 62 65 73 74 4a  oop %d\n", bestJ
2f097 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 4c 65  ,.           pLe
2f098 76 65 6c 2d 70 57 49 6e 66 6f 2d 3e 61 29 29 3b  vel-pWInfo->a));
2f099 0a 20 20 20 20 69 66 28 20 28 62 65 73 74 50 6c  .    if( (bestPl
2f09a 61 6e 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  an.plan.wsFlags 
2f09b 26 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 29  & WHERE_ORDERBY)
2f09c 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 70  !=0 ){.      *pp
2f09d 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 20  OrderBy = 0;.   
2f09e 20 7d 0a 20 20 20 20 61 6e 64 46 6c 61 67 73 20   }.    andFlags 
2f09f 26 3d 20 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e  &= bestPlan.plan
2f0a0 2e 77 73 46 6c 61 67 73 3b 0a 20 20 20 20 70 4c  .wsFlags;.    pL
2f0a1 65 76 65 6c 2d 3e 70 6c 61 6e 20 3d 20 62 65 73  evel->plan = bes
2f0a2 74 50 6c 61 6e 2e 70 6c 61 6e 3b 0a 20 20 20 20  tPlan.plan;.    
2f0a3 69 66 28 20 62 65 73 74 50 6c 61 6e 2e 70 6c 61  if( bestPlan.pla
2f0a4 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
2f0a5 45 5f 49 4e 44 45 58 45 44 20 29 7b 0a 20 20 20  E_INDEXED ){.   
2f0a6 20 20 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43     pLevel->iIdxC
2f0a7 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  ur = pParse->nTa
2f0a8 62 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  b++;.    }else{.
2f0a9 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 49        pLevel->iI
2f0aa 64 78 43 75 72 20 3d 20 2d 31 3b 0a 20 20 20 20  dxCur = -1;.    
2f0ab 7d 0a 20 20 20 20 6e 6f 74 52 65 61 64 79 20 26  }.    notReady &
2f0ac 3d 20 7e 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b  = ~getMask(pMask
2f0ad 53 65 74 2c 20 70 54 61 62 4c 69 73 74 2d 3e 61  Set, pTabList->a
2f0ae 5b 62 65 73 74 4a 5d 2e 69 43 75 72 73 6f 72 29  [bestJ].iCursor)
2f0af 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 46  ;.    pLevel->iF
2f0b0 72 6f 6d 20 3d 20 28 75 38 29 62 65 73 74 4a 3b  rom = (u8)bestJ;
2f0b1 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74  ..    /* Check t
2f0b2 68 61 74 20 69 66 20 74 68 65 20 74 61 62 6c 65  hat if the table
2f0b3 20 73 63 61 6e 6e 65 64 20 62 79 20 74 68 69 73   scanned by this
2f0b4 20 6c 6f 6f 70 20 69 74 65 72 61 74 69 6f 6e 20   loop iteration 
2f0b5 68 61 64 20 61 6e 0a 20 20 20 20 2a 2a 20 49 4e  had an.    ** IN
2f0b6 44 45 58 45 44 20 42 59 20 63 6c 61 75 73 65 20  DEXED BY clause 
2f0b7 61 74 74 61 63 68 65 64 20 74 6f 20 69 74 2c 20  attached to it, 
2f0b8 74 68 61 74 20 74 68 65 20 6e 61 6d 65 64 20 69  that the named i
2f0b9 6e 64 65 78 20 69 73 20 62 65 69 6e 67 0a 20 20  ndex is being.  
2f0ba 20 20 2a 2a 20 75 73 65 64 20 66 6f 72 20 74 68    ** used for th
2f0bb 65 20 73 63 61 6e 2e 20 49 66 20 6e 6f 74 2c 20  e scan. If not, 
2f0bc 74 68 65 6e 20 71 75 65 72 79 20 63 6f 6d 70 69  then query compi
2f0bd 6c 61 74 69 6f 6e 20 68 61 73 20 66 61 69 6c 65  lation has faile
2f0be 64 2e 0a 20 20 20 20 2a 2a 20 52 65 74 75 72 6e  d..    ** Return
2f0bf 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20 20 2a   an error..    *
2f0c0 2f 0a 20 20 20 20 70 49 64 78 20 3d 20 70 54 61  /.    pIdx = pTa
2f0c1 62 4c 69 73 74 2d 3e 61 5b 62 65 73 74 4a 5d 2e  bList->a[bestJ].
2f0c2 70 49 6e 64 65 78 3b 0a 20 20 20 20 69 66 28 20  pIndex;.    if( 
2f0c3 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20 69 66  pIdx ){.      if
2f0c4 28 20 28 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e  ( (bestPlan.plan
2f0c5 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
2f0c6 5f 49 4e 44 45 58 45 44 29 3d 3d 30 20 29 7b 0a  _INDEXED)==0 ){.
2f0c7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2f0c8 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2f0c9 22 63 61 6e 6e 6f 74 20 75 73 65 20 69 6e 64 65  "cannot use inde
2f0ca 78 3a 20 25 73 22 2c 20 70 49 64 78 2d 3e 7a 4e  x: %s", pIdx->zN
2f0cb 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 67 6f  ame);.        go
2f0cc 74 6f 20 77 68 65 72 65 42 65 67 69 6e 45 72 72  to whereBeginErr
2f0cd 6f 72 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  or;.      }else{
2f0ce 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61  .        /* If a
2f0cf 6e 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61  n INDEXED BY cla
2f0d0 75 73 65 20 69 73 20 75 73 65 64 2c 20 74 68 65  use is used, the
2f0d1 20 62 65 73 74 49 6e 64 65 78 28 29 20 66 75 6e   bestIndex() fun
2f0d2 63 74 69 6f 6e 20 69 73 0a 20 20 20 20 20 20 20  ction is.       
2f0d3 20 2a 2a 20 67 75 61 72 61 6e 74 65 65 64 20 74   ** guaranteed t
2f0d4 6f 20 66 69 6e 64 20 74 68 65 20 69 6e 64 65 78  o find the index
2f0d5 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68   specified in th
2f0d6 65 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61  e INDEXED BY cla
2f0d7 75 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  use.        ** i
2f0d8 66 20 69 74 20 66 69 6e 64 20 61 6e 20 69 6e 64  f it find an ind
2f0d9 65 78 20 61 74 20 61 6c 6c 2e 20 2a 2f 0a 20 20  ex at all. */.  
2f0da 20 20 20 20 20 20 61 73 73 65 72 74 28 20 62 65        assert( be
2f0db 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 75 2e 70 49  stPlan.plan.u.pI
2f0dc 64 78 3d 3d 70 49 64 78 20 29 3b 0a 20 20 20 20  dx==pIdx );.    
2f0dd 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
2f0de 57 48 45 52 45 54 52 41 43 45 28 28 22 2a 2a 2a  WHERETRACE(("***
2f0df 20 4f 70 74 69 6d 69 7a 65 72 20 46 69 6e 69 73   Optimizer Finis
2f0e0 68 65 64 20 2a 2a 2a 5c 6e 22 29 29 3b 0a 20 20  hed ***\n"));.  
2f0e1 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
2f0e2 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
2f0e3 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f  iled ){.    goto
2f0e4 20 77 68 65 72 65 42 65 67 69 6e 45 72 72 6f 72   whereBeginError
2f0e5 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
2f0e6 68 65 20 74 6f 74 61 6c 20 71 75 65 72 79 20 6f  he total query o
2f0e7 6e 6c 79 20 73 65 6c 65 63 74 73 20 61 20 73 69  nly selects a si
2f0e8 6e 67 6c 65 20 72 6f 77 2c 20 74 68 65 6e 20 74  ngle row, then t
2f0e9 68 65 20 4f 52 44 45 52 20 42 59 0a 20 20 2a 2a  he ORDER BY.  **
2f0ea 20 63 6c 61 75 73 65 20 69 73 20 69 72 72 65 6c   clause is irrel
2f0eb 65 76 61 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69 66  evant..  */.  if
2f0ec 28 20 28 61 6e 64 46 6c 61 67 73 20 26 20 57 48  ( (andFlags & WH
2f0ed 45 52 45 5f 55 4e 49 51 55 45 29 21 3d 30 20 26  ERE_UNIQUE)!=0 &
2f0ee 26 20 70 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  & ppOrderBy ){. 
2f0ef 20 20 20 2a 70 70 4f 72 64 65 72 42 79 20 3d 20     *ppOrderBy = 
2f0f0 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  0;.  }..  /* If 
2f0f1 74 68 65 20 63 61 6c 6c 65 72 20 69 73 20 61 6e  the caller is an
2f0f2 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54   UPDATE or DELET
2f0f3 45 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  E statement that
2f0f4 20 69 73 20 72 65 71 75 65 73 74 69 6e 67 0a 20   is requesting. 
2f0f5 20 2a 2a 20 74 6f 20 75 73 65 20 61 20 6f 6e 65   ** to use a one
2f0f6 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 2c  -pass algorithm,
2f0f7 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68   determine if th
2f0f8 69 73 20 69 73 20 61 70 70 72 6f 70 72 69 61 74  is is appropriat
2f0f9 65 2e 0a 20 20 2a 2a 20 54 68 65 20 6f 6e 65 2d  e..  ** The one-
2f0fa 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 20 6f  pass algorithm o
2f0fb 6e 6c 79 20 77 6f 72 6b 73 20 69 66 20 74 68 65  nly works if the
2f0fc 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f   WHERE clause co
2f0fd 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a 2a 20 74  nstraints.  ** t
2f0fe 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  he statement to 
2f0ff 75 70 64 61 74 65 20 61 20 73 69 6e 67 6c 65 20  update a single 
2f100 72 6f 77 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  row..  */.  asse
2f101 72 74 28 20 28 77 63 74 72 6c 46 6c 61 67 73 20  rt( (wctrlFlags 
2f102 26 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 53 5f  & WHERE_ONEPASS_
2f103 44 45 53 49 52 45 44 29 3d 3d 30 20 7c 7c 20 70  DESIRED)==0 || p
2f104 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 3d 3d 31  WInfo->nLevel==1
2f105 20 29 3b 0a 20 20 69 66 28 20 28 77 63 74 72 6c   );.  if( (wctrl
2f106 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 4f 4e  Flags & WHERE_ON
2f107 45 50 41 53 53 5f 44 45 53 49 52 45 44 29 21 3d  EPASS_DESIRED)!=
2f108 30 20 26 26 20 28 61 6e 64 46 6c 61 67 73 20 26  0 && (andFlags &
2f109 20 57 48 45 52 45 5f 55 4e 49 51 55 45 29 21 3d   WHERE_UNIQUE)!=
2f10a 30 20 29 7b 0a 20 20 20 20 70 57 49 6e 66 6f 2d  0 ){.    pWInfo-
2f10b 3e 6f 6b 4f 6e 65 50 61 73 73 20 3d 20 31 3b 0a  >okOnePass = 1;.
2f10c 20 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 30 5d      pWInfo->a[0]
2f10d 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 3d  .plan.wsFlags &=
2f10e 20 7e 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59   ~WHERE_IDX_ONLY
2f10f 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e  ;.  }..  /* Open
2f110 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 74   all tables in t
2f111 68 65 20 70 54 61 62 4c 69 73 74 20 61 6e 64 20  he pTabList and 
2f112 61 6e 79 20 69 6e 64 69 63 65 73 20 73 65 6c 65  any indices sele
2f113 63 74 65 64 20 66 6f 72 0a 20 20 2a 2a 20 73 65  cted for.  ** se
2f114 61 72 63 68 69 6e 67 20 74 68 6f 73 65 20 74 61  arching those ta
2f115 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  bles..  */.  sql
2f116 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63  ite3CodeVerifySc
2f117 68 65 6d 61 28 70 50 61 72 73 65 2c 20 2d 31 29  hema(pParse, -1)
2f118 3b 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 65 20  ; /* Insert the 
2f119 63 6f 6f 6b 69 65 20 76 65 72 69 66 69 65 72 20  cookie verifier 
2f11a 47 6f 74 6f 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  Goto */.  for(i=
2f11b 30 2c 20 70 4c 65 76 65 6c 3d 70 57 49 6e 66 6f  0, pLevel=pWInfo
2f11c 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d  ->a; i<pTabList-
2f11d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 4c 65 76  >nSrc; i++, pLev
2f11e 65 6c 2b 2b 29 7b 0a 20 20 20 20 54 61 62 6c 65  el++){.    Table
2f11f 20 2a 70 54 61 62 3b 20 20 20 20 20 2f 2a 20 54   *pTab;     /* T
2f120 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a  able to open */.
2f121 20 20 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20      int iDb;    
2f122 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
2f123 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
2f124 6e 69 6e 67 20 74 61 62 6c 65 2f 69 6e 64 65 78  ning table/index
2f125 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
2f126 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e  ITE_OMIT_EXPLAIN
2f127 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d  .    if( pParse-
2f128 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 29 7b 0a 20  >explain==2 ){. 
2f129 20 20 20 20 20 63 68 61 72 20 2a 7a 4d 73 67 3b       char *zMsg;
2f12a 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72  .      struct Sr
2f12b 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  cList_item *pIte
2f12c 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61  m = &pTabList->a
2f12d 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b  [pLevel->iFrom];
2f12e 0a 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71  .      zMsg = sq
2f12f 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
2f130 20 22 54 41 42 4c 45 20 25 73 22 2c 20 70 49 74   "TABLE %s", pIt
2f131 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  em->zName);.    
2f132 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 41 6c    if( pItem->zAl
2f133 69 61 73 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  ias ){.        z
2f134 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 41 70  Msg = sqlite3MAp
2f135 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20  pendf(db, zMsg, 
2f136 22 25 73 20 41 53 20 25 73 22 2c 20 7a 4d 73 67  "%s AS %s", zMsg
2f137 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29  , pItem->zAlias)
2f138 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2f139 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61  if( (pLevel->pla
2f13a 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
2f13b 45 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 29 7b  E_INDEXED)!=0 ){
2f13c 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20  .        zMsg = 
2f13d 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28  sqlite3MAppendf(
2f13e 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 57 49  db, zMsg, "%s WI
2f13f 54 48 20 49 4e 44 45 58 20 25 73 22 2c 0a 20 20  TH INDEX %s",.  
2f140 20 20 20 20 20 20 20 20 20 7a 4d 73 67 2c 20 70           zMsg, p
2f141 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49  Level->plan.u.pI
2f142 64 78 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  dx->zName);.    
2f143 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 65 76    }else if( pLev
2f144 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
2f145 20 26 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f   & WHERE_MULTI_O
2f146 52 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73  R ){.        zMs
2f147 67 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65  g = sqlite3MAppe
2f148 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25  ndf(db, zMsg, "%
2f149 73 20 56 49 41 20 4d 55 4c 54 49 2d 49 4e 44 45  s VIA MULTI-INDE
2f14a 58 20 55 4e 49 4f 4e 22 2c 20 7a 4d 73 67 29 3b  X UNION", zMsg);
2f14b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
2f14c 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73   pLevel->plan.ws
2f14d 46 6c 61 67 73 20 26 20 28 57 48 45 52 45 5f 52  Flags & (WHERE_R
2f14e 4f 57 49 44 5f 45 51 7c 57 48 45 52 45 5f 52 4f  OWID_EQ|WHERE_RO
2f14f 57 49 44 5f 52 41 4e 47 45 29 20 29 7b 0a 20 20  WID_RANGE) ){.  
2f150 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c        zMsg = sql
2f151 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64 62 2c  ite3MAppendf(db,
2f152 20 7a 4d 73 67 2c 20 22 25 73 20 55 53 49 4e 47   zMsg, "%s USING
2f153 20 50 52 49 4d 41 52 59 20 4b 45 59 22 2c 20 7a   PRIMARY KEY", z
2f154 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a 23 69  Msg);.      }.#i
2f155 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2f156 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
2f157 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 28 70       else if( (p
2f158 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
2f159 61 67 73 20 26 20 57 48 45 52 45 5f 56 49 52 54  ags & WHERE_VIRT
2f15a 55 41 4c 54 41 42 4c 45 29 21 3d 30 20 29 7b 0a  UALTABLE)!=0 ){.
2f15b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
2f15c 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 56 74 61  index_info *pVta
2f15d 62 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70  bIdx = pLevel->p
2f15e 6c 61 6e 2e 75 2e 70 56 74 61 62 49 64 78 3b 0a  lan.u.pVtabIdx;.
2f15f 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73          zMsg = s
2f160 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64  qlite3MAppendf(d
2f161 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 56 49 52  b, zMsg, "%s VIR
2f162 54 55 41 4c 20 54 41 42 4c 45 20 49 4e 44 45 58  TUAL TABLE INDEX
2f163 20 25 64 3a 25 73 22 2c 20 7a 4d 73 67 2c 0a 20   %d:%s", zMsg,. 
2f164 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f165 20 20 20 70 56 74 61 62 49 64 78 2d 3e 69 64 78     pVtabIdx->idx
2f166 4e 75 6d 2c 20 70 56 74 61 62 49 64 78 2d 3e 69  Num, pVtabIdx->i
2f167 64 78 53 74 72 29 3b 0a 20 20 20 20 20 20 7d 0a  dxStr);.      }.
2f168 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28  #endif.      if(
2f169 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73   pLevel->plan.ws
2f16a 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 4f 52  Flags & WHERE_OR
2f16b 44 45 52 42 59 20 29 7b 0a 20 20 20 20 20 20 20  DERBY ){.       
2f16c 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d   zMsg = sqlite3M
2f16d 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67  Appendf(db, zMsg
2f16e 2c 20 22 25 73 20 4f 52 44 45 52 20 42 59 22 2c  , "%s ORDER BY",
2f16f 20 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a   zMsg);.      }.
2f170 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2f171 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 45 78  eAddOp4(v, OP_Ex
2f172 70 6c 61 69 6e 2c 20 69 2c 20 70 4c 65 76 65 6c  plain, i, pLevel
2f173 2d 3e 69 46 72 6f 6d 2c 20 30 2c 20 7a 4d 73 67  ->iFrom, 0, zMsg
2f174 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  , P4_DYNAMIC);. 
2f175 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53     }.#endif /* S
2f176 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41  QLITE_OMIT_EXPLA
2f177 49 4e 20 2a 2f 0a 20 20 20 20 70 54 61 62 49 74  IN */.    pTabIt
2f178 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e  em = &pTabList->
2f179 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d  a[pLevel->iFrom]
2f17a 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 70 54 61  ;.    pTab = pTa
2f17b 62 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20  bItem->pTab;.   
2f17c 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
2f17d 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
2f17e 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
2f17f 20 20 20 20 69 66 28 20 28 70 54 61 62 2d 3e 74      if( (pTab->t
2f180 61 62 46 6c 61 67 73 20 26 20 54 46 5f 45 70 68  abFlags & TF_Eph
2f181 65 6d 65 72 61 6c 29 21 3d 30 20 7c 7c 20 70 54  emeral)!=0 || pT
2f182 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 20 63 6f  ab->pSelect ) co
2f183 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20  ntinue;.#ifndef 
2f184 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2f185 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 66 28  UALTABLE.    if(
2f186 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77   (pLevel->plan.w
2f187 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 56  sFlags & WHERE_V
2f188 49 52 54 55 41 4c 54 41 42 4c 45 29 21 3d 30 20  IRTUALTABLE)!=0 
2f189 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63  ){.      const c
2f18a 68 61 72 20 2a 70 56 54 61 62 20 3d 20 28 63 6f  har *pVTab = (co
2f18b 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74  nst char *)sqlit
2f18c 65 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20  e3GetVTable(db, 
2f18d 70 54 61 62 29 3b 0a 20 20 20 20 20 20 69 6e 74  pTab);.      int
2f18e 20 69 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d   iCur = pTabItem
2f18f 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 20  ->iCursor;.     
2f190 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2f191 70 34 28 76 2c 20 4f 50 5f 56 4f 70 65 6e 2c 20  p4(v, OP_VOpen, 
2f192 69 43 75 72 2c 20 30 2c 20 30 2c 20 70 56 54 61  iCur, 0, 0, pVTa
2f193 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 20 20  b, P4_VTAB);.   
2f194 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20   }else.#endif.  
2f195 20 20 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70    if( (pLevel->p
2f196 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
2f197 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 3d 3d 30  ERE_IDX_ONLY)==0
2f198 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 77 63  .         && (wc
2f199 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45  trlFlags & WHERE
2f19a 5f 4f 4d 49 54 5f 4f 50 45 4e 29 3d 3d 30 20 29  _OMIT_OPEN)==0 )
2f19b 7b 0a 20 20 20 20 20 20 69 6e 74 20 6f 70 20 3d  {.      int op =
2f19c 20 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61   pWInfo->okOnePa
2f19d 73 73 20 3f 20 4f 50 5f 4f 70 65 6e 57 72 69 74  ss ? OP_OpenWrit
2f19e 65 20 3a 20 4f 50 5f 4f 70 65 6e 52 65 61 64 3b  e : OP_OpenRead;
2f19f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70  .      sqlite3Op
2f1a0 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  enTable(pParse, 
2f1a1 70 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73 6f  pTabItem->iCurso
2f1a2 72 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 6f 70  r, iDb, pTab, op
2f1a3 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 57  );.      if( !pW
2f1a4 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73 20  Info->okOnePass 
2f1a5 26 26 20 70 54 61 62 2d 3e 6e 43 6f 6c 3c 42 4d  && pTab->nCol<BM
2f1a6 53 20 29 7b 0a 20 20 20 20 20 20 20 20 42 69 74  S ){.        Bit
2f1a7 6d 61 73 6b 20 62 20 3d 20 70 54 61 62 49 74 65  mask b = pTabIte
2f1a8 6d 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20 20 20 20  m->colUsed;.    
2f1a9 20 20 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20      int n = 0;. 
2f1aa 20 20 20 20 20 20 20 66 6f 72 28 3b 20 62 3b 20         for(; b; 
2f1ab 62 3d 62 3e 3e 31 2c 20 6e 2b 2b 29 7b 7d 0a 20  b=b>>1, n++){}. 
2f1ac 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2f1ad 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 73 71  beChangeP4(v, sq
2f1ae 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
2f1af 41 64 64 72 28 76 29 2d 31 2c 20 53 51 4c 49 54  Addr(v)-1, SQLIT
2f1b0 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 6e 29 2c  E_INT_TO_PTR(n),
2f1b1 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 20   P4_INT32);.    
2f1b2 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 3d 70      assert( n<=p
2f1b3 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20  Tab->nCol );.   
2f1b4 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
2f1b5 20 20 20 20 20 20 73 71 6c 69 74 65 33 54 61 62        sqlite3Tab
2f1b6 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69  leLock(pParse, i
2f1b7 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20  Db, pTab->tnum, 
2f1b8 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  0, pTab->zName);
2f1b9 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 65 76 65  .    }.    pLeve
2f1ba 6c 2d 3e 69 54 61 62 43 75 72 20 3d 20 70 54 61  l->iTabCur = pTa
2f1bb 62 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a  bItem->iCursor;.
2f1bc 20 20 20 20 69 66 28 20 28 70 4c 65 76 65 6c 2d      if( (pLevel-
2f1bd 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
2f1be 57 48 45 52 45 5f 49 4e 44 45 58 45 44 29 21 3d  WHERE_INDEXED)!=
2f1bf 30 20 29 7b 0a 20 20 20 20 20 20 49 6e 64 65 78  0 ){.      Index
2f1c0 20 2a 70 49 78 20 3d 20 70 4c 65 76 65 6c 2d 3e   *pIx = pLevel->
2f1c1 70 6c 61 6e 2e 75 2e 70 49 64 78 3b 0a 20 20 20  plan.u.pIdx;.   
2f1c2 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79     KeyInfo *pKey
2f1c3 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b   = sqlite3IndexK
2f1c4 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70  eyinfo(pParse, p
2f1c5 49 78 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 69  Ix);.      int i
2f1c6 49 64 78 43 75 72 20 3d 20 70 4c 65 76 65 6c 2d  IdxCur = pLevel-
2f1c7 3e 69 49 64 78 43 75 72 3b 0a 20 20 20 20 20 20  >iIdxCur;.      
2f1c8 61 73 73 65 72 74 28 20 70 49 78 2d 3e 70 53 63  assert( pIx->pSc
2f1c9 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68  hema==pTab->pSch
2f1ca 65 6d 61 20 29 3b 0a 20 20 20 20 20 20 61 73 73  ema );.      ass
2f1cb 65 72 74 28 20 69 49 64 78 43 75 72 3e 3d 30 20  ert( iIdxCur>=0 
2f1cc 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2f1cd 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2f1ce 5f 4f 70 65 6e 52 65 61 64 2c 20 69 49 64 78 43  _OpenRead, iIdxC
2f1cf 75 72 2c 20 70 49 78 2d 3e 74 6e 75 6d 2c 20 69  ur, pIx->tnum, i
2f1d0 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  Db,.            
2f1d1 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61              (cha
2f1d2 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49  r*)pKey, P4_KEYI
2f1d3 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20  NFO_HANDOFF);.  
2f1d4 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
2f1d5 28 76 2c 20 22 25 73 22 2c 20 70 49 78 2d 3e 7a  (v, "%s", pIx->z
2f1d6 4e 61 6d 65 29 29 3b 0a 20 20 20 20 7d 0a 20 20  Name));.    }.  
2f1d7 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72    sqlite3CodeVer
2f1d8 69 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65  ifySchema(pParse
2f1d9 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20 70 57  , iDb);.  }.  pW
2f1da 49 6e 66 6f 2d 3e 69 54 6f 70 20 3d 20 73 71 6c  Info->iTop = sql
2f1db 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
2f1dc 64 64 72 28 76 29 3b 0a 0a 20 20 2f 2a 20 47 65  ddr(v);..  /* Ge
2f1dd 6e 65 72 61 74 65 20 74 68 65 20 63 6f 64 65 20  nerate the code 
2f1de 74 6f 20 64 6f 20 74 68 65 20 73 65 61 72 63 68  to do the search
2f1df 2e 20 20 45 61 63 68 20 69 74 65 72 61 74 69 6f  .  Each iteratio
2f1e0 6e 20 6f 66 20 74 68 65 20 66 6f 72 0a 20 20 2a  n of the for.  *
2f1e1 2a 20 6c 6f 6f 70 20 62 65 6c 6f 77 20 67 65 6e  * loop below gen
2f1e2 65 72 61 74 65 73 20 63 6f 64 65 20 66 6f 72 20  erates code for 
2f1e3 61 20 73 69 6e 67 6c 65 20 6e 65 73 74 65 64 20  a single nested 
2f1e4 6c 6f 6f 70 20 6f 66 20 74 68 65 20 56 4d 0a 20  loop of the VM. 
2f1e5 20 2a 2a 20 70 72 6f 67 72 61 6d 2e 0a 20 20 2a   ** program..  *
2f1e6 2f 0a 20 20 6e 6f 74 52 65 61 64 79 20 3d 20 7e  /.  notReady = ~
2f1e7 28 42 69 74 6d 61 73 6b 29 30 3b 0a 20 20 66 6f  (Bitmask)0;.  fo
2f1e8 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 4c 69 73  r(i=0; i<pTabLis
2f1e9 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20  t->nSrc; i++){. 
2f1ea 20 20 20 6e 6f 74 52 65 61 64 79 20 3d 20 63 6f     notReady = co
2f1eb 64 65 4f 6e 65 4c 6f 6f 70 53 74 61 72 74 28 70  deOneLoopStart(p
2f1ec 57 49 6e 66 6f 2c 20 69 2c 20 77 63 74 72 6c 46  WInfo, i, wctrlF
2f1ed 6c 61 67 73 2c 20 6e 6f 74 52 65 61 64 79 29 3b  lags, notReady);
2f1ee 0a 20 20 20 20 70 57 49 6e 66 6f 2d 3e 69 43 6f  .    pWInfo->iCo
2f1ef 6e 74 69 6e 75 65 20 3d 20 70 57 49 6e 66 6f 2d  ntinue = pWInfo-
2f1f0 3e 61 5b 69 5d 2e 61 64 64 72 43 6f 6e 74 3b 0a  >a[i].addrCont;.
2f1f1 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49    }..#ifdef SQLI
2f1f2 54 45 5f 54 45 53 54 20 20 2f 2a 20 46 6f 72 20  TE_TEST  /* For 
2f1f3 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75  testing and debu
2f1f4 67 67 69 6e 67 20 75 73 65 20 6f 6e 6c 79 20 2a  gging use only *
2f1f5 2f 0a 20 20 2f 2a 20 52 65 63 6f 72 64 20 69 6e  /.  /* Record in
2f1f6 20 74 68 65 20 71 75 65 72 79 20 70 6c 61 6e 20   the query plan 
2f1f7 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
2f1f8 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 61  t the current ta
2f1f9 62 6c 65 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65  ble.  ** and the
2f1fa 20 69 6e 64 65 78 20 75 73 65 64 20 74 6f 20 61   index used to a
2f1fb 63 63 65 73 73 20 69 74 20 28 69 66 20 61 6e 79  ccess it (if any
2f1fc 29 2e 20 20 49 66 20 74 68 65 20 74 61 62 6c 65  ).  If the table
2f1fd 20 69 74 73 65 6c 66 0a 20 20 2a 2a 20 69 73 20   itself.  ** is 
2f1fe 6e 6f 74 20 75 73 65 64 2c 20 69 74 73 20 6e 61  not used, its na
2f1ff 6d 65 20 69 73 20 6a 75 73 74 20 27 7b 7d 27 2e  me is just '{}'.
2f200 20 20 49 66 20 6e 6f 20 69 6e 64 65 78 20 69 73    If no index is
2f201 20 75 73 65 64 0a 20 20 2a 2a 20 74 68 65 20 69   used.  ** the i
2f202 6e 64 65 78 20 69 73 20 6c 69 73 74 65 64 20 61  ndex is listed a
2f203 73 20 22 7b 7d 22 2e 20 20 49 66 20 74 68 65 20  s "{}".  If the 
2f204 70 72 69 6d 61 72 79 20 6b 65 79 20 69 73 20 75  primary key is u
2f205 73 65 64 20 74 68 65 0a 20 20 2a 2a 20 69 6e 64  sed the.  ** ind
2f206 65 78 20 6e 61 6d 65 20 69 73 20 27 2a 27 2e 0a  ex name is '*'..
2f207 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20    */.  for(i=0; 
2f208 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  i<pTabList->nSrc
2f209 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72  ; i++){.    char
2f20a 20 2a 7a 3b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a   *z;.    int n;.
2f20b 20 20 20 20 70 4c 65 76 65 6c 20 3d 20 26 70 57      pLevel = &pW
2f20c 49 6e 66 6f 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20  Info->a[i];.    
2f20d 70 54 61 62 49 74 65 6d 20 3d 20 26 70 54 61 62  pTabItem = &pTab
2f20e 4c 69 73 74 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e  List->a[pLevel->
2f20f 69 46 72 6f 6d 5d 3b 0a 20 20 20 20 7a 20 3d 20  iFrom];.    z = 
2f210 70 54 61 62 49 74 65 6d 2d 3e 7a 41 6c 69 61 73  pTabItem->zAlias
2f211 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29  ;.    if( z==0 )
2f212 20 7a 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 70   z = pTabItem->p
2f213 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20  Tab->zName;.    
2f214 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
2f215 6e 33 30 28 7a 29 3b 0a 20 20 20 20 69 66 28 20  n30(z);.    if( 
2f216 6e 2b 6e 51 50 6c 61 6e 20 3c 20 73 69 7a 65 6f  n+nQPlan < sizeo
2f217 66 28 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f  f(sqlite3_query_
2f218 70 6c 61 6e 29 2d 31 30 20 29 7b 0a 20 20 20 20  plan)-10 ){.    
2f219 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c    if( pLevel->pl
2f21a 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
2f21b 52 45 5f 49 44 58 5f 4f 4e 4c 59 20 29 7b 0a 20  RE_IDX_ONLY ){. 
2f21c 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 73         memcpy(&s
2f21d 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61  qlite3_query_pla
2f21e 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 22 7b 7d 22 2c  n[nQPlan], "{}",
2f21f 20 32 29 3b 0a 20 20 20 20 20 20 20 20 6e 51 50   2);.        nQP
2f220 6c 61 6e 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20  lan += 2;.      
2f221 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6d  }else{.        m
2f222 65 6d 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71  emcpy(&sqlite3_q
2f223 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e  uery_plan[nQPlan
2f224 5d 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20  ], z, n);.      
2f225 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 6e 3b 0a 20    nQPlan += n;. 
2f226 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
2f227 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b  ite3_query_plan[
2f228 6e 51 50 6c 61 6e 2b 2b 5d 20 3d 20 27 20 27 3b  nQPlan++] = ' ';
2f229 0a 20 20 20 20 7d 0a 20 20 20 20 74 65 73 74 63  .    }.    testc
2f22a 61 73 65 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61  ase( pLevel->pla
2f22b 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
2f22c 45 5f 52 4f 57 49 44 5f 45 51 20 29 3b 0a 20 20  E_ROWID_EQ );.  
2f22d 20 20 74 65 73 74 63 61 73 65 28 20 70 4c 65 76    testcase( pLev
2f22e 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
2f22f 20 26 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 52   & WHERE_ROWID_R
2f230 41 4e 47 45 20 29 3b 0a 20 20 20 20 69 66 28 20  ANGE );.    if( 
2f231 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
2f232 6c 61 67 73 20 26 20 28 57 48 45 52 45 5f 52 4f  lags & (WHERE_RO
2f233 57 49 44 5f 45 51 7c 57 48 45 52 45 5f 52 4f 57  WID_EQ|WHERE_ROW
2f234 49 44 5f 52 41 4e 47 45 29 20 29 7b 0a 20 20 20  ID_RANGE) ){.   
2f235 20 20 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74     memcpy(&sqlit
2f236 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51  e3_query_plan[nQ
2f237 50 6c 61 6e 5d 2c 20 22 2a 20 22 2c 20 32 29 3b  Plan], "* ", 2);
2f238 0a 20 20 20 20 20 20 6e 51 50 6c 61 6e 20 2b 3d  .      nQPlan +=
2f239 20 32 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   2;.    }else if
2f23a 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  ( (pLevel->plan.
2f23b 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
2f23c 49 4e 44 45 58 45 44 29 21 3d 30 20 29 7b 0a 20  INDEXED)!=0 ){. 
2f23d 20 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33       n = sqlite3
2f23e 53 74 72 6c 65 6e 33 30 28 70 4c 65 76 65 6c 2d  Strlen30(pLevel-
2f23f 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 2d 3e 7a 4e  >plan.u.pIdx->zN
2f240 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ame);.      if( 
2f241 6e 2b 6e 51 50 6c 61 6e 20 3c 20 73 69 7a 65 6f  n+nQPlan < sizeo
2f242 66 28 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f  f(sqlite3_query_
2f243 70 6c 61 6e 29 2d 32 20 29 7b 0a 20 20 20 20 20  plan)-2 ){.     
2f244 20 20 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74     memcpy(&sqlit
2f245 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51  e3_query_plan[nQ
2f246 50 6c 61 6e 5d 2c 20 70 4c 65 76 65 6c 2d 3e 70  Plan], pLevel->p
2f247 6c 61 6e 2e 75 2e 70 49 64 78 2d 3e 7a 4e 61 6d  lan.u.pIdx->zNam
2f248 65 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20 20 6e  e, n);.        n
2f249 51 50 6c 61 6e 20 2b 3d 20 6e 3b 0a 20 20 20 20  QPlan += n;.    
2f24a 20 20 20 20 73 71 6c 69 74 65 33 5f 71 75 65 72      sqlite3_quer
2f24b 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 2b 2b 5d  y_plan[nQPlan++]
2f24c 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 7d 0a   = ' ';.      }.
2f24d 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2f24e 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74 65 33   memcpy(&sqlite3
2f24f 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c  _query_plan[nQPl
2f250 61 6e 5d 2c 20 22 7b 7d 20 22 2c 20 33 29 3b 0a  an], "{} ", 3);.
2f251 20 20 20 20 20 20 6e 51 50 6c 61 6e 20 2b 3d 20        nQPlan += 
2f252 33 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 77  3;.    }.  }.  w
2f253 68 69 6c 65 28 20 6e 51 50 6c 61 6e 3e 30 20 26  hile( nQPlan>0 &
2f254 26 20 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f  & sqlite3_query_
2f255 70 6c 61 6e 5b 6e 51 50 6c 61 6e 2d 31 5d 3d 3d  plan[nQPlan-1]==
2f256 27 20 27 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ' ' ){.    sqlit
2f257 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 2d 2d  e3_query_plan[--
2f258 6e 51 50 6c 61 6e 5d 20 3d 20 30 3b 0a 20 20 7d  nQPlan] = 0;.  }
2f259 0a 20 20 73 71 6c 69 74 65 33 5f 71 75 65 72 79  .  sqlite3_query
2f25a 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 20 3d 20  _plan[nQPlan] = 
2f25b 30 3b 0a 20 20 6e 51 50 6c 61 6e 20 3d 20 30 3b  0;.  nQPlan = 0;
2f25c 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2f25d 45 5f 54 45 53 54 20 2f 2f 20 54 65 73 74 69 6e  E_TEST // Testin
2f25e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20  g and debugging 
2f25f 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 0a 20 20 2f  use only */..  /
2f260 2a 20 52 65 63 6f 72 64 20 74 68 65 20 63 6f 6e  * Record the con
2f261 74 69 6e 75 61 74 69 6f 6e 20 61 64 64 72 65 73  tinuation addres
2f262 73 20 69 6e 20 74 68 65 20 57 68 65 72 65 49 6e  s in the WhereIn
2f263 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  fo structure.  T
2f264 68 65 6e 0a 20 20 2a 2a 20 63 6c 65 61 6e 20 75  hen.  ** clean u
2f265 70 20 61 6e 64 20 72 65 74 75 72 6e 2e 0a 20 20  p and return..  
2f266 2a 2f 0a 20 20 72 65 74 75 72 6e 20 70 57 49 6e  */.  return pWIn
2f267 66 6f 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68  fo;..  /* Jump h
2f268 65 72 65 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61  ere if malloc fa
2f269 69 6c 73 20 2a 2f 0a 77 68 65 72 65 42 65 67 69  ils */.whereBegi
2f26a 6e 45 72 72 6f 72 3a 0a 20 20 77 68 65 72 65 49  nError:.  whereI
2f26b 6e 66 6f 46 72 65 65 28 64 62 2c 20 70 57 49 6e  nfoFree(db, pWIn
2f26c 66 6f 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b  fo);.  return 0;
2f26d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
2f26e 74 65 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  te the end of th
2f26f 65 20 57 48 45 52 45 20 6c 6f 6f 70 2e 20 20 53  e WHERE loop.  S
2f270 65 65 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 0a  ee comments on .
2f271 2a 2a 20 73 71 6c 69 74 65 33 57 68 65 72 65 42  ** sqlite3WhereB
2f272 65 67 69 6e 28 29 20 66 6f 72 20 61 64 64 69 74  egin() for addit
2f273 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
2f274 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
2f275 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2f276 33 57 68 65 72 65 45 6e 64 28 57 68 65 72 65 49  3WhereEnd(WhereI
2f277 6e 66 6f 20 2a 70 57 49 6e 66 6f 29 7b 0a 20 20  nfo *pWInfo){.  
2f278 50 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20  Parse *pParse = 
2f279 70 57 49 6e 66 6f 2d 3e 70 50 61 72 73 65 3b 0a  pWInfo->pParse;.
2f27a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
2f27b 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74  se->pVdbe;.  int
2f27c 20 69 3b 0a 20 20 57 68 65 72 65 4c 65 76 65 6c   i;.  WhereLevel
2f27d 20 2a 70 4c 65 76 65 6c 3b 0a 20 20 53 72 63 4c   *pLevel;.  SrcL
2f27e 69 73 74 20 2a 70 54 61 62 4c 69 73 74 20 3d 20  ist *pTabList = 
2f27f 70 57 49 6e 66 6f 2d 3e 70 54 61 62 4c 69 73 74  pWInfo->pTabList
2f280 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
2f281 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20  = pParse->db;.. 
2f282 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 6c 6f 6f   /* Generate loo
2f283 70 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20 63 6f  p termination co
2f284 64 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  de..  */.  sqlit
2f285 65 33 45 78 70 72 43 61 63 68 65 43 6c 65 61 72  e3ExprCacheClear
2f286 28 70 50 61 72 73 65 29 3b 0a 20 20 66 6f 72 28  (pParse);.  for(
2f287 69 3d 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  i=pTabList->nSrc
2f288 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a  -1; i>=0; i--){.
2f289 20 20 20 20 70 4c 65 76 65 6c 20 3d 20 26 70 57      pLevel = &pW
2f28a 49 6e 66 6f 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20  Info->a[i];.    
2f28b 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
2f28c 76 65 4c 61 62 65 6c 28 76 2c 20 70 4c 65 76 65  veLabel(v, pLeve
2f28d 6c 2d 3e 61 64 64 72 43 6f 6e 74 29 3b 0a 20 20  l->addrCont);.  
2f28e 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 6f 70    if( pLevel->op
2f28f 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20 20 20  !=OP_Noop ){.   
2f290 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2f291 64 4f 70 32 28 76 2c 20 70 4c 65 76 65 6c 2d 3e  dOp2(v, pLevel->
2f292 6f 70 2c 20 70 4c 65 76 65 6c 2d 3e 70 31 2c 20  op, pLevel->p1, 
2f293 70 4c 65 76 65 6c 2d 3e 70 32 29 3b 0a 20 20 20  pLevel->p2);.   
2f294 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
2f295 61 6e 67 65 50 35 28 76 2c 20 70 4c 65 76 65 6c  angeP5(v, pLevel
2f296 2d 3e 70 35 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ->p5);.    }.   
2f297 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61   if( pLevel->pla
2f298 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
2f299 45 5f 49 4e 5f 41 42 4c 45 20 26 26 20 70 4c 65  E_IN_ABLE && pLe
2f29a 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 3e 30 20  vel->u.in.nIn>0 
2f29b 29 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20  ){.      struct 
2f29c 49 6e 4c 6f 6f 70 20 2a 70 49 6e 3b 0a 20 20 20  InLoop *pIn;.   
2f29d 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
2f29e 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
2f29f 76 65 4c 61 62 65 6c 28 76 2c 20 70 4c 65 76 65  veLabel(v, pLeve
2f2a0 6c 2d 3e 61 64 64 72 4e 78 74 29 3b 0a 20 20 20  l->addrNxt);.   
2f2a1 20 20 20 66 6f 72 28 6a 3d 70 4c 65 76 65 6c 2d     for(j=pLevel-
2f2a2 3e 75 2e 69 6e 2e 6e 49 6e 2c 20 70 49 6e 3d 26  >u.in.nIn, pIn=&
2f2a3 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e  pLevel->u.in.aIn
2f2a4 4c 6f 6f 70 5b 6a 2d 31 5d 3b 20 6a 3e 30 3b 20  Loop[j-1]; j>0; 
2f2a5 6a 2d 2d 2c 20 70 49 6e 2d 2d 29 7b 0a 20 20 20  j--, pIn--){.   
2f2a6 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2f2a7 4a 75 6d 70 48 65 72 65 28 76 2c 20 70 49 6e 2d  JumpHere(v, pIn-
2f2a8 3e 61 64 64 72 49 6e 54 6f 70 2b 31 29 3b 0a 20  >addrInTop+1);. 
2f2a9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2f2aa 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
2f2ab 65 78 74 2c 20 70 49 6e 2d 3e 69 43 75 72 2c 20  ext, pIn->iCur, 
2f2ac 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 29 3b  pIn->addrInTop);
2f2ad 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2f2ae 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
2f2af 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 2d 31  pIn->addrInTop-1
2f2b0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
2f2b1 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2f2b2 62 2c 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e  b, pLevel->u.in.
2f2b3 61 49 6e 4c 6f 6f 70 29 3b 0a 20 20 20 20 7d 0a  aInLoop);.    }.
2f2b4 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
2f2b5 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 70  esolveLabel(v, p
2f2b6 4c 65 76 65 6c 2d 3e 61 64 64 72 42 72 6b 29 3b  Level->addrBrk);
2f2b7 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d  .    if( pLevel-
2f2b8 3e 69 4c 65 66 74 4a 6f 69 6e 20 29 7b 0a 20 20  >iLeftJoin ){.  
2f2b9 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20      int addr;.  
2f2ba 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
2f2bb 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2f2bc 4f 50 5f 49 66 50 6f 73 2c 20 70 4c 65 76 65 6c  OP_IfPos, pLevel
2f2bd 2d 3e 69 4c 65 66 74 4a 6f 69 6e 29 3b 0a 20 20  ->iLeftJoin);.  
2f2be 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2f2bf 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp1(v, OP_Null
2f2c0 52 6f 77 2c 20 70 54 61 62 4c 69 73 74 2d 3e 61  Row, pTabList->a
2f2c1 5b 69 5d 2e 69 43 75 72 73 6f 72 29 3b 0a 20 20  [i].iCursor);.  
2f2c2 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e      if( pLevel->
2f2c3 69 49 64 78 43 75 72 3e 3d 30 20 29 7b 0a 20 20  iIdxCur>=0 ){.  
2f2c4 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2f2c5 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 75  eAddOp1(v, OP_Nu
2f2c6 6c 6c 52 6f 77 2c 20 70 4c 65 76 65 6c 2d 3e 69  llRow, pLevel->i
2f2c7 49 64 78 43 75 72 29 3b 0a 20 20 20 20 20 20 7d  IdxCur);.      }
2f2c8 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65 76 65  .      if( pLeve
2f2c9 6c 2d 3e 6f 70 3d 3d 4f 50 5f 52 65 74 75 72 6e  l->op==OP_Return
2f2ca 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
2f2cb 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2f2cc 20 4f 50 5f 47 6f 73 75 62 2c 20 70 4c 65 76 65   OP_Gosub, pLeve
2f2cd 6c 2d 3e 70 31 2c 20 70 4c 65 76 65 6c 2d 3e 61  l->p1, pLevel->a
2f2ce 64 64 72 46 69 72 73 74 29 3b 0a 20 20 20 20 20  ddrFirst);.     
2f2cf 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2f2d0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2f2d1 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
2f2d2 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 46 69 72   pLevel->addrFir
2f2d3 73 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  st);.      }.   
2f2d4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
2f2d5 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b  mpHere(v, addr);
2f2d6 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
2f2d7 20 54 68 65 20 22 62 72 65 61 6b 22 20 70 6f 69   The "break" poi
2f2d8 6e 74 20 69 73 20 68 65 72 65 2c 20 6a 75 73 74  nt is here, just
2f2d9 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66   past the end of
2f2da 20 74 68 65 20 6f 75 74 65 72 20 6c 6f 6f 70 2e   the outer loop.
2f2db 0a 20 20 2a 2a 20 53 65 74 20 69 74 2e 0a 20 20  .  ** Set it..  
2f2dc 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
2f2dd 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
2f2de 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 29 3b  pWInfo->iBreak);
2f2df 0a 0a 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c  ..  /* Close all
2f2e0 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 73 20   of the cursors 
2f2e1 74 68 61 74 20 77 65 72 65 20 6f 70 65 6e 65 64  that were opened
2f2e2 20 62 79 20 73 71 6c 69 74 65 33 57 68 65 72 65   by sqlite3Where
2f2e3 42 65 67 69 6e 2e 0a 20 20 2a 2f 0a 20 20 66 6f  Begin..  */.  fo
2f2e4 72 28 69 3d 30 2c 20 70 4c 65 76 65 6c 3d 70 57  r(i=0, pLevel=pW
2f2e5 49 6e 66 6f 2d 3e 61 3b 20 69 3c 70 54 61 62 4c  Info->a; i<pTabL
2f2e6 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20  ist->nSrc; i++, 
2f2e7 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20 20 20 73  pLevel++){.    s
2f2e8 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
2f2e9 65 6d 20 2a 70 54 61 62 49 74 65 6d 20 3d 20 26  em *pTabItem = &
2f2ea 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76  pTabList->a[pLev
2f2eb 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20 20  el->iFrom];.    
2f2ec 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 54  Table *pTab = pT
2f2ed 61 62 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20  abItem->pTab;.  
2f2ee 20 20 61 73 73 65 72 74 28 20 70 54 61 62 21 3d    assert( pTab!=
2f2ef 30 20 29 3b 0a 20 20 20 20 69 66 28 20 28 70 54  0 );.    if( (pT
2f2f0 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54  ab->tabFlags & T
2f2f1 46 5f 45 70 68 65 6d 65 72 61 6c 29 21 3d 30 20  F_Ephemeral)!=0 
2f2f2 7c 7c 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  || pTab->pSelect
2f2f3 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
2f2f4 20 69 66 28 20 28 70 57 49 6e 66 6f 2d 3e 77 63   if( (pWInfo->wc
2f2f5 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45  trlFlags & WHERE
2f2f6 5f 4f 4d 49 54 5f 43 4c 4f 53 45 29 3d 3d 30 20  _OMIT_CLOSE)==0 
2f2f7 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 70 57  ){.      if( !pW
2f2f8 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73 20  Info->okOnePass 
2f2f9 26 26 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  && (pLevel->plan
2f2fa 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
2f2fb 5f 49 44 58 5f 4f 4e 4c 59 29 3d 3d 30 20 29 7b  _IDX_ONLY)==0 ){
2f2fc 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2f2fd 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
2f2fe 5f 43 6c 6f 73 65 2c 20 70 54 61 62 49 74 65 6d  _Close, pTabItem
2f2ff 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  ->iCursor);.    
2f300 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 28 70    }.      if( (p
2f301 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
2f302 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45  ags & WHERE_INDE
2f303 58 45 44 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  XED)!=0 ){.     
2f304 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2f305 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp1(v, OP_Close
2f306 2c 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75  , pLevel->iIdxCu
2f307 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  r);.      }.    
2f308 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69  }..    /* If thi
2f309 73 20 73 63 61 6e 20 75 73 65 73 20 61 6e 20 69  s scan uses an i
2f30a 6e 64 65 78 2c 20 6d 61 6b 65 20 63 6f 64 65 20  ndex, make code 
2f30b 73 75 62 73 74 69 74 75 74 69 6f 6e 73 20 74 6f  substitutions to
2f30c 20 72 65 61 64 20 64 61 74 61 0a 20 20 20 20 2a   read data.    *
2f30d 2a 20 66 72 6f 6d 20 74 68 65 20 69 6e 64 65 78  * from the index
2f30e 20 69 6e 20 70 72 65 66 65 72 65 6e 63 65 20 74   in preference t
2f30f 6f 20 74 68 65 20 74 61 62 6c 65 2e 20 53 6f 6d  o the table. Som
2f310 65 74 69 6d 65 73 2c 20 74 68 69 73 20 6d 65 61  etimes, this mea
2f311 6e 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 74 61  ns.    ** the ta
2f312 62 6c 65 20 6e 65 65 64 20 6e 65 76 65 72 20 62  ble need never b
2f313 65 20 72 65 61 64 20 66 72 6f 6d 2e 20 54 68 69  e read from. Thi
2f314 73 20 69 73 20 61 20 70 65 72 66 6f 72 6d 61 6e  s is a performan
2f315 63 65 20 62 6f 6f 73 74 2c 0a 20 20 20 20 2a 2a  ce boost,.    **
2f316 20 61 73 20 74 68 65 20 76 64 62 65 20 6c 65 76   as the vdbe lev
2f317 65 6c 20 77 61 69 74 73 20 75 6e 74 69 6c 20 74  el waits until t
2f318 68 65 20 74 61 62 6c 65 20 69 73 20 72 65 61 64  he table is read
2f319 20 62 65 66 6f 72 65 20 61 63 74 75 61 6c 6c 79   before actually
2f31a 0a 20 20 20 20 2a 2a 20 73 65 65 6b 69 6e 67 20  .    ** seeking 
2f31b 74 68 65 20 74 61 62 6c 65 20 63 75 72 73 6f 72  the table cursor
2f31c 20 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20 63   to the record c
2f31d 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
2f31e 74 68 65 20 63 75 72 72 65 6e 74 0a 20 20 20 20  the current.    
2f31f 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74  ** position in t
2f320 68 65 20 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2a  he index..    **
2f321 20 0a 20 20 20 20 2a 2a 20 43 61 6c 6c 73 20 74   .    ** Calls t
2f322 6f 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72  o the code gener
2f323 61 74 6f 72 20 69 6e 20 62 65 74 77 65 65 6e 20  ator in between 
2f324 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69  sqlite3WhereBegi
2f325 6e 20 61 6e 64 0a 20 20 20 20 2a 2a 20 73 71 6c  n and.    ** sql
2f326 69 74 65 33 57 68 65 72 65 45 6e 64 20 77 69 6c  ite3WhereEnd wil
2f327 6c 20 68 61 76 65 20 63 72 65 61 74 65 64 20 63  l have created c
2f328 6f 64 65 20 74 68 61 74 20 72 65 66 65 72 65 6e  ode that referen
2f329 63 65 73 20 74 68 65 20 74 61 62 6c 65 0a 20 20  ces the table.  
2f32a 20 20 2a 2a 20 64 69 72 65 63 74 6c 79 2e 20 20    ** directly.  
2f32b 54 68 69 73 20 6c 6f 6f 70 20 73 63 61 6e 73 20  This loop scans 
2f32c 61 6c 6c 20 74 68 61 74 20 63 6f 64 65 20 6c 6f  all that code lo
2f32d 6f 6b 69 6e 67 20 66 6f 72 20 6f 70 63 6f 64 65  oking for opcode
2f32e 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 72 65  s.    ** that re
2f32f 66 65 72 65 6e 63 65 20 74 68 65 20 74 61 62 6c  ference the tabl
2f330 65 20 61 6e 64 20 63 6f 6e 76 65 72 74 73 20 74  e and converts t
2f331 68 65 6d 20 69 6e 74 6f 20 6f 70 63 6f 64 65 73  hem into opcodes
2f332 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 72 65 66   that.    ** ref
2f333 65 72 65 6e 63 65 20 74 68 65 20 69 6e 64 65 78  erence the index
2f334 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
2f335 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77   (pLevel->plan.w
2f336 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49  sFlags & WHERE_I
2f337 4e 44 45 58 45 44 29 21 3d 30 20 26 26 20 21 64  NDEXED)!=0 && !d
2f338 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29  b->mallocFailed)
2f339 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b 2c 20 6a  {.      int k, j
2f33a 2c 20 6c 61 73 74 3b 0a 20 20 20 20 20 20 56 64  , last;.      Vd
2f33b 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 20  beOp *pOp;.     
2f33c 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 70   Index *pIdx = p
2f33d 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49  Level->plan.u.pI
2f33e 64 78 3b 0a 20 20 20 20 20 20 69 6e 74 20 75 73  dx;.      int us
2f33f 65 49 6e 64 65 78 4f 6e 6c 79 20 3d 20 70 4c 65  eIndexOnly = pLe
2f340 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
2f341 73 20 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e  s & WHERE_IDX_ON
2f342 4c 59 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72  LY;..      asser
2f343 74 28 20 70 49 64 78 21 3d 30 20 29 3b 0a 20 20  t( pIdx!=0 );.  
2f344 20 20 20 20 70 4f 70 20 3d 20 73 71 6c 69 74 65      pOp = sqlite
2f345 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20 70 57  3VdbeGetOp(v, pW
2f346 49 6e 66 6f 2d 3e 69 54 6f 70 29 3b 0a 20 20 20  Info->iTop);.   
2f347 20 20 20 6c 61 73 74 20 3d 20 73 71 6c 69 74 65     last = sqlite
2f348 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
2f349 28 76 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6b  (v);.      for(k
2f34a 3d 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 3b 20 6b  =pWInfo->iTop; k
2f34b 3c 6c 61 73 74 3b 20 6b 2b 2b 2c 20 70 4f 70 2b  <last; k++, pOp+
2f34c 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
2f34d 70 4f 70 2d 3e 70 31 21 3d 70 4c 65 76 65 6c 2d  pOp->p1!=pLevel-
2f34e 3e 69 54 61 62 43 75 72 20 29 20 63 6f 6e 74 69  >iTabCur ) conti
2f34f 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28  nue;.        if(
2f350 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
2f351 5f 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20  _Column ){.     
2f352 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
2f353 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a  pIdx->nColumn; j
2f354 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
2f355 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 70 49   if( pOp->p2==pI
2f356 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 20  dx->aiColumn[j] 
2f357 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
2f358 20 70 4f 70 2d 3e 70 32 20 3d 20 6a 3b 0a 20 20   pOp->p2 = j;.  
2f359 20 20 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d              pOp-
2f35a 3e 70 31 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 49  >p1 = pLevel->iI
2f35b 64 78 43 75 72 3b 0a 20 20 20 20 20 20 20 20 20  dxCur;.         
2f35c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2f35d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2f35e 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
2f35f 61 73 73 65 72 74 28 21 75 73 65 49 6e 64 65 78  assert(!useIndex
2f360 4f 6e 6c 79 20 7c 7c 20 6a 3c 70 49 64 78 2d 3e  Only || j<pIdx->
2f361 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20  nColumn);.      
2f362 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d    }else if( pOp-
2f363 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 69  >opcode==OP_Rowi
2f364 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  d ){.          p
2f365 4f 70 2d 3e 70 31 20 3d 20 70 4c 65 76 65 6c 2d  Op->p1 = pLevel-
2f366 3e 69 49 64 78 43 75 72 3b 0a 20 20 20 20 20 20  >iIdxCur;.      
2f367 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20      pOp->opcode 
2f368 3d 20 4f 50 5f 49 64 78 52 6f 77 69 64 3b 0a 20  = OP_IdxRowid;. 
2f369 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
2f36a 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
2f36b 5f 4e 75 6c 6c 52 6f 77 20 26 26 20 75 73 65 49  _NullRow && useI
2f36c 6e 64 65 78 4f 6e 6c 79 20 29 7b 0a 20 20 20 20  ndexOnly ){.    
2f36d 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64        pOp->opcod
2f36e 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20  e = OP_Noop;.   
2f36f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
2f370 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46     }.  }..  /* F
2f371 69 6e 61 6c 20 63 6c 65 61 6e 75 70 0a 20 20 2a  inal cleanup.  *
2f372 2f 0a 20 20 77 68 65 72 65 49 6e 66 6f 46 72 65  /.  whereInfoFre
2f373 65 28 64 62 2c 20 70 57 49 6e 66 6f 29 3b 0a 20  e(db, pWInfo);. 
2f374 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 2a 2a   return;.}../***
2f375 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
2f376 6f 66 20 77 68 65 72 65 2e 63 20 2a 2a 2a 2a 2a  of where.c *****
2f377 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f378 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f379 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2f37a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
2f37b 6e 20 66 69 6c 65 20 70 61 72 73 65 2e 63 20 2a  n file parse.c *
2f37c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f37d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f37e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44  **********/./* D
2f37f 72 69 76 65 72 20 74 65 6d 70 6c 61 74 65 20 66  river template f
2f380 6f 72 20 74 68 65 20 4c 45 4d 4f 4e 20 70 61 72  or the LEMON par
2f381 73 65 72 20 67 65 6e 65 72 61 74 6f 72 2e 0a 2a  ser generator..*
2f382 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
2f383 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
2f384 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
2f385 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  code..**.** This
2f386 20 76 65 72 73 69 6f 6e 20 6f 66 20 22 6c 65 6d   version of "lem
2f387 70 61 72 2e 63 22 20 69 73 20 6d 6f 64 69 66 69  par.c" is modifi
2f388 65 64 2c 20 73 6c 69 67 68 74 6c 79 2c 20 66 6f  ed, slightly, fo
2f389 72 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 2e  r use by SQLite.
2f38a 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 6d 6f 64  .** The only mod
2f38b 69 66 69 63 61 74 69 6f 6e 73 20 61 72 65 20 74  ifications are t
2f38c 68 65 20 61 64 64 69 74 69 6f 6e 20 6f 66 20 61  he addition of a
2f38d 20 63 6f 75 70 6c 65 20 6f 66 20 4e 45 56 45 52   couple of NEVER
2f38e 28 29 0a 2a 2a 20 6d 61 63 72 6f 73 20 74 6f 20  ().** macros to 
2f38f 64 69 73 61 62 6c 65 20 74 65 73 74 73 20 74 68  disable tests th
2f390 61 74 20 61 72 65 20 6e 65 65 64 65 64 20 69 6e  at are needed in
2f391 20 74 68 65 20 63 61 73 65 20 6f 66 20 61 20 67   the case of a g
2f392 65 6e 65 72 61 6c 0a 2a 2a 20 4c 41 4c 52 28 31  eneral.** LALR(1
2f393 29 20 67 72 61 6d 6d 61 72 20 62 75 74 20 77 68  ) grammar but wh
2f394 69 63 68 20 61 72 65 20 61 6c 77 61 79 73 20 66  ich are always f
2f395 61 6c 73 65 20 69 6e 20 74 68 65 0a 2a 2a 20 73  alse in the.** s
2f396 70 65 63 69 66 69 63 20 67 72 61 6d 6d 61 72 20  pecific grammar 
2f397 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a  used by SQLite..
2f398 2a 2f 0a 2f 2a 20 46 69 72 73 74 20 6f 66 66 2c  */./* First off,
2f399 20 63 6f 64 65 20 69 73 20 69 6e 63 6c 75 64 65   code is include
2f39a 64 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 74  d that follows t
2f39b 68 65 20 22 69 6e 63 6c 75 64 65 22 20 64 65 63  he "include" dec
2f39c 6c 61 72 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20 74  laration.** in t
2f39d 68 65 20 69 6e 70 75 74 20 67 72 61 6d 6d 61 72  he input grammar
2f39e 20 66 69 6c 65 2e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a   file. */.../*.*
2f39f 2a 20 44 69 73 61 62 6c 65 20 61 6c 6c 20 65 72  * Disable all er
2f3a0 72 6f 72 20 72 65 63 6f 76 65 72 79 20 70 72 6f  ror recovery pro
2f3a1 63 65 73 73 69 6e 67 20 69 6e 20 74 68 65 20 70  cessing in the p
2f3a2 61 72 73 65 72 20 70 75 73 68 2d 64 6f 77 6e 0a  arser push-down.
2f3a3 2a 2a 20 61 75 74 6f 6d 61 74 6f 6e 2e 0a 2a 2f  ** automaton..*/
2f3a4 0a 23 64 65 66 69 6e 65 20 59 59 4e 4f 45 52 52  .#define YYNOERR
2f3a5 4f 52 52 45 43 4f 56 45 52 59 20 31 0a 0a 2f 2a  ORRECOVERY 1../*
2f3a6 0a 2a 2a 20 4d 61 6b 65 20 79 79 74 65 73 74 63  .** Make yytestc
2f3a7 61 73 65 28 29 20 74 68 65 20 73 61 6d 65 20 61  ase() the same a
2f3a8 73 20 74 65 73 74 63 61 73 65 28 29 0a 2a 2f 0a  s testcase().*/.
2f3a9 23 64 65 66 69 6e 65 20 79 79 74 65 73 74 63 61  #define yytestca
2f3aa 73 65 28 58 29 20 74 65 73 74 63 61 73 65 28 58  se(X) testcase(X
2f3ab 29 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  )../*.** An inst
2f3ac 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
2f3ad 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69 6e 66  ucture holds inf
2f3ae 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
2f3af 68 65 0a 2a 2a 20 4c 49 4d 49 54 20 63 6c 61 75  he.** LIMIT clau
2f3b0 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73  se of a SELECT s
2f3b1 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72  tatement..*/.str
2f3b2 75 63 74 20 4c 69 6d 69 74 56 61 6c 20 7b 0a 20  uct LimitVal {. 
2f3b3 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 3b 20 20   Expr *pLimit;  
2f3b4 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 65    /* The LIMIT e
2f3b5 78 70 72 65 73 73 69 6f 6e 2e 20 20 4e 55 4c 4c  xpression.  NULL
2f3b6 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   if there is no 
2f3b7 6c 69 6d 69 74 20 2a 2f 0a 20 20 45 78 70 72 20  limit */.  Expr 
2f3b8 2a 70 4f 66 66 73 65 74 3b 20 20 20 2f 2a 20 54  *pOffset;   /* T
2f3b9 68 65 20 4f 46 46 53 45 54 20 65 78 70 72 65 73  he OFFSET expres
2f3ba 73 69 6f 6e 2e 20 20 4e 55 4c 4c 20 69 66 20 74  sion.  NULL if t
2f3bb 68 65 72 65 20 69 73 20 6e 6f 6e 65 20 2a 2f 0a  here is none */.
2f3bc 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  };../*.** An ins
2f3bd 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74  tance of this st
2f3be 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64 20  ructure is used 
2f3bf 74 6f 20 73 74 6f 72 65 20 74 68 65 20 4c 49 4b  to store the LIK
2f3c0 45 2c 0a 2a 2a 20 47 4c 4f 42 2c 20 4e 4f 54 20  E,.** GLOB, NOT 
2f3c1 4c 49 4b 45 2c 20 61 6e 64 20 4e 4f 54 20 47 4c  LIKE, and NOT GL
2f3c2 4f 42 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a 2f  OB operators..*/
2f3c3 0a 73 74 72 75 63 74 20 4c 69 6b 65 4f 70 20 7b  .struct LikeOp {
2f3c4 0a 20 20 54 6f 6b 65 6e 20 65 4f 70 65 72 61 74  .  Token eOperat
2f3c5 6f 72 3b 20 20 2f 2a 20 22 6c 69 6b 65 22 20 6f  or;  /* "like" o
2f3c6 72 20 22 67 6c 6f 62 22 20 6f 72 20 22 72 65 67  r "glob" or "reg
2f3c7 65 78 70 22 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f  exp" */.  int no
2f3c8 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  t;         /* Tr
2f3c9 75 65 20 69 66 20 74 68 65 20 4e 4f 54 20 6b 65  ue if the NOT ke
2f3ca 79 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74  yword is present
2f3cb 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
2f3cc 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
2f3cd 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
2f3ce 74 75 72 65 20 64 65 73 63 72 69 62 65 73 20 74  ture describes t
2f3cf 68 65 20 65 76 65 6e 74 20 6f 66 20 61 0a 2a 2a  he event of a.**
2f3d0 20 54 52 49 47 47 45 52 2e 20 20 22 61 22 20 69   TRIGGER.  "a" i
2f3d1 73 20 74 68 65 20 65 76 65 6e 74 20 74 79 70 65  s the event type
2f3d2 2c 20 6f 6e 65 20 6f 66 20 54 4b 5f 55 50 44 41  , one of TK_UPDA
2f3d3 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 0a 2a  TE, TK_INSERT,.*
2f3d4 2a 20 54 4b 5f 44 45 4c 45 54 45 2c 20 6f 72 20  * TK_DELETE, or 
2f3d5 54 4b 5f 49 4e 53 54 45 41 44 2e 20 20 49 66 20  TK_INSTEAD.  If 
2f3d6 74 68 65 20 65 76 65 6e 74 20 69 73 20 6f 66 20  the event is of 
2f3d7 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20  the form.**.**  
2f3d8 20 20 20 20 55 50 44 41 54 45 20 4f 4e 20 28 61      UPDATE ON (a
2f3d9 2c 62 2c 63 29 0a 2a 2a 0a 2a 2a 20 54 68 65 6e  ,b,c).**.** Then
2f3da 20 74 68 65 20 22 62 22 20 49 64 4c 69 73 74 20   the "b" IdList 
2f3db 72 65 63 6f 72 64 73 20 74 68 65 20 6c 69 73 74  records the list
2f3dc 20 22 61 2c 62 2c 63 22 2e 0a 2a 2f 0a 73 74 72   "a,b,c"..*/.str
2f3dd 75 63 74 20 54 72 69 67 45 76 65 6e 74 20 7b 20  uct TrigEvent { 
2f3de 69 6e 74 20 61 3b 20 49 64 4c 69 73 74 20 2a 20  int a; IdList * 
2f3df 62 3b 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  b; };../*.** An 
2f3e0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
2f3e1 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73   structure holds
2f3e2 20 74 68 65 20 41 54 54 41 43 48 20 6b 65 79 20   the ATTACH key 
2f3e3 61 6e 64 20 74 68 65 20 6b 65 79 20 74 79 70 65  and the key type
2f3e4 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 41 74 74 61  ..*/.struct Atta
2f3e5 63 68 4b 65 79 20 7b 20 69 6e 74 20 74 79 70 65  chKey { int type
2f3e6 3b 20 20 54 6f 6b 65 6e 20 6b 65 79 3b 20 7d 3b  ;  Token key; };
2f3e7 0a 0a 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 20  ...  /* This is 
2f3e8 61 20 75 74 69 6c 69 74 79 20 72 6f 75 74 69 6e  a utility routin
2f3e9 65 20 75 73 65 64 20 74 6f 20 73 65 74 20 74 68  e used to set th
2f3ea 65 20 45 78 70 72 53 70 61 6e 2e 7a 53 74 61 72  e ExprSpan.zStar
2f3eb 74 20 61 6e 64 0a 20 20 2a 2a 20 45 78 70 72 53  t and.  ** ExprS
2f3ec 70 61 6e 2e 7a 45 6e 64 20 76 61 6c 75 65 73 20  pan.zEnd values 
2f3ed 6f 66 20 70 4f 75 74 20 73 6f 20 74 68 61 74 20  of pOut so that 
2f3ee 74 68 65 20 73 70 61 6e 20 63 6f 76 65 72 73 20  the span covers 
2f3ef 74 68 65 20 63 6f 6d 70 6c 65 74 65 0a 20 20 2a  the complete.  *
2f3f0 2a 20 72 61 6e 67 65 20 6f 66 20 74 65 78 74 20  * range of text 
2f3f1 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 70  beginning with p
2f3f2 53 74 61 72 74 20 61 6e 64 20 67 6f 69 6e 67 20  Start and going 
2f3f3 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 70 45  to the end of pE
2f3f4 6e 64 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69  nd..  */.  stati
2f3f5 63 20 76 6f 69 64 20 73 70 61 6e 53 65 74 28 45  c void spanSet(E
2f3f6 78 70 72 53 70 61 6e 20 2a 70 4f 75 74 2c 20 54  xprSpan *pOut, T
2f3f7 6f 6b 65 6e 20 2a 70 53 74 61 72 74 2c 20 54 6f  oken *pStart, To
2f3f8 6b 65 6e 20 2a 70 45 6e 64 29 7b 0a 20 20 20 20  ken *pEnd){.    
2f3f9 70 4f 75 74 2d 3e 7a 53 74 61 72 74 20 3d 20 70  pOut->zStart = p
2f3fa 53 74 61 72 74 2d 3e 7a 3b 0a 20 20 20 20 70 4f  Start->z;.    pO
2f3fb 75 74 2d 3e 7a 45 6e 64 20 3d 20 26 70 45 6e 64  ut->zEnd = &pEnd
2f3fc 2d 3e 7a 5b 70 45 6e 64 2d 3e 6e 5d 3b 0a 20 20  ->z[pEnd->n];.  
2f3fd 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63  }..  /* Construc
2f3fe 74 20 61 20 6e 65 77 20 45 78 70 72 20 6f 62 6a  t a new Expr obj
2f3ff 65 63 74 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c  ect from a singl
2f400 65 20 69 64 65 6e 74 69 66 69 65 72 2e 20 20 55  e identifier.  U
2f401 73 65 20 74 68 65 0a 20 20 2a 2a 20 6e 65 77 20  se the.  ** new 
2f402 45 78 70 72 20 74 6f 20 70 6f 70 75 6c 61 74 65  Expr to populate
2f403 20 70 4f 75 74 2e 20 20 53 65 74 20 74 68 65 20   pOut.  Set the 
2f404 73 70 61 6e 20 6f 66 20 70 4f 75 74 20 74 6f 20  span of pOut to 
2f405 62 65 20 74 68 65 20 69 64 65 6e 74 69 66 69 65  be the identifie
2f406 72 0a 20 20 2a 2a 20 74 68 61 74 20 63 72 65 61  r.  ** that crea
2f407 74 65 64 20 74 68 65 20 65 78 70 72 65 73 73 69  ted the expressi
2f408 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69  on..  */.  stati
2f409 63 20 76 6f 69 64 20 73 70 61 6e 45 78 70 72 28  c void spanExpr(
2f40a 45 78 70 72 53 70 61 6e 20 2a 70 4f 75 74 2c 20  ExprSpan *pOut, 
2f40b 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
2f40c 6e 74 20 6f 70 2c 20 54 6f 6b 65 6e 20 2a 70 56  nt op, Token *pV
2f40d 61 6c 75 65 29 7b 0a 20 20 20 20 70 4f 75 74 2d  alue){.    pOut-
2f40e 3e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  >pExpr = sqlite3
2f40f 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 6f 70  PExpr(pParse, op
2f410 2c 20 30 2c 20 30 2c 20 70 56 61 6c 75 65 29 3b  , 0, 0, pValue);
2f411 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 53 74 61 72  .    pOut->zStar
2f412 74 20 3d 20 70 56 61 6c 75 65 2d 3e 7a 3b 0a 20  t = pValue->z;. 
2f413 20 20 20 70 4f 75 74 2d 3e 7a 45 6e 64 20 3d 20     pOut->zEnd = 
2f414 26 70 56 61 6c 75 65 2d 3e 7a 5b 70 56 61 6c 75  &pValue->z[pValu
2f415 65 2d 3e 6e 5d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  e->n];.  }..  /*
2f416 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f   This routine co
2f417 6e 73 74 72 75 63 74 73 20 61 20 62 69 6e 61 72  nstructs a binar
2f418 79 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64  y expression nod
2f419 65 20 6f 75 74 20 6f 66 20 74 77 6f 20 45 78 70  e out of two Exp
2f41a 72 53 70 61 6e 0a 20 20 2a 2a 20 6f 62 6a 65 63  rSpan.  ** objec
2f41b 74 73 20 61 6e 64 20 75 73 65 73 20 74 68 65 20  ts and uses the 
2f41c 72 65 73 75 6c 74 20 74 6f 20 70 6f 70 75 6c 61  result to popula
2f41d 74 65 20 61 20 6e 65 77 20 45 78 70 72 53 70 61  te a new ExprSpa
2f41e 6e 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2f 0a 20  n object..  */. 
2f41f 20 73 74 61 74 69 63 20 76 6f 69 64 20 73 70 61   static void spa
2f420 6e 42 69 6e 61 72 79 45 78 70 72 28 0a 20 20 20  nBinaryExpr(.   
2f421 20 45 78 70 72 53 70 61 6e 20 2a 70 4f 75 74 2c   ExprSpan *pOut,
2f422 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68       /* Write th
2f423 65 20 72 65 73 75 6c 74 20 68 65 72 65 20 2a 2f  e result here */
2f424 0a 20 20 20 20 50 61 72 73 65 20 2a 70 50 61 72  .    Parse *pPar
2f425 73 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  se,      /* The 
2f426 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e  parsing context.
2f427 20 20 45 72 72 6f 72 73 20 61 63 63 75 6d 75 6c    Errors accumul
2f428 61 74 65 20 68 65 72 65 20 2a 2f 0a 20 20 20 20  ate here */.    
2f429 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20  int op,         
2f42a 20 20 20 20 2f 2a 20 54 68 65 20 62 69 6e 61 72      /* The binar
2f42b 79 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20  y operation */. 
2f42c 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70 4c 65     ExprSpan *pLe
2f42d 66 74 2c 20 20 20 20 2f 2a 20 54 68 65 20 6c 65  ft,    /* The le
2f42e 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  ft operand */.  
2f42f 20 20 45 78 70 72 53 70 61 6e 20 2a 70 52 69 67    ExprSpan *pRig
2f430 68 74 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67  ht    /* The rig
2f431 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  ht operand */.  
2f432 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 70 45 78  ){.    pOut->pEx
2f433 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  pr = sqlite3PExp
2f434 72 28 70 50 61 72 73 65 2c 20 6f 70 2c 20 70 4c  r(pParse, op, pL
2f435 65 66 74 2d 3e 70 45 78 70 72 2c 20 70 52 69 67  eft->pExpr, pRig
2f436 68 74 2d 3e 70 45 78 70 72 2c 20 30 29 3b 0a 20  ht->pExpr, 0);. 
2f437 20 20 20 70 4f 75 74 2d 3e 7a 53 74 61 72 74 20     pOut->zStart 
2f438 3d 20 70 4c 65 66 74 2d 3e 7a 53 74 61 72 74 3b  = pLeft->zStart;
2f439 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 45 6e 64 20  .    pOut->zEnd 
2f43a 3d 20 70 52 69 67 68 74 2d 3e 7a 45 6e 64 3b 0a  = pRight->zEnd;.
2f43b 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72    }..  /* Constr
2f43c 75 63 74 20 61 6e 20 65 78 70 72 65 73 73 69 6f  uct an expressio
2f43d 6e 20 6e 6f 64 65 20 66 6f 72 20 61 20 75 6e 61  n node for a una
2f43e 72 79 20 70 6f 73 74 66 69 78 20 6f 70 65 72 61  ry postfix opera
2f43f 74 6f 72 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69  tor.  */.  stati
2f440 63 20 76 6f 69 64 20 73 70 61 6e 55 6e 61 72 79  c void spanUnary
2f441 50 6f 73 74 66 69 78 28 0a 20 20 20 20 45 78 70  Postfix(.    Exp
2f442 72 53 70 61 6e 20 2a 70 4f 75 74 2c 20 20 20 20  rSpan *pOut,    
2f443 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
2f444 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20   new expression 
2f445 6e 6f 64 65 20 68 65 72 65 20 2a 2f 0a 20 20 20  node here */.   
2f446 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2f447 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
2f448 6e 67 20 63 6f 6e 74 65 78 74 20 74 6f 20 72 65  ng context to re
2f449 63 6f 72 64 20 65 72 72 6f 72 73 20 2a 2f 0a 20  cord errors */. 
2f44a 20 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20     int op,      
2f44b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2f44c 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20   operator */.   
2f44d 20 45 78 70 72 53 70 61 6e 20 2a 70 4f 70 65 72   ExprSpan *pOper
2f44e 61 6e 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6f  and,    /* The o
2f44f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 54 6f  perand */.    To
2f450 6b 65 6e 20 2a 70 50 6f 73 74 4f 70 20 20 20 20  ken *pPostOp    
2f451 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 65 72       /* The oper
2f452 61 6e 64 20 74 6f 6b 65 6e 20 66 6f 72 20 73 65  and token for se
2f453 74 74 69 6e 67 20 74 68 65 20 73 70 61 6e 20 2a  tting the span *
2f454 2f 0a 20 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d  /.  ){.    pOut-
2f455 3e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  >pExpr = sqlite3
2f456 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 6f 70  PExpr(pParse, op
2f457 2c 20 70 4f 70 65 72 61 6e 64 2d 3e 70 45 78 70  , pOperand->pExp
2f458 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 70 4f  r, 0, 0);.    pO
2f459 75 74 2d 3e 7a 53 74 61 72 74 20 3d 20 70 4f 70  ut->zStart = pOp
2f45a 65 72 61 6e 64 2d 3e 7a 53 74 61 72 74 3b 0a 20  erand->zStart;. 
2f45b 20 20 20 70 4f 75 74 2d 3e 7a 45 6e 64 20 3d 20     pOut->zEnd = 
2f45c 26 70 50 6f 73 74 4f 70 2d 3e 7a 5b 70 50 6f 73  &pPostOp->z[pPos
2f45d 74 4f 70 2d 3e 6e 5d 3b 0a 20 20 7d 20 20 20 20  tOp->n];.  }    
2f45e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f45f 20 20 20 20 20 20 20 0a 0a 20 20 2f 2a 20 43 6f         ..  /* Co
2f460 6e 73 74 72 75 63 74 20 61 6e 20 65 78 70 72 65  nstruct an expre
2f461 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61  ssion node for a
2f462 20 75 6e 61 72 79 20 70 72 65 66 69 78 20 6f 70   unary prefix op
2f463 65 72 61 74 6f 72 0a 20 20 2a 2f 0a 20 20 73 74  erator.  */.  st
2f464 61 74 69 63 20 76 6f 69 64 20 73 70 61 6e 55 6e  atic void spanUn
2f465 61 72 79 50 72 65 66 69 78 28 0a 20 20 20 20 45  aryPrefix(.    E
2f466 78 70 72 53 70 61 6e 20 2a 70 4f 75 74 2c 20 20  xprSpan *pOut,  
2f467 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
2f468 68 65 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f  he new expressio
2f469 6e 20 6e 6f 64 65 20 68 65 72 65 20 2a 2f 0a 20  n node here */. 
2f46a 20 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65     Parse *pParse
2f46b 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  ,         /* Par
2f46c 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 74 6f 20  sing context to 
2f46d 72 65 63 6f 72 64 20 65 72 72 6f 72 73 20 2a 2f  record errors */
2f46e 0a 20 20 20 20 69 6e 74 20 6f 70 2c 20 20 20 20  .    int op,    
2f46f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2f470 68 65 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20  he operator */. 
2f471 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70 4f 70     ExprSpan *pOp
2f472 65 72 61 6e 64 2c 20 20 20 20 2f 2a 20 54 68 65  erand,    /* The
2f473 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20   operand */.    
2f474 54 6f 6b 65 6e 20 2a 70 50 72 65 4f 70 20 20 20  Token *pPreOp   
2f475 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 65        /* The ope
2f476 72 61 6e 64 20 74 6f 6b 65 6e 20 66 6f 72 20 73  rand token for s
2f477 65 74 74 69 6e 67 20 74 68 65 20 73 70 61 6e 20  etting the span 
2f478 2a 2f 0a 20 20 29 7b 0a 20 20 20 20 70 4f 75 74  */.  ){.    pOut
2f479 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65  ->pExpr = sqlite
2f47a 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 6f  3PExpr(pParse, o
2f47b 70 2c 20 70 4f 70 65 72 61 6e 64 2d 3e 70 45 78  p, pOperand->pEx
2f47c 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 70  pr, 0, 0);.    p
2f47d 4f 75 74 2d 3e 7a 53 74 61 72 74 20 3d 20 70 50  Out->zStart = pP
2f47e 72 65 4f 70 2d 3e 7a 3b 0a 20 20 20 20 70 4f 75  reOp->z;.    pOu
2f47f 74 2d 3e 7a 45 6e 64 20 3d 20 70 4f 70 65 72 61  t->zEnd = pOpera
2f480 6e 64 2d 3e 7a 45 6e 64 3b 0a 20 20 7d 0a 2f 2a  nd->zEnd;.  }./*
2f481 20 4e 65 78 74 20 69 73 20 61 6c 6c 20 74 6f 6b   Next is all tok
2f482 65 6e 20 76 61 6c 75 65 73 2c 20 69 6e 20 61 20  en values, in a 
2f483 66 6f 72 6d 20 73 75 69 74 61 62 6c 65 20 66 6f  form suitable fo
2f484 72 20 75 73 65 20 62 79 20 6d 61 6b 65 68 65 61  r use by makehea
2f485 64 65 72 73 2e 0a 2a 2a 20 54 68 69 73 20 73 65  ders..** This se
2f486 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 6e 75  ction will be nu
2f487 6c 6c 20 75 6e 6c 65 73 73 20 6c 65 6d 6f 6e 20  ll unless lemon 
2f488 69 73 20 72 75 6e 20 77 69 74 68 20 74 68 65 20  is run with the 
2f489 2d 6d 20 73 77 69 74 63 68 2e 0a 2a 2f 0a 2f 2a  -m switch..*/./*
2f48a 20 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74   .** These const
2f48b 61 6e 74 73 20 28 61 6c 6c 20 67 65 6e 65 72 61  ants (all genera
2f48c 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  ted automaticall
2f48d 79 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20  y by the parser 
2f48e 67 65 6e 65 72 61 74 6f 72 29 0a 2a 2a 20 73 70  generator).** sp
2f48f 65 63 69 66 79 20 74 68 65 20 76 61 72 69 6f 75  ecify the variou
2f490 73 20 6b 69 6e 64 73 20 6f 66 20 74 6f 6b 65 6e  s kinds of token
2f491 73 20 28 74 65 72 6d 69 6e 61 6c 73 29 20 74 68  s (terminals) th
2f492 61 74 20 74 68 65 20 70 61 72 73 65 72 0a 2a 2a  at the parser.**
2f493 20 75 6e 64 65 72 73 74 61 6e 64 73 2e 20 0a 2a   understands. .*
2f494 2a 0a 2a 2a 20 45 61 63 68 20 73 79 6d 62 6f 6c  *.** Each symbol
2f495 20 68 65 72 65 20 69 73 20 61 20 74 65 72 6d 69   here is a termi
2f496 6e 61 6c 20 73 79 6d 62 6f 6c 20 69 6e 20 74 68  nal symbol in th
2f497 65 20 67 72 61 6d 6d 61 72 2e 0a 2a 2f 0a 2f 2a  e grammar..*/./*
2f498 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 49   Make sure the I
2f499 4e 54 45 52 46 41 43 45 20 6d 61 63 72 6f 20 69  NTERFACE macro i
2f49a 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69  s defined..*/.#i
2f49b 66 6e 64 65 66 20 49 4e 54 45 52 46 41 43 45 0a  fndef INTERFACE.
2f49c 23 20 64 65 66 69 6e 65 20 49 4e 54 45 52 46 41  # define INTERFA
2f49d 43 45 20 31 0a 23 65 6e 64 69 66 0a 2f 2a 20 54  CE 1.#endif./* T
2f49e 68 65 20 6e 65 78 74 20 74 68 69 6e 67 20 69 6e  he next thing in
2f49f 63 6c 75 64 65 64 20 69 73 20 73 65 72 69 65 73  cluded is series
2f4a0 20 6f 66 20 64 65 66 69 6e 65 73 20 77 68 69 63   of defines whic
2f4a1 68 20 63 6f 6e 74 72 6f 6c 0a 2a 2a 20 76 61 72  h control.** var
2f4a2 69 6f 75 73 20 61 73 70 65 63 74 73 20 6f 66 20  ious aspects of 
2f4a3 74 68 65 20 67 65 6e 65 72 61 74 65 64 20 70 61  the generated pa
2f4a4 72 73 65 72 2e 0a 2a 2a 20 20 20 20 59 59 43 4f  rser..**    YYCO
2f4a5 44 45 54 59 50 45 20 20 20 20 20 20 20 20 20 69  DETYPE         i
2f4a6 73 20 74 68 65 20 64 61 74 61 20 74 79 70 65 20  s the data type 
2f4a7 75 73 65 64 20 66 6f 72 20 73 74 6f 72 69 6e 67  used for storing
2f4a8 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20 20 20 20   terminal.**    
2f4a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f4aa 20 20 20 61 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e     and nontermin
2f4ab 61 6c 20 6e 75 6d 62 65 72 73 2e 20 20 22 75 6e  al numbers.  "un
2f4ac 73 69 67 6e 65 64 20 63 68 61 72 22 20 69 73 0a  signed char" is.
2f4ad 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2f4ae 20 20 20 20 20 20 20 20 20 75 73 65 64 20 69 66           used if
2f4af 20 74 68 65 72 65 20 61 72 65 20 66 65 77 65 72   there are fewer
2f4b0 20 74 68 61 6e 20 32 35 30 20 74 65 72 6d 69 6e   than 250 termin
2f4b1 61 6c 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  als.**          
2f4b2 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 64               and
2f4b3 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 73 2e 20 20   nonterminals.  
2f4b4 22 69 6e 74 22 20 69 73 20 75 73 65 64 20 6f 74  "int" is used ot
2f4b5 68 65 72 77 69 73 65 2e 0a 2a 2a 20 20 20 20 59  herwise..**    Y
2f4b6 59 4e 4f 43 4f 44 45 20 20 20 20 20 20 20 20 20  YNOCODE         
2f4b7 20 20 69 73 20 61 20 6e 75 6d 62 65 72 20 6f 66    is a number of
2f4b8 20 74 79 70 65 20 59 59 43 4f 44 45 54 59 50 45   type YYCODETYPE
2f4b9 20 77 68 69 63 68 20 63 6f 72 72 65 73 70 6f 6e   which correspon
2f4ba 64 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ds.**           
2f4bb 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 20 6e              to n
2f4bc 6f 20 6c 65 67 61 6c 20 74 65 72 6d 69 6e 61 6c  o legal terminal
2f4bd 20 6f 72 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20   or nonterminal 
2f4be 6e 75 6d 62 65 72 2e 20 20 54 68 69 73 0a 2a 2a  number.  This.**
2f4bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f4c0 20 20 20 20 20 20 20 6e 75 6d 62 65 72 20 69 73         number is
2f4c1 20 75 73 65 64 20 74 6f 20 66 69 6c 6c 20 69 6e   used to fill in
2f4c2 20 65 6d 70 74 79 20 73 6c 6f 74 73 20 6f 66 20   empty slots of 
2f4c3 74 68 65 20 68 61 73 68 20 0a 2a 2a 20 20 20 20  the hash .**    
2f4c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f4c5 20 20 20 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20     table..**    
2f4c6 59 59 46 41 4c 4c 42 41 43 4b 20 20 20 20 20 20  YYFALLBACK      
2f4c7 20 20 20 49 66 20 64 65 66 69 6e 65 64 2c 20 74     If defined, t
2f4c8 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 74 68  his indicates th
2f4c9 61 74 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74  at one or more t
2f4ca 6f 6b 65 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20  okens.**        
2f4cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 68                 h
2f4cc 61 76 65 20 66 61 6c 6c 2d 62 61 63 6b 20 76 61  ave fall-back va
2f4cd 6c 75 65 73 20 77 68 69 63 68 20 73 68 6f 75 6c  lues which shoul
2f4ce 64 20 62 65 20 75 73 65 64 20 69 66 20 74 68 65  d be used if the
2f4cf 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2f4d0 20 20 20 20 20 20 20 20 20 20 6f 72 69 67 69 6e            origin
2f4d1 61 6c 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  al value of the 
2f4d2 74 6f 6b 65 6e 20 77 69 6c 6c 20 6e 6f 74 20 70  token will not p
2f4d3 61 72 73 65 2e 0a 2a 2a 20 20 20 20 59 59 41 43  arse..**    YYAC
2f4d4 54 49 4f 4e 54 59 50 45 20 20 20 20 20 20 20 69  TIONTYPE       i
2f4d5 73 20 74 68 65 20 64 61 74 61 20 74 79 70 65 20  s the data type 
2f4d6 75 73 65 64 20 66 6f 72 20 73 74 6f 72 69 6e 67  used for storing
2f4d7 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20 20 20 20   terminal.**    
2f4d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f4d9 20 20 20 61 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e     and nontermin
2f4da 61 6c 20 6e 75 6d 62 65 72 73 2e 20 20 22 75 6e  al numbers.  "un
2f4db 73 69 67 6e 65 64 20 63 68 61 72 22 20 69 73 0a  signed char" is.
2f4dc 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2f4dd 20 20 20 20 20 20 20 20 20 75 73 65 64 20 69 66           used if
2f4de 20 74 68 65 72 65 20 61 72 65 20 66 65 77 65 72   there are fewer
2f4df 20 74 68 61 6e 20 32 35 30 20 72 75 6c 65 73 20   than 250 rules 
2f4e0 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  and.**          
2f4e1 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61               sta
2f4e2 74 65 73 20 63 6f 6d 62 69 6e 65 64 2e 20 20 22  tes combined.  "
2f4e3 69 6e 74 22 20 69 73 20 75 73 65 64 20 6f 74 68  int" is used oth
2f4e4 65 72 77 69 73 65 2e 0a 2a 2a 20 20 20 20 73 71  erwise..**    sq
2f4e5 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b 45 4e  lite3ParserTOKEN
2f4e6 54 59 50 45 20 20 20 20 20 69 73 20 74 68 65 20  TYPE     is the 
2f4e7 64 61 74 61 20 74 79 70 65 20 75 73 65 64 20 66  data type used f
2f4e8 6f 72 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 73 20  or minor tokens 
2f4e9 67 69 76 65 6e 20 0a 2a 2a 20 20 20 20 20 20 20  given .**       
2f4ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f4eb 64 69 72 65 63 74 6c 79 20 74 6f 20 74 68 65 20  directly to the 
2f4ec 70 61 72 73 65 72 20 66 72 6f 6d 20 74 68 65 20  parser from the 
2f4ed 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2a 20 20 20  tokenizer..**   
2f4ee 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 20 20 20   YYMINORTYPE    
2f4ef 20 20 20 20 69 73 20 74 68 65 20 64 61 74 61 20      is the data 
2f4f0 74 79 70 65 20 75 73 65 64 20 66 6f 72 20 61 6c  type used for al
2f4f1 6c 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 73 2e 0a  l minor tokens..
2f4f2 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2f4f3 20 20 20 20 20 20 20 20 20 54 68 69 73 20 69 73           This is
2f4f4 20 74 79 70 69 63 61 6c 6c 79 20 61 20 75 6e 69   typically a uni
2f4f5 6f 6e 20 6f 66 20 6d 61 6e 79 20 74 79 70 65 73  on of many types
2f4f6 2c 20 6f 6e 65 20 6f 66 0a 2a 2a 20 20 20 20 20  , one of.**     
2f4f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f4f8 20 20 77 68 69 63 68 20 69 73 20 73 71 6c 69 74    which is sqlit
2f4f9 65 33 50 61 72 73 65 72 54 4f 4b 45 4e 54 59 50  e3ParserTOKENTYP
2f4fa 45 2e 20 20 54 68 65 20 65 6e 74 72 79 20 69 6e  E.  The entry in
2f4fb 20 74 68 65 20 75 6e 69 6f 6e 0a 2a 2a 20 20 20   the union.**   
2f4fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f4fd 20 20 20 20 66 6f 72 20 62 61 73 65 20 74 6f 6b      for base tok
2f4fe 65 6e 73 20 69 73 20 63 61 6c 6c 65 64 20 22 79  ens is called "y
2f4ff 79 30 22 2e 0a 2a 2a 20 20 20 20 59 59 53 54 41  y0"..**    YYSTA
2f500 43 4b 44 45 50 54 48 20 20 20 20 20 20 20 69 73  CKDEPTH       is
2f501 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 64 65 70   the maximum dep
2f502 74 68 20 6f 66 20 74 68 65 20 70 61 72 73 65 72  th of the parser
2f503 27 73 20 73 74 61 63 6b 2e 20 20 49 66 0a 2a 2a  's stack.  If.**
2f504 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f505 20 20 20 20 20 20 20 7a 65 72 6f 20 74 68 65 20         zero the 
2f506 73 74 61 63 6b 20 69 73 20 64 79 6e 61 6d 69 63  stack is dynamic
2f507 61 6c 6c 79 20 73 69 7a 65 64 20 75 73 69 6e 67  ally sized using
2f508 20 72 65 61 6c 6c 6f 63 28 29 0a 2a 2a 20 20 20   realloc().**   
2f509 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
2f50a 47 5f 53 44 45 43 4c 20 20 20 20 20 41 20 73 74  G_SDECL     A st
2f50b 61 74 69 63 20 76 61 72 69 61 62 6c 65 20 64 65  atic variable de
2f50c 63 6c 61 72 61 74 69 6f 6e 20 66 6f 72 20 74 68  claration for th
2f50d 65 20 25 65 78 74 72 61 5f 61 72 67 75 6d 65 6e  e %extra_argumen
2f50e 74 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65 33 50  t.**    sqlite3P
2f50f 61 72 73 65 72 41 52 47 5f 50 44 45 43 4c 20 20  arserARG_PDECL  
2f510 20 20 20 41 20 70 61 72 61 6d 65 74 65 72 20 64     A parameter d
2f511 65 63 6c 61 72 61 74 69 6f 6e 20 66 6f 72 20 74  eclaration for t
2f512 68 65 20 25 65 78 74 72 61 5f 61 72 67 75 6d 65  he %extra_argume
2f513 6e 74 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65 33  nt.**    sqlite3
2f514 50 61 72 73 65 72 41 52 47 5f 53 54 4f 52 45 20  ParserARG_STORE 
2f515 20 20 20 20 43 6f 64 65 20 74 6f 20 73 74 6f 72      Code to stor
2f516 65 20 25 65 78 74 72 61 5f 61 72 67 75 6d 65 6e  e %extra_argumen
2f517 74 20 69 6e 74 6f 20 79 79 70 50 61 72 73 65 72  t into yypParser
2f518 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65 33 50 61  .**    sqlite3Pa
2f519 72 73 65 72 41 52 47 5f 46 45 54 43 48 20 20 20  rserARG_FETCH   
2f51a 20 20 43 6f 64 65 20 74 6f 20 65 78 74 72 61 63    Code to extrac
2f51b 74 20 25 65 78 74 72 61 5f 61 72 67 75 6d 65 6e  t %extra_argumen
2f51c 74 20 66 72 6f 6d 20 79 79 70 50 61 72 73 65 72  t from yypParser
2f51d 0a 2a 2a 20 20 20 20 59 59 4e 53 54 41 54 45 20  .**    YYNSTATE 
2f51e 20 20 20 20 20 20 20 20 20 20 74 68 65 20 63 6f            the co
2f51f 6d 62 69 6e 65 64 20 6e 75 6d 62 65 72 20 6f 66  mbined number of
2f520 20 73 74 61 74 65 73 2e 0a 2a 2a 20 20 20 20 59   states..**    Y
2f521 59 4e 52 55 4c 45 20 20 20 20 20 20 20 20 20 20  YNRULE          
2f522 20 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20    the number of 
2f523 72 75 6c 65 73 20 69 6e 20 74 68 65 20 67 72 61  rules in the gra
2f524 6d 6d 61 72 0a 2a 2a 20 20 20 20 59 59 45 52 52  mmar.**    YYERR
2f525 4f 52 53 59 4d 42 4f 4c 20 20 20 20 20 20 69 73  ORSYMBOL      is
2f526 20 74 68 65 20 63 6f 64 65 20 6e 75 6d 62 65 72   the code number
2f527 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 73 79   of the error sy
2f528 6d 62 6f 6c 2e 20 20 49 66 20 6e 6f 74 0a 2a 2a  mbol.  If not.**
2f529 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f52a 20 20 20 20 20 20 20 64 65 66 69 6e 65 64 2c 20         defined, 
2f52b 74 68 65 6e 20 64 6f 20 6e 6f 20 65 72 72 6f 72  then do no error
2f52c 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a 2f 0a   processing..*/.
2f52d 23 64 65 66 69 6e 65 20 59 59 43 4f 44 45 54 59  #define YYCODETY
2f52e 50 45 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  PE unsigned char
2f52f 0a 23 64 65 66 69 6e 65 20 59 59 4e 4f 43 4f 44  .#define YYNOCOD
2f530 45 20 32 35 34 0a 23 64 65 66 69 6e 65 20 59 59  E 254.#define YY
2f531 41 43 54 49 4f 4e 54 59 50 45 20 75 6e 73 69 67  ACTIONTYPE unsig
2f532 6e 65 64 20 73 68 6f 72 74 20 69 6e 74 0a 23 64  ned short int.#d
2f533 65 66 69 6e 65 20 59 59 57 49 4c 44 43 41 52 44  efine YYWILDCARD
2f534 20 36 35 0a 23 64 65 66 69 6e 65 20 73 71 6c 69   65.#define sqli
2f535 74 65 33 50 61 72 73 65 72 54 4f 4b 45 4e 54 59  te3ParserTOKENTY
2f536 50 45 20 54 6f 6b 65 6e 0a 74 79 70 65 64 65 66  PE Token.typedef
2f537 20 75 6e 69 6f 6e 20 7b 0a 20 20 69 6e 74 20 79   union {.  int y
2f538 79 69 6e 69 74 3b 0a 20 20 73 71 6c 69 74 65 33  yinit;.  sqlite3
2f539 50 61 72 73 65 72 54 4f 4b 45 4e 54 59 50 45 20  ParserTOKENTYPE 
2f53a 79 79 30 3b 0a 20 20 53 65 6c 65 63 74 2a 20 79  yy0;.  Select* y
2f53b 79 33 3b 0a 20 20 45 78 70 72 4c 69 73 74 2a 20  y3;.  ExprList* 
2f53c 79 79 31 34 3b 0a 20 20 53 72 63 4c 69 73 74 2a  yy14;.  SrcList*
2f53d 20 79 79 36 35 3b 0a 20 20 73 74 72 75 63 74 20   yy65;.  struct 
2f53e 4c 69 6b 65 4f 70 20 79 79 39 36 3b 0a 20 20 45  LikeOp yy96;.  E
2f53f 78 70 72 2a 20 79 79 31 33 32 3b 0a 20 20 75 38  xpr* yy132;.  u8
2f540 20 79 79 31 38 36 3b 0a 20 20 69 6e 74 20 79 79   yy186;.  int yy
2f541 33 32 38 3b 0a 20 20 45 78 70 72 53 70 61 6e 20  328;.  ExprSpan 
2f542 79 79 33 34 36 3b 0a 20 20 73 74 72 75 63 74 20  yy346;.  struct 
2f543 54 72 69 67 45 76 65 6e 74 20 79 79 33 37 38 3b  TrigEvent yy378;
2f544 0a 20 20 49 64 4c 69 73 74 2a 20 79 79 34 30 38  .  IdList* yy408
2f545 3b 0a 20 20 73 74 72 75 63 74 20 7b 69 6e 74 20  ;.  struct {int 
2f546 76 61 6c 75 65 3b 20 69 6e 74 20 6d 61 73 6b 3b  value; int mask;
2f547 7d 20 79 79 34 32 39 3b 0a 20 20 54 72 69 67 67  } yy429;.  Trigg
2f548 65 72 53 74 65 70 2a 20 79 79 34 37 33 3b 0a 20  erStep* yy473;. 
2f549 20 73 74 72 75 63 74 20 4c 69 6d 69 74 56 61 6c   struct LimitVal
2f54a 20 79 79 34 37 36 3b 0a 7d 20 59 59 4d 49 4e 4f   yy476;.} YYMINO
2f54b 52 54 59 50 45 3b 0a 23 69 66 6e 64 65 66 20 59  RTYPE;.#ifndef Y
2f54c 59 53 54 41 43 4b 44 45 50 54 48 0a 23 64 65 66  YSTACKDEPTH.#def
2f54d 69 6e 65 20 59 59 53 54 41 43 4b 44 45 50 54 48  ine YYSTACKDEPTH
2f54e 20 31 30 30 0a 23 65 6e 64 69 66 0a 23 64 65 66   100.#endif.#def
2f54f 69 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73 65  ine sqlite3Parse
2f550 72 41 52 47 5f 53 44 45 43 4c 20 50 61 72 73 65  rARG_SDECL Parse
2f551 20 2a 70 50 61 72 73 65 3b 0a 23 64 65 66 69 6e   *pParse;.#defin
2f552 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41  e sqlite3ParserA
2f553 52 47 5f 50 44 45 43 4c 20 2c 50 61 72 73 65 20  RG_PDECL ,Parse 
2f554 2a 70 50 61 72 73 65 0a 23 64 65 66 69 6e 65 20  *pParse.#define 
2f555 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
2f556 5f 46 45 54 43 48 20 50 61 72 73 65 20 2a 70 50  _FETCH Parse *pP
2f557 61 72 73 65 20 3d 20 79 79 70 50 61 72 73 65 72  arse = yypParser
2f558 2d 3e 70 50 61 72 73 65 0a 23 64 65 66 69 6e 65  ->pParse.#define
2f559 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
2f55a 47 5f 53 54 4f 52 45 20 79 79 70 50 61 72 73 65  G_STORE yypParse
2f55b 72 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61 72  r->pParse = pPar
2f55c 73 65 0a 23 64 65 66 69 6e 65 20 59 59 4e 53 54  se.#define YYNST
2f55d 41 54 45 20 36 32 39 0a 23 64 65 66 69 6e 65 20  ATE 629.#define 
2f55e 59 59 4e 52 55 4c 45 20 33 32 39 0a 23 64 65 66  YYNRULE 329.#def
2f55f 69 6e 65 20 59 59 46 41 4c 4c 42 41 43 4b 20 31  ine YYFALLBACK 1
2f560 0a 23 64 65 66 69 6e 65 20 59 59 5f 4e 4f 5f 41  .#define YY_NO_A
2f561 43 54 49 4f 4e 20 20 20 20 20 20 28 59 59 4e 53  CTION      (YYNS
2f562 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b 32 29 0a  TATE+YYNRULE+2).
2f563 23 64 65 66 69 6e 65 20 59 59 5f 41 43 43 45 50  #define YY_ACCEP
2f564 54 5f 41 43 54 49 4f 4e 20 20 28 59 59 4e 53 54  T_ACTION  (YYNST
2f565 41 54 45 2b 59 59 4e 52 55 4c 45 2b 31 29 0a 23  ATE+YYNRULE+1).#
2f566 64 65 66 69 6e 65 20 59 59 5f 45 52 52 4f 52 5f  define YY_ERROR_
2f567 41 43 54 49 4f 4e 20 20 20 28 59 59 4e 53 54 41  ACTION   (YYNSTA
2f568 54 45 2b 59 59 4e 52 55 4c 45 29 0a 0a 2f 2a 20  TE+YYNRULE)../* 
2f569 54 68 65 20 79 79 7a 65 72 6f 6d 69 6e 6f 72 20  The yyzerominor 
2f56a 63 6f 6e 73 74 61 6e 74 20 69 73 20 75 73 65 64  constant is used
2f56b 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 69   to initialize i
2f56c 6e 73 74 61 6e 63 65 73 20 6f 66 0a 2a 2a 20 59  nstances of.** Y
2f56d 59 4d 49 4e 4f 52 54 59 50 45 20 6f 62 6a 65 63  YMINORTYPE objec
2f56e 74 73 20 74 6f 20 7a 65 72 6f 2e 20 2a 2f 0a 73  ts to zero. */.s
2f56f 74 61 74 69 63 20 63 6f 6e 73 74 20 59 59 4d 49  tatic const YYMI
2f570 4e 4f 52 54 59 50 45 20 79 79 7a 65 72 6f 6d 69  NORTYPE yyzeromi
2f571 6e 6f 72 20 3d 20 7b 20 30 20 7d 3b 0a 0a 2f 2a  nor = { 0 };../*
2f572 20 44 65 66 69 6e 65 20 74 68 65 20 79 79 74 65   Define the yyte
2f573 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 74  stcase() macro t
2f574 6f 20 62 65 20 61 20 6e 6f 2d 6f 70 20 69 66 20  o be a no-op if 
2f575 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 64  is not already d
2f576 65 66 69 6e 65 64 0a 2a 2a 20 6f 74 68 65 72 77  efined.** otherw
2f577 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69  ise..**.** Appli
2f578 63 61 74 69 6f 6e 73 20 63 61 6e 20 63 68 6f 6f  cations can choo
2f579 73 65 20 74 6f 20 64 65 66 69 6e 65 20 79 79 74  se to define yyt
2f57a 65 73 74 63 61 73 65 28 29 20 69 6e 20 74 68 65  estcase() in the
2f57b 20 25 69 6e 63 6c 75 64 65 20 73 65 63 74 69 6f   %include sectio
2f57c 6e 0a 2a 2a 20 74 6f 20 61 20 6d 61 63 72 6f 20  n.** to a macro 
2f57d 74 68 61 74 20 63 61 6e 20 61 73 73 69 73 74 20  that can assist 
2f57e 69 6e 20 76 65 72 69 66 79 69 6e 67 20 63 6f 64  in verifying cod
2f57f 65 20 63 6f 76 65 72 61 67 65 2e 20 20 46 6f 72  e coverage.  For
2f580 20 70 72 6f 64 75 63 74 69 6f 6e 0a 2a 2a 20 63   production.** c
2f581 6f 64 65 20 74 68 65 20 79 79 74 65 73 74 63 61  ode the yytestca
2f582 73 65 28 29 20 6d 61 63 72 6f 20 73 68 6f 75 6c  se() macro shoul
2f583 64 20 62 65 20 74 75 72 6e 65 64 20 6f 66 66 2e  d be turned off.
2f584 20 20 42 75 74 20 69 74 20 69 73 20 75 73 65 66    But it is usef
2f585 75 6c 0a 2a 2a 20 66 6f 72 20 74 65 73 74 69 6e  ul.** for testin
2f586 67 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 79 79  g..*/.#ifndef yy
2f587 74 65 73 74 63 61 73 65 0a 23 20 64 65 66 69 6e  testcase.# defin
2f588 65 20 79 79 74 65 73 74 63 61 73 65 28 58 29 0a  e yytestcase(X).
2f589 23 65 6e 64 69 66 0a 0a 0a 2f 2a 20 4e 65 78 74  #endif.../* Next
2f58a 20 61 72 65 20 74 68 65 20 74 61 62 6c 65 73 20   are the tables 
2f58b 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e  used to determin
2f58c 65 20 77 68 61 74 20 61 63 74 69 6f 6e 20 74 6f  e what action to
2f58d 20 74 61 6b 65 20 62 61 73 65 64 20 6f 6e 20 74   take based on t
2f58e 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 73 74  he.** current st
2f58f 61 74 65 20 61 6e 64 20 6c 6f 6f 6b 61 68 65 61  ate and lookahea
2f590 64 20 74 6f 6b 65 6e 2e 20 20 54 68 65 73 65 20  d token.  These 
2f591 74 61 62 6c 65 73 20 61 72 65 20 75 73 65 64 20  tables are used 
2f592 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20  to implement.** 
2f593 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 74  functions that t
2f594 61 6b 65 20 61 20 73 74 61 74 65 20 6e 75 6d 62  ake a state numb
2f595 65 72 20 61 6e 64 20 6c 6f 6f 6b 61 68 65 61 64  er and lookahead
2f596 20 76 61 6c 75 65 20 61 6e 64 20 72 65 74 75 72   value and retur
2f597 6e 20 61 6e 0a 2a 2a 20 61 63 74 69 6f 6e 20 69  n an.** action i
2f598 6e 74 65 67 65 72 2e 20 20 0a 2a 2a 0a 2a 2a 20  nteger.  .**.** 
2f599 53 75 70 70 6f 73 65 20 74 68 65 20 61 63 74 69  Suppose the acti
2f59a 6f 6e 20 69 6e 74 65 67 65 72 20 69 73 20 4e 2e  on integer is N.
2f59b 20 20 54 68 65 6e 20 74 68 65 20 61 63 74 69 6f    Then the actio
2f59c 6e 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20  n is determined 
2f59d 61 73 0a 2a 2a 20 66 6f 6c 6c 6f 77 73 0a 2a 2a  as.** follows.**
2f59e 0a 2a 2a 20 20 20 30 20 3c 3d 20 4e 20 3c 20 59  .**   0 <= N < Y
2f59f 59 4e 53 54 41 54 45 20 20 20 20 20 20 20 20 20  YNSTATE         
2f5a0 20 20 20 20 20 20 20 20 20 53 68 69 66 74 20 4e           Shift N
2f5a1 2e 20 20 54 68 61 74 20 69 73 2c 20 70 75 73 68  .  That is, push
2f5a2 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61 64 0a 2a   the lookahead.*
2f5a3 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2f5a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f5a5 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 6f 6e 74         token ont
2f5a6 6f 20 74 68 65 20 73 74 61 63 6b 20 61 6e 64 20  o the stack and 
2f5a7 67 6f 74 6f 20 73 74 61 74 65 20 4e 2e 0a 2a 2a  goto state N..**
2f5a8 0a 2a 2a 20 20 20 59 59 4e 53 54 41 54 45 20 3c  .**   YYNSTATE <
2f5a9 3d 20 4e 20 3c 20 59 59 4e 53 54 41 54 45 2b 59  = N < YYNSTATE+Y
2f5aa 59 4e 52 55 4c 45 20 20 20 52 65 64 75 63 65 20  YNRULE   Reduce 
2f5ab 62 79 20 72 75 6c 65 20 4e 2d 59 59 4e 53 54 41  by rule N-YYNSTA
2f5ac 54 45 2e 0a 2a 2a 0a 2a 2a 20 20 20 4e 20 3d 3d  TE..**.**   N ==
2f5ad 20 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c   YYNSTATE+YYNRUL
2f5ae 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 41  E              A
2f5af 20 73 79 6e 74 61 78 20 65 72 72 6f 72 20 68 61   syntax error ha
2f5b0 73 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 0a 2a  s occurred..**.*
2f5b1 2a 20 20 20 4e 20 3d 3d 20 59 59 4e 53 54 41 54  *   N == YYNSTAT
2f5b2 45 2b 59 59 4e 52 55 4c 45 2b 31 20 20 20 20 20  E+YYNRULE+1     
2f5b3 20 20 20 20 20 20 20 54 68 65 20 70 61 72 73 65         The parse
2f5b4 72 20 61 63 63 65 70 74 73 20 69 74 73 20 69 6e  r accepts its in
2f5b5 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 4e 20 3d  put..**.**   N =
2f5b6 3d 20 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55  = YYNSTATE+YYNRU
2f5b7 4c 45 2b 32 20 20 20 20 20 20 20 20 20 20 20 20  LE+2            
2f5b8 4e 6f 20 73 75 63 68 20 61 63 74 69 6f 6e 2e 20  No such action. 
2f5b9 20 44 65 6e 6f 74 65 73 20 75 6e 75 73 65 64 0a   Denotes unused.
2f5ba 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2f5bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f5bc 20 20 20 20 20 20 20 20 73 6c 6f 74 73 20 69 6e          slots in
2f5bd 20 74 68 65 20 79 79 5f 61 63 74 69 6f 6e 5b 5d   the yy_action[]
2f5be 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   table..**.** Th
2f5bf 65 20 61 63 74 69 6f 6e 20 74 61 62 6c 65 20 69  e action table i
2f5c0 73 20 63 6f 6e 73 74 72 75 63 74 65 64 20 61 73  s constructed as
2f5c1 20 61 20 73 69 6e 67 6c 65 20 6c 61 72 67 65 20   a single large 
2f5c2 74 61 62 6c 65 20 6e 61 6d 65 64 20 79 79 5f 61  table named yy_a
2f5c3 63 74 69 6f 6e 5b 5d 2e 0a 2a 2a 20 47 69 76 65  ction[]..** Give
2f5c4 6e 20 73 74 61 74 65 20 53 20 61 6e 64 20 6c 6f  n state S and lo
2f5c5 6f 6b 61 68 65 61 64 20 58 2c 20 74 68 65 20 61  okahead X, the a
2f5c6 63 74 69 6f 6e 20 69 73 20 63 6f 6d 70 75 74 65  ction is compute
2f5c7 64 20 61 73 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  d as.**.**      
2f5c8 79 79 5f 61 63 74 69 6f 6e 5b 20 79 79 5f 73 68  yy_action[ yy_sh
2f5c9 69 66 74 5f 6f 66 73 74 5b 53 5d 20 2b 20 58 20  ift_ofst[S] + X 
2f5ca 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69  ].**.** If the i
2f5cb 6e 64 65 78 20 76 61 6c 75 65 20 79 79 5f 73 68  ndex value yy_sh
2f5cc 69 66 74 5f 6f 66 73 74 5b 53 5d 2b 58 20 69 73  ift_ofst[S]+X is
2f5cd 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 6f 72   out of range or
2f5ce 20 69 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a   if the value.**
2f5cf 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 79 79   yy_lookahead[yy
2f5d0 5f 73 68 69 66 74 5f 6f 66 73 74 5b 53 5d 2b 58  _shift_ofst[S]+X
2f5d1 5d 20 69 73 20 6e 6f 74 20 65 71 75 61 6c 20 74  ] is not equal t
2f5d2 6f 20 58 20 6f 72 20 69 66 20 79 79 5f 73 68 69  o X or if yy_shi
2f5d3 66 74 5f 6f 66 73 74 5b 53 5d 0a 2a 2a 20 69 73  ft_ofst[S].** is
2f5d4 20 65 71 75 61 6c 20 74 6f 20 59 59 5f 53 48 49   equal to YY_SHI
2f5d5 46 54 5f 55 53 45 5f 44 46 4c 54 2c 20 69 74 20  FT_USE_DFLT, it 
2f5d6 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 61  means that the a
2f5d7 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 69 6e 20  ction is not in 
2f5d8 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 61 6e 64  the table.** and
2f5d9 20 74 68 61 74 20 79 79 5f 64 65 66 61 75 6c 74   that yy_default
2f5da 5b 53 5d 20 73 68 6f 75 6c 64 20 62 65 20 75 73  [S] should be us
2f5db 65 64 20 69 6e 73 74 65 61 64 2e 20 20 0a 2a 2a  ed instead.  .**
2f5dc 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 75 6c 61 20  .** The formula 
2f5dd 61 62 6f 76 65 20 69 73 20 66 6f 72 20 63 6f 6d  above is for com
2f5de 70 75 74 69 6e 67 20 74 68 65 20 61 63 74 69 6f  puting the actio
2f5df 6e 20 77 68 65 6e 20 74 68 65 20 6c 6f 6f 6b 61  n when the looka
2f5e0 68 65 61 64 20 69 73 0a 2a 2a 20 61 20 74 65 72  head is.** a ter
2f5e1 6d 69 6e 61 6c 20 73 79 6d 62 6f 6c 2e 20 20 49  minal symbol.  I
2f5e2 66 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61 64 20  f the lookahead 
2f5e3 69 73 20 61 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61  is a non-termina
2f5e4 6c 20 28 61 73 20 6f 63 63 75 72 73 20 61 66 74  l (as occurs aft
2f5e5 65 72 0a 2a 2a 20 61 20 72 65 64 75 63 65 20 61  er.** a reduce a
2f5e6 63 74 69 6f 6e 29 20 74 68 65 6e 20 74 68 65 20  ction) then the 
2f5e7 79 79 5f 72 65 64 75 63 65 5f 6f 66 73 74 5b 5d  yy_reduce_ofst[]
2f5e8 20 61 72 72 61 79 20 69 73 20 75 73 65 64 20 69   array is used i
2f5e9 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 74 68  n place of.** th
2f5ea 65 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b  e yy_shift_ofst[
2f5eb 5d 20 61 72 72 61 79 20 61 6e 64 20 59 59 5f 52  ] array and YY_R
2f5ec 45 44 55 43 45 5f 55 53 45 5f 44 46 4c 54 20 69  EDUCE_USE_DFLT i
2f5ed 73 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20  s used in place 
2f5ee 6f 66 0a 2a 2a 20 59 59 5f 53 48 49 46 54 5f 55  of.** YY_SHIFT_U
2f5ef 53 45 5f 44 46 4c 54 2e 0a 2a 2a 0a 2a 2a 20 54  SE_DFLT..**.** T
2f5f0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65  he following are
2f5f1 20 74 68 65 20 74 61 62 6c 65 73 20 67 65 6e 65   the tables gene
2f5f2 72 61 74 65 64 20 69 6e 20 74 68 69 73 20 73 65  rated in this se
2f5f3 63 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 79 79  ction:.**.**  yy
2f5f4 5f 61 63 74 69 6f 6e 5b 5d 20 20 20 20 20 20 20  _action[]       
2f5f5 20 41 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20   A single table 
2f5f6 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 61  containing all a
2f5f7 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20 79 79 5f 6c  ctions..**  yy_l
2f5f8 6f 6f 6b 61 68 65 61 64 5b 5d 20 20 20 20 20 41  ookahead[]     A
2f5f9 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e   table containin
2f5fa 67 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61 64 20  g the lookahead 
2f5fb 66 6f 72 20 65 61 63 68 20 65 6e 74 72 79 20 69  for each entry i
2f5fc 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  n.**            
2f5fd 20 20 20 20 20 20 20 20 20 79 79 5f 61 63 74 69           yy_acti
2f5fe 6f 6e 2e 20 20 55 73 65 64 20 74 6f 20 64 65 74  on.  Used to det
2f5ff 65 63 74 20 68 61 73 68 20 63 6f 6c 6c 69 73 69  ect hash collisi
2f600 6f 6e 73 2e 0a 2a 2a 20 20 79 79 5f 73 68 69 66  ons..**  yy_shif
2f601 74 5f 6f 66 73 74 5b 5d 20 20 20 20 46 6f 72 20  t_ofst[]    For 
2f602 65 61 63 68 20 73 74 61 74 65 2c 20 74 68 65 20  each state, the 
2f603 6f 66 66 73 65 74 20 69 6e 74 6f 20 79 79 5f 61  offset into yy_a
2f604 63 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 20 20 20  ction for.**    
2f605 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f606 20 73 68 69 66 74 69 6e 67 20 74 65 72 6d 69 6e   shifting termin
2f607 61 6c 73 2e 0a 2a 2a 20 20 79 79 5f 72 65 64 75  als..**  yy_redu
2f608 63 65 5f 6f 66 73 74 5b 5d 20 20 20 46 6f 72 20  ce_ofst[]   For 
2f609 65 61 63 68 20 73 74 61 74 65 2c 20 74 68 65 20  each state, the 
2f60a 6f 66 66 73 65 74 20 69 6e 74 6f 20 79 79 5f 61  offset into yy_a
2f60b 63 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 20 20 20  ction for.**    
2f60c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f60d 20 73 68 69 66 74 69 6e 67 20 6e 6f 6e 2d 74 65   shifting non-te
2f60e 72 6d 69 6e 61 6c 73 20 61 66 74 65 72 20 61 20  rminals after a 
2f60f 72 65 64 75 63 65 2e 0a 2a 2a 20 20 79 79 5f 64  reduce..**  yy_d
2f610 65 66 61 75 6c 74 5b 5d 20 20 20 20 20 20 20 44  efault[]       D
2f611 65 66 61 75 6c 74 20 61 63 74 69 6f 6e 20 66 6f  efault action fo
2f612 72 20 65 61 63 68 20 73 74 61 74 65 2e 0a 2a 2f  r each state..*/
2f613 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 59 59  .static const YY
2f614 41 43 54 49 4f 4e 54 59 50 45 20 79 79 5f 61 63  ACTIONTYPE yy_ac
2f615 74 69 6f 6e 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20  tion[] = {. /*  
2f616 20 20 20 30 20 2a 2f 20 20 20 33 30 39 2c 20 20     0 */   309,  
2f617 39 35 39 2c 20 20 31 37 38 2c 20 20 36 32 38 2c  959,  178,  628,
2f618 20 20 20 20 32 2c 20 20 31 35 33 2c 20 20 32 31      2,  153,  21
2f619 36 2c 20 20 34 34 38 2c 20 20 20 32 34 2c 20 20  6,  448,   24,  
2f61a 20 32 34 2c 0a 20 2f 2a 20 20 20 20 31 30 20 2a   24,. /*    10 *
2f61b 2f 20 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20  /    24,   24,  
2f61c 34 39 37 2c 20 20 20 32 36 2c 20 20 20 32 36 2c  497,   26,   26,
2f61d 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
2f61e 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c 0a 20  7,   27,   28,. 
2f61f 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20 20 20 32  /*    20 */    2
2f620 38 2c 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20  8,   28,   29,  
2f621 32 31 38 2c 20 20 34 32 32 2c 20 20 34 32 33 2c  218,  422,  423,
2f622 20 20 32 31 34 2c 20 20 34 32 32 2c 20 20 34 32    214,  422,  42
2f623 33 2c 20 20 34 35 35 2c 0a 20 2f 2a 20 20 20 20  3,  455,. /*    
2f624 33 30 20 2a 2f 20 20 20 34 36 31 2c 20 20 20 33  30 */   461,   3
2f625 31 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  1,   26,   26,  
2f626 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c   26,   26,   27,
2f627 20 20 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32     27,   28,   2
2f628 38 2c 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f 20  8,. /*    40 */ 
2f629 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20 32 31     28,   29,  21
2f62a 38 2c 20 20 20 33 30 2c 20 20 34 39 32 2c 20 20  8,   30,  492,  
2f62b 20 33 32 2c 20 20 31 33 37 2c 20 20 20 32 33 2c   32,  137,   23,
2f62c 20 20 20 32 32 2c 20 20 33 31 35 2c 0a 20 2f 2a     22,  315,. /*
2f62d 20 20 20 20 35 30 20 2a 2f 20 20 20 34 36 35 2c      50 */   465,
2f62e 20 20 34 36 36 2c 20 20 34 36 32 2c 20 20 34 36    466,  462,  46
2f62f 32 2c 20 20 20 32 35 2c 20 20 20 32 35 2c 20 20  2,   25,   25,  
2f630 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c   24,   24,   24,
2f631 20 20 20 32 34 2c 0a 20 2f 2a 20 20 20 20 36 30     24,. /*    60
2f632 20 2a 2f 20 20 20 34 34 35 2c 20 20 20 32 36 2c   */   445,   26,
2f633 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
2f634 36 2c 20 20 20 32 37 2c 20 20 20 32 37 2c 20 20  6,   27,   27,  
2f635 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 38 2c   28,   28,   28,
2f636 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f 20 20 20  . /*    70 */   
2f637 20 32 39 2c 20 20 32 31 38 2c 20 20 33 30 39 2c   29,  218,  309,
2f638 20 20 32 31 38 2c 20 20 33 31 38 2c 20 20 34 34    218,  318,  44
2f639 38 2c 20 20 35 32 31 2c 20 20 34 39 39 2c 20 20  8,  521,  499,  
2f63a 20 34 35 2c 20 20 20 32 36 2c 0a 20 2f 2a 20 20   45,   26,. /*  
2f63b 20 20 38 30 20 2a 2f 20 20 20 20 32 36 2c 20 20    80 */    26,  
2f63c 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c   26,   26,   27,
2f63d 20 20 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32     27,   28,   2
2f63e 38 2c 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20  8,   28,   29,  
2f63f 32 31 38 2c 0a 20 2f 2a 20 20 20 20 39 30 20 2a  218,. /*    90 *
2f640 2f 20 20 20 34 32 32 2c 20 20 34 32 33 2c 20 20  /   422,  423,  
2f641 34 32 35 2c 20 20 34 32 36 2c 20 20 31 35 39 2c  425,  426,  159,
2f642 20 20 34 32 35 2c 20 20 34 32 36 2c 20 20 33 36    425,  426,  36
2f643 36 2c 20 20 33 36 39 2c 20 20 33 37 30 2c 0a 20  6,  369,  370,. 
2f644 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20 20 33 31  /*   100 */   31
2f645 38 2c 20 20 34 35 35 2c 20 20 34 36 31 2c 20 20  8,  455,  461,  
2f646 33 39 34 2c 20 20 35 32 33 2c 20 20 20 32 31 2c  394,  523,   21,
2f647 20 20 31 38 38 2c 20 20 35 30 34 2c 20 20 33 37    188,  504,  37
2f648 31 2c 20 20 20 32 37 2c 0a 20 2f 2a 20 20 20 31  1,   27,. /*   1
2f649 31 30 20 2a 2f 20 20 20 20 32 37 2c 20 20 20 32  10 */    27,   2
2f64a 38 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20  8,   28,   28,  
2f64b 20 32 39 2c 20 20 32 31 38 2c 20 20 34 32 32 2c   29,  218,  422,
2f64c 20 20 34 32 33 2c 20 20 34 32 34 2c 20 20 20 32    423,  424,   2
2f64d 33 2c 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f 20  3,. /*   120 */ 
2f64e 20 20 20 32 32 2c 20 20 33 31 35 2c 20 20 34 36     22,  315,  46
2f64f 35 2c 20 20 34 36 36 2c 20 20 34 36 32 2c 20 20  5,  466,  462,  
2f650 34 36 32 2c 20 20 20 32 35 2c 20 20 20 32 35 2c  462,   25,   25,
2f651 20 20 20 32 34 2c 20 20 20 32 34 2c 0a 20 2f 2a     24,   24,. /*
2f652 20 20 20 31 33 30 20 2a 2f 20 20 20 20 32 34 2c     130 */    24,
2f653 20 20 20 32 34 2c 20 20 35 36 34 2c 20 20 20 32     24,  564,   2
2f654 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  6,   26,   26,  
2f655 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32 37 2c   26,   27,   27,
2f656 20 20 20 32 38 2c 0a 20 2f 2a 20 20 20 31 34 30     28,. /*   140
2f657 20 2a 2f 20 20 20 20 32 38 2c 20 20 20 32 38 2c   */    28,   28,
2f658 20 20 20 32 39 2c 20 20 32 31 38 2c 20 20 33 30     29,  218,  30
2f659 39 2c 20 20 32 33 30 2c 20 20 35 31 33 2c 20 20  9,  230,  513,  
2f65a 31 33 38 2c 20 20 34 37 37 2c 20 20 32 32 30 2c  138,  477,  220,
2f65b 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f 20 20 20  . /*   150 */   
2f65c 35 35 37 2c 20 20 31 34 38 2c 20 20 31 33 35 2c  557,  148,  135,
2f65d 20 20 32 36 30 2c 20 20 33 36 34 2c 20 20 32 36    260,  364,  26
2f65e 35 2c 20 20 33 36 35 2c 20 20 31 35 36 2c 20 20  5,  365,  156,  
2f65f 34 32 35 2c 20 20 34 32 36 2c 0a 20 2f 2a 20 20  425,  426,. /*  
2f660 20 31 36 30 20 2a 2f 20 20 20 32 34 35 2c 20 20   160 */   245,  
2f661 36 31 30 2c 20 20 33 33 37 2c 20 20 20 33 30 2c  610,  337,   30,
2f662 20 20 32 36 39 2c 20 20 20 33 32 2c 20 20 31 33    269,   32,  13
2f663 37 2c 20 20 34 34 38 2c 20 20 36 30 38 2c 20 20  7,  448,  608,  
2f664 36 30 39 2c 0a 20 2f 2a 20 20 20 31 37 30 20 2a  609,. /*   170 *
2f665 2f 20 20 20 32 33 33 2c 20 20 32 33 30 2c 20 20  /   233,  230,  
2f666 34 39 39 2c 20 20 34 35 35 2c 20 20 34 36 31 2c  499,  455,  461,
2f667 20 20 20 35 37 2c 20 20 35 31 35 2c 20 20 33 33     57,  515,  33
2f668 34 2c 20 20 31 33 35 2c 20 20 32 36 30 2c 0a 20  4,  135,  260,. 
2f669 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20 20 33 36  /*   180 */   36
2f66a 34 2c 20 20 32 36 35 2c 20 20 33 36 35 2c 20 20  4,  265,  365,  
2f66b 31 35 36 2c 20 20 34 32 35 2c 20 20 34 32 36 2c  156,  425,  426,
2f66c 20 20 34 34 34 2c 20 20 20 37 38 2c 20 20 34 31    444,   78,  41
2f66d 37 2c 20 20 34 31 34 2c 0a 20 2f 2a 20 20 20 31  7,  414,. /*   1
2f66e 39 30 20 2a 2f 20 20 20 32 36 39 2c 20 20 20 32  90 */   269,   2
2f66f 33 2c 20 20 20 32 32 2c 20 20 33 31 35 2c 20 20  3,   22,  315,  
2f670 34 36 35 2c 20 20 34 36 36 2c 20 20 34 36 32 2c  465,  466,  462,
2f671 20 20 34 36 32 2c 20 20 20 32 35 2c 20 20 20 32    462,   25,   2
2f672 35 2c 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f 20  5,. /*   200 */ 
2f673 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32     24,   24,   2
2f674 34 2c 20 20 20 32 34 2c 20 20 33 34 38 2c 20 20  4,   24,  348,  
2f675 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c   26,   26,   26,
2f676 20 20 20 32 36 2c 20 20 20 32 37 2c 0a 20 2f 2a     26,   27,. /*
2f677 20 20 20 32 31 30 20 2a 2f 20 20 20 20 32 37 2c     210 */    27,
2f678 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32     28,   28,   2
2f679 38 2c 20 20 20 32 39 2c 20 20 32 31 38 2c 20 20  8,   29,  218,  
2f67a 33 30 39 2c 20 20 32 31 36 2c 20 20 35 34 33 2c  309,  216,  543,
2f67b 20 20 35 35 36 2c 0a 20 2f 2a 20 20 20 32 32 30    556,. /*   220
2f67c 20 2a 2f 20 20 20 34 38 36 2c 20 20 31 33 30 2c   */   486,  130,
2f67d 20 20 34 39 38 2c 20 20 36 30 37 2c 20 20 20 33    498,  607,   3
2f67e 30 2c 20 20 33 33 37 2c 20 20 20 33 32 2c 20 20  0,  337,   32,  
2f67f 31 33 37 2c 20 20 33 35 31 2c 20 20 33 39 36 2c  137,  351,  396,
2f680 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f 20 20 20  . /*   230 */   
2f681 34 33 38 2c 20 20 20 36 33 2c 20 20 33 33 37 2c  438,   63,  337,
2f682 20 20 33 36 31 2c 20 20 34 32 34 2c 20 20 34 34    361,  424,  44
2f683 38 2c 20 20 34 38 37 2c 20 20 33 33 37 2c 20 20  8,  487,  337,  
2f684 34 32 34 2c 20 20 35 34 34 2c 0a 20 2f 2a 20 20  424,  544,. /*  
2f685 20 32 34 30 20 2a 2f 20 20 20 33 33 34 2c 20 20   240 */   334,  
2f686 32 31 37 2c 20 20 31 39 35 2c 20 20 36 30 36 2c  217,  195,  606,
2f687 20 20 36 30 35 2c 20 20 34 35 35 2c 20 20 34 36    605,  455,  46
2f688 31 2c 20 20 33 33 34 2c 20 20 20 31 38 2c 20 20  1,  334,   18,  
2f689 34 34 34 2c 0a 20 2f 2a 20 20 20 32 35 30 20 2a  444,. /*   250 *
2f68a 2f 20 20 20 20 38 35 2c 20 20 34 38 38 2c 20 20  /    85,  488,  
2f68b 33 33 34 2c 20 20 33 34 37 2c 20 20 31 39 32 2c  334,  347,  192,
2f68c 20 20 35 36 35 2c 20 20 34 34 34 2c 20 20 20 37    565,  444,   7
2f68d 38 2c 20 20 33 31 36 2c 20 20 34 37 32 2c 0a 20  8,  316,  472,. 
2f68e 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20 20 34 37  /*   260 */   47
2f68f 33 2c 20 20 34 34 34 2c 20 20 20 38 35 2c 20 20  3,  444,   85,  
2f690 20 32 33 2c 20 20 20 32 32 2c 20 20 33 31 35 2c   23,   22,  315,
2f691 20 20 34 36 35 2c 20 20 34 36 36 2c 20 20 34 36    465,  466,  46
2f692 32 2c 20 20 34 36 32 2c 0a 20 2f 2a 20 20 20 32  2,  462,. /*   2
2f693 37 30 20 2a 2f 20 20 20 20 32 35 2c 20 20 20 32  70 */    25,   2
2f694 35 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20  5,   24,   24,  
2f695 20 32 34 2c 20 20 20 32 34 2c 20 20 34 34 35 2c   24,   24,  445,
2f696 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
2f697 36 2c 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f 20  6,. /*   280 */ 
2f698 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32     26,   27,   2
2f699 37 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20  7,   28,   28,  
2f69a 20 32 38 2c 20 20 20 32 39 2c 20 20 32 31 38 2c   28,   29,  218,
2f69b 20 20 33 30 39 2c 20 20 33 35 33 2c 0a 20 2f 2a    309,  353,. /*
2f69c 20 20 20 32 39 30 20 2a 2f 20 20 20 32 32 33 2c     290 */   223,
2f69d 20 20 33 32 30 2c 20 20 36 30 37 2c 20 20 31 39    320,  607,  19
2f69e 33 2c 20 20 32 33 38 2c 20 20 33 33 37 2c 20 20  3,  238,  337,  
2f69f 34 38 31 2c 20 20 20 31 36 2c 20 20 33 35 31 2c  481,   16,  351,
2f6a0 20 20 31 38 35 2c 0a 20 2f 2a 20 20 20 33 30 30    185,. /*   300
2f6a1 20 2a 2f 20 20 20 33 33 30 2c 20 20 34 31 39 2c   */   330,  419,
2f6a2 20 20 32 32 32 2c 20 20 33 35 30 2c 20 20 36 30    222,  350,  60
2f6a3 34 2c 20 20 32 31 39 2c 20 20 32 31 35 2c 20 20  4,  219,  215,  
2f6a4 34 32 34 2c 20 20 31 31 32 2c 20 20 33 33 37 2c  424,  112,  337,
2f6a5 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f 20 20 20  . /*   310 */   
2f6a6 33 33 34 2c 20 20 31 35 37 2c 20 20 36 30 36 2c  334,  157,  606,
2f6a7 20 20 34 30 38 2c 20 20 32 31 33 2c 20 20 35 36    408,  213,  56
2f6a8 33 2c 20 20 35 33 38 2c 20 20 34 35 35 2c 20 20  3,  538,  455,  
2f6a9 34 36 31 2c 20 20 34 34 34 2c 0a 20 2f 2a 20 20  461,  444,. /*  
2f6aa 20 33 32 30 20 2a 2f 20 20 20 20 37 39 2c 20 20   320 */    79,  
2f6ab 32 31 39 2c 20 20 35 36 32 2c 20 20 35 32 34 2c  219,  562,  524,
2f6ac 20 20 33 33 34 2c 20 20 35 37 36 2c 20 20 35 32    334,  576,  52
2f6ad 32 2c 20 20 36 32 39 2c 20 20 34 31 37 2c 20 20  2,  629,  417,  
2f6ae 34 31 34 2c 0a 20 2f 2a 20 20 20 33 33 30 20 2a  414,. /*   330 *
2f6af 2f 20 20 20 34 35 30 2c 20 20 35 38 31 2c 20 20  /   450,  581,  
2f6b0 34 34 31 2c 20 20 34 34 34 2c 20 20 20 37 38 2c  441,  444,   78,
2f6b1 20 20 20 32 33 2c 20 20 20 32 32 2c 20 20 33 31     23,   22,  31
2f6b2 35 2c 20 20 34 36 35 2c 20 20 34 36 36 2c 0a 20  5,  465,  466,. 
2f6b3 2f 2a 20 20 20 33 34 30 20 2a 2f 20 20 20 34 36  /*   340 */   46
2f6b4 32 2c 20 20 34 36 32 2c 20 20 20 32 35 2c 20 20  2,  462,   25,  
2f6b5 20 32 35 2c 20 20 20 32 34 2c 20 20 20 32 34 2c   25,   24,   24,
2f6b6 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 34 34     24,   24,  44
2f6b7 35 2c 20 20 20 32 36 2c 0a 20 2f 2a 20 20 20 33  5,   26,. /*   3
2f6b8 35 30 20 2a 2f 20 20 20 20 32 36 2c 20 20 20 32  50 */    26,   2
2f6b9 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20  6,   26,   27,  
2f6ba 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32 38 2c   27,   28,   28,
2f6bb 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20 32 31     28,   29,  21
2f6bc 38 2c 0a 20 2f 2a 20 20 20 33 36 30 20 2a 2f 20  8,. /*   360 */ 
2f6bd 20 20 33 30 39 2c 20 20 34 35 32 2c 20 20 34 35    309,  452,  45
2f6be 32 2c 20 20 34 35 32 2c 20 20 31 35 39 2c 20 20  2,  452,  159,  
2f6bf 33 39 39 2c 20 20 33 31 31 2c 20 20 33 36 36 2c  399,  311,  366,
2f6c0 20 20 33 36 39 2c 20 20 33 37 30 2c 0a 20 2f 2a    369,  370,. /*
2f6c1 20 20 20 33 37 30 20 2a 2f 20 20 20 33 33 37 2c     370 */   337,
2f6c2 20 20 32 35 31 2c 20 20 34 30 34 2c 20 20 34 30    251,  404,  40
2f6c3 37 2c 20 20 32 31 39 2c 20 20 33 35 35 2c 20 20  7,  219,  355,  
2f6c4 35 35 36 2c 20 20 20 20 34 2c 20 20 33 37 31 2c  556,    4,  371,
2f6c5 20 20 34 32 32 2c 0a 20 2f 2a 20 20 20 33 38 30    422,. /*   380
2f6c6 20 2a 2f 20 20 20 34 32 33 2c 20 20 33 39 37 2c   */   423,  397,
2f6c7 20 20 32 38 36 2c 20 20 32 38 35 2c 20 20 32 34    286,  285,  24
2f6c8 34 2c 20 20 33 33 34 2c 20 20 35 34 30 2c 20 20  4,  334,  540,  
2f6c9 35 36 36 2c 20 20 20 36 33 2c 20 20 34 35 35 2c  566,   63,  455,
2f6ca 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f 20 20 20  . /*   390 */   
2f6cb 34 36 31 2c 20 20 34 32 34 2c 20 20 32 31 36 2c  461,  424,  216,
2f6cc 20 20 34 37 38 2c 20 20 34 34 34 2c 20 20 20 39    478,  444,   9
2f6cd 33 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20  3,   28,   28,  
2f6ce 20 32 38 2c 20 20 20 32 39 2c 0a 20 2f 2a 20 20   28,   29,. /*  
2f6cf 20 34 30 30 20 2a 2f 20 20 20 32 31 38 2c 20 20   400 */   218,  
2f6d0 34 31 33 2c 20 20 34 37 37 2c 20 20 32 32 30 2c  413,  477,  220,
2f6d1 20 20 35 37 38 2c 20 20 20 34 30 2c 20 20 35 34    578,   40,  54
2f6d2 35 2c 20 20 20 32 33 2c 20 20 20 32 32 2c 20 20  5,   23,   22,  
2f6d3 33 31 35 2c 0a 20 2f 2a 20 20 20 34 31 30 20 2a  315,. /*   410 *
2f6d4 2f 20 20 20 34 36 35 2c 20 20 34 36 36 2c 20 20  /   465,  466,  
2f6d5 34 36 32 2c 20 20 34 36 32 2c 20 20 20 32 35 2c  462,  462,   25,
2f6d6 20 20 20 32 35 2c 20 20 20 32 34 2c 20 20 20 32     25,   24,   2
2f6d7 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 0a 20  4,   24,   24,. 
2f6d8 2f 2a 20 20 20 34 32 30 20 2a 2f 20 20 20 35 38  /*   420 */   58
2f6d9 32 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  2,   26,   26,  
2f6da 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c   26,   26,   27,
2f6db 20 20 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32     27,   28,   2
2f6dc 38 2c 20 20 20 32 38 2c 0a 20 2f 2a 20 20 20 34  8,   28,. /*   4
2f6dd 33 30 20 2a 2f 20 20 20 20 32 39 2c 20 20 32 31  30 */    29,  21
2f6de 38 2c 20 20 33 30 39 2c 20 20 35 34 36 2c 20 20  8,  309,  546,  
2f6df 33 33 37 2c 20 20 20 33 30 2c 20 20 35 31 37 2c  337,   30,  517,
2f6e0 20 20 20 33 32 2c 20 20 31 33 37 2c 20 20 33 37     32,  137,  37
2f6e1 38 2c 0a 20 2f 2a 20 20 20 34 34 30 20 2a 2f 20  8,. /*   440 */ 
2f6e2 20 20 33 32 36 2c 20 20 33 33 37 2c 20 20 38 37    326,  337,  87
2f6e3 34 2c 20 20 31 35 33 2c 20 20 31 39 34 2c 20 20  4,  153,  194,  
2f6e4 34 34 38 2c 20 20 20 20 31 2c 20 20 34 32 35 2c  448,    1,  425,
2f6e5 20 20 34 32 36 2c 20 20 33 33 34 2c 0a 20 2f 2a    426,  334,. /*
2f6e6 20 20 20 34 35 30 20 2a 2f 20 20 20 34 32 32 2c     450 */   422,
2f6e7 20 20 34 32 33 2c 20 20 34 32 32 2c 20 20 34 32    423,  422,  42
2f6e8 33 2c 20 20 20 32 39 2c 20 20 32 31 38 2c 20 20  3,   29,  218,  
2f6e9 33 33 34 2c 20 20 36 31 33 2c 20 20 34 34 34 2c  334,  613,  444,
2f6ea 20 20 20 37 31 2c 0a 20 2f 2a 20 20 20 34 36 30     71,. /*   460
2f6eb 20 2a 2f 20 20 20 32 31 30 2c 20 20 34 35 35 2c   */   210,  455,
2f6ec 20 20 34 36 31 2c 20 20 20 36 36 2c 20 20 35 38    461,   66,  58
2f6ed 31 2c 20 20 34 34 34 2c 20 20 20 39 33 2c 20 20  1,  444,   93,  
2f6ee 34 32 32 2c 20 20 34 32 33 2c 20 20 36 32 36 2c  422,  423,  626,
2f6ef 0a 20 2f 2a 20 20 20 34 37 30 20 2a 2f 20 20 20  . /*   470 */   
2f6f0 39 34 39 2c 20 20 33 30 33 2c 20 20 39 34 39 2c  949,  303,  949,
2f6f1 20 20 35 30 30 2c 20 20 34 37 39 2c 20 20 35 35    500,  479,  55
2f6f2 35 2c 20 20 32 30 32 2c 20 20 20 34 33 2c 20 20  5,  202,   43,  
2f6f3 34 34 35 2c 20 20 20 32 33 2c 0a 20 2f 2a 20 20  445,   23,. /*  
2f6f4 20 34 38 30 20 2a 2f 20 20 20 20 32 32 2c 20 20   480 */    22,  
2f6f5 33 31 35 2c 20 20 34 36 35 2c 20 20 34 36 36 2c  315,  465,  466,
2f6f6 20 20 34 36 32 2c 20 20 34 36 32 2c 20 20 20 32    462,  462,   2
2f6f7 35 2c 20 20 20 32 35 2c 20 20 20 32 34 2c 20 20  5,   25,   24,  
2f6f8 20 32 34 2c 0a 20 2f 2a 20 20 20 34 39 30 20 2a   24,. /*   490 *
2f6f9 2f 20 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20  /    24,   24,  
2f6fa 34 33 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c  436,   26,   26,
2f6fb 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
2f6fc 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c 0a 20  7,   27,   28,. 
2f6fd 2f 2a 20 20 20 35 30 30 20 2a 2f 20 20 20 20 32  /*   500 */    2
2f6fe 38 2c 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20  8,   28,   29,  
2f6ff 32 31 38 2c 20 20 33 30 39 2c 20 20 31 38 37 2c  218,  309,  187,
2f700 20 20 32 31 31 2c 20 20 33 36 30 2c 20 20 35 32    211,  360,  52
2f701 30 2c 20 20 34 34 30 2c 0a 20 2f 2a 20 20 20 35  0,  440,. /*   5
2f702 31 30 20 2a 2f 20 20 20 32 34 36 2c 20 20 33 32  10 */   246,  32
2f703 37 2c 20 20 36 32 32 2c 20 20 34 34 38 2c 20 20  7,  622,  448,  
2f704 33 39 37 2c 20 20 32 38 36 2c 20 20 32 38 35 2c  397,  286,  285,
2f705 20 20 35 35 31 2c 20 20 34 32 35 2c 20 20 34 32    551,  425,  42
2f706 36 2c 0a 20 2f 2a 20 20 20 35 32 30 20 2a 2f 20  6,. /*   520 */ 
2f707 20 20 34 32 35 2c 20 20 34 32 36 2c 20 20 33 33    425,  426,  33
2f708 34 2c 20 20 31 35 39 2c 20 20 33 33 37 2c 20 20  4,  159,  337,  
2f709 32 31 36 2c 20 20 33 36 36 2c 20 20 33 36 39 2c  216,  366,  369,
2f70a 20 20 33 37 30 2c 20 20 34 39 34 2c 0a 20 2f 2a    370,  494,. /*
2f70b 20 20 20 35 33 30 20 2a 2f 20 20 20 35 35 36 2c     530 */   556,
2f70c 20 20 34 34 34 2c 20 20 20 20 39 2c 20 20 34 35    444,    9,  45
2f70d 35 2c 20 20 34 36 31 2c 20 20 34 32 35 2c 20 20  5,  461,  425,  
2f70e 34 32 36 2c 20 20 33 37 31 2c 20 20 34 39 35 2c  426,  371,  495,
2f70f 20 20 33 33 34 2c 0a 20 2f 2a 20 20 20 35 34 30    334,. /*   540
2f710 20 2a 2f 20 20 20 34 34 35 2c 20 20 36 31 38 2c   */   445,  618,
2f711 20 20 20 36 33 2c 20 20 35 30 34 2c 20 20 31 39     63,  504,  19
2f712 38 2c 20 20 34 32 34 2c 20 20 35 30 31 2c 20 20  8,  424,  501,  
2f713 34 34 39 2c 20 20 34 34 34 2c 20 20 20 37 32 2c  449,  444,   72,
2f714 0a 20 2f 2a 20 20 20 35 35 30 20 2a 2f 20 20 20  . /*   550 */   
2f715 34 37 34 2c 20 20 20 32 33 2c 20 20 20 32 32 2c  474,   23,   22,
2f716 20 20 33 31 35 2c 20 20 34 36 35 2c 20 20 34 36    315,  465,  46
2f717 36 2c 20 20 34 36 32 2c 20 20 34 36 32 2c 20 20  6,  462,  462,  
2f718 20 32 35 2c 20 20 20 32 35 2c 0a 20 2f 2a 20 20   25,   25,. /*  
2f719 20 35 36 30 20 2a 2f 20 20 20 20 32 34 2c 20 20   560 */    24,  
2f71a 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c   24,   24,   24,
2f71b 20 20 33 39 35 2c 20 20 20 32 36 2c 20 20 20 32    395,   26,   2
2f71c 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  6,   26,   26,  
2f71d 20 32 37 2c 0a 20 2f 2a 20 20 20 35 37 30 20 2a   27,. /*   570 *
2f71e 2f 20 20 20 20 32 37 2c 20 20 20 32 38 2c 20 20  /    27,   28,  
2f71f 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 39 2c   28,   28,   29,
2f720 20 20 32 31 38 2c 20 20 33 30 39 2c 20 20 34 38    218,  309,  48
2f721 36 2c 20 20 34 34 35 2c 20 20 33 33 37 2c 0a 20  6,  445,  337,. 
2f722 2f 2a 20 20 20 35 38 30 20 2a 2f 20 20 20 35 33  /*   580 */   53
2f723 37 2c 20 20 20 36 30 2c 20 20 32 32 34 2c 20 20  7,   60,  224,  
2f724 34 37 39 2c 20 20 33 34 33 2c 20 20 32 30 32 2c  479,  343,  202,
2f725 20 20 33 39 38 2c 20 20 33 33 37 2c 20 20 34 33    398,  337,  43
2f726 39 2c 20 20 35 35 34 2c 0a 20 2f 2a 20 20 20 35  9,  554,. /*   5
2f727 39 30 20 2a 2f 20 20 20 31 39 39 2c 20 20 31 34  90 */   199,  14
2f728 30 2c 20 20 33 33 37 2c 20 20 34 38 37 2c 20 20  0,  337,  487,  
2f729 33 33 34 2c 20 20 35 32 36 2c 20 20 35 32 37 2c  334,  526,  527,
2f72a 20 20 35 35 31 2c 20 20 35 31 36 2c 20 20 35 30    551,  516,  50
2f72b 38 2c 0a 20 2f 2a 20 20 20 36 30 30 20 2a 2f 20  8,. /*   600 */ 
2f72c 20 20 34 35 36 2c 20 20 34 35 37 2c 20 20 33 33    456,  457,  33
2f72d 34 2c 20 20 34 34 34 2c 20 20 20 36 37 2c 20 20  4,  444,   67,  
2f72e 34 35 35 2c 20 20 34 36 31 2c 20 20 33 33 34 2c  455,  461,  334,
2f72f 20 20 34 38 38 2c 20 20 34 37 36 2c 0a 20 2f 2a    488,  476,. /*
2f730 20 20 20 36 31 30 20 2a 2f 20 20 20 35 32 38 2c     610 */   528,
2f731 20 20 34 34 34 2c 20 20 20 37 36 2c 20 20 20 33    444,   76,   3
2f732 39 2c 20 20 34 32 34 2c 20 20 20 34 31 2c 20 20  9,  424,   41,  
2f733 34 34 34 2c 20 20 20 39 37 2c 20 20 35 37 39 2c  444,   97,  579,
2f734 20 20 35 32 37 2c 0a 20 2f 2a 20 20 20 36 32 30    527,. /*   620
2f735 20 2a 2f 20 20 20 35 32 39 2c 20 20 34 35 39 2c   */   529,  459,
2f736 20 20 34 36 30 2c 20 20 20 32 33 2c 20 20 20 32    460,   23,   2
2f737 32 2c 20 20 33 31 35 2c 20 20 34 36 35 2c 20 20  2,  315,  465,  
2f738 34 36 36 2c 20 20 34 36 32 2c 20 20 34 36 32 2c  466,  462,  462,
2f739 0a 20 2f 2a 20 20 20 36 33 30 20 2a 2f 20 20 20  . /*   630 */   
2f73a 20 32 35 2c 20 20 20 32 35 2c 20 20 20 32 34 2c   25,   25,   24,
2f73b 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32     24,   24,   2
2f73c 34 2c 20 20 33 33 37 2c 20 20 20 32 36 2c 20 20  4,  337,   26,  
2f73d 20 32 36 2c 20 20 20 32 36 2c 0a 20 2f 2a 20 20   26,   26,. /*  
2f73e 20 36 34 30 20 2a 2f 20 20 20 20 32 36 2c 20 20   640 */    26,  
2f73f 20 32 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c   27,   27,   28,
2f740 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32     28,   28,   2
2f741 39 2c 20 20 32 31 38 2c 20 20 33 30 39 2c 20 20  9,  218,  309,  
2f742 33 33 37 2c 0a 20 2f 2a 20 20 20 36 35 30 20 2a  337,. /*   650 *
2f743 2f 20 20 20 34 35 38 2c 20 20 33 33 34 2c 20 20  /   458,  334,  
2f744 32 37 32 2c 20 20 36 32 31 2c 20 20 33 30 37 2c  272,  621,  307,
2f745 20 20 33 33 37 2c 20 20 33 31 32 2c 20 20 33 33    337,  312,  33
2f746 37 2c 20 20 33 37 34 2c 20 20 20 36 34 2c 0a 20  7,  374,   64,. 
2f747 2f 2a 20 20 20 36 36 30 20 2a 2f 20 20 20 34 34  /*   660 */   44
2f748 34 2c 20 20 20 39 36 2c 20 20 33 31 37 2c 20 20  4,   96,  317,  
2f749 34 34 38 2c 20 20 33 33 34 2c 20 20 33 34 32 2c  448,  334,  342,
2f74a 20 20 34 37 32 2c 20 20 34 37 33 2c 20 20 34 36    472,  473,  46
2f74b 39 2c 20 20 33 33 37 2c 0a 20 2f 2a 20 20 20 36  9,  337,. /*   6
2f74c 37 30 20 2a 2f 20 20 20 33 33 34 2c 20 20 35 30  70 */   334,  50
2f74d 38 2c 20 20 33 33 34 2c 20 20 34 34 34 2c 20 20  8,  334,  444,  
2f74e 31 30 31 2c 20 20 33 35 39 2c 20 20 32 35 32 2c  101,  359,  252,
2f74f 20 20 34 35 35 2c 20 20 34 36 31 2c 20 20 34 34    455,  461,  44
2f750 34 2c 0a 20 2f 2a 20 20 20 36 38 30 20 2a 2f 20  4,. /*   680 */ 
2f751 20 20 20 39 39 2c 20 20 34 34 34 2c 20 20 31 30     99,  444,  10
2f752 34 2c 20 20 33 35 38 2c 20 20 33 33 34 2c 20 20  4,  358,  334,  
2f753 33 34 35 2c 20 20 34 32 34 2c 20 20 33 34 30 2c  345,  424,  340,
2f754 20 20 31 35 37 2c 20 20 34 36 38 2c 0a 20 2f 2a    157,  468,. /*
2f755 20 20 20 36 39 30 20 2a 2f 20 20 20 34 36 38 2c     690 */   468,
2f756 20 20 34 32 34 2c 20 20 34 39 33 2c 20 20 34 34    424,  493,  44
2f757 34 2c 20 20 31 30 35 2c 20 20 20 32 33 2c 20 20  4,  105,   23,  
2f758 20 32 32 2c 20 20 33 31 35 2c 20 20 34 36 35 2c   22,  315,  465,
2f759 20 20 34 36 36 2c 0a 20 2f 2a 20 20 20 37 30 30    466,. /*   700
2f75a 20 2a 2f 20 20 20 34 36 32 2c 20 20 34 36 32 2c   */   462,  462,
2f75b 20 20 20 32 35 2c 20 20 20 32 35 2c 20 20 20 32     25,   25,   2
2f75c 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20  4,   24,   24,  
2f75d 20 32 34 2c 20 20 33 33 37 2c 20 20 20 32 36 2c   24,  337,   26,
2f75e 0a 20 2f 2a 20 20 20 37 31 30 20 2a 2f 20 20 20  . /*   710 */   
2f75f 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c   26,   26,   26,
2f760 20 20 20 32 37 2c 20 20 20 32 37 2c 20 20 20 32     27,   27,   2
2f761 38 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20  8,   28,   28,  
2f762 20 32 39 2c 20 20 32 31 38 2c 0a 20 2f 2a 20 20   29,  218,. /*  
2f763 20 37 32 30 20 2a 2f 20 20 20 33 30 39 2c 20 20   720 */   309,  
2f764 33 33 37 2c 20 20 31 38 31 2c 20 20 33 33 34 2c  337,  181,  334,
2f765 20 20 34 39 39 2c 20 20 20 35 36 2c 20 20 31 33    499,   56,  13
2f766 39 2c 20 20 33 33 37 2c 20 20 32 31 39 2c 20 20  9,  337,  219,  
2f767 32 36 38 2c 0a 20 2f 2a 20 20 20 37 33 30 20 2a  268,. /*   730 *
2f768 2f 20 20 20 33 38 34 2c 20 20 34 34 38 2c 20 20  /   384,  448,  
2f769 34 34 34 2c 20 20 31 32 39 2c 20 20 33 38 32 2c  444,  129,  382,
2f76a 20 20 33 38 37 2c 20 20 33 33 34 2c 20 20 31 36    387,  334,  16
2f76b 38 2c 20 20 33 33 37 2c 20 20 33 38 39 2c 0a 20  8,  337,  389,. 
2f76c 2f 2a 20 20 20 37 34 30 20 2a 2f 20 20 20 35 30  /*   740 */   50
2f76d 38 2c 20 20 34 32 34 2c 20 20 33 33 34 2c 20 20  8,  424,  334,  
2f76e 33 31 31 2c 20 20 34 32 34 2c 20 20 34 34 34 2c  311,  424,  444,
2f76f 20 20 31 33 31 2c 20 20 34 39 36 2c 20 20 32 36    131,  496,  26
2f770 39 2c 20 20 34 35 35 2c 0a 20 2f 2a 20 20 20 37  9,  455,. /*   7
2f771 35 30 20 2a 2f 20 20 20 34 36 31 2c 20 20 34 34  50 */   461,  44
2f772 34 2c 20 20 20 35 39 2c 20 20 33 33 34 2c 20 20  4,   59,  334,  
2f773 34 32 34 2c 20 20 34 32 34 2c 20 20 33 39 31 2c  424,  424,  391,
2f774 20 20 33 34 30 2c 20 20 20 20 38 2c 20 20 34 36    340,    8,  46
2f775 38 2c 0a 20 2f 2a 20 20 20 37 36 30 20 2a 2f 20  8,. /*   760 */ 
2f776 20 20 34 36 38 2c 20 20 32 36 33 2c 20 20 34 34    468,  263,  44
2f777 34 2c 20 20 31 30 32 2c 20 20 33 39 30 2c 20 20  4,  102,  390,  
2f778 32 39 30 2c 20 20 33 32 31 2c 20 20 20 32 33 2c  290,  321,   23,
2f779 20 20 20 32 32 2c 20 20 33 31 35 2c 0a 20 2f 2a     22,  315,. /*
2f77a 20 20 20 37 37 30 20 2a 2f 20 20 20 34 36 35 2c     770 */   465,
2f77b 20 20 34 36 36 2c 20 20 34 36 32 2c 20 20 34 36    466,  462,  46
2f77c 32 2c 20 20 20 32 35 2c 20 20 20 32 35 2c 20 20  2,   25,   25,  
2f77d 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c   24,   24,   24,
2f77e 20 20 20 32 34 2c 0a 20 2f 2a 20 20 20 37 38 30     24,. /*   780
2f77f 20 2a 2f 20 20 20 33 33 37 2c 20 20 20 32 36 2c   */   337,   26,
2f780 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
2f781 36 2c 20 20 20 32 37 2c 20 20 20 32 37 2c 20 20  6,   27,   27,  
2f782 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 38 2c   28,   28,   28,
2f783 0a 20 2f 2a 20 20 20 37 39 30 20 2a 2f 20 20 20  . /*   790 */   
2f784 20 32 39 2c 20 20 32 31 38 2c 20 20 33 30 39 2c   29,  218,  309,
2f785 20 20 33 33 37 2c 20 20 31 33 38 2c 20 20 33 33    337,  138,  33
2f786 34 2c 20 20 34 31 36 2c 20 20 20 20 32 2c 20 20  4,  416,    2,  
2f787 32 36 38 2c 20 20 33 33 37 2c 0a 20 2f 2a 20 20  268,  337,. /*  
2f788 20 38 30 30 20 2a 2f 20 20 20 33 38 39 2c 20 20   800 */   389,  
2f789 33 33 37 2c 20 20 34 34 33 2c 20 20 33 32 35 2c  337,  443,  325,
2f78a 20 20 34 34 34 2c 20 20 20 37 37 2c 20 20 34 34    444,   77,  44
2f78b 32 2c 20 20 32 39 33 2c 20 20 33 33 34 2c 20 20  2,  293,  334,  
2f78c 32 39 31 2c 0a 20 2f 2a 20 20 20 38 31 30 20 2a  291,. /*   810 *
2f78d 2f 20 20 20 20 20 37 2c 20 20 34 38 32 2c 20 20  /     7,  482,  
2f78e 33 33 37 2c 20 20 34 32 34 2c 20 20 33 33 34 2c  337,  424,  334,
2f78f 20 20 34 32 34 2c 20 20 33 33 34 2c 20 20 34 34    424,  334,  44
2f790 34 2c 20 20 31 30 30 2c 20 20 34 39 39 2c 0a 20  4,  100,  499,. 
2f791 2f 2a 20 20 20 38 32 30 20 2a 2f 20 20 20 33 33  /*   820 */   33
2f792 39 2c 20 20 34 35 35 2c 20 20 34 36 31 2c 20 20  9,  455,  461,  
2f793 34 34 34 2c 20 20 20 36 38 2c 20 20 34 34 34 2c  444,   68,  444,
2f794 20 20 20 39 38 2c 20 20 33 33 34 2c 20 20 32 35     98,  334,  25
2f795 34 2c 20 20 35 30 34 2c 0a 20 2f 2a 20 20 20 38  4,  504,. /*   8
2f796 33 30 20 2a 2f 20 20 20 32 33 32 2c 20 20 36 32  30 */   232,  62
2f797 36 2c 20 20 39 34 38 2c 20 20 35 30 34 2c 20 20  6,  948,  504,  
2f798 39 34 38 2c 20 20 32 33 31 2c 20 20 34 34 34 2c  948,  231,  444,
2f799 20 20 31 33 32 2c 20 20 20 34 37 2c 20 20 20 32    132,   47,   2
2f79a 33 2c 0a 20 2f 2a 20 20 20 38 34 30 20 2a 2f 20  3,. /*   840 */ 
2f79b 20 20 20 32 32 2c 20 20 33 31 35 2c 20 20 34 36     22,  315,  46
2f79c 35 2c 20 20 34 36 36 2c 20 20 34 36 32 2c 20 20  5,  466,  462,  
2f79d 34 36 32 2c 20 20 20 32 35 2c 20 20 20 32 35 2c  462,   25,   25,
2f79e 20 20 20 32 34 2c 20 20 20 32 34 2c 0a 20 2f 2a     24,   24,. /*
2f79f 20 20 20 38 35 30 20 2a 2f 20 20 20 20 32 34 2c     850 */    24,
2f7a0 20 20 20 32 34 2c 20 20 33 33 37 2c 20 20 20 32     24,  337,   2
2f7a1 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  6,   26,   26,  
2f7a2 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32 37 2c   26,   27,   27,
2f7a3 20 20 20 32 38 2c 0a 20 2f 2a 20 20 20 38 36 30     28,. /*   860
2f7a4 20 2a 2f 20 20 20 20 32 38 2c 20 20 20 32 38 2c   */    28,   28,
2f7a5 20 20 20 32 39 2c 20 20 32 31 38 2c 20 20 33 30     29,  218,  30
2f7a6 39 2c 20 20 33 33 37 2c 20 20 32 38 30 2c 20 20  9,  337,  280,  
2f7a7 33 33 34 2c 20 20 32 35 36 2c 20 20 35 33 38 2c  334,  256,  538,
2f7a8 0a 20 2f 2a 20 20 20 38 37 30 20 2a 2f 20 20 20  . /*   870 */   
2f7a9 33 36 32 2c 20 20 33 33 37 2c 20 20 32 35 38 2c  362,  337,  258,
2f7aa 20 20 32 36 38 2c 20 20 36 32 32 2c 20 20 35 34    268,  622,  54
2f7ab 39 2c 20 20 34 34 34 2c 20 20 31 33 33 2c 20 20  9,  444,  133,  
2f7ac 32 30 33 2c 20 20 31 34 30 2c 0a 20 2f 2a 20 20  203,  140,. /*  
2f7ad 20 38 38 30 20 2a 2f 20 20 20 33 33 34 2c 20 20   880 */   334,  
2f7ae 34 32 34 2c 20 20 35 34 38 2c 20 20 33 33 37 2c  424,  548,  337,
2f7af 20 20 31 38 30 2c 20 20 31 35 38 2c 20 20 33 33    180,  158,  33
2f7b0 34 2c 20 20 32 39 32 2c 20 20 34 32 34 2c 20 20  4,  292,  424,  
2f7b1 34 34 34 2c 0a 20 2f 2a 20 20 20 38 39 30 20 2a  444,. /*   890 *
2f7b2 2f 20 20 20 31 33 34 2c 20 20 32 38 37 2c 20 20  /   134,  287,  
2f7b3 35 35 32 2c 20 20 34 35 35 2c 20 20 34 36 31 2c  552,  455,  461,
2f7b4 20 20 34 34 34 2c 20 20 20 36 39 2c 20 20 34 34    444,   69,  44
2f7b5 33 2c 20 20 33 33 34 2c 20 20 34 36 33 2c 0a 20  3,  334,  463,. 
2f7b6 2f 2a 20 20 20 39 30 30 20 2a 2f 20 20 20 33 34  /*   900 */   34
2f7b7 30 2c 20 20 34 34 32 2c 20 20 34 36 38 2c 20 20  0,  442,  468,  
2f7b8 34 36 38 2c 20 20 34 32 37 2c 20 20 34 32 38 2c  468,  427,  428,
2f7b9 20 20 34 32 39 2c 20 20 34 34 34 2c 20 20 20 38    429,  444,   8
2f7ba 30 2c 20 20 32 38 31 2c 0a 20 2f 2a 20 20 20 39  0,  281,. /*   9
2f7bb 31 30 20 2a 2f 20 20 20 33 32 32 2c 20 20 20 32  10 */   322,   2
2f7bc 33 2c 20 20 20 33 33 2c 20 20 33 31 35 2c 20 20  3,   33,  315,  
2f7bd 34 36 35 2c 20 20 34 36 36 2c 20 20 34 36 32 2c  465,  466,  462,
2f7be 20 20 34 36 32 2c 20 20 20 32 35 2c 20 20 20 32    462,   25,   2
2f7bf 35 2c 0a 20 2f 2a 20 20 20 39 32 30 20 2a 2f 20  5,. /*   920 */ 
2f7c0 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32     24,   24,   2
2f7c1 34 2c 20 20 20 32 34 2c 20 20 33 33 37 2c 20 20  4,   24,  337,  
2f7c2 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c   26,   26,   26,
2f7c3 20 20 20 32 36 2c 20 20 20 32 37 2c 0a 20 2f 2a     26,   27,. /*
2f7c4 20 20 20 39 33 30 20 2a 2f 20 20 20 20 32 37 2c     930 */    27,
2f7c5 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32     28,   28,   2
2f7c6 38 2c 20 20 20 32 39 2c 20 20 32 31 38 2c 20 20  8,   29,  218,  
2f7c7 33 30 39 2c 20 20 33 33 37 2c 20 20 34 30 36 2c  309,  337,  406,
2f7c8 20 20 33 33 34 2c 0a 20 2f 2a 20 20 20 39 34 30    334,. /*   940
2f7c9 20 2a 2f 20 20 20 32 31 32 2c 20 20 32 36 38 2c   */   212,  268,
2f7ca 20 20 35 35 30 2c 20 20 33 33 37 2c 20 20 32 36    550,  337,  26
2f7cb 38 2c 20 20 33 38 39 2c 20 20 33 32 39 2c 20 20  8,  389,  329,  
2f7cc 31 37 37 2c 20 20 34 34 34 2c 20 20 20 38 31 2c  177,  444,   81,
2f7cd 0a 20 2f 2a 20 20 20 39 35 30 20 2a 2f 20 20 20  . /*   950 */   
2f7ce 35 34 32 2c 20 20 35 34 31 2c 20 20 33 33 34 2c  542,  541,  334,
2f7cf 20 20 34 37 35 2c 20 20 34 37 35 2c 20 20 33 33    475,  475,  33
2f7d0 37 2c 20 20 34 32 34 2c 20 20 32 31 36 2c 20 20  7,  424,  216,  
2f7d1 33 33 34 2c 20 20 34 32 34 2c 0a 20 2f 2a 20 20  334,  424,. /*  
2f7d2 20 39 36 30 20 2a 2f 20 20 20 34 32 34 2c 20 20   960 */   424,  
2f7d3 34 34 34 2c 20 20 20 37 30 2c 20 20 35 33 35 2c  444,   70,  535,
2f7d4 20 20 33 36 38 2c 20 20 34 35 35 2c 20 20 34 36    368,  455,  46
2f7d5 31 2c 20 20 34 34 34 2c 20 20 20 38 32 2c 20 20  1,  444,   82,  
2f7d6 34 30 35 2c 0a 20 2f 2a 20 20 20 39 37 30 20 2a  405,. /*   970 *
2f7d7 2f 20 20 20 33 33 34 2c 20 20 32 36 31 2c 20 20  /   334,  261,  
2f7d8 33 39 32 2c 20 20 33 34 30 2c 20 20 34 34 35 2c  392,  340,  445,
2f7d9 20 20 34 36 38 2c 20 20 34 36 38 2c 20 20 35 38    468,  468,  58
2f7da 37 2c 20 20 33 32 33 2c 20 20 34 34 34 2c 0a 20  7,  323,  444,. 
2f7db 2f 2a 20 20 20 39 38 30 20 2a 2f 20 20 20 20 38  /*   980 */    8
2f7dc 33 2c 20 20 33 32 34 2c 20 20 32 36 32 2c 20 20  3,  324,  262,  
2f7dd 32 38 38 2c 20 20 20 32 32 2c 20 20 33 31 35 2c  288,   22,  315,
2f7de 20 20 34 36 35 2c 20 20 34 36 36 2c 20 20 34 36    465,  466,  46
2f7df 32 2c 20 20 34 36 32 2c 0a 20 2f 2a 20 20 20 39  2,  462,. /*   9
2f7e0 39 30 20 2a 2f 20 20 20 20 32 35 2c 20 20 20 32  90 */    25,   2
2f7e1 35 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20  5,   24,   24,  
2f7e2 20 32 34 2c 20 20 20 32 34 2c 20 20 33 33 37 2c   24,   24,  337,
2f7e3 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
2f7e4 36 2c 0a 20 2f 2a 20 20 31 30 30 30 20 2a 2f 20  6,. /*  1000 */ 
2f7e5 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32     26,   27,   2
2f7e6 37 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20  7,   28,   28,  
2f7e7 20 32 38 2c 20 20 20 32 39 2c 20 20 32 31 38 2c   28,   29,  218,
2f7e8 20 20 33 30 39 2c 20 20 33 33 37 2c 0a 20 2f 2a    309,  337,. /*
2f7e9 20 20 31 30 31 30 20 2a 2f 20 20 20 32 31 31 2c    1010 */   211,
2f7ea 20 20 33 33 34 2c 20 20 32 39 34 2c 20 20 33 35    334,  294,  35
2f7eb 36 2c 20 20 33 34 30 2c 20 20 33 33 37 2c 20 20  6,  340,  337,  
2f7ec 34 36 38 2c 20 20 34 36 38 2c 20 20 35 33 32 2c  468,  468,  532,
2f7ed 20 20 35 33 33 2c 0a 20 2f 2a 20 20 31 30 32 30    533,. /*  1020
2f7ee 20 2a 2f 20 20 20 34 34 34 2c 20 20 20 38 34 2c   */   444,   84,
2f7ef 20 20 34 30 33 2c 20 20 31 34 34 2c 20 20 33 33    403,  144,  33
2f7f0 34 2c 20 20 35 37 34 2c 20 20 36 30 30 2c 20 20  4,  574,  600,  
2f7f1 33 33 37 2c 20 20 34 32 34 2c 20 20 35 37 33 2c  337,  424,  573,
2f7f2 0a 20 2f 2a 20 20 31 30 33 30 20 2a 2f 20 20 20  . /*  1030 */   
2f7f3 33 33 34 2c 20 20 33 33 37 2c 20 20 34 32 30 2c  334,  337,  420,
2f7f4 20 20 34 34 34 2c 20 20 20 38 36 2c 20 20 32 35    444,   86,  25
2f7f5 33 2c 20 20 32 33 34 2c 20 20 34 35 35 2c 20 20  3,  234,  455,  
2f7f6 34 36 31 2c 20 20 34 34 34 2c 0a 20 2f 2a 20 20  461,  444,. /*  
2f7f7 31 30 34 30 20 2a 2f 20 20 20 20 38 37 2c 20 20  1040 */    87,  
2f7f8 34 33 30 2c 20 20 33 33 34 2c 20 20 33 38 33 2c  430,  334,  383,
2f7f9 20 20 34 34 35 2c 20 20 34 33 31 2c 20 20 33 33    445,  431,  33
2f7fa 34 2c 20 20 32 37 34 2c 20 20 31 39 36 2c 20 20  4,  274,  196,  
2f7fb 33 33 31 2c 0a 20 2f 2a 20 20 31 30 35 30 20 2a  331,. /*  1050 *
2f7fc 2f 20 20 20 34 32 34 2c 20 20 34 34 34 2c 20 20  /   424,  444,  
2f7fd 20 38 38 2c 20 20 34 33 32 2c 20 20 31 34 35 2c   88,  432,  145,
2f7fe 20 20 34 34 34 2c 20 20 20 37 33 2c 20 20 33 31    444,   73,  31
2f7ff 35 2c 20 20 34 36 35 2c 20 20 34 36 36 2c 0a 20  5,  465,  466,. 
2f800 2f 2a 20 20 31 30 36 30 20 2a 2f 20 20 20 34 36  /*  1060 */   46
2f801 32 2c 20 20 34 36 32 2c 20 20 20 32 35 2c 20 20  2,  462,   25,  
2f802 20 32 35 2c 20 20 20 32 34 2c 20 20 20 32 34 2c   25,   24,   24,
2f803 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 33 39     24,   24,  39
2f804 35 2c 20 20 20 32 36 2c 0a 20 2f 2a 20 20 31 30  5,   26,. /*  10
2f805 37 30 20 2a 2f 20 20 20 20 32 36 2c 20 20 20 32  70 */    26,   2
2f806 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20  6,   26,   27,  
2f807 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32 38 2c   27,   28,   28,
2f808 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20 32 31     28,   29,  21
2f809 38 2c 0a 20 2f 2a 20 20 31 30 38 30 20 2a 2f 20  8,. /*  1080 */ 
2f80a 20 20 20 33 35 2c 20 20 33 34 34 2c 20 20 34 34     35,  344,  44
2f80b 35 2c 20 20 20 20 33 2c 20 20 33 33 37 2c 20 20  5,    3,  337,  
2f80c 33 39 34 2c 20 20 33 33 37 2c 20 20 33 33 33 2c  394,  337,  333,
2f80d 20 20 34 32 33 2c 20 20 32 37 38 2c 0a 20 2f 2a    423,  278,. /*
2f80e 20 20 31 30 39 30 20 2a 2f 20 20 20 33 38 38 2c    1090 */   388,
2f80f 20 20 32 37 36 2c 20 20 32 38 30 2c 20 20 32 30    276,  280,  20
2f810 37 2c 20 20 31 34 37 2c 20 20 20 33 35 2c 20 20  7,  147,   35,  
2f811 33 34 34 2c 20 20 33 34 31 2c 20 20 20 20 33 2c  344,  341,    3,
2f812 20 20 33 33 34 2c 0a 20 2f 2a 20 20 31 31 30 30    334,. /*  1100
2f813 20 2a 2f 20 20 20 34 32 34 2c 20 20 33 33 34 2c   */   424,  334,
2f814 20 20 33 33 33 2c 20 20 34 32 33 2c 20 20 33 30    333,  423,  30
2f815 38 2c 20 20 36 32 33 2c 20 20 32 38 30 2c 20 20  8,  623,  280,  
2f816 34 32 34 2c 20 20 34 34 34 2c 20 20 20 37 34 2c  424,  444,   74,
2f817 0a 20 2f 2a 20 20 31 31 31 30 20 2a 2f 20 20 20  . /*  1110 */   
2f818 34 34 34 2c 20 20 20 38 39 2c 20 20 33 34 31 2c  444,   89,  341,
2f819 20 20 33 33 37 2c 20 20 20 20 36 2c 20 20 33 34    337,    6,  34
2f81a 36 2c 20 20 33 33 38 2c 20 20 33 33 37 2c 20 20  6,  338,  337,  
2f81b 34 32 31 2c 20 20 33 33 37 2c 0a 20 2f 2a 20 20  421,  337,. /*  
2f81c 31 31 32 30 20 2a 2f 20 20 20 34 37 30 2c 20 20  1120 */   470,  
2f81d 34 32 34 2c 20 20 20 36 35 2c 20 20 33 33 32 2c  424,   65,  332,
2f81e 20 20 32 38 30 2c 20 20 34 38 31 2c 20 20 34 34    280,  481,  44
2f81f 36 2c 20 20 34 34 35 2c 20 20 33 33 34 2c 20 20  6,  445,  334,  
2f820 32 34 37 2c 0a 20 2f 2a 20 20 31 31 33 30 20 2a  247,. /*  1130 *
2f821 2f 20 20 20 33 34 36 2c 20 20 34 32 34 2c 20 20  /   346,  424,  
2f822 33 33 34 2c 20 20 34 32 34 2c 20 20 33 33 34 2c  334,  424,  334,
2f823 20 20 35 39 34 2c 20 20 32 38 30 2c 20 20 34 34    594,  280,  44
2f824 34 2c 20 20 20 39 30 2c 20 20 34 32 34 2c 0a 20  4,   90,  424,. 
2f825 2f 2a 20 20 31 31 34 30 20 2a 2f 20 20 20 34 38  /*  1140 */   48
2f826 31 2c 20 20 34 34 34 2c 20 20 20 39 31 2c 20 20  1,  444,   91,  
2f827 34 34 34 2c 20 20 20 39 32 2c 20 20 20 33 38 2c  444,   92,   38,
2f828 20 20 20 33 37 2c 20 20 36 32 35 2c 20 20 33 33     37,  625,  33
2f829 37 2c 20 20 34 31 30 2c 0a 20 2f 2a 20 20 31 31  7,  410,. /*  11
2f82a 35 30 20 2a 2f 20 20 20 20 34 37 2c 20 20 34 32  50 */    47,  42
2f82b 34 2c 20 20 32 33 37 2c 20 20 32 38 30 2c 20 20  4,  237,  280,  
2f82c 20 33 36 2c 20 20 33 33 35 2c 20 20 33 33 36 2c   36,  335,  336,
2f82d 20 20 33 35 34 2c 20 20 32 34 38 2c 20 20 34 35    354,  248,  45
2f82e 30 2c 0a 20 2f 2a 20 20 31 31 36 30 20 2a 2f 20  0,. /*  1160 */ 
2f82f 20 20 20 33 38 2c 20 20 20 33 37 2c 20 20 35 31     38,   37,  51
2f830 34 2c 20 20 33 33 34 2c 20 20 35 37 32 2c 20 20  4,  334,  572,  
2f831 33 38 31 2c 20 20 35 37 32 2c 20 20 35 39 36 2c  381,  572,  596,
2f832 20 20 34 32 34 2c 20 20 20 33 36 2c 0a 20 2f 2a    424,   36,. /*
2f833 20 20 31 31 37 30 20 2a 2f 20 20 20 33 33 35 2c    1170 */   335,
2f834 20 20 33 33 36 2c 20 20 34 34 34 2c 20 20 20 37    336,  444,   7
2f835 35 2c 20 20 34 35 30 2c 20 20 32 30 30 2c 20 20  5,  450,  200,  
2f836 35 30 36 2c 20 20 32 31 36 2c 20 20 31 35 34 2c  506,  216,  154,
2f837 20 20 35 39 37 2c 0a 20 2f 2a 20 20 31 31 38 30    597,. /*  1180
2f838 20 2a 2f 20 20 20 32 33 39 2c 20 20 32 34 30 2c   */   239,  240,
2f839 20 20 32 34 31 2c 20 20 31 34 36 2c 20 20 32 34    241,  146,  24
2f83a 33 2c 20 20 32 34 39 2c 20 20 35 34 37 2c 20 20  3,  249,  547,  
2f83b 35 39 33 2c 20 20 31 35 38 2c 20 20 34 33 33 2c  593,  158,  433,
2f83c 0a 20 2f 2a 20 20 31 31 39 30 20 2a 2f 20 20 20  . /*  1190 */   
2f83d 34 35 32 2c 20 20 34 35 32 2c 20 20 34 35 32 2c  452,  452,  452,
2f83e 20 20 34 35 33 2c 20 20 34 35 34 2c 20 20 20 31    453,  454,   1
2f83f 30 2c 20 20 35 39 38 2c 20 20 32 38 30 2c 20 20  0,  598,  280,  
2f840 20 32 30 2c 20 20 20 34 36 2c 0a 20 2f 2a 20 20   20,   46,. /*  
2f841 31 32 30 30 20 2a 2f 20 20 20 31 37 34 2c 20 20  1200 */   174,  
2f842 34 31 32 2c 20 20 32 39 38 2c 20 20 33 33 37 2c  412,  298,  337,
2f843 20 20 34 32 34 2c 20 20 34 35 32 2c 20 20 34 35    424,  452,  45
2f844 32 2c 20 20 34 35 32 2c 20 20 34 35 33 2c 20 20  2,  452,  453,  
2f845 34 35 34 2c 0a 20 2f 2a 20 20 31 32 31 30 20 2a  454,. /*  1210 *
2f846 2f 20 20 20 20 31 30 2c 20 20 32 39 39 2c 20 20  /    10,  299,  
2f847 34 32 34 2c 20 20 20 33 35 2c 20 20 33 34 34 2c  424,   35,  344,
2f848 20 20 33 35 32 2c 20 20 20 20 33 2c 20 20 32 35    352,    3,  25
2f849 30 2c 20 20 33 33 34 2c 20 20 34 33 34 2c 0a 20  0,  334,  434,. 
2f84a 2f 2a 20 20 31 32 32 30 20 2a 2f 20 20 20 33 33  /*  1220 */   33
2f84b 33 2c 20 20 34 32 33 2c 20 20 33 33 37 2c 20 20  3,  423,  337,  
2f84c 31 37 32 2c 20 20 32 38 30 2c 20 20 35 38 31 2c  172,  280,  581,
2f84d 20 20 32 30 38 2c 20 20 34 34 34 2c 20 20 20 31    208,  444,   1
2f84e 37 2c 20 20 31 37 31 2c 0a 20 2f 2a 20 20 31 32  7,  171,. /*  12
2f84f 33 30 20 2a 2f 20 20 20 33 34 31 2c 20 20 20 31  30 */   341,   1
2f850 39 2c 20 20 31 37 33 2c 20 20 34 34 37 2c 20 20  9,  173,  447,  
2f851 34 32 34 2c 20 20 34 32 32 2c 20 20 34 32 33 2c  424,  422,  423,
2f852 20 20 33 33 34 2c 20 20 33 33 37 2c 20 20 34 32    334,  337,  42
2f853 34 2c 0a 20 2f 2a 20 20 31 32 34 30 20 2a 2f 20  4,. /*  1240 */ 
2f854 20 20 32 33 35 2c 20 20 32 38 30 2c 20 20 32 30    235,  280,  20
2f855 34 2c 20 20 32 30 35 2c 20 20 32 30 36 2c 20 20  4,  205,  206,  
2f856 20 34 32 2c 20 20 34 34 34 2c 20 20 20 39 34 2c   42,  444,   94,
2f857 20 20 33 34 36 2c 20 20 34 33 35 2c 0a 20 2f 2a    346,  435,. /*
2f858 20 20 31 32 35 30 20 2a 2f 20 20 20 31 33 36 2c    1250 */   136,
2f859 20 20 34 35 31 2c 20 20 32 32 31 2c 20 20 33 33    451,  221,  33
2f85a 34 2c 20 20 33 30 38 2c 20 20 36 32 34 2c 20 20  4,  308,  624,  
2f85b 34 32 34 2c 20 20 33 34 39 2c 20 20 34 38 31 2c  424,  349,  481,
2f85c 20 20 34 39 30 2c 0a 20 2f 2a 20 20 31 32 36 30    490,. /*  1260
2f85d 20 2a 2f 20 20 20 34 34 35 2c 20 20 31 35 32 2c   */   445,  152,
2f85e 20 20 34 34 34 2c 20 20 20 39 35 2c 20 20 34 32    444,   95,  42
2f85f 34 2c 20 20 34 32 34 2c 20 20 34 32 34 2c 20 20  4,  424,  424,  
2f860 32 33 36 2c 20 20 35 30 33 2c 20 20 34 39 31 2c  236,  503,  491,
2f861 0a 20 2f 2a 20 20 31 32 37 30 20 2a 2f 20 20 20  . /*  1270 */   
2f862 35 30 37 2c 20 20 31 37 39 2c 20 20 34 32 34 2c  507,  179,  424,
2f863 20 20 34 38 31 2c 20 20 34 32 34 2c 20 20 34 30    481,  424,  40
2f864 32 2c 20 20 32 39 35 2c 20 20 32 38 35 2c 20 20  2,  295,  285,  
2f865 20 33 38 2c 20 20 20 33 37 2c 0a 20 2f 2a 20 20   38,   37,. /*  
2f866 31 32 38 30 20 2a 2f 20 20 20 32 37 31 2c 20 20  1280 */   271,  
2f867 33 31 30 2c 20 20 31 35 38 2c 20 20 34 32 34 2c  310,  158,  424,
2f868 20 20 32 39 36 2c 20 20 34 32 34 2c 20 20 32 31    296,  424,  21
2f869 36 2c 20 20 20 33 36 2c 20 20 33 33 35 2c 20 20  6,   36,  335,  
2f86a 33 33 36 2c 0a 20 2f 2a 20 20 31 32 39 30 20 2a  336,. /*  1290 *
2f86b 2f 20 20 20 35 30 39 2c 20 20 32 36 36 2c 20 20  /   509,  266,  
2f86c 34 35 30 2c 20 20 31 39 30 2c 20 20 31 39 31 2c  450,  190,  191,
2f86d 20 20 35 33 39 2c 20 20 32 36 37 2c 20 20 36 32    539,  267,  62
2f86e 35 2c 20 20 35 35 38 2c 20 20 32 37 33 2c 0a 20  5,  558,  273,. 
2f86f 2f 2a 20 20 31 33 30 30 20 2a 2f 20 20 20 32 37  /*  1300 */   27
2f870 35 2c 20 20 20 34 38 2c 20 20 32 37 37 2c 20 20  5,   48,  277,  
2f871 35 32 32 2c 20 20 32 37 39 2c 20 20 34 32 34 2c  522,  279,  424,
2f872 20 20 34 32 34 2c 20 20 34 35 30 2c 20 20 32 35    424,  450,  25
2f873 35 2c 20 20 34 30 39 2c 0a 20 2f 2a 20 20 31 33  5,  409,. /*  13
2f874 31 30 20 2a 2f 20 20 20 34 32 34 2c 20 20 34 32  10 */   424,  42
2f875 34 2c 20 20 32 35 37 2c 20 20 34 32 34 2c 20 20  4,  257,  424,  
2f876 34 32 34 2c 20 20 34 32 34 2c 20 20 32 38 34 2c  424,  424,  284,
2f877 20 20 34 32 34 2c 20 20 33 38 36 2c 20 20 34 32    424,  386,  42
2f878 34 2c 0a 20 2f 2a 20 20 31 33 32 30 20 2a 2f 20  4,. /*  1320 */ 
2f879 20 20 33 35 37 2c 20 20 35 38 34 2c 20 20 35 38    357,  584,  58
2f87a 35 2c 20 20 34 35 32 2c 20 20 34 35 32 2c 20 20  5,  452,  452,  
2f87b 34 35 32 2c 20 20 34 35 33 2c 20 20 34 35 34 2c  452,  453,  454,
2f87c 20 20 20 31 30 2c 20 20 32 35 39 2c 0a 20 2f 2a     10,  259,. /*
2f87d 20 20 31 33 33 30 20 2a 2f 20 20 20 33 39 33 2c    1330 */   393,
2f87e 20 20 34 32 34 2c 20 20 32 38 39 2c 20 20 34 32    424,  289,  42
2f87f 34 2c 20 20 35 39 32 2c 20 20 36 30 33 2c 20 20  4,  592,  603,  
2f880 34 32 34 2c 20 20 34 32 34 2c 20 20 34 35 32 2c  424,  424,  452,
2f881 20 20 34 35 32 2c 0a 20 2f 2a 20 20 31 33 34 30    452,. /*  1340
2f882 20 2a 2f 20 20 20 34 35 32 2c 20 20 32 39 37 2c   */   452,  297,
2f883 20 20 33 30 30 2c 20 20 33 30 31 2c 20 20 35 30    300,  301,  50
2f884 35 2c 20 20 34 32 34 2c 20 20 36 31 37 2c 20 20  5,  424,  617,  
2f885 34 32 34 2c 20 20 33 36 33 2c 20 20 34 32 34 2c  424,  363,  424,
2f886 0a 20 2f 2a 20 20 31 33 35 30 20 2a 2f 20 20 20  . /*  1350 */   
2f887 34 32 34 2c 20 20 33 37 33 2c 20 20 35 37 37 2c  424,  373,  577,
2f888 20 20 31 35 38 2c 20 20 31 35 38 2c 20 20 35 31    158,  158,  51
2f889 31 2c 20 20 34 32 34 2c 20 20 34 32 34 2c 20 20  1,  424,  424,  
2f88a 34 32 34 2c 20 20 35 32 35 2c 0a 20 2f 2a 20 20  424,  525,. /*  
2f88b 31 33 36 30 20 2a 2f 20 20 20 35 38 38 2c 20 20  1360 */   588,  
2f88c 34 32 34 2c 20 20 31 35 34 2c 20 20 35 38 39 2c  424,  154,  589,
2f88d 20 20 36 30 31 2c 20 20 20 35 34 2c 20 20 20 35    601,   54,   5
2f88e 34 2c 20 20 36 32 30 2c 20 20 35 31 32 2c 20 20  4,  620,  512,  
2f88f 33 30 36 2c 0a 20 2f 2a 20 20 31 33 37 30 20 2a  306,. /*  1370 *
2f890 2f 20 20 20 33 31 39 2c 20 20 35 33 30 2c 20 20  /   319,  530,  
2f891 35 33 31 2c 20 20 35 33 35 2c 20 20 32 36 34 2c  531,  535,  264,
2f892 20 20 31 30 37 2c 20 20 32 32 38 2c 20 20 35 33    107,  228,  53
2f893 36 2c 20 20 35 33 34 2c 20 20 33 37 35 2c 0a 20  6,  534,  375,. 
2f894 2f 2a 20 20 31 33 38 30 20 2a 2f 20 20 20 35 35  /*  1380 */   55
2f895 39 2c 20 20 33 30 34 2c 20 20 35 36 30 2c 20 20  9,  304,  560,  
2f896 35 36 31 2c 20 20 33 30 35 2c 20 20 32 32 37 2c  561,  305,  227,
2f897 20 20 32 32 39 2c 20 20 35 35 33 2c 20 20 35 36    229,  553,  56
2f898 37 2c 20 20 31 36 31 2c 0a 20 2f 2a 20 20 31 33  7,  161,. /*  13
2f899 39 30 20 2a 2f 20 20 20 31 36 32 2c 20 20 33 37  90 */   162,  37
2f89a 39 2c 20 20 33 37 37 2c 20 20 31 36 33 2c 20 20  9,  377,  163,  
2f89b 20 35 31 2c 20 20 32 30 39 2c 20 20 35 36 39 2c   51,  209,  569,
2f89c 20 20 32 38 32 2c 20 20 31 36 34 2c 20 20 35 37    282,  164,  57
2f89d 30 2c 0a 20 2f 2a 20 20 31 34 30 30 20 2a 2f 20  0,. /*  1400 */ 
2f89e 20 20 33 38 35 2c 20 20 31 34 33 2c 20 20 35 38    385,  143,  58
2f89f 30 2c 20 20 31 31 36 2c 20 20 31 31 39 2c 20 20  0,  116,  119,  
2f8a0 31 38 33 2c 20 20 34 30 30 2c 20 20 35 39 30 2c  183,  400,  590,
2f8a1 20 20 34 30 31 2c 20 20 31 32 31 2c 0a 20 2f 2a    401,  121,. /*
2f8a2 20 20 31 34 31 30 20 2a 2f 20 20 20 31 32 32 2c    1410 */   122,
2f8a3 20 20 31 32 33 2c 20 20 31 32 34 2c 20 20 31 32    123,  124,  12
2f8a4 36 2c 20 20 35 39 39 2c 20 20 33 32 38 2c 20 20  6,  599,  328,  
2f8a5 36 31 34 2c 20 20 20 35 35 2c 20 20 20 35 38 2c  614,   55,   58,
2f8a6 20 20 36 31 35 2c 0a 20 2f 2a 20 20 31 34 32 30    615,. /*  1420
2f8a7 20 2a 2f 20 20 20 36 31 36 2c 20 20 36 31 39 2c   */   616,  619,
2f8a8 20 20 20 36 32 2c 20 20 34 31 38 2c 20 20 31 30     62,  418,  10
2f8a9 33 2c 20 20 32 32 36 2c 20 20 31 31 31 2c 20 20  3,  226,  111,  
2f8aa 31 37 36 2c 20 20 32 34 32 2c 20 20 31 38 32 2c  176,  242,  182,
2f8ab 0a 20 2f 2a 20 20 31 34 33 30 20 2a 2f 20 20 20  . /*  1430 */   
2f8ac 34 33 37 2c 20 20 33 31 33 2c 20 20 32 30 31 2c  437,  313,  201,
2f8ad 20 20 33 31 34 2c 20 20 36 37 30 2c 20 20 36 37    314,  670,  67
2f8ae 31 2c 20 20 36 37 32 2c 20 20 31 34 39 2c 20 20  1,  672,  149,  
2f8af 31 35 30 2c 20 20 34 36 37 2c 0a 20 2f 2a 20 20  150,  467,. /*  
2f8b0 31 34 34 30 20 2a 2f 20 20 20 34 36 34 2c 20 20  1440 */   464,  
2f8b1 20 33 34 2c 20 20 34 38 33 2c 20 20 34 37 31 2c   34,  483,  471,
2f8b2 20 20 34 38 30 2c 20 20 31 38 34 2c 20 20 31 39    480,  184,  19
2f8b3 37 2c 20 20 35 30 32 2c 20 20 34 38 34 2c 20 20  7,  502,  484,  
2f8b4 20 20 35 2c 0a 20 2f 2a 20 20 31 34 35 30 20 2a    5,. /*  1450 *
2f8b5 2f 20 20 20 34 38 35 2c 20 20 31 35 31 2c 20 20  /   485,  151,  
2f8b6 34 38 39 2c 20 20 20 34 34 2c 20 20 31 34 31 2c  489,   44,  141,
2f8b7 20 20 20 31 31 2c 20 20 31 30 36 2c 20 20 31 36     11,  106,  16
2f8b8 30 2c 20 20 32 32 35 2c 20 20 35 31 38 2c 0a 20  0,  225,  518,. 
2f8b9 2f 2a 20 20 31 34 36 30 20 2a 2f 20 20 20 35 31  /*  1460 */   51
2f8ba 39 2c 20 20 20 34 39 2c 20 20 35 31 30 2c 20 20  9,   49,  510,  
2f8bb 31 30 38 2c 20 20 33 36 37 2c 20 20 32 37 30 2c  108,  367,  270,
2f8bc 20 20 20 31 32 2c 20 20 31 35 35 2c 20 20 31 30     12,  155,  10
2f8bd 39 2c 20 20 20 35 30 2c 0a 20 2f 2a 20 20 31 34  9,   50,. /*  14
2f8be 37 30 20 2a 2f 20 20 20 31 31 30 2c 20 20 32 36  70 */   110,  26
2f8bf 32 2c 20 20 33 37 36 2c 20 20 31 38 36 2c 20 20  2,  376,  186,  
2f8c0 35 36 38 2c 20 20 31 31 33 2c 20 20 31 34 32 2c  568,  113,  142,
2f8c1 20 20 31 35 34 2c 20 20 31 36 35 2c 20 20 31 31    154,  165,  11
2f8c2 35 2c 0a 20 2f 2a 20 20 31 34 38 30 20 2a 2f 20  5,. /*  1480 */ 
2f8c3 20 20 20 31 35 2c 20 20 32 38 33 2c 20 20 35 38     15,  283,  58
2f8c4 33 2c 20 20 31 36 36 2c 20 20 31 36 37 2c 20 20  3,  166,  167,  
2f8c5 33 38 30 2c 20 20 35 38 36 2c 20 20 31 31 37 2c  380,  586,  117,
2f8c6 20 20 20 31 33 2c 20 20 31 32 30 2c 0a 20 2f 2a     13,  120,. /*
2f8c7 20 20 31 34 39 30 20 2a 2f 20 20 20 33 37 32 2c    1490 */   372,
2f8c8 20 20 20 35 32 2c 20 20 20 35 33 2c 20 20 31 31     52,   53,  11
2f8c9 38 2c 20 20 35 39 31 2c 20 20 31 36 39 2c 20 20  8,  591,  169,  
2f8ca 31 31 34 2c 20 20 31 37 30 2c 20 20 35 39 35 2c  114,  170,  595,
2f8cb 20 20 31 32 35 2c 0a 20 2f 2a 20 20 31 35 30 30    125,. /*  1500
2f8cc 20 2a 2f 20 20 20 31 32 37 2c 20 20 35 37 31 2c   */   127,  571,
2f8cd 20 20 35 37 35 2c 20 20 36 30 32 2c 20 20 20 31    575,  602,   1
2f8ce 34 2c 20 20 31 32 38 2c 20 20 36 31 31 2c 20 20  4,  128,  611,  
2f8cf 36 31 32 2c 20 20 20 36 31 2c 20 20 31 37 35 2c  612,   61,  175,
2f8d0 0a 20 2f 2a 20 20 31 35 31 30 20 2a 2f 20 20 20  . /*  1510 */   
2f8d1 31 38 39 2c 20 20 34 31 35 2c 20 20 33 30 32 2c  189,  415,  302,
2f8d2 20 20 36 32 37 2c 20 20 39 36 30 2c 20 20 39 36    627,  960,  96
2f8d3 30 2c 20 20 39 36 30 2c 20 20 39 36 30 2c 20 20  0,  960,  960,  
2f8d4 34 31 31 2c 0a 7d 3b 0a 73 74 61 74 69 63 20 63  411,.};.static c
2f8d5 6f 6e 73 74 20 59 59 43 4f 44 45 54 59 50 45 20  onst YYCODETYPE 
2f8d6 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 5d 20 3d  yy_lookahead[] =
2f8d7 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20   {. /*     0 */ 
2f8d8 20 20 20 31 39 2c 20 20 31 34 32 2c 20 20 31 34     19,  142,  14
2f8d9 33 2c 20 20 31 34 34 2c 20 20 31 34 35 2c 20 20  3,  144,  145,  
2f8da 20 32 34 2c 20 20 31 31 36 2c 20 20 20 32 36 2c   24,  116,   26,
2f8db 20 20 20 37 35 2c 20 20 20 37 36 2c 0a 20 2f 2a     75,   76,. /*
2f8dc 20 20 20 20 31 30 20 2a 2f 20 20 20 20 37 37 2c      10 */    77,
2f8dd 20 20 20 37 38 2c 20 20 20 32 35 2c 20 20 20 38     78,   25,   8
2f8de 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20  0,   81,   82,  
2f8df 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c   83,   84,   85,
2f8e0 20 20 20 38 36 2c 0a 20 2f 2a 20 20 20 20 32 30     86,. /*    20
2f8e1 20 2a 2f 20 20 20 20 38 37 2c 20 20 20 38 38 2c   */    87,   88,
2f8e2 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 32     89,   90,   2
2f8e3 36 2c 20 20 20 32 37 2c 20 20 31 36 30 2c 20 20  6,   27,  160,  
2f8e4 20 32 36 2c 20 20 20 32 37 2c 20 20 20 34 38 2c   26,   27,   48,
2f8e5 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20 20  . /*    30 */   
2f8e6 20 34 39 2c 20 20 20 37 39 2c 20 20 20 38 30 2c   49,   79,   80,
2f8e7 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38     81,   82,   8
2f8e8 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20  3,   84,   85,  
2f8e9 20 38 36 2c 20 20 20 38 37 2c 0a 20 2f 2a 20 20   86,   87,. /*  
2f8ea 20 20 34 30 20 2a 2f 20 20 20 20 38 38 2c 20 20    40 */    88,  
2f8eb 20 38 39 2c 20 20 20 39 30 2c 20 20 32 32 32 2c   89,   90,  222,
2f8ec 20 20 32 32 33 2c 20 20 32 32 34 2c 20 20 32 32    223,  224,  22
2f8ed 35 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20  5,   66,   67,  
2f8ee 20 36 38 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a   68,. /*    50 *
2f8ef 2f 20 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20  /    69,   70,  
2f8f0 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c   71,   72,   73,
2f8f1 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37     74,   75,   7
2f8f2 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 0a 20  6,   77,   78,. 
2f8f3 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20 31 39  /*    60 */   19
2f8f4 34 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20  4,   80,   81,  
2f8f5 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c   82,   83,   84,
2f8f6 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38     85,   86,   8
2f8f7 37 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20 20 20  7,   88,. /*    
2f8f8 37 30 20 2a 2f 20 20 20 20 38 39 2c 20 20 20 39  70 */    89,   9
2f8f9 30 2c 20 20 20 31 39 2c 20 20 20 39 30 2c 20 20  0,   19,   90,  
2f8fa 20 31 39 2c 20 20 20 39 34 2c 20 20 31 37 34 2c   19,   94,  174,
2f8fb 20 20 20 32 35 2c 20 20 20 32 35 2c 20 20 20 38     25,   25,   8
2f8fc 30 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20  0,. /*    80 */ 
2f8fd 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38     81,   82,   8
2f8fe 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20  3,   84,   85,  
2f8ff 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c   86,   87,   88,
2f900 20 20 20 38 39 2c 20 20 20 39 30 2c 0a 20 2f 2a     89,   90,. /*
2f901 20 20 20 20 39 30 20 2a 2f 20 20 20 20 32 36 2c      90 */    26,
2f902 20 20 20 32 37 2c 20 20 20 39 34 2c 20 20 20 39     27,   94,   9
2f903 35 2c 20 20 20 39 36 2c 20 20 20 39 34 2c 20 20  5,   96,   94,  
2f904 20 39 35 2c 20 20 20 39 39 2c 20 20 31 30 30 2c   95,   99,  100,
2f905 20 20 31 30 31 2c 0a 20 2f 2a 20 20 20 31 30 30    101,. /*   100
2f906 20 2a 2f 20 20 20 20 31 39 2c 20 20 20 34 38 2c   */    19,   48,
2f907 20 20 20 34 39 2c 20 20 31 35 30 2c 20 20 31 37     49,  150,  17
2f908 34 2c 20 20 20 35 32 2c 20 20 31 31 39 2c 20 20  4,   52,  119,  
2f909 31 36 36 2c 20 20 31 31 30 2c 20 20 20 38 34 2c  166,  110,   84,
2f90a 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20 20  . /*   110 */   
2f90b 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c   85,   86,   87,
2f90c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39     88,   89,   9
2f90d 30 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20  0,   26,   27,  
2f90e 31 36 35 2c 20 20 20 36 36 2c 0a 20 2f 2a 20 20  165,   66,. /*  
2f90f 20 31 32 30 20 2a 2f 20 20 20 20 36 37 2c 20 20   120 */    67,  
2f910 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c   68,   69,   70,
2f911 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37     71,   72,   7
2f912 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20  3,   74,   75,  
2f913 20 37 36 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a   76,. /*   130 *
2f914 2f 20 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20  /    77,   78,  
2f915 31 38 36 2c 20 20 20 38 30 2c 20 20 20 38 31 2c  186,   80,   81,
2f916 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
2f917 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 0a 20  4,   85,   86,. 
2f918 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20 20 38  /*   140 */    8
2f919 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20  7,   88,   89,  
2f91a 20 39 30 2c 20 20 20 31 39 2c 20 20 20 39 30 2c   90,   19,   90,
2f91b 20 20 32 30 35 2c 20 20 20 39 35 2c 20 20 20 38    205,   95,   8
2f91c 34 2c 20 20 20 38 35 2c 0a 20 2f 2a 20 20 20 31  4,   85,. /*   1
2f91d 35 30 20 2a 2f 20 20 20 31 38 36 2c 20 20 20 39  50 */   186,   9
2f91e 36 2c 20 20 20 39 37 2c 20 20 20 39 38 2c 20 20  6,   97,   98,  
2f91f 20 39 39 2c 20 20 31 30 30 2c 20 20 31 30 31 2c   99,  100,  101,
2f920 20 20 31 30 32 2c 20 20 20 39 34 2c 20 20 20 39    102,   94,   9
2f921 35 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20  5,. /*   160 */ 
2f922 20 20 31 39 35 2c 20 20 20 39 37 2c 20 20 31 35    195,   97,  15
2f923 30 2c 20 20 32 32 32 2c 20 20 31 30 39 2c 20 20  0,  222,  109,  
2f924 32 32 34 2c 20 20 32 32 35 2c 20 20 20 32 36 2c  224,  225,   26,
2f925 20 20 31 30 34 2c 20 20 31 30 35 2c 0a 20 2f 2a    104,  105,. /*
2f926 20 20 20 31 37 30 20 2a 2f 20 20 20 32 31 37 2c     170 */   217,
2f927 20 20 20 39 30 2c 20 20 31 32 30 2c 20 20 20 34     90,  120,   4
2f928 38 2c 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20  8,   49,   50,  
2f929 20 38 36 2c 20 20 31 36 35 2c 20 20 20 39 37 2c   86,  165,   97,
2f92a 20 20 20 39 38 2c 0a 20 2f 2a 20 20 20 31 38 30     98,. /*   180
2f92b 20 2a 2f 20 20 20 20 39 39 2c 20 20 31 30 30 2c   */    99,  100,
2f92c 20 20 31 30 31 2c 20 20 31 30 32 2c 20 20 20 39    101,  102,   9
2f92d 34 2c 20 20 20 39 35 2c 20 20 31 37 34 2c 20 20  4,   95,  174,  
2f92e 31 37 35 2c 20 20 20 20 31 2c 20 20 20 20 32 2c  175,    1,    2,
2f92f 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20 20  . /*   190 */   
2f930 31 30 39 2c 20 20 20 36 36 2c 20 20 20 36 37 2c  109,   66,   67,
2f931 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37     68,   69,   7
2f932 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20  0,   71,   72,  
2f933 20 37 33 2c 20 20 20 37 34 2c 0a 20 2f 2a 20 20   73,   74,. /*  
2f934 20 32 30 30 20 2a 2f 20 20 20 20 37 35 2c 20 20   200 */    75,  
2f935 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c   76,   77,   78,
2f936 20 20 31 39 31 2c 20 20 20 38 30 2c 20 20 20 38    191,   80,   8
2f937 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20  1,   82,   83,  
2f938 20 38 34 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a   84,. /*   210 *
2f939 2f 20 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20  /    85,   86,  
2f93a 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c   87,   88,   89,
2f93b 20 20 20 39 30 2c 20 20 20 31 39 2c 20 20 31 31     90,   19,  11
2f93c 36 2c 20 20 20 33 35 2c 20 20 31 35 30 2c 0a 20  6,   35,  150,. 
2f93d 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 20 20 31  /*   220 */    1
2f93e 32 2c 20 20 20 32 34 2c 20 20 32 30 38 2c 20 20  2,   24,  208,  
2f93f 31 35 30 2c 20 20 32 32 32 2c 20 20 31 35 30 2c  150,  222,  150,
2f940 20 20 32 32 34 2c 20 20 32 32 35 2c 20 20 32 31    224,  225,  21
2f941 36 2c 20 20 31 32 38 2c 0a 20 2f 2a 20 20 20 32  6,  128,. /*   2
2f942 33 30 20 2a 2f 20 20 20 31 36 31 2c 20 20 31 36  30 */   161,  16
2f943 32 2c 20 20 31 35 30 2c 20 20 32 32 31 2c 20 20  2,  150,  221,  
2f944 31 36 35 2c 20 20 20 39 34 2c 20 20 20 32 38 2c  165,   94,   28,
2f945 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 20 35    150,  165,   5
2f946 36 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20  6,. /*   240 */ 
2f947 20 20 31 36 35 2c 20 20 31 39 37 2c 20 20 31 36    165,  197,  16
2f948 30 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20 20  0,  170,  171,  
2f949 20 34 38 2c 20 20 20 34 39 2c 20 20 31 36 35 2c   48,   49,  165,
2f94a 20 20 32 30 34 2c 20 20 31 37 34 2c 0a 20 2f 2a    204,  174,. /*
2f94b 20 20 20 32 35 30 20 2a 2f 20 20 20 31 37 35 2c     250 */   175,
2f94c 20 20 20 34 33 2c 20 20 31 36 35 2c 20 20 20 34     43,  165,   4
2f94d 35 2c 20 20 31 38 35 2c 20 20 31 38 36 2c 20 20  5,  185,  186,  
2f94e 31 37 34 2c 20 20 31 37 35 2c 20 20 31 36 39 2c  174,  175,  169,
2f94f 20 20 31 37 30 2c 0a 20 2f 2a 20 20 20 32 36 30    170,. /*   260
2f950 20 2a 2f 20 20 20 31 37 31 2c 20 20 31 37 34 2c   */   171,  174,
2f951 20 20 31 37 35 2c 20 20 20 36 36 2c 20 20 20 36    175,   66,   6
2f952 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20  7,   68,   69,  
2f953 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c   70,   71,   72,
2f954 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20 20  . /*   270 */   
2f955 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c   73,   74,   75,
2f956 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37     76,   77,   7
2f957 38 2c 20 20 31 39 34 2c 20 20 20 38 30 2c 20 20  8,  194,   80,  
2f958 20 38 31 2c 20 20 20 38 32 2c 0a 20 2f 2a 20 20   81,   82,. /*  
2f959 20 32 38 30 20 2a 2f 20 20 20 20 38 33 2c 20 20   280 */    83,  
2f95a 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c   84,   85,   86,
2f95b 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38     87,   88,   8
2f95c 39 2c 20 20 20 39 30 2c 20 20 20 31 39 2c 20 20  9,   90,   19,  
2f95d 32 31 34 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a  214,. /*   290 *
2f95e 2f 20 20 20 32 31 35 2c 20 20 31 30 38 2c 20 20  /   215,  108,  
2f95f 31 35 30 2c 20 20 20 32 35 2c 20 20 31 34 38 2c  150,   25,  148,
2f960 20 20 31 35 30 2c 20 20 20 36 34 2c 20 20 20 32    150,   64,   2
2f961 32 2c 20 20 32 31 36 2c 20 20 20 32 34 2c 0a 20  2,  216,   24,. 
2f962 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20 20 31 34  /*   300 */   14
2f963 36 2c 20 20 31 34 37 2c 20 20 32 31 35 2c 20 20  6,  147,  215,  
2f964 32 32 31 2c 20 20 32 33 31 2c 20 20 32 33 32 2c  221,  231,  232,
2f965 20 20 31 35 32 2c 20 20 31 36 35 2c 20 20 31 35    152,  165,  15
2f966 34 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20 33  4,  150,. /*   3
2f967 31 30 20 2a 2f 20 20 20 31 36 35 2c 20 20 20 34  10 */   165,   4
2f968 39 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20 20  9,  170,  171,  
2f969 31 36 30 2c 20 20 31 38 31 2c 20 20 31 38 32 2c  160,  181,  182,
2f96a 20 20 20 34 38 2c 20 20 20 34 39 2c 20 20 31 37     48,   49,  17
2f96b 34 2c 0a 20 2f 2a 20 20 20 33 32 30 20 2a 2f 20  4,. /*   320 */ 
2f96c 20 20 31 37 35 2c 20 20 32 33 32 2c 20 20 31 38    175,  232,  18
2f96d 38 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20  8,  165,  165,  
2f96e 20 32 31 2c 20 20 20 39 34 2c 20 20 20 20 30 2c   21,   94,    0,
2f96f 20 20 20 20 31 2c 20 20 20 20 32 2c 0a 20 2f 2a      1,    2,. /*
2f970 20 20 20 33 33 30 20 2a 2f 20 20 20 20 39 38 2c     330 */    98,
2f971 20 20 20 35 35 2c 20 20 31 37 34 2c 20 20 31 37     55,  174,  17
2f972 34 2c 20 20 31 37 35 2c 20 20 20 36 36 2c 20 20  4,  175,   66,  
2f973 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c   67,   68,   69,
2f974 20 20 20 37 30 2c 0a 20 2f 2a 20 20 20 33 34 30     70,. /*   340
2f975 20 2a 2f 20 20 20 20 37 31 2c 20 20 20 37 32 2c   */    71,   72,
2f976 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
2f977 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20  5,   76,   77,  
2f978 20 37 38 2c 20 20 31 39 34 2c 20 20 20 38 30 2c   78,  194,   80,
2f979 0a 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20 20 20  . /*   350 */   
2f97a 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c   81,   82,   83,
2f97b 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
2f97c 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20  6,   87,   88,  
2f97d 20 38 39 2c 20 20 20 39 30 2c 0a 20 2f 2a 20 20   89,   90,. /*  
2f97e 20 33 36 30 20 2a 2f 20 20 20 20 31 39 2c 20 20   360 */    19,  
2f97f 31 32 39 2c 20 20 31 33 30 2c 20 20 31 33 31 2c  129,  130,  131,
2f980 20 20 20 39 36 2c 20 20 20 36 31 2c 20 20 31 30     96,   61,  10
2f981 34 2c 20 20 20 39 39 2c 20 20 31 30 30 2c 20 20  4,   99,  100,  
2f982 31 30 31 2c 0a 20 2f 2a 20 20 20 33 37 30 20 2a  101,. /*   370 *
2f983 2f 20 20 20 31 35 30 2c 20 20 32 32 36 2c 20 20  /   150,  226,  
2f984 32 31 38 2c 20 20 32 33 31 2c 20 20 32 33 32 2c  218,  231,  232,
2f985 20 20 32 31 36 2c 20 20 31 35 30 2c 20 20 31 39    216,  150,  19
2f986 36 2c 20 20 31 31 30 2c 20 20 20 32 36 2c 0a 20  6,  110,   26,. 
2f987 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20 20 20 32  /*   380 */    2
2f988 37 2c 20 20 31 30 35 2c 20 20 31 30 36 2c 20 20  7,  105,  106,  
2f989 31 30 37 2c 20 20 31 35 38 2c 20 20 31 36 35 2c  107,  158,  165,
2f98a 20 20 31 38 33 2c 20 20 31 36 31 2c 20 20 31 36    183,  161,  16
2f98b 32 2c 20 20 20 34 38 2c 0a 20 2f 2a 20 20 20 33  2,   48,. /*   3
2f98c 39 30 20 2a 2f 20 20 20 20 34 39 2c 20 20 31 36  90 */    49,  16
2f98d 35 2c 20 20 31 31 36 2c 20 20 31 36 36 2c 20 20  5,  116,  166,  
2f98e 31 37 34 2c 20 20 31 37 35 2c 20 20 20 38 36 2c  174,  175,   86,
2f98f 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38     87,   88,   8
2f990 39 2c 0a 20 2f 2a 20 20 20 34 30 30 20 2a 2f 20  9,. /*   400 */ 
2f991 20 20 20 39 30 2c 20 20 32 34 37 2c 20 20 20 38     90,  247,   8
2f992 34 2c 20 20 20 38 35 2c 20 20 31 30 30 2c 20 20  4,   85,  100,  
2f993 31 33 36 2c 20 20 31 38 33 2c 20 20 20 36 36 2c  136,  183,   66,
2f994 20 20 20 36 37 2c 20 20 20 36 38 2c 0a 20 2f 2a     67,   68,. /*
2f995 20 20 20 34 31 30 20 2a 2f 20 20 20 20 36 39 2c     410 */    69,
2f996 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37     70,   71,   7
2f997 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20  2,   73,   74,  
2f998 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c   75,   76,   77,
2f999 20 20 20 37 38 2c 0a 20 2f 2a 20 20 20 34 32 30     78,. /*   420
2f99a 20 2a 2f 20 20 20 20 31 31 2c 20 20 20 38 30 2c   */    11,   80,
2f99b 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38     81,   82,   8
2f99c 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20  3,   84,   85,  
2f99d 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c   86,   87,   88,
2f99e 0a 20 2f 2a 20 20 20 34 33 30 20 2a 2f 20 20 20  . /*   430 */   
2f99f 20 38 39 2c 20 20 20 39 30 2c 20 20 20 31 39 2c   89,   90,   19,
2f9a0 20 20 31 38 33 2c 20 20 31 35 30 2c 20 20 32 32    183,  150,  22
2f9a1 32 2c 20 20 20 32 33 2c 20 20 32 32 34 2c 20 20  2,   23,  224,  
2f9a2 32 32 35 2c 20 20 32 33 37 2c 0a 20 2f 2a 20 20  225,  237,. /*  
2f9a3 20 34 34 30 20 2a 2f 20 20 20 32 32 30 2c 20 20   440 */   220,  
2f9a4 31 35 30 2c 20 20 31 33 38 2c 20 20 20 32 34 2c  150,  138,   24,
2f9a5 20 20 31 36 30 2c 20 20 20 32 36 2c 20 20 20 32    160,   26,   2
2f9a6 32 2c 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20  2,   94,   95,  
2f9a7 31 36 35 2c 0a 20 2f 2a 20 20 20 34 35 30 20 2a  165,. /*   450 *
2f9a8 2f 20 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20  /    26,   27,  
2f9a9 20 32 36 2c 20 20 20 32 37 2c 20 20 20 38 39 2c   26,   27,   89,
2f9aa 20 20 20 39 30 2c 20 20 31 36 35 2c 20 20 32 34     90,  165,  24
2f9ab 34 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 0a 20  4,  174,  175,. 
2f9ac 2f 2a 20 20 20 34 36 30 20 2a 2f 20 20 20 32 33  /*   460 */   23
2f9ad 36 2c 20 20 20 34 38 2c 20 20 20 34 39 2c 20 20  6,   48,   49,  
2f9ae 20 32 32 2c 20 20 20 35 35 2c 20 20 31 37 34 2c   22,   55,  174,
2f9af 20 20 31 37 35 2c 20 20 20 32 36 2c 20 20 20 32    175,   26,   2
2f9b0 37 2c 20 20 20 32 32 2c 0a 20 2f 2a 20 20 20 34  7,   22,. /*   4
2f9b1 37 30 20 2a 2f 20 20 20 20 32 33 2c 20 20 31 36  70 */    23,  16
2f9b2 33 2c 20 20 20 32 35 2c 20 20 31 32 30 2c 20 20  3,   25,  120,  
2f9b3 31 36 36 2c 20 20 31 36 37 2c 20 20 31 36 38 2c  166,  167,  168,
2f9b4 20 20 31 33 36 2c 20 20 31 39 34 2c 20 20 20 36    136,  194,   6
2f9b5 36 2c 0a 20 2f 2a 20 20 20 34 38 30 20 2a 2f 20  6,. /*   480 */ 
2f9b6 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36     67,   68,   6
2f9b7 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20  9,   70,   71,  
2f9b8 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c   72,   73,   74,
2f9b9 20 20 20 37 35 2c 20 20 20 37 36 2c 0a 20 2f 2a     75,   76,. /*
2f9ba 20 20 20 34 39 30 20 2a 2f 20 20 20 20 37 37 2c     490 */    77,
2f9bb 20 20 20 37 38 2c 20 20 31 35 33 2c 20 20 20 38     78,  153,   8
2f9bc 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20  0,   81,   82,  
2f9bd 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c   83,   84,   85,
2f9be 20 20 20 38 36 2c 0a 20 2f 2a 20 20 20 35 30 30     86,. /*   500
2f9bf 20 2a 2f 20 20 20 20 38 37 2c 20 20 20 38 38 2c   */    87,   88,
2f9c0 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 31     89,   90,   1
2f9c1 39 2c 20 20 31 39 36 2c 20 20 31 36 30 2c 20 20  9,  196,  160,  
2f9c2 31 35 30 2c 20 20 20 32 33 2c 20 20 31 37 33 2c  150,   23,  173,
2f9c3 0a 20 2f 2a 20 20 20 35 31 30 20 2a 2f 20 20 20  . /*   510 */   
2f9c4 31 39 38 2c 20 20 32 32 30 2c 20 20 20 36 35 2c  198,  220,   65,
2f9c5 20 20 20 39 34 2c 20 20 31 30 35 2c 20 20 31 30     94,  105,  10
2f9c6 36 2c 20 20 31 30 37 2c 20 20 31 38 31 2c 20 20  6,  107,  181,  
2f9c7 20 39 34 2c 20 20 20 39 35 2c 0a 20 2f 2a 20 20   94,   95,. /*  
2f9c8 20 35 32 30 20 2a 2f 20 20 20 20 39 34 2c 20 20   520 */    94,  
2f9c9 20 39 35 2c 20 20 31 36 35 2c 20 20 20 39 36 2c   95,  165,   96,
2f9ca 20 20 31 35 30 2c 20 20 31 31 36 2c 20 20 20 39    150,  116,   9
2f9cb 39 2c 20 20 31 30 30 2c 20 20 31 30 31 2c 20 20  9,  100,  101,  
2f9cc 20 33 31 2c 0a 20 2f 2a 20 20 20 35 33 30 20 2a   31,. /*   530 *
2f9cd 2f 20 20 20 31 35 30 2c 20 20 31 37 34 2c 20 20  /   150,  174,  
2f9ce 31 37 35 2c 20 20 20 34 38 2c 20 20 20 34 39 2c  175,   48,   49,
2f9cf 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20 31 31     94,   95,  11
2f9d0 30 2c 20 20 20 34 30 2c 20 20 31 36 35 2c 0a 20  0,   40,  165,. 
2f9d1 2f 2a 20 20 20 35 34 30 20 2a 2f 20 20 20 31 39  /*   540 */   19
2f9d2 34 2c 20 20 31 36 31 2c 20 20 31 36 32 2c 20 20  4,  161,  162,  
2f9d3 31 36 36 2c 20 20 31 36 30 2c 20 20 31 36 35 2c  166,  160,  165,
2f9d4 20 20 31 32 30 2c 20 20 31 36 36 2c 20 20 31 37    120,  166,  17
2f9d5 34 2c 20 20 31 37 35 2c 0a 20 2f 2a 20 20 20 35  4,  175,. /*   5
2f9d6 35 30 20 2a 2f 20 20 20 32 33 33 2c 20 20 20 36  50 */   233,   6
2f9d7 36 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20  6,   67,   68,  
2f9d8 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c   69,   70,   71,
2f9d9 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37     72,   73,   7
2f9da 34 2c 0a 20 2f 2a 20 20 20 35 36 30 20 2a 2f 20  4,. /*   560 */ 
2f9db 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37     75,   76,   7
2f9dc 37 2c 20 20 20 37 38 2c 20 20 32 31 38 2c 20 20  7,   78,  218,  
2f9dd 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c   80,   81,   82,
2f9de 20 20 20 38 33 2c 20 20 20 38 34 2c 0a 20 2f 2a     83,   84,. /*
2f9df 20 20 20 35 37 30 20 2a 2f 20 20 20 20 38 35 2c     570 */    85,
2f9e0 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38     86,   87,   8
2f9e1 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20  8,   89,   90,  
2f9e2 20 31 39 2c 20 20 20 31 32 2c 20 20 31 39 34 2c   19,   12,  194,
2f9e3 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20 35 38 30    150,. /*   580
2f9e4 20 2a 2f 20 20 20 20 32 33 2c 20 20 32 33 35 2c   */    23,  235,
2f9e5 20 20 32 30 35 2c 20 20 31 36 36 2c 20 20 31 36    205,  166,  16
2f9e6 37 2c 20 20 31 36 38 2c 20 20 32 34 30 2c 20 20  7,  168,  240,  
2f9e7 31 35 30 2c 20 20 31 37 32 2c 20 20 31 37 33 2c  150,  172,  173,
2f9e8 0a 20 2f 2a 20 20 20 35 39 30 20 2a 2f 20 20 20  . /*   590 */   
2f9e9 32 30 36 2c 20 20 32 30 37 2c 20 20 31 35 30 2c  206,  207,  150,
2f9ea 20 20 20 32 38 2c 20 20 31 36 35 2c 20 20 31 39     28,  165,  19
2f9eb 30 2c 20 20 31 39 31 2c 20 20 31 38 31 2c 20 20  0,  191,  181,  
2f9ec 20 32 33 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20   23,  150,. /*  
2f9ed 20 36 30 30 20 2a 2f 20 20 20 20 34 38 2c 20 20   600 */    48,  
2f9ee 20 34 39 2c 20 20 31 36 35 2c 20 20 31 37 34 2c   49,  165,  174,
2f9ef 20 20 31 37 35 2c 20 20 20 34 38 2c 20 20 20 34    175,   48,   4
2f9f0 39 2c 20 20 31 36 35 2c 20 20 20 34 33 2c 20 20  9,  165,   43,  
2f9f1 32 33 33 2c 0a 20 2f 2a 20 20 20 36 31 30 20 2a  233,. /*   610 *
2f9f2 2f 20 20 20 20 34 35 2c 20 20 31 37 34 2c 20 20  /    45,  174,  
2f9f3 31 37 35 2c 20 20 31 33 35 2c 20 20 31 36 35 2c  175,  135,  165,
2f9f4 20 20 31 33 37 2c 20 20 31 37 34 2c 20 20 31 37    137,  174,  17
2f9f5 35 2c 20 20 31 39 30 2c 20 20 31 39 31 2c 0a 20  5,  190,  191,. 
2f9f6 2f 2a 20 20 20 36 32 30 20 2a 2f 20 20 20 20 35  /*   620 */    5
2f9f7 35 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20  5,   69,   70,  
2f9f8 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38 2c   66,   67,   68,
2f9f9 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37     69,   70,   7
2f9fa 31 2c 20 20 20 37 32 2c 0a 20 2f 2a 20 20 20 36  1,   72,. /*   6
2f9fb 33 30 20 2a 2f 20 20 20 20 37 33 2c 20 20 20 37  30 */    73,   7
2f9fc 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20  4,   75,   76,  
2f9fd 20 37 37 2c 20 20 20 37 38 2c 20 20 31 35 30 2c   77,   78,  150,
2f9fe 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38     80,   81,   8
2f9ff 32 2c 0a 20 2f 2a 20 20 20 36 34 30 20 2a 2f 20  2,. /*   640 */ 
2fa00 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38     83,   84,   8
2fa01 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20  5,   86,   87,  
2fa02 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c   88,   89,   90,
2fa03 20 20 20 31 39 2c 20 20 31 35 30 2c 0a 20 2f 2a     19,  150,. /*
2fa04 20 20 20 36 35 30 20 2a 2f 20 20 20 20 39 38 2c     650 */    98,
2fa05 20 20 31 36 35 2c 20 20 20 32 33 2c 20 20 32 35    165,   23,  25
2fa06 30 2c 20 20 32 35 31 2c 20 20 31 35 30 2c 20 20  0,  251,  150,  
2fa07 31 35 35 2c 20 20 31 35 30 2c 20 20 20 31 39 2c  155,  150,   19,
2fa08 20 20 20 32 32 2c 0a 20 2f 2a 20 20 20 36 36 30     22,. /*   660
2fa09 20 2a 2f 20 20 20 31 37 34 2c 20 20 31 37 35 2c   */   174,  175,
2fa0a 20 20 32 31 33 2c 20 20 20 32 36 2c 20 20 31 36    213,   26,  16
2fa0b 35 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20  5,  169,  170,  
2fa0c 31 37 31 2c 20 20 20 32 33 2c 20 20 31 35 30 2c  171,   23,  150,
2fa0d 0a 20 2f 2a 20 20 20 36 37 30 20 2a 2f 20 20 20  . /*   670 */   
2fa0e 31 36 35 2c 20 20 31 35 30 2c 20 20 31 36 35 2c  165,  150,  165,
2fa0f 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 31    174,  175,   1
2fa10 39 2c 20 20 31 35 30 2c 20 20 20 34 38 2c 20 20  9,  150,   48,  
2fa11 20 34 39 2c 20 20 31 37 34 2c 0a 20 2f 2a 20 20   49,  174,. /*  
2fa12 20 36 38 30 20 2a 2f 20 20 20 31 37 35 2c 20 20   680 */   175,  
2fa13 31 37 34 2c 20 20 31 37 35 2c 20 20 20 32 37 2c  174,  175,   27,
2fa14 20 20 31 36 35 2c 20 20 32 32 38 2c 20 20 31 36    165,  228,  16
2fa15 35 2c 20 20 31 31 32 2c 20 20 20 34 39 2c 20 20  5,  112,   49,  
2fa16 31 31 34 2c 0a 20 2f 2a 20 20 20 36 39 30 20 2a  114,. /*   690 *
2fa17 2f 20 20 20 31 31 35 2c 20 20 31 36 35 2c 20 20  /   115,  165,  
2fa18 31 37 37 2c 20 20 31 37 34 2c 20 20 31 37 35 2c  177,  174,  175,
2fa19 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36     66,   67,   6
2fa1a 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 0a 20  8,   69,   70,. 
2fa1b 2f 2a 20 20 20 37 30 30 20 2a 2f 20 20 20 20 37  /*   700 */    7
2fa1c 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20  1,   72,   73,  
2fa1d 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c   74,   75,   76,
2fa1e 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 31 35     77,   78,  15
2fa1f 30 2c 20 20 20 38 30 2c 0a 20 2f 2a 20 20 20 37  0,   80,. /*   7
2fa20 31 30 20 2a 2f 20 20 20 20 38 31 2c 20 20 20 38  10 */    81,   8
2fa21 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20  2,   83,   84,  
2fa22 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c   85,   86,   87,
2fa23 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39     88,   89,   9
2fa24 30 2c 0a 20 2f 2a 20 20 20 37 32 30 20 2a 2f 20  0,. /*   720 */ 
2fa25 20 20 20 31 39 2c 20 20 31 35 30 2c 20 20 20 32     19,  150,   2
2fa26 33 2c 20 20 31 36 35 2c 20 20 20 32 35 2c 20 20  3,  165,   25,  
2fa27 20 32 34 2c 20 20 31 35 30 2c 20 20 31 35 30 2c   24,  150,  150,
2fa28 20 20 32 33 32 2c 20 20 31 35 30 2c 0a 20 2f 2a    232,  150,. /*
2fa29 20 20 20 37 33 30 20 2a 2f 20 20 20 32 32 39 2c     730 */   229,
2fa2a 20 20 20 39 34 2c 20 20 31 37 34 2c 20 20 31 37     94,  174,  17
2fa2b 35 2c 20 20 32 31 33 2c 20 20 32 33 34 2c 20 20  5,  213,  234,  
2fa2c 31 36 35 2c 20 20 20 32 35 2c 20 20 31 35 30 2c  165,   25,  150,
2fa2d 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20 37 34 30    150,. /*   740
2fa2e 20 2a 2f 20 20 20 31 35 30 2c 20 20 31 36 35 2c   */   150,  165,
2fa2f 20 20 31 36 35 2c 20 20 31 30 34 2c 20 20 31 36    165,  104,  16
2fa30 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  5,  174,  175,  
2fa31 31 37 37 2c 20 20 31 30 39 2c 20 20 20 34 38 2c  177,  109,   48,
2fa32 0a 20 2f 2a 20 20 20 37 35 30 20 2a 2f 20 20 20  . /*   750 */   
2fa33 20 34 39 2c 20 20 31 37 34 2c 20 20 31 37 35 2c   49,  174,  175,
2fa34 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36    165,  165,  16
2fa35 35 2c 20 20 20 31 39 2c 20 20 31 31 32 2c 20 20  5,   19,  112,  
2fa36 20 32 32 2c 20 20 31 31 34 2c 0a 20 2f 2a 20 20   22,  114,. /*  
2fa37 20 37 36 30 20 2a 2f 20 20 20 31 31 35 2c 20 20   760 */   115,  
2fa38 31 37 37 2c 20 20 31 37 34 2c 20 20 31 37 35 2c  177,  174,  175,
2fa39 20 20 20 32 37 2c 20 20 20 31 36 2c 20 20 31 38     27,   16,  18
2fa3a 37 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20  7,   66,   67,  
2fa3b 20 36 38 2c 0a 20 2f 2a 20 20 20 37 37 30 20 2a   68,. /*   770 *
2fa3c 2f 20 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20  /    69,   70,  
2fa3d 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c   71,   72,   73,
2fa3e 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37     74,   75,   7
2fa3f 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 0a 20  6,   77,   78,. 
2fa40 2f 2a 20 20 20 37 38 30 20 2a 2f 20 20 20 31 35  /*   780 */   15
2fa41 30 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20  0,   80,   81,  
2fa42 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c   82,   83,   84,
2fa43 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38     85,   86,   8
2fa44 37 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20 20 37  7,   88,. /*   7
2fa45 39 30 20 2a 2f 20 20 20 20 38 39 2c 20 20 20 39  90 */    89,   9
2fa46 30 2c 20 20 20 31 39 2c 20 20 31 35 30 2c 20 20  0,   19,  150,  
2fa47 20 39 35 2c 20 20 31 36 35 2c 20 20 31 34 34 2c   95,  165,  144,
2fa48 20 20 31 34 35 2c 20 20 31 35 30 2c 20 20 31 35    145,  150,  15
2fa49 30 2c 0a 20 2f 2a 20 20 20 38 30 30 20 2a 2f 20  0,. /*   800 */ 
2fa4a 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 31    150,  150,  11
2fa4b 33 2c 20 20 32 31 33 2c 20 20 31 37 34 2c 20 20  3,  213,  174,  
2fa4c 31 37 35 2c 20 20 31 31 37 2c 20 20 20 35 38 2c  175,  117,   58,
2fa4d 20 20 31 36 35 2c 20 20 20 36 30 2c 0a 20 2f 2a    165,   60,. /*
2fa4e 20 20 20 38 31 30 20 2a 2f 20 20 20 20 37 34 2c     810 */    74,
2fa4f 20 20 20 32 33 2c 20 20 31 35 30 2c 20 20 31 36     23,  150,  16
2fa50 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20  5,  165,  165,  
2fa51 31 36 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c  165,  174,  175,
2fa52 20 20 31 32 30 2c 0a 20 2f 2a 20 20 20 38 32 30    120,. /*   820
2fa53 20 2a 2f 20 20 20 20 31 39 2c 20 20 20 34 38 2c   */    19,   48,
2fa54 20 20 20 34 39 2c 20 20 31 37 34 2c 20 20 31 37     49,  174,  17
2fa55 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  5,  174,  175,  
2fa56 31 36 35 2c 20 20 32 30 39 2c 20 20 31 36 36 2c  165,  209,  166,
2fa57 0a 20 2f 2a 20 20 20 38 33 30 20 2a 2f 20 20 20  . /*   830 */   
2fa58 32 34 31 2c 20 20 20 32 32 2c 20 20 20 32 33 2c  241,   22,   23,
2fa59 20 20 31 36 36 2c 20 20 20 32 35 2c 20 20 31 38    166,   25,  18
2fa5a 37 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  7,  174,  175,  
2fa5b 31 32 36 2c 20 20 20 36 36 2c 0a 20 2f 2a 20 20  126,   66,. /*  
2fa5c 20 38 34 30 20 2a 2f 20 20 20 20 36 37 2c 20 20   840 */    67,  
2fa5d 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c   68,   69,   70,
2fa5e 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37     71,   72,   7
2fa5f 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20  3,   74,   75,  
2fa60 20 37 36 2c 0a 20 2f 2a 20 20 20 38 35 30 20 2a   76,. /*   850 *
2fa61 2f 20 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20  /    77,   78,  
2fa62 31 35 30 2c 20 20 20 38 30 2c 20 20 20 38 31 2c  150,   80,   81,
2fa63 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
2fa64 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 0a 20  4,   85,   86,. 
2fa65 2f 2a 20 20 20 38 36 30 20 2a 2f 20 20 20 20 38  /*   860 */    8
2fa66 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20  7,   88,   89,  
2fa67 20 39 30 2c 20 20 20 31 39 2c 20 20 31 35 30 2c   90,   19,  150,
2fa68 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 32 30    150,  165,  20
2fa69 35 2c 20 20 31 38 32 2c 0a 20 2f 2a 20 20 20 38  5,  182,. /*   8
2fa6a 37 30 20 2a 2f 20 20 20 20 38 36 2c 20 20 31 35  70 */    86,  15
2fa6b 30 2c 20 20 32 30 35 2c 20 20 31 35 30 2c 20 20  0,  205,  150,  
2fa6c 20 36 35 2c 20 20 31 36 36 2c 20 20 31 37 34 2c   65,  166,  174,
2fa6d 20 20 31 37 35 2c 20 20 32 30 36 2c 20 20 32 30    175,  206,  20
2fa6e 37 2c 0a 20 2f 2a 20 20 20 38 38 30 20 2a 2f 20  7,. /*   880 */ 
2fa6f 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 37    165,  165,  17
2fa70 37 2c 20 20 31 35 30 2c 20 20 20 32 33 2c 20 20  7,  150,   23,  
2fa71 20 32 35 2c 20 20 31 36 35 2c 20 20 31 33 38 2c   25,  165,  138,
2fa72 20 20 31 36 35 2c 20 20 31 37 34 2c 0a 20 2f 2a    165,  174,. /*
2fa73 20 20 20 38 39 30 20 2a 2f 20 20 20 31 37 35 2c     890 */   175,
2fa74 20 20 32 34 31 2c 20 20 31 36 36 2c 20 20 20 34    241,  166,   4
2fa75 38 2c 20 20 20 34 39 2c 20 20 31 37 34 2c 20 20  8,   49,  174,  
2fa76 31 37 35 2c 20 20 31 31 33 2c 20 20 31 36 35 2c  175,  113,  165,
2fa77 20 20 20 39 38 2c 0a 20 2f 2a 20 20 20 39 30 30     98,. /*   900
2fa78 20 2a 2f 20 20 20 31 31 32 2c 20 20 31 31 37 2c   */   112,  117,
2fa79 20 20 31 31 34 2c 20 20 31 31 35 2c 20 20 20 20    114,  115,    
2fa7a 37 2c 20 20 20 20 38 2c 20 20 20 20 39 2c 20 20  7,    8,    9,  
2fa7b 31 37 34 2c 20 20 31 37 35 2c 20 20 31 39 33 2c  174,  175,  193,
2fa7c 0a 20 2f 2a 20 20 20 39 31 30 20 2a 2f 20 20 20  . /*   910 */   
2fa7d 31 38 37 2c 20 20 20 36 36 2c 20 20 20 36 37 2c  187,   66,   67,
2fa7e 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37     68,   69,   7
2fa7f 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20  0,   71,   72,  
2fa80 20 37 33 2c 20 20 20 37 34 2c 0a 20 2f 2a 20 20   73,   74,. /*  
2fa81 20 39 32 30 20 2a 2f 20 20 20 20 37 35 2c 20 20   920 */    75,  
2fa82 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c   76,   77,   78,
2fa83 20 20 31 35 30 2c 20 20 20 38 30 2c 20 20 20 38    150,   80,   8
2fa84 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20  1,   82,   83,  
2fa85 20 38 34 2c 0a 20 2f 2a 20 20 20 39 33 30 20 2a   84,. /*   930 *
2fa86 2f 20 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20  /    85,   86,  
2fa87 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c   87,   88,   89,
2fa88 20 20 20 39 30 2c 20 20 20 31 39 2c 20 20 31 35     90,   19,  15
2fa89 30 2c 20 20 20 39 37 2c 20 20 31 36 35 2c 0a 20  0,   97,  165,. 
2fa8a 2f 2a 20 20 20 39 34 30 20 2a 2f 20 20 20 31 36  /*   940 */   16
2fa8b 30 2c 20 20 31 35 30 2c 20 20 31 37 37 2c 20 20  0,  150,  177,  
2fa8c 31 35 30 2c 20 20 31 35 30 2c 20 20 31 35 30 2c  150,  150,  150,
2fa8d 20 20 32 34 38 2c 20 20 32 34 39 2c 20 20 31 37    248,  249,  17
2fa8e 34 2c 20 20 31 37 35 2c 0a 20 2f 2a 20 20 20 39  4,  175,. /*   9
2fa8f 35 30 20 2a 2f 20 20 20 20 39 37 2c 20 20 20 39  50 */    97,   9
2fa90 38 2c 20 20 31 36 35 2c 20 20 31 32 39 2c 20 20  8,  165,  129,  
2fa91 31 33 30 2c 20 20 31 35 30 2c 20 20 31 36 35 2c  130,  150,  165,
2fa92 20 20 31 31 36 2c 20 20 31 36 35 2c 20 20 31 36    116,  165,  16
2fa93 35 2c 0a 20 2f 2a 20 20 20 39 36 30 20 2a 2f 20  5,. /*   960 */ 
2fa94 20 20 31 36 35 2c 20 20 31 37 34 2c 20 20 31 37    165,  174,  17
2fa95 35 2c 20 20 31 30 33 2c 20 20 31 37 38 2c 20 20  5,  103,  178,  
2fa96 20 34 38 2c 20 20 20 34 39 2c 20 20 31 37 34 2c   48,   49,  174,
2fa97 20 20 31 37 35 2c 20 20 31 32 38 2c 0a 20 2f 2a    175,  128,. /*
2fa98 20 20 20 39 37 30 20 2a 2f 20 20 20 31 36 35 2c     970 */   165,
2fa99 20 20 20 39 38 2c 20 20 32 34 32 2c 20 20 31 31     98,  242,  11
2fa9a 32 2c 20 20 31 39 34 2c 20 20 31 31 34 2c 20 20  2,  194,  114,  
2fa9b 31 31 35 2c 20 20 31 39 39 2c 20 20 31 38 37 2c  115,  199,  187,
2fa9c 20 20 31 37 34 2c 0a 20 2f 2a 20 20 20 39 38 30    174,. /*   980
2fa9d 20 2a 2f 20 20 20 31 37 35 2c 20 20 31 38 37 2c   */   175,  187,
2fa9e 20 20 31 30 39 2c 20 20 32 34 32 2c 20 20 20 36    109,  242,   6
2fa9f 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20  7,   68,   69,  
2faa0 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c   70,   71,   72,
2faa1 0a 20 2f 2a 20 20 20 39 39 30 20 2a 2f 20 20 20  . /*   990 */   
2faa2 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c   73,   74,   75,
2faa3 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37     76,   77,   7
2faa4 38 2c 20 20 31 35 30 2c 20 20 20 38 30 2c 20 20  8,  150,   80,  
2faa5 20 38 31 2c 20 20 20 38 32 2c 0a 20 2f 2a 20 20   81,   82,. /*  
2faa6 31 30 30 30 20 2a 2f 20 20 20 20 38 33 2c 20 20  1000 */    83,  
2faa7 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c   84,   85,   86,
2faa8 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38     87,   88,   8
2faa9 39 2c 20 20 20 39 30 2c 20 20 20 31 39 2c 20 20  9,   90,   19,  
2faaa 31 35 30 2c 0a 20 2f 2a 20 20 31 30 31 30 20 2a  150,. /*  1010 *
2faab 2f 20 20 20 31 36 30 2c 20 20 31 36 35 2c 20 20  /   160,  165,  
2faac 32 30 39 2c 20 20 31 35 30 2c 20 20 31 31 32 2c  209,  150,  112,
2faad 20 20 31 35 30 2c 20 20 31 31 34 2c 20 20 31 31    150,  114,  11
2faae 35 2c 20 20 20 20 37 2c 20 20 20 20 38 2c 0a 20  5,    7,    8,. 
2faaf 2f 2a 20 20 31 30 32 30 20 2a 2f 20 20 20 31 37  /*  1020 */   17
2fab0 34 2c 20 20 31 37 35 2c 20 20 32 30 39 2c 20 20  4,  175,  209,  
2fab1 20 20 36 2c 20 20 31 36 35 2c 20 20 20 32 39 2c    6,  165,   29,
2fab2 20 20 31 39 39 2c 20 20 31 35 30 2c 20 20 31 36    199,  150,  16
2fab3 35 2c 20 20 20 33 33 2c 0a 20 2f 2a 20 20 31 30  5,   33,. /*  10
2fab4 33 30 20 2a 2f 20 20 20 31 36 35 2c 20 20 31 35  30 */   165,  15
2fab5 30 2c 20 20 31 34 39 2c 20 20 31 37 34 2c 20 20  0,  149,  174,  
2fab6 31 37 35 2c 20 20 31 35 30 2c 20 20 32 34 31 2c  175,  150,  241,
2fab7 20 20 20 34 38 2c 20 20 20 34 39 2c 20 20 31 37     48,   49,  17
2fab8 34 2c 0a 20 2f 2a 20 20 31 30 34 30 20 2a 2f 20  4,. /*  1040 */ 
2fab9 20 20 31 37 35 2c 20 20 31 34 39 2c 20 20 31 36    175,  149,  16
2faba 35 2c 20 20 20 34 37 2c 20 20 31 39 34 2c 20 20  5,   47,  194,  
2fabb 31 34 39 2c 20 20 31 36 35 2c 20 20 20 31 36 2c  149,  165,   16,
2fabc 20 20 31 36 30 2c 20 20 31 34 39 2c 0a 20 2f 2a    160,  149,. /*
2fabd 20 20 31 30 35 30 20 2a 2f 20 20 20 31 36 35 2c    1050 */   165,
2fabe 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 31    174,  175,   1
2fabf 33 2c 20 20 31 35 31 2c 20 20 31 37 34 2c 20 20  3,  151,  174,  
2fac0 31 37 35 2c 20 20 20 36 38 2c 20 20 20 36 39 2c  175,   68,   69,
2fac1 20 20 20 37 30 2c 0a 20 2f 2a 20 20 31 30 36 30     70,. /*  1060
2fac2 20 2a 2f 20 20 20 20 37 31 2c 20 20 20 37 32 2c   */    71,   72,
2fac3 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
2fac4 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20  5,   76,   77,  
2fac5 20 37 38 2c 20 20 32 31 38 2c 20 20 20 38 30 2c   78,  218,   80,
2fac6 0a 20 2f 2a 20 20 31 30 37 30 20 2a 2f 20 20 20  . /*  1070 */   
2fac7 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c   81,   82,   83,
2fac8 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
2fac9 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20  6,   87,   88,  
2faca 20 38 39 2c 20 20 20 39 30 2c 0a 20 2f 2a 20 20   89,   90,. /*  
2facb 31 30 38 30 20 2a 2f 20 20 20 20 31 39 2c 20 20  1080 */    19,  
2facc 20 32 30 2c 20 20 31 39 34 2c 20 20 20 32 32 2c   20,  194,   22,
2facd 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 35    150,  150,  15
2face 30 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20  0,   26,   27,  
2facf 20 35 38 2c 0a 20 2f 2a 20 20 31 30 39 30 20 2a   58,. /*  1090 *
2fad0 2f 20 20 20 32 34 30 2c 20 20 20 36 30 2c 20 20  /   240,   60,  
2fad1 31 35 30 2c 20 20 31 36 30 2c 20 20 31 35 31 2c  150,  160,  151,
2fad2 20 20 20 31 39 2c 20 20 20 32 30 2c 20 20 20 33     19,   20,   3
2fad3 36 2c 20 20 20 32 32 2c 20 20 31 36 35 2c 0a 20  6,   22,  165,. 
2fad4 2f 2a 20 20 31 31 30 30 20 2a 2f 20 20 20 31 36  /*  1100 */   16
2fad5 35 2c 20 20 31 36 35 2c 20 20 20 32 36 2c 20 20  5,  165,   26,  
2fad6 20 32 37 2c 20 20 20 32 32 2c 20 20 20 32 33 2c   27,   22,   23,
2fad7 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31 37    150,  165,  17
2fad8 34 2c 20 20 31 37 35 2c 0a 20 2f 2a 20 20 31 31  4,  175,. /*  11
2fad9 31 30 20 2a 2f 20 20 20 31 37 34 2c 20 20 31 37  10 */   174,  17
2fada 35 2c 20 20 20 33 36 2c 20 20 31 35 30 2c 20 20  5,   36,  150,  
2fadb 20 32 35 2c 20 20 20 35 34 2c 20 20 31 35 30 2c   25,   54,  150,
2fadc 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 35    150,  150,  15
2fadd 30 2c 0a 20 2f 2a 20 20 31 31 32 30 20 2a 2f 20  0,. /*  1120 */ 
2fade 20 20 20 32 33 2c 20 20 31 36 35 2c 20 20 20 32     23,  165,   2
2fadf 35 2c 20 20 31 35 39 2c 20 20 31 35 30 2c 20 20  5,  159,  150,  
2fae0 20 36 34 2c 20 20 31 39 34 2c 20 20 31 39 34 2c   64,  194,  194,
2fae1 20 20 31 36 35 2c 20 20 31 39 39 2c 0a 20 2f 2a    165,  199,. /*
2fae2 20 20 31 31 33 30 20 2a 2f 20 20 20 20 35 34 2c    1130 */    54,
2fae3 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36    165,  165,  16
2fae4 35 2c 20 20 31 36 35 2c 20 20 31 39 33 2c 20 20  5,  165,  193,  
2fae5 31 35 30 2c 20 20 31 37 34 2c 20 20 31 37 35 2c  150,  174,  175,
2fae6 20 20 31 36 35 2c 0a 20 2f 2a 20 20 31 31 34 30    165,. /*  1140
2fae7 20 2a 2f 20 20 20 20 36 34 2c 20 20 31 37 34 2c   */    64,  174,
2fae8 20 20 31 37 35 2c 20 20 31 37 34 2c 20 20 31 37    175,  174,  17
2fae9 35 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20  5,   84,   85,  
2faea 20 36 35 2c 20 20 31 35 30 2c 20 20 31 39 33 2c   65,  150,  193,
2faeb 0a 20 2f 2a 20 20 31 31 35 30 20 2a 2f 20 20 20  . /*  1150 */   
2faec 31 32 36 2c 20 20 31 36 35 2c 20 20 32 31 37 2c  126,  165,  217,
2faed 20 20 31 35 30 2c 20 20 20 39 33 2c 20 20 20 39    150,   93,   9
2faee 34 2c 20 20 20 39 35 2c 20 20 31 32 33 2c 20 20  4,   95,  123,  
2faef 32 30 30 2c 20 20 20 39 38 2c 0a 20 2f 2a 20 20  200,   98,. /*  
2faf0 31 31 36 30 20 2a 2f 20 20 20 20 38 34 2c 20 20  1160 */    84,  
2faf1 20 38 35 2c 20 20 20 38 36 2c 20 20 31 36 35 2c   85,   86,  165,
2faf2 20 20 31 30 35 2c 20 20 31 30 36 2c 20 20 31 30    105,  106,  10
2faf3 37 2c 20 20 31 39 33 2c 20 20 31 36 35 2c 20 20  7,  193,  165,  
2faf4 20 39 33 2c 0a 20 2f 2a 20 20 31 31 37 30 20 2a   93,. /*  1170 *
2faf5 2f 20 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20  /    94,   95,  
2faf6 31 37 34 2c 20 20 31 37 35 2c 20 20 20 39 38 2c  174,  175,   98,
2faf7 20 20 20 20 35 2c 20 20 20 32 33 2c 20 20 31 31      5,   23,  11
2faf8 36 2c 20 20 20 32 35 2c 20 20 31 39 33 2c 0a 20  6,   25,  193,. 
2faf9 2f 2a 20 20 31 31 38 30 20 2a 2f 20 20 20 20 31  /*  1180 */    1
2fafa 30 2c 20 20 20 31 31 2c 20 20 20 31 32 2c 20 20  0,   11,   12,  
2fafb 20 31 33 2c 20 20 20 31 34 2c 20 20 32 30 31 2c   13,   14,  201,
2fafc 20 20 20 32 33 2c 20 20 20 31 37 2c 20 20 20 32     23,   17,   2
2fafd 35 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 31 31  5,  150,. /*  11
2fafe 39 30 20 2a 2f 20 20 20 31 32 39 2c 20 20 31 33  90 */   129,  13
2faff 30 2c 20 20 31 33 31 2c 20 20 31 33 32 2c 20 20  0,  131,  132,  
2fb00 31 33 33 2c 20 20 31 33 34 2c 20 20 31 39 33 2c  133,  134,  193,
2fb01 20 20 31 35 30 2c 20 20 31 32 35 2c 20 20 31 32    150,  125,  12
2fb02 34 2c 0a 20 2f 2a 20 20 31 32 30 30 20 2a 2f 20  4,. /*  1200 */ 
2fb03 20 20 20 33 30 2c 20 20 32 34 35 2c 20 20 20 33     30,  245,   3
2fb04 32 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20  2,  150,  165,  
2fb05 31 32 39 2c 20 20 31 33 30 2c 20 20 31 33 31 2c  129,  130,  131,
2fb06 20 20 31 33 32 2c 20 20 31 33 33 2c 0a 20 2f 2a    132,  133,. /*
2fb07 20 20 31 32 31 30 20 2a 2f 20 20 20 31 33 34 2c    1210 */   134,
2fb08 20 20 20 34 31 2c 20 20 31 36 35 2c 20 20 20 31     41,  165,   1
2fb09 39 2c 20 20 20 32 30 2c 20 20 31 32 32 2c 20 20  9,   20,  122,  
2fb0a 20 32 32 2c 20 20 32 30 32 2c 20 20 31 36 35 2c   22,  202,  165,
2fb0b 20 20 31 35 30 2c 0a 20 2f 2a 20 20 31 32 32 30    150,. /*  1220
2fb0c 20 2a 2f 20 20 20 20 32 36 2c 20 20 20 32 37 2c   */    26,   27,
2fb0d 20 20 31 35 30 2c 20 20 20 35 33 2c 20 20 31 35    150,   53,  15
2fb0e 30 2c 20 20 20 35 35 2c 20 20 31 36 30 2c 20 20  0,   55,  160,  
2fb0f 31 37 34 2c 20 20 31 37 35 2c 20 20 20 35 39 2c  174,  175,   59,
2fb10 0a 20 2f 2a 20 20 31 32 33 30 20 2a 2f 20 20 20  . /*  1230 */   
2fb11 20 33 36 2c 20 20 20 32 32 2c 20 20 20 36 32 2c   36,   22,   62,
2fb12 20 20 32 30 33 2c 20 20 31 36 35 2c 20 20 20 32    203,  165,   2
2fb13 36 2c 20 20 20 32 37 2c 20 20 31 36 35 2c 20 20  6,   27,  165,  
2fb14 31 35 30 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20  150,  165,. /*  
2fb15 31 32 34 30 20 2a 2f 20 20 20 31 39 33 2c 20 20  1240 */   193,  
2fb16 31 35 30 2c 20 20 31 30 35 2c 20 20 31 30 36 2c  150,  105,  106,
2fb17 20 20 31 30 37 2c 20 20 31 33 35 2c 20 20 31 37    107,  135,  17
2fb18 34 2c 20 20 31 37 35 2c 20 20 20 35 34 2c 20 20  4,  175,   54,  
2fb19 31 35 30 2c 0a 20 2f 2a 20 20 31 32 35 30 20 2a  150,. /*  1250 *
2fb1a 2f 20 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20  /   150,  150,  
2fb1b 32 32 37 2c 20 20 31 36 35 2c 20 20 20 32 32 2c  227,  165,   22,
2fb1c 20 20 20 32 33 2c 20 20 31 36 35 2c 20 20 31 35     23,  165,  15
2fb1d 30 2c 20 20 20 36 34 2c 20 20 31 35 30 2c 0a 20  0,   64,  150,. 
2fb1e 2f 2a 20 20 31 32 36 30 20 2a 2f 20 20 20 31 39  /*  1260 */   19
2fb1f 34 2c 20 20 31 31 38 2c 20 20 31 37 34 2c 20 20  4,  118,  174,  
2fb20 31 37 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c  175,  165,  165,
2fb21 20 20 31 36 35 2c 20 20 31 39 33 2c 20 20 31 35    165,  193,  15
2fb22 30 2c 20 20 31 35 37 2c 0a 20 2f 2a 20 20 31 32  0,  157,. /*  12
2fb23 37 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 31 35  70 */   150,  15
2fb24 37 2c 20 20 31 36 35 2c 20 20 20 36 34 2c 20 20  7,  165,   64,  
2fb25 31 36 35 2c 20 20 31 30 35 2c 20 20 31 30 36 2c  165,  105,  106,
2fb26 20 20 31 30 37 2c 20 20 20 38 34 2c 20 20 20 38    107,   84,   8
2fb27 35 2c 0a 20 2f 2a 20 20 31 32 38 30 20 2a 2f 20  5,. /*  1280 */ 
2fb28 20 20 20 32 33 2c 20 20 31 31 31 2c 20 20 20 32     23,  111,   2
2fb29 35 2c 20 20 31 36 35 2c 20 20 31 39 33 2c 20 20  5,  165,  193,  
2fb2a 31 36 35 2c 20 20 31 31 36 2c 20 20 20 39 33 2c  165,  116,   93,
2fb2b 20 20 20 39 34 2c 20 20 20 39 35 2c 0a 20 2f 2a     94,   95,. /*
2fb2c 20 20 31 32 39 30 20 2a 2f 20 20 20 31 35 30 2c    1290 */   150,
2fb2d 20 20 31 35 30 2c 20 20 20 39 38 2c 20 20 20 38    150,   98,   8
2fb2e 34 2c 20 20 20 38 35 2c 20 20 31 35 30 2c 20 20  4,   85,  150,  
2fb2f 31 35 30 2c 20 20 20 36 35 2c 20 20 31 35 30 2c  150,   65,  150,
2fb30 20 20 31 35 30 2c 0a 20 2f 2a 20 20 31 33 30 30    150,. /*  1300
2fb31 20 2a 2f 20 20 20 31 35 30 2c 20 20 31 30 34 2c   */   150,  104,
2fb32 20 20 31 35 30 2c 20 20 20 39 34 2c 20 20 31 35    150,   94,  15
2fb33 30 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20  0,  165,  165,  
2fb34 20 39 38 2c 20 20 32 31 30 2c 20 20 31 33 39 2c   98,  210,  139,
2fb35 0a 20 2f 2a 20 20 31 33 31 30 20 2a 2f 20 20 20  . /*  1310 */   
2fb36 31 36 35 2c 20 20 31 36 35 2c 20 20 32 31 30 2c  165,  165,  210,
2fb37 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36    165,  165,  16
2fb38 35 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20  5,  150,  165,  
2fb39 31 35 30 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20  150,  165,. /*  
2fb3a 31 33 32 30 20 2a 2f 20 20 20 31 32 31 2c 20 20  1320 */   121,  
2fb3b 31 35 30 2c 20 20 31 35 30 2c 20 20 31 32 39 2c  150,  150,  129,
2fb3c 20 20 31 33 30 2c 20 20 31 33 31 2c 20 20 31 33    130,  131,  13
2fb3d 32 2c 20 20 31 33 33 2c 20 20 31 33 34 2c 20 20  2,  133,  134,  
2fb3e 32 31 30 2c 0a 20 2f 2a 20 20 31 33 33 30 20 2a  210,. /*  1330 *
2fb3f 2f 20 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20  /   150,  165,  
2fb40 31 35 30 2c 20 20 31 36 35 2c 20 20 31 35 30 2c  150,  165,  150,
2fb41 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31 36    150,  165,  16
2fb42 35 2c 20 20 31 32 39 2c 20 20 31 33 30 2c 0a 20  5,  129,  130,. 
2fb43 2f 2a 20 20 31 33 34 30 20 2a 2f 20 20 20 31 33  /*  1340 */   13
2fb44 31 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20  1,  150,  150,  
2fb45 31 35 30 2c 20 20 32 31 31 2c 20 20 31 36 35 2c  150,  211,  165,
2fb46 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31 30    150,  165,  10
2fb47 34 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 31 33  4,  165,. /*  13
2fb48 35 30 20 2a 2f 20 20 20 31 36 35 2c 20 20 20 32  50 */   165,   2
2fb49 33 2c 20 20 20 32 33 2c 20 20 20 32 35 2c 20 20  3,   23,   25,  
2fb4a 20 32 35 2c 20 20 32 31 31 2c 20 20 31 36 35 2c   25,  211,  165,
2fb4b 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 37    165,  165,  17
2fb4c 36 2c 0a 20 2f 2a 20 20 31 33 36 30 20 2a 2f 20  6,. /*  1360 */ 
2fb4d 20 20 20 32 33 2c 20 20 31 36 35 2c 20 20 20 32     23,  165,   2
2fb4e 35 2c 20 20 20 32 33 2c 20 20 20 32 33 2c 20 20  5,   23,   23,  
2fb4f 20 32 35 2c 20 20 20 32 35 2c 20 20 20 32 33 2c   25,   25,   23,
2fb50 20 20 32 31 31 2c 20 20 20 32 35 2c 0a 20 2f 2a    211,   25,. /*
2fb51 20 20 31 33 37 30 20 2a 2f 20 20 20 20 34 36 2c    1370 */    46,
2fb52 20 20 31 37 36 2c 20 20 31 38 34 2c 20 20 31 30    176,  184,  10
2fb53 33 2c 20 20 31 37 36 2c 20 20 20 32 32 2c 20 20  3,  176,   22,  
2fb54 20 39 30 2c 20 20 31 37 36 2c 20 20 31 37 38 2c   90,  176,  178,
2fb55 20 20 20 31 38 2c 0a 20 2f 2a 20 20 31 33 38 30     18,. /*  1380
2fb56 20 2a 2f 20 20 20 31 37 36 2c 20 20 31 37 39 2c   */   176,  179,
2fb57 20 20 31 37 36 2c 20 20 31 37 36 2c 20 20 31 37    176,  176,  17
2fb58 39 2c 20 20 32 33 30 2c 20 20 32 33 30 2c 20 20  9,  230,  230,  
2fb59 31 38 34 2c 20 20 31 35 37 2c 20 20 31 35 36 2c  184,  157,  156,
2fb5a 0a 20 2f 2a 20 20 31 33 39 30 20 2a 2f 20 20 20  . /*  1390 */   
2fb5b 31 35 36 2c 20 20 20 34 34 2c 20 20 31 35 37 2c  156,   44,  157,
2fb5c 20 20 31 35 36 2c 20 20 31 33 35 2c 20 20 31 35    156,  135,  15
2fb5d 37 2c 20 20 31 35 37 2c 20 20 32 33 38 2c 20 20  7,  157,  238,  
2fb5e 31 35 36 2c 20 20 32 33 39 2c 0a 20 2f 2a 20 20  156,  239,. /*  
2fb5f 31 34 30 30 20 2a 2f 20 20 20 31 35 37 2c 20 20  1400 */   157,  
2fb60 20 36 36 2c 20 20 31 38 39 2c 20 20 31 38 39 2c   66,  189,  189,
2fb61 20 20 20 32 32 2c 20 20 32 31 39 2c 20 20 31 35     22,  219,  15
2fb62 37 2c 20 20 31 39 39 2c 20 20 20 31 38 2c 20 20  7,  199,   18,  
2fb63 31 39 32 2c 0a 20 2f 2a 20 20 31 34 31 30 20 2a  192,. /*  1410 *
2fb64 2f 20 20 20 31 39 32 2c 20 20 31 39 32 2c 20 20  /   192,  192,  
2fb65 31 39 32 2c 20 20 31 38 39 2c 20 20 31 39 39 2c  192,  189,  199,
2fb66 20 20 31 35 37 2c 20 20 20 33 39 2c 20 20 32 34    157,   39,  24
2fb67 33 2c 20 20 32 34 33 2c 20 20 31 35 37 2c 0a 20  3,  243,  157,. 
2fb68 2f 2a 20 20 31 34 32 30 20 2a 2f 20 20 20 31 35  /*  1420 */   15
2fb69 37 2c 20 20 20 33 37 2c 20 20 32 34 36 2c 20 20  7,   37,  246,  
2fb6a 20 20 31 2c 20 20 31 36 34 2c 20 20 31 38 30 2c    1,  164,  180,
2fb6b 20 20 31 38 30 2c 20 20 32 34 39 2c 20 20 20 31    180,  249,   1
2fb6c 35 2c 20 20 32 31 39 2c 0a 20 2f 2a 20 20 31 34  5,  219,. /*  14
2fb6d 33 30 20 2a 2f 20 20 20 20 32 33 2c 20 20 32 35  30 */    23,  25
2fb6e 32 2c 20 20 20 32 32 2c 20 20 32 35 32 2c 20 20  2,   22,  252,  
2fb6f 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c  118,  118,  118,
2fb70 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31    118,  118,  11
2fb71 33 2c 0a 20 2f 2a 20 20 31 34 34 30 20 2a 2f 20  3,. /*  1440 */ 
2fb72 20 20 20 39 38 2c 20 20 20 32 32 2c 20 20 20 31     98,   22,   1
2fb73 31 2c 20 20 20 32 33 2c 20 20 20 32 33 2c 20 20  1,   23,   23,  
2fb74 20 32 32 2c 20 20 20 32 32 2c 20 20 31 32 30 2c   22,   22,  120,
2fb75 20 20 20 32 33 2c 20 20 20 33 34 2c 0a 20 2f 2a     23,   34,. /*
2fb76 20 20 31 34 35 30 20 2a 2f 20 20 20 20 32 33 2c    1450 */    23,
2fb77 20 20 20 32 35 2c 20 20 20 32 33 2c 20 20 20 32     25,   23,   2
2fb78 35 2c 20 20 31 31 38 2c 20 20 20 32 35 2c 20 20  5,  118,   25,  
2fb79 20 32 32 2c 20 20 31 30 32 2c 20 20 20 35 30 2c   22,  102,   50,
2fb7a 20 20 20 32 33 2c 0a 20 2f 2a 20 20 31 34 36 30     23,. /*  1460
2fb7b 20 2a 2f 20 20 20 20 32 33 2c 20 20 20 32 32 2c   */    23,   22,
2fb7c 20 20 20 32 37 2c 20 20 20 32 32 2c 20 20 20 35     27,   22,   5
2fb7d 30 2c 20 20 20 32 33 2c 20 20 20 33 34 2c 20 20  0,   23,   34,  
2fb7e 20 33 34 2c 20 20 20 32 32 2c 20 20 20 32 32 2c   34,   22,   22,
2fb7f 0a 20 2f 2a 20 20 31 34 37 30 20 2a 2f 20 20 20  . /*  1470 */   
2fb80 20 32 32 2c 20 20 31 30 39 2c 20 20 20 31 39 2c   22,  109,   19,
2fb81 20 20 20 32 34 2c 20 20 20 32 30 2c 20 20 31 30     24,   20,  10
2fb82 34 2c 20 20 20 33 38 2c 20 20 20 32 35 2c 20 20  4,   38,   25,  
2fb83 31 30 34 2c 20 20 20 32 32 2c 0a 20 2f 2a 20 20  104,   22,. /*  
2fb84 31 34 38 30 20 2a 2f 20 20 20 20 20 35 2c 20 20  1480 */     5,  
2fb85 31 33 38 2c 20 20 20 20 31 2c 20 20 31 31 38 2c  138,    1,  118,
2fb86 20 20 20 33 34 2c 20 20 20 34 32 2c 20 20 20 32     34,   42,   2
2fb87 37 2c 20 20 31 30 38 2c 20 20 20 32 32 2c 20 20  7,  108,   22,  
2fb88 31 31 39 2c 0a 20 2f 2a 20 20 31 34 39 30 20 2a  119,. /*  1490 *
2fb89 2f 20 20 20 20 35 30 2c 20 20 20 37 34 2c 20 20  /    50,   74,  
2fb8a 20 37 34 2c 20 20 31 32 37 2c 20 20 20 20 31 2c   74,  127,    1,
2fb8b 20 20 20 31 36 2c 20 20 20 35 31 2c 20 20 31 32     16,   51,  12
2fb8c 31 2c 20 20 20 32 30 2c 20 20 31 31 39 2c 0a 20  1,   20,  119,. 
2fb8d 2f 2a 20 20 31 35 30 30 20 2a 2f 20 20 20 31 30  /*  1500 */   10
2fb8e 38 2c 20 20 20 35 37 2c 20 20 20 35 31 2c 20 20  8,   57,   51,  
2fb8f 31 32 38 2c 20 20 20 32 32 2c 20 20 31 32 37 2c  128,   22,  127,
2fb90 20 20 20 32 33 2c 20 20 20 32 33 2c 20 20 20 31     23,   23,   1
2fb91 36 2c 20 20 20 31 35 2c 0a 20 2f 2a 20 20 31 35  6,   15,. /*  15
2fb92 31 30 20 2a 2f 20 20 20 20 32 32 2c 20 20 20 20  10 */    22,    
2fb93 33 2c 20 20 31 34 30 2c 20 20 20 20 34 2c 20 20  3,  140,    4,  
2fb94 32 35 33 2c 20 20 32 35 33 2c 20 20 32 35 33 2c  253,  253,  253,
2fb95 20 20 32 35 33 2c 20 20 20 36 33 2c 0a 7d 3b 0a    253,   63,.};.
2fb96 23 64 65 66 69 6e 65 20 59 59 5f 53 48 49 46 54  #define YY_SHIFT
2fb97 5f 55 53 45 5f 44 46 4c 54 20 28 2d 31 31 31 29  _USE_DFLT (-111)
2fb98 0a 23 64 65 66 69 6e 65 20 59 59 5f 53 48 49 46  .#define YY_SHIF
2fb99 54 5f 4d 41 58 20 34 31 35 0a 73 74 61 74 69 63  T_MAX 415.static
2fb9a 20 63 6f 6e 73 74 20 73 68 6f 72 74 20 79 79 5f   const short yy_
2fb9b 73 68 69 66 74 5f 6f 66 73 74 5b 5d 20 3d 20 7b  shift_ofst[] = {
2fb9c 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20 20  . /*     0 */   
2fb9d 31 38 37 2c 20 31 30 36 31 2c 20 31 31 37 30 2c  187, 1061, 1170,
2fb9e 20 31 30 36 31 2c 20 31 31 39 34 2c 20 31 31 39   1061, 1194, 119
2fb9f 34 2c 20 20 20 2d 32 2c 20 20 20 36 34 2c 20 20  4,   -2,   64,  
2fba0 20 36 34 2c 20 20 2d 31 39 2c 0a 20 2f 2a 20 20   64,  -19,. /*  
2fba1 20 20 31 30 20 2a 2f 20 20 31 31 39 34 2c 20 31    10 */  1194, 1
2fba2 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c  194, 1194, 1194,
2fba3 20 31 31 39 34 2c 20 20 32 37 36 2c 20 20 20 20   1194,  276,    
2fba4 31 2c 20 20 31 32 35 2c 20 31 30 37 36 2c 20 31  1,  125, 1076, 1
2fba5 31 39 34 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a  194,. /*    20 *
2fba6 2f 20 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31  /  1194, 1194, 1
2fba7 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c  194, 1194, 1194,
2fba8 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39   1194, 1194, 119
2fba9 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 0a 20  4, 1194, 1194,. 
2fbaa 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20 31 31 39  /*    30 */  119
2fbab 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31  4, 1194, 1194, 1
2fbac 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c  194, 1194, 1194,
2fbad 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39   1194, 1194, 119
2fbae 34 2c 20 31 31 39 34 2c 0a 20 2f 2a 20 20 20 20  4, 1194,. /*    
2fbaf 34 30 20 2a 2f 20 20 31 31 39 34 2c 20 31 31 39  40 */  1194, 119
2fbb0 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31  4, 1194, 1194, 1
2fbb1 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c  194, 1194, 1194,
2fbb2 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39   1194, 1194, 119
2fbb3 34 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20  4,. /*    50 */ 
2fbb4 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39   1194, 1194, 119
2fbb5 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31  4, 1194, 1194, 1
2fbb6 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c  194, 1194, 1194,
2fbb7 20 31 31 39 34 2c 20 20 2d 34 38 2c 0a 20 2f 2a   1194,  -48,. /*
2fbb8 20 20 20 20 36 30 20 2a 2f 20 20 20 34 30 39 2c      60 */   409,
2fbb9 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 31 34      1,    1,  14
2fbba 31 2c 20 20 33 31 38 2c 20 20 33 31 38 2c 20 2d  1,  318,  318, -
2fbbb 31 31 30 2c 20 20 20 35 33 2c 20 20 31 39 37 2c  110,   53,  197,
2fbbc 20 20 32 36 39 2c 0a 20 2f 2a 20 20 20 20 37 30    269,. /*    70
2fbbd 20 2a 2f 20 20 20 33 34 31 2c 20 20 34 31 33 2c   */   341,  413,
2fbbe 20 20 34 38 35 2c 20 20 35 35 37 2c 20 20 36 32    485,  557,  62
2fbbf 39 2c 20 20 37 30 31 2c 20 20 37 37 33 2c 20 20  9,  701,  773,  
2fbc0 38 34 35 2c 20 20 37 37 33 2c 20 20 37 37 33 2c  845,  773,  773,
2fbc1 0a 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20 20  . /*    80 */   
2fbc2 37 37 33 2c 20 20 37 37 33 2c 20 20 37 37 33 2c  773,  773,  773,
2fbc3 20 20 37 37 33 2c 20 20 37 37 33 2c 20 20 37 37    773,  773,  77
2fbc4 33 2c 20 20 37 37 33 2c 20 20 37 37 33 2c 20 20  3,  773,  773,  
2fbc5 37 37 33 2c 20 20 37 37 33 2c 0a 20 2f 2a 20 20  773,  773,. /*  
2fbc6 20 20 39 30 20 2a 2f 20 20 20 37 37 33 2c 20 20    90 */   773,  
2fbc7 37 37 33 2c 20 20 37 37 33 2c 20 20 37 37 33 2c  773,  773,  773,
2fbc8 20 20 37 37 33 2c 20 20 37 37 33 2c 20 20 39 31    773,  773,  91
2fbc9 37 2c 20 20 39 38 39 2c 20 20 39 38 39 2c 20 20  7,  989,  989,  
2fbca 2d 36 37 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a  -67,. /*   100 *
2fbcb 2f 20 20 20 2d 36 37 2c 20 20 20 2d 31 2c 20 20  /   -67,   -1,  
2fbcc 20 2d 31 2c 20 20 20 35 35 2c 20 20 20 32 35 2c   -1,   55,   25,
2fbcd 20 20 33 31 30 2c 20 20 20 20 31 2c 20 20 20 20    310,    1,    
2fbce 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 0a 20  1,    1,    1,. 
2fbcf 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20 20 20 20  /*   110 */     
2fbd0 31 2c 20 20 36 33 39 2c 20 20 33 30 34 2c 20 20  1,  639,  304,  
2fbd1 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c    1,    1,    1,
2fbd2 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20      1,    1,    
2fbd3 31 2c 20 20 20 20 31 2c 0a 20 2f 2a 20 20 20 31  1,    1,. /*   1
2fbd4 32 30 20 2a 2f 20 20 20 20 20 31 2c 20 20 20 20  20 */     1,    
2fbd5 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20  1,    1,    1,  
2fbd6 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c    1,    1,    1,
2fbd7 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 33 36      1,    1,  36
2fbd8 35 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20  5,. /*   130 */ 
2fbd9 20 20 31 34 31 2c 20 20 2d 31 37 2c 20 2d 31 31    141,  -17, -11
2fbda 31 2c 20 2d 31 31 31 2c 20 2d 31 31 31 2c 20 31  1, -111, -111, 1
2fbdb 32 30 39 2c 20 20 20 38 31 2c 20 20 34 32 34 2c  209,   81,  424,
2fbdc 20 20 33 35 33 2c 20 20 34 32 36 2c 0a 20 2f 2a    353,  426,. /*
2fbdd 20 20 20 31 34 30 20 2a 2f 20 20 20 34 34 31 2c     140 */   441,
2fbde 20 20 20 39 30 2c 20 20 35 36 35 2c 20 20 35 36     90,  565,  56
2fbdf 35 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20  5,    1,    1,  
2fbe0 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c    1,    1,    1,
2fbe1 20 20 20 20 31 2c 0a 20 2f 2a 20 20 20 31 35 30      1,. /*   150
2fbe2 20 2a 2f 20 20 20 20 20 31 2c 20 20 20 20 31 2c   */     1,    1,
2fbe3 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20      1,    1,    
2fbe4 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20  1,    1,    1,  
2fbe5 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c    1,    1,    1,
2fbe6 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20 20  . /*   160 */   
2fbe7 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c    1,    1,    1,
2fbe8 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20      1,    1,    
2fbe9 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20  1,    1,    1,  
2fbea 20 20 31 2c 20 20 20 20 31 2c 0a 20 2f 2a 20 20    1,    1,. /*  
2fbeb 20 31 37 30 20 2a 2f 20 20 20 20 20 31 2c 20 20   170 */     1,  
2fbec 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c    1,    1,    1,
2fbed 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 34 34      1,    1,  44
2fbee 37 2c 20 20 38 30 39 2c 20 20 33 32 37 2c 20 20  7,  809,  327,  
2fbef 34 31 39 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a  419,. /*   180 *
2fbf0 2f 20 20 20 34 31 39 2c 20 20 34 31 39 2c 20 20  /   419,  419,  
2fbf1 38 34 31 2c 20 20 31 30 31 2c 20 2d 31 31 30 2c  841,  101, -110,
2fbf2 20 2d 31 31 30 2c 20 2d 31 31 30 2c 20 2d 31 31   -110, -110, -11
2fbf3 31 2c 20 2d 31 31 31 2c 20 2d 31 31 31 2c 0a 20  1, -111, -111,. 
2fbf4 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20 20 32 33  /*   190 */   23
2fbf5 32 2c 20 20 32 33 32 2c 20 20 32 36 38 2c 20 20  2,  232,  268,  
2fbf6 34 32 37 2c 20 20 35 37 35 2c 20 20 36 34 35 2c  427,  575,  645,
2fbf7 20 20 37 38 38 2c 20 20 32 30 38 2c 20 20 38 36    788,  208,  86
2fbf8 31 2c 20 20 36 39 39 2c 0a 20 2f 2a 20 20 20 32  1,  699,. /*   2
2fbf9 30 30 20 2a 2f 20 20 20 38 39 37 2c 20 20 37 38  00 */   897,  78
2fbfa 34 2c 20 20 36 33 37 2c 20 20 20 35 32 2c 20 20  4,  637,   52,  
2fbfb 31 38 33 2c 20 20 31 38 33 2c 20 20 31 38 33 2c  183,  183,  183,
2fbfc 20 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39 39    902,  902,  99
2fbfd 36 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20  6,. /*   210 */ 
2fbfe 20 31 30 35 39 2c 20 20 39 30 32 2c 20 20 39 30   1059,  902,  90
2fbff 32 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20 20  2,  902,  902,  
2fc00 32 37 35 2c 20 20 36 38 39 2c 20 20 2d 31 33 2c  275,  689,  -13,
2fc01 20 20 31 34 31 2c 20 20 38 32 34 2c 0a 20 2f 2a    141,  824,. /*
2fc02 20 20 20 32 32 30 20 2a 2f 20 20 20 38 32 34 2c     220 */   824,
2fc03 20 20 34 37 38 2c 20 20 34 39 38 2c 20 20 34 39    478,  498,  49
2fc04 38 2c 20 20 36 35 36 2c 20 20 34 39 38 2c 20 20  8,  656,  498,  
2fc05 32 36 32 2c 20 20 34 39 38 2c 20 20 31 34 31 2c  262,  498,  141,
2fc06 20 20 34 39 38 2c 0a 20 2f 2a 20 20 20 32 33 30    498,. /*   230
2fc07 20 2a 2f 20 20 20 31 34 31 2c 20 20 38 36 30 2c   */   141,  860,
2fc08 20 20 37 33 37 2c 20 20 37 31 32 2c 20 20 37 33    737,  712,  73
2fc09 37 2c 20 20 36 35 36 2c 20 20 36 35 36 2c 20 20  7,  656,  656,  
2fc0a 37 31 32 2c 20 31 30 31 37 2c 20 31 30 31 37 2c  712, 1017, 1017,
2fc0b 0a 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20 31  . /*   240 */  1
2fc0c 30 31 37 2c 20 31 30 31 37 2c 20 31 30 34 30 2c  017, 1017, 1040,
2fc0d 20 31 30 34 30 2c 20 31 30 38 39 2c 20 2d 31 31   1040, 1089, -11
2fc0e 30 2c 20 31 30 32 34 2c 20 31 30 33 34 2c 20 31  0, 1024, 1034, 1
2fc0f 30 37 35 2c 20 31 30 39 33 2c 0a 20 2f 2a 20 20  075, 1093,. /*  
2fc10 20 32 35 30 20 2a 2f 20 20 31 30 37 33 2c 20 31   250 */  1073, 1
2fc11 31 31 30 2c 20 31 31 34 33 2c 20 31 31 34 33 2c  110, 1143, 1143,
2fc12 20 31 31 39 37 2c 20 31 31 39 39 2c 20 31 31 39   1197, 1199, 119
2fc13 37 2c 20 31 31 39 39 2c 20 31 31 39 37 2c 20 31  7, 1199, 1197, 1
2fc14 31 39 39 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a  199,. /*   260 *
2fc15 2f 20 20 31 32 34 34 2c 20 31 32 34 34 2c 20 31  /  1244, 1244, 1
2fc16 33 32 34 2c 20 31 32 34 34 2c 20 31 32 37 30 2c  324, 1244, 1270,
2fc17 20 31 32 34 34 2c 20 31 33 35 33 2c 20 31 32 38   1244, 1353, 128
2fc18 36 2c 20 31 32 38 36 2c 20 31 33 32 34 2c 0a 20  6, 1286, 1324,. 
2fc19 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20 31 32 34  /*   270 */  124
2fc1a 34 2c 20 31 32 34 34 2c 20 31 32 34 34 2c 20 31  4, 1244, 1244, 1
2fc1b 33 35 33 2c 20 31 33 36 31 2c 20 31 31 34 33 2c  353, 1361, 1143,
2fc1c 20 31 33 36 31 2c 20 31 31 34 33 2c 20 31 33 36   1361, 1143, 136
2fc1d 31 2c 20 31 31 34 33 2c 0a 20 2f 2a 20 20 20 32  1, 1143,. /*   2
2fc1e 38 30 20 2a 2f 20 20 31 31 34 33 2c 20 31 33 34  80 */  1143, 134
2fc1f 37 2c 20 31 32 35 39 2c 20 31 33 36 31 2c 20 31  7, 1259, 1361, 1
2fc20 31 34 33 2c 20 31 33 33 35 2c 20 31 33 33 35 2c  143, 1335, 1335,
2fc21 20 31 33 38 32 2c 20 31 30 32 34 2c 20 31 31 34   1382, 1024, 114
2fc22 33 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20  3,. /*   290 */ 
2fc23 20 31 33 39 30 2c 20 31 33 39 30 2c 20 31 33 39   1390, 1390, 139
2fc24 30 2c 20 31 33 39 30 2c 20 31 30 32 34 2c 20 31  0, 1390, 1024, 1
2fc25 33 33 35 2c 20 31 33 38 32 2c 20 31 31 34 33 2c  335, 1382, 1143,
2fc26 20 31 33 37 37 2c 20 31 33 37 37 2c 0a 20 2f 2a   1377, 1377,. /*
2fc27 20 20 20 33 30 30 20 2a 2f 20 20 31 31 34 33 2c     300 */  1143,
2fc28 20 31 31 34 33 2c 20 31 33 38 34 2c 20 2d 31 31   1143, 1384, -11
2fc29 31 2c 20 2d 31 31 31 2c 20 2d 31 31 31 2c 20 2d  1, -111, -111, -
2fc2a 31 31 31 2c 20 2d 31 31 31 2c 20 2d 31 31 31 2c  111, -111, -111,
2fc2b 20 20 35 35 32 2c 0a 20 2f 2a 20 20 20 33 31 30    552,. /*   310
2fc2c 20 2a 2f 20 20 20 37 34 39 2c 20 31 31 33 37 2c   */   749, 1137,
2fc2d 20 31 30 33 31 2c 20 31 30 38 32 2c 20 31 32 33   1031, 1082, 123
2fc2e 32 2c 20 20 38 30 31 2c 20 31 30 39 37 2c 20 31  2,  801, 1097, 1
2fc2f 31 35 33 2c 20 20 38 37 33 2c 20 31 30 31 31 2c  153,  873, 1011,
2fc30 0a 20 2f 2a 20 20 20 33 32 30 20 2a 2f 20 20 20  . /*   320 */   
2fc31 38 35 33 2c 20 31 31 36 33 2c 20 31 32 35 37 2c  853, 1163, 1257,
2fc32 20 31 33 32 38 2c 20 31 33 32 39 2c 20 31 33 33   1328, 1329, 133
2fc33 37 2c 20 31 33 34 30 2c 20 31 33 34 31 2c 20 20  7, 1340, 1341,  
2fc34 37 33 36 2c 20 31 33 34 34 2c 0a 20 2f 2a 20 20  736, 1344,. /*  
2fc35 20 33 33 30 20 2a 2f 20 20 31 34 32 32 2c 20 31   330 */  1422, 1
2fc36 34 31 33 2c 20 31 34 30 37 2c 20 31 34 31 30 2c  413, 1407, 1410,
2fc37 20 31 33 31 36 2c 20 31 33 31 37 2c 20 31 33 31   1316, 1317, 131
2fc38 38 2c 20 31 33 31 39 2c 20 31 33 32 30 2c 20 31  8, 1319, 1320, 1
2fc39 33 34 32 2c 0a 20 2f 2a 20 20 20 33 34 30 20 2a  342,. /*   340 *
2fc3a 2f 20 20 31 33 32 36 2c 20 31 34 31 39 2c 20 31  /  1326, 1419, 1
2fc3b 34 32 30 2c 20 31 34 32 31 2c 20 31 34 32 33 2c  420, 1421, 1423,
2fc3c 20 31 34 33 31 2c 20 31 34 32 34 2c 20 31 34 32   1431, 1424, 142
2fc3d 35 2c 20 31 34 32 36 2c 20 31 34 32 37 2c 0a 20  5, 1426, 1427,. 
2fc3e 2f 2a 20 20 20 33 35 30 20 2a 2f 20 20 31 34 32  /*   350 */  142
2fc3f 39 2c 20 31 34 32 38 2c 20 31 34 31 35 2c 20 31  9, 1428, 1415, 1
2fc40 34 33 30 2c 20 31 34 33 32 2c 20 31 34 32 38 2c  430, 1432, 1428,
2fc41 20 31 33 32 37 2c 20 31 34 33 34 2c 20 31 34 33   1327, 1434, 143
2fc42 33 2c 20 31 34 33 35 2c 0a 20 2f 2a 20 20 20 33  3, 1435,. /*   3
2fc43 36 30 20 2a 2f 20 20 31 33 33 36 2c 20 31 34 33  60 */  1336, 143
2fc44 36 2c 20 31 34 33 37 2c 20 31 34 33 38 2c 20 31  6, 1437, 1438, 1
2fc45 34 30 38 2c 20 31 34 33 39 2c 20 31 34 31 34 2c  408, 1439, 1414,
2fc46 20 31 34 34 31 2c 20 31 34 34 32 2c 20 31 34 34   1441, 1442, 144
2fc47 36 2c 0a 20 2f 2a 20 20 20 33 37 30 20 2a 2f 20  6,. /*   370 */ 
2fc48 20 31 34 34 37 2c 20 31 34 34 30 2c 20 31 34 34   1447, 1440, 144
2fc49 38 2c 20 31 33 35 35 2c 20 31 33 36 32 2c 20 31  8, 1355, 1362, 1
2fc4a 34 35 33 2c 20 31 34 35 34 2c 20 31 34 34 39 2c  453, 1454, 1449,
2fc4b 20 31 33 37 31 2c 20 31 34 34 33 2c 0a 20 2f 2a   1371, 1443,. /*
2fc4c 20 20 20 33 38 30 20 2a 2f 20 20 31 34 34 34 2c     380 */  1444,
2fc4d 20 31 34 34 35 2c 20 31 34 35 32 2c 20 31 34 35   1445, 1452, 145
2fc4e 31 2c 20 31 33 34 33 2c 20 31 33 37 34 2c 20 31  1, 1343, 1374, 1
2fc4f 34 35 37 2c 20 31 34 37 35 2c 20 31 34 38 31 2c  457, 1475, 1481,
2fc50 20 31 33 36 35 2c 0a 20 2f 2a 20 20 20 33 39 30   1365,. /*   390
2fc51 20 2a 2f 20 20 31 34 35 30 2c 20 31 34 35 39 2c   */  1450, 1459,
2fc52 20 31 33 37 39 2c 20 31 34 31 37 2c 20 31 34 31   1379, 1417, 141
2fc53 38 2c 20 31 33 36 36 2c 20 31 34 36 36 2c 20 31  8, 1366, 1466, 1
2fc54 33 37 30 2c 20 31 34 39 33 2c 20 31 34 37 39 2c  370, 1493, 1479,
2fc55 0a 20 2f 2a 20 20 20 34 30 30 20 2a 2f 20 20 31  . /*   400 */  1
2fc56 33 37 36 2c 20 31 34 37 38 2c 20 31 33 38 30 2c  376, 1478, 1380,
2fc57 20 31 33 39 32 2c 20 31 33 37 38 2c 20 31 34 38   1392, 1378, 148
2fc58 32 2c 20 31 33 37 35 2c 20 31 34 38 33 2c 20 31  2, 1375, 1483, 1
2fc59 34 38 34 2c 20 31 34 39 32 2c 0a 20 2f 2a 20 20  484, 1492,. /*  
2fc5a 20 34 31 30 20 2a 2f 20 20 31 34 35 35 2c 20 31   410 */  1455, 1
2fc5b 34 39 34 2c 20 31 33 37 32 2c 20 31 34 38 38 2c  494, 1372, 1488,
2fc5c 20 31 35 30 38 2c 20 31 35 30 39 2c 0a 7d 3b 0a   1508, 1509,.};.
2fc5d 23 64 65 66 69 6e 65 20 59 59 5f 52 45 44 55 43  #define YY_REDUC
2fc5e 45 5f 55 53 45 5f 44 46 4c 54 20 28 2d 31 38 30  E_USE_DFLT (-180
2fc5f 29 0a 23 64 65 66 69 6e 65 20 59 59 5f 52 45 44  ).#define YY_RED
2fc60 55 43 45 5f 4d 41 58 20 33 30 38 0a 73 74 61 74  UCE_MAX 308.stat
2fc61 69 63 20 63 6f 6e 73 74 20 73 68 6f 72 74 20 79  ic const short y
2fc62 79 5f 72 65 64 75 63 65 5f 6f 66 73 74 5b 5d 20  y_reduce_ofst[] 
2fc63 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f  = {. /*     0 */
2fc64 20 20 2d 31 34 31 2c 20 20 20 38 32 2c 20 20 31    -141,   82,  1
2fc65 35 34 2c 20 20 32 38 34 2c 20 20 20 31 32 2c 20  54,  284,   12, 
2fc66 20 20 37 35 2c 20 20 20 36 39 2c 20 20 20 37 33    75,   69,   73
2fc67 2c 20 20 31 34 32 2c 20 20 2d 35 39 2c 0a 20 2f  ,  142,  -59,. /
2fc68 2a 20 20 20 20 31 30 20 2a 2f 20 20 20 31 34 35  *    10 */   145
2fc69 2c 20 20 20 38 37 2c 20 20 31 35 39 2c 20 20 32  ,   87,  159,  2
2fc6a 32 30 2c 20 20 32 39 31 2c 20 20 33 34 36 2c 20  20,  291,  346, 
2fc6b 20 32 32 36 2c 20 20 32 31 33 2c 20 20 33 35 37   226,  213,  357
2fc6c 2c 20 20 33 37 34 2c 0a 20 2f 2a 20 20 20 20 32  ,  374,. /*    2
2fc6d 30 20 2a 2f 20 20 20 34 32 39 2c 20 20 34 33 37  0 */   429,  437
2fc6e 2c 20 20 34 34 32 2c 20 20 34 38 36 2c 20 20 34  ,  442,  486,  4
2fc6f 39 39 2c 20 20 35 30 35 2c 20 20 35 30 37 2c 20  99,  505,  507, 
2fc70 20 35 31 39 2c 20 20 35 35 38 2c 20 20 35 37 31   519,  558,  571
2fc71 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20  ,. /*    30 */  
2fc72 20 35 37 37 2c 20 20 35 38 38 2c 20 20 36 33 30   577,  588,  630
2fc73 2c 20 20 36 34 33 2c 20 20 36 34 39 2c 20 20 36  ,  643,  649,  6
2fc74 35 31 2c 20 20 36 36 32 2c 20 20 37 30 32 2c 20  51,  662,  702, 
2fc75 20 37 31 35 2c 20 20 37 32 31 2c 0a 20 2f 2a 20   715,  721,. /* 
2fc76 20 20 20 34 30 20 2a 2f 20 20 20 37 33 33 2c 20     40 */   733, 
2fc77 20 37 37 34 2c 20 20 37 38 37 2c 20 20 37 39 33   774,  787,  793
2fc78 2c 20 20 38 30 35 2c 20 20 38 34 36 2c 20 20 38  ,  805,  846,  8
2fc79 35 39 2c 20 20 38 36 35 2c 20 20 38 37 37 2c 20  59,  865,  877, 
2fc7a 20 38 38 31 2c 0a 20 2f 2a 20 20 20 20 35 30 20   881,. /*    50 
2fc7b 2a 2f 20 20 20 39 33 34 2c 20 20 39 33 36 2c 20  */   934,  936, 
2fc7c 20 39 36 33 2c 20 20 39 36 37 2c 20 20 39 36 39   963,  967,  969
2fc7d 2c 20 20 39 39 38 2c 20 31 30 35 33 2c 20 31 30  ,  998, 1053, 10
2fc7e 37 32 2c 20 31 30 38 38 2c 20 2d 31 37 39 2c 0a  72, 1088, -179,.
2fc7f 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20 38   /*    60 */   8
2fc80 35 30 2c 20 20 39 35 36 2c 20 20 33 38 30 2c 20  50,  956,  380, 
2fc81 20 33 30 38 2c 20 20 20 38 39 2c 20 20 34 39 36   308,   89,  496
2fc82 2c 20 20 33 38 34 2c 20 20 20 20 32 2c 20 20 20  ,  384,    2,   
2fc83 20 32 2c 20 20 20 20 32 2c 0a 20 2f 2a 20 20 20   2,    2,. /*   
2fc84 20 37 30 20 2a 2f 20 20 20 20 20 32 2c 20 20 20   70 */     2,   
2fc85 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20   2,    2,    2, 
2fc86 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32     2,    2,    2
2fc87 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20  ,    2,    2,   
2fc88 20 32 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a 2f   2,. /*    80 */
2fc89 20 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20       2,    2,   
2fc8a 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20   2,    2,    2, 
2fc8b 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32     2,    2,    2
2fc8c 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 0a 20 2f  ,    2,    2,. /
2fc8d 2a 20 20 20 20 39 30 20 2a 2f 20 20 20 20 20 32  *    90 */     2
2fc8e 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20  ,    2,    2,   
2fc8f 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20   2,    2,    2, 
2fc90 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32     2,    2,    2
2fc91 2c 20 20 20 20 32 2c 0a 20 2f 2a 20 20 20 31 30  ,    2,. /*   10
2fc92 30 20 2a 2f 20 20 20 20 20 32 2c 20 20 20 20 32  0 */     2,    2
2fc93 2c 20 20 20 20 32 2c 20 20 34 31 36 2c 20 20 20  ,    2,  416,   
2fc94 20 32 2c 20 20 20 20 32 2c 20 20 34 34 39 2c 20   2,    2,  449, 
2fc95 20 35 37 39 2c 20 20 36 34 38 2c 20 20 37 32 33   579,  648,  723
2fc96 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20  ,. /*   110 */  
2fc97 20 37 39 31 2c 20 20 31 33 34 2c 20 20 35 30 31   791,  134,  501
2fc98 2c 20 20 37 31 36 2c 20 20 35 32 31 2c 20 20 37  ,  716,  521,  7
2fc99 39 34 2c 20 20 35 38 39 2c 20 20 2d 34 37 2c 20  94,  589,  -47, 
2fc9a 20 36 35 30 2c 20 20 35 39 30 2c 0a 20 2f 2a 20   650,  590,. /* 
2fc9b 20 20 31 32 30 20 2a 2f 20 20 20 37 39 35 2c 20    120 */   795, 
2fc9c 20 39 34 32 2c 20 20 39 37 34 2c 20 20 39 38 36   942,  974,  986
2fc9d 2c 20 31 30 30 33 2c 20 31 30 34 37 2c 20 31 30  , 1003, 1047, 10
2fc9e 37 34 2c 20 20 39 33 35 2c 20 31 30 39 31 2c 20  74,  935, 1091, 
2fc9f 20 20 20 32 2c 0a 20 2f 2a 20 20 20 31 33 30 20     2,. /*   130 
2fca0 2a 2f 20 20 20 34 31 37 2c 20 20 20 20 32 2c 20  */   417,    2, 
2fca1 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32     2,    2,    2
2fca2 2c 20 20 31 35 38 2c 20 20 33 33 36 2c 20 20 35  ,  158,  336,  5
2fca3 32 36 2c 20 20 35 37 36 2c 20 20 38 36 33 2c 0a  26,  576,  863,.
2fca4 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20 38   /*   140 */   8
2fca5 38 35 2c 20 20 39 36 36 2c 20 20 34 30 35 2c 20  85,  966,  405, 
2fca6 20 34 32 38 2c 20 20 39 36 38 2c 20 31 30 33 39   428,  968, 1039
2fca7 2c 20 31 30 36 39 2c 20 31 30 39 39 2c 20 31 31  , 1069, 1099, 11
2fca8 30 30 2c 20 20 39 36 36 2c 0a 20 2f 2a 20 20 20  00,  966,. /*   
2fca9 31 35 30 20 2a 2f 20 20 31 31 30 31 2c 20 31 31  150 */  1101, 11
2fcaa 30 37 2c 20 31 31 30 39 2c 20 31 31 31 38 2c 20  07, 1109, 1118, 
2fcab 31 31 32 30 2c 20 31 31 34 30 2c 20 31 31 34 31  1120, 1140, 1141
2fcac 2c 20 31 31 34 35 2c 20 31 31 34 36 2c 20 31 31  , 1145, 1146, 11
2fcad 34 38 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f  48,. /*   160 */
2fcae 20 20 31 31 34 39 2c 20 31 31 35 30 2c 20 31 31    1149, 1150, 11
2fcaf 35 32 2c 20 31 31 35 34 2c 20 31 31 36 36 2c 20  52, 1154, 1166, 
2fcb0 31 31 36 38 2c 20 31 31 37 31 2c 20 31 31 37 32  1168, 1171, 1172
2fcb1 2c 20 31 31 38 30 2c 20 31 31 38 32 2c 0a 20 2f  , 1180, 1182,. /
2fcb2 2a 20 20 20 31 37 30 20 2a 2f 20 20 31 31 38 34  *   170 */  1184
2fcb3 2c 20 31 31 38 35 2c 20 31 31 39 31 2c 20 31 31  , 1185, 1191, 11
2fcb4 39 32 2c 20 31 31 39 33 2c 20 31 31 39 36 2c 20  92, 1193, 1196, 
2fcb5 20 34 30 33 2c 20 20 34 30 33 2c 20 20 36 35 32   403,  403,  652
2fcb6 2c 20 20 33 37 37 2c 0a 20 2f 2a 20 20 20 31 38  ,  377,. /*   18
2fcb7 30 20 2a 2f 20 20 20 36 36 33 2c 20 20 36 36 37  0 */   663,  667
2fcb8 2c 20 2d 31 33 34 2c 20 20 37 38 30 2c 20 20 38  , -134,  780,  8
2fcb9 38 38 2c 20 20 39 33 33 2c 20 31 30 36 36 2c 20  88,  933, 1066, 
2fcba 20 20 34 34 2c 20 20 36 37 32 2c 20 20 36 39 38    44,  672,  698
2fcbb 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20  ,. /*   190 */  
2fcbc 20 2d 39 38 2c 20 20 2d 37 30 2c 20 20 2d 35 34   -98,  -70,  -54
2fcbd 2c 20 20 2d 33 36 2c 20 20 2d 33 35 2c 20 20 2d  ,  -36,  -35,  -
2fcbe 33 35 2c 20 20 2d 33 35 2c 20 20 20 31 33 2c 20  35,  -35,   13, 
2fcbf 20 2d 33 35 2c 20 20 20 31 34 2c 0a 20 2f 2a 20   -35,   14,. /* 
2fcc0 20 20 32 30 30 20 2a 2f 20 20 20 31 34 36 2c 20    200 */   146, 
2fcc1 20 31 38 31 2c 20 20 32 32 37 2c 20 20 20 31 34   181,  227,   14
2fcc2 2c 20 20 32 30 33 2c 20 20 32 32 33 2c 20 20 32  ,  203,  223,  2
2fcc3 35 30 2c 20 20 2d 33 35 2c 20 20 2d 33 35 2c 20  50,  -35,  -35, 
2fcc4 20 32 32 34 2c 0a 20 2f 2a 20 20 20 32 31 30 20   224,. /*   210 
2fcc5 2a 2f 20 20 20 32 30 32 2c 20 20 2d 33 35 2c 20  */   202,  -35, 
2fcc6 20 2d 33 35 2c 20 20 2d 33 35 2c 20 20 2d 33 35   -35,  -35,  -35
2fcc7 2c 20 20 33 33 39 2c 20 20 33 30 39 2c 20 20 33  ,  339,  309,  3
2fcc8 31 32 2c 20 20 33 38 31 2c 20 20 33 31 37 2c 0a  12,  381,  317,.
2fcc9 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 20 33   /*   220 */   3
2fcca 37 36 2c 20 20 34 35 37 2c 20 20 35 31 35 2c 20  76,  457,  515, 
2fccb 20 35 37 30 2c 20 20 36 31 39 2c 20 20 35 38 34   570,  619,  584
2fccc 2c 20 20 36 38 37 2c 20 20 37 30 35 2c 20 20 37  ,  687,  705,  7
2fccd 30 39 2c 20 20 37 36 35 2c 0a 20 2f 2a 20 20 20  09,  765,. /*   
2fcce 32 33 30 20 2a 2f 20 20 20 37 32 36 2c 20 20 37  230 */   726,  7
2fccf 38 36 2c 20 20 37 33 30 2c 20 20 37 37 38 2c 20  86,  730,  778, 
2fcd0 20 37 34 31 2c 20 20 38 30 33 2c 20 20 38 31 33   741,  803,  813
2fcd1 2c 20 20 38 32 37 2c 20 20 38 38 33 2c 20 20 38  ,  827,  883,  8
2fcd2 39 32 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a 2f  92,. /*   240 */
2fcd3 20 20 20 38 39 36 2c 20 20 39 30 30 2c 20 20 39     896,  900,  9
2fcd4 30 33 2c 20 20 39 34 33 2c 20 20 39 36 34 2c 20  03,  943,  964, 
2fcd5 20 39 33 32 2c 20 20 39 33 30 2c 20 20 39 35 38   932,  930,  958
2fcd6 2c 20 20 39 38 34 2c 20 31 30 31 35 2c 0a 20 2f  ,  984, 1015,. /
2fcd7 2a 20 20 20 32 35 30 20 2a 2f 20 20 31 30 33 30  *   250 */  1030
2fcd8 2c 20 31 30 32 35 2c 20 31 31 31 32 2c 20 31 31  , 1025, 1112, 11
2fcd9 31 34 2c 20 31 30 39 38 2c 20 31 31 33 33 2c 20  14, 1098, 1133, 
2fcda 31 31 30 32 2c 20 31 31 34 34 2c 20 31 31 31 39  1102, 1144, 1119
2fcdb 2c 20 31 31 35 37 2c 0a 20 2f 2a 20 20 20 32 36  , 1157,. /*   26
2fcdc 30 20 2a 2f 20 20 31 31 38 33 2c 20 31 31 39 35  0 */  1183, 1195
2fcdd 2c 20 31 31 38 38 2c 20 31 31 39 38 2c 20 31 32  , 1188, 1198, 12
2fcde 30 30 2c 20 31 32 30 31 2c 20 31 32 30 32 2c 20  00, 1201, 1202, 
2fcdf 31 31 35 35 2c 20 31 31 35 36 2c 20 31 32 30 33  1155, 1156, 1203
2fce0 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20  ,. /*   270 */  
2fce1 31 32 30 34 2c 20 31 32 30 36 2c 20 31 32 30 37  1204, 1206, 1207
2fce2 2c 20 31 32 30 35 2c 20 31 32 33 33 2c 20 31 32  , 1205, 1233, 12
2fce3 33 31 2c 20 31 32 33 34 2c 20 31 32 33 35 2c 20  31, 1234, 1235, 
2fce4 31 32 33 37 2c 20 31 32 33 38 2c 0a 20 2f 2a 20  1237, 1238,. /* 
2fce5 20 20 32 38 30 20 2a 2f 20 20 31 32 33 39 2c 20    280 */  1239, 
2fce6 31 31 35 39 2c 20 31 31 36 30 2c 20 31 32 34 32  1159, 1160, 1242
2fce7 2c 20 31 32 34 33 2c 20 31 32 31 33 2c 20 31 32  , 1243, 1213, 12
2fce8 31 34 2c 20 31 31 38 36 2c 20 31 32 30 38 2c 20  14, 1186, 1208, 
2fce9 31 32 34 39 2c 0a 20 2f 2a 20 20 20 32 39 30 20  1249,. /*   290 
2fcea 2a 2f 20 20 31 32 31 37 2c 20 31 32 31 38 2c 20  */  1217, 1218, 
2fceb 31 32 31 39 2c 20 31 32 32 30 2c 20 31 32 31 35  1219, 1220, 1215
2fcec 2c 20 31 32 32 34 2c 20 31 32 31 30 2c 20 31 32  , 1224, 1210, 12
2fced 35 38 2c 20 31 31 37 34 2c 20 31 31 37 35 2c 0a  58, 1174, 1175,.
2fcee 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20 31 32   /*   300 */  12
2fcef 36 32 2c 20 31 32 36 33 2c 20 31 31 37 36 2c 20  62, 1263, 1176, 
2fcf0 31 32 36 30 2c 20 31 32 34 35 2c 20 31 32 34 36  1260, 1245, 1246
2fcf1 2c 20 31 31 37 38 2c 20 31 31 37 39 2c 20 31 31  , 1178, 1179, 11
2fcf2 38 31 2c 0a 7d 3b 0a 73 74 61 74 69 63 20 63 6f  81,.};.static co
2fcf3 6e 73 74 20 59 59 41 43 54 49 4f 4e 54 59 50 45  nst YYACTIONTYPE
2fcf4 20 79 79 5f 64 65 66 61 75 6c 74 5b 5d 20 3d 20   yy_default[] = 
2fcf5 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20  {. /*     0 */  
2fcf6 20 36 33 34 2c 20 20 38 36 39 2c 20 20 39 35 38   634,  869,  958
2fcf7 2c 20 20 39 35 38 2c 20 20 38 36 39 2c 20 20 39  ,  958,  869,  9
2fcf8 35 38 2c 20 20 39 35 38 2c 20 20 38 39 38 2c 20  58,  958,  898, 
2fcf9 20 38 39 38 2c 20 20 37 35 37 2c 0a 20 2f 2a 20   898,  757,. /* 
2fcfa 20 20 20 31 30 20 2a 2f 20 20 20 38 36 37 2c 20     10 */   867, 
2fcfb 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fcfc 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fcfd 35 38 2c 20 20 39 33 32 2c 20 20 39 35 38 2c 20  58,  932,  958, 
2fcfe 20 39 35 38 2c 0a 20 2f 2a 20 20 20 20 32 30 20   958,. /*    20 
2fcff 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20  */   958,  958, 
2fd00 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fd01 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fd02 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a  58,  958,  958,.
2fd03 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20 20 39   /*    30 */   9
2fd04 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fd05 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fd06 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fd07 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20  58,  958,. /*   
2fd08 20 34 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39   40 */   958,  9
2fd09 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fd0a 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fd0b 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fd0c 35 38 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f  58,. /*    50 */
2fd0d 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39     958,  958,  9
2fd0e 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fd0f 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fd10 2c 20 20 39 35 38 2c 20 20 38 34 31 2c 0a 20 2f  ,  958,  841,. /
2fd11 2a 20 20 20 20 36 30 20 2a 2f 20 20 20 39 35 38  *    60 */   958
2fd12 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 36  ,  958,  958,  6
2fd13 37 33 2c 20 20 38 39 38 2c 20 20 38 39 38 2c 20  73,  898,  898, 
2fd14 20 37 36 31 2c 20 20 37 39 32 2c 20 20 39 35 38   761,  792,  958
2fd15 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 20 37  ,  958,. /*    7
2fd16 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38  0 */   958,  958
2fd17 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fd18 35 38 2c 20 20 39 35 38 2c 20 20 37 39 33 2c 20  58,  958,  793, 
2fd19 20 39 35 38 2c 20 20 38 37 31 2c 20 20 38 36 36   958,  871,  866
2fd1a 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20  ,. /*    80 */  
2fd1b 20 38 36 32 2c 20 20 38 36 34 2c 20 20 38 36 33   862,  864,  863
2fd1c 2c 20 20 38 37 30 2c 20 20 37 39 34 2c 20 20 37  ,  870,  794,  7
2fd1d 38 33 2c 20 20 37 39 30 2c 20 20 37 39 37 2c 20  83,  790,  797, 
2fd1e 20 37 37 32 2c 20 20 39 31 31 2c 0a 20 2f 2a 20   772,  911,. /* 
2fd1f 20 20 20 39 30 20 2a 2f 20 20 20 37 39 39 2c 20     90 */   799, 
2fd20 20 38 30 30 2c 20 20 38 30 36 2c 20 20 38 30 37   800,  806,  807
2fd21 2c 20 20 39 33 33 2c 20 20 39 33 31 2c 20 20 38  ,  933,  931,  8
2fd22 32 39 2c 20 20 38 32 38 2c 20 20 38 34 37 2c 20  29,  828,  847, 
2fd23 20 38 33 31 2c 0a 20 2f 2a 20 20 20 31 30 30 20   831,. /*   100 
2fd24 2a 2f 20 20 20 38 35 33 2c 20 20 38 33 30 2c 20  */   853,  830, 
2fd25 20 38 34 30 2c 20 20 36 36 35 2c 20 20 38 33 32   840,  665,  832
2fd26 2c 20 20 38 33 33 2c 20 20 39 35 38 2c 20 20 39  ,  833,  958,  9
2fd27 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a  58,  958,  958,.
2fd28 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20 20 39   /*   110 */   9
2fd29 35 38 2c 20 20 37 32 36 2c 20 20 36 36 30 2c 20  58,  726,  660, 
2fd2a 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fd2b 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fd2c 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20  58,  958,. /*   
2fd2d 31 32 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39  120 */   958,  9
2fd2e 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fd2f 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fd30 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 38  ,  958,  958,  8
2fd31 33 34 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f  34,. /*   130 */
2fd32 20 20 20 39 35 38 2c 20 20 38 33 35 2c 20 20 38     958,  835,  8
2fd33 34 38 2c 20 20 38 34 39 2c 20 20 38 35 30 2c 20  48,  849,  850, 
2fd34 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fd35 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f  ,  958,  958,. /
2fd36 2a 20 20 20 31 34 30 20 2a 2f 20 20 20 39 35 38  *   140 */   958
2fd37 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fd38 35 38 2c 20 20 36 34 30 2c 20 20 39 35 38 2c 20  58,  640,  958, 
2fd39 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fd3a 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 31 35  ,  958,. /*   15
2fd3b 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38  0 */   958,  958
2fd3c 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fd3d 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fd3e 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fd3f 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20  ,. /*   160 */  
2fd40 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fd41 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fd42 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fd43 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20   958,  958,. /* 
2fd44 20 20 31 37 30 20 2a 2f 20 20 20 39 35 38 2c 20    170 */   958, 
2fd45 20 38 38 32 2c 20 20 39 35 38 2c 20 20 39 33 36   882,  958,  936
2fd46 2c 20 20 39 33 38 2c 20 20 39 35 38 2c 20 20 39  ,  938,  958,  9
2fd47 35 38 2c 20 20 39 35 38 2c 20 20 36 33 34 2c 20  58,  958,  634, 
2fd48 20 37 35 37 2c 0a 20 2f 2a 20 20 20 31 38 30 20   757,. /*   180 
2fd49 2a 2f 20 20 20 37 35 37 2c 20 20 37 35 37 2c 20  */   757,  757, 
2fd4a 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fd4b 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 37  ,  958,  958,  7
2fd4c 35 31 2c 20 20 37 36 31 2c 20 20 39 35 30 2c 0a  51,  761,  950,.
2fd4d 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20 20 39   /*   190 */   9
2fd4e 35 38 2c 20 20 39 35 38 2c 20 20 37 31 37 2c 20  58,  958,  717, 
2fd4f 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fd50 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fd51 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20  58,  958,. /*   
2fd52 32 30 30 20 2a 2f 20 20 20 36 34 32 2c 20 20 37  200 */   642,  7
2fd53 34 39 2c 20 20 36 37 35 2c 20 20 37 35 39 2c 20  49,  675,  759, 
2fd54 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fd55 2c 20 20 36 36 32 2c 20 20 37 33 38 2c 20 20 39  ,  662,  738,  9
2fd56 30 34 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f  04,. /*   210 */
2fd57 20 20 20 39 35 38 2c 20 20 39 32 33 2c 20 20 39     958,  923,  9
2fd58 32 31 2c 20 20 37 34 30 2c 20 20 38 30 32 2c 20  21,  740,  802, 
2fd59 20 39 35 38 2c 20 20 37 34 39 2c 20 20 37 35 38   958,  749,  758
2fd5a 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f  ,  958,  958,. /
2fd5b 2a 20 20 20 32 32 30 20 2a 2f 20 20 20 39 35 38  *   220 */   958
2fd5c 2c 20 20 38 36 35 2c 20 20 37 38 36 2c 20 20 37  ,  865,  786,  7
2fd5d 38 36 2c 20 20 37 37 34 2c 20 20 37 38 36 2c 20  86,  774,  786, 
2fd5e 20 36 39 36 2c 20 20 37 38 36 2c 20 20 39 35 38   696,  786,  958
2fd5f 2c 20 20 37 38 36 2c 0a 20 2f 2a 20 20 20 32 33  ,  786,. /*   23
2fd60 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 36 39 39  0 */   958,  699
2fd61 2c 20 20 39 31 36 2c 20 20 37 39 36 2c 20 20 39  ,  916,  796,  9
2fd62 31 36 2c 20 20 37 37 34 2c 20 20 37 37 34 2c 20  16,  774,  774, 
2fd63 20 37 39 36 2c 20 20 36 33 39 2c 20 20 36 33 39   796,  639,  639
2fd64 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20  ,. /*   240 */  
2fd65 20 36 33 39 2c 20 20 36 33 39 2c 20 20 36 35 30   639,  639,  650
2fd66 2c 20 20 36 35 30 2c 20 20 37 31 36 2c 20 20 39  ,  650,  716,  9
2fd67 35 38 2c 20 20 37 39 36 2c 20 20 37 38 37 2c 20  58,  796,  787, 
2fd68 20 37 38 39 2c 20 20 37 37 39 2c 0a 20 2f 2a 20   789,  779,. /* 
2fd69 20 20 32 35 30 20 2a 2f 20 20 20 37 39 31 2c 20    250 */   791, 
2fd6a 20 39 35 38 2c 20 20 37 36 35 2c 20 20 37 36 35   958,  765,  765
2fd6b 2c 20 20 37 37 33 2c 20 20 37 37 38 2c 20 20 37  ,  773,  778,  7
2fd6c 37 33 2c 20 20 37 37 38 2c 20 20 37 37 33 2c 20  73,  778,  773, 
2fd6d 20 37 37 38 2c 0a 20 2f 2a 20 20 20 32 36 30 20   778,. /*   260 
2fd6e 2a 2f 20 20 20 37 32 38 2c 20 20 37 32 38 2c 20  */   728,  728, 
2fd6f 20 37 31 33 2c 20 20 37 32 38 2c 20 20 36 39 39   713,  728,  699
2fd70 2c 20 20 37 32 38 2c 20 20 38 37 35 2c 20 20 38  ,  728,  875,  8
2fd71 37 39 2c 20 20 38 37 39 2c 20 20 37 31 33 2c 0a  79,  879,  713,.
2fd72 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20 20 37   /*   270 */   7
2fd73 32 38 2c 20 20 37 32 38 2c 20 20 37 32 38 2c 20  28,  728,  728, 
2fd74 20 38 37 35 2c 20 20 36 35 37 2c 20 20 37 36 35   875,  657,  765
2fd75 2c 20 20 36 35 37 2c 20 20 37 36 35 2c 20 20 36  ,  657,  765,  6
2fd76 35 37 2c 20 20 37 36 35 2c 0a 20 2f 2a 20 20 20  57,  765,. /*   
2fd77 32 38 30 20 2a 2f 20 20 20 37 36 35 2c 20 20 39  280 */   765,  9
2fd78 30 38 2c 20 20 39 31 30 2c 20 20 36 35 37 2c 20  08,  910,  657, 
2fd79 20 37 36 35 2c 20 20 37 33 30 2c 20 20 37 33 30   765,  730,  730
2fd7a 2c 20 20 38 30 38 2c 20 20 37 39 36 2c 20 20 37  ,  808,  796,  7
2fd7b 36 35 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f  65,. /*   290 */
2fd7c 20 20 20 37 33 37 2c 20 20 37 33 37 2c 20 20 37     737,  737,  7
2fd7d 33 37 2c 20 20 37 33 37 2c 20 20 37 39 36 2c 20  37,  737,  796, 
2fd7e 20 37 33 30 2c 20 20 38 30 38 2c 20 20 37 36 35   730,  808,  765
2fd7f 2c 20 20 39 33 35 2c 20 20 39 33 35 2c 0a 20 2f  ,  935,  935,. /
2fd80 2a 20 20 20 33 30 30 20 2a 2f 20 20 20 37 36 35  *   300 */   765
2fd81 2c 20 20 37 36 35 2c 20 20 39 34 33 2c 20 20 36  ,  765,  943,  6
2fd82 38 33 2c 20 20 37 30 31 2c 20 20 37 30 31 2c 20  83,  701,  701, 
2fd83 20 39 35 30 2c 20 20 39 35 35 2c 20 20 39 35 35   950,  955,  955
2fd84 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 33 31  ,  958,. /*   31
2fd85 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38  0 */   958,  958
2fd86 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fd87 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fd88 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fd89 2c 0a 20 2f 2a 20 20 20 33 32 30 20 2a 2f 20 20  ,. /*   320 */  
2fd8a 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fd8b 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fd8c 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fd8d 20 38 38 34 2c 20 20 39 35 38 2c 0a 20 2f 2a 20   884,  958,. /* 
2fd8e 20 20 33 33 30 20 2a 2f 20 20 20 39 35 38 2c 20    330 */   958, 
2fd8f 20 36 34 38 2c 20 20 39 35 38 2c 20 20 36 36 37   648,  958,  667
2fd90 2c 20 20 38 31 35 2c 20 20 38 32 30 2c 20 20 38  ,  815,  820,  8
2fd91 31 36 2c 20 20 39 35 38 2c 20 20 38 31 37 2c 20  16,  958,  817, 
2fd92 20 39 35 38 2c 0a 20 2f 2a 20 20 20 33 34 30 20   958,. /*   340 
2fd93 2a 2f 20 20 20 37 34 33 2c 20 20 39 35 38 2c 20  */   743,  958, 
2fd94 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fd95 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fd96 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a  58,  958,  958,.
2fd97 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20 20 20 39   /*   350 */   9
2fd98 35 38 2c 20 20 38 36 38 2c 20 20 39 35 38 2c 20  58,  868,  958, 
2fd99 20 37 38 30 2c 20 20 39 35 38 2c 20 20 37 38 38   780,  958,  788
2fd9a 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fd9b 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20  58,  958,. /*   
2fd9c 33 36 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39  360 */   958,  9
2fd9d 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fd9e 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fd9f 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fda0 35 38 2c 0a 20 2f 2a 20 20 20 33 37 30 20 2a 2f  58,. /*   370 */
2fda1 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39     958,  958,  9
2fda2 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fda3 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fda4 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f  ,  958,  958,. /
2fda5 2a 20 20 20 33 38 30 20 2a 2f 20 20 20 39 35 38  *   380 */   958
2fda6 2c 20 20 39 30 36 2c 20 20 39 30 37 2c 20 20 39  ,  906,  907,  9
2fda7 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fda8 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fda9 2c 20 20 39 31 34 2c 0a 20 2f 2a 20 20 20 33 39  ,  914,. /*   39
2fdaa 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38  0 */   958,  958
2fdab 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fdac 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fdad 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fdae 2c 0a 20 2f 2a 20 20 20 34 30 30 20 2a 2f 20 20  ,. /*   400 */  
2fdaf 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fdb0 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fdb1 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fdb2 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20   958,  958,. /* 
2fdb3 20 20 34 31 30 20 2a 2f 20 20 20 39 34 32 2c 20    410 */   942, 
2fdb4 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 34 35   958,  958,  945
2fdb5 2c 20 20 36 33 35 2c 20 20 39 35 38 2c 20 20 36  ,  635,  958,  6
2fdb6 33 30 2c 20 20 36 33 32 2c 20 20 36 33 33 2c 20  30,  632,  633, 
2fdb7 20 36 33 37 2c 0a 20 2f 2a 20 20 20 34 32 30 20   637,. /*   420 
2fdb8 2a 2f 20 20 20 36 33 38 2c 20 20 36 34 31 2c 20  */   638,  641, 
2fdb9 20 36 36 37 2c 20 20 36 36 38 2c 20 20 36 37 30   667,  668,  670
2fdba 2c 20 20 36 37 31 2c 20 20 36 37 32 2c 20 20 36  ,  671,  672,  6
2fdbb 34 33 2c 20 20 36 34 34 2c 20 20 36 34 35 2c 0a  43,  644,  645,.
2fdbc 20 2f 2a 20 20 20 34 33 30 20 2a 2f 20 20 20 36   /*   430 */   6
2fdbd 34 36 2c 20 20 36 34 37 2c 20 20 36 34 39 2c 20  46,  647,  649, 
2fdbe 20 36 35 33 2c 20 20 36 35 31 2c 20 20 36 35 32   653,  651,  652
2fdbf 2c 20 20 36 35 34 2c 20 20 36 36 31 2c 20 20 36  ,  654,  661,  6
2fdc0 36 33 2c 20 20 36 38 32 2c 0a 20 2f 2a 20 20 20  63,  682,. /*   
2fdc1 34 34 30 20 2a 2f 20 20 20 36 38 34 2c 20 20 36  440 */   684,  6
2fdc2 38 36 2c 20 20 37 34 37 2c 20 20 37 34 38 2c 20  86,  747,  748, 
2fdc3 20 38 31 32 2c 20 20 37 34 31 2c 20 20 37 34 32   812,  741,  742
2fdc4 2c 20 20 37 34 36 2c 20 20 36 36 39 2c 20 20 38  ,  746,  669,  8
2fdc5 32 33 2c 0a 20 2f 2a 20 20 20 34 35 30 20 2a 2f  23,. /*   450 */
2fdc6 20 20 20 38 31 34 2c 20 20 38 31 38 2c 20 20 38     814,  818,  8
2fdc7 31 39 2c 20 20 38 32 31 2c 20 20 38 32 32 2c 20  19,  821,  822, 
2fdc8 20 38 33 36 2c 20 20 38 33 37 2c 20 20 38 33 39   836,  837,  839
2fdc9 2c 20 20 38 34 35 2c 20 20 38 35 32 2c 0a 20 2f  ,  845,  852,. /
2fdca 2a 20 20 20 34 36 30 20 2a 2f 20 20 20 38 35 35  *   460 */   855
2fdcb 2c 20 20 38 33 38 2c 20 20 38 34 33 2c 20 20 38  ,  838,  843,  8
2fdcc 34 34 2c 20 20 38 34 36 2c 20 20 38 35 31 2c 20  44,  846,  851, 
2fdcd 20 38 35 34 2c 20 20 37 34 34 2c 20 20 37 34 35   854,  744,  745
2fdce 2c 20 20 38 35 38 2c 0a 20 2f 2a 20 20 20 34 37  ,  858,. /*   47
2fdcf 30 20 2a 2f 20 20 20 36 37 36 2c 20 20 36 37 37  0 */   676,  677
2fdd0 2c 20 20 36 38 30 2c 20 20 36 38 31 2c 20 20 38  ,  680,  681,  8
2fdd1 39 34 2c 20 20 38 39 36 2c 20 20 38 39 35 2c 20  94,  896,  895, 
2fdd2 20 38 39 37 2c 20 20 36 37 39 2c 20 20 36 37 38   897,  679,  678
2fdd3 2c 0a 20 2f 2a 20 20 20 34 38 30 20 2a 2f 20 20  ,. /*   480 */  
2fdd4 20 38 32 34 2c 20 20 38 32 37 2c 20 20 38 36 30   824,  827,  860
2fdd5 2c 20 20 38 36 31 2c 20 20 39 32 34 2c 20 20 39  ,  861,  924,  9
2fdd6 32 35 2c 20 20 39 32 36 2c 20 20 39 32 37 2c 20  25,  926,  927, 
2fdd7 20 39 32 38 2c 20 20 38 35 36 2c 0a 20 2f 2a 20   928,  856,. /* 
2fdd8 20 20 34 39 30 20 2a 2f 20 20 20 37 36 36 2c 20    490 */   766, 
2fdd9 20 38 35 39 2c 20 20 38 34 32 2c 20 20 37 38 31   859,  842,  781
2fdda 2c 20 20 37 38 34 2c 20 20 37 38 35 2c 20 20 37  ,  784,  785,  7
2fddb 38 32 2c 20 20 37 35 30 2c 20 20 37 36 30 2c 20  82,  750,  760, 
2fddc 20 37 36 38 2c 0a 20 2f 2a 20 20 20 35 30 30 20   768,. /*   500 
2fddd 2a 2f 20 20 20 37 36 39 2c 20 20 37 37 30 2c 20  */   769,  770, 
2fdde 20 37 37 31 2c 20 20 37 35 35 2c 20 20 37 35 36   771,  755,  756
2fddf 2c 20 20 37 36 32 2c 20 20 37 37 37 2c 20 20 38  ,  762,  777,  8
2fde0 31 30 2c 20 20 38 31 31 2c 20 20 37 37 35 2c 0a  10,  811,  775,.
2fde1 20 2f 2a 20 20 20 35 31 30 20 2a 2f 20 20 20 37   /*   510 */   7
2fde2 37 36 2c 20 20 37 36 33 2c 20 20 37 36 34 2c 20  76,  763,  764, 
2fde3 20 37 35 32 2c 20 20 37 35 33 2c 20 20 37 35 34   752,  753,  754
2fde4 2c 20 20 38 35 37 2c 20 20 38 31 33 2c 20 20 38  ,  857,  813,  8
2fde5 32 35 2c 20 20 38 32 36 2c 0a 20 2f 2a 20 20 20  25,  826,. /*   
2fde6 35 32 30 20 2a 2f 20 20 20 36 38 37 2c 20 20 36  520 */   687,  6
2fde7 38 38 2c 20 20 38 32 30 2c 20 20 36 38 39 2c 20  88,  820,  689, 
2fde8 20 36 39 30 2c 20 20 36 39 31 2c 20 20 37 32 39   690,  691,  729
2fde9 2c 20 20 37 33 32 2c 20 20 37 33 33 2c 20 20 37  ,  732,  733,  7
2fdea 33 34 2c 0a 20 2f 2a 20 20 20 35 33 30 20 2a 2f  34,. /*   530 */
2fdeb 20 20 20 36 39 32 2c 20 20 37 31 31 2c 20 20 37     692,  711,  7
2fdec 31 34 2c 20 20 37 31 35 2c 20 20 36 39 33 2c 20  14,  715,  693, 
2fded 20 37 30 30 2c 20 20 36 39 34 2c 20 20 36 39 35   700,  694,  695
2fdee 2c 20 20 37 30 32 2c 20 20 37 30 33 2c 0a 20 2f  ,  702,  703,. /
2fdef 2a 20 20 20 35 34 30 20 2a 2f 20 20 20 37 30 34  *   540 */   704
2fdf0 2c 20 20 37 30 37 2c 20 20 37 30 38 2c 20 20 37  ,  707,  708,  7
2fdf1 30 39 2c 20 20 37 31 30 2c 20 20 37 30 35 2c 20  09,  710,  705, 
2fdf2 20 37 30 36 2c 20 20 38 37 36 2c 20 20 38 37 37   706,  876,  877
2fdf3 2c 20 20 38 38 30 2c 0a 20 2f 2a 20 20 20 35 35  ,  880,. /*   55
2fdf4 30 20 2a 2f 20 20 20 38 37 38 2c 20 20 36 39 37  0 */   878,  697
2fdf5 2c 20 20 36 39 38 2c 20 20 37 31 32 2c 20 20 36  ,  698,  712,  6
2fdf6 38 35 2c 20 20 36 37 34 2c 20 20 36 36 36 2c 20  85,  674,  666, 
2fdf7 20 37 31 38 2c 20 20 37 32 31 2c 20 20 37 32 32   718,  721,  722
2fdf8 2c 0a 20 2f 2a 20 20 20 35 36 30 20 2a 2f 20 20  ,. /*   560 */  
2fdf9 20 37 32 33 2c 20 20 37 32 34 2c 20 20 37 32 35   723,  724,  725
2fdfa 2c 20 20 37 32 37 2c 20 20 37 31 39 2c 20 20 37  ,  727,  719,  7
2fdfb 32 30 2c 20 20 36 36 34 2c 20 20 36 35 35 2c 20  20,  664,  655, 
2fdfc 20 36 35 38 2c 20 20 37 36 37 2c 0a 20 2f 2a 20   658,  767,. /* 
2fdfd 20 20 35 37 30 20 2a 2f 20 20 20 39 30 30 2c 20    570 */   900, 
2fdfe 20 39 30 39 2c 20 20 39 30 35 2c 20 20 39 30 31   909,  905,  901
2fdff 2c 20 20 39 30 32 2c 20 20 39 30 33 2c 20 20 36  ,  902,  903,  6
2fe00 35 39 2c 20 20 38 37 32 2c 20 20 38 37 33 2c 20  59,  872,  873, 
2fe01 20 37 33 31 2c 0a 20 2f 2a 20 20 20 35 38 30 20   731,. /*   580 
2fe02 2a 2f 20 20 20 38 30 34 2c 20 20 38 30 35 2c 20  */   804,  805, 
2fe03 20 38 39 39 2c 20 20 39 31 32 2c 20 20 39 31 35   899,  912,  915
2fe04 2c 20 20 39 31 37 2c 20 20 39 31 38 2c 20 20 39  ,  917,  918,  9
2fe05 31 39 2c 20 20 38 30 39 2c 20 20 39 32 30 2c 0a  19,  809,  920,.
2fe06 20 2f 2a 20 20 20 35 39 30 20 2a 2f 20 20 20 39   /*   590 */   9
2fe07 32 32 2c 20 20 39 31 33 2c 20 20 39 34 37 2c 20  22,  913,  947, 
2fe08 20 36 35 36 2c 20 20 37 33 35 2c 20 20 37 33 36   656,  735,  736
2fe09 2c 20 20 37 33 39 2c 20 20 38 38 31 2c 20 20 39  ,  739,  881,  9
2fe0a 32 39 2c 20 20 37 39 35 2c 0a 20 2f 2a 20 20 20  29,  795,. /*   
2fe0b 36 30 30 20 2a 2f 20 20 20 37 39 38 2c 20 20 38  600 */   798,  8
2fe0c 30 31 2c 20 20 38 30 33 2c 20 20 38 38 33 2c 20  01,  803,  883, 
2fe0d 20 38 38 35 2c 20 20 38 38 37 2c 20 20 38 38 39   885,  887,  889
2fe0e 2c 20 20 38 39 30 2c 20 20 38 39 31 2c 20 20 38  ,  890,  891,  8
2fe0f 39 32 2c 0a 20 2f 2a 20 20 20 36 31 30 20 2a 2f  92,. /*   610 */
2fe10 20 20 20 38 39 33 2c 20 20 38 38 36 2c 20 20 38     893,  886,  8
2fe11 38 38 2c 20 20 39 33 30 2c 20 20 39 33 34 2c 20  88,  930,  934, 
2fe12 20 39 33 37 2c 20 20 39 33 39 2c 20 20 39 34 30   937,  939,  940
2fe13 2c 20 20 39 34 31 2c 20 20 39 34 34 2c 0a 20 2f  ,  941,  944,. /
2fe14 2a 20 20 20 36 32 30 20 2a 2f 20 20 20 39 34 36  *   620 */   946
2fe15 2c 20 20 39 35 31 2c 20 20 39 35 32 2c 20 20 39  ,  951,  952,  9
2fe16 35 33 2c 20 20 39 35 36 2c 20 20 39 35 37 2c 20  53,  956,  957, 
2fe17 20 39 35 34 2c 20 20 36 33 36 2c 20 20 36 33 31   954,  636,  631
2fe18 2c 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 59 59 5f  ,.};.#define YY_
2fe19 53 5a 5f 41 43 54 54 41 42 20 28 69 6e 74 29 28  SZ_ACTTAB (int)(
2fe1a 73 69 7a 65 6f 66 28 79 79 5f 61 63 74 69 6f 6e  sizeof(yy_action
2fe1b 29 2f 73 69 7a 65 6f 66 28 79 79 5f 61 63 74 69  )/sizeof(yy_acti
2fe1c 6f 6e 5b 30 5d 29 29 0a 0a 2f 2a 20 54 68 65 20  on[0]))../* The 
2fe1d 6e 65 78 74 20 74 61 62 6c 65 20 6d 61 70 73 20  next table maps 
2fe1e 74 6f 6b 65 6e 73 20 69 6e 74 6f 20 66 61 6c 6c  tokens into fall
2fe1f 62 61 63 6b 20 74 6f 6b 65 6e 73 2e 20 20 49 66  back tokens.  If
2fe20 20 61 20 63 6f 6e 73 74 72 75 63 74 0a 2a 2a 20   a construct.** 
2fe21 6c 69 6b 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69  like the followi
2fe22 6e 67 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 20 20 20  ng:.** .**      
2fe23 25 66 61 6c 6c 62 61 63 6b 20 49 44 20 58 20 59  %fallback ID X Y
2fe24 20 5a 2e 0a 2a 2a 0a 2a 2a 20 61 70 70 65 61 72   Z..**.** appear
2fe25 73 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61 72  s in the grammar
2fe26 2c 20 74 68 65 6e 20 49 44 20 62 65 63 6f 6d 65  , then ID become
2fe27 73 20 61 20 66 61 6c 6c 62 61 63 6b 20 74 6f 6b  s a fallback tok
2fe28 65 6e 20 66 6f 72 20 58 2c 20 59 2c 0a 2a 2a 20  en for X, Y,.** 
2fe29 61 6e 64 20 5a 2e 20 20 57 68 65 6e 65 76 65 72  and Z.  Whenever
2fe2a 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 6f 6b 65   one of the toke
2fe2b 6e 73 20 58 2c 20 59 2c 20 6f 72 20 5a 20 69 73  ns X, Y, or Z is
2fe2c 20 69 6e 70 75 74 20 74 6f 20 74 68 65 20 70 61   input to the pa
2fe2d 72 73 65 72 0a 2a 2a 20 62 75 74 20 69 74 20 64  rser.** but it d
2fe2e 6f 65 73 20 6e 6f 74 20 70 61 72 73 65 2c 20 74  oes not parse, t
2fe2f 68 65 20 74 79 70 65 20 6f 66 20 74 68 65 20 74  he type of the t
2fe30 6f 6b 65 6e 20 69 73 20 63 68 61 6e 67 65 64 20  oken is changed 
2fe31 74 6f 20 49 44 20 61 6e 64 0a 2a 2a 20 74 68 65  to ID and.** the
2fe32 20 70 61 72 73 65 20 69 73 20 72 65 74 72 69 65   parse is retrie
2fe33 64 20 62 65 66 6f 72 65 20 61 6e 20 65 72 72 6f  d before an erro
2fe34 72 20 69 73 20 74 68 72 6f 77 6e 2e 0a 2a 2f 0a  r is thrown..*/.
2fe35 23 69 66 64 65 66 20 59 59 46 41 4c 4c 42 41 43  #ifdef YYFALLBAC
2fe36 4b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 59  K.static const Y
2fe37 59 43 4f 44 45 54 59 50 45 20 79 79 46 61 6c 6c  YCODETYPE yyFall
2fe38 62 61 63 6b 5b 5d 20 3d 20 7b 0a 20 20 20 20 30  back[] = {.    0
2fe39 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 20 24  ,  /*          $
2fe3a 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2fe3b 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2fe3c 53 45 4d 49 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  SEMI => nothing 
2fe3d 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
2fe3e 20 45 58 50 4c 41 49 4e 20 3d 3e 20 49 44 20 2a   EXPLAIN => ID *
2fe3f 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
2fe40 20 20 51 55 45 52 59 20 3d 3e 20 49 44 20 2a 2f    QUERY => ID */
2fe41 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
2fe42 20 20 50 4c 41 4e 20 3d 3e 20 49 44 20 2a 2f 0a    PLAN => ID */.
2fe43 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20     26,  /*      
2fe44 42 45 47 49 4e 20 3d 3e 20 49 44 20 2a 2f 0a 20  BEGIN => ID */. 
2fe45 20 20 20 30 2c 20 20 2f 2a 20 54 52 41 4e 53 41     0,  /* TRANSA
2fe46 43 54 49 4f 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67  CTION => nothing
2fe47 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2fe48 20 44 45 46 45 52 52 45 44 20 3d 3e 20 49 44 20   DEFERRED => ID 
2fe49 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 49  */.   26,  /*  I
2fe4a 4d 4d 45 44 49 41 54 45 20 3d 3e 20 49 44 20 2a  MMEDIATE => ID *
2fe4b 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 45 58  /.   26,  /*  EX
2fe4c 43 4c 55 53 49 56 45 20 3d 3e 20 49 44 20 2a 2f  CLUSIVE => ID */
2fe4d 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20  .    0,  /*     
2fe4e 43 4f 4d 4d 49 54 20 3d 3e 20 6e 6f 74 68 69 6e  COMMIT => nothin
2fe4f 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  g */.   26,  /* 
2fe50 20 20 20 20 20 20 20 45 4e 44 20 3d 3e 20 49 44         END => ID
2fe51 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2fe52 20 52 4f 4c 4c 42 41 43 4b 20 3d 3e 20 49 44 20   ROLLBACK => ID 
2fe53 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 53  */.   26,  /*  S
2fe54 41 56 45 50 4f 49 4e 54 20 3d 3e 20 49 44 20 2a  AVEPOINT => ID *
2fe55 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
2fe56 52 45 4c 45 41 53 45 20 3d 3e 20 49 44 20 2a 2f  RELEASE => ID */
2fe57 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20  .    0,  /*     
2fe58 20 20 20 20 54 4f 20 3d 3e 20 6e 6f 74 68 69 6e      TO => nothin
2fe59 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20  g */.    0,  /* 
2fe5a 20 20 20 20 20 54 41 42 4c 45 20 3d 3e 20 6e 6f       TABLE => no
2fe5b 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20  thing */.    0, 
2fe5c 20 2f 2a 20 20 20 20 20 43 52 45 41 54 45 20 3d   /*     CREATE =
2fe5d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20  > nothing */.   
2fe5e 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20  26,  /*         
2fe5f 49 46 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20  IF => ID */.    
2fe60 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 4e 4f  0,  /*        NO
2fe61 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  T => nothing */.
2fe62 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 45      0,  /*     E
2fe63 58 49 53 54 53 20 3d 3e 20 6e 6f 74 68 69 6e 67  XISTS => nothing
2fe64 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2fe65 20 20 20 20 20 54 45 4d 50 20 3d 3e 20 49 44 20       TEMP => ID 
2fe66 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2fe67 20 20 20 20 20 20 4c 50 20 3d 3e 20 6e 6f 74 68        LP => noth
2fe68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2fe69 2a 20 20 20 20 20 20 20 20 20 52 50 20 3d 3e 20  *         RP => 
2fe6a 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2fe6b 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 41 53  ,  /*         AS
2fe6c 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2fe6d 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 43     0,  /*      C
2fe6e 4f 4d 4d 41 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  OMMA => nothing 
2fe6f 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2fe70 20 20 20 20 20 20 49 44 20 3d 3e 20 6e 6f 74 68        ID => noth
2fe71 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2fe72 2a 20 20 20 20 49 4e 44 45 58 45 44 20 3d 3e 20  *    INDEXED => 
2fe73 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32 36  nothing */.   26
2fe74 2c 20 20 2f 2a 20 20 20 20 20 20 41 42 4f 52 54  ,  /*      ABORT
2fe75 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
2fe76 20 20 2f 2a 20 20 20 20 20 20 41 46 54 45 52 20    /*      AFTER 
2fe77 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
2fe78 20 2f 2a 20 20 20 20 41 4e 41 4c 59 5a 45 20 3d   /*    ANALYZE =
2fe79 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
2fe7a 2f 2a 20 20 20 20 20 20 20 20 41 53 43 20 3d 3e  /*        ASC =>
2fe7b 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
2fe7c 2a 20 20 20 20 20 41 54 54 41 43 48 20 3d 3e 20  *     ATTACH => 
2fe7d 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
2fe7e 20 20 20 20 20 42 45 46 4f 52 45 20 3d 3e 20 49       BEFORE => I
2fe7f 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
2fe80 20 20 20 20 20 20 20 20 42 59 20 3d 3e 20 49 44          BY => ID
2fe81 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2fe82 20 20 43 41 53 43 41 44 45 20 3d 3e 20 49 44 20    CASCADE => ID 
2fe83 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
2fe84 20 20 20 20 43 41 53 54 20 3d 3e 20 49 44 20 2a      CAST => ID *
2fe85 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 43  /.   26,  /*   C
2fe86 4f 4c 55 4d 4e 4b 57 20 3d 3e 20 49 44 20 2a 2f  OLUMNKW => ID */
2fe87 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 43 4f  .   26,  /*   CO
2fe88 4e 46 4c 49 43 54 20 3d 3e 20 49 44 20 2a 2f 0a  NFLICT => ID */.
2fe89 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 44 41 54     26,  /*   DAT
2fe8a 41 42 41 53 45 20 3d 3e 20 49 44 20 2a 2f 0a 20  ABASE => ID */. 
2fe8b 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20    26,  /*       
2fe8c 44 45 53 43 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  DESC => ID */.  
2fe8d 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 44 45 54   26,  /*     DET
2fe8e 41 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ACH => ID */.   
2fe8f 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 45 41  26,  /*       EA
2fe90 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  CH => ID */.   2
2fe91 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 46 41 49  6,  /*       FAI
2fe92 4c 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  L => ID */.   26
2fe93 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 46 4f 52  ,  /*        FOR
2fe94 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
2fe95 20 20 2f 2a 20 20 20 20 20 49 47 4e 4f 52 45 20    /*     IGNORE 
2fe96 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
2fe97 20 2f 2a 20 20 49 4e 49 54 49 41 4c 4c 59 20 3d   /*  INITIALLY =
2fe98 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
2fe99 2f 2a 20 20 20 20 49 4e 53 54 45 41 44 20 3d 3e  /*    INSTEAD =>
2fe9a 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
2fe9b 2a 20 20 20 20 4c 49 4b 45 5f 4b 57 20 3d 3e 20  *    LIKE_KW => 
2fe9c 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
2fe9d 20 20 20 20 20 20 4d 41 54 43 48 20 3d 3e 20 49        MATCH => I
2fe9e 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
2fe9f 20 20 20 20 20 20 20 4b 45 59 20 3d 3e 20 49 44         KEY => ID
2fea0 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2fea1 20 20 20 20 20 20 20 4f 46 20 3d 3e 20 49 44 20         OF => ID 
2fea2 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
2fea3 20 20 4f 46 46 53 45 54 20 3d 3e 20 49 44 20 2a    OFFSET => ID *
2fea4 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
2fea5 20 50 52 41 47 4d 41 20 3d 3e 20 49 44 20 2a 2f   PRAGMA => ID */
2fea6 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
2fea7 20 52 41 49 53 45 20 3d 3e 20 49 44 20 2a 2f 0a   RAISE => ID */.
2fea8 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 52 45     26,  /*    RE
2fea9 50 4c 41 43 45 20 3d 3e 20 49 44 20 2a 2f 0a 20  PLACE => ID */. 
2feaa 20 20 32 36 2c 20 20 2f 2a 20 20 20 52 45 53 54    26,  /*   REST
2feab 52 49 43 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  RICT => ID */.  
2feac 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20   26,  /*        
2fead 52 4f 57 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ROW => ID */.   
2feae 32 36 2c 20 20 2f 2a 20 20 20 20 54 52 49 47 47  26,  /*    TRIGG
2feaf 45 52 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  ER => ID */.   2
2feb0 36 2c 20 20 2f 2a 20 20 20 20 20 56 41 43 55 55  6,  /*     VACUU
2feb1 4d 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  M => ID */.   26
2feb2 2c 20 20 2f 2a 20 20 20 20 20 20 20 56 49 45 57  ,  /*       VIEW
2feb3 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
2feb4 20 20 2f 2a 20 20 20 20 56 49 52 54 55 41 4c 20    /*    VIRTUAL 
2feb5 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
2feb6 20 2f 2a 20 20 20 20 52 45 49 4e 44 45 58 20 3d   /*    REINDEX =
2feb7 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
2feb8 2f 2a 20 20 20 20 20 52 45 4e 41 4d 45 20 3d 3e  /*     RENAME =>
2feb9 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
2feba 2a 20 20 20 43 54 49 4d 45 5f 4b 57 20 3d 3e 20  *   CTIME_KW => 
2febb 49 44 20 2a 2f 0a 7d 3b 0a 23 65 6e 64 69 66 20  ID */.};.#endif 
2febc 2f 2a 20 59 59 46 41 4c 4c 42 41 43 4b 20 2a 2f  /* YYFALLBACK */
2febd 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ../* The followi
2febe 6e 67 20 73 74 72 75 63 74 75 72 65 20 72 65 70  ng structure rep
2febf 72 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c 65  resents a single
2fec0 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 0a   element of the.
2fec1 2a 2a 20 70 61 72 73 65 72 27 73 20 73 74 61 63  ** parser's stac
2fec2 6b 2e 20 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20  k.  Information 
2fec3 73 74 6f 72 65 64 20 69 6e 63 6c 75 64 65 73 3a  stored includes:
2fec4 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 20 54 68 65 20  .**.**   +  The 
2fec5 73 74 61 74 65 20 6e 75 6d 62 65 72 20 66 6f 72  state number for
2fec6 20 74 68 65 20 70 61 72 73 65 72 20 61 74 20 74   the parser at t
2fec7 68 69 73 20 6c 65 76 65 6c 20 6f 66 20 74 68 65  his level of the
2fec8 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20   stack..**.**   
2fec9 2b 20 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20  +  The value of 
2feca 74 68 65 20 74 6f 6b 65 6e 20 73 74 6f 72 65 64  the token stored
2fecb 20 61 74 20 74 68 69 73 20 6c 65 76 65 6c 20 6f   at this level o
2fecc 66 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 20  f the stack..** 
2fecd 20 20 20 20 20 28 49 6e 20 6f 74 68 65 72 20 77       (In other w
2fece 6f 72 64 73 2c 20 74 68 65 20 22 6d 61 6a 6f 72  ords, the "major
2fecf 22 20 74 6f 6b 65 6e 2e 29 0a 2a 2a 0a 2a 2a 20  " token.).**.** 
2fed0 20 20 2b 20 20 54 68 65 20 73 65 6d 61 6e 74 69    +  The semanti
2fed1 63 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 61  c value stored a
2fed2 74 20 74 68 69 73 20 6c 65 76 65 6c 20 6f 66 20  t this level of 
2fed3 74 68 65 20 73 74 61 63 6b 2e 20 20 54 68 69 73  the stack.  This
2fed4 20 69 73 0a 2a 2a 20 20 20 20 20 20 74 68 65 20   is.**      the 
2fed5 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64  information used
2fed6 20 62 79 20 74 68 65 20 61 63 74 69 6f 6e 20 72   by the action r
2fed7 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 65 20 67  outines in the g
2fed8 72 61 6d 6d 61 72 2e 0a 2a 2a 20 20 20 20 20 20  rammar..**      
2fed9 49 74 20 69 73 20 73 6f 6d 65 74 69 6d 65 73 20  It is sometimes 
2feda 63 61 6c 6c 65 64 20 74 68 65 20 22 6d 69 6e 6f  called the "mino
2fedb 72 22 20 74 6f 6b 65 6e 2e 0a 2a 2f 0a 73 74 72  r" token..*/.str
2fedc 75 63 74 20 79 79 53 74 61 63 6b 45 6e 74 72 79  uct yyStackEntry
2fedd 20 7b 0a 20 20 59 59 41 43 54 49 4f 4e 54 59 50   {.  YYACTIONTYP
2fede 45 20 73 74 61 74 65 6e 6f 3b 20 20 2f 2a 20 54  E stateno;  /* T
2fedf 68 65 20 73 74 61 74 65 2d 6e 75 6d 62 65 72 20  he state-number 
2fee0 2a 2f 0a 20 20 59 59 43 4f 44 45 54 59 50 45 20  */.  YYCODETYPE 
2fee1 6d 61 6a 6f 72 3b 20 20 20 20 20 20 2f 2a 20 54  major;      /* T
2fee2 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 76  he major token v
2fee3 61 6c 75 65 2e 20 20 54 68 69 73 20 69 73 20 74  alue.  This is t
2fee4 68 65 20 63 6f 64 65 0a 20 20 20 20 20 20 20 20  he code.        
2fee5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fee6 20 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74   ** number for t
2fee7 68 65 20 74 6f 6b 65 6e 20 61 74 20 74 68 69 73  he token at this
2fee8 20 73 74 61 63 6b 20 6c 65 76 65 6c 20 2a 2f 0a   stack level */.
2fee9 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 6d 69    YYMINORTYPE mi
2feea 6e 6f 72 3b 20 20 20 20 20 2f 2a 20 54 68 65 20  nor;     /* The 
2feeb 75 73 65 72 2d 73 75 70 70 6c 69 65 64 20 6d 69  user-supplied mi
2feec 6e 6f 72 20 74 6f 6b 65 6e 20 76 61 6c 75 65 2e  nor token value.
2feed 20 20 54 68 69 73 0a 20 20 20 20 20 20 20 20 20    This.         
2feee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2feef 2a 2a 20 69 73 20 74 68 65 20 76 61 6c 75 65 20  ** is the value 
2fef0 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 20 2a 2f  of the token  */
2fef1 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  .};.typedef stru
2fef2 63 74 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20  ct yyStackEntry 
2fef3 79 79 53 74 61 63 6b 45 6e 74 72 79 3b 0a 0a 2f  yyStackEntry;../
2fef4 2a 20 54 68 65 20 73 74 61 74 65 20 6f 66 20 74  * The state of t
2fef5 68 65 20 70 61 72 73 65 72 20 69 73 20 63 6f 6d  he parser is com
2fef6 70 6c 65 74 65 6c 79 20 63 6f 6e 74 61 69 6e 65  pletely containe
2fef7 64 20 69 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65  d in an instance
2fef8 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f   of.** the follo
2fef9 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 2a  wing structure *
2fefa 2f 0a 73 74 72 75 63 74 20 79 79 50 61 72 73 65  /.struct yyParse
2fefb 72 20 7b 0a 20 20 69 6e 74 20 79 79 69 64 78 3b  r {.  int yyidx;
2fefc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fefd 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
2fefe 74 6f 70 20 65 6c 65 6d 65 6e 74 20 69 6e 20 73  top element in s
2feff 74 61 63 6b 20 2a 2f 0a 23 69 66 64 65 66 20 59  tack */.#ifdef Y
2ff00 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45  YTRACKMAXSTACKDE
2ff01 50 54 48 0a 20 20 69 6e 74 20 79 79 69 64 78 4d  PTH.  int yyidxM
2ff02 61 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ax;             
2ff03 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 76      /* Maximum v
2ff04 61 6c 75 65 20 6f 66 20 79 79 69 64 78 20 2a 2f  alue of yyidx */
2ff05 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 79 79  .#endif.  int yy
2ff06 65 72 72 63 6e 74 3b 20 20 20 20 20 20 20 20 20  errcnt;         
2ff07 20 20 20 20 20 20 20 20 2f 2a 20 53 68 69 66 74          /* Shift
2ff08 73 20 6c 65 66 74 20 62 65 66 6f 72 65 20 6f 75  s left before ou
2ff09 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 2a  t of the error *
2ff0a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  /.  sqlite3Parse
2ff0b 72 41 52 47 5f 53 44 45 43 4c 20 20 20 20 20 20  rARG_SDECL      
2ff0c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70            /* A p
2ff0d 6c 61 63 65 20 74 6f 20 68 6f 6c 64 20 25 65 78  lace to hold %ex
2ff0e 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 2a 2f 0a  tra_argument */.
2ff0f 23 69 66 20 59 59 53 54 41 43 4b 44 45 50 54 48  #if YYSTACKDEPTH
2ff10 3c 3d 30 0a 20 20 69 6e 74 20 79 79 73 74 6b 73  <=0.  int yystks
2ff11 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  z;              
2ff12 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 73      /* Current s
2ff13 69 64 65 20 6f 66 20 74 68 65 20 73 74 61 63 6b  ide of the stack
2ff14 20 2a 2f 0a 20 20 79 79 53 74 61 63 6b 45 6e 74   */.  yyStackEnt
2ff15 72 79 20 2a 79 79 73 74 61 63 6b 3b 20 20 20 20  ry *yystack;    
2ff16 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
2ff17 72 27 73 20 73 74 61 63 6b 20 2a 2f 0a 23 65 6c  r's stack */.#el
2ff18 73 65 0a 20 20 79 79 53 74 61 63 6b 45 6e 74 72  se.  yyStackEntr
2ff19 79 20 79 79 73 74 61 63 6b 5b 59 59 53 54 41 43  y yystack[YYSTAC
2ff1a 4b 44 45 50 54 48 5d 3b 20 20 2f 2a 20 54 68 65  KDEPTH];  /* The
2ff1b 20 70 61 72 73 65 72 27 73 20 73 74 61 63 6b 20   parser's stack 
2ff1c 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 74 79 70  */.#endif.};.typ
2ff1d 65 64 65 66 20 73 74 72 75 63 74 20 79 79 50 61  edef struct yyPa
2ff1e 72 73 65 72 20 79 79 50 61 72 73 65 72 3b 0a 0a  rser yyParser;..
2ff1f 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73  #ifndef NDEBUG.s
2ff20 74 61 74 69 63 20 46 49 4c 45 20 2a 79 79 54 72  tatic FILE *yyTr
2ff21 61 63 65 46 49 4c 45 20 3d 20 30 3b 0a 73 74 61  aceFILE = 0;.sta
2ff22 74 69 63 20 63 68 61 72 20 2a 79 79 54 72 61 63  tic char *yyTrac
2ff23 65 50 72 6f 6d 70 74 20 3d 20 30 3b 0a 23 65 6e  ePrompt = 0;.#en
2ff24 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f  dif /* NDEBUG */
2ff25 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
2ff26 0a 2f 2a 20 0a 2a 2a 20 54 75 72 6e 20 70 61 72  ./* .** Turn par
2ff27 73 65 72 20 74 72 61 63 69 6e 67 20 6f 6e 20 62  ser tracing on b
2ff28 79 20 67 69 76 69 6e 67 20 61 20 73 74 72 65 61  y giving a strea
2ff29 6d 20 74 6f 20 77 68 69 63 68 20 74 6f 20 77 72  m to which to wr
2ff2a 69 74 65 20 74 68 65 20 74 72 61 63 65 0a 2a 2a  ite the trace.**
2ff2b 20 61 6e 64 20 61 20 70 72 6f 6d 70 74 20 74 6f   and a prompt to
2ff2c 20 70 72 65 66 61 63 65 20 65 61 63 68 20 74 72   preface each tr
2ff2d 61 63 65 20 6d 65 73 73 61 67 65 2e 20 20 54 72  ace message.  Tr
2ff2e 61 63 69 6e 67 20 69 73 20 74 75 72 6e 65 64 20  acing is turned 
2ff2f 6f 66 66 0a 2a 2a 20 62 79 20 6d 61 6b 69 6e 67  off.** by making
2ff30 20 65 69 74 68 65 72 20 61 72 67 75 6d 65 6e 74   either argument
2ff31 20 4e 55 4c 4c 20 0a 2a 2a 0a 2a 2a 20 49 6e 70   NULL .**.** Inp
2ff32 75 74 73 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  uts:.** <ul>.** 
2ff33 3c 6c 69 3e 20 41 20 46 49 4c 45 2a 20 74 6f 20  <li> A FILE* to 
2ff34 77 68 69 63 68 20 74 72 61 63 65 20 6f 75 74 70  which trace outp
2ff35 75 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69  ut should be wri
2ff36 74 74 65 6e 2e 0a 2a 2a 20 20 20 20 20 20 49 66  tten..**      If
2ff37 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 72 61 63   NULL, then trac
2ff38 69 6e 67 20 69 73 20 74 75 72 6e 65 64 20 6f 66  ing is turned of
2ff39 66 2e 0a 2a 2a 20 3c 6c 69 3e 20 41 20 70 72 65  f..** <li> A pre
2ff3a 66 69 78 20 73 74 72 69 6e 67 20 77 72 69 74 74  fix string writt
2ff3b 65 6e 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  en at the beginn
2ff3c 69 6e 67 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20  ing of every.** 
2ff3d 20 20 20 20 20 6c 69 6e 65 20 6f 66 20 74 72 61       line of tra
2ff3e 63 65 20 6f 75 74 70 75 74 2e 20 20 49 66 20 4e  ce output.  If N
2ff3f 55 4c 4c 2c 20 74 68 65 6e 20 74 72 61 63 69 6e  ULL, then tracin
2ff40 67 20 69 73 0a 2a 2a 20 20 20 20 20 20 74 75 72  g is.**      tur
2ff41 6e 65 64 20 6f 66 66 2e 0a 2a 2a 20 3c 2f 75 6c  ned off..** </ul
2ff42 3e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74 73 3a  >.**.** Outputs:
2ff43 0a 2a 2a 20 4e 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c  .** None..*/.SQL
2ff44 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2ff45 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 72   sqlite3ParserTr
2ff46 61 63 65 28 46 49 4c 45 20 2a 54 72 61 63 65 46  ace(FILE *TraceF
2ff47 49 4c 45 2c 20 63 68 61 72 20 2a 7a 54 72 61 63  ILE, char *zTrac
2ff48 65 50 72 6f 6d 70 74 29 7b 0a 20 20 79 79 54 72  ePrompt){.  yyTr
2ff49 61 63 65 46 49 4c 45 20 3d 20 54 72 61 63 65 46  aceFILE = TraceF
2ff4a 49 4c 45 3b 0a 20 20 79 79 54 72 61 63 65 50 72  ILE;.  yyTracePr
2ff4b 6f 6d 70 74 20 3d 20 7a 54 72 61 63 65 50 72 6f  ompt = zTracePro
2ff4c 6d 70 74 3b 0a 20 20 69 66 28 20 79 79 54 72 61  mpt;.  if( yyTra
2ff4d 63 65 46 49 4c 45 3d 3d 30 20 29 20 79 79 54 72  ceFILE==0 ) yyTr
2ff4e 61 63 65 50 72 6f 6d 70 74 20 3d 20 30 3b 0a 20  acePrompt = 0;. 
2ff4f 20 65 6c 73 65 20 69 66 28 20 79 79 54 72 61 63   else if( yyTrac
2ff50 65 50 72 6f 6d 70 74 3d 3d 30 20 29 20 79 79 54  ePrompt==0 ) yyT
2ff51 72 61 63 65 46 49 4c 45 20 3d 20 30 3b 0a 7d 0a  raceFILE = 0;.}.
2ff52 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47  #endif /* NDEBUG
2ff53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45   */..#ifndef NDE
2ff54 42 55 47 0a 2f 2a 20 46 6f 72 20 74 72 61 63 69  BUG./* For traci
2ff55 6e 67 20 73 68 69 66 74 73 2c 20 74 68 65 20 6e  ng shifts, the n
2ff56 61 6d 65 73 20 6f 66 20 61 6c 6c 20 74 65 72 6d  ames of all term
2ff57 69 6e 61 6c 73 20 61 6e 64 20 6e 6f 6e 74 65 72  inals and nonter
2ff58 6d 69 6e 61 6c 73 0a 2a 2a 20 61 72 65 20 72 65  minals.** are re
2ff59 71 75 69 72 65 64 2e 20 20 54 68 65 20 66 6f 6c  quired.  The fol
2ff5a 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 73 75 70  lowing table sup
2ff5b 70 6c 69 65 73 20 74 68 65 73 65 20 6e 61 6d 65  plies these name
2ff5c 73 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  s */.static cons
2ff5d 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20 79 79  t char *const yy
2ff5e 54 6f 6b 65 6e 4e 61 6d 65 5b 5d 20 3d 20 7b 20  TokenName[] = { 
2ff5f 0a 20 20 22 24 22 2c 20 20 20 20 20 20 20 20 20  .  "$",         
2ff60 20 20 20 20 22 53 45 4d 49 22 2c 20 20 20 20 20      "SEMI",     
2ff61 20 20 20 20 20 22 45 58 50 4c 41 49 4e 22 2c 20       "EXPLAIN", 
2ff62 20 20 20 20 20 20 22 51 55 45 52 59 22 2c 20 20        "QUERY",  
2ff63 20 20 20 20 20 0a 20 20 22 50 4c 41 4e 22 2c 20       .  "PLAN", 
2ff64 20 20 20 20 20 20 20 20 20 22 42 45 47 49 4e 22           "BEGIN"
2ff65 2c 20 20 20 20 20 20 20 20 20 22 54 52 41 4e 53  ,         "TRANS
2ff66 41 43 54 49 4f 4e 22 2c 20 20 20 22 44 45 46 45  ACTION",   "DEFE
2ff67 52 52 45 44 22 2c 20 20 20 20 0a 20 20 22 49 4d  RRED",    .  "IM
2ff68 4d 45 44 49 41 54 45 22 2c 20 20 20 20 20 22 45  MEDIATE",     "E
2ff69 58 43 4c 55 53 49 56 45 22 2c 20 20 20 20 20 22  XCLUSIVE",     "
2ff6a 43 4f 4d 4d 49 54 22 2c 20 20 20 20 20 20 20 20  COMMIT",        
2ff6b 22 45 4e 44 22 2c 20 20 20 20 20 20 20 20 20 0a  "END",         .
2ff6c 20 20 22 52 4f 4c 4c 42 41 43 4b 22 2c 20 20 20    "ROLLBACK",   
2ff6d 20 20 20 22 53 41 56 45 50 4f 49 4e 54 22 2c 20     "SAVEPOINT", 
2ff6e 20 20 20 20 22 52 45 4c 45 41 53 45 22 2c 20 20      "RELEASE",  
2ff6f 20 20 20 20 20 22 54 4f 22 2c 20 20 20 20 20 20       "TO",      
2ff70 20 20 20 20 0a 20 20 22 54 41 42 4c 45 22 2c 20      .  "TABLE", 
2ff71 20 20 20 20 20 20 20 20 22 43 52 45 41 54 45 22          "CREATE"
2ff72 2c 20 20 20 20 20 20 20 20 22 49 46 22 2c 20 20  ,        "IF",  
2ff73 20 20 20 20 20 20 20 20 20 20 22 4e 4f 54 22 2c            "NOT",
2ff74 20 20 20 20 20 20 20 20 20 0a 20 20 22 45 58 49           .  "EXI
2ff75 53 54 53 22 2c 20 20 20 20 20 20 20 20 22 54 45  STS",        "TE
2ff76 4d 50 22 2c 20 20 20 20 20 20 20 20 20 20 22 4c  MP",          "L
2ff77 50 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  P",            "
2ff78 52 50 22 2c 20 20 20 20 20 20 20 20 20 20 0a 20  RP",          . 
2ff79 20 22 41 53 22 2c 20 20 20 20 20 20 20 20 20 20   "AS",          
2ff7a 20 20 22 43 4f 4d 4d 41 22 2c 20 20 20 20 20 20    "COMMA",      
2ff7b 20 20 20 22 49 44 22 2c 20 20 20 20 20 20 20 20     "ID",        
2ff7c 20 20 20 20 22 49 4e 44 45 58 45 44 22 2c 20 20      "INDEXED",  
2ff7d 20 20 20 0a 20 20 22 41 42 4f 52 54 22 2c 20 20     .  "ABORT",  
2ff7e 20 20 20 20 20 20 20 22 41 46 54 45 52 22 2c 20         "AFTER", 
2ff7f 20 20 20 20 20 20 20 20 22 41 4e 41 4c 59 5a 45          "ANALYZE
2ff80 22 2c 20 20 20 20 20 20 20 22 41 53 43 22 2c 20  ",       "ASC", 
2ff81 20 20 20 20 20 20 20 20 0a 20 20 22 41 54 54 41          .  "ATTA
2ff82 43 48 22 2c 20 20 20 20 20 20 20 20 22 42 45 46  CH",        "BEF
2ff83 4f 52 45 22 2c 20 20 20 20 20 20 20 20 22 42 59  ORE",        "BY
2ff84 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 43  ",            "C
2ff85 41 53 43 41 44 45 22 2c 20 20 20 20 20 0a 20 20  ASCADE",     .  
2ff86 22 43 41 53 54 22 2c 20 20 20 20 20 20 20 20 20  "CAST",         
2ff87 20 22 43 4f 4c 55 4d 4e 4b 57 22 2c 20 20 20 20   "COLUMNKW",    
2ff88 20 20 22 43 4f 4e 46 4c 49 43 54 22 2c 20 20 20    "CONFLICT",   
2ff89 20 20 20 22 44 41 54 41 42 41 53 45 22 2c 20 20     "DATABASE",  
2ff8a 20 20 0a 20 20 22 44 45 53 43 22 2c 20 20 20 20    .  "DESC",    
2ff8b 20 20 20 20 20 20 22 44 45 54 41 43 48 22 2c 20        "DETACH", 
2ff8c 20 20 20 20 20 20 20 22 45 41 43 48 22 2c 20 20         "EACH",  
2ff8d 20 20 20 20 20 20 20 20 22 46 41 49 4c 22 2c 20          "FAIL", 
2ff8e 20 20 20 20 20 20 20 0a 20 20 22 46 4f 52 22 2c         .  "FOR",
2ff8f 20 20 20 20 20 20 20 20 20 20 20 22 49 47 4e 4f             "IGNO
2ff90 52 45 22 2c 20 20 20 20 20 20 20 20 22 49 4e 49  RE",        "INI
2ff91 54 49 41 4c 4c 59 22 2c 20 20 20 20 20 22 49 4e  TIALLY",     "IN
2ff92 53 54 45 41 44 22 2c 20 20 20 20 20 0a 20 20 22  STEAD",     .  "
2ff93 4c 49 4b 45 5f 4b 57 22 2c 20 20 20 20 20 20 20  LIKE_KW",       
2ff94 22 4d 41 54 43 48 22 2c 20 20 20 20 20 20 20 20  "MATCH",        
2ff95 20 22 4b 45 59 22 2c 20 20 20 20 20 20 20 20 20   "KEY",         
2ff96 20 20 22 4f 46 22 2c 20 20 20 20 20 20 20 20 20    "OF",         
2ff97 20 0a 20 20 22 4f 46 46 53 45 54 22 2c 20 20 20   .  "OFFSET",   
2ff98 20 20 20 20 20 22 50 52 41 47 4d 41 22 2c 20 20       "PRAGMA",  
2ff99 20 20 20 20 20 20 22 52 41 49 53 45 22 2c 20 20        "RAISE",  
2ff9a 20 20 20 20 20 20 20 22 52 45 50 4c 41 43 45 22         "REPLACE"
2ff9b 2c 20 20 20 20 20 0a 20 20 22 52 45 53 54 52 49  ,     .  "RESTRI
2ff9c 43 54 22 2c 20 20 20 20 20 20 22 52 4f 57 22 2c  CT",      "ROW",
2ff9d 20 20 20 20 20 20 20 20 20 20 20 22 54 52 49 47             "TRIG
2ff9e 47 45 52 22 2c 20 20 20 20 20 20 20 22 56 41 43  GER",       "VAC
2ff9f 55 55 4d 22 2c 20 20 20 20 20 20 0a 20 20 22 56  UUM",      .  "V
2ffa0 49 45 57 22 2c 20 20 20 20 20 20 20 20 20 20 22  IEW",          "
2ffa1 56 49 52 54 55 41 4c 22 2c 20 20 20 20 20 20 20  VIRTUAL",       
2ffa2 22 52 45 49 4e 44 45 58 22 2c 20 20 20 20 20 20  "REINDEX",      
2ffa3 20 22 52 45 4e 41 4d 45 22 2c 20 20 20 20 20 20   "RENAME",      
2ffa4 0a 20 20 22 43 54 49 4d 45 5f 4b 57 22 2c 20 20  .  "CTIME_KW",  
2ffa5 20 20 20 20 22 41 4e 59 22 2c 20 20 20 20 20 20      "ANY",      
2ffa6 20 20 20 20 20 22 4f 52 22 2c 20 20 20 20 20 20       "OR",      
2ffa7 20 20 20 20 20 20 22 41 4e 44 22 2c 20 20 20 20        "AND",    
2ffa8 20 20 20 20 20 0a 20 20 22 49 53 22 2c 20 20 20       .  "IS",   
2ffa9 20 20 20 20 20 20 20 20 20 22 42 45 54 57 45 45           "BETWEE
2ffaa 4e 22 2c 20 20 20 20 20 20 20 22 49 4e 22 2c 20  N",       "IN", 
2ffab 20 20 20 20 20 20 20 20 20 20 20 22 49 53 4e 55             "ISNU
2ffac 4c 4c 22 2c 20 20 20 20 20 20 0a 20 20 22 4e 4f  LL",      .  "NO
2ffad 54 4e 55 4c 4c 22 2c 20 20 20 20 20 20 20 22 4e  TNULL",       "N
2ffae 45 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  E",            "
2ffaf 45 51 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  EQ",            
2ffb0 22 47 54 22 2c 20 20 20 20 20 20 20 20 20 20 0a  "GT",          .
2ffb1 20 20 22 4c 45 22 2c 20 20 20 20 20 20 20 20 20    "LE",         
2ffb2 20 20 20 22 4c 54 22 2c 20 20 20 20 20 20 20 20     "LT",        
2ffb3 20 20 20 20 22 47 45 22 2c 20 20 20 20 20 20 20      "GE",       
2ffb4 20 20 20 20 20 22 45 53 43 41 50 45 22 2c 20 20       "ESCAPE",  
2ffb5 20 20 20 20 0a 20 20 22 42 49 54 41 4e 44 22 2c      .  "BITAND",
2ffb6 20 20 20 20 20 20 20 20 22 42 49 54 4f 52 22 2c          "BITOR",
2ffb7 20 20 20 20 20 20 20 20 20 22 4c 53 48 49 46 54           "LSHIFT
2ffb8 22 2c 20 20 20 20 20 20 20 20 22 52 53 48 49 46  ",        "RSHIF
2ffb9 54 22 2c 20 20 20 20 20 20 0a 20 20 22 50 4c 55  T",      .  "PLU
2ffba 53 22 2c 20 20 20 20 20 20 20 20 20 20 22 4d 49  S",          "MI
2ffbb 4e 55 53 22 2c 20 20 20 20 20 20 20 20 20 22 53  NUS",         "S
2ffbc 54 41 52 22 2c 20 20 20 20 20 20 20 20 20 20 22  TAR",          "
2ffbd 53 4c 41 53 48 22 2c 20 20 20 20 20 20 20 0a 20  SLASH",       . 
2ffbe 20 22 52 45 4d 22 2c 20 20 20 20 20 20 20 20 20   "REM",         
2ffbf 20 20 22 43 4f 4e 43 41 54 22 2c 20 20 20 20 20    "CONCAT",     
2ffc0 20 20 20 22 43 4f 4c 4c 41 54 45 22 2c 20 20 20     "COLLATE",   
2ffc1 20 20 20 20 22 55 4d 49 4e 55 53 22 2c 20 20 20      "UMINUS",   
2ffc2 20 20 20 0a 20 20 22 55 50 4c 55 53 22 2c 20 20     .  "UPLUS",  
2ffc3 20 20 20 20 20 20 20 22 42 49 54 4e 4f 54 22 2c         "BITNOT",
2ffc4 20 20 20 20 20 20 20 20 22 53 54 52 49 4e 47 22          "STRING"
2ffc5 2c 20 20 20 20 20 20 20 20 22 4a 4f 49 4e 5f 4b  ,        "JOIN_K
2ffc6 57 22 2c 20 20 20 20 20 0a 20 20 22 43 4f 4e 53  W",     .  "CONS
2ffc7 54 52 41 49 4e 54 22 2c 20 20 20 20 22 44 45 46  TRAINT",    "DEF
2ffc8 41 55 4c 54 22 2c 20 20 20 20 20 20 20 22 4e 55  AULT",       "NU
2ffc9 4c 4c 22 2c 20 20 20 20 20 20 20 20 20 20 22 50  LL",          "P
2ffca 52 49 4d 41 52 59 22 2c 20 20 20 20 20 0a 20 20  RIMARY",     .  
2ffcb 22 55 4e 49 51 55 45 22 2c 20 20 20 20 20 20 20  "UNIQUE",       
2ffcc 20 22 43 48 45 43 4b 22 2c 20 20 20 20 20 20 20   "CHECK",       
2ffcd 20 20 22 52 45 46 45 52 45 4e 43 45 53 22 2c 20    "REFERENCES", 
2ffce 20 20 20 22 41 55 54 4f 49 4e 43 52 22 2c 20 20     "AUTOINCR",  
2ffcf 20 20 0a 20 20 22 4f 4e 22 2c 20 20 20 20 20 20    .  "ON",      
2ffd0 20 20 20 20 20 20 22 44 45 4c 45 54 45 22 2c 20        "DELETE", 
2ffd1 20 20 20 20 20 20 20 22 55 50 44 41 54 45 22 2c         "UPDATE",
2ffd2 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 22          "INSERT"
2ffd3 2c 20 20 20 20 20 20 0a 20 20 22 53 45 54 22 2c  ,      .  "SET",
2ffd4 20 20 20 20 20 20 20 20 20 20 20 22 44 45 46 45             "DEFE
2ffd5 52 52 41 42 4c 45 22 2c 20 20 20 20 22 46 4f 52  RRABLE",    "FOR
2ffd6 45 49 47 4e 22 2c 20 20 20 20 20 20 20 22 44 52  EIGN",       "DR
2ffd7 4f 50 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22  OP",        .  "
2ffd8 55 4e 49 4f 4e 22 2c 20 20 20 20 20 20 20 20 20  UNION",         
2ffd9 22 41 4c 4c 22 2c 20 20 20 20 20 20 20 20 20 20  "ALL",          
2ffda 20 22 45 58 43 45 50 54 22 2c 20 20 20 20 20 20   "EXCEPT",      
2ffdb 20 20 22 49 4e 54 45 52 53 45 43 54 22 2c 20 20    "INTERSECT",  
2ffdc 20 0a 20 20 22 53 45 4c 45 43 54 22 2c 20 20 20   .  "SELECT",   
2ffdd 20 20 20 20 20 22 44 49 53 54 49 4e 43 54 22 2c       "DISTINCT",
2ffde 20 20 20 20 20 20 22 44 4f 54 22 2c 20 20 20 20        "DOT",    
2ffdf 20 20 20 20 20 20 20 22 46 52 4f 4d 22 2c 20 20         "FROM",  
2ffe0 20 20 20 20 20 20 0a 20 20 22 4a 4f 49 4e 22 2c        .  "JOIN",
2ffe1 20 20 20 20 20 20 20 20 20 20 22 55 53 49 4e 47            "USING
2ffe2 22 2c 20 20 20 20 20 20 20 20 20 22 4f 52 44 45  ",         "ORDE
2ffe3 52 22 2c 20 20 20 20 20 20 20 20 20 22 47 52 4f  R",         "GRO
2ffe4 55 50 22 2c 20 20 20 20 20 20 20 0a 20 20 22 48  UP",       .  "H
2ffe5 41 56 49 4e 47 22 2c 20 20 20 20 20 20 20 20 22  AVING",        "
2ffe6 4c 49 4d 49 54 22 2c 20 20 20 20 20 20 20 20 20  LIMIT",         
2ffe7 22 57 48 45 52 45 22 2c 20 20 20 20 20 20 20 20  "WHERE",        
2ffe8 20 22 49 4e 54 4f 22 2c 20 20 20 20 20 20 20 20   "INTO",        
2ffe9 0a 20 20 22 56 41 4c 55 45 53 22 2c 20 20 20 20  .  "VALUES",    
2ffea 20 20 20 20 22 49 4e 54 45 47 45 52 22 2c 20 20      "INTEGER",  
2ffeb 20 20 20 20 20 22 46 4c 4f 41 54 22 2c 20 20 20       "FLOAT",   
2ffec 20 20 20 20 20 20 22 42 4c 4f 42 22 2c 20 20 20        "BLOB",   
2ffed 20 20 20 20 20 0a 20 20 22 52 45 47 49 53 54 45       .  "REGISTE
2ffee 52 22 2c 20 20 20 20 20 20 22 56 41 52 49 41 42  R",      "VARIAB
2ffef 4c 45 22 2c 20 20 20 20 20 20 22 43 41 53 45 22  LE",      "CASE"
2fff0 2c 20 20 20 20 20 20 20 20 20 20 22 57 48 45 4e  ,          "WHEN
2fff1 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22 54 48  ",        .  "TH
2fff2 45 4e 22 2c 20 20 20 20 20 20 20 20 20 20 22 45  EN",          "E
2fff3 4c 53 45 22 2c 20 20 20 20 20 20 20 20 20 20 22  LSE",          "
2fff4 49 4e 44 45 58 22 2c 20 20 20 20 20 20 20 20 20  INDEX",         
2fff5 22 41 4c 54 45 52 22 2c 20 20 20 20 20 20 20 0a  "ALTER",       .
2fff6 20 20 22 41 44 44 22 2c 20 20 20 20 20 20 20 20    "ADD",        
2fff7 20 20 20 22 65 72 72 6f 72 22 2c 20 20 20 20 20     "error",     
2fff8 20 20 20 20 22 69 6e 70 75 74 22 2c 20 20 20 20      "input",    
2fff9 20 20 20 20 20 22 63 6d 64 6c 69 73 74 22 2c 20       "cmdlist", 
2fffa 20 20 20 20 0a 20 20 22 65 63 6d 64 22 2c 20 20      .  "ecmd",  
2fffb 20 20 20 20 20 20 20 20 22 65 78 70 6c 61 69 6e          "explain
2fffc 22 2c 20 20 20 20 20 20 20 22 63 6d 64 78 22 2c  ",       "cmdx",
2fffd 20 20 20 20 20 20 20 20 20 20 22 63 6d 64 22 2c            "cmd",
2fffe 20 20 20 20 20 20 20 20 20 0a 20 20 22 74 72 61           .  "tra
2ffff 6e 73 74 79 70 65 22 2c 20 20 20 20 20 22 74 72  nstype",     "tr
30000 61 6e 73 5f 6f 70 74 22 2c 20 20 20 20 20 22 6e  ans_opt",     "n
30001 6d 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  m",            "
30002 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 22 2c 0a  savepoint_opt",.
30003 20 20 22 63 72 65 61 74 65 5f 74 61 62 6c 65 22    "create_table"
30004 2c 20 20 22 63 72 65 61 74 65 5f 74 61 62 6c 65  ,  "create_table
30005 5f 61 72 67 73 22 2c 20 20 22 63 72 65 61 74 65  _args",  "create
30006 6b 77 22 2c 20 20 20 20 20 20 22 74 65 6d 70 22  kw",      "temp"
30007 2c 20 20 20 20 20 20 20 20 0a 20 20 22 69 66 6e  ,        .  "ifn
30008 6f 74 65 78 69 73 74 73 22 2c 20 20 20 22 64 62  otexists",   "db
30009 6e 6d 22 2c 20 20 20 20 20 20 20 20 20 20 22 63  nm",          "c
3000a 6f 6c 75 6d 6e 6c 69 73 74 22 2c 20 20 20 20 22  olumnlist",    "
3000b 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 22 2c 0a 20  conslist_opt",. 
3000c 20 22 73 65 6c 65 63 74 22 2c 20 20 20 20 20 20   "select",      
3000d 20 20 22 63 6f 6c 75 6d 6e 22 2c 20 20 20 20 20    "column",     
3000e 20 20 20 22 63 6f 6c 75 6d 6e 69 64 22 2c 20 20     "columnid",  
3000f 20 20 20 20 22 74 79 70 65 22 2c 20 20 20 20 20      "type",     
30010 20 20 20 0a 20 20 22 63 61 72 67 6c 69 73 74 22     .  "carglist"
30011 2c 20 20 20 20 20 20 22 69 64 22 2c 20 20 20 20  ,      "id",    
30012 20 20 20 20 20 20 20 20 22 69 64 73 22 2c 20 20          "ids",  
30013 20 20 20 20 20 20 20 20 20 22 74 79 70 65 74 6f           "typeto
30014 6b 65 6e 22 2c 20 20 20 0a 20 20 22 74 79 70 65  ken",   .  "type
30015 6e 61 6d 65 22 2c 20 20 20 20 20 20 22 73 69 67  name",      "sig
30016 6e 65 64 22 2c 20 20 20 20 20 20 20 20 22 70 6c  ned",        "pl
30017 75 73 5f 6e 75 6d 22 2c 20 20 20 20 20 20 22 6d  us_num",      "m
30018 69 6e 75 73 5f 6e 75 6d 22 2c 20 20 20 0a 20 20  inus_num",   .  
30019 22 63 61 72 67 22 2c 20 20 20 20 20 20 20 20 20  "carg",         
3001a 20 22 63 63 6f 6e 73 22 2c 20 20 20 20 20 20 20   "ccons",       
3001b 20 20 22 74 65 72 6d 22 2c 20 20 20 20 20 20 20    "term",       
3001c 20 20 20 22 65 78 70 72 22 2c 20 20 20 20 20 20     "expr",      
3001d 20 20 0a 20 20 22 6f 6e 63 6f 6e 66 22 2c 20 20    .  "onconf",  
3001e 20 20 20 20 20 20 22 73 6f 72 74 6f 72 64 65 72        "sortorder
3001f 22 2c 20 20 20 20 20 22 61 75 74 6f 69 6e 63 22  ",     "autoinc"
30020 2c 20 20 20 20 20 20 20 22 69 64 78 6c 69 73 74  ,       "idxlist
30021 5f 6f 70 74 22 2c 20 0a 20 20 22 72 65 66 61 72  _opt", .  "refar
30022 67 73 22 2c 20 20 20 20 20 20 20 22 64 65 66 65  gs",       "defe
30023 72 5f 73 75 62 63 6c 61 75 73 65 22 2c 20 20 22  r_subclause",  "
30024 72 65 66 61 72 67 22 2c 20 20 20 20 20 20 20 20  refarg",        
30025 22 72 65 66 61 63 74 22 2c 20 20 20 20 20 20 0a  "refact",      .
30026 20 20 22 69 6e 69 74 5f 64 65 66 65 72 72 65 64    "init_deferred
30027 5f 70 72 65 64 5f 6f 70 74 22 2c 20 20 22 63 6f  _pred_opt",  "co
30028 6e 73 6c 69 73 74 22 2c 20 20 20 20 20 20 22 74  nslist",      "t
30029 63 6f 6e 73 22 2c 20 20 20 20 20 20 20 20 20 22  cons",         "
3002a 69 64 78 6c 69 73 74 22 2c 20 20 20 20 20 0a 20  idxlist",     . 
3002b 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73   "defer_subclaus
3002c 65 5f 6f 70 74 22 2c 20 20 22 6f 72 63 6f 6e 66  e_opt",  "orconf
3002d 22 2c 20 20 20 20 20 20 20 20 22 72 65 73 6f 6c  ",        "resol
3002e 76 65 74 79 70 65 22 2c 20 20 20 22 72 61 69 73  vetype",   "rais
3002f 65 74 79 70 65 22 2c 20 20 20 0a 20 20 22 69 66  etype",   .  "if
30030 65 78 69 73 74 73 22 2c 20 20 20 20 20 20 22 66  exists",      "f
30031 75 6c 6c 6e 61 6d 65 22 2c 20 20 20 20 20 20 22  ullname",      "
30032 6f 6e 65 73 65 6c 65 63 74 22 2c 20 20 20 20 20  oneselect",     
30033 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 22  "multiselect_op"
30034 2c 0a 20 20 22 64 69 73 74 69 6e 63 74 22 2c 20  ,.  "distinct", 
30035 20 20 20 20 20 22 73 65 6c 63 6f 6c 6c 69 73 74       "selcollist
30036 22 2c 20 20 20 20 22 66 72 6f 6d 22 2c 20 20 20  ",    "from",   
30037 20 20 20 20 20 20 20 22 77 68 65 72 65 5f 6f 70         "where_op
30038 74 22 2c 20 20 20 0a 20 20 22 67 72 6f 75 70 62  t",   .  "groupb
30039 79 5f 6f 70 74 22 2c 20 20 20 22 68 61 76 69 6e  y_opt",   "havin
3003a 67 5f 6f 70 74 22 2c 20 20 20 20 22 6f 72 64 65  g_opt",    "orde
3003b 72 62 79 5f 6f 70 74 22 2c 20 20 20 22 6c 69 6d  rby_opt",   "lim
3003c 69 74 5f 6f 70 74 22 2c 20 20 20 0a 20 20 22 73  it_opt",   .  "s
3003d 63 6c 70 22 2c 20 20 20 20 20 20 20 20 20 20 22  clp",          "
3003e 61 73 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  as",            
3003f 22 73 65 6c 74 61 62 6c 69 73 74 22 2c 20 20 20  "seltablist",   
30040 20 22 73 74 6c 5f 70 72 65 66 69 78 22 2c 20 20   "stl_prefix",  
30041 0a 20 20 22 6a 6f 69 6e 6f 70 22 2c 20 20 20 20  .  "joinop",    
30042 20 20 20 20 22 69 6e 64 65 78 65 64 5f 6f 70 74      "indexed_opt
30043 22 2c 20 20 20 22 6f 6e 5f 6f 70 74 22 2c 20 20  ",   "on_opt",  
30044 20 20 20 20 20 20 22 75 73 69 6e 67 5f 6f 70 74        "using_opt
30045 22 2c 20 20 20 0a 20 20 22 6a 6f 69 6e 6f 70 32  ",   .  "joinop2
30046 22 2c 20 20 20 20 20 20 20 22 69 6e 73 63 6f 6c  ",       "inscol
30047 6c 69 73 74 22 2c 20 20 20 20 22 73 6f 72 74 6c  list",    "sortl
30048 69 73 74 22 2c 20 20 20 20 20 20 22 73 6f 72 74  ist",      "sort
30049 69 74 65 6d 22 2c 20 20 20 20 0a 20 20 22 6e 65  item",    .  "ne
3004a 78 70 72 6c 69 73 74 22 2c 20 20 20 20 20 22 73  xprlist",     "s
3004b 65 74 6c 69 73 74 22 2c 20 20 20 20 20 20 20 22  etlist",       "
3004c 69 6e 73 65 72 74 5f 63 6d 64 22 2c 20 20 20 20  insert_cmd",    
3004d 22 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 22  "inscollist_opt"
3004e 2c 0a 20 20 22 69 74 65 6d 6c 69 73 74 22 2c 20  ,.  "itemlist", 
3004f 20 20 20 20 20 22 65 78 70 72 6c 69 73 74 22 2c       "exprlist",
30050 20 20 20 20 20 20 22 6c 69 6b 65 6f 70 22 2c 20        "likeop", 
30051 20 20 20 20 20 20 20 22 65 73 63 61 70 65 22 2c         "escape",
30052 20 20 20 20 20 20 0a 20 20 22 62 65 74 77 65 65        .  "betwee
30053 6e 5f 6f 70 22 2c 20 20 20 20 22 69 6e 5f 6f 70  n_op",    "in_op
30054 22 2c 20 20 20 20 20 20 20 20 20 22 63 61 73 65  ",         "case
30055 5f 6f 70 65 72 61 6e 64 22 2c 20 20 22 63 61 73  _operand",  "cas
30056 65 5f 65 78 70 72 6c 69 73 74 22 2c 0a 20 20 22  e_exprlist",.  "
30057 63 61 73 65 5f 65 6c 73 65 22 2c 20 20 20 20 20  case_else",     
30058 22 75 6e 69 71 75 65 66 6c 61 67 22 2c 20 20 20  "uniqueflag",   
30059 20 22 63 6f 6c 6c 61 74 65 22 2c 20 20 20 20 20   "collate",     
3005a 20 20 22 6e 6d 6e 75 6d 22 2c 20 20 20 20 20 20    "nmnum",      
3005b 20 0a 20 20 22 70 6c 75 73 5f 6f 70 74 22 2c 20   .  "plus_opt", 
3005c 20 20 20 20 20 22 6e 75 6d 62 65 72 22 2c 20 20       "number",  
3005d 20 20 20 20 20 20 22 74 72 69 67 67 65 72 5f 64        "trigger_d
3005e 65 63 6c 22 2c 20 20 22 74 72 69 67 67 65 72 5f  ecl",  "trigger_
3005f 63 6d 64 5f 6c 69 73 74 22 2c 0a 20 20 22 74 72  cmd_list",.  "tr
30060 69 67 67 65 72 5f 74 69 6d 65 22 2c 20 20 22 74  igger_time",  "t
30061 72 69 67 67 65 72 5f 65 76 65 6e 74 22 2c 20 20  rigger_event",  
30062 22 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 22  "foreach_clause"
30063 2c 20 20 22 77 68 65 6e 5f 63 6c 61 75 73 65 22  ,  "when_clause"
30064 2c 20 0a 20 20 22 74 72 69 67 67 65 72 5f 63 6d  , .  "trigger_cm
30065 64 22 2c 20 20 20 22 74 72 6e 6d 22 2c 20 20 20  d",   "trnm",   
30066 20 20 20 20 20 20 20 22 74 72 69 64 78 62 79 22         "tridxby"
30067 2c 20 20 20 20 20 20 20 22 64 61 74 61 62 61 73  ,       "databas
30068 65 5f 6b 77 5f 6f 70 74 22 2c 0a 20 20 22 6b 65  e_kw_opt",.  "ke
30069 79 5f 6f 70 74 22 2c 20 20 20 20 20 20 20 22 61  y_opt",       "a
3006a 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61  dd_column_fullna
3006b 6d 65 22 2c 20 20 22 6b 77 63 6f 6c 75 6d 6e 5f  me",  "kwcolumn_
3006c 6f 70 74 22 2c 20 20 22 63 72 65 61 74 65 5f 76  opt",  "create_v
3006d 74 61 62 22 2c 20 0a 20 20 22 76 74 61 62 61 72  tab", .  "vtabar
3006e 67 6c 69 73 74 22 2c 20 20 20 22 76 74 61 62 61  glist",   "vtaba
3006f 72 67 22 2c 20 20 20 20 20 20 20 22 76 74 61 62  rg",       "vtab
30070 61 72 67 74 6f 6b 65 6e 22 2c 20 20 22 6c 70 22  argtoken",  "lp"
30071 2c 20 20 20 20 20 20 20 20 20 20 0a 20 20 22 61  ,          .  "a
30072 6e 79 6c 69 73 74 22 2c 20 20 20 20 20 0a 7d 3b  nylist",     .};
30073 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55  .#endif /* NDEBU
30074 47 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44  G */..#ifndef ND
30075 45 42 55 47 0a 2f 2a 20 46 6f 72 20 74 72 61 63  EBUG./* For trac
30076 69 6e 67 20 72 65 64 75 63 65 20 61 63 74 69 6f  ing reduce actio
30077 6e 73 2c 20 74 68 65 20 6e 61 6d 65 73 20 6f 66  ns, the names of
30078 20 61 6c 6c 20 72 75 6c 65 73 20 61 72 65 20 72   all rules are r
30079 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 73 74 61 74  equired..*/.stat
3007a 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  ic const char *c
3007b 6f 6e 73 74 20 79 79 52 75 6c 65 4e 61 6d 65 5b  onst yyRuleName[
3007c 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 30 20 2a 2f  ] = {. /*   0 */
3007d 20 22 69 6e 70 75 74 20 3a 3a 3d 20 63 6d 64 6c   "input ::= cmdl
3007e 69 73 74 22 2c 0a 20 2f 2a 20 20 20 31 20 2a 2f  ist",. /*   1 */
3007f 20 22 63 6d 64 6c 69 73 74 20 3a 3a 3d 20 63 6d   "cmdlist ::= cm
30080 64 6c 69 73 74 20 65 63 6d 64 22 2c 0a 20 2f 2a  dlist ecmd",. /*
30081 20 20 20 32 20 2a 2f 20 22 63 6d 64 6c 69 73 74     2 */ "cmdlist
30082 20 3a 3a 3d 20 65 63 6d 64 22 2c 0a 20 2f 2a 20   ::= ecmd",. /* 
30083 20 20 33 20 2a 2f 20 22 65 63 6d 64 20 3a 3a 3d    3 */ "ecmd ::=
30084 20 53 45 4d 49 22 2c 0a 20 2f 2a 20 20 20 34 20   SEMI",. /*   4 
30085 2a 2f 20 22 65 63 6d 64 20 3a 3a 3d 20 65 78 70  */ "ecmd ::= exp
30086 6c 61 69 6e 20 63 6d 64 78 20 53 45 4d 49 22 2c  lain cmdx SEMI",
30087 0a 20 2f 2a 20 20 20 35 20 2a 2f 20 22 65 78 70  . /*   5 */ "exp
30088 6c 61 69 6e 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20  lain ::=",. /*  
30089 20 36 20 2a 2f 20 22 65 78 70 6c 61 69 6e 20 3a   6 */ "explain :
3008a 3a 3d 20 45 58 50 4c 41 49 4e 22 2c 0a 20 2f 2a  := EXPLAIN",. /*
3008b 20 20 20 37 20 2a 2f 20 22 65 78 70 6c 61 69 6e     7 */ "explain
3008c 20 3a 3a 3d 20 45 58 50 4c 41 49 4e 20 51 55 45   ::= EXPLAIN QUE
3008d 52 59 20 50 4c 41 4e 22 2c 0a 20 2f 2a 20 20 20  RY PLAN",. /*   
3008e 38 20 2a 2f 20 22 63 6d 64 78 20 3a 3a 3d 20 63  8 */ "cmdx ::= c
3008f 6d 64 22 2c 0a 20 2f 2a 20 20 20 39 20 2a 2f 20  md",. /*   9 */ 
30090 22 63 6d 64 20 3a 3a 3d 20 42 45 47 49 4e 20 74  "cmd ::= BEGIN t
30091 72 61 6e 73 74 79 70 65 20 74 72 61 6e 73 5f 6f  ranstype trans_o
30092 70 74 22 2c 0a 20 2f 2a 20 20 31 30 20 2a 2f 20  pt",. /*  10 */ 
30093 22 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 22 2c  "trans_opt ::=",
30094 0a 20 2f 2a 20 20 31 31 20 2a 2f 20 22 74 72 61  . /*  11 */ "tra
30095 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53  ns_opt ::= TRANS
30096 41 43 54 49 4f 4e 22 2c 0a 20 2f 2a 20 20 31 32  ACTION",. /*  12
30097 20 2a 2f 20 22 74 72 61 6e 73 5f 6f 70 74 20 3a   */ "trans_opt :
30098 3a 3d 20 54 52 41 4e 53 41 43 54 49 4f 4e 20 6e  := TRANSACTION n
30099 6d 22 2c 0a 20 2f 2a 20 20 31 33 20 2a 2f 20 22  m",. /*  13 */ "
3009a 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 22 2c 0a  transtype ::=",.
3009b 20 2f 2a 20 20 31 34 20 2a 2f 20 22 74 72 61 6e   /*  14 */ "tran
3009c 73 74 79 70 65 20 3a 3a 3d 20 44 45 46 45 52 52  stype ::= DEFERR
3009d 45 44 22 2c 0a 20 2f 2a 20 20 31 35 20 2a 2f 20  ED",. /*  15 */ 
3009e 22 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 49  "transtype ::= I
3009f 4d 4d 45 44 49 41 54 45 22 2c 0a 20 2f 2a 20 20  MMEDIATE",. /*  
300a0 31 36 20 2a 2f 20 22 74 72 61 6e 73 74 79 70 65  16 */ "transtype
300a1 20 3a 3a 3d 20 45 58 43 4c 55 53 49 56 45 22 2c   ::= EXCLUSIVE",
300a2 0a 20 2f 2a 20 20 31 37 20 2a 2f 20 22 63 6d 64  . /*  17 */ "cmd
300a3 20 3a 3a 3d 20 43 4f 4d 4d 49 54 20 74 72 61 6e   ::= COMMIT tran
300a4 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 31 38 20  s_opt",. /*  18 
300a5 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 45 4e 44 20  */ "cmd ::= END 
300a6 74 72 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20  trans_opt",. /* 
300a7 20 31 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20   19 */ "cmd ::= 
300a8 52 4f 4c 4c 42 41 43 4b 20 74 72 61 6e 73 5f 6f  ROLLBACK trans_o
300a9 70 74 22 2c 0a 20 2f 2a 20 20 32 30 20 2a 2f 20  pt",. /*  20 */ 
300aa 22 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 3a  "savepoint_opt :
300ab 3a 3d 20 53 41 56 45 50 4f 49 4e 54 22 2c 0a 20  := SAVEPOINT",. 
300ac 2f 2a 20 20 32 31 20 2a 2f 20 22 73 61 76 65 70  /*  21 */ "savep
300ad 6f 69 6e 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  oint_opt ::=",. 
300ae 2f 2a 20 20 32 32 20 2a 2f 20 22 63 6d 64 20 3a  /*  22 */ "cmd :
300af 3a 3d 20 53 41 56 45 50 4f 49 4e 54 20 6e 6d 22  := SAVEPOINT nm"
300b0 2c 0a 20 2f 2a 20 20 32 33 20 2a 2f 20 22 63 6d  ,. /*  23 */ "cm
300b1 64 20 3a 3a 3d 20 52 45 4c 45 41 53 45 20 73 61  d ::= RELEASE sa
300b2 76 65 70 6f 69 6e 74 5f 6f 70 74 20 6e 6d 22 2c  vepoint_opt nm",
300b3 0a 20 2f 2a 20 20 32 34 20 2a 2f 20 22 63 6d 64  . /*  24 */ "cmd
300b4 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20 74 72   ::= ROLLBACK tr
300b5 61 6e 73 5f 6f 70 74 20 54 4f 20 73 61 76 65 70  ans_opt TO savep
300b6 6f 69 6e 74 5f 6f 70 74 20 6e 6d 22 2c 0a 20 2f  oint_opt nm",. /
300b7 2a 20 20 32 35 20 2a 2f 20 22 63 6d 64 20 3a 3a  *  25 */ "cmd ::
300b8 3d 20 63 72 65 61 74 65 5f 74 61 62 6c 65 20 63  = create_table c
300b9 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72 67 73  reate_table_args
300ba 22 2c 0a 20 2f 2a 20 20 32 36 20 2a 2f 20 22 63  ",. /*  26 */ "c
300bb 72 65 61 74 65 5f 74 61 62 6c 65 20 3a 3a 3d 20  reate_table ::= 
300bc 63 72 65 61 74 65 6b 77 20 74 65 6d 70 20 54 41  createkw temp TA
300bd 42 4c 45 20 69 66 6e 6f 74 65 78 69 73 74 73 20  BLE ifnotexists 
300be 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20 20 32  nm dbnm",. /*  2
300bf 37 20 2a 2f 20 22 63 72 65 61 74 65 6b 77 20 3a  7 */ "createkw :
300c0 3a 3d 20 43 52 45 41 54 45 22 2c 0a 20 2f 2a 20  := CREATE",. /* 
300c1 20 32 38 20 2a 2f 20 22 69 66 6e 6f 74 65 78 69   28 */ "ifnotexi
300c2 73 74 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 32  sts ::=",. /*  2
300c3 39 20 2a 2f 20 22 69 66 6e 6f 74 65 78 69 73 74  9 */ "ifnotexist
300c4 73 20 3a 3a 3d 20 49 46 20 4e 4f 54 20 45 58 49  s ::= IF NOT EXI
300c5 53 54 53 22 2c 0a 20 2f 2a 20 20 33 30 20 2a 2f  STS",. /*  30 */
300c6 20 22 74 65 6d 70 20 3a 3a 3d 20 54 45 4d 50 22   "temp ::= TEMP"
300c7 2c 0a 20 2f 2a 20 20 33 31 20 2a 2f 20 22 74 65  ,. /*  31 */ "te
300c8 6d 70 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 33 32  mp ::=",. /*  32
300c9 20 2a 2f 20 22 63 72 65 61 74 65 5f 74 61 62 6c   */ "create_tabl
300ca 65 5f 61 72 67 73 20 3a 3a 3d 20 4c 50 20 63 6f  e_args ::= LP co
300cb 6c 75 6d 6e 6c 69 73 74 20 63 6f 6e 73 6c 69 73  lumnlist conslis
300cc 74 5f 6f 70 74 20 52 50 22 2c 0a 20 2f 2a 20 20  t_opt RP",. /*  
300cd 33 33 20 2a 2f 20 22 63 72 65 61 74 65 5f 74 61  33 */ "create_ta
300ce 62 6c 65 5f 61 72 67 73 20 3a 3a 3d 20 41 53 20  ble_args ::= AS 
300cf 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 20 33 34  select",. /*  34
300d0 20 2a 2f 20 22 63 6f 6c 75 6d 6e 6c 69 73 74 20   */ "columnlist 
300d1 3a 3a 3d 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 43  ::= columnlist C
300d2 4f 4d 4d 41 20 63 6f 6c 75 6d 6e 22 2c 0a 20 2f  OMMA column",. /
300d3 2a 20 20 33 35 20 2a 2f 20 22 63 6f 6c 75 6d 6e  *  35 */ "column
300d4 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 22  list ::= column"
300d5 2c 0a 20 2f 2a 20 20 33 36 20 2a 2f 20 22 63 6f  ,. /*  36 */ "co
300d6 6c 75 6d 6e 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 69  lumn ::= columni
300d7 64 20 74 79 70 65 20 63 61 72 67 6c 69 73 74 22  d type carglist"
300d8 2c 0a 20 2f 2a 20 20 33 37 20 2a 2f 20 22 63 6f  ,. /*  37 */ "co
300d9 6c 75 6d 6e 69 64 20 3a 3a 3d 20 6e 6d 22 2c 0a  lumnid ::= nm",.
300da 20 2f 2a 20 20 33 38 20 2a 2f 20 22 69 64 20 3a   /*  38 */ "id :
300db 3a 3d 20 49 44 22 2c 0a 20 2f 2a 20 20 33 39 20  := ID",. /*  39 
300dc 2a 2f 20 22 69 64 20 3a 3a 3d 20 49 4e 44 45 58  */ "id ::= INDEX
300dd 45 44 22 2c 0a 20 2f 2a 20 20 34 30 20 2a 2f 20  ED",. /*  40 */ 
300de 22 69 64 73 20 3a 3a 3d 20 49 44 7c 53 54 52 49  "ids ::= ID|STRI
300df 4e 47 22 2c 0a 20 2f 2a 20 20 34 31 20 2a 2f 20  NG",. /*  41 */ 
300e0 22 6e 6d 20 3a 3a 3d 20 69 64 22 2c 0a 20 2f 2a  "nm ::= id",. /*
300e1 20 20 34 32 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20    42 */ "nm ::= 
300e2 53 54 52 49 4e 47 22 2c 0a 20 2f 2a 20 20 34 33  STRING",. /*  43
300e3 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 4a 4f 49 4e   */ "nm ::= JOIN
300e4 5f 4b 57 22 2c 0a 20 2f 2a 20 20 34 34 20 2a 2f  _KW",. /*  44 */
300e5 20 22 74 79 70 65 20 3a 3a 3d 22 2c 0a 20 2f 2a   "type ::=",. /*
300e6 20 20 34 35 20 2a 2f 20 22 74 79 70 65 20 3a 3a    45 */ "type ::
300e7 3d 20 74 79 70 65 74 6f 6b 65 6e 22 2c 0a 20 2f  = typetoken",. /
300e8 2a 20 20 34 36 20 2a 2f 20 22 74 79 70 65 74 6f  *  46 */ "typeto
300e9 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65  ken ::= typename
300ea 22 2c 0a 20 2f 2a 20 20 34 37 20 2a 2f 20 22 74  ",. /*  47 */ "t
300eb 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70  ypetoken ::= typ
300ec 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65 64 20  ename LP signed 
300ed 52 50 22 2c 0a 20 2f 2a 20 20 34 38 20 2a 2f 20  RP",. /*  48 */ 
300ee 22 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74  "typetoken ::= t
300ef 79 70 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65  ypename LP signe
300f0 64 20 43 4f 4d 4d 41 20 73 69 67 6e 65 64 20 52  d COMMA signed R
300f1 50 22 2c 0a 20 2f 2a 20 20 34 39 20 2a 2f 20 22  P",. /*  49 */ "
300f2 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20 69 64 73  typename ::= ids
300f3 22 2c 0a 20 2f 2a 20 20 35 30 20 2a 2f 20 22 74  ",. /*  50 */ "t
300f4 79 70 65 6e 61 6d 65 20 3a 3a 3d 20 74 79 70 65  ypename ::= type
300f5 6e 61 6d 65 20 69 64 73 22 2c 0a 20 2f 2a 20 20  name ids",. /*  
300f6 35 31 20 2a 2f 20 22 73 69 67 6e 65 64 20 3a 3a  51 */ "signed ::
300f7 3d 20 70 6c 75 73 5f 6e 75 6d 22 2c 0a 20 2f 2a  = plus_num",. /*
300f8 20 20 35 32 20 2a 2f 20 22 73 69 67 6e 65 64 20    52 */ "signed 
300f9 3a 3a 3d 20 6d 69 6e 75 73 5f 6e 75 6d 22 2c 0a  ::= minus_num",.
300fa 20 2f 2a 20 20 35 33 20 2a 2f 20 22 63 61 72 67   /*  53 */ "carg
300fb 6c 69 73 74 20 3a 3a 3d 20 63 61 72 67 6c 69 73  list ::= carglis
300fc 74 20 63 61 72 67 22 2c 0a 20 2f 2a 20 20 35 34  t carg",. /*  54
300fd 20 2a 2f 20 22 63 61 72 67 6c 69 73 74 20 3a 3a   */ "carglist ::
300fe 3d 22 2c 0a 20 2f 2a 20 20 35 35 20 2a 2f 20 22  =",. /*  55 */ "
300ff 63 61 72 67 20 3a 3a 3d 20 43 4f 4e 53 54 52 41  carg ::= CONSTRA
30100 49 4e 54 20 6e 6d 20 63 63 6f 6e 73 22 2c 0a 20  INT nm ccons",. 
30101 2f 2a 20 20 35 36 20 2a 2f 20 22 63 61 72 67 20  /*  56 */ "carg 
30102 3a 3a 3d 20 63 63 6f 6e 73 22 2c 0a 20 2f 2a 20  ::= ccons",. /* 
30103 20 35 37 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a   57 */ "ccons ::
30104 3d 20 44 45 46 41 55 4c 54 20 74 65 72 6d 22 2c  = DEFAULT term",
30105 0a 20 2f 2a 20 20 35 38 20 2a 2f 20 22 63 63 6f  . /*  58 */ "cco
30106 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 4c  ns ::= DEFAULT L
30107 50 20 65 78 70 72 20 52 50 22 2c 0a 20 2f 2a 20  P expr RP",. /* 
30108 20 35 39 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a   59 */ "ccons ::
30109 3d 20 44 45 46 41 55 4c 54 20 50 4c 55 53 20 74  = DEFAULT PLUS t
3010a 65 72 6d 22 2c 0a 20 2f 2a 20 20 36 30 20 2a 2f  erm",. /*  60 */
3010b 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41   "ccons ::= DEFA
3010c 55 4c 54 20 4d 49 4e 55 53 20 74 65 72 6d 22 2c  ULT MINUS term",
3010d 0a 20 2f 2a 20 20 36 31 20 2a 2f 20 22 63 63 6f  . /*  61 */ "cco
3010e 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 69  ns ::= DEFAULT i
3010f 64 22 2c 0a 20 2f 2a 20 20 36 32 20 2a 2f 20 22  d",. /*  62 */ "
30110 63 63 6f 6e 73 20 3a 3a 3d 20 4e 55 4c 4c 20 6f  ccons ::= NULL o
30111 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 36 33 20  nconf",. /*  63 
30112 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 4e 4f  */ "ccons ::= NO
30113 54 20 4e 55 4c 4c 20 6f 6e 63 6f 6e 66 22 2c 0a  T NULL onconf",.
30114 20 2f 2a 20 20 36 34 20 2a 2f 20 22 63 63 6f 6e   /*  64 */ "ccon
30115 73 20 3a 3a 3d 20 50 52 49 4d 41 52 59 20 4b 45  s ::= PRIMARY KE
30116 59 20 73 6f 72 74 6f 72 64 65 72 20 6f 6e 63 6f  Y sortorder onco
30117 6e 66 20 61 75 74 6f 69 6e 63 22 2c 0a 20 2f 2a  nf autoinc",. /*
30118 20 20 36 35 20 2a 2f 20 22 63 63 6f 6e 73 20 3a    65 */ "ccons :
30119 3a 3d 20 55 4e 49 51 55 45 20 6f 6e 63 6f 6e 66  := UNIQUE onconf
3011a 22 2c 0a 20 2f 2a 20 20 36 36 20 2a 2f 20 22 63  ",. /*  66 */ "c
3011b 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43 4b 20 4c  cons ::= CHECK L
3011c 50 20 65 78 70 72 20 52 50 22 2c 0a 20 2f 2a 20  P expr RP",. /* 
3011d 20 36 37 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a   67 */ "ccons ::
3011e 3d 20 52 45 46 45 52 45 4e 43 45 53 20 6e 6d 20  = REFERENCES nm 
3011f 69 64 78 6c 69 73 74 5f 6f 70 74 20 72 65 66 61  idxlist_opt refa
30120 72 67 73 22 2c 0a 20 2f 2a 20 20 36 38 20 2a 2f  rgs",. /*  68 */
30121 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 64 65 66 65   "ccons ::= defe
30122 72 5f 73 75 62 63 6c 61 75 73 65 22 2c 0a 20 2f  r_subclause",. /
30123 2a 20 20 36 39 20 2a 2f 20 22 63 63 6f 6e 73 20  *  69 */ "ccons 
30124 3a 3a 3d 20 43 4f 4c 4c 41 54 45 20 69 64 73 22  ::= COLLATE ids"
30125 2c 0a 20 2f 2a 20 20 37 30 20 2a 2f 20 22 61 75  ,. /*  70 */ "au
30126 74 6f 69 6e 63 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  toinc ::=",. /* 
30127 20 37 31 20 2a 2f 20 22 61 75 74 6f 69 6e 63 20   71 */ "autoinc 
30128 3a 3a 3d 20 41 55 54 4f 49 4e 43 52 22 2c 0a 20  ::= AUTOINCR",. 
30129 2f 2a 20 20 37 32 20 2a 2f 20 22 72 65 66 61 72  /*  72 */ "refar
3012a 67 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 37 33  gs ::=",. /*  73
3012b 20 2a 2f 20 22 72 65 66 61 72 67 73 20 3a 3a 3d   */ "refargs ::=
3012c 20 72 65 66 61 72 67 73 20 72 65 66 61 72 67 22   refargs refarg"
3012d 2c 0a 20 2f 2a 20 20 37 34 20 2a 2f 20 22 72 65  ,. /*  74 */ "re
3012e 66 61 72 67 20 3a 3a 3d 20 4d 41 54 43 48 20 6e  farg ::= MATCH n
3012f 6d 22 2c 0a 20 2f 2a 20 20 37 35 20 2a 2f 20 22  m",. /*  75 */ "
30130 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 44 45  refarg ::= ON DE
30131 4c 45 54 45 20 72 65 66 61 63 74 22 2c 0a 20 2f  LETE refact",. /
30132 2a 20 20 37 36 20 2a 2f 20 22 72 65 66 61 72 67  *  76 */ "refarg
30133 20 3a 3a 3d 20 4f 4e 20 55 50 44 41 54 45 20 72   ::= ON UPDATE r
30134 65 66 61 63 74 22 2c 0a 20 2f 2a 20 20 37 37 20  efact",. /*  77 
30135 2a 2f 20 22 72 65 66 61 72 67 20 3a 3a 3d 20 4f  */ "refarg ::= O
30136 4e 20 49 4e 53 45 52 54 20 72 65 66 61 63 74 22  N INSERT refact"
30137 2c 0a 20 2f 2a 20 20 37 38 20 2a 2f 20 22 72 65  ,. /*  78 */ "re
30138 66 61 63 74 20 3a 3a 3d 20 53 45 54 20 4e 55 4c  fact ::= SET NUL
30139 4c 22 2c 0a 20 2f 2a 20 20 37 39 20 2a 2f 20 22  L",. /*  79 */ "
3013a 72 65 66 61 63 74 20 3a 3a 3d 20 53 45 54 20 44  refact ::= SET D
3013b 45 46 41 55 4c 54 22 2c 0a 20 2f 2a 20 20 38 30  EFAULT",. /*  80
3013c 20 2a 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d 20   */ "refact ::= 
3013d 43 41 53 43 41 44 45 22 2c 0a 20 2f 2a 20 20 38  CASCADE",. /*  8
3013e 31 20 2a 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d  1 */ "refact ::=
3013f 20 52 45 53 54 52 49 43 54 22 2c 0a 20 2f 2a 20   RESTRICT",. /* 
30140 20 38 32 20 2a 2f 20 22 64 65 66 65 72 5f 73 75   82 */ "defer_su
30141 62 63 6c 61 75 73 65 20 3a 3a 3d 20 4e 4f 54 20  bclause ::= NOT 
30142 44 45 46 45 52 52 41 42 4c 45 20 69 6e 69 74 5f  DEFERRABLE init_
30143 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70  deferred_pred_op
30144 74 22 2c 0a 20 2f 2a 20 20 38 33 20 2a 2f 20 22  t",. /*  83 */ "
30145 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20  defer_subclause 
30146 3a 3a 3d 20 44 45 46 45 52 52 41 42 4c 45 20 69  ::= DEFERRABLE i
30147 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65  nit_deferred_pre
30148 64 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 38 34 20  d_opt",. /*  84 
30149 2a 2f 20 22 69 6e 69 74 5f 64 65 66 65 72 72 65  */ "init_deferre
3014a 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 22 2c  d_pred_opt ::=",
3014b 0a 20 2f 2a 20 20 38 35 20 2a 2f 20 22 69 6e 69  . /*  85 */ "ini
3014c 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f  t_deferred_pred_
3014d 6f 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c  opt ::= INITIALL
3014e 59 20 44 45 46 45 52 52 45 44 22 2c 0a 20 2f 2a  Y DEFERRED",. /*
3014f 20 20 38 36 20 2a 2f 20 22 69 6e 69 74 5f 64 65    86 */ "init_de
30150 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20  ferred_pred_opt 
30151 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d  ::= INITIALLY IM
30152 4d 45 44 49 41 54 45 22 2c 0a 20 2f 2a 20 20 38  MEDIATE",. /*  8
30153 37 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 5f 6f  7 */ "conslist_o
30154 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 38 38  pt ::=",. /*  88
30155 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 5f 6f 70   */ "conslist_op
30156 74 20 3a 3a 3d 20 43 4f 4d 4d 41 20 63 6f 6e 73  t ::= COMMA cons
30157 6c 69 73 74 22 2c 0a 20 2f 2a 20 20 38 39 20 2a  list",. /*  89 *
30158 2f 20 22 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20  / "conslist ::= 
30159 63 6f 6e 73 6c 69 73 74 20 43 4f 4d 4d 41 20 74  conslist COMMA t
3015a 63 6f 6e 73 22 2c 0a 20 2f 2a 20 20 39 30 20 2a  cons",. /*  90 *
3015b 2f 20 22 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20  / "conslist ::= 
3015c 63 6f 6e 73 6c 69 73 74 20 74 63 6f 6e 73 22 2c  conslist tcons",
3015d 0a 20 2f 2a 20 20 39 31 20 2a 2f 20 22 63 6f 6e  . /*  91 */ "con
3015e 73 6c 69 73 74 20 3a 3a 3d 20 74 63 6f 6e 73 22  slist ::= tcons"
3015f 2c 0a 20 2f 2a 20 20 39 32 20 2a 2f 20 22 74 63  ,. /*  92 */ "tc
30160 6f 6e 73 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49  ons ::= CONSTRAI
30161 4e 54 20 6e 6d 22 2c 0a 20 2f 2a 20 20 39 33 20  NT nm",. /*  93 
30162 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 50 52  */ "tcons ::= PR
30163 49 4d 41 52 59 20 4b 45 59 20 4c 50 20 69 64 78  IMARY KEY LP idx
30164 6c 69 73 74 20 61 75 74 6f 69 6e 63 20 52 50 20  list autoinc RP 
30165 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 39 34  onconf",. /*  94
30166 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 55   */ "tcons ::= U
30167 4e 49 51 55 45 20 4c 50 20 69 64 78 6c 69 73 74  NIQUE LP idxlist
30168 20 52 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a   RP onconf",. /*
30169 20 20 39 35 20 2a 2f 20 22 74 63 6f 6e 73 20 3a    95 */ "tcons :
3016a 3a 3d 20 43 48 45 43 4b 20 4c 50 20 65 78 70 72  := CHECK LP expr
3016b 20 52 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a   RP onconf",. /*
3016c 20 20 39 36 20 2a 2f 20 22 74 63 6f 6e 73 20 3a    96 */ "tcons :
3016d 3a 3d 20 46 4f 52 45 49 47 4e 20 4b 45 59 20 4c  := FOREIGN KEY L
3016e 50 20 69 64 78 6c 69 73 74 20 52 50 20 52 45 46  P idxlist RP REF
3016f 45 52 45 4e 43 45 53 20 6e 6d 20 69 64 78 6c 69  ERENCES nm idxli
30170 73 74 5f 6f 70 74 20 72 65 66 61 72 67 73 20 64  st_opt refargs d
30171 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f  efer_subclause_o
30172 70 74 22 2c 0a 20 2f 2a 20 20 39 37 20 2a 2f 20  pt",. /*  97 */ 
30173 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65  "defer_subclause
30174 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20  _opt ::=",. /*  
30175 39 38 20 2a 2f 20 22 64 65 66 65 72 5f 73 75 62  98 */ "defer_sub
30176 63 6c 61 75 73 65 5f 6f 70 74 20 3a 3a 3d 20 64  clause_opt ::= d
30177 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 22 2c  efer_subclause",
30178 0a 20 2f 2a 20 20 39 39 20 2a 2f 20 22 6f 6e 63  . /*  99 */ "onc
30179 6f 6e 66 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 30  onf ::=",. /* 10
3017a 30 20 2a 2f 20 22 6f 6e 63 6f 6e 66 20 3a 3a 3d  0 */ "onconf ::=
3017b 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 72 65 73   ON CONFLICT res
3017c 6f 6c 76 65 74 79 70 65 22 2c 0a 20 2f 2a 20 31  olvetype",. /* 1
3017d 30 31 20 2a 2f 20 22 6f 72 63 6f 6e 66 20 3a 3a  01 */ "orconf ::
3017e 3d 22 2c 0a 20 2f 2a 20 31 30 32 20 2a 2f 20 22  =",. /* 102 */ "
3017f 6f 72 63 6f 6e 66 20 3a 3a 3d 20 4f 52 20 72 65  orconf ::= OR re
30180 73 6f 6c 76 65 74 79 70 65 22 2c 0a 20 2f 2a 20  solvetype",. /* 
30181 31 30 33 20 2a 2f 20 22 72 65 73 6f 6c 76 65 74  103 */ "resolvet
30182 79 70 65 20 3a 3a 3d 20 72 61 69 73 65 74 79 70  ype ::= raisetyp
30183 65 22 2c 0a 20 2f 2a 20 31 30 34 20 2a 2f 20 22  e",. /* 104 */ "
30184 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d 20  resolvetype ::= 
30185 49 47 4e 4f 52 45 22 2c 0a 20 2f 2a 20 31 30 35  IGNORE",. /* 105
30186 20 2a 2f 20 22 72 65 73 6f 6c 76 65 74 79 70 65   */ "resolvetype
30187 20 3a 3a 3d 20 52 45 50 4c 41 43 45 22 2c 0a 20   ::= REPLACE",. 
30188 2f 2a 20 31 30 36 20 2a 2f 20 22 63 6d 64 20 3a  /* 106 */ "cmd :
30189 3a 3d 20 44 52 4f 50 20 54 41 42 4c 45 20 69 66  := DROP TABLE if
3018a 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 22  exists fullname"
3018b 2c 0a 20 2f 2a 20 31 30 37 20 2a 2f 20 22 69 66  ,. /* 107 */ "if
3018c 65 78 69 73 74 73 20 3a 3a 3d 20 49 46 20 45 58  exists ::= IF EX
3018d 49 53 54 53 22 2c 0a 20 2f 2a 20 31 30 38 20 2a  ISTS",. /* 108 *
3018e 2f 20 22 69 66 65 78 69 73 74 73 20 3a 3a 3d 22  / "ifexists ::="
3018f 2c 0a 20 2f 2a 20 31 30 39 20 2a 2f 20 22 63 6d  ,. /* 109 */ "cm
30190 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 74  d ::= createkw t
30191 65 6d 70 20 56 49 45 57 20 69 66 6e 6f 74 65 78  emp VIEW ifnotex
30192 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 41 53 20  ists nm dbnm AS 
30193 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 31 30  select",. /* 110
30194 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 52 4f   */ "cmd ::= DRO
30195 50 20 56 49 45 57 20 69 66 65 78 69 73 74 73 20  P VIEW ifexists 
30196 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 31  fullname",. /* 1
30197 31 31 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 73  11 */ "cmd ::= s
30198 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 31 32 20  elect",. /* 112 
30199 2a 2f 20 22 73 65 6c 65 63 74 20 3a 3a 3d 20 6f  */ "select ::= o
3019a 6e 65 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31  neselect",. /* 1
3019b 31 33 20 2a 2f 20 22 73 65 6c 65 63 74 20 3a 3a  13 */ "select ::
3019c 3d 20 73 65 6c 65 63 74 20 6d 75 6c 74 69 73 65  = select multise
3019d 6c 65 63 74 5f 6f 70 20 6f 6e 65 73 65 6c 65 63  lect_op oneselec
3019e 74 22 2c 0a 20 2f 2a 20 31 31 34 20 2a 2f 20 22  t",. /* 114 */ "
3019f 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a  multiselect_op :
301a0 3a 3d 20 55 4e 49 4f 4e 22 2c 0a 20 2f 2a 20 31  := UNION",. /* 1
301a1 31 35 20 2a 2f 20 22 6d 75 6c 74 69 73 65 6c 65  15 */ "multisele
301a2 63 74 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e 20  ct_op ::= UNION 
301a3 41 4c 4c 22 2c 0a 20 2f 2a 20 31 31 36 20 2a 2f  ALL",. /* 116 */
301a4 20 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70   "multiselect_op
301a5 20 3a 3a 3d 20 45 58 43 45 50 54 7c 49 4e 54 45   ::= EXCEPT|INTE
301a6 52 53 45 43 54 22 2c 0a 20 2f 2a 20 31 31 37 20  RSECT",. /* 117 
301a7 2a 2f 20 22 6f 6e 65 73 65 6c 65 63 74 20 3a 3a  */ "oneselect ::
301a8 3d 20 53 45 4c 45 43 54 20 64 69 73 74 69 6e 63  = SELECT distinc
301a9 74 20 73 65 6c 63 6f 6c 6c 69 73 74 20 66 72 6f  t selcollist fro
301aa 6d 20 77 68 65 72 65 5f 6f 70 74 20 67 72 6f 75  m where_opt grou
301ab 70 62 79 5f 6f 70 74 20 68 61 76 69 6e 67 5f 6f  pby_opt having_o
301ac 70 74 20 6f 72 64 65 72 62 79 5f 6f 70 74 20 6c  pt orderby_opt l
301ad 69 6d 69 74 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31  imit_opt",. /* 1
301ae 31 38 20 2a 2f 20 22 64 69 73 74 69 6e 63 74 20  18 */ "distinct 
301af 3a 3a 3d 20 44 49 53 54 49 4e 43 54 22 2c 0a 20  ::= DISTINCT",. 
301b0 2f 2a 20 31 31 39 20 2a 2f 20 22 64 69 73 74 69  /* 119 */ "disti
301b1 6e 63 74 20 3a 3a 3d 20 41 4c 4c 22 2c 0a 20 2f  nct ::= ALL",. /
301b2 2a 20 31 32 30 20 2a 2f 20 22 64 69 73 74 69 6e  * 120 */ "distin
301b3 63 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32 31  ct ::=",. /* 121
301b4 20 2a 2f 20 22 73 63 6c 70 20 3a 3a 3d 20 73 65   */ "sclp ::= se
301b5 6c 63 6f 6c 6c 69 73 74 20 43 4f 4d 4d 41 22 2c  lcollist COMMA",
301b6 0a 20 2f 2a 20 31 32 32 20 2a 2f 20 22 73 63 6c  . /* 122 */ "scl
301b7 70 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32 33 20  p ::=",. /* 123 
301b8 2a 2f 20 22 73 65 6c 63 6f 6c 6c 69 73 74 20 3a  */ "selcollist :
301b9 3a 3d 20 73 63 6c 70 20 65 78 70 72 20 61 73 22  := sclp expr as"
301ba 2c 0a 20 2f 2a 20 31 32 34 20 2a 2f 20 22 73 65  ,. /* 124 */ "se
301bb 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c  lcollist ::= scl
301bc 70 20 53 54 41 52 22 2c 0a 20 2f 2a 20 31 32 35  p STAR",. /* 125
301bd 20 2a 2f 20 22 73 65 6c 63 6f 6c 6c 69 73 74 20   */ "selcollist 
301be 3a 3a 3d 20 73 63 6c 70 20 6e 6d 20 44 4f 54 20  ::= sclp nm DOT 
301bf 53 54 41 52 22 2c 0a 20 2f 2a 20 31 32 36 20 2a  STAR",. /* 126 *
301c0 2f 20 22 61 73 20 3a 3a 3d 20 41 53 20 6e 6d 22  / "as ::= AS nm"
301c1 2c 0a 20 2f 2a 20 31 32 37 20 2a 2f 20 22 61 73  ,. /* 127 */ "as
301c2 20 3a 3a 3d 20 69 64 73 22 2c 0a 20 2f 2a 20 31   ::= ids",. /* 1
301c3 32 38 20 2a 2f 20 22 61 73 20 3a 3a 3d 22 2c 0a  28 */ "as ::=",.
301c4 20 2f 2a 20 31 32 39 20 2a 2f 20 22 66 72 6f 6d   /* 129 */ "from
301c5 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 33 30 20 2a   ::=",. /* 130 *
301c6 2f 20 22 66 72 6f 6d 20 3a 3a 3d 20 46 52 4f 4d  / "from ::= FROM
301c7 20 73 65 6c 74 61 62 6c 69 73 74 22 2c 0a 20 2f   seltablist",. /
301c8 2a 20 31 33 31 20 2a 2f 20 22 73 74 6c 5f 70 72  * 131 */ "stl_pr
301c9 65 66 69 78 20 3a 3a 3d 20 73 65 6c 74 61 62 6c  efix ::= seltabl
301ca 69 73 74 20 6a 6f 69 6e 6f 70 22 2c 0a 20 2f 2a  ist joinop",. /*
301cb 20 31 33 32 20 2a 2f 20 22 73 74 6c 5f 70 72 65   132 */ "stl_pre
301cc 66 69 78 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 33  fix ::=",. /* 13
301cd 33 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69 73 74  3 */ "seltablist
301ce 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20   ::= stl_prefix 
301cf 6e 6d 20 64 62 6e 6d 20 61 73 20 69 6e 64 65 78  nm dbnm as index
301d0 65 64 5f 6f 70 74 20 6f 6e 5f 6f 70 74 20 75 73  ed_opt on_opt us
301d1 69 6e 67 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 33  ing_opt",. /* 13
301d2 34 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69 73 74  4 */ "seltablist
301d3 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20   ::= stl_prefix 
301d4 4c 50 20 73 65 6c 65 63 74 20 52 50 20 61 73 20  LP select RP as 
301d5 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74  on_opt using_opt
301d6 22 2c 0a 20 2f 2a 20 31 33 35 20 2a 2f 20 22 73  ",. /* 135 */ "s
301d7 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20 73 74  eltablist ::= st
301d8 6c 5f 70 72 65 66 69 78 20 4c 50 20 73 65 6c 74  l_prefix LP selt
301d9 61 62 6c 69 73 74 20 52 50 20 61 73 20 6f 6e 5f  ablist RP as on_
301da 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 22 2c 0a  opt using_opt",.
301db 20 2f 2a 20 31 33 36 20 2a 2f 20 22 64 62 6e 6d   /* 136 */ "dbnm
301dc 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 33 37 20 2a   ::=",. /* 137 *
301dd 2f 20 22 64 62 6e 6d 20 3a 3a 3d 20 44 4f 54 20  / "dbnm ::= DOT 
301de 6e 6d 22 2c 0a 20 2f 2a 20 31 33 38 20 2a 2f 20  nm",. /* 138 */ 
301df 22 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 6e 6d  "fullname ::= nm
301e0 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20 31 33 39 20   dbnm",. /* 139 
301e1 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 43  */ "joinop ::= C
301e2 4f 4d 4d 41 7c 4a 4f 49 4e 22 2c 0a 20 2f 2a 20  OMMA|JOIN",. /* 
301e3 31 34 30 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a  140 */ "joinop :
301e4 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 4a 4f 49 4e 22  := JOIN_KW JOIN"
301e5 2c 0a 20 2f 2a 20 31 34 31 20 2a 2f 20 22 6a 6f  ,. /* 141 */ "jo
301e6 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57  inop ::= JOIN_KW
301e7 20 6e 6d 20 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31   nm JOIN",. /* 1
301e8 34 32 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a  42 */ "joinop ::
301e9 3d 20 4a 4f 49 4e 5f 4b 57 20 6e 6d 20 6e 6d 20  = JOIN_KW nm nm 
301ea 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31 34 33 20 2a  JOIN",. /* 143 *
301eb 2f 20 22 6f 6e 5f 6f 70 74 20 3a 3a 3d 20 4f 4e  / "on_opt ::= ON
301ec 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 34 34 20   expr",. /* 144 
301ed 2a 2f 20 22 6f 6e 5f 6f 70 74 20 3a 3a 3d 22 2c  */ "on_opt ::=",
301ee 0a 20 2f 2a 20 31 34 35 20 2a 2f 20 22 69 6e 64  . /* 145 */ "ind
301ef 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  exed_opt ::=",. 
301f0 2f 2a 20 31 34 36 20 2a 2f 20 22 69 6e 64 65 78  /* 146 */ "index
301f1 65 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 44 45 58  ed_opt ::= INDEX
301f2 45 44 20 42 59 20 6e 6d 22 2c 0a 20 2f 2a 20 31  ED BY nm",. /* 1
301f3 34 37 20 2a 2f 20 22 69 6e 64 65 78 65 64 5f 6f  47 */ "indexed_o
301f4 70 74 20 3a 3a 3d 20 4e 4f 54 20 49 4e 44 45 58  pt ::= NOT INDEX
301f5 45 44 22 2c 0a 20 2f 2a 20 31 34 38 20 2a 2f 20  ED",. /* 148 */ 
301f6 22 75 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 55  "using_opt ::= U
301f7 53 49 4e 47 20 4c 50 20 69 6e 73 63 6f 6c 6c 69  SING LP inscolli
301f8 73 74 20 52 50 22 2c 0a 20 2f 2a 20 31 34 39 20  st RP",. /* 149 
301f9 2a 2f 20 22 75 73 69 6e 67 5f 6f 70 74 20 3a 3a  */ "using_opt ::
301fa 3d 22 2c 0a 20 2f 2a 20 31 35 30 20 2a 2f 20 22  =",. /* 150 */ "
301fb 6f 72 64 65 72 62 79 5f 6f 70 74 20 3a 3a 3d 22  orderby_opt ::="
301fc 2c 0a 20 2f 2a 20 31 35 31 20 2a 2f 20 22 6f 72  ,. /* 151 */ "or
301fd 64 65 72 62 79 5f 6f 70 74 20 3a 3a 3d 20 4f 52  derby_opt ::= OR
301fe 44 45 52 20 42 59 20 73 6f 72 74 6c 69 73 74 22  DER BY sortlist"
301ff 2c 0a 20 2f 2a 20 31 35 32 20 2a 2f 20 22 73 6f  ,. /* 152 */ "so
30200 72 74 6c 69 73 74 20 3a 3a 3d 20 73 6f 72 74 6c  rtlist ::= sortl
30201 69 73 74 20 43 4f 4d 4d 41 20 73 6f 72 74 69 74  ist COMMA sortit
30202 65 6d 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20  em sortorder",. 
30203 2f 2a 20 31 35 33 20 2a 2f 20 22 73 6f 72 74 6c  /* 153 */ "sortl
30204 69 73 74 20 3a 3a 3d 20 73 6f 72 74 69 74 65 6d  ist ::= sortitem
30205 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f 2a   sortorder",. /*
30206 20 31 35 34 20 2a 2f 20 22 73 6f 72 74 69 74 65   154 */ "sortite
30207 6d 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20 2f 2a  m ::= expr",. /*
30208 20 31 35 35 20 2a 2f 20 22 73 6f 72 74 6f 72 64   155 */ "sortord
30209 65 72 20 3a 3a 3d 20 41 53 43 22 2c 0a 20 2f 2a  er ::= ASC",. /*
3020a 20 31 35 36 20 2a 2f 20 22 73 6f 72 74 6f 72 64   156 */ "sortord
3020b 65 72 20 3a 3a 3d 20 44 45 53 43 22 2c 0a 20 2f  er ::= DESC",. /
3020c 2a 20 31 35 37 20 2a 2f 20 22 73 6f 72 74 6f 72  * 157 */ "sortor
3020d 64 65 72 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35  der ::=",. /* 15
3020e 38 20 2a 2f 20 22 67 72 6f 75 70 62 79 5f 6f 70  8 */ "groupby_op
3020f 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35 39 20  t ::=",. /* 159 
30210 2a 2f 20 22 67 72 6f 75 70 62 79 5f 6f 70 74 20  */ "groupby_opt 
30211 3a 3a 3d 20 47 52 4f 55 50 20 42 59 20 6e 65 78  ::= GROUP BY nex
30212 70 72 6c 69 73 74 22 2c 0a 20 2f 2a 20 31 36 30  prlist",. /* 160
30213 20 2a 2f 20 22 68 61 76 69 6e 67 5f 6f 70 74 20   */ "having_opt 
30214 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 36 31 20 2a 2f  ::=",. /* 161 */
30215 20 22 68 61 76 69 6e 67 5f 6f 70 74 20 3a 3a 3d   "having_opt ::=
30216 20 48 41 56 49 4e 47 20 65 78 70 72 22 2c 0a 20   HAVING expr",. 
30217 2f 2a 20 31 36 32 20 2a 2f 20 22 6c 69 6d 69 74  /* 162 */ "limit
30218 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31  _opt ::=",. /* 1
30219 36 33 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70 74  63 */ "limit_opt
3021a 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72 22   ::= LIMIT expr"
3021b 2c 0a 20 2f 2a 20 31 36 34 20 2a 2f 20 22 6c 69  ,. /* 164 */ "li
3021c 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49  mit_opt ::= LIMI
3021d 54 20 65 78 70 72 20 4f 46 46 53 45 54 20 65 78  T expr OFFSET ex
3021e 70 72 22 2c 0a 20 2f 2a 20 31 36 35 20 2a 2f 20  pr",. /* 165 */ 
3021f 22 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c  "limit_opt ::= L
30220 49 4d 49 54 20 65 78 70 72 20 43 4f 4d 4d 41 20  IMIT expr COMMA 
30221 65 78 70 72 22 2c 0a 20 2f 2a 20 31 36 36 20 2a  expr",. /* 166 *
30222 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 45 4c 45 54  / "cmd ::= DELET
30223 45 20 46 52 4f 4d 20 66 75 6c 6c 6e 61 6d 65 20  E FROM fullname 
30224 69 6e 64 65 78 65 64 5f 6f 70 74 20 77 68 65 72  indexed_opt wher
30225 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 36 37 20  e_opt",. /* 167 
30226 2a 2f 20 22 77 68 65 72 65 5f 6f 70 74 20 3a 3a  */ "where_opt ::
30227 3d 22 2c 0a 20 2f 2a 20 31 36 38 20 2a 2f 20 22  =",. /* 168 */ "
30228 77 68 65 72 65 5f 6f 70 74 20 3a 3a 3d 20 57 48  where_opt ::= WH
30229 45 52 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31  ERE expr",. /* 1
3022a 36 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 55  69 */ "cmd ::= U
3022b 50 44 41 54 45 20 6f 72 63 6f 6e 66 20 66 75 6c  PDATE orconf ful
3022c 6c 6e 61 6d 65 20 69 6e 64 65 78 65 64 5f 6f 70  lname indexed_op
3022d 74 20 53 45 54 20 73 65 74 6c 69 73 74 20 77 68  t SET setlist wh
3022e 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 37  ere_opt",. /* 17
3022f 30 20 2a 2f 20 22 73 65 74 6c 69 73 74 20 3a 3a  0 */ "setlist ::
30230 3d 20 73 65 74 6c 69 73 74 20 43 4f 4d 4d 41 20  = setlist COMMA 
30231 6e 6d 20 45 51 20 65 78 70 72 22 2c 0a 20 2f 2a  nm EQ expr",. /*
30232 20 31 37 31 20 2a 2f 20 22 73 65 74 6c 69 73 74   171 */ "setlist
30233 20 3a 3a 3d 20 6e 6d 20 45 51 20 65 78 70 72 22   ::= nm EQ expr"
30234 2c 0a 20 2f 2a 20 31 37 32 20 2a 2f 20 22 63 6d  ,. /* 172 */ "cm
30235 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64  d ::= insert_cmd
30236 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69   INTO fullname i
30237 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 56 41  nscollist_opt VA
30238 4c 55 45 53 20 4c 50 20 69 74 65 6d 6c 69 73 74  LUES LP itemlist
30239 20 52 50 22 2c 0a 20 2f 2a 20 31 37 33 20 2a 2f   RP",. /* 173 */
3023a 20 22 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74   "cmd ::= insert
3023b 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61  _cmd INTO fullna
3023c 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70  me inscollist_op
3023d 74 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31  t select",. /* 1
3023e 37 34 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 69  74 */ "cmd ::= i
3023f 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66  nsert_cmd INTO f
30240 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69  ullname inscolli
30241 73 74 5f 6f 70 74 20 44 45 46 41 55 4c 54 20 56  st_opt DEFAULT V
30242 41 4c 55 45 53 22 2c 0a 20 2f 2a 20 31 37 35 20  ALUES",. /* 175 
30243 2a 2f 20 22 69 6e 73 65 72 74 5f 63 6d 64 20 3a  */ "insert_cmd :
30244 3a 3d 20 49 4e 53 45 52 54 20 6f 72 63 6f 6e 66  := INSERT orconf
30245 22 2c 0a 20 2f 2a 20 31 37 36 20 2a 2f 20 22 69  ",. /* 176 */ "i
30246 6e 73 65 72 74 5f 63 6d 64 20 3a 3a 3d 20 52 45  nsert_cmd ::= RE
30247 50 4c 41 43 45 22 2c 0a 20 2f 2a 20 31 37 37 20  PLACE",. /* 177 
30248 2a 2f 20 22 69 74 65 6d 6c 69 73 74 20 3a 3a 3d  */ "itemlist ::=
30249 20 69 74 65 6d 6c 69 73 74 20 43 4f 4d 4d 41 20   itemlist COMMA 
3024a 65 78 70 72 22 2c 0a 20 2f 2a 20 31 37 38 20 2a  expr",. /* 178 *
3024b 2f 20 22 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20  / "itemlist ::= 
3024c 65 78 70 72 22 2c 0a 20 2f 2a 20 31 37 39 20 2a  expr",. /* 179 *
3024d 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70  / "inscollist_op
3024e 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 38 30 20  t ::=",. /* 180 
3024f 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f  */ "inscollist_o
30250 70 74 20 3a 3a 3d 20 4c 50 20 69 6e 73 63 6f 6c  pt ::= LP inscol
30251 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 31 38  list RP",. /* 18
30252 31 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74  1 */ "inscollist
30253 20 3a 3a 3d 20 69 6e 73 63 6f 6c 6c 69 73 74 20   ::= inscollist 
30254 43 4f 4d 4d 41 20 6e 6d 22 2c 0a 20 2f 2a 20 31  COMMA nm",. /* 1
30255 38 32 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73  82 */ "inscollis
30256 74 20 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a 20 31  t ::= nm",. /* 1
30257 38 33 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  83 */ "expr ::= 
30258 74 65 72 6d 22 2c 0a 20 2f 2a 20 31 38 34 20 2a  term",. /* 184 *
30259 2f 20 22 65 78 70 72 20 3a 3a 3d 20 4c 50 20 65  / "expr ::= LP e
3025a 78 70 72 20 52 50 22 2c 0a 20 2f 2a 20 31 38 35  xpr RP",. /* 185
3025b 20 2a 2f 20 22 74 65 72 6d 20 3a 3a 3d 20 4e 55   */ "term ::= NU
3025c 4c 4c 22 2c 0a 20 2f 2a 20 31 38 36 20 2a 2f 20  LL",. /* 186 */ 
3025d 22 65 78 70 72 20 3a 3a 3d 20 69 64 22 2c 0a 20  "expr ::= id",. 
3025e 2f 2a 20 31 38 37 20 2a 2f 20 22 65 78 70 72 20  /* 187 */ "expr 
3025f 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 22 2c 0a 20 2f  ::= JOIN_KW",. /
30260 2a 20 31 38 38 20 2a 2f 20 22 65 78 70 72 20 3a  * 188 */ "expr :
30261 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 22 2c 0a 20  := nm DOT nm",. 
30262 2f 2a 20 31 38 39 20 2a 2f 20 22 65 78 70 72 20  /* 189 */ "expr 
30263 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 44 4f  ::= nm DOT nm DO
30264 54 20 6e 6d 22 2c 0a 20 2f 2a 20 31 39 30 20 2a  T nm",. /* 190 *
30265 2f 20 22 74 65 72 6d 20 3a 3a 3d 20 49 4e 54 45  / "term ::= INTE
30266 47 45 52 7c 46 4c 4f 41 54 7c 42 4c 4f 42 22 2c  GER|FLOAT|BLOB",
30267 0a 20 2f 2a 20 31 39 31 20 2a 2f 20 22 74 65 72  . /* 191 */ "ter
30268 6d 20 3a 3a 3d 20 53 54 52 49 4e 47 22 2c 0a 20  m ::= STRING",. 
30269 2f 2a 20 31 39 32 20 2a 2f 20 22 65 78 70 72 20  /* 192 */ "expr 
3026a 3a 3a 3d 20 52 45 47 49 53 54 45 52 22 2c 0a 20  ::= REGISTER",. 
3026b 2f 2a 20 31 39 33 20 2a 2f 20 22 65 78 70 72 20  /* 193 */ "expr 
3026c 3a 3a 3d 20 56 41 52 49 41 42 4c 45 22 2c 0a 20  ::= VARIABLE",. 
3026d 2f 2a 20 31 39 34 20 2a 2f 20 22 65 78 70 72 20  /* 194 */ "expr 
3026e 3a 3a 3d 20 65 78 70 72 20 43 4f 4c 4c 41 54 45  ::= expr COLLATE
3026f 20 69 64 73 22 2c 0a 20 2f 2a 20 31 39 35 20 2a   ids",. /* 195 *
30270 2f 20 22 65 78 70 72 20 3a 3a 3d 20 43 41 53 54  / "expr ::= CAST
30271 20 4c 50 20 65 78 70 72 20 41 53 20 74 79 70 65   LP expr AS type
30272 74 6f 6b 65 6e 20 52 50 22 2c 0a 20 2f 2a 20 31  token RP",. /* 1
30273 39 36 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  96 */ "expr ::= 
30274 49 44 20 4c 50 20 64 69 73 74 69 6e 63 74 20 65  ID LP distinct e
30275 78 70 72 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a  xprlist RP",. /*
30276 20 31 39 37 20 2a 2f 20 22 65 78 70 72 20 3a 3a   197 */ "expr ::
30277 3d 20 49 44 20 4c 50 20 53 54 41 52 20 52 50 22  = ID LP STAR RP"
30278 2c 0a 20 2f 2a 20 31 39 38 20 2a 2f 20 22 74 65  ,. /* 198 */ "te
30279 72 6d 20 3a 3a 3d 20 43 54 49 4d 45 5f 4b 57 22  rm ::= CTIME_KW"
3027a 2c 0a 20 2f 2a 20 31 39 39 20 2a 2f 20 22 65 78  ,. /* 199 */ "ex
3027b 70 72 20 3a 3a 3d 20 65 78 70 72 20 41 4e 44 20  pr ::= expr AND 
3027c 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 30 20 2a  expr",. /* 200 *
3027d 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
3027e 20 4f 52 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32   OR expr",. /* 2
3027f 30 31 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  01 */ "expr ::= 
30280 65 78 70 72 20 4c 54 7c 47 54 7c 47 45 7c 4c 45  expr LT|GT|GE|LE
30281 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 32 20   expr",. /* 202 
30282 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70  */ "expr ::= exp
30283 72 20 45 51 7c 4e 45 20 65 78 70 72 22 2c 0a 20  r EQ|NE expr",. 
30284 2f 2a 20 32 30 33 20 2a 2f 20 22 65 78 70 72 20  /* 203 */ "expr 
30285 3a 3a 3d 20 65 78 70 72 20 42 49 54 41 4e 44 7c  ::= expr BITAND|
30286 42 49 54 4f 52 7c 4c 53 48 49 46 54 7c 52 53 48  BITOR|LSHIFT|RSH
30287 49 46 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32  IFT expr",. /* 2
30288 30 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  04 */ "expr ::= 
30289 65 78 70 72 20 50 4c 55 53 7c 4d 49 4e 55 53 20  expr PLUS|MINUS 
3028a 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 35 20 2a  expr",. /* 205 *
3028b 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
3028c 20 53 54 41 52 7c 53 4c 41 53 48 7c 52 45 4d 20   STAR|SLASH|REM 
3028d 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 36 20 2a  expr",. /* 206 *
3028e 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
3028f 20 43 4f 4e 43 41 54 20 65 78 70 72 22 2c 0a 20   CONCAT expr",. 
30290 2f 2a 20 32 30 37 20 2a 2f 20 22 6c 69 6b 65 6f  /* 207 */ "likeo
30291 70 20 3a 3a 3d 20 4c 49 4b 45 5f 4b 57 22 2c 0a  p ::= LIKE_KW",.
30292 20 2f 2a 20 32 30 38 20 2a 2f 20 22 6c 69 6b 65   /* 208 */ "like
30293 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4c 49 4b 45 5f  op ::= NOT LIKE_
30294 4b 57 22 2c 0a 20 2f 2a 20 32 30 39 20 2a 2f 20  KW",. /* 209 */ 
30295 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4d 41 54 43  "likeop ::= MATC
30296 48 22 2c 0a 20 2f 2a 20 32 31 30 20 2a 2f 20 22  H",. /* 210 */ "
30297 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4d  likeop ::= NOT M
30298 41 54 43 48 22 2c 0a 20 2f 2a 20 32 31 31 20 2a  ATCH",. /* 211 *
30299 2f 20 22 65 73 63 61 70 65 20 3a 3a 3d 20 45 53  / "escape ::= ES
3029a 43 41 50 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20  CAPE expr",. /* 
3029b 32 31 32 20 2a 2f 20 22 65 73 63 61 70 65 20 3a  212 */ "escape :
3029c 3a 3d 22 2c 0a 20 2f 2a 20 32 31 33 20 2a 2f 20  :=",. /* 213 */ 
3029d 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 6c  "expr ::= expr l
3029e 69 6b 65 6f 70 20 65 78 70 72 20 65 73 63 61 70  ikeop expr escap
3029f 65 22 2c 0a 20 2f 2a 20 32 31 34 20 2a 2f 20 22  e",. /* 214 */ "
302a0 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53  expr ::= expr IS
302a1 4e 55 4c 4c 7c 4e 4f 54 4e 55 4c 4c 22 2c 0a 20  NULL|NOTNULL",. 
302a2 2f 2a 20 32 31 35 20 2a 2f 20 22 65 78 70 72 20  /* 215 */ "expr 
302a3 3a 3a 3d 20 65 78 70 72 20 49 53 20 4e 55 4c 4c  ::= expr IS NULL
302a4 22 2c 0a 20 2f 2a 20 32 31 36 20 2a 2f 20 22 65  ",. /* 216 */ "e
302a5 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 4e 4f 54  xpr ::= expr NOT
302a6 20 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 32 31 37 20   NULL",. /* 217 
302a7 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70  */ "expr ::= exp
302a8 72 20 49 53 20 4e 4f 54 20 4e 55 4c 4c 22 2c 0a  r IS NOT NULL",.
302a9 20 2f 2a 20 32 31 38 20 2a 2f 20 22 65 78 70 72   /* 218 */ "expr
302aa 20 3a 3a 3d 20 4e 4f 54 20 65 78 70 72 22 2c 0a   ::= NOT expr",.
302ab 20 2f 2a 20 32 31 39 20 2a 2f 20 22 65 78 70 72   /* 219 */ "expr
302ac 20 3a 3a 3d 20 42 49 54 4e 4f 54 20 65 78 70 72   ::= BITNOT expr
302ad 22 2c 0a 20 2f 2a 20 32 32 30 20 2a 2f 20 22 65  ",. /* 220 */ "e
302ae 78 70 72 20 3a 3a 3d 20 4d 49 4e 55 53 20 65 78  xpr ::= MINUS ex
302af 70 72 22 2c 0a 20 2f 2a 20 32 32 31 20 2a 2f 20  pr",. /* 221 */ 
302b0 22 65 78 70 72 20 3a 3a 3d 20 50 4c 55 53 20 65  "expr ::= PLUS e
302b1 78 70 72 22 2c 0a 20 2f 2a 20 32 32 32 20 2a 2f  xpr",. /* 222 */
302b2 20 22 62 65 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d   "between_op ::=
302b3 20 42 45 54 57 45 45 4e 22 2c 0a 20 2f 2a 20 32   BETWEEN",. /* 2
302b4 32 33 20 2a 2f 20 22 62 65 74 77 65 65 6e 5f 6f  23 */ "between_o
302b5 70 20 3a 3a 3d 20 4e 4f 54 20 42 45 54 57 45 45  p ::= NOT BETWEE
302b6 4e 22 2c 0a 20 2f 2a 20 32 32 34 20 2a 2f 20 22  N",. /* 224 */ "
302b7 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 62 65  expr ::= expr be
302b8 74 77 65 65 6e 5f 6f 70 20 65 78 70 72 20 41 4e  tween_op expr AN
302b9 44 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 32 35  D expr",. /* 225
302ba 20 2a 2f 20 22 69 6e 5f 6f 70 20 3a 3a 3d 20 49   */ "in_op ::= I
302bb 4e 22 2c 0a 20 2f 2a 20 32 32 36 20 2a 2f 20 22  N",. /* 226 */ "
302bc 69 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20 49 4e  in_op ::= NOT IN
302bd 22 2c 0a 20 2f 2a 20 32 32 37 20 2a 2f 20 22 65  ",. /* 227 */ "e
302be 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f  xpr ::= expr in_
302bf 6f 70 20 4c 50 20 65 78 70 72 6c 69 73 74 20 52  op LP exprlist R
302c0 50 22 2c 0a 20 2f 2a 20 32 32 38 20 2a 2f 20 22  P",. /* 228 */ "
302c1 65 78 70 72 20 3a 3a 3d 20 4c 50 20 73 65 6c 65  expr ::= LP sele
302c2 63 74 20 52 50 22 2c 0a 20 2f 2a 20 32 32 39 20  ct RP",. /* 229 
302c3 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70  */ "expr ::= exp
302c4 72 20 69 6e 5f 6f 70 20 4c 50 20 73 65 6c 65 63  r in_op LP selec
302c5 74 20 52 50 22 2c 0a 20 2f 2a 20 32 33 30 20 2a  t RP",. /* 230 *
302c6 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
302c7 20 69 6e 5f 6f 70 20 6e 6d 20 64 62 6e 6d 22 2c   in_op nm dbnm",
302c8 0a 20 2f 2a 20 32 33 31 20 2a 2f 20 22 65 78 70  . /* 231 */ "exp
302c9 72 20 3a 3a 3d 20 45 58 49 53 54 53 20 4c 50 20  r ::= EXISTS LP 
302ca 73 65 6c 65 63 74 20 52 50 22 2c 0a 20 2f 2a 20  select RP",. /* 
302cb 32 33 32 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  232 */ "expr ::=
302cc 20 43 41 53 45 20 63 61 73 65 5f 6f 70 65 72 61   CASE case_opera
302cd 6e 64 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74  nd case_exprlist
302ce 20 63 61 73 65 5f 65 6c 73 65 20 45 4e 44 22 2c   case_else END",
302cf 0a 20 2f 2a 20 32 33 33 20 2a 2f 20 22 63 61 73  . /* 233 */ "cas
302d0 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 63  e_exprlist ::= c
302d1 61 73 65 5f 65 78 70 72 6c 69 73 74 20 57 48 45  ase_exprlist WHE
302d2 4e 20 65 78 70 72 20 54 48 45 4e 20 65 78 70 72  N expr THEN expr
302d3 22 2c 0a 20 2f 2a 20 32 33 34 20 2a 2f 20 22 63  ",. /* 234 */ "c
302d4 61 73 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d  ase_exprlist ::=
302d5 20 57 48 45 4e 20 65 78 70 72 20 54 48 45 4e 20   WHEN expr THEN 
302d6 65 78 70 72 22 2c 0a 20 2f 2a 20 32 33 35 20 2a  expr",. /* 235 *
302d7 2f 20 22 63 61 73 65 5f 65 6c 73 65 20 3a 3a 3d  / "case_else ::=
302d8 20 45 4c 53 45 20 65 78 70 72 22 2c 0a 20 2f 2a   ELSE expr",. /*
302d9 20 32 33 36 20 2a 2f 20 22 63 61 73 65 5f 65 6c   236 */ "case_el
302da 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 33 37  se ::=",. /* 237
302db 20 2a 2f 20 22 63 61 73 65 5f 6f 70 65 72 61 6e   */ "case_operan
302dc 64 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20 2f 2a  d ::= expr",. /*
302dd 20 32 33 38 20 2a 2f 20 22 63 61 73 65 5f 6f 70   238 */ "case_op
302de 65 72 61 6e 64 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  erand ::=",. /* 
302df 32 33 39 20 2a 2f 20 22 65 78 70 72 6c 69 73 74  239 */ "exprlist
302e0 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74 22 2c   ::= nexprlist",
302e1 0a 20 2f 2a 20 32 34 30 20 2a 2f 20 22 65 78 70  . /* 240 */ "exp
302e2 72 6c 69 73 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  rlist ::=",. /* 
302e3 32 34 31 20 2a 2f 20 22 6e 65 78 70 72 6c 69 73  241 */ "nexprlis
302e4 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74 20  t ::= nexprlist 
302e5 43 4f 4d 4d 41 20 65 78 70 72 22 2c 0a 20 2f 2a  COMMA expr",. /*
302e6 20 32 34 32 20 2a 2f 20 22 6e 65 78 70 72 6c 69   242 */ "nexprli
302e7 73 74 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20 2f  st ::= expr",. /
302e8 2a 20 32 34 33 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 243 */ "cmd ::
302e9 3d 20 63 72 65 61 74 65 6b 77 20 75 6e 69 71 75  = createkw uniqu
302ea 65 66 6c 61 67 20 49 4e 44 45 58 20 69 66 6e 6f  eflag INDEX ifno
302eb 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20  texists nm dbnm 
302ec 4f 4e 20 6e 6d 20 4c 50 20 69 64 78 6c 69 73 74  ON nm LP idxlist
302ed 20 52 50 22 2c 0a 20 2f 2a 20 32 34 34 20 2a 2f   RP",. /* 244 */
302ee 20 22 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d   "uniqueflag ::=
302ef 20 55 4e 49 51 55 45 22 2c 0a 20 2f 2a 20 32 34   UNIQUE",. /* 24
302f0 35 20 2a 2f 20 22 75 6e 69 71 75 65 66 6c 61 67  5 */ "uniqueflag
302f1 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 34 36 20 2a   ::=",. /* 246 *
302f2 2f 20 22 69 64 78 6c 69 73 74 5f 6f 70 74 20 3a  / "idxlist_opt :
302f3 3a 3d 22 2c 0a 20 2f 2a 20 32 34 37 20 2a 2f 20  :=",. /* 247 */ 
302f4 22 69 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d  "idxlist_opt ::=
302f5 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50 22 2c   LP idxlist RP",
302f6 0a 20 2f 2a 20 32 34 38 20 2a 2f 20 22 69 64 78  . /* 248 */ "idx
302f7 6c 69 73 74 20 3a 3a 3d 20 69 64 78 6c 69 73 74  list ::= idxlist
302f8 20 43 4f 4d 4d 41 20 6e 6d 20 63 6f 6c 6c 61 74   COMMA nm collat
302f9 65 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f  e sortorder",. /
302fa 2a 20 32 34 39 20 2a 2f 20 22 69 64 78 6c 69 73  * 249 */ "idxlis
302fb 74 20 3a 3a 3d 20 6e 6d 20 63 6f 6c 6c 61 74 65  t ::= nm collate
302fc 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f 2a   sortorder",. /*
302fd 20 32 35 30 20 2a 2f 20 22 63 6f 6c 6c 61 74 65   250 */ "collate
302fe 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 35 31 20 2a   ::=",. /* 251 *
302ff 2f 20 22 63 6f 6c 6c 61 74 65 20 3a 3a 3d 20 43  / "collate ::= C
30300 4f 4c 4c 41 54 45 20 69 64 73 22 2c 0a 20 2f 2a  OLLATE ids",. /*
30301 20 32 35 32 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   252 */ "cmd ::=
30302 20 44 52 4f 50 20 49 4e 44 45 58 20 69 66 65 78   DROP INDEX ifex
30303 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a  ists fullname",.
30304 20 2f 2a 20 32 35 33 20 2a 2f 20 22 63 6d 64 20   /* 253 */ "cmd 
30305 3a 3a 3d 20 56 41 43 55 55 4d 22 2c 0a 20 2f 2a  ::= VACUUM",. /*
30306 20 32 35 34 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   254 */ "cmd ::=
30307 20 56 41 43 55 55 4d 20 6e 6d 22 2c 0a 20 2f 2a   VACUUM nm",. /*
30308 20 32 35 35 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   255 */ "cmd ::=
30309 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 22   PRAGMA nm dbnm"
3030a 2c 0a 20 2f 2a 20 32 35 36 20 2a 2f 20 22 63 6d  ,. /* 256 */ "cm
3030b 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20  d ::= PRAGMA nm 
3030c 64 62 6e 6d 20 45 51 20 6e 6d 6e 75 6d 22 2c 0a  dbnm EQ nmnum",.
3030d 20 2f 2a 20 32 35 37 20 2a 2f 20 22 63 6d 64 20   /* 257 */ "cmd 
3030e 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62  ::= PRAGMA nm db
3030f 6e 6d 20 4c 50 20 6e 6d 6e 75 6d 20 52 50 22 2c  nm LP nmnum RP",
30310 0a 20 2f 2a 20 32 35 38 20 2a 2f 20 22 63 6d 64  . /* 258 */ "cmd
30311 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64   ::= PRAGMA nm d
30312 62 6e 6d 20 45 51 20 6d 69 6e 75 73 5f 6e 75 6d  bnm EQ minus_num
30313 22 2c 0a 20 2f 2a 20 32 35 39 20 2a 2f 20 22 63  ",. /* 259 */ "c
30314 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d  md ::= PRAGMA nm
30315 20 64 62 6e 6d 20 4c 50 20 6d 69 6e 75 73 5f 6e   dbnm LP minus_n
30316 75 6d 20 52 50 22 2c 0a 20 2f 2a 20 32 36 30 20  um RP",. /* 260 
30317 2a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a 3d 20 70 6c  */ "nmnum ::= pl
30318 75 73 5f 6e 75 6d 22 2c 0a 20 2f 2a 20 32 36 31  us_num",. /* 261
30319 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a 3d 20 6e   */ "nmnum ::= n
3031a 6d 22 2c 0a 20 2f 2a 20 32 36 32 20 2a 2f 20 22  m",. /* 262 */ "
3031b 6e 6d 6e 75 6d 20 3a 3a 3d 20 4f 4e 22 2c 0a 20  nmnum ::= ON",. 
3031c 2f 2a 20 32 36 33 20 2a 2f 20 22 6e 6d 6e 75 6d  /* 263 */ "nmnum
3031d 20 3a 3a 3d 20 44 45 4c 45 54 45 22 2c 0a 20 2f   ::= DELETE",. /
3031e 2a 20 32 36 34 20 2a 2f 20 22 6e 6d 6e 75 6d 20  * 264 */ "nmnum 
3031f 3a 3a 3d 20 44 45 46 41 55 4c 54 22 2c 0a 20 2f  ::= DEFAULT",. /
30320 2a 20 32 36 35 20 2a 2f 20 22 70 6c 75 73 5f 6e  * 265 */ "plus_n
30321 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6f 70 74 20  um ::= plus_opt 
30322 6e 75 6d 62 65 72 22 2c 0a 20 2f 2a 20 32 36 36  number",. /* 266
30323 20 2a 2f 20 22 6d 69 6e 75 73 5f 6e 75 6d 20 3a   */ "minus_num :
30324 3a 3d 20 4d 49 4e 55 53 20 6e 75 6d 62 65 72 22  := MINUS number"
30325 2c 0a 20 2f 2a 20 32 36 37 20 2a 2f 20 22 6e 75  ,. /* 267 */ "nu
30326 6d 62 65 72 20 3a 3a 3d 20 49 4e 54 45 47 45 52  mber ::= INTEGER
30327 7c 46 4c 4f 41 54 22 2c 0a 20 2f 2a 20 32 36 38  |FLOAT",. /* 268
30328 20 2a 2f 20 22 70 6c 75 73 5f 6f 70 74 20 3a 3a   */ "plus_opt ::
30329 3d 20 50 4c 55 53 22 2c 0a 20 2f 2a 20 32 36 39  = PLUS",. /* 269
3032a 20 2a 2f 20 22 70 6c 75 73 5f 6f 70 74 20 3a 3a   */ "plus_opt ::
3032b 3d 22 2c 0a 20 2f 2a 20 32 37 30 20 2a 2f 20 22  =",. /* 270 */ "
3032c 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77  cmd ::= createkw
3032d 20 74 72 69 67 67 65 72 5f 64 65 63 6c 20 42 45   trigger_decl BE
3032e 47 49 4e 20 74 72 69 67 67 65 72 5f 63 6d 64 5f  GIN trigger_cmd_
3032f 6c 69 73 74 20 45 4e 44 22 2c 0a 20 2f 2a 20 32  list END",. /* 2
30330 37 31 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 64  71 */ "trigger_d
30331 65 63 6c 20 3a 3a 3d 20 74 65 6d 70 20 54 52 49  ecl ::= temp TRI
30332 47 47 45 52 20 69 66 6e 6f 74 65 78 69 73 74 73  GGER ifnotexists
30333 20 6e 6d 20 64 62 6e 6d 20 74 72 69 67 67 65 72   nm dbnm trigger
30334 5f 74 69 6d 65 20 74 72 69 67 67 65 72 5f 65 76  _time trigger_ev
30335 65 6e 74 20 4f 4e 20 66 75 6c 6c 6e 61 6d 65 20  ent ON fullname 
30336 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 20 77  foreach_clause w
30337 68 65 6e 5f 63 6c 61 75 73 65 22 2c 0a 20 2f 2a  hen_clause",. /*
30338 20 32 37 32 20 2a 2f 20 22 74 72 69 67 67 65 72   272 */ "trigger
30339 5f 74 69 6d 65 20 3a 3a 3d 20 42 45 46 4f 52 45  _time ::= BEFORE
3033a 22 2c 0a 20 2f 2a 20 32 37 33 20 2a 2f 20 22 74  ",. /* 273 */ "t
3033b 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20  rigger_time ::= 
3033c 41 46 54 45 52 22 2c 0a 20 2f 2a 20 32 37 34 20  AFTER",. /* 274 
3033d 2a 2f 20 22 74 72 69 67 67 65 72 5f 74 69 6d 65  */ "trigger_time
3033e 20 3a 3a 3d 20 49 4e 53 54 45 41 44 20 4f 46 22   ::= INSTEAD OF"
3033f 2c 0a 20 2f 2a 20 32 37 35 20 2a 2f 20 22 74 72  ,. /* 275 */ "tr
30340 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 22 2c  igger_time ::=",
30341 0a 20 2f 2a 20 32 37 36 20 2a 2f 20 22 74 72 69  . /* 276 */ "tri
30342 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 44  gger_event ::= D
30343 45 4c 45 54 45 7c 49 4e 53 45 52 54 22 2c 0a 20  ELETE|INSERT",. 
30344 2f 2a 20 32 37 37 20 2a 2f 20 22 74 72 69 67 67  /* 277 */ "trigg
30345 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 55 50 44  er_event ::= UPD
30346 41 54 45 22 2c 0a 20 2f 2a 20 32 37 38 20 2a 2f  ATE",. /* 278 */
30347 20 22 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20   "trigger_event 
30348 3a 3a 3d 20 55 50 44 41 54 45 20 4f 46 20 69 6e  ::= UPDATE OF in
30349 73 63 6f 6c 6c 69 73 74 22 2c 0a 20 2f 2a 20 32  scollist",. /* 2
3034a 37 39 20 2a 2f 20 22 66 6f 72 65 61 63 68 5f 63  79 */ "foreach_c
3034b 6c 61 75 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  lause ::=",. /* 
3034c 32 38 30 20 2a 2f 20 22 66 6f 72 65 61 63 68 5f  280 */ "foreach_
3034d 63 6c 61 75 73 65 20 3a 3a 3d 20 46 4f 52 20 45  clause ::= FOR E
3034e 41 43 48 20 52 4f 57 22 2c 0a 20 2f 2a 20 32 38  ACH ROW",. /* 28
3034f 31 20 2a 2f 20 22 77 68 65 6e 5f 63 6c 61 75 73  1 */ "when_claus
30350 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 38 32 20  e ::=",. /* 282 
30351 2a 2f 20 22 77 68 65 6e 5f 63 6c 61 75 73 65 20  */ "when_clause 
30352 3a 3a 3d 20 57 48 45 4e 20 65 78 70 72 22 2c 0a  ::= WHEN expr",.
30353 20 2f 2a 20 32 38 33 20 2a 2f 20 22 74 72 69 67   /* 283 */ "trig
30354 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d  ger_cmd_list ::=
30355 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73   trigger_cmd_lis
30356 74 20 74 72 69 67 67 65 72 5f 63 6d 64 20 53 45  t trigger_cmd SE
30357 4d 49 22 2c 0a 20 2f 2a 20 32 38 34 20 2a 2f 20  MI",. /* 284 */ 
30358 22 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73  "trigger_cmd_lis
30359 74 20 3a 3a 3d 20 74 72 69 67 67 65 72 5f 63 6d  t ::= trigger_cm
3035a 64 20 53 45 4d 49 22 2c 0a 20 2f 2a 20 32 38 35  d SEMI",. /* 285
3035b 20 2a 2f 20 22 74 72 6e 6d 20 3a 3a 3d 20 6e 6d   */ "trnm ::= nm
3035c 22 2c 0a 20 2f 2a 20 32 38 36 20 2a 2f 20 22 74  ",. /* 286 */ "t
3035d 72 6e 6d 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e  rnm ::= nm DOT n
3035e 6d 22 2c 0a 20 2f 2a 20 32 38 37 20 2a 2f 20 22  m",. /* 287 */ "
3035f 74 72 69 64 78 62 79 20 3a 3a 3d 22 2c 0a 20 2f  tridxby ::=",. /
30360 2a 20 32 38 38 20 2a 2f 20 22 74 72 69 64 78 62  * 288 */ "tridxb
30361 79 20 3a 3a 3d 20 49 4e 44 45 58 45 44 20 42 59  y ::= INDEXED BY
30362 20 6e 6d 22 2c 0a 20 2f 2a 20 32 38 39 20 2a 2f   nm",. /* 289 */
30363 20 22 74 72 69 64 78 62 79 20 3a 3a 3d 20 4e 4f   "tridxby ::= NO
30364 54 20 49 4e 44 45 58 45 44 22 2c 0a 20 2f 2a 20  T INDEXED",. /* 
30365 32 39 30 20 2a 2f 20 22 74 72 69 67 67 65 72 5f  290 */ "trigger_
30366 63 6d 64 20 3a 3a 3d 20 55 50 44 41 54 45 20 6f  cmd ::= UPDATE o
30367 72 63 6f 6e 66 20 74 72 6e 6d 20 74 72 69 64 78  rconf trnm tridx
30368 62 79 20 53 45 54 20 73 65 74 6c 69 73 74 20 77  by SET setlist w
30369 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 32  here_opt",. /* 2
3036a 39 31 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63  91 */ "trigger_c
3036b 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d  md ::= insert_cm
3036c 64 20 49 4e 54 4f 20 74 72 6e 6d 20 69 6e 73 63  d INTO trnm insc
3036d 6f 6c 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55 45  ollist_opt VALUE
3036e 53 20 4c 50 20 69 74 65 6d 6c 69 73 74 20 52 50  S LP itemlist RP
3036f 22 2c 0a 20 2f 2a 20 32 39 32 20 2a 2f 20 22 74  ",. /* 292 */ "t
30370 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 69  rigger_cmd ::= i
30371 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 74  nsert_cmd INTO t
30372 72 6e 6d 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f  rnm inscollist_o
30373 70 74 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20  pt select",. /* 
30374 32 39 33 20 2a 2f 20 22 74 72 69 67 67 65 72 5f  293 */ "trigger_
30375 63 6d 64 20 3a 3a 3d 20 44 45 4c 45 54 45 20 46  cmd ::= DELETE F
30376 52 4f 4d 20 74 72 6e 6d 20 74 72 69 64 78 62 79  ROM trnm tridxby
30377 20 77 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a   where_opt",. /*
30378 20 32 39 34 20 2a 2f 20 22 74 72 69 67 67 65 72   294 */ "trigger
30379 5f 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63 74 22  _cmd ::= select"
3037a 2c 0a 20 2f 2a 20 32 39 35 20 2a 2f 20 22 65 78  ,. /* 295 */ "ex
3037b 70 72 20 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20  pr ::= RAISE LP 
3037c 49 47 4e 4f 52 45 20 52 50 22 2c 0a 20 2f 2a 20  IGNORE RP",. /* 
3037d 32 39 36 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  296 */ "expr ::=
3037e 20 52 41 49 53 45 20 4c 50 20 72 61 69 73 65 74   RAISE LP raiset
3037f 79 70 65 20 43 4f 4d 4d 41 20 6e 6d 20 52 50 22  ype COMMA nm RP"
30380 2c 0a 20 2f 2a 20 32 39 37 20 2a 2f 20 22 72 61  ,. /* 297 */ "ra
30381 69 73 65 74 79 70 65 20 3a 3a 3d 20 52 4f 4c 4c  isetype ::= ROLL
30382 42 41 43 4b 22 2c 0a 20 2f 2a 20 32 39 38 20 2a  BACK",. /* 298 *
30383 2f 20 22 72 61 69 73 65 74 79 70 65 20 3a 3a 3d  / "raisetype ::=
30384 20 41 42 4f 52 54 22 2c 0a 20 2f 2a 20 32 39 39   ABORT",. /* 299
30385 20 2a 2f 20 22 72 61 69 73 65 74 79 70 65 20 3a   */ "raisetype :
30386 3a 3d 20 46 41 49 4c 22 2c 0a 20 2f 2a 20 33 30  := FAIL",. /* 30
30387 30 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 52  0 */ "cmd ::= DR
30388 4f 50 20 54 52 49 47 47 45 52 20 69 66 65 78 69  OP TRIGGER ifexi
30389 73 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20  sts fullname",. 
3038a 2f 2a 20 33 30 31 20 2a 2f 20 22 63 6d 64 20 3a  /* 301 */ "cmd :
3038b 3a 3d 20 41 54 54 41 43 48 20 64 61 74 61 62 61  := ATTACH databa
3038c 73 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72 20 41  se_kw_opt expr A
3038d 53 20 65 78 70 72 20 6b 65 79 5f 6f 70 74 22 2c  S expr key_opt",
3038e 0a 20 2f 2a 20 33 30 32 20 2a 2f 20 22 63 6d 64  . /* 302 */ "cmd
3038f 20 3a 3a 3d 20 44 45 54 41 43 48 20 64 61 74 61   ::= DETACH data
30390 62 61 73 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72  base_kw_opt expr
30391 22 2c 0a 20 2f 2a 20 33 30 33 20 2a 2f 20 22 6b  ",. /* 303 */ "k
30392 65 79 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  ey_opt ::=",. /*
30393 20 33 30 34 20 2a 2f 20 22 6b 65 79 5f 6f 70 74   304 */ "key_opt
30394 20 3a 3a 3d 20 4b 45 59 20 65 78 70 72 22 2c 0a   ::= KEY expr",.
30395 20 2f 2a 20 33 30 35 20 2a 2f 20 22 64 61 74 61   /* 305 */ "data
30396 62 61 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 20  base_kw_opt ::= 
30397 44 41 54 41 42 41 53 45 22 2c 0a 20 2f 2a 20 33  DATABASE",. /* 3
30398 30 36 20 2a 2f 20 22 64 61 74 61 62 61 73 65 5f  06 */ "database_
30399 6b 77 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  kw_opt ::=",. /*
3039a 20 33 30 37 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   307 */ "cmd ::=
3039b 20 52 45 49 4e 44 45 58 22 2c 0a 20 2f 2a 20 33   REINDEX",. /* 3
3039c 30 38 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 52  08 */ "cmd ::= R
3039d 45 49 4e 44 45 58 20 6e 6d 20 64 62 6e 6d 22 2c  EINDEX nm dbnm",
3039e 0a 20 2f 2a 20 33 30 39 20 2a 2f 20 22 63 6d 64  . /* 309 */ "cmd
3039f 20 3a 3a 3d 20 41 4e 41 4c 59 5a 45 22 2c 0a 20   ::= ANALYZE",. 
303a0 2f 2a 20 33 31 30 20 2a 2f 20 22 63 6d 64 20 3a  /* 310 */ "cmd :
303a1 3a 3d 20 41 4e 41 4c 59 5a 45 20 6e 6d 20 64 62  := ANALYZE nm db
303a2 6e 6d 22 2c 0a 20 2f 2a 20 33 31 31 20 2a 2f 20  nm",. /* 311 */ 
303a3 22 63 6d 64 20 3a 3a 3d 20 41 4c 54 45 52 20 54  "cmd ::= ALTER T
303a4 41 42 4c 45 20 66 75 6c 6c 6e 61 6d 65 20 52 45  ABLE fullname RE
303a5 4e 41 4d 45 20 54 4f 20 6e 6d 22 2c 0a 20 2f 2a  NAME TO nm",. /*
303a6 20 33 31 32 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   312 */ "cmd ::=
303a7 20 41 4c 54 45 52 20 54 41 42 4c 45 20 61 64 64   ALTER TABLE add
303a8 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65  _column_fullname
303a9 20 41 44 44 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70   ADD kwcolumn_op
303aa 74 20 63 6f 6c 75 6d 6e 22 2c 0a 20 2f 2a 20 33  t column",. /* 3
303ab 31 33 20 2a 2f 20 22 61 64 64 5f 63 6f 6c 75 6d  13 */ "add_colum
303ac 6e 5f 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 66  n_fullname ::= f
303ad 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 33 31  ullname",. /* 31
303ae 34 20 2a 2f 20 22 6b 77 63 6f 6c 75 6d 6e 5f 6f  4 */ "kwcolumn_o
303af 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 31 35  pt ::=",. /* 315
303b0 20 2a 2f 20 22 6b 77 63 6f 6c 75 6d 6e 5f 6f 70   */ "kwcolumn_op
303b1 74 20 3a 3a 3d 20 43 4f 4c 55 4d 4e 4b 57 22 2c  t ::= COLUMNKW",
303b2 0a 20 2f 2a 20 33 31 36 20 2a 2f 20 22 63 6d 64  . /* 316 */ "cmd
303b3 20 3a 3a 3d 20 63 72 65 61 74 65 5f 76 74 61 62   ::= create_vtab
303b4 22 2c 0a 20 2f 2a 20 33 31 37 20 2a 2f 20 22 63  ",. /* 317 */ "c
303b5 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 76 74  md ::= create_vt
303b6 61 62 20 4c 50 20 76 74 61 62 61 72 67 6c 69 73  ab LP vtabarglis
303b7 74 20 52 50 22 2c 0a 20 2f 2a 20 33 31 38 20 2a  t RP",. /* 318 *
303b8 2f 20 22 63 72 65 61 74 65 5f 76 74 61 62 20 3a  / "create_vtab :
303b9 3a 3d 20 63 72 65 61 74 65 6b 77 20 56 49 52 54  := createkw VIRT
303ba 55 41 4c 20 54 41 42 4c 45 20 6e 6d 20 64 62 6e  UAL TABLE nm dbn
303bb 6d 20 55 53 49 4e 47 20 6e 6d 22 2c 0a 20 2f 2a  m USING nm",. /*
303bc 20 33 31 39 20 2a 2f 20 22 76 74 61 62 61 72 67   319 */ "vtabarg
303bd 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62 61 72 67  list ::= vtabarg
303be 22 2c 0a 20 2f 2a 20 33 32 30 20 2a 2f 20 22 76  ",. /* 320 */ "v
303bf 74 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20 76  tabarglist ::= v
303c0 74 61 62 61 72 67 6c 69 73 74 20 43 4f 4d 4d 41  tabarglist COMMA
303c1 20 76 74 61 62 61 72 67 22 2c 0a 20 2f 2a 20 33   vtabarg",. /* 3
303c2 32 31 20 2a 2f 20 22 76 74 61 62 61 72 67 20 3a  21 */ "vtabarg :
303c3 3a 3d 22 2c 0a 20 2f 2a 20 33 32 32 20 2a 2f 20  :=",. /* 322 */ 
303c4 22 76 74 61 62 61 72 67 20 3a 3a 3d 20 76 74 61  "vtabarg ::= vta
303c5 62 61 72 67 20 76 74 61 62 61 72 67 74 6f 6b 65  barg vtabargtoke
303c6 6e 22 2c 0a 20 2f 2a 20 33 32 33 20 2a 2f 20 22  n",. /* 323 */ "
303c7 76 74 61 62 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d  vtabargtoken ::=
303c8 20 41 4e 59 22 2c 0a 20 2f 2a 20 33 32 34 20 2a   ANY",. /* 324 *
303c9 2f 20 22 76 74 61 62 61 72 67 74 6f 6b 65 6e 20  / "vtabargtoken 
303ca 3a 3a 3d 20 6c 70 20 61 6e 79 6c 69 73 74 20 52  ::= lp anylist R
303cb 50 22 2c 0a 20 2f 2a 20 33 32 35 20 2a 2f 20 22  P",. /* 325 */ "
303cc 6c 70 20 3a 3a 3d 20 4c 50 22 2c 0a 20 2f 2a 20  lp ::= LP",. /* 
303cd 33 32 36 20 2a 2f 20 22 61 6e 79 6c 69 73 74 20  326 */ "anylist 
303ce 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 32 37 20 2a 2f  ::=",. /* 327 */
303cf 20 22 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61 6e   "anylist ::= an
303d0 79 6c 69 73 74 20 4c 50 20 61 6e 79 6c 69 73 74  ylist LP anylist
303d1 20 52 50 22 2c 0a 20 2f 2a 20 33 32 38 20 2a 2f   RP",. /* 328 */
303d2 20 22 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61 6e   "anylist ::= an
303d3 79 6c 69 73 74 20 41 4e 59 22 2c 0a 7d 3b 0a 23  ylist ANY",.};.#
303d4 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20  endif /* NDEBUG 
303d5 2a 2f 0a 0a 0a 23 69 66 20 59 59 53 54 41 43 4b  */...#if YYSTACK
303d6 44 45 50 54 48 3c 3d 30 0a 2f 2a 0a 2a 2a 20 54  DEPTH<=0./*.** T
303d7 72 79 20 74 6f 20 69 6e 63 72 65 61 73 65 20 74  ry to increase t
303d8 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70  he size of the p
303d9 61 72 73 65 72 20 73 74 61 63 6b 2e 0a 2a 2f 0a  arser stack..*/.
303da 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 47 72  static void yyGr
303db 6f 77 53 74 61 63 6b 28 79 79 50 61 72 73 65 72  owStack(yyParser
303dc 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 65 77 53   *p){.  int newS
303dd 69 7a 65 3b 0a 20 20 79 79 53 74 61 63 6b 45 6e  ize;.  yyStackEn
303de 74 72 79 20 2a 70 4e 65 77 3b 0a 0a 20 20 6e 65  try *pNew;..  ne
303df 77 53 69 7a 65 20 3d 20 70 2d 3e 79 79 73 74 6b  wSize = p->yystk
303e0 73 7a 2a 32 20 2b 20 31 30 30 3b 0a 20 20 70 4e  sz*2 + 100;.  pN
303e1 65 77 20 3d 20 72 65 61 6c 6c 6f 63 28 70 2d 3e  ew = realloc(p->
303e2 79 79 73 74 61 63 6b 2c 20 6e 65 77 53 69 7a 65  yystack, newSize
303e3 2a 73 69 7a 65 6f 66 28 70 4e 65 77 5b 30 5d 29  *sizeof(pNew[0])
303e4 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b  );.  if( pNew ){
303e5 0a 20 20 20 20 70 2d 3e 79 79 73 74 61 63 6b 20  .    p->yystack 
303e6 3d 20 70 4e 65 77 3b 0a 20 20 20 20 70 2d 3e 79  = pNew;.    p->y
303e7 79 73 74 6b 73 7a 20 3d 20 6e 65 77 53 69 7a 65  ystksz = newSize
303e8 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ;.#ifndef NDEBUG
303e9 0a 20 20 20 20 69 66 28 20 79 79 54 72 61 63 65  .    if( yyTrace
303ea 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 20 66 70  FILE ){.      fp
303eb 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
303ec 45 2c 22 25 73 53 74 61 63 6b 20 67 72 6f 77 73  E,"%sStack grows
303ed 20 74 6f 20 25 64 20 65 6e 74 72 69 65 73 21 5c   to %d entries!\
303ee 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
303ef 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c    yyTracePrompt,
303f0 20 70 2d 3e 79 79 73 74 6b 73 7a 29 3b 0a 20 20   p->yystksz);.  
303f1 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d    }.#endif.  }.}
303f2 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 0a 2a 2a 20  .#endif../* .** 
303f3 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c  This function al
303f4 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 20 70 61  locates a new pa
303f5 72 73 65 72 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c  rser..** The onl
303f6 79 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20  y argument is a 
303f7 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e  pointer to a fun
303f8 63 74 69 6f 6e 20 77 68 69 63 68 20 77 6f 72 6b  ction which work
303f9 73 20 6c 69 6b 65 0a 2a 2a 20 6d 61 6c 6c 6f 63  s like.** malloc
303fa 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a  ..**.** Inputs:.
303fb 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** A pointer to 
303fc 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 75 73 65  the function use
303fd 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65  d to allocate me
303fe 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70  mory..**.** Outp
303ff 75 74 73 3a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65  uts:.** A pointe
30400 72 20 74 6f 20 61 20 70 61 72 73 65 72 2e 20 20  r to a parser.  
30401 54 68 69 73 20 70 6f 69 6e 74 65 72 20 69 73 20  This pointer is 
30402 75 73 65 64 20 69 6e 20 73 75 62 73 65 71 75 65  used in subseque
30403 6e 74 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 73  nt calls.** to s
30404 71 6c 69 74 65 33 50 61 72 73 65 72 20 61 6e 64  qlite3Parser and
30405 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 46 72   sqlite3ParserFr
30406 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ee..*/.SQLITE_PR
30407 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
30408 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 28 76  te3ParserAlloc(v
30409 6f 69 64 20 2a 28 2a 6d 61 6c 6c 6f 63 50 72 6f  oid *(*mallocPro
3040a 63 29 28 73 69 7a 65 5f 74 29 29 7b 0a 20 20 79  c)(size_t)){.  y
3040b 79 50 61 72 73 65 72 20 2a 70 50 61 72 73 65 72  yParser *pParser
3040c 3b 0a 20 20 70 50 61 72 73 65 72 20 3d 20 28 79  ;.  pParser = (y
3040d 79 50 61 72 73 65 72 2a 29 28 2a 6d 61 6c 6c 6f  yParser*)(*mallo
3040e 63 50 72 6f 63 29 28 20 28 73 69 7a 65 5f 74 29  cProc)( (size_t)
3040f 73 69 7a 65 6f 66 28 79 79 50 61 72 73 65 72 29  sizeof(yyParser)
30410 20 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65   );.  if( pParse
30411 72 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 72  r ){.    pParser
30412 2d 3e 79 79 69 64 78 20 3d 20 2d 31 3b 0a 23 69  ->yyidx = -1;.#i
30413 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53  fdef YYTRACKMAXS
30414 54 41 43 4b 44 45 50 54 48 0a 20 20 20 20 70 50  TACKDEPTH.    pP
30415 61 72 73 65 72 2d 3e 79 79 69 64 78 4d 61 78 20  arser->yyidxMax 
30416 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20  = 0;.#endif.#if 
30417 59 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a  YYSTACKDEPTH<=0.
30418 20 20 20 20 70 50 61 72 73 65 72 2d 3e 79 79 73      pParser->yys
30419 74 61 63 6b 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  tack = NULL;.   
3041a 20 70 50 61 72 73 65 72 2d 3e 79 79 73 74 6b 73   pParser->yystks
3041b 7a 20 3d 20 30 3b 0a 20 20 20 20 79 79 47 72 6f  z = 0;.    yyGro
3041c 77 53 74 61 63 6b 28 70 50 61 72 73 65 72 29 3b  wStack(pParser);
3041d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65  .#endif.  }.  re
3041e 74 75 72 6e 20 70 50 61 72 73 65 72 3b 0a 7d 0a  turn pParser;.}.
3041f 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  ./* The followin
30420 67 20 66 75 6e 63 74 69 6f 6e 20 64 65 6c 65 74  g function delet
30421 65 73 20 74 68 65 20 76 61 6c 75 65 20 61 73 73  es the value ass
30422 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 0a 2a  ociated with a.*
30423 2a 20 73 79 6d 62 6f 6c 2e 20 20 54 68 65 20 73  * symbol.  The s
30424 79 6d 62 6f 6c 20 63 61 6e 20 62 65 20 65 69 74  ymbol can be eit
30425 68 65 72 20 61 20 74 65 72 6d 69 6e 61 6c 20 6f  her a terminal o
30426 72 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 2e 0a 2a  r nonterminal..*
30427 2a 20 22 79 79 6d 61 6a 6f 72 22 20 69 73 20 74  * "yymajor" is t
30428 68 65 20 73 79 6d 62 6f 6c 20 63 6f 64 65 2c 20  he symbol code, 
30429 61 6e 64 20 22 79 79 70 6d 69 6e 6f 72 22 20 69  and "yypminor" i
3042a 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a  s a pointer to.*
3042b 2a 20 74 68 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a  * the value..*/.
3042c 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 64  static void yy_d
3042d 65 73 74 72 75 63 74 6f 72 28 0a 20 20 79 79 50  estructor(.  yyP
3042e 61 72 73 65 72 20 2a 79 79 70 50 61 72 73 65 72  arser *yypParser
3042f 2c 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73  ,    /* The pars
30430 65 72 20 2a 2f 0a 20 20 59 59 43 4f 44 45 54 59  er */.  YYCODETY
30431 50 45 20 79 79 6d 61 6a 6f 72 2c 20 20 20 20 20  PE yymajor,     
30432 2f 2a 20 54 79 70 65 20 63 6f 64 65 20 66 6f 72  /* Type code for
30433 20 6f 62 6a 65 63 74 20 74 6f 20 64 65 73 74 72   object to destr
30434 6f 79 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52 54  oy */.  YYMINORT
30435 59 50 45 20 2a 79 79 70 6d 69 6e 6f 72 20 20 20  YPE *yypminor   
30436 2f 2a 20 54 68 65 20 6f 62 6a 65 63 74 20 74 6f  /* The object to
30437 20 62 65 20 64 65 73 74 72 6f 79 65 64 20 2a 2f   be destroyed */
30438 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 72  .){.  sqlite3Par
30439 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a 20 20  serARG_FETCH;.  
3043a 73 77 69 74 63 68 28 20 79 79 6d 61 6a 6f 72 20  switch( yymajor 
3043b 29 7b 0a 20 20 20 20 2f 2a 20 48 65 72 65 20 69  ){.    /* Here i
3043c 73 20 69 6e 73 65 72 74 65 64 20 74 68 65 20 61  s inserted the a
3043d 63 74 69 6f 6e 73 20 77 68 69 63 68 20 74 61 6b  ctions which tak
3043e 65 20 70 6c 61 63 65 20 77 68 65 6e 20 61 0a 20  e place when a. 
3043f 20 20 20 2a 2a 20 74 65 72 6d 69 6e 61 6c 20 6f     ** terminal o
30440 72 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c 20 69  r non-terminal i
30441 73 20 64 65 73 74 72 6f 79 65 64 2e 20 20 54 68  s destroyed.  Th
30442 69 73 20 63 61 6e 20 68 61 70 70 65 6e 0a 20 20  is can happen.  
30443 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 73 79    ** when the sy
30444 6d 62 6f 6c 20 69 73 20 70 6f 70 70 65 64 20 66  mbol is popped f
30445 72 6f 6d 20 74 68 65 20 73 74 61 63 6b 20 64 75  rom the stack du
30446 72 69 6e 67 20 61 0a 20 20 20 20 2a 2a 20 72 65  ring a.    ** re
30447 64 75 63 65 20 6f 72 20 64 75 72 69 6e 67 20 65  duce or during e
30448 72 72 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 20  rror processing 
30449 6f 72 20 77 68 65 6e 20 61 20 70 61 72 73 65 72  or when a parser
3044a 20 69 73 20 0a 20 20 20 20 2a 2a 20 62 65 69 6e   is .    ** bein
3044b 67 20 64 65 73 74 72 6f 79 65 64 20 62 65 66 6f  g destroyed befo
3044c 72 65 20 69 74 20 69 73 20 66 69 6e 69 73 68 65  re it is finishe
3044d 64 20 70 61 72 73 69 6e 67 2e 0a 20 20 20 20 2a  d parsing..    *
3044e 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 3a 20 64  *.    ** Note: d
3044f 75 72 69 6e 67 20 61 20 72 65 64 75 63 65 2c 20  uring a reduce, 
30450 74 68 65 20 6f 6e 6c 79 20 73 79 6d 62 6f 6c 73  the only symbols
30451 20 64 65 73 74 72 6f 79 65 64 20 61 72 65 20 74   destroyed are t
30452 68 6f 73 65 0a 20 20 20 20 2a 2a 20 77 68 69 63  hose.    ** whic
30453 68 20 61 70 70 65 61 72 20 6f 6e 20 74 68 65 20  h appear on the 
30454 52 48 53 20 6f 66 20 74 68 65 20 72 75 6c 65 2c  RHS of the rule,
30455 20 62 75 74 20 77 68 69 63 68 20 61 72 65 20 6e   but which are n
30456 6f 74 20 75 73 65 64 0a 20 20 20 20 2a 2a 20 69  ot used.    ** i
30457 6e 73 69 64 65 20 74 68 65 20 43 20 63 6f 64 65  nside the C code
30458 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
30459 65 20 31 36 30 3a 20 2f 2a 20 73 65 6c 65 63 74  e 160: /* select
3045a 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 39 34   */.    case 194
3045b 3a 20 2f 2a 20 6f 6e 65 73 65 6c 65 63 74 20 2a  : /* oneselect *
3045c 2f 0a 7b 0a 73 71 6c 69 74 65 33 53 65 6c 65 63  /.{.sqlite3Selec
3045d 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  tDelete(pParse->
3045e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79  db, (yypminor->y
3045f 79 33 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72  y3));.}.      br
30460 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 31 37  eak;.    case 17
30461 34 3a 20 2f 2a 20 74 65 72 6d 20 2a 2f 0a 20 20  4: /* term */.  
30462 20 20 63 61 73 65 20 31 37 35 3a 20 2f 2a 20 65    case 175: /* e
30463 78 70 72 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  xpr */.    case 
30464 32 32 33 3a 20 2f 2a 20 65 73 63 61 70 65 20 2a  223: /* escape *
30465 2f 0a 7b 0a 73 71 6c 69 74 65 33 45 78 70 72 44  /.{.sqlite3ExprD
30466 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
30467 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 33  , (yypminor->yy3
30468 34 36 29 2e 70 45 78 70 72 29 3b 0a 7d 0a 20 20  46).pExpr);.}.  
30469 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
3046a 61 73 65 20 31 37 39 3a 20 2f 2a 20 69 64 78 6c  ase 179: /* idxl
3046b 69 73 74 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63  ist_opt */.    c
3046c 61 73 65 20 31 38 37 3a 20 2f 2a 20 69 64 78 6c  ase 187: /* idxl
3046d 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  ist */.    case 
3046e 31 39 37 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69  197: /* selcolli
3046f 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32  st */.    case 2
30470 30 30 3a 20 2f 2a 20 67 72 6f 75 70 62 79 5f 6f  00: /* groupby_o
30471 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32  pt */.    case 2
30472 30 32 3a 20 2f 2a 20 6f 72 64 65 72 62 79 5f 6f  02: /* orderby_o
30473 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32  pt */.    case 2
30474 30 34 3a 20 2f 2a 20 73 63 6c 70 20 2a 2f 0a 20  04: /* sclp */. 
30475 20 20 20 63 61 73 65 20 32 31 34 3a 20 2f 2a 20     case 214: /* 
30476 73 6f 72 74 6c 69 73 74 20 2a 2f 0a 20 20 20 20  sortlist */.    
30477 63 61 73 65 20 32 31 36 3a 20 2f 2a 20 6e 65 78  case 216: /* nex
30478 70 72 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61  prlist */.    ca
30479 73 65 20 32 31 37 3a 20 2f 2a 20 73 65 74 6c 69  se 217: /* setli
3047a 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32  st */.    case 2
3047b 32 30 3a 20 2f 2a 20 69 74 65 6d 6c 69 73 74 20  20: /* itemlist 
3047c 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 32 31 3a  */.    case 221:
3047d 20 2f 2a 20 65 78 70 72 6c 69 73 74 20 2a 2f 0a   /* exprlist */.
3047e 20 20 20 20 63 61 73 65 20 32 32 37 3a 20 2f 2a      case 227: /*
3047f 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 2a   case_exprlist *
30480 2f 0a 7b 0a 73 71 6c 69 74 65 33 45 78 70 72 4c  /.{.sqlite3ExprL
30481 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65  istDelete(pParse
30482 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d  ->db, (yypminor-
30483 3e 79 79 31 34 29 29 3b 0a 7d 0a 20 20 20 20 20  >yy14));.}.     
30484 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
30485 20 31 39 33 3a 20 2f 2a 20 66 75 6c 6c 6e 61 6d   193: /* fullnam
30486 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 39  e */.    case 19
30487 38 3a 20 2f 2a 20 66 72 6f 6d 20 2a 2f 0a 20 20  8: /* from */.  
30488 20 20 63 61 73 65 20 32 30 36 3a 20 2f 2a 20 73    case 206: /* s
30489 65 6c 74 61 62 6c 69 73 74 20 2a 2f 0a 20 20 20  eltablist */.   
3048a 20 63 61 73 65 20 32 30 37 3a 20 2f 2a 20 73 74   case 207: /* st
3048b 6c 5f 70 72 65 66 69 78 20 2a 2f 0a 7b 0a 73 71  l_prefix */.{.sq
3048c 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65  lite3SrcListDele
3048d 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28  te(pParse->db, (
3048e 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 36 35 29 29  yypminor->yy65))
3048f 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.}.      break;
30490 0a 20 20 20 20 63 61 73 65 20 31 39 39 3a 20 2f  .    case 199: /
30491 2a 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a 20  * where_opt */. 
30492 20 20 20 63 61 73 65 20 32 30 31 3a 20 2f 2a 20     case 201: /* 
30493 68 61 76 69 6e 67 5f 6f 70 74 20 2a 2f 0a 20 20  having_opt */.  
30494 20 20 63 61 73 65 20 32 31 30 3a 20 2f 2a 20 6f    case 210: /* o
30495 6e 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73  n_opt */.    cas
30496 65 20 32 31 35 3a 20 2f 2a 20 73 6f 72 74 69 74  e 215: /* sortit
30497 65 6d 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32  em */.    case 2
30498 32 36 3a 20 2f 2a 20 63 61 73 65 5f 6f 70 65 72  26: /* case_oper
30499 61 6e 64 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  and */.    case 
3049a 32 32 38 3a 20 2f 2a 20 63 61 73 65 5f 65 6c 73  228: /* case_els
3049b 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 33  e */.    case 23
3049c 39 3a 20 2f 2a 20 77 68 65 6e 5f 63 6c 61 75 73  9: /* when_claus
3049d 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 34  e */.    case 24
3049e 34 3a 20 2f 2a 20 6b 65 79 5f 6f 70 74 20 2a 2f  4: /* key_opt */
3049f 0a 7b 0a 73 71 6c 69 74 65 33 45 78 70 72 44 65  .{.sqlite3ExprDe
304a0 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
304a1 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 31 33   (yypminor->yy13
304a2 32 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65  2));.}.      bre
304a3 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 32 31 31  ak;.    case 211
304a4 3a 20 2f 2a 20 75 73 69 6e 67 5f 6f 70 74 20 2a  : /* using_opt *
304a5 2f 0a 20 20 20 20 63 61 73 65 20 32 31 33 3a 20  /.    case 213: 
304a6 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 20 2a 2f  /* inscollist */
304a7 0a 20 20 20 20 63 61 73 65 20 32 31 39 3a 20 2f  .    case 219: /
304a8 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74  * inscollist_opt
304a9 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 49 64 4c   */.{.sqlite3IdL
304aa 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65  istDelete(pParse
304ab 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d  ->db, (yypminor-
304ac 3e 79 79 34 30 38 29 29 3b 0a 7d 0a 20 20 20 20  >yy408));.}.    
304ad 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
304ae 65 20 32 33 35 3a 20 2f 2a 20 74 72 69 67 67 65  e 235: /* trigge
304af 72 5f 63 6d 64 5f 6c 69 73 74 20 2a 2f 0a 20 20  r_cmd_list */.  
304b0 20 20 63 61 73 65 20 32 34 30 3a 20 2f 2a 20 74    case 240: /* t
304b1 72 69 67 67 65 72 5f 63 6d 64 20 2a 2f 0a 7b 0a  rigger_cmd */.{.
304b2 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
304b3 67 67 65 72 53 74 65 70 28 70 50 61 72 73 65 2d  ggerStep(pParse-
304b4 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e  >db, (yypminor->
304b5 79 79 34 37 33 29 29 3b 0a 7d 0a 20 20 20 20 20  yy473));.}.     
304b6 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
304b7 20 32 33 37 3a 20 2f 2a 20 74 72 69 67 67 65 72   237: /* trigger
304b8 5f 65 76 65 6e 74 20 2a 2f 0a 7b 0a 73 71 6c 69  _event */.{.sqli
304b9 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28  te3IdListDelete(
304ba 70 50 61 72 73 65 2d 3e 64 62 2c 20 28 79 79 70  pParse->db, (yyp
304bb 6d 69 6e 6f 72 2d 3e 79 79 33 37 38 29 2e 62 29  minor->yy378).b)
304bc 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.}.      break;
304bd 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 62  .    default:  b
304be 72 65 61 6b 3b 20 20 20 2f 2a 20 49 66 20 6e 6f  reak;   /* If no
304bf 20 64 65 73 74 72 75 63 74 6f 72 20 61 63 74 69   destructor acti
304c0 6f 6e 20 73 70 65 63 69 66 69 65 64 3a 20 64 6f  on specified: do
304c1 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 7d 0a   nothing */.  }.
304c2 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 20 74 68 65  }../*.** Pop the
304c3 20 70 61 72 73 65 72 27 73 20 73 74 61 63 6b 20   parser's stack 
304c4 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  once..**.** If t
304c5 68 65 72 65 20 69 73 20 61 20 64 65 73 74 72 75  here is a destru
304c6 63 74 6f 72 20 72 6f 75 74 69 6e 65 20 61 73 73  ctor routine ass
304c7 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
304c8 20 74 6f 6b 65 6e 20 77 68 69 63 68 0a 2a 2a 20   token which.** 
304c9 69 73 20 70 6f 70 70 65 64 20 66 72 6f 6d 20 74  is popped from t
304ca 68 65 20 73 74 61 63 6b 2c 20 74 68 65 6e 20 63  he stack, then c
304cb 61 6c 6c 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 52 65  all it..**.** Re
304cc 74 75 72 6e 20 74 68 65 20 6d 61 6a 6f 72 20 74  turn the major t
304cd 6f 6b 65 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20  oken number for 
304ce 74 68 65 20 73 79 6d 62 6f 6c 20 70 6f 70 70 65  the symbol poppe
304cf 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
304d0 20 79 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73   yy_pop_parser_s
304d1 74 61 63 6b 28 79 79 50 61 72 73 65 72 20 2a 70  tack(yyParser *p
304d2 50 61 72 73 65 72 29 7b 0a 20 20 59 59 43 4f 44  Parser){.  YYCOD
304d3 45 54 59 50 45 20 79 79 6d 61 6a 6f 72 3b 0a 20  ETYPE yymajor;. 
304d4 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20 2a 79   yyStackEntry *y
304d5 79 74 6f 73 20 3d 20 26 70 50 61 72 73 65 72 2d  ytos = &pParser-
304d6 3e 79 79 73 74 61 63 6b 5b 70 50 61 72 73 65 72  >yystack[pParser
304d7 2d 3e 79 79 69 64 78 5d 3b 0a 0a 20 20 2f 2a 20  ->yyidx];..  /* 
304d8 54 68 65 72 65 20 69 73 20 6e 6f 20 6d 65 63 68  There is no mech
304d9 61 6e 69 73 6d 20 62 79 20 77 68 69 63 68 20 74  anism by which t
304da 68 65 20 70 61 72 73 65 72 20 73 74 61 63 6b 20  he parser stack 
304db 63 61 6e 20 62 65 20 70 6f 70 70 65 64 20 62 65  can be popped be
304dc 6c 6f 77 0a 20 20 2a 2a 20 65 6d 70 74 79 20 69  low.  ** empty i
304dd 6e 20 53 51 4c 69 74 65 2e 20 20 2a 2f 0a 20 20  n SQLite.  */.  
304de 69 66 28 20 4e 45 56 45 52 28 70 50 61 72 73 65  if( NEVER(pParse
304df 72 2d 3e 79 79 69 64 78 3c 30 29 20 29 20 72 65  r->yyidx<0) ) re
304e0 74 75 72 6e 20 30 3b 0a 23 69 66 6e 64 65 66 20  turn 0;.#ifndef 
304e1 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79 79 54  NDEBUG.  if( yyT
304e2 72 61 63 65 46 49 4c 45 20 26 26 20 70 50 61 72  raceFILE && pPar
304e3 73 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 7b  ser->yyidx>=0 ){
304e4 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54  .    fprintf(yyT
304e5 72 61 63 65 46 49 4c 45 2c 22 25 73 50 6f 70 70  raceFILE,"%sPopp
304e6 69 6e 67 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20  ing %s\n",.     
304e7 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 0a   yyTracePrompt,.
304e8 20 20 20 20 20 20 79 79 54 6f 6b 65 6e 4e 61 6d        yyTokenNam
304e9 65 5b 79 79 74 6f 73 2d 3e 6d 61 6a 6f 72 5d 29  e[yytos->major])
304ea 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79  ;.  }.#endif.  y
304eb 79 6d 61 6a 6f 72 20 3d 20 79 79 74 6f 73 2d 3e  ymajor = yytos->
304ec 6d 61 6a 6f 72 3b 0a 20 20 79 79 5f 64 65 73 74  major;.  yy_dest
304ed 72 75 63 74 6f 72 28 70 50 61 72 73 65 72 2c 20  ructor(pParser, 
304ee 79 79 6d 61 6a 6f 72 2c 20 26 79 79 74 6f 73 2d  yymajor, &yytos-
304ef 3e 6d 69 6e 6f 72 29 3b 0a 20 20 70 50 61 72 73  >minor);.  pPars
304f0 65 72 2d 3e 79 79 69 64 78 2d 2d 3b 0a 20 20 72  er->yyidx--;.  r
304f1 65 74 75 72 6e 20 79 79 6d 61 6a 6f 72 3b 0a 7d  eturn yymajor;.}
304f2 0a 0a 2f 2a 20 0a 2a 2a 20 44 65 61 6c 6c 6f 63  ../* .** Dealloc
304f3 61 74 65 20 61 6e 64 20 64 65 73 74 72 6f 79 20  ate and destroy 
304f4 61 20 70 61 72 73 65 72 2e 20 20 44 65 73 74 72  a parser.  Destr
304f5 75 63 74 6f 72 73 20 61 72 65 20 61 6c 6c 20 63  uctors are all c
304f6 61 6c 6c 65 64 20 66 6f 72 0a 2a 2a 20 61 6c 6c  alled for.** all
304f7 20 73 74 61 63 6b 20 65 6c 65 6d 65 6e 74 73 20   stack elements 
304f8 62 65 66 6f 72 65 20 73 68 75 74 74 69 6e 67 20  before shutting 
304f9 74 68 65 20 70 61 72 73 65 72 20 64 6f 77 6e 2e  the parser down.
304fa 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a  .**.** Inputs:.*
304fb 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20  * <ul>.** <li>  
304fc 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
304fd 20 70 61 72 73 65 72 2e 20 20 54 68 69 73 20 73   parser.  This s
304fe 68 6f 75 6c 64 20 62 65 20 61 20 70 6f 69 6e 74  hould be a point
304ff 65 72 0a 2a 2a 20 20 20 20 20 20 20 6f 62 74 61  er.**       obta
30500 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
30501 33 50 61 72 73 65 72 41 6c 6c 6f 63 2e 0a 2a 2a  3ParserAlloc..**
30502 20 3c 6c 69 3e 20 20 41 20 70 6f 69 6e 74 65 72   <li>  A pointer
30503 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 75   to a function u
30504 73 65 64 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d  sed to reclaim m
30505 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 0a 2a  emory obtained.*
30506 2a 20 20 20 20 20 20 20 66 72 6f 6d 20 6d 61 6c  *       from mal
30507 6c 6f 63 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2f  loc..** </ul>.*/
30508 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
30509 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73  void sqlite3Pars
3050a 65 72 46 72 65 65 28 0a 20 20 76 6f 69 64 20 2a  erFree(.  void *
3050b 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
3050c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
3050d 73 65 72 20 74 6f 20 62 65 20 64 65 6c 65 74 65  ser to be delete
3050e 64 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 66 72  d */.  void (*fr
3050f 65 65 50 72 6f 63 29 28 76 6f 69 64 2a 29 20 20  eeProc)(void*)  
30510 20 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 75     /* Function u
30511 73 65 64 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d  sed to reclaim m
30512 65 6d 6f 72 79 20 2a 2f 0a 29 7b 0a 20 20 79 79  emory */.){.  yy
30513 50 61 72 73 65 72 20 2a 70 50 61 72 73 65 72 20  Parser *pParser 
30514 3d 20 28 79 79 50 61 72 73 65 72 2a 29 70 3b 0a  = (yyParser*)p;.
30515 20 20 2f 2a 20 49 6e 20 53 51 4c 69 74 65 2c 20    /* In SQLite, 
30516 77 65 20 6e 65 76 65 72 20 74 72 79 20 74 6f 20  we never try to 
30517 64 65 73 74 72 6f 79 20 61 20 70 61 72 73 65 72  destroy a parser
30518 20 74 68 61 74 20 77 61 73 20 6e 6f 74 20 73 75   that was not su
30519 63 63 65 73 73 66 75 6c 6c 79 0a 20 20 2a 2a 20  ccessfully.  ** 
3051a 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 66  created in the f
3051b 69 72 73 74 20 70 6c 61 63 65 2e 20 2a 2f 0a 20  irst place. */. 
3051c 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 72 73   if( NEVER(pPars
3051d 65 72 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b  er==0) ) return;
3051e 0a 20 20 77 68 69 6c 65 28 20 70 50 61 72 73 65  .  while( pParse
3051f 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20 79 79  r->yyidx>=0 ) yy
30520 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63  _pop_parser_stac
30521 6b 28 70 50 61 72 73 65 72 29 3b 0a 23 69 66 20  k(pParser);.#if 
30522 59 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a  YYSTACKDEPTH<=0.
30523 20 20 66 72 65 65 28 70 50 61 72 73 65 72 2d 3e    free(pParser->
30524 79 79 73 74 61 63 6b 29 3b 0a 23 65 6e 64 69 66  yystack);.#endif
30525 0a 20 20 28 2a 66 72 65 65 50 72 6f 63 29 28 28  .  (*freeProc)((
30526 76 6f 69 64 2a 29 70 50 61 72 73 65 72 29 3b 0a  void*)pParser);.
30527 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
30528 74 68 65 20 70 65 61 6b 20 64 65 70 74 68 20 6f  the peak depth o
30529 66 20 74 68 65 20 73 74 61 63 6b 20 66 6f 72 20  f the stack for 
3052a 61 20 70 61 72 73 65 72 2e 0a 2a 2f 0a 23 69 66  a parser..*/.#if
3052b 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53 54  def YYTRACKMAXST
3052c 41 43 4b 44 45 50 54 48 0a 53 51 4c 49 54 45 5f  ACKDEPTH.SQLITE_
3052d 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
3052e 74 65 33 50 61 72 73 65 72 53 74 61 63 6b 50 65  te3ParserStackPe
3052f 61 6b 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 79  ak(void *p){.  y
30530 79 50 61 72 73 65 72 20 2a 70 50 61 72 73 65 72  yParser *pParser
30531 20 3d 20 28 79 79 50 61 72 73 65 72 2a 29 70 3b   = (yyParser*)p;
30532 0a 20 20 72 65 74 75 72 6e 20 70 50 61 72 73 65  .  return pParse
30533 72 2d 3e 79 79 69 64 78 4d 61 78 3b 0a 7d 0a 23  r->yyidxMax;.}.#
30534 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e  endif../*.** Fin
30535 64 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  d the appropriat
30536 65 20 61 63 74 69 6f 6e 20 66 6f 72 20 61 20 70  e action for a p
30537 61 72 73 65 72 20 67 69 76 65 6e 20 74 68 65 20  arser given the 
30538 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20 6c 6f 6f 6b  terminal.** look
30539 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 69 4c 6f  -ahead token iLo
3053a 6f 6b 41 68 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49  okAhead..**.** I
3053b 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64  f the look-ahead
3053c 20 74 6f 6b 65 6e 20 69 73 20 59 59 4e 4f 43 4f   token is YYNOCO
3053d 44 45 2c 20 74 68 65 6e 20 63 68 65 63 6b 20 74  DE, then check t
3053e 6f 20 73 65 65 20 69 66 20 74 68 65 20 61 63 74  o see if the act
3053f 69 6f 6e 20 69 73 0a 2a 2a 20 69 6e 64 65 70 65  ion is.** indepe
30540 6e 64 65 6e 74 20 6f 66 20 74 68 65 20 6c 6f 6f  ndent of the loo
30541 6b 2d 61 68 65 61 64 2e 20 20 49 66 20 69 74 20  k-ahead.  If it 
30542 69 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 61  is, return the a
30543 63 74 69 6f 6e 2c 20 6f 74 68 65 72 77 69 73 65  ction, otherwise
30544 0a 2a 2a 20 72 65 74 75 72 6e 20 59 59 5f 4e 4f  .** return YY_NO
30545 5f 41 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74 61 74  _ACTION..*/.stat
30546 69 63 20 69 6e 74 20 79 79 5f 66 69 6e 64 5f 73  ic int yy_find_s
30547 68 69 66 74 5f 61 63 74 69 6f 6e 28 0a 20 20 79  hift_action(.  y
30548 79 50 61 72 73 65 72 20 2a 70 50 61 72 73 65 72  yParser *pParser
30549 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ,        /* The 
3054a 70 61 72 73 65 72 20 2a 2f 0a 20 20 59 59 43 4f  parser */.  YYCO
3054b 44 45 54 59 50 45 20 69 4c 6f 6f 6b 41 68 65 61  DETYPE iLookAhea
3054c 64 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f  d     /* The loo
3054d 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 2a 2f  k-ahead token */
3054e 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  .){.  int i;.  i
3054f 6e 74 20 73 74 61 74 65 6e 6f 20 3d 20 70 50 61  nt stateno = pPa
30550 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 70 50  rser->yystack[pP
30551 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 2e 73 74  arser->yyidx].st
30552 61 74 65 6e 6f 3b 0a 20 0a 20 20 69 66 28 20 73  ateno;. .  if( s
30553 74 61 74 65 6e 6f 3e 59 59 5f 53 48 49 46 54 5f  tateno>YY_SHIFT_
30554 4d 41 58 20 7c 7c 20 28 69 20 3d 20 79 79 5f 73  MAX || (i = yy_s
30555 68 69 66 74 5f 6f 66 73 74 5b 73 74 61 74 65 6e  hift_ofst[staten
30556 6f 5d 29 3d 3d 59 59 5f 53 48 49 46 54 5f 55 53  o])==YY_SHIFT_US
30557 45 5f 44 46 4c 54 20 29 7b 0a 20 20 20 20 72 65  E_DFLT ){.    re
30558 74 75 72 6e 20 79 79 5f 64 65 66 61 75 6c 74 5b  turn yy_default[
30559 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d 0a 20 20  stateno];.  }.  
3055a 61 73 73 65 72 74 28 20 69 4c 6f 6f 6b 41 68 65  assert( iLookAhe
3055b 61 64 21 3d 59 59 4e 4f 43 4f 44 45 20 29 3b 0a  ad!=YYNOCODE );.
3055c 20 20 69 20 2b 3d 20 69 4c 6f 6f 6b 41 68 65 61    i += iLookAhea
3055d 64 3b 0a 20 20 69 66 28 20 69 3c 30 20 7c 7c 20  d;.  if( i<0 || 
3055e 69 3e 3d 59 59 5f 53 5a 5f 41 43 54 54 41 42 20  i>=YY_SZ_ACTTAB 
3055f 7c 7c 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b  || yy_lookahead[
30560 69 5d 21 3d 69 4c 6f 6f 6b 41 68 65 61 64 20 29  i]!=iLookAhead )
30561 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 75 73 65  {.    /* The use
30562 72 20 6f 66 20 22 3b 22 20 69 6e 73 74 65 61 64  r of ";" instead
30563 20 6f 66 20 22 5c 30 30 30 22 20 61 73 20 61 20   of "\000" as a 
30564 73 74 61 74 65 6d 65 6e 74 20 74 65 72 6d 69 6e  statement termin
30565 61 74 6f 72 20 69 6e 20 53 51 4c 69 74 65 0a 20  ator in SQLite. 
30566 20 20 20 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74     ** means that
30567 20 77 65 20 61 6c 77 61 79 73 20 68 61 76 65 20   we always have 
30568 61 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b  a look-ahead tok
30569 65 6e 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 69  en. */.    if( i
3056a 4c 6f 6f 6b 41 68 65 61 64 3e 30 20 29 7b 0a 23  LookAhead>0 ){.#
3056b 69 66 64 65 66 20 59 59 46 41 4c 4c 42 41 43 4b  ifdef YYFALLBACK
3056c 0a 20 20 20 20 20 20 59 59 43 4f 44 45 54 59 50  .      YYCODETYP
3056d 45 20 69 46 61 6c 6c 62 61 63 6b 3b 20 20 20 20  E iFallback;    
3056e 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 62          /* Fallb
3056f 61 63 6b 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20  ack token */.   
30570 20 20 20 69 66 28 20 69 4c 6f 6f 6b 41 68 65 61     if( iLookAhea
30571 64 3c 73 69 7a 65 6f 66 28 79 79 46 61 6c 6c 62  d<sizeof(yyFallb
30572 61 63 6b 29 2f 73 69 7a 65 6f 66 28 79 79 46 61  ack)/sizeof(yyFa
30573 6c 6c 62 61 63 6b 5b 30 5d 29 0a 20 20 20 20 20  llback[0]).     
30574 20 20 20 20 20 20 20 20 26 26 20 28 69 46 61 6c          && (iFal
30575 6c 62 61 63 6b 20 3d 20 79 79 46 61 6c 6c 62 61  lback = yyFallba
30576 63 6b 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d 29 21  ck[iLookAhead])!
30577 3d 30 20 29 7b 0a 23 69 66 6e 64 65 66 20 4e 44  =0 ){.#ifndef ND
30578 45 42 55 47 0a 20 20 20 20 20 20 20 20 69 66 28  EBUG.        if(
30579 20 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a   yyTraceFILE ){.
3057a 20 20 20 20 20 20 20 20 20 20 66 70 72 69 6e 74            fprint
3057b 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 20 22  f(yyTraceFILE, "
3057c 25 73 46 41 4c 4c 42 41 43 4b 20 25 73 20 3d 3e  %sFALLBACK %s =>
3057d 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20   %s\n",.        
3057e 20 20 20 20 20 79 79 54 72 61 63 65 50 72 6f 6d       yyTraceProm
3057f 70 74 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b  pt, yyTokenName[
30580 69 4c 6f 6f 6b 41 68 65 61 64 5d 2c 20 79 79 54  iLookAhead], yyT
30581 6f 6b 65 6e 4e 61 6d 65 5b 69 46 61 6c 6c 62 61  okenName[iFallba
30582 63 6b 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ck]);.        }.
30583 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 72  #endif.        r
30584 65 74 75 72 6e 20 79 79 5f 66 69 6e 64 5f 73 68  eturn yy_find_sh
30585 69 66 74 5f 61 63 74 69 6f 6e 28 70 50 61 72 73  ift_action(pPars
30586 65 72 2c 20 69 46 61 6c 6c 62 61 63 6b 29 3b 0a  er, iFallback);.
30587 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 23        }.#endif.#
30588 69 66 64 65 66 20 59 59 57 49 4c 44 43 41 52 44  ifdef YYWILDCARD
30589 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20  .      {.       
3058a 20 69 6e 74 20 6a 20 3d 20 69 20 2d 20 69 4c 6f   int j = i - iLo
3058b 6f 6b 41 68 65 61 64 20 2b 20 59 59 57 49 4c 44  okAhead + YYWILD
3058c 43 41 52 44 3b 0a 20 20 20 20 20 20 20 20 69 66  CARD;.        if
3058d 28 20 6a 3e 3d 30 20 26 26 20 6a 3c 59 59 5f 53  ( j>=0 && j<YY_S
3058e 5a 5f 41 43 54 54 41 42 20 26 26 20 79 79 5f 6c  Z_ACTTAB && yy_l
3058f 6f 6f 6b 61 68 65 61 64 5b 6a 5d 3d 3d 59 59 57  ookahead[j]==YYW
30590 49 4c 44 43 41 52 44 20 29 7b 0a 23 69 66 6e 64  ILDCARD ){.#ifnd
30591 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 20 20  ef NDEBUG.      
30592 20 20 20 20 69 66 28 20 79 79 54 72 61 63 65 46      if( yyTraceF
30593 49 4c 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ILE ){.         
30594 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61     fprintf(yyTra
30595 63 65 46 49 4c 45 2c 20 22 25 73 57 49 4c 44 43  ceFILE, "%sWILDC
30596 41 52 44 20 25 73 20 3d 3e 20 25 73 5c 6e 22 2c  ARD %s => %s\n",
30597 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
30598 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 20 79  yyTracePrompt, y
30599 79 54 6f 6b 65 6e 4e 61 6d 65 5b 69 4c 6f 6f 6b  yTokenName[iLook
3059a 41 68 65 61 64 5d 2c 20 79 79 54 6f 6b 65 6e 4e  Ahead], yyTokenN
3059b 61 6d 65 5b 59 59 57 49 4c 44 43 41 52 44 5d 29  ame[YYWILDCARD])
3059c 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 23 65  ;.          }.#e
3059d 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a  ndif /* NDEBUG *
3059e 2f 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  /.          retu
3059f 72 6e 20 79 79 5f 61 63 74 69 6f 6e 5b 6a 5d 3b  rn yy_action[j];
305a0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
305a1 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 59 59 57   }.#endif /* YYW
305a2 49 4c 44 43 41 52 44 20 2a 2f 0a 20 20 20 20 7d  ILDCARD */.    }
305a3 0a 20 20 20 20 72 65 74 75 72 6e 20 79 79 5f 64  .    return yy_d
305a4 65 66 61 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b  efault[stateno];
305a5 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
305a6 74 75 72 6e 20 79 79 5f 61 63 74 69 6f 6e 5b 69  turn yy_action[i
305a7 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ];.  }.}../*.** 
305a8 46 69 6e 64 20 74 68 65 20 61 70 70 72 6f 70 72  Find the appropr
305a9 69 61 74 65 20 61 63 74 69 6f 6e 20 66 6f 72 20  iate action for 
305aa 61 20 70 61 72 73 65 72 20 67 69 76 65 6e 20 74  a parser given t
305ab 68 65 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c 0a  he non-terminal.
305ac 2a 2a 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f  ** look-ahead to
305ad 6b 65 6e 20 69 4c 6f 6f 6b 41 68 65 61 64 2e 0a  ken iLookAhead..
305ae 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 6f  **.** If the loo
305af 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 69 73  k-ahead token is
305b0 20 59 59 4e 4f 43 4f 44 45 2c 20 74 68 65 6e 20   YYNOCODE, then 
305b1 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  check to see if 
305b2 74 68 65 20 61 63 74 69 6f 6e 20 69 73 0a 2a 2a  the action is.**
305b3 20 69 6e 64 65 70 65 6e 64 65 6e 74 20 6f 66 20   independent of 
305b4 74 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 2e 20  the look-ahead. 
305b5 20 49 66 20 69 74 20 69 73 2c 20 72 65 74 75 72   If it is, retur
305b6 6e 20 74 68 65 20 61 63 74 69 6f 6e 2c 20 6f 74  n the action, ot
305b7 68 65 72 77 69 73 65 0a 2a 2a 20 72 65 74 75 72  herwise.** retur
305b8 6e 20 59 59 5f 4e 4f 5f 41 43 54 49 4f 4e 2e 0a  n YY_NO_ACTION..
305b9 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 79 79  */.static int yy
305ba 5f 66 69 6e 64 5f 72 65 64 75 63 65 5f 61 63 74  _find_reduce_act
305bb 69 6f 6e 28 0a 20 20 69 6e 74 20 73 74 61 74 65  ion(.  int state
305bc 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  no,             
305bd 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 74 61 74   /* Current stat
305be 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 59 59  e number */.  YY
305bf 43 4f 44 45 54 59 50 45 20 69 4c 6f 6f 6b 41 68  CODETYPE iLookAh
305c0 65 61 64 20 20 20 20 20 2f 2a 20 54 68 65 20 6c  ead     /* The l
305c1 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20  ook-ahead token 
305c2 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 23  */.){.  int i;.#
305c3 69 66 64 65 66 20 59 59 45 52 52 4f 52 53 59 4d  ifdef YYERRORSYM
305c4 42 4f 4c 0a 20 20 69 66 28 20 73 74 61 74 65 6e  BOL.  if( staten
305c5 6f 3e 59 59 5f 52 45 44 55 43 45 5f 4d 41 58 20  o>YY_REDUCE_MAX 
305c6 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 79 79  ){.    return yy
305c7 5f 64 65 66 61 75 6c 74 5b 73 74 61 74 65 6e 6f  _default[stateno
305c8 5d 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 61  ];.  }.#else.  a
305c9 73 73 65 72 74 28 20 73 74 61 74 65 6e 6f 3c 3d  ssert( stateno<=
305ca 59 59 5f 52 45 44 55 43 45 5f 4d 41 58 20 29 3b  YY_REDUCE_MAX );
305cb 0a 23 65 6e 64 69 66 0a 20 20 69 20 3d 20 79 79  .#endif.  i = yy
305cc 5f 72 65 64 75 63 65 5f 6f 66 73 74 5b 73 74 61  _reduce_ofst[sta
305cd 74 65 6e 6f 5d 3b 0a 20 20 61 73 73 65 72 74 28  teno];.  assert(
305ce 20 69 21 3d 59 59 5f 52 45 44 55 43 45 5f 55 53   i!=YY_REDUCE_US
305cf 45 5f 44 46 4c 54 20 29 3b 0a 20 20 61 73 73 65  E_DFLT );.  asse
305d0 72 74 28 20 69 4c 6f 6f 6b 41 68 65 61 64 21 3d  rt( iLookAhead!=
305d1 59 59 4e 4f 43 4f 44 45 20 29 3b 0a 20 20 69 20  YYNOCODE );.  i 
305d2 2b 3d 20 69 4c 6f 6f 6b 41 68 65 61 64 3b 0a 23  += iLookAhead;.#
305d3 69 66 64 65 66 20 59 59 45 52 52 4f 52 53 59 4d  ifdef YYERRORSYM
305d4 42 4f 4c 0a 20 20 69 66 28 20 69 3c 30 20 7c 7c  BOL.  if( i<0 ||
305d5 20 69 3e 3d 59 59 5f 53 5a 5f 41 43 54 54 41 42   i>=YY_SZ_ACTTAB
305d6 20 7c 7c 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64   || yy_lookahead
305d7 5b 69 5d 21 3d 69 4c 6f 6f 6b 41 68 65 61 64 20  [i]!=iLookAhead 
305d8 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 79 79  ){.    return yy
305d9 5f 64 65 66 61 75 6c 74 5b 73 74 61 74 65 6e 6f  _default[stateno
305da 5d 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 61  ];.  }.#else.  a
305db 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69  ssert( i>=0 && i
305dc 3c 59 59 5f 53 5a 5f 41 43 54 54 41 42 20 29 3b  <YY_SZ_ACTTAB );
305dd 0a 20 20 61 73 73 65 72 74 28 20 79 79 5f 6c 6f  .  assert( yy_lo
305de 6f 6b 61 68 65 61 64 5b 69 5d 3d 3d 69 4c 6f 6f  okahead[i]==iLoo
305df 6b 41 68 65 61 64 20 29 3b 0a 23 65 6e 64 69 66  kAhead );.#endif
305e0 0a 20 20 72 65 74 75 72 6e 20 79 79 5f 61 63 74  .  return yy_act
305e1 69 6f 6e 5b 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ion[i];.}../*.**
305e2 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   The following r
305e3 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
305e4 20 69 66 20 74 68 65 20 73 74 61 63 6b 20 6f 76   if the stack ov
305e5 65 72 66 6c 6f 77 73 2e 0a 2a 2f 0a 73 74 61 74  erflows..*/.stat
305e6 69 63 20 76 6f 69 64 20 79 79 53 74 61 63 6b 4f  ic void yyStackO
305e7 76 65 72 66 6c 6f 77 28 79 79 50 61 72 73 65 72  verflow(yyParser
305e8 20 2a 79 79 70 50 61 72 73 65 72 2c 20 59 59 4d   *yypParser, YYM
305e9 49 4e 4f 52 54 59 50 45 20 2a 79 79 70 4d 69 6e  INORTYPE *yypMin
305ea 6f 72 29 7b 0a 20 20 20 73 71 6c 69 74 65 33 50  or){.   sqlite3P
305eb 61 72 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a  arserARG_FETCH;.
305ec 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79     yypParser->yy
305ed 69 64 78 2d 2d 3b 0a 23 69 66 6e 64 65 66 20 4e  idx--;.#ifndef N
305ee 44 45 42 55 47 0a 20 20 20 69 66 28 20 79 79 54  DEBUG.   if( yyT
305ef 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20  raceFILE ){.    
305f0 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65   fprintf(yyTrace
305f1 46 49 4c 45 2c 22 25 73 53 74 61 63 6b 20 4f 76  FILE,"%sStack Ov
305f2 65 72 66 6c 6f 77 21 5c 6e 22 2c 79 79 54 72 61  erflow!\n",yyTra
305f3 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20 7d 0a  cePrompt);.   }.
305f4 23 65 6e 64 69 66 0a 20 20 20 77 68 69 6c 65 28  #endif.   while(
305f5 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64   yypParser->yyid
305f6 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70 5f 70 61  x>=0 ) yy_pop_pa
305f7 72 73 65 72 5f 73 74 61 63 6b 28 79 79 70 50 61  rser_stack(yypPa
305f8 72 73 65 72 29 3b 0a 20 20 20 2f 2a 20 48 65 72  rser);.   /* Her
305f9 65 20 63 6f 64 65 20 69 73 20 69 6e 73 65 72 74  e code is insert
305fa 65 64 20 77 68 69 63 68 20 77 69 6c 6c 20 65 78  ed which will ex
305fb 65 63 75 74 65 20 69 66 20 74 68 65 20 70 61 72  ecute if the par
305fc 73 65 72 0a 20 20 20 2a 2a 20 73 74 61 63 6b 20  ser.   ** stack 
305fd 65 76 65 72 79 20 6f 76 65 72 66 6c 6f 77 73 20  every overflows 
305fe 2a 2f 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  */..  UNUSED_PAR
305ff 41 4d 45 54 45 52 28 79 79 70 4d 69 6e 6f 72 29  AMETER(yypMinor)
30600 3b 20 2f 2a 20 53 69 6c 65 6e 63 65 20 73 6f 6d  ; /* Silence som
30601 65 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69  e compiler warni
30602 6e 67 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ngs */.  sqlite3
30603 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
30604 20 22 70 61 72 73 65 72 20 73 74 61 63 6b 20 6f   "parser stack o
30605 76 65 72 66 6c 6f 77 22 29 3b 0a 20 20 70 50 61  verflow");.  pPa
30606 72 73 65 2d 3e 70 61 72 73 65 45 72 72 6f 72 20  rse->parseError 
30607 3d 20 31 3b 0a 20 20 20 73 71 6c 69 74 65 33 50  = 1;.   sqlite3P
30608 61 72 73 65 72 41 52 47 5f 53 54 4f 52 45 3b 20  arserARG_STORE; 
30609 2f 2a 20 53 75 70 70 72 65 73 73 20 77 61 72 6e  /* Suppress warn
3060a 69 6e 67 20 61 62 6f 75 74 20 75 6e 75 73 65 64  ing about unused
3060b 20 25 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74   %extra_argument
3060c 20 76 61 72 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a   var */.}../*.**
3060d 20 50 65 72 66 6f 72 6d 20 61 20 73 68 69 66 74   Perform a shift
3060e 20 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74   action..*/.stat
3060f 69 63 20 76 6f 69 64 20 79 79 5f 73 68 69 66 74  ic void yy_shift
30610 28 0a 20 20 79 79 50 61 72 73 65 72 20 2a 79 79  (.  yyParser *yy
30611 70 50 61 72 73 65 72 2c 20 20 20 20 20 20 20 20  pParser,        
30612 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
30613 74 6f 20 62 65 20 73 68 69 66 74 65 64 20 2a 2f  to be shifted */
30614 0a 20 20 69 6e 74 20 79 79 4e 65 77 53 74 61 74  .  int yyNewStat
30615 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
30616 20 2f 2a 20 54 68 65 20 6e 65 77 20 73 74 61 74   /* The new stat
30617 65 20 74 6f 20 73 68 69 66 74 20 69 6e 20 2a 2f  e to shift in */
30618 0a 20 20 69 6e 74 20 79 79 4d 61 6a 6f 72 2c 20  .  int yyMajor, 
30619 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3061a 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74 6f   /* The major to
3061b 6b 65 6e 20 74 6f 20 73 68 69 66 74 20 69 6e 20  ken to shift in 
3061c 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50 45  */.  YYMINORTYPE
3061d 20 2a 79 79 70 4d 69 6e 6f 72 20 20 20 20 20 20   *yypMinor      
3061e 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
3061f 20 74 68 65 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e   the minor token
30620 20 74 6f 20 73 68 69 66 74 20 69 6e 20 2a 2f 0a   to shift in */.
30621 29 7b 0a 20 20 79 79 53 74 61 63 6b 45 6e 74 72  ){.  yyStackEntr
30622 79 20 2a 79 79 74 6f 73 3b 0a 20 20 79 79 70 50  y *yytos;.  yypP
30623 61 72 73 65 72 2d 3e 79 79 69 64 78 2b 2b 3b 0a  arser->yyidx++;.
30624 23 69 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41  #ifdef YYTRACKMA
30625 58 53 54 41 43 4b 44 45 50 54 48 0a 20 20 69 66  XSTACKDEPTH.  if
30626 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69  ( yypParser->yyi
30627 64 78 3e 79 79 70 50 61 72 73 65 72 2d 3e 79 79  dx>yypParser->yy
30628 69 64 78 4d 61 78 20 29 7b 0a 20 20 20 20 79 79  idxMax ){.    yy
30629 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 4d 61  pParser->yyidxMa
3062a 78 20 3d 20 79 79 70 50 61 72 73 65 72 2d 3e 79  x = yypParser->y
3062b 79 69 64 78 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  yidx;.  }.#endif
3062c 0a 23 69 66 20 59 59 53 54 41 43 4b 44 45 50 54  .#if YYSTACKDEPT
3062d 48 3e 30 20 0a 20 20 69 66 28 20 79 79 70 50 61  H>0 .  if( yypPa
3062e 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 59 59 53  rser->yyidx>=YYS
3062f 54 41 43 4b 44 45 50 54 48 20 29 7b 0a 20 20 20  TACKDEPTH ){.   
30630 20 79 79 53 74 61 63 6b 4f 76 65 72 66 6c 6f 77   yyStackOverflow
30631 28 79 79 70 50 61 72 73 65 72 2c 20 79 79 70 4d  (yypParser, yypM
30632 69 6e 6f 72 29 3b 0a 20 20 20 20 72 65 74 75 72  inor);.    retur
30633 6e 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69  n;.  }.#else.  i
30634 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  f( yypParser->yy
30635 69 64 78 3e 3d 79 79 70 50 61 72 73 65 72 2d 3e  idx>=yypParser->
30636 79 79 73 74 6b 73 7a 20 29 7b 0a 20 20 20 20 79  yystksz ){.    y
30637 79 47 72 6f 77 53 74 61 63 6b 28 79 79 70 50 61  yGrowStack(yypPa
30638 72 73 65 72 29 3b 0a 20 20 20 20 69 66 28 20 79  rser);.    if( y
30639 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e  ypParser->yyidx>
3063a 3d 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74  =yypParser->yyst
3063b 6b 73 7a 20 29 7b 0a 20 20 20 20 20 20 79 79 53  ksz ){.      yyS
3063c 74 61 63 6b 4f 76 65 72 66 6c 6f 77 28 79 79 70  tackOverflow(yyp
3063d 50 61 72 73 65 72 2c 20 79 79 70 4d 69 6e 6f 72  Parser, yypMinor
3063e 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  );.      return;
3063f 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
30640 66 0a 20 20 79 79 74 6f 73 20 3d 20 26 79 79 70  f.  yytos = &yyp
30641 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b  Parser->yystack[
30642 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
30643 5d 3b 0a 20 20 79 79 74 6f 73 2d 3e 73 74 61 74  ];.  yytos->stat
30644 65 6e 6f 20 3d 20 28 59 59 41 43 54 49 4f 4e 54  eno = (YYACTIONT
30645 59 50 45 29 79 79 4e 65 77 53 74 61 74 65 3b 0a  YPE)yyNewState;.
30646 20 20 79 79 74 6f 73 2d 3e 6d 61 6a 6f 72 20 3d    yytos->major =
30647 20 28 59 59 43 4f 44 45 54 59 50 45 29 79 79 4d   (YYCODETYPE)yyM
30648 61 6a 6f 72 3b 0a 20 20 79 79 74 6f 73 2d 3e 6d  ajor;.  yytos->m
30649 69 6e 6f 72 20 3d 20 2a 79 79 70 4d 69 6e 6f 72  inor = *yypMinor
3064a 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ;.#ifndef NDEBUG
3064b 0a 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49  .  if( yyTraceFI
3064c 4c 45 20 26 26 20 79 79 70 50 61 72 73 65 72 2d  LE && yypParser-
3064d 3e 79 79 69 64 78 3e 30 20 29 7b 0a 20 20 20 20  >yyidx>0 ){.    
3064e 69 6e 74 20 69 3b 0a 20 20 20 20 66 70 72 69 6e  int i;.    fprin
3064f 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22  tf(yyTraceFILE,"
30650 25 73 53 68 69 66 74 20 25 64 5c 6e 22 2c 79 79  %sShift %d\n",yy
30651 54 72 61 63 65 50 72 6f 6d 70 74 2c 79 79 4e 65  TracePrompt,yyNe
30652 77 53 74 61 74 65 29 3b 0a 20 20 20 20 66 70 72  wState);.    fpr
30653 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45  intf(yyTraceFILE
30654 2c 22 25 73 53 74 61 63 6b 3a 22 2c 79 79 54 72  ,"%sStack:",yyTr
30655 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20 20  acePrompt);.    
30656 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 79 79 70 50  for(i=1; i<=yypP
30657 61 72 73 65 72 2d 3e 79 79 69 64 78 3b 20 69 2b  arser->yyidx; i+
30658 2b 29 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66  +).      fprintf
30659 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 20 25  (yyTraceFILE," %
3065a 73 22 2c 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79  s",yyTokenName[y
3065b 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63  ypParser->yystac
3065c 6b 5b 69 5d 2e 6d 61 6a 6f 72 5d 29 3b 0a 20 20  k[i].major]);.  
3065d 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63    fprintf(yyTrac
3065e 65 46 49 4c 45 2c 22 5c 6e 22 29 3b 0a 20 20 7d  eFILE,"\n");.  }
3065f 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 20 54 68  .#endif.}../* Th
30660 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c  e following tabl
30661 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72  e contains infor
30662 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 76 65  mation about eve
30663 72 79 20 72 75 6c 65 20 74 68 61 74 0a 2a 2a 20  ry rule that.** 
30664 69 73 20 75 73 65 64 20 64 75 72 69 6e 67 20 74  is used during t
30665 68 65 20 72 65 64 75 63 65 2e 0a 2a 2f 0a 73 74  he reduce..*/.st
30666 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63  atic const struc
30667 74 20 7b 0a 20 20 59 59 43 4f 44 45 54 59 50 45  t {.  YYCODETYPE
30668 20 6c 68 73 3b 20 20 20 20 20 20 20 20 20 2f 2a   lhs;         /*
30669 20 53 79 6d 62 6f 6c 20 6f 6e 20 74 68 65 20 6c   Symbol on the l
3066a 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66  eft-hand side of
3066b 20 74 68 65 20 72 75 6c 65 20 2a 2f 0a 20 20 75   the rule */.  u
3066c 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6e 72 68  nsigned char nrh
3066d 73 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  s;     /* Number
3066e 20 6f 66 20 72 69 67 68 74 2d 68 61 6e 64 20 73   of right-hand s
3066f 69 64 65 20 73 79 6d 62 6f 6c 73 20 69 6e 20 74  ide symbols in t
30670 68 65 20 72 75 6c 65 20 2a 2f 0a 7d 20 79 79 52  he rule */.} yyR
30671 75 6c 65 49 6e 66 6f 5b 5d 20 3d 20 7b 0a 20 20  uleInfo[] = {.  
30672 7b 20 31 34 32 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 142, 1 },.  { 
30673 31 34 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34  143, 2 },.  { 14
30674 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 34 2c  3, 1 },.  { 144,
30675 20 31 20 7d 2c 0a 20 20 7b 20 31 34 34 2c 20 33   1 },.  { 144, 3
30676 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 30 20 7d   },.  { 145, 0 }
30677 2c 0a 20 20 7b 20 31 34 35 2c 20 31 20 7d 2c 0a  ,.  { 145, 1 },.
30678 20 20 7b 20 31 34 35 2c 20 33 20 7d 2c 0a 20 20    { 145, 3 },.  
30679 7b 20 31 34 36 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 146, 1 },.  { 
3067a 31 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34  147, 3 },.  { 14
3067b 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34 39 2c  9, 0 },.  { 149,
3067c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 39 2c 20 32   1 },.  { 149, 2
3067d 20 7d 2c 0a 20 20 7b 20 31 34 38 2c 20 30 20 7d   },.  { 148, 0 }
3067e 2c 0a 20 20 7b 20 31 34 38 2c 20 31 20 7d 2c 0a  ,.  { 148, 1 },.
3067f 20 20 7b 20 31 34 38 2c 20 31 20 7d 2c 0a 20 20    { 148, 1 },.  
30680 7b 20 31 34 38 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 148, 1 },.  { 
30681 31 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34  147, 2 },.  { 14
30682 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 2 },.  { 147,
30683 20 32 20 7d 2c 0a 20 20 7b 20 31 35 31 2c 20 31   2 },.  { 151, 1
30684 20 7d 2c 0a 20 20 7b 20 31 35 31 2c 20 30 20 7d   },.  { 151, 0 }
30685 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d 2c 0a  ,.  { 147, 2 },.
30686 20 20 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20 20    { 147, 3 },.  
30687 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 147, 5 },.  { 
30688 31 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 35  147, 2 },.  { 15
30689 32 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 35 34 2c  2, 6 },.  { 154,
3068a 20 31 20 7d 2c 0a 20 20 7b 20 31 35 36 2c 20 30   1 },.  { 156, 0
3068b 20 7d 2c 0a 20 20 7b 20 31 35 36 2c 20 33 20 7d   },.  { 156, 3 }
3068c 2c 0a 20 20 7b 20 31 35 35 2c 20 31 20 7d 2c 0a  ,.  { 155, 1 },.
3068d 20 20 7b 20 31 35 35 2c 20 30 20 7d 2c 0a 20 20    { 155, 0 },.  
3068e 7b 20 31 35 33 2c 20 34 20 7d 2c 0a 20 20 7b 20  { 153, 4 },.  { 
3068f 31 35 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 35  153, 2 },.  { 15
30690 38 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 35 38 2c  8, 3 },.  { 158,
30691 20 31 20 7d 2c 0a 20 20 7b 20 31 36 31 2c 20 33   1 },.  { 161, 3
30692 20 7d 2c 0a 20 20 7b 20 31 36 32 2c 20 31 20 7d   },.  { 162, 1 }
30693 2c 0a 20 20 7b 20 31 36 35 2c 20 31 20 7d 2c 0a  ,.  { 165, 1 },.
30694 20 20 7b 20 31 36 35 2c 20 31 20 7d 2c 0a 20 20    { 165, 1 },.  
30695 7b 20 31 36 36 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 166, 1 },.  { 
30696 31 35 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35  150, 1 },.  { 15
30697 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35 30 2c  0, 1 },.  { 150,
30698 20 31 20 7d 2c 0a 20 20 7b 20 31 36 33 2c 20 30   1 },.  { 163, 0
30699 20 7d 2c 0a 20 20 7b 20 31 36 33 2c 20 31 20 7d   },.  { 163, 1 }
3069a 2c 0a 20 20 7b 20 31 36 37 2c 20 31 20 7d 2c 0a  ,.  { 167, 1 },.
3069b 20 20 7b 20 31 36 37 2c 20 34 20 7d 2c 0a 20 20    { 167, 4 },.  
3069c 7b 20 31 36 37 2c 20 36 20 7d 2c 0a 20 20 7b 20  { 167, 6 },.  { 
3069d 31 36 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36  168, 1 },.  { 16
3069e 38 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 36 39 2c  8, 2 },.  { 169,
3069f 20 31 20 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 31   1 },.  { 169, 1
306a0 20 7d 2c 0a 20 20 7b 20 31 36 34 2c 20 32 20 7d   },.  { 164, 2 }
306a1 2c 0a 20 20 7b 20 31 36 34 2c 20 30 20 7d 2c 0a  ,.  { 164, 0 },.
306a2 20 20 7b 20 31 37 32 2c 20 33 20 7d 2c 0a 20 20    { 172, 3 },.  
306a3 7b 20 31 37 32 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 172, 1 },.  { 
306a4 31 37 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37  173, 2 },.  { 17
306a5 33 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 33 2c  3, 4 },.  { 173,
306a6 20 33 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 33   3 },.  { 173, 3
306a7 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 32 20 7d   },.  { 173, 2 }
306a8 2c 0a 20 20 7b 20 31 37 33 2c 20 32 20 7d 2c 0a  ,.  { 173, 2 },.
306a9 20 20 7b 20 31 37 33 2c 20 33 20 7d 2c 0a 20 20    { 173, 3 },.  
306aa 7b 20 31 37 33 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 173, 5 },.  { 
306ab 31 37 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37  173, 2 },.  { 17
306ac 33 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 33 2c  3, 4 },.  { 173,
306ad 20 34 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 31   4 },.  { 173, 1
306ae 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 32 20 7d   },.  { 173, 2 }
306af 2c 0a 20 20 7b 20 31 37 38 2c 20 30 20 7d 2c 0a  ,.  { 178, 0 },.
306b0 20 20 7b 20 31 37 38 2c 20 31 20 7d 2c 0a 20 20    { 178, 1 },.  
306b1 7b 20 31 38 30 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 180, 0 },.  { 
306b2 31 38 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38  180, 2 },.  { 18
306b3 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 32 2c  2, 2 },.  { 182,
306b4 20 33 20 7d 2c 0a 20 20 7b 20 31 38 32 2c 20 33   3 },.  { 182, 3
306b5 20 7d 2c 0a 20 20 7b 20 31 38 32 2c 20 33 20 7d   },.  { 182, 3 }
306b6 2c 0a 20 20 7b 20 31 38 33 2c 20 32 20 7d 2c 0a  ,.  { 183, 2 },.
306b7 20 20 7b 20 31 38 33 2c 20 32 20 7d 2c 0a 20 20    { 183, 2 },.  
306b8 7b 20 31 38 33 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 183, 1 },.  { 
306b9 31 38 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 38  183, 1 },.  { 18
306ba 31 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 38 31 2c  1, 3 },.  { 181,
306bb 20 32 20 7d 2c 0a 20 20 7b 20 31 38 34 2c 20 30   2 },.  { 184, 0
306bc 20 7d 2c 0a 20 20 7b 20 31 38 34 2c 20 32 20 7d   },.  { 184, 2 }
306bd 2c 0a 20 20 7b 20 31 38 34 2c 20 32 20 7d 2c 0a  ,.  { 184, 2 },.
306be 20 20 7b 20 31 35 39 2c 20 30 20 7d 2c 0a 20 20    { 159, 0 },.  
306bf 7b 20 31 35 39 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 159, 2 },.  { 
306c0 31 38 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 38  185, 3 },.  { 18
306c1 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 35 2c  5, 2 },.  { 185,
306c2 20 31 20 7d 2c 0a 20 20 7b 20 31 38 36 2c 20 32   1 },.  { 186, 2
306c3 20 7d 2c 0a 20 20 7b 20 31 38 36 2c 20 37 20 7d   },.  { 186, 7 }
306c4 2c 0a 20 20 7b 20 31 38 36 2c 20 35 20 7d 2c 0a  ,.  { 186, 5 },.
306c5 20 20 7b 20 31 38 36 2c 20 35 20 7d 2c 0a 20 20    { 186, 5 },.  
306c6 7b 20 31 38 36 2c 20 31 30 20 7d 2c 0a 20 20 7b  { 186, 10 },.  {
306c7 20 31 38 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   188, 0 },.  { 1
306c8 38 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 36  88, 1 },.  { 176
306c9 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 37 36 2c 20  , 0 },.  { 176, 
306ca 33 20 7d 2c 0a 20 20 7b 20 31 38 39 2c 20 30 20  3 },.  { 189, 0 
306cb 7d 2c 0a 20 20 7b 20 31 38 39 2c 20 32 20 7d 2c  },.  { 189, 2 },
306cc 0a 20 20 7b 20 31 39 30 2c 20 31 20 7d 2c 0a 20  .  { 190, 1 },. 
306cd 20 7b 20 31 39 30 2c 20 31 20 7d 2c 0a 20 20 7b   { 190, 1 },.  {
306ce 20 31 39 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   190, 1 },.  { 1
306cf 34 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 39 32  47, 4 },.  { 192
306d0 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 39 32 2c 20  , 2 },.  { 192, 
306d1 30 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 38 20  0 },.  { 147, 8 
306d2 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 34 20 7d 2c  },.  { 147, 4 },
306d3 0a 20 20 7b 20 31 34 37 2c 20 31 20 7d 2c 0a 20  .  { 147, 1 },. 
306d4 20 7b 20 31 36 30 2c 20 31 20 7d 2c 0a 20 20 7b   { 160, 1 },.  {
306d5 20 31 36 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   160, 3 },.  { 1
306d6 39 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 35  95, 1 },.  { 195
306d7 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 39 35 2c 20  , 2 },.  { 195, 
306d8 31 20 7d 2c 0a 20 20 7b 20 31 39 34 2c 20 39 20  1 },.  { 194, 9 
306d9 7d 2c 0a 20 20 7b 20 31 39 36 2c 20 31 20 7d 2c  },.  { 196, 1 },
306da 0a 20 20 7b 20 31 39 36 2c 20 31 20 7d 2c 0a 20  .  { 196, 1 },. 
306db 20 7b 20 31 39 36 2c 20 30 20 7d 2c 0a 20 20 7b   { 196, 0 },.  {
306dc 20 32 30 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   204, 2 },.  { 2
306dd 30 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 37  04, 0 },.  { 197
306de 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 39 37 2c 20  , 3 },.  { 197, 
306df 32 20 7d 2c 0a 20 20 7b 20 31 39 37 2c 20 34 20  2 },.  { 197, 4 
306e0 7d 2c 0a 20 20 7b 20 32 30 35 2c 20 32 20 7d 2c  },.  { 205, 2 },
306e1 0a 20 20 7b 20 32 30 35 2c 20 31 20 7d 2c 0a 20  .  { 205, 1 },. 
306e2 20 7b 20 32 30 35 2c 20 30 20 7d 2c 0a 20 20 7b   { 205, 0 },.  {
306e3 20 31 39 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   198, 0 },.  { 1
306e4 39 38 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 37  98, 2 },.  { 207
306e5 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 37 2c 20  , 2 },.  { 207, 
306e6 30 20 7d 2c 0a 20 20 7b 20 32 30 36 2c 20 37 20  0 },.  { 206, 7 
306e7 7d 2c 0a 20 20 7b 20 32 30 36 2c 20 37 20 7d 2c  },.  { 206, 7 },
306e8 0a 20 20 7b 20 32 30 36 2c 20 37 20 7d 2c 0a 20  .  { 206, 7 },. 
306e9 20 7b 20 31 35 37 2c 20 30 20 7d 2c 0a 20 20 7b   { 157, 0 },.  {
306ea 20 31 35 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   157, 2 },.  { 1
306eb 39 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 38  93, 2 },.  { 208
306ec 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 30 38 2c 20  , 1 },.  { 208, 
306ed 32 20 7d 2c 0a 20 20 7b 20 32 30 38 2c 20 33 20  2 },.  { 208, 3 
306ee 7d 2c 0a 20 20 7b 20 32 30 38 2c 20 34 20 7d 2c  },.  { 208, 4 },
306ef 0a 20 20 7b 20 32 31 30 2c 20 32 20 7d 2c 0a 20  .  { 210, 2 },. 
306f0 20 7b 20 32 31 30 2c 20 30 20 7d 2c 0a 20 20 7b   { 210, 0 },.  {
306f1 20 32 30 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   209, 0 },.  { 2
306f2 30 39 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 30 39  09, 3 },.  { 209
306f3 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31 31 2c 20  , 2 },.  { 211, 
306f4 34 20 7d 2c 0a 20 20 7b 20 32 31 31 2c 20 30 20  4 },.  { 211, 0 
306f5 7d 2c 0a 20 20 7b 20 32 30 32 2c 20 30 20 7d 2c  },.  { 202, 0 },
306f6 0a 20 20 7b 20 32 30 32 2c 20 33 20 7d 2c 0a 20  .  { 202, 3 },. 
306f7 20 7b 20 32 31 34 2c 20 34 20 7d 2c 0a 20 20 7b   { 214, 4 },.  {
306f8 20 32 31 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   214, 2 },.  { 2
306f9 31 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 37  15, 1 },.  { 177
306fa 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 37 2c 20  , 1 },.  { 177, 
306fb 31 20 7d 2c 0a 20 20 7b 20 31 37 37 2c 20 30 20  1 },.  { 177, 0 
306fc 7d 2c 0a 20 20 7b 20 32 30 30 2c 20 30 20 7d 2c  },.  { 200, 0 },
306fd 0a 20 20 7b 20 32 30 30 2c 20 33 20 7d 2c 0a 20  .  { 200, 3 },. 
306fe 20 7b 20 32 30 31 2c 20 30 20 7d 2c 0a 20 20 7b   { 201, 0 },.  {
306ff 20 32 30 31 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   201, 2 },.  { 2
30700 30 33 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 33  03, 0 },.  { 203
30701 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 33 2c 20  , 2 },.  { 203, 
30702 34 20 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 34 20  4 },.  { 203, 4 
30703 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c  },.  { 147, 5 },
30704 0a 20 20 7b 20 31 39 39 2c 20 30 20 7d 2c 0a 20  .  { 199, 0 },. 
30705 20 7b 20 31 39 39 2c 20 32 20 7d 2c 0a 20 20 7b   { 199, 2 },.  {
30706 20 31 34 37 2c 20 37 20 7d 2c 0a 20 20 7b 20 32   147, 7 },.  { 2
30707 31 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 31 37  17, 5 },.  { 217
30708 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 3 },.  { 147, 
30709 38 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 35 20  8 },.  { 147, 5 
3070a 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d 2c  },.  { 147, 6 },
3070b 0a 20 20 7b 20 32 31 38 2c 20 32 20 7d 2c 0a 20  .  { 218, 2 },. 
3070c 20 7b 20 32 31 38 2c 20 31 20 7d 2c 0a 20 20 7b   { 218, 1 },.  {
3070d 20 32 32 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   220, 3 },.  { 2
3070e 32 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 31 39  20, 1 },.  { 219
3070f 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 31 39 2c 20  , 0 },.  { 219, 
30710 33 20 7d 2c 0a 20 20 7b 20 32 31 33 2c 20 33 20  3 },.  { 213, 3 
30711 7d 2c 0a 20 20 7b 20 32 31 33 2c 20 31 20 7d 2c  },.  { 213, 1 },
30712 0a 20 20 7b 20 31 37 35 2c 20 31 20 7d 2c 0a 20  .  { 175, 1 },. 
30713 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b   { 175, 3 },.  {
30714 20 31 37 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   174, 1 },.  { 1
30715 37 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 1 },.  { 175
30716 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 1 },.  { 175, 
30717 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 35 20  3 },.  { 175, 5 
30718 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 31 20 7d 2c  },.  { 174, 1 },
30719 0a 20 20 7b 20 31 37 34 2c 20 31 20 7d 2c 0a 20  .  { 174, 1 },. 
3071a 20 7b 20 31 37 35 2c 20 31 20 7d 2c 0a 20 20 7b   { 175, 1 },.  {
3071b 20 31 37 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   175, 1 },.  { 1
3071c 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 3 },.  { 175
3071d 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 6 },.  { 175, 
3071e 35 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 34 20  5 },.  { 175, 4 
3071f 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 31 20 7d 2c  },.  { 174, 1 },
30720 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20  .  { 175, 3 },. 
30721 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b   { 175, 3 },.  {
30722 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   175, 3 },.  { 1
30723 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 3 },.  { 175
30724 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 3 },.  { 175, 
30725 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20  3 },.  { 175, 3 
30726 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c  },.  { 175, 3 },
30727 0a 20 20 7b 20 32 32 32 2c 20 31 20 7d 2c 0a 20  .  { 222, 1 },. 
30728 20 7b 20 32 32 32 2c 20 32 20 7d 2c 0a 20 20 7b   { 222, 2 },.  {
30729 20 32 32 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   222, 1 },.  { 2
3072a 32 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 32 33  22, 2 },.  { 223
3072b 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 32 33 2c 20  , 2 },.  { 223, 
3072c 30 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 34 20  0 },.  { 175, 4 
3072d 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 32 20 7d 2c  },.  { 175, 2 },
3072e 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20  .  { 175, 3 },. 
3072f 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b   { 175, 3 },.  {
30730 20 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b 20 31   175, 4 },.  { 1
30731 37 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 2 },.  { 175
30732 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 2 },.  { 175, 
30733 32 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 32 20  2 },.  { 175, 2 
30734 7d 2c 0a 20 20 7b 20 32 32 34 2c 20 31 20 7d 2c  },.  { 224, 1 },
30735 0a 20 20 7b 20 32 32 34 2c 20 32 20 7d 2c 0a 20  .  { 224, 2 },. 
30736 20 7b 20 31 37 35 2c 20 35 20 7d 2c 0a 20 20 7b   { 175, 5 },.  {
30737 20 32 32 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   225, 1 },.  { 2
30738 32 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 35  25, 2 },.  { 175
30739 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 5 },.  { 175, 
3073a 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 35 20  3 },.  { 175, 5 
3073b 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 34 20 7d 2c  },.  { 175, 4 },
3073c 0a 20 20 7b 20 31 37 35 2c 20 34 20 7d 2c 0a 20  .  { 175, 4 },. 
3073d 20 7b 20 31 37 35 2c 20 35 20 7d 2c 0a 20 20 7b   { 175, 5 },.  {
3073e 20 32 32 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 32   227, 5 },.  { 2
3073f 32 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 32 38  27, 4 },.  { 228
30740 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 32 38 2c 20  , 2 },.  { 228, 
30741 30 20 7d 2c 0a 20 20 7b 20 32 32 36 2c 20 31 20  0 },.  { 226, 1 
30742 7d 2c 0a 20 20 7b 20 32 32 36 2c 20 30 20 7d 2c  },.  { 226, 0 },
30743 0a 20 20 7b 20 32 32 31 2c 20 31 20 7d 2c 0a 20  .  { 221, 1 },. 
30744 20 7b 20 32 32 31 2c 20 30 20 7d 2c 0a 20 20 7b   { 221, 0 },.  {
30745 20 32 31 36 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   216, 3 },.  { 2
30746 31 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37  16, 1 },.  { 147
30747 2c 20 31 31 20 7d 2c 0a 20 20 7b 20 32 32 39 2c  , 11 },.  { 229,
30748 20 31 20 7d 2c 0a 20 20 7b 20 32 32 39 2c 20 30   1 },.  { 229, 0
30749 20 7d 2c 0a 20 20 7b 20 31 37 39 2c 20 30 20 7d   },.  { 179, 0 }
3074a 2c 0a 20 20 7b 20 31 37 39 2c 20 33 20 7d 2c 0a  ,.  { 179, 3 },.
3074b 20 20 7b 20 31 38 37 2c 20 35 20 7d 2c 0a 20 20    { 187, 5 },.  
3074c 7b 20 31 38 37 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 187, 3 },.  { 
3074d 32 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 33  230, 0 },.  { 23
3074e 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  0, 2 },.  { 147,
3074f 20 34 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31   4 },.  { 147, 1
30750 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d   },.  { 147, 2 }
30751 2c 0a 20 20 7b 20 31 34 37 2c 20 33 20 7d 2c 0a  ,.  { 147, 3 },.
30752 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20    { 147, 5 },.  
30753 7b 20 31 34 37 2c 20 36 20 7d 2c 0a 20 20 7b 20  { 147, 6 },.  { 
30754 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 34  147, 5 },.  { 14
30755 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 32 33 31 2c  7, 6 },.  { 231,
30756 20 31 20 7d 2c 0a 20 20 7b 20 32 33 31 2c 20 31   1 },.  { 231, 1
30757 20 7d 2c 0a 20 20 7b 20 32 33 31 2c 20 31 20 7d   },.  { 231, 1 }
30758 2c 0a 20 20 7b 20 32 33 31 2c 20 31 20 7d 2c 0a  ,.  { 231, 1 },.
30759 20 20 7b 20 32 33 31 2c 20 31 20 7d 2c 0a 20 20    { 231, 1 },.  
3075a 7b 20 31 37 30 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 170, 2 },.  { 
3075b 31 37 31 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33  171, 2 },.  { 23
3075c 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 32 2c  3, 1 },.  { 232,
3075d 20 31 20 7d 2c 0a 20 20 7b 20 32 33 32 2c 20 30   1 },.  { 232, 0
3075e 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 35 20 7d   },.  { 147, 5 }
3075f 2c 0a 20 20 7b 20 32 33 34 2c 20 31 31 20 7d 2c  ,.  { 234, 11 },
30760 0a 20 20 7b 20 32 33 36 2c 20 31 20 7d 2c 0a 20  .  { 236, 1 },. 
30761 20 7b 20 32 33 36 2c 20 31 20 7d 2c 0a 20 20 7b   { 236, 1 },.  {
30762 20 32 33 36 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   236, 2 },.  { 2
30763 33 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 33 37  36, 0 },.  { 237
30764 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 37 2c 20  , 1 },.  { 237, 
30765 31 20 7d 2c 0a 20 20 7b 20 32 33 37 2c 20 33 20  1 },.  { 237, 3 
30766 7d 2c 0a 20 20 7b 20 32 33 38 2c 20 30 20 7d 2c  },.  { 238, 0 },
30767 0a 20 20 7b 20 32 33 38 2c 20 33 20 7d 2c 0a 20  .  { 238, 3 },. 
30768 20 7b 20 32 33 39 2c 20 30 20 7d 2c 0a 20 20 7b   { 239, 0 },.  {
30769 20 32 33 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   239, 2 },.  { 2
3076a 33 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 33 35  35, 3 },.  { 235
3076b 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 34 31 2c 20  , 2 },.  { 241, 
3076c 31 20 7d 2c 0a 20 20 7b 20 32 34 31 2c 20 33 20  1 },.  { 241, 3 
3076d 7d 2c 0a 20 20 7b 20 32 34 32 2c 20 30 20 7d 2c  },.  { 242, 0 },
3076e 0a 20 20 7b 20 32 34 32 2c 20 33 20 7d 2c 0a 20  .  { 242, 3 },. 
3076f 20 7b 20 32 34 32 2c 20 32 20 7d 2c 0a 20 20 7b   { 242, 2 },.  {
30770 20 32 34 30 2c 20 37 20 7d 2c 0a 20 20 7b 20 32   240, 7 },.  { 2
30771 34 30 2c 20 38 20 7d 2c 0a 20 20 7b 20 32 34 30  40, 8 },.  { 240
30772 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 34 30 2c 20  , 5 },.  { 240, 
30773 35 20 7d 2c 0a 20 20 7b 20 32 34 30 2c 20 31 20  5 },.  { 240, 1 
30774 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 34 20 7d 2c  },.  { 175, 4 },
30775 0a 20 20 7b 20 31 37 35 2c 20 36 20 7d 2c 0a 20  .  { 175, 6 },. 
30776 20 7b 20 31 39 31 2c 20 31 20 7d 2c 0a 20 20 7b   { 191, 1 },.  {
30777 20 31 39 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   191, 1 },.  { 1
30778 39 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37  91, 1 },.  { 147
30779 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 4 },.  { 147, 
3077a 36 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 33 20  6 },.  { 147, 3 
3077b 7d 2c 0a 20 20 7b 20 32 34 34 2c 20 30 20 7d 2c  },.  { 244, 0 },
3077c 0a 20 20 7b 20 32 34 34 2c 20 32 20 7d 2c 0a 20  .  { 244, 2 },. 
3077d 20 7b 20 32 34 33 2c 20 31 20 7d 2c 0a 20 20 7b   { 243, 1 },.  {
3077e 20 32 34 33 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   243, 0 },.  { 1
3077f 34 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37  47, 1 },.  { 147
30780 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 3 },.  { 147, 
30781 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 33 20  1 },.  { 147, 3 
30782 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d 2c  },.  { 147, 6 },
30783 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0a 20  .  { 147, 6 },. 
30784 20 7b 20 32 34 35 2c 20 31 20 7d 2c 0a 20 20 7b   { 245, 1 },.  {
30785 20 32 34 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   246, 0 },.  { 2
30786 34 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37  46, 1 },.  { 147
30787 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 1 },.  { 147, 
30788 34 20 7d 2c 0a 20 20 7b 20 32 34 37 2c 20 37 20  4 },.  { 247, 7 
30789 7d 2c 0a 20 20 7b 20 32 34 38 2c 20 31 20 7d 2c  },.  { 248, 1 },
3078a 0a 20 20 7b 20 32 34 38 2c 20 33 20 7d 2c 0a 20  .  { 248, 3 },. 
3078b 20 7b 20 32 34 39 2c 20 30 20 7d 2c 0a 20 20 7b   { 249, 0 },.  {
3078c 20 32 34 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   249, 2 },.  { 2
3078d 35 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 35 30  50, 1 },.  { 250
3078e 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 35 31 2c 20  , 3 },.  { 251, 
3078f 31 20 7d 2c 0a 20 20 7b 20 32 35 32 2c 20 30 20  1 },.  { 252, 0 
30790 7d 2c 0a 20 20 7b 20 32 35 32 2c 20 34 20 7d 2c  },.  { 252, 4 },
30791 0a 20 20 7b 20 32 35 32 2c 20 32 20 7d 2c 0a 7d  .  { 252, 2 },.}
30792 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79  ;..static void y
30793 79 5f 61 63 63 65 70 74 28 79 79 50 61 72 73 65  y_accept(yyParse
30794 72 2a 29 3b 20 20 2f 2a 20 46 6f 72 77 61 72 64  r*);  /* Forward
30795 20 44 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a   Declaration */.
30796 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61  ./*.** Perform a
30797 20 72 65 64 75 63 65 20 61 63 74 69 6f 6e 20 61   reduce action a
30798 6e 64 20 74 68 65 20 73 68 69 66 74 20 74 68 61  nd the shift tha
30799 74 20 6d 75 73 74 20 69 6d 6d 65 64 69 61 74 65  t must immediate
3079a 6c 79 0a 2a 2a 20 66 6f 6c 6c 6f 77 20 74 68 65  ly.** follow the
3079b 20 72 65 64 75 63 65 2e 0a 2a 2f 0a 73 74 61 74   reduce..*/.stat
3079c 69 63 20 76 6f 69 64 20 79 79 5f 72 65 64 75 63  ic void yy_reduc
3079d 65 28 0a 20 20 79 79 50 61 72 73 65 72 20 2a 79  e(.  yyParser *y
3079e 79 70 50 61 72 73 65 72 2c 20 20 20 20 20 20 20  ypParser,       
3079f 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
307a0 2a 2f 0a 20 20 69 6e 74 20 79 79 72 75 6c 65 6e  */.  int yyrulen
307a1 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o               
307a2 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74    /* Number of t
307a3 68 65 20 72 75 6c 65 20 62 79 20 77 68 69 63 68  he rule by which
307a4 20 74 6f 20 72 65 64 75 63 65 20 2a 2f 0a 29 7b   to reduce */.){
307a5 0a 20 20 69 6e 74 20 79 79 67 6f 74 6f 3b 20 20  .  int yygoto;  
307a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
307a7 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 73     /* The next s
307a8 74 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 79 79  tate */.  int yy
307a9 61 63 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  act;            
307aa 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
307ab 20 6e 65 78 74 20 61 63 74 69 6f 6e 20 2a 2f 0a   next action */.
307ac 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 79 79    YYMINORTYPE yy
307ad 67 6f 74 6f 6d 69 6e 6f 72 3b 20 20 20 20 20 20  gotominor;      
307ae 20 20 2f 2a 20 54 68 65 20 4c 48 53 20 6f 66 20    /* The LHS of 
307af 74 68 65 20 72 75 6c 65 20 72 65 64 75 63 65 64  the rule reduced
307b0 20 2a 2f 0a 20 20 79 79 53 74 61 63 6b 45 6e 74   */.  yyStackEnt
307b1 72 79 20 2a 79 79 6d 73 70 3b 20 20 20 20 20 20  ry *yymsp;      
307b2 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 70        /* The top
307b3 20 6f 66 20 74 68 65 20 70 61 72 73 65 72 27 73   of the parser's
307b4 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20   stack */.  int 
307b5 79 79 73 69 7a 65 3b 20 20 20 20 20 20 20 20 20  yysize;         
307b6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
307b7 6d 6f 75 6e 74 20 74 6f 20 70 6f 70 20 74 68 65  mount to pop the
307b8 20 73 74 61 63 6b 20 2a 2f 0a 20 20 73 71 6c 69   stack */.  sqli
307b9 74 65 33 50 61 72 73 65 72 41 52 47 5f 46 45 54  te3ParserARG_FET
307ba 43 48 3b 0a 20 20 79 79 6d 73 70 20 3d 20 26 79  CH;.  yymsp = &y
307bb 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63  ypParser->yystac
307bc 6b 5b 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69  k[yypParser->yyi
307bd 64 78 5d 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45  dx];.#ifndef NDE
307be 42 55 47 0a 20 20 69 66 28 20 79 79 54 72 61 63  BUG.  if( yyTrac
307bf 65 46 49 4c 45 20 26 26 20 79 79 72 75 6c 65 6e  eFILE && yyrulen
307c0 6f 3e 3d 30 20 0a 20 20 20 20 20 20 20 20 26 26  o>=0 .        &&
307c1 20 79 79 72 75 6c 65 6e 6f 3c 28 69 6e 74 29 28   yyruleno<(int)(
307c2 73 69 7a 65 6f 66 28 79 79 52 75 6c 65 4e 61 6d  sizeof(yyRuleNam
307c3 65 29 2f 73 69 7a 65 6f 66 28 79 79 52 75 6c 65  e)/sizeof(yyRule
307c4 4e 61 6d 65 5b 30 5d 29 29 20 29 7b 0a 20 20 20  Name[0])) ){.   
307c5 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65   fprintf(yyTrace
307c6 46 49 4c 45 2c 20 22 25 73 52 65 64 75 63 65 20  FILE, "%sReduce 
307c7 5b 25 73 5d 2e 5c 6e 22 2c 20 79 79 54 72 61 63  [%s].\n", yyTrac
307c8 65 50 72 6f 6d 70 74 2c 0a 20 20 20 20 20 20 79  ePrompt,.      y
307c9 79 52 75 6c 65 4e 61 6d 65 5b 79 79 72 75 6c 65  yRuleName[yyrule
307ca 6e 6f 5d 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  no]);.  }.#endif
307cb 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 20   /* NDEBUG */.. 
307cc 20 2f 2a 20 53 69 6c 65 6e 63 65 20 63 6f 6d 70   /* Silence comp
307cd 6c 61 69 6e 74 73 20 66 72 6f 6d 20 70 75 72 69  laints from puri
307ce 66 79 20 61 62 6f 75 74 20 79 79 67 6f 74 6f 6d  fy about yygotom
307cf 69 6e 6f 72 20 62 65 69 6e 67 20 75 6e 69 6e 69  inor being unini
307d0 74 69 61 6c 69 7a 65 64 0a 20 20 2a 2a 20 69 6e  tialized.  ** in
307d1 20 73 6f 6d 65 20 63 61 73 65 73 20 77 68 65 6e   some cases when
307d2 20 69 74 20 69 73 20 63 6f 70 69 65 64 20 69 6e   it is copied in
307d3 74 6f 20 74 68 65 20 73 74 61 63 6b 20 61 66 74  to the stack aft
307d4 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  er the following
307d5 0a 20 20 2a 2a 20 73 77 69 74 63 68 2e 20 20 79  .  ** switch.  y
307d6 79 67 6f 74 6f 6d 69 6e 6f 72 20 69 73 20 75 6e  ygotominor is un
307d7 69 6e 69 74 69 61 6c 69 7a 65 64 20 77 68 65 6e  initialized when
307d8 20 61 20 72 75 6c 65 20 72 65 64 75 63 65 73 20   a rule reduces 
307d9 74 68 61 74 20 64 6f 65 73 0a 20 20 2a 2a 20 6e  that does.  ** n
307da 6f 74 20 73 65 74 20 74 68 65 20 76 61 6c 75 65  ot set the value
307db 20 6f 66 20 69 74 73 20 6c 65 66 74 2d 68 61 6e   of its left-han
307dc 64 20 73 69 64 65 20 6e 6f 6e 74 65 72 6d 69 6e  d side nontermin
307dd 61 6c 2e 20 20 4c 65 61 76 69 6e 67 20 74 68 65  al.  Leaving the
307de 0a 20 20 2a 2a 20 76 61 6c 75 65 20 6f 66 20 74  .  ** value of t
307df 68 65 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20 75  he nonterminal u
307e0 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 73 20  ninitialized is 
307e1 75 74 74 65 72 6c 79 20 68 61 72 6d 6c 65 73 73  utterly harmless
307e2 20 61 73 20 6c 6f 6e 67 0a 20 20 2a 2a 20 61 73   as long.  ** as
307e3 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 6e 65   the value is ne
307e4 76 65 72 20 75 73 65 64 2e 20 20 53 6f 20 72 65  ver used.  So re
307e5 61 6c 6c 79 20 74 68 65 20 6f 6e 6c 79 20 74 68  ally the only th
307e6 69 6e 67 20 74 68 69 73 20 63 6f 64 65 0a 20 20  ing this code.  
307e7 2a 2a 20 61 63 63 6f 6d 70 6c 69 73 68 65 73 20  ** accomplishes 
307e8 69 73 20 74 6f 20 71 75 69 65 74 65 6e 20 70 75  is to quieten pu
307e9 72 69 66 79 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a  rify.  .  **.  *
307ea 2a 20 32 30 30 37 2d 30 31 2d 31 36 3a 20 20 54  * 2007-01-16:  T
307eb 68 65 20 77 69 72 65 73 68 61 72 6b 20 70 72 6f  he wireshark pro
307ec 6a 65 63 74 20 28 77 77 77 2e 77 69 72 65 73 68  ject (www.wiresh
307ed 61 72 6b 2e 6f 72 67 29 20 72 65 70 6f 72 74 73  ark.org) reports
307ee 20 74 68 61 74 0a 20 20 2a 2a 20 77 69 74 68 6f   that.  ** witho
307ef 75 74 20 74 68 69 73 20 63 6f 64 65 2c 20 74 68  ut this code, th
307f0 65 69 72 20 70 61 72 73 65 72 20 73 65 67 66 61  eir parser segfa
307f1 75 6c 74 73 2e 20 20 49 27 6d 20 6e 6f 74 20 73  ults.  I'm not s
307f2 75 72 65 20 77 68 61 74 20 74 68 65 72 65 0a 20  ure what there. 
307f3 20 2a 2a 20 70 61 72 73 65 72 20 69 73 20 64 6f   ** parser is do
307f4 69 6e 67 20 74 6f 20 6d 61 6b 65 20 74 68 69 73  ing to make this
307f5 20 68 61 70 70 65 6e 2e 20 20 54 68 69 73 20 69   happen.  This i
307f6 73 20 74 68 65 20 73 65 63 6f 6e 64 20 62 75 67  s the second bug
307f7 20 72 65 70 6f 72 74 0a 20 20 2a 2a 20 66 72 6f   report.  ** fro
307f8 6d 20 77 69 72 65 73 68 61 72 6b 20 74 68 69 73  m wireshark this
307f9 20 77 65 65 6b 2e 20 20 43 6c 65 61 72 6c 79 20   week.  Clearly 
307fa 74 68 65 79 20 61 72 65 20 73 74 72 65 73 73 69  they are stressi
307fb 6e 67 20 4c 65 6d 6f 6e 20 69 6e 20 77 61 79 73  ng Lemon in ways
307fc 0a 20 20 2a 2a 20 74 68 61 74 20 69 74 20 68 61  .  ** that it ha
307fd 73 20 6e 6f 74 20 62 65 65 6e 20 70 72 65 76 69  s not been previ
307fe 6f 75 73 6c 79 20 73 74 72 65 73 73 65 64 2e 2e  ously stressed..
307ff 2e 20 20 28 53 51 4c 69 74 65 20 74 69 63 6b 65  .  (SQLite ticke
30800 74 20 23 32 31 37 32 29 0a 20 20 2a 2f 0a 20 20  t #2172).  */.  
30801 2f 2a 6d 65 6d 73 65 74 28 26 79 79 67 6f 74 6f  /*memset(&yygoto
30802 6d 69 6e 6f 72 2c 20 30 2c 20 73 69 7a 65 6f 66  minor, 0, sizeof
30803 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 29 29 3b 2a  (yygotominor));*
30804 2f 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 20  /.  yygotominor 
30805 3d 20 79 79 7a 65 72 6f 6d 69 6e 6f 72 3b 0a 0a  = yyzerominor;..
30806 0a 20 20 73 77 69 74 63 68 28 20 79 79 72 75 6c  .  switch( yyrul
30807 65 6e 6f 20 29 7b 0a 20 20 2f 2a 20 42 65 67 69  eno ){.  /* Begi
30808 6e 6e 69 6e 67 20 68 65 72 65 20 61 72 65 20 74  nning here are t
30809 68 65 20 72 65 64 75 63 74 69 6f 6e 20 63 61 73  he reduction cas
3080a 65 73 2e 20 20 41 20 74 79 70 69 63 61 6c 20 65  es.  A typical e
3080b 78 61 6d 70 6c 65 0a 20 20 2a 2a 20 66 6f 6c 6c  xample.  ** foll
3080c 6f 77 73 3a 0a 20 20 2a 2a 20 20 20 63 61 73 65  ows:.  **   case
3080d 20 30 3a 0a 20 20 2a 2a 20 20 23 6c 69 6e 65 20   0:.  **  #line 
3080e 3c 6c 69 6e 65 6e 6f 3e 20 3c 67 72 61 6d 6d 61  <lineno> <gramma
3080f 72 66 69 6c 65 3e 0a 20 20 2a 2a 20 20 20 20 20  rfile>.  **     
30810 7b 20 2e 2e 2e 20 7d 20 20 20 20 20 20 20 20 20  { ... }         
30811 20 20 2f 2f 20 55 73 65 72 20 73 75 70 70 6c 69    // User suppli
30812 65 64 20 63 6f 64 65 0a 20 20 2a 2a 20 20 23 6c  ed code.  **  #l
30813 69 6e 65 20 3c 6c 69 6e 65 6e 6f 3e 20 3c 74 68  ine <lineno> <th
30814 69 73 66 69 6c 65 3e 0a 20 20 2a 2a 20 20 20 20  isfile>.  **    
30815 20 62 72 65 61 6b 3b 0a 20 20 2a 2f 0a 20 20 20   break;.  */.   
30816 20 20 20 63 61 73 65 20 35 3a 20 2f 2a 20 65 78     case 5: /* ex
30817 70 6c 61 69 6e 20 3a 3a 3d 20 2a 2f 0a 7b 20 73  plain ::= */.{ s
30818 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73 65  qlite3BeginParse
30819 28 70 50 61 72 73 65 2c 20 30 29 3b 20 7d 0a 20  (pParse, 0); }. 
3081a 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3081b 20 20 20 20 63 61 73 65 20 36 3a 20 2f 2a 20 65      case 6: /* e
3081c 78 70 6c 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41  xplain ::= EXPLA
3081d 49 4e 20 2a 2f 0a 7b 20 73 71 6c 69 74 65 33 42  IN */.{ sqlite3B
3081e 65 67 69 6e 50 61 72 73 65 28 70 50 61 72 73 65  eginParse(pParse
3081f 2c 20 31 29 3b 20 7d 0a 20 20 20 20 20 20 20 20  , 1); }.        
30820 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
30821 65 20 37 3a 20 2f 2a 20 65 78 70 6c 61 69 6e 20  e 7: /* explain 
30822 3a 3a 3d 20 45 58 50 4c 41 49 4e 20 51 55 45 52  ::= EXPLAIN QUER
30823 59 20 50 4c 41 4e 20 2a 2f 0a 7b 20 73 71 6c 69  Y PLAN */.{ sqli
30824 74 65 33 42 65 67 69 6e 50 61 72 73 65 28 70 50  te3BeginParse(pP
30825 61 72 73 65 2c 20 32 29 3b 20 7d 0a 20 20 20 20  arse, 2); }.    
30826 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
30827 20 63 61 73 65 20 38 3a 20 2f 2a 20 63 6d 64 78   case 8: /* cmdx
30828 20 3a 3a 3d 20 63 6d 64 20 2a 2f 0a 7b 20 73 71   ::= cmd */.{ sq
30829 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e  lite3FinishCodin
3082a 67 28 70 50 61 72 73 65 29 3b 20 7d 0a 20 20 20  g(pParse); }.   
3082b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3082c 20 20 63 61 73 65 20 39 3a 20 2f 2a 20 63 6d 64    case 9: /* cmd
3082d 20 3a 3a 3d 20 42 45 47 49 4e 20 74 72 61 6e 73   ::= BEGIN trans
3082e 74 79 70 65 20 74 72 61 6e 73 5f 6f 70 74 20 2a  type trans_opt *
3082f 2f 0a 7b 73 71 6c 69 74 65 33 42 65 67 69 6e 54  /.{sqlite3BeginT
30830 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 72 73  ransaction(pPars
30831 65 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  e, yymsp[-1].min
30832 6f 72 2e 79 79 33 32 38 29 3b 7d 0a 20 20 20 20  or.yy328);}.    
30833 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
30834 20 63 61 73 65 20 31 33 3a 20 2f 2a 20 74 72 61   case 13: /* tra
30835 6e 73 74 79 70 65 20 3a 3a 3d 20 2a 2f 0a 7b 79  nstype ::= */.{y
30836 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38  ygotominor.yy328
30837 20 3d 20 54 4b 5f 44 45 46 45 52 52 45 44 3b 7d   = TK_DEFERRED;}
30838 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
30839 20 20 20 20 20 20 63 61 73 65 20 31 34 3a 20 2f        case 14: /
3083a 2a 20 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20  * transtype ::= 
3083b 44 45 46 45 52 52 45 44 20 2a 2f 0a 20 20 20 20  DEFERRED */.    
3083c 20 20 63 61 73 65 20 31 35 3a 20 2f 2a 20 74 72    case 15: /* tr
3083d 61 6e 73 74 79 70 65 20 3a 3a 3d 20 49 4d 4d 45  anstype ::= IMME
3083e 44 49 41 54 45 20 2a 2f 20 79 79 74 65 73 74 63  DIATE */ yytestc
3083f 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 35  ase(yyruleno==15
30840 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 36  );.      case 16
30841 3a 20 2f 2a 20 74 72 61 6e 73 74 79 70 65 20 3a  : /* transtype :
30842 3a 3d 20 45 58 43 4c 55 53 49 56 45 20 2a 2f 20  := EXCLUSIVE */ 
30843 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
30844 65 6e 6f 3d 3d 31 36 29 3b 0a 20 20 20 20 20 20  eno==16);.      
30845 63 61 73 65 20 31 31 34 3a 20 2f 2a 20 6d 75 6c  case 114: /* mul
30846 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20  tiselect_op ::= 
30847 55 4e 49 4f 4e 20 2a 2f 20 79 79 74 65 73 74 63  UNION */ yytestc
30848 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 31  ase(yyruleno==11
30849 34 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  4);.      case 1
3084a 31 36 3a 20 2f 2a 20 6d 75 6c 74 69 73 65 6c 65  16: /* multisele
3084b 63 74 5f 6f 70 20 3a 3a 3d 20 45 58 43 45 50 54  ct_op ::= EXCEPT
3084c 7c 49 4e 54 45 52 53 45 43 54 20 2a 2f 20 79 79  |INTERSECT */ yy
3084d 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3084e 6f 3d 3d 31 31 36 29 3b 0a 7b 79 79 67 6f 74 6f  o==116);.{yygoto
3084f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 79 79  minor.yy328 = yy
30850 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72 3b 7d 0a 20  msp[0].major;}. 
30851 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
30852 20 20 20 20 63 61 73 65 20 31 37 3a 20 2f 2a 20      case 17: /* 
30853 63 6d 64 20 3a 3a 3d 20 43 4f 4d 4d 49 54 20 74  cmd ::= COMMIT t
30854 72 61 6e 73 5f 6f 70 74 20 2a 2f 0a 20 20 20 20  rans_opt */.    
30855 20 20 63 61 73 65 20 31 38 3a 20 2f 2a 20 63 6d    case 18: /* cm
30856 64 20 3a 3a 3d 20 45 4e 44 20 74 72 61 6e 73 5f  d ::= END trans_
30857 6f 70 74 20 2a 2f 20 79 79 74 65 73 74 63 61 73  opt */ yytestcas
30858 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 38 29 3b  e(yyruleno==18);
30859 0a 7b 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 54  .{sqlite3CommitT
3085a 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 72 73  ransaction(pPars
3085b 65 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  e);}.        bre
3085c 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
3085d 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 4f  9: /* cmd ::= RO
3085e 4c 4c 42 41 43 4b 20 74 72 61 6e 73 5f 6f 70 74  LLBACK trans_opt
3085f 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 52 6f 6c 6c   */.{sqlite3Roll
30860 62 61 63 6b 54 72 61 6e 73 61 63 74 69 6f 6e 28  backTransaction(
30861 70 50 61 72 73 65 29 3b 7d 0a 20 20 20 20 20 20  pParse);}.      
30862 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
30863 61 73 65 20 32 32 3a 20 2f 2a 20 63 6d 64 20 3a  ase 22: /* cmd :
30864 3a 3d 20 53 41 56 45 50 4f 49 4e 54 20 6e 6d 20  := SAVEPOINT nm 
30865 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 53 61  */.{.  sqlite3Sa
30866 76 65 70 6f 69 6e 74 28 70 50 61 72 73 65 2c 20  vepoint(pParse, 
30867 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49 4e 2c  SAVEPOINT_BEGIN,
30868 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
30869 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20  .yy0);.}.       
3086a 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3086b 73 65 20 32 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a  se 23: /* cmd ::
3086c 3d 20 52 45 4c 45 41 53 45 20 73 61 76 65 70 6f  = RELEASE savepo
3086d 69 6e 74 5f 6f 70 74 20 6e 6d 20 2a 2f 0a 7b 0a  int_opt nm */.{.
3086e 20 20 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69    sqlite3Savepoi
3086f 6e 74 28 70 50 61 72 73 65 2c 20 53 41 56 45 50  nt(pParse, SAVEP
30870 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 26 79  OINT_RELEASE, &y
30871 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
30872 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
30873 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
30874 32 34 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52  24: /* cmd ::= R
30875 4f 4c 4c 42 41 43 4b 20 74 72 61 6e 73 5f 6f 70  OLLBACK trans_op
30876 74 20 54 4f 20 73 61 76 65 70 6f 69 6e 74 5f 6f  t TO savepoint_o
30877 70 74 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c  pt nm */.{.  sql
30878 69 74 65 33 53 61 76 65 70 6f 69 6e 74 28 70 50  ite3Savepoint(pP
30879 61 72 73 65 2c 20 53 41 56 45 50 4f 49 4e 54 5f  arse, SAVEPOINT_
3087a 52 4f 4c 4c 42 41 43 4b 2c 20 26 79 79 6d 73 70  ROLLBACK, &yymsp
3087b 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
3087c 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3087d 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 3a 20  .      case 26: 
3087e 2f 2a 20 63 72 65 61 74 65 5f 74 61 62 6c 65 20  /* create_table 
3087f 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 74 65 6d  ::= createkw tem
30880 70 20 54 41 42 4c 45 20 69 66 6e 6f 74 65 78 69  p TABLE ifnotexi
30881 73 74 73 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b  sts nm dbnm */.{
30882 0a 20 20 20 73 71 6c 69 74 65 33 53 74 61 72 74  .   sqlite3Start
30883 54 61 62 6c 65 28 70 50 61 72 73 65 2c 26 79 79  Table(pParse,&yy
30884 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
30885 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
30886 72 2e 79 79 30 2c 79 79 6d 73 70 5b 2d 34 5d 2e  r.yy0,yymsp[-4].
30887 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 30 2c 30 2c  minor.yy328,0,0,
30888 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
30889 79 79 33 32 38 29 3b 0a 7d 0a 20 20 20 20 20 20  yy328);.}.      
3088a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3088b 61 73 65 20 32 37 3a 20 2f 2a 20 63 72 65 61 74  ase 27: /* creat
3088c 65 6b 77 20 3a 3a 3d 20 43 52 45 41 54 45 20 2a  ekw ::= CREATE *
3088d 2f 0a 7b 0a 20 20 70 50 61 72 73 65 2d 3e 64 62  /.{.  pParse->db
3088e 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61  ->lookaside.bEna
3088f 62 6c 65 64 20 3d 20 30 3b 0a 20 20 79 79 67 6f  bled = 0;.  yygo
30890 74 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d 20 79 79  tominor.yy0 = yy
30891 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
30892 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
30893 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 38  k;.      case 28
30894 3a 20 2f 2a 20 69 66 6e 6f 74 65 78 69 73 74 73  : /* ifnotexists
30895 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61   ::= */.      ca
30896 73 65 20 33 31 3a 20 2f 2a 20 74 65 6d 70 20 3a  se 31: /* temp :
30897 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  := */ yytestcase
30898 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 31 29 3b 0a  (yyruleno==31);.
30899 20 20 20 20 20 20 63 61 73 65 20 37 30 3a 20 2f        case 70: /
3089a 2a 20 61 75 74 6f 69 6e 63 20 3a 3a 3d 20 2a 2f  * autoinc ::= */
3089b 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3089c 6c 65 6e 6f 3d 3d 37 30 29 3b 0a 20 20 20 20 20  leno==70);.     
3089d 20 63 61 73 65 20 38 34 3a 20 2f 2a 20 69 6e 69   case 84: /* ini
3089e 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f  t_deferred_pred_
3089f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73  opt ::= */ yytes
308a0 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
308a1 38 34 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  84);.      case 
308a2 38 36 3a 20 2f 2a 20 69 6e 69 74 5f 64 65 66 65  86: /* init_defe
308a3 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a  rred_pred_opt ::
308a4 3d 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45  = INITIALLY IMME
308a5 44 49 41 54 45 20 2a 2f 20 79 79 74 65 73 74 63  DIATE */ yytestc
308a6 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 38 36  ase(yyruleno==86
308a7 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39 37  );.      case 97
308a8 3a 20 2f 2a 20 64 65 66 65 72 5f 73 75 62 63 6c  : /* defer_subcl
308a9 61 75 73 65 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20  ause_opt ::= */ 
308aa 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
308ab 65 6e 6f 3d 3d 39 37 29 3b 0a 20 20 20 20 20 20  eno==97);.      
308ac 63 61 73 65 20 31 30 38 3a 20 2f 2a 20 69 66 65  case 108: /* ife
308ad 78 69 73 74 73 20 3a 3a 3d 20 2a 2f 20 79 79 74  xists ::= */ yyt
308ae 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
308af 3d 3d 31 30 38 29 3b 0a 20 20 20 20 20 20 63 61  ==108);.      ca
308b0 73 65 20 31 31 39 3a 20 2f 2a 20 64 69 73 74 69  se 119: /* disti
308b1 6e 63 74 20 3a 3a 3d 20 41 4c 4c 20 2a 2f 20 79  nct ::= ALL */ y
308b2 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
308b3 6e 6f 3d 3d 31 31 39 29 3b 0a 20 20 20 20 20 20  no==119);.      
308b4 63 61 73 65 20 31 32 30 3a 20 2f 2a 20 64 69 73  case 120: /* dis
308b5 74 69 6e 63 74 20 3a 3a 3d 20 2a 2f 20 79 79 74  tinct ::= */ yyt
308b6 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
308b7 3d 3d 31 32 30 29 3b 0a 20 20 20 20 20 20 63 61  ==120);.      ca
308b8 73 65 20 32 32 32 3a 20 2f 2a 20 62 65 74 77 65  se 222: /* betwe
308b9 65 6e 5f 6f 70 20 3a 3a 3d 20 42 45 54 57 45 45  en_op ::= BETWEE
308ba 4e 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  N */ yytestcase(
308bb 79 79 72 75 6c 65 6e 6f 3d 3d 32 32 32 29 3b 0a  yyruleno==222);.
308bc 20 20 20 20 20 20 63 61 73 65 20 32 32 35 3a 20        case 225: 
308bd 2f 2a 20 69 6e 5f 6f 70 20 3a 3a 3d 20 49 4e 20  /* in_op ::= IN 
308be 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
308bf 72 75 6c 65 6e 6f 3d 3d 32 32 35 29 3b 0a 7b 79  ruleno==225);.{y
308c0 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38  ygotominor.yy328
308c1 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62   = 0;}.        b
308c2 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
308c3 20 32 39 3a 20 2f 2a 20 69 66 6e 6f 74 65 78 69   29: /* ifnotexi
308c4 73 74 73 20 3a 3a 3d 20 49 46 20 4e 4f 54 20 45  sts ::= IF NOT E
308c5 58 49 53 54 53 20 2a 2f 0a 20 20 20 20 20 20 63  XISTS */.      c
308c6 61 73 65 20 33 30 3a 20 2f 2a 20 74 65 6d 70 20  ase 30: /* temp 
308c7 3a 3a 3d 20 54 45 4d 50 20 2a 2f 20 79 79 74 65  ::= TEMP */ yyte
308c8 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
308c9 3d 33 30 29 3b 0a 20 20 20 20 20 20 63 61 73 65  =30);.      case
308ca 20 37 31 3a 20 2f 2a 20 61 75 74 6f 69 6e 63 20   71: /* autoinc 
308cb 3a 3a 3d 20 41 55 54 4f 49 4e 43 52 20 2a 2f 20  ::= AUTOINCR */ 
308cc 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
308cd 65 6e 6f 3d 3d 37 31 29 3b 0a 20 20 20 20 20 20  eno==71);.      
308ce 63 61 73 65 20 38 35 3a 20 2f 2a 20 69 6e 69 74  case 85: /* init
308cf 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f  _deferred_pred_o
308d0 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59  pt ::= INITIALLY
308d1 20 44 45 46 45 52 52 45 44 20 2a 2f 20 79 79 74   DEFERRED */ yyt
308d2 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
308d3 3d 3d 38 35 29 3b 0a 20 20 20 20 20 20 63 61 73  ==85);.      cas
308d4 65 20 31 30 37 3a 20 2f 2a 20 69 66 65 78 69 73  e 107: /* ifexis
308d5 74 73 20 3a 3a 3d 20 49 46 20 45 58 49 53 54 53  ts ::= IF EXISTS
308d6 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
308d7 79 72 75 6c 65 6e 6f 3d 3d 31 30 37 29 3b 0a 20  yruleno==107);. 
308d8 20 20 20 20 20 63 61 73 65 20 31 31 38 3a 20 2f       case 118: /
308d9 2a 20 64 69 73 74 69 6e 63 74 20 3a 3a 3d 20 44  * distinct ::= D
308da 49 53 54 49 4e 43 54 20 2a 2f 20 79 79 74 65 73  ISTINCT */ yytes
308db 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
308dc 31 31 38 29 3b 0a 20 20 20 20 20 20 63 61 73 65  118);.      case
308dd 20 32 32 33 3a 20 2f 2a 20 62 65 74 77 65 65 6e   223: /* between
308de 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20 42 45 54 57  _op ::= NOT BETW
308df 45 45 4e 20 2a 2f 20 79 79 74 65 73 74 63 61 73  EEN */ yytestcas
308e0 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 32 33 29  e(yyruleno==223)
308e1 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 36  ;.      case 226
308e2 3a 20 2f 2a 20 69 6e 5f 6f 70 20 3a 3a 3d 20 4e  : /* in_op ::= N
308e3 4f 54 20 49 4e 20 2a 2f 20 79 79 74 65 73 74 63  OT IN */ yytestc
308e4 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 32  ase(yyruleno==22
308e5 36 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72  6);.{yygotominor
308e6 2e 79 79 33 32 38 20 3d 20 31 3b 7d 0a 20 20 20  .yy328 = 1;}.   
308e7 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
308e8 20 20 63 61 73 65 20 33 32 3a 20 2f 2a 20 63 72    case 32: /* cr
308e9 65 61 74 65 5f 74 61 62 6c 65 5f 61 72 67 73 20  eate_table_args 
308ea 3a 3a 3d 20 4c 50 20 63 6f 6c 75 6d 6e 6c 69 73  ::= LP columnlis
308eb 74 20 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 20 52  t conslist_opt R
308ec 50 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  P */.{.  sqlite3
308ed 45 6e 64 54 61 62 6c 65 28 70 50 61 72 73 65 2c  EndTable(pParse,
308ee 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
308ef 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  .yy0,&yymsp[0].m
308f0 69 6e 6f 72 2e 79 79 30 2c 30 29 3b 0a 7d 0a 20  inor.yy0,0);.}. 
308f1 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
308f2 20 20 20 20 63 61 73 65 20 33 33 3a 20 2f 2a 20      case 33: /* 
308f3 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72 67  create_table_arg
308f4 73 20 3a 3a 3d 20 41 53 20 73 65 6c 65 63 74 20  s ::= AS select 
308f5 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 45 6e  */.{.  sqlite3En
308f6 64 54 61 62 6c 65 28 70 50 61 72 73 65 2c 30 2c  dTable(pParse,0,
308f7 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  0,yymsp[0].minor
308f8 2e 79 79 33 29 3b 0a 20 20 73 71 6c 69 74 65 33  .yy3);.  sqlite3
308f9 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 50 61  SelectDelete(pPa
308fa 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 30  rse->db, yymsp[0
308fb 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29 3b 0a 7d 0a  ].minor.yy3);.}.
308fc 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
308fd 20 20 20 20 20 63 61 73 65 20 33 36 3a 20 2f 2a       case 36: /*
308fe 20 63 6f 6c 75 6d 6e 20 3a 3a 3d 20 63 6f 6c 75   column ::= colu
308ff 6d 6e 69 64 20 74 79 70 65 20 63 61 72 67 6c 69  mnid type cargli
30900 73 74 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  st */.{.  yygoto
30901 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20 79 79  minor.yy0.z = yy
30902 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
30903 30 2e 7a 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  0.z;.  yygotomin
30904 6f 72 2e 79 79 30 2e 6e 20 3d 20 28 69 6e 74 29  or.yy0.n = (int)
30905 28 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f  (pParse->sLastTo
30906 6b 65 6e 2e 7a 2d 79 79 6d 73 70 5b 2d 32 5d 2e  ken.z-yymsp[-2].
30907 6d 69 6e 6f 72 2e 79 79 30 2e 7a 29 20 2b 20 70  minor.yy0.z) + p
30908 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65  Parse->sLastToke
30909 6e 2e 6e 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  n.n;.}.        b
3090a 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
3090b 20 33 37 3a 20 2f 2a 20 63 6f 6c 75 6d 6e 69 64   37: /* columnid
3090c 20 3a 3a 3d 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73   ::= nm */.{.  s
3090d 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28  qlite3AddColumn(
3090e 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 30 5d  pParse,&yymsp[0]
3090f 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 79  .minor.yy0);.  y
30910 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d  ygotominor.yy0 =
30911 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
30912 79 79 30 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  yy0;.}.        b
30913 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
30914 20 33 38 3a 20 2f 2a 20 69 64 20 3a 3a 3d 20 49   38: /* id ::= I
30915 44 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  D */.      case 
30916 33 39 3a 20 2f 2a 20 69 64 20 3a 3a 3d 20 49 4e  39: /* id ::= IN
30917 44 45 58 45 44 20 2a 2f 20 79 79 74 65 73 74 63  DEXED */ yytestc
30918 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 39  ase(yyruleno==39
30919 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 34 30  );.      case 40
3091a 3a 20 2f 2a 20 69 64 73 20 3a 3a 3d 20 49 44 7c  : /* ids ::= ID|
3091b 53 54 52 49 4e 47 20 2a 2f 20 79 79 74 65 73 74  STRING */ yytest
3091c 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34  case(yyruleno==4
3091d 30 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 34  0);.      case 4
3091e 31 3a 20 2f 2a 20 6e 6d 20 3a 3a 3d 20 69 64 20  1: /* nm ::= id 
3091f 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
30920 72 75 6c 65 6e 6f 3d 3d 34 31 29 3b 0a 20 20 20  ruleno==41);.   
30921 20 20 20 63 61 73 65 20 34 32 3a 20 2f 2a 20 6e     case 42: /* n
30922 6d 20 3a 3a 3d 20 53 54 52 49 4e 47 20 2a 2f 20  m ::= STRING */ 
30923 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
30924 65 6e 6f 3d 3d 34 32 29 3b 0a 20 20 20 20 20 20  eno==42);.      
30925 63 61 73 65 20 34 33 3a 20 2f 2a 20 6e 6d 20 3a  case 43: /* nm :
30926 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 2a 2f 20 79 79  := JOIN_KW */ yy
30927 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
30928 6f 3d 3d 34 33 29 3b 0a 20 20 20 20 20 20 63 61  o==43);.      ca
30929 73 65 20 34 36 3a 20 2f 2a 20 74 79 70 65 74 6f  se 46: /* typeto
3092a 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65  ken ::= typename
3092b 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3092c 79 72 75 6c 65 6e 6f 3d 3d 34 36 29 3b 0a 20 20  yruleno==46);.  
3092d 20 20 20 20 63 61 73 65 20 34 39 3a 20 2f 2a 20      case 49: /* 
3092e 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20 69 64 73  typename ::= ids
3092f 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
30930 79 72 75 6c 65 6e 6f 3d 3d 34 39 29 3b 0a 20 20  yruleno==49);.  
30931 20 20 20 20 63 61 73 65 20 31 32 36 3a 20 2f 2a      case 126: /*
30932 20 61 73 20 3a 3a 3d 20 41 53 20 6e 6d 20 2a 2f   as ::= AS nm */
30933 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
30934 6c 65 6e 6f 3d 3d 31 32 36 29 3b 0a 20 20 20 20  leno==126);.    
30935 20 20 63 61 73 65 20 31 32 37 3a 20 2f 2a 20 61    case 127: /* a
30936 73 20 3a 3a 3d 20 69 64 73 20 2a 2f 20 79 79 74  s ::= ids */ yyt
30937 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
30938 3d 3d 31 32 37 29 3b 0a 20 20 20 20 20 20 63 61  ==127);.      ca
30939 73 65 20 31 33 37 3a 20 2f 2a 20 64 62 6e 6d 20  se 137: /* dbnm 
3093a 3a 3a 3d 20 44 4f 54 20 6e 6d 20 2a 2f 20 79 79  ::= DOT nm */ yy
3093b 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3093c 6f 3d 3d 31 33 37 29 3b 0a 20 20 20 20 20 20 63  o==137);.      c
3093d 61 73 65 20 31 34 36 3a 20 2f 2a 20 69 6e 64 65  ase 146: /* inde
3093e 78 65 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 44 45  xed_opt ::= INDE
3093f 58 45 44 20 42 59 20 6e 6d 20 2a 2f 20 79 79 74  XED BY nm */ yyt
30940 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
30941 3d 3d 31 34 36 29 3b 0a 20 20 20 20 20 20 63 61  ==146);.      ca
30942 73 65 20 32 35 31 3a 20 2f 2a 20 63 6f 6c 6c 61  se 251: /* colla
30943 74 65 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45 20 69  te ::= COLLATE i
30944 64 73 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  ds */ yytestcase
30945 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 35 31 29 3b  (yyruleno==251);
30946 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 30 3a  .      case 260:
30947 20 2f 2a 20 6e 6d 6e 75 6d 20 3a 3a 3d 20 70 6c   /* nmnum ::= pl
30948 75 73 5f 6e 75 6d 20 2a 2f 20 79 79 74 65 73 74  us_num */ yytest
30949 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
3094a 36 30 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  60);.      case 
3094b 32 36 31 3a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a 3a  261: /* nmnum ::
3094c 3d 20 6e 6d 20 2a 2f 20 79 79 74 65 73 74 63 61  = nm */ yytestca
3094d 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 36 31  se(yyruleno==261
3094e 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 36  );.      case 26
3094f 32 3a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a 3a 3d 20  2: /* nmnum ::= 
30950 4f 4e 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  ON */ yytestcase
30951 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 36 32 29 3b  (yyruleno==262);
30952 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 33 3a  .      case 263:
30953 20 2f 2a 20 6e 6d 6e 75 6d 20 3a 3a 3d 20 44 45   /* nmnum ::= DE
30954 4c 45 54 45 20 2a 2f 20 79 79 74 65 73 74 63 61  LETE */ yytestca
30955 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 36 33  se(yyruleno==263
30956 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 36  );.      case 26
30957 34 3a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a 3a 3d 20  4: /* nmnum ::= 
30958 44 45 46 41 55 4c 54 20 2a 2f 20 79 79 74 65 73  DEFAULT */ yytes
30959 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
3095a 32 36 34 29 3b 0a 20 20 20 20 20 20 63 61 73 65  264);.      case
3095b 20 32 36 35 3a 20 2f 2a 20 70 6c 75 73 5f 6e 75   265: /* plus_nu
3095c 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6f 70 74 20 6e  m ::= plus_opt n
3095d 75 6d 62 65 72 20 2a 2f 20 79 79 74 65 73 74 63  umber */ yytestc
3095e 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 36  ase(yyruleno==26
3095f 35 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  5);.      case 2
30960 36 36 3a 20 2f 2a 20 6d 69 6e 75 73 5f 6e 75 6d  66: /* minus_num
30961 20 3a 3a 3d 20 4d 49 4e 55 53 20 6e 75 6d 62 65   ::= MINUS numbe
30962 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  r */ yytestcase(
30963 79 79 72 75 6c 65 6e 6f 3d 3d 32 36 36 29 3b 0a  yyruleno==266);.
30964 20 20 20 20 20 20 63 61 73 65 20 32 36 37 3a 20        case 267: 
30965 2f 2a 20 6e 75 6d 62 65 72 20 3a 3a 3d 20 49 4e  /* number ::= IN
30966 54 45 47 45 52 7c 46 4c 4f 41 54 20 2a 2f 20 79  TEGER|FLOAT */ y
30967 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
30968 6e 6f 3d 3d 32 36 37 29 3b 0a 20 20 20 20 20 20  no==267);.      
30969 63 61 73 65 20 32 38 35 3a 20 2f 2a 20 74 72 6e  case 285: /* trn
3096a 6d 20 3a 3a 3d 20 6e 6d 20 2a 2f 20 79 79 74 65  m ::= nm */ yyte
3096b 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3096c 3d 32 38 35 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69  =285);.{yygotomi
3096d 6e 6f 72 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b  nor.yy0 = yymsp[
3096e 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 7d 0a 20  0].minor.yy0;}. 
3096f 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
30970 20 20 20 20 63 61 73 65 20 34 35 3a 20 2f 2a 20      case 45: /* 
30971 74 79 70 65 20 3a 3a 3d 20 74 79 70 65 74 6f 6b  type ::= typetok
30972 65 6e 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64  en */.{sqlite3Ad
30973 64 43 6f 6c 75 6d 6e 54 79 70 65 28 70 50 61 72  dColumnType(pPar
30974 73 65 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  se,&yymsp[0].min
30975 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
30976 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
30977 61 73 65 20 34 37 3a 20 2f 2a 20 74 79 70 65 74  ase 47: /* typet
30978 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d  oken ::= typenam
30979 65 20 4c 50 20 73 69 67 6e 65 64 20 52 50 20 2a  e LP signed RP *
3097a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
3097b 72 2e 79 79 30 2e 7a 20 3d 20 79 79 6d 73 70 5b  r.yy0.z = yymsp[
3097c 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b  -3].minor.yy0.z;
3097d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
3097e 79 30 2e 6e 20 3d 20 28 69 6e 74 29 28 26 79 79  y0.n = (int)(&yy
3097f 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
30980 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  .z[yymsp[0].mino
30981 72 2e 79 79 30 2e 6e 5d 20 2d 20 79 79 6d 73 70  r.yy0.n] - yymsp
30982 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  [-3].minor.yy0.z
30983 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
30984 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 34  ak;.      case 4
30985 38 3a 20 2f 2a 20 74 79 70 65 74 6f 6b 65 6e 20  8: /* typetoken 
30986 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 20 4c 50 20  ::= typename LP 
30987 73 69 67 6e 65 64 20 43 4f 4d 4d 41 20 73 69 67  signed COMMA sig
30988 6e 65 64 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79  ned RP */.{.  yy
30989 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20  gotominor.yy0.z 
3098a 3d 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f  = yymsp[-5].mino
3098b 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79 67 6f 74  r.yy0.z;.  yygot
3098c 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 28  ominor.yy0.n = (
3098d 69 6e 74 29 28 26 79 79 6d 73 70 5b 30 5d 2e 6d  int)(&yymsp[0].m
3098e 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70  inor.yy0.z[yymsp
3098f 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d  [0].minor.yy0.n]
30990 20 2d 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e   - yymsp[-5].min
30991 6f 72 2e 79 79 30 2e 7a 29 3b 0a 7d 0a 20 20 20  or.yy0.z);.}.   
30992 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30993 20 20 63 61 73 65 20 35 30 3a 20 2f 2a 20 74 79    case 50: /* ty
30994 70 65 6e 61 6d 65 20 3a 3a 3d 20 74 79 70 65 6e  pename ::= typen
30995 61 6d 65 20 69 64 73 20 2a 2f 0a 7b 79 79 67 6f  ame ids */.{yygo
30996 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3d 79 79  tominor.yy0.z=yy
30997 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
30998 30 2e 7a 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  0.z; yygotominor
30999 2e 79 79 30 2e 6e 3d 79 79 6d 73 70 5b 30 5d 2e  .yy0.n=yymsp[0].
3099a 6d 69 6e 6f 72 2e 79 79 30 2e 6e 2b 28 69 6e 74  minor.yy0.n+(int
3099b 29 28 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  )(yymsp[0].minor
3099c 2e 79 79 30 2e 7a 2d 79 79 6d 73 70 5b 2d 31 5d  .yy0.z-yymsp[-1]
3099d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 29 3b 7d 0a  .minor.yy0.z);}.
3099e 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3099f 20 20 20 20 20 63 61 73 65 20 35 37 3a 20 2f 2a       case 57: /*
309a0 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55   ccons ::= DEFAU
309a1 4c 54 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 20  LT term */.     
309a2 20 63 61 73 65 20 35 39 3a 20 2f 2a 20 63 63 6f   case 59: /* cco
309a3 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 50  ns ::= DEFAULT P
309a4 4c 55 53 20 74 65 72 6d 20 2a 2f 20 79 79 74 65  LUS term */ yyte
309a5 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
309a6 3d 35 39 29 3b 0a 7b 73 71 6c 69 74 65 33 41 64  =59);.{sqlite3Ad
309a7 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28 70 50  dDefaultValue(pP
309a8 61 72 73 65 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  arse,&yymsp[0].m
309a9 69 6e 6f 72 2e 79 79 33 34 36 29 3b 7d 0a 20 20  inor.yy346);}.  
309aa 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
309ab 20 20 20 63 61 73 65 20 35 38 3a 20 2f 2a 20 63     case 58: /* c
309ac 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54  cons ::= DEFAULT
309ad 20 4c 50 20 65 78 70 72 20 52 50 20 2a 2f 0a 7b   LP expr RP */.{
309ae 73 71 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c  sqlite3AddDefaul
309af 74 56 61 6c 75 65 28 70 50 61 72 73 65 2c 26 79  tValue(pParse,&y
309b0 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
309b1 79 33 34 36 29 3b 7d 0a 20 20 20 20 20 20 20 20  y346);}.        
309b2 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
309b3 65 20 36 30 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a  e 60: /* ccons :
309b4 3a 3d 20 44 45 46 41 55 4c 54 20 4d 49 4e 55 53  := DEFAULT MINUS
309b5 20 74 65 72 6d 20 2a 2f 0a 7b 0a 20 20 45 78 70   term */.{.  Exp
309b6 72 53 70 61 6e 20 76 3b 0a 20 20 76 2e 70 45 78  rSpan v;.  v.pEx
309b7 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  pr = sqlite3PExp
309b8 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 55 4d 49  r(pParse, TK_UMI
309b9 4e 55 53 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  NUS, yymsp[0].mi
309ba 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c  nor.yy346.pExpr,
309bb 20 30 2c 20 30 29 3b 0a 20 20 76 2e 7a 53 74 61   0, 0);.  v.zSta
309bc 72 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  rt = yymsp[-1].m
309bd 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 76 2e  inor.yy0.z;.  v.
309be 7a 45 6e 64 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  zEnd = yymsp[0].
309bf 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64  minor.yy346.zEnd
309c0 3b 0a 20 20 73 71 6c 69 74 65 33 41 64 64 44 65  ;.  sqlite3AddDe
309c1 66 61 75 6c 74 56 61 6c 75 65 28 70 50 61 72 73  faultValue(pPars
309c2 65 2c 26 76 29 3b 0a 7d 0a 20 20 20 20 20 20 20  e,&v);.}.       
309c3 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
309c4 73 65 20 36 31 3a 20 2f 2a 20 63 63 6f 6e 73 20  se 61: /* ccons 
309c5 3a 3a 3d 20 44 45 46 41 55 4c 54 20 69 64 20 2a  ::= DEFAULT id *
309c6 2f 0a 7b 0a 20 20 45 78 70 72 53 70 61 6e 20 76  /.{.  ExprSpan v
309c7 3b 0a 20 20 73 70 61 6e 45 78 70 72 28 26 76 2c  ;.  spanExpr(&v,
309c8 20 70 50 61 72 73 65 2c 20 54 4b 5f 53 54 52 49   pParse, TK_STRI
309c9 4e 47 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  NG, &yymsp[0].mi
309ca 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71 6c 69  nor.yy0);.  sqli
309cb 74 65 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c  te3AddDefaultVal
309cc 75 65 28 70 50 61 72 73 65 2c 26 76 29 3b 0a 7d  ue(pParse,&v);.}
309cd 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
309ce 20 20 20 20 20 20 63 61 73 65 20 36 33 3a 20 2f        case 63: /
309cf 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 4e 4f 54 20  * ccons ::= NOT 
309d0 4e 55 4c 4c 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b  NULL onconf */.{
309d1 73 71 6c 69 74 65 33 41 64 64 4e 6f 74 4e 75 6c  sqlite3AddNotNul
309d2 6c 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  l(pParse, yymsp[
309d3 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b  0].minor.yy328);
309d4 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
309d5 0a 20 20 20 20 20 20 63 61 73 65 20 36 34 3a 20  .      case 64: 
309d6 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 50 52 49  /* ccons ::= PRI
309d7 4d 41 52 59 20 4b 45 59 20 73 6f 72 74 6f 72 64  MARY KEY sortord
309d8 65 72 20 6f 6e 63 6f 6e 66 20 61 75 74 6f 69 6e  er onconf autoin
309d9 63 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64  c */.{sqlite3Add
309da 50 72 69 6d 61 72 79 4b 65 79 28 70 50 61 72 73  PrimaryKey(pPars
309db 65 2c 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  e,0,yymsp[-1].mi
309dc 6e 6f 72 2e 79 79 33 32 38 2c 79 79 6d 73 70 5b  nor.yy328,yymsp[
309dd 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 79  0].minor.yy328,y
309de 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
309df 79 33 32 38 29 3b 7d 0a 20 20 20 20 20 20 20 20  y328);}.        
309e0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
309e1 65 20 36 35 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a  e 65: /* ccons :
309e2 3a 3d 20 55 4e 49 51 55 45 20 6f 6e 63 6f 6e 66  := UNIQUE onconf
309e3 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 43 72 65 61   */.{sqlite3Crea
309e4 74 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c 30  teIndex(pParse,0
309e5 2c 30 2c 30 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e  ,0,0,0,yymsp[0].
309e6 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 30 2c 30 2c  minor.yy328,0,0,
309e7 30 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  0,0);}.        b
309e8 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
309e9 20 36 36 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a   66: /* ccons ::
309ea 3d 20 43 48 45 43 4b 20 4c 50 20 65 78 70 72 20  = CHECK LP expr 
309eb 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64  RP */.{sqlite3Ad
309ec 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e 74  dCheckConstraint
309ed 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 31  (pParse,yymsp[-1
309ee 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  ].minor.yy346.pE
309ef 78 70 72 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  xpr);}.        b
309f0 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
309f1 20 36 37 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a   67: /* ccons ::
309f2 3d 20 52 45 46 45 52 45 4e 43 45 53 20 6e 6d 20  = REFERENCES nm 
309f3 69 64 78 6c 69 73 74 5f 6f 70 74 20 72 65 66 61  idxlist_opt refa
309f4 72 67 73 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 43  rgs */.{sqlite3C
309f5 72 65 61 74 65 46 6f 72 65 69 67 6e 4b 65 79 28  reateForeignKey(
309f6 70 50 61 72 73 65 2c 30 2c 26 79 79 6d 73 70 5b  pParse,0,&yymsp[
309f7 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 79 79  -2].minor.yy0,yy
309f8 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
309f9 31 34 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  14,yymsp[0].mino
309fa 72 2e 79 79 33 32 38 29 3b 7d 0a 20 20 20 20 20  r.yy328);}.     
309fb 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
309fc 63 61 73 65 20 36 38 3a 20 2f 2a 20 63 63 6f 6e  case 68: /* ccon
309fd 73 20 3a 3a 3d 20 64 65 66 65 72 5f 73 75 62 63  s ::= defer_subc
309fe 6c 61 75 73 65 20 2a 2f 0a 7b 73 71 6c 69 74 65  lause */.{sqlite
309ff 33 44 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79  3DeferForeignKey
30a00 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 30 5d  (pParse,yymsp[0]
30a01 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b 7d 0a  .minor.yy328);}.
30a02 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
30a03 20 20 20 20 20 63 61 73 65 20 36 39 3a 20 2f 2a       case 69: /*
30a04 20 63 63 6f 6e 73 20 3a 3a 3d 20 43 4f 4c 4c 41   ccons ::= COLLA
30a05 54 45 20 69 64 73 20 2a 2f 0a 7b 73 71 6c 69 74  TE ids */.{sqlit
30a06 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79 70 65  e3AddCollateType
30a07 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b  (pParse, &yymsp[
30a08 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a  0].minor.yy0);}.
30a09 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
30a0a 20 20 20 20 20 63 61 73 65 20 37 32 3a 20 2f 2a       case 72: /*
30a0b 20 72 65 66 61 72 67 73 20 3a 3a 3d 20 2a 2f 0a   refargs ::= */.
30a0c 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
30a0d 33 32 38 20 3d 20 4f 45 5f 52 65 73 74 72 69 63  328 = OE_Restric
30a0e 74 20 2a 20 30 78 30 31 30 31 30 31 3b 20 7d 0a  t * 0x010101; }.
30a0f 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
30a10 20 20 20 20 20 63 61 73 65 20 37 33 3a 20 2f 2a       case 73: /*
30a11 20 72 65 66 61 72 67 73 20 3a 3a 3d 20 72 65 66   refargs ::= ref
30a12 61 72 67 73 20 72 65 66 61 72 67 20 2a 2f 0a 7b  args refarg */.{
30a13 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
30a14 32 38 20 3d 20 28 79 79 6d 73 70 5b 2d 31 5d 2e  28 = (yymsp[-1].
30a15 6d 69 6e 6f 72 2e 79 79 33 32 38 20 26 20 7e 79  minor.yy328 & ~y
30a16 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
30a17 34 32 39 2e 6d 61 73 6b 29 20 7c 20 79 79 6d 73  429.mask) | yyms
30a18 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 32 39  p[0].minor.yy429
30a19 2e 76 61 6c 75 65 3b 20 7d 0a 20 20 20 20 20 20  .value; }.      
30a1a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
30a1b 61 73 65 20 37 34 3a 20 2f 2a 20 72 65 66 61 72  ase 74: /* refar
30a1c 67 20 3a 3a 3d 20 4d 41 54 43 48 20 6e 6d 20 2a  g ::= MATCH nm *
30a1d 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  /.{ yygotominor.
30a1e 79 79 34 32 39 2e 76 61 6c 75 65 20 3d 20 30 3b  yy429.value = 0;
30a1f 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72       yygotominor
30a20 2e 79 79 34 32 39 2e 6d 61 73 6b 20 3d 20 30 78  .yy429.mask = 0x
30a21 30 30 30 30 30 30 3b 20 7d 0a 20 20 20 20 20 20  000000; }.      
30a22 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
30a23 61 73 65 20 37 35 3a 20 2f 2a 20 72 65 66 61 72  ase 75: /* refar
30a24 67 20 3a 3a 3d 20 4f 4e 20 44 45 4c 45 54 45 20  g ::= ON DELETE 
30a25 72 65 66 61 63 74 20 2a 2f 0a 7b 20 79 79 67 6f  refact */.{ yygo
30a26 74 6f 6d 69 6e 6f 72 2e 79 79 34 32 39 2e 76 61  tominor.yy429.va
30a27 6c 75 65 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  lue = yymsp[0].m
30a28 69 6e 6f 72 2e 79 79 33 32 38 3b 20 20 20 20 20  inor.yy328;     
30a29 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 32  yygotominor.yy42
30a2a 39 2e 6d 61 73 6b 20 3d 20 30 78 30 30 30 30 66  9.mask = 0x0000f
30a2b 66 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  f; }.        bre
30a2c 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37  ak;.      case 7
30a2d 36 3a 20 2f 2a 20 72 65 66 61 72 67 20 3a 3a 3d  6: /* refarg ::=
30a2e 20 4f 4e 20 55 50 44 41 54 45 20 72 65 66 61 63   ON UPDATE refac
30a2f 74 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  t */.{ yygotomin
30a30 6f 72 2e 79 79 34 32 39 2e 76 61 6c 75 65 20 3d  or.yy429.value =
30a31 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
30a32 79 79 33 32 38 3c 3c 38 3b 20 20 79 79 67 6f 74  yy328<<8;  yygot
30a33 6f 6d 69 6e 6f 72 2e 79 79 34 32 39 2e 6d 61 73  ominor.yy429.mas
30a34 6b 20 3d 20 30 78 30 30 66 66 30 30 3b 20 7d 0a  k = 0x00ff00; }.
30a35 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
30a36 20 20 20 20 20 63 61 73 65 20 37 37 3a 20 2f 2a       case 77: /*
30a37 20 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 49   refarg ::= ON I
30a38 4e 53 45 52 54 20 72 65 66 61 63 74 20 2a 2f 0a  NSERT refact */.
30a39 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
30a3a 34 32 39 2e 76 61 6c 75 65 20 3d 20 79 79 6d 73  429.value = yyms
30a3b 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  p[0].minor.yy328
30a3c 3c 3c 31 36 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f  <<16; yygotomino
30a3d 72 2e 79 79 34 32 39 2e 6d 61 73 6b 20 3d 20 30  r.yy429.mask = 0
30a3e 78 66 66 30 30 30 30 3b 20 7d 0a 20 20 20 20 20  xff0000; }.     
30a3f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
30a40 63 61 73 65 20 37 38 3a 20 2f 2a 20 72 65 66 61  case 78: /* refa
30a41 63 74 20 3a 3a 3d 20 53 45 54 20 4e 55 4c 4c 20  ct ::= SET NULL 
30a42 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  */.{ yygotominor
30a43 2e 79 79 33 32 38 20 3d 20 4f 45 5f 53 65 74 4e  .yy328 = OE_SetN
30a44 75 6c 6c 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  ull; }.        b
30a45 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
30a46 20 37 39 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a   79: /* refact :
30a47 3a 3d 20 53 45 54 20 44 45 46 41 55 4c 54 20 2a  := SET DEFAULT *
30a48 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  /.{ yygotominor.
30a49 79 79 33 32 38 20 3d 20 4f 45 5f 53 65 74 44 66  yy328 = OE_SetDf
30a4a 6c 74 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  lt; }.        br
30a4b 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
30a4c 38 30 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a 3a  80: /* refact ::
30a4d 3d 20 43 41 53 43 41 44 45 20 2a 2f 0a 7b 20 79  = CASCADE */.{ y
30a4e 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38  ygotominor.yy328
30a4f 20 3d 20 4f 45 5f 43 61 73 63 61 64 65 3b 20 7d   = OE_Cascade; }
30a50 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
30a51 20 20 20 20 20 20 63 61 73 65 20 38 31 3a 20 2f        case 81: /
30a52 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20 52 45 53  * refact ::= RES
30a53 54 52 49 43 54 20 2a 2f 0a 7b 20 79 79 67 6f 74  TRICT */.{ yygot
30a54 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f  ominor.yy328 = O
30a55 45 5f 52 65 73 74 72 69 63 74 3b 20 7d 0a 20 20  E_Restrict; }.  
30a56 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
30a57 20 20 20 63 61 73 65 20 38 32 3a 20 2f 2a 20 64     case 82: /* d
30a58 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 3a  efer_subclause :
30a59 3a 3d 20 4e 4f 54 20 44 45 46 45 52 52 41 42 4c  := NOT DEFERRABL
30a5a 45 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f  E init_deferred_
30a5b 70 72 65 64 5f 6f 70 74 20 2a 2f 0a 20 20 20 20  pred_opt */.    
30a5c 20 20 63 61 73 65 20 38 33 3a 20 2f 2a 20 64 65    case 83: /* de
30a5d 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 3a 3a  fer_subclause ::
30a5e 3d 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e 69  = DEFERRABLE ini
30a5f 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f  t_deferred_pred_
30a60 6f 70 74 20 2a 2f 20 79 79 74 65 73 74 63 61 73  opt */ yytestcas
30a61 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 38 33 29 3b  e(yyruleno==83);
30a62 0a 20 20 20 20 20 20 63 61 73 65 20 39 38 3a 20  .      case 98: 
30a63 2f 2a 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75  /* defer_subclau
30a64 73 65 5f 6f 70 74 20 3a 3a 3d 20 64 65 66 65 72  se_opt ::= defer
30a65 5f 73 75 62 63 6c 61 75 73 65 20 2a 2f 20 79 79  _subclause */ yy
30a66 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
30a67 6f 3d 3d 39 38 29 3b 0a 20 20 20 20 20 20 63 61  o==98);.      ca
30a68 73 65 20 31 30 30 3a 20 2f 2a 20 6f 6e 63 6f 6e  se 100: /* oncon
30a69 66 20 3a 3a 3d 20 4f 4e 20 43 4f 4e 46 4c 49 43  f ::= ON CONFLIC
30a6a 54 20 72 65 73 6f 6c 76 65 74 79 70 65 20 2a 2f  T resolvetype */
30a6b 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
30a6c 6c 65 6e 6f 3d 3d 31 30 30 29 3b 0a 20 20 20 20  leno==100);.    
30a6d 20 20 63 61 73 65 20 31 30 33 3a 20 2f 2a 20 72    case 103: /* r
30a6e 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d 20 72  esolvetype ::= r
30a6f 61 69 73 65 74 79 70 65 20 2a 2f 20 79 79 74 65  aisetype */ yyte
30a70 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
30a71 3d 31 30 33 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69  =103);.{yygotomi
30a72 6e 6f 72 2e 79 79 33 32 38 20 3d 20 79 79 6d 73  nor.yy328 = yyms
30a73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  p[0].minor.yy328
30a74 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
30a75 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38 37 3a  ;.      case 87:
30a76 20 2f 2a 20 63 6f 6e 73 6c 69 73 74 5f 6f 70 74   /* conslist_opt
30a77 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d   ::= */.{yygotom
30a78 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 30 3b 20  inor.yy0.n = 0; 
30a79 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e  yygotominor.yy0.
30a7a 7a 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20  z = 0;}.        
30a7b 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
30a7c 65 20 38 38 3a 20 2f 2a 20 63 6f 6e 73 6c 69 73  e 88: /* conslis
30a7d 74 5f 6f 70 74 20 3a 3a 3d 20 43 4f 4d 4d 41 20  t_opt ::= COMMA 
30a7e 63 6f 6e 73 6c 69 73 74 20 2a 2f 0a 7b 79 79 67  conslist */.{yyg
30a7f 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d 20 79  otominor.yy0 = y
30a80 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
30a81 79 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  y0;}.        bre
30a82 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39  ak;.      case 9
30a83 33 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20  3: /* tcons ::= 
30a84 50 52 49 4d 41 52 59 20 4b 45 59 20 4c 50 20 69  PRIMARY KEY LP i
30a85 64 78 6c 69 73 74 20 61 75 74 6f 69 6e 63 20 52  dxlist autoinc R
30a86 50 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c  P onconf */.{sql
30a87 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65  ite3AddPrimaryKe
30a88 79 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  y(pParse,yymsp[-
30a89 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 79 79  3].minor.yy14,yy
30a8a 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
30a8b 32 38 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  28,yymsp[-2].min
30a8c 6f 72 2e 79 79 33 32 38 2c 30 29 3b 7d 0a 20 20  or.yy328,0);}.  
30a8d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
30a8e 20 20 20 63 61 73 65 20 39 34 3a 20 2f 2a 20 74     case 94: /* t
30a8f 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55 45 20  cons ::= UNIQUE 
30a90 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20 6f 6e  LP idxlist RP on
30a91 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  conf */.{sqlite3
30a92 43 72 65 61 74 65 49 6e 64 65 78 28 70 50 61 72  CreateIndex(pPar
30a93 73 65 2c 30 2c 30 2c 30 2c 79 79 6d 73 70 5b 2d  se,0,0,0,yymsp[-
30a94 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 79 79  2].minor.yy14,yy
30a95 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
30a96 32 38 2c 30 2c 30 2c 30 2c 30 29 3b 7d 0a 20 20  28,0,0,0,0);}.  
30a97 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
30a98 20 20 20 63 61 73 65 20 39 35 3a 20 2f 2a 20 74     case 95: /* t
30a99 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43 4b 20 4c  cons ::= CHECK L
30a9a 50 20 65 78 70 72 20 52 50 20 6f 6e 63 6f 6e 66  P expr RP onconf
30a9b 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 43   */.{sqlite3AddC
30a9c 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e 74 28 70  heckConstraint(p
30a9d 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 32 5d 2e  Parse,yymsp[-2].
30a9e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
30a9f 72 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  r);}.        bre
30aa0 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39  ak;.      case 9
30aa1 36 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20  6: /* tcons ::= 
30aa2 46 4f 52 45 49 47 4e 20 4b 45 59 20 4c 50 20 69  FOREIGN KEY LP i
30aa3 64 78 6c 69 73 74 20 52 50 20 52 45 46 45 52 45  dxlist RP REFERE
30aa4 4e 43 45 53 20 6e 6d 20 69 64 78 6c 69 73 74 5f  NCES nm idxlist_
30aa5 6f 70 74 20 72 65 66 61 72 67 73 20 64 65 66 65  opt refargs defe
30aa6 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 20  r_subclause_opt 
30aa7 2a 2f 0a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  */.{.    sqlite3
30aa8 43 72 65 61 74 65 46 6f 72 65 69 67 6e 4b 65 79  CreateForeignKey
30aa9 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
30aaa 36 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 26  6].minor.yy14, &
30aab 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
30aac 79 79 30 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d  yy0, yymsp[-2].m
30aad 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d 73 70  inor.yy14, yymsp
30aae 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  [-1].minor.yy328
30aaf 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  );.    sqlite3De
30ab0 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 70 50  ferForeignKey(pP
30ab1 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  arse, yymsp[0].m
30ab2 69 6e 6f 72 2e 79 79 33 32 38 29 3b 0a 7d 0a 20  inor.yy328);.}. 
30ab3 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
30ab4 20 20 20 20 63 61 73 65 20 39 39 3a 20 2f 2a 20      case 99: /* 
30ab5 6f 6e 63 6f 6e 66 20 3a 3a 3d 20 2a 2f 0a 7b 79  onconf ::= */.{y
30ab6 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38  ygotominor.yy328
30ab7 20 3d 20 4f 45 5f 44 65 66 61 75 6c 74 3b 7d 0a   = OE_Default;}.
30ab8 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
30ab9 20 20 20 20 20 63 61 73 65 20 31 30 31 3a 20 2f       case 101: /
30aba 2a 20 6f 72 63 6f 6e 66 20 3a 3a 3d 20 2a 2f 0a  * orconf ::= */.
30abb 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  {yygotominor.yy1
30abc 38 36 20 3d 20 4f 45 5f 44 65 66 61 75 6c 74 3b  86 = OE_Default;
30abd 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
30abe 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 32 3a  .      case 102:
30abf 20 2f 2a 20 6f 72 63 6f 6e 66 20 3a 3a 3d 20 4f   /* orconf ::= O
30ac0 52 20 72 65 73 6f 6c 76 65 74 79 70 65 20 2a 2f  R resolvetype */
30ac1 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
30ac2 31 38 36 20 3d 20 28 75 38 29 79 79 6d 73 70 5b  186 = (u8)yymsp[
30ac3 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 7d  0].minor.yy328;}
30ac4 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
30ac5 20 20 20 20 20 20 63 61 73 65 20 31 30 34 3a 20        case 104: 
30ac6 2f 2a 20 72 65 73 6f 6c 76 65 74 79 70 65 20 3a  /* resolvetype :
30ac7 3a 3d 20 49 47 4e 4f 52 45 20 2a 2f 0a 7b 79 79  := IGNORE */.{yy
30ac8 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20  gotominor.yy328 
30ac9 3d 20 4f 45 5f 49 67 6e 6f 72 65 3b 7d 0a 20 20  = OE_Ignore;}.  
30aca 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
30acb 20 20 20 63 61 73 65 20 31 30 35 3a 20 2f 2a 20     case 105: /* 
30acc 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d 20  resolvetype ::= 
30acd 52 45 50 4c 41 43 45 20 2a 2f 0a 7b 79 79 67 6f  REPLACE */.{yygo
30ace 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20  tominor.yy328 = 
30acf 4f 45 5f 52 65 70 6c 61 63 65 3b 7d 0a 20 20 20  OE_Replace;}.   
30ad0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30ad1 20 20 63 61 73 65 20 31 30 36 3a 20 2f 2a 20 63    case 106: /* c
30ad2 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54 41 42 4c  md ::= DROP TABL
30ad3 45 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e  E ifexists fulln
30ad4 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74  ame */.{.  sqlit
30ad5 65 33 44 72 6f 70 54 61 62 6c 65 28 70 50 61 72  e3DropTable(pPar
30ad6 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  se, yymsp[0].min
30ad7 6f 72 2e 79 79 36 35 2c 20 30 2c 20 79 79 6d 73  or.yy65, 0, yyms
30ad8 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  p[-1].minor.yy32
30ad9 38 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  8);.}.        br
30ada 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
30adb 31 30 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  109: /* cmd ::= 
30adc 63 72 65 61 74 65 6b 77 20 74 65 6d 70 20 56 49  createkw temp VI
30add 45 57 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e  EW ifnotexists n
30ade 6d 20 64 62 6e 6d 20 41 53 20 73 65 6c 65 63 74  m dbnm AS select
30adf 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 43   */.{.  sqlite3C
30ae0 72 65 61 74 65 56 69 65 77 28 70 50 61 72 73 65  reateView(pParse
30ae1 2c 20 26 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e  , &yymsp[-7].min
30ae2 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 2d  or.yy0, &yymsp[-
30ae3 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79  3].minor.yy0, &y
30ae4 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
30ae5 79 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0, yymsp[0].min
30ae6 6f 72 2e 79 79 33 2c 20 79 79 6d 73 70 5b 2d 36  or.yy3, yymsp[-6
30ae7 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 20 79  ].minor.yy328, y
30ae8 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
30ae9 79 33 32 38 29 3b 0a 7d 0a 20 20 20 20 20 20 20  y328);.}.       
30aea 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
30aeb 73 65 20 31 31 30 3a 20 2f 2a 20 63 6d 64 20 3a  se 110: /* cmd :
30aec 3a 3d 20 44 52 4f 50 20 56 49 45 57 20 69 66 65  := DROP VIEW ife
30aed 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 20 2a  xists fullname *
30aee 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 44 72 6f  /.{.  sqlite3Dro
30aef 70 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 79  pTable(pParse, y
30af0 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
30af1 36 35 2c 20 31 2c 20 79 79 6d 73 70 5b 2d 31 5d  65, 1, yymsp[-1]
30af2 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b 0a 7d  .minor.yy328);.}
30af3 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
30af4 20 20 20 20 20 20 63 61 73 65 20 31 31 31 3a 20        case 111: 
30af5 2f 2a 20 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63  /* cmd ::= selec
30af6 74 20 2a 2f 0a 7b 0a 20 20 53 65 6c 65 63 74 44  t */.{.  SelectD
30af7 65 73 74 20 64 65 73 74 20 3d 20 7b 53 52 54 5f  est dest = {SRT_
30af8 4f 75 74 70 75 74 2c 20 30 2c 20 30 2c 20 30 2c  Output, 0, 0, 0,
30af9 20 30 7d 3b 0a 20 20 73 71 6c 69 74 65 33 53 65   0};.  sqlite3Se
30afa 6c 65 63 74 28 70 50 61 72 73 65 2c 20 79 79 6d  lect(pParse, yym
30afb 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 2c  sp[0].minor.yy3,
30afc 20 26 64 65 73 74 29 3b 0a 20 20 73 71 6c 69 74   &dest);.  sqlit
30afd 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70  e3SelectDelete(p
30afe 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70  Parse->db, yymsp
30aff 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29 3b 0a  [0].minor.yy3);.
30b00 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
30b01 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 32 3a  .      case 112:
30b02 20 2f 2a 20 73 65 6c 65 63 74 20 3a 3a 3d 20 6f   /* select ::= o
30b03 6e 65 73 65 6c 65 63 74 20 2a 2f 0a 7b 79 79 67  neselect */.{yyg
30b04 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 20 3d 20 79  otominor.yy3 = y
30b05 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
30b06 33 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  3;}.        brea
30b07 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 31  k;.      case 11
30b08 33 3a 20 2f 2a 20 73 65 6c 65 63 74 20 3a 3a 3d  3: /* select ::=
30b09 20 73 65 6c 65 63 74 20 6d 75 6c 74 69 73 65 6c   select multisel
30b0a 65 63 74 5f 6f 70 20 6f 6e 65 73 65 6c 65 63 74  ect_op oneselect
30b0b 20 2a 2f 0a 7b 0a 20 20 69 66 28 20 79 79 6d 73   */.{.  if( yyms
30b0c 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 20 29  p[0].minor.yy3 )
30b0d 7b 0a 20 20 20 20 79 79 6d 73 70 5b 30 5d 2e 6d  {.    yymsp[0].m
30b0e 69 6e 6f 72 2e 79 79 33 2d 3e 6f 70 20 3d 20 28  inor.yy3->op = (
30b0f 75 38 29 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  u8)yymsp[-1].min
30b10 6f 72 2e 79 79 33 32 38 3b 0a 20 20 20 20 79 79  or.yy328;.    yy
30b11 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
30b12 2d 3e 70 50 72 69 6f 72 20 3d 20 79 79 6d 73 70  ->pPrior = yymsp
30b13 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 3b 0a  [-2].minor.yy3;.
30b14 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
30b15 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
30b16 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d  (pParse->db, yym
30b17 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-2].minor.yy3
30b18 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d  );.  }.  yygotom
30b19 69 6e 6f 72 2e 79 79 33 20 3d 20 79 79 6d 73 70  inor.yy3 = yymsp
30b1a 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 3b 0a 7d  [0].minor.yy3;.}
30b1b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
30b1c 20 20 20 20 20 20 63 61 73 65 20 31 31 35 3a 20        case 115: 
30b1d 2f 2a 20 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f  /* multiselect_o
30b1e 70 20 3a 3a 3d 20 55 4e 49 4f 4e 20 41 4c 4c 20  p ::= UNION ALL 
30b1f 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
30b20 79 79 33 32 38 20 3d 20 54 4b 5f 41 4c 4c 3b 7d  yy328 = TK_ALL;}
30b21 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
30b22 20 20 20 20 20 20 63 61 73 65 20 31 31 37 3a 20        case 117: 
30b23 2f 2a 20 6f 6e 65 73 65 6c 65 63 74 20 3a 3a 3d  /* oneselect ::=
30b24 20 53 45 4c 45 43 54 20 64 69 73 74 69 6e 63 74   SELECT distinct
30b25 20 73 65 6c 63 6f 6c 6c 69 73 74 20 66 72 6f 6d   selcollist from
30b26 20 77 68 65 72 65 5f 6f 70 74 20 67 72 6f 75 70   where_opt group
30b27 62 79 5f 6f 70 74 20 68 61 76 69 6e 67 5f 6f 70  by_opt having_op
30b28 74 20 6f 72 64 65 72 62 79 5f 6f 70 74 20 6c 69  t orderby_opt li
30b29 6d 69 74 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 79  mit_opt */.{.  y
30b2a 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 20 3d  ygotominor.yy3 =
30b2b 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65   sqlite3SelectNe
30b2c 77 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  w(pParse,yymsp[-
30b2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 79 79  6].minor.yy14,yy
30b2e 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-5].minor.yy
30b2f 36 35 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  65,yymsp[-4].min
30b30 6f 72 2e 79 79 31 33 32 2c 79 79 6d 73 70 5b 2d  or.yy132,yymsp[-
30b31 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 79 79  3].minor.yy14,yy
30b32 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
30b33 31 33 32 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  132,yymsp[-1].mi
30b34 6e 6f 72 2e 79 79 31 34 2c 79 79 6d 73 70 5b 2d  nor.yy14,yymsp[-
30b35 37 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 79  7].minor.yy328,y
30b36 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
30b37 34 37 36 2e 70 4c 69 6d 69 74 2c 79 79 6d 73 70  476.pLimit,yymsp
30b38 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 36 2e  [0].minor.yy476.
30b39 70 4f 66 66 73 65 74 29 3b 0a 7d 0a 20 20 20 20  pOffset);.}.    
30b3a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
30b3b 20 63 61 73 65 20 31 32 31 3a 20 2f 2a 20 73 63   case 121: /* sc
30b3c 6c 70 20 3a 3a 3d 20 73 65 6c 63 6f 6c 6c 69 73  lp ::= selcollis
30b3d 74 20 43 4f 4d 4d 41 20 2a 2f 0a 20 20 20 20 20  t COMMA */.     
30b3e 20 63 61 73 65 20 32 34 37 3a 20 2f 2a 20 69 64   case 247: /* id
30b3f 78 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 4c 50  xlist_opt ::= LP
30b40 20 69 64 78 6c 69 73 74 20 52 50 20 2a 2f 20 79   idxlist RP */ y
30b41 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
30b42 6e 6f 3d 3d 32 34 37 29 3b 0a 7b 79 79 67 6f 74  no==247);.{yygot
30b43 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 79 79  ominor.yy14 = yy
30b44 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
30b45 31 34 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  14;}.        bre
30b46 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
30b47 32 32 3a 20 2f 2a 20 73 63 6c 70 20 3a 3a 3d 20  22: /* sclp ::= 
30b48 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 35  */.      case 15
30b49 30 3a 20 2f 2a 20 6f 72 64 65 72 62 79 5f 6f 70  0: /* orderby_op
30b4a 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63  t ::= */ yytestc
30b4b 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 35  ase(yyruleno==15
30b4c 30 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  0);.      case 1
30b4d 35 38 3a 20 2f 2a 20 67 72 6f 75 70 62 79 5f 6f  58: /* groupby_o
30b4e 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  pt ::= */ yytest
30b4f 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
30b50 35 38 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  58);.      case 
30b51 32 34 30 3a 20 2f 2a 20 65 78 70 72 6c 69 73 74  240: /* exprlist
30b52 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61   ::= */ yytestca
30b53 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 34 30  se(yyruleno==240
30b54 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 34  );.      case 24
30b55 36 3a 20 2f 2a 20 69 64 78 6c 69 73 74 5f 6f 70  6: /* idxlist_op
30b56 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63  t ::= */ yytestc
30b57 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 34  ase(yyruleno==24
30b58 36 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72  6);.{yygotominor
30b59 2e 79 79 31 34 20 3d 20 30 3b 7d 0a 20 20 20 20  .yy14 = 0;}.    
30b5a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
30b5b 20 63 61 73 65 20 31 32 33 3a 20 2f 2a 20 73 65   case 123: /* se
30b5c 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c  lcollist ::= scl
30b5d 70 20 65 78 70 72 20 61 73 20 2a 2f 0a 7b 0a 20  p expr as */.{. 
30b5e 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
30b5f 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  14 = sqlite3Expr
30b60 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
30b61 65 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  e, yymsp[-2].min
30b62 6f 72 2e 79 79 31 34 2c 20 79 79 6d 73 70 5b 2d  or.yy14, yymsp[-
30b63 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  1].minor.yy346.p
30b64 45 78 70 72 29 3b 0a 20 20 20 69 66 28 20 79 79  Expr);.   if( yy
30b65 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
30b66 2e 6e 3e 30 20 29 20 73 71 6c 69 74 65 33 45 78  .n>0 ) sqlite3Ex
30b67 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 70 50  prListSetName(pP
30b68 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f  arse, yygotomino
30b69 72 2e 79 79 31 34 2c 20 26 79 79 6d 73 70 5b 30  r.yy14, &yymsp[0
30b6a 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 31 29 3b  ].minor.yy0, 1);
30b6b 0a 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  .   sqlite3ExprL
30b6c 69 73 74 53 65 74 53 70 61 6e 28 70 50 61 72 73  istSetSpan(pPars
30b6d 65 2c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  e,yygotominor.yy
30b6e 31 34 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  14,&yymsp[-1].mi
30b6f 6e 6f 72 2e 79 79 33 34 36 29 3b 0a 7d 0a 20 20  nor.yy346);.}.  
30b70 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
30b71 20 20 20 63 61 73 65 20 31 32 34 3a 20 2f 2a 20     case 124: /* 
30b72 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73  selcollist ::= s
30b73 63 6c 70 20 53 54 41 52 20 2a 2f 0a 7b 0a 20 20  clp STAR */.{.  
30b74 45 78 70 72 20 2a 70 20 3d 20 73 71 6c 69 74 65  Expr *p = sqlite
30b75 33 45 78 70 72 28 70 50 61 72 73 65 2d 3e 64 62  3Expr(pParse->db
30b76 2c 20 54 4b 5f 41 4c 4c 2c 20 30 29 3b 0a 20 20  , TK_ALL, 0);.  
30b77 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34  yygotominor.yy14
30b78 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
30b79 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
30b7a 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
30b7b 2e 79 79 31 34 2c 20 70 29 3b 0a 7d 0a 20 20 20  .yy14, p);.}.   
30b7c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30b7d 20 20 63 61 73 65 20 31 32 35 3a 20 2f 2a 20 73    case 125: /* s
30b7e 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63  elcollist ::= sc
30b7f 6c 70 20 6e 6d 20 44 4f 54 20 53 54 41 52 20 2a  lp nm DOT STAR *
30b80 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 70 52 69 67  /.{.  Expr *pRig
30b81 68 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  ht = sqlite3PExp
30b82 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 41 4c 4c  r(pParse, TK_ALL
30b83 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 30  , 0, 0, &yymsp[0
30b84 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20  ].minor.yy0);.  
30b85 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 73 71  Expr *pLeft = sq
30b86 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
30b87 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20  e, TK_ID, 0, 0, 
30b88 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
30b89 2e 79 79 30 29 3b 0a 20 20 45 78 70 72 20 2a 70  .yy0);.  Expr *p
30b8a 44 6f 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78  Dot = sqlite3PEx
30b8b 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f  pr(pParse, TK_DO
30b8c 54 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74  T, pLeft, pRight
30b8d 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  , 0);.  yygotomi
30b8e 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74  nor.yy14 = sqlit
30b8f 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
30b90 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 33  (pParse,yymsp[-3
30b91 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 70 44  ].minor.yy14, pD
30b92 6f 74 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  ot);.}.        b
30b93 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
30b94 20 31 32 38 3a 20 2f 2a 20 61 73 20 3a 3a 3d 20   128: /* as ::= 
30b95 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
30b96 79 79 30 2e 6e 20 3d 20 30 3b 7d 0a 20 20 20 20  yy0.n = 0;}.    
30b97 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
30b98 20 63 61 73 65 20 31 32 39 3a 20 2f 2a 20 66 72   case 129: /* fr
30b99 6f 6d 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74  om ::= */.{yygot
30b9a 6f 6d 69 6e 6f 72 2e 79 79 36 35 20 3d 20 73 71  ominor.yy65 = sq
30b9b 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
30b9c 6f 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 73 69  o(pParse->db, si
30b9d 7a 65 6f 66 28 2a 79 79 67 6f 74 6f 6d 69 6e 6f  zeof(*yygotomino
30b9e 72 2e 79 79 36 35 29 29 3b 7d 0a 20 20 20 20 20  r.yy65));}.     
30b9f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
30ba0 63 61 73 65 20 31 33 30 3a 20 2f 2a 20 66 72 6f  case 130: /* fro
30ba1 6d 20 3a 3a 3d 20 46 52 4f 4d 20 73 65 6c 74 61  m ::= FROM selta
30ba2 62 6c 69 73 74 20 2a 2f 0a 7b 0a 20 20 79 79 67  blist */.{.  yyg
30ba3 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20 3d 20  otominor.yy65 = 
30ba4 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
30ba5 79 36 35 3b 0a 20 20 73 71 6c 69 74 65 33 53 72  y65;.  sqlite3Sr
30ba6 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79  cListShiftJoinTy
30ba7 70 65 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  pe(yygotominor.y
30ba8 79 36 35 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  y65);.}.        
30ba9 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
30baa 65 20 31 33 31 3a 20 2f 2a 20 73 74 6c 5f 70 72  e 131: /* stl_pr
30bab 65 66 69 78 20 3a 3a 3d 20 73 65 6c 74 61 62 6c  efix ::= seltabl
30bac 69 73 74 20 6a 6f 69 6e 6f 70 20 2a 2f 0a 7b 0a  ist joinop */.{.
30bad 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
30bae 79 36 35 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e  y65 = yymsp[-1].
30baf 6d 69 6e 6f 72 2e 79 79 36 35 3b 0a 20 20 20 69  minor.yy65;.   i
30bb0 66 28 20 41 4c 57 41 59 53 28 79 79 67 6f 74 6f  f( ALWAYS(yygoto
30bb1 6d 69 6e 6f 72 2e 79 79 36 35 20 26 26 20 79 79  minor.yy65 && yy
30bb2 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 2d 3e  gotominor.yy65->
30bb3 6e 53 72 63 3e 30 29 20 29 20 79 79 67 6f 74 6f  nSrc>0) ) yygoto
30bb4 6d 69 6e 6f 72 2e 79 79 36 35 2d 3e 61 5b 79 79  minor.yy65->a[yy
30bb5 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 2d 3e  gotominor.yy65->
30bb6 6e 53 72 63 2d 31 5d 2e 6a 6f 69 6e 74 79 70 65  nSrc-1].jointype
30bb7 20 3d 20 28 75 38 29 79 79 6d 73 70 5b 30 5d 2e   = (u8)yymsp[0].
30bb8 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 0a 7d 0a 20  minor.yy328;.}. 
30bb9 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
30bba 20 20 20 20 63 61 73 65 20 31 33 32 3a 20 2f 2a      case 132: /*
30bbb 20 73 74 6c 5f 70 72 65 66 69 78 20 3a 3a 3d 20   stl_prefix ::= 
30bbc 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
30bbd 79 79 36 35 20 3d 20 30 3b 7d 0a 20 20 20 20 20  yy65 = 0;}.     
30bbe 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
30bbf 63 61 73 65 20 31 33 33 3a 20 2f 2a 20 73 65 6c  case 133: /* sel
30bc0 74 61 62 6c 69 73 74 20 3a 3a 3d 20 73 74 6c 5f  tablist ::= stl_
30bc1 70 72 65 66 69 78 20 6e 6d 20 64 62 6e 6d 20 61  prefix nm dbnm a
30bc2 73 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 6f 6e  s indexed_opt on
30bc3 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 20 2a  _opt using_opt *
30bc4 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
30bc5 72 2e 79 79 36 35 20 3d 20 73 71 6c 69 74 65 33  r.yy65 = sqlite3
30bc6 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f  SrcListAppendFro
30bc7 6d 54 65 72 6d 28 70 50 61 72 73 65 2c 79 79 6d  mTerm(pParse,yym
30bc8 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 36  sp[-6].minor.yy6
30bc9 35 2c 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e  5,&yymsp[-5].min
30bca 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 34  or.yy0,&yymsp[-4
30bcb 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d  ].minor.yy0,&yym
30bcc 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
30bcd 2c 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  ,0,yymsp[-1].min
30bce 6f 72 2e 79 79 31 33 32 2c 79 79 6d 73 70 5b 30  or.yy132,yymsp[0
30bcf 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 29 3b 0a  ].minor.yy408);.
30bd0 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
30bd1 49 6e 64 65 78 65 64 42 79 28 70 50 61 72 73 65  IndexedBy(pParse
30bd2 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  , yygotominor.yy
30bd3 36 35 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d  65, &yymsp[-2].m
30bd4 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20  inor.yy0);.}.   
30bd5 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30bd6 20 20 63 61 73 65 20 31 33 34 3a 20 2f 2a 20 73    case 134: /* s
30bd7 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20 73 74  eltablist ::= st
30bd8 6c 5f 70 72 65 66 69 78 20 4c 50 20 73 65 6c 65  l_prefix LP sele
30bd9 63 74 20 52 50 20 61 73 20 6f 6e 5f 6f 70 74 20  ct RP as on_opt 
30bda 75 73 69 6e 67 5f 6f 70 74 20 2a 2f 0a 7b 0a 20  using_opt */.{. 
30bdb 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
30bdc 79 36 35 20 3d 20 73 71 6c 69 74 65 33 53 72 63  y65 = sqlite3Src
30bdd 4c 69 73 74 41 70 70 65 6e 64 46 72 6f 6d 54 65  ListAppendFromTe
30bde 72 6d 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  rm(pParse,yymsp[
30bdf 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 30  -6].minor.yy65,0
30be0 2c 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  ,0,&yymsp[-2].mi
30be1 6e 6f 72 2e 79 79 30 2c 79 79 6d 73 70 5b 2d 34  nor.yy0,yymsp[-4
30be2 5d 2e 6d 69 6e 6f 72 2e 79 79 33 2c 79 79 6d 73  ].minor.yy3,yyms
30be3 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33  p[-1].minor.yy13
30be4 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  2,yymsp[0].minor
30be5 2e 79 79 34 30 38 29 3b 0a 20 20 7d 0a 20 20 20  .yy408);.  }.   
30be6 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30be7 20 20 63 61 73 65 20 31 33 35 3a 20 2f 2a 20 73    case 135: /* s
30be8 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20 73 74  eltablist ::= st
30be9 6c 5f 70 72 65 66 69 78 20 4c 50 20 73 65 6c 74  l_prefix LP selt
30bea 61 62 6c 69 73 74 20 52 50 20 61 73 20 6f 6e 5f  ablist RP as on_
30beb 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 20 2a 2f  opt using_opt */
30bec 0a 7b 0a 20 20 20 20 69 66 28 20 79 79 6d 73 70  .{.    if( yymsp
30bed 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 3d  [-6].minor.yy65=
30bee 3d 30 20 26 26 20 79 79 6d 73 70 5b 2d 32 5d 2e  =0 && yymsp[-2].
30bef 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 3d 30 20 26  minor.yy0.n==0 &
30bf0 26 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  & yymsp[-1].mino
30bf1 72 2e 79 79 31 33 32 3d 3d 30 20 26 26 20 79 79  r.yy132==0 && yy
30bf2 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34  msp[0].minor.yy4
30bf3 30 38 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 79  08==0 ){.      y
30bf4 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20  ygotominor.yy65 
30bf5 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  = yymsp[-4].mino
30bf6 72 2e 79 79 36 35 3b 0a 20 20 20 20 7d 65 6c 73  r.yy65;.    }els
30bf7 65 7b 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20  e{.      Select 
30bf8 2a 70 53 75 62 71 75 65 72 79 3b 0a 20 20 20 20  *pSubquery;.    
30bf9 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
30bfa 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28 79 79  ShiftJoinType(yy
30bfb 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
30bfc 36 35 29 3b 0a 20 20 20 20 20 20 70 53 75 62 71  65);.      pSubq
30bfd 75 65 72 79 20 3d 20 73 71 6c 69 74 65 33 53 65  uery = sqlite3Se
30bfe 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c 30  lectNew(pParse,0
30bff 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  ,yymsp[-4].minor
30c00 2e 79 79 36 35 2c 30 2c 30 2c 30 2c 30 2c 30 2c  .yy65,0,0,0,0,0,
30c01 30 2c 30 29 3b 0a 20 20 20 20 20 20 79 79 67 6f  0,0);.      yygo
30c02 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20 3d 20 73  tominor.yy65 = s
30c03 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
30c04 65 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50 61 72  endFromTerm(pPar
30c05 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e  se,yymsp[-6].min
30c06 6f 72 2e 79 79 36 35 2c 30 2c 30 2c 26 79 79 6d  or.yy65,0,0,&yym
30c07 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
30c08 2c 70 53 75 62 71 75 65 72 79 2c 79 79 6d 73 70  ,pSubquery,yymsp
30c09 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32  [-1].minor.yy132
30c0a 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  ,yymsp[0].minor.
30c0b 79 79 34 30 38 29 3b 0a 20 20 20 20 7d 0a 20 20  yy408);.    }.  
30c0c 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
30c0d 0a 20 20 20 20 20 20 63 61 73 65 20 31 33 36 3a  .      case 136:
30c0e 20 2f 2a 20 64 62 6e 6d 20 3a 3a 3d 20 2a 2f 0a   /* dbnm ::= */.
30c0f 20 20 20 20 20 20 63 61 73 65 20 31 34 35 3a 20        case 145: 
30c10 2f 2a 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 3a  /* indexed_opt :
30c11 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  := */ yytestcase
30c12 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 34 35 29 3b  (yyruleno==145);
30c13 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
30c14 30 2e 7a 3d 30 3b 20 79 79 67 6f 74 6f 6d 69 6e  0.z=0; yygotomin
30c15 6f 72 2e 79 79 30 2e 6e 3d 30 3b 7d 0a 20 20 20  or.yy0.n=0;}.   
30c16 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30c17 20 20 63 61 73 65 20 31 33 38 3a 20 2f 2a 20 66    case 138: /* f
30c18 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 6e 6d 20 64  ullname ::= nm d
30c19 62 6e 6d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  bnm */.{yygotomi
30c1a 6e 6f 72 2e 79 79 36 35 20 3d 20 73 71 6c 69 74  nor.yy65 = sqlit
30c1b 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28  e3SrcListAppend(
30c1c 70 50 61 72 73 65 2d 3e 64 62 2c 30 2c 26 79 79  pParse->db,0,&yy
30c1d 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
30c1e 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
30c1f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20  r.yy0);}.       
30c20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
30c21 73 65 20 31 33 39 3a 20 2f 2a 20 6a 6f 69 6e 6f  se 139: /* joino
30c22 70 20 3a 3a 3d 20 43 4f 4d 4d 41 7c 4a 4f 49 4e  p ::= COMMA|JOIN
30c23 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
30c24 72 2e 79 79 33 32 38 20 3d 20 4a 54 5f 49 4e 4e  r.yy328 = JT_INN
30c25 45 52 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  ER; }.        br
30c26 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
30c27 31 34 30 3a 20 2f 2a 20 6a 6f 69 6e 6f 70 20 3a  140: /* joinop :
30c28 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 4a 4f 49 4e 20  := JOIN_KW JOIN 
30c29 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  */.{ yygotominor
30c2a 2e 79 79 33 32 38 20 3d 20 73 71 6c 69 74 65 33  .yy328 = sqlite3
30c2b 4a 6f 69 6e 54 79 70 65 28 70 50 61 72 73 65 2c  JoinType(pParse,
30c2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
30c2d 2e 79 79 30 2c 30 2c 30 29 3b 20 7d 0a 20 20 20  .yy0,0,0); }.   
30c2e 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30c2f 20 20 63 61 73 65 20 31 34 31 3a 20 2f 2a 20 6a    case 141: /* j
30c30 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b  oinop ::= JOIN_K
30c31 57 20 6e 6d 20 4a 4f 49 4e 20 2a 2f 0a 7b 20 79  W nm JOIN */.{ y
30c32 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38  ygotominor.yy328
30c33 20 3d 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79   = sqlite3JoinTy
30c34 70 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70  pe(pParse,&yymsp
30c35 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26  [-2].minor.yy0,&
30c36 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
30c37 79 79 30 2c 30 29 3b 20 7d 0a 20 20 20 20 20 20  yy0,0); }.      
30c38 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
30c39 61 73 65 20 31 34 32 3a 20 2f 2a 20 6a 6f 69 6e  ase 142: /* join
30c3a 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 6e  op ::= JOIN_KW n
30c3b 6d 20 6e 6d 20 4a 4f 49 4e 20 2a 2f 0a 7b 20 79  m nm JOIN */.{ y
30c3c 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38  ygotominor.yy328
30c3d 20 3d 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79   = sqlite3JoinTy
30c3e 70 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70  pe(pParse,&yymsp
30c3f 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26  [-3].minor.yy0,&
30c40 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
30c41 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  yy0,&yymsp[-1].m
30c42 69 6e 6f 72 2e 79 79 30 29 3b 20 7d 0a 20 20 20  inor.yy0); }.   
30c43 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30c44 20 20 63 61 73 65 20 31 34 33 3a 20 2f 2a 20 6f    case 143: /* o
30c45 6e 5f 6f 70 74 20 3a 3a 3d 20 4f 4e 20 65 78 70  n_opt ::= ON exp
30c46 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  r */.      case 
30c47 31 35 34 3a 20 2f 2a 20 73 6f 72 74 69 74 65 6d  154: /* sortitem
30c48 20 3a 3a 3d 20 65 78 70 72 20 2a 2f 20 79 79 74   ::= expr */ yyt
30c49 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
30c4a 3d 3d 31 35 34 29 3b 0a 20 20 20 20 20 20 63 61  ==154);.      ca
30c4b 73 65 20 31 36 31 3a 20 2f 2a 20 68 61 76 69 6e  se 161: /* havin
30c4c 67 5f 6f 70 74 20 3a 3a 3d 20 48 41 56 49 4e 47  g_opt ::= HAVING
30c4d 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63   expr */ yytestc
30c4e 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 36  ase(yyruleno==16
30c4f 31 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  1);.      case 1
30c50 36 38 3a 20 2f 2a 20 77 68 65 72 65 5f 6f 70 74  68: /* where_opt
30c51 20 3a 3a 3d 20 57 48 45 52 45 20 65 78 70 72 20   ::= WHERE expr 
30c52 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
30c53 72 75 6c 65 6e 6f 3d 3d 31 36 38 29 3b 0a 20 20  ruleno==168);.  
30c54 20 20 20 20 63 61 73 65 20 32 33 35 3a 20 2f 2a      case 235: /*
30c55 20 63 61 73 65 5f 65 6c 73 65 20 3a 3a 3d 20 45   case_else ::= E
30c56 4c 53 45 20 65 78 70 72 20 2a 2f 20 79 79 74 65  LSE expr */ yyte
30c57 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
30c58 3d 32 33 35 29 3b 0a 20 20 20 20 20 20 63 61 73  =235);.      cas
30c59 65 20 32 33 37 3a 20 2f 2a 20 63 61 73 65 5f 6f  e 237: /* case_o
30c5a 70 65 72 61 6e 64 20 3a 3a 3d 20 65 78 70 72 20  perand ::= expr 
30c5b 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
30c5c 72 75 6c 65 6e 6f 3d 3d 32 33 37 29 3b 0a 7b 79  ruleno==237);.{y
30c5d 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 33 32  ygotominor.yy132
30c5e 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
30c5f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 3b 7d 0a  r.yy346.pExpr;}.
30c60 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
30c61 20 20 20 20 20 63 61 73 65 20 31 34 34 3a 20 2f       case 144: /
30c62 2a 20 6f 6e 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a  * on_opt ::= */.
30c63 20 20 20 20 20 20 63 61 73 65 20 31 36 30 3a 20        case 160: 
30c64 2f 2a 20 68 61 76 69 6e 67 5f 6f 70 74 20 3a 3a  /* having_opt ::
30c65 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
30c66 79 79 72 75 6c 65 6e 6f 3d 3d 31 36 30 29 3b 0a  yyruleno==160);.
30c67 20 20 20 20 20 20 63 61 73 65 20 31 36 37 3a 20        case 167: 
30c68 2f 2a 20 77 68 65 72 65 5f 6f 70 74 20 3a 3a 3d  /* where_opt ::=
30c69 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
30c6a 79 72 75 6c 65 6e 6f 3d 3d 31 36 37 29 3b 0a 20  yruleno==167);. 
30c6b 20 20 20 20 20 63 61 73 65 20 32 33 36 3a 20 2f       case 236: /
30c6c 2a 20 63 61 73 65 5f 65 6c 73 65 20 3a 3a 3d 20  * case_else ::= 
30c6d 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
30c6e 72 75 6c 65 6e 6f 3d 3d 32 33 36 29 3b 0a 20 20  ruleno==236);.  
30c6f 20 20 20 20 63 61 73 65 20 32 33 38 3a 20 2f 2a      case 238: /*
30c70 20 63 61 73 65 5f 6f 70 65 72 61 6e 64 20 3a 3a   case_operand ::
30c71 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
30c72 79 79 72 75 6c 65 6e 6f 3d 3d 32 33 38 29 3b 0a  yyruleno==238);.
30c73 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  {yygotominor.yy1
30c74 33 32 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20  32 = 0;}.       
30c75 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
30c76 73 65 20 31 34 37 3a 20 2f 2a 20 69 6e 64 65 78  se 147: /* index
30c77 65 64 5f 6f 70 74 20 3a 3a 3d 20 4e 4f 54 20 49  ed_opt ::= NOT I
30c78 4e 44 45 58 45 44 20 2a 2f 0a 7b 79 79 67 6f 74  NDEXED */.{yygot
30c79 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3d 30 3b 20  ominor.yy0.z=0; 
30c7a 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e  yygotominor.yy0.
30c7b 6e 3d 31 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  n=1;}.        br
30c7c 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
30c7d 31 34 38 3a 20 2f 2a 20 75 73 69 6e 67 5f 6f 70  148: /* using_op
30c7e 74 20 3a 3a 3d 20 55 53 49 4e 47 20 4c 50 20 69  t ::= USING LP i
30c7f 6e 73 63 6f 6c 6c 69 73 74 20 52 50 20 2a 2f 0a  nscollist RP */.
30c80 20 20 20 20 20 20 63 61 73 65 20 31 38 30 3a 20        case 180: 
30c81 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70  /* inscollist_op
30c82 74 20 3a 3a 3d 20 4c 50 20 69 6e 73 63 6f 6c 6c  t ::= LP inscoll
30c83 69 73 74 20 52 50 20 2a 2f 20 79 79 74 65 73 74  ist RP */ yytest
30c84 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
30c85 38 30 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  80);.{yygotomino
30c86 72 2e 79 79 34 30 38 20 3d 20 79 79 6d 73 70 5b  r.yy408 = yymsp[
30c87 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 3b  -1].minor.yy408;
30c88 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
30c89 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 39 3a  .      case 149:
30c8a 20 2f 2a 20 75 73 69 6e 67 5f 6f 70 74 20 3a 3a   /* using_opt ::
30c8b 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  = */.      case 
30c8c 31 37 39 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69  179: /* inscolli
30c8d 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79  st_opt ::= */ yy
30c8e 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
30c8f 6f 3d 3d 31 37 39 29 3b 0a 7b 79 79 67 6f 74 6f  o==179);.{yygoto
30c90 6d 69 6e 6f 72 2e 79 79 34 30 38 20 3d 20 30 3b  minor.yy408 = 0;
30c91 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
30c92 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 31 3a  .      case 151:
30c93 20 2f 2a 20 6f 72 64 65 72 62 79 5f 6f 70 74 20   /* orderby_opt 
30c94 3a 3a 3d 20 4f 52 44 45 52 20 42 59 20 73 6f 72  ::= ORDER BY sor
30c95 74 6c 69 73 74 20 2a 2f 0a 20 20 20 20 20 20 63  tlist */.      c
30c96 61 73 65 20 31 35 39 3a 20 2f 2a 20 67 72 6f 75  ase 159: /* grou
30c97 70 62 79 5f 6f 70 74 20 3a 3a 3d 20 47 52 4f 55  pby_opt ::= GROU
30c98 50 20 42 59 20 6e 65 78 70 72 6c 69 73 74 20 2a  P BY nexprlist *
30c99 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
30c9a 75 6c 65 6e 6f 3d 3d 31 35 39 29 3b 0a 20 20 20  uleno==159);.   
30c9b 20 20 20 63 61 73 65 20 32 33 39 3a 20 2f 2a 20     case 239: /* 
30c9c 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 6e 65 78  exprlist ::= nex
30c9d 70 72 6c 69 73 74 20 2a 2f 20 79 79 74 65 73 74  prlist */ yytest
30c9e 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
30c9f 33 39 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  39);.{yygotomino
30ca0 72 2e 79 79 31 34 20 3d 20 79 79 6d 73 70 5b 30  r.yy14 = yymsp[0
30ca1 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 3b 7d 0a 20  ].minor.yy14;}. 
30ca2 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
30ca3 20 20 20 20 63 61 73 65 20 31 35 32 3a 20 2f 2a      case 152: /*
30ca4 20 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 73 6f   sortlist ::= so
30ca5 72 74 6c 69 73 74 20 43 4f 4d 4d 41 20 73 6f 72  rtlist COMMA sor
30ca6 74 69 74 65 6d 20 73 6f 72 74 6f 72 64 65 72 20  titem sortorder 
30ca7 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
30ca8 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65  or.yy14 = sqlite
30ca9 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
30caa 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 33 5d  pParse,yymsp[-3]
30cab 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 79 79 6d 73  .minor.yy14,yyms
30cac 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33  p[-1].minor.yy13
30cad 32 29 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f  2);.  if( yygoto
30cae 6d 69 6e 6f 72 2e 79 79 31 34 20 29 20 79 79 67  minor.yy14 ) yyg
30caf 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2d 3e 61  otominor.yy14->a
30cb0 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  [yygotominor.yy1
30cb1 34 2d 3e 6e 45 78 70 72 2d 31 5d 2e 73 6f 72 74  4->nExpr-1].sort
30cb2 4f 72 64 65 72 20 3d 20 28 75 38 29 79 79 6d 73  Order = (u8)yyms
30cb3 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  p[0].minor.yy328
30cb4 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
30cb5 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 35  k;.      case 15
30cb6 33 3a 20 2f 2a 20 73 6f 72 74 6c 69 73 74 20 3a  3: /* sortlist :
30cb7 3a 3d 20 73 6f 72 74 69 74 65 6d 20 73 6f 72 74  := sortitem sort
30cb8 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20 20 79 79 67  order */.{.  yyg
30cb9 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20  otominor.yy14 = 
30cba 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
30cbb 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30 2c 79  ppend(pParse,0,y
30cbc 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
30cbd 79 31 33 32 29 3b 0a 20 20 69 66 28 20 79 79 67  y132);.  if( yyg
30cbe 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 26 26  otominor.yy14 &&
30cbf 20 41 4c 57 41 59 53 28 79 79 67 6f 74 6f 6d 69   ALWAYS(yygotomi
30cc0 6e 6f 72 2e 79 79 31 34 2d 3e 61 29 20 29 20 79  nor.yy14->a) ) y
30cc1 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2d  ygotominor.yy14-
30cc2 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64 65 72 20  >a[0].sortOrder 
30cc3 3d 20 28 75 38 29 79 79 6d 73 70 5b 30 5d 2e 6d  = (u8)yymsp[0].m
30cc4 69 6e 6f 72 2e 79 79 33 32 38 3b 0a 7d 0a 20 20  inor.yy328;.}.  
30cc5 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
30cc6 20 20 20 63 61 73 65 20 31 35 35 3a 20 2f 2a 20     case 155: /* 
30cc7 73 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 20 41 53  sortorder ::= AS
30cc8 43 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  C */.      case 
30cc9 31 35 37 3a 20 2f 2a 20 73 6f 72 74 6f 72 64 65  157: /* sortorde
30cca 72 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63  r ::= */ yytestc
30ccb 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 35  ase(yyruleno==15
30ccc 37 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72  7);.{yygotominor
30ccd 2e 79 79 33 32 38 20 3d 20 53 51 4c 49 54 45 5f  .yy328 = SQLITE_
30cce 53 4f 5f 41 53 43 3b 7d 0a 20 20 20 20 20 20 20  SO_ASC;}.       
30ccf 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
30cd0 73 65 20 31 35 36 3a 20 2f 2a 20 73 6f 72 74 6f  se 156: /* sorto
30cd1 72 64 65 72 20 3a 3a 3d 20 44 45 53 43 20 2a 2f  rder ::= DESC */
30cd2 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
30cd3 33 32 38 20 3d 20 53 51 4c 49 54 45 5f 53 4f 5f  328 = SQLITE_SO_
30cd4 44 45 53 43 3b 7d 0a 20 20 20 20 20 20 20 20 62  DESC;}.        b
30cd5 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
30cd6 20 31 36 32 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f   162: /* limit_o
30cd7 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74  pt ::= */.{yygot
30cd8 6f 6d 69 6e 6f 72 2e 79 79 34 37 36 2e 70 4c 69  ominor.yy476.pLi
30cd9 6d 69 74 20 3d 20 30 3b 20 79 79 67 6f 74 6f 6d  mit = 0; yygotom
30cda 69 6e 6f 72 2e 79 79 34 37 36 2e 70 4f 66 66 73  inor.yy476.pOffs
30cdb 65 74 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20  et = 0;}.       
30cdc 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
30cdd 73 65 20 31 36 33 3a 20 2f 2a 20 6c 69 6d 69 74  se 163: /* limit
30cde 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65  _opt ::= LIMIT e
30cdf 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  xpr */.{yygotomi
30ce0 6e 6f 72 2e 79 79 34 37 36 2e 70 4c 69 6d 69 74  nor.yy476.pLimit
30ce1 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
30ce2 72 2e 79 79 33 34 36 2e 70 45 78 70 72 3b 20 79  r.yy346.pExpr; y
30ce3 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 36  ygotominor.yy476
30ce4 2e 70 4f 66 66 73 65 74 20 3d 20 30 3b 7d 0a 20  .pOffset = 0;}. 
30ce5 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
30ce6 20 20 20 20 63 61 73 65 20 31 36 34 3a 20 2f 2a      case 164: /*
30ce7 20 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c   limit_opt ::= L
30ce8 49 4d 49 54 20 65 78 70 72 20 4f 46 46 53 45 54  IMIT expr OFFSET
30ce9 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f   expr */.{yygoto
30cea 6d 69 6e 6f 72 2e 79 79 34 37 36 2e 70 4c 69 6d  minor.yy476.pLim
30ceb 69 74 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d  it = yymsp[-2].m
30cec 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
30ced 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ; yygotominor.yy
30cee 34 37 36 2e 70 4f 66 66 73 65 74 20 3d 20 79 79  476.pOffset = yy
30cef 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
30cf0 34 36 2e 70 45 78 70 72 3b 7d 0a 20 20 20 20 20  46.pExpr;}.     
30cf1 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
30cf2 63 61 73 65 20 31 36 35 3a 20 2f 2a 20 6c 69 6d  case 165: /* lim
30cf3 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54  it_opt ::= LIMIT
30cf4 20 65 78 70 72 20 43 4f 4d 4d 41 20 65 78 70 72   expr COMMA expr
30cf5 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
30cf6 2e 79 79 34 37 36 2e 70 4f 66 66 73 65 74 20 3d  .yy476.pOffset =
30cf7 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
30cf8 2e 79 79 33 34 36 2e 70 45 78 70 72 3b 20 79 79  .yy346.pExpr; yy
30cf9 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 36 2e  gotominor.yy476.
30cfa 70 4c 69 6d 69 74 20 3d 20 79 79 6d 73 70 5b 30  pLimit = yymsp[0
30cfb 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  ].minor.yy346.pE
30cfc 78 70 72 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  xpr;}.        br
30cfd 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
30cfe 31 36 36 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  166: /* cmd ::= 
30cff 44 45 4c 45 54 45 20 46 52 4f 4d 20 66 75 6c 6c  DELETE FROM full
30d00 6e 61 6d 65 20 69 6e 64 65 78 65 64 5f 6f 70 74  name indexed_opt
30d01 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a 7b 0a   where_opt */.{.
30d02 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
30d03 49 6e 64 65 78 65 64 42 79 28 70 50 61 72 73 65  IndexedBy(pParse
30d04 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  , yymsp[-2].mino
30d05 72 2e 79 79 36 35 2c 20 26 79 79 6d 73 70 5b 2d  r.yy65, &yymsp[-
30d06 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  1].minor.yy0);. 
30d07 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 46 72   sqlite3DeleteFr
30d08 6f 6d 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  om(pParse,yymsp[
30d09 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 79  -2].minor.yy65,y
30d0a 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
30d0b 31 33 32 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  132);.}.        
30d0c 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
30d0d 65 20 31 36 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 169: /* cmd ::
30d0e 3d 20 55 50 44 41 54 45 20 6f 72 63 6f 6e 66 20  = UPDATE orconf 
30d0f 66 75 6c 6c 6e 61 6d 65 20 69 6e 64 65 78 65 64  fullname indexed
30d10 5f 6f 70 74 20 53 45 54 20 73 65 74 6c 69 73 74  _opt SET setlist
30d11 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a 7b 0a   where_opt */.{.
30d12 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
30d13 49 6e 64 65 78 65 64 42 79 28 70 50 61 72 73 65  IndexedBy(pParse
30d14 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  , yymsp[-4].mino
30d15 72 2e 79 79 36 35 2c 20 26 79 79 6d 73 70 5b 2d  r.yy65, &yymsp[-
30d16 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  3].minor.yy0);. 
30d17 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
30d18 43 68 65 63 6b 4c 65 6e 67 74 68 28 70 50 61 72  CheckLength(pPar
30d19 73 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  se,yymsp[-1].min
30d1a 6f 72 2e 79 79 31 34 2c 22 73 65 74 20 6c 69 73  or.yy14,"set lis
30d1b 74 22 29 3b 20 0a 20 20 73 71 6c 69 74 65 33 55  t"); .  sqlite3U
30d1c 70 64 61 74 65 28 70 50 61 72 73 65 2c 79 79 6d  pdate(pParse,yym
30d1d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 36  sp[-4].minor.yy6
30d1e 35 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  5,yymsp[-1].mino
30d1f 72 2e 79 79 31 34 2c 79 79 6d 73 70 5b 30 5d 2e  r.yy14,yymsp[0].
30d20 6d 69 6e 6f 72 2e 79 79 31 33 32 2c 79 79 6d 73  minor.yy132,yyms
30d21 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38  p[-5].minor.yy18
30d22 36 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  6);.}.        br
30d23 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
30d24 31 37 30 3a 20 2f 2a 20 73 65 74 6c 69 73 74 20  170: /* setlist 
30d25 3a 3a 3d 20 73 65 74 6c 69 73 74 20 43 4f 4d 4d  ::= setlist COMM
30d26 41 20 6e 6d 20 45 51 20 65 78 70 72 20 2a 2f 0a  A nm EQ expr */.
30d27 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  {.  yygotominor.
30d28 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78  yy14 = sqlite3Ex
30d29 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
30d2a 72 73 65 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  rse, yymsp[-4].m
30d2b 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d 73 70  inor.yy14, yymsp
30d2c 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  [0].minor.yy346.
30d2d 70 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 65  pExpr);.  sqlite
30d2e 33 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65  3ExprListSetName
30d2f 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d  (pParse, yygotom
30d30 69 6e 6f 72 2e 79 79 31 34 2c 20 26 79 79 6d 73  inor.yy14, &yyms
30d31 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-2].minor.yy0,
30d32 20 31 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62   1);.}.        b
30d33 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
30d34 20 31 37 31 3a 20 2f 2a 20 73 65 74 6c 69 73 74   171: /* setlist
30d35 20 3a 3a 3d 20 6e 6d 20 45 51 20 65 78 70 72 20   ::= nm EQ expr 
30d36 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
30d37 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65  or.yy14 = sqlite
30d38 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
30d39 70 50 61 72 73 65 2c 20 30 2c 20 79 79 6d 73 70  pParse, 0, yymsp
30d3a 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  [0].minor.yy346.
30d3b 70 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 65  pExpr);.  sqlite
30d3c 33 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65  3ExprListSetName
30d3d 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d  (pParse, yygotom
30d3e 69 6e 6f 72 2e 79 79 31 34 2c 20 26 79 79 6d 73  inor.yy14, &yyms
30d3f 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-2].minor.yy0,
30d40 20 31 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62   1);.}.        b
30d41 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
30d42 20 31 37 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   172: /* cmd ::=
30d43 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f   insert_cmd INTO
30d44 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c   fullname inscol
30d45 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55 45 53 20  list_opt VALUES 
30d46 4c 50 20 69 74 65 6d 6c 69 73 74 20 52 50 20 2a  LP itemlist RP *
30d47 2f 0a 7b 73 71 6c 69 74 65 33 49 6e 73 65 72 74  /.{sqlite3Insert
30d48 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
30d49 35 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 20 79  5].minor.yy65, y
30d4a 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
30d4b 79 31 34 2c 20 30 2c 20 79 79 6d 73 70 5b 2d 34  y14, 0, yymsp[-4
30d4c 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 2c 20 79  ].minor.yy408, y
30d4d 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-7].minor.y
30d4e 79 31 38 36 29 3b 7d 0a 20 20 20 20 20 20 20 20  y186);}.        
30d4f 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
30d50 65 20 31 37 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 173: /* cmd ::
30d51 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54  = insert_cmd INT
30d52 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f  O fullname insco
30d53 6c 6c 69 73 74 5f 6f 70 74 20 73 65 6c 65 63 74  llist_opt select
30d54 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 49 6e 73 65   */.{sqlite3Inse
30d55 72 74 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  rt(pParse, yymsp
30d56 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c  [-2].minor.yy65,
30d57 20 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e   0, yymsp[0].min
30d58 6f 72 2e 79 79 33 2c 20 79 79 6d 73 70 5b 2d 31  or.yy3, yymsp[-1
30d59 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 2c 20 79  ].minor.yy408, y
30d5a 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
30d5b 79 31 38 36 29 3b 7d 0a 20 20 20 20 20 20 20 20  y186);}.        
30d5c 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
30d5d 65 20 31 37 34 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 174: /* cmd ::
30d5e 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54  = insert_cmd INT
30d5f 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f  O fullname insco
30d60 6c 6c 69 73 74 5f 6f 70 74 20 44 45 46 41 55 4c  llist_opt DEFAUL
30d61 54 20 56 41 4c 55 45 53 20 2a 2f 0a 7b 73 71 6c  T VALUES */.{sql
30d62 69 74 65 33 49 6e 73 65 72 74 28 70 50 61 72 73  ite3Insert(pPars
30d63 65 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  e, yymsp[-3].min
30d64 6f 72 2e 79 79 36 35 2c 20 30 2c 20 30 2c 20 79  or.yy65, 0, 0, y
30d65 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
30d66 79 34 30 38 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e  y408, yymsp[-5].
30d67 6d 69 6e 6f 72 2e 79 79 31 38 36 29 3b 7d 0a 20  minor.yy186);}. 
30d68 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
30d69 20 20 20 20 63 61 73 65 20 31 37 35 3a 20 2f 2a      case 175: /*
30d6a 20 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a 3d 20   insert_cmd ::= 
30d6b 49 4e 53 45 52 54 20 6f 72 63 6f 6e 66 20 2a 2f  INSERT orconf */
30d6c 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
30d6d 31 38 36 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  186 = yymsp[0].m
30d6e 69 6e 6f 72 2e 79 79 31 38 36 3b 7d 0a 20 20 20  inor.yy186;}.   
30d6f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30d70 20 20 63 61 73 65 20 31 37 36 3a 20 2f 2a 20 69    case 176: /* i
30d71 6e 73 65 72 74 5f 63 6d 64 20 3a 3a 3d 20 52 45  nsert_cmd ::= RE
30d72 50 4c 41 43 45 20 2a 2f 0a 7b 79 79 67 6f 74 6f  PLACE */.{yygoto
30d73 6d 69 6e 6f 72 2e 79 79 31 38 36 20 3d 20 4f 45  minor.yy186 = OE
30d74 5f 52 65 70 6c 61 63 65 3b 7d 0a 20 20 20 20 20  _Replace;}.     
30d75 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
30d76 63 61 73 65 20 31 37 37 3a 20 2f 2a 20 69 74 65  case 177: /* ite
30d77 6d 6c 69 73 74 20 3a 3a 3d 20 69 74 65 6d 6c 69  mlist ::= itemli
30d78 73 74 20 43 4f 4d 4d 41 20 65 78 70 72 20 2a 2f  st COMMA expr */
30d79 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 31 3a  .      case 241:
30d7a 20 2f 2a 20 6e 65 78 70 72 6c 69 73 74 20 3a 3a   /* nexprlist ::
30d7b 3d 20 6e 65 78 70 72 6c 69 73 74 20 43 4f 4d 4d  = nexprlist COMM
30d7c 41 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74  A expr */ yytest
30d7d 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
30d7e 34 31 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  41);.{yygotomino
30d7f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33  r.yy14 = sqlite3
30d80 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
30d81 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 32 5d 2e  Parse,yymsp[-2].
30d82 6d 69 6e 6f 72 2e 79 79 31 34 2c 79 79 6d 73 70  minor.yy14,yymsp
30d83 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  [0].minor.yy346.
30d84 70 45 78 70 72 29 3b 7d 0a 20 20 20 20 20 20 20  pExpr);}.       
30d85 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
30d86 73 65 20 31 37 38 3a 20 2f 2a 20 69 74 65 6d 6c  se 178: /* iteml
30d87 69 73 74 20 3a 3a 3d 20 65 78 70 72 20 2a 2f 0a  ist ::= expr */.
30d88 20 20 20 20 20 20 63 61 73 65 20 32 34 32 3a 20        case 242: 
30d89 2f 2a 20 6e 65 78 70 72 6c 69 73 74 20 3a 3a 3d  /* nexprlist ::=
30d8a 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63   expr */ yytestc
30d8b 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 34  ase(yyruleno==24
30d8c 32 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72  2);.{yygotominor
30d8d 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33 45  .yy14 = sqlite3E
30d8e 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
30d8f 61 72 73 65 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e  arse,0,yymsp[0].
30d90 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
30d91 72 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  r);}.        bre
30d92 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
30d93 38 31 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73  81: /* inscollis
30d94 74 20 3a 3a 3d 20 69 6e 73 63 6f 6c 6c 69 73 74  t ::= inscollist
30d95 20 43 4f 4d 4d 41 20 6e 6d 20 2a 2f 0a 7b 79 79   COMMA nm */.{yy
30d96 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 30 38 20  gotominor.yy408 
30d97 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 41  = sqlite3IdListA
30d98 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62  ppend(pParse->db
30d99 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  ,yymsp[-2].minor
30d9a 2e 79 79 34 30 38 2c 26 79 79 6d 73 70 5b 30 5d  .yy408,&yymsp[0]
30d9b 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20  .minor.yy0);}.  
30d9c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
30d9d 20 20 20 63 61 73 65 20 31 38 32 3a 20 2f 2a 20     case 182: /* 
30d9e 69 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 6e  inscollist ::= n
30d9f 6d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  m */.{yygotomino
30da0 72 2e 79 79 34 30 38 20 3d 20 73 71 6c 69 74 65  r.yy408 = sqlite
30da1 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28 70 50  3IdListAppend(pP
30da2 61 72 73 65 2d 3e 64 62 2c 30 2c 26 79 79 6d 73  arse->db,0,&yyms
30da3 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
30da4 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
30da5 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 33 3a  .      case 183:
30da6 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 74 65 72   /* expr ::= ter
30da7 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  m */.      case 
30da8 32 31 31 3a 20 2f 2a 20 65 73 63 61 70 65 20 3a  211: /* escape :
30da9 3a 3d 20 45 53 43 41 50 45 20 65 78 70 72 20 2a  := ESCAPE expr *
30daa 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
30dab 75 6c 65 6e 6f 3d 3d 32 31 31 29 3b 0a 7b 79 79  uleno==211);.{yy
30dac 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 20  gotominor.yy346 
30dad 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
30dae 2e 79 79 33 34 36 3b 7d 0a 20 20 20 20 20 20 20  .yy346;}.       
30daf 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
30db0 73 65 20 31 38 34 3a 20 2f 2a 20 65 78 70 72 20  se 184: /* expr 
30db1 3a 3a 3d 20 4c 50 20 65 78 70 72 20 52 50 20 2a  ::= LP expr RP *
30db2 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
30db3 79 33 34 36 2e 70 45 78 70 72 20 3d 20 79 79 6d  y346.pExpr = yym
30db4 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
30db5 34 36 2e 70 45 78 70 72 3b 20 73 70 61 6e 53 65  46.pExpr; spanSe
30db6 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  t(&yygotominor.y
30db7 79 33 34 36 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e  y346,&yymsp[-2].
30db8 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
30db9 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d  [0].minor.yy0);}
30dba 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
30dbb 20 20 20 20 20 20 63 61 73 65 20 31 38 35 3a 20        case 185: 
30dbc 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20 4e 55 4c 4c  /* term ::= NULL
30dbd 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
30dbe 39 30 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20  90: /* term ::= 
30dbf 49 4e 54 45 47 45 52 7c 46 4c 4f 41 54 7c 42 4c  INTEGER|FLOAT|BL
30dc0 4f 42 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  OB */ yytestcase
30dc1 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 39 30 29 3b  (yyruleno==190);
30dc2 0a 20 20 20 20 20 20 63 61 73 65 20 31 39 31 3a  .      case 191:
30dc3 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20 53 54 52   /* term ::= STR
30dc4 49 4e 47 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ING */ yytestcas
30dc5 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 39 31 29  e(yyruleno==191)
30dc6 3b 0a 7b 73 70 61 6e 45 78 70 72 28 26 79 79 67  ;.{spanExpr(&yyg
30dc7 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 20  otominor.yy346, 
30dc8 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d  pParse, yymsp[0]
30dc9 2e 6d 61 6a 6f 72 2c 20 26 79 79 6d 73 70 5b 30  .major, &yymsp[0
30dca 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20  ].minor.yy0);}. 
30dcb 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
30dcc 20 20 20 20 63 61 73 65 20 31 38 36 3a 20 2f 2a      case 186: /*
30dcd 20 65 78 70 72 20 3a 3a 3d 20 69 64 20 2a 2f 0a   expr ::= id */.
30dce 20 20 20 20 20 20 63 61 73 65 20 31 38 37 3a 20        case 187: 
30dcf 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 4a 4f 49 4e  /* expr ::= JOIN
30dd0 5f 4b 57 20 2a 2f 20 79 79 74 65 73 74 63 61 73  _KW */ yytestcas
30dd1 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 38 37 29  e(yyruleno==187)
30dd2 3b 0a 7b 73 70 61 6e 45 78 70 72 28 26 79 79 67  ;.{spanExpr(&yyg
30dd3 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 20  otominor.yy346, 
30dd4 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 26  pParse, TK_ID, &
30dd5 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
30dd6 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  y0);}.        br
30dd7 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
30dd8 31 38 38 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  188: /* expr ::=
30dd9 20 6e 6d 20 44 4f 54 20 6e 6d 20 2a 2f 0a 7b 0a   nm DOT nm */.{.
30dda 20 20 45 78 70 72 20 2a 74 65 6d 70 31 20 3d 20    Expr *temp1 = 
30ddb 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
30ddc 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30  rse, TK_ID, 0, 0
30ddd 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  , &yymsp[-2].min
30dde 6f 72 2e 79 79 30 29 3b 0a 20 20 45 78 70 72 20  or.yy0);.  Expr 
30ddf 2a 74 65 6d 70 32 20 3d 20 73 71 6c 69 74 65 33  *temp2 = sqlite3
30de0 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
30de1 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73  _ID, 0, 0, &yyms
30de2 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
30de3 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
30de4 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c  y346.pExpr = sql
30de5 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
30de6 2c 20 54 4b 5f 44 4f 54 2c 20 74 65 6d 70 31 2c  , TK_DOT, temp1,
30de7 20 74 65 6d 70 32 2c 20 30 29 3b 0a 20 20 73 70   temp2, 0);.  sp
30de8 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e  anSet(&yygotomin
30de9 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b  or.yy346,&yymsp[
30dea 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -2].minor.yy0,&y
30deb 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
30dec 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
30ded 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
30dee 31 38 39 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  189: /* expr ::=
30def 20 6e 6d 20 44 4f 54 20 6e 6d 20 44 4f 54 20 6e   nm DOT nm DOT n
30df0 6d 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 74  m */.{.  Expr *t
30df1 65 6d 70 31 20 3d 20 73 71 6c 69 74 65 33 50 45  emp1 = sqlite3PE
30df2 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
30df3 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b  D, 0, 0, &yymsp[
30df4 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  -4].minor.yy0);.
30df5 20 20 45 78 70 72 20 2a 74 65 6d 70 32 20 3d 20    Expr *temp2 = 
30df6 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
30df7 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30  rse, TK_ID, 0, 0
30df8 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  , &yymsp[-2].min
30df9 6f 72 2e 79 79 30 29 3b 0a 20 20 45 78 70 72 20  or.yy0);.  Expr 
30dfa 2a 74 65 6d 70 33 20 3d 20 73 71 6c 69 74 65 33  *temp3 = sqlite3
30dfb 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
30dfc 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73  _ID, 0, 0, &yyms
30dfd 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
30dfe 0a 20 20 45 78 70 72 20 2a 74 65 6d 70 34 20 3d  .  Expr *temp4 =
30dff 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
30e00 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 74 65  arse, TK_DOT, te
30e01 6d 70 32 2c 20 74 65 6d 70 33 2c 20 30 29 3b 0a  mp2, temp3, 0);.
30e02 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
30e03 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69  346.pExpr = sqli
30e04 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
30e05 20 54 4b 5f 44 4f 54 2c 20 74 65 6d 70 31 2c 20   TK_DOT, temp1, 
30e06 74 65 6d 70 34 2c 20 30 29 3b 0a 20 20 73 70 61  temp4, 0);.  spa
30e07 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f  nSet(&yygotomino
30e08 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b 2d  r.yy346,&yymsp[-
30e09 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  4].minor.yy0,&yy
30e0a 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
30e0b 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
30e0c 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
30e0d 39 32 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  92: /* expr ::= 
30e0e 52 45 47 49 53 54 45 52 20 2a 2f 0a 7b 0a 20 20  REGISTER */.{.  
30e0f 2f 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20 61 20  /* When doing a 
30e10 6e 65 73 74 65 64 20 70 61 72 73 65 2c 20 6f 6e  nested parse, on
30e11 65 20 63 61 6e 20 69 6e 63 6c 75 64 65 20 74 65  e can include te
30e12 72 6d 73 20 69 6e 20 61 6e 20 65 78 70 72 65 73  rms in an expres
30e13 73 69 6f 6e 0a 20 20 2a 2a 20 74 68 61 74 20 6c  sion.  ** that l
30e14 6f 6f 6b 20 6c 69 6b 65 20 74 68 69 73 3a 20 20  ook like this:  
30e15 20 23 31 20 23 32 20 2e 2e 2e 20 20 54 68 65 73   #1 #2 ...  Thes
30e16 65 20 74 65 72 6d 73 20 72 65 66 65 72 20 74 6f  e terms refer to
30e17 20 72 65 67 69 73 74 65 72 73 0a 20 20 2a 2a 20   registers.  ** 
30e18 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d  in the virtual m
30e19 61 63 68 69 6e 65 2e 20 20 23 4e 20 69 73 20 74  achine.  #N is t
30e1a 68 65 20 4e 2d 74 68 20 72 65 67 69 73 74 65 72  he N-th register
30e1b 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 72 73  . */.  if( pPars
30e1c 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b 0a  e->nested==0 ){.
30e1d 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
30e1e 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 65 61  Msg(pParse, "nea
30e1f 72 20 5c 22 25 54 5c 22 3a 20 73 79 6e 74 61 78  r \"%T\": syntax
30e20 20 65 72 72 6f 72 22 2c 20 26 79 79 6d 73 70 5b   error", &yymsp[
30e21 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  0].minor.yy0);. 
30e22 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
30e23 79 33 34 36 2e 70 45 78 70 72 20 3d 20 30 3b 0a  y346.pExpr = 0;.
30e24 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 79 79 67    }else{.    yyg
30e25 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
30e26 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45  Expr = sqlite3PE
30e27 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 52  xpr(pParse, TK_R
30e28 45 47 49 53 54 45 52 2c 20 30 2c 20 30 2c 20 26  EGISTER, 0, 0, &
30e29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
30e2a 79 30 29 3b 0a 20 20 20 20 69 66 28 20 79 79 67  y0);.    if( yyg
30e2b 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
30e2c 45 78 70 72 20 29 20 73 71 6c 69 74 65 33 47 65  Expr ) sqlite3Ge
30e2d 74 49 6e 74 33 32 28 26 79 79 6d 73 70 5b 30 5d  tInt32(&yymsp[0]
30e2e 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 31 5d 2c  .minor.yy0.z[1],
30e2f 20 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79   &yygotominor.yy
30e30 33 34 36 2e 70 45 78 70 72 2d 3e 69 54 61 62 6c  346.pExpr->iTabl
30e31 65 29 3b 0a 20 20 7d 0a 20 20 73 70 61 6e 53 65  e);.  }.  spanSe
30e32 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  t(&yygotominor.y
30e33 79 33 34 36 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  y346, &yymsp[0].
30e34 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73  minor.yy0, &yyms
30e35 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
30e36 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
30e37 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 39 33  ;.      case 193
30e38 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 56 41  : /* expr ::= VA
30e39 52 49 41 42 4c 45 20 2a 2f 0a 7b 0a 20 20 73 70  RIABLE */.{.  sp
30e3a 61 6e 45 78 70 72 28 26 79 79 67 6f 74 6f 6d 69  anExpr(&yygotomi
30e3b 6e 6f 72 2e 79 79 33 34 36 2c 20 70 50 61 72 73  nor.yy346, pPars
30e3c 65 2c 20 54 4b 5f 56 41 52 49 41 42 4c 45 2c 20  e, TK_VARIABLE, 
30e3d 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
30e3e 79 79 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  yy0);.  sqlite3E
30e3f 78 70 72 41 73 73 69 67 6e 56 61 72 4e 75 6d 62  xprAssignVarNumb
30e40 65 72 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74  er(pParse, yygot
30e41 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
30e42 70 72 29 3b 0a 20 20 73 70 61 6e 53 65 74 28 26  pr);.  spanSet(&
30e43 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
30e44 36 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  6, &yymsp[0].min
30e45 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 30  or.yy0, &yymsp[0
30e46 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a  ].minor.yy0);.}.
30e47 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
30e48 20 20 20 20 20 63 61 73 65 20 31 39 34 3a 20 2f       case 194: /
30e49 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20  * expr ::= expr 
30e4a 43 4f 4c 4c 41 54 45 20 69 64 73 20 2a 2f 0a 7b  COLLATE ids */.{
30e4b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
30e4c 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c  y346.pExpr = sql
30e4d 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28  ite3ExprSetColl(
30e4e 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 32  pParse, yymsp[-2
30e4f 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  ].minor.yy346.pE
30e50 78 70 72 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  xpr, &yymsp[0].m
30e51 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 79 79 67  inor.yy0);.  yyg
30e52 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a  otominor.yy346.z
30e53 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 32  Start = yymsp[-2
30e54 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53  ].minor.yy346.zS
30e55 74 61 72 74 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  tart;.  yygotomi
30e56 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64 20 3d  nor.yy346.zEnd =
30e57 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
30e58 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e  .yy0.z[yymsp[0].
30e59 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a 7d 0a  minor.yy0.n];.}.
30e5a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
30e5b 20 20 20 20 20 63 61 73 65 20 31 39 35 3a 20 2f       case 195: /
30e5c 2a 20 65 78 70 72 20 3a 3a 3d 20 43 41 53 54 20  * expr ::= CAST 
30e5d 4c 50 20 65 78 70 72 20 41 53 20 74 79 70 65 74  LP expr AS typet
30e5e 6f 6b 65 6e 20 52 50 20 2a 2f 0a 7b 0a 20 20 79  oken RP */.{.  y
30e5f 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
30e60 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  .pExpr = sqlite3
30e61 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
30e62 5f 43 41 53 54 2c 20 79 79 6d 73 70 5b 2d 33 5d  _CAST, yymsp[-3]
30e63 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  .minor.yy346.pEx
30e64 70 72 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 31  pr, 0, &yymsp[-1
30e65 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20  ].minor.yy0);.  
30e66 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d  spanSet(&yygotom
30e67 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73  inor.yy346,&yyms
30e68 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-5].minor.yy0,
30e69 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
30e6a 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  yy0);.}.        
30e6b 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
30e6c 65 20 31 39 36 3a 20 2f 2a 20 65 78 70 72 20 3a  e 196: /* expr :
30e6d 3a 3d 20 49 44 20 4c 50 20 64 69 73 74 69 6e 63  := ID LP distinc
30e6e 74 20 65 78 70 72 6c 69 73 74 20 52 50 20 2a 2f  t exprlist RP */
30e6f 0a 7b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d  .{.  if( yymsp[-
30e70 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 20 26 26  1].minor.yy14 &&
30e71 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
30e72 2e 79 79 31 34 2d 3e 6e 45 78 70 72 3e 70 50 61  .yy14->nExpr>pPa
30e73 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b  rse->db->aLimit[
30e74 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e  SQLITE_LIMIT_FUN
30e75 43 54 49 4f 4e 5f 41 52 47 5d 20 29 7b 0a 20 20  CTION_ARG] ){.  
30e76 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
30e77 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d  g(pParse, "too m
30e78 61 6e 79 20 61 72 67 75 6d 65 6e 74 73 20 6f 6e  any arguments on
30e79 20 66 75 6e 63 74 69 6f 6e 20 25 54 22 2c 20 26   function %T", &
30e7a 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
30e7b 79 79 30 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f  yy0);.  }.  yygo
30e7c 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
30e7d 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70  xpr = sqlite3Exp
30e7e 72 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65  rFunction(pParse
30e7f 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
30e80 72 2e 79 79 31 34 2c 20 26 79 79 6d 73 70 5b 2d  r.yy14, &yymsp[-
30e81 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  4].minor.yy0);. 
30e82 20 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f   spanSet(&yygoto
30e83 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d  minor.yy346,&yym
30e84 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-4].minor.yy0
30e85 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
30e86 2e 79 79 30 29 3b 0a 20 20 69 66 28 20 79 79 6d  .yy0);.  if( yym
30e87 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-2].minor.yy3
30e88 32 38 20 26 26 20 79 79 67 6f 74 6f 6d 69 6e 6f  28 && yygotomino
30e89 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29 7b  r.yy346.pExpr ){
30e8a 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
30e8b 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e 66 6c  .yy346.pExpr->fl
30e8c 61 67 73 20 7c 3d 20 45 50 5f 44 69 73 74 69 6e  ags |= EP_Distin
30e8d 63 74 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20 20 20  ct;.  }.}.      
30e8e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
30e8f 61 73 65 20 31 39 37 3a 20 2f 2a 20 65 78 70 72  ase 197: /* expr
30e90 20 3a 3a 3d 20 49 44 20 4c 50 20 53 54 41 52 20   ::= ID LP STAR 
30e91 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  RP */.{.  yygoto
30e92 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
30e93 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 46  r = sqlite3ExprF
30e94 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  unction(pParse, 
30e95 30 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  0, &yymsp[-3].mi
30e96 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 70 61 6e  nor.yy0);.  span
30e97 53 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72  Set(&yygotominor
30e98 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b 2d 33  .yy346,&yymsp[-3
30e99 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d  ].minor.yy0,&yym
30e9a 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
30e9b 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
30e9c 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 39  k;.      case 19
30e9d 38 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20 43  8: /* term ::= C
30e9e 54 49 4d 45 5f 4b 57 20 2a 2f 0a 7b 0a 20 20 2f  TIME_KW */.{.  /
30e9f 2a 20 54 68 65 20 43 55 52 52 45 4e 54 5f 54 49  * The CURRENT_TI
30ea0 4d 45 2c 20 43 55 52 52 45 4e 54 5f 44 41 54 45  ME, CURRENT_DATE
30ea1 2c 20 61 6e 64 20 43 55 52 52 45 4e 54 5f 54 49  , and CURRENT_TI
30ea2 4d 45 53 54 41 4d 50 20 76 61 6c 75 65 73 20 61  MESTAMP values a
30ea3 72 65 0a 20 20 2a 2a 20 74 72 65 61 74 65 64 20  re.  ** treated 
30ea4 61 73 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61  as functions tha
30ea5 74 20 72 65 74 75 72 6e 20 63 6f 6e 73 74 61 6e  t return constan
30ea6 74 73 20 2a 2f 0a 20 20 79 79 67 6f 74 6f 6d 69  ts */.  yygotomi
30ea7 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20  nor.yy346.pExpr 
30ea8 3d 20 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e  = sqlite3ExprFun
30ea9 63 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c  ction(pParse, 0,
30eaa 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
30eab 79 79 30 29 3b 0a 20 20 69 66 28 20 79 79 67 6f  yy0);.  if( yygo
30eac 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
30ead 78 70 72 20 29 7b 0a 20 20 20 20 79 79 67 6f 74  xpr ){.    yygot
30eae 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
30eaf 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4e 53  pr->op = TK_CONS
30eb0 54 5f 46 55 4e 43 3b 20 20 0a 20 20 7d 0a 20 20  T_FUNC;  .  }.  
30eb1 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d  spanSet(&yygotom
30eb2 69 6e 6f 72 2e 79 79 33 34 36 2c 20 26 79 79 6d  inor.yy346, &yym
30eb3 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  sp[0].minor.yy0,
30eb4 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
30eb5 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20  .yy0);.}.       
30eb6 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
30eb7 73 65 20 31 39 39 3a 20 2f 2a 20 65 78 70 72 20  se 199: /* expr 
30eb8 3a 3a 3d 20 65 78 70 72 20 41 4e 44 20 65 78 70  ::= expr AND exp
30eb9 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  r */.      case 
30eba 32 30 30 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  200: /* expr ::=
30ebb 20 65 78 70 72 20 4f 52 20 65 78 70 72 20 2a 2f   expr OR expr */
30ebc 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
30ebd 6c 65 6e 6f 3d 3d 32 30 30 29 3b 0a 20 20 20 20  leno==200);.    
30ebe 20 20 63 61 73 65 20 32 30 31 3a 20 2f 2a 20 65    case 201: /* e
30ebf 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 4c 54 7c  xpr ::= expr LT|
30ec0 47 54 7c 47 45 7c 4c 45 20 65 78 70 72 20 2a 2f  GT|GE|LE expr */
30ec1 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
30ec2 6c 65 6e 6f 3d 3d 32 30 31 29 3b 0a 20 20 20 20  leno==201);.    
30ec3 20 20 63 61 73 65 20 32 30 32 3a 20 2f 2a 20 65    case 202: /* e
30ec4 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 45 51 7c  xpr ::= expr EQ|
30ec5 4e 45 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73  NE expr */ yytes
30ec6 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
30ec7 32 30 32 29 3b 0a 20 20 20 20 20 20 63 61 73 65  202);.      case
30ec8 20 32 30 33 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   203: /* expr ::
30ec9 3d 20 65 78 70 72 20 42 49 54 41 4e 44 7c 42 49  = expr BITAND|BI
30eca 54 4f 52 7c 4c 53 48 49 46 54 7c 52 53 48 49 46  TOR|LSHIFT|RSHIF
30ecb 54 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74  T expr */ yytest
30ecc 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
30ecd 30 33 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  03);.      case 
30ece 32 30 34 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  204: /* expr ::=
30ecf 20 65 78 70 72 20 50 4c 55 53 7c 4d 49 4e 55 53   expr PLUS|MINUS
30ed0 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63   expr */ yytestc
30ed1 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 30  ase(yyruleno==20
30ed2 34 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  4);.      case 2
30ed3 30 35 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  05: /* expr ::= 
30ed4 65 78 70 72 20 53 54 41 52 7c 53 4c 41 53 48 7c  expr STAR|SLASH|
30ed5 52 45 4d 20 65 78 70 72 20 2a 2f 20 79 79 74 65  REM expr */ yyte
30ed6 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
30ed7 3d 32 30 35 29 3b 0a 20 20 20 20 20 20 63 61 73  =205);.      cas
30ed8 65 20 32 30 36 3a 20 2f 2a 20 65 78 70 72 20 3a  e 206: /* expr :
30ed9 3a 3d 20 65 78 70 72 20 43 4f 4e 43 41 54 20 65  := expr CONCAT e
30eda 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73  xpr */ yytestcas
30edb 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 30 36 29  e(yyruleno==206)
30edc 3b 0a 7b 73 70 61 6e 42 69 6e 61 72 79 45 78 70  ;.{spanBinaryExp
30edd 72 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  r(&yygotominor.y
30ede 79 33 34 36 2c 70 50 61 72 73 65 2c 79 79 6d 73  y346,pParse,yyms
30edf 70 5b 2d 31 5d 2e 6d 61 6a 6f 72 2c 26 79 79 6d  p[-1].major,&yym
30ee0 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-2].minor.yy3
30ee1 34 36 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  46,&yymsp[0].min
30ee2 6f 72 2e 79 79 33 34 36 29 3b 7d 0a 20 20 20 20  or.yy346);}.    
30ee3 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
30ee4 20 63 61 73 65 20 32 30 37 3a 20 2f 2a 20 6c 69   case 207: /* li
30ee5 6b 65 6f 70 20 3a 3a 3d 20 4c 49 4b 45 5f 4b 57  keop ::= LIKE_KW
30ee6 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
30ee7 30 39 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a 3a  09: /* likeop ::
30ee8 3d 20 4d 41 54 43 48 20 2a 2f 20 79 79 74 65 73  = MATCH */ yytes
30ee9 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
30eea 32 30 39 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e  209);.{yygotomin
30eeb 6f 72 2e 79 79 39 36 2e 65 4f 70 65 72 61 74 6f  or.yy96.eOperato
30eec 72 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  r = yymsp[0].min
30eed 6f 72 2e 79 79 30 3b 20 79 79 67 6f 74 6f 6d 69  or.yy0; yygotomi
30eee 6e 6f 72 2e 79 79 39 36 2e 6e 6f 74 20 3d 20 30  nor.yy96.not = 0
30eef 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
30ef0 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 30 38  ;.      case 208
30ef1 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a 3a 3d 20  : /* likeop ::= 
30ef2 4e 4f 54 20 4c 49 4b 45 5f 4b 57 20 2a 2f 0a 20  NOT LIKE_KW */. 
30ef3 20 20 20 20 20 63 61 73 65 20 32 31 30 3a 20 2f       case 210: /
30ef4 2a 20 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54  * likeop ::= NOT
30ef5 20 4d 41 54 43 48 20 2a 2f 20 79 79 74 65 73 74   MATCH */ yytest
30ef6 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
30ef7 31 30 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  10);.{yygotomino
30ef8 72 2e 79 79 39 36 2e 65 4f 70 65 72 61 74 6f 72  r.yy96.eOperator
30ef9 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
30efa 72 2e 79 79 30 3b 20 79 79 67 6f 74 6f 6d 69 6e  r.yy0; yygotomin
30efb 6f 72 2e 79 79 39 36 2e 6e 6f 74 20 3d 20 31 3b  or.yy96.not = 1;
30efc 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
30efd 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 32 3a  .      case 212:
30efe 20 2f 2a 20 65 73 63 61 70 65 20 3a 3a 3d 20 2a   /* escape ::= *
30eff 2f 0a 7b 6d 65 6d 73 65 74 28 26 79 79 67 6f 74  /.{memset(&yygot
30f00 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 30 2c 73  ominor.yy346,0,s
30f01 69 7a 65 6f 66 28 79 79 67 6f 74 6f 6d 69 6e 6f  izeof(yygotomino
30f02 72 2e 79 79 33 34 36 29 29 3b 7d 0a 20 20 20 20  r.yy346));}.    
30f03 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
30f04 20 63 61 73 65 20 32 31 33 3a 20 2f 2a 20 65 78   case 213: /* ex
30f05 70 72 20 3a 3a 3d 20 65 78 70 72 20 6c 69 6b 65  pr ::= expr like
30f06 6f 70 20 65 78 70 72 20 65 73 63 61 70 65 20 2a  op expr escape *
30f07 2f 0a 7b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a  /.{.  ExprList *
30f08 70 4c 69 73 74 3b 0a 20 20 70 4c 69 73 74 20 3d  pList;.  pList =
30f09 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
30f0a 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30 2c  Append(pParse,0,
30f0b 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
30f0c 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 20  .yy346.pExpr);. 
30f0d 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33   pList = sqlite3
30f0e 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
30f0f 50 61 72 73 65 2c 70 4c 69 73 74 2c 20 79 79 6d  Parse,pList, yym
30f10 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-3].minor.yy3
30f11 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 69 66 28  46.pExpr);.  if(
30f12 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
30f13 79 79 33 34 36 2e 70 45 78 70 72 20 29 7b 0a 20  yy346.pExpr ){. 
30f14 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74     pList = sqlit
30f15 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
30f16 28 70 50 61 72 73 65 2c 70 4c 69 73 74 2c 20 79  (pParse,pList, y
30f17 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
30f18 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 7d 0a  346.pExpr);.  }.
30f19 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
30f1a 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69  346.pExpr = sqli
30f1b 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28  te3ExprFunction(
30f1c 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c 20 26  pParse, pList, &
30f1d 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
30f1e 79 79 39 36 2e 65 4f 70 65 72 61 74 6f 72 29 3b  yy96.eOperator);
30f1f 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 32 5d  .  if( yymsp[-2]
30f20 2e 6d 69 6e 6f 72 2e 79 79 39 36 2e 6e 6f 74 20  .minor.yy96.not 
30f21 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ) yygotominor.yy
30f22 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69  346.pExpr = sqli
30f23 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
30f24 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d   TK_NOT, yygotom
30f25 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
30f26 2c 20 30 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74  , 0, 0);.  yygot
30f27 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74  ominor.yy346.zSt
30f28 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e  art = yymsp[-3].
30f29 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61  minor.yy346.zSta
30f2a 72 74 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  rt;.  yygotomino
30f2b 72 2e 79 79 33 34 36 2e 7a 45 6e 64 20 3d 20 79  r.yy346.zEnd = y
30f2c 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
30f2d 79 33 34 36 2e 7a 45 6e 64 3b 0a 20 20 69 66 28  y346.zEnd;.  if(
30f2e 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
30f2f 34 36 2e 70 45 78 70 72 20 29 20 79 79 67 6f 74  46.pExpr ) yygot
30f30 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
30f31 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f  pr->flags |= EP_
30f32 49 6e 66 69 78 46 75 6e 63 3b 0a 7d 0a 20 20 20  InfixFunc;.}.   
30f33 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30f34 20 20 63 61 73 65 20 32 31 34 3a 20 2f 2a 20 65    case 214: /* e
30f35 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 4e  xpr ::= expr ISN
30f36 55 4c 4c 7c 4e 4f 54 4e 55 4c 4c 20 2a 2f 0a 7b  ULL|NOTNULL */.{
30f37 73 70 61 6e 55 6e 61 72 79 50 6f 73 74 66 69 78  spanUnaryPostfix
30f38 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  (&yygotominor.yy
30f39 33 34 36 2c 70 50 61 72 73 65 2c 79 79 6d 73 70  346,pParse,yymsp
30f3a 5b 30 5d 2e 6d 61 6a 6f 72 2c 26 79 79 6d 73 70  [0].major,&yymsp
30f3b 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  [-1].minor.yy346
30f3c 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
30f3d 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  .yy0);}.        
30f3e 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
30f3f 65 20 32 31 35 3a 20 2f 2a 20 65 78 70 72 20 3a  e 215: /* expr :
30f40 3a 3d 20 65 78 70 72 20 49 53 20 4e 55 4c 4c 20  := expr IS NULL 
30f41 2a 2f 0a 7b 73 70 61 6e 55 6e 61 72 79 50 6f 73  */.{spanUnaryPos
30f42 74 66 69 78 28 26 79 79 67 6f 74 6f 6d 69 6e 6f  tfix(&yygotomino
30f43 72 2e 79 79 33 34 36 2c 70 50 61 72 73 65 2c 54  r.yy346,pParse,T
30f44 4b 5f 49 53 4e 55 4c 4c 2c 26 79 79 6d 73 70 5b  K_ISNULL,&yymsp[
30f45 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2c  -2].minor.yy346,
30f46 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
30f47 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  yy0);}.        b
30f48 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
30f49 20 32 31 36 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   216: /* expr ::
30f4a 3d 20 65 78 70 72 20 4e 4f 54 20 4e 55 4c 4c 20  = expr NOT NULL 
30f4b 2a 2f 0a 7b 73 70 61 6e 55 6e 61 72 79 50 6f 73  */.{spanUnaryPos
30f4c 74 66 69 78 28 26 79 79 67 6f 74 6f 6d 69 6e 6f  tfix(&yygotomino
30f4d 72 2e 79 79 33 34 36 2c 70 50 61 72 73 65 2c 54  r.yy346,pParse,T
30f4e 4b 5f 4e 4f 54 4e 55 4c 4c 2c 26 79 79 6d 73 70  K_NOTNULL,&yymsp
30f4f 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  [-2].minor.yy346
30f50 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
30f51 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  .yy0);}.        
30f52 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
30f53 65 20 32 31 37 3a 20 2f 2a 20 65 78 70 72 20 3a  e 217: /* expr :
30f54 3a 3d 20 65 78 70 72 20 49 53 20 4e 4f 54 20 4e  := expr IS NOT N
30f55 55 4c 4c 20 2a 2f 0a 7b 73 70 61 6e 55 6e 61 72  ULL */.{spanUnar
30f56 79 50 6f 73 74 66 69 78 28 26 79 79 67 6f 74 6f  yPostfix(&yygoto
30f57 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 70 50 61 72  minor.yy346,pPar
30f58 73 65 2c 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 26 79  se,TK_NOTNULL,&y
30f59 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
30f5a 79 33 34 36 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  y346,&yymsp[0].m
30f5b 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20  inor.yy0);}.    
30f5c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
30f5d 20 63 61 73 65 20 32 31 38 3a 20 2f 2a 20 65 78   case 218: /* ex
30f5e 70 72 20 3a 3a 3d 20 4e 4f 54 20 65 78 70 72 20  pr ::= NOT expr 
30f5f 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 31  */.      case 21
30f60 39 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 42  9: /* expr ::= B
30f61 49 54 4e 4f 54 20 65 78 70 72 20 2a 2f 20 79 79  ITNOT expr */ yy
30f62 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
30f63 6f 3d 3d 32 31 39 29 3b 0a 7b 73 70 61 6e 55 6e  o==219);.{spanUn
30f64 61 72 79 50 72 65 66 69 78 28 26 79 79 67 6f 74  aryPrefix(&yygot
30f65 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 70 50 61  ominor.yy346,pPa
30f66 72 73 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 61  rse,yymsp[-1].ma
30f67 6a 6f 72 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  jor,&yymsp[0].mi
30f68 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70  nor.yy346,&yymsp
30f69 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-1].minor.yy0);
30f6a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
30f6b 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 30 3a  .      case 220:
30f6c 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 4d 49 4e   /* expr ::= MIN
30f6d 55 53 20 65 78 70 72 20 2a 2f 0a 7b 73 70 61 6e  US expr */.{span
30f6e 55 6e 61 72 79 50 72 65 66 69 78 28 26 79 79 67  UnaryPrefix(&yyg
30f6f 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 70  otominor.yy346,p
30f70 50 61 72 73 65 2c 54 4b 5f 55 4d 49 4e 55 53 2c  Parse,TK_UMINUS,
30f71 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
30f72 79 79 33 34 36 2c 26 79 79 6d 73 70 5b 2d 31 5d  yy346,&yymsp[-1]
30f73 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20  .minor.yy0);}.  
30f74 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
30f75 20 20 20 63 61 73 65 20 32 32 31 3a 20 2f 2a 20     case 221: /* 
30f76 65 78 70 72 20 3a 3a 3d 20 50 4c 55 53 20 65 78  expr ::= PLUS ex
30f77 70 72 20 2a 2f 0a 7b 73 70 61 6e 55 6e 61 72 79  pr */.{spanUnary
30f78 50 72 65 66 69 78 28 26 79 79 67 6f 74 6f 6d 69  Prefix(&yygotomi
30f79 6e 6f 72 2e 79 79 33 34 36 2c 70 50 61 72 73 65  nor.yy346,pParse
30f7a 2c 54 4b 5f 55 50 4c 55 53 2c 26 79 79 6d 73 70  ,TK_UPLUS,&yymsp
30f7b 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2c  [0].minor.yy346,
30f7c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
30f7d 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  .yy0);}.        
30f7e 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
30f7f 65 20 32 32 34 3a 20 2f 2a 20 65 78 70 72 20 3a  e 224: /* expr :
30f80 3a 3d 20 65 78 70 72 20 62 65 74 77 65 65 6e 5f  := expr between_
30f81 6f 70 20 65 78 70 72 20 41 4e 44 20 65 78 70 72  op expr AND expr
30f82 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 4c 69 73 74   */.{.  ExprList
30f83 20 2a 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65   *pList = sqlite
30f84 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
30f85 70 50 61 72 73 65 2c 30 2c 20 79 79 6d 73 70 5b  pParse,0, yymsp[
30f86 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  -2].minor.yy346.
30f87 70 45 78 70 72 29 3b 0a 20 20 70 4c 69 73 74 20  pExpr);.  pList 
30f88 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
30f89 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 70  tAppend(pParse,p
30f8a 4c 69 73 74 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  List, yymsp[0].m
30f8b 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
30f8c 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  );.  yygotominor
30f8d 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73  .yy346.pExpr = s
30f8e 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
30f8f 73 65 2c 20 54 4b 5f 42 45 54 57 45 45 4e 2c 20  se, TK_BETWEEN, 
30f90 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
30f91 79 79 33 34 36 2e 70 45 78 70 72 2c 20 30 2c 20  yy346.pExpr, 0, 
30f92 30 29 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f  0);.  if( yygoto
30f93 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
30f94 72 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d  r ){.    yygotom
30f95 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
30f96 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 70 4c 69 73  ->x.pList = pLis
30f97 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  t;.  }else{.    
30f98 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
30f99 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
30f9a 2c 20 70 4c 69 73 74 29 3b 0a 20 20 7d 20 0a 20  , pList);.  } . 
30f9b 20 69 66 28 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d   if( yymsp[-3].m
30f9c 69 6e 6f 72 2e 79 79 33 32 38 20 29 20 79 79 67  inor.yy328 ) yyg
30f9d 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
30f9e 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45  Expr = sqlite3PE
30f9f 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e  xpr(pParse, TK_N
30fa0 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  OT, yygotominor.
30fa1 79 79 33 34 36 2e 70 45 78 70 72 2c 20 30 2c 20  yy346.pExpr, 0, 
30fa2 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  0);.  yygotomino
30fa3 72 2e 79 79 33 34 36 2e 7a 53 74 61 72 74 20 3d  r.yy346.zStart =
30fa4 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72   yymsp[-4].minor
30fa5 2e 79 79 33 34 36 2e 7a 53 74 61 72 74 3b 0a 20  .yy346.zStart;. 
30fa6 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
30fa7 34 36 2e 7a 45 6e 64 20 3d 20 79 79 6d 73 70 5b  46.zEnd = yymsp[
30fa8 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a  0].minor.yy346.z
30fa9 45 6e 64 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  End;.}.        b
30faa 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
30fab 20 32 32 37 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   227: /* expr ::
30fac 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20 4c 50 20  = expr in_op LP 
30fad 65 78 70 72 6c 69 73 74 20 52 50 20 2a 2f 0a 7b  exprlist RP */.{
30fae 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
30faf 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73  .yy346.pExpr = s
30fb0 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
30fb1 73 65 2c 20 54 4b 5f 49 4e 2c 20 79 79 6d 73 70  se, TK_IN, yymsp
30fb2 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  [-4].minor.yy346
30fb3 2e 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20  .pExpr, 0, 0);. 
30fb4 20 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e     if( yygotomin
30fb5 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29  or.yy346.pExpr )
30fb6 7b 0a 20 20 20 20 20 20 79 79 67 6f 74 6f 6d 69  {.      yygotomi
30fb7 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2d  nor.yy346.pExpr-
30fb8 3e 78 2e 70 4c 69 73 74 20 3d 20 79 79 6d 73 70  >x.pList = yymsp
30fb9 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 3b  [-1].minor.yy14;
30fba 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
30fbb 70 72 53 65 74 48 65 69 67 68 74 28 70 50 61 72  prSetHeight(pPar
30fbc 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  se, yygotominor.
30fbd 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20  yy346.pExpr);.  
30fbe 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
30fbf 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
30fc0 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
30fc1 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
30fc2 2e 79 79 31 34 29 3b 0a 20 20 20 20 7d 0a 20 20  .yy14);.    }.  
30fc3 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 33 5d 2e    if( yymsp[-3].
30fc4 6d 69 6e 6f 72 2e 79 79 33 32 38 20 29 20 79 79  minor.yy328 ) yy
30fc5 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
30fc6 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50  pExpr = sqlite3P
30fc7 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
30fc8 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  NOT, yygotominor
30fc9 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20 30 2c  .yy346.pExpr, 0,
30fca 20 30 29 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d   0);.    yygotom
30fcb 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72  inor.yy346.zStar
30fcc 74 20 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  t = yymsp[-4].mi
30fcd 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72 74  nor.yy346.zStart
30fce 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  ;.    yygotomino
30fcf 72 2e 79 79 33 34 36 2e 7a 45 6e 64 20 3d 20 26  r.yy346.zEnd = &
30fd0 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
30fd1 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69  y0.z[yymsp[0].mi
30fd2 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a 20 20 7d 0a  nor.yy0.n];.  }.
30fd3 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
30fd4 20 20 20 20 20 63 61 73 65 20 32 32 38 3a 20 2f       case 228: /
30fd5 2a 20 65 78 70 72 20 3a 3a 3d 20 4c 50 20 73 65  * expr ::= LP se
30fd6 6c 65 63 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 20  lect RP */.{.   
30fd7 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
30fd8 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74  46.pExpr = sqlit
30fd9 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
30fda 54 4b 5f 53 45 4c 45 43 54 2c 20 30 2c 20 30 2c  TK_SELECT, 0, 0,
30fdb 20 30 29 3b 0a 20 20 20 20 69 66 28 20 79 79 67   0);.    if( yyg
30fdc 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
30fdd 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 79 79  Expr ){.      yy
30fde 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
30fdf 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74  pExpr->x.pSelect
30fe0 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e   = yymsp[-1].min
30fe1 6f 72 2e 79 79 33 3b 0a 20 20 20 20 20 20 45 78  or.yy3;.      Ex
30fe2 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 79 79  prSetProperty(yy
30fe3 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
30fe4 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
30fe5 65 63 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ect);.      sqli
30fe6 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74  te3ExprSetHeight
30fe7 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d  (pParse, yygotom
30fe8 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
30fe9 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
30fea 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
30feb 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  tDelete(pParse->
30fec 64 62 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  db, yymsp[-1].mi
30fed 6e 6f 72 2e 79 79 33 29 3b 0a 20 20 20 20 7d 0a  nor.yy3);.    }.
30fee 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
30fef 79 79 33 34 36 2e 7a 53 74 61 72 74 20 3d 20 79  yy346.zStart = y
30ff0 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
30ff1 79 30 2e 7a 3b 0a 20 20 20 20 79 79 67 6f 74 6f  y0.z;.    yygoto
30ff2 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64  minor.yy346.zEnd
30ff3 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e   = &yymsp[0].min
30ff4 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30  or.yy0.z[yymsp[0
30ff5 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a  ].minor.yy0.n];.
30ff6 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61    }.        brea
30ff7 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 32  k;.      case 22
30ff8 39 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65  9: /* expr ::= e
30ff9 78 70 72 20 69 6e 5f 6f 70 20 4c 50 20 73 65 6c  xpr in_op LP sel
30ffa 65 63 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 20 20  ect RP */.{.    
30ffb 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
30ffc 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65  6.pExpr = sqlite
30ffd 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
30ffe 4b 5f 49 4e 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e  K_IN, yymsp[-4].
30fff 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
31000 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66  r, 0, 0);.    if
31001 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
31002 33 34 36 2e 70 45 78 70 72 20 29 7b 0a 20 20 20  346.pExpr ){.   
31003 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
31004 79 33 34 36 2e 70 45 78 70 72 2d 3e 78 2e 70 53  y346.pExpr->x.pS
31005 65 6c 65 63 74 20 3d 20 79 79 6d 73 70 5b 2d 31  elect = yymsp[-1
31006 5d 2e 6d 69 6e 6f 72 2e 79 79 33 3b 0a 20 20 20  ].minor.yy3;.   
31007 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72     ExprSetProper
31008 74 79 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  ty(yygotominor.y
31009 79 33 34 36 2e 70 45 78 70 72 2c 20 45 50 5f 78  y346.pExpr, EP_x
3100a 49 73 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20  IsSelect);.     
3100b 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48   sqlite3ExprSetH
3100c 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 79 79  eight(pParse, yy
3100d 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
3100e 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 65 6c 73  pExpr);.    }els
3100f 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
31010 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 50 61  SelectDelete(pPa
31011 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d  rse->db, yymsp[-
31012 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29 3b 0a 20  1].minor.yy3);. 
31013 20 20 20 7d 0a 20 20 20 20 69 66 28 20 79 79 6d     }.    if( yym
31014 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-3].minor.yy3
31015 32 38 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  28 ) yygotominor
31016 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73  .yy346.pExpr = s
31017 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
31018 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f  se, TK_NOT, yygo
31019 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
3101a 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  xpr, 0, 0);.    
3101b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3101c 36 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70  6.zStart = yymsp
3101d 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  [-4].minor.yy346
3101e 2e 7a 53 74 61 72 74 3b 0a 20 20 20 20 79 79 67  .zStart;.    yyg
3101f 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a  otominor.yy346.z
31020 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e  End = &yymsp[0].
31021 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73  minor.yy0.z[yyms
31022 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e  p[0].minor.yy0.n
31023 5d 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62  ];.  }.        b
31024 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31025 20 32 33 30 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   230: /* expr ::
31026 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20 6e 6d 20  = expr in_op nm 
31027 64 62 6e 6d 20 2a 2f 0a 7b 0a 20 20 20 20 53 72  dbnm */.{.    Sr
31028 63 4c 69 73 74 20 2a 70 53 72 63 20 3d 20 73 71  cList *pSrc = sq
31029 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
3102a 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 30  nd(pParse->db, 0
3102b 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  ,&yymsp[-1].mino
3102c 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  r.yy0,&yymsp[0].
3102d 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 20 20  minor.yy0);.    
3102e 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3102f 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65  6.pExpr = sqlite
31030 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
31031 4b 5f 49 4e 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e  K_IN, yymsp[-3].
31032 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
31033 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66  r, 0, 0);.    if
31034 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
31035 33 34 36 2e 70 45 78 70 72 20 29 7b 0a 20 20 20  346.pExpr ){.   
31036 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
31037 79 33 34 36 2e 70 45 78 70 72 2d 3e 78 2e 70 53  y346.pExpr->x.pS
31038 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53  elect = sqlite3S
31039 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c  electNew(pParse,
3103a 20 30 2c 70 53 72 63 2c 30 2c 30 2c 30 2c 30 2c   0,pSrc,0,0,0,0,
3103b 30 2c 30 2c 30 29 3b 0a 20 20 20 20 20 20 45 78  0,0,0);.      Ex
3103c 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 79 79  prSetProperty(yy
3103d 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
3103e 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
3103f 65 63 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ect);.      sqli
31040 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74  te3ExprSetHeight
31041 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d  (pParse, yygotom
31042 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
31043 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
31044 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69      sqlite3SrcLi
31045 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  stDelete(pParse-
31046 3e 64 62 2c 20 70 53 72 63 29 3b 0a 20 20 20 20  >db, pSrc);.    
31047 7d 0a 20 20 20 20 69 66 28 20 79 79 6d 73 70 5b  }.    if( yymsp[
31048 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 20  -2].minor.yy328 
31049 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ) yygotominor.yy
3104a 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69  346.pExpr = sqli
3104b 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
3104c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d   TK_NOT, yygotom
3104d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
3104e 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 79 79 67  , 0, 0);.    yyg
3104f 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a  otominor.yy346.z
31050 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 33  Start = yymsp[-3
31051 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53  ].minor.yy346.zS
31052 74 61 72 74 3b 0a 20 20 20 20 79 79 67 6f 74 6f  tart;.    yygoto
31053 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64  minor.yy346.zEnd
31054 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
31055 72 2e 79 79 30 2e 7a 20 3f 20 26 79 79 6d 73 70  r.yy0.z ? &yymsp
31056 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b  [0].minor.yy0.z[
31057 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
31058 79 30 2e 6e 5d 20 3a 20 26 79 79 6d 73 70 5b 2d  y0.n] : &yymsp[-
31059 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79  1].minor.yy0.z[y
3105a 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3105b 79 30 2e 6e 5d 3b 0a 20 20 7d 0a 20 20 20 20 20  y0.n];.  }.     
3105c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3105d 63 61 73 65 20 32 33 31 3a 20 2f 2a 20 65 78 70  case 231: /* exp
3105e 72 20 3a 3a 3d 20 45 58 49 53 54 53 20 4c 50 20  r ::= EXISTS LP 
3105f 73 65 6c 65 63 74 20 52 50 20 2a 2f 0a 7b 0a 20  select RP */.{. 
31060 20 20 20 45 78 70 72 20 2a 70 20 3d 20 79 79 67     Expr *p = yyg
31061 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
31062 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45  Expr = sqlite3PE
31063 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 45  xpr(pParse, TK_E
31064 58 49 53 54 53 2c 20 30 2c 20 30 2c 20 30 29 3b  XISTS, 0, 0, 0);
31065 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20  .    if( p ){.  
31066 20 20 20 20 70 2d 3e 78 2e 70 53 65 6c 65 63 74      p->x.pSelect
31067 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e   = yymsp[-1].min
31068 6f 72 2e 79 79 33 3b 0a 20 20 20 20 20 20 45 78  or.yy3;.      Ex
31069 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 2c  prSetProperty(p,
3106a 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 3b 0a   EP_xIsSelect);.
3106b 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
3106c 72 53 65 74 48 65 69 67 68 74 28 70 50 61 72 73  rSetHeight(pPars
3106d 65 2c 20 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65  e, p);.    }else
3106e 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  {.      sqlite3S
3106f 65 6c 65 63 74 44 65 6c 65 74 65 28 70 50 61 72  electDelete(pPar
31070 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d 31  se->db, yymsp[-1
31071 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29 3b 0a 20 20  ].minor.yy3);.  
31072 20 20 7d 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69    }.    yygotomi
31073 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72 74  nor.yy346.zStart
31074 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e   = yymsp[-3].min
31075 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 20 20 79 79  or.yy0.z;.    yy
31076 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
31077 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d  zEnd = &yymsp[0]
31078 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d  .minor.yy0.z[yym
31079 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
3107a 6e 5d 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20 20  n];.  }.        
3107b 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3107c 65 20 32 33 32 3a 20 2f 2a 20 65 78 70 72 20 3a  e 232: /* expr :
3107d 3a 3d 20 43 41 53 45 20 63 61 73 65 5f 6f 70 65  := CASE case_ope
3107e 72 61 6e 64 20 63 61 73 65 5f 65 78 70 72 6c 69  rand case_exprli
3107f 73 74 20 63 61 73 65 5f 65 6c 73 65 20 45 4e 44  st case_else END
31080 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69   */.{.  yygotomi
31081 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20  nor.yy346.pExpr 
31082 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
31083 50 61 72 73 65 2c 20 54 4b 5f 43 41 53 45 2c 20  Parse, TK_CASE, 
31084 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
31085 79 79 31 33 32 2c 20 79 79 6d 73 70 5b 2d 31 5d  yy132, yymsp[-1]
31086 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c 20 30 29  .minor.yy132, 0)
31087 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69  ;.  if( yygotomi
31088 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20  nor.yy346.pExpr 
31089 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  ){.    yygotomin
3108a 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e  or.yy346.pExpr->
3108b 78 2e 70 4c 69 73 74 20 3d 20 79 79 6d 73 70 5b  x.pList = yymsp[
3108c 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 3b 0a  -2].minor.yy14;.
3108d 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53      sqlite3ExprS
3108e 65 74 48 65 69 67 68 74 28 70 50 61 72 73 65 2c  etHeight(pParse,
3108f 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
31090 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 7d 65 6c  46.pExpr);.  }el
31091 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  se{.    sqlite3E
31092 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50  xprListDelete(pP
31093 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b  arse->db, yymsp[
31094 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 29 3b  -2].minor.yy14);
31095 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  .  }.  yygotomin
31096 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72 74 20  or.yy346.zStart 
31097 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  = yymsp[-4].mino
31098 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79 67 6f 74  r.yy0.z;.  yygot
31099 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e  ominor.yy346.zEn
3109a 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  d = &yymsp[0].mi
3109b 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b  nor.yy0.z[yymsp[
3109c 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b  0].minor.yy0.n];
3109d 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
3109e 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 33  ;.      case 233
3109f 3a 20 2f 2a 20 63 61 73 65 5f 65 78 70 72 6c 69  : /* case_exprli
310a0 73 74 20 3a 3a 3d 20 63 61 73 65 5f 65 78 70 72  st ::= case_expr
310a1 6c 69 73 74 20 57 48 45 4e 20 65 78 70 72 20 54  list WHEN expr T
310a2 48 45 4e 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20  HEN expr */.{.  
310a3 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34  yygotominor.yy14
310a4 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
310a5 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
310a6 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
310a7 79 79 31 34 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e  yy14, yymsp[-2].
310a8 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
310a9 72 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  r);.  yygotomino
310aa 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33  r.yy14 = sqlite3
310ab 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
310ac 50 61 72 73 65 2c 79 79 67 6f 74 6f 6d 69 6e 6f  Parse,yygotomino
310ad 72 2e 79 79 31 34 2c 20 79 79 6d 73 70 5b 30 5d  r.yy14, yymsp[0]
310ae 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  .minor.yy346.pEx
310af 70 72 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  pr);.}.        b
310b0 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
310b1 20 32 33 34 3a 20 2f 2a 20 63 61 73 65 5f 65 78   234: /* case_ex
310b2 70 72 6c 69 73 74 20 3a 3a 3d 20 57 48 45 4e 20  prlist ::= WHEN 
310b3 65 78 70 72 20 54 48 45 4e 20 65 78 70 72 20 2a  expr THEN expr *
310b4 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
310b5 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33  r.yy14 = sqlite3
310b6 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
310b7 50 61 72 73 65 2c 30 2c 20 79 79 6d 73 70 5b 2d  Parse,0, yymsp[-
310b8 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  2].minor.yy346.p
310b9 45 78 70 72 29 3b 0a 20 20 79 79 67 6f 74 6f 6d  Expr);.  yygotom
310ba 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69  inor.yy14 = sqli
310bb 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
310bc 64 28 70 50 61 72 73 65 2c 79 79 67 6f 74 6f 6d  d(pParse,yygotom
310bd 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d 73 70  inor.yy14, yymsp
310be 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  [0].minor.yy346.
310bf 70 45 78 70 72 29 3b 0a 7d 0a 20 20 20 20 20 20  pExpr);.}.      
310c0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
310c1 61 73 65 20 32 34 33 3a 20 2f 2a 20 63 6d 64 20  ase 243: /* cmd 
310c2 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 75 6e 69  ::= createkw uni
310c3 71 75 65 66 6c 61 67 20 49 4e 44 45 58 20 69 66  queflag INDEX if
310c4 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e  notexists nm dbn
310c5 6d 20 4f 4e 20 6e 6d 20 4c 50 20 69 64 78 6c 69  m ON nm LP idxli
310c6 73 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 73 71 6c  st RP */.{.  sql
310c7 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 28  ite3CreateIndex(
310c8 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d  pParse, &yymsp[-
310c9 36 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79  6].minor.yy0, &y
310ca 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-5].minor.y
310cb 79 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  y0, .           
310cc 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
310cd 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 70  3SrcListAppend(p
310ce 50 61 72 73 65 2d 3e 64 62 2c 30 2c 26 79 79 6d  Parse->db,0,&yym
310cf 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
310d0 2c 30 29 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  ,0), yymsp[-1].m
310d1 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d 73 70  inor.yy14, yymsp
310d2 5b 2d 39 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  [-9].minor.yy328
310d3 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
310d4 20 20 20 20 20 20 20 20 26 79 79 6d 73 70 5b 2d          &yymsp[-
310d5 31 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26  10].minor.yy0, &
310d6 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
310d7 79 30 2c 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53  y0, SQLITE_SO_AS
310d8 43 2c 20 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e  C, yymsp[-7].min
310d9 6f 72 2e 79 79 33 32 38 29 3b 0a 7d 0a 20 20 20  or.yy328);.}.   
310da 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
310db 20 20 63 61 73 65 20 32 34 34 3a 20 2f 2a 20 75    case 244: /* u
310dc 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d 20 55 4e  niqueflag ::= UN
310dd 49 51 55 45 20 2a 2f 0a 20 20 20 20 20 20 63 61  IQUE */.      ca
310de 73 65 20 32 39 38 3a 20 2f 2a 20 72 61 69 73 65  se 298: /* raise
310df 74 79 70 65 20 3a 3a 3d 20 41 42 4f 52 54 20 2a  type ::= ABORT *
310e0 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
310e1 75 6c 65 6e 6f 3d 3d 32 39 38 29 3b 0a 7b 79 79  uleno==298);.{yy
310e2 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20  gotominor.yy328 
310e3 3d 20 4f 45 5f 41 62 6f 72 74 3b 7d 0a 20 20 20  = OE_Abort;}.   
310e4 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
310e5 20 20 63 61 73 65 20 32 34 35 3a 20 2f 2a 20 75    case 245: /* u
310e6 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d 20 2a 2f  niqueflag ::= */
310e7 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
310e8 33 32 38 20 3d 20 4f 45 5f 4e 6f 6e 65 3b 7d 0a  328 = OE_None;}.
310e9 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
310ea 20 20 20 20 20 63 61 73 65 20 32 34 38 3a 20 2f       case 248: /
310eb 2a 20 69 64 78 6c 69 73 74 20 3a 3a 3d 20 69 64  * idxlist ::= id
310ec 78 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 20 63  xlist COMMA nm c
310ed 6f 6c 6c 61 74 65 20 73 6f 72 74 6f 72 64 65 72  ollate sortorder
310ee 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 70 20   */.{.  Expr *p 
310ef 3d 20 30 3b 0a 20 20 69 66 28 20 79 79 6d 73 70  = 0;.  if( yymsp
310f0 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e  [-1].minor.yy0.n
310f1 3e 30 20 29 7b 0a 20 20 20 20 70 20 3d 20 73 71  >0 ){.    p = sq
310f2 6c 69 74 65 33 45 78 70 72 28 70 50 61 72 73 65  lite3Expr(pParse
310f3 2d 3e 64 62 2c 20 54 4b 5f 43 4f 4c 55 4d 4e 2c  ->db, TK_COLUMN,
310f4 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
310f5 45 78 70 72 53 65 74 43 6f 6c 6c 28 70 50 61 72  ExprSetColl(pPar
310f6 73 65 2c 20 70 2c 20 26 79 79 6d 73 70 5b 2d 31  se, p, &yymsp[-1
310f7 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20  ].minor.yy0);.  
310f8 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  }.  yygotominor.
310f9 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78  yy14 = sqlite3Ex
310fa 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
310fb 72 73 65 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  rse,yymsp[-4].mi
310fc 6e 6f 72 2e 79 79 31 34 2c 20 70 29 3b 0a 20 20  nor.yy14, p);.  
310fd 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53  sqlite3ExprListS
310fe 65 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 79 79  etName(pParse,yy
310ff 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 26  gotominor.yy14,&
31100 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
31101 79 79 30 2c 31 29 3b 0a 20 20 73 71 6c 69 74 65  yy0,1);.  sqlite
31102 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65  3ExprListCheckLe
31103 6e 67 74 68 28 70 50 61 72 73 65 2c 20 79 79 67  ngth(pParse, yyg
31104 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 22  otominor.yy14, "
31105 69 6e 64 65 78 22 29 3b 0a 20 20 69 66 28 20 79  index");.  if( y
31106 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20  ygotominor.yy14 
31107 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ) yygotominor.yy
31108 31 34 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f  14->a[yygotomino
31109 72 2e 79 79 31 34 2d 3e 6e 45 78 70 72 2d 31 5d  r.yy14->nExpr-1]
3110a 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38  .sortOrder = (u8
3110b 29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  )yymsp[0].minor.
3110c 79 79 33 32 38 3b 0a 7d 0a 20 20 20 20 20 20 20  yy328;.}.       
3110d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3110e 73 65 20 32 34 39 3a 20 2f 2a 20 69 64 78 6c 69  se 249: /* idxli
3110f 73 74 20 3a 3a 3d 20 6e 6d 20 63 6f 6c 6c 61 74  st ::= nm collat
31110 65 20 73 6f 72 74 6f 72 64 65 72 20 2a 2f 0a 7b  e sortorder */.{
31111 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 30 3b 0a  .  Expr *p = 0;.
31112 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 31 5d 2e    if( yymsp[-1].
31113 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3e 30 20 29 7b  minor.yy0.n>0 ){
31114 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
31115 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
31116 5f 43 4f 4c 55 4d 4e 2c 20 30 2c 20 30 2c 20 30  _COLUMN, 0, 0, 0
31117 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
31118 70 72 53 65 74 43 6f 6c 6c 28 70 50 61 72 73 65  prSetColl(pParse
31119 2c 20 70 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e  , p, &yymsp[-1].
3111a 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d 0a  minor.yy0);.  }.
3111b 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3111c 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  14 = sqlite3Expr
3111d 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
3111e 65 2c 30 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74  e,0, p);.  sqlit
3111f 65 33 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d  e3ExprListSetNam
31120 65 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f  e(pParse, yygoto
31121 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 26 79 79 6d  minor.yy14, &yym
31122 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
31123 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  , 1);.  sqlite3E
31124 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67  xprListCheckLeng
31125 74 68 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74  th(pParse, yygot
31126 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 22 69 6e  ominor.yy14, "in
31127 64 65 78 22 29 3b 0a 20 20 69 66 28 20 79 79 67  dex");.  if( yyg
31128 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 29 20  otominor.yy14 ) 
31129 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34  yygotominor.yy14
3112a 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ->a[yygotominor.
3112b 79 79 31 34 2d 3e 6e 45 78 70 72 2d 31 5d 2e 73  yy14->nExpr-1].s
3112c 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 29 79  ortOrder = (u8)y
3112d 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3112e 33 32 38 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  328;.}.        b
3112f 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31130 20 32 35 30 3a 20 2f 2a 20 63 6f 6c 6c 61 74 65   250: /* collate
31131 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d   ::= */.{yygotom
31132 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20 30 3b 20  inor.yy0.z = 0; 
31133 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e  yygotominor.yy0.
31134 6e 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20  n = 0;}.        
31135 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
31136 65 20 32 35 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 252: /* cmd ::
31137 3d 20 44 52 4f 50 20 49 4e 44 45 58 20 69 66 65  = DROP INDEX ife
31138 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 20 2a  xists fullname *
31139 2f 0a 7b 73 71 6c 69 74 65 33 44 72 6f 70 49 6e  /.{sqlite3DropIn
3113a 64 65 78 28 70 50 61 72 73 65 2c 20 79 79 6d 73  dex(pParse, yyms
3113b 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c  p[0].minor.yy65,
3113c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
3113d 2e 79 79 33 32 38 29 3b 7d 0a 20 20 20 20 20 20  .yy328);}.      
3113e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3113f 61 73 65 20 32 35 33 3a 20 2f 2a 20 63 6d 64 20  ase 253: /* cmd 
31140 3a 3a 3d 20 56 41 43 55 55 4d 20 2a 2f 0a 20 20  ::= VACUUM */.  
31141 20 20 20 20 63 61 73 65 20 32 35 34 3a 20 2f 2a      case 254: /*
31142 20 63 6d 64 20 3a 3a 3d 20 56 41 43 55 55 4d 20   cmd ::= VACUUM 
31143 6e 6d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  nm */ yytestcase
31144 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 35 34 29 3b  (yyruleno==254);
31145 0a 7b 73 71 6c 69 74 65 33 56 61 63 75 75 6d 28  .{sqlite3Vacuum(
31146 70 50 61 72 73 65 29 3b 7d 0a 20 20 20 20 20 20  pParse);}.      
31147 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31148 61 73 65 20 32 35 35 3a 20 2f 2a 20 63 6d 64 20  ase 255: /* cmd 
31149 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62  ::= PRAGMA nm db
3114a 6e 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 50 72  nm */.{sqlite3Pr
3114b 61 67 6d 61 28 70 50 61 72 73 65 2c 26 79 79 6d  agma(pParse,&yym
3114c 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
3114d 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
3114e 2e 79 79 30 2c 30 2c 30 29 3b 7d 0a 20 20 20 20  .yy0,0,0);}.    
3114f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
31150 20 63 61 73 65 20 32 35 36 3a 20 2f 2a 20 63 6d   case 256: /* cm
31151 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20  d ::= PRAGMA nm 
31152 64 62 6e 6d 20 45 51 20 6e 6d 6e 75 6d 20 2a 2f  dbnm EQ nmnum */
31153 0a 7b 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28  .{sqlite3Pragma(
31154 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 33  pParse,&yymsp[-3
31155 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d  ].minor.yy0,&yym
31156 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
31157 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
31158 2e 79 79 30 2c 30 29 3b 7d 0a 20 20 20 20 20 20  .yy0,0);}.      
31159 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3115a 61 73 65 20 32 35 37 3a 20 2f 2a 20 63 6d 64 20  ase 257: /* cmd 
3115b 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62  ::= PRAGMA nm db
3115c 6e 6d 20 4c 50 20 6e 6d 6e 75 6d 20 52 50 20 2a  nm LP nmnum RP *
3115d 2f 0a 7b 73 71 6c 69 74 65 33 50 72 61 67 6d 61  /.{sqlite3Pragma
3115e 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d  (pParse,&yymsp[-
3115f 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  4].minor.yy0,&yy
31160 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
31161 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  0,&yymsp[-1].min
31162 6f 72 2e 79 79 30 2c 30 29 3b 7d 0a 20 20 20 20  or.yy0,0);}.    
31163 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
31164 20 63 61 73 65 20 32 35 38 3a 20 2f 2a 20 63 6d   case 258: /* cm
31165 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20  d ::= PRAGMA nm 
31166 64 62 6e 6d 20 45 51 20 6d 69 6e 75 73 5f 6e 75  dbnm EQ minus_nu
31167 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 50 72 61  m */.{sqlite3Pra
31168 67 6d 61 28 70 50 61 72 73 65 2c 26 79 79 6d 73  gma(pParse,&yyms
31169 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-3].minor.yy0,
3116a 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
3116b 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  .yy0,&yymsp[0].m
3116c 69 6e 6f 72 2e 79 79 30 2c 31 29 3b 7d 0a 20 20  inor.yy0,1);}.  
3116d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3116e 20 20 20 63 61 73 65 20 32 35 39 3a 20 2f 2a 20     case 259: /* 
3116f 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e  cmd ::= PRAGMA n
31170 6d 20 64 62 6e 6d 20 4c 50 20 6d 69 6e 75 73 5f  m dbnm LP minus_
31171 6e 75 6d 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74  num RP */.{sqlit
31172 65 33 50 72 61 67 6d 61 28 70 50 61 72 73 65 2c  e3Pragma(pParse,
31173 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  &yymsp[-4].minor
31174 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e  .yy0,&yymsp[-3].
31175 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
31176 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 31  [-1].minor.yy0,1
31177 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
31178 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 37  k;.      case 27
31179 30 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 63 72  0: /* cmd ::= cr
3117a 65 61 74 65 6b 77 20 74 72 69 67 67 65 72 5f 64  eatekw trigger_d
3117b 65 63 6c 20 42 45 47 49 4e 20 74 72 69 67 67 65  ecl BEGIN trigge
3117c 72 5f 63 6d 64 5f 6c 69 73 74 20 45 4e 44 20 2a  r_cmd_list END *
3117d 2f 0a 7b 0a 20 20 54 6f 6b 65 6e 20 61 6c 6c 3b  /.{.  Token all;
3117e 0a 20 20 61 6c 6c 2e 7a 20 3d 20 79 79 6d 73 70  .  all.z = yymsp
3117f 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  [-3].minor.yy0.z
31180 3b 0a 20 20 61 6c 6c 2e 6e 20 3d 20 28 69 6e 74  ;.  all.n = (int
31181 29 28 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  )(yymsp[0].minor
31182 2e 79 79 30 2e 7a 20 2d 20 79 79 6d 73 70 5b 2d  .yy0.z - yymsp[-
31183 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 29 20  3].minor.yy0.z) 
31184 2b 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  + yymsp[0].minor
31185 2e 79 79 30 2e 6e 3b 0a 20 20 73 71 6c 69 74 65  .yy0.n;.  sqlite
31186 33 46 69 6e 69 73 68 54 72 69 67 67 65 72 28 70  3FinishTrigger(p
31187 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 31 5d  Parse, yymsp[-1]
31188 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 2c 20 26 61  .minor.yy473, &a
31189 6c 6c 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  ll);.}.        b
3118a 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
3118b 20 32 37 31 3a 20 2f 2a 20 74 72 69 67 67 65 72   271: /* trigger
3118c 5f 64 65 63 6c 20 3a 3a 3d 20 74 65 6d 70 20 54  _decl ::= temp T
3118d 52 49 47 47 45 52 20 69 66 6e 6f 74 65 78 69 73  RIGGER ifnotexis
3118e 74 73 20 6e 6d 20 64 62 6e 6d 20 74 72 69 67 67  ts nm dbnm trigg
3118f 65 72 5f 74 69 6d 65 20 74 72 69 67 67 65 72 5f  er_time trigger_
31190 65 76 65 6e 74 20 4f 4e 20 66 75 6c 6c 6e 61 6d  event ON fullnam
31191 65 20 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65  e foreach_clause
31192 20 77 68 65 6e 5f 63 6c 61 75 73 65 20 2a 2f 0a   when_clause */.
31193 7b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e  {.  sqlite3Begin
31194 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c 20  Trigger(pParse, 
31195 26 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72  &yymsp[-7].minor
31196 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 2d 36 5d  .yy0, &yymsp[-6]
31197 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73  .minor.yy0, yyms
31198 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  p[-5].minor.yy32
31199 38 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  8, yymsp[-4].min
3119a 6f 72 2e 79 79 33 37 38 2e 61 2c 20 79 79 6d 73  or.yy378.a, yyms
3119b 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 37  p[-4].minor.yy37
3119c 38 2e 62 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d  8.b, yymsp[-2].m
3119d 69 6e 6f 72 2e 79 79 36 35 2c 20 79 79 6d 73 70  inor.yy65, yymsp
3119e 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c  [0].minor.yy132,
3119f 20 79 79 6d 73 70 5b 2d 31 30 5d 2e 6d 69 6e 6f   yymsp[-10].mino
311a0 72 2e 79 79 33 32 38 2c 20 79 79 6d 73 70 5b 2d  r.yy328, yymsp[-
311a1 38 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b  8].minor.yy328);
311a2 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
311a3 79 30 20 3d 20 28 79 79 6d 73 70 5b 2d 36 5d 2e  y0 = (yymsp[-6].
311a4 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 3d 30 3f 79  minor.yy0.n==0?y
311a5 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-7].minor.y
311a6 79 30 3a 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e  y0:yymsp[-6].min
311a7 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20  or.yy0);.}.     
311a8 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
311a9 63 61 73 65 20 32 37 32 3a 20 2f 2a 20 74 72 69  case 272: /* tri
311aa 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 42 45  gger_time ::= BE
311ab 46 4f 52 45 20 2a 2f 0a 20 20 20 20 20 20 63 61  FORE */.      ca
311ac 73 65 20 32 37 35 3a 20 2f 2a 20 74 72 69 67 67  se 275: /* trigg
311ad 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 2a 2f 20 79  er_time ::= */ y
311ae 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
311af 6e 6f 3d 3d 32 37 35 29 3b 0a 7b 20 79 79 67 6f  no==275);.{ yygo
311b0 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20  tominor.yy328 = 
311b1 54 4b 5f 42 45 46 4f 52 45 3b 20 7d 0a 20 20 20  TK_BEFORE; }.   
311b2 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
311b3 20 20 63 61 73 65 20 32 37 33 3a 20 2f 2a 20 74    case 273: /* t
311b4 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20  rigger_time ::= 
311b5 41 46 54 45 52 20 2a 2f 0a 7b 20 79 79 67 6f 74  AFTER */.{ yygot
311b6 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 54  ominor.yy328 = T
311b7 4b 5f 41 46 54 45 52 3b 20 20 7d 0a 20 20 20 20  K_AFTER;  }.    
311b8 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
311b9 20 63 61 73 65 20 32 37 34 3a 20 2f 2a 20 74 72   case 274: /* tr
311ba 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 49  igger_time ::= I
311bb 4e 53 54 45 41 44 20 4f 46 20 2a 2f 0a 7b 20 79  NSTEAD OF */.{ y
311bc 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38  ygotominor.yy328
311bd 20 3d 20 54 4b 5f 49 4e 53 54 45 41 44 3b 7d 0a   = TK_INSTEAD;}.
311be 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
311bf 20 20 20 20 20 63 61 73 65 20 32 37 36 3a 20 2f       case 276: /
311c0 2a 20 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20  * trigger_event 
311c1 3a 3a 3d 20 44 45 4c 45 54 45 7c 49 4e 53 45 52  ::= DELETE|INSER
311c2 54 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  T */.      case 
311c3 32 37 37 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  277: /* trigger_
311c4 65 76 65 6e 74 20 3a 3a 3d 20 55 50 44 41 54 45  event ::= UPDATE
311c5 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
311c6 79 72 75 6c 65 6e 6f 3d 3d 32 37 37 29 3b 0a 7b  yruleno==277);.{
311c7 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 37  yygotominor.yy37
311c8 38 2e 61 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  8.a = yymsp[0].m
311c9 61 6a 6f 72 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f  ajor; yygotomino
311ca 72 2e 79 79 33 37 38 2e 62 20 3d 20 30 3b 7d 0a  r.yy378.b = 0;}.
311cb 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
311cc 20 20 20 20 20 63 61 73 65 20 32 37 38 3a 20 2f       case 278: /
311cd 2a 20 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20  * trigger_event 
311ce 3a 3a 3d 20 55 50 44 41 54 45 20 4f 46 20 69 6e  ::= UPDATE OF in
311cf 73 63 6f 6c 6c 69 73 74 20 2a 2f 0a 7b 79 79 67  scollist */.{yyg
311d0 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 37 38 2e 61  otominor.yy378.a
311d1 20 3d 20 54 4b 5f 55 50 44 41 54 45 3b 20 79 79   = TK_UPDATE; yy
311d2 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 37 38 2e  gotominor.yy378.
311d3 62 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  b = yymsp[0].min
311d4 6f 72 2e 79 79 34 30 38 3b 7d 0a 20 20 20 20 20  or.yy408;}.     
311d5 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
311d6 63 61 73 65 20 32 38 31 3a 20 2f 2a 20 77 68 65  case 281: /* whe
311d7 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 2a 2f 0a  n_clause ::= */.
311d8 20 20 20 20 20 20 63 61 73 65 20 33 30 33 3a 20        case 303: 
311d9 2f 2a 20 6b 65 79 5f 6f 70 74 20 3a 3a 3d 20 2a  /* key_opt ::= *
311da 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
311db 75 6c 65 6e 6f 3d 3d 33 30 33 29 3b 0a 7b 20 79  uleno==303);.{ y
311dc 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 33 32  ygotominor.yy132
311dd 20 3d 20 30 3b 20 7d 0a 20 20 20 20 20 20 20 20   = 0; }.        
311de 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
311df 65 20 32 38 32 3a 20 2f 2a 20 77 68 65 6e 5f 63  e 282: /* when_c
311e0 6c 61 75 73 65 20 3a 3a 3d 20 57 48 45 4e 20 65  lause ::= WHEN e
311e1 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  xpr */.      cas
311e2 65 20 33 30 34 3a 20 2f 2a 20 6b 65 79 5f 6f 70  e 304: /* key_op
311e3 74 20 3a 3a 3d 20 4b 45 59 20 65 78 70 72 20 2a  t ::= KEY expr *
311e4 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
311e5 75 6c 65 6e 6f 3d 3d 33 30 34 29 3b 0a 7b 20 79  uleno==304);.{ y
311e6 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 33 32  ygotominor.yy132
311e7 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
311e8 72 2e 79 79 33 34 36 2e 70 45 78 70 72 3b 20 7d  r.yy346.pExpr; }
311e9 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
311ea 20 20 20 20 20 20 63 61 73 65 20 32 38 33 3a 20        case 283: 
311eb 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c  /* trigger_cmd_l
311ec 69 73 74 20 3a 3a 3d 20 74 72 69 67 67 65 72 5f  ist ::= trigger_
311ed 63 6d 64 5f 6c 69 73 74 20 74 72 69 67 67 65 72  cmd_list trigger
311ee 5f 63 6d 64 20 53 45 4d 49 20 2a 2f 0a 7b 0a 20  _cmd SEMI */.{. 
311ef 20 61 73 73 65 72 74 28 20 79 79 6d 73 70 5b 2d   assert( yymsp[-
311f0 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 21 3d  2].minor.yy473!=
311f1 30 20 29 3b 0a 20 20 79 79 6d 73 70 5b 2d 32 5d  0 );.  yymsp[-2]
311f2 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 2d 3e 70 4c  .minor.yy473->pL
311f3 61 73 74 2d 3e 70 4e 65 78 74 20 3d 20 79 79 6d  ast->pNext = yym
311f4 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-1].minor.yy4
311f5 37 33 3b 0a 20 20 79 79 6d 73 70 5b 2d 32 5d 2e  73;.  yymsp[-2].
311f6 6d 69 6e 6f 72 2e 79 79 34 37 33 2d 3e 70 4c 61  minor.yy473->pLa
311f7 73 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  st = yymsp[-1].m
311f8 69 6e 6f 72 2e 79 79 34 37 33 3b 0a 20 20 79 79  inor.yy473;.  yy
311f9 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 33 20  gotominor.yy473 
311fa 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  = yymsp[-2].mino
311fb 72 2e 79 79 34 37 33 3b 0a 7d 0a 20 20 20 20 20  r.yy473;.}.     
311fc 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
311fd 63 61 73 65 20 32 38 34 3a 20 2f 2a 20 74 72 69  case 284: /* tri
311fe 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a  gger_cmd_list ::
311ff 3d 20 74 72 69 67 67 65 72 5f 63 6d 64 20 53 45  = trigger_cmd SE
31200 4d 49 20 2a 2f 0a 7b 20 0a 20 20 61 73 73 65 72  MI */.{ .  asser
31201 74 28 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  t( yymsp[-1].min
31202 6f 72 2e 79 79 34 37 33 21 3d 30 20 29 3b 0a 20  or.yy473!=0 );. 
31203 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
31204 2e 79 79 34 37 33 2d 3e 70 4c 61 73 74 20 3d 20  .yy473->pLast = 
31205 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
31206 79 79 34 37 33 3b 0a 20 20 79 79 67 6f 74 6f 6d  yy473;.  yygotom
31207 69 6e 6f 72 2e 79 79 34 37 33 20 3d 20 79 79 6d  inor.yy473 = yym
31208 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-1].minor.yy4
31209 37 33 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  73;.}.        br
3120a 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3120b 32 38 36 3a 20 2f 2a 20 74 72 6e 6d 20 3a 3a 3d  286: /* trnm ::=
3120c 20 6e 6d 20 44 4f 54 20 6e 6d 20 2a 2f 0a 7b 0a   nm DOT nm */.{.
3120d 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3120e 30 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0 = yymsp[0].min
3120f 6f 72 2e 79 79 30 3b 0a 20 20 73 71 6c 69 74 65  or.yy0;.  sqlite
31210 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
31211 2c 20 0a 20 20 20 20 20 20 20 20 22 71 75 61 6c  , .        "qual
31212 69 66 69 65 64 20 74 61 62 6c 65 20 6e 61 6d 65  ified table name
31213 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65  s are not allowe
31214 64 20 6f 6e 20 49 4e 53 45 52 54 2c 20 55 50 44  d on INSERT, UPD
31215 41 54 45 2c 20 61 6e 64 20 44 45 4c 45 54 45 20  ATE, and DELETE 
31216 22 0a 20 20 20 20 20 20 20 20 22 73 74 61 74 65  ".        "state
31217 6d 65 6e 74 73 20 77 69 74 68 69 6e 20 74 72 69  ments within tri
31218 67 67 65 72 73 22 29 3b 0a 7d 0a 20 20 20 20 20  ggers");.}.     
31219 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3121a 63 61 73 65 20 32 38 38 3a 20 2f 2a 20 74 72 69  case 288: /* tri
3121b 64 78 62 79 20 3a 3a 3d 20 49 4e 44 45 58 45 44  dxby ::= INDEXED
3121c 20 42 59 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71   BY nm */.{.  sq
3121d 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
3121e 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 22 74  arse,.        "t
3121f 68 65 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c  he INDEXED BY cl
31220 61 75 73 65 20 69 73 20 6e 6f 74 20 61 6c 6c 6f  ause is not allo
31221 77 65 64 20 6f 6e 20 55 50 44 41 54 45 20 6f 72  wed on UPDATE or
31222 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e   DELETE statemen
31223 74 73 20 22 0a 20 20 20 20 20 20 20 20 22 77 69  ts ".        "wi
31224 74 68 69 6e 20 74 72 69 67 67 65 72 73 22 29 3b  thin triggers");
31225 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
31226 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 38 39  ;.      case 289
31227 3a 20 2f 2a 20 74 72 69 64 78 62 79 20 3a 3a 3d  : /* tridxby ::=
31228 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 2a 2f 0a   NOT INDEXED */.
31229 7b 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72  {.  sqlite3Error
3122a 4d 73 67 28 70 50 61 72 73 65 2c 0a 20 20 20 20  Msg(pParse,.    
3122b 20 20 20 20 22 74 68 65 20 4e 4f 54 20 49 4e 44      "the NOT IND
3122c 45 58 45 44 20 63 6c 61 75 73 65 20 69 73 20 6e  EXED clause is n
3122d 6f 74 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 55 50  ot allowed on UP
3122e 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20 73  DATE or DELETE s
3122f 74 61 74 65 6d 65 6e 74 73 20 22 0a 20 20 20 20  tatements ".    
31230 20 20 20 20 22 77 69 74 68 69 6e 20 74 72 69 67      "within trig
31231 67 65 72 73 22 29 3b 0a 7d 0a 20 20 20 20 20 20  gers");.}.      
31232 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31233 61 73 65 20 32 39 30 3a 20 2f 2a 20 74 72 69 67  ase 290: /* trig
31234 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 55 50 44 41  ger_cmd ::= UPDA
31235 54 45 20 6f 72 63 6f 6e 66 20 74 72 6e 6d 20 74  TE orconf trnm t
31236 72 69 64 78 62 79 20 53 45 54 20 73 65 74 6c 69  ridxby SET setli
31237 73 74 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a  st where_opt */.
31238 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
31239 34 37 33 20 3d 20 73 71 6c 69 74 65 33 54 72 69  473 = sqlite3Tri
3123a 67 67 65 72 55 70 64 61 74 65 53 74 65 70 28 70  ggerUpdateStep(p
3123b 50 61 72 73 65 2d 3e 64 62 2c 20 26 79 79 6d 73  Parse->db, &yyms
3123c 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-4].minor.yy0,
3123d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
3123e 2e 79 79 31 34 2c 20 79 79 6d 73 70 5b 30 5d 2e  .yy14, yymsp[0].
3123f 6d 69 6e 6f 72 2e 79 79 31 33 32 2c 20 79 79 6d  minor.yy132, yym
31240 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-5].minor.yy1
31241 38 36 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  86); }.        b
31242 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31243 20 32 39 31 3a 20 2f 2a 20 74 72 69 67 67 65 72   291: /* trigger
31244 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f  _cmd ::= insert_
31245 63 6d 64 20 49 4e 54 4f 20 74 72 6e 6d 20 69 6e  cmd INTO trnm in
31246 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 56 41 4c  scollist_opt VAL
31247 55 45 53 20 4c 50 20 69 74 65 6d 6c 69 73 74 20  UES LP itemlist 
31248 52 50 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  RP */.{yygotomin
31249 6f 72 2e 79 79 34 37 33 20 3d 20 73 71 6c 69 74  or.yy473 = sqlit
3124a 65 33 54 72 69 67 67 65 72 49 6e 73 65 72 74 53  e3TriggerInsertS
3124b 74 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  tep(pParse->db, 
3124c 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72  &yymsp[-5].minor
3124d 2e 79 79 30 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e  .yy0, yymsp[-4].
3124e 6d 69 6e 6f 72 2e 79 79 34 30 38 2c 20 79 79 6d  minor.yy408, yym
3124f 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
31250 34 2c 20 30 2c 20 79 79 6d 73 70 5b 2d 37 5d 2e  4, 0, yymsp[-7].
31251 6d 69 6e 6f 72 2e 79 79 31 38 36 29 3b 7d 0a 20  minor.yy186);}. 
31252 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
31253 20 20 20 20 63 61 73 65 20 32 39 32 3a 20 2f 2a      case 292: /*
31254 20 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d   trigger_cmd ::=
31255 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f   insert_cmd INTO
31256 20 74 72 6e 6d 20 69 6e 73 63 6f 6c 6c 69 73 74   trnm inscollist
31257 5f 6f 70 74 20 73 65 6c 65 63 74 20 2a 2f 0a 7b  _opt select */.{
31258 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37  yygotominor.yy47
31259 33 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67  3 = sqlite3Trigg
3125a 65 72 49 6e 73 65 72 74 53 74 65 70 28 70 50 61  erInsertStep(pPa
3125b 72 73 65 2d 3e 64 62 2c 20 26 79 79 6d 73 70 5b  rse->db, &yymsp[
3125c 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79  -2].minor.yy0, y
3125d 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3125e 79 34 30 38 2c 20 30 2c 20 79 79 6d 73 70 5b 30  y408, 0, yymsp[0
3125f 5d 2e 6d 69 6e 6f 72 2e 79 79 33 2c 20 79 79 6d  ].minor.yy3, yym
31260 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-4].minor.yy1
31261 38 36 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  86);}.        br
31262 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
31263 32 39 33 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  293: /* trigger_
31264 63 6d 64 20 3a 3a 3d 20 44 45 4c 45 54 45 20 46  cmd ::= DELETE F
31265 52 4f 4d 20 74 72 6e 6d 20 74 72 69 64 78 62 79  ROM trnm tridxby
31266 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a 7b 79   where_opt */.{y
31267 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 33  ygotominor.yy473
31268 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65   = sqlite3Trigge
31269 72 44 65 6c 65 74 65 53 74 65 70 28 70 50 61 72  rDeleteStep(pPar
3126a 73 65 2d 3e 64 62 2c 20 26 79 79 6d 73 70 5b 2d  se->db, &yymsp[-
3126b 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79  2].minor.yy0, yy
3126c 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31  msp[0].minor.yy1
3126d 33 32 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  32);}.        br
3126e 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3126f 32 39 34 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  294: /* trigger_
31270 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63 74 20 2a  cmd ::= select *
31271 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
31272 79 34 37 33 20 3d 20 73 71 6c 69 74 65 33 54 72  y473 = sqlite3Tr
31273 69 67 67 65 72 53 65 6c 65 63 74 53 74 65 70 28  iggerSelectStep(
31274 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73  pParse->db, yyms
31275 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29 3b  p[0].minor.yy3);
31276 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
31277 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 35  ;.      case 295
31278 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 52 41  : /* expr ::= RA
31279 49 53 45 20 4c 50 20 49 47 4e 4f 52 45 20 52 50  ISE LP IGNORE RP
3127a 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69   */.{.  yygotomi
3127b 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20  nor.yy346.pExpr 
3127c 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
3127d 50 61 72 73 65 2c 20 54 4b 5f 52 41 49 53 45 2c  Parse, TK_RAISE,
3127e 20 30 2c 20 30 2c 20 30 29 3b 20 0a 20 20 69 66   0, 0, 0); .  if
3127f 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
31280 33 34 36 2e 70 45 78 70 72 20 29 7b 0a 20 20 20  346.pExpr ){.   
31281 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
31282 34 36 2e 70 45 78 70 72 2d 3e 61 66 66 69 6e 69  46.pExpr->affini
31283 74 79 20 3d 20 4f 45 5f 49 67 6e 6f 72 65 3b 0a  ty = OE_Ignore;.
31284 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f    }.  yygotomino
31285 72 2e 79 79 33 34 36 2e 7a 53 74 61 72 74 20 3d  r.yy346.zStart =
31286 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72   yymsp[-3].minor
31287 2e 79 79 30 2e 7a 3b 0a 20 20 79 79 67 6f 74 6f  .yy0.z;.  yygoto
31288 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64  minor.yy346.zEnd
31289 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e   = &yymsp[0].min
3128a 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30  or.yy0.z[yymsp[0
3128b 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a  ].minor.yy0.n];.
3128c 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3128d 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 36 3a  .      case 296:
3128e 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 52 41 49   /* expr ::= RAI
3128f 53 45 20 4c 50 20 72 61 69 73 65 74 79 70 65 20  SE LP raisetype 
31290 43 4f 4d 4d 41 20 6e 6d 20 52 50 20 2a 2f 0a 7b  COMMA nm RP */.{
31291 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
31292 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c  y346.pExpr = sql
31293 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
31294 2c 20 54 4b 5f 52 41 49 53 45 2c 20 30 2c 20 30  , TK_RAISE, 0, 0
31295 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  , &yymsp[-1].min
31296 6f 72 2e 79 79 30 29 3b 20 0a 20 20 69 66 28 20  or.yy0); .  if( 
31297 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
31298 36 2e 70 45 78 70 72 20 29 20 7b 0a 20 20 20 20  6.pExpr ) {.    
31299 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3129a 36 2e 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74  6.pExpr->affinit
3129b 79 20 3d 20 28 63 68 61 72 29 79 79 6d 73 70 5b  y = (char)yymsp[
3129c 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b  -3].minor.yy328;
3129d 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  .  }.  yygotomin
3129e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72 74 20  or.yy346.zStart 
3129f 3d 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f  = yymsp[-5].mino
312a0 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79 67 6f 74  r.yy0.z;.  yygot
312a1 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e  ominor.yy346.zEn
312a2 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  d = &yymsp[0].mi
312a3 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b  nor.yy0.z[yymsp[
312a4 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b  0].minor.yy0.n];
312a5 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
312a6 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 37  ;.      case 297
312a7 3a 20 2f 2a 20 72 61 69 73 65 74 79 70 65 20 3a  : /* raisetype :
312a8 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20 2a 2f 0a 7b  := ROLLBACK */.{
312a9 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32  yygotominor.yy32
312aa 38 20 3d 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3b  8 = OE_Rollback;
312ab 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
312ac 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 39 3a  .      case 299:
312ad 20 2f 2a 20 72 61 69 73 65 74 79 70 65 20 3a 3a   /* raisetype ::
312ae 3d 20 46 41 49 4c 20 2a 2f 0a 7b 79 79 67 6f 74  = FAIL */.{yygot
312af 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f  ominor.yy328 = O
312b0 45 5f 46 61 69 6c 3b 7d 0a 20 20 20 20 20 20 20  E_Fail;}.       
312b1 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
312b2 73 65 20 33 30 30 3a 20 2f 2a 20 63 6d 64 20 3a  se 300: /* cmd :
312b3 3a 3d 20 44 52 4f 50 20 54 52 49 47 47 45 52 20  := DROP TRIGGER 
312b4 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d  ifexists fullnam
312b5 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  e */.{.  sqlite3
312b6 44 72 6f 70 54 72 69 67 67 65 72 28 70 50 61 72  DropTrigger(pPar
312b7 73 65 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  se,yymsp[0].mino
312b8 72 2e 79 79 36 35 2c 79 79 6d 73 70 5b 2d 31 5d  r.yy65,yymsp[-1]
312b9 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b 0a 7d  .minor.yy328);.}
312ba 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
312bb 20 20 20 20 20 20 63 61 73 65 20 33 30 31 3a 20        case 301: 
312bc 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41 54 54 41 43  /* cmd ::= ATTAC
312bd 48 20 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70  H database_kw_op
312be 74 20 65 78 70 72 20 41 53 20 65 78 70 72 20 6b  t expr AS expr k
312bf 65 79 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 73 71  ey_opt */.{.  sq
312c0 6c 69 74 65 33 41 74 74 61 63 68 28 70 50 61 72  lite3Attach(pPar
312c1 73 65 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  se, yymsp[-3].mi
312c2 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c  nor.yy346.pExpr,
312c3 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
312c4 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20 79 79  .yy346.pExpr, yy
312c5 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31  msp[0].minor.yy1
312c6 33 32 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  32);.}.        b
312c7 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
312c8 20 33 30 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   302: /* cmd ::=
312c9 20 44 45 54 41 43 48 20 64 61 74 61 62 61 73 65   DETACH database
312ca 5f 6b 77 5f 6f 70 74 20 65 78 70 72 20 2a 2f 0a  _kw_opt expr */.
312cb 7b 0a 20 20 73 71 6c 69 74 65 33 44 65 74 61 63  {.  sqlite3Detac
312cc 68 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  h(pParse, yymsp[
312cd 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  0].minor.yy346.p
312ce 45 78 70 72 29 3b 0a 7d 0a 20 20 20 20 20 20 20  Expr);.}.       
312cf 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
312d0 73 65 20 33 30 37 3a 20 2f 2a 20 63 6d 64 20 3a  se 307: /* cmd :
312d1 3a 3d 20 52 45 49 4e 44 45 58 20 2a 2f 0a 7b 73  := REINDEX */.{s
312d2 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28 70 50  qlite3Reindex(pP
312d3 61 72 73 65 2c 20 30 2c 20 30 29 3b 7d 0a 20 20  arse, 0, 0);}.  
312d4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
312d5 20 20 20 63 61 73 65 20 33 30 38 3a 20 2f 2a 20     case 308: /* 
312d6 63 6d 64 20 3a 3a 3d 20 52 45 49 4e 44 45 58 20  cmd ::= REINDEX 
312d7 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 73 71 6c 69  nm dbnm */.{sqli
312d8 74 65 33 52 65 69 6e 64 65 78 28 70 50 61 72 73  te3Reindex(pPars
312d9 65 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  e, &yymsp[-1].mi
312da 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b  nor.yy0, &yymsp[
312db 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a  0].minor.yy0);}.
312dc 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
312dd 20 20 20 20 20 63 61 73 65 20 33 30 39 3a 20 2f       case 309: /
312de 2a 20 63 6d 64 20 3a 3a 3d 20 41 4e 41 4c 59 5a  * cmd ::= ANALYZ
312df 45 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 6e 61  E */.{sqlite3Ana
312e0 6c 79 7a 65 28 70 50 61 72 73 65 2c 20 30 2c 20  lyze(pParse, 0, 
312e1 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
312e2 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33  ak;.      case 3
312e3 31 30 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41  10: /* cmd ::= A
312e4 4e 41 4c 59 5a 45 20 6e 6d 20 64 62 6e 6d 20 2a  NALYZE nm dbnm *
312e5 2f 0a 7b 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a  /.{sqlite3Analyz
312e6 65 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70  e(pParse, &yymsp
312e7 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-1].minor.yy0, 
312e8 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
312e9 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  yy0);}.        b
312ea 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
312eb 20 33 31 31 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   311: /* cmd ::=
312ec 20 41 4c 54 45 52 20 54 41 42 4c 45 20 66 75 6c   ALTER TABLE ful
312ed 6c 6e 61 6d 65 20 52 45 4e 41 4d 45 20 54 4f 20  lname RENAME TO 
312ee 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65  nm */.{.  sqlite
312ef 33 41 6c 74 65 72 52 65 6e 61 6d 65 54 61 62 6c  3AlterRenameTabl
312f0 65 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  e(pParse,yymsp[-
312f1 33 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 26 79  3].minor.yy65,&y
312f2 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
312f3 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
312f4 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
312f5 33 31 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  312: /* cmd ::= 
312f6 41 4c 54 45 52 20 54 41 42 4c 45 20 61 64 64 5f  ALTER TABLE add_
312f7 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 20  column_fullname 
312f8 41 44 44 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74  ADD kwcolumn_opt
312f9 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 7b 0a 20 20 73   column */.{.  s
312fa 71 6c 69 74 65 33 41 6c 74 65 72 46 69 6e 69 73  qlite3AlterFinis
312fb 68 41 64 64 43 6f 6c 75 6d 6e 28 70 50 61 72 73  hAddColumn(pPars
312fc 65 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  e, &yymsp[0].min
312fd 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20  or.yy0);.}.     
312fe 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
312ff 63 61 73 65 20 33 31 33 3a 20 2f 2a 20 61 64 64  case 313: /* add
31300 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65  _column_fullname
31301 20 3a 3a 3d 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f   ::= fullname */
31302 0a 7b 0a 20 20 70 50 61 72 73 65 2d 3e 64 62 2d  .{.  pParse->db-
31303 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62  >lookaside.bEnab
31304 6c 65 64 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  led = 0;.  sqlit
31305 65 33 41 6c 74 65 72 42 65 67 69 6e 41 64 64 43  e3AlterBeginAddC
31306 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 79 79  olumn(pParse, yy
31307 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 36  msp[0].minor.yy6
31308 35 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  5);.}.        br
31309 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3130a 33 31 36 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  316: /* cmd ::= 
3130b 63 72 65 61 74 65 5f 76 74 61 62 20 2a 2f 0a 7b  create_vtab */.{
3130c 73 71 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73  sqlite3VtabFinis
3130d 68 50 61 72 73 65 28 70 50 61 72 73 65 2c 30 29  hParse(pParse,0)
3130e 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
3130f 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 31 37  ;.      case 317
31310 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 63 72 65  : /* cmd ::= cre
31311 61 74 65 5f 76 74 61 62 20 4c 50 20 76 74 61 62  ate_vtab LP vtab
31312 61 72 67 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 73  arglist RP */.{s
31313 71 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73 68  qlite3VtabFinish
31314 50 61 72 73 65 28 70 50 61 72 73 65 2c 26 79 79  Parse(pParse,&yy
31315 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
31316 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
31317 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 31  k;.      case 31
31318 38 3a 20 2f 2a 20 63 72 65 61 74 65 5f 76 74 61  8: /* create_vta
31319 62 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 56  b ::= createkw V
3131a 49 52 54 55 41 4c 20 54 41 42 4c 45 20 6e 6d 20  IRTUAL TABLE nm 
3131b 64 62 6e 6d 20 55 53 49 4e 47 20 6e 6d 20 2a 2f  dbnm USING nm */
3131c 0a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74  .{.    sqlite3Vt
3131d 61 62 42 65 67 69 6e 50 61 72 73 65 28 70 50 61  abBeginParse(pPa
3131e 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e  rse, &yymsp[-3].
3131f 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73  minor.yy0, &yyms
31320 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-2].minor.yy0,
31321 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
31322 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20  .yy0);.}.       
31323 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31324 73 65 20 33 32 31 3a 20 2f 2a 20 76 74 61 62 61  se 321: /* vtaba
31325 72 67 20 3a 3a 3d 20 2a 2f 0a 7b 73 71 6c 69 74  rg ::= */.{sqlit
31326 65 33 56 74 61 62 41 72 67 49 6e 69 74 28 70 50  e3VtabArgInit(pP
31327 61 72 73 65 29 3b 7d 0a 20 20 20 20 20 20 20 20  arse);}.        
31328 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
31329 65 20 33 32 33 3a 20 2f 2a 20 76 74 61 62 61 72  e 323: /* vtabar
3132a 67 74 6f 6b 65 6e 20 3a 3a 3d 20 41 4e 59 20 2a  gtoken ::= ANY *
3132b 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33 32 34  /.      case 324
3132c 3a 20 2f 2a 20 76 74 61 62 61 72 67 74 6f 6b 65  : /* vtabargtoke
3132d 6e 20 3a 3a 3d 20 6c 70 20 61 6e 79 6c 69 73 74  n ::= lp anylist
3132e 20 52 50 20 2a 2f 20 79 79 74 65 73 74 63 61 73   RP */ yytestcas
3132f 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 32 34 29  e(yyruleno==324)
31330 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 32 35  ;.      case 325
31331 3a 20 2f 2a 20 6c 70 20 3a 3a 3d 20 4c 50 20 2a  : /* lp ::= LP *
31332 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
31333 75 6c 65 6e 6f 3d 3d 33 32 35 29 3b 0a 7b 73 71  uleno==325);.{sq
31334 6c 69 74 65 33 56 74 61 62 41 72 67 45 78 74 65  lite3VtabArgExte
31335 6e 64 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70  nd(pParse,&yymsp
31336 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d  [0].minor.yy0);}
31337 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
31338 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20        default:. 
31339 20 20 20 20 20 2f 2a 20 28 30 29 20 69 6e 70 75       /* (0) inpu
3133a 74 20 3a 3a 3d 20 63 6d 64 6c 69 73 74 20 2a 2f  t ::= cmdlist */
3133b 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3133c 6c 65 6e 6f 3d 3d 30 29 3b 0a 20 20 20 20 20 20  leno==0);.      
3133d 2f 2a 20 28 31 29 20 63 6d 64 6c 69 73 74 20 3a  /* (1) cmdlist :
3133e 3a 3d 20 63 6d 64 6c 69 73 74 20 65 63 6d 64 20  := cmdlist ecmd 
3133f 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
31340 72 75 6c 65 6e 6f 3d 3d 31 29 3b 0a 20 20 20 20  ruleno==1);.    
31341 20 20 2f 2a 20 28 32 29 20 63 6d 64 6c 69 73 74    /* (2) cmdlist
31342 20 3a 3a 3d 20 65 63 6d 64 20 2a 2f 20 79 79 74   ::= ecmd */ yyt
31343 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
31344 3d 3d 32 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  ==2);.      /* (
31345 33 29 20 65 63 6d 64 20 3a 3a 3d 20 53 45 4d 49  3) ecmd ::= SEMI
31346 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
31347 79 72 75 6c 65 6e 6f 3d 3d 33 29 3b 0a 20 20 20  yruleno==3);.   
31348 20 20 20 2f 2a 20 28 34 29 20 65 63 6d 64 20 3a     /* (4) ecmd :
31349 3a 3d 20 65 78 70 6c 61 69 6e 20 63 6d 64 78 20  := explain cmdx 
3134a 53 45 4d 49 20 2a 2f 20 79 79 74 65 73 74 63 61  SEMI */ yytestca
3134b 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34 29 3b  se(yyruleno==4);
3134c 0a 20 20 20 20 20 20 2f 2a 20 28 31 30 29 20 74  .      /* (10) t
3134d 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20  rans_opt ::= */ 
3134e 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3134f 65 6e 6f 3d 3d 31 30 29 3b 0a 20 20 20 20 20 20  eno==10);.      
31350 2f 2a 20 28 31 31 29 20 74 72 61 6e 73 5f 6f 70  /* (11) trans_op
31351 74 20 3a 3a 3d 20 54 52 41 4e 53 41 43 54 49 4f  t ::= TRANSACTIO
31352 4e 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  N */ yytestcase(
31353 79 79 72 75 6c 65 6e 6f 3d 3d 31 31 29 3b 0a 20  yyruleno==11);. 
31354 20 20 20 20 20 2f 2a 20 28 31 32 29 20 74 72 61       /* (12) tra
31355 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53  ns_opt ::= TRANS
31356 41 43 54 49 4f 4e 20 6e 6d 20 2a 2f 20 79 79 74  ACTION nm */ yyt
31357 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
31358 3d 3d 31 32 29 3b 0a 20 20 20 20 20 20 2f 2a 20  ==12);.      /* 
31359 28 32 30 29 20 73 61 76 65 70 6f 69 6e 74 5f 6f  (20) savepoint_o
3135a 70 74 20 3a 3a 3d 20 53 41 56 45 50 4f 49 4e 54  pt ::= SAVEPOINT
3135b 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3135c 79 72 75 6c 65 6e 6f 3d 3d 32 30 29 3b 0a 20 20  yruleno==20);.  
3135d 20 20 20 20 2f 2a 20 28 32 31 29 20 73 61 76 65      /* (21) save
3135e 70 6f 69 6e 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f  point_opt ::= */
3135f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
31360 6c 65 6e 6f 3d 3d 32 31 29 3b 0a 20 20 20 20 20  leno==21);.     
31361 20 2f 2a 20 28 32 35 29 20 63 6d 64 20 3a 3a 3d   /* (25) cmd ::=
31362 20 63 72 65 61 74 65 5f 74 61 62 6c 65 20 63 72   create_table cr
31363 65 61 74 65 5f 74 61 62 6c 65 5f 61 72 67 73 20  eate_table_args 
31364 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
31365 72 75 6c 65 6e 6f 3d 3d 32 35 29 3b 0a 20 20 20  ruleno==25);.   
31366 20 20 20 2f 2a 20 28 33 34 29 20 63 6f 6c 75 6d     /* (34) colum
31367 6e 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e  nlist ::= column
31368 6c 69 73 74 20 43 4f 4d 4d 41 20 63 6f 6c 75 6d  list COMMA colum
31369 6e 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  n */ yytestcase(
3136a 79 79 72 75 6c 65 6e 6f 3d 3d 33 34 29 3b 0a 20  yyruleno==34);. 
3136b 20 20 20 20 20 2f 2a 20 28 33 35 29 20 63 6f 6c       /* (35) col
3136c 75 6d 6e 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c 75  umnlist ::= colu
3136d 6d 6e 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  mn */ yytestcase
3136e 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 35 29 3b 0a  (yyruleno==35);.
3136f 20 20 20 20 20 20 2f 2a 20 28 34 34 29 20 74 79        /* (44) ty
31370 70 65 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  pe ::= */ yytest
31371 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34  case(yyruleno==4
31372 34 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 35 31  4);.      /* (51
31373 29 20 73 69 67 6e 65 64 20 3a 3a 3d 20 70 6c 75  ) signed ::= plu
31374 73 5f 6e 75 6d 20 2a 2f 20 79 79 74 65 73 74 63  s_num */ yytestc
31375 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 35 31  ase(yyruleno==51
31376 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 35 32 29  );.      /* (52)
31377 20 73 69 67 6e 65 64 20 3a 3a 3d 20 6d 69 6e 75   signed ::= minu
31378 73 5f 6e 75 6d 20 2a 2f 20 79 79 74 65 73 74 63  s_num */ yytestc
31379 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 35 32  ase(yyruleno==52
3137a 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 35 33 29  );.      /* (53)
3137b 20 63 61 72 67 6c 69 73 74 20 3a 3a 3d 20 63 61   carglist ::= ca
3137c 72 67 6c 69 73 74 20 63 61 72 67 20 2a 2f 20 79  rglist carg */ y
3137d 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3137e 6e 6f 3d 3d 35 33 29 3b 0a 20 20 20 20 20 20 2f  no==53);.      /
3137f 2a 20 28 35 34 29 20 63 61 72 67 6c 69 73 74 20  * (54) carglist 
31380 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ::= */ yytestcas
31381 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 35 34 29 3b  e(yyruleno==54);
31382 0a 20 20 20 20 20 20 2f 2a 20 28 35 35 29 20 63  .      /* (55) c
31383 61 72 67 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49  arg ::= CONSTRAI
31384 4e 54 20 6e 6d 20 63 63 6f 6e 73 20 2a 2f 20 79  NT nm ccons */ y
31385 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
31386 6e 6f 3d 3d 35 35 29 3b 0a 20 20 20 20 20 20 2f  no==55);.      /
31387 2a 20 28 35 36 29 20 63 61 72 67 20 3a 3a 3d 20  * (56) carg ::= 
31388 63 63 6f 6e 73 20 2a 2f 20 79 79 74 65 73 74 63  ccons */ yytestc
31389 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 35 36  ase(yyruleno==56
3138a 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 36 32 29  );.      /* (62)
3138b 20 63 63 6f 6e 73 20 3a 3a 3d 20 4e 55 4c 4c 20   ccons ::= NULL 
3138c 6f 6e 63 6f 6e 66 20 2a 2f 20 79 79 74 65 73 74  onconf */ yytest
3138d 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 36  case(yyruleno==6
3138e 32 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 38 39  2);.      /* (89
3138f 29 20 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 63  ) conslist ::= c
31390 6f 6e 73 6c 69 73 74 20 43 4f 4d 4d 41 20 74 63  onslist COMMA tc
31391 6f 6e 73 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ons */ yytestcas
31392 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 38 39 29 3b  e(yyruleno==89);
31393 0a 20 20 20 20 20 20 2f 2a 20 28 39 30 29 20 63  .      /* (90) c
31394 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 63 6f 6e 73  onslist ::= cons
31395 6c 69 73 74 20 74 63 6f 6e 73 20 2a 2f 20 79 79  list tcons */ yy
31396 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
31397 6f 3d 3d 39 30 29 3b 0a 20 20 20 20 20 20 2f 2a  o==90);.      /*
31398 20 28 39 31 29 20 63 6f 6e 73 6c 69 73 74 20 3a   (91) conslist :
31399 3a 3d 20 74 63 6f 6e 73 20 2a 2f 20 79 79 74 65  := tcons */ yyte
3139a 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3139b 3d 39 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  =91);.      /* (
3139c 39 32 29 20 74 63 6f 6e 73 20 3a 3a 3d 20 43 4f  92) tcons ::= CO
3139d 4e 53 54 52 41 49 4e 54 20 6e 6d 20 2a 2f 20 79  NSTRAINT nm */ y
3139e 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3139f 6e 6f 3d 3d 39 32 29 3b 0a 20 20 20 20 20 20 2f  no==92);.      /
313a0 2a 20 28 32 36 38 29 20 70 6c 75 73 5f 6f 70 74  * (268) plus_opt
313a1 20 3a 3a 3d 20 50 4c 55 53 20 2a 2f 20 79 79 74   ::= PLUS */ yyt
313a2 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
313a3 3d 3d 32 36 38 29 3b 0a 20 20 20 20 20 20 2f 2a  ==268);.      /*
313a4 20 28 32 36 39 29 20 70 6c 75 73 5f 6f 70 74 20   (269) plus_opt 
313a5 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ::= */ yytestcas
313a6 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 36 39 29  e(yyruleno==269)
313a7 3b 0a 20 20 20 20 20 20 2f 2a 20 28 32 37 39 29  ;.      /* (279)
313a8 20 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 20   foreach_clause 
313a9 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ::= */ yytestcas
313aa 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 37 39 29  e(yyruleno==279)
313ab 3b 0a 20 20 20 20 20 20 2f 2a 20 28 32 38 30 29  ;.      /* (280)
313ac 20 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 20   foreach_clause 
313ad 3a 3a 3d 20 46 4f 52 20 45 41 43 48 20 52 4f 57  ::= FOR EACH ROW
313ae 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
313af 79 72 75 6c 65 6e 6f 3d 3d 32 38 30 29 3b 0a 20  yruleno==280);. 
313b0 20 20 20 20 20 2f 2a 20 28 32 38 37 29 20 74 72       /* (287) tr
313b1 69 64 78 62 79 20 3a 3a 3d 20 2a 2f 20 79 79 74  idxby ::= */ yyt
313b2 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
313b3 3d 3d 32 38 37 29 3b 0a 20 20 20 20 20 20 2f 2a  ==287);.      /*
313b4 20 28 33 30 35 29 20 64 61 74 61 62 61 73 65 5f   (305) database_
313b5 6b 77 5f 6f 70 74 20 3a 3a 3d 20 44 41 54 41 42  kw_opt ::= DATAB
313b6 41 53 45 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ASE */ yytestcas
313b7 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 30 35 29  e(yyruleno==305)
313b8 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33 30 36 29  ;.      /* (306)
313b9 20 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74   database_kw_opt
313ba 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61   ::= */ yytestca
313bb 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 30 36  se(yyruleno==306
313bc 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33 31 34  );.      /* (314
313bd 29 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 3a  ) kwcolumn_opt :
313be 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  := */ yytestcase
313bf 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 31 34 29 3b  (yyruleno==314);
313c0 0a 20 20 20 20 20 20 2f 2a 20 28 33 31 35 29 20  .      /* (315) 
313c1 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 3a 3a 3d  kwcolumn_opt ::=
313c2 20 43 4f 4c 55 4d 4e 4b 57 20 2a 2f 20 79 79 74   COLUMNKW */ yyt
313c3 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
313c4 3d 3d 33 31 35 29 3b 0a 20 20 20 20 20 20 2f 2a  ==315);.      /*
313c5 20 28 33 31 39 29 20 76 74 61 62 61 72 67 6c 69   (319) vtabargli
313c6 73 74 20 3a 3a 3d 20 76 74 61 62 61 72 67 20 2a  st ::= vtabarg *
313c7 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
313c8 75 6c 65 6e 6f 3d 3d 33 31 39 29 3b 0a 20 20 20  uleno==319);.   
313c9 20 20 20 2f 2a 20 28 33 32 30 29 20 76 74 61 62     /* (320) vtab
313ca 61 72 67 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62  arglist ::= vtab
313cb 61 72 67 6c 69 73 74 20 43 4f 4d 4d 41 20 76 74  arglist COMMA vt
313cc 61 62 61 72 67 20 2a 2f 20 79 79 74 65 73 74 63  abarg */ yytestc
313cd 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 32  ase(yyruleno==32
313ce 30 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33 32  0);.      /* (32
313cf 32 29 20 76 74 61 62 61 72 67 20 3a 3a 3d 20 76  2) vtabarg ::= v
313d0 74 61 62 61 72 67 20 76 74 61 62 61 72 67 74 6f  tabarg vtabargto
313d1 6b 65 6e 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ken */ yytestcas
313d2 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 32 32 29  e(yyruleno==322)
313d3 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33 32 36 29  ;.      /* (326)
313d4 20 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 2a 2f 20   anylist ::= */ 
313d5 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
313d6 65 6e 6f 3d 3d 33 32 36 29 3b 0a 20 20 20 20 20  eno==326);.     
313d7 20 2f 2a 20 28 33 32 37 29 20 61 6e 79 6c 69 73   /* (327) anylis
313d8 74 20 3a 3a 3d 20 61 6e 79 6c 69 73 74 20 4c 50  t ::= anylist LP
313d9 20 61 6e 79 6c 69 73 74 20 52 50 20 2a 2f 20 79   anylist RP */ y
313da 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
313db 6e 6f 3d 3d 33 32 37 29 3b 0a 20 20 20 20 20 20  no==327);.      
313dc 2f 2a 20 28 33 32 38 29 20 61 6e 79 6c 69 73 74  /* (328) anylist
313dd 20 3a 3a 3d 20 61 6e 79 6c 69 73 74 20 41 4e 59   ::= anylist ANY
313de 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
313df 79 72 75 6c 65 6e 6f 3d 3d 33 32 38 29 3b 0a 20  yruleno==328);. 
313e0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
313e1 7d 3b 0a 20 20 79 79 67 6f 74 6f 20 3d 20 79 79  };.  yygoto = yy
313e2 52 75 6c 65 49 6e 66 6f 5b 79 79 72 75 6c 65 6e  RuleInfo[yyrulen
313e3 6f 5d 2e 6c 68 73 3b 0a 20 20 79 79 73 69 7a 65  o].lhs;.  yysize
313e4 20 3d 20 79 79 52 75 6c 65 49 6e 66 6f 5b 79 79   = yyRuleInfo[yy
313e5 72 75 6c 65 6e 6f 5d 2e 6e 72 68 73 3b 0a 20 20  ruleno].nrhs;.  
313e6 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
313e7 20 2d 3d 20 79 79 73 69 7a 65 3b 0a 20 20 79 79   -= yysize;.  yy
313e8 61 63 74 20 3d 20 79 79 5f 66 69 6e 64 5f 72 65  act = yy_find_re
313e9 64 75 63 65 5f 61 63 74 69 6f 6e 28 79 79 6d 73  duce_action(yyms
313ea 70 5b 2d 79 79 73 69 7a 65 5d 2e 73 74 61 74 65  p[-yysize].state
313eb 6e 6f 2c 28 59 59 43 4f 44 45 54 59 50 45 29 79  no,(YYCODETYPE)y
313ec 79 67 6f 74 6f 29 3b 0a 20 20 69 66 28 20 79 79  ygoto);.  if( yy
313ed 61 63 74 20 3c 20 59 59 4e 53 54 41 54 45 20 29  act < YYNSTATE )
313ee 7b 0a 23 69 66 64 65 66 20 4e 44 45 42 55 47 0a  {.#ifdef NDEBUG.
313ef 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65      /* If we are
313f0 20 6e 6f 74 20 64 65 62 75 67 67 69 6e 67 20 61   not debugging a
313f1 6e 64 20 74 68 65 20 72 65 64 75 63 65 20 61 63  nd the reduce ac
313f2 74 69 6f 6e 20 70 6f 70 70 65 64 20 61 74 20 6c  tion popped at l
313f3 65 61 73 74 0a 20 20 20 20 2a 2a 20 6f 6e 65 20  east.    ** one 
313f4 65 6c 65 6d 65 6e 74 20 6f 66 66 20 74 68 65 20  element off the 
313f5 73 74 61 63 6b 2c 20 74 68 65 6e 20 77 65 20 63  stack, then we c
313f6 61 6e 20 70 75 73 68 20 74 68 65 20 6e 65 77 20  an push the new 
313f7 65 6c 65 6d 65 6e 74 20 62 61 63 6b 0a 20 20 20  element back.   
313f8 20 2a 2a 20 6f 6e 74 6f 20 74 68 65 20 73 74 61   ** onto the sta
313f9 63 6b 20 68 65 72 65 2c 20 61 6e 64 20 73 6b 69  ck here, and ski
313fa 70 20 74 68 65 20 73 74 61 63 6b 20 6f 76 65 72  p the stack over
313fb 66 6c 6f 77 20 74 65 73 74 20 69 6e 20 79 79 5f  flow test in yy_
313fc 73 68 69 66 74 28 29 2e 0a 20 20 20 20 2a 2a 20  shift()..    ** 
313fd 54 68 61 74 20 67 69 76 65 73 20 61 20 73 69 67  That gives a sig
313fe 6e 69 66 69 63 61 6e 74 20 73 70 65 65 64 20 69  nificant speed i
313ff 6d 70 72 6f 76 65 6d 65 6e 74 2e 20 2a 2f 0a 20  mprovement. */. 
31400 20 20 20 69 66 28 20 79 79 73 69 7a 65 20 29 7b     if( yysize ){
31401 0a 20 20 20 20 20 20 79 79 70 50 61 72 73 65 72  .      yypParser
31402 2d 3e 79 79 69 64 78 2b 2b 3b 0a 20 20 20 20 20  ->yyidx++;.     
31403 20 79 79 6d 73 70 20 2d 3d 20 79 79 73 69 7a 65   yymsp -= yysize
31404 2d 31 3b 0a 20 20 20 20 20 20 79 79 6d 73 70 2d  -1;.      yymsp-
31405 3e 73 74 61 74 65 6e 6f 20 3d 20 28 59 59 41 43  >stateno = (YYAC
31406 54 49 4f 4e 54 59 50 45 29 79 79 61 63 74 3b 0a  TIONTYPE)yyact;.
31407 20 20 20 20 20 20 79 79 6d 73 70 2d 3e 6d 61 6a        yymsp->maj
31408 6f 72 20 3d 20 28 59 59 43 4f 44 45 54 59 50 45  or = (YYCODETYPE
31409 29 79 79 67 6f 74 6f 3b 0a 20 20 20 20 20 20 79  )yygoto;.      y
3140a 79 6d 73 70 2d 3e 6d 69 6e 6f 72 20 3d 20 79 79  ymsp->minor = yy
3140b 67 6f 74 6f 6d 69 6e 6f 72 3b 0a 20 20 20 20 7d  gotominor;.    }
3140c 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20  else.#endif.    
3140d 7b 0a 20 20 20 20 20 20 79 79 5f 73 68 69 66 74  {.      yy_shift
3140e 28 79 79 70 50 61 72 73 65 72 2c 79 79 61 63 74  (yypParser,yyact
3140f 2c 79 79 67 6f 74 6f 2c 26 79 79 67 6f 74 6f 6d  ,yygoto,&yygotom
31410 69 6e 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  inor);.    }.  }
31411 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
31412 28 20 79 79 61 63 74 20 3d 3d 20 59 59 4e 53 54  ( yyact == YYNST
31413 41 54 45 20 2b 20 59 59 4e 52 55 4c 45 20 2b 20  ATE + YYNRULE + 
31414 31 20 29 3b 0a 20 20 20 20 79 79 5f 61 63 63 65  1 );.    yy_acce
31415 70 74 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20  pt(yypParser);. 
31416 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20   }.}../*.** The 
31417 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 65  following code e
31418 78 65 63 75 74 65 73 20 77 68 65 6e 20 74 68 65  xecutes when the
31419 20 70 61 72 73 65 20 66 61 69 6c 73 0a 2a 2f 0a   parse fails.*/.
3141a 23 69 66 6e 64 65 66 20 59 59 4e 4f 45 52 52 4f  #ifndef YYNOERRO
3141b 52 52 45 43 4f 56 45 52 59 0a 73 74 61 74 69 63  RRECOVERY.static
3141c 20 76 6f 69 64 20 79 79 5f 70 61 72 73 65 5f 66   void yy_parse_f
3141d 61 69 6c 65 64 28 0a 20 20 79 79 50 61 72 73 65  ailed(.  yyParse
3141e 72 20 2a 79 79 70 50 61 72 73 65 72 20 20 20 20  r *yypParser    
3141f 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
31420 72 73 65 72 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  rser */.){.  sql
31421 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 46 45  ite3ParserARG_FE
31422 54 43 48 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45  TCH;.#ifndef NDE
31423 42 55 47 0a 20 20 69 66 28 20 79 79 54 72 61 63  BUG.  if( yyTrac
31424 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 66 70 72  eFILE ){.    fpr
31425 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45  intf(yyTraceFILE
31426 2c 22 25 73 46 61 69 6c 21 5c 6e 22 2c 79 79 54  ,"%sFail!\n",yyT
31427 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20 7d  racePrompt);.  }
31428 0a 23 65 6e 64 69 66 0a 20 20 77 68 69 6c 65 28  .#endif.  while(
31429 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64   yypParser->yyid
3142a 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70 5f 70 61  x>=0 ) yy_pop_pa
3142b 72 73 65 72 5f 73 74 61 63 6b 28 79 79 70 50 61  rser_stack(yypPa
3142c 72 73 65 72 29 3b 0a 20 20 2f 2a 20 48 65 72 65  rser);.  /* Here
3142d 20 63 6f 64 65 20 69 73 20 69 6e 73 65 72 74 65   code is inserte
3142e 64 20 77 68 69 63 68 20 77 69 6c 6c 20 62 65 20  d which will be 
3142f 65 78 65 63 75 74 65 64 20 77 68 65 6e 65 76 65  executed wheneve
31430 72 20 74 68 65 0a 20 20 2a 2a 20 70 61 72 73 65  r the.  ** parse
31431 72 20 66 61 69 6c 73 20 2a 2f 0a 20 20 73 71 6c  r fails */.  sql
31432 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 54  ite3ParserARG_ST
31433 4f 52 45 3b 20 2f 2a 20 53 75 70 70 72 65 73 73  ORE; /* Suppress
31434 20 77 61 72 6e 69 6e 67 20 61 62 6f 75 74 20 75   warning about u
31435 6e 75 73 65 64 20 25 65 78 74 72 61 5f 61 72 67  nused %extra_arg
31436 75 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 20 2a  ument variable *
31437 2f 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 59 59  /.}.#endif /* YY
31438 4e 4f 45 52 52 4f 52 52 45 43 4f 56 45 52 59 20  NOERRORRECOVERY 
31439 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  */../*.** The fo
3143a 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 65 78 65  llowing code exe
3143b 63 75 74 65 73 20 77 68 65 6e 20 61 20 73 79 6e  cutes when a syn
3143c 74 61 78 20 65 72 72 6f 72 20 66 69 72 73 74 20  tax error first 
3143d 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69  occurs..*/.stati
3143e 63 20 76 6f 69 64 20 79 79 5f 73 79 6e 74 61 78  c void yy_syntax
3143f 5f 65 72 72 6f 72 28 0a 20 20 79 79 50 61 72 73  _error(.  yyPars
31440 65 72 20 2a 79 79 70 50 61 72 73 65 72 2c 20 20  er *yypParser,  
31441 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
31442 70 61 72 73 65 72 20 2a 2f 0a 20 20 69 6e 74 20  parser */.  int 
31443 79 79 6d 61 6a 6f 72 2c 20 20 20 20 20 20 20 20  yymajor,        
31444 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
31445 65 20 6d 61 6a 6f 72 20 74 79 70 65 20 6f 66 20  e major type of 
31446 74 68 65 20 65 72 72 6f 72 20 74 6f 6b 65 6e 20  the error token 
31447 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50 45  */.  YYMINORTYPE
31448 20 79 79 6d 69 6e 6f 72 20 20 20 20 20 20 20 20   yyminor        
31449 20 20 20 20 2f 2a 20 54 68 65 20 6d 69 6e 6f 72      /* The minor
3144a 20 74 79 70 65 20 6f 66 20 74 68 65 20 65 72 72   type of the err
3144b 6f 72 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20  or token */.){. 
3144c 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
3144d 47 5f 46 45 54 43 48 3b 0a 23 64 65 66 69 6e 65  G_FETCH;.#define
3144e 20 54 4f 4b 45 4e 20 28 79 79 6d 69 6e 6f 72 2e   TOKEN (yyminor.
3144f 79 79 30 29 0a 0a 20 20 55 4e 55 53 45 44 5f 50  yy0)..  UNUSED_P
31450 41 52 41 4d 45 54 45 52 28 79 79 6d 61 6a 6f 72  ARAMETER(yymajor
31451 29 3b 20 20 2f 2a 20 53 69 6c 65 6e 63 65 20 73  );  /* Silence s
31452 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 20 77 61 72  ome compiler war
31453 6e 69 6e 67 73 20 2a 2f 0a 20 20 61 73 73 65 72  nings */.  asser
31454 74 28 20 54 4f 4b 45 4e 2e 7a 5b 30 5d 20 29 3b  t( TOKEN.z[0] );
31455 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 69 7a    /* The tokeniz
31456 65 72 20 61 6c 77 61 79 73 20 67 69 76 65 73 20  er always gives 
31457 75 73 20 61 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20  us a token */.  
31458 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
31459 70 50 61 72 73 65 2c 20 22 6e 65 61 72 20 5c 22  pParse, "near \"
3145a 25 54 5c 22 3a 20 73 79 6e 74 61 78 20 65 72 72  %T\": syntax err
3145b 6f 72 22 2c 20 26 54 4f 4b 45 4e 29 3b 0a 20 20  or", &TOKEN);.  
3145c 70 50 61 72 73 65 2d 3e 70 61 72 73 65 45 72 72  pParse->parseErr
3145d 6f 72 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65  or = 1;.  sqlite
3145e 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f 52 45  3ParserARG_STORE
3145f 3b 20 2f 2a 20 53 75 70 70 72 65 73 73 20 77 61  ; /* Suppress wa
31460 72 6e 69 6e 67 20 61 62 6f 75 74 20 75 6e 75 73  rning about unus
31461 65 64 20 25 65 78 74 72 61 5f 61 72 67 75 6d 65  ed %extra_argume
31462 6e 74 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 7d  nt variable */.}
31463 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
31464 6f 77 69 6e 67 20 69 73 20 65 78 65 63 75 74 65  owing is execute
31465 64 20 77 68 65 6e 20 74 68 65 20 70 61 72 73 65  d when the parse
31466 72 20 61 63 63 65 70 74 73 0a 2a 2f 0a 73 74 61  r accepts.*/.sta
31467 74 69 63 20 76 6f 69 64 20 79 79 5f 61 63 63 65  tic void yy_acce
31468 70 74 28 0a 20 20 79 79 50 61 72 73 65 72 20 2a  pt(.  yyParser *
31469 79 79 70 50 61 72 73 65 72 20 20 20 20 20 20 20  yypParser       
3146a 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
3146b 72 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  r */.){.  sqlite
3146c 33 50 61 72 73 65 72 41 52 47 5f 46 45 54 43 48  3ParserARG_FETCH
3146d 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ;.#ifndef NDEBUG
3146e 0a 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49  .  if( yyTraceFI
3146f 4c 45 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74  LE ){.    fprint
31470 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25  f(yyTraceFILE,"%
31471 73 41 63 63 65 70 74 21 5c 6e 22 2c 79 79 54 72  sAccept!\n",yyTr
31472 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20 7d 0a  acePrompt);.  }.
31473 23 65 6e 64 69 66 0a 20 20 77 68 69 6c 65 28 20  #endif.  while( 
31474 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
31475 3e 3d 30 20 29 20 79 79 5f 70 6f 70 5f 70 61 72  >=0 ) yy_pop_par
31476 73 65 72 5f 73 74 61 63 6b 28 79 79 70 50 61 72  ser_stack(yypPar
31477 73 65 72 29 3b 0a 20 20 2f 2a 20 48 65 72 65 20  ser);.  /* Here 
31478 63 6f 64 65 20 69 73 20 69 6e 73 65 72 74 65 64  code is inserted
31479 20 77 68 69 63 68 20 77 69 6c 6c 20 62 65 20 65   which will be e
3147a 78 65 63 75 74 65 64 20 77 68 65 6e 65 76 65 72  xecuted whenever
3147b 20 74 68 65 0a 20 20 2a 2a 20 70 61 72 73 65 72   the.  ** parser
3147c 20 61 63 63 65 70 74 73 20 2a 2f 0a 20 20 73 71   accepts */.  sq
3147d 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53  lite3ParserARG_S
3147e 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70 72 65 73  TORE; /* Suppres
3147f 73 20 77 61 72 6e 69 6e 67 20 61 62 6f 75 74 20  s warning about 
31480 75 6e 75 73 65 64 20 25 65 78 74 72 61 5f 61 72  unused %extra_ar
31481 67 75 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 20  gument variable 
31482 2a 2f 0a 7d 0a 0a 2f 2a 20 54 68 65 20 6d 61 69  */.}../* The mai
31483 6e 20 70 61 72 73 65 72 20 70 72 6f 67 72 61 6d  n parser program
31484 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61  ..** The first a
31485 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69  rgument is a poi
31486 6e 74 65 72 20 74 6f 20 61 20 73 74 72 75 63 74  nter to a struct
31487 75 72 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ure obtained fro
31488 6d 0a 2a 2a 20 22 73 71 6c 69 74 65 33 50 61 72  m.** "sqlite3Par
31489 73 65 72 41 6c 6c 6f 63 22 20 77 68 69 63 68 20  serAlloc" which 
3148a 64 65 73 63 72 69 62 65 73 20 74 68 65 20 63 75  describes the cu
3148b 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 20 74  rrent state of t
3148c 68 65 20 70 61 72 73 65 72 2e 0a 2a 2a 20 54 68  he parser..** Th
3148d 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
3148e 74 20 69 73 20 74 68 65 20 6d 61 6a 6f 72 20 74  t is the major t
3148f 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e 20 20 54 68  oken number.  Th
31490 65 20 74 68 69 72 64 20 69 73 0a 2a 2a 20 74 68  e third is.** th
31491 65 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 2e 20 20  e minor token.  
31492 54 68 65 20 66 6f 75 72 74 68 20 6f 70 74 69 6f  The fourth optio
31493 6e 61 6c 20 61 72 67 75 6d 65 6e 74 20 69 73 20  nal argument is 
31494 77 68 61 74 65 76 65 72 20 74 68 65 0a 2a 2a 20  whatever the.** 
31495 75 73 65 72 20 77 61 6e 74 73 20 28 61 6e 64 20  user wants (and 
31496 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65  specified in the
31497 20 67 72 61 6d 6d 61 72 29 20 61 6e 64 20 69 73   grammar) and is
31498 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 0a 2a   available for.*
31499 2a 20 75 73 65 20 62 79 20 74 68 65 20 61 63 74  * use by the act
3149a 69 6f 6e 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a  ion routines..**
3149b 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a 20 3c  .** Inputs:.** <
3149c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 41 20 70 6f  ul>.** <li> A po
3149d 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 72  inter to the par
3149e 73 65 72 20 28 61 6e 20 6f 70 61 71 75 65 20 73  ser (an opaque s
3149f 74 72 75 63 74 75 72 65 2e 29 0a 2a 2a 20 3c 6c  tructure.).** <l
314a0 69 3e 20 54 68 65 20 6d 61 6a 6f 72 20 74 6f 6b  i> The major tok
314a1 65 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 3c 6c  en number..** <l
314a2 69 3e 20 54 68 65 20 6d 69 6e 6f 72 20 74 6f 6b  i> The minor tok
314a3 65 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 3c 6c  en number..** <l
314a4 69 3e 20 41 6e 20 6f 70 74 69 6f 6e 20 61 72 67  i> An option arg
314a5 75 6d 65 6e 74 20 6f 66 20 61 20 67 72 61 6d 6d  ument of a gramm
314a6 61 72 2d 73 70 65 63 69 66 69 65 64 20 74 79 70  ar-specified typ
314a7 65 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a  e..** </ul>.**.*
314a8 2a 20 4f 75 74 70 75 74 73 3a 0a 2a 2a 20 4e 6f  * Outputs:.** No
314a9 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ne..*/.SQLITE_PR
314aa 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
314ab 65 33 50 61 72 73 65 72 28 0a 20 20 76 6f 69 64  e3Parser(.  void
314ac 20 2a 79 79 70 2c 20 20 20 20 20 20 20 20 20 20   *yyp,          
314ad 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
314ae 70 61 72 73 65 72 20 2a 2f 0a 20 20 69 6e 74 20  parser */.  int 
314af 79 79 6d 61 6a 6f 72 2c 20 20 20 20 20 20 20 20  yymajor,        
314b0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
314b1 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 63 6f 64 65  major token code
314b2 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73 71 6c   number */.  sql
314b3 69 74 65 33 50 61 72 73 65 72 54 4f 4b 45 4e 54  ite3ParserTOKENT
314b4 59 50 45 20 79 79 6d 69 6e 6f 72 20 20 20 20 20  YPE yyminor     
314b5 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 66    /* The value f
314b6 6f 72 20 74 68 65 20 74 6f 6b 65 6e 20 2a 2f 0a  or the token */.
314b7 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
314b8 52 47 5f 50 44 45 43 4c 20 20 20 20 20 20 20 20  RG_PDECL        
314b9 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e         /* Option
314ba 61 6c 20 25 65 78 74 72 61 5f 61 72 67 75 6d 65  al %extra_argume
314bb 6e 74 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a  nt parameter */.
314bc 29 7b 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50 45  ){.  YYMINORTYPE
314bd 20 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 3b 0a 20   yyminorunion;. 
314be 20 69 6e 74 20 79 79 61 63 74 3b 20 20 20 20 20   int yyact;     
314bf 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
314c0 72 73 65 72 20 61 63 74 69 6f 6e 2e 20 2a 2f 0a  rser action. */.
314c1 20 20 69 6e 74 20 79 79 65 6e 64 6f 66 69 6e 70    int yyendofinp
314c2 75 74 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20  ut;     /* True 
314c3 69 66 20 77 65 20 61 72 65 20 61 74 20 74 68 65  if we are at the
314c4 20 65 6e 64 20 6f 66 20 69 6e 70 75 74 20 2a 2f   end of input */
314c5 0a 23 69 66 64 65 66 20 59 59 45 52 52 4f 52 53  .#ifdef YYERRORS
314c6 59 4d 42 4f 4c 0a 20 20 69 6e 74 20 79 79 65 72  YMBOL.  int yyer
314c7 72 6f 72 68 69 74 20 3d 20 30 3b 20 20 20 2f 2a  rorhit = 0;   /*
314c8 20 54 72 75 65 20 69 66 20 79 79 6d 61 6a 6f 72   True if yymajor
314c9 20 68 61 73 20 69 6e 76 6f 6b 65 64 20 61 6e 20   has invoked an 
314ca 65 72 72 6f 72 20 2a 2f 0a 23 65 6e 64 69 66 0a  error */.#endif.
314cb 20 20 79 79 50 61 72 73 65 72 20 2a 79 79 70 50    yyParser *yypP
314cc 61 72 73 65 72 3b 20 20 2f 2a 20 54 68 65 20 70  arser;  /* The p
314cd 61 72 73 65 72 20 2a 2f 0a 0a 20 20 2f 2a 20 28  arser */..  /* (
314ce 72 65 29 69 6e 69 74 69 61 6c 69 7a 65 20 74 68  re)initialize th
314cf 65 20 70 61 72 73 65 72 2c 20 69 66 20 6e 65 63  e parser, if nec
314d0 65 73 73 61 72 79 20 2a 2f 0a 20 20 79 79 70 50  essary */.  yypP
314d1 61 72 73 65 72 20 3d 20 28 79 79 50 61 72 73 65  arser = (yyParse
314d2 72 2a 29 79 79 70 3b 0a 20 20 69 66 28 20 79 79  r*)yyp;.  if( yy
314d3 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3c 30  pParser->yyidx<0
314d4 20 29 7b 0a 23 69 66 20 59 59 53 54 41 43 4b 44   ){.#if YYSTACKD
314d5 45 50 54 48 3c 3d 30 0a 20 20 20 20 69 66 28 20  EPTH<=0.    if( 
314d6 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 6b  yypParser->yystk
314d7 73 7a 20 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20  sz <=0 ){.      
314d8 2f 2a 6d 65 6d 73 65 74 28 26 79 79 6d 69 6e 6f  /*memset(&yymino
314d9 72 75 6e 69 6f 6e 2c 20 30 2c 20 73 69 7a 65 6f  runion, 0, sizeo
314da 66 28 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 29  f(yyminorunion))
314db 3b 2a 2f 0a 20 20 20 20 20 20 79 79 6d 69 6e 6f  ;*/.      yymino
314dc 72 75 6e 69 6f 6e 20 3d 20 79 79 7a 65 72 6f 6d  runion = yyzerom
314dd 69 6e 6f 72 3b 0a 20 20 20 20 20 20 79 79 53 74  inor;.      yySt
314de 61 63 6b 4f 76 65 72 66 6c 6f 77 28 79 79 70 50  ackOverflow(yypP
314df 61 72 73 65 72 2c 20 26 79 79 6d 69 6e 6f 72 75  arser, &yyminoru
314e0 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 72 65 74  nion);.      ret
314e1 75 72 6e 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  urn;.    }.#endi
314e2 66 0a 20 20 20 20 79 79 70 50 61 72 73 65 72 2d  f.    yypParser-
314e3 3e 79 79 69 64 78 20 3d 20 30 3b 0a 20 20 20 20  >yyidx = 0;.    
314e4 79 79 70 50 61 72 73 65 72 2d 3e 79 79 65 72 72  yypParser->yyerr
314e5 63 6e 74 20 3d 20 2d 31 3b 0a 20 20 20 20 79 79  cnt = -1;.    yy
314e6 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b  pParser->yystack
314e7 5b 30 5d 2e 73 74 61 74 65 6e 6f 20 3d 20 30 3b  [0].stateno = 0;
314e8 0a 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e  .    yypParser->
314e9 79 79 73 74 61 63 6b 5b 30 5d 2e 6d 61 6a 6f 72  yystack[0].major
314ea 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 79 79 6d 69   = 0;.  }.  yymi
314eb 6e 6f 72 75 6e 69 6f 6e 2e 79 79 30 20 3d 20 79  norunion.yy0 = y
314ec 79 6d 69 6e 6f 72 3b 0a 20 20 79 79 65 6e 64 6f  yminor;.  yyendo
314ed 66 69 6e 70 75 74 20 3d 20 28 79 79 6d 61 6a 6f  finput = (yymajo
314ee 72 3d 3d 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  r==0);.  sqlite3
314ef 50 61 72 73 65 72 41 52 47 5f 53 54 4f 52 45 3b  ParserARG_STORE;
314f0 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
314f1 0a 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49  .  if( yyTraceFI
314f2 4c 45 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74  LE ){.    fprint
314f3 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25  f(yyTraceFILE,"%
314f4 73 49 6e 70 75 74 20 25 73 5c 6e 22 2c 79 79 54  sInput %s\n",yyT
314f5 72 61 63 65 50 72 6f 6d 70 74 2c 79 79 54 6f 6b  racePrompt,yyTok
314f6 65 6e 4e 61 6d 65 5b 79 79 6d 61 6a 6f 72 5d 29  enName[yymajor])
314f7 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
314f8 64 6f 7b 0a 20 20 20 20 79 79 61 63 74 20 3d 20  do{.    yyact = 
314f9 79 79 5f 66 69 6e 64 5f 73 68 69 66 74 5f 61 63  yy_find_shift_ac
314fa 74 69 6f 6e 28 79 79 70 50 61 72 73 65 72 2c 28  tion(yypParser,(
314fb 59 59 43 4f 44 45 54 59 50 45 29 79 79 6d 61 6a  YYCODETYPE)yymaj
314fc 6f 72 29 3b 0a 20 20 20 20 69 66 28 20 79 79 61  or);.    if( yya
314fd 63 74 3c 59 59 4e 53 54 41 54 45 20 29 7b 0a 20  ct<YYNSTATE ){. 
314fe 20 20 20 20 20 61 73 73 65 72 74 28 20 21 79 79       assert( !yy
314ff 65 6e 64 6f 66 69 6e 70 75 74 20 29 3b 20 20 2f  endofinput );  /
31500 2a 20 49 6d 70 6f 73 73 69 62 6c 65 20 74 6f 20  * Impossible to 
31501 73 68 69 66 74 20 74 68 65 20 24 20 74 6f 6b 65  shift the $ toke
31502 6e 20 2a 2f 0a 20 20 20 20 20 20 79 79 5f 73 68  n */.      yy_sh
31503 69 66 74 28 79 79 70 50 61 72 73 65 72 2c 79 79  ift(yypParser,yy
31504 61 63 74 2c 79 79 6d 61 6a 6f 72 2c 26 79 79 6d  act,yymajor,&yym
31505 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20  inorunion);.    
31506 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 65    yypParser->yye
31507 72 72 63 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 79  rrcnt--;.      y
31508 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f 44  ymajor = YYNOCOD
31509 45 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  E;.    }else if(
3150a 20 79 79 61 63 74 20 3c 20 59 59 4e 53 54 41 54   yyact < YYNSTAT
3150b 45 20 2b 20 59 59 4e 52 55 4c 45 20 29 7b 0a 20  E + YYNRULE ){. 
3150c 20 20 20 20 20 79 79 5f 72 65 64 75 63 65 28 79       yy_reduce(y
3150d 79 70 50 61 72 73 65 72 2c 79 79 61 63 74 2d 59  ypParser,yyact-Y
3150e 59 4e 53 54 41 54 45 29 3b 0a 20 20 20 20 7d 65  YNSTATE);.    }e
3150f 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72  lse{.      asser
31510 74 28 20 79 79 61 63 74 20 3d 3d 20 59 59 5f 45  t( yyact == YY_E
31511 52 52 4f 52 5f 41 43 54 49 4f 4e 20 29 3b 0a 23  RROR_ACTION );.#
31512 69 66 64 65 66 20 59 59 45 52 52 4f 52 53 59 4d  ifdef YYERRORSYM
31513 42 4f 4c 0a 20 20 20 20 20 20 69 6e 74 20 79 79  BOL.      int yy
31514 6d 78 3b 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64  mx;.#endif.#ifnd
31515 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 20 20  ef NDEBUG.      
31516 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20  if( yyTraceFILE 
31517 29 7b 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e  ){.        fprin
31518 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22  tf(yyTraceFILE,"
31519 25 73 53 79 6e 74 61 78 20 45 72 72 6f 72 21 5c  %sSyntax Error!\
3151a 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74  n",yyTracePrompt
3151b 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  );.      }.#endi
3151c 66 0a 23 69 66 64 65 66 20 59 59 45 52 52 4f 52  f.#ifdef YYERROR
3151d 53 59 4d 42 4f 4c 0a 20 20 20 20 20 20 2f 2a 20  SYMBOL.      /* 
3151e 41 20 73 79 6e 74 61 78 20 65 72 72 6f 72 20 68  A syntax error h
3151f 61 73 20 6f 63 63 75 72 72 65 64 2e 0a 20 20 20  as occurred..   
31520 20 20 20 2a 2a 20 54 68 65 20 72 65 73 70 6f 6e     ** The respon
31521 73 65 20 74 6f 20 61 6e 20 65 72 72 6f 72 20 64  se to an error d
31522 65 70 65 6e 64 73 20 75 70 6f 6e 20 77 68 65 74  epends upon whet
31523 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 0a 20  her or not the. 
31524 20 20 20 20 20 2a 2a 20 67 72 61 6d 6d 61 72 20       ** grammar 
31525 64 65 66 69 6e 65 73 20 61 6e 20 65 72 72 6f 72  defines an error
31526 20 74 6f 6b 65 6e 20 22 45 52 52 4f 52 22 2e 20   token "ERROR". 
31527 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20   .      **.     
31528 20 2a 2a 20 54 68 69 73 20 69 73 20 77 68 61 74   ** This is what
31529 20 77 65 20 64 6f 20 69 66 20 74 68 65 20 67 72   we do if the gr
3152a 61 6d 6d 61 72 20 64 6f 65 73 20 64 65 66 69 6e  ammar does defin
3152b 65 20 45 52 52 4f 52 3a 0a 20 20 20 20 20 20 2a  e ERROR:.      *
3152c 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 43 61  *.      **  * Ca
3152d 6c 6c 20 74 68 65 20 25 73 79 6e 74 61 78 5f 65  ll the %syntax_e
3152e 72 72 6f 72 20 66 75 6e 63 74 69 6f 6e 2e 0a 20  rror function.. 
3152f 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
31530 20 20 2a 20 42 65 67 69 6e 20 70 6f 70 70 69 6e    * Begin poppin
31531 67 20 74 68 65 20 73 74 61 63 6b 20 75 6e 74 69  g the stack unti
31532 6c 20 77 65 20 65 6e 74 65 72 20 61 20 73 74 61  l we enter a sta
31533 74 65 20 77 68 65 72 65 0a 20 20 20 20 20 20 2a  te where.      *
31534 2a 20 20 20 20 69 74 20 69 73 20 6c 65 67 61 6c  *    it is legal
31535 20 74 6f 20 73 68 69 66 74 20 74 68 65 20 65 72   to shift the er
31536 72 6f 72 20 73 79 6d 62 6f 6c 2c 20 74 68 65 6e  ror symbol, then
31537 20 73 68 69 66 74 0a 20 20 20 20 20 20 2a 2a 20   shift.      ** 
31538 20 20 20 74 68 65 20 65 72 72 6f 72 20 73 79 6d     the error sym
31539 62 6f 6c 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20  bol..      **.  
3153a 20 20 20 20 2a 2a 20 20 2a 20 53 65 74 20 74 68      **  * Set th
3153b 65 20 65 72 72 6f 72 20 63 6f 75 6e 74 20 74 6f  e error count to
3153c 20 74 68 72 65 65 2e 0a 20 20 20 20 20 20 2a 2a   three..      **
3153d 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 42 65 67  .      **  * Beg
3153e 69 6e 20 61 63 63 65 70 74 69 6e 67 20 61 6e 64  in accepting and
3153f 20 73 68 69 66 74 69 6e 67 20 6e 65 77 20 74 6f   shifting new to
31540 6b 65 6e 73 2e 20 20 4e 6f 20 6e 65 77 20 65 72  kens.  No new er
31541 72 6f 72 0a 20 20 20 20 20 20 2a 2a 20 20 20 20  ror.      **    
31542 70 72 6f 63 65 73 73 69 6e 67 20 77 69 6c 6c 20  processing will 
31543 6f 63 63 75 72 20 75 6e 74 69 6c 20 74 68 72 65  occur until thre
31544 65 20 74 6f 6b 65 6e 73 20 68 61 76 65 20 62 65  e tokens have be
31545 65 6e 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 73  en.      **    s
31546 68 69 66 74 65 64 20 73 75 63 63 65 73 73 66 75  hifted successfu
31547 6c 6c 79 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20  lly..      **.  
31548 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
31549 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 65 72   yypParser->yyer
3154a 72 63 6e 74 3c 30 20 29 7b 0a 20 20 20 20 20 20  rcnt<0 ){.      
3154b 20 20 79 79 5f 73 79 6e 74 61 78 5f 65 72 72 6f    yy_syntax_erro
3154c 72 28 79 79 70 50 61 72 73 65 72 2c 79 79 6d 61  r(yypParser,yyma
3154d 6a 6f 72 2c 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e  jor,yyminorunion
3154e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
3154f 20 79 79 6d 78 20 3d 20 79 79 70 50 61 72 73 65   yymx = yypParse
31550 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79 70 50 61  r->yystack[yypPa
31551 72 73 65 72 2d 3e 79 79 69 64 78 5d 2e 6d 61 6a  rser->yyidx].maj
31552 6f 72 3b 0a 20 20 20 20 20 20 69 66 28 20 79 79  or;.      if( yy
31553 6d 78 3d 3d 59 59 45 52 52 4f 52 53 59 4d 42 4f  mx==YYERRORSYMBO
31554 4c 20 7c 7c 20 79 79 65 72 72 6f 72 68 69 74 20  L || yyerrorhit 
31555 29 7b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  ){.#ifndef NDEBU
31556 47 0a 20 20 20 20 20 20 20 20 69 66 28 20 79 79  G.        if( yy
31557 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20  TraceFILE ){.   
31558 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 79         fprintf(y
31559 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73 44 69  yTraceFILE,"%sDi
3155a 73 63 61 72 64 20 69 6e 70 75 74 20 74 6f 6b 65  scard input toke
3155b 6e 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20  n %s\n",.       
3155c 20 20 20 20 20 20 79 79 54 72 61 63 65 50 72 6f        yyTracePro
3155d 6d 70 74 2c 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b  mpt,yyTokenName[
3155e 79 79 6d 61 6a 6f 72 5d 29 3b 0a 20 20 20 20 20  yymajor]);.     
3155f 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
31560 20 20 20 20 79 79 5f 64 65 73 74 72 75 63 74 6f      yy_destructo
31561 72 28 79 79 70 50 61 72 73 65 72 2c 20 28 59 59  r(yypParser, (YY
31562 43 4f 44 45 54 59 50 45 29 79 79 6d 61 6a 6f 72  CODETYPE)yymajor
31563 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b  ,&yyminorunion);
31564 0a 20 20 20 20 20 20 20 20 79 79 6d 61 6a 6f 72  .        yymajor
31565 20 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a 20 20 20   = YYNOCODE;.   
31566 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
31567 20 20 20 77 68 69 6c 65 28 0a 20 20 20 20 20 20     while(.      
31568 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79      yypParser->y
31569 79 69 64 78 20 3e 3d 20 30 20 26 26 0a 20 20 20  yidx >= 0 &&.   
3156a 20 20 20 20 20 20 20 79 79 6d 78 20 21 3d 20 59         yymx != Y
3156b 59 45 52 52 4f 52 53 59 4d 42 4f 4c 20 26 26 0a  YERRORSYMBOL &&.
3156c 20 20 20 20 20 20 20 20 20 20 28 79 79 61 63 74            (yyact
3156d 20 3d 20 79 79 5f 66 69 6e 64 5f 72 65 64 75 63   = yy_find_reduc
3156e 65 5f 61 63 74 69 6f 6e 28 0a 20 20 20 20 20 20  e_action(.      
3156f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31570 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73    yypParser->yys
31571 74 61 63 6b 5b 79 79 70 50 61 72 73 65 72 2d 3e  tack[yypParser->
31572 79 79 69 64 78 5d 2e 73 74 61 74 65 6e 6f 2c 0a  yyidx].stateno,.
31573 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31574 20 20 20 20 20 20 20 20 59 59 45 52 52 4f 52 53          YYERRORS
31575 59 4d 42 4f 4c 29 29 20 3e 3d 20 59 59 4e 53 54  YMBOL)) >= YYNST
31576 41 54 45 0a 20 20 20 20 20 20 20 20 29 7b 0a 20  ATE.        ){. 
31577 20 20 20 20 20 20 20 20 20 79 79 5f 70 6f 70 5f           yy_pop_
31578 70 61 72 73 65 72 5f 73 74 61 63 6b 28 79 79 70  parser_stack(yyp
31579 50 61 72 73 65 72 29 3b 0a 20 20 20 20 20 20 20  Parser);.       
3157a 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 79   }.        if( y
3157b 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 20  ypParser->yyidx 
3157c 3c 20 30 20 7c 7c 20 79 79 6d 61 6a 6f 72 3d 3d  < 0 || yymajor==
3157d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 79  0 ){.          y
3157e 79 5f 64 65 73 74 72 75 63 74 6f 72 28 79 79 70  y_destructor(yyp
3157f 50 61 72 73 65 72 2c 28 59 59 43 4f 44 45 54 59  Parser,(YYCODETY
31580 50 45 29 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69  PE)yymajor,&yymi
31581 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20  norunion);.     
31582 20 20 20 20 20 79 79 5f 70 61 72 73 65 5f 66 61       yy_parse_fa
31583 69 6c 65 64 28 79 79 70 50 61 72 73 65 72 29 3b  iled(yypParser);
31584 0a 20 20 20 20 20 20 20 20 20 20 79 79 6d 61 6a  .          yymaj
31585 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a 20  or = YYNOCODE;. 
31586 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
31587 20 79 79 6d 78 21 3d 59 59 45 52 52 4f 52 53 59   yymx!=YYERRORSY
31588 4d 42 4f 4c 20 29 7b 0a 20 20 20 20 20 20 20 20  MBOL ){.        
31589 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 75 32    YYMINORTYPE u2
3158a 3b 0a 20 20 20 20 20 20 20 20 20 20 75 32 2e 59  ;.          u2.Y
3158b 59 45 52 52 53 59 4d 44 54 20 3d 20 30 3b 0a 20  YERRSYMDT = 0;. 
3158c 20 20 20 20 20 20 20 20 20 79 79 5f 73 68 69 66           yy_shif
3158d 74 28 79 79 70 50 61 72 73 65 72 2c 79 79 61 63  t(yypParser,yyac
3158e 74 2c 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 2c  t,YYERRORSYMBOL,
3158f 26 75 32 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  &u2);.        }.
31590 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 79 79        }.      yy
31591 70 50 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e  pParser->yyerrcn
31592 74 20 3d 20 33 3b 0a 20 20 20 20 20 20 79 79 65  t = 3;.      yye
31593 72 72 6f 72 68 69 74 20 3d 20 31 3b 0a 23 65 6c  rrorhit = 1;.#el
31594 69 66 20 64 65 66 69 6e 65 64 28 59 59 4e 4f 45  if defined(YYNOE
31595 52 52 4f 52 52 45 43 4f 56 45 52 59 29 0a 20 20  RRORRECOVERY).  
31596 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 59 59      /* If the YY
31597 4e 4f 45 52 52 4f 52 52 45 43 4f 56 45 52 59 20  NOERRORRECOVERY 
31598 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64  macro is defined
31599 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 61 74  , then do not at
3159a 74 65 6d 70 74 20 74 6f 0a 20 20 20 20 20 20 2a  tempt to.      *
3159b 2a 20 64 6f 20 61 6e 79 20 6b 69 6e 64 20 6f 66  * do any kind of
3159c 20 65 72 72 6f 72 20 72 65 63 6f 76 65 72 79 2e   error recovery.
3159d 20 20 49 6e 73 74 65 61 64 2c 20 73 69 6d 70 6c    Instead, simpl
3159e 79 20 69 6e 76 6f 6b 65 20 74 68 65 20 73 79 6e  y invoke the syn
3159f 74 61 78 0a 20 20 20 20 20 20 2a 2a 20 65 72 72  tax.      ** err
315a0 6f 72 20 72 6f 75 74 69 6e 65 20 61 6e 64 20 63  or routine and c
315a1 6f 6e 74 69 6e 75 65 20 67 6f 69 6e 67 20 61 73  ontinue going as
315a2 20 69 66 20 6e 6f 74 68 69 6e 67 20 68 61 64 20   if nothing had 
315a3 68 61 70 70 65 6e 65 64 2e 0a 20 20 20 20 20 20  happened..      
315a4 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41 70 70 6c  **.      ** Appl
315a5 69 63 61 74 69 6f 6e 73 20 63 61 6e 20 73 65 74  ications can set
315a6 20 74 68 69 73 20 6d 61 63 72 6f 20 28 66 6f 72   this macro (for
315a7 20 65 78 61 6d 70 6c 65 20 69 6e 73 69 64 65 20   example inside 
315a8 25 69 6e 63 6c 75 64 65 29 20 69 66 0a 20 20 20  %include) if.   
315a9 20 20 20 2a 2a 20 74 68 65 79 20 69 6e 74 65 6e     ** they inten
315aa 64 20 74 6f 20 61 62 61 6e 64 6f 6e 20 74 68 65  d to abandon the
315ab 20 70 61 72 73 65 20 75 70 6f 6e 20 74 68 65 20   parse upon the 
315ac 66 69 72 73 74 20 73 79 6e 74 61 78 20 65 72 72  first syntax err
315ad 6f 72 20 73 65 65 6e 2e 0a 20 20 20 20 20 20 2a  or seen..      *
315ae 2f 0a 20 20 20 20 20 20 79 79 5f 73 79 6e 74 61  /.      yy_synta
315af 78 5f 65 72 72 6f 72 28 79 79 70 50 61 72 73 65  x_error(yypParse
315b0 72 2c 79 79 6d 61 6a 6f 72 2c 79 79 6d 69 6e 6f  r,yymajor,yymino
315b1 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 79  runion);.      y
315b2 79 5f 64 65 73 74 72 75 63 74 6f 72 28 79 79 70  y_destructor(yyp
315b3 50 61 72 73 65 72 2c 28 59 59 43 4f 44 45 54 59  Parser,(YYCODETY
315b4 50 45 29 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69  PE)yymajor,&yymi
315b5 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20  norunion);.     
315b6 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43   yymajor = YYNOC
315b7 4f 44 45 3b 0a 20 20 20 20 20 20 0a 23 65 6c 73  ODE;.      .#els
315b8 65 20 20 2f 2a 20 59 59 45 52 52 4f 52 53 59 4d  e  /* YYERRORSYM
315b9 42 4f 4c 20 69 73 20 6e 6f 74 20 64 65 66 69 6e  BOL is not defin
315ba 65 64 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 54  ed */.      /* T
315bb 68 69 73 20 69 73 20 77 68 61 74 20 77 65 20 64  his is what we d
315bc 6f 20 69 66 20 74 68 65 20 67 72 61 6d 6d 61 72  o if the grammar
315bd 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e 65   does not define
315be 20 45 52 52 4f 52 3a 0a 20 20 20 20 20 20 2a 2a   ERROR:.      **
315bf 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 52 65 70  .      **  * Rep
315c0 6f 72 74 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ort an error mes
315c1 73 61 67 65 2c 20 61 6e 64 20 74 68 72 6f 77 20  sage, and throw 
315c2 61 77 61 79 20 74 68 65 20 69 6e 70 75 74 20 74  away the input t
315c3 6f 6b 65 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20  oken..      **. 
315c4 20 20 20 20 20 2a 2a 20 20 2a 20 49 66 20 74 68       **  * If th
315c5 65 20 69 6e 70 75 74 20 74 6f 6b 65 6e 20 69 73  e input token is
315c6 20 24 2c 20 74 68 65 6e 20 66 61 69 6c 20 74 68   $, then fail th
315c7 65 20 70 61 72 73 65 2e 0a 20 20 20 20 20 20 2a  e parse..      *
315c8 2a 0a 20 20 20 20 20 20 2a 2a 20 41 73 20 62 65  *.      ** As be
315c9 66 6f 72 65 2c 20 73 75 62 73 65 71 75 65 6e 74  fore, subsequent
315ca 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20   error messages 
315cb 61 72 65 20 73 75 70 70 72 65 73 73 65 64 20 75  are suppressed u
315cc 6e 74 69 6c 0a 20 20 20 20 20 20 2a 2a 20 74 68  ntil.      ** th
315cd 72 65 65 20 69 6e 70 75 74 20 74 6f 6b 65 6e 73  ree input tokens
315ce 20 68 61 76 65 20 62 65 65 6e 20 73 75 63 63 65   have been succe
315cf 73 73 66 75 6c 6c 79 20 73 68 69 66 74 65 64 2e  ssfully shifted.
315d0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
315d1 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79  if( yypParser->y
315d2 79 65 72 72 63 6e 74 3c 3d 30 20 29 7b 0a 20 20  yerrcnt<=0 ){.  
315d3 20 20 20 20 20 20 79 79 5f 73 79 6e 74 61 78 5f        yy_syntax_
315d4 65 72 72 6f 72 28 79 79 70 50 61 72 73 65 72 2c  error(yypParser,
315d5 79 79 6d 61 6a 6f 72 2c 79 79 6d 69 6e 6f 72 75  yymajor,yyminoru
315d6 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20  nion);.      }. 
315d7 20 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e       yypParser->
315d8 79 79 65 72 72 63 6e 74 20 3d 20 33 3b 0a 20 20  yyerrcnt = 3;.  
315d9 20 20 20 20 79 79 5f 64 65 73 74 72 75 63 74 6f      yy_destructo
315da 72 28 79 79 70 50 61 72 73 65 72 2c 28 59 59 43  r(yypParser,(YYC
315db 4f 44 45 54 59 50 45 29 79 79 6d 61 6a 6f 72 2c  ODETYPE)yymajor,
315dc 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a  &yyminorunion);.
315dd 20 20 20 20 20 20 69 66 28 20 79 79 65 6e 64 6f        if( yyendo
315de 66 69 6e 70 75 74 20 29 7b 0a 20 20 20 20 20 20  finput ){.      
315df 20 20 79 79 5f 70 61 72 73 65 5f 66 61 69 6c 65    yy_parse_faile
315e0 64 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20  d(yypParser);.  
315e1 20 20 20 20 7d 0a 20 20 20 20 20 20 79 79 6d 61      }.      yyma
315e2 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a  jor = YYNOCODE;.
315e3 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 7d  #endif.    }.  }
315e4 77 68 69 6c 65 28 20 79 79 6d 61 6a 6f 72 21 3d  while( yymajor!=
315e5 59 59 4e 4f 43 4f 44 45 20 26 26 20 79 79 70 50  YYNOCODE && yypP
315e6 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20  arser->yyidx>=0 
315e7 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a  );.  return;.}..
315e8 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
315e9 45 6e 64 20 6f 66 20 70 61 72 73 65 2e 63 20 2a  End of parse.c *
315ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
315eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
315ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
315ed 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
315ee 42 65 67 69 6e 20 66 69 6c 65 20 74 6f 6b 65 6e  Begin file token
315ef 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ize.c **********
315f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
315f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
315f2 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
315f3 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
315f4 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
315f5 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
315f6 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
315f7 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
315f8 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
315f9 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
315fa 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
315fb 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
315fc 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
315fd 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
315fe 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
315ff 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
31600 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
31601 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
31602 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
31603 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
31604 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
31605 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31606 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31607 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31608 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31609 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 41 6e 20 74 6f 6b  ******.** An tok
3160a 65 6e 69 7a 65 72 20 66 6f 72 20 53 51 4c 0a 2a  enizer for SQL.*
3160b 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
3160c 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 74  ontains C code t
3160d 68 61 74 20 73 70 6c 69 74 73 20 61 6e 20 53 51  hat splits an SQ
3160e 4c 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 75  L input string u
3160f 70 20 69 6e 74 6f 0a 2a 2a 20 69 6e 64 69 76 69  p into.** indivi
31610 64 75 61 6c 20 74 6f 6b 65 6e 73 20 61 6e 64 20  dual tokens and 
31611 73 65 6e 64 73 20 74 68 6f 73 65 20 74 6f 6b 65  sends those toke
31612 6e 73 20 6f 6e 65 2d 62 79 2d 6f 6e 65 20 6f 76  ns one-by-one ov
31613 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 70 61 72  er to the.** par
31614 73 65 72 20 66 6f 72 20 61 6e 61 6c 79 73 69 73  ser for analysis
31615 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 74 6f 6b  ..**.** $Id: tok
31616 65 6e 69 7a 65 2e 63 2c 76 20 31 2e 31 36 33 20  enize.c,v 1.163 
31617 32 30 30 39 2f 30 37 2f 30 33 20 32 32 3a 35 34  2009/07/03 22:54
31618 3a 33 37 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :37 drh Exp $.*/
31619 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 68 61 72  ../*.** The char
3161a 4d 61 70 28 29 20 6d 61 63 72 6f 20 6d 61 70 73  Map() macro maps
3161b 20 61 6c 70 68 61 62 65 74 69 63 20 63 68 61 72   alphabetic char
3161c 61 63 74 65 72 73 20 69 6e 74 6f 20 74 68 65 69  acters into thei
3161d 72 0a 2a 2a 20 6c 6f 77 65 72 2d 63 61 73 65 20  r.** lower-case 
3161e 41 53 43 49 49 20 65 71 75 69 76 61 6c 65 6e 74  ASCII equivalent
3161f 2e 20 20 4f 6e 20 41 53 43 49 49 20 6d 61 63 68  .  On ASCII mach
31620 69 6e 65 73 2c 20 74 68 69 73 20 69 73 20 6a 75  ines, this is ju
31621 73 74 0a 2a 2a 20 61 6e 20 75 70 70 65 72 2d 74  st.** an upper-t
31622 6f 2d 6c 6f 77 65 72 20 63 61 73 65 20 6d 61 70  o-lower case map
31623 2e 20 20 4f 6e 20 45 42 43 44 49 43 20 6d 61 63  .  On EBCDIC mac
31624 68 69 6e 65 73 20 77 65 20 61 6c 73 6f 20 6e 65  hines we also ne
31625 65 64 0a 2a 2a 20 74 6f 20 61 64 6a 75 73 74 20  ed.** to adjust 
31626 74 68 65 20 65 6e 63 6f 64 69 6e 67 2e 20 20 4f  the encoding.  O
31627 6e 6c 79 20 61 6c 70 68 61 62 65 74 69 63 20 63  nly alphabetic c
31628 68 61 72 61 63 74 65 72 73 20 61 6e 64 20 75 6e  haracters and un
31629 64 65 72 73 63 6f 72 65 73 0a 2a 2a 20 6e 65 65  derscores.** nee
3162a 64 20 74 6f 20 62 65 20 74 72 61 6e 73 6c 61 74  d to be translat
3162b 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ed..*/.#ifdef SQ
3162c 4c 49 54 45 5f 41 53 43 49 49 0a 23 20 64 65 66  LITE_ASCII.# def
3162d 69 6e 65 20 63 68 61 72 4d 61 70 28 58 29 20 73  ine charMap(X) s
3162e 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77  qlite3UpperToLow
3162f 65 72 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61  er[(unsigned cha
31630 72 29 58 5d 0a 23 65 6e 64 69 66 0a 23 69 66 64  r)X].#endif.#ifd
31631 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43  ef SQLITE_EBCDIC
31632 0a 23 20 64 65 66 69 6e 65 20 63 68 61 72 4d 61  .# define charMa
31633 70 28 58 29 20 65 62 63 64 69 63 54 6f 41 73 63  p(X) ebcdicToAsc
31634 69 69 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61  ii[(unsigned cha
31635 72 29 58 5d 0a 63 6f 6e 73 74 20 75 6e 73 69 67  r)X].const unsig
31636 6e 65 64 20 63 68 61 72 20 65 62 63 64 69 63 54  ned char ebcdicT
31637 6f 41 73 63 69 69 5b 5d 20 3d 20 7b 0a 2f 2a 20  oAscii[] = {./* 
31638 30 20 20 20 31 20 20 20 32 20 20 20 33 20 20 20  0   1   2   3   
31639 34 20 20 20 35 20 20 20 36 20 20 20 37 20 20 20  4   5   6   7   
3163a 38 20 20 20 39 20 20 20 41 20 20 20 42 20 20 20  8   9   A   B   
3163b 43 20 20 20 44 20 20 20 45 20 20 20 46 20 2a 2f  C   D   E   F */
3163c 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  .   0,  0,  0,  
3163d 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3163e 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3163f 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31640 30 2c 20 20 2f 2a 20 30 78 20 2a 2f 0a 20 20 20  0,  /* 0x */.   
31641 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31642 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31643 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31644 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31645 2f 2a 20 31 78 20 2a 2f 0a 20 20 20 30 2c 20 20  /* 1x */.   0,  
31646 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31647 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31648 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31649 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 32  0,  0,  0,  /* 2
3164a 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20  x */.   0,  0,  
3164b 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3164c 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3164d 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3164e 30 2c 20 20 30 2c 20 20 2f 2a 20 33 78 20 2a 2f  0,  0,  /* 3x */
3164f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  .   0,  0,  0,  
31650 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31651 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31652 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31653 30 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20  0,  /* 4x */.   
31654 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31655 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31656 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31657 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31658 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 30 2c 20 20  /* 5x */.   0,  
31659 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3165a 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3165b 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 39  0,  0,  0,  0, 9
3165c 35 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 36  5,  0,  0,  /* 6
3165d 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20  x */.   0,  0,  
3165e 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3165f 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31660 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31661 30 2c 20 20 30 2c 20 20 2f 2a 20 37 78 20 2a 2f  0,  0,  /* 7x */
31662 0a 20 20 20 30 2c 20 39 37 2c 20 39 38 2c 20 39  .   0, 97, 98, 9
31663 39 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c 31 30  9,100,101,102,10
31664 33 2c 31 30 34 2c 31 30 35 2c 20 20 30 2c 20 20  3,104,105,  0,  
31665 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31666 30 2c 20 20 2f 2a 20 38 78 20 2a 2f 0a 20 20 20  0,  /* 8x */.   
31667 30 2c 31 30 36 2c 31 30 37 2c 31 30 38 2c 31 30  0,106,107,108,10
31668 39 2c 31 31 30 2c 31 31 31 2c 31 31 32 2c 31 31  9,110,111,112,11
31669 33 2c 31 31 34 2c 20 20 30 2c 20 20 30 2c 20 20  3,114,  0,  0,  
3166a 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3166b 2f 2a 20 39 78 20 2a 2f 0a 20 20 20 30 2c 20 20  /* 9x */.   0,  
3166c 30 2c 31 31 35 2c 31 31 36 2c 31 31 37 2c 31 31  0,115,116,117,11
3166d 38 2c 31 31 39 2c 31 32 30 2c 31 32 31 2c 31 32  8,119,120,121,12
3166e 32 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  2,  0,  0,  0,  
3166f 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 41  0,  0,  0,  /* A
31670 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20  x */.   0,  0,  
31671 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31672 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31673 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31674 30 2c 20 20 30 2c 20 20 2f 2a 20 42 78 20 2a 2f  0,  0,  /* Bx */
31675 0a 20 20 20 30 2c 20 39 37 2c 20 39 38 2c 20 39  .   0, 97, 98, 9
31676 39 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c 31 30  9,100,101,102,10
31677 33 2c 31 30 34 2c 31 30 35 2c 20 20 30 2c 20 20  3,104,105,  0,  
31678 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31679 30 2c 20 20 2f 2a 20 43 78 20 2a 2f 0a 20 20 20  0,  /* Cx */.   
3167a 30 2c 31 30 36 2c 31 30 37 2c 31 30 38 2c 31 30  0,106,107,108,10
3167b 39 2c 31 31 30 2c 31 31 31 2c 31 31 32 2c 31 31  9,110,111,112,11
3167c 33 2c 31 31 34 2c 20 20 30 2c 20 20 30 2c 20 20  3,114,  0,  0,  
3167d 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3167e 2f 2a 20 44 78 20 2a 2f 0a 20 20 20 30 2c 20 20  /* Dx */.   0,  
3167f 30 2c 31 31 35 2c 31 31 36 2c 31 31 37 2c 31 31  0,115,116,117,11
31680 38 2c 31 31 39 2c 31 32 30 2c 31 32 31 2c 31 32  8,119,120,121,12
31681 32 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  2,  0,  0,  0,  
31682 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 45  0,  0,  0,  /* E
31683 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20  x */.   0,  0,  
31684 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31685 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31686 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31687 30 2c 20 20 30 2c 20 20 2f 2a 20 46 78 20 2a 2f  0,  0,  /* Fx */
31688 0a 7d 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  .};.#endif../*.*
31689 2a 20 54 68 65 20 73 71 6c 69 74 65 33 4b 65 79  * The sqlite3Key
3168a 77 6f 72 64 43 6f 64 65 20 66 75 6e 63 74 69 6f  wordCode functio
3168b 6e 20 6c 6f 6f 6b 73 20 75 70 20 61 6e 20 69 64  n looks up an id
3168c 65 6e 74 69 66 69 65 72 20 74 6f 20 64 65 74 65  entifier to dete
3168d 72 6d 69 6e 65 20 69 66 0a 2a 2a 20 69 74 20 69  rmine if.** it i
3168e 73 20 61 20 6b 65 79 77 6f 72 64 2e 20 20 49 66  s a keyword.  If
3168f 20 69 74 20 69 73 20 61 20 6b 65 79 77 6f 72 64   it is a keyword
31690 2c 20 74 68 65 20 74 6f 6b 65 6e 20 63 6f 64 65  , the token code
31691 20 6f 66 20 74 68 61 74 20 6b 65 79 77 6f 72 64   of that keyword
31692 20 69 73 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64   is .** returned
31693 2e 20 20 49 66 20 74 68 65 20 69 6e 70 75 74 20  .  If the input 
31694 69 73 20 6e 6f 74 20 61 20 6b 65 79 77 6f 72 64  is not a keyword
31695 2c 20 54 4b 5f 49 44 20 69 73 20 72 65 74 75 72  , TK_ID is retur
31696 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ned..**.** The i
31697 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
31698 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61   this routine wa
31699 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 61  s generated by a
3169a 20 70 72 6f 67 72 61 6d 2c 0a 2a 2a 20 6d 6b 6b   program,.** mkk
3169b 65 79 77 6f 72 64 68 61 73 68 2e 68 2c 20 6c 6f  eywordhash.h, lo
3169c 63 61 74 65 64 20 69 6e 20 74 68 65 20 74 6f 6f  cated in the too
3169d 6c 20 73 75 62 64 69 72 65 63 74 6f 72 79 20 6f  l subdirectory o
3169e 66 20 74 68 65 20 64 69 73 74 72 69 62 75 74 69  f the distributi
3169f 6f 6e 2e 0a 2a 2a 20 54 68 65 20 6f 75 74 70 75  on..** The outpu
316a0 74 20 6f 66 20 74 68 65 20 6d 6b 6b 65 79 77 6f  t of the mkkeywo
316a1 72 64 68 61 73 68 2e 63 20 70 72 6f 67 72 61 6d  rdhash.c program
316a2 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f   is written into
316a3 20 61 20 66 69 6c 65 0a 2a 2a 20 6e 61 6d 65 64   a file.** named
316a4 20 6b 65 79 77 6f 72 64 68 61 73 68 2e 68 20 61   keywordhash.h a
316a5 6e 64 20 74 68 65 6e 20 69 6e 63 6c 75 64 65 64  nd then included
316a6 20 69 6e 74 6f 20 74 68 69 73 20 73 6f 75 72 63   into this sourc
316a7 65 20 66 69 6c 65 20 62 79 0a 2a 2a 20 74 68 65  e file by.** the
316a8 20 23 69 6e 63 6c 75 64 65 20 62 65 6c 6f 77 2e   #include below.
316a9 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .*/./***********
316aa 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6b 65 79 77  *** Include keyw
316ab 6f 72 64 68 61 73 68 2e 68 20 69 6e 20 74 68 65  ordhash.h in the
316ac 20 6d 69 64 64 6c 65 20 6f 66 20 74 6f 6b 65 6e   middle of token
316ad 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ize.c **********
316ae 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
316af 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6b  *** Begin file k
316b0 65 79 77 6f 72 64 68 61 73 68 2e 68 20 2a 2a 2a  eywordhash.h ***
316b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
316b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
316b3 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 20 54 68 69 73 20  **/./***** This 
316b4 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61 75  file contains au
316b5 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65  tomatically gene
316b6 72 61 74 65 64 20 63 6f 64 65 20 2a 2a 2a 2a 2a  rated code *****
316b7 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65  *.**.** The code
316b8 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 68 61   in this file ha
316b9 73 20 62 65 65 6e 20 61 75 74 6f 6d 61 74 69 63  s been automatic
316ba 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20 62  ally generated b
316bb 79 0a 2a 2a 0a 2a 2a 20 20 20 20 20 24 48 65 61  y.**.**     $Hea
316bc 64 65 72 3a 20 2f 68 6f 6d 65 2f 64 72 68 2f 73  der: /home/drh/s
316bd 71 6c 69 74 65 2f 74 72 61 6e 73 2f 63 76 73 2f  qlite/trans/cvs/
316be 73 71 6c 69 74 65 2f 73 71 6c 69 74 65 2f 74 6f  sqlite/sqlite/to
316bf 6f 6c 2f 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68  ol/mkkeywordhash
316c0 2e 63 2c 76 20 31 2e 33 38 20 32 30 30 39 2f 30  .c,v 1.38 2009/0
316c1 36 2f 30 39 20 31 34 3a 32 37 3a 34 31 20 64 72  6/09 14:27:41 dr
316c2 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68  h Exp $.**.** Th
316c3 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66  e code in this f
316c4 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61  ile implements a
316c5 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 64   function that d
316c6 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65  etermines whethe
316c7 72 0a 2a 2a 20 6f 72 20 6e 6f 74 20 61 20 67 69  r.** or not a gi
316c8 76 65 6e 20 69 64 65 6e 74 69 66 69 65 72 20 69  ven identifier i
316c9 73 20 72 65 61 6c 6c 79 20 61 6e 20 53 51 4c 20  s really an SQL 
316ca 6b 65 79 77 6f 72 64 2e 20 20 54 68 65 20 73 61  keyword.  The sa
316cb 6d 65 20 74 68 69 6e 67 0a 2a 2a 20 6d 69 67 68  me thing.** migh
316cc 74 20 62 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64  t be implemented
316cd 20 6d 6f 72 65 20 64 69 72 65 63 74 6c 79 20 75   more directly u
316ce 73 69 6e 67 20 61 20 68 61 6e 64 2d 77 72 69 74  sing a hand-writ
316cf 74 65 6e 20 68 61 73 68 20 74 61 62 6c 65 2e 0a  ten hash table..
316d0 2a 2a 20 42 75 74 20 62 79 20 75 73 69 6e 67 20  ** But by using 
316d1 74 68 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c  this automatical
316d2 6c 79 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64  ly generated cod
316d3 65 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  e, the size of t
316d4 68 65 20 63 6f 64 65 0a 2a 2a 20 69 73 20 73 75  he code.** is su
316d5 62 73 74 61 6e 74 69 61 6c 6c 79 20 72 65 64 75  bstantially redu
316d6 63 65 64 2e 20 20 54 68 69 73 20 69 73 20 69 6d  ced.  This is im
316d7 70 6f 72 74 61 6e 74 20 66 6f 72 20 65 6d 62 65  portant for embe
316d8 64 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e  dded application
316d9 73 0a 2a 2a 20 6f 6e 20 70 6c 61 74 66 6f 72 6d  s.** on platform
316da 73 20 77 69 74 68 20 6c 69 6d 69 74 65 64 20 6d  s with limited m
316db 65 6d 6f 72 79 2e 0a 2a 2f 0a 2f 2a 20 48 61 73  emory..*/./* Has
316dc 68 20 73 63 6f 72 65 3a 20 31 37 31 20 2a 2f 0a  h score: 171 */.
316dd 73 74 61 74 69 63 20 69 6e 74 20 6b 65 79 77 6f  static int keywo
316de 72 64 43 6f 64 65 28 63 6f 6e 73 74 20 63 68 61  rdCode(const cha
316df 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20  r *z, int n){.  
316e0 2f 2a 20 7a 54 65 78 74 5b 5d 20 65 6e 63 6f 64  /* zText[] encod
316e1 65 73 20 38 30 31 20 62 79 74 65 73 20 6f 66 20  es 801 bytes of 
316e2 6b 65 79 77 6f 72 64 73 20 69 6e 20 35 34 31 20  keywords in 541 
316e3 62 79 74 65 73 20 2a 2f 0a 20 20 2f 2a 20 20 20  bytes */.  /*   
316e4 52 45 49 4e 44 45 58 45 44 45 53 43 41 50 45 41  REINDEXEDESCAPEA
316e5 43 48 45 43 4b 45 59 42 45 46 4f 52 45 49 47 4e  CHECKEYBEFOREIGN
316e6 4f 52 45 47 45 58 50 4c 41 49 4e 53 54 45 41 44  OREGEXPLAINSTEAD
316e7 44 41 54 41 42 41 53 45 4c 45 43 54 20 20 20 20  DATABASELECT    
316e8 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 41 42 4c     */.  /*   ABL
316e9 45 46 54 48 45 4e 44 45 46 45 52 52 41 42 4c 45  EFTHENDEFERRABLE
316ea 4c 53 45 58 43 45 50 54 52 41 4e 53 41 43 54 49  LSEXCEPTRANSACTI
316eb 4f 4e 41 54 55 52 41 4c 54 45 52 41 49 53 45 58  ONATURALTERAISEX
316ec 43 4c 55 53 49 56 45 20 20 20 20 20 20 20 20 20  CLUSIVE         
316ed 2a 2f 0a 20 20 2f 2a 20 20 20 58 49 53 54 53 41  */.  /*   XISTSA
316ee 56 45 50 4f 49 4e 54 45 52 53 45 43 54 52 49 47  VEPOINTERSECTRIG
316ef 47 45 52 45 46 45 52 45 4e 43 45 53 43 4f 4e 53  GEREFERENCESCONS
316f0 54 52 41 49 4e 54 4f 46 46 53 45 54 45 4d 50 4f  TRAINTOFFSETEMPO
316f1 52 41 52 59 20 20 20 20 20 20 20 20 20 2a 2f 0a  RARY         */.
316f2 20 20 2f 2a 20 20 20 55 4e 49 51 55 45 52 59 41    /*   UNIQUERYA
316f3 54 54 41 43 48 41 56 49 4e 47 52 4f 55 50 44 41  TTACHAVINGROUPDA
316f4 54 45 42 45 47 49 4e 4e 45 52 45 4c 45 41 53 45  TEBEGINNERELEASE
316f5 42 45 54 57 45 45 4e 4f 54 4e 55 4c 4c 49 4b 45  BETWEENOTNULLIKE
316f6 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f            */.  /
316f7 2a 20 20 20 43 41 53 43 41 44 45 4c 45 54 45 43  *   CASCADELETEC
316f8 41 53 45 43 4f 4c 4c 41 54 45 43 52 45 41 54 45  ASECOLLATECREATE
316f9 43 55 52 52 45 4e 54 5f 44 41 54 45 44 45 54 41  CURRENT_DATEDETA
316fa 43 48 49 4d 4d 45 44 49 41 54 45 4a 4f 49 4e 20  CHIMMEDIATEJOIN 
316fb 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20         */.  /*  
316fc 20 53 45 52 54 4d 41 54 43 48 50 4c 41 4e 41 4c   SERTMATCHPLANAL
316fd 59 5a 45 50 52 41 47 4d 41 42 4f 52 54 56 41 4c  YZEPRAGMABORTVAL
316fe 55 45 53 56 49 52 54 55 41 4c 49 4d 49 54 57 48  UESVIRTUALIMITWH
316ff 45 4e 57 48 45 52 45 4e 41 4d 45 20 20 20 20 20  ENWHERENAME     
31700 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 41 46      */.  /*   AF
31701 54 45 52 45 50 4c 41 43 45 41 4e 44 45 46 41 55  TEREPLACEANDEFAU
31702 4c 54 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 43  LTAUTOINCREMENTC
31703 41 53 54 43 4f 4c 55 4d 4e 43 4f 4d 4d 49 54 43  ASTCOLUMNCOMMITC
31704 4f 4e 46 4c 49 43 54 43 52 4f 53 53 20 20 20 20  ONFLICTCROSS    
31705 20 2a 2f 0a 20 20 2f 2a 20 20 20 43 55 52 52 45   */.  /*   CURRE
31706 4e 54 5f 54 49 4d 45 53 54 41 4d 50 52 49 4d 41  NT_TIMESTAMPRIMA
31707 52 59 44 45 46 45 52 52 45 44 49 53 54 49 4e 43  RYDEFERREDISTINC
31708 54 44 52 4f 50 46 41 49 4c 46 52 4f 4d 46 55 4c  TDROPFAILFROMFUL
31709 4c 47 4c 4f 42 59 49 46 20 20 20 20 20 20 2a 2f  LGLOBYIF      */
3170a 0a 20 20 2f 2a 20 20 20 49 53 4e 55 4c 4c 4f 52  .  /*   ISNULLOR
3170b 44 45 52 45 53 54 52 49 43 54 4f 55 54 45 52 49  DERESTRICTOUTERI
3170c 47 48 54 52 4f 4c 4c 42 41 43 4b 52 4f 57 55 4e  GHTROLLBACKROWUN
3170d 49 4f 4e 55 53 49 4e 47 56 41 43 55 55 4d 56 49  IONUSINGVACUUMVI
3170e 45 57 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  EW         */.  
3170f 2f 2a 20 20 20 49 4e 49 54 49 41 4c 4c 59 20 20  /*   INITIALLY  
31710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31711 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31712 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31713 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 73 74 61          */.  sta
31714 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a  tic const char z
31715 54 65 78 74 5b 35 34 30 5d 20 3d 20 7b 0a 20 20  Text[540] = {.  
31716 20 20 27 52 27 2c 27 45 27 2c 27 49 27 2c 27 4e    'R','E','I','N
31717 27 2c 27 44 27 2c 27 45 27 2c 27 58 27 2c 27 45  ','D','E','X','E
31718 27 2c 27 44 27 2c 27 45 27 2c 27 53 27 2c 27 43  ','D','E','S','C
31719 27 2c 27 41 27 2c 27 50 27 2c 27 45 27 2c 27 41  ','A','P','E','A
3171a 27 2c 27 43 27 2c 27 48 27 2c 0a 20 20 20 20 27  ','C','H',.    '
3171b 45 27 2c 27 43 27 2c 27 4b 27 2c 27 45 27 2c 27  E','C','K','E','
3171c 59 27 2c 27 42 27 2c 27 45 27 2c 27 46 27 2c 27  Y','B','E','F','
3171d 4f 27 2c 27 52 27 2c 27 45 27 2c 27 49 27 2c 27  O','R','E','I','
3171e 47 27 2c 27 4e 27 2c 27 4f 27 2c 27 52 27 2c 27  G','N','O','R','
3171f 45 27 2c 27 47 27 2c 0a 20 20 20 20 27 45 27 2c  E','G',.    'E',
31720 27 58 27 2c 27 50 27 2c 27 4c 27 2c 27 41 27 2c  'X','P','L','A',
31721 27 49 27 2c 27 4e 27 2c 27 53 27 2c 27 54 27 2c  'I','N','S','T',
31722 27 45 27 2c 27 41 27 2c 27 44 27 2c 27 44 27 2c  'E','A','D','D',
31723 27 41 27 2c 27 54 27 2c 27 41 27 2c 27 42 27 2c  'A','T','A','B',
31724 27 41 27 2c 0a 20 20 20 20 27 53 27 2c 27 45 27  'A',.    'S','E'
31725 2c 27 4c 27 2c 27 45 27 2c 27 43 27 2c 27 54 27  ,'L','E','C','T'
31726 2c 27 41 27 2c 27 42 27 2c 27 4c 27 2c 27 45 27  ,'A','B','L','E'
31727 2c 27 46 27 2c 27 54 27 2c 27 48 27 2c 27 45 27  ,'F','T','H','E'
31728 2c 27 4e 27 2c 27 44 27 2c 27 45 27 2c 27 46 27  ,'N','D','E','F'
31729 2c 0a 20 20 20 20 27 45 27 2c 27 52 27 2c 27 52  ,.    'E','R','R
3172a 27 2c 27 41 27 2c 27 42 27 2c 27 4c 27 2c 27 45  ','A','B','L','E
3172b 27 2c 27 4c 27 2c 27 53 27 2c 27 45 27 2c 27 58  ','L','S','E','X
3172c 27 2c 27 43 27 2c 27 45 27 2c 27 50 27 2c 27 54  ','C','E','P','T
3172d 27 2c 27 52 27 2c 27 41 27 2c 27 4e 27 2c 0a 20  ','R','A','N',. 
3172e 20 20 20 27 53 27 2c 27 41 27 2c 27 43 27 2c 27     'S','A','C','
3172f 54 27 2c 27 49 27 2c 27 4f 27 2c 27 4e 27 2c 27  T','I','O','N','
31730 41 27 2c 27 54 27 2c 27 55 27 2c 27 52 27 2c 27  A','T','U','R','
31731 41 27 2c 27 4c 27 2c 27 54 27 2c 27 45 27 2c 27  A','L','T','E','
31732 52 27 2c 27 41 27 2c 27 49 27 2c 0a 20 20 20 20  R','A','I',.    
31733 27 53 27 2c 27 45 27 2c 27 58 27 2c 27 43 27 2c  'S','E','X','C',
31734 27 4c 27 2c 27 55 27 2c 27 53 27 2c 27 49 27 2c  'L','U','S','I',
31735 27 56 27 2c 27 45 27 2c 27 58 27 2c 27 49 27 2c  'V','E','X','I',
31736 27 53 27 2c 27 54 27 2c 27 53 27 2c 27 41 27 2c  'S','T','S','A',
31737 27 56 27 2c 27 45 27 2c 0a 20 20 20 20 27 50 27  'V','E',.    'P'
31738 2c 27 4f 27 2c 27 49 27 2c 27 4e 27 2c 27 54 27  ,'O','I','N','T'
31739 2c 27 45 27 2c 27 52 27 2c 27 53 27 2c 27 45 27  ,'E','R','S','E'
3173a 2c 27 43 27 2c 27 54 27 2c 27 52 27 2c 27 49 27  ,'C','T','R','I'
3173b 2c 27 47 27 2c 27 47 27 2c 27 45 27 2c 27 52 27  ,'G','G','E','R'
3173c 2c 27 45 27 2c 0a 20 20 20 20 27 46 27 2c 27 45  ,'E',.    'F','E
3173d 27 2c 27 52 27 2c 27 45 27 2c 27 4e 27 2c 27 43  ','R','E','N','C
3173e 27 2c 27 45 27 2c 27 53 27 2c 27 43 27 2c 27 4f  ','E','S','C','O
3173f 27 2c 27 4e 27 2c 27 53 27 2c 27 54 27 2c 27 52  ','N','S','T','R
31740 27 2c 27 41 27 2c 27 49 27 2c 27 4e 27 2c 27 54  ','A','I','N','T
31741 27 2c 0a 20 20 20 20 27 4f 27 2c 27 46 27 2c 27  ',.    'O','F','
31742 46 27 2c 27 53 27 2c 27 45 27 2c 27 54 27 2c 27  F','S','E','T','
31743 45 27 2c 27 4d 27 2c 27 50 27 2c 27 4f 27 2c 27  E','M','P','O','
31744 52 27 2c 27 41 27 2c 27 52 27 2c 27 59 27 2c 27  R','A','R','Y','
31745 55 27 2c 27 4e 27 2c 27 49 27 2c 27 51 27 2c 0a  U','N','I','Q',.
31746 20 20 20 20 27 55 27 2c 27 45 27 2c 27 52 27 2c      'U','E','R',
31747 27 59 27 2c 27 41 27 2c 27 54 27 2c 27 54 27 2c  'Y','A','T','T',
31748 27 41 27 2c 27 43 27 2c 27 48 27 2c 27 41 27 2c  'A','C','H','A',
31749 27 56 27 2c 27 49 27 2c 27 4e 27 2c 27 47 27 2c  'V','I','N','G',
3174a 27 52 27 2c 27 4f 27 2c 27 55 27 2c 0a 20 20 20  'R','O','U',.   
3174b 20 27 50 27 2c 27 44 27 2c 27 41 27 2c 27 54 27   'P','D','A','T'
3174c 2c 27 45 27 2c 27 42 27 2c 27 45 27 2c 27 47 27  ,'E','B','E','G'
3174d 2c 27 49 27 2c 27 4e 27 2c 27 4e 27 2c 27 45 27  ,'I','N','N','E'
3174e 2c 27 52 27 2c 27 45 27 2c 27 4c 27 2c 27 45 27  ,'R','E','L','E'
3174f 2c 27 41 27 2c 27 53 27 2c 0a 20 20 20 20 27 45  ,'A','S',.    'E
31750 27 2c 27 42 27 2c 27 45 27 2c 27 54 27 2c 27 57  ','B','E','T','W
31751 27 2c 27 45 27 2c 27 45 27 2c 27 4e 27 2c 27 4f  ','E','E','N','O
31752 27 2c 27 54 27 2c 27 4e 27 2c 27 55 27 2c 27 4c  ','T','N','U','L
31753 27 2c 27 4c 27 2c 27 49 27 2c 27 4b 27 2c 27 45  ','L','I','K','E
31754 27 2c 27 43 27 2c 0a 20 20 20 20 27 41 27 2c 27  ','C',.    'A','
31755 53 27 2c 27 43 27 2c 27 41 27 2c 27 44 27 2c 27  S','C','A','D','
31756 45 27 2c 27 4c 27 2c 27 45 27 2c 27 54 27 2c 27  E','L','E','T','
31757 45 27 2c 27 43 27 2c 27 41 27 2c 27 53 27 2c 27  E','C','A','S','
31758 45 27 2c 27 43 27 2c 27 4f 27 2c 27 4c 27 2c 27  E','C','O','L','
31759 4c 27 2c 0a 20 20 20 20 27 41 27 2c 27 54 27 2c  L',.    'A','T',
3175a 27 45 27 2c 27 43 27 2c 27 52 27 2c 27 45 27 2c  'E','C','R','E',
3175b 27 41 27 2c 27 54 27 2c 27 45 27 2c 27 43 27 2c  'A','T','E','C',
3175c 27 55 27 2c 27 52 27 2c 27 52 27 2c 27 45 27 2c  'U','R','R','E',
3175d 27 4e 27 2c 27 54 27 2c 27 5f 27 2c 27 44 27 2c  'N','T','_','D',
3175e 0a 20 20 20 20 27 41 27 2c 27 54 27 2c 27 45 27  .    'A','T','E'
3175f 2c 27 44 27 2c 27 45 27 2c 27 54 27 2c 27 41 27  ,'D','E','T','A'
31760 2c 27 43 27 2c 27 48 27 2c 27 49 27 2c 27 4d 27  ,'C','H','I','M'
31761 2c 27 4d 27 2c 27 45 27 2c 27 44 27 2c 27 49 27  ,'M','E','D','I'
31762 2c 27 41 27 2c 27 54 27 2c 27 45 27 2c 0a 20 20  ,'A','T','E',.  
31763 20 20 27 4a 27 2c 27 4f 27 2c 27 49 27 2c 27 4e    'J','O','I','N
31764 27 2c 27 53 27 2c 27 45 27 2c 27 52 27 2c 27 54  ','S','E','R','T
31765 27 2c 27 4d 27 2c 27 41 27 2c 27 54 27 2c 27 43  ','M','A','T','C
31766 27 2c 27 48 27 2c 27 50 27 2c 27 4c 27 2c 27 41  ','H','P','L','A
31767 27 2c 27 4e 27 2c 27 41 27 2c 0a 20 20 20 20 27  ','N','A',.    '
31768 4c 27 2c 27 59 27 2c 27 5a 27 2c 27 45 27 2c 27  L','Y','Z','E','
31769 50 27 2c 27 52 27 2c 27 41 27 2c 27 47 27 2c 27  P','R','A','G','
3176a 4d 27 2c 27 41 27 2c 27 42 27 2c 27 4f 27 2c 27  M','A','B','O','
3176b 52 27 2c 27 54 27 2c 27 56 27 2c 27 41 27 2c 27  R','T','V','A','
3176c 4c 27 2c 27 55 27 2c 0a 20 20 20 20 27 45 27 2c  L','U',.    'E',
3176d 27 53 27 2c 27 56 27 2c 27 49 27 2c 27 52 27 2c  'S','V','I','R',
3176e 27 54 27 2c 27 55 27 2c 27 41 27 2c 27 4c 27 2c  'T','U','A','L',
3176f 27 49 27 2c 27 4d 27 2c 27 49 27 2c 27 54 27 2c  'I','M','I','T',
31770 27 57 27 2c 27 48 27 2c 27 45 27 2c 27 4e 27 2c  'W','H','E','N',
31771 27 57 27 2c 0a 20 20 20 20 27 48 27 2c 27 45 27  'W',.    'H','E'
31772 2c 27 52 27 2c 27 45 27 2c 27 4e 27 2c 27 41 27  ,'R','E','N','A'
31773 2c 27 4d 27 2c 27 45 27 2c 27 41 27 2c 27 46 27  ,'M','E','A','F'
31774 2c 27 54 27 2c 27 45 27 2c 27 52 27 2c 27 45 27  ,'T','E','R','E'
31775 2c 27 50 27 2c 27 4c 27 2c 27 41 27 2c 27 43 27  ,'P','L','A','C'
31776 2c 0a 20 20 20 20 27 45 27 2c 27 41 27 2c 27 4e  ,.    'E','A','N
31777 27 2c 27 44 27 2c 27 45 27 2c 27 46 27 2c 27 41  ','D','E','F','A
31778 27 2c 27 55 27 2c 27 4c 27 2c 27 54 27 2c 27 41  ','U','L','T','A
31779 27 2c 27 55 27 2c 27 54 27 2c 27 4f 27 2c 27 49  ','U','T','O','I
3177a 27 2c 27 4e 27 2c 27 43 27 2c 27 52 27 2c 0a 20  ','N','C','R',. 
3177b 20 20 20 27 45 27 2c 27 4d 27 2c 27 45 27 2c 27     'E','M','E','
3177c 4e 27 2c 27 54 27 2c 27 43 27 2c 27 41 27 2c 27  N','T','C','A','
3177d 53 27 2c 27 54 27 2c 27 43 27 2c 27 4f 27 2c 27  S','T','C','O','
3177e 4c 27 2c 27 55 27 2c 27 4d 27 2c 27 4e 27 2c 27  L','U','M','N','
3177f 43 27 2c 27 4f 27 2c 27 4d 27 2c 0a 20 20 20 20  C','O','M',.    
31780 27 4d 27 2c 27 49 27 2c 27 54 27 2c 27 43 27 2c  'M','I','T','C',
31781 27 4f 27 2c 27 4e 27 2c 27 46 27 2c 27 4c 27 2c  'O','N','F','L',
31782 27 49 27 2c 27 43 27 2c 27 54 27 2c 27 43 27 2c  'I','C','T','C',
31783 27 52 27 2c 27 4f 27 2c 27 53 27 2c 27 53 27 2c  'R','O','S','S',
31784 27 43 27 2c 27 55 27 2c 0a 20 20 20 20 27 52 27  'C','U',.    'R'
31785 2c 27 52 27 2c 27 45 27 2c 27 4e 27 2c 27 54 27  ,'R','E','N','T'
31786 2c 27 5f 27 2c 27 54 27 2c 27 49 27 2c 27 4d 27  ,'_','T','I','M'
31787 2c 27 45 27 2c 27 53 27 2c 27 54 27 2c 27 41 27  ,'E','S','T','A'
31788 2c 27 4d 27 2c 27 50 27 2c 27 52 27 2c 27 49 27  ,'M','P','R','I'
31789 2c 27 4d 27 2c 0a 20 20 20 20 27 41 27 2c 27 52  ,'M',.    'A','R
3178a 27 2c 27 59 27 2c 27 44 27 2c 27 45 27 2c 27 46  ','Y','D','E','F
3178b 27 2c 27 45 27 2c 27 52 27 2c 27 52 27 2c 27 45  ','E','R','R','E
3178c 27 2c 27 44 27 2c 27 49 27 2c 27 53 27 2c 27 54  ','D','I','S','T
3178d 27 2c 27 49 27 2c 27 4e 27 2c 27 43 27 2c 27 54  ','I','N','C','T
3178e 27 2c 0a 20 20 20 20 27 44 27 2c 27 52 27 2c 27  ',.    'D','R','
3178f 4f 27 2c 27 50 27 2c 27 46 27 2c 27 41 27 2c 27  O','P','F','A','
31790 49 27 2c 27 4c 27 2c 27 46 27 2c 27 52 27 2c 27  I','L','F','R','
31791 4f 27 2c 27 4d 27 2c 27 46 27 2c 27 55 27 2c 27  O','M','F','U','
31792 4c 27 2c 27 4c 27 2c 27 47 27 2c 27 4c 27 2c 0a  L','L','G','L',.
31793 20 20 20 20 27 4f 27 2c 27 42 27 2c 27 59 27 2c      'O','B','Y',
31794 27 49 27 2c 27 46 27 2c 27 49 27 2c 27 53 27 2c  'I','F','I','S',
31795 27 4e 27 2c 27 55 27 2c 27 4c 27 2c 27 4c 27 2c  'N','U','L','L',
31796 27 4f 27 2c 27 52 27 2c 27 44 27 2c 27 45 27 2c  'O','R','D','E',
31797 27 52 27 2c 27 45 27 2c 27 53 27 2c 0a 20 20 20  'R','E','S',.   
31798 20 27 54 27 2c 27 52 27 2c 27 49 27 2c 27 43 27   'T','R','I','C'
31799 2c 27 54 27 2c 27 4f 27 2c 27 55 27 2c 27 54 27  ,'T','O','U','T'
3179a 2c 27 45 27 2c 27 52 27 2c 27 49 27 2c 27 47 27  ,'E','R','I','G'
3179b 2c 27 48 27 2c 27 54 27 2c 27 52 27 2c 27 4f 27  ,'H','T','R','O'
3179c 2c 27 4c 27 2c 27 4c 27 2c 0a 20 20 20 20 27 42  ,'L','L',.    'B
3179d 27 2c 27 41 27 2c 27 43 27 2c 27 4b 27 2c 27 52  ','A','C','K','R
3179e 27 2c 27 4f 27 2c 27 57 27 2c 27 55 27 2c 27 4e  ','O','W','U','N
3179f 27 2c 27 49 27 2c 27 4f 27 2c 27 4e 27 2c 27 55  ','I','O','N','U
317a0 27 2c 27 53 27 2c 27 49 27 2c 27 4e 27 2c 27 47  ','S','I','N','G
317a1 27 2c 27 56 27 2c 0a 20 20 20 20 27 41 27 2c 27  ','V',.    'A','
317a2 43 27 2c 27 55 27 2c 27 55 27 2c 27 4d 27 2c 27  C','U','U','M','
317a3 56 27 2c 27 49 27 2c 27 45 27 2c 27 57 27 2c 27  V','I','E','W','
317a4 49 27 2c 27 4e 27 2c 27 49 27 2c 27 54 27 2c 27  I','N','I','T','
317a5 49 27 2c 27 41 27 2c 27 4c 27 2c 27 4c 27 2c 27  I','A','L','L','
317a6 59 27 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69  Y',.  };.  stati
317a7 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  c const unsigned
317a8 20 63 68 61 72 20 61 48 61 73 68 5b 31 32 37 5d   char aHash[127]
317a9 20 3d 20 7b 0a 20 20 20 20 20 20 37 30 2c 20 20   = {.      70,  
317aa 39 39 2c 20 31 31 32 2c 20 20 36 38 2c 20 20 20  99, 112,  68,   
317ab 30 2c 20 20 34 33 2c 20 20 20 30 2c 20 20 20 30  0,  43,   0,   0
317ac 2c 20 20 37 36 2c 20 20 20 30 2c 20 20 37 31 2c  ,  76,   0,  71,
317ad 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20     0,   0,.     
317ae 20 34 31 2c 20 20 31 32 2c 20 20 37 32 2c 20 20   41,  12,  72,  
317af 31 35 2c 20 20 20 30 2c 20 31 31 31 2c 20 20 37  15,   0, 111,  7
317b0 39 2c 20 20 34 39 2c 20 31 30 36 2c 20 20 20 30  9,  49, 106,   0
317b1 2c 20 20 31 39 2c 20 20 20 30 2c 20 20 20 30 2c  ,  19,   0,   0,
317b2 0a 20 20 20 20 20 31 31 36 2c 20 20 20 30 2c 20  .     116,   0, 
317b3 31 31 34 2c 20 31 30 39 2c 20 20 20 30 2c 20 20  114, 109,   0,  
317b4 32 32 2c 20 20 38 37 2c 20 20 20 30 2c 20 20 20  22,  87,   0,   
317b5 39 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 36 34  9,   0,   0,  64
317b6 2c 20 20 36 35 2c 0a 20 20 20 20 20 20 20 30 2c  ,  65,.       0,
317b7 20 20 36 33 2c 20 20 20 36 2c 20 20 20 30 2c 20    63,   6,   0, 
317b8 20 34 37 2c 20 20 38 34 2c 20 20 39 36 2c 20 20   47,  84,  96,  
317b9 20 30 2c 20 31 31 33 2c 20 20 39 35 2c 20 20 20   0, 113,  95,   
317ba 30 2c 20 20 20 30 2c 20 20 34 34 2c 0a 20 20 20  0,   0,  44,.   
317bb 20 20 20 20 30 2c 20 20 39 37 2c 20 20 32 34 2c      0,  97,  24,
317bc 20 20 20 30 2c 20 20 31 37 2c 20 20 20 30 2c 20     0,  17,   0, 
317bd 31 31 37 2c 20 20 34 38 2c 20 20 32 33 2c 20 20  117,  48,  23,  
317be 20 30 2c 20 20 20 35 2c 20 31 30 34 2c 20 20 32   0,   5, 104,  2
317bf 35 2c 0a 20 20 20 20 20 20 39 30 2c 20 20 20 30  5,.      90,   0
317c0 2c 20 20 20 30 2c 20 31 31 39 2c 20 31 30 30 2c  ,   0, 119, 100,
317c1 20 20 35 35 2c 20 31 31 38 2c 20 20 35 32 2c 20    55, 118,  52, 
317c2 20 20 37 2c 20 20 35 30 2c 20 20 20 30 2c 20 20    7,  50,   0,  
317c3 38 35 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 39  85,   0,.      9
317c4 34 2c 20 20 32 36 2c 20 20 20 30 2c 20 20 39 33  4,  26,   0,  93
317c5 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
317c6 20 20 38 39 2c 20 20 38 36 2c 20 20 39 31 2c 20    89,  86,  91, 
317c7 20 38 32 2c 20 31 30 33 2c 20 20 31 34 2c 0a 20   82, 103,  14,. 
317c8 20 20 20 20 20 33 38 2c 20 31 30 32 2c 20 20 20       38, 102,   
317c9 30 2c 20 20 37 35 2c 20 20 20 30 2c 20 20 31 38  0,  75,   0,  18
317ca 2c 20 20 38 33 2c 20 31 30 35 2c 20 20 33 31 2c  ,  83, 105,  31,
317cb 20 20 20 30 2c 20 31 31 35 2c 20 20 37 34 2c 20     0, 115,  74, 
317cc 31 30 37 2c 0a 20 20 20 20 20 20 35 37 2c 20 20  107,.      57,  
317cd 34 35 2c 20 20 37 38 2c 20 20 20 30 2c 20 20 20  45,  78,   0,   
317ce 30 2c 20 20 38 38 2c 20 20 33 39 2c 20 20 20 30  0,  88,  39,   0
317cf 2c 20 31 31 30 2c 20 20 20 30 2c 20 20 33 35 2c  , 110,   0,  35,
317d0 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20     0,   0,.     
317d1 20 32 38 2c 20 20 20 30 2c 20 20 38 30 2c 20 20   28,   0,  80,  
317d2 35 33 2c 20 20 35 38 2c 20 20 20 30 2c 20 20 32  53,  58,   0,  2
317d3 30 2c 20 20 35 36 2c 20 20 20 30 2c 20 20 35 31  0,  56,   0,  51
317d4 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20  ,.  };.  static 
317d5 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
317d6 68 61 72 20 61 4e 65 78 74 5b 31 31 39 5d 20 3d  har aNext[119] =
317d7 20 7b 0a 20 20 20 20 20 20 20 30 2c 20 20 20 30   {.       0,   0
317d8 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 34 2c  ,   0,   0,   4,
317d9 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
317da 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
317db 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 20   0,   0,.       
317dc 30 2c 20 20 20 32 2c 20 20 20 30 2c 20 20 20 30  0,   2,   0,   0
317dd 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
317de 20 20 20 30 2c 20 20 31 33 2c 20 20 20 30 2c 20     0,  13,   0, 
317df 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20    0,   0,   0,. 
317e0 20 20 20 20 20 20 30 2c 20 20 20 30 2c 20 20 20        0,   0,   
317e1 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
317e2 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
317e3 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
317e4 20 20 30 2c 0a 20 20 20 20 20 20 20 30 2c 20 20    0,.       0,  
317e5 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 33   0,   0,   0,  3
317e6 32 2c 20 20 32 31 2c 20 20 20 30 2c 20 20 20 30  2,  21,   0,   0
317e7 2c 20 20 20 30 2c 20 20 34 32 2c 20 20 20 33 2c  ,   0,  42,   3,
317e8 20 20 34 36 2c 20 20 20 30 2c 0a 20 20 20 20 20    46,   0,.     
317e9 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
317ea 32 39 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 33  29,   0,   0,  3
317eb 37 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  7,   0,   0,   0
317ec 2c 20 20 20 31 2c 20 20 36 30 2c 20 20 20 30 2c  ,   1,  60,   0,
317ed 0a 20 20 20 20 20 20 20 30 2c 20 20 36 31 2c 20  .       0,  61, 
317ee 20 20 30 2c 20 20 34 30 2c 20 20 20 30 2c 20 20    0,  40,   0,  
317ef 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
317f0 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 35 39  0,   0,   0,  59
317f1 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 20 30 2c  ,   0,.       0,
317f2 20 20 20 30 2c 20 20 20 30 2c 20 20 33 30 2c 20     0,   0,  30, 
317f3 20 35 34 2c 20 20 31 36 2c 20 20 33 33 2c 20 20   54,  16,  33,  
317f4 31 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20  10,   0,   0,   
317f5 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20  0,   0,   0,.   
317f6 20 20 20 20 30 2c 20 20 20 30 2c 20 20 31 31 2c      0,   0,  11,
317f7 20 20 36 36 2c 20 20 37 33 2c 20 20 20 30 2c 20    66,  73,   0, 
317f8 20 20 38 2c 20 20 20 30 2c 20 20 39 38 2c 20 20    8,   0,  98,  
317f9 39 32 2c 20 20 20 30 2c 20 31 30 31 2c 20 20 20  92,   0, 101,   
317fa 30 2c 0a 20 20 20 20 20 20 38 31 2c 20 20 20 30  0,.      81,   0
317fb 2c 20 20 36 39 2c 20 20 20 30 2c 20 20 20 30 2c  ,  69,   0,   0,
317fc 20 31 30 38 2c 20 20 32 37 2c 20 20 33 36 2c 20   108,  27,  36, 
317fd 20 36 37 2c 20 20 37 37 2c 20 20 20 30 2c 20 20   67,  77,   0,  
317fe 33 34 2c 20 20 36 32 2c 0a 20 20 20 20 20 20 20  34,  62,.       
317ff 30 2c 20 20 20 30 2c 0a 20 20 7d 3b 0a 20 20 73  0,   0,.  };.  s
31800 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69  tatic const unsi
31801 67 6e 65 64 20 63 68 61 72 20 61 4c 65 6e 5b 31  gned char aLen[1
31802 31 39 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 37  19] = {.       7
31803 2c 20 20 20 37 2c 20 20 20 35 2c 20 20 20 34 2c  ,   7,   5,   4,
31804 20 20 20 36 2c 20 20 20 34 2c 20 20 20 35 2c 20     6,   4,   5, 
31805 20 20 33 2c 20 20 20 36 2c 20 20 20 37 2c 20 20    3,   6,   7,  
31806 20 33 2c 20 20 20 36 2c 20 20 20 36 2c 0a 20 20   3,   6,   6,.  
31807 20 20 20 20 20 37 2c 20 20 20 37 2c 20 20 20 33       7,   7,   3
31808 2c 20 20 20 38 2c 20 20 20 32 2c 20 20 20 36 2c  ,   8,   2,   6,
31809 20 20 20 35 2c 20 20 20 34 2c 20 20 20 34 2c 20     5,   4,   4, 
3180a 20 20 33 2c 20 20 31 30 2c 20 20 20 34 2c 20 20    3,  10,   4,  
3180b 20 36 2c 0a 20 20 20 20 20 20 31 31 2c 20 20 20   6,.      11,   
3180c 32 2c 20 20 20 37 2c 20 20 20 35 2c 20 20 20 35  2,   7,   5,   5
3180d 2c 20 20 20 39 2c 20 20 20 36 2c 20 20 20 39 2c  ,   9,   6,   9,
3180e 20 20 20 39 2c 20 20 20 37 2c 20 20 31 30 2c 20     9,   7,  10, 
3180f 20 31 30 2c 20 20 20 34 2c 0a 20 20 20 20 20 20   10,   4,.      
31810 20 36 2c 20 20 20 32 2c 20 20 20 33 2c 20 20 20   6,   2,   3,   
31811 34 2c 20 20 20 39 2c 20 20 20 32 2c 20 20 20 36  4,   9,   2,   6
31812 2c 20 20 20 35 2c 20 20 20 36 2c 20 20 20 36 2c  ,   5,   6,   6,
31813 20 20 20 35 2c 20 20 20 36 2c 20 20 20 35 2c 0a     5,   6,   5,.
31814 20 20 20 20 20 20 20 35 2c 20 20 20 37 2c 20 20         5,   7,  
31815 20 37 2c 20 20 20 37 2c 20 20 20 33 2c 20 20 20   7,   7,   3,   
31816 34 2c 20 20 20 34 2c 20 20 20 37 2c 20 20 20 33  4,   4,   7,   3
31817 2c 20 20 20 36 2c 20 20 20 34 2c 20 20 20 37 2c  ,   6,   4,   7,
31818 20 20 20 36 2c 0a 20 20 20 20 20 20 31 32 2c 20     6,.      12, 
31819 20 20 36 2c 20 20 20 39 2c 20 20 20 34 2c 20 20    6,   9,   4,  
3181a 20 36 2c 20 20 20 35 2c 20 20 20 34 2c 20 20 20   6,   5,   4,   
3181b 37 2c 20 20 20 36 2c 20 20 20 35 2c 20 20 20 36  7,   6,   5,   6
3181c 2c 20 20 20 37 2c 20 20 20 35 2c 0a 20 20 20 20  ,   7,   5,.    
3181d 20 20 20 34 2c 20 20 20 35 2c 20 20 20 36 2c 20     4,   5,   6, 
3181e 20 20 35 2c 20 20 20 37 2c 20 20 20 33 2c 20 20    5,   7,   3,  
3181f 20 37 2c 20 20 31 33 2c 20 20 20 32 2c 20 20 20   7,  13,   2,   
31820 32 2c 20 20 20 34 2c 20 20 20 36 2c 20 20 20 36  2,   4,   6,   6
31821 2c 0a 20 20 20 20 20 20 20 38 2c 20 20 20 35 2c  ,.       8,   5,
31822 20 20 31 37 2c 20 20 31 32 2c 20 20 20 37 2c 20    17,  12,   7, 
31823 20 20 38 2c 20 20 20 38 2c 20 20 20 32 2c 20 20    8,   8,   2,  
31824 20 34 2c 20 20 20 34 2c 20 20 20 34 2c 20 20 20   4,   4,   4,   
31825 34 2c 20 20 20 34 2c 0a 20 20 20 20 20 20 20 32  4,   4,.       2
31826 2c 20 20 20 32 2c 20 20 20 36 2c 20 20 20 35 2c  ,   2,   6,   5,
31827 20 20 20 38 2c 20 20 20 35 2c 20 20 20 35 2c 20     8,   5,   5, 
31828 20 20 38 2c 20 20 20 33 2c 20 20 20 35 2c 20 20    8,   3,   5,  
31829 20 35 2c 20 20 20 36 2c 20 20 20 34 2c 0a 20 20   5,   6,   4,.  
3182a 20 20 20 20 20 39 2c 20 20 20 33 2c 0a 20 20 7d       9,   3,.  }
3182b 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.  static const
3182c 20 75 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20   unsigned short 
3182d 69 6e 74 20 61 4f 66 66 73 65 74 5b 31 31 39 5d  int aOffset[119]
3182e 20 3d 20 7b 0a 20 20 20 20 20 20 20 30 2c 20 20   = {.       0,  
3182f 20 32 2c 20 20 20 32 2c 20 20 20 38 2c 20 20 20   2,   2,   8,   
31830 39 2c 20 20 31 34 2c 20 20 31 36 2c 20 20 32 30  9,  14,  16,  20
31831 2c 20 20 32 33 2c 20 20 32 35 2c 20 20 32 35 2c  ,  23,  25,  25,
31832 20 20 32 39 2c 20 20 33 33 2c 0a 20 20 20 20 20    29,  33,.     
31833 20 33 36 2c 20 20 34 31 2c 20 20 34 36 2c 20 20   36,  41,  46,  
31834 34 38 2c 20 20 35 33 2c 20 20 35 34 2c 20 20 35  48,  53,  54,  5
31835 39 2c 20 20 36 32 2c 20 20 36 35 2c 20 20 36 37  9,  62,  65,  67
31836 2c 20 20 36 39 2c 20 20 37 38 2c 20 20 38 31 2c  ,  69,  78,  81,
31837 0a 20 20 20 20 20 20 38 36 2c 20 20 39 35 2c 20  .      86,  95, 
31838 20 39 36 2c 20 31 30 31 2c 20 31 30 35 2c 20 31   96, 101, 105, 1
31839 30 39 2c 20 31 31 37 2c 20 31 32 32 2c 20 31 32  09, 117, 122, 12
3183a 38 2c 20 31 33 36 2c 20 31 34 32 2c 20 31 35 32  8, 136, 142, 152
3183b 2c 20 31 35 39 2c 0a 20 20 20 20 20 31 36 32 2c  , 159,.     162,
3183c 20 31 36 32 2c 20 31 36 35 2c 20 31 36 37 2c 20   162, 165, 167, 
3183d 31 36 37 2c 20 31 37 31 2c 20 31 37 36 2c 20 31  167, 171, 176, 1
3183e 37 39 2c 20 31 38 34 2c 20 31 38 39 2c 20 31 39  79, 184, 189, 19
3183f 34 2c 20 31 39 37 2c 20 32 30 33 2c 0a 20 20 20  4, 197, 203,.   
31840 20 20 32 30 36 2c 20 32 31 30 2c 20 32 31 37 2c    206, 210, 217,
31841 20 32 32 33 2c 20 32 32 33 2c 20 32 32 36 2c 20   223, 223, 226, 
31842 32 32 39 2c 20 32 33 33 2c 20 32 33 34 2c 20 32  229, 233, 234, 2
31843 33 38 2c 20 32 34 34 2c 20 32 34 38 2c 20 32 35  38, 244, 248, 25
31844 35 2c 0a 20 20 20 20 20 32 36 31 2c 20 32 37 33  5,.     261, 273
31845 2c 20 32 37 39 2c 20 32 38 38 2c 20 32 39 30 2c  , 279, 288, 290,
31846 20 32 39 36 2c 20 33 30 31 2c 20 33 30 33 2c 20   296, 301, 303, 
31847 33 31 30 2c 20 33 31 35 2c 20 33 32 30 2c 20 33  310, 315, 320, 3
31848 32 36 2c 20 33 33 32 2c 0a 20 20 20 20 20 33 33  26, 332,.     33
31849 37 2c 20 33 34 31 2c 20 33 34 34 2c 20 33 35 30  7, 341, 344, 350
3184a 2c 20 33 35 34 2c 20 33 36 31 2c 20 33 36 33 2c  , 354, 361, 363,
3184b 20 33 37 30 2c 20 33 37 32 2c 20 33 37 34 2c 20   370, 372, 374, 
3184c 33 38 33 2c 20 33 38 37 2c 20 33 39 33 2c 0a 20  383, 387, 393,. 
3184d 20 20 20 20 33 39 39 2c 20 34 30 37 2c 20 34 31      399, 407, 41
3184e 32 2c 20 34 31 32 2c 20 34 32 38 2c 20 34 33 35  2, 412, 428, 435
3184f 2c 20 34 34 32 2c 20 34 34 33 2c 20 34 35 30 2c  , 442, 443, 450,
31850 20 34 35 34 2c 20 34 35 38 2c 20 34 36 32 2c 20   454, 458, 462, 
31851 34 36 36 2c 0a 20 20 20 20 20 34 36 39 2c 20 34  466,.     469, 4
31852 37 31 2c 20 34 37 33 2c 20 34 37 39 2c 20 34 38  71, 473, 479, 48
31853 33 2c 20 34 39 31 2c 20 34 39 35 2c 20 35 30 30  3, 491, 495, 500
31854 2c 20 35 30 38 2c 20 35 31 31 2c 20 35 31 36 2c  , 508, 511, 516,
31855 20 35 32 31 2c 20 35 32 37 2c 0a 20 20 20 20 20   521, 527,.     
31856 35 33 31 2c 20 35 33 36 2c 0a 20 20 7d 3b 0a 20  531, 536,.  };. 
31857 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
31858 73 69 67 6e 65 64 20 63 68 61 72 20 61 43 6f 64  signed char aCod
31859 65 5b 31 31 39 5d 20 3d 20 7b 0a 20 20 20 20 54  e[119] = {.    T
3185a 4b 5f 52 45 49 4e 44 45 58 2c 20 20 20 20 54 4b  K_REINDEX,    TK
3185b 5f 49 4e 44 45 58 45 44 2c 20 20 20 20 54 4b 5f  _INDEXED,    TK_
3185c 49 4e 44 45 58 2c 20 20 20 20 20 20 54 4b 5f 44  INDEX,      TK_D
3185d 45 53 43 2c 20 20 20 20 20 20 20 54 4b 5f 45 53  ESC,       TK_ES
3185e 43 41 50 45 2c 20 20 20 20 20 0a 20 20 20 20 54  CAPE,     .    T
3185f 4b 5f 45 41 43 48 2c 20 20 20 20 20 20 20 54 4b  K_EACH,       TK
31860 5f 43 48 45 43 4b 2c 20 20 20 20 20 20 54 4b 5f  _CHECK,      TK_
31861 4b 45 59 2c 20 20 20 20 20 20 20 20 54 4b 5f 42  KEY,        TK_B
31862 45 46 4f 52 45 2c 20 20 20 20 20 54 4b 5f 46 4f  EFORE,     TK_FO
31863 52 45 49 47 4e 2c 20 20 20 20 0a 20 20 20 20 54  REIGN,    .    T
31864 4b 5f 46 4f 52 2c 20 20 20 20 20 20 20 20 54 4b  K_FOR,        TK
31865 5f 49 47 4e 4f 52 45 2c 20 20 20 20 20 54 4b 5f  _IGNORE,     TK_
31866 4c 49 4b 45 5f 4b 57 2c 20 20 20 20 54 4b 5f 45  LIKE_KW,    TK_E
31867 58 50 4c 41 49 4e 2c 20 20 20 20 54 4b 5f 49 4e  XPLAIN,    TK_IN
31868 53 54 45 41 44 2c 20 20 20 20 0a 20 20 20 20 54  STEAD,    .    T
31869 4b 5f 41 44 44 2c 20 20 20 20 20 20 20 20 54 4b  K_ADD,        TK
3186a 5f 44 41 54 41 42 41 53 45 2c 20 20 20 54 4b 5f  _DATABASE,   TK_
3186b 41 53 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 53  AS,         TK_S
3186c 45 4c 45 43 54 2c 20 20 20 20 20 54 4b 5f 54 41  ELECT,     TK_TA
3186d 42 4c 45 2c 20 20 20 20 20 20 0a 20 20 20 20 54  BLE,      .    T
3186e 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b  K_JOIN_KW,    TK
3186f 5f 54 48 45 4e 2c 20 20 20 20 20 20 20 54 4b 5f  _THEN,       TK_
31870 45 4e 44 2c 20 20 20 20 20 20 20 20 54 4b 5f 44  END,        TK_D
31871 45 46 45 52 52 41 42 4c 45 2c 20 54 4b 5f 45 4c  EFERRABLE, TK_EL
31872 53 45 2c 20 20 20 20 20 20 20 0a 20 20 20 20 54  SE,       .    T
31873 4b 5f 45 58 43 45 50 54 2c 20 20 20 20 20 54 4b  K_EXCEPT,     TK
31874 5f 54 52 41 4e 53 41 43 54 49 4f 4e 2c 54 4b 5f  _TRANSACTION,TK_
31875 4f 4e 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 4a  ON,         TK_J
31876 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f 41 4c  OIN_KW,    TK_AL
31877 54 45 52 2c 20 20 20 20 20 20 0a 20 20 20 20 54  TER,      .    T
31878 4b 5f 52 41 49 53 45 2c 20 20 20 20 20 20 54 4b  K_RAISE,      TK
31879 5f 45 58 43 4c 55 53 49 56 45 2c 20 20 54 4b 5f  _EXCLUSIVE,  TK_
3187a 45 58 49 53 54 53 2c 20 20 20 20 20 54 4b 5f 53  EXISTS,     TK_S
3187b 41 56 45 50 4f 49 4e 54 2c 20 20 54 4b 5f 49 4e  AVEPOINT,  TK_IN
3187c 54 45 52 53 45 43 54 2c 20 20 0a 20 20 20 20 54  TERSECT,  .    T
3187d 4b 5f 54 52 49 47 47 45 52 2c 20 20 20 20 54 4b  K_TRIGGER,    TK
3187e 5f 52 45 46 45 52 45 4e 43 45 53 2c 20 54 4b 5f  _REFERENCES, TK_
3187f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 54 4b 5f 49  CONSTRAINT, TK_I
31880 4e 54 4f 2c 20 20 20 20 20 20 20 54 4b 5f 4f 46  NTO,       TK_OF
31881 46 53 45 54 2c 20 20 20 20 20 0a 20 20 20 20 54  FSET,     .    T
31882 4b 5f 4f 46 2c 20 20 20 20 20 20 20 20 20 54 4b  K_OF,         TK
31883 5f 53 45 54 2c 20 20 20 20 20 20 20 20 54 4b 5f  _SET,        TK_
31884 54 45 4d 50 2c 20 20 20 20 20 20 20 54 4b 5f 54  TEMP,       TK_T
31885 45 4d 50 2c 20 20 20 20 20 20 20 54 4b 5f 4f 52  EMP,       TK_OR
31886 2c 20 20 20 20 20 20 20 20 20 0a 20 20 20 20 54  ,         .    T
31887 4b 5f 55 4e 49 51 55 45 2c 20 20 20 20 20 54 4b  K_UNIQUE,     TK
31888 5f 51 55 45 52 59 2c 20 20 20 20 20 20 54 4b 5f  _QUERY,      TK_
31889 41 54 54 41 43 48 2c 20 20 20 20 20 54 4b 5f 48  ATTACH,     TK_H
3188a 41 56 49 4e 47 2c 20 20 20 20 20 54 4b 5f 47 52  AVING,     TK_GR
3188b 4f 55 50 2c 20 20 20 20 20 20 0a 20 20 20 20 54  OUP,      .    T
3188c 4b 5f 55 50 44 41 54 45 2c 20 20 20 20 20 54 4b  K_UPDATE,     TK
3188d 5f 42 45 47 49 4e 2c 20 20 20 20 20 20 54 4b 5f  _BEGIN,      TK_
3188e 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f 52  JOIN_KW,    TK_R
3188f 45 4c 45 41 53 45 2c 20 20 20 20 54 4b 5f 42 45  ELEASE,    TK_BE
31890 54 57 45 45 4e 2c 20 20 20 20 0a 20 20 20 20 54  TWEEN,    .    T
31891 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 20 20 20 54 4b  K_NOTNULL,    TK
31892 5f 4e 4f 54 2c 20 20 20 20 20 20 20 20 54 4b 5f  _NOT,        TK_
31893 4e 55 4c 4c 2c 20 20 20 20 20 20 20 54 4b 5f 4c  NULL,       TK_L
31894 49 4b 45 5f 4b 57 2c 20 20 20 20 54 4b 5f 43 41  IKE_KW,    TK_CA
31895 53 43 41 44 45 2c 20 20 20 20 0a 20 20 20 20 54  SCADE,    .    T
31896 4b 5f 41 53 43 2c 20 20 20 20 20 20 20 20 54 4b  K_ASC,        TK
31897 5f 44 45 4c 45 54 45 2c 20 20 20 20 20 54 4b 5f  _DELETE,     TK_
31898 43 41 53 45 2c 20 20 20 20 20 20 20 54 4b 5f 43  CASE,       TK_C
31899 4f 4c 4c 41 54 45 2c 20 20 20 20 54 4b 5f 43 52  OLLATE,    TK_CR
3189a 45 41 54 45 2c 20 20 20 20 20 0a 20 20 20 20 54  EATE,     .    T
3189b 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20 20 20 54 4b  K_CTIME_KW,   TK
3189c 5f 44 45 54 41 43 48 2c 20 20 20 20 20 54 4b 5f  _DETACH,     TK_
3189d 49 4d 4d 45 44 49 41 54 45 2c 20 20 54 4b 5f 4a  IMMEDIATE,  TK_J
3189e 4f 49 4e 2c 20 20 20 20 20 20 20 54 4b 5f 49 4e  OIN,       TK_IN
3189f 53 45 52 54 2c 20 20 20 20 20 0a 20 20 20 20 54  SERT,     .    T
318a0 4b 5f 4d 41 54 43 48 2c 20 20 20 20 20 20 54 4b  K_MATCH,      TK
318a1 5f 50 4c 41 4e 2c 20 20 20 20 20 20 20 54 4b 5f  _PLAN,       TK_
318a2 41 4e 41 4c 59 5a 45 2c 20 20 20 20 54 4b 5f 50  ANALYZE,    TK_P
318a3 52 41 47 4d 41 2c 20 20 20 20 20 54 4b 5f 41 42  RAGMA,     TK_AB
318a4 4f 52 54 2c 20 20 20 20 20 20 0a 20 20 20 20 54  ORT,      .    T
318a5 4b 5f 56 41 4c 55 45 53 2c 20 20 20 20 20 54 4b  K_VALUES,     TK
318a6 5f 56 49 52 54 55 41 4c 2c 20 20 20 20 54 4b 5f  _VIRTUAL,    TK_
318a7 4c 49 4d 49 54 2c 20 20 20 20 20 20 54 4b 5f 57  LIMIT,      TK_W
318a8 48 45 4e 2c 20 20 20 20 20 20 20 54 4b 5f 57 48  HEN,       TK_WH
318a9 45 52 45 2c 20 20 20 20 20 20 0a 20 20 20 20 54  ERE,      .    T
318aa 4b 5f 52 45 4e 41 4d 45 2c 20 20 20 20 20 54 4b  K_RENAME,     TK
318ab 5f 41 46 54 45 52 2c 20 20 20 20 20 20 54 4b 5f  _AFTER,      TK_
318ac 52 45 50 4c 41 43 45 2c 20 20 20 20 54 4b 5f 41  REPLACE,    TK_A
318ad 4e 44 2c 20 20 20 20 20 20 20 20 54 4b 5f 44 45  ND,        TK_DE
318ae 46 41 55 4c 54 2c 20 20 20 20 0a 20 20 20 20 54  FAULT,    .    T
318af 4b 5f 41 55 54 4f 49 4e 43 52 2c 20 20 20 54 4b  K_AUTOINCR,   TK
318b0 5f 54 4f 2c 20 20 20 20 20 20 20 20 20 54 4b 5f  _TO,         TK_
318b1 49 4e 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 43  IN,         TK_C
318b2 41 53 54 2c 20 20 20 20 20 20 20 54 4b 5f 43 4f  AST,       TK_CO
318b3 4c 55 4d 4e 4b 57 2c 20 20 20 0a 20 20 20 20 54  LUMNKW,   .    T
318b4 4b 5f 43 4f 4d 4d 49 54 2c 20 20 20 20 20 54 4b  K_COMMIT,     TK
318b5 5f 43 4f 4e 46 4c 49 43 54 2c 20 20 20 54 4b 5f  _CONFLICT,   TK_
318b6 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f 43  JOIN_KW,    TK_C
318b7 54 49 4d 45 5f 4b 57 2c 20 20 20 54 4b 5f 43 54  TIME_KW,   TK_CT
318b8 49 4d 45 5f 4b 57 2c 20 20 20 0a 20 20 20 20 54  IME_KW,   .    T
318b9 4b 5f 50 52 49 4d 41 52 59 2c 20 20 20 20 54 4b  K_PRIMARY,    TK
318ba 5f 44 45 46 45 52 52 45 44 2c 20 20 20 54 4b 5f  _DEFERRED,   TK_
318bb 44 49 53 54 49 4e 43 54 2c 20 20 20 54 4b 5f 49  DISTINCT,   TK_I
318bc 53 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 44 52  S,         TK_DR
318bd 4f 50 2c 20 20 20 20 20 20 20 0a 20 20 20 20 54  OP,       .    T
318be 4b 5f 46 41 49 4c 2c 20 20 20 20 20 20 20 54 4b  K_FAIL,       TK
318bf 5f 46 52 4f 4d 2c 20 20 20 20 20 20 20 54 4b 5f  _FROM,       TK_
318c0 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f 4c  JOIN_KW,    TK_L
318c1 49 4b 45 5f 4b 57 2c 20 20 20 20 54 4b 5f 42 59  IKE_KW,    TK_BY
318c2 2c 20 20 20 20 20 20 20 20 20 0a 20 20 20 20 54  ,         .    T
318c3 4b 5f 49 46 2c 20 20 20 20 20 20 20 20 20 54 4b  K_IF,         TK
318c4 5f 49 53 4e 55 4c 4c 2c 20 20 20 20 20 54 4b 5f  _ISNULL,     TK_
318c5 4f 52 44 45 52 2c 20 20 20 20 20 20 54 4b 5f 52  ORDER,      TK_R
318c6 45 53 54 52 49 43 54 2c 20 20 20 54 4b 5f 4a 4f  ESTRICT,   TK_JO
318c7 49 4e 5f 4b 57 2c 20 20 20 20 0a 20 20 20 20 54  IN_KW,    .    T
318c8 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b  K_JOIN_KW,    TK
318c9 5f 52 4f 4c 4c 42 41 43 4b 2c 20 20 20 54 4b 5f  _ROLLBACK,   TK_
318ca 52 4f 57 2c 20 20 20 20 20 20 20 20 54 4b 5f 55  ROW,        TK_U
318cb 4e 49 4f 4e 2c 20 20 20 20 20 20 54 4b 5f 55 53  NION,      TK_US
318cc 49 4e 47 2c 20 20 20 20 20 20 0a 20 20 20 20 54  ING,      .    T
318cd 4b 5f 56 41 43 55 55 4d 2c 20 20 20 20 20 54 4b  K_VACUUM,     TK
318ce 5f 56 49 45 57 2c 20 20 20 20 20 20 20 54 4b 5f  _VIEW,       TK_
318cf 49 4e 49 54 49 41 4c 4c 59 2c 20 20 54 4b 5f 41  INITIALLY,  TK_A
318d0 4c 4c 2c 20 20 20 20 20 20 20 20 0a 20 20 7d 3b  LL,        .  };
318d1 0a 20 20 69 6e 74 20 68 2c 20 69 3b 0a 20 20 69  .  int h, i;.  i
318d2 66 28 20 6e 3c 32 20 29 20 72 65 74 75 72 6e 20  f( n<2 ) return 
318d3 54 4b 5f 49 44 3b 0a 20 20 68 20 3d 20 28 28 63  TK_ID;.  h = ((c
318d4 68 61 72 4d 61 70 28 7a 5b 30 5d 29 2a 34 29 20  harMap(z[0])*4) 
318d5 5e 0a 20 20 20 20 20 20 28 63 68 61 72 4d 61 70  ^.      (charMap
318d6 28 7a 5b 6e 2d 31 5d 29 2a 33 29 20 5e 0a 20 20  (z[n-1])*3) ^.  
318d7 20 20 20 20 6e 29 20 25 20 31 32 37 3b 0a 20 20      n) % 127;.  
318d8 66 6f 72 28 69 3d 28 28 69 6e 74 29 61 48 61 73  for(i=((int)aHas
318d9 68 5b 68 5d 29 2d 31 3b 20 69 3e 3d 30 3b 20 69  h[h])-1; i>=0; i
318da 3d 28 28 69 6e 74 29 61 4e 65 78 74 5b 69 5d 29  =((int)aNext[i])
318db 2d 31 29 7b 0a 20 20 20 20 69 66 28 20 61 4c 65  -1){.    if( aLe
318dc 6e 5b 69 5d 3d 3d 6e 20 26 26 20 73 71 6c 69 74  n[i]==n && sqlit
318dd 65 33 53 74 72 4e 49 43 6d 70 28 26 7a 54 65 78  e3StrNICmp(&zTex
318de 74 5b 61 4f 66 66 73 65 74 5b 69 5d 5d 2c 7a 2c  t[aOffset[i]],z,
318df 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 74  n)==0 ){.      t
318e0 65 73 74 63 61 73 65 28 20 69 3d 3d 30 20 29 3b  estcase( i==0 );
318e1 20 2f 2a 20 52 45 49 4e 44 45 58 20 2a 2f 0a 20   /* REINDEX */. 
318e2 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
318e3 3d 3d 31 20 29 3b 20 2f 2a 20 49 4e 44 45 58 45  ==1 ); /* INDEXE
318e4 44 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  D */.      testc
318e5 61 73 65 28 20 69 3d 3d 32 20 29 3b 20 2f 2a 20  ase( i==2 ); /* 
318e6 49 4e 44 45 58 20 2a 2f 0a 20 20 20 20 20 20 74  INDEX */.      t
318e7 65 73 74 63 61 73 65 28 20 69 3d 3d 33 20 29 3b  estcase( i==3 );
318e8 20 2f 2a 20 44 45 53 43 20 2a 2f 0a 20 20 20 20   /* DESC */.    
318e9 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34    testcase( i==4
318ea 20 29 3b 20 2f 2a 20 45 53 43 41 50 45 20 2a 2f   ); /* ESCAPE */
318eb 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
318ec 20 69 3d 3d 35 20 29 3b 20 2f 2a 20 45 41 43 48   i==5 ); /* EACH
318ed 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
318ee 73 65 28 20 69 3d 3d 36 20 29 3b 20 2f 2a 20 43  se( i==6 ); /* C
318ef 48 45 43 4b 20 2a 2f 0a 20 20 20 20 20 20 74 65  HECK */.      te
318f0 73 74 63 61 73 65 28 20 69 3d 3d 37 20 29 3b 20  stcase( i==7 ); 
318f1 2f 2a 20 4b 45 59 20 2a 2f 0a 20 20 20 20 20 20  /* KEY */.      
318f2 74 65 73 74 63 61 73 65 28 20 69 3d 3d 38 20 29  testcase( i==8 )
318f3 3b 20 2f 2a 20 42 45 46 4f 52 45 20 2a 2f 0a 20  ; /* BEFORE */. 
318f4 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
318f5 3d 3d 39 20 29 3b 20 2f 2a 20 46 4f 52 45 49 47  ==9 ); /* FOREIG
318f6 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  N */.      testc
318f7 61 73 65 28 20 69 3d 3d 31 30 20 29 3b 20 2f 2a  ase( i==10 ); /*
318f8 20 46 4f 52 20 2a 2f 0a 20 20 20 20 20 20 74 65   FOR */.      te
318f9 73 74 63 61 73 65 28 20 69 3d 3d 31 31 20 29 3b  stcase( i==11 );
318fa 20 2f 2a 20 49 47 4e 4f 52 45 20 2a 2f 0a 20 20   /* IGNORE */.  
318fb 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
318fc 3d 31 32 20 29 3b 20 2f 2a 20 52 45 47 45 58 50  =12 ); /* REGEXP
318fd 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
318fe 73 65 28 20 69 3d 3d 31 33 20 29 3b 20 2f 2a 20  se( i==13 ); /* 
318ff 45 58 50 4c 41 49 4e 20 2a 2f 0a 20 20 20 20 20  EXPLAIN */.     
31900 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 34   testcase( i==14
31901 20 29 3b 20 2f 2a 20 49 4e 53 54 45 41 44 20 2a   ); /* INSTEAD *
31902 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
31903 28 20 69 3d 3d 31 35 20 29 3b 20 2f 2a 20 41 44  ( i==15 ); /* AD
31904 44 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  D */.      testc
31905 61 73 65 28 20 69 3d 3d 31 36 20 29 3b 20 2f 2a  ase( i==16 ); /*
31906 20 44 41 54 41 42 41 53 45 20 2a 2f 0a 20 20 20   DATABASE */.   
31907 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
31908 31 37 20 29 3b 20 2f 2a 20 41 53 20 2a 2f 0a 20  17 ); /* AS */. 
31909 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3190a 3d 3d 31 38 20 29 3b 20 2f 2a 20 53 45 4c 45 43  ==18 ); /* SELEC
3190b 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  T */.      testc
3190c 61 73 65 28 20 69 3d 3d 31 39 20 29 3b 20 2f 2a  ase( i==19 ); /*
3190d 20 54 41 42 4c 45 20 2a 2f 0a 20 20 20 20 20 20   TABLE */.      
3190e 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32 30 20  testcase( i==20 
3190f 29 3b 20 2f 2a 20 4c 45 46 54 20 2a 2f 0a 20 20  ); /* LEFT */.  
31910 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
31911 3d 32 31 20 29 3b 20 2f 2a 20 54 48 45 4e 20 2a  =21 ); /* THEN *
31912 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
31913 28 20 69 3d 3d 32 32 20 29 3b 20 2f 2a 20 45 4e  ( i==22 ); /* EN
31914 44 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  D */.      testc
31915 61 73 65 28 20 69 3d 3d 32 33 20 29 3b 20 2f 2a  ase( i==23 ); /*
31916 20 44 45 46 45 52 52 41 42 4c 45 20 2a 2f 0a 20   DEFERRABLE */. 
31917 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
31918 3d 3d 32 34 20 29 3b 20 2f 2a 20 45 4c 53 45 20  ==24 ); /* ELSE 
31919 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
3191a 65 28 20 69 3d 3d 32 35 20 29 3b 20 2f 2a 20 45  e( i==25 ); /* E
3191b 58 43 45 50 54 20 2a 2f 0a 20 20 20 20 20 20 74  XCEPT */.      t
3191c 65 73 74 63 61 73 65 28 20 69 3d 3d 32 36 20 29  estcase( i==26 )
3191d 3b 20 2f 2a 20 54 52 41 4e 53 41 43 54 49 4f 4e  ; /* TRANSACTION
3191e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
3191f 73 65 28 20 69 3d 3d 32 37 20 29 3b 20 2f 2a 20  se( i==27 ); /* 
31920 4f 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  ON */.      test
31921 63 61 73 65 28 20 69 3d 3d 32 38 20 29 3b 20 2f  case( i==28 ); /
31922 2a 20 4e 41 54 55 52 41 4c 20 2a 2f 0a 20 20 20  * NATURAL */.   
31923 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
31924 32 39 20 29 3b 20 2f 2a 20 41 4c 54 45 52 20 2a  29 ); /* ALTER *
31925 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
31926 28 20 69 3d 3d 33 30 20 29 3b 20 2f 2a 20 52 41  ( i==30 ); /* RA
31927 49 53 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ISE */.      tes
31928 74 63 61 73 65 28 20 69 3d 3d 33 31 20 29 3b 20  tcase( i==31 ); 
31929 2f 2a 20 45 58 43 4c 55 53 49 56 45 20 2a 2f 0a  /* EXCLUSIVE */.
3192a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
3192b 69 3d 3d 33 32 20 29 3b 20 2f 2a 20 45 58 49 53  i==32 ); /* EXIS
3192c 54 53 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  TS */.      test
3192d 63 61 73 65 28 20 69 3d 3d 33 33 20 29 3b 20 2f  case( i==33 ); /
3192e 2a 20 53 41 56 45 50 4f 49 4e 54 20 2a 2f 0a 20  * SAVEPOINT */. 
3192f 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
31930 3d 3d 33 34 20 29 3b 20 2f 2a 20 49 4e 54 45 52  ==34 ); /* INTER
31931 53 45 43 54 20 2a 2f 0a 20 20 20 20 20 20 74 65  SECT */.      te
31932 73 74 63 61 73 65 28 20 69 3d 3d 33 35 20 29 3b  stcase( i==35 );
31933 20 2f 2a 20 54 52 49 47 47 45 52 20 2a 2f 0a 20   /* TRIGGER */. 
31934 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
31935 3d 3d 33 36 20 29 3b 20 2f 2a 20 52 45 46 45 52  ==36 ); /* REFER
31936 45 4e 43 45 53 20 2a 2f 0a 20 20 20 20 20 20 74  ENCES */.      t
31937 65 73 74 63 61 73 65 28 20 69 3d 3d 33 37 20 29  estcase( i==37 )
31938 3b 20 2f 2a 20 43 4f 4e 53 54 52 41 49 4e 54 20  ; /* CONSTRAINT 
31939 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
3193a 65 28 20 69 3d 3d 33 38 20 29 3b 20 2f 2a 20 49  e( i==38 ); /* I
3193b 4e 54 4f 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  NTO */.      tes
3193c 74 63 61 73 65 28 20 69 3d 3d 33 39 20 29 3b 20  tcase( i==39 ); 
3193d 2f 2a 20 4f 46 46 53 45 54 20 2a 2f 0a 20 20 20  /* OFFSET */.   
3193e 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3193f 34 30 20 29 3b 20 2f 2a 20 4f 46 20 2a 2f 0a 20  40 ); /* OF */. 
31940 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
31941 3d 3d 34 31 20 29 3b 20 2f 2a 20 53 45 54 20 2a  ==41 ); /* SET *
31942 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
31943 28 20 69 3d 3d 34 32 20 29 3b 20 2f 2a 20 54 45  ( i==42 ); /* TE
31944 4d 50 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  MP */.      test
31945 63 61 73 65 28 20 69 3d 3d 34 33 20 29 3b 20 2f  case( i==43 ); /
31946 2a 20 54 45 4d 50 4f 52 41 52 59 20 2a 2f 0a 20  * TEMPORARY */. 
31947 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
31948 3d 3d 34 34 20 29 3b 20 2f 2a 20 4f 52 20 2a 2f  ==44 ); /* OR */
31949 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
3194a 20 69 3d 3d 34 35 20 29 3b 20 2f 2a 20 55 4e 49   i==45 ); /* UNI
3194b 51 55 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  QUE */.      tes
3194c 74 63 61 73 65 28 20 69 3d 3d 34 36 20 29 3b 20  tcase( i==46 ); 
3194d 2f 2a 20 51 55 45 52 59 20 2a 2f 0a 20 20 20 20  /* QUERY */.    
3194e 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34    testcase( i==4
3194f 37 20 29 3b 20 2f 2a 20 41 54 54 41 43 48 20 2a  7 ); /* ATTACH *
31950 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
31951 28 20 69 3d 3d 34 38 20 29 3b 20 2f 2a 20 48 41  ( i==48 ); /* HA
31952 56 49 4e 47 20 2a 2f 0a 20 20 20 20 20 20 74 65  VING */.      te
31953 73 74 63 61 73 65 28 20 69 3d 3d 34 39 20 29 3b  stcase( i==49 );
31954 20 2f 2a 20 47 52 4f 55 50 20 2a 2f 0a 20 20 20   /* GROUP */.   
31955 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
31956 35 30 20 29 3b 20 2f 2a 20 55 50 44 41 54 45 20  50 ); /* UPDATE 
31957 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
31958 65 28 20 69 3d 3d 35 31 20 29 3b 20 2f 2a 20 42  e( i==51 ); /* B
31959 45 47 49 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65  EGIN */.      te
3195a 73 74 63 61 73 65 28 20 69 3d 3d 35 32 20 29 3b  stcase( i==52 );
3195b 20 2f 2a 20 49 4e 4e 45 52 20 2a 2f 0a 20 20 20   /* INNER */.   
3195c 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3195d 35 33 20 29 3b 20 2f 2a 20 52 45 4c 45 41 53 45  53 ); /* RELEASE
3195e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
3195f 73 65 28 20 69 3d 3d 35 34 20 29 3b 20 2f 2a 20  se( i==54 ); /* 
31960 42 45 54 57 45 45 4e 20 2a 2f 0a 20 20 20 20 20  BETWEEN */.     
31961 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35 35   testcase( i==55
31962 20 29 3b 20 2f 2a 20 4e 4f 54 4e 55 4c 4c 20 2a   ); /* NOTNULL *
31963 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
31964 28 20 69 3d 3d 35 36 20 29 3b 20 2f 2a 20 4e 4f  ( i==56 ); /* NO
31965 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  T */.      testc
31966 61 73 65 28 20 69 3d 3d 35 37 20 29 3b 20 2f 2a  ase( i==57 ); /*
31967 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 74   NULL */.      t
31968 65 73 74 63 61 73 65 28 20 69 3d 3d 35 38 20 29  estcase( i==58 )
31969 3b 20 2f 2a 20 4c 49 4b 45 20 2a 2f 0a 20 20 20  ; /* LIKE */.   
3196a 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3196b 35 39 20 29 3b 20 2f 2a 20 43 41 53 43 41 44 45  59 ); /* CASCADE
3196c 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
3196d 73 65 28 20 69 3d 3d 36 30 20 29 3b 20 2f 2a 20  se( i==60 ); /* 
3196e 41 53 43 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ASC */.      tes
3196f 74 63 61 73 65 28 20 69 3d 3d 36 31 20 29 3b 20  tcase( i==61 ); 
31970 2f 2a 20 44 45 4c 45 54 45 20 2a 2f 0a 20 20 20  /* DELETE */.   
31971 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
31972 36 32 20 29 3b 20 2f 2a 20 43 41 53 45 20 2a 2f  62 ); /* CASE */
31973 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
31974 20 69 3d 3d 36 33 20 29 3b 20 2f 2a 20 43 4f 4c   i==63 ); /* COL
31975 4c 41 54 45 20 2a 2f 0a 20 20 20 20 20 20 74 65  LATE */.      te
31976 73 74 63 61 73 65 28 20 69 3d 3d 36 34 20 29 3b  stcase( i==64 );
31977 20 2f 2a 20 43 52 45 41 54 45 20 2a 2f 0a 20 20   /* CREATE */.  
31978 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
31979 3d 36 35 20 29 3b 20 2f 2a 20 43 55 52 52 45 4e  =65 ); /* CURREN
3197a 54 5f 44 41 54 45 20 2a 2f 0a 20 20 20 20 20 20  T_DATE */.      
3197b 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36 36 20  testcase( i==66 
3197c 29 3b 20 2f 2a 20 44 45 54 41 43 48 20 2a 2f 0a  ); /* DETACH */.
3197d 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
3197e 69 3d 3d 36 37 20 29 3b 20 2f 2a 20 49 4d 4d 45  i==67 ); /* IMME
3197f 44 49 41 54 45 20 2a 2f 0a 20 20 20 20 20 20 74  DIATE */.      t
31980 65 73 74 63 61 73 65 28 20 69 3d 3d 36 38 20 29  estcase( i==68 )
31981 3b 20 2f 2a 20 4a 4f 49 4e 20 2a 2f 0a 20 20 20  ; /* JOIN */.   
31982 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
31983 36 39 20 29 3b 20 2f 2a 20 49 4e 53 45 52 54 20  69 ); /* INSERT 
31984 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
31985 65 28 20 69 3d 3d 37 30 20 29 3b 20 2f 2a 20 4d  e( i==70 ); /* M
31986 41 54 43 48 20 2a 2f 0a 20 20 20 20 20 20 74 65  ATCH */.      te
31987 73 74 63 61 73 65 28 20 69 3d 3d 37 31 20 29 3b  stcase( i==71 );
31988 20 2f 2a 20 50 4c 41 4e 20 2a 2f 0a 20 20 20 20   /* PLAN */.    
31989 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37    testcase( i==7
3198a 32 20 29 3b 20 2f 2a 20 41 4e 41 4c 59 5a 45 20  2 ); /* ANALYZE 
3198b 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
3198c 65 28 20 69 3d 3d 37 33 20 29 3b 20 2f 2a 20 50  e( i==73 ); /* P
3198d 52 41 47 4d 41 20 2a 2f 0a 20 20 20 20 20 20 74  RAGMA */.      t
3198e 65 73 74 63 61 73 65 28 20 69 3d 3d 37 34 20 29  estcase( i==74 )
3198f 3b 20 2f 2a 20 41 42 4f 52 54 20 2a 2f 0a 20 20  ; /* ABORT */.  
31990 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
31991 3d 37 35 20 29 3b 20 2f 2a 20 56 41 4c 55 45 53  =75 ); /* VALUES
31992 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
31993 73 65 28 20 69 3d 3d 37 36 20 29 3b 20 2f 2a 20  se( i==76 ); /* 
31994 56 49 52 54 55 41 4c 20 2a 2f 0a 20 20 20 20 20  VIRTUAL */.     
31995 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37 37   testcase( i==77
31996 20 29 3b 20 2f 2a 20 4c 49 4d 49 54 20 2a 2f 0a   ); /* LIMIT */.
31997 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
31998 69 3d 3d 37 38 20 29 3b 20 2f 2a 20 57 48 45 4e  i==78 ); /* WHEN
31999 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
3199a 73 65 28 20 69 3d 3d 37 39 20 29 3b 20 2f 2a 20  se( i==79 ); /* 
3199b 57 48 45 52 45 20 2a 2f 0a 20 20 20 20 20 20 74  WHERE */.      t
3199c 65 73 74 63 61 73 65 28 20 69 3d 3d 38 30 20 29  estcase( i==80 )
3199d 3b 20 2f 2a 20 52 45 4e 41 4d 45 20 2a 2f 0a 20  ; /* RENAME */. 
3199e 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3199f 3d 3d 38 31 20 29 3b 20 2f 2a 20 41 46 54 45 52  ==81 ); /* AFTER
319a0 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
319a1 73 65 28 20 69 3d 3d 38 32 20 29 3b 20 2f 2a 20  se( i==82 ); /* 
319a2 52 45 50 4c 41 43 45 20 2a 2f 0a 20 20 20 20 20  REPLACE */.     
319a3 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 38 33   testcase( i==83
319a4 20 29 3b 20 2f 2a 20 41 4e 44 20 2a 2f 0a 20 20   ); /* AND */.  
319a5 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
319a6 3d 38 34 20 29 3b 20 2f 2a 20 44 45 46 41 55 4c  =84 ); /* DEFAUL
319a7 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  T */.      testc
319a8 61 73 65 28 20 69 3d 3d 38 35 20 29 3b 20 2f 2a  ase( i==85 ); /*
319a9 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a   AUTOINCREMENT *
319aa 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
319ab 28 20 69 3d 3d 38 36 20 29 3b 20 2f 2a 20 54 4f  ( i==86 ); /* TO
319ac 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
319ad 73 65 28 20 69 3d 3d 38 37 20 29 3b 20 2f 2a 20  se( i==87 ); /* 
319ae 49 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  IN */.      test
319af 63 61 73 65 28 20 69 3d 3d 38 38 20 29 3b 20 2f  case( i==88 ); /
319b0 2a 20 43 41 53 54 20 2a 2f 0a 20 20 20 20 20 20  * CAST */.      
319b1 74 65 73 74 63 61 73 65 28 20 69 3d 3d 38 39 20  testcase( i==89 
319b2 29 3b 20 2f 2a 20 43 4f 4c 55 4d 4e 20 2a 2f 0a  ); /* COLUMN */.
319b3 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
319b4 69 3d 3d 39 30 20 29 3b 20 2f 2a 20 43 4f 4d 4d  i==90 ); /* COMM
319b5 49 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  IT */.      test
319b6 63 61 73 65 28 20 69 3d 3d 39 31 20 29 3b 20 2f  case( i==91 ); /
319b7 2a 20 43 4f 4e 46 4c 49 43 54 20 2a 2f 0a 20 20  * CONFLICT */.  
319b8 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
319b9 3d 39 32 20 29 3b 20 2f 2a 20 43 52 4f 53 53 20  =92 ); /* CROSS 
319ba 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
319bb 65 28 20 69 3d 3d 39 33 20 29 3b 20 2f 2a 20 43  e( i==93 ); /* C
319bc 55 52 52 45 4e 54 5f 54 49 4d 45 53 54 41 4d 50  URRENT_TIMESTAMP
319bd 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
319be 73 65 28 20 69 3d 3d 39 34 20 29 3b 20 2f 2a 20  se( i==94 ); /* 
319bf 43 55 52 52 45 4e 54 5f 54 49 4d 45 20 2a 2f 0a  CURRENT_TIME */.
319c0 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
319c1 69 3d 3d 39 35 20 29 3b 20 2f 2a 20 50 52 49 4d  i==95 ); /* PRIM
319c2 41 52 59 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ARY */.      tes
319c3 74 63 61 73 65 28 20 69 3d 3d 39 36 20 29 3b 20  tcase( i==96 ); 
319c4 2f 2a 20 44 45 46 45 52 52 45 44 20 2a 2f 0a 20  /* DEFERRED */. 
319c5 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
319c6 3d 3d 39 37 20 29 3b 20 2f 2a 20 44 49 53 54 49  ==97 ); /* DISTI
319c7 4e 43 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  NCT */.      tes
319c8 74 63 61 73 65 28 20 69 3d 3d 39 38 20 29 3b 20  tcase( i==98 ); 
319c9 2f 2a 20 49 53 20 2a 2f 0a 20 20 20 20 20 20 74  /* IS */.      t
319ca 65 73 74 63 61 73 65 28 20 69 3d 3d 39 39 20 29  estcase( i==99 )
319cb 3b 20 2f 2a 20 44 52 4f 50 20 2a 2f 0a 20 20 20  ; /* DROP */.   
319cc 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
319cd 31 30 30 20 29 3b 20 2f 2a 20 46 41 49 4c 20 2a  100 ); /* FAIL *
319ce 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
319cf 28 20 69 3d 3d 31 30 31 20 29 3b 20 2f 2a 20 46  ( i==101 ); /* F
319d0 52 4f 4d 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ROM */.      tes
319d1 74 63 61 73 65 28 20 69 3d 3d 31 30 32 20 29 3b  tcase( i==102 );
319d2 20 2f 2a 20 46 55 4c 4c 20 2a 2f 0a 20 20 20 20   /* FULL */.    
319d3 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
319d4 30 33 20 29 3b 20 2f 2a 20 47 4c 4f 42 20 2a 2f  03 ); /* GLOB */
319d5 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
319d6 20 69 3d 3d 31 30 34 20 29 3b 20 2f 2a 20 42 59   i==104 ); /* BY
319d7 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
319d8 73 65 28 20 69 3d 3d 31 30 35 20 29 3b 20 2f 2a  se( i==105 ); /*
319d9 20 49 46 20 2a 2f 0a 20 20 20 20 20 20 74 65 73   IF */.      tes
319da 74 63 61 73 65 28 20 69 3d 3d 31 30 36 20 29 3b  tcase( i==106 );
319db 20 2f 2a 20 49 53 4e 55 4c 4c 20 2a 2f 0a 20 20   /* ISNULL */.  
319dc 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
319dd 3d 31 30 37 20 29 3b 20 2f 2a 20 4f 52 44 45 52  =107 ); /* ORDER
319de 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
319df 73 65 28 20 69 3d 3d 31 30 38 20 29 3b 20 2f 2a  se( i==108 ); /*
319e0 20 52 45 53 54 52 49 43 54 20 2a 2f 0a 20 20 20   RESTRICT */.   
319e1 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
319e2 31 30 39 20 29 3b 20 2f 2a 20 4f 55 54 45 52 20  109 ); /* OUTER 
319e3 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
319e4 65 28 20 69 3d 3d 31 31 30 20 29 3b 20 2f 2a 20  e( i==110 ); /* 
319e5 52 49 47 48 54 20 2a 2f 0a 20 20 20 20 20 20 74  RIGHT */.      t
319e6 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31 31 20  estcase( i==111 
319e7 29 3b 20 2f 2a 20 52 4f 4c 4c 42 41 43 4b 20 2a  ); /* ROLLBACK *
319e8 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
319e9 28 20 69 3d 3d 31 31 32 20 29 3b 20 2f 2a 20 52  ( i==112 ); /* R
319ea 4f 57 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  OW */.      test
319eb 63 61 73 65 28 20 69 3d 3d 31 31 33 20 29 3b 20  case( i==113 ); 
319ec 2f 2a 20 55 4e 49 4f 4e 20 2a 2f 0a 20 20 20 20  /* UNION */.    
319ed 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
319ee 31 34 20 29 3b 20 2f 2a 20 55 53 49 4e 47 20 2a  14 ); /* USING *
319ef 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
319f0 28 20 69 3d 3d 31 31 35 20 29 3b 20 2f 2a 20 56  ( i==115 ); /* V
319f1 41 43 55 55 4d 20 2a 2f 0a 20 20 20 20 20 20 74  ACUUM */.      t
319f2 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31 36 20  estcase( i==116 
319f3 29 3b 20 2f 2a 20 56 49 45 57 20 2a 2f 0a 20 20  ); /* VIEW */.  
319f4 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
319f5 3d 31 31 37 20 29 3b 20 2f 2a 20 49 4e 49 54 49  =117 ); /* INITI
319f6 41 4c 4c 59 20 2a 2f 0a 20 20 20 20 20 20 74 65  ALLY */.      te
319f7 73 74 63 61 73 65 28 20 69 3d 3d 31 31 38 20 29  stcase( i==118 )
319f8 3b 20 2f 2a 20 41 4c 4c 20 2a 2f 0a 20 20 20 20  ; /* ALL */.    
319f9 20 20 72 65 74 75 72 6e 20 61 43 6f 64 65 5b 69    return aCode[i
319fa 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  ];.    }.  }.  r
319fb 65 74 75 72 6e 20 54 4b 5f 49 44 3b 0a 7d 0a 53  eturn TK_ID;.}.S
319fc 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
319fd 74 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64  t sqlite3Keyword
319fe 43 6f 64 65 28 63 6f 6e 73 74 20 75 6e 73 69 67  Code(const unsig
319ff 6e 65 64 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  ned char *z, int
31a00 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 6b 65   n){.  return ke
31a01 79 77 6f 72 64 43 6f 64 65 28 28 63 68 61 72 2a  ywordCode((char*
31a02 29 7a 2c 20 6e 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  )z, n);.}../****
31a03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
31a04 66 20 6b 65 79 77 6f 72 64 68 61 73 68 2e 68 20  f keywordhash.h 
31a05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31a06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31a07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
31a08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
31a09 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
31a0a 65 66 74 20 6f 66 66 20 69 6e 20 74 6f 6b 65 6e  eft off in token
31a0b 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ize.c **********
31a0c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 0a  *********/.../*.
31a0d 2a 2a 20 49 66 20 58 20 69 73 20 61 20 63 68 61  ** If X is a cha
31a0e 72 61 63 74 65 72 20 74 68 61 74 20 63 61 6e 20  racter that can 
31a0f 62 65 20 75 73 65 64 20 69 6e 20 61 6e 20 69 64  be used in an id
31a10 65 6e 74 69 66 69 65 72 20 74 68 65 6e 0a 2a 2a  entifier then.**
31a11 20 49 64 43 68 61 72 28 58 29 20 77 69 6c 6c 20   IdChar(X) will 
31a12 62 65 20 74 72 75 65 2e 20 20 4f 74 68 65 72 77  be true.  Otherw
31a13 69 73 65 20 69 74 20 69 73 20 66 61 6c 73 65 2e  ise it is false.
31a14 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 41 53 43 49 49  .**.** For ASCII
31a15 2c 20 61 6e 79 20 63 68 61 72 61 63 74 65 72 20  , any character 
31a16 77 69 74 68 20 74 68 65 20 68 69 67 68 2d 6f 72  with the high-or
31a17 64 65 72 20 62 69 74 20 73 65 74 20 69 73 0a 2a  der bit set is.*
31a18 2a 20 61 6c 6c 6f 77 65 64 20 69 6e 20 61 6e 20  * allowed in an 
31a19 69 64 65 6e 74 69 66 69 65 72 2e 20 20 46 6f 72  identifier.  For
31a1a 20 37 2d 62 69 74 20 63 68 61 72 61 63 74 65 72   7-bit character
31a1b 73 2c 20 0a 2a 2a 20 73 71 6c 69 74 65 33 49 73  s, .** sqlite3Is
31a1c 49 64 43 68 61 72 5b 58 5d 20 6d 75 73 74 20 62  IdChar[X] must b
31a1d 65 20 31 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 45  e 1..**.** For E
31a1e 42 43 44 49 43 2c 20 74 68 65 20 72 75 6c 65 73  BCDIC, the rules
31a1f 20 61 72 65 20 6d 6f 72 65 20 63 6f 6d 70 6c 65   are more comple
31a20 78 20 62 75 74 20 68 61 76 65 20 74 68 65 20 73  x but have the s
31a21 61 6d 65 0a 2a 2a 20 65 6e 64 20 72 65 73 75 6c  ame.** end resul
31a22 74 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20  t..**.** Ticket 
31a23 23 31 30 36 36 2e 20 20 74 68 65 20 53 51 4c 20  #1066.  the SQL 
31a24 73 74 61 6e 64 61 72 64 20 64 6f 65 73 20 6e 6f  standard does no
31a25 74 20 61 6c 6c 6f 77 20 27 24 27 20 69 6e 20 74  t allow '$' in t
31a26 68 65 0a 2a 2a 20 6d 69 64 64 6c 65 20 6f 66 20  he.** middle of 
31a27 69 64 65 6e 74 66 69 65 72 73 2e 20 20 42 75 74  identfiers.  But
31a28 20 6d 61 6e 79 20 53 51 4c 20 69 6d 70 6c 65 6d   many SQL implem
31a29 65 6e 74 61 74 69 6f 6e 73 20 64 6f 2e 20 0a 2a  entations do. .*
31a2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c  * SQLite will al
31a2b 6c 6f 77 20 27 24 27 20 69 6e 20 69 64 65 6e 74  low '$' in ident
31a2c 69 66 69 65 72 73 20 66 6f 72 20 63 6f 6d 70 61  ifiers for compa
31a2d 74 69 62 69 6c 69 74 79 2e 0a 2a 2a 20 42 75 74  tibility..** But
31a2e 20 74 68 65 20 66 65 61 74 75 72 65 20 69 73 20   the feature is 
31a2f 75 6e 64 6f 63 75 6d 65 6e 74 65 64 2e 0a 2a 2f  undocumented..*/
31a30 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41  .#ifdef SQLITE_A
31a31 53 43 49 49 0a 53 51 4c 49 54 45 5f 50 52 49 56  SCII.SQLITE_PRIV
31a32 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 73  ATE const char s
31a33 71 6c 69 74 65 33 49 73 41 73 63 69 69 49 64 43  qlite3IsAsciiIdC
31a34 68 61 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20  har[] = {./* x0 
31a35 78 31 20 78 32 20 78 33 20 78 34 20 78 35 20 78  x1 x2 x3 x4 x5 x
31a36 36 20 78 37 20 78 38 20 78 39 20 78 41 20 78 42  6 x7 x8 x9 xA xB
31a37 20 78 43 20 78 44 20 78 45 20 78 46 20 2a 2f 0a   xC xD xE xF */.
31a38 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20      0, 0, 0, 0, 
31a39 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 0, 0, 0, 0, 0
31a3a 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
31a3b 20 30 2c 20 20 2f 2a 20 32 78 20 2a 2f 0a 20 20   0,  /* 2x */.  
31a3c 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    1, 1, 1, 1, 1,
31a3d 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
31a3e 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
31a3f 2c 20 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 20  ,  /* 3x */.    
31a40 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
31a41 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
31a42 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
31a43 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20 31 2c   /* 4x */.    1,
31a44 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
31a45 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 1, 1, 1, 1, 0
31a46 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 20 2f  , 0, 0, 0, 1,  /
31a47 2a 20 35 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31  * 5x */.    0, 1
31a48 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
31a49 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
31a4a 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20  1, 1, 1, 1,  /* 
31a4b 36 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20  6x */.    1, 1, 
31a4c 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
31a4d 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c  , 1, 1, 1, 0, 0,
31a4e 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 37 78   0, 0, 0,  /* 7x
31a4f 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 49   */.};.#define I
31a50 64 43 68 61 72 28 43 29 20 20 28 28 28 63 3d 43  dChar(C)  (((c=C
31a51 29 26 30 78 38 30 29 21 3d 30 20 7c 7c 20 28 63  )&0x80)!=0 || (c
31a52 3e 30 78 31 66 20 26 26 20 73 71 6c 69 74 65 33  >0x1f && sqlite3
31a53 49 73 41 73 63 69 69 49 64 43 68 61 72 5b 63 2d  IsAsciiIdChar[c-
31a54 30 78 32 30 5d 29 29 0a 23 65 6e 64 69 66 0a 23  0x20])).#endif.#
31a55 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43  ifdef SQLITE_EBC
31a56 44 49 43 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  DIC.SQLITE_PRIVA
31a57 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71  TE const char sq
31a58 6c 69 74 65 33 49 73 45 62 63 64 69 63 49 64 43  lite3IsEbcdicIdC
31a59 68 61 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20  har[] = {./* x0 
31a5a 78 31 20 78 32 20 78 33 20 78 34 20 78 35 20 78  x1 x2 x3 x4 x5 x
31a5b 36 20 78 37 20 78 38 20 78 39 20 78 41 20 78 42  6 x7 x8 x9 xA xB
31a5c 20 78 43 20 78 44 20 78 45 20 78 46 20 2a 2f 0a   xC xD xE xF */.
31a5d 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20      0, 0, 1, 1, 
31a5e 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
31a5f 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
31a60 20 30 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a 20 20   0,  /* 4x */.  
31a61 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    0, 1, 1, 1, 1,
31a62 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
31a63 30 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 1, 0, 0, 0, 0
31a64 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20  ,  /* 5x */.    
31a65 30 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 0, 1, 1, 1, 1
31a66 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
31a67 20 30 2c 20 30 2c 20 31 2c 20 30 2c 20 30 2c 20   0, 0, 1, 0, 0, 
31a68 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 20 30 2c   /* 6x */.    0,
31a69 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
31a6a 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30  1, 1, 1, 0, 0, 0
31a6b 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 0, 0, 0,  /
31a6c 2a 20 37 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31  * 7x */.    0, 1
31a6d 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
31a6e 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20   1, 1, 1, 0, 0, 
31a6f 31 2c 20 31 2c 20 31 2c 20 30 2c 20 20 2f 2a 20  1, 1, 1, 0,  /* 
31a70 38 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20  8x */.    0, 1, 
31a71 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
31a72 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 31 2c  , 1, 1, 0, 0, 1,
31a73 20 30 2c 20 31 2c 20 30 2c 20 20 2f 2a 20 39 78   0, 1, 0,  /* 9x
31a74 20 2a 2f 0a 20 20 20 20 31 2c 20 30 2c 20 31 2c   */.    1, 0, 1,
31a75 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
31a76 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31  1, 1, 1, 0, 1, 1
31a77 2c 20 31 2c 20 30 2c 20 20 2f 2a 20 41 78 20 2a  , 1, 0,  /* Ax *
31a78 2f 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30  /.    0, 0, 0, 0
31a79 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
31a7a 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
31a7b 30 2c 20 30 2c 20 20 2f 2a 20 42 78 20 2a 2f 0a  0, 0,  /* Bx */.
31a7c 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20      0, 1, 1, 1, 
31a7d 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
31a7e 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 0, 1, 1, 1, 1,
31a7f 20 31 2c 20 20 2f 2a 20 43 78 20 2a 2f 0a 20 20   1,  /* Cx */.  
31a80 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    0, 1, 1, 1, 1,
31a81 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
31a82 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
31a83 2c 20 20 2f 2a 20 44 78 20 2a 2f 0a 20 20 20 20  ,  /* Dx */.    
31a84 30 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 0, 1, 1, 1, 1
31a85 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
31a86 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
31a87 20 2f 2a 20 45 78 20 2a 2f 0a 20 20 20 20 31 2c   /* Ex */.    1,
31a88 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
31a89 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31  1, 1, 1, 1, 0, 1
31a8a 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 20 2f  , 1, 1, 1, 0,  /
31a8b 2a 20 46 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69  * Fx */.};.#defi
31a8c 6e 65 20 49 64 43 68 61 72 28 43 29 20 20 28 28  ne IdChar(C)  ((
31a8d 28 63 3d 43 29 3e 3d 30 78 34 32 20 26 26 20 73  (c=C)>=0x42 && s
31a8e 71 6c 69 74 65 33 49 73 45 62 63 64 69 63 49 64  qlite3IsEbcdicId
31a8f 43 68 61 72 5b 63 2d 30 78 34 30 5d 29 29 0a 23  Char[c-0x40])).#
31a90 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65  endif.../*.** Re
31a91 74 75 72 6e 20 74 68 65 20 6c 65 6e 67 74 68 20  turn the length 
31a92 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61  of the token tha
31a93 74 20 62 65 67 69 6e 73 20 61 74 20 7a 5b 30 5d  t begins at z[0]
31a94 2e 20 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20  . .** Store the 
31a95 74 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20 2a 74  token type in *t
31a96 6f 6b 65 6e 54 79 70 65 20 62 65 66 6f 72 65 20  okenType before 
31a97 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51  returning..*/.SQ
31a98 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
31a99 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e   sqlite3GetToken
31a9a 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20  (const unsigned 
31a9b 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 74 6f  char *z, int *to
31a9c 6b 65 6e 54 79 70 65 29 7b 0a 20 20 69 6e 74 20  kenType){.  int 
31a9d 69 2c 20 63 3b 0a 20 20 73 77 69 74 63 68 28 20  i, c;.  switch( 
31a9e 2a 7a 20 29 7b 0a 20 20 20 20 63 61 73 65 20 27  *z ){.    case '
31a9f 20 27 3a 20 63 61 73 65 20 27 5c 74 27 3a 20 63   ': case '\t': c
31aa0 61 73 65 20 27 5c 6e 27 3a 20 63 61 73 65 20 27  ase '\n': case '
31aa1 5c 66 27 3a 20 63 61 73 65 20 27 5c 72 27 3a 20  \f': case '\r': 
31aa2 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
31aa3 28 20 7a 5b 30 5d 3d 3d 27 20 27 20 29 3b 0a 20  ( z[0]==' ' );. 
31aa4 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 7a       testcase( z
31aa5 5b 30 5d 3d 3d 27 5c 74 27 20 29 3b 0a 20 20 20  [0]=='\t' );.   
31aa6 20 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30     testcase( z[0
31aa7 5d 3d 3d 27 5c 6e 27 20 29 3b 0a 20 20 20 20 20  ]=='\n' );.     
31aa8 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d   testcase( z[0]=
31aa9 3d 27 5c 66 27 20 29 3b 0a 20 20 20 20 20 20 74  ='\f' );.      t
31aaa 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27  estcase( z[0]=='
31aab 5c 72 27 20 29 3b 0a 20 20 20 20 20 20 66 6f 72  \r' );.      for
31aac 28 69 3d 31 3b 20 73 71 6c 69 74 65 33 49 73 73  (i=1; sqlite3Iss
31aad 70 61 63 65 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29  pace(z[i]); i++)
31aae 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  {}.      *tokenT
31aaf 79 70 65 20 3d 20 54 4b 5f 53 50 41 43 45 3b 0a  ype = TK_SPACE;.
31ab0 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a        return i;.
31ab1 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
31ab2 2d 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20  -': {.      if( 
31ab3 7a 5b 31 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20  z[1]=='-' ){.   
31ab4 20 20 20 20 20 66 6f 72 28 69 3d 32 3b 20 28 63       for(i=2; (c
31ab5 3d 7a 5b 69 5d 29 21 3d 30 20 26 26 20 63 21 3d  =z[i])!=0 && c!=
31ab6 27 5c 6e 27 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20  '\n'; i++){}.   
31ab7 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
31ab8 3d 20 54 4b 5f 53 50 41 43 45 3b 0a 20 20 20 20  = TK_SPACE;.    
31ab9 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20      return i;.  
31aba 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 74 6f 6b      }.      *tok
31abb 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4d 49 4e 55  enType = TK_MINU
31abc 53 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  S;.      return 
31abd 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  1;.    }.    cas
31abe 65 20 27 28 27 3a 20 7b 0a 20 20 20 20 20 20 2a  e '(': {.      *
31abf 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4c  tokenType = TK_L
31ac0 50 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  P;.      return 
31ac1 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  1;.    }.    cas
31ac2 65 20 27 29 27 3a 20 7b 0a 20 20 20 20 20 20 2a  e ')': {.      *
31ac3 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 52  tokenType = TK_R
31ac4 50 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  P;.      return 
31ac5 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  1;.    }.    cas
31ac6 65 20 27 3b 27 3a 20 7b 0a 20 20 20 20 20 20 2a  e ';': {.      *
31ac7 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53  tokenType = TK_S
31ac8 45 4d 49 3b 0a 20 20 20 20 20 20 72 65 74 75 72  EMI;.      retur
31ac9 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 1;.    }.    c
31aca 61 73 65 20 27 2b 27 3a 20 7b 0a 20 20 20 20 20  ase '+': {.     
31acb 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
31acc 5f 50 4c 55 53 3b 0a 20 20 20 20 20 20 72 65 74  _PLUS;.      ret
31acd 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
31ace 20 63 61 73 65 20 27 2a 27 3a 20 7b 0a 20 20 20   case '*': {.   
31acf 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
31ad0 54 4b 5f 53 54 41 52 3b 0a 20 20 20 20 20 20 72  TK_STAR;.      r
31ad1 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
31ad2 20 20 20 63 61 73 65 20 27 2f 27 3a 20 7b 0a 20     case '/': {. 
31ad3 20 20 20 20 20 69 66 28 20 7a 5b 31 5d 21 3d 27       if( z[1]!='
31ad4 2a 27 20 7c 7c 20 7a 5b 32 5d 3d 3d 30 20 29 7b  *' || z[2]==0 ){
31ad5 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
31ad6 79 70 65 20 3d 20 54 4b 5f 53 4c 41 53 48 3b 0a  ype = TK_SLASH;.
31ad7 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31          return 1
31ad8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
31ad9 66 6f 72 28 69 3d 33 2c 20 63 3d 7a 5b 32 5d 3b  for(i=3, c=z[2];
31ada 20 28 63 21 3d 27 2a 27 20 7c 7c 20 7a 5b 69 5d   (c!='*' || z[i]
31adb 21 3d 27 2f 27 29 20 26 26 20 28 63 3d 7a 5b 69  !='/') && (c=z[i
31adc 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 7d 0a 20 20  ])!=0; i++){}.  
31add 20 20 20 20 69 66 28 20 63 20 29 20 69 2b 2b 3b      if( c ) i++;
31ade 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70  .      *tokenTyp
31adf 65 20 3d 20 54 4b 5f 53 50 41 43 45 3b 0a 20 20  e = TK_SPACE;.  
31ae0 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20      return i;.  
31ae1 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 25 27    }.    case '%'
31ae2 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e  : {.      *token
31ae3 54 79 70 65 20 3d 20 54 4b 5f 52 45 4d 3b 0a 20  Type = TK_REM;. 
31ae4 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
31ae5 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 3d     }.    case '=
31ae6 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65  ': {.      *toke
31ae7 6e 54 79 70 65 20 3d 20 54 4b 5f 45 51 3b 0a 20  nType = TK_EQ;. 
31ae8 20 20 20 20 20 72 65 74 75 72 6e 20 31 20 2b 20       return 1 + 
31ae9 28 7a 5b 31 5d 3d 3d 27 3d 27 29 3b 0a 20 20 20  (z[1]=='=');.   
31aea 20 7d 0a 20 20 20 20 63 61 73 65 20 27 3c 27 3a   }.    case '<':
31aeb 20 7b 0a 20 20 20 20 20 20 69 66 28 20 28 63 3d   {.      if( (c=
31aec 7a 5b 31 5d 29 3d 3d 27 3d 27 20 29 7b 0a 20 20  z[1])=='=' ){.  
31aed 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
31aee 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20   = TK_LE;.      
31aef 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20    return 2;.    
31af0 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27    }else if( c=='
31af1 3e 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74  >' ){.        *t
31af2 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4e 45  okenType = TK_NE
31af3 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
31af4 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20   2;.      }else 
31af5 69 66 28 20 63 3d 3d 27 3c 27 20 29 7b 0a 20 20  if( c=='<' ){.  
31af6 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
31af7 20 3d 20 54 4b 5f 4c 53 48 49 46 54 3b 0a 20 20   = TK_LSHIFT;.  
31af8 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a        return 2;.
31af9 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
31afa 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
31afb 3d 20 54 4b 5f 4c 54 3b 0a 20 20 20 20 20 20 20  = TK_LT;.       
31afc 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
31afd 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73   }.    }.    cas
31afe 65 20 27 3e 27 3a 20 7b 0a 20 20 20 20 20 20 69  e '>': {.      i
31aff 66 28 20 28 63 3d 7a 5b 31 5d 29 3d 3d 27 3d 27  f( (c=z[1])=='='
31b00 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b   ){.        *tok
31b01 65 6e 54 79 70 65 20 3d 20 54 4b 5f 47 45 3b 0a  enType = TK_GE;.
31b02 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32          return 2
31b03 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
31b04 28 20 63 3d 3d 27 3e 27 20 29 7b 0a 20 20 20 20  ( c=='>' ){.    
31b05 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
31b06 20 54 4b 5f 52 53 48 49 46 54 3b 0a 20 20 20 20   TK_RSHIFT;.    
31b07 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
31b08 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
31b09 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
31b0a 54 4b 5f 47 54 3b 0a 20 20 20 20 20 20 20 20 72  TK_GT;.        r
31b0b 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d  eturn 1;.      }
31b0c 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
31b0d 27 21 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28  '!': {.      if(
31b0e 20 7a 5b 31 5d 21 3d 27 3d 27 20 29 7b 0a 20 20   z[1]!='=' ){.  
31b0f 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
31b10 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20   = TK_ILLEGAL;. 
31b11 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b         return 2;
31b12 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
31b13 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
31b14 20 3d 20 54 4b 5f 4e 45 3b 0a 20 20 20 20 20 20   = TK_NE;.      
31b15 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20    return 2;.    
31b16 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63 61    }.    }.    ca
31b17 73 65 20 27 7c 27 3a 20 7b 0a 20 20 20 20 20 20  se '|': {.      
31b18 69 66 28 20 7a 5b 31 5d 21 3d 27 7c 27 20 29 7b  if( z[1]!='|' ){
31b19 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
31b1a 79 70 65 20 3d 20 54 4b 5f 42 49 54 4f 52 3b 0a  ype = TK_BITOR;.
31b1b 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31          return 1
31b1c 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
31b1d 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
31b1e 65 20 3d 20 54 4b 5f 43 4f 4e 43 41 54 3b 0a 20  e = TK_CONCAT;. 
31b1f 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b         return 2;
31b20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
31b21 20 20 20 63 61 73 65 20 27 2c 27 3a 20 7b 0a 20     case ',': {. 
31b22 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
31b23 3d 20 54 4b 5f 43 4f 4d 4d 41 3b 0a 20 20 20 20  = TK_COMMA;.    
31b24 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
31b25 7d 0a 20 20 20 20 63 61 73 65 20 27 26 27 3a 20  }.    case '&': 
31b26 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  {.      *tokenTy
31b27 70 65 20 3d 20 54 4b 5f 42 49 54 41 4e 44 3b 0a  pe = TK_BITAND;.
31b28 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
31b29 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
31b2a 7e 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b  ~': {.      *tok
31b2b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 42 49 54 4e  enType = TK_BITN
31b2c 4f 54 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  OT;.      return
31b2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   1;.    }.    ca
31b2e 73 65 20 27 60 27 3a 0a 20 20 20 20 63 61 73 65  se '`':.    case
31b2f 20 27 5c 27 27 3a 0a 20 20 20 20 63 61 73 65 20   '\'':.    case 
31b30 27 22 27 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  '"': {.      int
31b31 20 64 65 6c 69 6d 20 3d 20 7a 5b 30 5d 3b 0a 20   delim = z[0];. 
31b32 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 64       testcase( d
31b33 65 6c 69 6d 3d 3d 27 60 27 20 29 3b 0a 20 20 20  elim=='`' );.   
31b34 20 20 20 74 65 73 74 63 61 73 65 28 20 64 65 6c     testcase( del
31b35 69 6d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20  im=='\'' );.    
31b36 20 20 74 65 73 74 63 61 73 65 28 20 64 65 6c 69    testcase( deli
31b37 6d 3d 3d 27 22 27 20 29 3b 0a 20 20 20 20 20 20  m=='"' );.      
31b38 66 6f 72 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d  for(i=1; (c=z[i]
31b39 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  )!=0; i++){.    
31b3a 20 20 20 20 69 66 28 20 63 3d 3d 64 65 6c 69 6d      if( c==delim
31b3b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
31b3c 28 20 7a 5b 69 2b 31 5d 3d 3d 64 65 6c 69 6d 20  ( z[i+1]==delim 
31b3d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
31b3e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  ++;.          }e
31b3f 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
31b40 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
31b41 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
31b42 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
31b43 63 3d 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20 20  c=='\'' ){.     
31b44 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
31b45 54 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 20  TK_STRING;.     
31b46 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20     return i+1;. 
31b47 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63       }else if( c
31b48 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a  !=0 ){.        *
31b49 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49  tokenType = TK_I
31b4a 44 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  D;.        retur
31b4b 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20 7d 65 6c  n i+1;.      }el
31b4c 73 65 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b  se{.        *tok
31b4d 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45  enType = TK_ILLE
31b4e 47 41 4c 3b 0a 20 20 20 20 20 20 20 20 72 65 74  GAL;.        ret
31b4f 75 72 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20  urn i;.      }. 
31b50 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2e     }.    case '.
31b51 27 3a 20 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  ': {.#ifndef SQL
31b52 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
31b53 47 5f 50 4f 49 4e 54 0a 20 20 20 20 20 20 69 66  G_POINT.      if
31b54 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 69  ( !sqlite3Isdigi
31b55 74 28 7a 5b 31 5d 29 20 29 0a 23 65 6e 64 69 66  t(z[1]) ).#endif
31b56 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20  .      {.       
31b57 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
31b58 5f 44 4f 54 3b 0a 20 20 20 20 20 20 20 20 72 65  _DOT;.        re
31b59 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a  turn 1;.      }.
31b5a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
31b5b 6e 65 78 74 20 63 68 61 72 61 63 74 65 72 20 69  next character i
31b5c 73 20 61 20 64 69 67 69 74 2c 20 74 68 69 73 20  s a digit, this 
31b5d 69 73 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f  is a floating po
31b5e 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20 6e 75 6d  int.      ** num
31b5f 62 65 72 20 74 68 61 74 20 62 65 67 69 6e 73 20  ber that begins 
31b60 77 69 74 68 20 22 2e 22 2e 20 20 46 61 6c 6c 20  with ".".  Fall 
31b61 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20 6e 65  thru into the ne
31b62 78 74 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 7d  xt case */.    }
31b63 0a 20 20 20 20 63 61 73 65 20 27 30 27 3a 20 63  .    case '0': c
31b64 61 73 65 20 27 31 27 3a 20 63 61 73 65 20 27 32  ase '1': case '2
31b65 27 3a 20 63 61 73 65 20 27 33 27 3a 20 63 61 73  ': case '3': cas
31b66 65 20 27 34 27 3a 0a 20 20 20 20 63 61 73 65 20  e '4':.    case 
31b67 27 35 27 3a 20 63 61 73 65 20 27 36 27 3a 20 63  '5': case '6': c
31b68 61 73 65 20 27 37 27 3a 20 63 61 73 65 20 27 38  ase '7': case '8
31b69 27 3a 20 63 61 73 65 20 27 39 27 3a 20 7b 0a 20  ': case '9': {. 
31b6a 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 7a       testcase( z
31b6b 5b 30 5d 3d 3d 27 30 27 20 29 3b 20 20 74 65 73  [0]=='0' );  tes
31b6c 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 31 27  tcase( z[0]=='1'
31b6d 20 29 3b 20 20 74 65 73 74 63 61 73 65 28 20 7a   );  testcase( z
31b6e 5b 30 5d 3d 3d 27 32 27 20 29 3b 0a 20 20 20 20  [0]=='2' );.    
31b6f 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d    testcase( z[0]
31b70 3d 3d 27 33 27 20 29 3b 20 20 74 65 73 74 63 61  =='3' );  testca
31b71 73 65 28 20 7a 5b 30 5d 3d 3d 27 34 27 20 29 3b  se( z[0]=='4' );
31b72 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d    testcase( z[0]
31b73 3d 3d 27 35 27 20 29 3b 0a 20 20 20 20 20 20 74  =='5' );.      t
31b74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27  estcase( z[0]=='
31b75 36 27 20 29 3b 20 20 74 65 73 74 63 61 73 65 28  6' );  testcase(
31b76 20 7a 5b 30 5d 3d 3d 27 37 27 20 29 3b 20 20 74   z[0]=='7' );  t
31b77 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27  estcase( z[0]=='
31b78 38 27 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  8' );.      test
31b79 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 39 27 20  case( z[0]=='9' 
31b7a 29 3b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  );.      *tokenT
31b7b 79 70 65 20 3d 20 54 4b 5f 49 4e 54 45 47 45 52  ype = TK_INTEGER
31b7c 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ;.      for(i=0;
31b7d 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28   sqlite3Isdigit(
31b7e 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 23 69  z[i]); i++){}.#i
31b7f 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
31b80 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
31b81 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d  .      if( z[i]=
31b82 3d 27 2e 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='.' ){.        
31b83 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77 68 69  i++;.        whi
31b84 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 67  le( sqlite3Isdig
31b85 69 74 28 7a 5b 69 5d 29 20 29 7b 20 69 2b 2b 3b  it(z[i]) ){ i++;
31b86 20 7d 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65   }.        *toke
31b87 6e 54 79 70 65 20 3d 20 54 4b 5f 46 4c 4f 41 54  nType = TK_FLOAT
31b88 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
31b89 69 66 28 20 28 7a 5b 69 5d 3d 3d 27 65 27 20 7c  if( (z[i]=='e' |
31b8a 7c 20 7a 5b 69 5d 3d 3d 27 45 27 29 20 26 26 0a  | z[i]=='E') &&.
31b8b 20 20 20 20 20 20 20 20 20 20 20 28 20 73 71 6c             ( sql
31b8c 69 74 65 33 49 73 64 69 67 69 74 28 7a 5b 69 2b  ite3Isdigit(z[i+
31b8d 31 5d 29 20 0a 20 20 20 20 20 20 20 20 20 20 20  1]) .           
31b8e 20 7c 7c 20 28 28 7a 5b 69 2b 31 5d 3d 3d 27 2b   || ((z[i+1]=='+
31b8f 27 20 7c 7c 20 7a 5b 69 2b 31 5d 3d 3d 27 2d 27  ' || z[i+1]=='-'
31b90 29 20 26 26 20 73 71 6c 69 74 65 33 49 73 64 69  ) && sqlite3Isdi
31b91 67 69 74 28 7a 5b 69 2b 32 5d 29 29 0a 20 20 20  git(z[i+2])).   
31b92 20 20 20 20 20 20 20 20 29 0a 20 20 20 20 20 20          ).      
31b93 29 7b 0a 20 20 20 20 20 20 20 20 69 20 2b 3d 20  ){.        i += 
31b94 32 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  2;.        while
31b95 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  ( sqlite3Isdigit
31b96 28 7a 5b 69 5d 29 20 29 7b 20 69 2b 2b 3b 20 7d  (z[i]) ){ i++; }
31b97 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
31b98 79 70 65 20 3d 20 54 4b 5f 46 4c 4f 41 54 3b 0a  ype = TK_FLOAT;.
31b99 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
31b9a 20 20 20 20 20 77 68 69 6c 65 28 20 49 64 43 68       while( IdCh
31b9b 61 72 28 7a 5b 69 5d 29 20 29 7b 0a 20 20 20 20  ar(z[i]) ){.    
31b9c 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
31b9d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20   TK_ILLEGAL;.   
31b9e 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20       i++;.      
31b9f 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  }.      return i
31ba0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
31ba1 20 27 5b 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f   '[': {.      fo
31ba2 72 28 69 3d 31 2c 20 63 3d 7a 5b 30 5d 3b 20 63  r(i=1, c=z[0]; c
31ba3 21 3d 27 5d 27 20 26 26 20 28 63 3d 7a 5b 69 5d  !=']' && (c=z[i]
31ba4 29 21 3d 30 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20  )!=0; i++){}.   
31ba5 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
31ba6 63 3d 3d 27 5d 27 20 3f 20 54 4b 5f 49 44 20 3a  c==']' ? TK_ID :
31ba7 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20   TK_ILLEGAL;.   
31ba8 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20     return i;.   
31ba9 20 7d 0a 20 20 20 20 63 61 73 65 20 27 3f 27 3a   }.    case '?':
31baa 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54   {.      *tokenT
31bab 79 70 65 20 3d 20 54 4b 5f 56 41 52 49 41 42 4c  ype = TK_VARIABL
31bac 45 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31  E;.      for(i=1
31bad 3b 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  ; sqlite3Isdigit
31bae 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20  (z[i]); i++){}. 
31baf 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20       return i;. 
31bb0 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 23     }.    case '#
31bb1 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f 72 28 69  ': {.      for(i
31bb2 3d 31 3b 20 73 71 6c 69 74 65 33 49 73 64 69 67  =1; sqlite3Isdig
31bb3 69 74 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d  it(z[i]); i++){}
31bb4 0a 20 20 20 20 20 20 69 66 28 20 69 3e 31 20 29  .      if( i>1 )
31bb5 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  {.        /* Par
31bb6 61 6d 65 74 65 72 73 20 6f 66 20 74 68 65 20 66  ameters of the f
31bb7 6f 72 6d 20 23 4e 4e 4e 20 28 77 68 65 72 65 20  orm #NNN (where 
31bb8 4e 4e 4e 20 69 73 20 61 20 6e 75 6d 62 65 72 29  NNN is a number)
31bb9 20 61 72 65 20 75 73 65 64 0a 20 20 20 20 20 20   are used.      
31bba 20 20 2a 2a 20 69 6e 74 65 72 6e 61 6c 6c 79 20    ** internally 
31bbb 62 79 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64  by sqlite3Nested
31bbc 50 61 72 73 65 2e 20 20 2a 2f 0a 20 20 20 20 20  Parse.  */.     
31bbd 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
31bbe 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20  TK_REGISTER;.   
31bbf 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20       return i;. 
31bc0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20       }.      /* 
31bc1 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e 74  Fall through int
31bc2 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 20  o the next case 
31bc3 69 66 20 74 68 65 20 27 23 27 20 69 73 20 6e 6f  if the '#' is no
31bc4 74 20 66 6f 6c 6c 6f 77 65 64 20 62 79 0a 20 20  t followed by.  
31bc5 20 20 20 20 2a 2a 20 61 20 64 69 67 69 74 2e 20      ** a digit. 
31bc6 54 72 79 20 74 6f 20 6d 61 74 63 68 20 23 41 41  Try to match #AA
31bc7 41 41 20 77 68 65 72 65 20 41 41 41 41 20 69 73  AA where AAAA is
31bc8 20 61 20 70 61 72 61 6d 65 74 65 72 20 6e 61 6d   a parameter nam
31bc9 65 2e 20 2a 2f 0a 20 20 20 20 7d 0a 23 69 66 6e  e. */.    }.#ifn
31bca 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
31bcb 54 43 4c 5f 56 41 52 49 41 42 4c 45 0a 20 20 20  TCL_VARIABLE.   
31bcc 20 63 61 73 65 20 27 24 27 3a 0a 23 65 6e 64 69   case '$':.#endi
31bcd 66 0a 20 20 20 20 63 61 73 65 20 27 40 27 3a 20  f.    case '@': 
31bce 20 2f 2a 20 46 6f 72 20 63 6f 6d 70 61 74 69 62   /* For compatib
31bcf 69 6c 69 74 79 20 77 69 74 68 20 4d 53 20 53 51  ility with MS SQ
31bd0 4c 20 53 65 72 76 65 72 20 2a 2f 0a 20 20 20 20  L Server */.    
31bd1 63 61 73 65 20 27 3a 27 3a 20 7b 0a 20 20 20 20  case ':': {.    
31bd2 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 20    int n = 0;.   
31bd3 20 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30     testcase( z[0
31bd4 5d 3d 3d 27 24 27 20 29 3b 20 20 74 65 73 74 63  ]=='$' );  testc
31bd5 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 40 27 20 29  ase( z[0]=='@' )
31bd6 3b 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30  ;  testcase( z[0
31bd7 5d 3d 3d 27 3a 27 20 29 3b 0a 20 20 20 20 20 20  ]==':' );.      
31bd8 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
31bd9 56 41 52 49 41 42 4c 45 3b 0a 20 20 20 20 20 20  VARIABLE;.      
31bda 66 6f 72 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d  for(i=1; (c=z[i]
31bdb 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  )!=0; i++){.    
31bdc 20 20 20 20 69 66 28 20 49 64 43 68 61 72 28 63      if( IdChar(c
31bdd 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  ) ){.          n
31bde 2b 2b 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ++;.#ifndef SQLI
31bdf 54 45 5f 4f 4d 49 54 5f 54 43 4c 5f 56 41 52 49  TE_OMIT_TCL_VARI
31be0 41 42 4c 45 0a 20 20 20 20 20 20 20 20 7d 65 6c  ABLE.        }el
31be1 73 65 20 69 66 28 20 63 3d 3d 27 28 27 20 26 26  se if( c=='(' &&
31be2 20 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20   n>0 ){.        
31be3 20 20 64 6f 7b 0a 20 20 20 20 20 20 20 20 20 20    do{.          
31be4 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20    i++;.         
31be5 20 7d 77 68 69 6c 65 28 20 28 63 3d 7a 5b 69 5d   }while( (c=z[i]
31be6 29 21 3d 30 20 26 26 20 21 73 71 6c 69 74 65 33  )!=0 && !sqlite3
31be7 49 73 73 70 61 63 65 28 63 29 20 26 26 20 63 21  Isspace(c) && c!
31be8 3d 27 29 27 20 29 3b 0a 20 20 20 20 20 20 20 20  =')' );.        
31be9 20 20 69 66 28 20 63 3d 3d 27 29 27 20 29 7b 0a    if( c==')' ){.
31bea 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b              i++;
31beb 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
31bec 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 74  {.            *t
31bed 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c  okenType = TK_IL
31bee 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20 20 20  LEGAL;.         
31bef 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 72 65   }.          bre
31bf0 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ak;.        }els
31bf1 65 20 69 66 28 20 63 3d 3d 27 3a 27 20 26 26 20  e if( c==':' && 
31bf2 7a 5b 69 2b 31 5d 3d 3d 27 3a 27 20 29 7b 0a 20  z[i+1]==':' ){. 
31bf3 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 23 65           i++;.#e
31bf4 6e 64 69 66 0a 20 20 20 20 20 20 20 20 7d 65 6c  ndif.        }el
31bf5 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 62 72  se{.          br
31bf6 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
31bf7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
31bf8 20 6e 3d 3d 30 20 29 20 2a 74 6f 6b 65 6e 54 79   n==0 ) *tokenTy
31bf9 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b  pe = TK_ILLEGAL;
31bfa 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b  .      return i;
31bfb 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  .    }.#ifndef S
31bfc 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f  QLITE_OMIT_BLOB_
31bfd 4c 49 54 45 52 41 4c 0a 20 20 20 20 63 61 73 65  LITERAL.    case
31bfe 20 27 78 27 3a 20 63 61 73 65 20 27 58 27 3a 20   'x': case 'X': 
31bff 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
31c00 28 20 7a 5b 30 5d 3d 3d 27 78 27 20 29 3b 20 74  ( z[0]=='x' ); t
31c01 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27  estcase( z[0]=='
31c02 58 27 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  X' );.      if( 
31c03 7a 5b 31 5d 3d 3d 27 5c 27 27 20 29 7b 0a 20 20  z[1]=='\'' ){.  
31c04 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
31c05 20 3d 20 54 4b 5f 42 4c 4f 42 3b 0a 20 20 20 20   = TK_BLOB;.    
31c06 20 20 20 20 66 6f 72 28 69 3d 32 3b 20 28 63 3d      for(i=2; (c=
31c07 7a 5b 69 5d 29 21 3d 30 20 26 26 20 63 21 3d 27  z[i])!=0 && c!='
31c08 5c 27 27 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  \''; i++){.     
31c09 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65       if( !sqlite
31c0a 33 49 73 78 64 69 67 69 74 28 63 29 20 29 7b 0a  3Isxdigit(c) ){.
31c0b 20 20 20 20 20 20 20 20 20 20 20 20 2a 74 6f 6b              *tok
31c0c 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45  enType = TK_ILLE
31c0d 47 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  GAL;.          }
31c0e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
31c0f 20 20 20 69 66 28 20 69 25 32 20 7c 7c 20 21 63     if( i%2 || !c
31c10 20 29 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20   ) *tokenType = 
31c11 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20  TK_ILLEGAL;.    
31c12 20 20 20 20 69 66 28 20 63 20 29 20 69 2b 2b 3b      if( c ) i++;
31c13 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
31c14 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  i;.      }.     
31c15 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 66 61   /* Otherwise fa
31c16 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68  ll through to th
31c17 65 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a 20  e next case */. 
31c18 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
31c19 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
31c1a 20 69 66 28 20 21 49 64 43 68 61 72 28 2a 7a 29   if( !IdChar(*z)
31c1b 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61   ){.        brea
31c1c 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
31c1d 20 66 6f 72 28 69 3d 31 3b 20 49 64 43 68 61 72   for(i=1; IdChar
31c1e 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20  (z[i]); i++){}. 
31c1f 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
31c20 3d 20 6b 65 79 77 6f 72 64 43 6f 64 65 28 28 63  = keywordCode((c
31c21 68 61 72 2a 29 7a 2c 20 69 29 3b 0a 20 20 20 20  har*)z, i);.    
31c22 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20    return i;.    
31c23 7d 0a 20 20 7d 0a 20 20 2a 74 6f 6b 65 6e 54 79  }.  }.  *tokenTy
31c24 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b  pe = TK_ILLEGAL;
31c25 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
31c26 2f 2a 0a 2a 2a 20 52 75 6e 20 74 68 65 20 70 61  /*.** Run the pa
31c27 72 73 65 72 20 6f 6e 20 74 68 65 20 67 69 76 65  rser on the give
31c28 6e 20 53 51 4c 20 73 74 72 69 6e 67 2e 20 20 54  n SQL string.  T
31c29 68 65 20 70 61 72 73 65 72 20 73 74 72 75 63 74  he parser struct
31c2a 75 72 65 20 69 73 0a 2a 2a 20 70 61 73 73 65 64  ure is.** passed
31c2b 20 69 6e 2e 20 20 41 6e 20 53 51 4c 49 54 45 5f   in.  An SQLITE_
31c2c 20 73 74 61 74 75 73 20 63 6f 64 65 20 69 73 20   status code is 
31c2d 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 61 6e  returned.  If an
31c2e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 2a 2a   error occurs.**
31c2f 20 74 68 65 6e 20 61 6e 20 61 6e 64 20 61 74 74   then an and att
31c30 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20  empt is made to 
31c31 77 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d  write an error m
31c32 65 73 73 61 67 65 20 69 6e 74 6f 20 0a 2a 2a 20  essage into .** 
31c33 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20  memory obtained 
31c34 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  from sqlite3_mal
31c35 6c 6f 63 28 29 20 61 6e 64 20 74 6f 20 6d 61 6b  loc() and to mak
31c36 65 20 2a 70 7a 45 72 72 4d 73 67 20 70 6f 69 6e  e *pzErrMsg poin
31c37 74 20 74 6f 20 74 68 61 74 0a 2a 2a 20 65 72 72  t to that.** err
31c38 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f 0a 53  or message..*/.S
31c39 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
31c3a 74 20 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73  t sqlite3RunPars
31c3b 65 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  er(Parse *pParse
31c3c 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53  , const char *zS
31c3d 71 6c 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72 72  ql, char **pzErr
31c3e 4d 73 67 29 7b 0a 20 20 69 6e 74 20 6e 45 72 72  Msg){.  int nErr
31c3f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
31c40 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
31c41 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 6f  r of errors enco
31c42 75 6e 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e 74  untered */.  int
31c43 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
31c44 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
31c45 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
31c46 20 20 76 6f 69 64 20 2a 70 45 6e 67 69 6e 65 3b    void *pEngine;
31c47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31c48 20 20 2f 2a 20 54 68 65 20 4c 45 4d 4f 4e 2d 67    /* The LEMON-g
31c49 65 6e 65 72 61 74 65 64 20 4c 41 4c 52 28 31 29  enerated LALR(1)
31c4a 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 69 6e 74   parser */.  int
31c4b 20 74 6f 6b 65 6e 54 79 70 65 3b 20 20 20 20 20   tokenType;     
31c4c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
31c4d 74 79 70 65 20 6f 66 20 74 68 65 20 6e 65 78 74  type of the next
31c4e 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20   token */.  int 
31c4f 6c 61 73 74 54 6f 6b 65 6e 50 61 72 73 65 64 20  lastTokenParsed 
31c50 3d 20 2d 31 3b 20 20 20 20 20 20 20 2f 2a 20 74  = -1;       /* t
31c51 79 70 65 20 6f 66 20 74 68 65 20 70 72 65 76 69  ype of the previ
31c52 6f 75 73 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 75  ous token */.  u
31c53 38 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64  8 enableLookasid
31c54 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e;             /
31c55 2a 20 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66  * Saved value of
31c56 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62   db->lookaside.b
31c57 45 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 73 71 6c  Enabled */.  sql
31c58 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
31c59 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 2f 2a 20  e->db;       /* 
31c5a 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
31c5b 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74  nection */.  int
31c5c 20 6d 78 53 71 6c 4c 65 6e 3b 20 20 20 20 20 20   mxSqlLen;      
31c5d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
31c5e 4d 61 78 20 6c 65 6e 67 74 68 20 6f 66 20 61 6e  Max length of an
31c5f 20 53 51 4c 20 73 74 72 69 6e 67 20 2a 2f 0a 0a   SQL string */..
31c60 0a 20 20 6d 78 53 71 6c 4c 65 6e 20 3d 20 64 62  .  mxSqlLen = db
31c61 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
31c62 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54 48  LIMIT_SQL_LENGTH
31c63 5d 3b 0a 20 20 69 66 28 20 64 62 2d 3e 61 63 74  ];.  if( db->act
31c64 69 76 65 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b  iveVdbeCnt==0 ){
31c65 0a 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e  .    db->u1.isIn
31c66 74 65 72 72 75 70 74 65 64 20 3d 20 30 3b 0a 20  terrupted = 0;. 
31c67 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e 72 63 20   }.  pParse->rc 
31c68 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70  = SQLITE_OK;.  p
31c69 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 7a  Parse->zTail = z
31c6a 53 71 6c 3b 0a 20 20 69 20 3d 20 30 3b 0a 20 20  Sql;.  i = 0;.  
31c6b 61 73 73 65 72 74 28 20 70 7a 45 72 72 4d 73 67  assert( pzErrMsg
31c6c 21 3d 30 20 29 3b 0a 20 20 70 45 6e 67 69 6e 65  !=0 );.  pEngine
31c6d 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73 65 72   = sqlite3Parser
31c6e 41 6c 6c 6f 63 28 28 76 6f 69 64 2a 28 2a 29 28  Alloc((void*(*)(
31c6f 73 69 7a 65 5f 74 29 29 73 71 6c 69 74 65 33 4d  size_t))sqlite3M
31c70 61 6c 6c 6f 63 29 3b 0a 20 20 69 66 28 20 70 45  alloc);.  if( pE
31c71 6e 67 69 6e 65 3d 3d 30 20 29 7b 0a 20 20 20 20  ngine==0 ){.    
31c72 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
31c73 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 1;.    return
31c74 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
31c75 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61   }.  assert( pPa
31c76 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d  rse->pNewTable==
31c77 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
31c78 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67  Parse->pNewTrigg
31c79 65 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  er==0 );.  asser
31c7a 74 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 3d  t( pParse->nVar=
31c7b 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
31c7c 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72  pParse->nVarExpr
31c7d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
31c7e 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70   pParse->nVarExp
31c7f 72 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20 20 61  rAlloc==0 );.  a
31c80 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 61  ssert( pParse->a
31c81 70 56 61 72 45 78 70 72 3d 3d 30 20 29 3b 0a 20  pVarExpr==0 );. 
31c82 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64 65   enableLookaside
31c83 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65   = db->lookaside
31c84 2e 62 45 6e 61 62 6c 65 64 3b 0a 20 20 69 66 28  .bEnabled;.  if(
31c85 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70   db->lookaside.p
31c86 53 74 61 72 74 20 29 20 64 62 2d 3e 6c 6f 6f 6b  Start ) db->look
31c87 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d  aside.bEnabled =
31c88 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 21 64 62   1;.  while( !db
31c89 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26  ->mallocFailed &
31c8a 26 20 7a 53 71 6c 5b 69 5d 21 3d 30 20 29 7b 0a  & zSql[i]!=0 ){.
31c8b 20 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30      assert( i>=0
31c8c 20 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   );.    pParse->
31c8d 73 4c 61 73 74 54 6f 6b 65 6e 2e 7a 20 3d 20 26  sLastToken.z = &
31c8e 7a 53 71 6c 5b 69 5d 3b 0a 20 20 20 20 70 50 61  zSql[i];.    pPa
31c8f 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e  rse->sLastToken.
31c90 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 6f  n = sqlite3GetTo
31c91 6b 65 6e 28 28 75 6e 73 69 67 6e 65 64 20 63 68  ken((unsigned ch
31c92 61 72 2a 29 26 7a 53 71 6c 5b 69 5d 2c 26 74 6f  ar*)&zSql[i],&to
31c93 6b 65 6e 54 79 70 65 29 3b 0a 20 20 20 20 69 20  kenType);.    i 
31c94 2b 3d 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74  += pParse->sLast
31c95 54 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 69 66 28  Token.n;.    if(
31c96 20 69 3e 6d 78 53 71 6c 4c 65 6e 20 29 7b 0a 20   i>mxSqlLen ){. 
31c97 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20       pParse->rc 
31c98 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b  = SQLITE_TOOBIG;
31c99 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
31c9a 20 20 7d 0a 20 20 20 20 73 77 69 74 63 68 28 20    }.    switch( 
31c9b 74 6f 6b 65 6e 54 79 70 65 20 29 7b 0a 20 20 20  tokenType ){.   
31c9c 20 20 20 63 61 73 65 20 54 4b 5f 53 50 41 43 45     case TK_SPACE
31c9d 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  : {.        if( 
31c9e 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75  db->u1.isInterru
31c9f 70 74 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20  pted ){.        
31ca0 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
31ca1 67 28 70 50 61 72 73 65 2c 20 22 69 6e 74 65 72  g(pParse, "inter
31ca2 72 75 70 74 22 29 3b 0a 20 20 20 20 20 20 20 20  rupt");.        
31ca3 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
31ca4 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b  QLITE_INTERRUPT;
31ca5 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
31ca6 61 62 6f 72 74 5f 70 61 72 73 65 3b 0a 20 20 20  abort_parse;.   
31ca7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62       }.        b
31ca8 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
31ca9 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4c 4c 45      case TK_ILLE
31caa 47 41 4c 3a 20 7b 0a 20 20 20 20 20 20 20 20 73  GAL: {.        s
31cab 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
31cac 20 2a 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20   *pzErrMsg);.   
31cad 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d       *pzErrMsg =
31cae 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
31caf 64 62 2c 20 22 75 6e 72 65 63 6f 67 6e 69 7a 65  db, "unrecognize
31cb0 64 20 74 6f 6b 65 6e 3a 20 5c 22 25 54 5c 22 22  d token: \"%T\""
31cb1 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
31cb2 20 20 20 20 20 20 20 20 20 20 26 70 50 61 72 73            &pPars
31cb3 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 29 3b 0a  e->sLastToken);.
31cb4 20 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a          nErr++;.
31cb5 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f          goto abo
31cb6 72 74 5f 70 61 72 73 65 3b 0a 20 20 20 20 20 20  rt_parse;.      
31cb7 7d 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f  }.      case TK_
31cb8 53 45 4d 49 3a 20 7b 0a 20 20 20 20 20 20 20 20  SEMI: {.        
31cb9 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20  pParse->zTail = 
31cba 26 7a 53 71 6c 5b 69 5d 3b 0a 20 20 20 20 20 20  &zSql[i];.      
31cbb 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 69    /* Fall thru i
31cbc 6e 74 6f 20 74 68 65 20 64 65 66 61 75 6c 74 20  nto the default 
31cbd 63 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 7d 0a  case */.      }.
31cbe 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b        default: {
31cbf 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
31cc0 50 61 72 73 65 72 28 70 45 6e 67 69 6e 65 2c 20  Parser(pEngine, 
31cc1 74 6f 6b 65 6e 54 79 70 65 2c 20 70 50 61 72 73  tokenType, pPars
31cc2 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c 20 70  e->sLastToken, p
31cc3 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20  Parse);.        
31cc4 6c 61 73 74 54 6f 6b 65 6e 50 61 72 73 65 64 20  lastTokenParsed 
31cc5 3d 20 74 6f 6b 65 6e 54 79 70 65 3b 0a 20 20 20  = tokenType;.   
31cc6 20 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d       if( pParse-
31cc7 3e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  >rc!=SQLITE_OK )
31cc8 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  {.          goto
31cc9 20 61 62 6f 72 74 5f 70 61 72 73 65 3b 0a 20 20   abort_parse;.  
31cca 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
31ccb 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
31ccc 20 20 20 7d 0a 20 20 7d 0a 61 62 6f 72 74 5f 70     }.  }.abort_p
31ccd 61 72 73 65 3a 0a 20 20 69 66 28 20 7a 53 71 6c  arse:.  if( zSql
31cce 5b 69 5d 3d 3d 30 20 26 26 20 6e 45 72 72 3d 3d  [i]==0 && nErr==
31ccf 30 20 26 26 20 70 50 61 72 73 65 2d 3e 72 63 3d  0 && pParse->rc=
31cd0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
31cd1 20 20 69 66 28 20 6c 61 73 74 54 6f 6b 65 6e 50    if( lastTokenP
31cd2 61 72 73 65 64 21 3d 54 4b 5f 53 45 4d 49 20 29  arsed!=TK_SEMI )
31cd3 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50  {.      sqlite3P
31cd4 61 72 73 65 72 28 70 45 6e 67 69 6e 65 2c 20 54  arser(pEngine, T
31cd5 4b 5f 53 45 4d 49 2c 20 70 50 61 72 73 65 2d 3e  K_SEMI, pParse->
31cd6 73 4c 61 73 74 54 6f 6b 65 6e 2c 20 70 50 61 72  sLastToken, pPar
31cd7 73 65 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73  se);.      pPars
31cd8 65 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c  e->zTail = &zSql
31cd9 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  [i];.    }.    s
31cda 71 6c 69 74 65 33 50 61 72 73 65 72 28 70 45 6e  qlite3Parser(pEn
31cdb 67 69 6e 65 2c 20 30 2c 20 70 50 61 72 73 65 2d  gine, 0, pParse-
31cdc 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c 20 70 50 61  >sLastToken, pPa
31cdd 72 73 65 29 3b 0a 20 20 7d 0a 23 69 66 64 65 66  rse);.  }.#ifdef
31cde 20 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b   YYTRACKMAXSTACK
31cdf 44 45 50 54 48 0a 20 20 73 71 6c 69 74 65 33 53  DEPTH.  sqlite3S
31ce0 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f  tatusSet(SQLITE_
31ce1 53 54 41 54 55 53 5f 50 41 52 53 45 52 5f 53 54  STATUS_PARSER_ST
31ce2 41 43 4b 2c 0a 20 20 20 20 20 20 73 71 6c 69 74  ACK,.      sqlit
31ce3 65 33 50 61 72 73 65 72 53 74 61 63 6b 50 65 61  e3ParserStackPea
31ce4 6b 28 70 45 6e 67 69 6e 65 29 0a 20 20 29 3b 0a  k(pEngine).  );.
31ce5 23 65 6e 64 69 66 20 2f 2a 20 59 59 44 45 42 55  #endif /* YYDEBU
31ce6 47 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61  G */.  sqlite3Pa
31ce7 72 73 65 72 46 72 65 65 28 70 45 6e 67 69 6e 65  rserFree(pEngine
31ce8 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b  , sqlite3_free);
31ce9 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65  .  db->lookaside
31cea 2e 62 45 6e 61 62 6c 65 64 20 3d 20 65 6e 61 62  .bEnabled = enab
31ceb 6c 65 4c 6f 6f 6b 61 73 69 64 65 3b 0a 20 20 69  leLookaside;.  i
31cec 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
31ced 6c 65 64 20 29 7b 0a 20 20 20 20 70 50 61 72 73  led ){.    pPars
31cee 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e  e->rc = SQLITE_N
31cef 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20  OMEM;.  }.  if( 
31cf0 70 50 61 72 73 65 2d 3e 72 63 21 3d 53 51 4c 49  pParse->rc!=SQLI
31cf1 54 45 5f 4f 4b 20 26 26 20 70 50 61 72 73 65 2d  TE_OK && pParse-
31cf2 3e 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45  >rc!=SQLITE_DONE
31cf3 20 26 26 20 70 50 61 72 73 65 2d 3e 7a 45 72 72   && pParse->zErr
31cf4 4d 73 67 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  Msg==0 ){.    sq
31cf5 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
31cf6 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2c  pParse->zErrMsg,
31cf7 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74   db, "%s", sqlit
31cf8 65 33 45 72 72 53 74 72 28 70 50 61 72 73 65 2d  e3ErrStr(pParse-
31cf9 3e 72 63 29 29 3b 0a 20 20 7d 0a 20 20 61 73 73  >rc));.  }.  ass
31cfa 65 72 74 28 20 70 7a 45 72 72 4d 73 67 21 3d 30  ert( pzErrMsg!=0
31cfb 20 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65   );.  if( pParse
31cfc 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20  ->zErrMsg ){.   
31cfd 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 70 50 61   *pzErrMsg = pPa
31cfe 72 73 65 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20  rse->zErrMsg;.  
31cff 20 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73    pParse->zErrMs
31d00 67 20 3d 20 30 3b 0a 20 20 20 20 6e 45 72 72 2b  g = 0;.    nErr+
31d01 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  +;.  }.  if( pPa
31d02 72 73 65 2d 3e 70 56 64 62 65 20 26 26 20 70 50  rse->pVdbe && pP
31d03 61 72 73 65 2d 3e 6e 45 72 72 3e 30 20 26 26 20  arse->nErr>0 && 
31d04 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d  pParse->nested==
31d05 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
31d06 56 64 62 65 44 65 6c 65 74 65 28 70 50 61 72 73  VdbeDelete(pPars
31d07 65 2d 3e 70 56 64 62 65 29 3b 0a 20 20 20 20 70  e->pVdbe);.    p
31d08 50 61 72 73 65 2d 3e 70 56 64 62 65 20 3d 20 30  Parse->pVdbe = 0
31d09 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51  ;.  }.#ifndef SQ
31d0a 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
31d0b 5f 43 41 43 48 45 0a 20 20 69 66 28 20 70 50 61  _CACHE.  if( pPa
31d0c 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29  rse->nested==0 )
31d0d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  {.    sqlite3DbF
31d0e 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e  ree(db, pParse->
31d0f 61 54 61 62 6c 65 4c 6f 63 6b 29 3b 0a 20 20 20  aTableLock);.   
31d10 20 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c   pParse->aTableL
31d11 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 70 50 61  ock = 0;.    pPa
31d12 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 20  rse->nTableLock 
31d13 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  = 0;.  }.#endif.
31d14 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
31d15 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
31d16 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
31d17 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 70 56  (db, pParse->apV
31d18 74 61 62 4c 6f 63 6b 29 3b 0a 23 65 6e 64 69 66  tabLock);.#endif
31d19 0a 0a 20 20 69 66 28 20 21 49 4e 5f 44 45 43 4c  ..  if( !IN_DECL
31d1a 41 52 45 5f 56 54 41 42 20 29 7b 0a 20 20 20 20  ARE_VTAB ){.    
31d1b 2f 2a 20 49 66 20 74 68 65 20 70 50 61 72 73 65  /* If the pParse
31d1c 2d 3e 64 65 63 6c 61 72 65 56 74 61 62 20 66 6c  ->declareVtab fl
31d1d 61 67 20 69 73 20 73 65 74 2c 20 64 6f 20 6e 6f  ag is set, do no
31d1e 74 20 64 65 6c 65 74 65 20 61 6e 79 20 74 61 62  t delete any tab
31d1f 6c 65 20 0a 20 20 20 20 2a 2a 20 73 74 72 75 63  le .    ** struc
31d20 74 75 72 65 20 62 75 69 6c 74 20 75 70 20 69 6e  ture built up in
31d21 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
31d22 6c 65 2e 20 54 68 65 20 63 61 6c 6c 69 6e 67 20  le. The calling 
31d23 63 6f 64 65 20 28 73 65 65 20 76 74 61 62 2e 63  code (see vtab.c
31d24 29 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 74 61  ).    ** will ta
31d25 6b 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74  ke responsibilit
31d26 79 20 66 6f 72 20 66 72 65 65 69 6e 67 20 74 68  y for freeing th
31d27 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
31d28 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  e..    */.    sq
31d29 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65  lite3DeleteTable
31d2a 28 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62  (pParse->pNewTab
31d2b 6c 65 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69  le);.  }..  sqli
31d2c 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72  te3DeleteTrigger
31d2d 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 70 4e 65  (db, pParse->pNe
31d2e 77 54 72 69 67 67 65 72 29 3b 0a 20 20 73 71 6c  wTrigger);.  sql
31d2f 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
31d30 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72  Parse->apVarExpr
31d31 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
31d32 65 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61  ee(db, pParse->a
31d33 41 6c 69 61 73 29 3b 0a 20 20 77 68 69 6c 65 28  Alias);.  while(
31d34 20 70 50 61 72 73 65 2d 3e 70 41 69 6e 63 20 29   pParse->pAinc )
31d35 7b 0a 20 20 20 20 41 75 74 6f 69 6e 63 49 6e 66  {.    AutoincInf
31d36 6f 20 2a 70 20 3d 20 70 50 61 72 73 65 2d 3e 70  o *p = pParse->p
31d37 41 69 6e 63 3b 0a 20 20 20 20 70 50 61 72 73 65  Ainc;.    pParse
31d38 2d 3e 70 41 69 6e 63 20 3d 20 70 2d 3e 70 4e 65  ->pAinc = p->pNe
31d39 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  xt;.    sqlite3D
31d3a 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20  bFree(db, p);.  
31d3b 7d 0a 20 20 77 68 69 6c 65 28 20 70 50 61 72 73  }.  while( pPars
31d3c 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 20 29 7b  e->pZombieTab ){
31d3d 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 20 3d 20  .    Table *p = 
31d3e 70 50 61 72 73 65 2d 3e 70 5a 6f 6d 62 69 65 54  pParse->pZombieT
31d3f 61 62 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  ab;.    pParse->
31d40 70 5a 6f 6d 62 69 65 54 61 62 20 3d 20 70 2d 3e  pZombieTab = p->
31d41 70 4e 65 78 74 5a 6f 6d 62 69 65 3b 0a 20 20 20  pNextZombie;.   
31d42 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61   sqlite3DeleteTa
31d43 62 6c 65 28 70 29 3b 0a 20 20 7d 0a 20 20 69 66  ble(p);.  }.  if
31d44 28 20 6e 45 72 72 3e 30 20 26 26 20 70 50 61 72  ( nErr>0 && pPar
31d45 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  se->rc==SQLITE_O
31d46 4b 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d  K ){.    pParse-
31d47 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52  >rc = SQLITE_ERR
31d48 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  OR;.  }.  return
31d49 20 6e 45 72 72 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a   nErr;.}../*****
31d4a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
31d4b 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a   tokenize.c ****
31d4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31d4d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31d4e 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
31d4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
31d50 66 69 6c 65 20 63 6f 6d 70 6c 65 74 65 2e 63 20  file complete.c 
31d51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31d52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31d53 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
31d54 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
31d55 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
31d56 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
31d57 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
31d58 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
31d59 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
31d5a 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
31d5b 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
31d5c 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
31d5d 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
31d5e 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
31d5f 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
31d60 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
31d61 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
31d62 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
31d63 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
31d64 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
31d65 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
31d66 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
31d67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31d68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31d69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31d6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31d6b 0a 2a 2a 20 41 6e 20 74 6f 6b 65 6e 69 7a 65 72  .** An tokenizer
31d6c 20 66 6f 72 20 53 51 4c 0a 2a 2a 0a 2a 2a 20 54   for SQL.**.** T
31d6d 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
31d6e 73 20 43 20 63 6f 64 65 20 74 68 61 74 20 69 6d  s C code that im
31d6f 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 71 6c  plements the sql
31d70 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29 20  ite3_complete() 
31d71 41 50 49 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 64  API..** This cod
31d72 65 20 75 73 65 64 20 74 6f 20 62 65 20 70 61 72  e used to be par
31d73 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 69 7a  t of the tokeniz
31d74 65 72 2e 63 20 73 6f 75 72 63 65 20 66 69 6c 65  er.c source file
31d75 2e 20 20 42 75 74 20 62 79 0a 2a 2a 20 73 65 70  .  But by.** sep
31d76 61 72 61 74 69 6e 67 20 69 74 20 6f 75 74 2c 20  arating it out, 
31d77 74 68 65 20 63 6f 64 65 20 77 69 6c 6c 20 62 65  the code will be
31d78 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 6f   automatically o
31d79 6d 69 74 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 73  mitted from.** s
31d7a 74 61 74 69 63 20 6c 69 6e 6b 73 20 74 68 61 74  tatic links that
31d7b 20 64 6f 20 6e 6f 74 20 75 73 65 20 69 74 2e 0a   do not use it..
31d7c 2a 2a 0a 2a 2a 20 24 49 64 3a 20 63 6f 6d 70 6c  **.** $Id: compl
31d7d 65 74 65 2e 63 2c 76 20 31 2e 38 20 32 30 30 39  ete.c,v 1.8 2009
31d7e 2f 30 34 2f 32 38 20 30 34 3a 34 36 3a 34 32 20  /04/28 04:46:42 
31d7f 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66  drh Exp $.*/.#if
31d80 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
31d81 5f 43 4f 4d 50 4c 45 54 45 0a 0a 2f 2a 0a 2a 2a  _COMPLETE../*.**
31d82 20 54 68 69 73 20 69 73 20 64 65 66 69 6e 65 64   This is defined
31d83 20 69 6e 20 74 6f 6b 65 6e 69 7a 65 2e 63 2e 20   in tokenize.c. 
31d84 20 57 65 20 6a 75 73 74 20 68 61 76 65 20 74 6f   We just have to
31d85 20 69 6d 70 6f 72 74 20 74 68 65 20 64 65 66 69   import the defi
31d86 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64  nition..*/.#ifnd
31d87 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41  ef SQLITE_AMALGA
31d88 4d 41 54 49 4f 4e 0a 23 69 66 64 65 66 20 53 51  MATION.#ifdef SQ
31d89 4c 49 54 45 5f 41 53 43 49 49 0a 53 51 4c 49 54  LITE_ASCII.SQLIT
31d8a 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
31d8b 63 68 61 72 20 73 71 6c 69 74 65 33 49 73 41 73  char sqlite3IsAs
31d8c 63 69 69 49 64 43 68 61 72 5b 5d 3b 0a 23 64 65  ciiIdChar[];.#de
31d8d 66 69 6e 65 20 49 64 43 68 61 72 28 43 29 20 20  fine IdChar(C)  
31d8e 28 28 28 63 3d 43 29 26 30 78 38 30 29 21 3d 30  (((c=C)&0x80)!=0
31d8f 20 7c 7c 20 28 63 3e 30 78 31 66 20 26 26 20 73   || (c>0x1f && s
31d90 71 6c 69 74 65 33 49 73 41 73 63 69 69 49 64 43  qlite3IsAsciiIdC
31d91 68 61 72 5b 63 2d 30 78 32 30 5d 29 29 0a 23 65  har[c-0x20])).#e
31d92 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
31d93 54 45 5f 45 42 43 44 49 43 0a 53 51 4c 49 54 45  TE_EBCDIC.SQLITE
31d94 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
31d95 68 61 72 20 73 71 6c 69 74 65 33 49 73 45 62 63  har sqlite3IsEbc
31d96 64 69 63 49 64 43 68 61 72 5b 5d 3b 0a 23 64 65  dicIdChar[];.#de
31d97 66 69 6e 65 20 49 64 43 68 61 72 28 43 29 20 20  fine IdChar(C)  
31d98 28 28 28 63 3d 43 29 3e 3d 30 78 34 32 20 26 26  (((c=C)>=0x42 &&
31d99 20 73 71 6c 69 74 65 33 49 73 45 62 63 64 69 63   sqlite3IsEbcdic
31d9a 49 64 43 68 61 72 5b 63 2d 30 78 34 30 5d 29 29  IdChar[c-0x40]))
31d9b 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f  .#endif.#endif /
31d9c 2a 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d  * SQLITE_AMALGAM
31d9d 41 54 49 4f 4e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a  ATION */.../*.**
31d9e 20 54 6f 6b 65 6e 20 74 79 70 65 73 20 75 73 65   Token types use
31d9f 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33  d by the sqlite3
31da0 5f 63 6f 6d 70 6c 65 74 65 28 29 20 72 6f 75 74  _complete() rout
31da1 69 6e 65 2e 20 20 53 65 65 20 74 68 65 20 68 65  ine.  See the he
31da2 61 64 65 72 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73  ader.** comments
31da3 20 6f 6e 20 74 68 61 74 20 70 72 6f 63 65 64 75   on that procedu
31da4 72 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  re for additiona
31da5 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  l information..*
31da6 2f 0a 23 64 65 66 69 6e 65 20 74 6b 53 45 4d 49  /.#define tkSEMI
31da7 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 74 6b      0.#define tk
31da8 57 53 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e  WS      1.#defin
31da9 65 20 74 6b 4f 54 48 45 52 20 20 20 32 0a 23 64  e tkOTHER   2.#d
31daa 65 66 69 6e 65 20 74 6b 45 58 50 4c 41 49 4e 20  efine tkEXPLAIN 
31dab 33 0a 23 64 65 66 69 6e 65 20 74 6b 43 52 45 41  3.#define tkCREA
31dac 54 45 20 20 34 0a 23 64 65 66 69 6e 65 20 74 6b  TE  4.#define tk
31dad 54 45 4d 50 20 20 20 20 35 0a 23 64 65 66 69 6e  TEMP    5.#defin
31dae 65 20 74 6b 54 52 49 47 47 45 52 20 36 0a 23 64  e tkTRIGGER 6.#d
31daf 65 66 69 6e 65 20 74 6b 45 4e 44 20 20 20 20 20  efine tkEND     
31db0 37 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  7../*.** Return 
31db1 54 52 55 45 20 69 66 20 74 68 65 20 67 69 76 65  TRUE if the give
31db2 6e 20 53 51 4c 20 73 74 72 69 6e 67 20 65 6e 64  n SQL string end
31db3 73 20 69 6e 20 61 20 73 65 6d 69 63 6f 6c 6f 6e  s in a semicolon
31db4 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 61 6c 20  ..**.** Special 
31db5 68 61 6e 64 6c 69 6e 67 20 69 73 20 72 65 71 75  handling is requ
31db6 69 72 65 20 66 6f 72 20 43 52 45 41 54 45 20 54  ire for CREATE T
31db7 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74  RIGGER statement
31db8 73 2e 0a 2a 2a 20 57 68 65 6e 65 76 65 72 20 74  s..** Whenever t
31db9 68 65 20 43 52 45 41 54 45 20 54 52 49 47 47 45  he CREATE TRIGGE
31dba 52 20 6b 65 79 77 6f 72 64 73 20 61 72 65 20 73  R keywords are s
31dbb 65 65 6e 2c 20 74 68 65 20 73 74 61 74 65 6d 65  een, the stateme
31dbc 6e 74 0a 2a 2a 20 6d 75 73 74 20 65 6e 64 20 77  nt.** must end w
31dbd 69 74 68 20 22 3b 45 4e 44 3b 22 2e 0a 2a 2a 0a  ith ";END;"..**.
31dbe 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e  ** This implemen
31dbf 74 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 74  tation uses a st
31dc0 61 74 65 20 6d 61 63 68 69 6e 65 20 77 69 74 68  ate machine with
31dc1 20 37 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a   7 states:.**.**
31dc2 20 20 20 28 30 29 20 53 54 41 52 54 20 20 20 20     (0) START    
31dc3 20 41 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   At the beginnin
31dc4 67 20 6f 72 20 65 6e 64 20 6f 66 20 61 6e 20 53  g or end of an S
31dc5 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54  QL statement.  T
31dc6 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 20  his routine.**  
31dc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
31dc8 65 74 75 72 6e 73 20 31 20 69 66 20 69 74 20 65  eturns 1 if it e
31dc9 6e 64 73 20 69 6e 20 74 68 65 20 53 54 41 52 54  nds in the START
31dca 20 73 74 61 74 65 20 61 6e 64 20 30 20 69 66 20   state and 0 if 
31dcb 69 74 20 65 6e 64 73 0a 2a 2a 20 20 20 20 20 20  it ends.**      
31dcc 20 20 20 20 20 20 20 20 20 20 20 69 6e 20 61 6e             in an
31dcd 79 20 6f 74 68 65 72 20 73 74 61 74 65 2e 0a 2a  y other state..*
31dce 2a 0a 2a 2a 20 20 20 28 31 29 20 4e 4f 52 4d 41  *.**   (1) NORMA
31dcf 4c 20 20 20 20 57 65 20 61 72 65 20 69 6e 20 74  L    We are in t
31dd0 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 74 61  he middle of sta
31dd1 74 65 6d 65 6e 74 20 77 68 69 63 68 20 65 6e 64  tement which end
31dd2 73 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65 0a  s with a single.
31dd3 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
31dd4 20 20 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a     semicolon..**
31dd5 0a 2a 2a 20 20 20 28 32 29 20 45 58 50 4c 41 49  .**   (2) EXPLAI
31dd6 4e 20 20 20 54 68 65 20 6b 65 79 77 6f 72 64 20  N   The keyword 
31dd7 45 58 50 4c 41 49 4e 20 68 61 73 20 62 65 65 6e  EXPLAIN has been
31dd8 20 73 65 65 6e 20 61 74 20 74 68 65 20 62 65 67   seen at the beg
31dd9 69 6e 6e 69 6e 67 20 6f 66 20 0a 2a 2a 20 20 20  inning of .**   
31dda 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 20                a 
31ddb 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a  statement..**.**
31ddc 20 20 20 28 33 29 20 43 52 45 41 54 45 20 20 20     (3) CREATE   
31ddd 20 54 68 65 20 6b 65 79 77 6f 72 64 20 43 52 45   The keyword CRE
31dde 41 54 45 20 68 61 73 20 62 65 65 6e 20 73 65 65  ATE has been see
31ddf 6e 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69  n at the beginni
31de0 6e 67 20 6f 66 20 61 0a 2a 2a 20 20 20 20 20 20  ng of a.**      
31de1 20 20 20 20 20 20 20 20 20 20 20 73 74 61 74 65             state
31de2 6d 65 6e 74 2c 20 70 6f 73 73 69 62 6c 79 20 70  ment, possibly p
31de3 72 65 63 65 65 64 65 64 20 62 79 20 45 58 50 4c  receeded by EXPL
31de4 41 49 4e 20 61 6e 64 2f 6f 72 20 66 6f 6c 6c 6f  AIN and/or follo
31de5 77 65 64 20 62 79 0a 2a 2a 20 20 20 20 20 20 20  wed by.**       
31de6 20 20 20 20 20 20 20 20 20 20 54 45 4d 50 20 6f            TEMP o
31de7 72 20 54 45 4d 50 4f 52 41 52 59 0a 2a 2a 0a 2a  r TEMPORARY.**.*
31de8 2a 20 20 20 28 34 29 20 54 52 49 47 47 45 52 20  *   (4) TRIGGER 
31de9 20 20 57 65 20 61 72 65 20 69 6e 20 74 68 65 20    We are in the 
31dea 6d 69 64 64 6c 65 20 6f 66 20 61 20 74 72 69 67  middle of a trig
31deb 67 65 72 20 64 65 66 69 6e 69 74 69 6f 6e 20 74  ger definition t
31dec 68 61 74 20 6d 75 73 74 20 62 65 0a 2a 2a 20 20  hat must be.**  
31ded 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 65                 e
31dee 6e 64 65 64 20 62 79 20 61 20 73 65 6d 69 63 6f  nded by a semico
31def 6c 6f 6e 2c 20 74 68 65 20 6b 65 79 77 6f 72 64  lon, the keyword
31df0 20 45 4e 44 2c 20 61 6e 64 20 61 6e 6f 74 68 65   END, and anothe
31df1 72 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 0a  r semicolon..**.
31df2 2a 2a 20 20 20 28 35 29 20 53 45 4d 49 20 20 20  **   (5) SEMI   
31df3 20 20 20 57 65 27 76 65 20 73 65 65 6e 20 74 68     We've seen th
31df4 65 20 66 69 72 73 74 20 73 65 6d 69 63 6f 6c 6f  e first semicolo
31df5 6e 20 69 6e 20 74 68 65 20 22 3b 45 4e 44 3b 22  n in the ";END;"
31df6 20 74 68 61 74 20 6f 63 63 75 72 73 20 61 74 0a   that occurs at.
31df7 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
31df8 20 20 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20     the end of a 
31df9 74 72 69 67 67 65 72 20 64 65 66 69 6e 69 74 69  trigger definiti
31dfa 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 36 29 20  on..**.**   (6) 
31dfb 45 4e 44 20 20 20 20 20 20 20 57 65 27 76 65 20  END       We've 
31dfc 73 65 65 6e 20 74 68 65 20 22 3b 45 4e 44 22 20  seen the ";END" 
31dfd 6f 66 20 74 68 65 20 22 3b 45 4e 44 3b 22 20 74  of the ";END;" t
31dfe 68 61 74 20 6f 63 63 75 72 73 20 61 74 20 74 68  hat occurs at th
31dff 65 20 65 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20  e end.**        
31e00 20 20 20 20 20 20 20 20 20 6f 66 20 61 20 74 72           of a tr
31e01 69 67 67 65 72 20 64 69 66 69 6e 69 74 69 6f 6e  igger difinition
31e02 2e 0a 2a 2a 0a 2a 2a 20 54 72 61 6e 73 69 74 69  ..**.** Transiti
31e03 6f 6e 73 20 62 65 74 77 65 65 6e 20 73 74 61 74  ons between stat
31e04 65 73 20 61 62 6f 76 65 20 61 72 65 20 64 65 74  es above are det
31e05 65 72 6d 69 6e 65 64 20 62 79 20 74 6f 6b 65 6e  ermined by token
31e06 73 20 65 78 74 72 61 63 74 65 64 0a 2a 2a 20 66  s extracted.** f
31e07 72 6f 6d 20 74 68 65 20 69 6e 70 75 74 2e 20 20  rom the input.  
31e08 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 6f  The following to
31e09 6b 65 6e 73 20 61 72 65 20 73 69 67 6e 69 66 69  kens are signifi
31e0a 63 61 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 30  cant:.**.**   (0
31e0b 29 20 74 6b 53 45 4d 49 20 20 20 20 20 20 41 20  ) tkSEMI      A 
31e0c 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 20 20 20  semicolon..**   
31e0d 28 31 29 20 74 6b 57 53 20 20 20 20 20 20 20 20  (1) tkWS        
31e0e 57 68 69 74 65 73 70 61 63 65 0a 2a 2a 20 20 20  Whitespace.**   
31e0f 28 32 29 20 74 6b 4f 54 48 45 52 20 20 20 20 20  (2) tkOTHER     
31e10 41 6e 79 20 6f 74 68 65 72 20 53 51 4c 20 74 6f  Any other SQL to
31e11 6b 65 6e 2e 0a 2a 2a 20 20 20 28 33 29 20 74 6b  ken..**   (3) tk
31e12 45 58 50 4c 41 49 4e 20 20 20 54 68 65 20 22 65  EXPLAIN   The "e
31e13 78 70 6c 61 69 6e 22 20 6b 65 79 77 6f 72 64 2e  xplain" keyword.
31e14 0a 2a 2a 20 20 20 28 34 29 20 74 6b 43 52 45 41  .**   (4) tkCREA
31e15 54 45 20 20 20 20 54 68 65 20 22 63 72 65 61 74  TE    The "creat
31e16 65 22 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 20 20  e" keyword..**  
31e17 20 28 35 29 20 74 6b 54 45 4d 50 20 20 20 20 20   (5) tkTEMP     
31e18 20 54 68 65 20 22 74 65 6d 70 22 20 6f 72 20 22   The "temp" or "
31e19 74 65 6d 70 6f 72 61 72 79 22 20 6b 65 79 77 6f  temporary" keywo
31e1a 72 64 2e 0a 2a 2a 20 20 20 28 36 29 20 74 6b 54  rd..**   (6) tkT
31e1b 52 49 47 47 45 52 20 20 20 54 68 65 20 22 74 72  RIGGER   The "tr
31e1c 69 67 67 65 72 22 20 6b 65 79 77 6f 72 64 2e 0a  igger" keyword..
31e1d 2a 2a 20 20 20 28 37 29 20 74 6b 45 4e 44 20 20  **   (7) tkEND  
31e1e 20 20 20 20 20 54 68 65 20 22 65 6e 64 22 20 6b       The "end" k
31e1f 65 79 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 57 68  eyword..**.** Wh
31e20 69 74 65 73 70 61 63 65 20 6e 65 76 65 72 20 63  itespace never c
31e21 61 75 73 65 73 20 61 20 73 74 61 74 65 20 74 72  auses a state tr
31e22 61 6e 73 69 74 69 6f 6e 20 61 6e 64 20 69 73 20  ansition and is 
31e23 61 6c 77 61 79 73 20 69 67 6e 6f 72 65 64 2e 0a  always ignored..
31e24 2a 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70  **.** If we comp
31e25 69 6c 65 20 77 69 74 68 20 53 51 4c 49 54 45 5f  ile with SQLITE_
31e26 4f 4d 49 54 5f 54 52 49 47 47 45 52 2c 20 61 6c  OMIT_TRIGGER, al
31e27 6c 20 6f 66 20 74 68 65 20 63 6f 6d 70 75 74 61  l of the computa
31e28 74 69 6f 6e 20 6e 65 65 64 65 64 0a 2a 2a 20 74  tion needed.** t
31e29 6f 20 72 65 63 6f 67 6e 69 7a 65 20 74 68 65 20  o recognize the 
31e2a 65 6e 64 20 6f 66 20 61 20 74 72 69 67 67 65 72  end of a trigger
31e2b 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 2e   can be omitted.
31e2c 20 20 41 6c 6c 20 77 65 20 68 61 76 65 20 74 6f    All we have to
31e2d 20 64 6f 0a 2a 2a 20 69 73 20 6c 6f 6f 6b 20 66   do.** is look f
31e2e 6f 72 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 20 74  or a semicolon t
31e2f 68 61 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20  hat is not part 
31e30 6f 66 20 61 6e 20 73 74 72 69 6e 67 20 6f 72 20  of an string or 
31e31 63 6f 6d 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  comment..*/.SQLI
31e32 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
31e33 65 33 5f 63 6f 6d 70 6c 65 74 65 28 63 6f 6e 73  e3_complete(cons
31e34 74 20 63 68 61 72 20 2a 7a 53 71 6c 29 7b 0a 20  t char *zSql){. 
31e35 20 75 38 20 73 74 61 74 65 20 3d 20 30 3b 20 20   u8 state = 0;  
31e36 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 74 61 74   /* Current stat
31e37 65 2c 20 75 73 69 6e 67 20 6e 75 6d 62 65 72 73  e, using numbers
31e38 20 64 65 66 69 6e 65 64 20 69 6e 20 68 65 61 64   defined in head
31e39 65 72 20 63 6f 6d 6d 65 6e 74 20 2a 2f 0a 20 20  er comment */.  
31e3a 75 38 20 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20  u8 token;       
31e3b 2f 2a 20 56 61 6c 75 65 20 6f 66 20 74 68 65 20  /* Value of the 
31e3c 6e 65 78 74 20 74 6f 6b 65 6e 20 2a 2f 0a 0a 23  next token */..#
31e3d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
31e3e 49 54 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20  IT_TRIGGER.  /* 
31e3f 41 20 63 6f 6d 70 6c 65 78 20 73 74 61 74 65 6d  A complex statem
31e40 65 6e 74 20 6d 61 63 68 69 6e 65 20 75 73 65 64  ent machine used
31e41 20 74 6f 20 64 65 74 65 63 74 20 74 68 65 20 65   to detect the e
31e42 6e 64 20 6f 66 20 61 20 43 52 45 41 54 45 20 54  nd of a CREATE T
31e43 52 49 47 47 45 52 0a 20 20 2a 2a 20 73 74 61 74  RIGGER.  ** stat
31e44 65 6d 65 6e 74 2e 20 20 54 68 69 73 20 69 73 20  ement.  This is 
31e45 74 68 65 20 6e 6f 72 6d 61 6c 20 63 61 73 65 2e  the normal case.
31e46 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63  .  */.  static c
31e47 6f 6e 73 74 20 75 38 20 74 72 61 6e 73 5b 37 5d  onst u8 trans[7]
31e48 5b 38 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20  [8] = {.        
31e49 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
31e4a 54 6f 6b 65 6e 3a 20 20 20 20 20 20 20 20 20 20  Token:          
31e4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31e4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31e4d 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 2f 2a        */.     /*
31e4e 20 53 74 61 74 65 3a 20 20 20 20 20 20 20 2a 2a   State:       **
31e4f 20 20 53 45 4d 49 20 20 57 53 20 20 4f 54 48 45    SEMI  WS  OTHE
31e50 52 20 45 58 50 4c 41 49 4e 20 20 43 52 45 41 54  R EXPLAIN  CREAT
31e51 45 20 20 54 45 4d 50 20 20 54 52 49 47 47 45 52  E  TEMP  TRIGGER
31e52 20 20 45 4e 44 20 20 2a 2f 0a 20 20 20 20 20 2f    END  */.     /
31e53 2a 20 30 20 20 20 53 54 41 52 54 3a 20 2a 2f 20  * 0   START: */ 
31e54 7b 20 20 20 20 30 2c 20 20 30 2c 20 20 20 20 20  {    0,  0,     
31e55 31 2c 20 20 20 20 20 20 32 2c 20 20 20 20 20 20  1,      2,      
31e56 33 2c 20 20 20 20 31 2c 20 20 20 20 20 20 20 31  3,    1,       1
31e57 2c 20 20 20 31 2c 20 20 7d 2c 0a 20 20 20 20 20  ,   1,  },.     
31e58 2f 2a 20 31 20 20 4e 4f 52 4d 41 4c 3a 20 2a 2f  /* 1  NORMAL: */
31e59 20 7b 20 20 20 20 30 2c 20 20 31 2c 20 20 20 20   {    0,  1,    
31e5a 20 31 2c 20 20 20 20 20 20 31 2c 20 20 20 20 20   1,      1,     
31e5b 20 31 2c 20 20 20 20 31 2c 20 20 20 20 20 20 20   1,    1,       
31e5c 31 2c 20 20 20 31 2c 20 20 7d 2c 0a 20 20 20 20  1,   1,  },.    
31e5d 20 2f 2a 20 32 20 45 58 50 4c 41 49 4e 3a 20 2a   /* 2 EXPLAIN: *
31e5e 2f 20 7b 20 20 20 20 30 2c 20 20 32 2c 20 20 20  / {    0,  2,   
31e5f 20 20 32 2c 20 20 20 20 20 20 31 2c 20 20 20 20    2,      1,    
31e60 20 20 33 2c 20 20 20 20 31 2c 20 20 20 20 20 20    3,    1,      
31e61 20 31 2c 20 20 20 31 2c 20 20 7d 2c 0a 20 20 20   1,   1,  },.   
31e62 20 20 2f 2a 20 33 20 20 43 52 45 41 54 45 3a 20    /* 3  CREATE: 
31e63 2a 2f 20 7b 20 20 20 20 30 2c 20 20 33 2c 20 20  */ {    0,  3,  
31e64 20 20 20 31 2c 20 20 20 20 20 20 31 2c 20 20 20     1,      1,   
31e65 20 20 20 31 2c 20 20 20 20 33 2c 20 20 20 20 20     1,    3,     
31e66 20 20 34 2c 20 20 20 31 2c 20 20 7d 2c 0a 20 20    4,   1,  },.  
31e67 20 20 20 2f 2a 20 34 20 54 52 49 47 47 45 52 3a     /* 4 TRIGGER:
31e68 20 2a 2f 20 7b 20 20 20 20 35 2c 20 20 34 2c 20   */ {    5,  4, 
31e69 20 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20      4,      4,  
31e6a 20 20 20 20 34 2c 20 20 20 20 34 2c 20 20 20 20      4,    4,    
31e6b 20 20 20 34 2c 20 20 20 34 2c 20 20 7d 2c 0a 20     4,   4,  },. 
31e6c 20 20 20 20 2f 2a 20 35 20 20 20 20 53 45 4d 49      /* 5    SEMI
31e6d 3a 20 2a 2f 20 7b 20 20 20 20 35 2c 20 20 35 2c  : */ {    5,  5,
31e6e 20 20 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20       4,      4, 
31e6f 20 20 20 20 20 34 2c 20 20 20 20 34 2c 20 20 20       4,    4,   
31e70 20 20 20 20 34 2c 20 20 20 36 2c 20 20 7d 2c 0a      4,   6,  },.
31e71 20 20 20 20 20 2f 2a 20 36 20 20 20 20 20 45 4e       /* 6     EN
31e72 44 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 36  D: */ {    0,  6
31e73 2c 20 20 20 20 20 34 2c 20 20 20 20 20 20 34 2c  ,     4,      4,
31e74 20 20 20 20 20 20 34 2c 20 20 20 20 34 2c 20 20        4,    4,  
31e75 20 20 20 20 20 34 2c 20 20 20 34 2c 20 20 7d 2c       4,   4,  },
31e76 0a 20 20 7d 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a  .  };.#else.  /*
31e77 20 49 66 20 74 72 69 67 67 65 72 73 20 61 72 65   If triggers are
31e78 20 6e 6f 74 20 73 75 70 70 6f 72 65 64 20 62 79   not suppored by
31e79 20 74 68 69 73 20 63 6f 6d 70 69 6c 65 20 74 68   this compile th
31e7a 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  en the statement
31e7b 20 6d 61 63 68 69 6e 65 0a 20 20 2a 2a 20 75 73   machine.  ** us
31e7c 65 64 20 74 6f 20 64 65 74 65 63 74 20 74 68 65  ed to detect the
31e7d 20 65 6e 64 20 6f 66 20 61 20 73 74 61 74 65 6d   end of a statem
31e7e 65 6e 74 20 69 73 20 6d 75 63 68 20 73 69 6d 70  ent is much simp
31e7f 6c 69 65 72 0a 20 20 2a 2f 0a 20 20 73 74 61 74  lier.  */.  stat
31e80 69 63 20 63 6f 6e 73 74 20 75 38 20 74 72 61 6e  ic const u8 tran
31e81 73 5b 32 5d 5b 33 5d 20 3d 20 7b 0a 20 20 20 20  s[2][3] = {.    
31e82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31e83 20 2f 2a 20 54 6f 6b 65 6e 3a 20 20 20 20 20 20   /* Token:      
31e84 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 2f 2a 20       */.     /* 
31e85 53 74 61 74 65 3a 20 20 20 20 20 20 20 2a 2a 20  State:       ** 
31e86 20 53 45 4d 49 20 20 57 53 20 20 4f 54 48 45 52   SEMI  WS  OTHER
31e87 20 2a 2f 0a 20 20 20 20 20 2f 2a 20 30 20 20 20   */.     /* 0   
31e88 53 54 41 52 54 3a 20 2a 2f 20 7b 20 20 20 20 30  START: */ {    0
31e89 2c 20 20 30 2c 20 20 20 20 20 31 2c 20 7d 2c 0a  ,  0,     1, },.
31e8a 20 20 20 20 20 2f 2a 20 31 20 20 4e 4f 52 4d 41       /* 1  NORMA
31e8b 4c 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 31  L: */ {    0,  1
31e8c 2c 20 20 20 20 20 31 2c 20 7d 2c 0a 20 20 7d 3b  ,     1, },.  };
31e8d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
31e8e 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a  E_OMIT_TRIGGER *
31e8f 2f 0a 0a 20 20 77 68 69 6c 65 28 20 2a 7a 53 71  /..  while( *zSq
31e90 6c 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68 28  l ){.    switch(
31e91 20 2a 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20 20   *zSql ){.      
31e92 63 61 73 65 20 27 3b 27 3a 20 7b 20 20 2f 2a 20  case ';': {  /* 
31e93 41 20 73 65 6d 69 63 6f 6c 6f 6e 20 2a 2f 0a 20  A semicolon */. 
31e94 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
31e95 6b 53 45 4d 49 3b 0a 20 20 20 20 20 20 20 20 62  kSEMI;.        b
31e96 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
31e97 20 20 20 20 63 61 73 65 20 27 20 27 3a 0a 20 20      case ' ':.  
31e98 20 20 20 20 63 61 73 65 20 27 5c 72 27 3a 0a 20      case '\r':. 
31e99 20 20 20 20 20 63 61 73 65 20 27 5c 74 27 3a 0a       case '\t':.
31e9a 20 20 20 20 20 20 63 61 73 65 20 27 5c 6e 27 3a        case '\n':
31e9b 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c 66 27  .      case '\f'
31e9c 3a 20 7b 20 20 2f 2a 20 57 68 69 74 65 20 73 70  : {  /* White sp
31e9d 61 63 65 20 69 73 20 69 67 6e 6f 72 65 64 20 2a  ace is ignored *
31e9e 2f 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20  /.        token 
31e9f 3d 20 74 6b 57 53 3b 0a 20 20 20 20 20 20 20 20  = tkWS;.        
31ea0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
31ea1 20 20 20 20 20 63 61 73 65 20 27 2f 27 3a 20 7b       case '/': {
31ea2 20 20 20 2f 2a 20 43 2d 73 74 79 6c 65 20 63 6f     /* C-style co
31ea3 6d 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20  mments */.      
31ea4 20 20 69 66 28 20 7a 53 71 6c 5b 31 5d 21 3d 27    if( zSql[1]!='
31ea5 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  *' ){.          
31ea6 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b  token = tkOTHER;
31ea7 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
31ea8 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
31ea9 20 20 20 20 7a 53 71 6c 20 2b 3d 20 32 3b 0a 20      zSql += 2;. 
31eaa 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 7a 53         while( zS
31eab 71 6c 5b 30 5d 20 26 26 20 28 7a 53 71 6c 5b 30  ql[0] && (zSql[0
31eac 5d 21 3d 27 2a 27 20 7c 7c 20 7a 53 71 6c 5b 31  ]!='*' || zSql[1
31ead 5d 21 3d 27 2f 27 29 20 29 7b 20 7a 53 71 6c 2b  ]!='/') ){ zSql+
31eae 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28  +; }.        if(
31eaf 20 7a 53 71 6c 5b 30 5d 3d 3d 30 20 29 20 72 65   zSql[0]==0 ) re
31eb0 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20  turn 0;.        
31eb1 7a 53 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20  zSql++;.        
31eb2 74 6f 6b 65 6e 20 3d 20 74 6b 57 53 3b 0a 20 20  token = tkWS;.  
31eb3 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31eb4 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20     }.      case 
31eb5 27 2d 27 3a 20 7b 20 20 20 2f 2a 20 53 51 4c 2d  '-': {   /* SQL-
31eb6 73 74 79 6c 65 20 63 6f 6d 6d 65 6e 74 73 20 66  style comments f
31eb7 72 6f 6d 20 22 2d 2d 22 20 74 6f 20 65 6e 64 20  rom "--" to end 
31eb8 6f 66 20 6c 69 6e 65 20 2a 2f 0a 20 20 20 20 20  of line */.     
31eb9 20 20 20 69 66 28 20 7a 53 71 6c 5b 31 5d 21 3d     if( zSql[1]!=
31eba 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20  '-' ){.         
31ebb 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52   token = tkOTHER
31ebc 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
31ebd 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
31ebe 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 71       while( *zSq
31ebf 6c 20 26 26 20 2a 7a 53 71 6c 21 3d 27 5c 6e 27  l && *zSql!='\n'
31ec0 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20 20   ){ zSql++; }.  
31ec1 20 20 20 20 20 20 69 66 28 20 2a 7a 53 71 6c 3d        if( *zSql=
31ec2 3d 30 20 29 20 72 65 74 75 72 6e 20 73 74 61 74  =0 ) return stat
31ec3 65 3d 3d 30 3b 0a 20 20 20 20 20 20 20 20 74 6f  e==0;.        to
31ec4 6b 65 6e 20 3d 20 74 6b 57 53 3b 0a 20 20 20 20  ken = tkWS;.    
31ec5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
31ec6 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27 5b   }.      case '[
31ec7 27 3a 20 7b 20 20 20 2f 2a 20 4d 69 63 72 6f 73  ': {   /* Micros
31ec8 6f 66 74 2d 73 74 79 6c 65 20 69 64 65 6e 74 69  oft-style identi
31ec9 66 69 65 72 73 20 69 6e 20 5b 2e 2e 2e 5d 20 2a  fiers in [...] *
31eca 2f 0a 20 20 20 20 20 20 20 20 7a 53 71 6c 2b 2b  /.        zSql++
31ecb 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  ;.        while(
31ecc 20 2a 7a 53 71 6c 20 26 26 20 2a 7a 53 71 6c 21   *zSql && *zSql!
31ecd 3d 27 5d 27 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20  =']' ){ zSql++; 
31ece 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 2a 7a  }.        if( *z
31ecf 53 71 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  Sql==0 ) return 
31ed0 30 3b 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e  0;.        token
31ed1 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20   = tkOTHER;.    
31ed2 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
31ed3 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27 60   }.      case '`
31ed4 27 3a 20 20 20 20 20 2f 2a 20 47 72 61 76 65 2d  ':     /* Grave-
31ed5 61 63 63 65 6e 74 20 71 75 6f 74 65 64 20 73 79  accent quoted sy
31ed6 6d 62 6f 6c 73 20 75 73 65 64 20 62 79 20 4d 79  mbols used by My
31ed7 53 51 4c 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  SQL */.      cas
31ed8 65 20 27 22 27 3a 20 20 20 20 20 2f 2a 20 73 69  e '"':     /* si
31ed9 6e 67 6c 65 2d 20 61 6e 64 20 64 6f 75 62 6c 65  ngle- and double
31eda 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67 73 20  -quoted strings 
31edb 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c  */.      case '\
31edc 27 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e  '': {.        in
31edd 74 20 63 20 3d 20 2a 7a 53 71 6c 3b 0a 20 20 20  t c = *zSql;.   
31ede 20 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20 20       zSql++;.   
31edf 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 71       while( *zSq
31ee0 6c 20 26 26 20 2a 7a 53 71 6c 21 3d 63 20 29 7b  l && *zSql!=c ){
31ee1 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20   zSql++; }.     
31ee2 20 20 20 69 66 28 20 2a 7a 53 71 6c 3d 3d 30 20     if( *zSql==0 
31ee3 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
31ee4 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54      token = tkOT
31ee5 48 45 52 3b 0a 20 20 20 20 20 20 20 20 62 72 65  HER;.        bre
31ee6 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
31ee7 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
31ee8 20 20 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20       int c;.    
31ee9 20 20 20 20 69 66 28 20 49 64 43 68 61 72 28 28      if( IdChar((
31eea 75 38 29 2a 7a 53 71 6c 29 20 29 7b 0a 20 20 20  u8)*zSql) ){.   
31eeb 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 77 6f 72         /* Keywor
31eec 64 73 20 61 6e 64 20 75 6e 71 75 6f 74 65 64 20  ds and unquoted 
31eed 69 64 65 6e 74 69 66 69 65 72 73 20 2a 2f 0a 20  identifiers */. 
31eee 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 49 64           int nId
31eef 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28  ;.          for(
31ef0 6e 49 64 3d 31 3b 20 49 64 43 68 61 72 28 7a 53  nId=1; IdChar(zS
31ef1 71 6c 5b 6e 49 64 5d 29 3b 20 6e 49 64 2b 2b 29  ql[nId]); nId++)
31ef2 7b 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  {}.#ifdef SQLITE
31ef3 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20  _OMIT_TRIGGER.  
31ef4 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
31ef5 74 6b 4f 54 48 45 52 3b 0a 23 65 6c 73 65 0a 20  tkOTHER;.#else. 
31ef6 20 20 20 20 20 20 20 20 20 73 77 69 74 63 68 28           switch(
31ef7 20 2a 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20 20   *zSql ){.      
31ef8 20 20 20 20 20 20 63 61 73 65 20 27 63 27 3a 20        case 'c': 
31ef9 63 61 73 65 20 27 43 27 3a 20 7b 0a 20 20 20 20  case 'C': {.    
31efa 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 49            if( nI
31efb 64 3d 3d 36 20 26 26 20 73 71 6c 69 74 65 33 53  d==6 && sqlite3S
31efc 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 63  trNICmp(zSql, "c
31efd 72 65 61 74 65 22 2c 20 36 29 3d 3d 30 20 29 7b  reate", 6)==0 ){
31efe 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
31eff 20 74 6f 6b 65 6e 20 3d 20 74 6b 43 52 45 41 54   token = tkCREAT
31f00 45 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  E;.             
31f01 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
31f02 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
31f03 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20  tkOTHER;.       
31f04 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
31f05 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
31f06 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
31f07 20 20 20 20 20 20 20 20 63 61 73 65 20 27 74 27          case 't'
31f08 3a 20 63 61 73 65 20 27 54 27 3a 20 7b 0a 20 20  : case 'T': {.  
31f09 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
31f0a 6e 49 64 3d 3d 37 20 26 26 20 73 71 6c 69 74 65  nId==7 && sqlite
31f0b 33 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20  3StrNICmp(zSql, 
31f0c 22 74 72 69 67 67 65 72 22 2c 20 37 29 3d 3d 30  "trigger", 7)==0
31f0d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
31f0e 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 54 52      token = tkTR
31f0f 49 47 47 45 52 3b 0a 20 20 20 20 20 20 20 20 20  IGGER;.         
31f10 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e       }else if( n
31f11 49 64 3d 3d 34 20 26 26 20 73 71 6c 69 74 65 33  Id==4 && sqlite3
31f12 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22  StrNICmp(zSql, "
31f13 74 65 6d 70 22 2c 20 34 29 3d 3d 30 20 29 7b 0a  temp", 4)==0 ){.
31f14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31f15 74 6f 6b 65 6e 20 3d 20 74 6b 54 45 4d 50 3b 0a  token = tkTEMP;.
31f16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 65                }e
31f17 6c 73 65 20 69 66 28 20 6e 49 64 3d 3d 39 20 26  lse if( nId==9 &
31f18 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  & sqlite3StrNICm
31f19 70 28 7a 53 71 6c 2c 20 22 74 65 6d 70 6f 72 61  p(zSql, "tempora
31f1a 72 79 22 2c 20 39 29 3d 3d 30 20 29 7b 0a 20 20  ry", 9)==0 ){.  
31f1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
31f1c 6b 65 6e 20 3d 20 74 6b 54 45 4d 50 3b 0a 20 20  ken = tkTEMP;.  
31f1d 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73              }els
31f1e 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e{.             
31f1f 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48     token = tkOTH
31f20 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ER;.            
31f21 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
31f22 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
31f23 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
31f24 20 20 20 63 61 73 65 20 27 65 27 3a 20 20 63 61     case 'e':  ca
31f25 73 65 20 27 45 27 3a 20 7b 0a 20 20 20 20 20 20  se 'E': {.      
31f26 20 20 20 20 20 20 20 20 69 66 28 20 6e 49 64 3d          if( nId=
31f27 3d 33 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  =3 && sqlite3Str
31f28 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 65 6e 64  NICmp(zSql, "end
31f29 22 2c 20 33 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ", 3)==0 ){.    
31f2a 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65              toke
31f2b 6e 20 3d 20 74 6b 45 4e 44 3b 0a 20 20 20 20 20  n = tkEND;.     
31f2c 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 0a 23           }else.#
31f2d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
31f2e 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 20 20 20  IT_EXPLAIN.     
31f2f 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 49 64           if( nId
31f30 3d 3d 37 20 26 26 20 73 71 6c 69 74 65 33 53 74  ==7 && sqlite3St
31f31 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 65 78  rNICmp(zSql, "ex
31f32 70 6c 61 69 6e 22 2c 20 37 29 3d 3d 30 20 29 7b  plain", 7)==0 ){
31f33 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
31f34 20 74 6f 6b 65 6e 20 3d 20 74 6b 45 58 50 4c 41   token = tkEXPLA
31f35 49 4e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  IN;.            
31f36 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20    }else.#endif. 
31f37 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 0a 20               {. 
31f38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
31f39 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a  oken = tkOTHER;.
31f3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a                }.
31f3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72                br
31f3c 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  eak;.           
31f3d 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 64   }.            d
31f3e 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
31f3f 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
31f40 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20  tkOTHER;.       
31f41 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
31f42 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
31f43 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f        }.#endif /
31f44 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  * SQLITE_OMIT_TR
31f45 49 47 47 45 52 20 2a 2f 0a 20 20 20 20 20 20 20  IGGER */.       
31f46 20 20 20 7a 53 71 6c 20 2b 3d 20 6e 49 64 2d 31     zSql += nId-1
31f47 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
31f48 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70  .          /* Op
31f49 65 72 61 74 6f 72 73 20 61 6e 64 20 73 70 65 63  erators and spec
31f4a 69 61 6c 20 73 79 6d 62 6f 6c 73 20 2a 2f 0a 20  ial symbols */. 
31f4b 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d           token =
31f4c 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20   tkOTHER;.      
31f4d 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61    }.        brea
31f4e 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
31f4f 0a 20 20 20 20 73 74 61 74 65 20 3d 20 74 72 61  .    state = tra
31f50 6e 73 5b 73 74 61 74 65 5d 5b 74 6f 6b 65 6e 5d  ns[state][token]
31f51 3b 0a 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20  ;.    zSql++;.  
31f52 7d 0a 20 20 72 65 74 75 72 6e 20 73 74 61 74 65  }.  return state
31f53 3d 3d 30 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  ==0;.}..#ifndef 
31f54 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
31f55 36 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  6./*.** This rou
31f56 74 69 6e 65 20 69 73 20 74 68 65 20 73 61 6d 65  tine is the same
31f57 20 61 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f   as the sqlite3_
31f58 63 6f 6d 70 6c 65 74 65 28 29 20 72 6f 75 74 69  complete() routi
31f59 6e 65 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20  ne described.** 
31f5a 61 62 6f 76 65 2c 20 65 78 63 65 70 74 20 74 68  above, except th
31f5b 61 74 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  at the parameter
31f5c 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f 20   is required to 
31f5d 62 65 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65  be UTF-16 encode
31f5e 64 2c 20 6e 6f 74 0a 2a 2a 20 55 54 46 2d 38 2e  d, not.** UTF-8.
31f5f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
31f60 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  nt sqlite3_compl
31f61 65 74 65 31 36 28 63 6f 6e 73 74 20 76 6f 69 64  ete16(const void
31f62 20 2a 7a 53 71 6c 29 7b 0a 20 20 73 71 6c 69 74   *zSql){.  sqlit
31f63 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 3b 0a  e3_value *pVal;.
31f64 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 53    char const *zS
31f65 71 6c 38 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ql8;.  int rc = 
31f66 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a 23  SQLITE_NOMEM;..#
31f67 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
31f68 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 72 63  IT_AUTOINIT.  rc
31f69 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69   = sqlite3_initi
31f6a 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 72  alize();.  if( r
31f6b 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 23  c ) return rc;.#
31f6c 65 6e 64 69 66 0a 20 20 70 56 61 6c 20 3d 20 73  endif.  pVal = s
31f6d 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 30  qlite3ValueNew(0
31f6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 61 6c 75  );.  sqlite3Valu
31f6f 65 53 65 74 53 74 72 28 70 56 61 6c 2c 20 2d 31  eSetStr(pVal, -1
31f70 2c 20 7a 53 71 6c 2c 20 53 51 4c 49 54 45 5f 55  , zSql, SQLITE_U
31f71 54 46 31 36 4e 41 54 49 56 45 2c 20 53 51 4c 49  TF16NATIVE, SQLI
31f72 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7a 53  TE_STATIC);.  zS
31f73 71 6c 38 20 3d 20 73 71 6c 69 74 65 33 56 61 6c  ql8 = sqlite3Val
31f74 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c  ueText(pVal, SQL
31f75 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 69 66 28  ITE_UTF8);.  if(
31f76 20 7a 53 71 6c 38 20 29 7b 0a 20 20 20 20 72 63   zSql8 ){.    rc
31f77 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c   = sqlite3_compl
31f78 65 74 65 28 7a 53 71 6c 38 29 3b 0a 20 20 7d 65  ete(zSql8);.  }e
31f79 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  lse{.    rc = SQ
31f7a 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
31f7b 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72    sqlite3ValueFr
31f7c 65 65 28 70 56 61 6c 29 3b 0a 20 20 72 65 74 75  ee(pVal);.  retu
31f7d 72 6e 20 73 71 6c 69 74 65 33 41 70 69 45 78 69  rn sqlite3ApiExi
31f7e 74 28 30 2c 20 72 63 29 3b 0a 7d 0a 23 65 6e 64  t(0, rc);.}.#end
31f7f 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
31f80 54 5f 55 54 46 31 36 20 2a 2f 0a 23 65 6e 64 69  T_UTF16 */.#endi
31f81 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
31f82 5f 43 4f 4d 50 4c 45 54 45 20 2a 2f 0a 0a 2f 2a  _COMPLETE */../*
31f83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
31f84 64 20 6f 66 20 63 6f 6d 70 6c 65 74 65 2e 63 20  d of complete.c 
31f85 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31f86 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31f87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
31f88 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
31f89 67 69 6e 20 66 69 6c 65 20 6d 61 69 6e 2e 63 20  gin file main.c 
31f8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31f8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31f8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
31f8d 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
31f8e 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
31f8f 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
31f90 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
31f91 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
31f92 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
31f93 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
31f94 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
31f95 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
31f96 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
31f97 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
31f98 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
31f99 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
31f9a 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
31f9b 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
31f9c 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
31f9d 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
31f9e 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
31f9f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
31fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31fa1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31fa2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31fa3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31fa4 2a 2a 2a 2a 0a 2a 2a 20 4d 61 69 6e 20 66 69 6c  ****.** Main fil
31fa5 65 20 66 6f 72 20 74 68 65 20 53 51 4c 69 74 65  e for the SQLite
31fa6 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 72   library.  The r
31fa7 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20  outines in this 
31fa8 66 69 6c 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  file.** implemen
31fa9 74 20 74 68 65 20 70 72 6f 67 72 61 6d 6d 65 72  t the programmer
31faa 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68   interface to th
31fab 65 20 6c 69 62 72 61 72 79 2e 20 20 52 6f 75 74  e library.  Rout
31fac 69 6e 65 73 20 69 6e 0a 2a 2a 20 6f 74 68 65 72  ines in.** other
31fad 20 66 69 6c 65 73 20 61 72 65 20 66 6f 72 20 69   files are for i
31fae 6e 74 65 72 6e 61 6c 20 75 73 65 20 62 79 20 53  nternal use by S
31faf 51 4c 69 74 65 20 61 6e 64 20 73 68 6f 75 6c 64  QLite and should
31fb0 20 6e 6f 74 20 62 65 0a 2a 2a 20 61 63 63 65 73   not be.** acces
31fb1 73 65 64 20 62 79 20 75 73 65 72 73 20 6f 66 20  sed by users of 
31fb2 74 68 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a  the library..*/.
31fb3 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
31fb4 4e 41 42 4c 45 5f 46 54 53 33 0a 2f 2a 2a 2a 2a  NABLE_FTS3./****
31fb5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
31fb6 64 65 20 66 74 73 33 2e 68 20 69 6e 20 74 68 65  de fts3.h in the
31fb7 20 6d 69 64 64 6c 65 20 6f 66 20 6d 61 69 6e 2e   middle of main.
31fb8 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
31fb9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
31fba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
31fbb 20 66 69 6c 65 20 66 74 73 33 2e 68 20 2a 2a 2a   file fts3.h ***
31fbc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31fbd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31fbe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
31fbf 20 32 30 30 36 20 4f 63 74 20 31 30 0a 2a 2a 0a   2006 Oct 10.**.
31fc0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
31fc1 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
31fc2 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
31fc3 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
31fc4 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
31fc5 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
31fc6 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
31fc7 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
31fc8 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
31fc9 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
31fca 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
31fcb 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
31fcc 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
31fcd 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
31fce 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
31fcf 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
31fd0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
31fd1 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
31fd2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31fd3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31fd4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31fd5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31fd6 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64  .**.** This head
31fd7 65 72 20 66 69 6c 65 20 69 73 20 75 73 65 64 20  er file is used 
31fd8 62 79 20 70 72 6f 67 72 61 6d 73 20 74 68 61 74  by programs that
31fd9 20 77 61 6e 74 20 74 6f 20 6c 69 6e 6b 20 61 67   want to link ag
31fda 61 69 6e 73 74 20 74 68 65 0a 2a 2a 20 46 54 53  ainst the.** FTS
31fdb 33 20 6c 69 62 72 61 72 79 2e 20 20 41 6c 6c 20  3 library.  All 
31fdc 69 74 20 64 6f 65 73 20 69 73 20 64 65 63 6c 61  it does is decla
31fdd 72 65 20 74 68 65 20 73 71 6c 69 74 65 33 46 74  re the sqlite3Ft
31fde 73 33 49 6e 69 74 28 29 20 69 6e 74 65 72 66 61  s3Init() interfa
31fdf 63 65 2e 0a 2a 2f 0a 0a 23 69 66 20 30 0a 65 78  ce..*/..#if 0.ex
31fe0 74 65 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69  tern "C" {.#endi
31fe1 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75  f  /* __cplusplu
31fe2 73 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49  s */..SQLITE_PRI
31fe3 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
31fe4 46 74 73 33 49 6e 69 74 28 73 71 6c 69 74 65 33  Fts3Init(sqlite3
31fe5 20 2a 64 62 29 3b 0a 0a 23 69 66 20 30 0a 7d 20   *db);..#if 0.} 
31fe6 20 2f 2a 20 65 78 74 65 72 6e 20 22 43 22 20 2a   /* extern "C" *
31fe7 2f 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63  /.#endif  /* __c
31fe8 70 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 2f 2a 2a  plusplus */../**
31fe9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
31fea 20 6f 66 20 66 74 73 33 2e 68 20 2a 2a 2a 2a 2a   of fts3.h *****
31feb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31fec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31fed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
31fee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
31fef 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
31ff0 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6d 61 69   left off in mai
31ff1 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
31ff2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e  ***********/.#en
31ff3 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
31ff4 45 5f 45 4e 41 42 4c 45 5f 52 54 52 45 45 0a 2f  E_ENABLE_RTREE./
31ff5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
31ff6 6e 63 6c 75 64 65 20 72 74 72 65 65 2e 68 20 69  nclude rtree.h i
31ff7 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
31ff8 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  main.c *********
31ff9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
31ffa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
31ffb 65 67 69 6e 20 66 69 6c 65 20 72 74 72 65 65 2e  egin file rtree.
31ffc 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
31ffd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31ffe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
31fff 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 36  *.** 2008 May 26
32000 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
32001 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
32002 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
32003 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
32004 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
32005 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
32006 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
32007 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
32008 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
32009 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
3200a 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
3200b 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
3200c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
3200d 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
3200e 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
3200f 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
32010 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
32011 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
32012 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32013 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32014 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32015 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32016 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
32017 68 65 61 64 65 72 20 66 69 6c 65 20 69 73 20 75  header file is u
32018 73 65 64 20 62 79 20 70 72 6f 67 72 61 6d 73 20  sed by programs 
32019 74 68 61 74 20 77 61 6e 74 20 74 6f 20 6c 69 6e  that want to lin
3201a 6b 20 61 67 61 69 6e 73 74 20 74 68 65 0a 2a 2a  k against the.**
3201b 20 52 54 52 45 45 20 6c 69 62 72 61 72 79 2e 20   RTREE library. 
3201c 20 41 6c 6c 20 69 74 20 64 6f 65 73 20 69 73 20   All it does is 
3201d 64 65 63 6c 61 72 65 20 74 68 65 20 73 71 6c 69  declare the sqli
3201e 74 65 33 52 74 72 65 65 49 6e 69 74 28 29 20 69  te3RtreeInit() i
3201f 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 0a 23 69  nterface..*/..#i
32020 66 20 30 0a 65 78 74 65 72 6e 20 22 43 22 20 7b  f 0.extern "C" {
32021 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70  .#endif  /* __cp
32022 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 53 51 4c 49  lusplus */..SQLI
32023 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
32024 71 6c 69 74 65 33 52 74 72 65 65 49 6e 69 74 28  qlite3RtreeInit(
32025 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 0a 23  sqlite3 *db);..#
32026 69 66 20 30 0a 7d 20 20 2f 2a 20 65 78 74 65 72  if 0.}  /* exter
32027 6e 20 22 43 22 20 2a 2f 0a 23 65 6e 64 69 66 20  n "C" */.#endif 
32028 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20   /* __cplusplus 
32029 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
3202a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 74 72 65 65  *** End of rtree
3202b 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
3202c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3202d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3202e 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
3202f 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
32030 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
32031 20 69 6e 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a   in main.c *****
32032 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32033 2a 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  **/.#endif.#ifde
32034 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
32035 49 43 55 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ICU./***********
32036 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69  *** Include sqli
32037 74 65 69 63 75 2e 68 20 69 6e 20 74 68 65 20 6d  teicu.h in the m
32038 69 64 64 6c 65 20 6f 66 20 6d 61 69 6e 2e 63 20  iddle of main.c 
32039 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3203a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
3203b 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73  *** Begin file s
3203c 71 6c 69 74 65 69 63 75 2e 68 20 2a 2a 2a 2a 2a  qliteicu.h *****
3203d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3203e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3203f 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d  **/./*.** 2008 M
32040 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ay 26.**.** The 
32041 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
32042 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
32043 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
32044 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
32045 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
32046 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
32047 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
32048 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
32049 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
3204a 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
3204b 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
3204c 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
3204d 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
3204e 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
3204f 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
32050 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
32051 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
32052 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32053 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32054 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32055 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32056 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
32057 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65  This header file
32058 20 69 73 20 75 73 65 64 20 62 79 20 70 72 6f 67   is used by prog
32059 72 61 6d 73 20 74 68 61 74 20 77 61 6e 74 20 74  rams that want t
3205a 6f 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 74  o link against t
3205b 68 65 0a 2a 2a 20 49 43 55 20 65 78 74 65 6e 73  he.** ICU extens
3205c 69 6f 6e 2e 20 20 41 6c 6c 20 69 74 20 64 6f 65  ion.  All it doe
3205d 73 20 69 73 20 64 65 63 6c 61 72 65 20 74 68 65  s is declare the
3205e 20 73 71 6c 69 74 65 33 49 63 75 49 6e 69 74 28   sqlite3IcuInit(
3205f 29 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a  ) interface..*/.
32060 0a 23 69 66 20 30 0a 65 78 74 65 72 6e 20 22 43  .#if 0.extern "C
32061 22 20 7b 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f  " {.#endif  /* _
32062 5f 63 70 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 53  _cplusplus */..S
32063 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
32064 74 20 73 71 6c 69 74 65 33 49 63 75 49 6e 69 74  t sqlite3IcuInit
32065 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 0a  (sqlite3 *db);..
32066 23 69 66 20 30 0a 7d 20 20 2f 2a 20 65 78 74 65  #if 0.}  /* exte
32067 72 6e 20 22 43 22 20 2a 2f 0a 23 65 6e 64 69 66  rn "C" */.#endif
32068 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73    /* __cplusplus
32069 20 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a   */.../*********
3206a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c  ***** End of sql
3206b 69 74 65 69 63 75 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteicu.h *******
3206c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3206d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3206e 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
3206f 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
32070 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
32071 66 66 20 69 6e 20 6d 61 69 6e 2e 63 20 2a 2a 2a  ff in main.c ***
32072 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32073 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ****/.#endif../*
32074 0a 2a 2a 20 54 68 65 20 76 65 72 73 69 6f 6e 20  .** The version 
32075 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 0a 2a  of the library.*
32076 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
32077 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 53 51  _AMALGAMATION.SQ
32078 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63  LITE_API const c
32079 68 61 72 20 73 71 6c 69 74 65 33 5f 76 65 72 73  har sqlite3_vers
3207a 69 6f 6e 5b 5d 20 3d 20 53 51 4c 49 54 45 5f 56  ion[] = SQLITE_V
3207b 45 52 53 49 4f 4e 3b 0a 23 65 6e 64 69 66 0a 53  ERSION;.#endif.S
3207c 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
3207d 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c 69  char *sqlite3_li
3207e 62 76 65 72 73 69 6f 6e 28 76 6f 69 64 29 7b 20  bversion(void){ 
3207f 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 76  return sqlite3_v
32080 65 72 73 69 6f 6e 3b 20 7d 0a 53 51 4c 49 54 45  ersion; }.SQLITE
32081 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
32082 2a 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69  *sqlite3_sourcei
32083 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20  d(void){ return 
32084 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49 44  SQLITE_SOURCE_ID
32085 3b 20 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ; }.SQLITE_API i
32086 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65  nt sqlite3_libve
32087 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 76 6f 69  rsion_number(voi
32088 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54  d){ return SQLIT
32089 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52  E_VERSION_NUMBER
3208a 3b 20 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ; }.SQLITE_API i
3208b 6e 74 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61  nt sqlite3_threa
3208c 64 73 61 66 65 28 76 6f 69 64 29 7b 20 72 65 74  dsafe(void){ ret
3208d 75 72 6e 20 53 51 4c 49 54 45 5f 54 48 52 45 41  urn SQLITE_THREA
3208e 44 53 41 46 45 3b 20 7d 0a 0a 23 69 66 20 21 64  DSAFE; }..#if !d
3208f 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
32090 49 54 5f 54 52 41 43 45 29 20 26 26 20 64 65 66  IT_TRACE) && def
32091 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
32092 4c 45 5f 49 4f 54 52 41 43 45 29 0a 2f 2a 0a 2a  LE_IOTRACE)./*.*
32093 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * If the followi
32094 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e  ng function poin
32095 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20  ter is not NULL 
32096 61 6e 64 20 69 66 0a 2a 2a 20 53 51 4c 49 54 45  and if.** SQLITE
32097 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 20  _ENABLE_IOTRACE 
32098 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e  is enabled, then
32099 20 6d 65 73 73 61 67 65 73 20 64 65 73 63 72 69   messages descri
3209a 62 69 6e 67 0a 2a 2a 20 49 2f 4f 20 61 63 74 69  bing.** I/O acti
3209b 76 65 20 61 72 65 20 77 72 69 74 74 65 6e 20 75  ve are written u
3209c 73 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69  sing this functi
3209d 6f 6e 2e 20 20 54 68 65 73 65 20 6d 65 73 73 61  on.  These messa
3209e 67 65 73 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e  ges.** are inten
3209f 64 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e  ded for debuggin
320a0 67 20 61 63 74 69 76 69 74 79 20 6f 6e 6c 79 2e  g activity only.
320a1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
320a2 54 45 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65  TE void (*sqlite
320a3 33 49 6f 54 72 61 63 65 29 28 63 6f 6e 73 74 20  3IoTrace)(const 
320a4 63 68 61 72 2a 2c 20 2e 2e 2e 29 20 3d 20 30 3b  char*, ...) = 0;
320a5 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49  .#endif../*.** I
320a6 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
320a7 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
320a8 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 74 72 69  points to a stri
320a9 6e 67 20 77 68 69 63 68 20 69 73 20 74 68 65 0a  ng which is the.
320aa 2a 2a 20 6e 61 6d 65 20 6f 66 20 61 20 64 69 72  ** name of a dir
320ab 65 63 74 6f 72 79 2c 20 74 68 65 6e 20 74 68 61  ectory, then tha
320ac 74 20 64 69 72 65 63 74 6f 72 79 20 77 69 6c 6c  t directory will
320ad 20 62 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72   be used to stor
320ae 65 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66  e.** temporary f
320af 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  iles..**.** See 
320b0 61 6c 73 6f 20 74 68 65 20 22 50 52 41 47 4d 41  also the "PRAGMA
320b1 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65   temp_store_dire
320b2 63 74 6f 72 79 22 20 53 51 4c 20 63 6f 6d 6d 61  ctory" SQL comma
320b3 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  nd..*/.SQLITE_AP
320b4 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  I char *sqlite3_
320b5 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 3d  temp_directory =
320b6 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69   0;../*.** Initi
320b7 61 6c 69 7a 65 20 53 51 4c 69 74 65 2e 20 20 0a  alize SQLite.  .
320b8 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
320b9 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65  ne must be calle
320ba 64 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20  d to initialize 
320bb 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
320bc 61 74 69 6f 6e 2c 0a 2a 2a 20 56 46 53 2c 20 61  ation,.** VFS, a
320bd 6e 64 20 6d 75 74 65 78 20 73 75 62 73 79 73 74  nd mutex subsyst
320be 65 6d 73 20 70 72 69 6f 72 20 74 6f 20 64 6f 69  ems prior to doi
320bf 6e 67 20 61 6e 79 20 73 65 72 69 6f 75 73 20 77  ng any serious w
320c0 6f 72 6b 20 77 69 74 68 0a 2a 2a 20 53 51 4c 69  ork with.** SQLi
320c1 74 65 2e 20 20 42 75 74 20 61 73 20 6c 6f 6e 67  te.  But as long
320c2 20 61 73 20 79 6f 75 20 64 6f 20 6e 6f 74 20 63   as you do not c
320c3 6f 6d 70 69 6c 65 20 77 69 74 68 20 53 51 4c 49  ompile with SQLI
320c4 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54  TE_OMIT_AUTOINIT
320c5 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
320c6 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20   will be called 
320c7 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79  automatically by
320c8 20 6b 65 79 20 72 6f 75 74 69 6e 65 73 20 73 75   key routines su
320c9 63 68 20 61 73 0a 2a 2a 20 73 71 6c 69 74 65 33  ch as.** sqlite3
320ca 5f 6f 70 65 6e 28 29 2e 20 20 0a 2a 2a 0a 2a 2a  _open().  .**.**
320cb 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
320cc 20 61 20 6e 6f 2d 6f 70 20 65 78 63 65 70 74 20   a no-op except 
320cd 6f 6e 20 69 74 73 20 76 65 72 79 20 66 69 72 73  on its very firs
320ce 74 20 63 61 6c 6c 20 66 6f 72 20 74 68 65 20 70  t call for the p
320cf 72 6f 63 65 73 73 2c 0a 2a 2a 20 6f 72 20 66 6f  rocess,.** or fo
320d0 72 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c  r the first call
320d1 20 61 66 74 65 72 20 61 20 63 61 6c 6c 20 74 6f   after a call to
320d2 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77   sqlite3_shutdow
320d3 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72  n..**.** The fir
320d4 73 74 20 74 68 72 65 61 64 20 74 6f 20 63 61 6c  st thread to cal
320d5 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  l this routine r
320d6 75 6e 73 20 74 68 65 20 69 6e 69 74 69 61 6c 69  uns the initiali
320d7 7a 61 74 69 6f 6e 20 74 6f 0a 2a 2a 20 63 6f 6d  zation to.** com
320d8 70 6c 65 74 69 6f 6e 2e 20 20 49 66 20 73 75 62  pletion.  If sub
320d9 73 65 71 75 65 6e 74 20 74 68 72 65 61 64 73 20  sequent threads 
320da 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e  call this routin
320db 65 20 62 65 66 6f 72 65 20 74 68 65 20 66 69 72  e before the fir
320dc 73 74 0a 2a 2a 20 74 68 72 65 61 64 20 68 61 73  st.** thread has
320dd 20 66 69 6e 69 73 68 65 64 20 74 68 65 20 69 6e   finished the in
320de 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 70 72 6f  itialization pro
320df 63 65 73 73 2c 20 74 68 65 6e 20 74 68 65 20 73  cess, then the s
320e0 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 74 68 72  ubsequent.** thr
320e1 65 61 64 73 20 6d 75 73 74 20 62 6c 6f 63 6b 20  eads must block 
320e2 75 6e 74 69 6c 20 74 68 65 20 66 69 72 73 74 20  until the first 
320e3 74 68 72 65 61 64 20 66 69 6e 69 73 68 65 73 20  thread finishes 
320e4 77 69 74 68 20 74 68 65 20 69 6e 69 74 69 61 6c  with the initial
320e5 69 7a 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  ization..**.** T
320e6 68 65 20 66 69 72 73 74 20 74 68 72 65 61 64 20  he first thread 
320e7 6d 69 67 68 74 20 63 61 6c 6c 20 74 68 69 73 20  might call this 
320e8 72 6f 75 74 69 6e 65 20 72 65 63 75 72 73 69 76  routine recursiv
320e9 65 6c 79 2e 20 20 52 65 63 75 72 73 69 76 65 0a  ely.  Recursive.
320ea 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 74 68 69 73  ** calls to this
320eb 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20   routine should 
320ec 6e 6f 74 20 62 6c 6f 63 6b 2c 20 6f 66 20 63 6f  not block, of co
320ed 75 72 73 65 2e 20 20 4f 74 68 65 72 77 69 73 65  urse.  Otherwise
320ee 20 74 68 65 0a 2a 2a 20 69 6e 69 74 69 61 6c 69   the.** initiali
320ef 7a 61 74 69 6f 6e 20 70 72 6f 63 65 73 73 20 77  zation process w
320f0 6f 75 6c 64 20 6e 65 76 65 72 20 63 6f 6d 70 6c  ould never compl
320f1 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 4c 65 74 20 58  ete..**.** Let X
320f2 20 62 65 20 74 68 65 20 66 69 72 73 74 20 74 68   be the first th
320f3 72 65 61 64 20 74 6f 20 65 6e 74 65 72 20 74 68  read to enter th
320f4 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 4c 65 74  is routine.  Let
320f5 20 59 20 62 65 20 73 6f 6d 65 20 6f 74 68 65 72   Y be some other
320f6 0a 2a 2a 20 74 68 72 65 61 64 2e 20 20 54 68 65  .** thread.  The
320f7 6e 20 77 68 69 6c 65 20 74 68 65 20 69 6e 69 74  n while the init
320f8 69 61 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  ial invocation o
320f9 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 62  f this routine b
320fa 79 20 58 20 69 73 0a 2a 2a 20 69 6e 63 6f 6d 70  y X is.** incomp
320fb 6c 65 74 65 2c 20 69 74 20 69 73 20 72 65 71 75  lete, it is requ
320fc 69 72 65 64 20 74 68 61 74 3a 0a 2a 2a 0a 2a 2a  ired that:.**.**
320fd 20 20 20 20 2a 20 20 43 61 6c 6c 73 20 74 6f 20      *  Calls to 
320fe 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f  this routine fro
320ff 6d 20 59 20 6d 75 73 74 20 62 6c 6f 63 6b 20 75  m Y must block u
32100 6e 74 69 6c 20 74 68 65 20 6f 75 74 65 72 2d 6d  ntil the outer-m
32101 6f 73 74 0a 2a 2a 20 20 20 20 20 20 20 63 61 6c  ost.**       cal
32102 6c 20 62 79 20 58 20 63 6f 6d 70 6c 65 74 65 73  l by X completes
32103 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 52 65  ..**.**    *  Re
32104 63 75 72 73 69 76 65 20 63 61 6c 6c 73 20 74 6f  cursive calls to
32105 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72   this routine fr
32106 6f 6d 20 74 68 72 65 61 64 20 58 20 72 65 74 75  om thread X retu
32107 72 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a  rn immediately.*
32108 2a 20 20 20 20 20 20 20 77 69 74 68 6f 75 74 20  *       without 
32109 62 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 53 51 4c  blocking..*/.SQL
3210a 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
3210b 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 76  te3_initialize(v
3210c 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  oid){.  sqlite3_
3210d 6d 75 74 65 78 20 2a 70 4d 61 73 74 65 72 3b 20  mutex *pMaster; 
3210e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3210f 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e       /* The main
32110 20 73 74 61 74 69 63 20 6d 75 74 65 78 20 2a 2f   static mutex */
32111 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
32112 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32113 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32114 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 2a  /* Result code *
32115 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
32116 5f 4f 4d 49 54 5f 57 53 44 0a 20 20 72 63 20 3d  _OMIT_WSD.  rc =
32117 20 73 71 6c 69 74 65 33 5f 77 73 64 5f 69 6e 69   sqlite3_wsd_ini
32118 74 28 34 30 39 36 2c 20 32 34 29 3b 0a 20 20 69  t(4096, 24);.  i
32119 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3211a 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
3211b 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  c;.  }.#endif.. 
3211c 20 2f 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73   /* If SQLite is
3211d 20 61 6c 72 65 61 64 79 20 63 6f 6d 70 6c 65 74   already complet
3211e 65 6c 79 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c  ely initialized,
3211f 20 74 68 65 6e 20 74 68 69 73 20 63 61 6c 6c 0a   then this call.
32120 20 20 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f    ** to sqlite3_
32121 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 73 68 6f  initialize() sho
32122 75 6c 64 20 62 65 20 61 20 6e 6f 2d 6f 70 2e 20  uld be a no-op. 
32123 20 42 75 74 20 74 68 65 20 69 6e 69 74 69 61 6c   But the initial
32124 69 7a 61 74 69 6f 6e 0a 20 20 2a 2a 20 6d 75 73  ization.  ** mus
32125 74 20 62 65 20 63 6f 6d 70 6c 65 74 65 2e 20 20  t be complete.  
32126 53 6f 20 69 73 49 6e 69 74 20 6d 75 73 74 20 6e  So isInit must n
32127 6f 74 20 62 65 20 73 65 74 20 75 6e 74 69 6c 20  ot be set until 
32128 74 68 65 20 76 65 72 79 20 65 6e 64 0a 20 20 2a  the very end.  *
32129 2a 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e  * of this routin
3212a 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  e..  */.  if( sq
3212b 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
3212c 67 2e 69 73 49 6e 69 74 20 29 20 72 65 74 75 72  g.isInit ) retur
3212d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  n SQLITE_OK;..  
3212e 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  /* Make sure the
3212f 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d   mutex subsystem
32130 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e   is initialized.
32131 20 20 49 66 20 75 6e 61 62 6c 65 20 74 6f 20 0a    If unable to .
32132 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20    ** initialize 
32133 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73  the mutex subsys
32134 74 65 6d 2c 20 72 65 74 75 72 6e 20 65 61 72 6c  tem, return earl
32135 79 20 77 69 74 68 20 74 68 65 20 65 72 72 6f 72  y with the error
32136 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 73 79  ..  ** If the sy
32137 73 74 65 6d 20 69 73 20 73 6f 20 73 69 63 6b 20  stem is so sick 
32138 74 68 61 74 20 77 65 20 61 72 65 20 75 6e 61 62  that we are unab
32139 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61  le to allocate a
3213a 20 6d 75 74 65 78 2c 0a 20 20 2a 2a 20 74 68 65   mutex,.  ** the
3213b 72 65 20 69 73 20 6e 6f 74 20 6d 75 63 68 20 53  re is not much S
3213c 51 4c 69 74 65 20 69 73 20 67 6f 69 6e 67 20 74  QLite is going t
3213d 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 64 6f 2e  o be able to do.
3213e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 6d  .  **.  ** The m
3213f 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 20 6d  utex subsystem m
32140 75 73 74 20 74 61 6b 65 20 63 61 72 65 20 6f 66  ust take care of
32141 20 73 65 72 69 61 6c 69 7a 69 6e 67 20 69 74 73   serializing its
32142 20 6f 77 6e 0a 20 20 2a 2a 20 69 6e 69 74 69 61   own.  ** initia
32143 6c 69 7a 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20  lization..  */. 
32144 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4d 75 74   rc = sqlite3Mut
32145 65 78 49 6e 69 74 28 29 3b 0a 20 20 69 66 28 20  exInit();.  if( 
32146 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
32147 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65  .  /* Initialize
32148 20 74 68 65 20 6d 61 6c 6c 6f 63 28 29 20 73 79   the malloc() sy
32149 73 74 65 6d 20 61 6e 64 20 74 68 65 20 72 65 63  stem and the rec
3214a 75 72 73 69 76 65 20 70 49 6e 69 74 4d 75 74 65  ursive pInitMute
3214b 78 20 6d 75 74 65 78 2e 0a 20 20 2a 2a 20 54 68  x mutex..  ** Th
3214c 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20  is operation is 
3214d 70 72 6f 74 65 63 74 65 64 20 62 79 20 74 68 65  protected by the
3214e 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d   STATIC_MASTER m
3214f 75 74 65 78 2e 20 20 4e 6f 74 65 20 74 68 61 74  utex.  Note that
32150 0a 20 20 2a 2a 20 4d 75 74 65 78 41 6c 6c 6f 63  .  ** MutexAlloc
32151 28 29 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72  () is called for
32152 20 61 20 73 74 61 74 69 63 20 6d 75 74 65 78 20   a static mutex 
32153 70 72 69 6f 72 20 74 6f 20 69 6e 69 74 69 61 6c  prior to initial
32154 69 7a 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 6d  izing the.  ** m
32155 61 6c 6c 6f 63 20 73 75 62 73 79 73 74 65 6d 20  alloc subsystem 
32156 2d 20 74 68 69 73 20 69 6d 70 6c 69 65 73 20 74  - this implies t
32157 68 61 74 20 74 68 65 20 61 6c 6c 6f 63 61 74 69  hat the allocati
32158 6f 6e 20 6f 66 20 61 20 73 74 61 74 69 63 0a 20  on of a static. 
32159 20 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 6e   ** mutex must n
3215a 6f 74 20 72 65 71 75 69 72 65 20 73 75 70 70 6f  ot require suppo
3215b 72 74 20 66 72 6f 6d 20 74 68 65 20 6d 61 6c 6c  rt from the mall
3215c 6f 63 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20  oc subsystem..  
3215d 2a 2f 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 73  */.  pMaster = s
3215e 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
3215f 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
32160 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20  ATIC_MASTER);.  
32161 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
32162 74 65 72 28 70 4d 61 73 74 65 72 29 3b 0a 20 20  ter(pMaster);.  
32163 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
32164 66 69 67 2e 69 73 4d 75 74 65 78 49 6e 69 74 20  fig.isMutexInit 
32165 3d 20 31 3b 0a 20 20 69 66 28 20 21 73 71 6c 69  = 1;.  if( !sqli
32166 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
32167 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 20 29 7b 0a  isMallocInit ){.
32168 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
32169 4d 61 6c 6c 6f 63 49 6e 69 74 28 29 3b 0a 20 20  MallocInit();.  
3216a 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  }.  if( rc==SQLI
3216b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c  TE_OK ){.    sql
3216c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
3216d 2e 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 20 3d 20  .isMallocInit = 
3216e 31 3b 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69  1;.    if( !sqli
3216f 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
32170 70 49 6e 69 74 4d 75 74 65 78 20 29 7b 0a 20 20  pInitMutex ){.  
32171 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
32172 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74  lConfig.pInitMut
32173 65 78 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20  ex =.           
32174 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
32175 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  c(SQLITE_MUTEX_R
32176 45 43 55 52 53 49 56 45 29 3b 0a 20 20 20 20 20  ECURSIVE);.     
32177 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
32178 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75  alConfig.bCoreMu
32179 74 65 78 20 26 26 20 21 73 71 6c 69 74 65 33 47  tex && !sqlite3G
3217a 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69  lobalConfig.pIni
3217b 74 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 20 20  tMutex ){.      
3217c 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
3217d 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  MEM;.      }.   
3217e 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d   }.  }.  if( rc=
3217f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
32180 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
32181 6f 6e 66 69 67 2e 6e 52 65 66 49 6e 69 74 4d 75  onfig.nRefInitMu
32182 74 65 78 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c  tex++;.  }.  sql
32183 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
32184 28 70 4d 61 73 74 65 72 29 3b 0a 0a 20 20 2f 2a  (pMaster);..  /*
32185 20 49 66 20 72 63 20 69 73 20 6e 6f 74 20 53 51   If rc is not SQ
32186 4c 49 54 45 5f 4f 4b 20 61 74 20 74 68 69 73 20  LITE_OK at this 
32187 70 6f 69 6e 74 2c 20 74 68 65 6e 20 65 69 74 68  point, then eith
32188 65 72 20 74 68 65 20 6d 61 6c 6c 6f 63 0a 20 20  er the malloc.  
32189 2a 2a 20 73 75 62 73 79 73 74 65 6d 20 63 6f 75  ** subsystem cou
3218a 6c 64 20 6e 6f 74 20 62 65 20 69 6e 69 74 69 61  ld not be initia
3218b 6c 69 7a 65 64 20 6f 72 20 74 68 65 20 73 79 73  lized or the sys
3218c 74 65 6d 20 66 61 69 6c 65 64 20 74 6f 20 61 6c  tem failed to al
3218d 6c 6f 63 61 74 65 0a 20 20 2a 2a 20 74 68 65 20  locate.  ** the 
3218e 70 49 6e 69 74 4d 75 74 65 78 20 6d 75 74 65 78  pInitMutex mutex
3218f 2e 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  . Return an erro
32190 72 20 69 6e 20 65 69 74 68 65 72 20 63 61 73 65  r in either case
32191 2e 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d  .  */.  if( rc!=
32192 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
32193 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
32194 0a 20 20 2f 2a 20 44 6f 20 74 68 65 20 72 65 73  .  /* Do the res
32195 74 20 6f 66 20 74 68 65 20 69 6e 69 74 69 61 6c  t of the initial
32196 69 7a 61 74 69 6f 6e 20 75 6e 64 65 72 20 74 68  ization under th
32197 65 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  e recursive mute
32198 78 20 73 6f 0a 20 20 2a 2a 20 74 68 61 74 20 77  x so.  ** that w
32199 65 20 77 69 6c 6c 20 62 65 20 61 62 6c 65 20 74  e will be able t
3219a 6f 20 68 61 6e 64 6c 65 20 72 65 63 75 72 73 69  o handle recursi
3219b 76 65 20 63 61 6c 6c 73 20 69 6e 74 6f 0a 20 20  ve calls into.  
3219c 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  ** sqlite3_initi
3219d 61 6c 69 7a 65 28 29 2e 20 20 54 68 65 20 72 65  alize().  The re
3219e 63 75 72 73 69 76 65 20 63 61 6c 6c 73 20 6e 6f  cursive calls no
3219f 72 6d 61 6c 6c 79 20 63 6f 6d 65 20 74 68 72 6f  rmally come thro
321a0 75 67 68 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33  ugh.  ** sqlite3
321a1 5f 6f 73 5f 69 6e 69 74 28 29 20 77 68 65 6e 20  _os_init() when 
321a2 69 74 20 69 6e 76 6f 6b 65 73 20 73 71 6c 69 74  it invokes sqlit
321a3 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28  e3_vfs_register(
321a4 29 2c 20 62 75 74 20 6f 74 68 65 72 0a 20 20 2a  ), but other.  *
321a5 2a 20 72 65 63 75 72 73 69 76 65 20 63 61 6c 6c  * recursive call
321a6 73 20 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20  s might also be 
321a7 70 6f 73 73 69 62 6c 65 2e 0a 20 20 2a 2f 0a 20  possible..  */. 
321a8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
321a9 6e 74 65 72 28 73 71 6c 69 74 65 33 47 6c 6f 62  nter(sqlite3Glob
321aa 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75  alConfig.pInitMu
321ab 74 65 78 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  tex);.  if( sqli
321ac 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
321ad 69 73 49 6e 69 74 3d 3d 30 20 26 26 20 73 71 6c  isInit==0 && sql
321ae 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
321af 2e 69 6e 50 72 6f 67 72 65 73 73 3d 3d 30 20 29  .inProgress==0 )
321b0 7b 0a 20 20 20 20 46 75 6e 63 44 65 66 48 61 73  {.    FuncDefHas
321b1 68 20 2a 70 48 61 73 68 20 3d 20 26 47 4c 4f 42  h *pHash = &GLOB
321b2 41 4c 28 46 75 6e 63 44 65 66 48 61 73 68 2c 20  AL(FuncDefHash, 
321b3 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e  sqlite3GlobalFun
321b4 63 74 69 6f 6e 73 29 3b 0a 20 20 20 20 73 71 6c  ctions);.    sql
321b5 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
321b6 2e 69 6e 50 72 6f 67 72 65 73 73 20 3d 20 31 3b  .inProgress = 1;
321b7 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 48 61 73  .    memset(pHas
321b8 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 71 6c  h, 0, sizeof(sql
321b9 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69  ite3GlobalFuncti
321ba 6f 6e 73 29 29 3b 0a 20 20 20 20 73 71 6c 69 74  ons));.    sqlit
321bb 65 33 52 65 67 69 73 74 65 72 47 6c 6f 62 61 6c  e3RegisterGlobal
321bc 46 75 6e 63 74 69 6f 6e 73 28 29 3b 0a 20 20 20  Functions();.   
321bd 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
321be 61 6c 43 6f 6e 66 69 67 2e 69 73 50 43 61 63 68  alConfig.isPCach
321bf 65 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20  eInit==0 ){.    
321c0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 63    rc = sqlite3Pc
321c1 61 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 29  acheInitialize()
321c2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
321c3 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
321c4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
321c5 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 50 43 61  obalConfig.isPCa
321c6 63 68 65 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20  cheInit = 1;.   
321c7 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
321c8 73 49 6e 69 74 28 29 3b 0a 20 20 20 20 7d 0a 20  sInit();.    }. 
321c9 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
321ca 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71  E_OK ){.      sq
321cb 6c 69 74 65 33 50 43 61 63 68 65 42 75 66 66 65  lite3PCacheBuffe
321cc 72 53 65 74 75 70 28 20 73 71 6c 69 74 65 33 47  rSetup( sqlite3G
321cd 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67  lobalConfig.pPag
321ce 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 73 71  e, .          sq
321cf 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
321d0 67 2e 73 7a 50 61 67 65 2c 20 73 71 6c 69 74 65  g.szPage, sqlite
321d1 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50  3GlobalConfig.nP
321d2 61 67 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  age);.      sqli
321d3 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
321d4 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20  isInit = 1;.    
321d5 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  }.    sqlite3Glo
321d6 62 61 6c 43 6f 6e 66 69 67 2e 69 6e 50 72 6f 67  balConfig.inProg
321d7 72 65 73 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  ress = 0;.  }.  
321d8 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
321d9 61 76 65 28 73 71 6c 69 74 65 33 47 6c 6f 62 61  ave(sqlite3Globa
321da 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74  lConfig.pInitMut
321db 65 78 29 3b 0a 0a 20 20 2f 2a 20 47 6f 20 62 61  ex);..  /* Go ba
321dc 63 6b 20 75 6e 64 65 72 20 74 68 65 20 73 74 61  ck under the sta
321dd 74 69 63 20 6d 75 74 65 78 20 61 6e 64 20 63 6c  tic mutex and cl
321de 65 61 6e 20 75 70 20 74 68 65 20 72 65 63 75 72  ean up the recur
321df 73 69 76 65 0a 20 20 2a 2a 20 6d 75 74 65 78 20  sive.  ** mutex 
321e0 74 6f 20 70 72 65 76 65 6e 74 20 61 20 72 65 73  to prevent a res
321e1 6f 75 72 63 65 20 6c 65 61 6b 2e 0a 20 20 2a 2f  ource leak..  */
321e2 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
321e3 5f 65 6e 74 65 72 28 70 4d 61 73 74 65 72 29 3b  _enter(pMaster);
321e4 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  .  sqlite3Global
321e5 43 6f 6e 66 69 67 2e 6e 52 65 66 49 6e 69 74 4d  Config.nRefInitM
321e6 75 74 65 78 2d 2d 3b 0a 20 20 69 66 28 20 73 71  utex--;.  if( sq
321e7 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
321e8 67 2e 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 3c  g.nRefInitMutex<
321e9 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  =0 ){.    assert
321ea 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
321eb 6f 6e 66 69 67 2e 6e 52 65 66 49 6e 69 74 4d 75  onfig.nRefInitMu
321ec 74 65 78 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71  tex==0 );.    sq
321ed 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65  lite3_mutex_free
321ee 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  (sqlite3GlobalCo
321ef 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78 29  nfig.pInitMutex)
321f0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  ;.    sqlite3Glo
321f1 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d  balConfig.pInitM
321f2 75 74 65 78 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  utex = 0;.  }.  
321f3 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
321f4 61 76 65 28 70 4d 61 73 74 65 72 29 3b 0a 0a 20  ave(pMaster);.. 
321f5 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   /* The followin
321f6 67 20 69 73 20 6a 75 73 74 20 61 20 73 61 6e 69  g is just a sani
321f7 74 79 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65  ty check to make
321f8 20 73 75 72 65 20 53 51 4c 69 74 65 20 68 61 73   sure SQLite has
321f9 0a 20 20 2a 2a 20 62 65 65 6e 20 63 6f 6d 70 69  .  ** been compi
321fa 6c 65 64 20 63 6f 72 72 65 63 74 6c 79 2e 20 20  led correctly.  
321fb 49 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20  It is important 
321fc 74 6f 20 72 75 6e 20 74 68 69 73 20 63 6f 64 65  to run this code
321fd 2c 20 62 75 74 0a 20 20 2a 2a 20 77 65 20 64 6f  , but.  ** we do
321fe 6e 27 74 20 77 61 6e 74 20 74 6f 20 72 75 6e 20  n't want to run 
321ff 69 74 20 74 6f 6f 20 6f 66 74 65 6e 20 61 6e 64  it too often and
32200 20 73 6f 61 6b 20 75 70 20 43 50 55 20 63 79 63   soak up CPU cyc
32201 6c 65 73 20 66 6f 72 20 6e 6f 0a 20 20 2a 2a 20  les for no.  ** 
32202 72 65 61 73 6f 6e 2e 20 20 53 6f 20 77 65 20 72  reason.  So we r
32203 75 6e 20 69 74 20 6f 6e 63 65 20 64 75 72 69 6e  un it once durin
32204 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  g initialization
32205 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 4e  ..  */.#ifndef N
32206 44 45 42 55 47 0a 23 69 66 6e 64 65 66 20 53 51  DEBUG.#ifndef SQ
32207 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
32208 4e 47 5f 50 4f 49 4e 54 0a 20 20 2f 2a 20 54 68  NG_POINT.  /* Th
32209 69 73 20 73 65 63 74 69 6f 6e 20 6f 66 20 63 6f  is section of co
3220a 64 65 27 73 20 6f 6e 6c 79 20 22 6f 75 74 70 75  de's only "outpu
3220b 74 22 20 69 73 20 76 69 61 20 61 73 73 65 72 74  t" is via assert
3220c 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 2a  () statements. *
3220d 2f 0a 20 20 69 66 20 28 20 72 63 3d 3d 53 51 4c  /.  if ( rc==SQL
3220e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 36  ITE_OK ){.    u6
3220f 34 20 78 20 3d 20 28 28 28 75 36 34 29 31 29 3c  4 x = (((u64)1)<
32210 3c 36 33 29 2d 31 3b 0a 20 20 20 20 64 6f 75 62  <63)-1;.    doub
32211 6c 65 20 79 3b 0a 20 20 20 20 61 73 73 65 72 74  le y;.    assert
32212 28 73 69 7a 65 6f 66 28 78 29 3d 3d 38 29 3b 0a  (sizeof(x)==8);.
32213 20 20 20 20 61 73 73 65 72 74 28 73 69 7a 65 6f      assert(sizeo
32214 66 28 78 29 3d 3d 73 69 7a 65 6f 66 28 79 29 29  f(x)==sizeof(y))
32215 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 79 2c  ;.    memcpy(&y,
32216 20 26 78 2c 20 38 29 3b 0a 20 20 20 20 61 73 73   &x, 8);.    ass
32217 65 72 74 28 20 73 71 6c 69 74 65 33 49 73 4e 61  ert( sqlite3IsNa
32218 4e 28 79 29 20 29 3b 0a 20 20 7d 0a 23 65 6e 64  N(y) );.  }.#end
32219 69 66 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74  if.#endif..  ret
3221a 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
3221b 20 55 6e 64 6f 20 74 68 65 20 65 66 66 65 63 74   Undo the effect
3221c 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6e 69  s of sqlite3_ini
3221d 74 69 61 6c 69 7a 65 28 29 2e 20 20 4d 75 73 74  tialize().  Must
3221e 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 77   not be called w
3221f 68 69 6c 65 0a 2a 2a 20 74 68 65 72 65 20 61 72  hile.** there ar
32220 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 64 61  e outstanding da
32221 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
32222 6e 73 20 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c  ns or memory all
32223 6f 63 61 74 69 6f 6e 73 20 6f 72 0a 2a 2a 20 77  ocations or.** w
32224 68 69 6c 65 20 61 6e 79 20 70 61 72 74 20 6f 66  hile any part of
32225 20 53 51 4c 69 74 65 20 69 73 20 6f 74 68 65 72   SQLite is other
32226 77 69 73 65 20 69 6e 20 75 73 65 20 69 6e 20 61  wise in use in a
32227 6e 79 20 74 68 72 65 61 64 2e 20 20 54 68 69 73  ny thread.  This
32228 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 6e  .** routine is n
32229 6f 74 20 74 68 72 65 61 64 73 61 66 65 2e 20 20  ot threadsafe.  
3222a 42 75 74 20 69 74 20 69 73 20 73 61 66 65 20 74  But it is safe t
3222b 6f 20 69 6e 76 6f 6b 65 20 74 68 69 73 20 72 6f  o invoke this ro
3222c 75 74 69 6e 65 0a 2a 2a 20 6f 6e 20 77 68 65 6e  utine.** on when
3222d 20 53 51 4c 69 74 65 20 69 73 20 61 6c 72 65 61   SQLite is alrea
3222e 64 79 20 73 68 75 74 20 64 6f 77 6e 2e 20 20 49  dy shut down.  I
3222f 66 20 53 51 4c 69 74 65 20 69 73 20 61 6c 72 65  f SQLite is alre
32230 61 64 79 20 73 68 75 74 20 64 6f 77 6e 0a 2a 2a  ady shut down.**
32231 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   when this routi
32232 6e 65 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20 74  ne is invoked, t
32233 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
32234 20 69 73 20 61 20 68 61 72 6d 6c 65 73 73 20 6e   is a harmless n
32235 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  o-op..*/.SQLITE_
32236 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
32237 73 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 7b 0a  shutdown(void){.
32238 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
32239 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49 6e 69 74  balConfig.isInit
3223a 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
3223b 6f 73 5f 65 6e 64 28 29 3b 0a 20 20 20 20 73 71  os_end();.    sq
3223c 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75 74 6f  lite3_reset_auto
3223d 5f 65 78 74 65 6e 73 69 6f 6e 28 29 3b 0a 20 20  _extension();.  
3223e 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
3223f 6f 6e 66 69 67 2e 69 73 49 6e 69 74 20 3d 20 30  onfig.isInit = 0
32240 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69  ;.  }.  if( sqli
32241 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
32242 69 73 50 43 61 63 68 65 49 6e 69 74 20 29 7b 0a  isPCacheInit ){.
32243 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68      sqlite3Pcach
32244 65 53 68 75 74 64 6f 77 6e 28 29 3b 0a 20 20 20  eShutdown();.   
32245 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
32246 6e 66 69 67 2e 69 73 50 43 61 63 68 65 49 6e 69  nfig.isPCacheIni
32247 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  t = 0;.  }.  if(
32248 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
32249 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f 63 49 6e 69  nfig.isMallocIni
3224a 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
3224b 4d 61 6c 6c 6f 63 45 6e 64 28 29 3b 0a 20 20 20  MallocEnd();.   
3224c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
3224d 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f 63 49 6e 69  nfig.isMallocIni
3224e 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  t = 0;.  }.  if(
3224f 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
32250 6e 66 69 67 2e 69 73 4d 75 74 65 78 49 6e 69 74  nfig.isMutexInit
32251 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d   ){.    sqlite3M
32252 75 74 65 78 45 6e 64 28 29 3b 0a 20 20 20 20 73  utexEnd();.    s
32253 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
32254 69 67 2e 69 73 4d 75 74 65 78 49 6e 69 74 20 3d  ig.isMutexInit =
32255 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72   0;.  }..  retur
32256 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
32257 2f 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 61  /*.** This API a
32258 6c 6c 6f 77 73 20 61 70 70 6c 69 63 61 74 69 6f  llows applicatio
32259 6e 73 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65  ns to modify the
3225a 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72   global configur
3225b 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20  ation of.** the 
3225c 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 61  SQLite library a
3225d 74 20 72 75 6e 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a  t run-time..**.*
3225e 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
3225f 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61  hould only be ca
32260 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 72 65 20  lled when there 
32261 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69  are no outstandi
32262 6e 67 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  ng.** database c
32263 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f 72 20 6d 65  onnections or me
32264 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  mory allocations
32265 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
32266 69 73 20 6e 6f 74 0a 2a 2a 20 74 68 72 65 61 64  is not.** thread
32267 73 61 66 65 2e 20 20 46 61 69 6c 75 72 65 20 74  safe.  Failure t
32268 6f 20 68 65 65 64 20 74 68 65 73 65 20 77 61 72  o heed these war
32269 6e 69 6e 67 73 20 63 61 6e 20 6c 65 61 64 20 74  nings can lead t
3226a 6f 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 0a  o unpredictable.
3226b 2a 2a 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a  ** behavior..*/.
3226c 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3226d 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 69 6e  qlite3_config(in
3226e 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  t op, ...){.  va
3226f 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 6e 74 20  _list ap;.  int 
32270 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
32271 0a 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63 6f  .  /* sqlite3_co
32272 6e 66 69 67 28 29 20 73 68 61 6c 6c 20 72 65 74  nfig() shall ret
32273 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
32274 45 20 69 66 20 69 74 20 69 73 20 69 6e 76 6f 6b  E if it is invok
32275 65 64 20 77 68 69 6c 65 0a 20 20 2a 2a 20 74 68  ed while.  ** th
32276 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79  e SQLite library
32277 20 69 73 20 69 6e 20 75 73 65 2e 20 2a 2f 0a 20   is in use. */. 
32278 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
32279 61 6c 43 6f 6e 66 69 67 2e 69 73 49 6e 69 74 20  alConfig.isInit 
3227a 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
3227b 4d 49 53 55 53 45 3b 0a 0a 20 20 76 61 5f 73 74  MISUSE;..  va_st
3227c 61 72 74 28 61 70 2c 20 6f 70 29 3b 0a 20 20 73  art(ap, op);.  s
3227d 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 0a 20 20  witch( op ){..  
3227e 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 66 69    /* Mutex confi
3227f 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73  guration options
32280 20 61 72 65 20 6f 6e 6c 79 20 61 76 61 69 6c 61   are only availa
32281 62 6c 65 20 69 6e 20 61 20 74 68 72 65 61 64 73  ble in a threads
32282 61 66 65 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 69  afe.    ** compi
32283 6c 65 2e 20 0a 20 20 20 20 2a 2f 0a 23 69 66 20  le. .    */.#if 
32284 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
32285 45 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  E.    case SQLIT
32286 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54  E_CONFIG_SINGLET
32287 48 52 45 41 44 3a 20 7b 0a 20 20 20 20 20 20 2f  HREAD: {.      /
32288 2a 20 44 69 73 61 62 6c 65 20 61 6c 6c 20 6d 75  * Disable all mu
32289 74 65 78 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20  texing */.      
3228a 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3228b 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 3d  fig.bCoreMutex =
3228c 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   0;.      sqlite
3228d 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 46  3GlobalConfig.bF
3228e 75 6c 6c 4d 75 74 65 78 20 3d 20 30 3b 0a 20 20  ullMutex = 0;.  
3228f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
32290 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
32291 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52  _CONFIG_MULTITHR
32292 45 41 44 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  EAD: {.      /* 
32293 44 69 73 61 62 6c 65 20 6d 75 74 65 78 69 6e 67  Disable mutexing
32294 20 6f 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e   of database con
32295 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 20 20  nections */.    
32296 20 20 2f 2a 20 45 6e 61 62 6c 65 20 6d 75 74 65    /* Enable mute
32297 78 69 6e 67 20 6f 66 20 63 6f 72 65 20 64 61 74  xing of core dat
32298 61 20 73 74 72 75 63 74 75 72 65 73 20 2a 2f 0a  a structures */.
32299 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
3229a 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d  balConfig.bCoreM
3229b 75 74 65 78 20 3d 20 31 3b 0a 20 20 20 20 20 20  utex = 1;.      
3229c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3229d 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65 78 20 3d  fig.bFullMutex =
3229e 20 30 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   0;.      break;
3229f 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
322a0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 45  SQLITE_CONFIG_SE
322a1 52 49 41 4c 49 5a 45 44 3a 20 7b 0a 20 20 20 20  RIALIZED: {.    
322a2 20 20 2f 2a 20 45 6e 61 62 6c 65 20 61 6c 6c 20    /* Enable all 
322a3 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20 20 20  mutexing */.    
322a4 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
322a5 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78  onfig.bCoreMutex
322a6 20 3d 20 31 3b 0a 20 20 20 20 20 20 73 71 6c 69   = 1;.      sqli
322a7 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
322a8 62 46 75 6c 6c 4d 75 74 65 78 20 3d 20 31 3b 0a  bFullMutex = 1;.
322a9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
322aa 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49   }.    case SQLI
322ab 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 3a  TE_CONFIG_MUTEX:
322ac 20 7b 0a 20 20 20 20 20 20 2f 2a 20 53 70 65 63   {.      /* Spec
322ad 69 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 69  ify an alternati
322ae 76 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65  ve mutex impleme
322af 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  ntation */.     
322b0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
322b1 6e 66 69 67 2e 6d 75 74 65 78 20 3d 20 2a 76 61  nfig.mutex = *va
322b2 5f 61 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33  _arg(ap, sqlite3
322b3 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 2a 29  _mutex_methods*)
322b4 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
322b5 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51     }.    case SQ
322b6 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d  LITE_CONFIG_GETM
322b7 55 54 45 58 3a 20 7b 0a 20 20 20 20 20 20 2f 2a  UTEX: {.      /*
322b8 20 52 65 74 72 69 65 76 65 20 74 68 65 20 63 75   Retrieve the cu
322b9 72 72 65 6e 74 20 6d 75 74 65 78 20 69 6d 70 6c  rrent mutex impl
322ba 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20  ementation */.  
322bb 20 20 20 20 2a 76 61 5f 61 72 67 28 61 70 2c 20      *va_arg(ap, 
322bc 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
322bd 74 68 6f 64 73 2a 29 20 3d 20 73 71 6c 69 74 65  thods*) = sqlite
322be 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75  3GlobalConfig.mu
322bf 74 65 78 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  tex;.      break
322c0 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  ;.    }.#endif..
322c1 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
322c2 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 3a 20  _CONFIG_MALLOC: 
322c3 7b 0a 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69  {.      /* Speci
322c4 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76  fy an alternativ
322c5 65 20 6d 61 6c 6c 6f 63 20 69 6d 70 6c 65 6d 65  e malloc impleme
322c6 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  ntation */.     
322c7 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
322c8 6e 66 69 67 2e 6d 20 3d 20 2a 76 61 5f 61 72 67  nfig.m = *va_arg
322c9 28 61 70 2c 20 73 71 6c 69 74 65 33 5f 6d 65 6d  (ap, sqlite3_mem
322ca 5f 6d 65 74 68 6f 64 73 2a 29 3b 0a 20 20 20 20  _methods*);.    
322cb 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
322cc 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43     case SQLITE_C
322cd 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c 4f 43 3a  ONFIG_GETMALLOC:
322ce 20 7b 0a 20 20 20 20 20 20 2f 2a 20 52 65 74 72   {.      /* Retr
322cf 69 65 76 65 20 74 68 65 20 63 75 72 72 65 6e 74  ieve the current
322d0 20 6d 61 6c 6c 6f 63 28 29 20 69 6d 70 6c 65 6d   malloc() implem
322d1 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20  entation */.    
322d2 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
322d3 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c  balConfig.m.xMal
322d4 6c 6f 63 3d 3d 30 20 29 20 73 71 6c 69 74 65 33  loc==0 ) sqlite3
322d5 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 29 3b  MemSetDefault();
322d6 0a 20 20 20 20 20 20 2a 76 61 5f 61 72 67 28 61  .      *va_arg(a
322d7 70 2c 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  p, sqlite3_mem_m
322d8 65 74 68 6f 64 73 2a 29 20 3d 20 73 71 6c 69 74  ethods*) = sqlit
322d9 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
322da 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
322db 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51     }.    case SQ
322dc 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53  LITE_CONFIG_MEMS
322dd 54 41 54 55 53 3a 20 7b 0a 20 20 20 20 20 20 2f  TATUS: {.      /
322de 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64 69 73 61  * Enable or disa
322df 62 6c 65 20 74 68 65 20 6d 61 6c 6c 6f 63 20 73  ble the malloc s
322e0 74 61 74 75 73 20 63 6f 6c 6c 65 63 74 69 6f 6e  tatus collection
322e1 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
322e2 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d  3GlobalConfig.bM
322e3 65 6d 73 74 61 74 20 3d 20 76 61 5f 61 72 67 28  emstat = va_arg(
322e4 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ap, int);.      
322e5 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
322e6 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e   case SQLITE_CON
322e7 46 49 47 5f 53 43 52 41 54 43 48 3a 20 7b 0a 20  FIG_SCRATCH: {. 
322e8 20 20 20 20 20 2f 2a 20 44 65 73 69 67 6e 61 74       /* Designat
322e9 65 20 61 20 62 75 66 66 65 72 20 66 6f 72 20 73  e a buffer for s
322ea 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 73 70  cratch memory sp
322eb 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c  ace */.      sql
322ec 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
322ed 2e 70 53 63 72 61 74 63 68 20 3d 20 76 61 5f 61  .pScratch = va_a
322ee 72 67 28 61 70 2c 20 76 6f 69 64 2a 29 3b 0a 20  rg(ap, void*);. 
322ef 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
322f0 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74  alConfig.szScrat
322f1 63 68 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  ch = va_arg(ap, 
322f2 69 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  int);.      sqli
322f3 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
322f4 6e 53 63 72 61 74 63 68 20 3d 20 76 61 5f 61 72  nScratch = va_ar
322f5 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20  g(ap, int);.    
322f6 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
322f7 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43     case SQLITE_C
322f8 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 3a  ONFIG_PAGECACHE:
322f9 20 7b 0a 20 20 20 20 20 20 2f 2a 20 44 65 73 69   {.      /* Desi
322fa 67 6e 61 74 65 20 61 20 62 75 66 66 65 72 20 66  gnate a buffer f
322fb 6f 72 20 70 61 67 65 20 63 61 63 68 65 20 6d 65  or page cache me
322fc 6d 6f 72 79 20 73 70 61 63 65 20 2a 2f 0a 20 20  mory space */.  
322fd 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
322fe 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 3d 20  lConfig.pPage = 
322ff 76 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 2a  va_arg(ap, void*
32300 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
32301 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50  GlobalConfig.szP
32302 61 67 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  age = va_arg(ap,
32303 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c   int);.      sql
32304 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
32305 2e 6e 50 61 67 65 20 3d 20 76 61 5f 61 72 67 28  .nPage = va_arg(
32306 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ap, int);.      
32307 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
32308 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f    case SQLITE_CO
32309 4e 46 49 47 5f 50 43 41 43 48 45 3a 20 7b 0a 20  NFIG_PCACHE: {. 
3230a 20 20 20 20 20 2f 2a 20 53 70 65 63 69 66 79 20       /* Specify 
3230b 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20 70  an alternative p
3230c 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d  age cache implem
3230d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20  entation */.    
3230e 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
3230f 6f 6e 66 69 67 2e 70 63 61 63 68 65 20 3d 20 2a  onfig.pcache = *
32310 76 61 5f 61 72 67 28 61 70 2c 20 73 71 6c 69 74  va_arg(ap, sqlit
32311 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64  e3_pcache_method
32312 73 2a 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  s*);.      break
32313 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 73  ;.    }..    cas
32314 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
32315 47 45 54 50 43 41 43 48 45 3a 20 7b 0a 20 20 20  GETPCACHE: {.   
32316 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c     if( sqlite3Gl
32317 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68  obalConfig.pcach
32318 65 2e 78 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 20  e.xInit==0 ){.  
32319 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 43 61        sqlite3PCa
3231a 63 68 65 53 65 74 44 65 66 61 75 6c 74 28 29 3b  cheSetDefault();
3231b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a  .      }.      *
3231c 76 61 5f 61 72 67 28 61 70 2c 20 73 71 6c 69 74  va_arg(ap, sqlit
3231d 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64  e3_pcache_method
3231e 73 2a 29 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f  s*) = sqlite3Glo
3231f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65  balConfig.pcache
32320 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
32321 20 20 20 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65     }..#if define
32322 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
32323 4d 45 4d 53 59 53 33 29 20 7c 7c 20 64 65 66 69  MEMSYS3) || defi
32324 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
32325 45 5f 4d 45 4d 53 59 53 35 29 0a 20 20 20 20 63  E_MEMSYS5).    c
32326 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ase SQLITE_CONFI
32327 47 5f 48 45 41 50 3a 20 7b 0a 20 20 20 20 20 20  G_HEAP: {.      
32328 2f 2a 20 44 65 73 69 67 6e 61 74 65 20 61 20 62  /* Designate a b
32329 75 66 66 65 72 20 66 6f 72 20 68 65 61 70 20 6d  uffer for heap m
3232a 65 6d 6f 72 79 20 73 70 61 63 65 20 2a 2f 0a 20  emory space */. 
3232b 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
3232c 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 20 3d  alConfig.pHeap =
3232d 20 76 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64   va_arg(ap, void
3232e 2a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  *);.      sqlite
3232f 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48  3GlobalConfig.nH
32330 65 61 70 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  eap = va_arg(ap,
32331 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c   int);.      sql
32332 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
32333 2e 6d 6e 52 65 71 20 3d 20 76 61 5f 61 72 67 28  .mnReq = va_arg(
32334 61 70 2c 20 69 6e 74 29 3b 0a 0a 20 20 20 20 20  ap, int);..     
32335 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
32336 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 3d 3d  alConfig.pHeap==
32337 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  0 ){.        /* 
32338 49 66 20 74 68 65 20 68 65 61 70 20 70 6f 69 6e  If the heap poin
32339 74 65 72 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  ter is NULL, the
3233a 6e 20 72 65 73 74 6f 72 65 20 74 68 65 20 6d 61  n restore the ma
3233b 6c 6c 6f 63 20 69 6d 70 6c 65 6d 65 6e 74 61 74  lloc implementat
3233c 69 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 62  ion.        ** b
3233d 61 63 6b 20 74 6f 20 4e 55 4c 4c 20 70 6f 69 6e  ack to NULL poin
3233e 74 65 72 73 20 74 6f 6f 2e 20 20 54 68 69 73 20  ters too.  This 
3233f 77 69 6c 6c 20 63 61 75 73 65 20 74 68 65 20 6d  will cause the m
32340 61 6c 6c 6f 63 20 74 6f 20 67 6f 0a 20 20 20 20  alloc to go.    
32341 20 20 20 20 2a 2a 20 62 61 63 6b 20 74 6f 20 69      ** back to i
32342 74 73 20 64 65 66 61 75 6c 74 20 69 6d 70 6c 65  ts default imple
32343 6d 65 6e 74 61 74 69 6f 6e 20 77 68 65 6e 20 73  mentation when s
32344 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
32345 65 28 29 20 69 73 0a 20 20 20 20 20 20 20 20 2a  e() is.        *
32346 2a 20 72 75 6e 2e 0a 20 20 20 20 20 20 20 20 2a  * run..        *
32347 2f 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74  /.        memset
32348 28 26 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  (&sqlite3GlobalC
32349 6f 6e 66 69 67 2e 6d 2c 20 30 2c 20 73 69 7a 65  onfig.m, 0, size
3234a 6f 66 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  of(sqlite3Global
3234b 43 6f 6e 66 69 67 2e 6d 29 29 3b 0a 20 20 20 20  Config.m));.    
3234c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
3234d 20 2f 2a 20 54 68 65 20 68 65 61 70 20 70 6f 69   /* The heap poi
3234e 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  nter is not NULL
3234f 2c 20 74 68 65 6e 20 69 6e 73 74 61 6c 6c 20 6f  , then install o
32350 6e 65 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20  ne of the.      
32351 20 20 2a 2a 20 6d 65 6d 35 2e 63 2f 6d 65 6d 33    ** mem5.c/mem3
32352 2e 63 20 6d 65 74 68 6f 64 73 2e 20 49 66 20 6e  .c methods. If n
32353 65 69 74 68 65 72 20 45 4e 41 42 4c 45 5f 4d 45  either ENABLE_ME
32354 4d 53 59 53 33 20 6e 6f 72 0a 20 20 20 20 20 20  MSYS3 nor.      
32355 20 20 2a 2a 20 45 4e 41 42 4c 45 5f 4d 45 4d 53    ** ENABLE_MEMS
32356 59 53 35 20 69 73 20 64 65 66 69 6e 65 64 2c 20  YS5 is defined, 
32357 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e  return an error.
32358 0a 20 20 20 20 20 20 20 20 2a 2f 0a 23 69 66 64  .        */.#ifd
32359 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
3235a 5f 4d 45 4d 53 59 53 33 0a 20 20 20 20 20 20 20  _MEMSYS3.       
3235b 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
3235c 6e 66 69 67 2e 6d 20 3d 20 2a 73 71 6c 69 74 65  nfig.m = *sqlite
3235d 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 33 28 29  3MemGetMemsys3()
3235e 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  ;.#endif.#ifdef 
3235f 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
32360 4d 53 59 53 35 0a 20 20 20 20 20 20 20 20 73 71  MSYS5.        sq
32361 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
32362 67 2e 6d 20 3d 20 2a 73 71 6c 69 74 65 33 4d 65  g.m = *sqlite3Me
32363 6d 47 65 74 4d 65 6d 73 79 73 35 28 29 3b 0a 23  mGetMemsys5();.#
32364 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 0a 20 20  endif.      }.  
32365 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
32366 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 63 61 73  .#endif..    cas
32367 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
32368 4c 4f 4f 4b 41 53 49 44 45 3a 20 7b 0a 20 20 20  LOOKASIDE: {.   
32369 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
3236a 43 6f 6e 66 69 67 2e 73 7a 4c 6f 6f 6b 61 73 69  Config.szLookasi
3236b 64 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  de = va_arg(ap, 
3236c 69 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  int);.      sqli
3236d 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
3236e 6e 4c 6f 6f 6b 61 73 69 64 65 20 3d 20 76 61 5f  nLookaside = va_
3236f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20  arg(ap, int);.  
32370 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
32371 0a 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  ..    default: {
32372 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
32373 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20  TE_ERROR;.      
32374 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
32375 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20  .  va_end(ap);. 
32376 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
32377 2a 0a 2a 2a 20 53 65 74 20 75 70 20 74 68 65 20  *.** Set up the 
32378 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72  lookaside buffer
32379 73 20 66 6f 72 20 61 20 64 61 74 61 62 61 73 65  s for a database
3237a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 20   connection..** 
3237b 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
3237c 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 0a 2a   on success.  .*
3237d 2a 20 49 66 20 6c 6f 6f 6b 61 73 69 64 65 20 69  * If lookaside i
3237e 73 20 61 6c 72 65 61 64 79 20 61 63 74 69 76 65  s already active
3237f 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
32380 42 55 53 59 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  BUSY..**.** The 
32381 73 7a 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  sz parameter is 
32382 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
32383 74 65 73 20 69 6e 20 65 61 63 68 20 6c 6f 6f 6b  tes in each look
32384 61 73 69 64 65 20 73 6c 6f 74 2e 0a 2a 2a 20 54  aside slot..** T
32385 68 65 20 63 6e 74 20 70 61 72 61 6d 65 74 65 72  he cnt parameter
32386 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
32387 66 20 73 6c 6f 74 73 2e 20 20 49 66 20 70 53 74  f slots.  If pSt
32388 61 72 74 20 69 73 20 4e 55 4c 4c 20 74 68 65 0a  art is NULL the.
32389 2a 2a 20 73 70 61 63 65 20 66 6f 72 20 74 68 65  ** space for the
3238a 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72   lookaside memor
3238b 79 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72  y is obtained fr
3238c 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  om sqlite3_mallo
3238d 63 28 29 2e 0a 2a 2a 20 49 66 20 70 53 74 61 72  c()..** If pStar
3238e 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68  t is not NULL th
3238f 65 6e 20 69 74 20 69 73 20 73 7a 2a 63 6e 74 20  en it is sz*cnt 
32390 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20  bytes of memory 
32391 74 6f 20 75 73 65 20 66 6f 72 0a 2a 2a 20 74 68  to use for.** th
32392 65 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f  e lookaside memo
32393 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ry..*/.static in
32394 74 20 73 65 74 75 70 4c 6f 6f 6b 61 73 69 64 65  t setupLookaside
32395 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f  (sqlite3 *db, vo
32396 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 73 7a  id *pBuf, int sz
32397 2c 20 69 6e 74 20 63 6e 74 29 7b 0a 20 20 76 6f  , int cnt){.  vo
32398 69 64 20 2a 70 53 74 61 72 74 3b 0a 20 20 69 66  id *pStart;.  if
32399 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  ( db->lookaside.
3239a 6e 4f 75 74 20 29 7b 0a 20 20 20 20 72 65 74 75  nOut ){.    retu
3239b 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a  rn SQLITE_BUSY;.
3239c 20 20 7d 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e    }.  /* Free an
3239d 79 20 65 78 69 73 74 69 6e 67 20 6c 6f 6f 6b 61  y existing looka
3239e 73 69 64 65 20 62 75 66 66 65 72 20 66 6f 72 20  side buffer for 
3239f 74 68 69 73 20 68 61 6e 64 6c 65 20 62 65 66 6f  this handle befo
323a0 72 65 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 69  re.  ** allocati
323a1 6e 67 20 61 20 6e 65 77 20 6f 6e 65 20 73 6f 20  ng a new one so 
323a2 77 65 20 64 6f 6e 27 74 20 68 61 76 65 20 74 6f  we don't have to
323a3 20 68 61 76 65 20 73 70 61 63 65 20 66 6f 72 20   have space for 
323a4 0a 20 20 2a 2a 20 62 6f 74 68 20 61 74 20 74 68  .  ** both at th
323a5 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 20 20 2a  e same time..  *
323a6 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b  /.  if( db->look
323a7 61 73 69 64 65 2e 62 4d 61 6c 6c 6f 63 65 64 20  aside.bMalloced 
323a8 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
323a9 72 65 65 28 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  ree(db->lookasid
323aa 65 2e 70 53 74 61 72 74 29 3b 0a 20 20 7d 0a 20  e.pStart);.  }. 
323ab 20 2f 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20   /* The size of 
323ac 61 20 6c 6f 6f 6b 61 73 69 64 65 20 73 6c 6f 74  a lookaside slot
323ad 20 6e 65 65 64 73 20 74 6f 20 62 65 20 6c 61 72   needs to be lar
323ae 67 65 72 20 74 68 61 6e 20 61 20 70 6f 69 6e 74  ger than a point
323af 65 72 0a 20 20 2a 2a 20 74 6f 20 62 65 20 75 73  er.  ** to be us
323b0 65 66 75 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  eful..  */.  if(
323b1 20 73 7a 3c 3d 28 69 6e 74 29 73 69 7a 65 6f 66   sz<=(int)sizeof
323b2 28 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 2a 29  (LookasideSlot*)
323b3 20 29 20 73 7a 20 3d 20 30 3b 0a 20 20 69 66 28   ) sz = 0;.  if(
323b4 20 63 6e 74 3c 30 20 29 20 63 6e 74 20 3d 20 30   cnt<0 ) cnt = 0
323b5 3b 0a 20 20 69 66 28 20 73 7a 3d 3d 30 20 7c 7c  ;.  if( sz==0 ||
323b6 20 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73   cnt==0 ){.    s
323b7 7a 20 3d 20 30 3b 0a 20 20 20 20 70 53 74 61 72  z = 0;.    pStar
323b8 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69  t = 0;.  }else i
323b9 66 28 20 70 42 75 66 3d 3d 30 20 29 7b 0a 20 20  f( pBuf==0 ){.  
323ba 20 20 73 7a 20 3d 20 52 4f 55 4e 44 38 28 73 7a    sz = ROUND8(sz
323bb 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65  );.    sqlite3Be
323bc 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
323bd 29 3b 0a 20 20 20 20 70 53 74 61 72 74 20 3d 20  );.    pStart = 
323be 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 73  sqlite3Malloc( s
323bf 7a 2a 63 6e 74 20 29 3b 0a 20 20 20 20 73 71 6c  z*cnt );.    sql
323c0 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
323c1 6c 6f 63 28 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  loc();.  }else{.
323c2 20 20 20 20 73 7a 20 3d 20 52 4f 55 4e 44 44 4f      sz = ROUNDDO
323c3 57 4e 38 28 73 7a 29 3b 0a 20 20 20 20 70 53 74  WN8(sz);.    pSt
323c4 61 72 74 20 3d 20 70 42 75 66 3b 0a 20 20 7d 0a  art = pBuf;.  }.
323c5 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
323c6 70 53 74 61 72 74 20 3d 20 70 53 74 61 72 74 3b  pStart = pStart;
323c7 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65  .  db->lookaside
323c8 2e 70 46 72 65 65 20 3d 20 30 3b 0a 20 20 64 62  .pFree = 0;.  db
323c9 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 20 3d  ->lookaside.sz =
323ca 20 28 75 31 36 29 73 7a 3b 0a 20 20 69 66 28 20   (u16)sz;.  if( 
323cb 70 53 74 61 72 74 20 29 7b 0a 20 20 20 20 69 6e  pStart ){.    in
323cc 74 20 69 3b 0a 20 20 20 20 4c 6f 6f 6b 61 73 69  t i;.    Lookasi
323cd 64 65 53 6c 6f 74 20 2a 70 3b 0a 20 20 20 20 61  deSlot *p;.    a
323ce 73 73 65 72 74 28 20 73 7a 20 3e 20 28 69 6e 74  ssert( sz > (int
323cf 29 73 69 7a 65 6f 66 28 4c 6f 6f 6b 61 73 69 64  )sizeof(Lookasid
323d0 65 53 6c 6f 74 2a 29 20 29 3b 0a 20 20 20 20 70  eSlot*) );.    p
323d1 20 3d 20 28 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f   = (LookasideSlo
323d2 74 2a 29 70 53 74 61 72 74 3b 0a 20 20 20 20 66  t*)pStart;.    f
323d3 6f 72 28 69 3d 63 6e 74 2d 31 3b 20 69 3e 3d 30  or(i=cnt-1; i>=0
323d4 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 20 20 70 2d  ; i--){.      p-
323d5 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e 6c 6f 6f  >pNext = db->loo
323d6 6b 61 73 69 64 65 2e 70 46 72 65 65 3b 0a 20 20  kaside.pFree;.  
323d7 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
323d8 65 2e 70 46 72 65 65 20 3d 20 70 3b 0a 20 20 20  e.pFree = p;.   
323d9 20 20 20 70 20 3d 20 28 4c 6f 6f 6b 61 73 69 64     p = (Lookasid
323da 65 53 6c 6f 74 2a 29 26 28 28 75 38 2a 29 70 29  eSlot*)&((u8*)p)
323db 5b 73 7a 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  [sz];.    }.    
323dc 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45  db->lookaside.pE
323dd 6e 64 20 3d 20 70 3b 0a 20 20 20 20 64 62 2d 3e  nd = p;.    db->
323de 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c  lookaside.bEnabl
323df 65 64 20 3d 20 31 3b 0a 20 20 20 20 64 62 2d 3e  ed = 1;.    db->
323e0 6c 6f 6f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c 6f  lookaside.bMallo
323e1 63 65 64 20 3d 20 70 42 75 66 3d 3d 30 20 3f 31  ced = pBuf==0 ?1
323e2 3a 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  :0;.  }else{.   
323e3 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70   db->lookaside.p
323e4 45 6e 64 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d  End = 0;.    db-
323e5 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62  >lookaside.bEnab
323e6 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d  led = 0;.    db-
323e7 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c  >lookaside.bMall
323e8 6f 63 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  oced = 0;.  }.  
323e9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
323ea 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
323eb 6e 20 74 68 65 20 6d 75 74 65 78 20 61 73 73 6f  n the mutex asso
323ec 63 69 61 74 65 64 20 77 69 74 68 20 61 20 64 61  ciated with a da
323ed 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
323ee 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  n..*/.SQLITE_API
323ef 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
323f0 73 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78  sqlite3_db_mutex
323f1 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
323f2 20 72 65 74 75 72 6e 20 64 62 2d 3e 6d 75 74 65   return db->mute
323f3 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 66  x;.}../*.** Conf
323f4 69 67 75 72 61 74 69 6f 6e 20 73 65 74 74 69 6e  iguration settin
323f5 67 73 20 66 6f 72 20 61 6e 20 69 6e 64 69 76 69  gs for an indivi
323f6 64 75 61 6c 20 64 61 74 61 62 61 73 65 20 63 6f  dual database co
323f7 6e 6e 65 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49  nnection.*/.SQLI
323f8 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
323f9 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c  e3_db_config(sql
323fa 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f 70  ite3 *db, int op
323fb 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73  , ...){.  va_lis
323fc 74 20 61 70 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  t ap;.  int rc;.
323fd 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 6f    va_start(ap, o
323fe 70 29 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70  p);.  switch( op
323ff 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c   ){.    case SQL
32400 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f  ITE_DBCONFIG_LOO
32401 4b 41 53 49 44 45 3a 20 7b 0a 20 20 20 20 20 20  KASIDE: {.      
32402 76 6f 69 64 20 2a 70 42 75 66 20 3d 20 76 61 5f  void *pBuf = va_
32403 61 72 67 28 61 70 2c 20 76 6f 69 64 2a 29 3b 0a  arg(ap, void*);.
32404 20 20 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 76        int sz = v
32405 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a  a_arg(ap, int);.
32406 20 20 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20        int cnt = 
32407 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b  va_arg(ap, int);
32408 0a 20 20 20 20 20 20 72 63 20 3d 20 73 65 74 75  .      rc = setu
32409 70 4c 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70  pLookaside(db, p
3240a 42 75 66 2c 20 73 7a 2c 20 63 6e 74 29 3b 0a 20  Buf, sz, cnt);. 
3240b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3240c 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  }.    default: {
3240d 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
3240e 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20  TE_ERROR;.      
3240f 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
32410 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20  .  va_end(ap);. 
32411 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a   return rc;.}...
32412 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  /*.** Return tru
32413 65 20 69 66 20 74 68 65 20 62 75 66 66 65 72 20  e if the buffer 
32414 7a 5b 30 2e 2e 6e 2d 31 5d 20 63 6f 6e 74 61 69  z[0..n-1] contai
32415 6e 73 20 61 6c 6c 20 73 70 61 63 65 73 2e 0a 2a  ns all spaces..*
32416 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c  /.static int all
32417 53 70 61 63 65 73 28 63 6f 6e 73 74 20 63 68 61  Spaces(const cha
32418 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20  r *z, int n){.  
32419 77 68 69 6c 65 28 20 6e 3e 30 20 26 26 20 7a 5b  while( n>0 && z[
3241a 6e 2d 31 5d 3d 3d 27 20 27 20 29 7b 20 6e 2d 2d  n-1]==' ' ){ n--
3241b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3d 3d  ; }.  return n==
3241c 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  0;.}../*.** This
3241d 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 20   is the default 
3241e 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69  collating functi
3241f 6f 6e 20 6e 61 6d 65 64 20 22 42 49 4e 41 52 59  on named "BINARY
32420 22 20 77 68 69 63 68 20 69 73 20 61 6c 77 61 79  " which is alway
32421 73 0a 2a 2a 20 61 76 61 69 6c 61 62 6c 65 2e 0a  s.** available..
32422 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 64  **.** If the pad
32423 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 20 69 73  Flag argument is
32424 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 73   not NULL then s
32425 70 61 63 65 20 70 61 64 64 69 6e 67 20 61 74 20  pace padding at 
32426 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20 73 74  the end.** of st
32427 72 69 6e 67 73 20 69 73 20 69 67 6e 6f 72 65 64  rings is ignored
32428 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e  .  This implemen
32429 74 73 20 74 68 65 20 52 54 52 49 4d 20 63 6f 6c  ts the RTRIM col
3242a 6c 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  lation..*/.stati
3242b 63 20 69 6e 74 20 62 69 6e 43 6f 6c 6c 46 75 6e  c int binCollFun
3242c 63 28 0a 20 20 76 6f 69 64 20 2a 70 61 64 46 6c  c(.  void *padFl
3242d 61 67 2c 0a 20 20 69 6e 74 20 6e 4b 65 79 31 2c  ag,.  int nKey1,
3242e 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
3242f 79 31 2c 0a 20 20 69 6e 74 20 6e 4b 65 79 32 2c  y1,.  int nKey2,
32430 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
32431 79 32 0a 29 7b 0a 20 20 69 6e 74 20 72 63 2c 20  y2.){.  int rc, 
32432 6e 3b 0a 20 20 6e 20 3d 20 6e 4b 65 79 31 3c 6e  n;.  n = nKey1<n
32433 4b 65 79 32 20 3f 20 6e 4b 65 79 31 20 3a 20 6e  Key2 ? nKey1 : n
32434 4b 65 79 32 3b 0a 20 20 72 63 20 3d 20 6d 65 6d  Key2;.  rc = mem
32435 63 6d 70 28 70 4b 65 79 31 2c 20 70 4b 65 79 32  cmp(pKey1, pKey2
32436 2c 20 6e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  , n);.  if( rc==
32437 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 61 64  0 ){.    if( pad
32438 46 6c 61 67 0a 20 20 20 20 20 26 26 20 61 6c 6c  Flag.     && all
32439 53 70 61 63 65 73 28 28 28 63 68 61 72 2a 29 70  Spaces(((char*)p
3243a 4b 65 79 31 29 2b 6e 2c 20 6e 4b 65 79 31 2d 6e  Key1)+n, nKey1-n
3243b 29 0a 20 20 20 20 20 26 26 20 61 6c 6c 53 70 61  ).     && allSpa
3243c 63 65 73 28 28 28 63 68 61 72 2a 29 70 4b 65 79  ces(((char*)pKey
3243d 32 29 2b 6e 2c 20 6e 4b 65 79 32 2d 6e 29 0a 20  2)+n, nKey2-n). 
3243e 20 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4c     ){.      /* L
3243f 65 61 76 65 20 72 63 20 75 6e 63 68 61 6e 67 65  eave rc unchange
32440 64 20 61 74 20 30 20 2a 2f 0a 20 20 20 20 7d 65  d at 0 */.    }e
32441 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
32442 6e 4b 65 79 31 20 2d 20 6e 4b 65 79 32 3b 0a 20  nKey1 - nKey2;. 
32443 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
32444 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  n rc;.}../*.** A
32445 6e 6f 74 68 65 72 20 62 75 69 6c 74 2d 69 6e 20  nother built-in 
32446 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
32447 63 65 3a 20 4e 4f 43 41 53 45 2e 20 0a 2a 2a 0a  ce: NOCASE. .**.
32448 2a 2a 20 54 68 69 73 20 63 6f 6c 6c 61 74 69 6e  ** This collatin
32449 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 69 6e  g sequence is in
3244a 74 65 6e 64 65 64 20 74 6f 20 62 65 20 75 73 65  tended to be use
3244b 64 20 66 6f 72 20 22 63 61 73 65 20 69 6e 64 65  d for "case inde
3244c 70 65 6e 64 61 6e 74 0a 2a 2a 20 63 6f 6d 70 61  pendant.** compa
3244d 72 69 73 6f 6e 22 2e 20 53 51 4c 69 74 65 27 73  rison". SQLite's
3244e 20 6b 6e 6f 77 6c 65 64 67 65 20 6f 66 20 75 70   knowledge of up
3244f 70 65 72 20 61 6e 64 20 6c 6f 77 65 72 20 63 61  per and lower ca
32450 73 65 20 65 71 75 69 76 61 6c 65 6e 74 73 0a 2a  se equivalents.*
32451 2a 20 65 78 74 65 6e 64 73 20 6f 6e 6c 79 20 74  * extends only t
32452 6f 20 74 68 65 20 32 36 20 63 68 61 72 61 63 74  o the 26 charact
32453 65 72 73 20 75 73 65 64 20 69 6e 20 74 68 65 20  ers used in the 
32454 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65  English language
32455 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68 65 20 6d  ..**.** At the m
32456 6f 6d 65 6e 74 20 74 68 65 72 65 20 69 73 20 6f  oment there is o
32457 6e 6c 79 20 61 20 55 54 46 2d 38 20 69 6d 70 6c  nly a UTF-8 impl
32458 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 73  ementation..*/.s
32459 74 61 74 69 63 20 69 6e 74 20 6e 6f 63 61 73 65  tatic int nocase
3245a 43 6f 6c 6c 61 74 69 6e 67 46 75 6e 63 28 0a 20  CollatingFunc(. 
3245b 20 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 2c 0a   void *NotUsed,.
3245c 20 20 69 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e    int nKey1, con
3245d 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 0a  st void *pKey1,.
3245e 20 20 69 6e 74 20 6e 4b 65 79 32 2c 20 63 6f 6e    int nKey2, con
3245f 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32 0a 29  st void *pKey2.)
32460 7b 0a 20 20 69 6e 74 20 72 20 3d 20 73 71 6c 69  {.  int r = sqli
32461 74 65 33 53 74 72 4e 49 43 6d 70 28 0a 20 20 20  te3StrNICmp(.   
32462 20 20 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a     (const char *
32463 29 70 4b 65 79 31 2c 20 28 63 6f 6e 73 74 20 63  )pKey1, (const c
32464 68 61 72 20 2a 29 70 4b 65 79 32 2c 20 28 6e 4b  har *)pKey2, (nK
32465 65 79 31 3c 6e 4b 65 79 32 29 3f 6e 4b 65 79 31  ey1<nKey2)?nKey1
32466 3a 6e 4b 65 79 32 29 3b 0a 20 20 55 4e 55 53 45  :nKey2);.  UNUSE
32467 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
32468 73 65 64 29 3b 0a 20 20 69 66 28 20 30 3d 3d 72  sed);.  if( 0==r
32469 20 29 7b 0a 20 20 20 20 72 20 3d 20 6e 4b 65 79   ){.    r = nKey
3246a 31 2d 6e 4b 65 79 32 3b 0a 20 20 7d 0a 20 20 72  1-nKey2;.  }.  r
3246b 65 74 75 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn r;.}../*.*
3246c 2a 20 52 65 74 75 72 6e 20 74 68 65 20 52 4f 57  * Return the ROW
3246d 49 44 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72  ID of the most r
3246e 65 63 65 6e 74 20 69 6e 73 65 72 74 0a 2a 2f 0a  ecent insert.*/.
3246f 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
32470 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f  e_int64 sqlite3_
32471 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
32472 64 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  d(sqlite3 *db){.
32473 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 6c 61 73    return db->las
32474 74 52 6f 77 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  tRowid;.}../*.**
32475 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
32476 65 72 20 6f 66 20 63 68 61 6e 67 65 73 20 69 6e  er of changes in
32477 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
32478 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
32479 5f 65 78 65 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49  _exec()..*/.SQLI
3247a 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
3247b 65 33 5f 63 68 61 6e 67 65 73 28 73 71 6c 69 74  e3_changes(sqlit
3247c 65 33 20 2a 64 62 29 7b 0a 20 20 72 65 74 75 72  e3 *db){.  retur
3247d 6e 20 64 62 2d 3e 6e 43 68 61 6e 67 65 3b 0a 7d  n db->nChange;.}
3247e 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
3247f 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61  he number of cha
32480 6e 67 65 73 20 73 69 6e 63 65 20 74 68 65 20 64  nges since the d
32481 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 77  atabase handle w
32482 61 73 20 6f 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51  as opened..*/.SQ
32483 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
32484 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67  ite3_total_chang
32485 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  es(sqlite3 *db){
32486 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 6e 54  .  return db->nT
32487 6f 74 61 6c 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f  otalChange;.}../
32488 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 6f  *.** Close all o
32489 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e 20  pen savepoints. 
3248a 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6f 6e  This function on
3248b 6c 79 20 6d 61 6e 69 70 75 6c 61 74 65 73 20 66  ly manipulates f
3248c 69 65 6c 64 73 20 6f 66 20 74 68 65 0a 2a 2a 20  ields of the.** 
3248d 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
3248e 6f 62 6a 65 63 74 2c 20 69 74 20 64 6f 65 73 20  object, it does 
3248f 6e 6f 74 20 63 6c 6f 73 65 20 61 6e 79 20 73 61  not close any sa
32490 76 65 70 6f 69 6e 74 73 20 74 68 61 74 20 6d 61  vepoints that ma
32491 79 20 62 65 20 6f 70 65 6e 0a 2a 2a 20 61 74 20  y be open.** at 
32492 74 68 65 20 62 2d 74 72 65 65 2f 70 61 67 65 72  the b-tree/pager
32493 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 53 51 4c 49 54   level..*/.SQLIT
32494 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
32495 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65 70  qlite3CloseSavep
32496 6f 69 6e 74 73 28 73 71 6c 69 74 65 33 20 2a 64  oints(sqlite3 *d
32497 62 29 7b 0a 20 20 77 68 69 6c 65 28 20 64 62 2d  b){.  while( db-
32498 3e 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20  >pSavepoint ){. 
32499 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 54     Savepoint *pT
3249a 6d 70 20 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f  mp = db->pSavepo
3249b 69 6e 74 3b 0a 20 20 20 20 64 62 2d 3e 70 53 61  int;.    db->pSa
3249c 76 65 70 6f 69 6e 74 20 3d 20 70 54 6d 70 2d 3e  vepoint = pTmp->
3249d 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74  pNext;.    sqlit
3249e 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 6d  e3DbFree(db, pTm
3249f 70 29 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 6e 53  p);.  }.  db->nS
324a0 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20  avepoint = 0;.  
324a1 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 20 3d  db->nStatement =
324a2 20 30 3b 0a 20 20 64 62 2d 3e 69 73 54 72 61 6e   0;.  db->isTran
324a3 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74  sactionSavepoint
324a4 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43   = 0;.}../*.** C
324a5 6c 6f 73 65 20 61 6e 20 65 78 69 73 74 69 6e 67  lose an existing
324a6 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65   SQLite database
324a7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
324a8 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65  nt sqlite3_close
324a9 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
324aa 20 48 61 73 68 45 6c 65 6d 20 2a 69 3b 0a 20 20   HashElem *i;.  
324ab 69 6e 74 20 6a 3b 0a 0a 20 20 69 66 28 20 21 64  int j;..  if( !d
324ac 62 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  b ){.    return 
324ad 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
324ae 20 69 66 28 20 21 73 71 6c 69 74 65 33 53 61 66   if( !sqlite3Saf
324af 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b  etyCheckSickOrOk
324b0 28 64 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75  (db) ){.    retu
324b1 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
324b2 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
324b3 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
324b4 6d 75 74 65 78 29 3b 0a 0a 20 20 73 71 6c 69 74  mutex);..  sqlit
324b5 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53  e3ResetInternalS
324b6 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 0a 20  chema(db, 0);.. 
324b7 20 2f 2a 20 49 66 20 61 20 74 72 61 6e 73 61 63   /* If a transac
324b8 74 69 6f 6e 20 69 73 20 6f 70 65 6e 2c 20 74 68  tion is open, th
324b9 65 20 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53  e ResetInternalS
324ba 63 68 65 6d 61 28 29 20 63 61 6c 6c 20 61 62 6f  chema() call abo
324bb 76 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74  ve.  ** will not
324bc 20 68 61 76 65 20 63 61 6c 6c 65 64 20 74 68 65   have called the
324bd 20 78 44 69 73 63 6f 6e 6e 65 63 74 28 29 20 6d   xDisconnect() m
324be 65 74 68 6f 64 20 6f 6e 20 61 6e 79 20 76 69 72  ethod on any vir
324bf 74 75 61 6c 0a 20 20 2a 2a 20 74 61 62 6c 65 73  tual.  ** tables
324c0 20 69 6e 20 74 68 65 20 64 62 2d 3e 61 56 54 72   in the db->aVTr
324c1 61 6e 73 5b 5d 20 61 72 72 61 79 2e 20 54 68 65  ans[] array. The
324c2 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 71 6c 69 74   following sqlit
324c3 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 29  e3VtabRollback()
324c4 0a 20 20 2a 2a 20 63 61 6c 6c 20 77 69 6c 6c 20  .  ** call will 
324c5 64 6f 20 73 6f 2e 20 57 65 20 6e 65 65 64 20 74  do so. We need t
324c6 6f 20 64 6f 20 74 68 69 73 20 62 65 66 6f 72 65  o do this before
324c7 20 74 68 65 20 63 68 65 63 6b 20 66 6f 72 20 61   the check for a
324c8 63 74 69 76 65 0a 20 20 2a 2a 20 53 51 4c 20 73  ctive.  ** SQL s
324c9 74 61 74 65 6d 65 6e 74 73 20 62 65 6c 6f 77 2c  tatements below,
324ca 20 61 73 20 74 68 65 20 76 2d 74 61 62 6c 65 20   as the v-table 
324cb 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d  implementation m
324cc 61 79 20 62 65 20 73 74 6f 72 69 6e 67 0a 20 20  ay be storing.  
324cd 2a 2a 20 73 6f 6d 65 20 70 72 65 70 61 72 65 64  ** some prepared
324ce 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 74 65   statements inte
324cf 72 6e 61 6c 6c 79 2e 0a 20 20 2a 2f 0a 20 20 73  rnally..  */.  s
324d0 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61  qlite3VtabRollba
324d1 63 6b 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 49 66  ck(db);..  /* If
324d2 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f   there are any o
324d3 75 74 73 74 61 6e 64 69 6e 67 20 56 4d 73 2c 20  utstanding VMs, 
324d4 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
324d5 53 59 2e 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d  SY. */.  if( db-
324d6 3e 70 56 64 62 65 20 29 7b 0a 20 20 20 20 73 71  >pVdbe ){.    sq
324d7 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53  lite3Error(db, S
324d8 51 4c 49 54 45 5f 42 55 53 59 2c 20 0a 20 20 20  QLITE_BUSY, .   
324d9 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20       "unable to 
324da 63 6c 6f 73 65 20 64 75 65 20 74 6f 20 75 6e 66  close due to unf
324db 69 6e 61 6c 69 73 65 64 20 73 74 61 74 65 6d 65  inalised stateme
324dc 6e 74 73 22 29 3b 0a 20 20 20 20 73 71 6c 69 74  nts");.    sqlit
324dd 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
324de 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72  b->mutex);.    r
324df 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
324e0 59 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  Y;.  }.  assert(
324e1 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68   sqlite3SafetyCh
324e2 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20  eckSickOrOk(db) 
324e3 29 3b 0a 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a  );..  for(j=0; j
324e4 3c 64 62 2d 3e 6e 44 62 3b 20 6a 2b 2b 29 7b 0a  <db->nDb; j++){.
324e5 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d      Btree *pBt =
324e6 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 42 74 3b   db->aDb[j].pBt;
324e7 0a 20 20 20 20 69 66 28 20 70 42 74 20 26 26 20  .    if( pBt && 
324e8 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e  sqlite3BtreeIsIn
324e9 42 61 63 6b 75 70 28 70 42 74 29 20 29 7b 0a 20  Backup(pBt) ){. 
324ea 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
324eb 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 42 55 53  r(db, SQLITE_BUS
324ec 59 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22 75  Y, .          "u
324ed 6e 61 62 6c 65 20 74 6f 20 63 6c 6f 73 65 20 64  nable to close d
324ee 75 65 20 74 6f 20 75 6e 66 69 6e 69 73 68 65 64  ue to unfinished
324ef 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f   backup operatio
324f0 6e 22 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  n");.      sqlit
324f1 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
324f2 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  b->mutex);.     
324f3 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
324f4 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  USY;.    }.  }..
324f5 20 20 2f 2a 20 46 72 65 65 20 61 6e 79 20 6f 75    /* Free any ou
324f6 74 73 74 61 6e 64 69 6e 67 20 53 61 76 65 70 6f  tstanding Savepo
324f7 69 6e 74 20 73 74 72 75 63 74 75 72 65 73 2e 20  int structures. 
324f8 2a 2f 0a 20 20 73 71 6c 69 74 65 33 43 6c 6f 73  */.  sqlite3Clos
324f9 65 53 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b  eSavepoints(db);
324fa 0a 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 64  ..  for(j=0; j<d
324fb 62 2d 3e 6e 44 62 3b 20 6a 2b 2b 29 7b 0a 20 20  b->nDb; j++){.  
324fc 20 20 73 74 72 75 63 74 20 44 62 20 2a 70 44 62    struct Db *pDb
324fd 20 3d 20 26 64 62 2d 3e 61 44 62 5b 6a 5d 3b 0a   = &db->aDb[j];.
324fe 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74      if( pDb->pBt
324ff 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
32500 33 42 74 72 65 65 43 6c 6f 73 65 28 70 44 62 2d  3BtreeClose(pDb-
32501 3e 70 42 74 29 3b 0a 20 20 20 20 20 20 70 44 62  >pBt);.      pDb
32502 2d 3e 70 42 74 20 3d 20 30 3b 0a 20 20 20 20 20  ->pBt = 0;.     
32503 20 69 66 28 20 6a 21 3d 31 20 29 7b 0a 20 20 20   if( j!=1 ){.   
32504 20 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d       pDb->pSchem
32505 61 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  a = 0;.      }. 
32506 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
32507 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53  e3ResetInternalS
32508 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 0a 20  chema(db, 0);.. 
32509 20 2f 2a 20 54 65 6c 6c 20 74 68 65 20 63 6f 64   /* Tell the cod
3250a 65 20 69 6e 20 6e 6f 74 69 66 79 2e 63 20 74 68  e in notify.c th
3250b 61 74 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f  at the connectio
3250c 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 68 6f 6c 64  n no longer hold
3250d 73 20 61 6e 79 0a 20 20 2a 2a 20 6c 6f 63 6b 73  s any.  ** locks
3250e 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 72 65   and does not re
3250f 71 75 69 72 65 20 61 6e 79 20 66 75 72 74 68 65  quire any furthe
32510 72 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20  r unlock-notify 
32511 63 61 6c 6c 62 61 63 6b 73 2e 0a 20 20 2a 2f 0a  callbacks..  */.
32512 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74    sqlite3Connect
32513 69 6f 6e 43 6c 6f 73 65 64 28 64 62 29 3b 0a 0a  ionClosed(db);..
32514 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 44    assert( db->nD
32515 62 3c 3d 32 20 29 3b 0a 20 20 61 73 73 65 72 74  b<=2 );.  assert
32516 28 20 64 62 2d 3e 61 44 62 3d 3d 64 62 2d 3e 61  ( db->aDb==db->a
32517 44 62 53 74 61 74 69 63 20 29 3b 0a 20 20 66 6f  DbStatic );.  fo
32518 72 28 6a 3d 30 3b 20 6a 3c 41 72 72 61 79 53 69  r(j=0; j<ArraySi
32519 7a 65 28 64 62 2d 3e 61 46 75 6e 63 2e 61 29 3b  ze(db->aFunc.a);
3251a 20 6a 2b 2b 29 7b 0a 20 20 20 20 46 75 6e 63 44   j++){.    FuncD
3251b 65 66 20 2a 70 4e 65 78 74 2c 20 2a 70 48 61 73  ef *pNext, *pHas
3251c 68 2c 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28 70  h, *p;.    for(p
3251d 3d 64 62 2d 3e 61 46 75 6e 63 2e 61 5b 6a 5d 3b  =db->aFunc.a[j];
3251e 20 70 3b 20 70 3d 70 48 61 73 68 29 7b 0a 20 20   p; p=pHash){.  
3251f 20 20 20 20 70 48 61 73 68 20 3d 20 70 2d 3e 70      pHash = p->p
32520 48 61 73 68 3b 0a 20 20 20 20 20 20 77 68 69 6c  Hash;.      whil
32521 65 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20  e( p ){.        
32522 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74  pNext = p->pNext
32523 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
32524 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a  3DbFree(db, p);.
32525 20 20 20 20 20 20 20 20 70 20 3d 20 70 4e 65 78          p = pNex
32526 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  t;.      }.    }
32527 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 73 71 6c  .  }.  for(i=sql
32528 69 74 65 48 61 73 68 46 69 72 73 74 28 26 64 62  iteHashFirst(&db
32529 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b 20 69 3b 20  ->aCollSeq); i; 
3252a 69 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  i=sqliteHashNext
3252b 28 69 29 29 7b 0a 20 20 20 20 43 6f 6c 6c 53 65  (i)){.    CollSe
3252c 71 20 2a 70 43 6f 6c 6c 20 3d 20 28 43 6f 6c 6c  q *pColl = (Coll
3252d 53 65 71 20 2a 29 73 71 6c 69 74 65 48 61 73 68  Seq *)sqliteHash
3252e 44 61 74 61 28 69 29 3b 0a 20 20 20 20 2f 2a 20  Data(i);.    /* 
3252f 49 6e 76 6f 6b 65 20 61 6e 79 20 64 65 73 74 72  Invoke any destr
32530 75 63 74 6f 72 73 20 72 65 67 69 73 74 65 72 65  uctors registere
32531 64 20 66 6f 72 20 63 6f 6c 6c 61 74 69 6f 6e 20  d for collation 
32532 73 65 71 75 65 6e 63 65 20 75 73 65 72 20 64 61  sequence user da
32533 74 61 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 6a  ta. */.    for(j
32534 3d 30 3b 20 6a 3c 33 3b 20 6a 2b 2b 29 7b 0a 20  =0; j<3; j++){. 
32535 20 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 5b 6a       if( pColl[j
32536 5d 2e 78 44 65 6c 20 29 7b 0a 20 20 20 20 20 20  ].xDel ){.      
32537 20 20 70 43 6f 6c 6c 5b 6a 5d 2e 78 44 65 6c 28    pColl[j].xDel(
32538 70 43 6f 6c 6c 5b 6a 5d 2e 70 55 73 65 72 29 3b  pColl[j].pUser);
32539 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
3253a 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
3253b 28 64 62 2c 20 70 43 6f 6c 6c 29 3b 0a 20 20 7d  (db, pColl);.  }
3253c 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c  .  sqlite3HashCl
3253d 65 61 72 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65  ear(&db->aCollSe
3253e 71 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  q);.#ifndef SQLI
3253f 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
32540 41 42 4c 45 0a 20 20 66 6f 72 28 69 3d 73 71 6c  ABLE.  for(i=sql
32541 69 74 65 48 61 73 68 46 69 72 73 74 28 26 64 62  iteHashFirst(&db
32542 2d 3e 61 4d 6f 64 75 6c 65 29 3b 20 69 3b 20 69  ->aModule); i; i
32543 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28  =sqliteHashNext(
32544 69 29 29 7b 0a 20 20 20 20 4d 6f 64 75 6c 65 20  i)){.    Module 
32545 2a 70 4d 6f 64 20 3d 20 28 4d 6f 64 75 6c 65 20  *pMod = (Module 
32546 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61  *)sqliteHashData
32547 28 69 29 3b 0a 20 20 20 20 69 66 28 20 70 4d 6f  (i);.    if( pMo
32548 64 2d 3e 78 44 65 73 74 72 6f 79 20 29 7b 0a 20  d->xDestroy ){. 
32549 20 20 20 20 20 70 4d 6f 64 2d 3e 78 44 65 73 74       pMod->xDest
3254a 72 6f 79 28 70 4d 6f 64 2d 3e 70 41 75 78 29 3b  roy(pMod->pAux);
3254b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
3254c 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4d 6f  e3DbFree(db, pMo
3254d 64 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  d);.  }.  sqlite
3254e 33 48 61 73 68 43 6c 65 61 72 28 26 64 62 2d 3e  3HashClear(&db->
3254f 61 4d 6f 64 75 6c 65 29 3b 0a 23 65 6e 64 69 66  aModule);.#endif
32550 0a 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72  ..  sqlite3Error
32551 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20  (db, SQLITE_OK, 
32552 30 29 3b 20 2f 2a 20 44 65 61 6c 6c 6f 63 61 74  0); /* Deallocat
32553 65 73 20 61 6e 79 20 63 61 63 68 65 64 20 65 72  es any cached er
32554 72 6f 72 20 73 74 72 69 6e 67 73 2e 20 2a 2f 0a  ror strings. */.
32555 20 20 69 66 28 20 64 62 2d 3e 70 45 72 72 20 29    if( db->pErr )
32556 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c  {.    sqlite3Val
32557 75 65 46 72 65 65 28 64 62 2d 3e 70 45 72 72 29  ueFree(db->pErr)
32558 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 43  ;.  }.  sqlite3C
32559 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 64  loseExtensions(d
3255a 62 29 3b 0a 0a 20 20 64 62 2d 3e 6d 61 67 69 63  b);..  db->magic
3255b 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f   = SQLITE_MAGIC_
3255c 45 52 52 4f 52 3b 0a 0a 20 20 2f 2a 20 54 68 65  ERROR;..  /* The
3255d 20 74 65 6d 70 2d 64 61 74 61 62 61 73 65 20 73   temp-database s
3255e 63 68 65 6d 61 20 69 73 20 61 6c 6c 6f 63 61 74  chema is allocat
3255f 65 64 20 64 69 66 66 65 72 65 6e 74 6c 79 20 66  ed differently f
32560 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 20 73 63  rom the other sc
32561 68 65 6d 61 0a 20 20 2a 2a 20 6f 62 6a 65 63 74  hema.  ** object
32562 73 20 28 75 73 69 6e 67 20 73 71 6c 69 74 65 4d  s (using sqliteM
32563 61 6c 6c 6f 63 28 29 20 64 69 72 65 63 74 6c 79  alloc() directly
32564 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 73 71 6c  , instead of sql
32565 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 28  ite3BtreeSchema(
32566 29 29 2e 0a 20 20 2a 2a 20 53 6f 20 69 74 20 6e  ))..  ** So it n
32567 65 65 64 73 20 74 6f 20 62 65 20 66 72 65 65 64  eeds to be freed
32568 20 68 65 72 65 2e 20 54 6f 64 6f 3a 20 57 68 79   here. Todo: Why
32569 20 6e 6f 74 20 72 6f 6c 6c 20 74 68 65 20 74 65   not roll the te
3256a 6d 70 20 73 63 68 65 6d 61 20 69 6e 74 6f 0a 20  mp schema into. 
3256b 20 2a 2a 20 74 68 65 20 73 61 6d 65 20 73 71 6c   ** the same sql
3256c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 61 73 20 74  iteMalloc() as t
3256d 68 65 20 6f 6e 65 20 74 68 61 74 20 61 6c 6c 6f  he one that allo
3256e 63 61 74 65 73 20 74 68 65 20 64 61 74 61 62 61  cates the databa
3256f 73 65 20 0a 20 20 2a 2a 20 73 74 72 75 63 74 75  se .  ** structu
32570 72 65 3f 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  re?.  */.  sqlit
32571 65 33 44 62 46 72 65 65 28 64 62 2c 20 64 62 2d  e3DbFree(db, db-
32572 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 29  >aDb[1].pSchema)
32573 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
32574 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
32575 78 29 3b 0a 20 20 64 62 2d 3e 6d 61 67 69 63 20  x);.  db->magic 
32576 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 43  = SQLITE_MAGIC_C
32577 4c 4f 53 45 44 3b 0a 20 20 73 71 6c 69 74 65 33  LOSED;.  sqlite3
32578 5f 6d 75 74 65 78 5f 66 72 65 65 28 64 62 2d 3e  _mutex_free(db->
32579 6d 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72 74  mutex);.  assert
3257a 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  ( db->lookaside.
3257b 6e 4f 75 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 46  nOut==0 );  /* F
3257c 61 69 6c 73 20 6f 6e 20 61 20 6c 6f 6f 6b 61 73  ails on a lookas
3257d 69 64 65 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20  ide memory leak 
3257e 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f  */.  if( db->loo
3257f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c 6f 63 65 64  kaside.bMalloced
32580 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
32581 66 72 65 65 28 64 62 2d 3e 6c 6f 6f 6b 61 73 69  free(db->lookasi
32582 64 65 2e 70 53 74 61 72 74 29 3b 0a 20 20 7d 0a  de.pStart);.  }.
32583 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 64    sqlite3_free(d
32584 62 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  b);.  return SQL
32585 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
32586 20 52 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 64 61   Rollback all da
32587 74 61 62 61 73 65 20 66 69 6c 65 73 2e 0a 2a 2f  tabase files..*/
32588 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
32589 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c  void sqlite3Roll
3258a 62 61 63 6b 41 6c 6c 28 73 71 6c 69 74 65 33 20  backAll(sqlite3 
3258b 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  *db){.  int i;. 
3258c 20 69 6e 74 20 69 6e 54 72 61 6e 73 20 3d 20 30   int inTrans = 0
3258d 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
3258e 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
3258f 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73  b->mutex) );.  s
32590 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
32591 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 66 6f 72  nMalloc();.  for
32592 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
32593 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 64   i++){.    if( d
32594 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 20 29 7b  b->aDb[i].pBt ){
32595 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
32596 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73  e3BtreeIsInTrans
32597 28 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29  (db->aDb[i].pBt)
32598 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 54 72   ){.        inTr
32599 61 6e 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  ans = 1;.      }
3259a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  .      sqlite3Bt
3259b 72 65 65 52 6f 6c 6c 62 61 63 6b 28 64 62 2d 3e  reeRollback(db->
3259c 61 44 62 5b 69 5d 2e 70 42 74 29 3b 0a 20 20 20  aDb[i].pBt);.   
3259d 20 20 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 69 6e     db->aDb[i].in
3259e 54 72 61 6e 73 20 3d 20 30 3b 0a 20 20 20 20 7d  Trans = 0;.    }
3259f 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 74  .  }.  sqlite3Vt
325a0 61 62 52 6f 6c 6c 62 61 63 6b 28 64 62 29 3b 0a  abRollback(db);.
325a1 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69    sqlite3EndBeni
325a2 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 69  gnMalloc();..  i
325a3 66 28 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  f( db->flags&SQL
325a4 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65  ITE_InternChange
325a5 73 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  s ){.    sqlite3
325a6 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74  ExpirePreparedSt
325a7 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20  atements(db);.  
325a8 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
325a9 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
325aa 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49   0);.  }..  /* I
325ab 66 20 6f 6e 65 20 68 61 73 20 62 65 65 6e 20 63  f one has been c
325ac 6f 6e 66 69 67 75 72 65 64 2c 20 69 6e 76 6f 6b  onfigured, invok
325ad 65 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2d 68  e the rollback-h
325ae 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a  ook callback */.
325af 20 20 69 66 28 20 64 62 2d 3e 78 52 6f 6c 6c 62    if( db->xRollb
325b0 61 63 6b 43 61 6c 6c 62 61 63 6b 20 26 26 20 28  ackCallback && (
325b1 69 6e 54 72 61 6e 73 20 7c 7c 20 21 64 62 2d 3e  inTrans || !db->
325b2 61 75 74 6f 43 6f 6d 6d 69 74 29 20 29 7b 0a 20  autoCommit) ){. 
325b3 20 20 20 64 62 2d 3e 78 52 6f 6c 6c 62 61 63 6b     db->xRollback
325b4 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 52 6f  Callback(db->pRo
325b5 6c 6c 62 61 63 6b 41 72 67 29 3b 0a 20 20 7d 0a  llbackArg);.  }.
325b6 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
325b7 61 20 73 74 61 74 69 63 20 73 74 72 69 6e 67 20  a static string 
325b8 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74  that describes t
325b9 68 65 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72  he kind of error
325ba 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68   specified in th
325bb 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a  e.** argument..*
325bc 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
325bd 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
325be 69 74 65 33 45 72 72 53 74 72 28 69 6e 74 20 72  ite3ErrStr(int r
325bf 63 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  c){.  static con
325c0 73 74 20 63 68 61 72 2a 20 63 6f 6e 73 74 20 61  st char* const a
325c1 4d 73 67 5b 5d 20 3d 20 7b 0a 20 20 20 20 2f 2a  Msg[] = {.    /*
325c2 20 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20 20 20   SQLITE_OK      
325c3 20 20 20 20 2a 2f 20 22 6e 6f 74 20 61 6e 20 65      */ "not an e
325c4 72 72 6f 72 22 2c 0a 20 20 20 20 2f 2a 20 53 51  rror",.    /* SQ
325c5 4c 49 54 45 5f 45 52 52 4f 52 20 20 20 20 20 20  LITE_ERROR      
325c6 20 2a 2f 20 22 53 51 4c 20 6c 6f 67 69 63 20 65   */ "SQL logic e
325c7 72 72 6f 72 20 6f 72 20 6d 69 73 73 69 6e 67 20  rror or missing 
325c8 64 61 74 61 62 61 73 65 22 2c 0a 20 20 20 20 2f  database",.    /
325c9 2a 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41  * SQLITE_INTERNA
325ca 4c 20 20 20 20 2a 2f 20 30 2c 0a 20 20 20 20 2f  L    */ 0,.    /
325cb 2a 20 53 51 4c 49 54 45 5f 50 45 52 4d 20 20 20  * SQLITE_PERM   
325cc 20 20 20 20 20 2a 2f 20 22 61 63 63 65 73 73 20       */ "access 
325cd 70 65 72 6d 69 73 73 69 6f 6e 20 64 65 6e 69 65  permission denie
325ce 64 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54  d",.    /* SQLIT
325cf 45 5f 41 42 4f 52 54 20 20 20 20 20 20 20 2a 2f  E_ABORT       */
325d0 20 22 63 61 6c 6c 62 61 63 6b 20 72 65 71 75 65   "callback reque
325d1 73 74 65 64 20 71 75 65 72 79 20 61 62 6f 72 74  sted query abort
325d2 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45  ",.    /* SQLITE
325d3 5f 42 55 53 59 20 20 20 20 20 20 20 20 2a 2f 20  _BUSY        */ 
325d4 22 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63  "database is loc
325d5 6b 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c  ked",.    /* SQL
325d6 49 54 45 5f 4c 4f 43 4b 45 44 20 20 20 20 20 20  ITE_LOCKED      
325d7 2a 2f 20 22 64 61 74 61 62 61 73 65 20 74 61 62  */ "database tab
325d8 6c 65 20 69 73 20 6c 6f 63 6b 65 64 22 2c 0a 20  le is locked",. 
325d9 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d     /* SQLITE_NOM
325da 45 4d 20 20 20 20 20 20 20 2a 2f 20 22 6f 75 74  EM       */ "out
325db 20 6f 66 20 6d 65 6d 6f 72 79 22 2c 0a 20 20 20   of memory",.   
325dc 20 2f 2a 20 53 51 4c 49 54 45 5f 52 45 41 44 4f   /* SQLITE_READO
325dd 4e 4c 59 20 20 20 20 2a 2f 20 22 61 74 74 65 6d  NLY    */ "attem
325de 70 74 20 74 6f 20 77 72 69 74 65 20 61 20 72 65  pt to write a re
325df 61 64 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 22  adonly database"
325e0 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
325e1 49 4e 54 45 52 52 55 50 54 20 20 20 2a 2f 20 22  INTERRUPT   */ "
325e2 69 6e 74 65 72 72 75 70 74 65 64 22 2c 0a 20 20  interrupted",.  
325e3 20 20 2f 2a 20 53 51 4c 49 54 45 5f 49 4f 45 52    /* SQLITE_IOER
325e4 52 20 20 20 20 20 20 20 2a 2f 20 22 64 69 73 6b  R       */ "disk
325e5 20 49 2f 4f 20 65 72 72 6f 72 22 2c 0a 20 20 20   I/O error",.   
325e6 20 2f 2a 20 53 51 4c 49 54 45 5f 43 4f 52 52 55   /* SQLITE_CORRU
325e7 50 54 20 20 20 20 20 2a 2f 20 22 64 61 74 61 62  PT     */ "datab
325e8 61 73 65 20 64 69 73 6b 20 69 6d 61 67 65 20 69  ase disk image i
325e9 73 20 6d 61 6c 66 6f 72 6d 65 64 22 2c 0a 20 20  s malformed",.  
325ea 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4e 4f 54 46    /* SQLITE_NOTF
325eb 4f 55 4e 44 20 20 20 20 2a 2f 20 30 2c 0a 20 20  OUND    */ 0,.  
325ec 20 20 2f 2a 20 53 51 4c 49 54 45 5f 46 55 4c 4c    /* SQLITE_FULL
325ed 20 20 20 20 20 20 20 20 2a 2f 20 22 64 61 74 61          */ "data
325ee 62 61 73 65 20 6f 72 20 64 69 73 6b 20 69 73 20  base or disk is 
325ef 66 75 6c 6c 22 2c 0a 20 20 20 20 2f 2a 20 53 51  full",.    /* SQ
325f0 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 20 20 20  LITE_CANTOPEN   
325f1 20 2a 2f 20 22 75 6e 61 62 6c 65 20 74 6f 20 6f   */ "unable to o
325f2 70 65 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c  pen database fil
325f3 65 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54  e",.    /* SQLIT
325f4 45 5f 50 52 4f 54 4f 43 4f 4c 20 20 20 20 2a 2f  E_PROTOCOL    */
325f5 20 30 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54   0,.    /* SQLIT
325f6 45 5f 45 4d 50 54 59 20 20 20 20 20 20 20 2a 2f  E_EMPTY       */
325f7 20 22 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73   "table contains
325f8 20 6e 6f 20 64 61 74 61 22 2c 0a 20 20 20 20 2f   no data",.    /
325f9 2a 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20  * SQLITE_SCHEMA 
325fa 20 20 20 20 20 2a 2f 20 22 64 61 74 61 62 61 73       */ "databas
325fb 65 20 73 63 68 65 6d 61 20 68 61 73 20 63 68 61  e schema has cha
325fc 6e 67 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53 51  nged",.    /* SQ
325fd 4c 49 54 45 5f 54 4f 4f 42 49 47 20 20 20 20 20  LITE_TOOBIG     
325fe 20 2a 2f 20 22 73 74 72 69 6e 67 20 6f 72 20 62   */ "string or b
325ff 6c 6f 62 20 74 6f 6f 20 62 69 67 22 2c 0a 20 20  lob too big",.  
32600 20 20 2f 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 53    /* SQLITE_CONS
32601 54 52 41 49 4e 54 20 20 2a 2f 20 22 63 6f 6e 73  TRAINT  */ "cons
32602 74 72 61 69 6e 74 20 66 61 69 6c 65 64 22 2c 0a  traint failed",.
32603 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 49      /* SQLITE_MI
32604 53 4d 41 54 43 48 20 20 20 20 2a 2f 20 22 64 61  SMATCH    */ "da
32605 74 61 74 79 70 65 20 6d 69 73 6d 61 74 63 68 22  tatype mismatch"
32606 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
32607 4d 49 53 55 53 45 20 20 20 20 20 20 2a 2f 20 22  MISUSE      */ "
32608 6c 69 62 72 61 72 79 20 72 6f 75 74 69 6e 65 20  library routine 
32609 63 61 6c 6c 65 64 20 6f 75 74 20 6f 66 20 73 65  called out of se
3260a 71 75 65 6e 63 65 22 2c 0a 20 20 20 20 2f 2a 20  quence",.    /* 
3260b 53 51 4c 49 54 45 5f 4e 4f 4c 46 53 20 20 20 20  SQLITE_NOLFS    
3260c 20 20 20 2a 2f 20 22 6c 61 72 67 65 20 66 69 6c     */ "large fil
3260d 65 20 73 75 70 70 6f 72 74 20 69 73 20 64 69 73  e support is dis
3260e 61 62 6c 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53  abled",.    /* S
3260f 51 4c 49 54 45 5f 41 55 54 48 20 20 20 20 20 20  QLITE_AUTH      
32610 20 20 2a 2f 20 22 61 75 74 68 6f 72 69 7a 61 74    */ "authorizat
32611 69 6f 6e 20 64 65 6e 69 65 64 22 2c 0a 20 20 20  ion denied",.   
32612 20 2f 2a 20 53 51 4c 49 54 45 5f 46 4f 52 4d 41   /* SQLITE_FORMA
32613 54 20 20 20 20 20 20 2a 2f 20 22 61 75 78 69 6c  T      */ "auxil
32614 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66 6f  iary database fo
32615 72 6d 61 74 20 65 72 72 6f 72 22 2c 0a 20 20 20  rmat error",.   
32616 20 2f 2a 20 53 51 4c 49 54 45 5f 52 41 4e 47 45   /* SQLITE_RANGE
32617 20 20 20 20 20 20 20 2a 2f 20 22 62 69 6e 64 20         */ "bind 
32618 6f 72 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20  or column index 
32619 6f 75 74 20 6f 66 20 72 61 6e 67 65 22 2c 0a 20  out of range",. 
3261a 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4e 4f 54     /* SQLITE_NOT
3261b 41 44 42 20 20 20 20 20 20 2a 2f 20 22 66 69 6c  ADB      */ "fil
3261c 65 20 69 73 20 65 6e 63 72 79 70 74 65 64 20 6f  e is encrypted o
3261d 72 20 69 73 20 6e 6f 74 20 61 20 64 61 74 61 62  r is not a datab
3261e 61 73 65 22 2c 0a 20 20 7d 3b 0a 20 20 72 63 20  ase",.  };.  rc 
3261f 26 3d 20 30 78 66 66 3b 0a 20 20 69 66 28 20 41  &= 0xff;.  if( A
32620 4c 57 41 59 53 28 72 63 3e 3d 30 29 20 26 26 20  LWAYS(rc>=0) && 
32621 72 63 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66 28  rc<(int)(sizeof(
32622 61 4d 73 67 29 2f 73 69 7a 65 6f 66 28 61 4d 73  aMsg)/sizeof(aMs
32623 67 5b 30 5d 29 29 20 26 26 20 61 4d 73 67 5b 72  g[0])) && aMsg[r
32624 63 5d 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  c]!=0 ){.    ret
32625 75 72 6e 20 61 4d 73 67 5b 72 63 5d 3b 0a 20 20  urn aMsg[rc];.  
32626 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
32627 6e 20 22 75 6e 6b 6e 6f 77 6e 20 65 72 72 6f 72  n "unknown error
32628 22 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ";.  }.}../*.** 
32629 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70  This routine imp
3262a 6c 65 6d 65 6e 74 73 20 61 20 62 75 73 79 20 63  lements a busy c
3262b 61 6c 6c 62 61 63 6b 20 74 68 61 74 20 73 6c 65  allback that sle
3262c 65 70 73 20 61 6e 64 20 74 72 69 65 73 0a 2a 2a  eps and tries.**
3262d 20 61 67 61 69 6e 20 75 6e 74 69 6c 20 61 20 74   again until a t
3262e 69 6d 65 6f 75 74 20 76 61 6c 75 65 20 69 73 20  imeout value is 
3262f 72 65 61 63 68 65 64 2e 20 20 54 68 65 20 74 69  reached.  The ti
32630 6d 65 6f 75 74 20 76 61 6c 75 65 20 69 73 0a 2a  meout value is.*
32631 2a 20 61 6e 20 69 6e 74 65 67 65 72 20 6e 75 6d  * an integer num
32632 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f  ber of milliseco
32633 6e 64 73 20 70 61 73 73 65 64 20 69 6e 20 61 73  nds passed in as
32634 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72   the first.** ar
32635 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69  gument..*/.stati
32636 63 20 69 6e 74 20 73 71 6c 69 74 65 44 65 66 61  c int sqliteDefa
32637 75 6c 74 42 75 73 79 43 61 6c 6c 62 61 63 6b 28  ultBusyCallback(
32638 0a 20 76 6f 69 64 20 2a 70 74 72 2c 20 20 20 20  . void *ptr,    
32639 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
3263a 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3263b 6e 20 2a 2f 0a 20 69 6e 74 20 63 6f 75 6e 74 20  n */. int count 
3263c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3263d 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65  * Number of time
3263e 73 20 74 61 62 6c 65 20 68 61 73 20 62 65 65 6e  s table has been
3263f 20 62 75 73 79 20 2a 2f 0a 29 7b 0a 23 69 66 20   busy */.){.#if 
32640 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 7c 7c  SQLITE_OS_WIN ||
32641 20 28 64 65 66 69 6e 65 64 28 48 41 56 45 5f 55   (defined(HAVE_U
32642 53 4c 45 45 50 29 20 26 26 20 48 41 56 45 5f 55  SLEEP) && HAVE_U
32643 53 4c 45 45 50 29 0a 20 20 73 74 61 74 69 63 20  SLEEP).  static 
32644 63 6f 6e 73 74 20 75 38 20 64 65 6c 61 79 73 5b  const u8 delays[
32645 5d 20 3d 0a 20 20 20 20 20 7b 20 31 2c 20 32 2c  ] =.     { 1, 2,
32646 20 35 2c 20 31 30 2c 20 31 35 2c 20 32 30 2c 20   5, 10, 15, 20, 
32647 32 35 2c 20 32 35 2c 20 20 32 35 2c 20 20 35 30  25, 25,  25,  50
32648 2c 20 20 35 30 2c 20 31 30 30 20 7d 3b 0a 20 20  ,  50, 100 };.  
32649 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20  static const u8 
3264a 74 6f 74 61 6c 73 5b 5d 20 3d 0a 20 20 20 20 20  totals[] =.     
3264b 7b 20 30 2c 20 31 2c 20 33 2c 20 20 38 2c 20 31  { 0, 1, 3,  8, 1
3264c 38 2c 20 33 33 2c 20 35 33 2c 20 37 38 2c 20 31  8, 33, 53, 78, 1
3264d 30 33 2c 20 31 32 38 2c 20 31 37 38 2c 20 32 32  03, 128, 178, 22
3264e 38 20 7d 3b 0a 23 20 64 65 66 69 6e 65 20 4e 44  8 };.# define ND
3264f 45 4c 41 59 20 28 73 69 7a 65 6f 66 28 64 65 6c  ELAY (sizeof(del
32650 61 79 73 29 2f 73 69 7a 65 6f 66 28 64 65 6c 61  ays)/sizeof(dela
32651 79 73 5b 30 5d 29 29 0a 20 20 73 71 6c 69 74 65  ys[0])).  sqlite
32652 33 20 2a 64 62 20 3d 20 28 73 71 6c 69 74 65 33  3 *db = (sqlite3
32653 20 2a 29 70 74 72 3b 0a 20 20 69 6e 74 20 74 69   *)ptr;.  int ti
32654 6d 65 6f 75 74 20 3d 20 64 62 2d 3e 62 75 73 79  meout = db->busy
32655 54 69 6d 65 6f 75 74 3b 0a 20 20 69 6e 74 20 64  Timeout;.  int d
32656 65 6c 61 79 2c 20 70 72 69 6f 72 3b 0a 0a 20 20  elay, prior;..  
32657 61 73 73 65 72 74 28 20 63 6f 75 6e 74 3e 3d 30  assert( count>=0
32658 20 29 3b 0a 20 20 69 66 28 20 63 6f 75 6e 74 20   );.  if( count 
32659 3c 20 4e 44 45 4c 41 59 20 29 7b 0a 20 20 20 20  < NDELAY ){.    
3265a 64 65 6c 61 79 20 3d 20 64 65 6c 61 79 73 5b 63  delay = delays[c
3265b 6f 75 6e 74 5d 3b 0a 20 20 20 20 70 72 69 6f 72  ount];.    prior
3265c 20 3d 20 74 6f 74 61 6c 73 5b 63 6f 75 6e 74 5d   = totals[count]
3265d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64  ;.  }else{.    d
3265e 65 6c 61 79 20 3d 20 64 65 6c 61 79 73 5b 4e 44  elay = delays[ND
3265f 45 4c 41 59 2d 31 5d 3b 0a 20 20 20 20 70 72 69  ELAY-1];.    pri
32660 6f 72 20 3d 20 74 6f 74 61 6c 73 5b 4e 44 45 4c  or = totals[NDEL
32661 41 59 2d 31 5d 20 2b 20 64 65 6c 61 79 2a 28 63  AY-1] + delay*(c
32662 6f 75 6e 74 2d 28 4e 44 45 4c 41 59 2d 31 29 29  ount-(NDELAY-1))
32663 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 72 69 6f  ;.  }.  if( prio
32664 72 20 2b 20 64 65 6c 61 79 20 3e 20 74 69 6d 65  r + delay > time
32665 6f 75 74 20 29 7b 0a 20 20 20 20 64 65 6c 61 79  out ){.    delay
32666 20 3d 20 74 69 6d 65 6f 75 74 20 2d 20 70 72 69   = timeout - pri
32667 6f 72 3b 0a 20 20 20 20 69 66 28 20 64 65 6c 61  or;.    if( dela
32668 79 3c 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  y<=0 ) return 0;
32669 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 4f 73  .  }.  sqlite3Os
3266a 53 6c 65 65 70 28 64 62 2d 3e 70 56 66 73 2c 20  Sleep(db->pVfs, 
3266b 64 65 6c 61 79 2a 31 30 30 30 29 3b 0a 20 20 72  delay*1000);.  r
3266c 65 74 75 72 6e 20 31 3b 0a 23 65 6c 73 65 0a 20  eturn 1;.#else. 
3266d 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 28   sqlite3 *db = (
3266e 73 71 6c 69 74 65 33 20 2a 29 70 74 72 3b 0a 20  sqlite3 *)ptr;. 
3266f 20 69 6e 74 20 74 69 6d 65 6f 75 74 20 3d 20 28   int timeout = (
32670 28 73 71 6c 69 74 65 33 20 2a 29 70 74 72 29 2d  (sqlite3 *)ptr)-
32671 3e 62 75 73 79 54 69 6d 65 6f 75 74 3b 0a 20 20  >busyTimeout;.  
32672 69 66 28 20 28 63 6f 75 6e 74 2b 31 29 2a 31 30  if( (count+1)*10
32673 30 30 20 3e 20 74 69 6d 65 6f 75 74 20 29 7b 0a  00 > timeout ){.
32674 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
32675 7d 0a 20 20 73 71 6c 69 74 65 33 4f 73 53 6c 65  }.  sqlite3OsSle
32676 65 70 28 64 62 2d 3e 70 56 66 73 2c 20 31 30 30  ep(db->pVfs, 100
32677 30 30 30 30 29 3b 0a 20 20 72 65 74 75 72 6e 20  0000);.  return 
32678 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a  1;.#endif.}../*.
32679 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 67 69  ** Invoke the gi
3267a 76 65 6e 20 62 75 73 79 20 68 61 6e 64 6c 65 72  ven busy handler
3267b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
3267c 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77  tine is called w
3267d 68 65 6e 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e  hen an operation
3267e 20 66 61 69 6c 65 64 20 77 69 74 68 20 61 20 6c   failed with a l
3267f 6f 63 6b 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20  ock..** If this 
32680 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
32681 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 6c 6f  non-zero, the lo
32682 63 6b 20 69 73 20 72 65 74 72 69 65 64 2e 20 20  ck is retried.  
32683 49 66 20 69 74 0a 2a 2a 20 72 65 74 75 72 6e 73  If it.** returns
32684 20 30 2c 20 74 68 65 20 6f 70 65 72 61 74 69 6f   0, the operatio
32685 6e 20 61 62 6f 72 74 73 20 77 69 74 68 20 61 6e  n aborts with an
32686 20 53 51 4c 49 54 45 5f 42 55 53 59 20 65 72 72   SQLITE_BUSY err
32687 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  or..*/.SQLITE_PR
32688 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
32689 33 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c  3InvokeBusyHandl
3268a 65 72 28 42 75 73 79 48 61 6e 64 6c 65 72 20 2a  er(BusyHandler *
3268b 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  p){.  int rc;.  
3268c 69 66 28 20 4e 45 56 45 52 28 70 3d 3d 30 29 20  if( NEVER(p==0) 
3268d 7c 7c 20 70 2d 3e 78 46 75 6e 63 3d 3d 30 20 7c  || p->xFunc==0 |
3268e 7c 20 70 2d 3e 6e 42 75 73 79 3c 30 20 29 20 72  | p->nBusy<0 ) r
3268f 65 74 75 72 6e 20 30 3b 0a 20 20 72 63 20 3d 20  eturn 0;.  rc = 
32690 70 2d 3e 78 46 75 6e 63 28 70 2d 3e 70 41 72 67  p->xFunc(p->pArg
32691 2c 20 70 2d 3e 6e 42 75 73 79 29 3b 0a 20 20 69  , p->nBusy);.  i
32692 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20  f( rc==0 ){.    
32693 70 2d 3e 6e 42 75 73 79 20 3d 20 2d 31 3b 0a 20  p->nBusy = -1;. 
32694 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 6e   }else{.    p->n
32695 42 75 73 79 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65  Busy++;.  }.  re
32696 74 75 72 6e 20 72 63 3b 20 0a 7d 0a 0a 2f 2a 0a  turn rc; .}../*.
32697 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
32698 73 65 74 73 20 74 68 65 20 62 75 73 79 20 63 61  sets the busy ca
32699 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e 20 53 71  llback for an Sq
3269a 6c 69 74 65 20 64 61 74 61 62 61 73 65 20 74 6f  lite database to
3269b 20 74 68 65 0a 2a 2a 20 67 69 76 65 6e 20 63 61   the.** given ca
3269c 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
3269d 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 61  with the given a
3269e 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  rgument..*/.SQLI
3269f 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
326a0 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28  e3_busy_handler(
326a1 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
326a2 20 20 69 6e 74 20 28 2a 78 42 75 73 79 29 28 76    int (*xBusy)(v
326a3 6f 69 64 2a 2c 69 6e 74 29 2c 0a 20 20 76 6f 69  oid*,int),.  voi
326a4 64 20 2a 70 41 72 67 0a 29 7b 0a 20 20 73 71 6c  d *pArg.){.  sql
326a5 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
326a6 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64  (db->mutex);.  d
326a7 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 2e 78  b->busyHandler.x
326a8 46 75 6e 63 20 3d 20 78 42 75 73 79 3b 0a 20 20  Func = xBusy;.  
326a9 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 2e  db->busyHandler.
326aa 70 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 64  pArg = pArg;.  d
326ab 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 2e 6e  b->busyHandler.n
326ac 42 75 73 79 20 3d 20 30 3b 0a 20 20 73 71 6c 69  Busy = 0;.  sqli
326ad 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
326ae 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
326af 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
326b0 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
326b1 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f  E_OMIT_PROGRESS_
326b2 43 41 4c 4c 42 41 43 4b 0a 2f 2a 0a 2a 2a 20 54  CALLBACK./*.** T
326b3 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73  his routine sets
326b4 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61   the progress ca
326b5 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e 20 53 71  llback for an Sq
326b6 6c 69 74 65 20 64 61 74 61 62 61 73 65 20 74 6f  lite database to
326b7 20 74 68 65 0a 2a 2a 20 67 69 76 65 6e 20 63 61   the.** given ca
326b8 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
326b9 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 61  with the given a
326ba 72 67 75 6d 65 6e 74 2e 20 54 68 65 20 70 72 6f  rgument. The pro
326bb 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 77  gress callback w
326bc 69 6c 6c 0a 2a 2a 20 62 65 20 69 6e 76 6f 6b 65  ill.** be invoke
326bd 64 20 65 76 65 72 79 20 6e 4f 70 73 20 6f 70 63  d every nOps opc
326be 6f 64 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  odes..*/.SQLITE_
326bf 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
326c0 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65  _progress_handle
326c1 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  r(.  sqlite3 *db
326c2 2c 20 0a 20 20 69 6e 74 20 6e 4f 70 73 2c 0a 20  , .  int nOps,. 
326c3 20 69 6e 74 20 28 2a 78 50 72 6f 67 72 65 73 73   int (*xProgress
326c4 29 28 76 6f 69 64 2a 29 2c 20 0a 20 20 76 6f 69  )(void*), .  voi
326c5 64 20 2a 70 41 72 67 0a 29 7b 0a 20 20 73 71 6c  d *pArg.){.  sql
326c6 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
326c7 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69  (db->mutex);.  i
326c8 66 28 20 6e 4f 70 73 3e 30 20 29 7b 0a 20 20 20  f( nOps>0 ){.   
326c9 20 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 20 3d   db->xProgress =
326ca 20 78 50 72 6f 67 72 65 73 73 3b 0a 20 20 20 20   xProgress;.    
326cb 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73 4f 70 73  db->nProgressOps
326cc 20 3d 20 6e 4f 70 73 3b 0a 20 20 20 20 64 62 2d   = nOps;.    db-
326cd 3e 70 50 72 6f 67 72 65 73 73 41 72 67 20 3d 20  >pProgressArg = 
326ce 70 41 72 67 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  pArg;.  }else{. 
326cf 20 20 20 64 62 2d 3e 78 50 72 6f 67 72 65 73 73     db->xProgress
326d0 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 6e 50   = 0;.    db->nP
326d1 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b 0a  rogressOps = 0;.
326d2 20 20 20 20 64 62 2d 3e 70 50 72 6f 67 72 65 73      db->pProgres
326d3 73 41 72 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  sArg = 0;.  }.  
326d4 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
326d5 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
326d6 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  }.#endif.../*.**
326d7 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6e   This routine in
326d8 73 74 61 6c 6c 73 20 61 20 64 65 66 61 75 6c 74  stalls a default
326d9 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 74 68   busy handler th
326da 61 74 20 77 61 69 74 73 20 66 6f 72 20 74 68 65  at waits for the
326db 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 6e 75  .** specified nu
326dc 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63  mber of millisec
326dd 6f 6e 64 73 20 62 65 66 6f 72 65 20 72 65 74 75  onds before retu
326de 72 6e 69 6e 67 20 30 2e 0a 2a 2f 0a 53 51 4c 49  rning 0..*/.SQLI
326df 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
326e0 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28  e3_busy_timeout(
326e1 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
326e2 20 6d 73 29 7b 0a 20 20 69 66 28 20 6d 73 3e 30   ms){.  if( ms>0
326e3 20 29 7b 0a 20 20 20 20 64 62 2d 3e 62 75 73 79   ){.    db->busy
326e4 54 69 6d 65 6f 75 74 20 3d 20 6d 73 3b 0a 20 20  Timeout = ms;.  
326e5 20 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68    sqlite3_busy_h
326e6 61 6e 64 6c 65 72 28 64 62 2c 20 73 71 6c 69 74  andler(db, sqlit
326e7 65 44 65 66 61 75 6c 74 42 75 73 79 43 61 6c 6c  eDefaultBusyCall
326e8 62 61 63 6b 2c 20 28 76 6f 69 64 2a 29 64 62 29  back, (void*)db)
326e9 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
326ea 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64  qlite3_busy_hand
326eb 6c 65 72 28 64 62 2c 20 30 2c 20 30 29 3b 0a 20  ler(db, 0, 0);. 
326ec 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
326ed 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
326ee 43 61 75 73 65 20 61 6e 79 20 70 65 6e 64 69 6e  Cause any pendin
326ef 67 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 73  g operation to s
326f0 74 6f 70 20 61 74 20 69 74 73 20 65 61 72 6c 69  top at its earli
326f1 65 73 74 20 6f 70 70 6f 72 74 75 6e 69 74 79 2e  est opportunity.
326f2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
326f3 6f 69 64 20 73 71 6c 69 74 65 33 5f 69 6e 74 65  oid sqlite3_inte
326f4 72 72 75 70 74 28 73 71 6c 69 74 65 33 20 2a 64  rrupt(sqlite3 *d
326f5 62 29 7b 0a 20 20 64 62 2d 3e 75 31 2e 69 73 49  b){.  db->u1.isI
326f6 6e 74 65 72 72 75 70 74 65 64 20 3d 20 31 3b 0a  nterrupted = 1;.
326f7 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  }.../*.** This f
326f8 75 6e 63 74 69 6f 6e 20 69 73 20 65 78 61 63 74  unction is exact
326f9 6c 79 20 74 68 65 20 73 61 6d 65 20 61 73 20 73  ly the same as s
326fa 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
326fb 6e 63 74 69 6f 6e 28 29 2c 20 65 78 63 65 70 74  nction(), except
326fc 0a 2a 2a 20 74 68 61 74 20 69 74 20 69 73 20 64  .** that it is d
326fd 65 73 69 67 6e 65 64 20 74 6f 20 62 65 20 63 61  esigned to be ca
326fe 6c 6c 65 64 20 62 79 20 69 6e 74 65 72 6e 61 6c  lled by internal
326ff 20 63 6f 64 65 2e 20 54 68 65 20 64 69 66 66 65   code. The diffe
32700 72 65 6e 63 65 20 69 73 0a 2a 2a 20 74 68 61 74  rence is.** that
32701 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66   if a malloc() f
32702 61 69 6c 73 20 69 6e 20 73 71 6c 69 74 65 33 5f  ails in sqlite3_
32703 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
32704 29 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  ), an error code
32705 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20  .** is returned 
32706 61 6e 64 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61  and the mallocFa
32707 69 6c 65 64 20 66 6c 61 67 20 63 6c 65 61 72 65  iled flag cleare
32708 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  d. .*/.SQLITE_PR
32709 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
3270a 33 43 72 65 61 74 65 46 75 6e 63 28 0a 20 20 73  3CreateFunc(.  s
3270b 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f  qlite3 *db,.  co
3270c 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 74  nst char *zFunct
3270d 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e  ionName,.  int n
3270e 41 72 67 2c 0a 20 20 69 6e 74 20 65 6e 63 2c 0a  Arg,.  int enc,.
3270f 20 20 76 6f 69 64 20 2a 70 55 73 65 72 44 61 74    void *pUserDat
32710 61 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e  a,.  void (*xFun
32711 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  c)(sqlite3_conte
32712 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
32713 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f 69  value **),.  voi
32714 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74  d (*xStep)(sqlit
32715 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
32716 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
32717 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e  ),.  void (*xFin
32718 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  al)(sqlite3_cont
32719 65 78 74 2a 29 0a 29 7b 0a 20 20 46 75 6e 63 44  ext*).){.  FuncD
3271a 65 66 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 4e 61  ef *p;.  int nNa
3271b 6d 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  me;..  assert( s
3271c 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
3271d 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
3271e 20 20 69 66 28 20 7a 46 75 6e 63 74 69 6f 6e 4e    if( zFunctionN
3271f 61 6d 65 3d 3d 30 20 7c 7c 0a 20 20 20 20 20 20  ame==0 ||.      
32720 28 78 46 75 6e 63 20 26 26 20 28 78 46 69 6e 61  (xFunc && (xFina
32721 6c 20 7c 7c 20 78 53 74 65 70 29 29 20 7c 7c 20  l || xStep)) || 
32722 0a 20 20 20 20 20 20 28 21 78 46 75 6e 63 20 26  .      (!xFunc &
32723 26 20 28 78 46 69 6e 61 6c 20 26 26 20 21 78 53  & (xFinal && !xS
32724 74 65 70 29 29 20 7c 7c 0a 20 20 20 20 20 20 28  tep)) ||.      (
32725 21 78 46 75 6e 63 20 26 26 20 28 21 78 46 69 6e  !xFunc && (!xFin
32726 61 6c 20 26 26 20 78 53 74 65 70 29 29 20 7c 7c  al && xStep)) ||
32727 0a 20 20 20 20 20 20 28 6e 41 72 67 3c 2d 31 20  .      (nArg<-1 
32728 7c 7c 20 6e 41 72 67 3e 53 51 4c 49 54 45 5f 4d  || nArg>SQLITE_M
32729 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 29  AX_FUNCTION_ARG)
3272a 20 7c 7c 0a 20 20 20 20 20 20 28 32 35 35 3c 28   ||.      (255<(
3272b 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53  nName = sqlite3S
3272c 74 72 6c 65 6e 33 30 28 20 7a 46 75 6e 63 74 69  trlen30( zFuncti
3272d 6f 6e 4e 61 6d 65 29 29 29 20 29 7b 0a 20 20 20  onName))) ){.   
3272e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d   return SQLITE_M
3272f 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 0a 23 69  ISUSE;.  }.  .#i
32730 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
32731 54 5f 55 54 46 31 36 0a 20 20 2f 2a 20 49 66 20  T_UTF16.  /* If 
32732 53 51 4c 49 54 45 5f 55 54 46 31 36 20 69 73 20  SQLITE_UTF16 is 
32733 73 70 65 63 69 66 69 65 64 20 61 73 20 74 68 65  specified as the
32734 20 65 6e 63 6f 64 69 6e 67 20 74 79 70 65 2c 20   encoding type, 
32735 74 72 61 6e 73 66 6f 72 6d 20 74 68 69 73 0a 20  transform this. 
32736 20 2a 2a 20 74 6f 20 6f 6e 65 20 6f 66 20 53 51   ** to one of SQ
32737 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 20  LITE_UTF16LE or 
32738 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 75  SQLITE_UTF16BE u
32739 73 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 53 51  sing the.  ** SQ
3273a 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
3273b 20 6d 61 63 72 6f 2e 20 53 51 4c 49 54 45 5f 55   macro. SQLITE_U
3273c 54 46 31 36 20 69 73 20 6e 6f 74 20 75 73 65 64  TF16 is not used
3273d 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 2a   internally..  *
3273e 2a 0a 20 20 2a 2a 20 49 66 20 53 51 4c 49 54 45  *.  ** If SQLITE
3273f 5f 41 4e 59 20 69 73 20 73 70 65 63 69 66 69 65  _ANY is specifie
32740 64 2c 20 61 64 64 20 74 68 72 65 65 20 76 65 72  d, add three ver
32741 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 75 6e  sions of the fun
32742 63 74 69 6f 6e 0a 20 20 2a 2a 20 74 6f 20 74 68  ction.  ** to th
32743 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 20 20  e hash table..  
32744 2a 2f 0a 20 20 69 66 28 20 65 6e 63 3d 3d 53 51  */.  if( enc==SQ
32745 4c 49 54 45 5f 55 54 46 31 36 20 29 7b 0a 20 20  LITE_UTF16 ){.  
32746 20 20 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55    enc = SQLITE_U
32747 54 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 7d 65  TF16NATIVE;.  }e
32748 6c 73 65 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c  lse if( enc==SQL
32749 49 54 45 5f 41 4e 59 20 29 7b 0a 20 20 20 20 69  ITE_ANY ){.    i
3274a 6e 74 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20  nt rc;.    rc = 
3274b 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e  sqlite3CreateFun
3274c 63 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e  c(db, zFunctionN
3274d 61 6d 65 2c 20 6e 41 72 67 2c 20 53 51 4c 49 54  ame, nArg, SQLIT
3274e 45 5f 55 54 46 38 2c 0a 20 20 20 20 20 20 20 20  E_UTF8,.        
3274f 20 70 55 73 65 72 44 61 74 61 2c 20 78 46 75 6e   pUserData, xFun
32750 63 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c  c, xStep, xFinal
32751 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
32752 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
32753 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 43 72    rc = sqlite3Cr
32754 65 61 74 65 46 75 6e 63 28 64 62 2c 20 7a 46 75  eateFunc(db, zFu
32755 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e 41 72 67  nctionName, nArg
32756 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  , SQLITE_UTF16LE
32757 2c 0a 20 20 20 20 20 20 20 20 20 20 70 55 73 65  ,.          pUse
32758 72 44 61 74 61 2c 20 78 46 75 6e 63 2c 20 78 53  rData, xFunc, xS
32759 74 65 70 2c 20 78 46 69 6e 61 6c 29 3b 0a 20 20  tep, xFinal);.  
3275a 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d    }.    if( rc!=
3275b 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
3275c 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
3275d 20 20 7d 0a 20 20 20 20 65 6e 63 20 3d 20 53 51    }.    enc = SQ
3275e 4c 49 54 45 5f 55 54 46 31 36 42 45 3b 0a 20 20  LITE_UTF16BE;.  
3275f 7d 0a 23 65 6c 73 65 0a 20 20 65 6e 63 20 3d 20  }.#else.  enc = 
32760 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 23 65 6e  SQLITE_UTF8;.#en
32761 64 69 66 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63  dif.  .  /* Chec
32762 6b 20 69 66 20 61 6e 20 65 78 69 73 74 69 6e 67  k if an existing
32763 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 69   function is bei
32764 6e 67 20 6f 76 65 72 72 69 64 64 65 6e 20 6f 72  ng overridden or
32765 20 64 65 6c 65 74 65 64 2e 20 49 66 20 73 6f 2c   deleted. If so,
32766 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 72 65 20  .  ** and there 
32767 61 72 65 20 61 63 74 69 76 65 20 56 4d 73 2c 20  are active VMs, 
32768 74 68 65 6e 20 72 65 74 75 72 6e 20 53 51 4c 49  then return SQLI
32769 54 45 5f 42 55 53 59 2e 20 49 66 20 61 20 66 75  TE_BUSY. If a fu
3276a 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 62  nction.  ** is b
3276b 65 69 6e 67 20 6f 76 65 72 72 69 64 64 65 6e 2f  eing overridden/
3276c 64 65 6c 65 74 65 64 20 62 75 74 20 74 68 65 72  deleted but ther
3276d 65 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20  e are no active 
3276e 56 4d 73 2c 20 61 6c 6c 6f 77 20 74 68 65 0a 20  VMs, allow the. 
3276f 20 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f   ** operation to
32770 20 63 6f 6e 74 69 6e 75 65 20 62 75 74 20 69 6e   continue but in
32771 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 70 72 65  validate all pre
32772 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65  compiled stateme
32773 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d 20  nts..  */.  p = 
32774 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74  sqlite3FindFunct
32775 69 6f 6e 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f  ion(db, zFunctio
32776 6e 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e 41  nName, nName, nA
32777 72 67 2c 20 28 75 38 29 65 6e 63 2c 20 30 29 3b  rg, (u8)enc, 0);
32778 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 69  .  if( p && p->i
32779 50 72 65 66 45 6e 63 3d 3d 65 6e 63 20 26 26 20  PrefEnc==enc && 
3277a 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20 29 7b  p->nArg==nArg ){
3277b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63 74  .    if( db->act
3277c 69 76 65 56 64 62 65 43 6e 74 20 29 7b 0a 20 20  iveVdbeCnt ){.  
3277d 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
3277e 28 64 62 2c 20 53 51 4c 49 54 45 5f 42 55 53 59  (db, SQLITE_BUSY
3277f 2c 20 0a 20 20 20 20 20 20 20 20 22 75 6e 61 62  , .        "unab
32780 6c 65 20 74 6f 20 64 65 6c 65 74 65 2f 6d 6f 64  le to delete/mod
32781 69 66 79 20 75 73 65 72 2d 66 75 6e 63 74 69 6f  ify user-functio
32782 6e 20 64 75 65 20 74 6f 20 61 63 74 69 76 65 20  n due to active 
32783 73 74 61 74 65 6d 65 6e 74 73 22 29 3b 0a 20 20  statements");.  
32784 20 20 20 20 61 73 73 65 72 74 28 20 21 64 62 2d      assert( !db-
32785 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
32786 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
32787 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d  LITE_BUSY;.    }
32788 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
32789 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65  te3ExpirePrepare
3278a 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b  dStatements(db);
3278b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 20  .    }.  }..  p 
3278c 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e  = sqlite3FindFun
3278d 63 74 69 6f 6e 28 64 62 2c 20 7a 46 75 6e 63 74  ction(db, zFunct
3278e 69 6f 6e 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20  ionName, nName, 
3278f 6e 41 72 67 2c 20 28 75 38 29 65 6e 63 2c 20 31  nArg, (u8)enc, 1
32790 29 3b 0a 20 20 61 73 73 65 72 74 28 70 20 7c 7c  );.  assert(p ||
32791 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
32792 64 29 3b 0a 20 20 69 66 28 20 21 70 20 29 7b 0a  d);.  if( !p ){.
32793 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
32794 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70  E_NOMEM;.  }.  p
32795 2d 3e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 70  ->flags = 0;.  p
32796 2d 3e 78 46 75 6e 63 20 3d 20 78 46 75 6e 63 3b  ->xFunc = xFunc;
32797 0a 20 20 70 2d 3e 78 53 74 65 70 20 3d 20 78 53  .  p->xStep = xS
32798 74 65 70 3b 0a 20 20 70 2d 3e 78 46 69 6e 61 6c  tep;.  p->xFinal
32799 69 7a 65 20 3d 20 78 46 69 6e 61 6c 3b 0a 20 20  ize = xFinal;.  
3279a 70 2d 3e 70 55 73 65 72 44 61 74 61 20 3d 20 70  p->pUserData = p
3279b 55 73 65 72 44 61 74 61 3b 0a 20 20 70 2d 3e 6e  UserData;.  p->n
3279c 41 72 67 20 3d 20 28 75 31 36 29 6e 41 72 67 3b  Arg = (u16)nArg;
3279d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
3279e 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72  _OK;.}../*.** Cr
3279f 65 61 74 65 20 6e 65 77 20 75 73 65 72 20 66 75  eate new user fu
327a0 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49  nctions..*/.SQLI
327a1 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
327a2 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
327a3 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  on(.  sqlite3 *d
327a4 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  b,.  const char 
327a5 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a  *zFunctionName,.
327a6 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e    int nArg,.  in
327a7 74 20 65 6e 63 2c 0a 20 20 76 6f 69 64 20 2a 70  t enc,.  void *p
327a8 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63  ,.  void (*xFunc
327a9 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
327aa 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
327ab 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64  alue **),.  void
327ac 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65   (*xStep)(sqlite
327ad 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
327ae 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29  qlite3_value **)
327af 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61  ,.  void (*xFina
327b0 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  l)(sqlite3_conte
327b1 78 74 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63  xt*).){.  int rc
327b2 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
327b3 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
327b4 78 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  x);.  rc = sqlit
327b5 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c  e3CreateFunc(db,
327b6 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20   zFunctionName, 
327b7 6e 41 72 67 2c 20 65 6e 63 2c 20 70 2c 20 78 46  nArg, enc, p, xF
327b8 75 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 69 6e  unc, xStep, xFin
327b9 61 6c 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  al);.  rc = sqli
327ba 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72  te3ApiExit(db, r
327bb 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  c);.  sqlite3_mu
327bc 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
327bd 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
327be 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  c;.}..#ifndef SQ
327bf 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
327c0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
327c1 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
327c2 6e 63 74 69 6f 6e 31 36 28 0a 20 20 73 71 6c 69  nction16(.  sqli
327c3 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74  te3 *db,.  const
327c4 20 76 6f 69 64 20 2a 7a 46 75 6e 63 74 69 6f 6e   void *zFunction
327c5 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67  Name,.  int nArg
327c6 2c 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70  ,.  int eTextRep
327c7 2c 0a 20 20 76 6f 69 64 20 2a 70 2c 0a 20 20 76  ,.  void *p,.  v
327c8 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c  oid (*xFunc)(sql
327c9 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
327ca 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t,sqlite3_value*
327cb 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 74  *),.  void (*xSt
327cc 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ep)(sqlite3_cont
327cd 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
327ce 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69  _value**),.  voi
327cf 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69  d (*xFinal)(sqli
327d0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 7b  te3_context*).){
327d1 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 63 68 61  .  int rc;.  cha
327d2 72 20 2a 7a 46 75 6e 63 38 3b 0a 20 20 73 71 6c  r *zFunc8;.  sql
327d3 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
327d4 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61  (db->mutex);.  a
327d5 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c  ssert( !db->mall
327d6 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 7a 46  ocFailed );.  zF
327d7 75 6e 63 38 20 3d 20 73 71 6c 69 74 65 33 55 74  unc8 = sqlite3Ut
327d8 66 31 36 74 6f 38 28 64 62 2c 20 7a 46 75 6e 63  f16to8(db, zFunc
327d9 74 69 6f 6e 4e 61 6d 65 2c 20 2d 31 29 3b 0a 20  tionName, -1);. 
327da 20 72 63 20 3d 20 73 71 6c 69 74 65 33 43 72 65   rc = sqlite3Cre
327db 61 74 65 46 75 6e 63 28 64 62 2c 20 7a 46 75 6e  ateFunc(db, zFun
327dc 63 38 2c 20 6e 41 72 67 2c 20 65 54 65 78 74 52  c8, nArg, eTextR
327dd 65 70 2c 20 70 2c 20 78 46 75 6e 63 2c 20 78 53  ep, p, xFunc, xS
327de 74 65 70 2c 20 78 46 69 6e 61 6c 29 3b 0a 20 20  tep, xFinal);.  
327df 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
327e0 2c 20 7a 46 75 6e 63 38 29 3b 0a 20 20 72 63 20  , zFunc8);.  rc 
327e1 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
327e2 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69  (db, rc);.  sqli
327e3 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
327e4 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
327e5 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69  turn rc;.}.#endi
327e6 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6c 61 72  f.../*.** Declar
327e7 65 20 74 68 61 74 20 61 20 66 75 6e 63 74 69 6f  e that a functio
327e8 6e 20 68 61 73 20 62 65 65 6e 20 6f 76 65 72 6c  n has been overl
327e9 6f 61 64 65 64 20 62 79 20 61 20 76 69 72 74 75  oaded by a virtu
327ea 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  al table..**.** 
327eb 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  If the function 
327ec 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 61  already exists a
327ed 73 20 61 20 72 65 67 75 6c 61 72 20 67 6c 6f 62  s a regular glob
327ee 61 6c 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65  al function, the
327ef 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  n.** this routin
327f0 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 49  e is a no-op.  I
327f1 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 64  f the function d
327f2 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 74  oes not exist, t
327f3 68 65 6e 20 63 72 65 61 74 65 0a 2a 2a 20 61 20  hen create.** a 
327f4 6e 65 77 20 6f 6e 65 20 74 68 61 74 20 61 6c 77  new one that alw
327f5 61 79 73 20 74 68 72 6f 77 73 20 61 20 72 75 6e  ays throws a run
327f6 2d 74 69 6d 65 20 65 72 72 6f 72 2e 20 20 0a 2a  -time error.  .*
327f7 2a 0a 2a 2a 20 57 68 65 6e 20 76 69 72 74 75 61  *.** When virtua
327f8 6c 20 74 61 62 6c 65 73 20 69 6e 74 65 6e 64 20  l tables intend 
327f9 74 6f 20 70 72 6f 76 69 64 65 20 61 6e 20 6f 76  to provide an ov
327fa 65 72 6c 6f 61 64 65 64 20 66 75 6e 63 74 69 6f  erloaded functio
327fb 6e 2c 20 74 68 65 79 0a 2a 2a 20 73 68 6f 75 6c  n, they.** shoul
327fc 64 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74  d call this rout
327fd 69 6e 65 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  ine to make sure
327fe 20 74 68 65 20 67 6c 6f 62 61 6c 20 66 75 6e 63   the global func
327ff 74 69 6f 6e 20 65 78 69 73 74 73 2e 0a 2a 2a 20  tion exists..** 
32800 41 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f  A global functio
32801 6e 20 6d 75 73 74 20 65 78 69 73 74 20 69 6e 20  n must exist in 
32802 6f 72 64 65 72 20 66 6f 72 20 6e 61 6d 65 20 72  order for name r
32803 65 73 6f 6c 75 74 69 6f 6e 20 74 6f 20 77 6f 72  esolution to wor
32804 6b 0a 2a 2a 20 70 72 6f 70 65 72 6c 79 2e 0a 2a  k.** properly..*
32805 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
32806 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61   sqlite3_overloa
32807 64 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71  d_function(.  sq
32808 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e  lite3 *db,.  con
32809 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a  st char *zName,.
3280a 20 20 69 6e 74 20 6e 41 72 67 0a 29 7b 0a 20 20    int nArg.){.  
3280b 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69  int nName = sqli
3280c 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d  te3Strlen30(zNam
3280d 65 29 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  e);.  int rc;.  
3280e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
3280f 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
32810 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e    if( sqlite3Fin
32811 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 4e  dFunction(db, zN
32812 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e 41 72 67  ame, nName, nArg
32813 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30  , SQLITE_UTF8, 0
32814 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  )==0 ){.    sqli
32815 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62  te3CreateFunc(db
32816 2c 20 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 53  , zName, nArg, S
32817 51 4c 49 54 45 5f 55 54 46 38 2c 0a 20 20 20 20  QLITE_UTF8,.    
32818 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32819 20 20 30 2c 20 73 71 6c 69 74 65 33 49 6e 76 61    0, sqlite3Inva
3281a 6c 69 64 46 75 6e 63 74 69 6f 6e 2c 20 30 2c 20  lidFunction, 0, 
3281b 30 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73  0);.  }.  rc = s
3281c 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62  qlite3ApiExit(db
3281d 2c 20 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 20 20  , SQLITE_OK);.  
3281e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
3281f 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
32820 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
32821 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
32822 4d 49 54 5f 54 52 41 43 45 0a 2f 2a 0a 2a 2a 20  MIT_TRACE./*.** 
32823 52 65 67 69 73 74 65 72 20 61 20 74 72 61 63 65  Register a trace
32824 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20   function.  The 
32825 70 41 72 67 20 66 72 6f 6d 20 74 68 65 20 70 72  pArg from the pr
32826 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65  eviously registe
32827 72 65 64 20 74 72 61 63 65 0a 2a 2a 20 69 73 20  red trace.** is 
32828 72 65 74 75 72 6e 65 64 2e 20 20 0a 2a 2a 0a 2a  returned.  .**.*
32829 2a 20 41 20 4e 55 4c 4c 20 74 72 61 63 65 20 66  * A NULL trace f
3282a 75 6e 63 74 69 6f 6e 20 6d 65 61 6e 73 20 74 68  unction means th
3282b 61 74 20 6e 6f 20 74 72 61 63 69 6e 67 20 69 73  at no tracing is
3282c 20 65 78 65 63 75 74 65 73 2e 20 20 41 20 6e 6f   executes.  A no
3282d 6e 2d 4e 55 4c 4c 0a 2a 2a 20 74 72 61 63 65 20  n-NULL.** trace 
3282e 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
3282f 61 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20  a function that 
32830 69 73 20 69 6e 76 6f 6b 65 64 20 61 74 20 74 68  is invoked at th
32831 65 20 73 74 61 72 74 20 6f 66 20 65 61 63 68 0a  e start of each.
32832 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ** SQL statement
32833 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
32834 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 74 72  void *sqlite3_tr
32835 61 63 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ace(sqlite3 *db,
32836 20 76 6f 69 64 20 28 2a 78 54 72 61 63 65 29 28   void (*xTrace)(
32837 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72  void*,const char
32838 2a 29 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b  *), void *pArg){
32839 0a 20 20 76 6f 69 64 20 2a 70 4f 6c 64 3b 0a 20  .  void *pOld;. 
3283a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
3283b 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
3283c 0a 20 20 70 4f 6c 64 20 3d 20 64 62 2d 3e 70 54  .  pOld = db->pT
3283d 72 61 63 65 41 72 67 3b 0a 20 20 64 62 2d 3e 78  raceArg;.  db->x
3283e 54 72 61 63 65 20 3d 20 78 54 72 61 63 65 3b 0a  Trace = xTrace;.
3283f 20 20 64 62 2d 3e 70 54 72 61 63 65 41 72 67 20    db->pTraceArg 
32840 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65  = pArg;.  sqlite
32841 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
32842 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
32843 72 6e 20 70 4f 6c 64 3b 0a 7d 0a 2f 2a 0a 2a 2a  rn pOld;.}./*.**
32844 20 52 65 67 69 73 74 65 72 20 61 20 70 72 6f 66   Register a prof
32845 69 6c 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54  ile function.  T
32846 68 65 20 70 41 72 67 20 66 72 6f 6d 20 74 68 65  he pArg from the
32847 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69   previously regi
32848 73 74 65 72 65 64 20 0a 2a 2a 20 70 72 6f 66 69  stered .** profi
32849 6c 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72  le function is r
3284a 65 74 75 72 6e 65 64 2e 20 20 0a 2a 2a 0a 2a 2a  eturned.  .**.**
3284b 20 41 20 4e 55 4c 4c 20 70 72 6f 66 69 6c 65 20   A NULL profile 
3284c 66 75 6e 63 74 69 6f 6e 20 6d 65 61 6e 73 20 74  function means t
3284d 68 61 74 20 6e 6f 20 70 72 6f 66 69 6c 69 6e 67  hat no profiling
3284e 20 69 73 20 65 78 65 63 75 74 65 73 2e 20 20 41   is executes.  A
3284f 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 70 72 6f   non-NULL.** pro
32850 66 69 6c 65 20 69 73 20 61 20 70 6f 69 6e 74 65  file is a pointe
32851 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20  r to a function 
32852 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20  that is invoked 
32853 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f  at the conclusio
32854 6e 20 6f 66 0a 2a 2a 20 65 61 63 68 20 53 51 4c  n of.** each SQL
32855 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
32856 69 73 20 72 75 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  is run..*/.SQLIT
32857 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
32858 74 65 33 5f 70 72 6f 66 69 6c 65 28 0a 20 20 73  te3_profile(.  s
32859 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 76 6f  qlite3 *db,.  vo
3285a 69 64 20 28 2a 78 50 72 6f 66 69 6c 65 29 28 76  id (*xProfile)(v
3285b 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  oid*,const char*
3285c 2c 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 2c  ,sqlite_uint64),
3285d 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 0a 29 7b  .  void *pArg.){
3285e 0a 20 20 76 6f 69 64 20 2a 70 4f 6c 64 3b 0a 20  .  void *pOld;. 
3285f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
32860 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
32861 0a 20 20 70 4f 6c 64 20 3d 20 64 62 2d 3e 70 50  .  pOld = db->pP
32862 72 6f 66 69 6c 65 41 72 67 3b 0a 20 20 64 62 2d  rofileArg;.  db-
32863 3e 78 50 72 6f 66 69 6c 65 20 3d 20 78 50 72 6f  >xProfile = xPro
32864 66 69 6c 65 3b 0a 20 20 64 62 2d 3e 70 50 72 6f  file;.  db->pPro
32865 66 69 6c 65 41 72 67 20 3d 20 70 41 72 67 3b 0a  fileArg = pArg;.
32866 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
32867 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
32868 3b 0a 20 20 72 65 74 75 72 6e 20 70 4f 6c 64 3b  ;.  return pOld;
32869 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
3286a 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20 2a  ITE_OMIT_TRACE *
3286b 2f 0a 0a 2f 2a 2a 2a 20 45 58 50 45 52 49 4d 45  /../*** EXPERIME
3286c 4e 54 41 4c 20 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 52  NTAL ***.**.** R
3286d 65 67 69 73 74 65 72 20 61 20 66 75 6e 63 74 69  egister a functi
3286e 6f 6e 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64  on to be invoked
3286f 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74   when a transact
32870 69 6f 6e 20 63 6f 6d 6d 65 6e 74 73 2e 0a 2a 2a  ion comments..**
32871 20 49 66 20 74 68 65 20 69 6e 76 6f 6b 65 64 20   If the invoked 
32872 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
32873 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20   non-zero, then 
32874 74 68 65 20 63 6f 6d 6d 69 74 20 62 65 63 6f 6d  the commit becom
32875 65 73 20 61 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b  es a.** rollback
32876 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
32877 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f  void *sqlite3_co
32878 6d 6d 69 74 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c  mmit_hook(.  sql
32879 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
3287a 20 20 20 20 20 20 20 2f 2a 20 41 74 74 61 63 68         /* Attach
3287b 20 74 68 65 20 68 6f 6f 6b 20 74 6f 20 74 68 69   the hook to thi
3287c 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  s database */.  
3287d 69 6e 74 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29  int (*xCallback)
3287e 28 76 6f 69 64 2a 29 2c 20 20 2f 2a 20 46 75 6e  (void*),  /* Fun
3287f 63 74 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65 20  ction to invoke 
32880 6f 6e 20 65 61 63 68 20 63 6f 6d 6d 69 74 20 2a  on each commit *
32881 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 20 20  /.  void *pArg  
32882 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
32883 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   Argument to the
32884 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a   function */.){.
32885 20 20 76 6f 69 64 20 2a 70 4f 6c 64 3b 0a 20 20    void *pOld;.  
32886 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
32887 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
32888 20 20 70 4f 6c 64 20 3d 20 64 62 2d 3e 70 43 6f    pOld = db->pCo
32889 6d 6d 69 74 41 72 67 3b 0a 20 20 64 62 2d 3e 78  mmitArg;.  db->x
3288a 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 20 3d  CommitCallback =
3288b 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 64 62   xCallback;.  db
3288c 2d 3e 70 43 6f 6d 6d 69 74 41 72 67 20 3d 20 70  ->pCommitArg = p
3288d 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  Arg;.  sqlite3_m
3288e 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
3288f 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
32890 70 4f 6c 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  pOld;.}../*.** R
32891 65 67 69 73 74 65 72 20 61 20 63 61 6c 6c 62 61  egister a callba
32892 63 6b 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64  ck to be invoked
32893 20 65 61 63 68 20 74 69 6d 65 20 61 20 72 6f 77   each time a row
32894 20 69 73 20 75 70 64 61 74 65 64 2c 0a 2a 2a 20   is updated,.** 
32895 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c 65  inserted or dele
32896 74 65 64 20 75 73 69 6e 67 20 74 68 69 73 20 64  ted using this d
32897 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
32898 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  on..*/.SQLITE_AP
32899 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  I void *sqlite3_
3289a 75 70 64 61 74 65 5f 68 6f 6f 6b 28 0a 20 20 73  update_hook(.  s
3289b 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
3289c 20 20 20 20 20 20 20 20 20 2f 2a 20 41 74 74 61           /* Atta
3289d 63 68 20 74 68 65 20 68 6f 6f 6b 20 74 6f 20 74  ch the hook to t
3289e 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  his database */.
3289f 20 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61    void (*xCallba
328a0 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 68  ck)(void*,int,ch
328a1 61 72 20 63 6f 6e 73 74 20 2a 2c 63 68 61 72 20  ar const *,char 
328a2 63 6f 6e 73 74 20 2a 2c 73 71 6c 69 74 65 5f 69  const *,sqlite_i
328a3 6e 74 36 34 29 2c 0a 20 20 76 6f 69 64 20 2a 70  nt64),.  void *p
328a4 41 72 67 20 20 20 20 20 20 20 20 20 20 20 20 20  Arg             
328a5 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74     /* Argument t
328a6 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a  o the function *
328a7 2f 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 52 65  /.){.  void *pRe
328a8 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  t;.  sqlite3_mut
328a9 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
328aa 65 78 29 3b 0a 20 20 70 52 65 74 20 3d 20 64 62  ex);.  pRet = db
328ab 2d 3e 70 55 70 64 61 74 65 41 72 67 3b 0a 20 20  ->pUpdateArg;.  
328ac 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62  db->xUpdateCallb
328ad 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b  ack = xCallback;
328ae 0a 20 20 64 62 2d 3e 70 55 70 64 61 74 65 41 72  .  db->pUpdateAr
328af 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69  g = pArg;.  sqli
328b0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
328b1 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
328b2 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f 2a  turn pRet;.}../*
328b3 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 63  .** Register a c
328b4 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 69 6e  allback to be in
328b5 76 6f 6b 65 64 20 65 61 63 68 20 74 69 6d 65 20  voked each time 
328b6 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
328b7 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63 6b 20   rolled.** back 
328b8 62 79 20 74 68 69 73 20 64 61 74 61 62 61 73 65  by this database
328b9 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a   connection..*/.
328ba 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
328bb 2a 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63  *sqlite3_rollbac
328bc 6b 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c 69 74 65  k_hook(.  sqlite
328bd 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
328be 20 20 20 20 2f 2a 20 41 74 74 61 63 68 20 74 68      /* Attach th
328bf 65 20 68 6f 6f 6b 20 74 6f 20 74 68 69 73 20 64  e hook to this d
328c0 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 76 6f 69  atabase */.  voi
328c1 64 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76  d (*xCallback)(v
328c2 6f 69 64 2a 29 2c 20 2f 2a 20 43 61 6c 6c 62 61  oid*), /* Callba
328c3 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  ck function */. 
328c4 20 76 6f 69 64 20 2a 70 41 72 67 20 20 20 20 20   void *pArg     
328c5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
328c6 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 75  gument to the fu
328c7 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 76  nction */.){.  v
328c8 6f 69 64 20 2a 70 52 65 74 3b 0a 20 20 73 71 6c  oid *pRet;.  sql
328c9 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
328ca 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70  (db->mutex);.  p
328cb 52 65 74 20 3d 20 64 62 2d 3e 70 52 6f 6c 6c 62  Ret = db->pRollb
328cc 61 63 6b 41 72 67 3b 0a 20 20 64 62 2d 3e 78 52  ackArg;.  db->xR
328cd 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 20  ollbackCallback 
328ce 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 64  = xCallback;.  d
328cf 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b 41 72 67 20  b->pRollbackArg 
328d0 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65  = pArg;.  sqlite
328d1 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
328d2 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
328d3 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pRet;.}../*.*
328d4 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
328d5 72 65 74 75 72 6e 73 20 74 72 75 65 20 69 66 20  returns true if 
328d6 6d 61 69 6e 2d 6d 65 6d 6f 72 79 20 73 68 6f 75  main-memory shou
328d7 6c 64 20 62 65 20 75 73 65 64 20 69 6e 73 74 65  ld be used inste
328d8 61 64 20 6f 66 0a 2a 2a 20 61 20 74 65 6d 70 6f  ad of.** a tempo
328d9 72 61 72 79 20 66 69 6c 65 20 66 6f 72 20 74 72  rary file for tr
328da 61 6e 73 69 65 6e 74 20 70 61 67 65 72 20 66 69  ansient pager fi
328db 6c 65 73 20 61 6e 64 20 73 74 61 74 65 6d 65 6e  les and statemen
328dc 74 20 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2a 20 54  t journals..** T
328dd 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
328de 64 20 64 65 70 65 6e 64 73 20 6f 6e 20 74 68 65  d depends on the
328df 20 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e 74 65   value of db->te
328e0 6d 70 5f 73 74 6f 72 65 20 28 72 75 6e 74 69 6d  mp_store (runtim
328e1 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 29 20  e.** parameter) 
328e2 61 6e 64 20 74 68 65 20 63 6f 6d 70 69 6c 65 20  and the compile 
328e3 74 69 6d 65 20 76 61 6c 75 65 20 6f 66 20 53 51  time value of SQ
328e4 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 2e  LITE_TEMP_STORE.
328e5 20 54 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e   The.** followin
328e6 67 20 74 61 62 6c 65 20 64 65 73 63 72 69 62 65  g table describe
328e7 73 20 74 68 65 20 72 65 6c 61 74 69 6f 6e 73 68  s the relationsh
328e8 69 70 20 62 65 74 77 65 65 6e 20 74 68 65 73 65  ip between these
328e9 20 74 77 6f 20 76 61 6c 75 65 73 0a 2a 2a 20 61   two values.** a
328ea 6e 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  nd this function
328eb 73 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2e 0a  s return value..
328ec 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 54  **.**   SQLITE_T
328ed 45 4d 50 5f 53 54 4f 52 45 20 20 20 20 20 64 62  EMP_STORE     db
328ee 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 20 20 20 20  ->temp_store    
328ef 20 4c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 65 6d   Location of tem
328f0 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 0a  porary database.
328f1 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  **   -----------
328f2 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d 2d 2d  ------     -----
328f3 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d  ---------     --
328f4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
328f5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20  ------------.** 
328f6 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20    0             
328f7 20 20 20 20 20 20 20 20 61 6e 79 20 20 20 20 20          any     
328f8 20 20 20 20 20 20 20 20 20 20 20 66 69 6c 65 20             file 
328f9 20 20 20 20 20 28 72 65 74 75 72 6e 20 30 29 0a       (return 0).
328fa 2a 2a 20 20 20 31 20 20 20 20 20 20 20 20 20 20  **   1          
328fb 20 20 20 20 20 20 20 20 20 20 20 31 20 20 20 20             1    
328fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69                fi
328fd 6c 65 20 20 20 20 20 20 28 72 65 74 75 72 6e 20  le      (return 
328fe 30 29 0a 2a 2a 20 20 20 31 20 20 20 20 20 20 20  0).**   1       
328ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 20                2 
32900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32901 20 6d 65 6d 6f 72 79 20 20 20 20 28 72 65 74 75   memory    (retu
32902 72 6e 20 31 29 0a 2a 2a 20 20 20 31 20 20 20 20  rn 1).**   1    
32903 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32904 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20   0              
32905 20 20 20 20 66 69 6c 65 20 20 20 20 20 20 28 72      file      (r
32906 65 74 75 72 6e 20 30 29 0a 2a 2a 20 20 20 32 20  eturn 0).**   2 
32907 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32908 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20 20      1           
32909 20 20 20 20 20 20 20 66 69 6c 65 20 20 20 20 20         file     
3290a 20 28 72 65 74 75 72 6e 20 30 29 0a 2a 2a 20 20   (return 0).**  
3290b 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20   2              
3290c 20 20 20 20 20 20 20 32 20 20 20 20 20 20 20 20         2        
3290d 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6f 72 79            memory
3290e 20 20 20 20 28 72 65 74 75 72 6e 20 31 29 0a 2a      (return 1).*
3290f 2a 20 20 20 32 20 20 20 20 20 20 20 20 20 20 20  *   2           
32910 20 20 20 20 20 20 20 20 20 20 30 20 20 20 20 20            0     
32911 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d               mem
32912 6f 72 79 20 20 20 20 28 72 65 74 75 72 6e 20 31  ory    (return 1
32913 29 0a 2a 2a 20 20 20 33 20 20 20 20 20 20 20 20  ).**   3        
32914 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 79               any
32915 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32916 6d 65 6d 6f 72 79 20 20 20 20 28 72 65 74 75 72  memory    (retur
32917 6e 20 31 29 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  n 1).*/.SQLITE_P
32918 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
32919 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28 63  e3TempInMemory(c
3291a 6f 6e 73 74 20 73 71 6c 69 74 65 33 20 2a 64 62  onst sqlite3 *db
3291b 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 45  ){.#if SQLITE_TE
3291c 4d 50 5f 53 54 4f 52 45 3d 3d 31 0a 20 20 72 65  MP_STORE==1.  re
3291d 74 75 72 6e 20 28 20 64 62 2d 3e 74 65 6d 70 5f  turn ( db->temp_
3291e 73 74 6f 72 65 3d 3d 32 20 29 3b 0a 23 65 6e 64  store==2 );.#end
3291f 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 45  if.#if SQLITE_TE
32920 4d 50 5f 53 54 4f 52 45 3d 3d 32 0a 20 20 72 65  MP_STORE==2.  re
32921 74 75 72 6e 20 28 20 64 62 2d 3e 74 65 6d 70 5f  turn ( db->temp_
32922 73 74 6f 72 65 21 3d 31 20 29 3b 0a 23 65 6e 64  store!=1 );.#end
32923 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 45  if.#if SQLITE_TE
32924 4d 50 5f 53 54 4f 52 45 3d 3d 33 0a 20 20 72 65  MP_STORE==3.  re
32925 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 23  turn 1;.#endif.#
32926 69 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53  if SQLITE_TEMP_S
32927 54 4f 52 45 3c 31 20 7c 7c 20 53 51 4c 49 54 45  TORE<1 || SQLITE
32928 5f 54 45 4d 50 5f 53 54 4f 52 45 3e 33 0a 20 20  _TEMP_STORE>3.  
32929 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66  return 0;.#endif
3292a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
3292b 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
3292c 20 74 6f 20 63 72 65 61 74 65 20 61 20 63 6f 6e   to create a con
3292d 6e 65 63 74 69 6f 6e 20 74 6f 20 61 20 64 61 74  nection to a dat
3292e 61 62 61 73 65 20 42 54 72 65 65 0a 2a 2a 20 64  abase BTree.** d
3292f 72 69 76 65 72 2e 20 20 49 66 20 7a 46 69 6c 65  river.  If zFile
32930 6e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65  name is the name
32931 20 6f 66 20 61 20 66 69 6c 65 2c 20 74 68 65 6e   of a file, then
32932 20 74 68 61 74 20 66 69 6c 65 20 69 73 0a 2a 2a   that file is.**
32933 20 6f 70 65 6e 65 64 20 61 6e 64 20 75 73 65 64   opened and used
32934 2e 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20  .  If zFilename 
32935 69 73 20 74 68 65 20 6d 61 67 69 63 20 6e 61 6d  is the magic nam
32936 65 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 65  e ":memory:" the
32937 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73  n.** the databas
32938 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 6d  e is stored in m
32939 65 6d 6f 72 79 20 28 61 6e 64 20 69 73 20 74 68  emory (and is th
3293a 75 73 20 66 6f 72 67 6f 74 74 65 6e 20 61 73 20  us forgotten as 
3293b 73 6f 6f 6e 20 61 73 0a 2a 2a 20 74 68 65 20 63  soon as.** the c
3293c 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f  onnection is clo
3293d 73 65 64 2e 29 20 20 49 66 20 7a 46 69 6c 65 6e  sed.)  If zFilen
3293e 61 6d 65 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e  ame is NULL then
3293f 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a   the database.**
32940 20 69 73 20 61 20 22 76 69 72 74 75 61 6c 22 20   is a "virtual" 
32941 64 61 74 61 62 61 73 65 20 66 6f 72 20 74 72 61  database for tra
32942 6e 73 69 65 6e 74 20 75 73 65 20 6f 6e 6c 79 20  nsient use only 
32943 61 6e 64 20 69 73 20 64 65 6c 65 74 65 64 20 61  and is deleted a
32944 73 0a 2a 2a 20 73 6f 6f 6e 20 61 73 20 74 68 65  s.** soon as the
32945 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63   connection is c
32946 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 76  losed..**.** A v
32947 69 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20  irtual database 
32948 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 20  can be either a 
32949 64 69 73 6b 20 66 69 6c 65 20 28 74 68 61 74 20  disk file (that 
3294a 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  is automatically
3294b 0a 2a 2a 20 64 65 6c 65 74 65 64 20 77 68 65 6e  .** deleted when
3294c 20 74 68 65 20 66 69 6c 65 20 69 73 20 63 6c 6f   the file is clo
3294d 73 65 64 29 20 6f 72 20 69 74 20 61 6e 20 62 65  sed) or it an be
3294e 20 68 65 6c 64 20 65 6e 74 69 72 65 6c 79 20 69   held entirely i
3294f 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 54 68 65  n memory..** The
32950 20 73 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65   sqlite3TempInMe
32951 6d 6f 72 79 28 29 20 66 75 6e 63 74 69 6f 6e 20  mory() function 
32952 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72  is used to deter
32953 6d 69 6e 65 20 77 68 69 63 68 2e 0a 2a 2f 0a 53  mine which..*/.S
32954 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
32955 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 61  t sqlite3BtreeFa
32956 63 74 6f 72 79 28 0a 20 20 63 6f 6e 73 74 20 73  ctory(.  const s
32957 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
32958 20 20 20 2f 2a 20 4d 61 69 6e 20 64 61 74 61 62     /* Main datab
32959 61 73 65 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67  ase when opening
3295a 20 61 75 78 20 6f 74 68 65 72 77 69 73 65 20 30   aux otherwise 0
3295b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
3295c 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20   *zFilename,    
3295d 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66  /* Name of the f
3295e 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ile containing t
3295f 68 65 20 42 54 72 65 65 20 64 61 74 61 62 61 73  he BTree databas
32960 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6d 69 74 4a  e */.  int omitJ
32961 6f 75 72 6e 61 6c 2c 20 20 20 20 20 20 20 20 20  ournal,         
32962 20 2f 2a 20 69 66 20 54 52 55 45 20 74 68 65 6e   /* if TRUE then
32963 20 64 6f 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c 20   do not journal 
32964 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69  this file */.  i
32965 6e 74 20 6e 43 61 63 68 65 2c 20 20 20 20 20 20  nt nCache,      
32966 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20           /* How 
32967 6d 61 6e 79 20 70 61 67 65 73 20 69 6e 20 74 68  many pages in th
32968 65 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f 0a  e page cache */.
32969 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 2c 20    int vfsFlags, 
3296a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
3296b 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72 6f  lags passed thro
3296c 75 67 68 20 74 6f 20 76 66 73 4f 70 65 6e 20 2a  ugh to vfsOpen *
3296d 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70 70 42 74  /.  Btree **ppBt
3296e 72 65 65 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ree           /*
3296f 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e 65 77 20   Pointer to new 
32970 42 74 72 65 65 20 6f 62 6a 65 63 74 20 77 72 69  Btree object wri
32971 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  tten here */.){.
32972 20 20 69 6e 74 20 62 74 46 6c 61 67 73 20 3d 20    int btFlags = 
32973 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 0a  0;.  int rc;.  .
32974 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
32975 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
32976 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
32977 65 72 74 28 20 70 70 42 74 72 65 65 20 21 3d 20  ert( ppBtree != 
32978 30 29 3b 0a 20 20 69 66 28 20 6f 6d 69 74 4a 6f  0);.  if( omitJo
32979 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 62 74 46  urnal ){.    btF
3297a 6c 61 67 73 20 7c 3d 20 42 54 52 45 45 5f 4f 4d  lags |= BTREE_OM
3297b 49 54 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 7d 0a  IT_JOURNAL;.  }.
3297c 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20    if( db->flags 
3297d 26 20 53 51 4c 49 54 45 5f 4e 6f 52 65 61 64 6c  & SQLITE_NoReadl
3297e 6f 63 6b 20 29 7b 0a 20 20 20 20 62 74 46 6c 61  ock ){.    btFla
3297f 67 73 20 7c 3d 20 42 54 52 45 45 5f 4e 4f 5f 52  gs |= BTREE_NO_R
32980 45 41 44 4c 4f 43 4b 3b 0a 20 20 7d 0a 23 69 66  EADLOCK;.  }.#if
32981 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
32982 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 69 66 28 20  _MEMORYDB.  if( 
32983 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 26 26 20  zFilename==0 && 
32984 73 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d  sqlite3TempInMem
32985 6f 72 79 28 64 62 29 20 29 7b 0a 20 20 20 20 7a  ory(db) ){.    z
32986 46 69 6c 65 6e 61 6d 65 20 3d 20 22 3a 6d 65 6d  Filename = ":mem
32987 6f 72 79 3a 22 3b 0a 20 20 7d 0a 23 65 6e 64 69  ory:";.  }.#endi
32988 66 0a 0a 20 20 69 66 28 20 28 76 66 73 46 6c 61  f..  if( (vfsFla
32989 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
3298a 5f 4d 41 49 4e 5f 44 42 29 21 3d 30 20 26 26 20  _MAIN_DB)!=0 && 
3298b 28 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c  (zFilename==0 ||
3298c 20 2a 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 29 20   *zFilename==0) 
3298d 29 7b 0a 20 20 20 20 76 66 73 46 6c 61 67 73 20  ){.    vfsFlags 
3298e 3d 20 28 76 66 73 46 6c 61 67 73 20 26 20 7e 53  = (vfsFlags & ~S
3298f 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
32990 44 42 29 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45  DB) | SQLITE_OPE
32991 4e 5f 54 45 4d 50 5f 44 42 3b 0a 20 20 7d 0a 20  N_TEMP_DB;.  }. 
32992 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
32993 65 65 4f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65  eeOpen(zFilename
32994 2c 20 28 73 71 6c 69 74 65 33 20 2a 29 64 62 2c  , (sqlite3 *)db,
32995 20 70 70 42 74 72 65 65 2c 20 62 74 46 6c 61 67   ppBtree, btFlag
32996 73 2c 20 76 66 73 46 6c 61 67 73 29 3b 0a 0a 20  s, vfsFlags);.. 
32997 20 2f 2a 20 49 66 20 74 68 65 20 42 2d 54 72 65   /* If the B-Tre
32998 65 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c  e was successful
32999 6c 79 20 6f 70 65 6e 65 64 2c 20 73 65 74 20 74  ly opened, set t
3299a 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 73  he pager-cache s
3299b 69 7a 65 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20  ize to the.  ** 
3299c 64 65 66 61 75 6c 74 20 76 61 6c 75 65 2e 20 45  default value. E
3299d 78 63 65 70 74 2c 20 69 66 20 74 68 65 20 63 61  xcept, if the ca
3299e 6c 6c 20 74 6f 20 42 74 72 65 65 4f 70 65 6e 28  ll to BtreeOpen(
3299f 29 20 72 65 74 75 72 6e 65 64 20 61 20 68 61 6e  ) returned a han
329a0 64 6c 65 0a 20 20 2a 2a 20 6f 70 65 6e 20 6f 6e  dle.  ** open on
329a1 20 61 6e 20 65 78 69 73 74 69 6e 67 20 73 68 61   an existing sha
329a2 72 65 64 20 70 61 67 65 72 2d 63 61 63 68 65 2c  red pager-cache,
329a3 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20 74   do not change t
329a4 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 0a  he pager-cache .
329a5 20 20 2a 2a 20 73 69 7a 65 2e 0a 20 20 2a 2f 0a    ** size..  */.
329a6 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
329a7 5f 4f 4b 20 26 26 20 30 3d 3d 73 71 6c 69 74 65  _OK && 0==sqlite
329a8 33 42 74 72 65 65 53 63 68 65 6d 61 28 2a 70 70  3BtreeSchema(*pp
329a9 42 74 72 65 65 2c 20 30 2c 20 30 29 20 29 7b 0a  Btree, 0, 0) ){.
329aa 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
329ab 53 65 74 43 61 63 68 65 53 69 7a 65 28 2a 70 70  SetCacheSize(*pp
329ac 42 74 72 65 65 2c 20 6e 43 61 63 68 65 29 3b 0a  Btree, nCache);.
329ad 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
329ae 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
329af 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 45   UTF-8 encoded E
329b0 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 20  nglish language 
329b1 65 78 70 6c 61 6e 61 74 69 6f 6e 20 6f 66 20 74  explanation of t
329b2 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 0a 2a  he most recent.*
329b3 2a 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49  * error..*/.SQLI
329b4 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61  TE_API const cha
329b5 72 20 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  r *sqlite3_errms
329b6 67 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  g(sqlite3 *db){.
329b7 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b    const char *z;
329b8 0a 20 20 69 66 28 20 21 64 62 20 29 7b 0a 20 20  .  if( !db ){.  
329b9 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
329ba 45 72 72 53 74 72 28 53 51 4c 49 54 45 5f 4e 4f  ErrStr(SQLITE_NO
329bb 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  MEM);.  }.  if( 
329bc 21 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68  !sqlite3SafetyCh
329bd 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20  eckSickOrOk(db) 
329be 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71  ){.    return sq
329bf 6c 69 74 65 33 45 72 72 53 74 72 28 53 51 4c 49  lite3ErrStr(SQLI
329c0 54 45 5f 4d 49 53 55 53 45 29 3b 0a 20 20 7d 0a  TE_MISUSE);.  }.
329c1 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
329c2 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
329c3 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  ;.  if( db->mall
329c4 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
329c5 7a 20 3d 20 73 71 6c 69 74 65 33 45 72 72 53 74  z = sqlite3ErrSt
329c6 72 28 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b  r(SQLITE_NOMEM);
329c7 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20  .  }else{.    z 
329c8 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33  = (char*)sqlite3
329c9 5f 76 61 6c 75 65 5f 74 65 78 74 28 64 62 2d 3e  _value_text(db->
329ca 70 45 72 72 29 3b 0a 20 20 20 20 61 73 73 65 72  pErr);.    asser
329cb 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  t( !db->mallocFa
329cc 69 6c 65 64 20 29 3b 0a 20 20 20 20 69 66 28 20  iled );.    if( 
329cd 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 7a 20  z==0 ){.      z 
329ce 3d 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28  = sqlite3ErrStr(
329cf 64 62 2d 3e 65 72 72 43 6f 64 65 29 3b 0a 20 20  db->errCode);.  
329d0 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65    }.  }.  sqlite
329d1 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
329d2 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
329d3 72 6e 20 7a 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  rn z;.}..#ifndef
329d4 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
329d5 31 36 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  16./*.** Return 
329d6 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 45  UTF-16 encoded E
329d7 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 20  nglish language 
329d8 65 78 70 6c 61 6e 61 74 69 6f 6e 20 6f 66 20 74  explanation of t
329d9 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 0a 2a  he most recent.*
329da 2a 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49  * error..*/.SQLI
329db 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
329dc 64 20 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  d *sqlite3_errms
329dd 67 31 36 28 73 71 6c 69 74 65 33 20 2a 64 62 29  g16(sqlite3 *db)
329de 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  {.  static const
329df 20 75 31 36 20 6f 75 74 4f 66 4d 65 6d 5b 5d 20   u16 outOfMem[] 
329e0 3d 20 7b 0a 20 20 20 20 27 6f 27 2c 20 27 75 27  = {.    'o', 'u'
329e1 2c 20 27 74 27 2c 20 27 20 27 2c 20 27 6f 27 2c  , 't', ' ', 'o',
329e2 20 27 66 27 2c 20 27 20 27 2c 20 27 6d 27 2c 20   'f', ' ', 'm', 
329e3 27 65 27 2c 20 27 6d 27 2c 20 27 6f 27 2c 20 27  'e', 'm', 'o', '
329e4 72 27 2c 20 27 79 27 2c 20 30 0a 20 20 7d 3b 0a  r', 'y', 0.  };.
329e5 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
329e6 31 36 20 6d 69 73 75 73 65 5b 5d 20 3d 20 7b 0a  16 misuse[] = {.
329e7 20 20 20 20 27 6c 27 2c 20 27 69 27 2c 20 27 62      'l', 'i', 'b
329e8 27 2c 20 27 72 27 2c 20 27 61 27 2c 20 27 72 27  ', 'r', 'a', 'r'
329e9 2c 20 27 79 27 2c 20 27 20 27 2c 20 0a 20 20 20  , 'y', ' ', .   
329ea 20 27 72 27 2c 20 27 6f 27 2c 20 27 75 27 2c 20   'r', 'o', 'u', 
329eb 27 74 27 2c 20 27 69 27 2c 20 27 6e 27 2c 20 27  't', 'i', 'n', '
329ec 65 27 2c 20 27 20 27 2c 20 0a 20 20 20 20 27 63  e', ' ', .    'c
329ed 27 2c 20 27 61 27 2c 20 27 6c 27 2c 20 27 6c 27  ', 'a', 'l', 'l'
329ee 2c 20 27 65 27 2c 20 27 64 27 2c 20 27 20 27 2c  , 'e', 'd', ' ',
329ef 20 0a 20 20 20 20 27 6f 27 2c 20 27 75 27 2c 20   .    'o', 'u', 
329f0 27 74 27 2c 20 27 20 27 2c 20 0a 20 20 20 20 27  't', ' ', .    '
329f1 6f 27 2c 20 27 66 27 2c 20 27 20 27 2c 20 0a 20  o', 'f', ' ', . 
329f2 20 20 20 27 73 27 2c 20 27 65 27 2c 20 27 71 27     's', 'e', 'q'
329f3 2c 20 27 75 27 2c 20 27 65 27 2c 20 27 6e 27 2c  , 'u', 'e', 'n',
329f4 20 27 63 27 2c 20 27 65 27 2c 20 30 0a 20 20 7d   'c', 'e', 0.  }
329f5 3b 0a 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  ;..  const void 
329f6 2a 7a 3b 0a 20 20 69 66 28 20 21 64 62 20 29 7b  *z;.  if( !db ){
329f7 0a 20 20 20 20 72 65 74 75 72 6e 20 28 76 6f 69  .    return (voi
329f8 64 20 2a 29 6f 75 74 4f 66 4d 65 6d 3b 0a 20 20  d *)outOfMem;.  
329f9 7d 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33  }.  if( !sqlite3
329fa 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f  SafetyCheckSickO
329fb 72 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20 20 72  rOk(db) ){.    r
329fc 65 74 75 72 6e 20 28 76 6f 69 64 20 2a 29 6d 69  eturn (void *)mi
329fd 73 75 73 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  suse;.  }.  sqli
329fe 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
329ff 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66  db->mutex);.  if
32a00 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
32a01 65 64 20 29 7b 0a 20 20 20 20 7a 20 3d 20 28 76  ed ){.    z = (v
32a02 6f 69 64 20 2a 29 6f 75 74 4f 66 4d 65 6d 3b 0a  oid *)outOfMem;.
32a03 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d    }else{.    z =
32a04 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
32a05 65 78 74 31 36 28 64 62 2d 3e 70 45 72 72 29 3b  ext16(db->pErr);
32a06 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b  .    if( z==0 ){
32a07 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61  .      sqlite3Va
32a08 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45  lueSetStr(db->pE
32a09 72 72 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 45  rr, -1, sqlite3E
32a0a 72 72 53 74 72 28 64 62 2d 3e 65 72 72 43 6f 64  rrStr(db->errCod
32a0b 65 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 53  e),.           S
32a0c 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49  QLITE_UTF8, SQLI
32a0d 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
32a0e 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61    z = sqlite3_va
32a0f 6c 75 65 5f 74 65 78 74 31 36 28 64 62 2d 3e 70  lue_text16(db->p
32a10 45 72 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Err);.    }.    
32a11 2f 2a 20 41 20 6d 61 6c 6c 6f 63 28 29 20 6d 61  /* A malloc() ma
32a12 79 20 68 61 76 65 20 66 61 69 6c 65 64 20 77 69  y have failed wi
32a13 74 68 69 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f  thin the call to
32a14 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
32a15 65 78 74 31 36 28 29 0a 20 20 20 20 2a 2a 20 61  ext16().    ** a
32a16 62 6f 76 65 2e 20 49 66 20 74 68 69 73 20 69 73  bove. If this is
32a17 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e 20   the case, then 
32a18 74 68 65 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  the db->mallocFa
32a19 69 6c 65 64 20 66 6c 61 67 20 6e 65 65 64 73 20  iled flag needs 
32a1a 74 6f 0a 20 20 20 20 2a 2a 20 62 65 20 63 6c 65  to.    ** be cle
32a1b 61 72 65 64 20 62 65 66 6f 72 65 20 72 65 74 75  ared before retu
32a1c 72 6e 69 6e 67 2e 20 44 6f 20 74 68 69 73 20 64  rning. Do this d
32a1d 69 72 65 63 74 6c 79 2c 20 69 6e 73 74 65 61 64  irectly, instead
32a1e 20 6f 66 20 76 69 61 0a 20 20 20 20 2a 2a 20 73   of via.    ** s
32a1f 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 29 2c  qlite3ApiExit(),
32a20 20 74 6f 20 61 76 6f 69 64 20 73 65 74 74 69 6e   to avoid settin
32a21 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  g the database h
32a22 61 6e 64 6c 65 20 65 72 72 6f 72 20 6d 65 73 73  andle error mess
32a23 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  age..    */.    
32a24 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
32a25 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69   = 0;.  }.  sqli
32a26 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
32a27 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
32a28 74 75 72 6e 20 7a 3b 0a 7d 0a 23 65 6e 64 69 66  turn z;.}.#endif
32a29 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
32a2a 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  UTF16 */../*.** 
32a2b 52 65 74 75 72 6e 20 74 68 65 20 6d 6f 73 74 20  Return the most 
32a2c 72 65 63 65 6e 74 20 65 72 72 6f 72 20 63 6f 64  recent error cod
32a2d 65 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 61  e generated by a
32a2e 6e 20 53 51 4c 69 74 65 20 72 6f 75 74 69 6e 65  n SQLite routine
32a2f 2e 20 49 66 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20  . If NULL is.** 
32a30 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66  passed to this f
32a31 75 6e 63 74 69 6f 6e 2c 20 77 65 20 61 73 73 75  unction, we assu
32a32 6d 65 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61  me a malloc() fa
32a33 69 6c 65 64 20 64 75 72 69 6e 67 20 73 71 6c 69  iled during sqli
32a34 74 65 33 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a 53  te3_open()..*/.S
32a35 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
32a36 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 73 71  lite3_errcode(sq
32a37 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 66  lite3 *db){.  if
32a38 28 20 64 62 20 26 26 20 21 73 71 6c 69 74 65 33  ( db && !sqlite3
32a39 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f  SafetyCheckSickO
32a3a 72 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20 20 72  rOk(db) ){.    r
32a3b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
32a3c 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21  USE;.  }.  if( !
32a3d 64 62 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  db || db->malloc
32a3e 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65  Failed ){.    re
32a3f 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
32a40 4d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  M;.  }.  return 
32a41 64 62 2d 3e 65 72 72 43 6f 64 65 20 26 20 64 62  db->errCode & db
32a42 2d 3e 65 72 72 4d 61 73 6b 3b 0a 7d 0a 53 51 4c  ->errMask;.}.SQL
32a43 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
32a44 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72  te3_extended_err
32a45 63 6f 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62  code(sqlite3 *db
32a46 29 7b 0a 20 20 69 66 28 20 64 62 20 26 26 20 21  ){.  if( db && !
32a47 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65  sqlite3SafetyChe
32a48 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29  ckSickOrOk(db) )
32a49 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
32a4a 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a  ITE_MISUSE;.  }.
32a4b 20 20 69 66 28 20 21 64 62 20 7c 7c 20 64 62 2d    if( !db || db-
32a4c 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
32a4d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
32a4e 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
32a4f 72 65 74 75 72 6e 20 64 62 2d 3e 65 72 72 43 6f  return db->errCo
32a50 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  de;.}../*.** Cre
32a51 61 74 65 20 61 20 6e 65 77 20 63 6f 6c 6c 61 74  ate a new collat
32a52 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  ing function for
32a53 20 64 61 74 61 62 61 73 65 20 22 64 62 22 2e 20   database "db". 
32a54 20 54 68 65 20 6e 61 6d 65 20 69 73 20 7a 4e 61   The name is zNa
32a55 6d 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20 65 6e  me.** and the en
32a56 63 6f 64 69 6e 67 20 69 73 20 65 6e 63 2e 0a 2a  coding is enc..*
32a57 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 72 65  /.static int cre
32a58 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20  ateCollation(.  
32a59 73 71 6c 69 74 65 33 2a 20 64 62 2c 0a 20 20 63  sqlite3* db,.  c
32a5a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
32a5b 2c 20 0a 20 20 75 38 20 65 6e 63 2c 0a 20 20 75  , .  u8 enc,.  u
32a5c 38 20 63 6f 6c 6c 54 79 70 65 2c 0a 20 20 76 6f  8 collType,.  vo
32a5d 69 64 2a 20 70 43 74 78 2c 0a 20 20 69 6e 74 28  id* pCtx,.  int(
32a5e 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a  *xCompare)(void*
32a5f 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
32a60 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
32a61 29 2c 0a 20 20 76 6f 69 64 28 2a 78 44 65 6c 29  ),.  void(*xDel)
32a62 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 43 6f 6c  (void*).){.  Col
32a63 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69  lSeq *pColl;.  i
32a64 6e 74 20 65 6e 63 32 3b 0a 20 20 69 6e 74 20 6e  nt enc2;.  int n
32a65 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74  Name = sqlite3St
32a66 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20  rlen30(zName);. 
32a67 20 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69   .  assert( sqli
32a68 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
32a69 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20  b->mutex) );..  
32a6a 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f 55 54 46  /* If SQLITE_UTF
32a6b 31 36 20 69 73 20 73 70 65 63 69 66 69 65 64 20  16 is specified 
32a6c 61 73 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  as the encoding 
32a6d 74 79 70 65 2c 20 74 72 61 6e 73 66 6f 72 6d 20  type, transform 
32a6e 74 68 69 73 0a 20 20 2a 2a 20 74 6f 20 6f 6e 65  this.  ** to one
32a6f 20 6f 66 20 53 51 4c 49 54 45 5f 55 54 46 31 36   of SQLITE_UTF16
32a70 4c 45 20 6f 72 20 53 51 4c 49 54 45 5f 55 54 46  LE or SQLITE_UTF
32a71 31 36 42 45 20 75 73 69 6e 67 20 74 68 65 0a 20  16BE using the. 
32a72 20 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 31 36   ** SQLITE_UTF16
32a73 4e 41 54 49 56 45 20 6d 61 63 72 6f 2e 20 53 51  NATIVE macro. SQ
32a74 4c 49 54 45 5f 55 54 46 31 36 20 69 73 20 6e 6f  LITE_UTF16 is no
32a75 74 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c  t used internall
32a76 79 2e 0a 20 20 2a 2f 0a 20 20 65 6e 63 32 20 3d  y..  */.  enc2 =
32a77 20 65 6e 63 3b 0a 20 20 74 65 73 74 63 61 73 65   enc;.  testcase
32a78 28 20 65 6e 63 32 3d 3d 53 51 4c 49 54 45 5f 55  ( enc2==SQLITE_U
32a79 54 46 31 36 20 29 3b 0a 20 20 74 65 73 74 63 61  TF16 );.  testca
32a7a 73 65 28 20 65 6e 63 32 3d 3d 53 51 4c 49 54 45  se( enc2==SQLITE
32a7b 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 20 29  _UTF16_ALIGNED )
32a7c 3b 0a 20 20 69 66 28 20 65 6e 63 32 3d 3d 53 51  ;.  if( enc2==SQ
32a7d 4c 49 54 45 5f 55 54 46 31 36 20 7c 7c 20 65 6e  LITE_UTF16 || en
32a7e 63 32 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  c2==SQLITE_UTF16
32a7f 5f 41 4c 49 47 4e 45 44 20 29 7b 0a 20 20 20 20  _ALIGNED ){.    
32a80 65 6e 63 32 20 3d 20 53 51 4c 49 54 45 5f 55 54  enc2 = SQLITE_UT
32a81 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 7d 0a 20  F16NATIVE;.  }. 
32a82 20 69 66 28 20 65 6e 63 32 3c 53 51 4c 49 54 45   if( enc2<SQLITE
32a83 5f 55 54 46 38 20 7c 7c 20 65 6e 63 32 3e 53 51  _UTF8 || enc2>SQ
32a84 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 7b 0a  LITE_UTF16BE ){.
32a85 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
32a86 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 0a 20  E_MISUSE;.  }.. 
32a87 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 74 68 69   /* Check if thi
32a88 73 20 63 61 6c 6c 20 69 73 20 72 65 6d 6f 76 69  s call is removi
32a89 6e 67 20 6f 72 20 72 65 70 6c 61 63 69 6e 67 20  ng or replacing 
32a8a 61 6e 20 65 78 69 73 74 69 6e 67 20 63 6f 6c 6c  an existing coll
32a8b 61 74 69 6f 6e 20 0a 20 20 2a 2a 20 73 65 71 75  ation .  ** sequ
32a8c 65 6e 63 65 2e 20 49 66 20 73 6f 2c 20 61 6e 64  ence. If so, and
32a8d 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76   there are activ
32a8e 65 20 56 4d 73 2c 20 72 65 74 75 72 6e 20 62 75  e VMs, return bu
32a8f 73 79 2e 20 49 66 20 74 68 65 72 65 0a 20 20 2a  sy. If there.  *
32a90 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20  * are no active 
32a91 56 4d 73 2c 20 69 6e 76 61 6c 69 64 61 74 65 20  VMs, invalidate 
32a92 61 6e 79 20 70 72 65 2d 63 6f 6d 70 69 6c 65 64  any pre-compiled
32a93 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 2a   statements..  *
32a94 2f 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69  /.  pColl = sqli
32a95 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64  te3FindCollSeq(d
32a96 62 2c 20 28 75 38 29 65 6e 63 32 2c 20 7a 4e 61  b, (u8)enc2, zNa
32a97 6d 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 43  me, 0);.  if( pC
32a98 6f 6c 6c 20 26 26 20 70 43 6f 6c 6c 2d 3e 78 43  oll && pColl->xC
32a99 6d 70 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62  mp ){.    if( db
32a9a 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 20  ->activeVdbeCnt 
32a9b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
32a9c 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45  Error(db, SQLITE
32a9d 5f 42 55 53 59 2c 20 0a 20 20 20 20 20 20 20 20  _BUSY, .        
32a9e 22 75 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 74  "unable to delet
32a9f 65 2f 6d 6f 64 69 66 79 20 63 6f 6c 6c 61 74 69  e/modify collati
32aa0 6f 6e 20 73 65 71 75 65 6e 63 65 20 64 75 65 20  on sequence due 
32aa1 74 6f 20 61 63 74 69 76 65 20 73 74 61 74 65 6d  to active statem
32aa2 65 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 72 65  ents");.      re
32aa3 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
32aa4 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
32aa5 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65  te3ExpirePrepare
32aa6 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b  dStatements(db);
32aa7 0a 0a 20 20 20 20 2f 2a 20 49 66 20 63 6f 6c 6c  ..    /* If coll
32aa8 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 70  ation sequence p
32aa9 43 6f 6c 6c 20 77 61 73 20 63 72 65 61 74 65 64  Coll was created
32aaa 20 64 69 72 65 63 74 6c 79 20 62 79 20 61 20 63   directly by a c
32aab 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71  all to.    ** sq
32aac 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
32aad 6c 61 74 69 6f 6e 2c 20 61 6e 64 20 6e 6f 74 20  lation, and not 
32aae 67 65 6e 65 72 61 74 65 64 20 62 79 20 73 79 6e  generated by syn
32aaf 74 68 43 6f 6c 6c 53 65 71 28 29 2c 0a 20 20 20  thCollSeq(),.   
32ab0 20 2a 2a 20 74 68 65 6e 20 61 6e 79 20 63 6f 70   ** then any cop
32ab1 69 65 73 20 6d 61 64 65 20 62 79 20 73 79 6e 74  ies made by synt
32ab2 68 43 6f 6c 6c 53 65 71 28 29 20 6e 65 65 64 20  hCollSeq() need 
32ab3 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65  to be invalidate
32ab4 64 2e 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20  d..    ** Also, 
32ab5 63 6f 6c 6c 61 74 69 6f 6e 20 64 65 73 74 72 75  collation destru
32ab6 63 74 6f 72 20 2d 20 43 6f 6c 6c 53 65 71 2e 78  ctor - CollSeq.x
32ab7 44 65 6c 28 29 20 2d 20 66 75 6e 63 74 69 6f 6e  Del() - function
32ab8 20 6d 61 79 20 6e 65 65 64 0a 20 20 20 20 2a 2a   may need.    **
32ab9 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 2e 0a 20   to be called.. 
32aba 20 20 20 2a 2f 20 0a 20 20 20 20 69 66 28 20 28     */ .    if( (
32abb 70 43 6f 6c 6c 2d 3e 65 6e 63 20 26 20 7e 53 51  pColl->enc & ~SQ
32abc 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e  LITE_UTF16_ALIGN
32abd 45 44 29 3d 3d 65 6e 63 32 20 29 7b 0a 20 20 20  ED)==enc2 ){.   
32abe 20 20 20 43 6f 6c 6c 53 65 71 20 2a 61 43 6f 6c     CollSeq *aCol
32abf 6c 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46  l = sqlite3HashF
32ac0 69 6e 64 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65  ind(&db->aCollSe
32ac1 71 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  q, zName, nName)
32ac2 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20  ;.      int j;. 
32ac3 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
32ac4 33 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  3; j++){.       
32ac5 20 43 6f 6c 6c 53 65 71 20 2a 70 20 3d 20 26 61   CollSeq *p = &a
32ac6 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 20  Coll[j];.       
32ac7 20 69 66 28 20 70 2d 3e 65 6e 63 3d 3d 70 43 6f   if( p->enc==pCo
32ac8 6c 6c 2d 3e 65 6e 63 20 29 7b 0a 20 20 20 20 20  ll->enc ){.     
32ac9 20 20 20 20 20 69 66 28 20 70 2d 3e 78 44 65 6c       if( p->xDel
32aca 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
32acb 70 2d 3e 78 44 65 6c 28 70 2d 3e 70 55 73 65 72  p->xDel(p->pUser
32acc 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
32acd 20 20 20 20 20 20 20 20 20 70 2d 3e 78 43 6d 70           p->xCmp
32ace 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 0;.        }.
32acf 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
32ad0 7d 0a 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c  }..  pColl = sql
32ad1 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28  ite3FindCollSeq(
32ad2 64 62 2c 20 28 75 38 29 65 6e 63 32 2c 20 7a 4e  db, (u8)enc2, zN
32ad3 61 6d 65 2c 20 31 29 3b 0a 20 20 69 66 28 20 70  ame, 1);.  if( p
32ad4 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 70 43 6f 6c  Coll ){.    pCol
32ad5 6c 2d 3e 78 43 6d 70 20 3d 20 78 43 6f 6d 70 61  l->xCmp = xCompa
32ad6 72 65 3b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e 70  re;.    pColl->p
32ad7 55 73 65 72 20 3d 20 70 43 74 78 3b 0a 20 20 20  User = pCtx;.   
32ad8 20 70 43 6f 6c 6c 2d 3e 78 44 65 6c 20 3d 20 78   pColl->xDel = x
32ad9 44 65 6c 3b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e  Del;.    pColl->
32ada 65 6e 63 20 3d 20 28 75 38 29 28 65 6e 63 32 20  enc = (u8)(enc2 
32adb 7c 20 28 65 6e 63 20 26 20 53 51 4c 49 54 45 5f  | (enc & SQLITE_
32adc 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 29 3b  UTF16_ALIGNED));
32add 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e 74 79 70 65  .    pColl->type
32ade 20 3d 20 63 6f 6c 6c 54 79 70 65 3b 0a 20 20 7d   = collType;.  }
32adf 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28  .  sqlite3Error(
32ae0 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30  db, SQLITE_OK, 0
32ae1 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
32ae2 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  TE_OK;.}.../*.**
32ae3 20 54 68 69 73 20 61 72 72 61 79 20 64 65 66 69   This array defi
32ae4 6e 65 73 20 68 61 72 64 20 75 70 70 65 72 20 62  nes hard upper b
32ae5 6f 75 6e 64 73 20 6f 6e 20 6c 69 6d 69 74 20 76  ounds on limit v
32ae6 61 6c 75 65 73 2e 20 20 54 68 65 0a 2a 2a 20 69  alues.  The.** i
32ae7 6e 69 74 69 61 6c 69 7a 65 72 20 6d 75 73 74 20  nitializer must 
32ae8 62 65 20 6b 65 70 74 20 69 6e 20 73 79 6e 63 20  be kept in sync 
32ae9 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f  with the SQLITE_
32aea 4c 49 4d 49 54 5f 2a 0a 2a 2a 20 23 64 65 66 69  LIMIT_*.** #defi
32aeb 6e 65 73 20 69 6e 20 73 71 6c 69 74 65 33 2e 68  nes in sqlite3.h
32aec 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
32aed 74 20 69 6e 74 20 61 48 61 72 64 4c 69 6d 69 74  t int aHardLimit
32aee 5b 5d 20 3d 20 7b 0a 20 20 53 51 4c 49 54 45 5f  [] = {.  SQLITE_
32aef 4d 41 58 5f 4c 45 4e 47 54 48 2c 0a 20 20 53 51  MAX_LENGTH,.  SQ
32af0 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e  LITE_MAX_SQL_LEN
32af1 47 54 48 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41  GTH,.  SQLITE_MA
32af2 58 5f 43 4f 4c 55 4d 4e 2c 0a 20 20 53 51 4c 49  X_COLUMN,.  SQLI
32af3 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54  TE_MAX_EXPR_DEPT
32af4 48 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f  H,.  SQLITE_MAX_
32af5 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 2c  COMPOUND_SELECT,
32af6 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 44  .  SQLITE_MAX_VD
32af7 42 45 5f 4f 50 2c 0a 20 20 53 51 4c 49 54 45 5f  BE_OP,.  SQLITE_
32af8 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47  MAX_FUNCTION_ARG
32af9 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41  ,.  SQLITE_MAX_A
32afa 54 54 41 43 48 45 44 2c 0a 20 20 53 51 4c 49 54  TTACHED,.  SQLIT
32afb 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45  E_MAX_LIKE_PATTE
32afc 52 4e 5f 4c 45 4e 47 54 48 2c 0a 20 20 53 51 4c  RN_LENGTH,.  SQL
32afd 49 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45  ITE_MAX_VARIABLE
32afe 5f 4e 55 4d 42 45 52 2c 0a 20 20 53 51 4c 49 54  _NUMBER,.  SQLIT
32aff 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f 44 45  E_MAX_TRIGGER_DE
32b00 50 54 48 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d  PTH,.};../*.** M
32b01 61 6b 65 20 73 75 72 65 20 74 68 65 20 68 61 72  ake sure the har
32b02 64 20 6c 69 6d 69 74 73 20 61 72 65 20 73 65 74  d limits are set
32b03 20 74 6f 20 72 65 61 73 6f 6e 61 62 6c 65 20 76   to reasonable v
32b04 61 6c 75 65 73 0a 2a 2f 0a 23 69 66 20 53 51 4c  alues.*/.#if SQL
32b05 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 3c 31  ITE_MAX_LENGTH<1
32b06 30 30 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54  00.# error SQLIT
32b07 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 6d 75 73  E_MAX_LENGTH mus
32b08 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 31 30  t be at least 10
32b09 30 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c  0.#endif.#if SQL
32b0a 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47  ITE_MAX_SQL_LENG
32b0b 54 48 3c 31 30 30 0a 23 20 65 72 72 6f 72 20 53  TH<100.# error S
32b0c 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45  QLITE_MAX_SQL_LE
32b0d 4e 47 54 48 20 6d 75 73 74 20 62 65 20 61 74 20  NGTH must be at 
32b0e 6c 65 61 73 74 20 31 30 30 0a 23 65 6e 64 69 66  least 100.#endif
32b0f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
32b10 53 51 4c 5f 4c 45 4e 47 54 48 3e 53 51 4c 49 54  SQL_LENGTH>SQLIT
32b11 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a 23 20 65  E_MAX_LENGTH.# e
32b12 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f  rror SQLITE_MAX_
32b13 53 51 4c 5f 4c 45 4e 47 54 48 20 6d 75 73 74 20  SQL_LENGTH must 
32b14 6e 6f 74 20 62 65 20 67 72 65 61 74 65 72 20 74  not be greater t
32b15 68 61 6e 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c  han SQLITE_MAX_L
32b16 45 4e 47 54 48 0a 23 65 6e 64 69 66 0a 23 69 66  ENGTH.#endif.#if
32b17 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50   SQLITE_MAX_COMP
32b18 4f 55 4e 44 5f 53 45 4c 45 43 54 3c 32 0a 23 20  OUND_SELECT<2.# 
32b19 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58  error SQLITE_MAX
32b1a 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
32b1b 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 73   must be at leas
32b1c 74 20 32 0a 23 65 6e 64 69 66 0a 23 69 66 20 53  t 2.#endif.#if S
32b1d 51 4c 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f 4f  QLITE_MAX_VDBE_O
32b1e 50 3c 34 30 0a 23 20 65 72 72 6f 72 20 53 51 4c  P<40.# error SQL
32b1f 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 20  ITE_MAX_VDBE_OP 
32b20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 73 74  must be at least
32b21 20 34 30 0a 23 65 6e 64 69 66 0a 23 69 66 20 53   40.#endif.#if S
32b22 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49  QLITE_MAX_FUNCTI
32b23 4f 4e 5f 41 52 47 3c 30 20 7c 7c 20 53 51 4c 49  ON_ARG<0 || SQLI
32b24 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f  TE_MAX_FUNCTION_
32b25 41 52 47 3e 31 30 30 30 0a 23 20 65 72 72 6f 72  ARG>1000.# error
32b26 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43   SQLITE_MAX_FUNC
32b27 54 49 4f 4e 5f 41 52 47 20 6d 75 73 74 20 62 65  TION_ARG must be
32b28 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 31   between 0 and 1
32b29 30 30 30 0a 23 65 6e 64 69 66 0a 23 69 66 20 53  000.#endif.#if S
32b2a 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48  QLITE_MAX_ATTACH
32b2b 45 44 3c 30 20 7c 7c 20 53 51 4c 49 54 45 5f 4d  ED<0 || SQLITE_M
32b2c 41 58 5f 41 54 54 41 43 48 45 44 3e 33 30 0a 23  AX_ATTACHED>30.#
32b2d 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41   error SQLITE_MA
32b2e 58 5f 41 54 54 41 43 48 45 44 20 6d 75 73 74 20  X_ATTACHED must 
32b2f 62 65 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64  be between 0 and
32b30 20 33 30 0a 23 65 6e 64 69 66 0a 23 69 66 20 53   30.#endif.#if S
32b31 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50  QLITE_MAX_LIKE_P
32b32 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 3c 31 0a  ATTERN_LENGTH<1.
32b33 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d  # error SQLITE_M
32b34 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f  AX_LIKE_PATTERN_
32b35 4c 45 4e 47 54 48 20 6d 75 73 74 20 62 65 20 61  LENGTH must be a
32b36 74 20 6c 65 61 73 74 20 31 0a 23 65 6e 64 69 66  t least 1.#endif
32b37 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
32b38 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 3c  VARIABLE_NUMBER<
32b39 31 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45  1.# error SQLITE
32b3a 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55  _MAX_VARIABLE_NU
32b3b 4d 42 45 52 20 6d 75 73 74 20 62 65 20 61 74 20  MBER must be at 
32b3c 6c 65 61 73 74 20 31 0a 23 65 6e 64 69 66 0a 23  least 1.#endif.#
32b3d 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f  if SQLITE_MAX_CO
32b3e 4c 55 4d 4e 3e 33 32 37 36 37 0a 23 20 65 72 72  LUMN>32767.# err
32b3f 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f  or SQLITE_MAX_CO
32b40 4c 55 4d 4e 20 6d 75 73 74 20 6e 6f 74 20 65 78  LUMN must not ex
32b41 63 65 65 64 20 33 32 37 36 37 0a 23 65 6e 64 69  ceed 32767.#endi
32b42 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  f.#if SQLITE_MAX
32b43 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 3c 31  _TRIGGER_DEPTH<1
32b44 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f  .# error SQLITE_
32b45 4d 41 58 5f 54 52 49 47 47 45 52 5f 44 45 50 54  MAX_TRIGGER_DEPT
32b46 48 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61  H must be at lea
32b47 73 74 20 31 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  st 1.#endif.../*
32b48 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76  .** Change the v
32b49 61 6c 75 65 20 6f 66 20 61 20 6c 69 6d 69 74 2e  alue of a limit.
32b4a 20 20 52 65 70 6f 72 74 20 74 68 65 20 6f 6c 64    Report the old
32b4b 20 76 61 6c 75 65 2e 0a 2a 2a 20 49 66 20 61 6e   value..** If an
32b4c 20 69 6e 76 61 6c 69 64 20 6c 69 6d 69 74 20 69   invalid limit i
32b4d 6e 64 65 78 20 69 73 20 73 75 70 70 6c 69 65 64  ndex is supplied
32b4e 2c 20 72 65 70 6f 72 74 20 2d 31 2e 0a 2a 2a 20  , report -1..** 
32b4f 4d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 20  Make no changes 
32b50 62 75 74 20 73 74 69 6c 6c 20 72 65 70 6f 72 74  but still report
32b51 20 74 68 65 20 6f 6c 64 20 76 61 6c 75 65 20 69   the old value i
32b52 66 20 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 69 6d  f the.** new lim
32b53 69 74 20 69 73 20 6e 65 67 61 74 69 76 65 2e 0a  it is negative..
32b54 2a 2a 0a 2a 2a 20 41 20 6e 65 77 20 6c 6f 77 65  **.** A new lowe
32b55 72 20 6c 69 6d 69 74 20 64 6f 65 73 20 6e 6f 74  r limit does not
32b56 20 73 68 72 69 6e 6b 20 65 78 69 73 74 69 6e 67   shrink existing
32b57 20 63 6f 6e 73 74 72 75 63 74 73 2e 0a 2a 2a 20   constructs..** 
32b58 49 74 20 6d 65 72 65 6c 79 20 70 72 65 76 65 6e  It merely preven
32b59 74 73 20 6e 65 77 20 63 6f 6e 73 74 72 75 63 74  ts new construct
32b5a 73 20 74 68 61 74 20 65 78 63 65 65 64 20 74 68  s that exceed th
32b5b 65 20 6c 69 6d 69 74 0a 2a 2a 20 66 72 6f 6d 20  e limit.** from 
32b5c 66 6f 72 6d 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49  forming..*/.SQLI
32b5d 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
32b5e 65 33 5f 6c 69 6d 69 74 28 73 71 6c 69 74 65 33  e3_limit(sqlite3
32b5f 20 2a 64 62 2c 20 69 6e 74 20 6c 69 6d 69 74 49   *db, int limitI
32b60 64 2c 20 69 6e 74 20 6e 65 77 4c 69 6d 69 74 29  d, int newLimit)
32b61 7b 0a 20 20 69 6e 74 20 6f 6c 64 4c 69 6d 69 74  {.  int oldLimit
32b62 3b 0a 20 20 69 66 28 20 6c 69 6d 69 74 49 64 3c  ;.  if( limitId<
32b63 30 20 7c 7c 20 6c 69 6d 69 74 49 64 3e 3d 53 51  0 || limitId>=SQ
32b64 4c 49 54 45 5f 4e 5f 4c 49 4d 49 54 20 29 7b 0a  LITE_N_LIMIT ){.
32b65 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20      return -1;. 
32b66 20 7d 0a 20 20 6f 6c 64 4c 69 6d 69 74 20 3d 20   }.  oldLimit = 
32b67 64 62 2d 3e 61 4c 69 6d 69 74 5b 6c 69 6d 69 74  db->aLimit[limit
32b68 49 64 5d 3b 0a 20 20 69 66 28 20 6e 65 77 4c 69  Id];.  if( newLi
32b69 6d 69 74 3e 3d 30 20 29 7b 0a 20 20 20 20 69 66  mit>=0 ){.    if
32b6a 28 20 6e 65 77 4c 69 6d 69 74 3e 61 48 61 72 64  ( newLimit>aHard
32b6b 4c 69 6d 69 74 5b 6c 69 6d 69 74 49 64 5d 20 29  Limit[limitId] )
32b6c 7b 0a 20 20 20 20 20 20 6e 65 77 4c 69 6d 69 74  {.      newLimit
32b6d 20 3d 20 61 48 61 72 64 4c 69 6d 69 74 5b 6c 69   = aHardLimit[li
32b6e 6d 69 74 49 64 5d 3b 0a 20 20 20 20 7d 0a 20 20  mitId];.    }.  
32b6f 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 6c 69 6d    db->aLimit[lim
32b70 69 74 49 64 5d 20 3d 20 6e 65 77 4c 69 6d 69 74  itId] = newLimit
32b71 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6f  ;.  }.  return o
32b72 6c 64 4c 69 6d 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  ldLimit;.}../*.*
32b73 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64  * This routine d
32b74 6f 65 73 20 74 68 65 20 77 6f 72 6b 20 6f 66 20  oes the work of 
32b75 6f 70 65 6e 69 6e 67 20 61 20 64 61 74 61 62 61  opening a databa
32b76 73 65 20 6f 6e 20 62 65 68 61 6c 66 20 6f 66 0a  se on behalf of.
32b77 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  ** sqlite3_open(
32b78 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70  ) and sqlite3_op
32b79 65 6e 31 36 28 29 2e 20 54 68 65 20 64 61 74 61  en16(). The data
32b7a 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 22 7a  base filename "z
32b7b 46 69 6c 65 6e 61 6d 65 22 20 20 0a 2a 2a 20 69  Filename"  .** i
32b7c 73 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 2e  s UTF-8 encoded.
32b7d 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
32b7e 70 65 6e 44 61 74 61 62 61 73 65 28 0a 20 20 63  penDatabase(.  c
32b7f 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
32b80 6e 61 6d 65 2c 20 2f 2a 20 44 61 74 61 62 61 73  name, /* Databas
32b81 65 20 66 69 6c 65 6e 61 6d 65 20 55 54 46 2d 38  e filename UTF-8
32b82 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 73 71   encoded */.  sq
32b83 6c 69 74 65 33 20 2a 2a 70 70 44 62 2c 20 20 20  lite3 **ppDb,   
32b84 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74       /* OUT: Ret
32b85 75 72 6e 65 64 20 64 61 74 61 62 61 73 65 20 68  urned database h
32b86 61 6e 64 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67  andle */.  unsig
32b87 6e 65 64 20 66 6c 61 67 73 2c 20 20 20 20 20 20  ned flags,      
32b88 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 61 6c    /* Operational
32b89 20 66 6c 61 67 73 20 2a 2f 0a 20 20 63 6f 6e 73   flags */.  cons
32b8a 74 20 63 68 61 72 20 2a 7a 56 66 73 20 20 20 20  t char *zVfs    
32b8b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
32b8c 65 20 56 46 53 20 74 6f 20 75 73 65 20 2a 2f 0a  e VFS to use */.
32b8d 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
32b8e 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e  ;.  int rc;.  in
32b8f 74 20 69 73 54 68 72 65 61 64 73 61 66 65 3b 0a  t isThreadsafe;.
32b90 0a 20 20 2a 70 70 44 62 20 3d 20 30 3b 0a 23 69  .  *ppDb = 0;.#i
32b91 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
32b92 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 72 63 20  T_AUTOINIT.  rc 
32b93 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  = sqlite3_initia
32b94 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 72 63  lize();.  if( rc
32b95 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65   ) return rc;.#e
32b96 6e 64 69 66 0a 0a 20 20 69 66 28 20 73 71 6c 69  ndif..  if( sqli
32b97 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
32b98 62 43 6f 72 65 4d 75 74 65 78 3d 3d 30 20 29 7b  bCoreMutex==0 ){
32b99 0a 20 20 20 20 69 73 54 68 72 65 61 64 73 61 66  .    isThreadsaf
32b9a 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69  e = 0;.  }else i
32b9b 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f( flags & SQLIT
32b9c 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 20 29  E_OPEN_NOMUTEX )
32b9d 7b 0a 20 20 20 20 69 73 54 68 72 65 61 64 73 61  {.    isThreadsa
32b9e 66 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20  fe = 0;.  }else 
32b9f 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49  if( flags & SQLI
32ba0 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45  TE_OPEN_FULLMUTE
32ba1 58 20 29 7b 0a 20 20 20 20 69 73 54 68 72 65 61  X ){.    isThrea
32ba2 64 73 61 66 65 20 3d 20 31 3b 0a 20 20 7d 65 6c  dsafe = 1;.  }el
32ba3 73 65 7b 0a 20 20 20 20 69 73 54 68 72 65 61 64  se{.    isThread
32ba4 73 61 66 65 20 3d 20 73 71 6c 69 74 65 33 47 6c  safe = sqlite3Gl
32ba5 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 46 75 6c 6c  obalConfig.bFull
32ba6 4d 75 74 65 78 3b 0a 20 20 7d 0a 20 20 69 66 28  Mutex;.  }.  if(
32ba7 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f   flags & SQLITE_
32ba8 4f 50 45 4e 5f 50 52 49 56 41 54 45 43 41 43 48  OPEN_PRIVATECACH
32ba9 45 20 29 7b 0a 20 20 20 20 66 6c 61 67 73 20 26  E ){.    flags &
32baa 3d 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53  = ~SQLITE_OPEN_S
32bab 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 7d 65  HAREDCACHE;.  }e
32bac 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 47  lse if( sqlite3G
32bad 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 68 61 72  lobalConfig.shar
32bae 65 64 43 61 63 68 65 45 6e 61 62 6c 65 64 20 29  edCacheEnabled )
32baf 7b 0a 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 53  {.    flags |= S
32bb0 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45  QLITE_OPEN_SHARE
32bb1 44 43 41 43 48 45 3b 0a 20 20 7d 0a 0a 20 20 2f  DCACHE;.  }..  /
32bb2 2a 20 52 65 6d 6f 76 65 20 68 61 72 6d 66 75 6c  * Remove harmful
32bb3 20 62 69 74 73 20 66 72 6f 6d 20 74 68 65 20 66   bits from the f
32bb4 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 0a 20  lags parameter. 
32bb5 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 53 51 4c   **.  ** The SQL
32bb6 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58  ITE_OPEN_NOMUTEX
32bb7 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 50 45 4e   and SQLITE_OPEN
32bb8 5f 46 55 4c 4c 4d 55 54 45 58 20 66 6c 61 67 73  _FULLMUTEX flags
32bb9 20 77 65 72 65 0a 20 20 2a 2a 20 64 65 61 6c 74   were.  ** dealt
32bba 20 77 69 74 68 20 69 6e 20 74 68 65 20 70 72 65   with in the pre
32bbb 76 69 6f 75 73 20 63 6f 64 65 20 62 6c 6f 63 6b  vious code block
32bbc 2e 20 20 42 65 73 69 64 65 73 20 74 68 65 73 65  .  Besides these
32bbd 2c 20 74 68 65 20 6f 6e 6c 79 0a 20 20 2a 2a 20  , the only.  ** 
32bbe 76 61 6c 69 64 20 69 6e 70 75 74 20 66 6c 61 67  valid input flag
32bbf 73 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  s for sqlite3_op
32bc0 65 6e 5f 76 32 28 29 20 61 72 65 20 53 51 4c 49  en_v2() are SQLI
32bc1 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
32bc2 2c 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 50  ,.  ** SQLITE_OP
32bc3 45 4e 5f 52 45 41 44 57 52 49 54 45 2c 20 61 6e  EN_READWRITE, an
32bc4 64 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52  d SQLITE_OPEN_CR
32bc5 45 41 54 45 2e 20 20 53 69 6c 65 6e 74 6c 79 20  EATE.  Silently 
32bc6 6d 61 73 6b 0a 20 20 2a 2a 20 6f 66 66 20 61 6c  mask.  ** off al
32bc7 6c 20 6f 74 68 65 72 20 66 6c 61 67 73 2e 0a 20  l other flags.. 
32bc8 20 2a 2f 0a 20 20 66 6c 61 67 73 20 26 3d 20 20   */.  flags &=  
32bc9 7e 28 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44  ~( SQLITE_OPEN_D
32bca 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 7c 0a 20  ELETEONCLOSE |. 
32bcb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
32bcc 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53  LITE_OPEN_EXCLUS
32bcd 49 56 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20  IVE |.          
32bce 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
32bcf 5f 4d 41 49 4e 5f 44 42 20 7c 0a 20 20 20 20 20  _MAIN_DB |.     
32bd0 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
32bd1 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 20 7c 20  _OPEN_TEMP_DB | 
32bd2 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
32bd3 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e  SQLITE_OPEN_TRAN
32bd4 53 49 45 4e 54 5f 44 42 20 7c 20 0a 20 20 20 20  SIENT_DB | .    
32bd5 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
32bd6 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52  E_OPEN_MAIN_JOUR
32bd7 4e 41 4c 20 7c 20 0a 20 20 20 20 20 20 20 20 20  NAL | .         
32bd8 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
32bd9 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 7c  N_TEMP_JOURNAL |
32bda 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
32bdb 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42   SQLITE_OPEN_SUB
32bdc 4a 4f 55 52 4e 41 4c 20 7c 20 0a 20 20 20 20 20  JOURNAL | .     
32bdd 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
32bde 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55  _OPEN_MASTER_JOU
32bdf 52 4e 41 4c 20 7c 0a 20 20 20 20 20 20 20 20 20  RNAL |.         
32be0 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
32be1 4e 5f 4e 4f 4d 55 54 45 58 20 7c 0a 20 20 20 20  N_NOMUTEX |.    
32be2 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
32be3 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58  E_OPEN_FULLMUTEX
32be4 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 29 3b  .             );
32be5 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
32be6 74 68 65 20 73 71 6c 69 74 65 20 64 61 74 61 20  the sqlite data 
32be7 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 64  structure */.  d
32be8 62 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  b = sqlite3Mallo
32be9 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 73 71  cZero( sizeof(sq
32bea 6c 69 74 65 33 29 20 29 3b 0a 20 20 69 66 28 20  lite3) );.  if( 
32beb 64 62 3d 3d 30 20 29 20 67 6f 74 6f 20 6f 70 65  db==0 ) goto ope
32bec 6e 64 62 5f 6f 75 74 3b 0a 20 20 69 66 28 20 69  ndb_out;.  if( i
32bed 73 54 68 72 65 61 64 73 61 66 65 20 29 7b 0a 20  sThreadsafe ){. 
32bee 20 20 20 64 62 2d 3e 6d 75 74 65 78 20 3d 20 73     db->mutex = s
32bef 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
32bf0 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45  (SQLITE_MUTEX_RE
32bf1 43 55 52 53 49 56 45 29 3b 0a 20 20 20 20 69 66  CURSIVE);.    if
32bf2 28 20 64 62 2d 3e 6d 75 74 65 78 3d 3d 30 20 29  ( db->mutex==0 )
32bf3 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
32bf4 66 72 65 65 28 64 62 29 3b 0a 20 20 20 20 20 20  free(db);.      
32bf5 64 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 67 6f  db = 0;.      go
32bf6 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20  to opendb_out;. 
32bf7 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
32bf8 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
32bf9 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d  b->mutex);.  db-
32bfa 3e 65 72 72 4d 61 73 6b 20 3d 20 30 78 66 66 3b  >errMask = 0xff;
32bfb 0a 20 20 64 62 2d 3e 6e 44 62 20 3d 20 32 3b 0a  .  db->nDb = 2;.
32bfc 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51    db->magic = SQ
32bfd 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 3b  LITE_MAGIC_BUSY;
32bfe 0a 20 20 64 62 2d 3e 61 44 62 20 3d 20 64 62 2d  .  db->aDb = db-
32bff 3e 61 44 62 53 74 61 74 69 63 3b 0a 0a 20 20 61  >aDbStatic;..  a
32c00 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 64 62  ssert( sizeof(db
32c01 2d 3e 61 4c 69 6d 69 74 29 3d 3d 73 69 7a 65 6f  ->aLimit)==sizeo
32c02 66 28 61 48 61 72 64 4c 69 6d 69 74 29 20 29 3b  f(aHardLimit) );
32c03 0a 20 20 6d 65 6d 63 70 79 28 64 62 2d 3e 61 4c  .  memcpy(db->aL
32c04 69 6d 69 74 2c 20 61 48 61 72 64 4c 69 6d 69 74  imit, aHardLimit
32c05 2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61 4c 69  , sizeof(db->aLi
32c06 6d 69 74 29 29 3b 0a 20 20 64 62 2d 3e 61 75 74  mit));.  db->aut
32c07 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 64  oCommit = 1;.  d
32c08 62 2d 3e 6e 65 78 74 41 75 74 6f 76 61 63 20 3d  b->nextAutovac =
32c09 20 2d 31 3b 0a 20 20 64 62 2d 3e 6e 65 78 74 50   -1;.  db->nextP
32c0a 61 67 65 73 69 7a 65 20 3d 20 30 3b 0a 20 20 64  agesize = 0;.  d
32c0b 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49  b->flags |= SQLI
32c0c 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73  TE_ShortColNames
32c0d 0a 23 69 66 20 53 51 4c 49 54 45 5f 44 45 46 41  .#if SQLITE_DEFA
32c0e 55 4c 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 3c  ULT_FILE_FORMAT<
32c0f 34 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  4.              
32c10 20 20 20 7c 20 53 51 4c 49 54 45 5f 4c 65 67 61     | SQLITE_Lega
32c11 63 79 46 69 6c 65 46 6d 74 0a 23 65 6e 64 69 66  cyFileFmt.#endif
32c12 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
32c13 4e 41 42 4c 45 5f 4c 4f 41 44 5f 45 58 54 45 4e  NABLE_LOAD_EXTEN
32c14 53 49 4f 4e 0a 20 20 20 20 20 20 20 20 20 20 20  SION.           
32c15 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 4c        | SQLITE_L
32c16 6f 61 64 45 78 74 65 6e 73 69 6f 6e 0a 23 65 6e  oadExtension.#en
32c17 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 44  dif.#if SQLITE_D
32c18 45 46 41 55 4c 54 5f 52 45 43 55 52 53 49 56 45  EFAULT_RECURSIVE
32c19 5f 54 52 49 47 47 45 52 53 0a 20 20 20 20 20 20  _TRIGGERS.      
32c1a 20 20 20 20 20 20 20 20 20 20 20 7c 20 53 51 4c             | SQL
32c1b 49 54 45 5f 52 65 63 54 72 69 67 67 65 72 73 0a  ITE_RecTriggers.
32c1c 23 65 6e 64 69 66 0a 20 20 20 20 20 20 3b 0a 20  #endif.      ;. 
32c1d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74   sqlite3HashInit
32c1e 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b  (&db->aCollSeq);
32c1f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
32c20 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
32c21 45 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68 49  E.  sqlite3HashI
32c22 6e 69 74 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65  nit(&db->aModule
32c23 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 64 62 2d  );.#endif..  db-
32c24 3e 70 56 66 73 20 3d 20 73 71 6c 69 74 65 33 5f  >pVfs = sqlite3_
32c25 76 66 73 5f 66 69 6e 64 28 7a 56 66 73 29 3b 0a  vfs_find(zVfs);.
32c26 20 20 69 66 28 20 21 64 62 2d 3e 70 56 66 73 20    if( !db->pVfs 
32c27 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
32c28 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71  TE_ERROR;.    sq
32c29 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72  lite3Error(db, r
32c2a 63 2c 20 22 6e 6f 20 73 75 63 68 20 76 66 73 3a  c, "no such vfs:
32c2b 20 25 73 22 2c 20 7a 56 66 73 29 3b 0a 20 20 20   %s", zVfs);.   
32c2c 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74   goto opendb_out
32c2d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20  ;.  }..  /* Add 
32c2e 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c  the default coll
32c2f 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 42  ation sequence B
32c30 49 4e 41 52 59 2e 20 42 49 4e 41 52 59 20 77 6f  INARY. BINARY wo
32c31 72 6b 73 20 66 6f 72 20 62 6f 74 68 20 55 54 46  rks for both UTF
32c32 2d 38 0a 20 20 2a 2a 20 61 6e 64 20 55 54 46 2d  -8.  ** and UTF-
32c33 31 36 2c 20 73 6f 20 61 64 64 20 61 20 76 65 72  16, so add a ver
32c34 73 69 6f 6e 20 66 6f 72 20 65 61 63 68 20 74 6f  sion for each to
32c35 20 61 76 6f 69 64 20 61 6e 79 20 75 6e 6e 65 63   avoid any unnec
32c36 65 73 73 61 72 79 0a 20 20 2a 2a 20 63 6f 6e 76  essary.  ** conv
32c37 65 72 73 69 6f 6e 73 2e 20 54 68 65 20 6f 6e 6c  ersions. The onl
32c38 79 20 65 72 72 6f 72 20 74 68 61 74 20 63 61 6e  y error that can
32c39 20 6f 63 63 75 72 20 68 65 72 65 20 69 73 20 61   occur here is a
32c3a 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72   malloc() failur
32c3b 65 2e 0a 20 20 2a 2f 0a 20 20 63 72 65 61 74 65  e..  */.  create
32c3c 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 22 42  Collation(db, "B
32c3d 49 4e 41 52 59 22 2c 20 53 51 4c 49 54 45 5f 55  INARY", SQLITE_U
32c3e 54 46 38 2c 20 53 51 4c 49 54 45 5f 43 4f 4c 4c  TF8, SQLITE_COLL
32c3f 5f 42 49 4e 41 52 59 2c 20 30 2c 0a 20 20 20 20  _BINARY, 0,.    
32c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 69                bi
32c41 6e 43 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a 20  nCollFunc, 0);. 
32c42 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e   createCollation
32c43 28 64 62 2c 20 22 42 49 4e 41 52 59 22 2c 20 53  (db, "BINARY", S
32c44 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53  QLITE_UTF16BE, S
32c45 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52  QLITE_COLL_BINAR
32c46 59 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20  Y, 0,.          
32c47 20 20 20 20 20 20 20 20 62 69 6e 43 6f 6c 6c 46          binCollF
32c48 75 6e 63 2c 20 30 29 3b 0a 20 20 63 72 65 61 74  unc, 0);.  creat
32c49 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 22  eCollation(db, "
32c4a 42 49 4e 41 52 59 22 2c 20 53 51 4c 49 54 45 5f  BINARY", SQLITE_
32c4b 55 54 46 31 36 4c 45 2c 20 53 51 4c 49 54 45 5f  UTF16LE, SQLITE_
32c4c 43 4f 4c 4c 5f 42 49 4e 41 52 59 2c 20 30 2c 0a  COLL_BINARY, 0,.
32c4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32c4e 20 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c 20 30    binCollFunc, 0
32c4f 29 3b 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c 61  );.  createColla
32c50 74 69 6f 6e 28 64 62 2c 20 22 52 54 52 49 4d 22  tion(db, "RTRIM"
32c51 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53  , SQLITE_UTF8, S
32c52 51 4c 49 54 45 5f 43 4f 4c 4c 5f 55 53 45 52 2c  QLITE_COLL_USER,
32c53 20 28 76 6f 69 64 2a 29 31 2c 0a 20 20 20 20 20   (void*)1,.     
32c54 20 20 20 20 20 20 20 20 20 20 20 20 20 62 69 6e               bin
32c55 43 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a 20 20  CollFunc, 0);.  
32c56 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
32c57 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f  iled ){.    goto
32c58 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d   opendb_out;.  }
32c59 0a 20 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c  .  db->pDfltColl
32c5a 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f   = sqlite3FindCo
32c5b 6c 6c 53 65 71 28 64 62 2c 20 53 51 4c 49 54 45  llSeq(db, SQLITE
32c5c 5f 55 54 46 38 2c 20 22 42 49 4e 41 52 59 22 2c  _UTF8, "BINARY",
32c5d 20 30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64   0);.  assert( d
32c5e 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 21 3d 30 20  b->pDfltColl!=0 
32c5f 29 3b 0a 0a 20 20 2f 2a 20 41 6c 73 6f 20 61 64  );..  /* Also ad
32c60 64 20 61 20 55 54 46 2d 38 20 63 61 73 65 2d 69  d a UTF-8 case-i
32c61 6e 73 65 6e 73 69 74 69 76 65 20 63 6f 6c 6c 61  nsensitive colla
32c62 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 2e 20 2a  tion sequence. *
32c63 2f 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c 61 74  /.  createCollat
32c64 69 6f 6e 28 64 62 2c 20 22 4e 4f 43 41 53 45 22  ion(db, "NOCASE"
32c65 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53  , SQLITE_UTF8, S
32c66 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53  QLITE_COLL_NOCAS
32c67 45 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20  E, 0,.          
32c68 20 20 20 20 20 20 20 20 6e 6f 63 61 73 65 43 6f          nocaseCo
32c69 6c 6c 61 74 69 6e 67 46 75 6e 63 2c 20 30 29 3b  llatingFunc, 0);
32c6a 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20  ..  /* Open the 
32c6b 62 61 63 6b 65 6e 64 20 64 61 74 61 62 61 73 65  backend database
32c6c 20 64 72 69 76 65 72 20 2a 2f 0a 20 20 64 62 2d   driver */.  db-
32c6d 3e 6f 70 65 6e 46 6c 61 67 73 20 3d 20 66 6c 61  >openFlags = fla
32c6e 67 73 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  gs;.  rc = sqlit
32c6f 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28 64  e3BtreeFactory(d
32c70 62 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 30 2c  b, zFilename, 0,
32c71 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
32c72 43 41 43 48 45 5f 53 49 5a 45 2c 20 0a 20 20 20  CACHE_SIZE, .   
32c73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32c74 20 20 20 20 20 20 20 20 66 6c 61 67 73 20 7c 20          flags | 
32c75 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
32c76 5f 44 42 2c 0a 20 20 20 20 20 20 20 20 20 20 20  _DB,.           
32c77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32c78 26 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29  &db->aDb[0].pBt)
32c79 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
32c7a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28  TE_OK ){.    if(
32c7b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52   rc==SQLITE_IOER
32c7c 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20  R_NOMEM ){.     
32c7d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
32c7e 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  EM;.    }.    sq
32c7f 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72  lite3Error(db, r
32c80 63 2c 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20  c, 0);.    goto 
32c81 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a  opendb_out;.  }.
32c82 20 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 53 63    db->aDb[0].pSc
32c83 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 33 53 63  hema = sqlite3Sc
32c84 68 65 6d 61 47 65 74 28 64 62 2c 20 64 62 2d 3e  hemaGet(db, db->
32c85 61 44 62 5b 30 5d 2e 70 42 74 29 3b 0a 20 20 64  aDb[0].pBt);.  d
32c86 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d  b->aDb[1].pSchem
32c87 61 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  a = sqlite3Schem
32c88 61 47 65 74 28 64 62 2c 20 30 29 3b 0a 0a 0a 20  aGet(db, 0);... 
32c89 20 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20   /* The default 
32c8a 73 61 66 65 74 79 5f 6c 65 76 65 6c 20 66 6f 72  safety_level for
32c8b 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
32c8c 73 65 20 69 73 20 27 66 75 6c 6c 27 3b 20 66 6f  se is 'full'; fo
32c8d 72 20 74 68 65 20 74 65 6d 70 0a 20 20 2a 2a 20  r the temp.  ** 
32c8e 64 61 74 61 62 61 73 65 20 69 74 20 69 73 20 27  database it is '
32c8f 4e 4f 4e 45 27 2e 20 54 68 69 73 20 6d 61 74 63  NONE'. This matc
32c90 68 65 73 20 74 68 65 20 70 61 67 65 72 20 6c 61  hes the pager la
32c91 79 65 72 20 64 65 66 61 75 6c 74 73 2e 20 20 0a  yer defaults.  .
32c92 20 20 2a 2f 0a 20 20 64 62 2d 3e 61 44 62 5b 30    */.  db->aDb[0
32c93 5d 2e 7a 4e 61 6d 65 20 3d 20 22 6d 61 69 6e 22  ].zName = "main"
32c94 3b 0a 20 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 73  ;.  db->aDb[0].s
32c95 61 66 65 74 79 5f 6c 65 76 65 6c 20 3d 20 33 3b  afety_level = 3;
32c96 0a 20 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 7a 4e  .  db->aDb[1].zN
32c97 61 6d 65 20 3d 20 22 74 65 6d 70 22 3b 0a 20 20  ame = "temp";.  
32c98 64 62 2d 3e 61 44 62 5b 31 5d 2e 73 61 66 65 74  db->aDb[1].safet
32c99 79 5f 6c 65 76 65 6c 20 3d 20 31 3b 0a 0a 20 20  y_level = 1;..  
32c9a 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49  db->magic = SQLI
32c9b 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 3b 0a 20  TE_MAGIC_OPEN;. 
32c9c 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
32c9d 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74  ailed ){.    got
32c9e 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20  o opendb_out;.  
32c9f 7d 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72  }..  /* Register
32ca0 20 61 6c 6c 20 62 75 69 6c 74 2d 69 6e 20 66 75   all built-in fu
32ca1 6e 63 74 69 6f 6e 73 2c 20 62 75 74 20 64 6f 20  nctions, but do 
32ca2 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 72  not attempt to r
32ca3 65 61 64 20 74 68 65 0a 20 20 2a 2a 20 64 61 74  ead the.  ** dat
32ca4 61 62 61 73 65 20 73 63 68 65 6d 61 20 79 65 74  abase schema yet
32ca5 2e 20 54 68 69 73 20 69 73 20 64 65 6c 61 79 65  . This is delaye
32ca6 64 20 75 6e 74 69 6c 20 74 68 65 20 66 69 72 73  d until the firs
32ca7 74 20 74 69 6d 65 20 74 68 65 20 64 61 74 61 62  t time the datab
32ca8 61 73 65 0a 20 20 2a 2a 20 69 73 20 61 63 63 65  ase.  ** is acce
32ca9 73 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  ssed..  */.  sql
32caa 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
32cab 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 73  LITE_OK, 0);.  s
32cac 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 42 75  qlite3RegisterBu
32cad 69 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73 28 64  iltinFunctions(d
32cae 62 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 61  b);..  /* Load a
32caf 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69  utomatic extensi
32cb0 6f 6e 73 20 2d 20 65 78 74 65 6e 73 69 6f 6e 73  ons - extensions
32cb1 20 74 68 61 74 20 68 61 76 65 20 62 65 65 6e 20   that have been 
32cb2 72 65 67 69 73 74 65 72 65 64 0a 20 20 2a 2a 20  registered.  ** 
32cb3 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65  using the sqlite
32cb4 33 5f 61 75 74 6f 6d 61 74 69 63 5f 65 78 74 65  3_automatic_exte
32cb5 6e 73 69 6f 6e 28 29 20 41 50 49 2e 0a 20 20 2a  nsion() API..  *
32cb6 2f 0a 20 20 73 71 6c 69 74 65 33 41 75 74 6f 4c  /.  sqlite3AutoL
32cb7 6f 61 64 45 78 74 65 6e 73 69 6f 6e 73 28 64 62  oadExtensions(db
32cb8 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
32cb9 33 5f 65 72 72 63 6f 64 65 28 64 62 29 3b 0a 20  3_errcode(db);. 
32cba 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
32cbb 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6f  OK ){.    goto o
32cbc 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  pendb_out;.  }..
32cbd 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
32cbe 41 42 4c 45 5f 46 54 53 31 0a 20 20 69 66 28 20  ABLE_FTS1.  if( 
32cbf 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
32cc0 64 20 29 7b 0a 20 20 20 20 65 78 74 65 72 6e 20  d ){.    extern 
32cc1 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 31 49  int sqlite3Fts1I
32cc2 6e 69 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  nit(sqlite3*);. 
32cc3 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46     rc = sqlite3F
32cc4 74 73 31 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d  ts1Init(db);.  }
32cc5 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
32cc6 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
32cc7 53 32 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61  S2.  if( !db->ma
32cc8 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 72 63  llocFailed && rc
32cc9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
32cca 20 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71     extern int sq
32ccb 6c 69 74 65 33 46 74 73 32 49 6e 69 74 28 73 71  lite3Fts2Init(sq
32ccc 6c 69 74 65 33 2a 29 3b 0a 20 20 20 20 72 63 20  lite3*);.    rc 
32ccd 3d 20 73 71 6c 69 74 65 33 46 74 73 32 49 6e 69  = sqlite3Fts2Ini
32cce 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  t(db);.  }.#endi
32ccf 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
32cd0 5f 45 4e 41 42 4c 45 5f 46 54 53 33 0a 20 20 69  _ENABLE_FTS3.  i
32cd1 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  f( !db->mallocFa
32cd2 69 6c 65 64 20 26 26 20 72 63 3d 3d 53 51 4c 49  iled && rc==SQLI
32cd3 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20  TE_OK ){.    rc 
32cd4 3d 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69  = sqlite3Fts3Ini
32cd5 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  t(db);.  }.#endi
32cd6 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
32cd7 5f 45 4e 41 42 4c 45 5f 49 43 55 0a 20 20 69 66  _ENABLE_ICU.  if
32cd8 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
32cd9 6c 65 64 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  led && rc==SQLIT
32cda 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d  E_OK ){.    rc =
32cdb 20 73 71 6c 69 74 65 33 49 63 75 49 6e 69 74 28   sqlite3IcuInit(
32cdc 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  db);.  }.#endif.
32cdd 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
32cde 4e 41 42 4c 45 5f 52 54 52 45 45 0a 20 20 69 66  NABLE_RTREE.  if
32cdf 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
32ce0 6c 65 64 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  led && rc==SQLIT
32ce1 45 5f 4f 4b 29 7b 0a 20 20 20 20 72 63 20 3d 20  E_OK){.    rc = 
32ce2 73 71 6c 69 74 65 33 52 74 72 65 65 49 6e 69 74  sqlite3RtreeInit
32ce3 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  (db);.  }.#endif
32ce4 0a 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72  ..  sqlite3Error
32ce5 28 64 62 2c 20 72 63 2c 20 30 29 3b 0a 0a 20 20  (db, rc, 0);..  
32ce6 2f 2a 20 2d 44 53 51 4c 49 54 45 5f 44 45 46 41  /* -DSQLITE_DEFA
32ce7 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45  ULT_LOCKING_MODE
32ce8 3d 31 20 6d 61 6b 65 73 20 45 58 43 4c 55 53 49  =1 makes EXCLUSI
32ce9 56 45 20 74 68 65 20 64 65 66 61 75 6c 74 20 6c  VE the default l
32cea 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20 6d 6f 64 65  ocking.  ** mode
32ceb 2e 20 20 2d 44 53 51 4c 49 54 45 5f 44 45 46 41  .  -DSQLITE_DEFA
32cec 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45  ULT_LOCKING_MODE
32ced 3d 30 20 6d 61 6b 65 20 4e 4f 52 4d 41 4c 20 74  =0 make NORMAL t
32cee 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69  he default locki
32cef 6e 67 0a 20 20 2a 2a 20 6d 6f 64 65 2e 20 20 44  ng.  ** mode.  D
32cf0 6f 69 6e 67 20 6e 6f 74 68 69 6e 67 20 61 74 20  oing nothing at 
32cf1 61 6c 6c 20 61 6c 73 6f 20 6d 61 6b 65 73 20 4e  all also makes N
32cf2 4f 52 4d 41 4c 20 74 68 65 20 64 65 66 61 75 6c  ORMAL the defaul
32cf3 74 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53  t..  */.#ifdef S
32cf4 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c 4f  QLITE_DEFAULT_LO
32cf5 43 4b 49 4e 47 5f 4d 4f 44 45 0a 20 20 64 62 2d  CKING_MODE.  db-
32cf6 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 20 3d 20  >dfltLockMode = 
32cf7 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c  SQLITE_DEFAULT_L
32cf8 4f 43 4b 49 4e 47 5f 4d 4f 44 45 3b 0a 20 20 73  OCKING_MODE;.  s
32cf9 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69  qlite3PagerLocki
32cfa 6e 67 4d 6f 64 65 28 73 71 6c 69 74 65 33 42 74  ngMode(sqlite3Bt
32cfb 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61 44 62  reePager(db->aDb
32cfc 5b 30 5d 2e 70 42 74 29 2c 0a 20 20 20 20 20 20  [0].pBt),.      
32cfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32cfe 20 20 20 20 53 51 4c 49 54 45 5f 44 45 46 41 55      SQLITE_DEFAU
32cff 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 29  LT_LOCKING_MODE)
32d00 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 45  ;.#endif..  /* E
32d01 6e 61 62 6c 65 20 74 68 65 20 6c 6f 6f 6b 61 73  nable the lookas
32d02 69 64 65 2d 6d 61 6c 6c 6f 63 20 73 75 62 73 79  ide-malloc subsy
32d03 73 74 65 6d 20 2a 2f 0a 20 20 73 65 74 75 70 4c  stem */.  setupL
32d04 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 30 2c 20  ookaside(db, 0, 
32d05 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
32d06 66 69 67 2e 73 7a 4c 6f 6f 6b 61 73 69 64 65 2c  fig.szLookaside,
32d07 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
32d08 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
32d09 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 4c 6f  GlobalConfig.nLo
32d0a 6f 6b 61 73 69 64 65 29 3b 0a 0a 6f 70 65 6e 64  okaside);..opend
32d0b 62 5f 6f 75 74 3a 0a 20 20 69 66 28 20 64 62 20  b_out:.  if( db 
32d0c 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  ){.    assert( d
32d0d 62 2d 3e 6d 75 74 65 78 21 3d 30 20 7c 7c 20 69  b->mutex!=0 || i
32d0e 73 54 68 72 65 61 64 73 61 66 65 3d 3d 30 20 7c  sThreadsafe==0 |
32d0f 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  | sqlite3GlobalC
32d10 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65 78  onfig.bFullMutex
32d11 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ==0 );.    sqlit
32d12 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
32d13 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20  b->mutex);.  }. 
32d14 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 72   rc = sqlite3_er
32d15 72 63 6f 64 65 28 64 62 29 3b 0a 20 20 69 66 28  rcode(db);.  if(
32d16 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
32d17 4d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  M ){.    sqlite3
32d18 5f 63 6c 6f 73 65 28 64 62 29 3b 0a 20 20 20 20  _close(db);.    
32d19 64 62 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20  db = 0;.  }else 
32d1a 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
32d1b 4b 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67  K ){.    db->mag
32d1c 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49  ic = SQLITE_MAGI
32d1d 43 5f 53 49 43 4b 3b 0a 20 20 7d 0a 20 20 2a 70  C_SICK;.  }.  *p
32d1e 70 44 62 20 3d 20 64 62 3b 0a 20 20 72 65 74 75  pDb = db;.  retu
32d1f 72 6e 20 73 71 6c 69 74 65 33 41 70 69 45 78 69  rn sqlite3ApiExi
32d20 74 28 30 2c 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a  t(0, rc);.}../*.
32d21 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 64 61  ** Open a new da
32d22 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 0a 2a  tabase handle..*
32d23 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
32d24 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 0a 20   sqlite3_open(. 
32d25 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
32d26 6c 65 6e 61 6d 65 2c 20 0a 20 20 73 71 6c 69 74  lename, .  sqlit
32d27 65 33 20 2a 2a 70 70 44 62 20 0a 29 7b 0a 20 20  e3 **ppDb .){.  
32d28 72 65 74 75 72 6e 20 6f 70 65 6e 44 61 74 61 62  return openDatab
32d29 61 73 65 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 70  ase(zFilename, p
32d2a 70 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20  pDb,.           
32d2b 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
32d2c 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
32d2d 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43   | SQLITE_OPEN_C
32d2e 52 45 41 54 45 2c 20 30 29 3b 0a 7d 0a 53 51 4c  REATE, 0);.}.SQL
32d2f 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
32d30 74 65 33 5f 6f 70 65 6e 5f 76 32 28 0a 20 20 63  te3_open_v2(.  c
32d31 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e  onst char *filen
32d32 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61  ame,   /* Databa
32d33 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46  se filename (UTF
32d34 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  -8) */.  sqlite3
32d35 20 2a 2a 70 70 44 62 2c 20 20 20 20 20 20 20 20   **ppDb,        
32d36 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20   /* OUT: SQLite 
32d37 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69  db handle */.  i
32d38 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20  nt flags,       
32d39 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
32d3a 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
32d3b 2a 7a 56 66 73 20 20 20 20 20 20 20 20 2f 2a 20  *zVfs        /* 
32d3c 4e 61 6d 65 20 6f 66 20 56 46 53 20 6d 6f 64 75  Name of VFS modu
32d3d 6c 65 20 74 6f 20 75 73 65 20 2a 2f 0a 29 7b 0a  le to use */.){.
32d3e 20 20 72 65 74 75 72 6e 20 6f 70 65 6e 44 61 74    return openDat
32d3f 61 62 61 73 65 28 66 69 6c 65 6e 61 6d 65 2c 20  abase(filename, 
32d40 70 70 44 62 2c 20 66 6c 61 67 73 2c 20 7a 56 66  ppDb, flags, zVf
32d41 73 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  s);.}..#ifndef S
32d42 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
32d43 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65  ./*.** Open a ne
32d44 77 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  w database handl
32d45 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
32d46 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65   int sqlite3_ope
32d47 6e 31 36 28 0a 20 20 63 6f 6e 73 74 20 76 6f 69  n16(.  const voi
32d48 64 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 0a 20  d *zFilename, . 
32d49 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 0a   sqlite3 **ppDb.
32d4a 29 7b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20  ){.  char const 
32d4b 2a 7a 46 69 6c 65 6e 61 6d 65 38 3b 20 20 20 2f  *zFilename8;   /
32d4c 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 65 6e 63 6f  * zFilename enco
32d4d 64 65 64 20 69 6e 20 55 54 46 2d 38 20 69 6e 73  ded in UTF-8 ins
32d4e 74 65 61 64 20 6f 66 20 55 54 46 2d 31 36 20 2a  tead of UTF-16 *
32d4f 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  /.  sqlite3_valu
32d50 65 20 2a 70 56 61 6c 3b 0a 20 20 69 6e 74 20 72  e *pVal;.  int r
32d51 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 7a 46  c;..  assert( zF
32d52 69 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 61 73 73  ilename );.  ass
32d53 65 72 74 28 20 70 70 44 62 20 29 3b 0a 20 20 2a  ert( ppDb );.  *
32d54 70 70 44 62 20 3d 20 30 3b 0a 23 69 66 6e 64 65  ppDb = 0;.#ifnde
32d55 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
32d56 54 4f 49 4e 49 54 0a 20 20 72 63 20 3d 20 73 71  TOINIT.  rc = sq
32d57 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
32d58 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  ();.  if( rc ) r
32d59 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66  eturn rc;.#endif
32d5a 0a 20 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 65  .  pVal = sqlite
32d5b 33 56 61 6c 75 65 4e 65 77 28 30 29 3b 0a 20 20  3ValueNew(0);.  
32d5c 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53  sqlite3ValueSetS
32d5d 74 72 28 70 56 61 6c 2c 20 2d 31 2c 20 7a 46 69  tr(pVal, -1, zFi
32d5e 6c 65 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 55  lename, SQLITE_U
32d5f 54 46 31 36 4e 41 54 49 56 45 2c 20 53 51 4c 49  TF16NATIVE, SQLI
32d60 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7a 46  TE_STATIC);.  zF
32d61 69 6c 65 6e 61 6d 65 38 20 3d 20 73 71 6c 69 74  ilename8 = sqlit
32d62 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c  e3ValueText(pVal
32d63 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a  , SQLITE_UTF8);.
32d64 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 38    if( zFilename8
32d65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6f 70 65   ){.    rc = ope
32d66 6e 44 61 74 61 62 61 73 65 28 7a 46 69 6c 65 6e  nDatabase(zFilen
32d67 61 6d 65 38 2c 20 70 70 44 62 2c 0a 20 20 20 20  ame8, ppDb,.    
32d68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32d69 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45    SQLITE_OPEN_RE
32d6a 41 44 57 52 49 54 45 20 7c 20 53 51 4c 49 54 45  ADWRITE | SQLITE
32d6b 5f 4f 50 45 4e 5f 43 52 45 41 54 45 2c 20 30 29  _OPEN_CREATE, 0)
32d6c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 2a 70  ;.    assert( *p
32d6d 70 44 62 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  pDb || rc==SQLIT
32d6e 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 69  E_NOMEM );.    i
32d6f 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
32d70 20 26 26 20 21 44 62 48 61 73 50 72 6f 70 65 72   && !DbHasProper
32d71 74 79 28 2a 70 70 44 62 2c 20 30 2c 20 44 42 5f  ty(*ppDb, 0, DB_
32d72 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 20 29 7b  SchemaLoaded) ){
32d73 0a 20 20 20 20 20 20 45 4e 43 28 2a 70 70 44 62  .      ENC(*ppDb
32d74 29 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36  ) = SQLITE_UTF16
32d75 4e 41 54 49 56 45 3b 0a 20 20 20 20 7d 0a 20 20  NATIVE;.    }.  
32d76 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
32d77 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
32d78 7d 0a 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65  }.  sqlite3Value
32d79 46 72 65 65 28 70 56 61 6c 29 3b 0a 0a 20 20 72  Free(pVal);..  r
32d7a 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 70 69  eturn sqlite3Api
32d7b 45 78 69 74 28 30 2c 20 72 63 29 3b 0a 7d 0a 23  Exit(0, rc);.}.#
32d7c 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
32d7d 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f  OMIT_UTF16 */../
32d7e 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20  *.** Register a 
32d7f 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  new collation se
32d80 71 75 65 6e 63 65 20 77 69 74 68 20 74 68 65 20  quence with the 
32d81 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
32d82 64 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  db..*/.SQLITE_AP
32d83 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72  I int sqlite3_cr
32d84 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a  eate_collation(.
32d85 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 0a    sqlite3* db, .
32d86 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
32d87 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 6e 63 2c  ame, .  int enc,
32d88 20 0a 20 20 76 6f 69 64 2a 20 70 43 74 78 2c 0a   .  void* pCtx,.
32d89 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29    int(*xCompare)
32d8a 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74  (void*,int,const
32d8b 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74   void*,int,const
32d8c 20 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74   void*).){.  int
32d8d 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d   rc;.  sqlite3_m
32d8e 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
32d8f 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28  utex);.  assert(
32d90 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
32d91 65 64 20 29 3b 0a 20 20 72 63 20 3d 20 63 72 65  ed );.  rc = cre
32d92 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c  ateCollation(db,
32d93 20 7a 4e 61 6d 65 2c 20 28 75 38 29 65 6e 63 2c   zName, (u8)enc,
32d94 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 55 53 45   SQLITE_COLL_USE
32d95 52 2c 20 70 43 74 78 2c 20 78 43 6f 6d 70 61 72  R, pCtx, xCompar
32d96 65 2c 20 30 29 3b 0a 20 20 72 63 20 3d 20 73 71  e, 0);.  rc = sq
32d97 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c  lite3ApiExit(db,
32d98 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f   rc);.  sqlite3_
32d99 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
32d9a 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
32d9b 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
32d9c 67 69 73 74 65 72 20 61 20 6e 65 77 20 63 6f 6c  gister a new col
32d9d 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
32d9e 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73  with the databas
32d9f 65 20 68 61 6e 64 6c 65 20 64 62 2e 0a 2a 2f 0a  e handle db..*/.
32da0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
32da1 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
32da2 6c 6c 61 74 69 6f 6e 5f 76 32 28 0a 20 20 73 71  llation_v2(.  sq
32da3 6c 69 74 65 33 2a 20 64 62 2c 20 0a 20 20 63 6f  lite3* db, .  co
32da4 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
32da5 20 0a 20 20 69 6e 74 20 65 6e 63 2c 20 0a 20 20   .  int enc, .  
32da6 76 6f 69 64 2a 20 70 43 74 78 2c 0a 20 20 69 6e  void* pCtx,.  in
32da7 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69  t(*xCompare)(voi
32da8 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
32da9 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
32daa 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78 44 65  d*),.  void(*xDe
32dab 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69  l)(void*).){.  i
32dac 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33  nt rc;.  sqlite3
32dad 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
32dae 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72  >mutex);.  asser
32daf 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  t( !db->mallocFa
32db0 69 6c 65 64 20 29 3b 0a 20 20 72 63 20 3d 20 63  iled );.  rc = c
32db1 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64  reateCollation(d
32db2 62 2c 20 7a 4e 61 6d 65 2c 20 28 75 38 29 65 6e  b, zName, (u8)en
32db3 63 2c 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 55  c, SQLITE_COLL_U
32db4 53 45 52 2c 20 70 43 74 78 2c 20 78 43 6f 6d 70  SER, pCtx, xComp
32db5 61 72 65 2c 20 78 44 65 6c 29 3b 0a 20 20 72 63  are, xDel);.  rc
32db6 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
32db7 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c  t(db, rc);.  sql
32db8 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
32db9 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
32dba 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66  eturn rc;.}..#if
32dbb 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
32dbc 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 52 65 67  _UTF16./*.** Reg
32dbd 69 73 74 65 72 20 61 20 6e 65 77 20 63 6f 6c 6c  ister a new coll
32dbe 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77  ation sequence w
32dbf 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73 65  ith the database
32dc0 20 68 61 6e 64 6c 65 20 64 62 2e 0a 2a 2f 0a 53   handle db..*/.S
32dc1 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
32dc2 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
32dc3 6c 61 74 69 6f 6e 31 36 28 0a 20 20 73 71 6c 69  lation16(.  sqli
32dc4 74 65 33 2a 20 64 62 2c 20 0a 20 20 63 6f 6e 73  te3* db, .  cons
32dc5 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c 0a 20  t void *zName,. 
32dc6 20 69 6e 74 20 65 6e 63 2c 20 0a 20 20 76 6f 69   int enc, .  voi
32dc7 64 2a 20 70 43 74 78 2c 0a 20 20 69 6e 74 28 2a  d* pCtx,.  int(*
32dc8 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c  xCompare)(void*,
32dc9 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
32dca 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29  int,const void*)
32dcb 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
32dcc 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 68 61 72  QLITE_OK;.  char
32dcd 20 2a 7a 4e 61 6d 65 38 3b 0a 20 20 73 71 6c 69   *zName8;.  sqli
32dce 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
32dcf 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73  db->mutex);.  as
32dd0 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f  sert( !db->mallo
32dd1 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 7a 4e 61  cFailed );.  zNa
32dd2 6d 65 38 20 3d 20 73 71 6c 69 74 65 33 55 74 66  me8 = sqlite3Utf
32dd3 31 36 74 6f 38 28 64 62 2c 20 7a 4e 61 6d 65 2c  16to8(db, zName,
32dd4 20 2d 31 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d   -1);.  if( zNam
32dd5 65 38 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 63  e8 ){.    rc = c
32dd6 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64  reateCollation(d
32dd7 62 2c 20 7a 4e 61 6d 65 38 2c 20 28 75 38 29 65  b, zName8, (u8)e
32dd8 6e 63 2c 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f  nc, SQLITE_COLL_
32dd9 55 53 45 52 2c 20 70 43 74 78 2c 20 78 43 6f 6d  USER, pCtx, xCom
32dda 70 61 72 65 2c 20 30 29 3b 0a 20 20 20 20 73 71  pare, 0);.    sq
32ddb 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
32ddc 7a 4e 61 6d 65 38 29 3b 0a 20 20 7d 0a 20 20 72  zName8);.  }.  r
32ddd 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
32dde 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71  it(db, rc);.  sq
32ddf 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
32de0 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
32de1 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e  return rc;.}.#en
32de2 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
32de3 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a  IT_UTF16 */../*.
32de4 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 63 6f  ** Register a co
32de5 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
32de6 20 66 61 63 74 6f 72 79 20 63 61 6c 6c 62 61 63   factory callbac
32de7 6b 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62  k with the datab
32de8 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 64 62  ase handle.** db
32de9 2e 20 52 65 70 6c 61 63 65 20 61 6e 79 20 70 72  . Replace any pr
32dea 65 76 69 6f 75 73 6c 79 20 69 6e 73 74 61 6c 6c  eviously install
32deb 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ed collation seq
32dec 75 65 6e 63 65 20 66 61 63 74 6f 72 79 2e 0a 2a  uence factory..*
32ded 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
32dee 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69   sqlite3_collati
32def 6f 6e 5f 6e 65 65 64 65 64 28 0a 20 20 73 71 6c  on_needed(.  sql
32df0 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 76 6f 69  ite3 *db, .  voi
32df1 64 20 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72  d *pCollNeededAr
32df2 67 2c 20 0a 20 20 76 6f 69 64 28 2a 78 43 6f 6c  g, .  void(*xCol
32df3 6c 4e 65 65 64 65 64 29 28 76 6f 69 64 2a 2c 73  lNeeded)(void*,s
32df4 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78  qlite3*,int eTex
32df5 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 61 72 2a  tRep,const char*
32df6 29 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ).){.  sqlite3_m
32df7 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
32df8 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e 78 43 6f  utex);.  db->xCo
32df9 6c 6c 4e 65 65 64 65 64 20 3d 20 78 43 6f 6c 6c  llNeeded = xColl
32dfa 4e 65 65 64 65 64 3b 0a 20 20 64 62 2d 3e 78 43  Needed;.  db->xC
32dfb 6f 6c 6c 4e 65 65 64 65 64 31 36 20 3d 20 30 3b  ollNeeded16 = 0;
32dfc 0a 20 20 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64  .  db->pCollNeed
32dfd 65 64 41 72 67 20 3d 20 70 43 6f 6c 6c 4e 65 65  edArg = pCollNee
32dfe 64 65 64 41 72 67 3b 0a 20 20 73 71 6c 69 74 65  dedArg;.  sqlite
32dff 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
32e00 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
32e01 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
32e02 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
32e03 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a  OMIT_UTF16./*.**
32e04 20 52 65 67 69 73 74 65 72 20 61 20 63 6f 6c 6c   Register a coll
32e05 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66  ation sequence f
32e06 61 63 74 6f 72 79 20 63 61 6c 6c 62 61 63 6b 20  actory callback 
32e07 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73  with the databas
32e08 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 64 62 2e 20  e handle.** db. 
32e09 52 65 70 6c 61 63 65 20 61 6e 79 20 70 72 65 76  Replace any prev
32e0a 69 6f 75 73 6c 79 20 69 6e 73 74 61 6c 6c 65 64  iously installed
32e0b 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
32e0c 6e 63 65 20 66 61 63 74 6f 72 79 2e 0a 2a 2f 0a  nce factory..*/.
32e0d 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
32e0e 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
32e0f 5f 6e 65 65 64 65 64 31 36 28 0a 20 20 73 71 6c  _needed16(.  sql
32e10 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 76 6f 69  ite3 *db, .  voi
32e11 64 20 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72  d *pCollNeededAr
32e12 67 2c 20 0a 20 20 76 6f 69 64 28 2a 78 43 6f 6c  g, .  void(*xCol
32e13 6c 4e 65 65 64 65 64 31 36 29 28 76 6f 69 64 2a  lNeeded16)(void*
32e14 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54  ,sqlite3*,int eT
32e15 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69  extRep,const voi
32e16 64 2a 29 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33  d*).){.  sqlite3
32e17 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
32e18 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e 78  >mutex);.  db->x
32e19 43 6f 6c 6c 4e 65 65 64 65 64 20 3d 20 30 3b 0a  CollNeeded = 0;.
32e1a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65    db->xCollNeede
32e1b 64 31 36 20 3d 20 78 43 6f 6c 6c 4e 65 65 64 65  d16 = xCollNeede
32e1c 64 31 36 3b 0a 20 20 64 62 2d 3e 70 43 6f 6c 6c  d16;.  db->pColl
32e1d 4e 65 65 64 65 64 41 72 67 20 3d 20 70 43 6f 6c  NeededArg = pCol
32e1e 6c 4e 65 65 64 65 64 41 72 67 3b 0a 20 20 73 71  lNeededArg;.  sq
32e1f 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
32e20 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
32e21 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
32e22 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
32e23 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
32e24 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
32e25 54 45 5f 4f 4d 49 54 5f 47 4c 4f 42 41 4c 52 45  TE_OMIT_GLOBALRE
32e26 43 4f 56 45 52 0a 23 69 66 6e 64 65 66 20 53 51  COVER.#ifndef SQ
32e27 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43  LITE_OMIT_DEPREC
32e28 41 54 45 44 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ATED./*.** This 
32e29 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 77 20  function is now 
32e2a 61 6e 20 61 6e 61 63 68 72 6f 6e 69 73 6d 2e 20  an anachronism. 
32e2b 49 74 20 75 73 65 64 20 74 6f 20 62 65 20 75 73  It used to be us
32e2c 65 64 20 74 6f 20 72 65 63 6f 76 65 72 20 66 72  ed to recover fr
32e2d 6f 6d 20 61 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29  om a.** malloc()
32e2e 20 66 61 69 6c 75 72 65 2c 20 62 75 74 20 53 51   failure, but SQ
32e2f 4c 69 74 65 20 6e 6f 77 20 64 6f 65 73 20 74 68  Lite now does th
32e30 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  is automatically
32e31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
32e32 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62  int sqlite3_glob
32e33 61 6c 5f 72 65 63 6f 76 65 72 28 76 6f 69 64 29  al_recover(void)
32e34 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  {.  return SQLIT
32e35 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 23  E_OK;.}.#endif.#
32e36 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73  endif../*.** Tes
32e37 74 20 74 6f 20 73 65 65 20 77 68 65 74 68 65 72  t to see whether
32e38 20 6f 72 20 6e 6f 74 20 74 68 65 20 64 61 74 61   or not the data
32e39 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
32e3a 69 73 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74  is in autocommit
32e3b 0a 2a 2a 20 6d 6f 64 65 2e 20 20 52 65 74 75 72  .** mode.  Retur
32e3c 6e 20 54 52 55 45 20 69 66 20 69 74 20 69 73 20  n TRUE if it is 
32e3d 61 6e 64 20 46 41 4c 53 45 20 69 66 20 6e 6f 74  and FALSE if not
32e3e 2e 20 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f  .  Autocommit mo
32e3f 64 65 20 69 73 20 6f 6e 0a 2a 2a 20 62 79 20 64  de is on.** by d
32e40 65 66 61 75 6c 74 2e 20 20 41 75 74 6f 63 6f 6d  efault.  Autocom
32e41 6d 69 74 20 69 73 20 64 69 73 61 62 6c 65 64 20  mit is disabled 
32e42 62 79 20 61 20 42 45 47 49 4e 20 73 74 61 74 65  by a BEGIN state
32e43 6d 65 6e 74 20 61 6e 64 20 72 65 65 6e 61 62 6c  ment and reenabl
32e44 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 6e 65 78  ed.** by the nex
32e45 74 20 43 4f 4d 4d 49 54 20 6f 72 20 52 4f 4c 4c  t COMMIT or ROLL
32e46 42 41 43 4b 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  BACK..**.*******
32e47 20 54 48 49 53 20 49 53 20 41 4e 20 45 58 50 45   THIS IS AN EXPE
32e48 52 49 4d 45 4e 54 41 4c 20 41 50 49 20 41 4e 44  RIMENTAL API AND
32e49 20 49 53 20 53 55 42 4a 45 43 54 20 54 4f 20 43   IS SUBJECT TO C
32e4a 48 41 4e 47 45 20 2a 2a 2a 2a 2a 2a 0a 2a 2f 0a  HANGE ******.*/.
32e4b 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
32e4c 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63  qlite3_get_autoc
32e4d 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64  ommit(sqlite3 *d
32e4e 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 2d  b){.  return db-
32e4f 3e 61 75 74 6f 43 6f 6d 6d 69 74 3b 0a 7d 0a 0a  >autoCommit;.}..
32e50 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
32e51 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  BUG./*.** The fo
32e52 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20  llowing routine 
32e53 69 73 20 73 75 62 74 69 74 75 74 65 64 20 66 6f  is subtituted fo
32e54 72 20 63 6f 6e 73 74 61 6e 74 20 53 51 4c 49 54  r constant SQLIT
32e55 45 5f 43 4f 52 52 55 50 54 20 69 6e 0a 2a 2a 20  E_CORRUPT in.** 
32e56 64 65 62 75 67 67 69 6e 67 20 62 75 69 6c 64 73  debugging builds
32e57 2e 20 20 54 68 69 73 20 70 72 6f 76 69 64 65 73  .  This provides
32e58 20 61 20 77 61 79 20 74 6f 20 73 65 74 20 61 20   a way to set a 
32e59 62 72 65 61 6b 70 6f 69 6e 74 20 66 6f 72 20 77  breakpoint for w
32e5a 68 65 6e 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f  hen.** corruptio
32e5b 6e 20 69 73 20 66 69 72 73 74 20 64 65 74 65 63  n is first detec
32e5c 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ted..*/.SQLITE_P
32e5d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
32e5e 65 33 43 6f 72 72 75 70 74 28 76 6f 69 64 29 7b  e3Corrupt(void){
32e5f 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
32e60 5f 43 4f 52 52 55 50 54 3b 0a 7d 0a 23 65 6e 64  _CORRUPT;.}.#end
32e61 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
32e62 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54  TE_OMIT_DEPRECAT
32e63 45 44 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73  ED./*.** This is
32e64 20 61 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 72   a convenience r
32e65 6f 75 74 69 6e 65 20 74 68 61 74 20 6d 61 6b 65  outine that make
32e66 73 20 73 75 72 65 20 74 68 61 74 20 61 6c 6c 20  s sure that all 
32e67 74 68 72 65 61 64 2d 73 70 65 63 69 66 69 63 0a  thread-specific.
32e68 2a 2a 20 64 61 74 61 20 66 6f 72 20 74 68 69 73  ** data for this
32e69 20 74 68 72 65 61 64 20 68 61 73 20 62 65 65 6e   thread has been
32e6a 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 0a 2a 2a   deallocated..**
32e6b 0a 2a 2a 20 53 51 4c 69 74 65 20 6e 6f 20 6c 6f  .** SQLite no lo
32e6c 6e 67 65 72 20 75 73 65 73 20 74 68 72 65 61 64  nger uses thread
32e6d 2d 73 70 65 63 69 66 69 63 20 64 61 74 61 20 73  -specific data s
32e6e 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  o this routine i
32e6f 73 20 6e 6f 77 20 61 0a 2a 2a 20 6e 6f 2d 6f 70  s now a.** no-op
32e70 2e 20 20 49 74 20 69 73 20 72 65 74 61 69 6e 65  .  It is retaine
32e71 64 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c  d for historical
32e72 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a   compatibility..
32e73 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
32e74 69 64 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61  id sqlite3_threa
32e75 64 5f 63 6c 65 61 6e 75 70 28 76 6f 69 64 29 7b  d_cleanup(void){
32e76 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
32e77 20 52 65 74 75 72 6e 20 6d 65 74 61 20 69 6e 66   Return meta inf
32e78 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61  ormation about a
32e79 20 73 70 65 63 69 66 69 63 20 63 6f 6c 75 6d 6e   specific column
32e7a 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 74   of a database t
32e7b 61 62 6c 65 2e 0a 2a 2a 20 53 65 65 20 63 6f 6d  able..** See com
32e7c 6d 65 6e 74 20 69 6e 20 73 71 6c 69 74 65 33 2e  ment in sqlite3.
32e7d 68 20 28 73 71 6c 69 74 65 2e 68 2e 69 6e 29 20  h (sqlite.h.in) 
32e7e 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a  for details..*/.
32e7f 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
32e80 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41  ABLE_COLUMN_META
32e81 44 41 54 41 0a 53 51 4c 49 54 45 5f 41 50 49 20  DATA.SQLITE_API 
32e82 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 61 62 6c  int sqlite3_tabl
32e83 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74  e_column_metadat
32e84 61 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  a(.  sqlite3 *db
32e85 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
32e86 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 68   /* Connection h
32e87 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  andle */.  const
32e88 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20   char *zDbName, 
32e89 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
32e8a 73 65 20 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c 20  se name or NULL 
32e8b 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
32e8c 2a 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20  *zTableName,    
32e8d 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a   /* Table name *
32e8e 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
32e8f 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20 20  zColumnName,    
32e90 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a  /* Column name *
32e91 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  /.  char const *
32e92 2a 70 7a 44 61 74 61 54 79 70 65 2c 20 20 20 20  *pzDataType,    
32e93 2f 2a 20 4f 55 54 50 55 54 3a 20 44 65 63 6c 61  /* OUTPUT: Decla
32e94 72 65 64 20 64 61 74 61 20 74 79 70 65 20 2a 2f  red data type */
32e95 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a  .  char const **
32e96 70 7a 43 6f 6c 6c 53 65 71 2c 20 20 20 20 20 2f  pzCollSeq,     /
32e97 2a 20 4f 55 54 50 55 54 3a 20 43 6f 6c 6c 61 74  * OUTPUT: Collat
32e98 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d  ion sequence nam
32e99 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f 74  e */.  int *pNot
32e9a 4e 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20  Null,           
32e9b 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72     /* OUTPUT: Tr
32e9c 75 65 20 69 66 20 4e 4f 54 20 4e 55 4c 4c 20 63  ue if NOT NULL c
32e9d 6f 6e 73 74 72 61 69 6e 74 20 65 78 69 73 74 73  onstraint exists
32e9e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 50 72 69 6d   */.  int *pPrim
32e9f 61 72 79 4b 65 79 2c 20 20 20 20 20 20 20 20 20  aryKey,         
32ea0 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75    /* OUTPUT: Tru
32ea1 65 20 69 66 20 63 6f 6c 75 6d 6e 20 70 61 72 74  e if column part
32ea2 20 6f 66 20 50 4b 20 2a 2f 0a 20 20 69 6e 74 20   of PK */.  int 
32ea3 2a 70 41 75 74 6f 69 6e 63 20 20 20 20 20 20 20  *pAutoinc       
32ea4 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55          /* OUTPU
32ea5 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d  T: True if colum
32ea6 6e 20 69 73 20 61 75 74 6f 2d 69 6e 63 72 65 6d  n is auto-increm
32ea7 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ent */.){.  int 
32ea8 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72  rc;.  char *zErr
32ea9 4d 73 67 20 3d 20 30 3b 0a 20 20 54 61 62 6c 65  Msg = 0;.  Table
32eaa 20 2a 70 54 61 62 20 3d 20 30 3b 0a 20 20 43 6f   *pTab = 0;.  Co
32eab 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 30 3b 0a  lumn *pCol = 0;.
32eac 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 0a 20 20 63    int iCol;..  c
32ead 68 61 72 20 63 6f 6e 73 74 20 2a 7a 44 61 74 61  har const *zData
32eae 54 79 70 65 20 3d 20 30 3b 0a 20 20 63 68 61 72  Type = 0;.  char
32eaf 20 63 6f 6e 73 74 20 2a 7a 43 6f 6c 6c 53 65 71   const *zCollSeq
32eb0 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 6f 74 6e   = 0;.  int notn
32eb1 75 6c 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 70  ull = 0;.  int p
32eb2 72 69 6d 61 72 79 6b 65 79 20 3d 20 30 3b 0a 20  rimarykey = 0;. 
32eb3 20 69 6e 74 20 61 75 74 6f 69 6e 63 20 3d 20 30   int autoinc = 0
32eb4 3b 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 74  ;..  /* Ensure t
32eb5 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
32eb6 6d 61 20 68 61 73 20 62 65 65 6e 20 6c 6f 61 64  ma has been load
32eb7 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ed */.  sqlite3_
32eb8 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
32eb9 6d 75 74 65 78 29 3b 0a 20 20 28 76 6f 69 64 29  mutex);.  (void)
32eba 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
32ebb 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  db);.  sqlite3Bt
32ebc 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b  reeEnterAll(db);
32ebd 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49  .  rc = sqlite3I
32ebe 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 4d 73 67  nit(db, &zErrMsg
32ebf 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  );.  if( SQLITE_
32ec0 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 67 6f  OK!=rc ){.    go
32ec1 74 6f 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20  to error_out;.  
32ec2 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74  }..  /* Locate t
32ec3 68 65 20 74 61 62 6c 65 20 69 6e 20 71 75 65 73  he table in ques
32ec4 74 69 6f 6e 20 2a 2f 0a 20 20 70 54 61 62 20 3d  tion */.  pTab =
32ec5 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
32ec6 65 28 64 62 2c 20 7a 54 61 62 6c 65 4e 61 6d 65  e(db, zTableName
32ec7 2c 20 7a 44 62 4e 61 6d 65 29 3b 0a 20 20 69 66  , zDbName);.  if
32ec8 28 20 21 70 54 61 62 20 7c 7c 20 70 54 61 62 2d  ( !pTab || pTab-
32ec9 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  >pSelect ){.    
32eca 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 67 6f  pTab = 0;.    go
32ecb 74 6f 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20  to error_out;.  
32ecc 7d 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65  }..  /* Find the
32ecd 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 68 69 63   column for whic
32ece 68 20 69 6e 66 6f 20 69 73 20 72 65 71 75 65 73  h info is reques
32ecf 74 65 64 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ted */.  if( sql
32ed0 69 74 65 33 49 73 52 6f 77 69 64 28 7a 43 6f 6c  ite3IsRowid(zCol
32ed1 75 6d 6e 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20  umnName) ){.    
32ed2 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 69 50 4b  iCol = pTab->iPK
32ed3 65 79 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c  ey;.    if( iCol
32ed4 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 43 6f  >=0 ){.      pCo
32ed5 6c 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b  l = &pTab->aCol[
32ed6 69 43 6f 6c 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d  iCol];.    }.  }
32ed7 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28 69 43  else{.    for(iC
32ed8 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d  ol=0; iCol<pTab-
32ed9 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a  >nCol; iCol++){.
32eda 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 26 70 54        pCol = &pT
32edb 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a  ab->aCol[iCol];.
32edc 20 20 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c        if( 0==sql
32edd 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c  ite3StrICmp(pCol
32ede 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 75 6d 6e  ->zName, zColumn
32edf 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20  Name) ){.       
32ee0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
32ee1 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 43      }.    if( iC
32ee2 6f 6c 3d 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29  ol==pTab->nCol )
32ee3 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 30  {.      pTab = 0
32ee4 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 72 72  ;.      goto err
32ee5 6f 72 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20  or_out;.    }.  
32ee6 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c  }..  /* The foll
32ee7 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 73 74 6f 72  owing block stor
32ee8 65 73 20 74 68 65 20 6d 65 74 61 20 69 6e 66 6f  es the meta info
32ee9 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 77 69 6c  rmation that wil
32eea 6c 20 62 65 20 72 65 74 75 72 6e 65 64 0a 20 20  l be returned.  
32eeb 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  ** to the caller
32eec 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   in local variab
32eed 6c 65 73 20 7a 44 61 74 61 54 79 70 65 2c 20 7a  les zDataType, z
32eee 43 6f 6c 6c 53 65 71 2c 20 6e 6f 74 6e 75 6c 6c  CollSeq, notnull
32eef 2c 20 70 72 69 6d 61 72 79 6b 65 79 0a 20 20 2a  , primarykey.  *
32ef0 2a 20 61 6e 64 20 61 75 74 6f 69 6e 63 2e 20 41  * and autoinc. A
32ef1 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65  t this point the
32ef2 72 65 20 61 72 65 20 74 77 6f 20 70 6f 73 73 69  re are two possi
32ef3 62 69 6c 69 74 69 65 73 3a 0a 20 20 2a 2a 20 0a  bilities:.  ** .
32ef4 20 20 2a 2a 20 20 20 20 20 31 2e 20 54 68 65 20    **     1. The 
32ef5 73 70 65 63 69 66 69 65 64 20 63 6f 6c 75 6d 6e  specified column
32ef6 20 6e 61 6d 65 20 77 61 73 20 72 6f 77 69 64 22   name was rowid"
32ef7 2c 20 22 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77  , "oid" or "_row
32ef8 69 64 5f 22 20 0a 20 20 2a 2a 20 20 20 20 20 20  id_" .  **      
32ef9 20 20 61 6e 64 20 74 68 65 72 65 20 69 73 20 6e    and there is n
32efa 6f 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63  o explicitly dec
32efb 6c 61 72 65 64 20 49 50 4b 20 63 6f 6c 75 6d 6e  lared IPK column
32efc 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  . .  **.  **    
32efd 20 32 2e 20 54 68 65 20 74 61 62 6c 65 20 69 73   2. The table is
32efe 20 6e 6f 74 20 61 20 76 69 65 77 20 61 6e 64 20   not a view and 
32eff 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20  the column name 
32f00 69 64 65 6e 74 69 66 69 65 64 20 61 6e 20 0a 20  identified an . 
32f01 20 2a 2a 20 20 20 20 20 20 20 20 65 78 70 6c 69   **        expli
32f02 63 69 74 6c 79 20 64 65 63 6c 61 72 65 64 20 63  citly declared c
32f03 6f 6c 75 6d 6e 2e 20 43 6f 70 79 20 6d 65 74 61  olumn. Copy meta
32f04 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f   information fro
32f05 6d 20 2a 70 43 6f 6c 2e 0a 20 20 2a 2f 20 0a 20  m *pCol..  */ . 
32f06 20 69 66 28 20 70 43 6f 6c 20 29 7b 0a 20 20 20   if( pCol ){.   
32f07 20 7a 44 61 74 61 54 79 70 65 20 3d 20 70 43 6f   zDataType = pCo
32f08 6c 2d 3e 7a 54 79 70 65 3b 0a 20 20 20 20 7a 43  l->zType;.    zC
32f09 6f 6c 6c 53 65 71 20 3d 20 70 43 6f 6c 2d 3e 7a  ollSeq = pCol->z
32f0a 43 6f 6c 6c 3b 0a 20 20 20 20 6e 6f 74 6e 75 6c  Coll;.    notnul
32f0b 6c 20 3d 20 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c  l = pCol->notNul
32f0c 6c 21 3d 30 3b 0a 20 20 20 20 70 72 69 6d 61 72  l!=0;.    primar
32f0d 79 6b 65 79 20 20 3d 20 70 43 6f 6c 2d 3e 69 73  ykey  = pCol->is
32f0e 50 72 69 6d 4b 65 79 21 3d 30 3b 0a 20 20 20 20  PrimKey!=0;.    
32f0f 61 75 74 6f 69 6e 63 20 3d 20 70 54 61 62 2d 3e  autoinc = pTab->
32f10 69 50 4b 65 79 3d 3d 69 43 6f 6c 20 26 26 20 28  iPKey==iCol && (
32f11 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26  pTab->tabFlags &
32f12 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e   TF_Autoincremen
32f13 74 29 21 3d 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  t)!=0;.  }else{.
32f14 20 20 20 20 7a 44 61 74 61 54 79 70 65 20 3d 20      zDataType = 
32f15 22 49 4e 54 45 47 45 52 22 3b 0a 20 20 20 20 70  "INTEGER";.    p
32f16 72 69 6d 61 72 79 6b 65 79 20 3d 20 31 3b 0a 20  rimarykey = 1;. 
32f17 20 7d 0a 20 20 69 66 28 20 21 7a 43 6f 6c 6c 53   }.  if( !zCollS
32f18 65 71 20 29 7b 0a 20 20 20 20 7a 43 6f 6c 6c 53  eq ){.    zCollS
32f19 65 71 20 3d 20 22 42 49 4e 41 52 59 22 3b 0a 20  eq = "BINARY";. 
32f1a 20 7d 0a 0a 65 72 72 6f 72 5f 6f 75 74 3a 0a 20   }..error_out:. 
32f1b 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
32f1c 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 28 76 6f  veAll(db);.  (vo
32f1d 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
32f1e 4f 66 66 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 57  Off(db);..  /* W
32f1f 68 65 74 68 65 72 20 74 68 65 20 66 75 6e 63 74  hether the funct
32f20 69 6f 6e 20 63 61 6c 6c 20 73 75 63 63 65 65 64  ion call succeed
32f21 65 64 20 6f 72 20 66 61 69 6c 65 64 2c 20 73 65  ed or failed, se
32f22 74 20 74 68 65 20 6f 75 74 70 75 74 20 70 61 72  t the output par
32f23 61 6d 65 74 65 72 73 0a 20 20 2a 2a 20 74 6f 20  ameters.  ** to 
32f24 77 68 61 74 65 76 65 72 20 74 68 65 69 72 20 6c  whatever their l
32f25 6f 63 61 6c 20 63 6f 75 6e 74 65 72 70 61 72 74  ocal counterpart
32f26 73 20 63 6f 6e 74 61 69 6e 2e 20 49 66 20 61 6e  s contain. If an
32f27 20 65 72 72 6f 72 20 64 69 64 20 6f 63 63 75 72   error did occur
32f28 2c 0a 20 20 2a 2a 20 74 68 69 73 20 68 61 73 20  ,.  ** this has 
32f29 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 7a 65  the effect of ze
32f2a 72 6f 69 6e 67 20 61 6c 6c 20 6f 75 74 70 75 74  roing all output
32f2b 20 70 61 72 61 6d 65 74 65 72 73 2e 0a 20 20 2a   parameters..  *
32f2c 2f 0a 20 20 69 66 28 20 70 7a 44 61 74 61 54 79  /.  if( pzDataTy
32f2d 70 65 20 29 20 2a 70 7a 44 61 74 61 54 79 70 65  pe ) *pzDataType
32f2e 20 3d 20 7a 44 61 74 61 54 79 70 65 3b 0a 20 20   = zDataType;.  
32f2f 69 66 28 20 70 7a 43 6f 6c 6c 53 65 71 20 29 20  if( pzCollSeq ) 
32f30 2a 70 7a 43 6f 6c 6c 53 65 71 20 3d 20 7a 43 6f  *pzCollSeq = zCo
32f31 6c 6c 53 65 71 3b 0a 20 20 69 66 28 20 70 4e 6f  llSeq;.  if( pNo
32f32 74 4e 75 6c 6c 20 29 20 2a 70 4e 6f 74 4e 75 6c  tNull ) *pNotNul
32f33 6c 20 3d 20 6e 6f 74 6e 75 6c 6c 3b 0a 20 20 69  l = notnull;.  i
32f34 66 28 20 70 50 72 69 6d 61 72 79 4b 65 79 20 29  f( pPrimaryKey )
32f35 20 2a 70 50 72 69 6d 61 72 79 4b 65 79 20 3d 20   *pPrimaryKey = 
32f36 70 72 69 6d 61 72 79 6b 65 79 3b 0a 20 20 69 66  primarykey;.  if
32f37 28 20 70 41 75 74 6f 69 6e 63 20 29 20 2a 70 41  ( pAutoinc ) *pA
32f38 75 74 6f 69 6e 63 20 3d 20 61 75 74 6f 69 6e 63  utoinc = autoinc
32f39 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  ;..  if( SQLITE_
32f3a 4f 4b 3d 3d 72 63 20 26 26 20 21 70 54 61 62 20  OK==rc && !pTab 
32f3b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  ){.    sqlite3Db
32f3c 46 72 65 65 28 64 62 2c 20 7a 45 72 72 4d 73 67  Free(db, zErrMsg
32f3d 29 3b 0a 20 20 20 20 7a 45 72 72 4d 73 67 20 3d  );.    zErrMsg =
32f3e 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
32f3f 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 74 61 62  db, "no such tab
32f40 6c 65 20 63 6f 6c 75 6d 6e 3a 20 25 73 2e 25 73  le column: %s.%s
32f41 22 2c 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 0a 20  ", zTableName,. 
32f42 20 20 20 20 20 20 20 7a 43 6f 6c 75 6d 6e 4e 61         zColumnNa
32f43 6d 65 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  me);.    rc = SQ
32f44 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
32f45 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
32f46 62 2c 20 72 63 2c 20 28 7a 45 72 72 4d 73 67 3f  b, rc, (zErrMsg?
32f47 22 25 73 22 3a 30 29 2c 20 7a 45 72 72 4d 73 67  "%s":0), zErrMsg
32f48 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
32f49 65 65 28 64 62 2c 20 7a 45 72 72 4d 73 67 29 3b  ee(db, zErrMsg);
32f4a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41  .  rc = sqlite3A
32f4b 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a  piExit(db, rc);.
32f4c 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
32f4d 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
32f4e 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
32f4f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53  .#endif../*.** S
32f50 6c 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c  leep for a littl
32f51 65 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e  e while.  Return
32f52 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74   the amount of t
32f53 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a 53 51  ime slept..*/.SQ
32f54 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
32f55 69 74 65 33 5f 73 6c 65 65 70 28 69 6e 74 20 6d  ite3_sleep(int m
32f56 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  s){.  sqlite3_vf
32f57 73 20 2a 70 56 66 73 3b 0a 20 20 69 6e 74 20 72  s *pVfs;.  int r
32f58 63 3b 0a 20 20 70 56 66 73 20 3d 20 73 71 6c 69  c;.  pVfs = sqli
32f59 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 30 29 3b  te3_vfs_find(0);
32f5a 0a 20 20 69 66 28 20 70 56 66 73 3d 3d 30 20 29  .  if( pVfs==0 )
32f5b 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a   return 0;..  /*
32f5c 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77   This function w
32f5d 6f 72 6b 73 20 69 6e 20 6d 69 6c 6c 69 73 65 63  orks in millisec
32f5e 6f 6e 64 73 2c 20 62 75 74 20 74 68 65 20 75 6e  onds, but the un
32f5f 64 65 72 6c 79 69 6e 67 20 4f 73 53 6c 65 65 70  derlying OsSleep
32f60 28 29 20 0a 20 20 2a 2a 20 41 50 49 20 75 73 65  () .  ** API use
32f61 73 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 2e 20  s microseconds. 
32f62 48 65 6e 63 65 20 74 68 65 20 31 30 30 30 27 73  Hence the 1000's
32f63 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 28 73  ..  */.  rc = (s
32f64 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 70 56  qlite3OsSleep(pV
32f65 66 73 2c 20 31 30 30 30 2a 6d 73 29 2f 31 30 30  fs, 1000*ms)/100
32f66 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  0);.  return rc;
32f67 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65  .}../*.** Enable
32f68 20 6f 72 20 64 69 73 61 62 6c 65 20 74 68 65 20   or disable the 
32f69 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
32f6a 63 6f 64 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  codes..*/.SQLITE
32f6b 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
32f6c 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74  _extended_result
32f6d 5f 63 6f 64 65 73 28 73 71 6c 69 74 65 33 20 2a  _codes(sqlite3 *
32f6e 64 62 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 7b 0a  db, int onoff){.
32f6f 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
32f70 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
32f71 3b 0a 20 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20  ;.  db->errMask 
32f72 3d 20 6f 6e 6f 66 66 20 3f 20 30 78 66 66 66 66  = onoff ? 0xffff
32f73 66 66 66 66 20 3a 20 30 78 66 66 3b 0a 20 20 73  ffff : 0xff;.  s
32f74 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
32f75 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
32f76 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
32f77 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f  K;.}../*.** Invo
32f78 6b 65 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74  ke the xFileCont
32f79 72 6f 6c 20 6d 65 74 68 6f 64 20 6f 6e 20 61 20  rol method on a 
32f7a 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62  particular datab
32f7b 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ase..*/.SQLITE_A
32f7c 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66  PI int sqlite3_f
32f7d 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 73 71 6c 69  ile_control(sqli
32f7e 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
32f7f 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 69 6e  har *zDbName, in
32f80 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67  t op, void *pArg
32f81 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
32f82 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69 6e  LITE_ERROR;.  in
32f83 74 20 69 44 62 3b 0a 20 20 73 71 6c 69 74 65 33  t iDb;.  sqlite3
32f84 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
32f85 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 7a  >mutex);.  if( z
32f86 44 62 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20  DbName==0 ){.   
32f87 20 69 44 62 20 3d 20 30 3b 0a 20 20 7d 65 6c 73   iDb = 0;.  }els
32f88 65 7b 0a 20 20 20 20 66 6f 72 28 69 44 62 3d 30  e{.    for(iDb=0
32f89 3b 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 69  ; iDb<db->nDb; i
32f8a 44 62 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28  Db++){.      if(
32f8b 20 73 74 72 63 6d 70 28 64 62 2d 3e 61 44 62 5b   strcmp(db->aDb[
32f8c 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 44 62 4e  iDb].zName, zDbN
32f8d 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b  ame)==0 ) break;
32f8e 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
32f8f 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 7b 0a   iDb<db->nDb ){.
32f90 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 72 65      Btree *pBtre
32f91 65 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  e = db->aDb[iDb]
32f92 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 42  .pBt;.    if( pB
32f93 74 72 65 65 20 29 7b 0a 20 20 20 20 20 20 50 61  tree ){.      Pa
32f94 67 65 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20  ger *pPager;.   
32f95 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20     sqlite3_file 
32f96 2a 66 64 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  *fd;.      sqlit
32f97 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 42 74  e3BtreeEnter(pBt
32f98 72 65 65 29 3b 0a 20 20 20 20 20 20 70 50 61 67  ree);.      pPag
32f99 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  er = sqlite3Btre
32f9a 65 50 61 67 65 72 28 70 42 74 72 65 65 29 3b 0a  ePager(pBtree);.
32f9b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
32f9c 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 20 20 20  ager!=0 );.     
32f9d 20 66 64 20 3d 20 73 71 6c 69 74 65 33 50 61 67   fd = sqlite3Pag
32f9e 65 72 46 69 6c 65 28 70 50 61 67 65 72 29 3b 0a  erFile(pPager);.
32f9f 20 20 20 20 20 20 61 73 73 65 72 74 28 20 66 64        assert( fd
32fa0 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28  !=0 );.      if(
32fa1 20 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b   fd->pMethods ){
32fa2 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
32fa3 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72  lite3OsFileContr
32fa4 6f 6c 28 66 64 2c 20 6f 70 2c 20 70 41 72 67 29  ol(fd, op, pArg)
32fa5 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
32fa6 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
32fa7 65 28 70 42 74 72 65 65 29 3b 0a 20 20 20 20 7d  e(pBtree);.    }
32fa8 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
32fa9 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
32faa 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
32fab 72 63 3b 20 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rc;   .}../*.** 
32fac 49 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 65  Interface to the
32fad 20 74 65 73 74 69 6e 67 20 6c 6f 67 69 63 2e 0a   testing logic..
32fae 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
32faf 74 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63  t sqlite3_test_c
32fb0 6f 6e 74 72 6f 6c 28 69 6e 74 20 6f 70 2c 20 2e  ontrol(int op, .
32fb1 2e 2e 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ..){.  int rc = 
32fb2 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  0;.#ifndef SQLIT
32fb3 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54  E_OMIT_BUILTIN_T
32fb4 45 53 54 0a 20 20 76 61 5f 6c 69 73 74 20 61 70  EST.  va_list ap
32fb5 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c  ;.  va_start(ap,
32fb6 20 6f 70 29 3b 0a 20 20 73 77 69 74 63 68 28 20   op);.  switch( 
32fb7 6f 70 20 29 7b 0a 0a 20 20 20 20 2f 2a 0a 20 20  op ){..    /*.  
32fb8 20 20 2a 2a 20 53 61 76 65 20 74 68 65 20 63 75    ** Save the cu
32fb9 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 20 74  rrent state of t
32fba 68 65 20 50 52 4e 47 2e 0a 20 20 20 20 2a 2f 0a  he PRNG..    */.
32fbb 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
32fbc 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 53 41  TESTCTRL_PRNG_SA
32fbd 56 45 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  VE: {.      sqli
32fbe 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 74 65  te3PrngSaveState
32fbf 28 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ();.      break;
32fc0 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20  .    }..    /*. 
32fc1 20 20 20 2a 2a 20 52 65 73 74 6f 72 65 20 74 68     ** Restore th
32fc2 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 50  e state of the P
32fc3 52 4e 47 20 74 6f 20 74 68 65 20 6c 61 73 74 20  RNG to the last 
32fc4 73 74 61 74 65 20 73 61 76 65 64 20 75 73 69 6e  state saved usin
32fc5 67 0a 20 20 20 20 2a 2a 20 50 52 4e 47 5f 53 41  g.    ** PRNG_SA
32fc6 56 45 2e 20 20 49 66 20 50 52 4e 47 5f 53 41 56  VE.  If PRNG_SAV
32fc7 45 20 68 61 73 20 6e 65 76 65 72 20 62 65 66 6f  E has never befo
32fc8 72 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 20  re been called, 
32fc9 74 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 69 73  then.    ** this
32fca 20 76 65 72 62 20 61 63 74 73 20 6c 69 6b 65 20   verb acts like 
32fcb 50 52 4e 47 5f 52 45 53 45 54 2e 0a 20 20 20 20  PRNG_RESET..    
32fcc 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  */.    case SQLI
32fcd 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47  TE_TESTCTRL_PRNG
32fce 5f 52 45 53 54 4f 52 45 3a 20 7b 0a 20 20 20 20  _RESTORE: {.    
32fcf 20 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73    sqlite3PrngRes
32fd0 74 6f 72 65 53 74 61 74 65 28 29 3b 0a 20 20 20  toreState();.   
32fd1 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
32fd2 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 52  .    /*.    ** R
32fd3 65 73 65 74 20 74 68 65 20 50 52 4e 47 20 62 61  eset the PRNG ba
32fd4 63 6b 20 74 6f 20 69 74 73 20 75 6e 69 6e 69 74  ck to its uninit
32fd5 69 61 6c 69 7a 65 64 20 73 74 61 74 65 2e 20 20  ialized state.  
32fd6 54 68 65 20 6e 65 78 74 20 63 61 6c 6c 0a 20 20  The next call.  
32fd7 20 20 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f    ** to sqlite3_
32fd8 72 61 6e 64 6f 6d 6e 65 73 73 28 29 20 77 69 6c  randomness() wil
32fd9 6c 20 72 65 73 65 65 64 20 74 68 65 20 50 52 4e  l reseed the PRN
32fda 47 20 75 73 69 6e 67 20 61 20 73 69 6e 67 6c 65  G using a single
32fdb 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20   call.    ** to 
32fdc 74 68 65 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20  the xRandomness 
32fdd 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 64 65  method of the de
32fde 66 61 75 6c 74 20 56 46 53 2e 0a 20 20 20 20 2a  fault VFS..    *
32fdf 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  /.    case SQLIT
32fe0 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f  E_TESTCTRL_PRNG_
32fe1 52 45 53 45 54 3a 20 7b 0a 20 20 20 20 20 20 73  RESET: {.      s
32fe2 71 6c 69 74 65 33 50 72 6e 67 52 65 73 65 74 53  qlite3PrngResetS
32fe3 74 61 74 65 28 29 3b 0a 20 20 20 20 20 20 62 72  tate();.      br
32fe4 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
32fe5 2f 2a 0a 20 20 20 20 2a 2a 20 20 73 71 6c 69 74  /*.    **  sqlit
32fe6 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28  e3_test_control(
32fe7 42 49 54 56 45 43 5f 54 45 53 54 2c 20 73 69 7a  BITVEC_TEST, siz
32fe8 65 2c 20 70 72 6f 67 72 61 6d 29 0a 20 20 20 20  e, program).    
32fe9 2a 2a 0a 20 20 20 20 2a 2a 20 52 75 6e 20 61 20  **.    ** Run a 
32fea 74 65 73 74 20 61 67 61 69 6e 73 74 20 61 20 42  test against a B
32feb 69 74 76 65 63 20 6f 62 6a 65 63 74 20 6f 66 20  itvec object of 
32fec 73 69 7a 65 2e 20 20 54 68 65 20 70 72 6f 67 72  size.  The progr
32fed 61 6d 20 61 72 67 75 6d 65 6e 74 0a 20 20 20 20  am argument.    
32fee 2a 2a 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f  ** is an array o
32fef 66 20 69 6e 74 65 67 65 72 73 20 74 68 61 74 20  f integers that 
32ff0 64 65 66 69 6e 65 73 20 74 68 65 20 74 65 73 74  defines the test
32ff1 2e 20 20 52 65 74 75 72 6e 20 2d 31 20 6f 6e 20  .  Return -1 on 
32ff2 61 0a 20 20 20 20 2a 2a 20 6d 65 6d 6f 72 79 20  a.    ** memory 
32ff3 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72  allocation error
32ff4 2c 20 30 20 6f 6e 20 73 75 63 63 65 73 73 2c 20  , 0 on success, 
32ff5 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f 72 20  or non-zero for 
32ff6 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20 20 2a 2a  an error..    **
32ff7 20 53 65 65 20 74 68 65 20 73 71 6c 69 74 65 33   See the sqlite3
32ff8 42 69 74 76 65 63 42 75 69 6c 74 69 6e 54 65 73  BitvecBuiltinTes
32ff9 74 28 29 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  t() for addition
32ffa 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  al information..
32ffb 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
32ffc 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
32ffd 42 49 54 56 45 43 5f 54 45 53 54 3a 20 7b 0a 20  BITVEC_TEST: {. 
32ffe 20 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 76 61       int sz = va
32fff 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20  _arg(ap, int);. 
33000 20 20 20 20 20 69 6e 74 20 2a 61 50 72 6f 67 20       int *aProg 
33001 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
33002 2a 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  *);.      rc = s
33003 71 6c 69 74 65 33 42 69 74 76 65 63 42 75 69 6c  qlite3BitvecBuil
33004 74 69 6e 54 65 73 74 28 73 7a 2c 20 61 50 72 6f  tinTest(sz, aPro
33005 67 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  g);.      break;
33006 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20  .    }..    /*. 
33007 20 20 20 2a 2a 20 20 73 71 6c 69 74 65 33 5f 74     **  sqlite3_t
33008 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 42 45 4e 49  est_control(BENI
33009 47 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 2c  GN_MALLOC_HOOKS,
3300a 20 78 42 65 67 69 6e 2c 20 78 45 6e 64 29 0a 20   xBegin, xEnd). 
3300b 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 52 65 67     **.    ** Reg
3300c 69 73 74 65 72 20 68 6f 6f 6b 73 20 74 6f 20 63  ister hooks to c
3300d 61 6c 6c 20 74 6f 20 69 6e 64 69 63 61 74 65 20  all to indicate 
3300e 77 68 69 63 68 20 6d 61 6c 6c 6f 63 28 29 20 66  which malloc() f
3300f 61 69 6c 75 72 65 73 20 0a 20 20 20 20 2a 2a 20  ailures .    ** 
33010 61 72 65 20 62 65 6e 69 67 6e 2e 0a 20 20 20 20  are benign..    
33011 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  */.    case SQLI
33012 54 45 5f 54 45 53 54 43 54 52 4c 5f 42 45 4e 49  TE_TESTCTRL_BENI
33013 47 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 3a  GN_MALLOC_HOOKS:
33014 20 7b 0a 20 20 20 20 20 20 74 79 70 65 64 65 66   {.      typedef
33015 20 76 6f 69 64 20 28 2a 76 6f 69 64 5f 66 75 6e   void (*void_fun
33016 63 74 69 6f 6e 29 28 76 6f 69 64 29 3b 0a 20 20  ction)(void);.  
33017 20 20 20 20 76 6f 69 64 5f 66 75 6e 63 74 69 6f      void_functio
33018 6e 20 78 42 65 6e 69 67 6e 42 65 67 69 6e 3b 0a  n xBenignBegin;.
33019 20 20 20 20 20 20 76 6f 69 64 5f 66 75 6e 63 74        void_funct
3301a 69 6f 6e 20 78 42 65 6e 69 67 6e 45 6e 64 3b 0a  ion xBenignEnd;.
3301b 20 20 20 20 20 20 78 42 65 6e 69 67 6e 42 65 67        xBenignBeg
3301c 69 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  in = va_arg(ap, 
3301d 76 6f 69 64 5f 66 75 6e 63 74 69 6f 6e 29 3b 0a  void_function);.
3301e 20 20 20 20 20 20 78 42 65 6e 69 67 6e 45 6e 64        xBenignEnd
3301f 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 76 6f   = va_arg(ap, vo
33020 69 64 5f 66 75 6e 63 74 69 6f 6e 29 3b 0a 20 20  id_function);.  
33021 20 20 20 20 73 71 6c 69 74 65 33 42 65 6e 69 67      sqlite3Benig
33022 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 28 78 42 65  nMallocHooks(xBe
33023 6e 69 67 6e 42 65 67 69 6e 2c 20 78 42 65 6e 69  nignBegin, xBeni
33024 67 6e 45 6e 64 29 3b 0a 20 20 20 20 20 20 62 72  gnEnd);.      br
33025 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
33026 2f 2a 0a 20 20 20 20 2a 2a 20 20 73 71 6c 69 74  /*.    **  sqlit
33027 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28  e3_test_control(
33028 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
33029 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 75 6e  PENDING_BYTE, un
3302a 73 69 67 6e 65 64 20 69 6e 74 20 58 29 0a 20 20  signed int X).  
3302b 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 53 65 74 20    **.    ** Set 
3302c 74 68 65 20 50 45 4e 44 49 4e 47 20 62 79 74 65  the PENDING byte
3302d 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 69 6e   to the value in
3302e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 69   the argument, i
3302f 66 20 58 3e 30 2e 0a 20 20 20 20 2a 2a 20 4d 61  f X>0..    ** Ma
33030 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 20 69 66  ke no changes if
33031 20 58 3d 3d 30 2e 20 20 52 65 74 75 72 6e 20 74   X==0.  Return t
33032 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
33033 70 65 6e 64 69 6e 67 20 62 79 74 65 0a 20 20 20  pending byte.   
33034 20 2a 2a 20 61 73 20 69 74 20 65 78 69 73 74 69   ** as it existi
33035 6e 67 20 62 65 66 6f 72 65 20 74 68 69 73 20 72  ng before this r
33036 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65  outine was calle
33037 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  d..    **.    **
33038 20 49 4d 50 4f 52 54 41 4e 54 3a 20 20 43 68 61   IMPORTANT:  Cha
33039 6e 67 69 6e 67 20 74 68 65 20 50 45 4e 44 49 4e  nging the PENDIN
3303a 47 20 62 79 74 65 20 66 72 6f 6d 20 30 78 34 30  G byte from 0x40
3303b 30 30 30 30 30 30 20 72 65 73 75 6c 74 73 20 69  000000 results i
3303c 6e 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 63 6f  n.    ** an inco
3303d 6d 70 61 74 69 62 6c 65 20 64 61 74 61 62 61 73  mpatible databas
3303e 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 20 20  e file format.  
3303f 43 68 61 6e 67 69 6e 67 20 74 68 65 20 50 45 4e  Changing the PEN
33040 44 49 4e 47 20 62 79 74 65 0a 20 20 20 20 2a 2a  DING byte.    **
33041 20 77 68 69 6c 65 20 61 6e 79 20 64 61 74 61 62   while any datab
33042 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  ase connection i
33043 73 20 6f 70 65 6e 20 72 65 73 75 6c 74 73 20 69  s open results i
33044 6e 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 0a  n undefined and.
33045 20 20 20 20 2a 2a 20 64 69 6c 65 74 65 72 69 6f      ** dileterio
33046 75 73 20 62 65 68 61 76 69 6f 72 2e 0a 20 20 20  us behavior..   
33047 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c   */.    case SQL
33048 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 45 4e  ITE_TESTCTRL_PEN
33049 44 49 4e 47 5f 42 59 54 45 3a 20 7b 0a 20 20 20  DING_BYTE: {.   
3304a 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20     unsigned int 
3304b 6e 65 77 56 61 6c 20 3d 20 76 61 5f 61 72 67 28  newVal = va_arg(
3304c 61 70 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  ap, unsigned int
3304d 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  );.      rc = sq
3304e 6c 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65  lite3PendingByte
3304f 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 77 56  ;.      if( newV
33050 61 6c 20 29 20 73 71 6c 69 74 65 33 50 65 6e 64  al ) sqlite3Pend
33051 69 6e 67 42 79 74 65 20 3d 20 6e 65 77 56 61 6c  ingByte = newVal
33052 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
33053 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20     }..    /*.   
33054 20 2a 2a 20 20 73 71 6c 69 74 65 33 5f 74 65 73   **  sqlite3_tes
33055 74 5f 63 6f 6e 74 72 6f 6c 28 53 51 4c 49 54 45  t_control(SQLITE
33056 5f 54 45 53 54 43 54 52 4c 5f 41 53 53 45 52 54  _TESTCTRL_ASSERT
33057 2c 20 69 6e 74 20 58 29 0a 20 20 20 20 2a 2a 0a  , int X).    **.
33058 20 20 20 20 2a 2a 20 54 68 69 73 20 61 63 74 69      ** This acti
33059 6f 6e 20 70 72 6f 76 69 64 65 73 20 61 20 72 75  on provides a ru
3305a 6e 2d 74 69 6d 65 20 74 65 73 74 20 74 6f 20 73  n-time test to s
3305b 65 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ee whether or no
3305c 74 0a 20 20 20 20 2a 2a 20 61 73 73 65 72 74 28  t.    ** assert(
3305d 29 20 77 61 73 20 65 6e 61 62 6c 65 64 20 61 74  ) was enabled at
3305e 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 20 20   compile-time.  
3305f 49 66 20 58 20 69 73 20 74 72 75 65 20 61 6e 64  If X is true and
33060 20 61 73 73 65 72 74 28 29 0a 20 20 20 20 2a 2a   assert().    **
33061 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65   is enabled, the
33062 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  n the return val
33063 75 65 20 69 73 20 74 72 75 65 2e 20 20 49 66 20  ue is true.  If 
33064 58 20 69 73 20 74 72 75 65 20 61 6e 64 0a 20 20  X is true and.  
33065 20 20 2a 2a 20 61 73 73 65 72 74 28 29 20 69 73    ** assert() is
33066 20 64 69 73 61 62 6c 65 64 2c 20 74 68 65 6e 20   disabled, then 
33067 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
33068 20 69 73 20 7a 65 72 6f 2e 20 20 49 66 20 58 20   is zero.  If X 
33069 69 73 0a 20 20 20 20 2a 2a 20 66 61 6c 73 65 20  is.    ** false 
3306a 61 6e 64 20 61 73 73 65 72 74 28 29 20 69 73 20  and assert() is 
3306b 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 68  enabled, then th
3306c 65 20 61 73 73 65 72 74 69 6f 6e 20 66 69 72 65  e assertion fire
3306d 73 20 61 6e 64 20 74 68 65 0a 20 20 20 20 2a 2a  s and the.    **
3306e 20 70 72 6f 63 65 73 73 20 61 62 6f 72 74 73 2e   process aborts.
3306f 20 20 49 66 20 58 20 69 73 20 66 61 6c 73 65 20    If X is false 
33070 61 6e 64 20 61 73 73 65 72 74 28 29 20 69 73 20  and assert() is 
33071 64 69 73 61 62 6c 65 64 2c 20 74 68 65 6e 20 74  disabled, then t
33072 68 65 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e  he.    ** return
33073 20 76 61 6c 75 65 20 69 73 20 7a 65 72 6f 2e 0a   value is zero..
33074 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
33075 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
33076 41 53 53 45 52 54 3a 20 7b 0a 20 20 20 20 20 20  ASSERT: {.      
33077 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 78 20 3d  volatile int x =
33078 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   0;.      assert
33079 28 20 28 78 20 3d 20 76 61 5f 61 72 67 28 61 70  ( (x = va_arg(ap
3307a 2c 69 6e 74 29 29 21 3d 30 20 29 3b 0a 20 20 20  ,int))!=0 );.   
3307b 20 20 20 72 63 20 3d 20 78 3b 0a 20 20 20 20 20     rc = x;.     
3307c 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 0a   break;.    }...
3307d 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20 73      /*.    **  s
3307e 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74  qlite3_test_cont
3307f 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53 54 43  rol(SQLITE_TESTC
33080 54 52 4c 5f 41 4c 57 41 59 53 2c 20 69 6e 74 20  TRL_ALWAYS, int 
33081 58 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  X).    **.    **
33082 20 54 68 69 73 20 61 63 74 69 6f 6e 20 70 72 6f   This action pro
33083 76 69 64 65 73 20 61 20 72 75 6e 2d 74 69 6d 65  vides a run-time
33084 20 74 65 73 74 20 74 6f 20 73 65 65 20 68 6f 77   test to see how
33085 20 74 68 65 20 41 4c 57 41 59 53 20 61 6e 64 0a   the ALWAYS and.
33086 20 20 20 20 2a 2a 20 4e 45 56 45 52 20 6d 61 63      ** NEVER mac
33087 72 6f 73 20 77 65 72 65 20 64 65 66 69 6e 65 64  ros were defined
33088 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65   at compile-time
33089 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
3308a 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
3308b 20 69 73 20 41 4c 57 41 59 53 28 58 29 2e 20 20   is ALWAYS(X).  
3308c 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
3308d 68 65 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 74  he recommended t
3308e 65 73 74 20 69 73 20 58 3d 3d 32 2e 20 20 49 66  est is X==2.  If
3308f 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
33090 65 20 69 73 20 32 2c 20 74 68 61 74 20 6d 65 61  e is 2, that mea
33091 6e 73 0a 20 20 20 20 2a 2a 20 41 4c 57 41 59 53  ns.    ** ALWAYS
33092 28 29 20 61 6e 64 20 4e 45 56 45 52 28 29 20 61  () and NEVER() a
33093 72 65 20 62 6f 74 68 20 6e 6f 2d 6f 70 20 70 61  re both no-op pa
33094 73 73 2d 74 68 72 6f 75 67 68 20 6d 61 63 72 6f  ss-through macro
33095 73 2c 20 77 68 69 63 68 20 69 73 20 74 68 65 0a  s, which is the.
33096 20 20 20 20 2a 2a 20 64 65 66 61 75 6c 74 20 73      ** default s
33097 65 74 74 69 6e 67 2e 20 20 49 66 20 74 68 65 20  etting.  If the 
33098 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
33099 31 2c 20 74 68 65 6e 20 41 4c 57 41 59 53 28 29  1, then ALWAYS()
3309a 20 69 73 20 65 69 74 68 65 72 0a 20 20 20 20 2a   is either.    *
3309b 2a 20 68 61 72 64 2d 63 6f 64 65 64 20 74 6f 20  * hard-coded to 
3309c 74 72 75 65 20 6f 72 20 65 6c 73 65 20 69 74 20  true or else it 
3309d 61 73 73 65 72 74 73 20 69 66 20 69 74 73 20 61  asserts if its a
3309e 72 67 75 6d 65 6e 74 20 69 73 20 66 61 6c 73 65  rgument is false
3309f 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 69 72  ..    ** The fir
330a0 73 74 20 62 65 68 61 76 69 6f 72 20 28 68 61 72  st behavior (har
330a1 64 2d 63 6f 64 65 64 20 74 6f 20 74 72 75 65 29  d-coded to true)
330a2 20 69 73 20 74 68 65 20 63 61 73 65 20 69 66 0a   is the case if.
330a3 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 54 45      ** SQLITE_TE
330a4 53 54 43 54 52 4c 5f 41 53 53 45 52 54 20 73 68  STCTRL_ASSERT sh
330a5 6f 77 73 20 74 68 61 74 20 61 73 73 65 72 74 28  ows that assert(
330a6 29 20 69 73 20 64 69 73 61 62 6c 65 64 20 61 6e  ) is disabled an
330a7 64 20 74 68 65 20 73 65 63 6f 6e 64 0a 20 20 20  d the second.   
330a8 20 2a 2a 20 62 65 68 61 76 69 6f 72 20 28 61 73   ** behavior (as
330a9 73 65 72 74 20 69 66 20 74 68 65 20 61 72 67 75  sert if the argu
330aa 6d 65 6e 74 20 74 6f 20 41 4c 57 41 59 53 28 29  ment to ALWAYS()
330ab 20 69 73 20 66 61 6c 73 65 29 20 69 73 20 74 68   is false) is th
330ac 65 20 63 61 73 65 20 69 66 0a 20 20 20 20 2a 2a  e case if.    **
330ad 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c   SQLITE_TESTCTRL
330ae 5f 41 53 53 45 52 54 20 73 68 6f 77 73 20 74 68  _ASSERT shows th
330af 61 74 20 61 73 73 65 72 74 28 29 20 69 73 20 65  at assert() is e
330b0 6e 61 62 6c 65 64 2e 0a 20 20 20 20 2a 2a 0a 20  nabled..    **. 
330b1 20 20 20 2a 2a 20 54 68 65 20 72 75 6e 2d 74 69     ** The run-ti
330b2 6d 65 20 74 65 73 74 20 70 72 6f 63 65 64 75 72  me test procedur
330b3 65 20 6d 69 67 68 74 20 6c 6f 6f 6b 20 73 6f 6d  e might look som
330b4 65 74 68 69 6e 67 20 6c 69 6b 65 20 74 68 69 73  ething like this
330b5 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  :.    **.    ** 
330b6 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 74     if( sqlite3_t
330b7 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 53 51 4c 49  est_control(SQLI
330b8 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 4c 57 41  TE_TESTCTRL_ALWA
330b9 59 53 2c 20 32 29 3d 3d 32 20 29 7b 0a 20 20 20  YS, 2)==2 ){.   
330ba 20 2a 2a 20 20 20 20 20 20 2f 2f 20 41 4c 57 41   **      // ALWA
330bb 59 53 28 29 20 61 6e 64 20 4e 45 56 45 52 28 29  YS() and NEVER()
330bc 20 61 72 65 20 6e 6f 2d 6f 70 20 70 61 73 73 2d   are no-op pass-
330bd 74 68 72 6f 75 67 68 20 6d 61 63 72 6f 73 0a 20  through macros. 
330be 20 20 20 2a 2a 20 20 20 20 7d 65 6c 73 65 20 69     **    }else i
330bf 66 28 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f  f( sqlite3_test_
330c0 63 6f 6e 74 72 6f 6c 28 53 51 4c 49 54 45 5f 54  control(SQLITE_T
330c1 45 53 54 43 54 52 4c 5f 41 53 53 45 52 54 2c 20  ESTCTRL_ASSERT, 
330c2 31 29 20 29 7b 0a 20 20 20 20 2a 2a 20 20 20 20  1) ){.    **    
330c3 20 20 2f 2f 20 41 4c 57 41 59 53 28 78 29 20 61    // ALWAYS(x) a
330c4 73 73 65 72 74 73 20 74 68 61 74 20 78 20 69 73  sserts that x is
330c5 20 74 72 75 65 2e 20 4e 45 56 45 52 28 78 29 20   true. NEVER(x) 
330c6 61 73 73 65 72 74 73 20 78 20 69 73 20 66 61 6c  asserts x is fal
330c7 73 65 2e 0a 20 20 20 20 2a 2a 20 20 20 20 7d 65  se..    **    }e
330c8 6c 73 65 7b 0a 20 20 20 20 2a 2a 20 20 20 20 20  lse{.    **     
330c9 20 2f 2f 20 41 4c 57 41 59 53 28 78 29 20 69 73   // ALWAYS(x) is
330ca 20 61 20 63 6f 6e 73 74 61 6e 74 20 31 2e 20 20   a constant 1.  
330cb 4e 45 56 45 52 28 78 29 20 69 73 20 61 20 63 6f  NEVER(x) is a co
330cc 6e 73 74 61 6e 74 20 30 2e 0a 20 20 20 20 2a 2a  nstant 0..    **
330cd 20 20 20 20 7d 0a 20 20 20 20 2a 2f 0a 20 20 20      }.    */.   
330ce 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53   case SQLITE_TES
330cf 54 43 54 52 4c 5f 41 4c 57 41 59 53 3a 20 7b 0a  TCTRL_ALWAYS: {.
330d0 20 20 20 20 20 20 69 6e 74 20 78 20 3d 20 76 61        int x = va
330d1 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20  _arg(ap,int);.  
330d2 20 20 20 20 72 63 20 3d 20 41 4c 57 41 59 53 28      rc = ALWAYS(
330d3 78 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  x);.      break;
330d4 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 20  .    }..    /*  
330d5 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f   sqlite3_test_co
330d6 6e 74 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53  ntrol(SQLITE_TES
330d7 54 43 54 52 4c 5f 52 45 53 45 52 56 45 2c 20 73  TCTRL_RESERVE, s
330d8 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
330d9 4e 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  N).    **.    **
330da 20 53 65 74 20 74 68 65 20 6e 52 65 73 65 72 76   Set the nReserv
330db 65 20 73 69 7a 65 20 74 6f 20 4e 20 66 6f 72 20  e size to N for 
330dc 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
330dd 65 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  e on the databas
330de 65 0a 20 20 20 20 2a 2a 20 63 6f 6e 6e 65 63 74  e.    ** connect
330df 69 6f 6e 20 64 62 2e 0a 20 20 20 20 2a 2f 0a 20  ion db..    */. 
330e0 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54     case SQLITE_T
330e1 45 53 54 43 54 52 4c 5f 52 45 53 45 52 56 45 3a  ESTCTRL_RESERVE:
330e2 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
330e3 20 2a 64 62 20 3d 20 76 61 5f 61 72 67 28 61 70   *db = va_arg(ap
330e4 2c 20 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 20  , sqlite3*);.   
330e5 20 20 20 69 6e 74 20 78 20 3d 20 76 61 5f 61 72     int x = va_ar
330e6 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20  g(ap,int);.     
330e7 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
330e8 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
330e9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  .      sqlite3Bt
330ea 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 64  reeSetPageSize(d
330eb 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 2c 20 30  b->aDb[0].pBt, 0
330ec 2c 20 78 2c 20 30 29 3b 0a 20 20 20 20 20 20 73  , x, 0);.      s
330ed 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
330ee 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
330ef 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
330f0 7d 0a 0a 20 20 7d 0a 20 20 76 61 5f 65 6e 64 28  }..  }.  va_end(
330f1 61 70 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ap);.#endif /* S
330f2 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54  QLITE_OMIT_BUILT
330f3 49 4e 5f 54 45 53 54 20 2a 2f 0a 20 20 72 65 74  IN_TEST */.  ret
330f4 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  urn rc;.}../****
330f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
330f6 66 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f main.c *******
330f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
330f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
330f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
330fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
330fb 20 66 69 6c 65 20 6e 6f 74 69 66 79 2e 63 20 2a   file notify.c *
330fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
330fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
330fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
330ff 20 32 30 30 39 20 4d 61 72 63 68 20 33 0a 2a 2a   2009 March 3.**
33100 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
33101 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
33102 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
33103 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
33104 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
33105 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
33106 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
33107 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
33108 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
33109 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
3310a 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
3310b 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
3310c 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
3310d 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
3310e 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
3310f 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
33110 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
33111 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
33112 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33113 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33114 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33115 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
33116 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
33117 74 61 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d  tains the implem
33118 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
33119 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e  sqlite3_unlock_n
3311a 6f 74 69 66 79 28 29 0a 2a 2a 20 41 50 49 20 6d  otify().** API m
3311b 65 74 68 6f 64 20 61 6e 64 20 69 74 73 20 61 73  ethod and its as
3311c 73 6f 63 69 61 74 65 64 20 66 75 6e 63 74 69 6f  sociated functio
3311d 6e 61 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 24 49  nality..**.** $I
3311e 64 3a 20 6e 6f 74 69 66 79 2e 63 2c 76 20 31 2e  d: notify.c,v 1.
3311f 34 20 32 30 30 39 2f 30 34 2f 30 37 20 32 32 3a  4 2009/04/07 22:
33120 30 36 3a 35 37 20 64 72 68 20 45 78 70 20 24 0a  06:57 drh Exp $.
33121 2a 2f 0a 0a 2f 2a 20 4f 6d 69 74 20 74 68 69 73  */../* Omit this
33122 20 65 6e 74 69 72 65 20 66 69 6c 65 20 69 66 20   entire file if 
33123 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e  SQLITE_ENABLE_UN
33124 4c 4f 43 4b 5f 4e 4f 54 49 46 59 20 69 73 20 6e  LOCK_NOTIFY is n
33125 6f 74 20 64 65 66 69 6e 65 64 2e 20 2a 2f 0a 23  ot defined. */.#
33126 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
33127 42 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46  BLE_UNLOCK_NOTIF
33128 59 0a 0a 2f 2a 0a 2a 2a 20 50 75 62 6c 69 63 20  Y../*.** Public 
33129 69 6e 74 65 72 66 61 63 65 73 3a 0a 2a 2a 0a 2a  interfaces:.**.*
3312a 2a 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65  *   sqlite3Conne
3312b 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 29 0a 2a  ctionBlocked().*
3312c 2a 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65  *   sqlite3Conne
3312d 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 29 0a  ctionUnlocked().
3312e 2a 2a 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e  **   sqlite3Conn
3312f 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28 29 0a 2a  ectionClosed().*
33130 2a 20 20 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f  *   sqlite3_unlo
33131 63 6b 5f 6e 6f 74 69 66 79 28 29 0a 2a 2f 0a 0a  ck_notify().*/..
33132 23 64 65 66 69 6e 65 20 61 73 73 65 72 74 4d 75  #define assertMu
33133 74 65 78 48 65 6c 64 28 29 20 5c 0a 20 20 61 73  texHeld() \.  as
33134 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
33135 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33  tex_held(sqlite3
33136 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
33137 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
33138 41 53 54 45 52 29 29 20 29 0a 0a 2f 2a 0a 2a 2a  ASTER)) )../*.**
33139 20 48 65 61 64 20 6f 66 20 61 20 6c 69 6e 6b 65   Head of a linke
3313a 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 73 71  d list of all sq
3313b 6c 69 74 65 33 20 6f 62 6a 65 63 74 73 20 63 72  lite3 objects cr
3313c 65 61 74 65 64 20 62 79 20 74 68 69 73 20 70 72  eated by this pr
3313d 6f 63 65 73 73 0a 2a 2a 20 66 6f 72 20 77 68 69  ocess.** for whi
3313e 63 68 20 65 69 74 68 65 72 20 73 71 6c 69 74 65  ch either sqlite
3313f 33 2e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65  3.pBlockingConne
33140 63 74 69 6f 6e 20 6f 72 20 73 71 6c 69 74 65 33  ction or sqlite3
33141 2e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69  .pUnlockConnecti
33142 6f 6e 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e 55 4c  on.** is not NUL
33143 4c 2e 20 54 68 69 73 20 76 61 72 69 61 62 6c 65  L. This variable
33144 20 6d 61 79 20 6f 6e 6c 79 20 61 63 63 65 73 73   may only access
33145 65 64 20 77 68 69 6c 65 20 74 68 65 20 53 54 41  ed while the STA
33146 54 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 6d 75  TIC_MASTER.** mu
33147 74 65 78 20 69 73 20 68 65 6c 64 2e 0a 2a 2f 0a  tex is held..*/.
33148 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 20 2a  static sqlite3 *
33149 53 51 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 74  SQLITE_WSD sqlit
3314a 65 33 42 6c 6f 63 6b 65 64 4c 69 73 74 20 3d 20  e3BlockedList = 
3314b 30 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  0;..#ifndef NDEB
3314c 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  UG./*.** This fu
3314d 6e 63 74 69 6f 6e 20 69 73 20 61 20 63 6f 6d 70  nction is a comp
3314e 6c 65 78 20 61 73 73 65 72 74 28 29 20 74 68 61  lex assert() tha
3314f 74 20 76 65 72 69 66 69 65 73 20 74 68 65 20 66  t verifies the f
33150 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 70 72 6f  ollowing .** pro
33151 70 65 72 74 69 65 73 20 6f 66 20 74 68 65 20 62  perties of the b
33152 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f  locked connectio
33153 6e 73 20 6c 69 73 74 3a 0a 2a 2a 0a 2a 2a 20 20  ns list:.**.**  
33154 20 31 29 20 45 61 63 68 20 65 6e 74 72 79 20 69   1) Each entry i
33155 6e 20 74 68 65 20 6c 69 73 74 20 68 61 73 20 61  n the list has a
33156 20 6e 6f 6e 2d 4e 55 4c 4c 20 76 61 6c 75 65 20   non-NULL value 
33157 66 6f 72 20 65 69 74 68 65 72 20 0a 2a 2a 20 20  for either .**  
33158 20 20 20 20 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65      pUnlockConne
33159 63 74 69 6f 6e 20 6f 72 20 70 42 6c 6f 63 6b 69  ction or pBlocki
3315a 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 2c 20 6f 72  ngConnection, or
3315b 20 62 6f 74 68 2e 0a 2a 2a 0a 2a 2a 20 20 20 32   both..**.**   2
3315c 29 20 41 6c 6c 20 65 6e 74 72 69 65 73 20 69 6e  ) All entries in
3315d 20 74 68 65 20 6c 69 73 74 20 74 68 61 74 20 73   the list that s
3315e 68 61 72 65 20 61 20 63 6f 6d 6d 6f 6e 20 76 61  hare a common va
3315f 6c 75 65 20 66 6f 72 20 0a 2a 2a 20 20 20 20 20  lue for .**     
33160 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 61   xUnlockNotify a
33161 72 65 20 67 72 6f 75 70 65 64 20 74 6f 67 65 74  re grouped toget
33162 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 29 20  her..**.**   3) 
33163 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  If the argument 
33164 64 62 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  db is not NULL, 
33165 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65  then none of the
33166 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 0a   entries in the.
33167 2a 2a 20 20 20 20 20 20 62 6c 6f 63 6b 65 64 20  **      blocked 
33168 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74  connections list
33169 20 68 61 76 65 20 70 55 6e 6c 6f 63 6b 43 6f 6e   have pUnlockCon
3316a 6e 65 63 74 69 6f 6e 20 6f 72 20 70 42 6c 6f 63  nection or pBloc
3316b 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 0a 2a  kingConnection.*
3316c 2a 20 20 20 20 20 20 73 65 74 20 74 6f 20 64 62  *      set to db
3316d 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 77  . This is used w
3316e 68 65 6e 20 63 6c 6f 73 69 6e 67 20 63 6f 6e 6e  hen closing conn
3316f 65 63 74 69 6f 6e 20 64 62 2e 0a 2a 2f 0a 73 74  ection db..*/.st
33170 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 4c  atic void checkL
33171 69 73 74 50 72 6f 70 65 72 74 69 65 73 28 73 71  istProperties(sq
33172 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 71  lite3 *db){.  sq
33173 6c 69 74 65 33 20 2a 70 3b 0a 20 20 66 6f 72 28  lite3 *p;.  for(
33174 70 3d 73 71 6c 69 74 65 33 42 6c 6f 63 6b 65 64  p=sqlite3Blocked
33175 4c 69 73 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e  List; p; p=p->pN
33176 65 78 74 42 6c 6f 63 6b 65 64 29 7b 0a 20 20 20  extBlocked){.   
33177 20 69 6e 74 20 73 65 65 6e 20 3d 20 30 3b 0a 20   int seen = 0;. 
33178 20 20 20 73 71 6c 69 74 65 33 20 2a 70 32 3b 0a     sqlite3 *p2;.
33179 0a 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 70  .    /* Verify p
3317a 72 6f 70 65 72 74 79 20 28 31 29 20 2a 2f 0a 20  roperty (1) */. 
3317b 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 55     assert( p->pU
3317c 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 20  nlockConnection 
3317d 7c 7c 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43  || p->pBlockingC
3317e 6f 6e 6e 65 63 74 69 6f 6e 20 29 3b 0a 0a 20 20  onnection );..  
3317f 20 20 2f 2a 20 56 65 72 69 66 79 20 70 72 6f 70    /* Verify prop
33180 65 72 74 79 20 28 32 29 20 2a 2f 0a 20 20 20 20  erty (2) */.    
33181 66 6f 72 28 70 32 3d 73 71 6c 69 74 65 33 42 6c  for(p2=sqlite3Bl
33182 6f 63 6b 65 64 4c 69 73 74 3b 20 70 32 21 3d 70  ockedList; p2!=p
33183 3b 20 70 32 3d 70 32 2d 3e 70 4e 65 78 74 42 6c  ; p2=p2->pNextBl
33184 6f 63 6b 65 64 29 7b 0a 20 20 20 20 20 20 69 66  ocked){.      if
33185 28 20 70 32 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74  ( p2->xUnlockNot
33186 69 66 79 3d 3d 70 2d 3e 78 55 6e 6c 6f 63 6b 4e  ify==p->xUnlockN
33187 6f 74 69 66 79 20 29 20 73 65 65 6e 20 3d 20 31  otify ) seen = 1
33188 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
33189 70 32 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66  p2->xUnlockNotif
3318a 79 3d 3d 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74  y==p->xUnlockNot
3318b 69 66 79 20 7c 7c 20 21 73 65 65 6e 20 29 3b 0a  ify || !seen );.
3318c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 62        assert( db
3318d 3d 3d 30 20 7c 7c 20 70 2d 3e 70 55 6e 6c 6f 63  ==0 || p->pUnloc
3318e 6b 43 6f 6e 6e 65 63 74 69 6f 6e 21 3d 64 62 20  kConnection!=db 
3318f 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
33190 20 64 62 3d 3d 30 20 7c 7c 20 70 2d 3e 70 42 6c   db==0 || p->pBl
33191 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
33192 21 3d 64 62 20 29 3b 0a 20 20 20 20 7d 0a 20 20  !=db );.    }.  
33193 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  }.}.#else.# defi
33194 6e 65 20 63 68 65 63 6b 4c 69 73 74 50 72 6f 70  ne checkListProp
33195 65 72 74 69 65 73 28 78 29 0a 23 65 6e 64 69 66  erties(x).#endif
33196 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 63  ../*.** Remove c
33197 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 66 72 6f  onnection db fro
33198 6d 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f  m the blocked co
33199 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74 2e 20  nnections list. 
3319a 49 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a  If connection.**
3319b 20 64 62 20 69 73 20 6e 6f 74 20 63 75 72 72 65   db is not curre
3319c 6e 74 6c 79 20 61 20 70 61 72 74 20 6f 66 20 74  ntly a part of t
3319d 68 65 20 6c 69 73 74 2c 20 74 68 69 73 20 66 75  he list, this fu
3319e 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f  nction is a no-o
3319f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  p..*/.static voi
331a0 64 20 72 65 6d 6f 76 65 46 72 6f 6d 42 6c 6f 63  d removeFromBloc
331a1 6b 65 64 4c 69 73 74 28 73 71 6c 69 74 65 33 20  kedList(sqlite3 
331a2 2a 64 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  *db){.  sqlite3 
331a3 2a 2a 70 70 3b 0a 20 20 61 73 73 65 72 74 4d 75  **pp;.  assertMu
331a4 74 65 78 48 65 6c 64 28 29 3b 0a 20 20 66 6f 72  texHeld();.  for
331a5 28 70 70 3d 26 73 71 6c 69 74 65 33 42 6c 6f 63  (pp=&sqlite3Bloc
331a6 6b 65 64 4c 69 73 74 3b 20 2a 70 70 3b 20 70 70  kedList; *pp; pp
331a7 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74   = &(*pp)->pNext
331a8 42 6c 6f 63 6b 65 64 29 7b 0a 20 20 20 20 69 66  Blocked){.    if
331a9 28 20 2a 70 70 3d 3d 64 62 20 29 7b 0a 20 20 20  ( *pp==db ){.   
331aa 20 20 20 2a 70 70 20 3d 20 28 2a 70 70 29 2d 3e     *pp = (*pp)->
331ab 70 4e 65 78 74 42 6c 6f 63 6b 65 64 3b 0a 20 20  pNextBlocked;.  
331ac 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
331ad 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  .  }.}../*.** Ad
331ae 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20  d connection db 
331af 74 6f 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63  to the blocked c
331b0 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74 2e  onnections list.
331b1 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 0a 2a   It is assumed.*
331b2 2a 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74  * that it is not
331b3 20 61 6c 72 65 61 64 79 20 61 20 70 61 72 74 20   already a part 
331b4 6f 66 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a  of the list..*/.
331b5 73 74 61 74 69 63 20 76 6f 69 64 20 61 64 64 54  static void addT
331b6 6f 42 6c 6f 63 6b 65 64 4c 69 73 74 28 73 71 6c  oBlockedList(sql
331b7 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c  ite3 *db){.  sql
331b8 69 74 65 33 20 2a 2a 70 70 3b 0a 20 20 61 73 73  ite3 **pp;.  ass
331b9 65 72 74 4d 75 74 65 78 48 65 6c 64 28 29 3b 0a  ertMutexHeld();.
331ba 20 20 66 6f 72 28 0a 20 20 20 20 70 70 3d 26 73    for(.    pp=&s
331bb 71 6c 69 74 65 33 42 6c 6f 63 6b 65 64 4c 69 73  qlite3BlockedLis
331bc 74 3b 20 0a 20 20 20 20 2a 70 70 20 26 26 20 28  t; .    *pp && (
331bd 2a 70 70 29 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74  *pp)->xUnlockNot
331be 69 66 79 21 3d 64 62 2d 3e 78 55 6e 6c 6f 63 6b  ify!=db->xUnlock
331bf 4e 6f 74 69 66 79 3b 20 0a 20 20 20 20 70 70 3d  Notify; .    pp=
331c0 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 42 6c 6f  &(*pp)->pNextBlo
331c1 63 6b 65 64 0a 20 20 29 3b 0a 20 20 64 62 2d 3e  cked.  );.  db->
331c2 70 4e 65 78 74 42 6c 6f 63 6b 65 64 20 3d 20 2a  pNextBlocked = *
331c3 70 70 3b 0a 20 20 2a 70 70 20 3d 20 64 62 3b 0a  pp;.  *pp = db;.
331c4 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20  }../*.** Obtain 
331c5 74 68 65 20 53 54 41 54 49 43 5f 4d 41 53 54 45  the STATIC_MASTE
331c6 52 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 74  R mutex..*/.stat
331c7 69 63 20 76 6f 69 64 20 65 6e 74 65 72 4d 75 74  ic void enterMut
331c8 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69  ex(void){.  sqli
331c9 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
331ca 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
331cb 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
331cc 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a  TATIC_MASTER));.
331cd 20 20 63 68 65 63 6b 4c 69 73 74 50 72 6f 70 65    checkListPrope
331ce 72 74 69 65 73 28 30 29 3b 0a 7d 0a 0a 2f 2a 0a  rties(0);.}../*.
331cf 2a 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 53  ** Release the S
331d0 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74  TATIC_MASTER mut
331d1 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ex..*/.static vo
331d2 69 64 20 6c 65 61 76 65 4d 75 74 65 78 28 76 6f  id leaveMutex(vo
331d3 69 64 29 7b 0a 20 20 61 73 73 65 72 74 4d 75 74  id){.  assertMut
331d4 65 78 48 65 6c 64 28 29 3b 0a 20 20 63 68 65 63  exHeld();.  chec
331d5 6b 4c 69 73 74 50 72 6f 70 65 72 74 69 65 73 28  kListProperties(
331d6 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  0);.  sqlite3_mu
331d7 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65  tex_leave(sqlite
331d8 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
331d9 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
331da 4d 41 53 54 45 52 29 29 3b 0a 7d 0a 0a 2f 2a 0a  MASTER));.}../*.
331db 2a 2a 20 52 65 67 69 73 74 65 72 20 61 6e 20 75  ** Register an u
331dc 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c  nlock-notify cal
331dd 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  lback..**.** Thi
331de 73 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65  s is called afte
331df 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 22 64 62  r connection "db
331e0 22 20 68 61 73 20 61 74 74 65 6d 70 74 65 64 20  " has attempted 
331e1 73 6f 6d 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a  some operation.*
331e2 2a 20 62 75 74 20 68 61 73 20 72 65 63 65 69 76  * but has receiv
331e3 65 64 20 61 6e 20 53 51 4c 49 54 45 5f 4c 4f 43  ed an SQLITE_LOC
331e4 4b 45 44 20 65 72 72 6f 72 20 62 65 63 61 75 73  KED error becaus
331e5 65 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 63  e another connec
331e6 74 69 6f 6e 0a 2a 2a 20 28 63 61 6c 6c 20 69 74  tion.** (call it
331e7 20 70 4f 74 68 65 72 29 20 69 6e 20 74 68 65 20   pOther) in the 
331e8 73 61 6d 65 20 70 72 6f 63 65 73 73 20 77 61 73  same process was
331e9 20 62 75 73 79 20 75 73 69 6e 67 20 74 68 65 20   busy using the 
331ea 73 61 6d 65 20 73 68 61 72 65 64 0a 2a 2a 20 63  same shared.** c
331eb 61 63 68 65 2e 20 20 70 4f 74 68 65 72 20 69 73  ache.  pOther is
331ec 20 66 6f 75 6e 64 20 62 79 20 6c 6f 6f 6b 69 6e   found by lookin
331ed 67 20 61 74 20 64 62 2d 3e 70 42 6c 6f 63 6b 69  g at db->pBlocki
331ee 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a  ngConnection..**
331ef 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20  .** If there is 
331f0 6e 6f 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e  no blocking conn
331f1 65 63 74 69 6f 6e 2c 20 74 68 65 20 63 61 6c 6c  ection, the call
331f2 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20  back is invoked 
331f3 69 6d 6d 65 64 69 61 74 65 6c 79 2c 0a 2a 2a 20  immediately,.** 
331f4 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75 74  before this rout
331f5 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a  ine returns..**.
331f6 2a 2a 20 49 66 20 70 4f 74 68 65 72 20 69 73 20  ** If pOther is 
331f7 61 6c 72 65 61 64 79 20 62 6c 6f 63 6b 65 64 20  already blocked 
331f8 6f 6e 20 64 62 2c 20 74 68 65 6e 20 72 65 70 6f  on db, then repo
331f9 72 74 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  rt SQLITE_LOCKED
331fa 2c 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a  , to indicate.**
331fb 20 61 20 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a   a deadlock..**.
331fc 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 6d 61  ** Otherwise, ma
331fd 6b 65 20 61 72 72 61 6e 67 65 6d 65 6e 74 73 20  ke arrangements 
331fe 74 6f 20 69 6e 76 6f 6b 65 20 78 4e 6f 74 69 66  to invoke xNotif
331ff 79 20 77 68 65 6e 20 70 4f 74 68 65 72 20 64 72  y when pOther dr
33200 6f 70 73 0a 2a 2a 20 69 74 73 20 6c 6f 63 6b 73  ops.** its locks
33201 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 63 61 6c  ..**.** Each cal
33202 6c 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  l to this routin
33203 65 20 6f 76 65 72 72 69 64 65 73 20 61 6e 79 20  e overrides any 
33204 70 72 69 6f 72 20 63 61 6c 6c 62 61 63 6b 73 20  prior callbacks 
33205 72 65 67 69 73 74 65 72 65 64 0a 2a 2a 20 6f 6e  registered.** on
33206 20 74 68 65 20 73 61 6d 65 20 22 64 62 22 2e 20   the same "db". 
33207 20 49 66 20 78 4e 6f 74 69 66 79 3d 3d 30 20 74   If xNotify==0 t
33208 68 65 6e 20 61 6e 79 20 70 72 69 6f 72 20 63 61  hen any prior ca
33209 6c 6c 62 61 63 6b 73 20 61 72 65 20 69 6d 6d 65  llbacks are imme
3320a 64 69 61 74 65 6c 79 0a 2a 2a 20 63 61 6e 63 65  diately.** cance
3320b 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lled..*/.SQLITE_
3320c 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
3320d 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 0a 20  unlock_notify(. 
3320e 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20   sqlite3 *db,.  
3320f 76 6f 69 64 20 28 2a 78 4e 6f 74 69 66 79 29 28  void (*xNotify)(
33210 76 6f 69 64 20 2a 2a 2c 20 69 6e 74 29 2c 0a 20  void **, int),. 
33211 20 76 6f 69 64 20 2a 70 41 72 67 0a 29 7b 0a 20   void *pArg.){. 
33212 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
33213 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f  _OK;..  sqlite3_
33214 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
33215 6d 75 74 65 78 29 3b 0a 20 20 65 6e 74 65 72 4d  mutex);.  enterM
33216 75 74 65 78 28 29 3b 0a 0a 20 20 69 66 28 20 78  utex();..  if( x
33217 4e 6f 74 69 66 79 3d 3d 30 20 29 7b 0a 20 20 20  Notify==0 ){.   
33218 20 72 65 6d 6f 76 65 46 72 6f 6d 42 6c 6f 63 6b   removeFromBlock
33219 65 64 4c 69 73 74 28 64 62 29 3b 0a 20 20 20 20  edList(db);.    
3321a 64 62 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65  db->pUnlockConne
3321b 63 74 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 64  ction = 0;.    d
3321c 62 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79  b->xUnlockNotify
3321d 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 70 55   = 0;.    db->pU
3321e 6e 6c 6f 63 6b 41 72 67 20 3d 20 30 3b 0a 20 20  nlockArg = 0;.  
3321f 7d 65 6c 73 65 20 69 66 28 20 30 3d 3d 64 62 2d  }else if( 0==db-
33220 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63  >pBlockingConnec
33221 74 69 6f 6e 20 29 7b 0a 20 20 20 20 2f 2a 20 54  tion ){.    /* T
33222 68 65 20 62 6c 6f 63 6b 69 6e 67 20 74 72 61 6e  he blocking tran
33223 73 61 63 74 69 6f 6e 20 68 61 73 20 62 65 65 6e  saction has been
33224 20 63 6f 6e 63 6c 75 64 65 64 2e 20 4f 72 20 74   concluded. Or t
33225 68 65 72 65 20 6e 65 76 65 72 20 77 61 73 20 61  here never was a
33226 20 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 69 6e   .    ** blockin
33227 67 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49  g transaction. I
33228 6e 20 65 69 74 68 65 72 20 63 61 73 65 2c 20 69  n either case, i
33229 6e 76 6f 6b 65 20 74 68 65 20 6e 6f 74 69 66 79  nvoke the notify
3322a 20 63 61 6c 6c 62 61 63 6b 0a 20 20 20 20 2a 2a   callback.    **
3322b 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 0a 20   immediately. . 
3322c 20 20 20 2a 2f 0a 20 20 20 20 78 4e 6f 74 69 66     */.    xNotif
3322d 79 28 26 70 41 72 67 2c 20 31 29 3b 0a 20 20 7d  y(&pArg, 1);.  }
3322e 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
3322f 33 20 2a 70 3b 0a 0a 20 20 20 20 66 6f 72 28 70  3 *p;..    for(p
33230 3d 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f  =db->pBlockingCo
33231 6e 6e 65 63 74 69 6f 6e 3b 20 70 20 26 26 20 70  nnection; p && p
33232 21 3d 64 62 3b 20 70 3d 70 2d 3e 70 55 6e 6c 6f  !=db; p=p->pUnlo
33233 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 29 7b 7d 0a  ckConnection){}.
33234 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20      if( p ){.   
33235 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4c     rc = SQLITE_L
33236 4f 43 4b 45 44 3b 20 20 20 20 20 20 20 20 20 20  OCKED;          
33237 20 20 20 20 2f 2a 20 44 65 61 64 6c 6f 63 6b 20      /* Deadlock 
33238 64 65 74 65 63 74 65 64 2e 20 2a 2f 0a 20 20 20  detected. */.   
33239 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 62   }else{.      db
3323a 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74  ->pUnlockConnect
3323b 69 6f 6e 20 3d 20 64 62 2d 3e 70 42 6c 6f 63 6b  ion = db->pBlock
3323c 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3b 0a 20  ingConnection;. 
3323d 20 20 20 20 20 64 62 2d 3e 78 55 6e 6c 6f 63 6b       db->xUnlock
3323e 4e 6f 74 69 66 79 20 3d 20 78 4e 6f 74 69 66 79  Notify = xNotify
3323f 3b 0a 20 20 20 20 20 20 64 62 2d 3e 70 55 6e 6c  ;.      db->pUnl
33240 6f 63 6b 41 72 67 20 3d 20 70 41 72 67 3b 0a 20  ockArg = pArg;. 
33241 20 20 20 20 20 72 65 6d 6f 76 65 46 72 6f 6d 42       removeFromB
33242 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62 29 3b 0a  lockedList(db);.
33243 20 20 20 20 20 20 61 64 64 54 6f 42 6c 6f 63 6b        addToBlock
33244 65 64 4c 69 73 74 28 64 62 29 3b 0a 20 20 20 20  edList(db);.    
33245 7d 0a 20 20 7d 0a 0a 20 20 6c 65 61 76 65 4d 75  }.  }..  leaveMu
33246 74 65 78 28 29 3b 0a 20 20 61 73 73 65 72 74 28  tex();.  assert(
33247 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
33248 65 64 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ed );.  sqlite3E
33249 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 28 72 63  rror(db, rc, (rc
3324a 3f 22 64 61 74 61 62 61 73 65 20 69 73 20 64 65  ?"database is de
3324b 61 64 6c 6f 63 6b 65 64 22 3a 30 29 29 3b 0a 20  adlocked":0));. 
3324c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
3324d 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
3324e 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
3324f 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
33250 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77  tion is called w
33251 68 69 6c 65 20 73 74 65 70 70 69 6e 67 20 6f 72  hile stepping or
33252 20 70 72 65 70 61 72 69 6e 67 20 61 20 73 74 61   preparing a sta
33253 74 65 6d 65 6e 74 20 0a 2a 2a 20 61 73 73 6f 63  tement .** assoc
33254 69 61 74 65 64 20 77 69 74 68 20 63 6f 6e 6e 65  iated with conne
33255 63 74 69 6f 6e 20 64 62 2e 20 54 68 65 20 6f 70  ction db. The op
33256 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20 72 65 74  eration will ret
33257 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  urn SQLITE_LOCKE
33258 44 0a 2a 2a 20 74 6f 20 74 68 65 20 75 73 65 72  D.** to the user
33259 20 62 65 63 61 75 73 65 20 69 74 20 72 65 71 75   because it requ
3325a 69 72 65 73 20 61 20 6c 6f 63 6b 20 74 68 61 74  ires a lock that
3325b 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 76 61   will not be ava
3325c 69 6c 61 62 6c 65 0a 2a 2a 20 75 6e 74 69 6c 20  ilable.** until 
3325d 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 42 6c 6f 63  connection pBloc
3325e 6b 65 72 20 63 6f 6e 63 6c 75 64 65 73 20 69 74  ker concludes it
3325f 73 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61  s current transa
33260 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
33261 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
33262 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42  lite3ConnectionB
33263 6c 6f 63 6b 65 64 28 73 71 6c 69 74 65 33 20 2a  locked(sqlite3 *
33264 64 62 2c 20 73 71 6c 69 74 65 33 20 2a 70 42 6c  db, sqlite3 *pBl
33265 6f 63 6b 65 72 29 7b 0a 20 20 65 6e 74 65 72 4d  ocker){.  enterM
33266 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 64 62  utex();.  if( db
33267 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65  ->pBlockingConne
33268 63 74 69 6f 6e 3d 3d 30 20 26 26 20 64 62 2d 3e  ction==0 && db->
33269 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f  pUnlockConnectio
3326a 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 61 64 64 54  n==0 ){.    addT
3326b 6f 42 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62 29  oBlockedList(db)
3326c 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 70 42 6c 6f  ;.  }.  db->pBlo
3326d 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 20  ckingConnection 
3326e 3d 20 70 42 6c 6f 63 6b 65 72 3b 0a 20 20 6c 65  = pBlocker;.  le
3326f 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f  aveMutex();.}../
33270 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
33271 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65  on is called whe
33272 6e 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63  n.** the transac
33273 74 69 6f 6e 20 6f 70 65 6e 65 64 20 62 79 20 64  tion opened by d
33274 61 74 61 62 61 73 65 20 64 62 20 68 61 73 20 6a  atabase db has j
33275 75 73 74 20 66 69 6e 69 73 68 65 64 2e 20 4c 6f  ust finished. Lo
33276 63 6b 73 20 68 65 6c 64 20 0a 2a 2a 20 62 79 20  cks held .** by 
33277 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
33278 69 6f 6e 20 64 62 20 68 61 76 65 20 62 65 65 6e  ion db have been
33279 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a 2a   released..**.**
3327a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6c   This function l
3327b 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 65 61 63  oops through eac
3327c 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 62  h entry in the b
3327d 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f  locked connectio
3327e 6e 73 0a 2a 2a 20 6c 69 73 74 20 61 6e 64 20 64  ns.** list and d
3327f 6f 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  oes the followin
33280 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 49 66  g:.**.**   1) If
33281 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70 42 6c   the sqlite3.pBl
33282 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
33283 20 6d 65 6d 62 65 72 20 6f 66 20 61 20 6c 69 73   member of a lis
33284 74 20 65 6e 74 72 79 20 69 73 0a 2a 2a 20 20 20  t entry is.**   
33285 20 20 20 73 65 74 20 74 6f 20 64 62 2c 20 74 68     set to db, th
33286 65 6e 20 73 65 74 20 70 42 6c 6f 63 6b 69 6e 67  en set pBlocking
33287 43 6f 6e 6e 65 63 74 69 6f 6e 3d 30 2e 0a 2a 2a  Connection=0..**
33288 0a 2a 2a 20 20 20 32 29 20 49 66 20 74 68 65 20  .**   2) If the 
33289 73 71 6c 69 74 65 33 2e 70 55 6e 6c 6f 63 6b 43  sqlite3.pUnlockC
3328a 6f 6e 6e 65 63 74 69 6f 6e 20 6d 65 6d 62 65 72  onnection member
3328b 20 6f 66 20 61 20 6c 69 73 74 20 65 6e 74 72 79   of a list entry
3328c 20 69 73 0a 2a 2a 20 20 20 20 20 20 73 65 74 20   is.**      set 
3328d 74 6f 20 64 62 2c 20 74 68 65 6e 20 69 6e 76 6f  to db, then invo
3328e 6b 65 20 74 68 65 20 63 6f 6e 66 69 67 75 72 65  ke the configure
3328f 64 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20  d unlock-notify 
33290 63 61 6c 6c 62 61 63 6b 20 61 6e 64 0a 2a 2a 20  callback and.** 
33291 20 20 20 20 20 73 65 74 20 70 55 6e 6c 6f 63 6b       set pUnlock
33292 43 6f 6e 6e 65 63 74 69 6f 6e 3d 30 2e 0a 2a 2a  Connection=0..**
33293 0a 2a 2a 20 20 20 33 29 20 49 66 20 74 68 65 20  .**   3) If the 
33294 74 77 6f 20 73 74 65 70 73 20 61 62 6f 76 65 20  two steps above 
33295 6d 65 61 6e 20 74 68 61 74 20 70 42 6c 6f 63 6b  mean that pBlock
33296 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30  ingConnection==0
33297 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 70 55 6e   and.**      pUn
33298 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d  lockConnection==
33299 30 2c 20 72 65 6d 6f 76 65 20 74 68 65 20 65 6e  0, remove the en
3329a 74 72 79 20 66 72 6f 6d 20 74 68 65 20 62 6c 6f  try from the blo
3329b 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  cked connections
3329c 0a 2a 2a 20 20 20 20 20 20 6c 69 73 74 2e 0a 2a  .**      list..*
3329d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
3329e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e   void sqlite3Con
3329f 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28  nectionUnlocked(
332a0 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
332a1 76 6f 69 64 20 28 2a 78 55 6e 6c 6f 63 6b 4e 6f  void (*xUnlockNo
332a2 74 69 66 79 29 28 76 6f 69 64 20 2a 2a 2c 20 69  tify)(void **, i
332a3 6e 74 29 20 3d 20 30 3b 20 2f 2a 20 55 6e 6c 6f  nt) = 0; /* Unlo
332a4 63 6b 2d 6e 6f 74 69 66 79 20 63 62 20 74 6f 20  ck-notify cb to 
332a5 69 6e 76 6f 6b 65 20 2a 2f 0a 20 20 69 6e 74 20  invoke */.  int 
332a6 6e 41 72 67 20 3d 20 30 3b 20 20 20 20 20 20 20  nArg = 0;       
332a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332a8 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
332a9 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 41 72  f entries in aAr
332aa 67 5b 5d 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  g[] */.  sqlite3
332ab 20 2a 2a 70 70 3b 20 20 20 20 20 20 20 20 20 20   **pp;          
332ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332ad 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61    /* Iterator va
332ae 72 69 61 62 6c 65 20 2a 2f 0a 20 20 76 6f 69 64  riable */.  void
332af 20 2a 2a 61 41 72 67 3b 20 20 20 20 20 20 20 20   **aArg;        
332b0 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65         /* Argume
332b1 6e 74 73 20 74 6f 20 74 68 65 20 75 6e 6c 6f 63  nts to the unloc
332b2 6b 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20  k callback */.  
332b3 76 6f 69 64 20 2a 2a 61 44 79 6e 20 3d 20 30 3b  void **aDyn = 0;
332b4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 79             /* Dy
332b5 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
332b6 74 65 64 20 73 70 61 63 65 20 66 6f 72 20 61 41  ted space for aA
332b7 72 67 5b 5d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  rg[] */.  void *
332b8 61 53 74 61 74 69 63 5b 31 36 5d 3b 20 20 20 20  aStatic[16];    
332b9 20 20 20 20 20 2f 2a 20 53 74 61 72 74 65 72 20       /* Starter 
332ba 73 70 61 63 65 20 66 6f 72 20 61 41 72 67 5b 5d  space for aArg[]
332bb 2e 20 20 4e 6f 20 6d 61 6c 6c 6f 63 20 72 65 71  .  No malloc req
332bc 75 69 72 65 64 20 2a 2f 0a 0a 20 20 61 41 72 67  uired */..  aArg
332bd 20 3d 20 61 53 74 61 74 69 63 3b 0a 20 20 65 6e   = aStatic;.  en
332be 74 65 72 4d 75 74 65 78 28 29 3b 20 20 20 20 20  terMutex();     
332bf 20 20 20 20 2f 2a 20 45 6e 74 65 72 20 53 54 41      /* Enter STA
332c0 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74 65 78  TIC_MASTER mutex
332c1 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c   */..  /* This l
332c2 6f 6f 70 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f  oop runs once fo
332c3 72 20 65 61 63 68 20 65 6e 74 72 79 20 69 6e 20  r each entry in 
332c4 74 68 65 20 62 6c 6f 63 6b 65 64 2d 63 6f 6e 6e  the blocked-conn
332c5 65 63 74 69 6f 6e 73 20 6c 69 73 74 2e 20 2a 2f  ections list. */
332c6 0a 20 20 66 6f 72 28 70 70 3d 26 73 71 6c 69 74  .  for(pp=&sqlit
332c7 65 33 42 6c 6f 63 6b 65 64 4c 69 73 74 3b 20 2a  e3BlockedList; *
332c8 70 70 3b 20 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 20  pp; /* no-op */ 
332c9 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a  ){.    sqlite3 *
332ca 70 20 3d 20 2a 70 70 3b 0a 0a 20 20 20 20 2f 2a  p = *pp;..    /*
332cb 20 53 74 65 70 20 31 2e 20 2a 2f 0a 20 20 20 20   Step 1. */.    
332cc 69 66 28 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e 67  if( p->pBlocking
332cd 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 64 62 20 29  Connection==db )
332ce 7b 0a 20 20 20 20 20 20 70 2d 3e 70 42 6c 6f 63  {.      p->pBloc
332cf 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d  kingConnection =
332d0 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f   0;.    }..    /
332d1 2a 20 53 74 65 70 20 32 2e 20 2a 2f 0a 20 20 20  * Step 2. */.   
332d2 20 69 66 28 20 70 2d 3e 70 55 6e 6c 6f 63 6b 43   if( p->pUnlockC
332d3 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 64 62 20 29 7b  onnection==db ){
332d4 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
332d5 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20  ->xUnlockNotify 
332d6 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  );.      if( p->
332d7 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 21 3d 78  xUnlockNotify!=x
332d8 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 26 26 20  UnlockNotify && 
332d9 6e 41 72 67 21 3d 30 20 29 7b 0a 20 20 20 20 20  nArg!=0 ){.     
332da 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79     xUnlockNotify
332db 28 61 41 72 67 2c 20 6e 41 72 67 29 3b 0a 20 20  (aArg, nArg);.  
332dc 20 20 20 20 20 20 6e 41 72 67 20 3d 20 30 3b 0a        nArg = 0;.
332dd 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 73        }..      s
332de 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
332df 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20  nMalloc();.     
332e0 20 61 73 73 65 72 74 28 20 61 41 72 67 3d 3d 61   assert( aArg==a
332e1 44 79 6e 20 7c 7c 20 28 61 44 79 6e 3d 3d 30 20  Dyn || (aDyn==0 
332e2 26 26 20 61 41 72 67 3d 3d 61 53 74 61 74 69 63  && aArg==aStatic
332e3 29 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  ) );.      asser
332e4 74 28 20 6e 41 72 67 3c 3d 28 69 6e 74 29 41 72  t( nArg<=(int)Ar
332e5 72 61 79 53 69 7a 65 28 61 53 74 61 74 69 63 29  raySize(aStatic)
332e6 20 7c 7c 20 61 41 72 67 3d 3d 61 44 79 6e 20 29   || aArg==aDyn )
332e7 3b 0a 20 20 20 20 20 20 69 66 28 20 28 21 61 44  ;.      if( (!aD
332e8 79 6e 20 26 26 20 6e 41 72 67 3d 3d 28 69 6e 74  yn && nArg==(int
332e9 29 41 72 72 61 79 53 69 7a 65 28 61 53 74 61 74  )ArraySize(aStat
332ea 69 63 29 29 0a 20 20 20 20 20 20 20 7c 7c 20 28  ic)).       || (
332eb 61 44 79 6e 20 26 26 20 6e 41 72 67 3d 3d 28 69  aDyn && nArg==(i
332ec 6e 74 29 28 73 71 6c 69 74 65 33 44 62 4d 61 6c  nt)(sqlite3DbMal
332ed 6c 6f 63 53 69 7a 65 28 64 62 2c 20 61 44 79 6e  locSize(db, aDyn
332ee 29 2f 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 29  )/sizeof(void*))
332ef 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20  ).      ){.     
332f0 20 20 20 2f 2a 20 54 68 65 20 61 41 72 67 5b 5d     /* The aArg[]
332f1 20 61 72 72 61 79 20 6e 65 65 64 73 20 74 6f 20   array needs to 
332f2 67 72 6f 77 2e 20 2a 2f 0a 20 20 20 20 20 20 20  grow. */.       
332f3 20 76 6f 69 64 20 2a 2a 70 4e 65 77 20 3d 20 28   void **pNew = (
332f4 76 6f 69 64 20 2a 2a 29 73 71 6c 69 74 65 33 4d  void **)sqlite3M
332f5 61 6c 6c 6f 63 28 6e 41 72 67 2a 73 69 7a 65 6f  alloc(nArg*sizeo
332f6 66 28 76 6f 69 64 20 2a 29 2a 32 29 3b 0a 20 20  f(void *)*2);.  
332f7 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29        if( pNew )
332f8 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63  {.          memc
332f9 70 79 28 70 4e 65 77 2c 20 61 41 72 67 2c 20 6e  py(pNew, aArg, n
332fa 41 72 67 2a 73 69 7a 65 6f 66 28 76 6f 69 64 20  Arg*sizeof(void 
332fb 2a 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  *));.          s
332fc 71 6c 69 74 65 33 5f 66 72 65 65 28 61 44 79 6e  qlite3_free(aDyn
332fd 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 44 79  );.          aDy
332fe 6e 20 3d 20 61 41 72 67 20 3d 20 70 4e 65 77 3b  n = aArg = pNew;
332ff 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
33300 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69            /* Thi
33301 73 20 6f 63 63 75 72 73 20 77 68 65 6e 20 74 68  s occurs when th
33302 65 20 61 72 72 61 79 20 6f 66 20 63 6f 6e 74 65  e array of conte
33303 78 74 20 70 6f 69 6e 74 65 72 73 20 74 68 61 74  xt pointers that
33304 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20 20   need to.       
33305 20 20 20 2a 2a 20 62 65 20 70 61 73 73 65 64 20     ** be passed 
33306 74 6f 20 74 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f  to the unlock-no
33307 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73  tify callback is
33308 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65   larger than the
33309 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 53  .          ** aS
3330a 74 61 74 69 63 5b 5d 20 61 72 72 61 79 20 61 6c  tatic[] array al
3330b 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65 20 73  located on the s
3330c 74 61 63 6b 20 61 6e 64 20 74 68 65 20 61 74 74  tack and the att
3330d 65 6d 70 74 20 74 6f 20 0a 20 20 20 20 20 20 20  empt to .       
3330e 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 61     ** allocate a
3330f 20 6c 61 72 67 65 72 20 61 72 72 61 79 20 66 72   larger array fr
33310 6f 6d 20 74 68 65 20 68 65 61 70 20 68 61 73 20  om the heap has 
33311 66 61 69 6c 65 64 2e 0a 20 20 20 20 20 20 20 20  failed..        
33312 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 2a    **.          *
33313 2a 20 54 68 69 73 20 69 73 20 61 20 64 69 66 66  * This is a diff
33314 69 63 75 6c 74 20 73 69 74 75 61 74 69 6f 6e 20  icult situation 
33315 74 6f 20 68 61 6e 64 6c 65 2e 20 52 65 74 75 72  to handle. Retur
33316 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 0a 20 20  ning an error.  
33317 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 64 65 20          ** code 
33318 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73  to the caller is
33319 20 69 6e 73 75 66 66 69 63 69 65 6e 74 2c 20 61   insufficient, a
3331a 73 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72  s even if an err
3331b 6f 72 20 63 6f 64 65 0a 20 20 20 20 20 20 20 20  or code.        
3331c 20 20 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64    ** is returned
3331d 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
3331e 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64   on connection d
3331f 62 20 77 69 6c 6c 20 73 74 69 6c 6c 20 62 65 0a  b will still be.
33320 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6c 6f            ** clo
33321 73 65 64 20 61 6e 64 20 74 68 65 20 75 6e 6c 6f  sed and the unlo
33322 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61  ck-notify callba
33323 63 6b 73 20 6f 6e 20 62 6c 6f 63 6b 65 64 20 63  cks on blocked c
33324 6f 6e 6e 65 63 74 69 6f 6e 73 0a 20 20 20 20 20  onnections.     
33325 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 67 6f 20       ** will go 
33326 75 6e 69 73 73 75 65 64 2e 20 54 68 69 73 20 6d  unissued. This m
33327 69 67 68 74 20 63 61 75 73 65 20 74 68 65 20 61  ight cause the a
33328 70 70 6c 69 63 61 74 69 6f 6e 20 74 6f 20 77 61  pplication to wa
33329 69 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  it.          ** 
3332a 69 6e 64 65 66 69 6e 69 74 65 6c 79 20 66 6f 72  indefinitely for
3332b 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66   an unlock-notif
3332c 79 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 20  y callback that 
3332d 77 69 6c 6c 20 6e 65 76 65 72 20 0a 20 20 20 20  will never .    
3332e 20 20 20 20 20 20 2a 2a 20 61 72 72 69 76 65 2e        ** arrive.
3332f 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 0a 20 20  .          **.  
33330 20 20 20 20 20 20 20 20 2a 2a 20 49 6e 73 74 65          ** Inste
33331 61 64 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 75  ad, invoke the u
33332 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c  nlock-notify cal
33333 6c 62 61 63 6b 20 77 69 74 68 20 74 68 65 20 63  lback with the c
33334 6f 6e 74 65 78 74 0a 20 20 20 20 20 20 20 20 20  ontext.         
33335 20 2a 2a 20 61 72 72 61 79 20 61 6c 72 65 61 64   ** array alread
33336 79 20 61 63 63 75 6d 75 6c 61 74 65 64 2e 20 57  y accumulated. W
33337 65 20 63 61 6e 20 74 68 65 6e 20 63 6c 65 61 72  e can then clear
33338 20 74 68 65 20 61 72 72 61 79 20 61 6e 64 0a 20   the array and. 
33339 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 67 69           ** begi
3333a 6e 20 61 63 63 75 6d 75 6c 61 74 69 6e 67 20 61  n accumulating a
3333b 6e 79 20 66 75 72 74 68 65 72 20 63 6f 6e 74 65  ny further conte
3333c 78 74 20 70 6f 69 6e 74 65 72 73 20 77 69 74 68  xt pointers with
3333d 6f 75 74 20 0a 20 20 20 20 20 20 20 20 20 20 2a  out .          *
3333e 2a 20 72 65 71 75 69 72 69 6e 67 20 61 6e 79 20  * requiring any 
3333f 64 79 6e 61 6d 69 63 20 61 6c 6c 6f 63 61 74 69  dynamic allocati
33340 6f 6e 2e 20 54 68 69 73 20 69 73 20 73 75 62 2d  on. This is sub-
33341 6f 70 74 69 6d 61 6c 20 62 65 63 61 75 73 65 0a  optimal because.
33342 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20            ** it 
33343 6d 65 61 6e 73 20 74 68 61 74 20 69 6e 73 74 65  means that inste
33344 61 64 20 6f 66 20 6f 6e 65 20 63 61 6c 6c 62 61  ad of one callba
33345 63 6b 20 77 69 74 68 20 61 20 6c 61 72 67 65 20  ck with a large 
33346 61 72 72 61 79 20 6f 66 0a 20 20 20 20 20 20 20  array of.       
33347 20 20 20 2a 2a 20 63 6f 6e 74 65 78 74 20 70 6f     ** context po
33348 69 6e 74 65 72 73 20 74 68 65 20 61 70 70 6c 69  inters the appli
33349 63 61 74 69 6f 6e 20 77 69 6c 6c 20 72 65 63 65  cation will rece
3334a 69 76 65 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a  ive two or more.
3334b 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 61 6c            ** cal
3334c 6c 62 61 63 6b 73 20 77 69 74 68 20 73 6d 61 6c  lbacks with smal
3334d 6c 65 72 20 61 72 72 61 79 73 20 6f 66 20 63 6f  ler arrays of co
3334e 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 73 2c 20  ntext pointers, 
3334f 77 68 69 63 68 20 77 69 6c 6c 0a 20 20 20 20 20  which will.     
33350 20 20 20 20 20 2a 2a 20 72 65 64 75 63 65 20 74       ** reduce t
33351 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  he applications 
33352 61 62 69 6c 69 74 79 20 74 6f 20 70 72 69 6f 72  ability to prior
33353 69 74 69 7a 65 20 6d 75 6c 74 69 70 6c 65 20 0a  itize multiple .
33354 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e            ** con
33355 6e 65 63 74 69 6f 6e 73 2e 20 42 75 74 20 69 74  nections. But it
33356 20 69 73 20 74 68 65 20 62 65 73 74 20 74 68 61   is the best tha
33357 74 20 63 61 6e 20 62 65 20 64 6f 6e 65 20 75 6e  t can be done un
33358 64 65 72 20 74 68 65 0a 20 20 20 20 20 20 20 20  der the.        
33359 20 20 2a 2a 20 63 69 72 63 75 6d 73 74 61 6e 63    ** circumstanc
3335a 65 73 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  es..          */
3335b 0a 20 20 20 20 20 20 20 20 20 20 78 55 6e 6c 6f  .          xUnlo
3335c 63 6b 4e 6f 74 69 66 79 28 61 41 72 67 2c 20 6e  ckNotify(aArg, n
3335d 41 72 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Arg);.          
3335e 6e 41 72 67 20 3d 20 30 3b 0a 20 20 20 20 20 20  nArg = 0;.      
3335f 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
33360 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69    sqlite3EndBeni
33361 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 20  gnMalloc();..   
33362 20 20 20 61 41 72 67 5b 6e 41 72 67 2b 2b 5d 20     aArg[nArg++] 
33363 3d 20 70 2d 3e 70 55 6e 6c 6f 63 6b 41 72 67 3b  = p->pUnlockArg;
33364 0a 20 20 20 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f  .      xUnlockNo
33365 74 69 66 79 20 3d 20 70 2d 3e 78 55 6e 6c 6f 63  tify = p->xUnloc
33366 6b 4e 6f 74 69 66 79 3b 0a 20 20 20 20 20 20 70  kNotify;.      p
33367 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74  ->pUnlockConnect
33368 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  ion = 0;.      p
33369 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20  ->xUnlockNotify 
3336a 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 70 55  = 0;.      p->pU
3336b 6e 6c 6f 63 6b 41 72 67 20 3d 20 30 3b 0a 20 20  nlockArg = 0;.  
3336c 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 74 65 70    }..    /* Step
3336d 20 33 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70   3. */.    if( p
3336e 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65  ->pBlockingConne
3336f 63 74 69 6f 6e 3d 3d 30 20 26 26 20 70 2d 3e 70  ction==0 && p->p
33370 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e  UnlockConnection
33371 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ==0 ){.      /* 
33372 52 65 6d 6f 76 65 20 63 6f 6e 6e 65 63 74 69 6f  Remove connectio
33373 6e 20 70 20 66 72 6f 6d 20 74 68 65 20 62 6c 6f  n p from the blo
33374 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  cked connections
33375 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20   list. */.      
33376 2a 70 70 20 3d 20 70 2d 3e 70 4e 65 78 74 42 6c  *pp = p->pNextBl
33377 6f 63 6b 65 64 3b 0a 20 20 20 20 20 20 70 2d 3e  ocked;.      p->
33378 70 4e 65 78 74 42 6c 6f 63 6b 65 64 20 3d 20 30  pNextBlocked = 0
33379 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
3337a 20 20 20 70 70 20 3d 20 26 70 2d 3e 70 4e 65 78     pp = &p->pNex
3337b 74 42 6c 6f 63 6b 65 64 3b 0a 20 20 20 20 7d 0a  tBlocked;.    }.
3337c 20 20 7d 0a 0a 20 20 69 66 28 20 6e 41 72 67 21    }..  if( nArg!
3337d 3d 30 20 29 7b 0a 20 20 20 20 78 55 6e 6c 6f 63  =0 ){.    xUnloc
3337e 6b 4e 6f 74 69 66 79 28 61 41 72 67 2c 20 6e 41  kNotify(aArg, nA
3337f 72 67 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  rg);.  }.  sqlit
33380 65 33 5f 66 72 65 65 28 61 44 79 6e 29 3b 0a 20  e3_free(aDyn);. 
33381 20 6c 65 61 76 65 4d 75 74 65 78 28 29 3b 20 20   leaveMutex();  
33382 20 20 20 20 20 20 20 2f 2a 20 4c 65 61 76 65 20         /* Leave 
33383 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75  STATIC_MASTER mu
33384 74 65 78 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  tex */.}../*.** 
33385 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 77  This is called w
33386 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
33387 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 61 73 73   connection pass
33388 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e  ed as an argumen
33389 74 20 69 73 20 0a 2a 2a 20 62 65 69 6e 67 20 63  t is .** being c
3338a 6c 6f 73 65 64 2e 20 54 68 65 20 63 6f 6e 6e 65  losed. The conne
3338b 63 74 69 6f 6e 20 69 73 20 72 65 6d 6f 76 65 64  ction is removed
3338c 20 66 72 6f 6d 20 74 68 65 20 62 6c 6f 63 6b 65   from the blocke
3338d 64 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54  d list..*/.SQLIT
3338e 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
3338f 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e  qlite3Connection
33390 43 6c 6f 73 65 64 28 73 71 6c 69 74 65 33 20 2a  Closed(sqlite3 *
33391 64 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 43 6f  db){.  sqlite3Co
33392 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64  nnectionUnlocked
33393 28 64 62 29 3b 0a 20 20 65 6e 74 65 72 4d 75 74  (db);.  enterMut
33394 65 78 28 29 3b 0a 20 20 72 65 6d 6f 76 65 46 72  ex();.  removeFr
33395 6f 6d 42 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62  omBlockedList(db
33396 29 3b 0a 20 20 63 68 65 63 6b 4c 69 73 74 50 72  );.  checkListPr
33397 6f 70 65 72 74 69 65 73 28 64 62 29 3b 0a 20 20  operties(db);.  
33398 6c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a  leaveMutex();.}.
33399 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  #endif../*******
3339a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6e  ******* End of n
3339b 6f 74 69 66 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  otify.c ********
3339c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3339d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3339e 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
3339f 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
333a0 6c 65 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a  le fts3.c ******
333a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333a3 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
333a4 30 36 20 4f 63 74 20 31 30 0a 2a 2a 0a 2a 2a 20  06 Oct 10.**.** 
333a5 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
333a6 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
333a7 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
333a8 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
333a9 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
333aa 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
333ab 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
333ac 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
333ad 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
333ae 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
333af 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
333b0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
333b1 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
333b2 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
333b3 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
333b4 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
333b5 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
333b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
333bb 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20 53  .** This is an S
333bc 51 4c 69 74 65 20 6d 6f 64 75 6c 65 20 69 6d 70  QLite module imp
333bd 6c 65 6d 65 6e 74 69 6e 67 20 66 75 6c 6c 2d 74  lementing full-t
333be 65 78 74 20 73 65 61 72 63 68 2e 0a 2a 2f 0a 0a  ext search..*/..
333bf 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69  /*.** The code i
333c0 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f  n this file is o
333c1 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a  nly compiled if:
333c2 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65  .**.**     * The
333c3 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20   FTS3 module is 
333c4 62 65 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61  being built as a
333c5 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20  n extension.**  
333c6 20 20 20 20 20 28 69 6e 20 77 68 69 63 68 20 63       (in which c
333c7 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20  ase SQLITE_CORE 
333c8 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c  is not defined),
333c9 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20   or.**.**     * 
333ca 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20  The FTS3 module 
333cb 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69  is being built i
333cc 6e 74 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a  nto the core of.
333cd 2a 2a 20 20 20 20 20 20 20 53 51 4c 69 74 65 20  **       SQLite 
333ce 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53  (in which case S
333cf 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
333d0 33 20 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a  3 is defined)..*
333d1 2f 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  /../* TODO(shess
333d2 29 20 43 6f 6e 73 69 64 65 72 20 65 78 70 6f 72  ) Consider expor
333d3 74 69 6e 67 20 74 68 69 73 20 63 6f 6d 6d 65 6e  ting this commen
333d4 74 20 74 6f 20 61 6e 20 48 54 4d 4c 20 66 69 6c  t to an HTML fil
333d5 65 20 6f 72 20 74 68 65 0a 2a 2a 20 77 69 6b 69  e or the.** wiki
333d6 2e 0a 2a 2f 0a 2f 2a 20 54 68 65 20 66 75 6c 6c  ..*/./* The full
333d7 2d 74 65 78 74 20 69 6e 64 65 78 20 69 73 20 73  -text index is s
333d8 74 6f 72 65 64 20 69 6e 20 61 20 73 65 72 69 65  tored in a serie
333d9 73 20 6f 66 20 62 2b 74 72 65 65 20 28 2d 6c 69  s of b+tree (-li
333da 6b 65 29 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  ke).** structure
333db 73 20 63 61 6c 6c 65 64 20 73 65 67 6d 65 6e 74  s called segment
333dc 73 20 77 68 69 63 68 20 6d 61 70 20 74 65 72 6d  s which map term
333dd 73 20 74 6f 20 64 6f 63 6c 69 73 74 73 2e 20 20  s to doclists.  
333de 54 68 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  The.** structure
333df 73 20 61 72 65 20 6c 69 6b 65 20 62 2b 74 72 65  s are like b+tre
333e0 65 73 20 69 6e 20 6c 61 79 6f 75 74 2c 20 62 75  es in layout, bu
333e1 74 20 61 72 65 20 63 6f 6e 73 74 72 75 63 74 65  t are constructe
333e2 64 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 62 6f  d from the.** bo
333e3 74 74 6f 6d 20 75 70 20 69 6e 20 6f 70 74 69 6d  ttom up in optim
333e4 61 6c 20 66 61 73 68 69 6f 6e 20 61 6e 64 20 61  al fashion and a
333e5 72 65 20 6e 6f 74 20 75 70 64 61 74 61 62 6c 65  re not updatable
333e6 2e 20 20 53 69 6e 63 65 20 74 72 65 65 73 0a 2a  .  Since trees.*
333e7 2a 20 61 72 65 20 62 75 69 6c 74 20 66 72 6f 6d  * are built from
333e8 20 74 68 65 20 62 6f 74 74 6f 6d 20 75 70 2c 20   the bottom up, 
333e9 74 68 69 6e 67 73 20 77 69 6c 6c 20 62 65 20 64  things will be d
333ea 65 73 63 72 69 62 65 64 20 66 72 6f 6d 20 74 68  escribed from th
333eb 65 0a 2a 2a 20 62 6f 74 74 6f 6d 20 75 70 2e 0a  e.** bottom up..
333ec 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 56 61 72 69 6e  **.**.**** Varin
333ed 74 73 20 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 62  ts ****.** The b
333ee 61 73 69 63 20 75 6e 69 74 20 6f 66 20 65 6e 63  asic unit of enc
333ef 6f 64 69 6e 67 20 69 73 20 61 20 76 61 72 69 61  oding is a varia
333f0 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67  ble-length integ
333f1 65 72 20 63 61 6c 6c 65 64 20 61 0a 2a 2a 20 76  er called a.** v
333f2 61 72 69 6e 74 2e 20 20 57 65 20 65 6e 63 6f 64  arint.  We encod
333f3 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74  e variable-lengt
333f4 68 20 69 6e 74 65 67 65 72 73 20 69 6e 20 6c 69  h integers in li
333f5 74 74 6c 65 2d 65 6e 64 69 61 6e 20 6f 72 64 65  ttle-endian orde
333f6 72 0a 2a 2a 20 75 73 69 6e 67 20 73 65 76 65 6e  r.** using seven
333f7 20 62 69 74 73 20 2a 20 70 65 72 20 62 79 74 65   bits * per byte
333f8 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
333f9 2a 2a 20 4b 45 59 3a 0a 2a 2a 20 20 20 20 20 20  ** KEY:.**      
333fa 20 20 20 41 20 3d 20 30 78 78 78 78 78 78 78 20     A = 0xxxxxxx 
333fb 20 20 20 37 20 62 69 74 73 20 6f 66 20 64 61 74     7 bits of dat
333fc 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62  a and one flag b
333fd 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 42 20  it.**         B 
333fe 3d 20 31 78 78 78 78 78 78 78 20 20 20 20 37 20  = 1xxxxxxx    7 
333ff 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64  bits of data and
33400 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a   one flag bit.**
33401 0a 2a 2a 20 20 37 20 62 69 74 73 20 2d 20 41 0a  .**  7 bits - A.
33402 2a 2a 20 31 34 20 62 69 74 73 20 2d 20 42 41 0a  ** 14 bits - BA.
33403 2a 2a 20 32 31 20 62 69 74 73 20 2d 20 42 42 41  ** 21 bits - BBA
33404 0a 2a 2a 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a  .** and so on..*
33405 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 69 64 65  *.** This is ide
33406 6e 74 69 63 61 6c 20 74 6f 20 68 6f 77 20 73 71  ntical to how sq
33407 6c 69 74 65 20 65 6e 63 6f 64 65 73 20 76 61 72  lite encodes var
33408 69 6e 74 73 20 28 73 65 65 20 75 74 69 6c 2e 63  ints (see util.c
33409 29 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 44 6f  )..**.**.**** Do
3340a 63 75 6d 65 6e 74 20 6c 69 73 74 73 20 2a 2a 2a  cument lists ***
3340b 2a 0a 2a 2a 20 41 20 64 6f 63 6c 69 73 74 20 28  *.** A doclist (
3340c 64 6f 63 75 6d 65 6e 74 20 6c 69 73 74 29 20 68  document list) h
3340d 6f 6c 64 73 20 61 20 64 6f 63 69 64 2d 73 6f 72  olds a docid-sor
3340e 74 65 64 20 6c 69 73 74 20 6f 66 20 68 69 74 73  ted list of hits
3340f 20 66 6f 72 20 61 0a 2a 2a 20 67 69 76 65 6e 20   for a.** given 
33410 74 65 72 6d 2e 20 20 44 6f 63 6c 69 73 74 73 20  term.  Doclists 
33411 68 6f 6c 64 20 64 6f 63 69 64 73 2c 20 61 6e 64  hold docids, and
33412 20 63 61 6e 20 6f 70 74 69 6f 6e 61 6c 6c 79 20   can optionally 
33413 61 73 73 6f 63 69 61 74 65 0a 2a 2a 20 74 6f 6b  associate.** tok
33414 65 6e 20 70 6f 73 69 74 69 6f 6e 73 20 61 6e 64  en positions and
33415 20 6f 66 66 73 65 74 73 20 77 69 74 68 20 64 6f   offsets with do
33416 63 69 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 44 4c  cids..**.** A DL
33417 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45  _POSITIONS_OFFSE
33418 54 53 20 64 6f 63 6c 69 73 74 20 69 73 20 73 74  TS doclist is st
33419 6f 72 65 64 20 6c 69 6b 65 20 74 68 69 73 3a 0a  ored like this:.
3341a 2a 2a 0a 2a 2a 20 61 72 72 61 79 20 7b 0a 2a 2a  **.** array {.**
3341b 20 20 20 76 61 72 69 6e 74 20 64 6f 63 69 64 3b     varint docid;
3341c 0a 2a 2a 20 20 20 61 72 72 61 79 20 7b 20 20 20  .**   array {   
3341d 20 20 20 20 20 20 20 20 20 20 20 20 20 28 70 6f               (po
3341e 73 69 74 69 6f 6e 20 6c 69 73 74 20 66 6f 72 20  sition list for 
3341f 63 6f 6c 75 6d 6e 20 30 29 0a 2a 2a 20 20 20 20  column 0).**    
33420 20 76 61 72 69 6e 74 20 70 6f 73 69 74 69 6f 6e   varint position
33421 3b 20 20 20 20 20 28 64 65 6c 74 61 20 66 72 6f  ;     (delta fro
33422 6d 20 70 72 65 76 69 6f 75 73 20 70 6f 73 69 74  m previous posit
33423 69 6f 6e 20 70 6c 75 73 20 50 4f 53 5f 42 41 53  ion plus POS_BAS
33424 45 29 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74  E).**     varint
33425 20 73 74 61 72 74 4f 66 66 73 65 74 3b 20 20 28   startOffset;  (
33426 64 65 6c 74 61 20 66 72 6f 6d 20 70 72 65 76 69  delta from previ
33427 6f 75 73 20 73 74 61 72 74 4f 66 66 73 65 74 29  ous startOffset)
33428 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20 65  .**     varint e
33429 6e 64 4f 66 66 73 65 74 3b 20 20 20 20 28 64 65  ndOffset;    (de
3342a 6c 74 61 20 66 72 6f 6d 20 73 74 61 72 74 4f 66  lta from startOf
3342b 66 73 65 74 29 0a 2a 2a 20 20 20 7d 0a 2a 2a 20  fset).**   }.** 
3342c 20 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20    array {.**    
3342d 20 76 61 72 69 6e 74 20 50 4f 53 5f 43 4f 4c 55   varint POS_COLU
3342e 4d 4e 3b 20 20 20 28 6d 61 72 6b 73 20 73 74 61  MN;   (marks sta
3342f 72 74 20 6f 66 20 70 6f 73 69 74 69 6f 6e 20 6c  rt of position l
33430 69 73 74 20 66 6f 72 20 6e 65 77 20 63 6f 6c 75  ist for new colu
33431 6d 6e 29 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e  mn).**     varin
33432 74 20 63 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20  t column;       
33433 28 69 6e 64 65 78 20 6f 66 20 6e 65 77 20 63 6f  (index of new co
33434 6c 75 6d 6e 29 0a 2a 2a 20 20 20 20 20 61 72 72  lumn).**     arr
33435 61 79 20 7b 0a 2a 2a 20 20 20 20 20 20 20 76 61  ay {.**       va
33436 72 69 6e 74 20 70 6f 73 69 74 69 6f 6e 3b 20 20  rint position;  
33437 20 28 64 65 6c 74 61 20 66 72 6f 6d 20 70 72 65   (delta from pre
33438 76 69 6f 75 73 20 70 6f 73 69 74 69 6f 6e 20 70  vious position p
33439 6c 75 73 20 50 4f 53 5f 42 41 53 45 29 0a 2a 2a  lus POS_BASE).**
3343a 20 20 20 20 20 20 20 76 61 72 69 6e 74 20 73 74         varint st
3343b 61 72 74 4f 66 66 73 65 74 3b 28 64 65 6c 74 61  artOffset;(delta
3343c 20 66 72 6f 6d 20 70 72 65 76 69 6f 75 73 20 73   from previous s
3343d 74 61 72 74 4f 66 66 73 65 74 29 0a 2a 2a 20 20  tartOffset).**  
3343e 20 20 20 20 20 76 61 72 69 6e 74 20 65 6e 64 4f       varint endO
3343f 66 66 73 65 74 3b 20 20 28 64 65 6c 74 61 20 66  ffset;  (delta f
33440 72 6f 6d 20 73 74 61 72 74 4f 66 66 73 65 74 29  rom startOffset)
33441 0a 2a 2a 20 20 20 20 20 7d 0a 2a 2a 20 20 20 7d  .**     }.**   }
33442 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20 50 4f 53  .**   varint POS
33443 5f 45 4e 44 3b 20 20 20 20 20 20 20 20 28 6d 61  _END;        (ma
33444 72 6b 73 20 65 6e 64 20 6f 66 20 70 6f 73 69 74  rks end of posit
33445 69 6f 6e 73 20 66 6f 72 20 74 68 69 73 20 64 6f  ions for this do
33446 63 75 6d 65 6e 74 2e 0a 2a 2a 20 7d 0a 2a 2a 0a  cument..** }.**.
33447 2a 2a 20 48 65 72 65 2c 20 61 72 72 61 79 20 7b  ** Here, array {
33448 20 58 20 7d 20 6d 65 61 6e 73 20 7a 65 72 6f 20   X } means zero 
33449 6f 72 20 6d 6f 72 65 20 6f 63 63 75 72 72 65 6e  or more occurren
3344a 63 65 73 20 6f 66 20 58 2c 20 61 64 6a 61 63 65  ces of X, adjace
3344b 6e 74 20 69 6e 0a 2a 2a 20 6d 65 6d 6f 72 79 2e  nt in.** memory.
3344c 20 20 41 20 22 70 6f 73 69 74 69 6f 6e 22 20 69    A "position" i
3344d 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 61 20  s an index of a 
3344e 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 74 6f 6b  token in the tok
3344f 65 6e 20 73 74 72 65 61 6d 0a 2a 2a 20 67 65 6e  en stream.** gen
33450 65 72 61 74 65 64 20 62 79 20 74 68 65 20 74 6f  erated by the to
33451 6b 65 6e 69 7a 65 72 2c 20 77 68 69 6c 65 20 61  kenizer, while a
33452 6e 20 22 6f 66 66 73 65 74 22 20 69 73 20 61 20  n "offset" is a 
33453 62 79 74 65 20 6f 66 66 73 65 74 2c 0a 2a 2a 20  byte offset,.** 
33454 62 6f 74 68 20 62 61 73 65 64 20 61 74 20 30 2e  both based at 0.
33455 20 20 4e 6f 74 65 20 74 68 61 74 20 50 4f 53 5f    Note that POS_
33456 45 4e 44 20 61 6e 64 20 50 4f 53 5f 43 4f 4c 55  END and POS_COLU
33457 4d 4e 20 6f 63 63 75 72 20 69 6e 20 74 68 65 0a  MN occur in the.
33458 2a 2a 20 73 61 6d 65 20 6c 6f 67 69 63 61 6c 20  ** same logical 
33459 70 6c 61 63 65 20 61 73 20 74 68 65 20 70 6f 73  place as the pos
3345a 69 74 69 6f 6e 20 65 6c 65 6d 65 6e 74 2c 20 61  ition element, a
3345b 6e 64 20 61 63 74 20 61 73 20 73 65 6e 74 69 6e  nd act as sentin
3345c 61 6c 73 0a 2a 2a 20 65 6e 64 69 6e 67 20 61 20  als.** ending a 
3345d 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20 61 72  position list ar
3345e 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 44 4c 5f  ray..**.** A DL_
3345f 50 4f 53 49 54 49 4f 4e 53 20 64 6f 63 6c 69 73  POSITIONS doclis
33460 74 20 6f 6d 69 74 73 20 74 68 65 20 73 74 61 72  t omits the star
33461 74 4f 66 66 73 65 74 20 61 6e 64 20 65 6e 64 4f  tOffset and endO
33462 66 66 73 65 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61  ffset.** informa
33463 74 69 6f 6e 2e 20 20 41 20 44 4c 5f 44 4f 43 49  tion.  A DL_DOCI
33464 44 53 20 64 6f 63 6c 69 73 74 20 6f 6d 69 74 73  DS doclist omits
33465 20 62 6f 74 68 20 74 68 65 20 70 6f 73 69 74 69   both the positi
33466 6f 6e 20 61 6e 64 0a 2a 2a 20 6f 66 66 73 65 74  on and.** offset
33467 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 62 65   information, be
33468 63 6f 6d 69 6e 67 20 61 6e 20 61 72 72 61 79 20  coming an array 
33469 6f 66 20 76 61 72 69 6e 74 2d 65 6e 63 6f 64 65  of varint-encode
3346a 64 20 64 6f 63 69 64 73 2e 0a 2a 2a 0a 2a 2a 20  d docids..**.** 
3346b 4f 6e 2d 64 69 73 6b 20 64 61 74 61 20 69 73 20  On-disk data is 
3346c 73 74 6f 72 65 64 20 61 73 20 74 79 70 65 20 44  stored as type D
3346d 4c 5f 44 45 46 41 55 4c 54 2c 20 73 6f 20 77 65  L_DEFAULT, so we
3346e 20 64 6f 6e 27 74 20 73 65 72 69 61 6c 69 7a 65   don't serialize
3346f 0a 2a 2a 20 74 68 65 20 74 79 70 65 2e 20 20 44  .** the type.  D
33470 75 65 20 74 6f 20 68 6f 77 20 64 65 6c 65 74 69  ue to how deleti
33471 6f 6e 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65  on is implemente
33472 64 20 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74  d in the segment
33473 61 74 69 6f 6e 0a 2a 2a 20 73 79 73 74 65 6d 2c  ation.** system,
33474 20 6f 6e 2d 64 69 73 6b 20 64 6f 63 6c 69 73 74   on-disk doclist
33475 73 20 4d 55 53 54 20 73 74 6f 72 65 20 61 74 20  s MUST store at 
33476 6c 65 61 73 74 20 70 6f 73 69 74 69 6f 6e 73 2e  least positions.
33477 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67 6d  .**.**.**** Segm
33478 65 6e 74 20 6c 65 61 66 20 6e 6f 64 65 73 20 2a  ent leaf nodes *
33479 2a 2a 2a 0a 2a 2a 20 53 65 67 6d 65 6e 74 20 6c  ***.** Segment l
3347a 65 61 66 20 6e 6f 64 65 73 20 73 74 6f 72 65 20  eaf nodes store 
3347b 74 65 72 6d 73 20 61 6e 64 20 64 6f 63 6c 69 73  terms and doclis
3347c 74 73 2c 20 6f 72 64 65 72 65 64 20 62 79 20 74  ts, ordered by t
3347d 65 72 6d 2e 20 20 4c 65 61 66 0a 2a 2a 20 6e 6f  erm.  Leaf.** no
3347e 64 65 73 20 61 72 65 20 77 72 69 74 74 65 6e 20  des are written 
3347f 75 73 69 6e 67 20 4c 65 61 66 57 72 69 74 65 72  using LeafWriter
33480 2c 20 61 6e 64 20 72 65 61 64 20 75 73 69 6e 67  , and read using
33481 20 4c 65 61 66 52 65 61 64 65 72 20 28 74 6f 0a   LeafReader (to.
33482 2a 2a 20 69 74 65 72 61 74 65 20 74 68 72 6f 75  ** iterate throu
33483 67 68 20 61 20 73 69 6e 67 6c 65 20 6c 65 61 66  gh a single leaf
33484 20 6e 6f 64 65 27 73 20 64 61 74 61 29 20 61 6e   node's data) an
33485 64 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 28  d LeavesReader (
33486 74 6f 0a 2a 2a 20 69 74 65 72 61 74 65 20 74 68  to.** iterate th
33487 72 6f 75 67 68 20 61 20 73 65 67 6d 65 6e 74 27  rough a segment'
33488 73 20 65 6e 74 69 72 65 20 6c 65 61 66 20 6c 61  s entire leaf la
33489 79 65 72 29 2e 20 20 4c 65 61 66 20 6e 6f 64 65  yer).  Leaf node
3348a 73 20 68 61 76 65 0a 2a 2a 20 74 68 65 20 66 6f  s have.** the fo
3348b 72 6d 61 74 3a 0a 2a 2a 0a 2a 2a 20 76 61 72 69  rmat:.**.** vari
3348c 6e 74 20 69 48 65 69 67 68 74 3b 20 20 20 20 20  nt iHeight;     
3348d 20 20 20 20 20 20 20 20 28 68 65 69 67 68 74 20          (height 
3348e 66 72 6f 6d 20 6c 65 61 66 20 6c 65 76 65 6c 2c  from leaf level,
3348f 20 61 6c 77 61 79 73 20 30 29 0a 2a 2a 20 76 61   always 0).** va
33490 72 69 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20 20  rint nTerm;     
33491 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74            (lengt
33492 68 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d 29  h of first term)
33493 0a 2a 2a 20 63 68 61 72 20 70 54 65 72 6d 5b 6e  .** char pTerm[n
33494 54 65 72 6d 5d 3b 20 20 20 20 20 20 20 20 20 20  Term];          
33495 28 63 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72 73  (content of firs
33496 74 20 74 65 72 6d 29 0a 2a 2a 20 76 61 72 69 6e  t term).** varin
33497 74 20 6e 44 6f 63 6c 69 73 74 3b 20 20 20 20 20  t nDoclist;     
33498 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f         (length o
33499 66 20 74 65 72 6d 27 73 20 61 73 73 6f 63 69 61  f term's associa
3349a 74 65 64 20 64 6f 63 6c 69 73 74 29 0a 2a 2a 20  ted doclist).** 
3349b 63 68 61 72 20 70 44 6f 63 6c 69 73 74 5b 6e 44  char pDoclist[nD
3349c 6f 63 6c 69 73 74 5d 3b 20 20 20 20 28 63 6f 6e  oclist];    (con
3349d 74 65 6e 74 20 6f 66 20 64 6f 63 6c 69 73 74 29  tent of doclist)
3349e 0a 2a 2a 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20  .** array {.**  
3349f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
334a0 20 20 20 20 20 20 20 20 20 20 20 28 66 75 72 74             (furt
334a1 68 65 72 20 74 65 72 6d 73 20 61 72 65 20 64 65  her terms are de
334a2 6c 74 61 2d 65 6e 63 6f 64 65 64 29 0a 2a 2a 20  lta-encoded).** 
334a3 20 20 76 61 72 69 6e 74 20 6e 50 72 65 66 69 78    varint nPrefix
334a4 3b 20 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e  ;           (len
334a5 67 74 68 20 6f 66 20 70 72 65 66 69 78 20 73 68  gth of prefix sh
334a6 61 72 65 64 20 77 69 74 68 20 70 72 65 76 69 6f  ared with previo
334a7 75 73 20 74 65 72 6d 29 0a 2a 2a 20 20 20 76 61  us term).**   va
334a8 72 69 6e 74 20 6e 53 75 66 66 69 78 3b 20 20 20  rint nSuffix;   
334a9 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20          (length 
334aa 6f 66 20 75 6e 73 68 61 72 65 64 20 73 75 66 66  of unshared suff
334ab 69 78 29 0a 2a 2a 20 20 20 63 68 61 72 20 70 54  ix).**   char pT
334ac 65 72 6d 53 75 66 66 69 78 5b 6e 53 75 66 66 69  ermSuffix[nSuffi
334ad 78 5d 3b 28 75 6e 73 68 61 72 65 64 20 73 75 66  x];(unshared suf
334ae 66 69 78 20 6f 66 20 6e 65 78 74 20 74 65 72 6d  fix of next term
334af 29 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20 6e 44  ).**   varint nD
334b0 6f 63 6c 69 73 74 3b 20 20 20 20 20 20 20 20 20  oclist;         
334b1 20 28 6c 65 6e 67 74 68 20 6f 66 20 74 65 72 6d   (length of term
334b2 27 73 20 61 73 73 6f 63 69 61 74 65 64 20 64 6f  's associated do
334b3 63 6c 69 73 74 29 0a 2a 2a 20 20 20 63 68 61 72  clist).**   char
334b4 20 70 44 6f 63 6c 69 73 74 5b 6e 44 6f 63 6c 69   pDoclist[nDocli
334b5 73 74 5d 3b 20 20 28 63 6f 6e 74 65 6e 74 20 6f  st];  (content o
334b6 66 20 64 6f 63 6c 69 73 74 29 0a 2a 2a 20 7d 0a  f doclist).** }.
334b7 2a 2a 0a 2a 2a 20 48 65 72 65 2c 20 61 72 72 61  **.** Here, arra
334b8 79 20 7b 20 58 20 7d 20 6d 65 61 6e 73 20 7a 65  y { X } means ze
334b9 72 6f 20 6f 72 20 6d 6f 72 65 20 6f 63 63 75 72  ro or more occur
334ba 72 65 6e 63 65 73 20 6f 66 20 58 2c 20 61 64 6a  rences of X, adj
334bb 61 63 65 6e 74 20 69 6e 0a 2a 2a 20 6d 65 6d 6f  acent in.** memo
334bc 72 79 2e 0a 2a 2a 0a 2a 2a 20 4c 65 61 66 20 6e  ry..**.** Leaf n
334bd 6f 64 65 73 20 61 72 65 20 62 72 6f 6b 65 6e 20  odes are broken 
334be 69 6e 74 6f 20 62 6c 6f 63 6b 73 20 77 68 69 63  into blocks whic
334bf 68 20 61 72 65 20 73 74 6f 72 65 64 20 63 6f 6e  h are stored con
334c0 74 69 67 75 6f 75 73 6c 79 20 69 6e 0a 2a 2a 20  tiguously in.** 
334c1 74 68 65 20 25 5f 73 65 67 6d 65 6e 74 73 20 74  the %_segments t
334c2 61 62 6c 65 20 69 6e 20 73 6f 72 74 65 64 20 6f  able in sorted o
334c3 72 64 65 72 2e 20 20 54 68 69 73 20 6d 65 61 6e  rder.  This mean
334c4 73 20 74 68 61 74 20 77 68 65 6e 20 74 68 65 20  s that when the 
334c5 65 6e 64 0a 2a 2a 20 6f 66 20 61 20 6e 6f 64 65  end.** of a node
334c6 20 69 73 20 72 65 61 63 68 65 64 2c 20 74 68 65   is reached, the
334c7 20 6e 65 78 74 20 74 65 72 6d 20 69 73 20 69 6e   next term is in
334c8 20 74 68 65 20 6e 6f 64 65 20 77 69 74 68 20 74   the node with t
334c9 68 65 20 6e 65 78 74 0a 2a 2a 20 67 72 65 61 74  he next.** great
334ca 65 72 20 6e 6f 64 65 20 69 64 2e 0a 2a 2a 0a 2a  er node id..**.*
334cb 2a 20 4e 65 77 20 64 61 74 61 20 69 73 20 73 70  * New data is sp
334cc 69 6c 6c 65 64 20 74 6f 20 61 20 6e 65 77 20 6c  illed to a new l
334cd 65 61 66 20 6e 6f 64 65 20 77 68 65 6e 20 74 68  eaf node when th
334ce 65 20 63 75 72 72 65 6e 74 20 6e 6f 64 65 0a 2a  e current node.*
334cf 2a 20 65 78 63 65 65 64 73 20 4c 45 41 46 5f 4d  * exceeds LEAF_M
334d0 41 58 20 62 79 74 65 73 20 28 64 65 66 61 75 6c  AX bytes (defaul
334d1 74 20 32 30 34 38 29 2e 20 20 4e 65 77 20 64 61  t 2048).  New da
334d2 74 61 20 77 68 69 63 68 20 69 74 73 65 6c 66 20  ta which itself 
334d3 69 73 0a 2a 2a 20 6c 61 72 67 65 72 20 74 68 61  is.** larger tha
334d4 6e 20 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e  n STANDALONE_MIN
334d5 20 28 64 65 66 61 75 6c 74 20 31 30 32 34 29 20   (default 1024) 
334d6 69 73 20 70 6c 61 63 65 64 20 69 6e 20 61 20 73  is placed in a s
334d7 74 61 6e 64 61 6c 6f 6e 65 0a 2a 2a 20 6e 6f 64  tandalone.** nod
334d8 65 20 28 61 20 6c 65 61 66 20 6e 6f 64 65 20 77  e (a leaf node w
334d9 69 74 68 20 61 20 73 69 6e 67 6c 65 20 74 65 72  ith a single ter
334da 6d 20 61 6e 64 20 64 6f 63 6c 69 73 74 29 2e 20  m and doclist). 
334db 20 54 68 65 20 67 6f 61 6c 20 6f 66 0a 2a 2a 20   The goal of.** 
334dc 74 68 65 73 65 20 73 65 74 74 69 6e 67 73 20 69  these settings i
334dd 73 20 74 6f 20 70 61 63 6b 20 74 6f 67 65 74 68  s to pack togeth
334de 65 72 20 67 72 6f 75 70 73 20 6f 66 20 73 6d 61  er groups of sma
334df 6c 6c 20 64 6f 63 6c 69 73 74 73 20 77 68 69 6c  ll doclists whil
334e0 65 0a 2a 2a 20 6d 61 6b 69 6e 67 20 69 74 20 65  e.** making it e
334e1 66 66 69 63 69 65 6e 74 20 74 6f 20 64 69 72 65  fficient to dire
334e2 63 74 6c 79 20 61 63 63 65 73 73 20 6c 61 72 67  ctly access larg
334e3 65 20 64 6f 63 6c 69 73 74 73 2e 20 20 54 68 65  e doclists.  The
334e4 0a 2a 2a 20 61 73 73 75 6d 70 74 69 6f 6e 20 69  .** assumption i
334e5 73 20 74 68 61 74 20 6c 61 72 67 65 20 64 6f 63  s that large doc
334e6 6c 69 73 74 73 20 72 65 70 72 65 73 65 6e 74 20  lists represent 
334e7 74 65 72 6d 73 20 77 68 69 63 68 20 61 72 65 20  terms which are 
334e8 6d 6f 72 65 0a 2a 2a 20 6c 69 6b 65 6c 79 20 74  more.** likely t
334e9 6f 20 62 65 20 71 75 65 72 79 20 74 61 72 67 65  o be query targe
334ea 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73  ts..**.** TODO(s
334eb 68 65 73 73 29 20 49 74 20 6d 61 79 20 62 65 20  hess) It may be 
334ec 75 73 65 66 75 6c 20 66 6f 72 20 62 6c 6f 63 6b  useful for block
334ed 69 6e 67 20 64 65 63 69 73 69 6f 6e 73 20 74 6f  ing decisions to
334ee 20 62 65 20 6d 6f 72 65 0a 2a 2a 20 64 79 6e 61   be more.** dyna
334ef 6d 69 63 2e 20 20 46 6f 72 20 69 6e 73 74 61 6e  mic.  For instan
334f0 63 65 2c 20 69 74 20 6d 61 79 20 6d 61 6b 65 20  ce, it may make 
334f1 6d 6f 72 65 20 73 65 6e 73 65 20 74 6f 20 68 61  more sense to ha
334f2 76 65 20 61 20 32 2e 35 6b 20 6c 65 61 66 0a 2a  ve a 2.5k leaf.*
334f3 2a 20 6e 6f 64 65 20 72 61 74 68 65 72 20 74 68  * node rather th
334f4 61 6e 20 73 70 6c 69 74 74 69 6e 67 20 69 6e 74  an splitting int
334f5 6f 20 32 6b 20 61 6e 64 20 2e 35 6b 20 6e 6f 64  o 2k and .5k nod
334f6 65 73 2e 20 20 4d 79 20 69 6e 74 75 69 74 69 6f  es.  My intuitio
334f7 6e 20 69 73 0a 2a 2a 20 74 68 61 74 20 74 68 69  n is.** that thi
334f8 73 20 6d 69 67 68 74 20 65 78 74 65 6e 64 20 74  s might extend t
334f9 68 72 6f 75 67 68 20 32 78 20 6f 72 20 34 78 20  hrough 2x or 4x 
334fa 74 68 65 20 70 61 67 65 73 69 7a 65 2e 0a 2a 2a  the pagesize..**
334fb 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74  .**.**** Segment
334fc 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20   interior nodes 
334fd 2a 2a 2a 2a 0a 2a 2a 20 53 65 67 6d 65 6e 74 20  ****.** Segment 
334fe 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 73  interior nodes s
334ff 74 6f 72 65 20 62 6c 6f 63 6b 69 64 73 20 66 6f  tore blockids fo
33500 72 20 73 75 62 74 72 65 65 20 6e 6f 64 65 73 20  r subtree nodes 
33501 61 6e 64 20 74 65 72 6d 73 0a 2a 2a 20 74 6f 20  and terms.** to 
33502 64 65 73 63 72 69 62 65 20 77 68 61 74 20 64 61  describe what da
33503 74 61 20 69 73 20 73 74 6f 72 65 64 20 62 79 20  ta is stored by 
33504 74 68 65 20 65 61 63 68 20 73 75 62 74 72 65 65  the each subtree
33505 2e 20 20 49 6e 74 65 72 69 6f 72 0a 2a 2a 20 6e  .  Interior.** n
33506 6f 64 65 73 20 61 72 65 20 77 72 69 74 74 65 6e  odes are written
33507 20 75 73 69 6e 67 20 49 6e 74 65 72 69 6f 72 57   using InteriorW
33508 72 69 74 65 72 2c 20 61 6e 64 20 72 65 61 64 20  riter, and read 
33509 75 73 69 6e 67 0a 2a 2a 20 49 6e 74 65 72 69 6f  using.** Interio
3350a 72 52 65 61 64 65 72 2e 20 20 49 6e 74 65 72 69  rReader.  Interi
3350b 6f 72 57 72 69 74 65 72 73 20 61 72 65 20 63 72  orWriters are cr
3350c 65 61 74 65 64 20 61 73 20 6e 65 65 64 65 64 20  eated as needed 
3350d 77 68 65 6e 0a 2a 2a 20 53 65 67 6d 65 6e 74 57  when.** SegmentW
3350e 72 69 74 65 72 20 63 72 65 61 74 65 73 20 6e 65  riter creates ne
3350f 77 20 6c 65 61 66 20 6e 6f 64 65 73 2c 20 6f 72  w leaf nodes, or
33510 20 77 68 65 6e 20 61 6e 20 69 6e 74 65 72 69 6f   when an interio
33511 72 20 6e 6f 64 65 0a 2a 2a 20 69 74 73 65 6c 66  r node.** itself
33512 20 67 72 6f 77 73 20 74 6f 6f 20 62 69 67 20 61   grows too big a
33513 6e 64 20 6d 75 73 74 20 62 65 20 73 70 6c 69 74  nd must be split
33514 2e 20 20 54 68 65 20 66 6f 72 6d 61 74 20 6f 66  .  The format of
33515 20 69 6e 74 65 72 69 6f 72 0a 2a 2a 20 6e 6f 64   interior.** nod
33516 65 73 3a 0a 2a 2a 0a 2a 2a 20 76 61 72 69 6e 74  es:.**.** varint
33517 20 69 48 65 69 67 68 74 3b 20 20 20 20 20 20 20   iHeight;       
33518 20 20 20 20 28 68 65 69 67 68 74 20 66 72 6f 6d      (height from
33519 20 6c 65 61 66 20 6c 65 76 65 6c 2c 20 61 6c 77   leaf level, alw
3351a 61 79 73 20 3e 30 29 0a 2a 2a 20 76 61 72 69 6e  ays >0).** varin
3351b 74 20 69 42 6c 6f 63 6b 69 64 3b 20 20 20 20 20  t iBlockid;     
3351c 20 20 20 20 20 28 62 6c 6f 63 6b 20 69 64 20 6f       (block id o
3351d 66 20 6e 6f 64 65 27 73 20 6c 65 66 74 6d 6f 73  f node's leftmos
3351e 74 20 73 75 62 74 72 65 65 29 0a 2a 2a 20 6f 70  t subtree).** op
3351f 74 69 6f 6e 61 6c 20 7b 0a 2a 2a 20 20 20 76 61  tional {.**   va
33520 72 69 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20 20  rint nTerm;     
33521 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66        (length of
33522 20 66 69 72 73 74 20 74 65 72 6d 29 0a 2a 2a 20   first term).** 
33523 20 20 63 68 61 72 20 70 54 65 72 6d 5b 6e 54 65    char pTerm[nTe
33524 72 6d 5d 3b 20 20 20 20 20 20 28 63 6f 6e 74 65  rm];      (conte
33525 6e 74 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d  nt of first term
33526 29 0a 2a 2a 20 20 20 61 72 72 61 79 20 7b 0a 2a  ).**   array {.*
33527 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
33528 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33529 20 28 66 75 72 74 68 65 72 20 74 65 72 6d 73 20   (further terms 
3352a 61 72 65 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65  are delta-encode
3352b 64 29 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74  d).**     varint
3352c 20 6e 50 72 65 66 69 78 3b 20 20 20 20 20 20 20   nPrefix;       
3352d 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20       (length of 
3352e 73 68 61 72 65 64 20 70 72 65 66 69 78 20 77 69  shared prefix wi
3352f 74 68 20 70 72 65 76 69 6f 75 73 20 74 65 72 6d  th previous term
33530 29 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20  ).**     varint 
33531 6e 53 75 66 66 69 78 3b 20 20 20 20 20 20 20 20  nSuffix;        
33532 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 75      (length of u
33533 6e 73 68 61 72 65 64 20 73 75 66 66 69 78 29 0a  nshared suffix).
33534 2a 2a 20 20 20 20 20 63 68 61 72 20 70 54 65 72  **     char pTer
33535 6d 53 75 66 66 69 78 5b 6e 53 75 66 66 69 78 5d  mSuffix[nSuffix]
33536 3b 20 28 75 6e 73 68 61 72 65 64 20 73 75 66 66  ; (unshared suff
33537 69 78 20 6f 66 20 6e 65 78 74 20 74 65 72 6d 29  ix of next term)
33538 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 7d 0a 2a 2a 0a  .**   }.** }.**.
33539 2a 2a 20 48 65 72 65 2c 20 6f 70 74 69 6f 6e 61  ** Here, optiona
3353a 6c 20 7b 20 58 20 7d 20 6d 65 61 6e 73 20 61 6e  l { X } means an
3353b 20 6f 70 74 69 6f 6e 61 6c 20 65 6c 65 6d 65 6e   optional elemen
3353c 74 2c 20 77 68 69 6c 65 20 61 72 72 61 79 20 7b  t, while array {
3353d 20 58 20 7d 0a 2a 2a 20 6d 65 61 6e 73 20 7a 65   X }.** means ze
3353e 72 6f 20 6f 72 20 6d 6f 72 65 20 6f 63 63 75 72  ro or more occur
3353f 72 65 6e 63 65 73 20 6f 66 20 58 2c 20 61 64 6a  rences of X, adj
33540 61 63 65 6e 74 20 69 6e 20 6d 65 6d 6f 72 79 2e  acent in memory.
33541 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 74 65 72 69  .**.** An interi
33542 6f 72 20 6e 6f 64 65 20 65 6e 63 6f 64 65 73 20  or node encodes 
33543 6e 20 74 65 72 6d 73 20 73 65 70 61 72 61 74 69  n terms separati
33544 6e 67 20 6e 2b 31 20 73 75 62 74 72 65 65 73 2e  ng n+1 subtrees.
33545 20 20 54 68 65 0a 2a 2a 20 73 75 62 74 72 65 65    The.** subtree
33546 20 62 6c 6f 63 6b 73 20 61 72 65 20 63 6f 6e 74   blocks are cont
33547 69 67 75 6f 75 73 2c 20 73 6f 20 6f 6e 6c 79 20  iguous, so only 
33548 74 68 65 20 66 69 72 73 74 20 73 75 62 74 72 65  the first subtre
33549 65 27 73 20 62 6c 6f 63 6b 69 64 0a 2a 2a 20 69  e's blockid.** i
3354a 73 20 65 6e 63 6f 64 65 64 2e 20 20 54 68 65 20  s encoded.  The 
3354b 73 75 62 74 72 65 65 20 61 74 20 69 42 6c 6f 63  subtree at iBloc
3354c 6b 69 64 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e  kid will contain
3354d 20 61 6c 6c 20 74 65 72 6d 73 20 6c 65 73 73 0a   all terms less.
3354e 2a 2a 20 74 68 61 6e 20 74 68 65 20 66 69 72 73  ** than the firs
3354f 74 20 74 65 72 6d 20 65 6e 63 6f 64 65 64 20 28  t term encoded (
33550 6f 72 20 61 6c 6c 20 74 65 72 6d 73 20 69 66 20  or all terms if 
33551 6e 6f 20 74 65 72 6d 20 69 73 20 65 6e 63 6f 64  no term is encod
33552 65 64 29 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ed)..** Otherwis
33553 65 2c 20 66 6f 72 20 74 65 72 6d 73 20 67 72 65  e, for terms gre
33554 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75  ater than or equ
33555 61 6c 20 74 6f 20 70 54 65 72 6d 5b 69 5d 20 62  al to pTerm[i] b
33556 75 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20  ut less.** than 
33557 70 54 65 72 6d 5b 69 2b 31 5d 2c 20 74 68 65 20  pTerm[i+1], the 
33558 73 75 62 74 72 65 65 20 66 6f 72 20 74 68 61 74  subtree for that
33559 20 74 65 72 6d 20 77 69 6c 6c 20 62 65 20 72 6f   term will be ro
3355a 6f 74 65 64 20 61 74 0a 2a 2a 20 69 42 6c 6f 63  oted at.** iBloc
3355b 6b 69 64 2b 69 2e 20 20 49 6e 74 65 72 69 6f 72  kid+i.  Interior
3355c 20 6e 6f 64 65 73 20 6f 6e 6c 79 20 73 74 6f 72   nodes only stor
3355d 65 20 65 6e 6f 75 67 68 20 74 65 72 6d 20 64 61  e enough term da
3355e 74 61 20 74 6f 0a 2a 2a 20 64 69 73 74 69 6e 67  ta to.** disting
3355f 75 69 73 68 20 61 64 6a 61 63 65 6e 74 20 63 68  uish adjacent ch
33560 69 6c 64 72 65 6e 20 28 69 66 20 74 68 65 20 72  ildren (if the r
33561 69 67 68 74 6d 6f 73 74 20 74 65 72 6d 20 6f 66  ightmost term of
33562 20 74 68 65 20 6c 65 66 74 0a 2a 2a 20 63 68 69   the left.** chi
33563 6c 64 20 69 73 20 22 73 6f 6d 65 74 68 69 6e 67  ld is "something
33564 22 2c 20 61 6e 64 20 74 68 65 20 6c 65 66 74 6d  ", and the leftm
33565 6f 73 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20  ost term of the 
33566 72 69 67 68 74 20 63 68 69 6c 64 20 69 73 0a 2a  right child is.*
33567 2a 20 22 77 69 63 6b 65 64 22 2c 20 6f 6e 6c 79  * "wicked", only
33568 20 22 77 22 20 69 73 20 73 74 6f 72 65 64 29 2e   "w" is stored).
33569 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 64 61 74 61 20  .**.** New data 
3356a 69 73 20 73 70 69 6c 6c 65 64 20 74 6f 20 61 20  is spilled to a 
3356b 6e 65 77 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  new interior nod
3356c 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 68 65  e at the same he
3356d 69 67 68 74 20 77 68 65 6e 0a 2a 2a 20 74 68 65  ight when.** the
3356e 20 63 75 72 72 65 6e 74 20 6e 6f 64 65 20 65 78   current node ex
3356f 63 65 65 64 73 20 49 4e 54 45 52 49 4f 52 5f 4d  ceeds INTERIOR_M
33570 41 58 20 62 79 74 65 73 20 28 64 65 66 61 75 6c  AX bytes (defaul
33571 74 20 32 30 34 38 29 2e 0a 2a 2a 20 49 4e 54 45  t 2048)..** INTE
33572 52 49 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 20 28  RIOR_MIN_TERMS (
33573 64 65 66 61 75 6c 74 20 37 29 20 6b 65 65 70 73  default 7) keeps
33574 20 6c 61 72 67 65 20 74 65 72 6d 73 20 66 72 6f   large terms fro
33575 6d 20 6d 6f 6e 6f 70 6f 6c 69 7a 69 6e 67 0a 2a  m monopolizing.*
33576 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73  * interior nodes
33577 20 61 6e 64 20 6d 61 6b 69 6e 67 20 74 68 65 20   and making the 
33578 74 72 65 65 20 74 6f 6f 20 73 6b 69 6e 6e 79 2e  tree too skinny.
33579 20 20 54 68 65 20 69 6e 74 65 72 69 6f 72 20 6e    The interior n
3357a 6f 64 65 73 0a 2a 2a 20 61 74 20 61 20 67 69 76  odes.** at a giv
3357b 65 6e 20 68 65 69 67 68 74 20 61 72 65 20 6e 61  en height are na
3357c 74 75 72 61 6c 6c 79 20 74 72 61 63 6b 65 64 20  turally tracked 
3357d 62 79 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  by interior node
3357e 73 20 61 74 0a 2a 2a 20 68 65 69 67 68 74 2b 31  s at.** height+1
3357f 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a  , and so on..**.
33580 2a 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74 20  **.**** Segment 
33581 64 69 72 65 63 74 6f 72 79 20 2a 2a 2a 2a 0a 2a  directory ****.*
33582 2a 20 54 68 65 20 73 65 67 6d 65 6e 74 20 64 69  * The segment di
33583 72 65 63 74 6f 72 79 20 69 6e 20 74 61 62 6c 65  rectory in table
33584 20 25 5f 73 65 67 64 69 72 20 73 74 6f 72 65 73   %_segdir stores
33585 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f   meta-informatio
33586 6e 20 66 6f 72 0a 2a 2a 20 6d 65 72 67 69 6e 67  n for.** merging
33587 20 61 6e 64 20 64 65 6c 65 74 69 6e 67 20 73 65   and deleting se
33588 67 6d 65 6e 74 73 2c 20 61 6e 64 20 61 6c 73 6f  gments, and also
33589 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6f   the root node o
3358a 66 20 74 68 65 0a 2a 2a 20 73 65 67 6d 65 6e 74  f the.** segment
3358b 27 73 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 54  's tree..**.** T
3358c 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 69 73 20  he root node is 
3358d 74 68 65 20 74 6f 70 20 6e 6f 64 65 20 6f 66 20  the top node of 
3358e 74 68 65 20 73 65 67 6d 65 6e 74 27 73 20 74 72  the segment's tr
3358f 65 65 20 61 66 74 65 72 20 65 6e 63 6f 64 69 6e  ee after encodin
33590 67 0a 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20  g.** the entire 
33591 73 65 67 6d 65 6e 74 2c 20 72 65 73 74 72 69 63  segment, restric
33592 74 65 64 20 74 6f 20 52 4f 4f 54 5f 4d 41 58 20  ted to ROOT_MAX 
33593 62 79 74 65 73 20 28 64 65 66 61 75 6c 74 20 31  bytes (default 1
33594 30 32 34 29 2e 0a 2a 2a 20 54 68 69 73 20 63 6f  024)..** This co
33595 75 6c 64 20 62 65 20 65 69 74 68 65 72 20 61 20  uld be either a 
33596 6c 65 61 66 20 6e 6f 64 65 20 6f 72 20 61 6e 20  leaf node or an 
33597 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2e 20 20  interior node.  
33598 49 66 20 74 68 65 20 74 6f 70 0a 2a 2a 20 6e 6f  If the top.** no
33599 64 65 20 72 65 71 75 69 72 65 73 20 6d 6f 72 65  de requires more
3359a 20 74 68 61 6e 20 52 4f 4f 54 5f 4d 41 58 20 62   than ROOT_MAX b
3359b 79 74 65 73 2c 20 69 74 20 69 73 20 66 6c 75 73  ytes, it is flus
3359c 68 65 64 20 74 6f 20 25 5f 73 65 67 6d 65 6e 74  hed to %_segment
3359d 73 0a 2a 2a 20 61 6e 64 20 61 20 6e 65 77 20 72  s.** and a new r
3359e 6f 6f 74 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  oot interior nod
3359f 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 28  e is generated (
335a0 77 68 69 63 68 20 73 68 6f 75 6c 64 20 61 6c 77  which should alw
335a1 61 79 73 20 66 69 74 0a 2a 2a 20 77 69 74 68 69  ays fit.** withi
335a2 6e 20 52 4f 4f 54 5f 4d 41 58 20 62 65 63 61 75  n ROOT_MAX becau
335a3 73 65 20 69 74 20 6f 6e 6c 79 20 6e 65 65 64 73  se it only needs
335a4 20 73 70 61 63 65 20 66 6f 72 20 32 20 76 61 72   space for 2 var
335a5 69 6e 74 73 2c 20 74 68 65 0a 2a 2a 20 68 65 69  ints, the.** hei
335a6 67 68 74 20 61 6e 64 20 74 68 65 20 62 6c 6f 63  ght and the bloc
335a7 6b 69 64 20 6f 66 20 74 68 65 20 70 72 65 76 69  kid of the previ
335a8 6f 75 73 20 72 6f 6f 74 29 2e 0a 2a 2a 0a 2a 2a  ous root)..**.**
335a9 20 54 68 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d   The meta-inform
335aa 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 73 65 67  ation in the seg
335ab 6d 65 6e 74 20 64 69 72 65 63 74 6f 72 79 20 69  ment directory i
335ac 73 3a 0a 2a 2a 20 20 20 6c 65 76 65 6c 20 20 20  s:.**   level   
335ad 20 20 20 20 20 20 20 20 20 20 20 20 2d 20 73 65              - se
335ae 67 6d 65 6e 74 20 6c 65 76 65 6c 20 28 73 65 65  gment level (see
335af 20 62 65 6c 6f 77 29 0a 2a 2a 20 20 20 69 64 78   below).**   idx
335b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
335b1 20 2d 20 69 6e 64 65 78 20 77 69 74 68 69 6e 20   - index within 
335b2 6c 65 76 65 6c 0a 2a 2a 20 20 20 20 20 20 20 20  level.**        
335b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d                 -
335b4 20 28 6c 65 76 65 6c 2c 69 64 78 20 75 6e 69 71   (level,idx uniq
335b5 75 65 6c 79 20 69 64 65 6e 74 69 66 79 20 61 20  uely identify a 
335b6 73 65 67 6d 65 6e 74 29 0a 2a 2a 20 20 20 73 74  segment).**   st
335b7 61 72 74 5f 62 6c 6f 63 6b 20 20 20 20 20 20 20  art_block       
335b8 20 20 2d 20 66 69 72 73 74 20 6c 65 61 66 20 6e    - first leaf n
335b9 6f 64 65 0a 2a 2a 20 20 20 6c 65 61 76 65 73 5f  ode.**   leaves_
335ba 65 6e 64 5f 62 6c 6f 63 6b 20 20 20 20 2d 20 6c  end_block    - l
335bb 61 73 74 20 6c 65 61 66 20 6e 6f 64 65 0a 2a 2a  ast leaf node.**
335bc 20 20 20 65 6e 64 5f 62 6c 6f 63 6b 20 20 20 20     end_block    
335bd 20 20 20 20 20 20 20 2d 20 6c 61 73 74 20 62 6c         - last bl
335be 6f 63 6b 20 28 69 6e 63 6c 75 64 69 6e 67 20 69  ock (including i
335bf 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 29 0a 2a  nterior nodes).*
335c0 2a 20 20 20 72 6f 6f 74 20 20 20 20 20 20 20 20  *   root        
335c1 20 20 20 20 20 20 20 20 2d 20 63 6f 6e 74 65 6e          - conten
335c2 74 73 20 6f 66 20 72 6f 6f 74 20 6e 6f 64 65 0a  ts of root node.
335c3 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 6f 6f  **.** If the roo
335c4 74 20 6e 6f 64 65 20 69 73 20 61 20 6c 65 61 66  t node is a leaf
335c5 20 6e 6f 64 65 2c 20 74 68 65 6e 20 73 74 61 72   node, then star
335c6 74 5f 62 6c 6f 63 6b 2c 0a 2a 2a 20 6c 65 61 76  t_block,.** leav
335c7 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 61 6e  es_end_block, an
335c8 64 20 65 6e 64 5f 62 6c 6f 63 6b 20 61 72 65 20  d end_block are 
335c9 61 6c 6c 20 30 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a  all 0..**.**.***
335ca 2a 20 53 65 67 6d 65 6e 74 20 6d 65 72 67 69 6e  * Segment mergin
335cb 67 20 2a 2a 2a 2a 0a 2a 2a 20 54 6f 20 61 6d 6f  g ****.** To amo
335cc 72 74 69 7a 65 20 75 70 64 61 74 65 20 63 6f 73  rtize update cos
335cd 74 73 2c 20 73 65 67 6d 65 6e 74 73 20 61 72 65  ts, segments are
335ce 20 67 72 6f 75 70 65 64 20 69 6e 74 6f 20 6c 65   grouped into le
335cf 76 65 6c 73 20 61 6e 64 0a 2a 2a 20 6d 65 72 67  vels and.** merg
335d0 65 64 20 69 6e 20 62 61 74 63 68 65 73 2e 20 20  ed in batches.  
335d1 45 61 63 68 20 69 6e 63 72 65 61 73 65 20 69 6e  Each increase in
335d2 20 6c 65 76 65 6c 20 72 65 70 72 65 73 65 6e 74   level represent
335d3 73 20 65 78 70 6f 6e 65 6e 74 69 61 6c 6c 79 0a  s exponentially.
335d4 2a 2a 20 6d 6f 72 65 20 64 6f 63 75 6d 65 6e 74  ** more document
335d5 73 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 64 6f 63  s..**.** New doc
335d6 75 6d 65 6e 74 73 20 28 61 63 74 75 61 6c 6c 79  uments (actually
335d7 2c 20 64 6f 63 75 6d 65 6e 74 20 75 70 64 61 74  , document updat
335d8 65 73 29 20 61 72 65 20 74 6f 6b 65 6e 69 7a 65  es) are tokenize
335d9 64 20 61 6e 64 0a 2a 2a 20 77 72 69 74 74 65 6e  d and.** written
335da 20 69 6e 64 69 76 69 64 75 61 6c 6c 79 20 28 75   individually (u
335db 73 69 6e 67 20 4c 65 61 66 57 72 69 74 65 72 29  sing LeafWriter)
335dc 20 74 6f 20 61 20 6c 65 76 65 6c 20 30 20 73 65   to a level 0 se
335dd 67 6d 65 6e 74 2c 20 77 69 74 68 0a 2a 2a 20 69  gment, with.** i
335de 6e 63 72 65 6d 65 6e 74 69 6e 67 20 69 64 78 2e  ncrementing idx.
335df 20 20 57 68 65 6e 20 69 64 78 20 72 65 61 63 68    When idx reach
335e0 65 73 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 28  es MERGE_COUNT (
335e1 64 65 66 61 75 6c 74 20 31 36 29 2c 20 61 6c 6c  default 16), all
335e2 0a 2a 2a 20 6c 65 76 65 6c 20 30 20 73 65 67 6d  .** level 0 segm
335e3 65 6e 74 73 20 61 72 65 20 6d 65 72 67 65 64 20  ents are merged 
335e4 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 6c 65  into a single le
335e5 76 65 6c 20 31 20 73 65 67 6d 65 6e 74 2e 20 20  vel 1 segment.  
335e6 4c 65 76 65 6c 20 31 0a 2a 2a 20 69 73 20 70 6f  Level 1.** is po
335e7 70 75 6c 61 74 65 64 20 6c 69 6b 65 20 6c 65 76  pulated like lev
335e8 65 6c 20 30 2c 20 61 6e 64 20 65 76 65 6e 74 75  el 0, and eventu
335e9 61 6c 6c 79 20 4d 45 52 47 45 5f 43 4f 55 4e 54  ally MERGE_COUNT
335ea 20 6c 65 76 65 6c 20 31 0a 2a 2a 20 73 65 67 6d   level 1.** segm
335eb 65 6e 74 73 20 61 72 65 20 6d 65 72 67 65 64 20  ents are merged 
335ec 74 6f 20 61 20 73 69 6e 67 6c 65 20 6c 65 76 65  to a single leve
335ed 6c 20 32 20 73 65 67 6d 65 6e 74 20 28 72 65 70  l 2 segment (rep
335ee 72 65 73 65 6e 74 69 6e 67 0a 2a 2a 20 4d 45 52  resenting.** MER
335ef 47 45 5f 43 4f 55 4e 54 5e 32 20 75 70 64 61 74  GE_COUNT^2 updat
335f0 65 73 29 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a  es), and so on..
335f1 2a 2a 0a 2a 2a 20 41 20 73 65 67 6d 65 6e 74 20  **.** A segment 
335f2 6d 65 72 67 65 20 74 72 61 76 65 72 73 65 73 20  merge traverses 
335f3 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20 61 74 20  all segments at 
335f4 61 20 67 69 76 65 6e 20 6c 65 76 65 6c 20 69 6e  a given level in
335f5 0a 2a 2a 20 70 61 72 61 6c 6c 65 6c 2c 20 70 65  .** parallel, pe
335f6 72 66 6f 72 6d 69 6e 67 20 61 20 73 74 72 61 69  rforming a strai
335f7 67 68 74 66 6f 72 77 61 72 64 20 73 6f 72 74 65  ghtforward sorte
335f8 64 20 6d 65 72 67 65 2e 20 20 53 69 6e 63 65 20  d merge.  Since 
335f9 73 65 67 6d 65 6e 74 0a 2a 2a 20 6c 65 61 66 20  segment.** leaf 
335fa 6e 6f 64 65 73 20 61 72 65 20 77 72 69 74 74 65  nodes are writte
335fb 6e 20 69 6e 20 74 6f 20 74 68 65 20 25 5f 73 65  n in to the %_se
335fc 67 6d 65 6e 74 73 20 74 61 62 6c 65 20 69 6e 20  gments table in 
335fd 6f 72 64 65 72 2c 20 74 68 69 73 0a 2a 2a 20 6d  order, this.** m
335fe 65 72 67 65 20 74 72 61 76 65 72 73 65 73 20 74  erge traverses t
335ff 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 73 71  he underlying sq
33600 6c 69 74 65 20 64 69 73 6b 20 73 74 72 75 63 74  lite disk struct
33601 75 72 65 73 20 65 66 66 69 63 69 65 6e 74 6c 79  ures efficiently
33602 2e 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 20 6d  ..** After the m
33603 65 72 67 65 2c 20 61 6c 6c 20 73 65 67 6d 65 6e  erge, all segmen
33604 74 20 62 6c 6f 63 6b 73 20 66 72 6f 6d 20 74 68  t blocks from th
33605 65 20 6d 65 72 67 65 64 20 6c 65 76 65 6c 20 61  e merged level a
33606 72 65 0a 2a 2a 20 64 65 6c 65 74 65 64 2e 0a 2a  re.** deleted..*
33607 2a 0a 2a 2a 20 4d 45 52 47 45 5f 43 4f 55 4e 54  *.** MERGE_COUNT
33608 20 63 6f 6e 74 72 6f 6c 73 20 68 6f 77 20 6f 66   controls how of
33609 74 65 6e 20 77 65 20 6d 65 72 67 65 20 73 65 67  ten we merge seg
3360a 6d 65 6e 74 73 2e 20 20 31 36 20 73 65 65 6d 73  ments.  16 seems
3360b 20 74 6f 20 62 65 0a 2a 2a 20 73 6f 6d 65 77 68   to be.** somewh
3360c 61 74 20 6f 66 20 61 20 73 77 65 65 74 20 73 70  at of a sweet sp
3360d 6f 74 20 66 6f 72 20 69 6e 73 65 72 74 69 6f 6e  ot for insertion
3360e 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20 20 33   performance.  3
3360f 32 20 61 6e 64 20 36 34 20 73 68 6f 77 0a 2a 2a  2 and 64 show.**
33610 20 76 65 72 79 20 73 69 6d 69 6c 61 72 20 70 65   very similar pe
33611 72 66 6f 72 6d 61 6e 63 65 20 6e 75 6d 62 65 72  rformance number
33612 73 20 74 6f 20 31 36 20 6f 6e 20 69 6e 73 65 72  s to 16 on inser
33613 74 69 6f 6e 2c 20 74 68 6f 75 67 68 20 74 68 65  tion, though the
33614 79 27 72 65 0a 2a 2a 20 61 20 74 69 6e 79 20 62  y're.** a tiny b
33615 69 74 20 73 6c 6f 77 65 72 20 28 70 65 72 68 61  it slower (perha
33616 70 73 20 64 75 65 20 74 6f 20 6d 6f 72 65 20 6f  ps due to more o
33617 76 65 72 68 65 61 64 20 69 6e 20 6d 65 72 67 65  verhead in merge
33618 2d 74 69 6d 65 0a 2a 2a 20 73 6f 72 74 69 6e 67  -time.** sorting
33619 29 2e 20 20 38 20 69 73 20 61 62 6f 75 74 20 32  ).  8 is about 2
3361a 30 25 20 73 6c 6f 77 65 72 20 74 68 61 6e 20 31  0% slower than 1
3361b 36 2c 20 34 20 61 62 6f 75 74 20 35 30 25 20 73  6, 4 about 50% s
3361c 6c 6f 77 65 72 20 74 68 61 6e 0a 2a 2a 20 31 36  lower than.** 16
3361d 2c 20 32 20 61 62 6f 75 74 20 36 36 25 20 73 6c  , 2 about 66% sl
3361e 6f 77 65 72 20 74 68 61 6e 20 31 36 2e 0a 2a 2a  ower than 16..**
3361f 0a 2a 2a 20 41 74 20 71 75 65 72 79 20 74 69 6d  .** At query tim
33620 65 2c 20 68 69 67 68 20 4d 45 52 47 45 5f 43 4f  e, high MERGE_CO
33621 55 4e 54 20 69 6e 63 72 65 61 73 65 73 20 74 68  UNT increases th
33622 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 67 6d  e number of segm
33623 65 6e 74 73 0a 2a 2a 20 77 68 69 63 68 20 6e 65  ents.** which ne
33624 65 64 20 74 6f 20 62 65 20 73 63 61 6e 6e 65 64  ed to be scanned
33625 20 61 6e 64 20 6d 65 72 67 65 64 2e 20 20 46 6f   and merged.  Fo
33626 72 20 69 6e 73 74 61 6e 63 65 2c 20 77 69 74 68  r instance, with
33627 20 31 30 30 6b 20 64 6f 63 73 0a 2a 2a 20 69 6e   100k docs.** in
33628 73 65 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20  serted:.**.**   
33629 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 20 20 73   MERGE_COUNT   s
3362a 65 67 6d 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20  egments.**      
3362b 20 31 36 20 20 20 20 20 20 20 20 20 20 20 32 35   16           25
3362c 0a 2a 2a 20 20 20 20 20 20 20 20 38 20 20 20 20  .**        8    
3362d 20 20 20 20 20 20 20 31 32 0a 2a 2a 20 20 20 20         12.**    
3362e 20 20 20 20 34 20 20 20 20 20 20 20 20 20 20 20      4           
3362f 31 30 0a 2a 2a 20 20 20 20 20 20 20 20 32 20 20  10.**        2  
33630 20 20 20 20 20 20 20 20 20 20 36 0a 2a 2a 0a 2a            6.**.*
33631 2a 20 54 68 69 73 20 61 70 70 65 61 72 73 20 74  * This appears t
33632 6f 20 68 61 76 65 20 6f 6e 6c 79 20 61 20 6d 6f  o have only a mo
33633 64 65 72 61 74 65 20 69 6d 70 61 63 74 20 6f 6e  derate impact on
33634 20 71 75 65 72 69 65 73 20 66 6f 72 20 76 65 72   queries for ver
33635 79 0a 2a 2a 20 66 72 65 71 75 65 6e 74 20 74 65  y.** frequent te
33636 72 6d 73 20 28 77 68 69 63 68 20 61 72 65 20 73  rms (which are s
33637 6f 6d 65 77 68 61 74 20 64 6f 6d 69 6e 61 74 65  omewhat dominate
33638 64 20 62 79 20 73 65 67 6d 65 6e 74 20 6d 65 72  d by segment mer
33639 67 65 0a 2a 2a 20 63 6f 73 74 73 29 2c 20 61 6e  ge.** costs), an
3363a 64 20 69 6e 66 72 65 71 75 65 6e 74 20 61 6e 64  d infrequent and
3363b 20 6e 6f 6e 2d 65 78 69 73 74 65 6e 74 20 74 65   non-existent te
3363c 72 6d 73 20 73 74 69 6c 6c 20 73 65 65 6d 20 74  rms still seem t
3363d 6f 20 62 65 20 66 61 73 74 0a 2a 2a 20 65 76 65  o be fast.** eve
3363e 6e 20 77 69 74 68 20 6d 61 6e 79 20 73 65 67 6d  n with many segm
3363f 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f  ents..**.** TODO
33640 28 73 68 65 73 73 29 20 54 68 61 74 20 73 61 69  (shess) That sai
33641 64 2c 20 69 74 20 77 6f 75 6c 64 20 62 65 20 6e  d, it would be n
33642 69 63 65 20 74 6f 20 68 61 76 65 20 61 20 62 65  ice to have a be
33643 74 74 65 72 20 71 75 65 72 79 2d 73 69 64 65 0a  tter query-side.
33644 2a 2a 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 20  ** argument for 
33645 4d 45 52 47 45 5f 43 4f 55 4e 54 20 6f 66 20 31  MERGE_COUNT of 1
33646 36 2e 20 20 41 6c 73 6f 2c 20 69 74 20 69 73 20  6.  Also, it is 
33647 70 6f 73 73 69 62 6c 65 2f 6c 69 6b 65 6c 79 20  possible/likely 
33648 74 68 61 74 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61  that.** optimiza
33649 74 69 6f 6e 73 20 74 6f 20 74 68 69 6e 67 73 20  tions to things 
3364a 6c 69 6b 65 20 64 6f 63 6c 69 73 74 20 6d 65 72  like doclist mer
3364b 67 69 6e 67 20 77 69 6c 6c 20 73 77 69 6e 67 20  ging will swing 
3364c 74 68 65 20 73 77 65 65 74 0a 2a 2a 20 73 70 6f  the sweet.** spo
3364d 74 20 61 72 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 0a  t around..**.**.
3364e 2a 2a 0a 2a 2a 2a 2a 20 48 61 6e 64 6c 69 6e 67  **.**** Handling
3364f 20 6f 66 20 64 65 6c 65 74 69 6f 6e 73 20 61 6e   of deletions an
33650 64 20 75 70 64 61 74 65 73 20 2a 2a 2a 2a 0a 2a  d updates ****.*
33651 2a 20 53 69 6e 63 65 20 77 65 27 72 65 20 75 73  * Since we're us
33652 69 6e 67 20 61 20 73 65 67 6d 65 6e 74 65 64 20  ing a segmented 
33653 73 74 72 75 63 74 75 72 65 2c 20 77 69 74 68 20  structure, with 
33654 6e 6f 20 64 6f 63 69 64 2d 6f 72 69 65 6e 74 65  no docid-oriente
33655 64 0a 2a 2a 20 69 6e 64 65 78 20 69 6e 74 6f 20  d.** index into 
33656 74 68 65 20 74 65 72 6d 20 69 6e 64 65 78 2c 20  the term index, 
33657 77 65 20 63 6c 65 61 72 6c 79 20 63 61 6e 6e 6f  we clearly canno
33658 74 20 73 69 6d 70 6c 79 20 75 70 64 61 74 65 20  t simply update 
33659 74 68 65 20 74 65 72 6d 0a 2a 2a 20 69 6e 64 65  the term.** inde
3365a 78 20 77 68 65 6e 20 61 20 64 6f 63 75 6d 65 6e  x when a documen
3365b 74 20 69 73 20 64 65 6c 65 74 65 64 20 6f 72 20  t is deleted or 
3365c 75 70 64 61 74 65 64 2e 20 20 46 6f 72 20 64 65  updated.  For de
3365d 6c 65 74 69 6f 6e 73 2c 20 77 65 0a 2a 2a 20 77  letions, we.** w
3365e 72 69 74 65 20 61 6e 20 65 6d 70 74 79 20 64 6f  rite an empty do
3365f 63 6c 69 73 74 20 28 76 61 72 69 6e 74 28 64 6f  clist (varint(do
33660 63 69 64 29 20 76 61 72 69 6e 74 28 50 4f 53 5f  cid) varint(POS_
33661 45 4e 44 29 29 2c 20 66 6f 72 20 75 70 64 61 74  END)), for updat
33662 65 73 0a 2a 2a 20 77 65 20 73 69 6d 70 6c 79 20  es.** we simply 
33663 77 72 69 74 65 20 74 68 65 20 6e 65 77 20 64 6f  write the new do
33664 63 6c 69 73 74 2e 20 20 53 65 67 6d 65 6e 74 20  clist.  Segment 
33665 6d 65 72 67 65 73 20 6f 76 65 72 77 72 69 74 65  merges overwrite
33666 20 6f 6c 64 65 72 0a 2a 2a 20 64 61 74 61 20 66   older.** data f
33667 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  or a particular 
33668 64 6f 63 69 64 20 77 69 74 68 20 6e 65 77 65 72  docid with newer
33669 20 64 61 74 61 2c 20 73 6f 20 64 65 6c 65 74 65   data, so delete
3366a 73 20 6f 72 20 75 70 64 61 74 65 73 0a 2a 2a 20  s or updates.** 
3366b 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 20  will eventually 
3366c 6f 76 65 72 74 61 6b 65 20 74 68 65 20 65 61 72  overtake the ear
3366d 6c 69 65 72 20 64 61 74 61 20 61 6e 64 20 6b 6e  lier data and kn
3366e 6f 63 6b 20 69 74 20 6f 75 74 2e 20 20 54 68 65  ock it out.  The
3366f 0a 2a 2a 20 71 75 65 72 79 20 6c 6f 67 69 63 20  .** query logic 
33670 6c 69 6b 65 77 69 73 65 20 6d 65 72 67 65 73 20  likewise merges 
33671 64 6f 63 6c 69 73 74 73 20 73 6f 20 74 68 61 74  doclists so that
33672 20 6e 65 77 65 72 20 64 61 74 61 20 6b 6e 6f 63   newer data knoc
33673 6b 73 20 6f 75 74 0a 2a 2a 20 6f 6c 64 65 72 20  ks out.** older 
33674 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f  data..**.** TODO
33675 28 73 68 65 73 73 29 20 50 72 6f 76 69 64 65 20  (shess) Provide 
33676 61 20 56 41 43 55 55 4d 20 74 79 70 65 20 6f 70  a VACUUM type op
33677 65 72 61 74 69 6f 6e 20 74 6f 20 63 6c 65 61 72  eration to clear
33678 20 6f 75 74 20 61 6c 6c 0a 2a 2a 20 64 65 6c 65   out all.** dele
33679 74 69 6f 6e 73 20 61 6e 64 20 64 75 70 6c 69 63  tions and duplic
3367a 61 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 77 6f  ations.  This wo
3367b 75 6c 64 20 62 61 73 69 63 61 6c 6c 79 20 62 65  uld basically be
3367c 20 61 20 66 6f 72 63 65 64 20 6d 65 72 67 65 0a   a forced merge.
3367d 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65  ** into a single
3367e 20 73 65 67 6d 65 6e 74 2e 0a 2a 2f 0a 0a 23 69   segment..*/..#i
3367f 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
33680 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
33681 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
33682 5f 46 54 53 33 29 0a 0a 23 69 66 20 64 65 66 69  _FTS3)..#if defi
33683 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
33684 45 5f 46 54 53 33 29 20 26 26 20 21 64 65 66 69  E_FTS3) && !defi
33685 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29  ned(SQLITE_CORE)
33686 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
33687 5f 43 4f 52 45 20 31 0a 23 65 6e 64 69 66 0a 0a  _CORE 1.#endif..
33688 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
33689 20 49 6e 63 6c 75 64 65 20 66 74 73 33 5f 65 78   Include fts3_ex
3368a 70 72 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  pr.h in the midd
3368b 6c 65 20 6f 66 20 66 74 73 33 2e 63 20 2a 2a 2a  le of fts3.c ***
3368c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
3368d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3368e 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33   Begin file fts3
3368f 5f 65 78 70 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  _expr.h ********
33690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33691 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
33692 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4e 6f 76 20  ./*.** 2008 Nov 
33693 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  28.**.** The aut
33694 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
33695 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
33696 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
33697 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
33698 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
33699 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
3369a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
3369b 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
3369c 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
3369d 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
3369e 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
3369f 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
336a0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
336a1 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
336a2 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
336a3 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
336a4 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
336a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
336a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
336a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
336a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
336a9 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a  ******.**.*/../*
336aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
336ab 63 6c 75 64 65 20 66 74 73 33 5f 74 6f 6b 65 6e  clude fts3_token
336ac 69 7a 65 72 2e 68 20 69 6e 20 74 68 65 20 6d 69  izer.h in the mi
336ad 64 64 6c 65 20 6f 66 20 66 74 73 33 5f 65 78 70  ddle of fts3_exp
336ae 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  r.h ********/./*
336af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
336b0 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 74 6f  gin file fts3_to
336b1 6b 65 6e 69 7a 65 72 2e 68 20 2a 2a 2a 2a 2a 2a  kenizer.h ******
336b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
336b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
336b4 0a 2a 2a 20 32 30 30 36 20 4a 75 6c 79 20 31 30  .** 2006 July 10
336b5 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
336b6 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
336b7 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
336b8 75 72 63 65 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a  urce code..**.**
336b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
336ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
336bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
336bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
336bd 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 44 65 66 69 6e  *******.** Defin
336be 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65  es the interface
336bf 20 74 6f 20 74 6f 6b 65 6e 69 7a 65 72 73 20 75   to tokenizers u
336c0 73 65 64 20 62 79 20 66 75 6c 6c 74 65 78 74 2d  sed by fulltext-
336c1 73 65 61 72 63 68 2e 20 20 54 68 65 72 65 0a 2a  search.  There.*
336c2 2a 20 61 72 65 20 74 68 72 65 65 20 62 61 73 69  * are three basi
336c3 63 20 63 6f 6d 70 6f 6e 65 6e 74 73 3a 0a 2a 2a  c components:.**
336c4 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  .** sqlite3_toke
336c5 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 69 73 20  nizer_module is 
336c6 61 20 73 69 6e 67 6c 65 74 6f 6e 20 64 65 66 69  a singleton defi
336c7 6e 69 6e 67 20 74 68 65 20 74 6f 6b 65 6e 69 7a  ning the tokeniz
336c8 65 72 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20  er.** interface 
336c9 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69 73  functions.  This
336ca 20 69 73 20 65 73 73 65 6e 74 69 61 6c 6c 79 20   is essentially 
336cb 74 68 65 20 63 6c 61 73 73 20 73 74 72 75 63 74  the class struct
336cc 75 72 65 20 66 6f 72 0a 2a 2a 20 74 6f 6b 65 6e  ure for.** token
336cd 69 7a 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c  izers..**.** sql
336ce 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 69  ite3_tokenizer i
336cf 73 20 75 73 65 64 20 74 6f 20 64 65 66 69 6e 65  s used to define
336d0 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 6f   a particular to
336d1 6b 65 6e 69 7a 65 72 2c 20 70 65 72 68 61 70 73  kenizer, perhaps
336d2 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 75  .** including cu
336d3 73 74 6f 6d 69 7a 61 74 69 6f 6e 20 69 6e 66 6f  stomization info
336d4 72 6d 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20  rmation defined 
336d5 61 74 20 63 72 65 61 74 69 6f 6e 20 74 69 6d 65  at creation time
336d6 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ..**.** sqlite3_
336d7 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
336d8 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 79   is generated by
336d9 20 61 20 74 6f 6b 65 6e 69 7a 65 72 20 74 6f 20   a tokenizer to 
336da 67 65 6e 65 72 61 74 65 0a 2a 2a 20 74 6f 6b 65  generate.** toke
336db 6e 73 20 66 72 6f 6d 20 61 20 70 61 72 74 69 63  ns from a partic
336dc 75 6c 61 72 20 69 6e 70 75 74 2e 0a 2a 2f 0a 23  ular input..*/.#
336dd 69 66 6e 64 65 66 20 5f 46 54 53 33 5f 54 4f 4b  ifndef _FTS3_TOK
336de 45 4e 49 5a 45 52 5f 48 5f 0a 23 64 65 66 69 6e  ENIZER_H_.#defin
336df 65 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49 5a 45  e _FTS3_TOKENIZE
336e0 52 5f 48 5f 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68  R_H_../* TODO(sh
336e1 65 73 73 29 20 4f 6e 6c 79 20 75 73 65 64 20 66  ess) Only used f
336e2 6f 72 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64  or SQLITE_OK and
336e3 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 61 74 20   SQLITE_DONE at 
336e4 74 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 20 49 66  this time..** If
336e5 20 74 6f 6b 65 6e 69 7a 65 72 73 20 61 72 65 20   tokenizers are 
336e6 74 6f 20 62 65 20 61 6c 6c 6f 77 65 64 20 74 6f  to be allowed to
336e7 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 2a 28   call sqlite3_*(
336e8 29 20 66 75 6e 63 74 69 6f 6e 73 2c 20 74 68 65  ) functions, the
336e9 6e 0a 2a 2a 20 77 65 20 77 69 6c 6c 20 6e 65 65  n.** we will nee
336ea 64 20 61 20 77 61 79 20 74 6f 20 72 65 67 69 73  d a way to regis
336eb 74 65 72 20 74 68 65 20 41 50 49 20 63 6f 6e 73  ter the API cons
336ec 69 73 74 65 6e 74 6c 79 2e 0a 2a 2f 0a 0a 2f 2a  istently..*/../*
336ed 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 73 20 75  .** Structures u
336ee 73 65 64 20 62 79 20 74 68 65 20 74 6f 6b 65 6e  sed by the token
336ef 69 7a 65 72 20 69 6e 74 65 72 66 61 63 65 2e 20  izer interface. 
336f0 57 68 65 6e 20 61 20 6e 65 77 20 74 6f 6b 65 6e  When a new token
336f1 69 7a 65 72 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  izer.** implemen
336f2 74 61 74 69 6f 6e 20 69 73 20 72 65 67 69 73 74  tation is regist
336f3 65 72 65 64 2c 20 74 68 65 20 63 61 6c 6c 65 72  ered, the caller
336f4 20 70 72 6f 76 69 64 65 73 20 61 20 70 6f 69 6e   provides a poin
336f5 74 65 72 20 74 6f 0a 2a 2a 20 61 6e 20 73 71 6c  ter to.** an sql
336f6 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
336f7 6f 64 75 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67  odule containing
336f8 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65   pointers to the
336f9 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e   callback.** fun
336fa 63 74 69 6f 6e 73 20 74 68 61 74 20 6d 61 6b 65  ctions that make
336fb 20 75 70 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74   up an implement
336fc 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  ation..**.** Whe
336fd 6e 20 61 6e 20 66 74 73 33 20 74 61 62 6c 65 20  n an fts3 table 
336fe 69 73 20 63 72 65 61 74 65 64 2c 20 69 74 20 70  is created, it p
336ff 61 73 73 65 73 20 61 6e 79 20 61 72 67 75 6d 65  asses any argume
33700 6e 74 73 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a  nts passed to.**
33701 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 63   the tokenizer c
33702 6c 61 75 73 65 20 6f 66 20 74 68 65 20 43 52 45  lause of the CRE
33703 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c  ATE VIRTUAL TABL
33704 45 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 74  E statement to t
33705 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f  he.** sqlite3_to
33706 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 2e 78  kenizer_module.x
33707 43 72 65 61 74 65 28 29 20 66 75 6e 63 74 69 6f  Create() functio
33708 6e 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74  n of the request
33709 65 64 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20  ed tokenizer.** 
3370a 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20  implementation. 
3370b 54 68 65 20 78 43 72 65 61 74 65 28 29 20 66 75  The xCreate() fu
3370c 6e 63 74 69 6f 6e 20 69 6e 20 74 75 72 6e 20 72  nction in turn r
3370d 65 74 75 72 6e 73 20 61 6e 20 0a 2a 2a 20 73 71  eturns an .** sq
3370e 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
3370f 73 74 72 75 63 74 75 72 65 20 72 65 70 72 65 73  structure repres
33710 65 6e 74 69 6e 67 20 74 68 65 20 73 70 65 63 69  enting the speci
33711 66 69 63 20 74 6f 6b 65 6e 69 7a 65 72 20 74 6f  fic tokenizer to
33712 0a 2a 2a 20 62 65 20 75 73 65 64 20 66 6f 72 20  .** be used for 
33713 74 68 65 20 66 74 73 33 20 74 61 62 6c 65 20 28  the fts3 table (
33714 63 75 73 74 6f 6d 69 7a 65 64 20 62 79 20 74 68  customized by th
33715 65 20 74 6f 6b 65 6e 69 7a 65 72 20 63 6c 61 75  e tokenizer clau
33716 73 65 20 61 72 67 75 6d 65 6e 74 73 29 2e 0a 2a  se arguments)..*
33717 2a 0a 2a 2a 20 54 6f 20 74 6f 6b 65 6e 69 7a 65  *.** To tokenize
33718 20 61 6e 20 69 6e 70 75 74 20 62 75 66 66 65 72   an input buffer
33719 2c 20 74 68 65 20 73 71 6c 69 74 65 33 5f 74 6f  , the sqlite3_to
3371a 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 2e 78  kenizer_module.x
3371b 4f 70 65 6e 28 29 0a 2a 2a 20 6d 65 74 68 6f 64  Open().** method
3371c 20 69 73 20 63 61 6c 6c 65 64 2e 20 49 74 20 72   is called. It r
3371d 65 74 75 72 6e 73 20 61 6e 20 73 71 6c 69 74 65  eturns an sqlite
3371e 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
3371f 6f 72 20 6f 62 6a 65 63 74 0a 2a 2a 20 74 68 61  or object.** tha
33720 74 20 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f  t may be used to
33721 20 74 6f 6b 65 6e 69 7a 65 20 61 20 73 70 65 63   tokenize a spec
33722 69 66 69 63 20 69 6e 70 75 74 20 62 75 66 66 65  ific input buffe
33723 72 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 74 68  r based on.** th
33724 65 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 72  e tokenization r
33725 75 6c 65 73 20 73 75 70 70 6c 69 65 64 20 62 79  ules supplied by
33726 20 61 20 73 70 65 63 69 66 69 63 20 73 71 6c 69   a specific sqli
33727 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a  te3_tokenizer.**
33728 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 74 79 70 65   object..*/.type
33729 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
3372a 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
3372b 75 6c 65 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ule sqlite3_toke
3372c 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 3b 0a 74 79  nizer_module;.ty
3372d 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
3372e 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 73  ite3_tokenizer s
3372f 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
33730 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
33731 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
33732 65 72 5f 63 75 72 73 6f 72 20 73 71 6c 69 74 65  er_cursor sqlite
33733 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
33734 6f 72 3b 0a 0a 73 74 72 75 63 74 20 73 71 6c 69  or;..struct sqli
33735 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
33736 64 75 6c 65 20 7b 0a 0a 20 20 2f 2a 0a 20 20 2a  dule {..  /*.  *
33737 2a 20 53 74 72 75 63 74 75 72 65 20 76 65 72 73  * Structure vers
33738 69 6f 6e 2e 20 53 68 6f 75 6c 64 20 61 6c 77 61  ion. Should alwa
33739 79 73 20 62 65 20 73 65 74 20 74 6f 20 30 2e 0a  ys be set to 0..
3373a 20 20 2a 2f 0a 20 20 69 6e 74 20 69 56 65 72 73    */.  int iVers
3373b 69 6f 6e 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  ion;..  /*.  ** 
3373c 43 72 65 61 74 65 20 61 20 6e 65 77 20 74 6f 6b  Create a new tok
3373d 65 6e 69 7a 65 72 2e 20 54 68 65 20 76 61 6c 75  enizer. The valu
3373e 65 73 20 69 6e 20 74 68 65 20 61 72 67 76 5b 5d  es in the argv[]
3373f 20 61 72 72 61 79 20 61 72 65 20 74 68 65 0a 20   array are the. 
33740 20 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 70 61   ** arguments pa
33741 73 73 65 64 20 74 6f 20 74 68 65 20 22 74 6f 6b  ssed to the "tok
33742 65 6e 69 7a 65 72 22 20 63 6c 61 75 73 65 20 6f  enizer" clause o
33743 66 20 74 68 65 20 43 52 45 41 54 45 20 56 49 52  f the CREATE VIR
33744 54 55 41 4c 0a 20 20 2a 2a 20 54 41 42 4c 45 20  TUAL.  ** TABLE 
33745 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 63  statement that c
33746 72 65 61 74 65 64 20 74 68 65 20 66 74 73 33 20  reated the fts3 
33747 74 61 62 6c 65 2e 20 46 6f 72 20 65 78 61 6d 70  table. For examp
33748 6c 65 2c 20 69 66 0a 20 20 2a 2a 20 74 68 65 20  le, if.  ** the 
33749 66 6f 6c 6c 6f 77 69 6e 67 20 53 51 4c 20 69 73  following SQL is
3374a 20 65 78 65 63 75 74 65 64 3a 0a 20 20 2a 2a 0a   executed:.  **.
3374b 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20 2e 2e    **   CREATE ..
3374c 20 55 53 49 4e 47 20 66 74 73 33 28 20 2e 2e 2e   USING fts3( ...
3374d 20 2c 20 74 6f 6b 65 6e 69 7a 65 72 20 3c 74 6f   , tokenizer <to
3374e 6b 65 6e 69 7a 65 72 2d 6e 61 6d 65 3e 20 61 72  kenizer-name> ar
3374f 67 31 20 61 72 67 32 29 0a 20 20 2a 2a 0a 20 20  g1 arg2).  **.  
33750 2a 2a 20 74 68 65 6e 20 61 72 67 63 20 69 73 20  ** then argc is 
33751 73 65 74 20 74 6f 20 32 2c 20 61 6e 64 20 74 68  set to 2, and th
33752 65 20 61 72 67 76 5b 5d 20 61 72 72 61 79 20 63  e argv[] array c
33753 6f 6e 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 73  ontains pointers
33754 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 73 74 72  .  ** to the str
33755 69 6e 67 73 20 22 61 72 67 31 22 20 61 6e 64 20  ings "arg1" and 
33756 22 61 72 67 32 22 2e 0a 20 20 2a 2a 0a 20 20 2a  "arg2"..  **.  *
33757 2a 20 54 68 69 73 20 6d 65 74 68 6f 64 20 73 68  * This method sh
33758 6f 75 6c 64 20 72 65 74 75 72 6e 20 65 69 74 68  ould return eith
33759 65 72 20 53 51 4c 49 54 45 5f 4f 4b 20 28 30 29  er SQLITE_OK (0)
3375a 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20 65  , or an SQLite e
3375b 72 72 6f 72 20 0a 20 20 2a 2a 20 63 6f 64 65 2e  rror .  ** code.
3375c 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73   If SQLITE_OK is
3375d 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20   returned, then 
3375e 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20 73 68 6f  *ppTokenizer sho
3375f 75 6c 64 20 62 65 20 73 65 74 0a 20 20 2a 2a 20  uld be set.  ** 
33760 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20  to point at the 
33761 6e 65 77 6c 79 20 63 72 65 61 74 65 64 20 74 6f  newly created to
33762 6b 65 6e 69 7a 65 72 20 73 74 72 75 63 74 75 72  kenizer structur
33763 65 2e 20 54 68 65 20 67 65 6e 65 72 69 63 0a 20  e. The generic. 
33764 20 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65   ** sqlite3_toke
33765 6e 69 7a 65 72 2e 70 4d 6f 64 75 6c 65 20 76 61  nizer.pModule va
33766 72 69 61 62 6c 65 20 73 68 6f 75 6c 64 20 6e 6f  riable should no
33767 74 20 62 65 20 69 6e 69 74 69 61 6c 69 73 65 64  t be initialised
33768 20 62 79 0a 20 20 2a 2a 20 74 68 69 73 20 63 61   by.  ** this ca
33769 6c 6c 62 61 63 6b 2e 20 54 68 65 20 63 61 6c 6c  llback. The call
3376a 65 72 20 77 69 6c 6c 20 64 6f 20 73 6f 2e 0a 20  er will do so.. 
3376b 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 72 65   */.  int (*xCre
3376c 61 74 65 29 28 0a 20 20 20 20 69 6e 74 20 61 72  ate)(.    int ar
3376d 67 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  gc,             
3376e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3376f 20 53 69 7a 65 20 6f 66 20 61 72 67 76 20 61 72   Size of argv ar
33770 72 61 79 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74  ray */.    const
33771 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67   char *const*arg
33772 76 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  v,             /
33773 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 61 72 67 75  * Tokenizer argu
33774 6d 65 6e 74 20 73 74 72 69 6e 67 73 20 2a 2f 0a  ment strings */.
33775 20 20 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65      sqlite3_toke
33776 6e 69 7a 65 72 20 2a 2a 70 70 54 6f 6b 65 6e 69  nizer **ppTokeni
33777 7a 65 72 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20  zer     /* OUT: 
33778 43 72 65 61 74 65 64 20 74 6f 6b 65 6e 69 7a 65  Created tokenize
33779 72 20 2a 2f 0a 20 20 29 3b 0a 0a 20 20 2f 2a 0a  r */.  );..  /*.
3377a 20 20 2a 2a 20 44 65 73 74 72 6f 79 20 61 6e 20    ** Destroy an 
3377b 65 78 69 73 74 69 6e 67 20 74 6f 6b 65 6e 69 7a  existing tokeniz
3377c 65 72 2e 20 54 68 65 20 66 74 73 33 20 6d 6f 64  er. The fts3 mod
3377d 75 6c 65 20 63 61 6c 6c 73 20 74 68 69 73 20 6d  ule calls this m
3377e 65 74 68 6f 64 0a 20 20 2a 2a 20 65 78 61 63 74  ethod.  ** exact
3377f 6c 79 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ly once for each
33780 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c   successful call
33781 20 74 6f 20 78 43 72 65 61 74 65 28 29 2e 0a 20   to xCreate().. 
33782 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 44 65 73   */.  int (*xDes
33783 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f 74 6f  troy)(sqlite3_to
33784 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69  kenizer *pTokeni
33785 7a 65 72 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  zer);..  /*.  **
33786 20 43 72 65 61 74 65 20 61 20 74 6f 6b 65 6e 69   Create a tokeni
33787 7a 65 72 20 63 75 72 73 6f 72 20 74 6f 20 74 6f  zer cursor to to
33788 6b 65 6e 69 7a 65 20 61 6e 20 69 6e 70 75 74 20  kenize an input 
33789 62 75 66 66 65 72 2e 20 54 68 65 20 63 61 6c 6c  buffer. The call
3378a 65 72 0a 20 20 2a 2a 20 69 73 20 72 65 73 70 6f  er.  ** is respo
3378b 6e 73 69 62 6c 65 20 66 6f 72 20 65 6e 73 75 72  nsible for ensur
3378c 69 6e 67 20 74 68 61 74 20 74 68 65 20 69 6e 70  ing that the inp
3378d 75 74 20 62 75 66 66 65 72 20 72 65 6d 61 69 6e  ut buffer remain
3378e 73 20 76 61 6c 69 64 0a 20 20 2a 2a 20 75 6e 74  s valid.  ** unt
3378f 69 6c 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  il the cursor is
33790 20 63 6c 6f 73 65 64 20 28 75 73 69 6e 67 20 74   closed (using t
33791 68 65 20 78 43 6c 6f 73 65 28 29 20 6d 65 74 68  he xClose() meth
33792 6f 64 29 2e 20 0a 20 20 2a 2f 0a 20 20 69 6e 74  od). .  */.  int
33793 20 28 2a 78 4f 70 65 6e 29 28 0a 20 20 20 20 73   (*xOpen)(.    s
33794 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
33795 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 20 20   *pTokenizer,   
33796 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72      /* Tokenizer
33797 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 63   object */.    c
33798 6f 6e 73 74 20 63 68 61 72 20 2a 70 49 6e 70 75  onst char *pInpu
33799 74 2c 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20  t, int nBytes,  
3379a 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 62 75 66      /* Input buf
3379b 66 65 72 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  fer */.    sqlit
3379c 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  e3_tokenizer_cur
3379d 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20  sor **ppCursor  
3379e 2f 2a 20 4f 55 54 3a 20 43 72 65 61 74 65 64 20  /* OUT: Created 
3379f 74 6f 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72  tokenizer cursor
337a0 20 2a 2f 0a 20 20 29 3b 0a 0a 20 20 2f 2a 0a 20   */.  );..  /*. 
337a1 20 2a 2a 20 44 65 73 74 72 6f 79 20 61 6e 20 65   ** Destroy an e
337a2 78 69 73 74 69 6e 67 20 74 6f 6b 65 6e 69 7a 65  xisting tokenize
337a3 72 20 63 75 72 73 6f 72 2e 20 54 68 65 20 66 74  r cursor. The ft
337a4 73 33 20 6d 6f 64 75 6c 65 20 63 61 6c 6c 73 20  s3 module calls 
337a5 74 68 69 73 20 0a 20 20 2a 2a 20 6d 65 74 68 6f  this .  ** metho
337a6 64 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 20 66  d exactly once f
337a7 6f 72 20 65 61 63 68 20 73 75 63 63 65 73 73 66  or each successf
337a8 75 6c 20 63 61 6c 6c 20 74 6f 20 78 4f 70 65 6e  ul call to xOpen
337a9 28 29 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28  ()..  */.  int (
337aa 2a 78 43 6c 6f 73 65 29 28 73 71 6c 69 74 65 33  *xClose)(sqlite3
337ab 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
337ac 72 20 2a 70 43 75 72 73 6f 72 29 3b 0a 0a 20 20  r *pCursor);..  
337ad 2f 2a 0a 20 20 2a 2a 20 52 65 74 72 69 65 76 65  /*.  ** Retrieve
337ae 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20   the next token 
337af 66 72 6f 6d 20 74 68 65 20 74 6f 6b 65 6e 69 7a  from the tokeniz
337b0 65 72 20 63 75 72 73 6f 72 20 70 43 75 72 73 6f  er cursor pCurso
337b1 72 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6d 65 74  r. This.  ** met
337b2 68 6f 64 20 73 68 6f 75 6c 64 20 65 69 74 68 65  hod should eithe
337b3 72 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  r return SQLITE_
337b4 4f 4b 20 61 6e 64 20 73 65 74 20 74 68 65 20 76  OK and set the v
337b5 61 6c 75 65 73 20 6f 66 20 74 68 65 0a 20 20 2a  alues of the.  *
337b6 2a 20 22 4f 55 54 22 20 76 61 72 69 61 62 6c 65  * "OUT" variable
337b7 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 65 6c  s identified bel
337b8 6f 77 2c 20 6f 72 20 53 51 4c 49 54 45 5f 44 4f  ow, or SQLITE_DO
337b9 4e 45 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74  NE to indicate t
337ba 68 61 74 0a 20 20 2a 2a 20 74 68 65 20 65 6e 64  hat.  ** the end
337bb 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 68   of the buffer h
337bc 61 73 20 62 65 65 6e 20 72 65 61 63 68 65 64 2c  as been reached,
337bd 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20 65 72   or an SQLite er
337be 72 6f 72 20 63 6f 64 65 2e 0a 20 20 2a 2a 0a 20  ror code..  **. 
337bf 20 2a 2a 20 2a 70 70 54 6f 6b 65 6e 20 73 68 6f   ** *ppToken sho
337c0 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20 70 6f  uld be set to po
337c1 69 6e 74 20 61 74 20 61 20 62 75 66 66 65 72 20  int at a buffer 
337c2 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 0a  containing the .
337c3 20 20 2a 2a 20 6e 6f 72 6d 61 6c 69 7a 65 64 20    ** normalized 
337c4 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 74  version of the t
337c5 6f 6b 65 6e 20 28 69 2e 65 2e 20 61 66 74 65 72  oken (i.e. after
337c6 20 61 6e 79 20 63 61 73 65 2d 66 6f 6c 64 69 6e   any case-foldin
337c7 67 20 61 6e 64 2f 6f 72 0a 20 20 2a 2a 20 73 74  g and/or.  ** st
337c8 65 6d 6d 69 6e 67 20 68 61 73 20 62 65 65 6e 20  emming has been 
337c9 70 65 72 66 6f 72 6d 65 64 29 2e 20 2a 70 6e 42  performed). *pnB
337ca 79 74 65 73 20 73 68 6f 75 6c 64 20 62 65 20 73  ytes should be s
337cb 65 74 20 74 6f 20 74 68 65 20 6c 65 6e 67 74 68  et to the length
337cc 0a 20 20 2a 2a 20 6f 66 20 74 68 69 73 20 62 75  .  ** of this bu
337cd 66 66 65 72 20 69 6e 20 62 79 74 65 73 2e 20 54  ffer in bytes. T
337ce 68 65 20 69 6e 70 75 74 20 74 65 78 74 20 74 68  he input text th
337cf 61 74 20 67 65 6e 65 72 61 74 65 64 20 74 68 65  at generated the
337d0 20 74 6f 6b 65 6e 20 69 73 0a 20 20 2a 2a 20 69   token is.  ** i
337d1 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65  dentified by the
337d2 20 62 79 74 65 20 6f 66 66 73 65 74 73 20 72 65   byte offsets re
337d3 74 75 72 6e 65 64 20 69 6e 20 2a 70 69 53 74 61  turned in *piSta
337d4 72 74 4f 66 66 73 65 74 20 61 6e 64 0a 20 20 2a  rtOffset and.  *
337d5 2a 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 2e 20  * *piEndOffset. 
337d6 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74 20 73  *piStartOffset s
337d7 68 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20  hould be set to 
337d8 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
337d9 20 66 69 72 73 74 0a 20 20 2a 2a 20 62 79 74 65   first.  ** byte
337da 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 6e   of the token in
337db 20 74 68 65 20 69 6e 70 75 74 20 62 75 66 66 65   the input buffe
337dc 72 2e 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 20  r. *piEndOffset 
337dd 73 68 6f 75 6c 64 20 62 65 20 73 65 74 0a 20 20  should be set.  
337de 2a 2a 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20  ** to the index 
337df 6f 66 20 74 68 65 20 66 69 72 73 74 20 62 79 74  of the first byt
337e0 65 20 6a 75 73 74 20 70 61 73 74 20 74 68 65 20  e just past the 
337e1 65 6e 64 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  end of the token
337e2 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20 69 6e 70   in.  ** the inp
337e3 75 74 20 62 75 66 66 65 72 2e 0a 20 20 2a 2a 0a  ut buffer..  **.
337e4 20 20 2a 2a 20 54 68 65 20 62 75 66 66 65 72 20    ** The buffer 
337e5 2a 70 70 54 6f 6b 65 6e 20 69 73 20 73 65 74 20  *ppToken is set 
337e6 74 6f 20 70 6f 69 6e 74 20 61 74 20 69 73 20 6d  to point at is m
337e7 61 6e 61 67 65 64 20 62 79 20 74 68 65 20 74 6f  anaged by the to
337e8 6b 65 6e 69 7a 65 72 0a 20 20 2a 2a 20 69 6d 70  kenizer.  ** imp
337e9 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 49 74 20  lementation. It 
337ea 69 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64  is only required
337eb 20 74 6f 20 62 65 20 76 61 6c 69 64 20 75 6e 74   to be valid unt
337ec 69 6c 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c  il the next call
337ed 0a 20 20 2a 2a 20 74 6f 20 78 4e 65 78 74 28 29  .  ** to xNext()
337ee 20 6f 72 20 78 43 6c 6f 73 65 28 29 2e 20 0a 20   or xClose(). . 
337ef 20 2a 2f 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68   */.  /* TODO(sh
337f0 65 73 73 29 20 63 75 72 72 65 6e 74 20 69 6d 70  ess) current imp
337f1 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 72 65 71 75  lementation requ
337f2 69 72 65 73 20 70 49 6e 70 75 74 20 74 6f 20 62  ires pInput to b
337f3 65 0a 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69  e.  ** nul-termi
337f4 6e 61 74 65 64 2e 20 20 54 68 69 73 20 73 68 6f  nated.  This sho
337f5 75 6c 64 20 65 69 74 68 65 72 20 62 65 20 66 69  uld either be fi
337f6 78 65 64 2c 20 6f 72 20 70 49 6e 70 75 74 2f 6e  xed, or pInput/n
337f7 42 79 74 65 73 0a 20 20 2a 2a 20 73 68 6f 75 6c  Bytes.  ** shoul
337f8 64 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74  d be converted t
337f9 6f 20 7a 49 6e 70 75 74 2e 0a 20 20 2a 2f 0a 20  o zInput..  */. 
337fa 20 69 6e 74 20 28 2a 78 4e 65 78 74 29 28 0a 20   int (*xNext)(. 
337fb 20 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e     sqlite3_token
337fc 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75  izer_cursor *pCu
337fd 72 73 6f 72 2c 20 20 20 2f 2a 20 54 6f 6b 65 6e  rsor,   /* Token
337fe 69 7a 65 72 20 63 75 72 73 6f 72 20 2a 2f 0a 20  izer cursor */. 
337ff 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a     const char **
33800 70 70 54 6f 6b 65 6e 2c 20 69 6e 74 20 2a 70 6e  ppToken, int *pn
33801 42 79 74 65 73 2c 20 20 2f 2a 20 4f 55 54 3a 20  Bytes,  /* OUT: 
33802 4e 6f 72 6d 61 6c 69 7a 65 64 20 74 65 78 74 20  Normalized text 
33803 66 6f 72 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20  for token */.   
33804 20 69 6e 74 20 2a 70 69 53 74 61 72 74 4f 66 66   int *piStartOff
33805 73 65 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 42 79  set,  /* OUT: By
33806 74 65 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b  te offset of tok
33807 65 6e 20 69 6e 20 69 6e 70 75 74 20 62 75 66 66  en in input buff
33808 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a 70  er */.    int *p
33809 69 45 6e 64 4f 66 66 73 65 74 2c 20 20 20 20 2f  iEndOffset,    /
3380a 2a 20 4f 55 54 3a 20 42 79 74 65 20 6f 66 66 73  * OUT: Byte offs
3380b 65 74 20 6f 66 20 65 6e 64 20 6f 66 20 74 6f 6b  et of end of tok
3380c 65 6e 20 69 6e 20 69 6e 70 75 74 20 62 75 66 66  en in input buff
3380d 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a 70  er */.    int *p
3380e 69 50 6f 73 69 74 69 6f 6e 20 20 20 20 20 20 2f  iPosition      /
3380f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66  * OUT: Number of
33810 20 74 6f 6b 65 6e 73 20 72 65 74 75 72 6e 65 64   tokens returned
33811 20 62 65 66 6f 72 65 20 74 68 69 73 20 6f 6e 65   before this one
33812 20 2a 2f 0a 20 20 29 3b 0a 7d 3b 0a 0a 73 74 72   */.  );.};..str
33813 75 63 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  uct sqlite3_toke
33814 6e 69 7a 65 72 20 7b 0a 20 20 63 6f 6e 73 74 20  nizer {.  const 
33815 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
33816 72 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  r_module *pModul
33817 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c  e;  /* The modul
33818 65 20 66 6f 72 20 74 68 69 73 20 74 6f 6b 65 6e  e for this token
33819 69 7a 65 72 20 2a 2f 0a 20 20 2f 2a 20 54 6f 6b  izer */.  /* Tok
3381a 65 6e 69 7a 65 72 20 69 6d 70 6c 65 6d 65 6e 74  enizer implement
3381b 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69  ations will typi
3381c 63 61 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69  cally add additi
3381d 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d  onal fields */.}
3381e 3b 0a 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  ;..struct sqlite
3381f 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
33820 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74  or {.  sqlite3_t
33821 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
33822 69 7a 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 54  izer;       /* T
33823 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 74 68 69  okenizer for thi
33824 73 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20 2f  s cursor. */.  /
33825 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 69 6d 70 6c  * Tokenizer impl
33826 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c  ementations will
33827 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20 61   typically add a
33828 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73  dditional fields
33829 20 2a 2f 0a 7d 3b 0a 0a 23 65 6e 64 69 66 20 2f   */.};..#endif /
3382a 2a 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49 5a 45  * _FTS3_TOKENIZE
3382b 52 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  R_H_ */../******
3382c 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
3382d 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e 68  fts3_tokenizer.h
3382e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
3382f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33830 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
33831 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
33832 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
33833 74 20 6f 66 66 20 69 6e 20 66 74 73 33 5f 65 78  t off in fts3_ex
33834 70 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  pr.h ***********
33835 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
33836 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 64 65  The following de
33837 73 63 72 69 62 65 73 20 74 68 65 20 73 79 6e 74  scribes the synt
33838 61 78 20 73 75 70 70 6f 72 74 65 64 20 62 79 20  ax supported by 
33839 74 68 65 20 66 74 73 33 20 4d 41 54 43 48 0a 2a  the fts3 MATCH.*
3383a 2a 20 6f 70 65 72 61 74 6f 72 20 69 6e 20 61 20  * operator in a 
3383b 73 69 6d 69 6c 61 72 20 66 6f 72 6d 61 74 20 74  similar format t
3383c 6f 20 74 68 61 74 20 75 73 65 64 20 62 79 20 74  o that used by t
3383d 68 65 20 6c 65 6d 6f 6e 20 70 61 72 73 65 72 0a  he lemon parser.
3383e 2a 2a 20 67 65 6e 65 72 61 74 6f 72 2e 20 54 68  ** generator. Th
3383f 69 73 20 6d 6f 64 75 6c 65 20 64 6f 65 73 20 6e  is module does n
33840 6f 74 20 75 73 65 20 61 63 74 75 61 6c 6c 79 20  ot use actually 
33841 6c 65 6d 6f 6e 2c 20 69 74 20 75 73 65 73 20 61  lemon, it uses a
33842 0a 2a 2a 20 63 75 73 74 6f 6d 20 70 61 72 73 65  .** custom parse
33843 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 71 75 65 72 79  r..**.**   query
33844 20 3a 3a 3d 20 61 6e 64 65 78 70 72 20 28 4f 52   ::= andexpr (OR
33845 20 61 6e 64 65 78 70 72 29 2a 2e 0a 2a 2a 0a 2a   andexpr)*..**.*
33846 2a 20 20 20 61 6e 64 65 78 70 72 20 3a 3a 3d 20  *   andexpr ::= 
33847 6e 6f 74 65 78 70 72 20 28 41 4e 44 3f 20 6e 6f  notexpr (AND? no
33848 74 65 78 70 72 29 2a 2e 0a 2a 2a 0a 2a 2a 20 20  texpr)*..**.**  
33849 20 6e 6f 74 65 78 70 72 20 3a 3a 3d 20 6e 65 61   notexpr ::= nea
3384a 72 65 78 70 72 20 28 4e 4f 54 20 6e 65 61 72 65  rexpr (NOT neare
3384b 78 70 72 7c 2d 54 4f 4b 45 4e 29 2a 2e 0a 2a 2a  xpr|-TOKEN)*..**
3384c 20 20 20 6e 6f 74 65 78 70 72 20 3a 3a 3d 20 4c     notexpr ::= L
3384d 50 20 71 75 65 72 79 20 52 50 2e 0a 2a 2a 0a 2a  P query RP..**.*
3384e 2a 20 20 20 6e 65 61 72 65 78 70 72 20 3a 3a 3d  *   nearexpr ::=
3384f 20 70 68 72 61 73 65 20 28 4e 45 41 52 20 64 69   phrase (NEAR di
33850 73 74 61 6e 63 65 5f 6f 70 74 20 6e 65 61 72 65  stance_opt neare
33851 78 70 72 29 2a 2e 0a 2a 2a 0a 2a 2a 20 20 20 64  xpr)*..**.**   d
33852 69 73 74 61 6e 63 65 5f 6f 70 74 20 3a 3a 3d 20  istance_opt ::= 
33853 2e 0a 2a 2a 20 20 20 64 69 73 74 61 6e 63 65 5f  ..**   distance_
33854 6f 70 74 20 3a 3a 3d 20 2f 20 49 4e 54 45 47 45  opt ::= / INTEGE
33855 52 2e 0a 2a 2a 0a 2a 2a 20 20 20 70 68 72 61 73  R..**.**   phras
33856 65 20 3a 3a 3d 20 54 4f 4b 45 4e 2e 0a 2a 2a 20  e ::= TOKEN..** 
33857 20 20 70 68 72 61 73 65 20 3a 3a 3d 20 43 4f 4c    phrase ::= COL
33858 55 4d 4e 3a 54 4f 4b 45 4e 2e 0a 2a 2a 20 20 20  UMN:TOKEN..**   
33859 70 68 72 61 73 65 20 3a 3a 3d 20 22 54 4f 4b 45  phrase ::= "TOKE
3385a 4e 20 54 4f 4b 45 4e 20 54 4f 4b 45 4e 2e 2e 2e  N TOKEN TOKEN...
3385b 22 2e 0a 2a 2f 0a 0a 74 79 70 65 64 65 66 20 73  "..*/..typedef s
3385c 74 72 75 63 74 20 46 74 73 33 45 78 70 72 20 46  truct Fts3Expr F
3385d 74 73 33 45 78 70 72 3b 0a 74 79 70 65 64 65 66  ts3Expr;.typedef
3385e 20 73 74 72 75 63 74 20 46 74 73 33 50 68 72 61   struct Fts3Phra
3385f 73 65 20 46 74 73 33 50 68 72 61 73 65 3b 0a 0a  se Fts3Phrase;..
33860 2f 2a 0a 2a 2a 20 41 20 22 70 68 72 61 73 65 22  /*.** A "phrase"
33861 20 69 73 20 61 20 73 65 71 75 65 6e 63 65 20 6f   is a sequence o
33862 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 6f  f one or more to
33863 6b 65 6e 73 20 74 68 61 74 20 6d 75 73 74 20 6d  kens that must m
33864 61 74 63 68 20 69 6e 0a 2a 2a 20 73 65 71 75 65  atch in.** seque
33865 6e 63 65 2e 20 20 41 20 73 69 6e 67 6c 65 20 74  nce.  A single t
33866 6f 6b 65 6e 20 69 73 20 74 68 65 20 62 61 73 65  oken is the base
33867 20 63 61 73 65 20 61 6e 64 20 74 68 65 20 6d 6f   case and the mo
33868 73 74 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 2e 0a  st common case..
33869 2a 2a 20 46 6f 72 20 61 20 73 65 71 75 65 6e 63  ** For a sequenc
3386a 65 20 6f 66 20 74 6f 6b 65 6e 73 20 63 6f 6e 74  e of tokens cont
3386b 61 69 6e 65 64 20 69 6e 20 22 2e 2e 2e 22 2c 20  ained in "...", 
3386c 6e 54 6f 6b 65 6e 20 77 69 6c 6c 20 62 65 20 74  nToken will be t
3386d 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20  he number.** of 
3386e 74 6f 6b 65 6e 73 20 69 6e 20 74 68 65 20 73 74  tokens in the st
3386f 72 69 6e 67 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ring..*/.struct 
33870 46 74 73 33 50 68 72 61 73 65 20 7b 0a 20 20 69  Fts3Phrase {.  i
33871 6e 74 20 6e 54 6f 6b 65 6e 3b 20 20 20 20 20 20  nt nToken;      
33872 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
33873 20 74 6f 6b 65 6e 73 20 69 6e 20 74 68 65 20 70   tokens in the p
33874 68 72 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69  hrase */.  int i
33875 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20  Column;         
33876 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75  /* Index of colu
33877 6d 6e 20 74 68 69 73 20 70 68 72 61 73 65 20 6d  mn this phrase m
33878 75 73 74 20 6d 61 74 63 68 20 2a 2f 0a 20 20 69  ust match */.  i
33879 6e 74 20 69 73 4e 6f 74 3b 20 20 20 20 20 20 20  nt isNot;       
3387a 20 20 20 20 2f 2a 20 50 68 72 61 73 65 20 70 72      /* Phrase pr
3387b 65 66 69 78 65 64 20 62 79 20 75 6e 61 72 79 20  efixed by unary 
3387c 6e 6f 74 20 28 2d 29 20 6f 70 65 72 61 74 6f 72  not (-) operator
3387d 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 50 68 72   */.  struct Phr
3387e 61 73 65 54 6f 6b 65 6e 20 7b 0a 20 20 20 20 63  aseToken {.    c
3387f 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20  har *z;         
33880 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20       /* Text of 
33881 74 68 65 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20  the token */.   
33882 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20   int n;         
33883 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
33884 20 6f 66 20 62 79 74 65 73 20 69 6e 20 62 75 66   of bytes in buf
33885 66 65 72 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  fer pointed to b
33886 79 20 7a 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  y z */.    int i
33887 73 50 72 65 66 69 78 3b 20 20 20 20 20 20 20 20  sPrefix;        
33888 20 2f 2a 20 54 72 75 65 20 69 66 20 74 6f 6b 65   /* True if toke
33889 6e 20 65 6e 64 73 20 69 6e 20 77 69 74 68 20 61  n ends in with a
3388a 20 22 2a 22 20 63 68 61 72 61 63 74 65 72 20 2a   "*" character *
3388b 2f 0a 20 20 7d 20 61 54 6f 6b 65 6e 5b 31 5d 3b  /.  } aToken[1];
3388c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
3388d 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 74  entry for each t
3388e 6f 6b 65 6e 20 69 6e 20 74 68 65 20 70 68 72 61  oken in the phra
3388f 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  se */.};../*.** 
33890 41 20 74 72 65 65 20 6f 66 20 74 68 65 73 65 20  A tree of these 
33891 6f 62 6a 65 63 74 73 20 66 6f 72 6d 73 20 74 68  objects forms th
33892 65 20 52 48 53 20 6f 66 20 61 20 4d 41 54 43 48  e RHS of a MATCH
33893 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74   operator..*/.st
33894 72 75 63 74 20 46 74 73 33 45 78 70 72 20 7b 0a  ruct Fts3Expr {.
33895 20 20 69 6e 74 20 65 54 79 70 65 3b 20 20 20 20    int eType;    
33896 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
33897 4f 6e 65 20 6f 66 20 74 68 65 20 46 54 53 51 55  One of the FTSQU
33898 45 52 59 5f 58 58 58 20 76 61 6c 75 65 73 20 64  ERY_XXX values d
33899 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 2a 2f 0a  efined below */.
3389a 20 20 69 6e 74 20 6e 4e 65 61 72 3b 20 20 20 20    int nNear;    
3389b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3389c 56 61 6c 69 64 20 69 66 20 65 54 79 70 65 3d 3d  Valid if eType==
3389d 46 54 53 51 55 45 52 59 5f 4e 45 41 52 20 2a 2f  FTSQUERY_NEAR */
3389e 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 50 61  .  Fts3Expr *pPa
3389f 72 65 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  rent;         /*
338a0 20 70 50 61 72 65 6e 74 2d 3e 70 4c 65 66 74 3d   pParent->pLeft=
338a1 3d 74 68 69 73 20 6f 72 20 70 50 61 72 65 6e 74  =this or pParent
338a2 2d 3e 70 52 69 67 68 74 3d 3d 74 68 69 73 20 2a  ->pRight==this *
338a3 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 4c  /.  Fts3Expr *pL
338a4 65 66 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  eft;           /
338a5 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a  * Left operand *
338a6 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 52  /.  Fts3Expr *pR
338a7 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 2f  ight;          /
338a8 2a 20 52 69 67 68 74 20 6f 70 65 72 61 6e 64 20  * Right operand 
338a9 2a 2f 0a 20 20 46 74 73 33 50 68 72 61 73 65 20  */.  Fts3Phrase 
338aa 2a 70 50 68 72 61 73 65 3b 20 20 20 20 20 20 20  *pPhrase;       
338ab 2f 2a 20 56 61 6c 69 64 20 69 66 20 65 54 79 70  /* Valid if eTyp
338ac 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41  e==FTSQUERY_PHRA
338ad 53 45 20 2a 2f 0a 7d 3b 0a 0a 53 51 4c 49 54 45  SE */.};..SQLITE
338ae 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
338af 69 74 65 33 46 74 73 33 45 78 70 72 50 61 72 73  ite3Fts3ExprPars
338b0 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e(sqlite3_tokeni
338b1 7a 65 72 20 2a 2c 20 63 68 61 72 20 2a 2a 2c 20  zer *, char **, 
338b2 69 6e 74 2c 20 69 6e 74 2c 20 0a 20 20 20 20 20  int, int, .     
338b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
338b4 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
338b5 2c 20 69 6e 74 2c 20 46 74 73 33 45 78 70 72 20  , int, Fts3Expr 
338b6 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
338b7 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
338b8 46 74 73 33 45 78 70 72 46 72 65 65 28 46 74 73  Fts3ExprFree(Fts
338b9 33 45 78 70 72 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  3Expr *);../*.**
338ba 20 43 61 6e 64 69 64 61 74 65 20 76 61 6c 75 65   Candidate value
338bb 73 20 66 6f 72 20 46 74 73 33 51 75 65 72 79 2e  s for Fts3Query.
338bc 65 54 79 70 65 2e 20 4e 6f 74 65 20 74 68 61 74  eType. Note that
338bd 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20 74 68   the order of th
338be 65 20 66 69 72 73 74 0a 2a 2a 20 66 6f 75 72 20  e first.** four 
338bf 76 61 6c 75 65 73 20 69 73 20 69 6e 20 6f 72 64  values is in ord
338c0 65 72 20 6f 66 20 70 72 65 63 65 64 65 6e 63 65  er of precedence
338c1 20 77 68 65 6e 20 70 61 72 73 69 6e 67 20 65 78   when parsing ex
338c2 70 72 65 73 73 69 6f 6e 73 2e 20 46 6f 72 20 0a  pressions. For .
338c3 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20  ** example, the 
338c4 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a  following:.**.**
338c5 20 20 20 22 61 20 4f 52 20 62 20 41 4e 44 20 63     "a OR b AND c
338c6 20 4e 4f 54 20 64 20 4e 45 41 52 20 65 22 0a 2a   NOT d NEAR e".*
338c7 2a 0a 2a 2a 20 69 73 20 65 71 75 69 76 61 6c 65  *.** is equivale
338c8 6e 74 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 22  nt to:.**.**   "
338c9 61 20 4f 52 20 28 62 20 41 4e 44 20 28 63 20 4e  a OR (b AND (c N
338ca 4f 54 20 28 64 20 4e 45 41 52 20 65 29 29 29 22  OT (d NEAR e)))"
338cb 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 54 53 51  .*/.#define FTSQ
338cc 55 45 52 59 5f 4e 45 41 52 20 20 20 31 0a 23 64  UERY_NEAR   1.#d
338cd 65 66 69 6e 65 20 46 54 53 51 55 45 52 59 5f 4e  efine FTSQUERY_N
338ce 4f 54 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20  OT    2.#define 
338cf 46 54 53 51 55 45 52 59 5f 41 4e 44 20 20 20 20  FTSQUERY_AND    
338d0 33 0a 23 64 65 66 69 6e 65 20 46 54 53 51 55 45  3.#define FTSQUE
338d1 52 59 5f 4f 52 20 20 20 20 20 34 0a 23 64 65 66  RY_OR     4.#def
338d2 69 6e 65 20 46 54 53 51 55 45 52 59 5f 50 48 52  ine FTSQUERY_PHR
338d3 41 53 45 20 35 0a 0a 23 69 66 64 65 66 20 53 51  ASE 5..#ifdef SQ
338d4 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
338d5 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
338d6 6c 69 74 65 33 46 74 73 33 45 78 70 72 49 6e 69  lite3Fts3ExprIni
338d7 74 54 65 73 74 49 6e 74 65 72 66 61 63 65 28 73  tTestInterface(s
338d8 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 23 65 6e  qlite3 *db);.#en
338d9 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
338da 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33  **** End of fts3
338db 5f 65 78 70 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  _expr.h ********
338dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338de 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
338df 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
338e0 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
338e1 66 20 69 6e 20 66 74 73 33 2e 63 20 2a 2a 2a 2a  f in fts3.c ****
338e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338e3 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
338e4 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 66 74 73  **** Include fts
338e5 33 5f 68 61 73 68 2e 68 20 69 6e 20 74 68 65 20  3_hash.h in the 
338e6 6d 69 64 64 6c 65 20 6f 66 20 66 74 73 33 2e 63  middle of fts3.c
338e7 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
338e8 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
338e9 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
338ea 66 74 73 33 5f 68 61 73 68 2e 68 20 2a 2a 2a 2a  fts3_hash.h ****
338eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338ed 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
338ee 53 65 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a  September 22.**.
338ef 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
338f0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
338f1 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
338f2 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
338f3 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
338f4 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
338f5 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
338f6 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
338f7 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
338f8 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
338f9 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
338fa 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
338fb 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
338fc 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
338fd 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
338fe 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
338ff 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
33900 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
33901 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33902 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33903 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33904 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
33905 68 69 73 20 69 73 20 74 68 65 20 68 65 61 64 65  his is the heade
33906 72 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 67  r file for the g
33907 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c  eneric hash-tabl
33908 65 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 0a  e implemenation.
33909 2a 2a 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74  ** used in SQLit
3390a 65 2e 20 20 57 65 27 76 65 20 6d 6f 64 69 66 69  e.  We've modifi
3390b 65 64 20 69 74 20 73 6c 69 67 68 74 6c 79 20 74  ed it slightly t
3390c 6f 20 73 65 72 76 65 20 61 73 20 61 20 73 74 61  o serve as a sta
3390d 6e 64 61 6c 6f 6e 65 0a 2a 2a 20 68 61 73 68 20  ndalone.** hash 
3390e 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  table implementa
3390f 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 66 75 6c  tion for the ful
33910 6c 2d 74 65 78 74 20 69 6e 64 65 78 69 6e 67 20  l-text indexing 
33911 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2f 0a 23 69  module..**.*/.#i
33912 66 6e 64 65 66 20 5f 46 54 53 33 5f 48 41 53 48  fndef _FTS3_HASH
33913 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 46 54 53  _H_.#define _FTS
33914 33 5f 48 41 53 48 5f 48 5f 0a 0a 2f 2a 20 46 6f  3_HASH_H_../* Fo
33915 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f  rward declaratio
33916 6e 73 20 6f 66 20 73 74 72 75 63 74 75 72 65 73  ns of structures
33917 2e 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  . */.typedef str
33918 75 63 74 20 66 74 73 33 48 61 73 68 20 66 74 73  uct fts3Hash fts
33919 33 48 61 73 68 3b 0a 74 79 70 65 64 65 66 20 73  3Hash;.typedef s
3391a 74 72 75 63 74 20 66 74 73 33 48 61 73 68 45 6c  truct fts3HashEl
3391b 65 6d 20 66 74 73 33 48 61 73 68 45 6c 65 6d 3b  em fts3HashElem;
3391c 0a 0a 2f 2a 20 41 20 63 6f 6d 70 6c 65 74 65 20  ../* A complete 
3391d 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 61 6e  hash table is an
3391e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
3391f 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
33920 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20 69 6e 74  ture..** The int
33921 65 72 6e 61 6c 73 20 6f 66 20 74 68 69 73 20 73  ernals of this s
33922 74 72 75 63 74 75 72 65 20 61 72 65 20 69 6e 74  tructure are int
33923 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71  ended to be opaq
33924 75 65 20 2d 2d 20 63 6c 69 65 6e 74 0a 2a 2a 20  ue -- client.** 
33925 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20  code should not 
33926 61 74 74 65 6d 70 74 20 74 6f 20 61 63 63 65 73  attempt to acces
33927 73 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 65 20  s or modify the 
33928 66 69 65 6c 64 73 20 6f 66 20 74 68 69 73 20 73  fields of this s
33929 74 72 75 63 74 75 72 65 0a 2a 2a 20 64 69 72 65  tructure.** dire
3392a 63 74 6c 79 2e 20 20 43 68 61 6e 67 65 20 74 68  ctly.  Change th
3392b 69 73 20 73 74 72 75 63 74 75 72 65 20 6f 6e 6c  is structure onl
3392c 79 20 62 79 20 75 73 69 6e 67 20 74 68 65 20 72  y by using the r
3392d 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77 2e 0a 2a  outines below..*
3392e 2a 20 48 6f 77 65 76 65 72 2c 20 6d 61 6e 79 20  * However, many 
3392f 6f 66 20 74 68 65 20 22 70 72 6f 63 65 64 75 72  of the "procedur
33930 65 73 22 20 61 6e 64 20 22 66 75 6e 63 74 69 6f  es" and "functio
33931 6e 73 22 20 66 6f 72 20 6d 6f 64 69 66 79 69 6e  ns" for modifyin
33932 67 20 61 6e 64 0a 2a 2a 20 61 63 63 65 73 73 69  g and.** accessi
33933 6e 67 20 74 68 69 73 20 73 74 72 75 63 74 75 72  ng this structur
33934 65 20 61 72 65 20 72 65 61 6c 6c 79 20 6d 61 63  e are really mac
33935 72 6f 73 2c 20 73 6f 20 77 65 20 63 61 6e 27 74  ros, so we can't
33936 20 72 65 61 6c 6c 79 20 6d 61 6b 65 0a 2a 2a 20   really make.** 
33937 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6f  this structure o
33938 70 61 71 75 65 2e 0a 2a 2f 0a 73 74 72 75 63 74  paque..*/.struct
33939 20 66 74 73 33 48 61 73 68 20 7b 0a 20 20 63 68   fts3Hash {.  ch
3393a 61 72 20 6b 65 79 43 6c 61 73 73 3b 20 20 20 20  ar keyClass;    
3393b 20 20 20 20 20 20 2f 2a 20 48 41 53 48 5f 49 4e        /* HASH_IN
3393c 54 2c 20 5f 50 4f 49 4e 54 45 52 2c 20 5f 53 54  T, _POINTER, _ST
3393d 52 49 4e 47 2c 20 5f 42 49 4e 41 52 59 20 2a 2f  RING, _BINARY */
3393e 0a 20 20 63 68 61 72 20 63 6f 70 79 4b 65 79 3b  .  char copyKey;
3393f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
33940 75 65 20 69 66 20 63 6f 70 79 20 6f 66 20 6b 65  ue if copy of ke
33941 79 20 6d 61 64 65 20 6f 6e 20 69 6e 73 65 72 74  y made on insert
33942 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74 3b   */.  int count;
33943 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
33944 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
33945 65 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65  es in this table
33946 20 2a 2f 0a 20 20 66 74 73 33 48 61 73 68 45 6c   */.  fts3HashEl
33947 65 6d 20 2a 66 69 72 73 74 3b 20 20 20 20 2f 2a  em *first;    /*
33948 20 54 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65   The first eleme
33949 6e 74 20 6f 66 20 74 68 65 20 61 72 72 61 79 20  nt of the array 
3394a 2a 2f 0a 20 20 69 6e 74 20 68 74 73 69 7a 65 3b  */.  int htsize;
3394b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3394c 4e 75 6d 62 65 72 20 6f 66 20 62 75 63 6b 65 74  Number of bucket
3394d 73 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61  s in the hash ta
3394e 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ble */.  struct 
3394f 5f 66 74 73 33 68 74 20 7b 20 20 20 20 20 20 20  _fts3ht {       
33950 20 2f 2a 20 74 68 65 20 68 61 73 68 20 74 61 62   /* the hash tab
33951 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 6f  le */.    int co
33952 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  unt;            
33953 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
33954 65 6e 74 72 69 65 73 20 77 69 74 68 20 74 68 69  entries with thi
33955 73 20 68 61 73 68 20 2a 2f 0a 20 20 20 20 66 74  s hash */.    ft
33956 73 33 48 61 73 68 45 6c 65 6d 20 2a 63 68 61 69  s3HashElem *chai
33957 6e 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65  n;     /* Pointe
33958 72 20 74 6f 20 66 69 72 73 74 20 65 6e 74 72 79  r to first entry
33959 20 77 69 74 68 20 74 68 69 73 20 68 61 73 68 20   with this hash 
3395a 2a 2f 0a 20 20 7d 20 2a 68 74 3b 0a 7d 3b 0a 0a  */.  } *ht;.};..
3395b 2f 2a 20 45 61 63 68 20 65 6c 65 6d 65 6e 74 20  /* Each element 
3395c 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  in the hash tabl
3395d 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  e is an instance
3395e 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
3395f 67 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e  g .** structure.
33960 20 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 61    All elements a
33961 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 73  re stored on a s
33962 69 6e 67 6c 65 20 64 6f 75 62 6c 79 2d 6c 69 6e  ingle doubly-lin
33963 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  ked list..**.** 
33964 41 67 61 69 6e 2c 20 74 68 69 73 20 73 74 72 75  Again, this stru
33965 63 74 75 72 65 20 69 73 20 69 6e 74 65 6e 64 65  cture is intende
33966 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65 2c 20  d to be opaque, 
33967 62 75 74 20 69 74 20 63 61 6e 27 74 20 72 65 61  but it can't rea
33968 6c 6c 79 0a 2a 2a 20 62 65 20 6f 70 61 71 75 65  lly.** be opaque
33969 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20 75   because it is u
3396a 73 65 64 20 62 79 20 6d 61 63 72 6f 73 2e 0a 2a  sed by macros..*
3396b 2f 0a 73 74 72 75 63 74 20 66 74 73 33 48 61 73  /.struct fts3Has
3396c 68 45 6c 65 6d 20 7b 0a 20 20 66 74 73 33 48 61  hElem {.  fts3Ha
3396d 73 68 45 6c 65 6d 20 2a 6e 65 78 74 2c 20 2a 70  shElem *next, *p
3396e 72 65 76 3b 20 2f 2a 20 4e 65 78 74 20 61 6e 64  rev; /* Next and
3396f 20 70 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e   previous elemen
33970 74 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20  ts in the table 
33971 2a 2f 0a 20 20 76 6f 69 64 20 2a 64 61 74 61 3b  */.  void *data;
33972 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33973 2f 2a 20 44 61 74 61 20 61 73 73 6f 63 69 61 74  /* Data associat
33974 65 64 20 77 69 74 68 20 74 68 69 73 20 65 6c 65  ed with this ele
33975 6d 65 6e 74 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  ment */.  void *
33976 70 4b 65 79 3b 20 69 6e 74 20 6e 4b 65 79 3b 20  pKey; int nKey; 
33977 20 20 20 20 20 2f 2a 20 4b 65 79 20 61 73 73 6f       /* Key asso
33978 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73  ciated with this
33979 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a   element */.};..
3397a 2f 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20  /*.** There are 
3397b 32 20 64 69 66 66 65 72 65 6e 74 20 6d 6f 64 65  2 different mode
3397c 73 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 20 66  s of operation f
3397d 6f 72 20 61 20 68 61 73 68 20 74 61 62 6c 65 3a  or a hash table:
3397e 0a 2a 2a 0a 2a 2a 20 20 20 46 54 53 33 5f 48 41  .**.**   FTS3_HA
3397f 53 48 5f 53 54 52 49 4e 47 20 20 20 20 20 20 20  SH_STRING       
33980 20 70 4b 65 79 20 70 6f 69 6e 74 73 20 74 6f 20   pKey points to 
33981 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 69 73  a string that is
33982 20 6e 4b 65 79 20 62 79 74 65 73 20 6c 6f 6e 67   nKey bytes long
33983 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
33984 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69                (i
33985 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 6e 75 6c  ncluding the nul
33986 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 2c 20 69 66  l-terminator, if
33987 20 61 6e 79 29 2e 20 20 43 61 73 65 0a 2a 2a 20   any).  Case.** 
33988 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33989 20 20 20 20 20 20 20 20 20 20 69 73 20 72 65 73            is res
3398a 70 65 63 74 65 64 20 69 6e 20 63 6f 6d 70 61 72  pected in compar
3398b 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 46  isons..**.**   F
3398c 54 53 33 5f 48 41 53 48 5f 42 49 4e 41 52 59 20  TS3_HASH_BINARY 
3398d 20 20 20 20 20 20 20 70 4b 65 79 20 70 6f 69 6e         pKey poin
3398e 74 73 20 74 6f 20 62 69 6e 61 72 79 20 64 61 74  ts to binary dat
3398f 61 20 6e 4b 65 79 20 62 79 74 65 73 20 6c 6f 6e  a nKey bytes lon
33990 67 2e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  g. .**          
33991 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33992 20 6d 65 6d 63 6d 70 28 29 20 69 73 20 75 73 65   memcmp() is use
33993 64 20 74 6f 20 63 6f 6d 70 61 72 65 20 6b 65 79  d to compare key
33994 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 6f 70 79 20  s..**.** A copy 
33995 6f 66 20 74 68 65 20 6b 65 79 20 69 73 20 6d 61  of the key is ma
33996 64 65 20 69 66 20 74 68 65 20 63 6f 70 79 4b 65  de if the copyKe
33997 79 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 66  y parameter to f
33998 74 73 33 48 61 73 68 49 6e 69 74 20 69 73 20 31  ts3HashInit is 1
33999 2e 20 20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46  .  .*/.#define F
3399a 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 20  TS3_HASH_STRING 
3399b 20 20 20 31 0a 23 64 65 66 69 6e 65 20 46 54 53     1.#define FTS
3399c 33 5f 48 41 53 48 5f 42 49 4e 41 52 59 20 20 20  3_HASH_BINARY   
3399d 20 32 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73 73   2../*.** Access
3399e 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 6f 20 64   routines.  To d
3399f 65 6c 65 74 65 2c 20 69 6e 73 65 72 74 20 61 20  elete, insert a 
339a0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f  NULL pointer..*/
339a1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
339a2 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33  void sqlite3Fts3
339a3 48 61 73 68 49 6e 69 74 28 66 74 73 33 48 61 73  HashInit(fts3Has
339a4 68 2a 2c 20 69 6e 74 20 6b 65 79 74 79 70 65 2c  h*, int keytype,
339a5 20 69 6e 74 20 63 6f 70 79 4b 65 79 29 3b 0a 53   int copyKey);.S
339a6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
339a7 69 64 20 2a 73 71 6c 69 74 65 33 46 74 73 33 48  id *sqlite3Fts3H
339a8 61 73 68 49 6e 73 65 72 74 28 66 74 73 33 48 61  ashInsert(fts3Ha
339a9 73 68 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  sh*, const void 
339aa 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c  *pKey, int nKey,
339ab 20 76 6f 69 64 20 2a 70 44 61 74 61 29 3b 0a 53   void *pData);.S
339ac 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
339ad 69 64 20 2a 73 71 6c 69 74 65 33 46 74 73 33 48  id *sqlite3Fts3H
339ae 61 73 68 46 69 6e 64 28 63 6f 6e 73 74 20 66 74  ashFind(const ft
339af 73 33 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 76  s3Hash*, const v
339b0 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e  oid *pKey, int n
339b1 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  Key);.SQLITE_PRI
339b2 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
339b3 33 46 74 73 33 48 61 73 68 43 6c 65 61 72 28 66  3Fts3HashClear(f
339b4 74 73 33 48 61 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a  ts3Hash*);../*.*
339b5 2a 20 53 68 6f 72 74 68 61 6e 64 20 66 6f 72 20  * Shorthand for 
339b6 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 62  the functions ab
339b7 6f 76 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66  ove.*/.#define f
339b8 74 73 33 48 61 73 68 49 6e 69 74 20 20 20 73 71  ts3HashInit   sq
339b9 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 69  lite3Fts3HashIni
339ba 74 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48 61  t.#define fts3Ha
339bb 73 68 49 6e 73 65 72 74 20 73 71 6c 69 74 65 33  shInsert sqlite3
339bc 46 74 73 33 48 61 73 68 49 6e 73 65 72 74 0a 23  Fts3HashInsert.#
339bd 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 46  define fts3HashF
339be 69 6e 64 20 20 20 73 71 6c 69 74 65 33 46 74 73  ind   sqlite3Fts
339bf 33 48 61 73 68 46 69 6e 64 0a 23 64 65 66 69 6e  3HashFind.#defin
339c0 65 20 66 74 73 33 48 61 73 68 43 6c 65 61 72 20  e fts3HashClear 
339c1 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
339c2 43 6c 65 61 72 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63  Clear../*.** Mac
339c3 72 6f 73 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20  ros for looping 
339c4 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d 65 6e 74  over all element
339c5 73 20 6f 66 20 61 20 68 61 73 68 20 74 61 62 6c  s of a hash tabl
339c6 65 2e 20 20 54 68 65 20 69 64 69 6f 6d 20 69 73  e.  The idiom is
339c7 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a  .** like this:.*
339c8 2a 0a 2a 2a 20 20 20 66 74 73 33 48 61 73 68 20  *.**   fts3Hash 
339c9 68 3b 0a 2a 2a 20 20 20 66 74 73 33 48 61 73 68  h;.**   fts3Hash
339ca 45 6c 65 6d 20 2a 70 3b 0a 2a 2a 20 20 20 2e 2e  Elem *p;.**   ..
339cb 2e 0a 2a 2a 20 20 20 66 6f 72 28 70 3d 66 74 73  ..**   for(p=fts
339cc 33 48 61 73 68 46 69 72 73 74 28 26 68 29 3b 20  3HashFirst(&h); 
339cd 70 3b 20 70 3d 66 74 73 33 48 61 73 68 4e 65 78  p; p=fts3HashNex
339ce 74 28 70 29 29 7b 0a 2a 2a 20 20 20 20 20 53 6f  t(p)){.**     So
339cf 6d 65 53 74 72 75 63 74 75 72 65 20 2a 70 44 61  meStructure *pDa
339d0 74 61 20 3d 20 66 74 73 33 48 61 73 68 44 61 74  ta = fts3HashDat
339d1 61 28 70 29 3b 0a 2a 2a 20 20 20 20 20 2f 2f 20  a(p);.**     // 
339d2 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77 69 74  do something wit
339d3 68 20 70 44 61 74 61 0a 2a 2a 20 20 20 7d 0a 2a  h pData.**   }.*
339d4 2f 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48 61  /.#define fts3Ha
339d5 73 68 46 69 72 73 74 28 48 29 20 20 28 28 48 29  shFirst(H)  ((H)
339d6 2d 3e 66 69 72 73 74 29 0a 23 64 65 66 69 6e 65  ->first).#define
339d7 20 66 74 73 33 48 61 73 68 4e 65 78 74 28 45 29   fts3HashNext(E)
339d8 20 20 20 28 28 45 29 2d 3e 6e 65 78 74 29 0a 23     ((E)->next).#
339d9 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 44  define fts3HashD
339da 61 74 61 28 45 29 20 20 20 28 28 45 29 2d 3e 64  ata(E)   ((E)->d
339db 61 74 61 29 0a 23 64 65 66 69 6e 65 20 66 74 73  ata).#define fts
339dc 33 48 61 73 68 4b 65 79 28 45 29 20 20 20 20 28  3HashKey(E)    (
339dd 28 45 29 2d 3e 70 4b 65 79 29 0a 23 64 65 66 69  (E)->pKey).#defi
339de 6e 65 20 66 74 73 33 48 61 73 68 4b 65 79 73 69  ne fts3HashKeysi
339df 7a 65 28 45 29 20 28 28 45 29 2d 3e 6e 4b 65 79  ze(E) ((E)->nKey
339e0 29 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20  )../*.** Number 
339e1 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 20  of entries in a 
339e2 68 61 73 68 20 74 61 62 6c 65 0a 2a 2f 0a 23 64  hash table.*/.#d
339e3 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 43 6f  efine fts3HashCo
339e4 75 6e 74 28 48 29 20 20 28 28 48 29 2d 3e 63 6f  unt(H)  ((H)->co
339e5 75 6e 74 29 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  unt)..#endif /* 
339e6 5f 46 54 53 33 5f 48 41 53 48 5f 48 5f 20 2a 2f  _FTS3_HASH_H_ */
339e7 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
339e8 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 68 61  * End of fts3_ha
339e9 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  sh.h ***********
339ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
339eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
339ec 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
339ed 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
339ee 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
339ef 6e 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a  n fts3.c *******
339f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
339f1 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
339f2 5f 43 4f 52 45 20 0a 20 20 53 51 4c 49 54 45 5f  _CORE .  SQLITE_
339f3 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 0a  EXTENSION_INIT1.
339f4 23 65 6e 64 69 66 0a 0a 0a 2f 2a 20 54 4f 44 4f  #endif.../* TODO
339f5 28 73 68 65 73 73 29 20 4d 41 4e 2c 20 74 68 69  (shess) MAN, thi
339f6 73 20 74 68 69 6e 67 20 6e 65 65 64 73 20 73 6f  s thing needs so
339f7 6d 65 20 72 65 66 61 63 74 6f 72 69 6e 67 2e 20  me refactoring. 
339f8 20 41 74 20 6d 69 6e 69 6d 75 6d 2c 20 69 74 0a   At minimum, it.
339f9 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6e 69 63 65  ** would be nice
339fa 20 74 6f 20 6f 72 64 65 72 20 74 68 65 20 66 69   to order the fi
339fb 6c 65 20 62 65 74 74 65 72 2c 20 70 65 72 68 61  le better, perha
339fc 70 73 20 73 6f 6d 65 74 68 69 6e 67 20 61 6c 6f  ps something alo
339fd 6e 67 20 74 68 65 0a 2a 2a 20 6c 69 6e 65 73 20  ng the.** lines 
339fe 6f 66 3a 0a 2a 2a 0a 2a 2a 20 20 2d 20 75 74 69  of:.**.**  - uti
339ff 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e 73 0a 2a  lity functions.*
33a00 2a 20 20 2d 20 74 61 62 6c 65 20 73 65 74 75 70  *  - table setup
33a01 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 2d   functions.**  -
33a02 20 74 61 62 6c 65 20 75 70 64 61 74 65 20 66 75   table update fu
33a03 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 2d 20 74 61  nctions.**  - ta
33a04 62 6c 65 20 71 75 65 72 79 20 66 75 6e 63 74 69  ble query functi
33a05 6f 6e 73 0a 2a 2a 0a 2a 2a 20 50 75 74 20 74 68  ons.**.** Put th
33a06 65 20 71 75 65 72 79 20 66 75 6e 63 74 69 6f 6e  e query function
33a07 73 20 6c 61 73 74 20 62 65 63 61 75 73 65 20 74  s last because t
33a08 68 65 79 27 72 65 20 6c 69 6b 65 6c 79 20 74 6f  hey're likely to
33a09 20 72 65 66 65 72 65 6e 63 65 0a 2a 2a 20 74 79   reference.** ty
33a0a 70 65 64 65 66 73 20 6f 72 20 66 75 6e 63 74 69  pedefs or functi
33a0b 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20 74 61 62  ons from the tab
33a0c 6c 65 20 75 70 64 61 74 65 20 73 65 63 74 69 6f  le update sectio
33a0d 6e 2e 0a 2a 2f 0a 0a 23 69 66 20 30 0a 23 20 64  n..*/..#if 0.# d
33a0e 65 66 69 6e 65 20 46 54 53 54 52 41 43 45 28 41  efine FTSTRACE(A
33a0f 29 20 20 70 72 69 6e 74 66 20 41 3b 20 66 66 6c  )  printf A; ffl
33a10 75 73 68 28 73 74 64 6f 75 74 29 0a 23 65 6c 73  ush(stdout).#els
33a11 65 0a 23 20 64 65 66 69 6e 65 20 46 54 53 54 52  e.# define FTSTR
33a12 41 43 45 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f  ACE(A).#endif../
33a13 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65  * It is not safe
33a14 20 74 6f 20 63 61 6c 6c 20 69 73 73 70 61 63 65   to call isspace
33a15 28 29 2c 20 74 6f 6c 6f 77 65 72 28 29 2c 20 6f  (), tolower(), o
33a16 72 20 69 73 61 6c 6e 75 6d 28 29 20 6f 6e 0a 2a  r isalnum() on.*
33a17 2a 20 68 69 2d 62 69 74 2d 73 65 74 20 63 68 61  * hi-bit-set cha
33a18 72 61 63 74 65 72 73 2e 20 20 54 68 69 73 20 69  racters.  This i
33a19 73 20 74 68 65 20 73 61 6d 65 20 73 6f 6c 75 74  s the same solut
33a1a 69 6f 6e 20 75 73 65 64 20 69 6e 20 74 68 65 0a  ion used in the.
33a1b 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2f  ** tokenizer..*/
33a1c 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
33a1d 54 68 65 20 73 6e 69 70 70 65 74 2d 67 65 6e 65  The snippet-gene
33a1e 72 61 74 69 6f 6e 20 63 6f 64 65 20 73 68 6f 75  ration code shou
33a1f 6c 64 20 62 65 20 75 73 69 6e 67 20 74 68 65 0a  ld be using the.
33a20 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72 2d 67 65 6e  ** tokenizer-gen
33a21 65 72 61 74 65 64 20 74 6f 6b 65 6e 73 20 72 61  erated tokens ra
33a22 74 68 65 72 20 74 68 61 6e 20 64 6f 69 6e 67 20  ther than doing 
33a23 69 74 73 20 6f 77 6e 20 6c 6f 63 61 6c 0a 2a 2a  its own local.**
33a24 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 2e 0a 2a   tokenization..*
33a25 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  /./* TODO(shess)
33a26 20 49 73 20 5f 5f 69 73 61 73 63 69 69 28 29 20   Is __isascii() 
33a27 61 20 70 6f 72 74 61 62 6c 65 20 76 65 72 73 69  a portable versi
33a28 6f 6e 20 6f 66 20 28 63 26 30 78 38 30 29 3d 3d  on of (c&0x80)==
33a29 30 3f 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  0? */.static int
33a2a 20 73 61 66 65 5f 69 73 73 70 61 63 65 28 63 68   safe_isspace(ch
33a2b 61 72 20 63 29 7b 0a 20 20 72 65 74 75 72 6e 20  ar c){.  return 
33a2c 28 63 26 30 78 38 30 29 3d 3d 30 20 3f 20 69 73  (c&0x80)==0 ? is
33a2d 73 70 61 63 65 28 63 29 20 3a 20 30 3b 0a 7d 0a  space(c) : 0;.}.
33a2e 73 74 61 74 69 63 20 69 6e 74 20 73 61 66 65 5f  static int safe_
33a2f 74 6f 6c 6f 77 65 72 28 63 68 61 72 20 63 29 7b  tolower(char c){
33a30 0a 20 20 72 65 74 75 72 6e 20 28 63 26 30 78 38  .  return (c&0x8
33a31 30 29 3d 3d 30 20 3f 20 74 6f 6c 6f 77 65 72 28  0)==0 ? tolower(
33a32 63 29 20 3a 20 63 3b 0a 7d 0a 73 74 61 74 69 63  c) : c;.}.static
33a33 20 69 6e 74 20 73 61 66 65 5f 69 73 61 6c 6e 75   int safe_isalnu
33a34 6d 28 63 68 61 72 20 63 29 7b 0a 20 20 72 65 74  m(char c){.  ret
33a35 75 72 6e 20 28 63 26 30 78 38 30 29 3d 3d 30 20  urn (c&0x80)==0 
33a36 3f 20 69 73 61 6c 6e 75 6d 28 63 29 20 3a 20 30  ? isalnum(c) : 0
33a37 3b 0a 7d 0a 0a 74 79 70 65 64 65 66 20 65 6e 75  ;.}..typedef enu
33a38 6d 20 44 6f 63 4c 69 73 74 54 79 70 65 20 7b 0a  m DocListType {.
33a39 20 20 44 4c 5f 44 4f 43 49 44 53 2c 20 20 20 20    DL_DOCIDS,    
33a3a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 6f 63            /* doc
33a3b 69 64 73 20 6f 6e 6c 79 20 2a 2f 0a 20 20 44 4c  ids only */.  DL
33a3c 5f 50 4f 53 49 54 49 4f 4e 53 2c 20 20 20 20 20  _POSITIONS,     
33a3d 20 20 20 20 20 20 2f 2a 20 64 6f 63 69 64 73 20        /* docids 
33a3e 2b 20 70 6f 73 69 74 69 6f 6e 73 20 2a 2f 0a 20  + positions */. 
33a3f 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46   DL_POSITIONS_OF
33a40 46 53 45 54 53 20 20 20 20 2f 2a 20 64 6f 63 69  FSETS    /* doci
33a41 64 73 20 2b 20 70 6f 73 69 74 69 6f 6e 73 20 2b  ds + positions +
33a42 20 6f 66 66 73 65 74 73 20 2a 2f 0a 7d 20 44 6f   offsets */.} Do
33a43 63 4c 69 73 74 54 79 70 65 3b 0a 0a 2f 2a 0a 2a  cListType;../*.*
33a44 2a 20 42 79 20 64 65 66 61 75 6c 74 2c 20 6f 6e  * By default, on
33a45 6c 79 20 70 6f 73 69 74 69 6f 6e 73 20 61 6e 64  ly positions and
33a46 20 6e 6f 74 20 6f 66 66 73 65 74 73 20 61 72 65   not offsets are
33a47 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64   stored in the d
33a48 6f 63 6c 69 73 74 73 2e 0a 2a 2a 20 54 6f 20 63  oclists..** To c
33a49 68 61 6e 67 65 20 74 68 69 73 20 73 6f 20 74 68  hange this so th
33a4a 61 74 20 6f 66 66 73 65 74 73 20 61 72 65 20 73  at offsets are s
33a4b 74 6f 72 65 64 20 74 6f 6f 2c 20 63 6f 6d 70 69  tored too, compi
33a4c 6c 65 20 77 69 74 68 0a 2a 2a 0a 2a 2a 20 20 20  le with.**.**   
33a4d 20 20 20 20 20 20 20 2d 44 44 4c 5f 44 45 46 41         -DDL_DEFA
33a4e 55 4c 54 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53  ULT=DL_POSITIONS
33a4f 5f 4f 46 46 53 45 54 53 0a 2a 2a 0a 2a 2a 20 49  _OFFSETS.**.** I
33a50 66 20 44 4c 5f 44 45 46 41 55 4c 54 20 69 73 20  f DL_DEFAULT is 
33a51 73 65 74 20 74 6f 20 44 4c 5f 44 4f 43 49 44 53  set to DL_DOCIDS
33a52 2c 20 79 6f 75 72 20 74 61 62 6c 65 20 63 61 6e  , your table can
33a53 20 6f 6e 6c 79 20 62 65 20 69 6e 73 65 72 74 65   only be inserte
33a54 64 0a 2a 2a 20 69 6e 74 6f 20 28 6e 6f 20 64 65  d.** into (no de
33a55 6c 65 74 65 73 20 6f 72 20 75 70 64 61 74 65 73  letes or updates
33a56 29 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 44 4c  )..*/.#ifndef DL
33a57 5f 44 45 46 41 55 4c 54 0a 23 20 64 65 66 69 6e  _DEFAULT.# defin
33a58 65 20 44 4c 5f 44 45 46 41 55 4c 54 20 44 4c 5f  e DL_DEFAULT DL_
33a59 50 4f 53 49 54 49 4f 4e 53 0a 23 65 6e 64 69 66  POSITIONS.#endif
33a5a 0a 0a 65 6e 75 6d 20 7b 0a 20 20 50 4f 53 5f 45  ..enum {.  POS_E
33a5b 4e 44 20 3d 20 30 2c 20 20 20 20 20 20 20 20 2f  ND = 0,        /
33a5c 2a 20 65 6e 64 20 6f 66 20 74 68 69 73 20 70 6f  * end of this po
33a5d 73 69 74 69 6f 6e 20 6c 69 73 74 20 2a 2f 0a 20  sition list */. 
33a5e 20 50 4f 53 5f 43 4f 4c 55 4d 4e 2c 20 20 20 20   POS_COLUMN,    
33a5f 20 20 20 20 20 2f 2a 20 66 6f 6c 6c 6f 77 65 64       /* followed
33a60 20 62 79 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 6e   by new column n
33a61 75 6d 62 65 72 20 2a 2f 0a 20 20 50 4f 53 5f 42  umber */.  POS_B
33a62 41 53 45 0a 7d 3b 0a 0a 2f 2a 20 4d 45 52 47 45  ASE.};../* MERGE
33a63 5f 43 4f 55 4e 54 20 63 6f 6e 74 72 6f 6c 73 20  _COUNT controls 
33a64 68 6f 77 20 6f 66 74 65 6e 20 77 65 20 6d 65 72  how often we mer
33a65 67 65 20 73 65 67 6d 65 6e 74 73 20 28 73 65 65  ge segments (see
33a66 20 63 6f 6d 6d 65 6e 74 20 61 74 0a 2a 2a 20 74   comment at.** t
33a67 6f 70 20 6f 66 20 66 69 6c 65 29 2e 0a 2a 2f 0a  op of file)..*/.
33a68 23 64 65 66 69 6e 65 20 4d 45 52 47 45 5f 43 4f  #define MERGE_CO
33a69 55 4e 54 20 31 36 0a 0a 2f 2a 20 75 74 69 6c 69  UNT 16../* utili
33a6a 74 79 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a  ty functions */.
33a6b 0a 2f 2a 20 43 4c 45 41 52 28 29 20 61 6e 64 20  ./* CLEAR() and 
33a6c 53 43 52 41 4d 42 4c 45 28 29 20 61 62 73 74 72  SCRAMBLE() abstr
33a6d 61 63 74 20 6d 65 6d 73 65 74 28 29 20 6f 6e 20  act memset() on 
33a6e 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73  a pointer to a s
33a6f 69 6e 67 6c 65 0a 2a 2a 20 72 65 63 6f 72 64 20  ingle.** record 
33a70 74 6f 20 70 72 65 76 65 6e 74 20 65 72 72 6f 72  to prevent error
33a71 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a  s of the form:.*
33a72 2a 0a 2a 2a 20 6d 79 5f 66 75 6e 63 74 69 6f 6e  *.** my_function
33a73 28 53 6f 6d 65 54 79 70 65 20 2a 62 29 7b 0a 2a  (SomeType *b){.*
33a74 2a 20 20 20 6d 65 6d 73 65 74 28 62 2c 20 27 5c  *   memset(b, '\
33a75 30 27 2c 20 73 69 7a 65 6f 66 28 62 29 29 3b 20  0', sizeof(b)); 
33a76 20 2f 2f 20 73 69 7a 65 6f 66 28 62 29 21 3d 73   // sizeof(b)!=s
33a77 69 7a 65 6f 66 28 2a 62 29 0a 2a 2a 20 7d 0a 2a  izeof(*b).** }.*
33a78 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  /./* TODO(shess)
33a79 20 4f 62 76 69 6f 75 73 20 63 61 6e 64 69 64 61   Obvious candida
33a7a 74 65 73 20 66 6f 72 20 61 20 68 65 61 64 65 72  tes for a header
33a7b 20 66 69 6c 65 2e 20 2a 2f 0a 23 64 65 66 69 6e   file. */.#defin
33a7c 65 20 43 4c 45 41 52 28 62 29 20 6d 65 6d 73 65  e CLEAR(b) memse
33a7d 74 28 62 2c 20 27 5c 30 27 2c 20 73 69 7a 65 6f  t(b, '\0', sizeo
33a7e 66 28 2a 28 62 29 29 29 0a 0a 23 69 66 6e 64 65  f(*(b)))..#ifnde
33a7f 66 20 4e 44 45 42 55 47 0a 23 20 20 64 65 66 69  f NDEBUG.#  defi
33a80 6e 65 20 53 43 52 41 4d 42 4c 45 28 62 29 20 6d  ne SCRAMBLE(b) m
33a81 65 6d 73 65 74 28 62 2c 20 30 78 35 35 2c 20 73  emset(b, 0x55, s
33a82 69 7a 65 6f 66 28 2a 28 62 29 29 29 0a 23 65 6c  izeof(*(b))).#el
33a83 73 65 0a 23 20 20 64 65 66 69 6e 65 20 53 43 52  se.#  define SCR
33a84 41 4d 42 4c 45 28 62 29 0a 23 65 6e 64 69 66 0a  AMBLE(b).#endif.
33a85 0a 2f 2a 20 57 65 20 6d 61 79 20 6e 65 65 64 20  ./* We may need 
33a86 75 70 20 74 6f 20 56 41 52 49 4e 54 5f 4d 41 58  up to VARINT_MAX
33a87 20 62 79 74 65 73 20 74 6f 20 73 74 6f 72 65 20   bytes to store 
33a88 61 6e 20 65 6e 63 6f 64 65 64 20 36 34 2d 62 69  an encoded 64-bi
33a89 74 20 69 6e 74 65 67 65 72 2e 20 2a 2f 0a 23 64  t integer. */.#d
33a8a 65 66 69 6e 65 20 56 41 52 49 4e 54 5f 4d 41 58  efine VARINT_MAX
33a8b 20 31 30 0a 0a 2f 2a 20 57 72 69 74 65 20 61 20   10../* Write a 
33a8c 36 34 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d  64-bit variable-
33a8d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 74  length integer t
33a8e 6f 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e  o memory startin
33a8f 67 20 61 74 20 70 5b 30 5d 2e 0a 20 2a 20 54 68  g at p[0].. * Th
33a90 65 20 6c 65 6e 67 74 68 20 6f 66 20 64 61 74 61  e length of data
33a91 20 77 72 69 74 74 65 6e 20 77 69 6c 6c 20 62 65   written will be
33a92 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 56   between 1 and V
33a93 41 52 49 4e 54 5f 4d 41 58 20 62 79 74 65 73 2e  ARINT_MAX bytes.
33a94 0a 20 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f  . * The number o
33a95 66 20 62 79 74 65 73 20 77 72 69 74 74 65 6e 20  f bytes written 
33a96 69 73 20 72 65 74 75 72 6e 65 64 2e 20 2a 2f 0a  is returned. */.
33a97 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 50  static int fts3P
33a98 75 74 56 61 72 69 6e 74 28 63 68 61 72 20 2a 70  utVarint(char *p
33a99 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 76  , sqlite_int64 v
33a9a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  ){.  unsigned ch
33a9b 61 72 20 2a 71 20 3d 20 28 75 6e 73 69 67 6e 65  ar *q = (unsigne
33a9c 64 20 63 68 61 72 20 2a 29 20 70 3b 0a 20 20 73  d char *) p;.  s
33a9d 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 76 75 20  qlite_uint64 vu 
33a9e 3d 20 76 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 2a  = v;.  do{.    *
33a9f 71 2b 2b 20 3d 20 28 75 6e 73 69 67 6e 65 64 20  q++ = (unsigned 
33aa0 63 68 61 72 29 20 28 28 76 75 20 26 20 30 78 37  char) ((vu & 0x7
33aa1 66 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 20  f) | 0x80);.    
33aa2 76 75 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69  vu >>= 7;.  }whi
33aa3 6c 65 28 20 76 75 21 3d 30 20 29 3b 0a 20 20 71  le( vu!=0 );.  q
33aa4 5b 2d 31 5d 20 26 3d 20 30 78 37 66 3b 20 20 2f  [-1] &= 0x7f;  /
33aa5 2a 20 74 75 72 6e 20 6f 66 66 20 68 69 67 68 20  * turn off high 
33aa6 62 69 74 20 69 6e 20 66 69 6e 61 6c 20 62 79 74  bit in final byt
33aa7 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 71  e */.  assert( q
33aa8 20 2d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   - (unsigned cha
33aa9 72 20 2a 29 70 20 3c 3d 20 56 41 52 49 4e 54 5f  r *)p <= VARINT_
33aaa 4d 41 58 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  MAX );.  return 
33aab 28 69 6e 74 29 20 28 71 20 2d 20 28 75 6e 73 69  (int) (q - (unsi
33aac 67 6e 65 64 20 63 68 61 72 20 2a 29 70 29 3b 0a  gned char *)p);.
33aad 7d 0a 0a 2f 2a 20 52 65 61 64 20 61 20 36 34 2d  }../* Read a 64-
33aae 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e  bit variable-len
33aaf 67 74 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d  gth integer from
33ab0 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67   memory starting
33ab1 20 61 74 20 70 5b 30 5d 2e 0a 20 2a 20 52 65 74   at p[0].. * Ret
33ab2 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
33ab3 66 20 62 79 74 65 73 20 72 65 61 64 2c 20 6f 72  f bytes read, or
33ab4 20 30 20 6f 6e 20 65 72 72 6f 72 2e 0a 20 2a 20   0 on error.. * 
33ab5 54 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f  The value is sto
33ab6 72 65 64 20 69 6e 20 2a 76 2e 20 2a 2f 0a 73 74  red in *v. */.st
33ab7 61 74 69 63 20 69 6e 74 20 66 74 73 33 47 65 74  atic int fts3Get
33ab8 56 61 72 69 6e 74 28 63 6f 6e 73 74 20 63 68 61  Varint(const cha
33ab9 72 20 2a 70 2c 20 73 71 6c 69 74 65 5f 69 6e 74  r *p, sqlite_int
33aba 36 34 20 2a 76 29 7b 0a 20 20 63 6f 6e 73 74 20  64 *v){.  const 
33abb 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 71  unsigned char *q
33abc 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e   = (const unsign
33abd 65 64 20 63 68 61 72 20 2a 29 20 70 3b 0a 20 20  ed char *) p;.  
33abe 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 78 20  sqlite_uint64 x 
33abf 3d 20 30 2c 20 79 20 3d 20 31 3b 0a 20 20 77 68  = 0, y = 1;.  wh
33ac0 69 6c 65 28 20 28 2a 71 20 26 20 30 78 38 30 29  ile( (*q & 0x80)
33ac1 20 3d 3d 20 30 78 38 30 20 29 7b 0a 20 20 20 20   == 0x80 ){.    
33ac2 78 20 2b 3d 20 79 20 2a 20 28 2a 71 2b 2b 20 26  x += y * (*q++ &
33ac3 20 30 78 37 66 29 3b 0a 20 20 20 20 79 20 3c 3c   0x7f);.    y <<
33ac4 3d 20 37 3b 0a 20 20 20 20 69 66 28 20 71 20 2d  = 7;.    if( q -
33ac5 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
33ac6 2a 29 70 20 3e 3d 20 56 41 52 49 4e 54 5f 4d 41  *)p >= VARINT_MA
33ac7 58 20 29 7b 20 20 2f 2a 20 62 61 64 20 64 61 74  X ){  /* bad dat
33ac8 61 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72  a */.      asser
33ac9 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20 72 65  t( 0 );.      re
33aca 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
33acb 7d 0a 20 20 78 20 2b 3d 20 79 20 2a 20 28 2a 71  }.  x += y * (*q
33acc 2b 2b 29 3b 0a 20 20 2a 76 20 3d 20 28 73 71 6c  ++);.  *v = (sql
33acd 69 74 65 5f 69 6e 74 36 34 29 20 78 3b 0a 20 20  ite_int64) x;.  
33ace 72 65 74 75 72 6e 20 28 69 6e 74 29 20 28 71 20  return (int) (q 
33acf 2d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  - (unsigned char
33ad0 20 2a 29 70 29 3b 0a 7d 0a 0a 73 74 61 74 69 63   *)p);.}..static
33ad1 20 69 6e 74 20 66 74 73 33 47 65 74 56 61 72 69   int fts3GetVari
33ad2 6e 74 33 32 28 63 6f 6e 73 74 20 63 68 61 72 20  nt32(const char 
33ad3 2a 70 2c 20 69 6e 74 20 2a 70 69 29 7b 0a 20 73  *p, int *pi){. s
33ad4 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 3b 0a 20  qlite_int64 i;. 
33ad5 69 6e 74 20 72 65 74 20 3d 20 66 74 73 33 47 65  int ret = fts3Ge
33ad6 74 56 61 72 69 6e 74 28 70 2c 20 26 69 29 3b 0a  tVarint(p, &i);.
33ad7 20 2a 70 69 20 3d 20 28 69 6e 74 29 20 69 3b 0a   *pi = (int) i;.
33ad8 20 61 73 73 65 72 74 28 20 2a 70 69 3d 3d 69 20   assert( *pi==i 
33ad9 29 3b 0a 20 72 65 74 75 72 6e 20 72 65 74 3b 0a  );. return ret;.
33ada 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
33adb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33adc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33add 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33ade 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44 61 74 61  *******/./* Data
33adf 42 75 66 66 65 72 20 69 73 20 75 73 65 64 20 74  Buffer is used t
33ae0 6f 20 63 6f 6c 6c 65 63 74 20 64 61 74 61 20 69  o collect data i
33ae1 6e 74 6f 20 61 20 62 75 66 66 65 72 20 69 6e 20  nto a buffer in 
33ae2 70 69 65 63 65 6d 65 61 6c 0a 2a 2a 20 66 61 73  piecemeal.** fas
33ae3 68 69 6f 6e 2e 20 20 49 74 20 69 6d 70 6c 65 6d  hion.  It implem
33ae4 65 6e 74 73 20 74 68 65 20 75 73 75 61 6c 20 64  ents the usual d
33ae5 69 73 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65  istinction betwe
33ae6 65 6e 20 61 6d 6f 75 6e 74 20 6f 66 0a 2a 2a 20  en amount of.** 
33ae7 64 61 74 61 20 63 75 72 72 65 6e 74 6c 79 20 73  data currently s
33ae8 74 6f 72 65 64 20 28 6e 44 61 74 61 29 20 61 6e  tored (nData) an
33ae9 64 20 62 75 66 66 65 72 20 63 61 70 61 63 69 74  d buffer capacit
33aea 79 20 28 6e 43 61 70 61 63 69 74 79 29 2e 0a 2a  y (nCapacity)..*
33aeb 2a 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72 49  *.** dataBufferI
33aec 6e 69 74 20 2d 20 63 72 65 61 74 65 20 61 20 62  nit - create a b
33aed 75 66 66 65 72 20 77 69 74 68 20 67 69 76 65 6e  uffer with given
33aee 20 69 6e 69 74 69 61 6c 20 63 61 70 61 63 69 74   initial capacit
33aef 79 2e 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72  y..** dataBuffer
33af0 52 65 73 65 74 20 2d 20 66 6f 72 67 65 74 20 62  Reset - forget b
33af1 75 66 66 65 72 27 73 20 64 61 74 61 2c 20 72 65  uffer's data, re
33af2 74 61 69 6e 69 6e 67 20 63 61 70 61 63 69 74 79  taining capacity
33af3 2e 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72 44  ..** dataBufferD
33af4 65 73 74 72 6f 79 20 2d 20 66 72 65 65 20 62 75  estroy - free bu
33af5 66 66 65 72 27 73 20 64 61 74 61 2e 0a 2a 2a 20  ffer's data..** 
33af6 64 61 74 61 42 75 66 66 65 72 53 77 61 70 20 2d  dataBufferSwap -
33af7 20 73 77 61 70 20 63 6f 6e 74 65 6e 74 73 20 6f   swap contents o
33af8 66 20 74 77 6f 20 62 75 66 66 65 72 73 2e 0a 2a  f two buffers..*
33af9 2a 20 64 61 74 61 42 75 66 66 65 72 45 78 70 61  * dataBufferExpa
33afa 6e 64 20 2d 20 65 78 70 61 6e 64 20 63 61 70 61  nd - expand capa
33afb 63 69 74 79 20 77 69 74 68 6f 75 74 20 61 64 64  city without add
33afc 69 6e 67 20 64 61 74 61 2e 0a 2a 2a 20 64 61 74  ing data..** dat
33afd 61 42 75 66 66 65 72 41 70 70 65 6e 64 20 2d 20  aBufferAppend - 
33afe 61 70 70 65 6e 64 20 64 61 74 61 2e 0a 2a 2a 20  append data..** 
33aff 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
33b00 32 20 2d 20 61 70 70 65 6e 64 20 74 77 6f 20 70  2 - append two p
33b01 69 65 63 65 73 20 6f 66 20 64 61 74 61 20 61 74  ieces of data at
33b02 20 6f 6e 63 65 2e 0a 2a 2a 20 64 61 74 61 42 75   once..** dataBu
33b03 66 66 65 72 52 65 70 6c 61 63 65 20 2d 20 72 65  fferReplace - re
33b04 70 6c 61 63 65 20 62 75 66 66 65 72 27 73 20 64  place buffer's d
33b05 61 74 61 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ata..*/.typedef 
33b06 73 74 72 75 63 74 20 44 61 74 61 42 75 66 66 65  struct DataBuffe
33b07 72 20 7b 0a 20 20 63 68 61 72 20 2a 70 44 61 74  r {.  char *pDat
33b08 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  a;          /* P
33b09 6f 69 6e 74 65 72 20 74 6f 20 6d 61 6c 6c 6f 63  ointer to malloc
33b0a 27 65 64 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20  'ed buffer. */. 
33b0b 20 69 6e 74 20 6e 43 61 70 61 63 69 74 79 3b 20   int nCapacity; 
33b0c 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
33b0d 66 20 70 44 61 74 61 20 62 75 66 66 65 72 2e 20  f pData buffer. 
33b0e 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 20  */.  int nData; 
33b0f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e             /* En
33b10 64 20 6f 66 20 64 61 74 61 20 6c 6f 61 64 65 64  d of data loaded
33b11 20 69 6e 74 6f 20 70 44 61 74 61 2e 20 2a 2f 0a   into pData. */.
33b12 7d 20 44 61 74 61 42 75 66 66 65 72 3b 0a 0a 73  } DataBuffer;..s
33b13 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 61 42  tatic void dataB
33b14 75 66 66 65 72 49 6e 69 74 28 44 61 74 61 42 75  ufferInit(DataBu
33b15 66 66 65 72 20 2a 70 42 75 66 66 65 72 2c 20 69  ffer *pBuffer, i
33b16 6e 74 20 6e 43 61 70 61 63 69 74 79 29 7b 0a 20  nt nCapacity){. 
33b17 20 61 73 73 65 72 74 28 20 6e 43 61 70 61 63 69   assert( nCapaci
33b18 74 79 3e 3d 30 20 29 3b 0a 20 20 70 42 75 66 66  ty>=0 );.  pBuff
33b19 65 72 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 20  er->nData = 0;. 
33b1a 20 70 42 75 66 66 65 72 2d 3e 6e 43 61 70 61 63   pBuffer->nCapac
33b1b 69 74 79 20 3d 20 6e 43 61 70 61 63 69 74 79 3b  ity = nCapacity;
33b1c 0a 20 20 70 42 75 66 66 65 72 2d 3e 70 44 61 74  .  pBuffer->pDat
33b1d 61 20 3d 20 6e 43 61 70 61 63 69 74 79 3d 3d 30  a = nCapacity==0
33b1e 20 3f 20 4e 55 4c 4c 20 3a 20 73 71 6c 69 74 65   ? NULL : sqlite
33b1f 33 5f 6d 61 6c 6c 6f 63 28 6e 43 61 70 61 63 69  3_malloc(nCapaci
33b20 74 79 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  ty);.}.static vo
33b21 69 64 20 64 61 74 61 42 75 66 66 65 72 52 65 73  id dataBufferRes
33b22 65 74 28 44 61 74 61 42 75 66 66 65 72 20 2a 70  et(DataBuffer *p
33b23 42 75 66 66 65 72 29 7b 0a 20 20 70 42 75 66 66  Buffer){.  pBuff
33b24 65 72 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 7d  er->nData = 0;.}
33b25 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74  .static void dat
33b26 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 44  aBufferDestroy(D
33b27 61 74 61 42 75 66 66 65 72 20 2a 70 42 75 66 66  ataBuffer *pBuff
33b28 65 72 29 7b 0a 20 20 69 66 28 20 70 42 75 66 66  er){.  if( pBuff
33b29 65 72 2d 3e 70 44 61 74 61 21 3d 4e 55 4c 4c 20  er->pData!=NULL 
33b2a 29 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70  ) sqlite3_free(p
33b2b 42 75 66 66 65 72 2d 3e 70 44 61 74 61 29 3b 0a  Buffer->pData);.
33b2c 20 20 53 43 52 41 4d 42 4c 45 28 70 42 75 66 66    SCRAMBLE(pBuff
33b2d 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  er);.}.static vo
33b2e 69 64 20 64 61 74 61 42 75 66 66 65 72 53 77 61  id dataBufferSwa
33b2f 70 28 44 61 74 61 42 75 66 66 65 72 20 2a 70 42  p(DataBuffer *pB
33b30 75 66 66 65 72 31 2c 20 44 61 74 61 42 75 66 66  uffer1, DataBuff
33b31 65 72 20 2a 70 42 75 66 66 65 72 32 29 7b 0a 20  er *pBuffer2){. 
33b32 20 44 61 74 61 42 75 66 66 65 72 20 74 6d 70 20   DataBuffer tmp 
33b33 3d 20 2a 70 42 75 66 66 65 72 31 3b 0a 20 20 2a  = *pBuffer1;.  *
33b34 70 42 75 66 66 65 72 31 20 3d 20 2a 70 42 75 66  pBuffer1 = *pBuf
33b35 66 65 72 32 3b 0a 20 20 2a 70 42 75 66 66 65 72  fer2;.  *pBuffer
33b36 32 20 3d 20 74 6d 70 3b 0a 7d 0a 73 74 61 74 69  2 = tmp;.}.stati
33b37 63 20 76 6f 69 64 20 64 61 74 61 42 75 66 66 65  c void dataBuffe
33b38 72 45 78 70 61 6e 64 28 44 61 74 61 42 75 66 66  rExpand(DataBuff
33b39 65 72 20 2a 70 42 75 66 66 65 72 2c 20 69 6e 74  er *pBuffer, int
33b3a 20 6e 41 64 64 43 61 70 61 63 69 74 79 29 7b 0a   nAddCapacity){.
33b3b 20 20 61 73 73 65 72 74 28 20 6e 41 64 64 43 61    assert( nAddCa
33b3c 70 61 63 69 74 79 3e 30 20 29 3b 0a 20 20 2f 2a  pacity>0 );.  /*
33b3d 20 54 4f 44 4f 28 73 68 65 73 73 29 20 43 6f 6e   TODO(shess) Con
33b3e 73 69 64 65 72 20 65 78 70 61 6e 64 69 6e 67 20  sider expanding 
33b3f 6d 6f 72 65 20 61 67 67 72 65 73 73 69 76 65 6c  more aggressivel
33b40 79 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68  y.  Note that th
33b41 65 0a 20 20 2a 2a 20 75 6e 64 65 72 6c 79 69 6e  e.  ** underlyin
33b42 67 20 6d 61 6c 6c 6f 63 20 69 6d 70 6c 65 6d 65  g malloc impleme
33b43 6e 74 61 74 69 6f 6e 20 6d 61 79 20 74 61 6b 65  ntation may take
33b44 20 63 61 72 65 20 6f 66 20 73 75 63 68 20 74 68   care of such th
33b45 69 6e 67 73 20 66 6f 72 0a 20 20 2a 2a 20 75 73  ings for.  ** us
33b46 20 61 6c 72 65 61 64 79 2e 0a 20 20 2a 2f 0a 20   already..  */. 
33b47 20 69 66 28 20 70 42 75 66 66 65 72 2d 3e 6e 44   if( pBuffer->nD
33b48 61 74 61 2b 6e 41 64 64 43 61 70 61 63 69 74 79  ata+nAddCapacity
33b49 3e 70 42 75 66 66 65 72 2d 3e 6e 43 61 70 61 63  >pBuffer->nCapac
33b4a 69 74 79 20 29 7b 0a 20 20 20 20 70 42 75 66 66  ity ){.    pBuff
33b4b 65 72 2d 3e 6e 43 61 70 61 63 69 74 79 20 3d 20  er->nCapacity = 
33b4c 70 42 75 66 66 65 72 2d 3e 6e 44 61 74 61 2b 6e  pBuffer->nData+n
33b4d 41 64 64 43 61 70 61 63 69 74 79 3b 0a 20 20 20  AddCapacity;.   
33b4e 20 70 42 75 66 66 65 72 2d 3e 70 44 61 74 61 20   pBuffer->pData 
33b4f 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  = sqlite3_reallo
33b50 63 28 70 42 75 66 66 65 72 2d 3e 70 44 61 74 61  c(pBuffer->pData
33b51 2c 20 70 42 75 66 66 65 72 2d 3e 6e 43 61 70 61  , pBuffer->nCapa
33b52 63 69 74 79 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61  city);.  }.}.sta
33b53 74 69 63 20 76 6f 69 64 20 64 61 74 61 42 75 66  tic void dataBuf
33b54 66 65 72 41 70 70 65 6e 64 28 44 61 74 61 42 75  ferAppend(DataBu
33b55 66 66 65 72 20 2a 70 42 75 66 66 65 72 2c 0a 20  ffer *pBuffer,. 
33b56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33b57 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
33b58 74 20 63 68 61 72 20 2a 70 53 6f 75 72 63 65 2c  t char *pSource,
33b59 20 69 6e 74 20 6e 53 6f 75 72 63 65 29 7b 0a 20   int nSource){. 
33b5a 20 61 73 73 65 72 74 28 20 6e 53 6f 75 72 63 65   assert( nSource
33b5b 3e 30 20 26 26 20 70 53 6f 75 72 63 65 21 3d 4e  >0 && pSource!=N
33b5c 55 4c 4c 20 29 3b 0a 20 20 64 61 74 61 42 75 66  ULL );.  dataBuf
33b5d 66 65 72 45 78 70 61 6e 64 28 70 42 75 66 66 65  ferExpand(pBuffe
33b5e 72 2c 20 6e 53 6f 75 72 63 65 29 3b 0a 20 20 6d  r, nSource);.  m
33b5f 65 6d 63 70 79 28 70 42 75 66 66 65 72 2d 3e 70  emcpy(pBuffer->p
33b60 44 61 74 61 2b 70 42 75 66 66 65 72 2d 3e 6e 44  Data+pBuffer->nD
33b61 61 74 61 2c 20 70 53 6f 75 72 63 65 2c 20 6e 53  ata, pSource, nS
33b62 6f 75 72 63 65 29 3b 0a 20 20 70 42 75 66 66 65  ource);.  pBuffe
33b63 72 2d 3e 6e 44 61 74 61 20 2b 3d 20 6e 53 6f 75  r->nData += nSou
33b64 72 63 65 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  rce;.}.static vo
33b65 69 64 20 64 61 74 61 42 75 66 66 65 72 41 70 70  id dataBufferApp
33b66 65 6e 64 32 28 44 61 74 61 42 75 66 66 65 72 20  end2(DataBuffer 
33b67 2a 70 42 75 66 66 65 72 2c 0a 20 20 20 20 20 20  *pBuffer,.      
33b68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33b69 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
33b6a 61 72 20 2a 70 53 6f 75 72 63 65 31 2c 20 69 6e  ar *pSource1, in
33b6b 74 20 6e 53 6f 75 72 63 65 31 2c 0a 20 20 20 20  t nSource1,.    
33b6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33b6d 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
33b6e 63 68 61 72 20 2a 70 53 6f 75 72 63 65 32 2c 20  char *pSource2, 
33b6f 69 6e 74 20 6e 53 6f 75 72 63 65 32 29 7b 0a 20  int nSource2){. 
33b70 20 61 73 73 65 72 74 28 20 6e 53 6f 75 72 63 65   assert( nSource
33b71 31 3e 30 20 26 26 20 70 53 6f 75 72 63 65 31 21  1>0 && pSource1!
33b72 3d 4e 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65 72  =NULL );.  asser
33b73 74 28 20 6e 53 6f 75 72 63 65 32 3e 30 20 26 26  t( nSource2>0 &&
33b74 20 70 53 6f 75 72 63 65 32 21 3d 4e 55 4c 4c 20   pSource2!=NULL 
33b75 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 45  );.  dataBufferE
33b76 78 70 61 6e 64 28 70 42 75 66 66 65 72 2c 20 6e  xpand(pBuffer, n
33b77 53 6f 75 72 63 65 31 2b 6e 53 6f 75 72 63 65 32  Source1+nSource2
33b78 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 42 75 66  );.  memcpy(pBuf
33b79 66 65 72 2d 3e 70 44 61 74 61 2b 70 42 75 66 66  fer->pData+pBuff
33b7a 65 72 2d 3e 6e 44 61 74 61 2c 20 70 53 6f 75 72  er->nData, pSour
33b7b 63 65 31 2c 20 6e 53 6f 75 72 63 65 31 29 3b 0a  ce1, nSource1);.
33b7c 20 20 6d 65 6d 63 70 79 28 70 42 75 66 66 65 72    memcpy(pBuffer
33b7d 2d 3e 70 44 61 74 61 2b 70 42 75 66 66 65 72 2d  ->pData+pBuffer-
33b7e 3e 6e 44 61 74 61 2b 6e 53 6f 75 72 63 65 31 2c  >nData+nSource1,
33b7f 20 70 53 6f 75 72 63 65 32 2c 20 6e 53 6f 75 72   pSource2, nSour
33b80 63 65 32 29 3b 0a 20 20 70 42 75 66 66 65 72 2d  ce2);.  pBuffer-
33b81 3e 6e 44 61 74 61 20 2b 3d 20 6e 53 6f 75 72 63  >nData += nSourc
33b82 65 31 2b 6e 53 6f 75 72 63 65 32 3b 0a 7d 0a 73  e1+nSource2;.}.s
33b83 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 61 42  tatic void dataB
33b84 75 66 66 65 72 52 65 70 6c 61 63 65 28 44 61 74  ufferReplace(Dat
33b85 61 42 75 66 66 65 72 20 2a 70 42 75 66 66 65 72  aBuffer *pBuffer
33b86 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
33b87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33b88 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 53 6f 75  const char *pSou
33b89 72 63 65 2c 20 69 6e 74 20 6e 53 6f 75 72 63 65  rce, int nSource
33b8a 29 7b 0a 20 20 64 61 74 61 42 75 66 66 65 72 52  ){.  dataBufferR
33b8b 65 73 65 74 28 70 42 75 66 66 65 72 29 3b 0a 20  eset(pBuffer);. 
33b8c 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
33b8d 64 28 70 42 75 66 66 65 72 2c 20 70 53 6f 75 72  d(pBuffer, pSour
33b8e 63 65 2c 20 6e 53 6f 75 72 63 65 29 3b 0a 7d 0a  ce, nSource);.}.
33b8f 0a 2f 2a 20 53 74 72 69 6e 67 42 75 66 66 65 72  ./* StringBuffer
33b90 20 69 73 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69   is a null-termi
33b91 6e 61 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66  nated version of
33b92 20 44 61 74 61 42 75 66 66 65 72 2e 20 2a 2f 0a   DataBuffer. */.
33b93 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53  typedef struct S
33b94 74 72 69 6e 67 42 75 66 66 65 72 20 7b 0a 20 20  tringBuffer {.  
33b95 44 61 74 61 42 75 66 66 65 72 20 62 3b 20 20 20  DataBuffer b;   
33b96 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 6c           /* Incl
33b97 75 64 65 73 20 6e 75 6c 6c 20 74 65 72 6d 69 6e  udes null termin
33b98 61 74 6f 72 2e 20 2a 2f 0a 7d 20 53 74 72 69 6e  ator. */.} Strin
33b99 67 42 75 66 66 65 72 3b 0a 0a 73 74 61 74 69 63  gBuffer;..static
33b9a 20 76 6f 69 64 20 69 6e 69 74 53 74 72 69 6e 67   void initString
33b9b 42 75 66 66 65 72 28 53 74 72 69 6e 67 42 75 66  Buffer(StringBuf
33b9c 66 65 72 20 2a 73 62 29 7b 0a 20 20 64 61 74 61  fer *sb){.  data
33b9d 42 75 66 66 65 72 49 6e 69 74 28 26 73 62 2d 3e  BufferInit(&sb->
33b9e 62 2c 20 31 30 30 29 3b 0a 20 20 64 61 74 61 42  b, 100);.  dataB
33b9f 75 66 66 65 72 52 65 70 6c 61 63 65 28 26 73 62  ufferReplace(&sb
33ba0 2d 3e 62 2c 20 22 22 2c 20 31 29 3b 0a 7d 0a 73  ->b, "", 1);.}.s
33ba1 74 61 74 69 63 20 69 6e 74 20 73 74 72 69 6e 67  tatic int string
33ba2 42 75 66 66 65 72 4c 65 6e 67 74 68 28 53 74 72  BufferLength(Str
33ba3 69 6e 67 42 75 66 66 65 72 20 2a 73 62 29 7b 0a  ingBuffer *sb){.
33ba4 20 20 72 65 74 75 72 6e 20 73 62 2d 3e 62 2e 6e    return sb->b.n
33ba5 44 61 74 61 2d 31 3b 0a 7d 0a 73 74 61 74 69 63  Data-1;.}.static
33ba6 20 63 68 61 72 20 2a 73 74 72 69 6e 67 42 75 66   char *stringBuf
33ba7 66 65 72 44 61 74 61 28 53 74 72 69 6e 67 42 75  ferData(StringBu
33ba8 66 66 65 72 20 2a 73 62 29 7b 0a 20 20 72 65 74  ffer *sb){.  ret
33ba9 75 72 6e 20 73 62 2d 3e 62 2e 70 44 61 74 61 3b  urn sb->b.pData;
33baa 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  .}.static void s
33bab 74 72 69 6e 67 42 75 66 66 65 72 44 65 73 74 72  tringBufferDestr
33bac 6f 79 28 53 74 72 69 6e 67 42 75 66 66 65 72 20  oy(StringBuffer 
33bad 2a 73 62 29 7b 0a 20 20 64 61 74 61 42 75 66 66  *sb){.  dataBuff
33bae 65 72 44 65 73 74 72 6f 79 28 26 73 62 2d 3e 62  erDestroy(&sb->b
33baf 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
33bb0 64 20 6e 61 70 70 65 6e 64 28 53 74 72 69 6e 67  d nappend(String
33bb1 42 75 66 66 65 72 20 2a 73 62 2c 20 63 6f 6e 73  Buffer *sb, cons
33bb2 74 20 63 68 61 72 20 2a 7a 46 72 6f 6d 2c 20 69  t char *zFrom, i
33bb3 6e 74 20 6e 46 72 6f 6d 29 7b 0a 20 20 61 73 73  nt nFrom){.  ass
33bb4 65 72 74 28 20 73 62 2d 3e 62 2e 6e 44 61 74 61  ert( sb->b.nData
33bb5 3e 30 20 29 3b 0a 20 20 69 66 28 20 6e 46 72 6f  >0 );.  if( nFro
33bb6 6d 3e 30 20 29 7b 0a 20 20 20 20 73 62 2d 3e 62  m>0 ){.    sb->b
33bb7 2e 6e 44 61 74 61 2d 2d 3b 0a 20 20 20 20 64 61  .nData--;.    da
33bb8 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32 28  taBufferAppend2(
33bb9 26 73 62 2d 3e 62 2c 20 7a 46 72 6f 6d 2c 20 6e  &sb->b, zFrom, n
33bba 46 72 6f 6d 2c 20 22 22 2c 20 31 29 3b 0a 20 20  From, "", 1);.  
33bbb 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  }.}.static void 
33bbc 61 70 70 65 6e 64 28 53 74 72 69 6e 67 42 75 66  append(StringBuf
33bbd 66 65 72 20 2a 73 62 2c 20 63 6f 6e 73 74 20 63  fer *sb, const c
33bbe 68 61 72 20 2a 7a 46 72 6f 6d 29 7b 0a 20 20 6e  har *zFrom){.  n
33bbf 61 70 70 65 6e 64 28 73 62 2c 20 7a 46 72 6f 6d  append(sb, zFrom
33bc0 2c 20 73 74 72 6c 65 6e 28 7a 46 72 6f 6d 29 29  , strlen(zFrom))
33bc1 3b 0a 7d 0a 0a 2f 2a 20 41 70 70 65 6e 64 20 61  ;.}../* Append a
33bc2 20 6c 69 73 74 20 6f 66 20 73 74 72 69 6e 67 73   list of strings
33bc3 20 73 65 70 61 72 61 74 65 64 20 62 79 20 63 6f   separated by co
33bc4 6d 6d 61 73 2e 20 2a 2f 0a 73 74 61 74 69 63 20  mmas. */.static 
33bc5 76 6f 69 64 20 61 70 70 65 6e 64 4c 69 73 74 28  void appendList(
33bc6 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a 73 62  StringBuffer *sb
33bc7 2c 20 69 6e 74 20 6e 53 74 72 69 6e 67 2c 20 63  , int nString, c
33bc8 68 61 72 20 2a 2a 61 7a 53 74 72 69 6e 67 29 7b  har **azString){
33bc9 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28  .  int i;.  for(
33bca 69 3d 30 3b 20 69 3c 6e 53 74 72 69 6e 67 3b 20  i=0; i<nString; 
33bcb 2b 2b 69 29 7b 0a 20 20 20 20 69 66 28 20 69 3e  ++i){.    if( i>
33bcc 30 20 29 20 61 70 70 65 6e 64 28 73 62 2c 20 22  0 ) append(sb, "
33bcd 2c 20 22 29 3b 0a 20 20 20 20 61 70 70 65 6e 64  , ");.    append
33bce 28 73 62 2c 20 61 7a 53 74 72 69 6e 67 5b 69 5d  (sb, azString[i]
33bcf 29 3b 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63  );.  }.}..static
33bd0 20 69 6e 74 20 65 6e 64 73 49 6e 57 68 69 74 65   int endsInWhite
33bd1 53 70 61 63 65 28 53 74 72 69 6e 67 42 75 66 66  Space(StringBuff
33bd2 65 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  er *p){.  return
33bd3 20 73 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e   stringBufferLen
33bd4 67 74 68 28 70 29 3e 30 20 26 26 0a 20 20 20 20  gth(p)>0 &&.    
33bd5 73 61 66 65 5f 69 73 73 70 61 63 65 28 73 74 72  safe_isspace(str
33bd6 69 6e 67 42 75 66 66 65 72 44 61 74 61 28 70 29  ingBufferData(p)
33bd7 5b 73 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e  [stringBufferLen
33bd8 67 74 68 28 70 29 2d 31 5d 29 3b 0a 7d 0a 0a 2f  gth(p)-1]);.}../
33bd9 2a 20 49 66 20 74 68 65 20 53 74 72 69 6e 67 42  * If the StringB
33bda 75 66 66 65 72 20 65 6e 64 73 20 69 6e 20 73 6f  uffer ends in so
33bdb 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68  mething other th
33bdc 61 6e 20 77 68 69 74 65 20 73 70 61 63 65 2c 20  an white space, 
33bdd 61 64 64 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20  add a.** single 
33bde 73 70 61 63 65 20 63 68 61 72 61 63 74 65 72 20  space character 
33bdf 74 6f 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73  to the end..*/.s
33be0 74 61 74 69 63 20 76 6f 69 64 20 61 70 70 65 6e  tatic void appen
33be1 64 57 68 69 74 65 53 70 61 63 65 28 53 74 72 69  dWhiteSpace(Stri
33be2 6e 67 42 75 66 66 65 72 20 2a 70 29 7b 0a 20 20  ngBuffer *p){.  
33be3 69 66 28 20 73 74 72 69 6e 67 42 75 66 66 65 72  if( stringBuffer
33be4 4c 65 6e 67 74 68 28 70 29 3d 3d 30 20 29 20 72  Length(p)==0 ) r
33be5 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 65 6e  eturn;.  if( !en
33be6 64 73 49 6e 57 68 69 74 65 53 70 61 63 65 28 70  dsInWhiteSpace(p
33be7 29 20 29 20 61 70 70 65 6e 64 28 70 2c 20 22 20  ) ) append(p, " 
33be8 22 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65  ");.}../* Remove
33be9 20 77 68 69 74 65 20 73 70 61 63 65 20 66 72 6f   white space fro
33bea 6d 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  m the end of the
33beb 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a 2f   StringBuffer */
33bec 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 72 69  .static void tri
33bed 6d 57 68 69 74 65 53 70 61 63 65 28 53 74 72 69  mWhiteSpace(Stri
33bee 6e 67 42 75 66 66 65 72 20 2a 70 29 7b 0a 20 20  ngBuffer *p){.  
33bef 77 68 69 6c 65 28 20 65 6e 64 73 49 6e 57 68 69  while( endsInWhi
33bf0 74 65 53 70 61 63 65 28 70 29 20 29 7b 0a 20 20  teSpace(p) ){.  
33bf1 20 20 70 2d 3e 62 2e 70 44 61 74 61 5b 2d 2d 70    p->b.pData[--p
33bf2 2d 3e 62 2e 6e 44 61 74 61 2d 31 5d 20 3d 20 27  ->b.nData-1] = '
33bf3 5c 30 27 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a  \0';.  }.}../***
33bf4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33bf5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33bf6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33bf7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33bf8 2f 0a 2f 2a 20 44 4c 52 65 61 64 65 72 20 69 73  /./* DLReader is
33bf9 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 64 6f   used to read do
33bfa 63 75 6d 65 6e 74 20 65 6c 65 6d 65 6e 74 73 20  cument elements 
33bfb 66 72 6f 6d 20 61 20 64 6f 63 6c 69 73 74 2e 20  from a doclist. 
33bfc 20 54 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20   The.** current 
33bfd 64 6f 63 69 64 20 69 73 20 63 61 63 68 65 64 2c  docid is cached,
33bfe 20 73 6f 20 64 6c 72 44 6f 63 69 64 28 29 20 69   so dlrDocid() i
33bff 73 20 66 61 73 74 2e 20 20 44 4c 52 65 61 64 65  s fast.  DLReade
33c00 72 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6f 77  r does not.** ow
33c01 6e 20 74 68 65 20 64 6f 63 6c 69 73 74 20 62 75  n the doclist bu
33c02 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 64 6c 72 41  ffer..**.** dlrA
33c03 74 45 6e 64 20 2d 20 74 72 75 65 20 69 66 20 74  tEnd - true if t
33c04 68 65 72 65 27 73 20 6e 6f 20 6d 6f 72 65 20 64  here's no more d
33c05 61 74 61 20 74 6f 20 72 65 61 64 2e 0a 2a 2a 20  ata to read..** 
33c06 64 6c 72 44 6f 63 69 64 20 2d 20 64 6f 63 69 64  dlrDocid - docid
33c07 20 6f 66 20 63 75 72 72 65 6e 74 20 64 6f 63 75   of current docu
33c08 6d 65 6e 74 2e 0a 2a 2a 20 64 6c 72 44 6f 63 44  ment..** dlrDocD
33c09 61 74 61 20 2d 20 64 6f 63 6c 69 73 74 20 64 61  ata - doclist da
33c0a 74 61 20 66 6f 72 20 63 75 72 72 65 6e 74 20 64  ta for current d
33c0b 6f 63 75 6d 65 6e 74 20 28 69 6e 63 6c 75 64 69  ocument (includi
33c0c 6e 67 20 64 6f 63 69 64 29 2e 0a 2a 2a 20 64 6c  ng docid)..** dl
33c0d 72 44 6f 63 44 61 74 61 42 79 74 65 73 20 2d 20  rDocDataBytes - 
33c0e 6c 65 6e 67 74 68 20 6f 66 20 73 61 6d 65 2e 0a  length of same..
33c0f 2a 2a 20 64 6c 72 41 6c 6c 44 61 74 61 42 79 74  ** dlrAllDataByt
33c10 65 73 20 2d 20 6c 65 6e 67 74 68 20 6f 66 20 61  es - length of a
33c11 6c 6c 20 72 65 6d 61 69 6e 69 6e 67 20 64 61 74  ll remaining dat
33c12 61 2e 0a 2a 2a 20 64 6c 72 50 6f 73 44 61 74 61  a..** dlrPosData
33c13 20 2d 20 70 6f 73 69 74 69 6f 6e 20 64 61 74 61   - position data
33c14 20 66 6f 72 20 63 75 72 72 65 6e 74 20 64 6f 63   for current doc
33c15 75 6d 65 6e 74 2e 0a 2a 2a 20 64 6c 72 50 6f 73  ument..** dlrPos
33c16 44 61 74 61 4c 65 6e 20 2d 20 6c 65 6e 67 74 68  DataLen - length
33c17 20 6f 66 20 70 6f 73 20 64 61 74 61 20 66 6f 72   of pos data for
33c18 20 63 75 72 72 65 6e 74 20 64 6f 63 75 6d 65 6e   current documen
33c19 74 20 28 69 6e 63 6c 20 50 4f 53 5f 45 4e 44 29  t (incl POS_END)
33c1a 2e 0a 2a 2a 20 64 6c 72 53 74 65 70 20 2d 20 73  ..** dlrStep - s
33c1b 74 65 70 20 74 6f 20 63 75 72 72 65 6e 74 20 64  tep to current d
33c1c 6f 63 75 6d 65 6e 74 2e 0a 2a 2a 20 64 6c 72 49  ocument..** dlrI
33c1d 6e 69 74 20 2d 20 69 6e 69 74 69 61 6c 20 66 6f  nit - initial fo
33c1e 72 20 64 6f 63 6c 69 73 74 20 6f 66 20 67 69 76  r doclist of giv
33c1f 65 6e 20 74 79 70 65 20 61 67 61 69 6e 73 74 20  en type against 
33c20 67 69 76 65 6e 20 64 61 74 61 2e 0a 2a 2a 20 64  given data..** d
33c21 6c 72 44 65 73 74 72 6f 79 20 2d 20 63 6c 65 61  lrDestroy - clea
33c22 6e 20 75 70 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 65  n up..**.** Expe
33c23 63 74 65 64 20 75 73 61 67 65 20 69 73 20 73 6f  cted usage is so
33c24 6d 65 74 68 69 6e 67 20 6c 69 6b 65 3a 0a 2a 2a  mething like:.**
33c25 0a 2a 2a 20 20 20 44 4c 52 65 61 64 65 72 20 72  .**   DLReader r
33c26 65 61 64 65 72 3b 0a 2a 2a 20 20 20 64 6c 72 49  eader;.**   dlrI
33c27 6e 69 74 28 26 72 65 61 64 65 72 2c 20 70 44 61  nit(&reader, pDa
33c28 74 61 2c 20 6e 44 61 74 61 29 3b 0a 2a 2a 20 20  ta, nData);.**  
33c29 20 77 68 69 6c 65 28 20 21 64 6c 72 41 74 45 6e   while( !dlrAtEn
33c2a 64 28 26 72 65 61 64 65 72 29 20 29 7b 0a 2a 2a  d(&reader) ){.**
33c2b 20 20 20 20 20 2f 2f 20 63 61 6c 6c 73 20 74 6f       // calls to
33c2c 20 64 6c 72 44 6f 63 69 64 28 29 20 61 6e 64 20   dlrDocid() and 
33c2d 6b 69 6e 2e 0a 2a 2a 20 20 20 20 20 64 6c 72 53  kin..**     dlrS
33c2e 74 65 70 28 26 72 65 61 64 65 72 29 3b 0a 2a 2a  tep(&reader);.**
33c2f 20 20 20 7d 0a 2a 2a 20 20 20 64 6c 72 44 65 73     }.**   dlrDes
33c30 74 72 6f 79 28 26 72 65 61 64 65 72 29 3b 0a 2a  troy(&reader);.*
33c31 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
33c32 20 44 4c 52 65 61 64 65 72 20 7b 0a 20 20 44 6f   DLReader {.  Do
33c33 63 4c 69 73 74 54 79 70 65 20 69 54 79 70 65 3b  cListType iType;
33c34 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  .  const char *p
33c35 44 61 74 61 3b 0a 20 20 69 6e 74 20 6e 44 61 74  Data;.  int nDat
33c36 61 3b 0a 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  a;..  sqlite_int
33c37 36 34 20 69 44 6f 63 69 64 3b 0a 20 20 69 6e 74  64 iDocid;.  int
33c38 20 6e 45 6c 65 6d 65 6e 74 3b 0a 7d 20 44 4c 52   nElement;.} DLR
33c39 65 61 64 65 72 3b 0a 0a 73 74 61 74 69 63 20 69  eader;..static i
33c3a 6e 74 20 64 6c 72 41 74 45 6e 64 28 44 4c 52 65  nt dlrAtEnd(DLRe
33c3b 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
33c3c 20 20 61 73 73 65 72 74 28 20 70 52 65 61 64 65    assert( pReade
33c3d 72 2d 3e 6e 44 61 74 61 3e 3d 30 20 29 3b 0a 20  r->nData>=0 );. 
33c3e 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d   return pReader-
33c3f 3e 6e 44 61 74 61 3d 3d 30 3b 0a 7d 0a 73 74 61  >nData==0;.}.sta
33c40 74 69 63 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  tic sqlite_int64
33c41 20 64 6c 72 44 6f 63 69 64 28 44 4c 52 65 61 64   dlrDocid(DLRead
33c42 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
33c43 61 73 73 65 72 74 28 20 21 64 6c 72 41 74 45 6e  assert( !dlrAtEn
33c44 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20  d(pReader) );.  
33c45 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
33c46 69 44 6f 63 69 64 3b 0a 7d 0a 73 74 61 74 69 63  iDocid;.}.static
33c47 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 6c 72   const char *dlr
33c48 44 6f 63 44 61 74 61 28 44 4c 52 65 61 64 65 72  DocData(DLReader
33c49 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73   *pReader){.  as
33c4a 73 65 72 74 28 20 21 64 6c 72 41 74 45 6e 64 28  sert( !dlrAtEnd(
33c4b 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65  pReader) );.  re
33c4c 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 70 44  turn pReader->pD
33c4d 61 74 61 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e  ata;.}.static in
33c4e 74 20 64 6c 72 44 6f 63 44 61 74 61 42 79 74 65  t dlrDocDataByte
33c4f 73 28 44 4c 52 65 61 64 65 72 20 2a 70 52 65 61  s(DLReader *pRea
33c50 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20  der){.  assert( 
33c51 21 64 6c 72 41 74 45 6e 64 28 70 52 65 61 64 65  !dlrAtEnd(pReade
33c52 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  r) );.  return p
33c53 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74  Reader->nElement
33c54 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64  ;.}.static int d
33c55 6c 72 41 6c 6c 44 61 74 61 42 79 74 65 73 28 44  lrAllDataBytes(D
33c56 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  LReader *pReader
33c57 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 64 6c  ){.  assert( !dl
33c58 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20  rAtEnd(pReader) 
33c59 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61  );.  return pRea
33c5a 64 65 72 2d 3e 6e 44 61 74 61 3b 0a 7d 0a 2f 2a  der->nData;.}./*
33c5b 20 54 4f 44 4f 28 73 68 65 73 73 29 20 43 6f 6e   TODO(shess) Con
33c5c 73 69 64 65 72 20 61 64 64 69 6e 67 20 61 20 66  sider adding a f
33c5d 69 65 6c 64 20 74 6f 20 74 72 61 63 6b 20 69 44  ield to track iD
33c5e 6f 63 69 64 20 76 61 72 69 6e 74 20 6c 65 6e 67  ocid varint leng
33c5f 74 68 0a 2a 2a 20 74 6f 20 6d 61 6b 65 20 74 68  th.** to make th
33c60 65 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e  ese two function
33c61 73 20 66 61 73 74 65 72 2e 20 20 54 68 69 73 20  s faster.  This 
33c62 6d 69 67 68 74 20 6d 61 74 74 65 72 20 28 61 20  might matter (a 
33c63 74 69 6e 79 20 62 69 74 29 0a 2a 2a 20 66 6f 72  tiny bit).** for
33c64 20 71 75 65 72 69 65 73 2e 0a 2a 2f 0a 73 74 61   queries..*/.sta
33c65 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
33c66 64 6c 72 50 6f 73 44 61 74 61 28 44 4c 52 65 61  dlrPosData(DLRea
33c67 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
33c68 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44   sqlite_int64 iD
33c69 75 6d 6d 79 3b 0a 20 20 69 6e 74 20 6e 20 3d 20  ummy;.  int n = 
33c6a 66 74 73 33 47 65 74 56 61 72 69 6e 74 28 70 52  fts3GetVarint(pR
33c6b 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 69  eader->pData, &i
33c6c 44 75 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72 74  Dummy);.  assert
33c6d 28 20 21 64 6c 72 41 74 45 6e 64 28 70 52 65 61  ( !dlrAtEnd(pRea
33c6e 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  der) );.  return
33c6f 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b   pReader->pData+
33c70 6e 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  n;.}.static int 
33c71 64 6c 72 50 6f 73 44 61 74 61 4c 65 6e 28 44 4c  dlrPosDataLen(DL
33c72 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
33c73 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  {.  sqlite_int64
33c74 20 69 44 75 6d 6d 79 3b 0a 20 20 69 6e 74 20 6e   iDummy;.  int n
33c75 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
33c76 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c  (pReader->pData,
33c77 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73 73   &iDummy);.  ass
33c78 65 72 74 28 20 21 64 6c 72 41 74 45 6e 64 28 70  ert( !dlrAtEnd(p
33c79 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74  Reader) );.  ret
33c7a 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c  urn pReader->nEl
33c7b 65 6d 65 6e 74 2d 6e 3b 0a 7d 0a 73 74 61 74 69  ement-n;.}.stati
33c7c 63 20 76 6f 69 64 20 64 6c 72 53 74 65 70 28 44  c void dlrStep(D
33c7d 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  LReader *pReader
33c7e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 64 6c  ){.  assert( !dl
33c7f 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20  rAtEnd(pReader) 
33c80 29 3b 0a 0a 20 20 2f 2a 20 53 6b 69 70 20 70 61  );..  /* Skip pa
33c81 73 74 20 63 75 72 72 65 6e 74 20 64 6f 63 6c 69  st current docli
33c82 73 74 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 20  st element. */. 
33c83 20 61 73 73 65 72 74 28 20 70 52 65 61 64 65 72   assert( pReader
33c84 2d 3e 6e 45 6c 65 6d 65 6e 74 3c 3d 70 52 65 61  ->nElement<=pRea
33c85 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20  der->nData );.  
33c86 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 20 2b  pReader->pData +
33c87 3d 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d  = pReader->nElem
33c88 65 6e 74 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e  ent;.  pReader->
33c89 6e 44 61 74 61 20 2d 3d 20 70 52 65 61 64 65 72  nData -= pReader
33c8a 2d 3e 6e 45 6c 65 6d 65 6e 74 3b 0a 0a 20 20 2f  ->nElement;..  /
33c8b 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6d 6f  * If there is mo
33c8c 72 65 20 64 61 74 61 2c 20 72 65 61 64 20 74 68  re data, read th
33c8d 65 20 6e 65 78 74 20 64 6f 63 6c 69 73 74 20 65  e next doclist e
33c8e 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 66 28  lement. */.  if(
33c8f 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 21   pReader->nData!
33c90 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
33c91 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 44 65 6c  _int64 iDocidDel
33c92 74 61 3b 0a 20 20 20 20 69 6e 74 20 69 44 75 6d  ta;.    int iDum
33c93 6d 79 2c 20 6e 20 3d 20 66 74 73 33 47 65 74 56  my, n = fts3GetV
33c94 61 72 69 6e 74 28 70 52 65 61 64 65 72 2d 3e 70  arint(pReader->p
33c95 44 61 74 61 2c 20 26 69 44 6f 63 69 64 44 65 6c  Data, &iDocidDel
33c96 74 61 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72  ta);.    pReader
33c97 2d 3e 69 44 6f 63 69 64 20 2b 3d 20 69 44 6f 63  ->iDocid += iDoc
33c98 69 64 44 65 6c 74 61 3b 0a 20 20 20 20 69 66 28  idDelta;.    if(
33c99 20 70 52 65 61 64 65 72 2d 3e 69 54 79 70 65 3e   pReader->iType>
33c9a 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 29 7b  =DL_POSITIONS ){
33c9b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e  .      assert( n
33c9c 3c 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20  <pReader->nData 
33c9d 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  );.      while( 
33c9e 31 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 20 2b  1 ){.        n +
33c9f 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33  = fts3GetVarint3
33ca0 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61  2(pReader->pData
33ca1 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20  +n, &iDummy);.  
33ca2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c        assert( n<
33ca3 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20  =pReader->nData 
33ca4 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  );.        if( i
33ca5 44 75 6d 6d 79 3d 3d 50 4f 53 5f 45 4e 44 20 29  Dummy==POS_END )
33ca6 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
33ca7 69 66 28 20 69 44 75 6d 6d 79 3d 3d 50 4f 53 5f  if( iDummy==POS_
33ca8 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20  COLUMN ){.      
33ca9 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74      n += fts3Get
33caa 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65 72  Varint32(pReader
33cab 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d  ->pData+n, &iDum
33cac 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61  my);.          a
33cad 73 73 65 72 74 28 20 6e 3c 70 52 65 61 64 65 72  ssert( n<pReader
33cae 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 20  ->nData );.     
33caf 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 52 65     }else if( pRe
33cb0 61 64 65 72 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f  ader->iType==DL_
33cb1 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54  POSITIONS_OFFSET
33cb2 53 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  S ){.          n
33cb3 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e   += fts3GetVarin
33cb4 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61  t32(pReader->pDa
33cb5 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b 0a  ta+n, &iDummy);.
33cb6 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66            n += f
33cb7 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
33cb8 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c  Reader->pData+n,
33cb9 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20   &iDummy);.     
33cba 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 70       assert( n<p
33cbb 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b  Reader->nData );
33cbc 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
33cbd 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 65   }.    }.    pRe
33cbe 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 20 3d  ader->nElement =
33cbf 20 6e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   n;.    assert( 
33cc0 70 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e  pReader->nElemen
33cc1 74 3c 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  t<=pReader->nDat
33cc2 61 20 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69  a );.  }.}.stati
33cc3 63 20 76 6f 69 64 20 64 6c 72 49 6e 69 74 28 44  c void dlrInit(D
33cc4 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  LReader *pReader
33cc5 2c 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54  , DocListType iT
33cc6 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ype,.           
33cc7 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
33cc8 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20  har *pData, int 
33cc9 6e 44 61 74 61 29 7b 0a 20 20 61 73 73 65 72 74  nData){.  assert
33cca 28 20 70 44 61 74 61 21 3d 4e 55 4c 4c 20 26 26  ( pData!=NULL &&
33ccb 20 6e 44 61 74 61 21 3d 30 20 29 3b 0a 20 20 70   nData!=0 );.  p
33ccc 52 65 61 64 65 72 2d 3e 69 54 79 70 65 20 3d 20  Reader->iType = 
33ccd 69 54 79 70 65 3b 0a 20 20 70 52 65 61 64 65 72  iType;.  pReader
33cce 2d 3e 70 44 61 74 61 20 3d 20 70 44 61 74 61 3b  ->pData = pData;
33ccf 0a 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  .  pReader->nDat
33cd0 61 20 3d 20 6e 44 61 74 61 3b 0a 20 20 70 52 65  a = nData;.  pRe
33cd1 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 20 3d  ader->nElement =
33cd2 20 30 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69   0;.  pReader->i
33cd3 44 6f 63 69 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a  Docid = 0;..  /*
33cd4 20 4c 6f 61 64 20 74 68 65 20 66 69 72 73 74 20   Load the first 
33cd5 65 6c 65 6d 65 6e 74 27 73 20 64 61 74 61 2e 20  element's data. 
33cd6 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 61   There must be a
33cd7 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 2e 20   first element. 
33cd8 2a 2f 0a 20 20 64 6c 72 53 74 65 70 28 70 52 65  */.  dlrStep(pRe
33cd9 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20  ader);.}.static 
33cda 76 6f 69 64 20 64 6c 72 44 65 73 74 72 6f 79 28  void dlrDestroy(
33cdb 44 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  DLReader *pReade
33cdc 72 29 7b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70  r){.  SCRAMBLE(p
33cdd 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 23 69 66 6e  Reader);.}..#ifn
33cde 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 20 56 65  def NDEBUG./* Ve
33cdf 72 69 66 79 20 74 68 61 74 20 74 68 65 20 64 6f  rify that the do
33ce0 63 6c 69 73 74 20 63 61 6e 20 62 65 20 76 61 6c  clist can be val
33ce1 69 64 6c 79 20 64 65 63 6f 64 65 64 2e 20 20 41  idly decoded.  A
33ce2 6c 73 6f 20 72 65 74 75 72 6e 73 20 74 68 65 0a  lso returns the.
33ce3 2a 2a 20 6c 61 73 74 20 64 6f 63 69 64 20 66 6f  ** last docid fo
33ce4 75 6e 64 20 62 65 63 61 75 73 65 20 69 74 20 69  und because it i
33ce5 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20 69 6e 20  s convenient in 
33ce6 6f 74 68 65 72 20 61 73 73 65 72 74 69 6f 6e 73  other assertions
33ce7 20 66 6f 72 0a 2a 2a 20 44 4c 57 72 69 74 65 72   for.** DLWriter
33ce8 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
33ce9 20 64 6f 63 4c 69 73 74 56 61 6c 69 64 61 74 65   docListValidate
33cea 28 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79  (DocListType iTy
33ceb 70 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  pe, const char *
33cec 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61  pData, int nData
33ced 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
33cee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
33cef 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 4c 61 73  lite_int64 *pLas
33cf0 74 44 6f 63 69 64 29 7b 0a 20 20 73 71 6c 69 74  tDocid){.  sqlit
33cf1 65 5f 69 6e 74 36 34 20 69 50 72 65 76 44 6f 63  e_int64 iPrevDoc
33cf2 69 64 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  id = 0;.  assert
33cf3 28 20 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 61  ( nData>0 );.  a
33cf4 73 73 65 72 74 28 20 70 44 61 74 61 21 3d 30 20  ssert( pData!=0 
33cf5 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 61  );.  assert( pDa
33cf6 74 61 2b 6e 44 61 74 61 3e 70 44 61 74 61 20 29  ta+nData>pData )
33cf7 3b 0a 20 20 77 68 69 6c 65 28 20 6e 44 61 74 61  ;.  while( nData
33cf8 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  !=0 ){.    sqlit
33cf9 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 44 65  e_int64 iDocidDe
33cfa 6c 74 61 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d  lta;.    int n =
33cfb 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 28 70   fts3GetVarint(p
33cfc 44 61 74 61 2c 20 26 69 44 6f 63 69 64 44 65 6c  Data, &iDocidDel
33cfd 74 61 29 3b 0a 20 20 20 20 69 50 72 65 76 44 6f  ta);.    iPrevDo
33cfe 63 69 64 20 2b 3d 20 69 44 6f 63 69 64 44 65 6c  cid += iDocidDel
33cff 74 61 3b 0a 20 20 20 20 69 66 28 20 69 54 79 70  ta;.    if( iTyp
33d00 65 3e 44 4c 5f 44 4f 43 49 44 53 20 29 7b 0a 20  e>DL_DOCIDS ){. 
33d01 20 20 20 20 20 69 6e 74 20 69 44 75 6d 6d 79 3b       int iDummy;
33d02 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 31 20  .      while( 1 
33d03 29 7b 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d 20  ){.        n += 
33d04 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
33d05 70 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79  pData+n, &iDummy
33d06 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  );.        if( i
33d07 44 75 6d 6d 79 3d 3d 50 4f 53 5f 45 4e 44 20 29  Dummy==POS_END )
33d08 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
33d09 69 66 28 20 69 44 75 6d 6d 79 3d 3d 50 4f 53 5f  if( iDummy==POS_
33d0a 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20  COLUMN ){.      
33d0b 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74      n += fts3Get
33d0c 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2b 6e  Varint32(pData+n
33d0d 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20  , &iDummy);.    
33d0e 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 54      }else if( iT
33d0f 79 70 65 3e 44 4c 5f 50 4f 53 49 54 49 4f 4e 53  ype>DL_POSITIONS
33d10 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 20   ){.          n 
33d11 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74  += fts3GetVarint
33d12 33 32 28 70 44 61 74 61 2b 6e 2c 20 26 69 44 75  32(pData+n, &iDu
33d13 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20  mmy);.          
33d14 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69  n += fts3GetVari
33d15 6e 74 33 32 28 70 44 61 74 61 2b 6e 2c 20 26 69  nt32(pData+n, &i
33d16 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20  Dummy);.        
33d17 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  }.        assert
33d18 28 20 6e 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20  ( n<=nData );.  
33d19 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
33d1a 61 73 73 65 72 74 28 20 6e 3c 3d 6e 44 61 74 61  assert( n<=nData
33d1b 20 29 3b 0a 20 20 20 20 70 44 61 74 61 20 2b 3d   );.    pData +=
33d1c 20 6e 3b 0a 20 20 20 20 6e 44 61 74 61 20 2d 3d   n;.    nData -=
33d1d 20 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4c   n;.  }.  if( pL
33d1e 61 73 74 44 6f 63 69 64 20 29 20 2a 70 4c 61 73  astDocid ) *pLas
33d1f 74 44 6f 63 69 64 20 3d 20 69 50 72 65 76 44 6f  tDocid = iPrevDo
33d20 63 69 64 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 41  cid;.}.#define A
33d21 53 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c  SSERT_VALID_DOCL
33d22 49 53 54 28 69 2c 20 70 2c 20 6e 2c 20 6f 29 20  IST(i, p, n, o) 
33d23 64 6f 63 4c 69 73 74 56 61 6c 69 64 61 74 65 28  docListValidate(
33d24 69 2c 20 70 2c 20 6e 2c 20 6f 29 0a 23 65 6c 73  i, p, n, o).#els
33d25 65 0a 23 64 65 66 69 6e 65 20 41 53 53 45 52 54  e.#define ASSERT
33d26 5f 56 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28 69  _VALID_DOCLIST(i
33d27 2c 20 70 2c 20 6e 2c 20 6f 29 20 61 73 73 65 72  , p, n, o) asser
33d28 74 28 20 31 20 29 0a 23 65 6e 64 69 66 0a 0a 2f  t( 1 ).#endif../
33d29 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33d2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33d2b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33d2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33d2d 2a 2a 2a 2f 0a 2f 2a 20 44 4c 57 72 69 74 65 72  ***/./* DLWriter
33d2e 20 69 73 20 75 73 65 64 20 74 6f 20 77 72 69 74   is used to writ
33d2f 65 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 74  e doclist data t
33d30 6f 20 61 20 44 61 74 61 42 75 66 66 65 72 2e 20  o a DataBuffer. 
33d31 20 44 4c 57 72 69 74 65 72 0a 2a 2a 20 61 6c 77   DLWriter.** alw
33d32 61 79 73 20 61 70 70 65 6e 64 73 20 74 6f 20 74  ays appends to t
33d33 68 65 20 62 75 66 66 65 72 20 61 6e 64 20 64 6f  he buffer and do
33d34 65 73 20 6e 6f 74 20 6f 77 6e 20 69 74 2e 0a 2a  es not own it..*
33d35 2a 0a 2a 2a 20 64 6c 77 49 6e 69 74 20 2d 20 69  *.** dlwInit - i
33d36 6e 69 74 69 61 6c 69 7a 65 20 74 6f 20 77 72 69  nitialize to wri
33d37 74 65 20 61 20 67 69 76 65 6e 20 74 79 70 65 20  te a given type 
33d38 64 6f 63 6c 69 73 74 74 6f 20 61 20 62 75 66 66  doclistto a buff
33d39 65 72 2e 0a 2a 2a 20 64 6c 77 44 65 73 74 72 6f  er..** dlwDestro
33d3a 79 20 2d 20 63 6c 65 61 72 20 74 68 65 20 77 72  y - clear the wr
33d3b 69 74 65 72 27 73 20 6d 65 6d 6f 72 79 2e 20 20  iter's memory.  
33d3c 44 6f 65 73 20 6e 6f 74 20 66 72 65 65 20 62 75  Does not free bu
33d3d 66 66 65 72 2e 0a 2a 2a 20 64 6c 77 41 70 70 65  ffer..** dlwAppe
33d3e 6e 64 20 2d 20 61 70 70 65 6e 64 20 72 61 77 20  nd - append raw 
33d3f 64 6f 63 6c 69 73 74 20 64 61 74 61 20 74 6f 20  doclist data to 
33d40 62 75 66 66 65 72 2e 0a 2a 2a 20 64 6c 77 43 6f  buffer..** dlwCo
33d41 70 79 20 2d 20 63 6f 70 79 20 6e 65 78 74 20 64  py - copy next d
33d42 6f 63 6c 69 73 74 20 66 72 6f 6d 20 72 65 61 64  oclist from read
33d43 65 72 20 74 6f 20 77 72 69 74 65 72 2e 0a 2a 2a  er to writer..**
33d44 20 64 6c 77 41 64 64 20 2d 20 63 6f 6e 73 74 72   dlwAdd - constr
33d45 75 63 74 20 64 6f 63 6c 69 73 74 20 65 6c 65 6d  uct doclist elem
33d46 65 6e 74 20 61 6e 64 20 61 70 70 65 6e 64 20 74  ent and append t
33d47 6f 20 62 75 66 66 65 72 2e 0a 2a 2a 20 20 20 20  o buffer..**    
33d48 4f 6e 6c 79 20 61 70 70 6c 79 20 64 6c 77 41 64  Only apply dlwAd
33d49 64 28 29 20 74 6f 20 44 4c 5f 44 4f 43 49 44 53  d() to DL_DOCIDS
33d4a 20 64 6f 63 6c 69 73 74 73 20 28 65 6c 73 65 20   doclists (else 
33d4b 75 73 65 20 50 4c 57 72 69 74 65 72 29 2e 0a 2a  use PLWriter)..*
33d4c 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
33d4d 20 44 4c 57 72 69 74 65 72 20 7b 0a 20 20 44 6f   DLWriter {.  Do
33d4e 63 4c 69 73 74 54 79 70 65 20 69 54 79 70 65 3b  cListType iType;
33d4f 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 2a 62  .  DataBuffer *b
33d50 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  ;.  sqlite_int64
33d51 20 69 50 72 65 76 44 6f 63 69 64 3b 0a 23 69 66   iPrevDocid;.#if
33d52 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69 6e  ndef NDEBUG.  in
33d53 74 20 68 61 73 5f 69 50 72 65 76 44 6f 63 69 64  t has_iPrevDocid
33d54 3b 0a 23 65 6e 64 69 66 0a 7d 20 44 4c 57 72 69  ;.#endif.} DLWri
33d55 74 65 72 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69  ter;..static voi
33d56 64 20 64 6c 77 49 6e 69 74 28 44 4c 57 72 69 74  d dlwInit(DLWrit
33d57 65 72 20 2a 70 57 72 69 74 65 72 2c 20 44 6f 63  er *pWriter, Doc
33d58 4c 69 73 74 54 79 70 65 20 69 54 79 70 65 2c 20  ListType iType, 
33d59 44 61 74 61 42 75 66 66 65 72 20 2a 62 29 7b 0a  DataBuffer *b){.
33d5a 20 20 70 57 72 69 74 65 72 2d 3e 62 20 3d 20 62    pWriter->b = b
33d5b 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 54 79  ;.  pWriter->iTy
33d5c 70 65 20 3d 20 69 54 79 70 65 3b 0a 20 20 70 57  pe = iType;.  pW
33d5d 72 69 74 65 72 2d 3e 69 50 72 65 76 44 6f 63 69  riter->iPrevDoci
33d5e 64 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 4e  d = 0;.#ifndef N
33d5f 44 45 42 55 47 0a 20 20 70 57 72 69 74 65 72 2d  DEBUG.  pWriter-
33d60 3e 68 61 73 5f 69 50 72 65 76 44 6f 63 69 64 20  >has_iPrevDocid 
33d61 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 73 74  = 0;.#endif.}.st
33d62 61 74 69 63 20 76 6f 69 64 20 64 6c 77 44 65 73  atic void dlwDes
33d63 74 72 6f 79 28 44 4c 57 72 69 74 65 72 20 2a 70  troy(DLWriter *p
33d64 57 72 69 74 65 72 29 7b 0a 20 20 53 43 52 41 4d  Writer){.  SCRAM
33d65 42 4c 45 28 70 57 72 69 74 65 72 29 3b 0a 7d 0a  BLE(pWriter);.}.
33d66 2f 2a 20 69 46 69 72 73 74 44 6f 63 69 64 20 69  /* iFirstDocid i
33d67 73 20 74 68 65 20 66 69 72 73 74 20 64 6f 63 69  s the first doci
33d68 64 20 69 6e 20 74 68 65 20 64 6f 63 6c 69 73 74  d in the doclist
33d69 20 69 6e 20 70 44 61 74 61 2e 20 20 49 74 20 69   in pData.  It i
33d6a 73 0a 2a 2a 20 6e 65 65 64 65 64 20 62 65 63 61  s.** needed beca
33d6b 75 73 65 20 70 44 61 74 61 20 6d 61 79 20 70 6f  use pData may po
33d6c 69 6e 74 20 77 69 74 68 69 6e 20 61 20 6c 61 72  int within a lar
33d6d 67 65 72 20 64 6f 63 6c 69 73 74 2c 20 69 6e 20  ger doclist, in 
33d6e 77 68 69 63 68 0a 2a 2a 20 63 61 73 65 20 74 68  which.** case th
33d6f 65 20 66 69 72 73 74 20 69 74 65 6d 20 77 6f 75  e first item wou
33d70 6c 64 20 62 65 20 64 65 6c 74 61 2d 65 6e 63 6f  ld be delta-enco
33d71 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 69 4c 61 73 74  ded..**.** iLast
33d72 44 6f 63 69 64 20 69 73 20 74 68 65 20 66 69 6e  Docid is the fin
33d73 61 6c 20 64 6f 63 69 64 20 69 6e 20 74 68 65 20  al docid in the 
33d74 64 6f 63 6c 69 73 74 20 69 6e 20 70 44 61 74 61  doclist in pData
33d75 2e 20 20 49 74 20 69 73 0a 2a 2a 20 6e 65 65 64  .  It is.** need
33d76 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 68 65  ed to create the
33d77 20 6e 65 77 20 69 50 72 65 76 44 6f 63 69 64 20   new iPrevDocid 
33d78 66 6f 72 20 66 75 74 75 72 65 20 64 65 6c 74 61  for future delta
33d79 2d 65 6e 63 6f 64 69 6e 67 2e 20 20 54 68 65 0a  -encoding.  The.
33d7a 2a 2a 20 63 6f 64 65 20 63 6f 75 6c 64 20 64 65  ** code could de
33d7b 63 6f 64 65 20 74 68 65 20 70 61 73 73 65 64 20  code the passed 
33d7c 64 6f 63 6c 69 73 74 20 74 6f 20 72 65 63 72 65  doclist to recre
33d7d 61 74 65 20 69 4c 61 73 74 44 6f 63 69 64 2c 20  ate iLastDocid, 
33d7e 62 75 74 0a 2a 2a 20 74 68 65 20 6f 6e 6c 79 20  but.** the only 
33d7f 63 75 72 72 65 6e 74 20 75 73 65 72 20 28 64 6f  current user (do
33d80 63 4c 69 73 74 4d 65 72 67 65 29 20 61 6c 72 65  cListMerge) alre
33d81 61 64 79 20 68 61 73 20 64 65 63 6f 64 65 64 20  ady has decoded 
33d82 74 68 69 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74  this.** informat
33d83 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28  ion..*/./* TODO(
33d84 73 68 65 73 73 29 20 54 68 69 73 20 68 61 73 20  shess) This has 
33d85 62 65 63 6f 6d 65 20 6a 75 73 74 20 61 20 68 65  become just a he
33d86 6c 70 65 72 20 66 6f 72 20 64 6f 63 4c 69 73 74  lper for docList
33d87 4d 65 72 67 65 2e 0a 2a 2a 20 43 6f 6e 73 69 64  Merge..** Consid
33d88 65 72 20 61 20 72 65 66 61 63 74 6f 72 20 74 6f  er a refactor to
33d89 20 6d 61 6b 65 20 74 68 69 73 20 63 6c 65 61 6e   make this clean
33d8a 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  er..*/.static vo
33d8b 69 64 20 64 6c 77 41 70 70 65 6e 64 28 44 4c 57  id dlwAppend(DLW
33d8c 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a  riter *pWriter,.
33d8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d8e 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
33d8f 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61   *pData, int nDa
33d90 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ta,.            
33d91 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
33d92 5f 69 6e 74 36 34 20 69 46 69 72 73 74 44 6f 63  _int64 iFirstDoc
33d93 69 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  id, sqlite_int64
33d94 20 69 4c 61 73 74 44 6f 63 69 64 29 7b 0a 20 20   iLastDocid){.  
33d95 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f  sqlite_int64 iDo
33d96 63 69 64 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  cid = 0;.  char 
33d97 63 5b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20  c[VARINT_MAX];. 
33d98 20 69 6e 74 20 6e 46 69 72 73 74 4f 6c 64 2c 20   int nFirstOld, 
33d99 6e 46 69 72 73 74 4e 65 77 3b 20 20 20 20 20 2f  nFirstNew;     /
33d9a 2a 20 4f 6c 64 20 61 6e 64 20 6e 65 77 20 76 61  * Old and new va
33d9b 72 69 6e 74 20 6c 65 6e 20 6f 66 20 66 69 72 73  rint len of firs
33d9c 74 20 64 6f 63 69 64 2e 20 2a 2f 0a 23 69 66 6e  t docid. */.#ifn
33d9d 64 65 66 20 4e 44 45 42 55 47 0a 20 20 73 71 6c  def NDEBUG.  sql
33d9e 69 74 65 5f 69 6e 74 36 34 20 69 4c 61 73 74 44  ite_int64 iLastD
33d9f 6f 63 69 64 44 65 6c 74 61 3b 0a 23 65 6e 64 69  ocidDelta;.#endi
33da0 66 0a 0a 20 20 2f 2a 20 52 65 63 6f 64 65 20 74  f..  /* Recode t
33da1 68 65 20 69 6e 69 74 69 61 6c 20 64 6f 63 69 64  he initial docid
33da2 20 61 73 20 64 65 6c 74 61 20 66 72 6f 6d 20 69   as delta from i
33da3 50 72 65 76 44 6f 63 69 64 2e 20 2a 2f 0a 20 20  PrevDocid. */.  
33da4 6e 46 69 72 73 74 4f 6c 64 20 3d 20 66 74 73 33  nFirstOld = fts3
33da5 47 65 74 56 61 72 69 6e 74 28 70 44 61 74 61 2c  GetVarint(pData,
33da6 20 26 69 44 6f 63 69 64 29 3b 0a 20 20 61 73 73   &iDocid);.  ass
33da7 65 72 74 28 20 6e 46 69 72 73 74 4f 6c 64 3c 6e  ert( nFirstOld<n
33da8 44 61 74 61 20 7c 7c 20 28 6e 46 69 72 73 74 4f  Data || (nFirstO
33da9 6c 64 3d 3d 6e 44 61 74 61 20 26 26 20 70 57 72  ld==nData && pWr
33daa 69 74 65 72 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f  iter->iType==DL_
33dab 44 4f 43 49 44 53 29 20 29 3b 0a 20 20 6e 46 69  DOCIDS) );.  nFi
33dac 72 73 74 4e 65 77 20 3d 20 66 74 73 33 50 75 74  rstNew = fts3Put
33dad 56 61 72 69 6e 74 28 63 2c 20 69 46 69 72 73 74  Varint(c, iFirst
33dae 44 6f 63 69 64 2d 70 57 72 69 74 65 72 2d 3e 69  Docid-pWriter->i
33daf 50 72 65 76 44 6f 63 69 64 29 3b 0a 0a 20 20 2f  PrevDocid);..  /
33db0 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68  * Verify that th
33db1 65 20 69 6e 63 6f 6d 69 6e 67 20 64 6f 63 6c 69  e incoming docli
33db2 73 74 20 69 73 20 76 61 6c 69 64 20 41 4e 44 20  st is valid AND 
33db3 74 68 61 74 20 69 74 20 65 6e 64 73 20 77 69 74  that it ends wit
33db4 68 0a 20 20 2a 2a 20 74 68 65 20 65 78 70 65 63  h.  ** the expec
33db5 74 65 64 20 64 6f 63 69 64 2e 20 20 54 68 69 73  ted docid.  This
33db6 20 69 73 20 65 73 73 65 6e 74 69 61 6c 20 62 65   is essential be
33db7 63 61 75 73 65 20 77 65 27 6c 6c 20 74 72 75 73  cause we'll trus
33db8 74 20 74 68 69 73 0a 20 20 2a 2a 20 64 6f 63 69  t this.  ** doci
33db9 64 20 69 6e 20 66 75 74 75 72 65 20 64 65 6c 74  d in future delt
33dba 61 2d 65 6e 63 6f 64 69 6e 67 2e 0a 20 20 2a 2f  a-encoding..  */
33dbb 0a 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f  .  ASSERT_VALID_
33dbc 44 4f 43 4c 49 53 54 28 70 57 72 69 74 65 72 2d  DOCLIST(pWriter-
33dbd 3e 69 54 79 70 65 2c 20 70 44 61 74 61 2c 20 6e  >iType, pData, n
33dbe 44 61 74 61 2c 20 26 69 4c 61 73 74 44 6f 63 69  Data, &iLastDoci
33dbf 64 44 65 6c 74 61 29 3b 0a 20 20 61 73 73 65 72  dDelta);.  asser
33dc0 74 28 20 69 4c 61 73 74 44 6f 63 69 64 3d 3d 69  t( iLastDocid==i
33dc1 46 69 72 73 74 44 6f 63 69 64 2d 69 44 6f 63 69  FirstDocid-iDoci
33dc2 64 2b 69 4c 61 73 74 44 6f 63 69 64 44 65 6c 74  d+iLastDocidDelt
33dc3 61 20 29 3b 0a 0a 20 20 2f 2a 20 41 70 70 65 6e  a );..  /* Appen
33dc4 64 20 72 65 63 6f 64 65 64 20 69 6e 69 74 69 61  d recoded initia
33dc5 6c 20 64 6f 63 69 64 20 61 6e 64 20 65 76 65 72  l docid and ever
33dc6 79 74 68 69 6e 67 20 65 6c 73 65 2e 20 20 52 65  ything else.  Re
33dc7 73 74 20 6f 66 20 64 6f 63 69 64 73 0a 20 20 2a  st of docids.  *
33dc8 2a 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65  * should have be
33dc9 65 6e 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64  en delta-encoded
33dca 20 66 72 6f 6d 20 70 72 65 76 69 6f 75 73 20 69   from previous i
33dcb 6e 69 74 69 61 6c 20 64 6f 63 69 64 2e 0a 20 20  nitial docid..  
33dcc 2a 2f 0a 20 20 69 66 28 20 6e 46 69 72 73 74 4f  */.  if( nFirstO
33dcd 6c 64 3c 6e 44 61 74 61 20 29 7b 0a 20 20 20 20  ld<nData ){.    
33dce 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
33dcf 32 28 70 57 72 69 74 65 72 2d 3e 62 2c 20 63 2c  2(pWriter->b, c,
33dd0 20 6e 46 69 72 73 74 4e 65 77 2c 0a 20 20 20 20   nFirstNew,.    
33dd1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33dd2 20 20 70 44 61 74 61 2b 6e 46 69 72 73 74 4f 6c    pData+nFirstOl
33dd3 64 2c 20 6e 44 61 74 61 2d 6e 46 69 72 73 74 4f  d, nData-nFirstO
33dd4 6c 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ld);.  }else{.  
33dd5 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
33dd6 6e 64 28 70 57 72 69 74 65 72 2d 3e 62 2c 20 63  nd(pWriter->b, c
33dd7 2c 20 6e 46 69 72 73 74 4e 65 77 29 3b 0a 20 20  , nFirstNew);.  
33dd8 7d 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 50 72  }.  pWriter->iPr
33dd9 65 76 44 6f 63 69 64 20 3d 20 69 4c 61 73 74 44  evDocid = iLastD
33dda 6f 63 69 64 3b 0a 7d 0a 73 74 61 74 69 63 20 76  ocid;.}.static v
33ddb 6f 69 64 20 64 6c 77 43 6f 70 79 28 44 4c 57 72  oid dlwCopy(DLWr
33ddc 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c 20 44  iter *pWriter, D
33ddd 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  LReader *pReader
33dde 29 7b 0a 20 20 64 6c 77 41 70 70 65 6e 64 28 70  ){.  dlwAppend(p
33ddf 57 72 69 74 65 72 2c 20 64 6c 72 44 6f 63 44 61  Writer, dlrDocDa
33de0 74 61 28 70 52 65 61 64 65 72 29 2c 20 64 6c 72  ta(pReader), dlr
33de1 44 6f 63 44 61 74 61 42 79 74 65 73 28 70 52 65  DocDataBytes(pRe
33de2 61 64 65 72 29 2c 0a 20 20 20 20 20 20 20 20 20  ader),.         
33de3 20 20 20 64 6c 72 44 6f 63 69 64 28 70 52 65 61     dlrDocid(pRea
33de4 64 65 72 29 2c 20 64 6c 72 44 6f 63 69 64 28 70  der), dlrDocid(p
33de5 52 65 61 64 65 72 29 29 3b 0a 7d 0a 73 74 61 74  Reader));.}.stat
33de6 69 63 20 76 6f 69 64 20 64 6c 77 41 64 64 28 44  ic void dlwAdd(D
33de7 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  LWriter *pWriter
33de8 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  , sqlite_int64 i
33de9 44 6f 63 69 64 29 7b 0a 20 20 63 68 61 72 20 63  Docid){.  char c
33dea 5b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20  [VARINT_MAX];.  
33deb 69 6e 74 20 6e 20 3d 20 66 74 73 33 50 75 74 56  int n = fts3PutV
33dec 61 72 69 6e 74 28 63 2c 20 69 44 6f 63 69 64 2d  arint(c, iDocid-
33ded 70 57 72 69 74 65 72 2d 3e 69 50 72 65 76 44 6f  pWriter->iPrevDo
33dee 63 69 64 29 3b 0a 0a 20 20 2f 2a 20 44 6f 63 69  cid);..  /* Doci
33def 64 73 20 6d 75 73 74 20 61 73 63 65 6e 64 2e 20  ds must ascend. 
33df0 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 21 70 57  */.  assert( !pW
33df1 72 69 74 65 72 2d 3e 68 61 73 5f 69 50 72 65 76  riter->has_iPrev
33df2 44 6f 63 69 64 20 7c 7c 20 69 44 6f 63 69 64 3e  Docid || iDocid>
33df3 70 57 72 69 74 65 72 2d 3e 69 50 72 65 76 44 6f  pWriter->iPrevDo
33df4 63 69 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28  cid );.  assert(
33df5 20 70 57 72 69 74 65 72 2d 3e 69 54 79 70 65 3d   pWriter->iType=
33df6 3d 44 4c 5f 44 4f 43 49 44 53 20 29 3b 0a 0a 20  =DL_DOCIDS );.. 
33df7 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
33df8 64 28 70 57 72 69 74 65 72 2d 3e 62 2c 20 63 2c  d(pWriter->b, c,
33df9 20 6e 29 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e   n);.  pWriter->
33dfa 69 50 72 65 76 44 6f 63 69 64 20 3d 20 69 44 6f  iPrevDocid = iDo
33dfb 63 69 64 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45  cid;.#ifndef NDE
33dfc 42 55 47 0a 20 20 70 57 72 69 74 65 72 2d 3e 68  BUG.  pWriter->h
33dfd 61 73 5f 69 50 72 65 76 44 6f 63 69 64 20 3d 20  as_iPrevDocid = 
33dfe 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 2a  1;.#endif.}../**
33dff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33e01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33e02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33e03 2a 2f 0a 2f 2a 20 50 4c 52 65 61 64 65 72 20 69  */./* PLReader i
33e04 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 64  s used to read d
33e05 61 74 61 20 66 72 6f 6d 20 61 20 64 6f 63 75 6d  ata from a docum
33e06 65 6e 74 27 73 20 70 6f 73 69 74 69 6f 6e 20 6c  ent's position l
33e07 69 73 74 2e 20 20 41 73 0a 2a 2a 20 74 68 65 20  ist.  As.** the 
33e08 63 61 6c 6c 65 72 20 73 74 65 70 73 20 74 68 72  caller steps thr
33e09 6f 75 67 68 20 74 68 65 20 6c 69 73 74 2c 20 64  ough the list, d
33e0a 61 74 61 20 69 73 20 63 61 63 68 65 64 20 73 6f  ata is cached so
33e0b 20 74 68 61 74 20 76 61 72 69 6e 74 73 0a 2a 2a   that varints.**
33e0c 20 6f 6e 6c 79 20 6e 65 65 64 20 74 6f 20 62 65   only need to be
33e0d 20 64 65 63 6f 64 65 64 20 6f 6e 63 65 2e 0a 2a   decoded once..*
33e0e 2a 0a 2a 2a 20 70 6c 72 49 6e 69 74 2c 20 70 6c  *.** plrInit, pl
33e0f 72 44 65 73 74 72 6f 79 20 2d 20 63 72 65 61 74  rDestroy - creat
33e10 65 2f 64 65 73 74 72 6f 79 20 61 20 72 65 61 64  e/destroy a read
33e11 65 72 2e 0a 2a 2a 20 70 6c 72 43 6f 6c 75 6d 6e  er..** plrColumn
33e12 2c 20 70 6c 72 50 6f 73 69 74 69 6f 6e 2c 20 70  , plrPosition, p
33e13 6c 72 53 74 61 72 74 4f 66 66 73 65 74 2c 20 70  lrStartOffset, p
33e14 6c 72 45 6e 64 4f 66 66 73 65 74 20 2d 20 61 63  lrEndOffset - ac
33e15 63 65 73 73 6f 72 73 0a 2a 2a 20 70 6c 72 41 74  cessors.** plrAt
33e16 45 6e 64 20 2d 20 61 74 20 65 6e 64 20 6f 66 20  End - at end of 
33e17 73 74 72 65 61 6d 2c 20 6f 6e 6c 79 20 63 61 6c  stream, only cal
33e18 6c 20 70 6c 72 44 65 73 74 72 6f 79 20 6f 6e 63  l plrDestroy onc
33e19 65 20 74 72 75 65 2e 0a 2a 2a 20 70 6c 72 53 74  e true..** plrSt
33e1a 65 70 20 2d 20 73 74 65 70 20 74 6f 20 74 68 65  ep - step to the
33e1b 20 6e 65 78 74 20 65 6c 65 6d 65 6e 74 2e 0a 2a   next element..*
33e1c 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
33e1d 20 50 4c 52 65 61 64 65 72 20 7b 0a 20 20 2f 2a   PLReader {.  /*
33e1e 20 54 68 65 73 65 20 72 65 66 65 72 20 74 6f 20   These refer to 
33e1f 74 68 65 20 6e 65 78 74 20 70 6f 73 69 74 69 6f  the next positio
33e20 6e 27 73 20 64 61 74 61 2e 20 20 6e 44 61 74 61  n's data.  nData
33e21 20 77 69 6c 6c 20 72 65 61 63 68 20 30 20 77 68   will reach 0 wh
33e22 65 6e 0a 20 20 2a 2a 20 72 65 61 64 69 6e 67 20  en.  ** reading 
33e23 74 68 65 20 6c 61 73 74 20 70 6f 73 69 74 69 6f  the last positio
33e24 6e 2c 20 73 6f 20 70 6c 72 53 74 65 70 28 29 20  n, so plrStep() 
33e25 73 69 67 6e 61 6c 73 20 45 4f 46 20 62 79 20 73  signals EOF by s
33e26 65 74 74 69 6e 67 0a 20 20 2a 2a 20 70 44 61 74  etting.  ** pDat
33e27 61 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a  a to NULL..  */.
33e28 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44    const char *pD
33e29 61 74 61 3b 0a 20 20 69 6e 74 20 6e 44 61 74 61  ata;.  int nData
33e2a 3b 0a 0a 20 20 44 6f 63 4c 69 73 74 54 79 70 65  ;..  DocListType
33e2b 20 69 54 79 70 65 3b 0a 20 20 69 6e 74 20 69 43   iType;.  int iC
33e2c 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f  olumn;         /
33e2d 2a 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d  * the last colum
33e2e 6e 20 72 65 61 64 20 2a 2f 0a 20 20 69 6e 74 20  n read */.  int 
33e2f 69 50 6f 73 69 74 69 6f 6e 3b 20 20 20 20 20 20  iPosition;      
33e30 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 70 6f 73   /* the last pos
33e31 69 74 69 6f 6e 20 72 65 61 64 20 2a 2f 0a 20 20  ition read */.  
33e32 69 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65 74  int iStartOffset
33e33 3b 20 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74  ;    /* the last
33e34 20 73 74 61 72 74 20 6f 66 66 73 65 74 20 72 65   start offset re
33e35 61 64 20 2a 2f 0a 20 20 69 6e 74 20 69 45 6e 64  ad */.  int iEnd
33e36 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20  Offset;      /* 
33e37 74 68 65 20 6c 61 73 74 20 65 6e 64 20 6f 66 66  the last end off
33e38 73 65 74 20 72 65 61 64 20 2a 2f 0a 7d 20 50 4c  set read */.} PL
33e39 52 65 61 64 65 72 3b 0a 0a 73 74 61 74 69 63 20  Reader;..static 
33e3a 69 6e 74 20 70 6c 72 41 74 45 6e 64 28 50 4c 52  int plrAtEnd(PLR
33e3b 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
33e3c 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65  .  return pReade
33e3d 72 2d 3e 70 44 61 74 61 3d 3d 4e 55 4c 4c 3b 0a  r->pData==NULL;.
33e3e 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72  }.static int plr
33e3f 43 6f 6c 75 6d 6e 28 50 4c 52 65 61 64 65 72 20  Column(PLReader 
33e40 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73  *pReader){.  ass
33e41 65 72 74 28 20 21 70 6c 72 41 74 45 6e 64 28 70  ert( !plrAtEnd(p
33e42 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74  Reader) );.  ret
33e43 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 69 43 6f  urn pReader->iCo
33e44 6c 75 6d 6e 3b 0a 7d 0a 73 74 61 74 69 63 20 69  lumn;.}.static i
33e45 6e 74 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 50  nt plrPosition(P
33e46 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  LReader *pReader
33e47 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 70 6c  ){.  assert( !pl
33e48 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20  rAtEnd(pReader) 
33e49 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61  );.  return pRea
33e4a 64 65 72 2d 3e 69 50 6f 73 69 74 69 6f 6e 3b 0a  der->iPosition;.
33e4b 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72  }.static int plr
33e4c 53 74 61 72 74 4f 66 66 73 65 74 28 50 4c 52 65  StartOffset(PLRe
33e4d 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
33e4e 20 20 61 73 73 65 72 74 28 20 21 70 6c 72 41 74    assert( !plrAt
33e4f 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a  End(pReader) );.
33e50 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72    return pReader
33e51 2d 3e 69 53 74 61 72 74 4f 66 66 73 65 74 3b 0a  ->iStartOffset;.
33e52 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72  }.static int plr
33e53 45 6e 64 4f 66 66 73 65 74 28 50 4c 52 65 61 64  EndOffset(PLRead
33e54 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
33e55 61 73 73 65 72 74 28 20 21 70 6c 72 41 74 45 6e  assert( !plrAtEn
33e56 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20  d(pReader) );.  
33e57 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
33e58 69 45 6e 64 4f 66 66 73 65 74 3b 0a 7d 0a 73 74  iEndOffset;.}.st
33e59 61 74 69 63 20 76 6f 69 64 20 70 6c 72 53 74 65  atic void plrSte
33e5a 70 28 50 4c 52 65 61 64 65 72 20 2a 70 52 65 61  p(PLReader *pRea
33e5b 64 65 72 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6e  der){.  int i, n
33e5c 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 6c  ;..  assert( !pl
33e5d 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20  rAtEnd(pReader) 
33e5e 29 3b 0a 0a 20 20 69 66 28 20 70 52 65 61 64 65  );..  if( pReade
33e5f 72 2d 3e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20  r->nData==0 ){. 
33e60 20 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74     pReader->pDat
33e61 61 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 72 65  a = NULL;.    re
33e62 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 6e 20 3d  turn;.  }..  n =
33e63 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
33e64 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c  (pReader->pData,
33e65 20 26 69 29 3b 0a 20 20 69 66 28 20 69 3d 3d 50   &i);.  if( i==P
33e66 4f 53 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20  OS_COLUMN ){.   
33e67 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72   n += fts3GetVar
33e68 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70  int32(pReader->p
33e69 44 61 74 61 2b 6e 2c 20 26 70 52 65 61 64 65 72  Data+n, &pReader
33e6a 2d 3e 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  ->iColumn);.    
33e6b 70 52 65 61 64 65 72 2d 3e 69 50 6f 73 69 74 69  pReader->iPositi
33e6c 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 70 52 65 61  on = 0;.    pRea
33e6d 64 65 72 2d 3e 69 53 74 61 72 74 4f 66 66 73 65  der->iStartOffse
33e6e 74 20 3d 20 30 3b 0a 20 20 20 20 6e 20 2b 3d 20  t = 0;.    n += 
33e6f 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
33e70 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e  pReader->pData+n
33e71 2c 20 26 69 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20  , &i);.  }.  /* 
33e72 53 68 6f 75 6c 64 20 6e 65 76 65 72 20 73 65 65  Should never see
33e73 20 61 64 6a 61 63 65 6e 74 20 63 6f 6c 75 6d 6e   adjacent column
33e74 20 63 68 61 6e 67 65 73 2e 20 2a 2f 0a 20 20 61   changes. */.  a
33e75 73 73 65 72 74 28 20 69 21 3d 50 4f 53 5f 43 4f  ssert( i!=POS_CO
33e76 4c 55 4d 4e 20 29 3b 0a 0a 20 20 69 66 28 20 69  LUMN );..  if( i
33e77 3d 3d 50 4f 53 5f 45 4e 44 20 29 7b 0a 20 20 20  ==POS_END ){.   
33e78 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20   pReader->nData 
33e79 3d 20 30 3b 0a 20 20 20 20 70 52 65 61 64 65 72  = 0;.    pReader
33e7a 2d 3e 70 44 61 74 61 20 3d 20 4e 55 4c 4c 3b 0a  ->pData = NULL;.
33e7b 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
33e7c 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 50 6f 73  .  pReader->iPos
33e7d 69 74 69 6f 6e 20 2b 3d 20 69 2d 50 4f 53 5f 42  ition += i-POS_B
33e7e 41 53 45 3b 0a 20 20 69 66 28 20 70 52 65 61 64  ASE;.  if( pRead
33e7f 65 72 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 50 4f  er->iType==DL_PO
33e80 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20  SITIONS_OFFSETS 
33e81 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33  ){.    n += fts3
33e82 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61  GetVarint32(pRea
33e83 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69  der->pData+n, &i
33e84 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  );.    pReader->
33e85 69 53 74 61 72 74 4f 66 66 73 65 74 20 2b 3d 20  iStartOffset += 
33e86 69 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33  i;.    n += fts3
33e87 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61  GetVarint32(pRea
33e88 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69  der->pData+n, &i
33e89 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  );.    pReader->
33e8a 69 45 6e 64 4f 66 66 73 65 74 20 3d 20 70 52 65  iEndOffset = pRe
33e8b 61 64 65 72 2d 3e 69 53 74 61 72 74 4f 66 66 73  ader->iStartOffs
33e8c 65 74 2b 69 3b 0a 20 20 7d 0a 20 20 61 73 73 65  et+i;.  }.  asse
33e8d 72 74 28 20 6e 3c 3d 70 52 65 61 64 65 72 2d 3e  rt( n<=pReader->
33e8e 6e 44 61 74 61 20 29 3b 0a 20 20 70 52 65 61 64  nData );.  pRead
33e8f 65 72 2d 3e 70 44 61 74 61 20 2b 3d 20 6e 3b 0a  er->pData += n;.
33e90 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61    pReader->nData
33e91 20 2d 3d 20 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63   -= n;.}..static
33e92 20 76 6f 69 64 20 70 6c 72 49 6e 69 74 28 50 4c   void plrInit(PL
33e93 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 2c  Reader *pReader,
33e94 20 44 4c 52 65 61 64 65 72 20 2a 70 44 4c 52 65   DLReader *pDLRe
33e95 61 64 65 72 29 7b 0a 20 20 70 52 65 61 64 65 72  ader){.  pReader
33e96 2d 3e 70 44 61 74 61 20 3d 20 64 6c 72 50 6f 73  ->pData = dlrPos
33e97 44 61 74 61 28 70 44 4c 52 65 61 64 65 72 29 3b  Data(pDLReader);
33e98 0a 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  .  pReader->nDat
33e99 61 20 3d 20 64 6c 72 50 6f 73 44 61 74 61 4c 65  a = dlrPosDataLe
33e9a 6e 28 70 44 4c 52 65 61 64 65 72 29 3b 0a 20 20  n(pDLReader);.  
33e9b 70 52 65 61 64 65 72 2d 3e 69 54 79 70 65 20 3d  pReader->iType =
33e9c 20 70 44 4c 52 65 61 64 65 72 2d 3e 69 54 79 70   pDLReader->iTyp
33e9d 65 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 43  e;.  pReader->iC
33e9e 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20 70 52 65  olumn = 0;.  pRe
33e9f 61 64 65 72 2d 3e 69 50 6f 73 69 74 69 6f 6e 20  ader->iPosition 
33ea0 3d 20 30 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e  = 0;.  pReader->
33ea1 69 53 74 61 72 74 4f 66 66 73 65 74 20 3d 20 30  iStartOffset = 0
33ea2 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 45 6e  ;.  pReader->iEn
33ea3 64 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 70  dOffset = 0;.  p
33ea4 6c 72 53 74 65 70 28 70 52 65 61 64 65 72 29 3b  lrStep(pReader);
33ea5 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  .}.static void p
33ea6 6c 72 44 65 73 74 72 6f 79 28 50 4c 52 65 61 64  lrDestroy(PLRead
33ea7 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
33ea8 53 43 52 41 4d 42 4c 45 28 70 52 65 61 64 65 72  SCRAMBLE(pReader
33ea9 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  );.}../*********
33eaa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33eab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33eac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33ead 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 50  **********/./* P
33eae 4c 57 72 69 74 65 72 20 69 73 20 75 73 65 64 20  LWriter is used 
33eaf 69 6e 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20  in constructing 
33eb0 61 20 64 6f 63 75 6d 65 6e 74 27 73 20 70 6f 73  a document's pos
33eb1 69 74 69 6f 6e 20 6c 69 73 74 2e 20 20 41 73 20  ition list.  As 
33eb2 61 0a 2a 2a 20 63 6f 6e 76 65 6e 69 65 6e 63 65  a.** convenience
33eb3 2c 20 69 66 20 69 54 79 70 65 20 69 73 20 44 4c  , if iType is DL
33eb4 5f 44 4f 43 49 44 53 2c 20 50 4c 57 72 69 74 65  _DOCIDS, PLWrite
33eb5 72 20 62 65 63 6f 6d 65 73 20 61 20 6e 6f 2d 6f  r becomes a no-o
33eb6 70 2e 0a 2a 2a 20 50 4c 57 72 69 74 65 72 20 77  p..** PLWriter w
33eb7 72 69 74 65 73 20 74 6f 20 74 68 65 20 61 73 73  rites to the ass
33eb8 6f 63 69 61 74 65 64 20 44 4c 57 72 69 74 65 72  ociated DLWriter
33eb9 27 73 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a  's buffer..**.**
33eba 20 70 6c 77 49 6e 69 74 20 2d 20 69 6e 69 74 20   plwInit - init 
33ebb 66 6f 72 20 77 72 69 74 69 6e 67 20 61 20 64 6f  for writing a do
33ebc 63 75 6d 65 6e 74 27 73 20 70 6f 73 6c 69 73 74  cument's poslist
33ebd 2e 0a 2a 2a 20 70 6c 77 44 65 73 74 72 6f 79 20  ..** plwDestroy 
33ebe 2d 20 63 6c 65 61 72 20 61 20 77 72 69 74 65 72  - clear a writer
33ebf 2e 0a 2a 2a 20 70 6c 77 41 64 64 20 2d 20 61 70  ..** plwAdd - ap
33ec0 70 65 6e 64 20 70 6f 73 69 74 69 6f 6e 20 61 6e  pend position an
33ec1 64 20 6f 66 66 73 65 74 20 69 6e 66 6f 72 6d 61  d offset informa
33ec2 74 69 6f 6e 2e 0a 2a 2a 20 70 6c 77 43 6f 70 79  tion..** plwCopy
33ec3 20 2d 20 63 6f 70 79 20 6e 65 78 74 20 70 6f 73   - copy next pos
33ec4 69 74 69 6f 6e 27 73 20 64 61 74 61 20 66 72 6f  ition's data fro
33ec5 6d 20 72 65 61 64 65 72 20 74 6f 20 77 72 69 74  m reader to writ
33ec6 65 72 2e 0a 2a 2a 20 70 6c 77 54 65 72 6d 69 6e  er..** plwTermin
33ec7 61 74 65 20 2d 20 61 64 64 20 61 6e 79 20 6e 65  ate - add any ne
33ec8 63 65 73 73 61 72 79 20 64 6f 63 6c 69 73 74 20  cessary doclist 
33ec9 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a  terminator..**.*
33eca 2a 20 43 61 6c 6c 69 6e 67 20 70 6c 77 41 64 64  * Calling plwAdd
33ecb 28 29 20 61 66 74 65 72 20 70 6c 77 54 65 72 6d  () after plwTerm
33ecc 69 6e 61 74 65 28 29 20 6d 61 79 20 72 65 73 75  inate() may resu
33ecd 6c 74 20 69 6e 20 61 20 63 6f 72 72 75 70 74 0a  lt in a corrupt.
33ece 2a 2a 20 64 6f 63 6c 69 73 74 2e 0a 2a 2f 0a 2f  ** doclist..*/./
33ecf 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 55 6e  * TODO(shess) Un
33ed0 74 69 6c 20 77 65 27 76 65 20 77 72 69 74 74 65  til we've writte
33ed1 6e 20 74 68 65 20 73 65 63 6f 6e 64 20 69 74 65  n the second ite
33ed2 6d 2c 20 77 65 20 63 61 6e 20 63 61 63 68 65 20  m, we can cache 
33ed3 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 69 74 65  the.** first ite
33ed4 6d 27 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  m's information.
33ed5 20 20 54 68 65 6e 20 77 65 27 64 20 68 61 76 65    Then we'd have
33ed6 20 74 68 72 65 65 20 73 74 61 74 65 73 3a 0a 2a   three states:.*
33ed7 2a 0a 2a 2a 20 2d 20 69 6e 69 74 69 61 6c 69 7a  *.** - initializ
33ed8 65 64 20 77 69 74 68 20 64 6f 63 69 64 2c 20 6e  ed with docid, n
33ed9 6f 20 70 6f 73 69 74 69 6f 6e 73 2e 0a 2a 2a 20  o positions..** 
33eda 2d 20 64 6f 63 69 64 20 61 6e 64 20 6f 6e 65 20  - docid and one 
33edb 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 20 2d 20 64  position..** - d
33edc 6f 63 69 64 20 61 6e 64 20 6d 75 6c 74 69 70 6c  ocid and multipl
33edd 65 20 70 6f 73 69 74 69 6f 6e 73 2e 0a 2a 2a 0a  e positions..**.
33ede 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 6c 61 73 74  ** Only the last
33edf 20 73 74 61 74 65 20 6e 65 65 64 73 20 74 6f 20   state needs to 
33ee0 61 63 74 75 61 6c 6c 79 20 77 72 69 74 65 20 74  actually write t
33ee1 6f 20 64 6c 77 2d 3e 62 2c 20 77 68 69 63 68 20  o dlw->b, which 
33ee2 77 6f 75 6c 64 0a 2a 2a 20 62 65 20 61 6e 20 69  would.** be an i
33ee3 6d 70 72 6f 76 65 6d 65 6e 74 20 69 6e 20 74 68  mprovement in th
33ee4 65 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 63 61  e DLCollector ca
33ee5 73 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  se..*/.typedef s
33ee6 74 72 75 63 74 20 50 4c 57 72 69 74 65 72 20 7b  truct PLWriter {
33ee7 0a 20 20 44 4c 57 72 69 74 65 72 20 2a 64 6c 77  .  DLWriter *dlw
33ee8 3b 0a 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  ;..  int iColumn
33ee9 3b 20 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74  ;    /* the last
33eea 20 63 6f 6c 75 6d 6e 20 77 72 69 74 74 65 6e 20   column written 
33eeb 2a 2f 0a 20 20 69 6e 74 20 69 50 6f 73 3b 20 20  */.  int iPos;  
33eec 20 20 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74       /* the last
33eed 20 70 6f 73 69 74 69 6f 6e 20 77 72 69 74 74 65   position writte
33eee 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73  n */.  int iOffs
33eef 65 74 3b 20 20 20 20 2f 2a 20 74 68 65 20 6c 61  et;    /* the la
33ef0 73 74 20 73 74 61 72 74 20 6f 66 66 73 65 74 20  st start offset 
33ef1 77 72 69 74 74 65 6e 20 2a 2f 0a 7d 20 50 4c 57  written */.} PLW
33ef2 72 69 74 65 72 3b 0a 0a 2f 2a 20 54 4f 44 4f 28  riter;../* TODO(
33ef3 73 68 65 73 73 29 20 49 6e 20 74 68 65 20 63 61  shess) In the ca
33ef4 73 65 20 77 68 65 72 65 20 74 68 65 20 70 61 72  se where the par
33ef5 65 6e 74 20 69 73 20 72 65 61 64 69 6e 67 20 74  ent is reading t
33ef6 68 65 73 65 20 76 61 6c 75 65 73 0a 2a 2a 20 66  hese values.** f
33ef7 72 6f 6d 20 61 20 50 4c 52 65 61 64 65 72 2c 20  rom a PLReader, 
33ef8 77 65 20 63 6f 75 6c 64 20 6f 70 74 69 6d 69 7a  we could optimiz
33ef9 65 20 74 6f 20 61 20 63 6f 70 79 20 69 66 20 74  e to a copy if t
33efa 68 61 74 20 50 4c 52 65 61 64 65 72 20 68 61 73  hat PLReader has
33efb 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70  .** the same typ
33efc 65 20 61 73 20 70 57 72 69 74 65 72 2e 0a 2a 2f  e as pWriter..*/
33efd 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 6c 77  .static void plw
33efe 41 64 64 28 50 4c 57 72 69 74 65 72 20 2a 70 57  Add(PLWriter *pW
33eff 72 69 74 65 72 2c 20 69 6e 74 20 69 43 6f 6c 75  riter, int iColu
33f00 6d 6e 2c 20 69 6e 74 20 69 50 6f 73 2c 0a 20 20  mn, int iPos,.  
33f01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33f02 20 69 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65   int iStartOffse
33f03 74 2c 20 69 6e 74 20 69 45 6e 64 4f 66 66 73 65  t, int iEndOffse
33f04 74 29 7b 0a 20 20 2f 2a 20 57 6f 72 73 74 2d 63  t){.  /* Worst-c
33f05 61 73 65 20 73 70 61 63 65 20 66 6f 72 20 50 4f  ase space for PO
33f06 53 5f 43 4f 4c 55 4d 4e 2c 20 69 43 6f 6c 75 6d  S_COLUMN, iColum
33f07 6e 2c 20 69 50 6f 73 44 65 6c 74 61 2c 0a 20 20  n, iPosDelta,.  
33f08 2a 2a 20 69 53 74 61 72 74 4f 66 66 73 65 74 44  ** iStartOffsetD
33f09 65 6c 74 61 2c 20 61 6e 64 20 69 45 6e 64 4f 66  elta, and iEndOf
33f0a 66 73 65 74 44 65 6c 74 61 2e 0a 20 20 2a 2f 0a  fsetDelta..  */.
33f0b 20 20 63 68 61 72 20 63 5b 35 2a 56 41 52 49 4e    char c[5*VARIN
33f0c 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 6e 20  T_MAX];.  int n 
33f0d 3d 20 30 3b 0a 0a 20 20 2f 2a 20 42 61 6e 20 70  = 0;..  /* Ban p
33f0e 6c 77 41 64 64 28 29 20 61 66 74 65 72 20 70 6c  lwAdd() after pl
33f0f 77 54 65 72 6d 69 6e 61 74 65 28 29 2e 20 2a 2f  wTerminate(). */
33f10 0a 20 20 61 73 73 65 72 74 28 20 70 57 72 69 74  .  assert( pWrit
33f11 65 72 2d 3e 69 50 6f 73 21 3d 2d 31 20 29 3b 0a  er->iPos!=-1 );.
33f12 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e  .  if( pWriter->
33f13 64 6c 77 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 44  dlw->iType==DL_D
33f14 4f 43 49 44 53 20 29 20 72 65 74 75 72 6e 3b 0a  OCIDS ) return;.
33f15 0a 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 21 3d  .  if( iColumn!=
33f16 70 57 72 69 74 65 72 2d 3e 69 43 6f 6c 75 6d 6e  pWriter->iColumn
33f17 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73   ){.    n += fts
33f18 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20  3PutVarint(c+n, 
33f19 50 4f 53 5f 43 4f 4c 55 4d 4e 29 3b 0a 20 20 20  POS_COLUMN);.   
33f1a 20 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72   n += fts3PutVar
33f1b 69 6e 74 28 63 2b 6e 2c 20 69 43 6f 6c 75 6d 6e  int(c+n, iColumn
33f1c 29 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  );.    pWriter->
33f1d 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 75 6d  iColumn = iColum
33f1e 6e 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  n;.    pWriter->
33f1f 69 50 6f 73 20 3d 20 30 3b 0a 20 20 20 20 70 57  iPos = 0;.    pW
33f20 72 69 74 65 72 2d 3e 69 4f 66 66 73 65 74 20 3d  riter->iOffset =
33f21 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74   0;.  }.  assert
33f22 28 20 69 50 6f 73 3e 3d 70 57 72 69 74 65 72 2d  ( iPos>=pWriter-
33f23 3e 69 50 6f 73 20 29 3b 0a 20 20 6e 20 2b 3d 20  >iPos );.  n += 
33f24 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b  fts3PutVarint(c+
33f25 6e 2c 20 50 4f 53 5f 42 41 53 45 2b 28 69 50 6f  n, POS_BASE+(iPo
33f26 73 2d 70 57 72 69 74 65 72 2d 3e 69 50 6f 73 29  s-pWriter->iPos)
33f27 29 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 50  );.  pWriter->iP
33f28 6f 73 20 3d 20 69 50 6f 73 3b 0a 20 20 69 66 28  os = iPos;.  if(
33f29 20 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69   pWriter->dlw->i
33f2a 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f  Type==DL_POSITIO
33f2b 4e 53 5f 4f 46 46 53 45 54 53 20 29 7b 0a 20 20  NS_OFFSETS ){.  
33f2c 20 20 61 73 73 65 72 74 28 20 69 53 74 61 72 74    assert( iStart
33f2d 4f 66 66 73 65 74 3e 3d 70 57 72 69 74 65 72 2d  Offset>=pWriter-
33f2e 3e 69 4f 66 66 73 65 74 20 29 3b 0a 20 20 20 20  >iOffset );.    
33f2f 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69  n += fts3PutVari
33f30 6e 74 28 63 2b 6e 2c 20 69 53 74 61 72 74 4f 66  nt(c+n, iStartOf
33f31 66 73 65 74 2d 70 57 72 69 74 65 72 2d 3e 69 4f  fset-pWriter->iO
33f32 66 66 73 65 74 29 3b 0a 20 20 20 20 70 57 72 69  ffset);.    pWri
33f33 74 65 72 2d 3e 69 4f 66 66 73 65 74 20 3d 20 69  ter->iOffset = i
33f34 53 74 61 72 74 4f 66 66 73 65 74 3b 0a 20 20 20  StartOffset;.   
33f35 20 61 73 73 65 72 74 28 20 69 45 6e 64 4f 66 66   assert( iEndOff
33f36 73 65 74 3e 3d 69 53 74 61 72 74 4f 66 66 73 65  set>=iStartOffse
33f37 74 20 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74  t );.    n += ft
33f38 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c  s3PutVarint(c+n,
33f39 20 69 45 6e 64 4f 66 66 73 65 74 2d 69 53 74 61   iEndOffset-iSta
33f3a 72 74 4f 66 66 73 65 74 29 3b 0a 20 20 7d 0a 20  rtOffset);.  }. 
33f3b 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
33f3c 64 28 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e  d(pWriter->dlw->
33f3d 62 2c 20 63 2c 20 6e 29 3b 0a 7d 0a 73 74 61 74  b, c, n);.}.stat
33f3e 69 63 20 76 6f 69 64 20 70 6c 77 43 6f 70 79 28  ic void plwCopy(
33f3f 50 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  PLWriter *pWrite
33f40 72 2c 20 50 4c 52 65 61 64 65 72 20 2a 70 52 65  r, PLReader *pRe
33f41 61 64 65 72 29 7b 0a 20 20 70 6c 77 41 64 64 28  ader){.  plwAdd(
33f42 70 57 72 69 74 65 72 2c 20 70 6c 72 43 6f 6c 75  pWriter, plrColu
33f43 6d 6e 28 70 52 65 61 64 65 72 29 2c 20 70 6c 72  mn(pReader), plr
33f44 50 6f 73 69 74 69 6f 6e 28 70 52 65 61 64 65 72  Position(pReader
33f45 29 2c 0a 20 20 20 20 20 20 20 20 20 70 6c 72 53  ),.         plrS
33f46 74 61 72 74 4f 66 66 73 65 74 28 70 52 65 61 64  tartOffset(pRead
33f47 65 72 29 2c 20 70 6c 72 45 6e 64 4f 66 66 73 65  er), plrEndOffse
33f48 74 28 70 52 65 61 64 65 72 29 29 3b 0a 7d 0a 73  t(pReader));.}.s
33f49 74 61 74 69 63 20 76 6f 69 64 20 70 6c 77 49 6e  tatic void plwIn
33f4a 69 74 28 50 4c 57 72 69 74 65 72 20 2a 70 57 72  it(PLWriter *pWr
33f4b 69 74 65 72 2c 20 44 4c 57 72 69 74 65 72 20 2a  iter, DLWriter *
33f4c 64 6c 77 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36  dlw, sqlite_int6
33f4d 34 20 69 44 6f 63 69 64 29 7b 0a 20 20 63 68 61  4 iDocid){.  cha
33f4e 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b  r c[VARINT_MAX];
33f4f 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 70 57 72  .  int n;..  pWr
33f50 69 74 65 72 2d 3e 64 6c 77 20 3d 20 64 6c 77 3b  iter->dlw = dlw;
33f51 0a 0a 20 20 2f 2a 20 44 6f 63 69 64 73 20 6d 75  ..  /* Docids mu
33f52 73 74 20 61 73 63 65 6e 64 2e 20 2a 2f 0a 20 20  st ascend. */.  
33f53 61 73 73 65 72 74 28 20 21 70 57 72 69 74 65 72  assert( !pWriter
33f54 2d 3e 64 6c 77 2d 3e 68 61 73 5f 69 50 72 65 76  ->dlw->has_iPrev
33f55 44 6f 63 69 64 20 7c 7c 20 69 44 6f 63 69 64 3e  Docid || iDocid>
33f56 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69 50  pWriter->dlw->iP
33f57 72 65 76 44 6f 63 69 64 20 29 3b 0a 20 20 6e 20  revDocid );.  n 
33f58 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28  = fts3PutVarint(
33f59 63 2c 20 69 44 6f 63 69 64 2d 70 57 72 69 74 65  c, iDocid-pWrite
33f5a 72 2d 3e 64 6c 77 2d 3e 69 50 72 65 76 44 6f 63  r->dlw->iPrevDoc
33f5b 69 64 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  id);.  dataBuffe
33f5c 72 41 70 70 65 6e 64 28 70 57 72 69 74 65 72 2d  rAppend(pWriter-
33f5d 3e 64 6c 77 2d 3e 62 2c 20 63 2c 20 6e 29 3b 0a  >dlw->b, c, n);.
33f5e 20 20 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e    pWriter->dlw->
33f5f 69 50 72 65 76 44 6f 63 69 64 20 3d 20 69 44 6f  iPrevDocid = iDo
33f60 63 69 64 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45  cid;.#ifndef NDE
33f61 42 55 47 0a 20 20 70 57 72 69 74 65 72 2d 3e 64  BUG.  pWriter->d
33f62 6c 77 2d 3e 68 61 73 5f 69 50 72 65 76 44 6f 63  lw->has_iPrevDoc
33f63 69 64 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a  id = 1;.#endif..
33f64 20 20 70 57 72 69 74 65 72 2d 3e 69 43 6f 6c 75    pWriter->iColu
33f65 6d 6e 20 3d 20 30 3b 0a 20 20 70 57 72 69 74 65  mn = 0;.  pWrite
33f66 72 2d 3e 69 50 6f 73 20 3d 20 30 3b 0a 20 20 70  r->iPos = 0;.  p
33f67 57 72 69 74 65 72 2d 3e 69 4f 66 66 73 65 74 20  Writer->iOffset 
33f68 3d 20 30 3b 0a 7d 0a 2f 2a 20 54 4f 44 4f 28 73  = 0;.}./* TODO(s
33f69 68 65 73 73 29 20 53 68 6f 75 6c 64 20 70 6c 77  hess) Should plw
33f6a 44 65 73 74 72 6f 79 28 29 20 61 6c 73 6f 20 74  Destroy() also t
33f6b 65 72 6d 69 6e 61 74 65 20 74 68 65 20 64 6f 63  erminate the doc
33f6c 6c 69 73 74 3f 20 20 42 75 74 0a 2a 2a 20 74 68  list?  But.** th
33f6d 65 6e 20 70 6c 77 44 65 73 74 72 6f 79 28 29 20  en plwDestroy() 
33f6e 77 6f 75 6c 64 20 6e 6f 20 6c 6f 6e 67 65 72 20  would no longer 
33f6f 62 65 20 6a 75 73 74 20 61 20 64 65 73 74 72 75  be just a destru
33f70 63 74 6f 72 2c 20 69 74 20 77 6f 75 6c 64 0a 2a  ctor, it would.*
33f71 2a 20 61 6c 73 6f 20 62 65 20 64 6f 69 6e 67 20  * also be doing 
33f72 77 6f 72 6b 2c 20 77 68 69 63 68 20 69 73 6e 27  work, which isn'
33f73 74 20 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 74  t consistent wit
33f74 68 20 74 68 65 20 6f 76 65 72 61 6c 6c 20 69 64  h the overall id
33f75 69 6f 6d 2e 0a 2a 2a 20 41 6e 6f 74 68 65 72 20  iom..** Another 
33f76 6f 70 74 69 6f 6e 20 77 6f 75 6c 64 20 62 65 20  option would be 
33f77 66 6f 72 20 70 6c 77 41 64 64 28 29 20 74 6f 20  for plwAdd() to 
33f78 61 6c 77 61 79 73 20 61 70 70 65 6e 64 20 61 6e  always append an
33f79 79 20 6e 65 63 65 73 73 61 72 79 0a 2a 2a 20 74  y necessary.** t
33f7a 65 72 6d 69 6e 61 74 6f 72 2c 20 73 6f 20 74 68  erminator, so th
33f7b 61 74 20 74 68 65 20 6f 75 74 70 75 74 20 69 73  at the output is
33f7c 20 61 6c 77 61 79 73 20 63 6f 72 72 65 63 74 2e   always correct.
33f7d 20 20 42 75 74 20 74 68 61 74 20 77 6f 75 6c 64    But that would
33f7e 0a 2a 2a 20 61 64 64 20 69 6e 63 72 65 6d 65 6e  .** add incremen
33f7f 74 61 6c 20 77 6f 72 6b 20 74 6f 20 74 68 65 20  tal work to the 
33f80 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 69 74 68  common case with
33f81 20 74 68 65 20 6f 6e 6c 79 20 62 65 6e 65 66 69   the only benefi
33f82 74 20 62 65 69 6e 67 0a 2a 2a 20 41 50 49 20 65  t being.** API e
33f83 6c 65 67 61 6e 63 65 2e 20 20 50 75 6e 74 20 66  legance.  Punt f
33f84 6f 72 20 6e 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69  or now..*/.stati
33f85 63 20 76 6f 69 64 20 70 6c 77 54 65 72 6d 69 6e  c void plwTermin
33f86 61 74 65 28 50 4c 57 72 69 74 65 72 20 2a 70 57  ate(PLWriter *pW
33f87 72 69 74 65 72 29 7b 0a 20 20 69 66 28 20 70 57  riter){.  if( pW
33f88 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69 54 79 70  riter->dlw->iTyp
33f89 65 3e 44 4c 5f 44 4f 43 49 44 53 20 29 7b 0a 20  e>DL_DOCIDS ){. 
33f8a 20 20 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54     char c[VARINT
33f8b 5f 4d 41 58 5d 3b 0a 20 20 20 20 69 6e 74 20 6e  _MAX];.    int n
33f8c 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74   = fts3PutVarint
33f8d 28 63 2c 20 50 4f 53 5f 45 4e 44 29 3b 0a 20 20  (c, POS_END);.  
33f8e 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
33f8f 6e 64 28 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d  nd(pWriter->dlw-
33f90 3e 62 2c 20 63 2c 20 6e 29 3b 0a 20 20 7d 0a 23  >b, c, n);.  }.#
33f91 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
33f92 2f 2a 20 4d 61 72 6b 20 61 73 20 74 65 72 6d 69  /* Mark as termi
33f93 6e 61 74 65 64 20 66 6f 72 20 61 73 73 65 72 74  nated for assert
33f94 20 69 6e 20 70 6c 77 41 64 64 28 29 2e 20 2a 2f   in plwAdd(). */
33f95 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 50 6f 73  .  pWriter->iPos
33f96 20 3d 20 2d 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a   = -1;.#endif.}.
33f97 73 74 61 74 69 63 20 76 6f 69 64 20 70 6c 77 44  static void plwD
33f98 65 73 74 72 6f 79 28 50 4c 57 72 69 74 65 72 20  estroy(PLWriter 
33f99 2a 70 57 72 69 74 65 72 29 7b 0a 20 20 53 43 52  *pWriter){.  SCR
33f9a 41 4d 42 4c 45 28 70 57 72 69 74 65 72 29 3b 0a  AMBLE(pWriter);.
33f9b 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
33f9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33f9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33f9e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33f9f 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44 4c 43 6f  *******/./* DLCo
33fa0 6c 6c 65 63 74 6f 72 20 77 72 61 70 73 20 50 4c  llector wraps PL
33fa1 57 72 69 74 65 72 20 61 6e 64 20 44 4c 57 72 69  Writer and DLWri
33fa2 74 65 72 20 74 6f 20 70 72 6f 76 69 64 65 20 61  ter to provide a
33fa3 0a 2a 2a 20 64 79 6e 61 6d 69 63 61 6c 6c 79 2d  .** dynamically-
33fa4 61 6c 6c 6f 63 61 74 65 64 20 64 6f 63 6c 69 73  allocated doclis
33fa5 74 20 61 72 65 61 20 74 6f 20 75 73 65 20 64 75  t area to use du
33fa6 72 69 6e 67 20 74 6f 6b 65 6e 69 7a 61 74 69 6f  ring tokenizatio
33fa7 6e 2e 0a 2a 2a 0a 2a 2a 20 64 6c 63 4e 65 77 20  n..**.** dlcNew 
33fa8 2d 20 6d 61 6c 6c 6f 63 20 75 70 20 61 6e 64 20  - malloc up and 
33fa9 69 6e 69 74 69 61 6c 69 7a 65 20 61 20 63 6f 6c  initialize a col
33faa 6c 65 63 74 6f 72 2e 0a 2a 2a 20 64 6c 63 44 65  lector..** dlcDe
33fab 6c 65 74 65 20 2d 20 64 65 73 74 72 6f 79 20 61  lete - destroy a
33fac 20 63 6f 6c 6c 65 63 74 6f 72 20 61 6e 64 20 61   collector and a
33fad 6c 6c 20 63 6f 6e 74 61 69 6e 65 64 20 69 74 65  ll contained ite
33fae 6d 73 2e 0a 2a 2a 20 64 6c 63 41 64 64 50 6f 73  ms..** dlcAddPos
33faf 20 2d 20 61 70 70 65 6e 64 20 70 6f 73 69 74 69   - append positi
33fb0 6f 6e 20 61 6e 64 20 6f 66 66 73 65 74 20 69 6e  on and offset in
33fb1 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 64 6c  formation..** dl
33fb2 63 41 64 64 44 6f 63 6c 69 73 74 20 2d 20 61 64  cAddDoclist - ad
33fb3 64 20 74 68 65 20 63 6f 6c 6c 65 63 74 65 64 20  d the collected 
33fb4 64 6f 63 6c 69 73 74 20 74 6f 20 74 68 65 20 67  doclist to the g
33fb5 69 76 65 6e 20 62 75 66 66 65 72 2e 0a 2a 2a 20  iven buffer..** 
33fb6 64 6c 63 4e 65 78 74 20 2d 20 74 65 72 6d 69 6e  dlcNext - termin
33fb7 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ate the current 
33fb8 64 6f 63 75 6d 65 6e 74 20 61 6e 64 20 6f 70 65  document and ope
33fb9 6e 20 61 6e 6f 74 68 65 72 2e 0a 2a 2f 0a 74 79  n another..*/.ty
33fba 70 65 64 65 66 20 73 74 72 75 63 74 20 44 4c 43  pedef struct DLC
33fbb 6f 6c 6c 65 63 74 6f 72 20 7b 0a 20 20 44 61 74  ollector {.  Dat
33fbc 61 42 75 66 66 65 72 20 62 3b 0a 20 20 44 4c 57  aBuffer b;.  DLW
33fbd 72 69 74 65 72 20 64 6c 77 3b 0a 20 20 50 4c 57  riter dlw;.  PLW
33fbe 72 69 74 65 72 20 70 6c 77 3b 0a 7d 20 44 4c 43  riter plw;.} DLC
33fbf 6f 6c 6c 65 63 74 6f 72 3b 0a 0a 2f 2a 20 54 4f  ollector;../* TO
33fc0 44 4f 28 73 68 65 73 73 29 20 54 68 69 73 20 63  DO(shess) This c
33fc1 6f 75 6c 64 20 61 6c 73 6f 20 62 65 20 64 6f 6e  ould also be don
33fc2 65 20 62 79 20 63 61 6c 6c 69 6e 67 20 70 6c 77  e by calling plw
33fc3 54 65 72 6d 69 6e 61 74 65 28 29 20 61 6e 64 0a  Terminate() and.
33fc4 2a 2a 20 64 61 74 61 42 75 66 66 65 72 41 70 70  ** dataBufferApp
33fc5 65 6e 64 28 29 2e 20 20 49 20 74 72 69 65 64 20  end().  I tried 
33fc6 74 68 61 74 2c 20 65 78 70 65 63 74 69 6e 67 20  that, expecting 
33fc7 6e 6f 6d 69 6e 61 6c 20 70 65 72 66 6f 72 6d 61  nominal performa
33fc8 6e 63 65 0a 2a 2a 20 64 69 66 66 65 72 65 6e 63  nce.** differenc
33fc9 65 73 2c 20 62 75 74 20 69 74 20 73 65 65 6d 65  es, but it seeme
33fca 64 20 74 6f 20 70 72 65 74 74 79 20 72 65 6c 69  d to pretty reli
33fcb 61 62 6c 79 20 62 65 20 77 6f 72 74 68 20 31 25  ably be worth 1%
33fcc 20 74 6f 20 63 6f 64 65 0a 2a 2a 20 69 74 20 74   to code.** it t
33fcd 68 69 73 20 77 61 79 2e 20 20 49 20 73 75 73 70  his way.  I susp
33fce 65 63 74 20 69 74 20 69 73 20 74 68 65 20 69 6e  ect it is the in
33fcf 63 72 65 6d 65 6e 74 61 6c 20 6d 61 6c 6c 6f 63  cremental malloc
33fd0 20 6f 76 65 72 68 65 61 64 20 28 73 6f 6d 65 0a   overhead (some.
33fd1 2a 2a 20 70 65 72 63 65 6e 74 61 67 65 20 6f 66  ** percentage of
33fd2 20 74 68 65 20 70 6c 77 54 65 72 6d 69 6e 61 74   the plwTerminat
33fd3 65 28 29 20 63 61 6c 6c 73 20 77 69 6c 6c 20 63  e() calls will c
33fd4 61 75 73 65 20 61 20 72 65 61 6c 6c 6f 63 29 2c  ause a realloc),
33fd5 20 73 6f 0a 2a 2a 20 74 68 69 73 20 6d 69 67 68   so.** this migh
33fd6 74 20 62 65 20 77 6f 72 74 68 20 72 65 76 69 73  t be worth revis
33fd7 69 74 69 6e 67 20 69 66 20 74 68 65 20 44 61 74  iting if the Dat
33fd8 61 42 75 66 66 65 72 20 69 6d 70 6c 65 6d 65 6e  aBuffer implemen
33fd9 74 61 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65  tation.** change
33fda 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
33fdb 64 20 64 6c 63 41 64 64 44 6f 63 6c 69 73 74 28  d dlcAddDoclist(
33fdc 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f  DLCollector *pCo
33fdd 6c 6c 65 63 74 6f 72 2c 20 44 61 74 61 42 75 66  llector, DataBuf
33fde 66 65 72 20 2a 62 29 7b 0a 20 20 69 66 28 20 70  fer *b){.  if( p
33fdf 43 6f 6c 6c 65 63 74 6f 72 2d 3e 64 6c 77 2e 69  Collector->dlw.i
33fe0 54 79 70 65 3e 44 4c 5f 44 4f 43 49 44 53 20 29  Type>DL_DOCIDS )
33fe1 7b 0a 20 20 20 20 63 68 61 72 20 63 5b 56 41 52  {.    char c[VAR
33fe2 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 20 20 69 6e  INT_MAX];.    in
33fe3 74 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72  t n = fts3PutVar
33fe4 69 6e 74 28 63 2c 20 50 4f 53 5f 45 4e 44 29 3b  int(c, POS_END);
33fe5 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41  .    dataBufferA
33fe6 70 70 65 6e 64 32 28 62 2c 20 70 43 6f 6c 6c 65  ppend2(b, pColle
33fe7 63 74 6f 72 2d 3e 62 2e 70 44 61 74 61 2c 20 70  ctor->b.pData, p
33fe8 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62 2e 6e 44 61  Collector->b.nDa
33fe9 74 61 2c 20 63 2c 20 6e 29 3b 0a 20 20 7d 65 6c  ta, c, n);.  }el
33fea 73 65 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66  se{.    dataBuff
33feb 65 72 41 70 70 65 6e 64 28 62 2c 20 70 43 6f 6c  erAppend(b, pCol
33fec 6c 65 63 74 6f 72 2d 3e 62 2e 70 44 61 74 61 2c  lector->b.pData,
33fed 20 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62 2e 6e   pCollector->b.n
33fee 44 61 74 61 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61  Data);.  }.}.sta
33fef 74 69 63 20 76 6f 69 64 20 64 6c 63 4e 65 78 74  tic void dlcNext
33ff0 28 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43  (DLCollector *pC
33ff1 6f 6c 6c 65 63 74 6f 72 2c 20 73 71 6c 69 74 65  ollector, sqlite
33ff2 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 29 7b 0a  _int64 iDocid){.
33ff3 20 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 28 26    plwTerminate(&
33ff4 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 29  pCollector->plw)
33ff5 3b 0a 20 20 70 6c 77 44 65 73 74 72 6f 79 28 26  ;.  plwDestroy(&
33ff6 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 29  pCollector->plw)
33ff7 3b 0a 20 20 70 6c 77 49 6e 69 74 28 26 70 43 6f  ;.  plwInit(&pCo
33ff8 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 2c 20 26 70  llector->plw, &p
33ff9 43 6f 6c 6c 65 63 74 6f 72 2d 3e 64 6c 77 2c 20  Collector->dlw, 
33ffa 69 44 6f 63 69 64 29 3b 0a 7d 0a 73 74 61 74 69  iDocid);.}.stati
33ffb 63 20 76 6f 69 64 20 64 6c 63 41 64 64 50 6f 73  c void dlcAddPos
33ffc 28 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43  (DLCollector *pC
33ffd 6f 6c 6c 65 63 74 6f 72 2c 20 69 6e 74 20 69 43  ollector, int iC
33ffe 6f 6c 75 6d 6e 2c 20 69 6e 74 20 69 50 6f 73 2c  olumn, int iPos,
33fff 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
34000 20 20 20 20 20 20 20 69 6e 74 20 69 53 74 61 72         int iStar
34001 74 4f 66 66 73 65 74 2c 20 69 6e 74 20 69 45 6e  tOffset, int iEn
34002 64 4f 66 66 73 65 74 29 7b 0a 20 20 70 6c 77 41  dOffset){.  plwA
34003 64 64 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e  dd(&pCollector->
34004 70 6c 77 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 50  plw, iColumn, iP
34005 6f 73 2c 20 69 53 74 61 72 74 4f 66 66 73 65 74  os, iStartOffset
34006 2c 20 69 45 6e 64 4f 66 66 73 65 74 29 3b 0a 7d  , iEndOffset);.}
34007 0a 0a 73 74 61 74 69 63 20 44 4c 43 6f 6c 6c 65  ..static DLColle
34008 63 74 6f 72 20 2a 64 6c 63 4e 65 77 28 73 71 6c  ctor *dlcNew(sql
34009 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64  ite_int64 iDocid
3400a 2c 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54  , DocListType iT
3400b 79 70 65 29 7b 0a 20 20 44 4c 43 6f 6c 6c 65 63  ype){.  DLCollec
3400c 74 6f 72 20 2a 70 43 6f 6c 6c 65 63 74 6f 72 20  tor *pCollector 
3400d 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
3400e 28 73 69 7a 65 6f 66 28 44 4c 43 6f 6c 6c 65 63  (sizeof(DLCollec
3400f 74 6f 72 29 29 3b 0a 20 20 64 61 74 61 42 75 66  tor));.  dataBuf
34010 66 65 72 49 6e 69 74 28 26 70 43 6f 6c 6c 65 63  ferInit(&pCollec
34011 74 6f 72 2d 3e 62 2c 20 30 29 3b 0a 20 20 64 6c  tor->b, 0);.  dl
34012 77 49 6e 69 74 28 26 70 43 6f 6c 6c 65 63 74 6f  wInit(&pCollecto
34013 72 2d 3e 64 6c 77 2c 20 69 54 79 70 65 2c 20 26  r->dlw, iType, &
34014 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62 29 3b 0a  pCollector->b);.
34015 20 20 70 6c 77 49 6e 69 74 28 26 70 43 6f 6c 6c    plwInit(&pColl
34016 65 63 74 6f 72 2d 3e 70 6c 77 2c 20 26 70 43 6f  ector->plw, &pCo
34017 6c 6c 65 63 74 6f 72 2d 3e 64 6c 77 2c 20 69 44  llector->dlw, iD
34018 6f 63 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  ocid);.  return 
34019 70 43 6f 6c 6c 65 63 74 6f 72 3b 0a 7d 0a 73 74  pCollector;.}.st
3401a 61 74 69 63 20 76 6f 69 64 20 64 6c 63 44 65 6c  atic void dlcDel
3401b 65 74 65 28 44 4c 43 6f 6c 6c 65 63 74 6f 72 20  ete(DLCollector 
3401c 2a 70 43 6f 6c 6c 65 63 74 6f 72 29 7b 0a 20 20  *pCollector){.  
3401d 70 6c 77 44 65 73 74 72 6f 79 28 26 70 43 6f 6c  plwDestroy(&pCol
3401e 6c 65 63 74 6f 72 2d 3e 70 6c 77 29 3b 0a 20 20  lector->plw);.  
3401f 64 6c 77 44 65 73 74 72 6f 79 28 26 70 43 6f 6c  dlwDestroy(&pCol
34020 6c 65 63 74 6f 72 2d 3e 64 6c 77 29 3b 0a 20 20  lector->dlw);.  
34021 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
34022 79 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62  y(&pCollector->b
34023 29 3b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 43  );.  SCRAMBLE(pC
34024 6f 6c 6c 65 63 74 6f 72 29 3b 0a 20 20 73 71 6c  ollector);.  sql
34025 69 74 65 33 5f 66 72 65 65 28 70 43 6f 6c 6c 65  ite3_free(pColle
34026 63 74 6f 72 29 3b 0a 7d 0a 0a 0a 2f 2a 20 43 6f  ctor);.}.../* Co
34027 70 79 20 74 68 65 20 64 6f 63 6c 69 73 74 20 64  py the doclist d
34028 61 74 61 20 6f 66 20 69 54 79 70 65 20 69 6e 20  ata of iType in 
34029 70 44 61 74 61 2f 6e 44 61 74 61 20 69 6e 74 6f  pData/nData into
3402a 20 2a 6f 75 74 2c 20 74 72 69 6d 6d 69 6e 67 0a   *out, trimming.
3402b 2a 2a 20 75 6e 6e 65 63 65 73 73 61 72 79 20 64  ** unnecessary d
3402c 61 74 61 20 61 73 20 77 65 20 67 6f 2e 20 20 4f  ata as we go.  O
3402d 6e 6c 79 20 63 6f 6c 75 6d 6e 73 20 6d 61 74 63  nly columns matc
3402e 68 69 6e 67 20 69 43 6f 6c 75 6d 6e 20 61 72 65  hing iColumn are
3402f 0a 2a 2a 20 63 6f 70 69 65 64 2c 20 61 6c 6c 20  .** copied, all 
34030 63 6f 6c 75 6d 6e 73 20 63 6f 70 69 65 64 20 69  columns copied i
34031 66 20 69 43 6f 6c 75 6d 6e 20 69 73 20 2d 31 2e  f iColumn is -1.
34032 20 20 45 6c 65 6d 65 6e 74 73 20 77 69 74 68 20    Elements with 
34033 6e 6f 0a 2a 2a 20 6d 61 74 63 68 69 6e 67 20 63  no.** matching c
34034 6f 6c 75 6d 6e 73 20 61 72 65 20 64 72 6f 70 70  olumns are dropp
34035 65 64 2e 20 20 54 68 65 20 6f 75 74 70 75 74 20  ed.  The output 
34036 69 73 20 61 6e 20 69 4f 75 74 54 79 70 65 20 64  is an iOutType d
34037 6f 63 6c 69 73 74 2e 0a 2a 2f 0a 2f 2a 20 4e 4f  oclist..*/./* NO
34038 54 45 28 73 68 65 73 73 29 20 54 68 69 73 20 63  TE(shess) This c
34039 6f 64 65 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69  ode is only vali
3403a 64 20 61 66 74 65 72 20 61 6c 6c 20 64 6f 63 6c  d after all docl
3403b 69 73 74 73 20 61 72 65 20 6d 65 72 67 65 64 2e  ists are merged.
3403c 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 72  .** If this is r
3403d 75 6e 20 62 65 66 6f 72 65 20 6d 65 72 67 65 73  un before merges
3403e 2c 20 74 68 65 6e 20 64 6f 63 6c 69 73 74 20 69  , then doclist i
3403f 74 65 6d 73 20 77 68 69 63 68 20 72 65 70 72 65  tems which repre
34040 73 65 6e 74 0a 2a 2a 20 64 65 6c 65 74 69 6f 6e  sent.** deletion
34041 20 77 69 6c 6c 20 62 65 20 74 72 69 6d 6d 65 64   will be trimmed
34042 2c 20 61 6e 64 20 77 69 6c 6c 20 74 68 75 73 20  , and will thus 
34043 6e 6f 74 20 65 66 66 65 63 74 20 61 20 64 65 6c  not effect a del
34044 65 74 69 6f 6e 0a 2a 2a 20 64 75 72 69 6e 67 20  etion.** during 
34045 74 68 65 20 6d 65 72 67 65 2e 0a 2a 2f 0a 73 74  the merge..*/.st
34046 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c 69 73  atic void docLis
34047 74 54 72 69 6d 28 44 6f 63 4c 69 73 74 54 79 70  tTrim(DocListTyp
34048 65 20 69 54 79 70 65 2c 20 63 6f 6e 73 74 20 63  e iType, const c
34049 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20  har *pData, int 
3404a 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20  nData,.         
3404b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
3404c 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 44 6f 63 4c  nt iColumn, DocL
3404d 69 73 74 54 79 70 65 20 69 4f 75 74 54 79 70 65  istType iOutType
3404e 2c 20 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75  , DataBuffer *ou
3404f 74 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 64  t){.  DLReader d
34050 6c 52 65 61 64 65 72 3b 0a 20 20 44 4c 57 72 69  lReader;.  DLWri
34051 74 65 72 20 64 6c 57 72 69 74 65 72 3b 0a 0a 20  ter dlWriter;.. 
34052 20 61 73 73 65 72 74 28 20 69 4f 75 74 54 79 70   assert( iOutTyp
34053 65 3c 3d 69 54 79 70 65 20 29 3b 0a 0a 20 20 64  e<=iType );..  d
34054 6c 72 49 6e 69 74 28 26 64 6c 52 65 61 64 65 72  lrInit(&dlReader
34055 2c 20 69 54 79 70 65 2c 20 70 44 61 74 61 2c 20  , iType, pData, 
34056 6e 44 61 74 61 29 3b 0a 20 20 64 6c 77 49 6e 69  nData);.  dlwIni
34057 74 28 26 64 6c 57 72 69 74 65 72 2c 20 69 4f 75  t(&dlWriter, iOu
34058 74 54 79 70 65 2c 20 6f 75 74 29 3b 0a 0a 20 20  tType, out);..  
34059 77 68 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64  while( !dlrAtEnd
3405a 28 26 64 6c 52 65 61 64 65 72 29 20 29 7b 0a 20  (&dlReader) ){. 
3405b 20 20 20 50 4c 52 65 61 64 65 72 20 70 6c 52 65     PLReader plRe
3405c 61 64 65 72 3b 0a 20 20 20 20 50 4c 57 72 69 74  ader;.    PLWrit
3405d 65 72 20 70 6c 57 72 69 74 65 72 3b 0a 20 20 20  er plWriter;.   
3405e 20 69 6e 74 20 6d 61 74 63 68 20 3d 20 30 3b 0a   int match = 0;.
3405f 0a 20 20 20 20 70 6c 72 49 6e 69 74 28 26 70 6c  .    plrInit(&pl
34060 52 65 61 64 65 72 2c 20 26 64 6c 52 65 61 64 65  Reader, &dlReade
34061 72 29 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20  r);..    while( 
34062 21 70 6c 72 41 74 45 6e 64 28 26 70 6c 52 65 61  !plrAtEnd(&plRea
34063 64 65 72 29 20 29 7b 0a 20 20 20 20 20 20 69 66  der) ){.      if
34064 28 20 69 43 6f 6c 75 6d 6e 3d 3d 2d 31 20 7c 7c  ( iColumn==-1 ||
34065 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 70 6c 52 65   plrColumn(&plRe
34066 61 64 65 72 29 3d 3d 69 43 6f 6c 75 6d 6e 20 29  ader)==iColumn )
34067 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 6d  {.        if( !m
34068 61 74 63 68 20 29 7b 0a 20 20 20 20 20 20 20 20  atch ){.        
34069 20 20 70 6c 77 49 6e 69 74 28 26 70 6c 57 72 69    plwInit(&plWri
3406a 74 65 72 2c 20 26 64 6c 57 72 69 74 65 72 2c 20  ter, &dlWriter, 
3406b 64 6c 72 44 6f 63 69 64 28 26 64 6c 52 65 61 64  dlrDocid(&dlRead
3406c 65 72 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20  er));.          
3406d 6d 61 74 63 68 20 3d 20 31 3b 0a 20 20 20 20 20  match = 1;.     
3406e 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 6c 77     }.        plw
3406f 41 64 64 28 26 70 6c 57 72 69 74 65 72 2c 20 70  Add(&plWriter, p
34070 6c 72 43 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64  lrColumn(&plRead
34071 65 72 29 2c 20 70 6c 72 50 6f 73 69 74 69 6f 6e  er), plrPosition
34072 28 26 70 6c 52 65 61 64 65 72 29 2c 0a 20 20 20  (&plReader),.   
34073 20 20 20 20 20 20 20 20 20 20 20 20 70 6c 72 53              plrS
34074 74 61 72 74 4f 66 66 73 65 74 28 26 70 6c 52 65  tartOffset(&plRe
34075 61 64 65 72 29 2c 20 70 6c 72 45 6e 64 4f 66 66  ader), plrEndOff
34076 73 65 74 28 26 70 6c 52 65 61 64 65 72 29 29 3b  set(&plReader));
34077 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
34078 6c 72 53 74 65 70 28 26 70 6c 52 65 61 64 65 72  lrStep(&plReader
34079 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
3407a 20 6d 61 74 63 68 20 29 7b 0a 20 20 20 20 20 20   match ){.      
3407b 70 6c 77 54 65 72 6d 69 6e 61 74 65 28 26 70 6c  plwTerminate(&pl
3407c 57 72 69 74 65 72 29 3b 0a 20 20 20 20 20 20 70  Writer);.      p
3407d 6c 77 44 65 73 74 72 6f 79 28 26 70 6c 57 72 69  lwDestroy(&plWri
3407e 74 65 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ter);.    }..   
3407f 20 70 6c 72 44 65 73 74 72 6f 79 28 26 70 6c 52   plrDestroy(&plR
34080 65 61 64 65 72 29 3b 0a 20 20 20 20 64 6c 72 53  eader);.    dlrS
34081 74 65 70 28 26 64 6c 52 65 61 64 65 72 29 3b 0a  tep(&dlReader);.
34082 20 20 7d 0a 20 20 64 6c 77 44 65 73 74 72 6f 79    }.  dlwDestroy
34083 28 26 64 6c 57 72 69 74 65 72 29 3b 0a 20 20 64  (&dlWriter);.  d
34084 6c 72 44 65 73 74 72 6f 79 28 26 64 6c 52 65 61  lrDestroy(&dlRea
34085 64 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 55 73 65 64  der);.}../* Used
34086 20 62 79 20 64 6f 63 4c 69 73 74 4d 65 72 67 65   by docListMerge
34087 28 29 20 74 6f 20 6b 65 65 70 20 64 6f 63 6c 69  () to keep docli
34088 73 74 73 20 69 6e 20 74 68 65 20 61 73 63 65 6e  sts in the ascen
34089 64 69 6e 67 20 6f 72 64 65 72 20 62 79 0a 2a 2a  ding order by.**
3408a 20 64 6f 63 69 64 2c 20 74 68 65 6e 20 61 73 63   docid, then asc
3408b 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20  ending order by 
3408c 61 67 65 20 28 73 6f 20 74 68 65 20 6e 65 77 65  age (so the newe
3408d 73 74 20 63 6f 6d 65 73 20 66 69 72 73 74 29 2e  st comes first).
3408e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
3408f 63 74 20 4f 72 64 65 72 65 64 44 4c 52 65 61 64  ct OrderedDLRead
34090 65 72 20 7b 0a 20 20 44 4c 52 65 61 64 65 72 20  er {.  DLReader 
34091 2a 70 52 65 61 64 65 72 3b 0a 0a 20 20 2f 2a 20  *pReader;..  /* 
34092 54 4f 44 4f 28 73 68 65 73 73 29 20 49 66 20 77  TODO(shess) If w
34093 65 20 61 73 73 75 6d 65 20 74 68 61 74 20 64 6f  e assume that do
34094 63 4c 69 73 74 4d 65 72 67 65 20 70 52 65 61 64  cListMerge pRead
34095 65 72 73 20 69 73 20 6f 72 64 65 72 65 64 20 62  ers is ordered b
34096 79 0a 20 20 2a 2a 20 61 67 65 20 28 77 68 69 63  y.  ** age (whic
34097 68 20 77 65 20 64 6f 29 2c 20 74 68 65 6e 20 77  h we do), then w
34098 65 20 63 6f 75 6c 64 20 75 73 65 20 70 52 65 61  e could use pRea
34099 64 65 72 20 63 6f 6d 70 61 72 69 73 6f 6e 73 20  der comparisons 
3409a 74 6f 20 62 72 65 61 6b 0a 20 20 2a 2a 20 74 69  to break.  ** ti
3409b 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69  es..  */.  int i
3409c 64 78 3b 0a 7d 20 4f 72 64 65 72 65 64 44 4c 52  dx;.} OrderedDLR
3409d 65 61 64 65 72 3b 0a 0a 2f 2a 20 4f 72 64 65 72  eader;../* Order
3409e 20 65 6f 66 20 74 6f 20 65 6e 64 2c 20 74 68 65   eof to end, the
3409f 6e 20 62 79 20 64 6f 63 69 64 20 61 73 63 2c 20  n by docid asc, 
340a0 69 64 78 20 64 65 73 63 2e 20 2a 2f 0a 73 74 61  idx desc. */.sta
340a1 74 69 63 20 69 6e 74 20 6f 72 64 65 72 65 64 44  tic int orderedD
340a2 4c 52 65 61 64 65 72 43 6d 70 28 4f 72 64 65 72  LReaderCmp(Order
340a3 65 64 44 4c 52 65 61 64 65 72 20 2a 72 31 2c 20  edDLReader *r1, 
340a4 4f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72 20  OrderedDLReader 
340a5 2a 72 32 29 7b 0a 20 20 69 66 28 20 64 6c 72 41  *r2){.  if( dlrA
340a6 74 45 6e 64 28 72 31 2d 3e 70 52 65 61 64 65 72  tEnd(r1->pReader
340a7 29 20 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72  ) ){.    if( dlr
340a8 41 74 45 6e 64 28 72 32 2d 3e 70 52 65 61 64 65  AtEnd(r2->pReade
340a9 72 29 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20  r) ) return 0;  
340aa 2f 2a 20 42 6f 74 68 20 61 74 45 6e 64 28 29 2e  /* Both atEnd().
340ab 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31   */.    return 1
340ac 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
340ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
340ae 2a 20 4f 6e 6c 79 20 72 31 20 61 74 45 6e 64 28  * Only r1 atEnd(
340af 29 2e 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  ). */.  }.  if( 
340b0 64 6c 72 41 74 45 6e 64 28 72 32 2d 3e 70 52 65  dlrAtEnd(r2->pRe
340b1 61 64 65 72 29 20 29 20 72 65 74 75 72 6e 20 2d  ader) ) return -
340b2 31 3b 20 20 20 2f 2a 20 4f 6e 6c 79 20 72 32 20  1;   /* Only r2 
340b3 61 74 45 6e 64 28 29 2e 20 2a 2f 0a 0a 20 20 69  atEnd(). */..  i
340b4 66 28 20 64 6c 72 44 6f 63 69 64 28 72 31 2d 3e  f( dlrDocid(r1->
340b5 70 52 65 61 64 65 72 29 3c 64 6c 72 44 6f 63 69  pReader)<dlrDoci
340b6 64 28 72 32 2d 3e 70 52 65 61 64 65 72 29 20 29  d(r2->pReader) )
340b7 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66   return -1;.  if
340b8 28 20 64 6c 72 44 6f 63 69 64 28 72 31 2d 3e 70  ( dlrDocid(r1->p
340b9 52 65 61 64 65 72 29 3e 64 6c 72 44 6f 63 69 64  Reader)>dlrDocid
340ba 28 72 32 2d 3e 70 52 65 61 64 65 72 29 20 29 20  (r2->pReader) ) 
340bb 72 65 74 75 72 6e 20 31 3b 0a 0a 20 20 2f 2a 20  return 1;..  /* 
340bc 44 65 73 63 65 6e 64 69 6e 67 20 6f 6e 20 69 64  Descending on id
340bd 78 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72  x. */.  return r
340be 32 2d 3e 69 64 78 2d 72 31 2d 3e 69 64 78 3b 0a  2->idx-r1->idx;.
340bf 7d 0a 0a 2f 2a 20 42 75 62 62 6c 65 20 70 5b 30  }../* Bubble p[0
340c0 5d 20 74 6f 20 61 70 70 72 6f 70 72 69 61 74 65  ] to appropriate
340c1 20 70 6c 61 63 65 20 69 6e 20 70 5b 31 2e 2e 6e   place in p[1..n
340c2 2d 31 5d 2e 20 20 41 73 73 75 6d 65 73 20 74 68  -1].  Assumes th
340c3 61 74 0a 2a 2a 20 70 5b 31 2e 2e 6e 2d 31 5d 20  at.** p[1..n-1] 
340c4 69 73 20 61 6c 72 65 61 64 79 20 73 6f 72 74 65  is already sorte
340c5 64 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68  d..*/./* TODO(sh
340c6 65 73 73 29 20 49 73 20 74 68 69 73 20 66 72 65  ess) Is this fre
340c7 71 75 65 6e 74 20 65 6e 6f 75 67 68 20 74 6f 20  quent enough to 
340c8 77 61 72 72 61 6e 74 20 61 20 62 69 6e 61 72 79  warrant a binary
340c9 20 73 65 61 72 63 68 3f 0a 2a 2a 20 42 65 66 6f   search?.** Befo
340ca 72 65 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20  re implementing 
340cb 74 68 61 74 2c 20 69 6e 73 74 72 75 6d 65 6e 74  that, instrument
340cc 20 74 68 65 20 63 6f 64 65 20 74 6f 20 63 68 65   the code to che
340cd 63 6b 2e 20 20 49 6e 20 6d 6f 73 74 0a 2a 2a 20  ck.  In most.** 
340ce 63 75 72 72 65 6e 74 20 75 73 61 67 65 2c 20 49  current usage, I
340cf 20 65 78 70 65 63 74 20 74 68 61 74 20 70 5b 30   expect that p[0
340d0 5d 20 77 69 6c 6c 20 62 65 20 6c 65 73 73 20 74  ] will be less t
340d1 68 61 6e 20 70 5b 31 5d 20 61 20 76 65 72 79 0a  han p[1] a very.
340d2 2a 2a 20 68 69 67 68 20 70 72 6f 70 6f 72 74 69  ** high proporti
340d3 6f 6e 20 6f 66 20 74 68 65 20 74 69 6d 65 2e 0a  on of the time..
340d4 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f  */.static void o
340d5 72 64 65 72 65 64 44 4c 52 65 61 64 65 72 52 65  rderedDLReaderRe
340d6 6f 72 64 65 72 28 4f 72 64 65 72 65 64 44 4c 52  order(OrderedDLR
340d7 65 61 64 65 72 20 2a 70 2c 20 69 6e 74 20 6e 29  eader *p, int n)
340d8 7b 0a 20 20 77 68 69 6c 65 28 20 6e 3e 31 20 26  {.  while( n>1 &
340d9 26 20 6f 72 64 65 72 65 64 44 4c 52 65 61 64 65  & orderedDLReade
340da 72 43 6d 70 28 70 2c 20 70 2b 31 29 3e 30 20 29  rCmp(p, p+1)>0 )
340db 7b 0a 20 20 20 20 4f 72 64 65 72 65 64 44 4c 52  {.    OrderedDLR
340dc 65 61 64 65 72 20 74 6d 70 20 3d 20 70 5b 30 5d  eader tmp = p[0]
340dd 3b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 70 5b 31  ;.    p[0] = p[1
340de 5d 3b 0a 20 20 20 20 70 5b 31 5d 20 3d 20 74 6d  ];.    p[1] = tm
340df 70 3b 0a 20 20 20 20 6e 2d 2d 3b 0a 20 20 20 20  p;.    n--;.    
340e0 70 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 47  p++;.  }.}../* G
340e1 69 76 65 6e 20 61 6e 20 61 72 72 61 79 20 6f 66  iven an array of
340e2 20 64 6f 63 6c 69 73 74 20 72 65 61 64 65 72 73   doclist readers
340e3 2c 20 6d 65 72 67 65 20 74 68 65 69 72 20 64 6f  , merge their do
340e4 63 6c 69 73 74 20 65 6c 65 6d 65 6e 74 73 0a 2a  clist elements.*
340e5 2a 20 69 6e 74 6f 20 6f 75 74 20 69 6e 20 73 6f  * into out in so
340e6 72 74 65 64 20 6f 72 64 65 72 20 28 62 79 20 64  rted order (by d
340e7 6f 63 69 64 29 2c 20 64 72 6f 70 70 69 6e 67 20  ocid), dropping 
340e8 65 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 20 6f 6c  elements from ol
340e9 64 65 72 0a 2a 2a 20 72 65 61 64 65 72 73 20 77  der.** readers w
340ea 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 20 64  hen there is a d
340eb 75 70 6c 69 63 61 74 65 20 64 6f 63 69 64 2e 20  uplicate docid. 
340ec 20 70 52 65 61 64 65 72 73 20 69 73 20 61 73 73   pReaders is ass
340ed 75 6d 65 64 20 74 6f 20 62 65 0a 2a 2a 20 6f 72  umed to be.** or
340ee 64 65 72 65 64 20 62 79 20 61 67 65 2c 20 6f 6c  dered by age, ol
340ef 64 65 73 74 20 66 69 72 73 74 2e 0a 2a 2f 0a 2f  dest first..*/./
340f0 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 6e 52  * TODO(shess) nR
340f1 65 61 64 65 72 73 20 6d 75 73 74 20 62 65 20 3c  eaders must be <
340f2 3d 20 4d 45 52 47 45 5f 43 4f 55 4e 54 2e 20 20  = MERGE_COUNT.  
340f3 54 68 69 73 20 73 68 6f 75 6c 64 20 70 72 6f 62  This should prob
340f4 61 62 6c 79 0a 2a 2a 20 62 65 20 66 69 78 65 64  ably.** be fixed
340f5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
340f6 20 64 6f 63 4c 69 73 74 4d 65 72 67 65 28 44 61   docListMerge(Da
340f7 74 61 42 75 66 66 65 72 20 2a 6f 75 74 2c 0a 20  taBuffer *out,. 
340f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
340f9 20 20 20 20 20 20 20 20 44 4c 52 65 61 64 65 72          DLReader
340fa 20 2a 70 52 65 61 64 65 72 73 2c 20 69 6e 74 20   *pReaders, int 
340fb 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 4f 72 64  nReaders){.  Ord
340fc 65 72 65 64 44 4c 52 65 61 64 65 72 20 72 65 61  eredDLReader rea
340fd 64 65 72 73 5b 4d 45 52 47 45 5f 43 4f 55 4e 54  ders[MERGE_COUNT
340fe 5d 3b 0a 20 20 44 4c 57 72 69 74 65 72 20 77 72  ];.  DLWriter wr
340ff 69 74 65 72 3b 0a 20 20 69 6e 74 20 69 2c 20 6e  iter;.  int i, n
34100 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
34101 70 53 74 61 72 74 20 3d 20 30 3b 0a 20 20 69 6e  pStart = 0;.  in
34102 74 20 6e 53 74 61 72 74 20 3d 20 30 3b 0a 20 20  t nStart = 0;.  
34103 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 46 69  sqlite_int64 iFi
34104 72 73 74 44 6f 63 69 64 20 3d 20 30 2c 20 69 4c  rstDocid = 0, iL
34105 61 73 74 44 6f 63 69 64 20 3d 20 30 3b 0a 0a 20  astDocid = 0;.. 
34106 20 61 73 73 65 72 74 28 20 6e 52 65 61 64 65 72   assert( nReader
34107 73 3e 30 20 29 3b 0a 20 20 69 66 28 20 6e 52 65  s>0 );.  if( nRe
34108 61 64 65 72 73 3d 3d 31 20 29 7b 0a 20 20 20 20  aders==1 ){.    
34109 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
3410a 28 6f 75 74 2c 20 64 6c 72 44 6f 63 44 61 74 61  (out, dlrDocData
3410b 28 70 52 65 61 64 65 72 73 29 2c 20 64 6c 72 41  (pReaders), dlrA
3410c 6c 6c 44 61 74 61 42 79 74 65 73 28 70 52 65 61  llDataBytes(pRea
3410d 64 65 72 73 29 29 3b 0a 20 20 20 20 72 65 74 75  ders));.    retu
3410e 72 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  rn;.  }..  asser
3410f 74 28 20 6e 52 65 61 64 65 72 73 3c 3d 4d 45 52  t( nReaders<=MER
34110 47 45 5f 43 4f 55 4e 54 20 29 3b 0a 20 20 6e 20  GE_COUNT );.  n 
34111 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  = 0;.  for(i=0; 
34112 69 3c 6e 52 65 61 64 65 72 73 3b 20 69 2b 2b 29  i<nReaders; i++)
34113 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 52  {.    assert( pR
34114 65 61 64 65 72 73 5b 69 5d 2e 69 54 79 70 65 3d  eaders[i].iType=
34115 3d 70 52 65 61 64 65 72 73 5b 30 5d 2e 69 54 79  =pReaders[0].iTy
34116 70 65 20 29 3b 0a 20 20 20 20 72 65 61 64 65 72  pe );.    reader
34117 73 5b 69 5d 2e 70 52 65 61 64 65 72 20 3d 20 70  s[i].pReader = p
34118 52 65 61 64 65 72 73 2b 69 3b 0a 20 20 20 20 72  Readers+i;.    r
34119 65 61 64 65 72 73 5b 69 5d 2e 69 64 78 20 3d 20  eaders[i].idx = 
3411a 69 3b 0a 20 20 20 20 6e 20 2b 3d 20 64 6c 72 41  i;.    n += dlrA
3411b 6c 6c 44 61 74 61 42 79 74 65 73 28 26 70 52 65  llDataBytes(&pRe
3411c 61 64 65 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 20  aders[i]);.  }. 
3411d 20 2f 2a 20 43 6f 6e 73 65 72 76 61 74 69 76 65   /* Conservative
3411e 6c 79 20 73 69 7a 65 20 6f 75 74 70 75 74 20 74  ly size output t
3411f 6f 20 73 75 6d 20 6f 66 20 69 6e 70 75 74 73 2e  o sum of inputs.
34120 20 20 4f 75 74 70 75 74 20 73 68 6f 75 6c 64 20    Output should 
34121 65 6e 64 0a 20 20 2a 2a 20 75 70 20 73 74 72 69  end.  ** up stri
34122 63 74 6c 79 20 73 6d 61 6c 6c 65 72 20 74 68 61  ctly smaller tha
34123 6e 20 69 6e 70 75 74 2e 0a 20 20 2a 2f 0a 20 20  n input..  */.  
34124 64 61 74 61 42 75 66 66 65 72 45 78 70 61 6e 64  dataBufferExpand
34125 28 6f 75 74 2c 20 6e 29 3b 0a 0a 20 20 2f 2a 20  (out, n);..  /* 
34126 47 65 74 20 74 68 65 20 72 65 61 64 65 72 73 20  Get the readers 
34127 69 6e 74 6f 20 73 6f 72 74 65 64 20 6f 72 64 65  into sorted orde
34128 72 2e 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69  r. */.  while( i
34129 2d 2d 3e 30 20 29 7b 0a 20 20 20 20 6f 72 64 65  -->0 ){.    orde
3412a 72 65 64 44 4c 52 65 61 64 65 72 52 65 6f 72 64  redDLReaderReord
3412b 65 72 28 72 65 61 64 65 72 73 2b 69 2c 20 6e 52  er(readers+i, nR
3412c 65 61 64 65 72 73 2d 69 29 3b 0a 20 20 7d 0a 0a  eaders-i);.  }..
3412d 20 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65    dlwInit(&write
3412e 72 2c 20 70 52 65 61 64 65 72 73 5b 30 5d 2e 69  r, pReaders[0].i
3412f 54 79 70 65 2c 20 6f 75 74 29 3b 0a 20 20 77 68  Type, out);.  wh
34130 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28 72  ile( !dlrAtEnd(r
34131 65 61 64 65 72 73 5b 30 5d 2e 70 52 65 61 64 65  eaders[0].pReade
34132 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  r) ){.    sqlite
34133 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 20 3d 20  _int64 iDocid = 
34134 64 6c 72 44 6f 63 69 64 28 72 65 61 64 65 72 73  dlrDocid(readers
34135 5b 30 5d 2e 70 52 65 61 64 65 72 29 3b 0a 0a 20  [0].pReader);.. 
34136 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73     /* If this is
34137 20 61 20 63 6f 6e 74 69 6e 75 61 74 69 6f 6e 20   a continuation 
34138 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 62  of the current b
34139 75 66 66 65 72 20 74 6f 20 63 6f 70 79 2c 20 65  uffer to copy, e
3413a 78 74 65 6e 64 0a 20 20 20 20 2a 2a 20 74 68 61  xtend.    ** tha
3413b 74 20 62 75 66 66 65 72 2e 20 20 6d 65 6d 63 70  t buffer.  memcp
3413c 79 28 29 20 73 65 65 6d 73 20 74 6f 20 62 65 20  y() seems to be 
3413d 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 20 69  more efficient i
3413e 66 20 69 74 20 68 61 73 20 61 0a 20 20 20 20 2a  f it has a.    *
3413f 2a 20 6c 6f 74 73 20 6f 66 20 64 61 74 61 20 74  * lots of data t
34140 6f 20 63 6f 70 79 2e 0a 20 20 20 20 2a 2f 0a 20  o copy..    */. 
34141 20 20 20 69 66 28 20 64 6c 72 44 6f 63 44 61 74     if( dlrDocDat
34142 61 28 72 65 61 64 65 72 73 5b 30 5d 2e 70 52 65  a(readers[0].pRe
34143 61 64 65 72 29 3d 3d 70 53 74 61 72 74 2b 6e 53  ader)==pStart+nS
34144 74 61 72 74 20 29 7b 0a 20 20 20 20 20 20 6e 53  tart ){.      nS
34145 74 61 72 74 20 2b 3d 20 64 6c 72 44 6f 63 44 61  tart += dlrDocDa
34146 74 61 42 79 74 65 73 28 72 65 61 64 65 72 73 5b  taBytes(readers[
34147 30 5d 2e 70 52 65 61 64 65 72 29 3b 0a 20 20 20  0].pReader);.   
34148 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66   }else{.      if
34149 28 20 70 53 74 61 72 74 21 3d 30 20 29 7b 0a 20  ( pStart!=0 ){. 
3414a 20 20 20 20 20 20 20 64 6c 77 41 70 70 65 6e 64         dlwAppend
3414b 28 26 77 72 69 74 65 72 2c 20 70 53 74 61 72 74  (&writer, pStart
3414c 2c 20 6e 53 74 61 72 74 2c 20 69 46 69 72 73 74  , nStart, iFirst
3414d 44 6f 63 69 64 2c 20 69 4c 61 73 74 44 6f 63 69  Docid, iLastDoci
3414e 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  d);.      }.    
3414f 20 20 70 53 74 61 72 74 20 3d 20 64 6c 72 44 6f    pStart = dlrDo
34150 63 44 61 74 61 28 72 65 61 64 65 72 73 5b 30 5d  cData(readers[0]
34151 2e 70 52 65 61 64 65 72 29 3b 0a 20 20 20 20 20  .pReader);.     
34152 20 6e 53 74 61 72 74 20 3d 20 64 6c 72 44 6f 63   nStart = dlrDoc
34153 44 61 74 61 42 79 74 65 73 28 72 65 61 64 65 72  DataBytes(reader
34154 73 5b 30 5d 2e 70 52 65 61 64 65 72 29 3b 0a 20  s[0].pReader);. 
34155 20 20 20 20 20 69 46 69 72 73 74 44 6f 63 69 64       iFirstDocid
34156 20 3d 20 69 44 6f 63 69 64 3b 0a 20 20 20 20 7d   = iDocid;.    }
34157 0a 20 20 20 20 69 4c 61 73 74 44 6f 63 69 64 20  .    iLastDocid 
34158 3d 20 69 44 6f 63 69 64 3b 0a 20 20 20 20 64 6c  = iDocid;.    dl
34159 72 53 74 65 70 28 72 65 61 64 65 72 73 5b 30 5d  rStep(readers[0]
3415a 2e 70 52 65 61 64 65 72 29 3b 0a 0a 20 20 20 20  .pReader);..    
3415b 2f 2a 20 44 72 6f 70 20 61 6c 6c 20 6f 66 20 74  /* Drop all of t
3415c 68 65 20 6f 6c 64 65 72 20 65 6c 65 6d 65 6e 74  he older element
3415d 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20  s with the same 
3415e 64 6f 63 69 64 2e 20 2a 2f 0a 20 20 20 20 66 6f  docid. */.    fo
3415f 72 28 69 3d 31 3b 20 69 3c 6e 52 65 61 64 65 72  r(i=1; i<nReader
34160 73 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20  s &&.           
34161 20 20 21 64 6c 72 41 74 45 6e 64 28 72 65 61 64    !dlrAtEnd(read
34162 65 72 73 5b 69 5d 2e 70 52 65 61 64 65 72 29 20  ers[i].pReader) 
34163 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  &&.             
34164 64 6c 72 44 6f 63 69 64 28 72 65 61 64 65 72 73  dlrDocid(readers
34165 5b 69 5d 2e 70 52 65 61 64 65 72 29 3d 3d 69 44  [i].pReader)==iD
34166 6f 63 69 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ocid; i++){.    
34167 20 20 64 6c 72 53 74 65 70 28 72 65 61 64 65 72    dlrStep(reader
34168 73 5b 69 5d 2e 70 52 65 61 64 65 72 29 3b 0a 20  s[i].pReader);. 
34169 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 47 65 74     }..    /* Get
3416a 20 74 68 65 20 72 65 61 64 65 72 73 20 62 61 63   the readers bac
3416b 6b 20 69 6e 74 6f 20 6f 72 64 65 72 2e 20 2a 2f  k into order. */
3416c 0a 20 20 20 20 77 68 69 6c 65 28 20 69 2d 2d 3e  .    while( i-->
3416d 30 20 29 7b 0a 20 20 20 20 20 20 6f 72 64 65 72  0 ){.      order
3416e 65 64 44 4c 52 65 61 64 65 72 52 65 6f 72 64 65  edDLReaderReorde
3416f 72 28 72 65 61 64 65 72 73 2b 69 2c 20 6e 52 65  r(readers+i, nRe
34170 61 64 65 72 73 2d 69 29 3b 0a 20 20 20 20 7d 0a  aders-i);.    }.
34171 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 6f    }..  /* Copy o
34172 76 65 72 20 61 6e 79 20 72 65 6d 61 69 6e 69 6e  ver any remainin
34173 67 20 65 6c 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20  g elements. */. 
34174 20 69 66 28 20 6e 53 74 61 72 74 3e 30 20 29 20   if( nStart>0 ) 
34175 64 6c 77 41 70 70 65 6e 64 28 26 77 72 69 74 65  dlwAppend(&write
34176 72 2c 20 70 53 74 61 72 74 2c 20 6e 53 74 61 72  r, pStart, nStar
34177 74 2c 20 69 46 69 72 73 74 44 6f 63 69 64 2c 20  t, iFirstDocid, 
34178 69 4c 61 73 74 44 6f 63 69 64 29 3b 0a 20 20 64  iLastDocid);.  d
34179 6c 77 44 65 73 74 72 6f 79 28 26 77 72 69 74 65  lwDestroy(&write
3417a 72 29 3b 0a 7d 0a 0a 2f 2a 20 48 65 6c 70 65 72  r);.}../* Helper
3417b 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 70 6f   function for po
3417c 73 4c 69 73 74 55 6e 69 6f 6e 28 29 2e 20 20 43  sListUnion().  C
3417d 6f 6d 70 61 72 65 73 20 74 68 65 20 63 75 72 72  ompares the curr
3417e 65 6e 74 20 70 6f 73 69 74 69 6f 6e 0a 2a 2a 20  ent position.** 
3417f 62 65 74 77 65 65 6e 20 6c 65 66 74 20 61 6e 64  between left and
34180 20 72 69 67 68 74 2c 20 72 65 74 75 72 6e 69 6e   right, returnin
34181 67 20 61 73 20 73 74 61 6e 64 61 72 64 20 43 20  g as standard C 
34182 69 64 69 6f 6d 20 6f 66 20 3c 30 20 69 66 0a 2a  idiom of <0 if.*
34183 2a 20 6c 65 66 74 3c 72 69 67 68 74 2c 20 3e 30  * left<right, >0
34184 20 69 66 20 6c 65 66 74 3e 72 69 67 68 74 2c 20   if left>right, 
34185 61 6e 64 20 30 20 69 66 20 6c 65 66 74 3d 3d 72  and 0 if left==r
34186 69 67 68 74 2e 20 20 22 45 6e 64 22 20 61 6c 77  ight.  "End" alw
34187 61 79 73 0a 2a 2a 20 63 6f 6d 70 61 72 65 73 20  ays.** compares 
34188 67 72 65 61 74 65 72 2e 0a 2a 2f 0a 73 74 61 74  greater..*/.stat
34189 69 63 20 69 6e 74 20 70 6f 73 4c 69 73 74 43 6d  ic int posListCm
3418a 70 28 50 4c 52 65 61 64 65 72 20 2a 70 4c 65 66  p(PLReader *pLef
3418b 74 2c 20 50 4c 52 65 61 64 65 72 20 2a 70 52 69  t, PLReader *pRi
3418c 67 68 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20  ght){.  assert( 
3418d 70 4c 65 66 74 2d 3e 69 54 79 70 65 3d 3d 70 52  pLeft->iType==pR
3418e 69 67 68 74 2d 3e 69 54 79 70 65 20 29 3b 0a 20  ight->iType );. 
3418f 20 69 66 28 20 70 4c 65 66 74 2d 3e 69 54 79 70   if( pLeft->iTyp
34190 65 3d 3d 44 4c 5f 44 4f 43 49 44 53 20 29 20 72  e==DL_DOCIDS ) r
34191 65 74 75 72 6e 20 30 3b 0a 0a 20 20 69 66 28 20  eturn 0;..  if( 
34192 70 6c 72 41 74 45 6e 64 28 70 4c 65 66 74 29 20  plrAtEnd(pLeft) 
34193 29 20 72 65 74 75 72 6e 20 70 6c 72 41 74 45 6e  ) return plrAtEn
34194 64 28 70 52 69 67 68 74 29 20 3f 20 30 20 3a 20  d(pRight) ? 0 : 
34195 31 3b 0a 20 20 69 66 28 20 70 6c 72 41 74 45 6e  1;.  if( plrAtEn
34196 64 28 70 52 69 67 68 74 29 20 29 20 72 65 74 75  d(pRight) ) retu
34197 72 6e 20 2d 31 3b 0a 0a 20 20 69 66 28 20 70 6c  rn -1;..  if( pl
34198 72 43 6f 6c 75 6d 6e 28 70 4c 65 66 74 29 3c 70  rColumn(pLeft)<p
34199 6c 72 43 6f 6c 75 6d 6e 28 70 52 69 67 68 74 29  lrColumn(pRight)
3419a 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20   ) return -1;.  
3419b 69 66 28 20 70 6c 72 43 6f 6c 75 6d 6e 28 70 4c  if( plrColumn(pL
3419c 65 66 74 29 3e 70 6c 72 43 6f 6c 75 6d 6e 28 70  eft)>plrColumn(p
3419d 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20  Right) ) return 
3419e 31 3b 0a 0a 20 20 69 66 28 20 70 6c 72 50 6f 73  1;..  if( plrPos
3419f 69 74 69 6f 6e 28 70 4c 65 66 74 29 3c 70 6c 72  ition(pLeft)<plr
341a0 50 6f 73 69 74 69 6f 6e 28 70 52 69 67 68 74 29  Position(pRight)
341a1 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20   ) return -1;.  
341a2 69 66 28 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28  if( plrPosition(
341a3 70 4c 65 66 74 29 3e 70 6c 72 50 6f 73 69 74 69  pLeft)>plrPositi
341a4 6f 6e 28 70 52 69 67 68 74 29 20 29 20 72 65 74  on(pRight) ) ret
341a5 75 72 6e 20 31 3b 0a 20 20 69 66 28 20 70 4c 65  urn 1;.  if( pLe
341a6 66 74 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 50 4f  ft->iType==DL_PO
341a7 53 49 54 49 4f 4e 53 20 29 20 72 65 74 75 72 6e  SITIONS ) return
341a8 20 30 3b 0a 0a 20 20 69 66 28 20 70 6c 72 53 74   0;..  if( plrSt
341a9 61 72 74 4f 66 66 73 65 74 28 70 4c 65 66 74 29  artOffset(pLeft)
341aa 3c 70 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28  <plrStartOffset(
341ab 70 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e  pRight) ) return
341ac 20 2d 31 3b 0a 20 20 69 66 28 20 70 6c 72 53 74   -1;.  if( plrSt
341ad 61 72 74 4f 66 66 73 65 74 28 70 4c 65 66 74 29  artOffset(pLeft)
341ae 3e 70 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28  >plrStartOffset(
341af 70 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e  pRight) ) return
341b0 20 31 3b 0a 0a 20 20 69 66 28 20 70 6c 72 45 6e   1;..  if( plrEn
341b1 64 4f 66 66 73 65 74 28 70 4c 65 66 74 29 3c 70  dOffset(pLeft)<p
341b2 6c 72 45 6e 64 4f 66 66 73 65 74 28 70 52 69 67  lrEndOffset(pRig
341b3 68 74 29 20 29 20 72 65 74 75 72 6e 20 2d 31 3b  ht) ) return -1;
341b4 0a 20 20 69 66 28 20 70 6c 72 45 6e 64 4f 66 66  .  if( plrEndOff
341b5 73 65 74 28 70 4c 65 66 74 29 3e 70 6c 72 45 6e  set(pLeft)>plrEn
341b6 64 4f 66 66 73 65 74 28 70 52 69 67 68 74 29 20  dOffset(pRight) 
341b7 29 20 72 65 74 75 72 6e 20 31 3b 0a 0a 20 20 72  ) return 1;..  r
341b8 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 57  eturn 0;.}../* W
341b9 72 69 74 65 20 74 68 65 20 75 6e 69 6f 6e 20 6f  rite the union o
341ba 66 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 73  f position lists
341bb 20 69 6e 20 70 4c 65 66 74 20 61 6e 64 20 70 52   in pLeft and pR
341bc 69 67 68 74 20 74 6f 20 70 4f 75 74 2e 0a 2a 2a  ight to pOut..**
341bd 20 22 55 6e 69 6f 6e 22 20 69 6e 20 74 68 69 73   "Union" in this
341be 20 63 61 73 65 20 6d 65 61 6e 69 6e 67 20 22 41   case meaning "A
341bf 6c 6c 20 75 6e 69 71 75 65 20 70 6f 73 69 74 69  ll unique positi
341c0 6f 6e 20 74 75 70 6c 65 73 22 2e 20 20 53 68 6f  on tuples".  Sho
341c1 75 6c 64 0a 2a 2a 20 77 6f 72 6b 20 77 69 74 68  uld.** work with
341c2 20 61 6e 79 20 64 6f 63 6c 69 73 74 20 74 79 70   any doclist typ
341c3 65 2c 20 74 68 6f 75 67 68 20 62 6f 74 68 20 69  e, though both i
341c4 6e 70 75 74 73 20 61 6e 64 20 74 68 65 20 6f 75  nputs and the ou
341c5 74 70 75 74 0a 2a 2a 20 73 68 6f 75 6c 64 20 62  tput.** should b
341c6 65 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 2e  e the same type.
341c7 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
341c8 70 6f 73 4c 69 73 74 55 6e 69 6f 6e 28 44 4c 52  posListUnion(DLR
341c9 65 61 64 65 72 20 2a 70 4c 65 66 74 2c 20 44 4c  eader *pLeft, DL
341ca 52 65 61 64 65 72 20 2a 70 52 69 67 68 74 2c 20  Reader *pRight, 
341cb 44 4c 57 72 69 74 65 72 20 2a 70 4f 75 74 29 7b  DLWriter *pOut){
341cc 0a 20 20 50 4c 52 65 61 64 65 72 20 6c 65 66 74  .  PLReader left
341cd 2c 20 72 69 67 68 74 3b 0a 20 20 50 4c 57 72 69  , right;.  PLWri
341ce 74 65 72 20 77 72 69 74 65 72 3b 0a 0a 20 20 61  ter writer;..  a
341cf 73 73 65 72 74 28 20 64 6c 72 44 6f 63 69 64 28  ssert( dlrDocid(
341d0 70 4c 65 66 74 29 3d 3d 64 6c 72 44 6f 63 69 64  pLeft)==dlrDocid
341d1 28 70 52 69 67 68 74 29 20 29 3b 0a 20 20 61 73  (pRight) );.  as
341d2 73 65 72 74 28 20 70 4c 65 66 74 2d 3e 69 54 79  sert( pLeft->iTy
341d3 70 65 3d 3d 70 52 69 67 68 74 2d 3e 69 54 79 70  pe==pRight->iTyp
341d4 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  e );.  assert( p
341d5 4c 65 66 74 2d 3e 69 54 79 70 65 3d 3d 70 4f 75  Left->iType==pOu
341d6 74 2d 3e 69 54 79 70 65 20 29 3b 0a 0a 20 20 70  t->iType );..  p
341d7 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c 20 70 4c  lrInit(&left, pL
341d8 65 66 74 29 3b 0a 20 20 70 6c 72 49 6e 69 74 28  eft);.  plrInit(
341d9 26 72 69 67 68 74 2c 20 70 52 69 67 68 74 29 3b  &right, pRight);
341da 0a 20 20 70 6c 77 49 6e 69 74 28 26 77 72 69 74  .  plwInit(&writ
341db 65 72 2c 20 70 4f 75 74 2c 20 64 6c 72 44 6f 63  er, pOut, dlrDoc
341dc 69 64 28 70 4c 65 66 74 29 29 3b 0a 0a 20 20 77  id(pLeft));..  w
341dd 68 69 6c 65 28 20 21 70 6c 72 41 74 45 6e 64 28  hile( !plrAtEnd(
341de 26 6c 65 66 74 29 20 7c 7c 20 21 70 6c 72 41 74  &left) || !plrAt
341df 45 6e 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20  End(&right) ){. 
341e0 20 20 20 69 6e 74 20 63 20 3d 20 70 6f 73 4c 69     int c = posLi
341e1 73 74 43 6d 70 28 26 6c 65 66 74 2c 20 26 72 69  stCmp(&left, &ri
341e2 67 68 74 29 3b 0a 20 20 20 20 69 66 28 20 63 3c  ght);.    if( c<
341e3 30 20 29 7b 0a 20 20 20 20 20 20 70 6c 77 43 6f  0 ){.      plwCo
341e4 70 79 28 26 77 72 69 74 65 72 2c 20 26 6c 65 66  py(&writer, &lef
341e5 74 29 3b 0a 20 20 20 20 20 20 70 6c 72 53 74 65  t);.      plrSte
341e6 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65  p(&left);.    }e
341e7 6c 73 65 20 69 66 28 20 63 3e 30 20 29 7b 0a 20  lse if( c>0 ){. 
341e8 20 20 20 20 20 70 6c 77 43 6f 70 79 28 26 77 72       plwCopy(&wr
341e9 69 74 65 72 2c 20 26 72 69 67 68 74 29 3b 0a 20  iter, &right);. 
341ea 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 72 69       plrStep(&ri
341eb 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ght);.    }else{
341ec 0a 20 20 20 20 20 20 70 6c 77 43 6f 70 79 28 26  .      plwCopy(&
341ed 77 72 69 74 65 72 2c 20 26 6c 65 66 74 29 3b 0a  writer, &left);.
341ee 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 6c        plrStep(&l
341ef 65 66 74 29 3b 0a 20 20 20 20 20 20 70 6c 72 53  eft);.      plrS
341f0 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20  tep(&right);.   
341f1 20 7d 0a 20 20 7d 0a 0a 20 20 70 6c 77 54 65 72   }.  }..  plwTer
341f2 6d 69 6e 61 74 65 28 26 77 72 69 74 65 72 29 3b  minate(&writer);
341f3 0a 20 20 70 6c 77 44 65 73 74 72 6f 79 28 26 77  .  plwDestroy(&w
341f4 72 69 74 65 72 29 3b 0a 20 20 70 6c 72 44 65 73  riter);.  plrDes
341f5 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20 70  troy(&left);.  p
341f6 6c 72 44 65 73 74 72 6f 79 28 26 72 69 67 68 74  lrDestroy(&right
341f7 29 3b 0a 7d 0a 0a 2f 2a 20 57 72 69 74 65 20 74  );.}../* Write t
341f8 68 65 20 75 6e 69 6f 6e 20 6f 66 20 64 6f 63 6c  he union of docl
341f9 69 73 74 73 20 69 6e 20 70 4c 65 66 74 20 61 6e  ists in pLeft an
341fa 64 20 70 52 69 67 68 74 20 74 6f 20 70 4f 75 74  d pRight to pOut
341fb 2e 20 20 46 6f 72 0a 2a 2a 20 64 6f 63 69 64 73  .  For.** docids
341fc 20 69 6e 20 63 6f 6d 6d 6f 6e 20 62 65 74 77 65   in common betwe
341fd 65 6e 20 74 68 65 20 69 6e 70 75 74 73 2c 20 74  en the inputs, t
341fe 68 65 20 75 6e 69 6f 6e 20 6f 66 20 74 68 65 20  he union of the 
341ff 70 6f 73 69 74 69 6f 6e 0a 2a 2a 20 6c 69 73 74  position.** list
34200 73 20 69 73 20 77 72 69 74 74 65 6e 2e 20 20 49  s is written.  I
34201 6e 70 75 74 73 20 61 6e 64 20 6f 75 74 70 75 74  nputs and output
34202 73 20 61 72 65 20 61 6c 77 61 79 73 20 74 79 70  s are always typ
34203 65 20 44 4c 5f 44 45 46 41 55 4c 54 2e 0a 2a 2f  e DL_DEFAULT..*/
34204 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63  .static void doc
34205 4c 69 73 74 55 6e 69 6f 6e 28 0a 20 20 63 6f 6e  ListUnion(.  con
34206 73 74 20 63 68 61 72 20 2a 70 4c 65 66 74 2c 20  st char *pLeft, 
34207 69 6e 74 20 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e  int nLeft,.  con
34208 73 74 20 63 68 61 72 20 2a 70 52 69 67 68 74 2c  st char *pRight,
34209 20 69 6e 74 20 6e 52 69 67 68 74 2c 0a 20 20 44   int nRight,.  D
3420a 61 74 61 42 75 66 66 65 72 20 2a 70 4f 75 74 20  ataBuffer *pOut 
3420b 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68       /* Write th
3420c 65 20 63 6f 6d 62 69 6e 65 64 20 64 6f 63 6c 69  e combined docli
3420d 73 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  st here */.){.  
3420e 44 4c 52 65 61 64 65 72 20 6c 65 66 74 2c 20 72  DLReader left, r
3420f 69 67 68 74 3b 0a 20 20 44 4c 57 72 69 74 65 72  ight;.  DLWriter
34210 20 77 72 69 74 65 72 3b 0a 0a 20 20 69 66 28 20   writer;..  if( 
34211 6e 4c 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20  nLeft==0 ){.    
34212 69 66 28 20 6e 52 69 67 68 74 21 3d 30 29 20 64  if( nRight!=0) d
34213 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28  ataBufferAppend(
34214 70 4f 75 74 2c 20 70 52 69 67 68 74 2c 20 6e 52  pOut, pRight, nR
34215 69 67 68 74 29 3b 0a 20 20 20 20 72 65 74 75 72  ight);.    retur
34216 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 52 69  n;.  }.  if( nRi
34217 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64 61  ght==0 ){.    da
34218 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 70  taBufferAppend(p
34219 4f 75 74 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66  Out, pLeft, nLef
3421a 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  t);.    return;.
3421b 20 20 7d 0a 0a 20 20 64 6c 72 49 6e 69 74 28 26    }..  dlrInit(&
3421c 6c 65 66 74 2c 20 44 4c 5f 44 45 46 41 55 4c 54  left, DL_DEFAULT
3421d 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b  , pLeft, nLeft);
3421e 0a 20 20 64 6c 72 49 6e 69 74 28 26 72 69 67 68  .  dlrInit(&righ
3421f 74 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70  t, DL_DEFAULT, p
34220 52 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a  Right, nRight);.
34221 20 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65    dlwInit(&write
34222 72 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70  r, DL_DEFAULT, p
34223 4f 75 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20  Out);..  while( 
34224 21 64 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29  !dlrAtEnd(&left)
34225 20 7c 7c 20 21 64 6c 72 41 74 45 6e 64 28 26 72   || !dlrAtEnd(&r
34226 69 67 68 74 29 20 29 7b 0a 20 20 20 20 69 66 28  ight) ){.    if(
34227 20 64 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74   dlrAtEnd(&right
34228 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 77 43 6f  ) ){.      dlwCo
34229 70 79 28 26 77 72 69 74 65 72 2c 20 26 6c 65 66  py(&writer, &lef
3422a 74 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65  t);.      dlrSte
3422b 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65  p(&left);.    }e
3422c 6c 73 65 20 69 66 28 20 64 6c 72 41 74 45 6e 64  lse if( dlrAtEnd
3422d 28 26 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20  (&left) ){.     
3422e 20 64 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72   dlwCopy(&writer
3422f 2c 20 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20  , &right);.     
34230 20 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29   dlrStep(&right)
34231 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
34232 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29 3c  dlrDocid(&left)<
34233 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74 29  dlrDocid(&right)
34234 20 29 7b 0a 20 20 20 20 20 20 64 6c 77 43 6f 70   ){.      dlwCop
34235 79 28 26 77 72 69 74 65 72 2c 20 26 6c 65 66 74  y(&writer, &left
34236 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70  );.      dlrStep
34237 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c  (&left);.    }el
34238 73 65 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28  se if( dlrDocid(
34239 26 6c 65 66 74 29 3e 64 6c 72 44 6f 63 69 64 28  &left)>dlrDocid(
3423a 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20  &right) ){.     
3423b 20 64 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72   dlwCopy(&writer
3423c 2c 20 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20  , &right);.     
3423d 20 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29   dlrStep(&right)
3423e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
3423f 20 20 20 70 6f 73 4c 69 73 74 55 6e 69 6f 6e 28     posListUnion(
34240 26 6c 65 66 74 2c 20 26 72 69 67 68 74 2c 20 26  &left, &right, &
34241 77 72 69 74 65 72 29 3b 0a 20 20 20 20 20 20 64  writer);.      d
34242 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20  lrStep(&left);. 
34243 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 72 69       dlrStep(&ri
34244 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ght);.    }.  }.
34245 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 6c  .  dlrDestroy(&l
34246 65 66 74 29 3b 0a 20 20 64 6c 72 44 65 73 74 72  eft);.  dlrDestr
34247 6f 79 28 26 72 69 67 68 74 29 3b 0a 20 20 64 6c  oy(&right);.  dl
34248 77 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72  wDestroy(&writer
34249 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69  );.}../* .** Thi
3424a 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
3424b 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68  ed as part of th
3424c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
3424d 20 6f 66 20 70 68 72 61 73 65 20 61 6e 64 0a 2a   of phrase and.*
3424e 2a 20 4e 45 41 52 20 6d 61 74 63 68 69 6e 67 2e  * NEAR matching.
3424f 0a 2a 2a 0a 2a 2a 20 70 4c 65 66 74 20 61 6e 64  .**.** pLeft and
34250 20 70 52 69 67 68 74 20 61 72 65 20 44 4c 52 65   pRight are DLRe
34251 61 64 65 72 73 20 70 6f 73 69 74 69 6f 6e 65 64  aders positioned
34252 20 74 6f 20 74 68 65 20 73 61 6d 65 20 64 6f 63   to the same doc
34253 69 64 20 69 6e 0a 2a 2a 20 6c 69 73 74 73 20 6f  id in.** lists o
34254 66 20 74 79 70 65 20 44 4c 5f 50 4f 53 49 54 49  f type DL_POSITI
34255 4f 4e 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ON. This functio
34256 6e 20 77 72 69 74 65 73 20 61 6e 20 65 6e 74 72  n writes an entr
34257 79 20 74 6f 20 74 68 65 0a 2a 2a 20 44 4c 57 72  y to the.** DLWr
34258 69 74 65 72 20 70 4f 75 74 20 66 6f 72 20 65 61  iter pOut for ea
34259 63 68 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 70  ch position in p
3425a 52 69 67 68 74 20 74 68 61 74 20 69 73 20 6c 65  Right that is le
3425b 73 73 20 74 68 61 6e 0a 2a 2a 20 28 6e 4e 65 61  ss than.** (nNea
3425c 72 2b 31 29 20 67 72 65 61 74 65 72 20 28 62 75  r+1) greater (bu
3425d 74 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 6f  t not equal to o
3425e 72 20 73 6d 61 6c 6c 65 72 29 20 74 68 61 6e 20  r smaller) than 
3425f 61 20 70 6f 73 69 74 69 6f 6e 20 0a 2a 2a 20 69  a position .** i
34260 6e 20 70 4c 65 66 74 2e 20 46 6f 72 20 65 78 61  n pLeft. For exa
34261 6d 70 6c 65 2c 20 69 66 20 6e 4e 65 61 72 20 69  mple, if nNear i
34262 73 20 30 2c 20 61 6e 64 20 74 68 65 20 70 6f 73  s 0, and the pos
34263 69 74 69 6f 6e 73 20 63 6f 6e 74 61 69 6e 65 64  itions contained
34264 0a 2a 2a 20 62 79 20 70 4c 65 66 74 20 61 6e 64  .** by pLeft and
34265 20 70 52 69 67 68 74 20 61 72 65 3a 0a 2a 2a 0a   pRight are:.**.
34266 2a 2a 20 20 20 20 70 4c 65 66 74 3a 20 20 35 20  **    pLeft:  5 
34267 31 30 20 31 35 20 32 30 0a 2a 2a 20 20 20 20 70  10 15 20.**    p
34268 52 69 67 68 74 3a 20 36 20 20 39 20 31 37 20 32  Right: 6  9 17 2
34269 31 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 74 68 65  1.**.** then the
3426a 20 64 6f 63 69 64 20 69 73 20 61 64 64 65 64 20   docid is added 
3426b 74 6f 20 70 4f 75 74 2e 20 49 66 20 70 4f 75 74  to pOut. If pOut
3426c 20 69 73 20 6f 66 20 74 79 70 65 20 44 4c 5f 50   is of type DL_P
3426d 4f 53 49 54 49 4f 4e 53 2c 0a 2a 2a 20 74 68 65  OSITIONS,.** the
3426e 6e 20 61 20 70 6f 73 69 74 69 6f 6e 69 64 73 20  n a positionids 
3426f 22 36 22 20 61 6e 64 20 22 32 31 22 20 61 72 65  "6" and "21" are
34270 20 61 6c 73 6f 20 61 64 64 65 64 20 74 6f 20 70   also added to p
34271 4f 75 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f  Out..**.** If bo
34272 6f 6c 65 61 6e 20 61 72 67 75 6d 65 6e 74 20 69  olean argument i
34273 73 53 61 76 65 4c 65 66 74 20 69 73 20 74 72 75  sSaveLeft is tru
34274 65 2c 20 74 68 65 6e 20 70 6f 73 69 74 69 6f 6e  e, then position
34275 69 64 73 20 61 72 65 20 63 6f 70 69 65 64 0a 2a  ids are copied.*
34276 2a 20 66 72 6f 6d 20 70 4c 65 66 74 20 69 6e 73  * from pLeft ins
34277 74 65 61 64 20 6f 66 20 70 52 69 67 68 74 2e 20  tead of pRight. 
34278 49 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 20 61  In the example a
34279 62 6f 76 65 2c 20 74 68 65 20 70 6f 73 69 74 69  bove, the positi
3427a 6f 6e 73 20 22 35 22 0a 2a 2a 20 61 6e 64 20 22  ons "5".** and "
3427b 32 30 22 20 77 6f 75 6c 64 20 62 65 20 61 64 64  20" would be add
3427c 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 22 36  ed instead of "6
3427d 22 20 61 6e 64 20 22 32 31 22 2e 0a 2a 2f 0a 73  " and "21"..*/.s
3427e 74 61 74 69 63 20 76 6f 69 64 20 70 6f 73 4c 69  tatic void posLi
3427f 73 74 50 68 72 61 73 65 4d 65 72 67 65 28 0a 20  stPhraseMerge(. 
34280 20 44 4c 52 65 61 64 65 72 20 2a 70 4c 65 66 74   DLReader *pLeft
34281 2c 20 0a 20 20 44 4c 52 65 61 64 65 72 20 2a 70  , .  DLReader *p
34282 52 69 67 68 74 2c 0a 20 20 69 6e 74 20 6e 4e 65  Right,.  int nNe
34283 61 72 2c 0a 20 20 69 6e 74 20 69 73 53 61 76 65  ar,.  int isSave
34284 4c 65 66 74 2c 0a 20 20 44 4c 57 72 69 74 65 72  Left,.  DLWriter
34285 20 2a 70 4f 75 74 0a 29 7b 0a 20 20 50 4c 52 65   *pOut.){.  PLRe
34286 61 64 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74  ader left, right
34287 3b 0a 20 20 50 4c 57 72 69 74 65 72 20 77 72 69  ;.  PLWriter wri
34288 74 65 72 3b 0a 20 20 69 6e 74 20 6d 61 74 63 68  ter;.  int match
34289 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28   = 0;..  assert(
3428a 20 64 6c 72 44 6f 63 69 64 28 70 4c 65 66 74 29   dlrDocid(pLeft)
3428b 3d 3d 64 6c 72 44 6f 63 69 64 28 70 52 69 67 68  ==dlrDocid(pRigh
3428c 74 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  t) );.  assert( 
3428d 70 4f 75 74 2d 3e 69 54 79 70 65 21 3d 44 4c 5f  pOut->iType!=DL_
3428e 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54  POSITIONS_OFFSET
3428f 53 20 29 3b 0a 0a 20 20 70 6c 72 49 6e 69 74 28  S );..  plrInit(
34290 26 6c 65 66 74 2c 20 70 4c 65 66 74 29 3b 0a 20  &left, pLeft);. 
34291 20 70 6c 72 49 6e 69 74 28 26 72 69 67 68 74 2c   plrInit(&right,
34292 20 70 52 69 67 68 74 29 3b 0a 0a 20 20 77 68 69   pRight);..  whi
34293 6c 65 28 20 21 70 6c 72 41 74 45 6e 64 28 26 6c  le( !plrAtEnd(&l
34294 65 66 74 29 20 26 26 20 21 70 6c 72 41 74 45 6e  eft) && !plrAtEn
34295 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20  d(&right) ){.   
34296 20 69 66 28 20 70 6c 72 43 6f 6c 75 6d 6e 28 26   if( plrColumn(&
34297 6c 65 66 74 29 3c 70 6c 72 43 6f 6c 75 6d 6e 28  left)<plrColumn(
34298 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20  &right) ){.     
34299 20 70 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b   plrStep(&left);
3429a 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70  .    }else if( p
3429b 6c 72 43 6f 6c 75 6d 6e 28 26 6c 65 66 74 29 3e  lrColumn(&left)>
3429c 70 6c 72 43 6f 6c 75 6d 6e 28 26 72 69 67 68 74  plrColumn(&right
3429d 29 20 29 7b 0a 20 20 20 20 20 20 70 6c 72 53 74  ) ){.      plrSt
3429e 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20  ep(&right);.    
3429f 7d 65 6c 73 65 20 69 66 28 20 70 6c 72 50 6f 73  }else if( plrPos
342a0 69 74 69 6f 6e 28 26 6c 65 66 74 29 3e 3d 70 6c  ition(&left)>=pl
342a1 72 50 6f 73 69 74 69 6f 6e 28 26 72 69 67 68 74  rPosition(&right
342a2 29 20 29 7b 0a 20 20 20 20 20 20 70 6c 72 53 74  ) ){.      plrSt
342a3 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20  ep(&right);.    
342a4 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28  }else{.      if(
342a5 20 28 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26 72   (plrPosition(&r
342a6 69 67 68 74 29 2d 70 6c 72 50 6f 73 69 74 69 6f  ight)-plrPositio
342a7 6e 28 26 6c 65 66 74 29 29 3c 3d 28 6e 4e 65 61  n(&left))<=(nNea
342a8 72 2b 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20  r+1) ){.        
342a9 69 66 28 20 21 6d 61 74 63 68 20 29 7b 0a 20 20  if( !match ){.  
342aa 20 20 20 20 20 20 20 20 70 6c 77 49 6e 69 74 28          plwInit(
342ab 26 77 72 69 74 65 72 2c 20 70 4f 75 74 2c 20 64  &writer, pOut, d
342ac 6c 72 44 6f 63 69 64 28 70 4c 65 66 74 29 29 3b  lrDocid(pLeft));
342ad 0a 20 20 20 20 20 20 20 20 20 20 6d 61 74 63 68  .          match
342ae 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 1;.        }.
342af 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73 53          if( !isS
342b0 61 76 65 4c 65 66 74 20 29 7b 0a 20 20 20 20 20  aveLeft ){.     
342b1 20 20 20 20 20 70 6c 77 41 64 64 28 26 77 72 69       plwAdd(&wri
342b2 74 65 72 2c 20 70 6c 72 43 6f 6c 75 6d 6e 28 26  ter, plrColumn(&
342b3 72 69 67 68 74 29 2c 20 70 6c 72 50 6f 73 69 74  right), plrPosit
342b4 69 6f 6e 28 26 72 69 67 68 74 29 2c 20 30 2c 20  ion(&right), 0, 
342b5 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  0);.        }els
342b6 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77  e{.          plw
342b7 41 64 64 28 26 77 72 69 74 65 72 2c 20 70 6c 72  Add(&writer, plr
342b8 43 6f 6c 75 6d 6e 28 26 6c 65 66 74 29 2c 20 70  Column(&left), p
342b9 6c 72 50 6f 73 69 74 69 6f 6e 28 26 6c 65 66 74  lrPosition(&left
342ba 29 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  ), 0, 0);.      
342bb 20 20 7d 0a 20 20 20 20 20 20 20 20 70 6c 72 53    }.        plrS
342bc 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20  tep(&right);.   
342bd 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
342be 20 20 70 6c 72 53 74 65 70 28 26 6c 65 66 74 29    plrStep(&left)
342bf 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
342c0 20 20 7d 0a 0a 20 20 69 66 28 20 6d 61 74 63 68    }..  if( match
342c1 20 29 7b 0a 20 20 20 20 70 6c 77 54 65 72 6d 69   ){.    plwTermi
342c2 6e 61 74 65 28 26 77 72 69 74 65 72 29 3b 0a 20  nate(&writer);. 
342c3 20 20 20 70 6c 77 44 65 73 74 72 6f 79 28 26 77     plwDestroy(&w
342c4 72 69 74 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 70  riter);.  }..  p
342c5 6c 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29  lrDestroy(&left)
342c6 3b 0a 20 20 70 6c 72 44 65 73 74 72 6f 79 28 26  ;.  plrDestroy(&
342c7 72 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  right);.}../*.**
342c8 20 43 6f 6d 70 61 72 65 20 74 68 65 20 76 61 6c   Compare the val
342c9 75 65 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  ues pointed to b
342ca 79 20 74 68 65 20 50 4c 52 65 61 64 65 72 73 20  y the PLReaders 
342cb 70 61 73 73 65 64 20 61 73 20 61 72 67 75 6d 65  passed as argume
342cc 6e 74 73 2e 20 0a 2a 2a 20 52 65 74 75 72 6e 20  nts. .** Return 
342cd 2d 31 20 69 66 20 74 68 65 20 76 61 6c 75 65 20  -1 if the value 
342ce 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 4c  pointed to by pL
342cf 65 66 74 20 69 73 20 63 6f 6e 73 69 64 65 72 65  eft is considere
342d0 64 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 74  d less than.** t
342d1 68 65 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 64  he value pointed
342d2 20 74 6f 20 62 79 20 70 52 69 67 68 74 2c 20 2b   to by pRight, +
342d3 31 20 69 66 20 69 74 20 69 73 20 63 6f 6e 73 69  1 if it is consi
342d4 64 65 72 65 64 20 67 72 65 61 74 65 72 0a 2a 2a  dered greater.**
342d5 20 74 68 61 6e 20 69 74 2c 20 6f 72 20 30 20 69   than it, or 0 i
342d6 66 20 69 74 20 69 73 20 65 71 75 61 6c 2e 20 69  f it is equal. i
342d7 2e 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 2a  .e..**.**     (*
342d8 70 4c 65 66 74 20 2d 20 2a 70 52 69 67 68 74 29  pLeft - *pRight)
342d9 0a 2a 2a 0a 2a 2a 20 41 20 50 4c 52 65 61 64 65  .**.** A PLReade
342da 72 20 74 68 61 74 20 69 73 20 69 6e 20 74 68 65  r that is in the
342db 20 45 4f 46 20 63 6f 6e 64 69 74 69 6f 6e 20 69   EOF condition i
342dc 73 20 63 6f 6e 73 69 64 65 72 65 64 20 67 72 65  s considered gre
342dd 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 61 6e 79  ater than.** any
342de 20 6f 74 68 65 72 2e 20 49 66 20 6e 65 69 74 68   other. If neith
342df 65 72 20 61 72 67 75 6d 65 6e 74 20 69 73 20 69  er argument is i
342e0 6e 20 45 4f 46 20 73 74 61 74 65 2c 20 74 68 65  n EOF state, the
342e1 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66   return value of
342e2 0a 2a 2a 20 70 6c 72 43 6f 6c 75 6d 6e 28 29 20  .** plrColumn() 
342e3 69 73 20 75 73 65 64 2e 20 49 66 20 74 68 65 20  is used. If the 
342e4 70 6c 72 43 6f 6c 75 6d 6e 28 29 20 76 61 6c 75  plrColumn() valu
342e5 65 73 20 61 72 65 20 65 71 75 61 6c 2c 20 74 68  es are equal, th
342e6 65 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20  e.** comparison 
342e7 69 73 20 6f 6e 20 74 68 65 20 62 61 73 69 73 20  is on the basis 
342e8 6f 66 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 29  of plrPosition()
342e9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
342ea 70 6c 72 43 6f 6d 70 61 72 65 28 50 4c 52 65 61  plrCompare(PLRea
342eb 64 65 72 20 2a 70 4c 65 66 74 2c 20 50 4c 52 65  der *pLeft, PLRe
342ec 61 64 65 72 20 2a 70 52 69 67 68 74 29 7b 0a 20  ader *pRight){. 
342ed 20 61 73 73 65 72 74 28 21 70 6c 72 41 74 45 6e   assert(!plrAtEn
342ee 64 28 70 4c 65 66 74 29 20 7c 7c 20 21 70 6c 72  d(pLeft) || !plr
342ef 41 74 45 6e 64 28 70 52 69 67 68 74 29 29 3b 0a  AtEnd(pRight));.
342f0 0a 20 20 69 66 28 20 70 6c 72 41 74 45 6e 64 28  .  if( plrAtEnd(
342f1 70 52 69 67 68 74 29 20 7c 7c 20 70 6c 72 41 74  pRight) || plrAt
342f2 45 6e 64 28 70 4c 65 66 74 29 20 29 7b 0a 20 20  End(pLeft) ){.  
342f3 20 20 72 65 74 75 72 6e 20 28 70 6c 72 41 74 45    return (plrAtE
342f4 6e 64 28 70 52 69 67 68 74 29 20 3f 20 2d 31 20  nd(pRight) ? -1 
342f5 3a 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  : 1);.  }.  if( 
342f6 70 6c 72 43 6f 6c 75 6d 6e 28 70 4c 65 66 74 29  plrColumn(pLeft)
342f7 21 3d 70 6c 72 43 6f 6c 75 6d 6e 28 70 52 69 67  !=plrColumn(pRig
342f8 68 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ht) ){.    retur
342f9 6e 20 28 28 70 6c 72 43 6f 6c 75 6d 6e 28 70 4c  n ((plrColumn(pL
342fa 65 66 74 29 3c 70 6c 72 43 6f 6c 75 6d 6e 28 70  eft)<plrColumn(p
342fb 52 69 67 68 74 29 29 20 3f 20 2d 31 20 3a 20 31  Right)) ? -1 : 1
342fc 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 6c 72  );.  }.  if( plr
342fd 50 6f 73 69 74 69 6f 6e 28 70 4c 65 66 74 29 21  Position(pLeft)!
342fe 3d 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70 52 69  =plrPosition(pRi
342ff 67 68 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75  ght) ){.    retu
34300 72 6e 20 28 28 70 6c 72 50 6f 73 69 74 69 6f 6e  rn ((plrPosition
34301 28 70 4c 65 66 74 29 3c 70 6c 72 50 6f 73 69 74  (pLeft)<plrPosit
34302 69 6f 6e 28 70 52 69 67 68 74 29 29 20 3f 20 2d  ion(pRight)) ? -
34303 31 20 3a 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65  1 : 1);.  }.  re
34304 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 57 65  turn 0;.}../* We
34305 20 68 61 76 65 20 74 77 6f 20 64 6f 63 6c 69 73   have two doclis
34306 74 73 20 77 69 74 68 20 70 6f 73 69 74 69 6f 6e  ts with position
34307 73 3a 20 20 70 4c 65 66 74 20 61 6e 64 20 70 52  s:  pLeft and pR
34308 69 67 68 74 2e 20 44 65 70 65 6e 64 69 6e 67 0a  ight. Depending.
34309 2a 2a 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20  ** on the value 
3430a 6f 66 20 74 68 65 20 6e 4e 65 61 72 20 70 61 72  of the nNear par
3430b 61 6d 65 74 65 72 2c 20 70 65 72 66 6f 72 6d 20  ameter, perform 
3430c 65 69 74 68 65 72 20 61 20 70 68 72 61 73 65 0a  either a phrase.
3430d 2a 2a 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20  ** intersection 
3430e 28 69 66 20 6e 4e 65 61 72 3d 3d 30 29 20 6f 72  (if nNear==0) or
3430f 20 61 20 4e 45 41 52 20 69 6e 74 65 72 73 65 63   a NEAR intersec
34310 74 69 6f 6e 20 28 69 66 20 6e 4e 65 61 72 3e 30  tion (if nNear>0
34311 29 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 74  ).** and write t
34312 68 65 20 72 65 73 75 6c 74 73 20 69 6e 74 6f 20  he results into 
34313 70 4f 75 74 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 68  pOut..**.** A ph
34314 72 61 73 65 20 69 6e 74 65 72 73 65 63 74 69 6f  rase intersectio
34315 6e 20 6d 65 61 6e 73 20 74 68 61 74 20 74 77 6f  n means that two
34316 20 64 6f 63 75 6d 65 6e 74 73 20 6f 6e 6c 79 20   documents only 
34317 6d 61 74 63 68 0a 2a 2a 20 69 66 20 70 4c 65 66  match.** if pLef
34318 74 2e 69 50 6f 73 2b 31 3d 3d 70 52 69 67 68 74  t.iPos+1==pRight
34319 2e 69 50 6f 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e  .iPos..**.** A N
3431a 45 41 52 20 69 6e 74 65 72 73 65 63 74 69 6f 6e  EAR intersection
3431b 20 6d 65 61 6e 73 20 74 68 61 74 20 74 77 6f 20   means that two 
3431c 64 6f 63 75 6d 65 6e 74 73 20 6f 6e 6c 79 20 6d  documents only m
3431d 61 74 63 68 20 69 66 20 0a 2a 2a 20 28 61 62 73  atch if .** (abs
3431e 28 70 4c 65 66 74 2e 69 50 6f 73 2d 70 52 69 67  (pLeft.iPos-pRig
3431f 68 74 2e 69 50 6f 73 29 3c 6e 4e 65 61 72 29 2e  ht.iPos)<nNear).
34320 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 4e 45 41 52  .**.** If a NEAR
34321 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 69 73   intersection is
34322 20 72 65 71 75 65 73 74 65 64 2c 20 74 68 65 6e   requested, then
34323 20 74 68 65 20 6e 50 68 72 61 73 65 20 61 72 67   the nPhrase arg
34324 75 6d 65 6e 74 20 73 68 6f 75 6c 64 0a 2a 2a 20  ument should.** 
34325 62 65 20 70 61 73 73 65 64 20 74 68 65 20 6e 75  be passed the nu
34326 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69  mber of tokens i
34327 6e 20 74 68 65 20 74 77 6f 20 6f 70 65 72 61 6e  n the two operan
34328 64 73 20 74 6f 20 74 68 65 20 4e 45 41 52 20 6f  ds to the NEAR o
34329 70 65 72 61 74 6f 72 0a 2a 2a 20 63 6f 6d 62 69  perator.** combi
3432a 6e 65 64 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65  ned. For example
3432b 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 51 75  :.**.**       Qu
3432c 65 72 79 20 73 79 6e 74 61 78 20 20 20 20 20 20  ery syntax      
3432d 20 20 20 20 20 20 20 20 20 6e 50 68 72 61 73 65           nPhrase
3432e 0a 2a 2a 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d  .**      -------
3432f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
34330 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
34331 20 20 20 20 20 20 20 22 41 20 42 20 43 22 20 4e         "A B C" N
34332 45 41 52 20 22 44 20 45 22 20 20 20 20 20 20 20  EAR "D E"       
34333 20 20 35 0a 2a 2a 20 20 20 20 20 20 20 41 20 4e    5.**       A N
34334 45 41 52 20 42 20 20 20 20 20 20 20 20 20 20 20  EAR B           
34335 20 20 20 20 20 20 20 20 32 0a 2a 2a 0a 2a 2a 20          2.**.** 
34336 69 54 79 70 65 20 63 6f 6e 74 72 6f 6c 73 20 74  iType controls t
34337 68 65 20 74 79 70 65 20 6f 66 20 64 61 74 61 20  he type of data 
34338 77 72 69 74 74 65 6e 20 74 6f 20 70 4f 75 74 2e  written to pOut.
34339 20 20 49 66 20 69 54 79 70 65 20 69 73 0a 2a 2a    If iType is.**
3433a 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c 20 74   DL_POSITIONS, t
3433b 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 61 72 65  he positions are
3433c 20 74 68 6f 73 65 20 66 72 6f 6d 20 70 52 69 67   those from pRig
3433d 68 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ht..*/.static vo
3433e 69 64 20 64 6f 63 4c 69 73 74 50 68 72 61 73 65  id docListPhrase
3433f 4d 65 72 67 65 28 0a 20 20 63 6f 6e 73 74 20 63  Merge(.  const c
34340 68 61 72 20 2a 70 4c 65 66 74 2c 20 69 6e 74 20  har *pLeft, int 
34341 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20 63  nLeft,.  const c
34342 68 61 72 20 2a 70 52 69 67 68 74 2c 20 69 6e 74  har *pRight, int
34343 20 6e 52 69 67 68 74 2c 0a 20 20 69 6e 74 20 6e   nRight,.  int n
34344 4e 65 61 72 2c 20 20 20 20 20 20 20 20 20 20 20  Near,           
34345 20 2f 2a 20 30 20 66 6f 72 20 61 20 70 68 72 61   /* 0 for a phra
34346 73 65 20 6d 65 72 67 65 2c 20 6e 6f 6e 2d 7a 65  se merge, non-ze
34347 72 6f 20 66 6f 72 20 61 20 4e 45 41 52 20 6d 65  ro for a NEAR me
34348 72 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 68  rge */.  int nPh
34349 72 61 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  rase,          /
3434a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65  * Number of toke
3434b 6e 73 20 69 6e 20 6c 65 66 74 2b 72 69 67 68 74  ns in left+right
3434c 20 6f 70 65 72 61 6e 64 73 20 74 6f 20 4e 45 41   operands to NEA
3434d 52 20 2a 2f 0a 20 20 44 6f 63 4c 69 73 74 54 79  R */.  DocListTy
3434e 70 65 20 69 54 79 70 65 2c 20 20 20 20 2f 2a 20  pe iType,    /* 
3434f 54 79 70 65 20 6f 66 20 64 6f 63 6c 69 73 74 20  Type of doclist 
34350 74 6f 20 77 72 69 74 65 20 74 6f 20 70 4f 75 74  to write to pOut
34351 20 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72   */.  DataBuffer
34352 20 2a 70 4f 75 74 20 20 20 20 20 20 2f 2a 20 57   *pOut      /* W
34353 72 69 74 65 20 74 68 65 20 63 6f 6d 62 69 6e 65  rite the combine
34354 64 20 64 6f 63 6c 69 73 74 20 68 65 72 65 20 2a  d doclist here *
34355 2f 0a 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20  /.){.  DLReader 
34356 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 44  left, right;.  D
34357 4c 57 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a  LWriter writer;.
34358 0a 20 20 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20  .  if( nLeft==0 
34359 7c 7c 20 6e 52 69 67 68 74 3d 3d 30 20 29 20 72  || nRight==0 ) r
3435a 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74  eturn;..  assert
3435b 28 20 69 54 79 70 65 21 3d 44 4c 5f 50 4f 53 49  ( iType!=DL_POSI
3435c 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 3b  TIONS_OFFSETS );
3435d 0a 0a 20 20 64 6c 72 49 6e 69 74 28 26 6c 65 66  ..  dlrInit(&lef
3435e 74 2c 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c  t, DL_POSITIONS,
3435f 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a   pLeft, nLeft);.
34360 20 20 64 6c 72 49 6e 69 74 28 26 72 69 67 68 74    dlrInit(&right
34361 2c 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c 20  , DL_POSITIONS, 
34362 70 52 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b  pRight, nRight);
34363 0a 20 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74  .  dlwInit(&writ
34364 65 72 2c 20 69 54 79 70 65 2c 20 70 4f 75 74 29  er, iType, pOut)
34365 3b 0a 0a 20 20 77 68 69 6c 65 28 20 21 64 6c 72  ;..  while( !dlr
34366 41 74 45 6e 64 28 26 6c 65 66 74 29 20 26 26 20  AtEnd(&left) && 
34367 21 64 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74  !dlrAtEnd(&right
34368 29 20 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72  ) ){.    if( dlr
34369 44 6f 63 69 64 28 26 6c 65 66 74 29 3c 64 6c 72  Docid(&left)<dlr
3436a 44 6f 63 69 64 28 26 72 69 67 68 74 29 20 29 7b  Docid(&right) ){
3436b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26  .      dlrStep(&
3436c 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  left);.    }else
3436d 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28 26 72   if( dlrDocid(&r
3436e 69 67 68 74 29 3c 64 6c 72 44 6f 63 69 64 28 26  ight)<dlrDocid(&
3436f 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20 64  left) ){.      d
34370 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a  lrStep(&right);.
34371 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
34372 20 69 66 28 20 6e 4e 65 61 72 3d 3d 30 20 29 7b   if( nNear==0 ){
34373 0a 20 20 20 20 20 20 20 20 70 6f 73 4c 69 73 74  .        posList
34374 50 68 72 61 73 65 4d 65 72 67 65 28 26 6c 65 66  PhraseMerge(&lef
34375 74 2c 20 26 72 69 67 68 74 2c 20 30 2c 20 30 2c  t, &right, 0, 0,
34376 20 26 77 72 69 74 65 72 29 3b 0a 20 20 20 20 20   &writer);.     
34377 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
34378 2f 2a 20 54 68 69 73 20 63 61 73 65 20 6f 63 63  /* This case occ
34379 75 72 73 20 77 68 65 6e 20 74 77 6f 20 74 65 72  urs when two ter
3437a 6d 73 20 28 73 69 6d 70 6c 65 20 74 65 72 6d 73  ms (simple terms
3437b 20 6f 72 20 70 68 72 61 73 65 73 29 20 61 72 65   or phrases) are
3437c 0a 20 20 20 20 20 20 20 20 20 2a 20 63 6f 6e 6e  .         * conn
3437d 65 63 74 65 64 20 62 79 20 61 20 4e 45 41 52 20  ected by a NEAR 
3437e 6f 70 65 72 61 74 6f 72 2c 20 73 70 61 6e 20 28  operator, span (
3437f 6e 4e 65 61 72 2b 31 29 2e 20 69 2e 65 2e 0a 20  nNear+1). i.e.. 
34380 20 20 20 20 20 20 20 20 2a 0a 20 20 20 20 20 20          *.      
34381 20 20 20 2a 20 20 20 20 20 27 22 74 65 72 72 69     *     '"terri
34382 62 6c 65 20 63 6f 6d 70 61 6e 79 22 20 4e 45 41  ble company" NEA
34383 52 20 77 69 64 67 65 74 27 0a 20 20 20 20 20 20  R widget'.      
34384 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 44 61     */.        Da
34385 74 61 42 75 66 66 65 72 20 6f 6e 65 20 3d 20 7b  taBuffer one = {
34386 30 2c 20 30 2c 20 30 7d 3b 0a 20 20 20 20 20 20  0, 0, 0};.      
34387 20 20 44 61 74 61 42 75 66 66 65 72 20 74 77 6f    DataBuffer two
34388 20 3d 20 7b 30 2c 20 30 2c 20 30 7d 3b 0a 0a 20   = {0, 0, 0};.. 
34389 20 20 20 20 20 20 20 44 4c 57 72 69 74 65 72 20         DLWriter 
3438a 64 6c 77 72 69 74 65 72 32 3b 0a 20 20 20 20 20  dlwriter2;.     
3438b 20 20 20 44 4c 52 65 61 64 65 72 20 64 72 31 20     DLReader dr1 
3438c 3d 20 7b 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  = {0, 0, 0, 0, 0
3438d 7d 3b 20 0a 20 20 20 20 20 20 20 20 44 4c 52 65  }; .        DLRe
3438e 61 64 65 72 20 64 72 32 20 3d 20 7b 30 2c 20 30  ader dr2 = {0, 0
3438f 2c 20 30 2c 20 30 2c 20 30 7d 3b 0a 0a 20 20 20  , 0, 0, 0};..   
34390 20 20 20 20 20 64 6c 77 49 6e 69 74 28 26 64 6c       dlwInit(&dl
34391 77 72 69 74 65 72 32 2c 20 69 54 79 70 65 2c 20  writer2, iType, 
34392 26 6f 6e 65 29 3b 0a 20 20 20 20 20 20 20 20 70  &one);.        p
34393 6f 73 4c 69 73 74 50 68 72 61 73 65 4d 65 72 67  osListPhraseMerg
34394 65 28 26 72 69 67 68 74 2c 20 26 6c 65 66 74 2c  e(&right, &left,
34395 20 6e 4e 65 61 72 2d 33 2b 6e 50 68 72 61 73 65   nNear-3+nPhrase
34396 2c 20 31 2c 20 26 64 6c 77 72 69 74 65 72 32 29  , 1, &dlwriter2)
34397 3b 0a 20 20 20 20 20 20 20 20 64 6c 77 49 6e 69  ;.        dlwIni
34398 74 28 26 64 6c 77 72 69 74 65 72 32 2c 20 69 54  t(&dlwriter2, iT
34399 79 70 65 2c 20 26 74 77 6f 29 3b 0a 20 20 20 20  ype, &two);.    
3439a 20 20 20 20 70 6f 73 4c 69 73 74 50 68 72 61 73      posListPhras
3439b 65 4d 65 72 67 65 28 26 6c 65 66 74 2c 20 26 72  eMerge(&left, &r
3439c 69 67 68 74 2c 20 6e 4e 65 61 72 2d 31 2c 20 30  ight, nNear-1, 0
3439d 2c 20 26 64 6c 77 72 69 74 65 72 32 29 3b 0a 0a  , &dlwriter2);..
3439e 20 20 20 20 20 20 20 20 69 66 28 20 6f 6e 65 2e          if( one.
3439f 6e 44 61 74 61 29 20 64 6c 72 49 6e 69 74 28 26  nData) dlrInit(&
343a0 64 72 31 2c 20 69 54 79 70 65 2c 20 6f 6e 65 2e  dr1, iType, one.
343a1 70 44 61 74 61 2c 20 6f 6e 65 2e 6e 44 61 74 61  pData, one.nData
343a2 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 74  );.        if( t
343a3 77 6f 2e 6e 44 61 74 61 29 20 64 6c 72 49 6e 69  wo.nData) dlrIni
343a4 74 28 26 64 72 32 2c 20 69 54 79 70 65 2c 20 74  t(&dr2, iType, t
343a5 77 6f 2e 70 44 61 74 61 2c 20 74 77 6f 2e 6e 44  wo.pData, two.nD
343a6 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 20 20 69  ata);..        i
343a7 66 28 20 21 64 6c 72 41 74 45 6e 64 28 26 64 72  f( !dlrAtEnd(&dr
343a8 31 29 20 7c 7c 20 21 64 6c 72 41 74 45 6e 64 28  1) || !dlrAtEnd(
343a9 26 64 72 32 29 20 29 7b 0a 20 20 20 20 20 20 20  &dr2) ){.       
343aa 20 20 20 50 4c 52 65 61 64 65 72 20 70 72 31 20     PLReader pr1 
343ab 3d 20 7b 30 7d 3b 0a 20 20 20 20 20 20 20 20 20  = {0};.         
343ac 20 50 4c 52 65 61 64 65 72 20 70 72 32 20 3d 20   PLReader pr2 = 
343ad 7b 30 7d 3b 0a 0a 20 20 20 20 20 20 20 20 20 20  {0};..          
343ae 50 4c 57 72 69 74 65 72 20 70 6c 77 72 69 74 65  PLWriter plwrite
343af 72 3b 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77  r;.          plw
343b0 49 6e 69 74 28 26 70 6c 77 72 69 74 65 72 2c 20  Init(&plwriter, 
343b1 26 77 72 69 74 65 72 2c 20 64 6c 72 44 6f 63 69  &writer, dlrDoci
343b2 64 28 64 6c 72 41 74 45 6e 64 28 26 64 72 31 29  d(dlrAtEnd(&dr1)
343b3 3f 26 64 72 32 3a 26 64 72 31 29 29 3b 0a 0a 20  ?&dr2:&dr1));.. 
343b4 20 20 20 20 20 20 20 20 20 69 66 28 20 6f 6e 65           if( one
343b5 2e 6e 44 61 74 61 20 29 20 70 6c 72 49 6e 69 74  .nData ) plrInit
343b6 28 26 70 72 31 2c 20 26 64 72 31 29 3b 0a 20 20  (&pr1, &dr1);.  
343b7 20 20 20 20 20 20 20 20 69 66 28 20 74 77 6f 2e          if( two.
343b8 6e 44 61 74 61 20 29 20 70 6c 72 49 6e 69 74 28  nData ) plrInit(
343b9 26 70 72 32 2c 20 26 64 72 32 29 3b 0a 20 20 20  &pr2, &dr2);.   
343ba 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 21 70         while( !p
343bb 6c 72 41 74 45 6e 64 28 26 70 72 31 29 20 7c 7c  lrAtEnd(&pr1) ||
343bc 20 21 70 6c 72 41 74 45 6e 64 28 26 70 72 32 29   !plrAtEnd(&pr2)
343bd 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
343be 69 6e 74 20 69 43 6f 6d 70 61 72 65 20 3d 20 70  int iCompare = p
343bf 6c 72 43 6f 6d 70 61 72 65 28 26 70 72 31 2c 20  lrCompare(&pr1, 
343c0 26 70 72 32 29 3b 0a 20 20 20 20 20 20 20 20 20  &pr2);.         
343c1 20 20 20 73 77 69 74 63 68 28 20 69 43 6f 6d 70     switch( iComp
343c2 61 72 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  are ){.         
343c3 20 20 20 20 20 63 61 73 65 20 2d 31 3a 0a 20 20       case -1:.  
343c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6c                pl
343c5 77 43 6f 70 79 28 26 70 6c 77 72 69 74 65 72 2c  wCopy(&plwriter,
343c6 20 26 70 72 31 29 3b 0a 20 20 20 20 20 20 20 20   &pr1);.        
343c7 20 20 20 20 20 20 20 20 70 6c 72 53 74 65 70 28          plrStep(
343c8 26 70 72 31 29 3b 0a 20 20 20 20 20 20 20 20 20  &pr1);.         
343c9 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
343ca 20 20 20 20 20 20 20 20 20 20 20 20 63 61 73 65              case
343cb 20 31 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20   1:.            
343cc 20 20 20 20 70 6c 77 43 6f 70 79 28 26 70 6c 77      plwCopy(&plw
343cd 72 69 74 65 72 2c 20 26 70 72 32 29 3b 0a 20 20  riter, &pr2);.  
343ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6c                pl
343cf 72 53 74 65 70 28 26 70 72 32 29 3b 0a 20 20 20  rStep(&pr2);.   
343d0 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65               bre
343d1 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ak;.            
343d2 20 20 63 61 73 65 20 30 3a 0a 20 20 20 20 20 20    case 0:.      
343d3 20 20 20 20 20 20 20 20 20 20 70 6c 77 43 6f 70            plwCop
343d4 79 28 26 70 6c 77 72 69 74 65 72 2c 20 26 70 72  y(&plwriter, &pr
343d5 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  1);.            
343d6 20 20 20 20 70 6c 72 53 74 65 70 28 26 70 72 31      plrStep(&pr1
343d7 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
343d8 20 20 20 70 6c 72 53 74 65 70 28 26 70 72 32 29     plrStep(&pr2)
343d9 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
343da 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
343db 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
343dc 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77   }.          plw
343dd 54 65 72 6d 69 6e 61 74 65 28 26 70 6c 77 72 69  Terminate(&plwri
343de 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ter);.        }.
343df 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66          dataBuff
343e0 65 72 44 65 73 74 72 6f 79 28 26 6f 6e 65 29 3b  erDestroy(&one);
343e1 0a 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66  .        dataBuf
343e2 66 65 72 44 65 73 74 72 6f 79 28 26 74 77 6f 29  ferDestroy(&two)
343e3 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
343e4 64 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a  dlrStep(&left);.
343e5 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 72        dlrStep(&r
343e6 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ight);.    }.  }
343e7 0a 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26  ..  dlrDestroy(&
343e8 6c 65 66 74 29 3b 0a 20 20 64 6c 72 44 65 73 74  left);.  dlrDest
343e9 72 6f 79 28 26 72 69 67 68 74 29 3b 0a 20 20 64  roy(&right);.  d
343ea 6c 77 44 65 73 74 72 6f 79 28 26 77 72 69 74 65  lwDestroy(&write
343eb 72 29 3b 0a 7d 0a 0a 2f 2a 20 57 65 20 68 61 76  r);.}../* We hav
343ec 65 20 74 77 6f 20 44 4c 5f 44 4f 43 49 44 53 20  e two DL_DOCIDS 
343ed 64 6f 63 6c 69 73 74 73 3a 20 20 70 4c 65 66 74  doclists:  pLeft
343ee 20 61 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2a 20   and pRight..** 
343ef 57 72 69 74 65 20 74 68 65 20 69 6e 74 65 72 73  Write the inters
343f0 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20  ection of these 
343f1 74 77 6f 20 64 6f 63 6c 69 73 74 73 20 69 6e 74  two doclists int
343f2 6f 20 70 4f 75 74 20 61 73 20 61 0a 2a 2a 20 44  o pOut as a.** D
343f3 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74  L_DOCIDS doclist
343f4 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
343f5 20 64 6f 63 4c 69 73 74 41 6e 64 4d 65 72 67 65   docListAndMerge
343f6 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  (.  const char *
343f7 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c 65 66 74  pLeft, int nLeft
343f8 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
343f9 70 52 69 67 68 74 2c 20 69 6e 74 20 6e 52 69 67  pRight, int nRig
343fa 68 74 2c 0a 20 20 44 61 74 61 42 75 66 66 65 72  ht,.  DataBuffer
343fb 20 2a 70 4f 75 74 20 20 20 20 20 20 2f 2a 20 57   *pOut      /* W
343fc 72 69 74 65 20 74 68 65 20 63 6f 6d 62 69 6e 65  rite the combine
343fd 64 20 64 6f 63 6c 69 73 74 20 68 65 72 65 20 2a  d doclist here *
343fe 2f 0a 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20  /.){.  DLReader 
343ff 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 44  left, right;.  D
34400 4c 57 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a  LWriter writer;.
34401 0a 20 20 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20  .  if( nLeft==0 
34402 7c 7c 20 6e 52 69 67 68 74 3d 3d 30 20 29 20 72  || nRight==0 ) r
34403 65 74 75 72 6e 3b 0a 0a 20 20 64 6c 72 49 6e 69  eturn;..  dlrIni
34404 74 28 26 6c 65 66 74 2c 20 44 4c 5f 44 4f 43 49  t(&left, DL_DOCI
34405 44 53 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74  DS, pLeft, nLeft
34406 29 3b 0a 20 20 64 6c 72 49 6e 69 74 28 26 72 69  );.  dlrInit(&ri
34407 67 68 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20  ght, DL_DOCIDS, 
34408 70 52 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b  pRight, nRight);
34409 0a 20 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74  .  dlwInit(&writ
3440a 65 72 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70  er, DL_DOCIDS, p
3440b 4f 75 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20  Out);..  while( 
3440c 21 64 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29  !dlrAtEnd(&left)
3440d 20 26 26 20 21 64 6c 72 41 74 45 6e 64 28 26 72   && !dlrAtEnd(&r
3440e 69 67 68 74 29 20 29 7b 0a 20 20 20 20 69 66 28  ight) ){.    if(
3440f 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29   dlrDocid(&left)
34410 3c 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74  <dlrDocid(&right
34411 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 72 53 74  ) ){.      dlrSt
34412 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d  ep(&left);.    }
34413 65 6c 73 65 20 69 66 28 20 64 6c 72 44 6f 63 69  else if( dlrDoci
34414 64 28 26 72 69 67 68 74 29 3c 64 6c 72 44 6f 63  d(&right)<dlrDoc
34415 69 64 28 26 6c 65 66 74 29 20 29 7b 0a 20 20 20  id(&left) ){.   
34416 20 20 20 64 6c 72 53 74 65 70 28 26 72 69 67 68     dlrStep(&righ
34417 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  t);.    }else{. 
34418 20 20 20 20 20 64 6c 77 41 64 64 28 26 77 72 69       dlwAdd(&wri
34419 74 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 6c  ter, dlrDocid(&l
3441a 65 66 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72  eft));.      dlr
3441b 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20  Step(&left);.   
3441c 20 20 20 64 6c 72 53 74 65 70 28 26 72 69 67 68     dlrStep(&righ
3441d 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  t);.    }.  }.. 
3441e 20 64 6c 72 44 65 73 74 72 6f 79 28 26 6c 65 66   dlrDestroy(&lef
3441f 74 29 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f 79  t);.  dlrDestroy
34420 28 26 72 69 67 68 74 29 3b 0a 20 20 64 6c 77 44  (&right);.  dlwD
34421 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b  estroy(&writer);
34422 0a 7d 0a 0a 2f 2a 20 57 65 20 68 61 76 65 20 74  .}../* We have t
34423 77 6f 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63  wo DL_DOCIDS doc
34424 6c 69 73 74 73 3a 20 20 70 4c 65 66 74 20 61 6e  lists:  pLeft an
34425 64 20 70 52 69 67 68 74 2e 0a 2a 2a 20 57 72 69  d pRight..** Wri
34426 74 65 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20  te the union of 
34427 74 68 65 73 65 20 74 77 6f 20 64 6f 63 6c 69 73  these two doclis
34428 74 73 20 69 6e 74 6f 20 70 4f 75 74 20 61 73 20  ts into pOut as 
34429 61 0a 2a 2a 20 44 4c 5f 44 4f 43 49 44 53 20 64  a.** DL_DOCIDS d
3442a 6f 63 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69  oclist..*/.stati
3442b 63 20 76 6f 69 64 20 64 6f 63 4c 69 73 74 4f 72  c void docListOr
3442c 4d 65 72 67 65 28 0a 20 20 63 6f 6e 73 74 20 63  Merge(.  const c
3442d 68 61 72 20 2a 70 4c 65 66 74 2c 20 69 6e 74 20  har *pLeft, int 
3442e 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20 63  nLeft,.  const c
3442f 68 61 72 20 2a 70 52 69 67 68 74 2c 20 69 6e 74  har *pRight, int
34430 20 6e 52 69 67 68 74 2c 0a 20 20 44 61 74 61 42   nRight,.  DataB
34431 75 66 66 65 72 20 2a 70 4f 75 74 20 20 20 20 20  uffer *pOut     
34432 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f   /* Write the co
34433 6d 62 69 6e 65 64 20 64 6f 63 6c 69 73 74 20 68  mbined doclist h
34434 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 44 4c 52 65  ere */.){.  DLRe
34435 61 64 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74  ader left, right
34436 3b 0a 20 20 44 4c 57 72 69 74 65 72 20 77 72 69  ;.  DLWriter wri
34437 74 65 72 3b 0a 0a 20 20 69 66 28 20 6e 4c 65 66  ter;..  if( nLef
34438 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  t==0 ){.    if( 
34439 6e 52 69 67 68 74 21 3d 30 20 29 20 64 61 74 61  nRight!=0 ) data
3443a 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 4f 75  BufferAppend(pOu
3443b 74 2c 20 70 52 69 67 68 74 2c 20 6e 52 69 67 68  t, pRight, nRigh
3443c 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  t);.    return;.
3443d 20 20 7d 0a 20 20 69 66 28 20 6e 52 69 67 68 74    }.  if( nRight
3443e 3d 3d 30 20 29 7b 0a 20 20 20 20 64 61 74 61 42  ==0 ){.    dataB
3443f 75 66 66 65 72 41 70 70 65 6e 64 28 70 4f 75 74  ufferAppend(pOut
34440 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b  , pLeft, nLeft);
34441 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
34442 0a 0a 20 20 64 6c 72 49 6e 69 74 28 26 6c 65 66  ..  dlrInit(&lef
34443 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 4c  t, DL_DOCIDS, pL
34444 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20 64  eft, nLeft);.  d
34445 6c 72 49 6e 69 74 28 26 72 69 67 68 74 2c 20 44  lrInit(&right, D
34446 4c 5f 44 4f 43 49 44 53 2c 20 70 52 69 67 68 74  L_DOCIDS, pRight
34447 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20 64 6c 77  , nRight);.  dlw
34448 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20 44 4c  Init(&writer, DL
34449 5f 44 4f 43 49 44 53 2c 20 70 4f 75 74 29 3b 0a  _DOCIDS, pOut);.
3444a 0a 20 20 77 68 69 6c 65 28 20 21 64 6c 72 41 74  .  while( !dlrAt
3444b 45 6e 64 28 26 6c 65 66 74 29 20 7c 7c 20 21 64  End(&left) || !d
3444c 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29 20  lrAtEnd(&right) 
3444d 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72 41 74  ){.    if( dlrAt
3444e 45 6e 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20  End(&right) ){. 
3444f 20 20 20 20 20 64 6c 77 41 64 64 28 26 77 72 69       dlwAdd(&wri
34450 74 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 6c  ter, dlrDocid(&l
34451 65 66 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72  eft));.      dlr
34452 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20  Step(&left);.   
34453 20 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 41 74   }else if( dlrAt
34454 45 6e 64 28 26 6c 65 66 74 29 20 29 7b 0a 20 20  End(&left) ){.  
34455 20 20 20 20 64 6c 77 41 64 64 28 26 77 72 69 74      dlwAdd(&writ
34456 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 72 69  er, dlrDocid(&ri
34457 67 68 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72  ght));.      dlr
34458 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20  Step(&right);.  
34459 20 20 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 44    }else if( dlrD
3445a 6f 63 69 64 28 26 6c 65 66 74 29 3c 64 6c 72 44  ocid(&left)<dlrD
3445b 6f 63 69 64 28 26 72 69 67 68 74 29 20 29 7b 0a  ocid(&right) ){.
3445c 20 20 20 20 20 20 64 6c 77 41 64 64 28 26 77 72        dlwAdd(&wr
3445d 69 74 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26  iter, dlrDocid(&
3445e 6c 65 66 74 29 29 3b 0a 20 20 20 20 20 20 64 6c  left));.      dl
3445f 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20  rStep(&left);.  
34460 20 20 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 44    }else if( dlrD
34461 6f 63 69 64 28 26 72 69 67 68 74 29 3c 64 6c 72  ocid(&right)<dlr
34462 44 6f 63 69 64 28 26 6c 65 66 74 29 20 29 7b 0a  Docid(&left) ){.
34463 20 20 20 20 20 20 64 6c 77 41 64 64 28 26 77 72        dlwAdd(&wr
34464 69 74 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26  iter, dlrDocid(&
34465 72 69 67 68 74 29 29 3b 0a 20 20 20 20 20 20 64  right));.      d
34466 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a  lrStep(&right);.
34467 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
34468 20 64 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c   dlwAdd(&writer,
34469 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29   dlrDocid(&left)
3446a 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70  );.      dlrStep
3446b 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 64  (&left);.      d
3446c 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a  lrStep(&right);.
3446d 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 64 6c 72      }.  }..  dlr
3446e 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a  Destroy(&left);.
3446f 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72 69    dlrDestroy(&ri
34470 67 68 74 29 3b 0a 20 20 64 6c 77 44 65 73 74 72  ght);.  dlwDestr
34471 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 7d 0a 0a  oy(&writer);.}..
34472 2f 2a 20 57 65 20 68 61 76 65 20 74 77 6f 20 44  /* We have two D
34473 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74  L_DOCIDS doclist
34474 73 3a 20 20 70 4c 65 66 74 20 61 6e 64 20 70 52  s:  pLeft and pR
34475 69 67 68 74 2e 0a 2a 2a 20 57 72 69 74 65 20 69  ight..** Write i
34476 6e 74 6f 20 70 4f 75 74 20 61 73 20 44 4c 5f 44  nto pOut as DL_D
34477 4f 43 49 44 53 20 64 6f 63 6c 69 73 74 20 63 6f  OCIDS doclist co
34478 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 64 6f 63  ntaining all doc
34479 75 6d 65 6e 74 73 20 74 68 61 74 0a 2a 2a 20 6f  uments that.** o
3447a 63 63 75 72 20 69 6e 20 70 4c 65 66 74 20 62 75  ccur in pLeft bu
3447b 74 20 6e 6f 74 20 69 6e 20 70 52 69 67 68 74 2e  t not in pRight.
3447c 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
3447d 64 6f 63 4c 69 73 74 45 78 63 65 70 74 4d 65 72  docListExceptMer
3447e 67 65 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  ge(.  const char
3447f 20 2a 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c 65   *pLeft, int nLe
34480 66 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  ft,.  const char
34481 20 2a 70 52 69 67 68 74 2c 20 69 6e 74 20 6e 52   *pRight, int nR
34482 69 67 68 74 2c 0a 20 20 44 61 74 61 42 75 66 66  ight,.  DataBuff
34483 65 72 20 2a 70 4f 75 74 20 20 20 20 20 20 2f 2a  er *pOut      /*
34484 20 57 72 69 74 65 20 74 68 65 20 63 6f 6d 62 69   Write the combi
34485 6e 65 64 20 64 6f 63 6c 69 73 74 20 68 65 72 65  ned doclist here
34486 20 2a 2f 0a 29 7b 0a 20 20 44 4c 52 65 61 64 65   */.){.  DLReade
34487 72 20 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a 20  r left, right;. 
34488 20 44 4c 57 72 69 74 65 72 20 77 72 69 74 65 72   DLWriter writer
34489 3b 0a 0a 20 20 69 66 28 20 6e 4c 65 66 74 3d 3d  ;..  if( nLeft==
3448a 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  0 ) return;.  if
3448b 28 20 6e 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20  ( nRight==0 ){. 
3448c 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70     dataBufferApp
3448d 65 6e 64 28 70 4f 75 74 2c 20 70 4c 65 66 74 2c  end(pOut, pLeft,
3448e 20 6e 4c 65 66 74 29 3b 0a 20 20 20 20 72 65 74   nLeft);.    ret
3448f 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 64 6c 72 49  urn;.  }..  dlrI
34490 6e 69 74 28 26 6c 65 66 74 2c 20 44 4c 5f 44 4f  nit(&left, DL_DO
34491 43 49 44 53 2c 20 70 4c 65 66 74 2c 20 6e 4c 65  CIDS, pLeft, nLe
34492 66 74 29 3b 0a 20 20 64 6c 72 49 6e 69 74 28 26  ft);.  dlrInit(&
34493 72 69 67 68 74 2c 20 44 4c 5f 44 4f 43 49 44 53  right, DL_DOCIDS
34494 2c 20 70 52 69 67 68 74 2c 20 6e 52 69 67 68 74  , pRight, nRight
34495 29 3b 0a 20 20 64 6c 77 49 6e 69 74 28 26 77 72  );.  dlwInit(&wr
34496 69 74 65 72 2c 20 44 4c 5f 44 4f 43 49 44 53 2c  iter, DL_DOCIDS,
34497 20 70 4f 75 74 29 3b 0a 0a 20 20 77 68 69 6c 65   pOut);..  while
34498 28 20 21 64 6c 72 41 74 45 6e 64 28 26 6c 65 66  ( !dlrAtEnd(&lef
34499 74 29 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28  t) ){.    while(
3449a 20 21 64 6c 72 41 74 45 6e 64 28 26 72 69 67 68   !dlrAtEnd(&righ
3449b 74 29 20 26 26 20 64 6c 72 44 6f 63 69 64 28 26  t) && dlrDocid(&
3449c 72 69 67 68 74 29 3c 64 6c 72 44 6f 63 69 64 28  right)<dlrDocid(
3449d 26 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20  &left) ){.      
3449e 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b  dlrStep(&right);
3449f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64  .    }.    if( d
344a0 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29 20  lrAtEnd(&right) 
344a1 7c 7c 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66  || dlrDocid(&lef
344a2 74 29 3c 64 6c 72 44 6f 63 69 64 28 26 72 69 67  t)<dlrDocid(&rig
344a3 68 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 77  ht) ){.      dlw
344a4 41 64 64 28 26 77 72 69 74 65 72 2c 20 64 6c 72  Add(&writer, dlr
344a5 44 6f 63 69 64 28 26 6c 65 66 74 29 29 3b 0a 20  Docid(&left));. 
344a6 20 20 20 7d 0a 20 20 20 20 64 6c 72 53 74 65 70     }.    dlrStep
344a7 28 26 6c 65 66 74 29 3b 0a 20 20 7d 0a 0a 20 20  (&left);.  }..  
344a8 64 6c 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74  dlrDestroy(&left
344a9 29 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28  );.  dlrDestroy(
344aa 26 72 69 67 68 74 29 3b 0a 20 20 64 6c 77 44 65  &right);.  dlwDe
344ab 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a  stroy(&writer);.
344ac 7d 0a 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a  }..static char *
344ad 73 74 72 69 6e 67 5f 64 75 70 5f 6e 28 63 6f 6e  string_dup_n(con
344ae 73 74 20 63 68 61 72 20 2a 73 2c 20 69 6e 74 20  st char *s, int 
344af 6e 29 7b 0a 20 20 63 68 61 72 20 2a 73 74 72 20  n){.  char *str 
344b0 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
344b1 28 6e 20 2b 20 31 29 3b 0a 20 20 6d 65 6d 63 70  (n + 1);.  memcp
344b2 79 28 73 74 72 2c 20 73 2c 20 6e 29 3b 0a 20 20  y(str, s, n);.  
344b3 73 74 72 5b 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20  str[n] = '\0';. 
344b4 20 72 65 74 75 72 6e 20 73 74 72 3b 0a 7d 0a 0a   return str;.}..
344b5 2f 2a 20 44 75 70 6c 69 63 61 74 65 20 61 20 73  /* Duplicate a s
344b6 74 72 69 6e 67 3b 20 74 68 65 20 63 61 6c 6c 65  tring; the calle
344b7 72 20 6d 75 73 74 20 66 72 65 65 28 29 20 74 68  r must free() th
344b8 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e  e returned strin
344b9 67 2e 0a 20 2a 20 28 57 65 20 64 6f 6e 27 74 20  g.. * (We don't 
344ba 75 73 65 20 73 74 72 64 75 70 28 29 20 73 69 6e  use strdup() sin
344bb 63 65 20 69 74 20 69 73 20 6e 6f 74 20 70 61 72  ce it is not par
344bc 74 20 6f 66 20 74 68 65 20 73 74 61 6e 64 61 72  t of the standar
344bd 64 20 43 20 6c 69 62 72 61 72 79 20 61 6e 64 0a  d C library and.
344be 20 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 76   * may not be av
344bf 61 69 6c 61 62 6c 65 20 65 76 65 72 79 77 68 65  ailable everywhe
344c0 72 65 2e 29 20 2a 2f 0a 73 74 61 74 69 63 20 63  re.) */.static c
344c1 68 61 72 20 2a 73 74 72 69 6e 67 5f 64 75 70 28  har *string_dup(
344c2 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 29 7b 0a  const char *s){.
344c3 20 20 72 65 74 75 72 6e 20 73 74 72 69 6e 67 5f    return string_
344c4 64 75 70 5f 6e 28 73 2c 20 73 74 72 6c 65 6e 28  dup_n(s, strlen(
344c5 73 29 29 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 6d 61  s));.}../* Forma
344c6 74 20 61 20 73 74 72 69 6e 67 2c 20 72 65 70 6c  t a string, repl
344c7 61 63 69 6e 67 20 65 61 63 68 20 6f 63 63 75 72  acing each occur
344c8 72 65 6e 63 65 20 6f 66 20 74 68 65 20 25 20 63  rence of the % c
344c9 68 61 72 61 63 74 65 72 20 77 69 74 68 0a 20 2a  haracter with. *
344ca 20 7a 44 62 2e 7a 4e 61 6d 65 2e 20 20 54 68 69   zDb.zName.  Thi
344cb 73 20 6d 61 79 20 62 65 20 6d 6f 72 65 20 63 6f  s may be more co
344cc 6e 76 65 6e 69 65 6e 74 20 74 68 61 6e 20 73 71  nvenient than sq
344cd 6c 69 74 65 5f 6d 70 72 69 6e 74 66 28 29 0a 20  lite_mprintf(). 
344ce 2a 20 77 68 65 6e 20 6f 6e 65 20 73 74 72 69 6e  * when one strin
344cf 67 20 69 73 20 75 73 65 64 20 72 65 70 65 61 74  g is used repeat
344d0 65 64 6c 79 20 69 6e 20 61 20 66 6f 72 6d 61 74  edly in a format
344d1 20 73 74 72 69 6e 67 2e 0a 20 2a 20 54 68 65 20   string.. * The 
344d2 63 61 6c 6c 65 72 20 6d 75 73 74 20 66 72 65 65  caller must free
344d3 28 29 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  () the returned 
344d4 73 74 72 69 6e 67 2e 20 2a 2f 0a 73 74 61 74 69  string. */.stati
344d5 63 20 63 68 61 72 20 2a 73 74 72 69 6e 67 5f 66  c char *string_f
344d6 6f 72 6d 61 74 28 63 6f 6e 73 74 20 63 68 61 72  ormat(const char
344d7 20 2a 7a 46 6f 72 6d 61 74 2c 0a 20 20 20 20 20   *zFormat,.     
344d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
344d9 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
344da 20 2a 7a 44 62 2c 20 63 6f 6e 73 74 20 63 68 61   *zDb, const cha
344db 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 63 6f 6e  r *zName){.  con
344dc 73 74 20 63 68 61 72 20 2a 70 3b 0a 20 20 73 69  st char *p;.  si
344dd 7a 65 5f 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20  ze_t len = 0;.  
344de 73 69 7a 65 5f 74 20 6e 44 62 20 3d 20 73 74 72  size_t nDb = str
344df 6c 65 6e 28 7a 44 62 29 3b 0a 20 20 73 69 7a 65  len(zDb);.  size
344e0 5f 74 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65  _t nName = strle
344e1 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20 73 69 7a 65  n(zName);.  size
344e2 5f 74 20 6e 46 75 6c 6c 54 61 62 6c 65 4e 61 6d  _t nFullTableNam
344e3 65 20 3d 20 6e 44 62 2b 31 2b 6e 4e 61 6d 65 3b  e = nDb+1+nName;
344e4 0a 20 20 63 68 61 72 20 2a 72 65 73 75 6c 74 3b  .  char *result;
344e5 0a 20 20 63 68 61 72 20 2a 72 3b 0a 0a 20 20 2f  .  char *r;..  /
344e6 2a 20 66 69 72 73 74 20 63 6f 6d 70 75 74 65 20  * first compute 
344e7 6c 65 6e 67 74 68 20 6e 65 65 64 65 64 20 2a 2f  length needed */
344e8 0a 20 20 66 6f 72 28 70 20 3d 20 7a 46 6f 72 6d  .  for(p = zForm
344e9 61 74 20 3b 20 2a 70 20 3b 20 2b 2b 70 29 7b 0a  at ; *p ; ++p){.
344ea 20 20 20 20 6c 65 6e 20 2b 3d 20 28 2a 70 3d 3d      len += (*p==
344eb 27 25 27 20 3f 20 6e 46 75 6c 6c 54 61 62 6c 65  '%' ? nFullTable
344ec 4e 61 6d 65 20 3a 20 31 29 3b 0a 20 20 7d 0a 20  Name : 1);.  }. 
344ed 20 6c 65 6e 20 2b 3d 20 31 3b 20 20 2f 2a 20 66   len += 1;  /* f
344ee 6f 72 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74  or null terminat
344ef 6f 72 20 2a 2f 0a 0a 20 20 72 20 3d 20 72 65 73  or */..  r = res
344f0 75 6c 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  ult = sqlite3_ma
344f1 6c 6c 6f 63 28 6c 65 6e 29 3b 0a 20 20 66 6f 72  lloc(len);.  for
344f2 28 70 20 3d 20 7a 46 6f 72 6d 61 74 3b 20 2a 70  (p = zFormat; *p
344f3 3b 20 2b 2b 70 29 7b 0a 20 20 20 20 69 66 28 20  ; ++p){.    if( 
344f4 2a 70 3d 3d 27 25 27 20 29 7b 0a 20 20 20 20 20  *p=='%' ){.     
344f5 20 6d 65 6d 63 70 79 28 72 2c 20 7a 44 62 2c 20   memcpy(r, zDb, 
344f6 6e 44 62 29 3b 0a 20 20 20 20 20 20 72 20 2b 3d  nDb);.      r +=
344f7 20 6e 44 62 3b 0a 20 20 20 20 20 20 2a 72 2b 2b   nDb;.      *r++
344f8 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 6d 65   = '.';.      me
344f9 6d 63 70 79 28 72 2c 20 7a 4e 61 6d 65 2c 20 6e  mcpy(r, zName, n
344fa 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 20 2b  Name);.      r +
344fb 3d 20 6e 4e 61 6d 65 3b 0a 20 20 20 20 7d 20 65  = nName;.    } e
344fc 6c 73 65 20 7b 0a 20 20 20 20 20 20 2a 72 2b 2b  lse {.      *r++
344fd 20 3d 20 2a 70 3b 0a 20 20 20 20 7d 0a 20 20 7d   = *p;.    }.  }
344fe 0a 20 20 2a 72 2b 2b 20 3d 20 27 5c 30 27 3b 0a  .  *r++ = '\0';.
344ff 20 20 61 73 73 65 72 74 28 20 72 20 3d 3d 20 72    assert( r == r
34500 65 73 75 6c 74 20 2b 20 6c 65 6e 20 29 3b 0a 20  esult + len );. 
34501 20 72 65 74 75 72 6e 20 72 65 73 75 6c 74 3b 0a   return result;.
34502 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  }..static int sq
34503 6c 5f 65 78 65 63 28 73 71 6c 69 74 65 33 20 2a  l_exec(sqlite3 *
34504 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
34505 7a 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  zDb, const char 
34506 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  *zName,.        
34507 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
34508 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 29  t char *zFormat)
34509 7b 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6d 6d 61  {.  char *zComma
3450a 6e 64 20 3d 20 73 74 72 69 6e 67 5f 66 6f 72 6d  nd = string_form
3450b 61 74 28 7a 46 6f 72 6d 61 74 2c 20 7a 44 62 2c  at(zFormat, zDb,
3450c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20 72   zName);.  int r
3450d 63 3b 0a 20 20 46 54 53 54 52 41 43 45 28 28 22  c;.  FTSTRACE(("
3450e 46 54 53 33 20 73 71 6c 3a 20 25 73 5c 6e 22 2c  FTS3 sql: %s\n",
3450f 20 7a 43 6f 6d 6d 61 6e 64 29 29 3b 0a 20 20 72   zCommand));.  r
34510 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  c = sqlite3_exec
34511 28 64 62 2c 20 7a 43 6f 6d 6d 61 6e 64 2c 20 4e  (db, zCommand, N
34512 55 4c 4c 2c 20 30 2c 20 4e 55 4c 4c 29 3b 0a 20  ULL, 0, NULL);. 
34513 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43   sqlite3_free(zC
34514 6f 6d 6d 61 6e 64 29 3b 0a 20 20 72 65 74 75 72  ommand);.  retur
34515 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  n rc;.}..static 
34516 69 6e 74 20 73 71 6c 5f 70 72 65 70 61 72 65 28  int sql_prepare(
34517 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e  sqlite3 *db, con
34518 73 74 20 63 68 61 72 20 2a 7a 44 62 2c 20 63 6f  st char *zDb, co
34519 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
3451a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3451b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
3451c 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 63  stmt **ppStmt, c
3451d 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
3451e 61 74 29 7b 0a 20 20 63 68 61 72 20 2a 7a 43 6f  at){.  char *zCo
3451f 6d 6d 61 6e 64 20 3d 20 73 74 72 69 6e 67 5f 66  mmand = string_f
34520 6f 72 6d 61 74 28 7a 46 6f 72 6d 61 74 2c 20 7a  ormat(zFormat, z
34521 44 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 6e  Db, zName);.  in
34522 74 20 72 63 3b 0a 20 20 46 54 53 54 52 41 43 45  t rc;.  FTSTRACE
34523 28 28 22 46 54 53 33 20 70 72 65 70 61 72 65 3a  (("FTS3 prepare:
34524 20 25 73 5c 6e 22 2c 20 7a 43 6f 6d 6d 61 6e 64   %s\n", zCommand
34525 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  ));.  rc = sqlit
34526 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62  e3_prepare_v2(db
34527 2c 20 7a 43 6f 6d 6d 61 6e 64 2c 20 2d 31 2c 20  , zCommand, -1, 
34528 70 70 53 74 6d 74 2c 20 4e 55 4c 4c 29 3b 0a 20  ppStmt, NULL);. 
34529 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43   sqlite3_free(zC
3452a 6f 6d 6d 61 6e 64 29 3b 0a 20 20 72 65 74 75 72  ommand);.  retur
3452b 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 65 6e 64 20  n rc;.}../* end 
3452c 75 74 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e  utility function
3452d 73 20 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  s */../* Forward
3452e 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 74 79   reference */.ty
3452f 70 65 64 65 66 20 73 74 72 75 63 74 20 66 75 6c  pedef struct ful
34530 6c 74 65 78 74 5f 76 74 61 62 20 66 75 6c 6c 74  ltext_vtab fullt
34531 65 78 74 5f 76 74 61 62 3b 0a 0a 2f 2a 0a 2a 2a  ext_vtab;../*.**
34532 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
34533 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
34534 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 74 72  ructure keeps tr
34535 61 63 6b 20 6f 66 20 67 65 6e 65 72 61 74 65 64  ack of generated
34536 0a 2a 2a 20 6d 61 74 63 68 69 6e 67 2d 77 6f 72  .** matching-wor
34537 64 20 6f 66 66 73 65 74 20 69 6e 66 6f 72 6d 61  d offset informa
34538 74 69 6f 6e 20 61 6e 64 20 73 6e 69 70 70 65 74  tion and snippet
34539 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  s..*/.typedef st
3453a 72 75 63 74 20 53 6e 69 70 70 65 74 20 7b 0a 20  ruct Snippet {. 
3453b 20 69 6e 74 20 6e 4d 61 74 63 68 3b 20 20 20 20   int nMatch;    
3453c 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72   /* Total number
3453d 20 6f 66 20 6d 61 74 63 68 65 73 20 2a 2f 0a 20   of matches */. 
3453e 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20   int nAlloc;    
3453f 20 2f 2a 20 53 70 61 63 65 20 61 6c 6c 6f 63 61   /* Space alloca
34540 74 65 64 20 66 6f 72 20 61 4d 61 74 63 68 5b 5d  ted for aMatch[]
34541 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 6e 69   */.  struct sni
34542 70 70 65 74 4d 61 74 63 68 20 7b 20 2f 2a 20 4f  ppetMatch { /* O
34543 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63  ne entry for eac
34544 68 20 6d 61 74 63 68 69 6e 67 20 74 65 72 6d 20  h matching term 
34545 2a 2f 0a 20 20 20 20 63 68 61 72 20 73 6e 53 74  */.    char snSt
34546 61 74 75 73 3b 20 20 20 20 20 20 20 2f 2a 20 53  atus;       /* S
34547 74 61 74 75 73 20 66 6c 61 67 20 66 6f 72 20 75  tatus flag for u
34548 73 65 20 77 68 69 6c 65 20 63 6f 6e 73 74 72 75  se while constru
34549 63 74 69 6e 67 20 73 6e 69 70 70 65 74 73 20 2a  cting snippets *
3454a 2f 0a 20 20 20 20 73 68 6f 72 74 20 69 6e 74 20  /.    short int 
3454b 69 43 6f 6c 3b 20 20 20 20 20 20 2f 2a 20 54 68  iCol;      /* Th
3454c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 63 6f  e column that co
3454d 6e 74 61 69 6e 73 20 74 68 65 20 6d 61 74 63 68  ntains the match
3454e 20 2a 2f 0a 20 20 20 20 73 68 6f 72 74 20 69 6e   */.    short in
3454f 74 20 69 54 65 72 6d 3b 20 20 20 20 20 2f 2a 20  t iTerm;     /* 
34550 54 68 65 20 69 6e 64 65 78 20 69 6e 20 51 75 65  The index in Que
34551 72 79 2e 70 54 65 72 6d 73 5b 5d 20 6f 66 20 74  ry.pTerms[] of t
34552 68 65 20 6d 61 74 63 68 69 6e 67 20 74 65 72 6d  he matching term
34553 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 54 6f 6b   */.    int iTok
34554 65 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  en;          /* 
34555 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  The index of the
34556 20 6d 61 74 63 68 69 6e 67 20 64 6f 63 75 6d 65   matching docume
34557 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20  nt token */.    
34558 73 68 6f 72 74 20 69 6e 74 20 6e 42 79 74 65 3b  short int nByte;
34559 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
3455a 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 74  f bytes in the t
3455b 65 72 6d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  erm */.    int i
3455c 53 74 61 72 74 3b 20 20 20 20 20 20 20 20 20 20  Start;          
3455d 2f 2a 20 54 68 65 20 6f 66 66 73 65 74 20 74 6f  /* The offset to
3455e 20 74 68 65 20 66 69 72 73 74 20 63 68 61 72 61   the first chara
3455f 63 74 65 72 20 6f 66 20 74 68 65 20 74 65 72 6d  cter of the term
34560 20 2a 2f 0a 20 20 7d 20 2a 61 4d 61 74 63 68 3b   */.  } *aMatch;
34561 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 73 20        /* Points 
34562 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65  to space obtaine
34563 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 2a 2f  d from malloc */
34564 0a 20 20 63 68 61 72 20 2a 7a 4f 66 66 73 65 74  .  char *zOffset
34565 3b 20 20 2f 2a 20 54 65 78 74 20 72 65 6e 64 65  ;  /* Text rende
34566 72 69 6e 67 20 6f 66 20 61 4d 61 74 63 68 5b 5d  ring of aMatch[]
34567 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 66 66 73 65   */.  int nOffse
34568 74 3b 20 20 20 20 2f 2a 20 73 74 72 6c 65 6e 28  t;    /* strlen(
34569 7a 4f 66 66 73 65 74 29 20 2a 2f 0a 20 20 63 68  zOffset) */.  ch
3456a 61 72 20 2a 7a 53 6e 69 70 70 65 74 3b 20 2f 2a  ar *zSnippet; /*
3456b 20 53 6e 69 70 70 65 74 20 74 65 78 74 20 2a 2f   Snippet text */
3456c 0a 20 20 69 6e 74 20 6e 53 6e 69 70 70 65 74 3b  .  int nSnippet;
3456d 20 20 20 2f 2a 20 73 74 72 6c 65 6e 28 7a 53 6e     /* strlen(zSn
3456e 69 70 70 65 74 29 20 2a 2f 0a 7d 20 53 6e 69 70  ippet) */.} Snip
3456f 70 65 74 3b 0a 0a 0a 74 79 70 65 64 65 66 20 65  pet;...typedef e
34570 6e 75 6d 20 51 75 65 72 79 54 79 70 65 20 7b 0a  num QueryType {.
34571 20 20 51 55 45 52 59 5f 47 45 4e 45 52 49 43 2c    QUERY_GENERIC,
34572 20 20 20 2f 2a 20 74 61 62 6c 65 20 73 63 61 6e     /* table scan
34573 20 2a 2f 0a 20 20 51 55 45 52 59 5f 44 4f 43 49   */.  QUERY_DOCI
34574 44 2c 20 20 20 20 20 2f 2a 20 6c 6f 6f 6b 75 70  D,     /* lookup
34575 20 62 79 20 64 6f 63 69 64 20 2a 2f 0a 20 20 51   by docid */.  Q
34576 55 45 52 59 5f 46 55 4c 4c 54 45 58 54 20 20 20  UERY_FULLTEXT   
34577 2f 2a 20 51 55 45 52 59 5f 46 55 4c 4c 54 45 58  /* QUERY_FULLTEX
34578 54 20 2b 20 5b 69 5d 20 69 73 20 61 20 66 75 6c  T + [i] is a ful
34579 6c 2d 74 65 78 74 20 73 65 61 72 63 68 20 66 6f  l-text search fo
3457a 72 20 63 6f 6c 75 6d 6e 20 69 2a 2f 0a 7d 20 51  r column i*/.} Q
3457b 75 65 72 79 54 79 70 65 3b 0a 0a 74 79 70 65 64  ueryType;..typed
3457c 65 66 20 65 6e 75 6d 20 66 75 6c 6c 74 65 78 74  ef enum fulltext
3457d 5f 73 74 61 74 65 6d 65 6e 74 20 7b 0a 20 20 43  _statement {.  C
3457e 4f 4e 54 45 4e 54 5f 49 4e 53 45 52 54 5f 53 54  ONTENT_INSERT_ST
3457f 4d 54 2c 0a 20 20 43 4f 4e 54 45 4e 54 5f 53 45  MT,.  CONTENT_SE
34580 4c 45 43 54 5f 53 54 4d 54 2c 0a 20 20 43 4f 4e  LECT_STMT,.  CON
34581 54 45 4e 54 5f 55 50 44 41 54 45 5f 53 54 4d 54  TENT_UPDATE_STMT
34582 2c 0a 20 20 43 4f 4e 54 45 4e 54 5f 44 45 4c 45  ,.  CONTENT_DELE
34583 54 45 5f 53 54 4d 54 2c 0a 20 20 43 4f 4e 54 45  TE_STMT,.  CONTE
34584 4e 54 5f 45 58 49 53 54 53 5f 53 54 4d 54 2c 0a  NT_EXISTS_STMT,.
34585 0a 20 20 42 4c 4f 43 4b 5f 49 4e 53 45 52 54 5f  .  BLOCK_INSERT_
34586 53 54 4d 54 2c 0a 20 20 42 4c 4f 43 4b 5f 53 45  STMT,.  BLOCK_SE
34587 4c 45 43 54 5f 53 54 4d 54 2c 0a 20 20 42 4c 4f  LECT_STMT,.  BLO
34588 43 4b 5f 44 45 4c 45 54 45 5f 53 54 4d 54 2c 0a  CK_DELETE_STMT,.
34589 20 20 42 4c 4f 43 4b 5f 44 45 4c 45 54 45 5f 41    BLOCK_DELETE_A
3458a 4c 4c 5f 53 54 4d 54 2c 0a 0a 20 20 53 45 47 44  LL_STMT,..  SEGD
3458b 49 52 5f 4d 41 58 5f 49 4e 44 45 58 5f 53 54 4d  IR_MAX_INDEX_STM
3458c 54 2c 0a 20 20 53 45 47 44 49 52 5f 53 45 54 5f  T,.  SEGDIR_SET_
3458d 53 54 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f 53  STMT,.  SEGDIR_S
3458e 45 4c 45 43 54 5f 4c 45 56 45 4c 5f 53 54 4d 54  ELECT_LEVEL_STMT
3458f 2c 0a 20 20 53 45 47 44 49 52 5f 53 50 41 4e 5f  ,.  SEGDIR_SPAN_
34590 53 54 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f 44  STMT,.  SEGDIR_D
34591 45 4c 45 54 45 5f 53 54 4d 54 2c 0a 20 20 53 45  ELETE_STMT,.  SE
34592 47 44 49 52 5f 53 45 4c 45 43 54 5f 53 45 47 4d  GDIR_SELECT_SEGM
34593 45 4e 54 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44  ENT_STMT,.  SEGD
34594 49 52 5f 53 45 4c 45 43 54 5f 41 4c 4c 5f 53 54  IR_SELECT_ALL_ST
34595 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f 44 45 4c  MT,.  SEGDIR_DEL
34596 45 54 45 5f 41 4c 4c 5f 53 54 4d 54 2c 0a 20 20  ETE_ALL_STMT,.  
34597 53 45 47 44 49 52 5f 43 4f 55 4e 54 5f 53 54 4d  SEGDIR_COUNT_STM
34598 54 2c 0a 0a 20 20 4d 41 58 5f 53 54 4d 54 20 20  T,..  MAX_STMT  
34599 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3459a 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 61 74 20     /* Always at 
3459b 65 6e 64 21 20 2a 2f 0a 7d 20 66 75 6c 6c 74 65  end! */.} fullte
3459c 78 74 5f 73 74 61 74 65 6d 65 6e 74 3b 0a 0a 2f  xt_statement;../
3459d 2a 20 54 68 65 73 65 20 6d 75 73 74 20 65 78 61  * These must exa
3459e 63 74 6c 79 20 6d 61 74 63 68 20 74 68 65 20 65  ctly match the e
3459f 6e 75 6d 20 61 62 6f 76 65 2e 20 2a 2f 0a 2f 2a  num above. */./*
345a0 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20 49 73   TODO(shess): Is
345a1 20 74 68 65 72 65 20 73 6f 6d 65 20 72 69 73 6b   there some risk
345a2 20 74 68 61 74 20 61 20 73 74 61 74 65 6d 65 6e   that a statemen
345a3 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 69  t will be used i
345a4 6e 20 74 77 6f 0a 2a 2a 20 63 75 72 73 6f 72 73  n two.** cursors
345a5 20 61 74 20 6f 6e 63 65 2c 20 65 2e 67 2e 20 20   at once, e.g.  
345a6 69 66 20 61 20 71 75 65 72 79 20 6a 6f 69 6e 73  if a query joins
345a7 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
345a8 20 74 6f 20 69 74 73 65 6c 66 3f 0a 2a 2a 20 49   to itself?.** I
345a9 66 20 73 6f 20 70 65 72 68 61 70 73 20 77 65 20  f so perhaps we 
345aa 73 68 6f 75 6c 64 20 6d 6f 76 65 20 73 6f 6d 65  should move some
345ab 20 6f 66 20 74 68 65 73 65 20 74 6f 20 74 68 65   of these to the
345ac 20 63 75 72 73 6f 72 20 6f 62 6a 65 63 74 2e 0a   cursor object..
345ad 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
345ae 63 68 61 72 20 2a 63 6f 6e 73 74 20 66 75 6c 6c  char *const full
345af 74 65 78 74 5f 7a 53 74 61 74 65 6d 65 6e 74 5b  text_zStatement[
345b0 4d 41 58 5f 53 54 4d 54 5d 20 3d 20 7b 0a 20 20  MAX_STMT] = {.  
345b1 2f 2a 20 43 4f 4e 54 45 4e 54 5f 49 4e 53 45 52  /* CONTENT_INSER
345b2 54 20 2a 2f 20 4e 55 4c 4c 2c 20 20 2f 2a 20 67  T */ NULL,  /* g
345b3 65 6e 65 72 61 74 65 64 20 69 6e 20 63 6f 6e 74  enerated in cont
345b4 65 6e 74 49 6e 73 65 72 74 53 74 61 74 65 6d 65  entInsertStateme
345b5 6e 74 28 29 20 2a 2f 0a 20 20 2f 2a 20 43 4f 4e  nt() */.  /* CON
345b6 54 45 4e 54 5f 53 45 4c 45 43 54 20 2a 2f 20 4e  TENT_SELECT */ N
345b7 55 4c 4c 2c 20 20 2f 2a 20 67 65 6e 65 72 61 74  ULL,  /* generat
345b8 65 64 20 69 6e 20 63 6f 6e 74 65 6e 74 53 65 6c  ed in contentSel
345b9 65 63 74 53 74 61 74 65 6d 65 6e 74 28 29 20 2a  ectStatement() *
345ba 2f 0a 20 20 2f 2a 20 43 4f 4e 54 45 4e 54 5f 55  /.  /* CONTENT_U
345bb 50 44 41 54 45 20 2a 2f 20 4e 55 4c 4c 2c 20 20  PDATE */ NULL,  
345bc 2f 2a 20 67 65 6e 65 72 61 74 65 64 20 69 6e 20  /* generated in 
345bd 63 6f 6e 74 65 6e 74 55 70 64 61 74 65 53 74 61  contentUpdateSta
345be 74 65 6d 65 6e 74 28 29 20 2a 2f 0a 20 20 2f 2a  tement() */.  /*
345bf 20 43 4f 4e 54 45 4e 54 5f 44 45 4c 45 54 45 20   CONTENT_DELETE 
345c0 2a 2f 20 22 64 65 6c 65 74 65 20 66 72 6f 6d 20  */ "delete from 
345c1 25 5f 63 6f 6e 74 65 6e 74 20 77 68 65 72 65 20  %_content where 
345c2 64 6f 63 69 64 20 3d 20 3f 22 2c 0a 20 20 2f 2a  docid = ?",.  /*
345c3 20 43 4f 4e 54 45 4e 54 5f 45 58 49 53 54 53 20   CONTENT_EXISTS 
345c4 2a 2f 20 22 73 65 6c 65 63 74 20 64 6f 63 69 64  */ "select docid
345c5 20 66 72 6f 6d 20 25 5f 63 6f 6e 74 65 6e 74 20   from %_content 
345c6 6c 69 6d 69 74 20 31 22 2c 0a 0a 20 20 2f 2a 20  limit 1",..  /* 
345c7 42 4c 4f 43 4b 5f 49 4e 53 45 52 54 20 2a 2f 0a  BLOCK_INSERT */.
345c8 20 20 22 69 6e 73 65 72 74 20 69 6e 74 6f 20 25    "insert into %
345c9 5f 73 65 67 6d 65 6e 74 73 20 28 62 6c 6f 63 6b  _segments (block
345ca 69 64 2c 20 62 6c 6f 63 6b 29 20 76 61 6c 75 65  id, block) value
345cb 73 20 28 6e 75 6c 6c 2c 20 3f 29 22 2c 0a 20 20  s (null, ?)",.  
345cc 2f 2a 20 42 4c 4f 43 4b 5f 53 45 4c 45 43 54 20  /* BLOCK_SELECT 
345cd 2a 2f 20 22 73 65 6c 65 63 74 20 62 6c 6f 63 6b  */ "select block
345ce 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e 74 73   from %_segments
345cf 20 77 68 65 72 65 20 62 6c 6f 63 6b 69 64 20 3d   where blockid =
345d0 20 3f 22 2c 0a 20 20 2f 2a 20 42 4c 4f 43 4b 5f   ?",.  /* BLOCK_
345d1 44 45 4c 45 54 45 20 2a 2f 20 22 64 65 6c 65 74  DELETE */ "delet
345d2 65 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e 74  e from %_segment
345d3 73 20 77 68 65 72 65 20 62 6c 6f 63 6b 69 64 20  s where blockid 
345d4 62 65 74 77 65 65 6e 20 3f 20 61 6e 64 20 3f 22  between ? and ?"
345d5 2c 0a 20 20 2f 2a 20 42 4c 4f 43 4b 5f 44 45 4c  ,.  /* BLOCK_DEL
345d6 45 54 45 5f 41 4c 4c 20 2a 2f 20 22 64 65 6c 65  ETE_ALL */ "dele
345d7 74 65 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e  te from %_segmen
345d8 74 73 22 2c 0a 0a 20 20 2f 2a 20 53 45 47 44 49  ts",..  /* SEGDI
345d9 52 5f 4d 41 58 5f 49 4e 44 45 58 20 2a 2f 20 22  R_MAX_INDEX */ "
345da 73 65 6c 65 63 74 20 6d 61 78 28 69 64 78 29 20  select max(idx) 
345db 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 20 77 68  from %_segdir wh
345dc 65 72 65 20 6c 65 76 65 6c 20 3d 20 3f 22 2c 0a  ere level = ?",.
345dd 20 20 2f 2a 20 53 45 47 44 49 52 5f 53 45 54 20    /* SEGDIR_SET 
345de 2a 2f 20 22 69 6e 73 65 72 74 20 69 6e 74 6f 20  */ "insert into 
345df 25 5f 73 65 67 64 69 72 20 76 61 6c 75 65 73 20  %_segdir values 
345e0 28 3f 2c 20 3f 2c 20 3f 2c 20 3f 2c 20 3f 2c 20  (?, ?, ?, ?, ?, 
345e1 3f 29 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52  ?)",.  /* SEGDIR
345e2 5f 53 45 4c 45 43 54 5f 4c 45 56 45 4c 20 2a 2f  _SELECT_LEVEL */
345e3 0a 20 20 22 73 65 6c 65 63 74 20 73 74 61 72 74  .  "select start
345e4 5f 62 6c 6f 63 6b 2c 20 6c 65 61 76 65 73 5f 65  _block, leaves_e
345e5 6e 64 5f 62 6c 6f 63 6b 2c 20 72 6f 6f 74 20 66  nd_block, root f
345e6 72 6f 6d 20 25 5f 73 65 67 64 69 72 20 22 0a 20  rom %_segdir ". 
345e7 20 22 20 77 68 65 72 65 20 6c 65 76 65 6c 20 3d   " where level =
345e8 20 3f 20 6f 72 64 65 72 20 62 79 20 69 64 78 22   ? order by idx"
345e9 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f 53 50  ,.  /* SEGDIR_SP
345ea 41 4e 20 2a 2f 0a 20 20 22 73 65 6c 65 63 74 20  AN */.  "select 
345eb 6d 69 6e 28 73 74 61 72 74 5f 62 6c 6f 63 6b 29  min(start_block)
345ec 2c 20 6d 61 78 28 65 6e 64 5f 62 6c 6f 63 6b 29  , max(end_block)
345ed 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 20 22   from %_segdir "
345ee 0a 20 20 22 20 77 68 65 72 65 20 6c 65 76 65 6c  .  " where level
345ef 20 3d 20 3f 20 61 6e 64 20 73 74 61 72 74 5f 62   = ? and start_b
345f0 6c 6f 63 6b 20 3c 3e 20 30 22 2c 0a 20 20 2f 2a  lock <> 0",.  /*
345f1 20 53 45 47 44 49 52 5f 44 45 4c 45 54 45 20 2a   SEGDIR_DELETE *
345f2 2f 20 22 64 65 6c 65 74 65 20 66 72 6f 6d 20 25  / "delete from %
345f3 5f 73 65 67 64 69 72 20 77 68 65 72 65 20 6c 65  _segdir where le
345f4 76 65 6c 20 3d 20 3f 22 2c 0a 0a 20 20 2f 2a 20  vel = ?",..  /* 
345f5 4e 4f 54 45 28 73 68 65 73 73 29 3a 20 54 68 65  NOTE(shess): The
345f6 20 66 69 72 73 74 20 74 68 72 65 65 20 72 65 73   first three res
345f7 75 6c 74 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ults of the foll
345f8 6f 77 69 6e 67 20 74 77 6f 0a 20 20 2a 2a 20 73  owing two.  ** s
345f9 74 61 74 65 6d 65 6e 74 73 20 6d 75 73 74 20 6d  tatements must m
345fa 61 74 63 68 2e 0a 20 20 2a 2f 0a 20 20 2f 2a 20  atch..  */.  /* 
345fb 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 53 45  SEGDIR_SELECT_SE
345fc 47 4d 45 4e 54 20 2a 2f 0a 20 20 22 73 65 6c 65  GMENT */.  "sele
345fd 63 74 20 73 74 61 72 74 5f 62 6c 6f 63 6b 2c 20  ct start_block, 
345fe 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b  leaves_end_block
345ff 2c 20 72 6f 6f 74 20 66 72 6f 6d 20 25 5f 73 65  , root from %_se
34600 67 64 69 72 20 22 0a 20 20 22 20 77 68 65 72 65  gdir ".  " where
34601 20 6c 65 76 65 6c 20 3d 20 3f 20 61 6e 64 20 69   level = ? and i
34602 64 78 20 3d 20 3f 22 2c 0a 20 20 2f 2a 20 53 45  dx = ?",.  /* SE
34603 47 44 49 52 5f 53 45 4c 45 43 54 5f 41 4c 4c 20  GDIR_SELECT_ALL 
34604 2a 2f 0a 20 20 22 73 65 6c 65 63 74 20 73 74 61  */.  "select sta
34605 72 74 5f 62 6c 6f 63 6b 2c 20 6c 65 61 76 65 73  rt_block, leaves
34606 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 72 6f 6f 74  _end_block, root
34607 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 20 22   from %_segdir "
34608 0a 20 20 22 20 6f 72 64 65 72 20 62 79 20 6c 65  .  " order by le
34609 76 65 6c 20 64 65 73 63 2c 20 69 64 78 20 61 73  vel desc, idx as
3460a 63 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f  c",.  /* SEGDIR_
3460b 44 45 4c 45 54 45 5f 41 4c 4c 20 2a 2f 20 22 64  DELETE_ALL */ "d
3460c 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f 73 65 67  elete from %_seg
3460d 64 69 72 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49  dir",.  /* SEGDI
3460e 52 5f 43 4f 55 4e 54 20 2a 2f 20 22 73 65 6c 65  R_COUNT */ "sele
3460f 63 74 20 63 6f 75 6e 74 28 2a 29 2c 20 69 66 6e  ct count(*), ifn
34610 75 6c 6c 28 6d 61 78 28 6c 65 76 65 6c 29 2c 30  ull(max(level),0
34611 29 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 22  ) from %_segdir"
34612 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f  ,.};../*.** A co
34613 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 61 20 66 75  nnection to a fu
34614 6c 6c 74 65 78 74 20 69 6e 64 65 78 20 69 73 20  lltext index is 
34615 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
34616 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  he following.** 
34617 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20  structure.  The 
34618 78 43 72 65 61 74 65 20 61 6e 64 20 78 43 6f 6e  xCreate and xCon
34619 6e 65 63 74 20 6d 65 74 68 6f 64 73 20 63 72 65  nect methods cre
3461a 61 74 65 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a  ate an instance.
3461b 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ** of this struc
3461c 74 75 72 65 20 61 6e 64 20 78 44 65 73 74 72 6f  ture and xDestro
3461d 79 20 61 6e 64 20 78 44 69 73 63 6f 6e 6e 65 63  y and xDisconnec
3461e 74 20 66 72 65 65 20 74 68 61 74 20 69 6e 73 74  t free that inst
3461f 61 6e 63 65 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68  ance..** All oth
34620 65 72 20 6d 65 74 68 6f 64 73 20 72 65 63 65 69  er methods recei
34621 76 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ve a pointer to 
34622 74 68 65 20 73 74 72 75 63 74 75 72 65 20 61 73  the structure as
34623 20 6f 6e 65 20 6f 66 20 74 68 65 69 72 0a 2a 2a   one of their.**
34624 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 73   arguments..*/.s
34625 74 72 75 63 74 20 66 75 6c 6c 74 65 78 74 5f 76  truct fulltext_v
34626 74 61 62 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  tab {.  sqlite3_
34627 76 74 61 62 20 62 61 73 65 3b 20 20 20 20 20 20  vtab base;      
34628 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65           /* Base
34629 20 63 6c 61 73 73 20 75 73 65 64 20 62 79 20 53   class used by S
3462a 51 4c 69 74 65 20 63 6f 72 65 20 2a 2f 0a 20 20  QLite core */.  
3462b 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
3462c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3462d 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
3462e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
3462f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
34630 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
34631 20 20 2f 2a 20 6c 6f 67 69 63 61 6c 20 64 61 74    /* logical dat
34632 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  abase name */.  
34633 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
34634 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
34635 20 2f 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c   /* virtual tabl
34636 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20  e name */.  int 
34637 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20  nColumn;        
34638 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
34639 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
3463a 73 20 69 6e 20 76 69 72 74 75 61 6c 20 74 61 62  s in virtual tab
3463b 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61  le */.  char **a
3463c 7a 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20  zColumn;        
3463d 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6c 75           /* colu
3463e 6d 6e 20 6e 61 6d 65 73 2e 20 20 6d 61 6c 6c 6f  mn names.  mallo
3463f 63 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  ced */.  char **
34640 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 3b  azContentColumn;
34641 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6c            /* col
34642 75 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 63 6f 6e  umn names in con
34643 74 65 6e 74 20 74 61 62 6c 65 3b 20 6d 61 6c 6c  tent table; mall
34644 6f 63 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65  oced */.  sqlite
34645 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f  3_tokenizer *pTo
34646 6b 65 6e 69 7a 65 72 3b 20 20 20 2f 2a 20 74 6f  kenizer;   /* to
34647 6b 65 6e 69 7a 65 72 20 66 6f 72 20 69 6e 73 65  kenizer for inse
34648 72 74 73 20 61 6e 64 20 71 75 65 72 69 65 73 20  rts and queries 
34649 2a 2f 0a 0a 20 20 2f 2a 20 50 72 65 63 6f 6d 70  */..  /* Precomp
3464a 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20  iled statements 
3464b 77 68 69 63 68 20 77 65 20 6b 65 65 70 20 61 73  which we keep as
3464c 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 74 61 62   long as the tab
3464d 6c 65 20 69 73 0a 20 20 2a 2a 20 6f 70 65 6e 2e  le is.  ** open.
3464e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  .  */.  sqlite3_
3464f 73 74 6d 74 20 2a 70 46 75 6c 6c 74 65 78 74 53  stmt *pFulltextS
34650 74 61 74 65 6d 65 6e 74 73 5b 4d 41 58 5f 53 54  tatements[MAX_ST
34651 4d 54 5d 3b 0a 0a 20 20 2f 2a 20 50 72 65 63 6f  MT];..  /* Preco
34652 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74  mpiled statement
34653 73 20 75 73 65 64 20 66 6f 72 20 73 65 67 6d 65  s used for segme
34654 6e 74 20 6d 65 72 67 65 73 2e 20 20 57 65 20 72  nt merges.  We r
34655 75 6e 20 61 0a 20 20 2a 2a 20 73 65 70 61 72 61  un a.  ** separa
34656 74 65 20 73 65 6c 65 63 74 20 61 63 72 6f 73 73  te select across
34657 20 74 68 65 20 6c 65 61 66 20 6c 65 76 65 6c 20   the leaf level 
34658 6f 66 20 65 61 63 68 20 74 72 65 65 20 62 65 69  of each tree bei
34659 6e 67 20 6d 65 72 67 65 64 2e 0a 20 20 2a 2f 0a  ng merged..  */.
3465a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
3465b 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73  pLeafSelectStmts
3465c 5b 4d 45 52 47 45 5f 43 4f 55 4e 54 5d 3b 0a 20  [MERGE_COUNT];. 
3465d 20 2f 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e   /* The statemen
3465e 74 20 75 73 65 64 20 74 6f 20 70 72 65 70 61 72  t used to prepar
3465f 65 20 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d  e pLeafSelectStm
34660 74 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4c  ts. */.#define L
34661 45 41 46 5f 53 45 4c 45 43 54 20 5c 0a 20 20 22  EAF_SELECT \.  "
34662 73 65 6c 65 63 74 20 62 6c 6f 63 6b 20 66 72 6f  select block fro
34663 6d 20 25 5f 73 65 67 6d 65 6e 74 73 20 77 68 65  m %_segments whe
34664 72 65 20 62 6c 6f 63 6b 69 64 20 62 65 74 77 65  re blockid betwe
34665 65 6e 20 3f 20 61 6e 64 20 3f 20 6f 72 64 65 72  en ? and ? order
34666 20 62 79 20 62 6c 6f 63 6b 69 64 22 0a 0a 20 20   by blockid"..  
34667 2f 2a 20 54 68 65 73 65 20 62 75 66 66 65 72 20  /* These buffer 
34668 70 65 6e 64 69 6e 67 20 69 6e 64 65 78 20 75 70  pending index up
34669 64 61 74 65 73 20 64 75 72 69 6e 67 20 74 72 61  dates during tra
3466a 6e 73 61 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 20  nsactions..  ** 
3466b 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 65 73 74  nPendingData est
3466c 69 6d 61 74 65 73 20 74 68 65 20 6d 65 6d 6f 72  imates the memor
3466d 79 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70 65  y size of the pe
3466e 6e 64 69 6e 67 20 64 61 74 61 2e 20 20 49 74 0a  nding data.  It.
3466f 20 20 2a 2a 20 64 6f 65 73 6e 27 74 20 69 6e 63    ** doesn't inc
34670 6c 75 64 65 20 74 68 65 20 68 61 73 68 2d 62 75  lude the hash-bu
34671 63 6b 65 74 20 6f 76 65 72 68 65 61 64 2c 20 6e  cket overhead, n
34672 6f 72 20 61 6e 79 20 6d 61 6c 6c 6f 63 0a 20 20  or any malloc.  
34673 2a 2a 20 6f 76 65 72 68 65 61 64 2e 20 20 57 68  ** overhead.  Wh
34674 65 6e 20 6e 50 65 6e 64 69 6e 67 44 61 74 61 20  en nPendingData 
34675 65 78 63 65 65 64 73 20 6b 50 65 6e 64 69 6e 67  exceeds kPending
34676 54 68 72 65 73 68 6f 6c 64 2c 20 74 68 65 0a 20  Threshold, the. 
34677 20 2a 2a 20 62 75 66 66 65 72 20 69 73 20 66 6c   ** buffer is fl
34678 75 73 68 65 64 20 65 76 65 6e 20 62 65 66 6f 72  ushed even befor
34679 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  e the transactio
3467a 6e 20 63 6c 6f 73 65 73 2e 0a 20 20 2a 2a 20 70  n closes..  ** p
3467b 65 6e 64 69 6e 67 54 65 72 6d 73 20 73 74 6f 72  endingTerms stor
3467c 65 73 20 74 68 65 20 64 61 74 61 2c 20 61 6e 64  es the data, and
3467d 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69 64 20 77   is only valid w
3467e 68 65 6e 20 6e 50 65 6e 64 69 6e 67 44 61 74 61  hen nPendingData
3467f 0a 20 20 2a 2a 20 69 73 20 3e 3d 30 20 28 6e 50  .  ** is >=0 (nP
34680 65 6e 64 69 6e 67 44 61 74 61 3c 30 20 6d 65 61  endingData<0 mea
34681 6e 73 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20  ns pendingTerms 
34682 68 61 73 20 6e 6f 74 20 62 65 65 6e 0a 20 20 2a  has not been.  *
34683 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 29 2e 20  * initialized). 
34684 20 69 50 72 65 76 44 6f 63 69 64 20 69 73 20 74   iPrevDocid is t
34685 68 65 20 6c 61 73 74 20 64 6f 63 69 64 20 77 72  he last docid wr
34686 69 74 74 65 6e 2c 20 75 73 65 64 20 74 6f 20 6d  itten, used to m
34687 61 6b 65 0a 20 20 2a 2a 20 63 65 72 74 61 69 6e  ake.  ** certain
34688 20 77 65 27 72 65 20 69 6e 73 65 72 74 69 6e 67   we're inserting
34689 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72   in sorted order
3468a 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 65  ..  */.  int nPe
3468b 6e 64 69 6e 67 44 61 74 61 3b 0a 23 64 65 66 69  ndingData;.#defi
3468c 6e 65 20 6b 50 65 6e 64 69 6e 67 54 68 72 65 73  ne kPendingThres
3468d 68 6f 6c 64 20 28 31 2a 31 30 32 34 2a 31 30 32  hold (1*1024*102
3468e 34 29 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  4).  sqlite_int6
3468f 34 20 69 50 72 65 76 44 6f 63 69 64 3b 0a 20 20  4 iPrevDocid;.  
34690 66 74 73 33 48 61 73 68 20 70 65 6e 64 69 6e 67  fts3Hash pending
34691 54 65 72 6d 73 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  Terms;.};../*.**
34692 20 57 68 65 6e 20 74 68 65 20 63 6f 72 65 20 77   When the core w
34693 61 6e 74 73 20 74 6f 20 64 6f 20 61 20 71 75 65  ants to do a que
34694 72 79 2c 20 69 74 20 63 72 65 61 74 65 20 61 20  ry, it create a 
34695 63 75 72 73 6f 72 20 75 73 69 6e 67 20 61 0a 2a  cursor using a.*
34696 2a 20 63 61 6c 6c 20 74 6f 20 78 4f 70 65 6e 2e  * call to xOpen.
34697 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65    This structure
34698 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
34699 6f 66 20 61 20 63 75 72 73 6f 72 2e 20 20 49 74  of a cursor.  It
3469a 0a 2a 2a 20 69 73 20 64 65 73 74 72 6f 79 65 64  .** is destroyed
3469b 20 62 79 20 78 43 6c 6f 73 65 2e 0a 2a 2f 0a 74   by xClose..*/.t
3469c 79 70 65 64 65 66 20 73 74 72 75 63 74 20 66 75  ypedef struct fu
3469d 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 7b 0a  lltext_cursor {.
3469e 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63    sqlite3_vtab_c
3469f 75 72 73 6f 72 20 62 61 73 65 3b 20 20 20 20 20  ursor base;     
346a0 20 20 20 2f 2a 20 42 61 73 65 20 63 6c 61 73 73     /* Base class
346a1 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 20   used by SQLite 
346a2 63 6f 72 65 20 2a 2f 0a 20 20 51 75 65 72 79 54  core */.  QueryT
346a3 79 70 65 20 69 43 75 72 73 6f 72 54 79 70 65 3b  ype iCursorType;
346a4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
346a5 70 79 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6e  py of sqlite3_in
346a6 64 65 78 5f 69 6e 66 6f 2e 69 64 78 4e 75 6d 20  dex_info.idxNum 
346a7 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
346a8 74 20 2a 70 53 74 6d 74 3b 20 20 20 20 20 20 20  t *pStmt;       
346a9 20 20 20 20 20 20 2f 2a 20 50 72 65 70 61 72 65        /* Prepare
346aa 64 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 75  d statement in u
346ab 73 65 20 62 79 20 74 68 65 20 63 75 72 73 6f 72  se by the cursor
346ac 20 2a 2f 0a 20 20 69 6e 74 20 65 6f 66 3b 20 20   */.  int eof;  
346ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
346ae 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
346af 66 20 61 74 20 45 6e 64 20 4f 66 20 52 65 73 75  f at End Of Resu
346b0 6c 74 73 20 2a 2f 0a 20 20 46 74 73 33 45 78 70  lts */.  Fts3Exp
346b1 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20  r *pExpr;       
346b2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
346b3 73 65 64 20 4d 41 54 43 48 20 71 75 65 72 79 20  sed MATCH query 
346b4 73 74 72 69 6e 67 20 2a 2f 0a 20 20 53 6e 69 70  string */.  Snip
346b5 70 65 74 20 73 6e 69 70 70 65 74 3b 20 20 20 20  pet snippet;    
346b6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
346b7 43 61 63 68 65 64 20 73 6e 69 70 70 65 74 20 66  Cached snippet f
346b8 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  or the current r
346b9 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c  ow */.  int iCol
346ba 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  umn;            
346bb 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75           /* Colu
346bc 6d 6e 20 62 65 69 6e 67 20 73 65 61 72 63 68 65  mn being searche
346bd 64 20 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65  d */.  DataBuffe
346be 72 20 72 65 73 75 6c 74 3b 20 20 20 20 20 20 20  r result;       
346bf 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 63 6c 69          /* Docli
346c0 73 74 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20  st results from 
346c1 66 75 6c 6c 74 65 78 74 51 75 65 72 79 20 2a 2f  fulltextQuery */
346c2 0a 20 20 44 4c 52 65 61 64 65 72 20 72 65 61 64  .  DLReader read
346c3 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  er;             
346c4 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 72 65      /* Result re
346c5 61 64 65 72 20 69 66 20 72 65 73 75 6c 74 20 6e  ader if result n
346c6 6f 74 20 65 6d 70 74 79 20 2a 2f 0a 7d 20 66 75  ot empty */.} fu
346c7 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 3b 0a 0a  lltext_cursor;..
346c8 73 74 61 74 69 63 20 66 75 6c 6c 74 65 78 74 5f  static fulltext_
346c9 76 74 61 62 20 2a 63 75 72 73 6f 72 5f 76 74 61  vtab *cursor_vta
346ca 62 28 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f  b(fulltext_curso
346cb 72 20 2a 63 29 7b 0a 20 20 72 65 74 75 72 6e 20  r *c){.  return 
346cc 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
346cd 29 20 63 2d 3e 62 61 73 65 2e 70 56 74 61 62 3b  ) c->base.pVtab;
346ce 0a 7d 0a 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .}..static const
346cf 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
346d0 66 74 73 33 4d 6f 64 75 6c 65 3b 20 20 20 2f 2a  fts3Module;   /*
346d1 20 66 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61   forward declara
346d2 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 20 52 65 74 75  tion */../* Retu
346d3 72 6e 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79  rn a dynamically
346d4 20 67 65 6e 65 72 61 74 65 64 20 73 74 61 74 65   generated state
346d5 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 6d  ment of the form
346d6 0a 20 2a 20 20 20 69 6e 73 65 72 74 20 69 6e 74  . *   insert int
346d7 6f 20 25 5f 63 6f 6e 74 65 6e 74 20 28 64 6f 63  o %_content (doc
346d8 69 64 2c 20 2e 2e 2e 29 20 76 61 6c 75 65 73 20  id, ...) values 
346d9 28 3f 2c 20 2e 2e 2e 29 0a 20 2a 2f 0a 73 74 61  (?, ...). */.sta
346da 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
346db 63 6f 6e 74 65 6e 74 49 6e 73 65 72 74 53 74 61  contentInsertSta
346dc 74 65 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f  tement(fulltext_
346dd 76 74 61 62 20 2a 76 29 7b 0a 20 20 53 74 72 69  vtab *v){.  Stri
346de 6e 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20 69  ngBuffer sb;.  i
346df 6e 74 20 69 3b 0a 0a 20 20 69 6e 69 74 53 74 72  nt i;..  initStr
346e0 69 6e 67 42 75 66 66 65 72 28 26 73 62 29 3b 0a  ingBuffer(&sb);.
346e1 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 69    append(&sb, "i
346e2 6e 73 65 72 74 20 69 6e 74 6f 20 25 5f 63 6f 6e  nsert into %_con
346e3 74 65 6e 74 20 28 64 6f 63 69 64 2c 20 22 29 3b  tent (docid, ");
346e4 0a 20 20 61 70 70 65 6e 64 4c 69 73 74 28 26 73  .  appendList(&s
346e5 62 2c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 76  b, v->nColumn, v
346e6 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d  ->azContentColum
346e7 6e 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 62  n);.  append(&sb
346e8 2c 20 22 29 20 76 61 6c 75 65 73 20 28 3f 22 29  , ") values (?")
346e9 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 76  ;.  for(i=0; i<v
346ea 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 0a  ->nColumn; ++i).
346eb 20 20 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20      append(&sb, 
346ec 22 2c 20 3f 22 29 3b 0a 20 20 61 70 70 65 6e 64  ", ?");.  append
346ed 28 26 73 62 2c 20 22 29 22 29 3b 0a 20 20 72 65  (&sb, ")");.  re
346ee 74 75 72 6e 20 73 74 72 69 6e 67 42 75 66 66 65  turn stringBuffe
346ef 72 44 61 74 61 28 26 73 62 29 3b 0a 7d 0a 0a 2f  rData(&sb);.}../
346f0 2a 20 52 65 74 75 72 6e 20 61 20 64 79 6e 61 6d  * Return a dynam
346f1 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64  ically generated
346f2 20 73 74 61 74 65 6d 65 6e 74 20 6f 66 20 74 68   statement of th
346f3 65 20 66 6f 72 6d 0a 20 2a 20 20 20 73 65 6c 65  e form. *   sele
346f4 63 74 20 3c 63 6f 6e 74 65 6e 74 20 63 6f 6c 75  ct <content colu
346f5 6d 6e 73 3e 20 66 72 6f 6d 20 25 5f 63 6f 6e 74  mns> from %_cont
346f6 65 6e 74 20 77 68 65 72 65 20 64 6f 63 69 64 20  ent where docid 
346f7 3d 20 3f 0a 20 2a 2f 0a 73 74 61 74 69 63 20 63  = ?. */.static c
346f8 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 74 65  onst char *conte
346f9 6e 74 53 65 6c 65 63 74 53 74 61 74 65 6d 65 6e  ntSelectStatemen
346fa 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  t(fulltext_vtab 
346fb 2a 76 29 7b 0a 20 20 53 74 72 69 6e 67 42 75 66  *v){.  StringBuf
346fc 66 65 72 20 73 62 3b 0a 20 20 69 6e 69 74 53 74  fer sb;.  initSt
346fd 72 69 6e 67 42 75 66 66 65 72 28 26 73 62 29 3b  ringBuffer(&sb);
346fe 0a 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22  .  append(&sb, "
346ff 53 45 4c 45 43 54 20 22 29 3b 0a 20 20 61 70 70  SELECT ");.  app
34700 65 6e 64 4c 69 73 74 28 26 73 62 2c 20 76 2d 3e  endList(&sb, v->
34701 6e 43 6f 6c 75 6d 6e 2c 20 76 2d 3e 61 7a 43 6f  nColumn, v->azCo
34702 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20  ntentColumn);.  
34703 61 70 70 65 6e 64 28 26 73 62 2c 20 22 20 46 52  append(&sb, " FR
34704 4f 4d 20 25 5f 63 6f 6e 74 65 6e 74 20 57 48 45  OM %_content WHE
34705 52 45 20 64 6f 63 69 64 20 3d 20 3f 22 29 3b 0a  RE docid = ?");.
34706 20 20 72 65 74 75 72 6e 20 73 74 72 69 6e 67 42    return stringB
34707 75 66 66 65 72 44 61 74 61 28 26 73 62 29 3b 0a  ufferData(&sb);.
34708 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 61 20 64  }../* Return a d
34709 79 6e 61 6d 69 63 61 6c 6c 79 20 67 65 6e 65 72  ynamically gener
3470a 61 74 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6f  ated statement o
3470b 66 20 74 68 65 20 66 6f 72 6d 0a 20 2a 20 20 20  f the form. *   
3470c 75 70 64 61 74 65 20 25 5f 63 6f 6e 74 65 6e 74  update %_content
3470d 20 73 65 74 20 5b 63 6f 6c 5f 30 5d 20 3d 20 3f   set [col_0] = ?
3470e 2c 20 5b 63 6f 6c 5f 31 5d 20 3d 20 3f 2c 20 2e  , [col_1] = ?, .
3470f 2e 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20  ... *           
34710 20 20 20 20 20 20 20 20 20 77 68 65 72 65 20 64           where d
34711 6f 63 69 64 20 3d 20 3f 0a 20 2a 2f 0a 73 74 61  ocid = ?. */.sta
34712 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
34713 63 6f 6e 74 65 6e 74 55 70 64 61 74 65 53 74 61  contentUpdateSta
34714 74 65 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f  tement(fulltext_
34715 76 74 61 62 20 2a 76 29 7b 0a 20 20 53 74 72 69  vtab *v){.  Stri
34716 6e 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20 69  ngBuffer sb;.  i
34717 6e 74 20 69 3b 0a 0a 20 20 69 6e 69 74 53 74 72  nt i;..  initStr
34718 69 6e 67 42 75 66 66 65 72 28 26 73 62 29 3b 0a  ingBuffer(&sb);.
34719 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 75    append(&sb, "u
3471a 70 64 61 74 65 20 25 5f 63 6f 6e 74 65 6e 74 20  pdate %_content 
3471b 73 65 74 20 22 29 3b 0a 20 20 66 6f 72 28 69 3d  set ");.  for(i=
3471c 30 3b 20 69 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b  0; i<v->nColumn;
3471d 20 2b 2b 69 29 20 7b 0a 20 20 20 20 69 66 28 20   ++i) {.    if( 
3471e 69 3e 30 20 29 7b 0a 20 20 20 20 20 20 61 70 70  i>0 ){.      app
3471f 65 6e 64 28 26 73 62 2c 20 22 2c 20 22 29 3b 0a  end(&sb, ", ");.
34720 20 20 20 20 7d 0a 20 20 20 20 61 70 70 65 6e 64      }.    append
34721 28 26 73 62 2c 20 76 2d 3e 61 7a 43 6f 6e 74 65  (&sb, v->azConte
34722 6e 74 43 6f 6c 75 6d 6e 5b 69 5d 29 3b 0a 20 20  ntColumn[i]);.  
34723 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 20    append(&sb, " 
34724 3d 20 3f 22 29 3b 0a 20 20 7d 0a 20 20 61 70 70  = ?");.  }.  app
34725 65 6e 64 28 26 73 62 2c 20 22 20 77 68 65 72 65  end(&sb, " where
34726 20 64 6f 63 69 64 20 3d 20 3f 22 29 3b 0a 20 20   docid = ?");.  
34727 72 65 74 75 72 6e 20 73 74 72 69 6e 67 42 75 66  return stringBuf
34728 66 65 72 44 61 74 61 28 26 73 62 29 3b 0a 7d 0a  ferData(&sb);.}.
34729 0a 2f 2a 20 50 75 74 73 20 61 20 66 72 65 73 68  ./* Puts a fresh
3472a 6c 79 2d 70 72 65 70 61 72 65 64 20 73 74 61 74  ly-prepared stat
3472b 65 6d 65 6e 74 20 64 65 74 65 72 6d 69 6e 65 64  ement determined
3472c 20 62 79 20 69 53 74 6d 74 20 69 6e 20 2a 70 70   by iStmt in *pp
3472d 53 74 6d 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20  Stmt..** If the 
3472e 69 6e 64 69 63 61 74 65 64 20 73 74 61 74 65 6d  indicated statem
3472f 65 6e 74 20 68 61 73 20 6e 65 76 65 72 20 62 65  ent has never be
34730 65 6e 20 70 72 65 70 61 72 65 64 2c 20 69 74 20  en prepared, it 
34731 69 73 20 70 72 65 70 61 72 65 64 0a 2a 2a 20 61  is prepared.** a
34732 6e 64 20 63 61 63 68 65 64 2c 20 6f 74 68 65 72  nd cached, other
34733 77 69 73 65 20 74 68 65 20 63 61 63 68 65 64 20  wise the cached 
34734 76 65 72 73 69 6f 6e 20 69 73 20 72 65 73 65 74  version is reset
34735 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
34736 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e  sql_get_statemen
34737 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  t(fulltext_vtab 
34738 2a 76 2c 20 66 75 6c 6c 74 65 78 74 5f 73 74 61  *v, fulltext_sta
34739 74 65 6d 65 6e 74 20 69 53 74 6d 74 2c 0a 20 20  tement iStmt,.  
3473a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3473b 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
3473c 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74  e3_stmt **ppStmt
3473d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 53 74  ){.  assert( iSt
3473e 6d 74 3c 4d 41 58 5f 53 54 4d 54 20 29 3b 0a 20  mt<MAX_STMT );. 
3473f 20 69 66 28 20 76 2d 3e 70 46 75 6c 6c 74 65 78   if( v->pFulltex
34740 74 53 74 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d  tStatements[iStm
34741 74 5d 3d 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  t]==NULL ){.    
34742 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 6d  const char *zStm
34743 74 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20  t;.    int rc;. 
34744 20 20 20 73 77 69 74 63 68 28 20 69 53 74 6d 74     switch( iStmt
34745 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 43   ){.      case C
34746 4f 4e 54 45 4e 54 5f 49 4e 53 45 52 54 5f 53 54  ONTENT_INSERT_ST
34747 4d 54 3a 0a 20 20 20 20 20 20 20 20 7a 53 74 6d  MT:.        zStm
34748 74 20 3d 20 63 6f 6e 74 65 6e 74 49 6e 73 65 72  t = contentInser
34749 74 53 74 61 74 65 6d 65 6e 74 28 76 29 3b 20 62  tStatement(v); b
3474a 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
3474b 20 43 4f 4e 54 45 4e 54 5f 53 45 4c 45 43 54 5f   CONTENT_SELECT_
3474c 53 54 4d 54 3a 0a 20 20 20 20 20 20 20 20 7a 53  STMT:.        zS
3474d 74 6d 74 20 3d 20 63 6f 6e 74 65 6e 74 53 65 6c  tmt = contentSel
3474e 65 63 74 53 74 61 74 65 6d 65 6e 74 28 76 29 3b  ectStatement(v);
3474f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
34750 73 65 20 43 4f 4e 54 45 4e 54 5f 55 50 44 41 54  se CONTENT_UPDAT
34751 45 5f 53 54 4d 54 3a 0a 20 20 20 20 20 20 20 20  E_STMT:.        
34752 7a 53 74 6d 74 20 3d 20 63 6f 6e 74 65 6e 74 55  zStmt = contentU
34753 70 64 61 74 65 53 74 61 74 65 6d 65 6e 74 28 76  pdateStatement(v
34754 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  ); break;.      
34755 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20  default:.       
34756 20 7a 53 74 6d 74 20 3d 20 66 75 6c 6c 74 65 78   zStmt = fulltex
34757 74 5f 7a 53 74 61 74 65 6d 65 6e 74 5b 69 53 74  t_zStatement[iSt
34758 6d 74 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  mt];.    }.    r
34759 63 20 3d 20 73 71 6c 5f 70 72 65 70 61 72 65 28  c = sql_prepare(
3475a 76 2d 3e 64 62 2c 20 76 2d 3e 7a 44 62 2c 20 76  v->db, v->zDb, v
3475b 2d 3e 7a 4e 61 6d 65 2c 20 26 76 2d 3e 70 46 75  ->zName, &v->pFu
3475c 6c 6c 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73  lltextStatements
3475d 5b 69 53 74 6d 74 5d 2c 0a 20 20 20 20 20 20 20  [iStmt],.       
3475e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3475f 20 20 7a 53 74 6d 74 29 3b 0a 20 20 20 20 69 66    zStmt);.    if
34760 28 20 7a 53 74 6d 74 20 21 3d 20 66 75 6c 6c 74  ( zStmt != fullt
34761 65 78 74 5f 7a 53 74 61 74 65 6d 65 6e 74 5b 69  ext_zStatement[i
34762 53 74 6d 74 5d 29 20 73 71 6c 69 74 65 33 5f 66  Stmt]) sqlite3_f
34763 72 65 65 28 28 76 6f 69 64 20 2a 29 20 7a 53 74  ree((void *) zSt
34764 6d 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  mt);.    if( rc!
34765 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
34766 75 72 6e 20 72 63 3b 0a 20 20 7d 20 65 6c 73 65  urn rc;.  } else
34767 20 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20   {.    int rc = 
34768 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 76 2d  sqlite3_reset(v-
34769 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d  >pFulltextStatem
3476a 65 6e 74 73 5b 69 53 74 6d 74 5d 29 3b 0a 20 20  ents[iStmt]);.  
3476b 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
3476c 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
3476d 0a 20 20 7d 0a 0a 20 20 2a 70 70 53 74 6d 74 20  .  }..  *ppStmt 
3476e 3d 20 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74  = v->pFulltextSt
3476f 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d 74 5d 3b  atements[iStmt];
34770 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
34771 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b 65 20  _OK;.}../* Like 
34772 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2c 20  sqlite3_step(), 
34773 62 75 74 20 63 6f 6e 76 65 72 74 20 53 51 4c 49  but convert SQLI
34774 54 45 5f 44 4f 4e 45 20 74 6f 20 53 51 4c 49 54  TE_DONE to SQLIT
34775 45 5f 4f 4b 20 61 6e 64 0a 2a 2a 20 53 51 4c 49  E_OK and.** SQLI
34776 54 45 5f 52 4f 57 20 74 6f 20 53 51 4c 49 54 45  TE_ROW to SQLITE
34777 5f 45 52 52 4f 52 2e 20 20 55 73 65 66 75 6c 20  _ERROR.  Useful 
34778 66 6f 72 20 73 74 61 74 65 6d 65 6e 74 73 20 6c  for statements l
34779 69 6b 65 20 55 50 44 41 54 45 2c 0a 2a 2a 20 77  ike UPDATE,.** w
3477a 68 65 72 65 20 77 65 20 65 78 70 65 63 74 20 6e  here we expect n
3477b 6f 20 72 65 73 75 6c 74 73 2e 0a 2a 2f 0a 73 74  o results..*/.st
3477c 61 74 69 63 20 69 6e 74 20 73 71 6c 5f 73 69 6e  atic int sql_sin
3477d 67 6c 65 5f 73 74 65 70 28 73 71 6c 69 74 65 33  gle_step(sqlite3
3477e 5f 73 74 6d 74 20 2a 73 29 7b 0a 20 20 69 6e 74  _stmt *s){.  int
3477f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74   rc = sqlite3_st
34780 65 70 28 73 29 3b 0a 20 20 72 65 74 75 72 6e 20  ep(s);.  return 
34781 28 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45  (rc==SQLITE_DONE
34782 29 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20  ) ? SQLITE_OK : 
34783 72 63 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b 65 20 73  rc;.}../* Like s
34784 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
34785 28 29 2c 20 62 75 74 20 66 6f 72 20 73 70 65 63  (), but for spec
34786 69 61 6c 20 72 65 70 6c 69 63 61 74 65 64 20 4c  ial replicated L
34787 45 41 46 5f 53 45 4c 45 43 54 0a 2a 2a 20 73 74  EAF_SELECT.** st
34788 61 74 65 6d 65 6e 74 73 2e 20 20 69 64 78 20 2d  atements.  idx -
34789 31 20 69 73 20 61 20 73 70 65 63 69 61 6c 20 63  1 is a special c
3478a 61 73 65 20 66 6f 72 20 61 6e 20 75 6e 63 61 63  ase for an uncac
3478b 68 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 0a 2a  hed version of.*
3478c 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  * the statement 
3478d 28 75 73 65 64 20 69 6e 20 74 68 65 20 6f 70 74  (used in the opt
3478e 69 6d 69 7a 65 20 69 6d 70 6c 65 6d 65 6e 74 61  imize implementa
3478f 74 69 6f 6e 29 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44  tion)..*/./* TOD
34790 4f 28 73 68 65 73 73 29 20 57 72 69 74 65 20 76  O(shess) Write v
34791 65 72 73 69 6f 6e 20 66 6f 72 20 67 65 6e 65 72  ersion for gener
34792 69 63 20 73 74 61 74 65 6d 65 6e 74 73 20 61 6e  ic statements an
34793 64 20 74 68 65 6e 20 73 68 61 72 65 0a 2a 2a 20  d then share.** 
34794 74 68 61 74 20 62 65 74 77 65 65 6e 20 74 68 65  that between the
34795 20 63 61 63 68 65 64 2d 73 74 61 74 65 6d 65 6e   cached-statemen
34796 74 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a  t functions..*/.
34797 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 5f 67  static int sql_g
34798 65 74 5f 6c 65 61 66 5f 73 74 61 74 65 6d 65 6e  et_leaf_statemen
34799 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  t(fulltext_vtab 
3479a 2a 76 2c 20 69 6e 74 20 69 64 78 2c 0a 20 20 20  *v, int idx,.   
3479b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3479c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
3479d 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70  qlite3_stmt **pp
3479e 53 74 6d 74 29 7b 0a 20 20 61 73 73 65 72 74 28  Stmt){.  assert(
3479f 20 69 64 78 3e 3d 2d 31 20 26 26 20 69 64 78 3c   idx>=-1 && idx<
347a0 4d 45 52 47 45 5f 43 4f 55 4e 54 20 29 3b 0a 20  MERGE_COUNT );. 
347a1 20 69 66 28 20 69 64 78 3d 3d 2d 31 20 29 7b 0a   if( idx==-1 ){.
347a2 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 70      return sql_p
347a3 72 65 70 61 72 65 28 76 2d 3e 64 62 2c 20 76 2d  repare(v->db, v-
347a4 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61 6d 65 2c 20  >zDb, v->zName, 
347a5 70 70 53 74 6d 74 2c 20 4c 45 41 46 5f 53 45 4c  ppStmt, LEAF_SEL
347a6 45 43 54 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ECT);.  }else if
347a7 28 20 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74  ( v->pLeafSelect
347a8 53 74 6d 74 73 5b 69 64 78 5d 3d 3d 4e 55 4c 4c  Stmts[idx]==NULL
347a9 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d   ){.    int rc =
347aa 20 73 71 6c 5f 70 72 65 70 61 72 65 28 76 2d 3e   sql_prepare(v->
347ab 64 62 2c 20 76 2d 3e 7a 44 62 2c 20 76 2d 3e 7a  db, v->zDb, v->z
347ac 4e 61 6d 65 2c 20 26 76 2d 3e 70 4c 65 61 66 53  Name, &v->pLeafS
347ad 65 6c 65 63 74 53 74 6d 74 73 5b 69 64 78 5d 2c  electStmts[idx],
347ae 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
347af 20 20 20 20 20 20 20 20 20 20 4c 45 41 46 5f 53            LEAF_S
347b0 45 4c 45 43 54 29 3b 0a 20 20 20 20 69 66 28 20  ELECT);.    if( 
347b1 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
347b2 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 65 6c  return rc;.  }el
347b3 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d  se{.    int rc =
347b4 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 76   sqlite3_reset(v
347b5 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d  ->pLeafSelectStm
347b6 74 73 5b 69 64 78 5d 29 3b 0a 20 20 20 20 69 66  ts[idx]);.    if
347b7 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
347b8 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d  ) return rc;.  }
347b9 0a 0a 20 20 2a 70 70 53 74 6d 74 20 3d 20 76 2d  ..  *ppStmt = v-
347ba 3e 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d 74  >pLeafSelectStmt
347bb 73 5b 69 64 78 5d 3b 0a 20 20 72 65 74 75 72 6e  s[idx];.  return
347bc 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
347bd 2a 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 25 5f  * insert into %_
347be 63 6f 6e 74 65 6e 74 20 28 64 6f 63 69 64 2c 20  content (docid, 
347bf 2e 2e 2e 29 20 76 61 6c 75 65 73 20 28 5b 64 6f  ...) values ([do
347c0 63 69 64 5d 2c 20 5b 70 56 61 6c 75 65 73 5d 29  cid], [pValues])
347c1 0a 2a 2a 20 49 66 20 74 68 65 20 64 6f 63 69 64  .** If the docid
347c2 20 63 6f 6e 74 61 69 6e 73 20 53 51 4c 20 4e 55   contains SQL NU
347c3 4c 4c 2c 20 74 68 65 6e 20 61 20 75 6e 69 71 75  LL, then a uniqu
347c4 65 20 64 6f 63 69 64 20 77 69 6c 6c 20 62 65 0a  e docid will be.
347c5 2a 2a 20 67 65 6e 65 72 61 74 65 64 2e 0a 2a 2f  ** generated..*/
347c6 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6e 74  .static int cont
347c7 65 6e 74 5f 69 6e 73 65 72 74 28 66 75 6c 6c 74  ent_insert(fullt
347c8 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c  ext_vtab *v, sql
347c9 69 74 65 33 5f 76 61 6c 75 65 20 2a 64 6f 63 69  ite3_value *doci
347ca 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
347cb 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
347cc 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 56 61  ite3_value **pVa
347cd 6c 75 65 73 29 7b 0a 20 20 73 71 6c 69 74 65 33  lues){.  sqlite3
347ce 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20  _stmt *s;.  int 
347cf 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71  i;.  int rc = sq
347d0 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28  l_get_statement(
347d1 76 2c 20 43 4f 4e 54 45 4e 54 5f 49 4e 53 45 52  v, CONTENT_INSER
347d2 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69  T_STMT, &s);.  i
347d3 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
347d4 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
347d5 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
347d6 6e 64 5f 76 61 6c 75 65 28 73 2c 20 31 2c 20 64  nd_value(s, 1, d
347d7 6f 63 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21  ocid);.  if( rc!
347d8 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
347d9 75 72 6e 20 72 63 3b 0a 0a 20 20 66 6f 72 28 69  urn rc;..  for(i
347da 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e  =0; i<v->nColumn
347db 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 72 63 20 3d  ; ++i){.    rc =
347dc 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61   sqlite3_bind_va
347dd 6c 75 65 28 73 2c 20 32 2b 69 2c 20 70 56 61 6c  lue(s, 2+i, pVal
347de 75 65 73 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28  ues[i]);.    if(
347df 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
347e0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
347e1 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 73 69  .  return sql_si
347e2 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a  ngle_step(s);.}.
347e3 0a 2f 2a 20 75 70 64 61 74 65 20 25 5f 63 6f 6e  ./* update %_con
347e4 74 65 6e 74 20 73 65 74 20 63 6f 6c 30 20 3d 20  tent set col0 = 
347e5 70 56 61 6c 75 65 73 5b 30 5d 2c 20 63 6f 6c 31  pValues[0], col1
347e6 20 3d 20 70 56 61 6c 75 65 73 5b 31 5d 2c 20 2e   = pValues[1], .
347e7 2e 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20  ... *           
347e8 20 20 20 20 20 20 20 77 68 65 72 65 20 64 6f 63         where doc
347e9 69 64 20 3d 20 5b 69 44 6f 63 69 64 5d 20 2a 2f  id = [iDocid] */
347ea 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6e 74  .static int cont
347eb 65 6e 74 5f 75 70 64 61 74 65 28 66 75 6c 6c 74  ent_update(fullt
347ec 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c  ext_vtab *v, sql
347ed 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 56 61  ite3_value **pVa
347ee 6c 75 65 73 2c 0a 20 20 20 20 20 20 20 20 20 20  lues,.          
347ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
347f0 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f  sqlite_int64 iDo
347f1 63 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  cid){.  sqlite3_
347f2 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 69  stmt *s;.  int i
347f3 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c  ;.  int rc = sql
347f4 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
347f5 2c 20 43 4f 4e 54 45 4e 54 5f 55 50 44 41 54 45  , CONTENT_UPDATE
347f6 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66  _STMT, &s);.  if
347f7 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
347f8 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
347f9 66 6f 72 28 69 3d 30 3b 20 69 3c 76 2d 3e 6e 43  for(i=0; i<v->nC
347fa 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 7b 0a 20 20 20  olumn; ++i){.   
347fb 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
347fc 6e 64 5f 76 61 6c 75 65 28 73 2c 20 31 2b 69 2c  nd_value(s, 1+i,
347fd 20 70 56 61 6c 75 65 73 5b 69 5d 29 3b 0a 20 20   pValues[i]);.  
347fe 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
347ff 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
34800 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c  .  }..  rc = sql
34801 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
34802 73 2c 20 31 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c  s, 1+v->nColumn,
34803 20 69 44 6f 63 69 64 29 3b 0a 20 20 69 66 28 20   iDocid);.  if( 
34804 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
34805 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 65  return rc;..  re
34806 74 75 72 6e 20 73 71 6c 5f 73 69 6e 67 6c 65 5f  turn sql_single_
34807 73 74 65 70 28 73 29 3b 0a 7d 0a 0a 73 74 61 74  step(s);.}..stat
34808 69 63 20 76 6f 69 64 20 66 72 65 65 53 74 72 69  ic void freeStri
34809 6e 67 41 72 72 61 79 28 69 6e 74 20 6e 53 74 72  ngArray(int nStr
3480a 69 6e 67 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  ing, const char 
3480b 2a 2a 70 53 74 72 69 6e 67 29 7b 0a 20 20 69 6e  **pString){.  in
3480c 74 20 69 3b 0a 0a 20 20 66 6f 72 20 28 69 3d 30  t i;..  for (i=0
3480d 20 3b 20 69 20 3c 20 6e 53 74 72 69 6e 67 20 3b   ; i < nString ;
3480e 20 2b 2b 69 29 20 7b 0a 20 20 20 20 69 66 28 20   ++i) {.    if( 
3480f 70 53 74 72 69 6e 67 5b 69 5d 21 3d 4e 55 4c 4c  pString[i]!=NULL
34810 20 29 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28   ) sqlite3_free(
34811 28 76 6f 69 64 20 2a 29 20 70 53 74 72 69 6e 67  (void *) pString
34812 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  [i]);.  }.  sqli
34813 74 65 33 5f 66 72 65 65 28 28 76 6f 69 64 20 2a  te3_free((void *
34814 29 20 70 53 74 72 69 6e 67 29 3b 0a 7d 0a 0a 2f  ) pString);.}../
34815 2a 20 73 65 6c 65 63 74 20 2a 20 66 72 6f 6d 20  * select * from 
34816 25 5f 63 6f 6e 74 65 6e 74 20 77 68 65 72 65 20  %_content where 
34817 64 6f 63 69 64 20 3d 20 5b 69 44 6f 63 69 64 5d  docid = [iDocid]
34818 0a 20 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d  . * The caller m
34819 75 73 74 20 64 65 6c 65 74 65 20 74 68 65 20 72  ust delete the r
3481a 65 74 75 72 6e 65 64 20 61 72 72 61 79 20 61 6e  eturned array an
3481b 64 20 61 6c 6c 20 73 74 72 69 6e 67 73 20 69 6e  d all strings in
3481c 20 69 74 2e 0a 20 2a 20 6e 75 6c 6c 20 66 69 65   it.. * null fie
3481d 6c 64 73 20 77 69 6c 6c 20 62 65 20 4e 55 4c 4c  lds will be NULL
3481e 20 69 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64   in the returned
3481f 20 61 72 72 61 79 2e 0a 20 2a 0a 20 2a 20 54 4f   array.. *. * TO
34820 44 4f 3a 20 50 65 72 68 61 70 73 20 77 65 20 73  DO: Perhaps we s
34821 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 70 6f 69  hould return poi
34822 6e 74 65 72 2f 6c 65 6e 67 74 68 20 73 74 72 69  nter/length stri
34823 6e 67 73 20 68 65 72 65 20 66 6f 72 20 63 6f 6e  ngs here for con
34824 73 69 73 74 65 6e 63 79 0a 20 2a 20 77 69 74 68  sistency. * with
34825 20 6f 74 68 65 72 20 63 6f 64 65 20 77 68 69 63   other code whic
34826 68 20 75 73 65 73 20 70 6f 69 6e 74 65 72 2f 6c  h uses pointer/l
34827 65 6e 67 74 68 2e 20 2a 2f 0a 73 74 61 74 69 63  ength. */.static
34828 20 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 73 65 6c   int content_sel
34829 65 63 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  ect(fulltext_vta
3482a 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74  b *v, sqlite_int
3482b 36 34 20 69 44 6f 63 69 64 2c 0a 20 20 20 20 20  64 iDocid,.     
3482c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3482d 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
3482e 2a 2a 2a 70 56 61 6c 75 65 73 29 7b 0a 20 20 73  ***pValues){.  s
3482f 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a  qlite3_stmt *s;.
34830 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 76    const char **v
34831 61 6c 75 65 73 3b 0a 20 20 69 6e 74 20 69 3b 0a  alues;.  int i;.
34832 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2a 70 56    int rc;..  *pV
34833 61 6c 75 65 73 20 3d 20 4e 55 4c 4c 3b 0a 0a 20  alues = NULL;.. 
34834 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74   rc = sql_get_st
34835 61 74 65 6d 65 6e 74 28 76 2c 20 43 4f 4e 54 45  atement(v, CONTE
34836 4e 54 5f 53 45 4c 45 43 54 5f 53 54 4d 54 2c 20  NT_SELECT_STMT, 
34837 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  &s);.  if( rc!=S
34838 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
34839 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
3483a 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
3483b 28 73 2c 20 31 2c 20 69 44 6f 63 69 64 29 3b 0a  (s, 1, iDocid);.
3483c 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
3483d 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
3483e 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
3483f 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20  _step(s);.  if( 
34840 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  rc!=SQLITE_ROW )
34841 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 76   return rc;..  v
34842 61 6c 75 65 73 20 3d 20 28 63 6f 6e 73 74 20 63  alues = (const c
34843 68 61 72 20 2a 2a 29 20 73 71 6c 69 74 65 33 5f  har **) sqlite3_
34844 6d 61 6c 6c 6f 63 28 76 2d 3e 6e 43 6f 6c 75 6d  malloc(v->nColum
34845 6e 20 2a 20 73 69 7a 65 6f 66 28 63 6f 6e 73 74  n * sizeof(const
34846 20 63 68 61 72 20 2a 29 29 3b 0a 20 20 66 6f 72   char *));.  for
34847 28 69 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f 6c 75  (i=0; i<v->nColu
34848 6d 6e 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 69 66  mn; ++i){.    if
34849 28 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  ( sqlite3_column
3484a 5f 74 79 70 65 28 73 2c 20 69 29 3d 3d 53 51 4c  _type(s, i)==SQL
3484b 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  ITE_NULL ){.    
3484c 20 20 76 61 6c 75 65 73 5b 69 5d 20 3d 20 4e 55    values[i] = NU
3484d 4c 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  LL;.    }else{. 
3484e 20 20 20 20 20 76 61 6c 75 65 73 5b 69 5d 20 3d       values[i] =
3484f 20 73 74 72 69 6e 67 5f 64 75 70 28 28 63 68 61   string_dup((cha
34850 72 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  r*)sqlite3_colum
34851 6e 5f 74 65 78 74 28 73 2c 20 69 29 29 3b 0a 20  n_text(s, i));. 
34852 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57     }.  }..  /* W
34853 65 20 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e  e expect only on
34854 65 20 72 6f 77 2e 20 20 57 65 20 6d 75 73 74 20  e row.  We must 
34855 65 78 65 63 75 74 65 20 61 6e 6f 74 68 65 72 20  execute another 
34856 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a 20  sqlite3_step(). 
34857 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20    * to complete 
34858 74 68 65 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f  the iteration; o
34859 74 68 65 72 77 69 73 65 20 74 68 65 20 74 61 62  therwise the tab
3485a 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c  le will remain l
3485b 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d  ocked. */.  rc =
3485c 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29   sqlite3_step(s)
3485d 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
3485e 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 2a  TE_DONE ){.    *
3485f 70 56 61 6c 75 65 73 20 3d 20 76 61 6c 75 65 73  pValues = values
34860 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
34861 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 66  ITE_OK;.  }..  f
34862 72 65 65 53 74 72 69 6e 67 41 72 72 61 79 28 76  reeStringArray(v
34863 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 76 61 6c 75 65  ->nColumn, value
34864 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  s);.  return rc;
34865 0a 7d 0a 0a 2f 2a 20 64 65 6c 65 74 65 20 66 72  .}../* delete fr
34866 6f 6d 20 25 5f 63 6f 6e 74 65 6e 74 20 77 68 65  om %_content whe
34867 72 65 20 64 6f 63 69 64 20 3d 20 5b 69 44 6f 63  re docid = [iDoc
34868 69 64 20 5d 20 2a 2f 0a 73 74 61 74 69 63 20 69  id ] */.static i
34869 6e 74 20 63 6f 6e 74 65 6e 74 5f 64 65 6c 65 74  nt content_delet
3486a 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  e(fulltext_vtab 
3486b 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  *v, sqlite_int64
3486c 20 69 44 6f 63 69 64 29 7b 0a 20 20 73 71 6c 69   iDocid){.  sqli
3486d 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69  te3_stmt *s;.  i
3486e 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f  nt rc = sql_get_
3486f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 43 4f 4e  statement(v, CON
34870 54 45 4e 54 5f 44 45 4c 45 54 45 5f 53 54 4d 54  TENT_DELETE_STMT
34871 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21  , &s);.  if( rc!
34872 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
34873 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
34874 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
34875 36 34 28 73 2c 20 31 2c 20 69 44 6f 63 69 64 29  64(s, 1, iDocid)
34876 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
34877 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
34878 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  c;..  return sql
34879 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b  _single_step(s);
3487a 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 73 20 53  .}../* Returns S
3487b 51 4c 49 54 45 5f 52 4f 57 20 69 66 20 61 6e 79  QLITE_ROW if any
3487c 20 72 6f 77 73 20 65 78 69 73 74 20 69 6e 20 25   rows exist in %
3487d 5f 63 6f 6e 74 65 6e 74 2c 20 53 51 4c 49 54 45  _content, SQLITE
3487e 5f 44 4f 4e 45 20 69 66 0a 2a 2a 20 6e 6f 20 72  _DONE if.** no r
3487f 6f 77 73 20 65 78 69 73 74 2c 20 61 6e 64 20 61  ows exist, and a
34880 6e 79 20 65 72 72 6f 72 20 69 6e 20 63 61 73 65  ny error in case
34881 20 6f 66 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a   of failure..*/.
34882 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6e 74 65  static int conte
34883 6e 74 5f 65 78 69 73 74 73 28 66 75 6c 6c 74 65  nt_exists(fullte
34884 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20 73  xt_vtab *v){.  s
34885 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a  qlite3_stmt *s;.
34886 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67    int rc = sql_g
34887 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
34888 43 4f 4e 54 45 4e 54 5f 45 58 49 53 54 53 5f 53  CONTENT_EXISTS_S
34889 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20  TMT, &s);.  if( 
3488a 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
3488b 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
3488c 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
3488d 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  s);.  if( rc!=SQ
3488e 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72  LITE_ROW ) retur
3488f 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 57 65 20 65  n rc;..  /* We e
34890 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72  xpect only one r
34891 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65 78 65  ow.  We must exe
34892 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73 71 6c  cute another sql
34893 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20 20 2a  ite3_step().   *
34894 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65   to complete the
34895 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65   iteration; othe
34896 72 77 69 73 65 20 74 68 65 20 74 61 62 6c 65 20  rwise the table 
34897 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f 63 6b  will remain lock
34898 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71  ed. */.  rc = sq
34899 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20  lite3_step(s);. 
3489a 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
3489b 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 53 51  DONE ) return SQ
3489c 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 69 66 28 20  LITE_ROW;.  if( 
3489d 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  rc==SQLITE_ROW )
3489e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
3489f 52 52 4f 52 3b 0a 20 20 72 65 74 75 72 6e 20 72  RROR;.  return r
348a0 63 3b 0a 7d 0a 0a 2f 2a 20 69 6e 73 65 72 74 20  c;.}../* insert 
348a1 69 6e 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 20  into %_segments 
348a2 76 61 6c 75 65 73 20 28 5b 70 44 61 74 61 5d 29  values ([pData])
348a3 0a 2a 2a 20 20 20 72 65 74 75 72 6e 73 20 61 73  .**   returns as
348a4 73 69 67 6e 65 64 20 62 6c 6f 63 6b 69 64 20 69  signed blockid i
348a5 6e 20 2a 70 69 42 6c 6f 63 6b 69 64 0a 2a 2f 0a  n *piBlockid.*/.
348a6 73 74 61 74 69 63 20 69 6e 74 20 62 6c 6f 63 6b  static int block
348a7 5f 69 6e 73 65 72 74 28 66 75 6c 6c 74 65 78 74  _insert(fulltext
348a8 5f 76 74 61 62 20 2a 76 2c 20 63 6f 6e 73 74 20  _vtab *v, const 
348a9 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74  char *pData, int
348aa 20 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20   nData,.        
348ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
348ac 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69  sqlite_int64 *pi
348ad 42 6c 6f 63 6b 69 64 29 7b 0a 20 20 73 71 6c 69  Blockid){.  sqli
348ae 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69  te3_stmt *s;.  i
348af 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f  nt rc = sql_get_
348b0 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 42 4c 4f  statement(v, BLO
348b1 43 4b 5f 49 4e 53 45 52 54 5f 53 54 4d 54 2c 20  CK_INSERT_STMT, 
348b2 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  &s);.  if( rc!=S
348b3 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
348b4 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
348b5 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28  lite3_bind_blob(
348b6 73 2c 20 31 2c 20 70 44 61 74 61 2c 20 6e 44 61  s, 1, pData, nDa
348b7 74 61 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  ta, SQLITE_STATI
348b8 43 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  C);.  if( rc!=SQ
348b9 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
348ba 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
348bb 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20  ite3_step(s);.  
348bc 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52  if( rc==SQLITE_R
348bd 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  OW ) return SQLI
348be 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69 66 28 20  TE_ERROR;.  if( 
348bf 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc!=SQLITE_DONE 
348c0 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
348c1 2f 2a 20 62 6c 6f 63 6b 69 64 20 63 6f 6c 75 6d  /* blockid colum
348c2 6e 20 69 73 20 61 6e 20 61 6c 69 61 73 20 66 6f  n is an alias fo
348c3 72 20 72 6f 77 69 64 2e 20 2a 2f 0a 20 20 2a 70  r rowid. */.  *p
348c4 69 42 6c 6f 63 6b 69 64 20 3d 20 73 71 6c 69 74  iBlockid = sqlit
348c5 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  e3_last_insert_r
348c6 6f 77 69 64 28 76 2d 3e 64 62 29 3b 0a 20 20 72  owid(v->db);.  r
348c7 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
348c8 0a 7d 0a 0a 2f 2a 20 64 65 6c 65 74 65 20 66 72  .}../* delete fr
348c9 6f 6d 20 25 5f 73 65 67 6d 65 6e 74 73 0a 2a 2a  om %_segments.**
348ca 20 20 20 77 68 65 72 65 20 62 6c 6f 63 6b 69 64     where blockid
348cb 20 62 65 74 77 65 65 6e 20 5b 69 53 74 61 72 74   between [iStart
348cc 42 6c 6f 63 6b 69 64 5d 20 61 6e 64 20 5b 69 45  Blockid] and [iE
348cd 6e 64 42 6c 6f 63 6b 69 64 5d 0a 2a 2a 0a 2a 2a  ndBlockid].**.**
348ce 20 44 65 6c 65 74 65 73 20 74 68 65 20 72 61 6e   Deletes the ran
348cf 67 65 20 6f 66 20 62 6c 6f 63 6b 73 2c 20 69 6e  ge of blocks, in
348d0 63 6c 75 73 69 76 65 2c 20 75 73 65 64 20 74 6f  clusive, used to
348d1 20 64 65 6c 65 74 65 20 74 68 65 20 62 6c 6f 63   delete the bloc
348d2 6b 73 0a 2a 2a 20 77 68 69 63 68 20 66 6f 72 6d  ks.** which form
348d3 20 61 20 73 65 67 6d 65 6e 74 2e 0a 2a 2f 0a 73   a segment..*/.s
348d4 74 61 74 69 63 20 69 6e 74 20 62 6c 6f 63 6b 5f  tatic int block_
348d5 64 65 6c 65 74 65 28 66 75 6c 6c 74 65 78 74 5f  delete(fulltext_
348d6 76 74 61 62 20 2a 76 2c 0a 20 20 20 20 20 20 20  vtab *v,.       
348d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
348d8 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53   sqlite_int64 iS
348d9 74 61 72 74 42 6c 6f 63 6b 69 64 2c 20 73 71 6c  tartBlockid, sql
348da 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c  ite_int64 iEndBl
348db 6f 63 6b 69 64 29 7b 0a 20 20 73 71 6c 69 74 65  ockid){.  sqlite
348dc 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74  3_stmt *s;.  int
348dd 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74   rc = sql_get_st
348de 61 74 65 6d 65 6e 74 28 76 2c 20 42 4c 4f 43 4b  atement(v, BLOCK
348df 5f 44 45 4c 45 54 45 5f 53 54 4d 54 2c 20 26 73  _DELETE_STMT, &s
348e0 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
348e1 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
348e2 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  rc;..  rc = sqli
348e3 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73  te3_bind_int64(s
348e4 2c 20 31 2c 20 69 53 74 61 72 74 42 6c 6f 63 6b  , 1, iStartBlock
348e5 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  id);.  if( rc!=S
348e6 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
348e7 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
348e8 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
348e9 28 73 2c 20 32 2c 20 69 45 6e 64 42 6c 6f 63 6b  (s, 2, iEndBlock
348ea 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  id);.  if( rc!=S
348eb 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
348ec 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20  n rc;..  return 
348ed 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28  sql_single_step(
348ee 73 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e  s);.}../* Return
348ef 73 20 53 51 4c 49 54 45 5f 52 4f 57 20 77 69 74  s SQLITE_ROW wit
348f0 68 20 2a 70 69 64 78 20 73 65 74 20 74 6f 20 74  h *pidx set to t
348f1 68 65 20 6d 61 78 69 6d 75 6d 20 73 65 67 6d 65  he maximum segme
348f2 6e 74 20 69 64 78 20 66 6f 75 6e 64 0a 2a 2a 20  nt idx found.** 
348f3 61 74 20 69 4c 65 76 65 6c 2e 20 20 52 65 74 75  at iLevel.  Retu
348f4 72 6e 73 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rns SQLITE_DONE 
348f5 69 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  if there are no 
348f6 73 65 67 6d 65 6e 74 73 20 61 74 0a 2a 2a 20 69  segments at.** i
348f7 4c 65 76 65 6c 2e 20 20 4f 74 68 65 72 77 69 73  Level.  Otherwis
348f8 65 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72  e returns an err
348f9 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  or..*/.static in
348fa 74 20 73 65 67 64 69 72 5f 6d 61 78 5f 69 6e 64  t segdir_max_ind
348fb 65 78 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  ex(fulltext_vtab
348fc 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 2c   *v, int iLevel,
348fd 20 69 6e 74 20 2a 70 69 64 78 29 7b 0a 20 20 73   int *pidx){.  s
348fe 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a  qlite3_stmt *s;.
348ff 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67    int rc = sql_g
34900 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
34901 53 45 47 44 49 52 5f 4d 41 58 5f 49 4e 44 45 58  SEGDIR_MAX_INDEX
34902 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66  _STMT, &s);.  if
34903 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
34904 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
34905 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e  rc = sqlite3_bin
34906 64 5f 69 6e 74 28 73 2c 20 31 2c 20 69 4c 65 76  d_int(s, 1, iLev
34907 65 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  el);.  if( rc!=S
34908 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
34909 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
3490a 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20  lite3_step(s);. 
3490b 20 2f 2a 20 53 68 6f 75 6c 64 20 61 6c 77 61 79   /* Should alway
3490c 73 20 67 65 74 20 61 74 20 6c 65 61 73 74 20 6f  s get at least o
3490d 6e 65 20 72 6f 77 20 64 75 65 20 74 6f 20 68 6f  ne row due to ho
3490e 77 20 6d 61 78 28 29 20 77 6f 72 6b 73 2e 20 2a  w max() works. *
3490f 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
34910 54 45 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e  TE_DONE ) return
34911 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20   SQLITE_DONE;.  
34912 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52  if( rc!=SQLITE_R
34913 4f 57 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OW ) return rc;.
34914 0a 20 20 2f 2a 20 4e 55 4c 4c 20 6d 65 61 6e 73  .  /* NULL means
34915 20 74 68 61 74 20 74 68 65 72 65 20 77 65 72 65   that there were
34916 20 6e 6f 20 69 6e 70 75 74 73 20 74 6f 20 6d 61   no inputs to ma
34917 78 28 29 2e 20 2a 2f 0a 20 20 69 66 28 20 53 51  x(). */.  if( SQ
34918 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73 71 6c 69 74  LITE_NULL==sqlit
34919 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73  e3_column_type(s
3491a 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 63 20 3d  , 0) ){.    rc =
3491b 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29   sqlite3_step(s)
3491c 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
3491d 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72  LITE_ROW ) retur
3491e 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
3491f 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
34920 20 7d 0a 0a 20 20 2a 70 69 64 78 20 3d 20 73 71   }..  *pidx = sq
34921 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
34922 28 73 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 57 65  (s, 0);..  /* We
34923 20 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65   expect only one
34924 20 72 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65   row.  We must e
34925 78 65 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73  xecute another s
34926 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20  qlite3_step().  
34927 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74   * to complete t
34928 68 65 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74  he iteration; ot
34929 68 65 72 77 69 73 65 20 74 68 65 20 74 61 62 6c  herwise the tabl
3492a 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f  e will remain lo
3492b 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20  cked. */.  rc = 
3492c 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b  sqlite3_step(s);
3492d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
3492e 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20 53  E_ROW ) return S
3492f 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69  QLITE_ERROR;.  i
34930 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f  f( rc!=SQLITE_DO
34931 4e 45 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  NE ) return rc;.
34932 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
34933 52 4f 57 3b 0a 7d 0a 0a 2f 2a 20 69 6e 73 65 72  ROW;.}../* inser
34934 74 20 69 6e 74 6f 20 25 5f 73 65 67 64 69 72 20  t into %_segdir 
34935 76 61 6c 75 65 73 20 28 0a 2a 2a 20 20 20 5b 69  values (.**   [i
34936 4c 65 76 65 6c 5d 2c 20 5b 69 64 78 5d 2c 0a 2a  Level], [idx],.*
34937 2a 20 20 20 5b 69 53 74 61 72 74 42 6c 6f 63 6b  *   [iStartBlock
34938 69 64 5d 2c 20 5b 69 4c 65 61 76 65 73 45 6e 64  id], [iLeavesEnd
34939 42 6c 6f 63 6b 69 64 5d 2c 20 5b 69 45 6e 64 42  Blockid], [iEndB
3493a 6c 6f 63 6b 69 64 5d 2c 0a 2a 2a 20 20 20 5b 70  lockid],.**   [p
3493b 52 6f 6f 74 44 61 74 61 5d 0a 2a 2a 20 29 0a 2a  RootData].** ).*
3493c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 67  /.static int seg
3493d 64 69 72 5f 73 65 74 28 66 75 6c 6c 74 65 78 74  dir_set(fulltext
3493e 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c  _vtab *v, int iL
3493f 65 76 65 6c 2c 20 69 6e 74 20 69 64 78 2c 0a 20  evel, int idx,. 
34940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34941 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
34942 34 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c  4 iStartBlockid,
34943 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
34944 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e         sqlite_in
34945 74 36 34 20 69 4c 65 61 76 65 73 45 6e 64 42 6c  t64 iLeavesEndBl
34946 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20 20  ockid,.         
34947 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
34948 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c  ite_int64 iEndBl
34949 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20 20  ockid,.         
3494a 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
3494b 73 74 20 63 68 61 72 20 2a 70 52 6f 6f 74 44 61  st char *pRootDa
3494c 74 61 2c 20 69 6e 74 20 6e 52 6f 6f 74 44 61 74  ta, int nRootDat
3494d 61 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  a){.  sqlite3_st
3494e 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20  mt *s;.  int rc 
3494f 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d  = sql_get_statem
34950 65 6e 74 28 76 2c 20 53 45 47 44 49 52 5f 53 45  ent(v, SEGDIR_SE
34951 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69  T_STMT, &s);.  i
34952 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
34953 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
34954 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
34955 6e 64 5f 69 6e 74 28 73 2c 20 31 2c 20 69 4c 65  nd_int(s, 1, iLe
34956 76 65 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  vel);.  if( rc!=
34957 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
34958 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73  rn rc;..  rc = s
34959 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28  qlite3_bind_int(
3495a 73 2c 20 32 2c 20 69 64 78 29 3b 0a 20 20 69 66  s, 2, idx);.  if
3495b 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
3495c 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
3495d 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e  rc = sqlite3_bin
3495e 64 5f 69 6e 74 36 34 28 73 2c 20 33 2c 20 69 53  d_int64(s, 3, iS
3495f 74 61 72 74 42 6c 6f 63 6b 69 64 29 3b 0a 20 20  tartBlockid);.  
34960 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
34961 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
34962 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
34963 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 34 2c 20  ind_int64(s, 4, 
34964 69 4c 65 61 76 65 73 45 6e 64 42 6c 6f 63 6b 69  iLeavesEndBlocki
34965 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  d);.  if( rc!=SQ
34966 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
34967 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
34968 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
34969 73 2c 20 35 2c 20 69 45 6e 64 42 6c 6f 63 6b 69  s, 5, iEndBlocki
3496a 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  d);.  if( rc!=SQ
3496b 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
3496c 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
3496d 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 73  ite3_bind_blob(s
3496e 2c 20 36 2c 20 70 52 6f 6f 74 44 61 74 61 2c 20  , 6, pRootData, 
3496f 6e 52 6f 6f 74 44 61 74 61 2c 20 53 51 4c 49 54  nRootData, SQLIT
34970 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 69 66 28  E_STATIC);.  if(
34971 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
34972 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
34973 65 74 75 72 6e 20 73 71 6c 5f 73 69 6e 67 6c 65  eturn sql_single
34974 5f 73 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f 2a 20  _step(s);.}../* 
34975 51 75 65 72 69 65 73 20 25 5f 73 65 67 64 69 72  Queries %_segdir
34976 20 66 6f 72 20 74 68 65 20 62 6c 6f 63 6b 20 73   for the block s
34977 70 61 6e 20 6f 66 20 74 68 65 20 73 65 67 6d 65  pan of the segme
34978 6e 74 73 20 69 6e 20 6c 65 76 65 6c 0a 2a 2a 20  nts in level.** 
34979 69 4c 65 76 65 6c 2e 20 20 52 65 74 75 72 6e 73  iLevel.  Returns
3497a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 66 20   SQLITE_DONE if 
3497b 74 68 65 72 65 20 61 72 65 20 6e 6f 20 62 6c 6f  there are no blo
3497c 63 6b 73 20 66 6f 72 20 69 4c 65 76 65 6c 2c 0a  cks for iLevel,.
3497d 2a 2a 20 53 51 4c 49 54 45 5f 52 4f 57 20 69 66  ** SQLITE_ROW if
3497e 20 74 68 65 72 65 20 61 72 65 20 62 6c 6f 63 6b   there are block
3497f 73 2c 20 65 6c 73 65 20 61 6e 20 65 72 72 6f 72  s, else an error
34980 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
34981 73 65 67 64 69 72 5f 73 70 61 6e 28 66 75 6c 6c  segdir_span(full
34982 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e  text_vtab *v, in
34983 74 20 69 4c 65 76 65 6c 2c 0a 20 20 20 20 20 20  t iLevel,.      
34984 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34985 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70   sqlite_int64 *p
34986 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c 0a 20  iStartBlockid,. 
34987 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34988 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74        sqlite_int
34989 36 34 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64  64 *piEndBlockid
3498a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  ){.  sqlite3_stm
3498b 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d  t *s;.  int rc =
3498c 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65   sql_get_stateme
3498d 6e 74 28 76 2c 20 53 45 47 44 49 52 5f 53 50 41  nt(v, SEGDIR_SPA
3498e 4e 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69  N_STMT, &s);.  i
3498f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
34990 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
34991 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
34992 6e 64 5f 69 6e 74 28 73 2c 20 31 2c 20 69 4c 65  nd_int(s, 1, iLe
34993 76 65 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  vel);.  if( rc!=
34994 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
34995 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73  rn rc;..  rc = s
34996 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a  qlite3_step(s);.
34997 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
34998 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 53  _DONE ) return S
34999 51 4c 49 54 45 5f 44 4f 4e 45 3b 20 20 2f 2a 20  QLITE_DONE;  /* 
3499a 53 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 70  Should never hap
3499b 70 65 6e 20 2a 2f 0a 20 20 69 66 28 20 72 63 21  pen */.  if( rc!
3499c 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65  =SQLITE_ROW ) re
3499d 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 54  turn rc;..  /* T
3499e 68 69 73 20 68 61 70 70 65 6e 73 20 69 66 20 61  his happens if a
3499f 6c 6c 20 73 65 67 6d 65 6e 74 73 20 61 74 20 74  ll segments at t
349a0 68 69 73 20 6c 65 76 65 6c 20 61 72 65 20 65 6e  his level are en
349a1 74 69 72 65 6c 79 20 69 6e 6c 69 6e 65 2e 20 2a  tirely inline. *
349a2 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e  /.  if( SQLITE_N
349a3 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 63 6f 6c  ULL==sqlite3_col
349a4 75 6d 6e 5f 74 79 70 65 28 73 2c 20 30 29 20 29  umn_type(s, 0) )
349a5 7b 0a 20 20 20 20 2f 2a 20 57 65 20 65 78 70 65  {.    /* We expe
349a6 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e  ct only one row.
349a7 20 20 57 65 20 6d 75 73 74 20 65 78 65 63 75 74    We must execut
349a8 65 20 61 6e 6f 74 68 65 72 20 73 71 6c 69 74 65  e another sqlite
349a9 33 5f 73 74 65 70 28 29 0a 20 20 20 20 20 2a 20  3_step().     * 
349aa 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20  to complete the 
349ab 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65 72  iteration; other
349ac 77 69 73 65 20 74 68 65 20 74 61 62 6c 65 20 77  wise the table w
349ad 69 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f 63 6b 65  ill remain locke
349ae 64 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 63  d. */.    int rc
349af 32 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  2 = sqlite3_step
349b0 28 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 32  (s);.    if( rc2
349b1 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72  ==SQLITE_ROW ) r
349b2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
349b3 4f 52 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72  OR;.    return r
349b4 63 32 3b 0a 20 20 7d 0a 0a 20 20 2a 70 69 53 74  c2;.  }..  *piSt
349b5 61 72 74 42 6c 6f 63 6b 69 64 20 3d 20 73 71 6c  artBlockid = sql
349b6 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36  ite3_column_int6
349b7 34 28 73 2c 20 30 29 3b 0a 20 20 2a 70 69 45 6e  4(s, 0);.  *piEn
349b8 64 42 6c 6f 63 6b 69 64 20 3d 20 73 71 6c 69 74  dBlockid = sqlit
349b9 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28  e3_column_int64(
349ba 73 2c 20 31 29 3b 0a 0a 20 20 2f 2a 20 57 65 20  s, 1);..  /* We 
349bb 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20  expect only one 
349bc 72 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65 78  row.  We must ex
349bd 65 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73 71  ecute another sq
349be 6c 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20 20  lite3_step().   
349bf 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68  * to complete th
349c0 65 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68  e iteration; oth
349c1 65 72 77 69 73 65 20 74 68 65 20 74 61 62 6c 65  erwise the table
349c2 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f 63   will remain loc
349c3 6b 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73  ked. */.  rc = s
349c4 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a  qlite3_step(s);.
349c5 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
349c6 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51  _ROW ) return SQ
349c7 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69 66  LITE_ERROR;.  if
349c8 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc!=SQLITE_DON
349c9 45 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  E ) return rc;. 
349ca 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52   return SQLITE_R
349cb 4f 57 3b 0a 7d 0a 0a 2f 2a 20 44 65 6c 65 74 65  OW;.}../* Delete
349cc 20 74 68 65 20 73 65 67 6d 65 6e 74 20 62 6c 6f   the segment blo
349cd 63 6b 73 20 61 6e 64 20 73 65 67 6d 65 6e 74 20  cks and segment 
349ce 64 69 72 65 63 74 6f 72 79 20 72 65 63 6f 72 64  directory record
349cf 73 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20 73 65 67  s for all.** seg
349d0 6d 65 6e 74 73 20 61 74 20 69 4c 65 76 65 6c 2e  ments at iLevel.
349d1 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
349d2 65 67 64 69 72 5f 64 65 6c 65 74 65 28 66 75 6c  egdir_delete(ful
349d3 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69  ltext_vtab *v, i
349d4 6e 74 20 69 4c 65 76 65 6c 29 7b 0a 20 20 73 71  nt iLevel){.  sq
349d5 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20  lite3_stmt *s;. 
349d6 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53   sqlite_int64 iS
349d7 74 61 72 74 42 6c 6f 63 6b 69 64 2c 20 69 45 6e  tartBlockid, iEn
349d8 64 42 6c 6f 63 6b 69 64 3b 0a 20 20 69 6e 74 20  dBlockid;.  int 
349d9 72 63 20 3d 20 73 65 67 64 69 72 5f 73 70 61 6e  rc = segdir_span
349da 28 76 2c 20 69 4c 65 76 65 6c 2c 20 26 69 53 74  (v, iLevel, &iSt
349db 61 72 74 42 6c 6f 63 6b 69 64 2c 20 26 69 45 6e  artBlockid, &iEn
349dc 64 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28  dBlockid);.  if(
349dd 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20   rc!=SQLITE_ROW 
349de 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f  && rc!=SQLITE_DO
349df 4e 45 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  NE ) return rc;.
349e0 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
349e1 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 72 63 20  E_ROW ){.    rc 
349e2 3d 20 62 6c 6f 63 6b 5f 64 65 6c 65 74 65 28 76  = block_delete(v
349e3 2c 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c  , iStartBlockid,
349e4 20 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20   iEndBlockid);. 
349e5 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
349e6 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
349e7 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 6c 65  ;.  }..  /* Dele
349e8 74 65 20 74 68 65 20 73 65 67 6d 65 6e 74 20 64  te the segment d
349e9 69 72 65 63 74 6f 72 79 20 69 74 73 65 6c 66 2e  irectory itself.
349ea 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 5f 67   */.  rc = sql_g
349eb 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
349ec 53 45 47 44 49 52 5f 44 45 4c 45 54 45 5f 53 54  SEGDIR_DELETE_ST
349ed 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72  MT, &s);.  if( r
349ee 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
349ef 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
349f0 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  = sqlite3_bind_i
349f1 6e 74 36 34 28 73 2c 20 31 2c 20 69 4c 65 76 65  nt64(s, 1, iLeve
349f2 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  l);.  if( rc!=SQ
349f3 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
349f4 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73   rc;..  return s
349f5 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73  ql_single_step(s
349f6 29 3b 0a 7d 0a 0a 2f 2a 20 44 65 6c 65 74 65 20  );.}../* Delete 
349f7 65 6e 74 69 72 65 20 66 74 73 20 69 6e 64 65 78  entire fts index
349f8 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  , SQLITE_OK on s
349f9 75 63 63 65 73 73 2c 20 72 65 6c 65 76 61 6e 74  uccess, relevant
349fa 20 65 72 72 6f 72 20 6f 6e 0a 2a 2a 20 66 61 69   error on.** fai
349fb 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lure..*/.static 
349fc 69 6e 74 20 73 65 67 64 69 72 5f 64 65 6c 65 74  int segdir_delet
349fd 65 5f 61 6c 6c 28 66 75 6c 6c 74 65 78 74 5f 76  e_all(fulltext_v
349fe 74 61 62 20 2a 76 29 7b 0a 20 20 73 71 6c 69 74  tab *v){.  sqlit
349ff 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e  e3_stmt *s;.  in
34a00 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73  t rc = sql_get_s
34a01 74 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44  tatement(v, SEGD
34a02 49 52 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f 53 54  IR_DELETE_ALL_ST
34a03 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72  MT, &s);.  if( r
34a04 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
34a05 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
34a06 3d 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65  = sql_single_ste
34a07 70 28 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  p(s);.  if( rc!=
34a08 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
34a09 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73  rn rc;..  rc = s
34a0a 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
34a0b 28 76 2c 20 42 4c 4f 43 4b 5f 44 45 4c 45 54 45  (v, BLOCK_DELETE
34a0c 5f 41 4c 4c 5f 53 54 4d 54 2c 20 26 73 29 3b 0a  _ALL_STMT, &s);.
34a0d 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
34a0e 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
34a0f 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 73  ..  return sql_s
34a10 69 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d  ingle_step(s);.}
34a11 0a 0a 2f 2a 20 52 65 74 75 72 6e 73 20 53 51 4c  ../* Returns SQL
34a12 49 54 45 5f 4f 4b 20 77 69 74 68 20 2a 70 6e 53  ITE_OK with *pnS
34a13 65 67 6d 65 6e 74 73 20 73 65 74 20 74 6f 20 74  egments set to t
34a14 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74  he number of ent
34a15 72 69 65 73 20 69 6e 0a 2a 2a 20 25 5f 73 65 67  ries in.** %_seg
34a16 64 69 72 20 61 6e 64 20 2a 70 69 4d 61 78 4c 65  dir and *piMaxLe
34a17 76 65 6c 20 73 65 74 20 74 6f 20 74 68 65 20 68  vel set to the h
34a18 69 67 68 65 73 74 20 6c 65 76 65 6c 20 77 68 69  ighest level whi
34a19 63 68 20 68 61 73 20 61 0a 2a 2a 20 73 65 67 6d  ch has a.** segm
34a1a 65 6e 74 2e 20 20 4f 74 68 65 72 77 69 73 65 20  ent.  Otherwise 
34a1b 72 65 74 75 72 6e 73 20 74 68 65 20 53 51 4c 69  returns the SQLi
34a1c 74 65 20 65 72 72 6f 72 20 77 68 69 63 68 20 63  te error which c
34a1d 61 75 73 65 64 20 66 61 69 6c 75 72 65 2e 0a 2a  aused failure..*
34a1e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 67  /.static int seg
34a1f 64 69 72 5f 63 6f 75 6e 74 28 66 75 6c 6c 74 65  dir_count(fullte
34a20 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20  xt_vtab *v, int 
34a21 2a 70 6e 53 65 67 6d 65 6e 74 73 2c 20 69 6e 74  *pnSegments, int
34a22 20 2a 70 69 4d 61 78 4c 65 76 65 6c 29 7b 0a 20   *piMaxLevel){. 
34a23 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73   sqlite3_stmt *s
34a24 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c  ;.  int rc = sql
34a25 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
34a26 2c 20 53 45 47 44 49 52 5f 43 4f 55 4e 54 5f 53  , SEGDIR_COUNT_S
34a27 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20  TMT, &s);.  if( 
34a28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
34a29 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
34a2a 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
34a2b 73 29 3b 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68  s);.  /* TODO(sh
34a2c 65 73 73 29 3a 20 54 68 69 73 20 63 61 73 65 20  ess): This case 
34a2d 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 70 6f  should not be po
34a2e 73 73 69 62 6c 65 3f 20 20 53 68 6f 75 6c 64 20  ssible?  Should 
34a2f 73 74 72 6f 6e 67 65 72 0a 20 20 2a 2a 20 6d 65  stronger.  ** me
34a30 61 73 75 72 65 73 20 62 65 20 74 61 6b 65 6e 20  asures be taken 
34a31 69 66 20 69 74 20 68 61 70 70 65 6e 73 3f 0a 20  if it happens?. 
34a32 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   */.  if( rc==SQ
34a33 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20  LITE_DONE ){.   
34a34 20 2a 70 6e 53 65 67 6d 65 6e 74 73 20 3d 20 30   *pnSegments = 0
34a35 3b 0a 20 20 20 20 2a 70 69 4d 61 78 4c 65 76 65  ;.    *piMaxLeve
34a36 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72  l = 0;.    retur
34a37 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
34a38 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
34a39 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20 72  E_ROW ) return r
34a3a 63 3b 0a 0a 20 20 2a 70 6e 53 65 67 6d 65 6e 74  c;..  *pnSegment
34a3b 73 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  s = sqlite3_colu
34a3c 6d 6e 5f 69 6e 74 28 73 2c 20 30 29 3b 0a 20 20  mn_int(s, 0);.  
34a3d 2a 70 69 4d 61 78 4c 65 76 65 6c 20 3d 20 73 71  *piMaxLevel = sq
34a3e 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
34a3f 28 73 2c 20 31 29 3b 0a 0a 20 20 2f 2a 20 57 65  (s, 1);..  /* We
34a40 20 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65   expect only one
34a41 20 72 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65   row.  We must e
34a42 78 65 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73  xecute another s
34a43 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20  qlite3_step().  
34a44 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74   * to complete t
34a45 68 65 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74  he iteration; ot
34a46 68 65 72 77 69 73 65 20 74 68 65 20 74 61 62 6c  herwise the tabl
34a47 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f  e will remain lo
34a48 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20  cked. */.  rc = 
34a49 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b  sqlite3_step(s);
34a4a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
34a4b 45 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20  E_DONE ) return 
34a4c 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
34a4d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20   rc==SQLITE_ROW 
34a4e 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
34a4f 45 52 52 4f 52 3b 0a 20 20 72 65 74 75 72 6e 20  ERROR;.  return 
34a50 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f 28 73  rc;.}../* TODO(s
34a51 68 65 73 73 29 20 63 6c 65 61 72 50 65 6e 64 69  hess) clearPendi
34a52 6e 67 54 65 72 6d 73 28 29 20 69 73 20 66 61 72  ngTerms() is far
34a53 20 64 6f 77 6e 20 74 68 65 20 66 69 6c 65 20 62   down the file b
34a54 65 63 61 75 73 65 0a 2a 2a 20 77 72 69 74 65 5a  ecause.** writeZ
34a55 65 72 6f 53 65 67 6d 65 6e 74 28 29 20 69 73 20  eroSegment() is 
34a56 66 61 72 20 64 6f 77 6e 20 74 68 65 20 66 69 6c  far down the fil
34a57 65 20 62 65 63 61 75 73 65 20 4c 65 61 66 57 72  e because LeafWr
34a58 69 74 65 72 20 69 73 20 66 61 72 0a 2a 2a 20 64  iter is far.** d
34a59 6f 77 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 43  own the file.  C
34a5a 6f 6e 73 69 64 65 72 20 72 65 66 61 63 74 6f 72  onsider refactor
34a5b 69 6e 67 20 74 68 65 20 63 6f 64 65 20 74 6f 20  ing the code to 
34a5c 6d 6f 76 65 20 74 68 65 20 6e 6f 6e 2d 76 74 61  move the non-vta
34a5d 62 0a 2a 2a 20 63 6f 64 65 20 61 62 6f 76 65 20  b.** code above 
34a5e 74 68 65 20 76 74 61 62 20 63 6f 64 65 20 73 6f  the vtab code so
34a5f 20 74 68 61 74 20 77 65 20 64 6f 6e 27 74 20 6e   that we don't n
34a60 65 65 64 20 74 68 69 73 20 66 6f 72 77 61 72 64  eed this forward
34a61 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 2e 0a 2a  .** reference..*
34a62 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6c 65  /.static int cle
34a63 61 72 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 66  arPendingTerms(f
34a64 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29  ulltext_vtab *v)
34a65 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68  ;../*.** Free th
34a66 65 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f  e memory used to
34a67 20 63 6f 6e 74 61 69 6e 20 61 20 66 75 6c 6c 74   contain a fullt
34a68 65 78 74 5f 76 74 61 62 20 73 74 72 75 63 74 75  ext_vtab structu
34a69 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  re..*/.static vo
34a6a 69 64 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  id fulltext_vtab
34a6b 5f 64 65 73 74 72 6f 79 28 66 75 6c 6c 74 65 78  _destroy(fulltex
34a6c 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20 69 6e  t_vtab *v){.  in
34a6d 74 20 69 53 74 6d 74 2c 20 69 3b 0a 0a 20 20 46  t iStmt, i;..  F
34a6e 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20 44  TSTRACE(("FTS3 D
34a6f 65 73 74 72 6f 79 20 25 70 5c 6e 22 2c 20 76 29  estroy %p\n", v)
34a70 29 3b 0a 20 20 66 6f 72 28 20 69 53 74 6d 74 3d  );.  for( iStmt=
34a71 30 3b 20 69 53 74 6d 74 3c 4d 41 58 5f 53 54 4d  0; iStmt<MAX_STM
34a72 54 3b 20 69 53 74 6d 74 2b 2b 20 29 7b 0a 20 20  T; iStmt++ ){.  
34a73 20 20 69 66 28 20 76 2d 3e 70 46 75 6c 6c 74 65    if( v->pFullte
34a74 78 74 53 74 61 74 65 6d 65 6e 74 73 5b 69 53 74  xtStatements[iSt
34a75 6d 74 5d 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20  mt]!=NULL ){.   
34a76 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c     sqlite3_final
34a77 69 7a 65 28 76 2d 3e 70 46 75 6c 6c 74 65 78 74  ize(v->pFulltext
34a78 53 74 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d 74  Statements[iStmt
34a79 5d 29 3b 0a 20 20 20 20 20 20 76 2d 3e 70 46 75  ]);.      v->pFu
34a7a 6c 6c 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73  lltextStatements
34a7b 5b 69 53 74 6d 74 5d 20 3d 20 4e 55 4c 4c 3b 0a  [iStmt] = NULL;.
34a7c 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 66 6f 72      }.  }..  for
34a7d 28 20 69 3d 30 3b 20 69 3c 4d 45 52 47 45 5f 43  ( i=0; i<MERGE_C
34a7e 4f 55 4e 54 3b 20 69 2b 2b 20 29 7b 0a 20 20 20  OUNT; i++ ){.   
34a7f 20 69 66 28 20 76 2d 3e 70 4c 65 61 66 53 65 6c   if( v->pLeafSel
34a80 65 63 74 53 74 6d 74 73 5b 69 5d 21 3d 4e 55 4c  ectStmts[i]!=NUL
34a81 4c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  L ){.      sqlit
34a82 65 33 5f 66 69 6e 61 6c 69 7a 65 28 76 2d 3e 70  e3_finalize(v->p
34a83 4c 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73 5b  LeafSelectStmts[
34a84 69 5d 29 3b 0a 20 20 20 20 20 20 76 2d 3e 70 4c  i]);.      v->pL
34a85 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73 5b 69  eafSelectStmts[i
34a86 5d 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a  ] = NULL;.    }.
34a87 20 20 7d 0a 0a 20 20 69 66 28 20 76 2d 3e 70 54    }..  if( v->pT
34a88 6f 6b 65 6e 69 7a 65 72 21 3d 4e 55 4c 4c 20 29  okenizer!=NULL )
34a89 7b 0a 20 20 20 20 76 2d 3e 70 54 6f 6b 65 6e 69  {.    v->pTokeni
34a8a 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44  zer->pModule->xD
34a8b 65 73 74 72 6f 79 28 76 2d 3e 70 54 6f 6b 65 6e  estroy(v->pToken
34a8c 69 7a 65 72 29 3b 0a 20 20 20 20 76 2d 3e 70 54  izer);.    v->pT
34a8d 6f 6b 65 6e 69 7a 65 72 20 3d 20 4e 55 4c 4c 3b  okenizer = NULL;
34a8e 0a 20 20 7d 0a 0a 20 20 63 6c 65 61 72 50 65 6e  .  }..  clearPen
34a8f 64 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a 0a 20  dingTerms(v);.. 
34a90 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76 2d   sqlite3_free(v-
34a91 3e 61 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20 66 6f  >azColumn);.  fo
34a92 72 28 69 20 3d 20 30 3b 20 69 20 3c 20 76 2d 3e  r(i = 0; i < v->
34a93 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 20 7b 0a  nColumn; ++i) {.
34a94 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
34a95 28 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c  (v->azContentCol
34a96 75 6d 6e 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73  umn[i]);.  }.  s
34a97 71 6c 69 74 65 33 5f 66 72 65 65 28 76 2d 3e 61  qlite3_free(v->a
34a98 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b  zContentColumn);
34a99 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
34a9a 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 6f 6b  v);.}../*.** Tok
34a9b 65 6e 20 74 79 70 65 73 20 66 6f 72 20 70 61 72  en types for par
34a9c 73 69 6e 67 20 74 68 65 20 61 72 67 75 6d 65 6e  sing the argumen
34a9d 74 73 20 74 6f 20 78 43 6f 6e 6e 65 63 74 20 6f  ts to xConnect o
34a9e 72 20 78 43 72 65 61 74 65 2e 0a 2a 2f 0a 23 64  r xCreate..*/.#d
34a9f 65 66 69 6e 65 20 54 4f 4b 45 4e 5f 45 4f 46 20  efine TOKEN_EOF 
34aa0 20 20 20 20 20 20 20 20 30 20 20 20 20 2f 2a 20          0    /* 
34aa1 45 6e 64 20 6f 66 20 66 69 6c 65 20 2a 2f 0a 23  End of file */.#
34aa2 64 65 66 69 6e 65 20 54 4f 4b 45 4e 5f 53 50 41  define TOKEN_SPA
34aa3 43 45 20 20 20 20 20 20 20 31 20 20 20 20 2f 2a  CE       1    /*
34aa4 20 41 6e 79 20 6b 69 6e 64 20 6f 66 20 77 68 69   Any kind of whi
34aa5 74 65 73 70 61 63 65 20 2a 2f 0a 23 64 65 66 69  tespace */.#defi
34aa6 6e 65 20 54 4f 4b 45 4e 5f 49 44 20 20 20 20 20  ne TOKEN_ID     
34aa7 20 20 20 20 20 32 20 20 20 20 2f 2a 20 41 6e 20       2    /* An 
34aa8 69 64 65 6e 74 69 66 69 65 72 20 2a 2f 0a 23 64  identifier */.#d
34aa9 65 66 69 6e 65 20 54 4f 4b 45 4e 5f 53 54 52 49  efine TOKEN_STRI
34aaa 4e 47 20 20 20 20 20 20 33 20 20 20 20 2f 2a 20  NG      3    /* 
34aab 41 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c  A string literal
34aac 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4f 4b 45   */.#define TOKE
34aad 4e 5f 50 55 4e 43 54 20 20 20 20 20 20 20 34 20  N_PUNCT       4 
34aae 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 70     /* A single p
34aaf 75 6e 63 74 75 61 74 69 6f 6e 20 63 68 61 72 61  unctuation chara
34ab0 63 74 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49  cter */../*.** I
34ab1 66 20 58 20 69 73 20 61 20 63 68 61 72 61 63 74  f X is a charact
34ab2 65 72 20 74 68 61 74 20 63 61 6e 20 62 65 20 75  er that can be u
34ab3 73 65 64 20 69 6e 20 61 6e 20 69 64 65 6e 74 69  sed in an identi
34ab4 66 69 65 72 20 74 68 65 6e 0a 2a 2a 20 66 74 73  fier then.** fts
34ab5 49 64 43 68 61 72 28 58 29 20 77 69 6c 6c 20 62  IdChar(X) will b
34ab6 65 20 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69  e true.  Otherwi
34ab7 73 65 20 69 74 20 69 73 20 66 61 6c 73 65 2e 0a  se it is false..
34ab8 2a 2a 0a 2a 2a 20 46 6f 72 20 41 53 43 49 49 2c  **.** For ASCII,
34ab9 20 61 6e 79 20 63 68 61 72 61 63 74 65 72 20 77   any character w
34aba 69 74 68 20 74 68 65 20 68 69 67 68 2d 6f 72 64  ith the high-ord
34abb 65 72 20 62 69 74 20 73 65 74 20 69 73 0a 2a 2a  er bit set is.**
34abc 20 61 6c 6c 6f 77 65 64 20 69 6e 20 61 6e 20 69   allowed in an i
34abd 64 65 6e 74 69 66 69 65 72 2e 20 20 46 6f 72 20  dentifier.  For 
34abe 37 2d 62 69 74 20 63 68 61 72 61 63 74 65 72 73  7-bit characters
34abf 2c 20 0a 2a 2a 20 69 73 46 74 73 49 64 43 68 61  , .** isFtsIdCha
34ac0 72 5b 58 5d 20 6d 75 73 74 20 62 65 20 31 2e 0a  r[X] must be 1..
34ac1 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 31 30  **.** Ticket #10
34ac2 36 36 2e 20 20 74 68 65 20 53 51 4c 20 73 74 61  66.  the SQL sta
34ac3 6e 64 61 72 64 20 64 6f 65 73 20 6e 6f 74 20 61  ndard does not a
34ac4 6c 6c 6f 77 20 27 24 27 20 69 6e 20 74 68 65 0a  llow '$' in the.
34ac5 2a 2a 20 6d 69 64 64 6c 65 20 6f 66 20 69 64 65  ** middle of ide
34ac6 6e 74 66 69 65 72 73 2e 20 20 42 75 74 20 6d 61  ntfiers.  But ma
34ac7 6e 79 20 53 51 4c 20 69 6d 70 6c 65 6d 65 6e 74  ny SQL implement
34ac8 61 74 69 6f 6e 73 20 64 6f 2e 20 0a 2a 2a 20 53  ations do. .** S
34ac9 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 6c 6f 77  QLite will allow
34aca 20 27 24 27 20 69 6e 20 69 64 65 6e 74 69 66 69   '$' in identifi
34acb 65 72 73 20 66 6f 72 20 63 6f 6d 70 61 74 69 62  ers for compatib
34acc 69 6c 69 74 79 2e 0a 2a 2a 20 42 75 74 20 74 68  ility..** But th
34acd 65 20 66 65 61 74 75 72 65 20 69 73 20 75 6e 64  e feature is und
34ace 6f 63 75 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 73 74  ocumented..*/.st
34acf 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
34ad0 69 73 46 74 73 49 64 43 68 61 72 5b 5d 20 3d 20  isFtsIdChar[] = 
34ad1 7b 0a 2f 2a 20 78 30 20 78 31 20 78 32 20 78 33  {./* x0 x1 x2 x3
34ad2 20 78 34 20 78 35 20 78 36 20 78 37 20 78 38 20   x4 x5 x6 x7 x8 
34ad3 78 39 20 78 41 20 78 42 20 78 43 20 78 44 20 78  x9 xA xB xC xD x
34ad4 45 20 78 46 20 2a 2f 0a 20 20 20 20 30 2c 20 30  E xF */.    0, 0
34ad5 2c 20 30 2c 20 30 2c 20 31 2c 20 30 2c 20 30 2c  , 0, 0, 1, 0, 0,
34ad6 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
34ad7 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
34ad8 32 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20  2x */.    1, 1, 
34ad9 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
34ada 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 1, 0, 0, 0,
34adb 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 33 78   0, 0, 0,  /* 3x
34adc 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c   */.    0, 1, 1,
34add 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
34ade 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
34adf 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 34 78 20 2a  , 1, 1,  /* 4x *
34ae0 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31  /.    1, 1, 1, 1
34ae1 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
34ae2 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 1, 0, 0, 0, 
34ae3 30 2c 20 31 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a  0, 1,  /* 5x */.
34ae4 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20      0, 1, 1, 1, 
34ae5 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
34ae6 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
34ae7 20 31 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20   1,  /* 6x */.  
34ae8 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    1, 1, 1, 1, 1,
34ae9 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
34aea 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 0, 0, 0, 0, 0
34aeb 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a 7d 3b 0a 23  ,  /* 7x */.};.#
34aec 64 65 66 69 6e 65 20 66 74 73 49 64 43 68 61 72  define ftsIdChar
34aed 28 43 29 20 20 28 28 28 63 3d 43 29 26 30 78 38  (C)  (((c=C)&0x8
34aee 30 29 21 3d 30 20 7c 7c 20 28 63 3e 30 78 31 66  0)!=0 || (c>0x1f
34aef 20 26 26 20 69 73 46 74 73 49 64 43 68 61 72 5b   && isFtsIdChar[
34af0 63 2d 30 78 32 30 5d 29 29 0a 0a 0a 2f 2a 0a 2a  c-0x20])).../*.*
34af1 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6c 65 6e  * Return the len
34af2 67 74 68 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  gth of the token
34af3 20 74 68 61 74 20 62 65 67 69 6e 73 20 61 74 20   that begins at 
34af4 7a 5b 30 5d 2e 20 0a 2a 2a 20 53 74 6f 72 65 20  z[0]. .** Store 
34af5 74 68 65 20 74 6f 6b 65 6e 20 74 79 70 65 20 69  the token type i
34af6 6e 20 2a 74 6f 6b 65 6e 54 79 70 65 20 62 65 66  n *tokenType bef
34af7 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
34af8 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
34af9 47 65 74 54 6f 6b 65 6e 28 63 6f 6e 73 74 20 63  GetToken(const c
34afa 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 74 6f 6b  har *z, int *tok
34afb 65 6e 54 79 70 65 29 7b 0a 20 20 69 6e 74 20 69  enType){.  int i
34afc 2c 20 63 3b 0a 20 20 73 77 69 74 63 68 28 20 2a  , c;.  switch( *
34afd 7a 20 29 7b 0a 20 20 20 20 63 61 73 65 20 30 3a  z ){.    case 0:
34afe 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54   {.      *tokenT
34aff 79 70 65 20 3d 20 54 4f 4b 45 4e 5f 45 4f 46 3b  ype = TOKEN_EOF;
34b00 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
34b01 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
34b02 27 20 27 3a 20 63 61 73 65 20 27 5c 74 27 3a 20  ' ': case '\t': 
34b03 63 61 73 65 20 27 5c 6e 27 3a 20 63 61 73 65 20  case '\n': case 
34b04 27 5c 66 27 3a 20 63 61 73 65 20 27 5c 72 27 3a  '\f': case '\r':
34b05 20 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31   {.      for(i=1
34b06 3b 20 73 61 66 65 5f 69 73 73 70 61 63 65 28 7a  ; safe_isspace(z
34b07 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20  [i]); i++){}.   
34b08 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
34b09 54 4f 4b 45 4e 5f 53 50 41 43 45 3b 0a 20 20 20  TOKEN_SPACE;.   
34b0a 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20     return i;.   
34b0b 20 7d 0a 20 20 20 20 63 61 73 65 20 27 60 27 3a   }.    case '`':
34b0c 0a 20 20 20 20 63 61 73 65 20 27 5c 27 27 3a 0a  .    case '\'':.
34b0d 20 20 20 20 63 61 73 65 20 27 22 27 3a 20 7b 0a      case '"': {.
34b0e 20 20 20 20 20 20 69 6e 74 20 64 65 6c 69 6d 20        int delim 
34b0f 3d 20 7a 5b 30 5d 3b 0a 20 20 20 20 20 20 66 6f  = z[0];.      fo
34b10 72 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d 29 21  r(i=1; (c=z[i])!
34b11 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  =0; i++){.      
34b12 20 20 69 66 28 20 63 3d 3d 64 65 6c 69 6d 20 29    if( c==delim )
34b13 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
34b14 7a 5b 69 2b 31 5d 3d 3d 64 65 6c 69 6d 20 29 7b  z[i+1]==delim ){
34b15 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b  .            i++
34b16 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
34b17 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  e{.            b
34b18 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
34b19 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
34b1a 20 20 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e    }.      *token
34b1b 54 79 70 65 20 3d 20 54 4f 4b 45 4e 5f 53 54 52  Type = TOKEN_STR
34b1c 49 4e 47 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ING;.      retur
34b1d 6e 20 69 20 2b 20 28 63 21 3d 30 29 3b 0a 20 20  n i + (c!=0);.  
34b1e 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 5b 27    }.    case '['
34b1f 3a 20 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  : {.      for(i=
34b20 31 2c 20 63 3d 7a 5b 30 5d 3b 20 63 21 3d 27 5d  1, c=z[0]; c!=']
34b21 27 20 26 26 20 28 63 3d 7a 5b 69 5d 29 21 3d 30  ' && (c=z[i])!=0
34b22 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 2a  ; i++){}.      *
34b23 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4f 4b 45  tokenType = TOKE
34b24 4e 5f 49 44 3b 0a 20 20 20 20 20 20 72 65 74 75  N_ID;.      retu
34b25 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn i;.    }.    
34b26 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
34b27 20 69 66 28 20 21 66 74 73 49 64 43 68 61 72 28   if( !ftsIdChar(
34b28 2a 7a 29 20 29 7b 0a 20 20 20 20 20 20 20 20 62  *z) ){.        b
34b29 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
34b2a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 66 74 73      for(i=1; fts
34b2b 49 64 43 68 61 72 28 7a 5b 69 5d 29 3b 20 69 2b  IdChar(z[i]); i+
34b2c 2b 29 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65  +){}.      *toke
34b2d 6e 54 79 70 65 20 3d 20 54 4f 4b 45 4e 5f 49 44  nType = TOKEN_ID
34b2e 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  ;.      return i
34b2f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 74  ;.    }.  }.  *t
34b30 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4f 4b 45 4e  okenType = TOKEN
34b31 5f 50 55 4e 43 54 3b 0a 20 20 72 65 74 75 72 6e  _PUNCT;.  return
34b32 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 74   1;.}../*.** A t
34b33 6f 6b 65 6e 20 65 78 74 72 61 63 74 65 64 20 66  oken extracted f
34b34 72 6f 6d 20 61 20 73 74 72 69 6e 67 20 69 73 20  rom a string is 
34b35 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
34b36 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  he following.** 
34b37 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79  structure..*/.ty
34b38 70 65 64 65 66 20 73 74 72 75 63 74 20 46 74 73  pedef struct Fts
34b39 54 6f 6b 65 6e 20 7b 0a 20 20 63 6f 6e 73 74 20  Token {.  const 
34b3a 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 2f  char *z;       /
34b3b 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 6f 6b  * Pointer to tok
34b3c 65 6e 20 74 65 78 74 2e 20 20 4e 6f 74 20 27 5c  en text.  Not '\
34b3d 30 30 30 27 20 74 65 72 6d 69 6e 61 74 65 64 20  000' terminated 
34b3e 2a 2f 0a 20 20 73 68 6f 72 74 20 69 6e 74 20 6e  */.  short int n
34b3f 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e  ;         /* Len
34b40 67 74 68 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  gth of the token
34b41 20 74 65 78 74 20 69 6e 20 62 79 74 65 73 2e 20   text in bytes. 
34b42 2a 2f 0a 7d 20 46 74 73 54 6f 6b 65 6e 3b 0a 0a  */.} FtsToken;..
34b43 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 69 6e  /*.** Given a in
34b44 70 75 74 20 73 74 72 69 6e 67 20 28 77 68 69 63  put string (whic
34b45 68 20 69 73 20 72 65 61 6c 6c 79 20 6f 6e 65 20  h is really one 
34b46 6f 66 20 74 68 65 20 61 72 67 76 5b 5d 20 70 61  of the argv[] pa
34b47 72 61 6d 65 74 65 72 73 0a 2a 2a 20 70 61 73 73  rameters.** pass
34b48 65 64 20 69 6e 74 6f 20 78 43 6f 6e 6e 65 63 74  ed into xConnect
34b49 20 6f 72 20 78 43 72 65 61 74 65 29 20 73 70 6c   or xCreate) spl
34b4a 69 74 20 74 68 65 20 73 74 72 69 6e 67 20 75 70  it the string up
34b4b 20 69 6e 74 6f 20 74 6f 6b 65 6e 73 2e 0a 2a 2a   into tokens..**
34b4c 20 52 65 74 75 72 6e 20 61 6e 20 61 72 72 61 79   Return an array
34b4d 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20   of pointers to 
34b4e 27 5c 30 30 30 27 20 74 65 72 6d 69 6e 61 74 65  '\000' terminate
34b4f 64 20 73 74 72 69 6e 67 73 2c 20 6f 6e 65 20 73  d strings, one s
34b50 74 72 69 6e 67 0a 2a 2a 20 66 6f 72 20 65 61 63  tring.** for eac
34b51 68 20 6e 6f 6e 2d 77 68 69 74 65 73 70 61 63 65  h non-whitespace
34b52 20 74 6f 6b 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68   token..**.** Th
34b53 65 20 72 65 74 75 72 6e 65 64 20 61 72 72 61 79  e returned array
34b54 20 69 73 20 74 65 72 6d 69 6e 61 74 65 64 20 62   is terminated b
34b55 79 20 61 20 73 69 6e 67 6c 65 20 4e 55 4c 4c 20  y a single NULL 
34b56 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 53  pointer..**.** S
34b57 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65  pace to hold the
34b58 20 72 65 74 75 72 6e 65 64 20 61 72 72 61 79 20   returned array 
34b59 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  is obtained from
34b5a 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6d 61 6c   a single.** mal
34b5b 6c 6f 63 20 61 6e 64 20 73 68 6f 75 6c 64 20 62  loc and should b
34b5c 65 20 66 72 65 65 64 20 62 79 20 70 61 73 73 69  e freed by passi
34b5d 6e 67 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  ng the return va
34b5e 6c 75 65 20 74 6f 20 66 72 65 65 28 29 2e 0a 2a  lue to free()..*
34b5f 2a 20 54 68 65 20 69 6e 64 69 76 69 64 75 61 6c  * The individual
34b60 20 73 74 72 69 6e 67 73 20 77 69 74 68 69 6e 20   strings within 
34b61 74 68 65 20 74 6f 6b 65 6e 20 6c 69 73 74 20 61  the token list a
34b62 72 65 20 61 6c 6c 20 61 20 70 61 72 74 20 6f 66  re all a part of
34b63 0a 2a 2a 20 74 68 65 20 73 69 6e 67 6c 65 20 6d  .** the single m
34b64 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
34b65 20 61 6e 64 20 77 69 6c 6c 20 61 6c 6c 20 62 65   and will all be
34b66 20 66 72 65 65 64 20 61 74 20 6f 6e 63 65 2e 0a   freed at once..
34b67 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a  */.static char *
34b68 2a 74 6f 6b 65 6e 69 7a 65 53 74 72 69 6e 67 28  *tokenizeString(
34b69 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
34b6a 6e 74 20 2a 70 6e 54 6f 6b 65 6e 29 7b 0a 20 20  nt *pnToken){.  
34b6b 69 6e 74 20 6e 54 6f 6b 65 6e 20 3d 20 30 3b 0a  int nToken = 0;.
34b6c 20 20 46 74 73 54 6f 6b 65 6e 20 2a 61 54 6f 6b    FtsToken *aTok
34b6d 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  en = sqlite3_mal
34b6e 6c 6f 63 28 20 73 74 72 6c 65 6e 28 7a 29 20 2a  loc( strlen(z) *
34b6f 20 73 69 7a 65 6f 66 28 61 54 6f 6b 65 6e 5b 30   sizeof(aToken[0
34b70 5d 29 20 29 3b 0a 20 20 69 6e 74 20 6e 20 3d 20  ]) );.  int n = 
34b71 31 3b 0a 20 20 69 6e 74 20 65 2c 20 69 3b 0a 20  1;.  int e, i;. 
34b72 20 69 6e 74 20 74 6f 74 61 6c 53 69 7a 65 20 3d   int totalSize =
34b73 20 30 3b 0a 20 20 63 68 61 72 20 2a 2a 61 7a 54   0;.  char **azT
34b74 6f 6b 65 6e 3b 0a 20 20 63 68 61 72 20 2a 7a 43  oken;.  char *zC
34b75 6f 70 79 3b 0a 20 20 77 68 69 6c 65 28 20 6e 3e  opy;.  while( n>
34b76 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 66 74 73  0 ){.    n = fts
34b77 47 65 74 54 6f 6b 65 6e 28 7a 2c 20 26 65 29 3b  GetToken(z, &e);
34b78 0a 20 20 20 20 69 66 28 20 65 21 3d 54 4f 4b 45  .    if( e!=TOKE
34b79 4e 5f 53 50 41 43 45 20 29 7b 0a 20 20 20 20 20  N_SPACE ){.     
34b7a 20 61 54 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d 2e   aToken[nToken].
34b7b 7a 20 3d 20 7a 3b 0a 20 20 20 20 20 20 61 54 6f  z = z;.      aTo
34b7c 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d 2e 6e 20 3d 20  ken[nToken].n = 
34b7d 6e 3b 0a 20 20 20 20 20 20 6e 54 6f 6b 65 6e 2b  n;.      nToken+
34b7e 2b 3b 0a 20 20 20 20 20 20 74 6f 74 61 6c 53 69  +;.      totalSi
34b7f 7a 65 20 2b 3d 20 6e 2b 31 3b 0a 20 20 20 20 7d  ze += n+1;.    }
34b80 0a 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20 20 7d  .    z += n;.  }
34b81 0a 20 20 61 7a 54 6f 6b 65 6e 20 3d 20 28 63 68  .  azToken = (ch
34b82 61 72 2a 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  ar**)sqlite3_mal
34b83 6c 6f 63 28 20 6e 54 6f 6b 65 6e 2a 73 69 7a 65  loc( nToken*size
34b84 6f 66 28 63 68 61 72 2a 29 20 2b 20 74 6f 74 61  of(char*) + tota
34b85 6c 53 69 7a 65 20 29 3b 0a 20 20 7a 43 6f 70 79  lSize );.  zCopy
34b86 20 3d 20 28 63 68 61 72 2a 29 26 61 7a 54 6f 6b   = (char*)&azTok
34b87 65 6e 5b 6e 54 6f 6b 65 6e 5d 3b 0a 20 20 6e 54  en[nToken];.  nT
34b88 6f 6b 65 6e 2d 2d 3b 0a 20 20 66 6f 72 28 69 3d  oken--;.  for(i=
34b89 30 3b 20 69 3c 6e 54 6f 6b 65 6e 3b 20 69 2b 2b  0; i<nToken; i++
34b8a 29 7b 0a 20 20 20 20 61 7a 54 6f 6b 65 6e 5b 69  ){.    azToken[i
34b8b 5d 20 3d 20 7a 43 6f 70 79 3b 0a 20 20 20 20 6e  ] = zCopy;.    n
34b8c 20 3d 20 61 54 6f 6b 65 6e 5b 69 5d 2e 6e 3b 0a   = aToken[i].n;.
34b8d 20 20 20 20 6d 65 6d 63 70 79 28 7a 43 6f 70 79      memcpy(zCopy
34b8e 2c 20 61 54 6f 6b 65 6e 5b 69 5d 2e 7a 2c 20 6e  , aToken[i].z, n
34b8f 29 3b 0a 20 20 20 20 7a 43 6f 70 79 5b 6e 5d 20  );.    zCopy[n] 
34b90 3d 20 30 3b 0a 20 20 20 20 7a 43 6f 70 79 20 2b  = 0;.    zCopy +
34b91 3d 20 6e 2b 31 3b 0a 20 20 7d 0a 20 20 61 7a 54  = n+1;.  }.  azT
34b92 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d 20 3d 20 30  oken[nToken] = 0
34b93 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
34b94 28 61 54 6f 6b 65 6e 29 3b 0a 20 20 2a 70 6e 54  (aToken);.  *pnT
34b95 6f 6b 65 6e 20 3d 20 6e 54 6f 6b 65 6e 3b 0a 20  oken = nToken;. 
34b96 20 72 65 74 75 72 6e 20 61 7a 54 6f 6b 65 6e 3b   return azToken;
34b97 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
34b98 74 20 61 6e 20 53 51 4c 2d 73 74 79 6c 65 20 71  t an SQL-style q
34b99 75 6f 74 65 64 20 73 74 72 69 6e 67 20 69 6e 74  uoted string int
34b9a 6f 20 61 20 6e 6f 72 6d 61 6c 20 73 74 72 69 6e  o a normal strin
34b9b 67 20 62 79 20 72 65 6d 6f 76 69 6e 67 0a 2a 2a  g by removing.**
34b9c 20 74 68 65 20 71 75 6f 74 65 20 63 68 61 72 61   the quote chara
34b9d 63 74 65 72 73 2e 20 20 54 68 65 20 63 6f 6e 76  cters.  The conv
34b9e 65 72 73 69 6f 6e 20 69 73 20 64 6f 6e 65 20 69  ersion is done i
34b9f 6e 2d 70 6c 61 63 65 2e 20 20 49 66 20 74 68 65  n-place.  If the
34ba0 0a 2a 2a 20 69 6e 70 75 74 20 64 6f 65 73 20 6e  .** input does n
34ba1 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20 61 20  ot begin with a 
34ba2 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 2c  quote character,
34ba3 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
34ba4 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70  ne.** is a no-op
34ba5 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 73  ..**.** Examples
34ba6 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 22 61 62 63  :.**.**     "abc
34ba7 22 20 20 20 62 65 63 6f 6d 65 73 20 20 20 61 62  "   becomes   ab
34ba8 63 0a 2a 2a 20 20 20 20 20 27 78 79 7a 27 20 20  c.**     'xyz'  
34ba9 20 62 65 63 6f 6d 65 73 20 20 20 78 79 7a 0a 2a   becomes   xyz.*
34baa 2a 20 20 20 20 20 5b 70 71 72 5d 20 20 20 62 65  *     [pqr]   be
34bab 63 6f 6d 65 73 20 20 20 70 71 72 0a 2a 2a 20 20  comes   pqr.**  
34bac 20 20 20 60 6d 6e 6f 60 20 20 20 62 65 63 6f 6d     `mno`   becom
34bad 65 73 20 20 20 6d 6e 6f 0a 2a 2f 0a 73 74 61 74  es   mno.*/.stat
34bae 69 63 20 76 6f 69 64 20 64 65 71 75 6f 74 65 53  ic void dequoteS
34baf 74 72 69 6e 67 28 63 68 61 72 20 2a 7a 29 7b 0a  tring(char *z){.
34bb0 20 20 69 6e 74 20 71 75 6f 74 65 3b 0a 20 20 69    int quote;.  i
34bb1 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 66 28 20 7a  nt i, j;.  if( z
34bb2 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
34bb3 71 75 6f 74 65 20 3d 20 7a 5b 30 5d 3b 0a 20 20  quote = z[0];.  
34bb4 73 77 69 74 63 68 28 20 71 75 6f 74 65 20 29 7b  switch( quote ){
34bb5 0a 20 20 20 20 63 61 73 65 20 27 5c 27 27 3a 20  .    case '\'': 
34bb6 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
34bb7 20 27 22 27 3a 20 20 20 62 72 65 61 6b 3b 0a 20   '"':   break;. 
34bb8 20 20 20 63 61 73 65 20 27 60 27 3a 20 20 20 62     case '`':   b
34bb9 72 65 61 6b 3b 20 20 20 20 20 20 20 20 20 20 20  reak;           
34bba 20 20 20 20 20 2f 2a 20 46 6f 72 20 4d 79 53 51       /* For MySQ
34bbb 4c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20  L compatibility 
34bbc 2a 2f 0a 20 20 20 20 63 61 73 65 20 27 5b 27 3a  */.    case '[':
34bbd 20 20 20 71 75 6f 74 65 20 3d 20 27 5d 27 3b 20     quote = ']'; 
34bbe 20 62 72 65 61 6b 3b 20 20 2f 2a 20 46 6f 72 20   break;  /* For 
34bbf 4d 53 20 53 71 6c 53 65 72 76 65 72 20 63 6f 6d  MS SqlServer com
34bc0 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20  patibility */.  
34bc1 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 72 65    default:    re
34bc2 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  turn;.  }.  for(
34bc3 69 3d 31 2c 20 6a 3d 30 3b 20 7a 5b 69 5d 3b 20  i=1, j=0; z[i]; 
34bc4 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b  i++){.    if( z[
34bc5 69 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20  i]==quote ){.   
34bc6 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 71     if( z[i+1]==q
34bc7 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  uote ){.        
34bc8 7a 5b 6a 2b 2b 5d 20 3d 20 71 75 6f 74 65 3b 0a  z[j++] = quote;.
34bc9 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20          i++;.   
34bca 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
34bcb 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 30 3b 0a 20 20    z[j++] = 0;.  
34bcc 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
34bcd 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
34bce 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a        z[j++] = z
34bcf 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  [i];.    }.  }.}
34bd0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75  ../*.** The inpu
34bd1 74 20 61 7a 49 6e 20 69 73 20 61 20 4e 55 4c 4c  t azIn is a NULL
34bd2 2d 74 65 72 6d 69 6e 61 74 65 64 20 6c 69 73 74  -terminated list
34bd3 20 6f 66 20 74 6f 6b 65 6e 73 2e 20 20 52 65 6d   of tokens.  Rem
34bd4 6f 76 65 20 74 68 65 20 66 69 72 73 74 0a 2a 2a  ove the first.**
34bd5 20 74 6f 6b 65 6e 20 61 6e 64 20 61 6c 6c 20 70   token and all p
34bd6 75 6e 63 74 75 61 74 69 6f 6e 20 74 6f 6b 65 6e  unctuation token
34bd7 73 2e 20 20 52 65 6d 6f 76 65 20 74 68 65 20 71  s.  Remove the q
34bd8 75 6f 74 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 72  uotes from.** ar
34bd9 6f 75 6e 64 20 73 74 72 69 6e 67 20 6c 69 74 65  ound string lite
34bda 72 61 6c 20 74 6f 6b 65 6e 73 2e 0a 2a 2a 0a 2a  ral tokens..**.*
34bdb 2a 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a  * Example:.**.**
34bdc 20 20 20 20 20 69 6e 70 75 74 3a 20 20 20 20 20       input:     
34bdd 20 74 6f 6b 65 6e 69 7a 65 20 63 68 69 6e 65 73   tokenize chines
34bde 65 20 28 20 27 73 69 6d 70 6c 69 66 65 64 27 20  e ( 'simplifed' 
34bdf 2c 20 27 6d 69 78 65 64 27 20 29 0a 2a 2a 20 20  , 'mixed' ).**  
34be0 20 20 20 6f 75 74 70 75 74 3a 20 20 20 20 20 63     output:     c
34be1 68 69 6e 65 73 65 20 73 69 6d 70 6c 69 66 65 64  hinese simplifed
34be2 20 6d 69 78 65 64 0a 2a 2a 0a 2a 2a 20 41 6e 6f   mixed.**.** Ano
34be3 74 68 65 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a  ther example:.**
34be4 0a 2a 2a 20 20 20 20 20 69 6e 70 75 74 3a 20 20  .**     input:  
34be5 20 20 20 20 64 65 6c 69 6d 69 74 65 72 73 20 28      delimiters (
34be6 20 27 5b 27 20 2c 20 27 5d 27 20 2c 20 27 2e 2e   '[' , ']' , '..
34be7 2e 27 20 29 0a 2a 2a 20 20 20 20 20 6f 75 74 70  .' ).**     outp
34be8 75 74 3a 20 20 20 20 20 5b 20 5d 20 2e 2e 2e 0a  ut:     [ ] ....
34be9 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74  */.static void t
34bea 6f 6b 65 6e 4c 69 73 74 54 6f 49 64 4c 69 73 74  okenListToIdList
34beb 28 63 68 61 72 20 2a 2a 61 7a 49 6e 29 7b 0a 20  (char **azIn){. 
34bec 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 66 28   int i, j;.  if(
34bed 20 61 7a 49 6e 20 29 7b 0a 20 20 20 20 66 6f 72   azIn ){.    for
34bee 28 69 3d 30 2c 20 6a 3d 2d 31 3b 20 61 7a 49 6e  (i=0, j=-1; azIn
34bef 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  [i]; i++){.     
34bf0 20 69 66 28 20 73 61 66 65 5f 69 73 61 6c 6e 75   if( safe_isalnu
34bf1 6d 28 61 7a 49 6e 5b 69 5d 5b 30 5d 29 20 7c 7c  m(azIn[i][0]) ||
34bf2 20 61 7a 49 6e 5b 69 5d 5b 31 5d 20 29 7b 0a 20   azIn[i][1] ){. 
34bf3 20 20 20 20 20 20 20 64 65 71 75 6f 74 65 53 74         dequoteSt
34bf4 72 69 6e 67 28 61 7a 49 6e 5b 69 5d 29 3b 0a 20  ring(azIn[i]);. 
34bf5 20 20 20 20 20 20 20 69 66 28 20 6a 3e 3d 30 20         if( j>=0 
34bf6 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 7a 49  ){.          azI
34bf7 6e 5b 6a 5d 20 3d 20 61 7a 49 6e 5b 69 5d 3b 0a  n[j] = azIn[i];.
34bf8 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
34bf9 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20    j++;.      }. 
34bfa 20 20 20 7d 0a 20 20 20 20 61 7a 49 6e 5b 6a 5d     }.    azIn[j]
34bfb 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a   = 0;.  }.}.../*
34bfc 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 66 69 72  .** Find the fir
34bfd 73 74 20 61 6c 70 68 61 6e 75 6d 65 72 69 63 20  st alphanumeric 
34bfe 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 73 74 72  token in the str
34bff 69 6e 67 20 7a 49 6e 2e 20 20 4e 75 6c 6c 2d 74  ing zIn.  Null-t
34c00 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 74 68 69 73  erminate.** this
34c01 20 74 6f 6b 65 6e 2e 20 20 52 65 6d 6f 76 65 20   token.  Remove 
34c02 61 6e 79 20 71 75 6f 74 61 74 69 6f 6e 20 6d 61  any quotation ma
34c03 72 6b 73 2e 20 20 41 6e 64 20 72 65 74 75 72 6e  rks.  And return
34c04 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a   a pointer to.**
34c05 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a   the result..*/.
34c06 73 74 61 74 69 63 20 63 68 61 72 20 2a 66 69 72  static char *fir
34c07 73 74 54 6f 6b 65 6e 28 63 68 61 72 20 2a 7a 49  stToken(char *zI
34c08 6e 2c 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c  n, char **pzTail
34c09 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 74 74 79 70  ){.  int n, ttyp
34c0a 65 3b 0a 20 20 77 68 69 6c 65 28 31 29 7b 0a 20  e;.  while(1){. 
34c0b 20 20 20 6e 20 3d 20 66 74 73 47 65 74 54 6f 6b     n = ftsGetTok
34c0c 65 6e 28 7a 49 6e 2c 20 26 74 74 79 70 65 29 3b  en(zIn, &ttype);
34c0d 0a 20 20 20 20 69 66 28 20 74 74 79 70 65 3d 3d  .    if( ttype==
34c0e 54 4f 4b 45 4e 5f 53 50 41 43 45 20 29 7b 0a 20  TOKEN_SPACE ){. 
34c0f 20 20 20 20 20 7a 49 6e 20 2b 3d 20 6e 3b 0a 20       zIn += n;. 
34c10 20 20 20 7d 65 6c 73 65 20 69 66 28 20 74 74 79     }else if( tty
34c11 70 65 3d 3d 54 4f 4b 45 4e 5f 45 4f 46 20 29 7b  pe==TOKEN_EOF ){
34c12 0a 20 20 20 20 20 20 2a 70 7a 54 61 69 6c 20 3d  .      *pzTail =
34c13 20 7a 49 6e 3b 0a 20 20 20 20 20 20 72 65 74 75   zIn;.      retu
34c14 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  rn 0;.    }else{
34c15 0a 20 20 20 20 20 20 7a 49 6e 5b 6e 5d 20 3d 20  .      zIn[n] = 
34c16 30 3b 0a 20 20 20 20 20 20 2a 70 7a 54 61 69 6c  0;.      *pzTail
34c17 20 3d 20 26 7a 49 6e 5b 31 5d 3b 0a 20 20 20 20   = &zIn[1];.    
34c18 20 20 64 65 71 75 6f 74 65 53 74 72 69 6e 67 28    dequoteString(
34c19 7a 49 6e 29 3b 0a 20 20 20 20 20 20 72 65 74 75  zIn);.      retu
34c1a 72 6e 20 7a 49 6e 3b 0a 20 20 20 20 7d 0a 20 20  rn zIn;.    }.  
34c1b 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48 45 44  }.  /*NOTREACHED
34c1c 2a 2f 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 20  */.}../* Return 
34c1d 74 72 75 65 20 69 66 2e 2e 2e 0a 2a 2a 0a 2a 2a  true if....**.**
34c1e 20 20 20 2a 20 20 73 20 62 65 67 69 6e 73 20 77     *  s begins w
34c1f 69 74 68 20 74 68 65 20 73 74 72 69 6e 67 20 74  ith the string t
34c20 2c 20 69 67 6e 6f 72 69 6e 67 20 63 61 73 65 0a  , ignoring case.
34c21 2a 2a 20 20 20 2a 20 20 73 20 69 73 20 6c 6f 6e  **   *  s is lon
34c22 67 65 72 20 74 68 61 6e 20 74 0a 2a 2a 20 20 20  ger than t.**   
34c23 2a 20 20 54 68 65 20 66 69 72 73 74 20 63 68 61  *  The first cha
34c24 72 61 63 74 65 72 20 6f 66 20 73 20 62 65 79 6f  racter of s beyo
34c25 6e 64 20 74 20 69 73 20 6e 6f 74 20 61 20 61 6c  nd t is not a al
34c26 70 68 61 6e 75 6d 65 72 69 63 0a 2a 2a 20 0a 2a  phanumeric.** .*
34c27 2a 20 49 67 6e 6f 72 65 20 6c 65 61 64 69 6e 67  * Ignore leading
34c28 20 73 70 61 63 65 20 69 6e 20 2a 73 2e 0a 2a 2a   space in *s..**
34c29 0a 2a 2a 20 54 6f 20 70 75 74 20 69 74 20 61 6e  .** To put it an
34c2a 6f 74 68 65 72 20 77 61 79 2c 20 72 65 74 75 72  other way, retur
34c2b 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 66 69  n true if the fi
34c2c 72 73 74 20 74 6f 6b 65 6e 20 6f 66 0a 2a 2a 20  rst token of.** 
34c2d 73 5b 5d 20 69 73 20 74 5b 5d 2e 0a 2a 2f 0a 73  s[] is t[]..*/.s
34c2e 74 61 74 69 63 20 69 6e 74 20 73 74 61 72 74 73  tatic int starts
34c2f 57 69 74 68 28 63 6f 6e 73 74 20 63 68 61 72 20  With(const char 
34c30 2a 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  *s, const char *
34c31 74 29 7b 0a 20 20 77 68 69 6c 65 28 20 73 61 66  t){.  while( saf
34c32 65 5f 69 73 73 70 61 63 65 28 2a 73 29 20 29 7b  e_isspace(*s) ){
34c33 20 73 2b 2b 3b 20 7d 0a 20 20 77 68 69 6c 65 28   s++; }.  while(
34c34 20 2a 74 20 29 7b 0a 20 20 20 20 69 66 28 20 73   *t ){.    if( s
34c35 61 66 65 5f 74 6f 6c 6f 77 65 72 28 2a 73 2b 2b  afe_tolower(*s++
34c36 29 21 3d 73 61 66 65 5f 74 6f 6c 6f 77 65 72 28  )!=safe_tolower(
34c37 2a 74 2b 2b 29 20 29 20 72 65 74 75 72 6e 20 30  *t++) ) return 0
34c38 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a  ;.  }.  return *
34c39 73 21 3d 27 5f 27 20 26 26 20 21 73 61 66 65 5f  s!='_' && !safe_
34c3a 69 73 61 6c 6e 75 6d 28 2a 73 29 3b 0a 7d 0a 0a  isalnum(*s);.}..
34c3b 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
34c3c 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  e of this struct
34c3d 75 72 65 20 64 65 66 69 6e 65 73 20 74 68 65 20  ure defines the 
34c3e 22 73 70 65 63 22 20 6f 66 20 61 0a 2a 2a 20 66  "spec" of a.** f
34c3f 75 6c 6c 20 74 65 78 74 20 69 6e 64 65 78 2e 20  ull text index. 
34c40 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20   This structure 
34c41 69 73 20 70 6f 70 75 6c 61 74 65 64 20 62 79 20  is populated by 
34c42 70 61 72 73 65 53 70 65 63 0a 2a 2a 20 61 6e 64  parseSpec.** and
34c43 20 75 73 65 20 62 79 20 66 75 6c 6c 74 65 78 74   use by fulltext
34c44 43 6f 6e 6e 65 63 74 20 61 6e 64 20 66 75 6c 6c  Connect and full
34c45 74 65 78 74 43 72 65 61 74 65 2e 0a 2a 2f 0a 74  textCreate..*/.t
34c46 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 61  ypedef struct Ta
34c47 62 6c 65 53 70 65 63 20 7b 0a 20 20 63 6f 6e 73  bleSpec {.  cons
34c48 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20  t char *zDb;    
34c49 20 20 20 20 20 2f 2a 20 4c 6f 67 69 63 61 6c 20       /* Logical 
34c4a 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f  database name */
34c4b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
34c4c 4e 61 6d 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e  Name;       /* N
34c4d 61 6d 65 20 6f 66 20 74 68 65 20 66 75 6c 6c 2d  ame of the full-
34c4e 74 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a 20 20  text index */.  
34c4f 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20  int nColumn;    
34c50 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
34c51 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f  er of columns to
34c52 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20   be indexed */. 
34c53 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 75 6d 6e   char **azColumn
34c54 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 69  ;         /* Ori
34c55 67 69 6e 61 6c 20 6e 61 6d 65 73 20 6f 66 20 63  ginal names of c
34c56 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e 64  olumns to be ind
34c57 65 78 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a  exed */.  char *
34c58 2a 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e  *azContentColumn
34c59 3b 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d  ;  /* Column nam
34c5a 65 73 20 66 6f 72 20 25 5f 63 6f 6e 74 65 6e 74  es for %_content
34c5b 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 54   */.  char **azT
34c5c 6f 6b 65 6e 69 7a 65 72 3b 20 20 20 20 20 20 2f  okenizer;      /
34c5d 2a 20 4e 61 6d 65 20 6f 66 20 74 6f 6b 65 6e 69  * Name of tokeni
34c5e 7a 65 72 20 61 6e 64 20 69 74 73 20 61 72 67 75  zer and its argu
34c5f 6d 65 6e 74 73 20 2a 2f 0a 7d 20 54 61 62 6c 65  ments */.} Table
34c60 53 70 65 63 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 63  Spec;../*.** Rec
34c61 6c 61 69 6d 20 61 6c 6c 20 6f 66 20 74 68 65 20  laim all of the 
34c62 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20 61  memory used by a
34c63 20 54 61 62 6c 65 53 70 65 63 0a 2a 2f 0a 73 74   TableSpec.*/.st
34c64 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 54  atic void clearT
34c65 61 62 6c 65 53 70 65 63 28 54 61 62 6c 65 53 70  ableSpec(TableSp
34c66 65 63 20 2a 70 29 20 7b 0a 20 20 73 71 6c 69 74  ec *p) {.  sqlit
34c67 65 33 5f 66 72 65 65 28 70 2d 3e 61 7a 43 6f 6c  e3_free(p->azCol
34c68 75 6d 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  umn);.  sqlite3_
34c69 66 72 65 65 28 70 2d 3e 61 7a 43 6f 6e 74 65 6e  free(p->azConten
34c6a 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20 73 71 6c 69  tColumn);.  sqli
34c6b 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 7a 54 6f  te3_free(p->azTo
34c6c 6b 65 6e 69 7a 65 72 29 3b 0a 7d 0a 0a 2f 2a 20  kenizer);.}../* 
34c6d 50 61 72 73 65 20 61 20 43 52 45 41 54 45 20 56  Parse a CREATE V
34c6e 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61  IRTUAL TABLE sta
34c6f 74 65 6d 65 6e 74 2c 20 77 68 69 63 68 20 6c 6f  tement, which lo
34c70 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20  oks like this:. 
34c71 2a 0a 20 2a 20 43 52 45 41 54 45 20 56 49 52 54  *. * CREATE VIRT
34c72 55 41 4c 20 54 41 42 4c 45 20 65 6d 61 69 6c 0a  UAL TABLE email.
34c73 20 2a 20 20 20 20 20 20 20 20 55 53 49 4e 47 20   *        USING 
34c74 66 74 73 33 28 73 75 62 6a 65 63 74 2c 20 62 6f  fts3(subject, bo
34c75 64 79 2c 20 74 6f 6b 65 6e 69 7a 65 20 6d 79 74  dy, tokenize myt
34c76 6f 6b 65 6e 69 7a 65 72 28 6d 79 61 72 67 29 29  okenizer(myarg))
34c77 0a 20 2a 0a 20 2a 20 57 65 20 72 65 74 75 72 6e  . *. * We return
34c78 20 70 61 72 73 65 64 20 69 6e 66 6f 72 6d 61 74   parsed informat
34c79 69 6f 6e 20 69 6e 20 61 20 54 61 62 6c 65 53 70  ion in a TableSp
34c7a 65 63 20 73 74 72 75 63 74 75 72 65 2e 0a 20 2a  ec structure.. *
34c7b 20 0a 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74   . */.static int
34c7c 20 70 61 72 73 65 53 70 65 63 28 54 61 62 6c 65   parseSpec(Table
34c7d 53 70 65 63 20 2a 70 53 70 65 63 2c 20 69 6e 74  Spec *pSpec, int
34c7e 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61   argc, const cha
34c7f 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20  r *const*argv,. 
34c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34c81 20 20 20 20 63 68 61 72 2a 2a 70 7a 45 72 72 29      char**pzErr)
34c82 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20  {.  int i, n;.  
34c83 63 68 61 72 20 2a 7a 2c 20 2a 7a 44 75 6d 6d 79  char *z, *zDummy
34c84 3b 0a 20 20 63 68 61 72 20 2a 2a 61 7a 41 72 67  ;.  char **azArg
34c85 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
34c86 7a 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 30 3b 20  zTokenizer = 0; 
34c87 20 20 20 2f 2a 20 61 72 67 76 5b 5d 20 65 6e 74     /* argv[] ent
34c88 72 79 20 64 65 73 63 72 69 62 69 6e 67 20 74 68  ry describing th
34c89 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 0a  e tokenizer */..
34c8a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3e 3d    assert( argc>=
34c8b 33 20 29 3b 0a 20 20 2f 2a 20 43 75 72 72 65 6e  3 );.  /* Curren
34c8c 74 20 69 6e 74 65 72 66 61 63 65 3a 0a 20 20 2a  t interface:.  *
34c8d 2a 20 61 72 67 76 5b 30 5d 20 2d 20 6d 6f 64 75  * argv[0] - modu
34c8e 6c 65 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 72 67  le name.  ** arg
34c8f 76 5b 31 5d 20 2d 20 64 61 74 61 62 61 73 65 20  v[1] - database 
34c90 6e 61 6d 65 0a 20 20 2a 2a 20 61 72 67 76 5b 32  name.  ** argv[2
34c91 5d 20 2d 20 74 61 62 6c 65 20 6e 61 6d 65 0a 20  ] - table name. 
34c92 20 2a 2a 20 61 72 67 76 5b 33 2e 2e 5d 20 2d 20   ** argv[3..] - 
34c93 63 6f 6c 75 6d 6e 73 2c 20 6f 70 74 69 6f 6e 61  columns, optiona
34c94 6c 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  lly followed by 
34c95 74 6f 6b 65 6e 69 7a 65 72 20 73 70 65 63 69 66  tokenizer specif
34c96 69 63 61 74 69 6f 6e 0a 20 20 2a 2a 20 20 20 20  ication.  **    
34c97 20 20 20 20 20 20 20 20 20 61 6e 64 20 73 6e 69           and sni
34c98 70 70 65 74 20 64 65 6c 69 6d 69 74 65 72 73 20  ppet delimiters 
34c99 73 70 65 63 69 66 69 63 61 74 69 6f 6e 2e 0a 20  specification.. 
34c9a 20 2a 2f 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 61   */..  /* Make a
34c9b 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 6f 6d   copy of the com
34c9c 70 6c 65 74 65 20 61 72 67 76 5b 5d 5b 5d 20 61  plete argv[][] a
34c9d 72 72 61 79 20 69 6e 20 61 20 73 69 6e 67 6c 65  rray in a single
34c9e 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 20 20 2a   allocation..  *
34c9f 2a 20 54 68 65 20 61 72 67 76 5b 5d 5b 5d 20 61  * The argv[][] a
34ca0 72 72 61 79 20 69 73 20 72 65 61 64 2d 6f 6e 6c  rray is read-onl
34ca1 79 20 61 6e 64 20 74 72 61 6e 73 69 65 6e 74 2e  y and transient.
34ca2 20 20 57 65 20 63 61 6e 20 77 72 69 74 65 20 74    We can write t
34ca3 6f 20 74 68 65 0a 20 20 2a 2a 20 63 6f 70 79 20  o the.  ** copy 
34ca4 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d 6f 64 69  in order to modi
34ca5 66 79 20 74 68 69 6e 67 73 20 61 6e 64 20 74 68  fy things and th
34ca6 65 20 63 6f 70 79 20 69 73 20 70 65 72 73 69 73  e copy is persis
34ca7 74 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 43 4c 45  tent..  */.  CLE
34ca8 41 52 28 70 53 70 65 63 29 3b 0a 20 20 66 6f 72  AR(pSpec);.  for
34ca9 28 69 3d 6e 3d 30 3b 20 69 3c 61 72 67 63 3b 20  (i=n=0; i<argc; 
34caa 69 2b 2b 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 73  i++){.    n += s
34cab 74 72 6c 65 6e 28 61 72 67 76 5b 69 5d 29 20 2b  trlen(argv[i]) +
34cac 20 31 3b 0a 20 20 7d 0a 20 20 61 7a 41 72 67 20   1;.  }.  azArg 
34cad 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
34cae 28 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a  ( sizeof(char*)*
34caf 61 72 67 63 20 2b 20 6e 20 29 3b 0a 20 20 69 66  argc + n );.  if
34cb0 28 20 61 7a 41 72 67 3d 3d 30 20 29 7b 0a 20 20  ( azArg==0 ){.  
34cb1 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
34cb2 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 7a 20 3d  NOMEM;.  }.  z =
34cb3 20 28 63 68 61 72 2a 29 26 61 7a 41 72 67 5b 61   (char*)&azArg[a
34cb4 72 67 63 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  rgc];.  for(i=0;
34cb5 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20   i<argc; i++){. 
34cb6 20 20 20 61 7a 41 72 67 5b 69 5d 20 3d 20 7a 3b     azArg[i] = z;
34cb7 0a 20 20 20 20 73 74 72 63 70 79 28 7a 2c 20 61  .    strcpy(z, a
34cb8 72 67 76 5b 69 5d 29 3b 0a 20 20 20 20 7a 20 2b  rgv[i]);.    z +
34cb9 3d 20 73 74 72 6c 65 6e 28 7a 29 2b 31 3b 0a 20  = strlen(z)+1;. 
34cba 20 7d 0a 0a 20 20 2f 2a 20 49 64 65 6e 74 69 66   }..  /* Identif
34cbb 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  y the column nam
34cbc 65 73 20 61 6e 64 20 74 68 65 20 74 6f 6b 65 6e  es and the token
34cbd 69 7a 65 72 20 61 6e 64 20 64 65 6c 69 6d 69 74  izer and delimit
34cbe 65 72 20 61 72 67 75 6d 65 6e 74 73 0a 20 20 2a  er arguments.  *
34cbf 2a 20 69 6e 20 74 68 65 20 61 72 67 76 5b 5d 5b  * in the argv[][
34cc0 5d 20 61 72 72 61 79 2e 0a 20 20 2a 2f 0a 20 20  ] array..  */.  
34cc1 70 53 70 65 63 2d 3e 7a 44 62 20 3d 20 61 7a 41  pSpec->zDb = azA
34cc2 72 67 5b 31 5d 3b 0a 20 20 70 53 70 65 63 2d 3e  rg[1];.  pSpec->
34cc3 7a 4e 61 6d 65 20 3d 20 61 7a 41 72 67 5b 32 5d  zName = azArg[2]
34cc4 3b 0a 20 20 70 53 70 65 63 2d 3e 6e 43 6f 6c 75  ;.  pSpec->nColu
34cc5 6d 6e 20 3d 20 30 3b 0a 20 20 70 53 70 65 63 2d  mn = 0;.  pSpec-
34cc6 3e 61 7a 43 6f 6c 75 6d 6e 20 3d 20 61 7a 41 72  >azColumn = azAr
34cc7 67 3b 0a 20 20 7a 54 6f 6b 65 6e 69 7a 65 72 20  g;.  zTokenizer 
34cc8 3d 20 22 74 6f 6b 65 6e 69 7a 65 20 73 69 6d 70  = "tokenize simp
34cc9 6c 65 22 3b 0a 20 20 66 6f 72 28 69 3d 33 3b 20  le";.  for(i=3; 
34cca 69 3c 61 72 67 63 3b 20 2b 2b 69 29 7b 0a 20 20  i<argc; ++i){.  
34ccb 20 20 69 66 28 20 73 74 61 72 74 73 57 69 74 68    if( startsWith
34ccc 28 61 7a 41 72 67 5b 69 5d 2c 22 74 6f 6b 65 6e  (azArg[i],"token
34ccd 69 7a 65 22 29 20 29 7b 0a 20 20 20 20 20 20 7a  ize") ){.      z
34cce 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 61 7a 41 72  Tokenizer = azAr
34ccf 67 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  g[i];.    }else{
34cd0 0a 20 20 20 20 20 20 7a 20 3d 20 61 7a 41 72 67  .      z = azArg
34cd1 5b 70 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 5d  [pSpec->nColumn]
34cd2 20 3d 20 66 69 72 73 74 54 6f 6b 65 6e 28 61 7a   = firstToken(az
34cd3 41 72 67 5b 69 5d 2c 20 26 7a 44 75 6d 6d 79 29  Arg[i], &zDummy)
34cd4 3b 0a 20 20 20 20 20 20 70 53 70 65 63 2d 3e 6e  ;.      pSpec->n
34cd5 43 6f 6c 75 6d 6e 2b 2b 3b 0a 20 20 20 20 7d 0a  Column++;.    }.
34cd6 20 20 7d 0a 20 20 69 66 28 20 70 53 70 65 63 2d    }.  if( pSpec-
34cd7 3e 6e 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20  >nColumn==0 ){. 
34cd8 20 20 20 61 7a 41 72 67 5b 30 5d 20 3d 20 22 63     azArg[0] = "c
34cd9 6f 6e 74 65 6e 74 22 3b 0a 20 20 20 20 70 53 70  ontent";.    pSp
34cda 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 31 3b  ec->nColumn = 1;
34cdb 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  .  }..  /*.  ** 
34cdc 43 6f 6e 73 74 72 75 63 74 20 74 68 65 20 6c 69  Construct the li
34cdd 73 74 20 6f 66 20 63 6f 6e 74 65 6e 74 20 63 6f  st of content co
34cde 6c 75 6d 6e 20 6e 61 6d 65 73 2e 0a 20 20 2a 2a  lumn names..  **
34cdf 0a 20 20 2a 2a 20 45 61 63 68 20 63 6f 6e 74 65  .  ** Each conte
34ce0 6e 74 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 77  nt column name w
34ce1 69 6c 6c 20 62 65 20 6f 66 20 74 68 65 20 66 6f  ill be of the fo
34ce2 72 6d 20 63 4e 4e 41 41 41 41 0a 20 20 2a 2a 20  rm cNNAAAA.  ** 
34ce3 77 68 65 72 65 20 4e 4e 20 69 73 20 74 68 65 20  where NN is the 
34ce4 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 61 6e  column number an
34ce5 64 20 41 41 41 41 20 69 73 20 74 68 65 20 73 61  d AAAA is the sa
34ce6 6e 69 74 69 7a 65 64 0a 20 20 2a 2a 20 63 6f 6c  nitized.  ** col
34ce7 75 6d 6e 20 6e 61 6d 65 2e 20 20 22 73 61 6e 69  umn name.  "sani
34ce8 74 69 7a 65 64 22 20 6d 65 61 6e 73 20 74 68 61  tized" means tha
34ce9 74 20 73 70 65 63 69 61 6c 20 63 68 61 72 61 63  t special charac
34cea 74 65 72 73 20 61 72 65 0a 20 20 2a 2a 20 63 6f  ters are.  ** co
34ceb 6e 76 65 72 74 65 64 20 74 6f 20 22 5f 22 2e 20  nverted to "_". 
34cec 20 54 68 65 20 63 4e 4e 20 70 72 65 66 69 78 20   The cNN prefix 
34ced 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20  guarantees that 
34cee 61 6c 6c 20 63 6f 6c 75 6d 6e 0a 20 20 2a 2a 20  all column.  ** 
34cef 6e 61 6d 65 73 20 61 72 65 20 75 6e 69 71 75 65  names are unique
34cf0 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
34cf1 41 41 41 41 20 73 75 66 66 69 78 20 69 73 20 6e  AAAA suffix is n
34cf2 6f 74 20 73 74 72 69 63 74 6c 79 20 6e 65 63 65  ot strictly nece
34cf3 73 73 61 72 79 2e 20 20 49 74 20 69 73 20 69 6e  ssary.  It is in
34cf4 63 6c 75 64 65 64 0a 20 20 2a 2a 20 66 6f 72 20  cluded.  ** for 
34cf5 74 68 65 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20  the convenience 
34cf6 6f 66 20 70 65 6f 70 6c 65 20 77 68 6f 20 6d 69  of people who mi
34cf7 67 68 74 20 65 78 61 6d 69 6e 65 20 74 68 65 20  ght examine the 
34cf8 67 65 6e 65 72 61 74 65 64 0a 20 20 2a 2a 20 25  generated.  ** %
34cf9 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 20 61  _content table a
34cfa 6e 64 20 77 6f 6e 64 65 72 20 77 68 61 74 20 74  nd wonder what t
34cfb 68 65 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 75  he columns are u
34cfc 73 65 64 20 66 6f 72 2e 0a 20 20 2a 2f 0a 20 20  sed for..  */.  
34cfd 70 53 70 65 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74  pSpec->azContent
34cfe 43 6f 6c 75 6d 6e 20 3d 20 73 71 6c 69 74 65 33  Column = sqlite3
34cff 5f 6d 61 6c 6c 6f 63 28 20 70 53 70 65 63 2d 3e  _malloc( pSpec->
34d00 6e 43 6f 6c 75 6d 6e 20 2a 20 73 69 7a 65 6f 66  nColumn * sizeof
34d01 28 63 68 61 72 20 2a 29 20 29 3b 0a 20 20 69 66  (char *) );.  if
34d02 28 20 70 53 70 65 63 2d 3e 61 7a 43 6f 6e 74 65  ( pSpec->azConte
34d03 6e 74 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20  ntColumn==0 ){. 
34d04 20 20 20 63 6c 65 61 72 54 61 62 6c 65 53 70 65     clearTableSpe
34d05 63 28 70 53 70 65 63 29 3b 0a 20 20 20 20 72 65  c(pSpec);.    re
34d06 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
34d07 4d 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  M;.  }.  for(i=0
34d08 3b 20 69 3c 70 53 70 65 63 2d 3e 6e 43 6f 6c 75  ; i<pSpec->nColu
34d09 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68  mn; i++){.    ch
34d0a 61 72 20 2a 70 3b 0a 20 20 20 20 70 53 70 65 63  ar *p;.    pSpec
34d0b 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d  ->azContentColum
34d0c 6e 5b 69 5d 20 3d 20 73 71 6c 69 74 65 33 5f 6d  n[i] = sqlite3_m
34d0d 70 72 69 6e 74 66 28 22 63 25 64 25 73 22 2c 20  printf("c%d%s", 
34d0e 69 2c 20 61 7a 41 72 67 5b 69 5d 29 3b 0a 20 20  i, azArg[i]);.  
34d0f 20 20 66 6f 72 20 28 70 20 3d 20 70 53 70 65 63    for (p = pSpec
34d10 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d  ->azContentColum
34d11 6e 5b 69 5d 3b 20 2a 70 20 3b 20 2b 2b 70 29 20  n[i]; *p ; ++p) 
34d12 7b 0a 20 20 20 20 20 20 69 66 28 20 21 73 61 66  {.      if( !saf
34d13 65 5f 69 73 61 6c 6e 75 6d 28 2a 70 29 20 29 20  e_isalnum(*p) ) 
34d14 2a 70 20 3d 20 27 5f 27 3b 0a 20 20 20 20 7d 0a  *p = '_';.    }.
34d15 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50    }..  /*.  ** P
34d16 61 72 73 65 20 74 68 65 20 74 6f 6b 65 6e 69 7a  arse the tokeniz
34d17 65 72 20 73 70 65 63 69 66 69 63 61 74 69 6f 6e  er specification
34d18 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20   string..  */.  
34d19 70 53 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a  pSpec->azTokeniz
34d1a 65 72 20 3d 20 74 6f 6b 65 6e 69 7a 65 53 74 72  er = tokenizeStr
34d1b 69 6e 67 28 7a 54 6f 6b 65 6e 69 7a 65 72 2c 20  ing(zTokenizer, 
34d1c 26 6e 29 3b 0a 20 20 74 6f 6b 65 6e 4c 69 73 74  &n);.  tokenList
34d1d 54 6f 49 64 4c 69 73 74 28 70 53 70 65 63 2d 3e  ToIdList(pSpec->
34d1e 61 7a 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 0a 20  azTokenizer);.. 
34d1f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
34d20 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  K;.}../*.** Gene
34d21 72 61 74 65 20 61 20 43 52 45 41 54 45 20 54 41  rate a CREATE TA
34d22 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 74 68  BLE statement th
34d23 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65  at describes the
34d24 20 73 63 68 65 6d 61 20 6f 66 0a 2a 2a 20 74 68   schema of.** th
34d25 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
34d26 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74    Return a point
34d27 65 72 20 74 6f 20 74 68 69 73 20 73 63 68 65 6d  er to this schem
34d28 61 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  a string..**.** 
34d29 53 70 61 63 65 20 69 73 20 6f 62 74 61 69 6e 65  Space is obtaine
34d2a 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d  d from sqlite3_m
34d2b 70 72 69 6e 74 66 28 29 20 61 6e 64 20 73 68 6f  printf() and sho
34d2c 75 6c 64 20 62 65 20 66 72 65 65 64 0a 2a 2a 20  uld be freed.** 
34d2d 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 72  using sqlite3_fr
34d2e 65 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ee()..*/.static 
34d2f 63 68 61 72 20 2a 66 75 6c 6c 74 65 78 74 53 63  char *fulltextSc
34d30 68 65 6d 61 28 0a 20 20 69 6e 74 20 6e 43 6f 6c  hema(.  int nCol
34d31 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  umn,            
34d32 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
34d33 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20  of columns */.  
34d34 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
34d35 74 2a 20 61 7a 43 6f 6c 75 6d 6e 2c 20 20 2f 2a  t* azColumn,  /*
34d36 20 4c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73   List of columns
34d37 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
34d38 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 20 20 20 20   *zTableName    
34d39 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
34d3a 68 65 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20  he table */.){. 
34d3b 20 69 6e 74 20 69 3b 0a 20 20 63 68 61 72 20 2a   int i;.  char *
34d3c 7a 53 63 68 65 6d 61 2c 20 2a 7a 4e 65 78 74 3b  zSchema, *zNext;
34d3d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
34d3e 53 65 70 20 3d 20 22 28 22 3b 0a 20 20 7a 53 63  Sep = "(";.  zSc
34d3f 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 33 5f 6d  hema = sqlite3_m
34d40 70 72 69 6e 74 66 28 22 43 52 45 41 54 45 20 54  printf("CREATE T
34d41 41 42 4c 45 20 78 22 29 3b 0a 20 20 66 6f 72 28  ABLE x");.  for(
34d42 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20  i=0; i<nColumn; 
34d43 69 2b 2b 29 7b 0a 20 20 20 20 7a 4e 65 78 74 20  i++){.    zNext 
34d44 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
34d45 66 28 22 25 73 25 73 25 51 22 2c 20 7a 53 63 68  f("%s%s%Q", zSch
34d46 65 6d 61 2c 20 7a 53 65 70 2c 20 61 7a 43 6f 6c  ema, zSep, azCol
34d47 75 6d 6e 5b 69 5d 29 3b 0a 20 20 20 20 73 71 6c  umn[i]);.    sql
34d48 69 74 65 33 5f 66 72 65 65 28 7a 53 63 68 65 6d  ite3_free(zSchem
34d49 61 29 3b 0a 20 20 20 20 7a 53 63 68 65 6d 61 20  a);.    zSchema 
34d4a 3d 20 7a 4e 65 78 74 3b 0a 20 20 20 20 7a 53 65  = zNext;.    zSe
34d4b 70 20 3d 20 22 2c 22 3b 0a 20 20 7d 0a 20 20 7a  p = ",";.  }.  z
34d4c 4e 65 78 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Next = sqlite3_m
34d4d 70 72 69 6e 74 66 28 22 25 73 2c 25 51 20 48 49  printf("%s,%Q HI
34d4e 44 44 45 4e 22 2c 20 7a 53 63 68 65 6d 61 2c 20  DDEN", zSchema, 
34d4f 7a 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 73  zTableName);.  s
34d50 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 63 68  qlite3_free(zSch
34d51 65 6d 61 29 3b 0a 20 20 7a 53 63 68 65 6d 61 20  ema);.  zSchema 
34d52 3d 20 7a 4e 65 78 74 3b 0a 20 20 7a 4e 65 78 74  = zNext;.  zNext
34d53 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
34d54 74 66 28 22 25 73 2c 64 6f 63 69 64 20 48 49 44  tf("%s,docid HID
34d55 44 45 4e 29 22 2c 20 7a 53 63 68 65 6d 61 29 3b  DEN)", zSchema);
34d56 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
34d57 7a 53 63 68 65 6d 61 29 3b 0a 20 20 72 65 74 75  zSchema);.  retu
34d58 72 6e 20 7a 4e 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a  rn zNext;.}../*.
34d59 2a 2a 20 42 75 69 6c 64 20 61 20 6e 65 77 20 73  ** Build a new s
34d5a 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75  qlite3_vtab stru
34d5b 63 74 75 72 65 20 74 68 61 74 20 77 69 6c 6c 20  cture that will 
34d5c 64 65 73 63 72 69 62 65 20 74 68 65 0a 2a 2a 20  describe the.** 
34d5d 66 75 6c 6c 74 65 78 74 20 69 6e 64 65 78 20 64  fulltext index d
34d5e 65 66 69 6e 65 64 20 62 79 20 73 70 65 63 2e 0a  efined by spec..
34d5f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f  */.static int co
34d60 6e 73 74 72 75 63 74 56 74 61 62 28 0a 20 20 73  nstructVtab(.  s
34d61 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
34d62 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
34d63 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20  SQLite database 
34d64 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
34d65 66 74 73 33 48 61 73 68 20 2a 70 48 61 73 68 2c  fts3Hash *pHash,
34d66 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73            /* Has
34d67 68 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69  h table containi
34d68 6e 67 20 74 6f 6b 65 6e 69 7a 65 72 73 20 2a 2f  ng tokenizers */
34d69 0a 20 20 54 61 62 6c 65 53 70 65 63 20 2a 73 70  .  TableSpec *sp
34d6a 65 63 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ec,          /* 
34d6b 50 61 72 73 65 64 20 73 70 65 63 20 69 6e 66 6f  Parsed spec info
34d6c 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 70 61 72  rmation from par
34d6d 73 65 53 70 65 63 28 29 20 2a 2f 0a 20 20 73 71  seSpec() */.  sq
34d6e 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56  lite3_vtab **ppV
34d6f 54 61 62 2c 20 20 20 20 2f 2a 20 57 72 69 74 65  Tab,    /* Write
34d70 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 76   the resulting v
34d71 74 61 62 20 73 74 72 75 63 74 75 72 65 20 68 65  tab structure he
34d72 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70  re */.  char **p
34d73 7a 45 72 72 20 20 20 20 20 20 20 20 20 20 20 20  zErr            
34d74 20 20 2f 2a 20 57 72 69 74 65 20 61 6e 79 20 65    /* Write any e
34d75 72 72 6f 72 20 6d 65 73 73 61 67 65 20 68 65 72  rror message her
34d76 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
34d77 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 66 75 6c  ;.  int n;.  ful
34d78 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20  ltext_vtab *v = 
34d79 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  0;.  const sqlit
34d7a 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
34d7b 75 6c 65 20 2a 6d 20 3d 20 4e 55 4c 4c 3b 0a 20  ule *m = NULL;. 
34d7c 20 63 68 61 72 20 2a 73 63 68 65 6d 61 3b 0a 0a   char *schema;..
34d7d 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54    char const *zT
34d7e 6f 6b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ok;         /* N
34d7f 61 6d 65 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 72  ame of tokenizer
34d80 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 69 73   to use for this
34d81 20 66 74 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20   fts table */.  
34d82 69 6e 74 20 6e 54 6f 6b 3b 20 20 20 20 20 20 20  int nTok;       
34d83 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
34d84 67 74 68 20 6f 66 20 7a 54 6f 6b 2c 20 69 6e 63  gth of zTok, inc
34d85 6c 75 64 69 6e 67 20 6e 75 6c 20 74 65 72 6d 69  luding nul termi
34d86 6e 61 74 6f 72 20 2a 2f 0a 0a 20 20 76 20 3d 20  nator */..  v = 
34d87 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
34d88 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  ) sqlite3_malloc
34d89 28 73 69 7a 65 6f 66 28 66 75 6c 6c 74 65 78 74  (sizeof(fulltext
34d8a 5f 76 74 61 62 29 29 3b 0a 20 20 69 66 28 20 76  _vtab));.  if( v
34d8b 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ==0 ) return SQL
34d8c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 43 4c 45  ITE_NOMEM;.  CLE
34d8d 41 52 28 76 29 3b 0a 20 20 2f 2a 20 73 71 6c 69  AR(v);.  /* sqli
34d8e 74 65 20 77 69 6c 6c 20 69 6e 69 74 69 61 6c 69  te will initiali
34d8f 7a 65 20 76 2d 3e 62 61 73 65 20 2a 2f 0a 20 20  ze v->base */.  
34d90 76 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 76 2d  v->db = db;.  v-
34d91 3e 7a 44 62 20 3d 20 73 70 65 63 2d 3e 7a 44 62  >zDb = spec->zDb
34d92 3b 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 64  ;       /* Freed
34d93 20 77 68 65 6e 20 61 7a 43 6f 6c 75 6d 6e 20 69   when azColumn i
34d94 73 20 66 72 65 65 64 20 2a 2f 0a 20 20 76 2d 3e  s freed */.  v->
34d95 7a 4e 61 6d 65 20 3d 20 73 70 65 63 2d 3e 7a 4e  zName = spec->zN
34d96 61 6d 65 3b 20 20 20 2f 2a 20 46 72 65 65 64 20  ame;   /* Freed 
34d97 77 68 65 6e 20 61 7a 43 6f 6c 75 6d 6e 20 69 73  when azColumn is
34d98 20 66 72 65 65 64 20 2a 2f 0a 20 20 76 2d 3e 6e   freed */.  v->n
34d99 43 6f 6c 75 6d 6e 20 3d 20 73 70 65 63 2d 3e 6e  Column = spec->n
34d9a 43 6f 6c 75 6d 6e 3b 0a 20 20 76 2d 3e 61 7a 43  Column;.  v->azC
34d9b 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 20 3d 20 73  ontentColumn = s
34d9c 70 65 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f  pec->azContentCo
34d9d 6c 75 6d 6e 3b 0a 20 20 73 70 65 63 2d 3e 61 7a  lumn;.  spec->az
34d9e 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 20 3d 20  ContentColumn = 
34d9f 30 3b 0a 20 20 76 2d 3e 61 7a 43 6f 6c 75 6d 6e  0;.  v->azColumn
34da0 20 3d 20 73 70 65 63 2d 3e 61 7a 43 6f 6c 75 6d   = spec->azColum
34da1 6e 3b 0a 20 20 73 70 65 63 2d 3e 61 7a 43 6f 6c  n;.  spec->azCol
34da2 75 6d 6e 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20  umn = 0;..  if( 
34da3 73 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65  spec->azTokenize
34da4 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  r==0 ){.    retu
34da5 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
34da6 0a 20 20 7d 0a 0a 20 20 7a 54 6f 6b 20 3d 20 73  .  }..  zTok = s
34da7 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72  pec->azTokenizer
34da8 5b 30 5d 3b 20 0a 20 20 69 66 28 20 21 7a 54 6f  [0]; .  if( !zTo
34da9 6b 20 29 7b 0a 20 20 20 20 7a 54 6f 6b 20 3d 20  k ){.    zTok = 
34daa 22 73 69 6d 70 6c 65 22 3b 0a 20 20 7d 0a 20 20  "simple";.  }.  
34dab 6e 54 6f 6b 20 3d 20 73 74 72 6c 65 6e 28 7a 54  nTok = strlen(zT
34dac 6f 6b 29 2b 31 3b 0a 0a 20 20 6d 20 3d 20 28 73  ok)+1;..  m = (s
34dad 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
34dae 5f 6d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65  _module *)sqlite
34daf 33 46 74 73 33 48 61 73 68 46 69 6e 64 28 70 48  3Fts3HashFind(pH
34db0 61 73 68 2c 20 7a 54 6f 6b 2c 20 6e 54 6f 6b 29  ash, zTok, nTok)
34db1 3b 0a 20 20 69 66 28 20 21 6d 20 29 7b 0a 20 20  ;.  if( !m ){.  
34db2 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74    *pzErr = sqlit
34db3 65 33 5f 6d 70 72 69 6e 74 66 28 22 75 6e 6b 6e  e3_mprintf("unkn
34db4 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 25  own tokenizer: %
34db5 73 22 2c 20 73 70 65 63 2d 3e 61 7a 54 6f 6b 65  s", spec->azToke
34db6 6e 69 7a 65 72 5b 30 5d 29 3b 0a 20 20 20 20 72  nizer[0]);.    r
34db7 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
34db8 3b 0a 20 20 20 20 67 6f 74 6f 20 65 72 72 3b 0a  ;.    goto err;.
34db9 20 20 7d 0a 0a 20 20 66 6f 72 28 6e 3d 30 3b 20    }..  for(n=0; 
34dba 73 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65  spec->azTokenize
34dbb 72 5b 6e 5d 3b 20 6e 2b 2b 29 7b 7d 0a 20 20 69  r[n]; n++){}.  i
34dbc 66 28 20 6e 20 29 7b 0a 20 20 20 20 72 63 20 3d  f( n ){.    rc =
34dbd 20 6d 2d 3e 78 43 72 65 61 74 65 28 6e 2d 31 2c   m->xCreate(n-1,
34dbe 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 63 6f 6e   (const char*con
34dbf 73 74 2a 29 26 73 70 65 63 2d 3e 61 7a 54 6f 6b  st*)&spec->azTok
34dc0 65 6e 69 7a 65 72 5b 31 5d 2c 0a 20 20 20 20 20  enizer[1],.     
34dc1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
34dc2 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a  v->pTokenizer);.
34dc3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
34dc4 3d 20 6d 2d 3e 78 43 72 65 61 74 65 28 30 2c 20  = m->xCreate(0, 
34dc5 30 2c 20 26 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65  0, &v->pTokenize
34dc6 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  r);.  }.  if( rc
34dc7 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
34dc8 74 6f 20 65 72 72 3b 0a 20 20 76 2d 3e 70 54 6f  to err;.  v->pTo
34dc9 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65  kenizer->pModule
34dca 20 3d 20 6d 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f   = m;..  /* TODO
34dcb 3a 20 76 65 72 69 66 79 20 74 68 65 20 65 78 69  : verify the exi
34dcc 73 74 65 6e 63 65 20 6f 66 20 62 61 63 6b 69 6e  stence of backin
34dcd 67 20 74 61 62 6c 65 73 20 66 6f 6f 5f 63 6f 6e  g tables foo_con
34dce 74 65 6e 74 2c 20 66 6f 6f 5f 74 65 72 6d 20 2a  tent, foo_term *
34dcf 2f 0a 0a 20 20 73 63 68 65 6d 61 20 3d 20 66 75  /..  schema = fu
34dd0 6c 6c 74 65 78 74 53 63 68 65 6d 61 28 76 2d 3e  lltextSchema(v->
34dd1 6e 43 6f 6c 75 6d 6e 2c 20 28 63 6f 6e 73 74 20  nColumn, (const 
34dd2 63 68 61 72 2a 63 6f 6e 73 74 2a 29 76 2d 3e 61  char*const*)v->a
34dd3 7a 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20 20 20  zColumn,.       
34dd4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34dd5 20 20 20 73 70 65 63 2d 3e 7a 4e 61 6d 65 29 3b     spec->zName);
34dd6 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
34dd7 64 65 63 6c 61 72 65 5f 76 74 61 62 28 64 62 2c  declare_vtab(db,
34dd8 20 73 63 68 65 6d 61 29 3b 0a 20 20 73 71 6c 69   schema);.  sqli
34dd9 74 65 33 5f 66 72 65 65 28 73 63 68 65 6d 61 29  te3_free(schema)
34dda 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
34ddb 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72  TE_OK ) goto err
34ddc 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 76 2d 3e 70  ;..  memset(v->p
34ddd 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d 65 6e  FulltextStatemen
34dde 74 73 2c 20 30 2c 20 73 69 7a 65 6f 66 28 76 2d  ts, 0, sizeof(v-
34ddf 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d  >pFulltextStatem
34de0 65 6e 74 73 29 29 3b 0a 0a 20 20 2f 2a 20 49 6e  ents));..  /* In
34de1 64 69 63 61 74 65 20 74 68 61 74 20 74 68 65 20  dicate that the 
34de2 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20 6c 69  buffer is not li
34de3 76 65 2e 20 2a 2f 0a 20 20 76 2d 3e 6e 50 65 6e  ve. */.  v->nPen
34de4 64 69 6e 67 44 61 74 61 20 3d 20 2d 31 3b 0a 0a  dingData = -1;..
34de5 20 20 2a 70 70 56 54 61 62 20 3d 20 26 76 2d 3e    *ppVTab = &v->
34de6 62 61 73 65 3b 0a 20 20 46 54 53 54 52 41 43 45  base;.  FTSTRACE
34de7 28 28 22 46 54 53 33 20 43 6f 6e 6e 65 63 74 20  (("FTS3 Connect 
34de8 25 70 5c 6e 22 2c 20 76 29 29 3b 0a 0a 20 20 72  %p\n", v));..  r
34de9 65 74 75 72 6e 20 72 63 3b 0a 0a 65 72 72 3a 0a  eturn rc;..err:.
34dea 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 5f    fulltext_vtab_
34deb 64 65 73 74 72 6f 79 28 76 29 3b 0a 20 20 72 65  destroy(v);.  re
34dec 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74  turn rc;.}..stat
34ded 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 43  ic int fulltextC
34dee 6f 6e 6e 65 63 74 28 0a 20 20 73 71 6c 69 74 65  onnect(.  sqlite
34def 33 20 2a 64 62 2c 0a 20 20 76 6f 69 64 20 2a 70  3 *db,.  void *p
34df0 41 75 78 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c  Aux,.  int argc,
34df1 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e   const char *con
34df2 73 74 2a 61 72 67 76 2c 0a 20 20 73 71 6c 69 74  st*argv,.  sqlit
34df3 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 61 62  e3_vtab **ppVTab
34df4 2c 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72  ,.  char **pzErr
34df5 0a 29 7b 0a 20 20 54 61 62 6c 65 53 70 65 63 20  .){.  TableSpec 
34df6 73 70 65 63 3b 0a 20 20 69 6e 74 20 72 63 20 3d  spec;.  int rc =
34df7 20 70 61 72 73 65 53 70 65 63 28 26 73 70 65 63   parseSpec(&spec
34df8 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 70 7a  , argc, argv, pz
34df9 45 72 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  Err);.  if( rc!=
34dfa 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
34dfb 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 63  rn rc;..  rc = c
34dfc 6f 6e 73 74 72 75 63 74 56 74 61 62 28 64 62 2c  onstructVtab(db,
34dfd 20 28 66 74 73 33 48 61 73 68 20 2a 29 70 41 75   (fts3Hash *)pAu
34dfe 78 2c 20 26 73 70 65 63 2c 20 70 70 56 54 61 62  x, &spec, ppVTab
34dff 2c 20 70 7a 45 72 72 29 3b 0a 20 20 63 6c 65 61  , pzErr);.  clea
34e00 72 54 61 62 6c 65 53 70 65 63 28 26 73 70 65 63  rTableSpec(&spec
34e01 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
34e02 7d 0a 0a 2f 2a 20 54 68 65 20 25 5f 63 6f 6e 74  }../* The %_cont
34e03 65 6e 74 20 74 61 62 6c 65 20 68 6f 6c 64 73 20  ent table holds 
34e04 74 68 65 20 74 65 78 74 20 6f 66 20 65 61 63 68  the text of each
34e05 20 64 6f 63 75 6d 65 6e 74 2c 20 77 69 74 68 0a   document, with.
34e06 2a 2a 20 74 68 65 20 64 6f 63 69 64 20 63 6f 6c  ** the docid col
34e07 75 6d 6e 20 65 78 70 6f 73 65 64 20 61 73 20 74  umn exposed as t
34e08 68 65 20 53 51 4c 69 74 65 20 72 6f 77 69 64 20  he SQLite rowid 
34e09 66 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a  for the table..*
34e0a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  /./* TODO(shess)
34e0b 20 54 68 69 73 20 63 6f 6d 6d 65 6e 74 20 6e 65   This comment ne
34e0c 65 64 73 20 65 6c 61 62 6f 72 61 74 69 6f 6e 20  eds elaboration 
34e0d 74 6f 20 6d 61 74 63 68 20 74 68 65 20 75 70 64  to match the upd
34e0e 61 74 65 64 0a 2a 2a 20 63 6f 64 65 2e 20 20 57  ated.** code.  W
34e0f 6f 72 6b 20 69 74 20 69 6e 74 6f 20 74 68 65 20  ork it into the 
34e10 74 6f 70 2d 6f 66 2d 66 69 6c 65 20 63 6f 6d 6d  top-of-file comm
34e11 65 6e 74 20 61 74 20 74 68 61 74 20 74 69 6d 65  ent at that time
34e12 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
34e13 66 75 6c 6c 74 65 78 74 43 72 65 61 74 65 28 73  fulltextCreate(s
34e14 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64  qlite3 *db, void
34e15 20 2a 70 41 75 78 2c 0a 20 20 20 20 20 20 20 20   *pAux,.        
34e16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34e17 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73    int argc, cons
34e18 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 2a  t char * const *
34e19 61 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20 20  argv,.          
34e1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34e1b 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70  sqlite3_vtab **p
34e1c 70 56 54 61 62 2c 20 63 68 61 72 20 2a 2a 70 7a  pVTab, char **pz
34e1d 45 72 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Err){.  int rc;.
34e1e 20 20 54 61 62 6c 65 53 70 65 63 20 73 70 65 63    TableSpec spec
34e1f 3b 0a 20 20 53 74 72 69 6e 67 42 75 66 66 65 72  ;.  StringBuffer
34e20 20 73 63 68 65 6d 61 3b 0a 20 20 46 54 53 54 52   schema;.  FTSTR
34e21 41 43 45 28 28 22 46 54 53 33 20 43 72 65 61 74  ACE(("FTS3 Creat
34e22 65 5c 6e 22 29 29 3b 0a 0a 20 20 72 63 20 3d 20  e\n"));..  rc = 
34e23 70 61 72 73 65 53 70 65 63 28 26 73 70 65 63 2c  parseSpec(&spec,
34e24 20 61 72 67 63 2c 20 61 72 67 76 2c 20 70 7a 45   argc, argv, pzE
34e25 72 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  rr);.  if( rc!=S
34e26 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
34e27 6e 20 72 63 3b 0a 0a 20 20 69 6e 69 74 53 74 72  n rc;..  initStr
34e28 69 6e 67 42 75 66 66 65 72 28 26 73 63 68 65 6d  ingBuffer(&schem
34e29 61 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 63  a);.  append(&sc
34e2a 68 65 6d 61 2c 20 22 43 52 45 41 54 45 20 54 41  hema, "CREATE TA
34e2b 42 4c 45 20 25 5f 63 6f 6e 74 65 6e 74 28 22 29  BLE %_content(")
34e2c 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 63 68 65  ;.  append(&sche
34e2d 6d 61 2c 20 22 20 20 64 6f 63 69 64 20 49 4e 54  ma, "  docid INT
34e2e 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
34e2f 2c 22 29 3b 0a 20 20 61 70 70 65 6e 64 4c 69 73  ,");.  appendLis
34e30 74 28 26 73 63 68 65 6d 61 2c 20 73 70 65 63 2e  t(&schema, spec.
34e31 6e 43 6f 6c 75 6d 6e 2c 20 73 70 65 63 2e 61 7a  nColumn, spec.az
34e32 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a  ContentColumn);.
34e33 20 20 61 70 70 65 6e 64 28 26 73 63 68 65 6d 61    append(&schema
34e34 2c 20 22 29 22 29 3b 0a 20 20 72 63 20 3d 20 73  , ")");.  rc = s
34e35 71 6c 5f 65 78 65 63 28 64 62 2c 20 73 70 65 63  ql_exec(db, spec
34e36 2e 7a 44 62 2c 20 73 70 65 63 2e 7a 4e 61 6d 65  .zDb, spec.zName
34e37 2c 20 73 74 72 69 6e 67 42 75 66 66 65 72 44 61  , stringBufferDa
34e38 74 61 28 26 73 63 68 65 6d 61 29 29 3b 0a 20 20  ta(&schema));.  
34e39 73 74 72 69 6e 67 42 75 66 66 65 72 44 65 73 74  stringBufferDest
34e3a 72 6f 79 28 26 73 63 68 65 6d 61 29 3b 0a 20 20  roy(&schema);.  
34e3b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
34e3c 4b 20 29 20 67 6f 74 6f 20 6f 75 74 3b 0a 0a 20  K ) goto out;.. 
34e3d 20 72 63 20 3d 20 73 71 6c 5f 65 78 65 63 28 64   rc = sql_exec(d
34e3e 62 2c 20 73 70 65 63 2e 7a 44 62 2c 20 73 70 65  b, spec.zDb, spe
34e3f 63 2e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20  c.zName,.       
34e40 20 20 20 20 20 20 20 20 20 22 63 72 65 61 74 65           "create
34e41 20 74 61 62 6c 65 20 25 5f 73 65 67 6d 65 6e 74   table %_segment
34e42 73 28 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  s(".            
34e43 20 20 20 20 22 20 20 62 6c 6f 63 6b 69 64 20 49      "  blockid I
34e44 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
34e45 45 59 2c 22 0a 20 20 20 20 20 20 20 20 20 20 20  EY,".           
34e46 20 20 20 20 20 22 20 20 62 6c 6f 63 6b 20 62 6c       "  block bl
34e47 6f 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  ob".            
34e48 20 20 20 20 22 29 3b 22 0a 20 20 20 20 20 20 20      ");".       
34e49 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20 69 66           );.  if
34e4a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
34e4b 29 20 67 6f 74 6f 20 6f 75 74 3b 0a 0a 20 20 72  ) goto out;..  r
34e4c 63 20 3d 20 73 71 6c 5f 65 78 65 63 28 64 62 2c  c = sql_exec(db,
34e4d 20 73 70 65 63 2e 7a 44 62 2c 20 73 70 65 63 2e   spec.zDb, spec.
34e4e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20  zName,.         
34e4f 20 20 20 20 20 20 20 22 63 72 65 61 74 65 20 74         "create t
34e50 61 62 6c 65 20 25 5f 73 65 67 64 69 72 28 22 0a  able %_segdir(".
34e51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34e52 22 20 20 6c 65 76 65 6c 20 69 6e 74 65 67 65 72  "  level integer
34e53 2c 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,".             
34e54 20 20 20 22 20 20 69 64 78 20 69 6e 74 65 67 65     "  idx intege
34e55 72 2c 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  r,".            
34e56 20 20 20 20 22 20 20 73 74 61 72 74 5f 62 6c 6f      "  start_blo
34e57 63 6b 20 69 6e 74 65 67 65 72 2c 22 0a 20 20 20  ck integer,".   
34e58 20 20 20 20 20 20 20 20 20 20 20 20 20 22 20 20               "  
34e59 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b  leaves_end_block
34e5a 20 69 6e 74 65 67 65 72 2c 22 0a 20 20 20 20 20   integer,".     
34e5b 20 20 20 20 20 20 20 20 20 20 20 22 20 20 65 6e             "  en
34e5c 64 5f 62 6c 6f 63 6b 20 69 6e 74 65 67 65 72 2c  d_block integer,
34e5d 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
34e5e 20 20 22 20 20 72 6f 6f 74 20 62 6c 6f 62 2c 22    "  root blob,"
34e5f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
34e60 20 22 20 20 70 72 69 6d 61 72 79 20 6b 65 79 28   "  primary key(
34e61 6c 65 76 65 6c 2c 20 69 64 78 29 22 0a 20 20 20  level, idx)".   
34e62 20 20 20 20 20 20 20 20 20 20 20 20 20 22 29 3b               ");
34e63 22 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  ");.  if( rc!=SQ
34e64 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 6f  LITE_OK ) goto o
34e65 75 74 3b 0a 0a 20 20 72 63 20 3d 20 63 6f 6e 73  ut;..  rc = cons
34e66 74 72 75 63 74 56 74 61 62 28 64 62 2c 20 28 66  tructVtab(db, (f
34e67 74 73 33 48 61 73 68 20 2a 29 70 41 75 78 2c 20  ts3Hash *)pAux, 
34e68 26 73 70 65 63 2c 20 70 70 56 54 61 62 2c 20 70  &spec, ppVTab, p
34e69 7a 45 72 72 29 3b 0a 0a 6f 75 74 3a 0a 20 20 63  zErr);..out:.  c
34e6a 6c 65 61 72 54 61 62 6c 65 53 70 65 63 28 26 73  learTableSpec(&s
34e6b 70 65 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  pec);.  return r
34e6c 63 3b 0a 7d 0a 0a 2f 2a 20 44 65 63 69 64 65 20  c;.}../* Decide 
34e6d 68 6f 77 20 74 6f 20 68 61 6e 64 6c 65 20 61 6e  how to handle an
34e6e 20 53 51 4c 20 71 75 65 72 79 2e 20 2a 2f 0a 73   SQL query. */.s
34e6f 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65  tatic int fullte
34e70 78 74 42 65 73 74 49 6e 64 65 78 28 73 71 6c 69  xtBestIndex(sqli
34e71 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c  te3_vtab *pVTab,
34e72 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
34e73 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b 0a 20 20 66  nfo *pInfo){.  f
34e74 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20  ulltext_vtab *v 
34e75 3d 20 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  = (fulltext_vtab
34e76 20 2a 29 70 56 54 61 62 3b 0a 20 20 69 6e 74 20   *)pVTab;.  int 
34e77 69 3b 0a 20 20 46 54 53 54 52 41 43 45 28 28 22  i;.  FTSTRACE(("
34e78 46 54 53 33 20 42 65 73 74 49 6e 64 65 78 5c 6e  FTS3 BestIndex\n
34e79 22 29 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b  "));..  for(i=0;
34e7a 20 69 3c 70 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74   i<pInfo->nConst
34e7b 72 61 69 6e 74 3b 20 2b 2b 69 29 7b 0a 20 20 20  raint; ++i){.   
34e7c 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73 71   const struct sq
34e7d 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73  lite3_index_cons
34e7e 74 72 61 69 6e 74 20 2a 70 43 6f 6e 73 74 72 61  traint *pConstra
34e7f 69 6e 74 3b 0a 20 20 20 20 70 43 6f 6e 73 74 72  int;.    pConstr
34e80 61 69 6e 74 20 3d 20 26 70 49 6e 66 6f 2d 3e 61  aint = &pInfo->a
34e81 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 3b 0a 20  Constraint[i];. 
34e82 20 20 20 69 66 28 20 70 43 6f 6e 73 74 72 61 69     if( pConstrai
34e83 6e 74 2d 3e 75 73 61 62 6c 65 20 29 20 7b 0a 20  nt->usable ) {. 
34e84 20 20 20 20 20 69 66 28 20 28 70 43 6f 6e 73 74       if( (pConst
34e85 72 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d  raint->iColumn==
34e86 2d 31 20 7c 7c 20 70 43 6f 6e 73 74 72 61 69 6e  -1 || pConstrain
34e87 74 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 76 2d 3e 6e  t->iColumn==v->n
34e88 43 6f 6c 75 6d 6e 2b 31 29 20 26 26 0a 20 20 20  Column+1) &&.   
34e89 20 20 20 20 20 20 20 70 43 6f 6e 73 74 72 61 69         pConstrai
34e8a 6e 74 2d 3e 6f 70 3d 3d 53 51 4c 49 54 45 5f 49  nt->op==SQLITE_I
34e8b 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
34e8c 45 51 20 29 7b 0a 20 20 20 20 20 20 20 20 70 49  EQ ){.        pI
34e8d 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 51 55  nfo->idxNum = QU
34e8e 45 52 59 5f 44 4f 43 49 44 3b 20 20 20 20 20 20  ERY_DOCID;      
34e8f 2f 2a 20 6c 6f 6f 6b 75 70 20 62 79 20 64 6f 63  /* lookup by doc
34e90 69 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 46 54  id */.        FT
34e91 53 54 52 41 43 45 28 28 22 46 54 53 33 20 51 55  STRACE(("FTS3 QU
34e92 45 52 59 5f 44 4f 43 49 44 5c 6e 22 29 29 3b 0a  ERY_DOCID\n"));.
34e93 20 20 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28        } else if(
34e94 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43   pConstraint->iC
34e95 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20 70 43 6f 6e  olumn>=0 && pCon
34e96 73 74 72 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e  straint->iColumn
34e97 3c 3d 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 26 26 0a  <=v->nColumn &&.
34e98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34e99 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 6f 70   pConstraint->op
34e9a 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  ==SQLITE_INDEX_C
34e9b 4f 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43 48 20  ONSTRAINT_MATCH 
34e9c 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 66 75  ){.        /* fu
34e9d 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68 20 2a  ll-text search *
34e9e 2f 0a 20 20 20 20 20 20 20 20 70 49 6e 66 6f 2d  /.        pInfo-
34e9f 3e 69 64 78 4e 75 6d 20 3d 20 51 55 45 52 59 5f  >idxNum = QUERY_
34ea0 46 55 4c 4c 54 45 58 54 20 2b 20 70 43 6f 6e 73  FULLTEXT + pCons
34ea1 74 72 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e 3b  traint->iColumn;
34ea2 0a 20 20 20 20 20 20 20 20 46 54 53 54 52 41 43  .        FTSTRAC
34ea3 45 28 28 22 46 54 53 33 20 51 55 45 52 59 5f 46  E(("FTS3 QUERY_F
34ea4 55 4c 4c 54 45 58 54 20 25 64 5c 6e 22 2c 20 70  ULLTEXT %d\n", p
34ea5 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43 6f 6c  Constraint->iCol
34ea6 75 6d 6e 29 29 3b 0a 20 20 20 20 20 20 7d 20 65  umn));.      } e
34ea7 6c 73 65 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20  lse continue;.. 
34ea8 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6e       pInfo->aCon
34ea9 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e  straintUsage[i].
34eaa 61 72 67 76 49 6e 64 65 78 20 3d 20 31 3b 0a 20  argvIndex = 1;. 
34eab 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6e       pInfo->aCon
34eac 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e  straintUsage[i].
34ead 6f 6d 69 74 20 3d 20 31 3b 0a 0a 20 20 20 20 20  omit = 1;..     
34eae 20 2f 2a 20 41 6e 20 61 72 62 69 74 72 61 72 79   /* An arbitrary
34eaf 20 76 61 6c 75 65 20 66 6f 72 20 6e 6f 77 2e 0a   value for now..
34eb0 20 20 20 20 20 20 20 2a 20 54 4f 44 4f 3a 20 50         * TODO: P
34eb1 65 72 68 61 70 73 20 64 6f 63 69 64 20 6d 61 74  erhaps docid mat
34eb2 63 68 65 73 20 73 68 6f 75 6c 64 20 62 65 20 63  ches should be c
34eb3 6f 6e 73 69 64 65 72 65 64 20 63 68 65 61 70 65  onsidered cheape
34eb4 72 20 74 68 61 6e 0a 20 20 20 20 20 20 20 2a 20  r than.       * 
34eb5 66 75 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68  full-text search
34eb6 65 73 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49 6e  es. */.      pIn
34eb7 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73  fo->estimatedCos
34eb8 74 20 3d 20 31 2e 30 3b 20 20 20 0a 0a 20 20 20  t = 1.0;   ..   
34eb9 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
34eba 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  _OK;.    }.  }. 
34ebb 20 70 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d   pInfo->idxNum =
34ebc 20 51 55 45 52 59 5f 47 45 4e 45 52 49 43 3b 0a   QUERY_GENERIC;.
34ebd 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
34ebe 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  OK;.}..static in
34ebf 74 20 66 75 6c 6c 74 65 78 74 44 69 73 63 6f 6e  t fulltextDiscon
34ec0 6e 65 63 74 28 73 71 6c 69 74 65 33 5f 76 74 61  nect(sqlite3_vta
34ec1 62 20 2a 70 56 54 61 62 29 7b 0a 20 20 46 54 53  b *pVTab){.  FTS
34ec2 54 52 41 43 45 28 28 22 46 54 53 33 20 44 69 73  TRACE(("FTS3 Dis
34ec3 63 6f 6e 6e 65 63 74 20 25 70 5c 6e 22 2c 20 70  connect %p\n", p
34ec4 56 54 61 62 29 29 3b 0a 20 20 66 75 6c 6c 74 65  VTab));.  fullte
34ec5 78 74 5f 76 74 61 62 5f 64 65 73 74 72 6f 79 28  xt_vtab_destroy(
34ec6 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
34ec7 29 70 56 54 61 62 29 3b 0a 20 20 72 65 74 75 72  )pVTab);.  retur
34ec8 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
34ec9 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74  static int fullt
34eca 65 78 74 44 65 73 74 72 6f 79 28 73 71 6c 69 74  extDestroy(sqlit
34ecb 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 7b  e3_vtab *pVTab){
34ecc 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  .  fulltext_vtab
34ecd 20 2a 76 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f   *v = (fulltext_
34ece 76 74 61 62 20 2a 29 70 56 54 61 62 3b 0a 20 20  vtab *)pVTab;.  
34ecf 69 6e 74 20 72 63 3b 0a 0a 20 20 46 54 53 54 52  int rc;..  FTSTR
34ed0 41 43 45 28 28 22 46 54 53 33 20 44 65 73 74 72  ACE(("FTS3 Destr
34ed1 6f 79 20 25 70 5c 6e 22 2c 20 70 56 54 61 62 29  oy %p\n", pVTab)
34ed2 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 5f 65 78  );.  rc = sql_ex
34ed3 65 63 28 76 2d 3e 64 62 2c 20 76 2d 3e 7a 44 62  ec(v->db, v->zDb
34ed4 2c 20 76 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20  , v->zName,.    
34ed5 20 20 20 20 20 20 20 20 20 20 20 20 22 64 72 6f              "dro
34ed6 70 20 74 61 62 6c 65 20 69 66 20 65 78 69 73 74  p table if exist
34ed7 73 20 25 5f 63 6f 6e 74 65 6e 74 3b 22 0a 20 20  s %_content;".  
34ed8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 64                "d
34ed9 72 6f 70 20 74 61 62 6c 65 20 69 66 20 65 78 69  rop table if exi
34eda 73 74 73 20 25 5f 73 65 67 6d 65 6e 74 73 3b 22  sts %_segments;"
34edb 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
34edc 20 22 64 72 6f 70 20 74 61 62 6c 65 20 69 66 20   "drop table if 
34edd 65 78 69 73 74 73 20 25 5f 73 65 67 64 69 72 3b  exists %_segdir;
34ede 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
34edf 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53    );.  if( rc!=S
34ee0 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
34ee1 6e 20 72 63 3b 0a 0a 20 20 66 75 6c 6c 74 65 78  n rc;..  fulltex
34ee2 74 5f 76 74 61 62 5f 64 65 73 74 72 6f 79 28 28  t_vtab_destroy((
34ee3 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29  fulltext_vtab *)
34ee4 70 56 54 61 62 29 3b 0a 20 20 72 65 74 75 72 6e  pVTab);.  return
34ee5 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73   SQLITE_OK;.}..s
34ee6 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65  tatic int fullte
34ee7 78 74 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76  xtOpen(sqlite3_v
34ee8 74 61 62 20 2a 70 56 54 61 62 2c 20 73 71 6c 69  tab *pVTab, sqli
34ee9 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
34eea 2a 2a 70 70 43 75 72 73 6f 72 29 7b 0a 20 20 66  **ppCursor){.  f
34eeb 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a  ulltext_cursor *
34eec 63 3b 0a 0a 20 20 63 20 3d 20 28 66 75 6c 6c 74  c;..  c = (fullt
34eed 65 78 74 5f 63 75 72 73 6f 72 20 2a 29 20 73 71  ext_cursor *) sq
34eee 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
34eef 65 6f 66 28 66 75 6c 6c 74 65 78 74 5f 63 75 72  eof(fulltext_cur
34ef0 73 6f 72 29 29 3b 0a 20 20 69 66 28 20 63 20 29  sor));.  if( c )
34ef1 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 63 2c 20  {.    memset(c, 
34ef2 30 2c 20 73 69 7a 65 6f 66 28 66 75 6c 6c 74 65  0, sizeof(fullte
34ef3 78 74 5f 63 75 72 73 6f 72 29 29 3b 0a 20 20 20  xt_cursor));.   
34ef4 20 2f 2a 20 73 71 6c 69 74 65 20 77 69 6c 6c 20   /* sqlite will 
34ef5 69 6e 69 74 69 61 6c 69 7a 65 20 63 2d 3e 62 61  initialize c->ba
34ef6 73 65 20 2a 2f 0a 20 20 20 20 2a 70 70 43 75 72  se */.    *ppCur
34ef7 73 6f 72 20 3d 20 26 63 2d 3e 62 61 73 65 3b 0a  sor = &c->base;.
34ef8 20 20 20 20 46 54 53 54 52 41 43 45 28 28 22 46      FTSTRACE(("F
34ef9 54 53 33 20 4f 70 65 6e 20 25 70 3a 20 25 70 5c  TS3 Open %p: %p\
34efa 6e 22 2c 20 70 56 54 61 62 2c 20 63 29 29 3b 0a  n", pVTab, c));.
34efb 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
34efc 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  E_OK;.  }else{. 
34efd 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
34efe 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 7d 0a 0a 2f  _NOMEM;.  }.}../
34eff 2a 20 46 72 65 65 20 61 6c 6c 20 6f 66 20 74 68  * Free all of th
34f00 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  e dynamically al
34f01 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 20 68  located memory h
34f02 65 6c 64 20 62 79 20 74 68 65 0a 2a 2a 20 53 6e  eld by the.** Sn
34f03 69 70 70 65 74 0a 2a 2f 0a 73 74 61 74 69 63 20  ippet.*/.static 
34f04 76 6f 69 64 20 73 6e 69 70 70 65 74 43 6c 65 61  void snippetClea
34f05 72 28 53 6e 69 70 70 65 74 20 2a 70 29 7b 0a 20  r(Snippet *p){. 
34f06 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d   sqlite3_free(p-
34f07 3e 61 4d 61 74 63 68 29 3b 0a 20 20 73 71 6c 69  >aMatch);.  sqli
34f08 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 4f 66 66  te3_free(p->zOff
34f09 73 65 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  set);.  sqlite3_
34f0a 66 72 65 65 28 70 2d 3e 7a 53 6e 69 70 70 65 74  free(p->zSnippet
34f0b 29 3b 0a 20 20 43 4c 45 41 52 28 70 29 3b 0a 7d  );.  CLEAR(p);.}
34f0c 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61  ../*.** Append a
34f0d 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 74 6f   single entry to
34f0e 20 74 68 65 20 70 2d 3e 61 4d 61 74 63 68 5b 5d   the p->aMatch[]
34f0f 20 6c 6f 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   log..*/.static 
34f10 76 6f 69 64 20 73 6e 69 70 70 65 74 41 70 70 65  void snippetAppe
34f11 6e 64 4d 61 74 63 68 28 0a 20 20 53 6e 69 70 70  ndMatch(.  Snipp
34f12 65 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  et *p,          
34f13 20 20 20 20 20 2f 2a 20 41 70 70 65 6e 64 20 74       /* Append t
34f14 68 65 20 65 6e 74 72 79 20 74 6f 20 74 68 69 73  he entry to this
34f15 20 73 6e 69 70 70 65 74 20 2a 2f 0a 20 20 69 6e   snippet */.  in
34f16 74 20 69 43 6f 6c 2c 20 69 6e 74 20 69 54 65 72  t iCol, int iTer
34f17 6d 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63  m,      /* The c
34f18 6f 6c 75 6d 6e 20 61 6e 64 20 71 75 65 72 79 20  olumn and query 
34f19 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 54  term */.  int iT
34f1a 6f 6b 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20  oken,           
34f1b 20 20 20 20 2f 2a 20 4d 61 74 63 68 69 6e 67 20      /* Matching 
34f1c 74 6f 6b 65 6e 20 69 6e 20 64 6f 63 75 6d 65 6e  token in documen
34f1d 74 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 72  t */.  int iStar
34f1e 74 2c 20 69 6e 74 20 6e 42 79 74 65 20 20 20 20  t, int nByte    
34f1f 20 2f 2a 20 4f 66 66 73 65 74 20 61 6e 64 20 73   /* Offset and s
34f20 69 7a 65 20 6f 66 20 74 68 65 20 6d 61 74 63 68  ize of the match
34f21 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   */.){.  int i;.
34f22 20 20 73 74 72 75 63 74 20 73 6e 69 70 70 65 74    struct snippet
34f23 4d 61 74 63 68 20 2a 70 4d 61 74 63 68 3b 0a 20  Match *pMatch;. 
34f24 20 69 66 28 20 70 2d 3e 6e 4d 61 74 63 68 2b 31   if( p->nMatch+1
34f25 3e 3d 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20  >=p->nAlloc ){. 
34f26 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70     p->nAlloc = p
34f27 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b 20 31 30 3b  ->nAlloc*2 + 10;
34f28 0a 20 20 20 20 70 2d 3e 61 4d 61 74 63 68 20 3d  .    p->aMatch =
34f29 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
34f2a 28 70 2d 3e 61 4d 61 74 63 68 2c 20 70 2d 3e 6e  (p->aMatch, p->n
34f2b 41 6c 6c 6f 63 2a 73 69 7a 65 6f 66 28 70 2d 3e  Alloc*sizeof(p->
34f2c 61 4d 61 74 63 68 5b 30 5d 29 20 29 3b 0a 20 20  aMatch[0]) );.  
34f2d 20 20 69 66 28 20 70 2d 3e 61 4d 61 74 63 68 3d    if( p->aMatch=
34f2e 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e  =0 ){.      p->n
34f2f 4d 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 20  Match = 0;.     
34f30 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 30 3b 0a   p->nAlloc = 0;.
34f31 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
34f32 20 20 7d 0a 20 20 7d 0a 20 20 69 20 3d 20 70 2d    }.  }.  i = p-
34f33 3e 6e 4d 61 74 63 68 2b 2b 3b 0a 20 20 70 4d 61  >nMatch++;.  pMa
34f34 74 63 68 20 3d 20 26 70 2d 3e 61 4d 61 74 63 68  tch = &p->aMatch
34f35 5b 69 5d 3b 0a 20 20 70 4d 61 74 63 68 2d 3e 69  [i];.  pMatch->i
34f36 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a 20 20 70 4d  Col = iCol;.  pM
34f37 61 74 63 68 2d 3e 69 54 65 72 6d 20 3d 20 69 54  atch->iTerm = iT
34f38 65 72 6d 3b 0a 20 20 70 4d 61 74 63 68 2d 3e 69  erm;.  pMatch->i
34f39 54 6f 6b 65 6e 20 3d 20 69 54 6f 6b 65 6e 3b 0a  Token = iToken;.
34f3a 20 20 70 4d 61 74 63 68 2d 3e 69 53 74 61 72 74    pMatch->iStart
34f3b 20 3d 20 69 53 74 61 72 74 3b 0a 20 20 70 4d 61   = iStart;.  pMa
34f3c 74 63 68 2d 3e 6e 42 79 74 65 20 3d 20 6e 42 79  tch->nByte = nBy
34f3d 74 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a  te;.}../*.** Siz
34f3e 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ing information 
34f3f 66 6f 72 20 74 68 65 20 63 69 72 63 75 6c 61 72  for the circular
34f40 20 62 75 66 66 65 72 20 75 73 65 64 20 69 6e 20   buffer used in 
34f41 73 6e 69 70 70 65 74 4f 66 66 73 65 74 73 4f 66  snippetOffsetsOf
34f42 43 6f 6c 75 6d 6e 28 29 0a 2a 2f 0a 23 64 65 66  Column().*/.#def
34f43 69 6e 65 20 46 54 53 33 5f 52 4f 54 4f 52 5f 53  ine FTS3_ROTOR_S
34f44 5a 20 20 20 28 33 32 29 0a 23 64 65 66 69 6e 65  Z   (32).#define
34f45 20 46 54 53 33 5f 52 4f 54 4f 52 5f 4d 41 53 4b   FTS3_ROTOR_MASK
34f46 20 28 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 2d   (FTS3_ROTOR_SZ-
34f47 31 29 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69  1)../*.** Functi
34f48 6f 6e 20 74 6f 20 69 74 65 72 61 74 65 20 74 68  on to iterate th
34f49 72 6f 75 67 68 20 74 68 65 20 74 6f 6b 65 6e 73  rough the tokens
34f4a 20 6f 66 20 61 20 63 6f 6d 70 69 6c 65 64 20 65   of a compiled e
34f4b 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  xpression..**.**
34f4c 20 45 78 63 65 70 74 2c 20 73 6b 69 70 20 61 6c   Except, skip al
34f4d 6c 20 74 6f 6b 65 6e 73 20 6f 6e 20 74 68 65 20  l tokens on the 
34f4e 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20  right-hand side 
34f4f 6f 66 20 61 20 4e 4f 54 20 6f 70 65 72 61 74 6f  of a NOT operato
34f50 72 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  r..** This funct
34f51 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 66  ion is used to f
34f52 69 6e 64 20 74 6f 6b 65 6e 73 20 61 73 20 70 61  ind tokens as pa
34f53 72 74 20 6f 66 20 73 6e 69 70 70 65 74 20 61 6e  rt of snippet an
34f54 64 20 6f 66 66 73 65 74 0a 2a 2a 20 67 65 6e 65  d offset.** gene
34f55 72 61 74 69 6f 6e 20 61 6e 64 20 77 65 20 64 6f  ration and we do
34f56 20 6e 74 20 77 61 6e 74 20 73 6e 69 70 70 65 74   nt want snippet
34f57 73 20 61 6e 64 20 6f 66 66 73 65 74 73 20 74 6f  s and offsets to
34f58 20 72 65 70 6f 72 74 20 6d 61 74 63 68 65 73 0a   report matches.
34f59 2a 2a 20 66 6f 72 20 74 6f 6b 65 6e 73 20 6f 6e  ** for tokens on
34f5a 20 74 68 65 20 52 48 53 20 6f 66 20 61 20 4e 4f   the RHS of a NO
34f5b 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  T..*/.static int
34f5c 20 66 74 73 33 4e 65 78 74 45 78 70 72 54 6f 6b   fts3NextExprTok
34f5d 65 6e 28 46 74 73 33 45 78 70 72 20 2a 2a 70 70  en(Fts3Expr **pp
34f5e 45 78 70 72 2c 20 69 6e 74 20 2a 70 69 54 6f 6b  Expr, int *piTok
34f5f 65 6e 29 7b 0a 20 20 46 74 73 33 45 78 70 72 20  en){.  Fts3Expr 
34f60 2a 70 20 3d 20 2a 70 70 45 78 70 72 3b 0a 20 20  *p = *ppExpr;.  
34f61 69 6e 74 20 69 54 6f 6b 65 6e 20 3d 20 2a 70 69  int iToken = *pi
34f62 54 6f 6b 65 6e 3b 0a 20 20 69 66 28 20 69 54 6f  Token;.  if( iTo
34f63 6b 65 6e 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20  ken<0 ){.    /* 
34f64 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
34f65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 20 69 73   expression p is
34f66 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 61 6e 20   the root of an 
34f67 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e  expression tree.
34f68 0a 20 20 20 20 2a 2a 20 4d 6f 76 65 20 74 6f 20  .    ** Move to 
34f69 74 68 65 20 66 69 72 73 74 20 74 6f 6b 65 6e 20  the first token 
34f6a 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  in the expressio
34f6b 6e 20 74 72 65 65 2e 0a 20 20 20 20 2a 2f 0a 20  n tree..    */. 
34f6c 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 4c 65     while( p->pLe
34f6d 66 74 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20  ft ){.      p = 
34f6e 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 7d 0a  p->pLeft;.    }.
34f6f 20 20 20 20 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a      iToken = 0;.
34f70 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
34f71 65 72 74 28 70 20 26 26 20 70 2d 3e 65 54 79 70  ert(p && p->eTyp
34f72 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41  e==FTSQUERY_PHRA
34f73 53 45 20 29 3b 0a 20 20 20 20 69 66 28 20 69 54  SE );.    if( iT
34f74 6f 6b 65 6e 3c 28 70 2d 3e 70 50 68 72 61 73 65  oken<(p->pPhrase
34f75 2d 3e 6e 54 6f 6b 65 6e 2d 31 29 20 29 7b 0a 20  ->nToken-1) ){. 
34f76 20 20 20 20 20 69 54 6f 6b 65 6e 2b 2b 3b 0a 20       iToken++;. 
34f77 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
34f78 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 20 20  iToken = 0;.    
34f79 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 50 61 72    while( p->pPar
34f7a 65 6e 74 20 26 26 20 70 2d 3e 70 50 61 72 65 6e  ent && p->pParen
34f7b 74 2d 3e 70 4c 65 66 74 21 3d 70 20 29 7b 0a 20  t->pLeft!=p ){. 
34f7c 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
34f7d 2d 3e 70 50 61 72 65 6e 74 2d 3e 70 52 69 67 68  ->pParent->pRigh
34f7e 74 3d 3d 70 20 29 3b 0a 20 20 20 20 20 20 20 20  t==p );.        
34f7f 70 20 3d 20 70 2d 3e 70 50 61 72 65 6e 74 3b 0a  p = p->pParent;.
34f80 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 20        }.      p 
34f81 3d 20 70 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20  = p->pParent;.  
34f82 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20      if( p ){.   
34f83 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
34f84 70 52 69 67 68 74 21 3d 30 20 29 3b 0a 20 20 20  pRight!=0 );.   
34f85 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 52 69 67       p = p->pRig
34f86 68 74 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c  ht;.        whil
34f87 65 28 20 70 2d 3e 70 4c 65 66 74 20 29 7b 0a 20  e( p->pLeft ){. 
34f88 20 20 20 20 20 20 20 20 20 70 20 3d 20 70 2d 3e           p = p->
34f89 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 20 20 7d  pLeft;.        }
34f8a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
34f8b 20 7d 0a 0a 20 20 2a 70 70 45 78 70 72 20 3d 20   }..  *ppExpr = 
34f8c 70 3b 0a 20 20 2a 70 69 54 6f 6b 65 6e 20 3d 20  p;.  *piToken = 
34f8d 69 54 6f 6b 65 6e 3b 0a 20 20 72 65 74 75 72 6e  iToken;.  return
34f8e 20 70 3f 31 3a 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   p?1:0;.}../*.**
34f8f 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
34f90 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6e  the expression n
34f91 6f 64 65 20 70 45 78 70 72 20 69 73 20 6c 6f 63  ode pExpr is loc
34f92 61 74 65 64 20 62 65 6e 65 61 74 68 20 74 68 65  ated beneath the
34f93 0a 2a 2a 20 52 48 53 20 6f 66 20 61 20 4e 4f 54  .** RHS of a NOT
34f94 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74   operator..*/.st
34f95 61 74 69 63 20 69 6e 74 20 66 74 73 33 45 78 70  atic int fts3Exp
34f96 72 42 65 6e 65 61 74 68 4e 6f 74 28 46 74 73 33  rBeneathNot(Fts3
34f97 45 78 70 72 20 2a 70 29 7b 0a 20 20 46 74 73 33  Expr *p){.  Fts3
34f98 45 78 70 72 20 2a 70 50 61 72 65 6e 74 3b 0a 20  Expr *pParent;. 
34f99 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20   while( p ){.   
34f9a 20 70 50 61 72 65 6e 74 20 3d 20 70 2d 3e 70 50   pParent = p->pP
34f9b 61 72 65 6e 74 3b 0a 20 20 20 20 69 66 28 20 70  arent;.    if( p
34f9c 50 61 72 65 6e 74 20 26 26 20 70 50 61 72 65 6e  Parent && pParen
34f9d 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  t->eType==FTSQUE
34f9e 52 59 5f 4e 4f 54 20 26 26 20 70 50 61 72 65 6e  RY_NOT && pParen
34f9f 74 2d 3e 70 52 69 67 68 74 3d 3d 70 20 29 7b 0a  t->pRight==p ){.
34fa0 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
34fa1 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 50      }.    p = pP
34fa2 61 72 65 6e 74 3b 0a 20 20 7d 0a 20 20 72 65 74  arent;.  }.  ret
34fa3 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
34fa4 41 64 64 20 65 6e 74 72 69 65 73 20 74 6f 20 70  Add entries to p
34fa5 53 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b  Snippet->aMatch[
34fa6 5d 20 66 6f 72 20 65 76 65 72 79 20 6d 61 74 63  ] for every matc
34fa7 68 20 74 68 61 74 20 6f 63 63 75 72 73 20 61 67  h that occurs ag
34fa8 61 69 6e 73 74 0a 2a 2a 20 64 6f 63 75 6d 65 6e  ainst.** documen
34fa9 74 20 7a 44 6f 63 5b 30 2e 2e 6e 44 6f 63 2d 31  t zDoc[0..nDoc-1
34faa 5d 20 77 68 69 63 68 20 69 73 20 73 74 6f 72 65  ] which is store
34fab 64 20 69 6e 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c  d in column iCol
34fac 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  umn..*/.static v
34fad 6f 69 64 20 73 6e 69 70 70 65 74 4f 66 66 73 65  oid snippetOffse
34fae 74 73 4f 66 43 6f 6c 75 6d 6e 28 0a 20 20 66 75  tsOfColumn(.  fu
34faf 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 70  lltext_cursor *p
34fb0 43 75 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Cur,         /* 
34fb1 54 68 65 20 66 75 6c 6c 74 65 73 74 20 73 65 61  The fulltest sea
34fb2 72 63 68 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  rch cursor */.  
34fb3 53 6e 69 70 70 65 74 20 2a 70 53 6e 69 70 70 65  Snippet *pSnippe
34fb4 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  t,             /
34fb5 2a 20 54 68 65 20 53 6e 69 70 70 65 74 20 6f 62  * The Snippet ob
34fb6 6a 65 63 74 20 74 6f 20 62 65 20 66 69 6c 6c 65  ject to be fille
34fb7 64 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 43  d in */.  int iC
34fb8 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20  olumn,          
34fb9 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
34fba 78 20 6f 66 20 66 75 6c 6c 74 65 78 74 20 74 61  x of fulltext ta
34fbb 62 6c 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20  ble column */.  
34fbc 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 6f 63  const char *zDoc
34fbd 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
34fbe 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 66 75  * Text of the fu
34fbf 6c 6c 74 65 78 74 20 74 61 62 6c 65 20 63 6f 6c  lltext table col
34fc0 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 6f  umn */.  int nDo
34fc1 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c               
34fc2 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
34fc3 68 20 6f 66 20 7a 44 6f 63 20 69 6e 20 62 79 74  h of zDoc in byt
34fc4 65 73 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74  es */.){.  const
34fc5 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
34fc6 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 54 4d 6f 64  er_module *pTMod
34fc7 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20 74 6f 6b  ule;  /* The tok
34fc8 65 6e 69 7a 65 72 20 6d 6f 64 75 6c 65 20 2a 2f  enizer module */
34fc9 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
34fca 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72  izer *pTokenizer
34fcb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
34fcc 20 54 68 65 20 73 70 65 63 69 66 69 63 20 74 6f   The specific to
34fcd 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 73 71 6c  kenizer */.  sql
34fce 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
34fcf 75 72 73 6f 72 20 2a 70 54 43 75 72 73 6f 72 3b  ursor *pTCursor;
34fd0 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e          /* Token
34fd1 69 7a 65 72 20 63 75 72 73 6f 72 20 2a 2f 0a 20  izer cursor */. 
34fd2 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a   fulltext_vtab *
34fd3 70 56 74 61 62 3b 20 20 20 20 20 20 20 20 20 20  pVtab;          
34fd4 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6c        /* The ful
34fd5 6c 20 74 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a  l text index */.
34fd6 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20    int nColumn;  
34fd7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34fd8 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
34fd9 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
34fda 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  he index */.  in
34fdb 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20  t i, j;         
34fdc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34fdd 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
34fde 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  ers */.  int rc;
34fdf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
34fe1 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
34fe2 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6d    unsigned int m
34fe3 61 74 63 68 2c 20 70 72 65 76 4d 61 74 63 68 3b  atch, prevMatch;
34fe4 20 20 20 20 20 20 20 2f 2a 20 50 68 72 61 73 65         /* Phrase
34fe5 20 73 65 61 72 63 68 20 62 69 74 6d 61 73 6b 73   search bitmasks
34fe6 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
34fe7 20 2a 7a 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20   *zToken;       
34fe8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
34fe9 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 74 68  xt token from th
34fea 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20  e tokenizer */. 
34feb 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 20 20 20 20   int nToken;    
34fec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34fed 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
34fee 20 7a 54 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74   zToken */.  int
34fef 20 69 42 65 67 69 6e 2c 20 69 45 6e 64 2c 20 69   iBegin, iEnd, i
34ff0 50 6f 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  Pos;            
34ff1 20 20 2f 2a 20 4f 66 66 73 65 74 73 20 6f 66 20    /* Offsets of 
34ff2 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e  beginning and en
34ff3 64 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 66  d */..  /* The f
34ff4 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c  ollowing variabl
34ff5 65 73 20 6b 65 65 70 20 61 20 63 69 72 63 75 6c  es keep a circul
34ff6 61 72 20 62 75 66 66 65 72 20 6f 66 20 74 68 65  ar buffer of the
34ff7 20 6c 61 73 74 0a 20 20 2a 2a 20 66 65 77 20 74   last.  ** few t
34ff8 6f 6b 65 6e 73 20 2a 2f 0a 20 20 75 6e 73 69 67  okens */.  unsig
34ff9 6e 65 64 20 69 6e 74 20 69 52 6f 74 6f 72 20 3d  ned int iRotor =
34ffa 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
34ffb 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 72 72  /* Index of curr
34ffc 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69  ent token */.  i
34ffd 6e 74 20 69 52 6f 74 6f 72 42 65 67 69 6e 5b 46  nt iRotorBegin[F
34ffe 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 5d 3b 20 20  TS3_ROTOR_SZ];  
34fff 20 20 20 20 2f 2a 20 42 65 67 69 6e 6e 69 6e 67      /* Beginning
35000 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e   offset of token
35001 20 2a 2f 0a 20 20 69 6e 74 20 69 52 6f 74 6f 72   */.  int iRotor
35002 4c 65 6e 5b 46 54 53 33 5f 52 4f 54 4f 52 5f 53  Len[FTS3_ROTOR_S
35003 5a 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 65  Z];        /* Le
35004 6e 67 74 68 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f  ngth of token */
35005 0a 0a 20 20 70 56 74 61 62 20 3d 20 63 75 72 73  ..  pVtab = curs
35006 6f 72 5f 76 74 61 62 28 70 43 75 72 29 3b 0a 20  or_vtab(pCur);. 
35007 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 56 74 61 62   nColumn = pVtab
35008 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 70 54 6f  ->nColumn;.  pTo
35009 6b 65 6e 69 7a 65 72 20 3d 20 70 56 74 61 62 2d  kenizer = pVtab-
3500a 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 70  >pTokenizer;.  p
3500b 54 4d 6f 64 75 6c 65 20 3d 20 70 54 6f 6b 65 6e  TModule = pToken
3500c 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20  izer->pModule;. 
3500d 20 72 63 20 3d 20 70 54 4d 6f 64 75 6c 65 2d 3e   rc = pTModule->
3500e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65 72  xOpen(pTokenizer
3500f 2c 20 7a 44 6f 63 2c 20 6e 44 6f 63 2c 20 26 70  , zDoc, nDoc, &p
35010 54 43 75 72 73 6f 72 29 3b 0a 20 20 69 66 28 20  TCursor);.  if( 
35011 72 63 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70  rc ) return;.  p
35012 54 43 75 72 73 6f 72 2d 3e 70 54 6f 6b 65 6e 69  TCursor->pTokeni
35013 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72  zer = pTokenizer
35014 3b 0a 0a 20 20 70 72 65 76 4d 61 74 63 68 20 3d  ;..  prevMatch =
35015 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 21 70 54   0;.  while( !pT
35016 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 54  Module->xNext(pT
35017 43 75 72 73 6f 72 2c 20 26 7a 54 6f 6b 65 6e 2c  Cursor, &zToken,
35018 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 42 65 67 69   &nToken, &iBegi
35019 6e 2c 20 26 69 45 6e 64 2c 20 26 69 50 6f 73 29  n, &iEnd, &iPos)
3501a 20 29 7b 0a 20 20 20 20 46 74 73 33 45 78 70 72   ){.    Fts3Expr
3501b 20 2a 70 49 74 65 72 20 3d 20 70 43 75 72 2d 3e   *pIter = pCur->
3501c 70 45 78 70 72 3b 0a 20 20 20 20 69 6e 74 20 69  pExpr;.    int i
3501d 49 74 65 72 20 3d 20 2d 31 3b 0a 20 20 20 20 69  Iter = -1;.    i
3501e 52 6f 74 6f 72 42 65 67 69 6e 5b 69 52 6f 74 6f  RotorBegin[iRoto
3501f 72 26 46 54 53 33 5f 52 4f 54 4f 52 5f 4d 41 53  r&FTS3_ROTOR_MAS
35020 4b 5d 20 3d 20 69 42 65 67 69 6e 3b 0a 20 20 20  K] = iBegin;.   
35021 20 69 52 6f 74 6f 72 4c 65 6e 5b 69 52 6f 74 6f   iRotorLen[iRoto
35022 72 26 46 54 53 33 5f 52 4f 54 4f 52 5f 4d 41 53  r&FTS3_ROTOR_MAS
35023 4b 5d 20 3d 20 69 45 6e 64 2d 69 42 65 67 69 6e  K] = iEnd-iBegin
35024 3b 0a 20 20 20 20 6d 61 74 63 68 20 3d 20 30 3b  ;.    match = 0;
35025 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
35026 28 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 2d 31  (FTS3_ROTOR_SZ-1
35027 29 20 26 26 20 66 74 73 33 4e 65 78 74 45 78 70  ) && fts3NextExp
35028 72 54 6f 6b 65 6e 28 26 70 49 74 65 72 2c 20 26  rToken(&pIter, &
35029 69 49 74 65 72 29 3b 20 69 2b 2b 29 7b 0a 20 20  iIter); i++){.  
3502a 20 20 20 20 69 6e 74 20 6e 50 68 72 61 73 65 3b      int nPhrase;
3502b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3502c 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
3502d 20 74 6f 6b 65 6e 73 20 69 6e 20 63 75 72 72 65   tokens in curre
3502e 6e 74 20 70 68 72 61 73 65 20 2a 2f 0a 20 20 20  nt phrase */.   
3502f 20 20 20 73 74 72 75 63 74 20 50 68 72 61 73 65     struct Phrase
35030 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 3b 20 20  Token *pToken;  
35031 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 74 6f     /* Current to
35032 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ken */.      int
35033 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20   iCol;          
35034 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35035 43 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 2a 2f 0a  Column index */.
35036 0a 20 20 20 20 20 20 69 66 28 20 66 74 73 33 45  .      if( fts3E
35037 78 70 72 42 65 6e 65 61 74 68 4e 6f 74 28 70 49  xprBeneathNot(pI
35038 74 65 72 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b  ter) ) continue;
35039 0a 20 20 20 20 20 20 6e 50 68 72 61 73 65 20 3d  .      nPhrase =
3503a 20 70 49 74 65 72 2d 3e 70 50 68 72 61 73 65 2d   pIter->pPhrase-
3503b 3e 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 70  >nToken;.      p
3503c 54 6f 6b 65 6e 20 3d 20 26 70 49 74 65 72 2d 3e  Token = &pIter->
3503d 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
3503e 69 49 74 65 72 5d 3b 0a 20 20 20 20 20 20 69 43  iIter];.      iC
3503f 6f 6c 20 3d 20 70 49 74 65 72 2d 3e 70 50 68 72  ol = pIter->pPhr
35040 61 73 65 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20  ase->iColumn;.  
35041 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20      if( iCol>=0 
35042 26 26 20 69 43 6f 6c 3c 6e 43 6f 6c 75 6d 6e 20  && iCol<nColumn 
35043 26 26 20 69 43 6f 6c 21 3d 69 43 6f 6c 75 6d 6e  && iCol!=iColumn
35044 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
35045 20 20 20 69 66 28 20 70 54 6f 6b 65 6e 2d 3e 6e     if( pToken->n
35046 3e 6e 54 6f 6b 65 6e 20 29 20 63 6f 6e 74 69 6e  >nToken ) contin
35047 75 65 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70  ue;.      if( !p
35048 54 6f 6b 65 6e 2d 3e 69 73 50 72 65 66 69 78 20  Token->isPrefix 
35049 26 26 20 70 54 6f 6b 65 6e 2d 3e 6e 3c 6e 54 6f  && pToken->n<nTo
3504a 6b 65 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ken ) continue;.
3504b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
3504c 6f 6b 65 6e 2d 3e 6e 3c 3d 6e 54 6f 6b 65 6e 20  oken->n<=nToken 
3504d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d  );.      if( mem
3504e 63 6d 70 28 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 7a  cmp(pToken->z, z
3504f 54 6f 6b 65 6e 2c 20 70 54 6f 6b 65 6e 2d 3e 6e  Token, pToken->n
35050 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  ) ) continue;.  
35051 20 20 20 20 69 66 28 20 69 49 74 65 72 3e 30 20      if( iIter>0 
35052 26 26 20 28 70 72 65 76 4d 61 74 63 68 20 26 20  && (prevMatch & 
35053 28 31 3c 3c 69 29 29 3d 3d 30 20 29 20 63 6f 6e  (1<<i))==0 ) con
35054 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 6d 61 74  tinue;.      mat
35055 63 68 20 7c 3d 20 31 3c 3c 69 3b 0a 20 20 20 20  ch |= 1<<i;.    
35056 20 20 69 66 28 20 69 3d 3d 28 46 54 53 33 5f 52    if( i==(FTS3_R
35057 4f 54 4f 52 5f 53 5a 2d 32 29 20 7c 7c 20 6e 50  OTOR_SZ-2) || nP
35058 68 72 61 73 65 3d 3d 69 49 74 65 72 2b 31 20 29  hrase==iIter+1 )
35059 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d  {.        for(j=
3505a 6e 50 68 72 61 73 65 2d 31 3b 20 6a 3e 3d 30 3b  nPhrase-1; j>=0;
3505b 20 6a 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20   j--){.         
3505c 20 69 6e 74 20 6b 20 3d 20 28 69 52 6f 74 6f 72   int k = (iRotor
3505d 2d 6a 29 20 26 20 46 54 53 33 5f 52 4f 54 4f 52  -j) & FTS3_ROTOR
3505e 5f 4d 41 53 4b 3b 0a 20 20 20 20 20 20 20 20 20  _MASK;.         
3505f 20 73 6e 69 70 70 65 74 41 70 70 65 6e 64 4d 61   snippetAppendMa
35060 74 63 68 28 70 53 6e 69 70 70 65 74 2c 20 69 43  tch(pSnippet, iC
35061 6f 6c 75 6d 6e 2c 20 69 2d 6a 2c 20 69 50 6f 73  olumn, i-j, iPos
35062 2d 6a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  -j,.            
35063 20 20 20 20 69 52 6f 74 6f 72 42 65 67 69 6e 5b      iRotorBegin[
35064 6b 5d 2c 20 69 52 6f 74 6f 72 4c 65 6e 5b 6b 5d  k], iRotorLen[k]
35065 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
35066 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70     }.    }.    p
35067 72 65 76 4d 61 74 63 68 20 3d 20 6d 61 74 63 68  revMatch = match
35068 3c 3c 31 3b 0a 20 20 20 20 69 52 6f 74 6f 72 2b  <<1;.    iRotor+
35069 2b 3b 0a 20 20 7d 0a 20 20 70 54 4d 6f 64 75 6c  +;.  }.  pTModul
3506a 65 2d 3e 78 43 6c 6f 73 65 28 70 54 43 75 72 73  e->xClose(pTCurs
3506b 6f 72 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  or);  .}../*.** 
3506c 52 65 6d 6f 76 65 20 65 6e 74 72 69 65 73 20 66  Remove entries f
3506d 72 6f 6d 20 74 68 65 20 70 53 6e 69 70 70 65 74  rom the pSnippet
3506e 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 61 63   structure to ac
3506f 63 6f 75 6e 74 20 66 6f 72 20 74 68 65 20 4e 45  count for the NE
35070 41 52 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 20  AR.** operator. 
35071 57 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c  When this is cal
35072 6c 65 64 2c 20 70 53 6e 69 70 70 65 74 20 63 6f  led, pSnippet co
35073 6e 74 61 69 6e 73 20 74 68 65 20 6c 69 73 74 20  ntains the list 
35074 6f 66 20 74 6f 6b 65 6e 20 0a 2a 2a 20 6f 66 66  of token .** off
35075 73 65 74 73 20 70 72 6f 64 75 63 65 64 20 62 79  sets produced by
35076 20 74 72 65 61 74 69 6e 67 20 61 6c 6c 20 4e 45   treating all NE
35077 41 52 20 6f 70 65 72 61 74 6f 72 73 20 61 73 20  AR operators as 
35078 41 4e 44 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a  AND operators..*
35079 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
3507a 72 65 6d 6f 76 65 73 20 61 6e 79 20 65 6e 74 72  removes any entr
3507b 69 65 73 20 74 68 61 74 20 73 68 6f 75 6c 64 20  ies that should 
3507c 6e 6f 74 20 62 65 20 70 72 65 73 65 6e 74 20 61  not be present a
3507d 66 74 65 72 0a 2a 2a 20 61 63 63 6f 75 6e 74 69  fter.** accounti
3507e 6e 67 20 66 6f 72 20 74 68 65 20 4e 45 41 52 20  ng for the NEAR 
3507f 72 65 73 74 72 69 63 74 69 6f 6e 2e 20 46 6f 72  restriction. For
35080 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65   example, if the
35081 20 71 75 65 72 69 65 64 0a 2a 2a 20 64 6f 63 75   queried.** docu
35082 6d 65 6e 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  ment is:.**.**  
35083 20 20 20 22 41 20 42 20 43 20 44 20 45 20 41 22     "A B C D E A"
35084 0a 2a 2a 0a 2a 2a 20 61 6e 64 20 74 68 65 20 71  .**.** and the q
35085 75 65 72 79 20 69 73 3a 0a 2a 2a 20 0a 2a 2a 20  uery is:.** .** 
35086 20 20 20 20 41 20 4e 45 41 52 2f 30 20 45 0a 2a      A NEAR/0 E.*
35087 2a 0a 2a 2a 20 74 68 65 6e 20 77 68 65 6e 20 74  *.** then when t
35088 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
35089 63 61 6c 6c 65 64 20 74 68 65 20 53 6e 69 70 70  called the Snipp
3508a 65 74 20 63 6f 6e 74 61 69 6e 73 20 74 6f 6b 65  et contains toke
3508b 6e 20 6f 66 66 73 65 74 73 0a 2a 2a 20 30 2c 20  n offsets.** 0, 
3508c 34 20 61 6e 64 20 35 2e 20 54 68 69 73 20 66 75  4 and 5. This fu
3508d 6e 63 74 69 6f 6e 20 72 65 6d 6f 76 65 73 20 74  nction removes t
3508e 68 65 20 22 30 22 20 65 6e 74 72 79 20 28 62 65  he "0" entry (be
3508f 63 61 75 73 65 20 74 68 65 20 66 69 72 73 74 20  cause the first 
35090 41 0a 2a 2a 20 69 73 20 6e 6f 74 20 6e 65 61 72  A.** is not near
35091 20 65 6e 6f 75 67 68 20 74 6f 20 61 6e 20 45 29   enough to an E)
35092 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69  ..**.** When thi
35093 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
35094 6c 6c 65 64 2c 20 74 68 65 20 76 61 6c 75 65 20  lled, the value 
35095 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 61  pointed to by pa
35096 72 61 6d 65 74 65 72 20 70 69 4c 65 66 74 20 69  rameter piLeft i
35097 73 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65 72  s.** the integer
35098 20 69 64 20 6f 66 20 74 68 65 20 6c 65 66 74 2d   id of the left-
35099 6d 6f 73 74 20 74 6f 6b 65 6e 20 69 6e 20 74 68  most token in th
3509a 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  e expression tre
3509b 65 20 68 65 61 64 65 64 20 62 79 0a 2a 2a 20 70  e headed by.** p
3509c 45 78 70 72 2e 20 54 68 69 73 20 66 75 6e 63 74  Expr. This funct
3509d 69 6f 6e 20 69 6e 63 72 65 6d 65 6e 74 73 20 2a  ion increments *
3509e 70 69 4c 65 66 74 20 62 79 20 74 68 65 20 74 6f  piLeft by the to
3509f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 6f  tal number of to
350a0 6b 65 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 65  kens.** in the e
350a1 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 68  xpression tree h
350a2 65 61 64 65 64 20 62 79 20 70 45 78 70 72 2e 0a  eaded by pExpr..
350a3 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 31 20 69  **.** Return 1 i
350a4 66 20 61 6e 79 20 74 72 69 6d 6d 69 6e 67 20 6f  f any trimming o
350a5 63 63 75 72 73 2e 20 20 52 65 74 75 72 6e 20 30  ccurs.  Return 0
350a6 20 69 66 20 6e 6f 20 74 72 69 6d 6d 69 6e 67 20   if no trimming 
350a7 69 73 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a  is required..*/.
350a8 73 74 61 74 69 63 20 69 6e 74 20 74 72 69 6d 53  static int trimS
350a9 6e 69 70 70 65 74 4f 66 66 73 65 74 73 28 0a 20  nippetOffsets(. 
350aa 20 46 74 73 33 45 78 70 72 20 2a 70 45 78 70 72   Fts3Expr *pExpr
350ab 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 65  ,      /* The se
350ac 61 72 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20  arch expression 
350ad 2a 2f 0a 20 20 53 6e 69 70 70 65 74 20 2a 70 53  */.  Snippet *pS
350ae 6e 69 70 70 65 74 2c 20 20 20 20 2f 2a 20 54 68  nippet,    /* Th
350af 65 20 73 65 74 20 6f 66 20 73 6e 69 70 70 65 74  e set of snippet
350b0 20 6f 66 66 73 65 74 73 20 74 6f 20 62 65 20 74   offsets to be t
350b1 72 69 6d 6d 65 64 20 2a 2f 0a 20 20 69 6e 74 20  rimmed */.  int 
350b2 2a 70 69 4c 65 66 74 20 20 20 20 20 20 20 20 20  *piLeft         
350b3 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 6c 65    /* Index of le
350b4 66 74 2d 6d 6f 73 74 20 74 6f 6b 65 6e 20 69 6e  ft-most token in
350b5 20 70 45 78 70 72 20 2a 2f 0a 29 7b 0a 20 20 69   pExpr */.){.  i
350b6 66 28 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20  f( pExpr ){.    
350b7 69 66 28 20 74 72 69 6d 53 6e 69 70 70 65 74 4f  if( trimSnippetO
350b8 66 66 73 65 74 73 28 70 45 78 70 72 2d 3e 70 4c  ffsets(pExpr->pL
350b9 65 66 74 2c 20 70 53 6e 69 70 70 65 74 2c 20 70  eft, pSnippet, p
350ba 69 4c 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20  iLeft) ){.      
350bb 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
350bc 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 45 78  .    switch( pEx
350bd 70 72 2d 3e 65 54 79 70 65 20 29 7b 0a 20 20 20  pr->eType ){.   
350be 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52 59     case FTSQUERY
350bf 5f 50 48 52 41 53 45 3a 0a 20 20 20 20 20 20 20  _PHRASE:.       
350c0 20 2a 70 69 4c 65 66 74 20 2b 3d 20 70 45 78 70   *piLeft += pExp
350c1 72 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b  r->pPhrase->nTok
350c2 65 6e 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  en;.        brea
350c3 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 46 54  k;.      case FT
350c4 53 51 55 45 52 59 5f 4e 45 41 52 3a 20 7b 0a 20  SQUERY_NEAR: {. 
350c5 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69         /* The ri
350c6 67 68 74 2d 68 61 6e 64 2d 73 69 64 65 20 6f 66  ght-hand-side of
350c7 20 61 20 4e 45 41 52 20 6f 70 65 72 61 74 6f 72   a NEAR operator
350c8 20 69 73 20 61 6c 77 61 79 73 20 61 20 70 68 72   is always a phr
350c9 61 73 65 2e 20 54 68 65 0a 20 20 20 20 20 20 20  ase. The.       
350ca 20 2a 2a 20 6c 65 66 74 2d 68 61 6e 64 2d 73 69   ** left-hand-si
350cb 64 65 20 69 73 20 65 69 74 68 65 72 20 61 20 70  de is either a p
350cc 68 72 61 73 65 20 6f 72 20 61 6e 20 65 78 70 72  hrase or an expr
350cd 65 73 73 69 6f 6e 20 74 72 65 65 20 74 68 61 74  ession tree that
350ce 20 69 73 20 0a 20 20 20 20 20 20 20 20 2a 2a 20   is .        ** 
350cf 69 74 73 65 6c 66 20 68 65 61 64 65 64 20 62 79  itself headed by
350d0 20 61 20 4e 45 41 52 20 6f 70 65 72 61 74 6f 72   a NEAR operator
350d1 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  . The following 
350d2 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 73 0a  initializations.
350d3 20 20 20 20 20 20 20 20 2a 2a 20 73 65 74 20 6c          ** set l
350d4 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 20 69 4c  ocal variable iL
350d5 65 66 74 20 74 6f 20 74 68 65 20 74 6f 6b 65 6e  eft to the token
350d6 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6c   number of the l
350d7 65 66 74 2d 6d 6f 73 74 0a 20 20 20 20 20 20 20  eft-most.       
350d8 20 2a 2a 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65   ** token in the
350d9 20 72 69 67 68 74 2d 68 61 6e 64 20 70 68 72 61   right-hand phra
350da 73 65 2c 20 61 6e 64 20 69 52 69 67 68 74 20 74  se, and iRight t
350db 6f 20 74 68 65 20 72 69 67 68 74 20 6d 6f 73 74  o the right most
350dc 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 6b 65  .        ** toke
350dd 6e 20 69 6e 20 74 68 65 20 73 61 6d 65 20 70 68  n in the same ph
350de 72 61 73 65 2e 20 46 6f 72 20 65 78 61 6d 70 6c  rase. For exampl
350df 65 2c 20 69 66 20 77 65 20 68 61 64 3a 0a 20 20  e, if we had:.  
350e0 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
350e1 20 2a 2a 20 20 20 20 20 3c 63 6f 6c 3e 20 4d 41   **     <col> MA
350e2 54 43 48 20 27 22 61 62 63 20 64 65 66 22 20 4e  TCH '"abc def" N
350e3 45 41 52 2f 32 20 22 67 68 69 20 6a 6b 6c 22 27  EAR/2 "ghi jkl"'
350e4 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
350e5 20 20 20 20 2a 2a 20 74 68 65 6e 20 69 4c 65 66      ** then iLef
350e6 74 20 77 69 6c 6c 20 62 65 20 73 65 74 20 74 6f  t will be set to
350e7 20 32 20 28 74 6f 6b 65 6e 20 6e 75 6d 62 65 72   2 (token number
350e8 20 6f 66 20 67 68 69 29 20 61 6e 64 20 6e 54 6f   of ghi) and nTo
350e9 6b 65 6e 20 77 69 6c 6c 0a 20 20 20 20 20 20 20  ken will.       
350ea 20 2a 2a 20 62 65 20 73 65 74 20 74 6f 20 34 2e   ** be set to 4.
350eb 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
350ec 20 20 20 20 46 74 73 33 45 78 70 72 20 2a 70 4c      Fts3Expr *pL
350ed 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65  eft = pExpr->pLe
350ee 66 74 3b 0a 20 20 20 20 20 20 20 20 46 74 73 33  ft;.        Fts3
350ef 45 78 70 72 20 2a 70 52 69 67 68 74 20 3d 20 70  Expr *pRight = p
350f0 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20  Expr->pRight;.  
350f1 20 20 20 20 20 20 69 6e 74 20 69 4c 65 66 74 20        int iLeft 
350f2 3d 20 2a 70 69 4c 65 66 74 3b 0a 20 20 20 20 20  = *piLeft;.     
350f3 20 20 20 69 6e 74 20 6e 4e 65 61 72 20 3d 20 70     int nNear = p
350f4 45 78 70 72 2d 3e 6e 4e 65 61 72 3b 0a 20 20 20  Expr->nNear;.   
350f5 20 20 20 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 20       int nToken 
350f6 3d 20 70 52 69 67 68 74 2d 3e 70 50 68 72 61 73  = pRight->pPhras
350f7 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20  e->nToken;.     
350f8 20 20 20 69 6e 74 20 6a 6a 2c 20 69 69 3b 0a 20     int jj, ii;. 
350f9 20 20 20 20 20 20 20 69 66 28 20 70 4c 65 66 74         if( pLeft
350fa 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52  ->eType==FTSQUER
350fb 59 5f 4e 45 41 52 20 29 7b 0a 20 20 20 20 20 20  Y_NEAR ){.      
350fc 20 20 20 20 70 4c 65 66 74 20 3d 20 70 4c 65 66      pLeft = pLef
350fd 74 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20  t->pRight;.     
350fe 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73     }.        ass
350ff 65 72 74 28 20 70 52 69 67 68 74 2d 3e 65 54 79  ert( pRight->eTy
35100 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48 52  pe==FTSQUERY_PHR
35101 41 53 45 20 29 3b 0a 20 20 20 20 20 20 20 20 61  ASE );.        a
35102 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e 65 54  ssert( pLeft->eT
35103 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48  ype==FTSQUERY_PH
35104 52 41 53 45 20 29 3b 0a 20 20 20 20 20 20 20 20  RASE );.        
35105 6e 54 6f 6b 65 6e 20 2b 3d 20 70 4c 65 66 74 2d  nToken += pLeft-
35106 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e  >pPhrase->nToken
35107 3b 0a 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  ;..        for(i
35108 69 3d 30 3b 20 69 69 3c 70 53 6e 69 70 70 65 74  i=0; ii<pSnippet
35109 2d 3e 6e 4d 61 74 63 68 3b 20 69 69 2b 2b 29 7b  ->nMatch; ii++){
3510a 0a 20 20 20 20 20 20 20 20 20 20 73 74 72 75 63  .          struc
3510b 74 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20 2a  t snippetMatch *
3510c 70 20 3d 20 26 70 53 6e 69 70 70 65 74 2d 3e 61  p = &pSnippet->a
3510d 4d 61 74 63 68 5b 69 69 5d 3b 0a 20 20 20 20 20  Match[ii];.     
3510e 20 20 20 20 20 69 66 28 20 70 2d 3e 69 54 65 72       if( p->iTer
3510f 6d 3d 3d 69 4c 65 66 74 20 29 7b 0a 20 20 20 20  m==iLeft ){.    
35110 20 20 20 20 20 20 20 20 69 6e 74 20 69 73 4f 6b          int isOk
35111 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
35112 20 20 2f 2a 20 53 6e 69 70 70 65 74 20 69 69 20    /* Snippet ii 
35113 69 73 20 61 6e 20 6f 63 63 75 72 65 6e 63 65 20  is an occurence 
35114 6f 66 20 71 75 65 72 79 20 74 65 72 6d 20 69 4c  of query term iL
35115 65 66 74 20 69 6e 20 74 68 65 20 64 6f 63 75 6d  eft in the docum
35116 65 6e 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20  ent..           
35117 20 2a 2a 20 49 74 20 6f 63 63 75 72 73 20 61 74   ** It occurs at
35118 20 70 6f 73 69 74 69 6f 6e 20 28 70 2d 3e 69 54   position (p->iT
35119 6f 6b 65 6e 29 20 6f 66 20 74 68 65 20 64 6f 63  oken) of the doc
3511a 75 6d 65 6e 74 2e 20 57 65 20 6e 6f 77 0a 20 20  ument. We now.  
3511b 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 65 61            ** sea
3511c 72 63 68 20 66 6f 72 20 61 6e 20 69 6e 73 74 61  rch for an insta
3511d 6e 63 65 20 6f 66 20 74 6f 6b 65 6e 20 28 69 4c  nce of token (iL
3511e 65 66 74 2d 31 29 20 73 6f 6d 65 77 68 65 72 65  eft-1) somewhere
3511f 20 69 6e 20 74 68 65 20 0a 20 20 20 20 20 20 20   in the .       
35120 20 20 20 20 20 2a 2a 20 72 61 6e 67 65 20 28 70       ** range (p
35121 2d 3e 69 54 6f 6b 65 6e 20 2d 20 6e 4e 65 61 72  ->iToken - nNear
35122 29 2e 2e 2e 28 70 2d 3e 69 54 6f 6b 65 6e 20 2b  )...(p->iToken +
35123 20 6e 4e 65 61 72 20 2b 20 6e 54 6f 6b 65 6e 29   nNear + nToken)
35124 20 77 69 74 68 69 6e 20 0a 20 20 20 20 20 20 20   within .       
35125 20 20 20 20 20 2a 2a 20 74 68 65 20 73 65 74 20       ** the set 
35126 6f 66 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20  of snippetMatch 
35127 73 74 72 75 63 74 75 72 65 73 2e 20 49 66 20 6f  structures. If o
35128 6e 65 20 69 73 20 66 6f 75 6e 64 2c 20 70 72 6f  ne is found, pro
35129 63 65 65 64 2e 20 0a 20 20 20 20 20 20 20 20 20  ceed. .         
3512a 20 20 20 2a 2a 20 49 66 20 6f 6e 65 20 63 61 6e     ** If one can
3512b 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c 20 74 68  not be found, th
3512c 65 6e 20 72 65 6d 6f 76 65 20 73 6e 69 70 70 65  en remove snippe
3512d 74 73 20 69 69 2e 2e 28 69 69 2b 4e 2d 31 29 20  ts ii..(ii+N-1) 
3512e 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
3512f 66 72 6f 6d 20 74 68 65 20 6d 61 74 63 68 69 6e  from the matchin
35130 67 20 73 6e 69 70 70 65 74 73 2c 20 77 68 65 72  g snippets, wher
35131 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65  e N is the numbe
35132 72 20 6f 66 20 74 6f 6b 65 6e 73 20 0a 20 20 20  r of tokens .   
35133 20 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 70           ** in p
35134 68 72 61 73 65 20 70 52 69 67 68 74 2d 3e 70 50  hrase pRight->pP
35135 68 72 61 73 65 2e 0a 20 20 20 20 20 20 20 20 20  hrase..         
35136 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
35137 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 69 73 4f 6b    for(jj=0; isOk
35138 3d 3d 30 20 26 26 20 6a 6a 3c 70 53 6e 69 70 70  ==0 && jj<pSnipp
35139 65 74 2d 3e 6e 4d 61 74 63 68 3b 20 6a 6a 2b 2b  et->nMatch; jj++
3513a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
3513b 20 73 74 72 75 63 74 20 73 6e 69 70 70 65 74 4d   struct snippetM
3513c 61 74 63 68 20 2a 70 32 20 3d 20 26 70 53 6e 69  atch *p2 = &pSni
3513d 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b 6a 6a 5d  ppet->aMatch[jj]
3513e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
3513f 69 66 28 20 70 32 2d 3e 69 54 65 72 6d 3d 3d 28  if( p2->iTerm==(
35140 69 4c 65 66 74 2d 31 29 20 29 7b 0a 20 20 20 20  iLeft-1) ){.    
35141 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
35142 70 32 2d 3e 69 54 6f 6b 65 6e 3e 3d 28 70 2d 3e  p2->iToken>=(p->
35143 69 54 6f 6b 65 6e 2d 6e 4e 65 61 72 2d 31 29 20  iToken-nNear-1) 
35144 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
35145 20 20 26 26 20 70 32 2d 3e 69 54 6f 6b 65 6e 3c    && p2->iToken<
35146 28 70 2d 3e 69 54 6f 6b 65 6e 2b 6e 4e 65 61 72  (p->iToken+nNear
35147 2b 6e 54 6f 6b 65 6e 29 20 0a 20 20 20 20 20 20  +nToken) .      
35148 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20            ){.   
35149 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
3514a 73 4f 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  sOk = 1;.       
3514b 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
3514c 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
3514d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
3514e 20 20 20 20 20 69 66 28 20 21 69 73 4f 6b 20 29       if( !isOk )
3514f 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
35150 69 6e 74 20 6b 6b 3b 0a 20 20 20 20 20 20 20 20  int kk;.        
35151 20 20 20 20 20 20 66 6f 72 28 6b 6b 3d 30 3b 20        for(kk=0; 
35152 6b 6b 3c 70 52 69 67 68 74 2d 3e 70 50 68 72 61  kk<pRight->pPhra
35153 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 20 6b 6b 2b 2b  se->nToken; kk++
35154 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
35155 20 20 20 70 53 6e 69 70 70 65 74 2d 3e 61 4d 61     pSnippet->aMa
35156 74 63 68 5b 6b 6b 2b 69 69 5d 2e 69 54 65 72 6d  tch[kk+ii].iTerm
35157 20 3d 20 2d 32 3b 0a 20 20 20 20 20 20 20 20 20   = -2;.         
35158 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
35159 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
3515a 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
3515b 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
3515c 20 20 20 69 66 28 20 70 2d 3e 69 54 65 72 6d 3d     if( p->iTerm=
3515d 3d 28 69 4c 65 66 74 2d 31 29 20 29 7b 0a 20 20  =(iLeft-1) ){.  
3515e 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 73            int is
3515f 4f 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  Ok = 0;.        
35160 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 69 73      for(jj=0; is
35161 4f 6b 3d 3d 30 20 26 26 20 6a 6a 3c 70 53 6e 69  Ok==0 && jj<pSni
35162 70 70 65 74 2d 3e 6e 4d 61 74 63 68 3b 20 6a 6a  ppet->nMatch; jj
35163 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
35164 20 20 20 73 74 72 75 63 74 20 73 6e 69 70 70 65     struct snippe
35165 74 4d 61 74 63 68 20 2a 70 32 20 3d 20 26 70 53  tMatch *p2 = &pS
35166 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b 6a  nippet->aMatch[j
35167 6a 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  j];.            
35168 20 20 69 66 28 20 70 32 2d 3e 69 54 65 72 6d 3d    if( p2->iTerm=
35169 3d 69 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20  =iLeft ){.      
3516a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 32            if( p2
3516b 2d 3e 69 54 6f 6b 65 6e 3c 3d 28 70 2d 3e 69 54  ->iToken<=(p->iT
3516c 6f 6b 65 6e 2b 6e 4e 65 61 72 2b 31 29 20 0a 20  oken+nNear+1) . 
3516d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3516e 26 26 20 70 32 2d 3e 69 54 6f 6b 65 6e 3e 28 70  && p2->iToken>(p
3516f 2d 3e 69 54 6f 6b 65 6e 2d 6e 4e 65 61 72 2d 6e  ->iToken-nNear-n
35170 54 6f 6b 65 6e 29 20 0a 20 20 20 20 20 20 20 20  Token) .        
35171 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20          ){.     
35172 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 4f               isO
35173 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  k = 1;.         
35174 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
35175 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
35176 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
35177 20 20 20 69 66 28 20 21 69 73 4f 6b 20 29 7b 0a     if( !isOk ){.
35178 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
35179 74 20 6b 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  t kk;.          
3517a 20 20 20 20 66 6f 72 28 6b 6b 3d 30 3b 20 6b 6b      for(kk=0; kk
3517b 3c 70 4c 65 66 74 2d 3e 70 50 68 72 61 73 65 2d  <pLeft->pPhrase-
3517c 3e 6e 54 6f 6b 65 6e 3b 20 6b 6b 2b 2b 29 7b 0a  >nToken; kk++){.
3517d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3517e 70 53 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68  pSnippet->aMatch
3517f 5b 69 69 2d 6b 6b 5d 2e 69 54 65 72 6d 20 3d 20  [ii-kk].iTerm = 
35180 2d 32 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  -2;.            
35181 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
35182 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
35183 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
35184 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
35185 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
35186 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
35187 20 20 69 66 28 20 74 72 69 6d 53 6e 69 70 70 65    if( trimSnippe
35188 74 4f 66 66 73 65 74 73 28 70 45 78 70 72 2d 3e  tOffsets(pExpr->
35189 70 52 69 67 68 74 2c 20 70 53 6e 69 70 70 65 74  pRight, pSnippet
3518a 2c 20 70 69 4c 65 66 74 29 20 29 7b 0a 20 20 20  , piLeft) ){.   
3518b 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
3518c 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
3518d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  0;.}../*.** Comp
3518e 75 74 65 20 61 6c 6c 20 6f 66 66 73 65 74 73 20  ute all offsets 
3518f 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
35190 72 6f 77 20 6f 66 20 74 68 65 20 71 75 65 72 79  row of the query
35191 2e 20 20 0a 2a 2a 20 49 66 20 74 68 65 20 6f 66  .  .** If the of
35192 66 73 65 74 73 20 68 61 76 65 20 61 6c 72 65 61  fsets have alrea
35193 64 79 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64  dy been computed
35194 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
35195 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74  s a no-op..*/.st
35196 61 74 69 63 20 76 6f 69 64 20 73 6e 69 70 70 65  atic void snippe
35197 74 41 6c 6c 4f 66 66 73 65 74 73 28 66 75 6c 6c  tAllOffsets(full
35198 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 70 29 7b  text_cursor *p){
35199 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 0a  .  int nColumn;.
3519a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 69    int iColumn, i
3519b 3b 0a 20 20 69 6e 74 20 69 46 69 72 73 74 2c 20  ;.  int iFirst, 
3519c 69 4c 61 73 74 3b 0a 20 20 69 6e 74 20 69 54 65  iLast;.  int iTe
3519d 72 6d 20 3d 20 30 3b 0a 20 20 66 75 6c 6c 74 65  rm = 0;.  fullte
3519e 78 74 5f 76 74 61 62 20 2a 70 46 74 73 20 3d 20  xt_vtab *pFts = 
3519f 63 75 72 73 6f 72 5f 76 74 61 62 28 70 29 3b 0a  cursor_vtab(p);.
351a0 0a 20 20 69 66 28 20 70 2d 3e 73 6e 69 70 70 65  .  if( p->snippe
351a1 74 2e 6e 4d 61 74 63 68 20 7c 7c 20 70 2d 3e 70  t.nMatch || p->p
351a2 45 78 70 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72  Expr==0 ){.    r
351a3 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6e 43 6f  eturn;.  }.  nCo
351a4 6c 75 6d 6e 20 3d 20 70 46 74 73 2d 3e 6e 43 6f  lumn = pFts->nCo
351a5 6c 75 6d 6e 3b 0a 20 20 69 43 6f 6c 75 6d 6e 20  lumn;.  iColumn 
351a6 3d 20 28 70 2d 3e 69 43 75 72 73 6f 72 54 79 70  = (p->iCursorTyp
351a7 65 20 2d 20 51 55 45 52 59 5f 46 55 4c 4c 54 45  e - QUERY_FULLTE
351a8 58 54 29 3b 0a 20 20 69 66 28 20 69 43 6f 6c 75  XT);.  if( iColu
351a9 6d 6e 3c 30 20 7c 7c 20 69 43 6f 6c 75 6d 6e 3e  mn<0 || iColumn>
351aa 3d 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20  =nColumn ){.    
351ab 2f 2a 20 4c 6f 6f 6b 20 66 6f 72 20 6d 61 74 63  /* Look for matc
351ac 68 65 73 20 6f 76 65 72 20 61 6c 6c 20 63 6f 6c  hes over all col
351ad 75 6d 6e 73 20 6f 66 20 74 68 65 20 66 75 6c 6c  umns of the full
351ae 2d 74 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a 20  -text index */. 
351af 20 20 20 69 46 69 72 73 74 20 3d 20 30 3b 0a 20     iFirst = 0;. 
351b0 20 20 20 69 4c 61 73 74 20 3d 20 6e 43 6f 6c 75     iLast = nColu
351b1 6d 6e 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  mn-1;.  }else{. 
351b2 20 20 20 2f 2a 20 4c 6f 6f 6b 20 66 6f 72 20 6d     /* Look for m
351b3 61 74 63 68 65 73 20 69 6e 20 74 68 65 20 69 43  atches in the iC
351b4 6f 6c 75 6d 6e 2d 74 68 20 63 6f 6c 75 6d 6e 20  olumn-th column 
351b5 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 6c  of the index onl
351b6 79 20 2a 2f 0a 20 20 20 20 69 46 69 72 73 74 20  y */.    iFirst 
351b7 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 69  = iColumn;.    i
351b8 4c 61 73 74 20 3d 20 69 43 6f 6c 75 6d 6e 3b 0a  Last = iColumn;.
351b9 20 20 7d 0a 20 20 66 6f 72 28 69 3d 69 46 69 72    }.  for(i=iFir
351ba 73 74 3b 20 69 3c 3d 69 4c 61 73 74 3b 20 69 2b  st; i<=iLast; i+
351bb 2b 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  +){.    const ch
351bc 61 72 20 2a 7a 44 6f 63 3b 0a 20 20 20 20 69 6e  ar *zDoc;.    in
351bd 74 20 6e 44 6f 63 3b 0a 20 20 20 20 7a 44 6f 63  t nDoc;.    zDoc
351be 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29   = (const char*)
351bf 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
351c0 65 78 74 28 70 2d 3e 70 53 74 6d 74 2c 20 69 2b  ext(p->pStmt, i+
351c1 31 29 3b 0a 20 20 20 20 6e 44 6f 63 20 3d 20 73  1);.    nDoc = s
351c2 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
351c3 74 65 73 28 70 2d 3e 70 53 74 6d 74 2c 20 69 2b  tes(p->pStmt, i+
351c4 31 29 3b 0a 20 20 20 20 73 6e 69 70 70 65 74 4f  1);.    snippetO
351c5 66 66 73 65 74 73 4f 66 43 6f 6c 75 6d 6e 28 70  ffsetsOfColumn(p
351c6 2c 20 26 70 2d 3e 73 6e 69 70 70 65 74 2c 20 69  , &p->snippet, i
351c7 2c 20 7a 44 6f 63 2c 20 6e 44 6f 63 29 3b 0a 20  , zDoc, nDoc);. 
351c8 20 7d 0a 0a 20 20 77 68 69 6c 65 28 20 74 72 69   }..  while( tri
351c9 6d 53 6e 69 70 70 65 74 4f 66 66 73 65 74 73 28  mSnippetOffsets(
351ca 70 2d 3e 70 45 78 70 72 2c 20 26 70 2d 3e 73 6e  p->pExpr, &p->sn
351cb 69 70 70 65 74 2c 20 26 69 54 65 72 6d 29 20 29  ippet, &iTerm) )
351cc 7b 0a 20 20 20 20 69 54 65 72 6d 20 3d 20 30 3b  {.    iTerm = 0;
351cd 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  .  }.}../*.** Co
351ce 6e 76 65 72 74 20 74 68 65 20 69 6e 66 6f 72 6d  nvert the inform
351cf 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 61 4d 61  ation in the aMa
351d0 74 63 68 5b 5d 20 61 72 72 61 79 20 6f 66 20 74  tch[] array of t
351d1 68 65 20 73 6e 69 70 70 65 74 0a 2a 2a 20 69 6e  he snippet.** in
351d2 74 6f 20 74 68 65 20 73 74 72 69 6e 67 20 7a 4f  to the string zO
351d3 66 66 73 65 74 5b 30 2e 2e 6e 4f 66 66 73 65 74  ffset[0..nOffset
351d4 2d 31 5d 2e 20 54 68 69 73 20 73 74 72 69 6e 67  -1]. This string
351d5 20 69 73 20 75 73 65 64 20 61 73 0a 2a 2a 20 74   is used as.** t
351d6 68 65 20 72 65 74 75 72 6e 20 6f 66 20 74 68 65  he return of the
351d7 20 53 51 4c 20 6f 66 66 73 65 74 73 28 29 20 66   SQL offsets() f
351d8 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  unction..*/.stat
351d9 69 63 20 76 6f 69 64 20 73 6e 69 70 70 65 74 4f  ic void snippetO
351da 66 66 73 65 74 54 65 78 74 28 53 6e 69 70 70 65  ffsetText(Snippe
351db 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  t *p){.  int i;.
351dc 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20    int cnt = 0;. 
351dd 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 73 62   StringBuffer sb
351de 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30  ;.  char zBuf[20
351df 30 5d 3b 0a 20 20 69 66 28 20 70 2d 3e 7a 4f 66  0];.  if( p->zOf
351e0 66 73 65 74 20 29 20 72 65 74 75 72 6e 3b 0a 20  fset ) return;. 
351e1 20 69 6e 69 74 53 74 72 69 6e 67 42 75 66 66 65   initStringBuffe
351e2 72 28 26 73 62 29 3b 0a 20 20 66 6f 72 28 69 3d  r(&sb);.  for(i=
351e3 30 3b 20 69 3c 70 2d 3e 6e 4d 61 74 63 68 3b 20  0; i<p->nMatch; 
351e4 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74  i++){.    struct
351e5 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20 2a 70   snippetMatch *p
351e6 4d 61 74 63 68 20 3d 20 26 70 2d 3e 61 4d 61 74  Match = &p->aMat
351e7 63 68 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70  ch[i];.    if( p
351e8 4d 61 74 63 68 2d 3e 69 54 65 72 6d 3e 3d 30 20  Match->iTerm>=0 
351e9 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 73  ){.      /* If s
351ea 6e 69 70 70 65 74 4d 61 74 63 68 2e 69 54 65 72  nippetMatch.iTer
351eb 6d 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 30  m is less than 0
351ec 2c 20 74 68 65 6e 20 74 68 65 20 6d 61 74 63 68  , then the match
351ed 20 77 61 73 20 0a 20 20 20 20 20 20 2a 2a 20 64   was .      ** d
351ee 69 73 63 61 72 64 65 64 20 61 73 20 70 61 72 74  iscarded as part
351ef 20 6f 66 20 70 72 6f 63 65 73 73 69 6e 67 20 74   of processing t
351f0 68 65 20 4e 45 41 52 20 6f 70 65 72 61 74 6f 72  he NEAR operator
351f1 20 28 73 65 65 20 74 68 65 20 0a 20 20 20 20 20   (see the .     
351f2 20 2a 2a 20 74 72 69 6d 53 6e 69 70 70 65 74 4f   ** trimSnippetO
351f3 66 66 73 65 74 73 46 6f 72 4e 65 61 72 28 29 20  ffsetsForNear() 
351f4 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 64 65 74  function for det
351f5 61 69 6c 73 29 2e 20 49 67 6e 6f 72 65 20 0a 20  ails). Ignore . 
351f6 20 20 20 20 20 2a 2a 20 69 74 20 69 6e 20 74 68       ** it in th
351f7 69 73 20 63 61 73 65 0a 20 20 20 20 20 20 2a 2f  is case.      */
351f8 0a 20 20 20 20 20 20 7a 42 75 66 5b 30 5d 20 3d  .      zBuf[0] =
351f9 20 27 20 27 3b 0a 20 20 20 20 20 20 73 71 6c 69   ' ';.      sqli
351fa 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
351fb 65 6f 66 28 7a 42 75 66 29 2d 31 2c 20 26 7a 42  eof(zBuf)-1, &zB
351fc 75 66 5b 63 6e 74 3e 30 5d 2c 20 22 25 64 20 25  uf[cnt>0], "%d %
351fd 64 20 25 64 20 25 64 22 2c 0a 20 20 20 20 20 20  d %d %d",.      
351fe 20 20 20 20 70 4d 61 74 63 68 2d 3e 69 43 6f 6c      pMatch->iCol
351ff 2c 20 70 4d 61 74 63 68 2d 3e 69 54 65 72 6d 2c  , pMatch->iTerm,
35200 20 70 4d 61 74 63 68 2d 3e 69 53 74 61 72 74 2c   pMatch->iStart,
35201 20 70 4d 61 74 63 68 2d 3e 6e 42 79 74 65 29 3b   pMatch->nByte);
35202 0a 20 20 20 20 20 20 61 70 70 65 6e 64 28 26 73  .      append(&s
35203 62 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 20 20  b, zBuf);.      
35204 63 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  cnt++;.    }.  }
35205 0a 20 20 70 2d 3e 7a 4f 66 66 73 65 74 20 3d 20  .  p->zOffset = 
35206 73 74 72 69 6e 67 42 75 66 66 65 72 44 61 74 61  stringBufferData
35207 28 26 73 62 29 3b 0a 20 20 70 2d 3e 6e 4f 66 66  (&sb);.  p->nOff
35208 73 65 74 20 3d 20 73 74 72 69 6e 67 42 75 66 66  set = stringBuff
35209 65 72 4c 65 6e 67 74 68 28 26 73 62 29 3b 0a 7d  erLength(&sb);.}
3520a 0a 0a 2f 2a 0a 2a 2a 20 7a 44 6f 63 5b 30 2e 2e  ../*.** zDoc[0..
3520b 6e 44 6f 63 2d 31 5d 20 69 73 20 70 68 72 61 73  nDoc-1] is phras
3520c 65 20 6f 66 20 74 65 78 74 2e 20 20 61 4d 61 74  e of text.  aMat
3520d 63 68 5b 30 2e 2e 6e 4d 61 74 63 68 2d 31 5d 20  ch[0..nMatch-1] 
3520e 61 72 65 20 61 20 73 65 74 0a 2a 2a 20 6f 66 20  are a set.** of 
3520f 6d 61 74 63 68 69 6e 67 20 77 6f 72 64 73 20 73  matching words s
35210 6f 6d 65 20 6f 66 20 77 68 69 63 68 20 6d 69 67  ome of which mig
35211 68 74 20 62 65 20 69 6e 20 7a 44 6f 63 2e 20 20  ht be in zDoc.  
35212 7a 44 6f 63 20 69 73 20 63 6f 6c 75 6d 6e 0a 2a  zDoc is column.*
35213 2a 20 6e 75 6d 62 65 72 20 69 43 6f 6c 2e 0a 2a  * number iCol..*
35214 2a 0a 2a 2a 20 69 42 72 65 61 6b 20 69 73 20 73  *.** iBreak is s
35215 75 67 67 65 73 74 65 64 20 73 70 6f 74 20 69 6e  uggested spot in
35216 20 7a 44 6f 63 20 77 68 65 72 65 20 77 65 20 63   zDoc where we c
35217 6f 75 6c 64 20 62 65 67 69 6e 20 6f 72 20 65 6e  ould begin or en
35218 64 20 61 6e 0a 2a 2a 20 65 78 63 65 72 70 74 2e  d an.** excerpt.
35219 20 20 52 65 74 75 72 6e 20 61 20 76 61 6c 75 65    Return a value
3521a 20 73 69 6d 69 6c 61 72 20 74 6f 20 69 42 72 65   similar to iBre
3521b 61 6b 20 62 75 74 20 70 6f 73 73 69 62 6c 79 20  ak but possibly 
3521c 61 64 6a 75 73 74 65 64 0a 2a 2a 20 74 6f 20 62  adjusted.** to b
3521d 65 20 61 20 6c 69 74 74 6c 65 20 6c 65 66 74 20  e a little left 
3521e 6f 72 20 72 69 67 68 74 20 73 6f 20 74 68 61 74  or right so that
3521f 20 74 68 65 20 62 72 65 61 6b 20 70 6f 69 6e 74   the break point
35220 20 69 73 20 62 65 74 74 65 72 2e 0a 2a 2f 0a 73   is better..*/.s
35221 74 61 74 69 63 20 69 6e 74 20 77 6f 72 64 42 6f  tatic int wordBo
35222 75 6e 64 61 72 79 28 0a 20 20 69 6e 74 20 69 42  undary(.  int iB
35223 72 65 61 6b 2c 20 20 20 20 20 20 20 20 20 20 20  reak,           
35224 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73          /* The s
35225 75 67 67 65 73 74 65 64 20 62 72 65 61 6b 20 70  uggested break p
35226 6f 69 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  oint */.  const 
35227 63 68 61 72 20 2a 7a 44 6f 63 2c 20 20 20 20 20  char *zDoc,     
35228 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 63 75 6d          /* Docum
35229 65 6e 74 20 74 65 78 74 20 2a 2f 0a 20 20 69 6e  ent text */.  in
3522a 74 20 6e 44 6f 63 2c 20 20 20 20 20 20 20 20 20  t nDoc,         
3522b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
3522c 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
3522d 6e 20 7a 44 6f 63 5b 5d 20 2a 2f 0a 20 20 73 74  n zDoc[] */.  st
3522e 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63  ruct snippetMatc
3522f 68 20 2a 61 4d 61 74 63 68 2c 20 20 2f 2a 20 4d  h *aMatch,  /* M
35230 61 74 63 68 69 6e 67 20 77 6f 72 64 73 20 2a 2f  atching words */
35231 0a 20 20 69 6e 74 20 6e 4d 61 74 63 68 2c 20 20  .  int nMatch,  
35232 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35233 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
35234 74 72 69 65 73 20 69 6e 20 61 4d 61 74 63 68 5b  tries in aMatch[
35235 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 20  ] */.  int iCol 
35236 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35237 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75       /* The colu
35238 6d 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20 7a 44  mn number for zD
35239 6f 63 5b 5d 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  oc[] */.){.  int
3523a 20 69 3b 0a 20 20 69 66 28 20 69 42 72 65 61 6b   i;.  if( iBreak
3523b 3c 3d 31 30 20 29 7b 0a 20 20 20 20 72 65 74 75  <=10 ){.    retu
3523c 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rn 0;.  }.  if( 
3523d 69 42 72 65 61 6b 3e 3d 6e 44 6f 63 2d 31 30 20  iBreak>=nDoc-10 
3523e 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 44  ){.    return nD
3523f 6f 63 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  oc;.  }.  for(i=
35240 30 3b 20 69 3c 6e 4d 61 74 63 68 20 26 26 20 61  0; i<nMatch && a
35241 4d 61 74 63 68 5b 69 5d 2e 69 43 6f 6c 3c 69 43  Match[i].iCol<iC
35242 6f 6c 3b 20 69 2b 2b 29 7b 7d 0a 20 20 77 68 69  ol; i++){}.  whi
35243 6c 65 28 20 69 3c 6e 4d 61 74 63 68 20 26 26 20  le( i<nMatch && 
35244 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61 72 74  aMatch[i].iStart
35245 2b 61 4d 61 74 63 68 5b 69 5d 2e 6e 42 79 74 65  +aMatch[i].nByte
35246 3c 69 42 72 65 61 6b 20 29 7b 20 69 2b 2b 3b 20  <iBreak ){ i++; 
35247 7d 0a 20 20 69 66 28 20 69 3c 6e 4d 61 74 63 68  }.  if( i<nMatch
35248 20 29 7b 0a 20 20 20 20 69 66 28 20 61 4d 61 74   ){.    if( aMat
35249 63 68 5b 69 5d 2e 69 53 74 61 72 74 3c 69 42 72  ch[i].iStart<iBr
3524a 65 61 6b 2b 31 30 20 29 7b 0a 20 20 20 20 20 20  eak+10 ){.      
3524b 72 65 74 75 72 6e 20 61 4d 61 74 63 68 5b 69 5d  return aMatch[i]
3524c 2e 69 53 74 61 72 74 3b 0a 20 20 20 20 7d 0a 20  .iStart;.    }. 
3524d 20 20 20 69 66 28 20 69 3e 30 20 26 26 20 61 4d     if( i>0 && aM
3524e 61 74 63 68 5b 69 2d 31 5d 2e 69 53 74 61 72 74  atch[i-1].iStart
3524f 2b 61 4d 61 74 63 68 5b 69 2d 31 5d 2e 6e 42 79  +aMatch[i-1].nBy
35250 74 65 3e 3d 69 42 72 65 61 6b 20 29 7b 0a 20 20  te>=iBreak ){.  
35251 20 20 20 20 72 65 74 75 72 6e 20 61 4d 61 74 63      return aMatc
35252 68 5b 69 2d 31 5d 2e 69 53 74 61 72 74 3b 0a 20  h[i-1].iStart;. 
35253 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69     }.  }.  for(i
35254 3d 31 3b 20 69 3c 3d 31 30 3b 20 69 2b 2b 29 7b  =1; i<=10; i++){
35255 0a 20 20 20 20 69 66 28 20 73 61 66 65 5f 69 73  .    if( safe_is
35256 73 70 61 63 65 28 7a 44 6f 63 5b 69 42 72 65 61  space(zDoc[iBrea
35257 6b 2d 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 72  k-i]) ){.      r
35258 65 74 75 72 6e 20 69 42 72 65 61 6b 20 2d 20 69  eturn iBreak - i
35259 20 2b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   + 1;.    }.    
3525a 69 66 28 20 73 61 66 65 5f 69 73 73 70 61 63 65  if( safe_isspace
3525b 28 7a 44 6f 63 5b 69 42 72 65 61 6b 2b 69 5d 29  (zDoc[iBreak+i])
3525c 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
3525d 20 69 42 72 65 61 6b 20 2b 20 69 20 2b 20 31 3b   iBreak + i + 1;
3525e 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
3525f 75 72 6e 20 69 42 72 65 61 6b 3b 0a 7d 0a 0a 0a  urn iBreak;.}...
35260 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76  ./*.** Allowed v
35261 61 6c 75 65 73 20 66 6f 72 20 53 6e 69 70 70 65  alues for Snippe
35262 74 2e 61 4d 61 74 63 68 5b 5d 2e 73 6e 53 74 61  t.aMatch[].snSta
35263 74 75 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  tus.*/.#define S
35264 4e 49 50 50 45 54 5f 49 47 4e 4f 52 45 20 20 30  NIPPET_IGNORE  0
35265 20 20 20 2f 2a 20 49 74 20 69 73 20 6f 6b 20 74     /* It is ok t
35266 6f 20 6f 6d 69 74 20 74 68 69 73 20 6d 61 74 63  o omit this matc
35267 68 20 66 72 6f 6d 20 74 68 65 20 73 6e 69 70 70  h from the snipp
35268 65 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 4e  et */.#define SN
35269 49 50 50 45 54 5f 44 45 53 49 52 45 44 20 31 20  IPPET_DESIRED 1 
3526a 20 20 2f 2a 20 57 65 20 77 61 6e 74 20 74 6f 20    /* We want to 
3526b 69 6e 63 6c 75 64 65 20 74 68 69 73 20 6d 61 74  include this mat
3526c 63 68 20 69 6e 20 74 68 65 20 73 6e 69 70 70 65  ch in the snippe
3526d 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  t */../*.** Gene
3526e 72 61 74 65 20 74 68 65 20 74 65 78 74 20 6f 66  rate the text of
3526f 20 61 20 73 6e 69 70 70 65 74 2e 0a 2a 2f 0a 73   a snippet..*/.s
35270 74 61 74 69 63 20 76 6f 69 64 20 73 6e 69 70 70  tatic void snipp
35271 65 74 54 65 78 74 28 0a 20 20 66 75 6c 6c 74 65  etText(.  fullte
35272 78 74 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  xt_cursor *pCurs
35273 6f 72 2c 20 20 20 2f 2a 20 54 68 65 20 63 75 72  or,   /* The cur
35274 73 6f 72 20 77 65 20 6e 65 65 64 20 74 68 65 20  sor we need the 
35275 73 6e 69 70 70 65 74 20 66 6f 72 20 2a 2f 0a 20  snippet for */. 
35276 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74   const char *zSt
35277 61 72 74 4d 61 72 6b 2c 20 20 20 20 20 2f 2a 20  artMark,     /* 
35278 4d 61 72 6b 75 70 20 74 6f 20 61 70 70 65 61 72  Markup to appear
35279 20 62 65 66 6f 72 65 20 65 61 63 68 20 6d 61 74   before each mat
3527a 63 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ch */.  const ch
3527b 61 72 20 2a 7a 45 6e 64 4d 61 72 6b 2c 20 20 20  ar *zEndMark,   
3527c 20 20 20 20 2f 2a 20 4d 61 72 6b 75 70 20 74 6f      /* Markup to
3527d 20 61 70 70 65 61 72 20 61 66 74 65 72 20 65 61   appear after ea
3527e 63 68 20 6d 61 74 63 68 20 2a 2f 0a 20 20 63 6f  ch match */.  co
3527f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6c 6c 69 70  nst char *zEllip
35280 73 69 73 20 20 20 20 20 20 20 2f 2a 20 45 6c 6c  sis       /* Ell
35281 69 70 73 69 73 20 6d 61 72 6b 20 2a 2f 0a 29 7b  ipsis mark */.){
35282 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73  .  int i, j;.  s
35283 74 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74  truct snippetMat
35284 63 68 20 2a 61 4d 61 74 63 68 3b 0a 20 20 69 6e  ch *aMatch;.  in
35285 74 20 6e 4d 61 74 63 68 3b 0a 20 20 69 6e 74 20  t nMatch;.  int 
35286 6e 44 65 73 69 72 65 64 3b 0a 20 20 53 74 72 69  nDesired;.  Stri
35287 6e 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20 69  ngBuffer sb;.  i
35288 6e 74 20 74 61 69 6c 43 6f 6c 3b 0a 20 20 69 6e  nt tailCol;.  in
35289 74 20 74 61 69 6c 4f 66 66 73 65 74 3b 0a 20 20  t tailOffset;.  
3528a 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 69 6e 74 20  int iCol;.  int 
3528b 6e 44 6f 63 3b 0a 20 20 63 6f 6e 73 74 20 63 68  nDoc;.  const ch
3528c 61 72 20 2a 7a 44 6f 63 3b 0a 20 20 69 6e 74 20  ar *zDoc;.  int 
3528d 69 53 74 61 72 74 2c 20 69 45 6e 64 3b 0a 20 20  iStart, iEnd;.  
3528e 69 6e 74 20 74 61 69 6c 45 6c 6c 69 70 73 69 73  int tailEllipsis
3528f 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 4d 61 74   = 0;.  int iMat
35290 63 68 3b 0a 20 20 0a 0a 20 20 73 71 6c 69 74 65  ch;.  ..  sqlite
35291 33 5f 66 72 65 65 28 70 43 75 72 73 6f 72 2d 3e  3_free(pCursor->
35292 73 6e 69 70 70 65 74 2e 7a 53 6e 69 70 70 65 74  snippet.zSnippet
35293 29 3b 0a 20 20 70 43 75 72 73 6f 72 2d 3e 73 6e  );.  pCursor->sn
35294 69 70 70 65 74 2e 7a 53 6e 69 70 70 65 74 20 3d  ippet.zSnippet =
35295 20 30 3b 0a 20 20 61 4d 61 74 63 68 20 3d 20 70   0;.  aMatch = p
35296 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e  Cursor->snippet.
35297 61 4d 61 74 63 68 3b 0a 20 20 6e 4d 61 74 63 68  aMatch;.  nMatch
35298 20 3d 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70   = pCursor->snip
35299 70 65 74 2e 6e 4d 61 74 63 68 3b 0a 20 20 69 6e  pet.nMatch;.  in
3529a 69 74 53 74 72 69 6e 67 42 75 66 66 65 72 28 26  itStringBuffer(&
3529b 73 62 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b  sb);..  for(i=0;
3529c 20 69 3c 6e 4d 61 74 63 68 3b 20 69 2b 2b 29 7b   i<nMatch; i++){
3529d 0a 20 20 20 20 61 4d 61 74 63 68 5b 69 5d 2e 73  .    aMatch[i].s
3529e 6e 53 74 61 74 75 73 20 3d 20 53 4e 49 50 50 45  nStatus = SNIPPE
3529f 54 5f 49 47 4e 4f 52 45 3b 0a 20 20 7d 0a 20 20  T_IGNORE;.  }.  
352a0 6e 44 65 73 69 72 65 64 20 3d 20 30 3b 0a 20 20  nDesired = 0;.  
352a1 66 6f 72 28 69 3d 30 3b 20 69 3c 46 54 53 33 5f  for(i=0; i<FTS3_
352a2 52 4f 54 4f 52 5f 53 5a 3b 20 69 2b 2b 29 7b 0a  ROTOR_SZ; i++){.
352a3 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e      for(j=0; j<n
352a4 4d 61 74 63 68 3b 20 6a 2b 2b 29 7b 0a 20 20 20  Match; j++){.   
352a5 20 20 20 69 66 28 20 61 4d 61 74 63 68 5b 6a 5d     if( aMatch[j]
352a6 2e 69 54 65 72 6d 3d 3d 69 20 29 7b 0a 20 20 20  .iTerm==i ){.   
352a7 20 20 20 20 20 61 4d 61 74 63 68 5b 6a 5d 2e 73       aMatch[j].s
352a8 6e 53 74 61 74 75 73 20 3d 20 53 4e 49 50 50 45  nStatus = SNIPPE
352a9 54 5f 44 45 53 49 52 45 44 3b 0a 20 20 20 20 20  T_DESIRED;.     
352aa 20 20 20 6e 44 65 73 69 72 65 64 2b 2b 3b 0a 20     nDesired++;. 
352ab 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
352ac 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
352ad 0a 20 20 69 4d 61 74 63 68 20 3d 20 30 3b 0a 20  .  iMatch = 0;. 
352ae 20 74 61 69 6c 43 6f 6c 20 3d 20 2d 31 3b 0a 20   tailCol = -1;. 
352af 20 74 61 69 6c 4f 66 66 73 65 74 20 3d 20 30 3b   tailOffset = 0;
352b0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4d  .  for(i=0; i<nM
352b1 61 74 63 68 20 26 26 20 6e 44 65 73 69 72 65 64  atch && nDesired
352b2 3e 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  >0; i++){.    if
352b3 28 20 61 4d 61 74 63 68 5b 69 5d 2e 73 6e 53 74  ( aMatch[i].snSt
352b4 61 74 75 73 21 3d 53 4e 49 50 50 45 54 5f 44 45  atus!=SNIPPET_DE
352b5 53 49 52 45 44 20 29 20 63 6f 6e 74 69 6e 75 65  SIRED ) continue
352b6 3b 0a 20 20 20 20 6e 44 65 73 69 72 65 64 2d 2d  ;.    nDesired--
352b7 3b 0a 20 20 20 20 69 43 6f 6c 20 3d 20 61 4d 61  ;.    iCol = aMa
352b8 74 63 68 5b 69 5d 2e 69 43 6f 6c 3b 0a 20 20 20  tch[i].iCol;.   
352b9 20 7a 44 6f 63 20 3d 20 28 63 6f 6e 73 74 20 63   zDoc = (const c
352ba 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c  har*)sqlite3_col
352bb 75 6d 6e 5f 74 65 78 74 28 70 43 75 72 73 6f 72  umn_text(pCursor
352bc 2d 3e 70 53 74 6d 74 2c 20 69 43 6f 6c 2b 31 29  ->pStmt, iCol+1)
352bd 3b 0a 20 20 20 20 6e 44 6f 63 20 3d 20 73 71 6c  ;.    nDoc = sql
352be 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
352bf 73 28 70 43 75 72 73 6f 72 2d 3e 70 53 74 6d 74  s(pCursor->pStmt
352c0 2c 20 69 43 6f 6c 2b 31 29 3b 0a 20 20 20 20 69  , iCol+1);.    i
352c1 53 74 61 72 74 20 3d 20 61 4d 61 74 63 68 5b 69  Start = aMatch[i
352c2 5d 2e 69 53 74 61 72 74 20 2d 20 34 30 3b 0a 20  ].iStart - 40;. 
352c3 20 20 20 69 53 74 61 72 74 20 3d 20 77 6f 72 64     iStart = word
352c4 42 6f 75 6e 64 61 72 79 28 69 53 74 61 72 74 2c  Boundary(iStart,
352c5 20 7a 44 6f 63 2c 20 6e 44 6f 63 2c 20 61 4d 61   zDoc, nDoc, aMa
352c6 74 63 68 2c 20 6e 4d 61 74 63 68 2c 20 69 43 6f  tch, nMatch, iCo
352c7 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 53 74 61  l);.    if( iSta
352c8 72 74 3c 3d 31 30 20 29 7b 0a 20 20 20 20 20 20  rt<=10 ){.      
352c9 69 53 74 61 72 74 20 3d 20 30 3b 0a 20 20 20 20  iStart = 0;.    
352ca 7d 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d  }.    if( iCol==
352cb 74 61 69 6c 43 6f 6c 20 26 26 20 69 53 74 61 72  tailCol && iStar
352cc 74 3c 3d 74 61 69 6c 4f 66 66 73 65 74 2b 32 30  t<=tailOffset+20
352cd 20 29 7b 0a 20 20 20 20 20 20 69 53 74 61 72 74   ){.      iStart
352ce 20 3d 20 74 61 69 6c 4f 66 66 73 65 74 3b 0a 20   = tailOffset;. 
352cf 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 69 43     }.    if( (iC
352d0 6f 6c 21 3d 74 61 69 6c 43 6f 6c 20 26 26 20 74  ol!=tailCol && t
352d1 61 69 6c 43 6f 6c 3e 3d 30 29 20 7c 7c 20 69 53  ailCol>=0) || iS
352d2 74 61 72 74 21 3d 74 61 69 6c 4f 66 66 73 65 74  tart!=tailOffset
352d3 20 29 7b 0a 20 20 20 20 20 20 74 72 69 6d 57 68   ){.      trimWh
352d4 69 74 65 53 70 61 63 65 28 26 73 62 29 3b 0a 20  iteSpace(&sb);. 
352d5 20 20 20 20 20 61 70 70 65 6e 64 57 68 69 74 65       appendWhite
352d6 53 70 61 63 65 28 26 73 62 29 3b 0a 20 20 20 20  Space(&sb);.    
352d7 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 7a 45    append(&sb, zE
352d8 6c 6c 69 70 73 69 73 29 3b 0a 20 20 20 20 20 20  llipsis);.      
352d9 61 70 70 65 6e 64 57 68 69 74 65 53 70 61 63 65  appendWhiteSpace
352da 28 26 73 62 29 3b 0a 20 20 20 20 7d 0a 20 20 20  (&sb);.    }.   
352db 20 69 45 6e 64 20 3d 20 61 4d 61 74 63 68 5b 69   iEnd = aMatch[i
352dc 5d 2e 69 53 74 61 72 74 20 2b 20 61 4d 61 74 63  ].iStart + aMatc
352dd 68 5b 69 5d 2e 6e 42 79 74 65 20 2b 20 34 30 3b  h[i].nByte + 40;
352de 0a 20 20 20 20 69 45 6e 64 20 3d 20 77 6f 72 64  .    iEnd = word
352df 42 6f 75 6e 64 61 72 79 28 69 45 6e 64 2c 20 7a  Boundary(iEnd, z
352e0 44 6f 63 2c 20 6e 44 6f 63 2c 20 61 4d 61 74 63  Doc, nDoc, aMatc
352e1 68 2c 20 6e 4d 61 74 63 68 2c 20 69 43 6f 6c 29  h, nMatch, iCol)
352e2 3b 0a 20 20 20 20 69 66 28 20 69 45 6e 64 3e 3d  ;.    if( iEnd>=
352e3 6e 44 6f 63 2d 31 30 20 29 7b 0a 20 20 20 20 20  nDoc-10 ){.     
352e4 20 69 45 6e 64 20 3d 20 6e 44 6f 63 3b 0a 20 20   iEnd = nDoc;.  
352e5 20 20 20 20 74 61 69 6c 45 6c 6c 69 70 73 69 73      tailEllipsis
352e6 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 0;.    }else{
352e7 0a 20 20 20 20 20 20 74 61 69 6c 45 6c 6c 69 70  .      tailEllip
352e8 73 69 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  sis = 1;.    }. 
352e9 20 20 20 77 68 69 6c 65 28 20 69 4d 61 74 63 68     while( iMatch
352ea 3c 6e 4d 61 74 63 68 20 26 26 20 61 4d 61 74 63  <nMatch && aMatc
352eb 68 5b 69 4d 61 74 63 68 5d 2e 69 43 6f 6c 3c 69  h[iMatch].iCol<i
352ec 43 6f 6c 20 29 7b 20 69 4d 61 74 63 68 2b 2b 3b  Col ){ iMatch++;
352ed 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 69 53   }.    while( iS
352ee 74 61 72 74 3c 69 45 6e 64 20 29 7b 0a 20 20 20  tart<iEnd ){.   
352ef 20 20 20 77 68 69 6c 65 28 20 69 4d 61 74 63 68     while( iMatch
352f0 3c 6e 4d 61 74 63 68 20 26 26 20 61 4d 61 74 63  <nMatch && aMatc
352f1 68 5b 69 4d 61 74 63 68 5d 2e 69 53 74 61 72 74  h[iMatch].iStart
352f2 3c 69 53 74 61 72 74 0a 20 20 20 20 20 20 20 20  <iStart.        
352f3 20 20 20 20 20 26 26 20 61 4d 61 74 63 68 5b 69       && aMatch[i
352f4 4d 61 74 63 68 5d 2e 69 43 6f 6c 3c 3d 69 43 6f  Match].iCol<=iCo
352f5 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4d 61  l ){.        iMa
352f6 74 63 68 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20  tch++;.      }. 
352f7 20 20 20 20 20 69 66 28 20 69 4d 61 74 63 68 3c       if( iMatch<
352f8 6e 4d 61 74 63 68 20 26 26 20 61 4d 61 74 63 68  nMatch && aMatch
352f9 5b 69 4d 61 74 63 68 5d 2e 69 53 74 61 72 74 3c  [iMatch].iStart<
352fa 69 45 6e 64 0a 20 20 20 20 20 20 20 20 20 20 20  iEnd.           
352fb 20 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74    && aMatch[iMat
352fc 63 68 5d 2e 69 43 6f 6c 3d 3d 69 43 6f 6c 20 29  ch].iCol==iCol )
352fd 7b 0a 20 20 20 20 20 20 20 20 6e 61 70 70 65 6e  {.        nappen
352fe 64 28 26 73 62 2c 20 26 7a 44 6f 63 5b 69 53 74  d(&sb, &zDoc[iSt
352ff 61 72 74 5d 2c 20 61 4d 61 74 63 68 5b 69 4d 61  art], aMatch[iMa
35300 74 63 68 5d 2e 69 53 74 61 72 74 20 2d 20 69 53  tch].iStart - iS
35301 74 61 72 74 29 3b 0a 20 20 20 20 20 20 20 20 69  tart);.        i
35302 53 74 61 72 74 20 3d 20 61 4d 61 74 63 68 5b 69  Start = aMatch[i
35303 4d 61 74 63 68 5d 2e 69 53 74 61 72 74 3b 0a 20  Match].iStart;. 
35304 20 20 20 20 20 20 20 61 70 70 65 6e 64 28 26 73         append(&s
35305 62 2c 20 7a 53 74 61 72 74 4d 61 72 6b 29 3b 0a  b, zStartMark);.
35306 20 20 20 20 20 20 20 20 6e 61 70 70 65 6e 64 28          nappend(
35307 26 73 62 2c 20 26 7a 44 6f 63 5b 69 53 74 61 72  &sb, &zDoc[iStar
35308 74 5d 2c 20 61 4d 61 74 63 68 5b 69 4d 61 74 63  t], aMatch[iMatc
35309 68 5d 2e 6e 42 79 74 65 29 3b 0a 20 20 20 20 20  h].nByte);.     
3530a 20 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 7a     append(&sb, z
3530b 45 6e 64 4d 61 72 6b 29 3b 0a 20 20 20 20 20 20  EndMark);.      
3530c 20 20 69 53 74 61 72 74 20 2b 3d 20 61 4d 61 74    iStart += aMat
3530d 63 68 5b 69 4d 61 74 63 68 5d 2e 6e 42 79 74 65  ch[iMatch].nByte
3530e 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d  ;.        for(j=
3530f 69 4d 61 74 63 68 2b 31 3b 20 6a 3c 6e 4d 61 74  iMatch+1; j<nMat
35310 63 68 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  ch; j++){.      
35311 20 20 20 20 69 66 28 20 61 4d 61 74 63 68 5b 6a      if( aMatch[j
35312 5d 2e 69 54 65 72 6d 3d 3d 61 4d 61 74 63 68 5b  ].iTerm==aMatch[
35313 69 4d 61 74 63 68 5d 2e 69 54 65 72 6d 0a 20 20  iMatch].iTerm.  
35314 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 61              && a
35315 4d 61 74 63 68 5b 6a 5d 2e 73 6e 53 74 61 74 75  Match[j].snStatu
35316 73 3d 3d 53 4e 49 50 50 45 54 5f 44 45 53 49 52  s==SNIPPET_DESIR
35317 45 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ED ){.          
35318 20 20 6e 44 65 73 69 72 65 64 2d 2d 3b 0a 20 20    nDesired--;.  
35319 20 20 20 20 20 20 20 20 20 20 61 4d 61 74 63 68            aMatch
3531a 5b 6a 5d 2e 73 6e 53 74 61 74 75 73 20 3d 20 53  [j].snStatus = S
3531b 4e 49 50 50 45 54 5f 49 47 4e 4f 52 45 3b 0a 20  NIPPET_IGNORE;. 
3531c 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
3531d 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
3531e 7b 0a 20 20 20 20 20 20 20 20 6e 61 70 70 65 6e  {.        nappen
3531f 64 28 26 73 62 2c 20 26 7a 44 6f 63 5b 69 53 74  d(&sb, &zDoc[iSt
35320 61 72 74 5d 2c 20 69 45 6e 64 20 2d 20 69 53 74  art], iEnd - iSt
35321 61 72 74 29 3b 0a 20 20 20 20 20 20 20 20 69 53  art);.        iS
35322 74 61 72 74 20 3d 20 69 45 6e 64 3b 0a 20 20 20  tart = iEnd;.   
35323 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 74     }.    }.    t
35324 61 69 6c 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a 20  ailCol = iCol;. 
35325 20 20 20 74 61 69 6c 4f 66 66 73 65 74 20 3d 20     tailOffset = 
35326 69 45 6e 64 3b 0a 20 20 7d 0a 20 20 74 72 69 6d  iEnd;.  }.  trim
35327 57 68 69 74 65 53 70 61 63 65 28 26 73 62 29 3b  WhiteSpace(&sb);
35328 0a 20 20 69 66 28 20 74 61 69 6c 45 6c 6c 69 70  .  if( tailEllip
35329 73 69 73 20 29 7b 0a 20 20 20 20 61 70 70 65 6e  sis ){.    appen
3532a 64 57 68 69 74 65 53 70 61 63 65 28 26 73 62 29  dWhiteSpace(&sb)
3532b 3b 0a 20 20 20 20 61 70 70 65 6e 64 28 26 73 62  ;.    append(&sb
3532c 2c 20 7a 45 6c 6c 69 70 73 69 73 29 3b 0a 20 20  , zEllipsis);.  
3532d 7d 0a 20 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69  }.  pCursor->sni
3532e 70 70 65 74 2e 7a 53 6e 69 70 70 65 74 20 3d 20  ppet.zSnippet = 
3532f 73 74 72 69 6e 67 42 75 66 66 65 72 44 61 74 61  stringBufferData
35330 28 26 73 62 29 3b 0a 20 20 70 43 75 72 73 6f 72  (&sb);.  pCursor
35331 2d 3e 73 6e 69 70 70 65 74 2e 6e 53 6e 69 70 70  ->snippet.nSnipp
35332 65 74 20 3d 20 73 74 72 69 6e 67 42 75 66 66 65  et = stringBuffe
35333 72 4c 65 6e 67 74 68 28 26 73 62 29 3b 0a 7d 0a  rLength(&sb);.}.
35334 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68  ../*.** Close th
35335 65 20 63 75 72 73 6f 72 2e 20 20 46 6f 72 20 61  e cursor.  For a
35336 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
35337 61 74 69 6f 6e 20 73 65 65 20 74 68 65 20 64 6f  ation see the do
35338 63 75 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f  cumentation.** o
35339 6e 20 74 68 65 20 78 43 6c 6f 73 65 20 6d 65 74  n the xClose met
3533a 68 6f 64 20 6f 66 20 74 68 65 20 76 69 72 74 75  hod of the virtu
3533b 61 6c 20 74 61 62 6c 65 20 69 6e 74 65 72 66 61  al table interfa
3533c 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ce..*/.static in
3533d 74 20 66 75 6c 6c 74 65 78 74 43 6c 6f 73 65 28  t fulltextClose(
3533e 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
3533f 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20  sor *pCursor){. 
35340 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72   fulltext_cursor
35341 20 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f   *c = (fulltext_
35342 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f  cursor *) pCurso
35343 72 3b 0a 20 20 46 54 53 54 52 41 43 45 28 28 22  r;.  FTSTRACE(("
35344 46 54 53 33 20 43 6c 6f 73 65 20 25 70 5c 6e 22  FTS3 Close %p\n"
35345 2c 20 63 29 29 3b 0a 20 20 73 71 6c 69 74 65 33  , c));.  sqlite3
35346 5f 66 69 6e 61 6c 69 7a 65 28 63 2d 3e 70 53 74  _finalize(c->pSt
35347 6d 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 46 74  mt);.  sqlite3Ft
35348 73 33 45 78 70 72 46 72 65 65 28 63 2d 3e 70 45  s3ExprFree(c->pE
35349 78 70 72 29 3b 0a 20 20 73 6e 69 70 70 65 74 43  xpr);.  snippetC
3534a 6c 65 61 72 28 26 63 2d 3e 73 6e 69 70 70 65 74  lear(&c->snippet
3534b 29 3b 0a 20 20 69 66 28 20 63 2d 3e 72 65 73 75  );.  if( c->resu
3534c 6c 74 2e 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20  lt.nData!=0 ){. 
3534d 20 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 63     dlrDestroy(&c
3534e 2d 3e 72 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20  ->reader);.  }. 
3534f 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72   dataBufferDestr
35350 6f 79 28 26 63 2d 3e 72 65 73 75 6c 74 29 3b 0a  oy(&c->result);.
35351 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63    sqlite3_free(c
35352 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
35353 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63  TE_OK;.}..static
35354 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 4e 65 78   int fulltextNex
35355 74 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  t(sqlite3_vtab_c
35356 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b  ursor *pCursor){
35357 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  .  fulltext_curs
35358 6f 72 20 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78  or *c = (fulltex
35359 74 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72  t_cursor *) pCur
3535a 73 6f 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  sor;.  int rc;..
3535b 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53    FTSTRACE(("FTS
3535c 33 20 4e 65 78 74 20 25 70 5c 6e 22 2c 20 70 43  3 Next %p\n", pC
3535d 75 72 73 6f 72 29 29 3b 0a 20 20 73 6e 69 70 70  ursor));.  snipp
3535e 65 74 43 6c 65 61 72 28 26 63 2d 3e 73 6e 69 70  etClear(&c->snip
3535f 70 65 74 29 3b 0a 20 20 69 66 28 20 63 2d 3e 69  pet);.  if( c->i
35360 43 75 72 73 6f 72 54 79 70 65 20 3c 20 51 55 45  CursorType < QUE
35361 52 59 5f 46 55 4c 4c 54 45 58 54 20 29 7b 0a 20  RY_FULLTEXT ){. 
35362 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73     /* TODO(shess
35363 29 20 48 61 6e 64 6c 65 20 53 51 4c 49 54 45 5f  ) Handle SQLITE_
35364 53 43 48 45 4d 41 20 41 4e 44 20 53 51 4c 49 54  SCHEMA AND SQLIT
35365 45 5f 42 55 53 59 2e 20 2a 2f 0a 20 20 20 20 72  E_BUSY. */.    r
35366 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
35367 28 63 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20  (c->pStmt);.    
35368 73 77 69 74 63 68 28 20 72 63 20 29 7b 0a 20 20  switch( rc ){.  
35369 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
3536a 52 4f 57 3a 0a 20 20 20 20 20 20 20 20 63 2d 3e  ROW:.        c->
3536b 65 6f 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  eof = 0;.       
3536c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
3536d 4b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 53 51  K;.      case SQ
3536e 4c 49 54 45 5f 44 4f 4e 45 3a 0a 20 20 20 20 20  LITE_DONE:.     
3536f 20 20 20 63 2d 3e 65 6f 66 20 3d 20 31 3b 0a 20     c->eof = 1;. 
35370 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
35371 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 64  LITE_OK;.      d
35372 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20  efault:.        
35373 63 2d 3e 65 6f 66 20 3d 20 31 3b 0a 20 20 20 20  c->eof = 1;.    
35374 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
35375 20 20 20 7d 0a 20 20 7d 20 65 6c 73 65 20 7b 20     }.  } else { 
35376 20 2f 2a 20 66 75 6c 6c 2d 74 65 78 74 20 71 75   /* full-text qu
35377 65 72 79 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  ery */.    rc = 
35378 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 63 2d  sqlite3_reset(c-
35379 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 69 66 28  >pStmt);.    if(
3537a 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
3537b 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20   return rc;..   
3537c 20 69 66 28 20 63 2d 3e 72 65 73 75 6c 74 2e 6e   if( c->result.n
3537d 44 61 74 61 3d 3d 30 20 7c 7c 20 64 6c 72 41 74  Data==0 || dlrAt
3537e 45 6e 64 28 26 63 2d 3e 72 65 61 64 65 72 29 20  End(&c->reader) 
3537f 29 7b 0a 20 20 20 20 20 20 63 2d 3e 65 6f 66 20  ){.      c->eof 
35380 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72  = 1;.      retur
35381 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
35382 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69   }.    rc = sqli
35383 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 63  te3_bind_int64(c
35384 2d 3e 70 53 74 6d 74 2c 20 31 2c 20 64 6c 72 44  ->pStmt, 1, dlrD
35385 6f 63 69 64 28 26 63 2d 3e 72 65 61 64 65 72 29  ocid(&c->reader)
35386 29 3b 0a 20 20 20 20 64 6c 72 53 74 65 70 28 26  );.    dlrStep(&
35387 63 2d 3e 72 65 61 64 65 72 29 3b 0a 20 20 20 20  c->reader);.    
35388 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
35389 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
3538a 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73     /* TODO(shess
3538b 29 20 48 61 6e 64 6c 65 20 53 51 4c 49 54 45 5f  ) Handle SQLITE_
3538c 53 43 48 45 4d 41 20 41 4e 44 20 53 51 4c 49 54  SCHEMA AND SQLIT
3538d 45 5f 42 55 53 59 2e 20 2a 2f 0a 20 20 20 20 72  E_BUSY. */.    r
3538e 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
3538f 28 63 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20  (c->pStmt);.    
35390 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52  if( rc==SQLITE_R
35391 4f 57 20 29 7b 20 20 20 2f 2a 20 74 68 65 20 63  OW ){   /* the c
35392 61 73 65 20 77 65 20 65 78 70 65 63 74 20 2a 2f  ase we expect */
35393 0a 20 20 20 20 20 20 63 2d 3e 65 6f 66 20 3d 20  .      c->eof = 
35394 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  0;.      return 
35395 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
35396 0a 20 20 20 20 2f 2a 20 61 6e 20 65 72 72 6f 72  .    /* an error
35397 20 6f 63 63 75 72 72 65 64 3b 20 61 62 6f 72 74   occurred; abort
35398 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 72   */.    return r
35399 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3f  c==SQLITE_DONE ?
3539a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20   SQLITE_ERROR : 
3539b 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 20 54  rc;.  }.}.../* T
3539c 4f 44 4f 28 73 68 65 73 73 29 20 49 66 20 77 65  ODO(shess) If we
3539d 20 70 75 73 68 65 64 20 4c 65 61 66 52 65 61 64   pushed LeafRead
3539e 65 72 20 74 6f 20 74 68 65 20 74 6f 70 20 6f 66  er to the top of
3539f 20 74 68 65 20 66 69 6c 65 2c 20 6f 72 20 74 6f   the file, or to
353a0 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 66 69 6c 65  .** another file
353a1 2c 20 74 65 72 6d 5f 73 65 6c 65 63 74 28 29 20  , term_select() 
353a2 63 6f 75 6c 64 20 62 65 20 70 75 73 68 65 64 20  could be pushed 
353a3 61 62 6f 76 65 0a 2a 2a 20 64 6f 63 4c 69 73 74  above.** docList
353a4 4f 66 54 65 72 6d 28 29 2e 0a 2a 2f 0a 73 74 61  OfTerm()..*/.sta
353a5 74 69 63 20 69 6e 74 20 74 65 72 6d 53 65 6c 65  tic int termSele
353a6 63 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  ct(fulltext_vtab
353a7 20 2a 76 2c 20 69 6e 74 20 69 43 6f 6c 75 6d 6e   *v, int iColumn
353a8 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
353a9 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
353aa 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e  ar *pTerm, int n
353ab 54 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66  Term, int isPref
353ac 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ix,.            
353ad 20 20 20 20 20 20 20 20 20 20 44 6f 63 4c 69 73            DocLis
353ae 74 54 79 70 65 20 69 54 79 70 65 2c 20 44 61 74  tType iType, Dat
353af 61 42 75 66 66 65 72 20 2a 6f 75 74 29 3b 0a 0a  aBuffer *out);..
353b0 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  /* .** Return a 
353b1 44 6f 63 4c 69 73 74 20 63 6f 72 72 65 73 70 6f  DocList correspo
353b2 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 70 68 72  nding to the phr
353b3 61 73 65 20 2a 70 50 68 72 61 73 65 2e 0a 2a 2a  ase *pPhrase..**
353b4 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 69 6e  .** The resultin
353b5 67 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c  g DL_DOCIDS docl
353b6 69 73 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ist is stored in
353b7 20 70 52 65 73 75 6c 74 2c 20 77 68 69 63 68 20   pResult, which 
353b8 69 73 0a 2a 2a 20 6f 76 65 72 77 72 69 74 74 65  is.** overwritte
353b9 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  n..*/.static int
353ba 20 64 6f 63 4c 69 73 74 4f 66 50 68 72 61 73 65   docListOfPhrase
353bb 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61  (.  fulltext_vta
353bc 62 20 2a 70 54 61 62 2c 20 20 20 2f 2a 20 54 68  b *pTab,   /* Th
353bd 65 20 66 75 6c 6c 20 74 65 78 74 20 69 6e 64 65  e full text inde
353be 78 20 2a 2f 0a 20 20 46 74 73 33 50 68 72 61 73  x */.  Fts3Phras
353bf 65 20 2a 70 50 68 72 61 73 65 2c 20 20 20 2f 2a  e *pPhrase,   /*
353c0 20 50 68 72 61 73 65 20 74 6f 20 72 65 74 75 72   Phrase to retur
353c1 6e 20 61 20 64 6f 63 6c 69 73 74 20 63 6f 72 72  n a doclist corr
353c2 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 2a 2f 0a  esponding to */.
353c3 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20 65 4c    DocListType eL
353c4 69 73 74 54 79 70 65 2c 20 2f 2a 20 45 69 74 68  istType, /* Eith
353c5 65 72 20 44 4c 5f 44 4f 43 49 44 53 20 6f 72 20  er DL_DOCIDS or 
353c6 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 2a 2f 0a  DL_POSITIONS */.
353c7 20 20 44 61 74 61 42 75 66 66 65 72 20 2a 70 52    DataBuffer *pR
353c8 65 73 75 6c 74 20 20 20 20 2f 2a 20 57 72 69 74  esult    /* Writ
353c9 65 20 74 68 65 20 72 65 73 75 6c 74 20 68 65 72  e the result her
353ca 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 69  e */.){.  int ii
353cb 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
353cc 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 43  ITE_OK;.  int iC
353cd 6f 6c 20 3d 20 70 50 68 72 61 73 65 2d 3e 69 43  ol = pPhrase->iC
353ce 6f 6c 75 6d 6e 3b 0a 20 20 44 6f 63 4c 69 73 74  olumn;.  DocList
353cf 54 79 70 65 20 65 54 79 70 65 20 3d 20 65 4c 69  Type eType = eLi
353d0 73 74 54 79 70 65 3b 0a 20 20 61 73 73 65 72 74  stType;.  assert
353d1 28 20 65 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49  ( eType==DL_POSI
353d2 54 49 4f 4e 53 20 7c 7c 20 65 54 79 70 65 3d 3d  TIONS || eType==
353d3 44 4c 5f 44 4f 43 49 44 53 20 29 3b 0a 20 20 69  DL_DOCIDS );.  i
353d4 66 28 20 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b  f( pPhrase->nTok
353d5 65 6e 3e 31 20 29 7b 0a 20 20 20 20 65 54 79 70  en>1 ){.    eTyp
353d6 65 20 3d 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53  e = DL_POSITIONS
353d7 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73  ;.  }..  /* This
353d8 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e 65 76   code should nev
353d9 65 72 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74  er be called wit
353da 68 20 62 75 66 66 65 72 65 64 20 75 70 64 61 74  h buffered updat
353db 65 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  es. */.  assert(
353dc 20 70 54 61 62 2d 3e 6e 50 65 6e 64 69 6e 67 44   pTab->nPendingD
353dd 61 74 61 3c 30 20 29 3b 0a 0a 20 20 66 6f 72 28  ata<0 );..  for(
353de 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45  ii=0; rc==SQLITE
353df 5f 4f 4b 20 26 26 20 69 69 3c 70 50 68 72 61 73  _OK && ii<pPhras
353e0 65 2d 3e 6e 54 6f 6b 65 6e 3b 20 69 69 2b 2b 29  e->nToken; ii++)
353e1 7b 0a 20 20 20 20 44 61 74 61 42 75 66 66 65 72  {.    DataBuffer
353e2 20 74 6d 70 3b 0a 20 20 20 20 73 74 72 75 63 74   tmp;.    struct
353e3 20 50 68 72 61 73 65 54 6f 6b 65 6e 20 2a 70 20   PhraseToken *p 
353e4 3d 20 26 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b  = &pPhrase->aTok
353e5 65 6e 5b 69 69 5d 3b 0a 20 20 20 20 72 63 20 3d  en[ii];.    rc =
353e6 20 74 65 72 6d 53 65 6c 65 63 74 28 70 54 61 62   termSelect(pTab
353e7 2c 20 69 43 6f 6c 2c 20 70 2d 3e 7a 2c 20 70 2d  , iCol, p->z, p-
353e8 3e 6e 2c 20 70 2d 3e 69 73 50 72 65 66 69 78 2c  >n, p->isPrefix,
353e9 20 65 54 79 70 65 2c 20 26 74 6d 70 29 3b 0a 20   eType, &tmp);. 
353ea 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
353eb 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66  E_OK ){.      if
353ec 28 20 69 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ( ii==0 ){.     
353ed 20 20 20 2a 70 52 65 73 75 6c 74 20 3d 20 74 6d     *pResult = tm
353ee 70 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  p;.      }else{.
353ef 20 20 20 20 20 20 20 20 44 61 74 61 42 75 66 66          DataBuff
353f0 65 72 20 72 65 73 20 3d 20 2a 70 52 65 73 75 6c  er res = *pResul
353f1 74 3b 0a 20 20 20 20 20 20 20 20 64 61 74 61 42  t;.        dataB
353f2 75 66 66 65 72 49 6e 69 74 28 70 52 65 73 75 6c  ufferInit(pResul
353f3 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69  t, 0);.        i
353f4 66 28 20 69 69 3d 3d 28 70 50 68 72 61 73 65 2d  f( ii==(pPhrase-
353f5 3e 6e 54 6f 6b 65 6e 2d 31 29 20 29 7b 0a 20 20  >nToken-1) ){.  
353f6 20 20 20 20 20 20 20 20 65 54 79 70 65 20 3d 20          eType = 
353f7 65 4c 69 73 74 54 79 70 65 3b 0a 20 20 20 20 20  eListType;.     
353f8 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 6f 63     }.        doc
353f9 4c 69 73 74 50 68 72 61 73 65 4d 65 72 67 65 28  ListPhraseMerge(
353fa 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 2e 70  .          res.p
353fb 44 61 74 61 2c 20 72 65 73 2e 6e 44 61 74 61 2c  Data, res.nData,
353fc 20 74 6d 70 2e 70 44 61 74 61 2c 20 74 6d 70 2e   tmp.pData, tmp.
353fd 6e 44 61 74 61 2c 20 30 2c 20 30 2c 20 65 54 79  nData, 0, 0, eTy
353fe 70 65 2c 20 70 52 65 73 75 6c 74 0a 20 20 20 20  pe, pResult.    
353ff 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 64      );.        d
35400 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
35401 28 26 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20  (&res);.        
35402 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
35403 79 28 26 74 6d 70 29 3b 0a 20 20 20 20 20 20 7d  y(&tmp);.      }
35404 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65  .    }.  }..  re
35405 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
35406 2a 20 45 76 61 6c 75 61 74 65 20 74 68 65 20 66  * Evaluate the f
35407 75 6c 6c 2d 74 65 78 74 20 65 78 70 72 65 73 73  ull-text express
35408 69 6f 6e 20 70 45 78 70 72 20 61 67 61 69 6e 73  ion pExpr agains
35409 74 20 66 74 73 33 20 74 61 62 6c 65 20 70 54 61  t fts3 table pTa
3540a 62 2e 20 57 72 69 74 65 0a 2a 2a 20 74 68 65 20  b. Write.** the 
3540b 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 70 52 65  results into pRe
3540c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
3540d 20 65 76 61 6c 46 74 73 33 45 78 70 72 28 0a 20   evalFts3Expr(. 
3540e 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a   fulltext_vtab *
3540f 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 20  pTab,           
35410 2f 2a 20 46 74 73 33 20 56 69 72 74 75 61 6c 20  /* Fts3 Virtual 
35411 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 2a 2f 0a  table object */.
35412 20 20 46 74 73 33 45 78 70 72 20 2a 70 45 78 70    Fts3Expr *pExp
35413 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
35414 20 2f 2a 20 50 61 72 73 65 64 20 66 74 73 33 20   /* Parsed fts3 
35415 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
35416 44 61 74 61 42 75 66 66 65 72 20 2a 70 52 65 73  DataBuffer *pRes
35417 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35418 2a 20 4f 55 54 3a 20 57 72 69 74 65 20 72 65 73  * OUT: Write res
35419 75 6c 74 73 20 6f 66 20 74 68 65 20 65 78 70 72  ults of the expr
3541a 65 73 73 69 6f 6e 20 68 65 72 65 20 2a 2f 0a 29  ession here */.)
3541b 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
3541c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 49 6e  ITE_OK;..  /* In
3541d 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 75 74  itialize the out
3541e 70 75 74 20 62 75 66 66 65 72 2e 20 49 66 20 74  put buffer. If t
3541f 68 69 73 20 69 73 20 61 6e 20 65 6d 70 74 79 20  his is an empty 
35420 71 75 65 72 79 20 28 70 45 78 70 72 3d 3d 30 29  query (pExpr==0)
35421 2c 20 0a 20 20 2a 2a 20 74 68 69 73 20 69 73 20  , .  ** this is 
35422 61 6c 6c 20 74 68 61 74 20 6e 65 65 64 73 20 74  all that needs t
35423 6f 20 62 65 20 64 6f 6e 65 2e 20 45 6d 70 74 79  o be done. Empty
35424 20 71 75 65 72 69 65 73 20 70 72 6f 64 75 63 65   queries produce
35425 20 65 6d 70 74 79 20 0a 20 20 2a 2a 20 72 65 73   empty .  ** res
35426 75 6c 74 20 73 65 74 73 2e 0a 20 20 2a 2f 0a 20  ult sets..  */. 
35427 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28   dataBufferInit(
35428 70 52 65 73 2c 20 30 29 3b 0a 0a 20 20 69 66 28  pRes, 0);..  if(
35429 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20 69 66   pExpr ){.    if
3542a 28 20 70 45 78 70 72 2d 3e 65 54 79 70 65 3d 3d  ( pExpr->eType==
3542b 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20  FTSQUERY_PHRASE 
3542c 29 7b 0a 20 20 20 20 20 20 44 6f 63 4c 69 73 74  ){.      DocList
3542d 54 79 70 65 20 65 54 79 70 65 20 3d 20 44 4c 5f  Type eType = DL_
3542e 44 4f 43 49 44 53 3b 0a 20 20 20 20 20 20 69 66  DOCIDS;.      if
3542f 28 20 70 45 78 70 72 2d 3e 70 50 61 72 65 6e 74  ( pExpr->pParent
35430 20 26 26 20 70 45 78 70 72 2d 3e 70 50 61 72 65   && pExpr->pPare
35431 6e 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55  nt->eType==FTSQU
35432 45 52 59 5f 4e 45 41 52 20 29 7b 0a 20 20 20 20  ERY_NEAR ){.    
35433 20 20 20 20 65 54 79 70 65 20 3d 20 44 4c 5f 50      eType = DL_P
35434 4f 53 49 54 49 4f 4e 53 3b 0a 20 20 20 20 20 20  OSITIONS;.      
35435 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 64 6f 63  }.      rc = doc
35436 4c 69 73 74 4f 66 50 68 72 61 73 65 28 70 54 61  ListOfPhrase(pTa
35437 62 2c 20 70 45 78 70 72 2d 3e 70 50 68 72 61 73  b, pExpr->pPhras
35438 65 2c 20 65 54 79 70 65 2c 20 70 52 65 73 29 3b  e, eType, pRes);
35439 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
3543a 20 20 44 61 74 61 42 75 66 66 65 72 20 6c 68 73    DataBuffer lhs
3543b 3b 0a 20 20 20 20 20 20 44 61 74 61 42 75 66 66  ;.      DataBuff
3543c 65 72 20 72 68 73 3b 0a 0a 20 20 20 20 20 20 64  er rhs;..      d
3543d 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 72  ataBufferInit(&r
3543e 68 73 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66  hs, 0);.      if
3543f 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63  ( SQLITE_OK==(rc
35440 20 3d 20 65 76 61 6c 46 74 73 33 45 78 70 72 28   = evalFts3Expr(
35441 70 54 61 62 2c 20 70 45 78 70 72 2d 3e 70 4c 65  pTab, pExpr->pLe
35442 66 74 2c 20 26 6c 68 73 29 29 20 0a 20 20 20 20  ft, &lhs)) .    
35443 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d     && SQLITE_OK=
35444 3d 28 72 63 20 3d 20 65 76 61 6c 46 74 73 33 45  =(rc = evalFts3E
35445 78 70 72 28 70 54 61 62 2c 20 70 45 78 70 72 2d  xpr(pTab, pExpr-
35446 3e 70 52 69 67 68 74 2c 20 26 72 68 73 29 29 20  >pRight, &rhs)) 
35447 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20  .      ){.      
35448 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d    switch( pExpr-
35449 3e 65 54 79 70 65 20 29 7b 0a 20 20 20 20 20 20  >eType ){.      
3544a 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52      case FTSQUER
3544b 59 5f 4e 45 41 52 3a 20 7b 0a 20 20 20 20 20 20  Y_NEAR: {.      
3544c 20 20 20 20 20 20 69 6e 74 20 6e 54 6f 6b 65 6e        int nToken
3544d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 46 74  ;.            Ft
3544e 73 33 45 78 70 72 20 2a 70 4c 65 66 74 3b 0a 20  s3Expr *pLeft;. 
3544f 20 20 20 20 20 20 20 20 20 20 20 44 6f 63 4c 69             DocLi
35450 73 74 54 79 70 65 20 65 54 79 70 65 20 3d 20 44  stType eType = D
35451 4c 5f 44 4f 43 49 44 53 3b 0a 20 20 20 20 20 20  L_DOCIDS;.      
35452 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
35453 3e 70 50 61 72 65 6e 74 20 26 26 20 70 45 78 70  >pParent && pExp
35454 72 2d 3e 70 50 61 72 65 6e 74 2d 3e 65 54 79 70  r->pParent->eTyp
35455 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52  e==FTSQUERY_NEAR
35456 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
35457 20 20 65 54 79 70 65 20 3d 20 44 4c 5f 50 4f 53    eType = DL_POS
35458 49 54 49 4f 4e 53 3b 0a 20 20 20 20 20 20 20 20  ITIONS;.        
35459 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
3545a 20 20 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d    pLeft = pExpr-
3545b 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 20 20  >pLeft;.        
3545c 20 20 20 20 77 68 69 6c 65 28 20 70 4c 65 66 74      while( pLeft
3545d 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52  ->eType==FTSQUER
3545e 59 5f 4e 45 41 52 20 29 7b 20 0a 20 20 20 20 20  Y_NEAR ){ .     
3545f 20 20 20 20 20 20 20 20 20 70 4c 65 66 74 3d 70           pLeft=p
35460 4c 65 66 74 2d 3e 70 52 69 67 68 74 3b 0a 20 20  Left->pRight;.  
35461 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
35462 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
35463 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 65  pExpr->pRight->e
35464 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50  Type==FTSQUERY_P
35465 48 52 41 53 45 20 29 3b 0a 20 20 20 20 20 20 20  HRASE );.       
35466 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 65       assert( pLe
35467 66 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55  ft->eType==FTSQU
35468 45 52 59 5f 50 48 52 41 53 45 20 29 3b 0a 20 20  ERY_PHRASE );.  
35469 20 20 20 20 20 20 20 20 20 20 6e 54 6f 6b 65 6e            nToken
3546a 20 3d 20 70 4c 65 66 74 2d 3e 70 50 68 72 61 73   = pLeft->pPhras
3546b 65 2d 3e 6e 54 6f 6b 65 6e 20 2b 20 70 45 78 70  e->nToken + pExp
3546c 72 2d 3e 70 52 69 67 68 74 2d 3e 70 50 68 72 61  r->pRight->pPhra
3546d 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20  se->nToken;.    
3546e 20 20 20 20 20 20 20 20 64 6f 63 4c 69 73 74 50          docListP
3546f 68 72 61 73 65 4d 65 72 67 65 28 6c 68 73 2e 70  hraseMerge(lhs.p
35470 44 61 74 61 2c 20 6c 68 73 2e 6e 44 61 74 61 2c  Data, lhs.nData,
35471 20 72 68 73 2e 70 44 61 74 61 2c 20 72 68 73 2e   rhs.pData, rhs.
35472 6e 44 61 74 61 2c 20 0a 20 20 20 20 20 20 20 20  nData, .        
35473 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6e          pExpr->n
35474 4e 65 61 72 2b 31 2c 20 6e 54 6f 6b 65 6e 2c 20  Near+1, nToken, 
35475 65 54 79 70 65 2c 20 70 52 65 73 0a 20 20 20 20  eType, pRes.    
35476 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
35477 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
35478 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
35479 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52      case FTSQUER
3547a 59 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 20  Y_NOT: {.       
3547b 20 20 20 20 20 64 6f 63 4c 69 73 74 45 78 63 65       docListExce
3547c 70 74 4d 65 72 67 65 28 6c 68 73 2e 70 44 61 74  ptMerge(lhs.pDat
3547d 61 2c 20 6c 68 73 2e 6e 44 61 74 61 2c 20 72 68  a, lhs.nData, rh
3547e 73 2e 70 44 61 74 61 2c 20 72 68 73 2e 6e 44 61  s.pData, rhs.nDa
3547f 74 61 2c 70 52 65 73 29 3b 0a 20 20 20 20 20 20  ta,pRes);.      
35480 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
35481 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
35482 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52 59     case FTSQUERY
35483 5f 41 4e 44 3a 20 7b 0a 20 20 20 20 20 20 20 20  _AND: {.        
35484 20 20 20 20 64 6f 63 4c 69 73 74 41 6e 64 4d 65      docListAndMe
35485 72 67 65 28 6c 68 73 2e 70 44 61 74 61 2c 20 6c  rge(lhs.pData, l
35486 68 73 2e 6e 44 61 74 61 2c 20 72 68 73 2e 70 44  hs.nData, rhs.pD
35487 61 74 61 2c 20 72 68 73 2e 6e 44 61 74 61 2c 20  ata, rhs.nData, 
35488 70 52 65 73 29 3b 0a 20 20 20 20 20 20 20 20 20  pRes);.         
35489 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3548a 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
3548b 63 61 73 65 20 46 54 53 51 55 45 52 59 5f 4f 52  case FTSQUERY_OR
3548c 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  : {.            
3548d 64 6f 63 4c 69 73 74 4f 72 4d 65 72 67 65 28 6c  docListOrMerge(l
3548e 68 73 2e 70 44 61 74 61 2c 20 6c 68 73 2e 6e 44  hs.pData, lhs.nD
3548f 61 74 61 2c 20 72 68 73 2e 70 44 61 74 61 2c 20  ata, rhs.pData, 
35490 72 68 73 2e 6e 44 61 74 61 2c 20 70 52 65 73 29  rhs.nData, pRes)
35491 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72  ;.            br
35492 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  eak;.          }
35493 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
35494 20 7d 0a 20 20 20 20 20 20 64 61 74 61 42 75 66   }.      dataBuf
35495 66 65 72 44 65 73 74 72 6f 79 28 26 6c 68 73 29  ferDestroy(&lhs)
35496 3b 0a 20 20 20 20 20 20 64 61 74 61 42 75 66 66  ;.      dataBuff
35497 65 72 44 65 73 74 72 6f 79 28 26 72 68 73 29 3b  erDestroy(&rhs);
35498 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65  .    }.  }..  re
35499 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54  turn rc;.}../* T
3549a 4f 44 4f 28 73 68 65 73 73 29 20 52 65 66 61 63  ODO(shess) Refac
3549b 74 6f 72 20 74 68 65 20 63 6f 64 65 20 74 6f 20  tor the code to 
3549c 72 65 6d 6f 76 65 20 74 68 69 73 20 66 6f 72 77  remove this forw
3549d 61 72 64 20 64 65 63 6c 2e 20 2a 2f 0a 73 74 61  ard decl. */.sta
3549e 74 69 63 20 69 6e 74 20 66 6c 75 73 68 50 65 6e  tic int flushPen
3549f 64 69 6e 67 54 65 72 6d 73 28 66 75 6c 6c 74 65  dingTerms(fullte
354a0 78 74 5f 76 74 61 62 20 2a 76 29 3b 0a 0a 2f 2a  xt_vtab *v);../*
354a1 20 50 65 72 66 6f 72 6d 20 61 20 66 75 6c 6c 2d   Perform a full-
354a2 74 65 78 74 20 71 75 65 72 79 20 75 73 69 6e 67  text query using
354a3 20 74 68 65 20 73 65 61 72 63 68 20 65 78 70 72   the search expr
354a4 65 73 73 69 6f 6e 20 69 6e 0a 2a 2a 20 7a 49 6e  ession in.** zIn
354a5 70 75 74 5b 30 2e 2e 6e 49 6e 70 75 74 2d 31 5d  put[0..nInput-1]
354a6 2e 20 20 52 65 74 75 72 6e 20 61 20 6c 69 73 74  .  Return a list
354a7 20 6f 66 20 6d 61 74 63 68 69 6e 67 20 64 6f 63   of matching doc
354a8 75 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20 70 52 65  uments.** in pRe
354a9 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 51 75 65 72  sult..**.** Quer
354aa 69 65 73 20 6d 75 73 74 20 6d 61 74 63 68 20 63  ies must match c
354ab 6f 6c 75 6d 6e 20 69 43 6f 6c 75 6d 6e 2e 20 20  olumn iColumn.  
354ac 4f 72 20 69 66 20 69 43 6f 6c 75 6d 6e 3e 3d 6e  Or if iColumn>=n
354ad 43 6f 6c 75 6d 6e 0a 2a 2a 20 74 68 65 79 20 61  Column.** they a
354ae 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 6d 61  re allowed to ma
354af 74 63 68 20 61 67 61 69 6e 73 74 20 61 6e 79 20  tch against any 
354b0 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69  column..*/.stati
354b1 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 51 75  c int fulltextQu
354b2 65 72 79 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f  ery(.  fulltext_
354b3 76 74 61 62 20 2a 76 2c 20 20 20 20 20 20 2f 2a  vtab *v,      /*
354b4 20 54 68 65 20 66 75 6c 6c 20 74 65 78 74 20 69   The full text i
354b5 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 43  ndex */.  int iC
354b6 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20  olumn,          
354b7 20 2f 2a 20 4d 61 74 63 68 20 61 67 61 69 6e 73   /* Match agains
354b8 74 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 62 79  t this column by
354b9 20 64 65 66 61 75 6c 74 20 2a 2f 0a 20 20 63 6f   default */.  co
354ba 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74  nst char *zInput
354bb 2c 20 20 20 20 2f 2a 20 54 68 65 20 71 75 65 72  ,    /* The quer
354bc 79 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 69 6e  y string */.  in
354bd 74 20 6e 49 6e 70 75 74 2c 20 20 20 20 20 20 20  t nInput,       
354be 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
354bf 66 20 62 79 74 65 73 20 69 6e 20 7a 49 6e 70 75  f bytes in zInpu
354c0 74 5b 5d 20 2a 2f 0a 20 20 44 61 74 61 42 75 66  t[] */.  DataBuf
354c1 66 65 72 20 2a 70 52 65 73 75 6c 74 2c 20 20 20  fer *pResult,   
354c2 2f 2a 20 57 72 69 74 65 20 74 68 65 20 72 65 73  /* Write the res
354c3 75 6c 74 20 64 6f 63 6c 69 73 74 20 68 65 72 65  ult doclist here
354c4 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a   */.  Fts3Expr *
354c5 2a 70 70 45 78 70 72 20 20 20 20 20 20 20 20 2f  *ppExpr        /
354c6 2a 20 50 75 74 20 70 61 72 73 65 64 20 71 75 65  * Put parsed que
354c7 72 79 20 73 74 72 69 6e 67 20 68 65 72 65 20 2a  ry string here *
354c8 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  /.){.  int rc;..
354c9 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29    /* TODO(shess)
354ca 20 49 6e 73 74 65 61 64 20 6f 66 20 66 6c 75 73   Instead of flus
354cb 68 69 6e 67 20 70 65 6e 64 69 6e 67 54 65 72 6d  hing pendingTerm
354cc 73 2c 20 77 65 20 63 6f 75 6c 64 20 71 75 65 72  s, we could quer
354cd 79 20 66 6f 72 0a 20 20 2a 2a 20 74 68 65 20 72  y for.  ** the r
354ce 65 6c 65 76 61 6e 74 20 74 65 72 6d 20 61 6e 64  elevant term and
354cf 20 6d 65 72 67 65 20 74 68 65 20 64 6f 63 6c 69   merge the docli
354d0 73 74 20 69 6e 74 6f 20 77 68 61 74 20 77 65 20  st into what we 
354d1 72 65 63 65 69 76 65 20 66 72 6f 6d 0a 20 20 2a  receive from.  *
354d2 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  * the database. 
354d3 20 57 61 69 74 20 61 6e 64 20 73 65 65 20 69 66   Wait and see if
354d4 20 74 68 69 73 20 69 73 20 61 20 63 6f 6d 6d 6f   this is a commo
354d5 6e 20 69 73 73 75 65 2c 20 66 69 72 73 74 2e 0a  n issue, first..
354d6 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 67 6f 6f 64    **.  ** A good
354d7 20 72 65 61 73 6f 6e 20 6e 6f 74 20 74 6f 20 66   reason not to f
354d8 6c 75 73 68 20 69 73 20 74 6f 20 6e 6f 74 20 67  lush is to not g
354d9 65 6e 65 72 61 74 65 20 75 70 64 61 74 65 2d 72  enerate update-r
354da 65 6c 61 74 65 64 0a 20 20 2a 2a 20 65 72 72 6f  elated.  ** erro
354db 72 20 63 6f 64 65 73 20 66 72 6f 6d 20 68 65 72  r codes from her
354dc 65 2e 0a 20 20 2a 2f 0a 0a 20 20 2f 2a 20 46 6c  e..  */..  /* Fl
354dd 75 73 68 20 61 6e 79 20 62 75 66 66 65 72 65 64  ush any buffered
354de 20 75 70 64 61 74 65 73 20 62 65 66 6f 72 65 20   updates before 
354df 65 78 65 63 75 74 69 6e 67 20 74 68 65 20 71 75  executing the qu
354e0 65 72 79 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 66  ery. */.  rc = f
354e1 6c 75 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73  lushPendingTerms
354e2 28 76 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  (v);.  if( rc!=S
354e3 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
354e4 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
354e5 20 20 2f 2a 20 50 61 72 73 65 20 74 68 65 20 71    /* Parse the q
354e6 75 65 72 79 20 70 61 73 73 65 64 20 74 6f 20 74  uery passed to t
354e7 68 65 20 4d 41 54 43 48 20 6f 70 65 72 61 74 6f  he MATCH operato
354e8 72 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  r. */.  rc = sql
354e9 69 74 65 33 46 74 73 33 45 78 70 72 50 61 72 73  ite3Fts3ExprPars
354ea 65 28 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 2c  e(v->pTokenizer,
354eb 20 0a 20 20 20 20 20 20 76 2d 3e 61 7a 43 6f 6c   .      v->azCol
354ec 75 6d 6e 2c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c  umn, v->nColumn,
354ed 20 69 43 6f 6c 75 6d 6e 2c 20 7a 49 6e 70 75 74   iColumn, zInput
354ee 2c 20 6e 49 6e 70 75 74 2c 20 70 70 45 78 70 72  , nInput, ppExpr
354ef 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  .  );.  if( rc!=
354f0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
354f1 20 61 73 73 65 72 74 28 20 30 3d 3d 28 2a 70 70   assert( 0==(*pp
354f2 45 78 70 72 29 20 29 3b 0a 20 20 20 20 72 65 74  Expr) );.    ret
354f3 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 72  urn rc;.  }..  r
354f4 65 74 75 72 6e 20 65 76 61 6c 46 74 73 33 45 78  eturn evalFts3Ex
354f5 70 72 28 76 2c 20 2a 70 70 45 78 70 72 2c 20 70  pr(v, *ppExpr, p
354f6 52 65 73 75 6c 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  Result);.}../*.*
354f7 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 78 46  * This is the xF
354f8 69 6c 74 65 72 20 69 6e 74 65 72 66 61 63 65 20  ilter interface 
354f9 66 6f 72 20 74 68 65 20 76 69 72 74 75 61 6c 20  for the virtual 
354fa 74 61 62 6c 65 2e 20 20 53 65 65 0a 2a 2a 20 74  table.  See.** t
354fb 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
354fc 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 20   xFilter method 
354fd 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f  documentation fo
354fe 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20  r additional.** 
354ff 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a  information..**.
35500 2a 2a 20 49 66 20 69 64 78 4e 75 6d 3d 3d 51 55  ** If idxNum==QU
35501 45 52 59 5f 47 45 4e 45 52 49 43 20 74 68 65 6e  ERY_GENERIC then
35502 20 64 6f 20 61 20 66 75 6c 6c 20 74 61 62 6c 65   do a full table
35503 20 73 63 61 6e 20 61 67 61 69 6e 73 74 0a 2a 2a   scan against.**
35504 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74   the %_content t
35505 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69  able..**.** If i
35506 64 78 4e 75 6d 3d 3d 51 55 45 52 59 5f 44 4f 43  dxNum==QUERY_DOC
35507 49 44 20 74 68 65 6e 20 64 6f 20 61 20 64 6f 63  ID then do a doc
35508 69 64 20 6c 6f 6f 6b 75 70 20 66 6f 72 20 61 20  id lookup for a 
35509 73 69 6e 67 6c 65 20 65 6e 74 72 79 0a 2a 2a 20  single entry.** 
3550a 69 6e 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74  in the %_content
3550b 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66   table..**.** If
3550c 20 69 64 78 4e 75 6d 3e 3d 51 55 45 52 59 5f 46   idxNum>=QUERY_F
3550d 55 4c 4c 54 45 58 54 20 74 68 65 6e 20 75 73 65  ULLTEXT then use
3550e 20 74 68 65 20 66 75 6c 6c 20 74 65 78 74 20 69   the full text i
3550f 6e 64 65 78 2e 20 20 54 68 65 0a 2a 2a 20 63 6f  ndex.  The.** co
35510 6c 75 6d 6e 20 6f 6e 20 74 68 65 20 6c 65 66 74  lumn on the left
35511 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68  -hand side of th
35512 65 20 4d 41 54 43 48 20 6f 70 65 72 61 74 6f 72  e MATCH operator
35513 20 69 73 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6e 75   is column.** nu
35514 6d 62 65 72 20 69 64 78 4e 75 6d 2d 51 55 45 52  mber idxNum-QUER
35515 59 5f 46 55 4c 4c 54 45 58 54 2c 20 30 20 69 6e  Y_FULLTEXT, 0 in
35516 64 65 78 65 64 2e 20 20 61 72 67 76 5b 30 5d 20  dexed.  argv[0] 
35517 69 73 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e  is the right-han
35518 64 0a 2a 2a 20 73 69 64 65 20 6f 66 20 74 68 65  d.** side of the
35519 20 4d 41 54 43 48 20 6f 70 65 72 61 74 6f 72 2e   MATCH operator.
3551a 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  .*/./* TODO(shes
3551b 73 29 20 55 70 67 72 61 64 65 20 74 68 65 20 63  s) Upgrade the c
3551c 75 72 73 6f 72 20 69 6e 69 74 69 61 6c 69 7a 61  ursor initializa
3551d 74 69 6f 6e 20 61 6e 64 20 64 65 73 74 72 75 63  tion and destruc
3551e 74 69 6f 6e 20 74 6f 0a 2a 2a 20 61 63 63 6f 75  tion to.** accou
3551f 6e 74 20 66 6f 72 20 66 75 6c 6c 74 65 78 74 46  nt for fulltextF
35520 69 6c 74 65 72 28 29 20 62 65 69 6e 67 20 63 61  ilter() being ca
35521 6c 6c 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69  lled multiple ti
35522 6d 65 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 61  mes on the.** sa
35523 6d 65 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20  me cursor.  The 
35524 63 75 72 72 65 6e 74 20 73 6f 6c 75 74 69 6f 6e  current solution
35525 20 69 73 20 76 65 72 79 20 66 72 61 67 69 6c 65   is very fragile
35526 2e 20 20 41 70 70 6c 79 20 66 69 78 20 74 6f 0a  .  Apply fix to.
35527 2a 2a 20 66 74 73 33 20 61 73 20 61 70 70 72 6f  ** fts3 as appro
35528 70 72 69 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69  priate..*/.stati
35529 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 46 69  c int fulltextFi
3552a 6c 74 65 72 28 0a 20 20 73 71 6c 69 74 65 33 5f  lter(.  sqlite3_
3552b 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75  vtab_cursor *pCu
3552c 72 73 6f 72 2c 20 20 20 20 20 2f 2a 20 54 68 65  rsor,     /* The
3552d 20 63 75 72 73 6f 72 20 75 73 65 64 20 66 6f 72   cursor used for
3552e 20 74 68 69 73 20 71 75 65 72 79 20 2a 2f 0a 20   this query */. 
3552f 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e   int idxNum, con
35530 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 72 2c  st char *idxStr,
35531 20 20 20 2f 2a 20 57 68 69 63 68 20 69 6e 64 65     /* Which inde
35532 78 69 6e 67 20 73 63 68 65 6d 65 20 74 6f 20 75  xing scheme to u
35533 73 65 20 2a 2f 0a 20 20 69 6e 74 20 61 72 67 63  se */.  int argc
35534 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
35535 2a 2a 61 72 67 76 20 20 20 20 2f 2a 20 41 72 67  **argv    /* Arg
35536 75 6d 65 6e 74 73 20 66 6f 72 20 74 68 65 20 69  uments for the i
35537 6e 64 65 78 69 6e 67 20 73 63 68 65 6d 65 20 2a  ndexing scheme *
35538 2f 0a 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f  /.){.  fulltext_
35539 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 66 75 6c  cursor *c = (ful
3553a 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 29 20  ltext_cursor *) 
3553b 70 43 75 72 73 6f 72 3b 0a 20 20 66 75 6c 6c 74  pCursor;.  fullt
3553c 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20 63 75  ext_vtab *v = cu
3553d 72 73 6f 72 5f 76 74 61 62 28 63 29 3b 0a 20 20  rsor_vtab(c);.  
3553e 69 6e 74 20 72 63 3b 0a 0a 20 20 46 54 53 54 52  int rc;..  FTSTR
3553f 41 43 45 28 28 22 46 54 53 33 20 46 69 6c 74 65  ACE(("FTS3 Filte
35540 72 20 25 70 5c 6e 22 2c 70 43 75 72 73 6f 72 29  r %p\n",pCursor)
35541 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
35542 63 75 72 73 6f 72 20 68 61 73 20 61 20 73 74 61  cursor has a sta
35543 74 65 6d 65 6e 74 20 74 68 61 74 20 77 61 73 20  tement that was 
35544 6e 6f 74 20 70 72 65 70 61 72 65 64 20 61 63 63  not prepared acc
35545 6f 72 64 69 6e 67 20 74 6f 0a 20 20 2a 2a 20 69  ording to.  ** i
35546 64 78 4e 75 6d 2c 20 63 6c 65 61 72 20 69 74 2e  dxNum, clear it.
35547 20 20 49 20 62 65 6c 69 65 76 65 20 61 6c 6c 20    I believe all 
35548 63 61 6c 6c 73 20 74 6f 20 66 75 6c 6c 74 65 78  calls to fulltex
35549 74 46 69 6c 74 65 72 20 77 69 74 68 20 61 0a 20  tFilter with a. 
3554a 20 2a 2a 20 67 69 76 65 6e 20 63 75 72 73 6f 72   ** given cursor
3554b 20 77 69 6c 6c 20 68 61 76 65 20 74 68 65 20 73   will have the s
3554c 61 6d 65 20 69 64 78 4e 75 6d 20 2c 20 62 75 74  ame idxNum , but
3554d 20 69 6e 20 74 68 69 73 20 63 61 73 65 20 69 74   in this case it
3554e 27 73 0a 20 20 2a 2a 20 65 61 73 79 20 74 6f 20  's.  ** easy to 
3554f 62 65 20 73 61 66 65 2e 0a 20 20 2a 2f 0a 20 20  be safe..  */.  
35550 69 66 28 20 63 2d 3e 70 53 74 6d 74 20 26 26 20  if( c->pStmt && 
35551 63 2d 3e 69 43 75 72 73 6f 72 54 79 70 65 21 3d  c->iCursorType!=
35552 69 64 78 4e 75 6d 20 29 7b 0a 20 20 20 20 73 71  idxNum ){.    sq
35553 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 63  lite3_finalize(c
35554 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 63 2d  ->pStmt);.    c-
35555 3e 70 53 74 6d 74 20 3d 20 4e 55 4c 4c 3b 0a 20  >pStmt = NULL;. 
35556 20 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 61 20 66   }..  /* Get a f
35557 72 65 73 68 20 73 74 61 74 65 6d 65 6e 74 20 61  resh statement a
35558 70 70 72 6f 70 72 69 61 74 65 20 74 6f 20 69 64  ppropriate to id
35559 78 4e 75 6d 2e 20 2a 2f 0a 20 20 2f 2a 20 54 4f  xNum. */.  /* TO
3555a 44 4f 28 73 68 65 73 73 29 3a 20 41 64 64 20 61  DO(shess): Add a
3555b 20 70 72 65 70 61 72 65 64 2d 73 74 61 74 65 6d   prepared-statem
3555c 65 6e 74 20 63 61 63 68 65 20 69 6e 20 74 68 65  ent cache in the
3555d 20 76 74 20 73 74 72 75 63 74 75 72 65 2e 0a 20   vt structure.. 
3555e 20 2a 2a 20 54 68 65 20 63 61 63 68 65 20 6d 75   ** The cache mu
3555f 73 74 20 68 61 6e 64 6c 65 20 6d 75 6c 74 69 70  st handle multip
35560 6c 65 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 2e  le open cursors.
35561 20 20 45 61 73 69 65 72 20 74 6f 20 63 61 63 68    Easier to cach
35562 65 20 74 68 65 0a 20 20 2a 2a 20 73 74 61 74 65  e the.  ** state
35563 6d 65 6e 74 20 76 61 72 69 61 6e 74 73 20 61 74  ment variants at
35564 20 74 68 65 20 76 74 20 74 6f 20 72 65 64 75 63   the vt to reduc
35565 65 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c 6f 63  e malloc/realloc
35566 2f 66 72 65 65 20 68 65 72 65 2e 0a 20 20 2a 2a  /free here..  **
35567 20 4f 72 20 77 65 20 63 6f 75 6c 64 20 68 61 76   Or we could hav
35568 65 20 61 20 53 74 72 69 6e 67 42 75 66 66 65 72  e a StringBuffer
35569 20 76 61 72 69 61 6e 74 20 77 68 69 63 68 20 61   variant which a
3556a 6c 6c 6f 77 65 64 20 73 74 61 63 6b 0a 20 20 2a  llowed stack.  *
3556b 2a 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 66  * construction f
3556c 6f 72 20 73 6d 61 6c 6c 20 76 61 6c 75 65 73 2e  or small values.
3556d 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 63 2d 3e  .  */.  if( !c->
3556e 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 53 74 72  pStmt ){.    Str
3556f 69 6e 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20  ingBuffer sb;.  
35570 20 20 69 6e 69 74 53 74 72 69 6e 67 42 75 66 66    initStringBuff
35571 65 72 28 26 73 62 29 3b 0a 20 20 20 20 61 70 70  er(&sb);.    app
35572 65 6e 64 28 26 73 62 2c 20 22 53 45 4c 45 43 54  end(&sb, "SELECT
35573 20 64 6f 63 69 64 2c 20 22 29 3b 0a 20 20 20 20   docid, ");.    
35574 61 70 70 65 6e 64 4c 69 73 74 28 26 73 62 2c 20  appendList(&sb, 
35575 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 76 2d 3e 61  v->nColumn, v->a
35576 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b  zContentColumn);
35577 0a 20 20 20 20 61 70 70 65 6e 64 28 26 73 62 2c  .    append(&sb,
35578 20 22 20 46 52 4f 4d 20 25 5f 63 6f 6e 74 65 6e   " FROM %_conten
35579 74 22 29 3b 0a 20 20 20 20 69 66 28 20 69 64 78  t");.    if( idx
3557a 4e 75 6d 21 3d 51 55 45 52 59 5f 47 45 4e 45 52  Num!=QUERY_GENER
3557b 49 43 20 29 20 61 70 70 65 6e 64 28 26 73 62 2c  IC ) append(&sb,
3557c 20 22 20 57 48 45 52 45 20 64 6f 63 69 64 20 3d   " WHERE docid =
3557d 20 3f 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 73   ?");.    rc = s
3557e 71 6c 5f 70 72 65 70 61 72 65 28 76 2d 3e 64 62  ql_prepare(v->db
3557f 2c 20 76 2d 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61  , v->zDb, v->zNa
35580 6d 65 2c 20 26 63 2d 3e 70 53 74 6d 74 2c 0a 20  me, &c->pStmt,. 
35581 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35582 20 20 20 20 73 74 72 69 6e 67 42 75 66 66 65 72      stringBuffer
35583 44 61 74 61 28 26 73 62 29 29 3b 0a 20 20 20 20  Data(&sb));.    
35584 73 74 72 69 6e 67 42 75 66 66 65 72 44 65 73 74  stringBufferDest
35585 72 6f 79 28 26 73 62 29 3b 0a 20 20 20 20 69 66  roy(&sb);.    if
35586 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
35587 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
35588 20 63 2d 3e 69 43 75 72 73 6f 72 54 79 70 65 20   c->iCursorType 
35589 3d 20 69 64 78 4e 75 6d 3b 0a 20 20 7d 65 6c 73  = idxNum;.  }els
3558a 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  e{.    sqlite3_r
3558b 65 73 65 74 28 63 2d 3e 70 53 74 6d 74 29 3b 0a  eset(c->pStmt);.
3558c 20 20 20 20 61 73 73 65 72 74 28 20 63 2d 3e 69      assert( c->i
3558d 43 75 72 73 6f 72 54 79 70 65 3d 3d 69 64 78 4e  CursorType==idxN
3558e 75 6d 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 77 69  um );.  }..  swi
3558f 74 63 68 28 20 69 64 78 4e 75 6d 20 29 7b 0a 20  tch( idxNum ){. 
35590 20 20 20 63 61 73 65 20 51 55 45 52 59 5f 47 45     case QUERY_GE
35591 4e 45 52 49 43 3a 0a 20 20 20 20 20 20 62 72 65  NERIC:.      bre
35592 61 6b 3b 0a 0a 20 20 20 20 63 61 73 65 20 51 55  ak;..    case QU
35593 45 52 59 5f 44 4f 43 49 44 3a 0a 20 20 20 20 20  ERY_DOCID:.     
35594 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
35595 6e 64 5f 69 6e 74 36 34 28 63 2d 3e 70 53 74 6d  nd_int64(c->pStm
35596 74 2c 20 31 2c 20 73 71 6c 69 74 65 33 5f 76 61  t, 1, sqlite3_va
35597 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30  lue_int64(argv[0
35598 5d 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ]));.      if( r
35599 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
3559a 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
3559b 62 72 65 61 6b 3b 0a 0a 20 20 20 20 64 65 66 61  break;..    defa
3559c 75 6c 74 3a 20 20 20 2f 2a 20 66 75 6c 6c 2d 74  ult:   /* full-t
3559d 65 78 74 20 73 65 61 72 63 68 20 2a 2f 0a 20 20  ext search */.  
3559e 20 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43    {.      int iC
3559f 6f 6c 20 3d 20 69 64 78 4e 75 6d 2d 51 55 45 52  ol = idxNum-QUER
355a0 59 5f 46 55 4c 4c 54 45 58 54 3b 0a 20 20 20 20  Y_FULLTEXT;.    
355a1 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 51    const char *zQ
355a2 75 65 72 79 20 3d 20 28 63 6f 6e 73 74 20 63 68  uery = (const ch
355a3 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
355a4 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
355a5 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
355a6 69 64 78 4e 75 6d 3c 3d 51 55 45 52 59 5f 46 55  idxNum<=QUERY_FU
355a7 4c 4c 54 45 58 54 2b 76 2d 3e 6e 43 6f 6c 75 6d  LLTEXT+v->nColum
355a8 6e 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  n);.      assert
355a9 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 20  ( argc==1 );.   
355aa 20 20 20 69 66 28 20 63 2d 3e 72 65 73 75 6c 74     if( c->result
355ab 2e 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20  .nData!=0 ){.   
355ac 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 73       /* This cas
355ad 65 20 68 61 70 70 65 6e 73 20 69 66 20 74 68 65  e happens if the
355ae 20 73 61 6d 65 20 63 75 72 73 6f 72 20 69 73 20   same cursor is 
355af 75 73 65 64 20 72 65 70 65 61 74 65 64 6c 79 2e  used repeatedly.
355b0 20 2a 2f 0a 20 20 20 20 20 20 20 20 64 6c 72 44   */.        dlrD
355b1 65 73 74 72 6f 79 28 26 63 2d 3e 72 65 61 64 65  estroy(&c->reade
355b2 72 29 3b 0a 20 20 20 20 20 20 20 20 64 61 74 61  r);.        data
355b3 42 75 66 66 65 72 52 65 73 65 74 28 26 63 2d 3e  BufferReset(&c->
355b4 72 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 7d  result);.      }
355b5 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 64 61  else{.        da
355b6 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 63 2d  taBufferInit(&c-
355b7 3e 72 65 73 75 6c 74 2c 20 30 29 3b 0a 20 20 20  >result, 0);.   
355b8 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20     }.      rc = 
355b9 66 75 6c 6c 74 65 78 74 51 75 65 72 79 28 76 2c  fulltextQuery(v,
355ba 20 69 43 6f 6c 2c 20 7a 51 75 65 72 79 2c 20 2d   iCol, zQuery, -
355bb 31 2c 20 26 63 2d 3e 72 65 73 75 6c 74 2c 20 26  1, &c->result, &
355bc 63 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 20  c->pExpr);.     
355bd 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
355be 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
355bf 20 20 20 20 20 20 69 66 28 20 63 2d 3e 72 65 73        if( c->res
355c0 75 6c 74 2e 6e 44 61 74 61 21 3d 30 20 29 7b 0a  ult.nData!=0 ){.
355c1 20 20 20 20 20 20 20 20 64 6c 72 49 6e 69 74 28          dlrInit(
355c2 26 63 2d 3e 72 65 61 64 65 72 2c 20 44 4c 5f 44  &c->reader, DL_D
355c3 4f 43 49 44 53 2c 20 63 2d 3e 72 65 73 75 6c 74  OCIDS, c->result
355c4 2e 70 44 61 74 61 2c 20 63 2d 3e 72 65 73 75 6c  .pData, c->resul
355c5 74 2e 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20  t.nData);.      
355c6 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
355c7 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
355c8 72 6e 20 66 75 6c 6c 74 65 78 74 4e 65 78 74 28  rn fulltextNext(
355c9 70 43 75 72 73 6f 72 29 3b 0a 7d 0a 0a 2f 2a 20  pCursor);.}../* 
355ca 54 68 69 73 20 69 73 20 74 68 65 20 78 45 6f 66  This is the xEof
355cb 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 76   method of the v
355cc 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54  irtual table.  T
355cd 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 0a 2a  he SQLite core.*
355ce 2a 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  * calls this rou
355cf 74 69 6e 65 20 74 6f 20 66 69 6e 64 20 6f 75 74  tine to find out
355d0 20 69 66 20 69 74 20 68 61 73 20 72 65 61 63 68   if it has reach
355d1 65 64 20 74 68 65 20 65 6e 64 20 6f 66 0a 2a 2a  ed the end of.**
355d2 20 61 20 71 75 65 72 79 27 73 20 72 65 73 75 6c   a query's resul
355d3 74 73 20 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69  ts set..*/.stati
355d4 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 45 6f  c int fulltextEo
355d5 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  f(sqlite3_vtab_c
355d6 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b  ursor *pCursor){
355d7 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  .  fulltext_curs
355d8 6f 72 20 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78  or *c = (fulltex
355d9 74 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72  t_cursor *) pCur
355da 73 6f 72 3b 0a 20 20 72 65 74 75 72 6e 20 63 2d  sor;.  return c-
355db 3e 65 6f 66 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73  >eof;.}../* This
355dc 20 69 73 20 74 68 65 20 78 43 6f 6c 75 6d 6e 20   is the xColumn 
355dd 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 76 69  method of the vi
355de 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68  rtual table.  Th
355df 65 20 53 51 4c 69 74 65 0a 2a 2a 20 63 6f 72 65  e SQLite.** core
355e0 20 63 61 6c 6c 73 20 74 68 69 73 20 6d 65 74 68   calls this meth
355e1 6f 64 20 64 75 72 69 6e 67 20 61 20 71 75 65 72  od during a quer
355e2 79 20 77 68 65 6e 20 69 74 20 6e 65 65 64 73 20  y when it needs 
355e3 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20  the value.** of 
355e4 61 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 74 68  a column from th
355e5 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
355e6 20 20 54 68 69 73 20 6d 65 74 68 6f 64 20 6e 65    This method ne
355e7 65 64 73 20 74 6f 20 75 73 65 0a 2a 2a 20 6f 6e  eds to use.** on
355e8 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  e of the sqlite3
355e9 5f 72 65 73 75 6c 74 5f 2a 28 29 20 72 6f 75 74  _result_*() rout
355ea 69 6e 65 73 20 74 6f 20 73 74 6f 72 65 20 74 68  ines to store th
355eb 65 20 72 65 71 75 65 73 74 65 64 0a 2a 2a 20 76  e requested.** v
355ec 61 6c 75 65 20 62 61 63 6b 20 69 6e 20 74 68 65  alue back in the
355ed 20 70 43 6f 6e 74 65 78 74 2e 0a 2a 2f 0a 73 74   pContext..*/.st
355ee 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78  atic int fulltex
355ef 74 43 6f 6c 75 6d 6e 28 73 71 6c 69 74 65 33 5f  tColumn(sqlite3_
355f0 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75  vtab_cursor *pCu
355f1 72 73 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20  rsor,.          
355f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
355f3 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
355f4 2a 70 43 6f 6e 74 65 78 74 2c 20 69 6e 74 20 69  *pContext, int i
355f5 64 78 43 6f 6c 29 7b 0a 20 20 66 75 6c 6c 74 65  dxCol){.  fullte
355f6 78 74 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28  xt_cursor *c = (
355f7 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20  fulltext_cursor 
355f8 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 66 75  *) pCursor;.  fu
355f9 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d  lltext_vtab *v =
355fa 20 63 75 72 73 6f 72 5f 76 74 61 62 28 63 29 3b   cursor_vtab(c);
355fb 0a 0a 20 20 69 66 28 20 69 64 78 43 6f 6c 3c 76  ..  if( idxCol<v
355fc 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->nColumn ){.   
355fd 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
355fe 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 63  pVal = sqlite3_c
355ff 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 63 2d 3e 70  olumn_value(c->p
35600 53 74 6d 74 2c 20 69 64 78 43 6f 6c 2b 31 29 3b  Stmt, idxCol+1);
35601 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
35602 75 6c 74 5f 76 61 6c 75 65 28 70 43 6f 6e 74 65  ult_value(pConte
35603 78 74 2c 20 70 56 61 6c 29 3b 0a 20 20 7d 65 6c  xt, pVal);.  }el
35604 73 65 20 69 66 28 20 69 64 78 43 6f 6c 3d 3d 76  se if( idxCol==v
35605 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->nColumn ){.   
35606 20 2f 2a 20 54 68 65 20 65 78 74 72 61 20 63 6f   /* The extra co
35607 6c 75 6d 6e 20 77 68 6f 73 65 20 6e 61 6d 65 20  lumn whose name 
35608 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  is the same as t
35609 68 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a  he table..    **
3560a 20 52 65 74 75 72 6e 20 61 20 62 6c 6f 62 20 77   Return a blob w
3560b 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65  hich is a pointe
3560c 72 20 74 6f 20 74 68 65 20 63 75 72 73 6f 72 0a  r to the cursor.
3560d 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
3560e 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 70  e3_result_blob(p
3560f 43 6f 6e 74 65 78 74 2c 20 26 63 2c 20 73 69 7a  Context, &c, siz
35610 65 6f 66 28 63 29 2c 20 53 51 4c 49 54 45 5f 54  eof(c), SQLITE_T
35611 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 65 6c  RANSIENT);.  }el
35612 73 65 20 69 66 28 20 69 64 78 43 6f 6c 3d 3d 76  se if( idxCol==v
35613 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 20 29 7b 0a 20  ->nColumn+1 ){. 
35614 20 20 20 2f 2a 20 54 68 65 20 64 6f 63 69 64 20     /* The docid 
35615 63 6f 6c 75 6d 6e 2c 20 77 68 69 63 68 20 69 73  column, which is
35616 20 61 6e 20 61 6c 69 61 73 20 66 6f 72 20 72 6f   an alias for ro
35617 77 69 64 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69  wid. */.    sqli
35618 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20  te3_value *pVal 
35619 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
3561a 5f 76 61 6c 75 65 28 63 2d 3e 70 53 74 6d 74 2c  _value(c->pStmt,
3561b 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
3561c 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 70 43  _result_value(pC
3561d 6f 6e 74 65 78 74 2c 20 70 56 61 6c 29 3b 0a 20  ontext, pVal);. 
3561e 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
3561f 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 68 69  TE_OK;.}../* Thi
35620 73 20 69 73 20 74 68 65 20 78 52 6f 77 69 64 20  s is the xRowid 
35621 6d 65 74 68 6f 64 2e 20 20 54 68 65 20 53 51 4c  method.  The SQL
35622 69 74 65 20 63 6f 72 65 20 63 61 6c 6c 73 20 74  ite core calls t
35623 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 0a 2a  his routine to.*
35624 2a 20 72 65 74 72 69 65 76 65 20 74 68 65 20 72  * retrieve the r
35625 6f 77 69 64 20 66 6f 72 20 74 68 65 20 63 75 72  owid for the cur
35626 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20  rent row of the 
35627 72 65 73 75 6c 74 20 73 65 74 2e 20 20 66 74 73  result set.  fts
35628 33 0a 2a 2a 20 65 78 70 6f 73 65 73 20 25 5f 63  3.** exposes %_c
35629 6f 6e 74 65 6e 74 2e 64 6f 63 69 64 20 61 73 20  ontent.docid as 
3562a 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68  the rowid for th
3562b 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
3562c 20 20 54 68 65 0a 2a 2a 20 72 6f 77 69 64 20 73    The.** rowid s
3562d 68 6f 75 6c 64 20 62 65 20 77 72 69 74 74 65 6e  hould be written
3562e 20 74 6f 20 2a 70 52 6f 77 69 64 2e 0a 2a 2f 0a   to *pRowid..*/.
3562f 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74  static int fullt
35630 65 78 74 52 6f 77 69 64 28 73 71 6c 69 74 65 33  extRowid(sqlite3
35631 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43  _vtab_cursor *pC
35632 75 72 73 6f 72 2c 20 73 71 6c 69 74 65 5f 69 6e  ursor, sqlite_in
35633 74 36 34 20 2a 70 52 6f 77 69 64 29 7b 0a 20 20  t64 *pRowid){.  
35634 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20  fulltext_cursor 
35635 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 63  *c = (fulltext_c
35636 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72  ursor *) pCursor
35637 3b 0a 0a 20 20 2a 70 52 6f 77 69 64 20 3d 20 73  ;..  *pRowid = s
35638 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
35639 74 36 34 28 63 2d 3e 70 53 74 6d 74 2c 20 30 29  t64(c->pStmt, 0)
3563a 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
3563b 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20  E_OK;.}../* Add 
3563c 61 6c 6c 20 74 65 72 6d 73 20 69 6e 20 5b 7a 54  all terms in [zT
3563d 65 78 74 5d 20 74 6f 20 70 65 6e 64 69 6e 67 54  ext] to pendingT
3563e 65 72 6d 73 20 74 61 62 6c 65 2e 20 20 49 66 20  erms table.  If 
3563f 5b 69 43 6f 6c 75 6d 6e 5d 20 3e 20 30 2c 0a 2a  [iColumn] > 0,.*
35640 2a 20 77 65 20 61 6c 73 6f 20 73 74 6f 72 65 20  * we also store 
35641 70 6f 73 69 74 69 6f 6e 73 20 61 6e 64 20 6f 66  positions and of
35642 66 73 65 74 73 20 69 6e 20 74 68 65 20 68 61 73  fsets in the has
35643 68 20 74 61 62 6c 65 20 75 73 69 6e 67 20 74 68  h table using th
35644 61 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d  at.** column num
35645 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ber..*/.static i
35646 6e 74 20 62 75 69 6c 64 54 65 72 6d 73 28 66 75  nt buildTerms(fu
35647 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
35648 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f  sqlite_int64 iDo
35649 63 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20  cid,.           
3564a 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
3564b 20 63 68 61 72 20 2a 7a 54 65 78 74 2c 20 69 6e   char *zText, in
3564c 74 20 69 43 6f 6c 75 6d 6e 29 7b 0a 20 20 73 71  t iColumn){.  sq
3564d 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
3564e 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 76 2d  *pTokenizer = v-
3564f 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73  >pTokenizer;.  s
35650 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
35651 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
35652 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
35653 70 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20 6e 54  pToken;.  int nT
35654 6f 6b 65 6e 42 79 74 65 73 3b 0a 20 20 69 6e 74  okenBytes;.  int
35655 20 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 69   iStartOffset, i
35656 45 6e 64 4f 66 66 73 65 74 2c 20 69 50 6f 73 69  EndOffset, iPosi
35657 74 69 6f 6e 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  tion;.  int rc;.
35658 0a 20 20 72 63 20 3d 20 70 54 6f 6b 65 6e 69 7a  .  rc = pTokeniz
35659 65 72 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70  er->pModule->xOp
3565a 65 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a  en(pTokenizer, z
3565b 54 65 78 74 2c 20 2d 31 2c 20 26 70 43 75 72 73  Text, -1, &pCurs
3565c 6f 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  or);.  if( rc!=S
3565d 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
3565e 6e 20 72 63 3b 0a 0a 20 20 70 43 75 72 73 6f 72  n rc;..  pCursor
3565f 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70  ->pTokenizer = p
35660 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 77 68 69  Tokenizer;.  whi
35661 6c 65 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28  le( SQLITE_OK==(
35662 72 63 3d 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70  rc=pTokenizer->p
35663 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43  Module->xNext(pC
35664 75 72 73 6f 72 2c 0a 20 20 20 20 20 20 20 20 20  ursor,.         
35665 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35666 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35667 20 20 20 20 20 20 20 20 20 20 26 70 54 6f 6b 65            &pToke
35668 6e 2c 20 26 6e 54 6f 6b 65 6e 42 79 74 65 73 2c  n, &nTokenBytes,
35669 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3566a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3566b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3566c 20 20 20 20 26 69 53 74 61 72 74 4f 66 66 73 65      &iStartOffse
3566d 74 2c 20 26 69 45 6e 64 4f 66 66 73 65 74 2c 0a  t, &iEndOffset,.
3566e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3566f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35671 20 20 20 26 69 50 6f 73 69 74 69 6f 6e 29 29 20     &iPosition)) 
35672 29 7b 0a 20 20 20 20 44 4c 43 6f 6c 6c 65 63 74  ){.    DLCollect
35673 6f 72 20 2a 70 3b 0a 20 20 20 20 69 6e 74 20 6e  or *p;.    int n
35674 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  Data;           
35675 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
35676 6f 66 20 64 6f 63 6c 69 73 74 20 62 65 66 6f 72  of doclist befor
35677 65 20 6f 75 72 20 75 70 64 61 74 65 2e 20 2a 2f  e our update. */
35678 0a 0a 20 20 20 20 2f 2a 20 50 6f 73 69 74 69 6f  ..    /* Positio
35679 6e 73 20 63 61 6e 27 74 20 62 65 20 6e 65 67 61  ns can't be nega
3567a 74 69 76 65 3b 20 77 65 20 75 73 65 20 2d 31 20  tive; we use -1 
3567b 61 73 20 61 20 74 65 72 6d 69 6e 61 74 6f 72 0a  as a terminator.
3567c 20 20 20 20 20 2a 20 69 6e 74 65 72 6e 61 6c 6c       * internall
3567d 79 2e 20 20 54 6f 6b 65 6e 20 63 61 6e 27 74 20  y.  Token can't 
3567e 62 65 20 4e 55 4c 4c 20 6f 72 20 65 6d 70 74 79  be NULL or empty
3567f 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 50 6f  . */.    if( iPo
35680 73 69 74 69 6f 6e 3c 30 20 7c 7c 20 70 54 6f 6b  sition<0 || pTok
35681 65 6e 20 3d 3d 20 4e 55 4c 4c 20 7c 7c 20 6e 54  en == NULL || nT
35682 6f 6b 65 6e 42 79 74 65 73 20 3d 3d 20 30 20 29  okenBytes == 0 )
35683 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
35684 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
35685 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
35686 20 20 20 70 20 3d 20 66 74 73 33 48 61 73 68 46     p = fts3HashF
35687 69 6e 64 28 26 76 2d 3e 70 65 6e 64 69 6e 67 54  ind(&v->pendingT
35688 65 72 6d 73 2c 20 70 54 6f 6b 65 6e 2c 20 6e 54  erms, pToken, nT
35689 6f 6b 65 6e 42 79 74 65 73 29 3b 0a 20 20 20 20  okenBytes);.    
3568a 69 66 28 20 70 3d 3d 4e 55 4c 4c 20 29 7b 0a 20  if( p==NULL ){. 
3568b 20 20 20 20 20 6e 44 61 74 61 20 3d 20 30 3b 0a       nData = 0;.
3568c 20 20 20 20 20 20 70 20 3d 20 64 6c 63 4e 65 77        p = dlcNew
3568d 28 69 44 6f 63 69 64 2c 20 44 4c 5f 44 45 46 41  (iDocid, DL_DEFA
3568e 55 4c 54 29 3b 0a 20 20 20 20 20 20 66 74 73 33  ULT);.      fts3
3568f 48 61 73 68 49 6e 73 65 72 74 28 26 76 2d 3e 70  HashInsert(&v->p
35690 65 6e 64 69 6e 67 54 65 72 6d 73 2c 20 70 54 6f  endingTerms, pTo
35691 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 42 79 74 65 73  ken, nTokenBytes
35692 2c 20 70 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  , p);..      /* 
35693 4f 76 65 72 68 65 61 64 20 66 6f 72 20 6f 75 72  Overhead for our
35694 20 68 61 73 68 20 74 61 62 6c 65 20 65 6e 74 72   hash table entr
35695 79 2c 20 74 68 65 20 6b 65 79 2c 20 61 6e 64 20  y, the key, and 
35696 74 68 65 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20  the value. */.  
35697 20 20 20 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44      v->nPendingD
35698 61 74 61 20 2b 3d 20 73 69 7a 65 6f 66 28 73 74  ata += sizeof(st
35699 72 75 63 74 20 66 74 73 33 48 61 73 68 45 6c 65  ruct fts3HashEle
3569a 6d 29 2b 73 69 7a 65 6f 66 28 2a 70 29 2b 6e 54  m)+sizeof(*p)+nT
3569b 6f 6b 65 6e 42 79 74 65 73 3b 0a 20 20 20 20 7d  okenBytes;.    }
3569c 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 44 61 74  else{.      nDat
3569d 61 20 3d 20 70 2d 3e 62 2e 6e 44 61 74 61 3b 0a  a = p->b.nData;.
3569e 20 20 20 20 20 20 69 66 28 20 70 2d 3e 64 6c 77        if( p->dlw
3569f 2e 69 50 72 65 76 44 6f 63 69 64 21 3d 69 44 6f  .iPrevDocid!=iDo
356a0 63 69 64 20 29 20 64 6c 63 4e 65 78 74 28 70 2c  cid ) dlcNext(p,
356a1 20 69 44 6f 63 69 64 29 3b 0a 20 20 20 20 7d 0a   iDocid);.    }.
356a2 20 20 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3e      if( iColumn>
356a3 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 6c 63 41  =0 ){.      dlcA
356a4 64 64 50 6f 73 28 70 2c 20 69 43 6f 6c 75 6d 6e  ddPos(p, iColumn
356a5 2c 20 69 50 6f 73 69 74 69 6f 6e 2c 20 69 53 74  , iPosition, iSt
356a6 61 72 74 4f 66 66 73 65 74 2c 20 69 45 6e 64 4f  artOffset, iEndO
356a7 66 66 73 65 74 29 3b 0a 20 20 20 20 7d 0a 0a 20  ffset);.    }.. 
356a8 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65     /* Accumulate
356a9 20 64 61 74 61 20 61 64 64 65 64 20 62 79 20 64   data added by d
356aa 6c 63 4e 65 77 20 6f 72 20 64 6c 63 4e 65 78 74  lcNew or dlcNext
356ab 2c 20 61 6e 64 20 64 6c 63 41 64 64 50 6f 73 2e  , and dlcAddPos.
356ac 20 2a 2f 0a 20 20 20 20 76 2d 3e 6e 50 65 6e 64   */.    v->nPend
356ad 69 6e 67 44 61 74 61 20 2b 3d 20 70 2d 3e 62 2e  ingData += p->b.
356ae 6e 44 61 74 61 2d 6e 44 61 74 61 3b 0a 20 20 7d  nData-nData;.  }
356af 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73  ..  /* TODO(shes
356b0 73 29 20 43 68 65 63 6b 20 72 65 74 75 72 6e 3f  s) Check return?
356b1 20 20 53 68 6f 75 6c 64 20 74 68 69 73 20 62 65    Should this be
356b2 20 61 62 6c 65 20 74 6f 20 63 61 75 73 65 20 65   able to cause e
356b3 72 72 6f 72 73 20 61 74 0a 20 20 2a 2a 20 74 68  rrors at.  ** th
356b4 69 73 20 70 6f 69 6e 74 3f 20 20 41 63 74 75 61  is point?  Actua
356b5 6c 6c 79 2c 20 73 61 6d 65 20 71 75 65 73 74 69  lly, same questi
356b6 6f 6e 20 61 62 6f 75 74 20 73 71 6c 69 74 65 33  on about sqlite3
356b7 5f 66 69 6e 61 6c 69 7a 65 28 29 2c 0a 20 20 2a  _finalize(),.  *
356b8 2a 20 74 68 6f 75 67 68 20 6f 6e 65 20 63 6f 75  * though one cou
356b9 6c 64 20 61 72 67 75 65 20 74 68 61 74 20 66 61  ld argue that fa
356ba 69 6c 75 72 65 20 74 68 65 72 65 20 6d 65 61 6e  ilure there mean
356bb 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61 20  s that the data 
356bc 69 73 0a 20 20 2a 2a 20 6e 6f 74 20 64 75 72 61  is.  ** not dura
356bd 62 6c 65 2e 20 20 2a 70 6f 6e 64 65 72 2a 0a 20  ble.  *ponder*. 
356be 20 2a 2f 0a 20 20 70 54 6f 6b 65 6e 69 7a 65 72   */.  pTokenizer
356bf 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73  ->pModule->xClos
356c0 65 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 69 66  e(pCursor);.  if
356c1 28 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3d 3d  ( SQLITE_DONE ==
356c2 20 72 63 20 29 20 72 65 74 75 72 6e 20 53 51 4c   rc ) return SQL
356c3 49 54 45 5f 4f 4b 3b 0a 20 20 72 65 74 75 72 6e  ITE_OK;.  return
356c4 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20 64   rc;.}../* Add d
356c5 6f 63 6c 69 73 74 73 20 66 6f 72 20 61 6c 6c 20  oclists for all 
356c6 74 65 72 6d 73 20 69 6e 20 5b 70 56 61 6c 75 65  terms in [pValue
356c7 73 5d 20 74 6f 20 70 65 6e 64 69 6e 67 54 65 72  s] to pendingTer
356c8 6d 73 20 74 61 62 6c 65 2e 20 2a 2f 0a 73 74 61  ms table. */.sta
356c9 74 69 63 20 69 6e 74 20 69 6e 73 65 72 74 54 65  tic int insertTe
356ca 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  rms(fulltext_vta
356cb 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74  b *v, sqlite_int
356cc 36 34 20 69 44 6f 63 69 64 2c 0a 20 20 20 20 20  64 iDocid,.     
356cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
356ce 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
356cf 2a 2a 70 56 61 6c 75 65 73 29 7b 0a 20 20 69 6e  **pValues){.  in
356d0 74 20 69 3b 0a 20 20 66 6f 72 28 69 20 3d 20 30  t i;.  for(i = 0
356d1 3b 20 69 20 3c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e  ; i < v->nColumn
356d2 20 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 63 68 61   ; ++i){.    cha
356d3 72 20 2a 7a 54 65 78 74 20 3d 20 28 63 68 61 72  r *zText = (char
356d4 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
356d5 74 65 78 74 28 70 56 61 6c 75 65 73 5b 69 5d 29  text(pValues[i])
356d6 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 62  ;.    int rc = b
356d7 75 69 6c 64 54 65 72 6d 73 28 76 2c 20 69 44 6f  uildTerms(v, iDo
356d8 63 69 64 2c 20 7a 54 65 78 74 2c 20 69 29 3b 0a  cid, zText, i);.
356d9 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
356da 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
356db 63 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  c;.  }.  return 
356dc 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
356dd 20 41 64 64 20 65 6d 70 74 79 20 64 6f 63 6c 69   Add empty docli
356de 73 74 73 20 66 6f 72 20 61 6c 6c 20 74 65 72 6d  sts for all term
356df 73 20 69 6e 20 74 68 65 20 67 69 76 65 6e 20 72  s in the given r
356e0 6f 77 27 73 20 63 6f 6e 74 65 6e 74 20 74 6f 0a  ow's content to.
356e1 2a 2a 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 2e  ** pendingTerms.
356e2 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
356e3 65 6c 65 74 65 54 65 72 6d 73 28 66 75 6c 6c 74  eleteTerms(fullt
356e4 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c  ext_vtab *v, sql
356e5 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64  ite_int64 iDocid
356e6 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
356e7 2a 2a 70 56 61 6c 75 65 73 3b 0a 20 20 69 6e 74  **pValues;.  int
356e8 20 69 2c 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 4f   i, rc;..  /* TO
356e9 44 4f 28 73 68 65 73 73 29 20 53 68 6f 75 6c 64  DO(shess) Should
356ea 20 77 65 20 61 6c 6c 6f 77 20 73 75 63 68 20 74   we allow such t
356eb 61 62 6c 65 73 20 61 74 20 61 6c 6c 3f 20 2a 2f  ables at all? */
356ec 0a 20 20 69 66 28 20 44 4c 5f 44 45 46 41 55 4c  .  if( DL_DEFAUL
356ed 54 3d 3d 44 4c 5f 44 4f 43 49 44 53 20 29 20 72  T==DL_DOCIDS ) r
356ee 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
356ef 4f 52 3b 0a 0a 20 20 72 63 20 3d 20 63 6f 6e 74  OR;..  rc = cont
356f0 65 6e 74 5f 73 65 6c 65 63 74 28 76 2c 20 69 44  ent_select(v, iD
356f1 6f 63 69 64 2c 20 26 70 56 61 6c 75 65 73 29 3b  ocid, &pValues);
356f2 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
356f3 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
356f4 3b 0a 0a 20 20 66 6f 72 28 69 20 3d 20 30 20 3b  ;..  for(i = 0 ;
356f5 20 69 20 3c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b   i < v->nColumn;
356f6 20 2b 2b 69 29 20 7b 0a 20 20 20 20 72 63 20 3d   ++i) {.    rc =
356f7 20 62 75 69 6c 64 54 65 72 6d 73 28 76 2c 20 69   buildTerms(v, i
356f8 44 6f 63 69 64 2c 20 70 56 61 6c 75 65 73 5b 69  Docid, pValues[i
356f9 5d 2c 20 2d 31 29 3b 0a 20 20 20 20 69 66 28 20  ], -1);.    if( 
356fa 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
356fb 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 66 72  break;.  }..  fr
356fc 65 65 53 74 72 69 6e 67 41 72 72 61 79 28 76 2d  eeStringArray(v-
356fd 3e 6e 43 6f 6c 75 6d 6e 2c 20 70 56 61 6c 75 65  >nColumn, pValue
356fe 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  s);.  return SQL
356ff 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 4f  ITE_OK;.}../* TO
35700 44 4f 28 73 68 65 73 73 29 20 52 65 66 61 63 74  DO(shess) Refact
35701 6f 72 20 74 68 65 20 63 6f 64 65 20 74 6f 20 72  or the code to r
35702 65 6d 6f 76 65 20 74 68 69 73 20 66 6f 72 77 61  emove this forwa
35703 72 64 20 64 65 63 6c 2e 20 2a 2f 0a 73 74 61 74  rd decl. */.stat
35704 69 63 20 69 6e 74 20 69 6e 69 74 50 65 6e 64 69  ic int initPendi
35705 6e 67 54 65 72 6d 73 28 66 75 6c 6c 74 65 78 74  ngTerms(fulltext
35706 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65  _vtab *v, sqlite
35707 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 29 3b 0a  _int64 iDocid);.
35708 0a 2f 2a 20 49 6e 73 65 72 74 20 61 20 72 6f 77  ./* Insert a row
35709 20 69 6e 74 6f 20 74 68 65 20 25 5f 63 6f 6e 74   into the %_cont
3570a 65 6e 74 20 74 61 62 6c 65 3b 20 73 65 74 20 2a  ent table; set *
3570b 70 69 44 6f 63 69 64 20 74 6f 20 62 65 20 74 68  piDocid to be th
3570c 65 20 49 44 20 6f 66 20 74 68 65 0a 2a 2a 20 6e  e ID of the.** n
3570d 65 77 20 72 6f 77 2e 20 20 41 64 64 20 64 6f 63  ew row.  Add doc
3570e 6c 69 73 74 73 20 66 6f 72 20 74 65 72 6d 73 20  lists for terms 
3570f 74 6f 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 2e  to pendingTerms.
35710 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
35711 6e 64 65 78 5f 69 6e 73 65 72 74 28 66 75 6c 6c  ndex_insert(full
35712 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71  text_vtab *v, sq
35713 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 52 65  lite3_value *pRe
35714 71 75 65 73 74 44 6f 63 69 64 2c 0a 20 20 20 20  questDocid,.    
35715 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35716 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75      sqlite3_valu
35717 65 20 2a 2a 70 56 61 6c 75 65 73 2c 20 73 71 6c  e **pValues, sql
35718 69 74 65 5f 69 6e 74 36 34 20 2a 70 69 44 6f 63  ite_int64 *piDoc
35719 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  id){.  int rc;..
3571a 20 20 72 63 20 3d 20 63 6f 6e 74 65 6e 74 5f 69    rc = content_i
3571b 6e 73 65 72 74 28 76 2c 20 70 52 65 71 75 65 73  nsert(v, pReques
3571c 74 44 6f 63 69 64 2c 20 70 56 61 6c 75 65 73 29  tDocid, pValues)
3571d 3b 20 20 2f 2a 20 65 78 65 63 75 74 65 20 61 6e  ;  /* execute an
3571e 20 53 51 4c 20 49 4e 53 45 52 54 20 2a 2f 0a 20   SQL INSERT */. 
3571f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
35720 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
35721 0a 20 20 2f 2a 20 64 6f 63 69 64 20 63 6f 6c 75  .  /* docid colu
35722 6d 6e 20 69 73 20 61 6e 20 61 6c 69 61 73 20 66  mn is an alias f
35723 6f 72 20 72 6f 77 69 64 2e 20 2a 2f 0a 20 20 2a  or rowid. */.  *
35724 70 69 44 6f 63 69 64 20 3d 20 73 71 6c 69 74 65  piDocid = sqlite
35725 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
35726 77 69 64 28 76 2d 3e 64 62 29 3b 0a 20 20 72 63  wid(v->db);.  rc
35727 20 3d 20 69 6e 69 74 50 65 6e 64 69 6e 67 54 65   = initPendingTe
35728 72 6d 73 28 76 2c 20 2a 70 69 44 6f 63 69 64 29  rms(v, *piDocid)
35729 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
3572a 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
3572b 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 69 6e 73  c;..  return ins
3572c 65 72 74 54 65 72 6d 73 28 76 2c 20 2a 70 69 44  ertTerms(v, *piD
3572d 6f 63 69 64 2c 20 70 56 61 6c 75 65 73 29 3b 0a  ocid, pValues);.
3572e 7d 0a 0a 2f 2a 20 44 65 6c 65 74 65 20 61 20 72  }../* Delete a r
3572f 6f 77 20 66 72 6f 6d 20 74 68 65 20 25 5f 63 6f  ow from the %_co
35730 6e 74 65 6e 74 20 74 61 62 6c 65 3b 20 61 64 64  ntent table; add
35731 20 65 6d 70 74 79 20 64 6f 63 6c 69 73 74 73 20   empty doclists 
35732 66 6f 72 20 74 65 72 6d 73 0a 2a 2a 20 74 6f 20  for terms.** to 
35733 70 65 6e 64 69 6e 67 54 65 72 6d 73 2e 0a 2a 2f  pendingTerms..*/
35734 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 64 65  .static int inde
35735 78 5f 64 65 6c 65 74 65 28 66 75 6c 6c 74 65 78  x_delete(fulltex
35736 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74  t_vtab *v, sqlit
35737 65 5f 69 6e 74 36 34 20 69 52 6f 77 29 7b 0a 20  e_int64 iRow){. 
35738 20 69 6e 74 20 72 63 20 3d 20 69 6e 69 74 50 65   int rc = initPe
35739 6e 64 69 6e 67 54 65 72 6d 73 28 76 2c 20 69 52  ndingTerms(v, iR
3573a 6f 77 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  ow);.  if( rc!=S
3573b 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
3573c 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 64 65  n rc;..  rc = de
3573d 6c 65 74 65 54 65 72 6d 73 28 76 2c 20 69 52 6f  leteTerms(v, iRo
3573e 77 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  w);.  if( rc!=SQ
3573f 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
35740 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 63   rc;..  return c
35741 6f 6e 74 65 6e 74 5f 64 65 6c 65 74 65 28 76 2c  ontent_delete(v,
35742 20 69 52 6f 77 29 3b 20 20 2f 2a 20 65 78 65 63   iRow);  /* exec
35743 75 74 65 20 61 6e 20 53 51 4c 20 44 45 4c 45 54  ute an SQL DELET
35744 45 20 2a 2f 0a 7d 0a 0a 2f 2a 20 55 70 64 61 74  E */.}../* Updat
35745 65 20 61 20 72 6f 77 20 69 6e 20 74 68 65 20 25  e a row in the %
35746 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 3b 20  _content table; 
35747 61 64 64 20 64 65 6c 65 74 65 20 64 6f 63 6c 69  add delete docli
35748 73 74 73 20 74 6f 0a 2a 2a 20 70 65 6e 64 69 6e  sts to.** pendin
35749 67 54 65 72 6d 73 20 66 6f 72 20 6f 6c 64 20 74  gTerms for old t
3574a 65 72 6d 73 20 6e 6f 74 20 69 6e 20 74 68 65 20  erms not in the 
3574b 6e 65 77 20 64 61 74 61 2c 20 61 64 64 20 69 6e  new data, add in
3574c 73 65 72 74 20 64 6f 63 6c 69 73 74 73 0a 2a 2a  sert doclists.**
3574d 20 74 6f 20 70 65 6e 64 69 6e 67 54 65 72 6d 73   to pendingTerms
3574e 20 66 6f 72 20 74 65 72 6d 73 20 69 6e 20 74 68   for terms in th
3574f 65 20 6e 65 77 20 64 61 74 61 2e 0a 2a 2f 0a 73  e new data..*/.s
35750 74 61 74 69 63 20 69 6e 74 20 69 6e 64 65 78 5f  tatic int index_
35751 75 70 64 61 74 65 28 66 75 6c 6c 74 65 78 74 5f  update(fulltext_
35752 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f  vtab *v, sqlite_
35753 69 6e 74 36 34 20 69 52 6f 77 2c 0a 20 20 20 20  int64 iRow,.    
35754 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35755 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75      sqlite3_valu
35756 65 20 2a 2a 70 56 61 6c 75 65 73 29 7b 0a 20 20  e **pValues){.  
35757 69 6e 74 20 72 63 20 3d 20 69 6e 69 74 50 65 6e  int rc = initPen
35758 64 69 6e 67 54 65 72 6d 73 28 76 2c 20 69 52 6f  dingTerms(v, iRo
35759 77 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  w);.  if( rc!=SQ
3575a 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
3575b 20 72 63 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72   rc;..  /* Gener
3575c 61 74 65 20 61 6e 20 65 6d 70 74 79 20 64 6f 63  ate an empty doc
3575d 6c 69 73 74 20 66 6f 72 20 65 61 63 68 20 74 65  list for each te
3575e 72 6d 20 74 68 61 74 20 70 72 65 76 69 6f 75 73  rm that previous
3575f 6c 79 20 61 70 70 65 61 72 65 64 20 69 6e 20 74  ly appeared in t
35760 68 69 73 0a 20 20 20 2a 20 72 6f 77 2e 20 2a 2f  his.   * row. */
35761 0a 20 20 72 63 20 3d 20 64 65 6c 65 74 65 54 65  .  rc = deleteTe
35762 72 6d 73 28 76 2c 20 69 52 6f 77 29 3b 0a 20 20  rms(v, iRow);.  
35763 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
35764 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
35765 20 20 72 63 20 3d 20 63 6f 6e 74 65 6e 74 5f 75    rc = content_u
35766 70 64 61 74 65 28 76 2c 20 70 56 61 6c 75 65 73  pdate(v, pValues
35767 2c 20 69 52 6f 77 29 3b 20 20 2f 2a 20 65 78 65  , iRow);  /* exe
35768 63 75 74 65 20 61 6e 20 53 51 4c 20 55 50 44 41  cute an SQL UPDA
35769 54 45 20 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d  TE */.  if( rc!=
3576a 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
3576b 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 4e 6f 77  rn rc;..  /* Now
3576c 20 61 64 64 20 70 6f 73 69 74 69 6f 6e 73 20 66   add positions f
3576d 6f 72 20 74 65 72 6d 73 20 77 68 69 63 68 20 61  or terms which a
3576e 70 70 65 61 72 20 69 6e 20 74 68 65 20 75 70 64  ppear in the upd
3576f 61 74 65 64 20 72 6f 77 2e 20 2a 2f 0a 20 20 72  ated row. */.  r
35770 65 74 75 72 6e 20 69 6e 73 65 72 74 54 65 72 6d  eturn insertTerm
35771 73 28 76 2c 20 69 52 6f 77 2c 20 70 56 61 6c 75  s(v, iRow, pValu
35772 65 73 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  es);.}../*******
35773 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35774 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35775 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35776 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
35777 20 49 6e 74 65 72 69 6f 72 57 72 69 74 65 72 20   InteriorWriter 
35778 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6c 6c 65  is used to colle
35779 63 74 20 74 65 72 6d 73 20 61 6e 64 20 62 6c 6f  ct terms and blo
3577a 63 6b 20 72 65 66 65 72 65 6e 63 65 73 20 69 6e  ck references in
3577b 74 6f 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e  to.** interior n
3577c 6f 64 65 73 20 69 6e 20 25 5f 73 65 67 6d 65 6e  odes in %_segmen
3577d 74 73 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74  ts.  See comment
3577e 61 72 79 20 61 74 20 74 6f 70 20 6f 66 20 66 69  ary at top of fi
3577f 6c 65 20 66 6f 72 0a 2a 2a 20 66 6f 72 6d 61 74  le for.** format
35780 2e 0a 2a 2f 0a 0a 2f 2a 20 48 6f 77 20 6c 61 72  ..*/../* How lar
35781 67 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  ge interior node
35782 73 20 63 61 6e 20 67 72 6f 77 2e 20 2a 2f 0a 23  s can grow. */.#
35783 64 65 66 69 6e 65 20 49 4e 54 45 52 49 4f 52 5f  define INTERIOR_
35784 4d 41 58 20 32 30 34 38 0a 0a 2f 2a 20 4d 69 6e  MAX 2048../* Min
35785 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74  imum number of t
35786 65 72 6d 73 20 70 65 72 20 69 6e 74 65 72 69 6f  erms per interio
35787 72 20 6e 6f 64 65 20 28 65 78 63 65 70 74 20 74  r node (except t
35788 68 65 20 72 6f 6f 74 29 2e 20 54 68 69 73 0a 2a  he root). This.*
35789 2a 20 70 72 65 76 65 6e 74 73 20 6c 61 72 67 65  * prevents large
3578a 20 74 65 72 6d 73 20 66 72 6f 6d 20 6d 61 6b 69   terms from maki
3578b 6e 67 20 74 68 65 20 74 72 65 65 20 74 6f 6f 20  ng the tree too 
3578c 73 6b 69 6e 6e 79 20 2d 20 6d 75 73 74 20 62 65  skinny - must be
3578d 20 3e 30 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74   >0.** so that t
3578e 68 65 20 74 72 65 65 20 61 6c 77 61 79 73 20 6d  he tree always m
3578f 61 6b 65 73 20 70 72 6f 67 72 65 73 73 2e 20 20  akes progress.  
35790 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 6d 69  Note that the mi
35791 6e 20 74 72 65 65 0a 2a 2a 20 66 61 6e 6f 75 74  n tree.** fanout
35792 20 77 69 6c 6c 20 62 65 20 49 4e 54 45 52 49 4f   will be INTERIO
35793 52 5f 4d 49 4e 5f 54 45 52 4d 53 2b 31 2e 0a 2a  R_MIN_TERMS+1..*
35794 2f 0a 23 64 65 66 69 6e 65 20 49 4e 54 45 52 49  /.#define INTERI
35795 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 20 37 0a 23  OR_MIN_TERMS 7.#
35796 69 66 20 49 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f  if INTERIOR_MIN_
35797 54 45 52 4d 53 3c 31 0a 23 20 65 72 72 6f 72 20  TERMS<1.# error 
35798 49 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52  INTERIOR_MIN_TER
35799 4d 53 20 6d 75 73 74 20 62 65 20 67 72 65 61 74  MS must be great
3579a 65 72 20 74 68 61 6e 20 30 2e 0a 23 65 6e 64 69  er than 0..#endi
3579b 66 0a 0a 2f 2a 20 52 4f 4f 54 5f 4d 41 58 20 63  f../* ROOT_MAX c
3579c 6f 6e 74 72 6f 6c 73 20 68 6f 77 20 6d 75 63 68  ontrols how much
3579d 20 64 61 74 61 20 69 73 20 73 74 6f 72 65 64 20   data is stored 
3579e 69 6e 6c 69 6e 65 20 69 6e 20 74 68 65 20 73 65  inline in the se
3579f 67 6d 65 6e 74 0a 2a 2a 20 64 69 72 65 63 74 6f  gment.** directo
357a0 72 79 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73  ry..*/./* TODO(s
357a1 68 65 73 73 29 20 50 75 73 68 20 52 4f 4f 54 5f  hess) Push ROOT_
357a2 4d 41 58 20 64 6f 77 6e 20 74 6f 20 77 68 6f 65  MAX down to whoe
357a3 76 65 72 20 69 73 20 77 72 69 74 69 6e 67 20 74  ver is writing t
357a4 68 69 6e 67 73 2e 20 20 49 74 27 73 0a 2a 2a 20  hings.  It's.** 
357a5 6f 6e 6c 79 20 68 65 72 65 20 73 6f 20 74 68 61  only here so tha
357a6 74 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72  t interiorWriter
357a7 52 6f 6f 74 49 6e 66 6f 28 29 20 61 6e 64 20 6c  RootInfo() and l
357a8 65 61 66 57 72 69 74 65 72 52 6f 6f 74 49 6e 66  eafWriterRootInf
357a9 6f 28 29 0a 2a 2a 20 63 61 6e 20 62 6f 74 68 20  o().** can both 
357aa 73 65 65 20 69 74 2c 20 62 75 74 20 69 66 20 74  see it, but if t
357ab 68 65 20 63 61 6c 6c 65 72 20 70 61 73 73 65 64  he caller passed
357ac 20 69 74 20 69 6e 2c 20 77 65 20 77 6f 75 6c 64   it in, we would
357ad 6e 27 74 20 65 76 65 6e 0a 2a 2a 20 6e 65 65 64  n't even.** need
357ae 20 61 20 64 65 66 69 6e 65 2e 0a 2a 2f 0a 23 64   a define..*/.#d
357af 65 66 69 6e 65 20 52 4f 4f 54 5f 4d 41 58 20 31  efine ROOT_MAX 1
357b0 30 32 34 0a 23 69 66 20 52 4f 4f 54 5f 4d 41 58  024.#if ROOT_MAX
357b1 3c 56 41 52 49 4e 54 5f 4d 41 58 2a 32 0a 23 20  <VARINT_MAX*2.# 
357b2 65 72 72 6f 72 20 52 4f 4f 54 5f 4d 41 58 20 6d  error ROOT_MAX m
357b3 75 73 74 20 68 61 76 65 20 65 6e 6f 75 67 68 20  ust have enough 
357b4 73 70 61 63 65 20 66 6f 72 20 61 20 68 65 61 64  space for a head
357b5 65 72 2e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 49  er..#endif../* I
357b6 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 73 74 6f  nteriorBlock sto
357b7 72 65 73 20 61 20 6c 69 6e 6b 65 64 2d 6c 69 73  res a linked-lis
357b8 74 20 6f 66 20 69 6e 74 65 72 69 6f 72 20 62 6c  t of interior bl
357b9 6f 63 6b 73 20 77 68 69 6c 65 20 61 20 6c 6f 77  ocks while a low
357ba 65 72 0a 2a 2a 20 6c 61 79 65 72 20 69 73 20 62  er.** layer is b
357bb 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64  eing constructed
357bc 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
357bd 75 63 74 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63  uct InteriorBloc
357be 6b 20 7b 0a 20 20 44 61 74 61 42 75 66 66 65 72  k {.  DataBuffer
357bf 20 74 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20   term;          
357c0 20 2f 2a 20 4c 65 66 74 6d 6f 73 74 20 74 65 72   /* Leftmost ter
357c1 6d 20 69 6e 20 62 6c 6f 63 6b 27 73 20 73 75 62  m in block's sub
357c2 74 72 65 65 2e 20 2a 2f 0a 20 20 44 61 74 61 42  tree. */.  DataB
357c3 75 66 66 65 72 20 64 61 74 61 3b 20 20 20 20 20  uffer data;     
357c4 20 20 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c        /* Accumul
357c5 61 74 65 64 20 64 61 74 61 20 66 6f 72 20 74 68  ated data for th
357c6 65 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 73 74  e block. */.  st
357c7 72 75 63 74 20 49 6e 74 65 72 69 6f 72 42 6c 6f  ruct InteriorBlo
357c8 63 6b 20 2a 6e 65 78 74 3b 0a 7d 20 49 6e 74 65  ck *next;.} Inte
357c9 72 69 6f 72 42 6c 6f 63 6b 3b 0a 0a 73 74 61 74  riorBlock;..stat
357ca 69 63 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b  ic InteriorBlock
357cb 20 2a 69 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 4e   *interiorBlockN
357cc 65 77 28 69 6e 74 20 69 48 65 69 67 68 74 2c 20  ew(int iHeight, 
357cd 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 43 68  sqlite_int64 iCh
357ce 69 6c 64 42 6c 6f 63 6b 2c 0a 20 20 20 20 20 20  ildBlock,.      
357cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
357d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
357d1 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65   const char *pTe
357d2 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 29 7b 0a  rm, int nTerm){.
357d3 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20    InteriorBlock 
357d4 2a 62 6c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33  *block = sqlite3
357d5 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 49  _malloc(sizeof(I
357d6 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 29 29 3b 0a  nteriorBlock));.
357d7 20 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f    char c[VARINT_
357d8 4d 41 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b  MAX+VARINT_MAX];
357d9 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 69 66 28  .  int n;..  if(
357da 20 62 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 6d 65   block ){.    me
357db 6d 73 65 74 28 62 6c 6f 63 6b 2c 20 30 2c 20 73  mset(block, 0, s
357dc 69 7a 65 6f 66 28 2a 62 6c 6f 63 6b 29 29 3b 0a  izeof(*block));.
357dd 20 20 20 20 64 61 74 61 42 75 66 66 65 72 49 6e      dataBufferIn
357de 69 74 28 26 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2c  it(&block->term,
357df 20 30 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66   0);.    dataBuf
357e0 66 65 72 52 65 70 6c 61 63 65 28 26 62 6c 6f 63  ferReplace(&bloc
357e1 6b 2d 3e 74 65 72 6d 2c 20 70 54 65 72 6d 2c 20  k->term, pTerm, 
357e2 6e 54 65 72 6d 29 3b 0a 0a 20 20 20 20 6e 20 3d  nTerm);..    n =
357e3 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
357e4 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20 20 20  , iHeight);.    
357e5 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69  n += fts3PutVari
357e6 6e 74 28 63 2b 6e 2c 20 69 43 68 69 6c 64 42 6c  nt(c+n, iChildBl
357e7 6f 63 6b 29 3b 0a 20 20 20 20 64 61 74 61 42 75  ock);.    dataBu
357e8 66 66 65 72 49 6e 69 74 28 26 62 6c 6f 63 6b 2d  fferInit(&block-
357e9 3e 64 61 74 61 2c 20 49 4e 54 45 52 49 4f 52 5f  >data, INTERIOR_
357ea 4d 41 58 29 3b 0a 20 20 20 20 64 61 74 61 42 75  MAX);.    dataBu
357eb 66 66 65 72 52 65 70 6c 61 63 65 28 26 62 6c 6f  fferReplace(&blo
357ec 63 6b 2d 3e 64 61 74 61 2c 20 63 2c 20 6e 29 3b  ck->data, c, n);
357ed 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 62 6c  .  }.  return bl
357ee 6f 63 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  ock;.}..#ifndef 
357ef 4e 44 45 42 55 47 0a 2f 2a 20 56 65 72 69 66 79  NDEBUG./* Verify
357f0 20 74 68 61 74 20 74 68 65 20 64 61 74 61 20 69   that the data i
357f1 73 20 72 65 61 64 61 62 6c 65 20 61 73 20 61 6e  s readable as an
357f2 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2e 20   interior node. 
357f3 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  */.static void i
357f4 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 56 61 6c 69  nteriorBlockVali
357f5 64 61 74 65 28 49 6e 74 65 72 69 6f 72 42 6c 6f  date(InteriorBlo
357f6 63 6b 20 2a 70 42 6c 6f 63 6b 29 7b 0a 20 20 63  ck *pBlock){.  c
357f7 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61  onst char *pData
357f8 20 3d 20 70 42 6c 6f 63 6b 2d 3e 64 61 74 61 2e   = pBlock->data.
357f9 70 44 61 74 61 3b 0a 20 20 69 6e 74 20 6e 44 61  pData;.  int nDa
357fa 74 61 20 3d 20 70 42 6c 6f 63 6b 2d 3e 64 61 74  ta = pBlock->dat
357fb 61 2e 6e 44 61 74 61 3b 0a 20 20 69 6e 74 20 6e  a.nData;.  int n
357fc 2c 20 69 44 75 6d 6d 79 3b 0a 20 20 73 71 6c 69  , iDummy;.  sqli
357fd 74 65 5f 69 6e 74 36 34 20 69 42 6c 6f 63 6b 69  te_int64 iBlocki
357fe 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 44  d;..  assert( nD
357ff 61 74 61 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  ata>0 );.  asser
35800 74 28 20 70 44 61 74 61 21 3d 30 20 29 3b 0a 20  t( pData!=0 );. 
35801 20 61 73 73 65 72 74 28 20 70 44 61 74 61 2b 6e   assert( pData+n
35802 44 61 74 61 3e 70 44 61 74 61 20 29 3b 0a 0a 20  Data>pData );.. 
35803 20 2f 2a 20 4d 75 73 74 20 6c 65 61 64 20 77 69   /* Must lead wi
35804 74 68 20 68 65 69 67 68 74 20 6f 66 20 6e 6f 64  th height of nod
35805 65 20 61 73 20 61 20 76 61 72 69 6e 74 28 6e 29  e as a varint(n)
35806 2c 20 6e 3e 30 20 2a 2f 0a 20 20 6e 20 3d 20 66  , n>0 */.  n = f
35807 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
35808 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a  Data, &iDummy);.
35809 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b    assert( n>0 );
3580a 0a 20 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d  .  assert( iDumm
3580b 79 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  y>0 );.  assert(
3580c 20 6e 3c 6e 44 61 74 61 20 29 3b 0a 20 20 70 44   n<nData );.  pD
3580d 61 74 61 20 2b 3d 20 6e 3b 0a 20 20 6e 44 61 74  ata += n;.  nDat
3580e 61 20 2d 3d 20 6e 3b 0a 0a 20 20 2f 2a 20 4d 75  a -= n;..  /* Mu
3580f 73 74 20 63 6f 6e 74 61 69 6e 20 69 42 6c 6f 63  st contain iBloc
35810 6b 69 64 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74  kid. */.  n = ft
35811 73 33 47 65 74 56 61 72 69 6e 74 28 70 44 61 74  s3GetVarint(pDat
35812 61 2c 20 26 69 42 6c 6f 63 6b 69 64 29 3b 0a 20  a, &iBlockid);. 
35813 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a   assert( n>0 );.
35814 20 20 61 73 73 65 72 74 28 20 6e 3c 3d 6e 44 61    assert( n<=nDa
35815 74 61 20 29 3b 0a 20 20 70 44 61 74 61 20 2b 3d  ta );.  pData +=
35816 20 6e 3b 0a 20 20 6e 44 61 74 61 20 2d 3d 20 6e   n;.  nData -= n
35817 3b 0a 0a 20 20 2f 2a 20 5a 65 72 6f 20 6f 72 20  ;..  /* Zero or 
35818 6d 6f 72 65 20 74 65 72 6d 73 20 6f 66 20 70 6f  more terms of po
35819 73 69 74 69 76 65 20 6c 65 6e 67 74 68 20 2a 2f  sitive length */
3581a 0a 20 20 69 66 28 20 6e 44 61 74 61 21 3d 30 20  .  if( nData!=0 
3581b 29 7b 0a 20 20 20 20 2f 2a 20 46 69 72 73 74 20  ){.    /* First 
3581c 74 65 72 6d 20 69 73 20 6e 6f 74 20 64 65 6c 74  term is not delt
3581d 61 2d 65 6e 63 6f 64 65 64 2e 20 2a 2f 0a 20 20  a-encoded. */.  
3581e 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72    n = fts3GetVar
3581f 69 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44  int32(pData, &iD
35820 75 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73 65 72  ummy);.    asser
35821 74 28 20 6e 3e 30 20 29 3b 0a 20 20 20 20 61 73  t( n>0 );.    as
35822 73 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29  sert( iDummy>0 )
35823 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b  ;.    assert( n+
35824 69 44 75 6d 6d 79 3e 30 29 3b 0a 20 20 20 20 61  iDummy>0);.    a
35825 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c  ssert( n+iDummy<
35826 3d 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 70 44  =nData );.    pD
35827 61 74 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b  ata += n+iDummy;
35828 0a 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b  .    nData -= n+
35829 69 44 75 6d 6d 79 3b 0a 0a 20 20 20 20 2f 2a 20  iDummy;..    /* 
3582a 46 6f 6c 6c 6f 77 69 6e 67 20 74 65 72 6d 73 20  Following terms 
3582b 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 2e 20 2a  delta-encoded. *
3582c 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 44 61  /.    while( nDa
3582d 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f  ta!=0 ){.      /
3582e 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 68 61 72  * Length of shar
3582f 65 64 20 70 72 65 66 69 78 2e 20 2a 2f 0a 20 20  ed prefix. */.  
35830 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56      n = fts3GetV
35831 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c 20 26  arint32(pData, &
35832 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 61  iDummy);.      a
35833 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20  ssert( n>0 );.  
35834 20 20 20 20 61 73 73 65 72 74 28 20 69 44 75 6d      assert( iDum
35835 6d 79 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 61  my>=0 );.      a
35836 73 73 65 72 74 28 20 6e 3c 6e 44 61 74 61 20 29  ssert( n<nData )
35837 3b 0a 20 20 20 20 20 20 70 44 61 74 61 20 2b 3d  ;.      pData +=
35838 20 6e 3b 0a 20 20 20 20 20 20 6e 44 61 74 61 20   n;.      nData 
35839 2d 3d 20 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  -= n;..      /* 
3583a 4c 65 6e 67 74 68 20 61 6e 64 20 64 61 74 61 20  Length and data 
3583b 6f 66 20 64 69 73 74 69 6e 63 74 20 73 75 66 66  of distinct suff
3583c 69 78 2e 20 2a 2f 0a 20 20 20 20 20 20 6e 20 3d  ix. */.      n =
3583d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
3583e 28 70 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29  (pData, &iDummy)
3583f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
35840 6e 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  n>0 );.      ass
35841 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b  ert( iDummy>0 );
35842 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e  .      assert( n
35843 2b 69 44 75 6d 6d 79 3e 30 29 3b 0a 20 20 20 20  +iDummy>0);.    
35844 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d    assert( n+iDum
35845 6d 79 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20 20  my<=nData );.   
35846 20 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44     pData += n+iD
35847 75 6d 6d 79 3b 0a 20 20 20 20 20 20 6e 44 61 74  ummy;.      nDat
35848 61 20 2d 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20  a -= n+iDummy;. 
35849 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 64 65 66 69     }.  }.}.#defi
3584a 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f  ne ASSERT_VALID_
3584b 49 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 78  INTERIOR_BLOCK(x
3584c 29 20 69 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 56  ) interiorBlockV
3584d 61 6c 69 64 61 74 65 28 78 29 0a 23 65 6c 73 65  alidate(x).#else
3584e 0a 23 64 65 66 69 6e 65 20 41 53 53 45 52 54 5f  .#define ASSERT_
3584f 56 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42  VALID_INTERIOR_B
35850 4c 4f 43 4b 28 78 29 20 61 73 73 65 72 74 28 20  LOCK(x) assert( 
35851 31 20 29 0a 23 65 6e 64 69 66 0a 0a 74 79 70 65  1 ).#endif..type
35852 64 65 66 20 73 74 72 75 63 74 20 49 6e 74 65 72  def struct Inter
35853 69 6f 72 57 72 69 74 65 72 20 7b 0a 20 20 69 6e  iorWriter {.  in
35854 74 20 69 48 65 69 67 68 74 3b 20 20 20 20 20 20  t iHeight;      
35855 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35856 66 72 6f 6d 20 30 20 61 74 20 6c 65 61 76 65 73  from 0 at leaves
35857 2e 20 2a 2f 0a 20 20 49 6e 74 65 72 69 6f 72 42  . */.  InteriorB
35858 6c 6f 63 6b 20 2a 66 69 72 73 74 2c 20 2a 6c 61  lock *first, *la
35859 73 74 3b 0a 20 20 73 74 72 75 63 74 20 49 6e 74  st;.  struct Int
3585a 65 72 69 6f 72 57 72 69 74 65 72 20 2a 70 61 72  eriorWriter *par
3585b 65 6e 74 57 72 69 74 65 72 3b 0a 0a 20 20 44 61  entWriter;..  Da
3585c 74 61 42 75 66 66 65 72 20 74 65 72 6d 3b 20 20  taBuffer term;  
3585d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3585e 4c 61 73 74 20 74 65 72 6d 20 77 72 69 74 74 65  Last term writte
3585f 6e 20 74 6f 20 62 6c 6f 63 6b 20 22 6c 61 73 74  n to block "last
35860 22 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69  ". */.  sqlite_i
35861 6e 74 36 34 20 69 4f 70 65 6e 69 6e 67 43 68 69  nt64 iOpeningChi
35862 6c 64 42 6c 6f 63 6b 3b 20 2f 2a 20 46 69 72 73  ldBlock; /* Firs
35863 74 20 63 68 69 6c 64 20 62 6c 6f 63 6b 20 69 6e  t child block in
35864 20 62 6c 6f 63 6b 20 22 6c 61 73 74 22 2e 20 2a   block "last". *
35865 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  /.#ifndef NDEBUG
35866 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
35867 69 4c 61 73 74 43 68 69 6c 64 42 6c 6f 63 6b 3b  iLastChildBlock;
35868 20 20 2f 2a 20 66 6f 72 20 63 6f 6e 73 69 73 74    /* for consist
35869 65 6e 63 79 20 63 68 65 63 6b 73 2e 20 2a 2f 0a  ency checks. */.
3586a 23 65 6e 64 69 66 0a 7d 20 49 6e 74 65 72 69 6f  #endif.} Interio
3586b 72 57 72 69 74 65 72 3b 0a 0a 2f 2a 20 49 6e 69  rWriter;../* Ini
3586c 74 69 61 6c 69 7a 65 20 61 6e 20 69 6e 74 65 72  tialize an inter
3586d 69 6f 72 20 6e 6f 64 65 20 77 68 65 72 65 20 70  ior node where p
3586e 54 65 72 6d 5b 6e 54 65 72 6d 5d 20 6d 61 72 6b  Term[nTerm] mark
3586f 73 20 74 68 65 20 6c 65 66 74 6d 6f 73 74 0a 2a  s the leftmost.*
35870 2a 20 74 65 72 6d 20 69 6e 20 74 68 65 20 74 72  * term in the tr
35871 65 65 2e 20 20 69 43 68 69 6c 64 42 6c 6f 63 6b  ee.  iChildBlock
35872 20 69 73 20 74 68 65 20 6c 65 66 74 6d 6f 73 74   is the leftmost
35873 20 63 68 69 6c 64 20 62 6c 6f 63 6b 20 61 74 20   child block at 
35874 74 68 65 0a 2a 2a 20 6e 65 78 74 20 6c 65 76 65  the.** next leve
35875 6c 20 64 6f 77 6e 20 74 68 65 20 74 72 65 65 2e  l down the tree.
35876 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
35877 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 49 6e  interiorWriterIn
35878 69 74 28 69 6e 74 20 69 48 65 69 67 68 74 2c 20  it(int iHeight, 
35879 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72  const char *pTer
3587a 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 0a 20 20  m, int nTerm,.  
3587b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3587c 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
3587d 69 74 65 5f 69 6e 74 36 34 20 69 43 68 69 6c 64  ite_int64 iChild
3587e 42 6c 6f 63 6b 2c 0a 20 20 20 20 20 20 20 20 20  Block,.         
3587f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35880 20 20 20 20 20 20 49 6e 74 65 72 69 6f 72 57 72        InteriorWr
35881 69 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a  iter *pWriter){.
35882 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20    InteriorBlock 
35883 2a 62 6c 6f 63 6b 3b 0a 20 20 61 73 73 65 72 74  *block;.  assert
35884 28 20 69 48 65 69 67 68 74 3e 30 20 29 3b 0a 20  ( iHeight>0 );. 
35885 20 43 4c 45 41 52 28 70 57 72 69 74 65 72 29 3b   CLEAR(pWriter);
35886 0a 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 48 65  ..  pWriter->iHe
35887 69 67 68 74 20 3d 20 69 48 65 69 67 68 74 3b 0a  ight = iHeight;.
35888 20 20 70 57 72 69 74 65 72 2d 3e 69 4f 70 65 6e    pWriter->iOpen
35889 69 6e 67 43 68 69 6c 64 42 6c 6f 63 6b 20 3d 20  ingChildBlock = 
3588a 69 43 68 69 6c 64 42 6c 6f 63 6b 3b 0a 23 69 66  iChildBlock;.#if
3588b 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 70 57  ndef NDEBUG.  pW
3588c 72 69 74 65 72 2d 3e 69 4c 61 73 74 43 68 69 6c  riter->iLastChil
3588d 64 42 6c 6f 63 6b 20 3d 20 69 43 68 69 6c 64 42  dBlock = iChildB
3588e 6c 6f 63 6b 3b 0a 23 65 6e 64 69 66 0a 20 20 62  lock;.#endif.  b
3588f 6c 6f 63 6b 20 3d 20 69 6e 74 65 72 69 6f 72 42  lock = interiorB
35890 6c 6f 63 6b 4e 65 77 28 69 48 65 69 67 68 74 2c  lockNew(iHeight,
35891 20 69 43 68 69 6c 64 42 6c 6f 63 6b 2c 20 70 54   iChildBlock, pT
35892 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 70  erm, nTerm);.  p
35893 57 72 69 74 65 72 2d 3e 6c 61 73 74 20 3d 20 70  Writer->last = p
35894 57 72 69 74 65 72 2d 3e 66 69 72 73 74 20 3d 20  Writer->first = 
35895 62 6c 6f 63 6b 3b 0a 20 20 41 53 53 45 52 54 5f  block;.  ASSERT_
35896 56 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42  VALID_INTERIOR_B
35897 4c 4f 43 4b 28 70 57 72 69 74 65 72 2d 3e 6c 61  LOCK(pWriter->la
35898 73 74 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  st);.  dataBuffe
35899 72 49 6e 69 74 28 26 70 57 72 69 74 65 72 2d 3e  rInit(&pWriter->
3589a 74 65 72 6d 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 20  term, 0);.}../* 
3589b 41 70 70 65 6e 64 20 74 68 65 20 63 68 69 6c 64  Append the child
3589c 20 6e 6f 64 65 20 72 6f 6f 74 65 64 20 61 74 20   node rooted at 
3589d 69 43 68 69 6c 64 42 6c 6f 63 6b 20 74 6f 20 74  iChildBlock to t
3589e 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  he interior node
3589f 2c 0a 2a 2a 20 77 69 74 68 20 70 54 65 72 6d 5b  ,.** with pTerm[
358a0 6e 54 65 72 6d 5d 20 61 73 20 74 68 65 20 6c 65  nTerm] as the le
358a1 66 74 6d 6f 73 74 20 74 65 72 6d 20 69 6e 20 69  ftmost term in i
358a2 43 68 69 6c 64 42 6c 6f 63 6b 27 73 20 73 75 62  ChildBlock's sub
358a3 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tree..*/.static 
358a4 76 6f 69 64 20 69 6e 74 65 72 69 6f 72 57 72 69  void interiorWri
358a5 74 65 72 41 70 70 65 6e 64 28 49 6e 74 65 72 69  terAppend(Interi
358a6 6f 72 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  orWriter *pWrite
358a7 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
358a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
358a9 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
358aa 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d  pTerm, int nTerm
358ab 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
358ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
358ad 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
358ae 69 43 68 69 6c 64 42 6c 6f 63 6b 29 7b 0a 20 20  iChildBlock){.  
358af 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41  char c[VARINT_MA
358b0 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20  X+VARINT_MAX];. 
358b1 20 69 6e 74 20 6e 2c 20 6e 50 72 65 66 69 78 20   int n, nPrefix 
358b2 3d 20 30 3b 0a 0a 20 20 41 53 53 45 52 54 5f 56  = 0;..  ASSERT_V
358b3 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42 4c  ALID_INTERIOR_BL
358b4 4f 43 4b 28 70 57 72 69 74 65 72 2d 3e 6c 61 73  OCK(pWriter->las
358b5 74 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 69  t);..  /* The fi
358b6 72 73 74 20 74 65 72 6d 20 77 72 69 74 74 65 6e  rst term written
358b7 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 72 69 6f   into an interio
358b8 72 20 6e 6f 64 65 20 69 73 20 61 63 74 75 61 6c  r node is actual
358b9 6c 79 0a 20 20 2a 2a 20 61 73 73 6f 63 69 61 74  ly.  ** associat
358ba 65 64 20 77 69 74 68 20 74 68 65 20 73 65 63 6f  ed with the seco
358bb 6e 64 20 63 68 69 6c 64 20 61 64 64 65 64 20 28  nd child added (
358bc 74 68 65 20 66 69 72 73 74 20 63 68 69 6c 64 20  the first child 
358bd 77 61 73 20 61 64 64 65 64 0a 20 20 2a 2a 20 69  was added.  ** i
358be 6e 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72  n interiorWriter
358bf 49 6e 69 74 2c 20 6f 72 20 69 6e 20 74 68 65 20  Init, or in the 
358c0 69 66 20 63 6c 61 75 73 65 20 61 74 20 74 68 65  if clause at the
358c1 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 69 73 0a   bottom of this.
358c2 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 29 2e 20    ** function). 
358c3 20 54 68 61 74 20 74 65 72 6d 20 67 65 74 73 20   That term gets 
358c4 65 6e 63 6f 64 65 64 20 73 74 72 61 69 67 68 74  encoded straight
358c5 20 75 70 2c 20 77 69 74 68 20 6e 50 72 65 66 69   up, with nPrefi
358c6 78 20 6c 65 66 74 0a 20 20 2a 2a 20 61 74 20 30  x left.  ** at 0
358c7 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 57 72  ..  */.  if( pWr
358c8 69 74 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61  iter->term.nData
358c9 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 66  ==0 ){.    n = f
358ca 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20  ts3PutVarint(c, 
358cb 6e 54 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b  nTerm);.  }else{
358cc 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 50 72 65  .    while( nPre
358cd 66 69 78 3c 70 57 72 69 74 65 72 2d 3e 74 65 72  fix<pWriter->ter
358ce 6d 2e 6e 44 61 74 61 20 26 26 0a 20 20 20 20 20  m.nData &&.     
358cf 20 20 20 20 20 20 70 54 65 72 6d 5b 6e 50 72 65        pTerm[nPre
358d0 66 69 78 5d 3d 3d 70 57 72 69 74 65 72 2d 3e 74  fix]==pWriter->t
358d1 65 72 6d 2e 70 44 61 74 61 5b 6e 50 72 65 66 69  erm.pData[nPrefi
358d2 78 5d 20 29 7b 0a 20 20 20 20 20 20 6e 50 72 65  x] ){.      nPre
358d3 66 69 78 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20  fix++;.    }..  
358d4 20 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72    n = fts3PutVar
358d5 69 6e 74 28 63 2c 20 6e 50 72 65 66 69 78 29 3b  int(c, nPrefix);
358d6 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75  .    n += fts3Pu
358d7 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 6e 54 65  tVarint(c+n, nTe
358d8 72 6d 2d 6e 50 72 65 66 69 78 29 3b 0a 20 20 7d  rm-nPrefix);.  }
358d9 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
358da 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 4c 61 73  .  pWriter->iLas
358db 74 43 68 69 6c 64 42 6c 6f 63 6b 2b 2b 3b 0a 23  tChildBlock++;.#
358dc 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28 20  endif.  assert( 
358dd 70 57 72 69 74 65 72 2d 3e 69 4c 61 73 74 43 68  pWriter->iLastCh
358de 69 6c 64 42 6c 6f 63 6b 3d 3d 69 43 68 69 6c 64  ildBlock==iChild
358df 42 6c 6f 63 6b 20 29 3b 0a 0a 20 20 2f 2a 20 4f  Block );..  /* O
358e0 76 65 72 66 6c 6f 77 20 74 6f 20 61 20 6e 65 77  verflow to a new
358e1 20 62 6c 6f 63 6b 20 69 66 20 74 68 65 20 6e 65   block if the ne
358e2 77 20 74 65 72 6d 20 6d 61 6b 65 73 20 74 68 65  w term makes the
358e3 20 63 75 72 72 65 6e 74 20 62 6c 6f 63 6b 0a 20   current block. 
358e4 20 2a 2a 20 74 6f 6f 20 62 69 67 2c 20 61 6e 64   ** too big, and
358e5 20 74 68 65 20 63 75 72 72 65 6e 74 20 62 6c 6f   the current blo
358e6 63 6b 20 61 6c 72 65 61 64 79 20 68 61 73 20 65  ck already has e
358e7 6e 6f 75 67 68 20 74 65 72 6d 73 2e 0a 20 20 2a  nough terms..  *
358e8 2f 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d  /.  if( pWriter-
358e9 3e 6c 61 73 74 2d 3e 64 61 74 61 2e 6e 44 61 74  >last->data.nDat
358ea 61 2b 6e 2b 6e 54 65 72 6d 2d 6e 50 72 65 66 69  a+n+nTerm-nPrefi
358eb 78 3e 49 4e 54 45 52 49 4f 52 5f 4d 41 58 20 26  x>INTERIOR_MAX &
358ec 26 0a 20 20 20 20 20 20 69 43 68 69 6c 64 42 6c  &.      iChildBl
358ed 6f 63 6b 2d 70 57 72 69 74 65 72 2d 3e 69 4f 70  ock-pWriter->iOp
358ee 65 6e 69 6e 67 43 68 69 6c 64 42 6c 6f 63 6b 3e  eningChildBlock>
358ef 49 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52  INTERIOR_MIN_TER
358f0 4d 53 20 29 7b 0a 20 20 20 20 70 57 72 69 74 65  MS ){.    pWrite
358f1 72 2d 3e 6c 61 73 74 2d 3e 6e 65 78 74 20 3d 20  r->last->next = 
358f2 69 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 4e 65 77  interiorBlockNew
358f3 28 70 57 72 69 74 65 72 2d 3e 69 48 65 69 67 68  (pWriter->iHeigh
358f4 74 2c 20 69 43 68 69 6c 64 42 6c 6f 63 6b 2c 0a  t, iChildBlock,.
358f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
358f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
358f7 20 20 20 20 20 20 20 20 20 20 20 70 54 65 72 6d             pTerm
358f8 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20 20 70 57  , nTerm);.    pW
358f9 72 69 74 65 72 2d 3e 6c 61 73 74 20 3d 20 70 57  riter->last = pW
358fa 72 69 74 65 72 2d 3e 6c 61 73 74 2d 3e 6e 65 78  riter->last->nex
358fb 74 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  t;.    pWriter->
358fc 69 4f 70 65 6e 69 6e 67 43 68 69 6c 64 42 6c 6f  iOpeningChildBlo
358fd 63 6b 20 3d 20 69 43 68 69 6c 64 42 6c 6f 63 6b  ck = iChildBlock
358fe 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  ;.    dataBuffer
358ff 52 65 73 65 74 28 26 70 57 72 69 74 65 72 2d 3e  Reset(&pWriter->
35900 74 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  term);.  }else{.
35901 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70      dataBufferAp
35902 70 65 6e 64 32 28 26 70 57 72 69 74 65 72 2d 3e  pend2(&pWriter->
35903 6c 61 73 74 2d 3e 64 61 74 61 2c 20 63 2c 20 6e  last->data, c, n
35904 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
35905 20 20 20 20 20 20 20 20 70 54 65 72 6d 2b 6e 50          pTerm+nP
35906 72 65 66 69 78 2c 20 6e 54 65 72 6d 2d 6e 50 72  refix, nTerm-nPr
35907 65 66 69 78 29 3b 0a 20 20 20 20 64 61 74 61 42  efix);.    dataB
35908 75 66 66 65 72 52 65 70 6c 61 63 65 28 26 70 57  ufferReplace(&pW
35909 72 69 74 65 72 2d 3e 74 65 72 6d 2c 20 70 54 65  riter->term, pTe
3590a 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 7d 0a  rm, nTerm);.  }.
3590b 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 49    ASSERT_VALID_I
3590c 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 70 57  NTERIOR_BLOCK(pW
3590d 72 69 74 65 72 2d 3e 6c 61 73 74 29 3b 0a 7d 0a  riter->last);.}.
3590e 0a 2f 2a 20 46 72 65 65 20 74 68 65 20 73 70 61  ./* Free the spa
3590f 63 65 20 75 73 65 64 20 62 79 20 70 57 72 69 74  ce used by pWrit
35910 65 72 2c 20 69 6e 63 6c 75 64 69 6e 67 20 74 68  er, including th
35911 65 20 6c 69 6e 6b 65 64 2d 6c 69 73 74 20 6f 66  e linked-list of
35912 0a 2a 2a 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63  .** InteriorBloc
35913 6b 73 2c 20 61 6e 64 20 70 61 72 65 6e 74 57 72  ks, and parentWr
35914 69 74 65 72 2c 20 69 66 20 70 72 65 73 65 6e 74  iter, if present
35915 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
35916 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 44 65  interiorWriterDe
35917 73 74 72 6f 79 28 49 6e 74 65 72 69 6f 72 57 72  stroy(InteriorWr
35918 69 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a  iter *pWriter){.
35919 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20    InteriorBlock 
3591a 2a 62 6c 6f 63 6b 20 3d 20 70 57 72 69 74 65 72  *block = pWriter
3591b 2d 3e 66 69 72 73 74 3b 0a 0a 20 20 77 68 69 6c  ->first;..  whil
3591c 65 28 20 62 6c 6f 63 6b 21 3d 4e 55 4c 4c 20 29  e( block!=NULL )
3591d 7b 0a 20 20 20 20 49 6e 74 65 72 69 6f 72 42 6c  {.    InteriorBl
3591e 6f 63 6b 20 2a 62 20 3d 20 62 6c 6f 63 6b 3b 0a  ock *b = block;.
3591f 20 20 20 20 62 6c 6f 63 6b 20 3d 20 62 6c 6f 63      block = bloc
35920 6b 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 64 61 74  k->next;.    dat
35921 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  aBufferDestroy(&
35922 62 2d 3e 74 65 72 6d 29 3b 0a 20 20 20 20 64 61  b->term);.    da
35923 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
35924 26 62 2d 3e 64 61 74 61 29 3b 0a 20 20 20 20 73  &b->data);.    s
35925 71 6c 69 74 65 33 5f 66 72 65 65 28 62 29 3b 0a  qlite3_free(b);.
35926 20 20 7d 0a 20 20 69 66 28 20 70 57 72 69 74 65    }.  if( pWrite
35927 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 21  r->parentWriter!
35928 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 69 6e 74  =NULL ){.    int
35929 65 72 69 6f 72 57 72 69 74 65 72 44 65 73 74 72  eriorWriterDestr
3592a 6f 79 28 70 57 72 69 74 65 72 2d 3e 70 61 72 65  oy(pWriter->pare
3592b 6e 74 57 72 69 74 65 72 29 3b 0a 20 20 20 20 73  ntWriter);.    s
3592c 71 6c 69 74 65 33 5f 66 72 65 65 28 70 57 72 69  qlite3_free(pWri
3592d 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65  ter->parentWrite
3592e 72 29 3b 0a 20 20 7d 0a 20 20 64 61 74 61 42 75  r);.  }.  dataBu
3592f 66 66 65 72 44 65 73 74 72 6f 79 28 26 70 57 72  fferDestroy(&pWr
35930 69 74 65 72 2d 3e 74 65 72 6d 29 3b 0a 20 20 53  iter->term);.  S
35931 43 52 41 4d 42 4c 45 28 70 57 72 69 74 65 72 29  CRAMBLE(pWriter)
35932 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
35933 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 49 66 20 70  E_OK;.}../* If p
35934 57 72 69 74 65 72 20 63 61 6e 20 66 69 74 20 65  Writer can fit e
35935 6e 74 69 72 65 6c 79 20 69 6e 20 52 4f 4f 54 5f  ntirely in ROOT_
35936 4d 41 58 2c 20 72 65 74 75 72 6e 20 69 74 20 61  MAX, return it a
35937 73 20 74 68 65 20 72 6f 6f 74 20 69 6e 66 6f 0a  s the root info.
35938 2a 2a 20 64 69 72 65 63 74 6c 79 2c 20 6c 65 61  ** directly, lea
35939 76 69 6e 67 20 2a 70 69 45 6e 64 42 6c 6f 63 6b  ving *piEndBlock
3593a 69 64 20 75 6e 63 68 61 6e 67 65 64 2e 20 20 4f  id unchanged.  O
3593b 74 68 65 72 77 69 73 65 2c 20 66 6c 75 73 68 0a  therwise, flush.
3593c 2a 2a 20 70 57 72 69 74 65 72 20 74 6f 20 25 5f  ** pWriter to %_
3593d 73 65 67 6d 65 6e 74 73 2c 20 62 75 69 6c 64 69  segments, buildi
3593e 6e 67 20 61 20 6e 65 77 20 6c 61 79 65 72 20 6f  ng a new layer o
3593f 66 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73  f interior nodes
35940 2c 20 61 6e 64 0a 2a 2a 20 72 65 63 75 72 73 69  , and.** recursi
35941 76 65 6c 79 20 61 73 6b 20 66 6f 72 20 74 68 65  vely ask for the
35942 69 72 20 72 6f 6f 74 20 69 6e 74 6f 2e 0a 2a 2f  ir root into..*/
35943 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 74 65  .static int inte
35944 72 69 6f 72 57 72 69 74 65 72 52 6f 6f 74 49 6e  riorWriterRootIn
35945 66 6f 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  fo(fulltext_vtab
35946 20 2a 76 2c 20 49 6e 74 65 72 69 6f 72 57 72 69   *v, InteriorWri
35947 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20  ter *pWriter,.  
35948 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35949 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3594a 63 68 61 72 20 2a 2a 70 70 52 6f 6f 74 49 6e 66  char **ppRootInf
3594b 6f 2c 20 69 6e 74 20 2a 70 6e 52 6f 6f 74 49 6e  o, int *pnRootIn
3594c 66 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  fo,.            
3594d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3594e 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74        sqlite_int
3594f 36 34 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64  64 *piEndBlockid
35950 29 7b 0a 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f  ){.  InteriorBlo
35951 63 6b 20 2a 62 6c 6f 63 6b 20 3d 20 70 57 72 69  ck *block = pWri
35952 74 65 72 2d 3e 66 69 72 73 74 3b 0a 20 20 73 71  ter->first;.  sq
35953 6c 69 74 65 5f 69 6e 74 36 34 20 69 42 6c 6f 63  lite_int64 iBloc
35954 6b 69 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  kid = 0;.  int r
35955 63 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63  c;..  /* If we c
35956 61 6e 20 66 69 74 20 74 68 65 20 73 65 67 6d 65  an fit the segme
35957 6e 74 20 69 6e 6c 69 6e 65 20 2a 2f 0a 20 20 69  nt inline */.  i
35958 66 28 20 62 6c 6f 63 6b 3d 3d 70 57 72 69 74 65  f( block==pWrite
35959 72 2d 3e 6c 61 73 74 20 26 26 20 62 6c 6f 63 6b  r->last && block
3595a 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3c 52 4f 4f  ->data.nData<ROO
3595b 54 5f 4d 41 58 20 29 7b 0a 20 20 20 20 2a 70 70  T_MAX ){.    *pp
3595c 52 6f 6f 74 49 6e 66 6f 20 3d 20 62 6c 6f 63 6b  RootInfo = block
3595d 2d 3e 64 61 74 61 2e 70 44 61 74 61 3b 0a 20 20  ->data.pData;.  
3595e 20 20 2a 70 6e 52 6f 6f 74 49 6e 66 6f 20 3d 20    *pnRootInfo = 
3595f 62 6c 6f 63 6b 2d 3e 64 61 74 61 2e 6e 44 61 74  block->data.nDat
35960 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  a;.    return SQ
35961 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
35962 2f 2a 20 46 6c 75 73 68 20 74 68 65 20 66 69 72  /* Flush the fir
35963 73 74 20 62 6c 6f 63 6b 20 74 6f 20 25 5f 73 65  st block to %_se
35964 67 6d 65 6e 74 73 2c 20 61 6e 64 20 63 72 65 61  gments, and crea
35965 74 65 20 61 20 6e 65 77 20 6c 65 76 65 6c 20 6f  te a new level o
35966 66 0a 20 20 2a 2a 20 69 6e 74 65 72 69 6f 72 20  f.  ** interior 
35967 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 41 53 53  node..  */.  ASS
35968 45 52 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52 49  ERT_VALID_INTERI
35969 4f 52 5f 42 4c 4f 43 4b 28 62 6c 6f 63 6b 29 3b  OR_BLOCK(block);
3596a 0a 20 20 72 63 20 3d 20 62 6c 6f 63 6b 5f 69 6e  .  rc = block_in
3596b 73 65 72 74 28 76 2c 20 62 6c 6f 63 6b 2d 3e 64  sert(v, block->d
3596c 61 74 61 2e 70 44 61 74 61 2c 20 62 6c 6f 63 6b  ata.pData, block
3596d 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2c 20 26 69  ->data.nData, &i
3596e 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20  Blockid);.  if( 
3596f 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
35970 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 2a 70 69  return rc;.  *pi
35971 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 69 42 6c  EndBlockid = iBl
35972 6f 63 6b 69 64 3b 0a 0a 20 20 70 57 72 69 74 65  ockid;..  pWrite
35973 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 20  r->parentWriter 
35974 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
35975 28 73 69 7a 65 6f 66 28 2a 70 57 72 69 74 65 72  (sizeof(*pWriter
35976 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 29 29  ->parentWriter))
35977 3b 0a 20 20 69 6e 74 65 72 69 6f 72 57 72 69 74  ;.  interiorWrit
35978 65 72 49 6e 69 74 28 70 57 72 69 74 65 72 2d 3e  erInit(pWriter->
35979 69 48 65 69 67 68 74 2b 31 2c 0a 20 20 20 20 20  iHeight+1,.     
3597a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3597b 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2e 70 44 61 74  block->term.pDat
3597c 61 2c 20 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2e 6e  a, block->term.n
3597d 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20  Data,.          
3597e 20 20 20 20 20 20 20 20 20 20 20 69 42 6c 6f 63             iBloc
3597f 6b 69 64 2c 20 70 57 72 69 74 65 72 2d 3e 70 61  kid, pWriter->pa
35980 72 65 6e 74 57 72 69 74 65 72 29 3b 0a 0a 20 20  rentWriter);..  
35981 2f 2a 20 46 6c 75 73 68 20 61 64 64 69 74 69 6f  /* Flush additio
35982 6e 61 6c 20 62 6c 6f 63 6b 73 20 61 6e 64 20 61  nal blocks and a
35983 70 70 65 6e 64 20 74 6f 20 74 68 65 20 68 69 67  ppend to the hig
35984 68 65 72 20 69 6e 74 65 72 69 6f 72 0a 20 20 2a  her interior.  *
35985 2a 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 66  * node..  */.  f
35986 6f 72 28 62 6c 6f 63 6b 3d 62 6c 6f 63 6b 2d 3e  or(block=block->
35987 6e 65 78 74 3b 20 62 6c 6f 63 6b 21 3d 4e 55 4c  next; block!=NUL
35988 4c 3b 20 62 6c 6f 63 6b 3d 62 6c 6f 63 6b 2d 3e  L; block=block->
35989 6e 65 78 74 29 7b 0a 20 20 20 20 41 53 53 45 52  next){.    ASSER
3598a 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52  T_VALID_INTERIOR
3598b 5f 42 4c 4f 43 4b 28 62 6c 6f 63 6b 29 3b 0a 20  _BLOCK(block);. 
3598c 20 20 20 72 63 20 3d 20 62 6c 6f 63 6b 5f 69 6e     rc = block_in
3598d 73 65 72 74 28 76 2c 20 62 6c 6f 63 6b 2d 3e 64  sert(v, block->d
3598e 61 74 61 2e 70 44 61 74 61 2c 20 62 6c 6f 63 6b  ata.pData, block
3598f 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2c 20 26 69  ->data.nData, &i
35990 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 20 20 69 66  Blockid);.    if
35991 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
35992 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
35993 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64 20 3d   *piEndBlockid =
35994 20 69 42 6c 6f 63 6b 69 64 3b 0a 0a 20 20 20 20   iBlockid;..    
35995 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 41 70  interiorWriterAp
35996 70 65 6e 64 28 70 57 72 69 74 65 72 2d 3e 70 61  pend(pWriter->pa
35997 72 65 6e 74 57 72 69 74 65 72 2c 0a 20 20 20 20  rentWriter,.    
35998 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35999 20 20 20 20 20 62 6c 6f 63 6b 2d 3e 74 65 72 6d       block->term
3599a 2e 70 44 61 74 61 2c 20 62 6c 6f 63 6b 2d 3e 74  .pData, block->t
3599b 65 72 6d 2e 6e 44 61 74 61 2c 20 69 42 6c 6f 63  erm.nData, iBloc
3599c 6b 69 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  kid);.  }..  /* 
3599d 50 61 72 65 6e 74 20 6e 6f 64 65 20 67 65 74 73  Parent node gets
3599e 20 74 68 65 20 63 68 61 6e 63 65 20 74 6f 20 62   the chance to b
3599f 65 20 74 68 65 20 72 6f 6f 74 2e 20 2a 2f 0a 20  e the root. */. 
359a0 20 72 65 74 75 72 6e 20 69 6e 74 65 72 69 6f 72   return interior
359a1 57 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 76  WriterRootInfo(v
359a2 2c 20 70 57 72 69 74 65 72 2d 3e 70 61 72 65 6e  , pWriter->paren
359a3 74 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20  tWriter,.       
359a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
359a5 20 20 20 20 20 20 20 20 20 70 70 52 6f 6f 74 49           ppRootI
359a6 6e 66 6f 2c 20 70 6e 52 6f 6f 74 49 6e 66 6f 2c  nfo, pnRootInfo,
359a7 20 70 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a   piEndBlockid);.
359a8 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
359a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
359aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
359ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
359ac 2a 2a 2a 2a 2f 0a 2f 2a 20 49 6e 74 65 72 69 6f  ****/./* Interio
359ad 72 52 65 61 64 65 72 20 69 73 20 75 73 65 64 20  rReader is used 
359ae 74 6f 20 72 65 61 64 20 6f 66 66 20 74 68 65 20  to read off the 
359af 64 61 74 61 20 66 72 6f 6d 20 61 6e 20 69 6e 74  data from an int
359b0 65 72 69 6f 72 20 6e 6f 64 65 0a 2a 2a 20 28 73  erior node.** (s
359b1 65 65 20 63 6f 6d 6d 65 6e 74 20 61 74 20 74 6f  ee comment at to
359b2 70 20 6f 66 20 66 69 6c 65 20 66 6f 72 20 74 68  p of file for th
359b3 65 20 66 6f 72 6d 61 74 29 2e 0a 2a 2f 0a 74 79  e format)..*/.ty
359b4 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e 74  pedef struct Int
359b5 65 72 69 6f 72 52 65 61 64 65 72 20 7b 0a 20 20  eriorReader {.  
359b6 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
359b7 61 3b 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a  a;.  int nData;.
359b8 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 74 65  .  DataBuffer te
359b9 72 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  rm;          /* 
359ba 70 72 65 76 69 6f 75 73 20 74 65 72 6d 2c 20 66  previous term, f
359bb 6f 72 20 64 65 63 6f 64 69 6e 67 20 74 65 72 6d  or decoding term
359bc 20 64 65 6c 74 61 2e 20 2a 2f 0a 0a 20 20 73 71   delta. */..  sq
359bd 6c 69 74 65 5f 69 6e 74 36 34 20 69 42 6c 6f 63  lite_int64 iBloc
359be 6b 69 64 3b 0a 7d 20 49 6e 74 65 72 69 6f 72 52  kid;.} InteriorR
359bf 65 61 64 65 72 3b 0a 0a 73 74 61 74 69 63 20 76  eader;..static v
359c0 6f 69 64 20 69 6e 74 65 72 69 6f 72 52 65 61 64  oid interiorRead
359c1 65 72 44 65 73 74 72 6f 79 28 49 6e 74 65 72 69  erDestroy(Interi
359c2 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  orReader *pReade
359c3 72 29 7b 0a 20 20 64 61 74 61 42 75 66 66 65 72  r){.  dataBuffer
359c4 44 65 73 74 72 6f 79 28 26 70 52 65 61 64 65 72  Destroy(&pReader
359c5 2d 3e 74 65 72 6d 29 3b 0a 20 20 53 43 52 41 4d  ->term);.  SCRAM
359c6 42 4c 45 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a  BLE(pReader);.}.
359c7 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
359c8 54 68 65 20 61 73 73 65 72 74 69 6f 6e 73 20 61  The assertions a
359c9 72 65 20 67 72 65 61 74 2c 20 62 75 74 20 77 68  re great, but wh
359ca 61 74 20 69 66 20 77 65 27 72 65 20 69 6e 20 4e  at if we're in N
359cb 44 45 42 55 47 0a 2a 2a 20 61 6e 64 20 74 68 65  DEBUG.** and the
359cc 20 62 6c 6f 62 20 69 73 20 65 6d 70 74 79 20 6f   blob is empty o
359cd 72 20 6f 74 68 65 72 77 69 73 65 20 63 6f 6e 74  r otherwise cont
359ce 61 69 6e 73 20 73 75 73 70 65 63 74 20 64 61 74  ains suspect dat
359cf 61 3f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  a?.*/.static voi
359d0 64 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72  d interiorReader
359d1 49 6e 69 74 28 63 6f 6e 73 74 20 63 68 61 72 20  Init(const char 
359d2 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74  *pData, int nDat
359d3 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  a,.             
359d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
359d5 20 20 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72    InteriorReader
359d6 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 69 6e   *pReader){.  in
359d7 74 20 6e 2c 20 6e 54 65 72 6d 3b 0a 0a 20 20 2f  t n, nTerm;..  /
359d8 2a 20 52 65 71 75 69 72 65 20 61 74 20 6c 65 61  * Require at lea
359d9 73 74 20 74 68 65 20 6c 65 61 64 69 6e 67 20 66  st the leading f
359da 6c 61 67 20 62 79 74 65 20 2a 2f 0a 20 20 61 73  lag byte */.  as
359db 73 65 72 74 28 20 6e 44 61 74 61 3e 30 20 29 3b  sert( nData>0 );
359dc 0a 20 20 61 73 73 65 72 74 28 20 70 44 61 74 61  .  assert( pData
359dd 5b 30 5d 21 3d 27 5c 30 27 20 29 3b 0a 0a 20 20  [0]!='\0' );..  
359de 43 4c 45 41 52 28 70 52 65 61 64 65 72 29 3b 0a  CLEAR(pReader);.
359df 0a 20 20 2f 2a 20 44 65 63 6f 64 65 20 74 68 65  .  /* Decode the
359e0 20 62 61 73 65 20 62 6c 6f 63 6b 69 64 2c 20 61   base blockid, a
359e1 6e 64 20 73 65 74 20 74 68 65 20 63 75 72 73 6f  nd set the curso
359e2 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 74  r to the first t
359e3 65 72 6d 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74  erm. */.  n = ft
359e4 73 33 47 65 74 56 61 72 69 6e 74 28 70 44 61 74  s3GetVarint(pDat
359e5 61 2b 31 2c 20 26 70 52 65 61 64 65 72 2d 3e 69  a+1, &pReader->i
359e6 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 61 73 73 65  Blockid);.  asse
359e7 72 74 28 20 31 2b 6e 3c 3d 6e 44 61 74 61 20 29  rt( 1+n<=nData )
359e8 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61  ;.  pReader->pDa
359e9 74 61 20 3d 20 70 44 61 74 61 2b 31 2b 6e 3b 0a  ta = pData+1+n;.
359ea 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61    pReader->nData
359eb 20 3d 20 6e 44 61 74 61 2d 28 31 2b 6e 29 3b 0a   = nData-(1+n);.
359ec 0a 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 2d 63  .  /* A single-c
359ed 68 69 6c 64 20 69 6e 74 65 72 69 6f 72 20 6e 6f  hild interior no
359ee 64 65 20 28 73 75 63 68 20 61 73 20 77 68 65 6e  de (such as when
359ef 20 61 20 6c 65 61 66 20 6e 6f 64 65 20 77 61 73   a leaf node was
359f0 20 74 6f 6f 0a 20 20 2a 2a 20 6c 61 72 67 65 20   too.  ** large 
359f1 66 6f 72 20 74 68 65 20 73 65 67 6d 65 6e 74 20  for the segment 
359f2 64 69 72 65 63 74 6f 72 79 29 20 77 6f 6e 27 74  directory) won't
359f3 20 68 61 76 65 20 61 6e 79 20 74 65 72 6d 73 2e   have any terms.
359f4 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  .  ** Otherwise,
359f5 20 64 65 63 6f 64 65 20 74 68 65 20 66 69 72 73   decode the firs
359f6 74 20 74 65 72 6d 2e 0a 20 20 2a 2f 0a 20 20 69  t term..  */.  i
359f7 66 28 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  f( pReader->nDat
359f8 61 3d 3d 30 20 29 7b 0a 20 20 20 20 64 61 74 61  a==0 ){.    data
359f9 42 75 66 66 65 72 49 6e 69 74 28 26 70 52 65 61  BufferInit(&pRea
359fa 64 65 72 2d 3e 74 65 72 6d 2c 20 30 29 3b 0a 20  der->term, 0);. 
359fb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 20 3d 20   }else{.    n = 
359fc 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
359fd 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20  pReader->pData, 
359fe 26 6e 54 65 72 6d 29 3b 0a 20 20 20 20 64 61 74  &nTerm);.    dat
359ff 61 42 75 66 66 65 72 49 6e 69 74 28 26 70 52 65  aBufferInit(&pRe
35a00 61 64 65 72 2d 3e 74 65 72 6d 2c 20 6e 54 65 72  ader->term, nTer
35a01 6d 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66  m);.    dataBuff
35a02 65 72 52 65 70 6c 61 63 65 28 26 70 52 65 61 64  erReplace(&pRead
35a03 65 72 2d 3e 74 65 72 6d 2c 20 70 52 65 61 64 65  er->term, pReade
35a04 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 6e 54 65 72  r->pData+n, nTer
35a05 6d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  m);.    assert( 
35a06 6e 2b 6e 54 65 72 6d 3c 3d 70 52 65 61 64 65 72  n+nTerm<=pReader
35a07 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 70  ->nData );.    p
35a08 52 65 61 64 65 72 2d 3e 70 44 61 74 61 20 2b 3d  Reader->pData +=
35a09 20 6e 2b 6e 54 65 72 6d 3b 0a 20 20 20 20 70 52   n+nTerm;.    pR
35a0a 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 2d 3d 20  eader->nData -= 
35a0b 6e 2b 6e 54 65 72 6d 3b 0a 20 20 7d 0a 7d 0a 0a  n+nTerm;.  }.}..
35a0c 73 74 61 74 69 63 20 69 6e 74 20 69 6e 74 65 72  static int inter
35a0d 69 6f 72 52 65 61 64 65 72 41 74 45 6e 64 28 49  iorReaderAtEnd(I
35a0e 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a 70  nteriorReader *p
35a0f 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72  Reader){.  retur
35a10 6e 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e  n pReader->term.
35a11 6e 44 61 74 61 3d 3d 30 3b 0a 7d 0a 0a 73 74 61  nData==0;.}..sta
35a12 74 69 63 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  tic sqlite_int64
35a13 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 43   interiorReaderC
35a14 75 72 72 65 6e 74 42 6c 6f 63 6b 69 64 28 49 6e  urrentBlockid(In
35a15 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a 70 52  teriorReader *pR
35a16 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72 6e  eader){.  return
35a17 20 70 52 65 61 64 65 72 2d 3e 69 42 6c 6f 63 6b   pReader->iBlock
35a18 69 64 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  id;.}..static in
35a19 74 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72  t interiorReader
35a1a 54 65 72 6d 42 79 74 65 73 28 49 6e 74 65 72 69  TermBytes(Interi
35a1b 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  orReader *pReade
35a1c 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 69  r){.  assert( !i
35a1d 6e 74 65 72 69 6f 72 52 65 61 64 65 72 41 74 45  nteriorReaderAtE
35a1e 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20  nd(pReader) );. 
35a1f 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d   return pReader-
35a20 3e 74 65 72 6d 2e 6e 44 61 74 61 3b 0a 7d 0a 73  >term.nData;.}.s
35a21 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
35a22 20 2a 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72   *interiorReader
35a23 54 65 72 6d 28 49 6e 74 65 72 69 6f 72 52 65 61  Term(InteriorRea
35a24 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
35a25 20 61 73 73 65 72 74 28 20 21 69 6e 74 65 72 69   assert( !interi
35a26 6f 72 52 65 61 64 65 72 41 74 45 6e 64 28 70 52  orReaderAtEnd(pR
35a27 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75  eader) );.  retu
35a28 72 6e 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d  rn pReader->term
35a29 2e 70 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 20 53 74  .pData;.}../* St
35a2a 65 70 20 66 6f 72 77 61 72 64 20 74 6f 20 74 68  ep forward to th
35a2b 65 20 6e 65 78 74 20 74 65 72 6d 20 69 6e 20 74  e next term in t
35a2c 68 65 20 6e 6f 64 65 2e 20 2a 2f 0a 73 74 61 74  he node. */.stat
35a2d 69 63 20 76 6f 69 64 20 69 6e 74 65 72 69 6f 72  ic void interior
35a2e 52 65 61 64 65 72 53 74 65 70 28 49 6e 74 65 72  ReaderStep(Inter
35a2f 69 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61 64  iorReader *pRead
35a30 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21  er){.  assert( !
35a31 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 41 74  interiorReaderAt
35a32 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a  End(pReader) );.
35a33 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6c 61 73  .  /* If the las
35a34 74 20 74 65 72 6d 20 68 61 73 20 62 65 65 6e 20  t term has been 
35a35 72 65 61 64 2c 20 73 69 67 6e 61 6c 20 65 6f 66  read, signal eof
35a36 2c 20 65 6c 73 65 20 63 6f 6e 73 74 72 75 63 74  , else construct
35a37 20 74 68 65 0a 20 20 2a 2a 20 6e 65 78 74 20 74   the.  ** next t
35a38 65 72 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  erm..  */.  if( 
35a39 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 3d 3d  pReader->nData==
35a3a 30 20 29 7b 0a 20 20 20 20 64 61 74 61 42 75 66  0 ){.    dataBuf
35a3b 66 65 72 52 65 73 65 74 28 26 70 52 65 61 64 65  ferReset(&pReade
35a3c 72 2d 3e 74 65 72 6d 29 3b 0a 20 20 7d 65 6c 73  r->term);.  }els
35a3d 65 7b 0a 20 20 20 20 69 6e 74 20 6e 2c 20 6e 50  e{.    int n, nP
35a3e 72 65 66 69 78 2c 20 6e 53 75 66 66 69 78 3b 0a  refix, nSuffix;.
35a3f 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65 74  .    n = fts3Get
35a40 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65 72  Varint32(pReader
35a41 2d 3e 70 44 61 74 61 2c 20 26 6e 50 72 65 66 69  ->pData, &nPrefi
35a42 78 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73  x);.    n += fts
35a43 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65  3GetVarint32(pRe
35a44 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26  ader->pData+n, &
35a45 6e 53 75 66 66 69 78 29 3b 0a 0a 20 20 20 20 2f  nSuffix);..    /
35a46 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 63  * Truncate the c
35a47 75 72 72 65 6e 74 20 74 65 72 6d 20 61 6e 64 20  urrent term and 
35a48 61 70 70 65 6e 64 20 73 75 66 66 69 78 20 64 61  append suffix da
35a49 74 61 2e 20 2a 2f 0a 20 20 20 20 70 52 65 61 64  ta. */.    pRead
35a4a 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 20 3d  er->term.nData =
35a4b 20 6e 50 72 65 66 69 78 3b 0a 20 20 20 20 64 61   nPrefix;.    da
35a4c 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 26  taBufferAppend(&
35a4d 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2c 20 70  pReader->term, p
35a4e 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c  Reader->pData+n,
35a4f 20 6e 53 75 66 66 69 78 29 3b 0a 0a 20 20 20 20   nSuffix);..    
35a50 61 73 73 65 72 74 28 20 6e 2b 6e 53 75 66 66 69  assert( n+nSuffi
35a51 78 3c 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  x<=pReader->nDat
35a52 61 20 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72  a );.    pReader
35a53 2d 3e 70 44 61 74 61 20 2b 3d 20 6e 2b 6e 53 75  ->pData += n+nSu
35a54 66 66 69 78 3b 0a 20 20 20 20 70 52 65 61 64 65  ffix;.    pReade
35a55 72 2d 3e 6e 44 61 74 61 20 2d 3d 20 6e 2b 6e 53  r->nData -= n+nS
35a56 75 66 66 69 78 3b 0a 20 20 7d 0a 20 20 70 52 65  uffix;.  }.  pRe
35a57 61 64 65 72 2d 3e 69 42 6c 6f 63 6b 69 64 2b 2b  ader->iBlockid++
35a58 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6d 70 61 72 65 20  ;.}../* Compare 
35a59 74 68 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d  the current term
35a5a 20 74 6f 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d   to pTerm[nTerm]
35a5b 2c 20 72 65 74 75 72 6e 69 6e 67 20 73 74 72 63  , returning strc
35a5c 6d 70 2d 73 74 79 6c 65 0a 2a 2a 20 72 65 73 75  mp-style.** resu
35a5d 6c 74 73 2e 20 20 49 66 20 69 73 50 72 65 66 69  lts.  If isPrefi
35a5e 78 2c 20 65 71 75 61 6c 69 74 79 20 6d 65 61 6e  x, equality mean
35a5f 73 20 65 71 75 61 6c 20 74 68 72 6f 75 67 68 20  s equal through 
35a60 6e 54 65 72 6d 20 62 79 74 65 73 2e 0a 2a 2f 0a  nTerm bytes..*/.
35a61 73 74 61 74 69 63 20 69 6e 74 20 69 6e 74 65 72  static int inter
35a62 69 6f 72 52 65 61 64 65 72 54 65 72 6d 43 6d 70  iorReaderTermCmp
35a63 28 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20  (InteriorReader 
35a64 2a 70 52 65 61 64 65 72 2c 0a 20 20 20 20 20 20  *pReader,.      
35a65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35a66 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
35a67 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e   char *pTerm, in
35a68 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73 50  t nTerm, int isP
35a69 72 65 66 69 78 29 7b 0a 20 20 63 6f 6e 73 74 20  refix){.  const 
35a6a 63 68 61 72 20 2a 70 52 65 61 64 65 72 54 65 72  char *pReaderTer
35a6b 6d 20 3d 20 69 6e 74 65 72 69 6f 72 52 65 61 64  m = interiorRead
35a6c 65 72 54 65 72 6d 28 70 52 65 61 64 65 72 29 3b  erTerm(pReader);
35a6d 0a 20 20 69 6e 74 20 6e 52 65 61 64 65 72 54 65  .  int nReaderTe
35a6e 72 6d 20 3d 20 69 6e 74 65 72 69 6f 72 52 65 61  rm = interiorRea
35a6f 64 65 72 54 65 72 6d 42 79 74 65 73 28 70 52 65  derTermBytes(pRe
35a70 61 64 65 72 29 3b 0a 20 20 69 6e 74 20 63 2c 20  ader);.  int c, 
35a71 6e 20 3d 20 6e 52 65 61 64 65 72 54 65 72 6d 3c  n = nReaderTerm<
35a72 6e 54 65 72 6d 20 3f 20 6e 52 65 61 64 65 72 54  nTerm ? nReaderT
35a73 65 72 6d 20 3a 20 6e 54 65 72 6d 3b 0a 0a 20 20  erm : nTerm;..  
35a74 69 66 28 20 6e 3d 3d 30 20 29 7b 0a 20 20 20 20  if( n==0 ){.    
35a75 69 66 28 20 6e 52 65 61 64 65 72 54 65 72 6d 3e  if( nReaderTerm>
35a76 30 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20  0 ) return -1;. 
35a77 20 20 20 69 66 28 20 6e 54 65 72 6d 3e 30 20 29     if( nTerm>0 )
35a78 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 72   return 1;.    r
35a79 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20  eturn 0;.  }..  
35a7a 63 20 3d 20 6d 65 6d 63 6d 70 28 70 52 65 61 64  c = memcmp(pRead
35a7b 65 72 54 65 72 6d 2c 20 70 54 65 72 6d 2c 20 6e  erTerm, pTerm, n
35a7c 29 3b 0a 20 20 69 66 28 20 63 21 3d 30 20 29 20  );.  if( c!=0 ) 
35a7d 72 65 74 75 72 6e 20 63 3b 0a 20 20 69 66 28 20  return c;.  if( 
35a7e 69 73 50 72 65 66 69 78 20 26 26 20 6e 3d 3d 6e  isPrefix && n==n
35a7f 54 65 72 6d 20 29 20 72 65 74 75 72 6e 20 30 3b  Term ) return 0;
35a80 0a 20 20 72 65 74 75 72 6e 20 6e 52 65 61 64 65  .  return nReade
35a81 72 54 65 72 6d 20 2d 20 6e 54 65 72 6d 3b 0a 7d  rTerm - nTerm;.}
35a82 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
35a83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35a84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35a85 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35a86 2a 2a 2a 2f 0a 2f 2a 20 4c 65 61 66 57 72 69 74  ***/./* LeafWrit
35a87 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f  er is used to co
35a88 6c 6c 65 63 74 20 74 65 72 6d 73 20 61 6e 64 20  llect terms and 
35a89 61 73 73 6f 63 69 61 74 65 64 20 64 6f 63 6c 69  associated docli
35a8a 73 74 20 64 61 74 61 0a 2a 2a 20 69 6e 74 6f 20  st data.** into 
35a8b 6c 65 61 66 20 62 6c 6f 63 6b 73 20 69 6e 20 25  leaf blocks in %
35a8c 5f 73 65 67 6d 65 6e 74 73 20 28 73 65 65 20 74  _segments (see t
35a8d 6f 70 20 6f 66 20 66 69 6c 65 20 66 6f 72 20 66  op of file for f
35a8e 6f 72 6d 61 74 20 69 6e 66 6f 29 2e 0a 2a 2a 20  ormat info)..** 
35a8f 45 78 70 65 63 74 65 64 20 75 73 61 67 65 20 69  Expected usage i
35a90 73 3a 0a 2a 2a 0a 2a 2a 20 4c 65 61 66 57 72 69  s:.**.** LeafWri
35a91 74 65 72 20 77 72 69 74 65 72 3b 0a 2a 2a 20 6c  ter writer;.** l
35a92 65 61 66 57 72 69 74 65 72 49 6e 69 74 28 30 2c  eafWriterInit(0,
35a93 20 30 2c 20 26 77 72 69 74 65 72 29 3b 0a 2a 2a   0, &writer);.**
35a94 20 77 68 69 6c 65 28 20 73 6f 72 74 65 64 5f 74   while( sorted_t
35a95 65 72 6d 73 5f 6c 65 66 74 5f 74 6f 5f 70 72 6f  erms_left_to_pro
35a96 63 65 73 73 20 29 7b 0a 2a 2a 20 20 20 2f 2f 20  cess ){.**   // 
35a97 64 61 74 61 20 69 73 20 64 6f 63 6c 69 73 74 20  data is doclist 
35a98 64 61 74 61 20 66 6f 72 20 74 68 61 74 20 74 65  data for that te
35a99 72 6d 2e 0a 2a 2a 20 20 20 72 63 20 3d 20 6c 65  rm..**   rc = le
35a9a 61 66 57 72 69 74 65 72 53 74 65 70 28 76 2c 20  afWriterStep(v, 
35a9b 26 77 72 69 74 65 72 2c 20 70 54 65 72 6d 2c 20  &writer, pTerm, 
35a9c 6e 54 65 72 6d 2c 20 70 44 61 74 61 2c 20 6e 44  nTerm, pData, nD
35a9d 61 74 61 29 3b 0a 2a 2a 20 20 20 69 66 28 20 72  ata);.**   if( r
35a9e 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
35a9f 6f 74 6f 20 65 72 72 3b 0a 2a 2a 20 7d 0a 2a 2a  oto err;.** }.**
35aa0 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72   rc = leafWriter
35aa1 46 69 6e 61 6c 69 7a 65 28 76 2c 20 26 77 72 69  Finalize(v, &wri
35aa2 74 65 72 29 3b 0a 2a 2a 65 72 72 3a 0a 2a 2a 20  ter);.**err:.** 
35aa3 6c 65 61 66 57 72 69 74 65 72 44 65 73 74 72 6f  leafWriterDestro
35aa4 79 28 26 77 72 69 74 65 72 29 3b 0a 2a 2a 20 72  y(&writer);.** r
35aa5 65 74 75 72 6e 20 72 63 3b 0a 2a 2a 0a 2a 2a 20  eturn rc;.**.** 
35aa6 6c 65 61 66 57 72 69 74 65 72 53 74 65 70 28 29  leafWriterStep()
35aa7 20 6d 61 79 20 77 72 69 74 65 20 61 20 63 6f 6c   may write a col
35aa8 6c 65 63 74 65 64 20 6c 65 61 66 20 6f 75 74 20  lected leaf out 
35aa9 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 2e 0a 2a  to %_segments..*
35aaa 2a 20 6c 65 61 66 57 72 69 74 65 72 46 69 6e 61  * leafWriterFina
35aab 6c 69 7a 65 28 29 20 66 69 6e 69 73 68 65 73 20  lize() finishes 
35aac 77 72 69 74 69 6e 67 20 61 6e 79 20 62 75 66 66  writing any buff
35aad 65 72 65 64 20 64 61 74 61 20 61 6e 64 20 73 74  ered data and st
35aae 6f 72 65 73 0a 2a 2a 20 61 20 72 6f 6f 74 20 6e  ores.** a root n
35aaf 6f 64 65 20 69 6e 20 25 5f 73 65 67 64 69 72 2e  ode in %_segdir.
35ab0 20 20 6c 65 61 66 57 72 69 74 65 72 44 65 73 74    leafWriterDest
35ab1 72 6f 79 28 29 20 66 72 65 65 73 20 61 6c 6c 20  roy() frees all 
35ab2 62 75 66 66 65 72 73 20 61 6e 64 0a 2a 2a 20 49  buffers and.** I
35ab3 6e 74 65 72 69 6f 72 57 72 69 74 65 72 73 20 61  nteriorWriters a
35ab4 6c 6c 6f 63 61 74 65 64 20 61 73 20 70 61 72 74  llocated as part
35ab5 20 6f 66 20 77 72 69 74 69 6e 67 20 74 68 69 73   of writing this
35ab6 20 73 65 67 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20   segment..**.** 
35ab7 54 4f 44 4f 28 73 68 65 73 73 29 20 44 6f 63 75  TODO(shess) Docu
35ab8 6d 65 6e 74 20 6c 65 61 66 57 72 69 74 65 72 53  ment leafWriterS
35ab9 74 65 70 4d 65 72 67 65 28 29 2e 0a 2a 2f 0a 0a  tepMerge()..*/..
35aba 2f 2a 20 50 75 74 20 74 65 72 6d 73 20 77 69 74  /* Put terms wit
35abb 68 20 64 61 74 61 20 74 68 69 73 20 62 69 67 20  h data this big 
35abc 69 6e 20 74 68 65 69 72 20 6f 77 6e 20 62 6c 6f  in their own blo
35abd 63 6b 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ck. */.#define S
35abe 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 31 30  TANDALONE_MIN 10
35abf 32 34 0a 0a 2f 2a 20 4b 65 65 70 20 6c 65 61 66  24../* Keep leaf
35ac0 20 62 6c 6f 63 6b 73 20 62 65 6c 6f 77 20 74 68   blocks below th
35ac1 69 73 20 73 69 7a 65 2e 20 2a 2f 0a 23 64 65 66  is size. */.#def
35ac2 69 6e 65 20 4c 45 41 46 5f 4d 41 58 20 32 30 34  ine LEAF_MAX 204
35ac3 38 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  8..typedef struc
35ac4 74 20 4c 65 61 66 57 72 69 74 65 72 20 7b 0a 20  t LeafWriter {. 
35ac5 20 69 6e 74 20 69 4c 65 76 65 6c 3b 0a 20 20 69   int iLevel;.  i
35ac6 6e 74 20 69 64 78 3b 0a 20 20 73 71 6c 69 74 65  nt idx;.  sqlite
35ac7 5f 69 6e 74 36 34 20 69 53 74 61 72 74 42 6c 6f  _int64 iStartBlo
35ac8 63 6b 69 64 3b 20 20 20 20 20 2f 2a 20 6e 65 65  ckid;     /* nee
35ac9 64 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 68  ded to create th
35aca 65 20 72 6f 6f 74 20 69 6e 66 6f 20 2a 2f 0a 20  e root info */. 
35acb 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45   sqlite_int64 iE
35acc 6e 64 42 6c 6f 63 6b 69 64 3b 20 20 20 20 20 20  ndBlockid;      
35acd 20 2f 2a 20 77 68 65 6e 20 77 65 27 72 65 20 64   /* when we're d
35ace 6f 6e 65 20 77 72 69 74 69 6e 67 2e 20 2a 2f 0a  one writing. */.
35acf 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 74 65  .  DataBuffer te
35ad0 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rm;             
35ad1 20 20 20 2f 2a 20 70 72 65 76 69 6f 75 73 20 65     /* previous e
35ad2 6e 63 6f 64 65 64 20 74 65 72 6d 20 2a 2f 0a 20  ncoded term */. 
35ad3 20 44 61 74 61 42 75 66 66 65 72 20 64 61 74 61   DataBuffer data
35ad4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
35ad5 20 2f 2a 20 65 6e 63 6f 64 69 6e 67 20 62 75 66   /* encoding buf
35ad6 66 65 72 20 2a 2f 0a 0a 20 20 2f 2a 20 62 79 74  fer */..  /* byt
35ad7 65 73 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d  es of first term
35ad8 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20   in the current 
35ad9 6e 6f 64 65 20 77 68 69 63 68 20 64 69 73 74 69  node which disti
35ada 6e 67 75 69 73 68 65 73 20 74 68 61 74 0a 20 20  nguishes that.  
35adb 2a 2a 20 74 65 72 6d 20 66 72 6f 6d 20 74 68 65  ** term from the
35adc 20 6c 61 73 74 20 74 65 72 6d 20 6f 66 20 74 68   last term of th
35add 65 20 70 72 65 76 69 6f 75 73 20 6e 6f 64 65 2e  e previous node.
35ade 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72  .  */.  int nTer
35adf 6d 44 69 73 74 69 6e 63 74 3b 0a 0a 20 20 49 6e  mDistinct;..  In
35ae0 74 65 72 69 6f 72 57 72 69 74 65 72 20 70 61 72  teriorWriter par
35ae1 65 6e 74 57 72 69 74 65 72 3b 20 20 20 20 2f 2a  entWriter;    /*
35ae2 20 69 66 20 77 65 20 6f 76 65 72 66 6c 6f 77 20   if we overflow 
35ae3 2a 2f 0a 20 20 69 6e 74 20 68 61 73 5f 70 61 72  */.  int has_par
35ae4 65 6e 74 3b 0a 7d 20 4c 65 61 66 57 72 69 74 65  ent;.} LeafWrite
35ae5 72 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  r;..static void 
35ae6 6c 65 61 66 57 72 69 74 65 72 49 6e 69 74 28 69  leafWriterInit(i
35ae7 6e 74 20 69 4c 65 76 65 6c 2c 20 69 6e 74 20 69  nt iLevel, int i
35ae8 64 78 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a  dx, LeafWriter *
35ae9 70 57 72 69 74 65 72 29 7b 0a 20 20 43 4c 45 41  pWriter){.  CLEA
35aea 52 28 70 57 72 69 74 65 72 29 3b 0a 20 20 70 57  R(pWriter);.  pW
35aeb 72 69 74 65 72 2d 3e 69 4c 65 76 65 6c 20 3d 20  riter->iLevel = 
35aec 69 4c 65 76 65 6c 3b 0a 20 20 70 57 72 69 74 65  iLevel;.  pWrite
35aed 72 2d 3e 69 64 78 20 3d 20 69 64 78 3b 0a 0a 20  r->idx = idx;.. 
35aee 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28   dataBufferInit(
35aef 26 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 2c 20  &pWriter->term, 
35af0 33 32 29 3b 0a 0a 20 20 2f 2a 20 53 74 61 72 74  32);..  /* Start
35af1 20 6f 75 74 20 77 69 74 68 20 61 20 72 65 61 73   out with a reas
35af2 6f 6e 61 62 6c 79 20 73 69 7a 65 64 20 62 6c 6f  onably sized blo
35af3 63 6b 2c 20 74 68 6f 75 67 68 20 69 74 20 63 61  ck, though it ca
35af4 6e 20 67 72 6f 77 2e 20 2a 2f 0a 20 20 64 61 74  n grow. */.  dat
35af5 61 42 75 66 66 65 72 49 6e 69 74 28 26 70 57 72  aBufferInit(&pWr
35af6 69 74 65 72 2d 3e 64 61 74 61 2c 20 4c 45 41 46  iter->data, LEAF
35af7 5f 4d 41 58 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65  _MAX);.}..#ifnde
35af8 66 20 4e 44 45 42 55 47 0a 2f 2a 20 56 65 72 69  f NDEBUG./* Veri
35af9 66 79 20 74 68 61 74 20 74 68 65 20 64 61 74 61  fy that the data
35afa 20 69 73 20 72 65 61 64 61 62 6c 65 20 61 73 20   is readable as 
35afb 61 20 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a  a leaf node. */.
35afc 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65 61 66  static void leaf
35afd 4e 6f 64 65 56 61 6c 69 64 61 74 65 28 63 6f 6e  NodeValidate(con
35afe 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
35aff 69 6e 74 20 6e 44 61 74 61 29 7b 0a 20 20 69 6e  int nData){.  in
35b00 74 20 6e 2c 20 69 44 75 6d 6d 79 3b 0a 0a 20 20  t n, iDummy;..  
35b01 69 66 28 20 6e 44 61 74 61 3d 3d 30 20 29 20 72  if( nData==0 ) r
35b02 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28  eturn;.  assert(
35b03 20 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 61 73   nData>0 );.  as
35b04 73 65 72 74 28 20 70 44 61 74 61 21 3d 30 20 29  sert( pData!=0 )
35b05 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 61 74  ;.  assert( pDat
35b06 61 2b 6e 44 61 74 61 3e 70 44 61 74 61 20 29 3b  a+nData>pData );
35b07 0a 0a 20 20 2f 2a 20 4d 75 73 74 20 6c 65 61 64  ..  /* Must lead
35b08 20 77 69 74 68 20 61 20 76 61 72 69 6e 74 28 30   with a varint(0
35b09 29 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47  ) */.  n = fts3G
35b0a 65 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61  etVarint32(pData
35b0b 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73  , &iDummy);.  as
35b0c 73 65 72 74 28 20 69 44 75 6d 6d 79 3d 3d 30 20  sert( iDummy==0 
35b0d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30  );.  assert( n>0
35b0e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3c   );.  assert( n<
35b0f 6e 44 61 74 61 20 29 3b 0a 20 20 70 44 61 74 61  nData );.  pData
35b10 20 2b 3d 20 6e 3b 0a 20 20 6e 44 61 74 61 20 2d   += n;.  nData -
35b11 3d 20 6e 3b 0a 0a 20 20 2f 2a 20 4c 65 61 64 69  = n;..  /* Leadi
35b12 6e 67 20 74 65 72 6d 20 6c 65 6e 67 74 68 20 61  ng term length a
35b13 6e 64 20 64 61 74 61 20 6d 75 73 74 20 66 69 74  nd data must fit
35b14 20 69 6e 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20   in buffer. */. 
35b15 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
35b16 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44 75  nt32(pData, &iDu
35b17 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20  mmy);.  assert( 
35b18 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  n>0 );.  assert(
35b19 20 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 61   iDummy>0 );.  a
35b1a 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3e  ssert( n+iDummy>
35b1b 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  0 );.  assert( n
35b1c 2b 69 44 75 6d 6d 79 3c 6e 44 61 74 61 20 29 3b  +iDummy<nData );
35b1d 0a 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44  .  pData += n+iD
35b1e 75 6d 6d 79 3b 0a 20 20 6e 44 61 74 61 20 2d 3d  ummy;.  nData -=
35b1f 20 6e 2b 69 44 75 6d 6d 79 3b 0a 0a 20 20 2f 2a   n+iDummy;..  /*
35b20 20 4c 65 61 64 69 6e 67 20 74 65 72 6d 27 73 20   Leading term's 
35b21 64 6f 63 6c 69 73 74 20 6c 65 6e 67 74 68 20 61  doclist length a
35b22 6e 64 20 64 61 74 61 20 6d 75 73 74 20 66 69 74  nd data must fit
35b23 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47  . */.  n = fts3G
35b24 65 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61  etVarint32(pData
35b25 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73  , &iDummy);.  as
35b26 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 61  sert( n>0 );.  a
35b27 73 73 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20  ssert( iDummy>0 
35b28 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 2b 69  );.  assert( n+i
35b29 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 61 73 73  Dummy>0 );.  ass
35b2a 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c 3d 6e  ert( n+iDummy<=n
35b2b 44 61 74 61 20 29 3b 0a 20 20 41 53 53 45 52 54  Data );.  ASSERT
35b2c 5f 56 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28 44  _VALID_DOCLIST(D
35b2d 4c 5f 44 45 46 41 55 4c 54 2c 20 70 44 61 74 61  L_DEFAULT, pData
35b2e 2b 6e 2c 20 69 44 75 6d 6d 79 2c 20 4e 55 4c 4c  +n, iDummy, NULL
35b2f 29 3b 0a 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b  );.  pData += n+
35b30 69 44 75 6d 6d 79 3b 0a 20 20 6e 44 61 74 61 20  iDummy;.  nData 
35b31 2d 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 0a 20 20  -= n+iDummy;..  
35b32 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74  /* Verify that t
35b33 72 61 69 6c 69 6e 67 20 74 65 72 6d 73 20 61 6e  railing terms an
35b34 64 20 64 6f 63 6c 69 73 74 73 20 61 6c 73 6f 20  d doclists also 
35b35 61 72 65 20 72 65 61 64 61 62 6c 65 2e 20 2a 2f  are readable. */
35b36 0a 20 20 77 68 69 6c 65 28 20 6e 44 61 74 61 21  .  while( nData!
35b37 3d 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 66 74  =0 ){.    n = ft
35b38 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 44  s3GetVarint32(pD
35b39 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20  ata, &iDummy);. 
35b3a 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29     assert( n>0 )
35b3b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44  ;.    assert( iD
35b3c 75 6d 6d 79 3e 3d 30 20 29 3b 0a 20 20 20 20 61  ummy>=0 );.    a
35b3d 73 73 65 72 74 28 20 6e 3c 6e 44 61 74 61 20 29  ssert( n<nData )
35b3e 3b 0a 20 20 20 20 70 44 61 74 61 20 2b 3d 20 6e  ;.    pData += n
35b3f 3b 0a 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e  ;.    nData -= n
35b40 3b 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65  ;.    n = fts3Ge
35b41 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c  tVarint32(pData,
35b42 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 61   &iDummy);.    a
35b43 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20  ssert( n>0 );.  
35b44 20 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d 79    assert( iDummy
35b45 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  >0 );.    assert
35b46 28 20 6e 2b 69 44 75 6d 6d 79 3e 30 20 29 3b 0a  ( n+iDummy>0 );.
35b47 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44      assert( n+iD
35b48 75 6d 6d 79 3c 6e 44 61 74 61 20 29 3b 0a 20 20  ummy<nData );.  
35b49 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44 75    pData += n+iDu
35b4a 6d 6d 79 3b 0a 20 20 20 20 6e 44 61 74 61 20 2d  mmy;.    nData -
35b4b 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 0a 20 20 20  = n+iDummy;..   
35b4c 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
35b4d 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44 75  nt32(pData, &iDu
35b4e 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74  mmy);.    assert
35b4f 28 20 6e 3e 30 20 29 3b 0a 20 20 20 20 61 73 73  ( n>0 );.    ass
35b50 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b  ert( iDummy>0 );
35b51 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69  .    assert( n+i
35b52 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20 61  Dummy>0 );.    a
35b53 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c  ssert( n+iDummy<
35b54 3d 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 41 53  =nData );.    AS
35b55 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c 49  SERT_VALID_DOCLI
35b56 53 54 28 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70  ST(DL_DEFAULT, p
35b57 44 61 74 61 2b 6e 2c 20 69 44 75 6d 6d 79 2c 20  Data+n, iDummy, 
35b58 4e 55 4c 4c 29 3b 0a 20 20 20 20 70 44 61 74 61  NULL);.    pData
35b59 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20   += n+iDummy;.  
35b5a 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b 69 44 75    nData -= n+iDu
35b5b 6d 6d 79 3b 0a 20 20 7d 0a 7d 0a 23 64 65 66 69  mmy;.  }.}.#defi
35b5c 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f  ne ASSERT_VALID_
35b5d 4c 45 41 46 5f 4e 4f 44 45 28 70 2c 20 6e 29 20  LEAF_NODE(p, n) 
35b5e 6c 65 61 66 4e 6f 64 65 56 61 6c 69 64 61 74 65  leafNodeValidate
35b5f 28 70 2c 20 6e 29 0a 23 65 6c 73 65 0a 23 64 65  (p, n).#else.#de
35b60 66 69 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49  fine ASSERT_VALI
35b61 44 5f 4c 45 41 46 5f 4e 4f 44 45 28 70 2c 20 6e  D_LEAF_NODE(p, n
35b62 29 20 61 73 73 65 72 74 28 20 31 20 29 0a 23 65  ) assert( 1 ).#e
35b63 6e 64 69 66 0a 0a 2f 2a 20 46 6c 75 73 68 20 74  ndif../* Flush t
35b64 68 65 20 63 75 72 72 65 6e 74 20 6c 65 61 66 20  he current leaf 
35b65 6e 6f 64 65 20 74 6f 20 25 5f 73 65 67 6d 65 6e  node to %_segmen
35b66 74 73 2c 20 61 6e 64 20 61 64 64 69 6e 67 20 74  ts, and adding t
35b67 68 65 20 72 65 73 75 6c 74 69 6e 67 0a 2a 2a 20  he resulting.** 
35b68 62 6c 6f 63 6b 69 64 20 61 6e 64 20 74 68 65 20  blockid and the 
35b69 73 74 61 72 74 69 6e 67 20 74 65 72 6d 20 74 6f  starting term to
35b6a 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f   the interior no
35b6b 64 65 20 77 68 69 63 68 20 77 69 6c 6c 0a 2a 2a  de which will.**
35b6c 20 63 6f 6e 74 61 69 6e 20 69 74 2e 0a 2a 2f 0a   contain it..*/.
35b6d 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57  static int leafW
35b6e 72 69 74 65 72 49 6e 74 65 72 6e 61 6c 46 6c 75  riterInternalFlu
35b6f 73 68 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  sh(fulltext_vtab
35b70 20 2a 76 2c 20 4c 65 61 66 57 72 69 74 65 72 20   *v, LeafWriter 
35b71 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20  *pWriter,.      
35b72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35b73 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
35b74 20 69 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74   iData, int nDat
35b75 61 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  a){.  sqlite_int
35b76 36 34 20 69 42 6c 6f 63 6b 69 64 20 3d 20 30 3b  64 iBlockid = 0;
35b77 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  .  const char *p
35b78 53 74 61 72 74 69 6e 67 54 65 72 6d 3b 0a 20 20  StartingTerm;.  
35b79 69 6e 74 20 6e 53 74 61 72 74 69 6e 67 54 65 72  int nStartingTer
35b7a 6d 2c 20 72 63 2c 20 6e 3b 0a 0a 20 20 2f 2a 20  m, rc, n;..  /* 
35b7b 4d 75 73 74 20 68 61 76 65 20 74 68 65 20 6c 65  Must have the le
35b7c 61 64 69 6e 67 20 76 61 72 69 6e 74 28 30 29 20  ading varint(0) 
35b7d 66 6c 61 67 2c 20 70 6c 75 73 20 61 74 20 6c 65  flag, plus at le
35b7e 61 73 74 20 73 6f 6d 65 0a 20 20 2a 2a 20 76 61  ast some.  ** va
35b7f 6c 69 64 2d 6c 6f 6f 6b 69 6e 67 20 64 61 74 61  lid-looking data
35b80 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
35b81 20 6e 44 61 74 61 3e 32 20 29 3b 0a 20 20 61 73   nData>2 );.  as
35b82 73 65 72 74 28 20 69 44 61 74 61 3e 3d 30 20 29  sert( iData>=0 )
35b83 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 61 74  ;.  assert( iDat
35b84 61 2b 6e 44 61 74 61 3c 3d 70 57 72 69 74 65 72  a+nData<=pWriter
35b85 2d 3e 64 61 74 61 2e 6e 44 61 74 61 20 29 3b 0a  ->data.nData );.
35b86 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 4c    ASSERT_VALID_L
35b87 45 41 46 5f 4e 4f 44 45 28 70 57 72 69 74 65 72  EAF_NODE(pWriter
35b88 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 61  ->data.pData+iDa
35b89 74 61 2c 20 6e 44 61 74 61 29 3b 0a 0a 20 20 72  ta, nData);..  r
35b8a 63 20 3d 20 62 6c 6f 63 6b 5f 69 6e 73 65 72 74  c = block_insert
35b8b 28 76 2c 20 70 57 72 69 74 65 72 2d 3e 64 61 74  (v, pWriter->dat
35b8c 61 2e 70 44 61 74 61 2b 69 44 61 74 61 2c 20 6e  a.pData+iData, n
35b8d 44 61 74 61 2c 20 26 69 42 6c 6f 63 6b 69 64 29  Data, &iBlockid)
35b8e 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
35b8f 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
35b90 63 3b 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c  c;.  assert( iBl
35b91 6f 63 6b 69 64 21 3d 30 20 29 3b 0a 0a 20 20 2f  ockid!=0 );..  /
35b92 2a 20 52 65 63 6f 6e 73 74 72 75 63 74 20 74 68  * Reconstruct th
35b93 65 20 66 69 72 73 74 20 74 65 72 6d 20 69 6e 20  e first term in 
35b94 74 68 65 20 6c 65 61 66 20 66 6f 72 20 70 75 72  the leaf for pur
35b95 70 6f 73 65 73 20 6f 66 20 62 75 69 6c 64 69 6e  poses of buildin
35b96 67 0a 20 20 2a 2a 20 74 68 65 20 69 6e 74 65 72  g.  ** the inter
35b97 69 6f 72 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20  ior node..  */. 
35b98 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
35b99 6e 74 33 32 28 70 57 72 69 74 65 72 2d 3e 64 61  nt32(pWriter->da
35b9a 74 61 2e 70 44 61 74 61 2b 69 44 61 74 61 2b 31  ta.pData+iData+1
35b9b 2c 20 26 6e 53 74 61 72 74 69 6e 67 54 65 72 6d  , &nStartingTerm
35b9c 29 3b 0a 20 20 70 53 74 61 72 74 69 6e 67 54 65  );.  pStartingTe
35b9d 72 6d 20 3d 20 70 57 72 69 74 65 72 2d 3e 64 61  rm = pWriter->da
35b9e 74 61 2e 70 44 61 74 61 2b 69 44 61 74 61 2b 31  ta.pData+iData+1
35b9f 2b 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 57  +n;.  assert( pW
35ba0 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
35ba1 61 3e 69 44 61 74 61 2b 31 2b 6e 2b 6e 53 74 61  a>iData+1+n+nSta
35ba2 72 74 69 6e 67 54 65 72 6d 20 29 3b 0a 20 20 61  rtingTerm );.  a
35ba3 73 73 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e  ssert( pWriter->
35ba4 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3e 30 20  nTermDistinct>0 
35ba5 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 57 72  );.  assert( pWr
35ba6 69 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74 69  iter->nTermDisti
35ba7 6e 63 74 3c 3d 6e 53 74 61 72 74 69 6e 67 54 65  nct<=nStartingTe
35ba8 72 6d 20 29 3b 0a 20 20 6e 53 74 61 72 74 69 6e  rm );.  nStartin
35ba9 67 54 65 72 6d 20 3d 20 70 57 72 69 74 65 72 2d  gTerm = pWriter-
35baa 3e 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3b 0a  >nTermDistinct;.
35bab 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e  .  if( pWriter->
35bac 68 61 73 5f 70 61 72 65 6e 74 20 29 7b 0a 20 20  has_parent ){.  
35bad 20 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72    interiorWriter
35bae 41 70 70 65 6e 64 28 26 70 57 72 69 74 65 72 2d  Append(&pWriter-
35baf 3e 70 61 72 65 6e 74 57 72 69 74 65 72 2c 0a 20  >parentWriter,. 
35bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35bb1 20 20 20 20 20 20 20 20 70 53 74 61 72 74 69 6e          pStartin
35bb2 67 54 65 72 6d 2c 20 6e 53 74 61 72 74 69 6e 67  gTerm, nStarting
35bb3 54 65 72 6d 2c 20 69 42 6c 6f 63 6b 69 64 29 3b  Term, iBlockid);
35bb4 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
35bb5 74 65 72 69 6f 72 57 72 69 74 65 72 49 6e 69 74  teriorWriterInit
35bb6 28 31 2c 20 70 53 74 61 72 74 69 6e 67 54 65 72  (1, pStartingTer
35bb7 6d 2c 20 6e 53 74 61 72 74 69 6e 67 54 65 72 6d  m, nStartingTerm
35bb8 2c 20 69 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20  , iBlockid,.    
35bb9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35bba 20 20 20 26 70 57 72 69 74 65 72 2d 3e 70 61 72     &pWriter->par
35bbb 65 6e 74 57 72 69 74 65 72 29 3b 0a 20 20 20 20  entWriter);.    
35bbc 70 57 72 69 74 65 72 2d 3e 68 61 73 5f 70 61 72  pWriter->has_par
35bbd 65 6e 74 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20  ent = 1;.  }..  
35bbe 2f 2a 20 54 72 61 63 6b 20 74 68 65 20 73 70 61  /* Track the spa
35bbf 6e 20 6f 66 20 74 68 69 73 20 73 65 67 6d 65 6e  n of this segmen
35bc0 74 27 73 20 6c 65 61 66 20 6e 6f 64 65 73 2e 20  t's leaf nodes. 
35bc1 2a 2f 0a 20 20 69 66 28 20 70 57 72 69 74 65 72  */.  if( pWriter
35bc2 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69 64 3d 3d 30  ->iEndBlockid==0
35bc3 20 29 7b 0a 20 20 20 20 70 57 72 69 74 65 72 2d   ){.    pWriter-
35bc4 3e 69 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 70  >iEndBlockid = p
35bc5 57 72 69 74 65 72 2d 3e 69 53 74 61 72 74 42 6c  Writer->iStartBl
35bc6 6f 63 6b 69 64 20 3d 20 69 42 6c 6f 63 6b 69 64  ockid = iBlockid
35bc7 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
35bc8 57 72 69 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63  Writer->iEndBloc
35bc9 6b 69 64 2b 2b 3b 0a 20 20 20 20 61 73 73 65 72  kid++;.    asser
35bca 74 28 20 69 42 6c 6f 63 6b 69 64 3d 3d 70 57 72  t( iBlockid==pWr
35bcb 69 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69  iter->iEndBlocki
35bcc 64 20 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  d );.  }..  retu
35bcd 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
35bce 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57  static int leafW
35bcf 72 69 74 65 72 46 6c 75 73 68 28 66 75 6c 6c 74  riterFlush(fullt
35bd0 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61  ext_vtab *v, Lea
35bd1 66 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  fWriter *pWriter
35bd2 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 6c 65  ){.  int rc = le
35bd3 61 66 57 72 69 74 65 72 49 6e 74 65 72 6e 61 6c  afWriterInternal
35bd4 46 6c 75 73 68 28 76 2c 20 70 57 72 69 74 65 72  Flush(v, pWriter
35bd5 2c 20 30 2c 20 70 57 72 69 74 65 72 2d 3e 64 61  , 0, pWriter->da
35bd6 74 61 2e 6e 44 61 74 61 29 3b 0a 20 20 69 66 28  ta.nData);.  if(
35bd7 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
35bd8 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f   return rc;..  /
35bd9 2a 20 52 65 2d 69 6e 69 74 69 61 6c 69 7a 65 20  * Re-initialize 
35bda 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65  the output buffe
35bdb 72 2e 20 2a 2f 0a 20 20 64 61 74 61 42 75 66 66  r. */.  dataBuff
35bdc 65 72 52 65 73 65 74 28 26 70 57 72 69 74 65 72  erReset(&pWriter
35bdd 2d 3e 64 61 74 61 29 3b 0a 0a 20 20 72 65 74 75  ->data);..  retu
35bde 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
35bdf 0a 2f 2a 20 46 65 74 63 68 20 74 68 65 20 72 6f  ./* Fetch the ro
35be0 6f 74 20 69 6e 66 6f 20 66 6f 72 20 74 68 65 20  ot info for the 
35be1 73 65 67 6d 65 6e 74 2e 20 20 49 66 20 74 68 65  segment.  If the
35be2 20 65 6e 74 69 72 65 20 6c 65 61 66 20 66 69 74   entire leaf fit
35be3 73 0a 2a 2a 20 77 69 74 68 69 6e 20 52 4f 4f 54  s.** within ROOT
35be4 5f 4d 41 58 2c 20 74 68 65 6e 20 69 74 20 77 69  _MAX, then it wi
35be5 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 64  ll be returned d
35be6 69 72 65 63 74 6c 79 2c 20 6f 74 68 65 72 77 69  irectly, otherwi
35be7 73 65 20 69 74 0a 2a 2a 20 77 69 6c 6c 20 62 65  se it.** will be
35be8 20 66 6c 75 73 68 65 64 20 61 6e 64 20 74 68 65   flushed and the
35be9 20 72 6f 6f 74 20 69 6e 66 6f 20 77 69 6c 6c 20   root info will 
35bea 62 65 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d  be returned from
35beb 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 69 6f 72   the.** interior
35bec 20 6e 6f 64 65 2e 20 20 2a 70 69 45 6e 64 42 6c   node.  *piEndBl
35bed 6f 63 6b 69 64 20 69 73 20 73 65 74 20 74 6f 20  ockid is set to 
35bee 74 68 65 20 62 6c 6f 63 6b 69 64 20 6f 66 20 74  the blockid of t
35bef 68 65 20 6c 61 73 74 0a 2a 2a 20 69 6e 74 65 72  he last.** inter
35bf0 69 6f 72 20 6f 72 20 6c 65 61 66 20 6e 6f 64 65  ior or leaf node
35bf1 20 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b   written to disk
35bf2 20 28 30 20 69 66 20 6e 6f 6e 65 20 61 72 65 20   (0 if none are 
35bf3 77 72 69 74 74 65 6e 20 61 74 0a 2a 2a 20 61 6c  written at.** al
35bf4 6c 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  l)..*/.static in
35bf5 74 20 6c 65 61 66 57 72 69 74 65 72 52 6f 6f 74  t leafWriterRoot
35bf6 49 6e 66 6f 28 66 75 6c 6c 74 65 78 74 5f 76 74  Info(fulltext_vt
35bf7 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69 74 65  ab *v, LeafWrite
35bf8 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20  r *pWriter,.    
35bf9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35bfa 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
35bfb 2a 70 70 52 6f 6f 74 49 6e 66 6f 2c 20 69 6e 74  *ppRootInfo, int
35bfc 20 2a 70 6e 52 6f 6f 74 49 6e 66 6f 2c 0a 20 20   *pnRootInfo,.  
35bfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35bfe 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
35bff 74 65 5f 69 6e 74 36 34 20 2a 70 69 45 6e 64 42  te_int64 *piEndB
35c00 6c 6f 63 6b 69 64 29 7b 0a 20 20 2f 2a 20 77 65  lockid){.  /* we
35c01 20 63 61 6e 20 66 69 74 20 74 68 65 20 73 65 67   can fit the seg
35c02 6d 65 6e 74 20 65 6e 74 69 72 65 6c 79 20 69 6e  ment entirely in
35c03 6c 69 6e 65 20 2a 2f 0a 20 20 69 66 28 20 21 70  line */.  if( !p
35c04 57 72 69 74 65 72 2d 3e 68 61 73 5f 70 61 72 65  Writer->has_pare
35c05 6e 74 20 26 26 20 70 57 72 69 74 65 72 2d 3e 64  nt && pWriter->d
35c06 61 74 61 2e 6e 44 61 74 61 3c 52 4f 4f 54 5f 4d  ata.nData<ROOT_M
35c07 41 58 20 29 7b 0a 20 20 20 20 2a 70 70 52 6f 6f  AX ){.    *ppRoo
35c08 74 49 6e 66 6f 20 3d 20 70 57 72 69 74 65 72 2d  tInfo = pWriter-
35c09 3e 64 61 74 61 2e 70 44 61 74 61 3b 0a 20 20 20  >data.pData;.   
35c0a 20 2a 70 6e 52 6f 6f 74 49 6e 66 6f 20 3d 20 70   *pnRootInfo = p
35c0b 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61  Writer->data.nDa
35c0c 74 61 3b 0a 20 20 20 20 2a 70 69 45 6e 64 42 6c  ta;.    *piEndBl
35c0d 6f 63 6b 69 64 20 3d 20 30 3b 0a 20 20 20 20 72  ockid = 0;.    r
35c0e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
35c0f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6c 75 73 68  .  }..  /* Flush
35c10 20 72 65 6d 61 69 6e 69 6e 67 20 6c 65 61 66 20   remaining leaf 
35c11 64 61 74 61 2e 20 2a 2f 0a 20 20 69 66 28 20 70  data. */.  if( p
35c12 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61  Writer->data.nDa
35c13 74 61 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  ta>0 ){.    int 
35c14 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 46  rc = leafWriterF
35c15 6c 75 73 68 28 76 2c 20 70 57 72 69 74 65 72 29  lush(v, pWriter)
35c16 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
35c17 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
35c18 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57   rc;.  }..  /* W
35c19 65 20 6d 75 73 74 20 68 61 76 65 20 66 6c 75 73  e must have flus
35c1a 68 65 64 20 61 20 6c 65 61 66 20 61 74 20 73 6f  hed a leaf at so
35c1b 6d 65 20 70 6f 69 6e 74 2e 20 2a 2f 0a 20 20 61  me point. */.  a
35c1c 73 73 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e  ssert( pWriter->
35c1d 68 61 73 5f 70 61 72 65 6e 74 20 29 3b 0a 0a 20  has_parent );.. 
35c1e 20 2f 2a 20 54 65 6e 61 74 69 76 65 6c 79 20 73   /* Tenatively s
35c1f 65 74 20 74 68 65 20 65 6e 64 20 6c 65 61 66 20  et the end leaf 
35c20 62 6c 6f 63 6b 69 64 20 61 73 20 74 68 65 20 65  blockid as the e
35c21 6e 64 20 62 6c 6f 63 6b 69 64 2e 20 20 49 66 20  nd blockid.  If 
35c22 74 68 65 0a 20 20 2a 2a 20 69 6e 74 65 72 69 6f  the.  ** interio
35c23 72 20 6e 6f 64 65 20 63 61 6e 20 62 65 20 72 65  r node can be re
35c24 74 75 72 6e 65 64 20 69 6e 6c 69 6e 65 2c 20 74  turned inline, t
35c25 68 69 73 20 77 69 6c 6c 20 62 65 20 74 68 65 20  his will be the 
35c26 66 69 6e 61 6c 0a 20 20 2a 2a 20 62 6c 6f 63 6b  final.  ** block
35c27 69 64 2c 20 6f 74 68 65 72 77 69 73 65 20 69 74  id, otherwise it
35c28 20 77 69 6c 6c 20 62 65 20 6f 76 65 72 77 72 69   will be overwri
35c29 74 74 65 6e 20 62 79 0a 20 20 2a 2a 20 69 6e 74  tten by.  ** int
35c2a 65 72 69 6f 72 57 72 69 74 65 72 52 6f 6f 74 49  eriorWriterRootI
35c2b 6e 66 6f 28 29 2e 0a 20 20 2a 2f 0a 20 20 2a 70  nfo()..  */.  *p
35c2c 69 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 70 57  iEndBlockid = pW
35c2d 72 69 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b  riter->iEndBlock
35c2e 69 64 3b 0a 0a 20 20 72 65 74 75 72 6e 20 69 6e  id;..  return in
35c2f 74 65 72 69 6f 72 57 72 69 74 65 72 52 6f 6f 74  teriorWriterRoot
35c30 49 6e 66 6f 28 76 2c 20 26 70 57 72 69 74 65 72  Info(v, &pWriter
35c31 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 2c 0a  ->parentWriter,.
35c32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c34 70 70 52 6f 6f 74 49 6e 66 6f 2c 20 70 6e 52 6f  ppRootInfo, pnRo
35c35 6f 74 49 6e 66 6f 2c 20 70 69 45 6e 64 42 6c 6f  otInfo, piEndBlo
35c36 63 6b 69 64 29 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6c  ckid);.}../* Col
35c37 6c 65 63 74 20 74 68 65 20 72 6f 6f 74 49 6e 66  lect the rootInf
35c38 6f 20 64 61 74 61 20 61 6e 64 20 73 74 6f 72 65  o data and store
35c39 20 69 74 20 69 6e 74 6f 20 74 68 65 20 73 65 67   it into the seg
35c3a 6d 65 6e 74 20 64 69 72 65 63 74 6f 72 79 2e 0a  ment directory..
35c3b 2a 2a 20 54 68 69 73 20 68 61 73 20 74 68 65 20  ** This has the 
35c3c 65 66 66 65 63 74 20 6f 66 20 66 6c 75 73 68 69  effect of flushi
35c3d 6e 67 20 74 68 65 20 73 65 67 6d 65 6e 74 27 73  ng the segment's
35c3e 20 6c 65 61 66 20 64 61 74 61 20 74 6f 0a 2a 2a   leaf data to.**
35c3f 20 25 5f 73 65 67 6d 65 6e 74 73 2c 20 61 6e 64   %_segments, and
35c40 20 61 6c 73 6f 20 66 6c 75 73 68 69 6e 67 20 61   also flushing a
35c41 6e 79 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  ny interior node
35c42 73 20 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 2e  s to %_segments.
35c43 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
35c44 65 61 66 57 72 69 74 65 72 46 69 6e 61 6c 69 7a  eafWriterFinaliz
35c45 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  e(fulltext_vtab 
35c46 2a 76 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a  *v, LeafWriter *
35c47 70 57 72 69 74 65 72 29 7b 0a 20 20 73 71 6c 69  pWriter){.  sqli
35c48 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c 6f  te_int64 iEndBlo
35c49 63 6b 69 64 3b 0a 20 20 63 68 61 72 20 2a 70 52  ckid;.  char *pR
35c4a 6f 6f 74 49 6e 66 6f 3b 0a 20 20 69 6e 74 20 72  ootInfo;.  int r
35c4b 63 2c 20 6e 52 6f 6f 74 49 6e 66 6f 3b 0a 0a 20  c, nRootInfo;.. 
35c4c 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72   rc = leafWriter
35c4d 52 6f 6f 74 49 6e 66 6f 28 76 2c 20 70 57 72 69  RootInfo(v, pWri
35c4e 74 65 72 2c 20 26 70 52 6f 6f 74 49 6e 66 6f 2c  ter, &pRootInfo,
35c4f 20 26 6e 52 6f 6f 74 49 6e 66 6f 2c 20 26 69 45   &nRootInfo, &iE
35c50 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66  ndBlockid);.  if
35c51 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
35c52 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
35c53 2f 2a 20 44 6f 6e 27 74 20 62 6f 74 68 65 72 20  /* Don't bother 
35c54 73 74 6f 72 69 6e 67 20 61 6e 20 65 6e 74 69 72  storing an entir
35c55 65 6c 79 20 65 6d 70 74 79 20 73 65 67 6d 65 6e  ely empty segmen
35c56 74 2e 20 2a 2f 0a 20 20 69 66 28 20 69 45 6e 64  t. */.  if( iEnd
35c57 42 6c 6f 63 6b 69 64 3d 3d 30 20 26 26 20 6e 52  Blockid==0 && nR
35c58 6f 6f 74 49 6e 66 6f 3d 3d 30 20 29 20 72 65 74  ootInfo==0 ) ret
35c59 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  urn SQLITE_OK;..
35c5a 20 20 72 65 74 75 72 6e 20 73 65 67 64 69 72 5f    return segdir_
35c5b 73 65 74 28 76 2c 20 70 57 72 69 74 65 72 2d 3e  set(v, pWriter->
35c5c 69 4c 65 76 65 6c 2c 20 70 57 72 69 74 65 72 2d  iLevel, pWriter-
35c5d 3e 69 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20  >idx,.          
35c5e 20 20 20 20 20 20 20 20 20 20 70 57 72 69 74 65            pWrite
35c5f 72 2d 3e 69 53 74 61 72 74 42 6c 6f 63 6b 69 64  r->iStartBlockid
35c60 2c 20 70 57 72 69 74 65 72 2d 3e 69 45 6e 64 42  , pWriter->iEndB
35c61 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20  lockid,.        
35c62 20 20 20 20 20 20 20 20 20 20 20 20 69 45 6e 64              iEnd
35c63 42 6c 6f 63 6b 69 64 2c 20 70 52 6f 6f 74 49 6e  Blockid, pRootIn
35c64 66 6f 2c 20 6e 52 6f 6f 74 49 6e 66 6f 29 3b 0a  fo, nRootInfo);.
35c65 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c  }..static void l
35c66 65 61 66 57 72 69 74 65 72 44 65 73 74 72 6f 79  eafWriterDestroy
35c67 28 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57 72  (LeafWriter *pWr
35c68 69 74 65 72 29 7b 0a 20 20 69 66 28 20 70 57 72  iter){.  if( pWr
35c69 69 74 65 72 2d 3e 68 61 73 5f 70 61 72 65 6e 74  iter->has_parent
35c6a 20 29 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65   ) interiorWrite
35c6b 72 44 65 73 74 72 6f 79 28 26 70 57 72 69 74 65  rDestroy(&pWrite
35c6c 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 29  r->parentWriter)
35c6d 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  ;.  dataBufferDe
35c6e 73 74 72 6f 79 28 26 70 57 72 69 74 65 72 2d 3e  stroy(&pWriter->
35c6f 74 65 72 6d 29 3b 0a 20 20 64 61 74 61 42 75 66  term);.  dataBuf
35c70 66 65 72 44 65 73 74 72 6f 79 28 26 70 57 72 69  ferDestroy(&pWri
35c71 74 65 72 2d 3e 64 61 74 61 29 3b 0a 7d 0a 0a 2f  ter->data);.}../
35c72 2a 20 45 6e 63 6f 64 65 20 61 20 74 65 72 6d 20  * Encode a term 
35c73 69 6e 74 6f 20 74 68 65 20 6c 65 61 66 57 72 69  into the leafWri
35c74 74 65 72 2c 20 64 65 6c 74 61 2d 65 6e 63 6f 64  ter, delta-encod
35c75 69 6e 67 20 61 73 20 61 70 70 72 6f 70 72 69 61  ing as appropria
35c76 74 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 73 20 74  te..** Returns t
35c77 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  he length of the
35c78 20 6e 65 77 20 74 65 72 6d 20 77 68 69 63 68 20   new term which 
35c79 64 69 73 74 69 6e 67 75 69 73 68 65 73 20 69 74  distinguishes it
35c7a 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 70 72 65   from the.** pre
35c7b 76 69 6f 75 73 20 74 65 72 6d 2c 20 77 68 69 63  vious term, whic
35c7c 68 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  h can be used to
35c7d 20 73 65 74 20 6e 54 65 72 6d 44 69 73 74 69 6e   set nTermDistin
35c7e 63 74 20 77 68 65 6e 20 61 20 6e 6f 64 65 0a 2a  ct when a node.*
35c7f 2a 20 62 6f 75 6e 64 61 72 79 20 69 73 20 63 72  * boundary is cr
35c80 6f 73 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ossed..*/.static
35c81 20 69 6e 74 20 6c 65 61 66 57 72 69 74 65 72 45   int leafWriterE
35c82 6e 63 6f 64 65 54 65 72 6d 28 4c 65 61 66 57 72  ncodeTerm(LeafWr
35c83 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20  iter *pWriter,. 
35c84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
35c86 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d  onst char *pTerm
35c87 2c 20 69 6e 74 20 6e 54 65 72 6d 29 7b 0a 20 20  , int nTerm){.  
35c88 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41  char c[VARINT_MA
35c89 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20  X+VARINT_MAX];. 
35c8a 20 69 6e 74 20 6e 2c 20 6e 50 72 65 66 69 78 20   int n, nPrefix 
35c8b 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  = 0;..  assert( 
35c8c 6e 54 65 72 6d 3e 30 20 29 3b 0a 20 20 77 68 69  nTerm>0 );.  whi
35c8d 6c 65 28 20 6e 50 72 65 66 69 78 3c 70 57 72 69  le( nPrefix<pWri
35c8e 74 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 20  ter->term.nData 
35c8f 26 26 0a 20 20 20 20 20 20 20 20 20 70 54 65 72  &&.         pTer
35c90 6d 5b 6e 50 72 65 66 69 78 5d 3d 3d 70 57 72 69  m[nPrefix]==pWri
35c91 74 65 72 2d 3e 74 65 72 6d 2e 70 44 61 74 61 5b  ter->term.pData[
35c92 6e 50 72 65 66 69 78 5d 20 29 7b 0a 20 20 20 20  nPrefix] ){.    
35c93 6e 50 72 65 66 69 78 2b 2b 3b 0a 20 20 20 20 2f  nPrefix++;.    /
35c94 2a 20 46 61 69 6c 69 6e 67 20 74 68 69 73 20 69  * Failing this i
35c95 6d 70 6c 69 65 73 20 74 68 61 74 20 74 68 65 20  mplies that the 
35c96 74 65 72 6d 73 20 77 65 72 65 6e 27 74 20 69 6e  terms weren't in
35c97 20 6f 72 64 65 72 2e 20 2a 2f 0a 20 20 20 20 61   order. */.    a
35c98 73 73 65 72 74 28 20 6e 50 72 65 66 69 78 3c 6e  ssert( nPrefix<n
35c99 54 65 72 6d 20 29 3b 0a 20 20 7d 0a 0a 20 20 69  Term );.  }..  i
35c9a 66 28 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61  f( pWriter->data
35c9b 2e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20  .nData==0 ){.   
35c9c 20 2f 2a 20 45 6e 63 6f 64 65 20 74 68 65 20 6e   /* Encode the n
35c9d 6f 64 65 20 68 65 61 64 65 72 20 61 6e 64 20 6c  ode header and l
35c9e 65 61 64 69 6e 67 20 74 65 72 6d 20 61 73 3a 0a  eading term as:.
35c9f 20 20 20 20 2a 2a 20 20 76 61 72 69 6e 74 28 30      **  varint(0
35ca0 29 0a 20 20 20 20 2a 2a 20 20 76 61 72 69 6e 74  ).    **  varint
35ca1 28 6e 54 65 72 6d 29 0a 20 20 20 20 2a 2a 20 20  (nTerm).    **  
35ca2 63 68 61 72 20 70 54 65 72 6d 5b 6e 54 65 72 6d  char pTerm[nTerm
35ca3 5d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 20 3d  ].    */.    n =
35ca4 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
35ca5 2c 20 27 5c 30 27 29 3b 0a 20 20 20 20 6e 20 2b  , '\0');.    n +
35ca6 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28  = fts3PutVarint(
35ca7 63 2b 6e 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20  c+n, nTerm);.   
35ca8 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
35ca9 64 32 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74  d2(&pWriter->dat
35caa 61 2c 20 63 2c 20 6e 2c 20 70 54 65 72 6d 2c 20  a, c, n, pTerm, 
35cab 6e 54 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b  nTerm);.  }else{
35cac 0a 20 20 20 20 2f 2a 20 44 65 6c 74 61 2d 65 6e  .    /* Delta-en
35cad 63 6f 64 65 20 74 68 65 20 74 65 72 6d 20 61 73  code the term as
35cae 3a 0a 20 20 20 20 2a 2a 20 20 76 61 72 69 6e 74  :.    **  varint
35caf 28 6e 50 72 65 66 69 78 29 0a 20 20 20 20 2a 2a  (nPrefix).    **
35cb0 20 20 76 61 72 69 6e 74 28 6e 53 75 66 66 69 78    varint(nSuffix
35cb1 29 0a 20 20 20 20 2a 2a 20 20 63 68 61 72 20 70  ).    **  char p
35cb2 54 65 72 6d 53 75 66 66 69 78 5b 6e 53 75 66 66  TermSuffix[nSuff
35cb3 69 78 5d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e  ix].    */.    n
35cb4 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74   = fts3PutVarint
35cb5 28 63 2c 20 6e 50 72 65 66 69 78 29 3b 0a 20 20  (c, nPrefix);.  
35cb6 20 20 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61    n += fts3PutVa
35cb7 72 69 6e 74 28 63 2b 6e 2c 20 6e 54 65 72 6d 2d  rint(c+n, nTerm-
35cb8 6e 50 72 65 66 69 78 29 3b 0a 20 20 20 20 64 61  nPrefix);.    da
35cb9 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32 28  taBufferAppend2(
35cba 26 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2c 20  &pWriter->data, 
35cbb 63 2c 20 6e 2c 20 70 54 65 72 6d 2b 6e 50 72 65  c, n, pTerm+nPre
35cbc 66 69 78 2c 20 6e 54 65 72 6d 2d 6e 50 72 65 66  fix, nTerm-nPref
35cbd 69 78 29 3b 0a 20 20 7d 0a 20 20 64 61 74 61 42  ix);.  }.  dataB
35cbe 75 66 66 65 72 52 65 70 6c 61 63 65 28 26 70 57  ufferReplace(&pW
35cbf 72 69 74 65 72 2d 3e 74 65 72 6d 2c 20 70 54 65  riter->term, pTe
35cc0 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20 72  rm, nTerm);..  r
35cc1 65 74 75 72 6e 20 6e 50 72 65 66 69 78 2b 31 3b  eturn nPrefix+1;
35cc2 0a 7d 0a 0a 2f 2a 20 55 73 65 64 20 74 6f 20 61  .}../* Used to a
35cc3 76 6f 69 64 20 61 20 6d 65 6d 6d 6f 76 65 20 77  void a memmove w
35cc4 68 65 6e 20 61 20 6c 61 72 67 65 20 61 6d 6f 75  hen a large amou
35cc5 6e 74 20 6f 66 20 64 6f 63 6c 69 73 74 20 64 61  nt of doclist da
35cc6 74 61 20 69 73 20 69 6e 0a 2a 2a 20 74 68 65 20  ta is in.** the 
35cc7 62 75 66 66 65 72 2e 20 20 54 68 69 73 20 63 6f  buffer.  This co
35cc8 6e 73 74 72 75 63 74 73 20 61 20 6e 6f 64 65 20  nstructs a node 
35cc9 61 6e 64 20 74 65 72 6d 20 68 65 61 64 65 72 20  and term header 
35cca 62 65 66 6f 72 65 0a 2a 2a 20 69 44 6f 63 6c 69  before.** iDocli
35ccb 73 74 44 61 74 61 20 61 6e 64 20 66 6c 75 73 68  stData and flush
35ccc 65 73 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67  es the resulting
35ccd 20 63 6f 6d 70 6c 65 74 65 20 6e 6f 64 65 20 75   complete node u
35cce 73 69 6e 67 0a 2a 2a 20 6c 65 61 66 57 72 69 74  sing.** leafWrit
35ccf 65 72 49 6e 74 65 72 6e 61 6c 46 6c 75 73 68 28  erInternalFlush(
35cd0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
35cd1 20 6c 65 61 66 57 72 69 74 65 72 49 6e 6c 69 6e   leafWriterInlin
35cd2 65 46 6c 75 73 68 28 66 75 6c 6c 74 65 78 74 5f  eFlush(fulltext_
35cd3 76 74 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69  vtab *v, LeafWri
35cd4 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20  ter *pWriter,.  
35cd5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35cd6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
35cd7 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d  onst char *pTerm
35cd8 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 0a 20 20 20  , int nTerm,.   
35cd9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35cda 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
35cdb 74 20 69 44 6f 63 6c 69 73 74 44 61 74 61 29 7b  t iDoclistData){
35cdc 0a 20 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54  .  char c[VARINT
35cdd 5f 4d 41 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d  _MAX+VARINT_MAX]
35cde 3b 0a 20 20 69 6e 74 20 69 44 61 74 61 2c 20 6e  ;.  int iData, n
35cdf 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74   = fts3PutVarint
35ce0 28 63 2c 20 30 29 3b 0a 20 20 6e 20 2b 3d 20 66  (c, 0);.  n += f
35ce1 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e  ts3PutVarint(c+n
35ce2 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20 2f 2a 20  , nTerm);..  /* 
35ce3 54 68 65 72 65 20 73 68 6f 75 6c 64 20 61 6c 77  There should alw
35ce4 61 79 73 20 62 65 20 72 6f 6f 6d 20 66 6f 72 20  ays be room for 
35ce5 74 68 65 20 68 65 61 64 65 72 2e 20 20 45 76 65  the header.  Eve
35ce6 6e 20 69 66 20 70 54 65 72 6d 20 73 68 61 72 65  n if pTerm share
35ce7 64 0a 20 20 2a 2a 20 61 20 73 75 62 73 74 61 6e  d.  ** a substan
35ce8 74 69 61 6c 20 70 72 65 66 69 78 20 77 69 74 68  tial prefix with
35ce9 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 74 65   the previous te
35cea 72 6d 2c 20 74 68 65 20 65 6e 74 69 72 65 20 70  rm, the entire p
35ceb 72 65 66 69 78 0a 20 20 2a 2a 20 63 6f 75 6c 64  refix.  ** could
35cec 20 62 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20   be constructed 
35ced 66 72 6f 6d 20 65 61 72 6c 69 65 72 20 64 61 74  from earlier dat
35cee 61 20 69 6e 20 74 68 65 20 64 6f 63 6c 69 73 74  a in the doclist
35cef 2c 20 73 6f 20 74 68 65 72 65 0a 20 20 2a 2a 20  , so there.  ** 
35cf0 73 68 6f 75 6c 64 20 62 65 20 72 6f 6f 6d 2e 0a  should be room..
35cf1 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69    */.  assert( i
35cf2 44 6f 63 6c 69 73 74 44 61 74 61 3e 3d 6e 2b 6e  DoclistData>=n+n
35cf3 54 65 72 6d 20 29 3b 0a 0a 20 20 69 44 61 74 61  Term );..  iData
35cf4 20 3d 20 69 44 6f 63 6c 69 73 74 44 61 74 61 2d   = iDoclistData-
35cf5 28 6e 2b 6e 54 65 72 6d 29 3b 0a 20 20 6d 65 6d  (n+nTerm);.  mem
35cf6 63 70 79 28 70 57 72 69 74 65 72 2d 3e 64 61 74  cpy(pWriter->dat
35cf7 61 2e 70 44 61 74 61 2b 69 44 61 74 61 2c 20 63  a.pData+iData, c
35cf8 2c 20 6e 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70  , n);.  memcpy(p
35cf9 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61  Writer->data.pDa
35cfa 74 61 2b 69 44 61 74 61 2b 6e 2c 20 70 54 65 72  ta+iData+n, pTer
35cfb 6d 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20 72 65  m, nTerm);..  re
35cfc 74 75 72 6e 20 6c 65 61 66 57 72 69 74 65 72 49  turn leafWriterI
35cfd 6e 74 65 72 6e 61 6c 46 6c 75 73 68 28 76 2c 20  nternalFlush(v, 
35cfe 70 57 72 69 74 65 72 2c 20 69 44 61 74 61 2c 20  pWriter, iData, 
35cff 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44  pWriter->data.nD
35d00 61 74 61 2d 69 44 61 74 61 29 3b 0a 7d 0a 0a 2f  ata-iData);.}../
35d01 2a 20 50 75 73 68 20 70 54 65 72 6d 5b 6e 54 65  * Push pTerm[nTe
35d02 72 6d 5d 20 61 6c 6f 6e 67 20 77 69 74 68 20 74  rm] along with t
35d03 68 65 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20  he doclist data 
35d04 74 6f 20 74 68 65 20 6c 65 61 66 20 6c 61 79 65  to the leaf laye
35d05 72 20 6f 66 0a 2a 2a 20 25 5f 73 65 67 6d 65 6e  r of.** %_segmen
35d06 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ts..*/.static in
35d07 74 20 6c 65 61 66 57 72 69 74 65 72 53 74 65 70  t leafWriterStep
35d08 4d 65 72 67 65 28 66 75 6c 6c 74 65 78 74 5f 76  Merge(fulltext_v
35d09 74 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69 74  tab *v, LeafWrit
35d0a 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20  er *pWriter,.   
35d0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35d0c 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
35d0d 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69  t char *pTerm, i
35d0e 6e 74 20 6e 54 65 72 6d 2c 0a 20 20 20 20 20 20  nt nTerm,.      
35d0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35d10 20 20 20 20 20 20 20 20 20 44 4c 52 65 61 64 65           DLReade
35d11 72 20 2a 70 52 65 61 64 65 72 73 2c 20 69 6e 74  r *pReaders, int
35d12 20 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 63 68   nReaders){.  ch
35d13 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b  ar c[VARINT_MAX+
35d14 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69  VARINT_MAX];.  i
35d15 6e 74 20 69 54 65 72 6d 44 61 74 61 20 3d 20 70  nt iTermData = p
35d16 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61  Writer->data.nDa
35d17 74 61 2c 20 69 44 6f 63 6c 69 73 74 44 61 74 61  ta, iDoclistData
35d18 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 44 61 74 61  ;.  int i, nData
35d19 2c 20 6e 2c 20 6e 41 63 74 75 61 6c 44 61 74 61  , n, nActualData
35d1a 2c 20 6e 41 63 74 75 61 6c 2c 20 72 63 2c 20 6e  , nActual, rc, n
35d1b 54 65 72 6d 44 69 73 74 69 6e 63 74 3b 0a 0a 20  TermDistinct;.. 
35d1c 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 4c 45   ASSERT_VALID_LE
35d1d 41 46 5f 4e 4f 44 45 28 70 57 72 69 74 65 72 2d  AF_NODE(pWriter-
35d1e 3e 64 61 74 61 2e 70 44 61 74 61 2c 20 70 57 72  >data.pData, pWr
35d1f 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61  iter->data.nData
35d20 29 3b 0a 20 20 6e 54 65 72 6d 44 69 73 74 69 6e  );.  nTermDistin
35d21 63 74 20 3d 20 6c 65 61 66 57 72 69 74 65 72 45  ct = leafWriterE
35d22 6e 63 6f 64 65 54 65 72 6d 28 70 57 72 69 74 65  ncodeTerm(pWrite
35d23 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29  r, pTerm, nTerm)
35d24 3b 0a 0a 20 20 2f 2a 20 52 65 6d 65 6d 62 65 72  ;..  /* Remember
35d25 20 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 20 69   nTermDistinct i
35d26 66 20 6f 70 65 6e 69 6e 67 20 61 20 6e 65 77 20  f opening a new 
35d27 6e 6f 64 65 2e 20 2a 2f 0a 20 20 69 66 28 20 69  node. */.  if( i
35d28 54 65 72 6d 44 61 74 61 3d 3d 30 20 29 20 70 57  TermData==0 ) pW
35d29 72 69 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74  riter->nTermDist
35d2a 69 6e 63 74 20 3d 20 6e 54 65 72 6d 44 69 73 74  inct = nTermDist
35d2b 69 6e 63 74 3b 0a 0a 20 20 69 44 6f 63 6c 69 73  inct;..  iDoclis
35d2c 74 44 61 74 61 20 3d 20 70 57 72 69 74 65 72 2d  tData = pWriter-
35d2d 3e 64 61 74 61 2e 6e 44 61 74 61 3b 0a 0a 20 20  >data.nData;..  
35d2e 2f 2a 20 45 73 74 69 6d 61 74 65 20 74 68 65 20  /* Estimate the 
35d2f 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 6d 65  length of the me
35d30 72 67 65 64 20 64 6f 63 6c 69 73 74 20 73 6f 20  rged doclist so 
35d31 77 65 20 63 61 6e 20 6c 65 61 76 65 20 73 70 61  we can leave spa
35d32 63 65 0a 20 20 2a 2a 20 74 6f 20 65 6e 63 6f 64  ce.  ** to encod
35d33 65 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  e it..  */.  for
35d34 28 69 3d 30 2c 20 6e 44 61 74 61 3d 30 3b 20 69  (i=0, nData=0; i
35d35 3c 6e 52 65 61 64 65 72 73 3b 20 69 2b 2b 29 7b  <nReaders; i++){
35d36 0a 20 20 20 20 6e 44 61 74 61 20 2b 3d 20 64 6c  .    nData += dl
35d37 72 41 6c 6c 44 61 74 61 42 79 74 65 73 28 26 70  rAllDataBytes(&p
35d38 52 65 61 64 65 72 73 5b 69 5d 29 3b 0a 20 20 7d  Readers[i]);.  }
35d39 0a 20 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61  .  n = fts3PutVa
35d3a 72 69 6e 74 28 63 2c 20 6e 44 61 74 61 29 3b 0a  rint(c, nData);.
35d3b 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
35d3c 6e 64 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74  nd(&pWriter->dat
35d3d 61 2c 20 63 2c 20 6e 29 3b 0a 0a 20 20 64 6f 63  a, c, n);..  doc
35d3e 4c 69 73 74 4d 65 72 67 65 28 26 70 57 72 69 74  ListMerge(&pWrit
35d3f 65 72 2d 3e 64 61 74 61 2c 20 70 52 65 61 64 65  er->data, pReade
35d40 72 73 2c 20 6e 52 65 61 64 65 72 73 29 3b 0a 20  rs, nReaders);. 
35d41 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f   ASSERT_VALID_DO
35d42 43 4c 49 53 54 28 44 4c 5f 44 45 46 41 55 4c 54  CLIST(DL_DEFAULT
35d43 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
35d44 20 20 20 20 20 20 20 20 20 70 57 72 69 74 65 72           pWriter
35d45 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f  ->data.pData+iDo
35d46 63 6c 69 73 74 44 61 74 61 2b 6e 2c 0a 20 20 20  clistData+n,.   
35d47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35d48 20 20 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74      pWriter->dat
35d49 61 2e 6e 44 61 74 61 2d 69 44 6f 63 6c 69 73 74  a.nData-iDoclist
35d4a 44 61 74 61 2d 6e 2c 20 4e 55 4c 4c 29 3b 0a 0a  Data-n, NULL);..
35d4b 20 20 2f 2a 20 54 68 65 20 61 63 74 75 61 6c 20    /* The actual 
35d4c 61 6d 6f 75 6e 74 20 6f 66 20 64 6f 63 6c 69 73  amount of doclis
35d4d 74 20 64 61 74 61 20 61 74 20 74 68 69 73 20 70  t data at this p
35d4e 6f 69 6e 74 20 63 6f 75 6c 64 20 62 65 20 73 6d  oint could be sm
35d4f 61 6c 6c 65 72 0a 20 20 2a 2a 20 74 68 61 6e 20  aller.  ** than 
35d50 74 68 65 20 6c 65 6e 67 74 68 20 77 65 20 65 6e  the length we en
35d51 63 6f 64 65 64 2e 20 20 41 64 64 69 74 69 6f 6e  coded.  Addition
35d52 61 6c 6c 79 2c 20 74 68 65 20 73 70 61 63 65 20  ally, the space 
35d53 72 65 71 75 69 72 65 64 20 74 6f 0a 20 20 2a 2a  required to.  **
35d54 20 65 6e 63 6f 64 65 20 74 68 69 73 20 6c 65 6e   encode this len
35d55 67 74 68 20 63 6f 75 6c 64 20 62 65 20 73 6d 61  gth could be sma
35d56 6c 6c 65 72 2e 20 20 46 6f 72 20 73 6d 61 6c 6c  ller.  For small
35d57 20 64 6f 63 6c 69 73 74 73 2c 20 74 68 69 73 20   doclists, this 
35d58 69 73 0a 20 20 2a 2a 20 6e 6f 74 20 61 20 62 69  is.  ** not a bi
35d59 67 20 64 65 61 6c 2c 20 77 65 20 63 61 6e 20 6a  g deal, we can j
35d5a 75 73 74 20 75 73 65 20 6d 65 6d 6d 6f 76 65 28  ust use memmove(
35d5b 29 20 74 6f 20 61 64 6a 75 73 74 20 74 68 69 6e  ) to adjust thin
35d5c 67 73 2e 0a 20 20 2a 2f 0a 20 20 6e 41 63 74 75  gs..  */.  nActu
35d5d 61 6c 44 61 74 61 20 3d 20 70 57 72 69 74 65 72  alData = pWriter
35d5e 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2d 28 69 44  ->data.nData-(iD
35d5f 6f 63 6c 69 73 74 44 61 74 61 2b 6e 29 3b 0a 20  oclistData+n);. 
35d60 20 6e 41 63 74 75 61 6c 20 3d 20 66 74 73 33 50   nActual = fts3P
35d61 75 74 56 61 72 69 6e 74 28 63 2c 20 6e 41 63 74  utVarint(c, nAct
35d62 75 61 6c 44 61 74 61 29 3b 0a 20 20 61 73 73 65  ualData);.  asse
35d63 72 74 28 20 6e 41 63 74 75 61 6c 44 61 74 61 3c  rt( nActualData<
35d64 3d 6e 44 61 74 61 20 29 3b 0a 20 20 61 73 73 65  =nData );.  asse
35d65 72 74 28 20 6e 41 63 74 75 61 6c 3c 3d 6e 20 29  rt( nActual<=n )
35d66 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6e  ;..  /* If the n
35d67 65 77 20 64 6f 63 6c 69 73 74 20 69 73 20 62 69  ew doclist is bi
35d68 67 20 65 6e 6f 75 67 68 20 66 6f 72 20 66 6f 72  g enough for for
35d69 63 65 20 61 20 73 74 61 6e 64 61 6c 6f 6e 65 20  ce a standalone 
35d6a 6c 65 61 66 0a 20 20 2a 2a 20 6e 6f 64 65 2c 20  leaf.  ** node, 
35d6b 77 65 20 63 61 6e 20 69 6d 6d 65 64 69 61 74 65  we can immediate
35d6c 6c 79 20 66 6c 75 73 68 20 69 74 20 69 6e 6c 69  ly flush it inli
35d6d 6e 65 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67  ne without doing
35d6e 20 74 68 65 0a 20 20 2a 2a 20 6d 65 6d 6d 6f 76   the.  ** memmov
35d6f 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 2f 2a 20 54  e()..  */.  /* T
35d70 4f 44 4f 28 73 68 65 73 73 29 20 54 68 69 73 20  ODO(shess) This 
35d71 74 65 73 74 20 6d 61 74 63 68 65 73 20 6c 65 61  test matches lea
35d72 66 57 72 69 74 65 72 53 74 65 70 28 29 2c 20 77  fWriterStep(), w
35d73 68 69 63 68 20 64 6f 65 73 20 74 68 69 73 0a 20  hich does this. 
35d74 20 2a 2a 20 74 65 73 74 20 62 65 66 6f 72 65 20   ** test before 
35d75 69 74 20 6b 6e 6f 77 73 20 74 68 65 20 63 6f 73  it knows the cos
35d76 74 20 74 6f 20 76 61 72 69 6e 74 2d 65 6e 63 6f  t to varint-enco
35d77 64 65 20 74 68 65 20 74 65 72 6d 20 61 6e 64 0a  de the term and.
35d78 20 20 2a 2a 20 64 6f 63 6c 69 73 74 20 6c 65 6e    ** doclist len
35d79 67 74 68 73 2e 20 20 41 74 20 73 6f 6d 65 20 70  gths.  At some p
35d7a 6f 69 6e 74 2c 20 63 68 61 6e 67 65 20 74 6f 0a  oint, change to.
35d7b 20 20 2a 2a 20 70 57 72 69 74 65 72 2d 3e 64 61    ** pWriter->da
35d7c 74 61 2e 6e 44 61 74 61 2d 69 54 65 72 6d 44 61  ta.nData-iTermDa
35d7d 74 61 3e 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49  ta>STANDALONE_MI
35d7e 4e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 54  N..  */.  if( nT
35d7f 65 72 6d 2b 6e 41 63 74 75 61 6c 44 61 74 61 3e  erm+nActualData>
35d80 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 29  STANDALONE_MIN )
35d81 7b 0a 20 20 20 20 2f 2a 20 50 75 73 68 20 6c 65  {.    /* Push le
35d82 61 66 20 6e 6f 64 65 20 66 72 6f 6d 20 62 65 66  af node from bef
35d83 6f 72 65 20 74 68 69 73 20 74 65 72 6d 2e 20 2a  ore this term. *
35d84 2f 0a 20 20 20 20 69 66 28 20 69 54 65 72 6d 44  /.    if( iTermD
35d85 61 74 61 3e 30 20 29 7b 0a 20 20 20 20 20 20 72  ata>0 ){.      r
35d86 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 49 6e  c = leafWriterIn
35d87 74 65 72 6e 61 6c 46 6c 75 73 68 28 76 2c 20 70  ternalFlush(v, p
35d88 57 72 69 74 65 72 2c 20 30 2c 20 69 54 65 72 6d  Writer, 0, iTerm
35d89 44 61 74 61 29 3b 0a 20 20 20 20 20 20 69 66 28  Data);.      if(
35d8a 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
35d8b 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20   return rc;..   
35d8c 20 20 20 70 57 72 69 74 65 72 2d 3e 6e 54 65 72     pWriter->nTer
35d8d 6d 44 69 73 74 69 6e 63 74 20 3d 20 6e 54 65 72  mDistinct = nTer
35d8e 6d 44 69 73 74 69 6e 63 74 3b 0a 20 20 20 20 7d  mDistinct;.    }
35d8f 0a 0a 20 20 20 20 2f 2a 20 46 69 78 20 74 68 65  ..    /* Fix the
35d90 20 65 6e 63 6f 64 65 64 20 64 6f 63 6c 69 73 74   encoded doclist
35d91 20 6c 65 6e 67 74 68 2e 20 2a 2f 0a 20 20 20 20   length. */.    
35d92 69 44 6f 63 6c 69 73 74 44 61 74 61 20 2b 3d 20  iDoclistData += 
35d93 6e 20 2d 20 6e 41 63 74 75 61 6c 3b 0a 20 20 20  n - nActual;.   
35d94 20 6d 65 6d 63 70 79 28 70 57 72 69 74 65 72 2d   memcpy(pWriter-
35d95 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f 63  >data.pData+iDoc
35d96 6c 69 73 74 44 61 74 61 2c 20 63 2c 20 6e 41 63  listData, c, nAc
35d97 74 75 61 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 50  tual);..    /* P
35d98 75 73 68 20 74 68 65 20 73 74 61 6e 64 61 6c 6f  ush the standalo
35d99 6e 65 20 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f  ne leaf node. */
35d9a 0a 20 20 20 20 72 63 20 3d 20 6c 65 61 66 57 72  .    rc = leafWr
35d9b 69 74 65 72 49 6e 6c 69 6e 65 46 6c 75 73 68 28  iterInlineFlush(
35d9c 76 2c 20 70 57 72 69 74 65 72 2c 20 70 54 65 72  v, pWriter, pTer
35d9d 6d 2c 20 6e 54 65 72 6d 2c 20 69 44 6f 63 6c 69  m, nTerm, iDocli
35d9e 73 74 44 61 74 61 29 3b 0a 20 20 20 20 69 66 28  stData);.    if(
35d9f 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
35da0 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20   return rc;..   
35da1 20 2f 2a 20 4c 65 61 76 65 20 74 68 65 20 6e 6f   /* Leave the no
35da2 64 65 20 65 6d 70 74 79 2e 20 2a 2f 0a 20 20 20  de empty. */.   
35da3 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74   dataBufferReset
35da4 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74 61 29  (&pWriter->data)
35da5 3b 0a 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ;..    return rc
35da6 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74  ;.  }..  /* At t
35da7 68 69 73 20 70 6f 69 6e 74 2c 20 77 65 20 6b 6e  his point, we kn
35da8 6f 77 20 74 68 61 74 20 74 68 65 20 64 6f 63 6c  ow that the docl
35da9 69 73 74 20 77 61 73 20 73 6d 61 6c 6c 2c 20 73  ist was small, s
35daa 6f 20 64 6f 20 74 68 65 0a 20 20 2a 2a 20 6d 65  o do the.  ** me
35dab 6d 6d 6f 76 65 20 69 66 20 69 6e 64 69 63 61 74  mmove if indicat
35dac 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e  ed..  */.  if( n
35dad 41 63 74 75 61 6c 3c 6e 20 29 7b 0a 20 20 20 20  Actual<n ){.    
35dae 6d 65 6d 6d 6f 76 65 28 70 57 72 69 74 65 72 2d  memmove(pWriter-
35daf 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f 63  >data.pData+iDoc
35db0 6c 69 73 74 44 61 74 61 2b 6e 41 63 74 75 61 6c  listData+nActual
35db1 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 57  ,.            pW
35db2 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74  riter->data.pDat
35db3 61 2b 69 44 6f 63 6c 69 73 74 44 61 74 61 2b 6e  a+iDoclistData+n
35db4 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 57  ,.            pW
35db5 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
35db6 61 2d 28 69 44 6f 63 6c 69 73 74 44 61 74 61 2b  a-(iDoclistData+
35db7 6e 29 29 3b 0a 20 20 20 20 70 57 72 69 74 65 72  n));.    pWriter
35db8 2d 3e 64 61 74 61 2e 6e 44 61 74 61 20 2d 3d 20  ->data.nData -= 
35db9 6e 2d 6e 41 63 74 75 61 6c 3b 0a 20 20 7d 0a 0a  n-nActual;.  }..
35dba 20 20 2f 2a 20 52 65 70 6c 61 63 65 20 77 72 69    /* Replace wri
35dbb 74 74 65 6e 20 6c 65 6e 67 74 68 20 77 69 74 68  tten length with
35dbc 20 61 63 74 75 61 6c 20 6c 65 6e 67 74 68 2e 20   actual length. 
35dbd 2a 2f 0a 20 20 6d 65 6d 63 70 79 28 70 57 72 69  */.  memcpy(pWri
35dbe 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b  ter->data.pData+
35dbf 69 44 6f 63 6c 69 73 74 44 61 74 61 2c 20 63 2c  iDoclistData, c,
35dc0 20 6e 41 63 74 75 61 6c 29 3b 0a 0a 20 20 2f 2a   nActual);..  /*
35dc1 20 49 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20   If the node is 
35dc2 74 6f 6f 20 6c 61 72 67 65 2c 20 62 72 65 61 6b  too large, break
35dc3 20 74 68 69 6e 67 73 20 75 70 2e 20 2a 2f 0a 20   things up. */. 
35dc4 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20   /* TODO(shess) 
35dc5 54 68 69 73 20 74 65 73 74 20 6d 61 74 63 68 65  This test matche
35dc6 73 20 6c 65 61 66 57 72 69 74 65 72 53 74 65 70  s leafWriterStep
35dc7 28 29 2c 20 77 68 69 63 68 20 64 6f 65 73 20 74  (), which does t
35dc8 68 69 73 0a 20 20 2a 2a 20 74 65 73 74 20 62 65  his.  ** test be
35dc9 66 6f 72 65 20 69 74 20 6b 6e 6f 77 73 20 74 68  fore it knows th
35dca 65 20 63 6f 73 74 20 74 6f 20 76 61 72 69 6e 74  e cost to varint
35dcb 2d 65 6e 63 6f 64 65 20 74 68 65 20 74 65 72 6d  -encode the term
35dcc 20 61 6e 64 0a 20 20 2a 2a 20 64 6f 63 6c 69 73   and.  ** doclis
35dcd 74 20 6c 65 6e 67 74 68 73 2e 20 20 41 74 20 73  t lengths.  At s
35dce 6f 6d 65 20 70 6f 69 6e 74 2c 20 63 68 61 6e 67  ome point, chang
35dcf 65 20 74 6f 0a 20 20 2a 2a 20 70 57 72 69 74 65  e to.  ** pWrite
35dd0 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3e 4c 45  r->data.nData>LE
35dd1 41 46 5f 4d 41 58 2e 0a 20 20 2a 2f 0a 20 20 69  AF_MAX..  */.  i
35dd2 66 28 20 69 54 65 72 6d 44 61 74 61 2b 6e 54 65  f( iTermData+nTe
35dd3 72 6d 2b 6e 41 63 74 75 61 6c 44 61 74 61 3e 4c  rm+nActualData>L
35dd4 45 41 46 5f 4d 41 58 20 29 7b 0a 20 20 20 20 2f  EAF_MAX ){.    /
35dd5 2a 20 46 6c 75 73 68 20 6f 75 74 20 74 68 65 20  * Flush out the 
35dd6 6c 65 61 64 69 6e 67 20 64 61 74 61 20 61 73 20  leading data as 
35dd7 61 20 6e 6f 64 65 20 2a 2f 0a 20 20 20 20 72 63  a node */.    rc
35dd8 20 3d 20 6c 65 61 66 57 72 69 74 65 72 49 6e 74   = leafWriterInt
35dd9 65 72 6e 61 6c 46 6c 75 73 68 28 76 2c 20 70 57  ernalFlush(v, pW
35dda 72 69 74 65 72 2c 20 30 2c 20 69 54 65 72 6d 44  riter, 0, iTermD
35ddb 61 74 61 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ata);.    if( rc
35ddc 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
35ddd 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 70 57  turn rc;..    pW
35dde 72 69 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74  riter->nTermDist
35ddf 69 6e 63 74 20 3d 20 6e 54 65 72 6d 44 69 73 74  inct = nTermDist
35de0 69 6e 63 74 3b 0a 0a 20 20 20 20 2f 2a 20 52 65  inct;..    /* Re
35de1 62 75 69 6c 64 20 68 65 61 64 65 72 20 75 73 69  build header usi
35de2 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  ng the current t
35de3 65 72 6d 20 2a 2f 0a 20 20 20 20 6e 20 3d 20 66  erm */.    n = f
35de4 74 73 33 50 75 74 56 61 72 69 6e 74 28 70 57 72  ts3PutVarint(pWr
35de5 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61  iter->data.pData
35de6 2c 20 30 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66  , 0);.    n += f
35de7 74 73 33 50 75 74 56 61 72 69 6e 74 28 70 57 72  ts3PutVarint(pWr
35de8 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61  iter->data.pData
35de9 2b 6e 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20 20  +n, nTerm);.    
35dea 6d 65 6d 63 70 79 28 70 57 72 69 74 65 72 2d 3e  memcpy(pWriter->
35deb 64 61 74 61 2e 70 44 61 74 61 2b 6e 2c 20 70 54  data.pData+n, pT
35dec 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20  erm, nTerm);.   
35ded 20 6e 20 2b 3d 20 6e 54 65 72 6d 3b 0a 0a 20 20   n += nTerm;..  
35dee 20 20 2f 2a 20 54 68 65 72 65 20 73 68 6f 75 6c    /* There shoul
35def 64 20 61 6c 77 61 79 73 20 62 65 20 72 6f 6f 6d  d always be room
35df0 2c 20 62 65 63 61 75 73 65 20 74 68 65 20 70 72  , because the pr
35df1 65 76 69 6f 75 73 20 65 6e 63 6f 64 69 6e 67 0a  evious encoding.
35df2 20 20 20 20 2a 2a 20 69 6e 63 6c 75 64 65 64 20      ** included 
35df3 61 6c 6c 20 64 61 74 61 20 6e 65 63 65 73 73 61  all data necessa
35df4 72 79 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20  ry to construct 
35df5 74 68 65 20 74 65 72 6d 2e 0a 20 20 20 20 2a 2f  the term..    */
35df6 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 69  .    assert( n<i
35df7 44 6f 63 6c 69 73 74 44 61 74 61 20 29 3b 0a 20  DoclistData );. 
35df8 20 20 20 2f 2a 20 53 6f 20 6c 6f 6e 67 20 61 73     /* So long as
35df9 20 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20   STANDALONE_MIN 
35dfa 69 73 20 68 61 6c 66 20 6f 72 20 6c 65 73 73 20  is half or less 
35dfb 6f 66 20 4c 45 41 46 5f 4d 41 58 2c 20 74 68 65  of LEAF_MAX, the
35dfc 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e  .    ** followin
35dfd 67 20 6d 65 6d 63 70 79 28 29 20 69 73 20 73 61  g memcpy() is sa
35dfe 66 65 20 28 61 73 20 6f 70 70 6f 73 65 64 20 74  fe (as opposed t
35dff 6f 20 6e 65 65 64 69 6e 67 20 61 20 6d 65 6d 6d  o needing a memm
35e00 6f 76 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ove)..    */.   
35e01 20 61 73 73 65 72 74 28 20 32 2a 53 54 41 4e 44   assert( 2*STAND
35e02 41 4c 4f 4e 45 5f 4d 49 4e 3c 3d 4c 45 41 46 5f  ALONE_MIN<=LEAF_
35e03 4d 41 58 20 29 3b 0a 20 20 20 20 61 73 73 65 72  MAX );.    asser
35e04 74 28 20 6e 2b 70 57 72 69 74 65 72 2d 3e 64 61  t( n+pWriter->da
35e05 74 61 2e 6e 44 61 74 61 2d 69 44 6f 63 6c 69 73  ta.nData-iDoclis
35e06 74 44 61 74 61 3c 69 44 6f 63 6c 69 73 74 44 61  tData<iDoclistDa
35e07 74 61 20 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  ta );.    memcpy
35e08 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70  (pWriter->data.p
35e09 44 61 74 61 2b 6e 2c 0a 20 20 20 20 20 20 20 20  Data+n,.        
35e0a 20 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61     pWriter->data
35e0b 2e 70 44 61 74 61 2b 69 44 6f 63 6c 69 73 74 44  .pData+iDoclistD
35e0c 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
35e0d 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44  pWriter->data.nD
35e0e 61 74 61 2d 69 44 6f 63 6c 69 73 74 44 61 74 61  ata-iDoclistData
35e0f 29 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  );.    pWriter->
35e10 64 61 74 61 2e 6e 44 61 74 61 20 2d 3d 20 69 44  data.nData -= iD
35e11 6f 63 6c 69 73 74 44 61 74 61 2d 6e 3b 0a 20 20  oclistData-n;.  
35e12 7d 0a 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44  }.  ASSERT_VALID
35e13 5f 4c 45 41 46 5f 4e 4f 44 45 28 70 57 72 69 74  _LEAF_NODE(pWrit
35e14 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2c 20  er->data.pData, 
35e15 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44  pWriter->data.nD
35e16 61 74 61 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  ata);..  return 
35e17 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
35e18 20 50 75 73 68 20 70 54 65 72 6d 5b 6e 54 65 72   Push pTerm[nTer
35e19 6d 5d 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68  m] along with th
35e1a 65 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 74  e doclist data t
35e1b 6f 20 74 68 65 20 6c 65 61 66 20 6c 61 79 65 72  o the leaf layer
35e1c 20 6f 66 0a 2a 2a 20 25 5f 73 65 67 6d 65 6e 74   of.** %_segment
35e1d 73 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68  s..*/./* TODO(sh
35e1e 65 73 73 29 20 52 65 76 69 73 65 20 77 72 69 74  ess) Revise writ
35e1f 65 5a 65 72 6f 53 65 67 6d 65 6e 74 28 29 20 73  eZeroSegment() s
35e20 6f 20 74 68 61 74 20 64 6f 63 6c 69 73 74 73 20  o that doclists 
35e21 61 72 65 0a 2a 2a 20 63 6f 6e 73 74 72 75 63 74  are.** construct
35e22 65 64 20 64 69 72 65 63 74 6c 79 20 69 6e 20 70  ed directly in p
35e23 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 0a 2a 2f  Writer->data..*/
35e24 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66  .static int leaf
35e25 57 72 69 74 65 72 53 74 65 70 28 66 75 6c 6c 74  WriterStep(fullt
35e26 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61  ext_vtab *v, Lea
35e27 66 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  fWriter *pWriter
35e28 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
35e29 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
35e2a 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69  t char *pTerm, i
35e2b 6e 74 20 6e 54 65 72 6d 2c 0a 20 20 20 20 20 20  nt nTerm,.      
35e2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35e2d 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
35e2e 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61  pData, int nData
35e2f 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 44  ){.  int rc;.  D
35e30 4c 52 65 61 64 65 72 20 72 65 61 64 65 72 3b 0a  LReader reader;.
35e31 0a 20 20 64 6c 72 49 6e 69 74 28 26 72 65 61 64  .  dlrInit(&read
35e32 65 72 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20  er, DL_DEFAULT, 
35e33 70 44 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 20  pData, nData);. 
35e34 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72   rc = leafWriter
35e35 53 74 65 70 4d 65 72 67 65 28 76 2c 20 70 57 72  StepMerge(v, pWr
35e36 69 74 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65  iter, pTerm, nTe
35e37 72 6d 2c 20 26 72 65 61 64 65 72 2c 20 31 29 3b  rm, &reader, 1);
35e38 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72  .  dlrDestroy(&r
35e39 65 61 64 65 72 29 3b 0a 0a 20 20 72 65 74 75 72  eader);..  retur
35e3a 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a  n rc;.}.../*****
35e3b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35e3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35e3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35e3e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20  ***********/./* 
35e3f 4c 65 61 66 52 65 61 64 65 72 20 69 73 20 75 73  LeafReader is us
35e40 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 6f 76  ed to iterate ov
35e41 65 72 20 61 6e 20 69 6e 64 69 76 69 64 75 61 6c  er an individual
35e42 20 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a 74   leaf node. */.t
35e43 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4c 65  ypedef struct Le
35e44 61 66 52 65 61 64 65 72 20 7b 0a 20 20 44 61 74  afReader {.  Dat
35e45 61 42 75 66 66 65 72 20 74 65 72 6d 3b 20 20 20  aBuffer term;   
35e46 20 20 20 20 20 20 20 2f 2a 20 63 6f 70 79 20 6f         /* copy o
35e47 66 20 63 75 72 72 65 6e 74 20 74 65 72 6d 2e 20  f current term. 
35e48 2a 2f 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  */..  const char
35e49 20 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 20   *pData;        
35e4a 2f 2a 20 64 61 74 61 20 66 6f 72 20 63 75 72 72  /* data for curr
35e4b 65 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 69  ent term. */.  i
35e4c 6e 74 20 6e 44 61 74 61 3b 0a 7d 20 4c 65 61 66  nt nData;.} Leaf
35e4d 52 65 61 64 65 72 3b 0a 0a 73 74 61 74 69 63 20  Reader;..static 
35e4e 76 6f 69 64 20 6c 65 61 66 52 65 61 64 65 72 44  void leafReaderD
35e4f 65 73 74 72 6f 79 28 4c 65 61 66 52 65 61 64 65  estroy(LeafReade
35e50 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 64  r *pReader){.  d
35e51 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
35e52 28 26 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 29  (&pReader->term)
35e53 3b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 52 65  ;.  SCRAMBLE(pRe
35e54 61 64 65 72 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  ader);.}..static
35e55 20 69 6e 74 20 6c 65 61 66 52 65 61 64 65 72 41   int leafReaderA
35e56 74 45 6e 64 28 4c 65 61 66 52 65 61 64 65 72 20  tEnd(LeafReader 
35e57 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74  *pReader){.  ret
35e58 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 6e 44 61  urn pReader->nDa
35e59 74 61 3c 3d 30 3b 0a 7d 0a 0a 2f 2a 20 41 63 63  ta<=0;.}../* Acc
35e5a 65 73 73 20 74 68 65 20 63 75 72 72 65 6e 74 20  ess the current 
35e5b 74 65 72 6d 2e 20 2a 2f 0a 73 74 61 74 69 63 20  term. */.static 
35e5c 69 6e 74 20 6c 65 61 66 52 65 61 64 65 72 54 65  int leafReaderTe
35e5d 72 6d 42 79 74 65 73 28 4c 65 61 66 52 65 61 64  rmBytes(LeafRead
35e5e 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
35e5f 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
35e60 74 65 72 6d 2e 6e 44 61 74 61 3b 0a 7d 0a 73 74  term.nData;.}.st
35e61 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
35e62 2a 6c 65 61 66 52 65 61 64 65 72 54 65 72 6d 28  *leafReaderTerm(
35e63 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52 65 61  LeafReader *pRea
35e64 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20  der){.  assert( 
35e65 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44  pReader->term.nD
35e66 61 74 61 3e 30 20 29 3b 0a 20 20 72 65 74 75 72  ata>0 );.  retur
35e67 6e 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e  n pReader->term.
35e68 70 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 20 41 63 63  pData;.}../* Acc
35e69 65 73 73 20 74 68 65 20 64 6f 63 6c 69 73 74 20  ess the doclist 
35e6a 64 61 74 61 20 66 6f 72 20 74 68 65 20 63 75 72  data for the cur
35e6b 72 65 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 73 74  rent term. */.st
35e6c 61 74 69 63 20 69 6e 74 20 6c 65 61 66 52 65 61  atic int leafRea
35e6d 64 65 72 44 61 74 61 42 79 74 65 73 28 4c 65 61  derDataBytes(Lea
35e6e 66 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  fReader *pReader
35e6f 29 7b 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a  ){.  int nData;.
35e70 20 20 61 73 73 65 72 74 28 20 70 52 65 61 64 65    assert( pReade
35e71 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 3e 30 20  r->term.nData>0 
35e72 29 3b 0a 20 20 66 74 73 33 47 65 74 56 61 72 69  );.  fts3GetVari
35e73 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44  nt32(pReader->pD
35e74 61 74 61 2c 20 26 6e 44 61 74 61 29 3b 0a 20 20  ata, &nData);.  
35e75 72 65 74 75 72 6e 20 6e 44 61 74 61 3b 0a 7d 0a  return nData;.}.
35e76 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
35e77 72 20 2a 6c 65 61 66 52 65 61 64 65 72 44 61 74  r *leafReaderDat
35e78 61 28 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52  a(LeafReader *pR
35e79 65 61 64 65 72 29 7b 0a 20 20 69 6e 74 20 6e 2c  eader){.  int n,
35e7a 20 6e 44 61 74 61 3b 0a 20 20 61 73 73 65 72 74   nData;.  assert
35e7b 28 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e  ( pReader->term.
35e7c 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 6e 20 3d  nData>0 );.  n =
35e7d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
35e7e 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c  (pReader->pData,
35e7f 20 26 6e 44 61 74 61 29 3b 0a 20 20 72 65 74 75   &nData);.  retu
35e80 72 6e 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74  rn pReader->pDat
35e81 61 2b 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  a+n;.}..static v
35e82 6f 69 64 20 6c 65 61 66 52 65 61 64 65 72 49 6e  oid leafReaderIn
35e83 69 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  it(const char *p
35e84 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c  Data, int nData,
35e85 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
35e86 20 20 20 20 20 20 20 20 20 20 20 20 4c 65 61 66              Leaf
35e87 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
35e88 7b 0a 20 20 69 6e 74 20 6e 54 65 72 6d 2c 20 6e  {.  int nTerm, n
35e89 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61  ;..  assert( nDa
35e8a 74 61 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74  ta>0 );.  assert
35e8b 28 20 70 44 61 74 61 5b 30 5d 3d 3d 27 5c 30 27  ( pData[0]=='\0'
35e8c 20 29 3b 0a 0a 20 20 43 4c 45 41 52 28 70 52 65   );..  CLEAR(pRe
35e8d 61 64 65 72 29 3b 0a 0a 20 20 2f 2a 20 52 65 61  ader);..  /* Rea
35e8e 64 20 74 68 65 20 66 69 72 73 74 20 74 65 72 6d  d the first term
35e8f 2c 20 73 6b 69 70 70 69 6e 67 20 74 68 65 20 68  , skipping the h
35e90 65 61 64 65 72 20 62 79 74 65 2e 20 2a 2f 0a 20  eader byte. */. 
35e91 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
35e92 6e 74 33 32 28 70 44 61 74 61 2b 31 2c 20 26 6e  nt32(pData+1, &n
35e93 54 65 72 6d 29 3b 0a 20 20 64 61 74 61 42 75 66  Term);.  dataBuf
35e94 66 65 72 49 6e 69 74 28 26 70 52 65 61 64 65 72  ferInit(&pReader
35e95 2d 3e 74 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a  ->term, nTerm);.
35e96 20 20 64 61 74 61 42 75 66 66 65 72 52 65 70 6c    dataBufferRepl
35e97 61 63 65 28 26 70 52 65 61 64 65 72 2d 3e 74 65  ace(&pReader->te
35e98 72 6d 2c 20 70 44 61 74 61 2b 31 2b 6e 2c 20 6e  rm, pData+1+n, n
35e99 54 65 72 6d 29 3b 0a 0a 20 20 2f 2a 20 50 6f 73  Term);..  /* Pos
35e9a 69 74 69 6f 6e 20 61 66 74 65 72 20 74 68 65 20  ition after the 
35e9b 66 69 72 73 74 20 74 65 72 6d 2e 20 2a 2f 0a 20  first term. */. 
35e9c 20 61 73 73 65 72 74 28 20 31 2b 6e 2b 6e 54 65   assert( 1+n+nTe
35e9d 72 6d 3c 6e 44 61 74 61 20 29 3b 0a 20 20 70 52  rm<nData );.  pR
35e9e 65 61 64 65 72 2d 3e 70 44 61 74 61 20 3d 20 70  eader->pData = p
35e9f 44 61 74 61 2b 31 2b 6e 2b 6e 54 65 72 6d 3b 0a  Data+1+n+nTerm;.
35ea0 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61    pReader->nData
35ea1 20 3d 20 6e 44 61 74 61 2d 31 2d 6e 2d 6e 54 65   = nData-1-n-nTe
35ea2 72 6d 3b 0a 7d 0a 0a 2f 2a 20 53 74 65 70 20 74  rm;.}../* Step t
35ea3 68 65 20 72 65 61 64 65 72 20 66 6f 72 77 61 72  he reader forwar
35ea4 64 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74 65  d to the next te
35ea5 72 6d 2e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f  rm. */.static vo
35ea6 69 64 20 6c 65 61 66 52 65 61 64 65 72 53 74 65  id leafReaderSte
35ea7 70 28 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52  p(LeafReader *pR
35ea8 65 61 64 65 72 29 7b 0a 20 20 69 6e 74 20 6e 2c  eader){.  int n,
35ea9 20 6e 44 61 74 61 2c 20 6e 50 72 65 66 69 78 2c   nData, nPrefix,
35eaa 20 6e 53 75 66 66 69 78 3b 0a 20 20 61 73 73 65   nSuffix;.  asse
35eab 72 74 28 20 21 6c 65 61 66 52 65 61 64 65 72 41  rt( !leafReaderA
35eac 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b  tEnd(pReader) );
35ead 0a 0a 20 20 2f 2a 20 53 6b 69 70 20 70 72 65 76  ..  /* Skip prev
35eae 69 6f 75 73 20 65 6e 74 72 79 27 73 20 64 61 74  ious entry's dat
35eaf 61 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 6e 20  a block. */.  n 
35eb0 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33  = fts3GetVarint3
35eb1 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61  2(pReader->pData
35eb2 2c 20 26 6e 44 61 74 61 29 3b 0a 20 20 61 73 73  , &nData);.  ass
35eb3 65 72 74 28 20 6e 2b 6e 44 61 74 61 3c 3d 70 52  ert( n+nData<=pR
35eb4 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a  eader->nData );.
35eb5 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61    pReader->pData
35eb6 20 2b 3d 20 6e 2b 6e 44 61 74 61 3b 0a 20 20 70   += n+nData;.  p
35eb7 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 2d 3d  Reader->nData -=
35eb8 20 6e 2b 6e 44 61 74 61 3b 0a 0a 20 20 69 66 28   n+nData;..  if(
35eb9 20 21 6c 65 61 66 52 65 61 64 65 72 41 74 45 6e   !leafReaderAtEn
35eba 64 28 70 52 65 61 64 65 72 29 20 29 7b 0a 20 20  d(pReader) ){.  
35ebb 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20 74    /* Construct t
35ebc 68 65 20 6e 65 77 20 74 65 72 6d 20 75 73 69 6e  he new term usin
35ebd 67 20 61 20 70 72 65 66 69 78 20 66 72 6f 6d 20  g a prefix from 
35ebe 74 68 65 20 6f 6c 64 20 74 65 72 6d 20 70 6c 75  the old term plu
35ebf 73 20 61 0a 20 20 20 20 2a 2a 20 73 75 66 66 69  s a.    ** suffi
35ec0 78 20 66 72 6f 6d 20 74 68 65 20 6c 65 61 66 20  x from the leaf 
35ec1 64 61 74 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  data..    */.   
35ec2 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
35ec3 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44  nt32(pReader->pD
35ec4 61 74 61 2c 20 26 6e 50 72 65 66 69 78 29 3b 0a  ata, &nPrefix);.
35ec5 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74      n += fts3Get
35ec6 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65 72  Varint32(pReader
35ec7 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 6e 53 75 66  ->pData+n, &nSuf
35ec8 66 69 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74  fix);.    assert
35ec9 28 20 6e 2b 6e 53 75 66 66 69 78 3c 70 52 65 61  ( n+nSuffix<pRea
35eca 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20  der->nData );.  
35ecb 20 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e    pReader->term.
35ecc 6e 44 61 74 61 20 3d 20 6e 50 72 65 66 69 78 3b  nData = nPrefix;
35ecd 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41  .    dataBufferA
35ece 70 70 65 6e 64 28 26 70 52 65 61 64 65 72 2d 3e  ppend(&pReader->
35ecf 74 65 72 6d 2c 20 70 52 65 61 64 65 72 2d 3e 70  term, pReader->p
35ed0 44 61 74 61 2b 6e 2c 20 6e 53 75 66 66 69 78 29  Data+n, nSuffix)
35ed1 3b 0a 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  ;..    pReader->
35ed2 70 44 61 74 61 20 2b 3d 20 6e 2b 6e 53 75 66 66  pData += n+nSuff
35ed3 69 78 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d  ix;.    pReader-
35ed4 3e 6e 44 61 74 61 20 2d 3d 20 6e 2b 6e 53 75 66  >nData -= n+nSuf
35ed5 66 69 78 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 73  fix;.  }.}../* s
35ed6 74 72 63 6d 70 2d 73 74 79 6c 65 20 63 6f 6d 70  trcmp-style comp
35ed7 61 72 69 73 6f 6e 20 6f 66 20 70 52 65 61 64 65  arison of pReade
35ed8 72 27 73 20 63 75 72 72 65 6e 74 20 74 65 72 6d  r's current term
35ed9 20 61 67 61 69 6e 73 74 20 70 54 65 72 6d 2e 0a   against pTerm..
35eda 2a 2a 20 49 66 20 69 73 50 72 65 66 69 78 2c 20  ** If isPrefix, 
35edb 65 71 75 61 6c 69 74 79 20 6d 65 61 6e 73 20 65  equality means e
35edc 71 75 61 6c 20 74 68 72 6f 75 67 68 20 6e 54 65  qual through nTe
35edd 72 6d 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61  rm bytes..*/.sta
35ede 74 69 63 20 69 6e 74 20 6c 65 61 66 52 65 61 64  tic int leafRead
35edf 65 72 54 65 72 6d 43 6d 70 28 4c 65 61 66 52 65  erTermCmp(LeafRe
35ee0 61 64 65 72 20 2a 70 52 65 61 64 65 72 2c 0a 20  ader *pReader,. 
35ee1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35ee2 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
35ee3 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69  t char *pTerm, i
35ee4 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73  nt nTerm, int is
35ee5 50 72 65 66 69 78 29 7b 0a 20 20 69 6e 74 20 63  Prefix){.  int c
35ee6 2c 20 6e 20 3d 20 70 52 65 61 64 65 72 2d 3e 74  , n = pReader->t
35ee7 65 72 6d 2e 6e 44 61 74 61 3c 6e 54 65 72 6d 20  erm.nData<nTerm 
35ee8 3f 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e  ? pReader->term.
35ee9 6e 44 61 74 61 20 3a 20 6e 54 65 72 6d 3b 0a 20  nData : nTerm;. 
35eea 20 69 66 28 20 6e 3d 3d 30 20 29 7b 0a 20 20 20   if( n==0 ){.   
35eeb 20 69 66 28 20 70 52 65 61 64 65 72 2d 3e 74 65   if( pReader->te
35eec 72 6d 2e 6e 44 61 74 61 3e 30 20 29 20 72 65 74  rm.nData>0 ) ret
35eed 75 72 6e 20 2d 31 3b 0a 20 20 20 20 69 66 28 6e  urn -1;.    if(n
35eee 54 65 72 6d 3e 30 20 29 20 72 65 74 75 72 6e 20  Term>0 ) return 
35eef 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  1;.    return 0;
35ef0 0a 20 20 7d 0a 0a 20 20 63 20 3d 20 6d 65 6d 63  .  }..  c = memc
35ef1 6d 70 28 70 52 65 61 64 65 72 2d 3e 74 65 72 6d  mp(pReader->term
35ef2 2e 70 44 61 74 61 2c 20 70 54 65 72 6d 2c 20 6e  .pData, pTerm, n
35ef3 29 3b 0a 20 20 69 66 28 20 63 21 3d 30 20 29 20  );.  if( c!=0 ) 
35ef4 72 65 74 75 72 6e 20 63 3b 0a 20 20 69 66 28 20  return c;.  if( 
35ef5 69 73 50 72 65 66 69 78 20 26 26 20 6e 3d 3d 6e  isPrefix && n==n
35ef6 54 65 72 6d 20 29 20 72 65 74 75 72 6e 20 30 3b  Term ) return 0;
35ef7 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65  .  return pReade
35ef8 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 20 2d 20  r->term.nData - 
35ef9 6e 54 65 72 6d 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a  nTerm;.}.../****
35efa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35efb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35efc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35efd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
35efe 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 77 72   LeavesReader wr
35eff 61 70 73 20 4c 65 61 66 52 65 61 64 65 72 20 74  aps LeafReader t
35f00 6f 20 61 6c 6c 6f 77 20 69 74 65 72 61 74 69 6e  o allow iteratin
35f01 67 20 6f 76 65 72 20 74 68 65 20 65 6e 74 69 72  g over the entir
35f02 65 0a 2a 2a 20 6c 65 61 66 20 6c 61 79 65 72 20  e.** leaf layer 
35f03 6f 66 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a  of the tree..*/.
35f04 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4c  typedef struct L
35f05 65 61 76 65 73 52 65 61 64 65 72 20 7b 0a 20 20  eavesReader {.  
35f06 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 20  int idx;        
35f07 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
35f08 65 78 20 77 69 74 68 69 6e 20 74 68 65 20 73 65  ex within the se
35f09 67 6d 65 6e 74 2e 20 2a 2f 0a 0a 20 20 73 71 6c  gment. */..  sql
35f0a 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
35f0b 3b 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 6d  ;      /* Statem
35f0c 65 6e 74 20 77 65 27 72 65 20 73 74 72 65 61 6d  ent we're stream
35f0d 69 6e 67 20 6c 65 61 76 65 73 20 66 72 6f 6d 2e  ing leaves from.
35f0e 20 2a 2f 0a 20 20 69 6e 74 20 65 6f 66 3b 20 20   */.  int eof;  
35f0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35f10 2f 2a 20 77 65 27 76 65 20 73 65 65 6e 20 53 51  /* we've seen SQ
35f11 4c 49 54 45 5f 44 4f 4e 45 20 66 72 6f 6d 20 70  LITE_DONE from p
35f12 53 74 6d 74 2e 20 2a 2f 0a 0a 20 20 4c 65 61 66  Stmt. */..  Leaf
35f13 52 65 61 64 65 72 20 6c 65 61 66 52 65 61 64 65  Reader leafReade
35f14 72 3b 20 20 20 20 2f 2a 20 72 65 61 64 65 72 20  r;    /* reader 
35f15 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
35f16 6c 65 61 66 2e 20 2a 2f 0a 20 20 44 61 74 61 42  leaf. */.  DataB
35f17 75 66 66 65 72 20 72 6f 6f 74 44 61 74 61 3b 20  uffer rootData; 
35f18 20 20 20 20 20 2f 2a 20 72 6f 6f 74 20 64 61 74       /* root dat
35f19 61 20 66 6f 72 20 69 6e 6c 69 6e 65 2e 20 2a 2f  a for inline. */
35f1a 0a 7d 20 4c 65 61 76 65 73 52 65 61 64 65 72 3b  .} LeavesReader;
35f1b 0a 0a 2f 2a 20 41 63 63 65 73 73 20 74 68 65 20  ../* Access the 
35f1c 63 75 72 72 65 6e 74 20 74 65 72 6d 2e 20 2a 2f  current term. */
35f1d 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 76  .static int leav
35f1e 65 73 52 65 61 64 65 72 54 65 72 6d 42 79 74 65  esReaderTermByte
35f1f 73 28 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  s(LeavesReader *
35f20 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65  pReader){.  asse
35f21 72 74 28 20 21 70 52 65 61 64 65 72 2d 3e 65 6f  rt( !pReader->eo
35f22 66 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 6c 65  f );.  return le
35f23 61 66 52 65 61 64 65 72 54 65 72 6d 42 79 74 65  afReaderTermByte
35f24 73 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66  s(&pReader->leaf
35f25 52 65 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69  Reader);.}.stati
35f26 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 65  c const char *le
35f27 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 28 4c  avesReaderTerm(L
35f28 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 52 65  eavesReader *pRe
35f29 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28  ader){.  assert(
35f2a 20 21 70 52 65 61 64 65 72 2d 3e 65 6f 66 20 29   !pReader->eof )
35f2b 3b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 66 52  ;.  return leafR
35f2c 65 61 64 65 72 54 65 72 6d 28 26 70 52 65 61 64  eaderTerm(&pRead
35f2d 65 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b  er->leafReader);
35f2e 0a 7d 0a 0a 2f 2a 20 41 63 63 65 73 73 20 74 68  .}../* Access th
35f2f 65 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 66  e doclist data f
35f30 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  or the current t
35f31 65 72 6d 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69  erm. */.static i
35f32 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65 72 44  nt leavesReaderD
35f33 61 74 61 42 79 74 65 73 28 4c 65 61 76 65 73 52  ataBytes(LeavesR
35f34 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
35f35 0a 20 20 61 73 73 65 72 74 28 20 21 70 52 65 61  .  assert( !pRea
35f36 64 65 72 2d 3e 65 6f 66 20 29 3b 0a 20 20 72 65  der->eof );.  re
35f37 74 75 72 6e 20 6c 65 61 66 52 65 61 64 65 72 44  turn leafReaderD
35f38 61 74 61 42 79 74 65 73 28 26 70 52 65 61 64 65  ataBytes(&pReade
35f39 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a  r->leafReader);.
35f3a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  }.static const c
35f3b 68 61 72 20 2a 6c 65 61 76 65 73 52 65 61 64 65  har *leavesReade
35f3c 72 44 61 74 61 28 4c 65 61 76 65 73 52 65 61 64  rData(LeavesRead
35f3d 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
35f3e 61 73 73 65 72 74 28 20 21 70 52 65 61 64 65 72  assert( !pReader
35f3f 2d 3e 65 6f 66 20 29 3b 0a 20 20 72 65 74 75 72  ->eof );.  retur
35f40 6e 20 6c 65 61 66 52 65 61 64 65 72 44 61 74 61  n leafReaderData
35f41 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66 52  (&pReader->leafR
35f42 65 61 64 65 72 29 3b 0a 7d 0a 0a 73 74 61 74 69  eader);.}..stati
35f43 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64  c int leavesRead
35f44 65 72 41 74 45 6e 64 28 4c 65 61 76 65 73 52 65  erAtEnd(LeavesRe
35f45 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
35f46 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72    return pReader
35f47 2d 3e 65 6f 66 3b 0a 7d 0a 0a 2f 2a 20 6c 6f 61  ->eof;.}../* loa
35f48 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73 28 29  dSegmentLeaves()
35f49 20 6d 61 79 20 6e 6f 74 20 72 65 61 64 20 61 6c   may not read al
35f4a 6c 20 74 68 65 20 77 61 79 20 74 6f 20 53 51 4c  l the way to SQL
35f4b 49 54 45 5f 44 4f 4e 45 2c 20 74 68 75 73 0a 2a  ITE_DONE, thus.*
35f4c 2a 20 6c 65 61 76 69 6e 67 20 74 68 65 20 73 74  * leaving the st
35f4d 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 6f  atement handle o
35f4e 70 65 6e 2c 20 77 68 69 63 68 20 6c 6f 63 6b 73  pen, which locks
35f4f 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 2f   the table..*/./
35f50 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68  * TODO(shess) Th
35f51 69 73 20 22 73 6f 6c 75 74 69 6f 6e 22 20 69 73  is "solution" is
35f52 20 6e 6f 74 20 73 61 74 69 73 66 61 63 74 6f 72   not satisfactor
35f53 79 2e 20 20 52 65 61 6c 6c 79 2c 20 74 68 65 72  y.  Really, ther
35f54 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63  e.** should be c
35f55 68 65 63 6b 2d 69 6e 20 66 75 6e 63 74 69 6f 6e  heck-in function
35f56 20 66 6f 72 20 61 6c 6c 20 73 74 61 74 65 6d 65   for all stateme
35f57 6e 74 20 68 61 6e 64 6c 65 73 20 77 68 69 63 68  nt handles which
35f58 0a 2a 2a 20 61 72 72 61 6e 67 65 73 20 74 6f 20  .** arranges to 
35f59 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 72 65 73  call sqlite3_res
35f5a 65 74 28 29 2e 20 20 54 68 69 73 20 6d 6f 73 74  et().  This most
35f5b 20 6c 69 6b 65 6c 79 20 77 69 6c 6c 20 72 65 71   likely will req
35f5c 75 69 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 63 61  uire.** modifica
35f5d 74 69 6f 6e 20 74 6f 20 63 6f 6e 74 72 6f 6c 20  tion to control 
35f5e 66 6c 6f 77 20 61 6c 6c 20 6f 76 65 72 20 74 68  flow all over th
35f5f 65 20 70 6c 61 63 65 2c 20 74 68 6f 75 67 68 2c  e place, though,
35f60 20 73 6f 20 66 6f 72 20 6e 6f 77 0a 2a 2a 20 6a   so for now.** j
35f61 75 73 74 20 70 75 6e 74 2e 0a 2a 2a 0a 2a 2a 20  ust punt..**.** 
35f62 4e 6f 74 65 20 74 68 65 20 74 68 65 20 63 75 72  Note the the cur
35f63 72 65 6e 74 20 73 79 73 74 65 6d 20 61 73 73 75  rent system assu
35f64 6d 65 73 20 74 68 61 74 20 73 65 67 6d 65 6e 74  mes that segment
35f65 20 6d 65 72 67 65 73 20 77 69 6c 6c 20 72 75 6e   merges will run
35f66 20 74 6f 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f   to.** completio
35f67 6e 2c 20 77 68 69 63 68 20 69 73 20 77 68 79 20  n, which is why 
35f68 74 68 69 73 20 70 61 72 74 69 63 75 6c 61 72 20  this particular 
35f69 70 72 6f 62 61 62 6c 79 20 68 61 73 6e 27 74 20  probably hasn't 
35f6a 61 72 69 73 65 6e 20 69 6e 0a 2a 2a 20 74 68 69  arisen in.** thi
35f6b 73 20 63 61 73 65 2e 20 20 50 72 6f 62 61 62 6c  s case.  Probabl
35f6c 79 20 61 20 62 72 69 74 74 6c 65 20 61 73 73 75  y a brittle assu
35f6d 6d 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  mption..*/.stati
35f6e 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64  c int leavesRead
35f6f 65 72 52 65 73 65 74 28 4c 65 61 76 65 73 52 65  erReset(LeavesRe
35f70 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
35f71 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
35f72 5f 72 65 73 65 74 28 70 52 65 61 64 65 72 2d 3e  _reset(pReader->
35f73 70 53 74 6d 74 29 3b 0a 7d 0a 0a 73 74 61 74 69  pStmt);.}..stati
35f74 63 20 76 6f 69 64 20 6c 65 61 76 65 73 52 65 61  c void leavesRea
35f75 64 65 72 44 65 73 74 72 6f 79 28 4c 65 61 76 65  derDestroy(Leave
35f76 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  sReader *pReader
35f77 29 7b 0a 20 20 2f 2a 20 49 66 20 69 64 78 20 69  ){.  /* If idx i
35f78 73 20 2d 31 2c 20 74 68 61 74 20 6d 65 61 6e 73  s -1, that means
35f79 20 77 65 27 72 65 20 75 73 69 6e 67 20 61 20 6e   we're using a n
35f7a 6f 6e 2d 63 61 63 68 65 64 20 73 74 61 74 65 6d  on-cached statem
35f7b 65 6e 74 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20  ent.  ** handle 
35f7c 69 6e 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 28  in the optimize(
35f7d 29 20 63 61 73 65 2c 20 73 6f 20 77 65 20 6e 65  ) case, so we ne
35f7e 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20 69 74  ed to release it
35f7f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 52 65  ..  */.  if( pRe
35f80 61 64 65 72 2d 3e 70 53 74 6d 74 21 3d 4e 55 4c  ader->pStmt!=NUL
35f81 4c 20 26 26 20 70 52 65 61 64 65 72 2d 3e 69 64  L && pReader->id
35f82 78 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 73 71 6c  x==-1 ){.    sql
35f83 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52  ite3_finalize(pR
35f84 65 61 64 65 72 2d 3e 70 53 74 6d 74 29 3b 0a 20  eader->pStmt);. 
35f85 20 7d 0a 20 20 6c 65 61 66 52 65 61 64 65 72 44   }.  leafReaderD
35f86 65 73 74 72 6f 79 28 26 70 52 65 61 64 65 72 2d  estroy(&pReader-
35f87 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 20 20  >leafReader);.  
35f88 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
35f89 79 28 26 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74  y(&pReader->root
35f8a 44 61 74 61 29 3b 0a 20 20 53 43 52 41 4d 42 4c  Data);.  SCRAMBL
35f8b 45 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 2f  E(pReader);.}../
35f8c 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 70 52 65  * Initialize pRe
35f8d 61 64 65 72 20 77 69 74 68 20 74 68 65 20 67 69  ader with the gi
35f8e 76 65 6e 20 72 6f 6f 74 20 64 61 74 61 20 28 69  ven root data (i
35f8f 66 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 3d  f iStartBlockid=
35f90 3d 30 0a 2a 2a 20 74 68 65 20 6c 65 61 66 20 64  =0.** the leaf d
35f91 61 74 61 20 77 61 73 20 65 6e 74 69 72 65 6c 79  ata was entirely
35f92 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68   contained in th
35f93 65 20 72 6f 6f 74 29 2c 20 6f 72 20 66 72 6f 6d  e root), or from
35f94 20 74 68 65 0a 2a 2a 20 73 74 72 65 61 6d 20 6f   the.** stream o
35f95 66 20 62 6c 6f 63 6b 73 20 62 65 74 77 65 65 6e  f blocks between
35f96 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 20 61   iStartBlockid a
35f97 6e 64 20 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 20  nd iEndBlockid, 
35f98 69 6e 63 6c 75 73 69 76 65 2e 0a 2a 2f 0a 73 74  inclusive..*/.st
35f99 61 74 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52  atic int leavesR
35f9a 65 61 64 65 72 49 6e 69 74 28 66 75 6c 6c 74 65  eaderInit(fullte
35f9b 78 74 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20 20  xt_vtab *v,.    
35f9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35f9d 20 20 20 20 20 20 20 20 69 6e 74 20 69 64 78 2c          int idx,
35f9e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
35f9f 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
35fa0 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61 72 74  ite_int64 iStart
35fa1 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20  Blockid,.       
35fa2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35fa3 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
35fa4 34 20 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 0a 20  4 iEndBlockid,. 
35fa5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35fa6 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
35fa7 20 63 68 61 72 20 2a 70 52 6f 6f 74 44 61 74 61   char *pRootData
35fa8 2c 20 69 6e 74 20 6e 52 6f 6f 74 44 61 74 61 2c  , int nRootData,
35fa9 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
35faa 20 20 20 20 20 20 20 20 20 20 20 20 20 4c 65 61               Lea
35fab 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
35fac 65 72 29 7b 0a 20 20 43 4c 45 41 52 28 70 52 65  er){.  CLEAR(pRe
35fad 61 64 65 72 29 3b 0a 20 20 70 52 65 61 64 65 72  ader);.  pReader
35fae 2d 3e 69 64 78 20 3d 20 69 64 78 3b 0a 0a 20 20  ->idx = idx;..  
35faf 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26  dataBufferInit(&
35fb0 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61 74  pReader->rootDat
35fb1 61 2c 20 30 29 3b 0a 20 20 69 66 28 20 69 53 74  a, 0);.  if( iSt
35fb2 61 72 74 42 6c 6f 63 6b 69 64 3d 3d 30 20 29 7b  artBlockid==0 ){
35fb3 0a 20 20 20 20 2f 2a 20 45 6e 74 69 72 65 20 6c  .    /* Entire l
35fb4 65 61 66 20 6c 65 76 65 6c 20 66 69 74 20 69 6e  eaf level fit in
35fb5 20 72 6f 6f 74 20 64 61 74 61 2e 20 2a 2f 0a 20   root data. */. 
35fb6 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65 70     dataBufferRep
35fb7 6c 61 63 65 28 26 70 52 65 61 64 65 72 2d 3e 72  lace(&pReader->r
35fb8 6f 6f 74 44 61 74 61 2c 20 70 52 6f 6f 74 44 61  ootData, pRootDa
35fb9 74 61 2c 20 6e 52 6f 6f 74 44 61 74 61 29 3b 0a  ta, nRootData);.
35fba 20 20 20 20 6c 65 61 66 52 65 61 64 65 72 49 6e      leafReaderIn
35fbb 69 74 28 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74  it(pReader->root
35fbc 44 61 74 61 2e 70 44 61 74 61 2c 20 70 52 65 61  Data.pData, pRea
35fbd 64 65 72 2d 3e 72 6f 6f 74 44 61 74 61 2e 6e 44  der->rootData.nD
35fbe 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
35fbf 20 20 20 20 20 20 20 20 26 70 52 65 61 64 65 72          &pReader
35fc0 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 20  ->leafReader);. 
35fc1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
35fc2 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 20  te3_stmt *s;.   
35fc3 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65   int rc = sql_ge
35fc4 74 5f 6c 65 61 66 5f 73 74 61 74 65 6d 65 6e 74  t_leaf_statement
35fc5 28 76 2c 20 69 64 78 2c 20 26 73 29 3b 0a 20 20  (v, idx, &s);.  
35fc6 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
35fc7 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
35fc8 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ..    rc = sqlit
35fc9 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c  e3_bind_int64(s,
35fca 20 31 2c 20 69 53 74 61 72 74 42 6c 6f 63 6b 69   1, iStartBlocki
35fcb 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  d);.    if( rc!=
35fcc 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
35fcd 72 6e 20 72 63 3b 0a 0a 20 20 20 20 72 63 20 3d  rn rc;..    rc =
35fce 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
35fcf 74 36 34 28 73 2c 20 32 2c 20 69 45 6e 64 42 6c  t64(s, 2, iEndBl
35fd0 6f 63 6b 69 64 29 3b 0a 20 20 20 20 69 66 28 20  ockid);.    if( 
35fd1 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
35fd2 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20  return rc;..    
35fd3 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65  rc = sqlite3_ste
35fd4 70 28 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63  p(s);.    if( rc
35fd5 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
35fd6 0a 20 20 20 20 20 20 70 52 65 61 64 65 72 2d 3e  .      pReader->
35fd7 65 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 72  eof = 1;.      r
35fd8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
35fd9 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
35fda 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20  c!=SQLITE_ROW ) 
35fdb 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20  return rc;..    
35fdc 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 20 3d  pReader->pStmt =
35fdd 20 73 3b 0a 20 20 20 20 6c 65 61 66 52 65 61 64   s;.    leafRead
35fde 65 72 49 6e 69 74 28 73 71 6c 69 74 65 33 5f 63  erInit(sqlite3_c
35fdf 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 52 65 61 64  olumn_blob(pRead
35fe0 65 72 2d 3e 70 53 74 6d 74 2c 20 30 29 2c 0a 20  er->pStmt, 0),. 
35fe1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35fe2 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
35fe3 5f 62 79 74 65 73 28 70 52 65 61 64 65 72 2d 3e  _bytes(pReader->
35fe4 70 53 74 6d 74 2c 20 30 29 2c 0a 20 20 20 20 20  pStmt, 0),.     
35fe5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70                &p
35fe6 52 65 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64  Reader->leafRead
35fe7 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  er);.  }.  retur
35fe8 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
35fe9 2f 2a 20 53 74 65 70 20 74 68 65 20 63 75 72 72  /* Step the curr
35fea 65 6e 74 20 6c 65 61 66 20 66 6f 72 77 61 72 64  ent leaf forward
35feb 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74 65 72   to the next ter
35fec 6d 2e 20 20 49 66 20 77 65 20 72 65 61 63 68 20  m.  If we reach 
35fed 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68  the.** end of th
35fee 65 20 63 75 72 72 65 6e 74 20 6c 65 61 66 2c 20  e current leaf, 
35fef 73 74 65 70 20 66 6f 72 77 61 72 64 20 74 6f 20  step forward to 
35ff0 74 68 65 20 6e 65 78 74 20 6c 65 61 66 20 62 6c  the next leaf bl
35ff1 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ock..*/.static i
35ff2 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65 72 53  nt leavesReaderS
35ff3 74 65 70 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  tep(fulltext_vta
35ff4 62 20 2a 76 2c 20 4c 65 61 76 65 73 52 65 61 64  b *v, LeavesRead
35ff5 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
35ff6 61 73 73 65 72 74 28 20 21 6c 65 61 76 65 73 52  assert( !leavesR
35ff7 65 61 64 65 72 41 74 45 6e 64 28 70 52 65 61 64  eaderAtEnd(pRead
35ff8 65 72 29 20 29 3b 0a 20 20 6c 65 61 66 52 65 61  er) );.  leafRea
35ff9 64 65 72 53 74 65 70 28 26 70 52 65 61 64 65 72  derStep(&pReader
35ffa 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 0a  ->leafReader);..
35ffb 20 20 69 66 28 20 6c 65 61 66 52 65 61 64 65 72    if( leafReader
35ffc 41 74 45 6e 64 28 26 70 52 65 61 64 65 72 2d 3e  AtEnd(&pReader->
35ffd 6c 65 61 66 52 65 61 64 65 72 29 20 29 7b 0a 20  leafReader) ){. 
35ffe 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 69     int rc;.    i
35fff 66 28 20 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74  f( pReader->root
36000 44 61 74 61 2e 70 44 61 74 61 20 29 7b 0a 20 20  Data.pData ){.  
36001 20 20 20 20 70 52 65 61 64 65 72 2d 3e 65 6f 66      pReader->eof
36002 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75   = 1;.      retu
36003 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
36004 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c    }.    rc = sql
36005 69 74 65 33 5f 73 74 65 70 28 70 52 65 61 64 65  ite3_step(pReade
36006 72 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 69  r->pStmt);.    i
36007 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f  f( rc!=SQLITE_RO
36008 57 20 29 7b 0a 20 20 20 20 20 20 70 52 65 61 64  W ){.      pRead
36009 65 72 2d 3e 65 6f 66 20 3d 20 31 3b 0a 20 20 20  er->eof = 1;.   
3600a 20 20 20 72 65 74 75 72 6e 20 72 63 3d 3d 53 51     return rc==SQ
3600b 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51 4c 49  LITE_DONE ? SQLI
3600c 54 45 5f 4f 4b 20 3a 20 72 63 3b 0a 20 20 20 20  TE_OK : rc;.    
3600d 7d 0a 20 20 20 20 6c 65 61 66 52 65 61 64 65 72  }.    leafReader
3600e 44 65 73 74 72 6f 79 28 26 70 52 65 61 64 65 72  Destroy(&pReader
3600f 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 20  ->leafReader);. 
36010 20 20 20 6c 65 61 66 52 65 61 64 65 72 49 6e 69     leafReaderIni
36011 74 28 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t(sqlite3_column
36012 5f 62 6c 6f 62 28 70 52 65 61 64 65 72 2d 3e 70  _blob(pReader->p
36013 53 74 6d 74 2c 20 30 29 2c 0a 20 20 20 20 20 20  Stmt, 0),.      
36014 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
36015 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
36016 73 28 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74  s(pReader->pStmt
36017 2c 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20  , 0),.          
36018 20 20 20 20 20 20 20 20 20 26 70 52 65 61 64 65           &pReade
36019 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a  r->leafReader);.
3601a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
3601b 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4f 72  ITE_OK;.}../* Or
3601c 64 65 72 20 4c 65 61 76 65 73 52 65 61 64 65 72  der LeavesReader
3601d 73 20 62 79 20 74 68 65 69 72 20 74 65 72 6d 2c  s by their term,
3601e 20 69 67 6e 6f 72 69 6e 67 20 69 64 78 2e 20 20   ignoring idx.  
3601f 52 65 61 64 65 72 73 20 61 74 20 65 6f 66 0a 2a  Readers at eof.*
36020 2a 20 61 6c 77 61 79 73 20 73 6f 72 74 20 74 6f  * always sort to
36021 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61   the end..*/.sta
36022 74 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65  tic int leavesRe
36023 61 64 65 72 54 65 72 6d 43 6d 70 28 4c 65 61 76  aderTermCmp(Leav
36024 65 73 52 65 61 64 65 72 20 2a 6c 72 31 2c 20 4c  esReader *lr1, L
36025 65 61 76 65 73 52 65 61 64 65 72 20 2a 6c 72 32  eavesReader *lr2
36026 29 7b 0a 20 20 69 66 28 20 6c 65 61 76 65 73 52  ){.  if( leavesR
36027 65 61 64 65 72 41 74 45 6e 64 28 6c 72 31 29 20  eaderAtEnd(lr1) 
36028 29 7b 0a 20 20 20 20 69 66 28 20 6c 65 61 76 65  ){.    if( leave
36029 73 52 65 61 64 65 72 41 74 45 6e 64 28 6c 72 32  sReaderAtEnd(lr2
3602a 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  ) ) return 0;.  
3602b 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
3602c 20 20 69 66 28 20 6c 65 61 76 65 73 52 65 61 64    if( leavesRead
3602d 65 72 41 74 45 6e 64 28 6c 72 32 29 20 29 20 72  erAtEnd(lr2) ) r
3602e 65 74 75 72 6e 20 2d 31 3b 0a 0a 20 20 72 65 74  eturn -1;..  ret
3602f 75 72 6e 20 6c 65 61 66 52 65 61 64 65 72 54 65  urn leafReaderTe
36030 72 6d 43 6d 70 28 26 6c 72 31 2d 3e 6c 65 61 66  rmCmp(&lr1->leaf
36031 52 65 61 64 65 72 2c 0a 20 20 20 20 20 20 20 20  Reader,.        
36032 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36033 20 20 20 6c 65 61 76 65 73 52 65 61 64 65 72 54     leavesReaderT
36034 65 72 6d 28 6c 72 32 29 2c 20 6c 65 61 76 65 73  erm(lr2), leaves
36035 52 65 61 64 65 72 54 65 72 6d 42 79 74 65 73 28  ReaderTermBytes(
36036 6c 72 32 29 2c 0a 20 20 20 20 20 20 20 20 20 20  lr2),.          
36037 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36038 20 30 29 3b 0a 7d 0a 0a 2f 2a 20 53 69 6d 69 6c   0);.}../* Simil
36039 61 72 20 74 6f 20 6c 65 61 76 65 73 52 65 61 64  ar to leavesRead
3603a 65 72 54 65 72 6d 43 6d 70 28 29 2c 20 77 69 74  erTermCmp(), wit
3603b 68 20 61 64 64 69 74 69 6f 6e 61 6c 20 6f 72 64  h additional ord
3603c 65 72 69 6e 67 20 62 79 20 69 64 78 0a 2a 2a 20  ering by idx.** 
3603d 73 6f 20 74 68 61 74 20 6f 6c 64 65 72 20 73 65  so that older se
3603e 67 6d 65 6e 74 73 20 73 6f 72 74 20 62 65 66 6f  gments sort befo
3603f 72 65 20 6e 65 77 65 72 20 73 65 67 6d 65 6e 74  re newer segment
36040 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
36041 20 6c 65 61 76 65 73 52 65 61 64 65 72 43 6d 70   leavesReaderCmp
36042 28 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 6c  (LeavesReader *l
36043 72 31 2c 20 4c 65 61 76 65 73 52 65 61 64 65 72  r1, LeavesReader
36044 20 2a 6c 72 32 29 7b 0a 20 20 69 6e 74 20 63 20   *lr2){.  int c 
36045 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65  = leavesReaderTe
36046 72 6d 43 6d 70 28 6c 72 31 2c 20 6c 72 32 29 3b  rmCmp(lr1, lr2);
36047 0a 20 20 69 66 28 20 63 21 3d 30 20 29 20 72 65  .  if( c!=0 ) re
36048 74 75 72 6e 20 63 3b 0a 20 20 72 65 74 75 72 6e  turn c;.  return
36049 20 6c 72 31 2d 3e 69 64 78 2d 6c 72 32 2d 3e 69   lr1->idx-lr2->i
3604a 64 78 3b 0a 7d 0a 0a 2f 2a 20 41 73 73 75 6d 65  dx;.}../* Assume
3604b 20 74 68 61 74 20 70 4c 72 5b 31 5d 2e 2e 70 4c   that pLr[1]..pL
3604c 72 5b 6e 4c 72 5d 20 61 72 65 20 73 6f 72 74 65  r[nLr] are sorte
3604d 64 2e 20 20 42 75 62 62 6c 65 20 70 4c 72 5b 30  d.  Bubble pLr[0
3604e 5d 20 69 6e 74 6f 20 69 74 73 0a 2a 2a 20 73 6f  ] into its.** so
3604f 72 74 65 64 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a  rted position..*
36050 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65  /.static void le
36051 61 76 65 73 52 65 61 64 65 72 52 65 6f 72 64 65  avesReaderReorde
36052 72 28 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  r(LeavesReader *
36053 70 4c 72 2c 20 69 6e 74 20 6e 4c 72 29 7b 0a 20  pLr, int nLr){. 
36054 20 77 68 69 6c 65 28 20 6e 4c 72 3e 31 20 26 26   while( nLr>1 &&
36055 20 6c 65 61 76 65 73 52 65 61 64 65 72 43 6d 70   leavesReaderCmp
36056 28 70 4c 72 2c 20 70 4c 72 2b 31 29 3e 30 20 29  (pLr, pLr+1)>0 )
36057 7b 0a 20 20 20 20 4c 65 61 76 65 73 52 65 61 64  {.    LeavesRead
36058 65 72 20 74 6d 70 20 3d 20 70 4c 72 5b 30 5d 3b  er tmp = pLr[0];
36059 0a 20 20 20 20 70 4c 72 5b 30 5d 20 3d 20 70 4c  .    pLr[0] = pL
3605a 72 5b 31 5d 3b 0a 20 20 20 20 70 4c 72 5b 31 5d  r[1];.    pLr[1]
3605b 20 3d 20 74 6d 70 3b 0a 20 20 20 20 6e 4c 72 2d   = tmp;.    nLr-
3605c 2d 3b 0a 20 20 20 20 70 4c 72 2b 2b 3b 0a 20 20  -;.    pLr++;.  
3605d 7d 0a 7d 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69  }.}../* Initiali
3605e 7a 65 73 20 70 52 65 61 64 65 72 73 20 77 69 74  zes pReaders wit
3605f 68 20 74 68 65 20 73 65 67 6d 65 6e 74 73 20 66  h the segments f
36060 72 6f 6d 20 6c 65 76 65 6c 20 69 4c 65 76 65 6c  rom level iLevel
36061 2c 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 74  , returning.** t
36062 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 67  he number of seg
36063 6d 65 6e 74 73 20 69 6e 20 2a 70 69 52 65 61 64  ments in *piRead
36064 65 72 73 2e 20 20 4c 65 61 76 65 73 20 70 52 65  ers.  Leaves pRe
36065 61 64 65 72 73 20 69 6e 20 73 6f 72 74 65 64 0a  aders in sorted.
36066 2a 2a 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61  ** order..*/.sta
36067 74 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65  tic int leavesRe
36068 61 64 65 72 73 49 6e 69 74 28 66 75 6c 6c 74 65  adersInit(fullte
36069 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20  xt_vtab *v, int 
3606a 69 4c 65 76 65 6c 2c 0a 20 20 20 20 20 20 20 20  iLevel,.        
3606b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3606c 20 20 20 20 20 4c 65 61 76 65 73 52 65 61 64 65       LeavesReade
3606d 72 20 2a 70 52 65 61 64 65 72 73 2c 20 69 6e 74  r *pReaders, int
3606e 20 2a 70 69 52 65 61 64 65 72 73 29 7b 0a 20 20   *piReaders){.  
3606f 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b  sqlite3_stmt *s;
36070 0a 20 20 69 6e 74 20 69 2c 20 72 63 20 3d 20 73  .  int i, rc = s
36071 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
36072 28 76 2c 20 53 45 47 44 49 52 5f 53 45 4c 45 43  (v, SEGDIR_SELEC
36073 54 5f 4c 45 56 45 4c 5f 53 54 4d 54 2c 20 26 73  T_LEVEL_STMT, &s
36074 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
36075 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
36076 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  rc;..  rc = sqli
36077 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20  te3_bind_int(s, 
36078 31 2c 20 69 4c 65 76 65 6c 29 3b 0a 20 20 69 66  1, iLevel);.  if
36079 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
3607a 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
3607b 69 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20  i = 0;.  while( 
3607c 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74  (rc = sqlite3_st
3607d 65 70 28 73 29 29 3d 3d 53 51 4c 49 54 45 5f 52  ep(s))==SQLITE_R
3607e 4f 57 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OW ){.    sqlite
3607f 5f 69 6e 74 36 34 20 69 53 74 61 72 74 20 3d 20  _int64 iStart = 
36080 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
36081 6e 74 36 34 28 73 2c 20 30 29 3b 0a 20 20 20 20  nt64(s, 0);.    
36082 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e  sqlite_int64 iEn
36083 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  d = sqlite3_colu
36084 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29 3b 0a  mn_int64(s, 1);.
36085 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
36086 70 52 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c 69  pRootData = sqli
36087 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
36088 73 2c 20 32 29 3b 0a 20 20 20 20 69 6e 74 20 6e  s, 2);.    int n
36089 52 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c 69 74  RootData = sqlit
3608a 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
3608b 73 2c 20 32 29 3b 0a 0a 20 20 20 20 61 73 73 65  s, 2);..    asse
3608c 72 74 28 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e  rt( i<MERGE_COUN
3608d 54 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 6c 65  T );.    rc = le
3608e 61 76 65 73 52 65 61 64 65 72 49 6e 69 74 28 76  avesReaderInit(v
3608f 2c 20 69 2c 20 69 53 74 61 72 74 2c 20 69 45 6e  , i, iStart, iEn
36090 64 2c 20 70 52 6f 6f 74 44 61 74 61 2c 20 6e 52  d, pRootData, nR
36091 6f 6f 74 44 61 74 61 2c 0a 20 20 20 20 20 20 20  ootData,.       
36092 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36093 20 20 20 26 70 52 65 61 64 65 72 73 5b 69 5d 29     &pReaders[i])
36094 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
36095 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b  LITE_OK ) break;
36096 0a 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 20  ..    i++;.  }. 
36097 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
36098 44 4f 4e 45 20 29 7b 0a 20 20 20 20 77 68 69 6c  DONE ){.    whil
36099 65 28 20 69 2d 2d 3e 30 20 29 7b 0a 20 20 20 20  e( i-->0 ){.    
3609a 20 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 65    leavesReaderDe
3609b 73 74 72 6f 79 28 26 70 52 65 61 64 65 72 73 5b  stroy(&pReaders[
3609c 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  i]);.    }.    r
3609d 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
3609e 20 2a 70 69 52 65 61 64 65 72 73 20 3d 20 69 3b   *piReaders = i;
3609f 0a 0a 20 20 2f 2a 20 4c 65 61 76 65 20 6f 75 72  ..  /* Leave our
360a0 20 72 65 73 75 6c 74 73 20 73 6f 72 74 65 64 20   results sorted 
360a1 62 79 20 74 65 72 6d 2c 20 74 68 65 6e 20 61 67  by term, then ag
360a2 65 2e 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69  e. */.  while( i
360a3 2d 2d 20 29 7b 0a 20 20 20 20 6c 65 61 76 65 73  -- ){.    leaves
360a4 52 65 61 64 65 72 52 65 6f 72 64 65 72 28 70 52  ReaderReorder(pR
360a5 65 61 64 65 72 73 2b 69 2c 20 2a 70 69 52 65 61  eaders+i, *piRea
360a6 64 65 72 73 2d 69 29 3b 0a 20 20 7d 0a 20 20 72  ders-i);.  }.  r
360a7 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
360a8 0a 7d 0a 0a 2f 2a 20 4d 65 72 67 65 20 64 6f 63  .}../* Merge doc
360a9 6c 69 73 74 73 20 66 72 6f 6d 20 70 52 65 61 64  lists from pRead
360aa 65 72 73 5b 6e 52 65 61 64 65 72 73 5d 20 69 6e  ers[nReaders] in
360ab 74 6f 20 61 20 73 69 6e 67 6c 65 20 64 6f 63 6c  to a single docl
360ac 69 73 74 2c 20 77 68 69 63 68 0a 2a 2a 20 69 73  ist, which.** is
360ad 20 77 72 69 74 74 65 6e 20 74 6f 20 70 57 72 69   written to pWri
360ae 74 65 72 2e 20 20 41 73 73 75 6d 65 73 20 70 52  ter.  Assumes pR
360af 65 61 64 65 72 73 20 69 73 20 6f 72 64 65 72 65  eaders is ordere
360b0 64 20 6f 6c 64 65 73 74 20 74 6f 0a 2a 2a 20 6e  d oldest to.** n
360b1 65 77 65 73 74 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44  ewest..*/./* TOD
360b2 4f 28 73 68 65 73 73 29 20 43 6f 6e 73 69 64 65  O(shess) Conside
360b3 72 20 70 75 74 74 69 6e 67 20 74 68 69 73 20 69  r putting this i
360b4 6e 6c 69 6e 65 20 69 6e 20 73 65 67 6d 65 6e 74  nline in segment
360b5 4d 65 72 67 65 28 29 2e 20 2a 2f 0a 73 74 61 74  Merge(). */.stat
360b6 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61  ic int leavesRea
360b7 64 65 72 73 4d 65 72 67 65 28 66 75 6c 6c 74 65  dersMerge(fullte
360b8 78 74 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20 20  xt_vtab *v,.    
360b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
360ba 20 20 20 20 20 20 20 20 20 20 4c 65 61 76 65 73            Leaves
360bb 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 73  Reader *pReaders
360bc 2c 20 69 6e 74 20 6e 52 65 61 64 65 72 73 2c 0a  , int nReaders,.
360bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
360be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4c 65                Le
360bf 61 66 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  afWriter *pWrite
360c0 72 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 64  r){.  DLReader d
360c1 6c 52 65 61 64 65 72 73 5b 4d 45 52 47 45 5f 43  lReaders[MERGE_C
360c2 4f 55 4e 54 5d 3b 0a 20 20 63 6f 6e 73 74 20 63  OUNT];.  const c
360c3 68 61 72 20 2a 70 54 65 72 6d 20 3d 20 6c 65 61  har *pTerm = lea
360c4 76 65 73 52 65 61 64 65 72 54 65 72 6d 28 70 52  vesReaderTerm(pR
360c5 65 61 64 65 72 73 29 3b 0a 20 20 69 6e 74 20 69  eaders);.  int i
360c6 2c 20 6e 54 65 72 6d 20 3d 20 6c 65 61 76 65 73  , nTerm = leaves
360c7 52 65 61 64 65 72 54 65 72 6d 42 79 74 65 73 28  ReaderTermBytes(
360c8 70 52 65 61 64 65 72 73 29 3b 0a 0a 20 20 61 73  pReaders);..  as
360c9 73 65 72 74 28 20 6e 52 65 61 64 65 72 73 3c 3d  sert( nReaders<=
360ca 4d 45 52 47 45 5f 43 4f 55 4e 54 20 29 3b 0a 0a  MERGE_COUNT );..
360cb 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65    for(i=0; i<nRe
360cc 61 64 65 72 73 3b 20 69 2b 2b 29 7b 0a 20 20 20  aders; i++){.   
360cd 20 64 6c 72 49 6e 69 74 28 26 64 6c 52 65 61 64   dlrInit(&dlRead
360ce 65 72 73 5b 69 5d 2c 20 44 4c 5f 44 45 46 41 55  ers[i], DL_DEFAU
360cf 4c 54 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  LT,.            
360d0 6c 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61  leavesReaderData
360d1 28 70 52 65 61 64 65 72 73 2b 69 29 2c 0a 20 20  (pReaders+i),.  
360d2 20 20 20 20 20 20 20 20 20 20 6c 65 61 76 65 73            leaves
360d3 52 65 61 64 65 72 44 61 74 61 42 79 74 65 73 28  ReaderDataBytes(
360d4 70 52 65 61 64 65 72 73 2b 69 29 29 3b 0a 20 20  pReaders+i));.  
360d5 7d 0a 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 66  }..  return leaf
360d6 57 72 69 74 65 72 53 74 65 70 4d 65 72 67 65 28  WriterStepMerge(
360d7 76 2c 20 70 57 72 69 74 65 72 2c 20 70 54 65 72  v, pWriter, pTer
360d8 6d 2c 20 6e 54 65 72 6d 2c 20 64 6c 52 65 61 64  m, nTerm, dlRead
360d9 65 72 73 2c 20 6e 52 65 61 64 65 72 73 29 3b 0a  ers, nReaders);.
360da 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65  }../* Forward re
360db 66 20 64 75 65 20 74 6f 20 6d 75 74 75 61 6c 20  f due to mutual 
360dc 72 65 63 75 72 73 69 6f 6e 20 77 69 74 68 20 73  recursion with s
360dd 65 67 64 69 72 4e 65 78 74 49 6e 64 65 78 28 29  egdirNextIndex()
360de 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  . */.static int 
360df 73 65 67 6d 65 6e 74 4d 65 72 67 65 28 66 75 6c  segmentMerge(ful
360e0 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69  ltext_vtab *v, i
360e1 6e 74 20 69 4c 65 76 65 6c 29 3b 0a 0a 2f 2a 20  nt iLevel);../* 
360e2 50 75 74 20 74 68 65 20 6e 65 78 74 20 61 76 61  Put the next ava
360e3 69 6c 61 62 6c 65 20 69 6e 64 65 78 20 61 74 20  ilable index at 
360e4 69 4c 65 76 65 6c 20 69 6e 74 6f 20 2a 70 69 64  iLevel into *pid
360e5 78 2e 20 20 49 66 20 69 4c 65 76 65 6c 0a 2a 2a  x.  If iLevel.**
360e6 20 61 6c 72 65 61 64 79 20 68 61 73 20 4d 45 52   already has MER
360e7 47 45 5f 43 4f 55 4e 54 20 73 65 67 6d 65 6e 74  GE_COUNT segment
360e8 73 2c 20 74 68 65 79 20 61 72 65 20 6d 65 72 67  s, they are merg
360e9 65 64 20 74 6f 20 61 20 68 69 67 68 65 72 0a 2a  ed to a higher.*
360ea 2a 20 6c 65 76 65 6c 20 74 6f 20 6d 61 6b 65 20  * level to make 
360eb 72 6f 6f 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  room..*/.static 
360ec 69 6e 74 20 73 65 67 64 69 72 4e 65 78 74 49 6e  int segdirNextIn
360ed 64 65 78 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  dex(fulltext_vta
360ee 62 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c  b *v, int iLevel
360ef 2c 20 69 6e 74 20 2a 70 69 64 78 29 7b 0a 20 20  , int *pidx){.  
360f0 69 6e 74 20 72 63 20 3d 20 73 65 67 64 69 72 5f  int rc = segdir_
360f1 6d 61 78 5f 69 6e 64 65 78 28 76 2c 20 69 4c 65  max_index(v, iLe
360f2 76 65 6c 2c 20 70 69 64 78 29 3b 0a 20 20 69 66  vel, pidx);.  if
360f3 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
360f4 45 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  E ){            
360f5 20 20 2f 2a 20 4e 6f 20 73 65 67 6d 65 6e 74 73    /* No segments
360f6 20 61 74 20 69 4c 65 76 65 6c 2e 20 2a 2f 0a 20   at iLevel. */. 
360f7 20 20 20 2a 70 69 64 78 20 3d 20 30 3b 0a 20 20     *pidx = 0;.  
360f8 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51  }else if( rc==SQ
360f9 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
360fa 69 66 28 20 2a 70 69 64 78 3d 3d 28 4d 45 52 47  if( *pidx==(MERG
360fb 45 5f 43 4f 55 4e 54 2d 31 29 20 29 7b 0a 20 20  E_COUNT-1) ){.  
360fc 20 20 20 20 72 63 20 3d 20 73 65 67 6d 65 6e 74      rc = segment
360fd 4d 65 72 67 65 28 76 2c 20 69 4c 65 76 65 6c 29  Merge(v, iLevel)
360fe 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
360ff 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
36100 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 2a 70 69  rn rc;.      *pi
36101 64 78 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  dx = 0;.    }els
36102 65 7b 0a 20 20 20 20 20 20 28 2a 70 69 64 78 29  e{.      (*pidx)
36103 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  ++;.    }.  }els
36104 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  e{.    return rc
36105 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
36106 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20  QLITE_OK;.}../* 
36107 4d 65 72 67 65 20 4d 45 52 47 45 5f 43 4f 55 4e  Merge MERGE_COUN
36108 54 20 73 65 67 6d 65 6e 74 73 20 61 74 20 69 4c  T segments at iL
36109 65 76 65 6c 20 69 6e 74 6f 20 61 20 6e 65 77 20  evel into a new 
3610a 73 65 67 6d 65 6e 74 20 61 74 0a 2a 2a 20 69 4c  segment at.** iL
3610b 65 76 65 6c 2b 31 2e 20 20 49 66 20 69 4c 65 76  evel+1.  If iLev
3610c 65 6c 2b 31 20 69 73 20 61 6c 72 65 61 64 79 20  el+1 is already 
3610d 66 75 6c 6c 20 6f 66 20 73 65 67 6d 65 6e 74 73  full of segments
3610e 2c 20 74 68 6f 73 65 20 77 69 6c 6c 20 62 65 0a  , those will be.
3610f 2a 2a 20 6d 65 72 67 65 64 20 74 6f 20 6d 61 6b  ** merged to mak
36110 65 20 72 6f 6f 6d 2e 0a 2a 2f 0a 73 74 61 74 69  e room..*/.stati
36111 63 20 69 6e 74 20 73 65 67 6d 65 6e 74 4d 65 72  c int segmentMer
36112 67 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  ge(fulltext_vtab
36113 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 29   *v, int iLevel)
36114 7b 0a 20 20 4c 65 61 66 57 72 69 74 65 72 20 77  {.  LeafWriter w
36115 72 69 74 65 72 3b 0a 20 20 4c 65 61 76 65 73 52  riter;.  LeavesR
36116 65 61 64 65 72 20 6c 72 73 5b 4d 45 52 47 45 5f  eader lrs[MERGE_
36117 43 4f 55 4e 54 5d 3b 0a 20 20 69 6e 74 20 69 2c  COUNT];.  int i,
36118 20 72 63 2c 20 69 64 78 20 3d 20 30 3b 0a 0a 20   rc, idx = 0;.. 
36119 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68   /* Determine th
3611a 65 20 6e 65 78 74 20 61 76 61 69 6c 61 62 6c 65  e next available
3611b 20 73 65 67 6d 65 6e 74 20 69 6e 64 65 78 20 61   segment index a
3611c 74 20 74 68 65 20 6e 65 78 74 20 6c 65 76 65 6c  t the next level
3611d 2c 0a 20 20 2a 2a 20 6d 65 72 67 69 6e 67 20 61  ,.  ** merging a
3611e 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 20 20 2a  s necessary..  *
3611f 2f 0a 20 20 72 63 20 3d 20 73 65 67 64 69 72 4e  /.  rc = segdirN
36120 65 78 74 49 6e 64 65 78 28 76 2c 20 69 4c 65 76  extIndex(v, iLev
36121 65 6c 2b 31 2c 20 26 69 64 78 29 3b 0a 20 20 69  el+1, &idx);.  i
36122 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
36123 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
36124 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20   /* TODO(shess) 
36125 54 68 69 73 20 61 73 73 75 6d 65 73 20 74 68 61  This assumes tha
36126 74 20 77 65 27 6c 6c 20 61 6c 77 61 79 73 20 73  t we'll always s
36127 65 65 20 65 78 61 63 74 6c 79 0a 20 20 2a 2a 20  ee exactly.  ** 
36128 4d 45 52 47 45 5f 43 4f 55 4e 54 20 73 65 67 6d  MERGE_COUNT segm
36129 65 6e 74 73 20 74 6f 20 6d 65 72 67 65 20 61 74  ents to merge at
3612a 20 61 20 67 69 76 65 6e 20 6c 65 76 65 6c 2e 20   a given level. 
3612b 20 54 68 61 74 20 77 69 6c 6c 20 62 65 0a 20 20   That will be.  
3612c 2a 2a 20 62 72 6f 6b 65 6e 20 69 66 20 77 65 20  ** broken if we 
3612d 61 6c 6c 6f 77 20 74 68 65 20 64 65 76 65 6c 6f  allow the develo
3612e 70 65 72 20 74 6f 20 72 65 71 75 65 73 74 20 70  per to request p
3612f 72 65 65 6d 70 74 69 76 65 20 6f 72 0a 20 20 2a  reemptive or.  *
36130 2a 20 64 65 66 65 72 72 65 64 20 6d 65 72 67 69  * deferred mergi
36131 6e 67 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65  ng..  */.  memse
36132 74 28 26 6c 72 73 2c 20 27 5c 30 27 2c 20 73 69  t(&lrs, '\0', si
36133 7a 65 6f 66 28 6c 72 73 29 29 3b 0a 20 20 72 63  zeof(lrs));.  rc
36134 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 73   = leavesReaders
36135 49 6e 69 74 28 76 2c 20 69 4c 65 76 65 6c 2c 20  Init(v, iLevel, 
36136 6c 72 73 2c 20 26 69 29 3b 0a 20 20 69 66 28 20  lrs, &i);.  if( 
36137 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
36138 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 61 73 73  return rc;.  ass
36139 65 72 74 28 20 69 3d 3d 4d 45 52 47 45 5f 43 4f  ert( i==MERGE_CO
3613a 55 4e 54 20 29 3b 0a 0a 20 20 6c 65 61 66 57 72  UNT );..  leafWr
3613b 69 74 65 72 49 6e 69 74 28 69 4c 65 76 65 6c 2b  iterInit(iLevel+
3613c 31 2c 20 69 64 78 2c 20 26 77 72 69 74 65 72 29  1, idx, &writer)
3613d 3b 0a 0a 20 20 2f 2a 20 53 69 6e 63 65 20 6c 65  ;..  /* Since le
3613e 61 76 65 73 52 65 61 64 65 72 52 65 6f 72 64 65  avesReaderReorde
3613f 72 28 29 20 70 75 73 68 65 73 20 72 65 61 64 65  r() pushes reade
36140 72 73 20 61 74 20 65 6f 66 20 74 6f 20 74 68 65  rs at eof to the
36141 20 65 6e 64 2c 0a 20 20 2a 2a 20 77 68 65 6e 20   end,.  ** when 
36142 74 68 65 20 66 69 72 73 74 20 72 65 61 64 65 72  the first reader
36143 20 69 73 20 65 6d 70 74 79 2c 20 61 6c 6c 20 77   is empty, all w
36144 69 6c 6c 20 62 65 20 65 6d 70 74 79 2e 0a 20 20  ill be empty..  
36145 2a 2f 0a 20 20 77 68 69 6c 65 28 20 21 6c 65 61  */.  while( !lea
36146 76 65 73 52 65 61 64 65 72 41 74 45 6e 64 28 6c  vesReaderAtEnd(l
36147 72 73 29 20 29 7b 0a 20 20 20 20 2f 2a 20 46 69  rs) ){.    /* Fi
36148 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e  gure out how man
36149 79 20 72 65 61 64 65 72 73 20 73 68 61 72 65 20  y readers share 
3614a 74 68 65 69 72 20 6e 65 78 74 20 74 65 72 6d 2e  their next term.
3614b 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b   */.    for(i=1;
3614c 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 20 26   i<MERGE_COUNT &
3614d 26 20 21 6c 65 61 76 65 73 52 65 61 64 65 72 41  & !leavesReaderA
3614e 74 45 6e 64 28 6c 72 73 2b 69 29 3b 20 69 2b 2b  tEnd(lrs+i); i++
3614f 29 7b 0a 20 20 20 20 20 20 69 66 28 20 30 21 3d  ){.      if( 0!=
36150 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d  leavesReaderTerm
36151 43 6d 70 28 6c 72 73 2c 20 6c 72 73 2b 69 29 20  Cmp(lrs, lrs+i) 
36152 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a  ) break;.    }..
36153 20 20 20 20 72 63 20 3d 20 6c 65 61 76 65 73 52      rc = leavesR
36154 65 61 64 65 72 73 4d 65 72 67 65 28 76 2c 20 6c  eadersMerge(v, l
36155 72 73 2c 20 69 2c 20 26 77 72 69 74 65 72 29 3b  rs, i, &writer);
36156 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
36157 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72  ITE_OK ) goto er
36158 72 3b 0a 0a 20 20 20 20 2f 2a 20 53 74 65 70 20  r;..    /* Step 
36159 66 6f 72 77 61 72 64 20 74 68 6f 73 65 20 74 68  forward those th
3615a 61 74 20 77 65 72 65 20 6d 65 72 67 65 64 2e 20  at were merged. 
3615b 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 69 2d  */.    while( i-
3615c 2d 3e 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ->0 ){.      rc 
3615d 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 53 74  = leavesReaderSt
3615e 65 70 28 76 2c 20 6c 72 73 2b 69 29 3b 0a 20 20  ep(v, lrs+i);.  
3615f 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
36160 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72  TE_OK ) goto err
36161 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 52 65 6f 72  ;..      /* Reor
36162 64 65 72 20 62 79 20 74 65 72 6d 2c 20 74 68 65  der by term, the
36163 6e 20 62 79 20 61 67 65 2e 20 2a 2f 0a 20 20 20  n by age. */.   
36164 20 20 20 6c 65 61 76 65 73 52 65 61 64 65 72 52     leavesReaderR
36165 65 6f 72 64 65 72 28 6c 72 73 2b 69 2c 20 4d 45  eorder(lrs+i, ME
36166 52 47 45 5f 43 4f 55 4e 54 2d 69 29 3b 0a 20 20  RGE_COUNT-i);.  
36167 20 20 7d 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69    }.  }..  for(i
36168 3d 30 3b 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e  =0; i<MERGE_COUN
36169 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6c 65 61  T; i++){.    lea
3616a 76 65 73 52 65 61 64 65 72 44 65 73 74 72 6f 79  vesReaderDestroy
3616b 28 26 6c 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 0a  (&lrs[i]);.  }..
3616c 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65    rc = leafWrite
3616d 72 46 69 6e 61 6c 69 7a 65 28 76 2c 20 26 77 72  rFinalize(v, &wr
3616e 69 74 65 72 29 3b 0a 20 20 6c 65 61 66 57 72 69  iter);.  leafWri
3616f 74 65 72 44 65 73 74 72 6f 79 28 26 77 72 69 74  terDestroy(&writ
36170 65 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  er);.  if( rc!=S
36171 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
36172 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65  n rc;..  /* Dele
36173 74 65 20 74 68 65 20 6d 65 72 67 65 64 20 73 65  te the merged se
36174 67 6d 65 6e 74 20 64 61 74 61 2e 20 2a 2f 0a 20  gment data. */. 
36175 20 72 65 74 75 72 6e 20 73 65 67 64 69 72 5f 64   return segdir_d
36176 65 6c 65 74 65 28 76 2c 20 69 4c 65 76 65 6c 29  elete(v, iLevel)
36177 3b 0a 0a 20 65 72 72 3a 0a 20 20 66 6f 72 28 69  ;.. err:.  for(i
36178 3d 30 3b 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e  =0; i<MERGE_COUN
36179 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6c 65 61  T; i++){.    lea
3617a 76 65 73 52 65 61 64 65 72 44 65 73 74 72 6f 79  vesReaderDestroy
3617b 28 26 6c 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 20  (&lrs[i]);.  }. 
3617c 20 6c 65 61 66 57 72 69 74 65 72 44 65 73 74 72   leafWriterDestr
3617d 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 20 20 72  oy(&writer);.  r
3617e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
3617f 41 63 63 75 6d 75 6c 61 74 65 20 74 68 65 20 75  Accumulate the u
36180 6e 69 6f 6e 20 6f 66 20 2a 61 63 63 20 61 6e 64  nion of *acc and
36181 20 2a 70 44 61 74 61 20 69 6e 74 6f 20 2a 61 63   *pData into *ac
36182 63 2e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  c. */.static voi
36183 64 20 64 6f 63 4c 69 73 74 41 63 63 75 6d 75 6c  d docListAccumul
36184 61 74 65 55 6e 69 6f 6e 28 44 61 74 61 42 75 66  ateUnion(DataBuf
36185 66 65 72 20 2a 61 63 63 2c 0a 20 20 20 20 20 20  fer *acc,.      
36186 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36187 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
36188 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
36189 69 6e 74 20 6e 44 61 74 61 29 20 7b 0a 20 20 44  int nData) {.  D
3618a 61 74 61 42 75 66 66 65 72 20 74 6d 70 20 3d 20  ataBuffer tmp = 
3618b 2a 61 63 63 3b 0a 20 20 64 61 74 61 42 75 66 66  *acc;.  dataBuff
3618c 65 72 49 6e 69 74 28 61 63 63 2c 20 74 6d 70 2e  erInit(acc, tmp.
3618d 6e 44 61 74 61 2b 6e 44 61 74 61 29 3b 0a 20 20  nData+nData);.  
3618e 64 6f 63 4c 69 73 74 55 6e 69 6f 6e 28 74 6d 70  docListUnion(tmp
3618f 2e 70 44 61 74 61 2c 20 74 6d 70 2e 6e 44 61 74  .pData, tmp.nDat
36190 61 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c  a, pData, nData,
36191 20 61 63 63 29 3b 0a 20 20 64 61 74 61 42 75 66   acc);.  dataBuf
36192 66 65 72 44 65 73 74 72 6f 79 28 26 74 6d 70 29  ferDestroy(&tmp)
36193 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  ;.}../* TODO(she
36194 73 73 29 20 49 74 20 6d 69 67 68 74 20 62 65 20  ss) It might be 
36195 69 6e 74 65 72 65 73 74 69 6e 67 20 74 6f 20 65  interesting to e
36196 78 70 6c 6f 72 65 20 64 69 66 66 65 72 65 6e 74  xplore different
36197 20 6d 65 72 67 65 0a 2a 2a 20 73 74 72 61 74 65   merge.** strate
36198 67 69 65 73 2c 20 68 65 72 65 2e 20 20 46 6f 72  gies, here.  For
36199 20 69 6e 73 74 61 6e 63 65 2c 20 73 69 6e 63 65   instance, since
3619a 20 74 68 69 73 20 69 73 20 61 20 73 6f 72 74 65   this is a sorte
3619b 64 20 6d 65 72 67 65 2c 20 77 65 0a 2a 2a 20 63  d merge, we.** c
3619c 6f 75 6c 64 20 65 61 73 69 6c 79 20 6d 65 72 67  ould easily merg
3619d 65 20 6d 61 6e 79 20 64 6f 63 6c 69 73 74 73 20  e many doclists 
3619e 69 6e 20 70 61 72 61 6c 6c 65 6c 2e 20 20 57 69  in parallel.  Wi
3619f 74 68 20 73 6f 6d 65 0a 2a 2a 20 63 6f 6d 70 72  th some.** compr
361a0 65 68 65 6e 73 69 6f 6e 20 6f 66 20 74 68 65 20  ehension of the 
361a1 73 74 6f 72 61 67 65 20 66 6f 72 6d 61 74 2c 20  storage format, 
361a2 77 65 20 63 6f 75 6c 64 20 6d 65 72 67 65 20 61  we could merge a
361a3 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a 20 64 6f 63  ll of the.** doc
361a4 6c 69 73 74 73 20 77 69 74 68 69 6e 20 61 20 6c  lists within a l
361a5 65 61 66 20 6e 6f 64 65 20 64 69 72 65 63 74 6c  eaf node directl
361a6 79 20 66 72 6f 6d 20 74 68 65 20 6c 65 61 66 20  y from the leaf 
361a7 6e 6f 64 65 27 73 20 73 74 6f 72 61 67 65 2e 0a  node's storage..
361a8 2a 2a 20 49 74 20 6d 61 79 20 62 65 20 77 6f 72  ** It may be wor
361a9 74 68 77 68 69 6c 65 20 74 6f 20 6d 65 72 67 65  thwhile to merge
361aa 20 73 6d 61 6c 6c 65 72 20 64 6f 63 6c 69 73 74   smaller doclist
361ab 73 20 62 65 66 6f 72 65 20 6c 61 72 67 65 72 0a  s before larger.
361ac 2a 2a 20 64 6f 63 6c 69 73 74 73 2c 20 73 69 6e  ** doclists, sin
361ad 63 65 20 74 68 65 79 20 63 61 6e 20 62 65 20 74  ce they can be t
361ae 72 61 76 65 72 73 65 64 20 6d 6f 72 65 20 71 75  raversed more qu
361af 69 63 6b 6c 79 20 2d 20 62 75 74 20 74 68 65 0a  ickly - but the.
361b0 2a 2a 20 72 65 73 75 6c 74 73 20 6d 61 79 20 68  ** results may h
361b1 61 76 65 20 6c 65 73 73 20 6f 76 65 72 6c 61 70  ave less overlap
361b2 2c 20 6d 61 6b 69 6e 67 20 74 68 65 6d 20 6d 6f  , making them mo
361b3 72 65 20 65 78 70 65 6e 73 69 76 65 20 69 6e 20  re expensive in 
361b4 61 0a 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 77  a.** different w
361b5 61 79 2e 0a 2a 2f 0a 0a 2f 2a 20 53 63 61 6e 20  ay..*/../* Scan 
361b6 70 52 65 61 64 65 72 20 66 6f 72 20 70 54 65 72  pReader for pTer
361b7 6d 2f 6e 54 65 72 6d 2c 20 61 6e 64 20 6d 65 72  m/nTerm, and mer
361b8 67 65 20 74 68 65 20 74 65 72 6d 27 73 20 64 6f  ge the term's do
361b9 63 6c 69 73 74 20 6f 76 65 72 0a 2a 2a 20 2a 6f  clist over.** *o
361ba 75 74 20 28 61 6e 79 20 64 6f 63 6c 69 73 74 73  ut (any doclists
361bb 20 77 69 74 68 20 64 75 70 6c 69 63 61 74 65 20   with duplicate 
361bc 64 6f 63 69 64 73 20 6f 76 65 72 77 72 69 74 65  docids overwrite
361bd 20 74 68 6f 73 65 20 69 6e 20 2a 6f 75 74 29 2e   those in *out).
361be 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e  .** Internal fun
361bf 63 74 69 6f 6e 20 66 6f 72 20 6c 6f 61 64 53 65  ction for loadSe
361c0 67 6d 65 6e 74 4c 65 61 66 28 29 2e 0a 2a 2f 0a  gmentLeaf()..*/.
361c1 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 61 64 53  static int loadS
361c2 65 67 6d 65 6e 74 4c 65 61 76 65 73 49 6e 74 28  egmentLeavesInt(
361c3 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
361c4 2c 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  , LeavesReader *
361c5 70 52 65 61 64 65 72 2c 0a 20 20 20 20 20 20 20  pReader,.       
361c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
361c7 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
361c8 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20  har *pTerm, int 
361c9 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65  nTerm, int isPre
361ca 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20  fix,.           
361cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
361cc 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72 20       DataBuffer 
361cd 2a 6f 75 74 29 7b 0a 20 20 2f 2a 20 64 6f 63 6c  *out){.  /* docl
361ce 69 73 74 20 64 61 74 61 20 69 73 20 61 63 63 75  ist data is accu
361cf 6d 75 6c 61 74 65 64 20 69 6e 74 6f 20 70 42 75  mulated into pBu
361d0 66 66 65 72 73 20 73 69 6d 69 6c 61 72 20 74 6f  ffers similar to
361d1 20 68 6f 77 20 6f 6e 65 20 64 6f 65 73 0a 20 20   how one does.  
361d2 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 20 69 6e 20  ** increment in 
361d3 62 69 6e 61 72 79 20 61 72 69 74 68 6d 65 74 69  binary arithmeti
361d4 63 2e 20 20 49 66 20 69 6e 64 65 78 20 30 20 69  c.  If index 0 i
361d5 73 20 65 6d 70 74 79 2c 20 74 68 65 20 64 61 74  s empty, the dat
361d6 61 20 69 73 0a 20 20 2a 2a 20 73 74 6f 72 65 64  a is.  ** stored
361d7 20 74 68 65 72 65 2e 20 20 49 66 20 74 68 65 72   there.  If ther
361d8 65 20 69 73 20 64 61 74 61 20 74 68 65 72 65 2c  e is data there,
361d9 20 69 74 20 69 73 20 6d 65 72 67 65 64 20 61 6e   it is merged an
361da 64 20 74 68 65 0a 20 20 2a 2a 20 72 65 73 75 6c  d the.  ** resul
361db 74 73 20 63 61 72 72 69 65 64 20 69 6e 74 6f 20  ts carried into 
361dc 70 6f 73 69 74 69 6f 6e 20 31 2c 20 77 69 74 68  position 1, with
361dd 20 66 75 72 74 68 65 72 20 6d 65 72 67 65 2d 61   further merge-a
361de 6e 64 2d 63 61 72 72 79 0a 20 20 2a 2a 20 75 6e  nd-carry.  ** un
361df 74 69 6c 20 61 6e 20 65 6d 70 74 79 20 70 6f 73  til an empty pos
361e0 69 74 69 6f 6e 20 69 73 20 66 6f 75 6e 64 2e 0a  ition is found..
361e1 20 20 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65    */.  DataBuffe
361e2 72 20 2a 70 42 75 66 66 65 72 73 20 3d 20 4e 55  r *pBuffers = NU
361e3 4c 4c 3b 0a 20 20 69 6e 74 20 6e 42 75 66 66 65  LL;.  int nBuffe
361e4 72 73 20 3d 20 30 2c 20 6e 4d 61 78 42 75 66 66  rs = 0, nMaxBuff
361e5 65 72 73 20 3d 20 30 2c 20 72 63 3b 0a 0a 20 20  ers = 0, rc;..  
361e6 61 73 73 65 72 74 28 20 6e 54 65 72 6d 3e 30 20  assert( nTerm>0 
361e7 29 3b 0a 0a 20 20 66 6f 72 28 72 63 3d 53 51 4c  );..  for(rc=SQL
361e8 49 54 45 5f 4f 4b 3b 20 72 63 3d 3d 53 51 4c 49  ITE_OK; rc==SQLI
361e9 54 45 5f 4f 4b 20 26 26 20 21 6c 65 61 76 65 73  TE_OK && !leaves
361ea 52 65 61 64 65 72 41 74 45 6e 64 28 70 52 65 61  ReaderAtEnd(pRea
361eb 64 65 72 29 3b 0a 20 20 20 20 20 20 72 63 3d 6c  der);.      rc=l
361ec 65 61 76 65 73 52 65 61 64 65 72 53 74 65 70 28  eavesReaderStep(
361ed 76 2c 20 70 52 65 61 64 65 72 29 29 7b 0a 20 20  v, pReader)){.  
361ee 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29    /* TODO(shess)
361ef 20 52 65 61 6c 6c 79 20 77 61 6e 74 20 6c 65 61   Really want lea
361f0 76 65 73 52 65 61 64 65 72 54 65 72 6d 43 6d 70  vesReaderTermCmp
361f1 28 29 2c 20 62 75 74 20 74 68 61 74 20 6e 61 6d  (), but that nam
361f2 65 20 69 73 0a 20 20 20 20 2a 2a 20 61 6c 72 65  e is.    ** alre
361f3 61 64 79 20 74 61 6b 65 6e 20 74 6f 20 63 6f 6d  ady taken to com
361f4 70 61 72 65 20 74 68 65 20 74 65 72 6d 73 20 6f  pare the terms o
361f5 66 20 74 77 6f 20 4c 65 61 76 65 73 52 65 61 64  f two LeavesRead
361f6 65 72 73 2e 20 20 54 68 69 6e 6b 0a 20 20 20 20  ers.  Think.    
361f7 2a 2a 20 6f 6e 20 61 20 62 65 74 74 65 72 20 6e  ** on a better n
361f8 61 6d 65 2e 20 20 5b 4d 65 61 6e 77 68 69 6c 65  ame.  [Meanwhile
361f9 2c 20 62 72 65 61 6b 20 65 6e 63 61 70 73 75 6c  , break encapsul
361fa 61 74 69 6f 6e 20 72 61 74 68 65 72 20 74 68 61  ation rather tha
361fb 6e 0a 20 20 20 20 2a 2a 20 75 73 65 20 61 20 63  n.    ** use a c
361fc 6f 6e 66 75 73 69 6e 67 20 6e 61 6d 65 2e 5d 0a  onfusing name.].
361fd 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63      */.    int c
361fe 20 3d 20 6c 65 61 66 52 65 61 64 65 72 54 65 72   = leafReaderTer
361ff 6d 43 6d 70 28 26 70 52 65 61 64 65 72 2d 3e 6c  mCmp(&pReader->l
36200 65 61 66 52 65 61 64 65 72 2c 20 70 54 65 72 6d  eafReader, pTerm
36201 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69  , nTerm, isPrefi
36202 78 29 3b 0a 20 20 20 20 69 66 28 20 63 3e 30 20  x);.    if( c>0 
36203 29 20 62 72 65 61 6b 3b 20 20 20 20 20 20 2f 2a  ) break;      /*
36204 20 50 61 73 74 20 61 6e 79 20 70 6f 73 73 69 62   Past any possib
36205 6c 65 20 6d 61 74 63 68 65 73 2e 20 2a 2f 0a 20  le matches. */. 
36206 20 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20     if( c==0 ){. 
36207 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
36208 2a 70 44 61 74 61 20 3d 20 6c 65 61 76 65 73 52  *pData = leavesR
36209 65 61 64 65 72 44 61 74 61 28 70 52 65 61 64 65  eaderData(pReade
3620a 72 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 42  r);.      int iB
3620b 75 66 66 65 72 2c 20 6e 44 61 74 61 20 3d 20 6c  uffer, nData = l
3620c 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61 42  eavesReaderDataB
3620d 79 74 65 73 28 70 52 65 61 64 65 72 29 3b 0a 0a  ytes(pReader);..
3620e 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 20 74 68        /* Find th
3620f 65 20 66 69 72 73 74 20 65 6d 70 74 79 20 62 75  e first empty bu
36210 66 66 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 66  ffer. */.      f
36211 6f 72 28 69 42 75 66 66 65 72 3d 30 3b 20 69 42  or(iBuffer=0; iB
36212 75 66 66 65 72 3c 6e 42 75 66 66 65 72 73 3b 20  uffer<nBuffers; 
36213 2b 2b 69 42 75 66 66 65 72 29 7b 0a 20 20 20 20  ++iBuffer){.    
36214 20 20 20 20 69 66 28 20 30 3d 3d 70 42 75 66 66      if( 0==pBuff
36215 65 72 73 5b 69 42 75 66 66 65 72 5d 2e 6e 44 61  ers[iBuffer].nDa
36216 74 61 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  ta ) break;.    
36217 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 75    }..      /* Ou
36218 74 20 6f 66 20 62 75 66 66 65 72 73 2c 20 61 64  t of buffers, ad
36219 64 20 61 6e 20 65 6d 70 74 79 20 6f 6e 65 2e 20  d an empty one. 
3621a 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 42 75  */.      if( iBu
3621b 66 66 65 72 3d 3d 6e 42 75 66 66 65 72 73 20 29  ffer==nBuffers )
3621c 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 42  {.        if( nB
3621d 75 66 66 65 72 73 3d 3d 6e 4d 61 78 42 75 66 66  uffers==nMaxBuff
3621e 65 72 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ers ){.         
3621f 20 44 61 74 61 42 75 66 66 65 72 20 2a 70 3b 0a   DataBuffer *p;.
36220 20 20 20 20 20 20 20 20 20 20 6e 4d 61 78 42 75            nMaxBu
36221 66 66 65 72 73 20 2b 3d 20 32 30 3b 0a 0a 20 20  ffers += 20;..  
36222 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6e 75 61          /* Manua
36223 6c 20 72 65 61 6c 6c 6f 63 20 73 6f 20 77 65 20  l realloc so we 
36224 63 61 6e 20 68 61 6e 64 6c 65 20 4e 55 4c 4c 20  can handle NULL 
36225 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 20 2a  appropriately. *
36226 2f 0a 20 20 20 20 20 20 20 20 20 20 70 20 3d 20  /.          p = 
36227 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
36228 4d 61 78 42 75 66 66 65 72 73 2a 73 69 7a 65 6f  MaxBuffers*sizeo
36229 66 28 2a 70 42 75 66 66 65 72 73 29 29 3b 0a 20  f(*pBuffers));. 
3622a 20 20 20 20 20 20 20 20 20 69 66 28 20 70 3d 3d           if( p==
3622b 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20  NULL ){.        
3622c 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
3622d 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20  NOMEM;.         
3622e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3622f 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20      }..         
36230 20 69 66 28 20 6e 42 75 66 66 65 72 73 3e 30 20   if( nBuffers>0 
36231 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  ){.            a
36232 73 73 65 72 74 28 70 42 75 66 66 65 72 73 21 3d  ssert(pBuffers!=
36233 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 20 20 20  NULL);.         
36234 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 42 75     memcpy(p, pBu
36235 66 66 65 72 73 2c 20 6e 42 75 66 66 65 72 73 2a  ffers, nBuffers*
36236 73 69 7a 65 6f 66 28 2a 70 42 75 66 66 65 72 73  sizeof(*pBuffers
36237 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ));.            
36238 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 75  sqlite3_free(pBu
36239 66 66 65 72 73 29 3b 0a 20 20 20 20 20 20 20 20  ffers);.        
3623a 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 42    }.          pB
3623b 75 66 66 65 72 73 20 3d 20 70 3b 0a 20 20 20 20  uffers = p;.    
3623c 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 61      }.        da
3623d 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 28 70  taBufferInit(&(p
3623e 42 75 66 66 65 72 73 5b 6e 42 75 66 66 65 72 73  Buffers[nBuffers
3623f 5d 29 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  ]), 0);.        
36240 6e 42 75 66 66 65 72 73 2b 2b 3b 0a 20 20 20 20  nBuffers++;.    
36241 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 41 74    }..      /* At
36242 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 6d 75 73   this point, mus
36243 74 20 68 61 76 65 20 61 6e 20 65 6d 70 74 79 20  t have an empty 
36244 61 74 20 69 42 75 66 66 65 72 2e 20 2a 2f 0a 20  at iBuffer. */. 
36245 20 20 20 20 20 61 73 73 65 72 74 28 69 42 75 66       assert(iBuf
36246 66 65 72 3c 6e 42 75 66 66 65 72 73 20 26 26 20  fer<nBuffers && 
36247 70 42 75 66 66 65 72 73 5b 69 42 75 66 66 65 72  pBuffers[iBuffer
36248 5d 2e 6e 44 61 74 61 3d 3d 30 29 3b 0a 0a 20 20  ].nData==0);..  
36249 20 20 20 20 2f 2a 20 49 66 20 65 6d 70 74 79 20      /* If empty 
3624a 77 61 73 20 66 69 72 73 74 20 62 75 66 66 65 72  was first buffer
3624b 2c 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 6d 65  , no need for me
3624c 72 67 65 20 6c 6f 67 69 63 2e 20 2a 2f 0a 20 20  rge logic. */.  
3624d 20 20 20 20 69 66 28 20 69 42 75 66 66 65 72 3d      if( iBuffer=
3624e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 64 61  =0 ){.        da
3624f 74 61 42 75 66 66 65 72 52 65 70 6c 61 63 65 28  taBufferReplace(
36250 26 28 70 42 75 66 66 65 72 73 5b 30 5d 29 2c 20  &(pBuffers[0]), 
36251 70 44 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 20  pData, nData);. 
36252 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
36253 20 20 20 20 2f 2a 20 70 41 63 63 20 69 73 20 74      /* pAcc is t
36254 68 65 20 65 6d 70 74 79 20 62 75 66 66 65 72 20  he empty buffer 
36255 74 68 65 20 6d 65 72 67 65 64 20 64 61 74 61 20  the merged data 
36256 77 69 6c 6c 20 65 6e 64 20 75 70 20 69 6e 2e 20  will end up in. 
36257 2a 2f 0a 20 20 20 20 20 20 20 20 44 61 74 61 42  */.        DataB
36258 75 66 66 65 72 20 2a 70 41 63 63 20 3d 20 26 28  uffer *pAcc = &(
36259 70 42 75 66 66 65 72 73 5b 69 42 75 66 66 65 72  pBuffers[iBuffer
3625a 5d 29 3b 0a 20 20 20 20 20 20 20 20 44 61 74 61  ]);.        Data
3625b 42 75 66 66 65 72 20 2a 70 20 3d 20 26 28 70 42  Buffer *p = &(pB
3625c 75 66 66 65 72 73 5b 30 5d 29 3b 0a 0a 20 20 20  uffers[0]);..   
3625d 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 70       /* Handle p
3625e 6f 73 69 74 69 6f 6e 20 30 20 73 70 65 63 69 61  osition 0 specia
3625f 6c 6c 79 20 74 6f 20 61 76 6f 69 64 20 6e 65 65  lly to avoid nee
36260 64 20 74 6f 20 70 72 69 6d 65 20 70 41 63 63 0a  d to prime pAcc.
36261 20 20 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20          ** with 
36262 70 44 61 74 61 2f 6e 44 61 74 61 2e 0a 20 20 20  pData/nData..   
36263 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
36264 64 61 74 61 42 75 66 66 65 72 53 77 61 70 28 70  dataBufferSwap(p
36265 2c 20 70 41 63 63 29 3b 0a 20 20 20 20 20 20 20  , pAcc);.       
36266 20 64 6f 63 4c 69 73 74 41 63 63 75 6d 75 6c 61   docListAccumula
36267 74 65 55 6e 69 6f 6e 28 70 41 63 63 2c 20 70 44  teUnion(pAcc, pD
36268 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 0a 20 20  ata, nData);..  
36269 20 20 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c        /* Accumul
3626a 61 74 65 20 72 65 6d 61 69 6e 69 6e 67 20 64 6f  ate remaining do
3626b 63 6c 69 73 74 73 20 69 6e 74 6f 20 70 41 63 63  clists into pAcc
3626c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72  . */.        for
3626d 28 2b 2b 70 3b 20 70 3c 70 41 63 63 3b 20 2b 2b  (++p; p<pAcc; ++
3626e 70 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 6f  p){.          do
3626f 63 4c 69 73 74 41 63 63 75 6d 75 6c 61 74 65 55  cListAccumulateU
36270 6e 69 6f 6e 28 70 41 63 63 2c 20 70 2d 3e 70 44  nion(pAcc, p->pD
36271 61 74 61 2c 20 70 2d 3e 6e 44 61 74 61 29 3b 0a  ata, p->nData);.
36272 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 61  .          /* da
36273 74 61 42 75 66 66 65 72 52 65 73 65 74 28 29 20  taBufferReset() 
36274 63 6f 75 6c 64 20 61 6c 6c 6f 77 20 61 20 6c 61  could allow a la
36275 72 67 65 20 64 6f 63 6c 69 73 74 20 74 6f 20 62  rge doclist to b
36276 6c 6f 77 20 75 70 0a 20 20 20 20 20 20 20 20 20  low up.         
36277 20 2a 2a 20 6f 75 72 20 6d 65 6d 6f 72 79 20 72   ** our memory r
36278 65 71 75 69 72 65 6d 65 6e 74 73 2e 0a 20 20 20  equirements..   
36279 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
3627a 20 20 20 20 69 66 28 20 70 2d 3e 6e 43 61 70 61      if( p->nCapa
3627b 63 69 74 79 3c 31 30 32 34 20 29 7b 0a 20 20 20  city<1024 ){.   
3627c 20 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66           dataBuf
3627d 66 65 72 52 65 73 65 74 28 70 29 3b 0a 20 20 20  ferReset(p);.   
3627e 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
3627f 20 20 20 20 20 20 20 20 20 20 64 61 74 61 42 75            dataBu
36280 66 66 65 72 44 65 73 74 72 6f 79 28 70 29 3b 0a  fferDestroy(p);.
36281 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61              data
36282 42 75 66 66 65 72 49 6e 69 74 28 70 2c 20 30 29  BufferInit(p, 0)
36283 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
36284 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
36285 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
36286 55 6e 69 6f 6e 20 61 6c 6c 20 74 68 65 20 64 6f  Union all the do
36287 63 6c 69 73 74 73 20 74 6f 67 65 74 68 65 72 20  clists together 
36288 69 6e 74 6f 20 2a 6f 75 74 2e 20 2a 2f 0a 20 20  into *out. */.  
36289 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 57  /* TODO(shess) W
3628a 68 61 74 20 69 66 20 2a 6f 75 74 20 69 73 20 62  hat if *out is b
3628b 69 67 3f 20 20 53 69 67 68 2e 20 2a 2f 0a 20 20  ig?  Sigh. */.  
3628c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3628d 4b 20 26 26 20 6e 42 75 66 66 65 72 73 3e 30 20  K && nBuffers>0 
3628e 29 7b 0a 20 20 20 20 69 6e 74 20 69 42 75 66 66  ){.    int iBuff
3628f 65 72 3b 0a 20 20 20 20 66 6f 72 28 69 42 75 66  er;.    for(iBuf
36290 66 65 72 3d 30 3b 20 69 42 75 66 66 65 72 3c 6e  fer=0; iBuffer<n
36291 42 75 66 66 65 72 73 3b 20 2b 2b 69 42 75 66 66  Buffers; ++iBuff
36292 65 72 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  er){.      if( p
36293 42 75 66 66 65 72 73 5b 69 42 75 66 66 65 72 5d  Buffers[iBuffer]
36294 2e 6e 44 61 74 61 3e 30 20 29 7b 0a 20 20 20 20  .nData>0 ){.    
36295 20 20 20 20 69 66 28 20 6f 75 74 2d 3e 6e 44 61      if( out->nDa
36296 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ta==0 ){.       
36297 20 20 20 64 61 74 61 42 75 66 66 65 72 53 77 61     dataBufferSwa
36298 70 28 6f 75 74 2c 20 26 28 70 42 75 66 66 65 72  p(out, &(pBuffer
36299 73 5b 69 42 75 66 66 65 72 5d 29 29 3b 0a 20 20  s[iBuffer]));.  
3629a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
3629b 20 20 20 20 20 20 20 64 6f 63 4c 69 73 74 41 63         docListAc
3629c 63 75 6d 75 6c 61 74 65 55 6e 69 6f 6e 28 6f 75  cumulateUnion(ou
3629d 74 2c 20 70 42 75 66 66 65 72 73 5b 69 42 75 66  t, pBuffers[iBuf
3629e 66 65 72 5d 2e 70 44 61 74 61 2c 0a 20 20 20 20  fer].pData,.    
3629f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
362a0 20 20 20 20 20 20 20 20 20 20 20 20 20 70 42 75               pBu
362a1 66 66 65 72 73 5b 69 42 75 66 66 65 72 5d 2e 6e  ffers[iBuffer].n
362a2 44 61 74 61 29 3b 0a 20 20 20 20 20 20 20 20 7d  Data);.        }
362a3 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
362a4 20 7d 0a 0a 20 20 77 68 69 6c 65 28 20 6e 42 75   }..  while( nBu
362a5 66 66 65 72 73 2d 2d 20 29 7b 0a 20 20 20 20 64  ffers-- ){.    d
362a6 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
362a7 28 26 28 70 42 75 66 66 65 72 73 5b 6e 42 75 66  (&(pBuffers[nBuf
362a8 66 65 72 73 5d 29 29 3b 0a 20 20 7d 0a 20 20 69  fers]));.  }.  i
362a9 66 28 20 70 42 75 66 66 65 72 73 21 3d 4e 55 4c  f( pBuffers!=NUL
362aa 4c 20 29 20 73 71 6c 69 74 65 33 5f 66 72 65 65  L ) sqlite3_free
362ab 28 70 42 75 66 66 65 72 73 29 3b 0a 0a 20 20 72  (pBuffers);..  r
362ac 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
362ad 43 61 6c 6c 20 6c 6f 61 64 53 65 67 6d 65 6e 74  Call loadSegment
362ae 4c 65 61 76 65 73 49 6e 74 28 29 20 77 69 74 68  LeavesInt() with
362af 20 70 44 61 74 61 2f 6e 44 61 74 61 20 61 73 20   pData/nData as 
362b0 69 6e 70 75 74 2e 20 2a 2f 0a 73 74 61 74 69 63  input. */.static
362b1 20 69 6e 74 20 6c 6f 61 64 53 65 67 6d 65 6e 74   int loadSegment
362b2 4c 65 61 66 28 66 75 6c 6c 74 65 78 74 5f 76 74  Leaf(fulltext_vt
362b3 61 62 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61  ab *v, const cha
362b4 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44  r *pData, int nD
362b5 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
362b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
362b7 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72  const char *pTer
362b8 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e  m, int nTerm, in
362b9 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20 20 20  t isPrefix,.    
362ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
362bb 20 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65         DataBuffe
362bc 72 20 2a 6f 75 74 29 7b 0a 20 20 4c 65 61 76 65  r *out){.  Leave
362bd 73 52 65 61 64 65 72 20 72 65 61 64 65 72 3b 0a  sReader reader;.
362be 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73    int rc;..  ass
362bf 65 72 74 28 20 6e 44 61 74 61 3e 31 20 29 3b 0a  ert( nData>1 );.
362c0 20 20 61 73 73 65 72 74 28 20 2a 70 44 61 74 61    assert( *pData
362c1 3d 3d 27 5c 30 27 20 29 3b 0a 20 20 72 63 20 3d  =='\0' );.  rc =
362c2 20 6c 65 61 76 65 73 52 65 61 64 65 72 49 6e 69   leavesReaderIni
362c3 74 28 76 2c 20 30 2c 20 30 2c 20 30 2c 20 70 44  t(v, 0, 0, 0, pD
362c4 61 74 61 2c 20 6e 44 61 74 61 2c 20 26 72 65 61  ata, nData, &rea
362c5 64 65 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  der);.  if( rc!=
362c6 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
362c7 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 6c  rn rc;..  rc = l
362c8 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73  oadSegmentLeaves
362c9 49 6e 74 28 76 2c 20 26 72 65 61 64 65 72 2c 20  Int(v, &reader, 
362ca 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73  pTerm, nTerm, is
362cb 50 72 65 66 69 78 2c 20 6f 75 74 29 3b 0a 20 20  Prefix, out);.  
362cc 6c 65 61 76 65 73 52 65 61 64 65 72 52 65 73 65  leavesReaderRese
362cd 74 28 26 72 65 61 64 65 72 29 3b 0a 20 20 6c 65  t(&reader);.  le
362ce 61 76 65 73 52 65 61 64 65 72 44 65 73 74 72 6f  avesReaderDestro
362cf 79 28 26 72 65 61 64 65 72 29 3b 0a 20 20 72 65  y(&reader);.  re
362d0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 43  turn rc;.}../* C
362d1 61 6c 6c 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c  all loadSegmentL
362d2 65 61 76 65 73 49 6e 74 28 29 20 77 69 74 68 20  eavesInt() with 
362d3 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 73 20 66  the leaf nodes f
362d4 72 6f 6d 20 69 53 74 61 72 74 4c 65 61 66 20 74  rom iStartLeaf t
362d5 6f 0a 2a 2a 20 69 45 6e 64 4c 65 61 66 20 28 69  o.** iEndLeaf (i
362d6 6e 63 6c 75 73 69 76 65 29 20 61 73 20 69 6e 70  nclusive) as inp
362d7 75 74 2c 20 61 6e 64 20 6d 65 72 67 65 20 74 68  ut, and merge th
362d8 65 20 72 65 73 75 6c 74 69 6e 67 20 64 6f 63 6c  e resulting docl
362d9 69 73 74 20 69 6e 74 6f 0a 2a 2a 20 6f 75 74 2e  ist into.** out.
362da 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
362db 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73  oadSegmentLeaves
362dc 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
362dd 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  v,.             
362de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
362df 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53 74  sqlite_int64 iSt
362e0 61 72 74 4c 65 61 66 2c 20 73 71 6c 69 74 65 5f  artLeaf, sqlite_
362e1 69 6e 74 36 34 20 69 45 6e 64 4c 65 61 66 2c 0a  int64 iEndLeaf,.
362e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
362e3 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
362e4 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20  st char *pTerm, 
362e5 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69  int nTerm, int i
362e6 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20  sPrefix,.       
362e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
362e8 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72        DataBuffer
362e9 20 2a 6f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63   *out){.  int rc
362ea 3b 0a 20 20 4c 65 61 76 65 73 52 65 61 64 65 72  ;.  LeavesReader
362eb 20 72 65 61 64 65 72 3b 0a 0a 20 20 61 73 73 65   reader;..  asse
362ec 72 74 28 20 69 53 74 61 72 74 4c 65 61 66 3c 3d  rt( iStartLeaf<=
362ed 69 45 6e 64 4c 65 61 66 20 29 3b 0a 20 20 72 63  iEndLeaf );.  rc
362ee 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 49   = leavesReaderI
362ef 6e 69 74 28 76 2c 20 30 2c 20 69 53 74 61 72 74  nit(v, 0, iStart
362f0 4c 65 61 66 2c 20 69 45 6e 64 4c 65 61 66 2c 20  Leaf, iEndLeaf, 
362f1 4e 55 4c 4c 2c 20 30 2c 20 26 72 65 61 64 65 72  NULL, 0, &reader
362f2 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
362f3 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
362f4 72 63 3b 0a 0a 20 20 72 63 20 3d 20 6c 6f 61 64  rc;..  rc = load
362f5 53 65 67 6d 65 6e 74 4c 65 61 76 65 73 49 6e 74  SegmentLeavesInt
362f6 28 76 2c 20 26 72 65 61 64 65 72 2c 20 70 54 65  (v, &reader, pTe
362f7 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65  rm, nTerm, isPre
362f8 66 69 78 2c 20 6f 75 74 29 3b 0a 20 20 6c 65 61  fix, out);.  lea
362f9 76 65 73 52 65 61 64 65 72 52 65 73 65 74 28 26  vesReaderReset(&
362fa 72 65 61 64 65 72 29 3b 0a 20 20 6c 65 61 76 65  reader);.  leave
362fb 73 52 65 61 64 65 72 44 65 73 74 72 6f 79 28 26  sReaderDestroy(&
362fc 72 65 61 64 65 72 29 3b 0a 20 20 72 65 74 75 72  reader);.  retur
362fd 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 61 6b 69  n rc;.}../* Taki
362fe 6e 67 20 70 44 61 74 61 2f 6e 44 61 74 61 20 61  ng pData/nData a
362ff 73 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f  s an interior no
36300 64 65 2c 20 66 69 6e 64 20 74 68 65 20 73 65 71  de, find the seq
36301 75 65 6e 63 65 20 6f 66 20 63 68 69 6c 64 0a 2a  uence of child.*
36302 2a 20 6e 6f 64 65 73 20 77 68 69 63 68 20 63 6f  * nodes which co
36303 75 6c 64 20 69 6e 63 6c 75 64 65 20 70 54 65 72  uld include pTer
36304 6d 2f 6e 54 65 72 6d 2f 69 73 50 72 65 66 69 78  m/nTerm/isPrefix
36305 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  .  Note that the
36306 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  .** interior nod
36307 65 20 74 65 72 6d 73 20 6c 6f 67 69 63 61 6c 6c  e terms logicall
36308 79 20 63 6f 6d 65 20 62 65 74 77 65 65 6e 20 74  y come between t
36309 68 65 20 62 6c 6f 63 6b 73 2c 20 73 6f 20 74 68  he blocks, so th
3630a 65 72 65 20 69 73 0a 2a 2a 20 6f 6e 65 20 6d 6f  ere is.** one mo
3630b 72 65 20 62 6c 6f 63 6b 69 64 20 74 68 61 6e 20  re blockid than 
3630c 74 68 65 72 65 20 61 72 65 20 74 65 72 6d 73 20  there are terms 
3630d 28 74 68 61 74 20 62 6c 6f 63 6b 20 63 6f 6e 74  (that block cont
3630e 61 69 6e 73 20 74 65 72 6d 73 20 3e 3d 0a 2a 2a  ains terms >=.**
3630f 20 74 68 65 20 6c 61 73 74 20 69 6e 74 65 72 69   the last interi
36310 6f 72 2d 6e 6f 64 65 20 74 65 72 6d 29 2e 0a 2a  or-node term)..*
36311 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  /./* TODO(shess)
36312 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 63 6f 64   The calling cod
36313 65 20 6d 61 79 20 61 6c 72 65 61 64 79 20 6b 6e  e may already kn
36314 6f 77 20 74 68 61 74 20 74 68 65 20 65 6e 64 20  ow that the end 
36315 63 68 69 6c 64 20 69 73 0a 2a 2a 20 6e 6f 74 20  child is.** not 
36316 77 6f 72 74 68 20 63 61 6c 63 75 6c 61 74 69 6e  worth calculatin
36317 67 2c 20 62 65 63 61 75 73 65 20 74 68 65 20 65  g, because the e
36318 6e 64 20 6d 61 79 20 62 65 20 69 6e 20 61 20 6c  nd may be in a l
36319 61 74 65 72 20 73 69 62 6c 69 6e 67 0a 2a 2a 20  ater sibling.** 
3631a 6e 6f 64 65 2e 20 20 43 6f 6e 73 69 64 65 72 20  node.  Consider 
3631b 77 68 65 74 68 65 72 20 62 72 65 61 6b 69 6e 67  whether breaking
3631c 20 73 79 6d 6d 65 74 72 79 20 69 73 20 77 6f 72   symmetry is wor
3631d 74 68 77 68 69 6c 65 2e 20 20 49 20 73 75 73 70  thwhile.  I susp
3631e 65 63 74 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74  ect.** it is not
3631f 20 77 6f 72 74 68 77 68 69 6c 65 2e 0a 2a 2f 0a   worthwhile..*/.
36320 73 74 61 74 69 63 20 76 6f 69 64 20 67 65 74 43  static void getC
36321 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e  hildrenContainin
36322 67 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44  g(const char *pD
36323 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a  ata, int nData,.
36324 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36325 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36326 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54    const char *pT
36327 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20  erm, int nTerm, 
36328 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20  int isPrefix,.  
36329 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3632a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3632b 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69  sqlite_int64 *pi
3632c 53 74 61 72 74 43 68 69 6c 64 2c 0a 20 20 20 20  StartChild,.    
3632d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3632e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
3632f 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69 45 6e  lite_int64 *piEn
36330 64 43 68 69 6c 64 29 7b 0a 20 20 49 6e 74 65 72  dChild){.  Inter
36331 69 6f 72 52 65 61 64 65 72 20 72 65 61 64 65 72  iorReader reader
36332 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61  ;..  assert( nDa
36333 74 61 3e 31 20 29 3b 0a 20 20 61 73 73 65 72 74  ta>1 );.  assert
36334 28 20 2a 70 44 61 74 61 21 3d 27 5c 30 27 20 29  ( *pData!='\0' )
36335 3b 0a 20 20 69 6e 74 65 72 69 6f 72 52 65 61 64  ;.  interiorRead
36336 65 72 49 6e 69 74 28 70 44 61 74 61 2c 20 6e 44  erInit(pData, nD
36337 61 74 61 2c 20 26 72 65 61 64 65 72 29 3b 0a 0a  ata, &reader);..
36338 20 20 2f 2a 20 53 63 61 6e 20 66 6f 72 20 74 68    /* Scan for th
36339 65 20 66 69 72 73 74 20 63 68 69 6c 64 20 77 68  e first child wh
3633a 69 63 68 20 63 6f 75 6c 64 20 63 6f 6e 74 61 69  ich could contai
3633b 6e 20 70 54 65 72 6d 2f 6e 54 65 72 6d 2e 20 2a  n pTerm/nTerm. *
3633c 2f 0a 20 20 77 68 69 6c 65 28 20 21 69 6e 74 65  /.  while( !inte
3633d 72 69 6f 72 52 65 61 64 65 72 41 74 45 6e 64 28  riorReaderAtEnd(
3633e 26 72 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20  &reader) ){.    
3633f 69 66 28 20 69 6e 74 65 72 69 6f 72 52 65 61 64  if( interiorRead
36340 65 72 54 65 72 6d 43 6d 70 28 26 72 65 61 64 65  erTermCmp(&reade
36341 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c  r, pTerm, nTerm,
36342 20 30 29 3e 30 20 29 20 62 72 65 61 6b 3b 0a 20   0)>0 ) break;. 
36343 20 20 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65     interiorReade
36344 72 53 74 65 70 28 26 72 65 61 64 65 72 29 3b 0a  rStep(&reader);.
36345 20 20 7d 0a 20 20 2a 70 69 53 74 61 72 74 43 68    }.  *piStartCh
36346 69 6c 64 20 3d 20 69 6e 74 65 72 69 6f 72 52 65  ild = interiorRe
36347 61 64 65 72 43 75 72 72 65 6e 74 42 6c 6f 63 6b  aderCurrentBlock
36348 69 64 28 26 72 65 61 64 65 72 29 3b 0a 0a 20 20  id(&reader);..  
36349 2f 2a 20 4b 65 65 70 20 73 63 61 6e 6e 69 6e 67  /* Keep scanning
3634a 20 74 6f 20 66 69 6e 64 20 61 20 74 65 72 6d 20   to find a term 
3634b 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 75 72  greater than our
3634c 20 74 65 72 6d 2c 20 75 73 69 6e 67 20 70 72 65   term, using pre
3634d 66 69 78 0a 20 20 2a 2a 20 63 6f 6d 70 61 72 69  fix.  ** compari
3634e 73 6f 6e 20 69 66 20 69 6e 64 69 63 61 74 65 64  son if indicated
3634f 2e 20 20 49 66 20 69 73 50 72 65 66 69 78 20 69  .  If isPrefix i
36350 73 20 66 61 6c 73 65 2c 20 74 68 69 73 20 77 69  s false, this wi
36351 6c 6c 20 62 65 20 74 68 65 0a 20 20 2a 2a 20 73  ll be the.  ** s
36352 61 6d 65 20 62 6c 6f 63 6b 69 64 20 61 73 20 74  ame blockid as t
36353 68 65 20 73 74 61 72 74 69 6e 67 20 62 6c 6f 63  he starting bloc
36354 6b 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28  k..  */.  while(
36355 20 21 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72   !interiorReader
36356 41 74 45 6e 64 28 26 72 65 61 64 65 72 29 20 29  AtEnd(&reader) )
36357 7b 0a 20 20 20 20 69 66 28 20 69 6e 74 65 72 69  {.    if( interi
36358 6f 72 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28  orReaderTermCmp(
36359 26 72 65 61 64 65 72 2c 20 70 54 65 72 6d 2c 20  &reader, pTerm, 
3635a 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78 29  nTerm, isPrefix)
3635b 3e 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  >0 ) break;.    
3635c 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 53 74  interiorReaderSt
3635d 65 70 28 26 72 65 61 64 65 72 29 3b 0a 20 20 7d  ep(&reader);.  }
3635e 0a 20 20 2a 70 69 45 6e 64 43 68 69 6c 64 20 3d  .  *piEndChild =
3635f 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 43   interiorReaderC
36360 75 72 72 65 6e 74 42 6c 6f 63 6b 69 64 28 26 72  urrentBlockid(&r
36361 65 61 64 65 72 29 3b 0a 0a 20 20 69 6e 74 65 72  eader);..  inter
36362 69 6f 72 52 65 61 64 65 72 44 65 73 74 72 6f 79  iorReaderDestroy
36363 28 26 72 65 61 64 65 72 29 3b 0a 0a 20 20 2f 2a  (&reader);..  /*
36364 20 43 68 69 6c 64 72 65 6e 20 6d 75 73 74 20 61   Children must a
36365 73 63 65 6e 64 2c 20 61 6e 64 20 69 66 20 21 70  scend, and if !p
36366 72 65 66 69 78 2c 20 62 6f 74 68 20 6d 75 73 74  refix, both must
36367 20 62 65 20 74 68 65 20 73 61 6d 65 2e 20 2a 2f   be the same. */
36368 0a 20 20 61 73 73 65 72 74 28 20 2a 70 69 45 6e  .  assert( *piEn
36369 64 43 68 69 6c 64 3e 3d 2a 70 69 53 74 61 72 74  dChild>=*piStart
3636a 43 68 69 6c 64 20 29 3b 0a 20 20 61 73 73 65 72  Child );.  asser
3636b 74 28 20 69 73 50 72 65 66 69 78 20 7c 7c 20 2a  t( isPrefix || *
3636c 70 69 53 74 61 72 74 43 68 69 6c 64 3d 3d 2a 70  piStartChild==*p
3636d 69 45 6e 64 43 68 69 6c 64 20 29 3b 0a 7d 0a 0a  iEndChild );.}..
3636e 2f 2a 20 52 65 61 64 20 62 6c 6f 63 6b 20 61 74  /* Read block at
3636f 20 69 42 6c 6f 63 6b 69 64 20 61 6e 64 20 70 61   iBlockid and pa
36370 73 73 20 69 74 20 77 69 74 68 20 6f 74 68 65 72  ss it with other
36371 20 70 61 72 61 6d 73 20 74 6f 0a 2a 2a 20 67 65   params to.** ge
36372 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e  tChildrenContain
36373 69 6e 67 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  ing()..*/.static
36374 20 69 6e 74 20 6c 6f 61 64 41 6e 64 47 65 74 43   int loadAndGetC
36375 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e  hildrenContainin
36376 67 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74  g(.  fulltext_vt
36377 61 62 20 2a 76 2c 0a 20 20 73 71 6c 69 74 65 5f  ab *v,.  sqlite_
36378 69 6e 74 36 34 20 69 42 6c 6f 63 6b 69 64 2c 0a  int64 iBlockid,.
36379 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54    const char *pT
3637a 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20  erm, int nTerm, 
3637b 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20  int isPrefix,.  
3637c 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69  sqlite_int64 *pi
3637d 53 74 61 72 74 43 68 69 6c 64 2c 20 73 71 6c 69  StartChild, sqli
3637e 74 65 5f 69 6e 74 36 34 20 2a 70 69 45 6e 64 43  te_int64 *piEndC
3637f 68 69 6c 64 0a 29 7b 0a 20 20 73 71 6c 69 74 65  hild.){.  sqlite
36380 33 5f 73 74 6d 74 20 2a 73 20 3d 20 4e 55 4c 4c  3_stmt *s = NULL
36381 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61  ;.  int rc;..  a
36382 73 73 65 72 74 28 20 69 42 6c 6f 63 6b 69 64 21  ssert( iBlockid!
36383 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
36384 70 54 65 72 6d 21 3d 4e 55 4c 4c 20 29 3b 0a 20  pTerm!=NULL );. 
36385 20 61 73 73 65 72 74 28 20 6e 54 65 72 6d 21 3d   assert( nTerm!=
36386 30 20 29 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  0 );        /* T
36387 4f 44 4f 28 73 68 65 73 73 29 20 57 68 79 20 6e  ODO(shess) Why n
36388 6f 74 20 61 6c 6c 6f 77 20 74 68 69 73 3f 20 2a  ot allow this? *
36389 2f 0a 20 20 61 73 73 65 72 74 28 20 70 69 53 74  /.  assert( piSt
3638a 61 72 74 43 68 69 6c 64 21 3d 4e 55 4c 4c 20 29  artChild!=NULL )
3638b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 69 45 6e  ;.  assert( piEn
3638c 64 43 68 69 6c 64 21 3d 4e 55 4c 4c 20 29 3b 0a  dChild!=NULL );.
3638d 0a 20 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f  .  rc = sql_get_
3638e 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 42 4c 4f  statement(v, BLO
3638f 43 4b 5f 53 45 4c 45 43 54 5f 53 54 4d 54 2c 20  CK_SELECT_STMT, 
36390 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  &s);.  if( rc!=S
36391 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
36392 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
36393 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
36394 28 73 2c 20 31 2c 20 69 42 6c 6f 63 6b 69 64 29  (s, 1, iBlockid)
36395 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
36396 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
36397 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  c;..  rc = sqlit
36398 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66  e3_step(s);.  if
36399 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
3639a 45 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  E ) return SQLIT
3639b 45 5f 45 52 52 4f 52 3b 0a 20 20 69 66 28 20 72  E_ERROR;.  if( r
3639c 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20  c!=SQLITE_ROW ) 
3639d 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 67 65  return rc;..  ge
3639e 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e  tChildrenContain
3639f 69 6e 67 28 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ing(sqlite3_colu
363a0 6d 6e 5f 62 6c 6f 62 28 73 2c 20 30 29 2c 20 73  mn_blob(s, 0), s
363a1 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
363a2 74 65 73 28 73 2c 20 30 29 2c 0a 20 20 20 20 20  tes(s, 0),.     
363a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
363a4 20 20 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c     pTerm, nTerm,
363a5 20 69 73 50 72 65 66 69 78 2c 20 70 69 53 74 61   isPrefix, piSta
363a6 72 74 43 68 69 6c 64 2c 20 70 69 45 6e 64 43 68  rtChild, piEndCh
363a7 69 6c 64 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 65  ild);..  /* We e
363a8 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72  xpect only one r
363a9 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65 78 65  ow.  We must exe
363aa 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73 71 6c  cute another sql
363ab 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20 20 2a  ite3_step().   *
363ac 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65   to complete the
363ad 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65   iteration; othe
363ae 72 77 69 73 65 20 74 68 65 20 74 61 62 6c 65 20  rwise the table 
363af 77 69 6c 6c 20 72 65 6d 61 69 6e 0a 20 20 20 2a  will remain.   *
363b0 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63   locked. */.  rc
363b1 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
363b2 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  s);.  if( rc==SQ
363b3 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72  LITE_ROW ) retur
363b4 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
363b5 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
363b6 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 72  _DONE ) return r
363b7 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  c;..  return SQL
363b8 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 72  ITE_OK;.}../* Tr
363b9 61 76 65 72 73 65 20 74 68 65 20 74 72 65 65 20  averse the tree 
363ba 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 70  represented by p
363bb 44 61 74 61 5b 6e 44 61 74 61 5d 20 6c 6f 6f 6b  Data[nData] look
363bc 69 6e 67 20 66 6f 72 0a 2a 2a 20 70 54 65 72 6d  ing for.** pTerm
363bd 5b 6e 54 65 72 6d 5d 2c 20 70 6c 61 63 69 6e 67  [nTerm], placing
363be 20 69 74 73 20 64 6f 63 6c 69 73 74 20 69 6e 74   its doclist int
363bf 6f 20 2a 6f 75 74 2e 20 20 54 68 69 73 20 69 73  o *out.  This is
363c0 20 69 6e 74 65 72 6e 61 6c 20 74 6f 0a 2a 2a 20   internal to.** 
363c1 6c 6f 61 64 53 65 67 6d 65 6e 74 28 29 20 74 6f  loadSegment() to
363c2 20 6d 61 6b 65 20 65 72 72 6f 72 2d 68 61 6e 64   make error-hand
363c3 6c 69 6e 67 20 63 6c 65 61 6e 65 72 2e 0a 2a 2f  ling cleaner..*/
363c4 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 61 64  .static int load
363c5 53 65 67 6d 65 6e 74 49 6e 74 28 66 75 6c 6c 74  SegmentInt(fullt
363c6 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 63 6f 6e  ext_vtab *v, con
363c7 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
363c8 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 20  int nData,.     
363c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
363ca 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
363cb 34 20 69 4c 65 61 76 65 73 45 6e 64 2c 0a 20 20  4 iLeavesEnd,.  
363cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
363cd 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
363ce 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e  ar *pTerm, int n
363cf 54 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66  Term, int isPref
363d0 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ix,.            
363d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 44 61                Da
363d2 74 61 42 75 66 66 65 72 20 2a 6f 75 74 29 7b 0a  taBuffer *out){.
363d3 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73    /* Special cas
363d4 65 20 77 68 65 72 65 20 72 6f 6f 74 20 69 73 20  e where root is 
363d5 61 20 6c 65 61 66 2e 20 2a 2f 0a 20 20 69 66 28  a leaf. */.  if(
363d6 20 2a 70 44 61 74 61 3d 3d 27 5c 30 27 20 29 7b   *pData=='\0' ){
363d7 0a 20 20 20 20 72 65 74 75 72 6e 20 6c 6f 61 64  .    return load
363d8 53 65 67 6d 65 6e 74 4c 65 61 66 28 76 2c 20 70  SegmentLeaf(v, p
363d9 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 70 54 65  Data, nData, pTe
363da 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65  rm, nTerm, isPre
363db 66 69 78 2c 20 6f 75 74 29 3b 0a 20 20 7d 65 6c  fix, out);.  }el
363dc 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a  se{.    int rc;.
363dd 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
363de 20 69 53 74 61 72 74 43 68 69 6c 64 2c 20 69 45   iStartChild, iE
363df 6e 64 43 68 69 6c 64 3b 0a 0a 20 20 20 20 2f 2a  ndChild;..    /*
363e0 20 50 72 6f 63 65 73 73 20 70 44 61 74 61 20 61   Process pData a
363e1 73 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f  s an interior no
363e2 64 65 2c 20 74 68 65 6e 20 6c 6f 6f 70 20 64 6f  de, then loop do
363e3 77 6e 20 74 68 65 20 74 72 65 65 0a 20 20 20 20  wn the tree.    
363e4 2a 2a 20 75 6e 74 69 6c 20 77 65 20 66 69 6e 64  ** until we find
363e5 20 74 68 65 20 73 65 74 20 6f 66 20 6c 65 61 66   the set of leaf
363e6 20 6e 6f 64 65 73 20 74 6f 20 73 63 61 6e 20 66   nodes to scan f
363e7 6f 72 20 74 68 65 20 74 65 72 6d 2e 0a 20 20 20  or the term..   
363e8 20 2a 2f 0a 20 20 20 20 67 65 74 43 68 69 6c 64   */.    getChild
363e9 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 70 44  renContaining(pD
363ea 61 74 61 2c 20 6e 44 61 74 61 2c 20 70 54 65 72  ata, nData, pTer
363eb 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66  m, nTerm, isPref
363ec 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ix,.            
363ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 69                &i
363ee 53 74 61 72 74 43 68 69 6c 64 2c 20 26 69 45 6e  StartChild, &iEn
363ef 64 43 68 69 6c 64 29 3b 0a 20 20 20 20 77 68 69  dChild);.    whi
363f0 6c 65 28 20 69 53 74 61 72 74 43 68 69 6c 64 3e  le( iStartChild>
363f1 69 4c 65 61 76 65 73 45 6e 64 20 29 7b 0a 20 20  iLeavesEnd ){.  
363f2 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
363f3 20 69 4e 65 78 74 53 74 61 72 74 2c 20 69 4e 65   iNextStart, iNe
363f4 78 74 45 6e 64 3b 0a 20 20 20 20 20 20 72 63 20  xtEnd;.      rc 
363f5 3d 20 6c 6f 61 64 41 6e 64 47 65 74 43 68 69 6c  = loadAndGetChil
363f6 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 76  drenContaining(v
363f7 2c 20 69 53 74 61 72 74 43 68 69 6c 64 2c 20 70  , iStartChild, p
363f8 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50  Term, nTerm, isP
363f9 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20  refix,.         
363fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
363fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
363fc 69 4e 65 78 74 53 74 61 72 74 2c 20 26 69 4e 65  iNextStart, &iNe
363fd 78 74 45 6e 64 29 3b 0a 20 20 20 20 20 20 69 66  xtEnd);.      if
363fe 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
363ff 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
36400 20 20 20 20 2f 2a 20 49 66 20 77 65 27 76 65 20      /* If we've 
36401 62 72 61 6e 63 68 65 64 2c 20 66 6f 6c 6c 6f 77  branched, follow
36402 20 74 68 65 20 65 6e 64 20 62 72 61 6e 63 68 2c   the end branch,
36403 20 74 6f 6f 2e 20 2a 2f 0a 20 20 20 20 20 20 69   too. */.      i
36404 66 28 20 69 53 74 61 72 74 43 68 69 6c 64 21 3d  f( iStartChild!=
36405 69 45 6e 64 43 68 69 6c 64 20 29 7b 0a 20 20 20  iEndChild ){.   
36406 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
36407 34 20 69 44 75 6d 6d 79 3b 0a 20 20 20 20 20 20  4 iDummy;.      
36408 20 20 72 63 20 3d 20 6c 6f 61 64 41 6e 64 47 65    rc = loadAndGe
36409 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e  tChildrenContain
3640a 69 6e 67 28 76 2c 20 69 45 6e 64 43 68 69 6c 64  ing(v, iEndChild
3640b 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20  , pTerm, nTerm, 
3640c 69 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20  isPrefix,.      
3640d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3640e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3640f 20 20 20 20 26 69 44 75 6d 6d 79 2c 20 26 69 4e      &iDummy, &iN
36410 65 78 74 45 6e 64 29 3b 0a 20 20 20 20 20 20 20  extEnd);.       
36411 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
36412 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
36413 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61        }..      a
36414 73 73 65 72 74 28 20 69 4e 65 78 74 53 74 61 72  ssert( iNextStar
36415 74 3c 3d 69 4e 65 78 74 45 6e 64 20 29 3b 0a 20  t<=iNextEnd );. 
36416 20 20 20 20 20 69 53 74 61 72 74 43 68 69 6c 64       iStartChild
36417 20 3d 20 69 4e 65 78 74 53 74 61 72 74 3b 0a 20   = iNextStart;. 
36418 20 20 20 20 20 69 45 6e 64 43 68 69 6c 64 20 3d       iEndChild =
36419 20 69 4e 65 78 74 45 6e 64 3b 0a 20 20 20 20 7d   iNextEnd;.    }
3641a 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 53 74  .    assert( iSt
3641b 61 72 74 43 68 69 6c 64 3c 3d 69 4c 65 61 76 65  artChild<=iLeave
3641c 73 45 6e 64 20 29 3b 0a 20 20 20 20 61 73 73 65  sEnd );.    asse
3641d 72 74 28 20 69 45 6e 64 43 68 69 6c 64 3c 3d 69  rt( iEndChild<=i
3641e 4c 65 61 76 65 73 45 6e 64 20 29 3b 0a 0a 20 20  LeavesEnd );..  
3641f 20 20 2f 2a 20 53 63 61 6e 20 74 68 72 6f 75 67    /* Scan throug
36420 68 20 74 68 65 20 6c 65 61 66 20 73 65 67 6d 65  h the leaf segme
36421 6e 74 73 20 66 6f 72 20 64 6f 63 6c 69 73 74 73  nts for doclists
36422 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  . */.    return 
36423 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65  loadSegmentLeave
36424 73 28 76 2c 20 69 53 74 61 72 74 43 68 69 6c 64  s(v, iStartChild
36425 2c 20 69 45 6e 64 43 68 69 6c 64 2c 0a 20 20 20  , iEndChild,.   
36426 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36427 20 20 20 20 20 20 20 20 20 20 70 54 65 72 6d 2c            pTerm,
36428 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78   nTerm, isPrefix
36429 2c 20 6f 75 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  , out);.  }.}../
3642a 2a 20 43 61 6c 6c 20 6c 6f 61 64 53 65 67 6d 65  * Call loadSegme
3642b 6e 74 49 6e 74 28 29 20 74 6f 20 63 6f 6c 6c 65  ntInt() to colle
3642c 63 74 20 74 68 65 20 64 6f 63 6c 69 73 74 20 66  ct the doclist f
3642d 6f 72 20 70 54 65 72 6d 2f 6e 54 65 72 6d 2c 20  or pTerm/nTerm, 
3642e 74 68 65 6e 0a 2a 2a 20 6d 65 72 67 65 20 69 74  then.** merge it
3642f 73 20 64 6f 63 6c 69 73 74 20 6f 76 65 72 20 2a  s doclist over *
36430 6f 75 74 20 28 61 6e 79 20 64 75 70 6c 69 63 61  out (any duplica
36431 74 65 20 64 6f 63 6c 69 73 74 73 20 72 65 61 64  te doclists read
36432 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 73 65 67   from the.** seg
36433 6d 65 6e 74 20 72 6f 6f 74 65 64 20 61 74 20 70  ment rooted at p
36434 44 61 74 61 20 77 69 6c 6c 20 6f 76 65 72 77 72  Data will overwr
36435 69 74 65 20 74 68 6f 73 65 20 69 6e 20 2a 6f 75  ite those in *ou
36436 74 29 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73  t)..*/./* TODO(s
36437 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72 20 63  hess) Consider c
36438 68 61 6e 67 69 6e 67 20 74 68 69 73 20 74 6f 20  hanging this to 
36439 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 64 65  determine the de
3643a 70 74 68 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 65  pth of the.** le
3643b 61 76 65 73 20 75 73 69 6e 67 20 65 69 74 68 65  aves using eithe
3643c 72 20 74 68 65 20 66 69 72 73 74 20 63 68 61 72  r the first char
3643d 61 63 74 65 72 73 20 6f 66 20 69 6e 74 65 72 69  acters of interi
3643e 6f 72 20 6e 6f 64 65 73 20 28 77 68 65 6e 0a 2a  or nodes (when.*
3643f 2a 20 3d 3d 31 2c 20 77 65 27 72 65 20 6f 6e 65  * ==1, we're one
36440 20 6c 65 76 65 6c 20 61 62 6f 76 65 20 74 68 65   level above the
36441 20 6c 65 61 76 65 73 29 2c 20 6f 72 20 74 68 65   leaves), or the
36442 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 72   first character
36443 20 6f 66 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20   of.** the root 
36444 28 77 68 69 63 68 20 77 69 6c 6c 20 64 65 73 63  (which will desc
36445 72 69 62 65 20 74 68 65 20 68 65 69 67 68 74 20  ribe the height 
36446 6f 66 20 74 68 65 20 74 72 65 65 20 64 69 72 65  of the tree dire
36447 63 74 6c 79 29 2e 0a 2a 2a 20 45 69 74 68 65 72  ctly)..** Either
36448 20 66 65 65 6c 73 20 73 6f 6d 65 77 68 61 74 20   feels somewhat 
36449 74 72 69 63 6b 79 20 74 6f 20 6d 65 2e 0a 2a 2f  tricky to me..*/
3644a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
3644b 54 68 65 20 63 75 72 72 65 6e 74 20 6d 65 72 67  The current merg
3644c 65 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62  e is likely to b
3644d 65 20 73 6c 6f 77 20 66 6f 72 20 6c 61 72 67 65  e slow for large
3644e 0a 2a 2a 20 64 6f 63 6c 69 73 74 73 20 28 74 68  .** doclists (th
3644f 6f 75 67 68 20 69 74 20 73 68 6f 75 6c 64 20 70  ough it should p
36450 72 6f 63 65 73 73 20 66 72 6f 6d 20 6e 65 77 65  rocess from newe
36451 73 74 2f 73 6d 61 6c 6c 65 73 74 20 74 6f 0a 2a  st/smallest to.*
36452 2a 20 6f 6c 64 65 73 74 2f 6c 61 72 67 65 73 74  * oldest/largest
36453 2c 20 73 6f 20 69 74 20 6d 61 79 20 6e 6f 74 20  , so it may not 
36454 62 65 20 74 68 61 74 20 62 61 64 29 2e 20 20 49  be that bad).  I
36455 74 20 6d 69 67 68 74 20 62 65 20 75 73 65 66 75  t might be usefu
36456 6c 20 74 6f 0a 2a 2a 20 6d 6f 64 69 66 79 20 74  l to.** modify t
36457 68 69 6e 67 73 20 74 6f 20 61 6c 6c 6f 77 20 66  hings to allow f
36458 6f 72 20 4e 2d 77 61 79 20 6d 65 72 67 69 6e 67  or N-way merging
36459 2e 20 20 54 68 69 73 20 63 6f 75 6c 64 20 65 69  .  This could ei
3645a 74 68 65 72 20 62 65 0a 2a 2a 20 77 69 74 68 69  ther be.** withi
3645b 6e 20 61 20 73 65 67 6d 65 6e 74 2c 20 77 69 74  n a segment, wit
3645c 68 20 70 61 69 72 77 69 73 65 20 6d 65 72 67 65  h pairwise merge
3645d 73 20 61 63 72 6f 73 73 20 73 65 67 6d 65 6e 74  s across segment
3645e 73 2c 20 6f 72 20 61 63 72 6f 73 73 0a 2a 2a 20  s, or across.** 
3645f 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20 61 74 20  all segments at 
36460 6f 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  once..*/.static 
36461 69 6e 74 20 6c 6f 61 64 53 65 67 6d 65 6e 74 28  int loadSegment(
36462 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
36463 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44  , const char *pD
36464 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a  ata, int nData,.
36465 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36466 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e         sqlite_in
36467 74 36 34 20 69 4c 65 61 76 65 73 45 6e 64 2c 0a  t64 iLeavesEnd,.
36468 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36469 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
3646a 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54  r *pTerm, int nT
3646b 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69  erm, int isPrefi
3646c 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  x,.             
3646d 20 20 20 20 20 20 20 20 20 20 44 61 74 61 42 75            DataBu
3646e 66 66 65 72 20 2a 6f 75 74 29 7b 0a 20 20 44 61  ffer *out){.  Da
3646f 74 61 42 75 66 66 65 72 20 72 65 73 75 6c 74 3b  taBuffer result;
36470 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73  .  int rc;..  as
36471 73 65 72 74 28 20 6e 44 61 74 61 3e 31 20 29 3b  sert( nData>1 );
36472 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f 64 65  ..  /* This code
36473 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65   should never be
36474 20 63 61 6c 6c 65 64 20 77 69 74 68 20 62 75 66   called with buf
36475 66 65 72 65 64 20 75 70 64 61 74 65 73 2e 20 2a  fered updates. *
36476 2f 0a 20 20 61 73 73 65 72 74 28 20 76 2d 3e 6e  /.  assert( v->n
36477 50 65 6e 64 69 6e 67 44 61 74 61 3c 30 20 29 3b  PendingData<0 );
36478 0a 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e  ..  dataBufferIn
36479 69 74 28 26 72 65 73 75 6c 74 2c 20 30 29 3b 0a  it(&result, 0);.
3647a 20 20 72 63 20 3d 20 6c 6f 61 64 53 65 67 6d 65    rc = loadSegme
3647b 6e 74 49 6e 74 28 76 2c 20 70 44 61 74 61 2c 20  ntInt(v, pData, 
3647c 6e 44 61 74 61 2c 20 69 4c 65 61 76 65 73 45 6e  nData, iLeavesEn
3647d 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
3647e 20 20 20 20 20 20 20 20 20 70 54 65 72 6d 2c 20           pTerm, 
3647f 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c  nTerm, isPrefix,
36480 20 26 72 65 73 75 6c 74 29 3b 0a 20 20 69 66 28   &result);.  if(
36481 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
36482 26 20 72 65 73 75 6c 74 2e 6e 44 61 74 61 3e 30  & result.nData>0
36483 20 29 7b 0a 20 20 20 20 69 66 28 20 6f 75 74 2d   ){.    if( out-
36484 3e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20  >nData==0 ){.   
36485 20 20 20 44 61 74 61 42 75 66 66 65 72 20 74 6d     DataBuffer tm
36486 70 20 3d 20 2a 6f 75 74 3b 0a 20 20 20 20 20 20  p = *out;.      
36487 2a 6f 75 74 20 3d 20 72 65 73 75 6c 74 3b 0a 20  *out = result;. 
36488 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 74 6d       result = tm
36489 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  p;.    }else{.  
3648a 20 20 20 20 44 61 74 61 42 75 66 66 65 72 20 6d      DataBuffer m
3648b 65 72 67 65 64 3b 0a 20 20 20 20 20 20 44 4c 52  erged;.      DLR
3648c 65 61 64 65 72 20 72 65 61 64 65 72 73 5b 32 5d  eader readers[2]
3648d 3b 0a 0a 20 20 20 20 20 20 64 6c 72 49 6e 69 74  ;..      dlrInit
3648e 28 26 72 65 61 64 65 72 73 5b 30 5d 2c 20 44 4c  (&readers[0], DL
3648f 5f 44 45 46 41 55 4c 54 2c 20 6f 75 74 2d 3e 70  _DEFAULT, out->p
36490 44 61 74 61 2c 20 6f 75 74 2d 3e 6e 44 61 74 61  Data, out->nData
36491 29 3b 0a 20 20 20 20 20 20 64 6c 72 49 6e 69 74  );.      dlrInit
36492 28 26 72 65 61 64 65 72 73 5b 31 5d 2c 20 44 4c  (&readers[1], DL
36493 5f 44 45 46 41 55 4c 54 2c 20 72 65 73 75 6c 74  _DEFAULT, result
36494 2e 70 44 61 74 61 2c 20 72 65 73 75 6c 74 2e 6e  .pData, result.n
36495 44 61 74 61 29 3b 0a 20 20 20 20 20 20 64 61 74  Data);.      dat
36496 61 42 75 66 66 65 72 49 6e 69 74 28 26 6d 65 72  aBufferInit(&mer
36497 67 65 64 2c 20 6f 75 74 2d 3e 6e 44 61 74 61 2b  ged, out->nData+
36498 72 65 73 75 6c 74 2e 6e 44 61 74 61 29 3b 0a 20  result.nData);. 
36499 20 20 20 20 20 64 6f 63 4c 69 73 74 4d 65 72 67       docListMerg
3649a 65 28 26 6d 65 72 67 65 64 2c 20 72 65 61 64 65  e(&merged, reade
3649b 72 73 2c 20 32 29 3b 0a 20 20 20 20 20 20 64 61  rs, 2);.      da
3649c 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
3649d 6f 75 74 29 3b 0a 20 20 20 20 20 20 2a 6f 75 74  out);.      *out
3649e 20 3d 20 6d 65 72 67 65 64 3b 0a 20 20 20 20 20   = merged;.     
3649f 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72 65 61   dlrDestroy(&rea
364a0 64 65 72 73 5b 30 5d 29 3b 0a 20 20 20 20 20 20  ders[0]);.      
364a1 64 6c 72 44 65 73 74 72 6f 79 28 26 72 65 61 64  dlrDestroy(&read
364a2 65 72 73 5b 31 5d 29 3b 0a 20 20 20 20 7d 0a 20  ers[1]);.    }. 
364a3 20 7d 0a 20 20 64 61 74 61 42 75 66 66 65 72 44   }.  dataBufferD
364a4 65 73 74 72 6f 79 28 26 72 65 73 75 6c 74 29 3b  estroy(&result);
364a5 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
364a6 0a 2f 2a 20 53 63 61 6e 20 74 68 65 20 64 61 74  ./* Scan the dat
364a7 61 62 61 73 65 20 61 6e 64 20 6d 65 72 67 65 20  abase and merge 
364a8 74 6f 67 65 74 68 65 72 20 74 68 65 20 70 6f 73  together the pos
364a9 74 69 6e 67 20 6c 69 73 74 73 20 66 6f 72 20 74  ting lists for t
364aa 68 65 20 74 65 72 6d 0a 2a 2a 20 69 6e 74 6f 20  he term.** into 
364ab 2a 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  *out..*/.static 
364ac 69 6e 74 20 74 65 72 6d 53 65 6c 65 63 74 28 0a  int termSelect(.
364ad 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20    fulltext_vtab 
364ae 2a 76 2c 20 0a 20 20 69 6e 74 20 69 43 6f 6c 75  *v, .  int iColu
364af 6d 6e 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  mn,.  const char
364b0 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65   *pTerm, int nTe
364b1 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  rm,             
364b2 2f 2a 20 54 65 72 6d 20 74 6f 20 71 75 65 72 79  /* Term to query
364b3 20 66 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 69 73   for */.  int is
364b4 50 72 65 66 69 78 2c 20 20 20 20 20 20 20 20 20  Prefix,         
364b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
364b6 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
364b7 61 20 70 72 65 66 69 78 20 73 65 61 72 63 68 20  a prefix search 
364b8 2a 2f 0a 20 20 44 6f 63 4c 69 73 74 54 79 70 65  */.  DocListType
364b9 20 69 54 79 70 65 2c 20 0a 20 20 44 61 74 61 42   iType, .  DataB
364ba 75 66 66 65 72 20 2a 6f 75 74 20 20 20 20 20 20  uffer *out      
364bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
364bc 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 72 65       /* Write re
364bd 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b  sults here */.){
364be 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 64 6f  .  DataBuffer do
364bf 63 6c 69 73 74 3b 0a 20 20 73 71 6c 69 74 65 33  clist;.  sqlite3
364c0 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20  _stmt *s;.  int 
364c1 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61  rc = sql_get_sta
364c2 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49 52  tement(v, SEGDIR
364c3 5f 53 45 4c 45 43 54 5f 41 4c 4c 5f 53 54 4d 54  _SELECT_ALL_STMT
364c4 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21  , &s);.  if( rc!
364c5 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
364c6 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 68  urn rc;..  /* Th
364c7 69 73 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e  is code should n
364c8 65 76 65 72 20 62 65 20 63 61 6c 6c 65 64 20 77  ever be called w
364c9 69 74 68 20 62 75 66 66 65 72 65 64 20 75 70 64  ith buffered upd
364ca 61 74 65 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72  ates. */.  asser
364cb 74 28 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61  t( v->nPendingDa
364cc 74 61 3c 30 20 29 3b 0a 0a 20 20 64 61 74 61 42  ta<0 );..  dataB
364cd 75 66 66 65 72 49 6e 69 74 28 26 64 6f 63 6c 69  ufferInit(&docli
364ce 73 74 2c 20 30 29 3b 0a 20 20 64 61 74 61 42 75  st, 0);.  dataBu
364cf 66 66 65 72 49 6e 69 74 28 6f 75 74 2c 20 30 29  fferInit(out, 0)
364d0 3b 0a 0a 20 20 2f 2a 20 54 72 61 76 65 72 73 65  ;..  /* Traverse
364d1 20 74 68 65 20 73 65 67 6d 65 6e 74 73 20 66 72   the segments fr
364d2 6f 6d 20 6f 6c 64 65 73 74 20 74 6f 20 6e 65 77  om oldest to new
364d3 65 73 74 20 73 6f 20 74 68 61 74 20 6e 65 77 65  est so that newe
364d4 72 20 64 6f 63 6c 69 73 74 0a 20 20 2a 2a 20 65  r doclist.  ** e
364d5 6c 65 6d 65 6e 74 73 20 66 6f 72 20 67 69 76 65  lements for give
364d6 6e 20 64 6f 63 69 64 73 20 6f 76 65 72 77 72 69  n docids overwri
364d7 74 65 20 6f 6c 64 65 72 20 65 6c 65 6d 65 6e 74  te older element
364d8 73 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28  s..  */.  while(
364d9 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73   (rc = sqlite3_s
364da 74 65 70 28 73 29 29 3d 3d 53 51 4c 49 54 45 5f  tep(s))==SQLITE_
364db 52 4f 57 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  ROW ){.    const
364dc 20 63 68 61 72 20 2a 70 44 61 74 61 20 3d 20 73   char *pData = s
364dd 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
364de 6f 62 28 73 2c 20 32 29 3b 0a 20 20 20 20 63 6f  ob(s, 2);.    co
364df 6e 73 74 20 69 6e 74 20 6e 44 61 74 61 20 3d 20  nst int nData = 
364e0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
364e1 79 74 65 73 28 73 2c 20 32 29 3b 0a 20 20 20 20  ytes(s, 2);.    
364e2 63 6f 6e 73 74 20 73 71 6c 69 74 65 5f 69 6e 74  const sqlite_int
364e3 36 34 20 69 4c 65 61 76 65 73 45 6e 64 20 3d 20  64 iLeavesEnd = 
364e4 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
364e5 6e 74 36 34 28 73 2c 20 31 29 3b 0a 20 20 20 20  nt64(s, 1);.    
364e6 72 63 20 3d 20 6c 6f 61 64 53 65 67 6d 65 6e 74  rc = loadSegment
364e7 28 76 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61  (v, pData, nData
364e8 2c 20 69 4c 65 61 76 65 73 45 6e 64 2c 20 70 54  , iLeavesEnd, pT
364e9 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72  erm, nTerm, isPr
364ea 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20  efix,.          
364eb 20 20 20 20 20 20 20 20 20 20 20 26 64 6f 63 6c             &docl
364ec 69 73 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ist);.    if( rc
364ed 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
364ee 74 6f 20 65 72 72 3b 0a 20 20 7d 0a 20 20 69 66  to err;.  }.  if
364ef 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
364f0 45 20 29 7b 0a 20 20 20 20 69 66 28 20 64 6f 63  E ){.    if( doc
364f1 6c 69 73 74 2e 6e 44 61 74 61 21 3d 30 20 29 7b  list.nData!=0 ){
364f2 0a 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73  .      /* TODO(s
364f3 68 65 73 73 29 20 54 68 65 20 6f 6c 64 20 74 65  hess) The old te
364f4 72 6d 5f 73 65 6c 65 63 74 5f 61 6c 6c 28 29 20  rm_select_all() 
364f5 63 6f 64 65 20 61 70 70 6c 69 65 64 20 74 68 65  code applied the
364f6 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 2a 2a   column.      **
364f7 20 72 65 73 74 72 69 63 74 20 61 73 20 77 65 20   restrict as we 
364f8 6d 65 72 67 65 64 20 73 65 67 6d 65 6e 74 73 2c  merged segments,
364f9 20 6c 65 61 64 69 6e 67 20 74 6f 20 73 6d 61 6c   leading to smal
364fa 6c 65 72 20 62 75 66 66 65 72 73 2e 0a 20 20 20  ler buffers..   
364fb 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 70 72     ** This is pr
364fc 6f 62 61 62 6c 79 20 77 6f 72 74 68 77 68 69 6c  obably worthwhil
364fd 65 20 74 6f 20 62 72 69 6e 67 20 62 61 63 6b 2c  e to bring back,
364fe 20 6f 6e 63 65 20 74 68 65 20 6e 65 77 20 73 74   once the new st
364ff 6f 72 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 73  orage.      ** s
36500 79 73 74 65 6d 20 69 73 20 63 68 65 63 6b 65 64  ystem is checked
36501 20 69 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20   in..      */.  
36502 20 20 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3d      if( iColumn=
36503 3d 76 2d 3e 6e 43 6f 6c 75 6d 6e 29 20 69 43 6f  =v->nColumn) iCo
36504 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20  lumn = -1;.     
36505 20 64 6f 63 4c 69 73 74 54 72 69 6d 28 44 4c 5f   docListTrim(DL_
36506 44 45 46 41 55 4c 54 2c 20 64 6f 63 6c 69 73 74  DEFAULT, doclist
36507 2e 70 44 61 74 61 2c 20 64 6f 63 6c 69 73 74 2e  .pData, doclist.
36508 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20  nData,.         
36509 20 20 20 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e           iColumn
3650a 2c 20 69 54 79 70 65 2c 20 6f 75 74 29 3b 0a 20  , iType, out);. 
3650b 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 53 51     }.    rc = SQ
3650c 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 65  LITE_OK;.  }.. e
3650d 72 72 3a 0a 20 20 64 61 74 61 42 75 66 66 65 72  rr:.  dataBuffer
3650e 44 65 73 74 72 6f 79 28 26 64 6f 63 6c 69 73 74  Destroy(&doclist
3650f 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
36510 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
36511 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36512 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36513 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36514 2a 2a 2a 2a 2f 0a 2f 2a 20 55 73 65 64 20 74 6f  ****/./* Used to
36515 20 68 6f 6c 64 20 68 61 73 68 74 61 62 6c 65 20   hold hashtable 
36516 64 61 74 61 20 66 6f 72 20 73 6f 72 74 69 6e 67  data for sorting
36517 2e 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  . */.typedef str
36518 75 63 74 20 54 65 72 6d 44 61 74 61 20 7b 0a 20  uct TermData {. 
36519 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65   const char *pTe
3651a 72 6d 3b 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b  rm;.  int nTerm;
3651b 0a 20 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a  .  DLCollector *
3651c 70 43 6f 6c 6c 65 63 74 6f 72 3b 0a 7d 20 54 65  pCollector;.} Te
3651d 72 6d 44 61 74 61 3b 0a 0a 2f 2a 20 4f 72 64 65  rmData;../* Orde
3651e 72 73 20 54 65 72 6d 44 61 74 61 20 65 6c 65 6d  rs TermData elem
3651f 65 6e 74 73 20 69 6e 20 73 74 72 63 6d 70 20 66  ents in strcmp f
36520 61 73 68 69 6f 6e 20 28 20 3c 30 20 66 6f 72 20  ashion ( <0 for 
36521 6c 65 73 73 2d 74 68 61 6e 2c 20 30 0a 2a 2a 20  less-than, 0.** 
36522 66 6f 72 20 65 71 75 61 6c 2c 20 3e 30 20 66 6f  for equal, >0 fo
36523 72 20 67 72 65 61 74 65 72 2d 74 68 61 6e 29 2e  r greater-than).
36524 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74  .*/.static int t
36525 65 72 6d 44 61 74 61 43 6d 70 28 63 6f 6e 73 74  ermDataCmp(const
36526 20 76 6f 69 64 20 2a 61 76 2c 20 63 6f 6e 73 74   void *av, const
36527 20 76 6f 69 64 20 2a 62 76 29 7b 0a 20 20 63 6f   void *bv){.  co
36528 6e 73 74 20 54 65 72 6d 44 61 74 61 20 2a 61 20  nst TermData *a 
36529 3d 20 28 63 6f 6e 73 74 20 54 65 72 6d 44 61 74  = (const TermDat
3652a 61 20 2a 29 61 76 3b 0a 20 20 63 6f 6e 73 74 20  a *)av;.  const 
3652b 54 65 72 6d 44 61 74 61 20 2a 62 20 3d 20 28 63  TermData *b = (c
3652c 6f 6e 73 74 20 54 65 72 6d 44 61 74 61 20 2a 29  onst TermData *)
3652d 62 76 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 61 2d  bv;.  int n = a-
3652e 3e 6e 54 65 72 6d 3c 62 2d 3e 6e 54 65 72 6d 20  >nTerm<b->nTerm 
3652f 3f 20 61 2d 3e 6e 54 65 72 6d 20 3a 20 62 2d 3e  ? a->nTerm : b->
36530 6e 54 65 72 6d 3b 0a 20 20 69 6e 74 20 63 20 3d  nTerm;.  int c =
36531 20 6d 65 6d 63 6d 70 28 61 2d 3e 70 54 65 72 6d   memcmp(a->pTerm
36532 2c 20 62 2d 3e 70 54 65 72 6d 2c 20 6e 29 3b 0a  , b->pTerm, n);.
36533 20 20 69 66 28 20 63 21 3d 30 20 29 20 72 65 74    if( c!=0 ) ret
36534 75 72 6e 20 63 3b 0a 20 20 72 65 74 75 72 6e 20  urn c;.  return 
36535 61 2d 3e 6e 54 65 72 6d 2d 62 2d 3e 6e 54 65 72  a->nTerm-b->nTer
36536 6d 3b 0a 7d 0a 0a 2f 2a 20 4f 72 64 65 72 20 70  m;.}../* Order p
36537 54 65 72 6d 73 20 64 61 74 61 20 62 79 20 74 65  Terms data by te
36538 72 6d 2c 20 74 68 65 6e 20 77 72 69 74 65 20 61  rm, then write a
36539 20 6e 65 77 20 6c 65 76 65 6c 20 30 20 73 65 67   new level 0 seg
3653a 6d 65 6e 74 20 75 73 69 6e 67 0a 2a 2a 20 4c 65  ment using.** Le
3653b 61 66 57 72 69 74 65 72 2e 0a 2a 2f 0a 73 74 61  afWriter..*/.sta
3653c 74 69 63 20 69 6e 74 20 77 72 69 74 65 5a 65 72  tic int writeZer
3653d 6f 53 65 67 6d 65 6e 74 28 66 75 6c 6c 74 65 78  oSegment(fulltex
3653e 74 5f 76 74 61 62 20 2a 76 2c 20 66 74 73 33 48  t_vtab *v, fts3H
3653f 61 73 68 20 2a 70 54 65 72 6d 73 29 7b 0a 20 20  ash *pTerms){.  
36540 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 3b  fts3HashElem *e;
36541 0a 20 20 69 6e 74 20 69 64 78 2c 20 72 63 2c 20  .  int idx, rc, 
36542 69 2c 20 6e 3b 0a 20 20 54 65 72 6d 44 61 74 61  i, n;.  TermData
36543 20 2a 70 44 61 74 61 3b 0a 20 20 4c 65 61 66 57   *pData;.  LeafW
36544 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a 20 20  riter writer;.  
36545 44 61 74 61 42 75 66 66 65 72 20 64 6c 3b 0a 0a  DataBuffer dl;..
36546 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74    /* Determine t
36547 68 65 20 6e 65 78 74 20 69 6e 64 65 78 20 61 74  he next index at
36548 20 6c 65 76 65 6c 20 30 2c 20 6d 65 72 67 69 6e   level 0, mergin
36549 67 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 20  g as necessary. 
3654a 2a 2f 0a 20 20 72 63 20 3d 20 73 65 67 64 69 72  */.  rc = segdir
3654b 4e 65 78 74 49 6e 64 65 78 28 76 2c 20 30 2c 20  NextIndex(v, 0, 
3654c 26 69 64 78 29 3b 0a 20 20 69 66 28 20 72 63 21  &idx);.  if( rc!
3654d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
3654e 75 72 6e 20 72 63 3b 0a 0a 20 20 6e 20 3d 20 66  urn rc;..  n = f
3654f 74 73 33 48 61 73 68 43 6f 75 6e 74 28 70 54 65  ts3HashCount(pTe
36550 72 6d 73 29 3b 0a 20 20 70 44 61 74 61 20 3d 20  rms);.  pData = 
36551 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
36552 2a 73 69 7a 65 6f 66 28 54 65 72 6d 44 61 74 61  *sizeof(TermData
36553 29 29 3b 0a 0a 20 20 66 6f 72 28 69 20 3d 20 30  ));..  for(i = 0
36554 2c 20 65 20 3d 20 66 74 73 33 48 61 73 68 46 69  , e = fts3HashFi
36555 72 73 74 28 70 54 65 72 6d 73 29 3b 20 65 3b 20  rst(pTerms); e; 
36556 69 2b 2b 2c 20 65 20 3d 20 66 74 73 33 48 61 73  i++, e = fts3Has
36557 68 4e 65 78 74 28 65 29 29 7b 0a 20 20 20 20 61  hNext(e)){.    a
36558 73 73 65 72 74 28 20 69 3c 6e 20 29 3b 0a 20 20  ssert( i<n );.  
36559 20 20 70 44 61 74 61 5b 69 5d 2e 70 54 65 72 6d    pData[i].pTerm
3655a 20 3d 20 66 74 73 33 48 61 73 68 4b 65 79 28 65   = fts3HashKey(e
3655b 29 3b 0a 20 20 20 20 70 44 61 74 61 5b 69 5d 2e  );.    pData[i].
3655c 6e 54 65 72 6d 20 3d 20 66 74 73 33 48 61 73 68  nTerm = fts3Hash
3655d 4b 65 79 73 69 7a 65 28 65 29 3b 0a 20 20 20 20  Keysize(e);.    
3655e 70 44 61 74 61 5b 69 5d 2e 70 43 6f 6c 6c 65 63  pData[i].pCollec
3655f 74 6f 72 20 3d 20 66 74 73 33 48 61 73 68 44 61  tor = fts3HashDa
36560 74 61 28 65 29 3b 0a 20 20 7d 0a 20 20 61 73 73  ta(e);.  }.  ass
36561 65 72 74 28 20 69 3d 3d 6e 20 29 3b 0a 0a 20 20  ert( i==n );..  
36562 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 53  /* TODO(shess) S
36563 68 6f 75 6c 64 20 77 65 20 61 6c 6c 6f 77 20 75  hould we allow u
36564 73 65 72 2d 64 65 66 69 6e 65 64 20 63 6f 6c 6c  ser-defined coll
36565 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 2c  ation sequences,
36566 0a 20 20 2a 2a 20 68 65 72 65 3f 20 20 49 20 74  .  ** here?  I t
36567 68 69 6e 6b 20 77 65 20 6f 6e 6c 79 20 6e 65 65  hink we only nee
36568 64 20 74 68 61 74 20 6f 6e 63 65 20 77 65 20 73  d that once we s
36569 75 70 70 6f 72 74 20 70 72 65 66 69 78 20 73 65  upport prefix se
3656a 61 72 63 68 65 73 2e 0a 20 20 2a 2f 0a 20 20 69  arches..  */.  i
3656b 66 28 20 6e 3e 31 20 29 20 71 73 6f 72 74 28 70  f( n>1 ) qsort(p
3656c 44 61 74 61 2c 20 6e 2c 20 73 69 7a 65 6f 66 28  Data, n, sizeof(
3656d 2a 70 44 61 74 61 29 2c 20 74 65 72 6d 44 61 74  *pData), termDat
3656e 61 43 6d 70 29 3b 0a 0a 20 20 2f 2a 20 54 4f 44  aCmp);..  /* TOD
3656f 4f 28 73 68 65 73 73 29 20 52 65 66 61 63 74 6f  O(shess) Refacto
36570 72 20 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e  r so that we can
36571 20 77 72 69 74 65 20 64 69 72 65 63 74 6c 79 20   write directly 
36572 74 6f 20 74 68 65 20 73 65 67 6d 65 6e 74 0a 20  to the segment. 
36573 20 2a 2a 20 44 61 74 61 42 75 66 66 65 72 2c 20   ** DataBuffer, 
36574 61 73 20 68 61 70 70 65 6e 73 20 66 6f 72 20 73  as happens for s
36575 65 67 6d 65 6e 74 20 6d 65 72 67 65 73 2e 0a 20  egment merges.. 
36576 20 2a 2f 0a 20 20 6c 65 61 66 57 72 69 74 65 72   */.  leafWriter
36577 49 6e 69 74 28 30 2c 20 69 64 78 2c 20 26 77 72  Init(0, idx, &wr
36578 69 74 65 72 29 3b 0a 20 20 64 61 74 61 42 75 66  iter);.  dataBuf
36579 66 65 72 49 6e 69 74 28 26 64 6c 2c 20 30 29 3b  ferInit(&dl, 0);
3657a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b  .  for(i=0; i<n;
3657b 20 69 2b 2b 29 7b 0a 20 20 20 20 64 61 74 61 42   i++){.    dataB
3657c 75 66 66 65 72 52 65 73 65 74 28 26 64 6c 29 3b  ufferReset(&dl);
3657d 0a 20 20 20 20 64 6c 63 41 64 64 44 6f 63 6c 69  .    dlcAddDocli
3657e 73 74 28 70 44 61 74 61 5b 69 5d 2e 70 43 6f 6c  st(pData[i].pCol
3657f 6c 65 63 74 6f 72 2c 20 26 64 6c 29 3b 0a 20 20  lector, &dl);.  
36580 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65    rc = leafWrite
36581 72 53 74 65 70 28 76 2c 20 26 77 72 69 74 65 72  rStep(v, &writer
36582 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
36583 20 20 20 20 20 20 20 20 20 20 70 44 61 74 61 5b            pData[
36584 69 5d 2e 70 54 65 72 6d 2c 20 70 44 61 74 61 5b  i].pTerm, pData[
36585 69 5d 2e 6e 54 65 72 6d 2c 20 64 6c 2e 70 44 61  i].nTerm, dl.pDa
36586 74 61 2c 20 64 6c 2e 6e 44 61 74 61 29 3b 0a 20  ta, dl.nData);. 
36587 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
36588 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b  E_OK ) goto err;
36589 0a 20 20 7d 0a 20 20 72 63 20 3d 20 6c 65 61 66  .  }.  rc = leaf
3658a 57 72 69 74 65 72 46 69 6e 61 6c 69 7a 65 28 76  WriterFinalize(v
3658b 2c 20 26 77 72 69 74 65 72 29 3b 0a 0a 20 65 72  , &writer);.. er
3658c 72 3a 0a 20 20 64 61 74 61 42 75 66 66 65 72 44  r:.  dataBufferD
3658d 65 73 74 72 6f 79 28 26 64 6c 29 3b 0a 20 20 73  estroy(&dl);.  s
3658e 71 6c 69 74 65 33 5f 66 72 65 65 28 70 44 61 74  qlite3_free(pDat
3658f 61 29 3b 0a 20 20 6c 65 61 66 57 72 69 74 65 72  a);.  leafWriter
36590 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29  Destroy(&writer)
36591 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
36592 0a 0a 2f 2a 20 49 66 20 70 65 6e 64 69 6e 67 54  ../* If pendingT
36593 65 72 6d 73 20 68 61 73 20 64 61 74 61 2c 20 66  erms has data, f
36594 72 65 65 20 69 74 2e 20 2a 2f 0a 73 74 61 74 69  ree it. */.stati
36595 63 20 69 6e 74 20 63 6c 65 61 72 50 65 6e 64 69  c int clearPendi
36596 6e 67 54 65 72 6d 73 28 66 75 6c 6c 74 65 78 74  ngTerms(fulltext
36597 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20 69 66 28  _vtab *v){.  if(
36598 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61   v->nPendingData
36599 3e 3d 30 20 29 7b 0a 20 20 20 20 66 74 73 33 48  >=0 ){.    fts3H
3659a 61 73 68 45 6c 65 6d 20 2a 65 3b 0a 20 20 20 20  ashElem *e;.    
3659b 66 6f 72 28 65 3d 66 74 73 33 48 61 73 68 46 69  for(e=fts3HashFi
3659c 72 73 74 28 26 76 2d 3e 70 65 6e 64 69 6e 67 54  rst(&v->pendingT
3659d 65 72 6d 73 29 3b 20 65 3b 20 65 3d 66 74 73 33  erms); e; e=fts3
3659e 48 61 73 68 4e 65 78 74 28 65 29 29 7b 0a 20 20  HashNext(e)){.  
3659f 20 20 20 20 64 6c 63 44 65 6c 65 74 65 28 66 74      dlcDelete(ft
365a0 73 33 48 61 73 68 44 61 74 61 28 65 29 29 3b 0a  s3HashData(e));.
365a1 20 20 20 20 7d 0a 20 20 20 20 66 74 73 33 48 61      }.    fts3Ha
365a2 73 68 43 6c 65 61 72 28 26 76 2d 3e 70 65 6e 64  shClear(&v->pend
365a3 69 6e 67 54 65 72 6d 73 29 3b 0a 20 20 20 20 76  ingTerms);.    v
365a4 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 3d  ->nPendingData =
365a5 20 2d 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   -1;.  }.  retur
365a6 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
365a7 2f 2a 20 49 66 20 70 65 6e 64 69 6e 67 54 65 72  /* If pendingTer
365a8 6d 73 20 68 61 73 20 64 61 74 61 2c 20 66 6c 75  ms has data, flu
365a9 73 68 20 69 74 20 74 6f 20 61 20 6c 65 76 65 6c  sh it to a level
365aa 2d 7a 65 72 6f 20 73 65 67 6d 65 6e 74 2c 20 61  -zero segment, a
365ab 6e 64 0a 2a 2a 20 66 72 65 65 20 69 74 2e 0a 2a  nd.** free it..*
365ac 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 75  /.static int flu
365ad 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 66  shPendingTerms(f
365ae 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29  ulltext_vtab *v)
365af 7b 0a 20 20 69 66 28 20 76 2d 3e 6e 50 65 6e 64  {.  if( v->nPend
365b0 69 6e 67 44 61 74 61 3e 3d 30 20 29 7b 0a 20 20  ingData>=0 ){.  
365b1 20 20 69 6e 74 20 72 63 20 3d 20 77 72 69 74 65    int rc = write
365b2 5a 65 72 6f 53 65 67 6d 65 6e 74 28 76 2c 20 26  ZeroSegment(v, &
365b3 76 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 29  v->pendingTerms)
365b4 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
365b5 4c 49 54 45 5f 4f 4b 20 29 20 63 6c 65 61 72 50  LITE_OK ) clearP
365b6 65 6e 64 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a  endingTerms(v);.
365b7 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
365b8 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
365b9 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 49 66 20  TE_OK;.}../* If 
365ba 70 65 6e 64 69 6e 67 54 65 72 6d 73 20 69 73 20  pendingTerms is 
365bb 22 74 6f 6f 20 62 69 67 22 2c 20 6f 72 20 64 6f  "too big", or do
365bc 63 69 64 20 69 73 20 6f 75 74 20 6f 66 20 6f 72  cid is out of or
365bd 64 65 72 2c 20 66 6c 75 73 68 20 69 74 2e 0a 2a  der, flush it..*
365be 2a 20 52 65 67 61 72 64 6c 65 73 73 2c 20 62 65  * Regardless, be
365bf 20 63 65 72 74 61 69 6e 20 74 68 61 74 20 70 65   certain that pe
365c0 6e 64 69 6e 67 54 65 72 6d 73 20 69 73 20 69 6e  ndingTerms is in
365c1 69 74 69 61 6c 69 7a 65 64 20 66 6f 72 20 75 73  itialized for us
365c2 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
365c3 20 69 6e 69 74 50 65 6e 64 69 6e 67 54 65 72 6d   initPendingTerm
365c4 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  s(fulltext_vtab 
365c5 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  *v, sqlite_int64
365c6 20 69 44 6f 63 69 64 29 7b 0a 20 20 2f 2a 20 54   iDocid){.  /* T
365c7 4f 44 4f 28 73 68 65 73 73 29 20 45 78 70 6c 6f  ODO(shess) Explo
365c8 72 65 20 77 68 65 74 68 65 72 20 70 61 72 74 69  re whether parti
365c9 61 6c 6c 79 20 66 6c 75 73 68 69 6e 67 20 74 68  ally flushing th
365ca 65 20 62 75 66 66 65 72 20 6f 6e 0a 20 20 2a 2a  e buffer on.  **
365cb 20 66 6f 72 63 65 64 2d 66 6c 75 73 68 20 77 6f   forced-flush wo
365cc 75 6c 64 20 70 72 6f 76 69 64 65 20 62 65 74 74  uld provide bett
365cd 65 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20  er performance. 
365ce 20 49 20 73 75 73 70 65 63 74 20 74 68 61 74 20   I suspect that 
365cf 69 66 0a 20 20 2a 2a 20 77 65 20 6f 72 64 65 72  if.  ** we order
365d0 65 64 20 74 68 65 20 64 6f 63 6c 69 73 74 73 20  ed the doclists 
365d1 62 79 20 73 69 7a 65 20 61 6e 64 20 66 6c 75 73  by size and flus
365d2 68 65 64 20 74 68 65 20 6c 61 72 67 65 73 74 20  hed the largest 
365d3 75 6e 74 69 6c 20 74 68 65 0a 20 20 2a 2a 20 62  until the.  ** b
365d4 75 66 66 65 72 20 77 61 73 20 68 61 6c 66 20 65  uffer was half e
365d5 6d 70 74 79 2c 20 74 68 61 74 20 77 6f 75 6c 64  mpty, that would
365d6 20 6c 65 74 20 74 68 65 20 6c 65 73 73 20 66 72   let the less fr
365d7 65 71 75 65 6e 74 20 74 65 72 6d 73 0a 20 20 2a  equent terms.  *
365d8 2a 20 67 65 6e 65 72 61 74 65 20 6c 6f 6e 67 65  * generate longe
365d9 72 20 64 6f 63 6c 69 73 74 73 2e 0a 20 20 2a 2f  r doclists..  */
365da 0a 20 20 69 66 28 20 69 44 6f 63 69 64 3c 3d 76  .  if( iDocid<=v
365db 2d 3e 69 50 72 65 76 44 6f 63 69 64 20 7c 7c 20  ->iPrevDocid || 
365dc 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3e  v->nPendingData>
365dd 6b 50 65 6e 64 69 6e 67 54 68 72 65 73 68 6f 6c  kPendingThreshol
365de 64 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20  d ){.    int rc 
365df 3d 20 66 6c 75 73 68 50 65 6e 64 69 6e 67 54 65  = flushPendingTe
365e0 72 6d 73 28 76 29 3b 0a 20 20 20 20 69 66 28 20  rms(v);.    if( 
365e1 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
365e2 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
365e3 20 69 66 28 20 76 2d 3e 6e 50 65 6e 64 69 6e 67   if( v->nPending
365e4 44 61 74 61 3c 30 20 29 7b 0a 20 20 20 20 66 74  Data<0 ){.    ft
365e5 73 33 48 61 73 68 49 6e 69 74 28 26 76 2d 3e 70  s3HashInit(&v->p
365e6 65 6e 64 69 6e 67 54 65 72 6d 73 2c 20 46 54 53  endingTerms, FTS
365e7 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 2c 20 31  3_HASH_STRING, 1
365e8 29 3b 0a 20 20 20 20 76 2d 3e 6e 50 65 6e 64 69  );.    v->nPendi
365e9 6e 67 44 61 74 61 20 3d 20 30 3b 0a 20 20 7d 0a  ngData = 0;.  }.
365ea 20 20 76 2d 3e 69 50 72 65 76 44 6f 63 69 64 20    v->iPrevDocid 
365eb 3d 20 69 44 6f 63 69 64 3b 0a 20 20 72 65 74 75  = iDocid;.  retu
365ec 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
365ed 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ./* This functio
365ee 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  n implements the
365ef 20 78 55 70 64 61 74 65 20 63 61 6c 6c 62 61 63   xUpdate callbac
365f0 6b 3b 20 69 74 20 69 73 20 74 68 65 20 74 6f 70  k; it is the top
365f1 2d 6c 65 76 65 6c 20 65 6e 74 72 79 0a 20 2a 20  -level entry. * 
365f2 70 6f 69 6e 74 20 66 6f 72 20 69 6e 73 65 72 74  point for insert
365f3 69 6e 67 2c 20 64 65 6c 65 74 69 6e 67 20 6f 72  ing, deleting or
365f4 20 75 70 64 61 74 69 6e 67 20 61 20 72 6f 77 20   updating a row 
365f5 69 6e 20 61 20 66 75 6c 6c 2d 74 65 78 74 20 74  in a full-text t
365f6 61 62 6c 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20  able. */.static 
365f7 69 6e 74 20 66 75 6c 6c 74 65 78 74 55 70 64 61  int fulltextUpda
365f8 74 65 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  te(sqlite3_vtab 
365f9 2a 70 56 74 61 62 2c 20 69 6e 74 20 6e 41 72 67  *pVtab, int nArg
365fa 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
365fb 2a 2a 70 70 41 72 67 2c 0a 20 20 20 20 20 20 20  **ppArg,.       
365fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
365fd 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
365fe 2a 70 52 6f 77 69 64 29 7b 0a 20 20 66 75 6c 6c  *pRowid){.  full
365ff 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20 28  text_vtab *v = (
36600 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29  fulltext_vtab *)
36601 20 70 56 74 61 62 3b 0a 20 20 69 6e 74 20 72 63   pVtab;.  int rc
36602 3b 0a 0a 20 20 46 54 53 54 52 41 43 45 28 28 22  ;..  FTSTRACE(("
36603 46 54 53 33 20 55 70 64 61 74 65 20 25 70 5c 6e  FTS3 Update %p\n
36604 22 2c 20 70 56 74 61 62 29 29 3b 0a 0a 20 20 69  ", pVtab));..  i
36605 66 28 20 6e 41 72 67 3c 32 20 29 7b 0a 20 20 20  f( nArg<2 ){.   
36606 20 72 63 20 3d 20 69 6e 64 65 78 5f 64 65 6c 65   rc = index_dele
36607 74 65 28 76 2c 20 73 71 6c 69 74 65 33 5f 76 61  te(v, sqlite3_va
36608 6c 75 65 5f 69 6e 74 36 34 28 70 70 41 72 67 5b  lue_int64(ppArg[
36609 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63  0]));.    if( rc
3660a 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
3660b 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 6a 75       /* If we ju
3660c 73 74 20 64 65 6c 65 74 65 64 20 74 68 65 20 6c  st deleted the l
3660d 61 73 74 20 72 6f 77 20 69 6e 20 74 68 65 20 74  ast row in the t
3660e 61 62 6c 65 2c 20 63 6c 65 61 72 20 6f 75 74 20  able, clear out 
3660f 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 69 6e 64  the.      ** ind
36610 65 78 20 64 61 74 61 2e 0a 20 20 20 20 20 20 2a  ex data..      *
36611 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 63 6f 6e  /.      rc = con
36612 74 65 6e 74 5f 65 78 69 73 74 73 28 76 29 3b 0a  tent_exists(v);.
36613 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
36614 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
36615 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
36616 4f 4b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  OK;.      }else 
36617 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  if( rc==SQLITE_D
36618 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ONE ){.        /
36619 2a 20 43 6c 65 61 72 20 74 68 65 20 70 65 6e 64  * Clear the pend
3661a 69 6e 67 20 74 65 72 6d 73 20 73 6f 20 77 65 20  ing terms so we 
3661b 64 6f 6e 27 74 20 66 6c 75 73 68 20 61 20 75 73  don't flush a us
3661c 65 6c 65 73 73 20 6c 65 76 65 6c 2d 30 0a 20 20  eless level-0.  
3661d 20 20 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74        ** segment
3661e 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61   when the transa
3661f 63 74 69 6f 6e 20 63 6c 6f 73 65 73 2e 0a 20 20  ction closes..  
36620 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
36621 20 72 63 20 3d 20 63 6c 65 61 72 50 65 6e 64 69   rc = clearPendi
36622 6e 67 54 65 72 6d 73 28 76 29 3b 0a 20 20 20 20  ngTerms(v);.    
36623 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
36624 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
36625 20 20 20 72 63 20 3d 20 73 65 67 64 69 72 5f 64     rc = segdir_d
36626 65 6c 65 74 65 5f 61 6c 6c 28 76 29 3b 0a 20 20  elete_all(v);.  
36627 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
36628 20 20 20 20 7d 0a 20 20 7d 20 65 6c 73 65 20 69      }.  } else i
36629 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f( sqlite3_value
3662a 5f 74 79 70 65 28 70 70 41 72 67 5b 30 5d 29 20  _type(ppArg[0]) 
3662b 21 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29  != SQLITE_NULL )
3662c 7b 0a 20 20 20 20 2f 2a 20 41 6e 20 75 70 64 61  {.    /* An upda
3662d 74 65 3a 0a 20 20 20 20 20 2a 20 70 70 41 72 67  te:.     * ppArg
3662e 5b 30 5d 20 3d 20 6f 6c 64 20 72 6f 77 69 64 0a  [0] = old rowid.
3662f 20 20 20 20 20 2a 20 70 70 41 72 67 5b 31 5d 20       * ppArg[1] 
36630 3d 20 6e 65 77 20 72 6f 77 69 64 0a 20 20 20 20  = new rowid.    
36631 20 2a 20 70 70 41 72 67 5b 32 2e 2e 32 2b 76 2d   * ppArg[2..2+v-
36632 3e 6e 43 6f 6c 75 6d 6e 2d 31 5d 20 3d 20 76 61  >nColumn-1] = va
36633 6c 75 65 73 0a 20 20 20 20 20 2a 20 70 70 41 72  lues.     * ppAr
36634 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20  g[2+v->nColumn] 
36635 3d 20 76 61 6c 75 65 20 66 6f 72 20 6d 61 67 69  = value for magi
36636 63 20 63 6f 6c 75 6d 6e 20 28 77 65 20 69 67 6e  c column (we ign
36637 6f 72 65 20 74 68 69 73 29 0a 20 20 20 20 20 2a  ore this).     *
36638 20 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c   ppArg[2+v->nCol
36639 75 6d 6e 2b 31 5d 20 3d 20 76 61 6c 75 65 20 66  umn+1] = value f
3663a 6f 72 20 64 6f 63 69 64 0a 20 20 20 20 20 2a 2f  or docid.     */
3663b 0a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  .    sqlite_int6
3663c 34 20 72 6f 77 69 64 20 3d 20 73 71 6c 69 74 65  4 rowid = sqlite
3663d 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 70 70  3_value_int64(pp
3663e 41 72 67 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28  Arg[0]);.    if(
3663f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
36640 79 70 65 28 70 70 41 72 67 5b 31 5d 29 20 21 3d  ype(ppArg[1]) !=
36641 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20   SQLITE_INTEGER 
36642 7c 7c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ||.        sqlit
36643 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 70  e3_value_int64(p
36644 70 41 72 67 5b 31 5d 29 20 21 3d 20 72 6f 77 69  pArg[1]) != rowi
36645 64 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  d ){.      rc = 
36646 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 20 20 2f  SQLITE_ERROR;  /
36647 2a 20 77 65 20 64 6f 6e 27 74 20 61 6c 6c 6f 77  * we don't allow
36648 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 72 6f   changing the ro
36649 77 69 64 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65  wid */.    }else
3664a 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
3664b 75 65 5f 74 79 70 65 28 70 70 41 72 67 5b 32 2b  ue_type(ppArg[2+
3664c 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 29 20 21  v->nColumn+1]) !
3664d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  = SQLITE_INTEGER
3664e 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20   ||.            
3664f 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f    sqlite3_value_
36650 69 6e 74 36 34 28 70 70 41 72 67 5b 32 2b 76 2d  int64(ppArg[2+v-
36651 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 29 20 21 3d 20  >nColumn+1]) != 
36652 72 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 72  rowid ){.      r
36653 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
36654 3b 20 20 2f 2a 20 77 65 20 64 6f 6e 27 74 20 61  ;  /* we don't a
36655 6c 6c 6f 77 20 63 68 61 6e 67 69 6e 67 20 74 68  llow changing th
36656 65 20 64 6f 63 69 64 20 2a 2f 0a 20 20 20 20 7d  e docid */.    }
36657 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65  else{.      asse
36658 72 74 28 20 6e 41 72 67 3d 3d 32 2b 76 2d 3e 6e  rt( nArg==2+v->n
36659 43 6f 6c 75 6d 6e 2b 32 29 3b 0a 20 20 20 20 20  Column+2);.     
3665a 20 72 63 20 3d 20 69 6e 64 65 78 5f 75 70 64 61   rc = index_upda
3665b 74 65 28 76 2c 20 72 6f 77 69 64 2c 20 26 70 70  te(v, rowid, &pp
3665c 41 72 67 5b 32 5d 29 3b 0a 20 20 20 20 7d 0a 20  Arg[2]);.    }. 
3665d 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a   } else {.    /*
3665e 20 41 6e 20 69 6e 73 65 72 74 3a 0a 20 20 20 20   An insert:.    
3665f 20 2a 20 70 70 41 72 67 5b 31 5d 20 3d 20 72 65   * ppArg[1] = re
36660 71 75 65 73 74 65 64 20 72 6f 77 69 64 0a 20 20  quested rowid.  
36661 20 20 20 2a 20 70 70 41 72 67 5b 32 2e 2e 32 2b     * ppArg[2..2+
36662 76 2d 3e 6e 43 6f 6c 75 6d 6e 2d 31 5d 20 3d 20  v->nColumn-1] = 
36663 76 61 6c 75 65 73 0a 20 20 20 20 20 2a 20 70 70  values.     * pp
36664 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e  Arg[2+v->nColumn
36665 5d 20 3d 20 76 61 6c 75 65 20 66 6f 72 20 6d 61  ] = value for ma
36666 67 69 63 20 63 6f 6c 75 6d 6e 20 28 77 65 20 69  gic column (we i
36667 67 6e 6f 72 65 20 74 68 69 73 29 0a 20 20 20 20  gnore this).    
36668 20 2a 20 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43   * ppArg[2+v->nC
36669 6f 6c 75 6d 6e 2b 31 5d 20 3d 20 76 61 6c 75 65  olumn+1] = value
3666a 20 66 6f 72 20 64 6f 63 69 64 0a 20 20 20 20 20   for docid.     
3666b 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  */.    sqlite3_v
3666c 61 6c 75 65 20 2a 70 52 65 71 75 65 73 74 44 6f  alue *pRequestDo
3666d 63 69 64 20 3d 20 70 70 41 72 67 5b 32 2b 76 2d  cid = ppArg[2+v-
3666e 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 3b 0a 20 20 20  >nColumn+1];.   
3666f 20 61 73 73 65 72 74 28 20 6e 41 72 67 3d 3d 32   assert( nArg==2
36670 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32 29 3b 0a  +v->nColumn+2);.
36671 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e      if( SQLITE_N
36672 55 4c 4c 20 21 3d 20 73 71 6c 69 74 65 33 5f 76  ULL != sqlite3_v
36673 61 6c 75 65 5f 74 79 70 65 28 70 52 65 71 75 65  alue_type(pReque
36674 73 74 44 6f 63 69 64 29 20 26 26 0a 20 20 20 20  stDocid) &&.    
36675 20 20 20 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20      SQLITE_NULL 
36676 21 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  != sqlite3_value
36677 5f 74 79 70 65 28 70 70 41 72 67 5b 31 5d 29 20  _type(ppArg[1]) 
36678 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f  ){.      /* TODO
36679 28 73 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72  (shess) Consider
3667a 20 61 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 74   allowing this t
3667b 6f 20 77 6f 72 6b 20 69 66 20 74 68 65 20 76 61  o work if the va
3667c 6c 75 65 73 20 61 72 65 0a 20 20 20 20 20 20 2a  lues are.      *
3667d 2a 20 69 64 65 6e 74 69 63 61 6c 2e 20 20 49 27  * identical.  I'
3667e 6d 20 69 6e 63 6c 69 6e 65 64 20 74 6f 20 64 69  m inclined to di
3667f 73 63 6f 75 72 61 67 65 20 74 68 61 74 20 75 73  scourage that us
36680 61 67 65 2c 20 74 68 6f 75 67 68 2c 0a 20 20 20  age, though,.   
36681 20 20 20 2a 2a 20 67 69 76 65 6e 20 74 68 61 74     ** given that
36682 20 62 6f 74 68 20 72 6f 77 69 64 20 61 6e 64 20   both rowid and 
36683 64 6f 63 69 64 20 61 72 65 20 73 70 65 63 69 61  docid are specia
36684 6c 20 63 6f 6c 75 6d 6e 73 2e 20 20 42 65 74 74  l columns.  Bett
36685 65 72 0a 20 20 20 20 20 20 2a 2a 20 77 6f 75 6c  er.      ** woul
36686 64 20 62 65 20 74 6f 20 64 65 66 69 6e 65 20 6f  d be to define o
36687 6e 65 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20  ne or the other 
36688 61 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 77  as the default w
36689 69 6e 6e 65 72 2c 0a 20 20 20 20 20 20 2a 2a 20  inner,.      ** 
3668a 62 75 74 20 73 68 6f 75 6c 64 20 69 74 20 62 65  but should it be
3668b 20 66 74 73 33 2d 63 65 6e 74 72 69 63 20 28 64   fts3-centric (d
3668c 6f 63 69 64 29 20 6f 72 20 53 51 4c 69 74 65 2d  ocid) or SQLite-
3668d 63 65 6e 74 72 69 63 0a 20 20 20 20 20 20 2a 2a  centric.      **
3668e 20 28 72 6f 77 69 64 29 3f 0a 20 20 20 20 20 20   (rowid)?.      
3668f 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  */.      rc = SQ
36690 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
36691 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28  }else{.      if(
36692 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 3d 3d 20   SQLITE_NULL == 
36693 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
36694 70 65 28 70 52 65 71 75 65 73 74 44 6f 63 69 64  pe(pRequestDocid
36695 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 52 65  ) ){.        pRe
36696 71 75 65 73 74 44 6f 63 69 64 20 3d 20 70 70 41  questDocid = ppA
36697 72 67 5b 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20  rg[1];.      }. 
36698 20 20 20 20 20 72 63 20 3d 20 69 6e 64 65 78 5f       rc = index_
36699 69 6e 73 65 72 74 28 76 2c 20 70 52 65 71 75 65  insert(v, pReque
3669a 73 74 44 6f 63 69 64 2c 20 26 70 70 41 72 67 5b  stDocid, &ppArg[
3669b 32 5d 2c 20 70 52 6f 77 69 64 29 3b 0a 20 20 20  2], pRowid);.   
3669c 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
3669d 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69   rc;.}..static i
3669e 6e 74 20 66 75 6c 6c 74 65 78 74 53 79 6e 63 28  nt fulltextSync(
3669f 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
366a0 74 61 62 29 7b 0a 20 20 46 54 53 54 52 41 43 45  tab){.  FTSTRACE
366a1 28 28 22 46 54 53 33 20 78 53 79 6e 63 28 29 5c  (("FTS3 xSync()\
366a2 6e 22 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 66  n"));.  return f
366a3 6c 75 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73  lushPendingTerms
366a4 28 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  ((fulltext_vtab 
366a5 2a 29 70 56 74 61 62 29 3b 0a 7d 0a 0a 73 74 61  *)pVtab);.}..sta
366a6 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74  tic int fulltext
366a7 42 65 67 69 6e 28 73 71 6c 69 74 65 33 5f 76 74  Begin(sqlite3_vt
366a8 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 66 75  ab *pVtab){.  fu
366a9 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d  lltext_vtab *v =
366aa 20 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20   (fulltext_vtab 
366ab 2a 29 20 70 56 74 61 62 3b 0a 20 20 46 54 53 54  *) pVtab;.  FTST
366ac 52 41 43 45 28 28 22 46 54 53 33 20 78 42 65 67  RACE(("FTS3 xBeg
366ad 69 6e 28 29 5c 6e 22 29 29 3b 0a 0a 20 20 2f 2a  in()\n"));..  /*
366ae 20 41 6e 79 20 62 75 66 66 65 72 65 64 20 75 70   Any buffered up
366af 64 61 74 65 73 20 73 68 6f 75 6c 64 20 68 61 76  dates should hav
366b0 65 20 62 65 65 6e 20 63 6c 65 61 72 65 64 20 62  e been cleared b
366b1 79 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a 20  y the previous. 
366b2 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e   ** transaction.
366b3 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
366b4 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c  v->nPendingData<
366b5 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 6c  0 );.  return cl
366b6 65 61 72 50 65 6e 64 69 6e 67 54 65 72 6d 73 28  earPendingTerms(
366b7 76 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  v);.}..static in
366b8 74 20 66 75 6c 6c 74 65 78 74 43 6f 6d 6d 69 74  t fulltextCommit
366b9 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
366ba 56 74 61 62 29 7b 0a 20 20 66 75 6c 6c 74 65 78  Vtab){.  fulltex
366bb 74 5f 76 74 61 62 20 2a 76 20 3d 20 28 66 75 6c  t_vtab *v = (ful
366bc 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 20 70 56  ltext_vtab *) pV
366bd 74 61 62 3b 0a 20 20 46 54 53 54 52 41 43 45 28  tab;.  FTSTRACE(
366be 28 22 46 54 53 33 20 78 43 6f 6d 6d 69 74 28 29  ("FTS3 xCommit()
366bf 5c 6e 22 29 29 3b 0a 0a 20 20 2f 2a 20 42 75 66  \n"));..  /* Buf
366c0 66 65 72 65 64 20 75 70 64 61 74 65 73 20 73 68  fered updates sh
366c1 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 63  ould have been c
366c2 6c 65 61 72 65 64 20 62 79 20 66 75 6c 6c 74 65  leared by fullte
366c3 78 74 53 79 6e 63 28 29 2e 20 2a 2f 0a 20 20 61  xtSync(). */.  a
366c4 73 73 65 72 74 28 20 76 2d 3e 6e 50 65 6e 64 69  ssert( v->nPendi
366c5 6e 67 44 61 74 61 3c 30 20 29 3b 0a 20 20 72 65  ngData<0 );.  re
366c6 74 75 72 6e 20 63 6c 65 61 72 50 65 6e 64 69 6e  turn clearPendin
366c7 67 54 65 72 6d 73 28 76 29 3b 0a 7d 0a 0a 73 74  gTerms(v);.}..st
366c8 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78  atic int fulltex
366c9 74 52 6f 6c 6c 62 61 63 6b 28 73 71 6c 69 74 65  tRollback(sqlite
366ca 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a  3_vtab *pVtab){.
366cb 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53    FTSTRACE(("FTS
366cc 33 20 78 52 6f 6c 6c 62 61 63 6b 28 29 5c 6e 22  3 xRollback()\n"
366cd 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 6c 65  ));.  return cle
366ce 61 72 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 28  arPendingTerms((
366cf 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29  fulltext_vtab *)
366d0 70 56 74 61 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pVtab);.}../*.**
366d1 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
366d2 6f 66 20 74 68 65 20 73 6e 69 70 70 65 74 28 29  of the snippet()
366d3 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 46 54   function for FT
366d4 53 33 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  S3.*/.static voi
366d5 64 20 73 6e 69 70 70 65 74 46 75 6e 63 28 0a 20  d snippetFunc(. 
366d6 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
366d7 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20 20 69 6e   *pContext,.  in
366d8 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
366d9 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
366da 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72  {.  fulltext_cur
366db 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20  sor *pCursor;.  
366dc 69 66 28 20 61 72 67 63 3c 31 20 29 20 72 65 74  if( argc<1 ) ret
366dd 75 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  urn;.  if( sqlit
366de 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
366df 67 76 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42  gv[0])!=SQLITE_B
366e0 4c 4f 42 20 7c 7c 0a 20 20 20 20 20 20 73 71 6c  LOB ||.      sql
366e1 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
366e2 28 61 72 67 76 5b 30 5d 29 21 3d 73 69 7a 65 6f  (argv[0])!=sizeo
366e3 66 28 70 43 75 72 73 6f 72 29 20 29 7b 0a 20 20  f(pCursor) ){.  
366e4 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
366e5 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c  _error(pContext,
366e6 20 22 69 6c 6c 65 67 61 6c 20 66 69 72 73 74 20   "illegal first 
366e7 61 72 67 75 6d 65 6e 74 20 74 6f 20 68 74 6d 6c  argument to html
366e8 5f 73 6e 69 70 70 65 74 22 2c 2d 31 29 3b 0a 20  _snippet",-1);. 
366e9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73   }else{.    cons
366ea 74 20 63 68 61 72 20 2a 7a 53 74 61 72 74 20 3d  t char *zStart =
366eb 20 22 3c 62 3e 22 3b 0a 20 20 20 20 63 6f 6e 73   "<b>";.    cons
366ec 74 20 63 68 61 72 20 2a 7a 45 6e 64 20 3d 20 22  t char *zEnd = "
366ed 3c 2f 62 3e 22 3b 0a 20 20 20 20 63 6f 6e 73 74  </b>";.    const
366ee 20 63 68 61 72 20 2a 7a 45 6c 6c 69 70 73 69 73   char *zEllipsis
366ef 20 3d 20 22 3c 62 3e 2e 2e 2e 3c 2f 62 3e 22 3b   = "<b>...</b>";
366f0 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 43 75  .    memcpy(&pCu
366f1 72 73 6f 72 2c 20 73 71 6c 69 74 65 33 5f 76 61  rsor, sqlite3_va
366f2 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d  lue_blob(argv[0]
366f3 29 2c 20 73 69 7a 65 6f 66 28 70 43 75 72 73 6f  ), sizeof(pCurso
366f4 72 29 29 3b 0a 20 20 20 20 69 66 28 20 61 72 67  r));.    if( arg
366f5 63 3e 3d 32 20 29 7b 0a 20 20 20 20 20 20 7a 53  c>=2 ){.      zS
366f6 74 61 72 74 20 3d 20 28 63 6f 6e 73 74 20 63 68  tart = (const ch
366f7 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar*)sqlite3_valu
366f8 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b  e_text(argv[1]);
366f9 0a 20 20 20 20 20 20 69 66 28 20 61 72 67 63 3e  .      if( argc>
366fa 3d 33 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 45  =3 ){.        zE
366fb 6e 64 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  nd = (const char
366fc 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
366fd 74 65 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20  text(argv[2]);. 
366fe 20 20 20 20 20 20 20 69 66 28 20 61 72 67 63 3e         if( argc>
366ff 3d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =4 ){.          
36700 7a 45 6c 6c 69 70 73 69 73 20 3d 20 28 63 6f 6e  zEllipsis = (con
36701 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33  st char*)sqlite3
36702 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
36703 5b 33 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  [3]);.        }.
36704 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
36705 20 20 73 6e 69 70 70 65 74 41 6c 6c 4f 66 66 73    snippetAllOffs
36706 65 74 73 28 70 43 75 72 73 6f 72 29 3b 0a 20 20  ets(pCursor);.  
36707 20 20 73 6e 69 70 70 65 74 54 65 78 74 28 70 43    snippetText(pC
36708 75 72 73 6f 72 2c 20 7a 53 74 61 72 74 2c 20 7a  ursor, zStart, z
36709 45 6e 64 2c 20 7a 45 6c 6c 69 70 73 69 73 29 3b  End, zEllipsis);
3670a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
3670b 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78  ult_text(pContex
3670c 74 2c 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70  t, pCursor->snip
3670d 70 65 74 2e 7a 53 6e 69 70 70 65 74 2c 0a 20 20  pet.zSnippet,.  
3670e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3670f 20 20 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 73        pCursor->s
36710 6e 69 70 70 65 74 2e 6e 53 6e 69 70 70 65 74 2c  nippet.nSnippet,
36711 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
36712 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d  .  }.}../*.** Im
36713 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
36714 74 68 65 20 6f 66 66 73 65 74 73 28 29 20 66 75  the offsets() fu
36715 6e 63 74 69 6f 6e 20 66 6f 72 20 46 54 53 33 0a  nction for FTS3.
36716 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
36717 6e 69 70 70 65 74 4f 66 66 73 65 74 73 46 75 6e  nippetOffsetsFun
36718 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
36719 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a  text *pContext,.
3671a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
3671b 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
3671c 67 76 0a 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74  gv.){.  fulltext
3671d 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
3671e 3b 0a 20 20 69 66 28 20 61 72 67 63 3c 31 20 29  ;.  if( argc<1 )
3671f 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73   return;.  if( s
36720 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
36721 65 28 61 72 67 76 5b 30 5d 29 21 3d 53 51 4c 49  e(argv[0])!=SQLI
36722 54 45 5f 42 4c 4f 42 20 7c 7c 0a 20 20 20 20 20  TE_BLOB ||.     
36723 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
36724 79 74 65 73 28 61 72 67 76 5b 30 5d 29 21 3d 73  ytes(argv[0])!=s
36725 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 20 29  izeof(pCursor) )
36726 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
36727 73 75 6c 74 5f 65 72 72 6f 72 28 70 43 6f 6e 74  sult_error(pCont
36728 65 78 74 2c 20 22 69 6c 6c 65 67 61 6c 20 66 69  ext, "illegal fi
36729 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
3672a 6f 66 66 73 65 74 73 22 2c 2d 31 29 3b 0a 20 20  offsets",-1);.  
3672b 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 63 70  }else{.    memcp
3672c 79 28 26 70 43 75 72 73 6f 72 2c 20 73 71 6c 69  y(&pCursor, sqli
3672d 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61  te3_value_blob(a
3672e 72 67 76 5b 30 5d 29 2c 20 73 69 7a 65 6f 66 28  rgv[0]), sizeof(
3672f 70 43 75 72 73 6f 72 29 29 3b 0a 20 20 20 20 73  pCursor));.    s
36730 6e 69 70 70 65 74 41 6c 6c 4f 66 66 73 65 74 73  nippetAllOffsets
36731 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 73  (pCursor);.    s
36732 6e 69 70 70 65 74 4f 66 66 73 65 74 54 65 78 74  nippetOffsetText
36733 28 26 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70  (&pCursor->snipp
36734 65 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  et);.    sqlite3
36735 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43 6f  _result_text(pCo
36736 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20  ntext,.         
36737 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
36738 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e  Cursor->snippet.
36739 7a 4f 66 66 73 65 74 2c 20 70 43 75 72 73 6f 72  zOffset, pCursor
3673a 2d 3e 73 6e 69 70 70 65 74 2e 6e 4f 66 66 73 65  ->snippet.nOffse
3673b 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
3673c 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
3673d 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 7d  E_STATIC);.  }.}
3673e 0a 0a 2f 2a 20 4f 70 74 4c 65 61 76 65 73 52 65  ../* OptLeavesRe
3673f 61 64 65 72 20 69 73 20 6e 65 61 72 6c 79 20 69  ader is nearly i
36740 64 65 6e 74 69 63 61 6c 20 74 6f 20 4c 65 61 76  dentical to Leav
36741 65 73 52 65 61 64 65 72 2c 20 65 78 63 65 70 74  esReader, except
36742 20 74 68 61 74 0a 2a 2a 20 77 68 65 72 65 20 4c   that.** where L
36743 65 61 76 65 73 52 65 61 64 65 72 20 69 73 20 67  eavesReader is g
36744 65 61 72 65 64 20 74 6f 77 61 72 64 73 20 74 68  eared towards th
36745 65 20 6d 65 72 67 69 6e 67 20 6f 66 20 63 6f 6d  e merging of com
36746 70 6c 65 74 65 0a 2a 2a 20 73 65 67 6d 65 6e 74  plete.** segment
36747 20 6c 65 76 65 6c 73 20 28 77 69 74 68 20 65 78   levels (with ex
36748 61 63 74 6c 79 20 4d 45 52 47 45 5f 43 4f 55 4e  actly MERGE_COUN
36749 54 20 73 65 67 6d 65 6e 74 73 29 2c 20 4f 70 74  T segments), Opt
3674a 4c 65 61 76 65 73 52 65 61 64 65 72 0a 2a 2a 20  LeavesReader.** 
3674b 69 73 20 67 65 61 72 65 64 20 74 6f 77 61 72 64  is geared toward
3674c 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  s implementation
3674d 20 6f 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 65   of the optimize
3674e 28 29 20 66 75 6e 63 74 69 6f 6e 2c 20 61 6e 64  () function, and
3674f 0a 2a 2a 20 63 61 6e 20 6d 65 72 67 65 20 61 6c  .** can merge al
36750 6c 20 73 65 67 6d 65 6e 74 73 20 73 69 6d 75 6c  l segments simul
36751 74 61 6e 65 6f 75 73 6c 79 2e 20 20 54 68 69 73  taneously.  This
36752 20 76 65 72 73 69 6f 6e 20 6d 61 79 20 62 65 0a   version may be.
36753 2a 2a 20 73 6f 6d 65 77 68 61 74 20 6c 65 73 73  ** somewhat less
36754 20 65 66 66 69 63 69 65 6e 74 20 74 68 61 6e 20   efficient than 
36755 4c 65 61 76 65 73 52 65 61 64 65 72 20 62 65 63  LeavesReader bec
36756 61 75 73 65 20 69 74 20 6d 65 72 67 65 73 20 69  ause it merges i
36757 6e 74 6f 20 61 6e 0a 2a 2a 20 61 63 63 75 6d 75  nto an.** accumu
36758 6c 61 74 6f 72 20 72 61 74 68 65 72 20 74 68 61  lator rather tha
36759 6e 20 64 6f 69 6e 67 20 61 6e 20 4e 2d 77 61 79  n doing an N-way
3675a 20 6d 65 72 67 65 2c 20 62 75 74 20 73 69 6e 63   merge, but sinc
3675b 65 20 73 65 67 6d 65 6e 74 0a 2a 2a 20 73 69 7a  e segment.** siz
3675c 65 20 67 72 6f 77 73 20 65 78 70 6f 6e 65 6e 74  e grows exponent
3675d 69 61 6c 6c 79 20 28 73 6f 20 73 65 67 6d 65 6e  ially (so segmen
3675e 74 20 63 6f 75 6e 74 20 6c 6f 67 72 69 74 68 6d  t count logrithm
3675f 69 63 61 6c 6c 79 29 20 74 68 69 73 20 69 73 0a  ically) this is.
36760 2a 2a 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 20  ** probably not 
36761 61 6e 20 69 6d 6d 65 64 69 61 74 65 20 70 72 6f  an immediate pro
36762 62 6c 65 6d 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f  blem..*/./* TODO
36763 28 73 68 65 73 73 29 3a 20 50 72 6f 76 65 20 74  (shess): Prove t
36764 68 61 74 20 61 73 73 65 72 74 69 6f 6e 2c 20 6f  hat assertion, o
36765 72 20 65 78 74 65 6e 64 20 74 68 65 20 6d 65 72  r extend the mer
36766 67 65 20 63 6f 64 65 20 74 6f 0a 2a 2a 20 6d 65  ge code to.** me
36767 72 67 65 20 74 72 65 65 20 66 61 73 68 69 6f 6e  rge tree fashion
36768 20 28 6c 69 6b 65 20 74 68 65 20 70 72 65 66 69   (like the prefi
36769 78 2d 73 65 61 72 63 68 69 6e 67 20 63 6f 64 65  x-searching code
3676a 20 64 6f 65 73 29 2e 0a 2a 2f 0a 2f 2a 20 54 4f   does)..*/./* TO
3676b 44 4f 28 73 68 65 73 73 29 3a 20 4f 70 74 4c 65  DO(shess): OptLe
3676c 61 76 65 73 52 65 61 64 65 72 20 61 6e 64 20 4c  avesReader and L
3676d 65 61 76 65 73 52 65 61 64 65 72 20 63 6f 75 6c  eavesReader coul
3676e 64 20 70 72 6f 62 61 62 6c 79 20 62 65 0a 2a 2a  d probably be.**
3676f 20 6d 65 72 67 65 64 20 77 69 74 68 20 6c 69 74   merged with lit
36770 74 6c 65 20 6f 72 20 6e 6f 20 6c 6f 73 73 20 6f  tle or no loss o
36771 66 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 66 6f  f performance fo
36772 72 20 4c 65 61 76 65 73 52 65 61 64 65 72 2e 20  r LeavesReader. 
36773 20 54 68 65 0a 2a 2a 20 6d 65 72 67 65 64 20 63   The.** merged c
36774 6f 64 65 20 77 6f 75 6c 64 20 6e 65 65 64 20 74  ode would need t
36775 6f 20 68 61 6e 64 6c 65 20 3e 4d 45 52 47 45 5f  o handle >MERGE_
36776 43 4f 55 4e 54 20 73 65 67 6d 65 6e 74 73 2c 20  COUNT segments, 
36777 61 6e 64 20 77 6f 75 6c 64 0a 2a 2a 20 61 6c 73  and would.** als
36778 6f 20 6e 65 65 64 20 74 6f 20 62 65 20 61 62 6c  o need to be abl
36779 65 20 74 6f 20 6f 70 74 69 6f 6e 61 6c 6c 79 20  e to optionally 
3677a 6f 70 74 69 6d 69 7a 65 20 61 77 61 79 20 64 65  optimize away de
3677b 6c 65 74 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65  letes..*/.typede
3677c 66 20 73 74 72 75 63 74 20 4f 70 74 4c 65 61 76  f struct OptLeav
3677d 65 73 52 65 61 64 65 72 20 7b 0a 20 20 2f 2a 20  esReader {.  /* 
3677e 53 65 67 6d 65 6e 74 20 6e 75 6d 62 65 72 2c 20  Segment number, 
3677f 74 6f 20 6f 72 64 65 72 20 72 65 61 64 65 72 73  to order readers
36780 20 62 79 20 61 67 65 2e 20 2a 2f 0a 20 20 69 6e   by age. */.  in
36781 74 20 73 65 67 6d 65 6e 74 3b 0a 20 20 4c 65 61  t segment;.  Lea
36782 76 65 73 52 65 61 64 65 72 20 72 65 61 64 65 72  vesReader reader
36783 3b 0a 7d 20 4f 70 74 4c 65 61 76 65 73 52 65 61  ;.} OptLeavesRea
36784 64 65 72 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74  der;..static int
36785 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   optLeavesReader
36786 41 74 45 6e 64 28 4f 70 74 4c 65 61 76 65 73 52  AtEnd(OptLeavesR
36787 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
36788 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73  .  return leaves
36789 52 65 61 64 65 72 41 74 45 6e 64 28 26 70 52 65  ReaderAtEnd(&pRe
3678a 61 64 65 72 2d 3e 72 65 61 64 65 72 29 3b 0a 7d  ader->reader);.}
3678b 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 74 4c  .static int optL
3678c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 42  eavesReaderTermB
3678d 79 74 65 73 28 4f 70 74 4c 65 61 76 65 73 52 65  ytes(OptLeavesRe
3678e 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
3678f 20 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73 52    return leavesR
36790 65 61 64 65 72 54 65 72 6d 42 79 74 65 73 28 26  eaderTermBytes(&
36791 70 52 65 61 64 65 72 2d 3e 72 65 61 64 65 72 29  pReader->reader)
36792 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.}.static const
36793 20 63 68 61 72 20 2a 6f 70 74 4c 65 61 76 65 73   char *optLeaves
36794 52 65 61 64 65 72 44 61 74 61 28 4f 70 74 4c 65  ReaderData(OptLe
36795 61 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61  avesReader *pRea
36796 64 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c  der){.  return l
36797 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61 28  eavesReaderData(
36798 26 70 52 65 61 64 65 72 2d 3e 72 65 61 64 65 72  &pReader->reader
36799 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  );.}.static int 
3679a 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 44  optLeavesReaderD
3679b 61 74 61 42 79 74 65 73 28 4f 70 74 4c 65 61 76  ataBytes(OptLeav
3679c 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  esReader *pReade
3679d 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61  r){.  return lea
3679e 76 65 73 52 65 61 64 65 72 44 61 74 61 42 79 74  vesReaderDataByt
3679f 65 73 28 26 70 52 65 61 64 65 72 2d 3e 72 65 61  es(&pReader->rea
367a0 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 63  der);.}.static c
367a1 6f 6e 73 74 20 63 68 61 72 20 2a 6f 70 74 4c 65  onst char *optLe
367a2 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 28 4f  avesReaderTerm(O
367a3 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  ptLeavesReader *
367a4 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75  pReader){.  retu
367a5 72 6e 20 6c 65 61 76 65 73 52 65 61 64 65 72 54  rn leavesReaderT
367a6 65 72 6d 28 26 70 52 65 61 64 65 72 2d 3e 72 65  erm(&pReader->re
367a7 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20  ader);.}.static 
367a8 69 6e 74 20 6f 70 74 4c 65 61 76 65 73 52 65 61  int optLeavesRea
367a9 64 65 72 53 74 65 70 28 66 75 6c 6c 74 65 78 74  derStep(fulltext
367aa 5f 76 74 61 62 20 2a 76 2c 20 4f 70 74 4c 65 61  _vtab *v, OptLea
367ab 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
367ac 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65  er){.  return le
367ad 61 76 65 73 52 65 61 64 65 72 53 74 65 70 28 76  avesReaderStep(v
367ae 2c 20 26 70 52 65 61 64 65 72 2d 3e 72 65 61 64  , &pReader->read
367af 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e  er);.}.static in
367b0 74 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65  t optLeavesReade
367b1 72 54 65 72 6d 43 6d 70 28 4f 70 74 4c 65 61 76  rTermCmp(OptLeav
367b2 65 73 52 65 61 64 65 72 20 2a 6c 72 31 2c 20 4f  esReader *lr1, O
367b3 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  ptLeavesReader *
367b4 6c 72 32 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c  lr2){.  return l
367b5 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 43  eavesReaderTermC
367b6 6d 70 28 26 6c 72 31 2d 3e 72 65 61 64 65 72 2c  mp(&lr1->reader,
367b7 20 26 6c 72 32 2d 3e 72 65 61 64 65 72 29 3b 0a   &lr2->reader);.
367b8 7d 0a 2f 2a 20 4f 72 64 65 72 20 62 79 20 74 65  }./* Order by te
367b9 72 6d 20 61 73 63 65 6e 64 69 6e 67 2c 20 73 65  rm ascending, se
367ba 67 6d 65 6e 74 20 61 73 63 65 6e 64 69 6e 67 20  gment ascending 
367bb 28 6f 6c 64 65 73 74 20 74 6f 20 6e 65 77 65 73  (oldest to newes
367bc 74 29 2c 20 77 69 74 68 0a 2a 2a 20 65 78 68 61  t), with.** exha
367bd 75 73 74 65 64 20 72 65 61 64 65 72 73 20 74 6f  usted readers to
367be 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61   the end..*/.sta
367bf 74 69 63 20 69 6e 74 20 6f 70 74 4c 65 61 76 65  tic int optLeave
367c0 73 52 65 61 64 65 72 43 6d 70 28 4f 70 74 4c 65  sReaderCmp(OptLe
367c1 61 76 65 73 52 65 61 64 65 72 20 2a 6c 72 31 2c  avesReader *lr1,
367c2 20 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   OptLeavesReader
367c3 20 2a 6c 72 32 29 7b 0a 20 20 69 6e 74 20 63 20   *lr2){.  int c 
367c4 3d 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65  = optLeavesReade
367c5 72 54 65 72 6d 43 6d 70 28 6c 72 31 2c 20 6c 72  rTermCmp(lr1, lr
367c6 32 29 3b 0a 20 20 69 66 28 20 63 21 3d 30 20 29  2);.  if( c!=0 )
367c7 20 72 65 74 75 72 6e 20 63 3b 0a 20 20 72 65 74   return c;.  ret
367c8 75 72 6e 20 6c 72 31 2d 3e 73 65 67 6d 65 6e 74  urn lr1->segment
367c9 2d 6c 72 32 2d 3e 73 65 67 6d 65 6e 74 3b 0a 7d  -lr2->segment;.}
367ca 0a 2f 2a 20 42 75 62 62 6c 65 20 70 4c 72 5b 30  ./* Bubble pLr[0
367cb 5d 20 74 6f 20 61 70 70 72 6f 70 72 69 61 74 65  ] to appropriate
367cc 20 70 6c 61 63 65 20 69 6e 20 70 4c 72 5b 31 2e   place in pLr[1.
367cd 2e 6e 4c 72 2d 31 5d 2e 20 20 41 73 73 75 6d 65  .nLr-1].  Assume
367ce 73 20 74 68 61 74 0a 2a 2a 20 70 4c 72 5b 31 2e  s that.** pLr[1.
367cf 2e 6e 4c 72 2d 31 5d 20 69 73 20 61 6c 72 65 61  .nLr-1] is alrea
367d0 64 79 20 73 6f 72 74 65 64 2e 0a 2a 2f 0a 73 74  dy sorted..*/.st
367d1 61 74 69 63 20 76 6f 69 64 20 6f 70 74 4c 65 61  atic void optLea
367d2 76 65 73 52 65 61 64 65 72 52 65 6f 72 64 65 72  vesReaderReorder
367d3 28 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72  (OptLeavesReader
367d4 20 2a 70 4c 72 2c 20 69 6e 74 20 6e 4c 72 29 7b   *pLr, int nLr){
367d5 0a 20 20 77 68 69 6c 65 28 20 6e 4c 72 3e 31 20  .  while( nLr>1 
367d6 26 26 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64  && optLeavesRead
367d7 65 72 43 6d 70 28 70 4c 72 2c 20 70 4c 72 2b 31  erCmp(pLr, pLr+1
367d8 29 3e 30 20 29 7b 0a 20 20 20 20 4f 70 74 4c 65  )>0 ){.    OptLe
367d9 61 76 65 73 52 65 61 64 65 72 20 74 6d 70 20 3d  avesReader tmp =
367da 20 70 4c 72 5b 30 5d 3b 0a 20 20 20 20 70 4c 72   pLr[0];.    pLr
367db 5b 30 5d 20 3d 20 70 4c 72 5b 31 5d 3b 0a 20 20  [0] = pLr[1];.  
367dc 20 20 70 4c 72 5b 31 5d 20 3d 20 74 6d 70 3b 0a    pLr[1] = tmp;.
367dd 20 20 20 20 6e 4c 72 2d 2d 3b 0a 20 20 20 20 70      nLr--;.    p
367de 4c 72 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20  Lr++;.  }.}../* 
367df 6f 70 74 69 6d 69 7a 65 28 29 20 68 65 6c 70 65  optimize() helpe
367e0 72 20 66 75 6e 63 74 69 6f 6e 2e 20 20 50 75 74  r function.  Put
367e1 20 74 68 65 20 72 65 61 64 65 72 73 20 69 6e 20   the readers in 
367e2 6f 72 64 65 72 20 61 6e 64 20 69 74 65 72 61 74  order and iterat
367e3 65 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65  e.** through the
367e4 6d 2c 20 6d 65 72 67 69 6e 67 20 64 6f 63 6c 69  m, merging docli
367e5 73 74 73 20 66 6f 72 20 6d 61 74 63 68 69 6e 67  sts for matching
367e6 20 74 65 72 6d 73 20 69 6e 74 6f 20 70 57 72 69   terms into pWri
367e7 74 65 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 73 20  ter..** Returns 
367e8 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63  SQLITE_OK on suc
367e9 63 65 73 73 2c 20 6f 72 20 74 68 65 20 53 51 4c  cess, or the SQL
367ea 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 77  ite error code w
367eb 68 69 63 68 0a 2a 2a 20 70 72 65 76 65 6e 74 65  hich.** prevente
367ec 64 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 74  d success..*/.st
367ed 61 74 69 63 20 69 6e 74 20 6f 70 74 69 6d 69 7a  atic int optimiz
367ee 65 49 6e 74 65 72 6e 61 6c 28 66 75 6c 6c 74 65  eInternal(fullte
367ef 78 74 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20 20  xt_vtab *v,.    
367f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
367f1 20 20 20 20 20 20 20 20 4f 70 74 4c 65 61 76 65          OptLeave
367f2 73 52 65 61 64 65 72 20 2a 72 65 61 64 65 72 73  sReader *readers
367f3 2c 20 69 6e 74 20 6e 52 65 61 64 65 72 73 2c 0a  , int nReaders,.
367f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
367f5 20 20 20 20 20 20 20 20 20 20 20 20 4c 65 61 66              Leaf
367f6 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 29  Writer *pWriter)
367f7 7b 0a 20 20 69 6e 74 20 69 2c 20 72 63 20 3d 20  {.  int i, rc = 
367f8 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 44 61 74  SQLITE_OK;.  Dat
367f9 61 42 75 66 66 65 72 20 64 6f 63 6c 69 73 74 2c  aBuffer doclist,
367fa 20 6d 65 72 67 65 64 2c 20 74 6d 70 3b 0a 0a 20   merged, tmp;.. 
367fb 20 2f 2a 20 4f 72 64 65 72 20 74 68 65 20 72 65   /* Order the re
367fc 61 64 65 72 73 2e 20 2a 2f 0a 20 20 69 20 3d 20  aders. */.  i = 
367fd 6e 52 65 61 64 65 72 73 3b 0a 20 20 77 68 69 6c  nReaders;.  whil
367fe 65 28 20 69 2d 2d 20 3e 20 30 20 29 7b 0a 20 20  e( i-- > 0 ){.  
367ff 20 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65    optLeavesReade
36800 72 52 65 6f 72 64 65 72 28 26 72 65 61 64 65 72  rReorder(&reader
36801 73 5b 69 5d 2c 20 6e 52 65 61 64 65 72 73 2d 69  s[i], nReaders-i
36802 29 3b 0a 20 20 7d 0a 0a 20 20 64 61 74 61 42 75  );.  }..  dataBu
36803 66 66 65 72 49 6e 69 74 28 26 64 6f 63 6c 69 73  fferInit(&doclis
36804 74 2c 20 4c 45 41 46 5f 4d 41 58 29 3b 0a 20 20  t, LEAF_MAX);.  
36805 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26  dataBufferInit(&
36806 6d 65 72 67 65 64 2c 20 4c 45 41 46 5f 4d 41 58  merged, LEAF_MAX
36807 29 3b 0a 0a 20 20 2f 2a 20 45 78 68 61 75 73 74  );..  /* Exhaust
36808 65 64 20 72 65 61 64 65 72 73 20 62 75 62 62 6c  ed readers bubbl
36809 65 20 74 6f 20 74 68 65 20 65 6e 64 2c 20 73 6f  e to the end, so
3680a 20 77 68 65 6e 20 74 68 65 20 66 69 72 73 74 20   when the first 
3680b 72 65 61 64 65 72 20 69 73 0a 20 20 2a 2a 20 61  reader is.  ** a
3680c 74 20 65 6f 66 2c 20 61 6c 6c 20 61 72 65 20 61  t eof, all are a
3680d 74 20 65 6f 66 2e 0a 20 20 2a 2f 0a 20 20 77 68  t eof..  */.  wh
3680e 69 6c 65 28 20 21 6f 70 74 4c 65 61 76 65 73 52  ile( !optLeavesR
3680f 65 61 64 65 72 41 74 45 6e 64 28 26 72 65 61 64  eaderAtEnd(&read
36810 65 72 73 5b 30 5d 29 20 29 7b 0a 0a 20 20 20 20  ers[0]) ){..    
36811 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f  /* Figure out ho
36812 77 20 6d 61 6e 79 20 72 65 61 64 65 72 73 20 73  w many readers s
36813 68 61 72 65 20 74 68 65 20 6e 65 78 74 20 74 65  hare the next te
36814 72 6d 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69  rm. */.    for(i
36815 3d 31 3b 20 69 3c 6e 52 65 61 64 65 72 73 20 26  =1; i<nReaders &
36816 26 20 21 6f 70 74 4c 65 61 76 65 73 52 65 61 64  & !optLeavesRead
36817 65 72 41 74 45 6e 64 28 26 72 65 61 64 65 72 73  erAtEnd(&readers
36818 5b 69 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  [i]); i++){.    
36819 20 20 69 66 28 20 30 21 3d 6f 70 74 4c 65 61 76    if( 0!=optLeav
3681a 65 73 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28  esReaderTermCmp(
3681b 26 72 65 61 64 65 72 73 5b 30 5d 2c 20 26 72 65  &readers[0], &re
3681c 61 64 65 72 73 5b 69 5d 29 20 29 20 62 72 65 61  aders[i]) ) brea
3681d 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
3681e 20 53 70 65 63 69 61 6c 2d 63 61 73 65 20 66 6f   Special-case fo
3681f 72 20 6e 6f 20 6d 65 72 67 65 2e 20 2a 2f 0a 20  r no merge. */. 
36820 20 20 20 69 66 28 20 69 3d 3d 31 20 29 7b 0a 20     if( i==1 ){. 
36821 20 20 20 20 20 2f 2a 20 54 72 69 6d 20 64 65 6c       /* Trim del
36822 65 74 69 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20  etions from the 
36823 64 6f 63 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 20  doclist. */.    
36824 20 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65    dataBufferRese
36825 74 28 26 6d 65 72 67 65 64 29 3b 0a 20 20 20 20  t(&merged);.    
36826 20 20 64 6f 63 4c 69 73 74 54 72 69 6d 28 44 4c    docListTrim(DL
36827 5f 44 45 46 41 55 4c 54 2c 0a 20 20 20 20 20 20  _DEFAULT,.      
36828 20 20 20 20 20 20 20 20 20 20 20 20 6f 70 74 4c              optL
36829 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61 28  eavesReaderData(
3682a 26 72 65 61 64 65 72 73 5b 30 5d 29 2c 0a 20 20  &readers[0]),.  
3682b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3682c 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 44  optLeavesReaderD
3682d 61 74 61 42 79 74 65 73 28 26 72 65 61 64 65 72  ataBytes(&reader
3682e 73 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20 20 20  s[0]),.         
3682f 20 20 20 20 20 20 20 20 20 2d 31 2c 20 44 4c 5f           -1, DL_
36830 44 45 46 41 55 4c 54 2c 20 26 6d 65 72 67 65 64  DEFAULT, &merged
36831 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
36832 20 20 20 20 44 4c 52 65 61 64 65 72 20 64 6c 52      DLReader dlR
36833 65 61 64 65 72 73 5b 4d 45 52 47 45 5f 43 4f 55  eaders[MERGE_COU
36834 4e 54 5d 3b 0a 20 20 20 20 20 20 69 6e 74 20 69  NT];.      int i
36835 52 65 61 64 65 72 2c 20 6e 52 65 61 64 65 72 73  Reader, nReaders
36836 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 50 72 69 6d  ;..      /* Prim
36837 65 20 74 68 65 20 70 69 70 65 6c 69 6e 65 20 77  e the pipeline w
36838 69 74 68 20 74 68 65 20 66 69 72 73 74 20 72 65  ith the first re
36839 61 64 65 72 27 73 20 64 6f 63 6c 69 73 74 2e 20  ader's doclist. 
3683a 20 41 66 74 65 72 0a 20 20 20 20 20 20 2a 2a 20   After.      ** 
3683b 6f 6e 65 20 70 61 73 73 20 69 6e 64 65 78 20 30  one pass index 0
3683c 20 77 69 6c 6c 20 72 65 66 65 72 65 6e 63 65 20   will reference 
3683d 74 68 65 20 61 63 63 75 6d 75 6c 61 74 65 64 20  the accumulated 
3683e 64 6f 63 6c 69 73 74 2e 0a 20 20 20 20 20 20 2a  doclist..      *
3683f 2f 0a 20 20 20 20 20 20 64 6c 72 49 6e 69 74 28  /.      dlrInit(
36840 26 64 6c 52 65 61 64 65 72 73 5b 30 5d 2c 20 44  &dlReaders[0], D
36841 4c 5f 44 45 46 41 55 4c 54 2c 0a 20 20 20 20 20  L_DEFAULT,.     
36842 20 20 20 20 20 20 20 20 20 6f 70 74 4c 65 61 76           optLeav
36843 65 73 52 65 61 64 65 72 44 61 74 61 28 26 72 65  esReaderData(&re
36844 61 64 65 72 73 5b 30 5d 29 2c 0a 20 20 20 20 20  aders[0]),.     
36845 20 20 20 20 20 20 20 20 20 6f 70 74 4c 65 61 76           optLeav
36846 65 73 52 65 61 64 65 72 44 61 74 61 42 79 74 65  esReaderDataByte
36847 73 28 26 72 65 61 64 65 72 73 5b 30 5d 29 29 3b  s(&readers[0]));
36848 0a 20 20 20 20 20 20 69 52 65 61 64 65 72 20 3d  .      iReader =
36849 20 31 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72   1;..      asser
3684a 74 28 20 69 52 65 61 64 65 72 3c 69 20 29 3b 20  t( iReader<i ); 
3684b 20 2f 2a 20 4d 75 73 74 20 65 78 65 63 75 74 65   /* Must execute
3684c 20 74 68 65 20 6c 6f 6f 70 20 61 74 20 6c 65 61   the loop at lea
3684d 73 74 20 6f 6e 63 65 2e 20 2a 2f 0a 20 20 20 20  st once. */.    
3684e 20 20 77 68 69 6c 65 28 20 69 52 65 61 64 65 72    while( iReader
3684f 3c 69 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  <i ){.        /*
36850 20 4d 65 72 67 65 20 31 36 20 69 6e 70 75 74 73   Merge 16 inputs
36851 20 70 65 72 20 70 61 73 73 2e 20 2a 2f 0a 20 20   per pass. */.  
36852 20 20 20 20 20 20 66 6f 72 28 20 6e 52 65 61 64        for( nRead
36853 65 72 73 3d 31 3b 20 69 52 65 61 64 65 72 3c 69  ers=1; iReader<i
36854 20 26 26 20 6e 52 65 61 64 65 72 73 3c 4d 45 52   && nReaders<MER
36855 47 45 5f 43 4f 55 4e 54 3b 0a 20 20 20 20 20 20  GE_COUNT;.      
36856 20 20 20 20 20 20 20 69 52 65 61 64 65 72 2b 2b         iReader++
36857 2c 20 6e 52 65 61 64 65 72 73 2b 2b 20 29 7b 0a  , nReaders++ ){.
36858 20 20 20 20 20 20 20 20 20 20 64 6c 72 49 6e 69            dlrIni
36859 74 28 26 64 6c 52 65 61 64 65 72 73 5b 6e 52 65  t(&dlReaders[nRe
3685a 61 64 65 72 73 5d 2c 20 44 4c 5f 44 45 46 41 55  aders], DL_DEFAU
3685b 4c 54 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  LT,.            
3685c 20 20 20 20 20 20 6f 70 74 4c 65 61 76 65 73 52        optLeavesR
3685d 65 61 64 65 72 44 61 74 61 28 26 72 65 61 64 65  eaderData(&reade
3685e 72 73 5b 69 52 65 61 64 65 72 5d 29 2c 0a 20 20  rs[iReader]),.  
3685f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36860 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 44  optLeavesReaderD
36861 61 74 61 42 79 74 65 73 28 26 72 65 61 64 65 72  ataBytes(&reader
36862 73 5b 69 52 65 61 64 65 72 5d 29 29 3b 0a 20 20  s[iReader]));.  
36863 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
36864 20 2f 2a 20 4d 65 72 67 65 20 64 6f 63 6c 69 73   /* Merge doclis
36865 74 73 20 61 6e 64 20 73 77 61 70 20 72 65 73 75  ts and swap resu
36866 6c 74 20 69 6e 74 6f 20 61 63 63 75 6d 75 6c 61  lt into accumula
36867 74 6f 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  tor. */.        
36868 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74 28  dataBufferReset(
36869 26 6d 65 72 67 65 64 29 3b 0a 20 20 20 20 20 20  &merged);.      
3686a 20 20 64 6f 63 4c 69 73 74 4d 65 72 67 65 28 26    docListMerge(&
3686b 6d 65 72 67 65 64 2c 20 64 6c 52 65 61 64 65 72  merged, dlReader
3686c 73 2c 20 6e 52 65 61 64 65 72 73 29 3b 0a 20 20  s, nReaders);.  
3686d 20 20 20 20 20 20 74 6d 70 20 3d 20 6d 65 72 67        tmp = merg
3686e 65 64 3b 0a 20 20 20 20 20 20 20 20 6d 65 72 67  ed;.        merg
3686f 65 64 20 3d 20 64 6f 63 6c 69 73 74 3b 0a 20 20  ed = doclist;.  
36870 20 20 20 20 20 20 64 6f 63 6c 69 73 74 20 3d 20        doclist = 
36871 74 6d 70 3b 0a 0a 20 20 20 20 20 20 20 20 77 68  tmp;..        wh
36872 69 6c 65 28 20 6e 52 65 61 64 65 72 73 2d 2d 20  ile( nReaders-- 
36873 3e 20 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  > 0 ){.         
36874 20 64 6c 72 44 65 73 74 72 6f 79 28 26 64 6c 52   dlrDestroy(&dlR
36875 65 61 64 65 72 73 5b 6e 52 65 61 64 65 72 73 5d  eaders[nReaders]
36876 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  );.        }..  
36877 20 20 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c        /* Accumul
36878 61 74 65 64 20 64 6f 63 6c 69 73 74 20 74 6f 20  ated doclist to 
36879 72 65 61 64 65 72 20 30 20 66 6f 72 20 6e 65 78  reader 0 for nex
3687a 74 20 70 61 73 73 2e 20 2a 2f 0a 20 20 20 20 20  t pass. */.     
3687b 20 20 20 64 6c 72 49 6e 69 74 28 26 64 6c 52 65     dlrInit(&dlRe
3687c 61 64 65 72 73 5b 30 5d 2c 20 44 4c 5f 44 45 46  aders[0], DL_DEF
3687d 41 55 4c 54 2c 20 64 6f 63 6c 69 73 74 2e 70 44  AULT, doclist.pD
3687e 61 74 61 2c 20 64 6f 63 6c 69 73 74 2e 6e 44 61  ata, doclist.nDa
3687f 74 61 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  ta);.      }..  
36880 20 20 20 20 2f 2a 20 44 65 73 74 72 6f 79 20 72      /* Destroy r
36881 65 61 64 65 72 20 74 68 61 74 20 77 61 73 20 6c  eader that was l
36882 65 66 74 20 69 6e 20 74 68 65 20 70 69 70 65 6c  eft in the pipel
36883 69 6e 65 2e 20 2a 2f 0a 20 20 20 20 20 20 64 6c  ine. */.      dl
36884 72 44 65 73 74 72 6f 79 28 26 64 6c 52 65 61 64  rDestroy(&dlRead
36885 65 72 73 5b 30 5d 29 3b 0a 0a 20 20 20 20 20 20  ers[0]);..      
36886 2f 2a 20 54 72 69 6d 20 64 65 6c 65 74 69 6f 6e  /* Trim deletion
36887 73 20 66 72 6f 6d 20 74 68 65 20 64 6f 63 6c 69  s from the docli
36888 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 64 61 74  st. */.      dat
36889 61 42 75 66 66 65 72 52 65 73 65 74 28 26 6d 65  aBufferReset(&me
3688a 72 67 65 64 29 3b 0a 20 20 20 20 20 20 64 6f 63  rged);.      doc
3688b 4c 69 73 74 54 72 69 6d 28 44 4c 5f 44 45 46 41  ListTrim(DL_DEFA
3688c 55 4c 54 2c 20 64 6f 63 6c 69 73 74 2e 70 44 61  ULT, doclist.pDa
3688d 74 61 2c 20 64 6f 63 6c 69 73 74 2e 6e 44 61 74  ta, doclist.nDat
3688e 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  a,.             
3688f 20 20 20 20 20 2d 31 2c 20 44 4c 5f 44 45 46 41       -1, DL_DEFA
36890 55 4c 54 2c 20 26 6d 65 72 67 65 64 29 3b 0a 20  ULT, &merged);. 
36891 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f 6e 6c     }..    /* Onl
36892 79 20 70 61 73 73 20 64 6f 63 6c 69 73 74 73 20  y pass doclists 
36893 77 69 74 68 20 68 69 74 73 20 28 73 6b 69 70 20  with hits (skip 
36894 69 66 20 61 6c 6c 20 68 69 74 73 20 64 65 6c 65  if all hits dele
36895 74 65 64 29 2e 20 2a 2f 0a 20 20 20 20 69 66 28  ted). */.    if(
36896 20 6d 65 72 67 65 64 2e 6e 44 61 74 61 3e 30 20   merged.nData>0 
36897 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 65  ){.      rc = le
36898 61 66 57 72 69 74 65 72 53 74 65 70 28 76 2c 20  afWriterStep(v, 
36899 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20  pWriter,.       
3689a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3689b 20 20 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64     optLeavesRead
3689c 65 72 54 65 72 6d 28 26 72 65 61 64 65 72 73 5b  erTerm(&readers[
3689d 30 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  0]),.           
3689e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f                 o
3689f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 54 65  ptLeavesReaderTe
368a0 72 6d 42 79 74 65 73 28 26 72 65 61 64 65 72 73  rmBytes(&readers
368a1 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20  [0]),.          
368a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
368a3 6d 65 72 67 65 64 2e 70 44 61 74 61 2c 20 6d 65  merged.pData, me
368a4 72 67 65 64 2e 6e 44 61 74 61 29 3b 0a 20 20 20  rged.nData);.   
368a5 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
368a6 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b  E_OK ) goto err;
368a7 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53  .    }..    /* S
368a8 74 65 70 20 6d 65 72 67 65 64 20 72 65 61 64 65  tep merged reade
368a9 72 73 20 74 6f 20 6e 65 78 74 20 74 65 72 6d 20  rs to next term 
368aa 61 6e 64 20 72 65 6f 72 64 65 72 2e 20 2a 2f 0a  and reorder. */.
368ab 20 20 20 20 77 68 69 6c 65 28 20 69 2d 2d 20 3e      while( i-- >
368ac 20 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d   0 ){.      rc =
368ad 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   optLeavesReader
368ae 53 74 65 70 28 76 2c 20 26 72 65 61 64 65 72 73  Step(v, &readers
368af 5b 69 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  [i]);.      if( 
368b0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
368b1 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20 20 20 20  goto err;..     
368b2 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   optLeavesReader
368b3 52 65 6f 72 64 65 72 28 26 72 65 61 64 65 72 73  Reorder(&readers
368b4 5b 69 5d 2c 20 6e 52 65 61 64 65 72 73 2d 69 29  [i], nReaders-i)
368b5 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 65 72  ;.    }.  }.. er
368b6 72 3a 0a 20 20 64 61 74 61 42 75 66 66 65 72 44  r:.  dataBufferD
368b7 65 73 74 72 6f 79 28 26 64 6f 63 6c 69 73 74 29  estroy(&doclist)
368b8 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  ;.  dataBufferDe
368b9 73 74 72 6f 79 28 26 6d 65 72 67 65 64 29 3b 0a  stroy(&merged);.
368ba 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
368bb 2f 2a 20 49 6d 70 6c 65 6d 65 6e 74 20 6f 70 74  /* Implement opt
368bc 69 6d 69 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e  imize() function
368bd 20 66 6f 72 20 46 54 53 33 2e 20 20 6f 70 74 69   for FTS3.  opti
368be 6d 69 7a 65 28 74 29 20 6d 65 72 67 65 73 20 61  mize(t) merges a
368bf 6c 6c 0a 2a 2a 20 73 65 67 6d 65 6e 74 73 20 69  ll.** segments i
368c0 6e 20 74 68 65 20 66 74 73 20 69 6e 64 65 78 20  n the fts index 
368c1 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 65  into a single se
368c2 67 6d 65 6e 74 2e 20 20 27 74 27 20 69 73 20 74  gment.  't' is t
368c3 68 65 20 6d 61 67 69 63 0a 2a 2a 20 74 61 62 6c  he magic.** tabl
368c4 65 2d 6e 61 6d 65 64 20 63 6f 6c 75 6d 6e 2e 0a  e-named column..
368c5 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f  */.static void o
368c6 70 74 69 6d 69 7a 65 46 75 6e 63 28 73 71 6c 69  ptimizeFunc(sqli
368c7 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f  te3_context *pCo
368c8 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20  ntext,.         
368c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
368ca 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
368cb 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b  3_value **argv){
368cc 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  .  fulltext_curs
368cd 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20 69  or *pCursor;.  i
368ce 66 28 20 61 72 67 63 3e 31 20 29 7b 0a 20 20 20  f( argc>1 ){.   
368cf 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
368d0 65 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20  error(pContext, 
368d1 22 65 78 63 65 73 73 20 61 72 67 75 6d 65 6e 74  "excess argument
368d2 73 20 74 6f 20 6f 70 74 69 6d 69 7a 65 28 29 22  s to optimize()"
368d3 2c 2d 31 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ,-1);.  }else if
368d4 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
368d5 74 79 70 65 28 61 72 67 76 5b 30 5d 29 21 3d 53  type(argv[0])!=S
368d6 51 4c 49 54 45 5f 42 4c 4f 42 20 7c 7c 0a 20 20  QLITE_BLOB ||.  
368d7 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
368d8 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
368d9 67 76 5b 30 5d 29 21 3d 73 69 7a 65 6f 66 28 70  gv[0])!=sizeof(p
368da 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 73  Cursor) ){.    s
368db 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
368dc 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 69  ror(pContext, "i
368dd 6c 6c 65 67 61 6c 20 66 69 72 73 74 20 61 72 67  llegal first arg
368de 75 6d 65 6e 74 20 74 6f 20 6f 70 74 69 6d 69 7a  ument to optimiz
368df 65 22 2c 2d 31 29 3b 0a 20 20 7d 65 6c 73 65 7b  e",-1);.  }else{
368e0 0a 20 20 20 20 66 75 6c 6c 74 65 78 74 5f 76 74  .    fulltext_vt
368e1 61 62 20 2a 76 3b 0a 20 20 20 20 69 6e 74 20 69  ab *v;.    int i
368e2 2c 20 72 63 2c 20 69 4d 61 78 4c 65 76 65 6c 3b  , rc, iMaxLevel;
368e3 0a 20 20 20 20 4f 70 74 4c 65 61 76 65 73 52 65  .    OptLeavesRe
368e4 61 64 65 72 20 2a 72 65 61 64 65 72 73 3b 0a 20  ader *readers;. 
368e5 20 20 20 69 6e 74 20 6e 52 65 61 64 65 72 73 3b     int nReaders;
368e6 0a 20 20 20 20 4c 65 61 66 57 72 69 74 65 72 20  .    LeafWriter 
368e7 77 72 69 74 65 72 3b 0a 20 20 20 20 73 71 6c 69  writer;.    sqli
368e8 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 0a 20 20  te3_stmt *s;..  
368e9 20 20 6d 65 6d 63 70 79 28 26 70 43 75 72 73 6f    memcpy(&pCurso
368ea 72 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  r, sqlite3_value
368eb 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 2c 20  _blob(argv[0]), 
368ec 73 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 29  sizeof(pCursor))
368ed 3b 0a 20 20 20 20 76 20 3d 20 63 75 72 73 6f 72  ;.    v = cursor
368ee 5f 76 74 61 62 28 70 43 75 72 73 6f 72 29 3b 0a  _vtab(pCursor);.
368ef 0a 20 20 20 20 2f 2a 20 46 6c 75 73 68 20 61 6e  .    /* Flush an
368f0 79 20 62 75 66 66 65 72 65 64 20 75 70 64 61 74  y buffered updat
368f1 65 73 20 62 65 66 6f 72 65 20 6f 70 74 69 6d 69  es before optimi
368f2 7a 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 63 20  zing. */.    rc 
368f3 3d 20 66 6c 75 73 68 50 65 6e 64 69 6e 67 54 65  = flushPendingTe
368f4 72 6d 73 28 76 29 3b 0a 20 20 20 20 69 66 28 20  rms(v);.    if( 
368f5 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
368f6 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20 20 20 72  goto err;..    r
368f7 63 20 3d 20 73 65 67 64 69 72 5f 63 6f 75 6e 74  c = segdir_count
368f8 28 76 2c 20 26 6e 52 65 61 64 65 72 73 2c 20 26  (v, &nReaders, &
368f9 69 4d 61 78 4c 65 76 65 6c 29 3b 0a 20 20 20 20  iMaxLevel);.    
368fa 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
368fb 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 20 20  K ) goto err;.  
368fc 20 20 69 66 28 20 6e 52 65 61 64 65 72 73 3d 3d    if( nReaders==
368fd 30 20 7c 7c 20 6e 52 65 61 64 65 72 73 3d 3d 31  0 || nReaders==1
368fe 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
368ff 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43  3_result_text(pC
36900 6f 6e 74 65 78 74 2c 20 22 49 6e 64 65 78 20 61  ontext, "Index a
36901 6c 72 65 61 64 79 20 6f 70 74 69 6d 61 6c 22 2c  lready optimal",
36902 20 2d 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20   -1,.           
36903 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
36904 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
36905 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
36906 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   }..    rc = sql
36907 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
36908 2c 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f  , SEGDIR_SELECT_
36909 41 4c 4c 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20  ALL_STMT, &s);. 
3690a 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
3690b 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b  E_OK ) goto err;
3690c 0a 0a 20 20 20 20 72 65 61 64 65 72 73 20 3d 20  ..    readers = 
3690d 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
3690e 52 65 61 64 65 72 73 2a 73 69 7a 65 6f 66 28 72  Readers*sizeof(r
3690f 65 61 64 65 72 73 5b 30 5d 29 29 3b 0a 20 20 20  eaders[0]));.   
36910 20 69 66 28 20 72 65 61 64 65 72 73 3d 3d 4e 55   if( readers==NU
36911 4c 4c 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a  LL ) goto err;..
36912 20 20 20 20 2f 2a 20 4e 6f 74 65 20 74 68 61 74      /* Note that
36913 20 74 68 65 72 65 20 77 69 6c 6c 20 61 6c 72 65   there will alre
36914 61 64 79 20 62 65 20 61 20 73 65 67 6d 65 6e 74  ady be a segment
36915 20 61 74 20 74 68 69 73 20 70 6f 73 69 74 69 6f   at this positio
36916 6e 0a 20 20 20 20 2a 2a 20 75 6e 74 69 6c 20 77  n.    ** until w
36917 65 20 63 61 6c 6c 20 73 65 67 64 69 72 5f 64 65  e call segdir_de
36918 6c 65 74 65 28 29 20 6f 6e 20 69 4d 61 78 4c 65  lete() on iMaxLe
36919 76 65 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  vel..    */.    
3691a 6c 65 61 66 57 72 69 74 65 72 49 6e 69 74 28 69  leafWriterInit(i
3691b 4d 61 78 4c 65 76 65 6c 2c 20 30 2c 20 26 77 72  MaxLevel, 0, &wr
3691c 69 74 65 72 29 3b 0a 0a 20 20 20 20 69 20 3d 20  iter);..    i = 
3691d 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 72  0;.    while( (r
3691e 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
3691f 28 73 29 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  (s))==SQLITE_ROW
36920 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
36921 5f 69 6e 74 36 34 20 69 53 74 61 72 74 20 3d 20  _int64 iStart = 
36922 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
36923 6e 74 36 34 28 73 2c 20 30 29 3b 0a 20 20 20 20  nt64(s, 0);.    
36924 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
36925 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  End = sqlite3_co
36926 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29  lumn_int64(s, 1)
36927 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  ;.      const ch
36928 61 72 20 2a 70 52 6f 6f 74 44 61 74 61 20 3d 20  ar *pRootData = 
36929 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
3692a 6c 6f 62 28 73 2c 20 32 29 3b 0a 20 20 20 20 20  lob(s, 2);.     
3692b 20 69 6e 74 20 6e 52 6f 6f 74 44 61 74 61 20 3d   int nRootData =
3692c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
3692d 62 79 74 65 73 28 73 2c 20 32 29 3b 0a 0a 20 20  bytes(s, 2);..  
3692e 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e 52      assert( i<nR
3692f 65 61 64 65 72 73 20 29 3b 0a 20 20 20 20 20 20  eaders );.      
36930 72 63 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65  rc = leavesReade
36931 72 49 6e 69 74 28 76 2c 20 2d 31 2c 20 69 53 74  rInit(v, -1, iSt
36932 61 72 74 2c 20 69 45 6e 64 2c 20 70 52 6f 6f 74  art, iEnd, pRoot
36933 44 61 74 61 2c 20 6e 52 6f 6f 74 44 61 74 61 2c  Data, nRootData,
36934 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
36935 20 20 20 20 20 20 20 20 20 20 20 20 20 26 72 65               &re
36936 61 64 65 72 73 5b 69 5d 2e 72 65 61 64 65 72 29  aders[i].reader)
36937 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
36938 53 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65 61  SQLITE_OK ) brea
36939 6b 3b 0a 0a 20 20 20 20 20 20 72 65 61 64 65 72  k;..      reader
3693a 73 5b 69 5d 2e 73 65 67 6d 65 6e 74 20 3d 20 69  s[i].segment = i
3693b 3b 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20  ;.      i++;.   
3693c 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65   }..    /* If we
3693d 20 6d 61 6e 61 67 65 64 20 74 6f 20 73 75 63 63   managed to succ
3693e 65 73 73 66 75 6c 6c 79 20 72 65 61 64 20 74 68  essfully read th
3693f 65 6d 20 61 6c 6c 2c 20 6f 70 74 69 6d 69 7a 65  em all, optimize
36940 20 74 68 65 6d 2e 20 2a 2f 0a 20 20 20 20 69 66   them. */.    if
36941 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
36942 45 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  E ){.      asser
36943 74 28 20 69 3d 3d 6e 52 65 61 64 65 72 73 20 29  t( i==nReaders )
36944 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6f 70 74  ;.      rc = opt
36945 69 6d 69 7a 65 49 6e 74 65 72 6e 61 6c 28 76 2c  imizeInternal(v,
36946 20 72 65 61 64 65 72 73 2c 20 6e 52 65 61 64 65   readers, nReade
36947 72 73 2c 20 26 77 72 69 74 65 72 29 3b 0a 20 20  rs, &writer);.  
36948 20 20 7d 0a 0a 20 20 20 20 77 68 69 6c 65 28 20    }..    while( 
36949 69 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 20  i-- > 0 ){.     
3694a 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 65 73   leavesReaderDes
3694b 74 72 6f 79 28 26 72 65 61 64 65 72 73 5b 69 5d  troy(&readers[i]
3694c 2e 72 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a  .reader);.    }.
3694d 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
3694e 28 72 65 61 64 65 72 73 29 3b 0a 0a 20 20 20 20  (readers);..    
3694f 2f 2a 20 49 66 20 77 65 27 76 65 20 73 75 63 63  /* If we've succ
36950 65 73 73 66 75 6c 6c 79 20 67 6f 74 74 65 6e 20  essfully gotten 
36951 74 6f 20 68 65 72 65 2c 20 64 65 6c 65 74 65 20  to here, delete 
36952 74 68 65 20 6f 6c 64 20 73 65 67 6d 65 6e 74 73  the old segments
36953 0a 20 20 20 20 2a 2a 20 61 6e 64 20 66 6c 75 73  .    ** and flus
36954 68 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20 73  h the interior s
36955 74 72 75 63 74 75 72 65 20 6f 66 20 74 68 65 20  tructure of the 
36956 6e 65 77 20 73 65 67 6d 65 6e 74 2e 0a 20 20 20  new segment..   
36957 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   */.    if( rc==
36958 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
36959 20 20 20 66 6f 72 28 20 69 3d 30 3b 20 69 3c 3d     for( i=0; i<=
3695a 69 4d 61 78 4c 65 76 65 6c 3b 20 69 2b 2b 20 29  iMaxLevel; i++ )
3695b 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
3695c 65 67 64 69 72 5f 64 65 6c 65 74 65 28 76 2c 20  egdir_delete(v, 
3695d 69 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  i);.        if( 
3695e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
3695f 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a  break;.      }..
36960 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
36961 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 6c  LITE_OK ) rc = l
36962 65 61 66 57 72 69 74 65 72 46 69 6e 61 6c 69 7a  eafWriterFinaliz
36963 65 28 76 2c 20 26 77 72 69 74 65 72 29 3b 0a 20  e(v, &writer);. 
36964 20 20 20 7d 0a 0a 20 20 20 20 6c 65 61 66 57 72     }..    leafWr
36965 69 74 65 72 44 65 73 74 72 6f 79 28 26 77 72 69  iterDestroy(&wri
36966 74 65 72 29 3b 0a 0a 20 20 20 20 69 66 28 20 72  ter);..    if( r
36967 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
36968 6f 74 6f 20 65 72 72 3b 0a 0a 20 20 20 20 73 71  oto err;..    sq
36969 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
3696a 74 28 70 43 6f 6e 74 65 78 74 2c 20 22 49 6e 64  t(pContext, "Ind
3696b 65 78 20 6f 70 74 69 6d 69 7a 65 64 22 2c 20 2d  ex optimized", -
3696c 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  1, SQLITE_STATIC
3696d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 0a  );.    return;..
3696e 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73      /* TODO(shes
3696f 73 29 3a 20 45 72 72 6f 72 2d 68 61 6e 64 6c 69  s): Error-handli
36970 6e 67 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69  ng needs to be i
36971 6d 70 72 6f 76 65 64 20 61 6c 6f 6e 67 20 74 68  mproved along th
36972 65 0a 20 20 20 20 2a 2a 20 6c 69 6e 65 73 20 6f  e.    ** lines o
36973 66 20 74 68 65 20 64 75 6d 70 5f 20 66 75 6e 63  f the dump_ func
36974 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f 0a 20 65  tions..    */. e
36975 72 72 3a 0a 20 20 20 20 7b 0a 20 20 20 20 20 20  rr:.    {.      
36976 63 68 61 72 20 62 75 66 5b 35 31 32 5d 3b 0a 20  char buf[512];. 
36977 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
36978 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 62 75 66  rintf(sizeof(buf
36979 29 2c 20 62 75 66 2c 20 22 45 72 72 6f 72 20 69  ), buf, "Error i
3697a 6e 20 6f 70 74 69 6d 69 7a 65 3a 20 25 73 22 2c  n optimize: %s",
3697b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3697c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
3697d 65 72 72 6d 73 67 28 73 71 6c 69 74 65 33 5f 63  errmsg(sqlite3_c
3697e 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
3697f 28 70 43 6f 6e 74 65 78 74 29 29 29 3b 0a 20 20  (pContext)));.  
36980 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
36981 6c 74 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65 78  lt_error(pContex
36982 74 2c 20 62 75 66 2c 20 2d 31 29 3b 0a 20 20 20  t, buf, -1);.   
36983 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 65 66   }.  }.}..#ifdef
36984 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 20   SQLITE_TEST./* 
36985 47 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f  Generate an erro
36986 72 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3c  r of the form "<
36987 70 72 65 66 69 78 3e 3a 20 3c 6d 73 67 3e 22 2e  prefix>: <msg>".
36988 20 20 49 66 20 6d 73 67 20 69 73 20 4e 55 4c 4c    If msg is NULL
36989 2c 0a 2a 2a 20 70 75 6c 6c 20 74 68 65 20 65 72  ,.** pull the er
3698a 72 6f 72 20 66 72 6f 6d 20 74 68 65 20 63 6f 6e  ror from the con
3698b 74 65 78 74 27 73 20 64 62 20 68 61 6e 64 6c 65  text's db handle
3698c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
3698d 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72 28 73   generateError(s
3698e 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
3698f 70 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20  pContext,.      
36990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36991 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
36992 70 72 65 66 69 78 2c 20 63 6f 6e 73 74 20 63 68  prefix, const ch
36993 61 72 20 2a 6d 73 67 29 7b 0a 20 20 63 68 61 72  ar *msg){.  char
36994 20 62 75 66 5b 35 31 32 5d 3b 0a 20 20 69 66 28   buf[512];.  if(
36995 20 6d 73 67 3d 3d 4e 55 4c 4c 20 29 20 6d 73 67   msg==NULL ) msg
36996 20 3d 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73   = sqlite3_errms
36997 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  g(sqlite3_contex
36998 74 5f 64 62 5f 68 61 6e 64 6c 65 28 70 43 6f 6e  t_db_handle(pCon
36999 74 65 78 74 29 29 3b 0a 20 20 73 71 6c 69 74 65  text));.  sqlite
3699a 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
3699b 66 28 62 75 66 29 2c 20 62 75 66 2c 20 22 25 73  f(buf), buf, "%s
3699c 3a 20 25 73 22 2c 20 70 72 65 66 69 78 2c 20 6d  : %s", prefix, m
3699d 73 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72  sg);.  sqlite3_r
3699e 65 73 75 6c 74 5f 65 72 72 6f 72 28 70 43 6f 6e  esult_error(pCon
3699f 74 65 78 74 2c 20 62 75 66 2c 20 2d 31 29 3b 0a  text, buf, -1);.
369a0 7d 0a 0a 2f 2a 20 48 65 6c 70 65 72 20 66 75 6e  }../* Helper fun
369a1 63 74 69 6f 6e 20 74 6f 20 63 6f 6c 6c 65 63 74  ction to collect
369a2 20 74 68 65 20 73 65 74 20 6f 66 20 74 65 72 6d   the set of term
369a3 73 20 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74  s in the segment
369a4 20 69 6e 74 6f 0a 2a 2a 20 70 54 65 72 6d 73 2e   into.** pTerms.
369a5 20 20 54 68 65 20 73 65 67 6d 65 6e 74 20 69 73    The segment is
369a6 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 65 20   defined by the 
369a7 6c 65 61 66 20 6e 6f 64 65 73 20 62 65 74 77 65  leaf nodes betwe
369a8 65 6e 0a 2a 2a 20 69 53 74 61 72 74 42 6c 6f 63  en.** iStartBloc
369a9 6b 69 64 20 61 6e 64 20 69 45 6e 64 42 6c 6f 63  kid and iEndBloc
369aa 6b 69 64 2c 20 69 6e 63 6c 75 73 69 76 65 2c 20  kid, inclusive, 
369ab 6f 72 20 62 79 20 74 68 65 20 63 6f 6e 74 65 6e  or by the conten
369ac 74 73 20 6f 66 0a 2a 2a 20 70 52 6f 6f 74 44 61  ts of.** pRootDa
369ad 74 61 20 69 66 20 69 53 74 61 72 74 42 6c 6f 63  ta if iStartBloc
369ae 6b 69 64 20 69 73 20 30 20 28 69 6e 20 77 68 69  kid is 0 (in whi
369af 63 68 20 63 61 73 65 20 74 68 65 20 65 6e 74 69  ch case the enti
369b0 72 65 20 73 65 67 6d 65 6e 74 0a 2a 2a 20 66 69  re segment.** fi
369b1 74 20 69 6e 20 61 20 6c 65 61 66 29 2e 0a 2a 2f  t in a leaf)..*/
369b2 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6c 6c  .static int coll
369b3 65 63 74 53 65 67 6d 65 6e 74 54 65 72 6d 73 28  ectSegmentTerms(
369b4 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
369b5 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  , sqlite3_stmt *
369b6 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  s,.             
369b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
369b8 20 20 66 74 73 33 48 61 73 68 20 2a 70 54 65 72    fts3Hash *pTer
369b9 6d 73 29 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c  ms){.  const sql
369ba 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61 72 74  ite_int64 iStart
369bb 42 6c 6f 63 6b 69 64 20 3d 20 73 71 6c 69 74 65  Blockid = sqlite
369bc 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73  3_column_int64(s
369bd 2c 20 30 29 3b 0a 20 20 63 6f 6e 73 74 20 73 71  , 0);.  const sq
369be 6c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 42  lite_int64 iEndB
369bf 6c 6f 63 6b 69 64 20 3d 20 73 71 6c 69 74 65 33  lockid = sqlite3
369c0 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c  _column_int64(s,
369c1 20 31 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61   1);.  const cha
369c2 72 20 2a 70 52 6f 6f 74 44 61 74 61 20 3d 20 73  r *pRootData = s
369c3 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
369c4 6f 62 28 73 2c 20 32 29 3b 0a 20 20 63 6f 6e 73  ob(s, 2);.  cons
369c5 74 20 69 6e 74 20 6e 52 6f 6f 74 44 61 74 61 20  t int nRootData 
369c6 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
369c7 5f 62 79 74 65 73 28 73 2c 20 32 29 3b 0a 20 20  _bytes(s, 2);.  
369c8 4c 65 61 76 65 73 52 65 61 64 65 72 20 72 65 61  LeavesReader rea
369c9 64 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  der;.  int rc = 
369ca 6c 65 61 76 65 73 52 65 61 64 65 72 49 6e 69 74  leavesReaderInit
369cb 28 76 2c 20 30 2c 20 69 53 74 61 72 74 42 6c 6f  (v, 0, iStartBlo
369cc 63 6b 69 64 2c 20 69 45 6e 64 42 6c 6f 63 6b 69  ckid, iEndBlocki
369cd 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
369ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
369cf 52 6f 6f 74 44 61 74 61 2c 20 6e 52 6f 6f 74 44  RootData, nRootD
369d0 61 74 61 2c 20 26 72 65 61 64 65 72 29 3b 0a 20  ata, &reader);. 
369d1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
369d2 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
369d3 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51  .  while( rc==SQ
369d4 4c 49 54 45 5f 4f 4b 20 26 26 20 21 6c 65 61 76  LITE_OK && !leav
369d5 65 73 52 65 61 64 65 72 41 74 45 6e 64 28 26 72  esReaderAtEnd(&r
369d6 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20 63 6f  eader) ){.    co
369d7 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 20  nst char *pTerm 
369d8 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65  = leavesReaderTe
369d9 72 6d 28 26 72 65 61 64 65 72 29 3b 0a 20 20 20  rm(&reader);.   
369da 20 63 6f 6e 73 74 20 69 6e 74 20 6e 54 65 72 6d   const int nTerm
369db 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 54   = leavesReaderT
369dc 65 72 6d 42 79 74 65 73 28 26 72 65 61 64 65 72  ermBytes(&reader
369dd 29 3b 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64  );.    void *old
369de 56 61 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 46  Value = sqlite3F
369df 74 73 33 48 61 73 68 46 69 6e 64 28 70 54 65 72  ts3HashFind(pTer
369e0 6d 73 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d  ms, pTerm, nTerm
369e1 29 3b 0a 20 20 20 20 76 6f 69 64 20 2a 6e 65 77  );.    void *new
369e2 56 61 6c 75 65 20 3d 20 28 76 6f 69 64 20 2a 29  Value = (void *)
369e3 28 28 63 68 61 72 20 2a 29 6f 6c 64 56 61 6c 75  ((char *)oldValu
369e4 65 2b 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 72  e+1);..    /* Fr
369e5 6f 6d 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 62  om the comment b
369e6 65 66 6f 72 65 20 73 71 6c 69 74 65 33 46 74 73  efore sqlite3Fts
369e7 33 48 61 73 68 49 6e 73 65 72 74 20 69 6e 20 66  3HashInsert in f
369e8 74 73 33 5f 68 61 73 68 2e 63 2c 0a 20 20 20 20  ts3_hash.c,.    
369e9 2a 2a 20 74 68 65 20 64 61 74 61 20 76 61 6c 75  ** the data valu
369ea 65 20 70 61 73 73 65 64 20 69 73 20 72 65 74 75  e passed is retu
369eb 72 6e 65 64 20 69 6e 20 63 61 73 65 20 6f 66 20  rned in case of 
369ec 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 2e 0a  malloc failure..
369ed 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e      */.    if( n
369ee 65 77 56 61 6c 75 65 3d 3d 73 71 6c 69 74 65 33  ewValue==sqlite3
369ef 46 74 73 33 48 61 73 68 49 6e 73 65 72 74 28 70  Fts3HashInsert(p
369f0 54 65 72 6d 73 2c 20 70 54 65 72 6d 2c 20 6e 54  Terms, pTerm, nT
369f1 65 72 6d 2c 20 6e 65 77 56 61 6c 75 65 29 20 29  erm, newValue) )
369f2 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
369f3 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
369f4 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
369f5 20 6c 65 61 76 65 73 52 65 61 64 65 72 53 74 65   leavesReaderSte
369f6 70 28 76 2c 20 26 72 65 61 64 65 72 29 3b 0a 20  p(v, &reader);. 
369f7 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 6c 65 61 76     }.  }..  leav
369f8 65 73 52 65 61 64 65 72 44 65 73 74 72 6f 79 28  esReaderDestroy(
369f9 26 72 65 61 64 65 72 29 3b 0a 20 20 72 65 74 75  &reader);.  retu
369fa 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 48 65 6c  rn rc;.}../* Hel
369fb 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  per function to 
369fc 62 75 69 6c 64 20 74 68 65 20 72 65 73 75 6c 74  build the result
369fd 20 73 74 72 69 6e 67 20 66 6f 72 20 64 75 6d 70   string for dump
369fe 5f 74 65 72 6d 73 28 29 2e 20 2a 2f 0a 73 74 61  _terms(). */.sta
369ff 74 69 63 20 69 6e 74 20 67 65 6e 65 72 61 74 65  tic int generate
36a00 54 65 72 6d 73 52 65 73 75 6c 74 28 73 71 6c 69  TermsResult(sqli
36a01 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f  te3_context *pCo
36a02 6e 74 65 78 74 2c 20 66 74 73 33 48 61 73 68 20  ntext, fts3Hash 
36a03 2a 70 54 65 72 6d 73 29 7b 0a 20 20 69 6e 74 20  *pTerms){.  int 
36a04 69 54 65 72 6d 2c 20 6e 54 65 72 6d 73 2c 20 6e  iTerm, nTerms, n
36a05 52 65 73 75 6c 74 42 79 74 65 73 2c 20 69 42 79  ResultBytes, iBy
36a06 74 65 3b 0a 20 20 63 68 61 72 20 2a 72 65 73 75  te;.  char *resu
36a07 6c 74 3b 0a 20 20 54 65 72 6d 44 61 74 61 20 2a  lt;.  TermData *
36a08 70 44 61 74 61 3b 0a 20 20 66 74 73 33 48 61 73  pData;.  fts3Has
36a09 68 45 6c 65 6d 20 2a 65 3b 0a 0a 20 20 2f 2a 20  hElem *e;..  /* 
36a0a 49 74 65 72 61 74 65 20 70 54 65 72 6d 73 20 74  Iterate pTerms t
36a0b 6f 20 67 65 6e 65 72 61 74 65 20 61 6e 20 61 72  o generate an ar
36a0c 72 61 79 20 6f 66 20 74 65 72 6d 73 20 69 6e 20  ray of terms in 
36a0d 70 44 61 74 61 20 66 6f 72 0a 20 20 2a 2a 20 73  pData for.  ** s
36a0e 6f 72 74 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 6e  orting..  */.  n
36a0f 54 65 72 6d 73 20 3d 20 66 74 73 33 48 61 73 68  Terms = fts3Hash
36a10 43 6f 75 6e 74 28 70 54 65 72 6d 73 29 3b 0a 20  Count(pTerms);. 
36a11 20 61 73 73 65 72 74 28 20 6e 54 65 72 6d 73 3e   assert( nTerms>
36a12 30 20 29 3b 0a 20 20 70 44 61 74 61 20 3d 20 73  0 );.  pData = s
36a13 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 54  qlite3_malloc(nT
36a14 65 72 6d 73 2a 73 69 7a 65 6f 66 28 54 65 72 6d  erms*sizeof(Term
36a15 44 61 74 61 29 29 3b 0a 20 20 69 66 28 20 70 44  Data));.  if( pD
36a16 61 74 61 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75  ata==NULL ) retu
36a17 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
36a18 0a 0a 20 20 6e 52 65 73 75 6c 74 42 79 74 65 73  ..  nResultBytes
36a19 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 54 65 72   = 0;.  for(iTer
36a1a 6d 20 3d 20 30 2c 20 65 20 3d 20 66 74 73 33 48  m = 0, e = fts3H
36a1b 61 73 68 46 69 72 73 74 28 70 54 65 72 6d 73 29  ashFirst(pTerms)
36a1c 3b 20 65 3b 20 69 54 65 72 6d 2b 2b 2c 20 65 20  ; e; iTerm++, e 
36a1d 3d 20 66 74 73 33 48 61 73 68 4e 65 78 74 28 65  = fts3HashNext(e
36a1e 29 29 7b 0a 20 20 20 20 6e 52 65 73 75 6c 74 42  )){.    nResultB
36a1f 79 74 65 73 20 2b 3d 20 66 74 73 33 48 61 73 68  ytes += fts3Hash
36a20 4b 65 79 73 69 7a 65 28 65 29 2b 31 3b 20 20 20  Keysize(e)+1;   
36a21 2f 2a 20 54 65 72 6d 20 70 6c 75 73 20 74 72 61  /* Term plus tra
36a22 69 6c 69 6e 67 20 73 70 61 63 65 20 2a 2f 0a 20  iling space */. 
36a23 20 20 20 61 73 73 65 72 74 28 20 69 54 65 72 6d     assert( iTerm
36a24 3c 6e 54 65 72 6d 73 20 29 3b 0a 20 20 20 20 70  <nTerms );.    p
36a25 44 61 74 61 5b 69 54 65 72 6d 5d 2e 70 54 65 72  Data[iTerm].pTer
36a26 6d 20 3d 20 66 74 73 33 48 61 73 68 4b 65 79 28  m = fts3HashKey(
36a27 65 29 3b 0a 20 20 20 20 70 44 61 74 61 5b 69 54  e);.    pData[iT
36a28 65 72 6d 5d 2e 6e 54 65 72 6d 20 3d 20 66 74 73  erm].nTerm = fts
36a29 33 48 61 73 68 4b 65 79 73 69 7a 65 28 65 29 3b  3HashKeysize(e);
36a2a 0a 20 20 20 20 70 44 61 74 61 5b 69 54 65 72 6d  .    pData[iTerm
36a2b 5d 2e 70 43 6f 6c 6c 65 63 74 6f 72 20 3d 20 66  ].pCollector = f
36a2c 74 73 33 48 61 73 68 44 61 74 61 28 65 29 3b 20  ts3HashData(e); 
36a2d 20 2f 2a 20 75 6e 75 73 65 64 20 2a 2f 0a 20 20   /* unused */.  
36a2e 7d 0a 20 20 61 73 73 65 72 74 28 20 69 54 65 72  }.  assert( iTer
36a2f 6d 3d 3d 6e 54 65 72 6d 73 20 29 3b 0a 0a 20 20  m==nTerms );..  
36a30 61 73 73 65 72 74 28 20 6e 52 65 73 75 6c 74 42  assert( nResultB
36a31 79 74 65 73 3e 30 20 29 3b 20 20 20 2f 2a 20 6e  ytes>0 );   /* n
36a32 54 65 72 6d 73 3e 30 2c 20 6e 52 65 73 75 6c 74  Terms>0, nResult
36a33 73 42 79 74 65 73 20 6d 75 73 74 20 62 65 2c 20  sBytes must be, 
36a34 74 6f 6f 2e 20 2a 2f 0a 20 20 72 65 73 75 6c 74  too. */.  result
36a35 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
36a36 63 28 6e 52 65 73 75 6c 74 42 79 74 65 73 29 3b  c(nResultBytes);
36a37 0a 20 20 69 66 28 20 72 65 73 75 6c 74 3d 3d 4e  .  if( result==N
36a38 55 4c 4c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ULL ){.    sqlit
36a39 65 33 5f 66 72 65 65 28 70 44 61 74 61 29 3b 0a  e3_free(pData);.
36a3a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
36a3b 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20  E_NOMEM;.  }..  
36a3c 69 66 28 20 6e 54 65 72 6d 73 3e 31 20 29 20 71  if( nTerms>1 ) q
36a3d 73 6f 72 74 28 70 44 61 74 61 2c 20 6e 54 65 72  sort(pData, nTer
36a3e 6d 73 2c 20 73 69 7a 65 6f 66 28 2a 70 44 61 74  ms, sizeof(*pDat
36a3f 61 29 2c 20 74 65 72 6d 44 61 74 61 43 6d 70 29  a), termDataCmp)
36a40 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65  ;..  /* Read the
36a41 20 74 65 72 6d 73 20 69 6e 20 6f 72 64 65 72 20   terms in order 
36a42 74 6f 20 62 75 69 6c 64 20 74 68 65 20 72 65 73  to build the res
36a43 75 6c 74 2e 20 2a 2f 0a 20 20 69 42 79 74 65 20  ult. */.  iByte 
36a44 3d 20 30 3b 0a 20 20 66 6f 72 28 69 54 65 72 6d  = 0;.  for(iTerm
36a45 3d 30 3b 20 69 54 65 72 6d 3c 6e 54 65 72 6d 73  =0; iTerm<nTerms
36a46 3b 20 2b 2b 69 54 65 72 6d 29 7b 0a 20 20 20 20  ; ++iTerm){.    
36a47 6d 65 6d 63 70 79 28 72 65 73 75 6c 74 2b 69 42  memcpy(result+iB
36a48 79 74 65 2c 20 70 44 61 74 61 5b 69 54 65 72 6d  yte, pData[iTerm
36a49 5d 2e 70 54 65 72 6d 2c 20 70 44 61 74 61 5b 69  ].pTerm, pData[i
36a4a 54 65 72 6d 5d 2e 6e 54 65 72 6d 29 3b 0a 20 20  Term].nTerm);.  
36a4b 20 20 69 42 79 74 65 20 2b 3d 20 70 44 61 74 61    iByte += pData
36a4c 5b 69 54 65 72 6d 5d 2e 6e 54 65 72 6d 3b 0a 20  [iTerm].nTerm;. 
36a4d 20 20 20 72 65 73 75 6c 74 5b 69 42 79 74 65 2b     result[iByte+
36a4e 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 7d 0a 20 20  +] = ' ';.  }.  
36a4f 61 73 73 65 72 74 28 20 69 42 79 74 65 3d 3d 6e  assert( iByte==n
36a50 52 65 73 75 6c 74 42 79 74 65 73 20 29 3b 0a 20  ResultBytes );. 
36a51 20 61 73 73 65 72 74 28 20 72 65 73 75 6c 74 5b   assert( result[
36a52 6e 52 65 73 75 6c 74 42 79 74 65 73 2d 31 5d 3d  nResultBytes-1]=
36a53 3d 27 20 27 20 29 3b 0a 20 20 72 65 73 75 6c 74  =' ' );.  result
36a54 5b 6e 52 65 73 75 6c 74 42 79 74 65 73 2d 31 5d  [nResultBytes-1]
36a55 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 2f 2a 20 50   = '\0';..  /* P
36a56 61 73 73 65 73 20 61 77 61 79 20 6f 77 6e 65 72  asses away owner
36a57 73 68 69 70 20 6f 66 20 72 65 73 75 6c 74 2e 20  ship of result. 
36a58 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  */.  sqlite3_res
36a59 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78  ult_text(pContex
36a5a 74 2c 20 72 65 73 75 6c 74 2c 20 6e 52 65 73 75  t, result, nResu
36a5b 6c 74 42 79 74 65 73 2d 31 2c 20 73 71 6c 69 74  ltBytes-1, sqlit
36a5c 65 33 5f 66 72 65 65 29 3b 0a 20 20 73 71 6c 69  e3_free);.  sqli
36a5d 74 65 33 5f 66 72 65 65 28 70 44 61 74 61 29 3b  te3_free(pData);
36a5e 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
36a5f 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 49 6d 70 6c 65  _OK;.}../* Imple
36a60 6d 65 6e 74 73 20 64 75 6d 70 5f 74 65 72 6d 73  ments dump_terms
36a61 28 29 20 66 6f 72 20 75 73 65 20 69 6e 20 69 6e  () for use in in
36a62 73 70 65 63 74 69 6e 67 20 74 68 65 20 66 74 73  specting the fts
36a63 33 20 69 6e 64 65 78 20 66 72 6f 6d 0a 2a 2a 20  3 index from.** 
36a64 74 65 73 74 73 2e 20 20 54 45 58 54 20 72 65 73  tests.  TEXT res
36a65 75 6c 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ult containing t
36a66 68 65 20 6f 72 64 65 72 65 64 20 6c 69 73 74 20  he ordered list 
36a67 6f 66 20 74 65 72 6d 73 20 6a 6f 69 6e 65 64 20  of terms joined 
36a68 62 79 0a 2a 2a 20 73 70 61 63 65 73 2e 20 20 64  by.** spaces.  d
36a69 75 6d 70 5f 74 65 72 6d 73 28 74 2c 20 6c 65 76  ump_terms(t, lev
36a6a 65 6c 2c 20 69 64 78 29 20 64 75 6d 70 73 20 74  el, idx) dumps t
36a6b 68 65 20 74 65 72 6d 73 20 66 6f 72 20 74 68 65  he terms for the
36a6c 20 73 65 67 6d 65 6e 74 0a 2a 2a 20 73 70 65 63   segment.** spec
36a6d 69 66 69 65 64 20 62 79 20 6c 65 76 65 6c 2c 20  ified by level, 
36a6e 69 64 78 20 28 69 6e 20 25 5f 73 65 67 64 69 72  idx (in %_segdir
36a6f 29 2c 20 77 68 69 6c 65 20 64 75 6d 70 5f 74 65  ), while dump_te
36a70 72 6d 73 28 74 29 20 64 75 6d 70 73 0a 2a 2a 20  rms(t) dumps.** 
36a71 61 6c 6c 20 74 65 72 6d 73 20 69 6e 20 74 68 65  all terms in the
36a72 20 69 6e 64 65 78 2e 20 20 49 6e 20 62 6f 74 68   index.  In both
36a73 20 63 61 73 65 73 20 74 20 69 73 20 74 68 65 20   cases t is the 
36a74 66 74 73 20 74 61 62 6c 65 27 73 20 6d 61 67 69  fts table's magi
36a75 63 0a 2a 2a 20 74 61 62 6c 65 2d 6e 61 6d 65 64  c.** table-named
36a76 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74   column..*/.stat
36a77 69 63 20 76 6f 69 64 20 64 75 6d 70 54 65 72 6d  ic void dumpTerm
36a78 73 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  sFunc(.  sqlite3
36a79 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65  _context *pConte
36a7a 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 20  xt,.  int argc, 
36a7b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
36a7c 61 72 67 76 0a 29 7b 0a 20 20 66 75 6c 6c 74 65  argv.){.  fullte
36a7d 78 74 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  xt_cursor *pCurs
36a7e 6f 72 3b 0a 20 20 69 66 28 20 61 72 67 63 21 3d  or;.  if( argc!=
36a7f 33 20 26 26 20 61 72 67 63 21 3d 31 20 29 7b 0a  3 && argc!=1 ){.
36a80 20 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f      generateErro
36a81 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d  r(pContext, "dum
36a82 70 5f 74 65 72 6d 73 22 2c 20 22 69 6e 63 6f 72  p_terms", "incor
36a83 72 65 63 74 20 61 72 67 75 6d 65 6e 74 73 22 29  rect arguments")
36a84 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71  ;.  }else if( sq
36a85 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
36a86 28 61 72 67 76 5b 30 5d 29 21 3d 53 51 4c 49 54  (argv[0])!=SQLIT
36a87 45 5f 42 4c 4f 42 20 7c 7c 0a 20 20 20 20 20 20  E_BLOB ||.      
36a88 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61        sqlite3_va
36a89 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30  lue_bytes(argv[0
36a8a 5d 29 21 3d 73 69 7a 65 6f 66 28 70 43 75 72 73  ])!=sizeof(pCurs
36a8b 6f 72 29 20 29 7b 0a 20 20 20 20 67 65 6e 65 72  or) ){.    gener
36a8c 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78  ateError(pContex
36a8d 74 2c 20 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c  t, "dump_terms",
36a8e 20 22 69 6c 6c 65 67 61 6c 20 66 69 72 73 74 20   "illegal first 
36a8f 61 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 65  argument");.  }e
36a90 6c 73 65 7b 0a 20 20 20 20 66 75 6c 6c 74 65 78  lse{.    fulltex
36a91 74 5f 76 74 61 62 20 2a 76 3b 0a 20 20 20 20 66  t_vtab *v;.    f
36a92 74 73 33 48 61 73 68 20 74 65 72 6d 73 3b 0a 20  ts3Hash terms;. 
36a93 20 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20     sqlite3_stmt 
36a94 2a 73 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69  *s = NULL;.    i
36a95 6e 74 20 72 63 3b 0a 0a 20 20 20 20 6d 65 6d 63  nt rc;..    memc
36a96 70 79 28 26 70 43 75 72 73 6f 72 2c 20 73 71 6c  py(&pCursor, sql
36a97 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28  ite3_value_blob(
36a98 61 72 67 76 5b 30 5d 29 2c 20 73 69 7a 65 6f 66  argv[0]), sizeof
36a99 28 70 43 75 72 73 6f 72 29 29 3b 0a 20 20 20 20  (pCursor));.    
36a9a 76 20 3d 20 63 75 72 73 6f 72 5f 76 74 61 62 28  v = cursor_vtab(
36a9b 70 43 75 72 73 6f 72 29 3b 0a 0a 20 20 20 20 2f  pCursor);..    /
36a9c 2a 20 49 66 20 70 61 73 73 65 64 20 6f 6e 6c 79  * If passed only
36a9d 20 74 68 65 20 63 75 72 73 6f 72 20 63 6f 6c 75   the cursor colu
36a9e 6d 6e 2c 20 67 65 74 20 61 6c 6c 20 73 65 67 6d  mn, get all segm
36a9f 65 6e 74 73 2e 20 20 4f 74 68 65 72 77 69 73 65  ents.  Otherwise
36aa0 0a 20 20 20 20 2a 2a 20 67 65 74 20 74 68 65 20  .    ** get the 
36aa1 73 65 67 6d 65 6e 74 20 64 65 73 63 72 69 62 65  segment describe
36aa2 64 20 62 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69  d by the followi
36aa3 6e 67 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73  ng two arguments
36aa4 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
36aa5 20 61 72 67 63 3d 3d 31 20 29 7b 0a 20 20 20 20   argc==1 ){.    
36aa6 20 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73    rc = sql_get_s
36aa7 74 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44  tatement(v, SEGD
36aa8 49 52 5f 53 45 4c 45 43 54 5f 41 4c 4c 5f 53 54  IR_SELECT_ALL_ST
36aa9 4d 54 2c 20 26 73 29 3b 0a 20 20 20 20 7d 65 6c  MT, &s);.    }el
36aaa 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  se{.      rc = s
36aab 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
36aac 28 76 2c 20 53 45 47 44 49 52 5f 53 45 4c 45 43  (v, SEGDIR_SELEC
36aad 54 5f 53 45 47 4d 45 4e 54 5f 53 54 4d 54 2c 20  T_SEGMENT_STMT, 
36aae 26 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  &s);.      if( r
36aaf 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
36ab0 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
36ab1 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c  ite3_bind_int(s,
36ab2 20 31 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   1, sqlite3_valu
36ab3 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d 29 29 3b  e_int(argv[1]));
36ab4 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
36ab5 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
36ab6 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
36ab7 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c  ite3_bind_int(s,
36ab8 20 32 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   2, sqlite3_valu
36ab9 65 5f 69 6e 74 28 61 72 67 76 5b 32 5d 29 29 3b  e_int(argv[2]));
36aba 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
36abb 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66   }.    }..    if
36abc 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
36abd 29 7b 0a 20 20 20 20 20 20 67 65 6e 65 72 61 74  ){.      generat
36abe 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c  eError(pContext,
36abf 20 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20 4e   "dump_terms", N
36ac0 55 4c 4c 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ULL);.      retu
36ac1 72 6e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  rn;.    }..    /
36ac2 2a 20 43 6f 6c 6c 65 63 74 20 74 68 65 20 74 65  * Collect the te
36ac3 72 6d 73 20 66 6f 72 20 65 61 63 68 20 73 65 67  rms for each seg
36ac4 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 73 71 6c  ment. */.    sql
36ac5 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 69 74  ite3Fts3HashInit
36ac6 28 26 74 65 72 6d 73 2c 20 46 54 53 33 5f 48 41  (&terms, FTS3_HA
36ac7 53 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a 20  SH_STRING, 1);. 
36ac8 20 20 20 77 68 69 6c 65 28 20 28 72 63 20 3d 20     while( (rc = 
36ac9 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 29  sqlite3_step(s))
36aca 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  ==SQLITE_ROW ){.
36acb 20 20 20 20 20 20 72 63 20 3d 20 63 6f 6c 6c 65        rc = colle
36acc 63 74 53 65 67 6d 65 6e 74 54 65 72 6d 73 28 76  ctSegmentTerms(v
36acd 2c 20 73 2c 20 26 74 65 72 6d 73 29 3b 0a 20 20  , s, &terms);.  
36ace 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
36acf 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 20  TE_OK ) break;. 
36ad0 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63     }..    if( rc
36ad1 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  !=SQLITE_DONE ){
36ad2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
36ad3 65 73 65 74 28 73 29 3b 0a 20 20 20 20 20 20 67  eset(s);.      g
36ad4 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f  enerateError(pCo
36ad5 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 74 65 72  ntext, "dump_ter
36ad6 6d 73 22 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20  ms", NULL);.    
36ad7 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 6f 6e  }else{.      con
36ad8 73 74 20 69 6e 74 20 6e 54 65 72 6d 73 20 3d 20  st int nTerms = 
36ad9 66 74 73 33 48 61 73 68 43 6f 75 6e 74 28 26 74  fts3HashCount(&t
36ada 65 72 6d 73 29 3b 0a 20 20 20 20 20 20 69 66 28  erms);.      if(
36adb 20 6e 54 65 72 6d 73 3e 30 20 29 7b 0a 20 20 20   nTerms>0 ){.   
36adc 20 20 20 20 20 72 63 20 3d 20 67 65 6e 65 72 61       rc = genera
36add 74 65 54 65 72 6d 73 52 65 73 75 6c 74 28 70 43  teTermsResult(pC
36ade 6f 6e 74 65 78 74 2c 20 26 74 65 72 6d 73 29 3b  ontext, &terms);
36adf 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
36ae0 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b  =SQLITE_NOMEM ){
36ae1 0a 20 20 20 20 20 20 20 20 20 20 67 65 6e 65 72  .          gener
36ae2 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78  ateError(pContex
36ae3 74 2c 20 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c  t, "dump_terms",
36ae4 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22   "out of memory"
36ae5 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
36ae6 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
36ae7 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
36ae8 4b 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  K );.        }. 
36ae9 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 61       }else if( a
36aea 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20 20 20  rgc==3 ){.      
36aeb 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69 66 69    /* The specifi
36aec 63 20 73 65 67 6d 65 6e 74 20 61 73 6b 65 64 20  c segment asked 
36aed 66 6f 72 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65  for could not be
36aee 20 66 6f 75 6e 64 2e 20 2a 2f 0a 20 20 20 20 20   found. */.     
36aef 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72     generateError
36af0 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70  (pContext, "dump
36af1 5f 74 65 72 6d 73 22 2c 20 22 73 65 67 6d 65 6e  _terms", "segmen
36af2 74 20 6e 6f 74 20 66 6f 75 6e 64 22 29 3b 0a 20  t not found");. 
36af3 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
36af4 20 20 20 20 2f 2a 20 4e 6f 20 73 65 67 6d 65 6e      /* No segmen
36af5 74 73 20 66 6f 75 6e 64 2e 20 2a 2f 0a 20 20 20  ts found. */.   
36af6 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65       /* TODO(she
36af7 73 73 29 3a 20 49 74 20 73 68 6f 75 6c 64 20 62  ss): It should b
36af8 65 20 69 6d 70 6f 73 73 69 62 6c 65 20 74 6f 20  e impossible to 
36af9 72 65 61 63 68 20 74 68 69 73 2e 20 20 54 68 69  reach this.  Thi
36afa 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 61 73  s.        ** cas
36afb 65 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65  e can only happe
36afc 6e 20 66 6f 72 20 61 6e 20 65 6d 70 74 79 20 74  n for an empty t
36afd 61 62 6c 65 2c 20 69 6e 20 77 68 69 63 68 20 63  able, in which c
36afe 61 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 53  ase.        ** S
36aff 51 4c 69 74 65 20 68 61 73 20 6e 6f 20 72 6f 77  QLite has no row
36b00 73 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 66  s to call this f
36b01 75 6e 63 74 69 6f 6e 20 6f 6e 2e 0a 20 20 20 20  unction on..    
36b02 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73      */.        s
36b03 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
36b04 6c 6c 28 70 43 6f 6e 74 65 78 74 29 3b 0a 20 20  ll(pContext);.  
36b05 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
36b06 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 43  sqlite3Fts3HashC
36b07 6c 65 61 72 28 26 74 65 72 6d 73 29 3b 0a 20 20  lear(&terms);.  
36b08 7d 0a 7d 0a 0a 2f 2a 20 45 78 70 61 6e 64 20 74  }.}../* Expand t
36b09 68 65 20 44 4c 5f 44 45 46 41 55 4c 54 20 64 6f  he DL_DEFAULT do
36b0a 63 6c 69 73 74 20 69 6e 20 70 44 61 74 61 20 69  clist in pData i
36b0b 6e 74 6f 20 61 20 74 65 78 74 20 72 65 73 75 6c  nto a text resul
36b0c 74 20 69 6e 0a 2a 2a 20 70 43 6f 6e 74 65 78 74  t in.** pContext
36b0d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
36b0e 20 63 72 65 61 74 65 44 6f 63 6c 69 73 74 52 65   createDoclistRe
36b0f 73 75 6c 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e  sult(sqlite3_con
36b10 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a  text *pContext,.
36b11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b13 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
36b14 61 2c 20 69 6e 74 20 6e 44 61 74 61 29 7b 0a 20  a, int nData){. 
36b15 20 44 61 74 61 42 75 66 66 65 72 20 64 75 6d 70   DataBuffer dump
36b16 3b 0a 20 20 44 4c 52 65 61 64 65 72 20 64 6c 52  ;.  DLReader dlR
36b17 65 61 64 65 72 3b 0a 0a 20 20 61 73 73 65 72 74  eader;..  assert
36b18 28 20 70 44 61 74 61 21 3d 4e 55 4c 4c 20 26 26  ( pData!=NULL &&
36b19 20 6e 44 61 74 61 3e 30 20 29 3b 0a 0a 20 20 64   nData>0 );..  d
36b1a 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 64  ataBufferInit(&d
36b1b 75 6d 70 2c 20 30 29 3b 0a 20 20 64 6c 72 49 6e  ump, 0);.  dlrIn
36b1c 69 74 28 26 64 6c 52 65 61 64 65 72 2c 20 44 4c  it(&dlReader, DL
36b1d 5f 44 45 46 41 55 4c 54 2c 20 70 44 61 74 61 2c  _DEFAULT, pData,
36b1e 20 6e 44 61 74 61 29 3b 0a 20 20 66 6f 72 28 20   nData);.  for( 
36b1f 3b 20 21 64 6c 72 41 74 45 6e 64 28 26 64 6c 52  ; !dlrAtEnd(&dlR
36b20 65 61 64 65 72 29 3b 20 64 6c 72 53 74 65 70 28  eader); dlrStep(
36b21 26 64 6c 52 65 61 64 65 72 29 20 29 7b 0a 20 20  &dlReader) ){.  
36b22 20 20 63 68 61 72 20 62 75 66 5b 32 35 36 5d 3b    char buf[256];
36b23 0a 20 20 20 20 50 4c 52 65 61 64 65 72 20 70 6c  .    PLReader pl
36b24 52 65 61 64 65 72 3b 0a 0a 20 20 20 20 70 6c 72  Reader;..    plr
36b25 49 6e 69 74 28 26 70 6c 52 65 61 64 65 72 2c 20  Init(&plReader, 
36b26 26 64 6c 52 65 61 64 65 72 29 3b 0a 20 20 20 20  &dlReader);.    
36b27 69 66 28 20 44 4c 5f 44 45 46 41 55 4c 54 3d 3d  if( DL_DEFAULT==
36b28 44 4c 5f 44 4f 43 49 44 53 20 7c 7c 20 70 6c 72  DL_DOCIDS || plr
36b29 41 74 45 6e 64 28 26 70 6c 52 65 61 64 65 72 29  AtEnd(&plReader)
36b2a 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
36b2b 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
36b2c 66 28 62 75 66 29 2c 20 62 75 66 2c 20 22 5b 25  f(buf), buf, "[%
36b2d 6c 6c 64 5d 20 22 2c 20 64 6c 72 44 6f 63 69 64  lld] ", dlrDocid
36b2e 28 26 64 6c 52 65 61 64 65 72 29 29 3b 0a 20 20  (&dlReader));.  
36b2f 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70      dataBufferAp
36b30 70 65 6e 64 28 26 64 75 6d 70 2c 20 62 75 66 2c  pend(&dump, buf,
36b31 20 73 74 72 6c 65 6e 28 62 75 66 29 29 3b 0a 20   strlen(buf));. 
36b32 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
36b33 69 6e 74 20 69 43 6f 6c 75 6d 6e 20 3d 20 70 6c  int iColumn = pl
36b34 72 43 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64 65  rColumn(&plReade
36b35 72 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74  r);..      sqlit
36b36 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65  e3_snprintf(size
36b37 6f 66 28 62 75 66 29 2c 20 62 75 66 2c 20 22 5b  of(buf), buf, "[
36b38 25 6c 6c 64 20 25 64 5b 22 2c 0a 20 20 20 20 20  %lld %d[",.     
36b39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b3a 20 20 64 6c 72 44 6f 63 69 64 28 26 64 6c 52 65    dlrDocid(&dlRe
36b3b 61 64 65 72 29 2c 20 69 43 6f 6c 75 6d 6e 29 3b  ader), iColumn);
36b3c 0a 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65  .      dataBuffe
36b3d 72 41 70 70 65 6e 64 28 26 64 75 6d 70 2c 20 62  rAppend(&dump, b
36b3e 75 66 2c 20 73 74 72 6c 65 6e 28 62 75 66 29 29  uf, strlen(buf))
36b3f 3b 0a 0a 20 20 20 20 20 20 66 6f 72 28 20 3b 20  ;..      for( ; 
36b40 21 70 6c 72 41 74 45 6e 64 28 26 70 6c 52 65 61  !plrAtEnd(&plRea
36b41 64 65 72 29 3b 20 70 6c 72 53 74 65 70 28 26 70  der); plrStep(&p
36b42 6c 52 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20  lReader) ){.    
36b43 20 20 20 20 69 66 28 20 70 6c 72 43 6f 6c 75 6d      if( plrColum
36b44 6e 28 26 70 6c 52 65 61 64 65 72 29 21 3d 69 43  n(&plReader)!=iC
36b45 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20  olumn ){.       
36b46 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 70 6c 72     iColumn = plr
36b47 43 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64 65 72  Column(&plReader
36b48 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
36b49 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
36b4a 7a 65 6f 66 28 62 75 66 29 2c 20 62 75 66 2c 20  zeof(buf), buf, 
36b4b 22 5d 20 25 64 5b 22 2c 20 69 43 6f 6c 75 6d 6e  "] %d[", iColumn
36b4c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  );.          ass
36b4d 65 72 74 28 20 64 75 6d 70 2e 6e 44 61 74 61 3e  ert( dump.nData>
36b4e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 64  0 );.          d
36b4f 75 6d 70 2e 6e 44 61 74 61 2d 2d 3b 20 20 20 20  ump.nData--;    
36b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b51 20 2f 2a 20 4f 76 65 72 77 72 69 74 65 20 74 72   /* Overwrite tr
36b52 61 69 6c 69 6e 67 20 73 70 61 63 65 2e 20 2a 2f  ailing space. */
36b53 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
36b54 74 28 20 64 75 6d 70 2e 70 44 61 74 61 5b 64 75  t( dump.pData[du
36b55 6d 70 2e 6e 44 61 74 61 5d 3d 3d 27 20 27 29 3b  mp.nData]==' ');
36b56 0a 20 20 20 20 20 20 20 20 20 20 64 61 74 61 42  .          dataB
36b57 75 66 66 65 72 41 70 70 65 6e 64 28 26 64 75 6d  ufferAppend(&dum
36b58 70 2c 20 62 75 66 2c 20 73 74 72 6c 65 6e 28 62  p, buf, strlen(b
36b59 75 66 29 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  uf));.        }.
36b5a 20 20 20 20 20 20 20 20 69 66 28 20 44 4c 5f 44          if( DL_D
36b5b 45 46 41 55 4c 54 3d 3d 44 4c 5f 50 4f 53 49 54  EFAULT==DL_POSIT
36b5c 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 7b 0a  IONS_OFFSETS ){.
36b5d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
36b5e 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
36b5f 66 28 62 75 66 29 2c 20 62 75 66 2c 20 22 25 64  f(buf), buf, "%d
36b60 2c 25 64 2c 25 64 20 22 2c 0a 20 20 20 20 20 20  ,%d,%d ",.      
36b61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b62 20 20 20 20 20 70 6c 72 50 6f 73 69 74 69 6f 6e       plrPosition
36b63 28 26 70 6c 52 65 61 64 65 72 29 2c 0a 20 20 20  (&plReader),.   
36b64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b65 20 20 20 20 20 20 20 20 70 6c 72 53 74 61 72 74          plrStart
36b66 4f 66 66 73 65 74 28 26 70 6c 52 65 61 64 65 72  Offset(&plReader
36b67 29 2c 20 70 6c 72 45 6e 64 4f 66 66 73 65 74 28  ), plrEndOffset(
36b68 26 70 6c 52 65 61 64 65 72 29 29 3b 0a 20 20 20  &plReader));.   
36b69 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 44       }else if( D
36b6a 4c 5f 44 45 46 41 55 4c 54 3d 3d 44 4c 5f 50 4f  L_DEFAULT==DL_PO
36b6b 53 49 54 49 4f 4e 53 20 29 7b 0a 20 20 20 20 20  SITIONS ){.     
36b6c 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
36b6d 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 62 75 66  rintf(sizeof(buf
36b6e 29 2c 20 62 75 66 2c 20 22 25 64 20 22 2c 20 70  ), buf, "%d ", p
36b6f 6c 72 50 6f 73 69 74 69 6f 6e 28 26 70 6c 52 65  lrPosition(&plRe
36b70 61 64 65 72 29 29 3b 0a 20 20 20 20 20 20 20 20  ader));.        
36b71 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
36b72 20 61 73 73 65 72 74 28 20 4e 55 4c 4c 3d 3d 22   assert( NULL=="
36b73 55 6e 68 61 6e 64 6c 65 64 20 44 4c 5f 44 45 46  Unhandled DL_DEF
36b74 41 55 4c 54 20 76 61 6c 75 65 22 29 3b 0a 20 20  AULT value");.  
36b75 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
36b76 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
36b77 28 26 64 75 6d 70 2c 20 62 75 66 2c 20 73 74 72  (&dump, buf, str
36b78 6c 65 6e 28 62 75 66 29 29 3b 0a 20 20 20 20 20  len(buf));.     
36b79 20 7d 0a 20 20 20 20 20 20 70 6c 72 44 65 73 74   }.      plrDest
36b7a 72 6f 79 28 26 70 6c 52 65 61 64 65 72 29 3b 0a  roy(&plReader);.
36b7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64  .      assert( d
36b7c 75 6d 70 2e 6e 44 61 74 61 3e 30 20 29 3b 0a 20  ump.nData>0 );. 
36b7d 20 20 20 20 20 64 75 6d 70 2e 6e 44 61 74 61 2d       dump.nData-
36b7e 2d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  -;              
36b7f 20 20 20 20 20 20 20 2f 2a 20 4f 76 65 72 77 72         /* Overwr
36b80 69 74 65 20 74 72 61 69 6c 69 6e 67 20 73 70 61  ite trailing spa
36b81 63 65 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73  ce. */.      ass
36b82 65 72 74 28 20 64 75 6d 70 2e 70 44 61 74 61 5b  ert( dump.pData[
36b83 64 75 6d 70 2e 6e 44 61 74 61 5d 3d 3d 27 20 27  dump.nData]==' '
36b84 29 3b 0a 20 20 20 20 20 20 64 61 74 61 42 75 66  );.      dataBuf
36b85 66 65 72 41 70 70 65 6e 64 28 26 64 75 6d 70 2c  ferAppend(&dump,
36b86 20 22 5d 5d 20 22 2c 20 33 29 3b 0a 20 20 20 20   "]] ", 3);.    
36b87 7d 0a 20 20 7d 0a 20 20 64 6c 72 44 65 73 74 72  }.  }.  dlrDestr
36b88 6f 79 28 26 64 6c 52 65 61 64 65 72 29 3b 0a 0a  oy(&dlReader);..
36b89 20 20 61 73 73 65 72 74 28 20 64 75 6d 70 2e 6e    assert( dump.n
36b8a 44 61 74 61 3e 30 20 29 3b 0a 20 20 64 75 6d 70  Data>0 );.  dump
36b8b 2e 6e 44 61 74 61 2d 2d 3b 20 20 20 20 20 20 20  .nData--;       
36b8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
36b8d 20 4f 76 65 72 77 72 69 74 65 20 74 72 61 69 6c   Overwrite trail
36b8e 69 6e 67 20 73 70 61 63 65 2e 20 2a 2f 0a 20 20  ing space. */.  
36b8f 61 73 73 65 72 74 28 20 64 75 6d 70 2e 70 44 61  assert( dump.pDa
36b90 74 61 5b 64 75 6d 70 2e 6e 44 61 74 61 5d 3d 3d  ta[dump.nData]==
36b91 27 20 27 29 3b 0a 20 20 64 75 6d 70 2e 70 44 61  ' ');.  dump.pDa
36b92 74 61 5b 64 75 6d 70 2e 6e 44 61 74 61 5d 20 3d  ta[dump.nData] =
36b93 20 27 5c 30 27 3b 0a 20 20 61 73 73 65 72 74 28   '\0';.  assert(
36b94 20 64 75 6d 70 2e 6e 44 61 74 61 3e 30 20 29 3b   dump.nData>0 );
36b95 0a 0a 20 20 2f 2a 20 50 61 73 73 65 73 20 6f 77  ..  /* Passes ow
36b96 6e 65 72 73 68 69 70 20 6f 66 20 64 75 6d 70 27  nership of dump'
36b97 73 20 62 75 66 66 65 72 20 74 6f 20 70 43 6f 6e  s buffer to pCon
36b98 74 65 78 74 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  text. */.  sqlit
36b99 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70  e3_result_text(p
36b9a 43 6f 6e 74 65 78 74 2c 20 64 75 6d 70 2e 70 44  Context, dump.pD
36b9b 61 74 61 2c 20 64 75 6d 70 2e 6e 44 61 74 61 2c  ata, dump.nData,
36b9c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
36b9d 20 20 64 75 6d 70 2e 70 44 61 74 61 20 3d 20 4e    dump.pData = N
36b9e 55 4c 4c 3b 0a 20 20 64 75 6d 70 2e 6e 44 61 74  ULL;.  dump.nDat
36b9f 61 20 3d 20 64 75 6d 70 2e 6e 43 61 70 61 63 69  a = dump.nCapaci
36ba0 74 79 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20 49 6d  ty = 0;.}../* Im
36ba1 70 6c 65 6d 65 6e 74 73 20 64 75 6d 70 5f 64 6f  plements dump_do
36ba2 63 6c 69 73 74 28 29 20 66 6f 72 20 75 73 65 20  clist() for use 
36ba3 69 6e 20 69 6e 73 70 65 63 74 69 6e 67 20 74 68  in inspecting th
36ba4 65 20 66 74 73 33 20 69 6e 64 65 78 20 66 72 6f  e fts3 index fro
36ba5 6d 0a 2a 2a 20 74 65 73 74 73 2e 20 20 54 45 58  m.** tests.  TEX
36ba6 54 20 72 65 73 75 6c 74 20 63 6f 6e 74 61 69 6e  T result contain
36ba7 69 6e 67 20 61 20 73 74 72 69 6e 67 20 72 65 70  ing a string rep
36ba8 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  resentation of t
36ba9 68 65 0a 2a 2a 20 64 6f 63 6c 69 73 74 20 66 6f  he.** doclist fo
36baa 72 20 74 68 65 20 69 6e 64 69 63 61 74 65 64 20  r the indicated 
36bab 74 65 72 6d 2e 20 20 64 75 6d 70 5f 64 6f 63 6c  term.  dump_docl
36bac 69 73 74 28 74 2c 20 74 65 72 6d 2c 20 6c 65 76  ist(t, term, lev
36bad 65 6c 2c 20 69 64 78 29 0a 2a 2a 20 64 75 6d 70  el, idx).** dump
36bae 73 20 74 68 65 20 64 6f 63 6c 69 73 74 20 66 6f  s the doclist fo
36baf 72 20 74 65 72 6d 20 66 72 6f 6d 20 74 68 65 20  r term from the 
36bb0 73 65 67 6d 65 6e 74 20 73 70 65 63 69 66 69 65  segment specifie
36bb1 64 20 62 79 20 6c 65 76 65 6c 2c 20 69 64 78 0a  d by level, idx.
36bb2 2a 2a 20 28 69 6e 20 25 5f 73 65 67 64 69 72 29  ** (in %_segdir)
36bb3 2c 20 77 68 69 6c 65 20 64 75 6d 70 5f 64 6f 63  , while dump_doc
36bb4 6c 69 73 74 28 74 2c 20 74 65 72 6d 29 20 64 75  list(t, term) du
36bb5 6d 70 73 20 74 68 65 20 6c 6f 67 69 63 61 6c 0a  mps the logical.
36bb6 2a 2a 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 74  ** doclist for t
36bb7 68 65 20 74 65 72 6d 20 61 63 72 6f 73 73 20 61  he term across a
36bb8 6c 6c 20 73 65 67 6d 65 6e 74 73 2e 20 20 54 68  ll segments.  Th
36bb9 65 20 70 65 72 2d 73 65 67 6d 65 6e 74 20 64 6f  e per-segment do
36bba 63 6c 69 73 74 0a 2a 2a 20 63 61 6e 20 63 6f 6e  clist.** can con
36bbb 74 61 69 6e 20 64 65 6c 65 74 69 6f 6e 73 2c 20  tain deletions, 
36bbc 77 68 69 6c 65 20 74 68 65 20 66 75 6c 6c 2d 69  while the full-i
36bbd 6e 64 65 78 20 64 6f 63 6c 69 73 74 20 77 69 6c  ndex doclist wil
36bbe 6c 20 6e 6f 74 0a 2a 2a 20 28 64 65 6c 65 74 69  l not.** (deleti
36bbf 6f 6e 73 20 61 72 65 20 6f 6d 69 74 74 65 64 29  ons are omitted)
36bc0 2e 0a 2a 2a 0a 2a 2a 20 52 65 73 75 6c 74 20 66  ..**.** Result f
36bc1 6f 72 6d 61 74 73 20 64 69 66 66 65 72 20 77 69  ormats differ wi
36bc2 74 68 20 74 68 65 20 73 65 74 74 69 6e 67 20 6f  th the setting o
36bc3 66 20 44 4c 5f 44 45 46 41 55 4c 54 53 2e 20 20  f DL_DEFAULTS.  
36bc4 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20  Examples:.**.** 
36bc5 44 4c 5f 44 4f 43 49 44 53 3a 20 5b 31 5d 20 5b  DL_DOCIDS: [1] [
36bc6 33 5d 20 5b 37 5d 0a 2a 2a 20 44 4c 5f 50 4f 53  3] [7].** DL_POS
36bc7 49 54 49 4f 4e 53 3a 20 5b 31 20 30 5b 30 20 34  ITIONS: [1 0[0 4
36bc8 5d 20 31 5b 31 37 5d 5d 20 5b 33 20 31 5b 35 5d  ] 1[17]] [3 1[5]
36bc9 5d 0a 2a 2a 20 44 4c 5f 50 4f 53 49 54 49 4f 4e  ].** DL_POSITION
36bca 53 5f 4f 46 46 53 45 54 53 3a 20 5b 31 20 30 5b  S_OFFSETS: [1 0[
36bcb 30 2c 30 2c 33 20 34 2c 32 33 2c 32 36 5d 20 31  0,0,3 4,23,26] 1
36bcc 5b 31 37 2c 31 30 32 2c 31 30 35 5d 5d 20 5b 33  [17,102,105]] [3
36bcd 20 31 5b 35 2c 32 30 2c 32 33 5d 5d 0a 2a 2a 0a   1[5,20,23]].**.
36bce 2a 2a 20 49 6e 20 65 61 63 68 20 63 61 73 65 20  ** In each case 
36bcf 74 68 65 20 6e 75 6d 62 65 72 20 61 66 74 65 72  the number after
36bd0 20 74 68 65 20 6f 75 74 65 72 20 27 5b 27 20 69   the outer '[' i
36bd1 73 20 74 68 65 20 64 6f 63 69 64 2e 20 20 49 6e  s the docid.  In
36bd2 20 74 68 65 0a 2a 2a 20 6c 61 74 74 65 72 20 74   the.** latter t
36bd3 77 6f 20 63 61 73 65 73 2c 20 74 68 65 20 6e 75  wo cases, the nu
36bd4 6d 62 65 72 20 62 65 66 6f 72 65 20 74 68 65 20  mber before the 
36bd5 69 6e 6e 65 72 20 27 5b 27 20 69 73 20 74 68 65  inner '[' is the
36bd6 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 61 73 73 6f 63   column.** assoc
36bd7 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 76  iated with the v
36bd8 61 6c 75 65 73 20 77 69 74 68 69 6e 2e 20 20 46  alues within.  F
36bd9 6f 72 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20  or DL_POSITIONS 
36bda 74 68 65 20 6e 75 6d 62 65 72 73 0a 2a 2a 20 77  the numbers.** w
36bdb 69 74 68 69 6e 20 61 72 65 20 74 68 65 20 70 6f  ithin are the po
36bdc 73 69 74 69 6f 6e 73 2c 20 66 6f 72 20 44 4c 5f  sitions, for DL_
36bdd 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54  POSITIONS_OFFSET
36bde 53 20 74 68 65 79 20 61 72 65 20 74 68 65 0a 2a  S they are the.*
36bdf 2a 20 70 6f 73 69 74 69 6f 6e 2c 20 74 68 65 20  * position, the 
36be0 73 74 61 72 74 20 6f 66 66 73 65 74 2c 20 61 6e  start offset, an
36be1 64 20 74 68 65 20 65 6e 64 20 6f 66 66 73 65 74  d the end offset
36be2 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
36be3 20 64 75 6d 70 44 6f 63 6c 69 73 74 46 75 6e 63   dumpDoclistFunc
36be4 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
36be5 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20  ext *pContext,. 
36be6 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
36be7 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
36be8 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75  ){.  fulltext_cu
36be9 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20  rsor *pCursor;. 
36bea 20 69 66 28 20 61 72 67 63 21 3d 32 20 26 26 20   if( argc!=2 && 
36beb 61 72 67 63 21 3d 34 20 29 7b 0a 20 20 20 20 67  argc!=4 ){.    g
36bec 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f  enerateError(pCo
36bed 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63  ntext, "dump_doc
36bee 6c 69 73 74 22 2c 20 22 69 6e 63 6f 72 72 65 63  list", "incorrec
36bef 74 20 61 72 67 75 6d 65 6e 74 73 22 29 3b 0a 20  t arguments");. 
36bf0 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74   }else if( sqlit
36bf1 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
36bf2 67 76 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42  gv[0])!=SQLITE_B
36bf3 4c 4f 42 20 7c 7c 0a 20 20 20 20 20 20 20 20 20  LOB ||.         
36bf4 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65     sqlite3_value
36bf5 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 21  _bytes(argv[0])!
36bf6 3d 73 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29  =sizeof(pCursor)
36bf7 20 29 7b 0a 20 20 20 20 67 65 6e 65 72 61 74 65   ){.    generate
36bf8 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20  Error(pContext, 
36bf9 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20  "dump_doclist", 
36bfa 22 69 6c 6c 65 67 61 6c 20 66 69 72 73 74 20 61  "illegal first a
36bfb 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 65 6c  rgument");.  }el
36bfc 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76  se if( sqlite3_v
36bfd 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31  alue_text(argv[1
36bfe 5d 29 3d 3d 4e 55 4c 4c 20 7c 7c 0a 20 20 20 20  ])==NULL ||.    
36bff 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
36c00 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
36c01 31 5d 29 5b 30 5d 3d 3d 27 5c 30 27 20 29 7b 0a  1])[0]=='\0' ){.
36c02 20 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f      generateErro
36c03 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d  r(pContext, "dum
36c04 70 5f 64 6f 63 6c 69 73 74 22 2c 20 22 65 6d 70  p_doclist", "emp
36c05 74 79 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  ty second argume
36c06 6e 74 22 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  nt");.  }else{. 
36c07 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
36c08 54 65 72 6d 20 3d 20 28 63 6f 6e 73 74 20 63 68  Term = (const ch
36c09 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
36c0a 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29  ue_text(argv[1])
36c0b 3b 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20  ;.    const int 
36c0c 6e 54 65 72 6d 20 3d 20 73 74 72 6c 65 6e 28 70  nTerm = strlen(p
36c0d 54 65 72 6d 29 3b 0a 20 20 20 20 66 75 6c 6c 74  Term);.    fullt
36c0e 65 78 74 5f 76 74 61 62 20 2a 76 3b 0a 20 20 20  ext_vtab *v;.   
36c0f 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 44 61 74   int rc;.    Dat
36c10 61 42 75 66 66 65 72 20 64 6f 63 6c 69 73 74 3b  aBuffer doclist;
36c11 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 43  ..    memcpy(&pC
36c12 75 72 73 6f 72 2c 20 73 71 6c 69 74 65 33 5f 76  ursor, sqlite3_v
36c13 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30  alue_blob(argv[0
36c14 5d 29 2c 20 73 69 7a 65 6f 66 28 70 43 75 72 73  ]), sizeof(pCurs
36c15 6f 72 29 29 3b 0a 20 20 20 20 76 20 3d 20 63 75  or));.    v = cu
36c16 72 73 6f 72 5f 76 74 61 62 28 70 43 75 72 73 6f  rsor_vtab(pCurso
36c17 72 29 3b 0a 0a 20 20 20 20 64 61 74 61 42 75 66  r);..    dataBuf
36c18 66 65 72 49 6e 69 74 28 26 64 6f 63 6c 69 73 74  ferInit(&doclist
36c19 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 74 65  , 0);..    /* te
36c1a 72 6d 53 65 6c 65 63 74 28 29 20 79 69 65 6c 64  rmSelect() yield
36c1b 73 20 74 68 65 20 73 61 6d 65 20 6c 6f 67 69 63  s the same logic
36c1c 61 6c 20 64 6f 63 6c 69 73 74 20 74 68 61 74 20  al doclist that 
36c1d 71 75 65 72 69 65 73 20 61 72 65 0a 20 20 20 20  queries are.    
36c1e 2a 2a 20 72 75 6e 20 61 67 61 69 6e 73 74 2e 0a  ** run against..
36c1f 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 61      */.    if( a
36c20 72 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20  rgc==2 ){.      
36c21 72 63 20 3d 20 74 65 72 6d 53 65 6c 65 63 74 28  rc = termSelect(
36c22 76 2c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 70  v, v->nColumn, p
36c23 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 30 2c 20  Term, nTerm, 0, 
36c24 44 4c 5f 44 45 46 41 55 4c 54 2c 20 26 64 6f 63  DL_DEFAULT, &doc
36c25 6c 69 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  list);.    }else
36c26 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
36c27 73 74 6d 74 20 2a 73 20 3d 20 4e 55 4c 4c 3b 0a  stmt *s = NULL;.
36c28 0a 20 20 20 20 20 20 2f 2a 20 47 65 74 20 6f 75  .      /* Get ou
36c29 72 20 73 70 65 63 69 66 69 63 20 73 65 67 6d 65  r specific segme
36c2a 6e 74 27 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  nt's information
36c2b 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20  . */.      rc = 
36c2c 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e  sql_get_statemen
36c2d 74 28 76 2c 20 53 45 47 44 49 52 5f 53 45 4c 45  t(v, SEGDIR_SELE
36c2e 43 54 5f 53 45 47 4d 45 4e 54 5f 53 54 4d 54 2c  CT_SEGMENT_STMT,
36c2f 20 26 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20   &s);.      if( 
36c30 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
36c31 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
36c32 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73  lite3_bind_int(s
36c33 2c 20 31 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  , 1, sqlite3_val
36c34 75 65 5f 69 6e 74 28 61 72 67 76 5b 32 5d 29 29  ue_int(argv[2]))
36c35 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
36c36 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
36c37 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
36c38 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73  lite3_bind_int(s
36c39 2c 20 32 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  , 2, sqlite3_val
36c3a 75 65 5f 69 6e 74 28 61 72 67 76 5b 33 5d 29 29  ue_int(argv[3]))
36c3b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
36c3c 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 72    }..      if( r
36c3d 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
36c3e 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
36c3f 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 0a 20  ite3_step(s);.. 
36c40 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
36c41 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20  QLITE_DONE ){.  
36c42 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66          dataBuff
36c43 65 72 44 65 73 74 72 6f 79 28 26 64 6f 63 6c 69  erDestroy(&docli
36c44 73 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67  st);.          g
36c45 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f  enerateError(pCo
36c46 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63  ntext, "dump_doc
36c47 6c 69 73 74 22 2c 20 22 73 65 67 6d 65 6e 74 20  list", "segment 
36c48 6e 6f 74 20 66 6f 75 6e 64 22 29 3b 0a 20 20 20  not found");.   
36c49 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
36c4a 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
36c4b 20 20 2f 2a 20 46 6f 75 6e 64 20 61 20 73 65 67    /* Found a seg
36c4c 6d 65 6e 74 2c 20 6c 6f 61 64 20 69 74 20 69 6e  ment, load it in
36c4d 74 6f 20 64 6f 63 6c 69 73 74 2e 20 2a 2f 0a 20  to doclist. */. 
36c4e 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
36c4f 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
36c50 20 20 20 20 20 20 20 63 6f 6e 73 74 20 73 71 6c         const sql
36c51 69 74 65 5f 69 6e 74 36 34 20 69 4c 65 61 76 65  ite_int64 iLeave
36c52 73 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 5f 63  sEnd = sqlite3_c
36c53 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31  olumn_int64(s, 1
36c54 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e  );.          con
36c55 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 20 3d  st char *pData =
36c56 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
36c57 62 6c 6f 62 28 73 2c 20 32 29 3b 0a 20 20 20 20  blob(s, 2);.    
36c58 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20        const int 
36c59 6e 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 5f  nData = sqlite3_
36c5a 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 2c 20  column_bytes(s, 
36c5b 32 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f  2);..          /
36c5c 2a 20 6c 6f 61 64 53 65 67 6d 65 6e 74 28 29 20  * loadSegment() 
36c5d 69 73 20 75 73 65 64 20 62 79 20 74 65 72 6d 53  is used by termS
36c5e 65 6c 65 63 74 28 29 20 74 6f 20 6c 6f 61 64 20  elect() to load 
36c5f 65 61 63 68 0a 20 20 20 20 20 20 20 20 20 20 2a  each.          *
36c60 2a 20 73 65 67 6d 65 6e 74 27 73 20 64 61 74 61  * segment's data
36c61 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
36c62 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c 6f           rc = lo
36c63 61 64 53 65 67 6d 65 6e 74 28 76 2c 20 70 44 61  adSegment(v, pDa
36c64 74 61 2c 20 6e 44 61 74 61 2c 20 69 4c 65 61 76  ta, nData, iLeav
36c65 65 73 45 6e 64 2c 20 70 54 65 72 6d 2c 20 6e 54  esEnd, pTerm, nT
36c66 65 72 6d 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  erm, 0,.        
36c67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36c68 20 20 20 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20     &doclist);.  
36c69 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
36c6a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
36c6b 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
36c6c 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 0a  lite3_step(s);..
36c6d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
36c6e 68 6f 75 6c 64 20 6e 6f 74 20 68 61 76 65 20 6d  hould not have m
36c6f 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 6d 61 74  ore than one mat
36c70 63 68 69 6e 67 20 73 65 67 6d 65 6e 74 2e 20 2a  ching segment. *
36c71 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  /.            if
36c72 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc!=SQLITE_DON
36c73 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  E ){.           
36c74 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74     sqlite3_reset
36c75 28 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  (s);.           
36c76 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73     dataBufferDes
36c77 74 72 6f 79 28 26 64 6f 63 6c 69 73 74 29 3b 0a  troy(&doclist);.
36c78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 67 65                ge
36c79 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e  nerateError(pCon
36c7a 74 65 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63 6c  text, "dump_docl
36c7b 69 73 74 22 2c 20 22 69 6e 76 61 6c 69 64 20 73  ist", "invalid s
36c7c 65 67 64 69 72 22 29 3b 0a 20 20 20 20 20 20 20  egdir");.       
36c7d 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
36c7e 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
36c7f 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
36c80 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20  LITE_OK;.       
36c81 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
36c82 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 73 71       }..      sq
36c83 6c 69 74 65 33 5f 72 65 73 65 74 28 73 29 3b 0a  lite3_reset(s);.
36c84 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72      }..    if( r
36c85 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
36c86 20 20 20 20 20 20 69 66 28 20 64 6f 63 6c 69 73        if( doclis
36c87 74 2e 6e 44 61 74 61 3e 30 20 29 7b 0a 20 20 20  t.nData>0 ){.   
36c88 20 20 20 20 20 63 72 65 61 74 65 44 6f 63 6c 69       createDocli
36c89 73 74 52 65 73 75 6c 74 28 70 43 6f 6e 74 65 78  stResult(pContex
36c8a 74 2c 20 64 6f 63 6c 69 73 74 2e 70 44 61 74 61  t, doclist.pData
36c8b 2c 20 64 6f 63 6c 69 73 74 2e 6e 44 61 74 61 29  , doclist.nData)
36c8c 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
36c8d 20 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73         /* TODO(s
36c8e 68 65 73 73 29 3a 20 54 68 69 73 20 63 61 6e 20  hess): This can 
36c8f 68 61 70 70 65 6e 20 69 66 20 74 68 65 20 74 65  happen if the te
36c90 72 6d 20 69 73 20 6e 6f 74 20 70 72 65 73 65 6e  rm is not presen
36c91 74 2c 20 6f 72 0a 20 20 20 20 20 20 20 20 2a 2a  t, or.        **
36c92 20 69 66 20 61 6c 6c 20 69 6e 73 74 61 6e 63 65   if all instance
36c93 73 20 6f 66 20 74 68 65 20 74 65 72 6d 20 68 61  s of the term ha
36c94 76 65 20 62 65 65 6e 20 64 65 6c 65 74 65 64 20  ve been deleted 
36c95 61 6e 64 20 74 68 69 73 20 69 73 0a 20 20 20 20  and this is.    
36c96 20 20 20 20 2a 2a 20 61 6e 20 61 6c 6c 2d 69 6e      ** an all-in
36c97 64 65 78 20 64 75 6d 70 2e 20 20 49 74 20 6d 61  dex dump.  It ma
36c98 79 20 62 65 20 69 6e 74 65 72 65 73 74 69 6e 67  y be interesting
36c99 20 74 6f 20 64 69 73 74 69 6e 67 75 69 73 68 0a   to distinguish.
36c9a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 73 65          ** these
36c9b 20 63 61 73 65 73 2e 0a 20 20 20 20 20 20 20 20   cases..        
36c9c 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  */.        sqlit
36c9d 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70  e3_result_text(p
36c9e 43 6f 6e 74 65 78 74 2c 20 22 22 2c 20 30 2c 20  Context, "", 0, 
36c9f 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
36ca0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
36ca1 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45  e if( rc==SQLITE
36ca2 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20  _NOMEM ){.      
36ca3 2f 2a 20 48 61 6e 64 6c 65 20 6f 75 74 2d 6f 66  /* Handle out-of
36ca4 2d 6d 65 6d 6f 72 79 20 63 61 73 65 73 20 73 70  -memory cases sp
36ca5 65 63 69 61 6c 6c 79 20 62 65 63 61 75 73 65 20  ecially because 
36ca6 69 66 20 74 68 65 79 20 61 72 65 0a 20 20 20 20  if they are.    
36ca7 20 20 2a 2a 20 67 65 6e 65 72 61 74 65 64 20 69    ** generated i
36ca8 6e 20 66 74 73 33 20 63 6f 64 65 20 74 68 65 79  n fts3 code they
36ca9 20 6d 61 79 20 6e 6f 74 20 62 65 20 72 65 66 6c   may not be refl
36caa 65 63 74 65 64 20 69 6e 20 74 68 65 20 64 62 0a  ected in the db.
36cab 20 20 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 2e        ** handle.
36cac 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
36cad 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20  /* TODO(shess): 
36cae 48 61 6e 64 6c 65 20 74 68 69 73 20 6d 6f 72 65  Handle this more
36caf 20 63 6f 6d 70 72 65 68 65 6e 73 69 76 65 6c 79   comprehensively
36cb0 2e 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74  ..      ** sqlit
36cb1 65 33 45 72 72 53 74 72 28 29 20 68 61 73 20 77  e3ErrStr() has w
36cb2 68 61 74 20 49 20 6e 65 65 64 2c 20 62 75 74 20  hat I need, but 
36cb3 69 73 20 69 6e 74 65 72 6e 61 6c 2e 0a 20 20 20  is internal..   
36cb4 20 20 20 2a 2f 0a 20 20 20 20 20 20 67 65 6e 65     */.      gene
36cb5 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65  rateError(pConte
36cb6 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73  xt, "dump_doclis
36cb7 74 22 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f  t", "out of memo
36cb8 72 79 22 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ry");.    }else{
36cb9 0a 20 20 20 20 20 20 67 65 6e 65 72 61 74 65 45  .      generateE
36cba 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22  rror(pContext, "
36cbb 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20 4e  dump_doclist", N
36cbc 55 4c 4c 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ULL);.    }..   
36cbd 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72   dataBufferDestr
36cbe 6f 79 28 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20  oy(&doclist);.  
36cbf 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  }.}.#endif../*.*
36cc0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
36cc1 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 78 46  mplements the xF
36cc2 69 6e 64 46 75 6e 63 74 69 6f 6e 20 6d 65 74 68  indFunction meth
36cc3 6f 64 20 66 6f 72 20 74 68 65 20 46 54 53 33 0a  od for the FTS3.
36cc4 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ** virtual table
36cc5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
36cc6 66 75 6c 6c 74 65 78 74 46 69 6e 64 46 75 6e 63  fulltextFindFunc
36cc7 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f  tion(.  sqlite3_
36cc8 76 74 61 62 20 2a 70 56 74 61 62 2c 0a 20 20 69  vtab *pVtab,.  i
36cc9 6e 74 20 6e 41 72 67 2c 0a 20 20 63 6f 6e 73 74  nt nArg,.  const
36cca 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20   char *zName,.  
36ccb 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29 28  void (**pxFunc)(
36ccc 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
36ccd 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
36cce 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 2a 2a  ue**),.  void **
36ccf 70 70 41 72 67 0a 29 7b 0a 20 20 69 66 28 20 73  ppArg.){.  if( s
36cd0 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 22 73 6e 69  trcmp(zName,"sni
36cd1 70 70 65 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20  ppet")==0 ){.   
36cd2 20 2a 70 78 46 75 6e 63 20 3d 20 73 6e 69 70 70   *pxFunc = snipp
36cd3 65 74 46 75 6e 63 3b 0a 20 20 20 20 72 65 74 75  etFunc;.    retu
36cd4 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66  rn 1;.  }else if
36cd5 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 22  ( strcmp(zName,"
36cd6 6f 66 66 73 65 74 73 22 29 3d 3d 30 20 29 7b 0a  offsets")==0 ){.
36cd7 20 20 20 20 2a 70 78 46 75 6e 63 20 3d 20 73 6e      *pxFunc = sn
36cd8 69 70 70 65 74 4f 66 66 73 65 74 73 46 75 6e 63  ippetOffsetsFunc
36cd9 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
36cda 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63    }else if( strc
36cdb 6d 70 28 7a 4e 61 6d 65 2c 22 6f 70 74 69 6d 69  mp(zName,"optimi
36cdc 7a 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2a  ze")==0 ){.    *
36cdd 70 78 46 75 6e 63 20 3d 20 6f 70 74 69 6d 69 7a  pxFunc = optimiz
36cde 65 46 75 6e 63 3b 0a 20 20 20 20 72 65 74 75 72  eFunc;.    retur
36cdf 6e 20 31 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  n 1;.#ifdef SQLI
36ce0 54 45 5f 54 45 53 54 0a 20 20 20 20 2f 2a 20 4e  TE_TEST.    /* N
36ce1 4f 54 45 28 73 68 65 73 73 29 3a 20 54 68 65 73  OTE(shess): Thes
36ce2 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  e functions are 
36ce3 70 72 65 73 65 6e 74 20 6f 6e 6c 79 20 66 6f 72  present only for
36ce4 20 74 65 73 74 69 6e 67 0a 20 20 20 20 2a 2a 20   testing.    ** 
36ce5 70 75 72 70 6f 73 65 73 2e 20 20 4e 6f 20 70 61  purposes.  No pa
36ce6 72 74 69 63 75 6c 61 72 20 65 66 66 6f 72 74 20  rticular effort 
36ce7 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 74 69 6d  is made to optim
36ce8 69 7a 65 20 74 68 65 69 72 0a 20 20 20 20 2a 2a  ize their.    **
36ce9 20 65 78 65 63 75 74 69 6f 6e 20 6f 72 20 68 6f   execution or ho
36cea 77 20 74 68 65 79 20 62 75 69 6c 64 20 74 68 65  w they build the
36ceb 69 72 20 72 65 73 75 6c 74 73 2e 0a 20 20 20 20  ir results..    
36cec 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  */.  }else if( s
36ced 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 22 64 75 6d  trcmp(zName,"dum
36cee 70 5f 74 65 72 6d 73 22 29 3d 3d 30 20 29 7b 0a  p_terms")==0 ){.
36cef 20 20 20 20 2f 2a 20 66 70 72 69 6e 74 66 28 73      /* fprintf(s
36cf0 74 64 65 72 72 2c 20 22 46 6f 75 6e 64 20 64 75  tderr, "Found du
36cf1 6d 70 5f 74 65 72 6d 73 5c 6e 22 29 3b 20 2a 2f  mp_terms\n"); */
36cf2 0a 20 20 20 20 2a 70 78 46 75 6e 63 20 3d 20 64  .    *pxFunc = d
36cf3 75 6d 70 54 65 72 6d 73 46 75 6e 63 3b 0a 20 20  umpTermsFunc;.  
36cf4 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65    return 1;.  }e
36cf5 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a  lse if( strcmp(z
36cf6 4e 61 6d 65 2c 22 64 75 6d 70 5f 64 6f 63 6c 69  Name,"dump_docli
36cf7 73 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2f  st")==0 ){.    /
36cf8 2a 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72  * fprintf(stderr
36cf9 2c 20 22 46 6f 75 6e 64 20 64 75 6d 70 5f 64 6f  , "Found dump_do
36cfa 63 6c 69 73 74 5c 6e 22 29 3b 20 2a 2f 0a 20 20  clist\n"); */.  
36cfb 20 20 2a 70 78 46 75 6e 63 20 3d 20 64 75 6d 70    *pxFunc = dump
36cfc 44 6f 63 6c 69 73 74 46 75 6e 63 3b 0a 20 20 20  DoclistFunc;.   
36cfd 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69   return 1;.#endi
36cfe 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  f.  }.  return 0
36cff 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6e 61 6d  ;.}../*.** Renam
36d00 65 20 61 6e 20 66 74 73 33 20 74 61 62 6c 65 2e  e an fts3 table.
36d01 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
36d02 75 6c 6c 74 65 78 74 52 65 6e 61 6d 65 28 0a 20  ulltextRename(. 
36d03 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
36d04 56 74 61 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68  Vtab,.  const ch
36d05 61 72 20 2a 7a 4e 61 6d 65 0a 29 7b 0a 20 20 66  ar *zName.){.  f
36d06 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 70 20  ulltext_vtab *p 
36d07 3d 20 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  = (fulltext_vtab
36d08 20 2a 29 70 56 74 61 62 3b 0a 20 20 69 6e 74 20   *)pVtab;.  int 
36d09 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
36d0a 4d 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 20  M;.  char *zSql 
36d0b 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
36d0c 66 28 0a 20 20 20 20 22 41 4c 54 45 52 20 54 41  f(.    "ALTER TA
36d0d 42 4c 45 20 25 51 2e 27 25 71 5f 63 6f 6e 74 65  BLE %Q.'%q_conte
36d0e 6e 74 27 20 20 52 45 4e 41 4d 45 20 54 4f 20 27  nt'  RENAME TO '
36d0f 25 71 5f 63 6f 6e 74 65 6e 74 27 3b 22 0a 20 20  %q_content';".  
36d10 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 25    "ALTER TABLE %
36d11 51 2e 27 25 71 5f 73 65 67 6d 65 6e 74 73 27 20  Q.'%q_segments' 
36d12 52 45 4e 41 4d 45 20 54 4f 20 27 25 71 5f 73 65  RENAME TO '%q_se
36d13 67 6d 65 6e 74 73 27 3b 22 0a 20 20 20 20 22 41  gments';".    "A
36d14 4c 54 45 52 20 54 41 42 4c 45 20 25 51 2e 27 25  LTER TABLE %Q.'%
36d15 71 5f 73 65 67 64 69 72 27 20 20 20 52 45 4e 41  q_segdir'   RENA
36d16 4d 45 20 54 4f 20 27 25 71 5f 73 65 67 64 69 72  ME TO '%q_segdir
36d17 27 3b 22 0a 20 20 20 20 2c 20 70 2d 3e 7a 44 62  ';".    , p->zDb
36d18 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d  , p->zName, zNam
36d19 65 20 0a 20 20 20 20 2c 20 70 2d 3e 7a 44 62 2c  e .    , p->zDb,
36d1a 20 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65   p->zName, zName
36d1b 20 0a 20 20 20 20 2c 20 70 2d 3e 7a 44 62 2c 20   .    , p->zDb, 
36d1c 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 0a  p->zName, zName.
36d1d 20 20 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 20    );.  if( zSql 
36d1e 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
36d1f 74 65 33 5f 65 78 65 63 28 70 2d 3e 64 62 2c 20  te3_exec(p->db, 
36d20 7a 53 71 6c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  zSql, 0, 0, 0);.
36d21 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
36d22 28 7a 53 71 6c 29 3b 0a 20 20 7d 0a 20 20 72 65  (zSql);.  }.  re
36d23 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74  turn rc;.}..stat
36d24 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
36d25 5f 6d 6f 64 75 6c 65 20 66 74 73 33 4d 6f 64 75  _module fts3Modu
36d26 6c 65 20 3d 20 7b 0a 20 20 2f 2a 20 69 56 65 72  le = {.  /* iVer
36d27 73 69 6f 6e 20 20 20 20 20 20 2a 2f 20 30 2c 0a  sion      */ 0,.
36d28 20 20 2f 2a 20 78 43 72 65 61 74 65 20 20 20 20    /* xCreate    
36d29 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 43 72     */ fulltextCr
36d2a 65 61 74 65 2c 0a 20 20 2f 2a 20 78 43 6f 6e 6e  eate,.  /* xConn
36d2b 65 63 74 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c  ect      */ full
36d2c 74 65 78 74 43 6f 6e 6e 65 63 74 2c 0a 20 20 2f  textConnect,.  /
36d2d 2a 20 78 42 65 73 74 49 6e 64 65 78 20 20 20 20  * xBestIndex    
36d2e 2a 2f 20 66 75 6c 6c 74 65 78 74 42 65 73 74 49  */ fulltextBestI
36d2f 6e 64 65 78 2c 0a 20 20 2f 2a 20 78 44 69 73 63  ndex,.  /* xDisc
36d30 6f 6e 6e 65 63 74 20 20 20 2a 2f 20 66 75 6c 6c  onnect   */ full
36d31 74 65 78 74 44 69 73 63 6f 6e 6e 65 63 74 2c 0a  textDisconnect,.
36d32 20 20 2f 2a 20 78 44 65 73 74 72 6f 79 20 20 20    /* xDestroy   
36d33 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 44 65     */ fulltextDe
36d34 73 74 72 6f 79 2c 0a 20 20 2f 2a 20 78 4f 70 65  stroy,.  /* xOpe
36d35 6e 20 20 20 20 20 20 20 20 20 2a 2f 20 66 75 6c  n         */ ful
36d36 6c 74 65 78 74 4f 70 65 6e 2c 0a 20 20 2f 2a 20  ltextOpen,.  /* 
36d37 78 43 6c 6f 73 65 20 20 20 20 20 20 20 20 2a 2f  xClose        */
36d38 20 66 75 6c 6c 74 65 78 74 43 6c 6f 73 65 2c 0a   fulltextClose,.
36d39 20 20 2f 2a 20 78 46 69 6c 74 65 72 20 20 20 20    /* xFilter    
36d3a 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 46 69     */ fulltextFi
36d3b 6c 74 65 72 2c 0a 20 20 2f 2a 20 78 4e 65 78 74  lter,.  /* xNext
36d3c 20 20 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c           */ full
36d3d 74 65 78 74 4e 65 78 74 2c 0a 20 20 2f 2a 20 78  textNext,.  /* x
36d3e 45 6f 66 20 20 20 20 20 20 20 20 20 20 2a 2f 20  Eof          */ 
36d3f 66 75 6c 6c 74 65 78 74 45 6f 66 2c 0a 20 20 2f  fulltextEof,.  /
36d40 2a 20 78 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20  * xColumn       
36d41 2a 2f 20 66 75 6c 6c 74 65 78 74 43 6f 6c 75 6d  */ fulltextColum
36d42 6e 2c 0a 20 20 2f 2a 20 78 52 6f 77 69 64 20 20  n,.  /* xRowid  
36d43 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78        */ fulltex
36d44 74 52 6f 77 69 64 2c 0a 20 20 2f 2a 20 78 55 70  tRowid,.  /* xUp
36d45 64 61 74 65 20 20 20 20 20 20 20 2a 2f 20 66 75  date       */ fu
36d46 6c 6c 74 65 78 74 55 70 64 61 74 65 2c 0a 20 20  lltextUpdate,.  
36d47 2f 2a 20 78 42 65 67 69 6e 20 20 20 20 20 20 20  /* xBegin       
36d48 20 2a 2f 20 66 75 6c 6c 74 65 78 74 42 65 67 69   */ fulltextBegi
36d49 6e 2c 0a 20 20 2f 2a 20 78 53 79 6e 63 20 20 20  n,.  /* xSync   
36d4a 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78        */ fulltex
36d4b 74 53 79 6e 63 2c 0a 20 20 2f 2a 20 78 43 6f 6d  tSync,.  /* xCom
36d4c 6d 69 74 20 20 20 20 20 20 20 2a 2f 20 66 75 6c  mit       */ ful
36d4d 6c 74 65 78 74 43 6f 6d 6d 69 74 2c 0a 20 20 2f  ltextCommit,.  /
36d4e 2a 20 78 52 6f 6c 6c 62 61 63 6b 20 20 20 20 20  * xRollback     
36d4f 2a 2f 20 66 75 6c 6c 74 65 78 74 52 6f 6c 6c 62  */ fulltextRollb
36d50 61 63 6b 2c 0a 20 20 2f 2a 20 78 46 69 6e 64 46  ack,.  /* xFindF
36d51 75 6e 63 74 69 6f 6e 20 2a 2f 20 66 75 6c 6c 74  unction */ fullt
36d52 65 78 74 46 69 6e 64 46 75 6e 63 74 69 6f 6e 2c  extFindFunction,
36d53 0a 20 20 2f 2a 20 78 52 65 6e 61 6d 65 20 2a 2f  .  /* xRename */
36d54 20 20 20 20 20 20 20 66 75 6c 6c 74 65 78 74 52         fulltextR
36d55 65 6e 61 6d 65 2c 0a 7d 3b 0a 0a 73 74 61 74 69  ename,.};..stati
36d56 63 20 76 6f 69 64 20 68 61 73 68 44 65 73 74 72  c void hashDestr
36d57 6f 79 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 66  oy(void *p){.  f
36d58 74 73 33 48 61 73 68 20 2a 70 48 61 73 68 20 3d  ts3Hash *pHash =
36d59 20 28 66 74 73 33 48 61 73 68 20 2a 29 70 3b 0a   (fts3Hash *)p;.
36d5a 20 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73    sqlite3Fts3Has
36d5b 68 43 6c 65 61 72 28 70 48 61 73 68 29 3b 0a 20  hClear(pHash);. 
36d5c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48   sqlite3_free(pH
36d5d 61 73 68 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ash);.}../*.** T
36d5e 68 65 20 66 74 73 33 20 62 75 69 6c 74 2d 69 6e  he fts3 built-in
36d5f 20 74 6f 6b 65 6e 69 7a 65 72 73 20 2d 20 22 73   tokenizers - "s
36d60 69 6d 70 6c 65 22 20 61 6e 64 20 22 70 6f 72 74  imple" and "port
36d61 65 72 22 20 2d 20 61 72 65 20 69 6d 70 6c 65 6d  er" - are implem
36d62 65 6e 74 65 64 0a 2a 2a 20 69 6e 20 66 69 6c 65  ented.** in file
36d63 73 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72  s fts3_tokenizer
36d64 31 2e 63 20 61 6e 64 20 66 74 73 33 5f 70 6f 72  1.c and fts3_por
36d65 74 65 72 2e 63 20 72 65 73 70 65 63 74 69 76 65  ter.c respective
36d66 6c 79 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  ly. The followin
36d67 67 0a 2a 2a 20 74 77 6f 20 66 6f 72 77 61 72 64  g.** two forward
36d68 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 61 72   declarations ar
36d69 65 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 73 20  e for functions 
36d6a 64 65 63 6c 61 72 65 64 20 69 6e 20 74 68 65 73  declared in thes
36d6b 65 20 66 69 6c 65 73 0a 2a 2a 20 75 73 65 64 20  e files.** used 
36d6c 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 20  to retrieve the 
36d6d 72 65 73 70 65 63 74 69 76 65 20 69 6d 70 6c 65  respective imple
36d6e 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  mentations..**.*
36d6f 2a 20 43 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65  * Calling sqlite
36d70 33 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e  3Fts3SimpleToken
36d71 69 7a 65 72 4d 6f 64 75 6c 65 28 29 20 73 65 74  izerModule() set
36d72 73 20 74 68 65 20 76 61 6c 75 65 20 70 6f 69 6e  s the value poin
36d73 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 74 68 65  ted.** to by the
36d74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 70 6f 69   argument to poi
36d75 6e 74 20 61 20 74 68 65 20 22 73 69 6d 70 6c 65  nt a the "simple
36d76 22 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6d 70 6c  " tokenizer impl
36d77 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 46  ementation..** F
36d78 75 6e 63 74 69 6f 6e 20 2e 2e 2e 50 6f 72 74 65  unction ...Porte
36d79 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  rTokenizerModule
36d7a 28 29 20 73 65 74 73 20 2a 70 4d 6f 64 75 6c 65  () sets *pModule
36d7b 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   to point to the
36d7c 0a 2a 2a 20 70 6f 72 74 65 72 20 74 6f 6b 65 6e  .** porter token
36d7d 69 7a 65 72 2f 73 74 65 6d 6d 65 72 20 69 6d 70  izer/stemmer imp
36d7e 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2f 0a  lementation..*/.
36d7f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
36d80 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 53  oid sqlite3Fts3S
36d81 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f  impleTokenizerMo
36d82 64 75 6c 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b  dule(sqlite3_tok
36d83 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f  enizer_module co
36d84 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a  nst**ppModule);.
36d85 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
36d86 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 50  oid sqlite3Fts3P
36d87 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f  orterTokenizerMo
36d88 64 75 6c 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b  dule(sqlite3_tok
36d89 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f  enizer_module co
36d8a 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a  nst**ppModule);.
36d8b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
36d8c 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 49  oid sqlite3Fts3I
36d8d 63 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  cuTokenizerModul
36d8e 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e(sqlite3_tokeni
36d8f 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74  zer_module const
36d90 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a 0a 53 51  **ppModule);..SQ
36d91 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
36d92 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74   sqlite3Fts3Init
36d93 48 61 73 68 54 61 62 6c 65 28 73 71 6c 69 74 65  HashTable(sqlite
36d94 33 20 2a 2c 20 66 74 73 33 48 61 73 68 20 2a 2c  3 *, fts3Hash *,
36d95 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a   const char *);.
36d96 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 73  ./*.** Initialis
36d97 65 20 74 68 65 20 66 74 73 33 20 65 78 74 65 6e  e the fts3 exten
36d98 73 69 6f 6e 2e 20 49 66 20 74 68 69 73 20 65 78  sion. If this ex
36d99 74 65 6e 73 69 6f 6e 20 69 73 20 62 75 69 6c 74  tension is built
36d9a 20 61 73 20 70 61 72 74 0a 2a 2a 20 6f 66 20 74   as part.** of t
36d9b 68 65 20 73 71 6c 69 74 65 20 6c 69 62 72 61 72  he sqlite librar
36d9c 79 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e  y, then this fun
36d9d 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
36d9e 64 69 72 65 63 74 6c 79 20 62 79 0a 2a 2a 20 53  directly by.** S
36d9f 51 4c 69 74 65 2e 20 49 66 20 66 74 73 33 20 69  QLite. If fts3 i
36da0 73 20 62 75 69 6c 74 20 61 73 20 61 20 64 79 6e  s built as a dyn
36da1 61 6d 69 63 61 6c 6c 79 20 6c 6f 61 64 61 62 6c  amically loadabl
36da2 65 20 65 78 74 65 6e 73 69 6f 6e 2c 20 74 68 69  e extension, thi
36da3 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73  s.** function is
36da4 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 73   called by the s
36da5 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e  qlite3_extension
36da6 5f 69 6e 69 74 28 29 20 65 6e 74 72 79 20 70 6f  _init() entry po
36da7 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  int..*/.SQLITE_P
36da8 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
36da9 65 33 46 74 73 33 49 6e 69 74 28 73 71 6c 69 74  e3Fts3Init(sqlit
36daa 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 72  e3 *db){.  int r
36dab 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
36dac 20 66 74 73 33 48 61 73 68 20 2a 70 48 61 73 68   fts3Hash *pHash
36dad 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71   = 0;.  const sq
36dae 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
36daf 6d 6f 64 75 6c 65 20 2a 70 53 69 6d 70 6c 65 20  module *pSimple 
36db0 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c  = 0;.  const sql
36db1 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
36db2 6f 64 75 6c 65 20 2a 70 50 6f 72 74 65 72 20 3d  odule *pPorter =
36db3 20 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69   0;.  const sqli
36db4 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
36db5 64 75 6c 65 20 2a 70 49 63 75 20 3d 20 30 3b 0a  dule *pIcu = 0;.
36db6 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33 53 69  .  sqlite3Fts3Si
36db7 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64  mpleTokenizerMod
36db8 75 6c 65 28 26 70 53 69 6d 70 6c 65 29 3b 0a 20  ule(&pSimple);. 
36db9 20 73 71 6c 69 74 65 33 46 74 73 33 50 6f 72 74   sqlite3Fts3Port
36dba 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  erTokenizerModul
36dbb 65 28 26 70 50 6f 72 74 65 72 29 3b 0a 23 69 66  e(&pPorter);.#if
36dbc 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
36dbd 45 5f 49 43 55 0a 20 20 73 71 6c 69 74 65 33 46  E_ICU.  sqlite3F
36dbe 74 73 33 49 63 75 54 6f 6b 65 6e 69 7a 65 72 4d  ts3IcuTokenizerM
36dbf 6f 64 75 6c 65 28 26 70 49 63 75 29 3b 0a 23 65  odule(&pIcu);.#e
36dc0 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  ndif..  /* Alloc
36dc1 61 74 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69  ate and initiali
36dc2 73 65 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c  se the hash-tabl
36dc3 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  e used to store 
36dc4 74 6f 6b 65 6e 69 7a 65 72 73 2e 20 2a 2f 0a 20  tokenizers. */. 
36dc5 20 70 48 61 73 68 20 3d 20 73 71 6c 69 74 65 33   pHash = sqlite3
36dc6 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 66  _malloc(sizeof(f
36dc7 74 73 33 48 61 73 68 29 29 3b 0a 20 20 69 66 28  ts3Hash));.  if(
36dc8 20 21 70 48 61 73 68 20 29 7b 0a 20 20 20 20 72   !pHash ){.    r
36dc9 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
36dca 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
36dcb 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e  qlite3Fts3HashIn
36dcc 69 74 28 70 48 61 73 68 2c 20 46 54 53 33 5f 48  it(pHash, FTS3_H
36dcd 41 53 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a  ASH_STRING, 1);.
36dce 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 74    }..  /* Load t
36dcf 68 65 20 62 75 69 6c 74 2d 69 6e 20 74 6f 6b 65  he built-in toke
36dd0 6e 69 7a 65 72 73 20 69 6e 74 6f 20 74 68 65 20  nizers into the 
36dd1 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20  hash table */.  
36dd2 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
36dd3 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  K ){.    if( sql
36dd4 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 73 65  ite3Fts3HashInse
36dd5 72 74 28 70 48 61 73 68 2c 20 22 73 69 6d 70 6c  rt(pHash, "simpl
36dd6 65 22 2c 20 37 2c 20 28 76 6f 69 64 20 2a 29 70  e", 7, (void *)p
36dd7 53 69 6d 70 6c 65 29 0a 20 20 20 20 20 7c 7c 20  Simple).     || 
36dd8 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49  sqlite3Fts3HashI
36dd9 6e 73 65 72 74 28 70 48 61 73 68 2c 20 22 70 6f  nsert(pHash, "po
36dda 72 74 65 72 22 2c 20 37 2c 20 28 76 6f 69 64 20  rter", 7, (void 
36ddb 2a 29 70 50 6f 72 74 65 72 29 20 0a 20 20 20 20  *)pPorter) .    
36ddc 20 7c 7c 20 28 70 49 63 75 20 26 26 20 73 71 6c   || (pIcu && sql
36ddd 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 73 65  ite3Fts3HashInse
36dde 72 74 28 70 48 61 73 68 2c 20 22 69 63 75 22 2c  rt(pHash, "icu",
36ddf 20 34 2c 20 28 76 6f 69 64 20 2a 29 70 49 63 75   4, (void *)pIcu
36de0 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  )).    ){.      
36de1 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
36de2 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 23 69  M;.    }.  }..#i
36de3 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
36de4 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78  .  sqlite3Fts3Ex
36de5 70 72 49 6e 69 74 54 65 73 74 49 6e 74 65 72 66  prInitTestInterf
36de6 61 63 65 28 64 62 29 3b 0a 23 65 6e 64 69 66 0a  ace(db);.#endif.
36de7 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65  .  /* Create the
36de8 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 77   virtual table w
36de9 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74 68  rapper around th
36dea 65 20 68 61 73 68 2d 74 61 62 6c 65 20 61 6e 64  e hash-table and
36deb 20 6f 76 65 72 6c 6f 61 64 20 0a 20 20 2a 2a 20   overload .  ** 
36dec 74 68 65 20 74 77 6f 20 73 63 61 6c 61 72 20 66  the two scalar f
36ded 75 6e 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 69  unctions. If thi
36dee 73 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c  s is successful,
36def 20 72 65 67 69 73 74 65 72 20 74 68 65 0a 20 20   register the.  
36df0 2a 2a 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 73  ** module with s
36df1 71 6c 69 74 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  qlite..  */.  if
36df2 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20  ( SQLITE_OK==rc 
36df3 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b  .   && SQLITE_OK
36df4 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 46  ==(rc = sqlite3F
36df5 74 73 33 49 6e 69 74 48 61 73 68 54 61 62 6c 65  ts3InitHashTable
36df6 28 64 62 2c 20 70 48 61 73 68 2c 20 22 66 74 73  (db, pHash, "fts
36df7 33 5f 74 6f 6b 65 6e 69 7a 65 72 22 29 29 0a 20  3_tokenizer")). 
36df8 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d    && SQLITE_OK==
36df9 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76  (rc = sqlite3_ov
36dfa 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28  erload_function(
36dfb 64 62 2c 20 22 73 6e 69 70 70 65 74 22 2c 20 2d  db, "snippet", -
36dfc 31 29 29 0a 20 20 20 26 26 20 53 51 4c 49 54 45  1)).   && SQLITE
36dfd 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74  _OK==(rc = sqlit
36dfe 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63  e3_overload_func
36dff 74 69 6f 6e 28 64 62 2c 20 22 6f 66 66 73 65 74  tion(db, "offset
36e00 73 22 2c 20 2d 31 29 29 0a 20 20 20 26 26 20 53  s", -1)).   && S
36e01 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20  QLITE_OK==(rc = 
36e02 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64  sqlite3_overload
36e03 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 6f  _function(db, "o
36e04 70 74 69 6d 69 7a 65 22 2c 20 2d 31 29 29 0a 23  ptimize", -1)).#
36e05 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
36e06 54 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f  T.   && SQLITE_O
36e07 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K==(rc = sqlite3
36e08 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69  _overload_functi
36e09 6f 6e 28 64 62 2c 20 22 64 75 6d 70 5f 74 65 72  on(db, "dump_ter
36e0a 6d 73 22 2c 20 2d 31 29 29 0a 20 20 20 26 26 20  ms", -1)).   && 
36e0b 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d  SQLITE_OK==(rc =
36e0c 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61   sqlite3_overloa
36e0d 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22  d_function(db, "
36e0e 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20 2d  dump_doclist", -
36e0f 31 29 29 0a 23 65 6e 64 69 66 0a 20 20 29 7b 0a  1)).#endif.  ){.
36e10 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
36e11 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
36e12 5f 76 32 28 0a 20 20 20 20 20 20 20 20 64 62 2c  _v2(.        db,
36e13 20 22 66 74 73 33 22 2c 20 26 66 74 73 33 4d 6f   "fts3", &fts3Mo
36e14 64 75 6c 65 2c 20 28 76 6f 69 64 20 2a 29 70 48  dule, (void *)pH
36e15 61 73 68 2c 20 68 61 73 68 44 65 73 74 72 6f 79  ash, hashDestroy
36e16 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f  .    );.  }..  /
36e17 2a 20 41 6e 20 65 72 72 6f 72 20 68 61 73 20 6f  * An error has o
36e18 63 63 75 72 72 65 64 2e 20 44 65 6c 65 74 65 20  ccurred. Delete 
36e19 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 61  the hash table a
36e1a 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72  nd return the er
36e1b 72 6f 72 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 61  ror code. */.  a
36e1c 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54  ssert( rc!=SQLIT
36e1d 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20 70 48  E_OK );.  if( pH
36e1e 61 73 68 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ash ){.    sqlit
36e1f 65 33 46 74 73 33 48 61 73 68 43 6c 65 61 72 28  e3Fts3HashClear(
36e20 70 48 61 73 68 29 3b 0a 20 20 20 20 73 71 6c 69  pHash);.    sqli
36e21 74 65 33 5f 66 72 65 65 28 70 48 61 73 68 29 3b  te3_free(pHash);
36e22 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
36e23 3b 0a 7d 0a 0a 23 69 66 20 21 53 51 4c 49 54 45  ;.}..#if !SQLITE
36e24 5f 43 4f 52 45 0a 53 51 4c 49 54 45 5f 41 50 49  _CORE.SQLITE_API
36e25 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74   int sqlite3_ext
36e26 65 6e 73 69 6f 6e 5f 69 6e 69 74 28 0a 20 20 73  ension_init(.  s
36e27 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 63  qlite3 *db, .  c
36e28 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c 0a  har **pzErrMsg,.
36e29 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
36e2a 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 2a 70 41  api_routines *pA
36e2b 70 69 0a 29 7b 0a 20 20 53 51 4c 49 54 45 5f 45  pi.){.  SQLITE_E
36e2c 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 32 28 70  XTENSION_INIT2(p
36e2d 41 70 69 29 0a 20 20 72 65 74 75 72 6e 20 73 71  Api).  return sq
36e2e 6c 69 74 65 33 46 74 73 33 49 6e 69 74 28 64 62  lite3Fts3Init(db
36e2f 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 65 6e  );.}.#endif..#en
36e30 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
36e31 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
36e32 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
36e33 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a  NABLE_FTS3) */..
36e34 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
36e35 45 6e 64 20 6f 66 20 66 74 73 33 2e 63 20 2a 2a  End of fts3.c **
36e36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e38 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
36e39 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
36e3a 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f  Begin file fts3_
36e3b 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  expr.c *********
36e3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
36e3e 2f 2a 0a 2a 2a 20 32 30 30 38 20 4e 6f 76 20 32  /*.** 2008 Nov 2
36e3f 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  8.**.** The auth
36e40 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
36e41 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
36e42 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
36e43 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
36e44 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
36e45 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
36e46 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
36e47 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
36e48 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
36e49 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
36e4a 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
36e4b 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
36e4c 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
36e4d 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
36e4e 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
36e4f 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
36e50 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
36e51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e55 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
36e56 20 6d 6f 64 75 6c 65 20 63 6f 6e 74 61 69 6e 73   module contains
36e57 20 63 6f 64 65 20 74 68 61 74 20 69 6d 70 6c 65   code that imple
36e58 6d 65 6e 74 73 20 61 20 70 61 72 73 65 72 20 66  ments a parser f
36e59 6f 72 20 66 74 73 33 20 71 75 65 72 79 20 73 74  or fts3 query st
36e5a 72 69 6e 67 73 0a 2a 2a 20 28 74 68 65 20 72 69  rings.** (the ri
36e5b 67 68 74 2d 68 61 6e 64 20 61 72 67 75 6d 65 6e  ght-hand argumen
36e5c 74 20 74 6f 20 74 68 65 20 4d 41 54 43 48 20 6f  t to the MATCH o
36e5d 70 65 72 61 74 6f 72 29 2e 20 42 65 63 61 75 73  perator). Becaus
36e5e 65 20 74 68 65 20 73 75 70 70 6f 72 74 65 64 20  e the supported 
36e5f 0a 2a 2a 20 73 79 6e 74 61 78 20 69 73 20 72 65  .** syntax is re
36e60 6c 61 74 69 76 65 6c 79 20 73 69 6d 70 6c 65 2c  latively simple,
36e61 20 74 68 65 20 77 68 6f 6c 65 20 74 6f 6b 65 6e   the whole token
36e62 69 7a 65 72 2f 70 61 72 73 65 72 20 73 79 73 74  izer/parser syst
36e63 65 6d 20 69 73 0a 2a 2a 20 68 61 6e 64 2d 63 6f  em is.** hand-co
36e64 64 65 64 2e 20 54 68 65 20 70 75 62 6c 69 63 20  ded. The public 
36e65 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 69  interface to thi
36e66 73 20 6d 6f 64 75 6c 65 20 69 73 20 64 65 63 6c  s module is decl
36e67 61 72 65 64 20 69 6e 20 73 6f 75 72 63 65 0a 2a  ared in source.*
36e68 2a 20 63 6f 64 65 20 66 69 6c 65 20 22 66 74 73  * code file "fts
36e69 33 5f 65 78 70 72 2e 68 22 2e 0a 2a 2f 0a 23 69  3_expr.h"..*/.#i
36e6a 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
36e6b 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
36e6c 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
36e6d 5f 46 54 53 33 29 0a 0a 2f 2a 0a 2a 2a 20 42 79  _FTS3)../*.** By
36e6e 20 64 65 66 61 75 6c 74 2c 20 74 68 69 73 20 6d   default, this m
36e6f 6f 64 75 6c 65 20 70 61 72 73 65 73 20 74 68 65  odule parses the
36e70 20 6c 65 67 61 63 79 20 73 79 6e 74 61 78 20 74   legacy syntax t
36e71 68 61 74 20 68 61 73 20 62 65 65 6e 20 0a 2a 2a  hat has been .**
36e72 20 74 72 61 64 69 74 69 6f 6e 61 6c 6c 79 20 75   traditionally u
36e73 73 65 64 20 62 79 20 66 74 73 33 2e 20 4f 72 2c  sed by fts3. Or,
36e74 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   if SQLITE_ENABL
36e75 45 5f 46 54 53 33 5f 50 41 52 45 4e 54 48 45 53  E_FTS3_PARENTHES
36e76 49 53 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64  IS.** is defined
36e77 2c 20 74 68 65 6e 20 69 74 20 75 73 65 73 20 74  , then it uses t
36e78 68 65 20 6e 65 77 20 73 79 6e 74 61 78 2e 20 54  he new syntax. T
36e79 68 65 20 64 69 66 66 65 72 65 6e 63 65 73 20 62  he differences b
36e7a 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20 6e 65  etween.** the ne
36e7b 77 20 61 6e 64 20 74 68 65 20 6f 6c 64 20 73 79  w and the old sy
36e7c 6e 74 61 78 65 73 20 61 72 65 3a 0a 2a 2a 0a 2a  ntaxes are:.**.*
36e7d 2a 20 20 61 29 20 54 68 65 20 6e 65 77 20 73 79  *  a) The new sy
36e7e 6e 74 61 78 20 73 75 70 70 6f 72 74 73 20 70 61  ntax supports pa
36e7f 72 65 6e 74 68 65 73 69 73 2e 20 54 68 65 20 6f  renthesis. The o
36e80 6c 64 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a 0a  ld does not..**.
36e81 2a 2a 20 20 62 29 20 54 68 65 20 6e 65 77 20 73  **  b) The new s
36e82 79 6e 74 61 78 20 73 75 70 70 6f 72 74 73 20 74  yntax supports t
36e83 68 65 20 41 4e 44 20 61 6e 64 20 4e 4f 54 20 6f  he AND and NOT o
36e84 70 65 72 61 74 6f 72 73 2e 20 54 68 65 20 6f 6c  perators. The ol
36e85 64 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a 0a 2a  d does not..**.*
36e86 2a 20 20 63 29 20 54 68 65 20 6f 6c 64 20 73 79  *  c) The old sy
36e87 6e 74 61 78 20 73 75 70 70 6f 72 74 73 20 74 68  ntax supports th
36e88 65 20 22 2d 22 20 74 6f 6b 65 6e 20 71 75 61 6c  e "-" token qual
36e89 69 66 69 65 72 2e 20 54 68 69 73 20 69 73 20 6e  ifier. This is n
36e8a 6f 74 20 0a 2a 2a 20 20 20 20 20 73 75 70 70 6f  ot .**     suppo
36e8b 72 74 65 64 20 62 79 20 74 68 65 20 6e 65 77 20  rted by the new 
36e8c 73 79 6e 74 61 78 20 28 69 74 20 69 73 20 72 65  syntax (it is re
36e8d 70 6c 61 63 65 64 20 62 79 20 74 68 65 20 4e 4f  placed by the NO
36e8e 54 20 6f 70 65 72 61 74 6f 72 29 2e 0a 2a 2a 0a  T operator)..**.
36e8f 2a 2a 20 20 64 29 20 57 68 65 6e 20 75 73 69 6e  **  d) When usin
36e90 67 20 74 68 65 20 6f 6c 64 20 73 79 6e 74 61 78  g the old syntax
36e91 2c 20 74 68 65 20 4f 52 20 6f 70 65 72 61 74 6f  , the OR operato
36e92 72 20 68 61 73 20 61 20 67 72 65 61 74 65 72 20  r has a greater 
36e93 70 72 65 63 65 64 65 6e 63 65 0a 2a 2a 20 20 20  precedence.**   
36e94 20 20 74 68 61 6e 20 61 6e 20 69 6d 70 6c 69 63    than an implic
36e95 69 74 20 41 4e 44 2e 20 57 68 65 6e 20 75 73 69  it AND. When usi
36e96 6e 67 20 74 68 65 20 6e 65 77 2c 20 62 6f 74 68  ng the new, both
36e97 20 69 6d 70 6c 69 63 69 74 79 20 61 6e 64 20 65   implicity and e
36e98 78 70 6c 69 63 69 74 0a 2a 2a 20 20 20 20 20 41  xplicit.**     A
36e99 4e 44 20 6f 70 65 72 61 74 6f 72 73 20 68 61 76  ND operators hav
36e9a 65 20 61 20 68 69 67 68 65 72 20 70 72 65 63 65  e a higher prece
36e9b 64 65 6e 63 65 20 74 68 61 6e 20 4f 52 2e 0a 2a  dence than OR..*
36e9c 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 64  *.** If compiled
36e9d 20 77 69 74 68 20 53 51 4c 49 54 45 5f 54 45 53   with SQLITE_TES
36e9e 54 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20  T defined, then 
36e9f 74 68 69 73 20 6d 6f 64 75 6c 65 20 65 78 70 6f  this module expo
36ea0 72 74 73 20 74 68 65 0a 2a 2a 20 73 79 6d 62 6f  rts the.** symbo
36ea1 6c 20 22 69 6e 74 20 73 71 6c 69 74 65 33 5f 66  l "int sqlite3_f
36ea2 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e  ts3_enable_paren
36ea3 74 68 65 73 65 73 22 2e 20 53 65 74 74 69 6e 67  theses". Setting
36ea4 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 0a 2a   this variable.*
36ea5 2a 20 74 6f 20 7a 65 72 6f 20 63 61 75 73 65 73  * to zero causes
36ea6 20 74 68 65 20 6d 6f 64 75 6c 65 20 74 6f 20 75   the module to u
36ea7 73 65 20 74 68 65 20 6f 6c 64 20 73 79 6e 74 61  se the old synta
36ea8 78 2e 20 49 66 20 69 74 20 69 73 20 73 65 74 20  x. If it is set 
36ea9 74 6f 20 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20  to .** non-zero 
36eaa 74 68 65 20 6e 65 77 20 73 79 6e 74 61 78 20 69  the new syntax i
36eab 73 20 61 63 74 69 76 61 74 65 64 2e 20 54 68 69  s activated. Thi
36eac 73 20 69 73 20 73 6f 20 62 6f 74 68 20 73 79 6e  s is so both syn
36ead 74 61 78 65 73 20 63 61 6e 0a 2a 2a 20 62 65 20  taxes can.** be 
36eae 74 65 73 74 65 64 20 75 73 69 6e 67 20 61 20 73  tested using a s
36eaf 69 6e 67 6c 65 20 62 75 69 6c 64 20 6f 66 20 74  ingle build of t
36eb0 65 73 74 66 69 78 74 75 72 65 2e 0a 2a 2f 0a 23  estfixture..*/.#
36eb1 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
36eb2 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
36eb3 20 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e   sqlite3_fts3_en
36eb4 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73  able_parentheses
36eb5 20 3d 20 30 3b 0a 23 65 6c 73 65 0a 23 20 69 66   = 0;.#else.# if
36eb6 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
36eb7 45 5f 46 54 53 33 5f 50 41 52 45 4e 54 48 45 53  E_FTS3_PARENTHES
36eb8 49 53 20 0a 23 20 20 64 65 66 69 6e 65 20 73 71  IS .#  define sq
36eb9 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c  lite3_fts3_enabl
36eba 65 5f 70 61 72 65 6e 74 68 65 73 65 73 20 31 0a  e_parentheses 1.
36ebb 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65  # else.#  define
36ebc 20 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e   sqlite3_fts3_en
36ebd 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73  able_parentheses
36ebe 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69   0.# endif.#endi
36ebf 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61 75 6c 74  f../*.** Default
36ec0 20 73 70 61 6e 20 66 6f 72 20 4e 45 41 52 20 6f   span for NEAR o
36ec1 70 65 72 61 74 6f 72 73 2e 0a 2a 2f 0a 23 64 65  perators..*/.#de
36ec2 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 54 53 33  fine SQLITE_FTS3
36ec3 5f 44 45 46 41 55 4c 54 5f 4e 45 41 52 5f 50 41  _DEFAULT_NEAR_PA
36ec4 52 41 4d 20 31 30 0a 0a 0a 74 79 70 65 64 65 66  RAM 10...typedef
36ec5 20 73 74 72 75 63 74 20 50 61 72 73 65 43 6f 6e   struct ParseCon
36ec6 74 65 78 74 20 50 61 72 73 65 43 6f 6e 74 65 78  text ParseContex
36ec7 74 3b 0a 73 74 72 75 63 74 20 50 61 72 73 65 43  t;.struct ParseC
36ec8 6f 6e 74 65 78 74 20 7b 0a 20 20 73 71 6c 69 74  ontext {.  sqlit
36ec9 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
36eca 6f 6b 65 6e 69 7a 65 72 3b 20 20 20 20 20 20 2f  okenizer;      /
36ecb 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64 75  * Tokenizer modu
36ecc 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  le */.  const ch
36ecd 61 72 20 2a 2a 61 7a 43 6f 6c 3b 20 20 20 20 20  ar **azCol;     
36ece 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
36ecf 72 72 61 79 20 6f 66 20 63 6f 6c 75 6d 6e 20 6e  rray of column n
36ed0 61 6d 65 73 20 66 6f 72 20 66 74 73 33 20 74 61  ames for fts3 ta
36ed1 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  ble */.  int nCo
36ed2 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
36ed3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36ed4 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
36ed5 73 20 69 6e 20 61 7a 43 6f 6c 5b 5d 20 2a 2f 0a  s in azCol[] */.
36ed6 20 20 69 6e 74 20 69 44 65 66 61 75 6c 74 43 6f    int iDefaultCo
36ed7 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
36ed8 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74        /* Default
36ed9 20 63 6f 6c 75 6d 6e 20 74 6f 20 71 75 65 72 79   column to query
36eda 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f   */.  sqlite3_co
36edb 6e 74 65 78 74 20 2a 70 43 74 78 3b 20 20 20 20  ntext *pCtx;    
36edc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
36edd 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  te error message
36ede 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e   here */.  int n
36edf 4e 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  Nest;           
36ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
36ee1 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74  * Number of nest
36ee2 65 64 20 62 72 61 63 6b 65 74 73 20 2a 2f 0a 7d  ed brackets */.}
36ee3 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  ;../*.** This fu
36ee4 6e 63 74 69 6f 6e 20 69 73 20 65 71 75 69 76 61  nction is equiva
36ee5 6c 65 6e 74 20 74 6f 20 74 68 65 20 73 74 61 6e  lent to the stan
36ee6 64 61 72 64 20 69 73 73 70 61 63 65 28 29 20 66  dard isspace() f
36ee7 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20  unction. .**.** 
36ee8 54 68 65 20 73 74 61 6e 64 61 72 64 20 69 73 73  The standard iss
36ee9 70 61 63 65 28 29 20 63 61 6e 20 62 65 20 61 77  pace() can be aw
36eea 6b 77 61 72 64 20 74 6f 20 75 73 65 20 73 61 66  kward to use saf
36eeb 65 6c 79 2c 20 62 65 63 61 75 73 65 20 61 6c 74  ely, because alt
36eec 68 6f 75 67 68 20 69 74 0a 2a 2a 20 69 73 20 64  hough it.** is d
36eed 65 66 69 6e 65 64 20 74 6f 20 61 63 63 65 70 74  efined to accept
36eee 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 6f 66 20   an argument of 
36eef 74 79 70 65 20 69 6e 74 2c 20 69 74 73 20 62 65  type int, its be
36ef0 68 61 76 69 6f 75 72 20 77 68 65 6e 20 70 61 73  haviour when pas
36ef1 73 65 64 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65  sed.** an intege
36ef2 72 20 74 68 61 74 20 66 61 6c 6c 73 20 6f 75 74  r that falls out
36ef3 73 69 64 65 20 6f 66 20 74 68 65 20 72 61 6e 67  side of the rang
36ef4 65 20 6f 66 20 74 68 65 20 75 6e 73 69 67 6e 65  e of the unsigne
36ef5 64 20 63 68 61 72 20 74 79 70 65 0a 2a 2a 20 69  d char type.** i
36ef6 73 20 75 6e 64 65 66 69 6e 65 64 20 28 61 6e 64  s undefined (and
36ef7 20 73 6f 6d 65 74 69 6d 65 73 2c 20 22 75 6e 64   sometimes, "und
36ef8 65 66 69 6e 65 64 22 20 6d 65 61 6e 73 20 73 65  efined" means se
36ef9 67 66 61 75 6c 74 29 2e 20 54 68 69 73 20 77 72  gfault). This wr
36efa 61 70 70 65 72 0a 2a 2a 20 69 73 20 64 65 66 69  apper.** is defi
36efb 6e 65 64 20 74 6f 20 61 63 63 65 70 74 20 61 6e  ned to accept an
36efc 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 74 79 70   argument of typ
36efd 65 20 63 68 61 72 2c 20 61 6e 64 20 61 6c 77 61  e char, and alwa
36efe 79 73 20 72 65 74 75 72 6e 73 20 30 20 66 6f 72  ys returns 0 for
36eff 0a 2a 2a 20 61 6e 79 20 76 61 6c 75 65 73 20 74  .** any values t
36f00 68 61 74 20 66 61 6c 6c 20 6f 75 74 73 69 64 65  hat fall outside
36f01 20 6f 66 20 74 68 65 20 72 61 6e 67 65 20 6f 66   of the range of
36f02 20 74 68 65 20 75 6e 73 69 67 6e 65 64 20 63 68   the unsigned ch
36f03 61 72 20 74 79 70 65 20 28 69 2e 65 2e 0a 2a 2a  ar type (i.e..**
36f04 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 73   negative values
36f05 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
36f06 20 66 74 73 33 69 73 73 70 61 63 65 28 63 68 61   fts3isspace(cha
36f07 72 20 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 28  r c){.  return (
36f08 63 26 30 78 38 30 29 3d 3d 30 20 3f 20 69 73 73  c&0x80)==0 ? iss
36f09 70 61 63 65 28 63 29 20 3a 20 30 3b 0a 7d 0a 0a  pace(c) : 0;.}..
36f0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68  /*.** Extract th
36f0b 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f  e next token fro
36f0c 6d 20 62 75 66 66 65 72 20 7a 20 28 6c 65 6e 67  m buffer z (leng
36f0d 74 68 20 6e 29 20 75 73 69 6e 67 20 74 68 65 20  th n) using the 
36f0e 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20 61 6e 64  tokenizer.** and
36f0f 20 6f 74 68 65 72 20 69 6e 66 6f 72 6d 61 74 69   other informati
36f10 6f 6e 20 28 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  on (column names
36f11 20 65 74 63 2e 29 20 69 6e 20 70 50 61 72 73 65   etc.) in pParse
36f12 2e 20 43 72 65 61 74 65 20 61 6e 20 46 74 73 33  . Create an Fts3
36f13 45 78 70 72 0a 2a 2a 20 73 74 72 75 63 74 75 72  Expr.** structur
36f14 65 20 6f 66 20 74 79 70 65 20 46 54 53 51 55 45  e of type FTSQUE
36f15 52 59 5f 50 48 52 41 53 45 20 63 6f 6e 74 61 69  RY_PHRASE contai
36f16 6e 69 6e 67 20 61 20 70 68 72 61 73 65 20 63 6f  ning a phrase co
36f17 6e 73 69 73 74 69 6e 67 20 6f 66 20 74 68 69 73  nsisting of this
36f18 0a 2a 2a 20 73 69 6e 67 6c 65 20 74 6f 6b 65 6e  .** single token
36f19 20 61 6e 64 20 73 65 74 20 2a 70 70 45 78 70 72   and set *ppExpr
36f1a 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 69 74 2e   to point to it.
36f1b 20 49 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74   If the end of t
36f1c 68 65 20 62 75 66 66 65 72 20 69 73 0a 2a 2a 20  he buffer is.** 
36f1d 72 65 61 63 68 65 64 20 62 65 66 6f 72 65 20 61  reached before a
36f1e 20 74 6f 6b 65 6e 20 69 73 20 66 6f 75 6e 64 2c   token is found,
36f1f 20 73 65 74 20 2a 70 70 45 78 70 72 20 74 6f 20   set *ppExpr to 
36f20 7a 65 72 6f 2e 20 49 74 20 69 73 20 74 68 65 0a  zero. It is the.
36f21 2a 2a 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74  ** responsibilit
36f22 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20  y of the caller 
36f23 74 6f 20 65 76 65 6e 74 75 61 6c 6c 79 20 64 65  to eventually de
36f24 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 61 6c 6c  allocate the all
36f25 6f 63 61 74 65 64 20 0a 2a 2a 20 46 74 73 33 45  ocated .** Fts3E
36f26 78 70 72 20 73 74 72 75 63 74 75 72 65 20 28 69  xpr structure (i
36f27 66 20 61 6e 79 29 20 62 79 20 70 61 73 73 69 6e  f any) by passin
36f28 67 20 69 74 20 74 6f 20 73 71 6c 69 74 65 33 5f  g it to sqlite3_
36f29 66 72 65 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65  free()..**.** Re
36f2a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69  turn SQLITE_OK i
36f2b 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72  f successful, or
36f2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66   SQLITE_NOMEM if
36f2d 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
36f2e 74 69 6f 6e 0a 2a 2a 20 66 61 69 6c 73 2e 0a 2a  tion.** fails..*
36f2f 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
36f30 4e 65 78 74 54 6f 6b 65 6e 28 0a 20 20 50 61 72  NextToken(.  Par
36f31 73 65 43 6f 6e 74 65 78 74 20 2a 70 50 61 72 73  seContext *pPars
36f32 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
36f33 20 20 20 20 20 2f 2a 20 66 74 73 33 20 71 75 65       /* fts3 que
36f34 72 79 20 70 61 72 73 65 20 63 6f 6e 74 65 78 74  ry parse context
36f35 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20   */.  int iCol, 
36f36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36f37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
36f38 20 56 61 6c 75 65 20 66 6f 72 20 46 74 73 33 50   Value for Fts3P
36f39 68 72 61 73 65 2e 69 43 6f 6c 75 6d 6e 20 2a 2f  hrase.iColumn */
36f3a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
36f3b 2c 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20  , int n,        
36f3c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
36f3d 70 75 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  put string */.  
36f3e 46 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70  Fts3Expr **ppExp
36f3f 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
36f40 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
36f41 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
36f42 69 6e 74 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20  int *pnConsumed 
36f43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36f44 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
36f45 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
36f46 63 6f 6e 73 75 6d 65 64 20 2a 2f 0a 29 7b 0a 20  consumed */.){. 
36f47 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
36f48 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d  er *pTokenizer =
36f49 20 70 50 61 72 73 65 2d 3e 70 54 6f 6b 65 6e 69   pParse->pTokeni
36f4a 7a 65 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74  zer;.  sqlite3_t
36f4b 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
36f4c 63 6f 6e 73 74 20 2a 70 4d 6f 64 75 6c 65 20 3d  const *pModule =
36f4d 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f   pTokenizer->pMo
36f4e 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  dule;.  int rc;.
36f4f 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
36f50 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  zer_cursor *pCur
36f51 73 6f 72 3b 0a 20 20 46 74 73 33 45 78 70 72 20  sor;.  Fts3Expr 
36f52 2a 70 52 65 74 20 3d 20 30 3b 0a 20 20 69 6e 74  *pRet = 0;.  int
36f53 20 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 30 3b 0a   nConsumed = 0;.
36f54 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d  .  rc = pModule-
36f55 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65  >xOpen(pTokenize
36f56 72 2c 20 7a 2c 20 6e 2c 20 26 70 43 75 72 73 6f  r, z, n, &pCurso
36f57 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  r);.  if( rc==SQ
36f58 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 63  LITE_OK ){.    c
36f59 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f 6b 65  onst char *zToke
36f5a 6e 3b 0a 20 20 20 20 69 6e 74 20 6e 54 6f 6b 65  n;.    int nToke
36f5b 6e 2c 20 69 53 74 61 72 74 2c 20 69 45 6e 64 2c  n, iStart, iEnd,
36f5c 20 69 50 6f 73 69 74 69 6f 6e 3b 0a 20 20 20 20   iPosition;.    
36f5d 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20  int nByte;      
36f5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36f5f 20 20 20 20 20 20 20 20 20 2f 2a 20 74 6f 74 61           /* tota
36f60 6c 20 73 70 61 63 65 20 74 6f 20 61 6c 6c 6f 63  l space to alloc
36f61 61 74 65 20 2a 2f 0a 0a 20 20 20 20 70 43 75 72  ate */..    pCur
36f62 73 6f 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20  sor->pTokenizer 
36f63 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20  = pTokenizer;.  
36f64 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e    rc = pModule->
36f65 78 4e 65 78 74 28 70 43 75 72 73 6f 72 2c 20 26  xNext(pCursor, &
36f66 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65 6e 2c  zToken, &nToken,
36f67 20 26 69 53 74 61 72 74 2c 20 26 69 45 6e 64 2c   &iStart, &iEnd,
36f68 20 26 69 50 6f 73 69 74 69 6f 6e 29 3b 0a 0a 20   &iPosition);.. 
36f69 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
36f6a 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 6e 42  E_OK ){.      nB
36f6b 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 46 74 73  yte = sizeof(Fts
36f6c 33 45 78 70 72 29 20 2b 20 73 69 7a 65 6f 66 28  3Expr) + sizeof(
36f6d 46 74 73 33 50 68 72 61 73 65 29 20 2b 20 6e 54  Fts3Phrase) + nT
36f6e 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 70 52 65 74  oken;.      pRet
36f6f 20 3d 20 28 46 74 73 33 45 78 70 72 20 2a 29 73   = (Fts3Expr *)s
36f70 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42  qlite3_malloc(nB
36f71 79 74 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  yte);.      if( 
36f72 21 70 52 65 74 20 29 7b 0a 20 20 20 20 20 20 20  !pRet ){.       
36f73 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
36f74 45 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  EM;.      }else{
36f75 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28  .        memset(
36f76 70 52 65 74 2c 20 30 2c 20 6e 42 79 74 65 29 3b  pRet, 0, nByte);
36f77 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e 65  .        pRet->e
36f78 54 79 70 65 20 3d 20 46 54 53 51 55 45 52 59 5f  Type = FTSQUERY_
36f79 50 48 52 41 53 45 3b 0a 20 20 20 20 20 20 20 20  PHRASE;.        
36f7a 70 52 65 74 2d 3e 70 50 68 72 61 73 65 20 3d 20  pRet->pPhrase = 
36f7b 28 46 74 73 33 50 68 72 61 73 65 20 2a 29 26 70  (Fts3Phrase *)&p
36f7c 52 65 74 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20  Ret[1];.        
36f7d 70 52 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e  pRet->pPhrase->n
36f7e 54 6f 6b 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20  Token = 1;.     
36f7f 20 20 20 70 52 65 74 2d 3e 70 50 68 72 61 73 65     pRet->pPhrase
36f80 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c  ->iColumn = iCol
36f81 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e  ;.        pRet->
36f82 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
36f83 30 5d 2e 6e 20 3d 20 6e 54 6f 6b 65 6e 3b 0a 20  0].n = nToken;. 
36f84 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70 50 68         pRet->pPh
36f85 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 30 5d 2e  rase->aToken[0].
36f86 7a 20 3d 20 28 63 68 61 72 20 2a 29 26 70 52 65  z = (char *)&pRe
36f87 74 2d 3e 70 50 68 72 61 73 65 5b 31 5d 3b 0a 20  t->pPhrase[1];. 
36f88 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 52         memcpy(pR
36f89 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f  et->pPhrase->aTo
36f8a 6b 65 6e 5b 30 5d 2e 7a 2c 20 7a 54 6f 6b 65 6e  ken[0].z, zToken
36f8b 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 0a 20 20 20 20  , nToken);..    
36f8c 20 20 20 20 69 66 28 20 69 45 6e 64 3c 6e 20 26      if( iEnd<n &
36f8d 26 20 7a 5b 69 45 6e 64 5d 3d 3d 27 2a 27 20 29  & z[iEnd]=='*' )
36f8e 7b 0a 20 20 20 20 20 20 20 20 20 20 70 52 65 74  {.          pRet
36f8f 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65  ->pPhrase->aToke
36f90 6e 5b 30 5d 2e 69 73 50 72 65 66 69 78 20 3d 20  n[0].isPrefix = 
36f91 31 3b 0a 20 20 20 20 20 20 20 20 20 20 69 45 6e  1;.          iEn
36f92 64 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  d++;.        }. 
36f93 20 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69         if( !sqli
36f94 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f  te3_fts3_enable_
36f95 70 61 72 65 6e 74 68 65 73 65 73 20 26 26 20 69  parentheses && i
36f96 53 74 61 72 74 3e 30 20 26 26 20 7a 5b 69 53 74  Start>0 && z[iSt
36f97 61 72 74 2d 31 5d 3d 3d 27 2d 27 20 29 7b 0a 20  art-1]=='-' ){. 
36f98 20 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70           pRet->p
36f99 50 68 72 61 73 65 2d 3e 69 73 4e 6f 74 20 3d 20  Phrase->isNot = 
36f9a 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
36f9b 20 20 20 7d 0a 20 20 20 20 20 20 6e 43 6f 6e 73     }.      nCons
36f9c 75 6d 65 64 20 3d 20 69 45 6e 64 3b 0a 20 20 20  umed = iEnd;.   
36f9d 20 7d 0a 0a 20 20 20 20 70 4d 6f 64 75 6c 65 2d   }..    pModule-
36f9e 3e 78 43 6c 6f 73 65 28 70 43 75 72 73 6f 72 29  >xClose(pCursor)
36f9f 3b 0a 20 20 7d 0a 20 20 0a 20 20 2a 70 6e 43 6f  ;.  }.  .  *pnCo
36fa0 6e 73 75 6d 65 64 20 3d 20 6e 43 6f 6e 73 75 6d  nsumed = nConsum
36fa1 65 64 3b 0a 20 20 2a 70 70 45 78 70 72 20 3d 20  ed;.  *ppExpr = 
36fa2 70 52 65 74 3b 0a 20 20 72 65 74 75 72 6e 20 72  pRet;.  return r
36fa3 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 6c  c;.}.../*.** Enl
36fa4 61 72 67 65 20 61 20 6d 65 6d 6f 72 79 20 61 6c  arge a memory al
36fa5 6c 6f 63 61 74 69 6f 6e 2e 20 20 49 66 20 61 6e  location.  If an
36fa6 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 61   out-of-memory a
36fa7 6c 6c 6f 63 61 74 69 6f 6e 20 6f 63 63 75 72 73  llocation occurs
36fa8 2c 0a 2a 2a 20 74 68 65 6e 20 66 72 65 65 20 74  ,.** then free t
36fa9 68 65 20 6f 6c 64 20 61 6c 6c 6f 63 61 74 69 6f  he old allocatio
36faa 6e 2e 0a 2a 2f 0a 76 6f 69 64 20 2a 66 74 73 33  n..*/.void *fts3
36fab 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 76 6f  ReallocOrFree(vo
36fac 69 64 20 2a 70 4f 72 69 67 2c 20 69 6e 74 20 6e  id *pOrig, int n
36fad 4e 65 77 29 7b 0a 20 20 76 6f 69 64 20 2a 70 52  New){.  void *pR
36fae 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61  et = sqlite3_rea
36faf 6c 6c 6f 63 28 70 4f 72 69 67 2c 20 6e 4e 65 77  lloc(pOrig, nNew
36fb0 29 3b 0a 20 20 69 66 28 20 21 70 52 65 74 20 29  );.  if( !pRet )
36fb1 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
36fb2 65 65 28 70 4f 72 69 67 29 3b 0a 20 20 7d 0a 20  ee(pOrig);.  }. 
36fb3 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a   return pRet;.}.
36fb4 0a 2f 2a 0a 2a 2a 20 42 75 66 66 65 72 20 7a 49  ./*.** Buffer zI
36fb5 6e 70 75 74 2c 20 6c 65 6e 67 74 68 20 6e 49 6e  nput, length nIn
36fb6 70 75 74 2c 20 63 6f 6e 74 61 69 6e 73 20 74 68  put, contains th
36fb7 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 20  e contents of a 
36fb8 71 75 6f 74 65 64 20 73 74 72 69 6e 67 0a 2a 2a  quoted string.**
36fb9 20 74 68 61 74 20 61 70 70 65 61 72 65 64 20 61   that appeared a
36fba 73 20 70 61 72 74 20 6f 66 20 61 6e 20 66 74 73  s part of an fts
36fbb 33 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69  3 query expressi
36fbc 6f 6e 2e 20 4e 65 69 74 68 65 72 20 71 75 6f 74  on. Neither quot
36fbd 65 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 69  e character.** i
36fbe 73 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68  s included in th
36fbf 65 20 62 75 66 66 65 72 2e 20 54 68 69 73 20 66  e buffer. This f
36fc0 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73  unction attempts
36fc1 20 74 6f 20 74 6f 6b 65 6e 69 7a 65 20 74 68 65   to tokenize the
36fc2 20 65 6e 74 69 72 65 0a 2a 2a 20 69 6e 70 75 74   entire.** input
36fc3 20 62 75 66 66 65 72 20 61 6e 64 20 63 72 65 61   buffer and crea
36fc4 74 65 20 61 6e 20 46 74 73 33 45 78 70 72 20 73  te an Fts3Expr s
36fc5 74 72 75 63 74 75 72 65 20 6f 66 20 74 79 70 65  tructure of type
36fc6 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45   FTSQUERY_PHRASE
36fc7 20 0a 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20   .** containing 
36fc8 74 68 65 20 72 65 73 75 6c 74 73 2e 0a 2a 2a 0a  the results..**.
36fc9 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  ** If successful
36fca 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  , SQLITE_OK is r
36fcb 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 45  eturned and *ppE
36fcc 78 70 72 20 73 65 74 20 74 6f 20 70 6f 69 6e 74  xpr set to point
36fcd 20 61 74 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63   at the.** alloc
36fce 61 74 65 64 20 46 74 73 33 45 78 70 72 20 73 74  ated Fts3Expr st
36fcf 72 75 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69  ructure. Otherwi
36fd0 73 65 2c 20 65 69 74 68 65 72 20 53 51 4c 49 54  se, either SQLIT
36fd1 45 5f 4e 4f 4d 45 4d 20 28 6f 75 74 20 6f 66 20  E_NOMEM (out of 
36fd2 6d 65 6d 6f 72 79 0a 2a 2a 20 65 72 72 6f 72 29  memory.** error)
36fd3 20 6f 72 20 53 51 4c 49 54 45 5f 45 52 52 4f 52   or SQLITE_ERROR
36fd4 20 28 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 65   (tokenization e
36fd5 72 72 6f 72 29 20 69 73 20 72 65 74 75 72 6e 65  rror) is returne
36fd6 64 20 61 6e 64 20 2a 70 70 45 78 70 72 20 73 65  d and *ppExpr se
36fd7 74 0a 2a 2a 20 74 6f 20 30 2e 0a 2a 2f 0a 73 74  t.** to 0..*/.st
36fd8 61 74 69 63 20 69 6e 74 20 67 65 74 4e 65 78 74  atic int getNext
36fd9 53 74 72 69 6e 67 28 0a 20 20 50 61 72 73 65 43  String(.  ParseC
36fda 6f 6e 74 65 78 74 20 2a 70 50 61 72 73 65 2c 20  ontext *pParse, 
36fdb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36fdc 20 20 2f 2a 20 66 74 73 33 20 71 75 65 72 79 20    /* fts3 query 
36fdd 70 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f  parse context */
36fde 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
36fdf 49 6e 70 75 74 2c 20 69 6e 74 20 6e 49 6e 70 75  Input, int nInpu
36fe0 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  t,         /* In
36fe1 70 75 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  put string */.  
36fe2 46 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70  Fts3Expr **ppExp
36fe3 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
36fe4 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
36fe5 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b  expression */.){
36fe6 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
36fe7 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72  izer *pTokenizer
36fe8 20 3d 20 70 50 61 72 73 65 2d 3e 70 54 6f 6b 65   = pParse->pToke
36fe9 6e 69 7a 65 72 3b 0a 20 20 73 71 6c 69 74 65 33  nizer;.  sqlite3
36fea 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
36feb 65 20 63 6f 6e 73 74 20 2a 70 4d 6f 64 75 6c 65  e const *pModule
36fec 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70   = pTokenizer->p
36fed 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 63  Module;.  int rc
36fee 3b 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 20  ;.  Fts3Expr *p 
36fef 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74  = 0;.  sqlite3_t
36ff0 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
36ff1 2a 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20  *pCursor = 0;.  
36ff2 63 68 61 72 20 2a 7a 54 65 6d 70 20 3d 20 30 3b  char *zTemp = 0;
36ff3 0a 20 20 69 6e 74 20 6e 54 65 6d 70 20 3d 20 30  .  int nTemp = 0
36ff4 3b 0a 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c  ;..  rc = pModul
36ff5 65 2d 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69  e->xOpen(pTokeni
36ff6 7a 65 72 2c 20 7a 49 6e 70 75 74 2c 20 6e 49 6e  zer, zInput, nIn
36ff7 70 75 74 2c 20 26 70 43 75 72 73 6f 72 29 3b 0a  put, &pCursor);.
36ff8 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
36ff9 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  _OK ){.    int i
36ffa 69 3b 0a 20 20 20 20 70 43 75 72 73 6f 72 2d 3e  i;.    pCursor->
36ffb 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f  pTokenizer = pTo
36ffc 6b 65 6e 69 7a 65 72 3b 0a 20 20 20 20 66 6f 72  kenizer;.    for
36ffd 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54  (ii=0; rc==SQLIT
36ffe 45 5f 4f 4b 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  E_OK; ii++){.   
36fff 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
37000 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 69 6e 74  Token;.      int
37001 20 6e 54 6f 6b 65 6e 2c 20 69 42 65 67 69 6e 2c   nToken, iBegin,
37002 20 69 45 6e 64 2c 20 69 50 6f 73 3b 0a 20 20 20   iEnd, iPos;.   
37003 20 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d     rc = pModule-
37004 3e 78 4e 65 78 74 28 70 43 75 72 73 6f 72 2c 20  >xNext(pCursor, 
37005 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65 6e  &zToken, &nToken
37006 2c 20 26 69 42 65 67 69 6e 2c 20 26 69 45 6e 64  , &iBegin, &iEnd
37007 2c 20 26 69 50 6f 73 29 3b 0a 20 20 20 20 20 20  , &iPos);.      
37008 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
37009 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  K ){.        int
3700a 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28   nByte = sizeof(
3700b 46 74 73 33 45 78 70 72 29 20 2b 20 73 69 7a 65  Fts3Expr) + size
3700c 6f 66 28 46 74 73 33 50 68 72 61 73 65 29 3b 0a  of(Fts3Phrase);.
3700d 20 20 20 20 20 20 20 20 70 20 3d 20 66 74 73 33          p = fts3
3700e 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 2c  ReallocOrFree(p,
3700f 20 6e 42 79 74 65 2b 69 69 2a 73 69 7a 65 6f 66   nByte+ii*sizeof
37010 28 73 74 72 75 63 74 20 50 68 72 61 73 65 54 6f  (struct PhraseTo
37011 6b 65 6e 29 29 3b 0a 20 20 20 20 20 20 20 20 7a  ken));.        z
37012 54 65 6d 70 20 3d 20 66 74 73 33 52 65 61 6c 6c  Temp = fts3Reall
37013 6f 63 4f 72 46 72 65 65 28 7a 54 65 6d 70 2c 20  ocOrFree(zTemp, 
37014 6e 54 65 6d 70 20 2b 20 6e 54 6f 6b 65 6e 29 3b  nTemp + nToken);
37015 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 20  .        if( !p 
37016 7c 7c 20 21 7a 54 65 6d 70 20 29 7b 0a 20 20 20  || !zTemp ){.   
37017 20 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d         goto no_m
37018 65 6d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  em;.        }.  
37019 20 20 20 20 20 20 69 66 28 20 69 69 3d 3d 30 20        if( ii==0 
3701a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d  ){.          mem
3701b 73 65 74 28 70 2c 20 30 2c 20 6e 42 79 74 65 29  set(p, 0, nByte)
3701c 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70  ;.          p->p
3701d 50 68 72 61 73 65 20 3d 20 28 46 74 73 33 50 68  Phrase = (Fts3Ph
3701e 72 61 73 65 20 2a 29 26 70 5b 31 5d 3b 0a 20 20  rase *)&p[1];.  
3701f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
37020 70 2d 3e 70 50 68 72 61 73 65 20 3d 20 28 46 74  p->pPhrase = (Ft
37021 73 33 50 68 72 61 73 65 20 2a 29 26 70 5b 31 5d  s3Phrase *)&p[1]
37022 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 50 68  ;.        p->pPh
37023 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 20 3d 20 69  rase->nToken = i
37024 69 2b 31 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  i+1;.        p->
37025 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
37026 69 69 5d 2e 6e 20 3d 20 6e 54 6f 6b 65 6e 3b 0a  ii].n = nToken;.
37027 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
37028 7a 54 65 6d 70 5b 6e 54 65 6d 70 5d 2c 20 7a 54  zTemp[nTemp], zT
37029 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 20  oken, nToken);. 
3702a 20 20 20 20 20 20 20 6e 54 65 6d 70 20 2b 3d 20         nTemp += 
3702b 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20  nToken;.        
3702c 69 66 28 20 69 45 6e 64 3c 6e 49 6e 70 75 74 20  if( iEnd<nInput 
3702d 26 26 20 7a 49 6e 70 75 74 5b 69 45 6e 64 5d 3d  && zInput[iEnd]=
3702e 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='*' ){.        
3702f 20 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54    p->pPhrase->aT
37030 6f 6b 65 6e 5b 69 69 5d 2e 69 73 50 72 65 66 69  oken[ii].isPrefi
37031 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  x = 1;.        }
37032 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
37033 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b  p->pPhrase->aTok
37034 65 6e 5b 69 69 5d 2e 69 73 50 72 65 66 69 78 20  en[ii].isPrefix 
37035 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 0;.        }. 
37036 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
37037 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73    pModule->xClos
37038 65 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  e(pCursor);.    
37039 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 7d  pCursor = 0;.  }
3703a 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
3703b 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 69  TE_DONE ){.    i
3703c 6e 74 20 6a 6a 3b 0a 20 20 20 20 63 68 61 72 20  nt jj;.    char 
3703d 2a 7a 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20 6e  *zNew;.    int n
3703e 4e 65 77 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74  New = 0;.    int
3703f 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28   nByte = sizeof(
37040 46 74 73 33 45 78 70 72 29 20 2b 20 73 69 7a 65  Fts3Expr) + size
37041 6f 66 28 46 74 73 33 50 68 72 61 73 65 29 3b 0a  of(Fts3Phrase);.
37042 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 28 70 3f      nByte += (p?
37043 28 70 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f  (p->pPhrase->nTo
37044 6b 65 6e 2d 31 29 3a 30 29 20 2a 20 73 69 7a 65  ken-1):0) * size
37045 6f 66 28 73 74 72 75 63 74 20 50 68 72 61 73 65  of(struct Phrase
37046 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 70 20 3d 20  Token);.    p = 
37047 66 74 73 33 52 65 61 6c 6c 6f 63 4f 72 46 72 65  fts3ReallocOrFre
37048 65 28 70 2c 20 6e 42 79 74 65 20 2b 20 6e 54 65  e(p, nByte + nTe
37049 6d 70 29 3b 0a 20 20 20 20 69 66 28 20 21 70 20  mp);.    if( !p 
3704a 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f  ){.      goto no
3704b 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _mem;.    }.    
3704c 69 66 28 20 7a 54 65 6d 70 20 29 7b 0a 20 20 20  if( zTemp ){.   
3704d 20 20 20 7a 4e 65 77 20 3d 20 26 28 28 28 63 68     zNew = &(((ch
3704e 61 72 20 2a 29 70 29 5b 6e 42 79 74 65 5d 29 3b  ar *)p)[nByte]);
3704f 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e  .      memcpy(zN
37050 65 77 2c 20 7a 54 65 6d 70 2c 20 6e 54 65 6d 70  ew, zTemp, nTemp
37051 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
37052 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c      memset(p, 0,
37053 20 6e 42 79 74 65 2b 6e 54 65 6d 70 29 3b 0a 20   nByte+nTemp);. 
37054 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70 50 68 72     }.    p->pPhr
37055 61 73 65 20 3d 20 28 46 74 73 33 50 68 72 61 73  ase = (Fts3Phras
37056 65 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 20 66  e *)&p[1];.    f
37057 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 70 2d 3e 70  or(jj=0; jj<p->p
37058 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 20  Phrase->nToken; 
37059 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d 3e  jj++){.      p->
3705a 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
3705b 6a 6a 5d 2e 7a 20 3d 20 26 7a 4e 65 77 5b 6e 4e  jj].z = &zNew[nN
3705c 65 77 5d 3b 0a 20 20 20 20 20 20 6e 4e 65 77 20  ew];.      nNew 
3705d 2b 3d 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61  += p->pPhrase->a
3705e 54 6f 6b 65 6e 5b 6a 6a 5d 2e 6e 3b 0a 20 20 20  Token[jj].n;.   
3705f 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66   }.    sqlite3_f
37060 72 65 65 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20  ree(zTemp);.    
37061 70 2d 3e 65 54 79 70 65 20 3d 20 46 54 53 51 55  p->eType = FTSQU
37062 45 52 59 5f 50 48 52 41 53 45 3b 0a 20 20 20 20  ERY_PHRASE;.    
37063 70 2d 3e 70 50 68 72 61 73 65 2d 3e 69 43 6f 6c  p->pPhrase->iCol
37064 75 6d 6e 20 3d 20 70 50 61 72 73 65 2d 3e 69 44  umn = pParse->iD
37065 65 66 61 75 6c 74 43 6f 6c 3b 0a 20 20 20 20 72  efaultCol;.    r
37066 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
37067 20 7d 0a 0a 20 20 2a 70 70 45 78 70 72 20 3d 20   }..  *ppExpr = 
37068 70 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  p;.  return rc;.
37069 6e 6f 5f 6d 65 6d 3a 0a 0a 20 20 69 66 28 20 70  no_mem:..  if( p
3706a 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 70 4d  Cursor ){.    pM
3706b 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 43  odule->xClose(pC
3706c 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 73 71  ursor);.  }.  sq
3706d 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 65 6d 70  lite3_free(zTemp
3706e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
3706f 65 28 70 29 3b 0a 20 20 2a 70 70 45 78 70 72 20  e(p);.  *ppExpr 
37070 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  = 0;.  return SQ
37071 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 7d 0a 0a 2f  LITE_NOMEM;.}../
37072 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 67 65  *.** Function ge
37073 74 4e 65 78 74 4e 6f 64 65 28 29 2c 20 77 68 69  tNextNode(), whi
37074 63 68 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  ch is called by 
37075 66 74 73 33 45 78 70 72 50 61 72 73 65 28 29 2c  fts3ExprParse(),
37076 20 6d 61 79 20 69 74 73 65 6c 66 0a 2a 2a 20 63   may itself.** c
37077 61 6c 6c 20 66 74 73 33 45 78 70 72 50 61 72 73  all fts3ExprPars
37078 65 28 29 2e 20 53 6f 20 74 68 69 73 20 66 6f 72  e(). So this for
37079 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e  ward declaration
3707a 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f   is required..*/
3707b 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
3707c 45 78 70 72 50 61 72 73 65 28 50 61 72 73 65 43  ExprParse(ParseC
3707d 6f 6e 74 65 78 74 20 2a 2c 20 63 6f 6e 73 74 20  ontext *, const 
3707e 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 46 74 73  char *, int, Fts
3707f 33 45 78 70 72 20 2a 2a 2c 20 69 6e 74 20 2a 29  3Expr **, int *)
37080 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f 75 74  ;../*.** The out
37081 70 75 74 20 76 61 72 69 61 62 6c 65 20 2a 70 70  put variable *pp
37082 45 78 70 72 20 69 73 20 70 6f 70 75 6c 61 74 65  Expr is populate
37083 64 20 77 69 74 68 20 61 6e 20 61 6c 6c 6f 63 61  d with an alloca
37084 74 65 64 20 46 74 73 33 45 78 70 72 20 0a 2a 2a  ted Fts3Expr .**
37085 20 73 74 72 75 63 74 75 72 65 2c 20 6f 72 20 73   structure, or s
37086 65 74 20 74 6f 20 30 20 69 66 20 74 68 65 20 65  et to 0 if the e
37087 6e 64 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20  nd of the input 
37088 62 75 66 66 65 72 20 69 73 20 72 65 61 63 68 65  buffer is reache
37089 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 73  d..**.** Returns
3708a 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
3708b 20 63 6f 64 65 2e 20 53 51 4c 49 54 45 5f 4f 4b   code. SQLITE_OK
3708c 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77   if everything w
3708d 6f 72 6b 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d  orks, SQLITE_NOM
3708e 45 4d 0a 2a 2a 20 69 66 20 61 20 6d 61 6c 6c 6f  EM.** if a mallo
3708f 63 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73  c failure occurs
37090 2c 20 6f 72 20 53 51 4c 49 54 45 5f 45 52 52 4f  , or SQLITE_ERRO
37091 52 20 69 66 20 61 20 70 61 72 73 65 20 65 72 72  R if a parse err
37092 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  or is encountere
37093 64 2e 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f  d..** If SQLITE_
37094 45 52 52 4f 52 20 69 73 20 72 65 74 75 72 6e 65  ERROR is returne
37095 64 2c 20 70 43 6f 6e 74 65 78 74 20 69 73 20 70  d, pContext is p
37096 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 61 6e  opulated with an
37097 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a   error message..
37098 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
37099 74 4e 65 78 74 4e 6f 64 65 28 0a 20 20 50 61 72  tNextNode(.  Par
3709a 73 65 43 6f 6e 74 65 78 74 20 2a 70 50 61 72 73  seContext *pPars
3709b 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
3709c 20 20 20 20 20 2f 2a 20 66 74 73 33 20 71 75 65       /* fts3 que
3709d 72 79 20 70 61 72 73 65 20 63 6f 6e 74 65 78 74  ry parse context
3709e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
3709f 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 20 20 20 20   *z, int n,     
370a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
370a1 20 49 6e 70 75 74 20 73 74 72 69 6e 67 20 2a 2f   Input string */
370a2 0a 20 20 46 74 73 33 45 78 70 72 20 2a 2a 70 70  .  Fts3Expr **pp
370a3 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20  Expr,           
370a4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
370a5 54 3a 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  T: expression */
370a6 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6e 73 75 6d  .  int *pnConsum
370a7 65 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ed              
370a8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
370a9 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  T: Number of byt
370aa 65 73 20 63 6f 6e 73 75 6d 65 64 20 2a 2f 0a 29  es consumed */.)
370ab 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  {.  static const
370ac 20 73 74 72 75 63 74 20 46 74 73 33 4b 65 79 77   struct Fts3Keyw
370ad 6f 72 64 20 7b 0a 20 20 20 20 63 68 61 72 20 7a  ord {.    char z
370ae 5b 34 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20  [4];            
370af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
370b0 2f 2a 20 4b 65 79 77 6f 72 64 20 74 65 78 74 20  /* Keyword text 
370b1 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  */.    unsigned 
370b2 63 68 61 72 20 6e 3b 20 20 20 20 20 20 20 20 20  char n;         
370b3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
370b4 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 6b 65  Length of the ke
370b5 79 77 6f 72 64 20 2a 2f 0a 20 20 20 20 75 6e 73  yword */.    uns
370b6 69 67 6e 65 64 20 63 68 61 72 20 70 61 72 65 6e  igned char paren
370b7 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 20  Only;           
370b8 20 20 20 2f 2a 20 4f 6e 6c 79 20 76 61 6c 69 64     /* Only valid
370b9 20 69 6e 20 70 61 72 65 6e 20 6d 6f 64 65 20 2a   in paren mode *
370ba 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  /.    unsigned c
370bb 68 61 72 20 65 54 79 70 65 3b 20 20 20 20 20 20  har eType;      
370bc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b              /* K
370bd 65 79 77 6f 72 64 20 63 6f 64 65 20 2a 2f 0a 20  eyword code */. 
370be 20 7d 20 61 4b 65 79 77 6f 72 64 5b 5d 20 3d 20   } aKeyword[] = 
370bf 7b 0a 20 20 20 20 7b 20 22 4f 52 22 20 2c 20 20  {.    { "OR" ,  
370c0 32 2c 20 30 2c 20 46 54 53 51 55 45 52 59 5f 4f  2, 0, FTSQUERY_O
370c1 52 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 41 4e  R   },.    { "AN
370c2 44 22 2c 20 20 33 2c 20 31 2c 20 46 54 53 51 55  D",  3, 1, FTSQU
370c3 45 52 59 5f 41 4e 44 20 20 7d 2c 0a 20 20 20 20  ERY_AND  },.    
370c4 7b 20 22 4e 4f 54 22 2c 20 20 33 2c 20 31 2c 20  { "NOT",  3, 1, 
370c5 46 54 53 51 55 45 52 59 5f 4e 4f 54 20 20 7d 2c  FTSQUERY_NOT  },
370c6 0a 20 20 20 20 7b 20 22 4e 45 41 52 22 2c 20 34  .    { "NEAR", 4
370c7 2c 20 30 2c 20 46 54 53 51 55 45 52 59 5f 4e 45  , 0, FTSQUERY_NE
370c8 41 52 20 7d 0a 20 20 7d 3b 0a 20 20 69 6e 74 20  AR }.  };.  int 
370c9 69 69 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 0a  ii;.  int iCol;.
370ca 20 20 69 6e 74 20 69 43 6f 6c 4c 65 6e 3b 0a 20    int iColLen;. 
370cb 20 69 6e 74 20 72 63 3b 0a 20 20 46 74 73 33 45   int rc;.  Fts3E
370cc 78 70 72 20 2a 70 52 65 74 20 3d 20 30 3b 0a 0a  xpr *pRet = 0;..
370cd 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49    const char *zI
370ce 6e 70 75 74 20 3d 20 7a 3b 0a 20 20 69 6e 74 20  nput = z;.  int 
370cf 6e 49 6e 70 75 74 20 3d 20 6e 3b 0a 0a 20 20 2f  nInput = n;..  /
370d0 2a 20 53 6b 69 70 20 6f 76 65 72 20 61 6e 79 20  * Skip over any 
370d1 77 68 69 74 65 73 70 61 63 65 20 62 65 66 6f 72  whitespace befor
370d2 65 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 61  e checking for a
370d3 20 6b 65 79 77 6f 72 64 2c 20 61 6e 20 6f 70 65   keyword, an ope
370d4 6e 20 6f 72 0a 20 20 2a 2a 20 63 6c 6f 73 65 20  n or.  ** close 
370d5 62 72 61 63 6b 65 74 2c 20 6f 72 20 61 20 71 75  bracket, or a qu
370d6 6f 74 65 64 20 73 74 72 69 6e 67 2e 20 0a 20 20  oted string. .  
370d7 2a 2f 0a 20 20 77 68 69 6c 65 28 20 6e 49 6e 70  */.  while( nInp
370d8 75 74 3e 30 20 26 26 20 66 74 73 33 69 73 73 70  ut>0 && fts3issp
370d9 61 63 65 28 2a 7a 49 6e 70 75 74 29 20 29 7b 0a  ace(*zInput) ){.
370da 20 20 20 20 6e 49 6e 70 75 74 2d 2d 3b 0a 20 20      nInput--;.  
370db 20 20 7a 49 6e 70 75 74 2b 2b 3b 0a 20 20 7d 0a    zInput++;.  }.
370dc 20 20 69 66 28 20 6e 49 6e 70 75 74 3d 3d 30 20    if( nInput==0 
370dd 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
370de 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 0a  LITE_DONE;.  }..
370df 20 20 2f 2a 20 53 65 65 20 69 66 20 77 65 20 61    /* See if we a
370e0 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20  re dealing with 
370e1 61 20 6b 65 79 77 6f 72 64 2e 20 2a 2f 0a 20 20  a keyword. */.  
370e2 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 69 6e  for(ii=0; ii<(in
370e3 74 29 28 73 69 7a 65 6f 66 28 61 4b 65 79 77 6f  t)(sizeof(aKeywo
370e4 72 64 29 2f 73 69 7a 65 6f 66 28 73 74 72 75 63  rd)/sizeof(struc
370e5 74 20 46 74 73 33 4b 65 79 77 6f 72 64 29 29 3b  t Fts3Keyword));
370e6 20 69 69 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73   ii++){.    cons
370e7 74 20 73 74 72 75 63 74 20 46 74 73 33 4b 65 79  t struct Fts3Key
370e8 77 6f 72 64 20 2a 70 4b 65 79 20 3d 20 26 61 4b  word *pKey = &aK
370e9 65 79 77 6f 72 64 5b 69 69 5d 3b 0a 0a 20 20 20  eyword[ii];..   
370ea 20 69 66 28 20 28 70 4b 65 79 2d 3e 70 61 72 65   if( (pKey->pare
370eb 6e 4f 6e 6c 79 20 26 20 7e 73 71 6c 69 74 65 33  nOnly & ~sqlite3
370ec 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72  _fts3_enable_par
370ed 65 6e 74 68 65 73 65 73 29 21 3d 30 20 29 7b 0a  entheses)!=0 ){.
370ee 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
370ef 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 6e      }..    if( n
370f0 49 6e 70 75 74 3e 3d 70 4b 65 79 2d 3e 6e 20 26  Input>=pKey->n &
370f1 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 7a 49 6e 70  & 0==memcmp(zInp
370f2 75 74 2c 20 70 4b 65 79 2d 3e 7a 2c 20 70 4b 65  ut, pKey->z, pKe
370f3 79 2d 3e 6e 29 20 29 7b 0a 20 20 20 20 20 20 69  y->n) ){.      i
370f4 6e 74 20 6e 4e 65 61 72 20 3d 20 53 51 4c 49 54  nt nNear = SQLIT
370f5 45 5f 46 54 53 33 5f 44 45 46 41 55 4c 54 5f 4e  E_FTS3_DEFAULT_N
370f6 45 41 52 5f 50 41 52 41 4d 3b 0a 20 20 20 20 20  EAR_PARAM;.     
370f7 20 69 6e 74 20 6e 4b 65 79 20 3d 20 70 4b 65 79   int nKey = pKey
370f8 2d 3e 6e 3b 0a 20 20 20 20 20 20 63 68 61 72 20  ->n;.      char 
370f9 63 4e 65 78 74 3b 0a 0a 20 20 20 20 20 20 2f 2a  cNext;..      /*
370fa 20 49 66 20 74 68 69 73 20 69 73 20 61 20 22 4e   If this is a "N
370fb 45 41 52 22 20 6b 65 79 77 6f 72 64 2c 20 63 68  EAR" keyword, ch
370fc 65 63 6b 20 66 6f 72 20 61 6e 20 65 78 70 6c 69  eck for an expli
370fd 63 69 74 20 6e 65 61 72 6e 65 73 73 2e 20 2a 2f  cit nearness. */
370fe 0a 20 20 20 20 20 20 69 66 28 20 70 4b 65 79 2d  .      if( pKey-
370ff 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59  >eType==FTSQUERY
37100 5f 4e 45 41 52 20 29 7b 0a 20 20 20 20 20 20 20  _NEAR ){.       
37101 20 61 73 73 65 72 74 28 20 6e 4b 65 79 3d 3d 34   assert( nKey==4
37102 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
37103 7a 49 6e 70 75 74 5b 34 5d 3d 3d 27 2f 27 20 26  zInput[4]=='/' &
37104 26 20 7a 49 6e 70 75 74 5b 35 5d 3e 3d 27 30 27  & zInput[5]>='0'
37105 20 26 26 20 7a 49 6e 70 75 74 5b 35 5d 3c 3d 27   && zInput[5]<='
37106 39 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  9' ){.          
37107 6e 4e 65 61 72 20 3d 20 30 3b 0a 20 20 20 20 20  nNear = 0;.     
37108 20 20 20 20 20 66 6f 72 28 6e 4b 65 79 3d 35 3b       for(nKey=5;
37109 20 7a 49 6e 70 75 74 5b 6e 4b 65 79 5d 3e 3d 27   zInput[nKey]>='
3710a 30 27 20 26 26 20 7a 49 6e 70 75 74 5b 6e 4b 65  0' && zInput[nKe
3710b 79 5d 3c 3d 27 39 27 3b 20 6e 4b 65 79 2b 2b 29  y]<='9'; nKey++)
3710c 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 4e  {.            nN
3710d 65 61 72 20 3d 20 6e 4e 65 61 72 20 2a 20 31 30  ear = nNear * 10
3710e 20 2b 20 28 7a 49 6e 70 75 74 5b 6e 4b 65 79 5d   + (zInput[nKey]
3710f 20 2d 20 27 30 27 29 3b 0a 20 20 20 20 20 20 20   - '0');.       
37110 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
37111 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
37112 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74   At this point t
37113 68 69 73 20 69 73 20 70 72 6f 62 61 62 6c 79 20  his is probably 
37114 61 20 6b 65 79 77 6f 72 64 2e 20 42 75 74 20 66  a keyword. But f
37115 6f 72 20 74 68 61 74 20 74 6f 20 62 65 20 74 72  or that to be tr
37116 75 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  ue,.      ** the
37117 20 6e 65 78 74 20 62 79 74 65 20 6d 75 73 74 20   next byte must 
37118 63 6f 6e 74 61 69 6e 20 65 69 74 68 65 72 20 77  contain either w
37119 68 69 74 65 73 70 61 63 65 2c 20 61 6e 20 6f 70  hitespace, an op
3711a 65 6e 20 6f 72 20 63 6c 6f 73 65 0a 20 20 20 20  en or close.    
3711b 20 20 2a 2a 20 70 61 72 65 6e 74 68 65 73 69 73    ** parenthesis
3711c 2c 20 61 20 71 75 6f 74 65 20 63 68 61 72 61 63  , a quote charac
3711d 74 65 72 2c 20 6f 72 20 45 4f 46 2e 20 0a 20 20  ter, or EOF. .  
3711e 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 4e 65      */.      cNe
3711f 78 74 20 3d 20 7a 49 6e 70 75 74 5b 6e 4b 65 79  xt = zInput[nKey
37120 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 66 74 73  ];.      if( fts
37121 33 69 73 73 70 61 63 65 28 63 4e 65 78 74 29 20  3isspace(cNext) 
37122 0a 20 20 20 20 20 20 20 7c 7c 20 63 4e 65 78 74  .       || cNext
37123 3d 3d 27 22 27 20 7c 7c 20 63 4e 65 78 74 3d 3d  =='"' || cNext==
37124 27 28 27 20 7c 7c 20 63 4e 65 78 74 3d 3d 27 29  '(' || cNext==')
37125 27 20 7c 7c 20 63 4e 65 78 74 3d 3d 30 0a 20 20  ' || cNext==0.  
37126 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 70      ){.        p
37127 52 65 74 20 3d 20 28 46 74 73 33 45 78 70 72 20  Ret = (Fts3Expr 
37128 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
37129 28 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72  (sizeof(Fts3Expr
3712a 29 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73  ));.        mems
3712b 65 74 28 70 52 65 74 2c 20 30 2c 20 73 69 7a 65  et(pRet, 0, size
3712c 6f 66 28 46 74 73 33 45 78 70 72 29 29 3b 0a 20  of(Fts3Expr));. 
3712d 20 20 20 20 20 20 20 70 52 65 74 2d 3e 65 54 79         pRet->eTy
3712e 70 65 20 3d 20 70 4b 65 79 2d 3e 65 54 79 70 65  pe = pKey->eType
3712f 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e  ;.        pRet->
37130 6e 4e 65 61 72 20 3d 20 6e 4e 65 61 72 3b 0a 20  nNear = nNear;. 
37131 20 20 20 20 20 20 20 2a 70 70 45 78 70 72 20 3d         *ppExpr =
37132 20 70 52 65 74 3b 0a 20 20 20 20 20 20 20 20 2a   pRet;.        *
37133 70 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 28 7a 49  pnConsumed = (zI
37134 6e 70 75 74 20 2d 20 7a 29 20 2b 20 6e 4b 65 79  nput - z) + nKey
37135 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
37136 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
37137 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 54 75    }..      /* Tu
37138 72 6e 73 20 6f 75 74 20 74 68 61 74 20 77 61 73  rns out that was
37139 6e 27 74 20 61 20 6b 65 79 77 6f 72 64 20 61 66  n't a keyword af
3713a 74 65 72 20 61 6c 6c 2e 20 54 68 69 73 20 68 61  ter all. This ha
3713b 70 70 65 6e 73 20 69 66 20 74 68 65 0a 20 20 20  ppens if the.   
3713c 20 20 20 2a 2a 20 75 73 65 72 20 68 61 73 20 73     ** user has s
3713d 75 70 70 6c 69 65 64 20 61 20 74 6f 6b 65 6e 20  upplied a token 
3713e 73 75 63 68 20 61 73 20 22 4f 52 61 63 6c 65 22  such as "ORacle"
3713f 2e 20 43 6f 6e 74 69 6e 75 65 2e 0a 20 20 20 20  . Continue..    
37140 20 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a    */.    }.  }..
37141 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 61    /* Check for a
37142 6e 20 6f 70 65 6e 20 62 72 61 63 6b 65 74 2e 20  n open bracket. 
37143 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
37144 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72  _fts3_enable_par
37145 65 6e 74 68 65 73 65 73 20 29 7b 0a 20 20 20 20  entheses ){.    
37146 69 66 28 20 2a 7a 49 6e 70 75 74 3d 3d 27 28 27  if( *zInput=='('
37147 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 43   ){.      int nC
37148 6f 6e 73 75 6d 65 64 3b 0a 20 20 20 20 20 20 69  onsumed;.      i
37149 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 70 50 61  nt rc;.      pPa
3714a 72 73 65 2d 3e 6e 4e 65 73 74 2b 2b 3b 0a 20 20  rse->nNest++;.  
3714b 20 20 20 20 72 63 20 3d 20 66 74 73 33 45 78 70      rc = fts3Exp
3714c 72 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 26  rParse(pParse, &
3714d 7a 49 6e 70 75 74 5b 31 5d 2c 20 6e 49 6e 70 75  zInput[1], nInpu
3714e 74 2d 31 2c 20 70 70 45 78 70 72 2c 20 26 6e 43  t-1, ppExpr, &nC
3714f 6f 6e 73 75 6d 65 64 29 3b 0a 20 20 20 20 20 20  onsumed);.      
37150 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
37151 4b 20 26 26 20 21 2a 70 70 45 78 70 72 20 29 7b  K && !*ppExpr ){
37152 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
37153 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20  LITE_DONE;.     
37154 20 7d 0a 20 20 20 20 20 20 2a 70 6e 43 6f 6e 73   }.      *pnCons
37155 75 6d 65 64 20 3d 20 28 7a 49 6e 70 75 74 20 2d  umed = (zInput -
37156 20 7a 29 20 2b 20 31 20 2b 20 6e 43 6f 6e 73 75   z) + 1 + nConsu
37157 6d 65 64 3b 0a 20 20 20 20 20 20 72 65 74 75 72  med;.      retur
37158 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 0a 20  n rc;.    }.  . 
37159 20 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20     /* Check for 
3715a 61 20 63 6c 6f 73 65 20 62 72 61 63 6b 65 74 2e  a close bracket.
3715b 20 2a 2f 0a 20 20 20 20 69 66 28 20 2a 7a 49 6e   */.    if( *zIn
3715c 70 75 74 3d 3d 27 29 27 20 29 7b 0a 20 20 20 20  put==')' ){.    
3715d 20 20 70 50 61 72 73 65 2d 3e 6e 4e 65 73 74 2d    pParse->nNest-
3715e 2d 3b 0a 20 20 20 20 20 20 2a 70 6e 43 6f 6e 73  -;.      *pnCons
3715f 75 6d 65 64 20 3d 20 28 7a 49 6e 70 75 74 20 2d  umed = (zInput -
37160 20 7a 29 20 2b 20 31 3b 0a 20 20 20 20 20 20 72   z) + 1;.      r
37161 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e  eturn SQLITE_DON
37162 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  E;.    }.  }..  
37163 2f 2a 20 53 65 65 20 69 66 20 77 65 20 61 72 65  /* See if we are
37164 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 20   dealing with a 
37165 71 75 6f 74 65 64 20 70 68 72 61 73 65 2e 20 49  quoted phrase. I
37166 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61  f this is the ca
37167 73 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 73 65  se, then.  ** se
37168 61 72 63 68 20 66 6f 72 20 74 68 65 20 63 6c 6f  arch for the clo
37169 73 69 6e 67 20 71 75 6f 74 65 20 61 6e 64 20 70  sing quote and p
3716a 61 73 73 20 74 68 65 20 77 68 6f 6c 65 20 73 74  ass the whole st
3716b 72 69 6e 67 20 74 6f 20 67 65 74 4e 65 78 74 53  ring to getNextS
3716c 74 72 69 6e 67 28 29 0a 20 20 2a 2a 20 66 6f 72  tring().  ** for
3716d 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 54 68 69   processing. Thi
3716e 73 20 69 73 20 65 61 73 79 20 74 6f 20 64 6f 2c  s is easy to do,
3716f 20 61 73 20 66 74 73 33 20 68 61 73 20 6e 6f 20   as fts3 has no 
37170 73 79 6e 74 61 78 20 66 6f 72 20 65 73 63 61 70  syntax for escap
37171 69 6e 67 0a 20 20 2a 2a 20 61 20 71 75 6f 74 65  ing.  ** a quote
37172 20 63 68 61 72 61 63 74 65 72 20 65 6d 62 65 64   character embed
37173 64 65 64 20 69 6e 20 61 20 73 74 72 69 6e 67 2e  ded in a string.
37174 0a 20 20 2a 2f 0a 20 20 69 66 28 20 2a 7a 49 6e  .  */.  if( *zIn
37175 70 75 74 3d 3d 27 22 27 20 29 7b 0a 20 20 20 20  put=='"' ){.    
37176 66 6f 72 28 69 69 3d 31 3b 20 69 69 3c 6e 49 6e  for(ii=1; ii<nIn
37177 70 75 74 20 26 26 20 7a 49 6e 70 75 74 5b 69 69  put && zInput[ii
37178 5d 21 3d 27 22 27 3b 20 69 69 2b 2b 29 3b 0a 20  ]!='"'; ii++);. 
37179 20 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 3d     *pnConsumed =
3717a 20 28 7a 49 6e 70 75 74 20 2d 20 7a 29 20 2b 20   (zInput - z) + 
3717b 69 69 20 2b 20 31 3b 0a 20 20 20 20 69 66 28 20  ii + 1;.    if( 
3717c 69 69 3d 3d 6e 49 6e 70 75 74 20 29 7b 0a 20 20  ii==nInput ){.  
3717d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
3717e 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20  E_ERROR;.    }. 
3717f 20 20 20 72 65 74 75 72 6e 20 67 65 74 4e 65 78     return getNex
37180 74 53 74 72 69 6e 67 28 70 50 61 72 73 65 2c 20  tString(pParse, 
37181 26 7a 49 6e 70 75 74 5b 31 5d 2c 20 69 69 2d 31  &zInput[1], ii-1
37182 2c 20 70 70 45 78 70 72 29 3b 0a 20 20 7d 0a 0a  , ppExpr);.  }..
37183 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c  .  /* If control
37184 20 66 6c 6f 77 73 20 74 6f 20 74 68 69 73 20 70   flows to this p
37185 6f 69 6e 74 2c 20 74 68 69 73 20 6d 75 73 74 20  oint, this must 
37186 62 65 20 61 20 72 65 67 75 6c 61 72 20 74 6f 6b  be a regular tok
37187 65 6e 2c 20 6f 72 20 0a 20 20 2a 2a 20 74 68 65  en, or .  ** the
37188 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 70 75   end of the inpu
37189 74 2e 20 52 65 61 64 20 61 20 72 65 67 75 6c 61  t. Read a regula
3718a 72 20 74 6f 6b 65 6e 20 75 73 69 6e 67 20 74 68  r token using th
3718b 65 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e sqlite3_tokeni
3718c 7a 65 72 0a 20 20 2a 2a 20 69 6e 74 65 72 66 61  zer.  ** interfa
3718d 63 65 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e 67  ce. Before doing
3718e 20 73 6f 2c 20 66 69 67 75 72 65 20 6f 75 74 20   so, figure out 
3718f 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 65  if there is an e
37190 78 70 6c 69 63 69 74 0a 20 20 2a 2a 20 63 6f 6c  xplicit.  ** col
37191 75 6d 6e 20 73 70 65 63 69 66 69 65 72 20 66 6f  umn specifier fo
37192 72 20 74 68 65 20 74 6f 6b 65 6e 2e 20 0a 20 20  r the token. .  
37193 2a 2a 0a 20 20 2a 2a 20 54 4f 44 4f 3a 20 53 74  **.  ** TODO: St
37194 72 61 6e 67 65 6c 79 2c 20 69 74 20 69 73 20 6e  rangely, it is n
37195 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 61  ot possible to a
37196 73 73 6f 63 69 61 74 65 20 61 20 63 6f 6c 75 6d  ssociate a colum
37197 6e 20 73 70 65 63 69 66 69 65 72 0a 20 20 2a 2a  n specifier.  **
37198 20 77 69 74 68 20 61 20 71 75 6f 74 65 64 20 70   with a quoted p
37199 68 72 61 73 65 2c 20 6f 6e 6c 79 20 77 69 74 68  hrase, only with
3719a 20 61 20 73 69 6e 67 6c 65 20 74 6f 6b 65 6e 2e   a single token.
3719b 20 4e 6f 74 20 73 75 72 65 20 69 66 20 74 68 69   Not sure if thi
3719c 73 20 77 61 73 0a 20 20 2a 2a 20 61 6e 20 69 6d  s was.  ** an im
3719d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 72 74  plementation art
3719e 69 66 61 63 74 20 6f 72 20 61 6e 20 69 6e 74 65  ifact or an inte
3719f 6e 74 69 6f 6e 61 6c 20 64 65 63 69 73 69 6f 6e  ntional decision
371a0 20 77 68 65 6e 20 66 74 73 33 20 77 61 73 0a 20   when fts3 was. 
371a1 20 2a 2a 20 66 69 72 73 74 20 69 6d 70 6c 65 6d   ** first implem
371a2 65 6e 74 65 64 2e 20 57 68 69 63 68 65 76 65 72  ented. Whichever
371a3 20 69 74 20 77 61 73 2c 20 74 68 69 73 20 6d 6f   it was, this mo
371a4 64 75 6c 65 20 64 75 70 6c 69 63 61 74 65 73 20  dule duplicates 
371a5 74 68 65 20 0a 20 20 2a 2a 20 6c 69 6d 69 74 61  the .  ** limita
371a6 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 43 6f  tion..  */.  iCo
371a7 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 44 65 66  l = pParse->iDef
371a8 61 75 6c 74 43 6f 6c 3b 0a 20 20 69 43 6f 6c 4c  aultCol;.  iColL
371a9 65 6e 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 69  en = 0;.  for(ii
371aa 3d 30 3b 20 69 69 3c 70 50 61 72 73 65 2d 3e 6e  =0; ii<pParse->n
371ab 43 6f 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  Col; ii++){.    
371ac 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 72  const char *zStr
371ad 20 3d 20 70 50 61 72 73 65 2d 3e 61 7a 43 6f 6c   = pParse->azCol
371ae 5b 69 69 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 53  [ii];.    int nS
371af 74 72 20 3d 20 73 74 72 6c 65 6e 28 7a 53 74 72  tr = strlen(zStr
371b0 29 3b 0a 20 20 20 20 69 66 28 20 6e 49 6e 70 75  );.    if( nInpu
371b1 74 3e 6e 53 74 72 20 26 26 20 7a 49 6e 70 75 74  t>nStr && zInput
371b2 5b 6e 53 74 72 5d 3d 3d 27 3a 27 20 0a 20 20 20  [nStr]==':' .   
371b3 20 20 26 26 20 73 71 6c 69 74 65 33 5f 73 74 72    && sqlite3_str
371b4 6e 69 63 6d 70 28 7a 53 74 72 2c 20 7a 49 6e 70  nicmp(zStr, zInp
371b5 75 74 2c 20 6e 53 74 72 29 3d 3d 30 20 0a 20 20  ut, nStr)==0 .  
371b6 20 20 29 7b 0a 20 20 20 20 20 20 69 43 6f 6c 20    ){.      iCol 
371b7 3d 20 69 69 3b 0a 20 20 20 20 20 20 69 43 6f 6c  = ii;.      iCol
371b8 4c 65 6e 20 3d 20 28 28 7a 49 6e 70 75 74 20 2d  Len = ((zInput -
371b9 20 7a 29 20 2b 20 6e 53 74 72 20 2b 20 31 29 3b   z) + nStr + 1);
371ba 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
371bb 20 20 7d 0a 20 20 7d 0a 20 20 72 63 20 3d 20 67    }.  }.  rc = g
371bc 65 74 4e 65 78 74 54 6f 6b 65 6e 28 70 50 61 72  etNextToken(pPar
371bd 73 65 2c 20 69 43 6f 6c 2c 20 26 7a 5b 69 43 6f  se, iCol, &z[iCo
371be 6c 4c 65 6e 5d 2c 20 6e 2d 69 43 6f 6c 4c 65 6e  lLen], n-iColLen
371bf 2c 20 70 70 45 78 70 72 2c 20 70 6e 43 6f 6e 73  , ppExpr, pnCons
371c0 75 6d 65 64 29 3b 0a 20 20 2a 70 6e 43 6f 6e 73  umed);.  *pnCons
371c1 75 6d 65 64 20 2b 3d 20 69 43 6f 6c 4c 65 6e 3b  umed += iColLen;
371c2 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
371c3 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d  ./*.** The argum
371c4 65 6e 74 20 69 73 20 61 6e 20 46 74 73 33 45 78  ent is an Fts3Ex
371c5 70 72 20 73 74 72 75 63 74 75 72 65 20 66 6f 72  pr structure for
371c6 20 61 20 62 69 6e 61 72 79 20 6f 70 65 72 61 74   a binary operat
371c7 6f 72 20 28 61 6e 79 20 74 79 70 65 0a 2a 2a 20  or (any type.** 
371c8 65 78 63 65 70 74 20 61 6e 20 46 54 53 51 55 45  except an FTSQUE
371c9 52 59 5f 50 48 52 41 53 45 29 2e 20 52 65 74 75  RY_PHRASE). Retu
371ca 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 76 61  rn an integer va
371cb 6c 75 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67  lue representing
371cc 20 74 68 65 0a 2a 2a 20 70 72 65 63 65 64 65 6e   the.** preceden
371cd 63 65 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74  ce of the operat
371ce 6f 72 2e 20 4c 6f 77 65 72 20 76 61 6c 75 65 73  or. Lower values
371cf 20 68 61 76 65 20 61 20 68 69 67 68 65 72 20 70   have a higher p
371d0 72 65 63 65 64 65 6e 63 65 20 28 69 2e 65 2e 0a  recedence (i.e..
371d1 2a 2a 20 67 72 6f 75 70 20 6d 6f 72 65 20 74 69  ** group more ti
371d2 67 68 74 6c 79 29 2e 20 46 6f 72 20 65 78 61 6d  ghtly). For exam
371d3 70 6c 65 2c 20 69 6e 20 74 68 65 20 43 20 6c 61  ple, in the C la
371d4 6e 67 75 61 67 65 2c 20 74 68 65 20 3d 3d 20 6f  nguage, the == o
371d5 70 65 72 61 74 6f 72 0a 2a 2a 20 67 72 6f 75 70  perator.** group
371d6 73 20 6d 6f 72 65 20 74 69 67 68 74 6c 79 20 74  s more tightly t
371d7 68 61 6e 20 7c 7c 2c 20 61 6e 64 20 77 6f 75 6c  han ||, and woul
371d8 64 20 74 68 65 72 65 66 6f 72 65 20 68 61 76 65  d therefore have
371d9 20 61 20 68 69 67 68 65 72 20 70 72 65 63 65 64   a higher preced
371da 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  ence..**.** When
371db 20 75 73 69 6e 67 20 74 68 65 20 6e 65 77 20 66   using the new f
371dc 74 73 33 20 71 75 65 72 79 20 73 79 6e 74 61 78  ts3 query syntax
371dd 20 28 77 68 65 6e 20 53 51 4c 49 54 45 5f 45 4e   (when SQLITE_EN
371de 41 42 4c 45 5f 46 54 53 33 5f 50 41 52 45 4e 54  ABLE_FTS3_PARENT
371df 48 45 53 49 53 0a 2a 2a 20 69 73 20 64 65 66 69  HESIS.** is defi
371e0 6e 65 64 29 2c 20 74 68 65 20 6f 72 64 65 72 20  ned), the order 
371e1 6f 66 20 74 68 65 20 6f 70 65 72 61 74 6f 72 73  of the operators
371e2 20 69 6e 20 70 72 65 63 65 64 65 6e 63 65 20 66   in precedence f
371e3 72 6f 6d 20 68 69 67 68 65 73 74 20 74 6f 0a 2a  rom highest to.*
371e4 2a 20 6c 6f 77 65 73 74 20 69 73 3a 0a 2a 2a 0a  * lowest is:.**.
371e5 2a 2a 20 20 20 4e 45 41 52 0a 2a 2a 20 20 20 4e  **   NEAR.**   N
371e6 4f 54 0a 2a 2a 20 20 20 41 4e 44 20 28 69 6e 63  OT.**   AND (inc
371e7 6c 75 64 69 6e 67 20 69 6d 70 6c 69 63 69 74 20  luding implicit 
371e8 41 4e 44 73 29 0a 2a 2a 20 20 20 4f 52 0a 2a 2a  ANDs).**   OR.**
371e9 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 68  .** Note that wh
371ea 65 6e 20 75 73 69 6e 67 20 74 68 65 20 6f 6c 64  en using the old
371eb 20 71 75 65 72 79 20 73 79 6e 74 61 78 2c 20 74   query syntax, t
371ec 68 65 20 4f 52 20 6f 70 65 72 61 74 6f 72 20 68  he OR operator h
371ed 61 73 20 61 20 68 69 67 68 65 72 0a 2a 2a 20 70  as a higher.** p
371ee 72 65 63 65 64 65 6e 63 65 20 74 68 61 6e 20 74  recedence than t
371ef 68 65 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e  he AND operator.
371f0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
371f1 70 50 72 65 63 65 64 65 6e 63 65 28 46 74 73 33  pPrecedence(Fts3
371f2 45 78 70 72 20 2a 70 29 7b 0a 20 20 61 73 73 65  Expr *p){.  asse
371f3 72 74 28 20 70 2d 3e 65 54 79 70 65 21 3d 46 54  rt( p->eType!=FT
371f4 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 29 3b  SQUERY_PHRASE );
371f5 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 66  .  if( sqlite3_f
371f6 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e  ts3_enable_paren
371f7 74 68 65 73 65 73 20 29 7b 0a 20 20 20 20 72 65  theses ){.    re
371f8 74 75 72 6e 20 70 2d 3e 65 54 79 70 65 3b 0a 20  turn p->eType;. 
371f9 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 65 54   }else if( p->eT
371fa 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45  ype==FTSQUERY_NE
371fb 41 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  AR ){.    return
371fc 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   1;.  }else if( 
371fd 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  p->eType==FTSQUE
371fe 52 59 5f 4f 52 20 29 7b 0a 20 20 20 20 72 65 74  RY_OR ){.    ret
371ff 75 72 6e 20 32 3b 0a 20 20 7d 0a 20 20 61 73 73  urn 2;.  }.  ass
37200 65 72 74 28 20 70 2d 3e 65 54 79 70 65 3d 3d 46  ert( p->eType==F
37201 54 53 51 55 45 52 59 5f 41 4e 44 20 29 3b 0a 20  TSQUERY_AND );. 
37202 20 72 65 74 75 72 6e 20 33 3b 0a 7d 0a 0a 2f 2a   return 3;.}../*
37203 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70 70 48  .** Argument ppH
37204 65 61 64 20 63 6f 6e 74 61 69 6e 73 20 61 20 70  ead contains a p
37205 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 75  ointer to the cu
37206 72 72 65 6e 74 20 68 65 61 64 20 6f 66 20 61 20  rrent head of a 
37207 71 75 65 72 79 20 0a 2a 2a 20 65 78 70 72 65 73  query .** expres
37208 73 69 6f 6e 20 74 72 65 65 20 62 65 69 6e 67 20  sion tree being 
37209 70 61 72 73 65 64 2e 20 70 50 72 65 76 20 69 73  parsed. pPrev is
3720a 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
3720b 6e 6f 64 65 20 6d 6f 73 74 20 72 65 63 65 6e 74  node most recent
3720c 6c 79 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 69  ly.** inserted i
3720d 6e 74 6f 20 74 68 65 20 74 72 65 65 2e 20 54 68  nto the tree. Th
3720e 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 64 64 73  is function adds
3720f 20 70 4e 65 77 2c 20 77 68 69 63 68 20 69 73 20   pNew, which is 
37210 61 6c 77 61 79 73 20 61 20 62 69 6e 61 72 79 0a  always a binary.
37211 2a 2a 20 6f 70 65 72 61 74 6f 72 20 6e 6f 64 65  ** operator node
37212 2c 20 69 6e 74 6f 20 74 68 65 20 65 78 70 72 65  , into the expre
37213 73 73 69 6f 6e 20 74 72 65 65 20 62 61 73 65 64  ssion tree based
37214 20 6f 6e 20 74 68 65 20 72 65 6c 61 74 69 76 65   on the relative
37215 20 70 72 65 63 65 64 65 6e 63 65 0a 2a 2a 20 6f   precedence.** o
37216 66 20 70 4e 65 77 20 61 6e 64 20 74 68 65 20 65  f pNew and the e
37217 78 69 73 74 69 6e 67 20 6e 6f 64 65 73 20 6f 66  xisting nodes of
37218 20 74 68 65 20 74 72 65 65 2e 20 54 68 69 73 20   the tree. This 
37219 6d 61 79 20 72 65 73 75 6c 74 20 69 6e 20 74 68  may result in th
3721a 65 20 68 65 61 64 0a 2a 2a 20 6f 66 20 74 68 65  e head.** of the
3721b 20 74 72 65 65 20 63 68 61 6e 67 69 6e 67 2c 20   tree changing, 
3721c 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 2a 70  in which case *p
3721d 70 48 65 61 64 20 69 73 20 73 65 74 20 74 6f 20  pHead is set to 
3721e 74 68 65 20 6e 65 77 20 72 6f 6f 74 20 6e 6f 64  the new root nod
3721f 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
37220 64 20 69 6e 73 65 72 74 42 69 6e 61 72 79 4f 70  d insertBinaryOp
37221 65 72 61 74 6f 72 28 0a 20 20 46 74 73 33 45 78  erator(.  Fts3Ex
37222 70 72 20 2a 2a 70 70 48 65 61 64 2c 20 20 20 20  pr **ppHead,    
37223 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
37224 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6f   the root node o
37225 66 20 61 20 74 72 65 65 20 2a 2f 0a 20 20 46 74  f a tree */.  Ft
37226 73 33 45 78 70 72 20 2a 70 50 72 65 76 2c 20 20  s3Expr *pPrev,  
37227 20 20 20 20 20 20 20 2f 2a 20 4e 6f 64 65 20 6d         /* Node m
37228 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73  ost recently ins
37229 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74  erted into the t
3722a 72 65 65 20 2a 2f 0a 20 20 46 74 73 33 45 78 70  ree */.  Fts3Exp
3722b 72 20 2a 70 4e 65 77 20 20 20 20 20 20 20 20 20  r *pNew         
3722c 20 20 2f 2a 20 4e 65 77 20 62 69 6e 61 72 79 20    /* New binary 
3722d 6e 6f 64 65 20 74 6f 20 69 6e 73 65 72 74 20 69  node to insert i
3722e 6e 74 6f 20 65 78 70 72 65 73 73 69 6f 6e 20 74  nto expression t
3722f 72 65 65 20 2a 2f 0a 29 7b 0a 20 20 46 74 73 33  ree */.){.  Fts3
37230 45 78 70 72 20 2a 70 53 70 6c 69 74 20 3d 20 70  Expr *pSplit = p
37231 50 72 65 76 3b 0a 20 20 77 68 69 6c 65 28 20 70  Prev;.  while( p
37232 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74 20 26  Split->pParent &
37233 26 20 6f 70 50 72 65 63 65 64 65 6e 63 65 28 70  & opPrecedence(p
37234 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74 29 3c  Split->pParent)<
37235 3d 6f 70 50 72 65 63 65 64 65 6e 63 65 28 70 4e  =opPrecedence(pN
37236 65 77 29 20 29 7b 0a 20 20 20 20 70 53 70 6c 69  ew) ){.    pSpli
37237 74 20 3d 20 70 53 70 6c 69 74 2d 3e 70 50 61 72  t = pSplit->pPar
37238 65 6e 74 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  ent;.  }..  if( 
37239 70 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74 20  pSplit->pParent 
3723a 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
3723b 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74 2d 3e  Split->pParent->
3723c 70 52 69 67 68 74 3d 3d 70 53 70 6c 69 74 20 29  pRight==pSplit )
3723d 3b 0a 20 20 20 20 70 53 70 6c 69 74 2d 3e 70 50  ;.    pSplit->pP
3723e 61 72 65 6e 74 2d 3e 70 52 69 67 68 74 20 3d 20  arent->pRight = 
3723f 70 4e 65 77 3b 0a 20 20 20 20 70 4e 65 77 2d 3e  pNew;.    pNew->
37240 70 50 61 72 65 6e 74 20 3d 20 70 53 70 6c 69 74  pParent = pSplit
37241 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 7d 65 6c  ->pParent;.  }el
37242 73 65 7b 0a 20 20 20 20 2a 70 70 48 65 61 64 20  se{.    *ppHead 
37243 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 70 4e  = pNew;.  }.  pN
37244 65 77 2d 3e 70 4c 65 66 74 20 3d 20 70 53 70 6c  ew->pLeft = pSpl
37245 69 74 3b 0a 20 20 70 53 70 6c 69 74 2d 3e 70 50  it;.  pSplit->pP
37246 61 72 65 6e 74 20 3d 20 70 4e 65 77 3b 0a 7d 0a  arent = pNew;.}.
37247 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 74 68 65  ./*.** Parse the
37248 20 66 74 73 33 20 71 75 65 72 79 20 65 78 70 72   fts3 query expr
37249 65 73 73 69 6f 6e 20 66 6f 75 6e 64 20 69 6e 20  ession found in 
3724a 62 75 66 66 65 72 20 7a 2c 20 6c 65 6e 67 74 68  buffer z, length
3724b 20 6e 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f   n. This functio
3724c 6e 0a 2a 2a 20 72 65 74 75 72 6e 73 20 65 69 74  n.** returns eit
3724d 68 65 72 20 77 68 65 6e 20 74 68 65 20 65 6e 64  her when the end
3724e 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 69   of the buffer i
3724f 73 20 72 65 61 63 68 65 64 20 6f 72 20 61 6e 20  s reached or an 
37250 75 6e 6d 61 74 63 68 65 64 20 0a 2a 2a 20 63 6c  unmatched .** cl
37251 6f 73 69 6e 67 20 62 72 61 63 6b 65 74 20 2d 20  osing bracket - 
37252 27 29 27 20 2d 20 69 73 20 65 6e 63 6f 75 6e 74  ')' - is encount
37253 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73  ered..**.** If s
37254 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c 49 54  uccessful, SQLIT
37255 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
37256 2c 20 2a 70 70 45 78 70 72 20 69 73 20 73 65 74  , *ppExpr is set
37257 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   to point to the
37258 0a 2a 2a 20 70 61 72 73 65 64 20 66 6f 72 6d 20  .** parsed form 
37259 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  of the expressio
3725a 6e 20 61 6e 64 20 2a 70 6e 43 6f 6e 73 75 6d 65  n and *pnConsume
3725b 64 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  d is set to the 
3725c 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 79 74  number of.** byt
3725d 65 73 20 72 65 61 64 20 66 72 6f 6d 20 62 75 66  es read from buf
3725e 66 65 72 20 7a 2e 20 4f 74 68 65 72 77 69 73 65  fer z. Otherwise
3725f 2c 20 2a 70 70 45 78 70 72 20 69 73 20 73 65 74  , *ppExpr is set
37260 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45   to 0 and SQLITE
37261 5f 4e 4f 4d 45 4d 0a 2a 2a 20 28 6f 75 74 20 6f  _NOMEM.** (out o
37262 66 20 6d 65 6d 6f 72 79 20 65 72 72 6f 72 29 20  f memory error) 
37263 6f 72 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20  or SQLITE_ERROR 
37264 28 70 61 72 73 65 20 65 72 72 6f 72 29 20 69 73  (parse error) is
37265 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74   returned..*/.st
37266 61 74 69 63 20 69 6e 74 20 66 74 73 33 45 78 70  atic int fts3Exp
37267 72 50 61 72 73 65 28 0a 20 20 50 61 72 73 65 43  rParse(.  ParseC
37268 6f 6e 74 65 78 74 20 2a 70 50 61 72 73 65 2c 20  ontext *pParse, 
37269 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3726a 20 20 2f 2a 20 66 74 73 33 20 71 75 65 72 79 20    /* fts3 query 
3726b 70 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f  parse context */
3726c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
3726d 2c 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20  , int n,        
3726e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65             /* Te
3726f 78 74 20 6f 66 20 4d 41 54 43 48 20 71 75 65 72  xt of MATCH quer
37270 79 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20  y */.  Fts3Expr 
37271 2a 2a 70 70 45 78 70 72 2c 20 20 20 20 20 20 20  **ppExpr,       
37272 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37273 2a 20 4f 55 54 3a 20 50 61 72 73 65 64 20 71 75  * OUT: Parsed qu
37274 65 72 79 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ery structure */
37275 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6e 73 75 6d  .  int *pnConsum
37276 65 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ed              
37277 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
37278 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  T: Number of byt
37279 65 73 20 63 6f 6e 73 75 6d 65 64 20 2a 2f 0a 29  es consumed */.)
3727a 7b 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 52  {.  Fts3Expr *pR
3727b 65 74 20 3d 20 30 3b 0a 20 20 46 74 73 33 45 78  et = 0;.  Fts3Ex
3727c 70 72 20 2a 70 50 72 65 76 20 3d 20 30 3b 0a 20  pr *pPrev = 0;. 
3727d 20 46 74 73 33 45 78 70 72 20 2a 70 4e 6f 74 42   Fts3Expr *pNotB
3727e 72 61 6e 63 68 20 3d 20 30 3b 20 20 20 20 20 20  ranch = 0;      
3727f 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79           /* Only
37280 20 75 73 65 64 20 69 6e 20 6c 65 67 61 63 79 20   used in legacy 
37281 70 61 72 73 65 20 6d 6f 64 65 20 2a 2f 0a 20 20  parse mode */.  
37282 69 6e 74 20 6e 49 6e 20 3d 20 6e 3b 0a 20 20 63  int nIn = n;.  c
37283 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 20 3d  onst char *zIn =
37284 20 7a 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53   z;.  int rc = S
37285 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
37286 69 73 52 65 71 75 69 72 65 50 68 72 61 73 65 20  isRequirePhrase 
37287 3d 20 31 3b 0a 0a 20 20 77 68 69 6c 65 28 20 72  = 1;..  while( r
37288 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
37289 20 20 20 20 46 74 73 33 45 78 70 72 20 2a 70 20      Fts3Expr *p 
3728a 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79  = 0;.    int nBy
3728b 74 65 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d  te = 0;.    rc =
3728c 20 67 65 74 4e 65 78 74 4e 6f 64 65 28 70 50 61   getNextNode(pPa
3728d 72 73 65 2c 20 7a 49 6e 2c 20 6e 49 6e 2c 20 26  rse, zIn, nIn, &
3728e 70 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20  p, &nByte);.    
3728f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
37290 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  K ){.      int i
37291 73 50 68 72 61 73 65 3b 0a 0a 20 20 20 20 20 20  sPhrase;..      
37292 69 66 28 20 21 73 71 6c 69 74 65 33 5f 66 74 73  if( !sqlite3_fts
37293 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68  3_enable_parenth
37294 65 73 65 73 20 0a 20 20 20 20 20 20 20 26 26 20  eses .       && 
37295 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  p->eType==FTSQUE
37296 52 59 5f 50 48 52 41 53 45 20 26 26 20 70 2d 3e  RY_PHRASE && p->
37297 70 50 68 72 61 73 65 2d 3e 69 73 4e 6f 74 20 0a  pPhrase->isNot .
37298 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
37299 20 2f 2a 20 43 72 65 61 74 65 20 61 6e 20 69 6d   /* Create an im
3729a 70 6c 69 63 69 74 20 4e 4f 54 20 6f 70 65 72 61  plicit NOT opera
3729b 74 6f 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  tor. */.        
3729c 46 74 73 33 45 78 70 72 20 2a 70 4e 6f 74 20 3d  Fts3Expr *pNot =
3729d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
3729e 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72 29  sizeof(Fts3Expr)
3729f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  );.        if( !
372a0 70 4e 6f 74 20 29 7b 0a 20 20 20 20 20 20 20 20  pNot ){.        
372a1 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70    sqlite3Fts3Exp
372a2 72 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20  rFree(p);.      
372a3 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
372a4 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20  NOMEM;.         
372a5 20 67 6f 74 6f 20 65 78 70 72 70 61 72 73 65 5f   goto exprparse_
372a6 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  out;.        }. 
372a7 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 4e         memset(pN
372a8 6f 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 46 74  ot, 0, sizeof(Ft
372a9 73 33 45 78 70 72 29 29 3b 0a 20 20 20 20 20 20  s3Expr));.      
372aa 20 20 70 4e 6f 74 2d 3e 65 54 79 70 65 20 3d 20    pNot->eType = 
372ab 46 54 53 51 55 45 52 59 5f 4e 4f 54 3b 0a 20 20  FTSQUERY_NOT;.  
372ac 20 20 20 20 20 20 70 4e 6f 74 2d 3e 70 52 69 67        pNot->pRig
372ad 68 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20  ht = p;.        
372ae 69 66 28 20 70 4e 6f 74 42 72 61 6e 63 68 20 29  if( pNotBranch )
372af 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 6f 74  {.          pNot
372b0 2d 3e 70 4c 65 66 74 20 3d 20 70 4e 6f 74 42 72  ->pLeft = pNotBr
372b1 61 6e 63 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a  anch;.        }.
372b2 20 20 20 20 20 20 20 20 70 4e 6f 74 42 72 61 6e          pNotBran
372b3 63 68 20 3d 20 70 4e 6f 74 3b 0a 20 20 20 20 20  ch = pNot;.     
372b4 20 20 20 70 20 3d 20 70 50 72 65 76 3b 0a 20 20     p = pPrev;.  
372b5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
372b6 20 20 20 69 6e 74 20 65 54 79 70 65 20 3d 20 70     int eType = p
372b7 2d 3e 65 54 79 70 65 3b 0a 20 20 20 20 20 20 20  ->eType;.       
372b8 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21 3d   assert( eType!=
372b9 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20  FTSQUERY_PHRASE 
372ba 7c 7c 20 21 70 2d 3e 70 50 68 72 61 73 65 2d 3e  || !p->pPhrase->
372bb 69 73 4e 6f 74 20 29 3b 0a 20 20 20 20 20 20 20  isNot );.       
372bc 20 69 73 50 68 72 61 73 65 20 3d 20 28 65 54 79   isPhrase = (eTy
372bd 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48 52  pe==FTSQUERY_PHR
372be 41 53 45 20 7c 7c 20 70 2d 3e 70 4c 65 66 74 29  ASE || p->pLeft)
372bf 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ;..        /* Th
372c0 65 20 69 73 52 65 71 75 69 72 65 50 68 72 61 73  e isRequirePhras
372c1 65 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65  e variable is se
372c2 74 20 74 6f 20 74 72 75 65 20 69 66 20 61 20 70  t to true if a p
372c3 68 72 61 73 65 20 6f 72 0a 20 20 20 20 20 20 20  hrase or.       
372c4 20 2a 2a 20 61 6e 20 65 78 70 72 65 73 73 69 6f   ** an expressio
372c5 6e 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 70  n contained in p
372c6 61 72 65 6e 74 68 65 73 69 73 20 69 73 20 72 65  arenthesis is re
372c7 71 75 69 72 65 64 2e 20 49 66 20 61 0a 20 20 20  quired. If a.   
372c8 20 20 20 20 20 2a 2a 20 62 69 6e 61 72 79 20 6f       ** binary o
372c9 70 65 72 61 74 6f 72 20 28 41 4e 44 2c 20 4f 52  perator (AND, OR
372ca 2c 20 4e 4f 54 20 6f 72 20 4e 45 41 52 29 20 69  , NOT or NEAR) i
372cb 73 20 65 6e 63 6f 75 6e 74 65 64 20 77 68 65 6e  s encounted when
372cc 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73 52 65  .        ** isRe
372cd 71 75 69 72 65 50 68 72 61 73 65 20 69 73 20 73  quirePhrase is s
372ce 65 74 2c 20 74 68 69 73 20 69 73 20 61 20 73 79  et, this is a sy
372cf 6e 74 61 78 20 65 72 72 6f 72 2e 0a 20 20 20 20  ntax error..    
372d0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
372d1 66 28 20 21 69 73 50 68 72 61 73 65 20 26 26 20  f( !isPhrase && 
372d2 69 73 52 65 71 75 69 72 65 50 68 72 61 73 65 20  isRequirePhrase 
372d3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
372d4 69 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65  ite3Fts3ExprFree
372d5 28 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72  (p);.          r
372d6 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
372d7 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
372d8 20 65 78 70 72 70 61 72 73 65 5f 6f 75 74 3b 0a   exprparse_out;.
372d9 20 20 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20          }.  .   
372da 20 20 20 20 20 69 66 28 20 69 73 50 68 72 61 73       if( isPhras
372db 65 20 26 26 20 21 69 73 52 65 71 75 69 72 65 50  e && !isRequireP
372dc 68 72 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20  hrase ){.       
372dd 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20     /* Insert an 
372de 69 6d 70 6c 69 63 69 74 20 41 4e 44 20 6f 70 65  implicit AND ope
372df 72 61 74 6f 72 2e 20 2a 2f 0a 20 20 20 20 20 20  rator. */.      
372e0 20 20 20 20 46 74 73 33 45 78 70 72 20 2a 70 41      Fts3Expr *pA
372e1 6e 64 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73  nd;.          as
372e2 73 65 72 74 28 20 70 52 65 74 20 26 26 20 70 50  sert( pRet && pP
372e3 72 65 76 20 29 3b 0a 20 20 20 20 20 20 20 20 20  rev );.         
372e4 20 70 41 6e 64 20 3d 20 73 71 6c 69 74 65 33 5f   pAnd = sqlite3_
372e5 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46 74  malloc(sizeof(Ft
372e6 73 33 45 78 70 72 29 29 3b 0a 20 20 20 20 20 20  s3Expr));.      
372e7 20 20 20 20 69 66 28 20 21 70 41 6e 64 20 29 7b      if( !pAnd ){
372e8 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
372e9 69 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65  ite3Fts3ExprFree
372ea 28 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  (p);.           
372eb 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
372ec 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  EM;.            
372ed 67 6f 74 6f 20 65 78 70 72 70 61 72 73 65 5f 6f  goto exprparse_o
372ee 75 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ut;.          }.
372ef 20 20 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74            memset
372f0 28 70 41 6e 64 2c 20 30 2c 20 73 69 7a 65 6f 66  (pAnd, 0, sizeof
372f1 28 46 74 73 33 45 78 70 72 29 29 3b 0a 20 20 20  (Fts3Expr));.   
372f2 20 20 20 20 20 20 20 70 41 6e 64 2d 3e 65 54 79         pAnd->eTy
372f3 70 65 20 3d 20 46 54 53 51 55 45 52 59 5f 41 4e  pe = FTSQUERY_AN
372f4 44 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 73  D;.          ins
372f5 65 72 74 42 69 6e 61 72 79 4f 70 65 72 61 74 6f  ertBinaryOperato
372f6 72 28 26 70 52 65 74 2c 20 70 50 72 65 76 2c 20  r(&pRet, pPrev, 
372f7 70 41 6e 64 29 3b 0a 20 20 20 20 20 20 20 20 20  pAnd);.         
372f8 20 70 50 72 65 76 20 3d 20 70 41 6e 64 3b 0a 20   pPrev = pAnd;. 
372f9 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
372fa 20 20 2f 2a 20 54 68 69 73 20 74 65 73 74 20 63    /* This test c
372fb 61 74 63 68 65 73 20 61 74 74 65 6d 70 74 73 20  atches attempts 
372fc 74 6f 20 6d 61 6b 65 20 65 69 74 68 65 72 20 6f  to make either o
372fd 70 65 72 61 6e 64 20 6f 66 20 61 20 4e 45 41 52  perand of a NEAR
372fe 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 70 65 72  .        ** oper
372ff 61 74 6f 72 20 73 6f 6d 65 74 68 69 6e 67 20 6f  ator something o
37300 74 68 65 72 20 74 68 61 6e 20 61 20 70 68 72 61  ther than a phra
37301 73 65 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  se. For example,
37302 20 65 69 74 68 65 72 20 6f 66 0a 20 20 20 20 20   either of.     
37303 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77     ** the follow
37304 69 6e 67 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a  ing:.        **.
37305 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 28 62          **    (b
37306 72 61 63 6b 65 74 65 64 20 65 78 70 72 65 73 73  racketed express
37307 69 6f 6e 29 20 4e 45 41 52 20 70 68 72 61 73 65  ion) NEAR phrase
37308 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 70  .        **    p
37309 68 72 61 73 65 20 4e 45 41 52 20 28 62 72 61 63  hrase NEAR (brac
3730a 6b 65 74 65 64 20 65 78 70 72 65 73 73 69 6f 6e  keted expression
3730b 29 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  ).        **.   
3730c 20 20 20 20 20 2a 2a 20 52 65 74 75 72 6e 20 61       ** Return a
3730d 6e 20 65 72 72 6f 72 20 69 6e 20 65 69 74 68 65  n error in eithe
3730e 72 20 63 61 73 65 2e 0a 20 20 20 20 20 20 20 20  r case..        
3730f 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  */.        if( p
37310 50 72 65 76 20 26 26 20 28 0a 20 20 20 20 20 20  Prev && (.      
37311 20 20 20 20 20 20 28 65 54 79 70 65 3d 3d 46 54        (eType==FT
37312 53 51 55 45 52 59 5f 4e 45 41 52 20 26 26 20 21  SQUERY_NEAR && !
37313 69 73 50 68 72 61 73 65 20 26 26 20 70 50 72 65  isPhrase && pPre
37314 76 2d 3e 65 54 79 70 65 21 3d 46 54 53 51 55 45  v->eType!=FTSQUE
37315 52 59 5f 50 48 52 41 53 45 29 0a 20 20 20 20 20  RY_PHRASE).     
37316 20 20 20 20 7c 7c 20 28 65 54 79 70 65 21 3d 46      || (eType!=F
37317 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 26  TSQUERY_PHRASE &
37318 26 20 69 73 50 68 72 61 73 65 20 26 26 20 70 50  & isPhrase && pP
37319 72 65 76 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51  rev->eType==FTSQ
3731a 55 45 52 59 5f 4e 45 41 52 29 0a 20 20 20 20 20  UERY_NEAR).     
3731b 20 20 20 29 29 7b 0a 20 20 20 20 20 20 20 20 20     )){.         
3731c 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
3731d 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20  Free(p);.       
3731e 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
3731f 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 20 20  RROR;.          
37320 67 6f 74 6f 20 65 78 70 72 70 61 72 73 65 5f 6f  goto exprparse_o
37321 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ut;.        }.  
37322 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 73 50  .        if( isP
37323 68 72 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20  hrase ){.       
37324 20 20 20 69 66 28 20 70 52 65 74 20 29 7b 0a 20     if( pRet ){. 
37325 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72             asser
37326 74 28 20 70 50 72 65 76 20 26 26 20 70 50 72 65  t( pPrev && pPre
37327 76 2d 3e 70 4c 65 66 74 20 26 26 20 70 50 72 65  v->pLeft && pPre
37328 76 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a  v->pRight==0 );.
37329 20 20 20 20 20 20 20 20 20 20 20 20 70 50 72 65              pPre
3732a 76 2d 3e 70 52 69 67 68 74 20 3d 20 70 3b 0a 20  v->pRight = p;. 
3732b 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50             p->pP
3732c 61 72 65 6e 74 20 3d 20 70 50 72 65 76 3b 0a 20  arent = pPrev;. 
3732d 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
3732e 20 20 20 20 20 20 20 20 20 20 20 20 70 52 65 74              pRet
3732f 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20   = p;.          
37330 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  }.        }else{
37331 0a 20 20 20 20 20 20 20 20 20 20 69 6e 73 65 72  .          inser
37332 74 42 69 6e 61 72 79 4f 70 65 72 61 74 6f 72 28  tBinaryOperator(
37333 26 70 52 65 74 2c 20 70 50 72 65 76 2c 20 70 29  &pRet, pPrev, p)
37334 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
37335 20 20 20 20 69 73 52 65 71 75 69 72 65 50 68 72      isRequirePhr
37336 61 73 65 20 3d 20 21 69 73 50 68 72 61 73 65 3b  ase = !isPhrase;
37337 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61  .      }.      a
37338 73 73 65 72 74 28 20 6e 42 79 74 65 3e 30 20 29  ssert( nByte>0 )
37339 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
3733a 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  rt( rc!=SQLITE_O
3733b 4b 20 7c 7c 20 28 6e 42 79 74 65 3e 30 20 26 26  K || (nByte>0 &&
3733c 20 6e 42 79 74 65 3c 3d 6e 49 6e 29 20 29 3b 0a   nByte<=nIn) );.
3733d 20 20 20 20 6e 49 6e 20 2d 3d 20 6e 42 79 74 65      nIn -= nByte
3733e 3b 0a 20 20 20 20 7a 49 6e 20 2b 3d 20 6e 42 79  ;.    zIn += nBy
3733f 74 65 3b 0a 20 20 20 20 70 50 72 65 76 20 3d 20  te;.    pPrev = 
37340 70 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63  p;.  }..  if( rc
37341 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 26 26  ==SQLITE_DONE &&
37342 20 70 52 65 74 20 26 26 20 69 73 52 65 71 75 69   pRet && isRequi
37343 72 65 50 68 72 61 73 65 20 29 7b 0a 20 20 20 20  rePhrase ){.    
37344 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
37345 52 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63  R;.  }..  if( rc
37346 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
37347 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
37348 5f 4f 4b 3b 0a 20 20 20 20 69 66 28 20 21 73 71  _OK;.    if( !sq
37349 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c  lite3_fts3_enabl
3734a 65 5f 70 61 72 65 6e 74 68 65 73 65 73 20 26 26  e_parentheses &&
3734b 20 70 4e 6f 74 42 72 61 6e 63 68 20 29 7b 0a 20   pNotBranch ){. 
3734c 20 20 20 20 20 69 66 28 20 21 70 52 65 74 20 29       if( !pRet )
3734d 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  {.        rc = S
3734e 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
3734f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
37350 20 20 46 74 73 33 45 78 70 72 20 2a 70 49 74 65    Fts3Expr *pIte
37351 72 20 3d 20 70 4e 6f 74 42 72 61 6e 63 68 3b 0a  r = pNotBranch;.
37352 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70          while( p
37353 49 74 65 72 2d 3e 70 4c 65 66 74 20 29 7b 0a 20  Iter->pLeft ){. 
37354 20 20 20 20 20 20 20 20 20 70 49 74 65 72 20 3d           pIter =
37355 20 70 49 74 65 72 2d 3e 70 4c 65 66 74 3b 0a 20   pIter->pLeft;. 
37356 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
37357 20 70 49 74 65 72 2d 3e 70 4c 65 66 74 20 3d 20   pIter->pLeft = 
37358 70 52 65 74 3b 0a 20 20 20 20 20 20 20 20 70 52  pRet;.        pR
37359 65 74 20 3d 20 70 4e 6f 74 42 72 61 6e 63 68 3b  et = pNotBranch;
3735a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
3735b 20 7d 0a 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64   }.  *pnConsumed
3735c 20 3d 20 6e 20 2d 20 6e 49 6e 3b 0a 0a 65 78 70   = n - nIn;..exp
3735d 72 70 61 72 73 65 5f 6f 75 74 3a 0a 20 20 69 66  rparse_out:.  if
3735e 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
3735f 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74  ){.    sqlite3Ft
37360 73 33 45 78 70 72 46 72 65 65 28 70 52 65 74 29  s3ExprFree(pRet)
37361 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  ;.    sqlite3Fts
37362 33 45 78 70 72 46 72 65 65 28 70 4e 6f 74 42 72  3ExprFree(pNotBr
37363 61 6e 63 68 29 3b 0a 20 20 20 20 70 52 65 74 20  anch);.    pRet 
37364 3d 20 30 3b 0a 20 20 7d 0a 20 20 2a 70 70 45 78  = 0;.  }.  *ppEx
37365 70 72 20 3d 20 70 52 65 74 3b 0a 20 20 72 65 74  pr = pRet;.  ret
37366 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
37367 20 50 61 72 61 6d 65 74 65 72 73 20 7a 20 61 6e   Parameters z an
37368 64 20 6e 20 63 6f 6e 74 61 69 6e 20 61 20 70 6f  d n contain a po
37369 69 6e 74 65 72 20 74 6f 20 61 6e 64 20 6c 65 6e  inter to and len
3736a 67 74 68 20 6f 66 20 61 20 62 75 66 66 65 72 20  gth of a buffer 
3736b 63 6f 6e 74 61 69 6e 69 6e 67 0a 2a 2a 20 61 6e  containing.** an
3736c 20 66 74 73 33 20 71 75 65 72 79 20 65 78 70 72   fts3 query expr
3736d 65 73 73 69 6f 6e 2c 20 72 65 73 70 65 63 74 69  ession, respecti
3736e 76 65 6c 79 2e 20 54 68 69 73 20 66 75 6e 63 74  vely. This funct
3736f 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ion attempts to 
37370 70 61 72 73 65 20 74 68 65 0a 2a 2a 20 71 75 65  parse the.** que
37371 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e  ry expression an
37372 64 20 63 72 65 61 74 65 20 61 20 74 72 65 65 20  d create a tree 
37373 6f 66 20 46 74 73 33 45 78 70 72 20 73 74 72 75  of Fts3Expr stru
37374 63 74 75 72 65 73 20 72 65 70 72 65 73 65 6e 74  ctures represent
37375 69 6e 67 20 74 68 65 0a 2a 2a 20 70 61 72 73 65  ing the.** parse
37376 64 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 49 66  d expression. If
37377 20 73 75 63 63 65 73 73 66 75 6c 2c 20 2a 70 70   successful, *pp
37378 45 78 70 72 20 69 73 20 73 65 74 20 74 6f 20 70  Expr is set to p
37379 6f 69 6e 74 20 74 6f 20 74 68 65 20 68 65 61 64  oint to the head
3737a 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 72 73 65  .** of the parse
3737b 64 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  d expression tre
3737c 65 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20  e and SQLITE_OK 
3737d 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20  is returned. If 
3737e 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75  an error.** occu
3737f 72 73 2c 20 65 69 74 68 65 72 20 53 51 4c 49 54  rs, either SQLIT
37380 45 5f 4e 4f 4d 45 4d 20 28 6f 75 74 2d 6f 66 2d  E_NOMEM (out-of-
37381 6d 65 6d 6f 72 79 20 65 72 72 6f 72 29 20 6f 72  memory error) or
37382 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 28 70   SQLITE_ERROR (p
37383 61 72 73 65 0a 2a 2a 20 65 72 72 6f 72 29 20 69  arse.** error) i
37384 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a  s returned and *
37385 70 70 45 78 70 72 20 69 73 20 73 65 74 20 74 6f  ppExpr is set to
37386 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 72   0..**.** If par
37387 61 6d 65 74 65 72 20 6e 20 69 73 20 61 20 6e 65  ameter n is a ne
37388 67 61 74 69 76 65 20 6e 75 6d 62 65 72 2c 20 74  gative number, t
37389 68 65 6e 20 7a 20 69 73 20 61 73 73 75 6d 65 64  hen z is assumed
3738a 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 0a 2a   to point to a.*
3738b 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64  * nul-terminated
3738c 20 73 74 72 69 6e 67 20 61 6e 64 20 74 68 65 20   string and the 
3738d 6c 65 6e 67 74 68 20 69 73 20 64 65 74 65 72 6d  length is determ
3738e 69 6e 65 64 20 75 73 69 6e 67 20 73 74 72 6c 65  ined using strle
3738f 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  n()..**.** The f
37390 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 2c 20  irst parameter, 
37391 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 69 73 20 70  pTokenizer, is p
37392 61 73 73 65 64 20 74 68 65 20 66 74 73 33 20 74  assed the fts3 t
37393 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64 75 6c 65 20  okenizer module 
37394 74 6f 0a 2a 2a 20 75 73 65 20 74 6f 20 6e 6f 72  to.** use to nor
37395 6d 61 6c 69 7a 65 20 71 75 65 72 79 20 74 6f 6b  malize query tok
37396 65 6e 73 20 77 68 69 6c 65 20 70 61 72 73 69 6e  ens while parsin
37397 67 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  g the expression
37398 2e 20 54 68 65 20 61 7a 43 6f 6c 5b 5d 0a 2a 2a  . The azCol[].**
37399 20 61 72 72 61 79 2c 20 77 68 69 63 68 20 69 73   array, which is
3739a 20 61 73 73 75 6d 65 64 20 74 6f 20 63 6f 6e 74   assumed to cont
3739b 61 69 6e 20 6e 43 6f 6c 20 65 6e 74 72 69 65 73  ain nCol entries
3739c 2c 20 73 68 6f 75 6c 64 20 63 6f 6e 74 61 69 6e  , should contain
3739d 20 74 68 65 20 6e 61 6d 65 73 0a 2a 2a 20 6f 66   the names.** of
3739e 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 69 6e 20   each column in 
3739f 74 68 65 20 74 61 72 67 65 74 20 66 74 73 33 20  the target fts3 
373a0 74 61 62 6c 65 2c 20 69 6e 20 6f 72 64 65 72 20  table, in order 
373a1 66 72 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67  from left to rig
373a2 68 74 2e 20 0a 2a 2a 20 43 6f 6c 75 6d 6e 20 6e  ht. .** Column n
373a3 61 6d 65 73 20 6d 75 73 74 20 62 65 20 6e 75 6c  ames must be nul
373a4 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69  -terminated stri
373a5 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ngs..**.** The i
373a6 44 65 66 61 75 6c 74 43 6f 6c 20 70 61 72 61 6d  DefaultCol param
373a7 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 20 70  eter should be p
373a8 61 73 73 65 64 20 74 68 65 20 69 6e 64 65 78 20  assed the index 
373a9 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c  of the table col
373aa 75 6d 6e 0a 2a 2a 20 74 68 61 74 20 61 70 70 65  umn.** that appe
373ab 61 72 73 20 6f 6e 20 74 68 65 20 6c 65 66 74 2d  ars on the left-
373ac 68 61 6e 64 2d 73 69 64 65 20 6f 66 20 74 68 65  hand-side of the
373ad 20 4d 41 54 43 48 20 6f 70 65 72 61 74 6f 72 20   MATCH operator 
373ae 28 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20  (the default.** 
373af 63 6f 6c 75 6d 6e 20 74 6f 20 6d 61 74 63 68 20  column to match 
373b0 61 67 61 69 6e 73 74 20 66 6f 72 20 74 6f 6b 65  against for toke
373b1 6e 73 20 66 6f 72 20 77 68 69 63 68 20 61 20 63  ns for which a c
373b2 6f 6c 75 6d 6e 20 6e 61 6d 65 20 69 73 20 6e 6f  olumn name is no
373b3 74 20 65 78 70 6c 69 63 69 74 6c 79 0a 2a 2a 20  t explicitly.** 
373b4 73 70 65 63 69 66 69 65 64 20 61 73 20 70 61 72  specified as par
373b5 74 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 73  t of the query s
373b6 74 72 69 6e 67 29 2c 20 6f 72 20 2d 31 20 69 66  tring), or -1 if
373b7 20 74 6f 6b 65 6e 73 20 6d 61 79 20 62 79 20 64   tokens may by d
373b8 65 66 61 75 6c 74 0a 2a 2a 20 6d 61 74 63 68 20  efault.** match 
373b9 61 6e 79 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e  any table column
373ba 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
373bb 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
373bc 74 73 33 45 78 70 72 50 61 72 73 65 28 0a 20 20  ts3ExprParse(.  
373bd 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
373be 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 20  r *pTokenizer,  
373bf 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72      /* Tokenizer
373c0 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 68 61   module */.  cha
373c1 72 20 2a 2a 61 7a 43 6f 6c 2c 20 20 20 20 20 20  r **azCol,      
373c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
373c3 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 63 6f 6c   /* Array of col
373c4 75 6d 6e 20 6e 61 6d 65 73 20 66 6f 72 20 66 74  umn names for ft
373c5 73 33 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  s3 table */.  in
373c6 74 20 6e 43 6f 6c 2c 20 20 20 20 20 20 20 20 20  t nCol,         
373c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
373c8 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
373c9 6e 74 72 69 65 73 20 69 6e 20 61 7a 43 6f 6c 5b  ntries in azCol[
373ca 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 44 65 66 61  ] */.  int iDefa
373cb 75 6c 74 43 6f 6c 2c 20 20 20 20 20 20 20 20 20  ultCol,         
373cc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65             /* De
373cd 66 61 75 6c 74 20 63 6f 6c 75 6d 6e 20 74 6f 20  fault column to 
373ce 71 75 65 72 79 20 2a 2f 0a 20 20 63 6f 6e 73 74  query */.  const
373cf 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c   char *z, int n,
373d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
373d1 2a 20 54 65 78 74 20 6f 66 20 4d 41 54 43 48 20  * Text of MATCH 
373d2 71 75 65 72 79 20 2a 2f 0a 20 20 46 74 73 33 45  query */.  Fts3E
373d3 78 70 72 20 2a 2a 70 70 45 78 70 72 20 20 20 20  xpr **ppExpr    
373d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
373d5 2a 20 4f 55 54 3a 20 50 61 72 73 65 64 20 71 75  * OUT: Parsed qu
373d6 65 72 79 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ery structure */
373d7 0a 29 7b 0a 20 20 69 6e 74 20 6e 50 61 72 73 65  .){.  int nParse
373d8 64 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 50  d;.  int rc;.  P
373d9 61 72 73 65 43 6f 6e 74 65 78 74 20 73 50 61 72  arseContext sPar
373da 73 65 3b 0a 20 20 73 50 61 72 73 65 2e 70 54 6f  se;.  sParse.pTo
373db 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e  kenizer = pToken
373dc 69 7a 65 72 3b 0a 20 20 73 50 61 72 73 65 2e 61  izer;.  sParse.a
373dd 7a 43 6f 6c 20 3d 20 28 63 6f 6e 73 74 20 63 68  zCol = (const ch
373de 61 72 20 2a 2a 29 61 7a 43 6f 6c 3b 0a 20 20 73  ar **)azCol;.  s
373df 50 61 72 73 65 2e 6e 43 6f 6c 20 3d 20 6e 43 6f  Parse.nCol = nCo
373e0 6c 3b 0a 20 20 73 50 61 72 73 65 2e 69 44 65 66  l;.  sParse.iDef
373e1 61 75 6c 74 43 6f 6c 20 3d 20 69 44 65 66 61 75  aultCol = iDefau
373e2 6c 74 43 6f 6c 3b 0a 20 20 73 50 61 72 73 65 2e  ltCol;.  sParse.
373e3 6e 4e 65 73 74 20 3d 20 30 3b 0a 20 20 69 66 28  nNest = 0;.  if(
373e4 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 70   z==0 ){.    *pp
373e5 45 78 70 72 20 3d 20 30 3b 0a 20 20 20 20 72 65  Expr = 0;.    re
373e6 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
373e7 20 20 7d 0a 20 20 69 66 28 20 6e 3c 30 20 29 7b    }.  if( n<0 ){
373e8 0a 20 20 20 20 6e 20 3d 20 73 74 72 6c 65 6e 28  .    n = strlen(
373e9 7a 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 66  z);.  }.  rc = f
373ea 74 73 33 45 78 70 72 50 61 72 73 65 28 26 73 50  ts3ExprParse(&sP
373eb 61 72 73 65 2c 20 7a 2c 20 6e 2c 20 70 70 45 78  arse, z, n, ppEx
373ec 70 72 2c 20 26 6e 50 61 72 73 65 64 29 3b 0a 0a  pr, &nParsed);..
373ed 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 6d    /* Check for m
373ee 69 73 6d 61 74 63 68 65 64 20 70 61 72 65 6e 74  ismatched parent
373ef 68 65 73 69 73 20 2a 2f 0a 20 20 69 66 28 20 72  hesis */.  if( r
373f0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
373f1 73 50 61 72 73 65 2e 6e 4e 65 73 74 20 29 7b 0a  sParse.nNest ){.
373f2 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
373f3 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74  ERROR;.    sqlit
373f4 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28 2a  e3Fts3ExprFree(*
373f5 70 70 45 78 70 72 29 3b 0a 20 20 20 20 2a 70 70  ppExpr);.    *pp
373f6 45 78 70 72 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  Expr = 0;.  }.. 
373f7 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
373f8 2a 0a 2a 2a 20 46 72 65 65 20 61 20 70 61 72 73  *.** Free a pars
373f9 65 64 20 66 74 73 33 20 71 75 65 72 79 20 65 78  ed fts3 query ex
373fa 70 72 65 73 73 69 6f 6e 20 61 6c 6c 6f 63 61 74  pression allocat
373fb 65 64 20 62 79 20 73 71 6c 69 74 65 33 46 74 73  ed by sqlite3Fts
373fc 33 45 78 70 72 50 61 72 73 65 28 29 2e 0a 2a 2f  3ExprParse()..*/
373fd 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
373fe 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33  void sqlite3Fts3
373ff 45 78 70 72 46 72 65 65 28 46 74 73 33 45 78 70  ExprFree(Fts3Exp
37400 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29  r *p){.  if( p )
37401 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  {.    sqlite3Fts
37402 33 45 78 70 72 46 72 65 65 28 70 2d 3e 70 4c 65  3ExprFree(p->pLe
37403 66 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ft);.    sqlite3
37404 46 74 73 33 45 78 70 72 46 72 65 65 28 70 2d 3e  Fts3ExprFree(p->
37405 70 52 69 67 68 74 29 3b 0a 20 20 20 20 73 71 6c  pRight);.    sql
37406 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20  ite3_free(p);.  
37407 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }.}../**********
37408 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37409 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3740a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3740b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3740c 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
3740d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3740e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3740f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37411 0a 2a 2a 20 45 76 65 72 79 74 68 69 6e 67 20 61  .** Everything a
37412 66 74 65 72 20 74 68 69 73 20 70 6f 69 6e 74 20  fter this point 
37413 69 73 20 6a 75 73 74 20 74 65 73 74 20 63 6f 64  is just test cod
37414 65 2e 0a 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51  e..*/..#ifdef SQ
37415 4c 49 54 45 5f 54 45 53 54 0a 0a 0a 2f 2a 0a 2a  LITE_TEST.../*.*
37416 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 71 75  * Function to qu
37417 65 72 79 20 74 68 65 20 68 61 73 68 2d 74 61 62  ery the hash-tab
37418 6c 65 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 72 73  le of tokenizers
37419 20 28 73 65 65 20 52 45 41 44 4d 45 2e 74 6f 6b   (see README.tok
3741a 65 6e 69 7a 65 72 73 29 2e 0a 2a 2f 0a 73 74 61  enizers)..*/.sta
3741b 74 69 63 20 69 6e 74 20 71 75 65 72 79 54 65 73  tic int queryTes
3741c 74 54 6f 6b 65 6e 69 7a 65 72 28 0a 20 20 73 71  tTokenizer(.  sq
3741d 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 63 6f  lite3 *db, .  co
3741e 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
3741f 20 20 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74    .  const sqlit
37420 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
37421 75 6c 65 20 2a 2a 70 70 0a 29 7b 0a 20 20 69 6e  ule **pp.){.  in
37422 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f  t rc;.  sqlite3_
37423 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 63  stmt *pStmt;.  c
37424 6f 6e 73 74 20 63 68 61 72 20 7a 53 71 6c 5b 5d  onst char zSql[]
37425 20 3d 20 22 53 45 4c 45 43 54 20 66 74 73 33 5f   = "SELECT fts3_
37426 74 6f 6b 65 6e 69 7a 65 72 28 3f 29 22 3b 0a 0a  tokenizer(?)";..
37427 20 20 2a 70 70 20 3d 20 30 3b 0a 20 20 72 63 20    *pp = 0;.  rc 
37428 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  = sqlite3_prepar
37429 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d  e_v2(db, zSql, -
3742a 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20  1, &pStmt, 0);. 
3742b 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
3742c 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
3742d 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69   rc;.  }..  sqli
3742e 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 70 53  te3_bind_text(pS
3742f 74 6d 74 2c 20 31 2c 20 7a 4e 61 6d 65 2c 20 2d  tmt, 1, zName, -
37430 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  1, SQLITE_STATIC
37431 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  );.  if( SQLITE_
37432 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65  ROW==sqlite3_ste
37433 70 28 70 53 74 6d 74 29 20 29 7b 0a 20 20 20 20  p(pStmt) ){.    
37434 69 66 28 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  if( sqlite3_colu
37435 6d 6e 5f 74 79 70 65 28 70 53 74 6d 74 2c 20 30  mn_type(pStmt, 0
37436 29 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29  )==SQLITE_BLOB )
37437 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70  {.      memcpy(p
37438 70 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  p, sqlite3_colum
37439 6e 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 30 29  n_blob(pStmt, 0)
3743a 2c 20 73 69 7a 65 6f 66 28 2a 70 70 29 29 3b 0a  , sizeof(*pp));.
3743b 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
3743c 75 72 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  urn sqlite3_fina
3743d 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a  lize(pStmt);.}..
3743e 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
3743f 69 6f 6e 20 69 73 20 70 61 72 74 20 6f 66 20 74  ion is part of t
37440 68 65 20 74 65 73 74 20 69 6e 74 65 72 66 61 63  he test interfac
37441 65 20 66 6f 72 20 74 68 65 20 71 75 65 72 79 20  e for the query 
37442 70 61 72 73 65 72 2e 20 49 74 0a 2a 2a 20 77 72  parser. It.** wr
37443 69 74 65 73 20 61 20 74 65 78 74 20 72 65 70 72  ites a text repr
37444 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  esentation of th
37445 65 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69  e query expressi
37446 6f 6e 20 70 45 78 70 72 20 69 6e 74 6f 20 74 68  on pExpr into th
37447 65 0a 2a 2a 20 62 75 66 66 65 72 20 70 6f 69 6e  e.** buffer poin
37448 74 65 64 20 74 6f 20 62 79 20 61 72 67 75 6d 65  ted to by argume
37449 6e 74 20 7a 42 75 66 2e 20 49 74 20 69 73 20 61  nt zBuf. It is a
3744a 73 73 75 6d 65 64 20 74 68 61 74 20 7a 42 75 66  ssumed that zBuf
3744b 20 69 73 20 6c 61 72 67 65 20 0a 2a 2a 20 65 6e   is large .** en
3744c 6f 75 67 68 20 74 6f 20 73 74 6f 72 65 20 74 68  ough to store th
3744d 65 20 72 65 71 75 69 72 65 64 20 74 65 78 74 20  e required text 
3744e 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a  representation..
3744f 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65  */.static void e
37450 78 70 72 54 6f 53 74 72 69 6e 67 28 46 74 73 33  xprToString(Fts3
37451 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61  Expr *pExpr, cha
37452 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 77 69 74  r *zBuf){.  swit
37453 63 68 28 20 70 45 78 70 72 2d 3e 65 54 79 70 65  ch( pExpr->eType
37454 20 29 7b 0a 20 20 20 20 63 61 73 65 20 46 54 53   ){.    case FTS
37455 51 55 45 52 59 5f 50 48 52 41 53 45 3a 20 7b 0a  QUERY_PHRASE: {.
37456 20 20 20 20 20 20 46 74 73 33 50 68 72 61 73 65        Fts3Phrase
37457 20 2a 70 50 68 72 61 73 65 20 3d 20 70 45 78 70   *pPhrase = pExp
37458 72 2d 3e 70 50 68 72 61 73 65 3b 0a 20 20 20 20  r->pPhrase;.    
37459 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 7a    int i;.      z
3745a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a  Buf += sprintf(z
3745b 42 75 66 2c 20 22 50 48 52 41 53 45 20 25 64 20  Buf, "PHRASE %d 
3745c 25 64 22 2c 20 70 50 68 72 61 73 65 2d 3e 69 43  %d", pPhrase->iC
3745d 6f 6c 75 6d 6e 2c 20 70 50 68 72 61 73 65 2d 3e  olumn, pPhrase->
3745e 69 73 4e 6f 74 29 3b 0a 20 20 20 20 20 20 66 6f  isNot);.      fo
3745f 72 28 69 3d 30 3b 20 69 3c 70 50 68 72 61 73 65  r(i=0; i<pPhrase
37460 2d 3e 6e 54 6f 6b 65 6e 3b 20 69 2b 2b 29 7b 0a  ->nToken; i++){.
37461 20 20 20 20 20 20 20 20 7a 42 75 66 20 2b 3d 20          zBuf += 
37462 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 22 20 25  sprintf(zBuf," %
37463 2e 2a 73 22 2c 70 50 68 72 61 73 65 2d 3e 61 54  .*s",pPhrase->aT
37464 6f 6b 65 6e 5b 69 5d 2e 6e 2c 70 50 68 72 61 73  oken[i].n,pPhras
37465 65 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 2e 7a 29 3b  e->aToken[i].z);
37466 0a 20 20 20 20 20 20 20 20 7a 42 75 66 20 2b 3d  .        zBuf +=
37467 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 22 25   sprintf(zBuf,"%
37468 73 22 2c 20 28 70 50 68 72 61 73 65 2d 3e 61 54  s", (pPhrase->aT
37469 6f 6b 65 6e 5b 69 5d 2e 69 73 50 72 65 66 69 78  oken[i].isPrefix
3746a 3f 22 2b 22 3a 22 22 29 29 3b 0a 20 20 20 20 20  ?"+":""));.     
3746b 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b   }.      return;
3746c 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65  .    }..    case
3746d 20 46 54 53 51 55 45 52 59 5f 4e 45 41 52 3a 0a   FTSQUERY_NEAR:.
3746e 20 20 20 20 20 20 7a 42 75 66 20 2b 3d 20 73 70        zBuf += sp
3746f 72 69 6e 74 66 28 7a 42 75 66 2c 20 22 4e 45 41  rintf(zBuf, "NEA
37470 52 2f 25 64 20 22 2c 20 70 45 78 70 72 2d 3e 6e  R/%d ", pExpr->n
37471 4e 65 61 72 29 3b 0a 20 20 20 20 20 20 62 72 65  Near);.      bre
37472 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 46 54 53  ak;.    case FTS
37473 51 55 45 52 59 5f 4e 4f 54 3a 0a 20 20 20 20 20  QUERY_NOT:.     
37474 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74 66   zBuf += sprintf
37475 28 7a 42 75 66 2c 20 22 4e 4f 54 20 22 29 3b 0a  (zBuf, "NOT ");.
37476 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
37477 20 63 61 73 65 20 46 54 53 51 55 45 52 59 5f 41   case FTSQUERY_A
37478 4e 44 3a 0a 20 20 20 20 20 20 7a 42 75 66 20 2b  ND:.      zBuf +
37479 3d 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20  = sprintf(zBuf, 
3747a 22 41 4e 44 20 22 29 3b 0a 20 20 20 20 20 20 62  "AND ");.      b
3747b 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 46  reak;.    case F
3747c 54 53 51 55 45 52 59 5f 4f 52 3a 0a 20 20 20 20  TSQUERY_OR:.    
3747d 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74    zBuf += sprint
3747e 66 28 7a 42 75 66 2c 20 22 4f 52 20 22 29 3b 0a  f(zBuf, "OR ");.
3747f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d        break;.  }
37480 0a 0a 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69  ..  zBuf += spri
37481 6e 74 66 28 7a 42 75 66 2c 20 22 7b 22 29 3b 0a  ntf(zBuf, "{");.
37482 20 20 65 78 70 72 54 6f 53 74 72 69 6e 67 28 70    exprToString(p
37483 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 7a 42 75  Expr->pLeft, zBu
37484 66 29 3b 0a 20 20 7a 42 75 66 20 2b 3d 20 73 74  f);.  zBuf += st
37485 72 6c 65 6e 28 7a 42 75 66 29 3b 0a 20 20 7a 42  rlen(zBuf);.  zB
37486 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42  uf += sprintf(zB
37487 75 66 2c 20 22 7d 20 22 29 3b 0a 0a 20 20 7a 42  uf, "} ");..  zB
37488 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42  uf += sprintf(zB
37489 75 66 2c 20 22 7b 22 29 3b 0a 20 20 65 78 70 72  uf, "{");.  expr
3748a 54 6f 53 74 72 69 6e 67 28 70 45 78 70 72 2d 3e  ToString(pExpr->
3748b 70 52 69 67 68 74 2c 20 7a 42 75 66 29 3b 0a 20  pRight, zBuf);. 
3748c 20 7a 42 75 66 20 2b 3d 20 73 74 72 6c 65 6e 28   zBuf += strlen(
3748d 7a 42 75 66 29 3b 0a 20 20 7a 42 75 66 20 2b 3d  zBuf);.  zBuf +=
3748e 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20 22   sprintf(zBuf, "
3748f 7d 22 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  }");.}../*.** Th
37490 69 73 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d  is is the implem
37491 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 73 63  entation of a sc
37492 61 6c 61 72 20 53 51 4c 20 66 75 6e 63 74 69 6f  alar SQL functio
37493 6e 20 75 73 65 64 20 74 6f 20 74 65 73 74 20 74  n used to test t
37494 68 65 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  he .** expressio
37495 6e 20 70 61 72 73 65 72 2e 20 49 74 20 73 68 6f  n parser. It sho
37496 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 61 73  uld be called as
37497 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
37498 20 20 66 74 73 33 5f 65 78 70 72 74 65 73 74 28    fts3_exprtest(
37499 3c 74 6f 6b 65 6e 69 7a 65 72 3e 2c 20 3c 65 78  <tokenizer>, <ex
3749a 70 72 3e 2c 20 3c 63 6f 6c 75 6d 6e 20 31 3e 2c  pr>, <column 1>,
3749b 20 2e 2e 2e 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65   ...);.**.** The
3749c 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2c   first argument,
3749d 20 3c 74 6f 6b 65 6e 69 7a 65 72 3e 2c 20 69 73   <tokenizer>, is
3749e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
3749f 20 66 74 73 33 20 74 6f 6b 65 6e 69 7a 65 72 20   fts3 tokenizer 
374a0 75 73 65 64 0a 2a 2a 20 74 6f 20 70 61 72 73 65  used.** to parse
374a1 20 74 68 65 20 71 75 65 72 79 20 65 78 70 72 65   the query expre
374a2 73 73 69 6f 6e 20 28 73 65 65 20 52 45 41 44 4d  ssion (see READM
374a3 45 2e 74 6f 6b 65 6e 69 7a 65 72 73 29 2e 20 54  E.tokenizers). T
374a4 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
374a5 6e 74 0a 2a 2a 20 69 73 20 74 68 65 20 71 75 65  nt.** is the que
374a6 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f  ry expression to
374a7 20 70 61 72 73 65 2e 20 45 61 63 68 20 73 75 62   parse. Each sub
374a8 73 65 71 75 65 6e 74 20 61 72 67 75 6d 65 6e 74  sequent argument
374a9 20 69 73 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20   is the name.** 
374aa 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  of a column of t
374ab 68 65 20 66 74 73 33 20 74 61 62 6c 65 20 74 68  he fts3 table th
374ac 61 74 20 74 68 65 20 71 75 65 72 79 20 65 78 70  at the query exp
374ad 72 65 73 73 69 6f 6e 20 6d 61 79 20 72 65 66 65  ression may refe
374ae 72 20 74 6f 2e 0a 2a 2a 20 46 6f 72 20 65 78 61  r to..** For exa
374af 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45  mple:.**.**   SE
374b0 4c 45 43 54 20 66 74 73 33 5f 65 78 70 72 74 65  LECT fts3_exprte
374b1 73 74 28 27 73 69 6d 70 6c 65 27 2c 20 27 42 69  st('simple', 'Bi
374b2 6c 6c 20 63 6f 6c 32 3a 42 6c 6f 67 67 73 27 2c  ll col2:Bloggs',
374b3 20 27 63 6f 6c 31 27 2c 20 27 63 6f 6c 32 27 29   'col1', 'col2')
374b4 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.*/.static void
374b5 20 66 74 73 33 45 78 70 72 54 65 73 74 28 0a 20   fts3ExprTest(. 
374b6 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
374b7 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
374b8 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
374b9 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
374ba 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
374bb 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73  izer_module cons
374bc 74 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 30 3b 0a  t *pModule = 0;.
374bd 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
374be 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 20  zer *pTokenizer 
374bf 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  = 0;.  int rc;. 
374c0 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 20 3d 20   char **azCol = 
374c1 30 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  0;.  const char 
374c2 2a 7a 45 78 70 72 3b 0a 20 20 69 6e 74 20 6e 45  *zExpr;.  int nE
374c3 78 70 72 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b  xpr;.  int nCol;
374c4 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 46 74 73  .  int ii;.  Fts
374c5 33 45 78 70 72 20 2a 70 45 78 70 72 3b 0a 20 20  3Expr *pExpr;.  
374c6 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71  sqlite3 *db = sq
374c7 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
374c8 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29  _handle(context)
374c9 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3c 33 20  ;..  if( argc<3 
374ca 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
374cb 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74  esult_error(cont
374cc 65 78 74 2c 20 0a 20 20 20 20 20 20 20 20 22 55  ext, .        "U
374cd 73 61 67 65 3a 20 66 74 73 33 5f 65 78 70 72 74  sage: fts3_exprt
374ce 65 73 74 28 74 6f 6b 65 6e 69 7a 65 72 2c 20 65  est(tokenizer, e
374cf 78 70 72 2c 20 63 6f 6c 31 2c 20 2e 2e 2e 22 2c  xpr, col1, ...",
374d0 20 2d 31 0a 20 20 20 20 29 3b 0a 20 20 20 20 72   -1.    );.    r
374d1 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 72 63  eturn;.  }..  rc
374d2 20 3d 20 71 75 65 72 79 54 65 73 74 54 6f 6b 65   = queryTestToke
374d3 6e 69 7a 65 72 28 64 62 2c 0a 20 20 20 20 20 20  nizer(db,.      
374d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
374d5 20 20 20 20 28 63 6f 6e 73 74 20 63 68 61 72 20      (const char 
374d6 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
374d7 74 65 78 74 28 61 72 67 76 5b 30 5d 29 2c 20 26  text(argv[0]), &
374d8 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 69 66 28 20  pModule);.  if( 
374d9 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
374da 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
374db 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
374dc 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  em(context);.   
374dd 20 67 6f 74 6f 20 65 78 70 72 74 65 73 74 5f 6f   goto exprtest_o
374de 75 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ut;.  }else if( 
374df 21 70 4d 6f 64 75 6c 65 20 29 7b 0a 20 20 20 20  !pModule ){.    
374e0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
374e1 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 4e  rror(context, "N
374e2 6f 20 73 75 63 68 20 74 6f 6b 65 6e 69 7a 65 72  o such tokenizer
374e3 20 6d 6f 64 75 6c 65 22 2c 20 2d 31 29 3b 0a 20   module", -1);. 
374e4 20 20 20 67 6f 74 6f 20 65 78 70 72 74 65 73 74     goto exprtest
374e5 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 72 63 20  _out;.  }..  rc 
374e6 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 72 65 61  = pModule->xCrea
374e7 74 65 28 30 2c 20 30 2c 20 26 70 54 6f 6b 65 6e  te(0, 0, &pToken
374e8 69 7a 65 72 29 3b 0a 20 20 61 73 73 65 72 74 28  izer);.  assert(
374e9 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
374ea 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f  M || rc==SQLITE_
374eb 4f 4b 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  OK );.  if( rc==
374ec 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a  SQLITE_NOMEM ){.
374ed 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
374ee 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63  lt_error_nomem(c
374ef 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 67 6f 74  ontext);.    got
374f0 6f 20 65 78 70 72 74 65 73 74 5f 6f 75 74 3b 0a  o exprtest_out;.
374f1 20 20 7d 0a 20 20 70 54 6f 6b 65 6e 69 7a 65 72    }.  pTokenizer
374f2 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 4d 6f 64  ->pModule = pMod
374f3 75 6c 65 3b 0a 0a 20 20 7a 45 78 70 72 20 3d 20  ule;..  zExpr = 
374f4 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
374f5 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
374f6 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 6e 45 78  (argv[1]);.  nEx
374f7 70 72 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  pr = sqlite3_val
374f8 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 31 5d  ue_bytes(argv[1]
374f9 29 3b 0a 20 20 6e 43 6f 6c 20 3d 20 61 72 67 63  );.  nCol = argc
374fa 2d 32 3b 0a 20 20 61 7a 43 6f 6c 20 3d 20 28 63  -2;.  azCol = (c
374fb 68 61 72 20 2a 2a 29 73 71 6c 69 74 65 33 5f 6d  har **)sqlite3_m
374fc 61 6c 6c 6f 63 28 6e 43 6f 6c 2a 73 69 7a 65 6f  alloc(nCol*sizeo
374fd 66 28 63 68 61 72 20 2a 29 29 3b 0a 20 20 69 66  f(char *));.  if
374fe 28 20 21 61 7a 43 6f 6c 20 29 7b 0a 20 20 20 20  ( !azCol ){.    
374ff 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
37500 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65  rror_nomem(conte
37501 78 74 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78  xt);.    goto ex
37502 70 72 74 65 73 74 5f 6f 75 74 3b 0a 20 20 7d 0a  prtest_out;.  }.
37503 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e    for(ii=0; ii<n
37504 43 6f 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  Col; ii++){.    
37505 61 7a 43 6f 6c 5b 69 69 5d 20 3d 20 28 63 68 61  azCol[ii] = (cha
37506 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  r *)sqlite3_valu
37507 65 5f 74 65 78 74 28 61 72 67 76 5b 69 69 2b 32  e_text(argv[ii+2
37508 5d 29 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20  ]);.  }..  rc = 
37509 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 50  sqlite3Fts3ExprP
3750a 61 72 73 65 28 0a 20 20 20 20 20 20 70 54 6f 6b  arse(.      pTok
3750b 65 6e 69 7a 65 72 2c 20 61 7a 43 6f 6c 2c 20 6e  enizer, azCol, n
3750c 43 6f 6c 2c 20 6e 43 6f 6c 2c 20 7a 45 78 70 72  Col, nCol, zExpr
3750d 2c 20 6e 45 78 70 72 2c 20 26 70 45 78 70 72 0a  , nExpr, &pExpr.
3750e 20 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53    );.  if( rc==S
3750f 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20  QLITE_NOMEM ){. 
37510 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
37511 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f  t_error_nomem(co
37512 6e 74 65 78 74 29 3b 0a 20 20 20 20 67 6f 74 6f  ntext);.    goto
37513 20 65 78 70 72 74 65 73 74 5f 6f 75 74 3b 0a 20   exprtest_out;. 
37514 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53   }else if( rc==S
37515 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
37516 63 68 61 72 20 7a 42 75 66 5b 34 30 39 36 5d 3b  char zBuf[4096];
37517 0a 20 20 20 20 65 78 70 72 54 6f 53 74 72 69 6e  .    exprToStrin
37518 67 28 70 45 78 70 72 2c 20 7a 42 75 66 29 3b 0a  g(pExpr, zBuf);.
37519 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
3751a 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
3751b 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54   zBuf, -1, SQLIT
3751c 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  E_TRANSIENT);.  
3751d 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70    sqlite3Fts3Exp
3751e 72 46 72 65 65 28 70 45 78 70 72 29 3b 0a 20 20  rFree(pExpr);.  
3751f 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
37520 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
37521 63 6f 6e 74 65 78 74 2c 20 22 45 72 72 6f 72 20  context, "Error 
37522 70 61 72 73 69 6e 67 20 65 78 70 72 65 73 73 69  parsing expressi
37523 6f 6e 22 2c 20 2d 31 29 3b 0a 20 20 7d 0a 0a 65  on", -1);.  }..e
37524 78 70 72 74 65 73 74 5f 6f 75 74 3a 0a 20 20 69  xprtest_out:.  i
37525 66 28 20 70 4d 6f 64 75 6c 65 20 26 26 20 70 54  f( pModule && pT
37526 6f 6b 65 6e 69 7a 65 72 20 29 7b 0a 20 20 20 20  okenizer ){.    
37527 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 44  rc = pModule->xD
37528 65 73 74 72 6f 79 28 70 54 6f 6b 65 6e 69 7a 65  estroy(pTokenize
37529 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  r);.  }.  sqlite
3752a 33 5f 66 72 65 65 28 61 7a 43 6f 6c 29 3b 0a 7d  3_free(azCol);.}
3752b 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  ../*.** Register
3752c 20 74 68 65 20 71 75 65 72 79 20 65 78 70 72 65   the query expre
3752d 73 73 69 6f 6e 20 70 61 72 73 65 72 20 74 65 73  ssion parser tes
3752e 74 20 66 75 6e 63 74 69 6f 6e 20 66 74 73 33 5f  t function fts3_
3752f 65 78 70 72 74 65 73 74 28 29 20 0a 2a 2a 20 77  exprtest() .** w
37530 69 74 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ith database con
37531 6e 65 63 74 69 6f 6e 20 64 62 2e 20 0a 2a 2f 0a  nection db. .*/.
37532 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
37533 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 45  oid sqlite3Fts3E
37534 78 70 72 49 6e 69 74 54 65 73 74 49 6e 74 65 72  xprInitTestInter
37535 66 61 63 65 28 73 71 6c 69 74 65 33 2a 20 64 62  face(sqlite3* db
37536 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65  ){.  sqlite3_cre
37537 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20  ate_function(.  
37538 20 20 20 20 64 62 2c 20 22 66 74 73 33 5f 65 78      db, "fts3_ex
37539 70 72 74 65 73 74 22 2c 20 2d 31 2c 20 53 51 4c  prtest", -1, SQL
3753a 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 66 74 73  ITE_UTF8, 0, fts
3753b 33 45 78 70 72 54 65 73 74 2c 20 30 2c 20 30 0a  3ExprTest, 0, 0.
3753c 20 20 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 0a 23    );.}..#endif.#
3753d 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
3753e 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c  d(SQLITE_CORE) |
3753f 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
37540 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f  _ENABLE_FTS3) */
37541 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
37542 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 65 78  * End of fts3_ex
37543 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  pr.c ***********
37544 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37545 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37546 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
37547 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73  * Begin file fts
37548 33 5f 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a  3_hash.c *******
37549 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3754a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3754b 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
3754c 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20  tember 22.**.** 
3754d 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
3754e 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
3754f 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
37550 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
37551 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
37552 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
37553 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
37554 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
37555 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
37556 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
37557 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
37558 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
37559 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
3755a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
3755b 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
3755c 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
3755d 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
3755e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3755f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37561 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37562 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
37563 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e   is the implemen
37564 74 61 74 69 6f 6e 20 6f 66 20 67 65 6e 65 72 69  tation of generi
37565 63 20 68 61 73 68 2d 74 61 62 6c 65 73 20 75 73  c hash-tables us
37566 65 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a  ed in SQLite..**
37567 20 57 65 27 76 65 20 6d 6f 64 69 66 69 65 64 20   We've modified 
37568 69 74 20 73 6c 69 67 68 74 6c 79 20 74 6f 20 73  it slightly to s
37569 65 72 76 65 20 61 73 20 61 20 73 74 61 6e 64 61  erve as a standa
3756a 6c 6f 6e 65 20 68 61 73 68 20 74 61 62 6c 65 0a  lone hash table.
3756b 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
3756c 6e 20 66 6f 72 20 74 68 65 20 66 75 6c 6c 2d 74  n for the full-t
3756d 65 78 74 20 69 6e 64 65 78 69 6e 67 20 6d 6f 64  ext indexing mod
3756e 75 6c 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  ule..*/../*.** T
3756f 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20  he code in this 
37570 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63 6f 6d  file is only com
37571 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20  piled if:.**.** 
37572 20 20 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d      * The FTS3 m
37573 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62  odule is being b
37574 75 69 6c 74 20 61 73 20 61 6e 20 65 78 74 65 6e  uilt as an exten
37575 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 28 69  sion.**       (i
37576 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c  n which case SQL
37577 49 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f 74 20  ITE_CORE is not 
37578 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a 2a 2a 0a  defined), or.**.
37579 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53  **     * The FTS
3757a 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e  3 module is bein
3757b 67 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 65  g built into the
3757c 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20 20 20 20   core of.**     
3757d 20 20 53 51 4c 69 74 65 20 28 69 6e 20 77 68 69    SQLite (in whi
3757e 63 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f 45  ch case SQLITE_E
3757f 4e 41 42 4c 45 5f 46 54 53 33 20 69 73 20 64 65  NABLE_FTS3 is de
37580 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69 66 20 21  fined)..*/.#if !
37581 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43  defined(SQLITE_C
37582 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  ORE) || defined(
37583 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
37584 53 33 29 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c  S3)..../*.** Mal
37585 6c 6f 63 20 61 6e 64 20 46 72 65 65 20 66 75 6e  loc and Free fun
37586 63 74 69 6f 6e 73 0a 2a 2f 0a 73 74 61 74 69 63  ctions.*/.static
37587 20 76 6f 69 64 20 2a 66 74 73 33 48 61 73 68 4d   void *fts3HashM
37588 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20  alloc(int n){.  
37589 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65  void *p = sqlite
3758a 33 5f 6d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69  3_malloc(n);.  i
3758b 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73  f( p ){.    mems
3758c 65 74 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d  et(p, 0, n);.  }
3758d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 73  .  return p;.}.s
3758e 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 48  tatic void fts3H
3758f 61 73 68 46 72 65 65 28 76 6f 69 64 20 2a 70 29  ashFree(void *p)
37590 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  {.  sqlite3_free
37591 28 70 29 3b 0a 7d 0a 0a 2f 2a 20 54 75 72 6e 20  (p);.}../* Turn 
37592 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f  bulk memory into
37593 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f 62   a hash table ob
37594 6a 65 63 74 20 62 79 20 69 6e 69 74 69 61 6c 69  ject by initiali
37595 7a 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 65 6c  zing the.** fiel
37596 64 73 20 6f 66 20 74 68 65 20 48 61 73 68 20 73  ds of the Hash s
37597 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
37598 22 70 4e 65 77 22 20 69 73 20 61 20 70 6f 69 6e  "pNew" is a poin
37599 74 65 72 20 74 6f 20 74 68 65 20 68 61 73 68 20  ter to the hash 
3759a 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 74 6f  table that is to
3759b 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e   be initialized.
3759c 0a 2a 2a 20 6b 65 79 43 6c 61 73 73 20 69 73 20  .** keyClass is 
3759d 6f 6e 65 20 6f 66 20 74 68 65 20 63 6f 6e 73 74  one of the const
3759e 61 6e 74 73 20 0a 2a 2a 20 46 54 53 33 5f 48 41  ants .** FTS3_HA
3759f 53 48 5f 42 49 4e 41 52 59 20 6f 72 20 46 54 53  SH_BINARY or FTS
375a0 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 2e 20 20  3_HASH_STRING.  
375a1 54 68 65 20 76 61 6c 75 65 20 6f 66 20 6b 65 79  The value of key
375a2 43 6c 61 73 73 20 0a 2a 2a 20 64 65 74 65 72 6d  Class .** determ
375a3 69 6e 65 73 20 77 68 61 74 20 6b 69 6e 64 20 6f  ines what kind o
375a4 66 20 6b 65 79 20 74 68 65 20 68 61 73 68 20 74  f key the hash t
375a5 61 62 6c 65 20 77 69 6c 6c 20 75 73 65 2e 20 20  able will use.  
375a6 22 63 6f 70 79 4b 65 79 22 20 69 73 0a 2a 2a 20  "copyKey" is.** 
375a7 74 72 75 65 20 69 66 20 74 68 65 20 68 61 73 68  true if the hash
375a8 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 20 6d 61   table should ma
375a9 6b 65 20 69 74 73 20 6f 77 6e 20 70 72 69 76 61  ke its own priva
375aa 74 65 20 63 6f 70 79 20 6f 66 20 6b 65 79 73 20  te copy of keys 
375ab 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20  and.** false if 
375ac 69 74 20 73 68 6f 75 6c 64 20 6a 75 73 74 20 75  it should just u
375ad 73 65 20 74 68 65 20 73 75 70 70 6c 69 65 64 20  se the supplied 
375ae 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49  pointer..*/.SQLI
375af 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
375b0 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49  sqlite3Fts3HashI
375b1 6e 69 74 28 66 74 73 33 48 61 73 68 20 2a 70 4e  nit(fts3Hash *pN
375b2 65 77 2c 20 69 6e 74 20 6b 65 79 43 6c 61 73 73  ew, int keyClass
375b3 2c 20 69 6e 74 20 63 6f 70 79 4b 65 79 29 7b 0a  , int copyKey){.
375b4 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 21 3d    assert( pNew!=
375b5 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6b  0 );.  assert( k
375b6 65 79 43 6c 61 73 73 3e 3d 46 54 53 33 5f 48 41  eyClass>=FTS3_HA
375b7 53 48 5f 53 54 52 49 4e 47 20 26 26 20 6b 65 79  SH_STRING && key
375b8 43 6c 61 73 73 3c 3d 46 54 53 33 5f 48 41 53 48  Class<=FTS3_HASH
375b9 5f 42 49 4e 41 52 59 20 29 3b 0a 20 20 70 4e 65  _BINARY );.  pNe
375ba 77 2d 3e 6b 65 79 43 6c 61 73 73 20 3d 20 6b 65  w->keyClass = ke
375bb 79 43 6c 61 73 73 3b 0a 20 20 70 4e 65 77 2d 3e  yClass;.  pNew->
375bc 63 6f 70 79 4b 65 79 20 3d 20 63 6f 70 79 4b 65  copyKey = copyKe
375bd 79 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 72 73 74  y;.  pNew->first
375be 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f   = 0;.  pNew->co
375bf 75 6e 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d  unt = 0;.  pNew-
375c0 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 70  >htsize = 0;.  p
375c1 4e 65 77 2d 3e 68 74 20 3d 20 30 3b 0a 7d 0a 0a  New->ht = 0;.}..
375c2 2f 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 65 6e  /* Remove all en
375c3 74 72 69 65 73 20 66 72 6f 6d 20 61 20 68 61 73  tries from a has
375c4 68 20 74 61 62 6c 65 2e 20 20 52 65 63 6c 61 69  h table.  Reclai
375c5 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a  m all memory..**
375c6 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69   Call this routi
375c7 6e 65 20 74 6f 20 64 65 6c 65 74 65 20 61 20 68  ne to delete a h
375c8 61 73 68 20 74 61 62 6c 65 20 6f 72 20 74 6f 20  ash table or to 
375c9 72 65 73 65 74 20 61 20 68 61 73 68 20 74 61 62  reset a hash tab
375ca 6c 65 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6d 70  le.** to the emp
375cb 74 79 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c  ty state..*/.SQL
375cc 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
375cd 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
375ce 43 6c 65 61 72 28 66 74 73 33 48 61 73 68 20 2a  Clear(fts3Hash *
375cf 70 48 29 7b 0a 20 20 66 74 73 33 48 61 73 68 45  pH){.  fts3HashE
375d0 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20  lem *elem;      
375d1 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e     /* For loopin
375d2 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d 65  g over all eleme
375d3 6e 74 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65  nts of the table
375d4 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
375d5 48 21 3d 30 20 29 3b 0a 20 20 65 6c 65 6d 20 3d  H!=0 );.  elem =
375d6 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 70 48   pH->first;.  pH
375d7 2d 3e 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 66  ->first = 0;.  f
375d8 74 73 33 48 61 73 68 46 72 65 65 28 70 48 2d 3e  ts3HashFree(pH->
375d9 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20  ht);.  pH->ht = 
375da 30 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a 65 20  0;.  pH->htsize 
375db 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 65 6c  = 0;.  while( el
375dc 65 6d 20 29 7b 0a 20 20 20 20 66 74 73 33 48 61  em ){.    fts3Ha
375dd 73 68 45 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c 65  shElem *next_ele
375de 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a  m = elem->next;.
375df 20 20 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79      if( pH->copy
375e0 4b 65 79 20 26 26 20 65 6c 65 6d 2d 3e 70 4b 65  Key && elem->pKe
375e1 79 20 29 7b 0a 20 20 20 20 20 20 66 74 73 33 48  y ){.      fts3H
375e2 61 73 68 46 72 65 65 28 65 6c 65 6d 2d 3e 70 4b  ashFree(elem->pK
375e3 65 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  ey);.    }.    f
375e4 74 73 33 48 61 73 68 46 72 65 65 28 65 6c 65 6d  ts3HashFree(elem
375e5 29 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 6e 65  );.    elem = ne
375e6 78 74 5f 65 6c 65 6d 3b 0a 20 20 7d 0a 20 20 70  xt_elem;.  }.  p
375e7 48 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 7d 0a  H->count = 0;.}.
375e8 0a 2f 2a 0a 2a 2a 20 48 61 73 68 20 61 6e 64 20  ./*.** Hash and 
375e9 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74  comparison funct
375ea 69 6f 6e 73 20 77 68 65 6e 20 74 68 65 20 6d 6f  ions when the mo
375eb 64 65 20 69 73 20 46 54 53 33 5f 48 41 53 48 5f  de is FTS3_HASH_
375ec 53 54 52 49 4e 47 0a 2a 2f 0a 73 74 61 74 69 63  STRING.*/.static
375ed 20 69 6e 74 20 66 74 73 33 53 74 72 48 61 73 68   int fts3StrHash
375ee 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65  (const void *pKe
375ef 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20  y, int nKey){.  
375f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20  const char *z = 
375f1 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 4b  (const char *)pK
375f2 65 79 3b 0a 20 20 69 6e 74 20 68 20 3d 20 30 3b  ey;.  int h = 0;
375f3 0a 20 20 69 66 28 20 6e 4b 65 79 3c 3d 30 20 29  .  if( nKey<=0 )
375f4 20 6e 4b 65 79 20 3d 20 28 69 6e 74 29 20 73 74   nKey = (int) st
375f5 72 6c 65 6e 28 7a 29 3b 0a 20 20 77 68 69 6c 65  rlen(z);.  while
375f6 28 20 6e 4b 65 79 20 3e 20 30 20 20 29 7b 0a 20  ( nKey > 0  ){. 
375f7 20 20 20 68 20 3d 20 28 68 3c 3c 33 29 20 5e 20     h = (h<<3) ^ 
375f8 68 20 5e 20 2a 7a 2b 2b 3b 0a 20 20 20 20 6e 4b  h ^ *z++;.    nK
375f9 65 79 2d 2d 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ey--;.  }.  retu
375fa 72 6e 20 68 20 26 20 30 78 37 66 66 66 66 66 66  rn h & 0x7ffffff
375fb 66 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  f;.}.static int 
375fc 66 74 73 33 53 74 72 43 6f 6d 70 61 72 65 28 63  fts3StrCompare(c
375fd 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31  onst void *pKey1
375fe 2c 20 69 6e 74 20 6e 31 2c 20 63 6f 6e 73 74 20  , int n1, const 
375ff 76 6f 69 64 20 2a 70 4b 65 79 32 2c 20 69 6e 74  void *pKey2, int
37600 20 6e 32 29 7b 0a 20 20 69 66 28 20 6e 31 21 3d   n2){.  if( n1!=
37601 6e 32 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  n2 ) return 1;. 
37602 20 72 65 74 75 72 6e 20 73 74 72 6e 63 6d 70 28   return strncmp(
37603 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 4b 65  (const char*)pKe
37604 79 31 2c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29  y1,(const char*)
37605 70 4b 65 79 32 2c 6e 31 29 3b 0a 7d 0a 0a 2f 2a  pKey2,n1);.}../*
37606 0a 2a 2a 20 48 61 73 68 20 61 6e 64 20 63 6f 6d  .** Hash and com
37607 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e  parison function
37608 73 20 77 68 65 6e 20 74 68 65 20 6d 6f 64 65 20  s when the mode 
37609 69 73 20 46 54 53 33 5f 48 41 53 48 5f 42 49 4e  is FTS3_HASH_BIN
3760a 41 52 59 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ARY.*/.static in
3760b 74 20 66 74 73 33 42 69 6e 48 61 73 68 28 63 6f  t fts3BinHash(co
3760c 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
3760d 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74  int nKey){.  int
3760e 20 68 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20   h = 0;.  const 
3760f 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e 73 74  char *z = (const
37610 20 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a 20 20   char *)pKey;.  
37611 77 68 69 6c 65 28 20 6e 4b 65 79 2d 2d 20 3e 20  while( nKey-- > 
37612 30 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c  0 ){.    h = (h<
37613 3c 33 29 20 5e 20 68 20 5e 20 2a 28 7a 2b 2b 29  <3) ^ h ^ *(z++)
37614 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68  ;.  }.  return h
37615 20 26 20 30 78 37 66 66 66 66 66 66 66 3b 0a 7d   & 0x7fffffff;.}
37616 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
37617 42 69 6e 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74  BinCompare(const
37618 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 69 6e   void *pKey1, in
37619 74 20 6e 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64  t n1, const void
3761a 20 2a 70 4b 65 79 32 2c 20 69 6e 74 20 6e 32 29   *pKey2, int n2)
3761b 7b 0a 20 20 69 66 28 20 6e 31 21 3d 6e 32 20 29  {.  if( n1!=n2 )
3761c 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 74   return 1;.  ret
3761d 75 72 6e 20 6d 65 6d 63 6d 70 28 70 4b 65 79 31  urn memcmp(pKey1
3761e 2c 70 4b 65 79 32 2c 6e 31 29 3b 0a 7d 0a 0a 2f  ,pKey2,n1);.}../
3761f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  *.** Return a po
37620 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 70 70  inter to the app
37621 72 6f 70 72 69 61 74 65 20 68 61 73 68 20 66 75  ropriate hash fu
37622 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 74 68 65  nction given the
37623 20 6b 65 79 20 63 6c 61 73 73 2e 0a 2a 2a 0a 2a   key class..**.*
37624 2a 20 54 68 65 20 43 20 73 79 6e 74 61 78 20 69  * The C syntax i
37625 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
37626 64 65 66 69 6e 69 74 69 6f 6e 20 6d 61 79 20 62  definition may b
37627 65 20 75 6e 66 61 6d 69 6c 61 72 20 74 6f 20 73  e unfamilar to s
37628 6f 6d 65 20 0a 2a 2a 20 70 72 6f 67 72 61 6d 6d  ome .** programm
37629 65 72 73 2c 20 73 6f 20 77 65 20 70 72 6f 76 69  ers, so we provi
3762a 64 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  de the following
3762b 20 61 64 64 69 74 69 6f 6e 61 6c 20 65 78 70 6c   additional expl
3762c 61 6e 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54  anation:.**.** T
3762d 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66  he name of the f
3762e 75 6e 63 74 69 6f 6e 20 69 73 20 22 66 74 73 48  unction is "ftsH
3762f 61 73 68 46 75 6e 63 74 69 6f 6e 22 2e 20 20 54  ashFunction".  T
37630 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 65  he function take
37631 73 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 70 61  s a.** single pa
37632 72 61 6d 65 74 65 72 20 22 6b 65 79 43 6c 61 73  rameter "keyClas
37633 73 22 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20  s".  The return 
37634 76 61 6c 75 65 20 6f 66 20 66 74 73 48 61 73 68  value of ftsHash
37635 46 75 6e 63 74 69 6f 6e 28 29 0a 2a 2a 20 69 73  Function().** is
37636 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
37637 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20  other function. 
37638 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 74   Specifically, t
37639 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a  he return value.
3763a 2a 2a 20 6f 66 20 66 74 73 48 61 73 68 46 75 6e  ** of ftsHashFun
3763b 63 74 69 6f 6e 28 29 20 69 73 20 61 20 70 6f 69  ction() is a poi
3763c 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69  nter to a functi
3763d 6f 6e 20 74 68 61 74 20 74 61 6b 65 73 20 74 77  on that takes tw
3763e 6f 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20  o parameters.** 
3763f 77 69 74 68 20 74 79 70 65 73 20 22 63 6f 6e 73  with types "cons
37640 74 20 76 6f 69 64 2a 22 20 61 6e 64 20 22 69 6e  t void*" and "in
37641 74 22 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61  t" and returns a
37642 6e 20 22 69 6e 74 22 2e 0a 2a 2f 0a 73 74 61 74  n "int"..*/.stat
37643 69 63 20 69 6e 74 20 28 2a 66 74 73 48 61 73 68  ic int (*ftsHash
37644 46 75 6e 63 74 69 6f 6e 28 69 6e 74 20 6b 65 79  Function(int key
37645 43 6c 61 73 73 29 29 28 63 6f 6e 73 74 20 76 6f  Class))(const vo
37646 69 64 2a 2c 69 6e 74 29 7b 0a 20 20 69 66 28 20  id*,int){.  if( 
37647 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53 33 5f 48  keyClass==FTS3_H
37648 41 53 48 5f 53 54 52 49 4e 47 20 29 7b 0a 20 20  ASH_STRING ){.  
37649 20 20 72 65 74 75 72 6e 20 26 66 74 73 33 53 74    return &fts3St
3764a 72 48 61 73 68 3b 0a 20 20 7d 65 6c 73 65 7b 0a  rHash;.  }else{.
3764b 20 20 20 20 61 73 73 65 72 74 28 20 6b 65 79 43      assert( keyC
3764c 6c 61 73 73 3d 3d 46 54 53 33 5f 48 41 53 48 5f  lass==FTS3_HASH_
3764d 42 49 4e 41 52 59 20 29 3b 0a 20 20 20 20 72 65  BINARY );.    re
3764e 74 75 72 6e 20 26 66 74 73 33 42 69 6e 48 61 73  turn &fts3BinHas
3764f 68 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  h;.  }.}../*.** 
37650 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
37651 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69   to the appropri
37652 61 74 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f  ate hash functio
37653 6e 20 67 69 76 65 6e 20 74 68 65 20 6b 65 79 20  n given the key 
37654 63 6c 61 73 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  class..**.** For
37655 20 68 65 6c 70 20 69 6e 20 69 6e 74 65 72 70 72   help in interpr
37656 65 74 65 64 20 74 68 65 20 6f 62 73 63 75 72 65  eted the obscure
37657 20 43 20 63 6f 64 65 20 69 6e 20 74 68 65 20 66   C code in the f
37658 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69  unction definiti
37659 6f 6e 2c 0a 2a 2a 20 73 65 65 20 74 68 65 20 68  on,.** see the h
3765a 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e  eader comment on
3765b 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 66 75   the previous fu
3765c 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  nction..*/.stati
3765d 63 20 69 6e 74 20 28 2a 66 74 73 43 6f 6d 70 61  c int (*ftsCompa
3765e 72 65 46 75 6e 63 74 69 6f 6e 28 69 6e 74 20 6b  reFunction(int k
3765f 65 79 43 6c 61 73 73 29 29 28 63 6f 6e 73 74 20  eyClass))(const 
37660 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
37661 76 6f 69 64 2a 2c 69 6e 74 29 7b 0a 20 20 69 66  void*,int){.  if
37662 28 20 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53 33  ( keyClass==FTS3
37663 5f 48 41 53 48 5f 53 54 52 49 4e 47 20 29 7b 0a  _HASH_STRING ){.
37664 20 20 20 20 72 65 74 75 72 6e 20 26 66 74 73 33      return &fts3
37665 53 74 72 43 6f 6d 70 61 72 65 3b 0a 20 20 7d 65  StrCompare;.  }e
37666 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
37667 20 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53 33 5f   keyClass==FTS3_
37668 48 41 53 48 5f 42 49 4e 41 52 59 20 29 3b 0a 20  HASH_BINARY );. 
37669 20 20 20 72 65 74 75 72 6e 20 26 66 74 73 33 42     return &fts3B
3766a 69 6e 43 6f 6d 70 61 72 65 3b 0a 20 20 7d 0a 7d  inCompare;.  }.}
3766b 0a 0a 2f 2a 20 4c 69 6e 6b 20 61 6e 20 65 6c 65  ../* Link an ele
3766c 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61  ment into the ha
3766d 73 68 20 74 61 62 6c 65 0a 2a 2f 0a 73 74 61 74  sh table.*/.stat
3766e 69 63 20 76 6f 69 64 20 66 74 73 33 48 61 73 68  ic void fts3Hash
3766f 49 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 0a 20  InsertElement(. 
37670 20 66 74 73 33 48 61 73 68 20 2a 70 48 2c 20 20   fts3Hash *pH,  
37671 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
37672 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74   complete hash t
37673 61 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  able */.  struct
37674 20 5f 66 74 73 33 68 74 20 2a 70 45 6e 74 72 79   _fts3ht *pEntry
37675 2c 20 20 2f 2a 20 54 68 65 20 65 6e 74 72 79 20  ,  /* The entry 
37676 69 6e 74 6f 20 77 68 69 63 68 20 70 4e 65 77 20  into which pNew 
37677 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20  is inserted */. 
37678 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 70   fts3HashElem *p
37679 4e 65 77 20 20 20 20 20 20 20 2f 2a 20 54 68 65  New       /* The
3767a 20 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 69   element to be i
3767b 6e 73 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20  nserted */.){.  
3767c 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 70 48  fts3HashElem *pH
3767d 65 61 64 3b 20 20 20 20 20 2f 2a 20 46 69 72 73  ead;     /* Firs
3767e 74 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64  t element alread
3767f 79 20 69 6e 20 70 45 6e 74 72 79 20 2a 2f 0a 20  y in pEntry */. 
37680 20 70 48 65 61 64 20 3d 20 70 45 6e 74 72 79 2d   pHead = pEntry-
37681 3e 63 68 61 69 6e 3b 0a 20 20 69 66 28 20 70 48  >chain;.  if( pH
37682 65 61 64 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d  ead ){.    pNew-
37683 3e 6e 65 78 74 20 3d 20 70 48 65 61 64 3b 0a 20  >next = pHead;. 
37684 20 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20     pNew->prev = 
37685 70 48 65 61 64 2d 3e 70 72 65 76 3b 0a 20 20 20  pHead->prev;.   
37686 20 69 66 28 20 70 48 65 61 64 2d 3e 70 72 65 76   if( pHead->prev
37687 20 29 7b 20 70 48 65 61 64 2d 3e 70 72 65 76 2d   ){ pHead->prev-
37688 3e 6e 65 78 74 20 3d 20 70 4e 65 77 3b 20 7d 0a  >next = pNew; }.
37689 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 20 20      else        
3768a 20 20 20 20 20 7b 20 70 48 2d 3e 66 69 72 73 74       { pH->first
3768b 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70   = pNew; }.    p
3768c 48 65 61 64 2d 3e 70 72 65 76 20 3d 20 70 4e 65  Head->prev = pNe
3768d 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  w;.  }else{.    
3768e 70 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48 2d  pNew->next = pH-
3768f 3e 66 69 72 73 74 3b 0a 20 20 20 20 69 66 28 20  >first;.    if( 
37690 70 48 2d 3e 66 69 72 73 74 20 29 7b 20 70 48 2d  pH->first ){ pH-
37691 3e 66 69 72 73 74 2d 3e 70 72 65 76 20 3d 20 70  >first->prev = p
37692 4e 65 77 3b 20 7d 0a 20 20 20 20 70 4e 65 77 2d  New; }.    pNew-
37693 3e 70 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 70  >prev = 0;.    p
37694 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 77 3b  H->first = pNew;
37695 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 2d 3e 63  .  }.  pEntry->c
37696 6f 75 6e 74 2b 2b 3b 0a 20 20 70 45 6e 74 72 79  ount++;.  pEntry
37697 2d 3e 63 68 61 69 6e 20 3d 20 70 4e 65 77 3b 0a  ->chain = pNew;.
37698 7d 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 20 74 68  }.../* Resize th
37699 65 20 68 61 73 68 20 74 61 62 6c 65 20 73 6f 20  e hash table so 
3769a 74 68 61 74 20 69 74 20 63 61 6e 74 61 69 6e 73  that it cantains
3769b 20 22 6e 65 77 5f 73 69 7a 65 22 20 62 75 63 6b   "new_size" buck
3769c 65 74 73 2e 0a 2a 2a 20 22 6e 65 77 5f 73 69 7a  ets..** "new_siz
3769d 65 22 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77  e" must be a pow
3769e 65 72 20 6f 66 20 32 2e 20 20 54 68 65 20 68 61  er of 2.  The ha
3769f 73 68 20 74 61 62 6c 65 20 6d 69 67 68 74 20 66  sh table might f
376a0 61 69 6c 20 0a 2a 2a 20 74 6f 20 72 65 73 69 7a  ail .** to resiz
376a1 65 20 69 66 20 73 71 6c 69 74 65 4d 61 6c 6c 6f  e if sqliteMallo
376a2 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74  c() fails..*/.st
376a3 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 52 65  atic void fts3Re
376a4 68 61 73 68 28 66 74 73 33 48 61 73 68 20 2a 70  hash(fts3Hash *p
376a5 48 2c 20 69 6e 74 20 6e 65 77 5f 73 69 7a 65 29  H, int new_size)
376a6 7b 0a 20 20 73 74 72 75 63 74 20 5f 66 74 73 33  {.  struct _fts3
376a7 68 74 20 2a 6e 65 77 5f 68 74 3b 20 20 20 20 20  ht *new_ht;     
376a8 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20       /* The new 
376a9 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20  hash table */.  
376aa 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 6c  fts3HashElem *el
376ab 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65 6d 3b 20  em, *next_elem; 
376ac 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20   /* For looping 
376ad 6f 76 65 72 20 65 78 69 73 74 69 6e 67 20 65 6c  over existing el
376ae 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20  ements */.  int 
376af 28 2a 78 48 61 73 68 29 28 63 6f 6e 73 74 20 76  (*xHash)(const v
376b0 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 20 2f 2a 20  oid*,int);   /* 
376b1 54 68 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f  The hash functio
376b2 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  n */..  assert( 
376b3 28 6e 65 77 5f 73 69 7a 65 20 26 20 28 6e 65 77  (new_size & (new
376b4 5f 73 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a  _size-1))==0 );.
376b5 20 20 6e 65 77 5f 68 74 20 3d 20 28 73 74 72 75    new_ht = (stru
376b6 63 74 20 5f 66 74 73 33 68 74 20 2a 29 66 74 73  ct _fts3ht *)fts
376b7 33 48 61 73 68 4d 61 6c 6c 6f 63 28 20 6e 65 77  3HashMalloc( new
376b8 5f 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72  _size*sizeof(str
376b9 75 63 74 20 5f 66 74 73 33 68 74 29 20 29 3b 0a  uct _fts3ht) );.
376ba 20 20 69 66 28 20 6e 65 77 5f 68 74 3d 3d 30 20    if( new_ht==0 
376bb 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 74 73 33  ) return;.  fts3
376bc 48 61 73 68 46 72 65 65 28 70 48 2d 3e 68 74 29  HashFree(pH->ht)
376bd 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20 6e 65 77  ;.  pH->ht = new
376be 5f 68 74 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a  _ht;.  pH->htsiz
376bf 65 20 3d 20 6e 65 77 5f 73 69 7a 65 3b 0a 20 20  e = new_size;.  
376c0 78 48 61 73 68 20 3d 20 66 74 73 48 61 73 68 46  xHash = ftsHashF
376c1 75 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43  unction(pH->keyC
376c2 6c 61 73 73 29 3b 0a 20 20 66 6f 72 28 65 6c 65  lass);.  for(ele
376c3 6d 3d 70 48 2d 3e 66 69 72 73 74 2c 20 70 48 2d  m=pH->first, pH-
376c4 3e 66 69 72 73 74 3d 30 3b 20 65 6c 65 6d 3b 20  >first=0; elem; 
376c5 65 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d  elem = next_elem
376c6 29 7b 0a 20 20 20 20 69 6e 74 20 68 20 3d 20 28  ){.    int h = (
376c7 2a 78 48 61 73 68 29 28 65 6c 65 6d 2d 3e 70 4b  *xHash)(elem->pK
376c8 65 79 2c 20 65 6c 65 6d 2d 3e 6e 4b 65 79 29 20  ey, elem->nKey) 
376c9 26 20 28 6e 65 77 5f 73 69 7a 65 2d 31 29 3b 0a  & (new_size-1);.
376ca 20 20 20 20 6e 65 78 74 5f 65 6c 65 6d 20 3d 20      next_elem = 
376cb 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20  elem->next;.    
376cc 66 74 73 33 48 61 73 68 49 6e 73 65 72 74 45 6c  fts3HashInsertEl
376cd 65 6d 65 6e 74 28 70 48 2c 20 26 6e 65 77 5f 68  ement(pH, &new_h
376ce 74 5b 68 5d 2c 20 65 6c 65 6d 29 3b 0a 20 20 7d  t[h], elem);.  }
376cf 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63  .}../* This func
376d0 74 69 6f 6e 20 28 66 6f 72 20 69 6e 74 65 72 6e  tion (for intern
376d1 61 6c 20 75 73 65 20 6f 6e 6c 79 29 20 6c 6f 63  al use only) loc
376d2 61 74 65 73 20 61 6e 20 65 6c 65 6d 65 6e 74 20  ates an element 
376d3 69 6e 20 61 6e 0a 2a 2a 20 68 61 73 68 20 74 61  in an.** hash ta
376d4 62 6c 65 20 74 68 61 74 20 6d 61 74 63 68 65 73  ble that matches
376d5 20 74 68 65 20 67 69 76 65 6e 20 6b 65 79 2e 20   the given key. 
376d6 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 74 68   The hash for th
376d7 69 73 20 6b 65 79 20 68 61 73 0a 2a 2a 20 61 6c  is key has.** al
376d8 72 65 61 64 79 20 62 65 65 6e 20 63 6f 6d 70 75  ready been compu
376d9 74 65 64 20 61 6e 64 20 69 73 20 70 61 73 73 65  ted and is passe
376da 64 20 61 73 20 74 68 65 20 34 74 68 20 70 61 72  d as the 4th par
376db 61 6d 65 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  ameter..*/.stati
376dc 63 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a  c fts3HashElem *
376dd 66 74 73 33 46 69 6e 64 45 6c 65 6d 65 6e 74 42  fts3FindElementB
376de 79 48 61 73 68 28 0a 20 20 63 6f 6e 73 74 20 66  yHash(.  const f
376df 74 73 33 48 61 73 68 20 2a 70 48 2c 20 2f 2a 20  ts3Hash *pH, /* 
376e0 54 68 65 20 70 48 20 74 6f 20 62 65 20 73 65 61  The pH to be sea
376e1 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  rched */.  const
376e2 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f   void *pKey,   /
376e3 2a 20 54 68 65 20 6b 65 79 20 77 65 20 61 72 65  * The key we are
376e4 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a   searching for *
376e5 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 0a 20 20  /.  int nKey,.  
376e6 69 6e 74 20 68 20 20 20 20 20 20 20 20 20 20 20  int h           
376e7 20 20 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20      /* The hash 
376e8 66 6f 72 20 74 68 69 73 20 6b 65 79 2e 20 2a 2f  for this key. */
376e9 0a 29 7b 0a 20 20 66 74 73 33 48 61 73 68 45 6c  .){.  fts3HashEl
376ea 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20  em *elem;       
376eb 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20       /* Used to 
376ec 6c 6f 6f 70 20 74 68 72 75 20 74 68 65 20 65 6c  loop thru the el
376ed 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20  ement list */.  
376ee 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20  int count;      
376ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
376f0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d  * Number of elem
376f1 65 6e 74 73 20 6c 65 66 74 20 74 6f 20 74 65 73  ents left to tes
376f2 74 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6f  t */.  int (*xCo
376f3 6d 70 61 72 65 29 28 63 6f 6e 73 74 20 76 6f 69  mpare)(const voi
376f4 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
376f5 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 63 6f 6d  d*,int);  /* com
376f6 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e  parison function
376f7 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48 2d 3e 68   */..  if( pH->h
376f8 74 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  t ){.    struct 
376f9 5f 66 74 73 33 68 74 20 2a 70 45 6e 74 72 79 20  _fts3ht *pEntry 
376fa 3d 20 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20  = &pH->ht[h];.  
376fb 20 20 65 6c 65 6d 20 3d 20 70 45 6e 74 72 79 2d    elem = pEntry-
376fc 3e 63 68 61 69 6e 3b 0a 20 20 20 20 63 6f 75 6e  >chain;.    coun
376fd 74 20 3d 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e  t = pEntry->coun
376fe 74 3b 0a 20 20 20 20 78 43 6f 6d 70 61 72 65 20  t;.    xCompare 
376ff 3d 20 66 74 73 43 6f 6d 70 61 72 65 46 75 6e 63  = ftsCompareFunc
37700 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61 73  tion(pH->keyClas
37701 73 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 63  s);.    while( c
37702 6f 75 6e 74 2d 2d 20 26 26 20 65 6c 65 6d 20 29  ount-- && elem )
37703 7b 0a 20 20 20 20 20 20 69 66 28 20 28 2a 78 43  {.      if( (*xC
37704 6f 6d 70 61 72 65 29 28 65 6c 65 6d 2d 3e 70 4b  ompare)(elem->pK
37705 65 79 2c 65 6c 65 6d 2d 3e 6e 4b 65 79 2c 70 4b  ey,elem->nKey,pK
37706 65 79 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b 20 0a  ey,nKey)==0 ){ .
37707 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 65          return e
37708 6c 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  lem;.      }.   
37709 20 20 20 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e     elem = elem->
3770a 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  next;.    }.  }.
3770b 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
3770c 2a 20 52 65 6d 6f 76 65 20 61 20 73 69 6e 67 6c  * Remove a singl
3770d 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65  e entry from the
3770e 20 68 61 73 68 20 74 61 62 6c 65 20 67 69 76 65   hash table give
3770f 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
37710 68 61 74 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 61  hat.** element a
37711 6e 64 20 61 20 68 61 73 68 20 6f 6e 20 74 68 65  nd a hash on the
37712 20 65 6c 65 6d 65 6e 74 27 73 20 6b 65 79 2e 0a   element's key..
37713 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
37714 74 73 33 52 65 6d 6f 76 65 45 6c 65 6d 65 6e 74  ts3RemoveElement
37715 42 79 48 61 73 68 28 0a 20 20 66 74 73 33 48 61  ByHash(.  fts3Ha
37716 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20 20  sh *pH,         
37717 2f 2a 20 54 68 65 20 70 48 20 63 6f 6e 74 61 69  /* The pH contai
37718 6e 69 6e 67 20 22 65 6c 65 6d 22 20 2a 2f 0a 20  ning "elem" */. 
37719 20 66 74 73 33 48 61 73 68 45 6c 65 6d 2a 20 65   fts3HashElem* e
3771a 6c 65 6d 2c 20 20 20 2f 2a 20 54 68 65 20 65 6c  lem,   /* The el
3771b 65 6d 65 6e 74 20 74 6f 20 62 65 20 72 65 6d 6f  ement to be remo
3771c 76 65 64 20 66 72 6f 6d 20 74 68 65 20 70 48 20  ved from the pH 
3771d 2a 2f 0a 20 20 69 6e 74 20 68 20 20 20 20 20 20  */.  int h      
3771e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61             /* Ha
3771f 73 68 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  sh value for the
37720 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20   element */.){. 
37721 20 73 74 72 75 63 74 20 5f 66 74 73 33 68 74 20   struct _fts3ht 
37722 2a 70 45 6e 74 72 79 3b 0a 20 20 69 66 28 20 65  *pEntry;.  if( e
37723 6c 65 6d 2d 3e 70 72 65 76 20 29 7b 0a 20 20 20  lem->prev ){.   
37724 20 65 6c 65 6d 2d 3e 70 72 65 76 2d 3e 6e 65 78   elem->prev->nex
37725 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 20  t = elem->next; 
37726 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 48  .  }else{.    pH
37727 2d 3e 66 69 72 73 74 20 3d 20 65 6c 65 6d 2d 3e  ->first = elem->
37728 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20  next;.  }.  if( 
37729 65 6c 65 6d 2d 3e 6e 65 78 74 20 29 7b 0a 20 20  elem->next ){.  
3772a 20 20 65 6c 65 6d 2d 3e 6e 65 78 74 2d 3e 70 72    elem->next->pr
3772b 65 76 20 3d 20 65 6c 65 6d 2d 3e 70 72 65 76 3b  ev = elem->prev;
3772c 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d 20  .  }.  pEntry = 
3772d 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 69 66  &pH->ht[h];.  if
3772e 28 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3d  ( pEntry->chain=
3772f 3d 65 6c 65 6d 20 29 7b 0a 20 20 20 20 70 45 6e  =elem ){.    pEn
37730 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 65 6c 65  try->chain = ele
37731 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 70  m->next;.  }.  p
37732 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a  Entry->count--;.
37733 20 20 69 66 28 20 70 45 6e 74 72 79 2d 3e 63 6f    if( pEntry->co
37734 75 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20 70 45  unt<=0 ){.    pE
37735 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 30 3b  ntry->chain = 0;
37736 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e 63  .  }.  if( pH->c
37737 6f 70 79 4b 65 79 20 26 26 20 65 6c 65 6d 2d 3e  opyKey && elem->
37738 70 4b 65 79 20 29 7b 0a 20 20 20 20 66 74 73 33  pKey ){.    fts3
37739 48 61 73 68 46 72 65 65 28 65 6c 65 6d 2d 3e 70  HashFree(elem->p
3773a 4b 65 79 29 3b 0a 20 20 7d 0a 20 20 66 74 73 33  Key);.  }.  fts3
3773b 48 61 73 68 46 72 65 65 28 20 65 6c 65 6d 20 29  HashFree( elem )
3773c 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2d 2d 3b  ;.  pH->count--;
3773d 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74  .  if( pH->count
3773e 3c 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  <=0 ){.    asser
3773f 74 28 20 70 48 2d 3e 66 69 72 73 74 3d 3d 30 20  t( pH->first==0 
37740 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
37741 48 2d 3e 63 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20  H->count==0 );. 
37742 20 20 20 66 74 73 33 48 61 73 68 43 6c 65 61 72     fts3HashClear
37743 28 70 48 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20  (pH);.  }.}../* 
37744 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 61 74  Attempt to locat
37745 65 20 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20  e an element of 
37746 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 70  the hash table p
37747 48 20 77 69 74 68 20 61 20 6b 65 79 0a 2a 2a 20  H with a key.** 
37748 74 68 61 74 20 6d 61 74 63 68 65 73 20 70 4b 65  that matches pKe
37749 79 2c 6e 4b 65 79 2e 20 20 52 65 74 75 72 6e 20  y,nKey.  Return 
3774a 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 69  the data for thi
3774b 73 20 65 6c 65 6d 65 6e 74 20 69 66 20 69 74 20  s element if it 
3774c 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 6f 72 20  is.** found, or 
3774d 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73  NULL if there is
3774e 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51   no match..*/.SQ
3774f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
37750 64 20 2a 73 71 6c 69 74 65 33 46 74 73 33 48 61  d *sqlite3Fts3Ha
37751 73 68 46 69 6e 64 28 63 6f 6e 73 74 20 66 74 73  shFind(const fts
37752 33 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74  3Hash *pH, const
37753 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74   void *pKey, int
37754 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 20 68 3b   nKey){.  int h;
37755 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37756 20 2f 2a 20 41 20 68 61 73 68 20 6f 6e 20 6b 65   /* A hash on ke
37757 79 20 2a 2f 0a 20 20 66 74 73 33 48 61 73 68 45  y */.  fts3HashE
37758 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 2f 2a  lem *elem;    /*
37759 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 68 61   The element tha
3775a 74 20 6d 61 74 63 68 65 73 20 6b 65 79 20 2a 2f  t matches key */
3775b 0a 20 20 69 6e 74 20 28 2a 78 48 61 73 68 29 28  .  int (*xHash)(
3775c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29  const void*,int)
3775d 3b 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66  ;  /* The hash f
3775e 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66  unction */..  if
3775f 28 20 70 48 3d 3d 30 20 7c 7c 20 70 48 2d 3e 68  ( pH==0 || pH->h
37760 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  t==0 ) return 0;
37761 0a 20 20 78 48 61 73 68 20 3d 20 66 74 73 48 61  .  xHash = ftsHa
37762 73 68 46 75 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b  shFunction(pH->k
37763 65 79 43 6c 61 73 73 29 3b 0a 20 20 61 73 73 65  eyClass);.  asse
37764 72 74 28 20 78 48 61 73 68 21 3d 30 20 29 3b 0a  rt( xHash!=0 );.
37765 20 20 68 20 3d 20 28 2a 78 48 61 73 68 29 28 70    h = (*xHash)(p
37766 4b 65 79 2c 6e 4b 65 79 29 3b 0a 20 20 61 73 73  Key,nKey);.  ass
37767 65 72 74 28 20 28 70 48 2d 3e 68 74 73 69 7a 65  ert( (pH->htsize
37768 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31   & (pH->htsize-1
37769 29 29 3d 3d 30 20 29 3b 0a 20 20 65 6c 65 6d 20  ))==0 );.  elem 
3776a 3d 20 66 74 73 33 46 69 6e 64 45 6c 65 6d 65 6e  = fts3FindElemen
3776b 74 42 79 48 61 73 68 28 70 48 2c 70 4b 65 79 2c  tByHash(pH,pKey,
3776c 6e 4b 65 79 2c 20 68 20 26 20 28 70 48 2d 3e 68  nKey, h & (pH->h
3776d 74 73 69 7a 65 2d 31 29 29 3b 0a 20 20 72 65 74  tsize-1));.  ret
3776e 75 72 6e 20 65 6c 65 6d 20 3f 20 65 6c 65 6d 2d  urn elem ? elem-
3776f 3e 64 61 74 61 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a  >data : 0;.}../*
37770 20 49 6e 73 65 72 74 20 61 6e 20 65 6c 65 6d 65   Insert an eleme
37771 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68  nt into the hash
37772 20 74 61 62 6c 65 20 70 48 2e 20 20 54 68 65 20   table pH.  The 
37773 6b 65 79 20 69 73 20 70 4b 65 79 2c 6e 4b 65 79  key is pKey,nKey
37774 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61  .** and the data
37775 20 69 73 20 22 64 61 74 61 22 2e 0a 2a 2a 0a 2a   is "data"..**.*
37776 2a 20 49 66 20 6e 6f 20 65 6c 65 6d 65 6e 74 20  * If no element 
37777 65 78 69 73 74 73 20 77 69 74 68 20 61 20 6d 61  exists with a ma
37778 74 63 68 69 6e 67 20 6b 65 79 2c 20 74 68 65 6e  tching key, then
37779 20 61 20 6e 65 77 0a 2a 2a 20 65 6c 65 6d 65 6e   a new.** elemen
3777a 74 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 41  t is created.  A
3777b 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6b 65 79   copy of the key
3777c 20 69 73 20 6d 61 64 65 20 69 66 20 74 68 65 20   is made if the 
3777d 63 6f 70 79 4b 65 79 0a 2a 2a 20 66 6c 61 67 20  copyKey.** flag 
3777e 69 73 20 73 65 74 2e 20 20 4e 55 4c 4c 20 69 73  is set.  NULL is
3777f 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
37780 20 49 66 20 61 6e 6f 74 68 65 72 20 65 6c 65 6d   If another elem
37781 65 6e 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  ent already exis
37782 74 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65  ts with the same
37783 20 6b 65 79 2c 20 74 68 65 6e 20 74 68 65 0a 2a   key, then the.*
37784 2a 20 6e 65 77 20 64 61 74 61 20 72 65 70 6c 61  * new data repla
37785 63 65 73 20 74 68 65 20 6f 6c 64 20 64 61 74 61  ces the old data
37786 20 61 6e 64 20 74 68 65 20 6f 6c 64 20 64 61 74   and the old dat
37787 61 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  a is returned..*
37788 2a 20 54 68 65 20 6b 65 79 20 69 73 20 6e 6f 74  * The key is not
37789 20 63 6f 70 69 65 64 20 69 6e 20 74 68 69 73 20   copied in this 
3778a 69 6e 73 74 61 6e 63 65 2e 20 20 49 66 20 61 20  instance.  If a 
3778b 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 74 68  malloc fails, th
3778c 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61  en.** the new da
3778d 74 61 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  ta is returned a
3778e 6e 64 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  nd the hash tabl
3778f 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a  e is unchanged..
37790 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 64 61  **.** If the "da
37791 74 61 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ta" parameter to
37792 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
37793 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65  s NULL, then the
37794 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 63 6f 72 72  .** element corr
37795 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 22 6b 65  esponding to "ke
37796 79 22 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72  y" is removed fr
37797 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  om the hash tabl
37798 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
37799 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
3779a 65 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74  e3Fts3HashInsert
3779b 28 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70 48  (.  fts3Hash *pH
3779c 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ,        /* The 
3779d 68 61 73 68 20 74 61 62 6c 65 20 74 6f 20 69 6e  hash table to in
3779e 73 65 72 74 20 69 6e 74 6f 20 2a 2f 0a 20 20 63  sert into */.  c
3779f 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c  onst void *pKey,
377a0 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 2a      /* The key *
377a1 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20  /.  int nKey,   
377a2 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
377a3 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
377a4 68 65 20 6b 65 79 20 2a 2f 0a 20 20 76 6f 69 64  he key */.  void
377a5 20 2a 64 61 74 61 20 20 20 20 20 20 20 20 20 20   *data          
377a6 20 2f 2a 20 54 68 65 20 64 61 74 61 20 2a 2f 0a   /* The data */.
377a7 29 7b 0a 20 20 69 6e 74 20 68 72 61 77 3b 20 20  ){.  int hraw;  
377a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
377a9 2a 20 52 61 77 20 68 61 73 68 20 76 61 6c 75 65  * Raw hash value
377aa 20 6f 66 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20   of the key */. 
377ab 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20   int h;         
377ac 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68             /* th
377ad 65 20 68 61 73 68 20 6f 66 20 74 68 65 20 6b 65  e hash of the ke
377ae 79 20 6d 6f 64 75 6c 6f 20 68 61 73 68 20 74 61  y modulo hash ta
377af 62 6c 65 20 73 69 7a 65 20 2a 2f 0a 20 20 66 74  ble size */.  ft
377b0 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d  s3HashElem *elem
377b1 3b 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20  ;       /* Used 
377b2 74 6f 20 6c 6f 6f 70 20 74 68 72 75 20 74 68 65  to loop thru the
377b3 20 65 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f   element list */
377b4 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20  .  fts3HashElem 
377b5 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a 20  *new_elem;   /* 
377b6 4e 65 77 20 65 6c 65 6d 65 6e 74 20 61 64 64 65  New element adde
377b7 64 20 74 6f 20 74 68 65 20 70 48 20 2a 2f 0a 20  d to the pH */. 
377b8 20 69 6e 74 20 28 2a 78 48 61 73 68 29 28 63 6f   int (*xHash)(co
377b9 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20  nst void*,int); 
377ba 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66 75 6e   /* The hash fun
377bb 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65  ction */..  asse
377bc 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 78  rt( pH!=0 );.  x
377bd 48 61 73 68 20 3d 20 66 74 73 48 61 73 68 46 75  Hash = ftsHashFu
377be 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c  nction(pH->keyCl
377bf 61 73 73 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ass);.  assert( 
377c0 78 48 61 73 68 21 3d 30 20 29 3b 0a 20 20 68 72  xHash!=0 );.  hr
377c1 61 77 20 3d 20 28 2a 78 48 61 73 68 29 28 70 4b  aw = (*xHash)(pK
377c2 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 61 73 73  ey, nKey);.  ass
377c3 65 72 74 28 20 28 70 48 2d 3e 68 74 73 69 7a 65  ert( (pH->htsize
377c4 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31   & (pH->htsize-1
377c5 29 29 3d 3d 30 20 29 3b 0a 20 20 68 20 3d 20 68  ))==0 );.  h = h
377c6 72 61 77 20 26 20 28 70 48 2d 3e 68 74 73 69 7a  raw & (pH->htsiz
377c7 65 2d 31 29 3b 0a 20 20 65 6c 65 6d 20 3d 20 66  e-1);.  elem = f
377c8 74 73 33 46 69 6e 64 45 6c 65 6d 65 6e 74 42 79  ts3FindElementBy
377c9 48 61 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65  Hash(pH,pKey,nKe
377ca 79 2c 68 29 3b 0a 20 20 69 66 28 20 65 6c 65 6d  y,h);.  if( elem
377cb 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c   ){.    void *ol
377cc 64 5f 64 61 74 61 20 3d 20 65 6c 65 6d 2d 3e 64  d_data = elem->d
377cd 61 74 61 3b 0a 20 20 20 20 69 66 28 20 64 61 74  ata;.    if( dat
377ce 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 74  a==0 ){.      ft
377cf 73 33 52 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 42  s3RemoveElementB
377d0 79 48 61 73 68 28 70 48 2c 65 6c 65 6d 2c 68 29  yHash(pH,elem,h)
377d1 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
377d2 20 20 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20     elem->data = 
377d3 64 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20  data;.    }.    
377d4 72 65 74 75 72 6e 20 6f 6c 64 5f 64 61 74 61 3b  return old_data;
377d5 0a 20 20 7d 0a 20 20 69 66 28 20 64 61 74 61 3d  .  }.  if( data=
377d6 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
377d7 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 3d   if( pH->htsize=
377d8 3d 30 20 29 7b 0a 20 20 20 20 66 74 73 33 52 65  =0 ){.    fts3Re
377d9 68 61 73 68 28 70 48 2c 38 29 3b 0a 20 20 20 20  hash(pH,8);.    
377da 69 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 3d 3d  if( pH->htsize==
377db 30 20 29 7b 0a 20 20 20 20 20 20 70 48 2d 3e 63  0 ){.      pH->c
377dc 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  ount = 0;.      
377dd 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 20  return data;.   
377de 20 7d 0a 20 20 7d 0a 20 20 6e 65 77 5f 65 6c 65   }.  }.  new_ele
377df 6d 20 3d 20 28 66 74 73 33 48 61 73 68 45 6c 65  m = (fts3HashEle
377e0 6d 2a 29 66 74 73 33 48 61 73 68 4d 61 6c 6c 6f  m*)fts3HashMallo
377e1 63 28 20 73 69 7a 65 6f 66 28 66 74 73 33 48 61  c( sizeof(fts3Ha
377e2 73 68 45 6c 65 6d 29 20 29 3b 0a 20 20 69 66 28  shElem) );.  if(
377e3 20 6e 65 77 5f 65 6c 65 6d 3d 3d 30 20 29 20 72   new_elem==0 ) r
377e4 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 69 66  eturn data;.  if
377e5 28 20 70 48 2d 3e 63 6f 70 79 4b 65 79 20 26 26  ( pH->copyKey &&
377e6 20 70 4b 65 79 21 3d 30 20 29 7b 0a 20 20 20 20   pKey!=0 ){.    
377e7 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d  new_elem->pKey =
377e8 20 66 74 73 33 48 61 73 68 4d 61 6c 6c 6f 63 28   fts3HashMalloc(
377e9 20 6e 4b 65 79 20 29 3b 0a 20 20 20 20 69 66 28   nKey );.    if(
377ea 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 3d   new_elem->pKey=
377eb 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 74 73 33  =0 ){.      fts3
377ec 48 61 73 68 46 72 65 65 28 6e 65 77 5f 65 6c 65  HashFree(new_ele
377ed 6d 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  m);.      return
377ee 20 64 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20   data;.    }.   
377ef 20 6d 65 6d 63 70 79 28 28 76 6f 69 64 2a 29 6e   memcpy((void*)n
377f0 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 70  ew_elem->pKey, p
377f1 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 7d 65  Key, nKey);.  }e
377f2 6c 73 65 7b 0a 20 20 20 20 6e 65 77 5f 65 6c 65  lse{.    new_ele
377f3 6d 2d 3e 70 4b 65 79 20 3d 20 28 76 6f 69 64 2a  m->pKey = (void*
377f4 29 70 4b 65 79 3b 0a 20 20 7d 0a 20 20 6e 65 77  )pKey;.  }.  new
377f5 5f 65 6c 65 6d 2d 3e 6e 4b 65 79 20 3d 20 6e 4b  _elem->nKey = nK
377f6 65 79 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2b  ey;.  pH->count+
377f7 2b 3b 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 75  +;.  if( pH->cou
377f8 6e 74 20 3e 20 70 48 2d 3e 68 74 73 69 7a 65 20  nt > pH->htsize 
377f9 29 7b 0a 20 20 20 20 66 74 73 33 52 65 68 61 73  ){.    fts3Rehas
377fa 68 28 70 48 2c 70 48 2d 3e 68 74 73 69 7a 65 2a  h(pH,pH->htsize*
377fb 32 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  2);.  }.  assert
377fc 28 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20 29  ( pH->htsize>0 )
377fd 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 48 2d  ;.  assert( (pH-
377fe 3e 68 74 73 69 7a 65 20 26 20 28 70 48 2d 3e 68  >htsize & (pH->h
377ff 74 73 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a  tsize-1))==0 );.
37800 20 20 68 20 3d 20 68 72 61 77 20 26 20 28 70 48    h = hraw & (pH
37801 2d 3e 68 74 73 69 7a 65 2d 31 29 3b 0a 20 20 66  ->htsize-1);.  f
37802 74 73 33 48 61 73 68 49 6e 73 65 72 74 45 6c 65  ts3HashInsertEle
37803 6d 65 6e 74 28 70 48 2c 20 26 70 48 2d 3e 68 74  ment(pH, &pH->ht
37804 5b 68 5d 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a  [h], new_elem);.
37805 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 64 61 74 61    new_elem->data
37806 20 3d 20 64 61 74 61 3b 0a 20 20 72 65 74 75 72   = data;.  retur
37807 6e 20 30 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  n 0;.}..#endif /
37808 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
37809 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
3780a 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
3780b 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _FTS3) */../****
3780c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
3780d 66 20 66 74 73 33 5f 68 61 73 68 2e 63 20 2a 2a  f fts3_hash.c **
3780e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3780f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
37811 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
37812 20 66 69 6c 65 20 66 74 73 33 5f 70 6f 72 74 65   file fts3_porte
37813 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r.c ************
37814 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37815 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
37816 20 32 30 30 36 20 53 65 70 74 65 6d 62 65 72 20   2006 September 
37817 33 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  30.**.** The aut
37818 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
37819 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
3781a 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
3781b 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
3781c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
3781d 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
3781e 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
3781f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
37820 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
37821 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
37822 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
37823 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
37824 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
37825 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
37826 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
37827 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
37828 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
37829 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3782a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3782b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3782c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3782d 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
3782e 69 6f 6e 20 6f 66 20 74 68 65 20 66 75 6c 6c 2d  ion of the full-
3782f 74 65 78 74 2d 73 65 61 72 63 68 20 74 6f 6b 65  text-search toke
37830 6e 69 7a 65 72 20 74 68 61 74 20 69 6d 70 6c 65  nizer that imple
37831 6d 65 6e 74 73 0a 2a 2a 20 61 20 50 6f 72 74 65  ments.** a Porte
37832 72 20 73 74 65 6d 6d 65 72 2e 0a 2a 2f 0a 0a 2f  r stemmer..*/../
37833 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e  *.** The code in
37834 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e   this file is on
37835 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a  ly compiled if:.
37836 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20  **.**     * The 
37837 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62  FTS3 module is b
37838 65 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61 6e  eing built as an
37839 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20   extension.**   
3783a 20 20 20 20 28 69 6e 20 77 68 69 63 68 20 63 61      (in which ca
3783b 73 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 69  se SQLITE_CORE i
3783c 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20  s not defined), 
3783d 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54  or.**.**     * T
3783e 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69  he FTS3 module i
3783f 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69 6e  s being built in
37840 74 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a  to the core of.*
37841 2a 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 28  *       SQLite (
37842 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 51  in which case SQ
37843 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
37844 20 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f   is defined)..*/
37845 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
37846 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65  LITE_CORE) || de
37847 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
37848 42 4c 45 5f 46 54 53 33 29 0a 0a 0a 0a 0a 2f 2a  BLE_FTS3)...../*
37849 0a 2a 2a 20 43 6c 61 73 73 20 64 65 72 69 76 65  .** Class derive
3784a 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 74  d from sqlite3_t
3784b 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 74 79 70 65  okenizer.*/.type
3784c 64 65 66 20 73 74 72 75 63 74 20 70 6f 72 74 65  def struct porte
3784d 72 5f 74 6f 6b 65 6e 69 7a 65 72 20 7b 0a 20 20  r_tokenizer {.  
3784e 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
3784f 72 20 62 61 73 65 3b 20 20 20 20 20 20 2f 2a 20  r base;      /* 
37850 42 61 73 65 20 63 6c 61 73 73 20 2a 2f 0a 7d 20  Base class */.} 
37851 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72  porter_tokenizer
37852 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6c 61 73 73 20 64  ;../*.** Class d
37853 65 72 69 76 65 64 20 66 72 6f 6d 20 73 71 6c 69  erived from sqli
37854 74 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  t3_tokenizer_cur
37855 73 6f 72 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  sor.*/.typedef s
37856 74 72 75 63 74 20 70 6f 72 74 65 72 5f 74 6f 6b  truct porter_tok
37857 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 7b 0a  enizer_cursor {.
37858 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
37859 7a 65 72 5f 63 75 72 73 6f 72 20 62 61 73 65 3b  zer_cursor base;
3785a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
3785b 49 6e 70 75 74 3b 20 20 20 20 20 20 20 20 20 20  Input;          
3785c 2f 2a 20 69 6e 70 75 74 20 77 65 20 61 72 65 20  /* input we are 
3785d 74 6f 6b 65 6e 69 7a 69 6e 67 20 2a 2f 0a 20 20  tokenizing */.  
3785e 69 6e 74 20 6e 49 6e 70 75 74 3b 20 20 20 20 20  int nInput;     
3785f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
37860 73 69 7a 65 20 6f 66 20 74 68 65 20 69 6e 70 75  size of the inpu
37861 74 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73  t */.  int iOffs
37862 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  et;             
37863 20 20 20 20 2f 2a 20 63 75 72 72 65 6e 74 20 70      /* current p
37864 6f 73 69 74 69 6f 6e 20 69 6e 20 7a 49 6e 70 75  osition in zInpu
37865 74 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 6b 65  t */.  int iToke
37866 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
37867 20 20 20 20 2f 2a 20 69 6e 64 65 78 20 6f 66 20      /* index of 
37868 6e 65 78 74 20 74 6f 6b 65 6e 20 74 6f 20 62 65  next token to be
37869 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a 20 20 63   returned */.  c
3786a 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 20 20 20 20  har *zToken;    
3786b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
3786c 74 6f 72 61 67 65 20 66 6f 72 20 63 75 72 72 65  torage for curre
3786d 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e  nt token */.  in
3786e 74 20 6e 41 6c 6c 6f 63 61 74 65 64 3b 20 20 20  t nAllocated;   
3786f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 70             /* sp
37870 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f  ace allocated to
37871 20 7a 54 6f 6b 65 6e 20 62 75 66 66 65 72 20 2a   zToken buffer *
37872 2f 0a 7d 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e  /.} porter_token
37873 69 7a 65 72 5f 63 75 72 73 6f 72 3b 0a 0a 0a 2f  izer_cursor;.../
37874 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72  * Forward declar
37875 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20  ation */.static 
37876 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f  const sqlite3_to
37877 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 70  kenizer_module p
37878 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f  orterTokenizerMo
37879 64 75 6c 65 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72  dule;.../*.** Cr
3787a 65 61 74 65 20 61 20 6e 65 77 20 74 6f 6b 65 6e  eate a new token
3787b 69 7a 65 72 20 69 6e 73 74 61 6e 63 65 2e 0a 2a  izer instance..*
3787c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f 72  /.static int por
3787d 74 65 72 43 72 65 61 74 65 28 0a 20 20 69 6e 74  terCreate(.  int
3787e 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61   argc, const cha
3787f 72 20 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c  r * const *argv,
37880 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
37881 69 7a 65 72 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a  izer **ppTokeniz
37882 65 72 0a 29 7b 0a 20 20 70 6f 72 74 65 72 5f 74  er.){.  porter_t
37883 6f 6b 65 6e 69 7a 65 72 20 2a 74 3b 0a 20 20 74  okenizer *t;.  t
37884 20 3d 20 28 70 6f 72 74 65 72 5f 74 6f 6b 65 6e   = (porter_token
37885 69 7a 65 72 20 2a 29 20 73 71 6c 69 74 65 33 5f  izer *) sqlite3_
37886 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 74  malloc(sizeof(*t
37887 29 29 3b 0a 20 20 69 66 28 20 74 3d 3d 4e 55 4c  ));.  if( t==NUL
37888 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  L ) return SQLIT
37889 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 6d 65 6d 73 65  E_NOMEM;.  memse
3788a 74 28 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a  t(t, 0, sizeof(*
3788b 74 29 29 3b 0a 20 20 2a 70 70 54 6f 6b 65 6e 69  t));.  *ppTokeni
3788c 7a 65 72 20 3d 20 26 74 2d 3e 62 61 73 65 3b 0a  zer = &t->base;.
3788d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3788e 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73  OK;.}../*.** Des
3788f 74 72 6f 79 20 61 20 74 6f 6b 65 6e 69 7a 65 72  troy a tokenizer
37890 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
37891 6f 72 74 65 72 44 65 73 74 72 6f 79 28 73 71 6c  orterDestroy(sql
37892 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
37893 70 54 6f 6b 65 6e 69 7a 65 72 29 7b 0a 20 20 73  pTokenizer){.  s
37894 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6f 6b  qlite3_free(pTok
37895 65 6e 69 7a 65 72 29 3b 0a 20 20 72 65 74 75 72  enizer);.  retur
37896 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
37897 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 74 6f  /*.** Prepare to
37898 20 62 65 67 69 6e 20 74 6f 6b 65 6e 69 7a 69 6e   begin tokenizin
37899 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 73  g a particular s
3789a 74 72 69 6e 67 2e 20 20 54 68 65 20 69 6e 70 75  tring.  The inpu
3789b 74 0a 2a 2a 20 73 74 72 69 6e 67 20 74 6f 20 62  t.** string to b
3789c 65 20 74 6f 6b 65 6e 69 7a 65 64 20 69 73 20 7a  e tokenized is z
3789d 49 6e 70 75 74 5b 30 2e 2e 6e 49 6e 70 75 74 2d  Input[0..nInput-
3789e 31 5d 2e 20 20 41 20 63 75 72 73 6f 72 0a 2a 2a  1].  A cursor.**
3789f 20 75 73 65 64 20 74 6f 20 69 6e 63 72 65 6d 65   used to increme
378a0 6e 74 61 6c 6c 79 20 74 6f 6b 65 6e 69 7a 65 20  ntally tokenize 
378a1 74 68 69 73 20 73 74 72 69 6e 67 20 69 73 20 72  this string is r
378a2 65 74 75 72 6e 65 64 20 69 6e 20 0a 2a 2a 20 2a  eturned in .** *
378a3 70 70 43 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61  ppCursor..*/.sta
378a4 74 69 63 20 69 6e 74 20 70 6f 72 74 65 72 4f 70  tic int porterOp
378a5 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  en(.  sqlite3_to
378a6 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69  kenizer *pTokeni
378a7 7a 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  zer,         /* 
378a8 54 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f  The tokenizer */
378a9 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
378aa 49 6e 70 75 74 2c 20 69 6e 74 20 6e 49 6e 70 75  Input, int nInpu
378ab 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72  t,        /* Str
378ac 69 6e 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69  ing to be tokeni
378ad 7a 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  zed */.  sqlite3
378ae 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
378af 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20 20 20  r **ppCursor    
378b0 2f 2a 20 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a 61  /* OUT: Tokeniza
378b1 74 69 6f 6e 20 63 75 72 73 6f 72 20 2a 2f 0a 29  tion cursor */.)
378b2 7b 0a 20 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e  {.  porter_token
378b3 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 3b 0a  izer_cursor *c;.
378b4 0a 20 20 63 20 3d 20 28 70 6f 72 74 65 72 5f 74  .  c = (porter_t
378b5 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
378b6 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  *) sqlite3_mallo
378b7 63 28 73 69 7a 65 6f 66 28 2a 63 29 29 3b 0a 20  c(sizeof(*c));. 
378b8 20 69 66 28 20 63 3d 3d 4e 55 4c 4c 20 29 20 72   if( c==NULL ) r
378b9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
378ba 45 4d 3b 0a 0a 20 20 63 2d 3e 7a 49 6e 70 75 74  EM;..  c->zInput
378bb 20 3d 20 7a 49 6e 70 75 74 3b 0a 20 20 69 66 28   = zInput;.  if(
378bc 20 7a 49 6e 70 75 74 3d 3d 30 20 29 7b 0a 20 20   zInput==0 ){.  
378bd 20 20 63 2d 3e 6e 49 6e 70 75 74 20 3d 20 30 3b    c->nInput = 0;
378be 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 49 6e  .  }else if( nIn
378bf 70 75 74 3c 30 20 29 7b 0a 20 20 20 20 63 2d 3e  put<0 ){.    c->
378c0 6e 49 6e 70 75 74 20 3d 20 28 69 6e 74 29 73 74  nInput = (int)st
378c1 72 6c 65 6e 28 7a 49 6e 70 75 74 29 3b 0a 20 20  rlen(zInput);.  
378c2 7d 65 6c 73 65 7b 0a 20 20 20 20 63 2d 3e 6e 49  }else{.    c->nI
378c3 6e 70 75 74 20 3d 20 6e 49 6e 70 75 74 3b 0a 20  nput = nInput;. 
378c4 20 7d 0a 20 20 63 2d 3e 69 4f 66 66 73 65 74 20   }.  c->iOffset 
378c5 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
378c6 20 20 20 20 20 2f 2a 20 73 74 61 72 74 20 74 6f       /* start to
378c7 6b 65 6e 69 7a 69 6e 67 20 61 74 20 74 68 65 20  kenizing at the 
378c8 62 65 67 69 6e 6e 69 6e 67 20 2a 2f 0a 20 20 63  beginning */.  c
378c9 2d 3e 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20  ->iToken = 0;.  
378ca 63 2d 3e 7a 54 6f 6b 65 6e 20 3d 20 4e 55 4c 4c  c->zToken = NULL
378cb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
378cc 2f 2a 20 6e 6f 20 73 70 61 63 65 20 61 6c 6c 6f  /* no space allo
378cd 63 61 74 65 64 2c 20 79 65 74 2e 20 2a 2f 0a 20  cated, yet. */. 
378ce 20 63 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64 20 3d   c->nAllocated =
378cf 20 30 3b 0a 0a 20 20 2a 70 70 43 75 72 73 6f 72   0;..  *ppCursor
378d0 20 3d 20 26 63 2d 3e 62 61 73 65 3b 0a 20 20 72   = &c->base;.  r
378d1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
378d2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20  .}../*.** Close 
378d3 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63  a tokenization c
378d4 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73 6c 79  ursor previously
378d5 20 6f 70 65 6e 65 64 20 62 79 20 61 20 63 61 6c   opened by a cal
378d6 6c 20 74 6f 0a 2a 2a 20 70 6f 72 74 65 72 4f 70  l to.** porterOp
378d7 65 6e 28 29 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73  en() above..*/.s
378d8 74 61 74 69 63 20 69 6e 74 20 70 6f 72 74 65 72  tatic int porter
378d9 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 74 6f  Close(sqlite3_to
378da 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
378db 70 43 75 72 73 6f 72 29 7b 0a 20 20 70 6f 72 74  pCursor){.  port
378dc 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  er_tokenizer_cur
378dd 73 6f 72 20 2a 63 20 3d 20 28 70 6f 72 74 65 72  sor *c = (porter
378de 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
378df 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20  r *) pCursor;.  
378e0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63 2d 3e  sqlite3_free(c->
378e1 7a 54 6f 6b 65 6e 29 3b 0a 20 20 73 71 6c 69 74  zToken);.  sqlit
378e2 65 33 5f 66 72 65 65 28 63 29 3b 0a 20 20 72 65  e3_free(c);.  re
378e3 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
378e4 7d 0a 2f 2a 0a 2a 2a 20 56 6f 77 65 6c 20 6f 72  }./*.** Vowel or
378e5 20 63 6f 6e 73 6f 6e 61 6e 74 0a 2a 2f 0a 73 74   consonant.*/.st
378e6 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
378e7 63 54 79 70 65 5b 5d 20 3d 20 7b 0a 20 20 20 30  cType[] = {.   0
378e8 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c  , 1, 1, 1, 0, 1,
378e9 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20   1, 1, 0, 1, 1, 
378ea 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31  1, 1, 1, 0, 1, 1
378eb 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 0a 20 20  , 1, 1, 1, 0,.  
378ec 20 31 2c 20 31 2c 20 31 2c 20 32 2c 20 31 0a 7d   1, 1, 1, 2, 1.}
378ed 3b 0a 0a 2f 2a 0a 2a 2a 20 69 73 43 6f 6e 73 6f  ;../*.** isConso
378ee 6e 61 6e 74 28 29 20 61 6e 64 20 69 73 56 6f 77  nant() and isVow
378ef 65 6c 28 29 20 64 65 74 65 72 6d 69 6e 65 20 69  el() determine i
378f0 66 20 74 68 65 69 72 20 66 69 72 73 74 20 63 68  f their first ch
378f1 61 72 61 63 74 65 72 20 69 6e 0a 2a 2a 20 74 68  aracter in.** th
378f2 65 20 73 74 72 69 6e 67 20 74 68 65 79 20 70 6f  e string they po
378f3 69 6e 74 20 74 6f 20 69 73 20 61 20 63 6f 6e 73  int to is a cons
378f4 6f 6e 61 6e 74 20 6f 72 20 61 20 76 6f 77 65 6c  onant or a vowel
378f5 2c 20 61 63 63 6f 72 64 69 6e 67 0a 2a 2a 20 74  , according.** t
378f6 6f 20 50 6f 72 74 65 72 20 72 75 6c 73 2e 20 20  o Porter ruls.  
378f7 0a 2a 2a 0a 2a 2a 20 41 20 63 6f 6e 73 6f 6e 61  .**.** A consona
378f8 74 65 20 69 73 20 61 6e 79 20 6c 65 74 74 65 72  te is any letter
378f9 20 6f 74 68 65 72 20 74 68 61 6e 20 27 61 27 2c   other than 'a',
378fa 20 27 65 27 2c 20 27 69 27 2c 20 27 6f 27 2c 20   'e', 'i', 'o', 
378fb 6f 72 20 27 75 27 2e 0a 2a 2a 20 27 59 27 20 69  or 'u'..** 'Y' i
378fc 73 20 61 20 63 6f 6e 73 6f 6e 61 6e 74 20 75 6e  s a consonant un
378fd 6c 65 73 73 20 69 74 20 66 6f 6c 6c 6f 77 73 20  less it follows 
378fe 61 6e 6f 74 68 65 72 20 63 6f 6e 73 6f 6e 61 6e  another consonan
378ff 74 2c 0a 2a 2a 20 69 6e 20 77 68 69 63 68 20 63  t,.** in which c
37900 61 73 65 20 69 74 20 69 73 20 61 20 76 6f 77 65  ase it is a vowe
37901 6c 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 73  l..**.** In thes
37902 65 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 20 6c  e routine, the l
37903 65 74 74 65 72 73 20 61 72 65 20 69 6e 20 72 65  etters are in re
37904 76 65 72 73 65 20 6f 72 64 65 72 2e 20 20 53 6f  verse order.  So
37905 20 74 68 65 20 27 79 27 20 72 75 6c 65 0a 2a 2a   the 'y' rule.**
37906 20 69 73 20 74 68 61 74 20 27 79 27 20 69 73 20   is that 'y' is 
37907 61 20 63 6f 6e 73 6f 6e 61 6e 74 20 75 6e 6c 65  a consonant unle
37908 73 73 20 69 74 20 69 73 20 66 6f 6c 6c 6f 77 65  ss it is followe
37909 64 20 62 79 20 61 6e 6f 74 68 65 72 0a 2a 2a 20  d by another.** 
3790a 63 6f 6e 73 6f 6e 65 6e 74 2e 0a 2a 2f 0a 73 74  consonent..*/.st
3790b 61 74 69 63 20 69 6e 74 20 69 73 56 6f 77 65 6c  atic int isVowel
3790c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 73  (const char*);.s
3790d 74 61 74 69 63 20 69 6e 74 20 69 73 43 6f 6e 73  tatic int isCons
3790e 6f 6e 61 6e 74 28 63 6f 6e 73 74 20 63 68 61 72  onant(const char
3790f 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 6a 3b 0a 20   *z){.  int j;. 
37910 20 63 68 61 72 20 78 20 3d 20 2a 7a 3b 0a 20 20   char x = *z;.  
37911 69 66 28 20 78 3d 3d 30 20 29 20 72 65 74 75 72  if( x==0 ) retur
37912 6e 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 78  n 0;.  assert( x
37913 3e 3d 27 61 27 20 26 26 20 78 3c 3d 27 7a 27 20  >='a' && x<='z' 
37914 29 3b 0a 20 20 6a 20 3d 20 63 54 79 70 65 5b 78  );.  j = cType[x
37915 2d 27 61 27 5d 3b 0a 20 20 69 66 28 20 6a 3c 32  -'a'];.  if( j<2
37916 20 29 20 72 65 74 75 72 6e 20 6a 3b 0a 20 20 72   ) return j;.  r
37917 65 74 75 72 6e 20 7a 5b 31 5d 3d 3d 30 20 7c 7c  eturn z[1]==0 ||
37918 20 69 73 56 6f 77 65 6c 28 7a 20 2b 20 31 29 3b   isVowel(z + 1);
37919 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73  .}.static int is
3791a 56 6f 77 65 6c 28 63 6f 6e 73 74 20 63 68 61 72  Vowel(const char
3791b 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 6a 3b 0a 20   *z){.  int j;. 
3791c 20 63 68 61 72 20 78 20 3d 20 2a 7a 3b 0a 20 20   char x = *z;.  
3791d 69 66 28 20 78 3d 3d 30 20 29 20 72 65 74 75 72  if( x==0 ) retur
3791e 6e 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 78  n 0;.  assert( x
3791f 3e 3d 27 61 27 20 26 26 20 78 3c 3d 27 7a 27 20  >='a' && x<='z' 
37920 29 3b 0a 20 20 6a 20 3d 20 63 54 79 70 65 5b 78  );.  j = cType[x
37921 2d 27 61 27 5d 3b 0a 20 20 69 66 28 20 6a 3c 32  -'a'];.  if( j<2
37922 20 29 20 72 65 74 75 72 6e 20 31 2d 6a 3b 0a 20   ) return 1-j;. 
37923 20 72 65 74 75 72 6e 20 69 73 43 6f 6e 73 6f 6e   return isConson
37924 61 6e 74 28 7a 20 2b 20 31 29 3b 0a 7d 0a 0a 2f  ant(z + 1);.}../
37925 2a 0a 2a 2a 20 4c 65 74 20 61 6e 79 20 73 65 71  *.** Let any seq
37926 75 65 6e 63 65 20 6f 66 20 6f 6e 65 20 6f 72 20  uence of one or 
37927 6d 6f 72 65 20 76 6f 77 65 6c 73 20 62 65 20 72  more vowels be r
37928 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 56 20  epresented by V 
37929 61 6e 64 20 6c 65 74 0a 2a 2a 20 43 20 62 65 20  and let.** C be 
3792a 73 65 71 75 65 6e 63 65 20 6f 66 20 6f 6e 65 20  sequence of one 
3792b 6f 72 20 6d 6f 72 65 20 63 6f 6e 73 6f 6e 61 6e  or more consonan
3792c 74 73 2e 20 20 54 68 65 6e 20 65 76 65 72 79 20  ts.  Then every 
3792d 77 6f 72 64 20 63 61 6e 20 62 65 0a 2a 2a 20 72  word can be.** r
3792e 65 70 72 65 73 65 6e 74 65 64 20 61 73 3a 0a 2a  epresented as:.*
3792f 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b  *.**           [
37930 43 5d 20 28 56 43 29 7b 6d 7d 20 5b 56 5d 0a 2a  C] (VC){m} [V].*
37931 2a 0a 2a 2a 20 49 6e 20 70 72 6f 73 65 3a 20 20  *.** In prose:  
37932 41 20 77 6f 72 64 20 69 73 20 61 6e 20 6f 70 74  A word is an opt
37933 69 6f 6e 61 6c 20 63 6f 6e 73 6f 6e 61 6e 74 20  ional consonant 
37934 66 6f 6c 6c 6f 77 65 64 20 62 79 20 7a 65 72 6f  followed by zero
37935 20 6f 72 0a 2a 2a 20 76 6f 77 65 6c 2d 63 6f 6e   or.** vowel-con
37936 73 6f 6e 61 6e 74 20 70 61 69 72 73 20 66 6f 6c  sonant pairs fol
37937 6c 6f 77 65 64 20 62 79 20 61 6e 20 6f 70 74 69  lowed by an opti
37938 6f 6e 61 6c 20 76 6f 77 65 6c 2e 20 20 22 6d 22  onal vowel.  "m"
37939 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65   is the.** numbe
3793a 72 20 6f 66 20 76 6f 77 65 6c 20 63 6f 6e 73 6f  r of vowel conso
3793b 6e 61 6e 74 20 70 61 69 72 73 2e 20 20 54 68 69  nant pairs.  Thi
3793c 73 20 72 6f 75 74 69 6e 65 20 63 6f 6d 70 75 74  s routine comput
3793d 65 73 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20  es the value.** 
3793e 6f 66 20 6d 20 66 6f 72 20 74 68 65 20 66 69 72  of m for the fir
3793f 73 74 20 69 20 62 79 74 65 73 20 6f 66 20 61 20  st i bytes of a 
37940 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  word..**.** Retu
37941 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 6d  rn true if the m
37942 2d 76 61 6c 75 65 20 66 6f 72 20 7a 20 69 73 20  -value for z is 
37943 31 20 6f 72 20 6d 6f 72 65 2e 20 20 49 6e 20 6f  1 or more.  In o
37944 74 68 65 72 20 77 6f 72 64 73 2c 0a 2a 2a 20 72  ther words,.** r
37945 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 7a 20  eturn true if z 
37946 63 6f 6e 74 61 69 6e 73 20 61 74 20 6c 65 61 73  contains at leas
37947 74 20 6f 6e 65 20 76 6f 77 65 6c 20 74 68 61 74  t one vowel that
37948 20 69 73 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a 20   is followed.** 
37949 62 79 20 61 20 63 6f 6e 73 6f 6e 61 6e 74 2e 0a  by a consonant..
3794a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 72 6f  **.** In this ro
3794b 75 74 69 6e 65 20 7a 5b 5d 20 69 73 20 69 6e 20  utine z[] is in 
3794c 72 65 76 65 72 73 65 20 6f 72 64 65 72 2e 20 20  reverse order.  
3794d 53 6f 20 77 65 20 61 72 65 20 72 65 61 6c 6c 79  So we are really
3794e 20 6c 6f 6f 6b 69 6e 67 0a 2a 2a 20 66 6f 72 20   looking.** for 
3794f 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 6f  an instance of o
37950 66 20 61 20 63 6f 6e 73 6f 6e 61 6e 74 20 66 6f  f a consonant fo
37951 6c 6c 6f 77 65 64 20 62 79 20 61 20 76 6f 77 65  llowed by a vowe
37952 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  l..*/.static int
37953 20 6d 5f 67 74 5f 30 28 63 6f 6e 73 74 20 63 68   m_gt_0(const ch
37954 61 72 20 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28  ar *z){.  while(
37955 20 69 73 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a   isVowel(z) ){ z
37956 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d  ++; }.  if( *z==
37957 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
37958 77 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61  while( isConsona
37959 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a  nt(z) ){ z++; }.
3795a 20 20 72 65 74 75 72 6e 20 2a 7a 21 3d 30 3b 0a    return *z!=0;.
3795b 7d 0a 0a 2f 2a 20 4c 69 6b 65 20 6d 67 74 30 20  }../* Like mgt0 
3795c 61 62 6f 76 65 20 65 78 63 65 70 74 20 77 65 20  above except we 
3795d 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20  are looking for 
3795e 61 20 76 61 6c 75 65 20 6f 66 20 6d 20 77 68 69  a value of m whi
3795f 63 68 20 69 73 0a 2a 2a 20 65 78 61 63 74 6c 79  ch is.** exactly
37960 20 31 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74   1.*/.static int
37961 20 6d 5f 65 71 5f 31 28 63 6f 6e 73 74 20 63 68   m_eq_1(const ch
37962 61 72 20 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28  ar *z){.  while(
37963 20 69 73 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a   isVowel(z) ){ z
37964 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d  ++; }.  if( *z==
37965 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
37966 77 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61  while( isConsona
37967 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a  nt(z) ){ z++; }.
37968 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65    if( *z==0 ) re
37969 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28  turn 0;.  while(
3796a 20 69 73 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a   isVowel(z) ){ z
3796b 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d  ++; }.  if( *z==
3796c 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  0 ) return 1;.  
3796d 77 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61  while( isConsona
3796e 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a  nt(z) ){ z++; }.
3796f 20 20 72 65 74 75 72 6e 20 2a 7a 3d 3d 30 3b 0a    return *z==0;.
37970 7d 0a 0a 2f 2a 20 4c 69 6b 65 20 6d 67 74 30 20  }../* Like mgt0 
37971 61 62 6f 76 65 20 65 78 63 65 70 74 20 77 65 20  above except we 
37972 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20  are looking for 
37973 61 20 76 61 6c 75 65 20 6f 66 20 6d 3e 31 20 69  a value of m>1 i
37974 6e 73 74 65 61 64 0a 2a 2a 20 6f 72 20 6d 3e 30  nstead.** or m>0
37975 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
37976 5f 67 74 5f 31 28 63 6f 6e 73 74 20 63 68 61 72  _gt_1(const char
37977 20 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28 20 69   *z){.  while( i
37978 73 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b  sVowel(z) ){ z++
37979 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20  ; }.  if( *z==0 
3797a 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68  ) return 0;.  wh
3797b 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74  ile( isConsonant
3797c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20  (z) ){ z++; }.  
3797d 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75  if( *z==0 ) retu
3797e 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69  rn 0;.  while( i
3797f 73 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b  sVowel(z) ){ z++
37980 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20  ; }.  if( *z==0 
37981 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68  ) return 0;.  wh
37982 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74  ile( isConsonant
37983 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20  (z) ){ z++; }.  
37984 72 65 74 75 72 6e 20 2a 7a 21 3d 30 3b 0a 7d 0a  return *z!=0;.}.
37985 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
37986 55 45 20 69 66 20 74 68 65 72 65 20 69 73 20 61  UE if there is a
37987 20 76 6f 77 65 6c 20 61 6e 79 77 68 65 72 65 20   vowel anywhere 
37988 77 69 74 68 69 6e 20 7a 5b 30 2e 2e 6e 2d 31 5d  within z[0..n-1]
37989 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68  .*/.static int h
3798a 61 73 56 6f 77 65 6c 28 63 6f 6e 73 74 20 63 68  asVowel(const ch
3798b 61 72 20 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28  ar *z){.  while(
3798c 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20   isConsonant(z) 
3798d 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 72 65 74 75  ){ z++; }.  retu
3798e 72 6e 20 2a 7a 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a  rn *z!=0;.}../*.
3798f 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
37990 66 20 74 68 65 20 77 6f 72 64 20 65 6e 64 73 20  f the word ends 
37991 69 6e 20 61 20 64 6f 75 62 6c 65 20 63 6f 6e 73  in a double cons
37992 6f 6e 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  onant..**.** The
37993 20 74 65 78 74 20 69 73 20 72 65 76 65 72 73 65   text is reverse
37994 64 20 68 65 72 65 2e 20 53 6f 20 77 65 20 61 72  d here. So we ar
37995 65 20 72 65 61 6c 6c 79 20 6c 6f 6f 6b 69 6e 67  e really looking
37996 20 61 74 0a 2a 2a 20 74 68 65 20 66 69 72 73 74   at.** the first
37997 20 74 77 6f 20 63 68 61 72 61 63 74 65 72 73 20   two characters 
37998 6f 66 20 7a 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69  of z[]..*/.stati
37999 63 20 69 6e 74 20 64 6f 75 62 6c 65 43 6f 6e 73  c int doubleCons
3799a 6f 6e 61 6e 74 28 63 6f 6e 73 74 20 63 68 61 72  onant(const char
3799b 20 2a 7a 29 7b 0a 20 20 72 65 74 75 72 6e 20 69   *z){.  return i
3799c 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 26 26  sConsonant(z) &&
3799d 20 7a 5b 30 5d 3d 3d 7a 5b 31 5d 20 26 26 20 69   z[0]==z[1] && i
3799e 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 2b 31 29 3b  sConsonant(z+1);
3799f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
379a0 20 54 52 55 45 20 69 66 20 74 68 65 20 77 6f 72   TRUE if the wor
379a1 64 20 65 6e 64 73 20 77 69 74 68 20 74 68 72 65  d ends with thre
379a2 65 20 6c 65 74 74 65 72 73 20 77 68 69 63 68 0a  e letters which.
379a3 2a 2a 20 61 72 65 20 63 6f 6e 73 6f 6e 61 6e 74  ** are consonant
379a4 2d 76 6f 77 65 6c 2d 63 6f 6e 73 6f 6e 65 6e 74  -vowel-consonent
379a5 20 61 6e 64 20 77 68 65 72 65 20 74 68 65 20 66   and where the f
379a6 69 6e 61 6c 20 63 6f 6e 73 6f 6e 61 6e 74 0a 2a  inal consonant.*
379a7 2a 20 69 73 20 6e 6f 74 20 27 77 27 2c 20 27 78  * is not 'w', 'x
379a8 27 2c 20 6f 72 20 27 79 27 2e 0a 2a 2a 0a 2a 2a  ', or 'y'..**.**
379a9 20 54 68 65 20 77 6f 72 64 20 69 73 20 72 65 76   The word is rev
379aa 65 72 73 65 64 20 68 65 72 65 2e 20 20 53 6f 20  ersed here.  So 
379ab 77 65 20 61 72 65 20 72 65 61 6c 6c 79 20 63 68  we are really ch
379ac 65 63 6b 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69  ecking the.** fi
379ad 72 73 74 20 74 68 72 65 65 20 6c 65 74 74 65 72  rst three letter
379ae 73 20 61 6e 64 20 74 68 65 20 66 69 72 73 74 20  s and the first 
379af 6f 6e 65 20 63 61 6e 6e 6f 74 20 62 65 20 69 6e  one cannot be in
379b0 20 5b 77 78 79 5d 2e 0a 2a 2f 0a 73 74 61 74 69   [wxy]..*/.stati
379b1 63 20 69 6e 74 20 73 74 61 72 5f 6f 68 28 63 6f  c int star_oh(co
379b2 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20  nst char *z){.  
379b3 72 65 74 75 72 6e 0a 20 20 20 20 7a 5b 30 5d 21  return.    z[0]!
379b4 3d 30 20 26 26 20 69 73 43 6f 6e 73 6f 6e 61 6e  =0 && isConsonan
379b5 74 28 7a 29 20 26 26 0a 20 20 20 20 7a 5b 30 5d  t(z) &&.    z[0]
379b6 21 3d 27 77 27 20 26 26 20 7a 5b 30 5d 21 3d 27  !='w' && z[0]!='
379b7 78 27 20 26 26 20 7a 5b 30 5d 21 3d 27 79 27 20  x' && z[0]!='y' 
379b8 26 26 0a 20 20 20 20 7a 5b 31 5d 21 3d 30 20 26  &&.    z[1]!=0 &
379b9 26 20 69 73 56 6f 77 65 6c 28 7a 2b 31 29 20 26  & isVowel(z+1) &
379ba 26 0a 20 20 20 20 7a 5b 32 5d 21 3d 30 20 26 26  &.    z[2]!=0 &&
379bb 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 2b 32   isConsonant(z+2
379bc 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  );.}../*.** If t
379bd 68 65 20 77 6f 72 64 20 65 6e 64 73 20 77 69 74  he word ends wit
379be 68 20 7a 46 72 6f 6d 20 61 6e 64 20 78 43 6f 6e  h zFrom and xCon
379bf 64 28 29 20 69 73 20 74 72 75 65 20 66 6f 72 20  d() is true for 
379c0 74 68 65 20 73 74 65 6d 0a 2a 2a 20 6f 66 20 74  the stem.** of t
379c1 68 65 20 77 6f 72 64 20 74 68 61 74 20 70 72 65  he word that pre
379c2 63 65 65 64 73 20 74 68 65 20 7a 46 72 6f 6d 20  ceeds the zFrom 
379c3 65 6e 64 69 6e 67 2c 20 74 68 65 6e 20 63 68 61  ending, then cha
379c4 6e 67 65 20 74 68 65 20 0a 2a 2a 20 65 6e 64 69  nge the .** endi
379c5 6e 67 20 74 6f 20 7a 54 6f 2e 0a 2a 2a 0a 2a 2a  ng to zTo..**.**
379c6 20 54 68 65 20 69 6e 70 75 74 20 77 6f 72 64 20   The input word 
379c7 2a 70 7a 20 61 6e 64 20 7a 46 72 6f 6d 20 61 72  *pz and zFrom ar
379c8 65 20 62 6f 74 68 20 69 6e 20 72 65 76 65 72 73  e both in revers
379c9 65 20 6f 72 64 65 72 2e 20 20 7a 54 6f 0a 2a 2a  e order.  zTo.**
379ca 20 69 73 20 69 6e 20 6e 6f 72 6d 61 6c 20 6f 72   is in normal or
379cb 64 65 72 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 74 75  der. .**.** Retu
379cc 72 6e 20 54 52 55 45 20 69 66 20 7a 46 72 6f 6d  rn TRUE if zFrom
379cd 20 6d 61 74 63 68 65 73 2e 20 20 52 65 74 75 72   matches.  Retur
379ce 6e 20 46 41 4c 53 45 20 69 66 20 7a 46 72 6f 6d  n FALSE if zFrom
379cf 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d 61 74   does not.** mat
379d0 63 68 2e 20 20 4e 6f 74 20 74 68 61 74 20 54 52  ch.  Not that TR
379d1 55 45 20 69 73 20 72 65 74 75 72 6e 65 64 20 65  UE is returned e
379d2 76 65 6e 20 69 66 20 78 43 6f 6e 64 28 29 20 66  ven if xCond() f
379d3 61 69 6c 73 20 61 6e 64 0a 2a 2a 20 6e 6f 20 73  ails and.** no s
379d4 75 62 73 74 69 74 75 74 69 6f 6e 20 6f 63 63 75  ubstitution occu
379d5 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  rs..*/.static in
379d6 74 20 73 74 65 6d 28 0a 20 20 63 68 61 72 20 2a  t stem(.  char *
379d7 2a 70 7a 2c 20 20 20 20 20 20 20 20 20 20 20 20  *pz,            
379d8 20 2f 2a 20 54 68 65 20 77 6f 72 64 20 62 65 69   /* The word bei
379d9 6e 67 20 73 74 65 6d 6d 65 64 20 28 52 65 76 65  ng stemmed (Reve
379da 72 73 65 64 29 20 2a 2f 0a 20 20 63 6f 6e 73 74  rsed) */.  const
379db 20 63 68 61 72 20 2a 7a 46 72 6f 6d 2c 20 20 20   char *zFrom,   
379dc 20 20 2f 2a 20 49 66 20 74 68 65 20 65 6e 64 69    /* If the endi
379dd 6e 67 20 6d 61 74 63 68 65 73 20 74 68 69 73 2e  ng matches this.
379de 2e 2e 20 28 52 65 76 65 72 73 65 64 29 20 2a 2f  .. (Reversed) */
379df 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
379e0 54 6f 2c 20 20 20 20 20 20 20 2f 2a 20 2e 2e 2e  To,       /* ...
379e1 20 63 68 61 6e 67 65 20 74 68 65 20 65 6e 64 69   change the endi
379e2 6e 67 20 74 6f 20 74 68 69 73 20 28 6e 6f 74 20  ng to this (not 
379e3 72 65 76 65 72 73 65 64 29 20 2a 2f 0a 20 20 69  reversed) */.  i
379e4 6e 74 20 28 2a 78 43 6f 6e 64 29 28 63 6f 6e 73  nt (*xCond)(cons
379e5 74 20 63 68 61 72 2a 29 20 20 20 2f 2a 20 43 6f  t char*)   /* Co
379e6 6e 64 69 74 69 6f 6e 20 74 68 61 74 20 6d 75 73  ndition that mus
379e7 74 20 62 65 20 74 72 75 65 20 2a 2f 0a 29 7b 0a  t be true */.){.
379e8 20 20 63 68 61 72 20 2a 7a 20 3d 20 2a 70 7a 3b    char *z = *pz;
379e9 0a 20 20 77 68 69 6c 65 28 20 2a 7a 46 72 6f 6d  .  while( *zFrom
379ea 20 26 26 20 2a 7a 46 72 6f 6d 3d 3d 2a 7a 20 29   && *zFrom==*z )
379eb 7b 20 7a 2b 2b 3b 20 7a 46 72 6f 6d 2b 2b 3b 20  { z++; zFrom++; 
379ec 7d 0a 20 20 69 66 28 20 2a 7a 46 72 6f 6d 21 3d  }.  if( *zFrom!=
379ed 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
379ee 69 66 28 20 78 43 6f 6e 64 20 26 26 20 21 78 43  if( xCond && !xC
379ef 6f 6e 64 28 7a 29 20 29 20 72 65 74 75 72 6e 20  ond(z) ) return 
379f0 31 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 54 6f  1;.  while( *zTo
379f1 20 29 7b 0a 20 20 20 20 2a 28 2d 2d 7a 29 20 3d   ){.    *(--z) =
379f2 20 2a 28 7a 54 6f 2b 2b 29 3b 0a 20 20 7d 0a 20   *(zTo++);.  }. 
379f3 20 2a 70 7a 20 3d 20 7a 3b 0a 20 20 72 65 74 75   *pz = z;.  retu
379f4 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  rn 1;.}../*.** T
379f5 68 69 73 20 69 73 20 74 68 65 20 66 61 6c 6c 62  his is the fallb
379f6 61 63 6b 20 73 74 65 6d 6d 65 72 20 75 73 65 64  ack stemmer used
379f7 20 77 68 65 6e 20 74 68 65 20 70 6f 72 74 65 72   when the porter
379f8 20 73 74 65 6d 6d 65 72 20 69 73 0a 2a 2a 20 69   stemmer is.** i
379f9 6e 61 70 70 72 6f 70 72 69 61 74 65 2e 20 20 54  nappropriate.  T
379fa 68 65 20 69 6e 70 75 74 20 77 6f 72 64 20 69 73  he input word is
379fb 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65   copied into the
379fc 20 6f 75 74 70 75 74 20 77 69 74 68 0a 2a 2a 20   output with.** 
379fd 55 53 2d 41 53 43 49 49 20 63 61 73 65 20 66 6f  US-ASCII case fo
379fe 6c 64 69 6e 67 2e 20 20 49 66 20 74 68 65 20 69  lding.  If the i
379ff 6e 70 75 74 20 77 6f 72 64 20 69 73 20 74 6f 6f  nput word is too
37a00 20 6c 6f 6e 67 20 28 6d 6f 72 65 0a 2a 2a 20 74   long (more.** t
37a01 68 61 6e 20 32 30 20 62 79 74 65 73 20 69 66 20  han 20 bytes if 
37a02 69 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 64  it contains no d
37a03 69 67 69 74 73 20 6f 72 20 6d 6f 72 65 20 74 68  igits or more th
37a04 61 6e 20 36 20 62 79 74 65 73 20 69 66 0a 2a 2a  an 6 bytes if.**
37a05 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 64 69 67   it contains dig
37a06 69 74 73 29 20 74 68 65 6e 20 77 6f 72 64 20 69  its) then word i
37a07 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 32  s truncated to 2
37a08 30 20 6f 72 20 36 20 62 79 74 65 73 0a 2a 2a 20  0 or 6 bytes.** 
37a09 62 79 20 74 61 6b 69 6e 67 20 31 30 20 6f 72 20  by taking 10 or 
37a0a 33 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65  3 bytes from the
37a0b 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65   beginning and e
37a0c 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  nd..*/.static vo
37a0d 69 64 20 63 6f 70 79 5f 73 74 65 6d 6d 65 72 28  id copy_stemmer(
37a0e 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c  const char *zIn,
37a0f 20 69 6e 74 20 6e 49 6e 2c 20 63 68 61 72 20 2a   int nIn, char *
37a10 7a 4f 75 74 2c 20 69 6e 74 20 2a 70 6e 4f 75 74  zOut, int *pnOut
37a11 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6d 78 2c 20  ){.  int i, mx, 
37a12 6a 3b 0a 20 20 69 6e 74 20 68 61 73 44 69 67 69  j;.  int hasDigi
37a13 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30  t = 0;.  for(i=0
37a14 3b 20 69 3c 6e 49 6e 3b 20 69 2b 2b 29 7b 0a 20  ; i<nIn; i++){. 
37a15 20 20 20 69 6e 74 20 63 20 3d 20 7a 49 6e 5b 69     int c = zIn[i
37a16 5d 3b 0a 20 20 20 20 69 66 28 20 63 3e 3d 27 41  ];.    if( c>='A
37a17 27 20 26 26 20 63 3c 3d 27 5a 27 20 29 7b 0a 20  ' && c<='Z' ){. 
37a18 20 20 20 20 20 7a 4f 75 74 5b 69 5d 20 3d 20 63       zOut[i] = c
37a19 20 2d 20 27 41 27 20 2b 20 27 61 27 3b 0a 20 20   - 'A' + 'a';.  
37a1a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
37a1b 66 28 20 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d  f( c>='0' && c<=
37a1c 27 39 27 20 29 20 68 61 73 44 69 67 69 74 20 3d  '9' ) hasDigit =
37a1d 20 31 3b 0a 20 20 20 20 20 20 7a 4f 75 74 5b 69   1;.      zOut[i
37a1e 5d 20 3d 20 63 3b 0a 20 20 20 20 7d 0a 20 20 7d  ] = c;.    }.  }
37a1f 0a 20 20 6d 78 20 3d 20 68 61 73 44 69 67 69 74  .  mx = hasDigit
37a20 20 3f 20 33 20 3a 20 31 30 3b 0a 20 20 69 66 28   ? 3 : 10;.  if(
37a21 20 6e 49 6e 3e 6d 78 2a 32 20 29 7b 0a 20 20 20   nIn>mx*2 ){.   
37a22 20 66 6f 72 28 6a 3d 6d 78 2c 20 69 3d 6e 49 6e   for(j=mx, i=nIn
37a23 2d 6d 78 3b 20 69 3c 6e 49 6e 3b 20 69 2b 2b 2c  -mx; i<nIn; i++,
37a24 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 4f 75   j++){.      zOu
37a25 74 5b 6a 5d 20 3d 20 7a 4f 75 74 5b 69 5d 3b 0a  t[j] = zOut[i];.
37a26 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20 6a 3b      }.    i = j;
37a27 0a 20 20 7d 0a 20 20 7a 4f 75 74 5b 69 5d 20 3d  .  }.  zOut[i] =
37a28 20 30 3b 0a 20 20 2a 70 6e 4f 75 74 20 3d 20 69   0;.  *pnOut = i
37a29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 6d  ;.}.../*.** Stem
37a2a 20 74 68 65 20 69 6e 70 75 74 20 77 6f 72 64 20   the input word 
37a2b 7a 49 6e 5b 30 2e 2e 6e 49 6e 2d 31 5d 2e 20 20  zIn[0..nIn-1].  
37a2c 53 74 6f 72 65 20 74 68 65 20 6f 75 74 70 75 74  Store the output
37a2d 20 69 6e 20 7a 4f 75 74 2e 0a 2a 2a 20 7a 4f 75   in zOut..** zOu
37a2e 74 20 69 73 20 61 74 20 6c 65 61 73 74 20 62 69  t is at least bi
37a2f 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64  g enough to hold
37a30 20 6e 49 6e 20 62 79 74 65 73 2e 20 20 57 72 69   nIn bytes.  Wri
37a31 74 65 20 74 68 65 20 61 63 74 75 61 6c 0a 2a 2a  te the actual.**
37a32 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 74   size of the out
37a33 70 75 74 20 77 6f 72 64 20 28 65 78 63 6c 75 73  put word (exclus
37a34 69 76 65 20 6f 66 20 74 68 65 20 27 5c 30 27 20  ive of the '\0' 
37a35 74 65 72 6d 69 6e 61 74 6f 72 29 20 69 6e 74 6f  terminator) into
37a36 20 2a 70 6e 4f 75 74 2e 0a 2a 2a 0a 2a 2a 20 41   *pnOut..**.** A
37a37 6e 79 20 75 70 70 65 72 2d 63 61 73 65 20 63 68  ny upper-case ch
37a38 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 20  aracters in the 
37a39 55 53 2d 41 53 43 49 49 20 63 68 61 72 61 63 74  US-ASCII charact
37a3a 65 72 20 73 65 74 20 28 5b 41 2d 5a 5d 29 0a 2a  er set ([A-Z]).*
37a3b 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64 20  * are converted 
37a3c 74 6f 20 6c 6f 77 65 72 20 63 61 73 65 2e 20 20  to lower case.  
37a3d 55 70 70 65 72 2d 63 61 73 65 20 55 54 46 20 63  Upper-case UTF c
37a3e 68 61 72 61 63 74 65 72 73 20 61 72 65 0a 2a 2a  haracters are.**
37a3f 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a   unchanged..**.*
37a40 2a 20 57 6f 72 64 73 20 74 68 61 74 20 61 72 65  * Words that are
37a41 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 61 62 6f   longer than abo
37a42 75 74 20 32 30 20 62 79 74 65 73 20 61 72 65 20  ut 20 bytes are 
37a43 73 74 65 6d 6d 65 64 20 62 79 20 72 65 74 61 69  stemmed by retai
37a44 6e 69 6e 67 0a 2a 2a 20 61 20 66 65 77 20 62 79  ning.** a few by
37a45 74 65 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67  tes from the beg
37a46 69 6e 6e 69 6e 67 20 61 6e 64 20 74 68 65 20 65  inning and the e
37a47 6e 64 20 6f 66 20 74 68 65 20 77 6f 72 64 2e 20  nd of the word. 
37a48 20 49 66 20 74 68 65 0a 2a 2a 20 77 6f 72 64 20   If the.** word 
37a49 63 6f 6e 74 61 69 6e 73 20 64 69 67 69 74 73 2c  contains digits,
37a4a 20 33 20 62 79 74 65 73 20 61 72 65 20 74 61 6b   3 bytes are tak
37a4b 65 6e 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69  en from the begi
37a4c 6e 6e 69 6e 67 20 61 6e 64 0a 2a 2a 20 33 20 62  nning and.** 3 b
37a4d 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 65 6e  ytes from the en
37a4e 64 2e 20 20 46 6f 72 20 6c 6f 6e 67 20 77 6f 72  d.  For long wor
37a4f 64 73 20 77 69 74 68 6f 75 74 20 64 69 67 69 74  ds without digit
37a50 73 2c 20 31 30 20 62 79 74 65 73 0a 2a 2a 20 61  s, 10 bytes.** a
37a51 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 65 61  re taken from ea
37a52 63 68 20 65 6e 64 2e 20 20 55 53 2d 41 53 43 49  ch end.  US-ASCI
37a53 49 20 63 61 73 65 20 66 6f 6c 64 69 6e 67 20 73  I case folding s
37a54 74 69 6c 6c 20 61 70 70 6c 69 65 73 2e 0a 2a 2a  till applies..**
37a55 20 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75   .** If the inpu
37a56 74 20 77 6f 72 64 20 63 6f 6e 74 61 69 6e 73 20  t word contains 
37a57 6e 6f 74 20 64 69 67 69 74 73 20 62 75 74 20 64  not digits but d
37a58 6f 65 73 20 63 68 61 72 61 63 74 65 72 73 20 6e  oes characters n
37a59 6f 74 20 0a 2a 2a 20 69 6e 20 5b 61 2d 7a 41 2d  ot .** in [a-zA-
37a5a 5a 5d 20 74 68 65 6e 20 6e 6f 20 73 74 65 6d 6d  Z] then no stemm
37a5b 69 6e 67 20 69 73 20 61 74 74 65 6d 70 74 65 64  ing is attempted
37a5c 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e   and this routin
37a5d 65 20 6a 75 73 74 20 0a 2a 2a 20 63 6f 70 69 65  e just .** copie
37a5e 73 20 74 68 65 20 69 6e 70 75 74 20 69 6e 74 6f  s the input into
37a5f 20 74 68 65 20 69 6e 70 75 74 20 69 6e 74 6f 20   the input into 
37a60 74 68 65 20 6f 75 74 70 75 74 20 77 69 74 68 20  the output with 
37a61 55 53 2d 41 53 43 49 49 0a 2a 2a 20 63 61 73 65  US-ASCII.** case
37a62 20 66 6f 6c 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20   folding..**.** 
37a63 53 74 65 6d 6d 69 6e 67 20 6e 65 76 65 72 20 69  Stemming never i
37a64 6e 63 72 65 61 73 65 73 20 74 68 65 20 6c 65 6e  ncreases the len
37a65 67 74 68 20 6f 66 20 74 68 65 20 77 6f 72 64 2e  gth of the word.
37a66 20 20 53 6f 20 74 68 65 72 65 20 69 73 0a 2a 2a    So there is.**
37a67 20 6e 6f 20 63 68 61 6e 63 65 20 6f 66 20 6f 76   no chance of ov
37a68 65 72 66 6c 6f 77 69 6e 67 20 74 68 65 20 7a 4f  erflowing the zO
37a69 75 74 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74  ut buffer..*/.st
37a6a 61 74 69 63 20 76 6f 69 64 20 70 6f 72 74 65 72  atic void porter
37a6b 5f 73 74 65 6d 6d 65 72 28 63 6f 6e 73 74 20 63  _stemmer(const c
37a6c 68 61 72 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 49  har *zIn, int nI
37a6d 6e 2c 20 63 68 61 72 20 2a 7a 4f 75 74 2c 20 69  n, char *zOut, i
37a6e 6e 74 20 2a 70 6e 4f 75 74 29 7b 0a 20 20 69 6e  nt *pnOut){.  in
37a6f 74 20 69 2c 20 6a 2c 20 63 3b 0a 20 20 63 68 61  t i, j, c;.  cha
37a70 72 20 7a 52 65 76 65 72 73 65 5b 32 38 5d 3b 0a  r zReverse[28];.
37a71 20 20 63 68 61 72 20 2a 7a 2c 20 2a 7a 32 3b 0a    char *z, *z2;.
37a72 20 20 69 66 28 20 6e 49 6e 3c 33 20 7c 7c 20 6e    if( nIn<3 || n
37a73 49 6e 3e 3d 73 69 7a 65 6f 66 28 7a 52 65 76 65  In>=sizeof(zReve
37a74 72 73 65 29 2d 37 20 29 7b 0a 20 20 20 20 2f 2a  rse)-7 ){.    /*
37a75 20 54 68 65 20 77 6f 72 64 20 69 73 20 74 6f 6f   The word is too
37a76 20 62 69 67 20 6f 72 20 74 6f 6f 20 73 6d 61 6c   big or too smal
37a77 6c 20 66 6f 72 20 74 68 65 20 70 6f 72 74 65 72  l for the porter
37a78 20 73 74 65 6d 6d 65 72 2e 0a 20 20 20 20 2a 2a   stemmer..    **
37a79 20 46 61 6c 6c 62 61 63 6b 20 74 6f 20 74 68 65   Fallback to the
37a7a 20 63 6f 70 79 20 73 74 65 6d 6d 65 72 20 2a 2f   copy stemmer */
37a7b 0a 20 20 20 20 63 6f 70 79 5f 73 74 65 6d 6d 65  .    copy_stemme
37a7c 72 28 7a 49 6e 2c 20 6e 49 6e 2c 20 7a 4f 75 74  r(zIn, nIn, zOut
37a7d 2c 20 70 6e 4f 75 74 29 3b 0a 20 20 20 20 72 65  , pnOut);.    re
37a7e 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  turn;.  }.  for(
37a7f 69 3d 30 2c 20 6a 3d 73 69 7a 65 6f 66 28 7a 52  i=0, j=sizeof(zR
37a80 65 76 65 72 73 65 29 2d 36 3b 20 69 3c 6e 49 6e  everse)-6; i<nIn
37a81 3b 20 69 2b 2b 2c 20 6a 2d 2d 29 7b 0a 20 20 20  ; i++, j--){.   
37a82 20 63 20 3d 20 7a 49 6e 5b 69 5d 3b 0a 20 20 20   c = zIn[i];.   
37a83 20 69 66 28 20 63 3e 3d 27 41 27 20 26 26 20 63   if( c>='A' && c
37a84 3c 3d 27 5a 27 20 29 7b 0a 20 20 20 20 20 20 7a  <='Z' ){.      z
37a85 52 65 76 65 72 73 65 5b 6a 5d 20 3d 20 63 20 2b  Reverse[j] = c +
37a86 20 27 61 27 20 2d 20 27 41 27 3b 0a 20 20 20 20   'a' - 'A';.    
37a87 7d 65 6c 73 65 20 69 66 28 20 63 3e 3d 27 61 27  }else if( c>='a'
37a88 20 26 26 20 63 3c 3d 27 7a 27 20 29 7b 0a 20 20   && c<='z' ){.  
37a89 20 20 20 20 7a 52 65 76 65 72 73 65 5b 6a 5d 20      zReverse[j] 
37a8a 3d 20 63 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = c;.    }else{.
37a8b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 75 73 65        /* The use
37a8c 20 6f 66 20 61 20 63 68 61 72 61 63 74 65 72 20   of a character 
37a8d 6e 6f 74 20 69 6e 20 5b 61 2d 7a 41 2d 5a 5d 20  not in [a-zA-Z] 
37a8e 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 66 61  means that we fa
37a8f 6c 6c 62 61 63 6b 0a 20 20 20 20 20 20 2a 2a 20  llback.      ** 
37a90 74 6f 20 74 68 65 20 63 6f 70 79 20 73 74 65 6d  to the copy stem
37a91 6d 65 72 20 2a 2f 0a 20 20 20 20 20 20 63 6f 70  mer */.      cop
37a92 79 5f 73 74 65 6d 6d 65 72 28 7a 49 6e 2c 20 6e  y_stemmer(zIn, n
37a93 49 6e 2c 20 7a 4f 75 74 2c 20 70 6e 4f 75 74 29  In, zOut, pnOut)
37a94 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
37a95 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73      }.  }.  mems
37a96 65 74 28 26 7a 52 65 76 65 72 73 65 5b 73 69 7a  et(&zReverse[siz
37a97 65 6f 66 28 7a 52 65 76 65 72 73 65 29 2d 35 5d  eof(zReverse)-5]
37a98 2c 20 30 2c 20 35 29 3b 0a 20 20 7a 20 3d 20 26  , 0, 5);.  z = &
37a99 7a 52 65 76 65 72 73 65 5b 6a 2b 31 5d 3b 0a 0a  zReverse[j+1];..
37a9a 0a 20 20 2f 2a 20 53 74 65 70 20 31 61 20 2a 2f  .  /* Step 1a */
37a9b 0a 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 73 27  .  if( z[0]=='s'
37a9c 20 29 7b 0a 20 20 20 20 69 66 28 0a 20 20 20 20   ){.    if(.    
37a9d 20 21 73 74 65 6d 28 26 7a 2c 20 22 73 65 73 73   !stem(&z, "sess
37a9e 22 2c 20 22 73 73 22 2c 20 30 29 20 26 26 0a 20  ", "ss", 0) &&. 
37a9f 20 20 20 20 21 73 74 65 6d 28 26 7a 2c 20 22 73      !stem(&z, "s
37aa0 65 69 22 2c 20 22 69 22 2c 20 30 29 20 20 26 26  ei", "i", 0)  &&
37aa1 0a 20 20 20 20 20 21 73 74 65 6d 28 26 7a 2c 20  .     !stem(&z, 
37aa2 22 73 73 22 2c 20 22 73 73 22 2c 20 30 29 0a 20  "ss", "ss", 0). 
37aa3 20 20 20 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b     ){.      z++;
37aa4 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
37aa5 20 53 74 65 70 20 31 62 20 2a 2f 20 20 0a 20 20   Step 1b */  .  
37aa6 7a 32 20 3d 20 7a 3b 0a 20 20 69 66 28 20 73 74  z2 = z;.  if( st
37aa7 65 6d 28 26 7a 2c 20 22 64 65 65 22 2c 20 22 65  em(&z, "dee", "e
37aa8 65 22 2c 20 6d 5f 67 74 5f 30 29 20 29 7b 0a 20  e", m_gt_0) ){. 
37aa9 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67     /* Do nothing
37aaa 2e 20 20 54 68 65 20 77 6f 72 6b 20 77 61 73 20  .  The work was 
37aab 61 6c 6c 20 69 6e 20 74 68 65 20 74 65 73 74 20  all in the test 
37aac 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 0a  */.  }else if( .
37aad 20 20 20 20 20 28 73 74 65 6d 28 26 7a 2c 20 22       (stem(&z, "
37aae 67 6e 69 22 2c 20 22 22 2c 20 68 61 73 56 6f 77  gni", "", hasVow
37aaf 65 6c 29 20 7c 7c 20 73 74 65 6d 28 26 7a 2c 20  el) || stem(&z, 
37ab0 22 64 65 22 2c 20 22 22 2c 20 68 61 73 56 6f 77  "de", "", hasVow
37ab1 65 6c 29 29 0a 20 20 20 20 20 20 26 26 20 7a 21  el)).      && z!
37ab2 3d 7a 32 0a 20 20 29 7b 0a 20 20 20 20 20 69 66  =z2.  ){.     if
37ab3 28 20 73 74 65 6d 28 26 7a 2c 20 22 74 61 22 2c  ( stem(&z, "ta",
37ab4 20 22 61 74 65 22 2c 20 30 29 20 7c 7c 0a 20 20   "ate", 0) ||.  
37ab5 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20         stem(&z, 
37ab6 22 6c 62 22 2c 20 22 62 6c 65 22 2c 20 30 29 20  "lb", "ble", 0) 
37ab7 7c 7c 0a 20 20 20 20 20 20 20 20 20 73 74 65 6d  ||.         stem
37ab8 28 26 7a 2c 20 22 7a 69 22 2c 20 22 69 7a 65 22  (&z, "zi", "ize"
37ab9 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20 20 2f  , 0) ){.       /
37aba 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 54  * Do nothing.  T
37abb 68 65 20 77 6f 72 6b 20 77 61 73 20 61 6c 6c 20  he work was all 
37abc 69 6e 20 74 68 65 20 74 65 73 74 20 2a 2f 0a 20  in the test */. 
37abd 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 64 6f      }else if( do
37abe 75 62 6c 65 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29  ubleConsonant(z)
37abf 20 26 26 20 28 2a 7a 21 3d 27 6c 27 20 26 26 20   && (*z!='l' && 
37ac0 2a 7a 21 3d 27 73 27 20 26 26 20 2a 7a 21 3d 27  *z!='s' && *z!='
37ac1 7a 27 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 2b  z') ){.       z+
37ac2 2b 3b 0a 20 20 20 20 20 7d 65 6c 73 65 20 69 66  +;.     }else if
37ac3 28 20 6d 5f 65 71 5f 31 28 7a 29 20 26 26 20 73  ( m_eq_1(z) && s
37ac4 74 61 72 5f 6f 68 28 7a 29 20 29 7b 0a 20 20 20  tar_oh(z) ){.   
37ac5 20 20 20 20 2a 28 2d 2d 7a 29 20 3d 20 27 65 27      *(--z) = 'e'
37ac6 3b 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  ;.     }.  }..  
37ac7 2f 2a 20 53 74 65 70 20 31 63 20 2a 2f 0a 20 20  /* Step 1c */.  
37ac8 69 66 28 20 7a 5b 30 5d 3d 3d 27 79 27 20 26 26  if( z[0]=='y' &&
37ac9 20 68 61 73 56 6f 77 65 6c 28 7a 2b 31 29 20 29   hasVowel(z+1) )
37aca 7b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 27 69 27  {.    z[0] = 'i'
37acb 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70  ;.  }..  /* Step
37acc 20 32 20 2a 2f 0a 20 20 73 77 69 74 63 68 28 20   2 */.  switch( 
37acd 7a 5b 31 5d 20 29 7b 0a 20 20 20 63 61 73 65 20  z[1] ){.   case 
37ace 27 61 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  'a':.     stem(&
37acf 7a 2c 20 22 6c 61 6e 6f 69 74 61 22 2c 20 22 61  z, "lanoita", "a
37ad0 74 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  te", m_gt_0) ||.
37ad1 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c       stem(&z, "l
37ad2 61 6e 6f 69 74 22 2c 20 22 74 69 6f 6e 22 2c 20  anoit", "tion", 
37ad3 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72  m_gt_0);.     br
37ad4 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 63 27  eak;.   case 'c'
37ad5 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  :.     stem(&z, 
37ad6 22 69 63 6e 65 22 2c 20 22 65 6e 63 65 22 2c 20  "icne", "ence", 
37ad7 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
37ad8 73 74 65 6d 28 26 7a 2c 20 22 69 63 6e 61 22 2c  stem(&z, "icna",
37ad9 20 22 61 6e 63 65 22 2c 20 6d 5f 67 74 5f 30 29   "ance", m_gt_0)
37ada 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  ;.     break;.  
37adb 20 63 61 73 65 20 27 65 27 3a 0a 20 20 20 20 20   case 'e':.     
37adc 73 74 65 6d 28 26 7a 2c 20 22 72 65 7a 69 22 2c  stem(&z, "rezi",
37add 20 22 69 7a 65 22 2c 20 6d 5f 67 74 5f 30 29 3b   "ize", m_gt_0);
37ade 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
37adf 63 61 73 65 20 27 67 27 3a 0a 20 20 20 20 20 73  case 'g':.     s
37ae0 74 65 6d 28 26 7a 2c 20 22 69 67 6f 6c 22 2c 20  tem(&z, "igol", 
37ae1 22 6c 6f 67 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a  "log", m_gt_0);.
37ae2 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
37ae3 61 73 65 20 27 6c 27 3a 0a 20 20 20 20 20 73 74  ase 'l':.     st
37ae4 65 6d 28 26 7a 2c 20 22 69 6c 62 22 2c 20 22 62  em(&z, "ilb", "b
37ae5 6c 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  le", m_gt_0) ||.
37ae6 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
37ae7 6c 6c 61 22 2c 20 22 61 6c 22 2c 20 6d 5f 67 74  lla", "al", m_gt
37ae8 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  _0) ||.     stem
37ae9 28 26 7a 2c 20 22 69 6c 74 6e 65 22 2c 20 22 65  (&z, "iltne", "e
37aea 6e 74 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  nt", m_gt_0) ||.
37aeb 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
37aec 6c 65 22 2c 20 22 65 22 2c 20 6d 5f 67 74 5f 30  le", "e", m_gt_0
37aed 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
37aee 7a 2c 20 22 69 6c 73 75 6f 22 2c 20 22 6f 75 73  z, "ilsuo", "ous
37aef 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20  ", m_gt_0);.    
37af0 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
37af1 27 6f 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  'o':.     stem(&
37af2 7a 2c 20 22 6e 6f 69 74 61 7a 69 22 2c 20 22 69  z, "noitazi", "i
37af3 7a 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  ze", m_gt_0) ||.
37af4 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6e       stem(&z, "n
37af5 6f 69 74 61 22 2c 20 22 61 74 65 22 2c 20 6d 5f  oita", "ate", m_
37af6 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74  gt_0) ||.     st
37af7 65 6d 28 26 7a 2c 20 22 72 6f 74 61 22 2c 20 22  em(&z, "rota", "
37af8 61 74 65 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20  ate", m_gt_0);. 
37af9 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61      break;.   ca
37afa 73 65 20 27 73 27 3a 0a 20 20 20 20 20 73 74 65  se 's':.     ste
37afb 6d 28 26 7a 2c 20 22 6d 73 69 6c 61 22 2c 20 22  m(&z, "msila", "
37afc 61 6c 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  al", m_gt_0) ||.
37afd 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 73       stem(&z, "s
37afe 73 65 6e 65 76 69 22 2c 20 22 69 76 65 22 2c 20  senevi", "ive", 
37aff 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
37b00 73 74 65 6d 28 26 7a 2c 20 22 73 73 65 6e 6c 75  stem(&z, "ssenlu
37b01 66 22 2c 20 22 66 75 6c 22 2c 20 6d 5f 67 74 5f  f", "ful", m_gt_
37b02 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28  0) ||.     stem(
37b03 26 7a 2c 20 22 73 73 65 6e 73 75 6f 22 2c 20 22  &z, "ssensuo", "
37b04 6f 75 73 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20  ous", m_gt_0);. 
37b05 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61      break;.   ca
37b06 73 65 20 27 74 27 3a 0a 20 20 20 20 20 73 74 65  se 't':.     ste
37b07 6d 28 26 7a 2c 20 22 69 74 69 6c 61 22 2c 20 22  m(&z, "itila", "
37b08 61 6c 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  al", m_gt_0) ||.
37b09 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
37b0a 74 69 76 69 22 2c 20 22 69 76 65 22 2c 20 6d 5f  tivi", "ive", m_
37b0b 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74  gt_0) ||.     st
37b0c 65 6d 28 26 7a 2c 20 22 69 74 69 6c 69 62 22 2c  em(&z, "itilib",
37b0d 20 22 62 6c 65 22 2c 20 6d 5f 67 74 5f 30 29 3b   "ble", m_gt_0);
37b0e 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d  .     break;.  }
37b0f 0a 0a 20 20 2f 2a 20 53 74 65 70 20 33 20 2a 2f  ..  /* Step 3 */
37b10 0a 20 20 73 77 69 74 63 68 28 20 7a 5b 30 5d 20  .  switch( z[0] 
37b11 29 7b 0a 20 20 20 63 61 73 65 20 27 65 27 3a 0a  ){.   case 'e':.
37b12 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 65       stem(&z, "e
37b13 74 61 63 69 22 2c 20 22 69 63 22 2c 20 6d 5f 67  taci", "ic", m_g
37b14 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65  t_0) ||.     ste
37b15 6d 28 26 7a 2c 20 22 65 76 69 74 61 22 2c 20 22  m(&z, "evita", "
37b16 22 2c 20 6d 5f 67 74 5f 30 29 20 20 20 7c 7c 0a  ", m_gt_0)   ||.
37b17 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 65       stem(&z, "e
37b18 7a 69 6c 61 22 2c 20 22 61 6c 22 2c 20 6d 5f 67  zila", "al", m_g
37b19 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b  t_0);.     break
37b1a 3b 0a 20 20 20 63 61 73 65 20 27 69 27 3a 0a 20  ;.   case 'i':. 
37b1b 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 74      stem(&z, "it
37b1c 69 63 69 22 2c 20 22 69 63 22 2c 20 6d 5f 67 74  ici", "ic", m_gt
37b1d 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b  _0);.     break;
37b1e 0a 20 20 20 63 61 73 65 20 27 6c 27 3a 0a 20 20  .   case 'l':.  
37b1f 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c 61 63     stem(&z, "lac
37b20 69 22 2c 20 22 69 63 22 2c 20 6d 5f 67 74 5f 30  i", "ic", m_gt_0
37b21 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
37b22 7a 2c 20 22 6c 75 66 22 2c 20 22 22 2c 20 6d 5f  z, "luf", "", m_
37b23 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61  gt_0);.     brea
37b24 6b 3b 0a 20 20 20 63 61 73 65 20 27 73 27 3a 0a  k;.   case 's':.
37b25 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 73       stem(&z, "s
37b26 73 65 6e 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 30  sen", "", m_gt_0
37b27 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  );.     break;. 
37b28 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 34 20   }..  /* Step 4 
37b29 2a 2f 0a 20 20 73 77 69 74 63 68 28 20 7a 5b 31  */.  switch( z[1
37b2a 5d 20 29 7b 0a 20 20 20 63 61 73 65 20 27 61 27  ] ){.   case 'a'
37b2b 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d  :.     if( z[0]=
37b2c 3d 27 6c 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a  ='l' && m_gt_1(z
37b2d 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20  +2) ){.       z 
37b2e 2b 3d 20 32 3b 0a 20 20 20 20 20 7d 0a 20 20 20  += 2;.     }.   
37b2f 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
37b30 20 27 63 27 3a 0a 20 20 20 20 20 69 66 28 20 7a   'c':.     if( z
37b31 5b 30 5d 3d 3d 27 65 27 20 26 26 20 7a 5b 32 5d  [0]=='e' && z[2]
37b32 3d 3d 27 6e 27 20 26 26 20 28 7a 5b 33 5d 3d 3d  =='n' && (z[3]==
37b33 27 61 27 20 7c 7c 20 7a 5b 33 5d 3d 3d 27 65 27  'a' || z[3]=='e'
37b34 29 20 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 34  )  && m_gt_1(z+4
37b35 29 20 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b  )  ){.       z +
37b36 3d 20 34 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20  = 4;.     }.    
37b37 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
37b38 27 65 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b  'e':.     if( z[
37b39 30 5d 3d 3d 27 72 27 20 26 26 20 6d 5f 67 74 5f  0]=='r' && m_gt_
37b3a 31 28 7a 2b 32 29 20 29 7b 0a 20 20 20 20 20 20  1(z+2) ){.      
37b3b 20 7a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 7d 0a   z += 2;.     }.
37b3c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
37b3d 61 73 65 20 27 69 27 3a 0a 20 20 20 20 20 69 66  ase 'i':.     if
37b3e 28 20 7a 5b 30 5d 3d 3d 27 63 27 20 26 26 20 6d  ( z[0]=='c' && m
37b3f 5f 67 74 5f 31 28 7a 2b 32 29 20 29 7b 0a 20 20  _gt_1(z+2) ){.  
37b40 20 20 20 20 20 7a 20 2b 3d 20 32 3b 0a 20 20 20       z += 2;.   
37b41 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a    }.     break;.
37b42 20 20 20 63 61 73 65 20 27 6c 27 3a 0a 20 20 20     case 'l':.   
37b43 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 65 27 20    if( z[0]=='e' 
37b44 26 26 20 7a 5b 32 5d 3d 3d 27 62 27 20 26 26 20  && z[2]=='b' && 
37b45 28 7a 5b 33 5d 3d 3d 27 61 27 20 7c 7c 20 7a 5b  (z[3]=='a' || z[
37b46 33 5d 3d 3d 27 69 27 29 20 26 26 20 6d 5f 67 74  3]=='i') && m_gt
37b47 5f 31 28 7a 2b 34 29 20 29 7b 0a 20 20 20 20 20  _1(z+4) ){.     
37b48 20 20 7a 20 2b 3d 20 34 3b 0a 20 20 20 20 20 7d    z += 4;.     }
37b49 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
37b4a 63 61 73 65 20 27 6e 27 3a 0a 20 20 20 20 20 69  case 'n':.     i
37b4b 66 28 20 7a 5b 30 5d 3d 3d 27 74 27 20 29 7b 0a  f( z[0]=='t' ){.
37b4c 20 20 20 20 20 20 20 69 66 28 20 7a 5b 32 5d 3d         if( z[2]=
37b4d 3d 27 61 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='a' ){.        
37b4e 20 69 66 28 20 6d 5f 67 74 5f 31 28 7a 2b 33 29   if( m_gt_1(z+3)
37b4f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 7a   ){.           z
37b50 20 2b 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20   += 3;.         
37b51 7d 0a 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  }.       }else i
37b52 66 28 20 7a 5b 32 5d 3d 3d 27 65 27 20 29 7b 0a  f( z[2]=='e' ){.
37b53 20 20 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a           stem(&z
37b54 2c 20 22 74 6e 65 6d 65 22 2c 20 22 22 2c 20 6d  , "tneme", "", m
37b55 5f 67 74 5f 31 29 20 7c 7c 0a 20 20 20 20 20 20  _gt_1) ||.      
37b56 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 74 6e 65     stem(&z, "tne
37b57 6d 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29 20  m", "", m_gt_1) 
37b58 7c 7c 0a 20 20 20 20 20 20 20 20 20 73 74 65 6d  ||.         stem
37b59 28 26 7a 2c 20 22 74 6e 65 22 2c 20 22 22 2c 20  (&z, "tne", "", 
37b5a 6d 5f 67 74 5f 31 29 3b 0a 20 20 20 20 20 20 20  m_gt_1);.       
37b5b 7d 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72  }.     }.     br
37b5c 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 6f 27  eak;.   case 'o'
37b5d 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d  :.     if( z[0]=
37b5e 3d 27 75 27 20 29 7b 0a 20 20 20 20 20 20 20 69  ='u' ){.       i
37b5f 66 28 20 6d 5f 67 74 5f 31 28 7a 2b 32 29 20 29  f( m_gt_1(z+2) )
37b60 7b 0a 20 20 20 20 20 20 20 20 20 7a 20 2b 3d 20  {.         z += 
37b61 32 3b 0a 20 20 20 20 20 20 20 7d 0a 20 20 20 20  2;.       }.    
37b62 20 7d 65 6c 73 65 20 69 66 28 20 7a 5b 33 5d 3d   }else if( z[3]=
37b63 3d 27 73 27 20 7c 7c 20 7a 5b 33 5d 3d 3d 27 74  ='s' || z[3]=='t
37b64 27 20 29 7b 0a 20 20 20 20 20 20 20 73 74 65 6d  ' ){.       stem
37b65 28 26 7a 2c 20 22 6e 6f 69 22 2c 20 22 22 2c 20  (&z, "noi", "", 
37b66 6d 5f 67 74 5f 31 29 3b 0a 20 20 20 20 20 7d 0a  m_gt_1);.     }.
37b67 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
37b68 61 73 65 20 27 73 27 3a 0a 20 20 20 20 20 69 66  ase 's':.     if
37b69 28 20 7a 5b 30 5d 3d 3d 27 6d 27 20 26 26 20 7a  ( z[0]=='m' && z
37b6a 5b 32 5d 3d 3d 27 69 27 20 26 26 20 6d 5f 67 74  [2]=='i' && m_gt
37b6b 5f 31 28 7a 2b 33 29 20 29 7b 0a 20 20 20 20 20  _1(z+3) ){.     
37b6c 20 20 7a 20 2b 3d 20 33 3b 0a 20 20 20 20 20 7d    z += 3;.     }
37b6d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
37b6e 63 61 73 65 20 27 74 27 3a 0a 20 20 20 20 20 73  case 't':.     s
37b6f 74 65 6d 28 26 7a 2c 20 22 65 74 61 22 2c 20 22  tem(&z, "eta", "
37b70 22 2c 20 6d 5f 67 74 5f 31 29 20 7c 7c 0a 20 20  ", m_gt_1) ||.  
37b71 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 74 69     stem(&z, "iti
37b72 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29 3b 0a  ", "", m_gt_1);.
37b73 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
37b74 61 73 65 20 27 75 27 3a 0a 20 20 20 20 20 69 66  ase 'u':.     if
37b75 28 20 7a 5b 30 5d 3d 3d 27 73 27 20 26 26 20 7a  ( z[0]=='s' && z
37b76 5b 32 5d 3d 3d 27 6f 27 20 26 26 20 6d 5f 67 74  [2]=='o' && m_gt
37b77 5f 31 28 7a 2b 33 29 20 29 7b 0a 20 20 20 20 20  _1(z+3) ){.     
37b78 20 20 7a 20 2b 3d 20 33 3b 0a 20 20 20 20 20 7d    z += 3;.     }
37b79 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
37b7a 63 61 73 65 20 27 76 27 3a 0a 20 20 20 63 61 73  case 'v':.   cas
37b7b 65 20 27 7a 27 3a 0a 20 20 20 20 20 69 66 28 20  e 'z':.     if( 
37b7c 7a 5b 30 5d 3d 3d 27 65 27 20 26 26 20 7a 5b 32  z[0]=='e' && z[2
37b7d 5d 3d 3d 27 69 27 20 26 26 20 6d 5f 67 74 5f 31  ]=='i' && m_gt_1
37b7e 28 7a 2b 33 29 20 29 7b 0a 20 20 20 20 20 20 20  (z+3) ){.       
37b7f 7a 20 2b 3d 20 33 3b 0a 20 20 20 20 20 7d 0a 20  z += 3;.     }. 
37b80 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a      break;.  }..
37b81 20 20 2f 2a 20 53 74 65 70 20 35 61 20 2a 2f 0a    /* Step 5a */.
37b82 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 65 27 20    if( z[0]=='e' 
37b83 29 7b 0a 20 20 20 20 69 66 28 20 6d 5f 67 74 5f  ){.    if( m_gt_
37b84 31 28 7a 2b 31 29 20 29 7b 0a 20 20 20 20 20 20  1(z+1) ){.      
37b85 7a 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  z++;.    }else i
37b86 66 28 20 6d 5f 65 71 5f 31 28 7a 2b 31 29 20 26  f( m_eq_1(z+1) &
37b87 26 20 21 73 74 61 72 5f 6f 68 28 7a 2b 31 29 20  & !star_oh(z+1) 
37b88 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20  ){.      z++;.  
37b89 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74    }.  }..  /* St
37b8a 65 70 20 35 62 20 2a 2f 0a 20 20 69 66 28 20 6d  ep 5b */.  if( m
37b8b 5f 67 74 5f 31 28 7a 29 20 26 26 20 7a 5b 30 5d  _gt_1(z) && z[0]
37b8c 3d 3d 27 6c 27 20 26 26 20 7a 5b 31 5d 3d 3d 27  =='l' && z[1]=='
37b8d 6c 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20  l' ){.    z++;. 
37b8e 20 7d 0a 0a 20 20 2f 2a 20 7a 5b 5d 20 69 73 20   }..  /* z[] is 
37b8f 6e 6f 77 20 74 68 65 20 73 74 65 6d 6d 65 64 20  now the stemmed 
37b90 77 6f 72 64 20 69 6e 20 72 65 76 65 72 73 65 20  word in reverse 
37b91 6f 72 64 65 72 2e 20 20 46 6c 69 70 20 69 74 20  order.  Flip it 
37b92 62 61 63 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64  back.  ** around
37b93 20 69 6e 74 6f 20 66 6f 72 77 61 72 64 20 6f 72   into forward or
37b94 64 65 72 20 61 6e 64 20 72 65 74 75 72 6e 2e 0a  der and return..
37b95 20 20 2a 2f 0a 20 20 2a 70 6e 4f 75 74 20 3d 20    */.  *pnOut = 
37b96 69 20 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20  i = strlen(z);. 
37b97 20 7a 4f 75 74 5b 69 5d 20 3d 20 30 3b 0a 20 20   zOut[i] = 0;.  
37b98 77 68 69 6c 65 28 20 2a 7a 20 29 7b 0a 20 20 20  while( *z ){.   
37b99 20 7a 4f 75 74 5b 2d 2d 69 5d 20 3d 20 2a 28 7a   zOut[--i] = *(z
37b9a 2b 2b 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ++);.  }.}../*.*
37b9b 2a 20 43 68 61 72 61 63 74 65 72 73 20 74 68 61  * Characters tha
37b9c 74 20 63 61 6e 20 62 65 20 70 61 72 74 20 6f 66  t can be part of
37b9d 20 61 20 74 6f 6b 65 6e 2e 20 20 57 65 20 61 73   a token.  We as
37b9e 73 75 6d 65 20 61 6e 79 20 63 68 61 72 61 63 74  sume any charact
37b9f 65 72 0a 2a 2a 20 77 68 6f 73 65 20 76 61 6c 75  er.** whose valu
37ba0 65 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  e is greater tha
37ba1 6e 20 30 78 38 30 20 28 61 6e 79 20 55 54 46 20  n 0x80 (any UTF 
37ba2 63 68 61 72 61 63 74 65 72 29 20 63 61 6e 20 62  character) can b
37ba3 65 0a 2a 2a 20 70 61 72 74 20 6f 66 20 61 20 74  e.** part of a t
37ba4 6f 6b 65 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20  oken.  In other 
37ba5 77 6f 72 64 73 2c 20 64 65 6c 69 6d 69 74 65 72  words, delimiter
37ba6 73 20 61 6c 6c 20 6d 75 73 74 20 68 61 76 65 0a  s all must have.
37ba7 2a 2a 20 76 61 6c 75 65 73 20 6f 66 20 30 78 37  ** values of 0x7
37ba8 66 20 6f 72 20 6c 6f 77 65 72 2e 0a 2a 2f 0a 73  f or lower..*/.s
37ba9 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
37baa 20 70 6f 72 74 65 72 49 64 43 68 61 72 5b 5d 20   porterIdChar[] 
37bab 3d 20 7b 0a 2f 2a 20 78 30 20 78 31 20 78 32 20  = {./* x0 x1 x2 
37bac 78 33 20 78 34 20 78 35 20 78 36 20 78 37 20 78  x3 x4 x5 x6 x7 x
37bad 38 20 78 39 20 78 41 20 78 42 20 78 43 20 78 44  8 x9 xA xB xC xD
37bae 20 78 45 20 78 46 20 2a 2f 0a 20 20 20 20 31 2c   xE xF */.    1,
37baf 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
37bb0 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30  1, 1, 1, 1, 0, 0
37bb1 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 0, 0, 0,  /
37bb2 2a 20 33 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31  * 3x */.    0, 1
37bb3 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
37bb4 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
37bb5 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20  1, 1, 1, 1,  /* 
37bb6 34 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20  4x */.    1, 1, 
37bb7 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
37bb8 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c  , 1, 1, 1, 0, 0,
37bb9 20 30 2c 20 30 2c 20 31 2c 20 20 2f 2a 20 35 78   0, 0, 1,  /* 5x
37bba 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c   */.    0, 1, 1,
37bbb 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
37bbc 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
37bbd 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 36 78 20 2a  , 1, 1,  /* 6x *
37bbe 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31  /.    1, 1, 1, 1
37bbf 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
37bc0 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 1, 0, 0, 0, 
37bc1 30 2c 20 30 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a  0, 0,  /* 7x */.
37bc2 7d 3b 0a 23 64 65 66 69 6e 65 20 69 73 44 65 6c  };.#define isDel
37bc3 69 6d 28 43 29 20 28 28 28 63 68 3d 43 29 26 30  im(C) (((ch=C)&0
37bc4 78 38 30 29 3d 3d 30 20 26 26 20 28 63 68 3c 30  x80)==0 && (ch<0
37bc5 78 33 30 20 7c 7c 20 21 70 6f 72 74 65 72 49 64  x30 || !porterId
37bc6 43 68 61 72 5b 63 68 2d 30 78 33 30 5d 29 29 0a  Char[ch-0x30])).
37bc7 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74  ./*.** Extract t
37bc8 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72  he next token fr
37bc9 6f 6d 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f  om a tokenizatio
37bca 6e 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 63  n cursor.  The c
37bcb 75 72 73 6f 72 20 6d 75 73 74 0a 2a 2a 20 68 61  ursor must.** ha
37bcc 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62  ve been opened b
37bcd 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74  y a prior call t
37bce 6f 20 70 6f 72 74 65 72 4f 70 65 6e 28 29 2e 0a  o porterOpen()..
37bcf 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f  */.static int po
37bd0 72 74 65 72 4e 65 78 74 28 0a 20 20 73 71 6c 69  rterNext(.  sqli
37bd1 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
37bd2 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 20  rsor *pCursor,  
37bd3 2f 2a 20 43 75 72 73 6f 72 20 72 65 74 75 72 6e  /* Cursor return
37bd4 65 64 20 62 79 20 70 6f 72 74 65 72 4f 70 65 6e  ed by porterOpen
37bd5 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
37bd6 20 2a 2a 70 7a 54 6f 6b 65 6e 2c 20 20 20 20 20   **pzToken,     
37bd7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
37bd8 3a 20 2a 70 7a 54 6f 6b 65 6e 20 69 73 20 74 68  : *pzToken is th
37bd9 65 20 74 6f 6b 65 6e 20 74 65 78 74 20 2a 2f 0a  e token text */.
37bda 20 20 69 6e 74 20 2a 70 6e 42 79 74 65 73 2c 20    int *pnBytes, 
37bdb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37bdc 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75        /* OUT: Nu
37bdd 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
37bde 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20   token */.  int 
37bdf 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20  *piStartOffset, 
37be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37be1 2f 2a 20 4f 55 54 3a 20 53 74 61 72 74 69 6e 67  /* OUT: Starting
37be2 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e   offset of token
37be3 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 45 6e 64   */.  int *piEnd
37be4 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20  Offset,         
37be5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
37be6 3a 20 45 6e 64 69 6e 67 20 6f 66 66 73 65 74 20  : Ending offset 
37be7 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e  of token */.  in
37be8 74 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 20 20  t *piPosition   
37be9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37bea 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 73 69 74 69    /* OUT: Positi
37beb 6f 6e 20 69 6e 74 65 67 65 72 20 6f 66 20 74 6f  on integer of to
37bec 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 70 6f 72 74  ken */.){.  port
37bed 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  er_tokenizer_cur
37bee 73 6f 72 20 2a 63 20 3d 20 28 70 6f 72 74 65 72  sor *c = (porter
37bef 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
37bf0 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20  r *) pCursor;.  
37bf1 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20  const char *z = 
37bf2 63 2d 3e 7a 49 6e 70 75 74 3b 0a 0a 20 20 77 68  c->zInput;..  wh
37bf3 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c  ile( c->iOffset<
37bf4 63 2d 3e 6e 49 6e 70 75 74 20 29 7b 0a 20 20 20  c->nInput ){.   
37bf5 20 69 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65   int iStartOffse
37bf6 74 2c 20 63 68 3b 0a 0a 20 20 20 20 2f 2a 20 53  t, ch;..    /* S
37bf7 63 61 6e 20 70 61 73 74 20 64 65 6c 69 6d 69 74  can past delimit
37bf8 65 72 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f  er characters */
37bf9 0a 20 20 20 20 77 68 69 6c 65 28 20 63 2d 3e 69  .    while( c->i
37bfa 4f 66 66 73 65 74 3c 63 2d 3e 6e 49 6e 70 75 74  Offset<c->nInput
37bfb 20 26 26 20 69 73 44 65 6c 69 6d 28 7a 5b 63 2d   && isDelim(z[c-
37bfc 3e 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a 20 20  >iOffset]) ){.  
37bfd 20 20 20 20 63 2d 3e 69 4f 66 66 73 65 74 2b 2b      c->iOffset++
37bfe 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
37bff 43 6f 75 6e 74 20 6e 6f 6e 2d 64 65 6c 69 6d 69  Count non-delimi
37c00 74 65 72 20 63 68 61 72 61 63 74 65 72 73 2e 20  ter characters. 
37c01 2a 2f 0a 20 20 20 20 69 53 74 61 72 74 4f 66 66  */.    iStartOff
37c02 73 65 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74  set = c->iOffset
37c03 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 63 2d 3e  ;.    while( c->
37c04 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 49 6e 70 75  iOffset<c->nInpu
37c05 74 20 26 26 20 21 69 73 44 65 6c 69 6d 28 7a 5b  t && !isDelim(z[
37c06 63 2d 3e 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a  c->iOffset]) ){.
37c07 20 20 20 20 20 20 63 2d 3e 69 4f 66 66 73 65 74        c->iOffset
37c08 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  ++;.    }..    i
37c09 66 28 20 63 2d 3e 69 4f 66 66 73 65 74 3e 69 53  f( c->iOffset>iS
37c0a 74 61 72 74 4f 66 66 73 65 74 20 29 7b 0a 20 20  tartOffset ){.  
37c0b 20 20 20 20 69 6e 74 20 6e 20 3d 20 63 2d 3e 69      int n = c->i
37c0c 4f 66 66 73 65 74 2d 69 53 74 61 72 74 4f 66 66  Offset-iStartOff
37c0d 73 65 74 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  set;.      if( n
37c0e 3e 63 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64 20 29  >c->nAllocated )
37c0f 7b 0a 20 20 20 20 20 20 20 20 63 2d 3e 6e 41 6c  {.        c->nAl
37c10 6c 6f 63 61 74 65 64 20 3d 20 6e 2b 32 30 3b 0a  located = n+20;.
37c11 20 20 20 20 20 20 20 20 63 2d 3e 7a 54 6f 6b 65          c->zToke
37c12 6e 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  n = sqlite3_real
37c13 6c 6f 63 28 63 2d 3e 7a 54 6f 6b 65 6e 2c 20 63  loc(c->zToken, c
37c14 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64 29 3b 0a 20  ->nAllocated);. 
37c15 20 20 20 20 20 20 20 69 66 28 20 63 2d 3e 7a 54         if( c->zT
37c16 6f 6b 65 6e 3d 3d 4e 55 4c 4c 20 29 20 72 65 74  oken==NULL ) ret
37c17 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
37c18 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
37c19 70 6f 72 74 65 72 5f 73 74 65 6d 6d 65 72 28 26  porter_stemmer(&
37c1a 7a 5b 69 53 74 61 72 74 4f 66 66 73 65 74 5d 2c  z[iStartOffset],
37c1b 20 6e 2c 20 63 2d 3e 7a 54 6f 6b 65 6e 2c 20 70   n, c->zToken, p
37c1c 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 20 20 2a  nBytes);.      *
37c1d 70 7a 54 6f 6b 65 6e 20 3d 20 63 2d 3e 7a 54 6f  pzToken = c->zTo
37c1e 6b 65 6e 3b 0a 20 20 20 20 20 20 2a 70 69 53 74  ken;.      *piSt
37c1f 61 72 74 4f 66 66 73 65 74 20 3d 20 69 53 74 61  artOffset = iSta
37c20 72 74 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20  rtOffset;.      
37c21 2a 70 69 45 6e 64 4f 66 66 73 65 74 20 3d 20 63  *piEndOffset = c
37c22 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 20  ->iOffset;.     
37c23 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 3d 20 63   *piPosition = c
37c24 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a 20 20 20 20  ->iToken++;.    
37c25 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
37c26 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  OK;.    }.  }.  
37c27 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f  return SQLITE_DO
37c28 4e 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  NE;.}../*.** The
37c29 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73   set of routines
37c2a 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
37c2b 74 68 65 20 70 6f 72 74 65 72 2d 73 74 65 6d 6d  the porter-stemm
37c2c 65 72 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a  er tokenizer.*/.
37c2d 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
37c2e 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
37c2f 6f 64 75 6c 65 20 70 6f 72 74 65 72 54 6f 6b 65  odule porterToke
37c30 6e 69 7a 65 72 4d 6f 64 75 6c 65 20 3d 20 7b 0a  nizerModule = {.
37c31 20 20 30 2c 0a 20 20 70 6f 72 74 65 72 43 72 65    0,.  porterCre
37c32 61 74 65 2c 0a 20 20 70 6f 72 74 65 72 44 65 73  ate,.  porterDes
37c33 74 72 6f 79 2c 0a 20 20 70 6f 72 74 65 72 4f 70  troy,.  porterOp
37c34 65 6e 2c 0a 20 20 70 6f 72 74 65 72 43 6c 6f 73  en,.  porterClos
37c35 65 2c 0a 20 20 70 6f 72 74 65 72 4e 65 78 74 2c  e,.  porterNext,
37c36 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  .};../*.** Alloc
37c37 61 74 65 20 61 20 6e 65 77 20 70 6f 72 74 65 72  ate a new porter
37c38 20 74 6f 6b 65 6e 69 7a 65 72 2e 20 20 52 65 74   tokenizer.  Ret
37c39 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
37c3a 20 74 68 65 20 6e 65 77 0a 2a 2a 20 74 6f 6b 65   the new.** toke
37c3b 6e 69 7a 65 72 20 69 6e 20 2a 70 70 4d 6f 64 75  nizer in *ppModu
37c3c 6c 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  le.*/.SQLITE_PRI
37c3d 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
37c3e 33 46 74 73 33 50 6f 72 74 65 72 54 6f 6b 65 6e  3Fts3PorterToken
37c3f 69 7a 65 72 4d 6f 64 75 6c 65 28 0a 20 20 73 71  izerModule(.  sq
37c40 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
37c41 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70  module const**pp
37c42 4d 6f 64 75 6c 65 0a 29 7b 0a 20 20 2a 70 70 4d  Module.){.  *ppM
37c43 6f 64 75 6c 65 20 3d 20 26 70 6f 72 74 65 72 54  odule = &porterT
37c44 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a  okenizerModule;.
37c45 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  }..#endif /* !de
37c46 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
37c47 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
37c48 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
37c49 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
37c4a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73  ***** End of fts
37c4b 33 5f 70 6f 72 74 65 72 2e 63 20 2a 2a 2a 2a 2a  3_porter.c *****
37c4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37c4d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37c4e 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
37c4f 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
37c50 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e   fts3_tokenizer.
37c51 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
37c52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37c53 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
37c54 20 4a 75 6e 65 20 32 32 0a 2a 2a 0a 2a 2a 20 54   June 22.**.** T
37c55 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
37c56 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
37c57 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
37c58 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
37c59 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
37c5a 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
37c5b 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
37c5c 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
37c5d 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
37c5e 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
37c5f 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
37c60 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
37c61 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
37c62 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
37c63 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
37c64 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
37c65 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
37c66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37c67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37c68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37c69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37c6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
37c6b 2a 2a 20 54 68 69 73 20 69 73 20 70 61 72 74 20  ** This is part 
37c6c 6f 66 20 61 6e 20 53 51 4c 69 74 65 20 6d 6f 64  of an SQLite mod
37c6d 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67  ule implementing
37c6e 20 66 75 6c 6c 2d 74 65 78 74 20 73 65 61 72 63   full-text searc
37c6f 68 2e 0a 2a 2a 20 54 68 69 73 20 70 61 72 74 69  h..** This parti
37c70 63 75 6c 61 72 20 66 69 6c 65 20 69 6d 70 6c 65  cular file imple
37c71 6d 65 6e 74 73 20 74 68 65 20 67 65 6e 65 72 69  ments the generi
37c72 63 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 74 65  c tokenizer inte
37c73 72 66 61 63 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  rface..*/../*.**
37c74 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   The code in thi
37c75 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63  s file is only c
37c76 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a  ompiled if:.**.*
37c77 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53 33  *     * The FTS3
37c78 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67   module is being
37c79 20 62 75 69 6c 74 20 61 73 20 61 6e 20 65 78 74   built as an ext
37c7a 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  ension.**       
37c7b 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53  (in which case S
37c7c 51 4c 49 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f  QLITE_CORE is no
37c7d 74 20 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a 2a  t defined), or.*
37c7e 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46  *.**     * The F
37c7f 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65  TS3 module is be
37c80 69 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f 20 74  ing built into t
37c81 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20 20  he core of.**   
37c82 20 20 20 20 53 51 4c 69 74 65 20 28 69 6e 20 77      SQLite (in w
37c83 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45  hich case SQLITE
37c84 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20 69 73 20  _ENABLE_FTS3 is 
37c85 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69 66  defined)..*/.#if
37c86 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
37c87 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65  _CORE) || define
37c88 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
37c89 46 54 53 33 29 0a 0a 23 69 66 6e 64 65 66 20 53  FTS3)..#ifndef S
37c8a 51 4c 49 54 45 5f 43 4f 52 45 0a 20 20 53 51 4c  QLITE_CORE.  SQL
37c8b 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e  ITE_EXTENSION_IN
37c8c 49 54 31 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  IT1.#endif.../*.
37c8d 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
37c8e 6e 20 6f 66 20 74 68 65 20 53 51 4c 20 73 63 61  n of the SQL sca
37c8f 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  lar function for
37c90 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 75   accessing the u
37c91 6e 64 65 72 6c 79 69 6e 67 20 0a 2a 2a 20 68 61  nderlying .** ha
37c92 73 68 20 74 61 62 6c 65 2e 20 54 68 69 73 20 66  sh table. This f
37c93 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 63  unction may be c
37c94 61 6c 6c 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73  alled as follows
37c95 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54  :.**.**   SELECT
37c96 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e   <function-name>
37c97 28 3c 6b 65 79 2d 6e 61 6d 65 3e 29 3b 0a 2a 2a  (<key-name>);.**
37c98 20 20 20 53 45 4c 45 43 54 20 3c 66 75 6e 63 74     SELECT <funct
37c99 69 6f 6e 2d 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e  ion-name>(<key-n
37c9a 61 6d 65 3e 2c 20 3c 70 6f 69 6e 74 65 72 3e 29  ame>, <pointer>)
37c9b 3b 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 3c 66  ;.**.** where <f
37c9c 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 20 69 73  unction-name> is
37c9d 20 74 68 65 20 6e 61 6d 65 20 70 61 73 73 65 64   the name passed
37c9e 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61   as the second a
37c9f 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68  rgument.** to th
37ca0 65 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69  e sqlite3Fts3Ini
37ca1 74 48 61 73 68 54 61 62 6c 65 28 29 20 66 75 6e  tHashTable() fun
37ca2 63 74 69 6f 6e 20 28 65 2e 67 2e 20 27 66 74 73  ction (e.g. 'fts
37ca3 33 5f 74 6f 6b 65 6e 69 7a 65 72 27 29 2e 0a 2a  3_tokenizer')..*
37ca4 2a 0a 2a 2a 20 49 66 20 74 68 65 20 3c 70 6f 69  *.** If the <poi
37ca5 6e 74 65 72 3e 20 61 72 67 75 6d 65 6e 74 20 69  nter> argument i
37ca6 73 20 73 70 65 63 69 66 69 65 64 2c 20 69 74 20  s specified, it 
37ca7 6d 75 73 74 20 62 65 20 61 20 62 6c 6f 62 20 76  must be a blob v
37ca8 61 6c 75 65 0a 2a 2a 20 63 6f 6e 74 61 69 6e 69  alue.** containi
37ca9 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ng a pointer to 
37caa 62 65 20 73 74 6f 72 65 64 20 61 73 20 74 68 65  be stored as the
37cab 20 68 61 73 68 20 64 61 74 61 20 63 6f 72 72 65   hash data corre
37cac 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 74  sponding.** to t
37cad 68 65 20 73 74 72 69 6e 67 20 3c 6b 65 79 2d 6e  he string <key-n
37cae 61 6d 65 3e 2e 20 49 66 20 3c 70 6f 69 6e 74 65  ame>. If <pointe
37caf 72 3e 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66  r> is not specif
37cb0 69 65 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65  ied, then.** the
37cb1 20 73 74 72 69 6e 67 20 3c 6b 65 79 2d 6e 61 6d   string <key-nam
37cb2 65 3e 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20  e> must already 
37cb3 65 78 69 73 74 20 69 6e 20 74 68 65 20 68 61 73  exist in the has
37cb4 20 74 61 62 6c 65 2e 20 4f 74 68 65 72 77 69 73   table. Otherwis
37cb5 65 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69  e,.** an error i
37cb6 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
37cb7 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  * Whether or not
37cb8 20 74 68 65 20 3c 70 6f 69 6e 74 65 72 3e 20 61   the <pointer> a
37cb9 72 67 75 6d 65 6e 74 20 69 73 20 73 70 65 63 69  rgument is speci
37cba 66 69 65 64 2c 20 74 68 65 20 76 61 6c 75 65 20  fied, the value 
37cbb 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 73 20 61  returned.** is a
37cbc 20 62 6c 6f 62 20 63 6f 6e 74 61 69 6e 69 6e 67   blob containing
37cbd 20 74 68 65 20 70 6f 69 6e 74 65 72 20 73 74 6f   the pointer sto
37cbe 72 65 64 20 61 73 20 74 68 65 20 68 61 73 68 20  red as the hash 
37cbf 64 61 74 61 20 63 6f 72 72 65 73 70 6f 6e 64 69  data correspondi
37cc0 6e 67 0a 2a 2a 20 74 6f 20 73 74 72 69 6e 67 20  ng.** to string 
37cc1 3c 6b 65 79 2d 6e 61 6d 65 3e 20 28 61 66 74 65  <key-name> (afte
37cc2 72 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c 65  r the hash-table
37cc3 20 69 73 20 75 70 64 61 74 65 64 2c 20 69 66 20   is updated, if 
37cc4 61 70 70 6c 69 63 61 62 6c 65 29 2e 0a 2a 2f 0a  applicable)..*/.
37cc5 73 74 61 74 69 63 20 76 6f 69 64 20 73 63 61 6c  static void scal
37cc6 61 72 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  arFunc(.  sqlite
37cc7 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
37cc8 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
37cc9 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
37cca 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 66 74 73 33  **argv.){.  fts3
37ccb 48 61 73 68 20 2a 70 48 61 73 68 3b 0a 20 20 76  Hash *pHash;.  v
37ccc 6f 69 64 20 2a 70 50 74 72 20 3d 20 30 3b 0a 20  oid *pPtr = 0;. 
37ccd 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
37cce 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69  char *zName;.  i
37ccf 6e 74 20 6e 4e 61 6d 65 3b 0a 0a 20 20 61 73 73  nt nName;..  ass
37cd0 65 72 74 28 20 61 72 67 63 3d 3d 31 20 7c 7c 20  ert( argc==1 || 
37cd1 61 72 67 63 3d 3d 32 20 29 3b 0a 0a 20 20 70 48  argc==2 );..  pH
37cd2 61 73 68 20 3d 20 28 66 74 73 33 48 61 73 68 20  ash = (fts3Hash 
37cd3 2a 29 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64  *)sqlite3_user_d
37cd4 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20  ata(context);.. 
37cd5 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   zName = sqlite3
37cd6 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
37cd7 5b 30 5d 29 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20  [0]);.  nName = 
37cd8 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
37cd9 74 65 73 28 61 72 67 76 5b 30 5d 29 2b 31 3b 0a  tes(argv[0])+1;.
37cda 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 32 20 29  .  if( argc==2 )
37cdb 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 4f 6c 64  {.    void *pOld
37cdc 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71  ;.    int n = sq
37cdd 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
37cde 73 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20  s(argv[1]);.    
37cdf 69 66 28 20 6e 21 3d 73 69 7a 65 6f 66 28 70 50  if( n!=sizeof(pP
37ce0 74 72 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  tr) ){.      sql
37ce1 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
37ce2 72 28 63 6f 6e 74 65 78 74 2c 20 22 61 72 67 75  r(context, "argu
37ce3 6d 65 6e 74 20 74 79 70 65 20 6d 69 73 6d 61 74  ment type mismat
37ce4 63 68 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  ch", -1);.      
37ce5 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
37ce6 20 20 70 50 74 72 20 3d 20 2a 28 76 6f 69 64 20    pPtr = *(void 
37ce7 2a 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  **)sqlite3_value
37ce8 5f 62 6c 6f 62 28 61 72 67 76 5b 31 5d 29 3b 0a  _blob(argv[1]);.
37ce9 20 20 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74      pOld = sqlit
37cea 65 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74  e3Fts3HashInsert
37ceb 28 70 48 61 73 68 2c 20 28 76 6f 69 64 20 2a 29  (pHash, (void *)
37cec 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 70 50  zName, nName, pP
37ced 74 72 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c  tr);.    if( pOl
37cee 64 3d 3d 70 50 74 72 20 29 7b 0a 20 20 20 20 20  d==pPtr ){.     
37cef 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
37cf0 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22  error(context, "
37cf1 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 2c 20  out of memory", 
37cf2 2d 31 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  -1);.      retur
37cf3 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  n;.    }.  }else
37cf4 7b 0a 20 20 20 20 70 50 74 72 20 3d 20 73 71 6c  {.    pPtr = sql
37cf5 69 74 65 33 46 74 73 33 48 61 73 68 46 69 6e 64  ite3Fts3HashFind
37cf6 28 70 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e  (pHash, zName, n
37cf7 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 21  Name);.    if( !
37cf8 70 50 74 72 20 29 7b 0a 20 20 20 20 20 20 63 68  pPtr ){.      ch
37cf9 61 72 20 2a 7a 45 72 72 20 3d 20 73 71 6c 69 74  ar *zErr = sqlit
37cfa 65 33 5f 6d 70 72 69 6e 74 66 28 22 75 6e 6b 6e  e3_mprintf("unkn
37cfb 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 25  own tokenizer: %
37cfc 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  s", zName);.    
37cfd 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
37cfe 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
37cff 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20 20 20 20  zErr, -1);.     
37d00 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45   sqlite3_free(zE
37d01 72 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  rr);.      retur
37d02 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  n;.    }.  }..  
37d03 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
37d04 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28 76 6f  lob(context, (vo
37d05 69 64 20 2a 29 26 70 50 74 72 2c 20 73 69 7a 65  id *)&pPtr, size
37d06 6f 66 28 70 50 74 72 29 2c 20 53 51 4c 49 54 45  of(pPtr), SQLITE
37d07 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 0a  _TRANSIENT);.}..
37d08 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
37d09 53 54 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ST.../*.** Imple
37d0a 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 73  mentation of a s
37d0b 70 65 63 69 61 6c 20 53 51 4c 20 73 63 61 6c 61  pecial SQL scala
37d0c 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74  r function for t
37d0d 65 73 74 69 6e 67 20 74 6f 6b 65 6e 69 7a 65 72  esting tokenizer
37d0e 73 20 0a 2a 2a 20 64 65 73 69 67 6e 65 64 20 74  s .** designed t
37d0f 6f 20 62 65 20 75 73 65 64 20 69 6e 20 63 6f 6e  o be used in con
37d10 63 65 72 74 20 77 69 74 68 20 74 68 65 20 54 63  cert with the Tc
37d11 6c 20 74 65 73 74 69 6e 67 20 66 72 61 6d 65 77  l testing framew
37d12 6f 72 6b 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e  ork. This.** fun
37d13 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 63 61  ction must be ca
37d14 6c 6c 65 64 20 77 69 74 68 20 74 77 6f 20 61 72  lled with two ar
37d15 67 75 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20  guments:.**.**  
37d16 20 53 45 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f   SELECT <functio
37d17 6e 2d 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d  n-name>(<key-nam
37d18 65 3e 2c 20 3c 69 6e 70 75 74 2d 73 74 72 69 6e  e>, <input-strin
37d19 67 3e 29 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54  g>);.**   SELECT
37d1a 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e   <function-name>
37d1b 28 3c 6b 65 79 2d 6e 61 6d 65 3e 2c 20 3c 70 6f  (<key-name>, <po
37d1c 69 6e 74 65 72 3e 29 3b 0a 2a 2a 0a 2a 2a 20 77  inter>);.**.** w
37d1d 68 65 72 65 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e  here <function-n
37d1e 61 6d 65 3e 20 69 73 20 74 68 65 20 6e 61 6d 65  ame> is the name
37d1f 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73   passed as the s
37d20 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a  econd argument.*
37d21 2a 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  * to the sqlite3
37d22 46 74 73 33 49 6e 69 74 48 61 73 68 54 61 62 6c  Fts3InitHashTabl
37d23 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 28 65 2e  e() function (e.
37d24 67 2e 20 27 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  g. 'fts3_tokeniz
37d25 65 72 27 29 0a 2a 2a 20 63 6f 6e 63 61 74 65 6e  er').** concaten
37d26 61 74 65 64 20 77 69 74 68 20 74 68 65 20 73 74  ated with the st
37d27 72 69 6e 67 20 27 5f 74 65 73 74 27 20 28 65 2e  ring '_test' (e.
37d28 67 2e 20 27 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  g. 'fts3_tokeniz
37d29 65 72 5f 74 65 73 74 27 29 2e 0a 2a 2a 0a 2a 2a  er_test')..**.**
37d2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
37d2b 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 74 68  e is a string th
37d2c 61 74 20 6d 61 79 20 62 65 20 69 6e 74 65 72 70  at may be interp
37d2d 72 65 74 65 64 20 61 73 20 61 20 54 63 6c 0a 2a  reted as a Tcl.*
37d2e 2a 20 6c 69 73 74 2e 20 46 6f 72 20 65 61 63 68  * list. For each
37d2f 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 3c 69   token in the <i
37d30 6e 70 75 74 2d 73 74 72 69 6e 67 3e 2c 20 74 68  nput-string>, th
37d31 72 65 65 20 65 6c 65 6d 65 6e 74 73 20 61 72 65  ree elements are
37d32 0a 2a 2a 20 61 64 64 65 64 20 74 6f 20 74 68 65  .** added to the
37d33 20 72 65 74 75 72 6e 65 64 20 6c 69 73 74 2e 20   returned list. 
37d34 54 68 65 20 66 69 72 73 74 20 69 73 20 74 68 65  The first is the
37d35 20 74 6f 6b 65 6e 20 70 6f 73 69 74 69 6f 6e 2c   token position,
37d36 20 74 68 65 20 0a 2a 2a 20 73 65 63 6f 6e 64 20   the .** second 
37d37 69 73 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78  is the token tex
37d38 74 20 28 66 6f 6c 64 65 64 2c 20 73 74 65 6d 6d  t (folded, stemm
37d39 65 64 2c 20 65 74 63 2e 29 20 61 6e 64 20 74 68  ed, etc.) and th
37d3a 65 20 74 68 69 72 64 20 69 73 20 74 68 65 0a 2a  e third is the.*
37d3b 2a 20 73 75 62 73 74 72 69 6e 67 20 6f 66 20 3c  * substring of <
37d3c 69 6e 70 75 74 2d 73 74 72 69 6e 67 3e 20 61 73  input-string> as
37d3d 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
37d3e 65 20 74 6f 6b 65 6e 2e 20 46 6f 72 20 65 78 61  e token. For exa
37d3f 6d 70 6c 65 2c 20 0a 2a 2a 20 75 73 69 6e 67 20  mple, .** using 
37d40 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 22 73 69  the built-in "si
37d41 6d 70 6c 65 22 20 74 6f 6b 65 6e 69 7a 65 72 3a  mple" tokenizer:
37d42 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20  .**.**   SELECT 
37d43 66 74 73 5f 74 6f 6b 65 6e 69 7a 65 72 5f 74 65  fts_tokenizer_te
37d44 73 74 28 27 73 69 6d 70 6c 65 27 2c 20 27 49 20  st('simple', 'I 
37d45 64 6f 6e 27 74 20 73 65 65 20 68 6f 77 27 29 3b  don't see how');
37d46 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75  .**.** will retu
37d47 72 6e 20 74 68 65 20 73 74 72 69 6e 67 3a 0a 2a  rn the string:.*
37d48 2a 0a 2a 2a 20 20 20 22 7b 30 20 69 20 49 20 31  *.**   "{0 i I 1
37d49 20 64 6f 6e 74 20 64 6f 6e 27 74 20 32 20 73 65   dont don't 2 se
37d4a 65 20 73 65 65 20 33 20 68 6f 77 20 68 6f 77 7d  e see 3 how how}
37d4b 22 0a 2a 2a 20 20 20 0a 2a 2f 0a 73 74 61 74 69  ".**   .*/.stati
37d4c 63 20 76 6f 69 64 20 74 65 73 74 46 75 6e 63 28  c void testFunc(
37d4d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
37d4e 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
37d4f 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
37d50 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
37d51 29 7b 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70  ){.  fts3Hash *p
37d52 48 61 73 68 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Hash;.  sqlite3_
37d53 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
37d54 20 2a 70 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74   *p;.  sqlite3_t
37d55 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
37d56 69 7a 65 72 20 3d 20 30 3b 0a 20 20 73 71 6c 69  izer = 0;.  sqli
37d57 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
37d58 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 30 3b 0a  rsor *pCsr = 0;.
37d59 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
37d5a 45 72 72 20 3d 20 30 3b 0a 0a 20 20 63 6f 6e 73  Err = 0;..  cons
37d5b 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20  t char *zName;. 
37d5c 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 63 6f   int nName;.  co
37d5d 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74  nst char *zInput
37d5e 3b 0a 20 20 69 6e 74 20 6e 49 6e 70 75 74 3b 0a  ;.  int nInput;.
37d5f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
37d60 41 72 67 20 3d 20 30 3b 0a 0a 20 20 63 6f 6e 73  Arg = 0;..  cons
37d61 74 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 0a  t char *zToken;.
37d62 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20    int nToken;.  
37d63 69 6e 74 20 69 53 74 61 72 74 3b 0a 20 20 69 6e  int iStart;.  in
37d64 74 20 69 45 6e 64 3b 0a 20 20 69 6e 74 20 69 50  t iEnd;.  int iP
37d65 6f 73 3b 0a 0a 20 20 54 63 6c 5f 4f 62 6a 20 2a  os;..  Tcl_Obj *
37d66 70 52 65 74 3b 0a 0a 20 20 61 73 73 65 72 74 28  pRet;..  assert(
37d67 20 61 72 67 63 3d 3d 32 20 7c 7c 20 61 72 67 63   argc==2 || argc
37d68 3d 3d 33 20 29 3b 0a 0a 20 20 6e 4e 61 6d 65 20  ==3 );..  nName 
37d69 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
37d6a 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a  bytes(argv[0]);.
37d6b 20 20 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74    zName = (const
37d6c 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f   char *)sqlite3_
37d6d 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
37d6e 30 5d 29 3b 0a 20 20 6e 49 6e 70 75 74 20 3d 20  0]);.  nInput = 
37d6f 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
37d70 74 65 73 28 61 72 67 76 5b 61 72 67 63 2d 31 5d  tes(argv[argc-1]
37d71 29 3b 0a 20 20 7a 49 6e 70 75 74 20 3d 20 28 63  );.  zInput = (c
37d72 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69  onst char *)sqli
37d73 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
37d74 72 67 76 5b 61 72 67 63 2d 31 5d 29 3b 0a 0a 20  rgv[argc-1]);.. 
37d75 20 69 66 28 20 61 72 67 63 3d 3d 33 20 29 7b 0a   if( argc==3 ){.
37d76 20 20 20 20 7a 41 72 67 20 3d 20 28 63 6f 6e 73      zArg = (cons
37d77 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
37d78 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
37d79 5b 31 5d 29 3b 0a 20 20 7d 0a 0a 20 20 70 48 61  [1]);.  }..  pHa
37d7a 73 68 20 3d 20 28 66 74 73 33 48 61 73 68 20 2a  sh = (fts3Hash *
37d7b 29 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61  )sqlite3_user_da
37d7c 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 70  ta(context);.  p
37d7d 20 3d 20 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65   = (sqlite3_toke
37d7e 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 29 73  nizer_module *)s
37d7f 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 46 69  qlite3Fts3HashFi
37d80 6e 64 28 70 48 61 73 68 2c 20 7a 4e 61 6d 65 2c  nd(pHash, zName,
37d81 20 6e 4e 61 6d 65 2b 31 29 3b 0a 0a 20 20 69 66   nName+1);..  if
37d82 28 20 21 70 20 29 7b 0a 20 20 20 20 63 68 61 72  ( !p ){.    char
37d83 20 2a 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33   *zErr = sqlite3
37d84 5f 6d 70 72 69 6e 74 66 28 22 75 6e 6b 6e 6f 77  _mprintf("unknow
37d85 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 25 73 22  n tokenizer: %s"
37d86 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71  , zName);.    sq
37d87 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
37d88 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72  or(context, zErr
37d89 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  , -1);.    sqlit
37d8a 65 33 5f 66 72 65 65 28 7a 45 72 72 29 3b 0a 20  e3_free(zErr);. 
37d8b 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
37d8c 20 20 70 52 65 74 20 3d 20 54 63 6c 5f 4e 65 77    pRet = Tcl_New
37d8d 4f 62 6a 28 29 3b 0a 20 20 54 63 6c 5f 49 6e 63  Obj();.  Tcl_Inc
37d8e 72 52 65 66 43 6f 75 6e 74 28 70 52 65 74 29 3b  rRefCount(pRet);
37d8f 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ..  if( SQLITE_O
37d90 4b 21 3d 70 2d 3e 78 43 72 65 61 74 65 28 7a 41  K!=p->xCreate(zA
37d91 72 67 20 3f 20 31 20 3a 20 30 2c 20 26 7a 41 72  rg ? 1 : 0, &zAr
37d92 67 2c 20 26 70 54 6f 6b 65 6e 69 7a 65 72 29 20  g, &pTokenizer) 
37d93 29 7b 0a 20 20 20 20 7a 45 72 72 20 3d 20 22 65  ){.    zErr = "e
37d94 72 72 6f 72 20 69 6e 20 78 43 72 65 61 74 65 28  rror in xCreate(
37d95 29 22 3b 0a 20 20 20 20 67 6f 74 6f 20 66 69 6e  )";.    goto fin
37d96 69 73 68 3b 0a 20 20 7d 0a 20 20 70 54 6f 6b 65  ish;.  }.  pToke
37d97 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d  nizer->pModule =
37d98 20 70 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45   p;.  if( SQLITE
37d99 5f 4f 4b 21 3d 70 2d 3e 78 4f 70 65 6e 28 70 54  _OK!=p->xOpen(pT
37d9a 6f 6b 65 6e 69 7a 65 72 2c 20 7a 49 6e 70 75 74  okenizer, zInput
37d9b 2c 20 6e 49 6e 70 75 74 2c 20 26 70 43 73 72 29  , nInput, &pCsr)
37d9c 20 29 7b 0a 20 20 20 20 7a 45 72 72 20 3d 20 22   ){.    zErr = "
37d9d 65 72 72 6f 72 20 69 6e 20 78 4f 70 65 6e 28 29  error in xOpen()
37d9e 22 3b 0a 20 20 20 20 67 6f 74 6f 20 66 69 6e 69  ";.    goto fini
37d9f 73 68 3b 0a 20 20 7d 0a 20 20 70 43 73 72 2d 3e  sh;.  }.  pCsr->
37da0 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f  pTokenizer = pTo
37da1 6b 65 6e 69 7a 65 72 3b 0a 0a 20 20 77 68 69 6c  kenizer;..  whil
37da2 65 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 70 2d  e( SQLITE_OK==p-
37da3 3e 78 4e 65 78 74 28 70 43 73 72 2c 20 26 7a 54  >xNext(pCsr, &zT
37da4 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65 6e 2c 20 26  oken, &nToken, &
37da5 69 53 74 61 72 74 2c 20 26 69 45 6e 64 2c 20 26  iStart, &iEnd, &
37da6 69 50 6f 73 29 20 29 7b 0a 20 20 20 20 54 63 6c  iPos) ){.    Tcl
37da7 5f 4c 69 73 74 4f 62 6a 41 70 70 65 6e 64 45 6c  _ListObjAppendEl
37da8 65 6d 65 6e 74 28 30 2c 20 70 52 65 74 2c 20 54  ement(0, pRet, T
37da9 63 6c 5f 4e 65 77 49 6e 74 4f 62 6a 28 69 50 6f  cl_NewIntObj(iPo
37daa 73 29 29 3b 0a 20 20 20 20 54 63 6c 5f 4c 69 73  s));.    Tcl_Lis
37dab 74 4f 62 6a 41 70 70 65 6e 64 45 6c 65 6d 65 6e  tObjAppendElemen
37dac 74 28 30 2c 20 70 52 65 74 2c 20 54 63 6c 5f 4e  t(0, pRet, Tcl_N
37dad 65 77 53 74 72 69 6e 67 4f 62 6a 28 7a 54 6f 6b  ewStringObj(zTok
37dae 65 6e 2c 20 6e 54 6f 6b 65 6e 29 29 3b 0a 20 20  en, nToken));.  
37daf 20 20 7a 54 6f 6b 65 6e 20 3d 20 26 7a 49 6e 70    zToken = &zInp
37db0 75 74 5b 69 53 74 61 72 74 5d 3b 0a 20 20 20 20  ut[iStart];.    
37db1 6e 54 6f 6b 65 6e 20 3d 20 69 45 6e 64 2d 69 53  nToken = iEnd-iS
37db2 74 61 72 74 3b 0a 20 20 20 20 54 63 6c 5f 4c 69  tart;.    Tcl_Li
37db3 73 74 4f 62 6a 41 70 70 65 6e 64 45 6c 65 6d 65  stObjAppendEleme
37db4 6e 74 28 30 2c 20 70 52 65 74 2c 20 54 63 6c 5f  nt(0, pRet, Tcl_
37db5 4e 65 77 53 74 72 69 6e 67 4f 62 6a 28 7a 54 6f  NewStringObj(zTo
37db6 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 29 3b 0a 20  ken, nToken));. 
37db7 20 7d 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45   }..  if( SQLITE
37db8 5f 4f 4b 21 3d 70 2d 3e 78 43 6c 6f 73 65 28 70  _OK!=p->xClose(p
37db9 43 73 72 29 20 29 7b 0a 20 20 20 20 7a 45 72 72  Csr) ){.    zErr
37dba 20 3d 20 22 65 72 72 6f 72 20 69 6e 20 78 43 6c   = "error in xCl
37dbb 6f 73 65 28 29 22 3b 0a 20 20 20 20 67 6f 74 6f  ose()";.    goto
37dbc 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 20 20 69   finish;.  }.  i
37dbd 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d  f( SQLITE_OK!=p-
37dbe 3e 78 44 65 73 74 72 6f 79 28 70 54 6f 6b 65 6e  >xDestroy(pToken
37dbf 69 7a 65 72 29 20 29 7b 0a 20 20 20 20 7a 45 72  izer) ){.    zEr
37dc0 72 20 3d 20 22 65 72 72 6f 72 20 69 6e 20 78 44  r = "error in xD
37dc1 65 73 74 72 6f 79 28 29 22 3b 0a 20 20 20 20 67  estroy()";.    g
37dc2 6f 74 6f 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a  oto finish;.  }.
37dc3 0a 66 69 6e 69 73 68 3a 0a 20 20 69 66 28 20 7a  .finish:.  if( z
37dc4 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Err ){.    sqlit
37dc5 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
37dc6 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d  context, zErr, -
37dc7 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  1);.  }else{.   
37dc8 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
37dc9 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 54 63  text(context, Tc
37dca 6c 5f 47 65 74 53 74 72 69 6e 67 28 70 52 65 74  l_GetString(pRet
37dcb 29 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52  ), -1, SQLITE_TR
37dcc 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 20 20  ANSIENT);.  }.  
37dcd 54 63 6c 5f 44 65 63 72 52 65 66 43 6f 75 6e 74  Tcl_DecrRefCount
37dce 28 70 52 65 74 29 3b 0a 7d 0a 0a 73 74 61 74 69  (pRet);.}..stati
37dcf 63 0a 69 6e 74 20 72 65 67 69 73 74 65 72 54 6f  c.int registerTo
37dd0 6b 65 6e 69 7a 65 72 28 0a 20 20 73 71 6c 69 74  kenizer(.  sqlit
37dd1 65 33 20 2a 64 62 2c 20 0a 20 20 63 68 61 72 20  e3 *db, .  char 
37dd2 2a 7a 4e 61 6d 65 2c 20 0a 20 20 63 6f 6e 73 74  *zName, .  const
37dd3 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
37dd4 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 0a 29 7b 0a  er_module *p.){.
37dd5 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69    int rc;.  sqli
37dd6 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b  te3_stmt *pStmt;
37dd7 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 53  .  const char zS
37dd8 71 6c 5b 5d 20 3d 20 22 53 45 4c 45 43 54 20 66  ql[] = "SELECT f
37dd9 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 28 3f 2c  ts3_tokenizer(?,
37dda 20 3f 29 22 3b 0a 0a 20 20 72 63 20 3d 20 73 71   ?)";..  rc = sq
37ddb 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
37ddc 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26  (db, zSql, -1, &
37ddd 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28  pStmt, 0);.  if(
37dde 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
37ddf 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
37de0 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f  .  }..  sqlite3_
37de1 62 69 6e 64 5f 74 65 78 74 28 70 53 74 6d 74 2c  bind_text(pStmt,
37de2 20 31 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53   1, zName, -1, S
37de3 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
37de4 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c   sqlite3_bind_bl
37de5 6f 62 28 70 53 74 6d 74 2c 20 32 2c 20 26 70 2c  ob(pStmt, 2, &p,
37de6 20 73 69 7a 65 6f 66 28 70 29 2c 20 53 51 4c 49   sizeof(p), SQLI
37de7 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 73 71  TE_STATIC);.  sq
37de8 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74  lite3_step(pStmt
37de9 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  );..  return sql
37dea 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53  ite3_finalize(pS
37deb 74 6d 74 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 0a  tmt);.}..static.
37dec 69 6e 74 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a  int queryTokeniz
37ded 65 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  er(.  sqlite3 *d
37dee 62 2c 20 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d  b, .  char *zNam
37def 65 2c 20 20 0a 20 20 63 6f 6e 73 74 20 73 71 6c  e,  .  const sql
37df0 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
37df1 6f 64 75 6c 65 20 2a 2a 70 70 0a 29 7b 0a 20 20  odule **pp.){.  
37df2 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
37df3 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20  3_stmt *pStmt;. 
37df4 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 53 71 6c   const char zSql
37df5 5b 5d 20 3d 20 22 53 45 4c 45 43 54 20 66 74 73  [] = "SELECT fts
37df6 33 5f 74 6f 6b 65 6e 69 7a 65 72 28 3f 29 22 3b  3_tokenizer(?)";
37df7 0a 0a 20 20 2a 70 70 20 3d 20 30 3b 0a 20 20 72  ..  *pp = 0;.  r
37df8 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70  c = sqlite3_prep
37df9 61 72 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c  are_v2(db, zSql,
37dfa 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b   -1, &pStmt, 0);
37dfb 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
37dfc 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
37dfd 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 73 71  rn rc;.  }..  sq
37dfe 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28  lite3_bind_text(
37dff 70 53 74 6d 74 2c 20 31 2c 20 7a 4e 61 6d 65 2c  pStmt, 1, zName,
37e00 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54   -1, SQLITE_STAT
37e01 49 43 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54  IC);.  if( SQLIT
37e02 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73  E_ROW==sqlite3_s
37e03 74 65 70 28 70 53 74 6d 74 29 20 29 7b 0a 20 20  tep(pStmt) ){.  
37e04 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 6f    if( sqlite3_co
37e05 6c 75 6d 6e 5f 74 79 70 65 28 70 53 74 6d 74 2c  lumn_type(pStmt,
37e06 20 30 29 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42   0)==SQLITE_BLOB
37e07 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   ){.      memcpy
37e08 28 70 70 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c  (pp, sqlite3_col
37e09 75 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20  umn_blob(pStmt, 
37e0a 30 29 2c 20 73 69 7a 65 6f 66 28 2a 70 70 29 29  0), sizeof(*pp))
37e0b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  ;.    }.  }..  r
37e0c 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 66 69  eturn sqlite3_fi
37e0d 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d  nalize(pStmt);.}
37e0e 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
37e0f 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73   void sqlite3Fts
37e10 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72  3SimpleTokenizer
37e11 4d 6f 64 75 6c 65 28 73 71 6c 69 74 65 33 5f 74  Module(sqlite3_t
37e12 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
37e13 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 29  const**ppModule)
37e14 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  ;../*.** Impleme
37e15 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ntation of the s
37e16 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 66  calar function f
37e17 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 69 6e  ts3_tokenizer_in
37e18 74 65 72 6e 61 6c 5f 74 65 73 74 28 29 2e 0a 2a  ternal_test()..*
37e19 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
37e1a 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74  is used for test
37e1b 69 6e 67 20 6f 6e 6c 79 2c 20 69 74 20 69 73 20  ing only, it is 
37e1c 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 6e 20  not included in 
37e1d 74 68 65 0a 2a 2a 20 62 75 69 6c 64 20 75 6e 6c  the.** build unl
37e1e 65 73 73 20 53 51 4c 49 54 45 5f 54 45 53 54 20  ess SQLITE_TEST 
37e1f 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a  is defined..**.*
37e20 2a 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66  * The purpose of
37e21 20 74 68 69 73 20 69 73 20 74 6f 20 74 65 73 74   this is to test
37e22 20 74 68 61 74 20 74 68 65 20 66 74 73 33 5f 74   that the fts3_t
37e23 6f 6b 65 6e 69 7a 65 72 28 29 20 66 75 6e 63 74  okenizer() funct
37e24 69 6f 6e 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73  ion.** can be us
37e25 65 64 20 61 73 20 64 65 73 69 67 6e 65 64 20 62  ed as designed b
37e26 79 20 74 68 65 20 43 2d 63 6f 64 65 20 69 6e 20  y the C-code in 
37e27 74 68 65 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a  the queryTokeniz
37e28 65 72 20 61 6e 64 0a 2a 2a 20 72 65 67 69 73 74  er and.** regist
37e29 65 72 54 6f 6b 65 6e 69 7a 65 72 28 29 20 66 75  erTokenizer() fu
37e2a 6e 63 74 69 6f 6e 73 20 61 62 6f 76 65 2e 20 54  nctions above. T
37e2b 68 65 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f  hese two functio
37e2c 6e 73 20 61 72 65 20 72 65 70 65 61 74 65 64 0a  ns are repeated.
37e2d 2a 2a 20 69 6e 20 74 68 65 20 52 45 41 44 4d 45  ** in the README
37e2e 2e 74 6f 6b 65 6e 69 7a 65 72 20 66 69 6c 65 20  .tokenizer file 
37e2f 61 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c 20 73  as an example, s
37e30 6f 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e  o it is importan
37e31 74 20 74 6f 0a 2a 2a 20 74 65 73 74 20 74 68 65  t to.** test the
37e32 6d 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 75 6e 20  m..**.** To run 
37e33 74 68 65 20 74 65 73 74 73 2c 20 65 76 61 6c 75  the tests, evalu
37e34 61 74 65 20 74 68 65 20 66 74 73 33 5f 74 6f 6b  ate the fts3_tok
37e35 65 6e 69 7a 65 72 5f 69 6e 74 65 72 6e 61 6c 5f  enizer_internal_
37e36 74 65 73 74 28 29 20 73 63 61 6c 61 72 0a 2a 2a  test() scalar.**
37e37 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 6e   function with n
37e38 6f 20 61 72 67 75 6d 65 6e 74 73 2e 20 41 6e 20  o arguments. An 
37e39 61 73 73 65 72 74 28 29 20 77 69 6c 6c 20 66 61  assert() will fa
37e3a 69 6c 20 69 66 20 61 20 70 72 6f 62 6c 65 6d 20  il if a problem 
37e3b 69 73 0a 2a 2a 20 64 65 74 65 63 74 65 64 2e 20  is.** detected. 
37e3c 69 2e 65 2e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  i.e.:.**.**     
37e3d 53 45 4c 45 43 54 20 66 74 73 33 5f 74 6f 6b 65  SELECT fts3_toke
37e3e 6e 69 7a 65 72 5f 69 6e 74 65 72 6e 61 6c 5f 74  nizer_internal_t
37e3f 65 73 74 28 29 3b 0a 2a 2a 0a 2a 2f 0a 73 74 61  est();.**.*/.sta
37e40 74 69 63 20 76 6f 69 64 20 69 6e 74 54 65 73 74  tic void intTest
37e41 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
37e42 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
37e43 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
37e44 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
37e45 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 72 63  argv.){.  int rc
37e46 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ;.  const sqlite
37e47 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
37e48 6c 65 20 2a 70 31 3b 0a 20 20 63 6f 6e 73 74 20  le *p1;.  const 
37e49 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
37e4a 72 5f 6d 6f 64 75 6c 65 20 2a 70 32 3b 0a 20 20  r_module *p2;.  
37e4b 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 28 73  sqlite3 *db = (s
37e4c 71 6c 69 74 65 33 20 2a 29 73 71 6c 69 74 65 33  qlite3 *)sqlite3
37e4d 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65  _user_data(conte
37e4e 78 74 29 3b 0a 0a 20 20 2f 2a 20 54 65 73 74 20  xt);..  /* Test 
37e4f 74 68 65 20 71 75 65 72 79 20 66 75 6e 63 74 69  the query functi
37e50 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 46  on */.  sqlite3F
37e51 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a  ts3SimpleTokeniz
37e52 65 72 4d 6f 64 75 6c 65 28 26 70 31 29 3b 0a 20  erModule(&p1);. 
37e53 20 72 63 20 3d 20 71 75 65 72 79 54 6f 6b 65 6e   rc = queryToken
37e54 69 7a 65 72 28 64 62 2c 20 22 73 69 6d 70 6c 65  izer(db, "simple
37e55 22 2c 20 26 70 32 29 3b 0a 20 20 61 73 73 65 72  ", &p2);.  asser
37e56 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
37e57 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 31   );.  assert( p1
37e58 3d 3d 70 32 20 29 3b 0a 20 20 72 63 20 3d 20 71  ==p2 );.  rc = q
37e59 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72 28 64 62  ueryTokenizer(db
37e5a 2c 20 22 6e 6f 73 75 63 68 74 6f 6b 65 6e 69 7a  , "nosuchtokeniz
37e5b 65 72 22 2c 20 26 70 32 29 3b 0a 20 20 61 73 73  er", &p2);.  ass
37e5c 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
37e5d 45 52 52 4f 52 20 29 3b 0a 20 20 61 73 73 65 72  ERROR );.  asser
37e5e 74 28 20 70 32 3d 3d 30 20 29 3b 0a 20 20 61 73  t( p2==0 );.  as
37e5f 73 65 72 74 28 20 30 3d 3d 73 74 72 63 6d 70 28  sert( 0==strcmp(
37e60 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64  sqlite3_errmsg(d
37e61 62 29 2c 20 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b  b), "unknown tok
37e62 65 6e 69 7a 65 72 3a 20 6e 6f 73 75 63 68 74 6f  enizer: nosuchto
37e63 6b 65 6e 69 7a 65 72 22 29 20 29 3b 0a 0a 20 20  kenizer") );..  
37e64 2f 2a 20 54 65 73 74 20 74 68 65 20 73 74 6f 72  /* Test the stor
37e65 61 67 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  age function */.
37e66 20 20 72 63 20 3d 20 72 65 67 69 73 74 65 72 54    rc = registerT
37e67 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 20 22 6e 6f  okenizer(db, "no
37e68 73 75 63 68 74 6f 6b 65 6e 69 7a 65 72 22 2c 20  suchtokenizer", 
37e69 70 31 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72  p1);.  assert( r
37e6a 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  c==SQLITE_OK );.
37e6b 20 20 72 63 20 3d 20 71 75 65 72 79 54 6f 6b 65    rc = queryToke
37e6c 6e 69 7a 65 72 28 64 62 2c 20 22 6e 6f 73 75 63  nizer(db, "nosuc
37e6d 68 74 6f 6b 65 6e 69 7a 65 72 22 2c 20 26 70 32  htokenizer", &p2
37e6e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d  );.  assert( rc=
37e6f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
37e70 61 73 73 65 72 74 28 20 70 32 3d 3d 70 31 20 29  assert( p2==p1 )
37e71 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  ;..  sqlite3_res
37e72 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
37e73 2c 20 22 6f 6b 22 2c 20 2d 31 2c 20 53 51 4c 49  , "ok", -1, SQLI
37e74 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 23  TE_STATIC);.}..#
37e75 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  endif../*.** Set
37e76 20 75 70 20 53 51 4c 20 6f 62 6a 65 63 74 73 20   up SQL objects 
37e77 69 6e 20 64 61 74 61 62 61 73 65 20 64 62 20 75  in database db u
37e78 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68  sed to access th
37e79 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 2a 2a  e contents of.**
37e7a 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
37e7b 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 61 72  pointed to by ar
37e7c 67 75 6d 65 6e 74 20 70 48 61 73 68 2e 20 54 68  gument pHash. Th
37e7d 65 20 68 61 73 68 20 74 61 62 6c 65 20 6d 75 73  e hash table mus
37e7e 74 0a 2a 2a 20 62 65 65 6e 20 69 6e 69 74 69 61  t.** been initia
37e7f 6c 69 73 65 64 20 74 6f 20 75 73 65 20 73 74 72  lised to use str
37e80 69 6e 67 20 6b 65 79 73 2c 20 61 6e 64 20 74 6f  ing keys, and to
37e81 20 74 61 6b 65 20 61 20 70 72 69 76 61 74 65 20   take a private 
37e82 63 6f 70 79 20 0a 2a 2a 20 6f 66 20 74 68 65 20  copy .** of the 
37e83 6b 65 79 20 77 68 65 6e 20 61 20 76 61 6c 75 65  key when a value
37e84 20 69 73 20 69 6e 73 65 72 74 65 64 2e 20 69 2e   is inserted. i.
37e85 65 2e 20 62 79 20 61 20 63 61 6c 6c 20 73 69 6d  e. by a call sim
37e86 69 6c 61 72 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20  ilar to:.**.**  
37e87 20 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73    sqlite3Fts3Has
37e88 68 49 6e 69 74 28 70 48 61 73 68 2c 20 46 54 53  hInit(pHash, FTS
37e89 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 2c 20 31  3_HASH_STRING, 1
37e8a 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  );.**.** This fu
37e8b 6e 63 74 69 6f 6e 20 61 64 64 73 20 61 20 73 63  nction adds a sc
37e8c 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 28 73  alar function (s
37e8d 65 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e  ee header commen
37e8e 74 20 61 62 6f 76 65 0a 2a 2a 20 73 63 61 6c 61  t above.** scala
37e8f 72 46 75 6e 63 28 29 20 69 6e 20 74 68 69 73 20  rFunc() in this 
37e90 66 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73  file for details
37e91 29 20 61 6e 64 2c 20 69 66 20 45 4e 41 42 4c 45  ) and, if ENABLE
37e92 5f 54 41 42 4c 45 20 69 73 0a 2a 2a 20 64 65 66  _TABLE is.** def
37e93 69 6e 65 64 20 61 74 20 63 6f 6d 70 69 6c 61 74  ined at compilat
37e94 69 6f 6e 20 74 69 6d 65 2c 20 61 20 74 65 6d 70  ion time, a temp
37e95 6f 72 61 72 79 20 76 69 72 74 75 61 6c 20 74 61  orary virtual ta
37e96 62 6c 65 20 28 73 65 65 20 68 65 61 64 65 72 20  ble (see header 
37e97 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76  .** comment abov
37e98 65 20 73 74 72 75 63 74 20 48 61 73 68 54 61 62  e struct HashTab
37e99 6c 65 56 74 61 62 29 20 74 6f 20 74 68 65 20 64  leVtab) to the d
37e9a 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20  atabase schema. 
37e9b 42 6f 74 68 20 0a 2a 2a 20 70 72 6f 76 69 64 65  Both .** provide
37e9c 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65   read/write acce
37e9d 73 73 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e  ss to the conten
37e9e 74 73 20 6f 66 20 2a 70 48 61 73 68 2e 0a 2a 2a  ts of *pHash..**
37e9f 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61 72  .** The third ar
37ea0 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66  gument to this f
37ea1 75 6e 63 74 69 6f 6e 2c 20 7a 4e 61 6d 65 2c 20  unction, zName, 
37ea2 69 73 20 75 73 65 64 20 61 73 20 74 68 65 20 6e  is used as the n
37ea3 61 6d 65 0a 2a 2a 20 6f 66 20 62 6f 74 68 20 74  ame.** of both t
37ea4 68 65 20 73 63 61 6c 61 72 20 61 6e 64 2c 20 69  he scalar and, i
37ea5 66 20 63 72 65 61 74 65 64 2c 20 74 68 65 20 76  f created, the v
37ea6 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f  irtual table..*/
37ea7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
37ea8 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 49  int sqlite3Fts3I
37ea9 6e 69 74 48 61 73 68 54 61 62 6c 65 28 0a 20 20  nitHashTable(.  
37eaa 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20  sqlite3 *db, .  
37eab 66 74 73 33 48 61 73 68 20 2a 70 48 61 73 68 2c  fts3Hash *pHash,
37eac 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   .  const char *
37ead 7a 4e 61 6d 65 0a 29 7b 0a 20 20 69 6e 74 20 72  zName.){.  int r
37eae 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
37eaf 20 76 6f 69 64 20 2a 70 20 3d 20 28 76 6f 69 64   void *p = (void
37eb0 20 2a 29 70 48 61 73 68 3b 0a 20 20 63 6f 6e 73   *)pHash;.  cons
37eb1 74 20 69 6e 74 20 61 6e 79 20 3d 20 53 51 4c 49  t int any = SQLI
37eb2 54 45 5f 41 4e 59 3b 0a 20 20 63 68 61 72 20 2a  TE_ANY;.  char *
37eb3 7a 54 65 73 74 20 3d 20 30 3b 0a 20 20 63 68 61  zTest = 0;.  cha
37eb4 72 20 2a 7a 54 65 73 74 32 20 3d 20 30 3b 0a 0a  r *zTest2 = 0;..
37eb5 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
37eb6 53 54 0a 20 20 76 6f 69 64 20 2a 70 64 62 20 3d  ST.  void *pdb =
37eb7 20 28 76 6f 69 64 20 2a 29 64 62 3b 0a 20 20 7a   (void *)db;.  z
37eb8 54 65 73 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Test = sqlite3_m
37eb9 70 72 69 6e 74 66 28 22 25 73 5f 74 65 73 74 22  printf("%s_test"
37eba 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 7a 54 65 73  , zName);.  zTes
37ebb 74 32 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  t2 = sqlite3_mpr
37ebc 69 6e 74 66 28 22 25 73 5f 69 6e 74 65 72 6e 61  intf("%s_interna
37ebd 6c 5f 74 65 73 74 22 2c 20 7a 4e 61 6d 65 29 3b  l_test", zName);
37ebe 0a 20 20 69 66 28 20 21 7a 54 65 73 74 20 7c 7c  .  if( !zTest ||
37ebf 20 21 7a 54 65 73 74 32 20 29 7b 0a 20 20 20 20   !zTest2 ){.    
37ec0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
37ec1 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  M;.  }.#endif.. 
37ec2 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
37ec3 4f 4b 0a 20 20 20 7c 7c 20 28 72 63 20 3d 20 73  OK.   || (rc = s
37ec4 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
37ec5 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65  nction(db, zName
37ec6 2c 20 31 2c 20 61 6e 79 2c 20 70 2c 20 73 63 61  , 1, any, p, sca
37ec7 6c 61 72 46 75 6e 63 2c 20 30 2c 20 30 29 29 0a  larFunc, 0, 0)).
37ec8 20 20 20 7c 7c 20 28 72 63 20 3d 20 73 71 6c 69     || (rc = sqli
37ec9 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
37eca 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 32  ion(db, zName, 2
37ecb 2c 20 61 6e 79 2c 20 70 2c 20 73 63 61 6c 61 72  , any, p, scalar
37ecc 46 75 6e 63 2c 20 30 2c 20 30 29 29 0a 23 69 66  Func, 0, 0)).#if
37ecd 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
37ece 20 20 20 7c 7c 20 28 72 63 20 3d 20 73 71 6c 69     || (rc = sqli
37ecf 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
37ed0 69 6f 6e 28 64 62 2c 20 7a 54 65 73 74 2c 20 32  ion(db, zTest, 2
37ed1 2c 20 61 6e 79 2c 20 70 2c 20 74 65 73 74 46 75  , any, p, testFu
37ed2 6e 63 2c 20 30 2c 20 30 29 29 0a 20 20 20 7c 7c  nc, 0, 0)).   ||
37ed3 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63   (rc = sqlite3_c
37ed4 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64  reate_function(d
37ed5 62 2c 20 7a 54 65 73 74 2c 20 33 2c 20 61 6e 79  b, zTest, 3, any
37ed6 2c 20 70 2c 20 74 65 73 74 46 75 6e 63 2c 20 30  , p, testFunc, 0
37ed7 2c 20 30 29 29 0a 20 20 20 7c 7c 20 28 72 63 20  , 0)).   || (rc 
37ed8 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  = sqlite3_create
37ed9 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 54  _function(db, zT
37eda 65 73 74 32 2c 20 30 2c 20 61 6e 79 2c 20 70 64  est2, 0, any, pd
37edb 62 2c 20 69 6e 74 54 65 73 74 46 75 6e 63 2c 20  b, intTestFunc, 
37edc 30 2c 20 30 29 29 0a 23 65 6e 64 69 66 0a 20 20  0, 0)).#endif.  
37edd 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  );..  sqlite3_fr
37ede 65 65 28 7a 54 65 73 74 29 3b 0a 20 20 73 71 6c  ee(zTest);.  sql
37edf 69 74 65 33 5f 66 72 65 65 28 7a 54 65 73 74 32  ite3_free(zTest2
37ee0 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
37ee1 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  }..#endif /* !de
37ee2 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
37ee3 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
37ee4 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
37ee5 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
37ee6 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73  ***** End of fts
37ee7 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e 63 20 2a 2a  3_tokenizer.c **
37ee8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37ee9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37eea 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
37eeb 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
37eec 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 31   fts3_tokenizer1
37eed 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
37eee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37eef 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36  ****/./*.** 2006
37ef0 20 4f 63 74 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68   Oct 10.**.** Th
37ef1 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
37ef2 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
37ef3 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
37ef4 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
37ef5 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
37ef6 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
37ef7 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
37ef8 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
37ef9 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
37efa 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
37efb 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
37efc 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
37efd 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
37efe 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
37eff 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
37f00 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
37f01 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
37f02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37f03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37f04 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37f05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37f06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
37f07 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
37f08 20 6f 66 20 74 68 65 20 22 73 69 6d 70 6c 65 22   of the "simple"
37f09 20 66 75 6c 6c 2d 74 65 78 74 2d 73 65 61 72 63   full-text-searc
37f0a 68 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2f 0a  h tokenizer..*/.
37f0b 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  ./*.** The code 
37f0c 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20  in this file is 
37f0d 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66  only compiled if
37f0e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68  :.**.**     * Th
37f0f 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73  e FTS3 module is
37f10 20 62 65 69 6e 67 20 62 75 69 6c 74 20 61 73 20   being built as 
37f11 61 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20  an extension.** 
37f12 20 20 20 20 20 20 28 69 6e 20 77 68 69 63 68 20        (in which 
37f13 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 52 45  case SQLITE_CORE
37f14 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29   is not defined)
37f15 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a  , or.**.**     *
37f16 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65   The FTS3 module
37f17 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20   is being built 
37f18 69 6e 74 6f 20 74 68 65 20 63 6f 72 65 20 6f 66  into the core of
37f19 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 69 74 65  .**       SQLite
37f1a 20 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20   (in which case 
37f1b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
37f1c 53 33 20 69 73 20 64 65 66 69 6e 65 64 29 2e 0a  S3 is defined)..
37f1d 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  */.#if !defined(
37f1e 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
37f1f 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
37f20 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a 0a 0a 0a  NABLE_FTS3).....
37f21 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
37f22 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20  imple_tokenizer 
37f23 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  {.  sqlite3_toke
37f24 6e 69 7a 65 72 20 62 61 73 65 3b 0a 20 20 63 68  nizer base;.  ch
37f25 61 72 20 64 65 6c 69 6d 5b 31 32 38 5d 3b 20 20  ar delim[128];  
37f26 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c             /* fl
37f27 61 67 20 41 53 43 49 49 20 64 65 6c 69 6d 69 74  ag ASCII delimit
37f28 65 72 73 20 2a 2f 0a 7d 20 73 69 6d 70 6c 65 5f  ers */.} simple_
37f29 74 6f 6b 65 6e 69 7a 65 72 3b 0a 0a 74 79 70 65  tokenizer;..type
37f2a 64 65 66 20 73 74 72 75 63 74 20 73 69 6d 70 6c  def struct simpl
37f2b 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  e_tokenizer_curs
37f2c 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74  or {.  sqlite3_t
37f2d 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
37f2e 62 61 73 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68  base;.  const ch
37f2f 61 72 20 2a 70 49 6e 70 75 74 3b 20 20 20 20 20  ar *pInput;     
37f30 20 20 20 20 20 2f 2a 20 69 6e 70 75 74 20 77 65       /* input we
37f31 20 61 72 65 20 74 6f 6b 65 6e 69 7a 69 6e 67 20   are tokenizing 
37f32 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 3b  */.  int nBytes;
37f33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37f34 20 20 2f 2a 20 73 69 7a 65 20 6f 66 20 74 68 65    /* size of the
37f35 20 69 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20   input */.  int 
37f36 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20  iOffset;        
37f37 20 20 20 20 20 20 20 20 20 2f 2a 20 63 75 72 72           /* curr
37f38 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20  ent position in 
37f39 70 49 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20  pInput */.  int 
37f3a 69 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20  iToken;         
37f3b 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 64 65           /* inde
37f3c 78 20 6f 66 20 6e 65 78 74 20 74 6f 6b 65 6e 20  x of next token 
37f3d 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 2a  to be returned *
37f3e 2f 0a 20 20 63 68 61 72 20 2a 70 54 6f 6b 65 6e  /.  char *pToken
37f3f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
37f40 20 2f 2a 20 73 74 6f 72 61 67 65 20 66 6f 72 20   /* storage for 
37f41 63 75 72 72 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f  current token */
37f42 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 41 6c 6c  .  int nTokenAll
37f43 6f 63 61 74 65 64 3b 20 20 20 20 20 20 20 20 20  ocated;         
37f44 2f 2a 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74  /* space allocat
37f45 65 64 20 74 6f 20 7a 54 6f 6b 65 6e 20 62 75 66  ed to zToken buf
37f46 66 65 72 20 2a 2f 0a 7d 20 73 69 6d 70 6c 65 5f  fer */.} simple_
37f47 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
37f48 3b 0a 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64  ;.../* Forward d
37f49 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 73 74  eclaration */.st
37f4a 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
37f4b 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
37f4c 75 6c 65 20 73 69 6d 70 6c 65 54 6f 6b 65 6e 69  ule simpleTokeni
37f4d 7a 65 72 4d 6f 64 75 6c 65 3b 0a 0a 73 74 61 74  zerModule;..stat
37f4e 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 44 65 6c  ic int simpleDel
37f4f 69 6d 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69  im(simple_tokeni
37f50 7a 65 72 20 2a 74 2c 20 75 6e 73 69 67 6e 65 64  zer *t, unsigned
37f51 20 63 68 61 72 20 63 29 7b 0a 20 20 72 65 74 75   char c){.  retu
37f52 72 6e 20 63 3c 30 78 38 30 20 26 26 20 74 2d 3e  rn c<0x80 && t->
37f53 64 65 6c 69 6d 5b 63 5d 3b 0a 7d 0a 0a 2f 2a 0a  delim[c];.}../*.
37f54 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
37f55 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 73 74 61 6e  tokenizer instan
37f56 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ce..*/.static in
37f57 74 20 73 69 6d 70 6c 65 43 72 65 61 74 65 28 0a  t simpleCreate(.
37f58 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73    int argc, cons
37f59 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 2a  t char * const *
37f5a 61 72 67 76 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argv,.  sqlite3_
37f5b 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70 70 54 6f  tokenizer **ppTo
37f5c 6b 65 6e 69 7a 65 72 0a 29 7b 0a 20 20 73 69 6d  kenizer.){.  sim
37f5d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 74  ple_tokenizer *t
37f5e 3b 0a 0a 20 20 74 20 3d 20 28 73 69 6d 70 6c 65  ;..  t = (simple
37f5f 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 20 73 71  _tokenizer *) sq
37f60 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
37f61 65 6f 66 28 2a 74 29 29 3b 0a 20 20 69 66 28 20  eof(*t));.  if( 
37f62 74 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e  t==NULL ) return
37f63 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
37f64 20 6d 65 6d 73 65 74 28 74 2c 20 30 2c 20 73 69   memset(t, 0, si
37f65 7a 65 6f 66 28 2a 74 29 29 3b 0a 0a 20 20 2f 2a  zeof(*t));..  /*
37f66 20 54 4f 44 4f 28 73 68 65 73 73 29 20 44 65 6c   TODO(shess) Del
37f67 69 6d 69 74 65 72 73 20 6e 65 65 64 20 74 6f 20  imiters need to 
37f68 72 65 6d 61 69 6e 20 74 68 65 20 73 61 6d 65 20  remain the same 
37f69 66 72 6f 6d 20 72 75 6e 20 74 6f 20 72 75 6e 2c  from run to run,
37f6a 0a 20 20 2a 2a 20 65 6c 73 65 20 77 65 20 6e 65  .  ** else we ne
37f6b 65 64 20 74 6f 20 72 65 69 6e 64 65 78 2e 20 20  ed to reindex.  
37f6c 4f 6e 65 20 73 6f 6c 75 74 69 6f 6e 20 77 6f 75  One solution wou
37f6d 6c 64 20 62 65 20 61 20 6d 65 74 61 2d 74 61 62  ld be a meta-tab
37f6e 6c 65 20 74 6f 0a 20 20 2a 2a 20 74 72 61 63 6b  le to.  ** track
37f6f 20 73 75 63 68 20 69 6e 66 6f 72 6d 61 74 69 6f   such informatio
37f70 6e 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  n in the databas
37f71 65 2c 20 74 68 65 6e 20 77 65 27 64 20 6f 6e 6c  e, then we'd onl
37f72 79 20 77 61 6e 74 20 74 68 69 73 0a 20 20 2a 2a  y want this.  **
37f73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20   information on 
37f74 74 68 65 20 69 6e 69 74 69 61 6c 20 63 72 65 61  the initial crea
37f75 74 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61  te..  */.  if( a
37f76 72 67 63 3e 31 20 29 7b 0a 20 20 20 20 69 6e 74  rgc>1 ){.    int
37f77 20 69 2c 20 6e 20 3d 20 73 74 72 6c 65 6e 28 61   i, n = strlen(a
37f78 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 66 6f 72  rgv[1]);.    for
37f79 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b  (i=0; i<n; i++){
37f7a 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  .      unsigned 
37f7b 63 68 61 72 20 63 68 20 3d 20 61 72 67 76 5b 31  char ch = argv[1
37f7c 5d 5b 69 5d 3b 0a 20 20 20 20 20 20 2f 2a 20 57  ][i];.      /* W
37f7d 65 20 65 78 70 6c 69 63 69 74 6c 79 20 64 6f 6e  e explicitly don
37f7e 27 74 20 73 75 70 70 6f 72 74 20 55 54 46 2d 38  't support UTF-8
37f7f 20 64 65 6c 69 6d 69 74 65 72 73 20 66 6f 72 20   delimiters for 
37f80 6e 6f 77 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66  now. */.      if
37f81 28 20 63 68 3e 3d 30 78 38 30 20 29 7b 0a 20 20  ( ch>=0x80 ){.  
37f82 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
37f83 65 65 28 74 29 3b 0a 20 20 20 20 20 20 20 20 72  ee(t);.        r
37f84 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
37f85 4f 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  OR;.      }.    
37f86 20 20 74 2d 3e 64 65 6c 69 6d 5b 63 68 5d 20 3d    t->delim[ch] =
37f87 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c   1;.    }.  } el
37f88 73 65 20 7b 0a 20 20 20 20 2f 2a 20 4d 61 72 6b  se {.    /* Mark
37f89 20 6e 6f 6e 2d 61 6c 70 68 61 6e 75 6d 65 72 69   non-alphanumeri
37f8a 63 20 41 53 43 49 49 20 63 68 61 72 61 63 74 65  c ASCII characte
37f8b 72 73 20 61 73 20 64 65 6c 69 6d 69 74 65 72 73  rs as delimiters
37f8c 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   */.    int i;. 
37f8d 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 30 78     for(i=1; i<0x
37f8e 38 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  80; i++){.      
37f8f 74 2d 3e 64 65 6c 69 6d 5b 69 5d 20 3d 20 21 69  t->delim[i] = !i
37f90 73 61 6c 6e 75 6d 28 69 29 3b 0a 20 20 20 20 7d  salnum(i);.    }
37f91 0a 20 20 7d 0a 0a 20 20 2a 70 70 54 6f 6b 65 6e  .  }..  *ppToken
37f92 69 7a 65 72 20 3d 20 26 74 2d 3e 62 61 73 65 3b  izer = &t->base;
37f93 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
37f94 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  _OK;.}../*.** De
37f95 73 74 72 6f 79 20 61 20 74 6f 6b 65 6e 69 7a 65  stroy a tokenize
37f96 72 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  r.*/.static int 
37f97 73 69 6d 70 6c 65 44 65 73 74 72 6f 79 28 73 71  simpleDestroy(sq
37f98 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
37f99 2a 70 54 6f 6b 65 6e 69 7a 65 72 29 7b 0a 20 20  *pTokenizer){.  
37f9a 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6f  sqlite3_free(pTo
37f9b 6b 65 6e 69 7a 65 72 29 3b 0a 20 20 72 65 74 75  kenizer);.  retu
37f9c 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
37f9d 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 74  ./*.** Prepare t
37f9e 6f 20 62 65 67 69 6e 20 74 6f 6b 65 6e 69 7a 69  o begin tokenizi
37f9f 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  ng a particular 
37fa0 73 74 72 69 6e 67 2e 20 20 54 68 65 20 69 6e 70  string.  The inp
37fa1 75 74 0a 2a 2a 20 73 74 72 69 6e 67 20 74 6f 20  ut.** string to 
37fa2 62 65 20 74 6f 6b 65 6e 69 7a 65 64 20 69 73 20  be tokenized is 
37fa3 70 49 6e 70 75 74 5b 30 2e 2e 6e 42 79 74 65 73  pInput[0..nBytes
37fa4 2d 31 5d 2e 20 20 41 20 63 75 72 73 6f 72 0a 2a  -1].  A cursor.*
37fa5 2a 20 75 73 65 64 20 74 6f 20 69 6e 63 72 65 6d  * used to increm
37fa6 65 6e 74 61 6c 6c 79 20 74 6f 6b 65 6e 69 7a 65  entally tokenize
37fa7 20 74 68 69 73 20 73 74 72 69 6e 67 20 69 73 20   this string is 
37fa8 72 65 74 75 72 6e 65 64 20 69 6e 20 0a 2a 2a 20  returned in .** 
37fa9 2a 70 70 43 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74  *ppCursor..*/.st
37faa 61 74 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 4f  atic int simpleO
37fab 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 74  pen(.  sqlite3_t
37fac 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
37fad 69 7a 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a  izer,         /*
37fae 20 54 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a   The tokenizer *
37faf 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
37fb0 70 49 6e 70 75 74 2c 20 69 6e 74 20 6e 42 79 74  pInput, int nByt
37fb1 65 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 53 74  es,        /* St
37fb2 72 69 6e 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e  ring to be token
37fb3 69 7a 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ized */.  sqlite
37fb4 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
37fb5 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20 20  or **ppCursor   
37fb6 20 2f 2a 20 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a   /* OUT: Tokeniz
37fb7 61 74 69 6f 6e 20 63 75 72 73 6f 72 20 2a 2f 0a  ation cursor */.
37fb8 29 7b 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65  ){.  simple_toke
37fb9 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 3b  nizer_cursor *c;
37fba 0a 0a 20 20 63 20 3d 20 28 73 69 6d 70 6c 65 5f  ..  c = (simple_
37fbb 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
37fbc 20 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c   *) sqlite3_mall
37fbd 6f 63 28 73 69 7a 65 6f 66 28 2a 63 29 29 3b 0a  oc(sizeof(*c));.
37fbe 20 20 69 66 28 20 63 3d 3d 4e 55 4c 4c 20 29 20    if( c==NULL ) 
37fbf 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
37fc0 4d 45 4d 3b 0a 0a 20 20 63 2d 3e 70 49 6e 70 75  MEM;..  c->pInpu
37fc1 74 20 3d 20 70 49 6e 70 75 74 3b 0a 20 20 69 66  t = pInput;.  if
37fc2 28 20 70 49 6e 70 75 74 3d 3d 30 20 29 7b 0a 20  ( pInput==0 ){. 
37fc3 20 20 20 63 2d 3e 6e 42 79 74 65 73 20 3d 20 30     c->nBytes = 0
37fc4 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 42  ;.  }else if( nB
37fc5 79 74 65 73 3c 30 20 29 7b 0a 20 20 20 20 63 2d  ytes<0 ){.    c-
37fc6 3e 6e 42 79 74 65 73 20 3d 20 28 69 6e 74 29 73  >nBytes = (int)s
37fc7 74 72 6c 65 6e 28 70 49 6e 70 75 74 29 3b 0a 20  trlen(pInput);. 
37fc8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 2d 3e 6e   }else{.    c->n
37fc9 42 79 74 65 73 20 3d 20 6e 42 79 74 65 73 3b 0a  Bytes = nBytes;.
37fca 20 20 7d 0a 20 20 63 2d 3e 69 4f 66 66 73 65 74    }.  c->iOffset
37fcb 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
37fcc 20 20 20 20 20 20 2f 2a 20 73 74 61 72 74 20 74        /* start t
37fcd 6f 6b 65 6e 69 7a 69 6e 67 20 61 74 20 74 68 65  okenizing at the
37fce 20 62 65 67 69 6e 6e 69 6e 67 20 2a 2f 0a 20 20   beginning */.  
37fcf 63 2d 3e 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20  c->iToken = 0;. 
37fd0 20 63 2d 3e 70 54 6f 6b 65 6e 20 3d 20 4e 55 4c   c->pToken = NUL
37fd1 4c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  L;              
37fd2 20 2f 2a 20 6e 6f 20 73 70 61 63 65 20 61 6c 6c   /* no space all
37fd3 6f 63 61 74 65 64 2c 20 79 65 74 2e 20 2a 2f 0a  ocated, yet. */.
37fd4 20 20 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f 63    c->nTokenAlloc
37fd5 61 74 65 64 20 3d 20 30 3b 0a 0a 20 20 2a 70 70  ated = 0;..  *pp
37fd6 43 75 72 73 6f 72 20 3d 20 26 63 2d 3e 62 61 73  Cursor = &c->bas
37fd7 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  e;.  return SQLI
37fd8 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
37fd9 43 6c 6f 73 65 20 61 20 74 6f 6b 65 6e 69 7a 61  Close a tokeniza
37fda 74 69 6f 6e 20 63 75 72 73 6f 72 20 70 72 65 76  tion cursor prev
37fdb 69 6f 75 73 6c 79 20 6f 70 65 6e 65 64 20 62 79  iously opened by
37fdc 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 69   a call to.** si
37fdd 6d 70 6c 65 4f 70 65 6e 28 29 20 61 62 6f 76 65  mpleOpen() above
37fde 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
37fdf 73 69 6d 70 6c 65 43 6c 6f 73 65 28 73 71 6c 69  simpleClose(sqli
37fe0 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
37fe1 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a  rsor *pCursor){.
37fe2 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a    simple_tokeniz
37fe3 65 72 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28  er_cursor *c = (
37fe4 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72  simple_tokenizer
37fe5 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73  _cursor *) pCurs
37fe6 6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  or;.  sqlite3_fr
37fe7 65 65 28 63 2d 3e 70 54 6f 6b 65 6e 29 3b 0a 20  ee(c->pToken);. 
37fe8 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63 29   sqlite3_free(c)
37fe9 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
37fea 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  E_OK;.}../*.** E
37feb 78 74 72 61 63 74 20 74 68 65 20 6e 65 78 74 20  xtract the next 
37fec 74 6f 6b 65 6e 20 66 72 6f 6d 20 61 20 74 6f 6b  token from a tok
37fed 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72  enization cursor
37fee 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d 75  .  The cursor mu
37fef 73 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20  st.** have been 
37ff0 6f 70 65 6e 65 64 20 62 79 20 61 20 70 72 69 6f  opened by a prio
37ff1 72 20 63 61 6c 6c 20 74 6f 20 73 69 6d 70 6c 65  r call to simple
37ff2 4f 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  Open()..*/.stati
37ff3 63 20 69 6e 74 20 73 69 6d 70 6c 65 4e 65 78 74  c int simpleNext
37ff4 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  (.  sqlite3_toke
37ff5 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43  nizer_cursor *pC
37ff6 75 72 73 6f 72 2c 20 20 2f 2a 20 43 75 72 73 6f  ursor,  /* Curso
37ff7 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 69  r returned by si
37ff8 6d 70 6c 65 4f 70 65 6e 20 2a 2f 0a 20 20 63 6f  mpleOpen */.  co
37ff9 6e 73 74 20 63 68 61 72 20 2a 2a 70 70 54 6f 6b  nst char **ppTok
37ffa 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  en,             
37ffb 20 20 2f 2a 20 4f 55 54 3a 20 2a 70 70 54 6f 6b    /* OUT: *ppTok
37ffc 65 6e 20 69 73 20 74 68 65 20 74 6f 6b 65 6e 20  en is the token 
37ffd 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  text */.  int *p
37ffe 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20  nBytes,         
37fff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
38000 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20   OUT: Number of 
38001 62 79 74 65 73 20 69 6e 20 74 6f 6b 65 6e 20 2a  bytes in token *
38002 2f 0a 20 20 69 6e 74 20 2a 70 69 53 74 61 72 74  /.  int *piStart
38003 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20  Offset,         
38004 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
38005 53 74 61 72 74 69 6e 67 20 6f 66 66 73 65 74 20  Starting offset 
38006 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e  of token */.  in
38007 74 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 2c 20  t *piEndOffset, 
38008 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38009 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 69 6e 67    /* OUT: Ending
3800a 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e   offset of token
3800b 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 50 6f 73   */.  int *piPos
3800c 69 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20  ition           
3800d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
3800e 3a 20 50 6f 73 69 74 69 6f 6e 20 69 6e 74 65 67  : Position integ
3800f 65 72 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 29  er of token */.)
38010 7b 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e  {.  simple_token
38011 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 20 3d  izer_cursor *c =
38012 20 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a   (simple_tokeniz
38013 65 72 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75  er_cursor *) pCu
38014 72 73 6f 72 3b 0a 20 20 73 69 6d 70 6c 65 5f 74  rsor;.  simple_t
38015 6f 6b 65 6e 69 7a 65 72 20 2a 74 20 3d 20 28 73  okenizer *t = (s
38016 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20  imple_tokenizer 
38017 2a 29 20 70 43 75 72 73 6f 72 2d 3e 70 54 6f 6b  *) pCursor->pTok
38018 65 6e 69 7a 65 72 3b 0a 20 20 75 6e 73 69 67 6e  enizer;.  unsign
38019 65 64 20 63 68 61 72 20 2a 70 20 3d 20 28 75 6e  ed char *p = (un
3801a 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 63 2d  signed char *)c-
3801b 3e 70 49 6e 70 75 74 3b 0a 0a 20 20 77 68 69 6c  >pInput;..  whil
3801c 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d  e( c->iOffset<c-
3801d 3e 6e 42 79 74 65 73 20 29 7b 0a 20 20 20 20 69  >nBytes ){.    i
3801e 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65 74 3b  nt iStartOffset;
3801f 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e 20 70 61  ..    /* Scan pa
38020 73 74 20 64 65 6c 69 6d 69 74 65 72 20 63 68 61  st delimiter cha
38021 72 61 63 74 65 72 73 20 2a 2f 0a 20 20 20 20 77  racters */.    w
38022 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73 65 74  hile( c->iOffset
38023 3c 63 2d 3e 6e 42 79 74 65 73 20 26 26 20 73 69  <c->nBytes && si
38024 6d 70 6c 65 44 65 6c 69 6d 28 74 2c 20 70 5b 63  mpleDelim(t, p[c
38025 2d 3e 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a 20  ->iOffset]) ){. 
38026 20 20 20 20 20 63 2d 3e 69 4f 66 66 73 65 74 2b       c->iOffset+
38027 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  +;.    }..    /*
38028 20 43 6f 75 6e 74 20 6e 6f 6e 2d 64 65 6c 69 6d   Count non-delim
38029 69 74 65 72 20 63 68 61 72 61 63 74 65 72 73 2e  iter characters.
3802a 20 2a 2f 0a 20 20 20 20 69 53 74 61 72 74 4f 66   */.    iStartOf
3802b 66 73 65 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65  fset = c->iOffse
3802c 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 63 2d  t;.    while( c-
3802d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 42 79 74  >iOffset<c->nByt
3802e 65 73 20 26 26 20 21 73 69 6d 70 6c 65 44 65 6c  es && !simpleDel
3802f 69 6d 28 74 2c 20 70 5b 63 2d 3e 69 4f 66 66 73  im(t, p[c->iOffs
38030 65 74 5d 29 20 29 7b 0a 20 20 20 20 20 20 63 2d  et]) ){.      c-
38031 3e 69 4f 66 66 73 65 74 2b 2b 3b 0a 20 20 20 20  >iOffset++;.    
38032 7d 0a 0a 20 20 20 20 69 66 28 20 63 2d 3e 69 4f  }..    if( c->iO
38033 66 66 73 65 74 3e 69 53 74 61 72 74 4f 66 66 73  ffset>iStartOffs
38034 65 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  et ){.      int 
38035 69 2c 20 6e 20 3d 20 63 2d 3e 69 4f 66 66 73 65  i, n = c->iOffse
38036 74 2d 69 53 74 61 72 74 4f 66 66 73 65 74 3b 0a  t-iStartOffset;.
38037 20 20 20 20 20 20 69 66 28 20 6e 3e 63 2d 3e 6e        if( n>c->n
38038 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65 64 20 29  TokenAllocated )
38039 7b 0a 20 20 20 20 20 20 20 20 63 2d 3e 6e 54 6f  {.        c->nTo
3803a 6b 65 6e 41 6c 6c 6f 63 61 74 65 64 20 3d 20 6e  kenAllocated = n
3803b 2b 32 30 3b 0a 20 20 20 20 20 20 20 20 63 2d 3e  +20;.        c->
3803c 70 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33  pToken = sqlite3
3803d 5f 72 65 61 6c 6c 6f 63 28 63 2d 3e 70 54 6f 6b  _realloc(c->pTok
3803e 65 6e 2c 20 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c  en, c->nTokenAll
3803f 6f 63 61 74 65 64 29 3b 0a 20 20 20 20 20 20 20  ocated);.       
38040 20 69 66 28 20 63 2d 3e 70 54 6f 6b 65 6e 3d 3d   if( c->pToken==
38041 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 20 53 51  NULL ) return SQ
38042 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
38043 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d    }.      for(i=
38044 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20  0; i<n; i++){.  
38045 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68        /* TODO(sh
38046 65 73 73 29 20 54 68 69 73 20 6e 65 65 64 73 20  ess) This needs 
38047 65 78 70 61 6e 73 69 6f 6e 20 74 6f 20 68 61 6e  expansion to han
38048 64 6c 65 20 55 54 46 2d 38 0a 20 20 20 20 20 20  dle UTF-8.      
38049 20 20 2a 2a 20 63 61 73 65 2d 69 6e 73 65 6e 73    ** case-insens
3804a 69 74 69 76 69 74 79 2e 0a 20 20 20 20 20 20 20  itivity..       
3804b 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 6e 73 69   */.        unsi
3804c 67 6e 65 64 20 63 68 61 72 20 63 68 20 3d 20 70  gned char ch = p
3804d 5b 69 53 74 61 72 74 4f 66 66 73 65 74 2b 69 5d  [iStartOffset+i]
3804e 3b 0a 20 20 20 20 20 20 20 20 63 2d 3e 70 54 6f  ;.        c->pTo
3804f 6b 65 6e 5b 69 5d 20 3d 20 63 68 3c 30 78 38 30  ken[i] = ch<0x80
38050 20 3f 20 74 6f 6c 6f 77 65 72 28 63 68 29 20 3a   ? tolower(ch) :
38051 20 63 68 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   ch;.      }.   
38052 20 20 20 2a 70 70 54 6f 6b 65 6e 20 3d 20 63 2d     *ppToken = c-
38053 3e 70 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 2a  >pToken;.      *
38054 70 6e 42 79 74 65 73 20 3d 20 6e 3b 0a 20 20 20  pnBytes = n;.   
38055 20 20 20 2a 70 69 53 74 61 72 74 4f 66 66 73 65     *piStartOffse
38056 74 20 3d 20 69 53 74 61 72 74 4f 66 66 73 65 74  t = iStartOffset
38057 3b 0a 20 20 20 20 20 20 2a 70 69 45 6e 64 4f 66  ;.      *piEndOf
38058 66 73 65 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65  fset = c->iOffse
38059 74 3b 0a 20 20 20 20 20 20 2a 70 69 50 6f 73 69  t;.      *piPosi
3805a 74 69 6f 6e 20 3d 20 63 2d 3e 69 54 6f 6b 65 6e  tion = c->iToken
3805b 2b 2b 3b 0a 0a 20 20 20 20 20 20 72 65 74 75 72  ++;..      retur
3805c 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
3805d 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
3805e 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 7d 0a 0a  SQLITE_DONE;.}..
3805f 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 74 20 6f 66  /*.** The set of
38060 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 69   routines that i
38061 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 73 69 6d  mplement the sim
38062 70 6c 65 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f  ple tokenizer.*/
38063 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71  .static const sq
38064 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
38065 6d 6f 64 75 6c 65 20 73 69 6d 70 6c 65 54 6f 6b  module simpleTok
38066 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 20 3d 20 7b  enizerModule = {
38067 0a 20 20 30 2c 0a 20 20 73 69 6d 70 6c 65 43 72  .  0,.  simpleCr
38068 65 61 74 65 2c 0a 20 20 73 69 6d 70 6c 65 44 65  eate,.  simpleDe
38069 73 74 72 6f 79 2c 0a 20 20 73 69 6d 70 6c 65 4f  stroy,.  simpleO
3806a 70 65 6e 2c 0a 20 20 73 69 6d 70 6c 65 43 6c 6f  pen,.  simpleClo
3806b 73 65 2c 0a 20 20 73 69 6d 70 6c 65 4e 65 78 74  se,.  simpleNext
3806c 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  ,.};../*.** Allo
3806d 63 61 74 65 20 61 20 6e 65 77 20 73 69 6d 70 6c  cate a new simpl
3806e 65 20 74 6f 6b 65 6e 69 7a 65 72 2e 20 20 52 65  e tokenizer.  Re
3806f 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
38070 6f 20 74 68 65 20 6e 65 77 0a 2a 2a 20 74 6f 6b  o the new.** tok
38071 65 6e 69 7a 65 72 20 69 6e 20 2a 70 70 4d 6f 64  enizer in *ppMod
38072 75 6c 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ule.*/.SQLITE_PR
38073 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
38074 65 33 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65  e3Fts3SimpleToke
38075 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 0a 20 20 73  nizerModule(.  s
38076 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
38077 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70  _module const**p
38078 70 4d 6f 64 75 6c 65 0a 29 7b 0a 20 20 2a 70 70  pModule.){.  *pp
38079 4d 6f 64 75 6c 65 20 3d 20 26 73 69 6d 70 6c 65  Module = &simple
3807a 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b  TokenizerModule;
3807b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64  .}..#endif /* !d
3807c 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
3807d 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
3807e 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
3807f 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  3) */../********
38080 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74  ****** End of ft
38081 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 31 2e 63 20  s3_tokenizer1.c 
38082 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38083 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38084 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
38085 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
38086 65 20 72 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a  e rtree.c ******
38087 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38088 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38089 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
3808a 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
3808b 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
3808c 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
3808d 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
3808e 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
3808f 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
38090 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
38091 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
38092 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
38093 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
38094 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
38095 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
38096 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
38097 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
38098 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
38099 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
3809a 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
3809b 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
3809c 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
3809d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3809e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3809f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
380a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
380a1 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
380a2 69 6e 73 20 63 6f 64 65 20 66 6f 72 20 69 6d 70  ins code for imp
380a3 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20  lementations of 
380a4 74 68 65 20 72 2d 74 72 65 65 20 61 6e 64 20 72  the r-tree and r
380a5 2a 2d 74 72 65 65 0a 2a 2a 20 61 6c 67 6f 72 69  *-tree.** algori
380a6 74 68 6d 73 20 70 61 63 6b 61 67 65 64 20 61 73  thms packaged as
380a7 20 61 6e 20 53 51 4c 69 74 65 20 76 69 72 74 75   an SQLite virtu
380a8 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 2e  al table module.
380a9 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 74 72 65  .**.** $Id: rtre
380aa 65 2e 63 2c 76 20 31 2e 31 34 20 32 30 30 39 2f  e.c,v 1.14 2009/
380ab 30 38 2f 30 36 20 31 38 3a 33 36 3a 34 37 20 64  08/06 18:36:47 d
380ac 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
380ad 0a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65  .*/..#if !define
380ae 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c  d(SQLITE_CORE) |
380af 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
380b0 5f 45 4e 41 42 4c 45 5f 52 54 52 45 45 29 0a 0a  _ENABLE_RTREE)..
380b1 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  /*.** This file 
380b2 63 6f 6e 74 61 69 6e 73 20 61 6e 20 69 6d 70 6c  contains an impl
380b3 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20  ementation of a 
380b4 63 6f 75 70 6c 65 20 6f 66 20 64 69 66 66 65 72  couple of differ
380b5 65 6e 74 20 76 61 72 69 61 6e 74 73 0a 2a 2a 20  ent variants.** 
380b6 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20 61 6c  of the r-tree al
380b7 67 6f 72 69 74 68 6d 2e 20 53 65 65 20 74 68 65  gorithm. See the
380b8 20 52 45 41 44 4d 45 20 66 69 6c 65 20 66 6f 72   README file for
380b9 20 66 75 72 74 68 65 72 20 64 65 74 61 69 6c 73   further details
380ba 2e 20 54 68 65 20 0a 2a 2a 20 73 61 6d 65 20 64  . The .** same d
380bb 61 74 61 2d 73 74 72 75 63 74 75 72 65 20 69 73  ata-structure is
380bc 20 75 73 65 64 20 66 6f 72 20 61 6c 6c 2c 20 62   used for all, b
380bd 75 74 20 74 68 65 20 61 6c 67 6f 72 69 74 68 6d  ut the algorithm
380be 73 20 66 6f 72 20 69 6e 73 65 72 74 20 61 6e 64  s for insert and
380bf 0a 2a 2a 20 64 65 6c 65 74 65 20 6f 70 65 72 61  .** delete opera
380c0 74 69 6f 6e 73 20 76 61 72 79 2e 20 54 68 65 20  tions vary. The 
380c1 76 61 72 69 61 6e 74 73 20 75 73 65 64 20 61 72  variants used ar
380c2 65 20 73 65 6c 65 63 74 65 64 20 61 74 20 63 6f  e selected at co
380c3 6d 70 69 6c 65 20 74 69 6d 65 20 0a 2a 2a 20 62  mpile time .** b
380c4 79 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 66  y defining the f
380c5 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d 62 6f 6c 73  ollowing symbols
380c6 3a 0a 2a 2f 0a 0a 2f 2a 20 45 69 74 68 65 72 2c  :.*/../* Either,
380c7 20 62 6f 74 68 20 6f 72 20 6e 6f 6e 65 20 6f 66   both or none of
380c8 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   the following m
380c9 61 79 20 62 65 20 73 65 74 20 74 6f 20 61 63 74  ay be set to act
380ca 69 76 61 74 65 20 0a 2a 2a 20 72 2a 74 72 65 65  ivate .** r*tree
380cb 20 76 61 72 69 61 6e 74 20 61 6c 67 6f 72 69 74   variant algorit
380cc 68 6d 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  hms..*/.#define 
380cd 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45  VARIANT_RSTARTRE
380ce 45 5f 43 48 4f 4f 53 45 53 55 42 54 52 45 45 20  E_CHOOSESUBTREE 
380cf 30 0a 23 64 65 66 69 6e 65 20 56 41 52 49 41 4e  0.#define VARIAN
380d0 54 5f 52 53 54 41 52 54 52 45 45 5f 52 45 49 4e  T_RSTARTREE_REIN
380d1 53 45 52 54 20 20 20 20 20 20 31 0a 0a 2f 2a 20  SERT      1../* 
380d2 0a 2a 2a 20 45 78 61 63 74 6c 79 20 6f 6e 65 20  .** Exactly one 
380d3 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
380d4 20 6d 75 73 74 20 62 65 20 73 65 74 20 74 6f 20   must be set to 
380d5 31 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 56 41  1..*/.#define VA
380d6 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 51 55  RIANT_GUTTMAN_QU
380d7 41 44 52 41 54 49 43 5f 53 50 4c 49 54 20 30 0a  ADRATIC_SPLIT 0.
380d8 23 64 65 66 69 6e 65 20 56 41 52 49 41 4e 54 5f  #define VARIANT_
380d9 47 55 54 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53  GUTTMAN_LINEAR_S
380da 50 4c 49 54 20 20 20 20 30 0a 23 64 65 66 69 6e  PLIT    0.#defin
380db 65 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54  e VARIANT_RSTART
380dc 52 45 45 5f 53 50 4c 49 54 20 20 20 20 20 20 20  REE_SPLIT       
380dd 20 20 31 0a 0a 23 64 65 66 69 6e 65 20 56 41 52    1..#define VAR
380de 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 53 50 4c  IANT_GUTTMAN_SPL
380df 49 54 20 5c 0a 20 20 20 20 20 20 20 20 28 56 41  IT \.        (VA
380e0 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49  RIANT_GUTTMAN_LI
380e1 4e 45 41 52 5f 53 50 4c 49 54 7c 7c 56 41 52 49  NEAR_SPLIT||VARI
380e2 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 51 55 41 44  ANT_GUTTMAN_QUAD
380e3 52 41 54 49 43 5f 53 50 4c 49 54 29 0a 0a 23 69  RATIC_SPLIT)..#i
380e4 66 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41  f VARIANT_GUTTMA
380e5 4e 5f 51 55 41 44 52 41 54 49 43 5f 53 50 4c 49  N_QUADRATIC_SPLI
380e6 54 0a 20 20 23 64 65 66 69 6e 65 20 50 69 63 6b  T.  #define Pick
380e7 4e 65 78 74 20 51 75 61 64 72 61 74 69 63 50 69  Next QuadraticPi
380e8 63 6b 4e 65 78 74 0a 20 20 23 64 65 66 69 6e 65  ckNext.  #define
380e9 20 50 69 63 6b 53 65 65 64 73 20 51 75 61 64 72   PickSeeds Quadr
380ea 61 74 69 63 50 69 63 6b 53 65 65 64 73 0a 20 20  aticPickSeeds.  
380eb 23 64 65 66 69 6e 65 20 41 73 73 69 67 6e 43 65  #define AssignCe
380ec 6c 6c 73 20 73 70 6c 69 74 4e 6f 64 65 47 75 74  lls splitNodeGut
380ed 74 6d 61 6e 0a 23 65 6e 64 69 66 0a 23 69 66 20  tman.#endif.#if 
380ee 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f  VARIANT_GUTTMAN_
380ef 4c 49 4e 45 41 52 5f 53 50 4c 49 54 0a 20 20 23  LINEAR_SPLIT.  #
380f0 64 65 66 69 6e 65 20 50 69 63 6b 4e 65 78 74 20  define PickNext 
380f1 4c 69 6e 65 61 72 50 69 63 6b 4e 65 78 74 0a 20  LinearPickNext. 
380f2 20 23 64 65 66 69 6e 65 20 50 69 63 6b 53 65 65   #define PickSee
380f3 64 73 20 4c 69 6e 65 61 72 50 69 63 6b 53 65 65  ds LinearPickSee
380f4 64 73 0a 20 20 23 64 65 66 69 6e 65 20 41 73 73  ds.  #define Ass
380f5 69 67 6e 43 65 6c 6c 73 20 73 70 6c 69 74 4e 6f  ignCells splitNo
380f6 64 65 47 75 74 74 6d 61 6e 0a 23 65 6e 64 69 66  deGuttman.#endif
380f7 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54  .#if VARIANT_RST
380f8 41 52 54 52 45 45 5f 53 50 4c 49 54 0a 20 20 23  ARTREE_SPLIT.  #
380f9 64 65 66 69 6e 65 20 41 73 73 69 67 6e 43 65 6c  define AssignCel
380fa 6c 73 20 73 70 6c 69 74 4e 6f 64 65 53 74 61 72  ls splitNodeStar
380fb 74 72 65 65 0a 23 65 6e 64 69 66 0a 0a 0a 23 69  tree.#endif...#i
380fc 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52  fndef SQLITE_COR
380fd 45 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e  E.  SQLITE_EXTEN
380fe 53 49 4f 4e 5f 49 4e 49 54 31 0a 23 65 6c 73 65  SION_INIT1.#else
380ff 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65  .#endif...#ifnde
38100 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d  f SQLITE_AMALGAM
38101 41 54 49 4f 4e 0a 74 79 70 65 64 65 66 20 73 71  ATION.typedef sq
38102 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 36 34 3b  lite3_int64 i64;
38103 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65  .typedef unsigne
38104 64 20 63 68 61 72 20 75 38 3b 0a 74 79 70 65 64  d char u8;.typed
38105 65 66 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  ef unsigned int 
38106 75 33 32 3b 0a 23 65 6e 64 69 66 0a 0a 74 79 70  u32;.#endif..typ
38107 65 64 65 66 20 73 74 72 75 63 74 20 52 74 72 65  edef struct Rtre
38108 65 20 52 74 72 65 65 3b 0a 74 79 70 65 64 65 66  e Rtree;.typedef
38109 20 73 74 72 75 63 74 20 52 74 72 65 65 43 75 72   struct RtreeCur
3810a 73 6f 72 20 52 74 72 65 65 43 75 72 73 6f 72 3b  sor RtreeCursor;
3810b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
3810c 52 74 72 65 65 4e 6f 64 65 20 52 74 72 65 65 4e  RtreeNode RtreeN
3810d 6f 64 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ode;.typedef str
3810e 75 63 74 20 52 74 72 65 65 43 65 6c 6c 20 52 74  uct RtreeCell Rt
3810f 72 65 65 43 65 6c 6c 3b 0a 74 79 70 65 64 65 66  reeCell;.typedef
38110 20 73 74 72 75 63 74 20 52 74 72 65 65 43 6f 6e   struct RtreeCon
38111 73 74 72 61 69 6e 74 20 52 74 72 65 65 43 6f 6e  straint RtreeCon
38112 73 74 72 61 69 6e 74 3b 0a 74 79 70 65 64 65 66  straint;.typedef
38113 20 75 6e 69 6f 6e 20 52 74 72 65 65 43 6f 6f 72   union RtreeCoor
38114 64 20 52 74 72 65 65 43 6f 6f 72 64 3b 0a 0a 2f  d RtreeCoord;../
38115 2a 20 54 68 65 20 72 74 72 65 65 20 6d 61 79 20  * The rtree may 
38116 68 61 76 65 20 62 65 74 77 65 65 6e 20 31 20 61  have between 1 a
38117 6e 64 20 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d  nd RTREE_MAX_DIM
38118 45 4e 53 49 4f 4e 53 20 64 69 6d 65 6e 73 69 6f  ENSIONS dimensio
38119 6e 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 52  ns. */.#define R
3811a 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49  TREE_MAX_DIMENSI
3811b 4f 4e 53 20 35 0a 0a 2f 2a 20 53 69 7a 65 20 6f  ONS 5../* Size o
3811c 66 20 68 61 73 68 20 74 61 62 6c 65 20 52 74 72  f hash table Rtr
3811d 65 65 2e 61 48 61 73 68 2e 20 54 68 69 73 20 68  ee.aHash. This h
3811e 61 73 68 20 74 61 62 6c 65 20 69 73 20 6e 6f 74  ash table is not
3811f 20 65 78 70 65 63 74 65 64 20 74 6f 0a 2a 2a 20   expected to.** 
38120 65 76 65 72 20 63 6f 6e 74 61 69 6e 20 76 65 72  ever contain ver
38121 79 20 6d 61 6e 79 20 65 6e 74 72 69 65 73 2c 20  y many entries, 
38122 73 6f 20 61 20 66 69 78 65 64 20 6e 75 6d 62 65  so a fixed numbe
38123 72 20 6f 66 20 62 75 63 6b 65 74 73 20 69 73 20  r of buckets is 
38124 0a 2a 2a 20 75 73 65 64 2e 0a 2a 2f 0a 23 64 65  .** used..*/.#de
38125 66 69 6e 65 20 48 41 53 48 53 49 5a 45 20 31 32  fine HASHSIZE 12
38126 38 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 72 74 72  8../* .** An rtr
38127 65 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  ee virtual-table
38128 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 72 75   object..*/.stru
38129 63 74 20 52 74 72 65 65 20 7b 0a 20 20 73 71 6c  ct Rtree {.  sql
3812a 69 74 65 33 5f 76 74 61 62 20 62 61 73 65 3b 0a  ite3_vtab base;.
3812b 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
3812c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3812d 20 48 6f 73 74 20 64 61 74 61 62 61 73 65 20 63   Host database c
3812e 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69  onnection */.  i
3812f 6e 74 20 69 4e 6f 64 65 53 69 7a 65 3b 20 20 20  nt iNodeSize;   
38130 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
38131 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 65  ze in bytes of e
38132 61 63 68 20 6e 6f 64 65 20 69 6e 20 74 68 65 20  ach node in the 
38133 6e 6f 64 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20  node table */.  
38134 69 6e 74 20 6e 44 69 6d 3b 20 20 20 20 20 20 20  int nDim;       
38135 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
38136 75 6d 62 65 72 20 6f 66 20 64 69 6d 65 6e 73 69  umber of dimensi
38137 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79  ons */.  int nBy
38138 74 65 73 50 65 72 43 65 6c 6c 3b 20 20 20 20 20  tesPerCell;     
38139 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 63 6f       /* Bytes co
3813a 6e 73 75 6d 65 64 20 70 65 72 20 63 65 6c 6c 20  nsumed per cell 
3813b 2a 2f 0a 20 20 69 6e 74 20 69 44 65 70 74 68 3b  */.  int iDepth;
3813c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3813d 20 2f 2a 20 43 75 72 72 65 6e 74 20 64 65 70 74   /* Current dept
3813e 68 20 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20  h of the r-tree 
3813f 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 63  structure */.  c
38140 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20  har *zDb;       
38141 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
38142 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 63  me of database c
38143 6f 6e 74 61 69 6e 69 6e 67 20 72 2d 74 72 65 65  ontaining r-tree
38144 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72   table */.  char
38145 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20   *zName;        
38146 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
38147 6f 66 20 72 2d 74 72 65 65 20 74 61 62 6c 65 20  of r-tree table 
38148 2a 2f 20 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  */ .  RtreeNode 
38149 2a 61 48 61 73 68 5b 48 41 53 48 53 49 5a 45 5d  *aHash[HASHSIZE]
3814a 3b 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20  ; /* Hash table 
3814b 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 6e 6f 64  of in-memory nod
3814c 65 73 2e 20 2a 2f 20 0a 20 20 69 6e 74 20 6e 42  es. */ .  int nB
3814d 75 73 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  usy;            
3814e 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
3814f 20 6e 75 6d 62 65 72 20 6f 66 20 75 73 65 72 73   number of users
38150 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
38151 72 65 20 2a 2f 0a 0a 20 20 2f 2a 20 4c 69 73 74  re */..  /* List
38152 20 6f 66 20 6e 6f 64 65 73 20 72 65 6d 6f 76 65   of nodes remove
38153 64 20 64 75 72 69 6e 67 20 61 20 43 6f 6e 64 65  d during a Conde
38154 6e 73 65 54 72 65 65 20 6f 70 65 72 61 74 69 6f  nseTree operatio
38155 6e 2e 20 4c 69 73 74 20 69 73 0a 20 20 2a 2a 20  n. List is.  ** 
38156 6c 69 6e 6b 65 64 20 74 6f 67 65 74 68 65 72 20  linked together 
38157 76 69 61 20 74 68 65 20 70 6f 69 6e 74 65 72 20  via the pointer 
38158 6e 6f 72 6d 61 6c 6c 79 20 75 73 65 64 20 66 6f  normally used fo
38159 72 20 68 61 73 68 20 63 68 61 69 6e 73 20 2d 0a  r hash chains -.
3815a 20 20 2a 2a 20 52 74 72 65 65 4e 6f 64 65 2e 70    ** RtreeNode.p
3815b 4e 65 78 74 2e 20 52 74 72 65 65 4e 6f 64 65 2e  Next. RtreeNode.
3815c 69 4e 6f 64 65 20 73 74 6f 72 65 73 20 74 68 65  iNode stores the
3815d 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 73 75   depth of the su
3815e 62 2d 74 72 65 65 20 0a 20 20 2a 2a 20 68 65 61  b-tree .  ** hea
3815f 64 65 64 20 62 79 20 74 68 65 20 6e 6f 64 65 20  ded by the node 
38160 28 6c 65 61 66 20 6e 6f 64 65 73 20 68 61 76 65  (leaf nodes have
38161 20 52 74 72 65 65 4e 6f 64 65 2e 69 4e 6f 64 65   RtreeNode.iNode
38162 3d 3d 30 29 2e 0a 20 20 2a 2f 0a 20 20 52 74 72  ==0)..  */.  Rtr
38163 65 65 4e 6f 64 65 20 2a 70 44 65 6c 65 74 65 64  eeNode *pDeleted
38164 3b 0a 20 20 69 6e 74 20 69 52 65 69 6e 73 65 72  ;.  int iReinser
38165 74 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20  tHeight;        
38166 2f 2a 20 48 65 69 67 68 74 20 6f 66 20 73 75 62  /* Height of sub
38167 2d 74 72 65 65 73 20 52 65 69 6e 73 65 72 74 28  -trees Reinsert(
38168 29 20 68 61 73 20 72 75 6e 20 6f 6e 20 2a 2f 0a  ) has run on */.
38169 0a 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 73  .  /* Statements
3816a 20 74 6f 20 72 65 61 64 2f 77 72 69 74 65 2f 64   to read/write/d
3816b 65 6c 65 74 65 20 61 20 72 65 63 6f 72 64 20 66  elete a record f
3816c 72 6f 6d 20 78 78 78 5f 6e 6f 64 65 20 2a 2f 0a  rom xxx_node */.
3816d 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
3816e 70 52 65 61 64 4e 6f 64 65 3b 0a 20 20 73 71 6c  pReadNode;.  sql
3816f 69 74 65 33 5f 73 74 6d 74 20 2a 70 57 72 69 74  ite3_stmt *pWrit
38170 65 4e 6f 64 65 3b 0a 20 20 73 71 6c 69 74 65 33  eNode;.  sqlite3
38171 5f 73 74 6d 74 20 2a 70 44 65 6c 65 74 65 4e 6f  _stmt *pDeleteNo
38172 64 65 3b 0a 0a 20 20 2f 2a 20 53 74 61 74 65 6d  de;..  /* Statem
38173 65 6e 74 73 20 74 6f 20 72 65 61 64 2f 77 72 69  ents to read/wri
38174 74 65 2f 64 65 6c 65 74 65 20 61 20 72 65 63 6f  te/delete a reco
38175 72 64 20 66 72 6f 6d 20 78 78 78 5f 72 6f 77 69  rd from xxx_rowi
38176 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  d */.  sqlite3_s
38177 74 6d 74 20 2a 70 52 65 61 64 52 6f 77 69 64 3b  tmt *pReadRowid;
38178 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
38179 2a 70 57 72 69 74 65 52 6f 77 69 64 3b 0a 20 20  *pWriteRowid;.  
3817a 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 44  sqlite3_stmt *pD
3817b 65 6c 65 74 65 52 6f 77 69 64 3b 0a 0a 20 20 2f  eleteRowid;..  /
3817c 2a 20 53 74 61 74 65 6d 65 6e 74 73 20 74 6f 20  * Statements to 
3817d 72 65 61 64 2f 77 72 69 74 65 2f 64 65 6c 65 74  read/write/delet
3817e 65 20 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20  e a record from 
3817f 78 78 78 5f 70 61 72 65 6e 74 20 2a 2f 0a 20 20  xxx_parent */.  
38180 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 52  sqlite3_stmt *pR
38181 65 61 64 50 61 72 65 6e 74 3b 0a 20 20 73 71 6c  eadParent;.  sql
38182 69 74 65 33 5f 73 74 6d 74 20 2a 70 57 72 69 74  ite3_stmt *pWrit
38183 65 50 61 72 65 6e 74 3b 0a 20 20 73 71 6c 69 74  eParent;.  sqlit
38184 65 33 5f 73 74 6d 74 20 2a 70 44 65 6c 65 74 65  e3_stmt *pDelete
38185 50 61 72 65 6e 74 3b 0a 0a 20 20 69 6e 74 20 65  Parent;..  int e
38186 43 6f 6f 72 64 54 79 70 65 3b 0a 7d 3b 0a 0a 2f  CoordType;.};../
38187 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65  * Possible value
38188 73 20 66 6f 72 20 65 43 6f 6f 72 64 54 79 70 65  s for eCoordType
38189 3a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 52 54 52  : */.#define RTR
3818a 45 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32 20  EE_COORD_REAL32 
3818b 30 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f  0.#define RTREE_
3818c 43 4f 4f 52 44 5f 49 4e 54 33 32 20 20 31 0a 0a  COORD_INT32  1..
3818d 2f 2a 0a 2a 2a 20 54 68 65 20 6d 69 6e 69 6d 75  /*.** The minimu
3818e 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c  m number of cell
3818f 73 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20 61 20  s allowed for a 
38190 6e 6f 64 65 20 69 73 20 61 20 74 68 69 72 64 20  node is a third 
38191 6f 66 20 74 68 65 20 0a 2a 2a 20 6d 61 78 69 6d  of the .** maxim
38192 75 6d 2e 20 49 6e 20 47 75 74 6d 61 6e 27 73 20  um. In Gutman's 
38193 6e 6f 74 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20  notation:.**.** 
38194 20 20 20 20 6d 20 3d 20 4d 2f 33 0a 2a 2a 0a 2a      m = M/3.**.*
38195 2a 20 49 66 20 61 6e 20 52 2a 2d 74 72 65 65 20  * If an R*-tree 
38196 22 52 65 69 6e 73 65 72 74 22 20 6f 70 65 72 61  "Reinsert" opera
38197 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64  tion is required
38198 2c 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65  , the same numbe
38199 72 20 6f 66 0a 2a 2a 20 63 65 6c 6c 73 20 61 72  r of.** cells ar
3819a 65 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74  e removed from t
3819b 68 65 20 6f 76 65 72 66 75 6c 6c 20 6e 6f 64 65  he overfull node
3819c 20 61 6e 64 20 72 65 69 6e 73 65 72 74 65 64 20   and reinserted 
3819d 69 6e 74 6f 20 74 68 65 20 74 72 65 65 2e 0a 2a  into the tree..*
3819e 2f 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f  /.#define RTREE_
3819f 4d 49 4e 43 45 4c 4c 53 28 70 29 20 28 28 28 28  MINCELLS(p) ((((
381a0 70 29 2d 3e 69 4e 6f 64 65 53 69 7a 65 2d 34 29  p)->iNodeSize-4)
381a1 2f 28 70 29 2d 3e 6e 42 79 74 65 73 50 65 72 43  /(p)->nBytesPerC
381a2 65 6c 6c 29 2f 33 29 0a 23 64 65 66 69 6e 65 20  ell)/3).#define 
381a3 52 54 52 45 45 5f 52 45 49 4e 53 45 52 54 28 70  RTREE_REINSERT(p
381a4 29 20 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53  ) RTREE_MINCELLS
381a5 28 70 29 0a 23 64 65 66 69 6e 65 20 52 54 52 45  (p).#define RTRE
381a6 45 5f 4d 41 58 43 45 4c 4c 53 20 35 31 0a 0a 2f  E_MAXCELLS 51../
381a7 2a 20 0a 2a 2a 20 41 6e 20 72 74 72 65 65 20 63  * .** An rtree c
381a8 75 72 73 6f 72 20 6f 62 6a 65 63 74 2e 0a 2a 2f  ursor object..*/
381a9 0a 73 74 72 75 63 74 20 52 74 72 65 65 43 75 72  .struct RtreeCur
381aa 73 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  sor {.  sqlite3_
381ab 76 74 61 62 5f 63 75 72 73 6f 72 20 62 61 73 65  vtab_cursor base
381ac 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ;.  RtreeNode *p
381ad 4e 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20  Node;           
381ae 20 20 20 20 20 20 2f 2a 20 4e 6f 64 65 20 63 75        /* Node cu
381af 72 73 6f 72 20 69 73 20 63 75 72 72 65 6e 74 6c  rsor is currentl
381b0 79 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 2a 2f  y pointing at */
381b1 0a 20 20 69 6e 74 20 69 43 65 6c 6c 3b 20 20 20  .  int iCell;   
381b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
381b3 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
381b4 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 69 6e   current cell in
381b5 20 70 4e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20   pNode */.  int 
381b6 69 53 74 72 61 74 65 67 79 3b 20 20 20 20 20 20  iStrategy;      
381b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
381b8 20 43 6f 70 79 20 6f 66 20 69 64 78 4e 75 6d 20   Copy of idxNum 
381b9 73 65 61 72 63 68 20 70 61 72 61 6d 65 74 65 72  search parameter
381ba 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6e 73 74   */.  int nConst
381bb 72 61 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20  raint;          
381bc 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
381bd 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
381be 61 43 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20  aConstraint */. 
381bf 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69 6e 74   RtreeConstraint
381c0 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20   *aConstraint;  
381c1 20 20 20 2f 2a 20 53 65 61 72 63 68 20 63 6f 6e     /* Search con
381c2 73 74 72 61 69 6e 74 73 2e 20 2a 2f 0a 7d 3b 0a  straints. */.};.
381c3 0a 75 6e 69 6f 6e 20 52 74 72 65 65 43 6f 6f 72  .union RtreeCoor
381c4 64 20 7b 0a 20 20 66 6c 6f 61 74 20 66 3b 0a 20  d {.  float f;. 
381c5 20 69 6e 74 20 69 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a   int i;.};../*.*
381c6 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69  * The argument i
381c7 73 20 61 6e 20 52 74 72 65 65 43 6f 6f 72 64 2e  s an RtreeCoord.
381c8 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75   Return the valu
381c9 65 20 73 74 6f 72 65 64 20 77 69 74 68 69 6e 20  e stored within 
381ca 74 68 65 20 52 74 72 65 65 43 6f 6f 72 64 0a 2a  the RtreeCoord.*
381cb 2a 20 66 6f 72 6d 61 74 74 65 64 20 61 73 20 61  * formatted as a
381cc 20 64 6f 75 62 6c 65 2e 20 54 68 69 73 20 6d 61   double. This ma
381cd 63 72 6f 20 61 73 73 75 6d 65 73 20 74 68 61 74  cro assumes that
381ce 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 20   local variable 
381cf 70 52 74 72 65 65 20 70 6f 69 6e 74 73 0a 2a 2a  pRtree points.**
381d0 20 74 6f 20 74 68 65 20 52 74 72 65 65 20 73 74   to the Rtree st
381d1 72 75 63 74 75 72 65 20 61 73 73 6f 63 69 61 74  ructure associat
381d2 65 64 20 77 69 74 68 20 74 68 65 20 52 74 72 65  ed with the Rtre
381d3 65 43 6f 6f 72 64 2e 0a 2a 2f 0a 23 64 65 66 69  eCoord..*/.#defi
381d4 6e 65 20 44 43 4f 4f 52 44 28 63 6f 6f 72 64 29  ne DCOORD(coord)
381d5 20 28 20 20 20 20 20 20 20 20 20 20 20 20 20 20   (              
381d6 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
381d7 20 28 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64   (pRtree->eCoord
381d8 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52  Type==RTREE_COOR
381d9 44 5f 52 45 41 4c 33 32 29 20 3f 20 20 20 20 20  D_REAL32) ?     
381da 20 5c 0a 20 20 20 20 28 28 64 6f 75 62 6c 65 29   \.    ((double)
381db 63 6f 6f 72 64 2e 66 29 20 3a 20 20 20 20 20 20  coord.f) :      
381dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
381dd 20 20 20 20 20 5c 0a 20 20 20 20 28 28 64 6f 75       \.    ((dou
381de 62 6c 65 29 63 6f 6f 72 64 2e 69 29 20 20 20 20  ble)coord.i)    
381df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
381e0 20 20 20 20 20 20 20 20 20 5c 0a 29 0a 0a 2f 2a           \.)../*
381e1 0a 2a 2a 20 41 20 73 65 61 72 63 68 20 63 6f 6e  .** A search con
381e2 73 74 72 61 69 6e 74 2e 0a 2a 2f 0a 73 74 72 75  straint..*/.stru
381e3 63 74 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69  ct RtreeConstrai
381e4 6e 74 20 7b 0a 20 20 69 6e 74 20 69 43 6f 6f 72  nt {.  int iCoor
381e5 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
381e6 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
381e7 78 20 6f 66 20 63 6f 6e 73 74 72 61 69 6e 65 64  x of constrained
381e8 20 63 6f 6f 72 64 69 6e 61 74 65 20 2a 2f 0a 20   coordinate */. 
381e9 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20   int op;        
381ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
381eb 20 20 20 2f 2a 20 43 6f 6e 73 74 72 61 69 6e 69     /* Constraini
381ec 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a  ng operation */.
381ed 20 20 64 6f 75 62 6c 65 20 72 56 61 6c 75 65 3b    double rValue;
381ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
381ef 20 20 20 20 2f 2a 20 43 6f 6e 73 74 72 61 69 6e      /* Constrain
381f0 74 20 76 61 6c 75 65 2e 20 2a 2f 0a 7d 3b 0a 0a  t value. */.};..
381f1 2f 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75  /* Possible valu
381f2 65 73 20 66 6f 72 20 52 74 72 65 65 43 6f 6e 73  es for RtreeCons
381f3 74 72 61 69 6e 74 2e 6f 70 20 2a 2f 0a 23 64 65  traint.op */.#de
381f4 66 69 6e 65 20 52 54 52 45 45 5f 45 51 20 30 78  fine RTREE_EQ 0x
381f5 34 31 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45  41.#define RTREE
381f6 5f 4c 45 20 30 78 34 32 0a 23 64 65 66 69 6e 65  _LE 0x42.#define
381f7 20 52 54 52 45 45 5f 4c 54 20 30 78 34 33 0a 23   RTREE_LT 0x43.#
381f8 64 65 66 69 6e 65 20 52 54 52 45 45 5f 47 45 20  define RTREE_GE 
381f9 30 78 34 34 0a 23 64 65 66 69 6e 65 20 52 54 52  0x44.#define RTR
381fa 45 45 5f 47 54 20 30 78 34 35 0a 0a 2f 2a 20 0a  EE_GT 0x45../* .
381fb 2a 2a 20 41 6e 20 72 74 72 65 65 20 73 74 72 75  ** An rtree stru
381fc 63 74 75 72 65 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a  cture node..**.*
381fd 2a 20 44 61 74 61 20 66 6f 72 6d 61 74 20 28 52  * Data format (R
381fe 74 72 65 65 4e 6f 64 65 2e 7a 44 61 74 61 29 3a  treeNode.zData):
381ff 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 49 66 20 74  .**.**   1. If t
38200 68 65 20 6e 6f 64 65 20 69 73 20 74 68 65 20 72  he node is the r
38201 6f 6f 74 20 6e 6f 64 65 20 28 6e 6f 64 65 20 31  oot node (node 1
38202 29 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72 73  ), then the firs
38203 74 20 32 20 62 79 74 65 73 0a 2a 2a 20 20 20 20  t 2 bytes.**    
38204 20 20 6f 66 20 74 68 65 20 6e 6f 64 65 20 63 6f    of the node co
38205 6e 74 61 69 6e 20 74 68 65 20 74 72 65 65 20 64  ntain the tree d
38206 65 70 74 68 20 61 73 20 61 20 62 69 67 2d 65 6e  epth as a big-en
38207 64 69 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a  dian integer..**
38208 20 20 20 20 20 20 46 6f 72 20 6e 6f 6e 2d 72 6f        For non-ro
38209 6f 74 20 6e 6f 64 65 73 2c 20 74 68 65 20 66 69  ot nodes, the fi
3820a 72 73 74 20 32 20 62 79 74 65 73 20 61 72 65 20  rst 2 bytes are 
3820b 6c 65 66 74 20 75 6e 75 73 65 64 2e 0a 2a 2a 0a  left unused..**.
3820c 2a 2a 20 20 20 32 2e 20 54 68 65 20 6e 65 78 74  **   2. The next
3820d 20 32 20 62 79 74 65 73 20 63 6f 6e 74 61 69 6e   2 bytes contain
3820e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
3820f 6e 74 72 69 65 73 20 63 75 72 72 65 6e 74 6c 79  ntries currently
38210 20 0a 2a 2a 20 20 20 20 20 20 73 74 6f 72 65 64   .**      stored
38211 20 69 6e 20 74 68 65 20 6e 6f 64 65 2e 0a 2a 2a   in the node..**
38212 0a 2a 2a 20 20 20 33 2e 20 54 68 65 20 72 65 6d  .**   3. The rem
38213 61 69 6e 64 65 72 20 6f 66 20 74 68 65 20 6e 6f  ainder of the no
38214 64 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  de contains the 
38215 6e 6f 64 65 20 65 6e 74 72 69 65 73 2e 20 45 61  node entries. Ea
38216 63 68 20 65 6e 74 72 79 0a 2a 2a 20 20 20 20 20  ch entry.**     
38217 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 73   consists of a s
38218 69 6e 67 6c 65 20 38 2d 62 79 74 65 20 69 6e 74  ingle 8-byte int
38219 65 67 65 72 20 66 6f 6c 6c 6f 77 65 64 20 62 79  eger followed by
3821a 20 61 6e 20 65 76 65 6e 20 6e 75 6d 62 65 72 0a   an even number.
3821b 2a 2a 20 20 20 20 20 20 6f 66 20 34 2d 62 79 74  **      of 4-byt
3821c 65 20 63 6f 6f 72 64 69 6e 61 74 65 73 2e 20 46  e coordinates. F
3821d 6f 72 20 6c 65 61 66 20 6e 6f 64 65 73 20 74 68  or leaf nodes th
3821e 65 20 69 6e 74 65 67 65 72 20 69 73 20 74 68 65  e integer is the
3821f 20 72 6f 77 69 64 0a 2a 2a 20 20 20 20 20 20 6f   rowid.**      o
38220 66 20 61 20 72 65 63 6f 72 64 2e 20 46 6f 72 20  f a record. For 
38221 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 73 20 69  internal nodes i
38222 74 20 69 73 20 74 68 65 20 6e 6f 64 65 20 6e 75  t is the node nu
38223 6d 62 65 72 20 6f 66 20 61 0a 2a 2a 20 20 20 20  mber of a.**    
38224 20 20 63 68 69 6c 64 20 70 61 67 65 2e 0a 2a 2f    child page..*/
38225 0a 73 74 72 75 63 74 20 52 74 72 65 65 4e 6f 64  .struct RtreeNod
38226 65 20 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  e {.  RtreeNode 
38227 2a 70 50 61 72 65 6e 74 3b 20 20 20 20 20 20 20  *pParent;       
38228 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e          /* Paren
38229 74 20 6e 6f 64 65 20 2a 2f 0a 20 20 69 36 34 20  t node */.  i64 
3822a 69 4e 6f 64 65 3b 0a 20 20 69 6e 74 20 6e 52 65  iNode;.  int nRe
3822b 66 3b 0a 20 20 69 6e 74 20 69 73 44 69 72 74 79  f;.  int isDirty
3822c 3b 0a 20 20 75 38 20 2a 7a 44 61 74 61 3b 0a 20  ;.  u8 *zData;. 
3822d 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 65 78   RtreeNode *pNex
3822e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
3822f 20 20 20 2f 2a 20 4e 65 78 74 20 6e 6f 64 65 20     /* Next node 
38230 69 6e 20 74 68 69 73 20 68 61 73 68 20 63 68 61  in this hash cha
38231 69 6e 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65  in */.};.#define
38232 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 20 72 65   NCELL(pNode) re
38233 61 64 49 6e 74 31 36 28 26 28 70 4e 6f 64 65 29  adInt16(&(pNode)
38234 2d 3e 7a 44 61 74 61 5b 32 5d 29 0a 0a 2f 2a 20  ->zData[2])../* 
38235 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 20 74 6f  .** Structure to
38236 20 73 74 6f 72 65 20 61 20 64 65 73 65 72 69 61   store a deseria
38237 6c 69 7a 65 64 20 72 74 72 65 65 20 72 65 63 6f  lized rtree reco
38238 72 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74  rd..*/.struct Rt
38239 72 65 65 43 65 6c 6c 20 7b 0a 20 20 69 36 34 20  reeCell {.  i64 
3823a 69 52 6f 77 69 64 3b 0a 20 20 52 74 72 65 65 43  iRowid;.  RtreeC
3823b 6f 6f 72 64 20 61 43 6f 6f 72 64 5b 52 54 52 45  oord aCoord[RTRE
3823c 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53  E_MAX_DIMENSIONS
3823d 2a 32 5d 3b 0a 7d 3b 0a 0a 23 69 66 6e 64 65 66  *2];.};..#ifndef
3823e 20 4d 41 58 0a 23 20 64 65 66 69 6e 65 20 4d 41   MAX.# define MA
3823f 58 28 78 2c 79 29 20 28 28 78 29 20 3c 20 28 79  X(x,y) ((x) < (y
38240 29 20 3f 20 28 79 29 20 3a 20 28 78 29 29 0a 23  ) ? (y) : (x)).#
38241 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4d 49  endif.#ifndef MI
38242 4e 0a 23 20 64 65 66 69 6e 65 20 4d 49 4e 28 78  N.# define MIN(x
38243 2c 79 29 20 28 28 78 29 20 3e 20 28 79 29 20 3f  ,y) ((x) > (y) ?
38244 20 28 79 29 20 3a 20 28 78 29 29 0a 23 65 6e 64   (y) : (x)).#end
38245 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69  if../*.** Functi
38246 6f 6e 73 20 74 6f 20 64 65 73 65 72 69 61 6c 69  ons to deseriali
38247 7a 65 20 61 20 31 36 20 62 69 74 20 69 6e 74 65  ze a 16 bit inte
38248 67 65 72 2c 20 33 32 20 62 69 74 20 72 65 61 6c  ger, 32 bit real
38249 20 6e 75 6d 62 65 72 20 61 6e 64 0a 2a 2a 20 36   number and.** 6
3824a 34 20 62 69 74 20 69 6e 74 65 67 65 72 2e 20 54  4 bit integer. T
3824b 68 65 20 64 65 73 65 72 69 61 6c 69 7a 65 64 20  he deserialized 
3824c 76 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65  value is returne
3824d 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
3824e 20 72 65 61 64 49 6e 74 31 36 28 75 38 20 2a 70   readInt16(u8 *p
3824f 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 5b 30  ){.  return (p[0
38250 5d 3c 3c 38 29 20 2b 20 70 5b 31 5d 3b 0a 7d 0a  ]<<8) + p[1];.}.
38251 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 61 64  static void read
38252 43 6f 6f 72 64 28 75 38 20 2a 70 2c 20 52 74 72  Coord(u8 *p, Rtr
38253 65 65 43 6f 6f 72 64 20 2a 70 43 6f 6f 72 64 29  eeCoord *pCoord)
38254 7b 0a 20 20 75 33 32 20 69 20 3d 20 28 0a 20 20  {.  u32 i = (.  
38255 20 20 28 28 28 75 33 32 29 70 5b 30 5d 29 20 3c    (((u32)p[0]) <
38256 3c 20 32 34 29 20 2b 20 0a 20 20 20 20 28 28 28  < 24) + .    (((
38257 75 33 32 29 70 5b 31 5d 29 20 3c 3c 20 31 36 29  u32)p[1]) << 16)
38258 20 2b 20 0a 20 20 20 20 28 28 28 75 33 32 29 70   + .    (((u32)p
38259 5b 32 5d 29 20 3c 3c 20 20 38 29 20 2b 20 0a 20  [2]) <<  8) + . 
3825a 20 20 20 28 28 28 75 33 32 29 70 5b 33 5d 29 20     (((u32)p[3]) 
3825b 3c 3c 20 20 30 29 0a 20 20 29 3b 0a 20 20 2a 28  <<  0).  );.  *(
3825c 75 33 32 20 2a 29 70 43 6f 6f 72 64 20 3d 20 69  u32 *)pCoord = i
3825d 3b 0a 7d 0a 73 74 61 74 69 63 20 69 36 34 20 72  ;.}.static i64 r
3825e 65 61 64 49 6e 74 36 34 28 75 38 20 2a 70 29 7b  eadInt64(u8 *p){
3825f 0a 20 20 72 65 74 75 72 6e 20 28 0a 20 20 20 20  .  return (.    
38260 28 28 28 69 36 34 29 70 5b 30 5d 29 20 3c 3c 20  (((i64)p[0]) << 
38261 35 36 29 20 2b 20 0a 20 20 20 20 28 28 28 69 36  56) + .    (((i6
38262 34 29 70 5b 31 5d 29 20 3c 3c 20 34 38 29 20 2b  4)p[1]) << 48) +
38263 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 32   .    (((i64)p[2
38264 5d 29 20 3c 3c 20 34 30 29 20 2b 20 0a 20 20 20  ]) << 40) + .   
38265 20 28 28 28 69 36 34 29 70 5b 33 5d 29 20 3c 3c   (((i64)p[3]) <<
38266 20 33 32 29 20 2b 20 0a 20 20 20 20 28 28 28 69   32) + .    (((i
38267 36 34 29 70 5b 34 5d 29 20 3c 3c 20 32 34 29 20  64)p[4]) << 24) 
38268 2b 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b  + .    (((i64)p[
38269 35 5d 29 20 3c 3c 20 31 36 29 20 2b 20 0a 20 20  5]) << 16) + .  
3826a 20 20 28 28 28 69 36 34 29 70 5b 36 5d 29 20 3c    (((i64)p[6]) <
3826b 3c 20 20 38 29 20 2b 20 0a 20 20 20 20 28 28 28  <  8) + .    (((
3826c 69 36 34 29 70 5b 37 5d 29 20 3c 3c 20 20 30 29  i64)p[7]) <<  0)
3826d 0a 20 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  .  );.}../*.** F
3826e 75 6e 63 74 69 6f 6e 73 20 74 6f 20 73 65 72 69  unctions to seri
3826f 61 6c 69 7a 65 20 61 20 31 36 20 62 69 74 20 69  alize a 16 bit i
38270 6e 74 65 67 65 72 2c 20 33 32 20 62 69 74 20 72  nteger, 32 bit r
38271 65 61 6c 20 6e 75 6d 62 65 72 20 61 6e 64 0a 2a  eal number and.*
38272 2a 20 36 34 20 62 69 74 20 69 6e 74 65 67 65 72  * 64 bit integer
38273 2e 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75  . The value retu
38274 72 6e 65 64 20 69 73 20 74 68 65 20 6e 75 6d 62  rned is the numb
38275 65 72 20 6f 66 20 62 79 74 65 73 20 77 72 69 74  er of bytes writ
38276 74 65 6e 0a 2a 2a 20 74 6f 20 74 68 65 20 61 72  ten.** to the ar
38277 67 75 6d 65 6e 74 20 62 75 66 66 65 72 20 28 61  gument buffer (a
38278 6c 77 61 79 73 20 32 2c 20 34 20 61 6e 64 20 38  lways 2, 4 and 8
38279 20 72 65 73 70 65 63 74 69 76 65 6c 79 29 2e 0a   respectively)..
3827a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72  */.static int wr
3827b 69 74 65 49 6e 74 31 36 28 75 38 20 2a 70 2c 20  iteInt16(u8 *p, 
3827c 69 6e 74 20 69 29 7b 0a 20 20 70 5b 30 5d 20 3d  int i){.  p[0] =
3827d 20 28 69 3e 3e 20 38 29 26 30 78 46 46 3b 0a 20   (i>> 8)&0xFF;. 
3827e 20 70 5b 31 5d 20 3d 20 28 69 3e 3e 20 30 29 26   p[1] = (i>> 0)&
3827f 30 78 46 46 3b 0a 20 20 72 65 74 75 72 6e 20 32  0xFF;.  return 2
38280 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77  ;.}.static int w
38281 72 69 74 65 43 6f 6f 72 64 28 75 38 20 2a 70 2c  riteCoord(u8 *p,
38282 20 52 74 72 65 65 43 6f 6f 72 64 20 2a 70 43 6f   RtreeCoord *pCo
38283 6f 72 64 29 7b 0a 20 20 75 33 32 20 69 3b 0a 20  ord){.  u32 i;. 
38284 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28   assert( sizeof(
38285 52 74 72 65 65 43 6f 6f 72 64 29 3d 3d 34 20 29  RtreeCoord)==4 )
38286 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65  ;.  assert( size
38287 6f 66 28 75 33 32 29 3d 3d 34 20 29 3b 0a 20 20  of(u32)==4 );.  
38288 69 20 3d 20 2a 28 75 33 32 20 2a 29 70 43 6f 6f  i = *(u32 *)pCoo
38289 72 64 3b 0a 20 20 70 5b 30 5d 20 3d 20 28 69 3e  rd;.  p[0] = (i>
3828a 3e 32 34 29 26 30 78 46 46 3b 0a 20 20 70 5b 31  >24)&0xFF;.  p[1
3828b 5d 20 3d 20 28 69 3e 3e 31 36 29 26 30 78 46 46  ] = (i>>16)&0xFF
3828c 3b 0a 20 20 70 5b 32 5d 20 3d 20 28 69 3e 3e 20  ;.  p[2] = (i>> 
3828d 38 29 26 30 78 46 46 3b 0a 20 20 70 5b 33 5d 20  8)&0xFF;.  p[3] 
3828e 3d 20 28 69 3e 3e 20 30 29 26 30 78 46 46 3b 0a  = (i>> 0)&0xFF;.
3828f 20 20 72 65 74 75 72 6e 20 34 3b 0a 7d 0a 73 74    return 4;.}.st
38290 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 49 6e  atic int writeIn
38291 74 36 34 28 75 38 20 2a 70 2c 20 69 36 34 20 69  t64(u8 *p, i64 i
38292 29 7b 0a 20 20 70 5b 30 5d 20 3d 20 28 69 3e 3e  ){.  p[0] = (i>>
38293 35 36 29 26 30 78 46 46 3b 0a 20 20 70 5b 31 5d  56)&0xFF;.  p[1]
38294 20 3d 20 28 69 3e 3e 34 38 29 26 30 78 46 46 3b   = (i>>48)&0xFF;
38295 0a 20 20 70 5b 32 5d 20 3d 20 28 69 3e 3e 34 30  .  p[2] = (i>>40
38296 29 26 30 78 46 46 3b 0a 20 20 70 5b 33 5d 20 3d  )&0xFF;.  p[3] =
38297 20 28 69 3e 3e 33 32 29 26 30 78 46 46 3b 0a 20   (i>>32)&0xFF;. 
38298 20 70 5b 34 5d 20 3d 20 28 69 3e 3e 32 34 29 26   p[4] = (i>>24)&
38299 30 78 46 46 3b 0a 20 20 70 5b 35 5d 20 3d 20 28  0xFF;.  p[5] = (
3829a 69 3e 3e 31 36 29 26 30 78 46 46 3b 0a 20 20 70  i>>16)&0xFF;.  p
3829b 5b 36 5d 20 3d 20 28 69 3e 3e 20 38 29 26 30 78  [6] = (i>> 8)&0x
3829c 46 46 3b 0a 20 20 70 5b 37 5d 20 3d 20 28 69 3e  FF;.  p[7] = (i>
3829d 3e 20 30 29 26 30 78 46 46 3b 0a 20 20 72 65 74  > 0)&0xFF;.  ret
3829e 75 72 6e 20 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 8;.}../*.** 
3829f 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65  Increment the re
382a0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 66  ference count of
382a1 20 6e 6f 64 65 20 70 2e 0a 2a 2f 0a 73 74 61 74   node p..*/.stat
382a2 69 63 20 76 6f 69 64 20 6e 6f 64 65 52 65 66 65  ic void nodeRefe
382a3 72 65 6e 63 65 28 52 74 72 65 65 4e 6f 64 65 20  rence(RtreeNode 
382a4 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a  *p){.  if( p ){.
382a5 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20      p->nRef++;. 
382a6 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61   }.}../*.** Clea
382a7 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  r the content of
382a8 20 6e 6f 64 65 20 70 20 28 73 65 74 20 61 6c 6c   node p (set all
382a9 20 62 79 74 65 73 20 74 6f 20 30 78 30 30 29 2e   bytes to 0x00).
382aa 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
382ab 6e 6f 64 65 5a 65 72 6f 28 52 74 72 65 65 20 2a  nodeZero(Rtree *
382ac 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64  pRtree, RtreeNod
382ad 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29  e *p){.  if( p )
382ae 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 70 2d  {.    memset(&p-
382af 3e 7a 44 61 74 61 5b 32 5d 2c 20 30 2c 20 70 52  >zData[2], 0, pR
382b0 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 2d  tree->iNodeSize-
382b1 32 29 3b 0a 20 20 20 20 70 2d 3e 69 73 44 69 72  2);.    p->isDir
382b2 74 79 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ty = 1;.  }.}../
382b3 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 6e 6f 64  *.** Given a nod
382b4 65 20 6e 75 6d 62 65 72 20 69 4e 6f 64 65 2c 20  e number iNode, 
382b5 72 65 74 75 72 6e 20 74 68 65 20 63 6f 72 72 65  return the corre
382b6 73 70 6f 6e 64 69 6e 67 20 6b 65 79 20 74 6f 20  sponding key to 
382b7 75 73 65 0a 2a 2a 20 69 6e 20 74 68 65 20 52 74  use.** in the Rt
382b8 72 65 65 2e 61 48 61 73 68 20 74 61 62 6c 65 2e  ree.aHash table.
382b9 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e  .*/.static int n
382ba 6f 64 65 48 61 73 68 28 69 36 34 20 69 4e 6f 64  odeHash(i64 iNod
382bb 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 0a 20  e){.  return (. 
382bc 20 20 20 28 69 4e 6f 64 65 3e 3e 35 36 29 20 5e     (iNode>>56) ^
382bd 20 28 69 4e 6f 64 65 3e 3e 34 38 29 20 5e 20 28   (iNode>>48) ^ (
382be 69 4e 6f 64 65 3e 3e 34 30 29 20 5e 20 28 69 4e  iNode>>40) ^ (iN
382bf 6f 64 65 3e 3e 33 32 29 20 5e 20 0a 20 20 20 20  ode>>32) ^ .    
382c0 28 69 4e 6f 64 65 3e 3e 32 34 29 20 5e 20 28 69  (iNode>>24) ^ (i
382c1 4e 6f 64 65 3e 3e 31 36 29 20 5e 20 28 69 4e 6f  Node>>16) ^ (iNo
382c2 64 65 3e 3e 20 38 29 20 5e 20 28 69 4e 6f 64 65  de>> 8) ^ (iNode
382c3 3e 3e 20 30 29 0a 20 20 29 20 25 20 48 41 53 48  >> 0).  ) % HASH
382c4 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  SIZE;.}../*.** S
382c5 65 61 72 63 68 20 74 68 65 20 6e 6f 64 65 20 68  earch the node h
382c6 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 6e 6f  ash table for no
382c7 64 65 20 69 4e 6f 64 65 2e 20 49 66 20 66 6f 75  de iNode. If fou
382c8 6e 64 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69  nd, return a poi
382c9 6e 74 65 72 0a 2a 2a 20 74 6f 20 69 74 2e 20 4f  nter.** to it. O
382ca 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e  therwise, return
382cb 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52 74   0..*/.static Rt
382cc 72 65 65 4e 6f 64 65 20 2a 6e 6f 64 65 48 61 73  reeNode *nodeHas
382cd 68 4c 6f 6f 6b 75 70 28 52 74 72 65 65 20 2a 70  hLookup(Rtree *p
382ce 52 74 72 65 65 2c 20 69 36 34 20 69 4e 6f 64 65  Rtree, i64 iNode
382cf 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  ){.  RtreeNode *
382d0 70 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4e 6f  p;.  assert( iNo
382d1 64 65 21 3d 30 20 29 3b 0a 20 20 66 6f 72 28 70  de!=0 );.  for(p
382d2 3d 70 52 74 72 65 65 2d 3e 61 48 61 73 68 5b 6e  =pRtree->aHash[n
382d3 6f 64 65 48 61 73 68 28 69 4e 6f 64 65 29 5d 3b  odeHash(iNode)];
382d4 20 70 20 26 26 20 70 2d 3e 69 4e 6f 64 65 21 3d   p && p->iNode!=
382d5 69 4e 6f 64 65 3b 20 70 3d 70 2d 3e 70 4e 65 78  iNode; p=p->pNex
382d6 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a  t);.  return p;.
382d7 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 6e 6f 64  }../*.** Add nod
382d8 65 20 70 4e 6f 64 65 20 74 6f 20 74 68 65 20 6e  e pNode to the n
382d9 6f 64 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a  ode hash table..
382da 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e  */.static void n
382db 6f 64 65 48 61 73 68 49 6e 73 65 72 74 28 52 74  odeHashInsert(Rt
382dc 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
382dd 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a  eeNode *pNode){.
382de 20 20 69 66 28 20 70 4e 6f 64 65 20 29 7b 0a 20    if( pNode ){. 
382df 20 20 20 69 6e 74 20 69 48 61 73 68 3b 0a 20 20     int iHash;.  
382e0 20 20 61 73 73 65 72 74 28 20 70 4e 6f 64 65 2d    assert( pNode-
382e1 3e 70 4e 65 78 74 3d 3d 30 20 29 3b 0a 20 20 20  >pNext==0 );.   
382e2 20 69 48 61 73 68 20 3d 20 6e 6f 64 65 48 61 73   iHash = nodeHas
382e3 68 28 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b  h(pNode->iNode);
382e4 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 70 4e 65 78  .    pNode->pNex
382e5 74 20 3d 20 70 52 74 72 65 65 2d 3e 61 48 61 73  t = pRtree->aHas
382e6 68 5b 69 48 61 73 68 5d 3b 0a 20 20 20 20 70 52  h[iHash];.    pR
382e7 74 72 65 65 2d 3e 61 48 61 73 68 5b 69 48 61 73  tree->aHash[iHas
382e8 68 5d 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 7d 0a  h] = pNode;.  }.
382e9 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20  }../*.** Remove 
382ea 6e 6f 64 65 20 70 4e 6f 64 65 20 66 72 6f 6d 20  node pNode from 
382eb 74 68 65 20 6e 6f 64 65 20 68 61 73 68 20 74 61  the node hash ta
382ec 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ble..*/.static v
382ed 6f 69 64 20 6e 6f 64 65 48 61 73 68 44 65 6c 65  oid nodeHashDele
382ee 74 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  te(Rtree *pRtree
382ef 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f  , RtreeNode *pNo
382f0 64 65 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65  de){.  RtreeNode
382f1 20 2a 2a 70 70 3b 0a 20 20 69 66 28 20 70 4e 6f   **pp;.  if( pNo
382f2 64 65 2d 3e 69 4e 6f 64 65 21 3d 30 20 29 7b 0a  de->iNode!=0 ){.
382f3 20 20 20 20 70 70 20 3d 20 26 70 52 74 72 65 65      pp = &pRtree
382f4 2d 3e 61 48 61 73 68 5b 6e 6f 64 65 48 61 73 68  ->aHash[nodeHash
382f5 28 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 5d 3b  (pNode->iNode)];
382f6 0a 20 20 20 20 66 6f 72 28 20 3b 20 28 2a 70 70  .    for( ; (*pp
382f7 29 21 3d 70 4e 6f 64 65 3b 20 70 70 20 3d 20 26  )!=pNode; pp = &
382f8 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 7b 20 61  (*pp)->pNext){ a
382f9 73 73 65 72 74 28 2a 70 70 29 3b 20 7d 0a 20 20  ssert(*pp); }.  
382fa 20 20 2a 70 70 20 3d 20 70 4e 6f 64 65 2d 3e 70    *pp = pNode->p
382fb 4e 65 78 74 3b 0a 20 20 20 20 70 4e 6f 64 65 2d  Next;.    pNode-
382fc 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 7d 0a  >pNext = 0;.  }.
382fd 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  }../*.** Allocat
382fe 65 20 61 6e 64 20 72 65 74 75 72 6e 20 6e 65 77  e and return new
382ff 20 72 2d 74 72 65 65 20 6e 6f 64 65 2e 20 49 6e   r-tree node. In
38300 69 74 69 61 6c 6c 79 2c 20 28 52 74 72 65 65 4e  itially, (RtreeN
38301 6f 64 65 2e 69 4e 6f 64 65 3d 3d 30 29 2c 0a 2a  ode.iNode==0),.*
38302 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61  * indicating tha
38303 74 20 6e 6f 64 65 20 68 61 73 20 6e 6f 74 20 79  t node has not y
38304 65 74 20 62 65 65 6e 20 61 73 73 69 67 6e 65 64  et been assigned
38305 20 61 20 6e 6f 64 65 20 6e 75 6d 62 65 72 2e 20   a node number. 
38306 49 74 20 69 73 0a 2a 2a 20 61 73 73 69 67 6e 65  It is.** assigne
38307 64 20 61 20 6e 6f 64 65 20 6e 75 6d 62 65 72 20  d a node number 
38308 77 68 65 6e 20 6e 6f 64 65 57 72 69 74 65 28 29  when nodeWrite()
38309 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 77 72   is called to wr
3830a 69 74 65 20 74 68 65 0a 2a 2a 20 6e 6f 64 65 20  ite the.** node 
3830b 63 6f 6e 74 65 6e 74 73 20 6f 75 74 20 74 6f 20  contents out to 
3830c 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f  the database..*/
3830d 0a 73 74 61 74 69 63 20 52 74 72 65 65 4e 6f 64  .static RtreeNod
3830e 65 20 2a 6e 6f 64 65 4e 65 77 28 52 74 72 65 65  e *nodeNew(Rtree
3830f 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e   *pRtree, RtreeN
38310 6f 64 65 20 2a 70 50 61 72 65 6e 74 2c 20 69 6e  ode *pParent, in
38311 74 20 7a 65 72 6f 29 7b 0a 20 20 52 74 72 65 65  t zero){.  Rtree
38312 4e 6f 64 65 20 2a 70 4e 6f 64 65 3b 0a 20 20 70  Node *pNode;.  p
38313 4e 6f 64 65 20 3d 20 28 52 74 72 65 65 4e 6f 64  Node = (RtreeNod
38314 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  e *)sqlite3_mall
38315 6f 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65 4e  oc(sizeof(RtreeN
38316 6f 64 65 29 20 2b 20 70 52 74 72 65 65 2d 3e 69  ode) + pRtree->i
38317 4e 6f 64 65 53 69 7a 65 29 3b 0a 20 20 69 66 28  NodeSize);.  if(
38318 20 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20 6d 65   pNode ){.    me
38319 6d 73 65 74 28 70 4e 6f 64 65 2c 20 30 2c 20 73  mset(pNode, 0, s
3831a 69 7a 65 6f 66 28 52 74 72 65 65 4e 6f 64 65 29  izeof(RtreeNode)
3831b 20 2b 20 28 7a 65 72 6f 3f 70 52 74 72 65 65 2d   + (zero?pRtree-
3831c 3e 69 4e 6f 64 65 53 69 7a 65 3a 30 29 29 3b 0a  >iNodeSize:0));.
3831d 20 20 20 20 70 4e 6f 64 65 2d 3e 7a 44 61 74 61      pNode->zData
3831e 20 3d 20 28 75 38 20 2a 29 26 70 4e 6f 64 65 5b   = (u8 *)&pNode[
3831f 31 5d 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 6e  1];.    pNode->n
38320 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 70 4e 6f  Ref = 1;.    pNo
38321 64 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 50  de->pParent = pP
38322 61 72 65 6e 74 3b 0a 20 20 20 20 70 4e 6f 64 65  arent;.    pNode
38323 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b 0a 20  ->isDirty = 1;. 
38324 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65     nodeReference
38325 28 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a 20  (pParent);.  }. 
38326 20 72 65 74 75 72 6e 20 70 4e 6f 64 65 3b 0a 7d   return pNode;.}
38327 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 61  ../*.** Obtain a
38328 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 6e   reference to an
38329 20 72 2d 74 72 65 65 20 6e 6f 64 65 2e 0a 2a 2f   r-tree node..*/
3832a 0a 73 74 61 74 69 63 20 69 6e 74 0a 6e 6f 64 65  .static int.node
3832b 41 63 71 75 69 72 65 28 0a 20 20 52 74 72 65 65  Acquire(.  Rtree
3832c 20 2a 70 52 74 72 65 65 2c 20 20 20 20 20 20 20   *pRtree,       
3832d 20 20 20 20 20 20 2f 2a 20 52 2d 74 72 65 65 20        /* R-tree 
3832e 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69  structure */.  i
3832f 36 34 20 69 4e 6f 64 65 2c 20 20 20 20 20 20 20  64 iNode,       
38330 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 64            /* Nod
38331 65 20 6e 75 6d 62 65 72 20 74 6f 20 6c 6f 61 64  e number to load
38332 20 2a 2f 0a 20 20 52 74 72 65 65 4e 6f 64 65 20   */.  RtreeNode 
38333 2a 70 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20  *pParent,       
38334 20 2f 2a 20 45 69 74 68 65 72 20 74 68 65 20 70   /* Either the p
38335 61 72 65 6e 74 20 6e 6f 64 65 20 6f 72 20 4e 55  arent node or NU
38336 4c 4c 20 2a 2f 0a 20 20 52 74 72 65 65 4e 6f 64  LL */.  RtreeNod
38337 65 20 2a 2a 70 70 4e 6f 64 65 20 20 20 20 20 20  e **ppNode      
38338 20 20 20 2f 2a 20 4f 55 54 3a 20 41 63 71 75 69     /* OUT: Acqui
38339 72 65 64 20 6e 6f 64 65 20 2a 2f 0a 29 7b 0a 20  red node */.){. 
3833a 20 69 6e 74 20 72 63 3b 0a 20 20 52 74 72 65 65   int rc;.  Rtree
3833b 4e 6f 64 65 20 2a 70 4e 6f 64 65 3b 0a 0a 20 20  Node *pNode;..  
3833c 2f 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65 20  /* Check if the 
3833d 72 65 71 75 65 73 74 65 64 20 6e 6f 64 65 20 69  requested node i
3833e 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  s already in the
3833f 20 68 61 73 68 20 74 61 62 6c 65 2e 20 49 66 20   hash table. If 
38340 73 6f 2c 0a 20 20 2a 2a 20 69 6e 63 72 65 61 73  so,.  ** increas
38341 65 20 69 74 73 20 72 65 66 65 72 65 6e 63 65 20  e its reference 
38342 63 6f 75 6e 74 20 61 6e 64 20 72 65 74 75 72 6e  count and return
38343 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20   it..  */.  if( 
38344 28 70 4e 6f 64 65 20 3d 20 6e 6f 64 65 48 61 73  (pNode = nodeHas
38345 68 4c 6f 6f 6b 75 70 28 70 52 74 72 65 65 2c 20  hLookup(pRtree, 
38346 69 4e 6f 64 65 29 29 20 29 7b 0a 20 20 20 20 61  iNode)) ){.    a
38347 73 73 65 72 74 28 20 21 70 50 61 72 65 6e 74 20  ssert( !pParent 
38348 7c 7c 20 21 70 4e 6f 64 65 2d 3e 70 50 61 72 65  || !pNode->pPare
38349 6e 74 20 7c 7c 20 70 4e 6f 64 65 2d 3e 70 50 61  nt || pNode->pPa
3834a 72 65 6e 74 3d 3d 70 50 61 72 65 6e 74 20 29 3b  rent==pParent );
3834b 0a 20 20 20 20 69 66 28 20 70 50 61 72 65 6e 74  .    if( pParent
3834c 20 26 26 20 21 70 4e 6f 64 65 2d 3e 70 50 61 72   && !pNode->pPar
3834d 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 6e 6f 64  ent ){.      nod
3834e 65 52 65 66 65 72 65 6e 63 65 28 70 50 61 72 65  eReference(pPare
3834f 6e 74 29 3b 0a 20 20 20 20 20 20 70 4e 6f 64 65  nt);.      pNode
38350 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 50 61 72  ->pParent = pPar
38351 65 6e 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ent;.    }.    p
38352 4e 6f 64 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20  Node->nRef++;.  
38353 20 20 2a 70 70 4e 6f 64 65 20 3d 20 70 4e 6f 64    *ppNode = pNod
38354 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  e;.    return SQ
38355 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
38356 70 4e 6f 64 65 20 3d 20 28 52 74 72 65 65 4e 6f  pNode = (RtreeNo
38357 64 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  de *)sqlite3_mal
38358 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65  loc(sizeof(Rtree
38359 4e 6f 64 65 29 20 2b 20 70 52 74 72 65 65 2d 3e  Node) + pRtree->
3835a 69 4e 6f 64 65 53 69 7a 65 29 3b 0a 20 20 69 66  iNodeSize);.  if
3835b 28 20 21 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20  ( !pNode ){.    
3835c 2a 70 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20  *ppNode = 0;.   
3835d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
3835e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 4e 6f 64  OMEM;.  }.  pNod
3835f 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 50 61  e->pParent = pPa
38360 72 65 6e 74 3b 0a 20 20 70 4e 6f 64 65 2d 3e 7a  rent;.  pNode->z
38361 44 61 74 61 20 3d 20 28 75 38 20 2a 29 26 70 4e  Data = (u8 *)&pN
38362 6f 64 65 5b 31 5d 3b 0a 20 20 70 4e 6f 64 65 2d  ode[1];.  pNode-
38363 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70 4e 6f  >nRef = 1;.  pNo
38364 64 65 2d 3e 69 4e 6f 64 65 20 3d 20 69 4e 6f 64  de->iNode = iNod
38365 65 3b 0a 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69  e;.  pNode->isDi
38366 72 74 79 20 3d 20 30 3b 0a 20 20 70 4e 6f 64 65  rty = 0;.  pNode
38367 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 0a 20 20  ->pNext = 0;..  
38368 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
38369 36 34 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64  64(pRtree->pRead
3836a 4e 6f 64 65 2c 20 31 2c 20 69 4e 6f 64 65 29 3b  Node, 1, iNode);
3836b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
3836c 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70 52 65  step(pRtree->pRe
3836d 61 64 4e 6f 64 65 29 3b 0a 20 20 69 66 28 20 72  adNode);.  if( r
3836e 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  c==SQLITE_ROW ){
3836f 0a 20 20 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a  .    const u8 *z
38370 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f 63  Blob = sqlite3_c
38371 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 52 74 72 65  olumn_blob(pRtre
38372 65 2d 3e 70 52 65 61 64 4e 6f 64 65 2c 20 30 29  e->pReadNode, 0)
38373 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 6f  ;.    memcpy(pNo
38374 64 65 2d 3e 7a 44 61 74 61 2c 20 7a 42 6c 6f 62  de->zData, zBlob
38375 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53  , pRtree->iNodeS
38376 69 7a 65 29 3b 0a 20 20 20 20 6e 6f 64 65 52 65  ize);.    nodeRe
38377 66 65 72 65 6e 63 65 28 70 50 61 72 65 6e 74 29  ference(pParent)
38378 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
38379 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 6f 64  qlite3_free(pNod
3837a 65 29 3b 0a 20 20 20 20 70 4e 6f 64 65 20 3d 20  e);.    pNode = 
3837b 30 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 4e 6f 64  0;.  }..  *ppNod
3837c 65 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 72 63 20  e = pNode;.  rc 
3837d 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  = sqlite3_reset(
3837e 70 52 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64  pRtree->pReadNod
3837f 65 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53  e);..  if( rc==S
38380 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4e 6f 64  QLITE_OK && iNod
38381 65 3d 3d 31 20 29 7b 0a 20 20 20 20 70 52 74 72  e==1 ){.    pRtr
38382 65 65 2d 3e 69 44 65 70 74 68 20 3d 20 72 65 61  ee->iDepth = rea
38383 64 49 6e 74 31 36 28 70 4e 6f 64 65 2d 3e 7a 44  dInt16(pNode->zD
38384 61 74 61 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  ata);.  }..  ass
38385 65 72 74 28 20 28 72 63 3d 3d 53 51 4c 49 54 45  ert( (rc==SQLITE
38386 5f 4f 4b 20 26 26 20 70 4e 6f 64 65 29 20 7c 7c  _OK && pNode) ||
38387 20 28 70 4e 6f 64 65 3d 3d 30 20 26 26 20 72 63   (pNode==0 && rc
38388 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 3b 0a  !=SQLITE_OK) );.
38389 20 20 6e 6f 64 65 48 61 73 68 49 6e 73 65 72 74    nodeHashInsert
3838a 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b  (pRtree, pNode);
3838b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
3838c 0a 0a 2f 2a 0a 2a 2a 20 4f 76 65 72 77 72 69 74  ../*.** Overwrit
3838d 65 20 63 65 6c 6c 20 69 43 65 6c 6c 20 6f 66 20  e cell iCell of 
3838e 6e 6f 64 65 20 70 4e 6f 64 65 20 77 69 74 68 20  node pNode with 
3838f 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
38390 70 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63  pCell..*/.static
38391 20 76 6f 69 64 20 6e 6f 64 65 4f 76 65 72 77 72   void nodeOverwr
38392 69 74 65 43 65 6c 6c 28 0a 20 20 52 74 72 65 65  iteCell(.  Rtree
38393 20 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72   *pRtree, .  Rtr
38394 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 20  eeNode *pNode,  
38395 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43  .  RtreeCell *pC
38396 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c  ell, .  int iCel
38397 6c 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  l.){.  int ii;. 
38398 20 75 38 20 2a 70 20 3d 20 26 70 4e 6f 64 65 2d   u8 *p = &pNode-
38399 3e 7a 44 61 74 61 5b 34 20 2b 20 70 52 74 72 65  >zData[4 + pRtre
3839a 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c  e->nBytesPerCell
3839b 2a 69 43 65 6c 6c 5d 3b 0a 20 20 70 20 2b 3d 20  *iCell];.  p += 
3839c 77 72 69 74 65 49 6e 74 36 34 28 70 2c 20 70 43  writeInt64(p, pC
3839d 65 6c 6c 2d 3e 69 52 6f 77 69 64 29 3b 0a 20 20  ell->iRowid);.  
3839e 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52  for(ii=0; ii<(pR
3839f 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69  tree->nDim*2); i
383a0 69 2b 2b 29 7b 0a 20 20 20 20 70 20 2b 3d 20 77  i++){.    p += w
383a1 72 69 74 65 43 6f 6f 72 64 28 70 2c 20 26 70 43  riteCoord(p, &pC
383a2 65 6c 6c 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 29  ell->aCoord[ii])
383a3 3b 0a 20 20 7d 0a 20 20 70 4e 6f 64 65 2d 3e 69  ;.  }.  pNode->i
383a4 73 44 69 72 74 79 20 3d 20 31 3b 0a 7d 0a 0a 2f  sDirty = 1;.}../
383a5 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 63 65 6c 6c  *.** Remove cell
383a6 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68 20 69   the cell with i
383a7 6e 64 65 78 20 69 43 65 6c 6c 20 66 72 6f 6d 20  ndex iCell from 
383a8 6e 6f 64 65 20 70 4e 6f 64 65 2e 0a 2a 2f 0a 73  node pNode..*/.s
383a9 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 44  tatic void nodeD
383aa 65 6c 65 74 65 43 65 6c 6c 28 52 74 72 65 65 20  eleteCell(Rtree 
383ab 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f  *pRtree, RtreeNo
383ac 64 65 20 2a 70 4e 6f 64 65 2c 20 69 6e 74 20 69  de *pNode, int i
383ad 43 65 6c 6c 29 7b 0a 20 20 75 38 20 2a 70 44 73  Cell){.  u8 *pDs
383ae 74 20 3d 20 26 70 4e 6f 64 65 2d 3e 7a 44 61 74  t = &pNode->zDat
383af 61 5b 34 20 2b 20 70 52 74 72 65 65 2d 3e 6e 42  a[4 + pRtree->nB
383b0 79 74 65 73 50 65 72 43 65 6c 6c 2a 69 43 65 6c  ytesPerCell*iCel
383b1 6c 5d 3b 0a 20 20 75 38 20 2a 70 53 72 63 20 3d  l];.  u8 *pSrc =
383b2 20 26 70 44 73 74 5b 70 52 74 72 65 65 2d 3e 6e   &pDst[pRtree->n
383b3 42 79 74 65 73 50 65 72 43 65 6c 6c 5d 3b 0a 20  BytesPerCell];. 
383b4 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 4e 43   int nByte = (NC
383b5 45 4c 4c 28 70 4e 6f 64 65 29 20 2d 20 69 43 65  ELL(pNode) - iCe
383b6 6c 6c 20 2d 20 31 29 20 2a 20 70 52 74 72 65 65  ll - 1) * pRtree
383b7 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 3b  ->nBytesPerCell;
383b8 0a 20 20 6d 65 6d 6d 6f 76 65 28 70 44 73 74 2c  .  memmove(pDst,
383b9 20 70 53 72 63 2c 20 6e 42 79 74 65 29 3b 0a 20   pSrc, nByte);. 
383ba 20 77 72 69 74 65 49 6e 74 31 36 28 26 70 4e 6f   writeInt16(&pNo
383bb 64 65 2d 3e 7a 44 61 74 61 5b 32 5d 2c 20 4e 43  de->zData[2], NC
383bc 45 4c 4c 28 70 4e 6f 64 65 29 2d 31 29 3b 0a 20  ELL(pNode)-1);. 
383bd 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20   pNode->isDirty 
383be 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  = 1;.}../*.** In
383bf 73 65 72 74 20 74 68 65 20 63 6f 6e 74 65 6e 74  sert the content
383c0 73 20 6f 66 20 63 65 6c 6c 20 70 43 65 6c 6c 20  s of cell pCell 
383c1 69 6e 74 6f 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  into node pNode.
383c2 20 49 66 20 74 68 65 20 69 6e 73 65 72 74 0a 2a   If the insert.*
383c3 2a 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c  * is successful,
383c4 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
383c5 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72  K..**.** If ther
383c6 65 20 69 73 20 6e 6f 74 20 65 6e 6f 75 67 68 20  e is not enough 
383c7 66 72 65 65 20 73 70 61 63 65 20 69 6e 20 70 4e  free space in pN
383c8 6f 64 65 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  ode, return SQLI
383c9 54 45 5f 46 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74  TE_FULL..*/.stat
383ca 69 63 20 69 6e 74 0a 6e 6f 64 65 49 6e 73 65 72  ic int.nodeInser
383cb 74 43 65 6c 6c 28 0a 20 20 52 74 72 65 65 20 2a  tCell(.  Rtree *
383cc 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65  pRtree, .  Rtree
383cd 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20  Node *pNode, .  
383ce 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c  RtreeCell *pCell
383cf 20 0a 29 7b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c   .){.  int nCell
383d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
383d1 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
383d2 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20  number of cells 
383d3 69 6e 20 70 4e 6f 64 65 20 2a 2f 0a 20 20 69 6e  in pNode */.  in
383d4 74 20 6e 4d 61 78 43 65 6c 6c 3b 20 20 20 20 20  t nMaxCell;     
383d5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
383d6 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  aximum number of
383d7 20 63 65 6c 6c 73 20 66 6f 72 20 70 4e 6f 64 65   cells for pNode
383d8 20 2a 2f 0a 0a 20 20 6e 4d 61 78 43 65 6c 6c 20   */..  nMaxCell 
383d9 3d 20 28 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65  = (pRtree->iNode
383da 53 69 7a 65 2d 34 29 2f 70 52 74 72 65 65 2d 3e  Size-4)/pRtree->
383db 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 3b 0a 20  nBytesPerCell;. 
383dc 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70   nCell = NCELL(p
383dd 4e 6f 64 65 29 3b 0a 0a 20 20 61 73 73 65 72 74  Node);..  assert
383de 28 6e 43 65 6c 6c 3c 3d 6e 4d 61 78 43 65 6c 6c  (nCell<=nMaxCell
383df 29 3b 0a 0a 20 20 69 66 28 20 6e 43 65 6c 6c 3c  );..  if( nCell<
383e0 6e 4d 61 78 43 65 6c 6c 20 29 7b 0a 20 20 20 20  nMaxCell ){.    
383e1 6e 6f 64 65 4f 76 65 72 77 72 69 74 65 43 65 6c  nodeOverwriteCel
383e2 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c  l(pRtree, pNode,
383e3 20 70 43 65 6c 6c 2c 20 6e 43 65 6c 6c 29 3b 0a   pCell, nCell);.
383e4 20 20 20 20 77 72 69 74 65 49 6e 74 31 36 28 26      writeInt16(&
383e5 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 32 5d 2c  pNode->zData[2],
383e6 20 6e 43 65 6c 6c 2b 31 29 3b 0a 20 20 20 20 70   nCell+1);.    p
383e7 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20  Node->isDirty = 
383e8 31 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  1;.  }..  return
383e9 20 28 6e 43 65 6c 6c 3d 3d 6e 4d 61 78 43 65 6c   (nCell==nMaxCel
383ea 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  l);.}../*.** If 
383eb 74 68 65 20 6e 6f 64 65 20 69 73 20 64 69 72 74  the node is dirt
383ec 79 2c 20 77 72 69 74 65 20 69 74 20 6f 75 74 20  y, write it out 
383ed 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  to the database.
383ee 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 0a 6e  .*/.static int.n
383ef 6f 64 65 57 72 69 74 65 28 52 74 72 65 65 20 2a  odeWrite(Rtree *
383f0 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64  pRtree, RtreeNod
383f1 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 69 6e 74  e *pNode){.  int
383f2 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
383f3 0a 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 73  .  if( pNode->is
383f4 44 69 72 74 79 20 29 7b 0a 20 20 20 20 73 71 6c  Dirty ){.    sql
383f5 69 74 65 33 5f 73 74 6d 74 20 2a 70 20 3d 20 70  ite3_stmt *p = p
383f6 52 74 72 65 65 2d 3e 70 57 72 69 74 65 4e 6f 64  Rtree->pWriteNod
383f7 65 3b 0a 20 20 20 20 69 66 28 20 70 4e 6f 64 65  e;.    if( pNode
383f8 2d 3e 69 4e 6f 64 65 20 29 7b 0a 20 20 20 20 20  ->iNode ){.     
383f9 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
383fa 74 36 34 28 70 2c 20 31 2c 20 70 4e 6f 64 65 2d  t64(p, 1, pNode-
383fb 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 65 6c  >iNode);.    }el
383fc 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
383fd 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 2c 20 31  3_bind_null(p, 1
383fe 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
383ff 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 70  ite3_bind_blob(p
38400 2c 20 32 2c 20 70 4e 6f 64 65 2d 3e 7a 44 61 74  , 2, pNode->zDat
38401 61 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65  a, pRtree->iNode
38402 53 69 7a 65 2c 20 53 51 4c 49 54 45 5f 53 54 41  Size, SQLITE_STA
38403 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  TIC);.    sqlite
38404 33 5f 73 74 65 70 28 70 29 3b 0a 20 20 20 20 70  3_step(p);.    p
38405 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20  Node->isDirty = 
38406 30 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  0;.    rc = sqli
38407 74 65 33 5f 72 65 73 65 74 28 70 29 3b 0a 20 20  te3_reset(p);.  
38408 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f    if( pNode->iNo
38409 64 65 3d 3d 30 20 26 26 20 72 63 3d 3d 53 51 4c  de==0 && rc==SQL
3840a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
3840b 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 3d 20 73  pNode->iNode = s
3840c 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
3840d 72 74 5f 72 6f 77 69 64 28 70 52 74 72 65 65 2d  rt_rowid(pRtree-
3840e 3e 64 62 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65  >db);.      node
3840f 48 61 73 68 49 6e 73 65 72 74 28 70 52 74 72 65  HashInsert(pRtre
38410 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 7d  e, pNode);.    }
38411 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
38412 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61  ;.}../*.** Relea
38413 73 65 20 61 20 72 65 66 65 72 65 6e 63 65 20 74  se a reference t
38414 6f 20 61 20 6e 6f 64 65 2e 20 49 66 20 74 68 65  o a node. If the
38415 20 6e 6f 64 65 20 69 73 20 64 69 72 74 79 20 61   node is dirty a
38416 6e 64 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  nd the reference
38417 0a 2a 2a 20 63 6f 75 6e 74 20 64 72 6f 70 73 20  .** count drops 
38418 74 6f 20 7a 65 72 6f 2c 20 74 68 65 20 6e 6f 64  to zero, the nod
38419 65 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65  e data is writte
3841a 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  n to the databas
3841b 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
3841c 0a 6e 6f 64 65 52 65 6c 65 61 73 65 28 52 74 72  .nodeRelease(Rtr
3841d 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
3841e 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a 20  eNode *pNode){. 
3841f 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
38420 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 4e 6f 64 65  _OK;.  if( pNode
38421 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
38422 70 4e 6f 64 65 2d 3e 6e 52 65 66 3e 30 20 29 3b  pNode->nRef>0 );
38423 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66  .    pNode->nRef
38424 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 4e 6f 64  --;.    if( pNod
38425 65 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20  e->nRef==0 ){.  
38426 20 20 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69      if( pNode->i
38427 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20 20 20 20 20  Node==1 ){.     
38428 20 20 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74     pRtree->iDept
38429 68 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a  h = -1;.      }.
3842a 20 20 20 20 20 20 69 66 28 20 70 4e 6f 64 65 2d        if( pNode-
3842b 3e 70 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20  >pParent ){.    
3842c 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 52 65 6c      rc = nodeRel
3842d 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 4e 6f  ease(pRtree, pNo
3842e 64 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20  de->pParent);.  
3842f 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
38430 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
38431 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 6e 6f  .        rc = no
38432 64 65 57 72 69 74 65 28 70 52 74 72 65 65 2c 20  deWrite(pRtree, 
38433 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 7d 0a  pNode);.      }.
38434 20 20 20 20 20 20 6e 6f 64 65 48 61 73 68 44 65        nodeHashDe
38435 6c 65 74 65 28 70 52 74 72 65 65 2c 20 70 4e 6f  lete(pRtree, pNo
38436 64 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  de);.      sqlit
38437 65 33 5f 66 72 65 65 28 70 4e 6f 64 65 29 3b 0a  e3_free(pNode);.
38438 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
38439 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
3843a 52 65 74 75 72 6e 20 74 68 65 20 36 34 2d 62 69  Return the 64-bi
3843b 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  t integer value 
3843c 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
3843d 63 65 6c 6c 20 69 43 65 6c 6c 20 6f 66 0a 2a 2a  cell iCell of.**
3843e 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 49 66 20   node pNode. If 
3843f 70 4e 6f 64 65 20 69 73 20 61 20 6c 65 61 66 20  pNode is a leaf 
38440 6e 6f 64 65 2c 20 74 68 69 73 20 69 73 20 61 20  node, this is a 
38441 72 6f 77 69 64 2e 20 49 66 20 69 74 20 69 73 0a  rowid. If it is.
38442 2a 2a 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 6e  ** an internal n
38443 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 20 36 34  ode, then the 64
38444 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 73 20  -bit integer is 
38445 61 20 63 68 69 6c 64 20 70 61 67 65 20 6e 75 6d  a child page num
38446 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ber..*/.static i
38447 36 34 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28  64 nodeGetRowid(
38448 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
38449 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  , .  RtreeNode *
3844a 70 4e 6f 64 65 2c 20 0a 20 20 69 6e 74 20 69 43  pNode, .  int iC
3844b 65 6c 6c 0a 29 7b 0a 20 20 61 73 73 65 72 74 28  ell.){.  assert(
3844c 20 69 43 65 6c 6c 3c 4e 43 45 4c 4c 28 70 4e 6f   iCell<NCELL(pNo
3844d 64 65 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  de) );.  return 
3844e 72 65 61 64 49 6e 74 36 34 28 26 70 4e 6f 64 65  readInt64(&pNode
3844f 2d 3e 7a 44 61 74 61 5b 34 20 2b 20 70 52 74 72  ->zData[4 + pRtr
38450 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c  ee->nBytesPerCel
38451 6c 2a 69 43 65 6c 6c 5d 29 3b 0a 7d 0a 0a 2f 2a  l*iCell]);.}../*
38452 0a 2a 2a 20 52 65 74 75 72 6e 20 63 6f 6f 72 64  .** Return coord
38453 69 6e 61 74 65 20 69 43 6f 6f 72 64 20 66 72 6f  inate iCoord fro
38454 6d 20 63 65 6c 6c 20 69 43 65 6c 6c 20 69 6e 20  m cell iCell in 
38455 6e 6f 64 65 20 70 4e 6f 64 65 2e 0a 2a 2f 0a 73  node pNode..*/.s
38456 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 47  tatic void nodeG
38457 65 74 43 6f 6f 72 64 28 0a 20 20 52 74 72 65 65  etCoord(.  Rtree
38458 20 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72   *pRtree, .  Rtr
38459 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a  eeNode *pNode, .
3845a 20 20 69 6e 74 20 69 43 65 6c 6c 2c 0a 20 20 69    int iCell,.  i
3845b 6e 74 20 69 43 6f 6f 72 64 2c 0a 20 20 52 74 72  nt iCoord,.  Rtr
3845c 65 65 43 6f 6f 72 64 20 2a 70 43 6f 6f 72 64 20  eeCoord *pCoord 
3845d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61            /* Spa
3845e 63 65 20 74 6f 20 77 72 69 74 65 20 72 65 73 75  ce to write resu
3845f 6c 74 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 72 65  lt to */.){.  re
38460 61 64 43 6f 6f 72 64 28 26 70 4e 6f 64 65 2d 3e  adCoord(&pNode->
38461 7a 44 61 74 61 5b 31 32 20 2b 20 70 52 74 72 65  zData[12 + pRtre
38462 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c  e->nBytesPerCell
38463 2a 69 43 65 6c 6c 20 2b 20 34 2a 69 43 6f 6f 72  *iCell + 4*iCoor
38464 64 5d 2c 20 70 43 6f 6f 72 64 29 3b 0a 7d 0a 0a  d], pCoord);.}..
38465 2f 2a 0a 2a 2a 20 44 65 73 65 72 69 61 6c 69 7a  /*.** Deserializ
38466 65 20 63 65 6c 6c 20 69 43 65 6c 6c 20 6f 66 20  e cell iCell of 
38467 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 50 6f 70 75  node pNode. Popu
38468 6c 61 74 65 20 74 68 65 20 73 74 72 75 63 74 75  late the structu
38469 72 65 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f  re pointed.** to
3846a 20 62 79 20 70 43 65 6c 6c 20 77 69 74 68 20 74   by pCell with t
3846b 68 65 20 72 65 73 75 6c 74 73 2e 0a 2a 2f 0a 73  he results..*/.s
3846c 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 47  tatic void nodeG
3846d 65 74 43 65 6c 6c 28 0a 20 20 52 74 72 65 65 20  etCell(.  Rtree 
3846e 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65  *pRtree, .  Rtre
3846f 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20  eNode *pNode, . 
38470 20 69 6e 74 20 69 43 65 6c 6c 2c 0a 20 20 52 74   int iCell,.  Rt
38471 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 0a 29  reeCell *pCell.)
38472 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 70 43  {.  int ii;.  pC
38473 65 6c 6c 2d 3e 69 52 6f 77 69 64 20 3d 20 6e 6f  ell->iRowid = no
38474 64 65 47 65 74 52 6f 77 69 64 28 70 52 74 72 65  deGetRowid(pRtre
38475 65 2c 20 70 4e 6f 64 65 2c 20 69 43 65 6c 6c 29  e, pNode, iCell)
38476 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ;.  for(ii=0; ii
38477 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 3b  <pRtree->nDim*2;
38478 20 69 69 2b 2b 29 7b 0a 20 20 20 20 6e 6f 64 65   ii++){.    node
38479 47 65 74 43 6f 6f 72 64 28 70 52 74 72 65 65 2c  GetCoord(pRtree,
3847a 20 70 4e 6f 64 65 2c 20 69 43 65 6c 6c 2c 20 69   pNode, iCell, i
3847b 69 2c 20 26 70 43 65 6c 6c 2d 3e 61 43 6f 6f 72  i, &pCell->aCoor
3847c 64 5b 69 69 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a  d[ii]);.  }.}...
3847d 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61  /* Forward decla
3847e 72 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 66  ration for the f
3847f 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 64 6f 65  unction that doe
38480 73 20 74 68 65 20 77 6f 72 6b 20 6f 66 0a 2a 2a  s the work of.**
38481 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
38482 6c 65 20 6d 6f 64 75 6c 65 20 78 43 72 65 61 74  le module xCreat
38483 65 28 29 20 61 6e 64 20 78 43 6f 6e 6e 65 63 74  e() and xConnect
38484 28 29 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 73  () methods..*/.s
38485 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 49  tatic int rtreeI
38486 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  nit(.  sqlite3 *
38487 2c 20 76 6f 69 64 20 2a 2c 20 69 6e 74 2c 20 63  , void *, int, c
38488 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74  onst char *const
38489 2a 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  *, sqlite3_vtab 
3848a 2a 2a 2c 20 63 68 61 72 20 2a 2a 2c 20 69 6e 74  **, char **, int
3848b 0a 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65  .);../* .** Rtre
3848c 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
3848d 6d 6f 64 75 6c 65 20 78 43 72 65 61 74 65 20 6d  module xCreate m
3848e 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ethod..*/.static
3848f 20 69 6e 74 20 72 74 72 65 65 43 72 65 61 74 65   int rtreeCreate
38490 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
38491 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20  .  void *pAux,. 
38492 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74   int argc, const
38493 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67   char *const*arg
38494 76 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  v,.  sqlite3_vta
38495 62 20 2a 2a 70 70 56 74 61 62 2c 0a 20 20 63 68  b **ppVtab,.  ch
38496 61 72 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20 20  ar **pzErr.){.  
38497 72 65 74 75 72 6e 20 72 74 72 65 65 49 6e 69 74  return rtreeInit
38498 28 64 62 2c 20 70 41 75 78 2c 20 61 72 67 63 2c  (db, pAux, argc,
38499 20 61 72 67 76 2c 20 70 70 56 74 61 62 2c 20 70   argv, ppVtab, p
3849a 7a 45 72 72 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 20  zErr, 1);.}../* 
3849b 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61  .** Rtree virtua
3849c 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78  l table module x
3849d 43 6f 6e 6e 65 63 74 20 6d 65 74 68 6f 64 2e 0a  Connect method..
3849e 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74  */.static int rt
3849f 72 65 65 43 6f 6e 6e 65 63 74 28 0a 20 20 73 71  reeConnect(.  sq
384a0 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 76 6f 69  lite3 *db,.  voi
384a1 64 20 2a 70 41 75 78 2c 0a 20 20 69 6e 74 20 61  d *pAux,.  int a
384a2 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  rgc, const char 
384a3 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 73  *const*argv,.  s
384a4 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70  qlite3_vtab **pp
384a5 56 74 61 62 2c 0a 20 20 63 68 61 72 20 2a 2a 70  Vtab,.  char **p
384a6 7a 45 72 72 0a 29 7b 0a 20 20 72 65 74 75 72 6e  zErr.){.  return
384a7 20 72 74 72 65 65 49 6e 69 74 28 64 62 2c 20 70   rtreeInit(db, p
384a8 41 75 78 2c 20 61 72 67 63 2c 20 61 72 67 76 2c  Aux, argc, argv,
384a9 20 70 70 56 74 61 62 2c 20 70 7a 45 72 72 2c 20   ppVtab, pzErr, 
384aa 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63  0);.}../*.** Inc
384ab 72 65 6d 65 6e 74 20 74 68 65 20 72 2d 74 72 65  rement the r-tre
384ac 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
384ad 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
384ae 64 20 72 74 72 65 65 52 65 66 65 72 65 6e 63 65  d rtreeReference
384af 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 29 7b  (Rtree *pRtree){
384b0 0a 20 20 70 52 74 72 65 65 2d 3e 6e 42 75 73 79  .  pRtree->nBusy
384b1 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63  ++;.}../*.** Dec
384b2 72 65 6d 65 6e 74 20 74 68 65 20 72 2d 74 72 65  rement the r-tre
384b3 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
384b4 74 2e 20 57 68 65 6e 20 74 68 65 20 72 65 66 65  t. When the refe
384b5 72 65 6e 63 65 20 63 6f 75 6e 74 20 72 65 61 63  rence count reac
384b6 68 65 73 0a 2a 2a 20 7a 65 72 6f 20 74 68 65 20  hes.** zero the 
384b7 73 74 72 75 63 74 75 72 65 20 69 73 20 64 65 6c  structure is del
384b8 65 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  eted..*/.static 
384b9 76 6f 69 64 20 72 74 72 65 65 52 65 6c 65 61 73  void rtreeReleas
384ba 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 29  e(Rtree *pRtree)
384bb 7b 0a 20 20 70 52 74 72 65 65 2d 3e 6e 42 75 73  {.  pRtree->nBus
384bc 79 2d 2d 3b 0a 20 20 69 66 28 20 70 52 74 72 65  y--;.  if( pRtre
384bd 65 2d 3e 6e 42 75 73 79 3d 3d 30 20 29 7b 0a 20  e->nBusy==0 ){. 
384be 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c     sqlite3_final
384bf 69 7a 65 28 70 52 74 72 65 65 2d 3e 70 52 65 61  ize(pRtree->pRea
384c0 64 4e 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69  dNode);.    sqli
384c1 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74  te3_finalize(pRt
384c2 72 65 65 2d 3e 70 57 72 69 74 65 4e 6f 64 65 29  ree->pWriteNode)
384c3 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  ;.    sqlite3_fi
384c4 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70  nalize(pRtree->p
384c5 44 65 6c 65 74 65 4e 6f 64 65 29 3b 0a 20 20 20  DeleteNode);.   
384c6 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
384c7 65 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52  e(pRtree->pReadR
384c8 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  owid);.    sqlit
384c9 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72  e3_finalize(pRtr
384ca 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29  ee->pWriteRowid)
384cb 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  ;.    sqlite3_fi
384cc 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70  nalize(pRtree->p
384cd 44 65 6c 65 74 65 52 6f 77 69 64 29 3b 0a 20 20  DeleteRowid);.  
384ce 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69    sqlite3_finali
384cf 7a 65 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64  ze(pRtree->pRead
384d0 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 73 71 6c  Parent);.    sql
384d1 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52  ite3_finalize(pR
384d2 74 72 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65  tree->pWritePare
384d3 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  nt);.    sqlite3
384d4 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65  _finalize(pRtree
384d5 2d 3e 70 44 65 6c 65 74 65 50 61 72 65 6e 74 29  ->pDeleteParent)
384d6 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
384d7 65 65 28 70 52 74 72 65 65 29 3b 0a 20 20 7d 0a  ee(pRtree);.  }.
384d8 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20  }../* .** Rtree 
384d9 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
384da 64 75 6c 65 20 78 44 69 73 63 6f 6e 6e 65 63 74  dule xDisconnect
384db 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74   method..*/.stat
384dc 69 63 20 69 6e 74 20 72 74 72 65 65 44 69 73 63  ic int rtreeDisc
384dd 6f 6e 6e 65 63 74 28 73 71 6c 69 74 65 33 5f 76  onnect(sqlite3_v
384de 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 72  tab *pVtab){.  r
384df 74 72 65 65 52 65 6c 65 61 73 65 28 28 52 74 72  treeRelease((Rtr
384e0 65 65 20 2a 29 70 56 74 61 62 29 3b 0a 20 20 72  ee *)pVtab);.  r
384e1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
384e2 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65  .}../* .** Rtree
384e3 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
384e4 6f 64 75 6c 65 20 78 44 65 73 74 72 6f 79 20 6d  odule xDestroy m
384e5 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ethod..*/.static
384e6 20 69 6e 74 20 72 74 72 65 65 44 65 73 74 72 6f   int rtreeDestro
384e7 79 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  y(sqlite3_vtab *
384e8 70 56 74 61 62 29 7b 0a 20 20 52 74 72 65 65 20  pVtab){.  Rtree 
384e9 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65  *pRtree = (Rtree
384ea 20 2a 29 70 56 74 61 62 3b 0a 20 20 69 6e 74 20   *)pVtab;.  int 
384eb 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 43 72 65  rc;.  char *zCre
384ec 61 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  ate = sqlite3_mp
384ed 72 69 6e 74 66 28 0a 20 20 20 20 22 44 52 4f 50  rintf(.    "DROP
384ee 20 54 41 42 4c 45 20 27 25 71 27 2e 27 25 71 5f   TABLE '%q'.'%q_
384ef 6e 6f 64 65 27 3b 22 0a 20 20 20 20 22 44 52 4f  node';".    "DRO
384f0 50 20 54 41 42 4c 45 20 27 25 71 27 2e 27 25 71  P TABLE '%q'.'%q
384f1 5f 72 6f 77 69 64 27 3b 22 0a 20 20 20 20 22 44  _rowid';".    "D
384f2 52 4f 50 20 54 41 42 4c 45 20 27 25 71 27 2e 27  ROP TABLE '%q'.'
384f3 25 71 5f 70 61 72 65 6e 74 27 3b 22 2c 0a 20 20  %q_parent';",.  
384f4 20 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70    pRtree->zDb, p
384f5 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 0a 20  Rtree->zName, . 
384f6 20 20 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20     pRtree->zDb, 
384f7 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 0a 20  pRtree->zName,. 
384f8 20 20 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20     pRtree->zDb, 
384f9 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 0a 20 20  pRtree->zName.  
384fa 29 3b 0a 20 20 69 66 28 20 21 7a 43 72 65 61 74  );.  if( !zCreat
384fb 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  e ){.    rc = SQ
384fc 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65  LITE_NOMEM;.  }e
384fd 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  lse{.    rc = sq
384fe 6c 69 74 65 33 5f 65 78 65 63 28 70 52 74 72 65  lite3_exec(pRtre
384ff 65 2d 3e 64 62 2c 20 7a 43 72 65 61 74 65 2c 20  e->db, zCreate, 
38500 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  0, 0, 0);.    sq
38501 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 72 65 61  lite3_free(zCrea
38502 74 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  te);.  }.  if( r
38503 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
38504 20 20 20 20 72 74 72 65 65 52 65 6c 65 61 73 65      rtreeRelease
38505 28 70 52 74 72 65 65 29 3b 0a 20 20 7d 0a 0a 20  (pRtree);.  }.. 
38506 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
38507 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74  * .** Rtree virt
38508 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65  ual table module
38509 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 2e 0a 2a   xOpen method..*
3850a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72  /.static int rtr
3850b 65 65 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76  eeOpen(sqlite3_v
3850c 74 61 62 20 2a 70 56 54 61 62 2c 20 73 71 6c 69  tab *pVTab, sqli
3850d 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
3850e 2a 2a 70 70 43 75 72 73 6f 72 29 7b 0a 20 20 69  **ppCursor){.  i
3850f 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e  nt rc = SQLITE_N
38510 4f 4d 45 4d 3b 0a 20 20 52 74 72 65 65 43 75 72  OMEM;.  RtreeCur
38511 73 6f 72 20 2a 70 43 73 72 3b 0a 0a 20 20 70 43  sor *pCsr;..  pC
38512 73 72 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f  sr = (RtreeCurso
38513 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  r *)sqlite3_mall
38514 6f 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65 43  oc(sizeof(RtreeC
38515 75 72 73 6f 72 29 29 3b 0a 20 20 69 66 28 20 70  ursor));.  if( p
38516 43 73 72 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65  Csr ){.    memse
38517 74 28 70 43 73 72 2c 20 30 2c 20 73 69 7a 65 6f  t(pCsr, 0, sizeo
38518 66 28 52 74 72 65 65 43 75 72 73 6f 72 29 29 3b  f(RtreeCursor));
38519 0a 20 20 20 20 70 43 73 72 2d 3e 62 61 73 65 2e  .    pCsr->base.
3851a 70 56 74 61 62 20 3d 20 70 56 54 61 62 3b 0a 20  pVtab = pVTab;. 
3851b 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
3851c 4b 3b 0a 20 20 7d 0a 20 20 2a 70 70 43 75 72 73  K;.  }.  *ppCurs
3851d 6f 72 20 3d 20 28 73 71 6c 69 74 65 33 5f 76 74  or = (sqlite3_vt
3851e 61 62 5f 63 75 72 73 6f 72 20 2a 29 70 43 73 72  ab_cursor *)pCsr
3851f 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  ;..  return rc;.
38520 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20  }../* .** Rtree 
38521 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
38522 64 75 6c 65 20 78 43 6c 6f 73 65 20 6d 65 74 68  dule xClose meth
38523 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  od..*/.static in
38524 74 20 72 74 72 65 65 43 6c 6f 73 65 28 73 71 6c  t rtreeClose(sql
38525 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
38526 20 2a 63 75 72 29 7b 0a 20 20 52 74 72 65 65 20   *cur){.  Rtree 
38527 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65  *pRtree = (Rtree
38528 20 2a 29 28 63 75 72 2d 3e 70 56 74 61 62 29 3b   *)(cur->pVtab);
38529 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 52 74 72  .  int rc;.  Rtr
3852a 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d  eeCursor *pCsr =
3852b 20 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29   (RtreeCursor *)
3852c 63 75 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  cur;.  sqlite3_f
3852d 72 65 65 28 70 43 73 72 2d 3e 61 43 6f 6e 73 74  ree(pCsr->aConst
3852e 72 61 69 6e 74 29 3b 0a 20 20 72 63 20 3d 20 6e  raint);.  rc = n
3852f 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
38530 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b  e, pCsr->pNode);
38531 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
38532 70 43 73 72 29 3b 0a 20 20 72 65 74 75 72 6e 20  pCsr);.  return 
38533 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 74 72  rc;.}../*.** Rtr
38534 65 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ee virtual table
38535 20 6d 6f 64 75 6c 65 20 78 45 6f 66 20 6d 65 74   module xEof met
38536 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  hod..**.** Retur
38537 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68  n non-zero if th
38538 65 20 63 75 72 73 6f 72 20 64 6f 65 73 20 6e 6f  e cursor does no
38539 74 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  t currently poin
3853a 74 20 74 6f 20 61 20 76 61 6c 69 64 20 0a 2a 2a  t to a valid .**
3853b 20 72 65 63 6f 72 64 20 28 69 2e 65 20 69 66 20   record (i.e if 
3853c 74 68 65 20 73 63 61 6e 20 68 61 73 20 66 69 6e  the scan has fin
3853d 69 73 68 65 64 29 2c 20 6f 72 20 7a 65 72 6f 20  ished), or zero 
3853e 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74  otherwise..*/.st
3853f 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 45 6f  atic int rtreeEo
38540 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  f(sqlite3_vtab_c
38541 75 72 73 6f 72 20 2a 63 75 72 29 7b 0a 20 20 52  ursor *cur){.  R
38542 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72  treeCursor *pCsr
38543 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72 20   = (RtreeCursor 
38544 2a 29 63 75 72 3b 0a 20 20 72 65 74 75 72 6e 20  *)cur;.  return 
38545 28 70 43 73 72 2d 3e 70 4e 6f 64 65 3d 3d 30 29  (pCsr->pNode==0)
38546 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 43 75 72 73  ;.}../* .** Curs
38547 6f 72 20 70 43 75 72 73 6f 72 20 63 75 72 72 65  or pCursor curre
38548 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 61  ntly points to a
38549 20 63 65 6c 6c 20 69 6e 20 61 20 6e 6f 6e 2d 6c   cell in a non-l
3854a 65 61 66 20 70 61 67 65 2e 0a 2a 2a 20 52 65 74  eaf page..** Ret
3854b 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20  urn true if the 
3854c 73 75 62 2d 74 72 65 65 20 68 65 61 64 65 64 20  sub-tree headed 
3854d 62 79 20 74 68 65 20 63 65 6c 6c 20 69 73 20 66  by the cell is f
3854e 69 6c 74 65 72 65 64 0a 2a 2a 20 28 65 78 63 6c  iltered.** (excl
3854f 75 64 65 64 29 20 62 79 20 74 68 65 20 63 6f 6e  uded) by the con
38550 73 74 72 61 69 6e 74 73 20 69 6e 20 74 68 65 20  straints in the 
38551 70 43 75 72 73 6f 72 2d 3e 61 43 6f 6e 73 74 72  pCursor->aConstr
38552 61 69 6e 74 5b 5d 20 0a 2a 2a 20 61 72 72 61 79  aint[] .** array
38553 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 72  , or false other
38554 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  wise..*/.static 
38555 69 6e 74 20 74 65 73 74 52 74 72 65 65 43 65 6c  int testRtreeCel
38556 6c 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  l(Rtree *pRtree,
38557 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43   RtreeCursor *pC
38558 75 72 73 6f 72 29 7b 0a 20 20 52 74 72 65 65 43  ursor){.  RtreeC
38559 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 69 6e 74 20  ell cell;.  int 
3855a 69 69 3b 0a 20 20 69 6e 74 20 62 52 65 73 20 3d  ii;.  int bRes =
3855b 20 30 3b 0a 0a 20 20 6e 6f 64 65 47 65 74 43 65   0;..  nodeGetCe
3855c 6c 6c 28 70 52 74 72 65 65 2c 20 70 43 75 72 73  ll(pRtree, pCurs
3855d 6f 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 75 72 73  or->pNode, pCurs
3855e 6f 72 2d 3e 69 43 65 6c 6c 2c 20 26 63 65 6c 6c  or->iCell, &cell
3855f 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 62  );.  for(ii=0; b
38560 52 65 73 3d 3d 30 20 26 26 20 69 69 3c 70 43 75  Res==0 && ii<pCu
38561 72 73 6f 72 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e  rsor->nConstrain
38562 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74  t; ii++){.    Rt
38563 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 2a 70  reeConstraint *p
38564 20 3d 20 26 70 43 75 72 73 6f 72 2d 3e 61 43 6f   = &pCursor->aCo
38565 6e 73 74 72 61 69 6e 74 5b 69 69 5d 3b 0a 20 20  nstraint[ii];.  
38566 20 20 64 6f 75 62 6c 65 20 63 65 6c 6c 5f 6d 69    double cell_mi
38567 6e 20 3d 20 44 43 4f 4f 52 44 28 63 65 6c 6c 2e  n = DCOORD(cell.
38568 61 43 6f 6f 72 64 5b 28 70 2d 3e 69 43 6f 6f 72  aCoord[(p->iCoor
38569 64 3e 3e 31 29 2a 32 5d 29 3b 0a 20 20 20 20 64  d>>1)*2]);.    d
3856a 6f 75 62 6c 65 20 63 65 6c 6c 5f 6d 61 78 20 3d  ouble cell_max =
3856b 20 44 43 4f 4f 52 44 28 63 65 6c 6c 2e 61 43 6f   DCOORD(cell.aCo
3856c 6f 72 64 5b 28 70 2d 3e 69 43 6f 6f 72 64 3e 3e  ord[(p->iCoord>>
3856d 31 29 2a 32 2b 31 5d 29 3b 0a 0a 20 20 20 20 61  1)*2+1]);..    a
3856e 73 73 65 72 74 28 70 2d 3e 6f 70 3d 3d 52 54 52  ssert(p->op==RTR
3856f 45 45 5f 4c 45 20 7c 7c 20 70 2d 3e 6f 70 3d 3d  EE_LE || p->op==
38570 52 54 52 45 45 5f 4c 54 20 7c 7c 20 70 2d 3e 6f  RTREE_LT || p->o
38571 70 3d 3d 52 54 52 45 45 5f 47 45 20 0a 20 20 20  p==RTREE_GE .   
38572 20 20 20 20 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52       || p->op==R
38573 54 52 45 45 5f 47 54 20 7c 7c 20 70 2d 3e 6f 70  TREE_GT || p->op
38574 3d 3d 52 54 52 45 45 5f 45 51 0a 20 20 20 20 29  ==RTREE_EQ.    )
38575 3b 0a 0a 20 20 20 20 73 77 69 74 63 68 28 20 70  ;..    switch( p
38576 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 20 20 63 61  ->op ){.      ca
38577 73 65 20 52 54 52 45 45 5f 4c 45 3a 20 63 61 73  se RTREE_LE: cas
38578 65 20 52 54 52 45 45 5f 4c 54 3a 20 62 52 65 73  e RTREE_LT: bRes
38579 20 3d 20 70 2d 3e 72 56 61 6c 75 65 3c 63 65 6c   = p->rValue<cel
3857a 6c 5f 6d 69 6e 3b 20 62 72 65 61 6b 3b 0a 20 20  l_min; break;.  
3857b 20 20 20 20 63 61 73 65 20 52 54 52 45 45 5f 47      case RTREE_G
3857c 45 3a 20 63 61 73 65 20 52 54 52 45 45 5f 47 54  E: case RTREE_GT
3857d 3a 20 62 52 65 73 20 3d 20 70 2d 3e 72 56 61 6c  : bRes = p->rVal
3857e 75 65 3e 63 65 6c 6c 5f 6d 61 78 3b 20 62 72 65  ue>cell_max; bre
3857f 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 52  ak;.      case R
38580 54 52 45 45 5f 45 51 3a 20 0a 20 20 20 20 20 20  TREE_EQ: .      
38581 20 20 62 52 65 73 20 3d 20 28 70 2d 3e 72 56 61    bRes = (p->rVa
38582 6c 75 65 3e 63 65 6c 6c 5f 6d 61 78 20 7c 7c 20  lue>cell_max || 
38583 70 2d 3e 72 56 61 6c 75 65 3c 63 65 6c 6c 5f 6d  p->rValue<cell_m
38584 69 6e 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  in);.        bre
38585 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ak;.    }.  }.. 
38586 20 72 65 74 75 72 6e 20 62 52 65 73 3b 0a 7d 0a   return bRes;.}.
38587 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ./* .** Return t
38588 72 75 65 20 69 66 20 74 68 65 20 63 65 6c 6c 20  rue if the cell 
38589 74 68 61 74 20 63 75 72 73 6f 72 20 70 43 75 72  that cursor pCur
3858a 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f  sor currently po
3858b 69 6e 74 73 20 74 6f 0a 2a 2a 20 77 6f 75 6c 64  ints to.** would
3858c 20 62 65 20 66 69 6c 74 65 72 65 64 20 28 65 78   be filtered (ex
3858d 63 6c 75 64 65 64 29 20 62 79 20 74 68 65 20 63  cluded) by the c
3858e 6f 6e 73 74 72 61 69 6e 74 73 20 69 6e 20 74 68  onstraints in th
3858f 65 20 0a 2a 2a 20 70 43 75 72 73 6f 72 2d 3e 61  e .** pCursor->a
38590 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72  Constraint[] arr
38591 61 79 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68  ay, or false oth
38592 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  erwise..**.** Th
38593 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75  is function assu
38594 6d 65 73 20 74 68 61 74 20 74 68 65 20 63 65 6c  mes that the cel
38595 6c 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 6c  l is part of a l
38596 65 61 66 20 6e 6f 64 65 2e 0a 2a 2f 0a 73 74 61  eaf node..*/.sta
38597 74 69 63 20 69 6e 74 20 74 65 73 74 52 74 72 65  tic int testRtre
38598 65 45 6e 74 72 79 28 52 74 72 65 65 20 2a 70 52  eEntry(Rtree *pR
38599 74 72 65 65 2c 20 52 74 72 65 65 43 75 72 73 6f  tree, RtreeCurso
3859a 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 52  r *pCursor){.  R
3859b 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20  treeCell cell;. 
3859c 20 69 6e 74 20 69 69 3b 0a 0a 20 20 6e 6f 64 65   int ii;..  node
3859d 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  GetCell(pRtree, 
3859e 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20  pCursor->pNode, 
3859f 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 2c 20  pCursor->iCell, 
385a0 26 63 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 69  &cell);.  for(ii
385a1 3d 30 3b 20 69 69 3c 70 43 75 72 73 6f 72 2d 3e  =0; ii<pCursor->
385a2 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 69 2b  nConstraint; ii+
385a3 2b 29 7b 0a 20 20 20 20 52 74 72 65 65 43 6f 6e  +){.    RtreeCon
385a4 73 74 72 61 69 6e 74 20 2a 70 20 3d 20 26 70 43  straint *p = &pC
385a5 75 72 73 6f 72 2d 3e 61 43 6f 6e 73 74 72 61 69  ursor->aConstrai
385a6 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 64 6f 75 62  nt[ii];.    doub
385a7 6c 65 20 63 6f 6f 72 64 20 3d 20 44 43 4f 4f 52  le coord = DCOOR
385a8 44 28 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 70 2d  D(cell.aCoord[p-
385a9 3e 69 43 6f 6f 72 64 5d 29 3b 0a 20 20 20 20 69  >iCoord]);.    i
385aa 6e 74 20 72 65 73 3b 0a 20 20 20 20 61 73 73 65  nt res;.    asse
385ab 72 74 28 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f  rt(p->op==RTREE_
385ac 4c 45 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52  LE || p->op==RTR
385ad 45 45 5f 4c 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d  EE_LT || p->op==
385ae 52 54 52 45 45 5f 47 45 20 0a 20 20 20 20 20 20  RTREE_GE .      
385af 20 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45    || p->op==RTRE
385b0 45 5f 47 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52  E_GT || p->op==R
385b1 54 52 45 45 5f 45 51 0a 20 20 20 20 29 3b 0a 20  TREE_EQ.    );. 
385b2 20 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70     switch( p->op
385b3 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 52   ){.      case R
385b4 54 52 45 45 5f 4c 45 3a 20 72 65 73 20 3d 20 28  TREE_LE: res = (
385b5 63 6f 6f 72 64 3c 3d 70 2d 3e 72 56 61 6c 75 65  coord<=p->rValue
385b6 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  ); break;.      
385b7 63 61 73 65 20 52 54 52 45 45 5f 4c 54 3a 20 72  case RTREE_LT: r
385b8 65 73 20 3d 20 28 63 6f 6f 72 64 3c 70 2d 3e 72  es = (coord<p->r
385b9 56 61 6c 75 65 29 3b 20 20 62 72 65 61 6b 3b 0a  Value);  break;.
385ba 20 20 20 20 20 20 63 61 73 65 20 52 54 52 45 45        case RTREE
385bb 5f 47 45 3a 20 72 65 73 20 3d 20 28 63 6f 6f 72  _GE: res = (coor
385bc 64 3e 3d 70 2d 3e 72 56 61 6c 75 65 29 3b 20 62  d>=p->rValue); b
385bd 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
385be 20 52 54 52 45 45 5f 47 54 3a 20 72 65 73 20 3d   RTREE_GT: res =
385bf 20 28 63 6f 6f 72 64 3e 70 2d 3e 72 56 61 6c 75   (coord>p->rValu
385c0 65 29 3b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20  e);  break;.    
385c1 20 20 63 61 73 65 20 52 54 52 45 45 5f 45 51 3a    case RTREE_EQ:
385c2 20 72 65 73 20 3d 20 28 63 6f 6f 72 64 3d 3d 70   res = (coord==p
385c3 2d 3e 72 56 61 6c 75 65 29 3b 20 62 72 65 61 6b  ->rValue); break
385c4 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
385c5 20 21 72 65 73 20 29 20 72 65 74 75 72 6e 20 31   !res ) return 1
385c6 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
385c7 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 75 72 73  0;.}../*.** Curs
385c8 6f 72 20 70 43 75 72 73 6f 72 20 63 75 72 72 65  or pCursor curre
385c9 6e 74 6c 79 20 70 6f 69 6e 74 73 20 61 74 20 61  ntly points at a
385ca 20 6e 6f 64 65 20 74 68 61 74 20 68 65 61 64 73   node that heads
385cb 20 61 20 73 75 62 2d 74 72 65 65 20 6f 66 0a 2a   a sub-tree of.*
385cc 2a 20 68 65 69 67 68 74 20 69 48 65 69 67 68 74  * height iHeight
385cd 20 28 69 66 20 69 48 65 69 67 68 74 3d 3d 30 2c   (if iHeight==0,
385ce 20 74 68 65 6e 20 74 68 65 20 6e 6f 64 65 20 69   then the node i
385cf 73 20 61 20 6c 65 61 66 29 2e 20 44 65 73 63 65  s a leaf). Desce
385d0 6e 64 0a 2a 2a 20 74 6f 20 70 6f 69 6e 74 20 74  nd.** to point t
385d1 6f 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20  o the left-most 
385d2 63 65 6c 6c 20 6f 66 20 74 68 65 20 73 75 62 2d  cell of the sub-
385d3 74 72 65 65 20 74 68 61 74 20 6d 61 74 63 68 65  tree that matche
385d4 73 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 66 69 67  s the .** config
385d5 75 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 73  ured constraints
385d6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
385d7 64 65 73 63 65 6e 64 54 6f 43 65 6c 6c 28 0a 20  descendToCell(. 
385d8 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20   Rtree *pRtree, 
385d9 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a  .  RtreeCursor *
385da 70 43 75 72 73 6f 72 2c 20 0a 20 20 69 6e 74 20  pCursor, .  int 
385db 69 48 65 69 67 68 74 2c 0a 20 20 69 6e 74 20 2a  iHeight,.  int *
385dc 70 45 6f 66 20 20 20 20 20 20 20 20 20 20 20 20  pEof            
385dd 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65 74       /* OUT: Set
385de 20 74 6f 20 74 72 75 65 20 69 66 20 63 61 6e 6e   to true if cann
385df 6f 74 20 64 65 73 63 65 6e 64 20 2a 2f 0a 29 7b  ot descend */.){
385e0 0a 20 20 69 6e 74 20 69 73 45 6f 66 3b 0a 20 20  .  int isEof;.  
385e1 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 69  int rc;.  int ii
385e2 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ;.  RtreeNode *p
385e3 43 68 69 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33  Child;.  sqlite3
385e4 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 3b 0a 0a  _int64 iRowid;..
385e5 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 53 61    RtreeNode *pSa
385e6 76 65 64 4e 6f 64 65 20 3d 20 70 43 75 72 73 6f  vedNode = pCurso
385e7 72 2d 3e 70 4e 6f 64 65 3b 0a 20 20 69 6e 74 20  r->pNode;.  int 
385e8 69 53 61 76 65 64 43 65 6c 6c 20 3d 20 70 43 75  iSavedCell = pCu
385e9 72 73 6f 72 2d 3e 69 43 65 6c 6c 3b 0a 0a 20 20  rsor->iCell;..  
385ea 61 73 73 65 72 74 28 20 69 48 65 69 67 68 74 3e  assert( iHeight>
385eb 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 69 48 65  =0 );..  if( iHe
385ec 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69  ight==0 ){.    i
385ed 73 45 6f 66 20 3d 20 74 65 73 74 52 74 72 65 65  sEof = testRtree
385ee 45 6e 74 72 79 28 70 52 74 72 65 65 2c 20 70 43  Entry(pRtree, pC
385ef 75 72 73 6f 72 29 3b 0a 20 20 7d 65 6c 73 65 7b  ursor);.  }else{
385f0 0a 20 20 20 20 69 73 45 6f 66 20 3d 20 74 65 73  .    isEof = tes
385f1 74 52 74 72 65 65 43 65 6c 6c 28 70 52 74 72 65  tRtreeCell(pRtre
385f2 65 2c 20 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d  e, pCursor);.  }
385f3 0a 20 20 69 66 28 20 69 73 45 6f 66 20 7c 7c 20  .  if( isEof || 
385f4 69 48 65 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20  iHeight==0 ){.  
385f5 20 20 2a 70 45 6f 66 20 3d 20 69 73 45 6f 66 3b    *pEof = isEof;
385f6 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
385f7 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 52  TE_OK;.  }..  iR
385f8 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f  owid = nodeGetRo
385f9 77 69 64 28 70 52 74 72 65 65 2c 20 70 43 75 72  wid(pRtree, pCur
385fa 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 75 72  sor->pNode, pCur
385fb 73 6f 72 2d 3e 69 43 65 6c 6c 29 3b 0a 20 20 72  sor->iCell);.  r
385fc 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28  c = nodeAcquire(
385fd 70 52 74 72 65 65 2c 20 69 52 6f 77 69 64 2c 20  pRtree, iRowid, 
385fe 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20  pCursor->pNode, 
385ff 26 70 43 68 69 6c 64 29 3b 0a 20 20 69 66 28 20  &pChild);.  if( 
38600 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
38601 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
38602 20 20 7d 0a 0a 20 20 6e 6f 64 65 52 65 6c 65 61    }..  nodeRelea
38603 73 65 28 70 52 74 72 65 65 2c 20 70 43 75 72 73  se(pRtree, pCurs
38604 6f 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20 20 70 43  or->pNode);.  pC
38605 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 20 3d 20 70  ursor->pNode = p
38606 43 68 69 6c 64 3b 0a 20 20 69 73 45 6f 66 20 3d  Child;.  isEof =
38607 20 31 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20   1;.  for(ii=0; 
38608 69 73 45 6f 66 20 26 26 20 69 69 3c 4e 43 45 4c  isEof && ii<NCEL
38609 4c 28 70 43 68 69 6c 64 29 3b 20 69 69 2b 2b 29  L(pChild); ii++)
3860a 7b 0a 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 69  {.    pCursor->i
3860b 43 65 6c 6c 20 3d 20 69 69 3b 0a 20 20 20 20 72  Cell = ii;.    r
3860c 63 20 3d 20 64 65 73 63 65 6e 64 54 6f 43 65 6c  c = descendToCel
3860d 6c 28 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f  l(pRtree, pCurso
3860e 72 2c 20 69 48 65 69 67 68 74 2d 31 2c 20 26 69  r, iHeight-1, &i
3860f 73 45 6f 66 29 3b 0a 20 20 20 20 69 66 28 20 72  sEof);.    if( r
38610 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
38611 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
38612 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66  .    }.  }..  if
38613 28 20 69 73 45 6f 66 20 29 7b 0a 20 20 20 20 61  ( isEof ){.    a
38614 73 73 65 72 74 28 20 70 43 75 72 73 6f 72 2d 3e  ssert( pCursor->
38615 70 4e 6f 64 65 3d 3d 70 43 68 69 6c 64 20 29 3b  pNode==pChild );
38616 0a 20 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e  .    nodeReferen
38617 63 65 28 70 53 61 76 65 64 4e 6f 64 65 29 3b 0a  ce(pSavedNode);.
38618 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28      nodeRelease(
38619 70 52 74 72 65 65 2c 20 70 43 68 69 6c 64 29 3b  pRtree, pChild);
3861a 0a 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 70 4e  .    pCursor->pN
3861b 6f 64 65 20 3d 20 70 53 61 76 65 64 4e 6f 64 65  ode = pSavedNode
3861c 3b 0a 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 69  ;.    pCursor->i
3861d 43 65 6c 6c 20 3d 20 69 53 61 76 65 64 43 65 6c  Cell = iSavedCel
3861e 6c 3b 0a 20 20 7d 0a 0a 20 20 2a 70 45 6f 66 20  l;.  }..  *pEof 
3861f 3d 20 69 73 45 6f 66 3b 0a 20 20 72 65 74 75 72  = isEof;.  retur
38620 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
38621 2f 2a 0a 2a 2a 20 4f 6e 65 20 6f 66 20 74 68 65  /*.** One of the
38622 20 63 65 6c 6c 73 20 69 6e 20 6e 6f 64 65 20 70   cells in node p
38623 4e 6f 64 65 20 69 73 20 67 75 61 72 61 6e 74 65  Node is guarante
38624 65 64 20 74 6f 20 68 61 76 65 20 61 20 36 34 2d  ed to have a 64-
38625 62 69 74 20 0a 2a 2a 20 69 6e 74 65 67 65 72 20  bit .** integer 
38626 76 61 6c 75 65 20 65 71 75 61 6c 20 74 6f 20 69  value equal to i
38627 52 6f 77 69 64 2e 20 52 65 74 75 72 6e 20 74 68  Rowid. Return th
38628 65 20 69 6e 64 65 78 20 6f 66 20 74 68 69 73 20  e index of this 
38629 63 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  cell..*/.static 
3862a 69 6e 74 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64  int nodeRowidInd
3862b 65 78 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  ex(Rtree *pRtree
3862c 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f  , RtreeNode *pNo
3862d 64 65 2c 20 69 36 34 20 69 52 6f 77 69 64 29 7b  de, i64 iRowid){
3862e 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72  .  int ii;.  for
3862f 28 69 69 3d 30 3b 20 6e 6f 64 65 47 65 74 52 6f  (ii=0; nodeGetRo
38630 77 69 64 28 70 52 74 72 65 65 2c 20 70 4e 6f 64  wid(pRtree, pNod
38631 65 2c 20 69 69 29 21 3d 69 52 6f 77 69 64 3b 20  e, ii)!=iRowid; 
38632 69 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72  ii++){.    asser
38633 74 28 20 69 69 3c 28 4e 43 45 4c 4c 28 70 4e 6f  t( ii<(NCELL(pNo
38634 64 65 29 2d 31 29 20 29 3b 0a 20 20 7d 0a 20 20  de)-1) );.  }.  
38635 72 65 74 75 72 6e 20 69 69 3b 0a 7d 0a 0a 2f 2a  return ii;.}../*
38636 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 69  .** Return the i
38637 6e 64 65 78 20 6f 66 20 74 68 65 20 63 65 6c 6c  ndex of the cell
38638 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 20 70 6f   containing a po
38639 69 6e 74 65 72 20 74 6f 20 6e 6f 64 65 20 70 4e  inter to node pN
3863a 6f 64 65 0a 2a 2a 20 69 6e 20 69 74 73 20 70 61  ode.** in its pa
3863b 72 65 6e 74 2e 20 49 66 20 70 4e 6f 64 65 20 69  rent. If pNode i
3863c 73 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 2c  s the root node,
3863d 20 72 65 74 75 72 6e 20 2d 31 2e 0a 2a 2f 0a 73   return -1..*/.s
3863e 74 61 74 69 63 20 69 6e 74 20 6e 6f 64 65 50 61  tatic int nodePa
3863f 72 65 6e 74 49 6e 64 65 78 28 52 74 72 65 65 20  rentIndex(Rtree 
38640 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f  *pRtree, RtreeNo
38641 64 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 52 74  de *pNode){.  Rt
38642 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74  reeNode *pParent
38643 20 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e   = pNode->pParen
38644 74 3b 0a 20 20 69 66 28 20 70 50 61 72 65 6e 74  t;.  if( pParent
38645 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6e   ){.    return n
38646 6f 64 65 52 6f 77 69 64 49 6e 64 65 78 28 70 52  odeRowidIndex(pR
38647 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 70  tree, pParent, p
38648 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20  Node->iNode);.  
38649 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 7d  }.  return -1;.}
3864a 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76  ../* .** Rtree v
3864b 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
3864c 75 6c 65 20 78 4e 65 78 74 20 6d 65 74 68 6f 64  ule xNext method
3864d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3864e 72 74 72 65 65 4e 65 78 74 28 73 71 6c 69 74 65  rtreeNext(sqlite
3864f 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70  3_vtab_cursor *p
38650 56 74 61 62 43 75 72 73 6f 72 29 7b 0a 20 20 52  VtabCursor){.  R
38651 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28  tree *pRtree = (
38652 52 74 72 65 65 20 2a 29 28 70 56 74 61 62 43 75  Rtree *)(pVtabCu
38653 72 73 6f 72 2d 3e 70 56 74 61 62 29 3b 0a 20 20  rsor->pVtab);.  
38654 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73  RtreeCursor *pCs
38655 72 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72  r = (RtreeCursor
38656 20 2a 29 70 56 74 61 62 43 75 72 73 6f 72 3b 0a   *)pVtabCursor;.
38657 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
38658 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 70 43 73  E_OK;..  if( pCs
38659 72 2d 3e 69 53 74 72 61 74 65 67 79 3d 3d 31 20  r->iStrategy==1 
3865a 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 22  ){.    /* This "
3865b 73 63 61 6e 22 20 69 73 20 61 20 64 69 72 65 63  scan" is a direc
3865c 74 20 6c 6f 6f 6b 75 70 20 62 79 20 72 6f 77 69  t lookup by rowi
3865d 64 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e  d. There is no n
3865e 65 78 74 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20  ext entry. */.  
3865f 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52    nodeRelease(pR
38660 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64  tree, pCsr->pNod
38661 65 29 3b 0a 20 20 20 20 70 43 73 72 2d 3e 70 4e  e);.    pCsr->pN
38662 6f 64 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  ode = 0;.  }..  
38663 65 6c 73 65 20 69 66 28 20 70 43 73 72 2d 3e 70  else if( pCsr->p
38664 4e 6f 64 65 20 29 7b 0a 20 20 20 20 2f 2a 20 4d  Node ){.    /* M
38665 6f 76 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ove to the next 
38666 65 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 68  entry that match
38667 65 73 20 74 68 65 20 63 6f 6e 66 69 67 75 72 65  es the configure
38668 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 2a  d constraints. *
38669 2f 0a 20 20 20 20 69 6e 74 20 69 48 65 69 67 68  /.    int iHeigh
3866a 74 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65  t = 0;.    while
3866b 28 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20 29 7b  ( pCsr->pNode ){
3866c 0a 20 20 20 20 20 20 52 74 72 65 65 4e 6f 64 65  .      RtreeNode
3866d 20 2a 70 4e 6f 64 65 20 3d 20 70 43 73 72 2d 3e   *pNode = pCsr->
3866e 70 4e 6f 64 65 3b 0a 20 20 20 20 20 20 69 6e 74  pNode;.      int
3866f 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70   nCell = NCELL(p
38670 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 66 6f 72  Node);.      for
38671 28 70 43 73 72 2d 3e 69 43 65 6c 6c 2b 2b 3b 20  (pCsr->iCell++; 
38672 70 43 73 72 2d 3e 69 43 65 6c 6c 3c 6e 43 65 6c  pCsr->iCell<nCel
38673 6c 3b 20 70 43 73 72 2d 3e 69 43 65 6c 6c 2b 2b  l; pCsr->iCell++
38674 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  ){.        int i
38675 73 45 6f 66 3b 0a 20 20 20 20 20 20 20 20 72 63  sEof;.        rc
38676 20 3d 20 64 65 73 63 65 6e 64 54 6f 43 65 6c 6c   = descendToCell
38677 28 70 52 74 72 65 65 2c 20 70 43 73 72 2c 20 69  (pRtree, pCsr, i
38678 48 65 69 67 68 74 2c 20 26 69 73 45 6f 66 29 3b  Height, &isEof);
38679 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
3867a 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 69  =SQLITE_OK || !i
3867b 73 45 6f 66 20 29 7b 0a 20 20 20 20 20 20 20 20  sEof ){.        
3867c 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
3867d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
3867e 20 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65       pCsr->pNode
3867f 20 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e   = pNode->pParen
38680 74 3b 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 69  t;.      pCsr->i
38681 43 65 6c 6c 20 3d 20 6e 6f 64 65 50 61 72 65 6e  Cell = nodeParen
38682 74 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70  tIndex(pRtree, p
38683 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 6e 6f 64  Node);.      nod
38684 65 52 65 66 65 72 65 6e 63 65 28 70 43 73 72 2d  eReference(pCsr-
38685 3e 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 6e  >pNode);.      n
38686 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
38687 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20  e, pNode);.     
38688 20 69 48 65 69 67 68 74 2b 2b 3b 0a 20 20 20 20   iHeight++;.    
38689 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  }.  }..  return 
3868a 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74  rc;.}../* .** Rt
3868b 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ree virtual tabl
3868c 65 20 6d 6f 64 75 6c 65 20 78 52 6f 77 69 64 20  e module xRowid 
3868d 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69  method..*/.stati
3868e 63 20 69 6e 74 20 72 74 72 65 65 52 6f 77 69 64  c int rtreeRowid
3868f 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75  (sqlite3_vtab_cu
38690 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f  rsor *pVtabCurso
38691 72 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  r, sqlite_int64 
38692 2a 70 52 6f 77 69 64 29 7b 0a 20 20 52 74 72 65  *pRowid){.  Rtre
38693 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72  e *pRtree = (Rtr
38694 65 65 20 2a 29 70 56 74 61 62 43 75 72 73 6f 72  ee *)pVtabCursor
38695 2d 3e 70 56 74 61 62 3b 0a 20 20 52 74 72 65 65  ->pVtab;.  Rtree
38696 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28  Cursor *pCsr = (
38697 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29 70 56  RtreeCursor *)pV
38698 74 61 62 43 75 72 73 6f 72 3b 0a 0a 20 20 61 73  tabCursor;..  as
38699 73 65 72 74 28 70 43 73 72 2d 3e 70 4e 6f 64 65  sert(pCsr->pNode
3869a 29 3b 0a 20 20 2a 70 52 6f 77 69 64 20 3d 20 6e  );.  *pRowid = n
3869b 6f 64 65 47 65 74 52 6f 77 69 64 28 70 52 74 72  odeGetRowid(pRtr
3869c 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65 2c  ee, pCsr->pNode,
3869d 20 70 43 73 72 2d 3e 69 43 65 6c 6c 29 3b 0a 0a   pCsr->iCell);..
3869e 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3869f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74  OK;.}../* .** Rt
386a0 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ree virtual tabl
386a1 65 20 6d 6f 64 75 6c 65 20 78 43 6f 6c 75 6d 6e  e module xColumn
386a2 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74   method..*/.stat
386a3 69 63 20 69 6e 74 20 72 74 72 65 65 43 6f 6c 75  ic int rtreeColu
386a4 6d 6e 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  mn(sqlite3_vtab_
386a5 63 75 72 73 6f 72 20 2a 63 75 72 2c 20 73 71 6c  cursor *cur, sql
386a6 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 74  ite3_context *ct
386a7 78 2c 20 69 6e 74 20 69 29 7b 0a 20 20 52 74 72  x, int i){.  Rtr
386a8 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74  ee *pRtree = (Rt
386a9 72 65 65 20 2a 29 63 75 72 2d 3e 70 56 74 61 62  ree *)cur->pVtab
386aa 3b 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72 20  ;.  RtreeCursor 
386ab 2a 70 43 73 72 20 3d 20 28 52 74 72 65 65 43 75  *pCsr = (RtreeCu
386ac 72 73 6f 72 20 2a 29 63 75 72 3b 0a 0a 20 20 69  rsor *)cur;..  i
386ad 66 28 20 69 3d 3d 30 20 29 7b 0a 20 20 20 20 69  f( i==0 ){.    i
386ae 36 34 20 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65  64 iRowid = node
386af 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c  GetRowid(pRtree,
386b0 20 70 43 73 72 2d 3e 70 4e 6f 64 65 2c 20 70 43   pCsr->pNode, pC
386b1 73 72 2d 3e 69 43 65 6c 6c 29 3b 0a 20 20 20 20  sr->iCell);.    
386b2 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
386b3 6e 74 36 34 28 63 74 78 2c 20 69 52 6f 77 69 64  nt64(ctx, iRowid
386b4 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
386b5 52 74 72 65 65 43 6f 6f 72 64 20 63 3b 0a 20 20  RtreeCoord c;.  
386b6 20 20 6e 6f 64 65 47 65 74 43 6f 6f 72 64 28 70    nodeGetCoord(p
386b7 52 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f  Rtree, pCsr->pNo
386b8 64 65 2c 20 70 43 73 72 2d 3e 69 43 65 6c 6c 2c  de, pCsr->iCell,
386b9 20 69 2d 31 2c 20 26 63 29 3b 0a 20 20 20 20 69   i-1, &c);.    i
386ba 66 28 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72  f( pRtree->eCoor
386bb 64 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f  dType==RTREE_COO
386bc 52 44 5f 52 45 41 4c 33 32 20 29 7b 0a 20 20 20  RD_REAL32 ){.   
386bd 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
386be 74 5f 64 6f 75 62 6c 65 28 63 74 78 2c 20 63 2e  t_double(ctx, c.
386bf 66 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  f);.    }else{. 
386c0 20 20 20 20 20 61 73 73 65 72 74 28 20 70 52 74       assert( pRt
386c1 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d  ree->eCoordType=
386c2 3d 52 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54  =RTREE_COORD_INT
386c3 33 32 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  32 );.      sqli
386c4 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63  te3_result_int(c
386c5 74 78 2c 20 63 2e 69 29 3b 0a 20 20 20 20 7d 0a  tx, c.i);.    }.
386c6 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51    }..  return SQ
386c7 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a  LITE_OK;.}../* .
386c8 2a 2a 20 55 73 65 20 6e 6f 64 65 41 63 71 75 69  ** Use nodeAcqui
386c9 72 65 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 74  re() to obtain t
386ca 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 63 6f 6e  he leaf node con
386cb 74 61 69 6e 69 6e 67 20 74 68 65 20 72 65 63 6f  taining the reco
386cc 72 64 20 77 69 74 68 20 0a 2a 2a 20 72 6f 77 69  rd with .** rowi
386cd 64 20 69 52 6f 77 69 64 2e 20 49 66 20 73 75 63  d iRowid. If suc
386ce 63 65 73 73 66 75 6c 2c 20 73 65 74 20 2a 70 70  cessful, set *pp
386cf 4c 65 61 66 20 74 6f 20 70 6f 69 6e 74 20 74 6f  Leaf to point to
386d0 20 74 68 65 20 6e 6f 64 65 20 61 6e 64 0a 2a 2a   the node and.**
386d1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
386d2 4b 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  K. If there is n
386d3 6f 20 73 75 63 68 20 72 65 63 6f 72 64 20 69 6e  o such record in
386d4 20 74 68 65 20 74 61 62 6c 65 2c 20 73 65 74 0a   the table, set.
386d5 2a 2a 20 2a 70 70 4c 65 61 66 20 74 6f 20 30 20  ** *ppLeaf to 0 
386d6 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54  and return SQLIT
386d7 45 5f 4f 4b 2e 20 49 66 20 61 6e 20 65 72 72 6f  E_OK. If an erro
386d8 72 20 6f 63 63 75 72 73 2c 20 73 65 74 20 2a 70  r occurs, set *p
386d9 70 4c 65 61 66 0a 2a 2a 20 74 6f 20 7a 65 72 6f  pLeaf.** to zero
386da 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 53   and return an S
386db 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
386dc 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
386dd 66 69 6e 64 4c 65 61 66 4e 6f 64 65 28 52 74 72  findLeafNode(Rtr
386de 65 65 20 2a 70 52 74 72 65 65 2c 20 69 36 34 20  ee *pRtree, i64 
386df 69 52 6f 77 69 64 2c 20 52 74 72 65 65 4e 6f 64  iRowid, RtreeNod
386e0 65 20 2a 2a 70 70 4c 65 61 66 29 7b 0a 20 20 69  e **ppLeaf){.  i
386e1 6e 74 20 72 63 3b 0a 20 20 2a 70 70 4c 65 61 66  nt rc;.  *ppLeaf
386e2 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f   = 0;.  sqlite3_
386e3 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65  bind_int64(pRtre
386e4 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 2c 20 31  e->pReadRowid, 1
386e5 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20 69 66 28  , iRowid);.  if(
386e6 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52   sqlite3_step(pR
386e7 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64  tree->pReadRowid
386e8 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  )==SQLITE_ROW ){
386e9 0a 20 20 20 20 69 36 34 20 69 4e 6f 64 65 20 3d  .    i64 iNode =
386ea 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
386eb 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 52  int64(pRtree->pR
386ec 65 61 64 52 6f 77 69 64 2c 20 30 29 3b 0a 20 20  eadRowid, 0);.  
386ed 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69    rc = nodeAcqui
386ee 72 65 28 70 52 74 72 65 65 2c 20 69 4e 6f 64 65  re(pRtree, iNode
386ef 2c 20 30 2c 20 70 70 4c 65 61 66 29 3b 0a 20 20  , 0, ppLeaf);.  
386f0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28    sqlite3_reset(
386f1 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77  pRtree->pReadRow
386f2 69 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  id);.  }else{.  
386f3 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72    rc = sqlite3_r
386f4 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65  eset(pRtree->pRe
386f5 61 64 52 6f 77 69 64 29 3b 0a 20 20 7d 0a 20 20  adRowid);.  }.  
386f6 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
386f7 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74  * .** Rtree virt
386f8 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65  ual table module
386f9 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 2e   xFilter method.
386fa 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
386fb 74 72 65 65 46 69 6c 74 65 72 28 0a 20 20 73 71  treeFilter(.  sq
386fc 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
386fd 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 2c 20  r *pVtabCursor, 
386fe 0a 20 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63  .  int idxNum, c
386ff 6f 6e 73 74 20 63 68 61 72 20 2a 69 64 78 53 74  onst char *idxSt
38700 72 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 73  r,.  int argc, s
38701 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
38702 72 67 76 0a 29 7b 0a 20 20 52 74 72 65 65 20 2a  rgv.){.  Rtree *
38703 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20  pRtree = (Rtree 
38704 2a 29 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70  *)pVtabCursor->p
38705 56 74 61 62 3b 0a 20 20 52 74 72 65 65 43 75 72  Vtab;.  RtreeCur
38706 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 52 74 72  sor *pCsr = (Rtr
38707 65 65 43 75 72 73 6f 72 20 2a 29 70 56 74 61 62  eeCursor *)pVtab
38708 43 75 72 73 6f 72 3b 0a 0a 20 20 52 74 72 65 65  Cursor;..  Rtree
38709 4e 6f 64 65 20 2a 70 52 6f 6f 74 20 3d 20 30 3b  Node *pRoot = 0;
3870a 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74  .  int ii;.  int
3870b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
3870c 0a 0a 20 20 72 74 72 65 65 52 65 66 65 72 65 6e  ..  rtreeReferen
3870d 63 65 28 70 52 74 72 65 65 29 3b 0a 0a 20 20 73  ce(pRtree);..  s
3870e 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73 72  qlite3_free(pCsr
3870f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 29 3b 0a  ->aConstraint);.
38710 20 20 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61    pCsr->aConstra
38711 69 6e 74 20 3d 20 30 3b 0a 20 20 70 43 73 72 2d  int = 0;.  pCsr-
38712 3e 69 53 74 72 61 74 65 67 79 20 3d 20 69 64 78  >iStrategy = idx
38713 4e 75 6d 3b 0a 0a 20 20 69 66 28 20 69 64 78 4e  Num;..  if( idxN
38714 75 6d 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20  um==1 ){.    /* 
38715 53 70 65 63 69 61 6c 20 63 61 73 65 20 2d 20 6c  Special case - l
38716 6f 6f 6b 75 70 20 62 79 20 72 6f 77 69 64 2e 20  ookup by rowid. 
38717 2a 2f 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65  */.    RtreeNode
38718 20 2a 70 4c 65 61 66 3b 20 20 20 20 20 20 20 20   *pLeaf;        
38719 2f 2a 20 4c 65 61 66 20 6f 6e 20 77 68 69 63 68  /* Leaf on which
3871a 20 74 68 65 20 72 65 71 75 69 72 65 64 20 63 65   the required ce
3871b 6c 6c 20 72 65 73 69 64 65 73 20 2a 2f 0a 20 20  ll resides */.  
3871c 20 20 69 36 34 20 69 52 6f 77 69 64 20 3d 20 73    i64 iRowid = s
3871d 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
3871e 36 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  64(argv[0]);.   
3871f 20 72 63 20 3d 20 66 69 6e 64 4c 65 61 66 4e 6f   rc = findLeafNo
38720 64 65 28 70 52 74 72 65 65 2c 20 69 52 6f 77 69  de(pRtree, iRowi
38721 64 2c 20 26 70 4c 65 61 66 29 3b 0a 20 20 20 20  d, &pLeaf);.    
38722 70 43 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 4c  pCsr->pNode = pL
38723 65 61 66 3b 20 0a 20 20 20 20 69 66 28 20 70 4c  eaf; .    if( pL
38724 65 61 66 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  eaf && rc==SQLIT
38725 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 43  E_OK ){.      pC
38726 73 72 2d 3e 69 43 65 6c 6c 20 3d 20 6e 6f 64 65  sr->iCell = node
38727 52 6f 77 69 64 49 6e 64 65 78 28 70 52 74 72 65  RowidIndex(pRtre
38728 65 2c 20 70 4c 65 61 66 2c 20 69 52 6f 77 69 64  e, pLeaf, iRowid
38729 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
3872a 7b 0a 20 20 20 20 2f 2a 20 4e 6f 72 6d 61 6c 20  {.    /* Normal 
3872b 63 61 73 65 20 2d 20 72 2d 74 72 65 65 20 73 63  case - r-tree sc
3872c 61 6e 2e 20 53 65 74 20 75 70 20 74 68 65 20 52  an. Set up the R
3872d 74 72 65 65 43 75 72 73 6f 72 2e 61 43 6f 6e 73  treeCursor.aCons
3872e 74 72 61 69 6e 74 20 61 72 72 61 79 20 0a 20 20  traint array .  
3872f 20 20 2a 2a 20 77 69 74 68 20 74 68 65 20 63 6f    ** with the co
38730 6e 66 69 67 75 72 65 64 20 63 6f 6e 73 74 72 61  nfigured constra
38731 69 6e 74 73 2e 20 0a 20 20 20 20 2a 2f 0a 20 20  ints. .    */.  
38732 20 20 69 66 28 20 61 72 67 63 3e 30 20 29 7b 0a    if( argc>0 ){.
38733 20 20 20 20 20 20 70 43 73 72 2d 3e 61 43 6f 6e        pCsr->aCon
38734 73 74 72 61 69 6e 74 20 3d 20 73 71 6c 69 74 65  straint = sqlite
38735 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
38736 52 74 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 29  RtreeConstraint)
38737 2a 61 72 67 63 29 3b 0a 20 20 20 20 20 20 70 43  *argc);.      pC
38738 73 72 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 20  sr->nConstraint 
38739 3d 20 61 72 67 63 3b 0a 20 20 20 20 20 20 69 66  = argc;.      if
3873a 28 20 21 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72  ( !pCsr->aConstr
3873b 61 69 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20  aint ){.        
3873c 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
3873d 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  M;.      }else{.
3873e 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
3873f 28 69 64 78 53 74 72 3d 3d 30 20 26 26 20 61 72  (idxStr==0 && ar
38740 67 63 3d 3d 30 29 20 7c 7c 20 73 74 72 6c 65 6e  gc==0) || strlen
38741 28 69 64 78 53 74 72 29 3d 3d 61 72 67 63 2a 32  (idxStr)==argc*2
38742 20 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28   );.        for(
38743 69 69 3d 30 3b 20 69 69 3c 61 72 67 63 3b 20 69  ii=0; ii<argc; i
38744 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
38745 52 74 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 20  RtreeConstraint 
38746 2a 70 20 3d 20 26 70 43 73 72 2d 3e 61 43 6f 6e  *p = &pCsr->aCon
38747 73 74 72 61 69 6e 74 5b 69 69 5d 3b 0a 20 20 20  straint[ii];.   
38748 20 20 20 20 20 20 20 70 2d 3e 6f 70 20 3d 20 69         p->op = i
38749 64 78 53 74 72 5b 69 69 2a 32 5d 3b 0a 20 20 20  dxStr[ii*2];.   
3874a 20 20 20 20 20 20 20 70 2d 3e 69 43 6f 6f 72 64         p->iCoord
3874b 20 3d 20 69 64 78 53 74 72 5b 69 69 2a 32 2b 31   = idxStr[ii*2+1
3874c 5d 2d 27 61 27 3b 0a 20 20 20 20 20 20 20 20 20  ]-'a';.         
3874d 20 70 2d 3e 72 56 61 6c 75 65 20 3d 20 73 71 6c   p->rValue = sql
3874e 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
3874f 65 28 61 72 67 76 5b 69 69 5d 29 3b 0a 20 20 20  e(argv[ii]);.   
38750 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
38751 20 20 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20     }.  .    if( 
38752 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
38753 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f  .      pCsr->pNo
38754 64 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63  de = 0;.      rc
38755 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28 70   = nodeAcquire(p
38756 52 74 72 65 65 2c 20 31 2c 20 30 2c 20 26 70 52  Rtree, 1, 0, &pR
38757 6f 6f 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  oot);.    }.    
38758 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
38759 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  K ){.      int i
3875a 73 45 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20  sEof = 1;.      
3875b 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c  int nCell = NCEL
3875c 4c 28 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20  L(pRoot);.      
3875d 70 43 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 52  pCsr->pNode = pR
3875e 6f 6f 74 3b 0a 20 20 20 20 20 20 66 6f 72 28 70  oot;.      for(p
3875f 43 73 72 2d 3e 69 43 65 6c 6c 3d 30 3b 20 72 63  Csr->iCell=0; rc
38760 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
38761 43 73 72 2d 3e 69 43 65 6c 6c 3c 6e 43 65 6c 6c  Csr->iCell<nCell
38762 3b 20 70 43 73 72 2d 3e 69 43 65 6c 6c 2b 2b 29  ; pCsr->iCell++)
38763 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
38764 28 20 70 43 73 72 2d 3e 70 4e 6f 64 65 3d 3d 70  ( pCsr->pNode==p
38765 52 6f 6f 74 20 29 3b 0a 20 20 20 20 20 20 20 20  Root );.        
38766 72 63 20 3d 20 64 65 73 63 65 6e 64 54 6f 43 65  rc = descendToCe
38767 6c 6c 28 70 52 74 72 65 65 2c 20 70 43 73 72 2c  ll(pRtree, pCsr,
38768 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2c   pRtree->iDepth,
38769 20 26 69 73 45 6f 66 29 3b 0a 20 20 20 20 20 20   &isEof);.      
3876a 20 20 69 66 28 20 21 69 73 45 6f 66 20 29 7b 0a    if( !isEof ){.
3876b 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
3876c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
3876d 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d   }.      if( rc=
3876e 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 73  =SQLITE_OK && is
3876f 45 6f 66 20 29 7b 0a 20 20 20 20 20 20 20 20 61  Eof ){.        a
38770 73 73 65 72 74 28 20 70 43 73 72 2d 3e 70 4e 6f  ssert( pCsr->pNo
38771 64 65 3d 3d 70 52 6f 6f 74 20 29 3b 0a 20 20 20  de==pRoot );.   
38772 20 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65       nodeRelease
38773 28 70 52 74 72 65 65 2c 20 70 52 6f 6f 74 29 3b  (pRtree, pRoot);
38774 0a 20 20 20 20 20 20 20 20 70 43 73 72 2d 3e 70  .        pCsr->p
38775 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20 20 20  Node = 0;.      
38776 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  }.      assert( 
38777 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
38778 20 21 70 43 73 72 2d 3e 70 4e 6f 64 65 20 7c 7c   !pCsr->pNode ||
38779 20 70 43 73 72 2d 3e 69 43 65 6c 6c 3c 4e 43 45   pCsr->iCell<NCE
3877a 4c 4c 28 70 43 73 72 2d 3e 70 4e 6f 64 65 29 20  LL(pCsr->pNode) 
3877b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
3877c 72 74 72 65 65 52 65 6c 65 61 73 65 28 70 52 74  rtreeRelease(pRt
3877d 72 65 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ree);.  return r
3877e 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 74 72 65  c;.}../*.** Rtre
3877f 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
38780 6d 6f 64 75 6c 65 20 78 42 65 73 74 49 6e 64 65  module xBestInde
38781 78 20 6d 65 74 68 6f 64 2e 20 54 68 65 72 65 20  x method. There 
38782 61 72 65 20 74 68 72 65 65 0a 2a 2a 20 74 61 62  are three.** tab
38783 6c 65 20 73 63 61 6e 20 73 74 72 61 74 65 67 69  le scan strategi
38784 65 73 20 74 6f 20 63 68 6f 6f 73 65 20 66 72 6f  es to choose fro
38785 6d 20 28 69 6e 20 6f 72 64 65 72 20 66 72 6f 6d  m (in order from
38786 20 6d 6f 73 74 20 74 6f 20 0a 2a 2a 20 6c 65 61   most to .** lea
38787 73 74 20 64 65 73 69 72 61 62 6c 65 29 3a 0a 2a  st desirable):.*
38788 2a 0a 2a 2a 20 20 20 69 64 78 4e 75 6d 20 20 20  *.**   idxNum   
38789 20 20 69 64 78 53 74 72 20 20 20 20 20 20 20 20    idxStr        
3878a 53 74 72 61 74 65 67 79 0a 2a 2a 20 20 20 2d 2d  Strategy.**   --
3878b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3878c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3878d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
3878e 2a 20 20 20 20 20 31 20 20 20 20 20 20 20 20 55  *     1        U
3878f 6e 75 73 65 64 20 20 20 20 20 20 20 20 44 69 72  nused        Dir
38790 65 63 74 20 6c 6f 6f 6b 75 70 20 62 79 20 72 6f  ect lookup by ro
38791 77 69 64 2e 0a 2a 2a 20 20 20 20 20 32 20 20 20  wid..**     2   
38792 20 20 20 20 20 53 65 65 20 62 65 6c 6f 77 20 20       See below  
38793 20 20 20 52 2d 74 72 65 65 20 71 75 65 72 79 2e     R-tree query.
38794 0a 2a 2a 20 20 20 20 20 33 20 20 20 20 20 20 20  .**     3       
38795 20 55 6e 75 73 65 64 20 20 20 20 20 20 20 20 46   Unused        F
38796 75 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e 2e 0a  ull table scan..
38797 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  **   -----------
38798 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
38799 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3879a 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 49 66 20 73  -----.**.** If s
3879b 74 72 61 74 65 67 79 20 31 20 6f 72 20 33 20 69  trategy 1 or 3 i
3879c 73 20 75 73 65 64 2c 20 74 68 65 6e 20 69 64 78  s used, then idx
3879d 53 74 72 20 69 73 20 6e 6f 74 20 6d 65 61 6e 69  Str is not meani
3879e 6e 67 66 75 6c 2e 20 49 66 20 73 74 72 61 74 65  ngful. If strate
3879f 67 79 0a 2a 2a 20 32 20 69 73 20 75 73 65 64 2c  gy.** 2 is used,
387a0 20 69 64 78 53 74 72 20 69 73 20 66 6f 72 6d 61   idxStr is forma
387a1 74 74 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20  tted to contain 
387a2 32 20 62 79 74 65 73 20 66 6f 72 20 65 61 63 68  2 bytes for each
387a3 20 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20   .** constraint 
387a4 75 73 65 64 2e 20 54 68 65 20 66 69 72 73 74 20  used. The first 
387a5 74 77 6f 20 62 79 74 65 73 20 6f 66 20 69 64 78  two bytes of idx
387a6 53 74 72 20 63 6f 72 72 65 73 70 6f 6e 64 20 74  Str correspond t
387a7 6f 20 0a 2a 2a 20 74 68 65 20 63 6f 6e 73 74 72  o .** the constr
387a8 61 69 6e 74 20 69 6e 20 73 71 6c 69 74 65 33 5f  aint in sqlite3_
387a9 69 6e 64 65 78 5f 69 6e 66 6f 2e 61 43 6f 6e 73  index_info.aCons
387aa 74 72 61 69 6e 74 55 73 61 67 65 5b 5d 20 77 69  traintUsage[] wi
387ab 74 68 0a 2a 2a 20 28 61 72 67 76 49 6e 64 65 78  th.** (argvIndex
387ac 3d 3d 31 29 20 65 74 63 2e 0a 2a 2a 0a 2a 2a 20  ==1) etc..**.** 
387ad 54 68 65 20 66 69 72 73 74 20 6f 66 20 65 61 63  The first of eac
387ae 68 20 70 61 69 72 20 6f 66 20 62 79 74 65 73 20  h pair of bytes 
387af 69 6e 20 69 64 78 53 74 72 20 69 64 65 6e 74 69  in idxStr identi
387b0 66 69 65 73 20 74 68 65 20 63 6f 6e 73 74 72 61  fies the constra
387b1 69 6e 74 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20  int.** operator 
387b2 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
387b3 2a 20 20 20 4f 70 65 72 61 74 6f 72 20 20 20 20  *   Operator    
387b4 42 79 74 65 20 56 61 6c 75 65 0a 2a 2a 20 20 20  Byte Value.**   
387b5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
387b6 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 3d  ------.**      =
387b7 20 20 20 20 20 20 20 20 30 78 34 31 20 28 27 41          0x41 ('A
387b8 27 29 0a 2a 2a 20 20 20 20 20 3c 3d 20 20 20 20  ').**     <=    
387b9 20 20 20 20 30 78 34 32 20 28 27 42 27 29 0a 2a      0x42 ('B').*
387ba 2a 20 20 20 20 20 20 3c 20 20 20 20 20 20 20 20  *      <        
387bb 30 78 34 33 20 28 27 43 27 29 0a 2a 2a 20 20 20  0x43 ('C').**   
387bc 20 20 3e 3d 20 20 20 20 20 20 20 20 30 78 34 34    >=        0x44
387bd 20 28 27 44 27 29 0a 2a 2a 20 20 20 20 20 20 3e   ('D').**      >
387be 20 20 20 20 20 20 20 20 30 78 34 35 20 28 27 45          0x45 ('E
387bf 27 29 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d  ').**   --------
387c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
387c1 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  *.** The second 
387c2 6f 66 20 65 61 63 68 20 70 61 69 72 20 6f 66 20  of each pair of 
387c3 62 79 74 65 73 20 69 64 65 6e 74 69 66 69 65 73  bytes identifies
387c4 20 74 68 65 20 63 6f 6f 72 64 69 6e 61 74 65 20   the coordinate 
387c5 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 6f 20 77 68 69  column.** to whi
387c6 63 68 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  ch the constrain
387c7 74 20 61 70 70 6c 69 65 73 2e 20 54 68 65 20 6c  t applies. The l
387c8 65 66 74 6d 6f 73 74 20 63 6f 6f 72 64 69 6e 61  eftmost coordina
387c9 74 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20  te column.** is 
387ca 27 61 27 2c 20 74 68 65 20 73 65 63 6f 6e 64 20  'a', the second 
387cb 66 72 6f 6d 20 74 68 65 20 6c 65 66 74 20 27 62  from the left 'b
387cc 27 20 65 74 63 2e 0a 2a 2f 0a 73 74 61 74 69 63  ' etc..*/.static
387cd 20 69 6e 74 20 72 74 72 65 65 42 65 73 74 49 6e   int rtreeBestIn
387ce 64 65 78 28 73 71 6c 69 74 65 33 5f 76 74 61 62  dex(sqlite3_vtab
387cf 20 2a 74 61 62 2c 20 73 71 6c 69 74 65 33 5f 69   *tab, sqlite3_i
387d0 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49 64 78 49  ndex_info *pIdxI
387d1 6e 66 6f 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  nfo){.  int rc =
387d2 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e   SQLITE_OK;.  in
387d3 74 20 69 69 2c 20 63 43 6f 6c 3b 0a 0a 20 20 69  t ii, cCol;..  i
387d4 6e 74 20 69 49 64 78 20 3d 20 30 3b 0a 20 20 63  nt iIdx = 0;.  c
387d5 68 61 72 20 7a 49 64 78 53 74 72 5b 52 54 52 45  har zIdxStr[RTRE
387d6 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53  E_MAX_DIMENSIONS
387d7 2a 38 2b 31 5d 3b 0a 20 20 6d 65 6d 73 65 74 28  *8+1];.  memset(
387d8 7a 49 64 78 53 74 72 2c 20 30 2c 20 73 69 7a 65  zIdxStr, 0, size
387d9 6f 66 28 7a 49 64 78 53 74 72 29 29 3b 0a 0a 20  of(zIdxStr));.. 
387da 20 61 73 73 65 72 74 28 20 70 49 64 78 49 6e 66   assert( pIdxInf
387db 6f 2d 3e 69 64 78 53 74 72 3d 3d 30 20 29 3b 0a  o->idxStr==0 );.
387dc 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70    for(ii=0; ii<p
387dd 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72  IdxInfo->nConstr
387de 61 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  aint; ii++){.   
387df 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
387e0 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74  index_constraint
387e1 20 2a 70 20 3d 20 26 70 49 64 78 49 6e 66 6f 2d   *p = &pIdxInfo-
387e2 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 69 5d  >aConstraint[ii]
387e3 3b 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 73  ;..    if( p->us
387e4 61 62 6c 65 20 26 26 20 70 2d 3e 69 43 6f 6c 75  able && p->iColu
387e5 6d 6e 3d 3d 30 20 26 26 20 70 2d 3e 6f 70 3d 3d  mn==0 && p->op==
387e6 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
387e7 53 54 52 41 49 4e 54 5f 45 51 20 29 7b 0a 20 20  STRAINT_EQ ){.  
387e8 20 20 20 20 2f 2a 20 57 65 20 68 61 76 65 20 61      /* We have a
387e9 6e 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74  n equality const
387ea 72 61 69 6e 74 20 6f 6e 20 74 68 65 20 72 6f 77  raint on the row
387eb 69 64 2e 20 55 73 65 20 73 74 72 61 74 65 67 79  id. Use strategy
387ec 20 31 2e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74   1. */.      int
387ed 20 6a 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a   jj;.      for(j
387ee 6a 3d 30 3b 20 6a 6a 3c 69 69 3b 20 6a 6a 2b 2b  j=0; jj<ii; jj++
387ef 29 7b 0a 20 20 20 20 20 20 20 20 70 49 64 78 49  ){.        pIdxI
387f0 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  nfo->aConstraint
387f1 55 73 61 67 65 5b 6a 6a 5d 2e 61 72 67 76 49 6e  Usage[jj].argvIn
387f2 64 65 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  dex = 0;.       
387f3 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73   pIdxInfo->aCons
387f4 74 72 61 69 6e 74 55 73 61 67 65 5b 6a 6a 5d 2e  traintUsage[jj].
387f5 6f 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  omit = 0;.      
387f6 7d 0a 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f  }.      pIdxInfo
387f7 2d 3e 69 64 78 4e 75 6d 20 3d 20 31 3b 0a 20 20  ->idxNum = 1;.  
387f8 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43      pIdxInfo->aC
387f9 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69  onstraintUsage[i
387fa 69 5d 2e 61 72 67 76 49 6e 64 65 78 20 3d 20 31  i].argvIndex = 1
387fb 3b 0a 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f  ;.      pIdxInfo
387fc 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61  ->aConstraintUsa
387fd 67 65 5b 6a 6a 5d 2e 6f 6d 69 74 20 3d 20 31 3b  ge[jj].omit = 1;
387fe 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20  ..      /* This 
387ff 73 74 72 61 74 65 67 79 20 69 6e 76 6f 6c 76 65  strategy involve
38800 73 20 61 20 74 77 6f 20 72 6f 77 69 64 20 6c 6f  s a two rowid lo
38801 6f 6b 75 70 73 20 6f 6e 20 61 6e 20 42 2d 54 72  okups on an B-Tr
38802 65 65 20 73 74 72 75 63 74 75 72 65 73 0a 20 20  ee structures.  
38803 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 6e 20      ** and then 
38804 61 20 6c 69 6e 65 61 72 20 73 65 61 72 63 68 20  a linear search 
38805 6f 66 20 61 6e 20 52 2d 54 72 65 65 20 6e 6f 64  of an R-Tree nod
38806 65 2e 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62  e. This should b
38807 65 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73  e .      ** cons
38808 69 64 65 72 65 64 20 61 6c 6d 6f 73 74 20 61 73  idered almost as
38809 20 71 75 69 63 6b 20 61 73 20 61 20 64 69 72 65   quick as a dire
3880a 63 74 20 72 6f 77 69 64 20 6c 6f 6f 6b 75 70 20  ct rowid lookup 
3880b 28 66 6f 72 20 77 68 69 63 68 20 0a 20 20 20 20  (for which .    
3880c 20 20 2a 2a 20 73 71 6c 69 74 65 20 75 73 65 73    ** sqlite uses
3880d 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 63 6f 73   an internal cos
3880e 74 20 6f 66 20 30 2e 30 29 2e 0a 20 20 20 20 20  t of 0.0)..     
3880f 20 2a 2f 20 0a 20 20 20 20 20 20 70 49 64 78 49   */ .      pIdxI
38810 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f  nfo->estimatedCo
38811 73 74 20 3d 20 31 30 2e 30 3b 0a 20 20 20 20 20  st = 10.0;.     
38812 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
38813 4b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  K;.    }..    if
38814 28 20 70 2d 3e 75 73 61 62 6c 65 20 26 26 20 70  ( p->usable && p
38815 2d 3e 69 43 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20  ->iColumn>0 ){. 
38816 20 20 20 20 20 75 38 20 6f 70 20 3d 20 30 3b 0a       u8 op = 0;.
38817 20 20 20 20 20 20 73 77 69 74 63 68 28 20 70 2d        switch( p-
38818 3e 6f 70 20 29 7b 0a 20 20 20 20 20 20 20 20 63  >op ){.        c
38819 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  ase SQLITE_INDEX
3881a 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51 3a 20  _CONSTRAINT_EQ: 
3881b 6f 70 20 3d 20 52 54 52 45 45 5f 45 51 3b 20 62  op = RTREE_EQ; b
3881c 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61  reak;.        ca
3881d 73 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  se SQLITE_INDEX_
3881e 43 4f 4e 53 54 52 41 49 4e 54 5f 47 54 3a 20 6f  CONSTRAINT_GT: o
3881f 70 20 3d 20 52 54 52 45 45 5f 47 54 3b 20 62 72  p = RTREE_GT; br
38820 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
38821 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  e SQLITE_INDEX_C
38822 4f 4e 53 54 52 41 49 4e 54 5f 4c 45 3a 20 6f 70  ONSTRAINT_LE: op
38823 20 3d 20 52 54 52 45 45 5f 4c 45 3b 20 62 72 65   = RTREE_LE; bre
38824 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
38825 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f   SQLITE_INDEX_CO
38826 4e 53 54 52 41 49 4e 54 5f 4c 54 3a 20 6f 70 20  NSTRAINT_LT: op 
38827 3d 20 52 54 52 45 45 5f 4c 54 3b 20 62 72 65 61  = RTREE_LT; brea
38828 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k;.        case 
38829 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
3882a 53 54 52 41 49 4e 54 5f 47 45 3a 20 6f 70 20 3d  STRAINT_GE: op =
3882b 20 52 54 52 45 45 5f 47 45 3b 20 62 72 65 61 6b   RTREE_GE; break
3882c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
3882d 69 66 28 20 6f 70 20 29 7b 0a 20 20 20 20 20 20  if( op ){.      
3882e 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74    /* Make sure t
3882f 68 69 73 20 70 61 72 74 69 63 75 6c 61 72 20 63  his particular c
38830 6f 6e 73 74 72 61 69 6e 74 20 68 61 73 20 6e 6f  onstraint has no
38831 74 20 62 65 65 6e 20 75 73 65 64 20 62 65 66 6f  t been used befo
38832 72 65 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 49  re..        ** I
38833 66 20 69 74 20 68 61 73 20 62 65 65 6e 20 75 73  f it has been us
38834 65 64 20 62 65 66 6f 72 65 2c 20 69 67 6e 6f 72  ed before, ignor
38835 65 20 69 74 2e 0a 20 20 20 20 20 20 20 20 2a 2a  e it..        **
38836 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 20 3c 3d  .        ** A <=
38837 20 6f 72 20 3c 20 63 61 6e 20 62 65 20 75 73 65   or < can be use
38838 64 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  d if there is a 
38839 70 72 69 6f 72 20 3e 3d 20 6f 72 20 3e 2e 0a 20  prior >= or >.. 
3883a 20 20 20 20 20 20 20 2a 2a 20 41 20 3e 3d 20 6f         ** A >= o
3883b 72 20 3e 20 63 61 6e 20 62 65 20 75 73 65 64 20  r > can be used 
3883c 69 66 20 74 68 65 72 65 20 69 73 20 61 20 70 72  if there is a pr
3883d 69 6f 72 20 3c 20 6f 72 20 3c 3d 2e 0a 20 20 20  ior < or <=..   
3883e 20 20 20 20 20 2a 2a 20 41 20 3c 3d 20 6f 72 20       ** A <= or 
3883f 3c 20 69 73 20 64 69 73 71 75 61 6c 69 66 69 65  < is disqualifie
38840 64 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  d if there is a 
38841 70 72 69 6f 72 20 3c 3d 2c 20 3c 2c 20 6f 72 20  prior <=, <, or 
38842 3d 3d 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 41  ==..        ** A
38843 20 3e 3d 20 6f 72 20 3e 20 69 73 20 64 69 73 71   >= or > is disq
38844 75 61 6c 69 66 69 65 64 20 69 66 20 74 68 65 72  ualified if ther
38845 65 20 69 73 20 61 20 70 72 69 6f 72 20 3e 3d 2c  e is a prior >=,
38846 20 3e 2c 20 6f 72 20 3d 3d 2e 0a 20 20 20 20 20   >, or ==..     
38847 20 20 20 2a 2a 20 41 20 3d 3d 20 69 73 20 64 69     ** A == is di
38848 73 71 75 61 6c 69 66 65 64 20 69 66 20 74 68 65  squalifed if the
38849 72 65 20 69 73 20 61 6e 79 20 70 72 69 6f 72 20  re is any prior 
3884a 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 20 20 20 20  constraint..    
3884b 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
3884c 6e 74 20 6a 2c 20 6f 70 6d 73 6b 3b 0a 20 20 20  nt j, opmsk;.   
3884d 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73       static cons
3884e 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
3884f 63 6f 6d 70 61 74 69 62 6c 65 5b 5d 20 3d 20 7b  compatible[] = {
38850 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20 32 2c 20   0, 0, 1, 1, 2, 
38851 32 20 7d 3b 0a 20 20 20 20 20 20 20 20 61 73 73  2 };.        ass
38852 65 72 74 28 20 63 6f 6d 70 61 74 69 62 6c 65 5b  ert( compatible[
38853 52 54 52 45 45 5f 45 51 20 26 20 37 5d 3d 3d 30  RTREE_EQ & 7]==0
38854 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65   );.        asse
38855 72 74 28 20 63 6f 6d 70 61 74 69 62 6c 65 5b 52  rt( compatible[R
38856 54 52 45 45 5f 4c 54 20 26 20 37 5d 3d 3d 31 20  TREE_LT & 7]==1 
38857 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
38858 74 28 20 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54  t( compatible[RT
38859 52 45 45 5f 4c 45 20 26 20 37 5d 3d 3d 31 20 29  REE_LE & 7]==1 )
3885a 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
3885b 28 20 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52  ( compatible[RTR
3885c 45 45 5f 47 54 20 26 20 37 5d 3d 3d 32 20 29 3b  EE_GT & 7]==2 );
3885d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
3885e 20 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52 45   compatible[RTRE
3885f 45 5f 47 45 20 26 20 37 5d 3d 3d 32 20 29 3b 0a  E_GE & 7]==2 );.
38860 20 20 20 20 20 20 20 20 63 43 6f 6c 20 3d 20 70          cCol = p
38861 2d 3e 69 43 6f 6c 75 6d 6e 20 2d 20 31 20 2b 20  ->iColumn - 1 + 
38862 27 61 27 3b 0a 20 20 20 20 20 20 20 20 6f 70 6d  'a';.        opm
38863 73 6b 20 3d 20 63 6f 6d 70 61 74 69 62 6c 65 5b  sk = compatible[
38864 6f 70 20 26 20 37 5d 3b 0a 20 20 20 20 20 20 20  op & 7];.       
38865 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 69 49 64 78   for(j=0; j<iIdx
38866 3b 20 6a 2b 3d 32 29 7b 0a 20 20 20 20 20 20 20  ; j+=2){.       
38867 20 20 20 69 66 28 20 7a 49 64 78 53 74 72 5b 6a     if( zIdxStr[j
38868 2b 31 5d 3d 3d 63 43 6f 6c 20 26 26 20 28 63 6f  +1]==cCol && (co
38869 6d 70 61 74 69 62 6c 65 5b 7a 49 64 78 53 74 72  mpatible[zIdxStr
3886a 5b 6a 5d 20 26 20 37 5d 20 26 20 6f 70 6d 73 6b  [j] & 7] & opmsk
3886b 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
3886c 20 20 20 20 6f 70 20 3d 20 30 3b 0a 20 20 20 20      op = 0;.    
3886d 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3886e 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
3886f 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
38870 20 20 20 69 66 28 20 6f 70 20 29 7b 0a 20 20 20     if( op ){.   
38871 20 20 20 20 20 61 73 73 65 72 74 28 20 69 49 64       assert( iId
38872 78 3c 73 69 7a 65 6f 66 28 7a 49 64 78 53 74 72  x<sizeof(zIdxStr
38873 29 2d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 7a  )-1 );.        z
38874 49 64 78 53 74 72 5b 69 49 64 78 2b 2b 5d 20 3d  IdxStr[iIdx++] =
38875 20 6f 70 3b 0a 20 20 20 20 20 20 20 20 7a 49 64   op;.        zId
38876 78 53 74 72 5b 69 49 64 78 2b 2b 5d 20 3d 20 63  xStr[iIdx++] = c
38877 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 64  Col;.        pId
38878 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  xInfo->aConstrai
38879 6e 74 55 73 61 67 65 5b 69 69 5d 2e 61 72 67 76  ntUsage[ii].argv
3887a 49 6e 64 65 78 20 3d 20 28 69 49 64 78 2f 32 29  Index = (iIdx/2)
3887b 3b 0a 20 20 20 20 20 20 20 20 70 49 64 78 49 6e  ;.        pIdxIn
3887c 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55  fo->aConstraintU
3887d 73 61 67 65 5b 69 69 5d 2e 6f 6d 69 74 20 3d 20  sage[ii].omit = 
3887e 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
3887f 0a 20 20 7d 0a 0a 20 20 70 49 64 78 49 6e 66 6f  .  }..  pIdxInfo
38880 2d 3e 69 64 78 4e 75 6d 20 3d 20 32 3b 0a 20 20  ->idxNum = 2;.  
38881 70 49 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f  pIdxInfo->needTo
38882 46 72 65 65 49 64 78 53 74 72 20 3d 20 31 3b 0a  FreeIdxStr = 1;.
38883 20 20 69 66 28 20 69 49 64 78 3e 30 20 26 26 20    if( iIdx>0 && 
38884 30 3d 3d 28 70 49 64 78 49 6e 66 6f 2d 3e 69 64  0==(pIdxInfo->id
38885 78 53 74 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d  xStr = sqlite3_m
38886 70 72 69 6e 74 66 28 22 25 73 22 2c 20 7a 49 64  printf("%s", zId
38887 78 53 74 72 29 29 20 29 7b 0a 20 20 20 20 72 65  xStr)) ){.    re
38888 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
38889 4d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  M;.  }.  assert(
3888a 20 69 49 64 78 3e 3d 30 20 29 3b 0a 20 20 70 49   iIdx>=0 );.  pI
3888b 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65  dxInfo->estimate
3888c 64 43 6f 73 74 20 3d 20 28 32 30 30 30 30 30 30  dCost = (2000000
3888d 2e 30 20 2f 20 28 64 6f 75 62 6c 65 29 28 69 49  .0 / (double)(iI
3888e 64 78 20 2b 20 31 29 29 3b 0a 20 20 72 65 74 75  dx + 1));.  retu
3888f 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
38890 52 65 74 75 72 6e 20 74 68 65 20 4e 2d 64 69 6d  Return the N-dim
38891 65 6e 73 69 6f 6e 61 6c 20 76 6f 6c 75 6d 6e 20  ensional volumn 
38892 6f 66 20 74 68 65 20 63 65 6c 6c 20 73 74 6f 72  of the cell stor
38893 65 64 20 69 6e 20 2a 70 2e 0a 2a 2f 0a 73 74 61  ed in *p..*/.sta
38894 74 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c 41 72  tic float cellAr
38895 65 61 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  ea(Rtree *pRtree
38896 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 29 7b  , RtreeCell *p){
38897 0a 20 20 66 6c 6f 61 74 20 61 72 65 61 20 3d 20  .  float area = 
38898 31 2e 30 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  1.0;.  int ii;. 
38899 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70   for(ii=0; ii<(p
3889a 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20  Rtree->nDim*2); 
3889b 69 69 2b 3d 32 29 7b 0a 20 20 20 20 61 72 65 61  ii+=2){.    area
3889c 20 3d 20 61 72 65 61 20 2a 20 28 44 43 4f 4f 52   = area * (DCOOR
3889d 44 28 70 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31  D(p->aCoord[ii+1
3889e 5d 29 20 2d 20 44 43 4f 4f 52 44 28 70 2d 3e 61  ]) - DCOORD(p->a
3889f 43 6f 6f 72 64 5b 69 69 5d 29 29 3b 0a 20 20 7d  Coord[ii]));.  }
388a0 0a 20 20 72 65 74 75 72 6e 20 61 72 65 61 3b 0a  .  return area;.
388a1 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
388a2 74 68 65 20 6d 61 72 67 69 6e 20 6c 65 6e 67 74  the margin lengt
388a3 68 20 6f 66 20 63 65 6c 6c 20 70 2e 20 54 68 65  h of cell p. The
388a4 20 6d 61 72 67 69 6e 20 6c 65 6e 67 74 68 20 69   margin length i
388a5 73 20 74 68 65 20 73 75 6d 0a 2a 2a 20 6f 66 20  s the sum.** of 
388a6 74 68 65 20 6f 62 6a 65 63 74 73 20 73 69 7a 65  the objects size
388a7 20 69 6e 20 65 61 63 68 20 64 69 6d 65 6e 73 69   in each dimensi
388a8 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 66 6c  on..*/.static fl
388a9 6f 61 74 20 63 65 6c 6c 4d 61 72 67 69 6e 28 52  oat cellMargin(R
388aa 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74  tree *pRtree, Rt
388ab 72 65 65 43 65 6c 6c 20 2a 70 29 7b 0a 20 20 66  reeCell *p){.  f
388ac 6c 6f 61 74 20 6d 61 72 67 69 6e 20 3d 20 30 2e  loat margin = 0.
388ad 30 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 66  0;.  int ii;.  f
388ae 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74  or(ii=0; ii<(pRt
388af 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69  ree->nDim*2); ii
388b0 2b 3d 32 29 7b 0a 20 20 20 20 6d 61 72 67 69 6e  +=2){.    margin
388b1 20 2b 3d 20 28 44 43 4f 4f 52 44 28 70 2d 3e 61   += (DCOORD(p->a
388b2 43 6f 6f 72 64 5b 69 69 2b 31 5d 29 20 2d 20 44  Coord[ii+1]) - D
388b3 43 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64 5b  COORD(p->aCoord[
388b4 69 69 5d 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ii]));.  }.  ret
388b5 75 72 6e 20 6d 61 72 67 69 6e 3b 0a 7d 0a 0a 2f  urn margin;.}../
388b6 2a 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 75  *.** Store the u
388b7 6e 69 6f 6e 20 6f 66 20 63 65 6c 6c 73 20 70 31  nion of cells p1
388b8 20 61 6e 64 20 70 32 20 69 6e 20 70 31 2e 0a 2a   and p2 in p1..*
388b9 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 65  /.static void ce
388ba 6c 6c 55 6e 69 6f 6e 28 52 74 72 65 65 20 2a 70  llUnion(Rtree *p
388bb 52 74 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c  Rtree, RtreeCell
388bc 20 2a 70 31 2c 20 52 74 72 65 65 43 65 6c 6c 20   *p1, RtreeCell 
388bd 2a 70 32 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a  *p2){.  int ii;.
388be 20 20 69 66 28 20 70 52 74 72 65 65 2d 3e 65 43    if( pRtree->eC
388bf 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52 45 45 5f  oordType==RTREE_
388c0 43 4f 4f 52 44 5f 52 45 41 4c 33 32 20 29 7b 0a  COORD_REAL32 ){.
388c1 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69      for(ii=0; ii
388c2 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
388c3 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 20  ); ii+=2){.     
388c4 20 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e   p1->aCoord[ii].
388c5 66 20 3d 20 4d 49 4e 28 70 31 2d 3e 61 43 6f 6f  f = MIN(p1->aCoo
388c6 72 64 5b 69 69 5d 2e 66 2c 20 70 32 2d 3e 61 43  rd[ii].f, p2->aC
388c7 6f 6f 72 64 5b 69 69 5d 2e 66 29 3b 0a 20 20 20  oord[ii].f);.   
388c8 20 20 20 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69     p1->aCoord[ii
388c9 2b 31 5d 2e 66 20 3d 20 4d 41 58 28 70 31 2d 3e  +1].f = MAX(p1->
388ca 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 2c 20  aCoord[ii+1].f, 
388cb 70 32 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d  p2->aCoord[ii+1]
388cc 2e 66 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  .f);.    }.  }el
388cd 73 65 7b 0a 20 20 20 20 66 6f 72 28 69 69 3d 30  se{.    for(ii=0
388ce 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44  ; ii<(pRtree->nD
388cf 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20  im*2); ii+=2){. 
388d0 20 20 20 20 20 70 31 2d 3e 61 43 6f 6f 72 64 5b       p1->aCoord[
388d1 69 69 5d 2e 69 20 3d 20 4d 49 4e 28 70 31 2d 3e  ii].i = MIN(p1->
388d2 61 43 6f 6f 72 64 5b 69 69 5d 2e 69 2c 20 70 32  aCoord[ii].i, p2
388d3 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e 69 29 3b  ->aCoord[ii].i);
388d4 0a 20 20 20 20 20 20 70 31 2d 3e 61 43 6f 6f 72  .      p1->aCoor
388d5 64 5b 69 69 2b 31 5d 2e 69 20 3d 20 4d 41 58 28  d[ii+1].i = MAX(
388d6 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d  p1->aCoord[ii+1]
388d7 2e 69 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b 69  .i, p2->aCoord[i
388d8 69 2b 31 5d 2e 69 29 3b 0a 20 20 20 20 7d 0a 20  i+1].i);.    }. 
388d9 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75   }.}../*.** Retu
388da 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 61  rn true if the a
388db 72 65 61 20 63 6f 76 65 72 65 64 20 62 79 20 70  rea covered by p
388dc 32 20 69 73 20 61 20 73 75 62 73 65 74 20 6f 66  2 is a subset of
388dd 20 74 68 65 20 61 72 65 61 20 63 6f 76 65 72 65   the area covere
388de 64 0a 2a 2a 20 62 79 20 70 31 2e 20 46 61 6c 73  d.** by p1. Fals
388df 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a  e otherwise..*/.
388e0 73 74 61 74 69 63 20 69 6e 74 20 63 65 6c 6c 43  static int cellC
388e1 6f 6e 74 61 69 6e 73 28 52 74 72 65 65 20 2a 70  ontains(Rtree *p
388e2 52 74 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c  Rtree, RtreeCell
388e3 20 2a 70 31 2c 20 52 74 72 65 65 43 65 6c 6c 20   *p1, RtreeCell 
388e4 2a 70 32 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a  *p2){.  int ii;.
388e5 20 20 69 6e 74 20 69 73 49 6e 74 20 3d 20 28 70    int isInt = (p
388e6 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79 70  Rtree->eCoordTyp
388e7 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52 44 5f 49  e==RTREE_COORD_I
388e8 4e 54 33 32 29 3b 0a 20 20 66 6f 72 28 69 69 3d  NT32);.  for(ii=
388e9 30 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e  0; ii<(pRtree->n
388ea 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a  Dim*2); ii+=2){.
388eb 20 20 20 20 52 74 72 65 65 43 6f 6f 72 64 20 2a      RtreeCoord *
388ec 61 31 20 3d 20 26 70 31 2d 3e 61 43 6f 6f 72 64  a1 = &p1->aCoord
388ed 5b 69 69 5d 3b 0a 20 20 20 20 52 74 72 65 65 43  [ii];.    RtreeC
388ee 6f 6f 72 64 20 2a 61 32 20 3d 20 26 70 32 2d 3e  oord *a2 = &p2->
388ef 61 43 6f 6f 72 64 5b 69 69 5d 3b 0a 20 20 20 20  aCoord[ii];.    
388f0 69 66 28 20 28 21 69 73 49 6e 74 20 26 26 20 28  if( (!isInt && (
388f1 61 32 5b 30 5d 2e 66 3c 61 31 5b 30 5d 2e 66 20  a2[0].f<a1[0].f 
388f2 7c 7c 20 61 32 5b 31 5d 2e 66 3e 61 31 5b 31 5d  || a2[1].f>a1[1]
388f3 2e 66 29 29 20 0a 20 20 20 20 20 7c 7c 20 28 20  .f)) .     || ( 
388f4 69 73 49 6e 74 20 26 26 20 28 61 32 5b 30 5d 2e  isInt && (a2[0].
388f5 69 3c 61 31 5b 30 5d 2e 69 20 7c 7c 20 61 32 5b  i<a1[0].i || a2[
388f6 31 5d 2e 69 3e 61 31 5b 31 5d 2e 69 29 29 20 0a  1].i>a1[1].i)) .
388f7 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74      ){.      ret
388f8 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 0;.    }.  }
388f9 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
388fa 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
388fb 20 61 6d 6f 75 6e 74 20 63 65 6c 6c 20 70 20 77   amount cell p w
388fc 6f 75 6c 64 20 67 72 6f 77 20 62 79 20 69 66 20  ould grow by if 
388fd 69 74 20 77 65 72 65 20 75 6e 69 6f 6e 65 64 20  it were unioned 
388fe 77 69 74 68 20 70 43 65 6c 6c 2e 0a 2a 2f 0a 73  with pCell..*/.s
388ff 74 61 74 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c  tatic float cell
38900 47 72 6f 77 74 68 28 52 74 72 65 65 20 2a 70 52  Growth(Rtree *pR
38901 74 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c 20  tree, RtreeCell 
38902 2a 70 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70  *p, RtreeCell *p
38903 43 65 6c 6c 29 7b 0a 20 20 66 6c 6f 61 74 20 61  Cell){.  float a
38904 72 65 61 3b 0a 20 20 52 74 72 65 65 43 65 6c 6c  rea;.  RtreeCell
38905 20 63 65 6c 6c 3b 0a 20 20 6d 65 6d 63 70 79 28   cell;.  memcpy(
38906 26 63 65 6c 6c 2c 20 70 2c 20 73 69 7a 65 6f 66  &cell, p, sizeof
38907 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20  (RtreeCell));.  
38908 61 72 65 61 20 3d 20 63 65 6c 6c 41 72 65 61 28  area = cellArea(
38909 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 29 3b 0a  pRtree, &cell);.
3890a 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72    cellUnion(pRtr
3890b 65 65 2c 20 26 63 65 6c 6c 2c 20 70 43 65 6c 6c  ee, &cell, pCell
3890c 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 63 65 6c  );.  return (cel
3890d 6c 41 72 65 61 28 70 52 74 72 65 65 2c 20 26 63  lArea(pRtree, &c
3890e 65 6c 6c 29 2d 61 72 65 61 29 3b 0a 7d 0a 0a 23  ell)-area);.}..#
3890f 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52  if VARIANT_RSTAR
38910 54 52 45 45 5f 43 48 4f 4f 53 45 53 55 42 54 52  TREE_CHOOSESUBTR
38911 45 45 20 7c 7c 20 56 41 52 49 41 4e 54 5f 52 53  EE || VARIANT_RS
38912 54 41 52 54 52 45 45 5f 53 50 4c 49 54 0a 73 74  TARTREE_SPLIT.st
38913 61 74 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c 4f  atic float cellO
38914 76 65 72 6c 61 70 28 0a 20 20 52 74 72 65 65 20  verlap(.  Rtree 
38915 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65  *pRtree, .  Rtre
38916 65 43 65 6c 6c 20 2a 70 2c 20 0a 20 20 52 74 72  eCell *p, .  Rtr
38917 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a  eeCell *aCell, .
38918 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20    int nCell, .  
38919 69 6e 74 20 69 45 78 63 6c 75 64 65 0a 29 7b 0a  int iExclude.){.
3891a 20 20 69 6e 74 20 69 69 3b 0a 20 20 66 6c 6f 61    int ii;.  floa
3891b 74 20 6f 76 65 72 6c 61 70 20 3d 20 30 2e 30 3b  t overlap = 0.0;
3891c 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c  .  for(ii=0; ii<
3891d 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20  nCell; ii++){.  
3891e 20 20 69 66 28 20 69 69 21 3d 69 45 78 63 6c 75    if( ii!=iExclu
3891f 64 65 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  de ){.      int 
38920 6a 6a 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20  jj;.      float 
38921 6f 20 3d 20 31 2e 30 3b 0a 20 20 20 20 20 20 66  o = 1.0;.      f
38922 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 28 70 52 74  or(jj=0; jj<(pRt
38923 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 6a 6a  ree->nDim*2); jj
38924 2b 3d 32 29 7b 0a 20 20 20 20 20 20 20 20 64 6f  +=2){.        do
38925 75 62 6c 65 20 78 31 3b 0a 20 20 20 20 20 20 20  uble x1;.       
38926 20 64 6f 75 62 6c 65 20 78 32 3b 0a 0a 20 20 20   double x2;..   
38927 20 20 20 20 20 78 31 20 3d 20 4d 41 58 28 44 43       x1 = MAX(DC
38928 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64 5b 6a  OORD(p->aCoord[j
38929 6a 5d 29 2c 20 44 43 4f 4f 52 44 28 61 43 65 6c  j]), DCOORD(aCel
3892a 6c 5b 69 69 5d 2e 61 43 6f 6f 72 64 5b 6a 6a 5d  l[ii].aCoord[jj]
3892b 29 29 3b 0a 20 20 20 20 20 20 20 20 78 32 20 3d  ));.        x2 =
3892c 20 4d 49 4e 28 44 43 4f 4f 52 44 28 70 2d 3e 61   MIN(DCOORD(p->a
3892d 43 6f 6f 72 64 5b 6a 6a 2b 31 5d 29 2c 20 44 43  Coord[jj+1]), DC
3892e 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61  OORD(aCell[ii].a
3892f 43 6f 6f 72 64 5b 6a 6a 2b 31 5d 29 29 3b 0a 0a  Coord[jj+1]));..
38930 20 20 20 20 20 20 20 20 69 66 28 20 78 32 3c 78          if( x2<x
38931 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6f  1 ){.          o
38932 20 3d 20 30 2e 30 3b 0a 20 20 20 20 20 20 20 20   = 0.0;.        
38933 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
38934 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
38935 20 20 6f 20 3d 20 6f 20 2a 20 28 78 32 2d 78 31    o = o * (x2-x1
38936 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
38937 20 20 20 7d 0a 20 20 20 20 20 20 6f 76 65 72 6c     }.      overl
38938 61 70 20 2b 3d 20 6f 3b 0a 20 20 20 20 7d 0a 20  ap += o;.    }. 
38939 20 7d 0a 20 20 72 65 74 75 72 6e 20 6f 76 65 72   }.  return over
3893a 6c 61 70 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  lap;.}.#endif..#
3893b 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52  if VARIANT_RSTAR
3893c 54 52 45 45 5f 43 48 4f 4f 53 45 53 55 42 54 52  TREE_CHOOSESUBTR
3893d 45 45 0a 73 74 61 74 69 63 20 66 6c 6f 61 74 20  EE.static float 
3893e 63 65 6c 6c 4f 76 65 72 6c 61 70 45 6e 6c 61 72  cellOverlapEnlar
3893f 67 65 6d 65 6e 74 28 0a 20 20 52 74 72 65 65 20  gement(.  Rtree 
38940 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65  *pRtree, .  Rtre
38941 65 43 65 6c 6c 20 2a 70 2c 20 0a 20 20 52 74 72  eCell *p, .  Rtr
38942 65 65 43 65 6c 6c 20 2a 70 49 6e 73 65 72 74 2c  eeCell *pInsert,
38943 20 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61   .  RtreeCell *a
38944 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65  Cell, .  int nCe
38945 6c 6c 2c 20 0a 20 20 69 6e 74 20 69 45 78 63 6c  ll, .  int iExcl
38946 75 64 65 0a 29 7b 0a 20 20 66 6c 6f 61 74 20 62  ude.){.  float b
38947 65 66 6f 72 65 3b 0a 20 20 66 6c 6f 61 74 20 61  efore;.  float a
38948 66 74 65 72 3b 0a 20 20 62 65 66 6f 72 65 20 3d  fter;.  before =
38949 20 63 65 6c 6c 4f 76 65 72 6c 61 70 28 70 52 74   cellOverlap(pRt
3894a 72 65 65 2c 20 70 2c 20 61 43 65 6c 6c 2c 20 6e  ree, p, aCell, n
3894b 43 65 6c 6c 2c 20 69 45 78 63 6c 75 64 65 29 3b  Cell, iExclude);
3894c 0a 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74  .  cellUnion(pRt
3894d 72 65 65 2c 20 70 2c 20 70 49 6e 73 65 72 74 29  ree, p, pInsert)
3894e 3b 0a 20 20 61 66 74 65 72 20 3d 20 63 65 6c 6c  ;.  after = cell
3894f 4f 76 65 72 6c 61 70 28 70 52 74 72 65 65 2c 20  Overlap(pRtree, 
38950 70 2c 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c  p, aCell, nCell,
38951 20 69 45 78 63 6c 75 64 65 29 3b 0a 20 20 72 65   iExclude);.  re
38952 74 75 72 6e 20 61 66 74 65 72 2d 62 65 66 6f 72  turn after-befor
38953 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  e;.}.#endif.../*
38954 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
38955 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  n implements the
38956 20 43 68 6f 6f 73 65 4c 65 61 66 20 61 6c 67 6f   ChooseLeaf algo
38957 72 69 74 68 6d 20 66 72 6f 6d 20 47 75 74 6d 61  rithm from Gutma
38958 6e 5b 38 34 5d 2e 0a 2a 2a 20 43 68 6f 6f 73 65  n[84]..** Choose
38959 53 75 62 54 72 65 65 20 69 6e 20 72 2a 74 72 65  SubTree in r*tre
3895a 65 20 74 65 72 6d 69 6e 6f 6c 6f 67 79 2e 0a 2a  e terminology..*
3895b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 43 68 6f  /.static int Cho
3895c 6f 73 65 4c 65 61 66 28 0a 20 20 52 74 72 65 65  oseLeaf(.  Rtree
3895d 20 2a 70 52 74 72 65 65 2c 20 20 20 20 20 20 20   *pRtree,       
3895e 20 20 20 20 20 20 20 20 2f 2a 20 52 74 72 65 65          /* Rtree
3895f 20 74 61 62 6c 65 20 2a 2f 0a 20 20 52 74 72 65   table */.  Rtre
38960 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c 20 20 20  eCell *pCell,   
38961 20 20 20 20 20 20 20 20 20 2f 2a 20 43 65 6c 6c           /* Cell
38962 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20   to insert into 
38963 72 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69  rtree */.  int i
38964 48 65 69 67 68 74 2c 20 20 20 20 20 20 20 20 20  Height,         
38965 20 20 20 20 20 20 20 20 2f 2a 20 48 65 69 67 68          /* Heigh
38966 74 20 6f 66 20 73 75 62 2d 74 72 65 65 20 72 6f  t of sub-tree ro
38967 6f 74 65 64 20 61 74 20 70 43 65 6c 6c 20 2a 2f  oted at pCell */
38968 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 2a 70  .  RtreeNode **p
38969 70 4c 65 61 66 20 20 20 20 20 20 20 20 20 20 20  pLeaf           
3896a 2f 2a 20 4f 55 54 3a 20 53 65 6c 65 63 74 65 64  /* OUT: Selected
3896b 20 6c 65 61 66 20 70 61 67 65 20 2a 2f 0a 29 7b   leaf page */.){
3896c 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74  .  int rc;.  int
3896d 20 69 69 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65   ii;.  RtreeNode
3896e 20 2a 70 4e 6f 64 65 3b 0a 20 20 72 63 20 3d 20   *pNode;.  rc = 
3896f 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52 74 72  nodeAcquire(pRtr
38970 65 65 2c 20 31 2c 20 30 2c 20 26 70 4e 6f 64 65  ee, 1, 0, &pNode
38971 29 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  );..  for(ii=0; 
38972 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
38973 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 69 44 65   ii<(pRtree->iDe
38974 70 74 68 2d 69 48 65 69 67 68 74 29 3b 20 69 69  pth-iHeight); ii
38975 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 69 43 65  ++){.    int iCe
38976 6c 6c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ll;.    sqlite3_
38977 69 6e 74 36 34 20 69 42 65 73 74 3b 0a 0a 20 20  int64 iBest;..  
38978 20 20 66 6c 6f 61 74 20 66 4d 69 6e 47 72 6f 77    float fMinGrow
38979 74 68 3b 0a 20 20 20 20 66 6c 6f 61 74 20 66 4d  th;.    float fM
3897a 69 6e 41 72 65 61 3b 0a 20 20 20 20 66 6c 6f 61  inArea;.    floa
3897b 74 20 66 4d 69 6e 4f 76 65 72 6c 61 70 3b 0a 0a  t fMinOverlap;..
3897c 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20      int nCell = 
3897d 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 20 20  NCELL(pNode);.  
3897e 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c    RtreeCell cell
3897f 3b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20  ;.    RtreeNode 
38980 2a 70 43 68 69 6c 64 3b 0a 0a 20 20 20 20 52 74  *pChild;..    Rt
38981 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 20 3d  reeCell *aCell =
38982 20 30 3b 0a 0a 23 69 66 20 56 41 52 49 41 4e 54   0;..#if VARIANT
38983 5f 52 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53  _RSTARTREE_CHOOS
38984 45 53 55 42 54 52 45 45 0a 20 20 20 20 69 66 28  ESUBTREE.    if(
38985 20 69 69 3d 3d 28 70 52 74 72 65 65 2d 3e 69 44   ii==(pRtree->iD
38986 65 70 74 68 2d 31 29 20 29 7b 0a 20 20 20 20 20  epth-1) ){.     
38987 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20 61   int jj;.      a
38988 43 65 6c 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Cell = sqlite3_m
38989 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74 72  alloc(sizeof(Rtr
3898a 65 65 43 65 6c 6c 29 2a 6e 43 65 6c 6c 29 3b 0a  eeCell)*nCell);.
3898b 20 20 20 20 20 20 69 66 28 20 21 61 43 65 6c 6c        if( !aCell
3898c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
3898d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
3898e 20 20 20 20 20 20 20 6e 6f 64 65 52 65 6c 65 61         nodeRelea
3898f 73 65 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  se(pRtree, pNode
38990 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 6f 64 65  );.        pNode
38991 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 63 6f   = 0;.        co
38992 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a  ntinue;.      }.
38993 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20        for(jj=0; 
38994 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b  jj<nCell; jj++){
38995 0a 20 20 20 20 20 20 20 20 6e 6f 64 65 47 65 74  .        nodeGet
38996 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f  Cell(pRtree, pNo
38997 64 65 2c 20 6a 6a 2c 20 26 61 43 65 6c 6c 5b 6a  de, jj, &aCell[j
38998 6a 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  j]);.      }.   
38999 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f   }.#endif..    /
3899a 2a 20 53 65 6c 65 63 74 20 74 68 65 20 63 68 69  * Select the chi
3899b 6c 64 20 6e 6f 64 65 20 77 68 69 63 68 20 77 69  ld node which wi
3899c 6c 6c 20 62 65 20 65 6e 6c 61 72 67 65 64 20 74  ll be enlarged t
3899d 68 65 20 6c 65 61 73 74 20 69 66 20 70 43 65 6c  he least if pCel
3899e 6c 0a 20 20 20 20 2a 2a 20 69 73 20 69 6e 73 65  l.    ** is inse
3899f 72 74 65 64 20 69 6e 74 6f 20 69 74 2e 20 52 65  rted into it. Re
389a0 73 6f 6c 76 65 20 74 69 65 73 20 62 79 20 63 68  solve ties by ch
389a1 6f 6f 73 69 6e 67 20 74 68 65 20 65 6e 74 72 79  oosing the entry
389a2 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 74 68 65   with.    ** the
389a3 20 73 6d 61 6c 6c 65 73 74 20 61 72 65 61 2e 0a   smallest area..
389a4 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69      */.    for(i
389a5 43 65 6c 6c 3d 30 3b 20 69 43 65 6c 6c 3c 6e 43  Cell=0; iCell<nC
389a6 65 6c 6c 3b 20 69 43 65 6c 6c 2b 2b 29 7b 0a 20  ell; iCell++){. 
389a7 20 20 20 20 20 66 6c 6f 61 74 20 67 72 6f 77 74       float growt
389a8 68 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 61  h;.      float a
389a9 72 65 61 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74  rea;.      float
389aa 20 6f 76 65 72 6c 61 70 20 3d 20 30 2e 30 3b 0a   overlap = 0.0;.
389ab 20 20 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c        nodeGetCel
389ac 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c  l(pRtree, pNode,
389ad 20 69 43 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b 0a   iCell, &cell);.
389ae 20 20 20 20 20 20 67 72 6f 77 74 68 20 3d 20 63        growth = c
389af 65 6c 6c 47 72 6f 77 74 68 28 70 52 74 72 65 65  ellGrowth(pRtree
389b0 2c 20 26 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 3b  , &cell, pCell);
389b1 0a 20 20 20 20 20 20 61 72 65 61 20 3d 20 63 65  .      area = ce
389b2 6c 6c 41 72 65 61 28 70 52 74 72 65 65 2c 20 26  llArea(pRtree, &
389b3 63 65 6c 6c 29 3b 0a 23 69 66 20 56 41 52 49 41  cell);.#if VARIA
389b4 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 43 48 4f  NT_RSTARTREE_CHO
389b5 4f 53 45 53 55 42 54 52 45 45 0a 20 20 20 20 20  OSESUBTREE.     
389b6 20 69 66 28 20 69 69 3d 3d 28 70 52 74 72 65 65   if( ii==(pRtree
389b7 2d 3e 69 44 65 70 74 68 2d 31 29 20 29 7b 0a 20  ->iDepth-1) ){. 
389b8 20 20 20 20 20 20 20 6f 76 65 72 6c 61 70 20 3d         overlap =
389b9 20 63 65 6c 6c 4f 76 65 72 6c 61 70 45 6e 6c 61   cellOverlapEnla
389ba 72 67 65 6d 65 6e 74 28 70 52 74 72 65 65 2c 26  rgement(pRtree,&
389bb 63 65 6c 6c 2c 70 43 65 6c 6c 2c 61 43 65 6c 6c  cell,pCell,aCell
389bc 2c 6e 43 65 6c 6c 2c 69 43 65 6c 6c 29 3b 0a 20  ,nCell,iCell);. 
389bd 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
389be 20 20 20 20 69 66 28 20 28 69 43 65 6c 6c 3d 3d      if( (iCell==
389bf 30 29 20 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f  0) .       || (o
389c0 76 65 72 6c 61 70 3c 66 4d 69 6e 4f 76 65 72 6c  verlap<fMinOverl
389c1 61 70 29 20 0a 20 20 20 20 20 20 20 7c 7c 20 28  ap) .       || (
389c2 6f 76 65 72 6c 61 70 3d 3d 66 4d 69 6e 4f 76 65  overlap==fMinOve
389c3 72 6c 61 70 20 26 26 20 67 72 6f 77 74 68 3c 66  rlap && growth<f
389c4 4d 69 6e 47 72 6f 77 74 68 29 0a 20 20 20 20 20  MinGrowth).     
389c5 20 20 7c 7c 20 28 6f 76 65 72 6c 61 70 3d 3d 66    || (overlap==f
389c6 4d 69 6e 4f 76 65 72 6c 61 70 20 26 26 20 67 72  MinOverlap && gr
389c7 6f 77 74 68 3d 3d 66 4d 69 6e 47 72 6f 77 74 68  owth==fMinGrowth
389c8 20 26 26 20 61 72 65 61 3c 66 4d 69 6e 41 72 65   && area<fMinAre
389c9 61 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  a).      ){.    
389ca 20 20 20 20 66 4d 69 6e 4f 76 65 72 6c 61 70 20      fMinOverlap 
389cb 3d 20 6f 76 65 72 6c 61 70 3b 0a 20 20 20 20 20  = overlap;.     
389cc 20 20 20 66 4d 69 6e 47 72 6f 77 74 68 20 3d 20     fMinGrowth = 
389cd 67 72 6f 77 74 68 3b 0a 20 20 20 20 20 20 20 20  growth;.        
389ce 66 4d 69 6e 41 72 65 61 20 3d 20 61 72 65 61 3b  fMinArea = area;
389cf 0a 20 20 20 20 20 20 20 20 69 42 65 73 74 20 3d  .        iBest =
389d0 20 63 65 6c 6c 2e 69 52 6f 77 69 64 3b 0a 20 20   cell.iRowid;.  
389d1 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
389d2 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 43   sqlite3_free(aC
389d3 65 6c 6c 29 3b 0a 20 20 20 20 72 63 20 3d 20 6e  ell);.    rc = n
389d4 6f 64 65 41 63 71 75 69 72 65 28 70 52 74 72 65  odeAcquire(pRtre
389d5 65 2c 20 69 42 65 73 74 2c 20 70 4e 6f 64 65 2c  e, iBest, pNode,
389d6 20 26 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 6e   &pChild);.    n
389d7 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
389d8 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 70  e, pNode);.    p
389d9 4e 6f 64 65 20 3d 20 70 43 68 69 6c 64 3b 0a 20  Node = pChild;. 
389da 20 7d 0a 0a 20 20 2a 70 70 4c 65 61 66 20 3d 20   }..  *ppLeaf = 
389db 70 4e 6f 64 65 3b 0a 20 20 72 65 74 75 72 6e 20  pNode;.  return 
389dc 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 63  rc;.}../*.** A c
389dd 65 6c 6c 20 77 69 74 68 20 74 68 65 20 73 61 6d  ell with the sam
389de 65 20 63 6f 6e 74 65 6e 74 20 61 73 20 70 43 65  e content as pCe
389df 6c 6c 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e  ll has just been
389e0 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 0a 2a   inserted into.*
389e1 2a 20 74 68 65 20 6e 6f 64 65 20 70 4e 6f 64 65  * the node pNode
389e2 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
389e3 75 70 64 61 74 65 73 20 74 68 65 20 62 6f 75 6e  updates the boun
389e4 64 69 6e 67 20 62 6f 78 20 63 65 6c 6c 73 20 69  ding box cells i
389e5 6e 0a 2a 2a 20 61 6c 6c 20 61 6e 63 65 73 74 6f  n.** all ancesto
389e6 72 20 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73  r elements..*/.s
389e7 74 61 74 69 63 20 76 6f 69 64 20 41 64 6a 75 73  tatic void Adjus
389e8 74 54 72 65 65 28 0a 20 20 52 74 72 65 65 20 2a  tTree(.  Rtree *
389e9 70 52 74 72 65 65 2c 20 20 20 20 20 20 20 20 20  pRtree,         
389ea 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 74             /* Rt
389eb 72 65 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 52  ree table */.  R
389ec 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c  treeNode *pNode,
389ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
389ee 20 2f 2a 20 41 64 6a 75 73 74 20 61 6e 63 65 73   /* Adjust ances
389ef 74 72 79 20 6f 66 20 74 68 69 73 20 6e 6f 64 65  try of this node
389f0 2e 20 2a 2f 0a 20 20 52 74 72 65 65 43 65 6c 6c  . */.  RtreeCell
389f1 20 2a 70 43 65 6c 6c 20 20 20 20 20 20 20 20 20   *pCell         
389f2 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73           /* This
389f3 20 63 65 6c 6c 20 77 61 73 20 6a 75 73 74 20 69   cell was just i
389f4 6e 73 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20  nserted */.){.  
389f5 52 74 72 65 65 4e 6f 64 65 20 2a 70 20 3d 20 70  RtreeNode *p = p
389f6 4e 6f 64 65 3b 0a 20 20 77 68 69 6c 65 28 20 70  Node;.  while( p
389f7 2d 3e 70 50 61 72 65 6e 74 20 29 7b 0a 20 20 20  ->pParent ){.   
389f8 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b   RtreeCell cell;
389f9 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  .    RtreeNode *
389fa 70 50 61 72 65 6e 74 20 3d 20 70 2d 3e 70 50 61  pParent = p->pPa
389fb 72 65 6e 74 3b 0a 20 20 20 20 69 6e 74 20 69 43  rent;.    int iC
389fc 65 6c 6c 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74  ell = nodeParent
389fd 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70 29  Index(pRtree, p)
389fe 3b 0a 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65  ;..    nodeGetCe
389ff 6c 6c 28 70 52 74 72 65 65 2c 20 70 50 61 72 65  ll(pRtree, pPare
38a00 6e 74 2c 20 69 43 65 6c 6c 2c 20 26 63 65 6c 6c  nt, iCell, &cell
38a01 29 3b 0a 20 20 20 20 69 66 28 20 21 63 65 6c 6c  );.    if( !cell
38a02 43 6f 6e 74 61 69 6e 73 28 70 52 74 72 65 65 2c  Contains(pRtree,
38a03 20 26 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 20 29   &cell, pCell) )
38a04 7b 0a 20 20 20 20 20 20 63 65 6c 6c 55 6e 69 6f  {.      cellUnio
38a05 6e 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c  n(pRtree, &cell,
38a06 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 6e   pCell);.      n
38a07 6f 64 65 4f 76 65 72 77 72 69 74 65 43 65 6c 6c  odeOverwriteCell
38a08 28 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74  (pRtree, pParent
38a09 2c 20 26 63 65 6c 6c 2c 20 69 43 65 6c 6c 29 3b  , &cell, iCell);
38a0a 0a 20 20 20 20 7d 0a 20 0a 20 20 20 20 70 20 3d  .    }. .    p =
38a0b 20 70 50 61 72 65 6e 74 3b 0a 20 20 7d 0a 7d 0a   pParent;.  }.}.
38a0c 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d 61 70  ./*.** Write map
38a0d 70 69 6e 67 20 28 69 52 6f 77 69 64 2d 3e 69 4e  ping (iRowid->iN
38a0e 6f 64 65 29 20 74 6f 20 74 68 65 20 3c 72 74 72  ode) to the <rtr
38a0f 65 65 3e 5f 72 6f 77 69 64 20 74 61 62 6c 65 2e  ee>_rowid table.
38a10 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
38a11 6f 77 69 64 57 72 69 74 65 28 52 74 72 65 65 20  owidWrite(Rtree 
38a12 2a 70 52 74 72 65 65 2c 20 73 71 6c 69 74 65 33  *pRtree, sqlite3
38a13 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 2c 20 73  _int64 iRowid, s
38a14 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4e 6f  qlite3_int64 iNo
38a15 64 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62  de){.  sqlite3_b
38a16 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65  ind_int64(pRtree
38a17 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 2c 20 31  ->pWriteRowid, 1
38a18 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c  , iRowid);.  sql
38a19 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
38a1a 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f  pRtree->pWriteRo
38a1b 77 69 64 2c 20 32 2c 20 69 4e 6f 64 65 29 3b 0a  wid, 2, iNode);.
38a1c 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70    sqlite3_step(p
38a1d 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77  Rtree->pWriteRow
38a1e 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71  id);.  return sq
38a1f 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72  lite3_reset(pRtr
38a20 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29  ee->pWriteRowid)
38a21 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  ;.}../*.** Write
38a22 20 6d 61 70 70 69 6e 67 20 28 69 4e 6f 64 65 2d   mapping (iNode-
38a23 3e 69 50 61 72 29 20 74 6f 20 74 68 65 20 3c 72  >iPar) to the <r
38a24 74 72 65 65 3e 5f 70 61 72 65 6e 74 20 74 61 62  tree>_parent tab
38a25 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
38a26 74 20 70 61 72 65 6e 74 57 72 69 74 65 28 52 74  t parentWrite(Rt
38a27 72 65 65 20 2a 70 52 74 72 65 65 2c 20 73 71 6c  ree *pRtree, sql
38a28 69 74 65 33 5f 69 6e 74 36 34 20 69 4e 6f 64 65  ite3_int64 iNode
38a29 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
38a2a 69 50 61 72 29 7b 0a 20 20 73 71 6c 69 74 65 33  iPar){.  sqlite3
38a2b 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72  _bind_int64(pRtr
38a2c 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65 6e 74  ee->pWriteParent
38a2d 2c 20 31 2c 20 69 4e 6f 64 65 29 3b 0a 20 20 73  , 1, iNode);.  s
38a2e 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
38a2f 34 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  4(pRtree->pWrite
38a30 50 61 72 65 6e 74 2c 20 32 2c 20 69 50 61 72 29  Parent, 2, iPar)
38a31 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70  ;.  sqlite3_step
38a32 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50  (pRtree->pWriteP
38a33 61 72 65 6e 74 29 3b 0a 20 20 72 65 74 75 72 6e  arent);.  return
38a34 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
38a35 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50 61 72  Rtree->pWritePar
38a36 65 6e 74 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  ent);.}..static 
38a37 69 6e 74 20 72 74 72 65 65 49 6e 73 65 72 74 43  int rtreeInsertC
38a38 65 6c 6c 28 52 74 72 65 65 20 2a 2c 20 52 74 72  ell(Rtree *, Rtr
38a39 65 65 4e 6f 64 65 20 2a 2c 20 52 74 72 65 65 43  eeNode *, RtreeC
38a3a 65 6c 6c 20 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69  ell *, int);..#i
38a3b 66 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41  f VARIANT_GUTTMA
38a3c 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49 54 0a 2f  N_LINEAR_SPLIT./
38a3d 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
38a3e 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 6e 65 61  ion of the linea
38a3f 72 20 76 61 72 69 61 6e 74 20 6f 66 20 74 68 65  r variant of the
38a40 20 50 69 63 6b 4e 65 78 74 28 29 20 66 75 6e 63   PickNext() func
38a41 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 47 75 74  tion from.** Gut
38a42 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2f 0a 73 74 61  tman[84]..*/.sta
38a43 74 69 63 20 52 74 72 65 65 43 65 6c 6c 20 2a 4c  tic RtreeCell *L
38a44 69 6e 65 61 72 50 69 63 6b 4e 65 78 74 28 0a 20  inearPickNext(. 
38a45 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a   Rtree *pRtree,.
38a46 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65    RtreeCell *aCe
38a47 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c  ll, .  int nCell
38a48 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  , .  RtreeCell *
38a49 70 4c 65 66 74 42 6f 78 2c 20 0a 20 20 52 74 72  pLeftBox, .  Rtr
38a4a 65 65 43 65 6c 6c 20 2a 70 52 69 67 68 74 42 6f  eeCell *pRightBo
38a4b 78 2c 0a 20 20 69 6e 74 20 2a 61 69 55 73 65 64  x,.  int *aiUsed
38a4c 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20  .){.  int ii;.  
38a4d 66 6f 72 28 69 69 3d 30 3b 20 61 69 55 73 65 64  for(ii=0; aiUsed
38a4e 5b 69 69 5d 3b 20 69 69 2b 2b 29 3b 0a 20 20 61  [ii]; ii++);.  a
38a4f 69 55 73 65 64 5b 69 69 5d 20 3d 20 31 3b 0a 20  iUsed[ii] = 1;. 
38a50 20 72 65 74 75 72 6e 20 26 61 43 65 6c 6c 5b 69   return &aCell[i
38a51 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70  i];.}../*.** Imp
38a52 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
38a53 68 65 20 6c 69 6e 65 61 72 20 76 61 72 69 61 6e  he linear varian
38a54 74 20 6f 66 20 74 68 65 20 50 69 63 6b 53 65 65  t of the PickSee
38a55 64 73 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 72  ds() function fr
38a56 6f 6d 0a 2a 2a 20 47 75 74 74 6d 61 6e 5b 38 34  om.** Guttman[84
38a57 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ]..*/.static voi
38a58 64 20 4c 69 6e 65 61 72 50 69 63 6b 53 65 65 64  d LinearPickSeed
38a59 73 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  s(.  Rtree *pRtr
38a5a 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  ee,.  RtreeCell 
38a5b 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e  *aCell, .  int n
38a5c 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 2a 70 69  Cell, .  int *pi
38a5d 4c 65 66 74 53 65 65 64 2c 20 0a 20 20 69 6e 74  LeftSeed, .  int
38a5e 20 2a 70 69 52 69 67 68 74 53 65 65 64 0a 29 7b   *piRightSeed.){
38a5f 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
38a60 69 4c 65 66 74 53 65 65 64 20 3d 20 30 3b 0a 20  iLeftSeed = 0;. 
38a61 20 69 6e 74 20 69 52 69 67 68 74 53 65 65 64 20   int iRightSeed 
38a62 3d 20 31 3b 0a 20 20 66 6c 6f 61 74 20 6d 61 78  = 1;.  float max
38a63 4e 6f 72 6d 61 6c 49 6e 6e 65 72 57 69 64 74 68  NormalInnerWidth
38a64 20 3d 20 30 2e 30 3b 0a 0a 20 20 2f 2a 20 50 69   = 0.0;..  /* Pi
38a65 63 6b 20 74 77 6f 20 22 73 65 65 64 22 20 63 65  ck two "seed" ce
38a66 6c 6c 73 20 66 72 6f 6d 20 74 68 65 20 61 72 72  lls from the arr
38a67 61 79 20 6f 66 20 63 65 6c 6c 73 2e 20 54 68 65  ay of cells. The
38a68 20 61 6c 67 6f 72 69 74 68 6d 20 75 73 65 64 0a   algorithm used.
38a69 20 20 2a 2a 20 68 65 72 65 20 69 73 20 74 68 65    ** here is the
38a6a 20 4c 69 6e 65 61 72 50 69 63 6b 53 65 65 64 73   LinearPickSeeds
38a6b 20 61 6c 67 6f 72 69 74 68 6d 20 66 72 6f 6d 20   algorithm from 
38a6c 47 75 74 6d 61 6e 5b 31 39 38 34 5d 2e 20 54 68  Gutman[1984]. Th
38a6d 65 20 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73 20  e .  ** indices 
38a6e 6f 66 20 74 68 65 20 74 77 6f 20 73 65 65 64 20  of the two seed 
38a6f 63 65 6c 6c 73 20 69 6e 20 74 68 65 20 61 72 72  cells in the arr
38a70 61 79 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e  ay are stored in
38a71 20 6c 6f 63 61 6c 0a 20 20 2a 2a 20 76 61 72 69   local.  ** vari
38a72 61 62 6c 65 73 20 69 4c 65 66 74 53 65 65 6b 20  ables iLeftSeek 
38a73 61 6e 64 20 69 52 69 67 68 74 53 65 65 64 2e 0a  and iRightSeed..
38a74 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20    */.  for(i=0; 
38a75 69 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20  i<pRtree->nDim; 
38a76 69 2b 2b 29 7b 0a 20 20 20 20 66 6c 6f 61 74 20  i++){.    float 
38a77 78 31 20 3d 20 61 43 65 6c 6c 5b 30 5d 2e 61 43  x1 = aCell[0].aC
38a78 6f 6f 72 64 5b 69 2a 32 5d 3b 0a 20 20 20 20 66  oord[i*2];.    f
38a79 6c 6f 61 74 20 78 32 20 3d 20 61 43 65 6c 6c 5b  loat x2 = aCell[
38a7a 30 5d 2e 61 43 6f 6f 72 64 5b 69 2a 32 2b 31 5d  0].aCoord[i*2+1]
38a7b 3b 0a 20 20 20 20 66 6c 6f 61 74 20 78 33 20 3d  ;.    float x3 =
38a7c 20 78 31 3b 0a 20 20 20 20 66 6c 6f 61 74 20 78   x1;.    float x
38a7d 34 20 3d 20 78 32 3b 0a 20 20 20 20 69 6e 74 20  4 = x2;.    int 
38a7e 6a 6a 3b 0a 0a 20 20 20 20 69 6e 74 20 69 43 65  jj;..    int iCe
38a7f 6c 6c 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20 20  llLeft = 0;.    
38a80 69 6e 74 20 69 43 65 6c 6c 52 69 67 68 74 20 3d  int iCellRight =
38a81 20 30 3b 0a 0a 20 20 20 20 66 6f 72 28 6a 6a 3d   0;..    for(jj=
38a82 31 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b  1; jj<nCell; jj+
38a83 2b 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20  +){.      float 
38a84 6c 65 66 74 20 3d 20 61 43 65 6c 6c 5b 6a 6a 5d  left = aCell[jj]
38a85 2e 61 43 6f 6f 72 64 5b 69 2a 32 5d 3b 0a 20 20  .aCoord[i*2];.  
38a86 20 20 20 20 66 6c 6f 61 74 20 72 69 67 68 74 20      float right 
38a87 3d 20 61 43 65 6c 6c 5b 6a 6a 5d 2e 61 43 6f 6f  = aCell[jj].aCoo
38a88 72 64 5b 69 2a 32 2b 31 5d 3b 0a 0a 20 20 20 20  rd[i*2+1];..    
38a89 20 20 69 66 28 20 6c 65 66 74 3c 78 31 20 29 20    if( left<x1 ) 
38a8a 78 31 20 3d 20 6c 65 66 74 3b 0a 20 20 20 20 20  x1 = left;.     
38a8b 20 69 66 28 20 72 69 67 68 74 3e 78 34 20 29 20   if( right>x4 ) 
38a8c 78 34 20 3d 20 72 69 67 68 74 3b 0a 20 20 20 20  x4 = right;.    
38a8d 20 20 69 66 28 20 6c 65 66 74 3e 78 33 20 29 7b    if( left>x3 ){
38a8e 0a 20 20 20 20 20 20 20 20 78 33 20 3d 20 6c 65  .        x3 = le
38a8f 66 74 3b 0a 20 20 20 20 20 20 20 20 69 43 65 6c  ft;.        iCel
38a90 6c 52 69 67 68 74 20 3d 20 6a 6a 3b 0a 20 20 20  lRight = jj;.   
38a91 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72     }.      if( r
38a92 69 67 68 74 3c 78 32 20 29 7b 0a 20 20 20 20 20  ight<x2 ){.     
38a93 20 20 20 78 32 20 3d 20 72 69 67 68 74 3b 0a 20     x2 = right;. 
38a94 20 20 20 20 20 20 20 69 43 65 6c 6c 4c 65 66 74         iCellLeft
38a95 20 3d 20 6a 6a 3b 0a 20 20 20 20 20 20 7d 0a 20   = jj;.      }. 
38a96 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 78 34     }..    if( x4
38a97 21 3d 78 31 20 29 7b 0a 20 20 20 20 20 20 66 6c  !=x1 ){.      fl
38a98 6f 61 74 20 6e 6f 72 6d 61 6c 77 69 64 74 68 20  oat normalwidth 
38a99 3d 20 28 78 33 20 2d 20 78 32 29 20 2f 20 28 78  = (x3 - x2) / (x
38a9a 34 20 2d 20 78 31 29 3b 0a 20 20 20 20 20 20 69  4 - x1);.      i
38a9b 66 28 20 6e 6f 72 6d 61 6c 77 69 64 74 68 3e 6d  f( normalwidth>m
38a9c 61 78 4e 6f 72 6d 61 6c 49 6e 6e 65 72 57 69 64  axNormalInnerWid
38a9d 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4c  th ){.        iL
38a9e 65 66 74 53 65 65 64 20 3d 20 69 43 65 6c 6c 4c  eftSeed = iCellL
38a9f 65 66 74 3b 0a 20 20 20 20 20 20 20 20 69 52 69  eft;.        iRi
38aa0 67 68 74 53 65 65 64 20 3d 20 69 43 65 6c 6c 52  ghtSeed = iCellR
38aa1 69 67 68 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ight;.      }.  
38aa2 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 69 4c 65    }.  }..  *piLe
38aa3 66 74 53 65 65 64 20 3d 20 69 4c 65 66 74 53 65  ftSeed = iLeftSe
38aa4 65 64 3b 0a 20 20 2a 70 69 52 69 67 68 74 53 65  ed;.  *piRightSe
38aa5 65 64 20 3d 20 69 52 69 67 68 74 53 65 65 64 3b  ed = iRightSeed;
38aa6 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 56 41 52  .}.#endif /* VAR
38aa7 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e  IANT_GUTTMAN_LIN
38aa8 45 41 52 5f 53 50 4c 49 54 20 2a 2f 0a 0a 23 69  EAR_SPLIT */..#i
38aa9 66 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41  f VARIANT_GUTTMA
38aaa 4e 5f 51 55 41 44 52 41 54 49 43 5f 53 50 4c 49  N_QUADRATIC_SPLI
38aab 54 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  T./*.** Implemen
38aac 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75  tation of the qu
38aad 61 64 72 61 74 69 63 20 76 61 72 69 61 6e 74 20  adratic variant 
38aae 6f 66 20 74 68 65 20 50 69 63 6b 4e 65 78 74 28  of the PickNext(
38aaf 29 20 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a  ) function from.
38ab0 2a 2a 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a  ** Guttman[84]..
38ab1 2a 2f 0a 73 74 61 74 69 63 20 52 74 72 65 65 43  */.static RtreeC
38ab2 65 6c 6c 20 2a 51 75 61 64 72 61 74 69 63 50 69  ell *QuadraticPi
38ab3 63 6b 4e 65 78 74 28 0a 20 20 52 74 72 65 65 20  ckNext(.  Rtree 
38ab4 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65  *pRtree,.  Rtree
38ab5 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20  Cell *aCell, .  
38ab6 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 52 74  int nCell, .  Rt
38ab7 72 65 65 43 65 6c 6c 20 2a 70 4c 65 66 74 42 6f  reeCell *pLeftBo
38ab8 78 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  x, .  RtreeCell 
38ab9 2a 70 52 69 67 68 74 42 6f 78 2c 0a 20 20 69 6e  *pRightBox,.  in
38aba 74 20 2a 61 69 55 73 65 64 0a 29 7b 0a 20 20 23  t *aiUsed.){.  #
38abb 64 65 66 69 6e 65 20 46 41 42 53 28 61 29 20 28  define FABS(a) (
38abc 28 61 29 3c 30 2e 30 3f 2d 31 2e 30 2a 28 61 29  (a)<0.0?-1.0*(a)
38abd 3a 28 61 29 29 0a 0a 20 20 69 6e 74 20 69 53 65  :(a))..  int iSe
38abe 6c 65 63 74 20 3d 20 2d 31 3b 0a 20 20 66 6c 6f  lect = -1;.  flo
38abf 61 74 20 66 44 69 66 66 3b 0a 20 20 69 6e 74 20  at fDiff;.  int 
38ac0 69 69 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  ii;.  for(ii=0; 
38ac1 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b  ii<nCell; ii++){
38ac2 0a 20 20 20 20 69 66 28 20 61 69 55 73 65 64 5b  .    if( aiUsed[
38ac3 69 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ii]==0 ){.      
38ac4 66 6c 6f 61 74 20 6c 65 66 74 20 3d 20 63 65 6c  float left = cel
38ac5 6c 47 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20  lGrowth(pRtree, 
38ac6 70 4c 65 66 74 42 6f 78 2c 20 26 61 43 65 6c 6c  pLeftBox, &aCell
38ac7 5b 69 69 5d 29 3b 0a 20 20 20 20 20 20 66 6c 6f  [ii]);.      flo
38ac8 61 74 20 72 69 67 68 74 20 3d 20 63 65 6c 6c 47  at right = cellG
38ac9 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20 70 4c  rowth(pRtree, pL
38aca 65 66 74 42 6f 78 2c 20 26 61 43 65 6c 6c 5b 69  eftBox, &aCell[i
38acb 69 5d 29 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74  i]);.      float
38acc 20 64 69 66 66 20 3d 20 46 41 42 53 28 72 69 67   diff = FABS(rig
38acd 68 74 2d 6c 65 66 74 29 3b 0a 20 20 20 20 20 20  ht-left);.      
38ace 69 66 28 20 69 53 65 6c 65 63 74 3c 30 20 7c 7c  if( iSelect<0 ||
38acf 20 64 69 66 66 3e 66 44 69 66 66 20 29 7b 0a 20   diff>fDiff ){. 
38ad0 20 20 20 20 20 20 20 66 44 69 66 66 20 3d 20 64         fDiff = d
38ad1 69 66 66 3b 0a 20 20 20 20 20 20 20 20 69 53 65  iff;.        iSe
38ad2 6c 65 63 74 20 3d 20 69 69 3b 0a 20 20 20 20 20  lect = ii;.     
38ad3 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61   }.    }.  }.  a
38ad4 69 55 73 65 64 5b 69 53 65 6c 65 63 74 5d 20 3d  iUsed[iSelect] =
38ad5 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 26 61 43   1;.  return &aC
38ad6 65 6c 6c 5b 69 53 65 6c 65 63 74 5d 3b 0a 7d 0a  ell[iSelect];.}.
38ad7 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
38ad8 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 61  ation of the qua
38ad9 64 72 61 74 69 63 20 76 61 72 69 61 6e 74 20 6f  dratic variant o
38ada 66 20 74 68 65 20 50 69 63 6b 53 65 65 64 73 28  f the PickSeeds(
38adb 29 20 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a  ) function from.
38adc 2a 2a 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a  ** Guttman[84]..
38add 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 51  */.static void Q
38ade 75 61 64 72 61 74 69 63 50 69 63 6b 53 65 65 64  uadraticPickSeed
38adf 73 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  s(.  Rtree *pRtr
38ae0 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  ee,.  RtreeCell 
38ae1 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e  *aCell, .  int n
38ae2 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 2a 70 69  Cell, .  int *pi
38ae3 4c 65 66 74 53 65 65 64 2c 20 0a 20 20 69 6e 74  LeftSeed, .  int
38ae4 20 2a 70 69 52 69 67 68 74 53 65 65 64 0a 29 7b   *piRightSeed.){
38ae5 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74  .  int ii;.  int
38ae6 20 6a 6a 3b 0a 0a 20 20 69 6e 74 20 69 4c 65 66   jj;..  int iLef
38ae7 74 53 65 65 64 20 3d 20 30 3b 0a 20 20 69 6e 74  tSeed = 0;.  int
38ae8 20 69 52 69 67 68 74 53 65 65 64 20 3d 20 31 3b   iRightSeed = 1;
38ae9 0a 20 20 66 6c 6f 61 74 20 66 57 61 73 74 65 20  .  float fWaste 
38aea 3d 20 30 2e 30 3b 0a 0a 20 20 66 6f 72 28 69 69  = 0.0;..  for(ii
38aeb 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69  =0; ii<nCell; ii
38aec 2b 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6a 6a 3d  ++){.    for(jj=
38aed 69 69 2b 31 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20  ii+1; jj<nCell; 
38aee 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6c 6f  jj++){.      flo
38aef 61 74 20 72 69 67 68 74 20 3d 20 63 65 6c 6c 41  at right = cellA
38af0 72 65 61 28 70 52 74 72 65 65 2c 20 26 61 43 65  rea(pRtree, &aCe
38af1 6c 6c 5b 6a 6a 5d 29 3b 0a 20 20 20 20 20 20 66  ll[jj]);.      f
38af2 6c 6f 61 74 20 67 72 6f 77 74 68 20 3d 20 63 65  loat growth = ce
38af3 6c 6c 47 72 6f 77 74 68 28 70 52 74 72 65 65 2c  llGrowth(pRtree,
38af4 20 26 61 43 65 6c 6c 5b 69 69 5d 2c 20 26 61 43   &aCell[ii], &aC
38af5 65 6c 6c 5b 6a 6a 5d 29 3b 0a 20 20 20 20 20 20  ell[jj]);.      
38af6 66 6c 6f 61 74 20 77 61 73 74 65 20 3d 20 67 72  float waste = gr
38af7 6f 77 74 68 20 2d 20 72 69 67 68 74 3b 0a 0a 20  owth - right;.. 
38af8 20 20 20 20 20 69 66 28 20 77 61 73 74 65 3e 66       if( waste>f
38af9 57 61 73 74 65 20 29 7b 0a 20 20 20 20 20 20 20  Waste ){.       
38afa 20 69 4c 65 66 74 53 65 65 64 20 3d 20 69 69 3b   iLeftSeed = ii;
38afb 0a 20 20 20 20 20 20 20 20 69 52 69 67 68 74 53  .        iRightS
38afc 65 65 64 20 3d 20 6a 6a 3b 0a 20 20 20 20 20 20  eed = jj;.      
38afd 20 20 66 57 61 73 74 65 20 3d 20 77 61 73 74 65    fWaste = waste
38afe 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
38aff 20 20 7d 0a 0a 20 20 2a 70 69 4c 65 66 74 53 65    }..  *piLeftSe
38b00 65 64 20 3d 20 69 4c 65 66 74 53 65 65 64 3b 0a  ed = iLeftSeed;.
38b01 20 20 2a 70 69 52 69 67 68 74 53 65 65 64 20 3d    *piRightSeed =
38b02 20 69 52 69 67 68 74 53 65 65 64 3b 0a 7d 0a 23   iRightSeed;.}.#
38b03 65 6e 64 69 66 20 2f 2a 20 56 41 52 49 41 4e 54  endif /* VARIANT
38b04 5f 47 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54  _GUTTMAN_QUADRAT
38b05 49 43 5f 53 50 4c 49 54 20 2a 2f 0a 0a 2f 2a 0a  IC_SPLIT */../*.
38b06 2a 2a 20 41 72 67 75 6d 65 6e 74 73 20 61 49 64  ** Arguments aId
38b07 78 2c 20 61 44 69 73 74 61 6e 63 65 20 61 6e 64  x, aDistance and
38b08 20 61 53 70 61 72 65 20 61 6c 6c 20 70 6f 69 6e   aSpare all poin
38b09 74 20 74 6f 20 61 72 72 61 79 73 20 6f 66 20 73  t to arrays of s
38b0a 69 7a 65 0a 2a 2a 20 6e 49 64 78 2e 20 54 68 65  ize.** nIdx. The
38b0b 20 61 49 64 78 20 61 72 72 61 79 20 63 6f 6e 74   aIdx array cont
38b0c 61 69 6e 73 20 74 68 65 20 73 65 74 20 6f 66 20  ains the set of 
38b0d 69 6e 74 65 67 65 72 73 20 66 72 6f 6d 20 30 20  integers from 0 
38b0e 74 6f 20 0a 2a 2a 20 28 6e 49 64 78 2d 31 29 20  to .** (nIdx-1) 
38b0f 69 6e 20 6e 6f 20 70 61 72 74 69 63 75 6c 61 72  in no particular
38b10 20 6f 72 64 65 72 2e 20 54 68 69 73 20 66 75 6e   order. This fun
38b11 63 74 69 6f 6e 20 73 6f 72 74 73 20 74 68 65 20  ction sorts the 
38b12 76 61 6c 75 65 73 0a 2a 2a 20 69 6e 20 61 49 64  values.** in aId
38b13 78 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74  x according to t
38b14 68 65 20 69 6e 64 65 78 65 64 20 76 61 6c 75 65  he indexed value
38b15 73 20 69 6e 20 61 44 69 73 74 61 6e 63 65 2e 20  s in aDistance. 
38b16 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20  For.** example, 
38b17 61 73 73 75 6d 69 6e 67 20 74 68 65 20 69 6e 70  assuming the inp
38b18 75 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 49 64  uts:.**.**   aId
38b19 78 20 20 20 20 20 20 3d 20 7b 20 30 2c 20 20 20  x      = { 0,   
38b1a 31 2c 20 20 20 32 2c 20 20 20 33 20 7d 0a 2a 2a  1,   2,   3 }.**
38b1b 20 20 20 61 44 69 73 74 61 6e 63 65 20 3d 20 7b     aDistance = {
38b1c 20 35 2e 30 2c 20 32 2e 30 2c 20 37 2e 30 2c 20   5.0, 2.0, 7.0, 
38b1d 36 2e 30 20 7d 0a 2a 2a 0a 2a 2a 20 74 68 69 73  6.0 }.**.** this
38b1e 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 74   function sets t
38b1f 68 65 20 61 49 64 78 20 61 72 72 61 79 20 74 6f  he aIdx array to
38b20 20 63 6f 6e 74 61 69 6e 3a 0a 2a 2a 0a 2a 2a 20   contain:.**.** 
38b21 20 20 61 49 64 78 20 20 20 20 20 20 3d 20 7b 20    aIdx      = { 
38b22 30 2c 20 20 20 31 2c 20 20 20 32 2c 20 20 20 33  0,   1,   2,   3
38b23 20 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 53 70   }.**.** The aSp
38b24 61 72 65 20 61 72 72 61 79 20 69 73 20 75 73 65  are array is use
38b25 64 20 61 73 20 74 65 6d 70 6f 72 61 72 79 20 77  d as temporary w
38b26 6f 72 6b 69 6e 67 20 73 70 61 63 65 20 62 79 20  orking space by 
38b27 74 68 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 61  the.** sorting a
38b28 6c 67 6f 72 69 74 68 6d 2e 0a 2a 2f 0a 73 74 61  lgorithm..*/.sta
38b29 74 69 63 20 76 6f 69 64 20 53 6f 72 74 42 79 44  tic void SortByD
38b2a 69 73 74 61 6e 63 65 28 0a 20 20 69 6e 74 20 2a  istance(.  int *
38b2b 61 49 64 78 2c 20 0a 20 20 69 6e 74 20 6e 49 64  aIdx, .  int nId
38b2c 78 2c 20 0a 20 20 66 6c 6f 61 74 20 2a 61 44 69  x, .  float *aDi
38b2d 73 74 61 6e 63 65 2c 20 0a 20 20 69 6e 74 20 2a  stance, .  int *
38b2e 61 53 70 61 72 65 0a 29 7b 0a 20 20 69 66 28 20  aSpare.){.  if( 
38b2f 6e 49 64 78 3e 31 20 29 7b 0a 20 20 20 20 69 6e  nIdx>1 ){.    in
38b30 74 20 69 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20  t iLeft = 0;.   
38b31 20 69 6e 74 20 69 52 69 67 68 74 20 3d 20 30 3b   int iRight = 0;
38b32 0a 0a 20 20 20 20 69 6e 74 20 6e 4c 65 66 74 20  ..    int nLeft 
38b33 3d 20 6e 49 64 78 2f 32 3b 0a 20 20 20 20 69 6e  = nIdx/2;.    in
38b34 74 20 6e 52 69 67 68 74 20 3d 20 6e 49 64 78 2d  t nRight = nIdx-
38b35 6e 4c 65 66 74 3b 0a 20 20 20 20 69 6e 74 20 2a  nLeft;.    int *
38b36 61 4c 65 66 74 20 3d 20 61 49 64 78 3b 0a 20 20  aLeft = aIdx;.  
38b37 20 20 69 6e 74 20 2a 61 52 69 67 68 74 20 3d 20    int *aRight = 
38b38 26 61 49 64 78 5b 6e 4c 65 66 74 5d 3b 0a 0a 20  &aIdx[nLeft];.. 
38b39 20 20 20 53 6f 72 74 42 79 44 69 73 74 61 6e 63     SortByDistanc
38b3a 65 28 61 4c 65 66 74 2c 20 6e 4c 65 66 74 2c 20  e(aLeft, nLeft, 
38b3b 61 44 69 73 74 61 6e 63 65 2c 20 61 53 70 61 72  aDistance, aSpar
38b3c 65 29 3b 0a 20 20 20 20 53 6f 72 74 42 79 44 69  e);.    SortByDi
38b3d 73 74 61 6e 63 65 28 61 52 69 67 68 74 2c 20 6e  stance(aRight, n
38b3e 52 69 67 68 74 2c 20 61 44 69 73 74 61 6e 63 65  Right, aDistance
38b3f 2c 20 61 53 70 61 72 65 29 3b 0a 0a 20 20 20 20  , aSpare);..    
38b40 6d 65 6d 63 70 79 28 61 53 70 61 72 65 2c 20 61  memcpy(aSpare, a
38b41 4c 65 66 74 2c 20 73 69 7a 65 6f 66 28 69 6e 74  Left, sizeof(int
38b42 29 2a 6e 4c 65 66 74 29 3b 0a 20 20 20 20 61 4c  )*nLeft);.    aL
38b43 65 66 74 20 3d 20 61 53 70 61 72 65 3b 0a 0a 20  eft = aSpare;.. 
38b44 20 20 20 77 68 69 6c 65 28 20 69 4c 65 66 74 3c     while( iLeft<
38b45 6e 4c 65 66 74 20 7c 7c 20 69 52 69 67 68 74 3c  nLeft || iRight<
38b46 6e 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  nRight ){.      
38b47 69 66 28 20 69 4c 65 66 74 3d 3d 6e 4c 65 66 74  if( iLeft==nLeft
38b48 20 29 7b 0a 20 20 20 20 20 20 20 20 61 49 64 78   ){.        aIdx
38b49 5b 69 4c 65 66 74 2b 69 52 69 67 68 74 5d 20 3d  [iLeft+iRight] =
38b4a 20 61 52 69 67 68 74 5b 69 52 69 67 68 74 5d 3b   aRight[iRight];
38b4b 0a 20 20 20 20 20 20 20 20 69 52 69 67 68 74 2b  .        iRight+
38b4c 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  +;.      }else i
38b4d 66 28 20 69 52 69 67 68 74 3d 3d 6e 52 69 67 68  f( iRight==nRigh
38b4e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 61 49 64  t ){.        aId
38b4f 78 5b 69 4c 65 66 74 2b 69 52 69 67 68 74 5d 20  x[iLeft+iRight] 
38b50 3d 20 61 4c 65 66 74 5b 69 4c 65 66 74 5d 3b 0a  = aLeft[iLeft];.
38b51 20 20 20 20 20 20 20 20 69 4c 65 66 74 2b 2b 3b          iLeft++;
38b52 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
38b53 20 20 20 20 20 20 66 6c 6f 61 74 20 66 4c 65 66        float fLef
38b54 74 20 3d 20 61 44 69 73 74 61 6e 63 65 5b 61 4c  t = aDistance[aL
38b55 65 66 74 5b 69 4c 65 66 74 5d 5d 3b 0a 20 20 20  eft[iLeft]];.   
38b56 20 20 20 20 20 66 6c 6f 61 74 20 66 52 69 67 68       float fRigh
38b57 74 20 3d 20 61 44 69 73 74 61 6e 63 65 5b 61 52  t = aDistance[aR
38b58 69 67 68 74 5b 69 52 69 67 68 74 5d 5d 3b 0a 20  ight[iRight]];. 
38b59 20 20 20 20 20 20 20 69 66 28 20 66 4c 65 66 74         if( fLeft
38b5a 3c 66 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  <fRight ){.     
38b5b 20 20 20 20 20 61 49 64 78 5b 69 4c 65 66 74 2b       aIdx[iLeft+
38b5c 69 52 69 67 68 74 5d 20 3d 20 61 4c 65 66 74 5b  iRight] = aLeft[
38b5d 69 4c 65 66 74 5d 3b 0a 20 20 20 20 20 20 20 20  iLeft];.        
38b5e 20 20 69 4c 65 66 74 2b 2b 3b 0a 20 20 20 20 20    iLeft++;.     
38b5f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
38b60 20 20 20 20 61 49 64 78 5b 69 4c 65 66 74 2b 69      aIdx[iLeft+i
38b61 52 69 67 68 74 5d 20 3d 20 61 52 69 67 68 74 5b  Right] = aRight[
38b62 69 52 69 67 68 74 5d 3b 0a 20 20 20 20 20 20 20  iRight];.       
38b63 20 20 20 69 52 69 67 68 74 2b 2b 3b 0a 20 20 20     iRight++;.   
38b64 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
38b65 20 20 20 7d 0a 0a 23 69 66 20 30 0a 20 20 20 20     }..#if 0.    
38b66 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  /* Check that th
38b67 65 20 73 6f 72 74 20 77 6f 72 6b 65 64 20 2a 2f  e sort worked */
38b68 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74  .    {.      int
38b69 20 6a 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a   jj;.      for(j
38b6a 6a 3d 31 3b 20 6a 6a 3c 6e 49 64 78 3b 20 6a 6a  j=1; jj<nIdx; jj
38b6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 6f  ++){.        flo
38b6c 61 74 20 6c 65 66 74 20 3d 20 61 44 69 73 74 61  at left = aDista
38b6d 6e 63 65 5b 61 49 64 78 5b 6a 6a 2d 31 5d 5d 3b  nce[aIdx[jj-1]];
38b6e 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74 20 72  .        float r
38b6f 69 67 68 74 20 3d 20 61 44 69 73 74 61 6e 63 65  ight = aDistance
38b70 5b 61 49 64 78 5b 6a 6a 5d 5d 3b 0a 20 20 20 20  [aIdx[jj]];.    
38b71 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 66 74      assert( left
38b72 3c 3d 72 69 67 68 74 20 29 3b 0a 20 20 20 20 20  <=right );.     
38b73 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a   }.    }.#endif.
38b74 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 67    }.}../*.** Arg
38b75 75 6d 65 6e 74 73 20 61 49 64 78 2c 20 61 43 65  uments aIdx, aCe
38b76 6c 6c 20 61 6e 64 20 61 53 70 61 72 65 20 61 6c  ll and aSpare al
38b77 6c 20 70 6f 69 6e 74 20 74 6f 20 61 72 72 61 79  l point to array
38b78 73 20 6f 66 20 73 69 7a 65 0a 2a 2a 20 6e 49 64  s of size.** nId
38b79 78 2e 20 54 68 65 20 61 49 64 78 20 61 72 72 61  x. The aIdx arra
38b7a 79 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73  y contains the s
38b7b 65 74 20 6f 66 20 69 6e 74 65 67 65 72 73 20 66  et of integers f
38b7c 72 6f 6d 20 30 20 74 6f 20 0a 2a 2a 20 28 6e 49  rom 0 to .** (nI
38b7d 64 78 2d 31 29 20 69 6e 20 6e 6f 20 70 61 72 74  dx-1) in no part
38b7e 69 63 75 6c 61 72 20 6f 72 64 65 72 2e 20 54 68  icular order. Th
38b7f 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 6f 72 74  is function sort
38b80 73 20 74 68 65 20 76 61 6c 75 65 73 0a 2a 2a 20  s the values.** 
38b81 69 6e 20 61 49 64 78 20 61 63 63 6f 72 64 69 6e  in aIdx accordin
38b82 67 20 74 6f 20 64 69 6d 65 6e 73 69 6f 6e 20 69  g to dimension i
38b83 44 69 6d 20 6f 66 20 74 68 65 20 63 65 6c 6c 73  Dim of the cells
38b84 20 69 6e 20 61 43 65 6c 6c 2e 20 54 68 65 0a 2a   in aCell. The.*
38b85 2a 20 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20  * minimum value 
38b86 6f 66 20 64 69 6d 65 6e 73 69 6f 6e 20 69 44 69  of dimension iDi
38b87 6d 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  m is considered 
38b88 66 69 72 73 74 2c 20 74 68 65 0a 2a 2a 20 6d 61  first, the.** ma
38b89 78 69 6d 75 6d 20 75 73 65 64 20 74 6f 20 62 72  ximum used to br
38b8a 65 61 6b 20 74 69 65 73 2e 0a 2a 2a 0a 2a 2a 20  eak ties..**.** 
38b8b 54 68 65 20 61 53 70 61 72 65 20 61 72 72 61 79  The aSpare array
38b8c 20 69 73 20 75 73 65 64 20 61 73 20 74 65 6d 70   is used as temp
38b8d 6f 72 61 72 79 20 77 6f 72 6b 69 6e 67 20 73 70  orary working sp
38b8e 61 63 65 20 62 79 20 74 68 65 0a 2a 2a 20 73 6f  ace by the.** so
38b8f 72 74 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 2e  rting algorithm.
38b90 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
38b91 53 6f 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e 28  SortByDimension(
38b92 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
38b93 2c 0a 20 20 69 6e 74 20 2a 61 49 64 78 2c 20 0a  ,.  int *aIdx, .
38b94 20 20 69 6e 74 20 6e 49 64 78 2c 20 0a 20 20 69    int nIdx, .  i
38b95 6e 74 20 69 44 69 6d 2c 20 0a 20 20 52 74 72 65  nt iDim, .  Rtre
38b96 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20  eCell *aCell, . 
38b97 20 69 6e 74 20 2a 61 53 70 61 72 65 0a 29 7b 0a   int *aSpare.){.
38b98 20 20 69 66 28 20 6e 49 64 78 3e 31 20 29 7b 0a    if( nIdx>1 ){.
38b99 0a 20 20 20 20 69 6e 74 20 69 4c 65 66 74 20 3d  .    int iLeft =
38b9a 20 30 3b 0a 20 20 20 20 69 6e 74 20 69 52 69 67   0;.    int iRig
38b9b 68 74 20 3d 20 30 3b 0a 0a 20 20 20 20 69 6e 74  ht = 0;..    int
38b9c 20 6e 4c 65 66 74 20 3d 20 6e 49 64 78 2f 32 3b   nLeft = nIdx/2;
38b9d 0a 20 20 20 20 69 6e 74 20 6e 52 69 67 68 74 20  .    int nRight 
38b9e 3d 20 6e 49 64 78 2d 6e 4c 65 66 74 3b 0a 20 20  = nIdx-nLeft;.  
38b9f 20 20 69 6e 74 20 2a 61 4c 65 66 74 20 3d 20 61    int *aLeft = a
38ba0 49 64 78 3b 0a 20 20 20 20 69 6e 74 20 2a 61 52  Idx;.    int *aR
38ba1 69 67 68 74 20 3d 20 26 61 49 64 78 5b 6e 4c 65  ight = &aIdx[nLe
38ba2 66 74 5d 3b 0a 0a 20 20 20 20 53 6f 72 74 42 79  ft];..    SortBy
38ba3 44 69 6d 65 6e 73 69 6f 6e 28 70 52 74 72 65 65  Dimension(pRtree
38ba4 2c 20 61 4c 65 66 74 2c 20 6e 4c 65 66 74 2c 20  , aLeft, nLeft, 
38ba5 69 44 69 6d 2c 20 61 43 65 6c 6c 2c 20 61 53 70  iDim, aCell, aSp
38ba6 61 72 65 29 3b 0a 20 20 20 20 53 6f 72 74 42 79  are);.    SortBy
38ba7 44 69 6d 65 6e 73 69 6f 6e 28 70 52 74 72 65 65  Dimension(pRtree
38ba8 2c 20 61 52 69 67 68 74 2c 20 6e 52 69 67 68 74  , aRight, nRight
38ba9 2c 20 69 44 69 6d 2c 20 61 43 65 6c 6c 2c 20 61  , iDim, aCell, a
38baa 53 70 61 72 65 29 3b 0a 0a 20 20 20 20 6d 65 6d  Spare);..    mem
38bab 63 70 79 28 61 53 70 61 72 65 2c 20 61 4c 65 66  cpy(aSpare, aLef
38bac 74 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e  t, sizeof(int)*n
38bad 4c 65 66 74 29 3b 0a 20 20 20 20 61 4c 65 66 74  Left);.    aLeft
38bae 20 3d 20 61 53 70 61 72 65 3b 0a 20 20 20 20 77   = aSpare;.    w
38baf 68 69 6c 65 28 20 69 4c 65 66 74 3c 6e 4c 65 66  hile( iLeft<nLef
38bb0 74 20 7c 7c 20 69 52 69 67 68 74 3c 6e 52 69 67  t || iRight<nRig
38bb1 68 74 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62  ht ){.      doub
38bb2 6c 65 20 78 6c 65 66 74 31 20 3d 20 44 43 4f 4f  le xleft1 = DCOO
38bb3 52 44 28 61 43 65 6c 6c 5b 61 4c 65 66 74 5b 69  RD(aCell[aLeft[i
38bb4 4c 65 66 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44  Left]].aCoord[iD
38bb5 69 6d 2a 32 5d 29 3b 0a 20 20 20 20 20 20 64 6f  im*2]);.      do
38bb6 75 62 6c 65 20 78 6c 65 66 74 32 20 3d 20 44 43  uble xleft2 = DC
38bb7 4f 4f 52 44 28 61 43 65 6c 6c 5b 61 4c 65 66 74  OORD(aCell[aLeft
38bb8 5b 69 4c 65 66 74 5d 5d 2e 61 43 6f 6f 72 64 5b  [iLeft]].aCoord[
38bb9 69 44 69 6d 2a 32 2b 31 5d 29 3b 0a 20 20 20 20  iDim*2+1]);.    
38bba 20 20 64 6f 75 62 6c 65 20 78 72 69 67 68 74 31    double xright1
38bbb 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b   = DCOORD(aCell[
38bbc 61 52 69 67 68 74 5b 69 52 69 67 68 74 5d 5d 2e  aRight[iRight]].
38bbd 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 29 3b  aCoord[iDim*2]);
38bbe 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 78 72  .      double xr
38bbf 69 67 68 74 32 20 3d 20 44 43 4f 4f 52 44 28 61  ight2 = DCOORD(a
38bc0 43 65 6c 6c 5b 61 52 69 67 68 74 5b 69 52 69 67  Cell[aRight[iRig
38bc1 68 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d  ht]].aCoord[iDim
38bc2 2a 32 2b 31 5d 29 3b 0a 20 20 20 20 20 20 69 66  *2+1]);.      if
38bc3 28 20 28 69 4c 65 66 74 21 3d 6e 4c 65 66 74 29  ( (iLeft!=nLeft)
38bc4 20 26 26 20 28 28 69 52 69 67 68 74 3d 3d 6e 52   && ((iRight==nR
38bc5 69 67 68 74 29 0a 20 20 20 20 20 20 20 7c 7c 20  ight).       || 
38bc6 28 78 6c 65 66 74 31 3c 78 72 69 67 68 74 31 29  (xleft1<xright1)
38bc7 0a 20 20 20 20 20 20 20 7c 7c 20 28 78 6c 65 66  .       || (xlef
38bc8 74 31 3d 3d 78 72 69 67 68 74 31 20 26 26 20 78  t1==xright1 && x
38bc9 6c 65 66 74 32 3c 78 72 69 67 68 74 32 29 0a 20  left2<xright2). 
38bca 20 20 20 20 20 29 29 7b 0a 20 20 20 20 20 20 20       )){.       
38bcb 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67   aIdx[iLeft+iRig
38bcc 68 74 5d 20 3d 20 61 4c 65 66 74 5b 69 4c 65 66  ht] = aLeft[iLef
38bcd 74 5d 3b 0a 20 20 20 20 20 20 20 20 69 4c 65 66  t];.        iLef
38bce 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  t++;.      }else
38bcf 7b 0a 20 20 20 20 20 20 20 20 61 49 64 78 5b 69  {.        aIdx[i
38bd0 4c 65 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61  Left+iRight] = a
38bd1 52 69 67 68 74 5b 69 52 69 67 68 74 5d 3b 0a 20  Right[iRight];. 
38bd2 20 20 20 20 20 20 20 69 52 69 67 68 74 2b 2b 3b         iRight++;
38bd3 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
38bd4 23 69 66 20 30 0a 20 20 20 20 2f 2a 20 43 68 65  #if 0.    /* Che
38bd5 63 6b 20 74 68 61 74 20 74 68 65 20 73 6f 72 74  ck that the sort
38bd6 20 77 6f 72 6b 65 64 20 2a 2f 0a 20 20 20 20 7b   worked */.    {
38bd7 0a 20 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 20  .      int jj;. 
38bd8 20 20 20 20 20 66 6f 72 28 6a 6a 3d 31 3b 20 6a       for(jj=1; j
38bd9 6a 3c 6e 49 64 78 3b 20 6a 6a 2b 2b 29 7b 0a 20  j<nIdx; jj++){. 
38bda 20 20 20 20 20 20 20 66 6c 6f 61 74 20 78 6c 65         float xle
38bdb 66 74 31 20 3d 20 61 43 65 6c 6c 5b 61 49 64 78  ft1 = aCell[aIdx
38bdc 5b 6a 6a 2d 31 5d 5d 2e 61 43 6f 6f 72 64 5b 69  [jj-1]].aCoord[i
38bdd 44 69 6d 2a 32 5d 3b 0a 20 20 20 20 20 20 20 20  Dim*2];.        
38bde 66 6c 6f 61 74 20 78 6c 65 66 74 32 20 3d 20 61  float xleft2 = a
38bdf 43 65 6c 6c 5b 61 49 64 78 5b 6a 6a 2d 31 5d 5d  Cell[aIdx[jj-1]]
38be0 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31  .aCoord[iDim*2+1
38be1 5d 3b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74  ];.        float
38be2 20 78 72 69 67 68 74 31 20 3d 20 61 43 65 6c 6c   xright1 = aCell
38be3 5b 61 49 64 78 5b 6a 6a 5d 5d 2e 61 43 6f 6f 72  [aIdx[jj]].aCoor
38be4 64 5b 69 44 69 6d 2a 32 5d 3b 0a 20 20 20 20 20  d[iDim*2];.     
38be5 20 20 20 66 6c 6f 61 74 20 78 72 69 67 68 74 32     float xright2
38be6 20 3d 20 61 43 65 6c 6c 5b 61 49 64 78 5b 6a 6a   = aCell[aIdx[jj
38be7 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32  ]].aCoord[iDim*2
38be8 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 61 73 73  +1];.        ass
38be9 65 72 74 28 20 78 6c 65 66 74 31 3c 3d 78 72 69  ert( xleft1<=xri
38bea 67 68 74 31 20 26 26 20 28 78 6c 65 66 74 31 3c  ght1 && (xleft1<
38beb 78 72 69 67 68 74 31 20 7c 7c 20 78 6c 65 66 74  xright1 || xleft
38bec 32 3c 3d 78 72 69 67 68 74 32 29 20 29 3b 0a 20  2<=xright2) );. 
38bed 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e       }.    }.#en
38bee 64 69 66 0a 20 20 7d 0a 7d 0a 0a 23 69 66 20 56  dif.  }.}..#if V
38bef 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45  ARIANT_RSTARTREE
38bf0 5f 53 50 4c 49 54 0a 2f 2a 0a 2a 2a 20 49 6d 70  _SPLIT./*.** Imp
38bf1 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
38bf2 68 65 20 52 2a 2d 74 72 65 65 20 76 61 72 69 61  he R*-tree varia
38bf3 6e 74 20 6f 66 20 53 70 6c 69 74 4e 6f 64 65 20  nt of SplitNode 
38bf4 66 72 6f 6d 20 42 65 63 6b 6d 61 6e 5b 31 39 39  from Beckman[199
38bf5 30 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  0]..*/.static in
38bf6 74 20 73 70 6c 69 74 4e 6f 64 65 53 74 61 72 74  t splitNodeStart
38bf7 72 65 65 28 0a 20 20 52 74 72 65 65 20 2a 70 52  ree(.  Rtree *pR
38bf8 74 72 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c  tree,.  RtreeCel
38bf9 6c 20 2a 61 43 65 6c 6c 2c 0a 20 20 69 6e 74 20  l *aCell,.  int 
38bfa 6e 43 65 6c 6c 2c 0a 20 20 52 74 72 65 65 4e 6f  nCell,.  RtreeNo
38bfb 64 65 20 2a 70 4c 65 66 74 2c 0a 20 20 52 74 72  de *pLeft,.  Rtr
38bfc 65 65 4e 6f 64 65 20 2a 70 52 69 67 68 74 2c 0a  eeNode *pRight,.
38bfd 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 42 62    RtreeCell *pBb
38bfe 6f 78 4c 65 66 74 2c 0a 20 20 52 74 72 65 65 43  oxLeft,.  RtreeC
38bff 65 6c 6c 20 2a 70 42 62 6f 78 52 69 67 68 74 0a  ell *pBboxRight.
38c00 29 7b 0a 20 20 69 6e 74 20 2a 2a 61 61 53 6f 72  ){.  int **aaSor
38c01 74 65 64 3b 0a 20 20 69 6e 74 20 2a 61 53 70 61  ted;.  int *aSpa
38c02 72 65 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 0a 20  re;.  int ii;.. 
38c03 20 69 6e 74 20 69 42 65 73 74 44 69 6d 3b 0a 20   int iBestDim;. 
38c04 20 69 6e 74 20 69 42 65 73 74 53 70 6c 69 74 3b   int iBestSplit;
38c05 0a 20 20 66 6c 6f 61 74 20 66 42 65 73 74 4d 61  .  float fBestMa
38c06 72 67 69 6e 3b 0a 0a 20 20 69 6e 74 20 6e 42 79  rgin;..  int nBy
38c07 74 65 20 3d 20 28 70 52 74 72 65 65 2d 3e 6e 44  te = (pRtree->nD
38c08 69 6d 2b 31 29 2a 28 73 69 7a 65 6f 66 28 69 6e  im+1)*(sizeof(in
38c09 74 2a 29 2b 6e 43 65 6c 6c 2a 73 69 7a 65 6f 66  t*)+nCell*sizeof
38c0a 28 69 6e 74 29 29 3b 0a 0a 20 20 61 61 53 6f 72  (int));..  aaSor
38c0b 74 65 64 20 3d 20 28 69 6e 74 20 2a 2a 29 73 71  ted = (int **)sq
38c0c 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79  lite3_malloc(nBy
38c0d 74 65 29 3b 0a 20 20 69 66 28 20 21 61 61 53 6f  te);.  if( !aaSo
38c0e 72 74 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75  rted ){.    retu
38c0f 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
38c10 0a 20 20 7d 0a 0a 20 20 61 53 70 61 72 65 20 3d  .  }..  aSpare =
38c11 20 26 28 28 69 6e 74 20 2a 29 26 61 61 53 6f 72   &((int *)&aaSor
38c12 74 65 64 5b 70 52 74 72 65 65 2d 3e 6e 44 69 6d  ted[pRtree->nDim
38c13 5d 29 5b 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a  ])[pRtree->nDim*
38c14 6e 43 65 6c 6c 5d 3b 0a 20 20 6d 65 6d 73 65 74  nCell];.  memset
38c15 28 61 61 53 6f 72 74 65 64 2c 20 30 2c 20 6e 42  (aaSorted, 0, nB
38c16 79 74 65 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30  yte);.  for(ii=0
38c17 3b 20 69 69 3c 70 52 74 72 65 65 2d 3e 6e 44 69  ; ii<pRtree->nDi
38c18 6d 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 6e  m; ii++){.    in
38c19 74 20 6a 6a 3b 0a 20 20 20 20 61 61 53 6f 72 74  t jj;.    aaSort
38c1a 65 64 5b 69 69 5d 20 3d 20 26 28 28 69 6e 74 20  ed[ii] = &((int 
38c1b 2a 29 26 61 61 53 6f 72 74 65 64 5b 70 52 74 72  *)&aaSorted[pRtr
38c1c 65 65 2d 3e 6e 44 69 6d 5d 29 5b 69 69 2a 6e 43  ee->nDim])[ii*nC
38c1d 65 6c 6c 5d 3b 0a 20 20 20 20 66 6f 72 28 6a 6a  ell];.    for(jj
38c1e 3d 30 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a  =0; jj<nCell; jj
38c1f 2b 2b 29 7b 0a 20 20 20 20 20 20 61 61 53 6f 72  ++){.      aaSor
38c20 74 65 64 5b 69 69 5d 5b 6a 6a 5d 20 3d 20 6a 6a  ted[ii][jj] = jj
38c21 3b 0a 20 20 20 20 7d 0a 20 20 20 20 53 6f 72 74  ;.    }.    Sort
38c22 42 79 44 69 6d 65 6e 73 69 6f 6e 28 70 52 74 72  ByDimension(pRtr
38c23 65 65 2c 20 61 61 53 6f 72 74 65 64 5b 69 69 5d  ee, aaSorted[ii]
38c24 2c 20 6e 43 65 6c 6c 2c 20 69 69 2c 20 61 43 65  , nCell, ii, aCe
38c25 6c 6c 2c 20 61 53 70 61 72 65 29 3b 0a 20 20 7d  ll, aSpare);.  }
38c26 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ..  for(ii=0; ii
38c27 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69  <pRtree->nDim; i
38c28 69 2b 2b 29 7b 0a 20 20 20 20 66 6c 6f 61 74 20  i++){.    float 
38c29 6d 61 72 67 69 6e 20 3d 20 30 2e 30 3b 0a 20 20  margin = 0.0;.  
38c2a 20 20 66 6c 6f 61 74 20 66 42 65 73 74 4f 76 65    float fBestOve
38c2b 72 6c 61 70 3b 0a 20 20 20 20 66 6c 6f 61 74 20  rlap;.    float 
38c2c 66 42 65 73 74 41 72 65 61 3b 0a 20 20 20 20 69  fBestArea;.    i
38c2d 6e 74 20 69 42 65 73 74 4c 65 66 74 3b 0a 20 20  nt iBestLeft;.  
38c2e 20 20 69 6e 74 20 6e 4c 65 66 74 3b 0a 0a 20 20    int nLeft;..  
38c2f 20 20 66 6f 72 28 0a 20 20 20 20 20 20 6e 4c 65    for(.      nLe
38c30 66 74 3d 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c  ft=RTREE_MINCELL
38c31 53 28 70 52 74 72 65 65 29 3b 20 0a 20 20 20 20  S(pRtree); .    
38c32 20 20 6e 4c 65 66 74 3c 3d 28 6e 43 65 6c 6c 2d    nLeft<=(nCell-
38c33 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70  RTREE_MINCELLS(p
38c34 52 74 72 65 65 29 29 3b 20 0a 20 20 20 20 20 20  Rtree)); .      
38c35 6e 4c 65 66 74 2b 2b 0a 20 20 20 20 29 7b 0a 20  nLeft++.    ){. 
38c36 20 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 6c       RtreeCell l
38c37 65 66 74 3b 0a 20 20 20 20 20 20 52 74 72 65 65  eft;.      Rtree
38c38 43 65 6c 6c 20 72 69 67 68 74 3b 0a 20 20 20 20  Cell right;.    
38c39 20 20 69 6e 74 20 6b 6b 3b 0a 20 20 20 20 20 20    int kk;.      
38c3a 66 6c 6f 61 74 20 6f 76 65 72 6c 61 70 3b 0a 20  float overlap;. 
38c3b 20 20 20 20 20 66 6c 6f 61 74 20 61 72 65 61 3b       float area;
38c3c 0a 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26  ..      memcpy(&
38c3d 6c 65 66 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53  left, &aCell[aaS
38c3e 6f 72 74 65 64 5b 69 69 5d 5b 30 5d 5d 2c 20 73  orted[ii][0]], s
38c3f 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29  izeof(RtreeCell)
38c40 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  );.      memcpy(
38c41 26 72 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b 61  &right, &aCell[a
38c42 61 53 6f 72 74 65 64 5b 69 69 5d 5b 6e 43 65 6c  aSorted[ii][nCel
38c43 6c 2d 31 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74  l-1]], sizeof(Rt
38c44 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 20 20 20  reeCell));.     
38c45 20 66 6f 72 28 6b 6b 3d 31 3b 20 6b 6b 3c 28 6e   for(kk=1; kk<(n
38c46 43 65 6c 6c 2d 31 29 3b 20 6b 6b 2b 2b 29 7b 0a  Cell-1); kk++){.
38c47 20 20 20 20 20 20 20 20 69 66 28 20 6b 6b 3c 6e          if( kk<n
38c48 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Left ){.        
38c49 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72    cellUnion(pRtr
38c4a 65 65 2c 20 26 6c 65 66 74 2c 20 26 61 43 65 6c  ee, &left, &aCel
38c4b 6c 5b 61 61 53 6f 72 74 65 64 5b 69 69 5d 5b 6b  l[aaSorted[ii][k
38c4c 6b 5d 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  k]]);.        }e
38c4d 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63  lse{.          c
38c4e 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c  ellUnion(pRtree,
38c4f 20 26 72 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b   &right, &aCell[
38c50 61 61 53 6f 72 74 65 64 5b 69 69 5d 5b 6b 6b 5d  aaSorted[ii][kk]
38c51 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ]);.        }.  
38c52 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 61 72 67      }.      marg
38c53 69 6e 20 2b 3d 20 63 65 6c 6c 4d 61 72 67 69 6e  in += cellMargin
38c54 28 70 52 74 72 65 65 2c 20 26 6c 65 66 74 29 3b  (pRtree, &left);
38c55 0a 20 20 20 20 20 20 6d 61 72 67 69 6e 20 2b 3d  .      margin +=
38c56 20 63 65 6c 6c 4d 61 72 67 69 6e 28 70 52 74 72   cellMargin(pRtr
38c57 65 65 2c 20 26 72 69 67 68 74 29 3b 0a 20 20 20  ee, &right);.   
38c58 20 20 20 6f 76 65 72 6c 61 70 20 3d 20 63 65 6c     overlap = cel
38c59 6c 4f 76 65 72 6c 61 70 28 70 52 74 72 65 65 2c  lOverlap(pRtree,
38c5a 20 26 6c 65 66 74 2c 20 26 72 69 67 68 74 2c 20   &left, &right, 
38c5b 31 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 61 72  1, -1);.      ar
38c5c 65 61 20 3d 20 63 65 6c 6c 41 72 65 61 28 70 52  ea = cellArea(pR
38c5d 74 72 65 65 2c 20 26 6c 65 66 74 29 20 2b 20 63  tree, &left) + c
38c5e 65 6c 6c 41 72 65 61 28 70 52 74 72 65 65 2c 20  ellArea(pRtree, 
38c5f 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20 20 69  &right);.      i
38c60 66 28 20 28 6e 4c 65 66 74 3d 3d 52 54 52 45 45  f( (nLeft==RTREE
38c61 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65  _MINCELLS(pRtree
38c62 29 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f 76  )).       || (ov
38c63 65 72 6c 61 70 3c 66 42 65 73 74 4f 76 65 72 6c  erlap<fBestOverl
38c64 61 70 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f  ap).       || (o
38c65 76 65 72 6c 61 70 3d 3d 66 42 65 73 74 4f 76 65  verlap==fBestOve
38c66 72 6c 61 70 20 26 26 20 61 72 65 61 3c 66 42 65  rlap && area<fBe
38c67 73 74 41 72 65 61 29 0a 20 20 20 20 20 20 29 7b  stArea).      ){
38c68 0a 20 20 20 20 20 20 20 20 69 42 65 73 74 4c 65  .        iBestLe
38c69 66 74 20 3d 20 6e 4c 65 66 74 3b 0a 20 20 20 20  ft = nLeft;.    
38c6a 20 20 20 20 66 42 65 73 74 4f 76 65 72 6c 61 70      fBestOverlap
38c6b 20 3d 20 6f 76 65 72 6c 61 70 3b 0a 20 20 20 20   = overlap;.    
38c6c 20 20 20 20 66 42 65 73 74 41 72 65 61 20 3d 20      fBestArea = 
38c6d 61 72 65 61 3b 0a 20 20 20 20 20 20 7d 0a 20 20  area;.      }.  
38c6e 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 69 69 3d    }..    if( ii=
38c6f 3d 30 20 7c 7c 20 6d 61 72 67 69 6e 3c 66 42 65  =0 || margin<fBe
38c70 73 74 4d 61 72 67 69 6e 20 29 7b 0a 20 20 20 20  stMargin ){.    
38c71 20 20 69 42 65 73 74 44 69 6d 20 3d 20 69 69 3b    iBestDim = ii;
38c72 0a 20 20 20 20 20 20 66 42 65 73 74 4d 61 72 67  .      fBestMarg
38c73 69 6e 20 3d 20 6d 61 72 67 69 6e 3b 0a 20 20 20  in = margin;.   
38c74 20 20 20 69 42 65 73 74 53 70 6c 69 74 20 3d 20     iBestSplit = 
38c75 69 42 65 73 74 4c 65 66 74 3b 0a 20 20 20 20 7d  iBestLeft;.    }
38c76 0a 20 20 7d 0a 0a 20 20 6d 65 6d 63 70 79 28 70  .  }..  memcpy(p
38c77 42 62 6f 78 4c 65 66 74 2c 20 26 61 43 65 6c 6c  BboxLeft, &aCell
38c78 5b 61 61 53 6f 72 74 65 64 5b 69 42 65 73 74 44  [aaSorted[iBestD
38c79 69 6d 5d 5b 30 5d 5d 2c 20 73 69 7a 65 6f 66 28  im][0]], sizeof(
38c7a 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 6d  RtreeCell));.  m
38c7b 65 6d 63 70 79 28 70 42 62 6f 78 52 69 67 68 74  emcpy(pBboxRight
38c7c 2c 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65  , &aCell[aaSorte
38c7d 64 5b 69 42 65 73 74 44 69 6d 5d 5b 69 42 65 73  d[iBestDim][iBes
38c7e 74 53 70 6c 69 74 5d 5d 2c 20 73 69 7a 65 6f 66  tSplit]], sizeof
38c7f 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20  (RtreeCell));.  
38c80 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65  for(ii=0; ii<nCe
38c81 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52  ll; ii++){.    R
38c82 74 72 65 65 4e 6f 64 65 20 2a 70 54 61 72 67 65  treeNode *pTarge
38c83 74 20 3d 20 28 69 69 3c 69 42 65 73 74 53 70 6c  t = (ii<iBestSpl
38c84 69 74 29 3f 70 4c 65 66 74 3a 70 52 69 67 68 74  it)?pLeft:pRight
38c85 3b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20  ;.    RtreeCell 
38c86 2a 70 42 62 6f 78 20 3d 20 28 69 69 3c 69 42 65  *pBbox = (ii<iBe
38c87 73 74 53 70 6c 69 74 29 3f 70 42 62 6f 78 4c 65  stSplit)?pBboxLe
38c88 66 74 3a 70 42 62 6f 78 52 69 67 68 74 3b 0a 20  ft:pBboxRight;. 
38c89 20 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43     RtreeCell *pC
38c8a 65 6c 6c 20 3d 20 26 61 43 65 6c 6c 5b 61 61 53  ell = &aCell[aaS
38c8b 6f 72 74 65 64 5b 69 42 65 73 74 44 69 6d 5d 5b  orted[iBestDim][
38c8c 69 69 5d 5d 3b 0a 20 20 20 20 6e 6f 64 65 49 6e  ii]];.    nodeIn
38c8d 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  sertCell(pRtree,
38c8e 20 70 54 61 72 67 65 74 2c 20 70 43 65 6c 6c 29   pTarget, pCell)
38c8f 3b 0a 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28  ;.    cellUnion(
38c90 70 52 74 72 65 65 2c 20 70 42 62 6f 78 2c 20 70  pRtree, pBbox, p
38c91 43 65 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20 73 71  Cell);.  }..  sq
38c92 6c 69 74 65 33 5f 66 72 65 65 28 61 61 53 6f 72  lite3_free(aaSor
38c93 74 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  ted);.  return S
38c94 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64  QLITE_OK;.}.#end
38c95 69 66 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f  if..#if VARIANT_
38c96 47 55 54 54 4d 41 4e 5f 53 50 4c 49 54 0a 2f 2a  GUTTMAN_SPLIT./*
38c97 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
38c98 6f 6e 20 6f 66 20 74 68 65 20 72 65 67 75 6c 61  on of the regula
38c99 72 20 52 2d 74 72 65 65 20 53 70 6c 69 74 4e 6f  r R-tree SplitNo
38c9a 64 65 20 66 72 6f 6d 20 47 75 74 74 6d 61 6e 5b  de from Guttman[
38c9b 31 39 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63  1984]..*/.static
38c9c 20 69 6e 74 20 73 70 6c 69 74 4e 6f 64 65 47 75   int splitNodeGu
38c9d 74 74 6d 61 6e 28 0a 20 20 52 74 72 65 65 20 2a  ttman(.  Rtree *
38c9e 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65 43  pRtree,.  RtreeC
38c9f 65 6c 6c 20 2a 61 43 65 6c 6c 2c 0a 20 20 69 6e  ell *aCell,.  in
38ca0 74 20 6e 43 65 6c 6c 2c 0a 20 20 52 74 72 65 65  t nCell,.  Rtree
38ca1 4e 6f 64 65 20 2a 70 4c 65 66 74 2c 0a 20 20 52  Node *pLeft,.  R
38ca2 74 72 65 65 4e 6f 64 65 20 2a 70 52 69 67 68 74  treeNode *pRight
38ca3 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70  ,.  RtreeCell *p
38ca4 42 62 6f 78 4c 65 66 74 2c 0a 20 20 52 74 72 65  BboxLeft,.  Rtre
38ca5 65 43 65 6c 6c 20 2a 70 42 62 6f 78 52 69 67 68  eCell *pBboxRigh
38ca6 74 0a 29 7b 0a 20 20 69 6e 74 20 69 4c 65 66 74  t.){.  int iLeft
38ca7 53 65 65 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20  Seed = 0;.  int 
38ca8 69 52 69 67 68 74 53 65 65 64 20 3d 20 31 3b 0a  iRightSeed = 1;.
38ca9 20 20 69 6e 74 20 2a 61 69 55 73 65 64 3b 0a 20    int *aiUsed;. 
38caa 20 69 6e 74 20 69 3b 0a 0a 20 20 61 69 55 73 65   int i;..  aiUse
38cab 64 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  d = sqlite3_mall
38cac 6f 63 28 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e  oc(sizeof(int)*n
38cad 43 65 6c 6c 29 3b 0a 20 20 6d 65 6d 73 65 74 28  Cell);.  memset(
38cae 61 69 55 73 65 64 2c 20 30 2c 20 73 69 7a 65 6f  aiUsed, 0, sizeo
38caf 66 28 69 6e 74 29 2a 6e 43 65 6c 6c 29 3b 0a 0a  f(int)*nCell);..
38cb0 20 20 50 69 63 6b 53 65 65 64 73 28 70 52 74 72    PickSeeds(pRtr
38cb1 65 65 2c 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c  ee, aCell, nCell
38cb2 2c 20 26 69 4c 65 66 74 53 65 65 64 2c 20 26 69  , &iLeftSeed, &i
38cb3 52 69 67 68 74 53 65 65 64 29 3b 0a 0a 20 20 6d  RightSeed);..  m
38cb4 65 6d 63 70 79 28 70 42 62 6f 78 4c 65 66 74 2c  emcpy(pBboxLeft,
38cb5 20 26 61 43 65 6c 6c 5b 69 4c 65 66 74 53 65 65   &aCell[iLeftSee
38cb6 64 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65  d], sizeof(Rtree
38cb7 43 65 6c 6c 29 29 3b 0a 20 20 6d 65 6d 63 70 79  Cell));.  memcpy
38cb8 28 70 42 62 6f 78 52 69 67 68 74 2c 20 26 61 43  (pBboxRight, &aC
38cb9 65 6c 6c 5b 69 52 69 67 68 74 53 65 65 64 5d 2c  ell[iRightSeed],
38cba 20 73 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c   sizeof(RtreeCel
38cbb 6c 29 29 3b 0a 20 20 6e 6f 64 65 49 6e 73 65 72  l));.  nodeInser
38cbc 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4c  tCell(pRtree, pL
38cbd 65 66 74 2c 20 26 61 43 65 6c 6c 5b 69 4c 65 66  eft, &aCell[iLef
38cbe 74 53 65 65 64 5d 29 3b 0a 20 20 6e 6f 64 65 49  tSeed]);.  nodeI
38cbf 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65  nsertCell(pRtree
38cc0 2c 20 70 52 69 67 68 74 2c 20 26 61 43 65 6c 6c  , pRight, &aCell
38cc1 5b 69 52 69 67 68 74 53 65 65 64 5d 29 3b 0a 20  [iRightSeed]);. 
38cc2 20 61 69 55 73 65 64 5b 69 4c 65 66 74 53 65 65   aiUsed[iLeftSee
38cc3 64 5d 20 3d 20 31 3b 0a 20 20 61 69 55 73 65 64  d] = 1;.  aiUsed
38cc4 5b 69 52 69 67 68 74 53 65 65 64 5d 20 3d 20 31  [iRightSeed] = 1
38cc5 3b 0a 0a 20 20 66 6f 72 28 69 3d 6e 43 65 6c 6c  ;..  for(i=nCell
38cc6 2d 32 3b 20 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20  -2; i>0; i--){. 
38cc7 20 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 4e     RtreeCell *pN
38cc8 65 78 74 3b 0a 20 20 20 20 70 4e 65 78 74 20 3d  ext;.    pNext =
38cc9 20 50 69 63 6b 4e 65 78 74 28 70 52 74 72 65 65   PickNext(pRtree
38cca 2c 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20  , aCell, nCell, 
38ccb 70 42 62 6f 78 4c 65 66 74 2c 20 70 42 62 6f 78  pBboxLeft, pBbox
38ccc 52 69 67 68 74 2c 20 61 69 55 73 65 64 29 3b 0a  Right, aiUsed);.
38ccd 20 20 20 20 66 6c 6f 61 74 20 64 69 66 66 20 3d      float diff =
38cce 20 20 0a 20 20 20 20 20 20 63 65 6c 6c 47 72 6f    .      cellGro
38ccf 77 74 68 28 70 52 74 72 65 65 2c 20 70 42 62 6f  wth(pRtree, pBbo
38cd0 78 4c 65 66 74 2c 20 70 4e 65 78 74 29 20 2d 20  xLeft, pNext) - 
38cd1 0a 20 20 20 20 20 20 63 65 6c 6c 47 72 6f 77 74  .      cellGrowt
38cd2 68 28 70 52 74 72 65 65 2c 20 70 42 62 6f 78 52  h(pRtree, pBboxR
38cd3 69 67 68 74 2c 20 70 4e 65 78 74 29 0a 20 20 20  ight, pNext).   
38cd4 20 3b 0a 20 20 20 20 69 66 28 20 28 52 54 52 45   ;.    if( (RTRE
38cd5 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65  E_MINCELLS(pRtre
38cd6 65 29 2d 4e 43 45 4c 4c 28 70 52 69 67 68 74 29  e)-NCELL(pRight)
38cd7 3d 3d 69 29 0a 20 20 20 20 20 7c 7c 20 28 64 69  ==i).     || (di
38cd8 66 66 3e 30 2e 30 20 26 26 20 28 52 54 52 45 45  ff>0.0 && (RTREE
38cd9 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65  _MINCELLS(pRtree
38cda 29 2d 4e 43 45 4c 4c 28 70 4c 65 66 74 29 21 3d  )-NCELL(pLeft)!=
38cdb 69 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  i)).    ){.     
38cdc 20 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28   nodeInsertCell(
38cdd 70 52 74 72 65 65 2c 20 70 52 69 67 68 74 2c 20  pRtree, pRight, 
38cde 70 4e 65 78 74 29 3b 0a 20 20 20 20 20 20 63 65  pNext);.      ce
38cdf 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20  llUnion(pRtree, 
38ce0 70 42 62 6f 78 52 69 67 68 74 2c 20 70 4e 65 78  pBboxRight, pNex
38ce1 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  t);.    }else{. 
38ce2 20 20 20 20 20 6e 6f 64 65 49 6e 73 65 72 74 43       nodeInsertC
38ce3 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4c 65 66  ell(pRtree, pLef
38ce4 74 2c 20 70 4e 65 78 74 29 3b 0a 20 20 20 20 20  t, pNext);.     
38ce5 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65   cellUnion(pRtre
38ce6 65 2c 20 70 42 62 6f 78 4c 65 66 74 2c 20 70 4e  e, pBboxLeft, pN
38ce7 65 78 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ext);.    }.  }.
38ce8 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
38ce9 61 69 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72  aiUsed);.  retur
38cea 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23  n SQLITE_OK;.}.#
38ceb 65 6e 64 69 66 0a 0a 73 74 61 74 69 63 20 69 6e  endif..static in
38cec 74 20 75 70 64 61 74 65 4d 61 70 70 69 6e 67 28  t updateMapping(
38ced 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
38cee 2c 20 0a 20 20 69 36 34 20 69 52 6f 77 69 64 2c  , .  i64 iRowid,
38cef 20 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70   .  RtreeNode *p
38cf0 4e 6f 64 65 2c 20 0a 20 20 69 6e 74 20 69 48 65  Node, .  int iHe
38cf1 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74 20 28 2a  ight.){.  int (*
38cf2 78 53 65 74 4d 61 70 70 69 6e 67 29 28 52 74 72  xSetMapping)(Rtr
38cf3 65 65 20 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e  ee *, sqlite3_in
38cf4 74 36 34 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74  t64, sqlite3_int
38cf5 36 34 29 3b 0a 20 20 78 53 65 74 4d 61 70 70 69  64);.  xSetMappi
38cf6 6e 67 20 3d 20 28 28 69 48 65 69 67 68 74 3d 3d  ng = ((iHeight==
38cf7 30 29 3f 72 6f 77 69 64 57 72 69 74 65 3a 70 61  0)?rowidWrite:pa
38cf8 72 65 6e 74 57 72 69 74 65 29 3b 0a 20 20 69 66  rentWrite);.  if
38cf9 28 20 69 48 65 69 67 68 74 3e 30 20 29 7b 0a 20  ( iHeight>0 ){. 
38cfa 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43     RtreeNode *pC
38cfb 68 69 6c 64 20 3d 20 6e 6f 64 65 48 61 73 68 4c  hild = nodeHashL
38cfc 6f 6f 6b 75 70 28 70 52 74 72 65 65 2c 20 69 52  ookup(pRtree, iR
38cfd 6f 77 69 64 29 3b 0a 20 20 20 20 69 66 28 20 70  owid);.    if( p
38cfe 43 68 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 6e  Child ){.      n
38cff 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
38d00 65 2c 20 70 43 68 69 6c 64 2d 3e 70 50 61 72 65  e, pChild->pPare
38d01 6e 74 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 52  nt);.      nodeR
38d02 65 66 65 72 65 6e 63 65 28 70 4e 6f 64 65 29 3b  eference(pNode);
38d03 0a 20 20 20 20 20 20 70 43 68 69 6c 64 2d 3e 70  .      pChild->p
38d04 50 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65 3b 0a  Parent = pNode;.
38d05 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
38d06 72 6e 20 78 53 65 74 4d 61 70 70 69 6e 67 28 70  rn xSetMapping(p
38d07 52 74 72 65 65 2c 20 69 52 6f 77 69 64 2c 20 70  Rtree, iRowid, p
38d08 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 7d 0a  Node->iNode);.}.
38d09 0a 73 74 61 74 69 63 20 69 6e 74 20 53 70 6c 69  .static int Spli
38d0a 74 4e 6f 64 65 28 0a 20 20 52 74 72 65 65 20 2a  tNode(.  Rtree *
38d0b 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65 4e  pRtree,.  RtreeN
38d0c 6f 64 65 20 2a 70 4e 6f 64 65 2c 0a 20 20 52 74  ode *pNode,.  Rt
38d0d 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c 0a  reeCell *pCell,.
38d0e 20 20 69 6e 74 20 69 48 65 69 67 68 74 0a 29 7b    int iHeight.){
38d0f 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
38d10 6e 65 77 43 65 6c 6c 49 73 52 69 67 68 74 20 3d  newCellIsRight =
38d11 20 30 3b 0a 0a 20 20 69 6e 74 20 72 63 20 3d 20   0;..  int rc = 
38d12 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74  SQLITE_OK;.  int
38d13 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70   nCell = NCELL(p
38d14 4e 6f 64 65 29 3b 0a 20 20 52 74 72 65 65 43 65  Node);.  RtreeCe
38d15 6c 6c 20 2a 61 43 65 6c 6c 3b 0a 20 20 69 6e 74  ll *aCell;.  int
38d16 20 2a 61 69 55 73 65 64 3b 0a 0a 20 20 52 74 72   *aiUsed;..  Rtr
38d17 65 65 4e 6f 64 65 20 2a 70 4c 65 66 74 20 3d 20  eeNode *pLeft = 
38d18 30 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  0;.  RtreeNode *
38d19 70 52 69 67 68 74 20 3d 20 30 3b 0a 0a 20 20 52  pRight = 0;..  R
38d1a 74 72 65 65 43 65 6c 6c 20 6c 65 66 74 62 62 6f  treeCell leftbbo
38d1b 78 3b 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 72  x;.  RtreeCell r
38d1c 69 67 68 74 62 62 6f 78 3b 0a 0a 20 20 2f 2a 20  ightbbox;..  /* 
38d1d 41 6c 6c 6f 63 61 74 65 20 61 6e 20 61 72 72 61  Allocate an arra
38d1e 79 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 69  y and populate i
38d1f 74 20 77 69 74 68 20 61 20 63 6f 70 79 20 6f 66  t with a copy of
38d20 20 70 43 65 6c 6c 20 61 6e 64 20 0a 20 20 2a 2a   pCell and .  **
38d21 20 61 6c 6c 20 63 65 6c 6c 73 20 66 72 6f 6d 20   all cells from 
38d22 6e 6f 64 65 20 70 4c 65 66 74 2e 20 54 68 65 6e  node pLeft. Then
38d23 20 7a 65 72 6f 20 74 68 65 20 6f 72 69 67 69 6e   zero the origin
38d24 61 6c 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20  al node..  */.  
38d25 61 43 65 6c 6c 20 3d 20 73 71 6c 69 74 65 33 5f  aCell = sqlite3_
38d26 6d 61 6c 6c 6f 63 28 28 73 69 7a 65 6f 66 28 52  malloc((sizeof(R
38d27 74 72 65 65 43 65 6c 6c 29 2b 73 69 7a 65 6f 66  treeCell)+sizeof
38d28 28 69 6e 74 29 29 2a 28 6e 43 65 6c 6c 2b 31 29  (int))*(nCell+1)
38d29 29 3b 0a 20 20 69 66 28 20 21 61 43 65 6c 6c 20  );.  if( !aCell 
38d2a 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
38d2b 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f  TE_NOMEM;.    go
38d2c 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74  to splitnode_out
38d2d 3b 0a 20 20 7d 0a 20 20 61 69 55 73 65 64 20 3d  ;.  }.  aiUsed =
38d2e 20 28 69 6e 74 20 2a 29 26 61 43 65 6c 6c 5b 6e   (int *)&aCell[n
38d2f 43 65 6c 6c 2b 31 5d 3b 0a 20 20 6d 65 6d 73 65  Cell+1];.  memse
38d30 74 28 61 69 55 73 65 64 2c 20 30 2c 20 73 69 7a  t(aiUsed, 0, siz
38d31 65 6f 66 28 69 6e 74 29 2a 28 6e 43 65 6c 6c 2b  eof(int)*(nCell+
38d32 31 29 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  1));.  for(i=0; 
38d33 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20  i<nCell; i++){. 
38d34 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70     nodeGetCell(p
38d35 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 2c  Rtree, pNode, i,
38d36 20 26 61 43 65 6c 6c 5b 69 5d 29 3b 0a 20 20 7d   &aCell[i]);.  }
38d37 0a 20 20 6e 6f 64 65 5a 65 72 6f 28 70 52 74 72  .  nodeZero(pRtr
38d38 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 6d 65  ee, pNode);.  me
38d39 6d 63 70 79 28 26 61 43 65 6c 6c 5b 6e 43 65 6c  mcpy(&aCell[nCel
38d3a 6c 5d 2c 20 70 43 65 6c 6c 2c 20 73 69 7a 65 6f  l], pCell, sizeo
38d3b 66 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20  f(RtreeCell));. 
38d3c 20 6e 43 65 6c 6c 2b 2b 3b 0a 0a 20 20 69 66 28   nCell++;..  if(
38d3d 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31   pNode->iNode==1
38d3e 20 29 7b 0a 20 20 20 20 70 52 69 67 68 74 20 3d   ){.    pRight =
38d3f 20 6e 6f 64 65 4e 65 77 28 70 52 74 72 65 65 2c   nodeNew(pRtree,
38d40 20 70 4e 6f 64 65 2c 20 31 29 3b 0a 20 20 20 20   pNode, 1);.    
38d41 70 4c 65 66 74 20 3d 20 6e 6f 64 65 4e 65 77 28  pLeft = nodeNew(
38d42 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 31  pRtree, pNode, 1
38d43 29 3b 0a 20 20 20 20 70 52 74 72 65 65 2d 3e 69  );.    pRtree->i
38d44 44 65 70 74 68 2b 2b 3b 0a 20 20 20 20 70 4e 6f  Depth++;.    pNo
38d45 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b  de->isDirty = 1;
38d46 0a 20 20 20 20 77 72 69 74 65 49 6e 74 31 36 28  .    writeInt16(
38d47 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 2c 20 70 52  pNode->zData, pR
38d48 74 72 65 65 2d 3e 69 44 65 70 74 68 29 3b 0a 20  tree->iDepth);. 
38d49 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4c 65 66   }else{.    pLef
38d4a 74 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 20 20 70  t = pNode;.    p
38d4b 52 69 67 68 74 20 3d 20 6e 6f 64 65 4e 65 77 28  Right = nodeNew(
38d4c 70 52 74 72 65 65 2c 20 70 4c 65 66 74 2d 3e 70  pRtree, pLeft->p
38d4d 50 61 72 65 6e 74 2c 20 31 29 3b 0a 20 20 20 20  Parent, 1);.    
38d4e 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70 4c  nodeReference(pL
38d4f 65 66 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  eft);.  }..  if(
38d50 20 21 70 4c 65 66 74 20 7c 7c 20 21 70 52 69 67   !pLeft || !pRig
38d51 68 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  ht ){.    rc = S
38d52 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
38d53 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f   goto splitnode_
38d54 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 6d 65 6d 73  out;.  }..  mems
38d55 65 74 28 70 4c 65 66 74 2d 3e 7a 44 61 74 61 2c  et(pLeft->zData,
38d56 20 30 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64   0, pRtree->iNod
38d57 65 53 69 7a 65 29 3b 0a 20 20 6d 65 6d 73 65 74  eSize);.  memset
38d58 28 70 52 69 67 68 74 2d 3e 7a 44 61 74 61 2c 20  (pRight->zData, 
38d59 30 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65  0, pRtree->iNode
38d5a 53 69 7a 65 29 3b 0a 0a 20 20 72 63 20 3d 20 41  Size);..  rc = A
38d5b 73 73 69 67 6e 43 65 6c 6c 73 28 70 52 74 72 65  ssignCells(pRtre
38d5c 65 2c 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c  e, aCell, nCell,
38d5d 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20   pLeft, pRight, 
38d5e 26 6c 65 66 74 62 62 6f 78 2c 20 26 72 69 67 68  &leftbbox, &righ
38d5f 74 62 62 6f 78 29 3b 0a 20 20 69 66 28 20 72 63  tbbox);.  if( rc
38d60 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
38d61 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64     goto splitnod
38d62 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  e_out;.  }..  /*
38d63 20 45 6e 73 75 72 65 20 62 6f 74 68 20 63 68 69   Ensure both chi
38d64 6c 64 20 6e 6f 64 65 73 20 68 61 76 65 20 6e 6f  ld nodes have no
38d65 64 65 20 6e 75 6d 62 65 72 73 20 61 73 73 69 67  de numbers assig
38d66 6e 65 64 20 74 6f 20 74 68 65 6d 2e 20 2a 2f 0a  ned to them. */.
38d67 20 20 69 66 28 20 28 30 3d 3d 70 52 69 67 68 74    if( (0==pRight
38d68 2d 3e 69 4e 6f 64 65 20 26 26 20 53 51 4c 49 54  ->iNode && SQLIT
38d69 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 6e 6f 64 65  E_OK!=(rc = node
38d6a 57 72 69 74 65 28 70 52 74 72 65 65 2c 20 70 52  Write(pRtree, pR
38d6b 69 67 68 74 29 29 29 0a 20 20 20 7c 7c 20 28 30  ight))).   || (0
38d6c 3d 3d 70 4c 65 66 74 2d 3e 69 4e 6f 64 65 20 26  ==pLeft->iNode &
38d6d 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  & SQLITE_OK!=(rc
38d6e 20 3d 20 6e 6f 64 65 57 72 69 74 65 28 70 52 74   = nodeWrite(pRt
38d6f 72 65 65 2c 20 70 4c 65 66 74 29 29 29 0a 20 20  ree, pLeft))).  
38d70 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 70 6c 69  ){.    goto spli
38d71 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  tnode_out;.  }..
38d72 20 20 72 69 67 68 74 62 62 6f 78 2e 69 52 6f 77    rightbbox.iRow
38d73 69 64 20 3d 20 70 52 69 67 68 74 2d 3e 69 4e 6f  id = pRight->iNo
38d74 64 65 3b 0a 20 20 6c 65 66 74 62 62 6f 78 2e 69  de;.  leftbbox.i
38d75 52 6f 77 69 64 20 3d 20 70 4c 65 66 74 2d 3e 69  Rowid = pLeft->i
38d76 4e 6f 64 65 3b 0a 0a 20 20 69 66 28 20 70 4e 6f  Node;..  if( pNo
38d77 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a  de->iNode==1 ){.
38d78 20 20 20 20 72 63 20 3d 20 72 74 72 65 65 49 6e      rc = rtreeIn
38d79 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  sertCell(pRtree,
38d7a 20 70 4c 65 66 74 2d 3e 70 50 61 72 65 6e 74 2c   pLeft->pParent,
38d7b 20 26 6c 65 66 74 62 62 6f 78 2c 20 69 48 65 69   &leftbbox, iHei
38d7c 67 68 74 2b 31 29 3b 0a 20 20 20 20 69 66 28 20  ght+1);.    if( 
38d7d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
38d7e 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 70 6c 69  .      goto spli
38d7f 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 20 20 7d  tnode_out;.    }
38d80 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 52 74  .  }else{.    Rt
38d81 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74  reeNode *pParent
38d82 20 3d 20 70 4c 65 66 74 2d 3e 70 50 61 72 65 6e   = pLeft->pParen
38d83 74 3b 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c  t;.    int iCell
38d84 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64   = nodeParentInd
38d85 65 78 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74  ex(pRtree, pLeft
38d86 29 3b 0a 20 20 20 20 6e 6f 64 65 4f 76 65 72 77  );.    nodeOverw
38d87 72 69 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c  riteCell(pRtree,
38d88 20 70 50 61 72 65 6e 74 2c 20 26 6c 65 66 74 62   pParent, &leftb
38d89 62 6f 78 2c 20 69 43 65 6c 6c 29 3b 0a 20 20 20  box, iCell);.   
38d8a 20 41 64 6a 75 73 74 54 72 65 65 28 70 52 74 72   AdjustTree(pRtr
38d8b 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 26 6c 65  ee, pParent, &le
38d8c 66 74 62 62 6f 78 29 3b 0a 20 20 7d 0a 20 20 69  ftbbox);.  }.  i
38d8d 66 28 20 28 72 63 20 3d 20 72 74 72 65 65 49 6e  f( (rc = rtreeIn
38d8e 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  sertCell(pRtree,
38d8f 20 70 52 69 67 68 74 2d 3e 70 50 61 72 65 6e 74   pRight->pParent
38d90 2c 20 26 72 69 67 68 74 62 62 6f 78 2c 20 69 48  , &rightbbox, iH
38d91 65 69 67 68 74 2b 31 29 29 20 29 7b 0a 20 20 20  eight+1)) ){.   
38d92 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f   goto splitnode_
38d93 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28  out;.  }..  for(
38d94 69 3d 30 3b 20 69 3c 4e 43 45 4c 4c 28 70 52 69  i=0; i<NCELL(pRi
38d95 67 68 74 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ght); i++){.    
38d96 69 36 34 20 69 52 6f 77 69 64 20 3d 20 6e 6f 64  i64 iRowid = nod
38d97 65 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65  eGetRowid(pRtree
38d98 2c 20 70 52 69 67 68 74 2c 20 69 29 3b 0a 20 20  , pRight, i);.  
38d99 20 20 72 63 20 3d 20 75 70 64 61 74 65 4d 61 70    rc = updateMap
38d9a 70 69 6e 67 28 70 52 74 72 65 65 2c 20 69 52 6f  ping(pRtree, iRo
38d9b 77 69 64 2c 20 70 52 69 67 68 74 2c 20 69 48 65  wid, pRight, iHe
38d9c 69 67 68 74 29 3b 0a 20 20 20 20 69 66 28 20 69  ight);.    if( i
38d9d 52 6f 77 69 64 3d 3d 70 43 65 6c 6c 2d 3e 69 52  Rowid==pCell->iR
38d9e 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 6e 65  owid ){.      ne
38d9f 77 43 65 6c 6c 49 73 52 69 67 68 74 20 3d 20 31  wCellIsRight = 1
38da0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
38da1 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
38da2 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 70 6c 69  .      goto spli
38da3 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 20 20 7d  tnode_out;.    }
38da4 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e 6f 64 65  .  }.  if( pNode
38da5 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20 20  ->iNode==1 ){.  
38da6 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 45    for(i=0; i<NCE
38da7 4c 4c 28 70 4c 65 66 74 29 3b 20 69 2b 2b 29 7b  LL(pLeft); i++){
38da8 0a 20 20 20 20 20 20 69 36 34 20 69 52 6f 77 69  .      i64 iRowi
38da9 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64  d = nodeGetRowid
38daa 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 2c 20  (pRtree, pLeft, 
38dab 69 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 75  i);.      rc = u
38dac 70 64 61 74 65 4d 61 70 70 69 6e 67 28 70 52 74  pdateMapping(pRt
38dad 72 65 65 2c 20 69 52 6f 77 69 64 2c 20 70 4c 65  ree, iRowid, pLe
38dae 66 74 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20  ft, iHeight);.  
38daf 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
38db0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
38db1 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f   goto splitnode_
38db2 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  out;.      }.   
38db3 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e   }.  }else if( n
38db4 65 77 43 65 6c 6c 49 73 52 69 67 68 74 3d 3d 30  ewCellIsRight==0
38db5 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 75 70 64   ){.    rc = upd
38db6 61 74 65 4d 61 70 70 69 6e 67 28 70 52 74 72 65  ateMapping(pRtre
38db7 65 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64  e, pCell->iRowid
38db8 2c 20 70 4c 65 66 74 2c 20 69 48 65 69 67 68 74  , pLeft, iHeight
38db9 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63  );.  }..  if( rc
38dba 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
38dbb 20 20 20 72 63 20 3d 20 6e 6f 64 65 52 65 6c 65     rc = nodeRele
38dbc 61 73 65 28 70 52 74 72 65 65 2c 20 70 52 69 67  ase(pRtree, pRig
38dbd 68 74 29 3b 0a 20 20 20 20 70 52 69 67 68 74 20  ht);.    pRight 
38dbe 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  = 0;.  }.  if( r
38dbf 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
38dc0 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 52 65 6c      rc = nodeRel
38dc1 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65  ease(pRtree, pLe
38dc2 66 74 29 3b 0a 20 20 20 20 70 4c 65 66 74 20 3d  ft);.    pLeft =
38dc3 20 30 3b 0a 20 20 7d 0a 0a 73 70 6c 69 74 6e 6f   0;.  }..splitno
38dc4 64 65 5f 6f 75 74 3a 0a 20 20 6e 6f 64 65 52 65  de_out:.  nodeRe
38dc5 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 52  lease(pRtree, pR
38dc6 69 67 68 74 29 3b 0a 20 20 6e 6f 64 65 52 65 6c  ight);.  nodeRel
38dc7 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65  ease(pRtree, pLe
38dc8 66 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ft);.  sqlite3_f
38dc9 72 65 65 28 61 43 65 6c 6c 29 3b 0a 20 20 72 65  ree(aCell);.  re
38dca 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74  turn rc;.}..stat
38dcb 69 63 20 69 6e 74 20 66 69 78 4c 65 61 66 50 61  ic int fixLeafPa
38dcc 72 65 6e 74 28 52 74 72 65 65 20 2a 70 52 74 72  rent(Rtree *pRtr
38dcd 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ee, RtreeNode *p
38dce 4c 65 61 66 29 7b 0a 20 20 69 6e 74 20 72 63 20  Leaf){.  int rc 
38dcf 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
38dd0 66 28 20 70 4c 65 61 66 2d 3e 69 4e 6f 64 65 21  f( pLeaf->iNode!
38dd1 3d 31 20 26 26 20 70 4c 65 61 66 2d 3e 70 50 61  =1 && pLeaf->pPa
38dd2 72 65 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73  rent==0 ){.    s
38dd3 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
38dd4 34 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 50  4(pRtree->pReadP
38dd5 61 72 65 6e 74 2c 20 31 2c 20 70 4c 65 61 66 2d  arent, 1, pLeaf-
38dd6 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20 69 66 28  >iNode);.    if(
38dd7 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52   sqlite3_step(pR
38dd8 74 72 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e  tree->pReadParen
38dd9 74 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  t)==SQLITE_ROW )
38dda 7b 0a 20 20 20 20 20 20 69 36 34 20 69 4e 6f 64  {.      i64 iNod
38ddb 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  e = sqlite3_colu
38ddc 6d 6e 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d  mn_int64(pRtree-
38ddd 3e 70 52 65 61 64 50 61 72 65 6e 74 2c 20 30 29  >pReadParent, 0)
38dde 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 6f 64  ;.      rc = nod
38ddf 65 41 63 71 75 69 72 65 28 70 52 74 72 65 65 2c  eAcquire(pRtree,
38de0 20 69 4e 6f 64 65 2c 20 30 2c 20 26 70 4c 65 61   iNode, 0, &pLea
38de1 66 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20 20  f->pParent);.   
38de2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
38de3 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
38de4 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
38de5 65 33 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d  e3_reset(pRtree-
38de6 3e 70 52 65 61 64 50 61 72 65 6e 74 29 3b 0a 20  >pReadParent);. 
38de7 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
38de8 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63  E_OK ){.      rc
38de9 20 3d 20 66 69 78 4c 65 61 66 50 61 72 65 6e 74   = fixLeafParent
38dea 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66 2d 3e  (pRtree, pLeaf->
38deb 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d 0a  pParent);.    }.
38dec 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
38ded 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .}..static int d
38dee 65 6c 65 74 65 43 65 6c 6c 28 52 74 72 65 65 20  eleteCell(Rtree 
38def 2a 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 2c 20  *, RtreeNode *, 
38df0 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 73 74 61 74  int, int);..stat
38df1 69 63 20 69 6e 74 20 72 65 6d 6f 76 65 4e 6f 64  ic int removeNod
38df2 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  e(Rtree *pRtree,
38df3 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
38df4 65 2c 20 69 6e 74 20 69 48 65 69 67 68 74 29 7b  e, int iHeight){
38df5 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 52 74 72  .  int rc;.  Rtr
38df6 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 3b  eeNode *pParent;
38df7 0a 20 20 69 6e 74 20 69 43 65 6c 6c 3b 0a 0a 20  .  int iCell;.. 
38df8 20 61 73 73 65 72 74 28 20 70 4e 6f 64 65 2d 3e   assert( pNode->
38df9 6e 52 65 66 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a  nRef==1 );..  /*
38dfa 20 52 65 6d 6f 76 65 20 74 68 65 20 65 6e 74 72   Remove the entr
38dfb 79 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20  y in the parent 
38dfc 63 65 6c 6c 2e 20 2a 2f 0a 20 20 69 43 65 6c 6c  cell. */.  iCell
38dfd 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64   = nodeParentInd
38dfe 65 78 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ex(pRtree, pNode
38dff 29 3b 0a 20 20 70 50 61 72 65 6e 74 20 3d 20 70  );.  pParent = p
38e00 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20  Node->pParent;. 
38e01 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20   pNode->pParent 
38e02 3d 20 30 3b 0a 20 20 69 66 28 20 53 51 4c 49 54  = 0;.  if( SQLIT
38e03 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 64 65 6c 65  E_OK!=(rc = dele
38e04 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  teCell(pRtree, p
38e05 50 61 72 65 6e 74 2c 20 69 43 65 6c 6c 2c 20 69  Parent, iCell, i
38e06 48 65 69 67 68 74 2b 31 29 29 20 0a 20 20 20 7c  Height+1)) .   |
38e07 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  | SQLITE_OK!=(rc
38e08 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70   = nodeRelease(p
38e09 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74 29 29  Rtree, pParent))
38e0a 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  .  ){.    return
38e0b 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52   rc;.  }..  /* R
38e0c 65 6d 6f 76 65 20 74 68 65 20 78 78 78 5f 6e 6f  emove the xxx_no
38e0d 64 65 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 73  de entry. */.  s
38e0e 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
38e0f 34 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  4(pRtree->pDelet
38e10 65 4e 6f 64 65 2c 20 31 2c 20 70 4e 6f 64 65 2d  eNode, 1, pNode-
38e11 3e 69 4e 6f 64 65 29 3b 0a 20 20 73 71 6c 69 74  >iNode);.  sqlit
38e12 65 33 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e  e3_step(pRtree->
38e13 70 44 65 6c 65 74 65 4e 6f 64 65 29 3b 0a 20 20  pDeleteNode);.  
38e14 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28  if( SQLITE_OK!=(
38e15 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73  rc = sqlite3_res
38e16 65 74 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  et(pRtree->pDele
38e17 74 65 4e 6f 64 65 29 29 20 29 7b 0a 20 20 20 20  teNode)) ){.    
38e18 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
38e19 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20    /* Remove the 
38e1a 78 78 78 5f 70 61 72 65 6e 74 20 65 6e 74 72 79  xxx_parent entry
38e1b 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62  . */.  sqlite3_b
38e1c 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65  ind_int64(pRtree
38e1d 2d 3e 70 44 65 6c 65 74 65 50 61 72 65 6e 74 2c  ->pDeleteParent,
38e1e 20 31 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65   1, pNode->iNode
38e1f 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65  );.  sqlite3_ste
38e20 70 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  p(pRtree->pDelet
38e21 65 50 61 72 65 6e 74 29 3b 0a 20 20 69 66 28 20  eParent);.  if( 
38e22 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
38e23 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
38e24 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61  Rtree->pDeletePa
38e25 72 65 6e 74 29 29 20 29 7b 0a 20 20 20 20 72 65  rent)) ){.    re
38e26 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 0a  turn rc;.  }.  .
38e27 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20    /* Remove the 
38e28 6e 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 69 6e  node from the in
38e29 2d 6d 65 6d 6f 72 79 20 68 61 73 68 20 74 61 62  -memory hash tab
38e2a 6c 65 20 61 6e 64 20 6c 69 6e 6b 20 69 74 20 69  le and link it i
38e2b 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20 52 74 72  nto.  ** the Rtr
38e2c 65 65 2e 70 44 65 6c 65 74 65 64 20 6c 69 73 74  ee.pDeleted list
38e2d 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74 73 20 77  . Its contents w
38e2e 69 6c 6c 20 62 65 20 72 65 2d 69 6e 73 65 72 74  ill be re-insert
38e2f 65 64 20 6c 61 74 65 72 20 6f 6e 2e 0a 20 20 2a  ed later on..  *
38e30 2f 0a 20 20 6e 6f 64 65 48 61 73 68 44 65 6c 65  /.  nodeHashDele
38e31 74 65 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  te(pRtree, pNode
38e32 29 3b 0a 20 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64  );.  pNode->iNod
38e33 65 20 3d 20 69 48 65 69 67 68 74 3b 0a 20 20 70  e = iHeight;.  p
38e34 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d 20 70 52  Node->pNext = pR
38e35 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 64 3b 0a  tree->pDeleted;.
38e36 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 2b 2b 3b    pNode->nRef++;
38e37 0a 20 20 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  .  pRtree->pDele
38e38 74 65 64 20 3d 20 70 4e 6f 64 65 3b 0a 0a 20 20  ted = pNode;..  
38e39 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
38e3a 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
38e3b 20 66 69 78 42 6f 75 6e 64 69 6e 67 42 6f 78 28   fixBoundingBox(
38e3c 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52  Rtree *pRtree, R
38e3d 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29  treeNode *pNode)
38e3e 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  {.  RtreeNode *p
38e3f 50 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65 2d 3e  Parent = pNode->
38e40 70 50 61 72 65 6e 74 3b 0a 20 20 69 66 28 20 70  pParent;.  if( p
38e41 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e  Parent ){.    in
38e42 74 20 69 69 3b 20 0a 20 20 20 20 69 6e 74 20 6e  t ii; .    int n
38e43 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f  Cell = NCELL(pNo
38e44 64 65 29 3b 0a 20 20 20 20 52 74 72 65 65 43 65  de);.    RtreeCe
38e45 6c 6c 20 62 6f 78 3b 20 20 20 20 20 20 20 20 20  ll box;         
38e46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38e47 20 20 20 2f 2a 20 42 6f 75 6e 64 69 6e 67 20 62     /* Bounding b
38e48 6f 78 20 66 6f 72 20 70 4e 6f 64 65 20 2a 2f 0a  ox for pNode */.
38e49 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28      nodeGetCell(
38e4a 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 30  pRtree, pNode, 0
38e4b 2c 20 26 62 6f 78 29 3b 0a 20 20 20 20 66 6f 72  , &box);.    for
38e4c 28 69 69 3d 31 3b 20 69 69 3c 6e 43 65 6c 6c 3b  (ii=1; ii<nCell;
38e4d 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 52 74   ii++){.      Rt
38e4e 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20  reeCell cell;.  
38e4f 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28      nodeGetCell(
38e50 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69  pRtree, pNode, i
38e51 69 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 20 20 20  i, &cell);.     
38e52 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65   cellUnion(pRtre
38e53 65 2c 20 26 62 6f 78 2c 20 26 63 65 6c 6c 29 3b  e, &box, &cell);
38e54 0a 20 20 20 20 7d 0a 20 20 20 20 62 6f 78 2e 69  .    }.    box.i
38e55 52 6f 77 69 64 20 3d 20 70 4e 6f 64 65 2d 3e 69  Rowid = pNode->i
38e56 4e 6f 64 65 3b 0a 20 20 20 20 69 69 20 3d 20 6e  Node;.    ii = n
38e57 6f 64 65 50 61 72 65 6e 74 49 6e 64 65 78 28 70  odeParentIndex(p
38e58 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20  Rtree, pNode);. 
38e59 20 20 20 6e 6f 64 65 4f 76 65 72 77 72 69 74 65     nodeOverwrite
38e5a 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 50 61  Cell(pRtree, pPa
38e5b 72 65 6e 74 2c 20 26 62 6f 78 2c 20 69 69 29 3b  rent, &box, ii);
38e5c 0a 20 20 20 20 66 69 78 42 6f 75 6e 64 69 6e 67  .    fixBounding
38e5d 42 6f 78 28 70 52 74 72 65 65 2c 20 70 50 61 72  Box(pRtree, pPar
38e5e 65 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ent);.  }.}../*.
38e5f 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 63 65  ** Delete the ce
38e60 6c 6c 20 61 74 20 69 6e 64 65 78 20 69 43 65 6c  ll at index iCel
38e61 6c 20 6f 66 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  l of node pNode.
38e62 20 41 66 74 65 72 20 72 65 6d 6f 76 69 6e 67 20   After removing 
38e63 74 68 65 0a 2a 2a 20 63 65 6c 6c 2c 20 61 64 6a  the.** cell, adj
38e64 75 73 74 20 74 68 65 20 72 2d 74 72 65 65 20 64  ust the r-tree d
38e65 61 74 61 20 73 74 72 75 63 74 75 72 65 20 69 66  ata structure if
38e66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 73 74   required..*/.st
38e67 61 74 69 63 20 69 6e 74 20 64 65 6c 65 74 65 43  atic int deleteC
38e68 65 6c 6c 28 52 74 72 65 65 20 2a 70 52 74 72 65  ell(Rtree *pRtre
38e69 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e  e, RtreeNode *pN
38e6a 6f 64 65 2c 20 69 6e 74 20 69 43 65 6c 6c 2c 20  ode, int iCell, 
38e6b 69 6e 74 20 69 48 65 69 67 68 74 29 7b 0a 20 20  int iHeight){.  
38e6c 69 6e 74 20 72 63 3b 0a 0a 20 20 69 66 28 20 53  int rc;..  if( S
38e6d 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
38e6e 66 69 78 4c 65 61 66 50 61 72 65 6e 74 28 70 52  fixLeafParent(pR
38e6f 74 72 65 65 2c 20 70 4e 6f 64 65 29 29 20 29 7b  tree, pNode)) ){
38e70 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
38e71 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65    }..  /* Remove
38e72 20 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 74   the cell from t
38e73 68 65 20 6e 6f 64 65 2e 20 54 68 69 73 20 63 61  he node. This ca
38e74 6c 6c 20 6a 75 73 74 20 6d 6f 76 65 73 20 62 79  ll just moves by
38e75 74 65 73 20 61 72 6f 75 6e 64 0a 20 20 2a 2a 20  tes around.  ** 
38e76 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 6e 6f  the in-memory no
38e77 64 65 20 69 6d 61 67 65 2c 20 73 6f 20 69 74 20  de image, so it 
38e78 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 0a 20 20 2a  cannot fail..  *
38e79 2f 0a 20 20 6e 6f 64 65 44 65 6c 65 74 65 43 65  /.  nodeDeleteCe
38e7a 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ll(pRtree, pNode
38e7b 2c 20 69 43 65 6c 6c 29 3b 0a 0a 20 20 2f 2a 20  , iCell);..  /* 
38e7c 49 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20 6e  If the node is n
38e7d 6f 74 20 74 68 65 20 74 72 65 65 20 72 6f 6f 74  ot the tree root
38e7e 20 61 6e 64 20 6e 6f 77 20 68 61 73 20 6c 65 73   and now has les
38e7f 73 20 74 68 61 6e 20 74 68 65 20 6d 69 6e 69 6d  s than the minim
38e80 75 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f  um.  ** number o
38e81 66 20 63 65 6c 6c 73 2c 20 72 65 6d 6f 76 65 20  f cells, remove 
38e82 69 74 20 66 72 6f 6d 20 74 68 65 20 74 72 65 65  it from the tree
38e83 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 75 70 64  . Otherwise, upd
38e84 61 74 65 20 74 68 65 0a 20 20 2a 2a 20 63 65 6c  ate the.  ** cel
38e85 6c 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20  l in the parent 
38e86 6e 6f 64 65 20 73 6f 20 74 68 61 74 20 69 74 20  node so that it 
38e87 74 69 67 68 74 6c 79 20 63 6f 6e 74 61 69 6e 73  tightly contains
38e88 20 74 68 65 20 75 70 64 61 74 65 64 0a 20 20 2a   the updated.  *
38e89 2a 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69  * node..  */.  i
38e8a 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 21  f( pNode->iNode!
38e8b 3d 31 20 29 7b 0a 20 20 20 20 52 74 72 65 65 4e  =1 ){.    RtreeN
38e8c 6f 64 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70  ode *pParent = p
38e8d 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20  Node->pParent;. 
38e8e 20 20 20 69 66 28 20 28 70 50 61 72 65 6e 74 2d     if( (pParent-
38e8f 3e 69 4e 6f 64 65 21 3d 31 20 7c 7c 20 4e 43 45  >iNode!=1 || NCE
38e90 4c 4c 28 70 50 61 72 65 6e 74 29 21 3d 31 29 20  LL(pParent)!=1) 
38e91 0a 20 20 20 20 20 26 26 20 28 4e 43 45 4c 4c 28  .     && (NCELL(
38e92 70 4e 6f 64 65 29 3c 52 54 52 45 45 5f 4d 49 4e  pNode)<RTREE_MIN
38e93 43 45 4c 4c 53 28 70 52 74 72 65 65 29 29 0a 20  CELLS(pRtree)). 
38e94 20 20 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d     ){.      rc =
38e95 20 72 65 6d 6f 76 65 4e 6f 64 65 28 70 52 74 72   removeNode(pRtr
38e96 65 65 2c 20 70 4e 6f 64 65 2c 20 69 48 65 69 67  ee, pNode, iHeig
38e97 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ht);.    }else{.
38e98 20 20 20 20 20 20 66 69 78 42 6f 75 6e 64 69 6e        fixBoundin
38e99 67 42 6f 78 28 70 52 74 72 65 65 2c 20 70 4e 6f  gBox(pRtree, pNo
38e9a 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  de);.    }.  }..
38e9b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
38e9c 73 74 61 74 69 63 20 69 6e 74 20 52 65 69 6e 73  static int Reins
38e9d 65 72 74 28 0a 20 20 52 74 72 65 65 20 2a 70 52  ert(.  Rtree *pR
38e9e 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f  tree, .  RtreeNo
38e9f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 52 74  de *pNode, .  Rt
38ea0 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c 20  reeCell *pCell, 
38ea1 0a 20 20 69 6e 74 20 69 48 65 69 67 68 74 0a 29  .  int iHeight.)
38ea2 7b 0a 20 20 69 6e 74 20 2a 61 4f 72 64 65 72 3b  {.  int *aOrder;
38ea3 0a 20 20 69 6e 74 20 2a 61 53 70 61 72 65 3b 0a  .  int *aSpare;.
38ea4 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65    RtreeCell *aCe
38ea5 6c 6c 3b 0a 20 20 66 6c 6f 61 74 20 2a 61 44 69  ll;.  float *aDi
38ea6 73 74 61 6e 63 65 3b 0a 20 20 69 6e 74 20 6e 43  stance;.  int nC
38ea7 65 6c 6c 3b 0a 20 20 66 6c 6f 61 74 20 61 43 65  ell;.  float aCe
38ea8 6e 74 65 72 43 6f 6f 72 64 5b 52 54 52 45 45 5f  nterCoord[RTREE_
38ea9 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 5d 3b  MAX_DIMENSIONS];
38eaa 0a 20 20 69 6e 74 20 69 44 69 6d 3b 0a 20 20 69  .  int iDim;.  i
38eab 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 72 63 20  nt ii;.  int rc 
38eac 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  = SQLITE_OK;..  
38ead 6d 65 6d 73 65 74 28 61 43 65 6e 74 65 72 43 6f  memset(aCenterCo
38eae 6f 72 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66  ord, 0, sizeof(f
38eaf 6c 6f 61 74 29 2a 52 54 52 45 45 5f 4d 41 58 5f  loat)*RTREE_MAX_
38eb0 44 49 4d 45 4e 53 49 4f 4e 53 29 3b 0a 0a 20 20  DIMENSIONS);..  
38eb1 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e  nCell = NCELL(pN
38eb2 6f 64 65 29 2b 31 3b 0a 0a 20 20 2f 2a 20 41 6c  ode)+1;..  /* Al
38eb3 6c 6f 63 61 74 65 20 74 68 65 20 62 75 66 66 65  locate the buffe
38eb4 72 73 20 75 73 65 64 20 62 79 20 74 68 69 73 20  rs used by this 
38eb5 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 65 20 61  operation. The a
38eb6 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 0a 20 20 2a  llocation is.  *
38eb7 2a 20 72 65 6c 69 6e 71 75 69 73 68 65 64 20 62  * relinquished b
38eb8 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74  efore this funct
38eb9 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a 20 20 2a  ion returns..  *
38eba 2f 0a 20 20 61 43 65 6c 6c 20 3d 20 28 52 74 72  /.  aCell = (Rtr
38ebb 65 65 43 65 6c 6c 20 2a 29 73 71 6c 69 74 65 33  eeCell *)sqlite3
38ebc 5f 6d 61 6c 6c 6f 63 28 6e 43 65 6c 6c 20 2a 20  _malloc(nCell * 
38ebd 28 0a 20 20 20 20 73 69 7a 65 6f 66 28 52 74 72  (.    sizeof(Rtr
38ebe 65 65 43 65 6c 6c 29 20 2b 20 20 20 20 20 20 20  eeCell) +       
38ebf 20 20 2f 2a 20 61 43 65 6c 6c 20 61 72 72 61 79    /* aCell array
38ec0 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 69   */.    sizeof(i
38ec1 6e 74 29 20 20 20 20 20 20 20 2b 20 20 20 20 20  nt)       +     
38ec2 20 20 20 20 2f 2a 20 61 4f 72 64 65 72 20 61 72      /* aOrder ar
38ec3 72 61 79 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f  ray */.    sizeo
38ec4 66 28 69 6e 74 29 20 20 20 20 20 20 20 2b 20 20  f(int)       +  
38ec5 20 20 20 20 20 20 20 2f 2a 20 61 53 70 61 72 65         /* aSpare
38ec6 20 61 72 72 61 79 20 2a 2f 0a 20 20 20 20 73 69   array */.    si
38ec7 7a 65 6f 66 28 66 6c 6f 61 74 29 20 20 20 20 20  zeof(float)     
38ec8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 44 69            /* aDi
38ec9 73 74 61 6e 63 65 20 61 72 72 61 79 20 2a 2f 0a  stance array */.
38eca 20 20 29 29 3b 0a 20 20 69 66 28 20 21 61 43 65    ));.  if( !aCe
38ecb 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ll ){.    return
38ecc 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
38ecd 20 7d 0a 20 20 61 4f 72 64 65 72 20 20 20 20 3d   }.  aOrder    =
38ece 20 28 69 6e 74 20 2a 29 26 61 43 65 6c 6c 5b 6e   (int *)&aCell[n
38ecf 43 65 6c 6c 5d 3b 0a 20 20 61 53 70 61 72 65 20  Cell];.  aSpare 
38ed0 20 20 20 3d 20 28 69 6e 74 20 2a 29 26 61 4f 72     = (int *)&aOr
38ed1 64 65 72 5b 6e 43 65 6c 6c 5d 3b 0a 20 20 61 44  der[nCell];.  aD
38ed2 69 73 74 61 6e 63 65 20 3d 20 28 66 6c 6f 61 74  istance = (float
38ed3 20 2a 29 26 61 53 70 61 72 65 5b 6e 43 65 6c 6c   *)&aSpare[nCell
38ed4 5d 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  ];..  for(ii=0; 
38ed5 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b  ii<nCell; ii++){
38ed6 0a 20 20 20 20 69 66 28 20 69 69 3d 3d 28 6e 43  .    if( ii==(nC
38ed7 65 6c 6c 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  ell-1) ){.      
38ed8 6d 65 6d 63 70 79 28 26 61 43 65 6c 6c 5b 69 69  memcpy(&aCell[ii
38ed9 5d 2c 20 70 43 65 6c 6c 2c 20 73 69 7a 65 6f 66  ], pCell, sizeof
38eda 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20  (RtreeCell));.  
38edb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e    }else{.      n
38edc 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65  odeGetCell(pRtre
38edd 65 2c 20 70 4e 6f 64 65 2c 20 69 69 2c 20 26 61  e, pNode, ii, &a
38ede 43 65 6c 6c 5b 69 69 5d 29 3b 0a 20 20 20 20 7d  Cell[ii]);.    }
38edf 0a 20 20 20 20 61 4f 72 64 65 72 5b 69 69 5d 20  .    aOrder[ii] 
38ee0 3d 20 69 69 3b 0a 20 20 20 20 66 6f 72 28 69 44  = ii;.    for(iD
38ee1 69 6d 3d 30 3b 20 69 44 69 6d 3c 70 52 74 72 65  im=0; iDim<pRtre
38ee2 65 2d 3e 6e 44 69 6d 3b 20 69 44 69 6d 2b 2b 29  e->nDim; iDim++)
38ee3 7b 0a 20 20 20 20 20 20 61 43 65 6e 74 65 72 43  {.      aCenterC
38ee4 6f 6f 72 64 5b 69 44 69 6d 5d 20 2b 3d 20 44 43  oord[iDim] += DC
38ee5 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61  OORD(aCell[ii].a
38ee6 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a  Coord[iDim*2]);.
38ee7 20 20 20 20 20 20 61 43 65 6e 74 65 72 43 6f 6f        aCenterCoo
38ee8 72 64 5b 69 44 69 6d 5d 20 2b 3d 20 44 43 4f 4f  rd[iDim] += DCOO
38ee9 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f  RD(aCell[ii].aCo
38eea 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 29 3b 0a  ord[iDim*2+1]);.
38eeb 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28      }.  }.  for(
38eec 69 44 69 6d 3d 30 3b 20 69 44 69 6d 3c 70 52 74  iDim=0; iDim<pRt
38eed 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 44 69 6d 2b  ree->nDim; iDim+
38eee 2b 29 7b 0a 20 20 20 20 61 43 65 6e 74 65 72 43  +){.    aCenterC
38eef 6f 6f 72 64 5b 69 44 69 6d 5d 20 3d 20 61 43 65  oord[iDim] = aCe
38ef0 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 2f  nterCoord[iDim]/
38ef1 28 28 66 6c 6f 61 74 29 6e 43 65 6c 6c 2a 32 2e  ((float)nCell*2.
38ef2 30 29 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69  0);.  }..  for(i
38ef3 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69  i=0; ii<nCell; i
38ef4 69 2b 2b 29 7b 0a 20 20 20 20 61 44 69 73 74 61  i++){.    aDista
38ef5 6e 63 65 5b 69 69 5d 20 3d 20 30 2e 30 3b 0a 20  nce[ii] = 0.0;. 
38ef6 20 20 20 66 6f 72 28 69 44 69 6d 3d 30 3b 20 69     for(iDim=0; i
38ef7 44 69 6d 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d  Dim<pRtree->nDim
38ef8 3b 20 69 44 69 6d 2b 2b 29 7b 0a 20 20 20 20 20  ; iDim++){.     
38ef9 20 66 6c 6f 61 74 20 63 6f 6f 72 64 20 3d 20 44   float coord = D
38efa 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e  COORD(aCell[ii].
38efb 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d  aCoord[iDim*2+1]
38efc 29 20 2d 20 0a 20 20 20 20 20 20 20 20 20 20 44  ) - .          D
38efd 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e  COORD(aCell[ii].
38efe 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 29 3b  aCoord[iDim*2]);
38eff 0a 20 20 20 20 20 20 61 44 69 73 74 61 6e 63 65  .      aDistance
38f00 5b 69 69 5d 20 2b 3d 20 28 63 6f 6f 72 64 2d 61  [ii] += (coord-a
38f01 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69 6d  CenterCoord[iDim
38f02 5d 29 2a 28 63 6f 6f 72 64 2d 61 43 65 6e 74 65  ])*(coord-aCente
38f03 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 29 3b 0a 20  rCoord[iDim]);. 
38f04 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 53 6f 72 74     }.  }..  Sort
38f05 42 79 44 69 73 74 61 6e 63 65 28 61 4f 72 64 65  ByDistance(aOrde
38f06 72 2c 20 6e 43 65 6c 6c 2c 20 61 44 69 73 74 61  r, nCell, aDista
38f07 6e 63 65 2c 20 61 53 70 61 72 65 29 3b 0a 20 20  nce, aSpare);.  
38f08 6e 6f 64 65 5a 65 72 6f 28 70 52 74 72 65 65 2c  nodeZero(pRtree,
38f09 20 70 4e 6f 64 65 29 3b 0a 0a 20 20 66 6f 72 28   pNode);..  for(
38f0a 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45  ii=0; rc==SQLITE
38f0b 5f 4f 4b 20 26 26 20 69 69 3c 28 6e 43 65 6c 6c  _OK && ii<(nCell
38f0c 2d 28 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53  -(RTREE_MINCELLS
38f0d 28 70 52 74 72 65 65 29 2b 31 29 29 3b 20 69 69  (pRtree)+1)); ii
38f0e 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65 65 43 65  ++){.    RtreeCe
38f0f 6c 6c 20 2a 70 20 3d 20 26 61 43 65 6c 6c 5b 61  ll *p = &aCell[a
38f10 4f 72 64 65 72 5b 69 69 5d 5d 3b 0a 20 20 20 20  Order[ii]];.    
38f11 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70  nodeInsertCell(p
38f12 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 29  Rtree, pNode, p)
38f13 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52 6f  ;.    if( p->iRo
38f14 77 69 64 3d 3d 70 43 65 6c 6c 2d 3e 69 52 6f 77  wid==pCell->iRow
38f15 69 64 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  id ){.      if( 
38f16 69 48 65 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20  iHeight==0 ){.  
38f17 20 20 20 20 20 20 72 63 20 3d 20 72 6f 77 69 64        rc = rowid
38f18 57 72 69 74 65 28 70 52 74 72 65 65 2c 20 70 2d  Write(pRtree, p-
38f19 3e 69 52 6f 77 69 64 2c 20 70 4e 6f 64 65 2d 3e  >iRowid, pNode->
38f1a 69 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 7d 65  iNode);.      }e
38f1b 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20  lse{.        rc 
38f1c 3d 20 70 61 72 65 6e 74 57 72 69 74 65 28 70 52  = parentWrite(pR
38f1d 74 72 65 65 2c 20 70 2d 3e 69 52 6f 77 69 64 2c  tree, p->iRowid,
38f1e 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a   pNode->iNode);.
38f1f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
38f20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  }.  if( rc==SQLI
38f21 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 66 69 78  TE_OK ){.    fix
38f22 42 6f 75 6e 64 69 6e 67 42 6f 78 28 70 52 74 72  BoundingBox(pRtr
38f23 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 7d 0a  ee, pNode);.  }.
38f24 20 20 66 6f 72 28 3b 20 72 63 3d 3d 53 51 4c 49    for(; rc==SQLI
38f25 54 45 5f 4f 4b 20 26 26 20 69 69 3c 6e 43 65 6c  TE_OK && ii<nCel
38f26 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 2f 2a  l; ii++){.    /*
38f27 20 46 69 6e 64 20 61 20 6e 6f 64 65 20 74 6f 20   Find a node to 
38f28 73 74 6f 72 65 20 74 68 69 73 20 63 65 6c 6c 20  store this cell 
38f29 69 6e 2e 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  in. pNode->iNode
38f2a 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61   currently conta
38f2b 69 6e 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 68  ins.    ** the h
38f2c 65 69 67 68 74 20 6f 66 20 74 68 65 20 73 75 62  eight of the sub
38f2d 2d 74 72 65 65 20 68 65 61 64 65 64 20 62 79 20  -tree headed by 
38f2e 74 68 65 20 63 65 6c 6c 2e 0a 20 20 20 20 2a 2f  the cell..    */
38f2f 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  .    RtreeNode *
38f30 70 49 6e 73 65 72 74 3b 0a 20 20 20 20 52 74 72  pInsert;.    Rtr
38f31 65 65 43 65 6c 6c 20 2a 70 20 3d 20 26 61 43 65  eeCell *p = &aCe
38f32 6c 6c 5b 61 4f 72 64 65 72 5b 69 69 5d 5d 3b 0a  ll[aOrder[ii]];.
38f33 20 20 20 20 72 63 20 3d 20 43 68 6f 6f 73 65 4c      rc = ChooseL
38f34 65 61 66 28 70 52 74 72 65 65 2c 20 70 2c 20 69  eaf(pRtree, p, i
38f35 48 65 69 67 68 74 2c 20 26 70 49 6e 73 65 72 74  Height, &pInsert
38f36 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
38f37 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
38f38 20 20 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 20    int rc2;.     
38f39 20 72 63 20 3d 20 72 74 72 65 65 49 6e 73 65 72   rc = rtreeInser
38f3a 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 49  tCell(pRtree, pI
38f3b 6e 73 65 72 74 2c 20 70 2c 20 69 48 65 69 67 68  nsert, p, iHeigh
38f3c 74 29 3b 0a 20 20 20 20 20 20 72 63 32 20 3d 20  t);.      rc2 = 
38f3d 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72  nodeRelease(pRtr
38f3e 65 65 2c 20 70 49 6e 73 65 72 74 29 3b 0a 20 20  ee, pInsert);.  
38f3f 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
38f40 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
38f41 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 20   rc = rc2;.     
38f42 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
38f43 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 43 65  sqlite3_free(aCe
38f44 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ll);.  return rc
38f45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72  ;.}../*.** Inser
38f46 74 20 63 65 6c 6c 20 70 43 65 6c 6c 20 69 6e 74  t cell pCell int
38f47 6f 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 4e 6f  o node pNode. No
38f48 64 65 20 70 4e 6f 64 65 20 69 73 20 74 68 65 20  de pNode is the 
38f49 68 65 61 64 20 6f 66 20 61 20 0a 2a 2a 20 73 75  head of a .** su
38f4a 62 74 72 65 65 20 69 48 65 69 67 68 74 20 68 69  btree iHeight hi
38f4b 67 68 20 28 6c 65 61 66 20 6e 6f 64 65 73 20 68  gh (leaf nodes h
38f4c 61 76 65 20 69 48 65 69 67 68 74 3d 3d 30 29 2e  ave iHeight==0).
38f4d 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
38f4e 74 72 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 0a  treeInsertCell(.
38f4f 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c    Rtree *pRtree,
38f50 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e  .  RtreeNode *pN
38f51 6f 64 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c  ode,.  RtreeCell
38f52 20 2a 70 43 65 6c 6c 2c 0a 20 20 69 6e 74 20 69   *pCell,.  int i
38f53 48 65 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74 20  Height.){.  int 
38f54 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
38f55 20 20 69 66 28 20 69 48 65 69 67 68 74 3e 30 20    if( iHeight>0 
38f56 29 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65  ){.    RtreeNode
38f57 20 2a 70 43 68 69 6c 64 20 3d 20 6e 6f 64 65 48   *pChild = nodeH
38f58 61 73 68 4c 6f 6f 6b 75 70 28 70 52 74 72 65 65  ashLookup(pRtree
38f59 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 29  , pCell->iRowid)
38f5a 3b 0a 20 20 20 20 69 66 28 20 70 43 68 69 6c 64  ;.    if( pChild
38f5b 20 29 7b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65   ){.      nodeRe
38f5c 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 43  lease(pRtree, pC
38f5d 68 69 6c 64 2d 3e 70 50 61 72 65 6e 74 29 3b 0a  hild->pParent);.
38f5e 20 20 20 20 20 20 6e 6f 64 65 52 65 66 65 72 65        nodeRefere
38f5f 6e 63 65 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20  nce(pNode);.    
38f60 20 20 70 43 68 69 6c 64 2d 3e 70 50 61 72 65 6e    pChild->pParen
38f61 74 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 20 20 7d  t = pNode;.    }
38f62 0a 20 20 7d 0a 20 20 69 66 28 20 6e 6f 64 65 49  .  }.  if( nodeI
38f63 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65  nsertCell(pRtree
38f64 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 29 20  , pNode, pCell) 
38f65 29 7b 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 52  ){.#if VARIANT_R
38f66 53 54 41 52 54 52 45 45 5f 52 45 49 4e 53 45 52  STARTREE_REINSER
38f67 54 0a 20 20 20 20 69 66 28 20 69 48 65 69 67 68  T.    if( iHeigh
38f68 74 3c 3d 70 52 74 72 65 65 2d 3e 69 52 65 69 6e  t<=pRtree->iRein
38f69 73 65 72 74 48 65 69 67 68 74 20 7c 7c 20 70 4e  sertHeight || pN
38f6a 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 29 7b 0a  ode->iNode==1){.
38f6b 20 20 20 20 20 20 72 63 20 3d 20 53 70 6c 69 74        rc = Split
38f6c 4e 6f 64 65 28 70 52 74 72 65 65 2c 20 70 4e 6f  Node(pRtree, pNo
38f6d 64 65 2c 20 70 43 65 6c 6c 2c 20 69 48 65 69 67  de, pCell, iHeig
38f6e 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ht);.    }else{.
38f6f 20 20 20 20 20 20 70 52 74 72 65 65 2d 3e 69 52        pRtree->iR
38f70 65 69 6e 73 65 72 74 48 65 69 67 68 74 20 3d 20  einsertHeight = 
38f71 69 48 65 69 67 68 74 3b 0a 20 20 20 20 20 20 72  iHeight;.      r
38f72 63 20 3d 20 52 65 69 6e 73 65 72 74 28 70 52 74  c = Reinsert(pRt
38f73 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c  ree, pNode, pCel
38f74 6c 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20 20  l, iHeight);.   
38f75 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 72 63 20   }.#else.    rc 
38f76 3d 20 53 70 6c 69 74 4e 6f 64 65 28 70 52 74 72  = SplitNode(pRtr
38f77 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c 6c  ee, pNode, pCell
38f78 2c 20 69 48 65 69 67 68 74 29 3b 0a 23 65 6e 64  , iHeight);.#end
38f79 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  if.  }else{.    
38f7a 41 64 6a 75 73 74 54 72 65 65 28 70 52 74 72 65  AdjustTree(pRtre
38f7b 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 29  e, pNode, pCell)
38f7c 3b 0a 20 20 20 20 69 66 28 20 69 48 65 69 67 68  ;.    if( iHeigh
38f7d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63  t==0 ){.      rc
38f7e 20 3d 20 72 6f 77 69 64 57 72 69 74 65 28 70 52   = rowidWrite(pR
38f7f 74 72 65 65 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f  tree, pCell->iRo
38f80 77 69 64 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64  wid, pNode->iNod
38f81 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  e);.    }else{. 
38f82 20 20 20 20 20 72 63 20 3d 20 70 61 72 65 6e 74       rc = parent
38f83 57 72 69 74 65 28 70 52 74 72 65 65 2c 20 70 43  Write(pRtree, pC
38f84 65 6c 6c 2d 3e 69 52 6f 77 69 64 2c 20 70 4e 6f  ell->iRowid, pNo
38f85 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20  de->iNode);.    
38f86 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
38f87 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  c;.}..static int
38f88 20 72 65 69 6e 73 65 72 74 4e 6f 64 65 43 6f 6e   reinsertNodeCon
38f89 74 65 6e 74 28 52 74 72 65 65 20 2a 70 52 74 72  tent(Rtree *pRtr
38f8a 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ee, RtreeNode *p
38f8b 4e 6f 64 65 29 7b 0a 20 20 69 6e 74 20 69 69 3b  Node){.  int ii;
38f8c 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
38f8d 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 43 65  TE_OK;.  int nCe
38f8e 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65  ll = NCELL(pNode
38f8f 29 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  );..  for(ii=0; 
38f90 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
38f91 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29   ii<nCell; ii++)
38f92 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20  {.    RtreeNode 
38f93 2a 70 49 6e 73 65 72 74 3b 0a 20 20 20 20 52 74  *pInsert;.    Rt
38f94 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20  reeCell cell;.  
38f95 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52    nodeGetCell(pR
38f96 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 69 2c  tree, pNode, ii,
38f97 20 26 63 65 6c 6c 29 3b 0a 0a 20 20 20 20 2f 2a   &cell);..    /*
38f98 20 46 69 6e 64 20 61 20 6e 6f 64 65 20 74 6f 20   Find a node to 
38f99 73 74 6f 72 65 20 74 68 69 73 20 63 65 6c 6c 20  store this cell 
38f9a 69 6e 2e 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  in. pNode->iNode
38f9b 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61   currently conta
38f9c 69 6e 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 68  ins.    ** the h
38f9d 65 69 67 68 74 20 6f 66 20 74 68 65 20 73 75 62  eight of the sub
38f9e 2d 74 72 65 65 20 68 65 61 64 65 64 20 62 79 20  -tree headed by 
38f9f 74 68 65 20 63 65 6c 6c 2e 0a 20 20 20 20 2a 2f  the cell..    */
38fa0 0a 20 20 20 20 72 63 20 3d 20 43 68 6f 6f 73 65  .    rc = Choose
38fa1 4c 65 61 66 28 70 52 74 72 65 65 2c 20 26 63 65  Leaf(pRtree, &ce
38fa2 6c 6c 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  ll, pNode->iNode
38fa3 2c 20 26 70 49 6e 73 65 72 74 29 3b 0a 20 20 20  , &pInsert);.   
38fa4 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
38fa5 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  OK ){.      int 
38fa6 72 63 32 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  rc2;.      rc = 
38fa7 72 74 72 65 65 49 6e 73 65 72 74 43 65 6c 6c 28  rtreeInsertCell(
38fa8 70 52 74 72 65 65 2c 20 70 49 6e 73 65 72 74 2c  pRtree, pInsert,
38fa9 20 26 63 65 6c 6c 2c 20 70 4e 6f 64 65 2d 3e 69   &cell, pNode->i
38faa 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 72 63 32  Node);.      rc2
38fab 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70   = nodeRelease(p
38fac 52 74 72 65 65 2c 20 70 49 6e 73 65 72 74 29 3b  Rtree, pInsert);
38fad 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
38fae 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
38faf 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20      rc = rc2;.  
38fb0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
38fb1 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
38fb2 2f 2a 0a 2a 2a 20 53 65 6c 65 63 74 20 61 20 63  /*.** Select a c
38fb3 75 72 72 65 6e 74 6c 79 20 75 6e 75 73 65 64 20  urrently unused 
38fb4 72 6f 77 69 64 20 66 6f 72 20 61 20 6e 65 77 20  rowid for a new 
38fb5 72 2d 74 72 65 65 20 72 65 63 6f 72 64 2e 0a 2a  r-tree record..*
38fb6 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 65 77  /.static int new
38fb7 52 6f 77 69 64 28 52 74 72 65 65 20 2a 70 52 74  Rowid(Rtree *pRt
38fb8 72 65 65 2c 20 69 36 34 20 2a 70 69 52 6f 77 69  ree, i64 *piRowi
38fb9 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  d){.  int rc;.  
38fba 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c  sqlite3_bind_nul
38fbb 6c 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  l(pRtree->pWrite
38fbc 52 6f 77 69 64 2c 20 31 29 3b 0a 20 20 73 71 6c  Rowid, 1);.  sql
38fbd 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70  ite3_bind_null(p
38fbe 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77  Rtree->pWriteRow
38fbf 69 64 2c 20 32 29 3b 0a 20 20 73 71 6c 69 74 65  id, 2);.  sqlite
38fc0 33 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70  3_step(pRtree->p
38fc1 57 72 69 74 65 52 6f 77 69 64 29 3b 0a 20 20 72  WriteRowid);.  r
38fc2 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65  c = sqlite3_rese
38fc3 74 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  t(pRtree->pWrite
38fc4 52 6f 77 69 64 29 3b 0a 20 20 2a 70 69 52 6f 77  Rowid);.  *piRow
38fc5 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61 73  id = sqlite3_las
38fc6 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 70  t_insert_rowid(p
38fc7 52 74 72 65 65 2d 3e 64 62 29 3b 0a 20 20 72 65  Rtree->db);.  re
38fc8 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e  turn rc;.}..#ifn
38fc9 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69  def NDEBUG.stati
38fca 63 20 69 6e 74 20 68 61 73 68 49 73 45 6d 70 74  c int hashIsEmpt
38fcb 79 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 29  y(Rtree *pRtree)
38fcc 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f  {.  int ii;.  fo
38fcd 72 28 69 69 3d 30 3b 20 69 69 3c 48 41 53 48 53  r(ii=0; ii<HASHS
38fce 49 5a 45 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  IZE; ii++){.    
38fcf 61 73 73 65 72 74 28 20 21 70 52 74 72 65 65 2d  assert( !pRtree-
38fd0 3e 61 48 61 73 68 5b 69 69 5d 20 29 3b 0a 20 20  >aHash[ii] );.  
38fd1 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  }.  return 1;.}.
38fd2 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
38fd3 65 20 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64  e xUpdate method
38fd4 20 66 6f 72 20 72 74 72 65 65 20 6d 6f 64 75 6c   for rtree modul
38fd5 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  e virtual tables
38fd6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
38fd7 72 74 72 65 65 55 70 64 61 74 65 28 0a 20 20 73  rtreeUpdate(.  s
38fd8 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
38fd9 61 62 2c 20 0a 20 20 69 6e 74 20 6e 44 61 74 61  ab, .  int nData
38fda 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  , .  sqlite3_val
38fdb 75 65 20 2a 2a 61 7a 44 61 74 61 2c 20 0a 20 20  ue **azData, .  
38fdc 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 52  sqlite_int64 *pR
38fdd 6f 77 69 64 0a 29 7b 0a 20 20 52 74 72 65 65 20  owid.){.  Rtree 
38fde 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65  *pRtree = (Rtree
38fdf 20 2a 29 70 56 74 61 62 3b 0a 20 20 69 6e 74 20   *)pVtab;.  int 
38fe0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
38fe1 0a 20 20 72 74 72 65 65 52 65 66 65 72 65 6e 63  .  rtreeReferenc
38fe2 65 28 70 52 74 72 65 65 29 3b 0a 0a 20 20 61 73  e(pRtree);..  as
38fe3 73 65 72 74 28 6e 44 61 74 61 3e 3d 31 29 3b 0a  sert(nData>=1);.
38fe4 20 20 61 73 73 65 72 74 28 68 61 73 68 49 73 45    assert(hashIsE
38fe5 6d 70 74 79 28 70 52 74 72 65 65 29 29 3b 0a 0a  mpty(pRtree));..
38fe6 20 20 2f 2a 20 49 66 20 61 7a 44 61 74 61 5b 30    /* If azData[0
38fe7 5d 20 69 73 20 6e 6f 74 20 61 6e 20 53 51 4c 20  ] is not an SQL 
38fe8 4e 55 4c 4c 20 76 61 6c 75 65 2c 20 69 74 20 69  NULL value, it i
38fe9 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 61  s the rowid of a
38fea 0a 20 20 2a 2a 20 72 65 63 6f 72 64 20 74 6f 20  .  ** record to 
38feb 64 65 6c 65 74 65 20 66 72 6f 6d 20 74 68 65 20  delete from the 
38fec 72 2d 74 72 65 65 20 74 61 62 6c 65 2e 20 54 68  r-tree table. Th
38fed 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63  e following bloc
38fee 6b 20 64 6f 65 73 0a 20 20 2a 2a 20 6a 75 73 74  k does.  ** just
38fef 20 74 68 61 74 2e 0a 20 20 2a 2f 0a 20 20 69 66   that..  */.  if
38ff0 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
38ff1 74 79 70 65 28 61 7a 44 61 74 61 5b 30 5d 29 21  type(azData[0])!
38ff2 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a  =SQLITE_NULL ){.
38ff3 20 20 20 20 69 36 34 20 69 44 65 6c 65 74 65 3b      i64 iDelete;
38ff4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38ff5 2f 2a 20 54 68 65 20 72 6f 77 69 64 20 74 6f 20  /* The rowid to 
38ff6 64 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 52 74  delete */.    Rt
38ff7 72 65 65 4e 6f 64 65 20 2a 70 4c 65 61 66 3b 20  reeNode *pLeaf; 
38ff8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 61            /* Lea
38ff9 66 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e 69 6e  f node containin
38ffa 67 20 72 65 63 6f 72 64 20 69 44 65 6c 65 74 65  g record iDelete
38ffb 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 65 6c   */.    int iCel
38ffc 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
38ffd 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
38ffe 69 44 65 6c 65 74 65 20 63 65 6c 6c 20 69 6e 20  iDelete cell in 
38fff 70 4c 65 61 66 20 2a 2f 0a 20 20 20 20 52 74 72  pLeaf */.    Rtr
39000 65 65 4e 6f 64 65 20 2a 70 52 6f 6f 74 3b 0a 0a  eeNode *pRoot;..
39001 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 61 20      /* Obtain a 
39002 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65  reference to the
39003 20 72 6f 6f 74 20 6e 6f 64 65 20 74 6f 20 69 6e   root node to in
39004 69 74 69 61 6c 69 73 65 20 52 74 72 65 65 2e 69  itialise Rtree.i
39005 44 65 70 74 68 20 2a 2f 0a 20 20 20 20 72 63 20  Depth */.    rc 
39006 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52  = nodeAcquire(pR
39007 74 72 65 65 2c 20 31 2c 20 30 2c 20 26 70 52 6f  tree, 1, 0, &pRo
39008 6f 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 4f 62 74  ot);..    /* Obt
39009 61 69 6e 20 61 20 72 65 66 65 72 65 6e 63 65 20  ain a reference 
3900a 74 6f 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65  to the leaf node
3900b 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
3900c 68 65 20 65 6e 74 72 79 20 0a 20 20 20 20 2a 2a  he entry .    **
3900d 20 61 62 6f 75 74 20 74 6f 20 62 65 20 64 65 6c   about to be del
3900e 65 74 65 64 2e 20 0a 20 20 20 20 2a 2f 0a 20 20  eted. .    */.  
3900f 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
39010 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 44 65  _OK ){.      iDe
39011 6c 65 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 76  lete = sqlite3_v
39012 61 6c 75 65 5f 69 6e 74 36 34 28 61 7a 44 61 74  alue_int64(azDat
39013 61 5b 30 5d 29 3b 0a 20 20 20 20 20 20 72 63 20  a[0]);.      rc 
39014 3d 20 66 69 6e 64 4c 65 61 66 4e 6f 64 65 28 70  = findLeafNode(p
39015 52 74 72 65 65 2c 20 69 44 65 6c 65 74 65 2c 20  Rtree, iDelete, 
39016 26 70 4c 65 61 66 29 3b 0a 20 20 20 20 7d 0a 0a  &pLeaf);.    }..
39017 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68      /* Delete th
39018 65 20 63 65 6c 6c 20 69 6e 20 71 75 65 73 74 69  e cell in questi
39019 6f 6e 20 66 72 6f 6d 20 74 68 65 20 6c 65 61 66  on from the leaf
3901a 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20 20 20 69 66   node. */.    if
3901b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
3901c 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32  ){.      int rc2
3901d 3b 0a 20 20 20 20 20 20 69 43 65 6c 6c 20 3d 20  ;.      iCell = 
3901e 6e 6f 64 65 52 6f 77 69 64 49 6e 64 65 78 28 70  nodeRowidIndex(p
3901f 52 74 72 65 65 2c 20 70 4c 65 61 66 2c 20 69 44  Rtree, pLeaf, iD
39020 65 6c 65 74 65 29 3b 0a 20 20 20 20 20 20 72 63  elete);.      rc
39021 20 3d 20 64 65 6c 65 74 65 43 65 6c 6c 28 70 52   = deleteCell(pR
39022 74 72 65 65 2c 20 70 4c 65 61 66 2c 20 69 43 65  tree, pLeaf, iCe
39023 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 63  ll, 0);.      rc
39024 32 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  2 = nodeRelease(
39025 70 52 74 72 65 65 2c 20 70 4c 65 61 66 29 3b 0a  pRtree, pLeaf);.
39026 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
39027 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
39028 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20     rc = rc2;.   
39029 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
3902a 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 63 6f  /* Delete the co
3902b 72 72 65 73 70 6f 6e 64 69 6e 67 20 65 6e 74 72  rresponding entr
3902c 79 20 69 6e 20 74 68 65 20 3c 72 74 72 65 65 3e  y in the <rtree>
3902d 5f 72 6f 77 69 64 20 74 61 62 6c 65 2e 20 2a 2f  _rowid table. */
3902e 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
3902f 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
39030 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
39031 36 34 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  64(pRtree->pDele
39032 74 65 52 6f 77 69 64 2c 20 31 2c 20 69 44 65 6c  teRowid, 1, iDel
39033 65 74 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ete);.      sqli
39034 74 65 33 5f 73 74 65 70 28 70 52 74 72 65 65 2d  te3_step(pRtree-
39035 3e 70 44 65 6c 65 74 65 52 6f 77 69 64 29 3b 0a  >pDeleteRowid);.
39036 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
39037 65 33 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d  e3_reset(pRtree-
39038 3e 70 44 65 6c 65 74 65 52 6f 77 69 64 29 3b 0a  >pDeleteRowid);.
39039 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68      }..    /* Ch
3903a 65 63 6b 20 69 66 20 74 68 65 20 72 6f 6f 74 20  eck if the root 
3903b 6e 6f 64 65 20 6e 6f 77 20 68 61 73 20 65 78 61  node now has exa
3903c 63 74 6c 79 20 6f 6e 65 20 63 68 69 6c 64 2e 20  ctly one child. 
3903d 49 66 20 73 6f 2c 20 72 65 6d 6f 76 65 0a 20 20  If so, remove.  
3903e 20 20 2a 2a 20 69 74 2c 20 73 63 68 65 64 75 6c    ** it, schedul
3903f 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  e the contents o
39040 66 20 74 68 65 20 63 68 69 6c 64 20 66 6f 72 20  f the child for 
39041 72 65 69 6e 73 65 72 74 69 6f 6e 20 61 6e 64 20  reinsertion and 
39042 0a 20 20 20 20 2a 2a 20 72 65 64 75 63 65 20 74  .    ** reduce t
39043 68 65 20 74 72 65 65 20 68 65 69 67 68 74 20 62  he tree height b
39044 79 20 6f 6e 65 2e 0a 20 20 20 20 2a 2a 0a 20 20  y one..    **.  
39045 20 20 2a 2a 20 54 68 69 73 20 69 73 20 65 71 75    ** This is equ
39046 69 76 61 6c 65 6e 74 20 74 6f 20 63 6f 70 79 69  ivalent to copyi
39047 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  ng the contents 
39048 6f 66 20 74 68 65 20 63 68 69 6c 64 20 69 6e 74  of the child int
39049 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 6f 6f  o.    ** the roo
3904a 74 20 6e 6f 64 65 20 28 74 68 65 20 6f 70 65 72  t node (the oper
3904b 61 74 69 6f 6e 20 74 68 61 74 20 47 75 74 6d 61  ation that Gutma
3904c 6e 27 73 20 70 61 70 65 72 20 73 61 79 73 20 74  n's paper says t
3904d 6f 20 70 65 72 66 6f 72 6d 20 0a 20 20 20 20 2a  o perform .    *
3904e 2a 20 69 6e 20 74 68 69 73 20 73 63 65 6e 61 72  * in this scenar
3904f 69 6f 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  io)..    */.    
39050 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
39051 4b 20 26 26 20 70 52 74 72 65 65 2d 3e 69 44 65  K && pRtree->iDe
39052 70 74 68 3e 30 20 29 7b 0a 20 20 20 20 20 20 69  pth>0 ){.      i
39053 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
39054 20 26 26 20 4e 43 45 4c 4c 28 70 52 6f 6f 74 29   && NCELL(pRoot)
39055 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 52  ==1 ){.        R
39056 74 72 65 65 4e 6f 64 65 20 2a 70 43 68 69 6c 64  treeNode *pChild
39057 3b 0a 20 20 20 20 20 20 20 20 69 36 34 20 69 43  ;.        i64 iC
39058 68 69 6c 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f  hild = nodeGetRo
39059 77 69 64 28 70 52 74 72 65 65 2c 20 70 52 6f 6f  wid(pRtree, pRoo
3905a 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 72  t, 0);.        r
3905b 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28  c = nodeAcquire(
3905c 70 52 74 72 65 65 2c 20 69 43 68 69 6c 64 2c 20  pRtree, iChild, 
3905d 70 52 6f 6f 74 2c 20 26 70 43 68 69 6c 64 29 3b  pRoot, &pChild);
3905e 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
3905f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
39060 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 6d          rc = rem
39061 6f 76 65 4e 6f 64 65 28 70 52 74 72 65 65 2c 20  oveNode(pRtree, 
39062 70 43 68 69 6c 64 2c 20 70 52 74 72 65 65 2d 3e  pChild, pRtree->
39063 69 44 65 70 74 68 2d 31 29 3b 0a 20 20 20 20 20  iDepth-1);.     
39064 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
39065 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
39066 7b 0a 20 20 20 20 20 20 20 20 20 20 70 52 74 72  {.          pRtr
39067 65 65 2d 3e 69 44 65 70 74 68 2d 2d 3b 0a 20 20  ee->iDepth--;.  
39068 20 20 20 20 20 20 20 20 77 72 69 74 65 49 6e 74          writeInt
39069 31 36 28 70 52 6f 6f 74 2d 3e 7a 44 61 74 61 2c  16(pRoot->zData,
3906a 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 29   pRtree->iDepth)
3906b 3b 0a 20 20 20 20 20 20 20 20 20 20 70 52 6f 6f  ;.          pRoo
3906c 74 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b 0a  t->isDirty = 1;.
3906d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
3906e 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
3906f 52 65 2d 69 6e 73 65 72 74 20 74 68 65 20 63 6f  Re-insert the co
39070 6e 74 65 6e 74 73 20 6f 66 20 61 6e 79 20 75 6e  ntents of any un
39071 64 65 72 66 75 6c 6c 20 6e 6f 64 65 73 20 72 65  derfull nodes re
39072 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 74  moved from the t
39073 72 65 65 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28  ree. */.    for(
39074 70 4c 65 61 66 3d 70 52 74 72 65 65 2d 3e 70 44  pLeaf=pRtree->pD
39075 65 6c 65 74 65 64 3b 20 70 4c 65 61 66 3b 20 70  eleted; pLeaf; p
39076 4c 65 61 66 3d 70 52 74 72 65 65 2d 3e 70 44 65  Leaf=pRtree->pDe
39077 6c 65 74 65 64 29 7b 0a 20 20 20 20 20 20 69 66  leted){.      if
39078 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
39079 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
3907a 72 65 69 6e 73 65 72 74 4e 6f 64 65 43 6f 6e 74  reinsertNodeCont
3907b 65 6e 74 28 70 52 74 72 65 65 2c 20 70 4c 65 61  ent(pRtree, pLea
3907c 66 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  f);.      }.    
3907d 20 20 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74    pRtree->pDelet
3907e 65 64 20 3d 20 70 4c 65 61 66 2d 3e 70 4e 65 78  ed = pLeaf->pNex
3907f 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  t;.      sqlite3
39080 5f 66 72 65 65 28 70 4c 65 61 66 29 3b 0a 20 20  _free(pLeaf);.  
39081 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65    }..    /* Rele
39082 61 73 65 20 74 68 65 20 72 65 66 65 72 65 6e 63  ase the referenc
39083 65 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e 6f  e to the root no
39084 64 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 72  de. */.    if( r
39085 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
39086 20 20 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 52        rc = nodeR
39087 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70  elease(pRtree, p
39088 52 6f 6f 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Root);.    }else
39089 7b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65 6c 65  {.      nodeRele
3908a 61 73 65 28 70 52 74 72 65 65 2c 20 70 52 6f 6f  ase(pRtree, pRoo
3908b 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  t);.    }.  }.. 
3908c 20 2f 2a 20 49 66 20 74 68 65 20 61 7a 44 61 74   /* If the azDat
3908d 61 5b 5d 20 61 72 72 61 79 20 63 6f 6e 74 61 69  a[] array contai
3908e 6e 73 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  ns more than one
3908f 20 65 6c 65 6d 65 6e 74 2c 20 65 6c 65 6d 65 6e   element, elemen
39090 74 73 0a 20 20 2a 2a 20 28 61 7a 44 61 74 61 5b  ts.  ** (azData[
39091 32 5d 2e 2e 61 7a 44 61 74 61 5b 61 72 67 63 2d  2]..azData[argc-
39092 31 5d 29 20 63 6f 6e 74 61 69 6e 20 61 20 6e 65  1]) contain a ne
39093 77 20 72 65 63 6f 72 64 20 74 6f 20 69 6e 73 65  w record to inse
39094 72 74 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65  rt into.  ** the
39095 20 72 2d 74 72 65 65 20 73 74 72 75 63 74 75 72   r-tree structur
39096 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63  e..  */.  if( rc
39097 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e  ==SQLITE_OK && n
39098 44 61 74 61 3e 31 20 29 7b 0a 20 20 20 20 2f 2a  Data>1 ){.    /*
39099 20 49 6e 73 65 72 74 20 61 20 6e 65 77 20 72 65   Insert a new re
3909a 63 6f 72 64 20 69 6e 74 6f 20 74 68 65 20 72 2d  cord into the r-
3909b 74 72 65 65 20 2a 2f 0a 20 20 20 20 52 74 72 65  tree */.    Rtre
3909c 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20 20  eCell cell;.    
3909d 69 6e 74 20 69 69 3b 0a 20 20 20 20 52 74 72 65  int ii;.    Rtre
3909e 65 4e 6f 64 65 20 2a 70 4c 65 61 66 3b 0a 0a 20  eNode *pLeaf;.. 
3909f 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74     /* Populate t
390a0 68 65 20 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 5d  he cell.aCoord[]
390a1 20 61 72 72 61 79 2e 20 54 68 65 20 66 69 72 73   array. The firs
390a2 74 20 63 6f 6f 72 64 69 6e 61 74 65 20 69 73 20  t coordinate is 
390a3 61 7a 44 61 74 61 5b 33 5d 2e 20 2a 2f 0a 20 20  azData[3]. */.  
390a4 20 20 61 73 73 65 72 74 28 20 6e 44 61 74 61 3d    assert( nData=
390a5 3d 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  =(pRtree->nDim*2
390a6 20 2b 20 33 29 20 29 3b 0a 20 20 20 20 69 66 28   + 3) );.    if(
390a7 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54   pRtree->eCoordT
390a8 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52 44  ype==RTREE_COORD
390a9 5f 52 45 41 4c 33 32 20 29 7b 0a 20 20 20 20 20  _REAL32 ){.     
390aa 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70   for(ii=0; ii<(p
390ab 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20  Rtree->nDim*2); 
390ac 69 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 20 20  ii+=2){.        
390ad 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e  cell.aCoord[ii].
390ae 66 20 3d 20 28 66 6c 6f 61 74 29 73 71 6c 69 74  f = (float)sqlit
390af 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28  e3_value_double(
390b0 61 7a 44 61 74 61 5b 69 69 2b 33 5d 29 3b 0a 20  azData[ii+3]);. 
390b1 20 20 20 20 20 20 20 63 65 6c 6c 2e 61 43 6f 6f         cell.aCoo
390b2 72 64 5b 69 69 2b 31 5d 2e 66 20 3d 20 28 66 6c  rd[ii+1].f = (fl
390b3 6f 61 74 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  oat)sqlite3_valu
390b4 65 5f 64 6f 75 62 6c 65 28 61 7a 44 61 74 61 5b  e_double(azData[
390b5 69 69 2b 34 5d 29 3b 0a 20 20 20 20 20 20 20 20  ii+4]);.        
390b6 69 66 28 20 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b  if( cell.aCoord[
390b7 69 69 5d 2e 66 3e 63 65 6c 6c 2e 61 43 6f 6f 72  ii].f>cell.aCoor
390b8 64 5b 69 69 2b 31 5d 2e 66 20 29 7b 0a 20 20 20  d[ii+1].f ){.   
390b9 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
390ba 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a 20  TE_CONSTRAINT;. 
390bb 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 63 6f           goto co
390bc 6e 73 74 72 61 69 6e 74 3b 0a 20 20 20 20 20 20  nstraint;.      
390bd 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
390be 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 6f 72  }else{.      for
390bf 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72 65  (ii=0; ii<(pRtre
390c0 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d  e->nDim*2); ii+=
390c1 32 29 7b 0a 20 20 20 20 20 20 20 20 63 65 6c 6c  2){.        cell
390c2 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e 69 20 3d 20  .aCoord[ii].i = 
390c3 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
390c4 74 28 61 7a 44 61 74 61 5b 69 69 2b 33 5d 29 3b  t(azData[ii+3]);
390c5 0a 20 20 20 20 20 20 20 20 63 65 6c 6c 2e 61 43  .        cell.aC
390c6 6f 6f 72 64 5b 69 69 2b 31 5d 2e 69 20 3d 20 73  oord[ii+1].i = s
390c7 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
390c8 28 61 7a 44 61 74 61 5b 69 69 2b 34 5d 29 3b 0a  (azData[ii+4]);.
390c9 20 20 20 20 20 20 20 20 69 66 28 20 63 65 6c 6c          if( cell
390ca 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e 69 3e 63 65  .aCoord[ii].i>ce
390cb 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e  ll.aCoord[ii+1].
390cc 69 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  i ){.          r
390cd 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54  c = SQLITE_CONST
390ce 52 41 49 4e 54 3b 0a 20 20 20 20 20 20 20 20 20  RAINT;.         
390cf 20 67 6f 74 6f 20 63 6f 6e 73 74 72 61 69 6e 74   goto constraint
390d0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
390d1 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
390d2 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74 68 65  * Figure out the
390d3 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 6e 65   rowid of the ne
390d4 77 20 72 6f 77 2e 20 2a 2f 0a 20 20 20 20 69 66  w row. */.    if
390d5 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
390d6 74 79 70 65 28 61 7a 44 61 74 61 5b 32 5d 29 3d  type(azData[2])=
390d7 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a  =SQLITE_NULL ){.
390d8 20 20 20 20 20 20 72 63 20 3d 20 6e 65 77 52 6f        rc = newRo
390d9 77 69 64 28 70 52 74 72 65 65 2c 20 26 63 65 6c  wid(pRtree, &cel
390da 6c 2e 69 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d  l.iRowid);.    }
390db 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 65 6c 6c  else{.      cell
390dc 2e 69 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65  .iRowid = sqlite
390dd 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 7a  3_value_int64(az
390de 44 61 74 61 5b 32 5d 29 3b 0a 20 20 20 20 20 20  Data[2]);.      
390df 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
390e0 36 34 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64  64(pRtree->pRead
390e1 52 6f 77 69 64 2c 20 31 2c 20 63 65 6c 6c 2e 69  Rowid, 1, cell.i
390e2 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 69 66  Rowid);.      if
390e3 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71  ( SQLITE_ROW==sq
390e4 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72 65  lite3_step(pRtre
390e5 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 20 29  e->pReadRowid) )
390e6 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
390e7 33 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e  3_reset(pRtree->
390e8 70 52 65 61 64 52 6f 77 69 64 29 3b 0a 20 20 20  pReadRowid);.   
390e9 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
390ea 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20  _CONSTRAINT;.   
390eb 20 20 20 20 20 67 6f 74 6f 20 63 6f 6e 73 74 72       goto constr
390ec 61 69 6e 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  aint;.      }.  
390ed 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
390ee 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70  _reset(pRtree->p
390ef 52 65 61 64 52 6f 77 69 64 29 3b 0a 20 20 20 20  ReadRowid);.    
390f0 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  }..    if( rc==S
390f1 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
390f2 20 20 72 63 20 3d 20 43 68 6f 6f 73 65 4c 65 61    rc = ChooseLea
390f3 66 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c  f(pRtree, &cell,
390f4 20 30 2c 20 26 70 4c 65 61 66 29 3b 0a 20 20 20   0, &pLeaf);.   
390f5 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53   }.    if( rc==S
390f6 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
390f7 20 20 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 20    int rc2;.     
390f8 20 70 52 74 72 65 65 2d 3e 69 52 65 69 6e 73 65   pRtree->iReinse
390f9 72 74 48 65 69 67 68 74 20 3d 20 2d 31 3b 0a 20  rtHeight = -1;. 
390fa 20 20 20 20 20 72 63 20 3d 20 72 74 72 65 65 49       rc = rtreeI
390fb 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65  nsertCell(pRtree
390fc 2c 20 70 4c 65 61 66 2c 20 26 63 65 6c 6c 2c 20  , pLeaf, &cell, 
390fd 30 29 3b 0a 20 20 20 20 20 20 72 63 32 20 3d 20  0);.      rc2 = 
390fe 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72  nodeRelease(pRtr
390ff 65 65 2c 20 70 4c 65 61 66 29 3b 0a 20 20 20 20  ee, pLeaf);.    
39100 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
39101 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
39102 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 20 20 7d  c = rc2;.      }
39103 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 63 6f 6e 73  .    }.  }..cons
39104 74 72 61 69 6e 74 3a 0a 20 20 72 74 72 65 65 52  traint:.  rtreeR
39105 65 6c 65 61 73 65 28 70 52 74 72 65 65 29 3b 0a  elease(pRtree);.
39106 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
39107 2f 2a 0a 2a 2a 20 54 68 65 20 78 52 65 6e 61 6d  /*.** The xRenam
39108 65 20 6d 65 74 68 6f 64 20 66 6f 72 20 72 74 72  e method for rtr
39109 65 65 20 6d 6f 64 75 6c 65 20 76 69 72 74 75 61  ee module virtua
3910a 6c 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a 73 74 61  l tables..*/.sta
3910b 74 69 63 20 69 6e 74 20 72 74 72 65 65 52 65 6e  tic int rtreeRen
3910c 61 6d 65 28 73 71 6c 69 74 65 33 5f 76 74 61 62  ame(sqlite3_vtab
3910d 20 2a 70 56 74 61 62 2c 20 63 6f 6e 73 74 20 63   *pVtab, const c
3910e 68 61 72 20 2a 7a 4e 65 77 4e 61 6d 65 29 7b 0a  har *zNewName){.
3910f 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 20    Rtree *pRtree 
39110 3d 20 28 52 74 72 65 65 20 2a 29 70 56 74 61 62  = (Rtree *)pVtab
39111 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
39112 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 63 68 61  ITE_NOMEM;.  cha
39113 72 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65  r *zSql = sqlite
39114 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20 20 20 22  3_mprintf(.    "
39115 41 4c 54 45 52 20 54 41 42 4c 45 20 25 51 2e 27  ALTER TABLE %Q.'
39116 25 71 5f 6e 6f 64 65 27 20 20 20 52 45 4e 41 4d  %q_node'   RENAM
39117 45 20 54 4f 20 5c 22 25 77 5f 6e 6f 64 65 5c 22  E TO \"%w_node\"
39118 3b 22 0a 20 20 20 20 22 41 4c 54 45 52 20 54 41  ;".    "ALTER TA
39119 42 4c 45 20 25 51 2e 27 25 71 5f 70 61 72 65 6e  BLE %Q.'%q_paren
3911a 74 27 20 52 45 4e 41 4d 45 20 54 4f 20 5c 22 25  t' RENAME TO \"%
3911b 77 5f 70 61 72 65 6e 74 5c 22 3b 22 0a 20 20 20  w_parent\";".   
3911c 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 25 51   "ALTER TABLE %Q
3911d 2e 27 25 71 5f 72 6f 77 69 64 27 20 20 52 45 4e  .'%q_rowid'  REN
3911e 41 4d 45 20 54 4f 20 5c 22 25 77 5f 72 6f 77 69  AME TO \"%w_rowi
3911f 64 5c 22 3b 22 0a 20 20 20 20 2c 20 70 52 74 72  d\";".    , pRtr
39120 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65 65 2d  ee->zDb, pRtree-
39121 3e 7a 4e 61 6d 65 2c 20 7a 4e 65 77 4e 61 6d 65  >zName, zNewName
39122 20 0a 20 20 20 20 2c 20 70 52 74 72 65 65 2d 3e   .    , pRtree->
39123 7a 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61  zDb, pRtree->zNa
39124 6d 65 2c 20 7a 4e 65 77 4e 61 6d 65 20 0a 20 20  me, zNewName .  
39125 20 20 2c 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c    , pRtree->zDb,
39126 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20   pRtree->zName, 
39127 7a 4e 65 77 4e 61 6d 65 0a 20 20 29 3b 0a 20 20  zNewName.  );.  
39128 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20  if( zSql ){.    
39129 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65  rc = sqlite3_exe
3912a 63 28 70 52 74 72 65 65 2d 3e 64 62 2c 20 7a 53  c(pRtree->db, zS
3912b 71 6c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  ql, 0, 0, 0);.  
3912c 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
3912d 53 71 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Sql);.  }.  retu
3912e 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63  rn rc;.}..static
3912f 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
39130 72 74 72 65 65 4d 6f 64 75 6c 65 20 3d 20 7b 0a  rtreeModule = {.
39131 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
39132 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
39133 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 72 74  iVersion */.  rt
39134 72 65 65 43 72 65 61 74 65 2c 20 20 20 20 20 20  reeCreate,      
39135 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 72            /* xCr
39136 65 61 74 65 20 2d 20 63 72 65 61 74 65 20 61 20  eate - create a 
39137 74 61 62 6c 65 20 2a 2f 0a 20 20 72 74 72 65 65  table */.  rtree
39138 43 6f 6e 6e 65 63 74 2c 20 20 20 20 20 20 20 20  Connect,        
39139 20 20 20 20 20 20 20 2f 2a 20 78 43 6f 6e 6e 65         /* xConne
3913a 63 74 20 2d 20 63 6f 6e 6e 65 63 74 20 74 6f 20  ct - connect to 
3913b 61 6e 20 65 78 69 73 74 69 6e 67 20 74 61 62 6c  an existing tabl
3913c 65 20 2a 2f 0a 20 20 72 74 72 65 65 42 65 73 74  e */.  rtreeBest
3913d 49 6e 64 65 78 2c 20 20 20 20 20 20 20 20 20 20  Index,          
3913e 20 20 20 2f 2a 20 78 42 65 73 74 49 6e 64 65 78     /* xBestIndex
3913f 20 2d 20 44 65 74 65 72 6d 69 6e 65 20 73 65 61   - Determine sea
39140 72 63 68 20 73 74 72 61 74 65 67 79 20 2a 2f 0a  rch strategy */.
39141 20 20 72 74 72 65 65 44 69 73 63 6f 6e 6e 65 63    rtreeDisconnec
39142 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t,            /*
39143 20 78 44 69 73 63 6f 6e 6e 65 63 74 20 2d 20 44   xDisconnect - D
39144 69 73 63 6f 6e 6e 65 63 74 20 66 72 6f 6d 20 61  isconnect from a
39145 20 74 61 62 6c 65 20 2a 2f 0a 20 20 72 74 72 65   table */.  rtre
39146 65 44 65 73 74 72 6f 79 2c 20 20 20 20 20 20 20  eDestroy,       
39147 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 73 74          /* xDest
39148 72 6f 79 20 2d 20 44 72 6f 70 20 61 20 74 61 62  roy - Drop a tab
39149 6c 65 20 2a 2f 0a 20 20 72 74 72 65 65 4f 70 65  le */.  rtreeOpe
3914a 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
3914b 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2d 20 6f      /* xOpen - o
3914c 70 65 6e 20 61 20 63 75 72 73 6f 72 20 2a 2f 0a  pen a cursor */.
3914d 20 20 72 74 72 65 65 43 6c 6f 73 65 2c 20 20 20    rtreeClose,   
3914e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3914f 20 78 43 6c 6f 73 65 20 2d 20 63 6c 6f 73 65 20   xClose - close 
39150 61 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 72 74  a cursor */.  rt
39151 72 65 65 46 69 6c 74 65 72 2c 20 20 20 20 20 20  reeFilter,      
39152 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69            /* xFi
39153 6c 74 65 72 20 2d 20 63 6f 6e 66 69 67 75 72 65  lter - configure
39154 20 73 63 61 6e 20 63 6f 6e 73 74 72 61 69 6e 74   scan constraint
39155 73 20 2a 2f 0a 20 20 72 74 72 65 65 4e 65 78 74  s */.  rtreeNext
39156 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
39157 20 20 20 2f 2a 20 78 4e 65 78 74 20 2d 20 61 64     /* xNext - ad
39158 76 61 6e 63 65 20 61 20 63 75 72 73 6f 72 20 2a  vance a cursor *
39159 2f 0a 20 20 72 74 72 65 65 45 6f 66 2c 20 20 20  /.  rtreeEof,   
3915a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3915b 2f 2a 20 78 45 6f 66 20 2a 2f 0a 20 20 72 74 72  /* xEof */.  rtr
3915c 65 65 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20  eeColumn,       
3915d 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6f 6c           /* xCol
3915e 75 6d 6e 20 2d 20 72 65 61 64 20 64 61 74 61 20  umn - read data 
3915f 2a 2f 0a 20 20 72 74 72 65 65 52 6f 77 69 64 2c  */.  rtreeRowid,
39160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39161 20 2f 2a 20 78 52 6f 77 69 64 20 2d 20 72 65 61   /* xRowid - rea
39162 64 20 64 61 74 61 20 2a 2f 0a 20 20 72 74 72 65  d data */.  rtre
39163 65 55 70 64 61 74 65 2c 20 20 20 20 20 20 20 20  eUpdate,        
39164 20 20 20 20 20 20 20 20 2f 2a 20 78 55 70 64 61          /* xUpda
39165 74 65 20 2d 20 77 72 69 74 65 20 64 61 74 61 20  te - write data 
39166 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20  */.  0,         
39167 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39168 20 2f 2a 20 78 42 65 67 69 6e 20 2d 20 62 65 67   /* xBegin - beg
39169 69 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a  in transaction *
3916a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  /.  0,          
3916b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3916c 2f 2a 20 78 53 79 6e 63 20 2d 20 73 79 6e 63 20  /* xSync - sync 
3916d 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20  transaction */. 
3916e 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
3916f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
39170 78 43 6f 6d 6d 69 74 20 2d 20 63 6f 6d 6d 69 74  xCommit - commit
39171 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a   transaction */.
39172 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
39173 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
39174 20 78 52 6f 6c 6c 62 61 63 6b 20 2d 20 72 6f 6c   xRollback - rol
39175 6c 62 61 63 6b 20 74 72 61 6e 73 61 63 74 69 6f  lback transactio
39176 6e 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20  n */.  0,       
39177 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39178 20 20 20 2f 2a 20 78 46 69 6e 64 46 75 6e 63 74     /* xFindFunct
39179 69 6f 6e 20 2d 20 66 75 6e 63 74 69 6f 6e 20 6f  ion - function o
3917a 76 65 72 6c 6f 61 64 69 6e 67 20 2a 2f 0a 20 20  verloading */.  
3917b 72 74 72 65 65 52 65 6e 61 6d 65 20 20 20 20 20  rtreeRename     
3917c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
3917d 52 65 6e 61 6d 65 20 2d 20 72 65 6e 61 6d 65 20  Rename - rename 
3917e 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a  the table */.};.
3917f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65  .static int rtre
39180 65 53 71 6c 49 6e 69 74 28 0a 20 20 52 74 72 65  eSqlInit(.  Rtre
39181 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20 73 71  e *pRtree, .  sq
39182 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 63 6f  lite3 *db, .  co
39183 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 2c 20 0a  nst char *zDb, .
39184 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50    const char *zP
39185 72 65 66 69 78 2c 20 0a 20 20 69 6e 74 20 69 73  refix, .  int is
39186 43 72 65 61 74 65 0a 29 7b 0a 20 20 69 6e 74 20  Create.){.  int 
39187 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
39188 0a 20 20 23 64 65 66 69 6e 65 20 4e 5f 53 54 41  .  #define N_STA
39189 54 45 4d 45 4e 54 20 39 0a 20 20 73 74 61 74 69  TEMENT 9.  stati
3918a 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a  c const char *az
3918b 53 71 6c 5b 4e 5f 53 54 41 54 45 4d 45 4e 54 5d  Sql[N_STATEMENT]
3918c 20 3d 20 7b 0a 20 20 20 20 2f 2a 20 52 65 61 64   = {.    /* Read
3918d 20 61 6e 64 20 77 72 69 74 65 20 74 68 65 20 78   and write the x
3918e 78 78 5f 6e 6f 64 65 20 74 61 62 6c 65 20 2a 2f  xx_node table */
3918f 0a 20 20 20 20 22 53 45 4c 45 43 54 20 64 61 74  .    "SELECT dat
39190 61 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f  a FROM '%q'.'%q_
39191 6e 6f 64 65 27 20 57 48 45 52 45 20 6e 6f 64 65  node' WHERE node
39192 6e 6f 20 3d 20 3a 31 22 2c 0a 20 20 20 20 22 49  no = :1",.    "I
39193 4e 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43 45  NSERT OR REPLACE
39194 20 49 4e 54 4f 20 27 25 71 27 2e 27 25 71 5f 6e   INTO '%q'.'%q_n
39195 6f 64 65 27 20 56 41 4c 55 45 53 28 3a 31 2c 20  ode' VALUES(:1, 
39196 3a 32 29 22 2c 0a 20 20 20 20 22 44 45 4c 45 54  :2)",.    "DELET
39197 45 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f  E FROM '%q'.'%q_
39198 6e 6f 64 65 27 20 57 48 45 52 45 20 6e 6f 64 65  node' WHERE node
39199 6e 6f 20 3d 20 3a 31 22 2c 0a 0a 20 20 20 20 2f  no = :1",..    /
3919a 2a 20 52 65 61 64 20 61 6e 64 20 77 72 69 74 65  * Read and write
3919b 20 74 68 65 20 78 78 78 5f 72 6f 77 69 64 20 74   the xxx_rowid t
3919c 61 62 6c 65 20 2a 2f 0a 20 20 20 20 22 53 45 4c  able */.    "SEL
3919d 45 43 54 20 6e 6f 64 65 6e 6f 20 46 52 4f 4d 20  ECT nodeno FROM 
3919e 27 25 71 27 2e 27 25 71 5f 72 6f 77 69 64 27 20  '%q'.'%q_rowid' 
3919f 57 48 45 52 45 20 72 6f 77 69 64 20 3d 20 3a 31  WHERE rowid = :1
391a0 22 2c 0a 20 20 20 20 22 49 4e 53 45 52 54 20 4f  ",.    "INSERT O
391a1 52 20 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 27  R REPLACE INTO '
391a2 25 71 27 2e 27 25 71 5f 72 6f 77 69 64 27 20 56  %q'.'%q_rowid' V
391a3 41 4c 55 45 53 28 3a 31 2c 20 3a 32 29 22 2c 0a  ALUES(:1, :2)",.
391a4 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d      "DELETE FROM
391a5 20 27 25 71 27 2e 27 25 71 5f 72 6f 77 69 64 27   '%q'.'%q_rowid'
391a6 20 57 48 45 52 45 20 72 6f 77 69 64 20 3d 20 3a   WHERE rowid = :
391a7 31 22 2c 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64  1",..    /* Read
391a8 20 61 6e 64 20 77 72 69 74 65 20 74 68 65 20 78   and write the x
391a9 78 78 5f 70 61 72 65 6e 74 20 74 61 62 6c 65 20  xx_parent table 
391aa 2a 2f 0a 20 20 20 20 22 53 45 4c 45 43 54 20 70  */.    "SELECT p
391ab 61 72 65 6e 74 6e 6f 64 65 20 46 52 4f 4d 20 27  arentnode FROM '
391ac 25 71 27 2e 27 25 71 5f 70 61 72 65 6e 74 27 20  %q'.'%q_parent' 
391ad 57 48 45 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a  WHERE nodeno = :
391ae 31 22 2c 0a 20 20 20 20 22 49 4e 53 45 52 54 20  1",.    "INSERT 
391af 4f 52 20 52 45 50 4c 41 43 45 20 49 4e 54 4f 20  OR REPLACE INTO 
391b0 27 25 71 27 2e 27 25 71 5f 70 61 72 65 6e 74 27  '%q'.'%q_parent'
391b1 20 56 41 4c 55 45 53 28 3a 31 2c 20 3a 32 29 22   VALUES(:1, :2)"
391b2 2c 0a 20 20 20 20 22 44 45 4c 45 54 45 20 46 52  ,.    "DELETE FR
391b3 4f 4d 20 27 25 71 27 2e 27 25 71 5f 70 61 72 65  OM '%q'.'%q_pare
391b4 6e 74 27 20 57 48 45 52 45 20 6e 6f 64 65 6e 6f  nt' WHERE nodeno
391b5 20 3d 20 3a 31 22 0a 20 20 7d 3b 0a 20 20 73 71   = :1".  };.  sq
391b6 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 61 70 70  lite3_stmt **app
391b7 53 74 6d 74 5b 4e 5f 53 54 41 54 45 4d 45 4e 54  Stmt[N_STATEMENT
391b8 5d 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 70  ];.  int i;..  p
391b9 52 74 72 65 65 2d 3e 64 62 20 3d 20 64 62 3b 0a  Rtree->db = db;.
391ba 0a 20 20 69 66 28 20 69 73 43 72 65 61 74 65 20  .  if( isCreate 
391bb 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 72  ){.    char *zCr
391bc 65 61 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 6d  eate = sqlite3_m
391bd 70 72 69 6e 74 66 28 0a 22 43 52 45 41 54 45 20  printf(."CREATE 
391be 54 41 42 4c 45 20 5c 22 25 77 5c 22 2e 5c 22 25  TABLE \"%w\".\"%
391bf 77 5f 6e 6f 64 65 5c 22 28 6e 6f 64 65 6e 6f 20  w_node\"(nodeno 
391c0 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
391c1 4b 45 59 2c 20 64 61 74 61 20 42 4c 4f 42 29 3b  KEY, data BLOB);
391c2 22 0a 22 43 52 45 41 54 45 20 54 41 42 4c 45 20  "."CREATE TABLE 
391c3 5c 22 25 77 5c 22 2e 5c 22 25 77 5f 72 6f 77 69  \"%w\".\"%w_rowi
391c4 64 5c 22 28 72 6f 77 69 64 20 49 4e 54 45 47 45  d\"(rowid INTEGE
391c5 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 6e  R PRIMARY KEY, n
391c6 6f 64 65 6e 6f 20 49 4e 54 45 47 45 52 29 3b 22  odeno INTEGER);"
391c7 0a 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 5c  ."CREATE TABLE \
391c8 22 25 77 5c 22 2e 5c 22 25 77 5f 70 61 72 65 6e  "%w\".\"%w_paren
391c9 74 5c 22 28 6e 6f 64 65 6e 6f 20 49 4e 54 45 47  t\"(nodeno INTEG
391ca 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20  ER PRIMARY KEY, 
391cb 70 61 72 65 6e 74 6e 6f 64 65 20 49 4e 54 45 47  parentnode INTEG
391cc 45 52 29 3b 22 0a 22 49 4e 53 45 52 54 20 49 4e  ER);"."INSERT IN
391cd 54 4f 20 27 25 71 27 2e 27 25 71 5f 6e 6f 64 65  TO '%q'.'%q_node
391ce 27 20 56 41 4c 55 45 53 28 31 2c 20 7a 65 72 6f  ' VALUES(1, zero
391cf 62 6c 6f 62 28 25 64 29 29 22 2c 0a 20 20 20 20  blob(%d))",.    
391d0 20 20 7a 44 62 2c 20 7a 50 72 65 66 69 78 2c 20    zDb, zPrefix, 
391d1 7a 44 62 2c 20 7a 50 72 65 66 69 78 2c 20 7a 44  zDb, zPrefix, zD
391d2 62 2c 20 7a 50 72 65 66 69 78 2c 20 7a 44 62 2c  b, zPrefix, zDb,
391d3 20 7a 50 72 65 66 69 78 2c 20 70 52 74 72 65 65   zPrefix, pRtree
391d4 2d 3e 69 4e 6f 64 65 53 69 7a 65 0a 20 20 20 20  ->iNodeSize.    
391d5 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 43 72 65  );.    if( !zCre
391d6 61 74 65 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ate ){.      ret
391d7 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
391d8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
391d9 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62   sqlite3_exec(db
391da 2c 20 7a 43 72 65 61 74 65 2c 20 30 2c 20 30 2c  , zCreate, 0, 0,
391db 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
391dc 5f 66 72 65 65 28 7a 43 72 65 61 74 65 29 3b 0a  _free(zCreate);.
391dd 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
391de 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
391df 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
391e0 20 20 7d 0a 0a 20 20 61 70 70 53 74 6d 74 5b 30    }..  appStmt[0
391e1 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 52 65  ] = &pRtree->pRe
391e2 61 64 4e 6f 64 65 3b 0a 20 20 61 70 70 53 74 6d  adNode;.  appStm
391e3 74 5b 31 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e  t[1] = &pRtree->
391e4 70 57 72 69 74 65 4e 6f 64 65 3b 0a 20 20 61 70  pWriteNode;.  ap
391e5 70 53 74 6d 74 5b 32 5d 20 3d 20 26 70 52 74 72  pStmt[2] = &pRtr
391e6 65 65 2d 3e 70 44 65 6c 65 74 65 4e 6f 64 65 3b  ee->pDeleteNode;
391e7 0a 20 20 61 70 70 53 74 6d 74 5b 33 5d 20 3d 20  .  appStmt[3] = 
391e8 26 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f  &pRtree->pReadRo
391e9 77 69 64 3b 0a 20 20 61 70 70 53 74 6d 74 5b 34  wid;.  appStmt[4
391ea 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 57 72  ] = &pRtree->pWr
391eb 69 74 65 52 6f 77 69 64 3b 0a 20 20 61 70 70 53  iteRowid;.  appS
391ec 74 6d 74 5b 35 5d 20 3d 20 26 70 52 74 72 65 65  tmt[5] = &pRtree
391ed 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69 64 3b 0a  ->pDeleteRowid;.
391ee 20 20 61 70 70 53 74 6d 74 5b 36 5d 20 3d 20 26    appStmt[6] = &
391ef 70 52 74 72 65 65 2d 3e 70 52 65 61 64 50 61 72  pRtree->pReadPar
391f0 65 6e 74 3b 0a 20 20 61 70 70 53 74 6d 74 5b 37  ent;.  appStmt[7
391f1 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 57 72  ] = &pRtree->pWr
391f2 69 74 65 50 61 72 65 6e 74 3b 0a 20 20 61 70 70  iteParent;.  app
391f3 53 74 6d 74 5b 38 5d 20 3d 20 26 70 52 74 72 65  Stmt[8] = &pRtre
391f4 65 2d 3e 70 44 65 6c 65 74 65 50 61 72 65 6e 74  e->pDeleteParent
391f5 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  ;..  for(i=0; i<
391f6 4e 5f 53 54 41 54 45 4d 45 4e 54 20 26 26 20 72  N_STATEMENT && r
391f7 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 2b  c==SQLITE_OK; i+
391f8 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53  +){.    char *zS
391f9 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  ql = sqlite3_mpr
391fa 69 6e 74 66 28 61 7a 53 71 6c 5b 69 5d 2c 20 7a  intf(azSql[i], z
391fb 44 62 2c 20 7a 50 72 65 66 69 78 29 3b 0a 20 20  Db, zPrefix);.  
391fc 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20    if( zSql ){.  
391fd 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
391fe 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c 20  _prepare_v2(db, 
391ff 7a 53 71 6c 2c 20 2d 31 2c 20 61 70 70 53 74 6d  zSql, -1, appStm
39200 74 5b 69 5d 2c 20 30 29 3b 20 0a 20 20 20 20 7d  t[i], 0); .    }
39201 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
39202 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
39203 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
39204 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20 7d  _free(zSql);.  }
39205 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
39206 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
39207 74 69 6e 65 20 71 75 65 72 69 65 73 20 64 61 74  tine queries dat
39208 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62 20  abase handle db 
39209 66 6f 72 20 74 68 65 20 70 61 67 65 2d 73 69 7a  for the page-siz
3920a 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 64 61 74  e used by.** dat
3920b 61 62 61 73 65 20 7a 44 62 2e 20 49 66 20 73 75  abase zDb. If su
3920c 63 63 65 73 73 66 75 6c 2c 20 74 68 65 20 70 61  ccessful, the pa
3920d 67 65 2d 73 69 7a 65 20 69 6e 20 62 79 74 65 73  ge-size in bytes
3920e 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a   is written to.*
3920f 2a 20 2a 70 69 50 61 67 65 53 69 7a 65 20 61 6e  * *piPageSize an
39210 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75  d SQLITE_OK retu
39211 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c  rned. Otherwise,
39212 20 61 6e 64 20 61 6e 20 53 51 4c 69 74 65 20 65   and an SQLite e
39213 72 72 6f 72 20 0a 2a 2a 20 63 6f 64 65 20 69 73  rror .** code is
39214 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74   returned..*/.st
39215 61 74 69 63 20 69 6e 74 20 67 65 74 50 61 67 65  atic int getPage
39216 53 69 7a 65 28 73 71 6c 69 74 65 33 20 2a 64 62  Size(sqlite3 *db
39217 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  , const char *zD
39218 62 2c 20 69 6e 74 20 2a 70 69 50 61 67 65 53 69  b, int *piPageSi
39219 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ze){.  int rc = 
3921a 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
3921b 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 73 71  char *zSql;.  sq
3921c 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
3921d 74 20 3d 20 30 3b 0a 0a 20 20 7a 53 71 6c 20 3d  t = 0;..  zSql =
3921e 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
3921f 28 22 50 52 41 47 4d 41 20 25 51 2e 70 61 67 65  ("PRAGMA %Q.page
39220 5f 73 69 7a 65 22 2c 20 7a 44 62 29 3b 0a 20 20  _size", zDb);.  
39221 69 66 28 20 21 7a 53 71 6c 20 29 7b 0a 20 20 20  if( !zSql ){.   
39222 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
39223 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 72 63 20  OMEM;.  }..  rc 
39224 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  = sqlite3_prepar
39225 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d  e_v2(db, zSql, -
39226 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20  1, &pStmt, 0);. 
39227 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53   sqlite3_free(zS
39228 71 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  ql);.  if( rc!=S
39229 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
3922a 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
3922b 20 20 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57    if( SQLITE_ROW
3922c 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70  ==sqlite3_step(p
3922d 53 74 6d 74 29 20 29 7b 0a 20 20 20 20 2a 70 69  Stmt) ){.    *pi
3922e 50 61 67 65 53 69 7a 65 20 3d 20 73 71 6c 69 74  PageSize = sqlit
3922f 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 70 53  e3_column_int(pS
39230 74 6d 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72  tmt, 0);.  }.  r
39231 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 66 69  eturn sqlite3_fi
39232 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d  nalize(pStmt);.}
39233 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 66 75  ../* .** This fu
39234 6e 63 74 69 6f 6e 20 69 73 20 74 68 65 20 69 6d  nction is the im
39235 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
39236 62 6f 74 68 20 74 68 65 20 78 43 6f 6e 6e 65 63  both the xConnec
39237 74 20 61 6e 64 20 78 43 72 65 61 74 65 0a 2a 2a  t and xCreate.**
39238 20 6d 65 74 68 6f 64 73 20 6f 66 20 74 68 65 20   methods of the 
39239 72 2d 74 72 65 65 20 76 69 72 74 75 61 6c 20 74  r-tree virtual t
3923a 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 61 72  able..**.**   ar
3923b 67 76 5b 30 5d 20 20 20 2d 3e 20 6d 6f 64 75 6c  gv[0]   -> modul
3923c 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 61 72 67 76  e name.**   argv
3923d 5b 31 5d 20 20 20 2d 3e 20 64 61 74 61 62 61 73  [1]   -> databas
3923e 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 61 72 67 76  e name.**   argv
3923f 5b 32 5d 20 20 20 2d 3e 20 74 61 62 6c 65 20 6e  [2]   -> table n
39240 61 6d 65 0a 2a 2a 20 20 20 61 72 67 76 5b 2e 2e  ame.**   argv[..
39241 2e 5d 20 2d 3e 20 63 6f 6c 75 6d 6e 20 6e 61 6d  .] -> column nam
39242 65 73 2e 2e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  es....*/.static 
39243 69 6e 74 20 72 74 72 65 65 49 6e 69 74 28 0a 20  int rtreeInit(. 
39244 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
39245 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39246 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
39247 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
39248 20 76 6f 69 64 20 2a 70 41 75 78 2c 20 20 20 20   void *pAux,    
39249 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3924a 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74       /* One of t
3924b 68 65 20 52 54 52 45 45 5f 43 4f 4f 52 44 5f 2a  he RTREE_COORD_*
3924c 20 63 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a 20 20   constants */.  
3924d 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20  int argc, const 
3924e 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76  char *const*argv
3924f 2c 20 20 20 2f 2a 20 50 61 72 61 6d 65 74 65 72  ,   /* Parameter
39250 73 20 74 6f 20 43 52 45 41 54 45 20 54 41 42 4c  s to CREATE TABL
39251 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  E statement */. 
39252 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a   sqlite3_vtab **
39253 70 70 56 74 61 62 2c 20 20 20 20 20 20 20 20 20  ppVtab,         
39254 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 65 77       /* OUT: New
39255 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a   virtual table *
39256 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72  /.  char **pzErr
39257 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
39258 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
39259 45 72 72 6f 72 20 6d 65 73 73 61 67 65 2c 20 69  Error message, i
3925a 66 20 61 6e 79 20 2a 2f 0a 20 20 69 6e 74 20 69  f any */.  int i
3925b 73 43 72 65 61 74 65 20 20 20 20 20 20 20 20 20  sCreate         
3925c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3925d 2a 20 54 72 75 65 20 66 6f 72 20 78 43 72 65 61  * True for xCrea
3925e 74 65 2c 20 66 61 6c 73 65 20 66 6f 72 20 78 43  te, false for xC
3925f 6f 6e 6e 65 63 74 20 2a 2f 0a 29 7b 0a 20 20 69  onnect */.){.  i
39260 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
39261 4b 3b 0a 20 20 69 6e 74 20 69 50 61 67 65 53 69  K;.  int iPageSi
39262 7a 65 20 3d 20 30 3b 0a 20 20 52 74 72 65 65 20  ze = 0;.  Rtree 
39263 2a 70 52 74 72 65 65 3b 0a 20 20 69 6e 74 20 6e  *pRtree;.  int n
39264 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
39265 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 74   /* Length of st
39266 72 69 6e 67 20 61 72 67 76 5b 31 5d 20 2a 2f 0a  ring argv[1] */.
39267 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 20 20 20 20    int nName;    
39268 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
39269 68 20 6f 66 20 73 74 72 69 6e 67 20 61 72 67 76  h of string argv
3926a 5b 32 5d 20 2a 2f 0a 20 20 69 6e 74 20 65 43 6f  [2] */.  int eCo
3926b 6f 72 64 54 79 70 65 20 3d 20 28 69 6e 74 29 70  ordType = (int)p
3926c 41 75 78 3b 0a 0a 20 20 63 6f 6e 73 74 20 63 68  Aux;..  const ch
3926d 61 72 20 2a 61 45 72 72 4d 73 67 5b 5d 20 3d 20  ar *aErrMsg[] = 
3926e 7b 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  {.    0,        
3926f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39271 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30              /* 0
39272 20 2a 2f 0a 20 20 20 20 22 57 72 6f 6e 67 20 6e   */.    "Wrong n
39273 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
39274 20 66 6f 72 20 61 6e 20 72 74 72 65 65 20 74 61   for an rtree ta
39275 62 6c 65 22 2c 20 20 20 20 20 20 20 20 20 2f 2a  ble",         /*
39276 20 31 20 2a 2f 0a 20 20 20 20 22 54 6f 6f 20 66   1 */.    "Too f
39277 65 77 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 61  ew columns for a
39278 6e 20 72 74 72 65 65 20 74 61 62 6c 65 22 2c 20  n rtree table", 
39279 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3927a 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 22 54 6f 6f  /* 2 */.    "Too
3927b 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 66 6f   many columns fo
3927c 72 20 61 6e 20 72 74 72 65 65 20 74 61 62 6c 65  r an rtree table
3927d 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  "               
3927e 20 20 2f 2a 20 33 20 2a 2f 0a 20 20 7d 3b 0a 0a    /* 3 */.  };..
3927f 20 20 69 6e 74 20 69 45 72 72 20 3d 20 28 61 72    int iErr = (ar
39280 67 63 3c 36 29 20 3f 20 32 20 3a 20 61 72 67 63  gc<6) ? 2 : argc
39281 3e 28 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45  >(RTREE_MAX_DIME
39282 4e 53 49 4f 4e 53 2a 32 2b 34 29 20 3f 20 33 20  NSIONS*2+4) ? 3 
39283 3a 20 61 72 67 63 25 32 3b 0a 20 20 69 66 28 20  : argc%2;.  if( 
39284 61 45 72 72 4d 73 67 5b 69 45 72 72 5d 20 29 7b  aErrMsg[iErr] ){
39285 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71  .    *pzErr = sq
39286 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25  lite3_mprintf("%
39287 73 22 2c 20 61 45 72 72 4d 73 67 5b 69 45 72 72  s", aErrMsg[iErr
39288 5d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  ]);.    return S
39289 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
3928a 0a 0a 20 20 72 63 20 3d 20 67 65 74 50 61 67 65  ..  rc = getPage
3928b 53 69 7a 65 28 64 62 2c 20 61 72 67 76 5b 31 5d  Size(db, argv[1]
3928c 2c 20 26 69 50 61 67 65 53 69 7a 65 29 3b 0a 20  , &iPageSize);. 
3928d 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
3928e 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
3928f 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41   rc;.  }..  /* A
39290 6c 6c 6f 63 61 74 65 20 74 68 65 20 73 71 6c 69  llocate the sqli
39291 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75  te3_vtab structu
39292 72 65 20 2a 2f 0a 20 20 6e 44 62 20 3d 20 73 74  re */.  nDb = st
39293 72 6c 65 6e 28 61 72 67 76 5b 31 5d 29 3b 0a 20  rlen(argv[1]);. 
39294 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28   nName = strlen(
39295 61 72 67 76 5b 32 5d 29 3b 0a 20 20 70 52 74 72  argv[2]);.  pRtr
39296 65 65 20 3d 20 28 52 74 72 65 65 20 2a 29 73 71  ee = (Rtree *)sq
39297 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
39298 65 6f 66 28 52 74 72 65 65 29 2b 6e 44 62 2b 6e  eof(Rtree)+nDb+n
39299 4e 61 6d 65 2b 32 29 3b 0a 20 20 69 66 28 20 21  Name+2);.  if( !
3929a 70 52 74 72 65 65 20 29 7b 0a 20 20 20 20 72 65  pRtree ){.    re
3929b 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
3929c 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28  M;.  }.  memset(
3929d 70 52 74 72 65 65 2c 20 30 2c 20 73 69 7a 65 6f  pRtree, 0, sizeo
3929e 66 28 52 74 72 65 65 29 2b 6e 44 62 2b 6e 4e 61  f(Rtree)+nDb+nNa
3929f 6d 65 2b 32 29 3b 0a 20 20 70 52 74 72 65 65 2d  me+2);.  pRtree-
392a0 3e 6e 42 75 73 79 20 3d 20 31 3b 0a 20 20 70 52  >nBusy = 1;.  pR
392a1 74 72 65 65 2d 3e 62 61 73 65 2e 70 4d 6f 64 75  tree->base.pModu
392a2 6c 65 20 3d 20 26 72 74 72 65 65 4d 6f 64 75 6c  le = &rtreeModul
392a3 65 3b 0a 20 20 70 52 74 72 65 65 2d 3e 7a 44 62  e;.  pRtree->zDb
392a4 20 3d 20 28 63 68 61 72 20 2a 29 26 70 52 74 72   = (char *)&pRtr
392a5 65 65 5b 31 5d 3b 0a 20 20 70 52 74 72 65 65 2d  ee[1];.  pRtree-
392a6 3e 7a 4e 61 6d 65 20 3d 20 26 70 52 74 72 65 65  >zName = &pRtree
392a7 2d 3e 7a 44 62 5b 6e 44 62 2b 31 5d 3b 0a 20 20  ->zDb[nDb+1];.  
392a8 70 52 74 72 65 65 2d 3e 6e 44 69 6d 20 3d 20 28  pRtree->nDim = (
392a9 61 72 67 63 2d 34 29 2f 32 3b 0a 20 20 70 52 74  argc-4)/2;.  pRt
392aa 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65  ree->nBytesPerCe
392ab 6c 6c 20 3d 20 38 20 2b 20 70 52 74 72 65 65 2d  ll = 8 + pRtree-
392ac 3e 6e 44 69 6d 2a 34 2a 32 3b 0a 20 20 70 52 74  >nDim*4*2;.  pRt
392ad 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65 20  ree->eCoordType 
392ae 3d 20 65 43 6f 6f 72 64 54 79 70 65 3b 0a 20 20  = eCoordType;.  
392af 6d 65 6d 63 70 79 28 70 52 74 72 65 65 2d 3e 7a  memcpy(pRtree->z
392b0 44 62 2c 20 61 72 67 76 5b 31 5d 2c 20 6e 44 62  Db, argv[1], nDb
392b1 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 52 74 72  );.  memcpy(pRtr
392b2 65 65 2d 3e 7a 4e 61 6d 65 2c 20 61 72 67 76 5b  ee->zName, argv[
392b3 32 5d 2c 20 6e 4e 61 6d 65 29 3b 0a 0a 20 20 2f  2], nName);..  /
392b4 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74 68 65  * Figure out the
392b5 20 6e 6f 64 65 20 73 69 7a 65 20 74 6f 20 75 73   node size to us
392b6 65 2e 20 42 79 20 64 65 66 61 75 6c 74 2c 20 75  e. By default, u
392b7 73 65 20 36 34 20 62 79 74 65 73 20 6c 65 73 73  se 64 bytes less
392b8 20 74 68 61 6e 0a 20 20 2a 2a 20 74 68 65 20 64   than.  ** the d
392b9 61 74 61 62 61 73 65 20 70 61 67 65 2d 73 69 7a  atabase page-siz
392ba 65 2e 20 54 68 69 73 20 65 6e 73 75 72 65 73 20  e. This ensures 
392bb 74 68 61 74 20 65 61 63 68 20 6e 6f 64 65 20 69  that each node i
392bc 73 20 73 74 6f 72 65 64 20 6f 6e 0a 20 20 2a 2a  s stored on.  **
392bd 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61   a single databa
392be 73 65 20 70 61 67 65 2e 0a 20 20 2a 2a 0a 20 20  se page..  **.  
392bf 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61  ** If the databa
392c0 73 64 20 70 61 67 65 2d 73 69 7a 65 20 69 73 20  sd page-size is 
392c1 73 6f 20 6c 61 72 67 65 20 74 68 61 74 20 6d 6f  so large that mo
392c2 72 65 20 74 68 61 6e 20 52 54 52 45 45 5f 4d 41  re than RTREE_MA
392c3 58 43 45 4c 4c 53 0a 20 20 2a 2a 20 65 6e 74 72  XCELLS.  ** entr
392c4 69 65 73 20 77 6f 75 6c 64 20 66 69 74 20 69 6e  ies would fit in
392c5 20 61 20 73 69 6e 67 6c 65 20 6e 6f 64 65 2c 20   a single node, 
392c6 75 73 65 20 61 20 73 6d 61 6c 6c 65 72 20 6e 6f  use a smaller no
392c7 64 65 2d 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20  de-size..  */.  
392c8 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a  pRtree->iNodeSiz
392c9 65 20 3d 20 69 50 61 67 65 53 69 7a 65 2d 36 34  e = iPageSize-64
392ca 3b 0a 20 20 69 66 28 20 28 34 2b 70 52 74 72 65  ;.  if( (4+pRtre
392cb 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c  e->nBytesPerCell
392cc 2a 52 54 52 45 45 5f 4d 41 58 43 45 4c 4c 53 29  *RTREE_MAXCELLS)
392cd 3c 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69  <pRtree->iNodeSi
392ce 7a 65 20 29 7b 0a 20 20 20 20 70 52 74 72 65 65  ze ){.    pRtree
392cf 2d 3e 69 4e 6f 64 65 53 69 7a 65 20 3d 20 34 2b  ->iNodeSize = 4+
392d0 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65  pRtree->nBytesPe
392d1 72 43 65 6c 6c 2a 52 54 52 45 45 5f 4d 41 58 43  rCell*RTREE_MAXC
392d2 45 4c 4c 53 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ELLS;.  }..  /* 
392d3 43 72 65 61 74 65 2f 43 6f 6e 6e 65 63 74 20 74  Create/Connect t
392d4 6f 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  o the underlying
392d5 20 72 65 6c 61 74 69 6f 6e 61 6c 20 64 61 74 61   relational data
392d6 62 61 73 65 20 73 63 68 65 6d 61 2e 20 49 66 0a  base schema. If.
392d7 20 20 2a 2a 20 74 68 61 74 20 69 73 20 73 75 63    ** that is suc
392d8 63 65 73 73 66 75 6c 2c 20 63 61 6c 6c 20 73 71  cessful, call sq
392d9 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74  lite3_declare_vt
392da 61 62 28 29 20 74 6f 20 63 6f 6e 66 69 67 75 72  ab() to configur
392db 65 0a 20 20 2a 2a 20 74 68 65 20 72 2d 74 72 65  e.  ** the r-tre
392dc 65 20 74 61 62 6c 65 20 73 63 68 65 6d 61 2e 0a  e table schema..
392dd 20 20 2a 2f 0a 20 20 69 66 28 20 28 72 63 20 3d    */.  if( (rc =
392de 20 72 74 72 65 65 53 71 6c 49 6e 69 74 28 70 52   rtreeSqlInit(pR
392df 74 72 65 65 2c 20 64 62 2c 20 61 72 67 76 5b 31  tree, db, argv[1
392e0 5d 2c 20 61 72 67 76 5b 32 5d 2c 20 69 73 43 72  ], argv[2], isCr
392e1 65 61 74 65 29 29 20 29 7b 0a 20 20 20 20 2a 70  eate)) ){.    *p
392e2 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zErr = sqlite3_m
392e3 70 72 69 6e 74 66 28 22 25 73 22 2c 20 73 71 6c  printf("%s", sql
392e4 69 74 65 33 5f 65 72 72 6d 73 67 28 64 62 29 29  ite3_errmsg(db))
392e5 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63  ;.  }else{.    c
392e6 68 61 72 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69  har *zSql = sqli
392e7 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 43 52 45  te3_mprintf("CRE
392e8 41 54 45 20 54 41 42 4c 45 20 78 28 25 73 22 2c  ATE TABLE x(%s",
392e9 20 61 72 67 76 5b 33 5d 29 3b 0a 20 20 20 20 63   argv[3]);.    c
392ea 68 61 72 20 2a 7a 54 6d 70 3b 0a 20 20 20 20 69  har *zTmp;.    i
392eb 6e 74 20 69 69 3b 0a 20 20 20 20 66 6f 72 28 69  nt ii;.    for(i
392ec 69 3d 34 3b 20 7a 53 71 6c 20 26 26 20 69 69 3c  i=4; zSql && ii<
392ed 61 72 67 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  argc; ii++){.   
392ee 20 20 20 7a 54 6d 70 20 3d 20 7a 53 71 6c 3b 0a     zTmp = zSql;.
392ef 20 20 20 20 20 20 7a 53 71 6c 20 3d 20 73 71 6c        zSql = sql
392f0 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73  ite3_mprintf("%s
392f1 2c 20 25 73 22 2c 20 7a 54 6d 70 2c 20 61 72 67  , %s", zTmp, arg
392f2 76 5b 69 69 5d 29 3b 0a 20 20 20 20 20 20 73 71  v[ii]);.      sq
392f3 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 6d 70 29  lite3_free(zTmp)
392f4 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
392f5 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20 20 7a 54  zSql ){.      zT
392f6 6d 70 20 3d 20 7a 53 71 6c 3b 0a 20 20 20 20 20  mp = zSql;.     
392f7 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f   zSql = sqlite3_
392f8 6d 70 72 69 6e 74 66 28 22 25 73 29 3b 22 2c 20  mprintf("%s);", 
392f9 7a 54 6d 70 29 3b 0a 20 20 20 20 20 20 73 71 6c  zTmp);.      sql
392fa 69 74 65 33 5f 66 72 65 65 28 7a 54 6d 70 29 3b  ite3_free(zTmp);
392fb 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21  .    }.    if( !
392fc 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20 20 72 63  zSql ){.      rc
392fd 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
392fe 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 53  .    }else if( S
392ff 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
39300 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f  sqlite3_declare_
39301 76 74 61 62 28 64 62 2c 20 7a 53 71 6c 29 29 20  vtab(db, zSql)) 
39302 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 20  ){.      *pzErr 
39303 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
39304 66 28 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f  f("%s", sqlite3_
39305 65 72 72 6d 73 67 28 64 62 29 29 3b 0a 20 20 20  errmsg(db));.   
39306 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66   }.    sqlite3_f
39307 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20 7d 0a 0a  ree(zSql);.  }..
39308 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
39309 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 70 56 74  _OK ){.    *ppVt
3930a 61 62 20 3d 20 28 73 71 6c 69 74 65 33 5f 76 74  ab = (sqlite3_vt
3930b 61 62 20 2a 29 70 52 74 72 65 65 3b 0a 20 20 7d  ab *)pRtree;.  }
3930c 65 6c 73 65 7b 0a 20 20 20 20 72 74 72 65 65 52  else{.    rtreeR
3930d 65 6c 65 61 73 65 28 70 52 74 72 65 65 29 3b 0a  elease(pRtree);.
3930e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
3930f 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  .}.../*.** Imple
39310 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 73  mentation of a s
39311 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 74  calar function t
39312 68 61 74 20 64 65 63 6f 64 65 73 20 72 2d 74 72  hat decodes r-tr
39313 65 65 20 6e 6f 64 65 73 20 74 6f 0a 2a 2a 20 68  ee nodes to.** h
39314 75 6d 61 6e 20 72 65 61 64 61 62 6c 65 20 73 74  uman readable st
39315 72 69 6e 67 73 2e 20 54 68 69 73 20 63 61 6e 20  rings. This can 
39316 62 65 20 75 73 65 64 20 66 6f 72 20 64 65 62 75  be used for debu
39317 67 67 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73  gging and analys
39318 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 63  is..**.** The sc
39319 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 74 61  alar function ta
3931a 6b 65 73 20 74 77 6f 20 61 72 67 75 6d 65 6e 74  kes two argument
3931b 73 2c 20 61 20 62 6c 6f 62 20 6f 66 20 64 61 74  s, a blob of dat
3931c 61 20 63 6f 6e 74 61 69 6e 69 6e 67 0a 2a 2a 20  a containing.** 
3931d 61 6e 20 72 2d 74 72 65 65 20 6e 6f 64 65 2c 20  an r-tree node, 
3931e 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  and the number o
3931f 66 20 64 69 6d 65 6e 73 69 6f 6e 73 20 74 68 65  f dimensions the
39320 20 72 2d 74 72 65 65 20 69 6e 64 65 78 65 73 2e   r-tree indexes.
39321 0a 2a 2a 20 46 6f 72 20 61 20 74 77 6f 2d 64 69  .** For a two-di
39322 6d 65 6e 73 69 6f 6e 61 6c 20 72 2d 74 72 65 65  mensional r-tree
39323 20 73 74 72 75 63 74 75 72 65 20 63 61 6c 6c 65   structure calle
39324 64 20 22 72 74 22 2c 20 74 6f 20 64 65 73 65 72  d "rt", to deser
39325 69 61 6c 69 7a 65 0a 2a 2a 20 61 6c 6c 20 6e 6f  ialize.** all no
39326 64 65 73 2c 20 61 20 73 74 61 74 65 6d 65 6e 74  des, a statement
39327 20 6c 69 6b 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 53   like:.**.**   S
39328 45 4c 45 43 54 20 72 74 72 65 65 6e 6f 64 65 28  ELECT rtreenode(
39329 32 2c 20 64 61 74 61 29 20 46 52 4f 4d 20 72 74  2, data) FROM rt
3932a 5f 6e 6f 64 65 3b 0a 2a 2a 0a 2a 2a 20 54 68 65  _node;.**.** The
3932b 20 68 75 6d 61 6e 20 72 65 61 64 61 62 6c 65 20   human readable 
3932c 73 74 72 69 6e 67 20 74 61 6b 65 73 20 74 68 65  string takes the
3932d 20 66 6f 72 6d 20 6f 66 20 61 20 54 63 6c 20 6c   form of a Tcl l
3932e 69 73 74 20 77 69 74 68 20 6f 6e 65 0a 2a 2a 20  ist with one.** 
3932f 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 63  entry for each c
39330 65 6c 6c 20 69 6e 20 74 68 65 20 72 2d 74 72 65  ell in the r-tre
39331 65 20 6e 6f 64 65 2e 20 45 61 63 68 20 65 6e 74  e node. Each ent
39332 72 79 20 69 73 20 69 74 73 65 6c 66 20 61 0a 2a  ry is itself a.*
39333 2a 20 6c 69 73 74 2c 20 63 6f 6e 74 61 69 6e 69  * list, containi
39334 6e 67 20 74 68 65 20 38 2d 62 79 74 65 20 72 6f  ng the 8-byte ro
39335 77 69 64 2f 70 61 67 65 6e 6f 20 66 6f 6c 6c 6f  wid/pageno follo
39336 77 65 64 20 62 79 20 74 68 65 20 0a 2a 2a 20 3c  wed by the .** <
39337 6e 75 6d 2d 64 69 6d 65 6e 73 69 6f 6e 3e 2a 32  num-dimension>*2
39338 20 63 6f 6f 72 64 69 6e 61 74 65 73 2e 0a 2a 2f   coordinates..*/
39339 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 74 72  .static void rtr
3933a 65 65 6e 6f 64 65 28 73 71 6c 69 74 65 33 5f 63  eenode(sqlite3_c
3933b 6f 6e 74 65 78 74 20 2a 63 74 78 2c 20 69 6e 74  ontext *ctx, int
3933c 20 6e 41 72 67 2c 20 73 71 6c 69 74 65 33 5f 76   nArg, sqlite3_v
3933d 61 6c 75 65 20 2a 2a 61 70 41 72 67 29 7b 0a 20  alue **apArg){. 
3933e 20 63 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 30   char *zText = 0
3933f 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 6e 6f  ;.  RtreeNode no
39340 64 65 3b 0a 20 20 52 74 72 65 65 20 74 72 65 65  de;.  Rtree tree
39341 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 0a 20 20 6d  ;.  int ii;..  m
39342 65 6d 73 65 74 28 26 6e 6f 64 65 2c 20 30 2c 20  emset(&node, 0, 
39343 73 69 7a 65 6f 66 28 52 74 72 65 65 4e 6f 64 65  sizeof(RtreeNode
39344 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 74 72  ));.  memset(&tr
39345 65 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 52 74  ee, 0, sizeof(Rt
39346 72 65 65 29 29 3b 0a 20 20 74 72 65 65 2e 6e 44  ree));.  tree.nD
39347 69 6d 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  im = sqlite3_val
39348 75 65 5f 69 6e 74 28 61 70 41 72 67 5b 30 5d 29  ue_int(apArg[0])
39349 3b 0a 20 20 74 72 65 65 2e 6e 42 79 74 65 73 50  ;.  tree.nBytesP
3934a 65 72 43 65 6c 6c 20 3d 20 38 20 2b 20 38 20 2a  erCell = 8 + 8 *
3934b 20 74 72 65 65 2e 6e 44 69 6d 3b 0a 20 20 6e 6f   tree.nDim;.  no
3934c 64 65 2e 7a 44 61 74 61 20 3d 20 28 75 38 20 2a  de.zData = (u8 *
3934d 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62  )sqlite3_value_b
3934e 6c 6f 62 28 61 70 41 72 67 5b 31 5d 29 3b 0a 0a  lob(apArg[1]);..
3934f 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 4e    for(ii=0; ii<N
39350 43 45 4c 4c 28 26 6e 6f 64 65 29 3b 20 69 69 2b  CELL(&node); ii+
39351 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 43 65  +){.    char zCe
39352 6c 6c 5b 35 31 32 5d 3b 0a 20 20 20 20 69 6e 74  ll[512];.    int
39353 20 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 20 20 20   nCell = 0;.    
39354 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a  RtreeCell cell;.
39355 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 0a 20 20 20      int jj;..   
39356 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 26 74 72   nodeGetCell(&tr
39357 65 65 2c 20 26 6e 6f 64 65 2c 20 69 69 2c 20 26  ee, &node, ii, &
39358 63 65 6c 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74  cell);.    sqlit
39359 65 33 5f 73 6e 70 72 69 6e 74 66 28 35 31 32 2d  e3_snprintf(512-
3935a 6e 43 65 6c 6c 2c 26 7a 43 65 6c 6c 5b 6e 43 65  nCell,&zCell[nCe
3935b 6c 6c 5d 2c 22 25 64 22 2c 20 63 65 6c 6c 2e 69  ll],"%d", cell.i
3935c 52 6f 77 69 64 29 3b 0a 20 20 20 20 6e 43 65 6c  Rowid);.    nCel
3935d 6c 20 3d 20 73 74 72 6c 65 6e 28 7a 43 65 6c 6c  l = strlen(zCell
3935e 29 3b 0a 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b  );.    for(jj=0;
3935f 20 6a 6a 3c 74 72 65 65 2e 6e 44 69 6d 2a 32 3b   jj<tree.nDim*2;
39360 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71   jj++){.      sq
39361 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 35  lite3_snprintf(5
39362 31 32 2d 6e 43 65 6c 6c 2c 26 7a 43 65 6c 6c 5b  12-nCell,&zCell[
39363 6e 43 65 6c 6c 5d 2c 22 20 25 66 22 2c 28 64 6f  nCell]," %f",(do
39364 75 62 6c 65 29 63 65 6c 6c 2e 61 43 6f 6f 72 64  uble)cell.aCoord
39365 5b 6a 6a 5d 2e 66 29 3b 0a 20 20 20 20 20 20 6e  [jj].f);.      n
39366 43 65 6c 6c 20 3d 20 73 74 72 6c 65 6e 28 7a 43  Cell = strlen(zC
39367 65 6c 6c 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ell);.    }..   
39368 20 69 66 28 20 7a 54 65 78 74 20 29 7b 0a 20 20   if( zText ){.  
39369 20 20 20 20 63 68 61 72 20 2a 7a 54 65 78 74 4e      char *zTextN
3936a 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  ew = sqlite3_mpr
3936b 69 6e 74 66 28 22 25 73 20 7b 25 73 7d 22 2c 20  intf("%s {%s}", 
3936c 7a 54 65 78 74 2c 20 7a 43 65 6c 6c 29 3b 0a 20  zText, zCell);. 
3936d 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
3936e 65 28 7a 54 65 78 74 29 3b 0a 20 20 20 20 20 20  e(zText);.      
3936f 7a 54 65 78 74 20 3d 20 7a 54 65 78 74 4e 65 77  zText = zTextNew
39370 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
39371 20 20 20 7a 54 65 78 74 20 3d 20 73 71 6c 69 74     zText = sqlit
39372 65 33 5f 6d 70 72 69 6e 74 66 28 22 7b 25 73 7d  e3_mprintf("{%s}
39373 22 2c 20 7a 43 65 6c 6c 29 3b 0a 20 20 20 20 7d  ", zCell);.    }
39374 0a 20 20 7d 0a 20 20 0a 20 20 73 71 6c 69 74 65  .  }.  .  sqlite
39375 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 74  3_result_text(ct
39376 78 2c 20 7a 54 65 78 74 2c 20 2d 31 2c 20 73 71  x, zText, -1, sq
39377 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 7d 0a 0a  lite3_free);.}..
39378 73 74 61 74 69 63 20 76 6f 69 64 20 72 74 72 65  static void rtre
39379 65 64 65 70 74 68 28 73 71 6c 69 74 65 33 5f 63  edepth(sqlite3_c
3937a 6f 6e 74 65 78 74 20 2a 63 74 78 2c 20 69 6e 74  ontext *ctx, int
3937b 20 6e 41 72 67 2c 20 73 71 6c 69 74 65 33 5f 76   nArg, sqlite3_v
3937c 61 6c 75 65 20 2a 2a 61 70 41 72 67 29 7b 0a 20  alue **apArg){. 
3937d 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
3937e 75 65 5f 74 79 70 65 28 61 70 41 72 67 5b 30 5d  ue_type(apArg[0]
3937f 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 0a  )!=SQLITE_BLOB .
39380 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 76 61     || sqlite3_va
39381 6c 75 65 5f 62 79 74 65 73 28 61 70 41 72 67 5b  lue_bytes(apArg[
39382 30 5d 29 3c 32 0a 20 20 29 7b 0a 20 20 20 20 73  0])<2.  ){.    s
39383 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
39384 72 6f 72 28 63 74 78 2c 20 22 49 6e 76 61 6c 69  ror(ctx, "Invali
39385 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 72 74  d argument to rt
39386 72 65 65 64 65 70 74 68 28 29 22 2c 20 2d 31 29  reedepth()", -1)
39387 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  ; .  }else{.    
39388 75 38 20 2a 7a 42 6c 6f 62 20 3d 20 28 75 38 20  u8 *zBlob = (u8 
39389 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
3938a 62 6c 6f 62 28 61 70 41 72 67 5b 30 5d 29 3b 0a  blob(apArg[0]);.
3938b 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
3938c 6c 74 5f 69 6e 74 28 63 74 78 2c 20 72 65 61 64  lt_int(ctx, read
3938d 49 6e 74 31 36 28 7a 42 6c 6f 62 29 29 3b 0a 20  Int16(zBlob));. 
3938e 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69   }.}../*.** Regi
3938f 73 74 65 72 20 74 68 65 20 72 2d 74 72 65 65 20  ster the r-tree 
39390 6d 6f 64 75 6c 65 20 77 69 74 68 20 64 61 74 61  module with data
39391 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62 2e 20  base handle db. 
39392 54 68 69 73 20 63 72 65 61 74 65 73 20 74 68 65  This creates the
39393 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c  .** virtual tabl
39394 65 20 6d 6f 64 75 6c 65 20 22 72 74 72 65 65 22  e module "rtree"
39395 20 61 6e 64 20 74 68 65 20 64 65 62 75 67 67 69   and the debuggi
39396 6e 67 2f 61 6e 61 6c 79 73 69 73 20 73 63 61 6c  ng/analysis scal
39397 61 72 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  ar .** function 
39398 22 72 74 72 65 65 6e 6f 64 65 22 2e 0a 2a 2f 0a  "rtreenode"..*/.
39399 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
3939a 6e 74 20 73 71 6c 69 74 65 33 52 74 72 65 65 49  nt sqlite3RtreeI
3939b 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29  nit(sqlite3 *db)
3939c 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
3939d 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 72  ITE_OK;..  if( r
3939e 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
3939f 20 20 20 20 69 6e 74 20 75 74 66 38 20 3d 20 53      int utf8 = S
393a0 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20  QLITE_UTF8;.    
393a1 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65  rc = sqlite3_cre
393a2 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c  ate_function(db,
393a3 20 22 72 74 72 65 65 6e 6f 64 65 22 2c 20 32 2c   "rtreenode", 2,
393a4 20 75 74 66 38 2c 20 30 2c 20 72 74 72 65 65 6e   utf8, 0, rtreen
393a5 6f 64 65 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a  ode, 0, 0);.  }.
393a6 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
393a7 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 75  _OK ){.    int u
393a8 74 66 38 20 3d 20 53 51 4c 49 54 45 5f 55 54 46  tf8 = SQLITE_UTF
393a9 38 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  8;.    rc = sqli
393aa 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
393ab 69 6f 6e 28 64 62 2c 20 22 72 74 72 65 65 64 65  ion(db, "rtreede
393ac 70 74 68 22 2c 20 31 2c 20 75 74 66 38 2c 20 30  pth", 1, utf8, 0
393ad 2c 72 74 72 65 65 64 65 70 74 68 2c 20 30 2c 20  ,rtreedepth, 0, 
393ae 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  0);.  }.  if( rc
393af 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
393b0 20 20 20 76 6f 69 64 20 2a 63 20 3d 20 28 76 6f     void *c = (vo
393b1 69 64 20 2a 29 52 54 52 45 45 5f 43 4f 4f 52 44  id *)RTREE_COORD
393b2 5f 52 45 41 4c 33 32 3b 0a 20 20 20 20 72 63 20  _REAL32;.    rc 
393b3 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  = sqlite3_create
393b4 5f 6d 6f 64 75 6c 65 5f 76 32 28 64 62 2c 20 22  _module_v2(db, "
393b5 72 74 72 65 65 22 2c 20 26 72 74 72 65 65 4d 6f  rtree", &rtreeMo
393b6 64 75 6c 65 2c 20 63 2c 20 30 29 3b 0a 20 20 7d  dule, c, 0);.  }
393b7 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
393b8 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 76 6f 69 64  E_OK ){.    void
393b9 20 2a 63 20 3d 20 28 76 6f 69 64 20 2a 29 52 54   *c = (void *)RT
393ba 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54 33 32 3b  REE_COORD_INT32;
393bb 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
393bc 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f  3_create_module_
393bd 76 32 28 64 62 2c 20 22 72 74 72 65 65 5f 69 33  v2(db, "rtree_i3
393be 32 22 2c 20 26 72 74 72 65 65 4d 6f 64 75 6c 65  2", &rtreeModule
393bf 2c 20 63 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20  , c, 0);.  }..  
393c0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69  return rc;.}..#i
393c1 66 20 21 53 51 4c 49 54 45 5f 43 4f 52 45 0a 53  f !SQLITE_CORE.S
393c2 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
393c3 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f  lite3_extension_
393c4 69 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33 20  init(.  sqlite3 
393c5 2a 64 62 2c 0a 20 20 63 68 61 72 20 2a 2a 70 7a  *db,.  char **pz
393c6 45 72 72 4d 73 67 2c 0a 20 20 63 6f 6e 73 74 20  ErrMsg,.  const 
393c7 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74  sqlite3_api_rout
393c8 69 6e 65 73 20 2a 70 41 70 69 0a 29 7b 0a 20 20  ines *pApi.){.  
393c9 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e  SQLITE_EXTENSION
393ca 5f 49 4e 49 54 32 28 70 41 70 69 29 0a 20 20 72  _INIT2(pApi).  r
393cb 65 74 75 72 6e 20 73 71 6c 69 74 65 33 52 74 72  eturn sqlite3Rtr
393cc 65 65 49 6e 69 74 28 64 62 29 3b 0a 7d 0a 23 65  eeInit(db);.}.#e
393cd 6e 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ndif..#endif../*
393ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
393cf 64 20 6f 66 20 72 74 72 65 65 2e 63 20 2a 2a 2a  d of rtree.c ***
393d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
393d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
393d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
393d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
393d4 67 69 6e 20 66 69 6c 65 20 69 63 75 2e 63 20 2a  gin file icu.c *
393d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
393d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
393d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
393d8 0a 2a 2a 20 32 30 30 37 20 4d 61 79 20 36 0a 2a  .** 2007 May 6.*
393d9 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
393da 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
393db 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
393dc 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
393dd 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
393de 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
393df 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
393e0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
393e1 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
393e2 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
393e3 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
393e4 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
393e5 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
393e6 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
393e7 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
393e8 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
393e9 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
393ea 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
393eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
393ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
393ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
393ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
393ef 20 24 49 64 3a 20 69 63 75 2e 63 2c 76 20 31 2e   $Id: icu.c,v 1.
393f0 37 20 32 30 30 37 2f 31 32 2f 31 33 20 32 31 3a  7 2007/12/13 21:
393f1 35 34 3a 31 31 20 64 72 68 20 45 78 70 20 24 0a  54:11 drh Exp $.
393f2 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
393f3 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 69 6e  implements an in
393f4 74 65 67 72 61 74 69 6f 6e 20 62 65 74 77 65 65  tegration betwee
393f5 6e 20 74 68 65 20 49 43 55 20 6c 69 62 72 61 72  n the ICU librar
393f6 79 20 0a 2a 2a 20 28 22 49 6e 74 65 72 6e 61 74  y .** ("Internat
393f7 69 6f 6e 61 6c 20 43 6f 6d 70 6f 6e 65 6e 74 73  ional Components
393f8 20 66 6f 72 20 55 6e 69 63 6f 64 65 22 2c 20 61   for Unicode", a
393f9 6e 20 6f 70 65 6e 2d 73 6f 75 72 63 65 20 6c 69  n open-source li
393fa 62 72 61 72 79 20 0a 2a 2a 20 66 6f 72 20 68 61  brary .** for ha
393fb 6e 64 6c 69 6e 67 20 75 6e 69 63 6f 64 65 20 64  ndling unicode d
393fc 61 74 61 29 20 61 6e 64 20 53 51 4c 69 74 65 2e  ata) and SQLite.
393fd 20 54 68 65 20 69 6e 74 65 67 72 61 74 69 6f 6e   The integration
393fe 20 75 73 65 73 20 0a 2a 2a 20 49 43 55 20 74 6f   uses .** ICU to
393ff 20 70 72 6f 76 69 64 65 20 74 68 65 20 66 6f 6c   provide the fol
39400 6c 6f 77 69 6e 67 20 74 6f 20 53 51 4c 69 74 65  lowing to SQLite
39401 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69  :.**.**   * An i
39402 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
39403 20 74 68 65 20 53 51 4c 20 72 65 67 65 78 70 28   the SQL regexp(
39404 29 20 66 75 6e 63 74 69 6f 6e 20 28 61 6e 64 20  ) function (and 
39405 68 65 6e 63 65 20 52 45 47 45 58 50 0a 2a 2a 20  hence REGEXP.** 
39406 20 20 20 20 6f 70 65 72 61 74 6f 72 29 20 75 73      operator) us
39407 69 6e 67 20 74 68 65 20 49 43 55 20 75 72 65 67  ing the ICU ureg
39408 65 78 5f 58 58 28 29 20 41 50 49 73 2e 0a 2a 2a  ex_XX() APIs..**
39409 0a 2a 2a 20 20 20 2a 20 49 6d 70 6c 65 6d 65 6e  .**   * Implemen
3940a 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 53  tations of the S
3940b 51 4c 20 73 63 61 6c 61 72 20 75 70 70 65 72 28  QL scalar upper(
3940c 29 20 61 6e 64 20 6c 6f 77 65 72 28 29 20 66 75  ) and lower() fu
3940d 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 66  nctions.**     f
3940e 6f 72 20 63 61 73 65 20 6d 61 70 70 69 6e 67 2e  or case mapping.
3940f 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 6e 74 65 67  .**.**   * Integ
39410 72 61 74 69 6f 6e 20 6f 66 20 49 43 55 20 61 6e  ration of ICU an
39411 64 20 53 51 4c 69 74 65 20 63 6f 6c 6c 61 74 69  d SQLite collati
39412 6f 6e 20 73 65 71 65 6e 63 65 73 2e 0a 2a 2a 0a  on seqences..**.
39413 2a 2a 20 20 20 2a 20 41 6e 20 69 6d 70 6c 65 6d  **   * An implem
39414 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
39415 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 74 68  LIKE operator th
39416 61 74 20 75 73 65 73 20 49 43 55 20 74 6f 20 0a  at uses ICU to .
39417 2a 2a 20 20 20 20 20 70 72 6f 76 69 64 65 20 63  **     provide c
39418 61 73 65 2d 69 6e 64 65 70 65 6e 64 65 6e 74 20  ase-independent 
39419 6d 61 74 63 68 69 6e 67 2e 0a 2a 2f 0a 0a 23 69  matching..*/..#i
3941a 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
3941b 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
3941c 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
3941d 5f 49 43 55 29 0a 0a 2f 2a 20 49 6e 63 6c 75 64  _ICU)../* Includ
3941e 65 20 49 43 55 20 68 65 61 64 65 72 73 20 2a 2f  e ICU headers */
3941f 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f  .#include <unico
39420 64 65 2f 75 74 79 70 65 73 2e 68 3e 0a 23 69 6e  de/utypes.h>.#in
39421 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75  clude <unicode/u
39422 72 65 67 65 78 2e 68 3e 0a 23 69 6e 63 6c 75 64  regex.h>.#includ
39423 65 20 3c 75 6e 69 63 6f 64 65 2f 75 73 74 72 69  e <unicode/ustri
39424 6e 67 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  ng.h>.#include <
39425 75 6e 69 63 6f 64 65 2f 75 63 6f 6c 2e 68 3e 0a  unicode/ucol.h>.
39426 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
39427 5f 43 4f 52 45 0a 20 20 53 51 4c 49 54 45 5f 45  _CORE.  SQLITE_E
39428 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 0a 23  XTENSION_INIT1.#
39429 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  else.#endif../*.
3942a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  ** Maximum lengt
3942b 68 20 28 69 6e 20 62 79 74 65 73 29 20 6f 66 20  h (in bytes) of 
3942c 74 68 65 20 70 61 74 74 65 72 6e 20 69 6e 20 61  the pattern in a
3942d 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 0a 2a 2a   LIKE or GLOB.**
3942e 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 23 69   operator..*/.#i
3942f 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
39430 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45  _LIKE_PATTERN_LE
39431 4e 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51  NGTH.# define SQ
39432 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41  LITE_MAX_LIKE_PA
39433 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20 35 30 30  TTERN_LENGTH 500
39434 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  00.#endif../*.**
39435 20 56 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69   Version of sqli
39436 74 65 33 5f 66 72 65 65 28 29 20 74 68 61 74 20  te3_free() that 
39437 69 73 20 61 6c 77 61 79 73 20 61 20 66 75 6e 63  is always a func
39438 74 69 6f 6e 2c 20 6e 65 76 65 72 20 61 20 6d 61  tion, never a ma
39439 63 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  cro..*/.static v
3943a 6f 69 64 20 78 46 72 65 65 28 76 6f 69 64 20 2a  oid xFree(void *
3943b 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  p){.  sqlite3_fr
3943c 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ee(p);.}../*.** 
3943d 43 6f 6d 70 61 72 65 20 74 77 6f 20 55 54 46 2d  Compare two UTF-
3943e 38 20 73 74 72 69 6e 67 73 20 66 6f 72 20 65 71  8 strings for eq
3943f 75 61 6c 69 74 79 20 77 68 65 72 65 20 74 68 65  uality where the
39440 20 66 69 72 73 74 20 73 74 72 69 6e 67 20 69 73   first string is
39441 0a 2a 2a 20 61 20 22 4c 49 4b 45 22 20 65 78 70  .** a "LIKE" exp
39442 72 65 73 73 69 6f 6e 2e 20 52 65 74 75 72 6e 20  ression. Return 
39443 74 72 75 65 20 28 31 29 20 69 66 20 74 68 65 79  true (1) if they
39444 20 61 72 65 20 74 68 65 20 73 61 6d 65 20 61 6e   are the same an
39445 64 20 0a 2a 2a 20 66 61 6c 73 65 20 28 30 29 20  d .** false (0) 
39446 69 66 20 74 68 65 79 20 61 72 65 20 64 69 66 66  if they are diff
39447 65 72 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  erent..*/.static
39448 20 69 6e 74 20 69 63 75 4c 69 6b 65 43 6f 6d 70   int icuLikeComp
39449 61 72 65 28 0a 20 20 63 6f 6e 73 74 20 75 69 6e  are(.  const uin
3944a 74 38 5f 74 20 2a 7a 50 61 74 74 65 72 6e 2c 20  t8_t *zPattern, 
3944b 20 20 2f 2a 20 4c 49 4b 45 20 70 61 74 74 65 72    /* LIKE patter
3944c 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 69 6e  n */.  const uin
3944d 74 38 5f 74 20 2a 7a 53 74 72 69 6e 67 2c 20 20  t8_t *zString,  
3944e 20 20 2f 2a 20 54 68 65 20 55 54 46 2d 38 20 73    /* The UTF-8 s
3944f 74 72 69 6e 67 20 74 6f 20 63 6f 6d 70 61 72 65  tring to compare
39450 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 63 6f   against */.  co
39451 6e 73 74 20 55 43 68 61 72 33 32 20 75 45 73 63  nst UChar32 uEsc
39452 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
39453 65 73 63 61 70 65 20 63 68 61 72 61 63 74 65 72  escape character
39454 20 2a 2f 0a 29 7b 0a 20 20 73 74 61 74 69 63 20   */.){.  static 
39455 63 6f 6e 73 74 20 69 6e 74 20 4d 41 54 43 48 5f  const int MATCH_
39456 4f 4e 45 20 3d 20 28 55 43 68 61 72 33 32 29 27  ONE = (UChar32)'
39457 5f 27 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  _';.  static con
39458 73 74 20 69 6e 74 20 4d 41 54 43 48 5f 41 4c 4c  st int MATCH_ALL
39459 20 3d 20 28 55 43 68 61 72 33 32 29 27 25 27 3b   = (UChar32)'%';
3945a 0a 0a 20 20 69 6e 74 20 69 50 61 74 74 65 72 6e  ..  int iPattern
3945b 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 43   = 0;       /* C
3945c 75 72 72 65 6e 74 20 62 79 74 65 20 69 6e 64 65  urrent byte inde
3945d 78 20 69 6e 20 7a 50 61 74 74 65 72 6e 20 2a 2f  x in zPattern */
3945e 0a 20 20 69 6e 74 20 69 53 74 72 69 6e 67 20 3d  .  int iString =
3945f 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 75   0;        /* Cu
39460 72 72 65 6e 74 20 62 79 74 65 20 69 6e 64 65 78  rrent byte index
39461 20 69 6e 20 7a 53 74 72 69 6e 67 20 2a 2f 0a 0a   in zString */..
39462 20 20 69 6e 74 20 70 72 65 76 45 73 63 61 70 65    int prevEscape
39463 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 72 75   = 0;     /* Tru
39464 65 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75  e if the previou
39465 73 20 63 68 61 72 61 63 74 65 72 20 77 61 73 20  s character was 
39466 75 45 73 63 20 2a 2f 0a 0a 20 20 77 68 69 6c 65  uEsc */..  while
39467 28 20 7a 50 61 74 74 65 72 6e 5b 69 50 61 74 74  ( zPattern[iPatt
39468 65 72 6e 5d 21 3d 30 20 29 7b 0a 0a 20 20 20 20  ern]!=0 ){..    
39469 2f 2a 20 52 65 61 64 20 28 61 6e 64 20 63 6f 6e  /* Read (and con
3946a 73 75 6d 65 29 20 74 68 65 20 6e 65 78 74 20 63  sume) the next c
3946b 68 61 72 61 63 74 65 72 20 66 72 6f 6d 20 74 68  haracter from th
3946c 65 20 69 6e 70 75 74 20 70 61 74 74 65 72 6e 2e  e input pattern.
3946d 20 2a 2f 0a 20 20 20 20 55 43 68 61 72 33 32 20   */.    UChar32 
3946e 75 50 61 74 74 65 72 6e 3b 0a 20 20 20 20 55 38  uPattern;.    U8
3946f 5f 4e 45 58 54 5f 55 4e 53 41 46 45 28 7a 50 61  _NEXT_UNSAFE(zPa
39470 74 74 65 72 6e 2c 20 69 50 61 74 74 65 72 6e 2c  ttern, iPattern,
39471 20 75 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 20   uPattern);.    
39472 61 73 73 65 72 74 28 75 50 61 74 74 65 72 6e 21  assert(uPattern!
39473 3d 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65  =0);..    /* The
39474 72 65 20 61 72 65 20 6e 6f 77 20 34 20 70 6f 73  re are now 4 pos
39475 73 69 62 69 6c 69 74 69 65 73 3a 0a 20 20 20 20  sibilities:.    
39476 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 31 2e  **.    **     1.
39477 20 75 50 61 74 74 65 72 6e 20 69 73 20 61 6e 20   uPattern is an 
39478 75 6e 65 73 63 61 70 65 64 20 6d 61 74 63 68 2d  unescaped match-
39479 61 6c 6c 20 63 68 61 72 61 63 74 65 72 20 22 25  all character "%
3947a 22 2c 0a 20 20 20 20 2a 2a 20 20 20 20 20 32 2e  ",.    **     2.
3947b 20 75 50 61 74 74 65 72 6e 20 69 73 20 61 6e 20   uPattern is an 
3947c 75 6e 65 73 63 61 70 65 64 20 6d 61 74 63 68 2d  unescaped match-
3947d 6f 6e 65 20 63 68 61 72 61 63 74 65 72 20 22 5f  one character "_
3947e 22 2c 0a 20 20 20 20 2a 2a 20 20 20 20 20 33 2e  ",.    **     3.
3947f 20 75 50 61 74 74 65 72 6e 20 69 73 20 61 6e 20   uPattern is an 
39480 75 6e 65 73 63 61 70 65 64 20 65 73 63 61 70 65  unescaped escape
39481 20 63 68 61 72 61 63 74 65 72 2c 20 6f 72 0a 20   character, or. 
39482 20 20 20 2a 2a 20 20 20 20 20 34 2e 20 75 50 61     **     4. uPa
39483 74 74 65 72 6e 20 69 73 20 74 6f 20 62 65 20 68  ttern is to be h
39484 61 6e 64 6c 65 64 20 61 73 20 61 6e 20 6f 72 64  andled as an ord
39485 69 6e 61 72 79 20 63 68 61 72 61 63 74 65 72 0a  inary character.
39486 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21      */.    if( !
39487 70 72 65 76 45 73 63 61 70 65 20 26 26 20 75 50  prevEscape && uP
39488 61 74 74 65 72 6e 3d 3d 4d 41 54 43 48 5f 41 4c  attern==MATCH_AL
39489 4c 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61  L ){.      /* Ca
3948a 73 65 20 31 2e 20 2a 2f 0a 20 20 20 20 20 20 75  se 1. */.      u
3948b 69 6e 74 38 5f 74 20 63 3b 0a 0a 20 20 20 20 20  int8_t c;..     
3948c 20 2f 2a 20 53 6b 69 70 20 61 6e 79 20 4d 41 54   /* Skip any MAT
3948d 43 48 5f 41 4c 4c 20 6f 72 20 4d 41 54 43 48 5f  CH_ALL or MATCH_
3948e 4f 4e 45 20 63 68 61 72 61 63 74 65 72 73 20 74  ONE characters t
3948f 68 61 74 20 66 6f 6c 6c 6f 77 20 61 0a 20 20 20  hat follow a.   
39490 20 20 20 2a 2a 20 4d 41 54 43 48 5f 41 4c 4c 2e     ** MATCH_ALL.
39491 20 46 6f 72 20 65 61 63 68 20 4d 41 54 43 48 5f   For each MATCH_
39492 4f 4e 45 2c 20 73 6b 69 70 20 6f 6e 65 20 63 68  ONE, skip one ch
39493 61 72 61 63 74 65 72 20 69 6e 20 74 68 65 20 0a  aracter in the .
39494 20 20 20 20 20 20 2a 2a 20 74 65 73 74 20 73 74        ** test st
39495 72 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ring..      */. 
39496 20 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d 7a       while( (c=z
39497 50 61 74 74 65 72 6e 5b 69 50 61 74 74 65 72 6e  Pattern[iPattern
39498 5d 29 20 3d 3d 20 4d 41 54 43 48 5f 41 4c 4c 20  ]) == MATCH_ALL 
39499 7c 7c 20 63 20 3d 3d 20 4d 41 54 43 48 5f 4f 4e  || c == MATCH_ON
3949a 45 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  E ){.        if(
3949b 20 63 3d 3d 4d 41 54 43 48 5f 4f 4e 45 20 29 7b   c==MATCH_ONE ){
3949c 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a  .          if( z
3949d 53 74 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d 3d  String[iString]=
3949e 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
3949f 20 20 20 20 20 20 20 20 20 55 38 5f 46 57 44 5f           U8_FWD_
394a0 31 5f 55 4e 53 41 46 45 28 7a 53 74 72 69 6e 67  1_UNSAFE(zString
394a1 2c 20 69 53 74 72 69 6e 67 29 3b 0a 20 20 20 20  , iString);.    
394a2 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 50      }.        iP
394a3 61 74 74 65 72 6e 2b 2b 3b 0a 20 20 20 20 20 20  attern++;.      
394a4 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 7a 50 61  }..      if( zPa
394a5 74 74 65 72 6e 5b 69 50 61 74 74 65 72 6e 5d 3d  ttern[iPattern]=
394a6 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 0a  =0 ) return 1;..
394a7 20 20 20 20 20 20 77 68 69 6c 65 28 20 7a 53 74        while( zSt
394a8 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d 20 29 7b  ring[iString] ){
394a9 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 63 75  .        if( icu
394aa 4c 69 6b 65 43 6f 6d 70 61 72 65 28 26 7a 50 61  LikeCompare(&zPa
394ab 74 74 65 72 6e 5b 69 50 61 74 74 65 72 6e 5d 2c  ttern[iPattern],
394ac 20 26 7a 53 74 72 69 6e 67 5b 69 53 74 72 69 6e   &zString[iStrin
394ad 67 5d 2c 20 75 45 73 63 29 20 29 7b 0a 20 20 20  g], uEsc) ){.   
394ae 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b         return 1;
394af 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
394b0 20 20 20 55 38 5f 46 57 44 5f 31 5f 55 4e 53 41     U8_FWD_1_UNSA
394b1 46 45 28 7a 53 74 72 69 6e 67 2c 20 69 53 74 72  FE(zString, iStr
394b2 69 6e 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ing);.      }.  
394b3 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20      return 0;.. 
394b4 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 72     }else if( !pr
394b5 65 76 45 73 63 61 70 65 20 26 26 20 75 50 61 74  evEscape && uPat
394b6 74 65 72 6e 3d 3d 4d 41 54 43 48 5f 4f 4e 45 20  tern==MATCH_ONE 
394b7 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 73 65  ){.      /* Case
394b8 20 32 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28   2. */.      if(
394b9 20 7a 53 74 72 69 6e 67 5b 69 53 74 72 69 6e 67   zString[iString
394ba 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  ]==0 ) return 0;
394bb 0a 20 20 20 20 20 20 55 38 5f 46 57 44 5f 31 5f  .      U8_FWD_1_
394bc 55 4e 53 41 46 45 28 7a 53 74 72 69 6e 67 2c 20  UNSAFE(zString, 
394bd 69 53 74 72 69 6e 67 29 3b 0a 0a 20 20 20 20 7d  iString);..    }
394be 65 6c 73 65 20 69 66 28 20 21 70 72 65 76 45 73  else if( !prevEs
394bf 63 61 70 65 20 26 26 20 75 50 61 74 74 65 72 6e  cape && uPattern
394c0 3d 3d 75 45 73 63 29 7b 0a 20 20 20 20 20 20 2f  ==uEsc){.      /
394c1 2a 20 43 61 73 65 20 33 2e 20 2a 2f 0a 20 20 20  * Case 3. */.   
394c2 20 20 20 70 72 65 76 45 73 63 61 70 65 20 3d 20     prevEscape = 
394c3 31 3b 0a 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  1;..    }else{. 
394c4 20 20 20 20 20 2f 2a 20 43 61 73 65 20 34 2e 20       /* Case 4. 
394c5 2a 2f 0a 20 20 20 20 20 20 55 43 68 61 72 33 32  */.      UChar32
394c6 20 75 53 74 72 69 6e 67 3b 0a 20 20 20 20 20 20   uString;.      
394c7 55 38 5f 4e 45 58 54 5f 55 4e 53 41 46 45 28 7a  U8_NEXT_UNSAFE(z
394c8 53 74 72 69 6e 67 2c 20 69 53 74 72 69 6e 67 2c  String, iString,
394c9 20 75 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20   uString);.     
394ca 20 75 53 74 72 69 6e 67 20 3d 20 75 5f 66 6f 6c   uString = u_fol
394cb 64 43 61 73 65 28 75 53 74 72 69 6e 67 2c 20 55  dCase(uString, U
394cc 5f 46 4f 4c 44 5f 43 41 53 45 5f 44 45 46 41 55  _FOLD_CASE_DEFAU
394cd 4c 54 29 3b 0a 20 20 20 20 20 20 75 50 61 74 74  LT);.      uPatt
394ce 65 72 6e 20 3d 20 75 5f 66 6f 6c 64 43 61 73 65  ern = u_foldCase
394cf 28 75 50 61 74 74 65 72 6e 2c 20 55 5f 46 4f 4c  (uPattern, U_FOL
394d0 44 5f 43 41 53 45 5f 44 45 46 41 55 4c 54 29 3b  D_CASE_DEFAULT);
394d1 0a 20 20 20 20 20 20 69 66 28 20 75 53 74 72 69  .      if( uStri
394d2 6e 67 21 3d 75 50 61 74 74 65 72 6e 20 29 7b 0a  ng!=uPattern ){.
394d3 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30          return 0
394d4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
394d5 70 72 65 76 45 73 63 61 70 65 20 3d 20 30 3b 0a  prevEscape = 0;.
394d6 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
394d7 75 72 6e 20 7a 53 74 72 69 6e 67 5b 69 53 74 72  urn zString[iStr
394d8 69 6e 67 5d 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  ing]==0;.}../*.*
394d9 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
394da 20 6f 66 20 74 68 65 20 6c 69 6b 65 28 29 20 53   of the like() S
394db 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  QL function.  Th
394dc 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c  is function impl
394dd 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 65 20 62 75  ements.** the bu
394de 69 6c 64 2d 69 6e 20 4c 49 4b 45 20 6f 70 65 72  ild-in LIKE oper
394df 61 74 6f 72 2e 20 20 54 68 65 20 66 69 72 73 74  ator.  The first
394e0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
394e1 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 68 65   function is the
394e2 0a 2a 2a 20 70 61 74 74 65 72 6e 20 61 6e 64 20  .** pattern and 
394e3 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
394e4 65 6e 74 20 69 73 20 74 68 65 20 73 74 72 69 6e  ent is the strin
394e5 67 2e 20 20 53 6f 2c 20 74 68 65 20 53 51 4c 20  g.  So, the SQL 
394e6 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a  statements:.**.*
394e7 2a 20 20 20 20 20 20 20 41 20 4c 49 4b 45 20 42  *       A LIKE B
394e8 0a 2a 2a 0a 2a 2a 20 69 73 20 69 6d 70 6c 65 6d  .**.** is implem
394e9 65 6e 74 65 64 20 61 73 20 6c 69 6b 65 28 42 2c  ented as like(B,
394ea 20 41 29 2e 20 49 66 20 74 68 65 72 65 20 69 73   A). If there is
394eb 20 61 6e 20 65 73 63 61 70 65 20 63 68 61 72 61   an escape chara
394ec 63 74 65 72 20 45 2c 20 0a 2a 2a 0a 2a 2a 20 20  cter E, .**.**  
394ed 20 20 20 20 20 41 20 4c 49 4b 45 20 42 20 45 53       A LIKE B ES
394ee 43 41 50 45 20 45 0a 2a 2a 0a 2a 2a 20 69 73 20  CAPE E.**.** is 
394ef 6d 61 70 70 65 64 20 74 6f 20 6c 69 6b 65 28 42  mapped to like(B
394f0 2c 20 41 2c 20 45 29 2e 0a 2a 2f 0a 73 74 61 74  , A, E)..*/.stat
394f1 69 63 20 76 6f 69 64 20 69 63 75 4c 69 6b 65 46  ic void icuLikeF
394f2 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
394f3 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
394f4 20 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20   .  int argc, . 
394f5 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
394f6 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74  *argv.){.  const
394f7 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
394f8 7a 41 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  zA = sqlite3_val
394f9 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
394fa 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ;.  const unsign
394fb 65 64 20 63 68 61 72 20 2a 7a 42 20 3d 20 73 71  ed char *zB = sq
394fc 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
394fd 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 55 43 68  (argv[1]);.  UCh
394fe 61 72 33 32 20 75 45 73 63 20 3d 20 30 3b 0a 0a  ar32 uEsc = 0;..
394ff 20 20 2f 2a 20 4c 69 6d 69 74 20 74 68 65 20 6c    /* Limit the l
39500 65 6e 67 74 68 20 6f 66 20 74 68 65 20 4c 49 4b  ength of the LIK
39501 45 20 6f 72 20 47 4c 4f 42 20 70 61 74 74 65 72  E or GLOB patter
39502 6e 20 74 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c  n to avoid probl
39503 65 6d 73 0a 20 20 2a 2a 20 6f 66 20 64 65 65 70  ems.  ** of deep
39504 20 72 65 63 75 72 73 69 6f 6e 20 61 6e 64 20 4e   recursion and N
39505 2a 4e 20 62 65 68 61 76 69 6f 72 20 69 6e 20 70  *N behavior in p
39506 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 29 2e  atternCompare().
39507 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
39508 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
39509 61 72 67 76 5b 30 5d 29 3e 53 51 4c 49 54 45 5f  argv[0])>SQLITE_
3950a 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e  MAX_LIKE_PATTERN
3950b 5f 4c 45 4e 47 54 48 20 29 7b 0a 20 20 20 20 73  _LENGTH ){.    s
3950c 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
3950d 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 4c 49  ror(context, "LI
3950e 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61 74 74 65  KE or GLOB patte
3950f 72 6e 20 74 6f 6f 20 63 6f 6d 70 6c 65 78 22 2c  rn too complex",
39510 20 2d 31 29 3b 0a 20 20 20 20 72 65 74 75 72 6e   -1);.    return
39511 3b 0a 20 20 7d 0a 0a 0a 20 20 69 66 28 20 61 72  ;.  }...  if( ar
39512 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20 2f 2a 20  gc==3 ){.    /* 
39513 54 68 65 20 65 73 63 61 70 65 20 63 68 61 72 61  The escape chara
39514 63 74 65 72 20 73 74 72 69 6e 67 20 6d 75 73 74  cter string must
39515 20 63 6f 6e 73 69 73 74 20 6f 66 20 61 20 73 69   consist of a si
39516 6e 67 6c 65 20 55 54 46 2d 38 20 63 68 61 72 61  ngle UTF-8 chara
39517 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20 4f 74 68  cter..    ** Oth
39518 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 20 61  erwise, return a
39519 6e 20 65 72 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a  n error..    */.
3951a 20 20 20 20 69 6e 74 20 6e 45 3d 20 73 71 6c 69      int nE= sqli
3951b 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
3951c 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 63 6f  argv[2]);.    co
3951d 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
3951e 72 20 2a 7a 45 20 3d 20 73 71 6c 69 74 65 33 5f  r *zE = sqlite3_
3951f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
39520 32 5d 29 3b 0a 20 20 20 20 69 6e 74 20 69 20 3d  2]);.    int i =
39521 20 30 3b 0a 20 20 20 20 69 66 28 20 7a 45 3d 3d   0;.    if( zE==
39522 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  0 ) return;.    
39523 55 38 5f 4e 45 58 54 28 7a 45 2c 20 69 2c 20 6e  U8_NEXT(zE, i, n
39524 45 2c 20 75 45 73 63 29 3b 0a 20 20 20 20 69 66  E, uEsc);.    if
39525 28 20 69 21 3d 6e 45 29 7b 0a 20 20 20 20 20 20  ( i!=nE){.      
39526 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
39527 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 0a 20  rror(context, . 
39528 20 20 20 20 20 20 20 20 20 22 45 53 43 41 50 45           "ESCAPE
39529 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 75 73 74   expression must
3952a 20 62 65 20 61 20 73 69 6e 67 6c 65 20 63 68 61   be a single cha
3952b 72 61 63 74 65 72 22 2c 20 2d 31 29 3b 0a 20 20  racter", -1);.  
3952c 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
3952d 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 7a 41 20  }.  }..  if( zA 
3952e 26 26 20 7a 42 20 29 7b 0a 20 20 20 20 73 71 6c  && zB ){.    sql
3952f 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28  ite3_result_int(
39530 63 6f 6e 74 65 78 74 2c 20 69 63 75 4c 69 6b 65  context, icuLike
39531 43 6f 6d 70 61 72 65 28 7a 41 2c 20 7a 42 2c 20  Compare(zA, zB, 
39532 75 45 73 63 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  uEsc));.  }.}../
39533 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
39534 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65  on is called whe
39535 6e 20 61 6e 20 49 43 55 20 66 75 6e 63 74 69 6f  n an ICU functio
39536 6e 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69  n called from wi
39537 74 68 69 6e 0a 2a 2a 20 74 68 65 20 69 6d 70 6c  thin.** the impl
39538 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e  ementation of an
39539 20 53 51 4c 20 73 63 61 6c 61 72 20 66 75 6e 63   SQL scalar func
3953a 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 6e 20  tion returns an 
3953b 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  error..**.** The
3953c 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
3953d 20 63 6f 6e 74 65 78 74 20 70 61 73 73 65 64 20   context passed 
3953e 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67  as the first arg
3953f 75 6d 65 6e 74 20 69 73 20 0a 2a 2a 20 6c 6f 61  ument is .** loa
39540 64 65 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f  ded with an erro
39541 72 20 6d 65 73 73 61 67 65 20 62 61 73 65 64 20  r message based 
39542 6f 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  on the following
39543 20 74 77 6f 20 61 72 67 73 2e 0a 2a 2f 0a 73 74   two args..*/.st
39544 61 74 69 63 20 76 6f 69 64 20 69 63 75 46 75 6e  atic void icuFun
39545 63 74 69 6f 6e 45 72 72 6f 72 28 0a 20 20 73 71  ctionError(.  sq
39546 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
39547 43 74 78 2c 20 20 20 20 20 20 20 2f 2a 20 53 51  Ctx,       /* SQ
39548 4c 69 74 65 20 73 63 61 6c 61 72 20 66 75 6e 63  Lite scalar func
39549 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  tion context */.
3954a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
3954b 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ame,           /
3954c 2a 20 4e 61 6d 65 20 6f 66 20 49 43 55 20 66 75  * Name of ICU fu
3954d 6e 63 74 69 6f 6e 20 74 68 61 74 20 66 61 69 6c  nction that fail
3954e 65 64 20 2a 2f 0a 20 20 55 45 72 72 6f 72 43 6f  ed */.  UErrorCo
3954f 64 65 20 65 20 20 20 20 20 20 20 20 20 20 20 20  de e            
39550 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f       /* Error co
39551 64 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 49  de returned by I
39552 43 55 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29  CU function */.)
39553 7b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 31 32  {.  char zBuf[12
39554 38 5d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e  8];.  sqlite3_sn
39555 70 72 69 6e 74 66 28 31 32 38 2c 20 7a 42 75 66  printf(128, zBuf
39556 2c 20 22 49 43 55 20 65 72 72 6f 72 3a 20 25 73  , "ICU error: %s
39557 28 29 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 2c 20  (): %s", zName, 
39558 75 5f 65 72 72 6f 72 4e 61 6d 65 28 65 29 29 3b  u_errorName(e));
39559 0a 20 20 7a 42 75 66 5b 31 32 37 5d 20 3d 20 27  .  zBuf[127] = '
3955a 5c 30 27 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72  \0';.  sqlite3_r
3955b 65 73 75 6c 74 5f 65 72 72 6f 72 28 70 43 74 78  esult_error(pCtx
3955c 2c 20 7a 42 75 66 2c 20 2d 31 29 3b 0a 7d 0a 0a  , zBuf, -1);.}..
3955d 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 74  /*.** Function t
3955e 6f 20 64 65 6c 65 74 65 20 63 6f 6d 70 69 6c 65  o delete compile
3955f 64 20 72 65 67 65 78 70 20 6f 62 6a 65 63 74 73  d regexp objects
39560 2e 20 52 65 67 69 73 74 65 72 65 64 20 61 73 0a  . Registered as.
39561 2a 2a 20 61 20 64 65 73 74 72 75 63 74 6f 72 20  ** a destructor 
39562 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 73 71  function with sq
39563 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74  lite3_set_auxdat
39564 61 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  a()..*/.static v
39565 6f 69 64 20 69 63 75 52 65 67 65 78 70 44 65 6c  oid icuRegexpDel
39566 65 74 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20  ete(void *p){.  
39567 55 52 65 67 75 6c 61 72 45 78 70 72 65 73 73 69  URegularExpressi
39568 6f 6e 20 2a 70 45 78 70 72 20 3d 20 28 55 52 65  on *pExpr = (URe
39569 67 75 6c 61 72 45 78 70 72 65 73 73 69 6f 6e 20  gularExpression 
3956a 2a 29 70 3b 0a 20 20 75 72 65 67 65 78 5f 63 6c  *)p;.  uregex_cl
3956b 6f 73 65 28 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f  ose(pExpr);.}../
3956c 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
3956d 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 20 52 45  ion of SQLite RE
3956e 47 45 58 50 20 6f 70 65 72 61 74 6f 72 2e 20 54  GEXP operator. T
3956f 68 69 73 20 73 63 61 6c 61 72 20 66 75 6e 63 74  his scalar funct
39570 69 6f 6e 20 74 61 6b 65 73 0a 2a 2a 20 74 77 6f  ion takes.** two
39571 20 61 72 67 75 6d 65 6e 74 73 2e 20 54 68 65 20   arguments. The 
39572 66 69 72 73 74 20 69 73 20 61 20 72 65 67 75 6c  first is a regul
39573 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 70 61  ar expression pa
39574 74 74 65 72 6e 20 74 6f 20 63 6f 6d 70 69 6c 65  ttern to compile
39575 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 69  .** the second i
39576 73 20 61 20 73 74 72 69 6e 67 20 74 6f 20 6d 61  s a string to ma
39577 74 63 68 20 61 67 61 69 6e 73 74 20 74 68 61 74  tch against that
39578 20 70 61 74 74 65 72 6e 2e 20 49 66 20 65 69 74   pattern. If eit
39579 68 65 72 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  her .** argument
3957a 20 69 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2c   is an SQL NULL,
3957b 20 74 68 65 6e 20 4e 55 4c 4c 20 49 73 20 72 65   then NULL Is re
3957c 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73  turned. Otherwis
3957d 65 2c 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a  e, the result.**
3957e 20 69 73 20 31 20 69 66 20 74 68 65 20 73 74 72   is 1 if the str
3957f 69 6e 67 20 6d 61 74 63 68 65 73 20 74 68 65 20  ing matches the 
39580 70 61 74 74 65 72 6e 2c 20 6f 72 20 30 20 6f 74  pattern, or 0 ot
39581 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 53  herwise..**.** S
39582 51 4c 69 74 65 20 6d 61 70 73 20 74 68 65 20 72  QLite maps the r
39583 65 67 65 78 70 28 29 20 66 75 6e 63 74 69 6f 6e  egexp() function
39584 20 74 6f 20 74 68 65 20 72 65 67 65 78 70 28 29   to the regexp()
39585 20 6f 70 65 72 61 74 6f 72 20 73 75 63 68 0a 2a   operator such.*
39586 2a 20 74 68 61 74 20 74 68 65 20 66 6f 6c 6c 6f  * that the follo
39587 77 69 6e 67 20 74 77 6f 20 61 72 65 20 65 71 75  wing two are equ
39588 69 76 61 6c 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20  ivalent:.**.**  
39589 20 20 20 7a 53 74 72 69 6e 67 20 52 45 47 45 58     zString REGEX
3958a 50 20 7a 50 61 74 74 65 72 6e 0a 2a 2a 20 20 20  P zPattern.**   
3958b 20 20 72 65 67 65 78 70 28 7a 50 61 74 74 65 72    regexp(zPatter
3958c 6e 2c 20 7a 53 74 72 69 6e 67 29 0a 2a 2a 0a 2a  n, zString).**.*
3958d 2a 20 55 73 65 73 20 74 68 65 20 66 6f 6c 6c 6f  * Uses the follo
3958e 77 69 6e 67 20 49 43 55 20 72 65 67 65 78 70 20  wing ICU regexp 
3958f 41 50 49 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  APIs:.**.**     
39590 75 72 65 67 65 78 5f 6f 70 65 6e 28 29 0a 2a 2a  uregex_open().**
39591 20 20 20 20 20 75 72 65 67 65 78 5f 6d 61 74 63       uregex_matc
39592 68 65 73 28 29 0a 2a 2a 20 20 20 20 20 75 72 65  hes().**     ure
39593 67 65 78 5f 63 6c 6f 73 65 28 29 0a 2a 2f 0a 73  gex_close().*/.s
39594 74 61 74 69 63 20 76 6f 69 64 20 69 63 75 52 65  tatic void icuRe
39595 67 65 78 70 46 75 6e 63 28 73 71 6c 69 74 65 33  gexpFunc(sqlite3
39596 5f 63 6f 6e 74 65 78 74 20 2a 70 2c 20 69 6e 74  _context *p, int
39597 20 6e 41 72 67 2c 20 73 71 6c 69 74 65 33 5f 76   nArg, sqlite3_v
39598 61 6c 75 65 20 2a 2a 61 70 41 72 67 29 7b 0a 20  alue **apArg){. 
39599 20 55 45 72 72 6f 72 43 6f 64 65 20 73 74 61 74   UErrorCode stat
3959a 75 73 20 3d 20 55 5f 5a 45 52 4f 5f 45 52 52 4f  us = U_ZERO_ERRO
3959b 52 3b 0a 20 20 55 52 65 67 75 6c 61 72 45 78 70  R;.  URegularExp
3959c 72 65 73 73 69 6f 6e 20 2a 70 45 78 70 72 3b 0a  ression *pExpr;.
3959d 20 20 55 42 6f 6f 6c 20 72 65 73 3b 0a 20 20 63    UBool res;.  c
3959e 6f 6e 73 74 20 55 43 68 61 72 20 2a 7a 53 74 72  onst UChar *zStr
3959f 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  ing = sqlite3_va
395a0 6c 75 65 5f 74 65 78 74 31 36 28 61 70 41 72 67  lue_text16(apArg
395a1 5b 31 5d 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  [1]);..  /* If t
395a2 68 65 20 6c 65 66 74 20 68 61 6e 64 20 73 69 64  he left hand sid
395a3 65 20 6f 66 20 74 68 65 20 72 65 67 65 78 70 20  e of the regexp 
395a4 6f 70 65 72 61 74 6f 72 20 69 73 20 4e 55 4c 4c  operator is NULL
395a5 2c 20 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65  , .  ** then the
395a6 20 72 65 73 75 6c 74 20 69 73 20 61 6c 73 6f 20   result is also 
395a7 4e 55 4c 4c 2e 20 0a 20 20 2a 2f 0a 20 20 69 66  NULL. .  */.  if
395a8 28 20 21 7a 53 74 72 69 6e 67 20 29 7b 0a 20 20  ( !zString ){.  
395a9 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
395aa 20 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33   pExpr = sqlite3
395ab 5f 67 65 74 5f 61 75 78 64 61 74 61 28 70 2c 20  _get_auxdata(p, 
395ac 30 29 3b 0a 20 20 69 66 28 20 21 70 45 78 70 72  0);.  if( !pExpr
395ad 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 55 43   ){.    const UC
395ae 68 61 72 20 2a 7a 50 61 74 74 65 72 6e 20 3d 20  har *zPattern = 
395af 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
395b0 78 74 31 36 28 61 70 41 72 67 5b 30 5d 29 3b 0a  xt16(apArg[0]);.
395b1 20 20 20 20 69 66 28 20 21 7a 50 61 74 74 65 72      if( !zPatter
395b2 6e 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  n ){.      retur
395b3 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 45 78  n;.    }.    pEx
395b4 70 72 20 3d 20 75 72 65 67 65 78 5f 6f 70 65 6e  pr = uregex_open
395b5 28 7a 50 61 74 74 65 72 6e 2c 20 2d 31 2c 20 30  (zPattern, -1, 0
395b6 2c 20 30 2c 20 26 73 74 61 74 75 73 29 3b 0a 0a  , 0, &status);..
395b7 20 20 20 20 69 66 28 20 55 5f 53 55 43 43 45 53      if( U_SUCCES
395b8 53 28 73 74 61 74 75 73 29 20 29 7b 0a 20 20 20  S(status) ){.   
395b9 20 20 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61     sqlite3_set_a
395ba 75 78 64 61 74 61 28 70 2c 20 30 2c 20 70 45 78  uxdata(p, 0, pEx
395bb 70 72 2c 20 69 63 75 52 65 67 65 78 70 44 65 6c  pr, icuRegexpDel
395bc 65 74 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ete);.    }else{
395bd 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 21 70  .      assert(!p
395be 45 78 70 72 29 3b 0a 20 20 20 20 20 20 69 63 75  Expr);.      icu
395bf 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 70 2c  FunctionError(p,
395c0 20 22 75 72 65 67 65 78 5f 6f 70 65 6e 22 2c 20   "uregex_open", 
395c1 73 74 61 74 75 73 29 3b 0a 20 20 20 20 20 20 72  status);.      r
395c2 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d  eturn;.    }.  }
395c3 0a 0a 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65  ..  /* Configure
395c4 20 74 68 65 20 74 65 78 74 20 74 68 61 74 20 74   the text that t
395c5 68 65 20 72 65 67 75 6c 61 72 20 65 78 70 72 65  he regular expre
395c6 73 73 69 6f 6e 20 6f 70 65 72 61 74 65 73 20 6f  ssion operates o
395c7 6e 2e 20 2a 2f 0a 20 20 75 72 65 67 65 78 5f 73  n. */.  uregex_s
395c8 65 74 54 65 78 74 28 70 45 78 70 72 2c 20 7a 53  etText(pExpr, zS
395c9 74 72 69 6e 67 2c 20 2d 31 2c 20 26 73 74 61 74  tring, -1, &stat
395ca 75 73 29 3b 0a 20 20 69 66 28 20 21 55 5f 53 55  us);.  if( !U_SU
395cb 43 43 45 53 53 28 73 74 61 74 75 73 29 20 29 7b  CCESS(status) ){
395cc 0a 20 20 20 20 69 63 75 46 75 6e 63 74 69 6f 6e  .    icuFunction
395cd 45 72 72 6f 72 28 70 2c 20 22 75 72 65 67 65 78  Error(p, "uregex
395ce 5f 73 65 74 54 65 78 74 22 2c 20 73 74 61 74 75  _setText", statu
395cf 73 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  s);.    return;.
395d0 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 74 65 6d 70    }..  /* Attemp
395d1 74 20 74 68 65 20 6d 61 74 63 68 20 2a 2f 0a 20  t the match */. 
395d2 20 72 65 73 20 3d 20 75 72 65 67 65 78 5f 6d 61   res = uregex_ma
395d3 74 63 68 65 73 28 70 45 78 70 72 2c 20 30 2c 20  tches(pExpr, 0, 
395d4 26 73 74 61 74 75 73 29 3b 0a 20 20 69 66 28 20  &status);.  if( 
395d5 21 55 5f 53 55 43 43 45 53 53 28 73 74 61 74 75  !U_SUCCESS(statu
395d6 73 29 20 29 7b 0a 20 20 20 20 69 63 75 46 75 6e  s) ){.    icuFun
395d7 63 74 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75  ctionError(p, "u
395d8 72 65 67 65 78 5f 6d 61 74 63 68 65 73 22 2c 20  regex_matches", 
395d9 73 74 61 74 75 73 29 3b 0a 20 20 20 20 72 65 74  status);.    ret
395da 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53  urn;.  }..  /* S
395db 65 74 20 74 68 65 20 74 65 78 74 20 74 68 61 74  et the text that
395dc 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 70   the regular exp
395dd 72 65 73 73 69 6f 6e 20 6f 70 65 72 61 74 65 73  ression operates
395de 20 6f 6e 20 74 6f 20 61 20 4e 55 4c 4c 0a 20 20   on to a NULL.  
395df 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 54 68 69 73  ** pointer. This
395e0 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6e   is not really n
395e1 65 63 65 73 73 61 72 79 2c 20 62 75 74 20 69 74  ecessary, but it
395e2 20 69 73 20 74 69 64 69 65 72 20 74 68 61 6e 20   is tidier than 
395e3 0a 20 20 2a 2a 20 6c 65 61 76 69 6e 67 20 74 68  .  ** leaving th
395e4 65 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 73  e regular expres
395e5 73 69 6f 6e 20 6f 62 6a 65 63 74 20 63 6f 6e 66  sion object conf
395e6 69 67 75 72 65 64 20 77 69 74 68 20 61 6e 20 69  igured with an i
395e7 6e 76 61 6c 69 64 0a 20 20 2a 2a 20 70 6f 69 6e  nvalid.  ** poin
395e8 74 65 72 20 61 66 74 65 72 20 74 68 69 73 20 66  ter after this f
395e9 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e  unction returns.
395ea 0a 20 20 2a 2f 0a 20 20 75 72 65 67 65 78 5f 73  .  */.  uregex_s
395eb 65 74 54 65 78 74 28 70 45 78 70 72 2c 20 30 2c  etText(pExpr, 0,
395ec 20 30 2c 20 26 73 74 61 74 75 73 29 3b 0a 0a 20   0, &status);.. 
395ed 20 2f 2a 20 52 65 74 75 72 6e 20 31 20 6f 72 20   /* Return 1 or 
395ee 30 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  0. */.  sqlite3_
395ef 72 65 73 75 6c 74 5f 69 6e 74 28 70 2c 20 72 65  result_int(p, re
395f0 73 20 3f 20 31 20 3a 20 30 29 3b 0a 7d 0a 0a 2f  s ? 1 : 0);.}../
395f1 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
395f2 69 6f 6e 73 20 6f 66 20 73 63 61 6c 61 72 20 66  ions of scalar f
395f3 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 63 61 73  unctions for cas
395f4 65 20 6d 61 70 70 69 6e 67 20 2d 20 75 70 70 65  e mapping - uppe
395f5 72 28 29 20 61 6e 64 20 0a 2a 2a 20 6c 6f 77 65  r() and .** lowe
395f6 72 28 29 2e 20 46 75 6e 63 74 69 6f 6e 20 75 70  r(). Function up
395f7 70 65 72 28 29 20 63 6f 6e 76 65 72 74 73 20 69  per() converts i
395f8 74 73 20 69 6e 70 75 74 20 74 6f 20 75 70 70 65  ts input to uppe
395f9 72 2d 63 61 73 65 20 28 41 42 43 29 2e 0a 2a 2a  r-case (ABC)..**
395fa 20 46 75 6e 63 74 69 6f 6e 20 6c 6f 77 65 72 28   Function lower(
395fb 29 20 63 6f 6e 76 65 72 74 73 20 74 6f 20 6c 6f  ) converts to lo
395fc 77 65 72 2d 63 61 73 65 20 28 61 62 63 29 2e 0a  wer-case (abc)..
395fd 2a 2a 0a 2a 2a 20 49 43 55 20 70 72 6f 76 69 64  **.** ICU provid
395fe 65 73 20 74 77 6f 20 74 79 70 65 73 20 6f 66 20  es two types of 
395ff 63 61 73 65 20 6d 61 70 70 69 6e 67 2c 20 22 67  case mapping, "g
39600 65 6e 65 72 61 6c 22 20 63 61 73 65 20 6d 61 70  eneral" case map
39601 70 69 6e 67 20 61 6e 64 0a 2a 2a 20 22 6c 61 6e  ping and.** "lan
39602 67 75 61 67 65 20 73 70 65 63 69 66 69 63 22 2e  guage specific".
39603 20 52 65 66 65 72 20 74 6f 20 49 43 55 20 64 6f   Refer to ICU do
39604 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20  cumentation for 
39605 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 73 0a  the differences.
39606 2a 2a 20 62 65 74 77 65 65 6e 20 74 68 65 20 74  ** between the t
39607 77 6f 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 75 74 69  wo..**.** To uti
39608 6c 69 73 65 20 22 67 65 6e 65 72 61 6c 22 20 63  lise "general" c
39609 61 73 65 20 6d 61 70 70 69 6e 67 2c 20 74 68 65  ase mapping, the
3960a 20 75 70 70 65 72 28 29 20 6f 72 20 6c 6f 77 65   upper() or lowe
3960b 72 28 29 20 73 63 61 6c 61 72 20 0a 2a 2a 20 66  r() scalar .** f
3960c 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 69 6e 76  unctions are inv
3960d 6f 6b 65 64 20 77 69 74 68 20 6f 6e 65 20 61 72  oked with one ar
3960e 67 75 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20  gument:.**.**   
3960f 20 20 75 70 70 65 72 28 27 41 42 43 27 29 20 2d    upper('ABC') -
39610 3e 20 27 61 62 63 27 0a 2a 2a 20 20 20 20 20 6c  > 'abc'.**     l
39611 6f 77 65 72 28 27 61 62 63 27 29 20 2d 3e 20 27  ower('abc') -> '
39612 41 42 43 27 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 63  ABC'.**.** To ac
39613 63 65 73 73 20 49 43 55 20 22 6c 61 6e 67 75 61  cess ICU "langua
39614 67 65 20 73 70 65 63 69 66 69 63 22 20 63 61 73  ge specific" cas
39615 65 20 6d 61 70 70 69 6e 67 2c 20 75 70 70 65 72  e mapping, upper
39616 28 29 20 6f 72 20 6c 6f 77 65 72 28 29 0a 2a 2a  () or lower().**
39617 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 76 6f 6b   should be invok
39618 65 64 20 77 69 74 68 20 74 77 6f 20 61 72 67 75  ed with two argu
39619 6d 65 6e 74 73 2e 20 54 68 65 20 73 65 63 6f 6e  ments. The secon
3961a 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  d argument is th
3961b 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 74 68 65  e name.** of the
3961c 20 6c 6f 63 61 6c 65 20 74 6f 20 75 73 65 2e 20   locale to use. 
3961d 50 61 73 73 69 6e 67 20 61 6e 20 65 6d 70 74 79  Passing an empty
3961e 20 73 74 72 69 6e 67 20 28 22 22 29 20 6f 72 20   string ("") or 
3961f 53 51 4c 20 4e 55 4c 4c 20 76 61 6c 75 65 0a 2a  SQL NULL value.*
39620 2a 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  * as the second 
39621 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
39622 73 61 6d 65 20 61 73 20 69 6e 76 6f 6b 69 6e 67  same as invoking
39623 20 74 68 65 20 31 20 61 72 67 75 6d 65 6e 74 20   the 1 argument 
39624 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 75 70  version.** of up
39625 70 65 72 28 29 20 6f 72 20 6c 6f 77 65 72 28 29  per() or lower()
39626 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 6c 6f 77 65  ..**.**     lowe
39627 72 28 27 49 27 2c 20 27 65 6e 5f 75 73 27 29 20  r('I', 'en_us') 
39628 2d 3e 20 27 69 27 0a 2a 2a 20 20 20 20 20 6c 6f  -> 'i'.**     lo
39629 77 65 72 28 27 49 27 2c 20 27 74 72 5f 74 72 27  wer('I', 'tr_tr'
3962a 29 20 2d 3e 20 27 c4 b1 27 20 28 73 6d 61 6c 6c  ) -> '..' (small
3962b 20 64 6f 74 6c 65 73 73 20 69 29 0a 2a 2a 0a 2a   dotless i).**.*
3962c 2a 20 68 74 74 70 3a 2f 2f 77 77 77 2e 69 63 75  * http://www.icu
3962d 2d 70 72 6f 6a 65 63 74 2e 6f 72 67 2f 75 73 65  -project.org/use
3962e 72 67 75 69 64 65 2f 70 6f 73 69 78 2e 68 74 6d  rguide/posix.htm
3962f 6c 23 63 61 73 65 5f 6d 61 70 70 69 6e 67 73 0a  l#case_mappings.
39630 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  */.static void i
39631 63 75 43 61 73 65 46 75 6e 63 31 36 28 73 71 6c  cuCaseFunc16(sql
39632 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 2c  ite3_context *p,
39633 20 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c 69 74   int nArg, sqlit
39634 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67  e3_value **apArg
39635 29 7b 0a 20 20 63 6f 6e 73 74 20 55 43 68 61 72  ){.  const UChar
39636 20 2a 7a 49 6e 70 75 74 3b 0a 20 20 55 43 68 61   *zInput;.  UCha
39637 72 20 2a 7a 4f 75 74 70 75 74 3b 0a 20 20 69 6e  r *zOutput;.  in
39638 74 20 6e 49 6e 70 75 74 3b 0a 20 20 69 6e 74 20  t nInput;.  int 
39639 6e 4f 75 74 70 75 74 3b 0a 0a 20 20 55 45 72 72  nOutput;..  UErr
3963a 6f 72 43 6f 64 65 20 73 74 61 74 75 73 20 3d 20  orCode status = 
3963b 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a 20 20  U_ZERO_ERROR;.  
3963c 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 6f 63  const char *zLoc
3963d 61 6c 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65  ale = 0;..  asse
3963e 72 74 28 6e 41 72 67 3d 3d 31 20 7c 7c 20 6e 41  rt(nArg==1 || nA
3963f 72 67 3d 3d 32 29 3b 0a 20 20 69 66 28 20 6e 41  rg==2);.  if( nA
39640 72 67 3d 3d 32 20 29 7b 0a 20 20 20 20 7a 4c 6f  rg==2 ){.    zLo
39641 63 61 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68  cale = (const ch
39642 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
39643 75 65 5f 74 65 78 74 28 61 70 41 72 67 5b 31 5d  ue_text(apArg[1]
39644 29 3b 0a 20 20 7d 0a 0a 20 20 7a 49 6e 70 75 74  );.  }..  zInput
39645 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
39646 5f 74 65 78 74 31 36 28 61 70 41 72 67 5b 30 5d  _text16(apArg[0]
39647 29 3b 0a 20 20 69 66 28 20 21 7a 49 6e 70 75 74  );.  if( !zInput
39648 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
39649 20 20 7d 0a 20 20 6e 49 6e 70 75 74 20 3d 20 73    }.  nInput = s
3964a 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
3964b 65 73 31 36 28 61 70 41 72 67 5b 30 5d 29 3b 0a  es16(apArg[0]);.
3964c 0a 20 20 6e 4f 75 74 70 75 74 20 3d 20 6e 49 6e  .  nOutput = nIn
3964d 70 75 74 20 2a 20 32 20 2b 20 32 3b 0a 20 20 7a  put * 2 + 2;.  z
3964e 4f 75 74 70 75 74 20 3d 20 73 71 6c 69 74 65 33  Output = sqlite3
3964f 5f 6d 61 6c 6c 6f 63 28 6e 4f 75 74 70 75 74 29  _malloc(nOutput)
39650 3b 0a 20 20 69 66 28 20 21 7a 4f 75 74 70 75 74  ;.  if( !zOutput
39651 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
39652 20 20 7d 0a 0a 20 20 69 66 28 20 73 71 6c 69 74    }..  if( sqlit
39653 65 33 5f 75 73 65 72 5f 64 61 74 61 28 70 29 20  e3_user_data(p) 
39654 29 7b 0a 20 20 20 20 75 5f 73 74 72 54 6f 55 70  ){.    u_strToUp
39655 70 65 72 28 7a 4f 75 74 70 75 74 2c 20 6e 4f 75  per(zOutput, nOu
39656 74 70 75 74 2f 32 2c 20 7a 49 6e 70 75 74 2c 20  tput/2, zInput, 
39657 6e 49 6e 70 75 74 2f 32 2c 20 7a 4c 6f 63 61 6c  nInput/2, zLocal
39658 65 2c 20 26 73 74 61 74 75 73 29 3b 0a 20 20 7d  e, &status);.  }
39659 65 6c 73 65 7b 0a 20 20 20 20 75 5f 73 74 72 54  else{.    u_strT
3965a 6f 4c 6f 77 65 72 28 7a 4f 75 74 70 75 74 2c 20  oLower(zOutput, 
3965b 6e 4f 75 74 70 75 74 2f 32 2c 20 7a 49 6e 70 75  nOutput/2, zInpu
3965c 74 2c 20 6e 49 6e 70 75 74 2f 32 2c 20 7a 4c 6f  t, nInput/2, zLo
3965d 63 61 6c 65 2c 20 26 73 74 61 74 75 73 29 3b 0a  cale, &status);.
3965e 20 20 7d 0a 0a 20 20 69 66 28 20 21 55 5f 53 55    }..  if( !U_SU
3965f 43 43 45 53 53 28 73 74 61 74 75 73 29 20 29 7b  CCESS(status) ){
39660 0a 20 20 20 20 69 63 75 46 75 6e 63 74 69 6f 6e  .    icuFunction
39661 45 72 72 6f 72 28 70 2c 20 22 75 5f 73 74 72 54  Error(p, "u_strT
39662 6f 4c 6f 77 65 72 28 29 2f 75 5f 73 74 72 54 6f  oLower()/u_strTo
39663 55 70 70 65 72 22 2c 20 73 74 61 74 75 73 29 3b  Upper", status);
39664 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
39665 0a 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
39666 6c 74 5f 74 65 78 74 31 36 28 70 2c 20 7a 4f 75  lt_text16(p, zOu
39667 74 70 75 74 2c 20 2d 31 2c 20 78 46 72 65 65 29  tput, -1, xFree)
39668 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c 6c 61  ;.}../*.** Colla
39669 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 64 65  tion sequence de
3966a 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f  structor functio
3966b 6e 2e 20 54 68 65 20 70 43 74 78 20 61 72 67 75  n. The pCtx argu
3966c 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f 0a 2a  ment points to.*
3966d 2a 20 61 20 55 43 6f 6c 6c 61 74 6f 72 20 73 74  * a UCollator st
3966e 72 75 63 74 75 72 65 20 70 72 65 76 69 6f 75 73  ructure previous
3966f 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 75 73 69  ly allocated usi
39670 6e 67 20 75 63 6f 6c 5f 6f 70 65 6e 28 29 2e 0a  ng ucol_open()..
39671 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  */.static void i
39672 63 75 43 6f 6c 6c 61 74 69 6f 6e 44 65 6c 28 76  cuCollationDel(v
39673 6f 69 64 20 2a 70 43 74 78 29 7b 0a 20 20 55 43  oid *pCtx){.  UC
39674 6f 6c 6c 61 74 6f 72 20 2a 70 20 3d 20 28 55 43  ollator *p = (UC
39675 6f 6c 6c 61 74 6f 72 20 2a 29 70 43 74 78 3b 0a  ollator *)pCtx;.
39676 20 20 75 63 6f 6c 5f 63 6c 6f 73 65 28 70 29 3b    ucol_close(p);
39677 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c 6c 61 74  .}../*.** Collat
39678 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 63 6f 6d  ion sequence com
39679 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e  parison function
3967a 2e 20 54 68 65 20 70 43 74 78 20 61 72 67 75 6d  . The pCtx argum
3967b 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a  ent points to.**
3967c 20 61 20 55 43 6f 6c 6c 61 74 6f 72 20 73 74 72   a UCollator str
3967d 75 63 74 75 72 65 20 70 72 65 76 69 6f 75 73 6c  ucture previousl
3967e 79 20 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e  y allocated usin
3967f 67 20 75 63 6f 6c 5f 6f 70 65 6e 28 29 2e 0a 2a  g ucol_open()..*
39680 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 63 75  /.static int icu
39681 43 6f 6c 6c 61 74 69 6f 6e 43 6f 6c 6c 28 0a 20  CollationColl(. 
39682 20 76 6f 69 64 20 2a 70 43 74 78 2c 0a 20 20 69   void *pCtx,.  i
39683 6e 74 20 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e 73  nt nLeft,.  cons
39684 74 20 76 6f 69 64 20 2a 7a 4c 65 66 74 2c 0a 20  t void *zLeft,. 
39685 20 69 6e 74 20 6e 52 69 67 68 74 2c 0a 20 20 63   int nRight,.  c
39686 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 52 69 67 68  onst void *zRigh
39687 74 0a 29 7b 0a 20 20 55 43 6f 6c 6c 61 74 69 6f  t.){.  UCollatio
39688 6e 52 65 73 75 6c 74 20 72 65 73 3b 0a 20 20 55  nResult res;.  U
39689 43 6f 6c 6c 61 74 6f 72 20 2a 70 20 3d 20 28 55  Collator *p = (U
3968a 43 6f 6c 6c 61 74 6f 72 20 2a 29 70 43 74 78 3b  Collator *)pCtx;
3968b 0a 20 20 72 65 73 20 3d 20 75 63 6f 6c 5f 73 74  .  res = ucol_st
3968c 72 63 6f 6c 6c 28 70 2c 20 28 55 43 68 61 72 20  rcoll(p, (UChar 
3968d 2a 29 7a 4c 65 66 74 2c 20 6e 4c 65 66 74 2f 32  *)zLeft, nLeft/2
3968e 2c 20 28 55 43 68 61 72 20 2a 29 7a 52 69 67 68  , (UChar *)zRigh
3968f 74 2c 20 6e 52 69 67 68 74 2f 32 29 3b 0a 20 20  t, nRight/2);.  
39690 73 77 69 74 63 68 28 20 72 65 73 20 29 7b 0a 20  switch( res ){. 
39691 20 20 20 63 61 73 65 20 55 43 4f 4c 5f 4c 45 53     case UCOL_LES
39692 53 3a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b  S:    return -1;
39693 0a 20 20 20 20 63 61 73 65 20 55 43 4f 4c 5f 47  .    case UCOL_G
39694 52 45 41 54 45 52 3a 20 72 65 74 75 72 6e 20 2b  REATER: return +
39695 31 3b 0a 20 20 20 20 63 61 73 65 20 55 43 4f 4c  1;.    case UCOL
39696 5f 45 51 55 41 4c 3a 20 20 20 72 65 74 75 72 6e  _EQUAL:   return
39697 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74   0;.  }.  assert
39698 28 21 22 55 6e 65 78 70 65 63 74 65 64 20 72 65  (!"Unexpected re
39699 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20  turn value from 
3969a 75 63 6f 6c 5f 73 74 72 63 6f 6c 6c 28 29 22 29  ucol_strcoll()")
3969b 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ;.  return 0;.}.
3969c 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
3969d 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 63 61  ation of the sca
3969e 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 69 63 75  lar function icu
3969f 5f 6c 6f 61 64 5f 63 6f 6c 6c 61 74 69 6f 6e 28  _load_collation(
396a0 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 63  )..**.** This sc
396a1 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 69 73  alar function is
396a2 20 75 73 65 64 20 74 6f 20 61 64 64 20 49 43 55   used to add ICU
396a3 20 63 6f 6c 6c 61 74 69 6f 6e 20 62 61 73 65 64   collation based
396a4 20 63 6f 6c 6c 61 74 69 6f 6e 20 0a 2a 2a 20 74   collation .** t
396a5 79 70 65 73 20 74 6f 20 61 6e 20 53 51 4c 69 74  ypes to an SQLit
396a6 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
396a7 63 74 69 6f 6e 2e 20 49 74 20 69 73 20 69 6e 74  ction. It is int
396a8 65 6e 64 65 64 20 74 6f 20 62 65 20 63 61 6c 6c  ended to be call
396a9 65 64 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77 73  ed.** as follows
396aa 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45  :.**.**     SELE
396ab 43 54 20 69 63 75 5f 6c 6f 61 64 5f 63 6f 6c 6c  CT icu_load_coll
396ac 61 74 69 6f 6e 28 3c 6c 6f 63 61 6c 65 3e 2c 20  ation(<locale>, 
396ad 3c 63 6f 6c 6c 61 74 69 6f 6e 2d 6e 61 6d 65 3e  <collation-name>
396ae 29 3b 0a 2a 2a 0a 2a 2a 20 57 68 65 72 65 20 3c  );.**.** Where <
396af 6c 6f 63 61 6c 65 3e 20 69 73 20 61 20 73 74 72  locale> is a str
396b0 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61  ing containing a
396b1 6e 20 49 43 55 20 6c 6f 63 61 6c 65 20 69 64 65  n ICU locale ide
396b2 6e 74 69 66 69 65 72 20 28 69 2e 65 2e 0a 2a 2a  ntifier (i.e..**
396b3 20 22 65 6e 5f 41 55 22 2c 20 22 74 72 5f 54 52   "en_AU", "tr_TR
396b4 22 20 65 74 63 2e 29 20 61 6e 64 20 3c 63 6f 6c  " etc.) and <col
396b5 6c 61 74 69 6f 6e 2d 6e 61 6d 65 3e 20 69 73 20  lation-name> is 
396b6 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a  the name of the.
396b7 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ** collation seq
396b8 75 65 6e 63 65 20 74 6f 20 63 72 65 61 74 65 2e  uence to create.
396b9 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
396ba 69 63 75 4c 6f 61 64 43 6f 6c 6c 61 74 69 6f 6e  icuLoadCollation
396bb 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
396bc 65 78 74 20 2a 70 2c 20 0a 20 20 69 6e 74 20 6e  ext *p, .  int n
396bd 41 72 67 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f  Arg, .  sqlite3_
396be 76 61 6c 75 65 20 2a 2a 61 70 41 72 67 0a 29 7b  value **apArg.){
396bf 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
396c0 20 28 73 71 6c 69 74 65 33 20 2a 29 73 71 6c 69   (sqlite3 *)sqli
396c1 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 70 29  te3_user_data(p)
396c2 3b 0a 20 20 55 45 72 72 6f 72 43 6f 64 65 20 73  ;.  UErrorCode s
396c3 74 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f 45  tatus = U_ZERO_E
396c4 52 52 4f 52 3b 0a 20 20 63 6f 6e 73 74 20 63 68  RROR;.  const ch
396c5 61 72 20 2a 7a 4c 6f 63 61 6c 65 3b 20 20 20 20  ar *zLocale;    
396c6 20 20 2f 2a 20 4c 6f 63 61 6c 65 20 69 64 65 6e    /* Locale iden
396c7 74 69 66 69 65 72 20 2d 20 28 65 67 2e 20 22 6a  tifier - (eg. "j
396c8 70 5f 4a 50 22 29 20 2a 2f 0a 20 20 63 6f 6e 73  p_JP") */.  cons
396c9 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20  t char *zName;  
396ca 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 43 6f 6c        /* SQL Col
396cb 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
396cc 6e 61 6d 65 20 28 65 67 2e 20 22 6a 61 70 61 6e  name (eg. "japan
396cd 65 73 65 22 29 20 2a 2f 0a 20 20 55 43 6f 6c 6c  ese") */.  UColl
396ce 61 74 6f 72 20 2a 70 55 43 6f 6c 6c 61 74 6f 72  ator *pUCollator
396cf 3b 20 20 20 20 2f 2a 20 49 43 55 20 6c 69 62 72  ;    /* ICU libr
396d0 61 72 79 20 63 6f 6c 6c 61 74 69 6f 6e 20 6f 62  ary collation ob
396d1 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63  ject */.  int rc
396d2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
396d3 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
396d4 64 65 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  de from sqlite3_
396d5 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
396d6 5f 78 28 29 20 2a 2f 0a 0a 20 20 61 73 73 65 72  _x() */..  asser
396d7 74 28 6e 41 72 67 3d 3d 32 29 3b 0a 20 20 7a 4c  t(nArg==2);.  zL
396d8 6f 63 61 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63  ocale = (const c
396d9 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  har *)sqlite3_va
396da 6c 75 65 5f 74 65 78 74 28 61 70 41 72 67 5b 30  lue_text(apArg[0
396db 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 28 63  ]);.  zName = (c
396dc 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69  onst char *)sqli
396dd 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
396de 70 41 72 67 5b 31 5d 29 3b 0a 0a 20 20 69 66 28  pArg[1]);..  if(
396df 20 21 7a 4c 6f 63 61 6c 65 20 7c 7c 20 21 7a 4e   !zLocale || !zN
396e0 61 6d 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ame ){.    retur
396e1 6e 3b 0a 20 20 7d 0a 0a 20 20 70 55 43 6f 6c 6c  n;.  }..  pUColl
396e2 61 74 6f 72 20 3d 20 75 63 6f 6c 5f 6f 70 65 6e  ator = ucol_open
396e3 28 7a 4c 6f 63 61 6c 65 2c 20 26 73 74 61 74 75  (zLocale, &statu
396e4 73 29 3b 0a 20 20 69 66 28 20 21 55 5f 53 55 43  s);.  if( !U_SUC
396e5 43 45 53 53 28 73 74 61 74 75 73 29 20 29 7b 0a  CESS(status) ){.
396e6 20 20 20 20 69 63 75 46 75 6e 63 74 69 6f 6e 45      icuFunctionE
396e7 72 72 6f 72 28 70 2c 20 22 75 63 6f 6c 5f 6f 70  rror(p, "ucol_op
396e8 65 6e 22 2c 20 73 74 61 74 75 73 29 3b 0a 20 20  en", status);.  
396e9 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
396ea 61 73 73 65 72 74 28 70 29 3b 0a 0a 20 20 72 63  assert(p);..  rc
396eb 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74   = sqlite3_creat
396ec 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 64  e_collation_v2(d
396ed 62 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c 49 54 45  b, zName, SQLITE
396ee 5f 55 54 46 31 36 2c 20 28 76 6f 69 64 20 2a 29  _UTF16, (void *)
396ef 70 55 43 6f 6c 6c 61 74 6f 72 2c 20 0a 20 20 20  pUCollator, .   
396f0 20 20 20 69 63 75 43 6f 6c 6c 61 74 69 6f 6e 43     icuCollationC
396f1 6f 6c 6c 2c 20 69 63 75 43 6f 6c 6c 61 74 69 6f  oll, icuCollatio
396f2 6e 44 65 6c 0a 20 20 29 3b 0a 20 20 69 66 28 20  nDel.  );.  if( 
396f3 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
396f4 0a 20 20 20 20 75 63 6f 6c 5f 63 6c 6f 73 65 28  .    ucol_close(
396f5 70 55 43 6f 6c 6c 61 74 6f 72 29 3b 0a 20 20 20  pUCollator);.   
396f6 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
396f7 65 72 72 6f 72 28 70 2c 20 22 45 72 72 6f 72 20  error(p, "Error 
396f8 72 65 67 69 73 74 65 72 69 6e 67 20 63 6f 6c 6c  registering coll
396f9 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 22 2c  ation function",
396fa 20 2d 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   -1);.  }.}../*.
396fb 2a 2a 20 52 65 67 69 73 74 65 72 20 74 68 65 20  ** Register the 
396fc 49 43 55 20 65 78 74 65 6e 73 69 6f 6e 20 66 75  ICU extension fu
396fd 6e 63 74 69 6f 6e 73 20 77 69 74 68 20 64 61 74  nctions with dat
396fe 61 62 61 73 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c  abase db..*/.SQL
396ff 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
39700 73 71 6c 69 74 65 33 49 63 75 49 6e 69 74 28 73  sqlite3IcuInit(s
39701 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 73  qlite3 *db){.  s
39702 74 72 75 63 74 20 49 63 75 53 63 61 6c 61 72 20  truct IcuScalar 
39703 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
39704 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20   *zName;        
39705 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39706 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 6e 61 6d 65  /* Function name
39707 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 41 72 67   */.    int nArg
39708 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
39709 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3970a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
3970b 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20  rguments */.    
3970c 69 6e 74 20 65 6e 63 3b 20 20 20 20 20 20 20 20  int enc;        
3970d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3970e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 74            /* Opt
3970f 69 6d 61 6c 20 74 65 78 74 20 65 6e 63 6f 64 69  imal text encodi
39710 6e 67 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a  ng */.    void *
39711 70 43 6f 6e 74 65 78 74 3b 20 20 20 20 20 20 20  pContext;       
39712 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39713 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 75      /* sqlite3_u
39714 73 65 72 5f 64 61 74 61 28 29 20 63 6f 6e 74 65  ser_data() conte
39715 78 74 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 28  xt */.    void (
39716 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f  *xFunc)(sqlite3_
39717 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
39718 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 0a 20  ite3_value**);. 
39719 20 7d 20 73 63 61 6c 61 72 73 5b 5d 20 3d 20 7b   } scalars[] = {
3971a 0a 20 20 20 20 7b 22 72 65 67 65 78 70 22 2c 2d  .    {"regexp",-
3971b 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 20  1, SQLITE_ANY,  
3971c 20 20 20 20 20 20 20 20 30 2c 20 69 63 75 52 65          0, icuRe
3971d 67 65 78 70 46 75 6e 63 7d 2c 0a 0a 20 20 20 20  gexpFunc},..    
3971e 7b 22 6c 6f 77 65 72 22 2c 20 20 31 2c 20 53 51  {"lower",  1, SQ
3971f 4c 49 54 45 5f 55 54 46 31 36 2c 20 20 20 20 20  LITE_UTF16,     
39720 20 20 20 30 2c 20 69 63 75 43 61 73 65 46 75 6e     0, icuCaseFun
39721 63 31 36 7d 2c 0a 20 20 20 20 7b 22 6c 6f 77 65  c16},.    {"lowe
39722 72 22 2c 20 20 32 2c 20 53 51 4c 49 54 45 5f 55  r",  2, SQLITE_U
39723 54 46 31 36 2c 20 20 20 20 20 20 20 20 30 2c 20  TF16,        0, 
39724 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a  icuCaseFunc16},.
39725 20 20 20 20 7b 22 75 70 70 65 72 22 2c 20 20 31      {"upper",  1
39726 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 2c 20  , SQLITE_UTF16, 
39727 28 76 6f 69 64 2a 29 31 2c 20 69 63 75 43 61 73  (void*)1, icuCas
39728 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22  eFunc16},.    {"
39729 75 70 70 65 72 22 2c 20 20 32 2c 20 53 51 4c 49  upper",  2, SQLI
3972a 54 45 5f 55 54 46 31 36 2c 20 28 76 6f 69 64 2a  TE_UTF16, (void*
3972b 29 31 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31  )1, icuCaseFunc1
3972c 36 7d 2c 0a 0a 20 20 20 20 7b 22 6c 6f 77 65 72  6},..    {"lower
3972d 22 2c 20 20 31 2c 20 53 51 4c 49 54 45 5f 55 54  ",  1, SQLITE_UT
3972e 46 38 2c 20 20 20 20 20 20 20 20 20 30 2c 20 69  F8,         0, i
3972f 63 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20  cuCaseFunc16},. 
39730 20 20 20 7b 22 6c 6f 77 65 72 22 2c 20 20 32 2c     {"lower",  2,
39731 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20   SQLITE_UTF8,   
39732 20 20 20 20 20 20 30 2c 20 69 63 75 43 61 73 65        0, icuCase
39733 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22 75  Func16},.    {"u
39734 70 70 65 72 22 2c 20 20 31 2c 20 53 51 4c 49 54  pper",  1, SQLIT
39735 45 5f 55 54 46 38 2c 20 20 28 76 6f 69 64 2a 29  E_UTF8,  (void*)
39736 31 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36  1, icuCaseFunc16
39737 7d 2c 0a 20 20 20 20 7b 22 75 70 70 65 72 22 2c  },.    {"upper",
39738 20 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38    2, SQLITE_UTF8
39739 2c 20 20 28 76 6f 69 64 2a 29 31 2c 20 69 63 75  ,  (void*)1, icu
3973a 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 0a 20 20  CaseFunc16},..  
3973b 20 20 7b 22 6c 69 6b 65 22 2c 20 20 20 32 2c 20    {"like",   2, 
3973c 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20  SQLITE_UTF8,    
3973d 20 20 20 20 20 30 2c 20 69 63 75 4c 69 6b 65 46       0, icuLikeF
3973e 75 6e 63 7d 2c 0a 20 20 20 20 7b 22 6c 69 6b 65  unc},.    {"like
3973f 22 2c 20 20 20 33 2c 20 53 51 4c 49 54 45 5f 55  ",   3, SQLITE_U
39740 54 46 38 2c 20 20 20 20 20 20 20 20 20 30 2c 20  TF8,         0, 
39741 69 63 75 4c 69 6b 65 46 75 6e 63 7d 2c 0a 0a 20  icuLikeFunc},.. 
39742 20 20 20 7b 22 69 63 75 5f 6c 6f 61 64 5f 63 6f     {"icu_load_co
39743 6c 6c 61 74 69 6f 6e 22 2c 20 20 32 2c 20 53 51  llation",  2, SQ
39744 4c 49 54 45 5f 55 54 46 38 2c 20 28 76 6f 69 64  LITE_UTF8, (void
39745 2a 29 64 62 2c 20 69 63 75 4c 6f 61 64 43 6f 6c  *)db, icuLoadCol
39746 6c 61 74 69 6f 6e 7d 2c 0a 20 20 7d 3b 0a 0a 20  lation},.  };.. 
39747 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
39748 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20  _OK;.  int i;.. 
39749 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51   for(i=0; rc==SQ
3974a 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 28 73 69  LITE_OK && i<(si
3974b 7a 65 6f 66 28 73 63 61 6c 61 72 73 29 2f 73 69  zeof(scalars)/si
3974c 7a 65 6f 66 28 73 74 72 75 63 74 20 49 63 75 53  zeof(struct IcuS
3974d 63 61 6c 61 72 29 29 3b 20 69 2b 2b 29 7b 0a 20  calar)); i++){. 
3974e 20 20 20 73 74 72 75 63 74 20 49 63 75 53 63 61     struct IcuSca
3974f 6c 61 72 20 2a 70 20 3d 20 26 73 63 61 6c 61 72  lar *p = &scalar
39750 73 5b 69 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73  s[i];.    rc = s
39751 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
39752 6e 63 74 69 6f 6e 28 0a 20 20 20 20 20 20 20 20  nction(.        
39753 64 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 70 2d  db, p->zName, p-
39754 3e 6e 41 72 67 2c 20 70 2d 3e 65 6e 63 2c 20 70  >nArg, p->enc, p
39755 2d 3e 70 43 6f 6e 74 65 78 74 2c 20 70 2d 3e 78  ->pContext, p->x
39756 46 75 6e 63 2c 20 30 2c 20 30 0a 20 20 20 20 29  Func, 0, 0.    )
39757 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
39758 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 53 51 4c 49  rc;.}..#if !SQLI
39759 54 45 5f 43 4f 52 45 0a 53 51 4c 49 54 45 5f 41  TE_CORE.SQLITE_A
3975a 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65  PI int sqlite3_e
3975b 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 28 0a 20  xtension_init(. 
3975c 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20   sqlite3 *db, . 
3975d 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67   char **pzErrMsg
3975e 2c 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ,.  const sqlite
3975f 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 2a  3_api_routines *
39760 70 41 70 69 0a 29 7b 0a 20 20 53 51 4c 49 54 45  pApi.){.  SQLITE
39761 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 32  _EXTENSION_INIT2
39762 28 70 41 70 69 29 0a 20 20 72 65 74 75 72 6e 20  (pApi).  return 
39763 73 71 6c 69 74 65 33 49 63 75 49 6e 69 74 28 64  sqlite3IcuInit(d
39764 62 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 65  b);.}.#endif..#e
39765 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
39766 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 69 63 75  ***** End of icu
39767 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
39768 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39769 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3976a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
3976b 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
3976c 20 66 74 73 33 5f 69 63 75 2e 63 20 2a 2a 2a 2a   fts3_icu.c ****
3976d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3976e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3976f 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
39770 20 4a 75 6e 65 20 32 32 0a 2a 2a 0a 2a 2a 20 54   June 22.**.** T
39771 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
39772 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
39773 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
39774 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
39775 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
39776 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
39777 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
39778 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
39779 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
3977a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
3977b 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
3977c 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
3977d 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
3977e 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
3977f 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
39780 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
39781 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
39782 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39783 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39784 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39785 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39786 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
39787 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  file implements 
39788 61 20 74 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20  a tokenizer for 
39789 66 74 73 33 20 62 61 73 65 64 20 6f 6e 20 74 68  fts3 based on th
3978a 65 20 49 43 55 20 6c 69 62 72 61 72 79 2e 0a 2a  e ICU library..*
3978b 2a 20 0a 2a 2a 20 24 49 64 3a 20 66 74 73 33 5f  * .** $Id: fts3_
3978c 69 63 75 2e 63 2c 76 20 31 2e 33 20 32 30 30 38  icu.c,v 1.3 2008
3978d 2f 30 39 2f 30 31 20 31 38 3a 33 34 3a 32 30 20  /09/01 18:34:20 
3978e 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
3978f 24 0a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e  $.*/..#if !defin
39790 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20  ed(SQLITE_CORE) 
39791 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
39792 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 0a 23  E_ENABLE_FTS3).#
39793 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
39794 42 4c 45 5f 49 43 55 0a 0a 0a 23 69 6e 63 6c 75  BLE_ICU...#inclu
39795 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75 62 72 6b  de <unicode/ubrk
39796 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e  .h>.#include <un
39797 69 63 6f 64 65 2f 75 74 66 31 36 2e 68 3e 0a 0a  icode/utf16.h>..
39798 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49  typedef struct I
39799 63 75 54 6f 6b 65 6e 69 7a 65 72 20 49 63 75 54  cuTokenizer IcuT
3979a 6f 6b 65 6e 69 7a 65 72 3b 0a 74 79 70 65 64 65  okenizer;.typede
3979b 66 20 73 74 72 75 63 74 20 49 63 75 43 75 72 73  f struct IcuCurs
3979c 6f 72 20 49 63 75 43 75 72 73 6f 72 3b 0a 0a 73  or IcuCursor;..s
3979d 74 72 75 63 74 20 49 63 75 54 6f 6b 65 6e 69 7a  truct IcuTokeniz
3979e 65 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74  er {.  sqlite3_t
3979f 6f 6b 65 6e 69 7a 65 72 20 62 61 73 65 3b 0a 20  okenizer base;. 
397a0 20 63 68 61 72 20 2a 7a 4c 6f 63 61 6c 65 3b 0a   char *zLocale;.
397a1 7d 3b 0a 0a 73 74 72 75 63 74 20 49 63 75 43 75  };..struct IcuCu
397a2 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33  rsor {.  sqlite3
397a3 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
397a4 72 20 62 61 73 65 3b 0a 0a 20 20 55 42 72 65 61  r base;..  UBrea
397a5 6b 49 74 65 72 61 74 6f 72 20 2a 70 49 74 65 72  kIterator *pIter
397a6 3b 20 20 20 20 20 20 2f 2a 20 49 43 55 20 62 72  ;      /* ICU br
397a7 65 61 6b 2d 69 74 65 72 61 74 6f 72 20 6f 62 6a  eak-iterator obj
397a8 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68  ect */.  int nCh
397a9 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ar;             
397aa 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
397ab 66 20 55 43 68 61 72 20 65 6c 65 6d 65 6e 74 73  f UChar elements
397ac 20 69 6e 20 70 49 6e 70 75 74 20 2a 2f 0a 20 20   in pInput */.  
397ad 55 43 68 61 72 20 2a 61 43 68 61 72 3b 20 20 20  UChar *aChar;   
397ae 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
397af 6f 70 79 20 6f 66 20 69 6e 70 75 74 20 75 73 69  opy of input usi
397b0 6e 67 20 75 74 66 2d 31 36 20 65 6e 63 6f 64 69  ng utf-16 encodi
397b1 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 4f 66  ng */.  int *aOf
397b2 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20  fset;           
397b3 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 73 20 6f      /* Offsets o
397b4 66 20 65 61 63 68 20 63 68 61 72 61 63 74 65 72  f each character
397b5 20 69 6e 20 75 74 66 2d 38 20 69 6e 70 75 74 20   in utf-8 input 
397b6 2a 2f 0a 0a 20 20 69 6e 74 20 6e 42 75 66 66 65  */..  int nBuffe
397b7 72 3b 0a 20 20 63 68 61 72 20 2a 7a 42 75 66 66  r;.  char *zBuff
397b8 65 72 3b 0a 0a 20 20 69 6e 74 20 69 54 6f 6b 65  er;..  int iToke
397b9 6e 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  n;.};../*.** Cre
397ba 61 74 65 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69  ate a new tokeni
397bb 7a 65 72 20 69 6e 73 74 61 6e 63 65 2e 0a 2a 2f  zer instance..*/
397bc 0a 73 74 61 74 69 63 20 69 6e 74 20 69 63 75 43  .static int icuC
397bd 72 65 61 74 65 28 0a 20 20 69 6e 74 20 61 72 67  reate(.  int arg
397be 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c,              
397bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
397c0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
397c1 65 73 20 69 6e 20 61 72 67 76 5b 5d 20 2a 2f 0a  es in argv[] */.
397c2 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63    const char * c
397c3 6f 6e 73 74 20 2a 61 72 67 76 2c 20 20 20 20 20  onst *argv,     
397c4 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69         /* Tokeni
397c5 7a 65 72 20 63 72 65 61 74 69 6f 6e 20 61 72 67  zer creation arg
397c6 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 73 71 6c 69  uments */.  sqli
397c7 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a  te3_tokenizer **
397c8 70 70 54 6f 6b 65 6e 69 7a 65 72 20 20 20 20 20  ppTokenizer     
397c9 20 2f 2a 20 4f 55 54 3a 20 43 72 65 61 74 65 64   /* OUT: Created
397ca 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 29 7b   tokenizer */.){
397cb 0a 20 20 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20  .  IcuTokenizer 
397cc 2a 70 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b  *p;.  int n = 0;
397cd 0a 0a 20 20 69 66 28 20 61 72 67 63 3e 30 20 29  ..  if( argc>0 )
397ce 7b 0a 20 20 20 20 6e 20 3d 20 73 74 72 6c 65 6e  {.    n = strlen
397cf 28 61 72 67 76 5b 30 5d 29 2b 31 3b 0a 20 20 7d  (argv[0])+1;.  }
397d0 0a 20 20 70 20 3d 20 28 49 63 75 54 6f 6b 65 6e  .  p = (IcuToken
397d1 69 7a 65 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d  izer *)sqlite3_m
397d2 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 49 63 75  alloc(sizeof(Icu
397d3 54 6f 6b 65 6e 69 7a 65 72 29 2b 6e 29 3b 0a 20  Tokenizer)+n);. 
397d4 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72   if( !p ){.    r
397d5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
397d6 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74  EM;.  }.  memset
397d7 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 49 63  (p, 0, sizeof(Ic
397d8 75 54 6f 6b 65 6e 69 7a 65 72 29 29 3b 0a 0a 20  uTokenizer));.. 
397d9 20 69 66 28 20 6e 20 29 7b 0a 20 20 20 20 70 2d   if( n ){.    p-
397da 3e 7a 4c 6f 63 61 6c 65 20 3d 20 28 63 68 61 72  >zLocale = (char
397db 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 20 6d 65   *)&p[1];.    me
397dc 6d 63 70 79 28 70 2d 3e 7a 4c 6f 63 61 6c 65 2c  mcpy(p->zLocale,
397dd 20 61 72 67 76 5b 30 5d 2c 20 6e 29 3b 0a 20 20   argv[0], n);.  
397de 7d 0a 0a 20 20 2a 70 70 54 6f 6b 65 6e 69 7a 65  }..  *ppTokenize
397df 72 20 3d 20 28 73 71 6c 69 74 65 33 5f 74 6f 6b  r = (sqlite3_tok
397e0 65 6e 69 7a 65 72 20 2a 29 70 3b 0a 0a 20 20 72  enizer *)p;..  r
397e1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
397e2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f  .}../*.** Destro
397e3 79 20 61 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f  y a tokenizer.*/
397e4 0a 73 74 61 74 69 63 20 69 6e 74 20 69 63 75 44  .static int icuD
397e5 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33 5f 74  estroy(sqlite3_t
397e6 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
397e7 69 7a 65 72 29 7b 0a 20 20 49 63 75 54 6f 6b 65  izer){.  IcuToke
397e8 6e 69 7a 65 72 20 2a 70 20 3d 20 28 49 63 75 54  nizer *p = (IcuT
397e9 6f 6b 65 6e 69 7a 65 72 20 2a 29 70 54 6f 6b 65  okenizer *)pToke
397ea 6e 69 7a 65 72 3b 0a 20 20 73 71 6c 69 74 65 33  nizer;.  sqlite3
397eb 5f 66 72 65 65 28 70 29 3b 0a 20 20 72 65 74 75  _free(p);.  retu
397ec 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
397ed 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 74  ./*.** Prepare t
397ee 6f 20 62 65 67 69 6e 20 74 6f 6b 65 6e 69 7a 69  o begin tokenizi
397ef 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  ng a particular 
397f0 73 74 72 69 6e 67 2e 20 20 54 68 65 20 69 6e 70  string.  The inp
397f1 75 74 0a 2a 2a 20 73 74 72 69 6e 67 20 74 6f 20  ut.** string to 
397f2 62 65 20 74 6f 6b 65 6e 69 7a 65 64 20 69 73 20  be tokenized is 
397f3 70 49 6e 70 75 74 5b 30 2e 2e 6e 42 79 74 65 73  pInput[0..nBytes
397f4 2d 31 5d 2e 20 20 41 20 63 75 72 73 6f 72 0a 2a  -1].  A cursor.*
397f5 2a 20 75 73 65 64 20 74 6f 20 69 6e 63 72 65 6d  * used to increm
397f6 65 6e 74 61 6c 6c 79 20 74 6f 6b 65 6e 69 7a 65  entally tokenize
397f7 20 74 68 69 73 20 73 74 72 69 6e 67 20 69 73 20   this string is 
397f8 72 65 74 75 72 6e 65 64 20 69 6e 20 0a 2a 2a 20  returned in .** 
397f9 2a 70 70 43 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74  *ppCursor..*/.st
397fa 61 74 69 63 20 69 6e 74 20 69 63 75 4f 70 65 6e  atic int icuOpen
397fb 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  (.  sqlite3_toke
397fc 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65  nizer *pTokenize
397fd 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  r,         /* Th
397fe 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20  e tokenizer */. 
397ff 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e   const char *zIn
39800 70 75 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  put,            
39801 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74          /* Input
39802 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 69 6e 74   string */.  int
39803 20 6e 49 6e 70 75 74 2c 20 20 20 20 20 20 20 20   nInput,        
39804 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39805 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
39806 20 7a 49 6e 70 75 74 20 69 6e 20 62 79 74 65 73   zInput in bytes
39807 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f   */.  sqlite3_to
39808 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
39809 2a 70 70 43 75 72 73 6f 72 20 20 20 20 2f 2a 20  *ppCursor    /* 
3980a 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a 61 74 69 6f  OUT: Tokenizatio
3980b 6e 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20  n cursor */.){. 
3980c 20 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 70   IcuTokenizer *p
3980d 20 3d 20 28 49 63 75 54 6f 6b 65 6e 69 7a 65 72   = (IcuTokenizer
3980e 20 2a 29 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20   *)pTokenizer;. 
3980f 20 49 63 75 43 75 72 73 6f 72 20 2a 70 43 73 72   IcuCursor *pCsr
39810 3b 0a 0a 20 20 63 6f 6e 73 74 20 69 6e 74 33 32  ;..  const int32
39811 5f 74 20 6f 70 74 20 3d 20 55 5f 46 4f 4c 44 5f  _t opt = U_FOLD_
39812 43 41 53 45 5f 44 45 46 41 55 4c 54 3b 0a 20 20  CASE_DEFAULT;.  
39813 55 45 72 72 6f 72 43 6f 64 65 20 73 74 61 74 75  UErrorCode statu
39814 73 20 3d 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52  s = U_ZERO_ERROR
39815 3b 0a 20 20 69 6e 74 20 6e 43 68 61 72 3b 0a 0a  ;.  int nChar;..
39816 20 20 55 43 68 61 72 33 32 20 63 3b 0a 20 20 69    UChar32 c;.  i
39817 6e 74 20 69 49 6e 70 75 74 20 3d 20 30 3b 0a 20  nt iInput = 0;. 
39818 20 69 6e 74 20 69 4f 75 74 20 3d 20 30 3b 0a 0a   int iOut = 0;..
39819 20 20 2a 70 70 43 75 72 73 6f 72 20 3d 20 30 3b    *ppCursor = 0;
3981a 0a 0a 20 20 69 66 28 20 6e 49 6e 70 75 74 3c 30  ..  if( nInput<0
3981b 20 29 7b 0a 20 20 20 20 6e 49 6e 70 75 74 20 3d   ){.    nInput =
3981c 20 73 74 72 6c 65 6e 28 7a 49 6e 70 75 74 29 3b   strlen(zInput);
3981d 0a 20 20 7d 0a 20 20 6e 43 68 61 72 20 3d 20 6e  .  }.  nChar = n
3981e 49 6e 70 75 74 2b 31 3b 0a 20 20 70 43 73 72 20  Input+1;.  pCsr 
3981f 3d 20 28 49 63 75 43 75 72 73 6f 72 20 2a 29 73  = (IcuCursor *)s
39820 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 0a 20  qlite3_malloc(. 
39821 20 20 20 20 20 73 69 7a 65 6f 66 28 49 63 75 43       sizeof(IcuC
39822 75 72 73 6f 72 29 20 2b 20 20 20 20 20 20 20 20  ursor) +        
39823 20 20 20 20 20 20 20 20 2f 2a 20 49 63 75 43 75          /* IcuCu
39824 72 73 6f 72 20 2a 2f 0a 20 20 20 20 20 20 6e 43  rsor */.      nC
39825 68 61 72 20 2a 20 73 69 7a 65 6f 66 28 55 43 68  har * sizeof(UCh
39826 61 72 29 20 2b 20 20 20 20 20 20 20 20 20 20 20  ar) +           
39827 20 2f 2a 20 49 63 75 43 75 72 73 6f 72 2e 61 43   /* IcuCursor.aC
39828 68 61 72 5b 5d 20 2a 2f 0a 20 20 20 20 20 20 28  har[] */.      (
39829 6e 43 68 61 72 2b 31 29 20 2a 20 73 69 7a 65 6f  nChar+1) * sizeo
3982a 66 28 69 6e 74 29 20 20 20 20 20 20 20 20 20 20  f(int)          
3982b 20 20 2f 2a 20 49 63 75 43 75 72 73 6f 72 2e 61    /* IcuCursor.a
3982c 4f 66 66 73 65 74 5b 5d 20 2a 2f 0a 20 20 29 3b  Offset[] */.  );
3982d 0a 20 20 69 66 28 20 21 70 43 73 72 20 29 7b 0a  .  if( !pCsr ){.
3982e 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
3982f 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d  E_NOMEM;.  }.  m
39830 65 6d 73 65 74 28 70 43 73 72 2c 20 30 2c 20 73  emset(pCsr, 0, s
39831 69 7a 65 6f 66 28 49 63 75 43 75 72 73 6f 72 29  izeof(IcuCursor)
39832 29 3b 0a 20 20 70 43 73 72 2d 3e 61 43 68 61 72  );.  pCsr->aChar
39833 20 3d 20 28 55 43 68 61 72 20 2a 29 26 70 43 73   = (UChar *)&pCs
39834 72 5b 31 5d 3b 0a 20 20 70 43 73 72 2d 3e 61 4f  r[1];.  pCsr->aO
39835 66 66 73 65 74 20 3d 20 28 69 6e 74 20 2a 29 26  ffset = (int *)&
39836 70 43 73 72 2d 3e 61 43 68 61 72 5b 6e 43 68 61  pCsr->aChar[nCha
39837 72 5d 3b 0a 0a 20 20 70 43 73 72 2d 3e 61 4f 66  r];..  pCsr->aOf
39838 66 73 65 74 5b 69 4f 75 74 5d 20 3d 20 69 49 6e  fset[iOut] = iIn
39839 70 75 74 3b 0a 20 20 55 38 5f 4e 45 58 54 28 7a  put;.  U8_NEXT(z
3983a 49 6e 70 75 74 2c 20 69 49 6e 70 75 74 2c 20 6e  Input, iInput, n
3983b 49 6e 70 75 74 2c 20 63 29 3b 20 0a 20 20 77 68  Input, c); .  wh
3983c 69 6c 65 28 20 63 3e 30 20 29 7b 0a 20 20 20 20  ile( c>0 ){.    
3983d 69 6e 74 20 69 73 45 72 72 6f 72 20 3d 20 30 3b  int isError = 0;
3983e 0a 20 20 20 20 63 20 3d 20 75 5f 66 6f 6c 64 43  .    c = u_foldC
3983f 61 73 65 28 63 2c 20 6f 70 74 29 3b 0a 20 20 20  ase(c, opt);.   
39840 20 55 31 36 5f 41 50 50 45 4e 44 28 70 43 73 72   U16_APPEND(pCsr
39841 2d 3e 61 43 68 61 72 2c 20 69 4f 75 74 2c 20 6e  ->aChar, iOut, n
39842 43 68 61 72 2c 20 63 2c 20 69 73 45 72 72 6f 72  Char, c, isError
39843 29 3b 0a 20 20 20 20 69 66 28 20 69 73 45 72 72  );.    if( isErr
39844 6f 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  or ){.      sqli
39845 74 65 33 5f 66 72 65 65 28 70 43 73 72 29 3b 0a  te3_free(pCsr);.
39846 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
39847 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d  ITE_ERROR;.    }
39848 0a 20 20 20 20 70 43 73 72 2d 3e 61 4f 66 66 73  .    pCsr->aOffs
39849 65 74 5b 69 4f 75 74 5d 20 3d 20 69 49 6e 70 75  et[iOut] = iInpu
3984a 74 3b 0a 0a 20 20 20 20 69 66 28 20 69 49 6e 70  t;..    if( iInp
3984b 75 74 3c 6e 49 6e 70 75 74 20 29 7b 0a 20 20 20  ut<nInput ){.   
3984c 20 20 20 55 38 5f 4e 45 58 54 28 7a 49 6e 70 75     U8_NEXT(zInpu
3984d 74 2c 20 69 49 6e 70 75 74 2c 20 6e 49 6e 70 75  t, iInput, nInpu
3984e 74 2c 20 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65  t, c);.    }else
3984f 7b 0a 20 20 20 20 20 20 63 20 3d 20 30 3b 0a 20  {.      c = 0;. 
39850 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 43 73 72     }.  }..  pCsr
39851 2d 3e 70 49 74 65 72 20 3d 20 75 62 72 6b 5f 6f  ->pIter = ubrk_o
39852 70 65 6e 28 55 42 52 4b 5f 57 4f 52 44 2c 20 70  pen(UBRK_WORD, p
39853 2d 3e 7a 4c 6f 63 61 6c 65 2c 20 70 43 73 72 2d  ->zLocale, pCsr-
39854 3e 61 43 68 61 72 2c 20 69 4f 75 74 2c 20 26 73  >aChar, iOut, &s
39855 74 61 74 75 73 29 3b 0a 20 20 69 66 28 20 21 55  tatus);.  if( !U
39856 5f 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29  _SUCCESS(status)
39857 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
39858 66 72 65 65 28 70 43 73 72 29 3b 0a 20 20 20 20  free(pCsr);.    
39859 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
3985a 52 4f 52 3b 0a 20 20 7d 0a 20 20 70 43 73 72 2d  ROR;.  }.  pCsr-
3985b 3e 6e 43 68 61 72 20 3d 20 69 4f 75 74 3b 0a 0a  >nChar = iOut;..
3985c 20 20 75 62 72 6b 5f 66 69 72 73 74 28 70 43 73    ubrk_first(pCs
3985d 72 2d 3e 70 49 74 65 72 29 3b 0a 20 20 2a 70 70  r->pIter);.  *pp
3985e 43 75 72 73 6f 72 20 3d 20 28 73 71 6c 69 74 65  Cursor = (sqlite
3985f 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
39860 6f 72 20 2a 29 70 43 73 72 3b 0a 20 20 72 65 74  or *)pCsr;.  ret
39861 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
39862 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20  ../*.** Close a 
39863 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72  tokenization cur
39864 73 6f 72 20 70 72 65 76 69 6f 75 73 6c 79 20 6f  sor previously o
39865 70 65 6e 65 64 20 62 79 20 61 20 63 61 6c 6c 20  pened by a call 
39866 74 6f 20 69 63 75 4f 70 65 6e 28 29 2e 0a 2a 2f  to icuOpen()..*/
39867 0a 73 74 61 74 69 63 20 69 6e 74 20 69 63 75 43  .static int icuC
39868 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b  lose(sqlite3_tok
39869 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70  enizer_cursor *p
3986a 43 75 72 73 6f 72 29 7b 0a 20 20 49 63 75 43 75  Cursor){.  IcuCu
3986b 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 49 63  rsor *pCsr = (Ic
3986c 75 43 75 72 73 6f 72 20 2a 29 70 43 75 72 73 6f  uCursor *)pCurso
3986d 72 3b 0a 20 20 75 62 72 6b 5f 63 6c 6f 73 65 28  r;.  ubrk_close(
3986e 70 43 73 72 2d 3e 70 49 74 65 72 29 3b 0a 20 20  pCsr->pIter);.  
3986f 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73  sqlite3_free(pCs
39870 72 2d 3e 7a 42 75 66 66 65 72 29 3b 0a 20 20 73  r->zBuffer);.  s
39871 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73 72  qlite3_free(pCsr
39872 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
39873 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
39874 45 78 74 72 61 63 74 20 74 68 65 20 6e 65 78 74  Extract the next
39875 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 61 20 74 6f   token from a to
39876 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f  kenization curso
39877 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
39878 20 69 63 75 4e 65 78 74 28 0a 20 20 73 71 6c 69   icuNext(.  sqli
39879 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
3987a 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 20  rsor *pCursor,  
3987b 2f 2a 20 43 75 72 73 6f 72 20 72 65 74 75 72 6e  /* Cursor return
3987c 65 64 20 62 79 20 73 69 6d 70 6c 65 4f 70 65 6e  ed by simpleOpen
3987d 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
3987e 20 2a 2a 70 70 54 6f 6b 65 6e 2c 20 20 20 20 20   **ppToken,     
3987f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
39880 3a 20 2a 70 70 54 6f 6b 65 6e 20 69 73 20 74 68  : *ppToken is th
39881 65 20 74 6f 6b 65 6e 20 74 65 78 74 20 2a 2f 0a  e token text */.
39882 20 20 69 6e 74 20 2a 70 6e 42 79 74 65 73 2c 20    int *pnBytes, 
39883 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39884 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75        /* OUT: Nu
39885 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
39886 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20   token */.  int 
39887 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20  *piStartOffset, 
39888 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39889 2f 2a 20 4f 55 54 3a 20 53 74 61 72 74 69 6e 67  /* OUT: Starting
3988a 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e   offset of token
3988b 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 45 6e 64   */.  int *piEnd
3988c 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20  Offset,         
3988d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
3988e 3a 20 45 6e 64 69 6e 67 20 6f 66 66 73 65 74 20  : Ending offset 
3988f 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e  of token */.  in
39890 74 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 20 20  t *piPosition   
39891 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39892 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 73 69 74 69    /* OUT: Positi
39893 6f 6e 20 69 6e 74 65 67 65 72 20 6f 66 20 74 6f  on integer of to
39894 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 49 63 75 43  ken */.){.  IcuC
39895 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 49  ursor *pCsr = (I
39896 63 75 43 75 72 73 6f 72 20 2a 29 70 43 75 72 73  cuCursor *)pCurs
39897 6f 72 3b 0a 0a 20 20 69 6e 74 20 69 53 74 61 72  or;..  int iStar
39898 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 45 6e  t = 0;.  int iEn
39899 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 42 79  d = 0;.  int nBy
3989a 74 65 20 3d 20 30 3b 0a 0a 20 20 77 68 69 6c 65  te = 0;..  while
3989b 28 20 69 53 74 61 72 74 3d 3d 69 45 6e 64 20 29  ( iStart==iEnd )
3989c 7b 0a 20 20 20 20 55 43 68 61 72 33 32 20 63 3b  {.    UChar32 c;
3989d 0a 0a 20 20 20 20 69 53 74 61 72 74 20 3d 20 75  ..    iStart = u
3989e 62 72 6b 5f 63 75 72 72 65 6e 74 28 70 43 73 72  brk_current(pCsr
3989f 2d 3e 70 49 74 65 72 29 3b 0a 20 20 20 20 69 45  ->pIter);.    iE
398a0 6e 64 20 3d 20 75 62 72 6b 5f 6e 65 78 74 28 70  nd = ubrk_next(p
398a1 43 73 72 2d 3e 70 49 74 65 72 29 3b 0a 20 20 20  Csr->pIter);.   
398a2 20 69 66 28 20 69 45 6e 64 3d 3d 55 42 52 4b 5f   if( iEnd==UBRK_
398a3 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 72 65  DONE ){.      re
398a4 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45  turn SQLITE_DONE
398a5 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 77 68 69  ;.    }..    whi
398a6 6c 65 28 20 69 53 74 61 72 74 3c 69 45 6e 64 20  le( iStart<iEnd 
398a7 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 57 68  ){.      int iWh
398a8 69 74 65 20 3d 20 69 53 74 61 72 74 3b 0a 20 20  ite = iStart;.  
398a9 20 20 20 20 55 38 5f 4e 45 58 54 28 70 43 73 72      U8_NEXT(pCsr
398aa 2d 3e 61 43 68 61 72 2c 20 69 57 68 69 74 65 2c  ->aChar, iWhite,
398ab 20 70 43 73 72 2d 3e 6e 43 68 61 72 2c 20 63 29   pCsr->nChar, c)
398ac 3b 0a 20 20 20 20 20 20 69 66 28 20 75 5f 69 73  ;.      if( u_is
398ad 73 70 61 63 65 28 63 29 20 29 7b 0a 20 20 20 20  space(c) ){.    
398ae 20 20 20 20 69 53 74 61 72 74 20 3d 20 69 57 68      iStart = iWh
398af 69 74 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ite;.      }else
398b0 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  {.        break;
398b1 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
398b2 20 20 20 61 73 73 65 72 74 28 69 53 74 61 72 74     assert(iStart
398b3 3c 3d 69 45 6e 64 29 3b 0a 20 20 7d 0a 0a 20 20  <=iEnd);.  }..  
398b4 64 6f 20 7b 0a 20 20 20 20 55 45 72 72 6f 72 43  do {.    UErrorC
398b5 6f 64 65 20 73 74 61 74 75 73 20 3d 20 55 5f 5a  ode status = U_Z
398b6 45 52 4f 5f 45 52 52 4f 52 3b 0a 20 20 20 20 69  ERO_ERROR;.    i
398b7 66 28 20 6e 42 79 74 65 20 29 7b 0a 20 20 20 20  f( nByte ){.    
398b8 20 20 63 68 61 72 20 2a 7a 4e 65 77 20 3d 20 73    char *zNew = s
398b9 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70  qlite3_realloc(p
398ba 43 73 72 2d 3e 7a 42 75 66 66 65 72 2c 20 6e 42  Csr->zBuffer, nB
398bb 79 74 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  yte);.      if( 
398bc 21 7a 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20  !zNew ){.       
398bd 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
398be 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  OMEM;.      }.  
398bf 20 20 20 20 70 43 73 72 2d 3e 7a 42 75 66 66 65      pCsr->zBuffe
398c0 72 20 3d 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20  r = zNew;.      
398c1 70 43 73 72 2d 3e 6e 42 75 66 66 65 72 20 3d 20  pCsr->nBuffer = 
398c2 6e 42 79 74 65 3b 0a 20 20 20 20 7d 0a 0a 20 20  nByte;.    }..  
398c3 20 20 75 5f 73 74 72 54 6f 55 54 46 38 28 0a 20    u_strToUTF8(. 
398c4 20 20 20 20 20 20 20 70 43 73 72 2d 3e 7a 42 75         pCsr->zBu
398c5 66 66 65 72 2c 20 70 43 73 72 2d 3e 6e 42 75 66  ffer, pCsr->nBuf
398c6 66 65 72 2c 20 26 6e 42 79 74 65 2c 20 20 20 20  fer, &nByte,    
398c7 2f 2a 20 4f 75 74 70 75 74 20 76 61 72 73 20 2a  /* Output vars *
398c8 2f 0a 20 20 20 20 20 20 20 20 26 70 43 73 72 2d  /.        &pCsr-
398c9 3e 61 43 68 61 72 5b 69 53 74 61 72 74 5d 2c 20  >aChar[iStart], 
398ca 69 45 6e 64 2d 69 53 74 61 72 74 2c 20 20 20 20  iEnd-iStart,    
398cb 20 20 20 2f 2a 20 49 6e 70 75 74 20 76 61 72 73     /* Input vars
398cc 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 73 74 61   */.        &sta
398cd 74 75 73 20 20 20 20 20 20 20 20 20 20 20 20 20  tus             
398ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
398cf 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 73       /* Output s
398d0 75 63 63 65 73 73 2f 66 61 69 6c 75 72 65 20 2a  uccess/failure *
398d1 2f 0a 20 20 20 20 29 3b 0a 20 20 7d 20 77 68 69  /.    );.  } whi
398d2 6c 65 28 20 6e 42 79 74 65 3e 70 43 73 72 2d 3e  le( nByte>pCsr->
398d3 6e 42 75 66 66 65 72 20 29 3b 0a 0a 20 20 2a 70  nBuffer );..  *p
398d4 70 54 6f 6b 65 6e 20 3d 20 70 43 73 72 2d 3e 7a  pToken = pCsr->z
398d5 42 75 66 66 65 72 3b 0a 20 20 2a 70 6e 42 79 74  Buffer;.  *pnByt
398d6 65 73 20 3d 20 6e 42 79 74 65 3b 0a 20 20 2a 70  es = nByte;.  *p
398d7 69 53 74 61 72 74 4f 66 66 73 65 74 20 3d 20 70  iStartOffset = p
398d8 43 73 72 2d 3e 61 4f 66 66 73 65 74 5b 69 53 74  Csr->aOffset[iSt
398d9 61 72 74 5d 3b 0a 20 20 2a 70 69 45 6e 64 4f 66  art];.  *piEndOf
398da 66 73 65 74 20 3d 20 70 43 73 72 2d 3e 61 4f 66  fset = pCsr->aOf
398db 66 73 65 74 5b 69 45 6e 64 5d 3b 0a 20 20 2a 70  fset[iEnd];.  *p
398dc 69 50 6f 73 69 74 69 6f 6e 20 3d 20 70 43 73 72  iPosition = pCsr
398dd 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a 0a 20 20 72  ->iToken++;..  r
398de 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
398df 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65  .}../*.** The se
398e0 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 74 68  t of routines th
398e1 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  at implement the
398e2 20 73 69 6d 70 6c 65 20 74 6f 6b 65 6e 69 7a 65   simple tokenize
398e3 72 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  r.*/.static cons
398e4 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
398e5 7a 65 72 5f 6d 6f 64 75 6c 65 20 69 63 75 54 6f  zer_module icuTo
398e6 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 20 3d 20  kenizerModule = 
398e7 7b 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  {.  0,          
398e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
398e9 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a   /* iVersion */.
398ea 20 20 69 63 75 43 72 65 61 74 65 2c 20 20 20 20    icuCreate,    
398eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
398ec 2a 20 78 43 72 65 61 74 65 20 20 2a 2f 0a 20 20  * xCreate  */.  
398ed 69 63 75 44 65 73 74 72 6f 79 2c 20 20 20 20 20  icuDestroy,     
398ee 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
398ef 78 43 72 65 61 74 65 20 20 2a 2f 0a 20 20 69 63  xCreate  */.  ic
398f0 75 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20  uOpen,          
398f1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f             /* xO
398f2 70 65 6e 20 20 20 20 2a 2f 0a 20 20 69 63 75 43  pen    */.  icuC
398f3 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  lose,           
398f4 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f           /* xClo
398f5 73 65 20 20 20 2a 2f 0a 20 20 69 63 75 4e 65 78  se   */.  icuNex
398f6 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
398f7 20 20 20 20 20 20 20 2f 2a 20 78 4e 65 78 74 20         /* xNext 
398f8 20 20 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20     */.};../*.** 
398f9 53 65 74 20 2a 70 70 4d 6f 64 75 6c 65 20 74 6f  Set *ppModule to
398fa 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 69 6d   point at the im
398fb 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
398fc 74 68 65 20 49 43 55 20 74 6f 6b 65 6e 69 7a 65  the ICU tokenize
398fd 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
398fe 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
398ff 33 46 74 73 33 49 63 75 54 6f 6b 65 6e 69 7a 65  3Fts3IcuTokenize
39900 72 4d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74  rModule(.  sqlit
39901 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
39902 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64  ule const**ppMod
39903 75 6c 65 0a 29 7b 0a 20 20 2a 70 70 4d 6f 64 75  ule.){.  *ppModu
39904 6c 65 20 3d 20 26 69 63 75 54 6f 6b 65 6e 69 7a  le = &icuTokeniz
39905 65 72 4d 6f 64 75 6c 65 3b 0a 7d 0a 0a 23 65 6e  erModule;.}..#en
39906 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53  dif /* defined(S
39907 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55  QLITE_ENABLE_ICU
39908 29 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 21  ) */.#endif /* !
39909 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43  defined(SQLITE_C
3990a 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  ORE) || defined(
3990b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
3990c 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  S3) */../*******
3990d 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66  ******* End of f
3990e 74 73 33 5f 69 63 75 2e 63 20 2a 2a 2a 2a 2a 2a  ts3_icu.c ******
3990f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39911 2a 2a 2a 2a 2a 2a 2f 0a                          ******/.